tcmodules.pas 891 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702237032370423705237062370723708237092371023711237122371323714237152371623717237182371923720237212372223723237242372523726237272372823729237302373123732237332373423735237362373723738237392374023741237422374323744237452374623747237482374923750237512375223753237542375523756237572375823759237602376123762237632376423765237662376723768237692377023771237722377323774237752377623777237782377923780237812378223783237842378523786237872378823789237902379123792237932379423795237962379723798237992380023801238022380323804238052380623807238082380923810238112381223813238142381523816238172381823819238202382123822238232382423825238262382723828238292383023831238322383323834238352383623837238382383923840238412384223843238442384523846238472384823849238502385123852238532385423855238562385723858238592386023861238622386323864238652386623867238682386923870238712387223873238742387523876238772387823879238802388123882238832388423885238862388723888238892389023891238922389323894238952389623897238982389923900239012390223903239042390523906239072390823909239102391123912239132391423915239162391723918239192392023921239222392323924239252392623927239282392923930239312393223933239342393523936239372393823939239402394123942239432394423945239462394723948239492395023951239522395323954239552395623957239582395923960239612396223963239642396523966239672396823969239702397123972239732397423975239762397723978239792398023981239822398323984239852398623987239882398923990239912399223993239942399523996239972399823999240002400124002240032400424005240062400724008240092401024011240122401324014240152401624017240182401924020240212402224023240242402524026240272402824029240302403124032240332403424035240362403724038240392404024041240422404324044240452404624047240482404924050240512405224053240542405524056240572405824059240602406124062240632406424065240662406724068240692407024071240722407324074240752407624077240782407924080240812408224083240842408524086240872408824089240902409124092240932409424095240962409724098240992410024101241022410324104241052410624107241082410924110241112411224113241142411524116241172411824119241202412124122241232412424125241262412724128241292413024131241322413324134241352413624137241382413924140241412414224143241442414524146241472414824149241502415124152241532415424155241562415724158241592416024161241622416324164241652416624167241682416924170241712417224173241742417524176241772417824179241802418124182241832418424185241862418724188241892419024191241922419324194241952419624197241982419924200242012420224203242042420524206242072420824209242102421124212242132421424215242162421724218242192422024221242222422324224242252422624227242282422924230242312423224233242342423524236242372423824239242402424124242242432424424245242462424724248242492425024251242522425324254242552425624257242582425924260242612426224263242642426524266242672426824269242702427124272242732427424275242762427724278242792428024281242822428324284242852428624287242882428924290242912429224293242942429524296242972429824299243002430124302243032430424305243062430724308243092431024311243122431324314243152431624317243182431924320243212432224323243242432524326243272432824329243302433124332243332433424335243362433724338243392434024341243422434324344243452434624347243482434924350243512435224353243542435524356243572435824359243602436124362243632436424365243662436724368243692437024371243722437324374243752437624377243782437924380243812438224383243842438524386243872438824389243902439124392243932439424395243962439724398243992440024401244022440324404244052440624407244082440924410244112441224413244142441524416244172441824419244202442124422244232442424425244262442724428244292443024431244322443324434244352443624437244382443924440244412444224443244442444524446244472444824449244502445124452244532445424455244562445724458244592446024461244622446324464244652446624467244682446924470244712447224473244742447524476244772447824479244802448124482244832448424485244862448724488244892449024491244922449324494244952449624497244982449924500245012450224503245042450524506245072450824509245102451124512245132451424515245162451724518245192452024521245222452324524245252452624527245282452924530245312453224533245342453524536245372453824539245402454124542245432454424545245462454724548245492455024551245522455324554245552455624557245582455924560245612456224563245642456524566245672456824569245702457124572245732457424575245762457724578245792458024581245822458324584245852458624587245882458924590245912459224593245942459524596245972459824599246002460124602246032460424605246062460724608246092461024611246122461324614246152461624617246182461924620246212462224623246242462524626246272462824629246302463124632246332463424635246362463724638246392464024641246422464324644246452464624647246482464924650246512465224653246542465524656246572465824659246602466124662246632466424665246662466724668246692467024671246722467324674246752467624677246782467924680246812468224683246842468524686246872468824689246902469124692246932469424695246962469724698246992470024701247022470324704247052470624707247082470924710247112471224713247142471524716247172471824719247202472124722247232472424725247262472724728247292473024731247322473324734247352473624737247382473924740247412474224743247442474524746247472474824749247502475124752247532475424755247562475724758247592476024761247622476324764247652476624767247682476924770247712477224773247742477524776247772477824779247802478124782247832478424785247862478724788247892479024791247922479324794247952479624797247982479924800248012480224803248042480524806248072480824809248102481124812248132481424815248162481724818248192482024821248222482324824248252482624827248282482924830248312483224833248342483524836248372483824839248402484124842248432484424845248462484724848248492485024851248522485324854248552485624857248582485924860248612486224863248642486524866248672486824869248702487124872248732487424875248762487724878248792488024881248822488324884248852488624887248882488924890248912489224893248942489524896248972489824899249002490124902249032490424905249062490724908249092491024911249122491324914249152491624917249182491924920249212492224923249242492524926249272492824929249302493124932249332493424935249362493724938249392494024941249422494324944249452494624947249482494924950249512495224953249542495524956249572495824959249602496124962249632496424965249662496724968249692497024971249722497324974249752497624977249782497924980249812498224983249842498524986249872498824989249902499124992249932499424995249962499724998249992500025001250022500325004250052500625007250082500925010250112501225013250142501525016250172501825019250202502125022250232502425025250262502725028250292503025031250322503325034250352503625037250382503925040250412504225043250442504525046250472504825049250502505125052250532505425055250562505725058250592506025061250622506325064250652506625067250682506925070250712507225073250742507525076250772507825079250802508125082250832508425085250862508725088250892509025091250922509325094250952509625097250982509925100251012510225103251042510525106251072510825109251102511125112251132511425115251162511725118251192512025121251222512325124251252512625127251282512925130251312513225133251342513525136251372513825139251402514125142251432514425145251462514725148251492515025151251522515325154251552515625157251582515925160251612516225163251642516525166251672516825169251702517125172251732517425175251762517725178251792518025181251822518325184251852518625187251882518925190251912519225193251942519525196251972519825199252002520125202252032520425205252062520725208252092521025211252122521325214252152521625217252182521925220252212522225223252242522525226252272522825229252302523125232252332523425235252362523725238252392524025241252422524325244252452524625247252482524925250252512525225253252542525525256252572525825259252602526125262252632526425265252662526725268252692527025271252722527325274252752527625277252782527925280252812528225283252842528525286252872528825289252902529125292252932529425295252962529725298252992530025301253022530325304253052530625307253082530925310253112531225313253142531525316253172531825319253202532125322253232532425325253262532725328253292533025331253322533325334253352533625337253382533925340253412534225343253442534525346253472534825349253502535125352253532535425355253562535725358253592536025361253622536325364253652536625367253682536925370253712537225373253742537525376253772537825379253802538125382253832538425385253862538725388253892539025391253922539325394253952539625397253982539925400254012540225403254042540525406254072540825409254102541125412254132541425415254162541725418254192542025421254222542325424254252542625427254282542925430254312543225433254342543525436254372543825439254402544125442254432544425445254462544725448254492545025451254522545325454254552545625457254582545925460254612546225463254642546525466254672546825469254702547125472254732547425475254762547725478254792548025481254822548325484254852548625487254882548925490254912549225493254942549525496254972549825499255002550125502255032550425505255062550725508255092551025511255122551325514255152551625517255182551925520255212552225523255242552525526255272552825529255302553125532255332553425535255362553725538255392554025541255422554325544255452554625547255482554925550255512555225553255542555525556255572555825559255602556125562255632556425565255662556725568255692557025571255722557325574255752557625577255782557925580255812558225583255842558525586255872558825589255902559125592255932559425595255962559725598255992560025601256022560325604256052560625607256082560925610256112561225613256142561525616256172561825619256202562125622256232562425625256262562725628256292563025631256322563325634256352563625637256382563925640256412564225643256442564525646256472564825649256502565125652256532565425655256562565725658256592566025661256622566325664256652566625667256682566925670256712567225673256742567525676256772567825679256802568125682256832568425685256862568725688256892569025691256922569325694256952569625697256982569925700257012570225703257042570525706257072570825709257102571125712257132571425715257162571725718257192572025721257222572325724257252572625727257282572925730257312573225733257342573525736257372573825739257402574125742257432574425745257462574725748257492575025751257522575325754257552575625757257582575925760257612576225763257642576525766257672576825769257702577125772257732577425775257762577725778257792578025781257822578325784257852578625787257882578925790257912579225793257942579525796257972579825799258002580125802258032580425805258062580725808258092581025811258122581325814258152581625817258182581925820258212582225823258242582525826258272582825829258302583125832258332583425835258362583725838258392584025841258422584325844258452584625847258482584925850258512585225853258542585525856258572585825859258602586125862258632586425865258662586725868258692587025871258722587325874258752587625877258782587925880258812588225883258842588525886258872588825889258902589125892258932589425895258962589725898258992590025901259022590325904259052590625907259082590925910259112591225913259142591525916259172591825919259202592125922259232592425925259262592725928259292593025931259322593325934259352593625937259382593925940259412594225943259442594525946259472594825949259502595125952259532595425955259562595725958259592596025961259622596325964259652596625967259682596925970259712597225973259742597525976259772597825979259802598125982259832598425985259862598725988259892599025991259922599325994259952599625997259982599926000260012600226003260042600526006260072600826009260102601126012260132601426015260162601726018260192602026021260222602326024260252602626027260282602926030260312603226033260342603526036260372603826039260402604126042260432604426045260462604726048260492605026051260522605326054260552605626057260582605926060260612606226063260642606526066260672606826069260702607126072260732607426075260762607726078260792608026081260822608326084260852608626087260882608926090260912609226093260942609526096260972609826099261002610126102261032610426105261062610726108261092611026111261122611326114261152611626117261182611926120261212612226123261242612526126261272612826129261302613126132261332613426135261362613726138261392614026141261422614326144261452614626147261482614926150261512615226153261542615526156261572615826159261602616126162261632616426165261662616726168261692617026171261722617326174261752617626177261782617926180261812618226183261842618526186261872618826189261902619126192261932619426195261962619726198261992620026201262022620326204262052620626207262082620926210262112621226213262142621526216262172621826219262202622126222262232622426225262262622726228262292623026231262322623326234262352623626237262382623926240262412624226243262442624526246262472624826249262502625126252262532625426255262562625726258262592626026261262622626326264262652626626267262682626926270262712627226273262742627526276262772627826279262802628126282262832628426285262862628726288262892629026291262922629326294262952629626297262982629926300263012630226303263042630526306263072630826309263102631126312263132631426315263162631726318263192632026321263222632326324263252632626327263282632926330263312633226333263342633526336263372633826339263402634126342263432634426345263462634726348263492635026351263522635326354263552635626357263582635926360263612636226363263642636526366263672636826369263702637126372263732637426375263762637726378263792638026381263822638326384263852638626387263882638926390263912639226393263942639526396263972639826399264002640126402264032640426405264062640726408264092641026411264122641326414264152641626417264182641926420264212642226423264242642526426264272642826429264302643126432264332643426435264362643726438264392644026441264422644326444264452644626447264482644926450264512645226453264542645526456264572645826459264602646126462264632646426465264662646726468264692647026471264722647326474264752647626477264782647926480264812648226483264842648526486264872648826489264902649126492264932649426495264962649726498264992650026501265022650326504265052650626507265082650926510265112651226513265142651526516265172651826519265202652126522265232652426525265262652726528265292653026531265322653326534265352653626537265382653926540265412654226543265442654526546265472654826549265502655126552265532655426555265562655726558265592656026561265622656326564265652656626567265682656926570265712657226573265742657526576265772657826579265802658126582265832658426585265862658726588265892659026591265922659326594265952659626597265982659926600266012660226603266042660526606266072660826609266102661126612266132661426615266162661726618266192662026621266222662326624266252662626627266282662926630266312663226633266342663526636266372663826639266402664126642266432664426645266462664726648266492665026651266522665326654266552665626657266582665926660266612666226663266642666526666266672666826669266702667126672266732667426675266762667726678266792668026681266822668326684266852668626687266882668926690266912669226693266942669526696266972669826699267002670126702267032670426705267062670726708267092671026711267122671326714267152671626717267182671926720267212672226723267242672526726267272672826729267302673126732267332673426735267362673726738267392674026741267422674326744267452674626747267482674926750267512675226753267542675526756267572675826759267602676126762267632676426765267662676726768267692677026771267722677326774267752677626777267782677926780267812678226783267842678526786267872678826789267902679126792267932679426795267962679726798267992680026801268022680326804268052680626807268082680926810268112681226813268142681526816268172681826819268202682126822268232682426825268262682726828268292683026831268322683326834268352683626837268382683926840268412684226843268442684526846268472684826849268502685126852268532685426855268562685726858268592686026861268622686326864268652686626867268682686926870268712687226873268742687526876268772687826879268802688126882268832688426885268862688726888268892689026891268922689326894268952689626897268982689926900269012690226903269042690526906269072690826909269102691126912269132691426915269162691726918269192692026921269222692326924269252692626927269282692926930269312693226933269342693526936269372693826939269402694126942269432694426945269462694726948269492695026951269522695326954269552695626957269582695926960269612696226963269642696526966269672696826969269702697126972269732697426975269762697726978269792698026981269822698326984269852698626987269882698926990269912699226993269942699526996269972699826999270002700127002270032700427005270062700727008270092701027011270122701327014270152701627017270182701927020270212702227023270242702527026270272702827029270302703127032270332703427035270362703727038270392704027041270422704327044270452704627047270482704927050270512705227053270542705527056270572705827059270602706127062270632706427065270662706727068270692707027071270722707327074270752707627077270782707927080270812708227083270842708527086270872708827089270902709127092270932709427095270962709727098270992710027101271022710327104271052710627107271082710927110271112711227113271142711527116271172711827119271202712127122271232712427125271262712727128271292713027131271322713327134271352713627137271382713927140271412714227143271442714527146271472714827149271502715127152271532715427155271562715727158271592716027161271622716327164271652716627167271682716927170271712717227173271742717527176271772717827179271802718127182271832718427185271862718727188271892719027191271922719327194271952719627197271982719927200272012720227203272042720527206272072720827209272102721127212272132721427215272162721727218272192722027221272222722327224272252722627227272282722927230272312723227233272342723527236272372723827239272402724127242272432724427245272462724727248272492725027251272522725327254272552725627257272582725927260272612726227263272642726527266272672726827269272702727127272272732727427275272762727727278272792728027281272822728327284272852728627287272882728927290272912729227293272942729527296272972729827299273002730127302273032730427305273062730727308273092731027311273122731327314273152731627317273182731927320273212732227323273242732527326273272732827329273302733127332273332733427335273362733727338273392734027341273422734327344273452734627347273482734927350273512735227353273542735527356273572735827359273602736127362273632736427365273662736727368273692737027371273722737327374273752737627377273782737927380273812738227383273842738527386273872738827389273902739127392273932739427395273962739727398273992740027401274022740327404274052740627407274082740927410274112741227413274142741527416274172741827419274202742127422274232742427425274262742727428274292743027431274322743327434274352743627437274382743927440274412744227443274442744527446274472744827449274502745127452274532745427455274562745727458274592746027461274622746327464274652746627467274682746927470274712747227473274742747527476274772747827479274802748127482274832748427485274862748727488274892749027491274922749327494274952749627497274982749927500275012750227503275042750527506275072750827509275102751127512275132751427515275162751727518275192752027521275222752327524275252752627527275282752927530275312753227533275342753527536275372753827539275402754127542275432754427545275462754727548275492755027551275522755327554275552755627557275582755927560275612756227563275642756527566275672756827569275702757127572275732757427575275762757727578275792758027581275822758327584275852758627587275882758927590275912759227593275942759527596275972759827599276002760127602276032760427605276062760727608276092761027611276122761327614276152761627617276182761927620276212762227623276242762527626276272762827629276302763127632276332763427635276362763727638276392764027641276422764327644276452764627647276482764927650276512765227653276542765527656276572765827659276602766127662276632766427665276662766727668276692767027671276722767327674276752767627677276782767927680276812768227683276842768527686276872768827689276902769127692276932769427695276962769727698276992770027701277022770327704277052770627707277082770927710277112771227713277142771527716277172771827719277202772127722277232772427725277262772727728277292773027731277322773327734277352773627737277382773927740277412774227743277442774527746277472774827749277502775127752277532775427755277562775727758277592776027761277622776327764277652776627767277682776927770277712777227773277742777527776277772777827779277802778127782277832778427785277862778727788277892779027791277922779327794277952779627797277982779927800278012780227803278042780527806278072780827809278102781127812278132781427815278162781727818278192782027821278222782327824278252782627827278282782927830278312783227833278342783527836278372783827839278402784127842278432784427845278462784727848278492785027851278522785327854278552785627857278582785927860278612786227863278642786527866278672786827869278702787127872278732787427875278762787727878278792788027881278822788327884278852788627887278882788927890278912789227893278942789527896278972789827899279002790127902279032790427905279062790727908279092791027911279122791327914279152791627917279182791927920279212792227923279242792527926279272792827929279302793127932279332793427935279362793727938279392794027941279422794327944279452794627947279482794927950279512795227953279542795527956279572795827959279602796127962279632796427965279662796727968279692797027971279722797327974279752797627977279782797927980279812798227983279842798527986279872798827989279902799127992279932799427995279962799727998279992800028001280022800328004280052800628007280082800928010280112801228013280142801528016280172801828019280202802128022280232802428025280262802728028280292803028031280322803328034280352803628037280382803928040280412804228043280442804528046280472804828049280502805128052280532805428055280562805728058280592806028061280622806328064280652806628067280682806928070280712807228073280742807528076280772807828079280802808128082280832808428085280862808728088280892809028091280922809328094280952809628097280982809928100281012810228103281042810528106281072810828109281102811128112281132811428115281162811728118281192812028121281222812328124281252812628127281282812928130281312813228133281342813528136281372813828139281402814128142281432814428145281462814728148281492815028151281522815328154281552815628157281582815928160281612816228163281642816528166281672816828169281702817128172281732817428175281762817728178281792818028181281822818328184281852818628187281882818928190281912819228193281942819528196281972819828199282002820128202282032820428205282062820728208282092821028211282122821328214282152821628217282182821928220282212822228223282242822528226282272822828229282302823128232282332823428235282362823728238282392824028241282422824328244282452824628247282482824928250282512825228253282542825528256282572825828259282602826128262282632826428265282662826728268282692827028271282722827328274282752827628277282782827928280282812828228283282842828528286282872828828289282902829128292282932829428295282962829728298282992830028301283022830328304283052830628307283082830928310283112831228313283142831528316283172831828319283202832128322283232832428325283262832728328283292833028331283322833328334283352833628337283382833928340283412834228343283442834528346283472834828349283502835128352283532835428355283562835728358283592836028361283622836328364283652836628367283682836928370283712837228373283742837528376283772837828379283802838128382283832838428385283862838728388283892839028391283922839328394283952839628397283982839928400284012840228403284042840528406284072840828409284102841128412284132841428415284162841728418284192842028421284222842328424284252842628427284282842928430284312843228433284342843528436284372843828439284402844128442284432844428445284462844728448284492845028451284522845328454284552845628457284582845928460284612846228463284642846528466284672846828469284702847128472284732847428475284762847728478284792848028481284822848328484284852848628487284882848928490284912849228493284942849528496284972849828499285002850128502285032850428505285062850728508285092851028511285122851328514285152851628517285182851928520285212852228523285242852528526285272852828529285302853128532285332853428535285362853728538285392854028541285422854328544285452854628547285482854928550285512855228553285542855528556285572855828559285602856128562285632856428565285662856728568285692857028571285722857328574285752857628577285782857928580285812858228583285842858528586285872858828589285902859128592285932859428595285962859728598285992860028601286022860328604286052860628607286082860928610286112861228613286142861528616286172861828619286202862128622286232862428625286262862728628286292863028631286322863328634286352863628637286382863928640286412864228643286442864528646286472864828649286502865128652286532865428655286562865728658286592866028661286622866328664286652866628667286682866928670286712867228673286742867528676286772867828679286802868128682286832868428685286862868728688286892869028691286922869328694286952869628697286982869928700287012870228703287042870528706287072870828709287102871128712287132871428715287162871728718287192872028721287222872328724287252872628727287282872928730287312873228733287342873528736287372873828739287402874128742287432874428745287462874728748287492875028751287522875328754287552875628757287582875928760287612876228763287642876528766287672876828769287702877128772287732877428775287762877728778287792878028781287822878328784287852878628787287882878928790287912879228793287942879528796287972879828799288002880128802288032880428805288062880728808288092881028811288122881328814288152881628817288182881928820288212882228823288242882528826288272882828829288302883128832288332883428835288362883728838288392884028841288422884328844288452884628847288482884928850288512885228853288542885528856288572885828859288602886128862288632886428865288662886728868288692887028871288722887328874288752887628877288782887928880288812888228883288842888528886288872888828889288902889128892288932889428895288962889728898288992890028901289022890328904289052890628907289082890928910289112891228913289142891528916289172891828919289202892128922289232892428925289262892728928289292893028931289322893328934289352893628937289382893928940289412894228943289442894528946289472894828949289502895128952289532895428955289562895728958289592896028961289622896328964289652896628967289682896928970289712897228973289742897528976289772897828979289802898128982289832898428985289862898728988289892899028991289922899328994289952899628997289982899929000290012900229003290042900529006290072900829009290102901129012290132901429015290162901729018290192902029021290222902329024290252902629027290282902929030290312903229033290342903529036290372903829039290402904129042290432904429045290462904729048290492905029051290522905329054290552905629057290582905929060290612906229063290642906529066290672906829069290702907129072290732907429075290762907729078290792908029081290822908329084290852908629087290882908929090290912909229093290942909529096290972909829099291002910129102291032910429105291062910729108291092911029111291122911329114291152911629117291182911929120291212912229123291242912529126291272912829129291302913129132291332913429135291362913729138291392914029141291422914329144291452914629147291482914929150291512915229153291542915529156291572915829159291602916129162291632916429165291662916729168291692917029171291722917329174291752917629177291782917929180291812918229183291842918529186291872918829189291902919129192291932919429195291962919729198291992920029201292022920329204292052920629207292082920929210292112921229213292142921529216292172921829219292202922129222292232922429225292262922729228292292923029231292322923329234292352923629237292382923929240292412924229243292442924529246292472924829249292502925129252292532925429255292562925729258292592926029261292622926329264292652926629267292682926929270292712927229273292742927529276292772927829279292802928129282292832928429285292862928729288292892929029291292922929329294292952929629297292982929929300293012930229303293042930529306293072930829309293102931129312293132931429315293162931729318293192932029321293222932329324293252932629327293282932929330293312933229333293342933529336293372933829339293402934129342293432934429345293462934729348293492935029351293522935329354293552935629357293582935929360293612936229363293642936529366293672936829369293702937129372293732937429375293762937729378293792938029381293822938329384293852938629387293882938929390293912939229393293942939529396293972939829399294002940129402294032940429405294062940729408294092941029411294122941329414294152941629417294182941929420294212942229423294242942529426294272942829429294302943129432294332943429435294362943729438294392944029441294422944329444294452944629447294482944929450294512945229453294542945529456294572945829459294602946129462294632946429465294662946729468294692947029471294722947329474294752947629477294782947929480294812948229483294842948529486294872948829489294902949129492294932949429495294962949729498294992950029501295022950329504295052950629507295082950929510295112951229513295142951529516295172951829519295202952129522295232952429525295262952729528295292953029531295322953329534295352953629537295382953929540295412954229543295442954529546295472954829549295502955129552295532955429555295562955729558295592956029561295622956329564295652956629567295682956929570295712957229573295742957529576295772957829579295802958129582295832958429585295862958729588295892959029591295922959329594295952959629597295982959929600296012960229603296042960529606296072960829609296102961129612296132961429615296162961729618296192962029621296222962329624296252962629627296282962929630296312963229633296342963529636296372963829639296402964129642296432964429645296462964729648296492965029651296522965329654296552965629657296582965929660296612966229663296642966529666296672966829669296702967129672296732967429675296762967729678296792968029681296822968329684296852968629687296882968929690296912969229693296942969529696296972969829699297002970129702297032970429705297062970729708297092971029711297122971329714297152971629717297182971929720297212972229723297242972529726297272972829729297302973129732297332973429735297362973729738297392974029741297422974329744297452974629747297482974929750297512975229753297542975529756297572975829759297602976129762297632976429765297662976729768297692977029771297722977329774297752977629777297782977929780297812978229783297842978529786297872978829789297902979129792297932979429795297962979729798297992980029801298022980329804298052980629807298082980929810298112981229813298142981529816298172981829819298202982129822298232982429825298262982729828298292983029831298322983329834298352983629837298382983929840298412984229843298442984529846298472984829849298502985129852298532985429855298562985729858298592986029861298622986329864298652986629867298682986929870298712987229873298742987529876298772987829879298802988129882298832988429885298862988729888298892989029891298922989329894298952989629897298982989929900299012990229903299042990529906299072990829909299102991129912299132991429915299162991729918299192992029921299222992329924299252992629927299282992929930299312993229933299342993529936299372993829939299402994129942299432994429945299462994729948299492995029951299522995329954299552995629957299582995929960299612996229963299642996529966299672996829969299702997129972299732997429975299762997729978299792998029981299822998329984299852998629987299882998929990299912999229993299942999529996299972999829999300003000130002300033000430005300063000730008300093001030011300123001330014300153001630017300183001930020300213002230023300243002530026300273002830029300303003130032300333003430035300363003730038300393004030041300423004330044300453004630047300483004930050300513005230053300543005530056300573005830059300603006130062300633006430065300663006730068300693007030071300723007330074300753007630077300783007930080300813008230083300843008530086300873008830089300903009130092300933009430095300963009730098300993010030101301023010330104301053010630107301083010930110301113011230113301143011530116301173011830119301203012130122301233012430125301263012730128301293013030131301323013330134301353013630137301383013930140301413014230143301443014530146301473014830149301503015130152301533015430155301563015730158301593016030161301623016330164301653016630167301683016930170301713017230173301743017530176301773017830179301803018130182301833018430185301863018730188301893019030191301923019330194301953019630197301983019930200302013020230203302043020530206302073020830209302103021130212302133021430215302163021730218302193022030221302223022330224302253022630227302283022930230302313023230233302343023530236302373023830239302403024130242302433024430245302463024730248302493025030251302523025330254302553025630257302583025930260302613026230263302643026530266302673026830269302703027130272302733027430275302763027730278302793028030281302823028330284302853028630287302883028930290302913029230293302943029530296302973029830299303003030130302303033030430305303063030730308303093031030311303123031330314303153031630317303183031930320303213032230323303243032530326303273032830329303303033130332303333033430335303363033730338303393034030341303423034330344303453034630347303483034930350303513035230353303543035530356303573035830359303603036130362303633036430365303663036730368303693037030371303723037330374303753037630377303783037930380303813038230383303843038530386303873038830389303903039130392303933039430395303963039730398303993040030401304023040330404304053040630407304083040930410304113041230413304143041530416304173041830419304203042130422304233042430425304263042730428304293043030431304323043330434304353043630437304383043930440304413044230443304443044530446304473044830449304503045130452304533045430455304563045730458304593046030461304623046330464304653046630467304683046930470304713047230473304743047530476304773047830479304803048130482304833048430485304863048730488304893049030491304923049330494304953049630497304983049930500305013050230503305043050530506305073050830509305103051130512305133051430515305163051730518305193052030521305223052330524305253052630527305283052930530305313053230533305343053530536305373053830539305403054130542305433054430545305463054730548305493055030551305523055330554305553055630557305583055930560305613056230563305643056530566305673056830569305703057130572305733057430575305763057730578305793058030581305823058330584305853058630587305883058930590305913059230593305943059530596305973059830599306003060130602306033060430605306063060730608306093061030611306123061330614306153061630617306183061930620306213062230623306243062530626306273062830629306303063130632306333063430635306363063730638306393064030641306423064330644306453064630647306483064930650306513065230653306543065530656306573065830659306603066130662306633066430665306663066730668306693067030671306723067330674306753067630677306783067930680306813068230683306843068530686306873068830689306903069130692306933069430695306963069730698306993070030701307023070330704307053070630707307083070930710307113071230713307143071530716307173071830719307203072130722307233072430725307263072730728307293073030731307323073330734307353073630737307383073930740307413074230743307443074530746307473074830749307503075130752307533075430755307563075730758307593076030761307623076330764307653076630767307683076930770307713077230773307743077530776307773077830779307803078130782307833078430785307863078730788307893079030791307923079330794307953079630797307983079930800308013080230803308043080530806308073080830809308103081130812308133081430815308163081730818308193082030821308223082330824308253082630827308283082930830308313083230833308343083530836308373083830839308403084130842308433084430845308463084730848308493085030851308523085330854308553085630857308583085930860308613086230863308643086530866308673086830869308703087130872308733087430875308763087730878308793088030881308823088330884308853088630887308883088930890308913089230893308943089530896308973089830899309003090130902309033090430905309063090730908309093091030911309123091330914309153091630917309183091930920309213092230923309243092530926309273092830929309303093130932309333093430935309363093730938309393094030941309423094330944309453094630947309483094930950309513095230953309543095530956309573095830959309603096130962309633096430965309663096730968309693097030971309723097330974309753097630977309783097930980309813098230983309843098530986309873098830989309903099130992309933099430995309963099730998309993100031001310023100331004310053100631007310083100931010310113101231013310143101531016310173101831019310203102131022310233102431025310263102731028310293103031031310323103331034310353103631037310383103931040310413104231043310443104531046310473104831049310503105131052310533105431055310563105731058310593106031061310623106331064310653106631067310683106931070310713107231073310743107531076310773107831079310803108131082310833108431085310863108731088310893109031091310923109331094310953109631097310983109931100311013110231103311043110531106311073110831109311103111131112311133111431115311163111731118311193112031121311223112331124311253112631127311283112931130311313113231133311343113531136311373113831139311403114131142311433114431145311463114731148311493115031151311523115331154311553115631157311583115931160311613116231163311643116531166311673116831169311703117131172311733117431175311763117731178311793118031181311823118331184311853118631187311883118931190311913119231193311943119531196311973119831199312003120131202312033120431205312063120731208312093121031211312123121331214312153121631217312183121931220312213122231223312243122531226312273122831229312303123131232312333123431235312363123731238312393124031241312423124331244312453124631247312483124931250312513125231253312543125531256312573125831259312603126131262312633126431265312663126731268312693127031271312723127331274312753127631277312783127931280312813128231283312843128531286312873128831289312903129131292312933129431295312963129731298312993130031301313023130331304313053130631307313083130931310313113131231313313143131531316313173131831319313203132131322313233132431325313263132731328313293133031331313323133331334313353133631337313383133931340313413134231343313443134531346313473134831349313503135131352313533135431355313563135731358313593136031361313623136331364313653136631367313683136931370313713137231373313743137531376313773137831379313803138131382313833138431385313863138731388313893139031391313923139331394313953139631397313983139931400314013140231403314043140531406314073140831409314103141131412314133141431415314163141731418314193142031421314223142331424314253142631427314283142931430314313143231433314343143531436314373143831439314403144131442314433144431445314463144731448314493145031451314523145331454314553145631457314583145931460314613146231463314643146531466314673146831469314703147131472314733147431475314763147731478314793148031481314823148331484314853148631487314883148931490314913149231493314943149531496314973149831499315003150131502315033150431505315063150731508315093151031511315123151331514315153151631517315183151931520315213152231523315243152531526315273152831529315303153131532315333153431535315363153731538315393154031541315423154331544315453154631547315483154931550315513155231553315543155531556315573155831559315603156131562315633156431565315663156731568315693157031571315723157331574315753157631577315783157931580315813158231583315843158531586315873158831589315903159131592315933159431595315963159731598315993160031601316023160331604316053160631607316083160931610316113161231613316143161531616316173161831619316203162131622316233162431625316263162731628316293163031631316323163331634316353163631637316383163931640316413164231643316443164531646316473164831649316503165131652316533165431655316563165731658316593166031661316623166331664316653166631667316683166931670316713167231673316743167531676316773167831679316803168131682316833168431685316863168731688316893169031691316923169331694316953169631697316983169931700317013170231703317043170531706317073170831709317103171131712317133171431715317163171731718317193172031721317223172331724317253172631727317283172931730317313173231733317343173531736317373173831739317403174131742317433174431745317463174731748317493175031751317523175331754317553175631757317583175931760317613176231763317643176531766317673176831769317703177131772317733177431775317763177731778317793178031781317823178331784317853178631787317883178931790317913179231793317943179531796317973179831799318003180131802318033180431805318063180731808318093181031811318123181331814318153181631817318183181931820318213182231823318243182531826318273182831829318303183131832318333183431835318363183731838318393184031841318423184331844318453184631847318483184931850318513185231853318543185531856318573185831859318603186131862318633186431865318663186731868318693187031871318723187331874318753187631877318783187931880318813188231883318843188531886318873188831889318903189131892318933189431895318963189731898318993190031901319023190331904319053190631907319083190931910319113191231913319143191531916319173191831919319203192131922319233192431925319263192731928319293193031931319323193331934319353193631937319383193931940319413194231943319443194531946319473194831949319503195131952319533195431955319563195731958319593196031961319623196331964319653196631967319683196931970319713197231973319743197531976319773197831979319803198131982319833198431985319863198731988319893199031991319923199331994319953199631997319983199932000320013200232003320043200532006320073200832009320103201132012320133201432015320163201732018320193202032021320223202332024320253202632027320283202932030320313203232033320343203532036320373203832039320403204132042320433204432045320463204732048320493205032051320523205332054320553205632057320583205932060320613206232063320643206532066320673206832069320703207132072320733207432075320763207732078320793208032081320823208332084320853208632087320883208932090320913209232093320943209532096320973209832099321003210132102321033210432105321063210732108321093211032111321123211332114321153211632117321183211932120321213212232123321243212532126321273212832129321303213132132321333213432135321363213732138321393214032141321423214332144321453214632147321483214932150321513215232153321543215532156321573215832159321603216132162321633216432165321663216732168321693217032171321723217332174321753217632177321783217932180321813218232183321843218532186321873218832189321903219132192321933219432195321963219732198321993220032201322023220332204322053220632207322083220932210322113221232213322143221532216322173221832219322203222132222322233222432225322263222732228322293223032231322323223332234322353223632237322383223932240322413224232243322443224532246322473224832249322503225132252322533225432255322563225732258322593226032261322623226332264322653226632267322683226932270322713227232273322743227532276322773227832279322803228132282322833228432285322863228732288322893229032291322923229332294322953229632297322983229932300323013230232303323043230532306323073230832309323103231132312323133231432315323163231732318323193232032321323223232332324323253232632327323283232932330323313233232333323343233532336323373233832339323403234132342323433234432345323463234732348323493235032351323523235332354323553235632357323583235932360323613236232363323643236532366323673236832369323703237132372323733237432375323763237732378323793238032381323823238332384323853238632387323883238932390323913239232393323943239532396323973239832399324003240132402324033240432405324063240732408324093241032411324123241332414324153241632417324183241932420324213242232423324243242532426324273242832429324303243132432324333243432435324363243732438324393244032441324423244332444324453244632447324483244932450324513245232453324543245532456324573245832459324603246132462324633246432465324663246732468324693247032471324723247332474324753247632477324783247932480324813248232483324843248532486324873248832489324903249132492324933249432495324963249732498324993250032501325023250332504325053250632507325083250932510325113251232513325143251532516325173251832519325203252132522325233252432525325263252732528325293253032531325323253332534325353253632537325383253932540325413254232543325443254532546325473254832549325503255132552325533255432555325563255732558325593256032561325623256332564325653256632567325683256932570325713257232573325743257532576325773257832579325803258132582325833258432585325863258732588325893259032591325923259332594325953259632597325983259932600326013260232603326043260532606326073260832609326103261132612326133261432615326163261732618326193262032621326223262332624326253262632627326283262932630326313263232633326343263532636326373263832639326403264132642326433264432645326463264732648326493265032651326523265332654326553265632657326583265932660326613266232663326643266532666326673266832669326703267132672326733267432675326763267732678326793268032681326823268332684326853268632687326883268932690326913269232693326943269532696326973269832699327003270132702327033270432705327063270732708327093271032711327123271332714327153271632717327183271932720327213272232723327243272532726327273272832729327303273132732327333273432735327363273732738327393274032741327423274332744327453274632747327483274932750327513275232753327543275532756327573275832759327603276132762327633276432765327663276732768327693277032771327723277332774327753277632777327783277932780327813278232783327843278532786327873278832789327903279132792327933279432795327963279732798327993280032801328023280332804328053280632807328083280932810328113281232813328143281532816328173281832819328203282132822328233282432825328263282732828328293283032831328323283332834328353283632837328383283932840328413284232843328443284532846328473284832849328503285132852328533285432855328563285732858328593286032861328623286332864328653286632867328683286932870328713287232873328743287532876328773287832879328803288132882328833288432885328863288732888328893289032891328923289332894328953289632897328983289932900329013290232903329043290532906329073290832909329103291132912329133291432915329163291732918329193292032921329223292332924329253292632927329283292932930329313293232933329343293532936329373293832939329403294132942329433294432945329463294732948329493295032951329523295332954329553295632957329583295932960329613296232963329643296532966329673296832969329703297132972329733297432975329763297732978329793298032981329823298332984329853298632987329883298932990329913299232993329943299532996329973299832999330003300133002330033300433005330063300733008330093301033011330123301333014330153301633017330183301933020330213302233023330243302533026330273302833029330303303133032330333303433035330363303733038330393304033041330423304333044330453304633047330483304933050330513305233053330543305533056330573305833059330603306133062330633306433065330663306733068330693307033071330723307333074330753307633077330783307933080330813308233083330843308533086330873308833089330903309133092330933309433095330963309733098330993310033101331023310333104331053310633107331083310933110331113311233113331143311533116331173311833119331203312133122331233312433125331263312733128331293313033131331323313333134331353313633137331383313933140331413314233143331443314533146331473314833149331503315133152331533315433155331563315733158331593316033161331623316333164331653316633167331683316933170331713317233173331743317533176331773317833179331803318133182331833318433185331863318733188331893319033191331923319333194331953319633197331983319933200332013320233203332043320533206332073320833209332103321133212332133321433215332163321733218332193322033221332223322333224332253322633227332283322933230332313323233233332343323533236332373323833239332403324133242332433324433245332463324733248332493325033251332523325333254332553325633257332583325933260332613326233263332643326533266332673326833269332703327133272332733327433275332763327733278332793328033281332823328333284332853328633287332883328933290332913329233293332943329533296332973329833299333003330133302333033330433305333063330733308333093331033311333123331333314333153331633317333183331933320333213332233323333243332533326333273332833329333303333133332333333333433335333363333733338333393334033341333423334333344333453334633347333483334933350333513335233353333543335533356333573335833359333603336133362333633336433365333663336733368333693337033371333723337333374333753337633377333783337933380333813338233383333843338533386333873338833389333903339133392333933339433395333963339733398333993340033401334023340333404334053340633407334083340933410334113341233413334143341533416334173341833419334203342133422334233342433425334263342733428334293343033431334323343333434334353343633437334383343933440334413344233443334443344533446334473344833449334503345133452334533345433455334563345733458334593346033461334623346333464334653346633467334683346933470334713347233473334743347533476334773347833479334803348133482334833348433485334863348733488334893349033491334923349333494334953349633497334983349933500335013350233503335043350533506335073350833509335103351133512335133351433515335163351733518335193352033521335223352333524335253352633527335283352933530335313353233533335343353533536335373353833539335403354133542335433354433545335463354733548335493355033551335523355333554335553355633557335583355933560335613356233563335643356533566335673356833569335703357133572335733357433575335763357733578335793358033581335823358333584335853358633587335883358933590335913359233593335943359533596335973359833599336003360133602336033360433605336063360733608336093361033611336123361333614336153361633617336183361933620336213362233623336243362533626336273362833629336303363133632336333363433635336363363733638336393364033641336423364333644336453364633647336483364933650336513365233653336543365533656336573365833659336603366133662336633366433665336663366733668336693367033671336723367333674336753367633677336783367933680336813368233683336843368533686336873368833689336903369133692336933369433695336963369733698336993370033701337023370333704337053370633707337083370933710337113371233713337143371533716337173371833719337203372133722337233372433725337263372733728337293373033731337323373333734337353373633737337383373933740337413374233743337443374533746337473374833749337503375133752337533375433755337563375733758337593376033761337623376333764337653376633767337683376933770337713377233773337743377533776337773377833779337803378133782337833378433785337863378733788337893379033791337923379333794337953379633797337983379933800338013380233803338043380533806338073380833809338103381133812338133381433815338163381733818338193382033821338223382333824338253382633827338283382933830338313383233833338343383533836338373383833839338403384133842338433384433845338463384733848338493385033851338523385333854338553385633857338583385933860338613386233863338643386533866338673386833869338703387133872338733387433875338763387733878338793388033881338823388333884338853388633887338883388933890338913389233893338943389533896338973389833899339003390133902339033390433905339063390733908339093391033911339123391333914339153391633917339183391933920339213392233923339243392533926339273392833929339303393133932339333393433935339363393733938339393394033941339423394333944339453394633947339483394933950339513395233953339543395533956339573395833959339603396133962339633396433965339663396733968339693397033971
  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. {$Optimization }
  18. interface
  19. uses
  20. Classes, SysUtils, fpcunit, testregistry, contnrs,
  21. jstree, jswriter, jsbase,
  22. PasTree, PScanner, PasResolver, PParser, PasResolveEval,
  23. FPPas2Js;
  24. const
  25. // default parser+scanner options
  26. po_tcmodules = po_Pas2js+[po_KeepScannerError];
  27. co_tcmodules = [];
  28. type
  29. TSrcMarkerKind = (
  30. mkLabel,
  31. mkResolverReference,
  32. mkDirectReference
  33. );
  34. PSrcMarker = ^TSrcMarker;
  35. TSrcMarker = record
  36. Kind: TSrcMarkerKind;
  37. Filename: string;
  38. Row: integer;
  39. StartCol, EndCol: integer; // token start, end column
  40. Identifier: string;
  41. Next: PSrcMarker;
  42. end;
  43. TSystemUnitPart = (
  44. supTObject,
  45. supTVarRec,
  46. supTypeInfo,
  47. supTInterfacedObject,
  48. supWriteln
  49. );
  50. TSystemUnitParts = set of TSystemUnitPart;
  51. { TTestHintMessage }
  52. TTestHintMessage = class
  53. public
  54. Id: int64;
  55. MsgType: TMessageType;
  56. MsgNumber: integer;
  57. Msg: string;
  58. SourcePos: TPasSourcePos;
  59. end;
  60. { TTestPasParser }
  61. TTestPasParser = Class(TPasParser)
  62. end;
  63. TOnFindUnit = function(const aUnitName: String): TPasModule of object;
  64. { TTestEnginePasResolver }
  65. TTestEnginePasResolver = class(TPas2JsResolver)
  66. private
  67. FFilename: string;
  68. FModule: TPasModule;
  69. FOnFindUnit: TOnFindUnit;
  70. FParser: TTestPasParser;
  71. FStreamResolver: TStreamResolver;
  72. FScanner: TPas2jsPasScanner;
  73. FSource: string;
  74. public
  75. destructor Destroy; override;
  76. function FindUnit(const AName, InFilename: String; NameExpr,
  77. InFileExpr: TPasExpr): TPasModule; override;
  78. procedure UsedInterfacesFinished(Section: TPasSection); override;
  79. property OnFindUnit: TOnFindUnit read FOnFindUnit write FOnFindUnit;
  80. property Filename: string read FFilename write FFilename;
  81. property StreamResolver: TStreamResolver read FStreamResolver write FStreamResolver;
  82. property Scanner: TPas2jsPasScanner read FScanner write FScanner;
  83. property Parser: TTestPasParser read FParser write FParser;
  84. property Source: string read FSource write FSource;
  85. property Module: TPasModule read FModule;
  86. end;
  87. { TCustomTestModule }
  88. TCustomTestModule = Class(TTestCase)
  89. private
  90. FConverter: TPasToJSConverter;
  91. FEngine: TTestEnginePasResolver;
  92. FExpectedErrorClass: ExceptClass;
  93. FExpectedErrorMsg: string;
  94. FExpectedErrorNumber: integer;
  95. FFilename: string;
  96. FFileResolver: TStreamResolver;
  97. FHub: TPas2JSResolverHub;
  98. FJSImplementationUses: TJSArrayLiteral;
  99. FJSInitBody: TJSFunctionBody;
  100. FJSImplentationUses: TJSArrayLiteral;
  101. FJSInterfaceUses: TJSArrayLiteral;
  102. FJSModule: TJSSourceElements;
  103. FJSModuleSrc: TJSSourceElements;
  104. FJSSource: TStringList;
  105. FModule: TPasModule;
  106. FJSModuleCallArgs: TJSArguments;
  107. FModules: TObjectList;// list of TTestEnginePasResolver
  108. FParser: TTestPasParser;
  109. FPasProgram: TPasProgram;
  110. FPasLibrary: TPasLibrary;
  111. FHintMsgs: TObjectList; // list of TTestHintMessage
  112. FHintMsgsGood: TFPList; // list of TTestHintMessage marked as expected
  113. FJSRegModuleCall: TJSCallExpression;
  114. FScanner: TPas2jsPasScanner;
  115. FSkipTests: boolean;
  116. FSource: TStringList;
  117. FFirstPasStatement: TPasImplBlock;
  118. FWithTypeInfo: boolean;
  119. {$IFDEF EnablePasTreeGlobalRefCount}
  120. FElementRefCountAtSetup: int64;
  121. {$ENDIF}
  122. function GetMsgCount: integer;
  123. function GetMsgs(Index: integer): TTestHintMessage;
  124. function GetResolverCount: integer;
  125. function GetResolvers(Index: integer): TTestEnginePasResolver;
  126. function OnPasResolverFindUnit(const aUnitName: String): TPasModule;
  127. procedure OnParserLog(Sender: TObject; const Msg: String);
  128. procedure OnPasResolverLog(Sender: TObject; const Msg: String);
  129. procedure OnScannerLog(Sender: TObject; const Msg: String);
  130. procedure SetWithTypeInfo(const AValue: boolean);
  131. protected
  132. procedure SetUp; override;
  133. function CreateConverter: TPasToJSConverter; virtual;
  134. function LoadUnit(const aUnitName: String): TPasModule;
  135. procedure InitScanner(aScanner: TPas2jsPasScanner); virtual;
  136. procedure TearDown; override;
  137. Procedure Add(Line: string); virtual;
  138. Procedure Add(const Lines: array of string);
  139. Procedure StartParsing; virtual;
  140. procedure ParseModuleQueue; virtual;
  141. procedure ParseModule; virtual;
  142. procedure ParseProgram; virtual;
  143. procedure ParseLibrary; virtual;
  144. procedure ParseUnit; virtual;
  145. protected
  146. function FindModuleWithFilename(aFilename: string): TTestEnginePasResolver; virtual;
  147. function AddModule(aFilename: string): TTestEnginePasResolver; virtual;
  148. function AddModuleWithSrc(aFilename, Src: string): TTestEnginePasResolver; virtual;
  149. function AddModuleWithIntfImplSrc(aFilename, InterfaceSrc,
  150. ImplementationSrc: string): TTestEnginePasResolver; virtual;
  151. procedure AddSystemUnit(Parts: TSystemUnitParts = []); virtual;
  152. procedure StartProgram(NeedSystemUnit: boolean; SystemUnitParts: TSystemUnitParts = []); virtual;
  153. procedure StartLibrary(NeedSystemUnit: boolean; SystemUnitParts: TSystemUnitParts = []); virtual;
  154. procedure StartUnit(NeedSystemUnit: boolean; SystemUnitParts: TSystemUnitParts = []); virtual;
  155. procedure ConvertModule; virtual;
  156. procedure ConvertProgram; virtual;
  157. procedure ConvertLibrary; virtual;
  158. procedure ConvertUnit; virtual;
  159. function ConvertJSModuleToString(El: TJSElement): string; virtual;
  160. procedure CheckDottedIdentifier(Msg: string; El: TJSElement; DottedName: string);
  161. function GetDottedIdentifier(El: TJSElement): string;
  162. procedure CheckSource(Msg,Statements: String; InitStatements: string = '';
  163. ImplStatements: string = ''); virtual;
  164. procedure CheckDiff(Msg, Expected, Actual: string); virtual;
  165. procedure CheckUnit(Filename, ExpectedSrc: string); virtual;
  166. procedure CheckHint(MsgType: TMessageType; MsgNumber: integer;
  167. Msg: string; Marker: PSrcMarker = nil); virtual;
  168. procedure CheckResolverUnexpectedHints(WithSourcePos: boolean = false); virtual;
  169. procedure SetExpectedScannerError(Msg: string; MsgNumber: integer);
  170. procedure SetExpectedParserError(Msg: string; MsgNumber: integer);
  171. procedure SetExpectedPasResolverError(Msg: string; MsgNumber: integer);
  172. procedure SetExpectedConverterError(Msg: string; MsgNumber: integer);
  173. function IsErrorExpected(E: Exception): boolean;
  174. procedure HandleScannerError(E: EScannerError);
  175. procedure HandleParserError(E: EParserError);
  176. procedure HandlePasResolveError(E: EPasResolve);
  177. procedure HandlePas2JSError(E: EPas2JS);
  178. procedure HandleException(E: Exception);
  179. procedure FailException(E: Exception);
  180. procedure WriteSources(const aFilename: string; aRow, aCol: integer);
  181. function IndexOfResolver(const Filename: string): integer;
  182. function GetResolver(const Filename: string): TTestEnginePasResolver;
  183. function GetDefaultNamespace: string;
  184. property PasProgram: TPasProgram Read FPasProgram;
  185. property PasLibrary: TPasLibrary Read FPasLibrary;
  186. property Resolvers[Index: integer]: TTestEnginePasResolver read GetResolvers;
  187. property ResolverCount: integer read GetResolverCount;
  188. property Engine: TTestEnginePasResolver read FEngine;
  189. property Filename: string read FFilename;
  190. Property Module: TPasModule Read FModule;
  191. property FirstPasStatement: TPasImplBlock read FFirstPasStatement;
  192. property Converter: TPasToJSConverter read FConverter;
  193. property JSSource: TStringList read FJSSource;
  194. property JSModule: TJSSourceElements read FJSModule;
  195. property JSRegModuleCall: TJSCallExpression read FJSRegModuleCall;
  196. property JSModuleCallArgs: TJSArguments read FJSModuleCallArgs;
  197. property JSImplementationUses: TJSArrayLiteral read FJSImplementationUses;
  198. property JSInterfaceUses: TJSArrayLiteral read FJSInterfaceUses;
  199. property JSModuleSrc: TJSSourceElements read FJSModuleSrc;
  200. property JSInitBody: TJSFunctionBody read FJSInitBody;
  201. property ExpectedErrorClass: ExceptClass read FExpectedErrorClass write FExpectedErrorClass;
  202. property ExpectedErrorMsg: string read FExpectedErrorMsg write FExpectedErrorMsg;
  203. property ExpectedErrorNumber: integer read FExpectedErrorNumber write FExpectedErrorNumber;
  204. property SkipTests: boolean read FSkipTests write FSkipTests;
  205. public
  206. constructor Create; override;
  207. destructor Destroy; override;
  208. property Hub: TPas2JSResolverHub read FHub;
  209. property Source: TStringList read FSource;
  210. property FileResolver: TStreamResolver read FFileResolver;
  211. property Scanner: TPas2jsPasScanner read FScanner;
  212. property Parser: TTestPasParser read FParser;
  213. property MsgCount: integer read GetMsgCount;
  214. property Msgs[Index: integer]: TTestHintMessage read GetMsgs;
  215. property WithTypeInfo: boolean read FWithTypeInfo write SetWithTypeInfo;
  216. end;
  217. { TTestModule }
  218. TTestModule = class(TCustomTestModule)
  219. Published
  220. Procedure TestReservedWords;
  221. // program, units, includes
  222. Procedure TestEmptyProgram;
  223. Procedure TestEmptyProgramUseStrict;
  224. Procedure TestEmptyUnit;
  225. Procedure TestEmptyUnitUseStrict;
  226. Procedure TestDottedUnitNames;
  227. Procedure TestDottedUnitNameImpl;
  228. Procedure TestDottedUnitExpr;
  229. Procedure Test_ModeFPCFail;
  230. Procedure Test_ModeSwitchCBlocksFail;
  231. Procedure TestUnit_UseSystem;
  232. Procedure TestUnit_Intf1Impl2Intf1;
  233. Procedure TestIncludeVersion;
  234. // vars/const
  235. Procedure TestVarInt;
  236. Procedure TestVarBaseTypes;
  237. Procedure TestBaseTypeSingleFail;
  238. Procedure TestBaseTypeExtendedFail;
  239. Procedure TestConstBaseTypes;
  240. Procedure TestUnitImplVars;
  241. Procedure TestUnitImplConsts;
  242. Procedure TestUnitImplRecord;
  243. Procedure TestRenameJSNameConflict;
  244. Procedure TestLocalConst;
  245. Procedure TestVarExternal;
  246. Procedure TestVarExternalOtherUnit;
  247. Procedure TestVarAbsoluteFail;
  248. Procedure TestConstExternal;
  249. // numbers
  250. Procedure TestDouble;
  251. Procedure TestInteger;
  252. Procedure TestIntegerRange;
  253. Procedure TestIntegerTypecasts;
  254. Procedure TestInteger_BitwiseShrNativeInt;
  255. Procedure TestInteger_BitwiseShlNativeInt;
  256. Procedure TestInteger_SystemFunc;
  257. Procedure TestInteger_AssignOutsideConst;
  258. Procedure TestCurrency;
  259. Procedure TestForBoolDo;
  260. Procedure TestForIntDo;
  261. Procedure TestForIntInDo;
  262. // strings
  263. Procedure TestCharConst;
  264. Procedure TestChar_Compare;
  265. Procedure TestChar_BuiltInProcs;
  266. Procedure TestStringConst;
  267. Procedure TestStringConst_InvalidUTF16;
  268. Procedure TestStringConstSurrogate;
  269. Procedure TestString_Length;
  270. Procedure TestString_Compare;
  271. Procedure TestString_SetLength;
  272. Procedure TestString_CharAt;
  273. Procedure TestStringHMinusFail;
  274. Procedure TestStr;
  275. Procedure TestBaseType_AnsiStringFail;
  276. Procedure TestBaseType_WideStringFail;
  277. Procedure TestBaseType_ShortStringFail;
  278. Procedure TestBaseType_RawByteStringFail;
  279. Procedure TestTypeShortstring_Fail;
  280. Procedure TestCharSet_Custom;
  281. Procedure TestWideChar;
  282. Procedure TestForCharDo;
  283. Procedure TestForCharInDo;
  284. // alias types
  285. Procedure TestAliasTypeRef;
  286. Procedure TestTypeCast_BaseTypes;
  287. Procedure TestTypeCast_AliasBaseTypes;
  288. // functions
  289. Procedure TestEmptyProc;
  290. Procedure TestProcOneParam;
  291. Procedure TestFunctionWithoutParams;
  292. Procedure TestProcedureWithoutParams;
  293. Procedure TestPrgProcVar;
  294. Procedure TestProcTwoArgs;
  295. Procedure TestProc_DefaultValue;
  296. Procedure TestUnitProcVar;
  297. Procedure TestImplProc;
  298. Procedure TestFunctionResult;
  299. Procedure TestNestedProc;
  300. Procedure TestNestedProc_ResultString;
  301. Procedure TestForwardProc;
  302. Procedure TestNestedForwardProc;
  303. Procedure TestAssignFunctionResult;
  304. Procedure TestFunctionResultInCondition;
  305. Procedure TestFunctionResultInForLoop;
  306. Procedure TestFunctionResultInTypeCast;
  307. Procedure TestExit;
  308. Procedure TestExit_ResultInFinally;
  309. Procedure TestBreak;
  310. Procedure TestBreakAsVar;
  311. Procedure TestContinue;
  312. Procedure TestProc_External;
  313. Procedure TestProc_ExternalOtherUnit;
  314. Procedure TestProc_Asm;
  315. Procedure TestProc_AsmSubBlock;
  316. Procedure TestProc_Assembler;
  317. Procedure TestProc_VarParam;
  318. Procedure TestProc_VarParamString;
  319. Procedure TestProc_VarParamV;
  320. Procedure TestProc_Overload;
  321. Procedure TestProc_OverloadForward;
  322. Procedure TestProc_OverloadIntfImpl;
  323. Procedure TestProc_OverloadNested;
  324. Procedure TestProc_OverloadNestedForward;
  325. Procedure TestProc_OverloadUnitCycle;
  326. Procedure TestProc_Varargs;
  327. Procedure TestProc_ConstOrder;
  328. Procedure TestProc_DuplicateConst;
  329. Procedure TestProc_LocalVarAbsolute;
  330. Procedure TestProc_LocalVarInit;
  331. Procedure TestProc_ReservedWords;
  332. Procedure TestProc_ConstRefWord;
  333. // anonymous functions
  334. Procedure TestAnonymousProc_Assign_ObjFPC;
  335. Procedure TestAnonymousProc_Assign_Delphi;
  336. Procedure TestAnonymousProc_Arg;
  337. Procedure TestAnonymousProc_Typecast;
  338. Procedure TestAnonymousProc_With;
  339. Procedure TestAnonymousProc_ExceptOn;
  340. Procedure TestAnonymousProc_Nested;
  341. Procedure TestAnonymousProc_NestedAssignResult;
  342. Procedure TestAnonymousProc_Class;
  343. Procedure TestAnonymousProc_ForLoop;
  344. Procedure TestAnonymousProc_AsmDelphi;
  345. // enums, sets
  346. Procedure TestEnum_Name;
  347. Procedure TestEnum_Number;
  348. Procedure TestEnum_ConstFail;
  349. Procedure TestEnum_Functions;
  350. Procedure TestEnumRg_Functions;
  351. Procedure TestEnum_AsParams;
  352. Procedure TestEnumRange_Array;
  353. Procedure TestEnum_ForIn;
  354. Procedure TestEnum_ScopedNumber;
  355. Procedure TestEnum_InFunction;
  356. Procedure TestEnum_Name_Anonymous_Unit;
  357. Procedure TestSet_Enum;
  358. Procedure TestSet_Operators;
  359. Procedure TestSet_Operator_In;
  360. Procedure TestSet_Functions;
  361. Procedure TestSet_PassAsArgClone;
  362. Procedure TestSet_AsParams;
  363. Procedure TestSet_Property;
  364. Procedure TestSet_EnumConst;
  365. Procedure TestSet_IntConst;
  366. Procedure TestSet_IntRange;
  367. Procedure TestSet_AnonymousEnumType;
  368. Procedure TestSet_AnonymousEnumTypeChar; // ToDo
  369. Procedure TestSet_ConstEnum;
  370. Procedure TestSet_ConstChar;
  371. Procedure TestSet_ConstInt;
  372. Procedure TestSet_InFunction;
  373. Procedure TestSet_ForIn;
  374. // statements
  375. Procedure TestNestBegin;
  376. Procedure TestIncDec;
  377. Procedure TestLoHiFpcMode;
  378. Procedure TestLoHiDelphiMode;
  379. Procedure TestAssignments;
  380. Procedure TestArithmeticOperators1;
  381. Procedure TestMultiAdd;
  382. Procedure TestLogicalOperators;
  383. Procedure TestBitwiseOperators;
  384. Procedure TestBitwiseOperatorsLongword;
  385. Procedure TestFunctionInt;
  386. Procedure TestFunctionString;
  387. Procedure TestIfThen;
  388. Procedure TestForLoop;
  389. Procedure TestForLoopInsideFunction;
  390. Procedure TestForLoop_ReadVarAfter;
  391. Procedure TestForLoop_Nested;
  392. Procedure TestRepeatUntil;
  393. Procedure TestAsmBlock;
  394. Procedure TestAsmPas_Impl; // ToDo
  395. Procedure TestTryFinally;
  396. Procedure TestTryExcept;
  397. Procedure TestTryExcept_ReservedWords;
  398. Procedure TestIfThenRaiseElse;
  399. Procedure TestCaseOf;
  400. Procedure TestCaseOf_UseSwitch;
  401. Procedure TestCaseOfNoElse;
  402. Procedure TestCaseOfNoElse_UseSwitch;
  403. Procedure TestCaseOfRange;
  404. Procedure TestCaseOfString;
  405. Procedure TestCaseOfChar;
  406. Procedure TestCaseOfExternalClassConst;
  407. Procedure TestDebugger;
  408. // arrays
  409. Procedure TestArray_Dynamic;
  410. Procedure TestArray_Dynamic_Nil;
  411. Procedure TestArray_DynMultiDimensional;
  412. Procedure TestArray_DynamicAssign;
  413. Procedure TestArray_StaticInt;
  414. Procedure TestArray_StaticBool;
  415. Procedure TestArray_StaticChar;
  416. Procedure TestArray_StaticMultiDim;
  417. Procedure TestArray_StaticInFunction;
  418. Procedure TestArray_StaticMultiDimEqualNotImplemented;
  419. Procedure TestArrayOfRecord;
  420. Procedure TestArray_StaticRecord;
  421. Procedure TestArrayOfSet;
  422. Procedure TestArray_DynAsParam;
  423. Procedure TestArray_StaticAsParam;
  424. Procedure TestArrayElement_AsParams;
  425. Procedure TestArrayElementFromFuncResult_AsParams;
  426. Procedure TestArrayEnumTypeRange;
  427. Procedure TestArray_SetLengthOutArg;
  428. Procedure TestArray_SetLengthProperty;
  429. Procedure TestArray_SetLengthMultiDim;
  430. Procedure TestArray_SetLengthDynOfStatic;
  431. Procedure TestArray_OpenArrayOfString;
  432. Procedure TestArray_ArrayOfCharAssignString; // ToDo
  433. Procedure TestArray_ConstRef;
  434. Procedure TestArray_Concat;
  435. Procedure TestArray_Copy;
  436. Procedure TestArray_InsertDelete;
  437. Procedure TestArray_DynArrayConstObjFPC;
  438. Procedure TestArray_DynArrayConstDelphi;
  439. Procedure TestArray_ArrayLitAsParam;
  440. Procedure TestArray_ArrayLitMultiDimAsParam;
  441. Procedure TestArray_ArrayLitStaticAsParam;
  442. Procedure TestArray_ForInArrOfString;
  443. Procedure TestExternalClass_TypeCastArrayToExternalClass;
  444. Procedure TestExternalClass_TypeCastArrayFromExternalClass;
  445. Procedure TestArrayOfConst_TVarRec;
  446. Procedure TestArrayOfConst_PassBaseTypes;
  447. Procedure TestArrayOfConst_PassObj;
  448. // record
  449. Procedure TestRecord_Empty;
  450. Procedure TestRecord_Var;
  451. Procedure TestRecord_VarExternal;
  452. Procedure TestRecord_WithDo;
  453. Procedure TestRecord_Assign;
  454. Procedure TestRecord_AsParams;
  455. Procedure TestRecord_ConstRef;
  456. Procedure TestRecordElement_AsParams;
  457. Procedure TestRecordElementFromFuncResult_AsParams;
  458. Procedure TestRecordElementFromWith_AsParams;
  459. Procedure TestRecord_Equal;
  460. Procedure TestRecord_JSValue;
  461. Procedure TestRecord_VariantFail;
  462. Procedure TestRecord_FieldArray;
  463. Procedure TestRecord_Const;
  464. Procedure TestRecord_TypecastFail;
  465. Procedure TestRecord_InFunction;
  466. Procedure TestRecord_AnonymousFail;
  467. // advanced record
  468. Procedure TestAdvRecord_Function;
  469. Procedure TestAdvRecord_Property;
  470. Procedure TestAdvRecord_PropertyDefault;
  471. Procedure TestAdvRecord_Property_ClassMethod;
  472. Procedure TestAdvRecord_Const;
  473. Procedure TestAdvRecord_ExternalField;
  474. Procedure TestAdvRecord_SubRecord;
  475. Procedure TestAdvRecord_SubClass;
  476. Procedure TestAdvRecord_SubInterfaceFail;
  477. Procedure TestAdvRecord_Constructor;
  478. Procedure TestAdvRecord_ClassConstructor_Program;
  479. Procedure TestAdvRecord_ClassConstructor_Unit;
  480. // classes
  481. Procedure TestClass_TObjectDefaultConstructor;
  482. Procedure TestClass_TObjectConstructorWithParams;
  483. Procedure TestClass_TObjectConstructorWithDefaultParam;
  484. Procedure TestClass_Var;
  485. Procedure TestClass_Method;
  486. Procedure TestClass_Implementation;
  487. Procedure TestClass_Inheritance;
  488. Procedure TestClass_TypeAlias;
  489. Procedure TestClass_AbstractMethod;
  490. Procedure TestClass_CallInherited_ProcNoParams;
  491. Procedure TestClass_CallInherited_WithParams;
  492. Procedure TestClasS_CallInheritedConstructor;
  493. Procedure TestClass_ClassVar_Assign;
  494. Procedure TestClass_CallClassMethod;
  495. Procedure TestClass_CallClassMethodStatic;
  496. Procedure TestClass_Property;
  497. Procedure TestClass_Property_ClassMethod;
  498. Procedure TestClass_Property_ClassMethodStatic;
  499. Procedure TestClass_Property_Indexed;
  500. Procedure TestClass_Property_IndexSpec;
  501. Procedure TestClass_PropertyOfTypeArray;
  502. Procedure TestClass_PropertyDefault;
  503. Procedure TestClass_PropertyDefault_TypecastToOtherDefault;
  504. //Procedure TestClass_PropertyDefault;
  505. Procedure TestClass_PropertyOverride;
  506. Procedure TestClass_PropertyIncVisibility;
  507. Procedure TestClass_Assigned;
  508. Procedure TestClass_WithClassDoCreate;
  509. Procedure TestClass_WithClassInstDoProperty;
  510. Procedure TestClass_WithClassInstDoPropertyWithParams;
  511. Procedure TestClass_WithClassInstDoFunc;
  512. Procedure TestClass_TypeCast;
  513. Procedure TestClass_TypeCastUntypedParam;
  514. Procedure TestClass_Overloads;
  515. Procedure TestClass_OverloadsAncestor;
  516. Procedure TestClass_OverloadConstructor;
  517. Procedure TestClass_OverloadDelphiOverride;
  518. Procedure TestClass_ReintroduceVarDelphi;
  519. Procedure TestClass_ReintroducedVar;
  520. Procedure TestClass_RaiseDescendant;
  521. Procedure TestClass_ExternalMethod;
  522. Procedure TestClass_ExternalVirtualNameMismatchFail;
  523. Procedure TestClass_ExternalOverrideFail;
  524. Procedure TestClass_ExternalVar;
  525. Procedure TestClass_Const;
  526. Procedure TestClass_ConstEnum;
  527. Procedure TestClass_LocalConstDuplicate_Prg;
  528. Procedure TestClass_LocalConstDuplicate_Unit;
  529. // ToDo: Procedure TestAdvRecord_LocalConstDuplicate;
  530. Procedure TestClass_LocalVarSelfFail;
  531. Procedure TestClass_ArgSelfFail;
  532. Procedure TestClass_NestedProcSelf;
  533. Procedure TestClass_NestedProcSelf2;
  534. Procedure TestClass_NestedProcClassSelf;
  535. Procedure TestClass_NestedProcCallInherited;
  536. Procedure TestClass_TObjectFree;
  537. Procedure TestClass_TObjectFree_VarArg;
  538. Procedure TestClass_TObjectFreeNewInstance;
  539. Procedure TestClass_TObjectFreeLowerCase;
  540. Procedure TestClass_TObjectFreeFunctionFail;
  541. Procedure TestClass_TObjectFreePropertyFail;
  542. Procedure TestClass_ForIn;
  543. Procedure TestClass_DispatchMessage;
  544. Procedure TestClass_Message_DuplicateIntFail;
  545. Procedure TestClass_DispatchMessage_WrongFieldNameFail;
  546. // class of
  547. Procedure TestClassOf_Create;
  548. Procedure TestClassOf_Call;
  549. Procedure TestClassOf_Assign;
  550. Procedure TestClassOf_Is;
  551. Procedure TestClassOf_Compare;
  552. Procedure TestClassOf_ClassVar;
  553. Procedure TestClassOf_ClassMethod;
  554. Procedure TestClassOf_ClassProperty;
  555. Procedure TestClassOf_ClassMethodSelf;
  556. Procedure TestClassOf_TypeCast;
  557. Procedure TestClassOf_ImplicitFunctionCall;
  558. Procedure TestClassOf_Const;
  559. // nested class
  560. Procedure TestNestedClass_Alias;
  561. Procedure TestNestedClass_Record;
  562. Procedure TestNestedClass_Class;
  563. // external class
  564. Procedure TestExternalClass_Var;
  565. Procedure TestExternalClass_Const;
  566. Procedure TestExternalClass_Dollar;
  567. Procedure TestExternalClass_DuplicateVarFail;
  568. Procedure TestExternalClass_Method;
  569. Procedure TestExternalClass_ClassMethod;
  570. Procedure TestExternalClass_ClassMethodStatic;
  571. Procedure TestExternalClass_FunctionResultInTypeCast;
  572. Procedure TestExternalClass_NonExternalOverride;
  573. Procedure TestExternalClass_OverloadHint;
  574. Procedure TestExternalClass_SameNamePublishedProperty;
  575. Procedure TestExternalClass_Property;
  576. Procedure TestExternalClass_PropertyDate;
  577. Procedure TestExternalClass_ClassProperty;
  578. Procedure TestExternalClass_ClassOf;
  579. Procedure TestExternalClass_ClassOtherUnit;
  580. Procedure TestExternalClass_Is;
  581. Procedure TestExternalClass_As;
  582. Procedure TestExternalClass_DestructorFail;
  583. Procedure TestExternalClass_New;
  584. Procedure TestExternalClass_ClassOf_New;
  585. Procedure TestExternalClass_FuncClassOf_New;
  586. Procedure TestExternalClass_New_PasClassFail;
  587. Procedure TestExternalClass_New_PasClassBracketsFail;
  588. Procedure TestExternalClass_NewExtName;
  589. Procedure TestExternalClass_Constructor;
  590. Procedure TestExternalClass_ConstructorBrackets;
  591. Procedure TestExternalClass_LocalConstSameName;
  592. Procedure TestExternalClass_ReintroduceOverload;
  593. Procedure TestExternalClass_Inherited;
  594. Procedure TestExternalClass_PascalAncestorFail;
  595. Procedure TestExternalClass_NewInstance;
  596. Procedure TestExternalClass_NewInstance_NonVirtualFail;
  597. Procedure TestExternalClass_NewInstance_FirstParamNotString_Fail;
  598. Procedure TestExternalClass_NewInstance_SecondParamTyped_Fail;
  599. Procedure TestExternalClass_JSFunctionPasDescendant;
  600. Procedure TestExternalClass_PascalProperty;
  601. Procedure TestExternalClass_TypeCastToRootClass;
  602. Procedure TestExternalClass_TypeCastToJSObject;
  603. Procedure TestExternalClass_TypeCastStringToExternalString;
  604. Procedure TestExternalClass_TypeCastToJSFunction;
  605. Procedure TestExternalClass_TypeCastDelphiUnrelated;
  606. Procedure TestExternalClass_CallClassFunctionOfInstanceFail;
  607. Procedure TestExternalClass_BracketAccessor;
  608. Procedure TestExternalClass_BracketAccessor_Call;
  609. Procedure TestExternalClass_BracketAccessor_2ParamsFail;
  610. Procedure TestExternalClass_BracketAccessor_ReadOnly;
  611. Procedure TestExternalClass_BracketAccessor_WriteOnly;
  612. Procedure TestExternalClass_BracketAccessor_MultiType;
  613. Procedure TestExternalClass_BracketAccessor_Index;
  614. Procedure TestExternalClass_ForInJSObject;
  615. Procedure TestExternalClass_ForInJSArray;
  616. Procedure TestExternalClass_IncompatibleArgDuplicateIdentifier;
  617. // class interfaces
  618. Procedure TestClassInterface_Corba;
  619. Procedure TestClassInterface_ProcExternalFail;
  620. Procedure TestClassInterface_Overloads;
  621. Procedure TestClassInterface_DuplicateGUIInIntfListFail;
  622. Procedure TestClassInterface_DuplicateGUIInAncestorFail;
  623. Procedure TestClassInterface_AncestorImpl;
  624. Procedure TestClassInterface_ImplReintroduce;
  625. Procedure TestClassInterface_MethodResolution;
  626. Procedure TestClassInterface_AncestorMoreInterfaces;
  627. Procedure TestClassInterface_MethodOverride;
  628. Procedure TestClassInterface_Corba_Delegation;
  629. Procedure TestClassInterface_Corba_DelegationStatic;
  630. Procedure TestClassInterface_Corba_Operators;
  631. Procedure TestClassInterface_Corba_Args;
  632. Procedure TestClassInterface_Corba_ForIn;
  633. Procedure TestClassInterface_COM_AssignVar;
  634. Procedure TestClassInterface_COM_AssignArg;
  635. Procedure TestClassInterface_COM_FunctionResult;
  636. Procedure TestClassInterface_COM_InheritedFuncResult;
  637. Procedure TestClassInterface_COM_IsAsTypeCasts;
  638. Procedure TestClassInterface_COM_PassAsArg;
  639. Procedure TestClassInterface_COM_PassToUntypedParam;
  640. Procedure TestClassInterface_COM_FunctionInExpr;
  641. Procedure TestClassInterface_COM_Property;
  642. Procedure TestClassInterface_COM_IntfProperty;
  643. Procedure TestClassInterface_COM_Delegation;
  644. Procedure TestClassInterface_COM_With;
  645. Procedure TestClassInterface_COM_ForIn;
  646. Procedure TestClassInterface_COM_ArrayOfIntfFail;
  647. Procedure TestClassInterface_COM_RecordIntfFail;
  648. Procedure TestClassInterface_COM_UnitInitialization;
  649. Procedure TestClassInterface_GUID;
  650. Procedure TestClassInterface_GUIDProperty;
  651. // helpers
  652. Procedure TestClassHelper_ClassVar;
  653. Procedure TestClassHelper_Method_AccessInstanceFields;
  654. Procedure TestClassHelper_Method_Call;
  655. Procedure TestClassHelper_Method_Nested_Call;
  656. Procedure TestClassHelper_ClassMethod_Call;
  657. Procedure TestClassHelper_ClassOf;
  658. Procedure TestClassHelper_MethodRefObjFPC;
  659. Procedure TestClassHelper_Constructor;
  660. Procedure TestClassHelper_InheritedObjFPC;
  661. Procedure TestClassHelper_Property;
  662. Procedure TestClassHelper_Property_Array;
  663. Procedure TestClassHelper_Property_Array_Default;
  664. Procedure TestClassHelper_Property_Array_DefaultDefault;
  665. Procedure TestClassHelper_ClassProperty;
  666. Procedure TestClassHelper_ClassPropertyStatic;
  667. Procedure TestClassHelper_ClassProperty_Array;
  668. Procedure TestClassHelper_ForIn;
  669. Procedure TestClassHelper_PassProperty;
  670. Procedure TestExtClassHelper_ClassVar;
  671. Procedure TestExtClassHelper_Method_Call;
  672. Procedure TestExtClassHelper_ClassMethod_MissingStatic;
  673. Procedure TestRecordHelper_ClassVar;
  674. Procedure TestRecordHelper_Method_Call;
  675. Procedure TestRecordHelper_Constructor;
  676. Procedure TestTypeHelper_ClassVar;
  677. Procedure TestTypeHelper_PassResultElement;
  678. Procedure TestTypeHelper_PassArgs;
  679. Procedure TestTypeHelper_PassVarConst;
  680. Procedure TestTypeHelper_PassFuncResult;
  681. Procedure TestTypeHelper_PassPropertyField;
  682. Procedure TestTypeHelper_PassPropertyGetter;
  683. Procedure TestTypeHelper_PassClassPropertyField;
  684. Procedure TestTypeHelper_PassClassPropertyGetterStatic;
  685. Procedure TestTypeHelper_PassClassPropertyGetterNonStatic;
  686. Procedure TestTypeHelper_Property;
  687. Procedure TestTypeHelper_Property_Array;
  688. Procedure TestTypeHelper_ClassProperty;
  689. Procedure TestTypeHelper_ClassProperty_Array;
  690. Procedure TestTypeHelper_ClassMethod;
  691. Procedure TestTypeHelper_ExtClassMethodFail;
  692. Procedure TestTypeHelper_Constructor;
  693. Procedure TestTypeHelper_Word;
  694. Procedure TestTypeHelper_Boolean;
  695. Procedure TestTypeHelper_WordBool;
  696. Procedure TestTypeHelper_Double;
  697. Procedure TestTypeHelper_NativeInt;
  698. Procedure TestTypeHelper_StringChar;
  699. Procedure TestTypeHelper_JSValue;
  700. Procedure TestTypeHelper_Array;
  701. Procedure TestTypeHelper_EnumType;
  702. Procedure TestTypeHelper_SetType;
  703. Procedure TestTypeHelper_InterfaceType;
  704. Procedure TestTypeHelper_NestedSelf;
  705. // proc types
  706. Procedure TestProcType;
  707. Procedure TestProcType_Arg;
  708. Procedure TestProcType_FunctionFPC;
  709. Procedure TestProcType_FunctionDelphi;
  710. Procedure TestProcType_ProcedureDelphi;
  711. Procedure TestProcType_AsParam;
  712. Procedure TestProcType_MethodFPC;
  713. Procedure TestProcType_MethodDelphi;
  714. Procedure TestProcType_PropertyFPC;
  715. Procedure TestProcType_PropertyDelphi;
  716. Procedure TestProcType_WithClassInstDoPropertyFPC;
  717. Procedure TestProcType_Nested;
  718. Procedure TestProcType_NestedOfObject;
  719. Procedure TestProcType_ReferenceToProc;
  720. Procedure TestProcType_ReferenceToMethod;
  721. Procedure TestProcType_Typecast;
  722. Procedure TestProcType_PassProcToUntyped;
  723. Procedure TestProcType_PassProcToArray;
  724. Procedure TestProcType_SafeCallObjFPC;
  725. Procedure TestProcType_SafeCallDelphi;
  726. // pointer
  727. Procedure TestPointer;
  728. Procedure TestPointer_Proc;
  729. Procedure TestPointer_AssignRecordFail;
  730. Procedure TestPointer_AssignStaticArrayFail;
  731. Procedure TestPointer_TypeCastJSValueToPointer;
  732. Procedure TestPointer_NonRecordFail;
  733. Procedure TestPointer_AnonymousArgTypeFail;
  734. Procedure TestPointer_AnonymousVarTypeFail;
  735. Procedure TestPointer_AnonymousResultTypeFail;
  736. Procedure TestPointer_AddrOperatorFail;
  737. Procedure TestPointer_ArrayParamsFail;
  738. Procedure TestPointer_PointerAddFail;
  739. Procedure TestPointer_IncPointerFail;
  740. Procedure TestPointer_Record;
  741. Procedure TestPointer_RecordArg;
  742. // jsvalue
  743. Procedure TestJSValue_AssignToJSValue;
  744. Procedure TestJSValue_TypeCastToBaseType;
  745. Procedure TestJSValue_TypecastToJSValue;
  746. Procedure TestJSValue_Equal;
  747. Procedure TestJSValue_If;
  748. Procedure TestJSValue_Not;
  749. Procedure TestJSValue_Enum;
  750. Procedure TestJSValue_ClassInstance;
  751. Procedure TestJSValue_ClassOf;
  752. Procedure TestJSValue_ArrayOfJSValue;
  753. Procedure TestJSValue_ArrayLit;
  754. Procedure TestJSValue_Params;
  755. Procedure TestJSValue_UntypedParam;
  756. Procedure TestJSValue_FuncResultType;
  757. Procedure TestJSValue_ProcType_Assign;
  758. Procedure TestJSValue_ProcType_Equal;
  759. Procedure TestJSValue_ProcType_Param;
  760. Procedure TestJSValue_AssignToPointerFail;
  761. Procedure TestJSValue_OverloadDouble;
  762. Procedure TestJSValue_OverloadNativeInt;
  763. Procedure TestJSValue_OverloadWord;
  764. Procedure TestJSValue_OverloadString;
  765. Procedure TestJSValue_OverloadChar;
  766. Procedure TestJSValue_OverloadPointer;
  767. Procedure TestJSValue_ForIn;
  768. // RTTI
  769. Procedure TestRTTI_IntRange;
  770. Procedure TestRTTI_Double;
  771. Procedure TestRTTI_ProcType;
  772. Procedure TestRTTI_ProcType_ArgFromOtherUnit;
  773. Procedure TestRTTI_EnumAndSetType;
  774. Procedure TestRTTI_EnumRange;
  775. Procedure TestRTTI_AnonymousEnumType;
  776. Procedure TestRTTI_StaticArray;
  777. Procedure TestRTTI_DynArray;
  778. Procedure TestRTTI_ArrayNestedAnonymous;
  779. Procedure TestRTTI_PublishedMethodOverloadFail;
  780. Procedure TestRTTI_PublishedMethodHideNoHint;
  781. Procedure TestRTTI_PublishedMethodExternalFail;
  782. Procedure TestRTTI_PublishedClassPropertyFail;
  783. Procedure TestRTTI_PublishedClassFieldFail;
  784. Procedure TestRTTI_PublishedFieldExternalFail;
  785. Procedure TestRTTI_Class_Field;
  786. Procedure TestRTTI_Class_Method;
  787. Procedure TestRTTI_Class_MethodArgFlags;
  788. Procedure TestRTTI_Class_Property;
  789. Procedure TestRTTI_Class_PropertyParams;
  790. Procedure TestRTTI_Class_OtherUnit_TypeAlias;
  791. Procedure TestRTTI_Class_OmitRTTI;
  792. Procedure TestRTTI_Class_Field_AnonymousArrayOfSelfClass;
  793. Procedure TestRTTI_IndexModifier;
  794. Procedure TestRTTI_StoredModifier;
  795. Procedure TestRTTI_DefaultValue;
  796. Procedure TestRTTI_DefaultValueSet;
  797. Procedure TestRTTI_DefaultValueRangeType;
  798. Procedure TestRTTI_DefaultValueInherit;
  799. Procedure TestRTTI_OverrideMethod;
  800. Procedure TestRTTI_ReintroduceMethod;
  801. Procedure TestRTTI_OverloadProperty;
  802. // ToDo: array argument
  803. Procedure TestRTTI_ClassForward;
  804. Procedure TestRTTI_ClassOf;
  805. Procedure TestRTTI_Record;
  806. Procedure TestRTTI_RecordAnonymousArray;
  807. Procedure TestRTTI_Record_ClassVarType;
  808. Procedure TestRTTI_LocalTypes;
  809. Procedure TestRTTI_TypeInfo_BaseTypes;
  810. Procedure TestRTTI_TypeInfo_Type_BaseTypes;
  811. Procedure TestRTTI_TypeInfo_LocalFail;
  812. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses1;
  813. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses2;
  814. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses3;
  815. Procedure TestRTTI_TypeInfo_FunctionClassType;
  816. Procedure TestRTTI_TypeInfo_MixedUnits_PointerAndClass;
  817. Procedure TestRTTI_Interface_Corba;
  818. Procedure TestRTTI_Interface_COM;
  819. Procedure TestRTTI_ClassHelper;
  820. Procedure TestRTTI_ExternalClass;
  821. Procedure TestRTTI_Unit;
  822. // Resourcestring
  823. Procedure TestResourcestringProgram;
  824. Procedure TestResourcestringUnit;
  825. Procedure TestResourcestringImplementation;
  826. // Attributes
  827. Procedure TestAttributes_Members;
  828. Procedure TestAttributes_Types;
  829. Procedure TestAttributes_HelperConstructor_Fail;
  830. // Assertions, checks
  831. procedure TestAssert;
  832. procedure TestAssert_SysUtils;
  833. procedure TestObjectChecks;
  834. procedure TestOverflowChecks_Int;
  835. procedure TestRangeChecks_AssignInt;
  836. procedure TestRangeChecks_AssignIntRange;
  837. procedure TestRangeChecks_AssignEnum;
  838. procedure TestRangeChecks_AssignEnumRange;
  839. procedure TestRangeChecks_AssignChar;
  840. procedure TestRangeChecks_AssignCharRange;
  841. procedure TestRangeChecks_ArrayIndex;
  842. procedure TestRangeChecks_ArrayOfRecIndex;
  843. procedure TestRangeChecks_StringIndex;
  844. procedure TestRangeChecks_TypecastInt;
  845. procedure TestRangeChecks_TypeHelperInt;
  846. // Async/AWait
  847. Procedure TestAsync_Proc;
  848. Procedure TestAsync_CallResultIsPromise;
  849. Procedure TestAsync_ConstructorFail;
  850. Procedure TestAsync_PropertyGetterFail;
  851. Procedure TestAwait_NonPromiseWithTypeFail;
  852. Procedure TestAwait_AsyncCallTypeMismatch;
  853. Procedure TestAWait_OutsideAsyncFail;
  854. Procedure TestAWait_IntegerFail;
  855. Procedure TestAWait_ExternalClassPromise;
  856. Procedure TestAWait_JSValue;
  857. Procedure TestAWait_Result;
  858. Procedure TestAWait_ResultPromiseMissingTypeFail; // await(AsyncCallResultPromise) needs T
  859. Procedure TestAsync_AnonymousProc;
  860. Procedure TestAsync_ProcType;
  861. Procedure TestAsync_ProcTypeAsyncModMismatchFail;
  862. Procedure TestAsync_Inherited;
  863. Procedure TestAsync_ClassInterface;
  864. Procedure TestAsync_ClassInterface_AsyncMissmatchFail;
  865. // Library
  866. Procedure TestLibrary_Empty;
  867. Procedure TestLibrary_ExportFunc;
  868. Procedure TestLibrary_Export_Index_Fail;
  869. Procedure TestLibrary_ExportVar;
  870. Procedure TestLibrary_ExportUnitFunc;
  871. // ToDo: test delayed specialization init
  872. // ToDo: analyzer
  873. end;
  874. function LinesToStr(Args: array of const): string;
  875. function ExtractFileUnitName(aFilename: string): string;
  876. function JSToStr(El: TJSElement): string;
  877. function CheckSrcDiff(Expected, Actual: string; out Msg: string): boolean;
  878. implementation
  879. function LinesToStr(Args: array of const): string;
  880. var
  881. s: String;
  882. i: Integer;
  883. begin
  884. s:='';
  885. for i:=Low(Args) to High(Args) do
  886. case Args[i].VType of
  887. vtChar: s += Args[i].VChar+LineEnding;
  888. vtString: s += Args[i].VString^+LineEnding;
  889. vtPChar: s += Args[i].VPChar+LineEnding;
  890. vtWideChar: s += AnsiString(Args[i].VWideChar)+LineEnding;
  891. vtPWideChar: s += AnsiString(Args[i].VPWideChar)+LineEnding;
  892. vtAnsiString: s += AnsiString(Args[i].VAnsiString)+LineEnding;
  893. vtWidestring: s += AnsiString(WideString(Args[i].VWideString))+LineEnding;
  894. vtUnicodeString:s += AnsiString(UnicodeString(Args[i].VUnicodeString))+LineEnding;
  895. end;
  896. Result:=s;
  897. end;
  898. function ExtractFileUnitName(aFilename: string): string;
  899. var
  900. p: Integer;
  901. begin
  902. Result:=ExtractFileName(aFilename);
  903. if Result='' then exit;
  904. for p:=length(Result) downto 1 do
  905. case Result[p] of
  906. '/','\': exit;
  907. '.':
  908. begin
  909. Delete(Result,p,length(Result));
  910. exit;
  911. end;
  912. end;
  913. end;
  914. function JSToStr(El: TJSElement): string;
  915. var
  916. aWriter: TBufferWriter;
  917. aJSWriter: TJSWriter;
  918. begin
  919. aJSWriter:=nil;
  920. aWriter:=TBufferWriter.Create(1000);
  921. try
  922. aJSWriter:=TJSWriter.Create(aWriter);
  923. aJSWriter.IndentSize:=2;
  924. aJSWriter.WriteJS(El);
  925. Result:=aWriter.AsString;
  926. finally
  927. aJSWriter.Free;
  928. aWriter.Free;
  929. end;
  930. end;
  931. function CheckSrcDiff(Expected, Actual: string; out Msg: string): boolean;
  932. // search diff, ignore changes in spaces
  933. const
  934. SpaceChars = [#9,#10,#13,' '];
  935. var
  936. ExpectedP, ActualP: PChar;
  937. function FindLineEnd(p: PChar): PChar;
  938. begin
  939. Result:=p;
  940. while not (Result^ in [#0,#10,#13]) do inc(Result);
  941. end;
  942. function FindLineStart(p, MinP: PChar): PChar;
  943. begin
  944. while (p>MinP) and not (p[-1] in [#10,#13]) do dec(p);
  945. Result:=p;
  946. end;
  947. procedure SkipLineEnd(var p: PChar);
  948. begin
  949. if p^ in [#10,#13] then
  950. begin
  951. if (p[1] in [#10,#13]) and (p^<>p[1]) then
  952. inc(p,2)
  953. else
  954. inc(p);
  955. end;
  956. end;
  957. function HasSpecialChar(s: string): boolean;
  958. var
  959. i: Integer;
  960. begin
  961. for i:=1 to length(s) do
  962. if s[i] in [#0..#31,#127..#255] then
  963. exit(true);
  964. Result:=false;
  965. end;
  966. function HashSpecialChars(s: string): string;
  967. var
  968. i: Integer;
  969. begin
  970. Result:='';
  971. for i:=1 to length(s) do
  972. if s[i] in [#0..#31,#127..#255] then
  973. Result:=Result+'#'+hexstr(ord(s[i]),2)
  974. else
  975. Result:=Result+s[i];
  976. end;
  977. procedure DiffFound;
  978. var
  979. ActLineStartP, ActLineEndP, p, StartPos: PChar;
  980. ExpLine, ActLine: String;
  981. i, LineNo, DiffLineNo: Integer;
  982. begin
  983. writeln('Diff found "',Msg,'". Lines:');
  984. // write correct lines
  985. p:=PChar(Expected);
  986. LineNo:=0;
  987. DiffLineNo:=0;
  988. repeat
  989. StartPos:=p;
  990. while not (p^ in [#0,#10,#13]) do inc(p);
  991. ExpLine:=copy(Expected,StartPos-PChar(Expected)+1,p-StartPos);
  992. SkipLineEnd(p);
  993. inc(LineNo);
  994. if (p<=ExpectedP) and (p^<>#0) then
  995. begin
  996. writeln('= ',ExpLine);
  997. end else begin
  998. // diff line
  999. if DiffLineNo=0 then DiffLineNo:=LineNo;
  1000. // write actual line
  1001. ActLineStartP:=FindLineStart(ActualP,PChar(Actual));
  1002. ActLineEndP:=FindLineEnd(ActualP);
  1003. ActLine:=copy(Actual,ActLineStartP-PChar(Actual)+1,ActLineEndP-ActLineStartP);
  1004. writeln('- ',ActLine);
  1005. if HasSpecialChar(ActLine) then
  1006. writeln('- ',HashSpecialChars(ActLine));
  1007. // write expected line
  1008. writeln('+ ',ExpLine);
  1009. if HasSpecialChar(ExpLine) then
  1010. writeln('- ',HashSpecialChars(ExpLine));
  1011. // write empty line with pointer ^
  1012. for i:=1 to 2+ExpectedP-StartPos do write(' ');
  1013. writeln('^');
  1014. Msg:='expected "'+ExpLine+'", but got "'+ActLine+'".';
  1015. CheckSrcDiff:=false;
  1016. // write up to three following actual lines to get some context
  1017. for i:=1 to 3 do begin
  1018. ActLineStartP:=ActLineEndP;
  1019. SkipLineEnd(ActLineStartP);
  1020. if ActLineStartP^=#0 then break;
  1021. ActLineEndP:=FindLineEnd(ActLineStartP);
  1022. ActLine:=copy(Actual,ActLineStartP-PChar(Actual)+1,ActLineEndP-ActLineStartP);
  1023. writeln('~ ',ActLine);
  1024. end;
  1025. exit;
  1026. end;
  1027. until p^=#0;
  1028. writeln('DiffFound Actual:-----------------------');
  1029. writeln(Actual);
  1030. writeln('DiffFound Expected:---------------------');
  1031. writeln(Expected);
  1032. writeln('DiffFound ------------------------------');
  1033. Msg:='diff found, but lines are the same, internal error';
  1034. CheckSrcDiff:=false;
  1035. end;
  1036. var
  1037. IsSpaceNeeded: Boolean;
  1038. LastChar, Quote: Char;
  1039. begin
  1040. Result:=true;
  1041. Msg:='';
  1042. if Expected='' then Expected:=' ';
  1043. if Actual='' then Actual:=' ';
  1044. ExpectedP:=PChar(Expected);
  1045. ActualP:=PChar(Actual);
  1046. repeat
  1047. //writeln('TTestModule.CheckDiff Exp="',ExpectedP^,'" Act="',ActualP^,'"');
  1048. case ExpectedP^ of
  1049. #0:
  1050. begin
  1051. // check that rest of Actual has only spaces
  1052. while ActualP^ in SpaceChars do inc(ActualP);
  1053. if ActualP^<>#0 then
  1054. begin
  1055. DiffFound;
  1056. exit;
  1057. end;
  1058. exit(true);
  1059. end;
  1060. ' ',#9,#10,#13:
  1061. begin
  1062. // skip space in Expected
  1063. IsSpaceNeeded:=false;
  1064. if ExpectedP>PChar(Expected) then
  1065. LastChar:=ExpectedP[-1]
  1066. else
  1067. LastChar:=#0;
  1068. while ExpectedP^ in SpaceChars do inc(ExpectedP);
  1069. if (LastChar in ['a'..'z','A'..'Z','0'..'9','_','$'])
  1070. and (ExpectedP^ in ['a'..'z','A'..'Z','0'..'9','_','$']) then
  1071. IsSpaceNeeded:=true;
  1072. if IsSpaceNeeded and (not (ActualP^ in SpaceChars)) then
  1073. begin
  1074. DiffFound;
  1075. exit;
  1076. end;
  1077. while ActualP^ in SpaceChars do inc(ActualP);
  1078. end;
  1079. '''','"':
  1080. begin
  1081. while ActualP^ in SpaceChars do inc(ActualP);
  1082. if ExpectedP^<>ActualP^ then
  1083. begin
  1084. DiffFound;
  1085. exit;
  1086. end;
  1087. Quote:=ExpectedP^;
  1088. repeat
  1089. inc(ExpectedP);
  1090. inc(ActualP);
  1091. if ExpectedP^<>ActualP^ then
  1092. begin
  1093. DiffFound;
  1094. exit;
  1095. end;
  1096. if (ExpectedP^ in [#0,#10,#13]) then
  1097. break
  1098. else if (ExpectedP^=Quote) then
  1099. begin
  1100. inc(ExpectedP);
  1101. inc(ActualP);
  1102. break;
  1103. end;
  1104. until false;
  1105. end;
  1106. else
  1107. while ActualP^ in SpaceChars do inc(ActualP);
  1108. if ExpectedP^<>ActualP^ then
  1109. begin
  1110. DiffFound;
  1111. exit;
  1112. end;
  1113. inc(ExpectedP);
  1114. inc(ActualP);
  1115. end;
  1116. until false;
  1117. end;
  1118. { TTestEnginePasResolver }
  1119. destructor TTestEnginePasResolver.Destroy;
  1120. begin
  1121. FreeAndNil(FStreamResolver);
  1122. FreeAndNil(FParser);
  1123. FreeAndNil(FScanner);
  1124. FreeAndNil(FStreamResolver);
  1125. if Module<>nil then
  1126. begin
  1127. Module.Release{$IFDEF CheckPasTreeRefCount}('CreateElement'){$ENDIF};
  1128. FModule:=nil;
  1129. end;
  1130. inherited Destroy;
  1131. end;
  1132. function TTestEnginePasResolver.FindUnit(const AName, InFilename: String;
  1133. NameExpr, InFileExpr: TPasExpr): TPasModule;
  1134. begin
  1135. Result:=nil;
  1136. if InFilename<>'' then
  1137. RaiseNotYetImplemented(20180224101926,InFileExpr,'Use testcase tcunitsearch instead');
  1138. if Assigned(OnFindUnit) then
  1139. Result:=OnFindUnit(AName);
  1140. if NameExpr=nil then ;
  1141. end;
  1142. procedure TTestEnginePasResolver.UsedInterfacesFinished(Section: TPasSection);
  1143. begin
  1144. // do not parse recursively
  1145. // parse via the queue
  1146. if Section=nil then ;
  1147. end;
  1148. { TCustomTestModule }
  1149. function TCustomTestModule.GetMsgCount: integer;
  1150. begin
  1151. Result:=FHintMsgs.Count;
  1152. end;
  1153. function TCustomTestModule.GetMsgs(Index: integer): TTestHintMessage;
  1154. begin
  1155. Result:=TTestHintMessage(FHintMsgs[Index]);
  1156. end;
  1157. function TCustomTestModule.GetResolverCount: integer;
  1158. begin
  1159. Result:=FModules.Count;
  1160. end;
  1161. function TCustomTestModule.GetResolvers(Index: integer
  1162. ): TTestEnginePasResolver;
  1163. begin
  1164. Result:=TTestEnginePasResolver(FModules[Index]);
  1165. end;
  1166. function TCustomTestModule.OnPasResolverFindUnit(const aUnitName: String
  1167. ): TPasModule;
  1168. var
  1169. DefNamespace: String;
  1170. begin
  1171. //writeln('TTestModule.OnPasResolverFindUnit START Unit="',aUnitName,'"');
  1172. if (Pos('.',aUnitName)<1) then
  1173. begin
  1174. DefNamespace:=GetDefaultNamespace;
  1175. if DefNamespace<>'' then
  1176. begin
  1177. Result:=LoadUnit(DefNamespace+'.'+aUnitName);
  1178. if Result<>nil then exit;
  1179. end;
  1180. end;
  1181. Result:=LoadUnit(aUnitName);
  1182. if Result<>nil then exit;
  1183. {$IFDEF VerbosePas2JS}
  1184. writeln('TTestModule.OnPasResolverFindUnit missing unit "',aUnitName,'"');
  1185. {$ENDIF}
  1186. Fail('can''t find unit "'+aUnitName+'"');
  1187. end;
  1188. procedure TCustomTestModule.OnParserLog(Sender: TObject; const Msg: String);
  1189. var
  1190. aParser: TPasParser;
  1191. Item: TTestHintMessage;
  1192. begin
  1193. aParser:=Sender as TPasParser;
  1194. Item:=TTestHintMessage.Create;
  1195. Item.Id:=aParser.LastMsgNumber;
  1196. Item.MsgType:=aParser.LastMsgType;
  1197. Item.MsgNumber:=aParser.LastMsgNumber;
  1198. Item.Msg:=Msg;
  1199. Item.SourcePos:=aParser.Scanner.CurSourcePos;
  1200. {$IFDEF VerbosePas2JS}
  1201. writeln('TCustomTestModule.OnParserLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1202. {$ENDIF}
  1203. FHintMsgs.Add(Item);
  1204. end;
  1205. procedure TCustomTestModule.OnPasResolverLog(Sender: TObject; const Msg: String
  1206. );
  1207. var
  1208. aResolver: TTestEnginePasResolver;
  1209. Item: TTestHintMessage;
  1210. begin
  1211. aResolver:=Sender as TTestEnginePasResolver;
  1212. Item:=TTestHintMessage.Create;
  1213. Item.Id:=aResolver.LastMsgId;
  1214. Item.MsgType:=aResolver.LastMsgType;
  1215. Item.MsgNumber:=aResolver.LastMsgNumber;
  1216. Item.Msg:=Msg;
  1217. Item.SourcePos:=aResolver.LastSourcePos;
  1218. {$IFDEF VerbosePas2JS}
  1219. writeln('TCustomTestModule.OnPasResolverLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1220. {$ENDIF}
  1221. FHintMsgs.Add(Item);
  1222. end;
  1223. procedure TCustomTestModule.OnScannerLog(Sender: TObject; const Msg: String);
  1224. var
  1225. Item: TTestHintMessage;
  1226. aScanner: TPas2jsPasScanner;
  1227. begin
  1228. aScanner:=Sender as TPas2jsPasScanner;
  1229. Item:=TTestHintMessage.Create;
  1230. Item.Id:=aScanner.LastMsgNumber;
  1231. Item.MsgType:=aScanner.LastMsgType;
  1232. Item.MsgNumber:=aScanner.LastMsgNumber;
  1233. Item.Msg:=Msg;
  1234. Item.SourcePos:=aScanner.CurSourcePos;
  1235. {$IFDEF VerbosePas2JS}
  1236. writeln('TCustomTestModule.OnScannerLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1237. {$ENDIF}
  1238. FHintMsgs.Add(Item);
  1239. end;
  1240. procedure TCustomTestModule.SetWithTypeInfo(const AValue: boolean);
  1241. begin
  1242. if FWithTypeInfo=AValue then Exit;
  1243. FWithTypeInfo:=AValue;
  1244. if AValue then
  1245. Converter.Options:=Converter.Options-[coNoTypeInfo]
  1246. else
  1247. Converter.Options:=Converter.Options+[coNoTypeInfo];
  1248. end;
  1249. function TCustomTestModule.LoadUnit(const aUnitName: String): TPasModule;
  1250. var
  1251. i: Integer;
  1252. CurEngine: TTestEnginePasResolver;
  1253. CurUnitName: String;
  1254. begin
  1255. //writeln('TTestModule.FindUnit START Unit="',aUnitName,'"');
  1256. Result:=nil;
  1257. if (Module.ClassType=TPasModule)
  1258. and (CompareText(Module.Name,aUnitName)=0) then
  1259. exit(Module);
  1260. for i:=0 to ResolverCount-1 do
  1261. begin
  1262. CurEngine:=Resolvers[i];
  1263. CurUnitName:=ExtractFileUnitName(CurEngine.Filename);
  1264. //writeln('TTestModule.FindUnit Checking ',i,'/',ResolverCount,' ',CurEngine.Filename,' ',CurUnitName);
  1265. if CompareText(aUnitName,CurUnitName)=0 then
  1266. begin
  1267. Result:=CurEngine.Module;
  1268. if Result<>nil then exit;
  1269. //writeln('TTestModule.FindUnit PARSING unit "',CurEngine.Filename,'"');
  1270. FileResolver.FindSourceFile(aUnitName);
  1271. CurEngine.StreamResolver:=TStreamResolver.Create;
  1272. CurEngine.StreamResolver.OwnsStreams:=True;
  1273. //writeln('TTestModule.FindUnit SOURCE=',CurEngine.Source);
  1274. CurEngine.StreamResolver.AddStream(CurEngine.FileName,TStringStream.Create(CurEngine.Source));
  1275. CurEngine.Scanner:=TPas2jsPasScanner.Create(CurEngine.StreamResolver);
  1276. InitScanner(CurEngine.Scanner);
  1277. CurEngine.Parser:=TTestPasParser.Create(CurEngine.Scanner,CurEngine.StreamResolver,CurEngine);
  1278. CurEngine.Parser.Options:=po_tcmodules;
  1279. if CompareText(CurUnitName,'System')=0 then
  1280. CurEngine.Parser.ImplicitUses.Clear;
  1281. CurEngine.Scanner.OpenFile(CurEngine.Filename);
  1282. try
  1283. CurEngine.Parser.NextToken;
  1284. CurEngine.Parser.ParseUnit(CurEngine.FModule);
  1285. except
  1286. on E: Exception do
  1287. HandleException(E);
  1288. end;
  1289. //writeln('TTestModule.FindUnit END ',CurUnitName);
  1290. Result:=CurEngine.Module;
  1291. exit;
  1292. end;
  1293. end;
  1294. end;
  1295. procedure TCustomTestModule.SetUp;
  1296. begin
  1297. {$IFDEF EnablePasTreeGlobalRefCount}
  1298. FElementRefCountAtSetup:=TPasElement.GlobalRefCount;
  1299. {$ENDIF}
  1300. if FModules<>nil then
  1301. begin
  1302. writeln('TCustomTestModule.SetUp FModules<>nil');
  1303. Halt;
  1304. end;
  1305. inherited SetUp;
  1306. FSkipTests:=false;
  1307. FWithTypeInfo:=false;
  1308. FSource:=TStringList.Create;
  1309. FHub:=TPas2JSResolverHub.Create(Self);
  1310. FModules:=TObjectList.Create(true);
  1311. FFilename:='test1.pp';
  1312. FFileResolver:=TStreamResolver.Create;
  1313. FFileResolver.OwnsStreams:=True;
  1314. FScanner:=TPas2jsPasScanner.Create(FFileResolver);
  1315. InitScanner(FScanner);
  1316. FEngine:=AddModule(Filename);
  1317. FEngine.Scanner:=FScanner;
  1318. FScanner.Resolver:=FEngine;
  1319. FParser:=TTestPasParser.Create(FScanner,FFileResolver,FEngine);
  1320. FParser.OnLog:=@OnParserLog;
  1321. FEngine.Parser:=FParser;
  1322. Parser.Options:=po_tcmodules;
  1323. FModule:=Nil;
  1324. FConverter:=CreateConverter;
  1325. FExpectedErrorClass:=nil;
  1326. end;
  1327. function TCustomTestModule.CreateConverter: TPasToJSConverter;
  1328. var
  1329. Options: TPasToJsConverterOptions;
  1330. begin
  1331. Result:=TPasToJSConverter.Create;
  1332. Options:=co_tcmodules;
  1333. if WithTypeInfo then
  1334. Exclude(Options,coNoTypeInfo)
  1335. else
  1336. Include(Options,coNoTypeInfo);
  1337. Result.Options:=Options;
  1338. Result.Globals:=TPasToJSConverterGlobals.Create(Result);
  1339. end;
  1340. procedure TCustomTestModule.InitScanner(aScanner: TPas2jsPasScanner);
  1341. begin
  1342. aScanner.AllowedModeSwitches:=msAllPas2jsModeSwitches;
  1343. aScanner.ReadOnlyModeSwitches:=msAllPas2jsModeSwitchesReadOnly;
  1344. aScanner.CurrentModeSwitches:=OBJFPCModeSwitches*msAllPas2jsModeSwitches+msAllPas2jsModeSwitchesReadOnly;
  1345. aScanner.AllowedBoolSwitches:=bsAllPas2jsBoolSwitches;
  1346. aScanner.ReadOnlyBoolSwitches:=bsAllPas2jsBoolSwitchesReadOnly;
  1347. aScanner.CurrentBoolSwitches:=bsAllPas2jsBoolSwitchesReadOnly+[bsHints,bsNotes,bsWarnings,bsWriteableConst];
  1348. aScanner.AllowedValueSwitches:=vsAllPas2jsValueSwitches;
  1349. aScanner.ReadOnlyValueSwitches:=vsAllPas2jsValueSwitchesReadOnly;
  1350. aScanner.OnLog:=@OnScannerLog;
  1351. aScanner.CompilerVersion:='Comp.Ver.tcmodules';
  1352. end;
  1353. procedure TCustomTestModule.TearDown;
  1354. {$IFDEF CheckPasTreeRefCount}
  1355. var
  1356. El: TPasElement;
  1357. {$ENDIF}
  1358. var
  1359. i: Integer;
  1360. CurModule: TPasModule;
  1361. begin
  1362. FHintMsgs.Clear;
  1363. FHintMsgsGood.Clear;
  1364. FSkipTests:=false;
  1365. FWithTypeInfo:=false;
  1366. FJSRegModuleCall:=nil;
  1367. FJSModuleCallArgs:=nil;
  1368. FJSImplentationUses:=nil;
  1369. FJSInterfaceUses:=nil;
  1370. FJSModuleSrc:=nil;
  1371. FJSInitBody:=nil;
  1372. FreeAndNil(FJSSource);
  1373. FreeAndNil(FJSModule);
  1374. FreeAndNil(FConverter);
  1375. Engine.Clear;
  1376. FreeAndNil(FSource);
  1377. FreeAndNil(FFileResolver);
  1378. if FModules<>nil then
  1379. begin
  1380. for i:=0 to FModules.Count-1 do
  1381. begin
  1382. CurModule:=TTestEnginePasResolver(FModules[i]).Module;
  1383. if CurModule=nil then continue;
  1384. //writeln('TCustomTestModule.TearDown ReleaseUsedUnits ',CurModule.Name,' ',CurModule.RefCount,' ',CurModule.RefIds.Text);
  1385. CurModule.ReleaseUsedUnits;
  1386. end;
  1387. if FModule<>nil then
  1388. FModule.ReleaseUsedUnits;
  1389. for i:=0 to FModules.Count-1 do
  1390. begin
  1391. CurModule:=TTestEnginePasResolver(FModules[i]).Module;
  1392. if CurModule=nil then continue;
  1393. //writeln('TCustomTestModule.TearDown UsesReleased ',CurModule.Name,' ',CurModule.RefCount,' ',CurModule.RefIds.Text);
  1394. end;
  1395. FreeAndNil(FModules);
  1396. ReleaseAndNil(TPasElement(FModule){$IFDEF CheckPasTreeRefCount},'CreateElement'{$ENDIF});
  1397. FEngine:=nil;
  1398. end;
  1399. FreeAndNil(FHub);
  1400. inherited TearDown;
  1401. {$IFDEF EnablePasTreeGlobalRefCount}
  1402. if FElementRefCountAtSetup<>TPasElement.GlobalRefCount then
  1403. begin
  1404. writeln('TCustomTestModule.TearDown GlobalRefCount Was='+IntToStr(FElementRefCountAtSetup)+' Now='+IntToStr(TPasElement.GlobalRefCount));
  1405. {$IFDEF CheckPasTreeRefCount}
  1406. El:=TPasElement.FirstRefEl;
  1407. while El<>nil do
  1408. begin
  1409. writeln(' ',GetObjName(El),' RefIds.Count=',El.RefIds.Count,':');
  1410. for i:=0 to El.RefIds.Count-1 do
  1411. writeln(' ',El.RefIds[i]);
  1412. El:=El.NextRefEl;
  1413. end;
  1414. {$ENDIF}
  1415. Halt;
  1416. Fail('TCustomTestModule.TearDown Was='+IntToStr(FElementRefCountAtSetup)+' Now='+IntToStr(TPasElement.GlobalRefCount));
  1417. end;
  1418. {$ENDIF}
  1419. end;
  1420. procedure TCustomTestModule.Add(Line: string);
  1421. begin
  1422. Source.Add(Line);
  1423. end;
  1424. procedure TCustomTestModule.Add(const Lines: array of string);
  1425. var
  1426. i: Integer;
  1427. begin
  1428. for i:=low(Lines) to high(Lines) do
  1429. Add(Lines[i]);
  1430. end;
  1431. procedure TCustomTestModule.StartParsing;
  1432. var
  1433. Src: String;
  1434. begin
  1435. Src:=Source.Text;
  1436. FEngine.Source:=Src;
  1437. FileResolver.AddStream(FileName,TStringStream.Create(Src));
  1438. Scanner.OpenFile(FileName);
  1439. Writeln('// Test : ',Self.TestName);
  1440. Writeln(Src);
  1441. end;
  1442. procedure TCustomTestModule.ParseModuleQueue;
  1443. var
  1444. i: Integer;
  1445. CurResolver: TTestEnginePasResolver;
  1446. Found: Boolean;
  1447. Section: TPasSection;
  1448. begin
  1449. // parse til exception or all modules finished
  1450. while not SkipTests do
  1451. begin
  1452. Found:=false;
  1453. for i:=0 to ResolverCount-1 do
  1454. begin
  1455. CurResolver:=Resolvers[i];
  1456. if CurResolver.CurrentParser=nil then continue;
  1457. if not CurResolver.CurrentParser.CanParseContinue(Section) then
  1458. continue;
  1459. CurResolver.Parser.ParseContinue;
  1460. Found:=true;
  1461. break;
  1462. end;
  1463. if not Found then break;
  1464. end;
  1465. for i:=0 to ResolverCount-1 do
  1466. begin
  1467. CurResolver:=Resolvers[i];
  1468. if CurResolver.Parser=nil then
  1469. begin
  1470. if CurResolver.CurrentParser<>nil then
  1471. Fail('TCustomTestModule.ParseModuleQueue '+CurResolver.Filename+' '+GetObjName(CurResolver.Parser)+'=Parser<>CurrentParser='+GetObjName(CurResolver.CurrentParser));
  1472. continue;
  1473. end;
  1474. if CurResolver.Parser.CurModule<>nil then
  1475. Fail('TCustomTestModule.ParseModuleQueue '+CurResolver.Filename+' NOT FINISHED CurModule='+GetObjName(CurResolver.Parser.CurModule));
  1476. end;
  1477. end;
  1478. procedure TCustomTestModule.ParseModule;
  1479. begin
  1480. if SkipTests then exit;
  1481. FFirstPasStatement:=nil;
  1482. try
  1483. StartParsing;
  1484. Parser.ParseMain(FModule);
  1485. ParseModuleQueue;
  1486. except
  1487. on E: Exception do
  1488. HandleException(E);
  1489. end;
  1490. if SkipTests then exit;
  1491. AssertNotNull('Module resulted in Module',Module);
  1492. AssertEquals('modulename',lowercase(ChangeFileExt(FFileName,'')),lowercase(Module.Name));
  1493. TAssert.AssertSame('Has resolver',Engine,Parser.Engine);
  1494. end;
  1495. procedure TCustomTestModule.ParseProgram;
  1496. begin
  1497. if SkipTests then exit;
  1498. ParseModule;
  1499. if SkipTests then exit;
  1500. AssertEquals('Has program',TPasProgram,Module.ClassType);
  1501. FPasProgram:=TPasProgram(Module);
  1502. AssertNotNull('Has program section',PasProgram.ProgramSection);
  1503. AssertNotNull('Has initialization section',PasProgram.InitializationSection);
  1504. if (PasProgram.InitializationSection.Elements.Count>0) then
  1505. if TObject(PasProgram.InitializationSection.Elements[0]) is TPasImplBlock then
  1506. FFirstPasStatement:=TPasImplBlock(PasProgram.InitializationSection.Elements[0]);
  1507. end;
  1508. procedure TCustomTestModule.ParseLibrary;
  1509. var
  1510. Init: TInitializationSection;
  1511. begin
  1512. if SkipTests then exit;
  1513. ParseModule;
  1514. if SkipTests then exit;
  1515. AssertEquals('Has library',TPasLibrary,Module.ClassType);
  1516. FPasLibrary:=TPasLibrary(Module);
  1517. AssertNotNull('Has library section',PasLibrary.LibrarySection);
  1518. Init:=PasLibrary.InitializationSection;
  1519. if (Init<>nil) and (Init.Elements.Count>0) then
  1520. if TObject(Init.Elements[0]) is TPasImplBlock then
  1521. FFirstPasStatement:=TPasImplBlock(PasLibrary.InitializationSection.Elements[0]);
  1522. end;
  1523. procedure TCustomTestModule.ParseUnit;
  1524. begin
  1525. if SkipTests then exit;
  1526. ParseModule;
  1527. if SkipTests then exit;
  1528. AssertEquals('Has unit (TPasModule)',TPasModule,Module.ClassType);
  1529. AssertNotNull('Has interface section',Module.InterfaceSection);
  1530. AssertNotNull('Has implementation section',Module.ImplementationSection);
  1531. if (Module.InitializationSection<>nil)
  1532. and (Module.InitializationSection.Elements.Count>0)
  1533. and (TObject(Module.InitializationSection.Elements[0]) is TPasImplBlock) then
  1534. FFirstPasStatement:=TPasImplBlock(Module.InitializationSection.Elements[0]);
  1535. end;
  1536. function TCustomTestModule.FindModuleWithFilename(aFilename: string
  1537. ): TTestEnginePasResolver;
  1538. var
  1539. i: Integer;
  1540. begin
  1541. for i:=0 to ResolverCount-1 do
  1542. if CompareText(Resolvers[i].Filename,aFilename)=0 then
  1543. exit(Resolvers[i]);
  1544. Result:=nil;
  1545. end;
  1546. function TCustomTestModule.AddModule(aFilename: string
  1547. ): TTestEnginePasResolver;
  1548. begin
  1549. //writeln('TTestModuleConverter.AddModule ',aFilename);
  1550. if FindModuleWithFilename(aFilename)<>nil then
  1551. Fail('TTestModuleConverter.AddModule: file "'+aFilename+'" already exists');
  1552. Result:=TTestEnginePasResolver.Create;
  1553. Result.Filename:=aFilename;
  1554. Result.AddObjFPCBuiltInIdentifiers(btAllJSBaseTypes,bfAllJSBaseProcs);
  1555. Result.OnFindUnit:=@OnPasResolverFindUnit;
  1556. Result.OnLog:=@OnPasResolverLog;
  1557. Result.Hub:=Hub;
  1558. FModules.Add(Result);
  1559. end;
  1560. function TCustomTestModule.AddModuleWithSrc(aFilename, Src: string
  1561. ): TTestEnginePasResolver;
  1562. begin
  1563. Result:=AddModule(aFilename);
  1564. Result.Source:=Src;
  1565. end;
  1566. function TCustomTestModule.AddModuleWithIntfImplSrc(aFilename, InterfaceSrc,
  1567. ImplementationSrc: string): TTestEnginePasResolver;
  1568. var
  1569. Src: String;
  1570. begin
  1571. Src:='unit '+ExtractFileUnitName(aFilename)+';'+LineEnding;
  1572. Src+=LineEnding;
  1573. Src+='interface'+LineEnding;
  1574. Src+=LineEnding;
  1575. Src+=InterfaceSrc;
  1576. Src+='implementation'+LineEnding;
  1577. Src+=LineEnding;
  1578. Src+=ImplementationSrc;
  1579. Src+='end.'+LineEnding;
  1580. Result:=AddModuleWithSrc(aFilename,Src);
  1581. end;
  1582. procedure TCustomTestModule.AddSystemUnit(Parts: TSystemUnitParts);
  1583. var
  1584. Intf, Impl: TStringList;
  1585. begin
  1586. Intf:=TStringList.Create;
  1587. if supTInterfacedObject in Parts then Include(Parts,supTObject);
  1588. // unit interface
  1589. if [supTVarRec,supTypeInfo]*Parts<>[] then
  1590. Intf.Add('{$modeswitch externalclass}');
  1591. Intf.Add('type');
  1592. Intf.Add(' integer=longint;');
  1593. Intf.Add(' sizeint=nativeint;');
  1594. //'const',
  1595. //' LineEnding = #10;',
  1596. //' DirectorySeparator = ''/'';',
  1597. //' DriveSeparator = '''';',
  1598. //' AllowDirectorySeparators : set of char = [''\'',''/''];',
  1599. //' AllowDriveSeparators : set of char = [];',
  1600. if supTObject in Parts then
  1601. Intf.AddStrings([
  1602. 'type',
  1603. ' TClass = class of TObject;',
  1604. ' TObject = class',
  1605. ' constructor Create;',
  1606. ' destructor Destroy; virtual;',
  1607. ' class function ClassType: TClass; assembler;',
  1608. ' class function ClassName: String; assembler;',
  1609. ' class function ClassNameIs(const Name: string): boolean;',
  1610. ' class function ClassParent: TClass; assembler;',
  1611. ' class function InheritsFrom(aClass: TClass): boolean; assembler;',
  1612. ' class function UnitName: String; assembler;',
  1613. ' procedure AfterConstruction; virtual;',
  1614. ' procedure BeforeDestruction;virtual;',
  1615. ' function Equals(Obj: TObject): boolean; virtual;',
  1616. ' function ToString: String; virtual;',
  1617. ' end;']);
  1618. if supTInterfacedObject in Parts then
  1619. Intf.AddStrings([
  1620. ' {$Interfaces COM}',
  1621. ' IUnknown = interface',
  1622. ' [''{00000000-0000-0000-C000-000000000046}'']',
  1623. //' function QueryInterface(const iid: TGuid; out obj): Integer;',
  1624. ' function _AddRef: Integer;',
  1625. ' function _Release: Integer;',
  1626. ' end;',
  1627. ' IInterface = IUnknown;',
  1628. ' TInterfacedObject = class(TObject,IUnknown)',
  1629. ' protected',
  1630. ' fRefCount: Integer;',
  1631. ' { implement methods of IUnknown }',
  1632. //' function QueryInterface(const iid: TGuid; out obj): Integer; virtual;',
  1633. ' function _AddRef: Integer; virtual;',
  1634. ' function _Release: Integer; virtual;',
  1635. ' end;',
  1636. ' TInterfacedClass = class of TInterfacedObject;',
  1637. '',
  1638. '']);
  1639. if supTVarRec in Parts then
  1640. Intf.AddStrings([
  1641. 'const',
  1642. ' vtInteger = 0;',
  1643. ' vtBoolean = 1;',
  1644. ' vtJSValue = 19;',
  1645. 'type',
  1646. ' PVarRec = ^TVarRec;',
  1647. ' TVarRec = record',
  1648. ' VType : byte;',
  1649. ' VJSValue: JSValue;',
  1650. ' vInteger: longint external name ''VJSValue'';',
  1651. ' vBoolean: boolean external name ''VJSValue'';',
  1652. ' end;',
  1653. ' TVarRecArray = array of TVarRec;',
  1654. 'function VarRecs: TVarRecArray; varargs;',
  1655. '']);
  1656. if supTypeInfo in Parts then
  1657. begin
  1658. Intf.AddStrings([
  1659. 'type',
  1660. ' TTypeKind = (',
  1661. ' tkUnknown, // 0',
  1662. ' tkInteger, // 1',
  1663. ' tkChar, // 2 in Delphi/FPC tkWChar, tkUChar',
  1664. ' tkString, // 3 in Delphi/FPC tkSString, tkWString or tkUString',
  1665. ' tkEnumeration, // 4',
  1666. ' tkSet, // 5',
  1667. ' tkDouble, // 6',
  1668. ' tkBool, // 7',
  1669. ' tkProcVar, // 8 function or procedure',
  1670. ' tkMethod, // 9 proc var of object',
  1671. ' tkArray, // 10 static array',
  1672. ' tkDynArray, // 11',
  1673. ' tkRecord, // 12',
  1674. ' tkClass, // 13',
  1675. ' tkClassRef, // 14',
  1676. ' tkPointer, // 15',
  1677. ' tkJSValue, // 16',
  1678. ' tkRefToProcVar, // 17 variable of procedure type',
  1679. ' tkInterface, // 18',
  1680. ' //tkObject,',
  1681. ' //tkSString,tkLString,tkAString,tkWString,',
  1682. ' //tkVariant,',
  1683. ' //tkWChar,',
  1684. ' //tkInt64,',
  1685. ' //tkQWord,',
  1686. ' //tkInterfaceRaw,',
  1687. ' //tkUString,tkUChar,',
  1688. ' tkHelper, // 19',
  1689. ' //tkFile,',
  1690. ' tkExtClass // 20',
  1691. ' );',
  1692. ' TTypeKinds = set of TTypeKind;',
  1693. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  1694. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo)',
  1695. ' end;',
  1696. ' TTypeInfoEnum = class external name ''rtl.tTypeInfoEnum''(TTypeInfoInteger) end;',
  1697. ' TTypeInfoSet = class external name ''rtl.tTypeInfoSet''(TTypeInfo) end;',
  1698. ' TTypeInfoStaticArray = class external name ''rtl.tTypeInfoStaticArray''(TTypeInfo) end;',
  1699. ' TTypeInfoDynArray = class external name ''rtl.tTypeInfoDynArray''(TTypeInfo) end;',
  1700. ' TTypeInfoProcVar = class external name ''rtl.tTypeInfoProcVar''(TTypeInfo) end;',
  1701. ' TTypeInfoMethodVar = class external name ''rtl.tTypeInfoMethodVar''(TTypeInfoProcVar) end;',
  1702. ' TTypeInfoClass = class external name ''rtl.tTypeInfoClass''(TTypeInfo) end;',
  1703. ' TTypeInfoClassRef = class external name ''rtl.tTypeInfoClassRef''(TTypeInfo) end;',
  1704. ' TTypeInfoExtClass = class external name ''rtl.tTypeInfoExtClass''(TTypeInfo) end;',
  1705. ' TTypeInfoRecord = class external name ''rtl.tTypeInfoRecord''(TTypeInfo) end;',
  1706. ' TTypeInfoPointer = class external name ''rtl.tTypeInfoPointer''(TTypeInfo) end;',
  1707. ' TTypeInfoHelper = class external name ''rtl.tTypeInfoHelper''(TTypeInfo) end;',
  1708. ' TTypeInfoInterface = class external name ''rtl.tTypeInfoInterface''(TTypeInfo) end;',
  1709. '']);
  1710. end;
  1711. if supWriteln in Parts then
  1712. Intf.Add('procedure writeln; varargs; external name ''console.log'';');
  1713. Intf.Add('var');
  1714. Intf.Add(' ExitCode: Longint = 0;');
  1715. // unit implementation
  1716. Impl:=TStringList.Create;
  1717. if supTObject in Parts then
  1718. Impl.AddStrings([
  1719. '// needed by ClassNameIs, the real SameText is in SysUtils',
  1720. 'function SameText(const s1, s2: String): Boolean; assembler;',
  1721. 'asm',
  1722. 'end;',
  1723. 'constructor TObject.Create; begin end;',
  1724. 'destructor TObject.Destroy; begin end;',
  1725. 'class function TObject.ClassType: TClass; assembler;',
  1726. 'asm',
  1727. 'end;',
  1728. 'class function TObject.ClassName: String; assembler;',
  1729. 'asm',
  1730. 'end;',
  1731. 'class function TObject.ClassNameIs(const Name: string): boolean;',
  1732. 'begin',
  1733. ' Result:=SameText(Name,ClassName);',
  1734. 'end;',
  1735. 'class function TObject.ClassParent: TClass; assembler;',
  1736. 'asm',
  1737. 'end;',
  1738. 'class function TObject.InheritsFrom(aClass: TClass): boolean; assembler;',
  1739. 'asm',
  1740. 'end;',
  1741. 'class function TObject.UnitName: String; assembler;',
  1742. 'asm',
  1743. 'end;',
  1744. 'procedure TObject.AfterConstruction; begin end;',
  1745. 'procedure TObject.BeforeDestruction; begin end;',
  1746. 'function TObject.Equals(Obj: TObject): boolean;',
  1747. 'begin',
  1748. ' Result:=Obj=Self;',
  1749. 'end;',
  1750. 'function TObject.ToString: String;',
  1751. 'begin',
  1752. ' Result:=ClassName;',
  1753. 'end;'
  1754. ]);
  1755. if supTInterfacedObject in Parts then
  1756. Impl.AddStrings([
  1757. //'function TInterfacedObject.QueryInterface(const iid: TGuid; out obj): Integer;',
  1758. //'begin',
  1759. //'end;',
  1760. 'function TInterfacedObject._AddRef: Integer;',
  1761. 'begin',
  1762. 'end;',
  1763. 'function TInterfacedObject._Release: Integer;',
  1764. 'begin',
  1765. 'end;',
  1766. '']);
  1767. if supTVarRec in Parts then
  1768. Impl.AddStrings([
  1769. 'function VarRecs: TVarRecArray; varargs;',
  1770. 'var',
  1771. ' v: PVarRec;',
  1772. 'begin',
  1773. ' v^.VType:=1;',
  1774. ' v^.VJSValue:=2;',
  1775. 'end;',
  1776. '']);
  1777. try
  1778. AddModuleWithIntfImplSrc('system.pp',Intf.Text,Impl.Text);
  1779. finally
  1780. Intf.Free;
  1781. Impl.Free;
  1782. end;
  1783. end;
  1784. procedure TCustomTestModule.StartProgram(NeedSystemUnit: boolean;
  1785. SystemUnitParts: TSystemUnitParts);
  1786. begin
  1787. if NeedSystemUnit then
  1788. AddSystemUnit(SystemUnitParts)
  1789. else
  1790. Parser.ImplicitUses.Clear;
  1791. Add('program '+ExtractFileUnitName(Filename)+';');
  1792. Add('');
  1793. end;
  1794. procedure TCustomTestModule.StartLibrary(NeedSystemUnit: boolean;
  1795. SystemUnitParts: TSystemUnitParts);
  1796. begin
  1797. if NeedSystemUnit then
  1798. AddSystemUnit(SystemUnitParts)
  1799. else
  1800. Parser.ImplicitUses.Clear;
  1801. Add('library '+ExtractFileUnitName(Filename)+';');
  1802. Add('');
  1803. end;
  1804. procedure TCustomTestModule.StartUnit(NeedSystemUnit: boolean;
  1805. SystemUnitParts: TSystemUnitParts);
  1806. begin
  1807. if NeedSystemUnit then
  1808. AddSystemUnit(SystemUnitParts)
  1809. else
  1810. Parser.ImplicitUses.Clear;
  1811. Add('unit Test1;');
  1812. Add('');
  1813. end;
  1814. procedure TCustomTestModule.ConvertModule;
  1815. procedure CheckUsesList(UsesName: String; Arg: TJSArrayLiteralElement;
  1816. out UsesLit: TJSArrayLiteral);
  1817. var
  1818. i: Integer;
  1819. Item: TJSElement;
  1820. Lit: TJSLiteral;
  1821. begin
  1822. UsesLit:=nil;
  1823. AssertNotNull(UsesName+' uses section',Arg.Expr);
  1824. if (Arg.Expr.ClassType=TJSLiteral) and TJSLiteral(Arg.Expr).Value.IsNull then
  1825. exit; // null is ok
  1826. AssertEquals(UsesName+' uses section param is array',TJSArrayLiteral,Arg.Expr.ClassType);
  1827. FJSInterfaceUses:=TJSArrayLiteral(Arg.Expr);
  1828. for i:=0 to FJSInterfaceUses.Elements.Count-1 do
  1829. begin
  1830. Item:=FJSInterfaceUses.Elements.Elements[i].Expr;
  1831. AssertNotNull(UsesName+' uses section item['+IntToStr(i)+'].Expr',Item);
  1832. AssertEquals(UsesName+' uses section item['+IntToStr(i)+'] is lit',TJSLiteral,Item.ClassType);
  1833. Lit:=TJSLiteral(Item);
  1834. AssertEquals(UsesName+' uses section item['+IntToStr(i)+'] is string lit',
  1835. ord(jsbase.jstString),ord(Lit.Value.ValueType));
  1836. end;
  1837. end;
  1838. procedure CheckFunctionParam(ParamName: string; Arg: TJSArrayLiteralElement;
  1839. out Src: TJSSourceElements);
  1840. var
  1841. FunDecl: TJSFunctionDeclarationStatement;
  1842. FunDef: TJSFuncDef;
  1843. FunBody: TJSFunctionBody;
  1844. begin
  1845. Src:=nil;
  1846. AssertNotNull(ParamName,Arg.Expr);
  1847. AssertEquals(ParamName+' Arg.Expr type',TJSFunctionDeclarationStatement,Arg.Expr.ClassType);
  1848. FunDecl:=Arg.Expr as TJSFunctionDeclarationStatement;
  1849. AssertNotNull(ParamName+' FunDecl.AFunction',FunDecl.AFunction);
  1850. AssertEquals(ParamName+' FunDecl.AFunction type',TJSFuncDef,FunDecl.AFunction.ClassType);
  1851. FunDef:=FunDecl.AFunction as TJSFuncDef;
  1852. AssertEquals(ParamName+' name empty','',String(FunDef.Name));
  1853. AssertNotNull(ParamName+' body',FunDef.Body);
  1854. AssertEquals(ParamName+' body type',TJSFunctionBody,FunDef.Body.ClassType);
  1855. FunBody:=FunDef.Body as TJSFunctionBody;
  1856. AssertNotNull(ParamName+' body.A',FunBody.A);
  1857. AssertEquals(ParamName+' body.A type',TJSSourceElements,FunBody.A.ClassType);
  1858. Src:=FunBody.A as TJSSourceElements;
  1859. end;
  1860. var
  1861. ModuleNameExpr: TJSLiteral;
  1862. InitFunction: TJSFunctionDeclarationStatement;
  1863. InitAssign: TJSSimpleAssignStatement;
  1864. InitName: String;
  1865. LastNode: TJSElement;
  1866. Arg: TJSArrayLiteralElement;
  1867. IsProg, IsLib: Boolean;
  1868. begin
  1869. if SkipTests then exit;
  1870. IsProg:=false;
  1871. IsLib:=false;
  1872. if Module is TPasProgram then
  1873. IsProg:=true
  1874. else if Module is TPasLibrary then
  1875. IsLib:=true;
  1876. try
  1877. FJSModule:=FConverter.ConvertPasElement(Module,Engine) as TJSSourceElements;
  1878. except
  1879. on E: Exception do
  1880. HandleException(E);
  1881. end;
  1882. if SkipTests then exit;
  1883. if ExpectedErrorClass<>nil then
  1884. Fail('Missing '+ExpectedErrorClass.ClassName+' error {'+ExpectedErrorMsg+'} ('+IntToStr(ExpectedErrorNumber)+')');
  1885. FJSSource:=TStringList.Create;
  1886. FJSSource.Text:=ConvertJSModuleToString(JSModule);
  1887. {$IFDEF VerbosePas2JS}
  1888. writeln('TTestModule.ConvertModule JS:');
  1889. write(FJSSource.Text);
  1890. {$ENDIF}
  1891. // rtl.module(...
  1892. AssertEquals('jsmodule has one statement - the call',1,JSModule.Statements.Count);
  1893. AssertNotNull('register module call',JSModule.Statements.Nodes[0].Node);
  1894. AssertEquals('register module call',TJSCallExpression,JSModule.Statements.Nodes[0].Node.ClassType);
  1895. FJSRegModuleCall:=JSModule.Statements.Nodes[0].Node as TJSCallExpression;
  1896. AssertNotNull('register module rtl.module expr',JSRegModuleCall.Expr);
  1897. AssertNotNull('register module rtl.module args',JSRegModuleCall.Args);
  1898. AssertEquals('rtl.module args',TJSArguments,JSRegModuleCall.Args.ClassType);
  1899. FJSModuleCallArgs:=JSRegModuleCall.Args as TJSArguments;
  1900. // parameter 'unitname'
  1901. if JSModuleCallArgs.Elements.Count<1 then
  1902. Fail('rtl.module first param unit missing');
  1903. Arg:=JSModuleCallArgs.Elements.Elements[0];
  1904. AssertNotNull('module name param',Arg.Expr);
  1905. ModuleNameExpr:=Arg.Expr as TJSLiteral;
  1906. AssertEquals('module name param is string',ord(jstString),ord(ModuleNameExpr.Value.ValueType));
  1907. if IsProg then
  1908. AssertEquals('module name','program',String(ModuleNameExpr.Value.AsString))
  1909. else if IsLib then
  1910. AssertEquals('module name','library',String(ModuleNameExpr.Value.AsString))
  1911. else
  1912. AssertEquals('module name',Module.Name,String(ModuleNameExpr.Value.AsString));
  1913. // main uses section
  1914. if JSModuleCallArgs.Elements.Count<2 then
  1915. Fail('rtl.module second param main uses missing');
  1916. Arg:=JSModuleCallArgs.Elements.Elements[1];
  1917. CheckUsesList('interface',Arg,FJSInterfaceUses);
  1918. // program/library/interface function()
  1919. if JSModuleCallArgs.Elements.Count<3 then
  1920. Fail('rtl.module third param intf-function missing');
  1921. Arg:=JSModuleCallArgs.Elements.Elements[2];
  1922. CheckFunctionParam('module intf-function',Arg,FJSModuleSrc);
  1923. // search for $mod.$init or $mod.$main - the last statement
  1924. if IsProg or IsLib then
  1925. begin
  1926. InitName:='$main';
  1927. AssertEquals('$mod.'+InitName+' function 1',true,JSModuleSrc.Statements.Count>0);
  1928. end
  1929. else
  1930. InitName:='$init';
  1931. InitAssign:=nil;
  1932. InitFunction:=nil;
  1933. FJSInitBody:=nil;
  1934. if JSModuleSrc.Statements.Count>0 then
  1935. begin
  1936. LastNode:=JSModuleSrc.Statements.Nodes[JSModuleSrc.Statements.Count-1].Node;
  1937. if LastNode is TJSSimpleAssignStatement then
  1938. begin
  1939. InitAssign:=LastNode as TJSSimpleAssignStatement;
  1940. if GetDottedIdentifier(InitAssign.LHS)='$mod.'+InitName then
  1941. begin
  1942. InitFunction:=InitAssign.Expr as TJSFunctionDeclarationStatement;
  1943. FJSInitBody:=InitFunction.AFunction.Body as TJSFunctionBody;
  1944. end
  1945. else if IsProg or IsLib then
  1946. CheckDottedIdentifier('init function',InitAssign.LHS,'$mod.'+InitName);
  1947. end;
  1948. end;
  1949. // optional: implementation uses section
  1950. if JSModuleCallArgs.Elements.Count<4 then
  1951. exit;
  1952. Arg:=JSModuleCallArgs.Elements.Elements[3];
  1953. CheckUsesList('implementation',Arg,FJSImplentationUses);
  1954. end;
  1955. procedure TCustomTestModule.ConvertProgram;
  1956. begin
  1957. Add('end.');
  1958. ParseProgram;
  1959. ConvertModule;
  1960. end;
  1961. procedure TCustomTestModule.ConvertLibrary;
  1962. begin
  1963. Add('end.');
  1964. ParseLibrary;
  1965. ConvertModule;
  1966. end;
  1967. procedure TCustomTestModule.ConvertUnit;
  1968. begin
  1969. Add('end.');
  1970. ParseUnit;
  1971. ConvertModule;
  1972. end;
  1973. function TCustomTestModule.ConvertJSModuleToString(El: TJSElement): string;
  1974. begin
  1975. Result:=tcmodules.JSToStr(El);
  1976. end;
  1977. procedure TCustomTestModule.CheckDottedIdentifier(Msg: string; El: TJSElement;
  1978. DottedName: string);
  1979. begin
  1980. if DottedName='' then
  1981. begin
  1982. AssertNull(Msg,El);
  1983. end
  1984. else
  1985. begin
  1986. AssertNotNull(Msg,El);
  1987. AssertEquals(Msg,DottedName,GetDottedIdentifier(El));
  1988. end;
  1989. end;
  1990. function TCustomTestModule.GetDottedIdentifier(El: TJSElement): string;
  1991. begin
  1992. if El=nil then
  1993. Result:=''
  1994. else if El is TJSPrimaryExpressionIdent then
  1995. Result:=String(TJSPrimaryExpressionIdent(El).Name)
  1996. else if El is TJSDotMemberExpression then
  1997. Result:=GetDottedIdentifier(TJSDotMemberExpression(El).MExpr)+'.'+String(TJSDotMemberExpression(El).Name)
  1998. else
  1999. AssertEquals('GetDottedIdentifier',TJSPrimaryExpressionIdent,El.ClassType);
  2000. end;
  2001. procedure TCustomTestModule.CheckSource(Msg, Statements: String;
  2002. InitStatements: string; ImplStatements: string);
  2003. var
  2004. ActualSrc, ExpectedSrc, InitName: String;
  2005. IsProg, IsLib: Boolean;
  2006. begin
  2007. ActualSrc:=JSToStr(JSModuleSrc);
  2008. if coUseStrict in Converter.Options then
  2009. ExpectedSrc:='"use strict";'+LineEnding
  2010. else
  2011. ExpectedSrc:='';
  2012. ExpectedSrc:=ExpectedSrc+'var $mod = this;'+LineEnding;
  2013. ExpectedSrc:=ExpectedSrc+Statements;
  2014. // unit implementation
  2015. if (Trim(ImplStatements)<>'') then
  2016. ExpectedSrc:=ExpectedSrc+LineEnding
  2017. +'$mod.$implcode = function () {'+LineEnding
  2018. +ImplStatements
  2019. +'};'+LineEnding;
  2020. // program main or unit initialization
  2021. IsProg:=false;
  2022. IsLib:=false;
  2023. if Module is TPasProgram then
  2024. IsProg:=true
  2025. else if Module is TPasLibrary then
  2026. IsLib:=true;
  2027. if IsProg or IsLib or (Trim(InitStatements)<>'') then
  2028. begin
  2029. if IsProg or IsLib then
  2030. InitName:='$main'
  2031. else
  2032. InitName:='$init';
  2033. ExpectedSrc:=ExpectedSrc+LineEnding
  2034. +'$mod.'+InitName+' = function () {'+LineEnding
  2035. +InitStatements
  2036. +'};'+LineEnding;
  2037. end;
  2038. //writeln('TCustomTestModule.CheckSource ExpectedIntf="',ExpectedSrc,'"');
  2039. //writeln('TTestModule.CheckSource InitStatements="',Trim(InitStatements),'"');
  2040. //writeln('TCustomTestModule.CheckSource ',ActualSrc);
  2041. CheckDiff(Msg,ExpectedSrc,ActualSrc);
  2042. end;
  2043. procedure TCustomTestModule.CheckDiff(Msg, Expected, Actual: string);
  2044. // search diff, ignore changes in spaces
  2045. var
  2046. s: string;
  2047. begin
  2048. if CheckSrcDiff(Expected,Actual,s) then exit;
  2049. Fail(Msg+': '+s);
  2050. end;
  2051. procedure TCustomTestModule.CheckUnit(Filename, ExpectedSrc: string);
  2052. var
  2053. aResolver: TTestEnginePasResolver;
  2054. aConverter: TPasToJSConverter;
  2055. aJSModule: TJSSourceElements;
  2056. ActualSrc: String;
  2057. begin
  2058. aResolver:=GetResolver(Filename);
  2059. AssertNotNull('missing resolver of unit '+Filename,aResolver);
  2060. AssertNotNull('missing resolver.module of unit '+Filename,aResolver.Module);
  2061. {$IFDEF VerbosePas2JS}
  2062. writeln('CheckUnit '+Filename+' converting ...');
  2063. {$ENDIF}
  2064. aConverter:=CreateConverter;
  2065. aJSModule:=nil;
  2066. try
  2067. try
  2068. aJSModule:=aConverter.ConvertPasElement(aResolver.Module,aResolver) as TJSSourceElements;
  2069. except
  2070. on E: Exception do
  2071. HandleException(E);
  2072. end;
  2073. ActualSrc:=ConvertJSModuleToString(aJSModule);
  2074. {$IFDEF VerbosePas2JS}
  2075. writeln('TTestModule.CheckUnit ',Filename,' Pas:');
  2076. write(aResolver.Source);
  2077. writeln('TTestModule.CheckUnit ',Filename,' JS:');
  2078. write(ActualSrc);
  2079. {$ENDIF}
  2080. CheckDiff('Converted unit: "'+ChangeFileExt(Filename,'.js')+'"',ExpectedSrc,ActualSrc);
  2081. finally
  2082. aJSModule.Free;
  2083. aConverter.Free;
  2084. end;
  2085. end;
  2086. procedure TCustomTestModule.CheckHint(MsgType: TMessageType;
  2087. MsgNumber: integer; Msg: string; Marker: PSrcMarker);
  2088. var
  2089. i: Integer;
  2090. Item: TTestHintMessage;
  2091. Expected,Actual: string;
  2092. begin
  2093. //writeln('TCustomTestModule.CheckHint MsgCount=',MsgCount);
  2094. for i:=0 to MsgCount-1 do
  2095. begin
  2096. Item:=Msgs[i];
  2097. if (Item.MsgNumber<>MsgNumber) or (Item.Msg<>Msg) then continue;
  2098. if (Marker<>nil) then
  2099. begin
  2100. if Item.SourcePos.Row<>cardinal(Marker^.Row) then continue;
  2101. if (Item.SourcePos.Column<cardinal(Marker^.StartCol))
  2102. or (Item.SourcePos.Column>cardinal(Marker^.EndCol)) then continue;
  2103. end;
  2104. // found
  2105. FHintMsgsGood.Add(Item);
  2106. str(Item.MsgType,Actual);
  2107. str(MsgType,Expected);
  2108. AssertEquals('MsgType',Expected,Actual);
  2109. exit;
  2110. end;
  2111. // needed message missing -> show emitted messages
  2112. WriteSources('',0,0);
  2113. for i:=0 to MsgCount-1 do
  2114. begin
  2115. Item:=Msgs[i];
  2116. write('TCustomTestModule.CheckHint ',i,'/',MsgCount,' ',Item.MsgType,
  2117. ' ('+IntToStr(Item.MsgNumber),')');
  2118. if Marker<>nil then
  2119. write(' '+ExtractFileName(Item.SourcePos.FileName),'(',Item.SourcePos.Row,',',Item.SourcePos.Column,')');
  2120. writeln(' {',Item.Msg,'}');
  2121. end;
  2122. str(MsgType,Expected);
  2123. Actual:='Missing '+Expected+' ('+IntToStr(MsgNumber)+')';
  2124. if Marker<>nil then
  2125. Actual:=Actual+' '+ExtractFileName(Marker^.Filename)+'('+IntToStr(Marker^.Row)+','+IntToStr(Marker^.StartCol)+'..'+IntToStr(Marker^.EndCol)+')';
  2126. Actual:=Actual+' '+Msg;
  2127. Fail(Actual);
  2128. end;
  2129. procedure TCustomTestModule.CheckResolverUnexpectedHints(WithSourcePos: boolean
  2130. );
  2131. var
  2132. i: Integer;
  2133. s, Txt: String;
  2134. Msg: TTestHintMessage;
  2135. begin
  2136. for i:=0 to MsgCount-1 do
  2137. begin
  2138. Msg:=Msgs[i];
  2139. if FHintMsgsGood.IndexOf(Msg)>=0 then continue;
  2140. s:='';
  2141. str(Msg.MsgType,s);
  2142. Txt:='Unexpected resolver message found ['+IntToStr(Msg.Id)+'] '
  2143. +s+': ('+IntToStr(Msg.MsgNumber)+')';
  2144. if WithSourcePos then
  2145. Txt:=Txt+' '+ExtractFileName(Msg.SourcePos.FileName)+'('+IntToStr(Msg.SourcePos.Row)+','+IntToStr(Msg.SourcePos.Column)+')';
  2146. Txt:=Txt+' {'+Msg.Msg+'}';
  2147. Fail(Txt);
  2148. end;
  2149. end;
  2150. procedure TCustomTestModule.SetExpectedScannerError(Msg: string;
  2151. MsgNumber: integer);
  2152. begin
  2153. ExpectedErrorClass:=EScannerError;
  2154. ExpectedErrorMsg:=Msg;
  2155. ExpectedErrorNumber:=MsgNumber;
  2156. end;
  2157. procedure TCustomTestModule.SetExpectedParserError(Msg: string;
  2158. MsgNumber: integer);
  2159. begin
  2160. ExpectedErrorClass:=EParserError;
  2161. ExpectedErrorMsg:=Msg;
  2162. ExpectedErrorNumber:=MsgNumber;
  2163. end;
  2164. procedure TCustomTestModule.SetExpectedPasResolverError(Msg: string;
  2165. MsgNumber: integer);
  2166. begin
  2167. ExpectedErrorClass:=EPasResolve;
  2168. ExpectedErrorMsg:=Msg;
  2169. ExpectedErrorNumber:=MsgNumber;
  2170. end;
  2171. procedure TCustomTestModule.SetExpectedConverterError(Msg: string;
  2172. MsgNumber: integer);
  2173. begin
  2174. ExpectedErrorClass:=EPas2JS;
  2175. ExpectedErrorMsg:=Msg;
  2176. ExpectedErrorNumber:=MsgNumber;
  2177. end;
  2178. function TCustomTestModule.IsErrorExpected(E: Exception): boolean;
  2179. var
  2180. MsgNumber: Integer;
  2181. Msg: String;
  2182. begin
  2183. Result:=false;
  2184. if (ExpectedErrorClass=nil) or (ExpectedErrorClass<>E.ClassType) then exit;
  2185. Msg:=E.Message;
  2186. if E is EPas2JS then
  2187. MsgNumber:=EPas2JS(E).MsgNumber
  2188. else if E is EPasResolve then
  2189. MsgNumber:=EPasResolve(E).MsgNumber
  2190. else if E is EParserError then
  2191. MsgNumber:=Parser.LastMsgNumber
  2192. else if E is EScannerError then
  2193. begin
  2194. MsgNumber:=Scanner.LastMsgNumber;
  2195. Msg:=Scanner.LastMsg;
  2196. end
  2197. else
  2198. MsgNumber:=0;
  2199. Result:=(MsgNumber=ExpectedErrorNumber) and (Msg=ExpectedErrorMsg);
  2200. if Result then
  2201. SkipTests:=true;
  2202. end;
  2203. procedure TCustomTestModule.HandleScannerError(E: EScannerError);
  2204. begin
  2205. if IsErrorExpected(E) then exit;
  2206. WriteSources(Scanner.CurFilename,Scanner.CurRow,Scanner.CurColumn);
  2207. writeln('ERROR: TCustomTestModule.HandleScannerError '+E.ClassName+':'+E.Message
  2208. +' '+Scanner.CurFilename
  2209. +'('+IntToStr(Scanner.CurRow)+','+IntToStr(Scanner.CurColumn)+')');
  2210. FailException(E);
  2211. end;
  2212. procedure TCustomTestModule.HandleParserError(E: EParserError);
  2213. begin
  2214. if IsErrorExpected(E) then exit;
  2215. WriteSources(E.Filename,E.Row,E.Column);
  2216. writeln('ERROR: TCustomTestModule.HandleParserError '+E.ClassName+':'+E.Message
  2217. +' '+E.Filename+'('+IntToStr(E.Row)+','+IntToStr(E.Column)+')'
  2218. +' MainModuleScannerLine="'+Scanner.CurLine+'"'
  2219. );
  2220. FailException(E);
  2221. end;
  2222. procedure TCustomTestModule.HandlePasResolveError(E: EPasResolve);
  2223. var
  2224. P: TPasSourcePos;
  2225. begin
  2226. if IsErrorExpected(E) then exit;
  2227. P:=E.SourcePos;
  2228. WriteSources(P.FileName,P.Row,P.Column);
  2229. writeln('ERROR: TCustomTestModule.HandlePasResolveError '+E.ClassName+':'+E.Message
  2230. +' '+P.FileName+'('+IntToStr(P.Row)+','+IntToStr(P.Column)+')');
  2231. FailException(E);
  2232. end;
  2233. procedure TCustomTestModule.HandlePas2JSError(E: EPas2JS);
  2234. var
  2235. Row, Col: integer;
  2236. begin
  2237. if IsErrorExpected(E) then exit;
  2238. Engine.UnmangleSourceLineNumber(E.PasElement.SourceLinenumber,Row,Col);
  2239. WriteSources(E.PasElement.SourceFilename,Row,Col);
  2240. writeln('ERROR: TCustomTestModule.HandlePas2JSError '+E.ClassName+':'+E.Message
  2241. +' '+E.PasElement.SourceFilename
  2242. +'('+IntToStr(Row)+','+IntToStr(Col)+')');
  2243. FailException(E);
  2244. end;
  2245. procedure TCustomTestModule.HandleException(E: Exception);
  2246. begin
  2247. if E is EScannerError then
  2248. HandleScannerError(EScannerError(E))
  2249. else if E is EParserError then
  2250. HandleParserError(EParserError(E))
  2251. else if E is EPasResolve then
  2252. HandlePasResolveError(EPasResolve(E))
  2253. else if E is EPas2JS then
  2254. HandlePas2JSError(EPas2JS(E))
  2255. else
  2256. begin
  2257. if IsErrorExpected(E) then exit;
  2258. if not (E is EAssertionFailedError) then
  2259. begin
  2260. WriteSources('',0,0);
  2261. writeln('ERROR: TCustomTestModule.HandleException '+E.ClassName+':'+E.Message);
  2262. end;
  2263. FailException(E);
  2264. end;
  2265. end;
  2266. procedure TCustomTestModule.FailException(E: Exception);
  2267. var
  2268. MsgNumber: Integer;
  2269. begin
  2270. if ExpectedErrorClass<>nil then
  2271. begin
  2272. if FExpectedErrorClass=E.ClassType then
  2273. begin
  2274. if E is EPas2JS then
  2275. MsgNumber:=EPas2JS(E).MsgNumber
  2276. else if E is EPasResolve then
  2277. MsgNumber:=EPasResolve(E).MsgNumber
  2278. else if E is EParserError then
  2279. MsgNumber:=Parser.LastMsgNumber
  2280. else if E is EScannerError then
  2281. MsgNumber:=Scanner.LastMsgNumber
  2282. else
  2283. MsgNumber:=0;
  2284. AssertEquals('Expected error message ('+IntToStr(ExpectedErrorNumber)+')','{'+ExpectedErrorMsg+'}','{'+E.Message+'}');
  2285. AssertEquals('Expected {'+ExpectedErrorMsg+'}, but got msg {'+E.Message+'} number',
  2286. ExpectedErrorNumber,MsgNumber);
  2287. end else begin
  2288. AssertEquals('Wrong exception class',ExpectedErrorClass.ClassName,E.ClassName);
  2289. end;
  2290. end;
  2291. Fail(E.Message);
  2292. end;
  2293. procedure TCustomTestModule.WriteSources(const aFilename: string; aRow,
  2294. aCol: integer);
  2295. var
  2296. IsSrc: Boolean;
  2297. i, j: Integer;
  2298. SrcLines: TStringList;
  2299. Line: string;
  2300. aModule: TTestEnginePasResolver;
  2301. begin
  2302. writeln('TCustomTestModule.WriteSources File="',aFilename,'" Row=',aRow,' Col=',aCol);
  2303. for i:=0 to ResolverCount-1 do
  2304. begin
  2305. aModule:=Resolvers[i];
  2306. SrcLines:=TStringList.Create;
  2307. try
  2308. SrcLines.Text:=aModule.Source;
  2309. IsSrc:=ExtractFilename(aModule.Filename)=ExtractFileName(aFilename);
  2310. writeln('Testcode:-File="',aModule.Filename,'"----------------------------------:');
  2311. for j:=1 to SrcLines.Count do
  2312. begin
  2313. Line:=SrcLines[j-1];
  2314. if IsSrc and (j=aRow) then
  2315. begin
  2316. write('*');
  2317. Line:=LeftStr(Line,aCol-1)+'|'+copy(Line,aCol,length(Line));
  2318. end;
  2319. writeln(Format('%:4d: ',[j]),Line);
  2320. end;
  2321. finally
  2322. SrcLines.Free;
  2323. end;
  2324. end;
  2325. end;
  2326. function TCustomTestModule.IndexOfResolver(const Filename: string): integer;
  2327. var
  2328. i: Integer;
  2329. begin
  2330. for i:=0 to ResolverCount-1 do
  2331. if Filename=Resolvers[i].Filename then exit(i);
  2332. Result:=-1;
  2333. end;
  2334. function TCustomTestModule.GetResolver(const Filename: string
  2335. ): TTestEnginePasResolver;
  2336. var
  2337. i: Integer;
  2338. begin
  2339. i:=IndexOfResolver(Filename);
  2340. if i<0 then exit(nil);
  2341. Result:=Resolvers[i];
  2342. end;
  2343. function TCustomTestModule.GetDefaultNamespace: string;
  2344. var
  2345. C: TClass;
  2346. begin
  2347. Result:='';
  2348. if FModule=nil then exit;
  2349. C:=FModule.ClassType;
  2350. if (C=TPasProgram) or (C=TPasLibrary) or (C=TPasPackage) then
  2351. Result:=Engine.DefaultNameSpace;
  2352. end;
  2353. constructor TCustomTestModule.Create;
  2354. begin
  2355. inherited Create;
  2356. FHintMsgs:=TObjectList.Create(true);
  2357. FHintMsgsGood:=TFPList.Create;
  2358. end;
  2359. destructor TCustomTestModule.Destroy;
  2360. begin
  2361. FreeAndNil(FHintMsgs);
  2362. FreeAndNil(FHintMsgsGood);
  2363. inherited Destroy;
  2364. end;
  2365. { TTestModule }
  2366. procedure TTestModule.TestReservedWords;
  2367. var
  2368. i: integer;
  2369. begin
  2370. for i:=low(JSReservedWords) to High(JSReservedWords)-1 do
  2371. if CompareStr(JSReservedWords[i],JSReservedWords[i+1])>=0 then
  2372. Fail('20170203135442 '+JSReservedWords[i]+' >= '+JSReservedWords[i+1]);
  2373. for i:=low(JSReservedGlobalWords) to High(JSReservedGlobalWords)-1 do
  2374. if CompareStr(JSReservedGlobalWords[i],JSReservedGlobalWords[i+1])>=0 then
  2375. Fail('20170203135443 '+JSReservedGlobalWords[i]+' >= '+JSReservedGlobalWords[i+1]);
  2376. end;
  2377. procedure TTestModule.TestEmptyProgram;
  2378. begin
  2379. StartProgram(false);
  2380. Add('begin');
  2381. ConvertProgram;
  2382. CheckSource('TestEmptyProgram','','');
  2383. end;
  2384. procedure TTestModule.TestEmptyProgramUseStrict;
  2385. begin
  2386. Converter.Options:=Converter.Options+[coUseStrict];
  2387. StartProgram(false);
  2388. Add('begin');
  2389. ConvertProgram;
  2390. CheckSource('TestEmptyProgramUseStrict','','');
  2391. end;
  2392. procedure TTestModule.TestEmptyUnit;
  2393. begin
  2394. StartUnit(false);
  2395. Add('interface');
  2396. Add('implementation');
  2397. ConvertUnit;
  2398. CheckSource('TestEmptyUnit',
  2399. LinesToStr([
  2400. ]),
  2401. '');
  2402. end;
  2403. procedure TTestModule.TestEmptyUnitUseStrict;
  2404. begin
  2405. Converter.Options:=Converter.Options+[coUseStrict];
  2406. StartUnit(false);
  2407. Add('interface');
  2408. Add('implementation');
  2409. ConvertUnit;
  2410. CheckSource('TestEmptyUnitUseStrict',
  2411. LinesToStr([
  2412. ''
  2413. ]),
  2414. '');
  2415. end;
  2416. procedure TTestModule.TestDottedUnitNames;
  2417. begin
  2418. AddModuleWithIntfImplSrc('NS1.Unit2.pas',
  2419. LinesToStr([
  2420. 'var iV: longint;'
  2421. ]),
  2422. '');
  2423. FFilename:='ns1.test1.pp';
  2424. StartProgram(true);
  2425. Add('uses unIt2;');
  2426. Add('var');
  2427. Add(' i: longint;');
  2428. Add('begin');
  2429. Add(' i:=iv;');
  2430. Add(' i:=uNit2.iv;');
  2431. Add(' i:=Ns1.TEst1.i;');
  2432. ConvertProgram;
  2433. CheckSource('TestDottedUnitNames',
  2434. LinesToStr([
  2435. 'this.i = 0;',
  2436. '']),
  2437. LinesToStr([ // this.$init
  2438. '$mod.i = pas["NS1.Unit2"].iV;',
  2439. '$mod.i = pas["NS1.Unit2"].iV;',
  2440. '$mod.i = $mod.i;',
  2441. '']) );
  2442. end;
  2443. procedure TTestModule.TestDottedUnitNameImpl;
  2444. begin
  2445. AddModuleWithIntfImplSrc('TEST.UnitA.pas',
  2446. LinesToStr([
  2447. 'type',
  2448. ' TObject = class end;',
  2449. ' TTestA = class',
  2450. ' end;'
  2451. ]),
  2452. LinesToStr(['uses TEST.UnitB;'])
  2453. );
  2454. AddModuleWithIntfImplSrc('TEST.UnitB.pas',
  2455. LinesToStr([
  2456. 'uses TEST.UnitA;',
  2457. 'type TTestB = class(TTestA);'
  2458. ]),
  2459. ''
  2460. );
  2461. StartProgram(true);
  2462. Add('uses TEST.UnitA;');
  2463. Add('begin');
  2464. ConvertProgram;
  2465. CheckSource('TestDottedUnitNameImpl',
  2466. LinesToStr([
  2467. '']),
  2468. LinesToStr([ // this.$init
  2469. '']) );
  2470. CheckUnit('TEST.UnitA.pas',
  2471. LinesToStr([
  2472. 'rtl.module("TEST.UnitA", ["system"], function () {',
  2473. ' var $mod = this;',
  2474. ' rtl.createClass(this, "TObject", null, function () {',
  2475. ' this.$init = function () {',
  2476. ' };',
  2477. ' this.$final = function () {',
  2478. ' };',
  2479. ' });',
  2480. ' rtl.createClass(this, "TTestA", this.TObject, function () {',
  2481. ' });',
  2482. '}, ["TEST.UnitB"]);'
  2483. ]));
  2484. CheckUnit('TEST.UnitB.pas',
  2485. LinesToStr([
  2486. 'rtl.module("TEST.UnitB", ["system","TEST.UnitA"], function () {',
  2487. ' var $mod = this;',
  2488. ' rtl.createClass(this, "TTestB", pas["TEST.UnitA"].TTestA, function () {',
  2489. ' });',
  2490. '});'
  2491. ]));
  2492. end;
  2493. procedure TTestModule.TestDottedUnitExpr;
  2494. begin
  2495. AddModuleWithIntfImplSrc('NS2.SubNs2.Unit2.pas',
  2496. LinesToStr([
  2497. 'procedure DoIt;'
  2498. ]),
  2499. 'procedure DoIt; begin end;');
  2500. FFilename:='Ns1.SubNs1.Test1.pp';
  2501. StartProgram(true);
  2502. Add('uses Ns2.sUbnS2.unIt2;');
  2503. Add('var');
  2504. Add(' i: longint;');
  2505. Add('begin');
  2506. Add(' ns2.subns2.unit2.doit;');
  2507. Add(' i:=Ns1.SubNS1.TEst1.i;');
  2508. ConvertProgram;
  2509. CheckSource('TestDottedUnitExpr',
  2510. LinesToStr([
  2511. 'this.i = 0;',
  2512. '']),
  2513. LinesToStr([ // this.$init
  2514. 'pas["NS2.SubNs2.Unit2"].DoIt();',
  2515. '$mod.i = $mod.i;',
  2516. '']) );
  2517. end;
  2518. procedure TTestModule.Test_ModeFPCFail;
  2519. begin
  2520. StartProgram(false);
  2521. Add('{$mode FPC}');
  2522. Add('begin');
  2523. SetExpectedScannerError('Invalid mode: "FPC"',nErrInvalidMode);
  2524. ConvertProgram;
  2525. end;
  2526. procedure TTestModule.Test_ModeSwitchCBlocksFail;
  2527. begin
  2528. StartProgram(false);
  2529. Add('{$modeswitch cblocks-}');
  2530. Add('begin');
  2531. ConvertProgram;
  2532. CheckHint(mtWarning,nErrInvalidModeSwitch,'Warning: test1.pp(3,23) : Invalid mode switch: "cblocks"');
  2533. CheckResolverUnexpectedHints();
  2534. end;
  2535. procedure TTestModule.TestUnit_UseSystem;
  2536. begin
  2537. StartUnit(true);
  2538. Add([
  2539. 'interface',
  2540. 'var i: integer;',
  2541. 'implementation']);
  2542. ConvertUnit;
  2543. CheckSource('TestUnit_UseSystem',
  2544. LinesToStr([
  2545. 'this.i = 0;',
  2546. '']),
  2547. LinesToStr([
  2548. '']) );
  2549. end;
  2550. procedure TTestModule.TestUnit_Intf1Impl2Intf1;
  2551. begin
  2552. AddModuleWithIntfImplSrc('unit1.pp',
  2553. LinesToStr([
  2554. 'type number = longint;']),
  2555. LinesToStr([
  2556. 'uses test1;',
  2557. 'procedure DoIt;',
  2558. 'begin',
  2559. ' i:=3;',
  2560. 'end;']));
  2561. StartUnit(true);
  2562. Add([
  2563. 'interface',
  2564. 'uses unit1;',
  2565. 'var i: number;',
  2566. 'implementation']);
  2567. ConvertUnit;
  2568. CheckSource('TestUnit_Intf1Impl2Intf1',
  2569. LinesToStr([
  2570. 'this.i = 0;',
  2571. '']),
  2572. LinesToStr([
  2573. '']) );
  2574. end;
  2575. procedure TTestModule.TestIncludeVersion;
  2576. begin
  2577. StartProgram(false);
  2578. Add([
  2579. 'var',
  2580. ' s: string;',
  2581. ' i: word;',
  2582. 'begin',
  2583. ' s:={$I %line%};',
  2584. ' i:={$I %linenum%};',
  2585. ' s:={$I %currentroutine%};',
  2586. ' s:={$I %pas2jsversion%};',
  2587. ' s:={$I %pas2jstarget%};',
  2588. ' s:={$I %pas2jstargetos%};',
  2589. ' s:={$I %pas2jstargetcpu%};',
  2590. ' s:={$I %file%};',
  2591. '']);
  2592. ConvertProgram;
  2593. CheckSource('TestIncludeVersion',
  2594. LinesToStr([
  2595. 'this.s="";',
  2596. 'this.i = 0;']),
  2597. LinesToStr([
  2598. '$mod.s = "7";',
  2599. '$mod.i = 8;',
  2600. '$mod.s = "<anonymous>";',
  2601. '$mod.s = "Comp.Ver.tcmodules";',
  2602. '$mod.s = "Browser";',
  2603. '$mod.s = "Browser";',
  2604. '$mod.s = "ECMAScript5";',
  2605. '$mod.s = "test1.pp";',
  2606. '']));
  2607. end;
  2608. procedure TTestModule.TestVarInt;
  2609. begin
  2610. StartProgram(false);
  2611. Add('var MyI: longint;');
  2612. Add('begin');
  2613. ConvertProgram;
  2614. CheckSource('TestVarInt','this.MyI=0;','');
  2615. end;
  2616. procedure TTestModule.TestVarBaseTypes;
  2617. begin
  2618. StartProgram(false);
  2619. Add('var');
  2620. Add(' i: longint;');
  2621. Add(' s: string;');
  2622. Add(' c: char;');
  2623. Add(' b: boolean;');
  2624. Add(' d: double;');
  2625. Add(' i2: longint = 3;');
  2626. Add(' s2: string = ''foo'';');
  2627. Add(' c2: char = ''4'';');
  2628. Add(' b2: boolean = true;');
  2629. Add(' d2: double = 5.6;');
  2630. Add(' i3: longint = $707;');
  2631. Add(' i4: nativeint = 9007199254740991;');
  2632. Add(' i5: nativeint = -9007199254740991-1;');
  2633. Add(' i6: nativeint = $fffffffffffff;');
  2634. Add(' i7: nativeint = -$fffffffffffff-1;');
  2635. Add(' i8: byte = 00;');
  2636. Add(' u8: nativeuint = $fffffffffffff;');
  2637. Add(' u9: nativeuint = $0000000000000;');
  2638. Add(' u10: nativeuint = $00ff00;');
  2639. Add('begin');
  2640. ConvertProgram;
  2641. CheckSource('TestVarBaseTypes',
  2642. LinesToStr([
  2643. 'this.i = 0;',
  2644. 'this.s = "";',
  2645. 'this.c = "";',
  2646. 'this.b = false;',
  2647. 'this.d = 0.0;',
  2648. 'this.i2 = 3;',
  2649. 'this.s2 = "foo";',
  2650. 'this.c2 = "4";',
  2651. 'this.b2 = true;',
  2652. 'this.d2 = 5.6;',
  2653. 'this.i3 = 0x707;',
  2654. 'this.i4 = 9007199254740991;',
  2655. 'this.i5 = -9007199254740991-1;',
  2656. 'this.i6 = 0xfffffffffffff;',
  2657. 'this.i7 =-0xfffffffffffff-1;',
  2658. 'this.i8 = 0;',
  2659. 'this.u8 = 0xfffffffffffff;',
  2660. 'this.u9 = 0x0;',
  2661. 'this.u10 = 0xff00;'
  2662. ]),
  2663. '');
  2664. end;
  2665. procedure TTestModule.TestBaseTypeSingleFail;
  2666. begin
  2667. StartProgram(false);
  2668. Add('var s: single;');
  2669. SetExpectedPasResolverError('identifier not found "single"',PasResolveEval.nIdentifierNotFound);
  2670. ConvertProgram;
  2671. end;
  2672. procedure TTestModule.TestBaseTypeExtendedFail;
  2673. begin
  2674. StartProgram(false);
  2675. Add('var e: extended;');
  2676. SetExpectedPasResolverError('identifier not found "extended"',PasResolveEval.nIdentifierNotFound);
  2677. ConvertProgram;
  2678. end;
  2679. procedure TTestModule.TestConstBaseTypes;
  2680. begin
  2681. StartProgram(false);
  2682. Add('const');
  2683. Add(' i: longint = 3;');
  2684. Add(' s: string = ''foo'';');
  2685. Add(' c: char = ''4'';');
  2686. Add(' b: boolean = true;');
  2687. Add(' d: double = 5.6;');
  2688. Add(' e = low(word);');
  2689. Add(' f = high(word);');
  2690. Add('begin');
  2691. ConvertProgram;
  2692. CheckSource('TestVarBaseTypes',
  2693. LinesToStr([
  2694. 'this.i=3;',
  2695. 'this.s="foo";',
  2696. 'this.c="4";',
  2697. 'this.b=true;',
  2698. 'this.d=5.6;',
  2699. 'this.e = 0;',
  2700. 'this.f = 65535;'
  2701. ]),
  2702. '');
  2703. end;
  2704. procedure TTestModule.TestAliasTypeRef;
  2705. begin
  2706. StartProgram(false);
  2707. Add('type');
  2708. Add(' a=longint;');
  2709. Add(' b=a;');
  2710. Add('var');
  2711. Add(' c: A;');
  2712. Add(' d: B;');
  2713. Add('begin');
  2714. ConvertProgram;
  2715. CheckSource('TestAliasTypeRef',
  2716. LinesToStr([ // statements
  2717. 'this.c = 0;',
  2718. 'this.d = 0;'
  2719. ]),
  2720. LinesToStr([ // this.$main
  2721. ''
  2722. ]));
  2723. end;
  2724. procedure TTestModule.TestTypeCast_BaseTypes;
  2725. begin
  2726. StartProgram(false);
  2727. Add([
  2728. 'var',
  2729. ' i: longint;',
  2730. ' b: boolean;',
  2731. ' d: double;',
  2732. ' s: string;',
  2733. ' c: char;',
  2734. 'begin',
  2735. ' i:=longint(i);',
  2736. ' i:=longint(b);',
  2737. ' b:=boolean(b);',
  2738. ' b:=boolean(i);',
  2739. ' d:=double(d);',
  2740. ' d:=double(i);',
  2741. ' s:=string(s);',
  2742. ' s:=string(c);',
  2743. ' c:=char(c);',
  2744. ' c:=char(i);',
  2745. ' c:=char(65);',
  2746. ' c:=char(#10);',
  2747. ' c:=char(#$E000);',
  2748. '']);
  2749. ConvertProgram;
  2750. CheckSource('TestAliasTypeRef',
  2751. LinesToStr([ // statements
  2752. 'this.i = 0;',
  2753. 'this.b = false;',
  2754. 'this.d = 0.0;',
  2755. 'this.s = "";',
  2756. 'this.c = "";',
  2757. '']),
  2758. LinesToStr([ // this.$main
  2759. '$mod.i = $mod.i;',
  2760. '$mod.i = ($mod.b ? 1 : 0);',
  2761. '$mod.b = $mod.b;',
  2762. '$mod.b = $mod.i != 0;',
  2763. '$mod.d = $mod.d;',
  2764. '$mod.d = $mod.i;',
  2765. '$mod.s = $mod.s;',
  2766. '$mod.s = $mod.c;',
  2767. '$mod.c = $mod.c;',
  2768. '$mod.c = String.fromCharCode($mod.i);',
  2769. '$mod.c = "A";',
  2770. '$mod.c = "\n";',
  2771. '$mod.c = "";',
  2772. '']));
  2773. end;
  2774. procedure TTestModule.TestTypeCast_AliasBaseTypes;
  2775. begin
  2776. StartProgram(false);
  2777. Add('type');
  2778. Add(' integer = longint;');
  2779. Add(' TYesNo = boolean;');
  2780. Add(' TFloat = double;');
  2781. Add(' TCaption = string;');
  2782. Add(' TChar = char;');
  2783. Add('var');
  2784. Add(' i: integer;');
  2785. Add(' b: TYesNo;');
  2786. Add(' d: TFloat;');
  2787. Add(' s: TCaption;');
  2788. Add(' c: TChar;');
  2789. Add('begin');
  2790. Add(' i:=integer(i);');
  2791. Add(' i:=integer(b);');
  2792. Add(' b:=TYesNo(b);');
  2793. Add(' b:=TYesNo(i);');
  2794. Add(' d:=TFloat(d);');
  2795. Add(' d:=TFloat(i);');
  2796. Add(' s:=TCaption(s);');
  2797. Add(' s:=TCaption(c);');
  2798. Add(' c:=TChar(c);');
  2799. ConvertProgram;
  2800. CheckSource('TestAliasTypeRef',
  2801. LinesToStr([ // statements
  2802. 'this.i = 0;',
  2803. 'this.b = false;',
  2804. 'this.d = 0.0;',
  2805. 'this.s = "";',
  2806. 'this.c = "";',
  2807. '']),
  2808. LinesToStr([ // this.$main
  2809. '$mod.i = $mod.i;',
  2810. '$mod.i = ($mod.b ? 1 : 0);',
  2811. '$mod.b = $mod.b;',
  2812. '$mod.b = $mod.i != 0;',
  2813. '$mod.d = $mod.d;',
  2814. '$mod.d = $mod.i;',
  2815. '$mod.s = $mod.s;',
  2816. '$mod.s = $mod.c;',
  2817. '$mod.c = $mod.c;',
  2818. '']));
  2819. end;
  2820. procedure TTestModule.TestEmptyProc;
  2821. begin
  2822. StartProgram(false);
  2823. Add('procedure Test;');
  2824. Add('begin');
  2825. Add('end;');
  2826. Add('begin');
  2827. ConvertProgram;
  2828. CheckSource('TestEmptyProc',
  2829. LinesToStr([ // statements
  2830. 'this.Test = function () {',
  2831. '};'
  2832. ]),
  2833. LinesToStr([ // this.$main
  2834. ''
  2835. ]));
  2836. end;
  2837. procedure TTestModule.TestProcOneParam;
  2838. begin
  2839. StartProgram(false);
  2840. Add('procedure ProcA(i: longint);');
  2841. Add('begin');
  2842. Add('end;');
  2843. Add('begin');
  2844. Add(' PROCA(3);');
  2845. ConvertProgram;
  2846. CheckSource('TestProcOneParam',
  2847. LinesToStr([ // statements
  2848. 'this.ProcA = function (i) {',
  2849. '};'
  2850. ]),
  2851. LinesToStr([ // this.$main
  2852. '$mod.ProcA(3);'
  2853. ]));
  2854. end;
  2855. procedure TTestModule.TestFunctionWithoutParams;
  2856. begin
  2857. StartProgram(false);
  2858. Add('function FuncA: longint;');
  2859. Add('begin');
  2860. Add('end;');
  2861. Add('var i: longint;');
  2862. Add('begin');
  2863. Add(' I:=FUNCA();');
  2864. Add(' I:=FUNCA;');
  2865. Add(' FUNCA();');
  2866. Add(' FUNCA;');
  2867. ConvertProgram;
  2868. CheckSource('TestProcWithoutParams',
  2869. LinesToStr([ // statements
  2870. 'this.FuncA = function () {',
  2871. ' var Result = 0;',
  2872. ' return Result;',
  2873. '};',
  2874. 'this.i=0;'
  2875. ]),
  2876. LinesToStr([ // this.$main
  2877. '$mod.i=$mod.FuncA();',
  2878. '$mod.i=$mod.FuncA();',
  2879. '$mod.FuncA();',
  2880. '$mod.FuncA();'
  2881. ]));
  2882. end;
  2883. procedure TTestModule.TestProcedureWithoutParams;
  2884. begin
  2885. StartProgram(false);
  2886. Add('procedure ProcA;');
  2887. Add('begin');
  2888. Add('end;');
  2889. Add('begin');
  2890. Add(' PROCA();');
  2891. Add(' PROCA;');
  2892. ConvertProgram;
  2893. CheckSource('TestProcWithoutParams',
  2894. LinesToStr([ // statements
  2895. 'this.ProcA = function () {',
  2896. '};'
  2897. ]),
  2898. LinesToStr([ // this.$main
  2899. '$mod.ProcA();',
  2900. '$mod.ProcA();'
  2901. ]));
  2902. end;
  2903. procedure TTestModule.TestIncDec;
  2904. begin
  2905. StartProgram(false);
  2906. Add([
  2907. 'procedure DoIt(var i: longint);',
  2908. 'begin',
  2909. ' inc(i);',
  2910. ' inc(i,2);',
  2911. 'end;',
  2912. 'var',
  2913. ' Bar: longint;',
  2914. 'begin',
  2915. ' inc(bar);',
  2916. ' inc(bar,2);',
  2917. ' dec(bar);',
  2918. ' dec(bar,3);',
  2919. '']);
  2920. ConvertProgram;
  2921. CheckSource('TestIncDec',
  2922. LinesToStr([ // statements
  2923. 'this.DoIt = function (i) {',
  2924. ' i.set(i.get()+1);',
  2925. ' i.set(i.get()+2);',
  2926. '};',
  2927. 'this.Bar = 0;'
  2928. ]),
  2929. LinesToStr([ // this.$main
  2930. '$mod.Bar+=1;',
  2931. '$mod.Bar+=2;',
  2932. '$mod.Bar-=1;',
  2933. '$mod.Bar-=3;'
  2934. ]));
  2935. end;
  2936. procedure TTestModule.TestLoHiFpcMode;
  2937. begin
  2938. StartProgram(false);
  2939. Add([
  2940. '{$mode objfpc}',
  2941. 'const',
  2942. ' LoByte1 = Lo(Word($1234));',
  2943. ' HiByte1 = Hi(Word($1234));',
  2944. ' LoByte2 = Lo(SmallInt($1234));',
  2945. ' HiByte2 = Hi(SmallInt($1234));',
  2946. ' LoWord1 = Lo($1234CDEF);',
  2947. ' HiWord1 = Hi($1234CDEF);',
  2948. ' LoWord2 = Lo(-$1234CDEF);',
  2949. ' HiWord2 = Hi(-$1234CDEF);',
  2950. ' lo4:byte=lo(byte($34));',
  2951. ' hi4:byte=hi(byte($34));',
  2952. ' lo5:byte=lo(shortint(-$34));',
  2953. ' hi5:byte=hi(shortint(-$34));',
  2954. ' lo6:longword=lo($123456789ABCD);',
  2955. ' hi6:longword=hi($123456789ABCD);',
  2956. ' lo7:longword=lo(-$123456789ABCD);',
  2957. ' hi7:longword=hi(-$123456789ABCD);',
  2958. 'var',
  2959. ' b: Byte;',
  2960. ' ss: shortint;',
  2961. ' w: Word;',
  2962. ' si: SmallInt;',
  2963. ' lw: LongWord;',
  2964. ' li: LongInt;',
  2965. ' b2: Byte;',
  2966. ' ni: nativeint;',
  2967. 'begin',
  2968. ' w := $1234;',
  2969. ' ss := -$12;',
  2970. ' b := lo(ss);',
  2971. ' b := HI(ss);',
  2972. ' b := lo(w);',
  2973. ' b := HI(w);',
  2974. ' b2 := lo(b);',
  2975. ' b2 := hi(b);',
  2976. ' lw := $1234CDEF;',
  2977. ' w := lo(lw);',
  2978. ' w := hi(lw);',
  2979. ' ni := $123456789ABCD;',
  2980. ' lw := lo(ni);',
  2981. ' lw := hi(ni);',
  2982. '']);
  2983. ConvertProgram;
  2984. CheckSource('TestLoHiFpcMode',
  2985. LinesToStr([ // statements
  2986. 'this.LoByte1 = 0x1234 & 0xFF;',
  2987. 'this.HiByte1 = (0x1234 >> 8) & 0xFF;',
  2988. 'this.LoByte2 = 0x1234 & 0xFF;',
  2989. 'this.HiByte2 = (0x1234 >> 8) & 0xFF;',
  2990. 'this.LoWord1 = 0x1234CDEF & 0xFFFF;',
  2991. 'this.HiWord1 = (0x1234CDEF >> 16) & 0xFFFF;',
  2992. 'this.LoWord2 = -0x1234CDEF >>> 0;',
  2993. 'this.HiWord2 = Math.floor(-0x1234CDEF / 4294967296) >>> 0;',
  2994. 'this.lo4 = 0x34 & 0xF;',
  2995. 'this.hi4 = (0x34 >> 4) & 0xF;',
  2996. 'this.lo5 = (((-0x34 & 255) << 24) >> 24) & 0xFF;',
  2997. 'this.hi5 = ((((-0x34 & 255) << 24) >> 24) >> 8) & 0xFF;',
  2998. 'this.lo6 = 0x123456789ABCD >>> 0;',
  2999. 'this.hi6 = 74565 >>> 0;',
  3000. 'this.lo7 = -0x123456789ABCD >>> 0;',
  3001. 'this.hi7 = Math.floor(-0x123456789ABCD / 4294967296) >>> 0;',
  3002. 'this.b = 0;',
  3003. 'this.ss = 0;',
  3004. 'this.w = 0;',
  3005. 'this.si = 0;',
  3006. 'this.lw = 0;',
  3007. 'this.li = 0;',
  3008. 'this.b2 = 0;',
  3009. 'this.ni = 0;',
  3010. '']),
  3011. LinesToStr([ // this.$main
  3012. '$mod.w = 0x1234;',
  3013. '$mod.ss = -0x12;',
  3014. '$mod.b = $mod.ss & 0xFF;',
  3015. '$mod.b = ($mod.ss >> 8) & 0xFF;',
  3016. '$mod.b = $mod.w & 0xFF;',
  3017. '$mod.b = ($mod.w >> 8) & 0xFF;',
  3018. '$mod.b2 = $mod.b & 0xF;',
  3019. '$mod.b2 = ($mod.b >> 4) & 0xF;',
  3020. '$mod.lw = 0x1234CDEF;',
  3021. '$mod.w = $mod.lw & 0xFFFF;',
  3022. '$mod.w = ($mod.lw >> 16) & 0xFFFF;',
  3023. '$mod.ni = 0x123456789ABCD;',
  3024. '$mod.lw = $mod.ni >>> 0;',
  3025. '$mod.lw = Math.floor($mod.ni / 4294967296) >>> 0;',
  3026. '']));
  3027. end;
  3028. procedure TTestModule.TestLoHiDelphiMode;
  3029. begin
  3030. StartProgram(false);
  3031. Add([
  3032. '{$mode delphi}',
  3033. 'const',
  3034. ' LoByte1 = Lo(Word($1234));',
  3035. ' HiByte1 = Hi(Word($1234));',
  3036. ' LoByte2 = Lo(SmallInt($1234));',
  3037. ' HiByte2 = Hi(SmallInt($1234));',
  3038. ' LoByte3 = Lo($1234CDEF);',
  3039. ' HiByte3 = Hi($1234CDEF);',
  3040. ' LoByte4 = Lo(-$1234CDEF);',
  3041. ' HiByte4 = Hi(-$1234CDEF);',
  3042. 'var',
  3043. ' b: Byte;',
  3044. ' w: Word;',
  3045. ' si: SmallInt;',
  3046. ' lw: LongWord;',
  3047. ' li: LongInt;',
  3048. 'begin',
  3049. ' w := $1234;',
  3050. ' b := lo(w);',
  3051. ' b := HI(w);',
  3052. ' lw := $1234CDEF;',
  3053. ' b := lo(lw);',
  3054. ' b := hi(lw);',
  3055. '']);
  3056. ConvertProgram;
  3057. CheckSource('TestLoHiDelphiMode',
  3058. LinesToStr([ // statements
  3059. 'this.LoByte1 = 0x1234 & 0xFF;',
  3060. 'this.HiByte1 = (0x1234 >> 8) & 0xFF;',
  3061. 'this.LoByte2 = 0x1234 & 0xFF;',
  3062. 'this.HiByte2 = (0x1234 >> 8) & 0xFF;',
  3063. 'this.LoByte3 = 0x1234CDEF & 0xFF;',
  3064. 'this.HiByte3 = (0x1234CDEF >> 8) & 0xFF;',
  3065. 'this.LoByte4 = -0x1234CDEF & 0xFF;',
  3066. 'this.HiByte4 = (-0x1234CDEF >> 8) & 0xFF;',
  3067. 'this.b = 0;',
  3068. 'this.w = 0;',
  3069. 'this.si = 0;',
  3070. 'this.lw = 0;',
  3071. 'this.li = 0;'
  3072. ]),
  3073. LinesToStr([ // this.$main
  3074. '$mod.w = 0x1234;',
  3075. '$mod.b = $mod.w & 0xFF;',
  3076. '$mod.b = ($mod.w >> 8) & 0xFF;',
  3077. '$mod.lw = 0x1234CDEF;',
  3078. '$mod.b = $mod.lw & 0xFF;',
  3079. '$mod.b = ($mod.lw >> 8) & 0xFF;'
  3080. ]));
  3081. end;
  3082. procedure TTestModule.TestAssignments;
  3083. begin
  3084. StartProgram(false);
  3085. Parser.Options:=Parser.Options+[po_cassignments];
  3086. Add('var');
  3087. Add(' Bar:longint;');
  3088. Add('begin');
  3089. Add(' bar:=3;');
  3090. Add(' bar+=4;');
  3091. Add(' bar-=5;');
  3092. Add(' bar*=6;');
  3093. ConvertProgram;
  3094. CheckSource('TestAssignments',
  3095. LinesToStr([ // statements
  3096. 'this.Bar = 0;'
  3097. ]),
  3098. LinesToStr([ // this.$main
  3099. '$mod.Bar=3;',
  3100. '$mod.Bar+=4;',
  3101. '$mod.Bar-=5;',
  3102. '$mod.Bar*=6;'
  3103. ]));
  3104. end;
  3105. procedure TTestModule.TestArithmeticOperators1;
  3106. begin
  3107. StartProgram(false);
  3108. Add('var');
  3109. Add(' vA,vB,vC:longint;');
  3110. Add('begin');
  3111. Add(' va:=1;');
  3112. Add(' vb:=va+va;');
  3113. Add(' vb:=va div vb;');
  3114. Add(' vb:=va mod vb;');
  3115. Add(' vb:=va+va*vb+va div vb;');
  3116. Add(' vc:=-va;');
  3117. Add(' va:=va-vb;');
  3118. Add(' vb:=va;');
  3119. Add(' if va<vb then vc:=va else vc:=vb;');
  3120. ConvertProgram;
  3121. CheckSource('TestArithmeticOperators1',
  3122. LinesToStr([ // statements
  3123. 'this.vA = 0;',
  3124. 'this.vB = 0;',
  3125. 'this.vC = 0;'
  3126. ]),
  3127. LinesToStr([ // this.$main
  3128. '$mod.vA = 1;',
  3129. '$mod.vB = $mod.vA + $mod.vA;',
  3130. '$mod.vB = rtl.trunc($mod.vA / $mod.vB);',
  3131. '$mod.vB = $mod.vA % $mod.vB;',
  3132. '$mod.vB = $mod.vA + ($mod.vA * $mod.vB) + rtl.trunc($mod.vA / $mod.vB);',
  3133. '$mod.vC = -$mod.vA;',
  3134. '$mod.vA = $mod.vA - $mod.vB;',
  3135. '$mod.vB = $mod.vA;',
  3136. 'if ($mod.vA < $mod.vB){ $mod.vC = $mod.vA } else $mod.vC = $mod.vB;'
  3137. ]));
  3138. end;
  3139. procedure TTestModule.TestMultiAdd;
  3140. begin
  3141. StartProgram(false);
  3142. Add([
  3143. 'function Fly: string; external name ''fly'';',
  3144. 'function TryEncodeDate(Year, Month, Day: Word): Boolean;',
  3145. 'var',
  3146. ' Date: double;',
  3147. 'begin',
  3148. ' Result:=(Year>0) and (Year<10000) and',
  3149. ' (Month >= 1) and (Month<=12) and',
  3150. ' (Day>0) and (Day<=31);',
  3151. ' Date := (146097*Year) SHR 2 + (1461*Year) SHR 2 + (153*LongWord(Month)+2) DIV 5 + LongWord(Day);',
  3152. 'end;',
  3153. 'var s: string;',
  3154. 'begin',
  3155. ' s:=''a''+''b''+''c''+''d'';',
  3156. ' s:=s+Fly+''e'';',
  3157. ' s:=Fly+Fly+Fly;',
  3158. '']);
  3159. ConvertProgram;
  3160. CheckSource('TestMultiAdd',
  3161. LinesToStr([ // statements
  3162. 'this.TryEncodeDate = function (Year, Month, Day) {',
  3163. ' var Result = false;',
  3164. ' var date = 0.0;',
  3165. ' Result = (Year > 0) && (Year < 10000) && (Month >= 1) && (Month <= 12) && (Day > 0) && (Day <= 31);',
  3166. ' date = ((146097 * Year) >>> 2) + ((1461 * Year) >>> 2) + rtl.trunc(((153 * Month) + 2) / 5) + Day;',
  3167. ' return Result;',
  3168. '};',
  3169. 'this.s = "";',
  3170. '']),
  3171. LinesToStr([ // this.$main
  3172. '$mod.s = "a" + "b" + "c" + "d";',
  3173. '$mod.s = $mod.s + fly() + "e";',
  3174. '$mod.s = fly() + fly() + fly();',
  3175. '']));
  3176. end;
  3177. procedure TTestModule.TestLogicalOperators;
  3178. begin
  3179. StartProgram(false);
  3180. Add('var');
  3181. Add(' vA,vB,vC:boolean;');
  3182. Add('begin');
  3183. Add(' va:=vb and vc;');
  3184. Add(' va:=vb or vc;');
  3185. Add(' va:=vb xor vc;');
  3186. Add(' va:=true and vc;');
  3187. Add(' va:=(vb and vc) or (va and vb);');
  3188. Add(' va:=not vb;');
  3189. ConvertProgram;
  3190. CheckSource('TestLogicalOperators',
  3191. LinesToStr([ // statements
  3192. 'this.vA = false;',
  3193. 'this.vB = false;',
  3194. 'this.vC = false;'
  3195. ]),
  3196. LinesToStr([ // this.$main
  3197. '$mod.vA = $mod.vB && $mod.vC;',
  3198. '$mod.vA = $mod.vB || $mod.vC;',
  3199. '$mod.vA = $mod.vB ^ $mod.vC;',
  3200. '$mod.vA = true && $mod.vC;',
  3201. '$mod.vA = ($mod.vB && $mod.vC) || ($mod.vA && $mod.vB);',
  3202. '$mod.vA = !$mod.vB;'
  3203. ]));
  3204. end;
  3205. procedure TTestModule.TestBitwiseOperators;
  3206. begin
  3207. StartProgram(false);
  3208. Add([
  3209. 'var',
  3210. ' vA,vB,vC:longint;',
  3211. ' X,Y,Z: nativeint;',
  3212. 'begin',
  3213. ' va:=vb and vc;',
  3214. ' va:=vb or vc;',
  3215. ' va:=vb xor vc;',
  3216. ' va:=vb shl vc;',
  3217. ' va:=vb shr vc;',
  3218. ' va:=3 and vc;',
  3219. ' va:=(vb and vc) or (va and vb);',
  3220. ' va:=not vb;',
  3221. ' X:=Y and Z;',
  3222. ' X:=Y and va;',
  3223. ' X:=Y or Z;',
  3224. ' X:=Y or va;',
  3225. ' X:=Y xor Z;',
  3226. ' X:=Y xor va;',
  3227. '']);
  3228. ConvertProgram;
  3229. CheckSource('TestBitwiseOperators',
  3230. LinesToStr([ // statements
  3231. 'this.vA = 0;',
  3232. 'this.vB = 0;',
  3233. 'this.vC = 0;',
  3234. 'this.X = 0;',
  3235. 'this.Y = 0;',
  3236. 'this.Z = 0;',
  3237. '']),
  3238. LinesToStr([ // this.$main
  3239. '$mod.vA = $mod.vB & $mod.vC;',
  3240. '$mod.vA = $mod.vB | $mod.vC;',
  3241. '$mod.vA = $mod.vB ^ $mod.vC;',
  3242. '$mod.vA = $mod.vB << $mod.vC;',
  3243. '$mod.vA = $mod.vB >>> $mod.vC;',
  3244. '$mod.vA = 3 & $mod.vC;',
  3245. '$mod.vA = ($mod.vB & $mod.vC) | ($mod.vA & $mod.vB);',
  3246. '$mod.vA = ~$mod.vB;',
  3247. '$mod.X = rtl.and($mod.Y, $mod.Z);',
  3248. '$mod.X = $mod.Y & $mod.vA;',
  3249. '$mod.X = rtl.or($mod.Y, $mod.Z);',
  3250. '$mod.X = rtl.or($mod.Y, $mod.vA);',
  3251. '$mod.X = rtl.xor($mod.Y, $mod.Z);',
  3252. '$mod.X = rtl.xor($mod.Y, $mod.vA);',
  3253. '']));
  3254. end;
  3255. procedure TTestModule.TestBitwiseOperatorsLongword;
  3256. begin
  3257. StartProgram(false);
  3258. Add([
  3259. 'var',
  3260. ' a,b,c:longword;',
  3261. ' i: longint;',
  3262. 'begin',
  3263. ' a:=$12345678;',
  3264. ' b:=$EDCBA987;',
  3265. ' c:=not a;',
  3266. ' c:=a and b;',
  3267. ' c:=a and $ffff0000;',
  3268. ' c:=a or b;',
  3269. ' c:=a or $ff00ff00;',
  3270. ' c:=a xor b;',
  3271. ' c:=a xor $f0f0f0f0;',
  3272. ' c:=a shl 1;',
  3273. ' c:=a shl 16;',
  3274. ' c:=a shl 24;',
  3275. ' c:=a shl b;',
  3276. ' c:=a shr 1;',
  3277. ' c:=a shr 16;',
  3278. ' c:=a shr 24;',
  3279. ' c:=a shr b;',
  3280. ' c:=(b and c) or (a and b);',
  3281. ' c:=i and a;',
  3282. ' c:=i or a;',
  3283. ' c:=i xor a;',
  3284. '']);
  3285. ConvertProgram;
  3286. CheckSource('TestBitwiseOperatorsLongword',
  3287. LinesToStr([ // statements
  3288. 'this.a = 0;',
  3289. 'this.b = 0;',
  3290. 'this.c = 0;',
  3291. 'this.i = 0;',
  3292. '']),
  3293. LinesToStr([ // this.$main
  3294. '$mod.a = 0x12345678;',
  3295. '$mod.b = 0xEDCBA987;',
  3296. '$mod.c = rtl.lw(~$mod.a);',
  3297. '$mod.c = rtl.lw($mod.a & $mod.b);',
  3298. '$mod.c = rtl.lw($mod.a & 0xffff0000);',
  3299. '$mod.c = rtl.lw($mod.a | $mod.b);',
  3300. '$mod.c = rtl.lw($mod.a | 0xff00ff00);',
  3301. '$mod.c = rtl.lw($mod.a ^ $mod.b);',
  3302. '$mod.c = rtl.lw($mod.a ^ 0xf0f0f0f0);',
  3303. '$mod.c = rtl.lw($mod.a << 1);',
  3304. '$mod.c = rtl.lw($mod.a << 16);',
  3305. '$mod.c = rtl.lw($mod.a << 24);',
  3306. '$mod.c = rtl.lw($mod.a << $mod.b);',
  3307. '$mod.c = rtl.lw($mod.a >>> 1);',
  3308. '$mod.c = rtl.lw($mod.a >>> 16);',
  3309. '$mod.c = rtl.lw($mod.a >>> 24);',
  3310. '$mod.c = rtl.lw($mod.a >>> $mod.b);',
  3311. '$mod.c = rtl.lw(rtl.lw($mod.b & $mod.c) | rtl.lw($mod.a & $mod.b));',
  3312. '$mod.c = $mod.i & $mod.a;',
  3313. '$mod.c = $mod.i | $mod.a;',
  3314. '$mod.c = $mod.i ^ $mod.a;',
  3315. '']));
  3316. end;
  3317. procedure TTestModule.TestPrgProcVar;
  3318. begin
  3319. StartProgram(false);
  3320. Add('procedure Proc1;');
  3321. Add('type');
  3322. Add(' t1=longint;');
  3323. Add('var');
  3324. Add(' vA:t1;');
  3325. Add('begin');
  3326. Add('end;');
  3327. Add('begin');
  3328. ConvertProgram;
  3329. CheckSource('TestPrgProcVar',
  3330. LinesToStr([ // statements
  3331. 'this.Proc1 = function () {',
  3332. ' var vA=0;',
  3333. '};'
  3334. ]),
  3335. LinesToStr([ // this.$main
  3336. ''
  3337. ]));
  3338. end;
  3339. procedure TTestModule.TestUnitProcVar;
  3340. begin
  3341. StartUnit(false);
  3342. Add('interface');
  3343. Add('');
  3344. Add('type tA=string; // unit scope');
  3345. Add('procedure Proc1;');
  3346. Add('');
  3347. Add('implementation');
  3348. Add('');
  3349. Add('procedure Proc1;');
  3350. Add('type tA=longint; // local proc scope');
  3351. Add('var v1:tA; // using local tA');
  3352. Add('begin');
  3353. Add('end;');
  3354. Add('var v2:tA; // using interface tA');
  3355. ConvertUnit;
  3356. CheckSource('TestUnitProcVar',
  3357. LinesToStr([ // statements
  3358. 'var $impl = $mod.$impl;',
  3359. 'this.Proc1 = function () {',
  3360. ' var v1 = 0;',
  3361. '};',
  3362. '']),
  3363. // this.$init
  3364. '',
  3365. // implementation
  3366. LinesToStr([
  3367. '$impl.v2 = "";',
  3368. '']));
  3369. end;
  3370. procedure TTestModule.TestImplProc;
  3371. begin
  3372. StartUnit(false);
  3373. Add('interface');
  3374. Add('');
  3375. Add('procedure Proc1;');
  3376. Add('');
  3377. Add('implementation');
  3378. Add('');
  3379. Add('procedure Proc1; begin end;');
  3380. Add('procedure Proc2; begin end;');
  3381. Add('initialization');
  3382. Add(' Proc1;');
  3383. Add(' Proc2;');
  3384. ConvertUnit;
  3385. CheckSource('TestImplProc',
  3386. LinesToStr([ // statements
  3387. 'var $impl = $mod.$impl;',
  3388. 'this.Proc1 = function () {',
  3389. '};',
  3390. '']),
  3391. LinesToStr([ // this.$init
  3392. '$mod.Proc1();',
  3393. '$impl.Proc2();',
  3394. '']),
  3395. LinesToStr([ // implementation
  3396. '$impl.Proc2 = function () {',
  3397. '};',
  3398. ''])
  3399. );
  3400. end;
  3401. procedure TTestModule.TestFunctionResult;
  3402. begin
  3403. StartProgram(false);
  3404. Add('function Func1: longint;');
  3405. Add('begin');
  3406. Add(' Result:=3;');
  3407. Add(' Func1:=4;');
  3408. Add('end;');
  3409. Add('begin');
  3410. ConvertProgram;
  3411. CheckSource('TestFunctionResult',
  3412. LinesToStr([ // statements
  3413. 'this.Func1 = function () {',
  3414. ' var Result = 0;',
  3415. ' Result = 3;',
  3416. ' Result = 4;',
  3417. ' return Result;',
  3418. '};'
  3419. ]),
  3420. '');
  3421. end;
  3422. procedure TTestModule.TestNestedProc;
  3423. begin
  3424. StartProgram(false);
  3425. Add([
  3426. 'var vInUnit: longint;',
  3427. 'function DoIt(pA,pD: longint): longint;',
  3428. 'var',
  3429. ' vB: longint;',
  3430. ' vC: longint;',
  3431. ' function Nesty(pA: longint): longint; ',
  3432. ' var vB: longint;',
  3433. ' begin',
  3434. ' Result:=pa+vb+vc+pd+vInUnit;',
  3435. ' nesty:=3;',
  3436. ' doit:=4;',
  3437. ' exit;',
  3438. ' end;',
  3439. 'begin',
  3440. ' Result:=pa+vb+vc;',
  3441. ' doit:=6;',
  3442. ' exit;',
  3443. 'end;',
  3444. 'begin']);
  3445. ConvertProgram;
  3446. CheckSource('TestNestedProc',
  3447. LinesToStr([ // statements
  3448. 'this.vInUnit = 0;',
  3449. 'this.DoIt = function (pA, pD) {',
  3450. ' var Result = 0;',
  3451. ' var vB = 0;',
  3452. ' var vC = 0;',
  3453. ' function Nesty(pA) {',
  3454. ' var Result$1 = 0;',
  3455. ' var vB = 0;',
  3456. ' Result$1 = pA + vB + vC + pD + $mod.vInUnit;',
  3457. ' Result$1 = 3;',
  3458. ' Result = 4;',
  3459. ' return Result$1;',
  3460. ' return Result$1;',
  3461. ' };',
  3462. ' Result = pA + vB + vC;',
  3463. ' Result = 6;',
  3464. ' return Result;',
  3465. ' return Result;',
  3466. '};'
  3467. ]),
  3468. '');
  3469. end;
  3470. procedure TTestModule.TestNestedProc_ResultString;
  3471. begin
  3472. StartProgram(false);
  3473. Add([
  3474. 'function DoIt: string;',
  3475. ' function Nesty: string; ',
  3476. ' begin',
  3477. ' nesty:=#65#66;',
  3478. ' nesty[1]:=#67;',
  3479. ' doit:=#68;',
  3480. ' doit[2]:=#69;',
  3481. ' end;',
  3482. 'begin',
  3483. ' doit:=#70;',
  3484. ' doit[3]:=#71;',
  3485. 'end;',
  3486. 'begin']);
  3487. ConvertProgram;
  3488. CheckSource('TestNestedProc_ResultString',
  3489. LinesToStr([ // statements
  3490. 'this.DoIt = function () {',
  3491. ' var Result = "";',
  3492. ' function Nesty() {',
  3493. ' var Result$1 = "";',
  3494. ' Result$1 = "AB";',
  3495. ' Result$1 = rtl.setCharAt(Result$1, 0, "C");',
  3496. ' Result = "D";',
  3497. ' Result = rtl.setCharAt(Result, 1, "E");',
  3498. ' return Result$1;',
  3499. ' };',
  3500. ' Result = "F";',
  3501. ' Result = rtl.setCharAt(Result, 2, "G");',
  3502. ' return Result;',
  3503. '};'
  3504. ]),
  3505. '');
  3506. end;
  3507. procedure TTestModule.TestForwardProc;
  3508. begin
  3509. StartProgram(false);
  3510. Add('procedure FuncA(Bar: longint); forward;');
  3511. Add('procedure FuncB(Bar: longint);');
  3512. Add('begin');
  3513. Add(' funca(bar);');
  3514. Add('end;');
  3515. Add('procedure funca(bar: longint);');
  3516. Add('begin');
  3517. Add(' if bar=3 then ;');
  3518. Add('end;');
  3519. Add('begin');
  3520. Add(' funca(4);');
  3521. Add(' funcb(5);');
  3522. ConvertProgram;
  3523. CheckSource('TestForwardProc',
  3524. LinesToStr([ // statements'
  3525. 'this.FuncB = function (Bar) {',
  3526. ' $mod.FuncA(Bar);',
  3527. '};',
  3528. 'this.FuncA = function (Bar) {',
  3529. ' if (Bar === 3);',
  3530. '};'
  3531. ]),
  3532. LinesToStr([
  3533. '$mod.FuncA(4);',
  3534. '$mod.FuncB(5);'
  3535. ])
  3536. );
  3537. end;
  3538. procedure TTestModule.TestNestedForwardProc;
  3539. begin
  3540. StartProgram(false);
  3541. Add('procedure FuncA;');
  3542. Add(' procedure FuncB(i: longint); forward;');
  3543. Add(' procedure FuncC(i: longint);');
  3544. Add(' begin');
  3545. Add(' funcb(i);');
  3546. Add(' end;');
  3547. Add(' procedure FuncB(i: longint);');
  3548. Add(' begin');
  3549. Add(' if i=3 then ;');
  3550. Add(' end;');
  3551. Add('begin');
  3552. Add(' funcc(4)');
  3553. Add('end;');
  3554. Add('begin');
  3555. Add(' funca;');
  3556. ConvertProgram;
  3557. CheckSource('TestNestedForwardProc',
  3558. LinesToStr([ // statements'
  3559. 'this.FuncA = function () {',
  3560. ' function FuncC(i) {',
  3561. ' FuncB(i);',
  3562. ' };',
  3563. ' function FuncB(i) {',
  3564. ' if (i === 3);',
  3565. ' };',
  3566. ' FuncC(4);',
  3567. '};'
  3568. ]),
  3569. LinesToStr([
  3570. '$mod.FuncA();'
  3571. ])
  3572. );
  3573. end;
  3574. procedure TTestModule.TestAssignFunctionResult;
  3575. begin
  3576. StartProgram(false);
  3577. Add('function Func1: longint;');
  3578. Add('begin');
  3579. Add('end;');
  3580. Add('var i: longint;');
  3581. Add('begin');
  3582. Add(' i:=func1();');
  3583. Add(' i:=func1()+func1();');
  3584. ConvertProgram;
  3585. CheckSource('TestAssignFunctionResult',
  3586. LinesToStr([ // statements
  3587. 'this.Func1 = function () {',
  3588. ' var Result = 0;',
  3589. ' return Result;',
  3590. '};',
  3591. 'this.i = 0;'
  3592. ]),
  3593. LinesToStr([
  3594. '$mod.i = $mod.Func1();',
  3595. '$mod.i = $mod.Func1() + $mod.Func1();'
  3596. ]));
  3597. end;
  3598. procedure TTestModule.TestFunctionResultInCondition;
  3599. begin
  3600. StartProgram(false);
  3601. Add('function Func1: longint;');
  3602. Add('begin');
  3603. Add('end;');
  3604. Add('function Func2: boolean;');
  3605. Add('begin');
  3606. Add('end;');
  3607. Add('var i: longint;');
  3608. Add('begin');
  3609. Add(' if func2 then ;');
  3610. Add(' if i=func1() then ;');
  3611. Add(' if i=func1 then ;');
  3612. ConvertProgram;
  3613. CheckSource('TestFunctionResultInCondition',
  3614. LinesToStr([ // statements
  3615. 'this.Func1 = function () {',
  3616. ' var Result = 0;',
  3617. ' return Result;',
  3618. '};',
  3619. 'this.Func2 = function () {',
  3620. ' var Result = false;',
  3621. ' return Result;',
  3622. '};',
  3623. 'this.i = 0;'
  3624. ]),
  3625. LinesToStr([
  3626. 'if ($mod.Func2());',
  3627. 'if ($mod.i === $mod.Func1());',
  3628. 'if ($mod.i === $mod.Func1());'
  3629. ]));
  3630. end;
  3631. procedure TTestModule.TestFunctionResultInForLoop;
  3632. begin
  3633. StartProgram(false);
  3634. Add([
  3635. 'function Func1(a: array of longint): longint;',
  3636. 'begin',
  3637. ' for Result:=High(a) downto Low(a) do if a[Result]=0 then exit;',
  3638. ' for Result in a do if a[Result]=0 then exit;',
  3639. 'end;',
  3640. 'begin',
  3641. ' Func1([1,2,3])']);
  3642. ConvertProgram;
  3643. CheckSource('TestFunctionResultInForLoop',
  3644. LinesToStr([ // statements
  3645. 'this.Func1 = function (a) {',
  3646. ' var Result = 0;',
  3647. ' for (var $l = rtl.length(a) - 1; $l >= 0; $l--) {',
  3648. ' Result = $l;',
  3649. ' if (a[Result] === 0) return Result;',
  3650. ' };',
  3651. ' for (var $in = a, $l1 = 0, $end = rtl.length($in) - 1; $l1 <= $end; $l1++) {',
  3652. ' Result = $in[$l1];',
  3653. ' if (a[Result] === 0) return Result;',
  3654. ' };',
  3655. ' return Result;',
  3656. '};',
  3657. '']),
  3658. LinesToStr([
  3659. '$mod.Func1([1, 2, 3]);'
  3660. ]));
  3661. end;
  3662. procedure TTestModule.TestFunctionResultInTypeCast;
  3663. begin
  3664. StartProgram(false);
  3665. Add([
  3666. 'function GetInt: longint;',
  3667. 'begin',
  3668. 'end;',
  3669. 'begin',
  3670. ' if Byte(GetInt)=0 then ;',
  3671. '']);
  3672. ConvertProgram;
  3673. CheckSource('TestFunctionResultInTypeCast',
  3674. LinesToStr([ // statements
  3675. 'this.GetInt = function () {',
  3676. ' var Result = 0;',
  3677. ' return Result;',
  3678. '};',
  3679. '']),
  3680. LinesToStr([
  3681. 'if (($mod.GetInt() & 255) === 0) ;'
  3682. ]));
  3683. end;
  3684. procedure TTestModule.TestExit;
  3685. begin
  3686. StartProgram(false);
  3687. Add('procedure ProcA;');
  3688. Add('begin');
  3689. Add(' exit;');
  3690. Add('end;');
  3691. Add('function FuncB: longint;');
  3692. Add('begin');
  3693. Add(' exit;');
  3694. Add(' exit(3);');
  3695. Add('end;');
  3696. Add('function FuncC: string;');
  3697. Add('begin');
  3698. Add(' exit;');
  3699. Add(' exit(''a'');');
  3700. Add(' exit(''abc'');');
  3701. Add('end;');
  3702. Add('begin');
  3703. Add(' exit;');
  3704. Add(' exit(1);');
  3705. ConvertProgram;
  3706. CheckSource('TestExit',
  3707. LinesToStr([ // statements
  3708. 'this.ProcA = function () {',
  3709. ' return;',
  3710. '};',
  3711. 'this.FuncB = function () {',
  3712. ' var Result = 0;',
  3713. ' return Result;',
  3714. ' return 3;',
  3715. ' return Result;',
  3716. '};',
  3717. 'this.FuncC = function () {',
  3718. ' var Result = "";',
  3719. ' return Result;',
  3720. ' return "a";',
  3721. ' return "abc";',
  3722. ' return Result;',
  3723. '};'
  3724. ]),
  3725. LinesToStr([
  3726. 'return;',
  3727. 'return 1;',
  3728. '']));
  3729. end;
  3730. procedure TTestModule.TestExit_ResultInFinally;
  3731. begin
  3732. StartProgram(false);
  3733. Add([
  3734. 'function Run: word;',
  3735. 'begin',
  3736. ' try',
  3737. ' exit(3);', // no Result in finally -> use return 3
  3738. ' finally',
  3739. ' end;',
  3740. 'end;',
  3741. 'function Fly: word;',
  3742. 'begin',
  3743. ' try',
  3744. ' exit(3);',
  3745. ' finally',
  3746. ' if Result>0 then ;',
  3747. ' end;',
  3748. 'end;',
  3749. 'function Jump: word;',
  3750. 'begin',
  3751. ' try',
  3752. ' try',
  3753. ' exit(4);',
  3754. ' finally',
  3755. ' end;',
  3756. ' finally',
  3757. ' if Result>0 then ;',
  3758. ' end;',
  3759. 'end;',
  3760. 'begin',
  3761. '']);
  3762. ConvertProgram;
  3763. CheckSource('TestExit_ResultInFinally',
  3764. LinesToStr([ // statements
  3765. 'this.Run = function () {',
  3766. ' var Result = 0;',
  3767. ' try {',
  3768. ' return 3;',
  3769. ' } finally {',
  3770. ' };',
  3771. ' return Result;',
  3772. '};',
  3773. 'this.Fly = function () {',
  3774. ' var Result = 0;',
  3775. ' try {',
  3776. ' Result = 3;',
  3777. ' return Result;',
  3778. ' } finally {',
  3779. ' if (Result > 0) ;',
  3780. ' };',
  3781. ' return Result;',
  3782. '};',
  3783. 'this.Jump = function () {',
  3784. ' var Result = 0;',
  3785. ' try {',
  3786. ' try {',
  3787. ' Result = 4;',
  3788. ' return Result;',
  3789. ' } finally {',
  3790. ' };',
  3791. ' } finally {',
  3792. ' if (Result > 0) ;',
  3793. ' };',
  3794. ' return Result;',
  3795. '};',
  3796. '']),
  3797. LinesToStr([
  3798. '']));
  3799. end;
  3800. procedure TTestModule.TestBreak;
  3801. begin
  3802. StartProgram(false);
  3803. Add([
  3804. 'var',
  3805. ' i: longint;',
  3806. 'begin',
  3807. ' repeat',
  3808. ' break;',
  3809. ' until true;',
  3810. ' while true do',
  3811. ' break;',
  3812. ' for i:=1 to 2 do',
  3813. ' break;']);
  3814. ConvertProgram;
  3815. CheckSource('TestBreak',
  3816. LinesToStr([ // statements
  3817. 'this.i = 0;'
  3818. ]),
  3819. LinesToStr([
  3820. 'do {',
  3821. ' break;',
  3822. '} while (!true);',
  3823. 'while (true) break;',
  3824. 'for ($mod.i = 1; $mod.i <= 2; $mod.i++) break;',
  3825. '']));
  3826. end;
  3827. procedure TTestModule.TestBreakAsVar;
  3828. begin
  3829. StartProgram(false);
  3830. Add([
  3831. 'procedure DoIt(break: boolean);',
  3832. 'begin',
  3833. ' if break then ;',
  3834. 'end;',
  3835. 'var',
  3836. ' break: boolean;',
  3837. 'begin',
  3838. ' if break then ;']);
  3839. ConvertProgram;
  3840. CheckSource('TestBreakAsVar',
  3841. LinesToStr([ // statements
  3842. 'this.DoIt = function (Break) {',
  3843. ' if (Break) ;',
  3844. '};',
  3845. 'this.Break = false;',
  3846. '']),
  3847. LinesToStr([
  3848. 'if($mod.Break) ;',
  3849. '']));
  3850. end;
  3851. procedure TTestModule.TestContinue;
  3852. begin
  3853. StartProgram(false);
  3854. Add('var i: longint;');
  3855. Add('begin');
  3856. Add(' repeat');
  3857. Add(' continue;');
  3858. Add(' until true;');
  3859. Add(' while true do');
  3860. Add(' continue;');
  3861. Add(' for i:=1 to 2 do');
  3862. Add(' continue;');
  3863. ConvertProgram;
  3864. CheckSource('TestContinue',
  3865. LinesToStr([ // statements
  3866. 'this.i = 0;'
  3867. ]),
  3868. LinesToStr([
  3869. 'do {',
  3870. ' continue;',
  3871. '} while (!true);',
  3872. 'while (true) continue;',
  3873. 'for ($mod.i = 1; $mod.i <= 2; $mod.i++) continue;',
  3874. '']));
  3875. end;
  3876. procedure TTestModule.TestProc_External;
  3877. begin
  3878. StartProgram(false);
  3879. Add('procedure Foo; external name ''console.log'';');
  3880. Add('function Bar: longint; external name ''get.item'';');
  3881. Add('function Bla(s: string): longint; external name ''apply.something'';');
  3882. Add('var');
  3883. Add(' i: longint;');
  3884. Add('begin');
  3885. Add(' Foo;');
  3886. Add(' i:=Bar;');
  3887. Add(' i:=Bla(''abc'');');
  3888. ConvertProgram;
  3889. CheckSource('TestProc_External',
  3890. LinesToStr([ // statements
  3891. 'this.i = 0;'
  3892. ]),
  3893. LinesToStr([
  3894. 'console.log();',
  3895. '$mod.i = get.item();',
  3896. '$mod.i = apply.something("abc");'
  3897. ]));
  3898. end;
  3899. procedure TTestModule.TestProc_ExternalOtherUnit;
  3900. begin
  3901. AddModuleWithIntfImplSrc('unit2.pas',
  3902. LinesToStr([
  3903. 'procedure Now; external name ''Date.now'';',
  3904. 'procedure DoIt;'
  3905. ]),
  3906. 'procedure doit; begin end;');
  3907. StartUnit(true);
  3908. Add('interface');
  3909. Add('uses unit2;');
  3910. Add('implementation');
  3911. Add('begin');
  3912. Add(' now;');
  3913. Add(' now();');
  3914. Add(' uNit2.now;');
  3915. Add(' uNit2.now();');
  3916. Add(' doit;');
  3917. Add(' uNit2.doit;');
  3918. ConvertUnit;
  3919. CheckSource('TestProc_ExternalOtherUnit',
  3920. LinesToStr([
  3921. '']),
  3922. LinesToStr([
  3923. 'Date.now();',
  3924. 'Date.now();',
  3925. 'Date.now();',
  3926. 'Date.now();',
  3927. 'pas.unit2.DoIt();',
  3928. 'pas.unit2.DoIt();',
  3929. '']));
  3930. end;
  3931. procedure TTestModule.TestProc_Asm;
  3932. begin
  3933. StartProgram(false);
  3934. Add([
  3935. '{$mode delphi}',
  3936. 'function DoIt: longint;',
  3937. 'begin;',
  3938. ' asm',
  3939. ' { a:{ b:{}, c:[]}, d:''1'' };',
  3940. ' end;',
  3941. ' asm console.log(); end;',
  3942. ' asm',
  3943. ' s = "'' ";',
  3944. ' s = ''" '';',
  3945. ' s = s + "world" + "''";',
  3946. ' // end',
  3947. ' s = ''end'';',
  3948. ' s = "end";',
  3949. ' s = "foo\"bar";',
  3950. ' s = ''a\''b'';',
  3951. ' s = `${expr}\`-"-''-`;',
  3952. ' s = `multi',
  3953. 'line`;',
  3954. ' end;',
  3955. 'end;',
  3956. 'procedure Fly;',
  3957. 'asm',
  3958. ' return;',
  3959. 'end;',
  3960. 'begin']);
  3961. ConvertProgram;
  3962. CheckSource('TestProc_Asm',
  3963. LinesToStr([ // statements
  3964. 'this.DoIt = function () {',
  3965. ' var Result = 0;',
  3966. ' { a:{ b:{}, c:[]}, d:''1'' };',
  3967. ' console.log();',
  3968. ' s = "'' ";',
  3969. ' s = ''" '';',
  3970. ' s = s + "world" + "''";',
  3971. ' // end',
  3972. ' s = ''end'';',
  3973. ' s = "end";',
  3974. ' s = "foo\"bar";',
  3975. ' s = ''a\''b'';',
  3976. ' s = `${expr}\`-"-''-`;',
  3977. ' s = `multi',
  3978. 'line`;',
  3979. ' return Result;',
  3980. '};',
  3981. 'this.Fly = function () {',
  3982. ' return;',
  3983. '};',
  3984. '']),
  3985. LinesToStr([
  3986. ''
  3987. ]));
  3988. end;
  3989. procedure TTestModule.TestProc_AsmSubBlock;
  3990. begin
  3991. StartProgram(true,[supTObject]);
  3992. Add([
  3993. '{$mode delphi}',
  3994. 'type',
  3995. ' TBird = class end;',
  3996. 'procedure Run(w: word);',
  3997. 'begin;',
  3998. ' if true then asm console.log(); end;',
  3999. ' if w>3 then asm',
  4000. ' var a = w+1;',
  4001. ' w = a+3;',
  4002. ' end;',
  4003. ' while (w>7) do asm',
  4004. ' w+=3; w*=2;',
  4005. ' end;',
  4006. ' try',
  4007. ' except',
  4008. ' on E: TBird do',
  4009. ' asm console.log(E); end;',
  4010. ' on E: TObject do',
  4011. ' asm var i=3; i--; end;',
  4012. ' else asm Fly; High; end;',
  4013. ' end;',
  4014. 'end;',
  4015. 'begin']);
  4016. ConvertProgram;
  4017. CheckSource('TestProc_AsmSubBlock',
  4018. LinesToStr([ // statements
  4019. 'rtl.createClass(this, "TBird", pas.system.TObject, function () {',
  4020. '});',
  4021. 'this.Run = function (w) {',
  4022. ' if (true) console.log();',
  4023. ' if (w > 3) {',
  4024. ' var a = w+1;',
  4025. ' w = a+3;',
  4026. ' };',
  4027. ' while (w > 7) {',
  4028. ' w+=3; w*=2;',
  4029. ' };',
  4030. ' try {} catch ($e) {',
  4031. ' if ($mod.TBird.isPrototypeOf($e)) {',
  4032. ' var E = $e;',
  4033. ' console.log(E);',
  4034. ' } else if (pas.system.TObject.isPrototypeOf($e)) {',
  4035. ' var E = $e;',
  4036. ' var i=3; i--;',
  4037. ' } else {',
  4038. ' Fly; High;',
  4039. ' }',
  4040. ' };',
  4041. '};',
  4042. '']),
  4043. LinesToStr([
  4044. ''
  4045. ]));
  4046. end;
  4047. procedure TTestModule.TestProc_Assembler;
  4048. begin
  4049. StartProgram(false);
  4050. Add('function DoIt: longint; assembler;');
  4051. Add('asm');
  4052. Add('{ a:{ b:{}, c:[]}, d:''1'' };');
  4053. Add('end;');
  4054. Add('begin');
  4055. ConvertProgram;
  4056. CheckSource('TestProc_Assembler',
  4057. LinesToStr([ // statements
  4058. 'this.DoIt = function () {',
  4059. ' { a:{ b:{}, c:[]}, d:''1'' };',
  4060. '};'
  4061. ]),
  4062. LinesToStr([
  4063. ''
  4064. ]));
  4065. end;
  4066. procedure TTestModule.TestProc_VarParam;
  4067. begin
  4068. StartProgram(false);
  4069. Add('type integer = longint;');
  4070. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  4071. Add('var vJ: integer;');
  4072. Add('begin');
  4073. Add(' vg:=vg+1;');
  4074. Add(' vj:=vh+2;');
  4075. Add(' vi:=vi+3;');
  4076. Add(' doit(vg,vg,vg);');
  4077. Add(' doit(vh,vh,vj);');
  4078. Add(' doit(vi,vi,vi);');
  4079. Add(' doit(vj,vj,vj);');
  4080. Add('end;');
  4081. Add('var i: integer;');
  4082. Add('begin');
  4083. Add(' doit(i,i,i);');
  4084. ConvertProgram;
  4085. CheckSource('TestProc_VarParam',
  4086. LinesToStr([ // statements
  4087. 'this.DoIt = function (vG,vH,vI) {',
  4088. ' var vJ = 0;',
  4089. ' vG = vG + 1;',
  4090. ' vJ = vH + 2;',
  4091. ' vI.set(vI.get()+3);',
  4092. ' $mod.DoIt(vG, vG, {',
  4093. ' get: function () {',
  4094. ' return vG;',
  4095. ' },',
  4096. ' set: function (v) {',
  4097. ' vG = v;',
  4098. ' }',
  4099. ' });',
  4100. ' $mod.DoIt(vH, vH, {',
  4101. ' get: function () {',
  4102. ' return vJ;',
  4103. ' },',
  4104. ' set: function (v) {',
  4105. ' vJ = v;',
  4106. ' }',
  4107. ' });',
  4108. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  4109. ' $mod.DoIt(vJ, vJ, {',
  4110. ' get: function () {',
  4111. ' return vJ;',
  4112. ' },',
  4113. ' set: function (v) {',
  4114. ' vJ = v;',
  4115. ' }',
  4116. ' });',
  4117. '};',
  4118. 'this.i = 0;'
  4119. ]),
  4120. LinesToStr([
  4121. '$mod.DoIt($mod.i,$mod.i,{',
  4122. ' p: $mod,',
  4123. ' get: function () {',
  4124. ' return this.p.i;',
  4125. ' },',
  4126. ' set: function (v) {',
  4127. ' this.p.i = v;',
  4128. ' }',
  4129. '});'
  4130. ]));
  4131. end;
  4132. procedure TTestModule.TestProc_VarParamString;
  4133. begin
  4134. StartProgram(false);
  4135. Add(['type TCaption = string;',
  4136. 'procedure DoIt(vA: TCaption; var vB: TCaption; out vC: TCaption);',
  4137. 'var c: char;',
  4138. 'begin',
  4139. ' va[1]:=c;',
  4140. ' vb[2]:=c;',
  4141. ' vc[3]:=c;',
  4142. 'end;',
  4143. 'begin']);
  4144. ConvertProgram;
  4145. CheckSource('TestProc_VarParamString',
  4146. LinesToStr([ // statements
  4147. 'this.DoIt = function (vA,vB,vC) {',
  4148. ' var c = "";',
  4149. ' vA = rtl.setCharAt(vA, 0, c);',
  4150. ' vB.set(rtl.setCharAt(vB.get(), 1, c));',
  4151. ' vC.set(rtl.setCharAt(vC.get(), 2, c));',
  4152. '};',
  4153. '']),
  4154. LinesToStr([
  4155. ]));
  4156. end;
  4157. procedure TTestModule.TestProc_VarParamV;
  4158. begin
  4159. StartProgram(false);
  4160. Add([
  4161. 'procedure Inc2(var i: longint);',
  4162. 'begin',
  4163. ' i:=i+2;',
  4164. 'end;',
  4165. 'procedure DoIt(v: longint);',
  4166. 'var p: array of longint;',
  4167. 'begin',
  4168. ' Inc2(v);',
  4169. ' Inc2(p[v]);',
  4170. 'end;',
  4171. 'begin']);
  4172. ConvertProgram;
  4173. CheckSource('TestProc_VarParamV',
  4174. LinesToStr([ // statements
  4175. 'this.Inc2 = function (i) {',
  4176. ' i.set(i.get()+2);',
  4177. '};',
  4178. 'this.DoIt = function (v) {',
  4179. ' var p = [];',
  4180. ' $mod.Inc2({get: function () {',
  4181. ' return v;',
  4182. ' }, set: function (w) {',
  4183. ' v = w;',
  4184. ' }});',
  4185. ' $mod.Inc2({',
  4186. ' a: v,',
  4187. ' p: p,',
  4188. ' get: function () {',
  4189. ' return this.p[this.a];',
  4190. ' },',
  4191. ' set: function (v) {',
  4192. ' this.p[this.a] = v;',
  4193. ' }',
  4194. ' });',
  4195. '};',
  4196. '']),
  4197. LinesToStr([
  4198. '']));
  4199. end;
  4200. procedure TTestModule.TestProc_Overload;
  4201. begin
  4202. StartProgram(false);
  4203. Add('procedure DoIt(vI: longint); begin end;');
  4204. Add('procedure DoIt(vI, vJ: longint); begin end;');
  4205. Add('procedure DoIt(vD: double); begin end;');
  4206. Add('begin');
  4207. Add(' DoIt(1);');
  4208. Add(' DoIt(2,3);');
  4209. Add(' DoIt(4.5);');
  4210. ConvertProgram;
  4211. CheckSource('TestProcedureOverload',
  4212. LinesToStr([ // statements
  4213. 'this.DoIt = function (vI) {',
  4214. '};',
  4215. 'this.DoIt$1 = function (vI, vJ) {',
  4216. '};',
  4217. 'this.DoIt$2 = function (vD) {',
  4218. '};',
  4219. '']),
  4220. LinesToStr([
  4221. '$mod.DoIt(1);',
  4222. '$mod.DoIt$1(2, 3);',
  4223. '$mod.DoIt$2(4.5);',
  4224. '']));
  4225. end;
  4226. procedure TTestModule.TestProc_OverloadForward;
  4227. begin
  4228. StartProgram(false);
  4229. Add('procedure DoIt(vI: longint); forward;');
  4230. Add('procedure DoIt(vI, vJ: longint); begin end;');
  4231. Add('procedure doit(vi: longint); begin end;');
  4232. Add('begin');
  4233. Add(' doit(1);');
  4234. Add(' doit(2,3);');
  4235. ConvertProgram;
  4236. CheckSource('TestProcedureOverloadForward',
  4237. LinesToStr([ // statements
  4238. 'this.DoIt$1 = function (vI, vJ) {',
  4239. '};',
  4240. 'this.DoIt = function (vI) {',
  4241. '};',
  4242. '']),
  4243. LinesToStr([
  4244. '$mod.DoIt(1);',
  4245. '$mod.DoIt$1(2, 3);',
  4246. '']));
  4247. end;
  4248. procedure TTestModule.TestProc_OverloadIntfImpl;
  4249. begin
  4250. StartUnit(false);
  4251. Add('interface');
  4252. Add('procedure DoIt(vI: longint);');
  4253. Add('procedure DoIt(vI, vJ: longint);');
  4254. Add('implementation');
  4255. Add('procedure DoIt(vI, vJ, vK, vL, vM: longint); forward;');
  4256. Add('procedure DoIt(vI, vJ, vK: longint); begin end;');
  4257. Add('procedure DoIt(vi: longint); begin end;');
  4258. Add('procedure DoIt(vI, vJ, vK, vL: longint); begin end;');
  4259. Add('procedure DoIt(vi, vj: longint); begin end;');
  4260. Add('procedure DoIt(vi, vj, vk, vl, vm: longint); begin end;');
  4261. Add('begin');
  4262. Add(' doit(1);');
  4263. Add(' doit(2,3);');
  4264. Add(' doit(4,5,6);');
  4265. Add(' doit(7,8,9,10);');
  4266. Add(' doit(11,12,13,14,15);');
  4267. ConvertUnit;
  4268. CheckSource('TestProcedureOverloadUnit',
  4269. LinesToStr([ // statements
  4270. 'var $impl = $mod.$impl;',
  4271. 'this.DoIt = function (vI) {',
  4272. '};',
  4273. 'this.DoIt$1 = function (vI, vJ) {',
  4274. '};',
  4275. '']),
  4276. LinesToStr([ // this.$init
  4277. '$mod.DoIt(1);',
  4278. '$mod.DoIt$1(2, 3);',
  4279. '$impl.DoIt$3(4,5,6);',
  4280. '$impl.DoIt$4(7,8,9,10);',
  4281. '$impl.DoIt$2(11,12,13,14,15);',
  4282. '']),
  4283. LinesToStr([ // implementation
  4284. '$impl.DoIt$3 = function (vI, vJ, vK) {',
  4285. '};',
  4286. '$impl.DoIt$4 = function (vI, vJ, vK, vL) {',
  4287. '};',
  4288. '$impl.DoIt$2 = function (vI, vJ, vK, vL, vM) {',
  4289. '};',
  4290. '']));
  4291. end;
  4292. procedure TTestModule.TestProc_OverloadNested;
  4293. begin
  4294. StartProgram(false);
  4295. Add([
  4296. 'procedure doit(vA: longint);',
  4297. ' procedure DoIt(vA, vB: longint); overload;',
  4298. ' begin',
  4299. ' doit(1);',
  4300. ' doit(1,2);',
  4301. ' end;',
  4302. ' procedure doit(vA, vB, vC: longint);',
  4303. ' begin',
  4304. ' doit(1);',
  4305. ' doit(1,2);',
  4306. ' doit(1,2,3);',
  4307. ' end;',
  4308. 'begin',
  4309. ' doit(1);',
  4310. ' doit(1,2);',
  4311. ' doit(1,2,3);',
  4312. 'end;',
  4313. 'begin // main',
  4314. ' doit(1);']);
  4315. ConvertProgram;
  4316. CheckSource('TestProcedureOverloadNested',
  4317. LinesToStr([ // statements
  4318. 'this.doit = function (vA) {',
  4319. ' function DoIt$1(vA, vB) {',
  4320. ' $mod.doit(1);',
  4321. ' DoIt$1(1, 2);',
  4322. ' };',
  4323. ' function doit$2(vA, vB, vC) {',
  4324. ' $mod.doit(1);',
  4325. ' DoIt$1(1, 2);',
  4326. ' doit$2(1, 2, 3);',
  4327. ' };',
  4328. ' $mod.doit(1);',
  4329. ' DoIt$1(1, 2);',
  4330. ' doit$2(1, 2, 3);',
  4331. '};',
  4332. '']),
  4333. LinesToStr([
  4334. '$mod.doit(1);',
  4335. '']));
  4336. end;
  4337. procedure TTestModule.TestProc_OverloadNestedForward;
  4338. begin
  4339. StartProgram(false);
  4340. Add([
  4341. 'procedure DoIt(vA: longint); overload; forward;',
  4342. 'procedure DoIt(vB, vC: longint); overload;',
  4343. 'begin // 2 param overload',
  4344. ' doit(1);',
  4345. ' doit(1,2);',
  4346. 'end;',
  4347. 'procedure doit(vA: longint);',
  4348. ' procedure DoIt(vA, vB, vC: longint); overload; forward;',
  4349. ' procedure DoIt(vA, vB, vC, vD: longint); overload;',
  4350. ' begin // 4 param overload',
  4351. ' doit(1);',
  4352. ' doit(1,2);',
  4353. ' doit(1,2,3);',
  4354. ' doit(1,2,3,4);',
  4355. ' end;',
  4356. ' procedure doit(vA, vB, vC: longint);',
  4357. ' procedure DoIt(vA, vB, vC, vD, vE: longint); overload; forward;',
  4358. ' procedure DoIt(vA, vB, vC, vD, vE, vF: longint); overload;',
  4359. ' begin // 6 param overload',
  4360. ' doit(1);',
  4361. ' doit(1,2);',
  4362. ' doit(1,2,3);',
  4363. ' doit(1,2,3,4);',
  4364. ' doit(1,2,3,4,5);',
  4365. ' doit(1,2,3,4,5,6);',
  4366. ' end;',
  4367. ' procedure doit(vA, vB, vC, vD, vE: longint);',
  4368. ' begin // 5 param overload',
  4369. ' doit(1);',
  4370. ' doit(1,2);',
  4371. ' doit(1,2,3);',
  4372. ' doit(1,2,3,4);',
  4373. ' doit(1,2,3,4,5);',
  4374. ' doit(1,2,3,4,5,6);',
  4375. ' end;',
  4376. ' begin // 3 param overload',
  4377. ' doit(1);',
  4378. ' doit(1,2);',
  4379. ' doit(1,2,3);',
  4380. ' doit(1,2,3,4);',
  4381. ' doit(1,2,3,4,5);',
  4382. ' doit(1,2,3,4,5,6);',
  4383. ' end;',
  4384. 'begin // 1 param overload',
  4385. ' doit(1);',
  4386. ' doit(1,2);',
  4387. ' doit(1,2,3);',
  4388. ' doit(1,2,3,4);',
  4389. 'end;',
  4390. 'begin // main',
  4391. ' doit(1);',
  4392. ' doit(1,2);']);
  4393. ConvertProgram;
  4394. CheckSource('TestProc_OverloadNestedForward',
  4395. LinesToStr([ // statements
  4396. 'this.DoIt$1 = function (vB, vC) {',
  4397. ' $mod.DoIt(1);',
  4398. ' $mod.DoIt$1(1, 2);',
  4399. '};',
  4400. 'this.DoIt = function (vA) {',
  4401. ' function DoIt$3(vA, vB, vC, vD) {',
  4402. ' $mod.DoIt(1);',
  4403. ' $mod.DoIt$1(1, 2);',
  4404. ' DoIt$2(1, 2, 3);',
  4405. ' DoIt$3(1, 2, 3, 4);',
  4406. ' };',
  4407. ' function DoIt$2(vA, vB, vC) {',
  4408. ' function DoIt$5(vA, vB, vC, vD, vE, vF) {',
  4409. ' $mod.DoIt(1);',
  4410. ' $mod.DoIt$1(1, 2);',
  4411. ' DoIt$2(1, 2, 3);',
  4412. ' DoIt$3(1, 2, 3, 4);',
  4413. ' DoIt$4(1, 2, 3, 4, 5);',
  4414. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  4415. ' };',
  4416. ' function DoIt$4(vA, vB, vC, vD, vE) {',
  4417. ' $mod.DoIt(1);',
  4418. ' $mod.DoIt$1(1, 2);',
  4419. ' DoIt$2(1, 2, 3);',
  4420. ' DoIt$3(1, 2, 3, 4);',
  4421. ' DoIt$4(1, 2, 3, 4, 5);',
  4422. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  4423. ' };',
  4424. ' $mod.DoIt(1);',
  4425. ' $mod.DoIt$1(1, 2);',
  4426. ' DoIt$2(1, 2, 3);',
  4427. ' DoIt$3(1, 2, 3, 4);',
  4428. ' DoIt$4(1, 2, 3, 4, 5);',
  4429. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  4430. ' };',
  4431. ' $mod.DoIt(1);',
  4432. ' $mod.DoIt$1(1, 2);',
  4433. ' DoIt$2(1, 2, 3);',
  4434. ' DoIt$3(1, 2, 3, 4);',
  4435. '};',
  4436. '']),
  4437. LinesToStr([
  4438. '$mod.DoIt(1);',
  4439. '$mod.DoIt$1(1, 2);',
  4440. '']));
  4441. end;
  4442. procedure TTestModule.TestProc_OverloadUnitCycle;
  4443. begin
  4444. AddModuleWithIntfImplSrc('Unit2.pas',
  4445. LinesToStr([
  4446. 'type',
  4447. ' TObject = class',
  4448. ' procedure DoIt(b: boolean); virtual; abstract;',
  4449. ' procedure DoIt(i: longint); virtual; abstract;',
  4450. ' end;',
  4451. '']),
  4452. 'uses test1;');
  4453. StartUnit(true);
  4454. Add([
  4455. 'interface',
  4456. 'uses unit2;',
  4457. 'type',
  4458. ' TEagle = class(TObject)',
  4459. ' procedure DoIt(b: boolean); override;',
  4460. ' procedure DoIt(i: longint); override;',
  4461. ' end;',
  4462. 'implementation',
  4463. 'procedure TEagle.DoIt(b: boolean); begin end;',
  4464. 'procedure TEagle.DoIt(i: longint); begin end;',
  4465. '']);
  4466. ConvertUnit;
  4467. CheckSource('TestProc_OverloadUnitCycle',
  4468. LinesToStr([ // statements
  4469. 'rtl.createClass(this, "TEagle", pas.Unit2.TObject, function () {',
  4470. ' this.DoIt = function (b) {',
  4471. ' };',
  4472. ' this.DoIt$1 = function (i) {',
  4473. ' };',
  4474. '});',
  4475. '']),
  4476. '',
  4477. LinesToStr([
  4478. '']));
  4479. end;
  4480. procedure TTestModule.TestProc_Varargs;
  4481. begin
  4482. StartProgram(false);
  4483. Add([
  4484. 'procedure ProcA(i:longint); varargs; external name ''ProcA'';',
  4485. 'procedure ProcB; varargs; external name ''ProcB'';',
  4486. 'procedure ProcC(i: longint = 17); varargs; external name ''ProcC'';',
  4487. 'function GetIt: longint; begin end;',
  4488. 'begin',
  4489. ' ProcA(1);',
  4490. ' ProcA(1,2);',
  4491. ' ProcA(1,2.0);',
  4492. ' ProcA(1,2,3);',
  4493. ' ProcA(1,''2'');',
  4494. ' ProcA(2,'''');',
  4495. ' ProcA(3,false);',
  4496. ' ProcB;',
  4497. ' ProcB();',
  4498. ' ProcB(4);',
  4499. ' ProcB(''foo'');',
  4500. ' ProcC;',
  4501. ' ProcC();',
  4502. ' ProcC(4);',
  4503. ' ProcC(5,''foo'');',
  4504. ' ProcB(GetIt);',
  4505. ' ProcB(GetIt());',
  4506. ' ProcB(GetIt,GetIt());']);
  4507. ConvertProgram;
  4508. CheckSource('TestProc_Varargs',
  4509. LinesToStr([ // statements
  4510. 'this.GetIt = function () {',
  4511. ' var Result = 0;',
  4512. ' return Result;',
  4513. '};',
  4514. '']),
  4515. LinesToStr([
  4516. 'ProcA(1);',
  4517. 'ProcA(1, 2);',
  4518. 'ProcA(1, 2.0);',
  4519. 'ProcA(1, 2, 3);',
  4520. 'ProcA(1, "2");',
  4521. 'ProcA(2, "");',
  4522. 'ProcA(3, false);',
  4523. 'ProcB();',
  4524. 'ProcB();',
  4525. 'ProcB(4);',
  4526. 'ProcB("foo");',
  4527. 'ProcC(17);',
  4528. 'ProcC(17);',
  4529. 'ProcC(4);',
  4530. 'ProcC(5, "foo");',
  4531. 'ProcB($mod.GetIt());',
  4532. 'ProcB($mod.GetIt());',
  4533. 'ProcB($mod.GetIt(), $mod.GetIt());',
  4534. '']));
  4535. end;
  4536. procedure TTestModule.TestProc_ConstOrder;
  4537. begin
  4538. StartProgram(false);
  4539. Add([
  4540. 'const A = 3;',
  4541. 'const B = A+1;',
  4542. 'procedure DoIt;',
  4543. 'const C = A+1;',
  4544. 'const D = B+1;',
  4545. 'const E = D+C+B+A;',
  4546. 'begin',
  4547. 'end;',
  4548. 'begin'
  4549. ]);
  4550. ConvertProgram;
  4551. CheckSource('TestProc_ConstOrder',
  4552. LinesToStr([ // statements
  4553. 'this.A = 3;',
  4554. 'this.B = 3 + 1;',
  4555. 'var C = 3 + 1;',
  4556. 'var D = 4 + 1;',
  4557. 'var E = 5 + 4 + 4 + 3;',
  4558. 'this.DoIt = function () {',
  4559. '};',
  4560. '']),
  4561. LinesToStr([
  4562. ''
  4563. ]));
  4564. end;
  4565. procedure TTestModule.TestProc_DuplicateConst;
  4566. begin
  4567. StartProgram(false);
  4568. Add([
  4569. 'const A = 1;',
  4570. 'procedure DoIt;',
  4571. 'const A = 2;',
  4572. ' procedure SubIt;',
  4573. ' const A = 21;',
  4574. ' begin',
  4575. ' end;',
  4576. 'begin',
  4577. 'end;',
  4578. 'procedure DoSome;',
  4579. 'const A = 3;',
  4580. 'begin',
  4581. 'end;',
  4582. 'begin'
  4583. ]);
  4584. ConvertProgram;
  4585. CheckSource('TestProc_DuplicateConst',
  4586. LinesToStr([ // statements
  4587. 'this.A = 1;',
  4588. 'var A$1 = 2;',
  4589. 'var A$2 = 21;',
  4590. 'this.DoIt = function () {',
  4591. ' function SubIt() {',
  4592. ' };',
  4593. '};',
  4594. 'var A$3 = 3;',
  4595. 'this.DoSome = function () {',
  4596. '};',
  4597. '']),
  4598. LinesToStr([
  4599. ''
  4600. ]));
  4601. end;
  4602. procedure TTestModule.TestProc_LocalVarAbsolute;
  4603. begin
  4604. StartProgram(false);
  4605. Add([
  4606. 'type',
  4607. ' TObject = class',
  4608. ' Index: longint;',
  4609. ' procedure DoAbs(Item: pointer);',
  4610. ' end;',
  4611. 'procedure TObject.DoAbs(Item: pointer);',
  4612. 'var',
  4613. ' o: TObject absolute Item;',
  4614. 'begin',
  4615. ' if o.Index<o.Index then o.Index:=o.Index;',
  4616. 'end;',
  4617. 'procedure DoIt(i: longint; p: pointer);',
  4618. 'var',
  4619. ' d: double absolute i;',
  4620. ' s: string absolute d;',
  4621. ' oi: TObject absolute i;',
  4622. ' op: TObject absolute p;',
  4623. 'begin',
  4624. ' if d=d then d:=d;',
  4625. ' if s=s then s:=s;',
  4626. ' if oi.Index<oi.Index then oi.Index:=oi.Index;',
  4627. ' if op.Index=op.Index then op.Index:=op.Index;',
  4628. 'end;',
  4629. 'begin']);
  4630. ConvertProgram;
  4631. CheckSource('TestProc_LocalVarAbsolute',
  4632. LinesToStr([ // statements
  4633. 'rtl.createClass(this, "TObject", null, function () {',
  4634. ' this.$init = function () {',
  4635. ' this.Index = 0;',
  4636. ' };',
  4637. ' this.$final = function () {',
  4638. ' };',
  4639. ' this.DoAbs = function (Item) {',
  4640. ' if (Item.Index < Item.Index) Item.Index = Item.Index;',
  4641. ' };',
  4642. '});',
  4643. 'this.DoIt = function (i, p) {',
  4644. ' if (i === i) i = i;',
  4645. ' if (i === i) i = i;',
  4646. ' if (i.Index < i.Index) i.Index = i.Index;',
  4647. ' if (p.Index === p.Index) p.Index = p.Index;',
  4648. '};'
  4649. ]),
  4650. LinesToStr([
  4651. ]));
  4652. end;
  4653. procedure TTestModule.TestProc_LocalVarInit;
  4654. begin
  4655. StartProgram(false);
  4656. Add([
  4657. 'type TBytes = array of byte;',
  4658. 'procedure DoIt;',
  4659. 'const c = 4;',
  4660. 'var',
  4661. ' b: byte = 1;',
  4662. ' w: word = 2+c;',
  4663. ' p: pointer = nil;',
  4664. ' Buffer: TBytes = nil;',
  4665. 'begin',
  4666. 'end;',
  4667. 'begin']);
  4668. ConvertProgram;
  4669. CheckSource('TestProc_LocalVarInit',
  4670. LinesToStr([ // statements
  4671. 'var c = 4;',
  4672. 'this.DoIt = function () {',
  4673. ' var b = 1;',
  4674. ' var w = 2 + 4;',
  4675. ' var p = null;',
  4676. ' var Buffer = [];',
  4677. '};',
  4678. '']),
  4679. LinesToStr([
  4680. ]));
  4681. end;
  4682. procedure TTestModule.TestProc_ReservedWords;
  4683. begin
  4684. StartProgram(false);
  4685. Add([
  4686. 'procedure Date(ArrayBuffer: longint);',
  4687. 'const',
  4688. ' NaN: longint = 3;',
  4689. 'var',
  4690. ' &Boolean: longint;',
  4691. ' procedure Error(ArrayBuffer: longint);',
  4692. ' begin',
  4693. ' end;',
  4694. 'begin',
  4695. ' Nan:=&bOolean;',
  4696. 'end;',
  4697. 'begin',
  4698. ' Date(1);']);
  4699. ConvertProgram;
  4700. CheckSource('TestProc_ReservedWords',
  4701. LinesToStr([ // statements
  4702. 'var naN = 3;',
  4703. 'this.Date = function (arrayBuffer) {',
  4704. ' var boolean = 0;',
  4705. ' function error(arrayBuffer) {',
  4706. ' };',
  4707. ' naN = boolean;',
  4708. '};',
  4709. '']),
  4710. LinesToStr([
  4711. ' $mod.Date(1);'
  4712. ]));
  4713. end;
  4714. procedure TTestModule.TestProc_ConstRefWord;
  4715. begin
  4716. StartProgram(false);
  4717. Add([
  4718. 'procedure Run(constref w: word);',
  4719. 'var l: word;',
  4720. 'begin',
  4721. ' l:=w;',
  4722. ' Run(w);',
  4723. ' Run(l);',
  4724. 'end;',
  4725. 'procedure Fly(a: word; var b: word; out c: word; const d: word; constref e: word);',
  4726. 'begin',
  4727. ' Run(a);',
  4728. ' Run(b);',
  4729. ' Run(c);',
  4730. ' Run(d);',
  4731. ' Run(e);',
  4732. 'end;',
  4733. 'begin',
  4734. ' Run(1);']);
  4735. ConvertProgram;
  4736. CheckHint(mtWarning,nConstRefNotForXAsConst,'ConstRef not yet implemented for Word. Treating as Const');
  4737. CheckSource('TestProc_ConstRefWord',
  4738. LinesToStr([ // statements
  4739. 'this.Run = function (w) {',
  4740. ' var l = 0;',
  4741. ' l = w;',
  4742. ' $mod.Run(w);',
  4743. ' $mod.Run(l);',
  4744. '};',
  4745. 'this.Fly = function (a, b, c, d, e) {',
  4746. ' $mod.Run(a);',
  4747. ' $mod.Run(b.get());',
  4748. ' $mod.Run(c.get());',
  4749. ' $mod.Run(d);',
  4750. ' $mod.Run(e);',
  4751. '};',
  4752. '']),
  4753. LinesToStr([
  4754. '$mod.Run(1);'
  4755. ]));
  4756. end;
  4757. procedure TTestModule.TestAnonymousProc_Assign_ObjFPC;
  4758. begin
  4759. StartProgram(false);
  4760. Add([
  4761. '{$mode objfpc}',
  4762. 'type',
  4763. ' TFunc = reference to function(x: word): word;',
  4764. 'var Func: TFunc;',
  4765. 'procedure DoIt(a: word);',
  4766. 'begin',
  4767. ' Func:=function(b:word): word',
  4768. ' begin',
  4769. ' Result:=a+b;',
  4770. ' exit(b);',
  4771. ' exit(Result);',
  4772. ' end;',// test semicolon
  4773. ' a:=3;',
  4774. 'end;',
  4775. 'begin',
  4776. ' Func:=function(c:word):word begin',
  4777. ' Result:=3+c;',
  4778. ' exit(c);',
  4779. ' exit(Result);',
  4780. ' end;']);
  4781. ConvertProgram;
  4782. CheckSource('TestAnonymousProc_Assign_ObjFPC',
  4783. LinesToStr([ // statements
  4784. 'this.Func = null;',
  4785. 'this.DoIt = function (a) {',
  4786. ' $mod.Func = function (b) {',
  4787. ' var Result = 0;',
  4788. ' Result = a + b;',
  4789. ' return b;',
  4790. ' return Result;',
  4791. ' return Result;',
  4792. ' };',
  4793. ' a = 3;',
  4794. '};',
  4795. '']),
  4796. LinesToStr([
  4797. '$mod.Func = function (c) {',
  4798. ' var Result = 0;',
  4799. ' Result = 3 + c;',
  4800. ' return c;',
  4801. ' return Result;',
  4802. ' return Result;',
  4803. '};',
  4804. '']));
  4805. end;
  4806. procedure TTestModule.TestAnonymousProc_Assign_Delphi;
  4807. begin
  4808. StartProgram(false);
  4809. Add([
  4810. '{$mode delphi}',
  4811. 'type',
  4812. ' TProc = reference to procedure(x: word);',
  4813. 'procedure DoIt(a: word);',
  4814. 'var Proc: TProc;',
  4815. 'begin',
  4816. ' Proc:=procedure(b:word) begin end;',
  4817. 'end;',
  4818. 'var Proc: TProc;',
  4819. 'begin',
  4820. ' Proc:=procedure(c:word) begin end;',
  4821. '']);
  4822. ConvertProgram;
  4823. CheckSource('TestAnonymousProc_Assign_Delphi',
  4824. LinesToStr([ // statements
  4825. 'this.DoIt = function (a) {',
  4826. ' var Proc = null;',
  4827. ' Proc = function (b) {',
  4828. ' };',
  4829. '};',
  4830. 'this.Proc = null;',
  4831. '']),
  4832. LinesToStr([
  4833. '$mod.Proc = function (c) {',
  4834. '};',
  4835. '']));
  4836. end;
  4837. procedure TTestModule.TestAnonymousProc_Arg;
  4838. begin
  4839. StartProgram(false);
  4840. Add([
  4841. 'type',
  4842. ' TProc = reference to procedure;',
  4843. ' TFunc = reference to function(x: word): word;',
  4844. 'procedure DoMore(f,g: TProc);',
  4845. 'begin',
  4846. 'end;',
  4847. 'procedure DoOdd(v: jsvalue);',
  4848. 'begin',
  4849. 'end;',
  4850. 'procedure DoIt(f: TFunc);',
  4851. 'begin',
  4852. ' DoIt(function(b:word): word',
  4853. ' begin',
  4854. ' Result:=1+b;',
  4855. ' end);',
  4856. ' DoMore(procedure begin end, procedure begin end);',
  4857. ' DoOdd(procedure begin end);',
  4858. 'end;',
  4859. 'begin',
  4860. ' DoMore(procedure begin end,',
  4861. ' procedure assembler asm',
  4862. ' console.log("c");',
  4863. ' end);',
  4864. '']);
  4865. ConvertProgram;
  4866. CheckSource('TestAnonymousProc_Arg',
  4867. LinesToStr([ // statements
  4868. 'this.DoMore = function (f, g) {',
  4869. '};',
  4870. 'this.DoOdd = function (v) {',
  4871. '};',
  4872. 'this.DoIt = function (f) {',
  4873. ' $mod.DoIt(function (b) {',
  4874. ' var Result = 0;',
  4875. ' Result = 1 + b;',
  4876. ' return Result;',
  4877. ' });',
  4878. ' $mod.DoMore(function () {',
  4879. ' }, function () {',
  4880. ' });',
  4881. ' $mod.DoOdd(function () {',
  4882. ' });',
  4883. '};',
  4884. '']),
  4885. LinesToStr([
  4886. '$mod.DoMore(function () {',
  4887. '}, function () {',
  4888. ' console.log("c");',
  4889. '});',
  4890. '']));
  4891. end;
  4892. procedure TTestModule.TestAnonymousProc_Typecast;
  4893. begin
  4894. StartProgram(false);
  4895. Add([
  4896. 'type',
  4897. ' TProc = reference to procedure(w: word);',
  4898. ' TArr = array of word;',
  4899. ' TFuncArr = reference to function: TArr;',
  4900. 'procedure DoIt(p: TProc);',
  4901. 'var',
  4902. ' w: word;',
  4903. ' a: TArr;',
  4904. 'begin',
  4905. ' p:=TProc(procedure(b: smallint) begin end);',
  4906. ' a:=TFuncArr(function: TArr begin end)();',
  4907. ' w:=TFuncArr(function: TArr begin end)()[3];',
  4908. 'end;',
  4909. 'begin']);
  4910. ConvertProgram;
  4911. CheckSource('TestAnonymousProc_Typecast',
  4912. LinesToStr([ // statements
  4913. 'this.DoIt = function (p) {',
  4914. ' var w = 0;',
  4915. ' var a = [];',
  4916. ' p = function (b) {',
  4917. ' };',
  4918. ' a = function () {',
  4919. ' var Result = [];',
  4920. ' return Result;',
  4921. ' }();',
  4922. ' w = function () {',
  4923. ' var Result = [];',
  4924. ' return Result;',
  4925. ' }()[3];',
  4926. '};',
  4927. '']),
  4928. LinesToStr([
  4929. '']));
  4930. end;
  4931. procedure TTestModule.TestAnonymousProc_With;
  4932. begin
  4933. StartProgram(false);
  4934. Add([
  4935. 'type',
  4936. ' TProc = reference to procedure(w: word);',
  4937. ' TObject = class',
  4938. ' b: boolean;',
  4939. ' end;',
  4940. 'var',
  4941. ' p: TProc;',
  4942. ' bird: TObject;',
  4943. 'begin',
  4944. ' with bird do',
  4945. ' p:=procedure(w: word)',
  4946. ' begin',
  4947. ' b:=w>2;',
  4948. ' end;',
  4949. '']);
  4950. ConvertProgram;
  4951. CheckSource('TestAnonymousProc_With',
  4952. LinesToStr([ // statements
  4953. 'rtl.createClass(this, "TObject", null, function () {',
  4954. ' this.$init = function () {',
  4955. ' this.b = false;',
  4956. ' };',
  4957. ' this.$final = function () {',
  4958. ' };',
  4959. '});',
  4960. 'this.p = null;',
  4961. 'this.bird = null;',
  4962. '']),
  4963. LinesToStr([
  4964. 'var $with = $mod.bird;',
  4965. '$mod.p = function (w) {',
  4966. ' $with.b = w > 2;',
  4967. '};',
  4968. '']));
  4969. end;
  4970. procedure TTestModule.TestAnonymousProc_ExceptOn;
  4971. begin
  4972. StartProgram(false);
  4973. Add([
  4974. 'type',
  4975. ' TProc = reference to procedure;',
  4976. ' TObject = class',
  4977. ' b: boolean;',
  4978. ' end;',
  4979. 'procedure DoIt;',
  4980. 'var',
  4981. ' p: TProc;',
  4982. 'begin',
  4983. ' try',
  4984. ' except',
  4985. ' on E: TObject do',
  4986. ' p:=procedure',
  4987. ' begin',
  4988. ' E.b:=true;',
  4989. ' end;',
  4990. ' end;',
  4991. 'end;',
  4992. 'begin']);
  4993. ConvertProgram;
  4994. CheckSource('TestAnonymousProc_ExceptOn',
  4995. LinesToStr([ // statements
  4996. 'rtl.createClass(this, "TObject", null, function () {',
  4997. ' this.$init = function () {',
  4998. ' this.b = false;',
  4999. ' };',
  5000. ' this.$final = function () {',
  5001. ' };',
  5002. '});',
  5003. 'this.DoIt = function () {',
  5004. ' var p = null;',
  5005. ' try {} catch ($e) {',
  5006. ' if ($mod.TObject.isPrototypeOf($e)) {',
  5007. ' var E = $e;',
  5008. ' p = function () {',
  5009. ' E.b = true;',
  5010. ' };',
  5011. ' } else throw $e',
  5012. ' };',
  5013. '};',
  5014. '']),
  5015. LinesToStr([
  5016. '']));
  5017. end;
  5018. procedure TTestModule.TestAnonymousProc_Nested;
  5019. begin
  5020. StartProgram(false);
  5021. Add([
  5022. 'type',
  5023. ' TProc = reference to procedure;',
  5024. ' TObject = class',
  5025. ' i: byte;',
  5026. ' procedure DoIt;',
  5027. ' end;',
  5028. 'procedure TObject.DoIt;',
  5029. 'var',
  5030. ' p: TProc;',
  5031. ' procedure Sub;',
  5032. ' begin',
  5033. ' p:=procedure',
  5034. ' begin',
  5035. ' i:=3;',
  5036. ' Self.i:=4;',
  5037. ' p:=procedure',
  5038. ' procedure SubSub;',
  5039. ' begin',
  5040. ' i:=13;',
  5041. ' Self.i:=14;',
  5042. ' end;',
  5043. ' begin',
  5044. ' i:=13;',
  5045. ' Self.i:=14;',
  5046. ' end;',
  5047. ' end;',
  5048. ' end;',
  5049. 'begin',
  5050. 'end;',
  5051. 'begin']);
  5052. ConvertProgram;
  5053. CheckSource('TestAnonymousProc_Nested',
  5054. LinesToStr([ // statements
  5055. 'rtl.createClass(this, "TObject", null, function () {',
  5056. ' this.$init = function () {',
  5057. ' this.i = 0;',
  5058. ' };',
  5059. ' this.$final = function () {',
  5060. ' };',
  5061. ' this.DoIt = function () {',
  5062. ' var $Self = this;',
  5063. ' var p = null;',
  5064. ' function Sub() {',
  5065. ' p = function () {',
  5066. ' $Self.i = 3;',
  5067. ' $Self.i = 4;',
  5068. ' p = function () {',
  5069. ' function SubSub() {',
  5070. ' $Self.i = 13;',
  5071. ' $Self.i = 14;',
  5072. ' };',
  5073. ' $Self.i = 13;',
  5074. ' $Self.i = 14;',
  5075. ' };',
  5076. ' };',
  5077. ' };',
  5078. ' };',
  5079. '});',
  5080. '']),
  5081. LinesToStr([
  5082. '']));
  5083. end;
  5084. procedure TTestModule.TestAnonymousProc_NestedAssignResult;
  5085. begin
  5086. StartProgram(false);
  5087. Add([
  5088. 'type',
  5089. ' TProc = reference to procedure;',
  5090. 'function DoIt: TProc;',
  5091. ' function Sub: TProc;',
  5092. ' begin',
  5093. ' Result:=procedure',
  5094. ' begin',
  5095. ' Sub:=procedure',
  5096. ' procedure SubSub;',
  5097. ' begin',
  5098. ' Result:=nil;',
  5099. ' Sub:=nil;',
  5100. ' DoIt:=nil;',
  5101. ' end;',
  5102. ' begin',
  5103. ' Result:=nil;',
  5104. ' Sub:=nil;',
  5105. ' DoIt:=nil;',
  5106. ' end;',
  5107. ' end;',
  5108. ' end;',
  5109. 'begin',
  5110. 'end;',
  5111. 'begin']);
  5112. ConvertProgram;
  5113. CheckSource('TestAnonymousProc_NestedAssignResult',
  5114. LinesToStr([ // statements
  5115. 'this.DoIt = function () {',
  5116. ' var Result = null;',
  5117. ' function Sub() {',
  5118. ' var Result$1 = null;',
  5119. ' Result$1 = function () {',
  5120. ' Result$1 = function () {',
  5121. ' function SubSub() {',
  5122. ' Result$1 = null;',
  5123. ' Result$1 = null;',
  5124. ' Result = null;',
  5125. ' };',
  5126. ' Result$1 = null;',
  5127. ' Result$1 = null;',
  5128. ' Result = null;',
  5129. ' };',
  5130. ' };',
  5131. ' return Result$1;',
  5132. ' };',
  5133. ' return Result;',
  5134. '};',
  5135. '']),
  5136. LinesToStr([
  5137. '']));
  5138. end;
  5139. procedure TTestModule.TestAnonymousProc_Class;
  5140. begin
  5141. StartProgram(false);
  5142. Add([
  5143. 'type',
  5144. ' TProc = reference to procedure;',
  5145. ' TEvent = procedure of object;',
  5146. ' TObject = class',
  5147. ' Size: word;',
  5148. ' function GetIt: TProc;',
  5149. ' procedure DoIt; virtual; abstract;',
  5150. ' end;',
  5151. 'function TObject.GetIt: TProc;',
  5152. 'begin',
  5153. ' Result:=procedure',
  5154. ' var p: TEvent;',
  5155. ' begin',
  5156. ' Size:=Size;',
  5157. ' Size:=Self.Size;',
  5158. ' p:=@DoIt;',
  5159. ' p:[email protected];',
  5160. ' end;',
  5161. 'end;',
  5162. 'begin']);
  5163. ConvertProgram;
  5164. CheckSource('TestAnonymousProc_Class',
  5165. LinesToStr([ // statements
  5166. 'rtl.createClass(this, "TObject", null, function () {',
  5167. ' this.$init = function () {',
  5168. ' this.Size = 0;',
  5169. ' };',
  5170. ' this.$final = function () {',
  5171. ' };',
  5172. ' this.GetIt = function () {',
  5173. ' var $Self = this;',
  5174. ' var Result = null;',
  5175. ' Result = function () {',
  5176. ' var p = null;',
  5177. ' $Self.Size = $Self.Size;',
  5178. ' $Self.Size = $Self.Size;',
  5179. ' p = rtl.createCallback($Self, "DoIt");',
  5180. ' p = rtl.createCallback($Self, "DoIt");',
  5181. ' };',
  5182. ' return Result;',
  5183. ' };',
  5184. '});',
  5185. '']),
  5186. LinesToStr([
  5187. '']));
  5188. end;
  5189. procedure TTestModule.TestAnonymousProc_ForLoop;
  5190. begin
  5191. StartProgram(false);
  5192. Add([
  5193. 'type TProc = reference to procedure;',
  5194. 'procedure Foo(p: TProc);',
  5195. 'begin',
  5196. 'end;',
  5197. 'procedure DoIt;',
  5198. 'var i: word;',
  5199. ' a: word;',
  5200. 'begin',
  5201. ' for i:=1 to 10 do begin',
  5202. ' Foo(procedure begin a:=3; end);',
  5203. ' end;',
  5204. 'end;',
  5205. 'begin',
  5206. ' DoIt;']);
  5207. ConvertProgram;
  5208. CheckSource('TestAnonymousProc_ForLoop',
  5209. LinesToStr([ // statements
  5210. 'this.Foo = function (p) {',
  5211. '};',
  5212. 'this.DoIt = function () {',
  5213. ' var i = 0;',
  5214. ' var a = 0;',
  5215. ' for (i = 1; i <= 10; i++) {',
  5216. ' $mod.Foo(function () {',
  5217. ' a = 3;',
  5218. ' });',
  5219. ' };',
  5220. '};',
  5221. '']),
  5222. LinesToStr([
  5223. '$mod.DoIt();'
  5224. ]));
  5225. end;
  5226. procedure TTestModule.TestAnonymousProc_AsmDelphi;
  5227. begin
  5228. StartProgram(false);
  5229. Add([
  5230. '{$mode delphi}',
  5231. 'type',
  5232. ' TProc = reference to procedure;',
  5233. ' TFunc = reference to function(x: word): word;',
  5234. 'procedure Run;',
  5235. 'asm',
  5236. 'end;',
  5237. 'procedure Walk(p: TProc; f: TFunc);',
  5238. 'begin',
  5239. ' Walk(procedure asm end, function(b:word): word asm return 1+b; end);',
  5240. 'end;',
  5241. 'begin',
  5242. ' Walk(procedure',
  5243. ' asm',
  5244. ' console.log("a");',
  5245. ' end,',
  5246. ' function(x: word): word asm',
  5247. ' console.log("c");',
  5248. ' end);',
  5249. '']);
  5250. ConvertProgram;
  5251. CheckSource('TestAnonymousProc_AsmDelphi',
  5252. LinesToStr([ // statements
  5253. 'this.Run = function () {',
  5254. '};',
  5255. 'this.Walk = function (p, f) {',
  5256. ' $mod.Walk(function () {',
  5257. ' }, function (b) {',
  5258. ' return 1+b;',
  5259. ' });',
  5260. '};',
  5261. '']),
  5262. LinesToStr([
  5263. '$mod.Walk(function () {',
  5264. ' console.log("a");',
  5265. '}, function (x) {',
  5266. ' console.log("c");',
  5267. '});',
  5268. '']));
  5269. end;
  5270. procedure TTestModule.TestEnum_Name;
  5271. begin
  5272. StartProgram(false);
  5273. Add('type TMyEnum = (Red, Green, Blue);');
  5274. Add('var e: TMyEnum;');
  5275. Add('var f: TMyEnum = Blue;');
  5276. Add('begin');
  5277. Add(' e:=green;');
  5278. Add(' e:=default(TMyEnum);');
  5279. ConvertProgram;
  5280. CheckSource('TestEnum_Name',
  5281. LinesToStr([ // statements
  5282. 'this.TMyEnum = {',
  5283. ' "0":"Red",',
  5284. ' Red:0,',
  5285. ' "1":"Green",',
  5286. ' Green:1,',
  5287. ' "2":"Blue",',
  5288. ' Blue:2',
  5289. ' };',
  5290. 'this.e = 0;',
  5291. 'this.f = this.TMyEnum.Blue;'
  5292. ]),
  5293. LinesToStr([
  5294. '$mod.e=$mod.TMyEnum.Green;',
  5295. '$mod.e=$mod.TMyEnum.Red;'
  5296. ]));
  5297. end;
  5298. procedure TTestModule.TestEnum_Number;
  5299. begin
  5300. Converter.Options:=Converter.Options+[coEnumNumbers];
  5301. StartProgram(false);
  5302. Add('type TMyEnum = (Red, Green);');
  5303. Add('var');
  5304. Add(' e: TMyEnum;');
  5305. Add(' f: TMyEnum = Green;');
  5306. Add(' i: longint;');
  5307. Add('begin');
  5308. Add(' e:=green;');
  5309. Add(' i:=longint(e);');
  5310. ConvertProgram;
  5311. CheckSource('TestEnumNumber',
  5312. LinesToStr([ // statements
  5313. 'this.TMyEnum = {',
  5314. ' "0":"Red",',
  5315. ' Red:0,',
  5316. ' "1":"Green",',
  5317. ' Green:1',
  5318. ' };',
  5319. 'this.e = 0;',
  5320. 'this.f = 1;',
  5321. 'this.i = 0;'
  5322. ]),
  5323. LinesToStr([
  5324. '$mod.e=1;',
  5325. '$mod.i=$mod.e;'
  5326. ]));
  5327. end;
  5328. procedure TTestModule.TestEnum_ConstFail;
  5329. begin
  5330. StartProgram(false);
  5331. Add([
  5332. 'type TMyEnum = (Red = 100, Green = 101);',
  5333. 'var',
  5334. ' e: TMyEnum;',
  5335. ' f: TMyEnum = Green;',
  5336. 'begin',
  5337. ' e:=green;']);
  5338. SetExpectedPasResolverError('not yet implemented: Red:TPasEnumValue [20180126202434] "enum const"',3002);
  5339. ConvertProgram;
  5340. end;
  5341. procedure TTestModule.TestEnum_Functions;
  5342. begin
  5343. StartProgram(false);
  5344. Add([
  5345. 'type TMyEnum = (Red, Green);',
  5346. 'procedure DoIt(var e: TMyEnum; var i: word);',
  5347. 'var',
  5348. ' v: longint;',
  5349. ' s: string;',
  5350. 'begin',
  5351. ' val(s,e,v);',
  5352. ' val(s,e,i);',
  5353. 'end;',
  5354. 'var',
  5355. ' e: TMyEnum;',
  5356. ' i: longint;',
  5357. ' s: string;',
  5358. ' b: boolean;',
  5359. 'begin',
  5360. ' i:=ord(red);',
  5361. ' i:=ord(green);',
  5362. ' i:=ord(e);',
  5363. ' i:=ord(b);',
  5364. ' e:=low(tmyenum);',
  5365. ' e:=low(e);',
  5366. ' b:=low(boolean);',
  5367. ' e:=high(tmyenum);',
  5368. ' e:=high(e);',
  5369. ' b:=high(boolean);',
  5370. ' e:=pred(green);',
  5371. ' e:=pred(e);',
  5372. ' b:=pred(b);',
  5373. ' e:=succ(red);',
  5374. ' e:=succ(e);',
  5375. ' b:=succ(b);',
  5376. ' e:=tmyenum(1);',
  5377. ' e:=tmyenum(i);',
  5378. ' s:=str(e);',
  5379. ' str(e,s);',
  5380. ' str(red,s);',
  5381. ' s:=str(e:3);',
  5382. ' writestr(s,e:3,red);',
  5383. ' val(s,e,i);',
  5384. ' i:=longint(e);']);
  5385. ConvertProgram;
  5386. CheckSource('TestEnum_Functions',
  5387. LinesToStr([ // statements
  5388. 'this.TMyEnum = {',
  5389. ' "0":"Red",',
  5390. ' Red:0,',
  5391. ' "1":"Green",',
  5392. ' Green:1',
  5393. ' };',
  5394. 'this.DoIt = function (e, i) {',
  5395. ' var v = 0;',
  5396. ' var s = "";',
  5397. ' e.set(rtl.valEnum(s, $mod.TMyEnum, function (w) {',
  5398. ' v = w;',
  5399. ' }));',
  5400. ' e.set(rtl.valEnum(s, $mod.TMyEnum, i.set));',
  5401. '};',
  5402. 'this.e = 0;',
  5403. 'this.i = 0;',
  5404. 'this.s = "";',
  5405. 'this.b = false;',
  5406. '']),
  5407. LinesToStr([
  5408. '$mod.i=$mod.TMyEnum.Red;',
  5409. '$mod.i=$mod.TMyEnum.Green;',
  5410. '$mod.i=$mod.e;',
  5411. '$mod.i=$mod.b+0;',
  5412. '$mod.e=$mod.TMyEnum.Red;',
  5413. '$mod.e=$mod.TMyEnum.Red;',
  5414. '$mod.b=false;',
  5415. '$mod.e=$mod.TMyEnum.Green;',
  5416. '$mod.e=$mod.TMyEnum.Green;',
  5417. '$mod.b=true;',
  5418. '$mod.e=$mod.TMyEnum.Green-1;',
  5419. '$mod.e=$mod.e-1;',
  5420. '$mod.b=false;',
  5421. '$mod.e=$mod.TMyEnum.Red+1;',
  5422. '$mod.e=$mod.e+1;',
  5423. '$mod.b=true;',
  5424. '$mod.e=1;',
  5425. '$mod.e=$mod.i;',
  5426. '$mod.s = $mod.TMyEnum[$mod.e];',
  5427. '$mod.s = $mod.TMyEnum[$mod.e];',
  5428. '$mod.s = $mod.TMyEnum[$mod.TMyEnum.Red];',
  5429. '$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3);',
  5430. '$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3)+$mod.TMyEnum[$mod.TMyEnum.Red];',
  5431. '$mod.e = rtl.valEnum($mod.s, $mod.TMyEnum, function (v) {',
  5432. ' $mod.i = v;',
  5433. '});',
  5434. '$mod.i=$mod.e;',
  5435. '']));
  5436. end;
  5437. procedure TTestModule.TestEnumRg_Functions;
  5438. begin
  5439. StartProgram(false);
  5440. Add([
  5441. 'type',
  5442. ' TEnum = (Red, Green, Blue);',
  5443. ' TEnumRg = Green..Blue;',
  5444. 'procedure DoIt(var e: TEnumRg; var i: word);',
  5445. 'var',
  5446. ' v: longint;',
  5447. ' s: string;',
  5448. 'begin',
  5449. ' val(s,e,v);',
  5450. ' val(s,e,i);',
  5451. 'end;',
  5452. 'var',
  5453. ' e: TEnumRg;',
  5454. ' i: longint;',
  5455. ' s: string;',
  5456. 'begin',
  5457. ' i:=ord(green);',
  5458. ' i:=ord(e);',
  5459. ' e:=low(tenumrg);',
  5460. ' e:=low(e);',
  5461. ' e:=high(tenumrg);',
  5462. ' e:=high(e);',
  5463. ' e:=pred(blue);',
  5464. ' e:=pred(e);',
  5465. ' e:=succ(green);',
  5466. ' e:=succ(e);',
  5467. ' e:=tenumrg(1);',
  5468. ' e:=tenumrg(i);',
  5469. ' s:=str(e);',
  5470. ' str(e,s);',
  5471. ' str(red,s);',
  5472. ' s:=str(e:3);',
  5473. ' writestr(s,e:3,blue);',
  5474. ' val(s,e,i);',
  5475. ' i:=longint(e);']);
  5476. ConvertProgram;
  5477. CheckSource('TestEnumRg_Functions',
  5478. LinesToStr([ // statements
  5479. 'this.TEnum = {',
  5480. ' "0":"Red",',
  5481. ' Red:0,',
  5482. ' "1":"Green",',
  5483. ' Green:1,',
  5484. ' "2":"Blue",',
  5485. ' Blue:2',
  5486. ' };',
  5487. 'this.DoIt = function (e, i) {',
  5488. ' var v = 0;',
  5489. ' var s = "";',
  5490. ' e.set(rtl.valEnum(s, $mod.TEnum, function (w) {',
  5491. ' v = w;',
  5492. ' }));',
  5493. ' e.set(rtl.valEnum(s, $mod.TEnum, i.set));',
  5494. '};',
  5495. 'this.e = this.TEnum.Green;',
  5496. 'this.i = 0;',
  5497. 'this.s = "";',
  5498. '']),
  5499. LinesToStr([
  5500. '$mod.i=$mod.TEnum.Green;',
  5501. '$mod.i=$mod.e;',
  5502. '$mod.e=$mod.TEnum.Green;',
  5503. '$mod.e=$mod.TEnum.Green;',
  5504. '$mod.e=$mod.TEnum.Blue;',
  5505. '$mod.e=$mod.TEnum.Blue;',
  5506. '$mod.e=$mod.TEnum.Blue-1;',
  5507. '$mod.e=$mod.e-1;',
  5508. '$mod.e=$mod.TEnum.Green+1;',
  5509. '$mod.e=$mod.e+1;',
  5510. '$mod.e=1;',
  5511. '$mod.e=$mod.i;',
  5512. '$mod.s = $mod.TEnum[$mod.e];',
  5513. '$mod.s = $mod.TEnum[$mod.e];',
  5514. '$mod.s = $mod.TEnum[$mod.TEnum.Red];',
  5515. '$mod.s = rtl.spaceLeft($mod.TEnum[$mod.e], 3);',
  5516. '$mod.s = rtl.spaceLeft($mod.TEnum[$mod.e], 3)+$mod.TEnum[$mod.TEnum.Blue];',
  5517. '$mod.e = rtl.valEnum($mod.s, $mod.TEnum, function (v) {',
  5518. ' $mod.i = v;',
  5519. '});',
  5520. '$mod.i=$mod.e;',
  5521. '']));
  5522. end;
  5523. procedure TTestModule.TestEnum_AsParams;
  5524. begin
  5525. StartProgram(false);
  5526. Add('type TEnum = (Red,Blue);');
  5527. Add('procedure DoIt(vG: TEnum; const vH: TEnum; var vI: TEnum);');
  5528. Add('var vJ: TEnum;');
  5529. Add('begin');
  5530. Add(' vg:=vg;');
  5531. Add(' vj:=vh;');
  5532. Add(' vi:=vi;');
  5533. Add(' doit(vg,vg,vg);');
  5534. Add(' doit(vh,vh,vj);');
  5535. Add(' doit(vi,vi,vi);');
  5536. Add(' doit(vj,vj,vj);');
  5537. Add('end;');
  5538. Add('var i: TEnum;');
  5539. Add('begin');
  5540. Add(' doit(i,i,i);');
  5541. ConvertProgram;
  5542. CheckSource('TestEnum_AsParams',
  5543. LinesToStr([ // statements
  5544. 'this.TEnum = {',
  5545. ' "0": "Red",',
  5546. ' Red: 0,',
  5547. ' "1": "Blue",',
  5548. ' Blue: 1',
  5549. '};',
  5550. 'this.DoIt = function (vG,vH,vI) {',
  5551. ' var vJ = 0;',
  5552. ' vG = vG;',
  5553. ' vJ = vH;',
  5554. ' vI.set(vI.get());',
  5555. ' $mod.DoIt(vG, vG, {',
  5556. ' get: function () {',
  5557. ' return vG;',
  5558. ' },',
  5559. ' set: function (v) {',
  5560. ' vG = v;',
  5561. ' }',
  5562. ' });',
  5563. ' $mod.DoIt(vH, vH, {',
  5564. ' get: function () {',
  5565. ' return vJ;',
  5566. ' },',
  5567. ' set: function (v) {',
  5568. ' vJ = v;',
  5569. ' }',
  5570. ' });',
  5571. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  5572. ' $mod.DoIt(vJ, vJ, {',
  5573. ' get: function () {',
  5574. ' return vJ;',
  5575. ' },',
  5576. ' set: function (v) {',
  5577. ' vJ = v;',
  5578. ' }',
  5579. ' });',
  5580. '};',
  5581. 'this.i = 0;'
  5582. ]),
  5583. LinesToStr([
  5584. '$mod.DoIt($mod.i,$mod.i,{',
  5585. ' p: $mod,',
  5586. ' get: function () {',
  5587. ' return this.p.i;',
  5588. ' },',
  5589. ' set: function (v) {',
  5590. ' this.p.i = v;',
  5591. ' }',
  5592. '});'
  5593. ]));
  5594. end;
  5595. procedure TTestModule.TestEnumRange_Array;
  5596. begin
  5597. StartProgram(false);
  5598. Add([
  5599. 'type',
  5600. ' TEnum = (Red, Green, Blue);',
  5601. ' TEnumRg = green..blue;',
  5602. ' TArr = array[TEnumRg] of byte;',
  5603. ' TArr2 = array[green..blue] of byte;',
  5604. 'var',
  5605. ' a: TArr;',
  5606. ' b: TArr = (3,4);',
  5607. ' c: TArr2 = (5,6);',
  5608. 'begin',
  5609. ' a[green] := b[blue];',
  5610. ' c[green] := c[blue];',
  5611. '']);
  5612. ConvertProgram;
  5613. CheckSource('TestEnumRange_Array',
  5614. LinesToStr([ // statements
  5615. 'this.TEnum = {',
  5616. ' "0": "Red",',
  5617. ' Red: 0,',
  5618. ' "1": "Green",',
  5619. ' Green: 1,',
  5620. ' "2": "Blue",',
  5621. ' Blue: 2',
  5622. '};',
  5623. 'this.a = rtl.arraySetLength(null, 0, 2);',
  5624. 'this.b = [3, 4];',
  5625. 'this.c = [5, 6];',
  5626. '']),
  5627. LinesToStr([
  5628. ' $mod.a[$mod.TEnum.Green - 1] = $mod.b[$mod.TEnum.Blue - 1];',
  5629. ' $mod.c[$mod.TEnum.Green - 1] = $mod.c[$mod.TEnum.Blue - 1];',
  5630. '']));
  5631. end;
  5632. procedure TTestModule.TestEnum_ForIn;
  5633. begin
  5634. StartProgram(false);
  5635. Add([
  5636. 'type',
  5637. ' TEnum = (Red, Green, Blue);',
  5638. ' TEnumRg = green..blue;',
  5639. ' TArr = array[TEnum] of byte;',
  5640. ' TArrRg = array[TEnumRg] of byte;',
  5641. 'var',
  5642. ' e: TEnum;',
  5643. ' a1: TArr = (3,4,5);',
  5644. ' a2: TArrRg = (11,12);',
  5645. ' b: byte;',
  5646. 'begin',
  5647. ' for e in TEnum do ;',
  5648. ' for e in TEnumRg do ;',
  5649. ' for e in TArr do ;',
  5650. ' for e in TArrRg do ;',
  5651. ' for b in a1 do ;',
  5652. ' for b in a2 do ;',
  5653. '']);
  5654. ConvertProgram;
  5655. CheckSource('TestEnum_ForIn',
  5656. LinesToStr([ // statements
  5657. 'this.TEnum = {',
  5658. ' "0": "Red",',
  5659. ' Red: 0,',
  5660. ' "1": "Green",',
  5661. ' Green: 1,',
  5662. ' "2": "Blue",',
  5663. ' Blue: 2',
  5664. '};',
  5665. 'this.e = 0;',
  5666. 'this.a1 = [3, 4, 5];',
  5667. 'this.a2 = [11, 12];',
  5668. 'this.b = 0;',
  5669. '']),
  5670. LinesToStr([
  5671. ' for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  5672. ' for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  5673. ' for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  5674. ' for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  5675. ' for (var $in = $mod.a1, $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) $mod.b = $in[$l];',
  5676. ' for (var $in1 = $mod.a2, $l1 = 0, $end1 = rtl.length($in1) - 1; $l1 <= $end1; $l1++) $mod.b = $in1[$l1];',
  5677. '']));
  5678. end;
  5679. procedure TTestModule.TestEnum_ScopedNumber;
  5680. begin
  5681. Converter.Options:=Converter.Options+[coEnumNumbers];
  5682. StartProgram(false);
  5683. Add([
  5684. 'type',
  5685. ' TEnum = (Red, Green);',
  5686. 'var',
  5687. ' e: TEnum;',
  5688. 'begin',
  5689. ' e:=TEnum.Green;',
  5690. '']);
  5691. ConvertProgram;
  5692. CheckSource('TestEnum_ScopedNumber',
  5693. LinesToStr([ // statements
  5694. 'this.TEnum = {',
  5695. ' "0": "Red",',
  5696. ' Red: 0,',
  5697. ' "1": "Green",',
  5698. ' Green: 1',
  5699. '};',
  5700. 'this.e = 0;',
  5701. '']),
  5702. LinesToStr([
  5703. '$mod.e = 1;']));
  5704. end;
  5705. procedure TTestModule.TestEnum_InFunction;
  5706. begin
  5707. StartProgram(false);
  5708. Add([
  5709. 'const TEnum = 3;',
  5710. 'procedure DoIt;',
  5711. 'type',
  5712. ' TEnum = (Red, Green, Blue);',
  5713. ' procedure Sub;',
  5714. ' type',
  5715. ' TEnumSub = (Left, Right);',
  5716. ' var',
  5717. ' es: TEnumSub;',
  5718. ' begin',
  5719. ' es:=Left;',
  5720. ' end;',
  5721. 'var',
  5722. ' e, e2: TEnum;',
  5723. 'begin',
  5724. ' if e in [red,blue] then e2:=e;',
  5725. 'end;',
  5726. 'begin']);
  5727. ConvertProgram;
  5728. CheckSource('TestEnum_InFunction',
  5729. LinesToStr([ // statements
  5730. 'this.TEnum = 3;',
  5731. 'var TEnum$1 = {',
  5732. ' "0":"Red",',
  5733. ' Red:0,',
  5734. ' "1":"Green",',
  5735. ' Green:1,',
  5736. ' "2":"Blue",',
  5737. ' Blue:2',
  5738. ' };',
  5739. 'var TEnumSub = {',
  5740. ' "0": "Left",',
  5741. ' Left: 0,',
  5742. ' "1": "Right",',
  5743. ' Right: 1',
  5744. '};',
  5745. 'this.DoIt = function () {',
  5746. ' function Sub() {',
  5747. ' var es = 0;',
  5748. ' es = TEnumSub.Left;',
  5749. ' };',
  5750. ' var e = 0;',
  5751. ' var e2 = 0;',
  5752. ' if (e in rtl.createSet(TEnum$1.Red, TEnum$1.Blue)) e2 = e;',
  5753. '};',
  5754. '']),
  5755. LinesToStr([
  5756. '']));
  5757. end;
  5758. procedure TTestModule.TestEnum_Name_Anonymous_Unit;
  5759. begin
  5760. StartUnit(true);
  5761. Add([
  5762. 'interface',
  5763. 'var color: (red, green);',
  5764. 'implementation',
  5765. 'initialization',
  5766. ' color:=green;',
  5767. '']);
  5768. ConvertUnit;
  5769. CheckSource('TestEnum_Name_Anonymous_Unit',
  5770. LinesToStr([
  5771. 'this.color$a = {',
  5772. ' "0": "red",',
  5773. ' red: 0,',
  5774. ' "1": "green",',
  5775. ' green: 1',
  5776. '};',
  5777. 'this.color = 0;',
  5778. '']),
  5779. LinesToStr([ // this.$init
  5780. '$mod.color = $mod.color$a.green;',
  5781. '']),
  5782. LinesToStr([ // implementation
  5783. '']) );
  5784. end;
  5785. procedure TTestModule.TestSet_Enum;
  5786. begin
  5787. StartProgram(false);
  5788. Add([
  5789. 'type',
  5790. ' TColor = (Red, Green, Blue);',
  5791. ' TColors = set of TColor;',
  5792. 'var',
  5793. ' c: TColor;',
  5794. ' s: TColors;',
  5795. ' t: TColors = [];',
  5796. ' u: TColors = [Red];',
  5797. 'begin',
  5798. ' s:=[];',
  5799. ' s:=[Green];',
  5800. ' s:=[Green,Blue];',
  5801. ' s:=[Red..Blue];',
  5802. ' s:=[Red,Green..Blue];',
  5803. ' s:=[Red,c];',
  5804. ' s:=t;',
  5805. ' s:=default(TColors);',
  5806. '']);
  5807. ConvertProgram;
  5808. CheckSource('TestSet',
  5809. LinesToStr([ // statements
  5810. 'this.TColor = {',
  5811. ' "0":"Red",',
  5812. ' Red:0,',
  5813. ' "1":"Green",',
  5814. ' Green:1,',
  5815. ' "2":"Blue",',
  5816. ' Blue:2',
  5817. ' };',
  5818. 'this.c = 0;',
  5819. 'this.s = {};',
  5820. 'this.t = {};',
  5821. 'this.u = rtl.createSet(this.TColor.Red);'
  5822. ]),
  5823. LinesToStr([
  5824. '$mod.s={};',
  5825. '$mod.s=rtl.createSet($mod.TColor.Green);',
  5826. '$mod.s=rtl.createSet($mod.TColor.Green,$mod.TColor.Blue);',
  5827. '$mod.s=rtl.createSet(null,$mod.TColor.Red,$mod.TColor.Blue);',
  5828. '$mod.s=rtl.createSet($mod.TColor.Red,null,$mod.TColor.Green,$mod.TColor.Blue);',
  5829. '$mod.s=rtl.createSet($mod.TColor.Red,$mod.c);',
  5830. '$mod.s=rtl.refSet($mod.t);',
  5831. '$mod.s={};',
  5832. '']));
  5833. end;
  5834. procedure TTestModule.TestSet_Operators;
  5835. begin
  5836. StartProgram(false);
  5837. Add('type');
  5838. Add(' TColor = (Red, Green, Blue);');
  5839. Add(' TColors = set of tcolor;');
  5840. Add('var');
  5841. Add(' vC: TColor;');
  5842. Add(' vS: TColors;');
  5843. Add(' vT: TColors;');
  5844. Add(' vU: TColors;');
  5845. Add(' B: boolean;');
  5846. Add('begin');
  5847. Add(' include(vs,green);');
  5848. Add(' exclude(vs,vc);');
  5849. Add(' vs:=vt+vu;');
  5850. Add(' vs:=vt+[red];');
  5851. Add(' vs:=[red]+vt;');
  5852. Add(' vs:=[red]+[green];');
  5853. Add(' vs:=vt-vu;');
  5854. Add(' vs:=vt-[red];');
  5855. Add(' vs:=[red]-vt;');
  5856. Add(' vs:=[red]-[green];');
  5857. Add(' vs:=vt*vu;');
  5858. Add(' vs:=vt*[red];');
  5859. Add(' vs:=[red]*vt;');
  5860. Add(' vs:=[red]*[green];');
  5861. Add(' vs:=vt><vu;');
  5862. Add(' vs:=vt><[red];');
  5863. Add(' vs:=[red]><vt;');
  5864. Add(' vs:=[red]><[green];');
  5865. Add(' b:=vt=vu;');
  5866. Add(' b:=vt=[red];');
  5867. Add(' b:=[red]=vt;');
  5868. Add(' b:=[red]=[green];');
  5869. Add(' b:=vt<>vu;');
  5870. Add(' b:=vt<>[red];');
  5871. Add(' b:=[red]<>vt;');
  5872. Add(' b:=[red]<>[green];');
  5873. Add(' b:=vt<=vu;');
  5874. Add(' b:=vt<=[red];');
  5875. Add(' b:=[red]<=vt;');
  5876. Add(' b:=[red]<=[green];');
  5877. Add(' b:=vt>=vu;');
  5878. Add(' b:=vt>=[red];');
  5879. Add(' b:=[red]>=vt;');
  5880. Add(' b:=[red]>=[green];');
  5881. ConvertProgram;
  5882. CheckSource('TestSet_Operators',
  5883. LinesToStr([ // statements
  5884. 'this.TColor = {',
  5885. ' "0":"Red",',
  5886. ' Red:0,',
  5887. ' "1":"Green",',
  5888. ' Green:1,',
  5889. ' "2":"Blue",',
  5890. ' Blue:2',
  5891. ' };',
  5892. 'this.vC = 0;',
  5893. 'this.vS = {};',
  5894. 'this.vT = {};',
  5895. 'this.vU = {};',
  5896. 'this.B = false;'
  5897. ]),
  5898. LinesToStr([
  5899. '$mod.vS = rtl.includeSet($mod.vS,$mod.TColor.Green);',
  5900. '$mod.vS = rtl.excludeSet($mod.vS,$mod.vC);',
  5901. '$mod.vS = rtl.unionSet($mod.vT, $mod.vU);',
  5902. '$mod.vS = rtl.unionSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5903. '$mod.vS = rtl.unionSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5904. '$mod.vS = rtl.unionSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5905. '$mod.vS = rtl.diffSet($mod.vT, $mod.vU);',
  5906. '$mod.vS = rtl.diffSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5907. '$mod.vS = rtl.diffSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5908. '$mod.vS = rtl.diffSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5909. '$mod.vS = rtl.intersectSet($mod.vT, $mod.vU);',
  5910. '$mod.vS = rtl.intersectSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5911. '$mod.vS = rtl.intersectSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5912. '$mod.vS = rtl.intersectSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5913. '$mod.vS = rtl.symDiffSet($mod.vT, $mod.vU);',
  5914. '$mod.vS = rtl.symDiffSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5915. '$mod.vS = rtl.symDiffSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5916. '$mod.vS = rtl.symDiffSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5917. '$mod.B = rtl.eqSet($mod.vT, $mod.vU);',
  5918. '$mod.B = rtl.eqSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5919. '$mod.B = rtl.eqSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5920. '$mod.B = rtl.eqSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5921. '$mod.B = rtl.neSet($mod.vT, $mod.vU);',
  5922. '$mod.B = rtl.neSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5923. '$mod.B = rtl.neSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5924. '$mod.B = rtl.neSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5925. '$mod.B = rtl.leSet($mod.vT, $mod.vU);',
  5926. '$mod.B = rtl.leSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5927. '$mod.B = rtl.leSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5928. '$mod.B = rtl.leSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5929. '$mod.B = rtl.geSet($mod.vT, $mod.vU);',
  5930. '$mod.B = rtl.geSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5931. '$mod.B = rtl.geSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5932. '$mod.B = rtl.geSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5933. '']));
  5934. end;
  5935. procedure TTestModule.TestSet_Operator_In;
  5936. begin
  5937. StartProgram(false);
  5938. Add([
  5939. 'type',
  5940. ' TColor = (Red, Green, Blue);',
  5941. ' TColors = set of tcolor;',
  5942. ' TColorRg = green..blue;',
  5943. 'var',
  5944. ' vC: tcolor;',
  5945. ' vT: tcolors;',
  5946. ' B: boolean;',
  5947. ' rg: TColorRg;',
  5948. 'begin',
  5949. ' b:=red in vt;',
  5950. ' b:=vc in vt;',
  5951. ' b:=green in [red..blue];',
  5952. ' b:=vc in [red..blue];',
  5953. ' ',
  5954. ' if red in vt then ;',
  5955. ' while vC in vt do ;',
  5956. ' repeat',
  5957. ' until vC in vt;',
  5958. ' if rg in [green..blue] then ;',
  5959. '']);
  5960. ConvertProgram;
  5961. CheckSource('TestSet_Operator_In',
  5962. LinesToStr([ // statements
  5963. 'this.TColor = {',
  5964. ' "0":"Red",',
  5965. ' Red:0,',
  5966. ' "1":"Green",',
  5967. ' Green:1,',
  5968. ' "2":"Blue",',
  5969. ' Blue:2',
  5970. ' };',
  5971. 'this.vC = 0;',
  5972. 'this.vT = {};',
  5973. 'this.B = false;',
  5974. 'this.rg = this.TColor.Green;',
  5975. '']),
  5976. LinesToStr([
  5977. '$mod.B = $mod.TColor.Red in $mod.vT;',
  5978. '$mod.B = $mod.vC in $mod.vT;',
  5979. '$mod.B = $mod.TColor.Green in rtl.createSet(null, $mod.TColor.Red, $mod.TColor.Blue);',
  5980. '$mod.B = $mod.vC in rtl.createSet(null, $mod.TColor.Red, $mod.TColor.Blue);',
  5981. 'if ($mod.TColor.Red in $mod.vT) ;',
  5982. 'while ($mod.vC in $mod.vT) {',
  5983. '};',
  5984. 'do {',
  5985. '} while (!($mod.vC in $mod.vT));',
  5986. 'if ($mod.rg in rtl.createSet(null, $mod.TColor.Green, $mod.TColor.Blue)) ;',
  5987. '']));
  5988. end;
  5989. procedure TTestModule.TestSet_Functions;
  5990. begin
  5991. StartProgram(false);
  5992. Add('type');
  5993. Add(' TMyEnum = (Red, Green);');
  5994. Add(' TMyEnums = set of TMyEnum;');
  5995. Add('var');
  5996. Add(' e: TMyEnum;');
  5997. Add(' s: TMyEnums;');
  5998. Add('begin');
  5999. Add(' e:=Low(TMyEnums);');
  6000. Add(' e:=Low(s);');
  6001. Add(' e:=High(TMyEnums);');
  6002. Add(' e:=High(s);');
  6003. ConvertProgram;
  6004. CheckSource('TestSetFunctions',
  6005. LinesToStr([ // statements
  6006. 'this.TMyEnum = {',
  6007. ' "0":"Red",',
  6008. ' Red:0,',
  6009. ' "1":"Green",',
  6010. ' Green:1',
  6011. ' };',
  6012. 'this.e = 0;',
  6013. 'this.s = {};'
  6014. ]),
  6015. LinesToStr([
  6016. '$mod.e=$mod.TMyEnum.Red;',
  6017. '$mod.e=$mod.TMyEnum.Red;',
  6018. '$mod.e=$mod.TMyEnum.Green;',
  6019. '$mod.e=$mod.TMyEnum.Green;',
  6020. '']));
  6021. end;
  6022. procedure TTestModule.TestSet_PassAsArgClone;
  6023. begin
  6024. StartProgram(false);
  6025. Add('type');
  6026. Add(' TMyEnum = (Red, Green);');
  6027. Add(' TMyEnums = set of TMyEnum;');
  6028. Add('procedure DoDefault(s: tmyenums); begin end;');
  6029. Add('procedure DoConst(const s: tmyenums); begin end;');
  6030. Add('var');
  6031. Add(' aSet: tmyenums;');
  6032. Add('begin');
  6033. Add(' dodefault(aset);');
  6034. Add(' doconst(aset);');
  6035. ConvertProgram;
  6036. CheckSource('TestSetFunctions',
  6037. LinesToStr([ // statements
  6038. 'this.TMyEnum = {',
  6039. ' "0":"Red",',
  6040. ' Red:0,',
  6041. ' "1":"Green",',
  6042. ' Green:1',
  6043. ' };',
  6044. 'this.DoDefault = function (s) {',
  6045. '};',
  6046. 'this.DoConst = function (s) {',
  6047. '};',
  6048. 'this.aSet = {};'
  6049. ]),
  6050. LinesToStr([
  6051. '$mod.DoDefault(rtl.refSet($mod.aSet));',
  6052. '$mod.DoConst($mod.aSet);',
  6053. '']));
  6054. end;
  6055. procedure TTestModule.TestSet_AsParams;
  6056. begin
  6057. StartProgram(false);
  6058. Add([
  6059. 'type TEnum = (Red,Blue);',
  6060. 'type TEnums = set of TEnum;',
  6061. 'function DoIt(vG: TEnums; const vH: TEnums; var vI: TEnums): TEnums;',
  6062. 'var vJ: TEnums;',
  6063. 'begin',
  6064. ' Include(vg,red);',
  6065. ' Include(result,blue);',
  6066. ' vg:=vg;',
  6067. ' vj:=vh;',
  6068. ' vi:=vi;',
  6069. ' doit(vg,vg,vg);',
  6070. ' doit(vh,vh,vj);',
  6071. ' doit(vi,vi,vi);',
  6072. ' doit(vj,vj,vj);',
  6073. 'end;',
  6074. 'var i: TEnums;',
  6075. 'begin',
  6076. ' doit(i,i,i);']);
  6077. ConvertProgram;
  6078. CheckSource('TestSet_AsParams',
  6079. LinesToStr([ // statements
  6080. 'this.TEnum = {',
  6081. ' "0": "Red",',
  6082. ' Red: 0,',
  6083. ' "1": "Blue",',
  6084. ' Blue: 1',
  6085. '};',
  6086. 'this.DoIt = function (vG,vH,vI) {',
  6087. ' var Result = {};',
  6088. ' var vJ = {};',
  6089. ' vG = rtl.includeSet(vG, $mod.TEnum.Red);',
  6090. ' Result = rtl.includeSet(Result, $mod.TEnum.Blue);',
  6091. ' vG = rtl.refSet(vG);',
  6092. ' vJ = rtl.refSet(vH);',
  6093. ' vI.set(rtl.refSet(vI.get()));',
  6094. ' $mod.DoIt(rtl.refSet(vG), vG, {',
  6095. ' get: function () {',
  6096. ' return vG;',
  6097. ' },',
  6098. ' set: function (v) {',
  6099. ' vG = v;',
  6100. ' }',
  6101. ' });',
  6102. ' $mod.DoIt(rtl.refSet(vH), vH, {',
  6103. ' get: function () {',
  6104. ' return vJ;',
  6105. ' },',
  6106. ' set: function (v) {',
  6107. ' vJ = v;',
  6108. ' }',
  6109. ' });',
  6110. ' $mod.DoIt(rtl.refSet(vI.get()), vI.get(), vI);',
  6111. ' $mod.DoIt(rtl.refSet(vJ), vJ, {',
  6112. ' get: function () {',
  6113. ' return vJ;',
  6114. ' },',
  6115. ' set: function (v) {',
  6116. ' vJ = v;',
  6117. ' }',
  6118. ' });',
  6119. ' return Result;',
  6120. '};',
  6121. 'this.i = {};'
  6122. ]),
  6123. LinesToStr([
  6124. '$mod.DoIt(rtl.refSet($mod.i),$mod.i,{',
  6125. ' p: $mod,',
  6126. ' get: function () {',
  6127. ' return this.p.i;',
  6128. ' },',
  6129. ' set: function (v) {',
  6130. ' this.p.i = v;',
  6131. ' }',
  6132. '});'
  6133. ]));
  6134. end;
  6135. procedure TTestModule.TestSet_Property;
  6136. begin
  6137. StartProgram(false);
  6138. Add('type');
  6139. Add(' TEnum = (Red,Blue);');
  6140. Add(' TEnums = set of TEnum;');
  6141. Add(' TObject = class');
  6142. Add(' function GetColors: TEnums; external name ''GetColors'';');
  6143. Add(' procedure SetColors(const Value: TEnums); external name ''SetColors'';');
  6144. Add(' property Colors: TEnums read GetColors write SetColors;');
  6145. Add(' end;');
  6146. Add('procedure DoIt(i: TEnums; const j: TEnums; var k: TEnums; out l: TEnums);');
  6147. Add('begin end;');
  6148. Add('var Obj: TObject;');
  6149. Add('begin');
  6150. Add(' Include(Obj.Colors,Red);');
  6151. Add(' Exclude(Obj.Colors,Red);');
  6152. //Add(' DoIt(Obj.Colors,Obj.Colors,Obj.Colors,Obj.Colors);');
  6153. ConvertProgram;
  6154. CheckSource('TestSet_Property',
  6155. LinesToStr([ // statements
  6156. 'this.TEnum = {',
  6157. ' "0": "Red",',
  6158. ' Red: 0,',
  6159. ' "1": "Blue",',
  6160. ' Blue: 1',
  6161. '};',
  6162. 'rtl.createClass(this, "TObject", null, function () {',
  6163. ' this.$init = function () {',
  6164. ' };',
  6165. ' this.$final = function () {',
  6166. ' };',
  6167. '});',
  6168. 'this.DoIt = function (i, j, k, l) {',
  6169. '};',
  6170. 'this.Obj = null;',
  6171. '']),
  6172. LinesToStr([
  6173. '$mod.Obj.SetColors(rtl.includeSet($mod.Obj.GetColors(), $mod.TEnum.Red));',
  6174. '$mod.Obj.SetColors(rtl.excludeSet($mod.Obj.GetColors(), $mod.TEnum.Red));',
  6175. '']));
  6176. end;
  6177. procedure TTestModule.TestSet_EnumConst;
  6178. begin
  6179. StartProgram(false);
  6180. Add([
  6181. 'type',
  6182. ' TEnum = (Red,Blue);',
  6183. ' TEnums = set of TEnum;',
  6184. 'const',
  6185. ' Orange = red;',
  6186. 'var',
  6187. ' Enum: tenum;',
  6188. ' Enums: tenums;',
  6189. 'begin',
  6190. ' Include(enums,orange);',
  6191. ' Exclude(enums,orange);',
  6192. ' if orange in enums then;',
  6193. ' if orange in [orange,red] then;']);
  6194. ConvertProgram;
  6195. CheckSource('TestSet_EnumConst',
  6196. LinesToStr([ // statements
  6197. 'this.TEnum = {',
  6198. ' "0": "Red",',
  6199. ' Red: 0,',
  6200. ' "1": "Blue",',
  6201. ' Blue: 1',
  6202. '};',
  6203. 'this.Orange = this.TEnum.Red;',
  6204. 'this.Enum = 0;',
  6205. 'this.Enums = {};',
  6206. '']),
  6207. LinesToStr([
  6208. '$mod.Enums = rtl.includeSet($mod.Enums, $mod.TEnum.Red);',
  6209. '$mod.Enums = rtl.excludeSet($mod.Enums, $mod.TEnum.Red);',
  6210. 'if ($mod.TEnum.Red in $mod.Enums) ;',
  6211. 'if ($mod.TEnum.Red in rtl.createSet($mod.TEnum.Red, $mod.TEnum.Red)) ;',
  6212. '']));
  6213. end;
  6214. procedure TTestModule.TestSet_IntConst;
  6215. begin
  6216. StartProgram(false);
  6217. Add([
  6218. 'type',
  6219. ' TEnums = set of Byte;',
  6220. 'const',
  6221. ' Orange = 0;',
  6222. 'var',
  6223. ' Enum: byte;',
  6224. ' Enums: tenums;',
  6225. 'begin',
  6226. ' Enums:=[];',
  6227. ' Enums:=[0];',
  6228. ' Enums:=[1..2];',
  6229. //' Include(enums,orange);',
  6230. //' Exclude(enums,orange);',
  6231. ' if orange in enums then;',
  6232. ' if orange in [orange,1] then;']);
  6233. ConvertProgram;
  6234. CheckSource('TestSet_IntConst',
  6235. LinesToStr([ // statements
  6236. 'this.Orange = 0;',
  6237. 'this.Enum = 0;',
  6238. 'this.Enums = {};',
  6239. '']),
  6240. LinesToStr([
  6241. '$mod.Enums = {};',
  6242. '$mod.Enums = rtl.createSet(0);',
  6243. '$mod.Enums = rtl.createSet(null, 1, 2);',
  6244. 'if (0 in $mod.Enums) ;',
  6245. 'if (0 in rtl.createSet(0, 1)) ;',
  6246. '']));
  6247. end;
  6248. procedure TTestModule.TestSet_IntRange;
  6249. begin
  6250. StartProgram(false);
  6251. Add([
  6252. 'type',
  6253. ' TRange = 1..3;',
  6254. ' TEnums = set of TRange;',
  6255. 'const',
  6256. ' Orange = 2;',
  6257. 'var',
  6258. ' Enum: byte;',
  6259. ' Enums: TEnums;',
  6260. 'begin',
  6261. ' Enums:=[];',
  6262. ' Enums:=[1];',
  6263. ' Enums:=[2..3];',
  6264. ' Include(enums,orange);',
  6265. ' Exclude(enums,orange);',
  6266. ' if orange in enums then;',
  6267. ' if orange in [orange,1] then;']);
  6268. ConvertProgram;
  6269. CheckSource('TestSet_IntRange',
  6270. LinesToStr([ // statements
  6271. 'this.Orange = 2;',
  6272. 'this.Enum = 0;',
  6273. 'this.Enums = {};',
  6274. '']),
  6275. LinesToStr([
  6276. '$mod.Enums = {};',
  6277. '$mod.Enums = rtl.createSet(1);',
  6278. '$mod.Enums = rtl.createSet(null, 2, 3);',
  6279. '$mod.Enums = rtl.includeSet($mod.Enums, 2);',
  6280. '$mod.Enums = rtl.excludeSet($mod.Enums, 2);',
  6281. 'if (2 in $mod.Enums) ;',
  6282. 'if (2 in rtl.createSet(2, 1)) ;',
  6283. '']));
  6284. end;
  6285. procedure TTestModule.TestSet_AnonymousEnumType;
  6286. begin
  6287. StartProgram(false);
  6288. Add('type');
  6289. Add(' TFlags = set of (red, green);');
  6290. Add('const');
  6291. Add(' favorite = red;');
  6292. Add('var');
  6293. Add(' f: TFlags;');
  6294. Add(' i: longint;');
  6295. Add('begin');
  6296. Add(' Include(f,red);');
  6297. Add(' Include(f,favorite);');
  6298. Add(' i:=ord(red);');
  6299. Add(' i:=ord(favorite);');
  6300. Add(' i:=ord(low(TFlags));');
  6301. Add(' i:=ord(low(f));');
  6302. Add(' i:=ord(low(favorite));');
  6303. Add(' i:=ord(high(TFlags));');
  6304. Add(' i:=ord(high(f));');
  6305. Add(' i:=ord(high(favorite));');
  6306. Add(' f:=[green,favorite];');
  6307. ConvertProgram;
  6308. CheckSource('TestSet_AnonymousEnumType',
  6309. LinesToStr([ // statements
  6310. 'this.TFlags$a = {',
  6311. ' "0": "red",',
  6312. ' red: 0,',
  6313. ' "1": "green",',
  6314. ' green: 1',
  6315. '};',
  6316. 'this.favorite = this.TFlags$a.red;',
  6317. 'this.f = {};',
  6318. 'this.i = 0;',
  6319. '']),
  6320. LinesToStr([
  6321. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  6322. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  6323. '$mod.i = $mod.TFlags$a.red;',
  6324. '$mod.i = $mod.TFlags$a.red;',
  6325. '$mod.i = $mod.TFlags$a.red;',
  6326. '$mod.i = $mod.TFlags$a.red;',
  6327. '$mod.i = $mod.TFlags$a.red;',
  6328. '$mod.i = $mod.TFlags$a.green;',
  6329. '$mod.i = $mod.TFlags$a.green;',
  6330. '$mod.i = $mod.TFlags$a.green;',
  6331. '$mod.f = rtl.createSet($mod.TFlags$a.green, $mod.TFlags$a.red);',
  6332. '']));
  6333. end;
  6334. procedure TTestModule.TestSet_AnonymousEnumTypeChar;
  6335. begin
  6336. exit;
  6337. StartProgram(false);
  6338. Add([
  6339. 'type',
  6340. ' TAtoZ = ''A''..''Z'';',
  6341. ' TSetOfAZ = set of TAtoZ;',
  6342. 'var',
  6343. ' c: char;',
  6344. ' a: TAtoZ;',
  6345. ' s: TSetOfAZ = [''P'',''A''];',
  6346. ' i: longint;',
  6347. 'begin',
  6348. ' Include(s,''S'');',
  6349. ' Include(s,c);',
  6350. ' Include(s,a);',
  6351. ' c:=low(TAtoZ);',
  6352. ' i:=ord(low(TAtoZ));',
  6353. ' a:=high(TAtoZ);',
  6354. ' a:=high(TSetOfAtoZ);',
  6355. ' s:=[a,c,''M''];',
  6356. '']);
  6357. ConvertProgram;
  6358. CheckSource('TestSet_AnonymousEnumTypeChar',
  6359. LinesToStr([ // statements
  6360. '']),
  6361. LinesToStr([
  6362. '']));
  6363. end;
  6364. procedure TTestModule.TestSet_ConstEnum;
  6365. begin
  6366. StartProgram(false);
  6367. Add([
  6368. 'type',
  6369. ' TEnum = (red,blue,green);',
  6370. ' TEnums = set of TEnum;',
  6371. 'const',
  6372. ' teAny = [low(TEnum)..high(TEnum)];',
  6373. ' teRedBlue = [low(TEnum)..pred(high(TEnum))];',
  6374. 'var',
  6375. ' e: TEnum;',
  6376. ' s: TEnums;',
  6377. 'begin',
  6378. ' if blue in teAny then;',
  6379. ' if blue in teAny+[e] then;',
  6380. ' if blue in teAny+teRedBlue then;',
  6381. ' if e in [red,blue] then;',
  6382. ' s:=teAny;',
  6383. ' s:=teAny+[e];',
  6384. ' s:=[e]+teAny;',
  6385. ' s:=teAny+teRedBlue;',
  6386. ' s:=teAny+teRedBlue+[e];',
  6387. '']);
  6388. ConvertProgram;
  6389. CheckSource('TestSet_ConstEnum',
  6390. LinesToStr([ // statements
  6391. 'this.TEnum = {',
  6392. ' "0": "red",',
  6393. ' red: 0,',
  6394. ' "1": "blue",',
  6395. ' blue: 1,',
  6396. ' "2": "green",',
  6397. ' green: 2',
  6398. '};',
  6399. 'this.teAny = rtl.createSet(null, this.TEnum.red, this.TEnum.green);',
  6400. 'this.teRedBlue = rtl.createSet(null, this.TEnum.red, this.TEnum.green - 1);',
  6401. 'this.e = 0;',
  6402. 'this.s = {};',
  6403. '']),
  6404. LinesToStr([
  6405. 'if ($mod.TEnum.blue in $mod.teAny) ;',
  6406. 'if ($mod.TEnum.blue in rtl.unionSet($mod.teAny, rtl.createSet($mod.e))) ;',
  6407. 'if ($mod.TEnum.blue in rtl.unionSet($mod.teAny, $mod.teRedBlue)) ;',
  6408. 'if ($mod.e in rtl.createSet($mod.TEnum.red, $mod.TEnum.blue)) ;',
  6409. '$mod.s = rtl.refSet($mod.teAny);',
  6410. '$mod.s = rtl.unionSet($mod.teAny, rtl.createSet($mod.e));',
  6411. '$mod.s = rtl.unionSet(rtl.createSet($mod.e), $mod.teAny);',
  6412. '$mod.s = rtl.unionSet($mod.teAny, $mod.teRedBlue);',
  6413. '$mod.s = rtl.unionSet(rtl.unionSet($mod.teAny, $mod.teRedBlue), rtl.createSet($mod.e));',
  6414. '']));
  6415. end;
  6416. procedure TTestModule.TestSet_ConstChar;
  6417. begin
  6418. StartProgram(false);
  6419. Add([
  6420. 'const',
  6421. ' LowChars = [''a''..''z''];',
  6422. ' Chars = LowChars+[''A''..''Z''];',
  6423. ' sc = [''А'', ''Я''];',
  6424. 'var',
  6425. ' c: char;',
  6426. ' s: string;',
  6427. 'begin',
  6428. ' if c in lowchars then ;',
  6429. ' if ''a'' in lowchars then ;',
  6430. ' if s[1] in lowchars then ;',
  6431. ' if c in chars then ;',
  6432. ' if c in [''a''..''z'',''_''] then ;',
  6433. ' if ''b'' in [''a''..''z'',''_''] then ;',
  6434. ' if ''Я'' in sc then ;',
  6435. ' if 3=ord('' '') then ;',
  6436. '']);
  6437. ConvertProgram;
  6438. CheckSource('TestSet_ConstChar',
  6439. LinesToStr([ // statements
  6440. 'this.LowChars = rtl.createSet(null, 97, 122);',
  6441. 'this.Chars = rtl.unionSet(this.LowChars, rtl.createSet(null, 65, 90));',
  6442. 'this.sc = rtl.createSet(1040, 1071);',
  6443. 'this.c = "";',
  6444. 'this.s = "";',
  6445. '']),
  6446. LinesToStr([
  6447. 'if ($mod.c.charCodeAt() in $mod.LowChars) ;',
  6448. 'if (97 in $mod.LowChars) ;',
  6449. 'if ($mod.s.charCodeAt(0) in $mod.LowChars) ;',
  6450. 'if ($mod.c.charCodeAt() in $mod.Chars) ;',
  6451. 'if ($mod.c.charCodeAt() in rtl.createSet(null, 97, 122, 95)) ;',
  6452. 'if (98 in rtl.createSet(null, 97, 122, 95)) ;',
  6453. 'if (1071 in $mod.sc) ;',
  6454. 'if (3 === 32) ;',
  6455. '']));
  6456. end;
  6457. procedure TTestModule.TestSet_ConstInt;
  6458. begin
  6459. StartProgram(false);
  6460. Add([
  6461. 'const',
  6462. ' Months = [1..12];',
  6463. ' Mirror = [-12..-1]+Months;',
  6464. 'var',
  6465. ' i: smallint;',
  6466. 'begin',
  6467. ' if 3 in Months then;',
  6468. ' if i in Months+[i] then;',
  6469. ' if i in Months+Mirror then;',
  6470. ' if i in [4..6,8] then;',
  6471. '']);
  6472. ConvertProgram;
  6473. CheckSource('TestSet_ConstInt',
  6474. LinesToStr([ // statements
  6475. 'this.Months = rtl.createSet(null, 1, 12);',
  6476. 'this.Mirror = rtl.unionSet(rtl.createSet(null, -12, -1), this.Months);',
  6477. 'this.i = 0;',
  6478. '']),
  6479. LinesToStr([
  6480. 'if (3 in $mod.Months) ;',
  6481. 'if ($mod.i in rtl.unionSet($mod.Months, rtl.createSet($mod.i))) ;',
  6482. 'if ($mod.i in rtl.unionSet($mod.Months, $mod.Mirror)) ;',
  6483. 'if ($mod.i in rtl.createSet(null, 4, 6, 8)) ;',
  6484. '']));
  6485. end;
  6486. procedure TTestModule.TestSet_InFunction;
  6487. begin
  6488. StartProgram(false);
  6489. Add([
  6490. 'const',
  6491. ' TEnum = 3;',
  6492. ' TSetOfEnum = 4;',
  6493. ' TSetOfAno = 5;',
  6494. 'procedure DoIt;',
  6495. 'type',
  6496. ' TEnum = (red, blue);',
  6497. ' TSetOfEnum = set of TEnum;',
  6498. ' TSetOfAno = set of (up,down);',
  6499. 'var',
  6500. ' e: TEnum;',
  6501. ' se: TSetOfEnum;',
  6502. ' sa: TSetOfAno;',
  6503. 'begin',
  6504. ' se:=[e];',
  6505. ' sa:=[up];',
  6506. 'end;',
  6507. 'begin',
  6508. '']);
  6509. ConvertProgram;
  6510. CheckSource('TestSet_InFunction',
  6511. LinesToStr([ // statements
  6512. 'this.TEnum = 3;',
  6513. 'this.TSetOfEnum = 4;',
  6514. 'this.TSetOfAno = 5;',
  6515. 'var TEnum$1 = {',
  6516. ' "0": "red",',
  6517. ' red: 0,',
  6518. ' "1": "blue",',
  6519. ' blue: 1',
  6520. '};',
  6521. 'var TSetOfAno$a = {',
  6522. ' "0": "up",',
  6523. ' up: 0,',
  6524. ' "1": "down",',
  6525. ' down: 1',
  6526. '};',
  6527. 'this.DoIt = function () {',
  6528. ' var e = 0;',
  6529. ' var se = {};',
  6530. ' var sa = {};',
  6531. ' se = rtl.createSet(e);',
  6532. ' sa = rtl.createSet(TSetOfAno$a.up);',
  6533. '};',
  6534. '']),
  6535. LinesToStr([
  6536. '']));
  6537. end;
  6538. procedure TTestModule.TestSet_ForIn;
  6539. begin
  6540. StartProgram(false);
  6541. Add([
  6542. 'type',
  6543. ' TEnum = (Red, Green, Blue);',
  6544. ' TEnumRg = green..blue;',
  6545. ' TSetOfEnum = set of TEnum;',
  6546. ' TSetOfEnumRg = set of TEnumRg;',
  6547. 'var',
  6548. ' e, e2: TEnum;',
  6549. ' er: TEnum;',
  6550. ' s: TSetOfEnum;',
  6551. 'begin',
  6552. ' for e in TSetOfEnum do ;',
  6553. ' for e in TSetOfEnumRg do ;',
  6554. ' for e in [] do e2:=e;',
  6555. ' for e in [red..green] do e2:=e;',
  6556. ' for e in [green,blue] do e2:=e;',
  6557. ' for e in [red,blue] do e2:=e;',
  6558. ' for e in s do e2:=e;',
  6559. ' for er in TSetOfEnumRg do ;',
  6560. '']);
  6561. ConvertProgram;
  6562. CheckSource('TestSet_ForIn',
  6563. LinesToStr([ // statements
  6564. 'this.TEnum = {',
  6565. ' "0":"Red",',
  6566. ' Red:0,',
  6567. ' "1":"Green",',
  6568. ' Green:1,',
  6569. ' "2":"Blue",',
  6570. ' Blue:2',
  6571. ' };',
  6572. 'this.e = 0;',
  6573. 'this.e2 = 0;',
  6574. 'this.er = 0;',
  6575. 'this.s = {};',
  6576. '']),
  6577. LinesToStr([
  6578. 'for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  6579. 'for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  6580. 'for ($mod.e = 0; $mod.e <= 1; $mod.e++) $mod.e2 = $mod.e;',
  6581. 'for ($mod.e = 1; $mod.e <= 2; $mod.e++) $mod.e2 = $mod.e;',
  6582. 'for ($mod.e in rtl.createSet($mod.TEnum.Red, $mod.TEnum.Blue)) $mod.e2 = $mod.e;',
  6583. 'for (var $l in $mod.s){',
  6584. ' $mod.e = +$l;',
  6585. ' $mod.e2 = $mod.e;',
  6586. '};',
  6587. 'for ($mod.er = 1; $mod.er <= 2; $mod.er++) ;',
  6588. '']));
  6589. end;
  6590. procedure TTestModule.TestNestBegin;
  6591. begin
  6592. StartProgram(false);
  6593. Add('begin');
  6594. Add(' begin');
  6595. Add(' begin');
  6596. Add(' end;');
  6597. Add(' begin');
  6598. Add(' if true then ;');
  6599. Add(' end;');
  6600. Add(' end;');
  6601. ConvertProgram;
  6602. CheckSource('TestNestBegin',
  6603. '',
  6604. 'if (true) ;');
  6605. end;
  6606. procedure TTestModule.TestUnitImplVars;
  6607. begin
  6608. StartUnit(false);
  6609. Add('interface');
  6610. Add('implementation');
  6611. Add('var');
  6612. Add(' V1:longint;');
  6613. Add(' V2:longint = 3;');
  6614. Add(' V3:string = ''abc'';');
  6615. ConvertUnit;
  6616. CheckSource('TestUnitImplVars',
  6617. LinesToStr([ // statements
  6618. 'var $impl = $mod.$impl;',
  6619. '']),
  6620. '', // this.$init
  6621. LinesToStr([ // implementation
  6622. '$impl.V1 = 0;',
  6623. '$impl.V2 = 3;',
  6624. '$impl.V3 = "abc";',
  6625. '']) );
  6626. end;
  6627. procedure TTestModule.TestUnitImplConsts;
  6628. begin
  6629. StartUnit(false);
  6630. Add('interface');
  6631. Add('implementation');
  6632. Add('const');
  6633. Add(' v1 = 3;');
  6634. Add(' v2:longint = 4;');
  6635. Add(' v3:string = ''abc'';');
  6636. ConvertUnit;
  6637. CheckSource('TestUnitImplConsts',
  6638. LinesToStr([ // statements
  6639. 'var $impl = $mod.$impl;',
  6640. '']),
  6641. '', // this.$init
  6642. LinesToStr([ // implementation
  6643. '$impl.v1 = 3;',
  6644. '$impl.v2 = 4;',
  6645. '$impl.v3 = "abc";',
  6646. '']) );
  6647. end;
  6648. procedure TTestModule.TestUnitImplRecord;
  6649. begin
  6650. StartUnit(false);
  6651. Add('interface');
  6652. Add('implementation');
  6653. Add('type');
  6654. Add(' TMyRecord = record');
  6655. Add(' i: longint;');
  6656. Add(' end;');
  6657. Add('var aRec: TMyRecord;');
  6658. Add('initialization');
  6659. Add(' arec.i:=3;');
  6660. ConvertUnit;
  6661. CheckSource('TestUnitImplRecord',
  6662. LinesToStr([ // statements
  6663. 'var $impl = $mod.$impl;',
  6664. '']),
  6665. // this.$init
  6666. '$impl.aRec.i = 3;',
  6667. LinesToStr([ // implementation
  6668. 'rtl.recNewT($impl, "TMyRecord", function () {',
  6669. ' this.i = 0;',
  6670. ' this.$eq = function (b) {',
  6671. ' return this.i === b.i;',
  6672. ' };',
  6673. ' this.$assign = function (s) {',
  6674. ' this.i = s.i;',
  6675. ' return this;',
  6676. ' };',
  6677. '});',
  6678. '$impl.aRec = $impl.TMyRecord.$new();',
  6679. '']) );
  6680. end;
  6681. procedure TTestModule.TestRenameJSNameConflict;
  6682. begin
  6683. StartProgram(false);
  6684. Add('var apply: longint;');
  6685. Add('var bind: longint;');
  6686. Add('var call: longint;');
  6687. Add('begin');
  6688. ConvertProgram;
  6689. CheckSource('TestRenameJSNameConflict',
  6690. LinesToStr([ // statements
  6691. 'this.Apply = 0;',
  6692. 'this.Bind = 0;',
  6693. 'this.Call = 0;'
  6694. ]),
  6695. LinesToStr([ // this.$main
  6696. ''
  6697. ]));
  6698. end;
  6699. procedure TTestModule.TestLocalConst;
  6700. begin
  6701. StartProgram(false);
  6702. Add('procedure DoIt;');
  6703. Add('const');
  6704. Add(' cA: longint = 1;');
  6705. Add(' cB = 2;');
  6706. Add(' procedure Sub;');
  6707. Add(' const');
  6708. Add(' csA = 3;');
  6709. Add(' cB: double = 4;');
  6710. Add(' begin');
  6711. Add(' cb:=cb+csa;');
  6712. Add(' ca:=ca+csa+5;');
  6713. Add(' end;');
  6714. Add('begin');
  6715. Add(' ca:=ca+cb+6;');
  6716. Add('end;');
  6717. Add('begin');
  6718. ConvertProgram;
  6719. CheckSource('TestLocalConst',
  6720. LinesToStr([
  6721. 'var cA = 1;',
  6722. 'var cB = 2;',
  6723. 'var csA = 3;',
  6724. 'var cB$1 = 4;',
  6725. 'this.DoIt = function () {',
  6726. ' function Sub() {',
  6727. ' cB$1 = cB$1 + 3;',
  6728. ' cA = cA + 3 + 5;',
  6729. ' };',
  6730. ' cA = cA + 2 + 6;',
  6731. '};'
  6732. ]),
  6733. LinesToStr([
  6734. ]));
  6735. end;
  6736. procedure TTestModule.TestVarExternal;
  6737. begin
  6738. StartProgram(false);
  6739. Add('var');
  6740. Add(' NaN: double; external name ''Global.NaN'';');
  6741. Add(' d: double;');
  6742. Add('begin');
  6743. Add(' d:=NaN;');
  6744. ConvertProgram;
  6745. CheckSource('TestVarExternal',
  6746. LinesToStr([
  6747. 'this.d = 0.0;'
  6748. ]),
  6749. LinesToStr([
  6750. '$mod.d = Global.NaN;'
  6751. ]));
  6752. end;
  6753. procedure TTestModule.TestVarExternalOtherUnit;
  6754. begin
  6755. AddModuleWithIntfImplSrc('unit2.pas',
  6756. LinesToStr([
  6757. 'var NaN: double; external name ''Global.NaN'';',
  6758. 'var iV: longint;'
  6759. ]),
  6760. '');
  6761. StartUnit(true);
  6762. Add('interface');
  6763. Add('uses unit2;');
  6764. Add('implementation');
  6765. Add('var');
  6766. Add(' d: double;');
  6767. Add(' i: longint; external name ''$i'';');
  6768. Add('begin');
  6769. Add(' d:=nan;');
  6770. Add(' d:=uNit2.nan;');
  6771. Add(' d:=test1.d;');
  6772. Add(' i:=iv;');
  6773. Add(' i:=uNit2.iv;');
  6774. Add(' i:=test1.i;');
  6775. ConvertUnit;
  6776. CheckSource('TestVarExternalOtherUnit',
  6777. LinesToStr([
  6778. 'var $impl = $mod.$impl;',
  6779. '']),
  6780. LinesToStr([ // this.$init
  6781. '$impl.d = Global.NaN;',
  6782. '$impl.d = Global.NaN;',
  6783. '$impl.d = $impl.d;',
  6784. '$i = pas.unit2.iV;',
  6785. '$i = pas.unit2.iV;',
  6786. '$i = $i;',
  6787. '']),
  6788. LinesToStr([ // implementation
  6789. '$impl.d = 0.0;',
  6790. '']) );
  6791. end;
  6792. procedure TTestModule.TestVarAbsoluteFail;
  6793. begin
  6794. StartProgram(false);
  6795. Add([
  6796. 'var',
  6797. ' a: longint;',
  6798. ' b: longword absolute a;',
  6799. 'begin']);
  6800. SetExpectedPasResolverError('Invalid variable modifier "absolute"',nInvalidVariableModifier);
  6801. ConvertProgram;
  6802. end;
  6803. procedure TTestModule.TestConstExternal;
  6804. begin
  6805. StartProgram(false);
  6806. Add([
  6807. 'const',
  6808. ' PI: double; external name ''Global.PI'';',
  6809. ' Tau = 2*pi;',
  6810. 'var d: double;',
  6811. 'begin',
  6812. ' d:=pi;',
  6813. ' d:=tau+pi;']);
  6814. ConvertProgram;
  6815. CheckSource('TestConstExternal',
  6816. LinesToStr([
  6817. 'this.Tau = 2*Global.PI;',
  6818. 'this.d = 0.0;'
  6819. ]),
  6820. LinesToStr([
  6821. '$mod.d = Global.PI;',
  6822. '$mod.d = $mod.Tau + Global.PI;'
  6823. ]));
  6824. end;
  6825. procedure TTestModule.TestDouble;
  6826. begin
  6827. StartProgram(false);
  6828. Add([
  6829. 'type',
  6830. ' TDateTime = double;',
  6831. 'const',
  6832. ' a = TDateTime(2.7);',
  6833. ' b = a + TDateTime(1.7);',
  6834. ' c = 0.9 + 0.1;',
  6835. ' f0_1 = 0.1;',
  6836. ' f0_3 = 0.3;',
  6837. ' fn0_1 = -0.1;',
  6838. ' fn0_3 = -0.3;',
  6839. ' fn0_003 = -0.003;',
  6840. ' fn0_123456789 = -0.123456789;',
  6841. ' fn300_0 = -300.0;',
  6842. ' fn123456_0 = -123456.0;',
  6843. ' fn1234567_8 = -1234567.8;',
  6844. ' fn12345678_9 = -12345678.9;',
  6845. ' f1_0En12 = 1E-12;',
  6846. ' fn1_0En12 = -1E-12;',
  6847. ' maxdouble = 1.7e+308;',
  6848. ' mindouble = -1.7e+308;',
  6849. ' MinSafeIntDouble = -$1fffffffffffff;',
  6850. ' MinSafeIntDouble2 = -$20000000000000-1;',
  6851. ' MaxSafeIntDouble = $1fffffffffffff;',
  6852. ' DZeroResolution = 1E-12;',
  6853. ' Minus1 = -1E-12;',
  6854. ' EPS = 1E-9;',
  6855. ' DELTA = 0.001;',
  6856. ' Big = 129.789E+100;',
  6857. ' Test0_15 = 0.15;',
  6858. ' Test999 = 2.9999999999999;',
  6859. ' Test111999 = 211199999999999000.0;',
  6860. ' TestMinus111999 = -211199999999999000.0;',
  6861. ' Inf = 1.0 / 0.0;',
  6862. ' NegInf = -1.0 / 0.0;',
  6863. 'procedure Run(d: double); external name ''Run'';',
  6864. 'var',
  6865. ' d: double = b;',
  6866. 'begin',
  6867. ' d:=1.0;',
  6868. ' d:=1.0/3.0;',
  6869. ' d:=1.0/(3-2-1);',
  6870. ' d:=1/3;',
  6871. ' d:=5.0E-324;',
  6872. ' d:=1.7E308;',
  6873. ' d:=001.00E00;',
  6874. ' d:=002.00E001;',
  6875. ' d:=003.000E000;',
  6876. ' d:=-004.00E-00;',
  6877. ' d:=-005.00E-001;',
  6878. ' d:=10**3;',
  6879. ' d:=10 mod 3;',
  6880. ' d:=10 div 3;',
  6881. ' d:=c;',
  6882. ' d:=f0_1;',
  6883. ' d:=f0_3;',
  6884. ' d:=fn0_1;',
  6885. ' d:=fn0_3;',
  6886. ' d:=fn0_003;',
  6887. ' d:=fn0_123456789;',
  6888. ' d:=fn300_0;',
  6889. ' d:=fn123456_0;',
  6890. ' d:=fn1234567_8;',
  6891. ' d:=fn12345678_9;',
  6892. ' d:=f1_0En12;',
  6893. ' d:=fn1_0En12;',
  6894. ' d:=maxdouble;',
  6895. ' d:=mindouble;',
  6896. ' d:=MinSafeIntDouble;',
  6897. ' d:=double(MinSafeIntDouble);',
  6898. ' d:=MinSafeIntDouble2;',
  6899. ' d:=double(MinSafeIntDouble2);',
  6900. ' d:=MaxSafeIntDouble;',
  6901. ' d:=default(double);',
  6902. ' Run(Inf);',
  6903. ' Run(NegInf);',
  6904. '']);
  6905. ConvertProgram;
  6906. CheckSource('TestDouble',
  6907. LinesToStr([
  6908. 'this.a = 2.7;',
  6909. 'this.b = 2.7 + 1.7;',
  6910. 'this.c = 0.9 + 0.1;',
  6911. 'this.f0_1 = 0.1;',
  6912. 'this.f0_3 = 0.3;',
  6913. 'this.fn0_1 = -0.1;',
  6914. 'this.fn0_3 = -0.3;',
  6915. 'this.fn0_003 = -0.003;',
  6916. 'this.fn0_123456789 = -0.123456789;',
  6917. 'this.fn300_0 = -300.0;',
  6918. 'this.fn123456_0 = -123456.0;',
  6919. 'this.fn1234567_8 = -1234567.8;',
  6920. 'this.fn12345678_9 = -12345678.9;',
  6921. 'this.f1_0En12 = 1E-12;',
  6922. 'this.fn1_0En12 = -1E-12;',
  6923. 'this.maxdouble = 1.7e+308;',
  6924. 'this.mindouble = -1.7e+308;',
  6925. 'this.MinSafeIntDouble = -0x1fffffffffffff;',
  6926. 'this.MinSafeIntDouble2 = -0x20000000000000 - 1;',
  6927. 'this.MaxSafeIntDouble = 0x1fffffffffffff;',
  6928. 'this.DZeroResolution = 1E-12;',
  6929. 'this.Minus1 = -1E-12;',
  6930. 'this.EPS = 1E-9;',
  6931. 'this.DELTA = 0.001;',
  6932. 'this.Big = 129.789E+100;',
  6933. 'this.Test0_15 = 0.15;',
  6934. 'this.Test999 = 2.9999999999999;',
  6935. 'this.Test111999 = 211199999999999000.0;',
  6936. 'this.TestMinus111999 = -211199999999999000.0;',
  6937. 'this.Inf = 1.0 / 0.0;',
  6938. 'this.NegInf = -1.0 / 0.0;',
  6939. 'this.d = 4.4;',
  6940. '']),
  6941. LinesToStr([
  6942. '$mod.d = 1.0;',
  6943. '$mod.d = 1.0 / 3.0;',
  6944. '$mod.d = 1.0 / (3 - 2 - 1);',
  6945. '$mod.d = 1 / 3;',
  6946. '$mod.d = 5.0E-324;',
  6947. '$mod.d = 1.7E308;',
  6948. '$mod.d = 1.00E0;',
  6949. '$mod.d = 2.00E1;',
  6950. '$mod.d = 3.000E0;',
  6951. '$mod.d = -4.00E-0;',
  6952. '$mod.d = -5.00E-1;',
  6953. '$mod.d = Math.pow(10, 3);',
  6954. '$mod.d = 10 % 3;',
  6955. '$mod.d = rtl.trunc(10 / 3);',
  6956. '$mod.d = 1;',
  6957. '$mod.d = 0.1;',
  6958. '$mod.d = 0.3;',
  6959. '$mod.d = -0.1;',
  6960. '$mod.d = -0.3;',
  6961. '$mod.d = -0.003;',
  6962. '$mod.d = -0.123456789;',
  6963. '$mod.d = -300;',
  6964. '$mod.d = -123456;',
  6965. '$mod.d = -1234567.8;',
  6966. '$mod.d = -1.23456789E7;',
  6967. '$mod.d = 1E-12;',
  6968. '$mod.d = -1E-12;',
  6969. '$mod.d = 1.7E308;',
  6970. '$mod.d = -1.7E308;',
  6971. '$mod.d = -9007199254740991;',
  6972. '$mod.d = -9007199254740991;',
  6973. '$mod.d = -9.007199254740992E15;',
  6974. '$mod.d = -9.007199254740992E15;',
  6975. '$mod.d = 9007199254740991;',
  6976. '$mod.d = 0.0;',
  6977. 'Run(1 / 0);',
  6978. 'Run(-1 / 0);',
  6979. '']));
  6980. end;
  6981. procedure TTestModule.TestInteger;
  6982. begin
  6983. StartProgram(false);
  6984. Add([
  6985. 'const',
  6986. ' MinInt = low(NativeInt);',
  6987. ' MaxInt = high(NativeInt);',
  6988. 'type',
  6989. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  6990. 'const',
  6991. ' a = low(TMyInt)+High(TMyInt);',
  6992. 'var',
  6993. ' i: TMyInt;',
  6994. 'begin',
  6995. ' i:=-MinInt;',
  6996. ' i:=default(TMyInt);',
  6997. ' i:=low(i)+high(i);',
  6998. '']);
  6999. ConvertProgram;
  7000. CheckSource('TestIntegerRange',
  7001. LinesToStr([
  7002. 'this.MinInt = -9007199254740991;',
  7003. 'this.MaxInt = 9007199254740991;',
  7004. 'this.a = -9007199254740991 + 9007199254740991;',
  7005. 'this.i = 0;',
  7006. '']),
  7007. LinesToStr([
  7008. '$mod.i = - -9007199254740991;',
  7009. '$mod.i = -9007199254740991;',
  7010. '$mod.i = -9007199254740991 + 9007199254740991;',
  7011. '']));
  7012. end;
  7013. procedure TTestModule.TestIntegerRange;
  7014. begin
  7015. StartProgram(false);
  7016. Add([
  7017. 'const',
  7018. ' MinInt = -1;',
  7019. ' MaxInt = +1;',
  7020. 'type',
  7021. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  7022. ' TInt2 = 1..3;',
  7023. 'const',
  7024. ' a = low(TMyInt)+High(TMyInt);',
  7025. ' b = low(TInt2)+High(TInt2);',
  7026. ' s1 = [1];',
  7027. ' s2 = [1,2];',
  7028. ' s3 = [1..3];',
  7029. ' s4 = [low(shortint)..high(shortint)];',
  7030. ' s5 = [succ(low(shortint))..pred(high(shortint))];',
  7031. ' s6 = 1 in s2;',
  7032. 'var',
  7033. ' i: TMyInt;',
  7034. ' i2: TInt2;',
  7035. 'begin',
  7036. ' i:=i2;',
  7037. ' i:=default(TMyInt);',
  7038. ' if i=i2 then ;',
  7039. ' i:=ord(i2);',
  7040. '']);
  7041. ConvertProgram;
  7042. CheckSource('TestIntegerRange',
  7043. LinesToStr([
  7044. 'this.MinInt = -1;',
  7045. 'this.MaxInt = +1;',
  7046. 'this.a = -1 + 1;',
  7047. 'this.b = 1 + 3;',
  7048. 'this.s1 = rtl.createSet(1);',
  7049. 'this.s2 = rtl.createSet(1, 2);',
  7050. 'this.s3 = rtl.createSet(null, 1, 3);',
  7051. 'this.s4 = rtl.createSet(null, -128, 127);',
  7052. 'this.s5 = rtl.createSet(null, -128 + 1, 127 - 1);',
  7053. 'this.s6 = 1 in this.s2;',
  7054. 'this.i = 0;',
  7055. 'this.i2 = 0;',
  7056. '']),
  7057. LinesToStr([
  7058. '$mod.i = $mod.i2;',
  7059. '$mod.i = -1;',
  7060. 'if ($mod.i === $mod.i2) ;',
  7061. '$mod.i = $mod.i2;',
  7062. '']));
  7063. end;
  7064. procedure TTestModule.TestIntegerTypecasts;
  7065. begin
  7066. StartProgram(false);
  7067. Add([
  7068. 'var',
  7069. ' i: nativeint;',
  7070. ' b: byte;',
  7071. ' sh: shortint;',
  7072. ' w: word;',
  7073. ' sm: smallint;',
  7074. ' lw: longword;',
  7075. ' li: longint;',
  7076. 'begin',
  7077. ' b:=byte(i);',
  7078. ' sh:=shortint(i);',
  7079. ' w:=word(i);',
  7080. ' sm:=smallint(i);',
  7081. ' lw:=longword(i);',
  7082. ' li:=longint(i);',
  7083. '']);
  7084. ConvertProgram;
  7085. CheckSource('TestIntegerTypecasts',
  7086. LinesToStr([
  7087. 'this.i = 0;',
  7088. 'this.b = 0;',
  7089. 'this.sh = 0;',
  7090. 'this.w = 0;',
  7091. 'this.sm = 0;',
  7092. 'this.lw = 0;',
  7093. 'this.li = 0;',
  7094. '']),
  7095. LinesToStr([
  7096. '$mod.b = $mod.i & 255;',
  7097. '$mod.sh = (($mod.i & 255) << 24) >> 24;',
  7098. '$mod.w = $mod.i & 65535;',
  7099. '$mod.sm = (($mod.i & 65535) << 16) >> 16;',
  7100. '$mod.lw = $mod.i >>> 0;',
  7101. '$mod.li = $mod.i & 0xFFFFFFFF;',
  7102. '']));
  7103. end;
  7104. procedure TTestModule.TestInteger_BitwiseShrNativeInt;
  7105. begin
  7106. StartProgram(false);
  7107. Add([
  7108. 'var',
  7109. ' i,j: nativeint;',
  7110. 'begin',
  7111. ' i:=i shr 0;',
  7112. ' i:=i shr 1;',
  7113. ' i:=i shr 3;',
  7114. ' i:=i shr 54;',
  7115. ' i:=j shr i;',
  7116. '']);
  7117. ConvertProgram;
  7118. CheckResolverUnexpectedHints;
  7119. CheckSource('TestInteger_BitwiseShrNativeInt',
  7120. LinesToStr([
  7121. 'this.i = 0;',
  7122. 'this.j = 0;',
  7123. '']),
  7124. LinesToStr([
  7125. '$mod.i = $mod.i;',
  7126. '$mod.i = Math.floor($mod.i / 2);',
  7127. '$mod.i = Math.floor($mod.i / 8);',
  7128. '$mod.i = 0;',
  7129. '$mod.i = rtl.shr($mod.j, $mod.i);',
  7130. '']));
  7131. end;
  7132. procedure TTestModule.TestInteger_BitwiseShlNativeInt;
  7133. begin
  7134. StartProgram(false);
  7135. Add([
  7136. 'var',
  7137. ' i: nativeint;',
  7138. 'begin',
  7139. ' i:=i shl 0;',
  7140. ' i:=i shl 54;',
  7141. ' i:=123456789012 shl 1;',
  7142. ' i:=i shl 1;',
  7143. '']);
  7144. ConvertProgram;
  7145. CheckResolverUnexpectedHints;
  7146. CheckSource('TestInteger_BitwiseShrNativeInt',
  7147. LinesToStr([
  7148. 'this.i = 0;',
  7149. '']),
  7150. LinesToStr([
  7151. '$mod.i = $mod.i;',
  7152. '$mod.i = 0;',
  7153. '$mod.i = 246913578024;',
  7154. '$mod.i = rtl.shl($mod.i, 1);',
  7155. '']));
  7156. end;
  7157. procedure TTestModule.TestInteger_SystemFunc;
  7158. begin
  7159. StartProgram(true);
  7160. Add([
  7161. 'var',
  7162. ' i: byte;',
  7163. ' s: string;',
  7164. 'begin',
  7165. ' system.inc(i);',
  7166. ' system.str(i,s);',
  7167. ' s:=system.str(i);',
  7168. ' i:=system.low(i);',
  7169. ' i:=system.high(i);',
  7170. ' i:=system.pred(i);',
  7171. ' i:=system.succ(i);',
  7172. ' i:=system.ord(i);',
  7173. '']);
  7174. ConvertProgram;
  7175. CheckResolverUnexpectedHints;
  7176. CheckSource('TestInteger_SystemFunc',
  7177. LinesToStr([
  7178. 'this.i = 0;',
  7179. 'this.s = "";',
  7180. '']),
  7181. LinesToStr([
  7182. '$mod.i += 1;',
  7183. '$mod.s = "" + $mod.i;',
  7184. '$mod.s = "" + $mod.i;',
  7185. '$mod.i = 0;',
  7186. '$mod.i = 255;',
  7187. '$mod.i = $mod.i - 1;',
  7188. '$mod.i = $mod.i + 1;',
  7189. '$mod.i = $mod.i;',
  7190. '']));
  7191. end;
  7192. procedure TTestModule.TestInteger_AssignOutsideConst;
  7193. begin
  7194. StartProgram(false);
  7195. Add([
  7196. 'const',
  7197. ' MinInt = low(longint);',
  7198. ' MaxInt = high(longint);',
  7199. 'type',
  7200. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  7201. 'var',
  7202. ' i: TMyInt;',
  7203. ' aByte: byte;',
  7204. ' aShortInt: shortint;',
  7205. ' aWord: word;',
  7206. ' aSmallInt: smallint;',
  7207. ' aLongWord: longword;',
  7208. ' aLongInt: longint;',
  7209. ' aNativeInt: nativeint;',
  7210. ' aNativeUInt: nativeuint;',
  7211. 'begin',
  7212. ' aByte:=$FF;',
  7213. ' aByte:=$100;',
  7214. ' aByte:=-1;',
  7215. ' aByte:=-127;',
  7216. ' aByte:=-128;',
  7217. ' aByte:=-254;',
  7218. ' aByte:=-255;',
  7219. ' aByte:=-256;',
  7220. ' aShortInt:=127;',
  7221. ' aShortInt:=128;',
  7222. ' aShortInt:=-128;',
  7223. ' aShortInt:=-129;',
  7224. ' aWord:=$ffff;',
  7225. ' aWord:=$10000;',
  7226. ' aWord:=-1;',
  7227. ' aWord:=-$ffff;',
  7228. ' aWord:=-$10000;',
  7229. ' aWord:=-$10001;',
  7230. ' aSmallInt:=$7fff;',
  7231. ' aSmallInt:=$8000;',
  7232. ' aSmallInt:=-$8000;',
  7233. ' aSmallInt:=-$8001;',
  7234. ' aLongWord:=$ffffffff;',
  7235. ' aLongWord:=$100000000;',
  7236. ' aLongWord:=-1;',
  7237. ' aLongWord:=-$ffffffff;',
  7238. ' aNativeInt:=$1fffffffffffff;',
  7239. ' aNativeInt:=-$1fffffffffffff;',
  7240. ' aNativeUInt:=$1fffffffffffff;',
  7241. ' aNativeUInt:=-$1fffffffffffff;',
  7242. '']);
  7243. ConvertProgram;
  7244. CheckSource('TestInteger_AssignOutsideConst',
  7245. LinesToStr([
  7246. 'this.MinInt = -2147483648;',
  7247. 'this.MaxInt = 2147483647;',
  7248. 'this.i = 0;',
  7249. 'this.aByte = 0;',
  7250. 'this.aShortInt = 0;',
  7251. 'this.aWord = 0;',
  7252. 'this.aSmallInt = 0;',
  7253. 'this.aLongWord = 0;',
  7254. 'this.aLongInt = 0;',
  7255. 'this.aNativeInt = 0;',
  7256. 'this.aNativeUInt = 0;',
  7257. '']),
  7258. LinesToStr([
  7259. '$mod.aByte = 0xFF;',
  7260. '$mod.aByte = 0;',
  7261. '$mod.aByte = 255;',
  7262. '$mod.aByte = 129;',
  7263. '$mod.aByte = 128;',
  7264. '$mod.aByte = 2;',
  7265. '$mod.aByte = 1;',
  7266. '$mod.aByte = 0;',
  7267. '$mod.aShortInt = 127;',
  7268. '$mod.aShortInt = -128;',
  7269. '$mod.aShortInt = -128;',
  7270. '$mod.aShortInt = 127;',
  7271. '$mod.aWord = 0xffff;',
  7272. '$mod.aWord = 0;',
  7273. '$mod.aWord = 65535;',
  7274. '$mod.aWord = 1;',
  7275. '$mod.aWord = 0;',
  7276. '$mod.aWord = 65535;',
  7277. '$mod.aSmallInt = 0x7fff;',
  7278. '$mod.aSmallInt = -32768;',
  7279. '$mod.aSmallInt = -0x8000;',
  7280. '$mod.aSmallInt = 32767;',
  7281. '$mod.aLongWord = 0xffffffff;',
  7282. '$mod.aLongWord = 0;',
  7283. '$mod.aLongWord = 4294967295;',
  7284. '$mod.aLongWord = 1;',
  7285. '$mod.aNativeInt = 0x1fffffffffffff;',
  7286. '$mod.aNativeInt = -0x1fffffffffffff;',
  7287. '$mod.aNativeUInt = 0x1fffffffffffff;',
  7288. '$mod.aNativeUInt = 1;',
  7289. '']));
  7290. end;
  7291. procedure TTestModule.TestCurrency;
  7292. begin
  7293. StartProgram(false);
  7294. Add([
  7295. 'type',
  7296. ' TCoin = currency;',
  7297. 'const',
  7298. ' a = TCoin(2.7);',
  7299. ' b = a + TCoin(1.7);',
  7300. ' MinSafeIntCurrency: TCoin = -92233720368.5477;',
  7301. ' MaxSafeIntCurrency: TCoin = 92233720368.5477;',
  7302. 'var',
  7303. ' c: TCoin = b;',
  7304. ' i: nativeint;',
  7305. ' d: double;',
  7306. ' j: jsvalue;',
  7307. 'function DoIt(c: currency): currency; begin end;',
  7308. 'function GetIt(d: double): double; begin end;',
  7309. 'procedure Write(v: jsvalue); begin end;',
  7310. 'begin',
  7311. ' c:=1.0;',
  7312. ' c:=0.1;',
  7313. ' c:=1.0/3.0;',
  7314. ' c:=1/3;',
  7315. ' c:=a;',
  7316. ' d:=c;',
  7317. ' c:=d;',
  7318. ' c:=currency(c);',
  7319. ' c:=currency(d);',
  7320. ' d:=double(c);',
  7321. ' c:=i;',
  7322. ' c:=currency(i);',
  7323. //' i:=c;', not allowed
  7324. ' i:=nativeint(c);',
  7325. ' c:=c+a;',
  7326. ' c:=-c-a;',
  7327. ' c:=d+c;',
  7328. ' c:=c+d;',
  7329. ' c:=d-c;',
  7330. ' c:=c-d;',
  7331. ' c:=c*a;',
  7332. ' c:=a*c;',
  7333. ' c:=d*c;',
  7334. ' c:=c*d;',
  7335. ' c:=c/a;',
  7336. ' c:=a/c;',
  7337. ' c:=d/c;',
  7338. ' c:=c/d;',
  7339. ' c:=c**a;',
  7340. ' c:=a**c;',
  7341. ' c:=d**c;',
  7342. ' c:=c**d;',
  7343. ' if c=c then ;',
  7344. ' if c=a then ;',
  7345. ' if a=c then ;',
  7346. ' if d=c then ;',
  7347. ' if c=d then ;',
  7348. ' c:=DoIt(c);',
  7349. ' c:=DoIt(i);',
  7350. ' c:=DoIt(d);',
  7351. ' c:=GetIt(c);',
  7352. ' j:=c;',
  7353. ' Write(c);',
  7354. ' c:=default(currency);',
  7355. ' j:=str(c);',
  7356. ' j:=str(c:0:3);',
  7357. '']);
  7358. ConvertProgram;
  7359. CheckSource('TestCurrency',
  7360. LinesToStr([
  7361. 'this.a = 27000;',
  7362. 'this.b = this.a + 17000;',
  7363. 'this.MinSafeIntCurrency = -92233720368.5477;',
  7364. 'this.MaxSafeIntCurrency = 92233720368.5477;',
  7365. 'this.c = this.b;',
  7366. 'this.i = 0;',
  7367. 'this.d = 0.0;',
  7368. 'this.j = undefined;',
  7369. 'this.DoIt = function (c) {',
  7370. ' var Result = 0;',
  7371. ' return Result;',
  7372. '};',
  7373. 'this.GetIt = function (d) {',
  7374. ' var Result = 0.0;',
  7375. ' return Result;',
  7376. '};',
  7377. 'this.Write = function (v) {',
  7378. '};',
  7379. '']),
  7380. LinesToStr([
  7381. '$mod.c = 10000;',
  7382. '$mod.c = 1000;',
  7383. '$mod.c = rtl.trunc((1.0 / 3.0) * 10000);',
  7384. '$mod.c = rtl.trunc((1 / 3) * 10000);',
  7385. '$mod.c = $mod.a;',
  7386. '$mod.d = $mod.c / 10000;',
  7387. '$mod.c = rtl.trunc($mod.d * 10000);',
  7388. '$mod.c = $mod.c;',
  7389. '$mod.c = $mod.d * 10000;',
  7390. '$mod.d = $mod.c / 10000;',
  7391. '$mod.c = $mod.i * 10000;',
  7392. '$mod.c = $mod.i * 10000;',
  7393. '$mod.i = rtl.trunc($mod.c / 10000);',
  7394. '$mod.c = $mod.c + $mod.a;',
  7395. '$mod.c = -$mod.c - $mod.a;',
  7396. '$mod.c = ($mod.d * 10000) + $mod.c;',
  7397. '$mod.c = $mod.c + ($mod.d * 10000);',
  7398. '$mod.c = ($mod.d * 10000) - $mod.c;',
  7399. '$mod.c = $mod.c - ($mod.d * 10000);',
  7400. '$mod.c = ($mod.c * $mod.a) / 10000;',
  7401. '$mod.c = ($mod.a * $mod.c) / 10000;',
  7402. '$mod.c = $mod.d * $mod.c;',
  7403. '$mod.c = $mod.c * $mod.d;',
  7404. '$mod.c = rtl.trunc(($mod.c / $mod.a) * 10000);',
  7405. '$mod.c = rtl.trunc(($mod.a / $mod.c) * 10000);',
  7406. '$mod.c = rtl.trunc($mod.d / $mod.c);',
  7407. '$mod.c = rtl.trunc($mod.c / $mod.d);',
  7408. '$mod.c = rtl.trunc(Math.pow($mod.c / 10000, $mod.a / 10000) * 10000);',
  7409. '$mod.c = rtl.trunc(Math.pow($mod.a / 10000, $mod.c / 10000) * 10000);',
  7410. '$mod.c = rtl.trunc(Math.pow($mod.d, $mod.c / 10000) * 10000);',
  7411. '$mod.c = rtl.trunc(Math.pow($mod.c / 10000, $mod.d) * 10000);',
  7412. 'if ($mod.c === $mod.c) ;',
  7413. 'if ($mod.c === $mod.a) ;',
  7414. 'if ($mod.a === $mod.c) ;',
  7415. 'if (($mod.d * 10000) === $mod.c) ;',
  7416. 'if ($mod.c === ($mod.d * 10000)) ;',
  7417. '$mod.c = $mod.DoIt($mod.c);',
  7418. '$mod.c = $mod.DoIt($mod.i * 10000);',
  7419. '$mod.c = $mod.DoIt($mod.d * 10000);',
  7420. '$mod.c = rtl.trunc($mod.GetIt($mod.c / 10000) * 10000);',
  7421. '$mod.j = $mod.c / 10000;',
  7422. '$mod.Write($mod.c / 10000);',
  7423. '$mod.c = 0;',
  7424. '$mod.j = rtl.floatToStr($mod.c / 10000);',
  7425. '$mod.j = rtl.floatToStr($mod.c / 10000, 0, 3);',
  7426. '']));
  7427. end;
  7428. procedure TTestModule.TestForBoolDo;
  7429. begin
  7430. StartProgram(false);
  7431. Add([
  7432. 'var b: boolean;',
  7433. 'begin',
  7434. ' for b:=false to true do ;',
  7435. ' for b:=b downto false do ;',
  7436. ' for b in boolean do ;',
  7437. '']);
  7438. ConvertProgram;
  7439. CheckSource('TestForBoolDo',
  7440. LinesToStr([ // statements
  7441. 'this.b = false;']),
  7442. LinesToStr([ // this.$main
  7443. 'for (var $l = 0; $l <= 1; $l++) $mod.b = $l !== 0;',
  7444. 'for (var $l1 = +$mod.b; $l1 >= 0; $l1--) $mod.b = $l1 !== 0;',
  7445. 'for (var $l2 = 0; $l2 <= 1; $l2++) $mod.b = $l2 !== 0;',
  7446. '']));
  7447. end;
  7448. procedure TTestModule.TestForIntDo;
  7449. begin
  7450. StartProgram(false);
  7451. Add([
  7452. 'var i: longint;',
  7453. 'begin',
  7454. ' for i:=3 to 5 do ;',
  7455. ' for i:=i downto 2 do ;',
  7456. ' for i in byte do ;',
  7457. '']);
  7458. ConvertProgram;
  7459. CheckSource('TestForIntDo',
  7460. LinesToStr([ // statements
  7461. 'this.i = 0;']),
  7462. LinesToStr([ // this.$main
  7463. 'for ($mod.i = 3; $mod.i <= 5; $mod.i++) ;',
  7464. 'for (var $l = $mod.i; $l >= 2; $l--) $mod.i = $l;',
  7465. 'for (var $l1 = 0; $l1 <= 255; $l1++) $mod.i = $l1;',
  7466. '']));
  7467. end;
  7468. procedure TTestModule.TestForIntInDo;
  7469. begin
  7470. StartProgram(false);
  7471. Add([
  7472. 'type',
  7473. ' TSetOfInt = set of byte;',
  7474. ' TIntRg = 3..7;',
  7475. ' TSetOfIntRg = set of TIntRg;',
  7476. 'var',
  7477. ' i,i2: longint;',
  7478. ' a1: array of byte;',
  7479. ' a2: array[1..3] of byte;',
  7480. ' soi: TSetOfInt;',
  7481. ' soir: TSetOfIntRg;',
  7482. ' ir: TIntRg;',
  7483. 'begin',
  7484. ' for i in byte do ;',
  7485. ' for i in a1 do ;',
  7486. ' for i in a2 do ;',
  7487. ' for i in [11..13] do ;',
  7488. ' for i in TSetOfInt do ;',
  7489. ' for i in TIntRg do ;',
  7490. ' for i in soi do i2:=i;',
  7491. ' for i in TSetOfIntRg do ;',
  7492. ' for i in soir do ;',
  7493. ' for ir in TIntRg do ;',
  7494. ' for ir in TSetOfIntRg do ;',
  7495. ' for ir in soir do ;',
  7496. '']);
  7497. ConvertProgram;
  7498. CheckSource('TestForIntInDo',
  7499. LinesToStr([ // statements
  7500. 'this.i = 0;',
  7501. 'this.i2 = 0;',
  7502. 'this.a1 = [];',
  7503. 'this.a2 = rtl.arraySetLength(null, 0, 3);',
  7504. 'this.soi = {};',
  7505. 'this.soir = {};',
  7506. 'this.ir = 0;',
  7507. '']),
  7508. LinesToStr([ // this.$main
  7509. 'for (var $l = 0; $l <= 255; $l++) $mod.i = $l;',
  7510. 'for (var $in = $mod.a1, $l1 = 0, $end = rtl.length($in) - 1; $l1 <= $end; $l1++) $mod.i = $in[$l1];',
  7511. 'for (var $in1 = $mod.a2, $l2 = 0, $end1 = rtl.length($in1) - 1; $l2 <= $end1; $l2++) $mod.i = $in1[$l2];',
  7512. 'for (var $l3 = 11; $l3 <= 13; $l3++) $mod.i = $l3;',
  7513. 'for (var $l4 = 0; $l4 <= 255; $l4++) $mod.i = $l4;',
  7514. 'for (var $l5 = 3; $l5 <= 7; $l5++) $mod.i = $l5;',
  7515. 'for (var $l6 in $mod.soi) {',
  7516. ' $mod.i = +$l6;',
  7517. ' $mod.i2 = $mod.i;',
  7518. '};',
  7519. 'for (var $l7 = 3; $l7 <= 7; $l7++) $mod.i = $l7;',
  7520. 'for (var $l8 in $mod.soir) $mod.i = +$l8;',
  7521. 'for (var $l9 = 3; $l9 <= 7; $l9++) $mod.ir = $l9;',
  7522. 'for (var $l10 = 3; $l10 <= 7; $l10++) $mod.ir = $l10;',
  7523. 'for (var $l11 in $mod.soir) $mod.ir = +$l11;',
  7524. '']));
  7525. end;
  7526. procedure TTestModule.TestCharConst;
  7527. begin
  7528. StartProgram(false);
  7529. Add([
  7530. 'const',
  7531. ' a = #$00F3;',
  7532. ' c: char = ''1'';',
  7533. ' wc: widechar = ''ä'';',
  7534. 'begin',
  7535. ' c:=#0;',
  7536. ' c:=#1;',
  7537. ' c:=#9;',
  7538. ' c:=#10;',
  7539. ' c:=#13;',
  7540. ' c:=#31;',
  7541. ' c:=#32;',
  7542. ' c:=#$A;',
  7543. ' c:=#$0A;',
  7544. ' c:=#$b;',
  7545. ' c:=#$0b;',
  7546. ' c:=^A;',
  7547. ' c:=''"'';',
  7548. ' c:=default(char);',
  7549. ' c:=#$00E4;', // ä
  7550. ' c:=''ä'';',
  7551. ' c:=#$E4;', // ä
  7552. ' c:=#$D800;', // invalid UTF-16
  7553. ' c:=#$DFFF;', // invalid UTF-16
  7554. ' c:=#$FFFF;', // last UCS-2
  7555. ' c:=high(c);', // last UCS-2
  7556. ' c:=#269;',
  7557. '']);
  7558. ConvertProgram;
  7559. CheckSource('TestCharConst',
  7560. LinesToStr([
  7561. 'this.a="ó";',
  7562. 'this.c="1";',
  7563. 'this.wc="ä";'
  7564. ]),
  7565. LinesToStr([
  7566. '$mod.c="\x00";',
  7567. '$mod.c="\x01";',
  7568. '$mod.c="\t";',
  7569. '$mod.c="\n";',
  7570. '$mod.c="\r";',
  7571. '$mod.c="\x1F";',
  7572. '$mod.c=" ";',
  7573. '$mod.c="\n";',
  7574. '$mod.c="\n";',
  7575. '$mod.c="\x0B";',
  7576. '$mod.c="\x0B";',
  7577. '$mod.c="\x01";',
  7578. '$mod.c=''"'';',
  7579. '$mod.c="\x00";',
  7580. '$mod.c = "ä";',
  7581. '$mod.c = "ä";',
  7582. '$mod.c = "ä";',
  7583. '$mod.c="\uD800";',
  7584. '$mod.c="\uDFFF";',
  7585. '$mod.c="\uFFFF";',
  7586. '$mod.c="\uFFFF";',
  7587. '$mod.c = "č";',
  7588. '']));
  7589. end;
  7590. procedure TTestModule.TestChar_Compare;
  7591. begin
  7592. StartProgram(false);
  7593. Add('var');
  7594. Add(' c: char;');
  7595. Add(' b: boolean;');
  7596. Add('begin');
  7597. Add(' b:=c=''1'';');
  7598. Add(' b:=''2''=c;');
  7599. Add(' b:=''3''=''4'';');
  7600. Add(' b:=c<>''5'';');
  7601. Add(' b:=''6''<>c;');
  7602. Add(' b:=c>''7'';');
  7603. Add(' b:=''8''>c;');
  7604. Add(' b:=c>=''9'';');
  7605. Add(' b:=''A''>=c;');
  7606. Add(' b:=c<''B'';');
  7607. Add(' b:=''C''<c;');
  7608. Add(' b:=c<=''D'';');
  7609. Add(' b:=''E''<=c;');
  7610. ConvertProgram;
  7611. CheckSource('TestChar_Compare',
  7612. LinesToStr([
  7613. 'this.c="";',
  7614. 'this.b = false;'
  7615. ]),
  7616. LinesToStr([
  7617. '$mod.b = $mod.c === "1";',
  7618. '$mod.b = "2" === $mod.c;',
  7619. '$mod.b = "3" === "4";',
  7620. '$mod.b = $mod.c !== "5";',
  7621. '$mod.b = "6" !== $mod.c;',
  7622. '$mod.b = $mod.c > "7";',
  7623. '$mod.b = "8" > $mod.c;',
  7624. '$mod.b = $mod.c >= "9";',
  7625. '$mod.b = "A" >= $mod.c;',
  7626. '$mod.b = $mod.c < "B";',
  7627. '$mod.b = "C" < $mod.c;',
  7628. '$mod.b = $mod.c <= "D";',
  7629. '$mod.b = "E" <= $mod.c;',
  7630. '']));
  7631. end;
  7632. procedure TTestModule.TestChar_BuiltInProcs;
  7633. begin
  7634. StartProgram(false);
  7635. Add([
  7636. 'var',
  7637. ' c: char;',
  7638. ' i: longint;',
  7639. ' s: string;',
  7640. 'begin',
  7641. ' i:=ord(c);',
  7642. ' i:=ord(s[i]);',
  7643. ' c:=chr(i);',
  7644. ' c:=pred(c);',
  7645. ' c:=succ(c);',
  7646. ' c:=low(c);',
  7647. ' c:=high(c);',
  7648. ' i:=byte(c);',
  7649. ' i:=word(c);',
  7650. ' i:=longint(c);',
  7651. '']);
  7652. ConvertProgram;
  7653. CheckSource('TestChar_BuiltInProcs',
  7654. LinesToStr([
  7655. 'this.c = "";',
  7656. 'this.i = 0;',
  7657. 'this.s = "";'
  7658. ]),
  7659. LinesToStr([
  7660. '$mod.i = $mod.c.charCodeAt();',
  7661. '$mod.i = $mod.s.charCodeAt($mod.i-1);',
  7662. '$mod.c = String.fromCharCode($mod.i);',
  7663. '$mod.c = String.fromCharCode($mod.c.charCodeAt() - 1);',
  7664. '$mod.c = String.fromCharCode($mod.c.charCodeAt() + 1);',
  7665. '$mod.c = "\x00";',
  7666. '$mod.c = "\uFFFF";',
  7667. '$mod.i = $mod.c.charCodeAt() & 255;',
  7668. '$mod.i = $mod.c.charCodeAt();',
  7669. '$mod.i = $mod.c.charCodeAt() & 0xFFFFFFFF;',
  7670. '']));
  7671. end;
  7672. procedure TTestModule.TestStringConst;
  7673. begin
  7674. StartProgram(false);
  7675. Add([
  7676. '{$H+}',
  7677. 'const',
  7678. ' a = #$00F3#$017C;', // first <256, then >=256
  7679. ' b = string(''a'');',
  7680. ' c = string(''ä'');',
  7681. ' d = UnicodeString(''b'');',
  7682. ' e = UnicodeString(''ö'');',
  7683. ' f = low(a)+high(b);',
  7684. ' g: word = low(a);',
  7685. 'var',
  7686. ' s: string = ''abc'';',
  7687. ' i: longint;',
  7688. 'begin',
  7689. ' s:='''';',
  7690. ' s:=#13#10;',
  7691. ' s:=#9''foo'';',
  7692. ' s:=#$A9;',
  7693. ' s:=''foo''#13''bar'';',
  7694. ' s:=''"'';',
  7695. ' s:=''"''''"'';',
  7696. ' s:=#$20AC;', // euro
  7697. ' s:=#$10437;', // outside BMP
  7698. ' s:=''abc''#$20AC;', // ascii,#
  7699. ' s:=''ä''#$20AC;', // non ascii,#
  7700. ' s:=#$20AC''abc'';', // #, ascii
  7701. ' s:=#$20AC''ä'';', // #, non ascii
  7702. ' s:=default(string);',
  7703. ' s:=concat(s);',
  7704. ' s:=concat(s,''a'',s);',
  7705. ' s:=#250#269;',
  7706. ' i:=low(s)+high(a);',
  7707. //' s:=#$2F804;',
  7708. // ToDo: \uD87E\uDC04 -> \u{2F804}
  7709. '']);
  7710. ConvertProgram;
  7711. CheckSource('TestStringConst',
  7712. LinesToStr([
  7713. 'this.a = "óż";',
  7714. 'this.b = "a";',
  7715. 'this.c = "ä";',
  7716. 'this.d = "b";',
  7717. 'this.e = "ö";',
  7718. 'this.f = 1 + this.b.length;',
  7719. 'this.g = 1;',
  7720. 'this.s="abc";',
  7721. 'this.i = 0;',
  7722. '']),
  7723. LinesToStr([
  7724. '$mod.s="";',
  7725. '$mod.s="\r\n";',
  7726. '$mod.s="\tfoo";',
  7727. '$mod.s="©";',
  7728. '$mod.s="foo\rbar";',
  7729. '$mod.s=''"'';',
  7730. '$mod.s=''"\''"'';',
  7731. '$mod.s="€";',
  7732. '$mod.s="'#$F0#$90#$90#$B7'";',
  7733. '$mod.s = "abc€";',
  7734. '$mod.s = "ä€";',
  7735. '$mod.s = "€abc";',
  7736. '$mod.s = "ۊ";',
  7737. '$mod.s="";',
  7738. '$mod.s = $mod.s;',
  7739. '$mod.s = $mod.s.concat("a", $mod.s);',
  7740. '$mod.s = "úč";',
  7741. '$mod.i = 1 + $mod.a.length;',
  7742. '']));
  7743. end;
  7744. procedure TTestModule.TestStringConst_InvalidUTF16;
  7745. begin
  7746. StartProgram(false);
  7747. Add([
  7748. 'const',
  7749. ' a: char = #$D87E;',
  7750. ' b: string = #$D87E;',
  7751. ' c: string = #$D87E#43;',
  7752. 'begin',
  7753. ' c:=''abc''#$D87E;',
  7754. ' c:=#0#1#2;',
  7755. ' c:=#127;',
  7756. ' c:=#128;',
  7757. ' c:=#255;',
  7758. ' c:=#256;',
  7759. '']);
  7760. ConvertProgram;
  7761. CheckSource('TestStringConst',
  7762. LinesToStr([
  7763. 'this.a = "\uD87E";',
  7764. 'this.b = "\uD87E";',
  7765. 'this.c = "\uD87E+";',
  7766. '']),
  7767. LinesToStr([
  7768. '$mod.c = "abc\uD87E";',
  7769. '$mod.c = "\x00\x01\x02";',
  7770. '$mod.c = "'#127'";',
  7771. '$mod.c = "'#$c2#$80'";',
  7772. '$mod.c = "'#$c3#$BF'";',
  7773. '$mod.c = "'#$c4#$80'";',
  7774. '']));
  7775. end;
  7776. procedure TTestModule.TestStringConstSurrogate;
  7777. begin
  7778. StartProgram(false);
  7779. Add([
  7780. 'var',
  7781. ' s: string;',
  7782. 'begin',
  7783. ' s:=''😊'';', // 1F60A
  7784. '']);
  7785. ConvertProgram;
  7786. CheckSource('TestStringConstSurrogate',
  7787. LinesToStr([
  7788. 'this.s="";'
  7789. ]),
  7790. LinesToStr([
  7791. '$mod.s="😊";'
  7792. ]));
  7793. end;
  7794. procedure TTestModule.TestString_Length;
  7795. begin
  7796. StartProgram(false);
  7797. Add('const c = ''foo'';');
  7798. Add('var');
  7799. Add(' s: string;');
  7800. Add(' i: longint;');
  7801. Add('begin');
  7802. Add(' i:=length(s);');
  7803. Add(' i:=length(s+s);');
  7804. Add(' i:=length(''abc'');');
  7805. Add(' i:=length(c);');
  7806. ConvertProgram;
  7807. CheckSource('TestString_Length',
  7808. LinesToStr([
  7809. 'this.c = "foo";',
  7810. 'this.s = "";',
  7811. 'this.i = 0;',
  7812. '']),
  7813. LinesToStr([
  7814. '$mod.i = $mod.s.length;',
  7815. '$mod.i = ($mod.s+$mod.s).length;',
  7816. '$mod.i = "abc".length;',
  7817. '$mod.i = $mod.c.length;',
  7818. '']));
  7819. end;
  7820. procedure TTestModule.TestString_Compare;
  7821. begin
  7822. StartProgram(false);
  7823. Add('var');
  7824. Add(' s, t: string;');
  7825. Add(' b: boolean;');
  7826. Add('begin');
  7827. Add(' b:=s=t;');
  7828. Add(' b:=s<>t;');
  7829. Add(' b:=s>t;');
  7830. Add(' b:=s>=t;');
  7831. Add(' b:=s<t;');
  7832. Add(' b:=s<=t;');
  7833. ConvertProgram;
  7834. CheckSource('TestString_Compare',
  7835. LinesToStr([ // statements
  7836. 'this.s = "";',
  7837. 'this.t = "";',
  7838. 'this.b =false;'
  7839. ]),
  7840. LinesToStr([ // this.$main
  7841. '$mod.b = $mod.s === $mod.t;',
  7842. '$mod.b = $mod.s !== $mod.t;',
  7843. '$mod.b = $mod.s > $mod.t;',
  7844. '$mod.b = $mod.s >= $mod.t;',
  7845. '$mod.b = $mod.s < $mod.t;',
  7846. '$mod.b = $mod.s <= $mod.t;',
  7847. '']));
  7848. end;
  7849. procedure TTestModule.TestString_SetLength;
  7850. begin
  7851. StartProgram(false);
  7852. Add([
  7853. 'procedure DoIt(var s: string);',
  7854. 'begin',
  7855. ' SetLength(s,2);',
  7856. 'end;',
  7857. 'var s: string;',
  7858. 'begin',
  7859. ' SetLength(s,3);',
  7860. '']);
  7861. ConvertProgram;
  7862. CheckSource('TestString_SetLength',
  7863. LinesToStr([ // statements
  7864. 'this.DoIt = function (s) {',
  7865. ' s.set(rtl.strSetLength(s.get(), 2));',
  7866. '};',
  7867. 'this.s = "";',
  7868. '']),
  7869. LinesToStr([ // this.$main
  7870. '$mod.s = rtl.strSetLength($mod.s, 3);'
  7871. ]));
  7872. end;
  7873. procedure TTestModule.TestString_CharAt;
  7874. begin
  7875. StartProgram(false);
  7876. Add([
  7877. 'var',
  7878. ' s: string;',
  7879. ' c: char;',
  7880. ' b: boolean;',
  7881. 'begin',
  7882. ' b:= s[1] = c;',
  7883. ' b:= c = s[1];',
  7884. ' b:= c <> s[1];',
  7885. ' b:= c > s[1];',
  7886. ' b:= c >= s[1];',
  7887. ' b:= c < s[2];',
  7888. ' b:= c <= s[1];',
  7889. ' s[1] := c;',
  7890. ' s[2+3] := c;']);
  7891. ConvertProgram;
  7892. CheckSource('TestString_CharAt',
  7893. LinesToStr([ // statements
  7894. 'this.s = "";',
  7895. 'this.c = "";',
  7896. 'this.b = false;'
  7897. ]),
  7898. LinesToStr([ // this.$main
  7899. '$mod.b = $mod.s.charAt(0) === $mod.c;',
  7900. '$mod.b = $mod.c === $mod.s.charAt(0);',
  7901. '$mod.b = $mod.c !== $mod.s.charAt(0);',
  7902. '$mod.b = $mod.c > $mod.s.charAt(0);',
  7903. '$mod.b = $mod.c >= $mod.s.charAt(0);',
  7904. '$mod.b = $mod.c < $mod.s.charAt(1);',
  7905. '$mod.b = $mod.c <= $mod.s.charAt(0);',
  7906. '$mod.s = rtl.setCharAt($mod.s, 0, $mod.c);',
  7907. '$mod.s = rtl.setCharAt($mod.s, (2 + 3) - 1, $mod.c);',
  7908. '']));
  7909. end;
  7910. procedure TTestModule.TestStringHMinusFail;
  7911. begin
  7912. StartProgram(false);
  7913. Add([
  7914. '{$H-}',
  7915. 'var s: string;',
  7916. 'begin']);
  7917. ConvertProgram;
  7918. CheckHint(mtWarning,nWarnIllegalCompilerDirectiveX,'Warning: test1.pp(3,6) : Illegal compiler directive "H-"');
  7919. end;
  7920. procedure TTestModule.TestStr;
  7921. begin
  7922. StartProgram(false);
  7923. Add('var');
  7924. Add(' b: boolean;');
  7925. Add(' i: longint;');
  7926. Add(' d: double;');
  7927. Add(' s: string;');
  7928. Add('begin');
  7929. Add(' str(b,s);');
  7930. Add(' str(i,s);');
  7931. Add(' str(d,s);');
  7932. Add(' str(i:3,s);');
  7933. Add(' str(d:3:2,s);');
  7934. Add(' Str(12.456:12:1,s);');
  7935. Add(' Str(12.456:12,s);');
  7936. Add(' s:=str(b);');
  7937. Add(' s:=str(i);');
  7938. Add(' s:=str(d);');
  7939. Add(' s:=str(i,i);');
  7940. Add(' s:=str(i:3);');
  7941. Add(' s:=str(d:3:2);');
  7942. Add(' s:=str(i:4,i);');
  7943. Add(' s:=str(i,i:5);');
  7944. Add(' s:=str(i:4,i:5);');
  7945. Add(' s:=str(s,s);');
  7946. Add(' s:=str(s,''foo'');');
  7947. ConvertProgram;
  7948. CheckSource('TestStr',
  7949. LinesToStr([ // statements
  7950. 'this.b = false;',
  7951. 'this.i = 0;',
  7952. 'this.d = 0.0;',
  7953. 'this.s = "";',
  7954. '']),
  7955. LinesToStr([ // this.$main
  7956. '$mod.s = ""+$mod.b;',
  7957. '$mod.s = ""+$mod.i;',
  7958. '$mod.s = rtl.floatToStr($mod.d);',
  7959. '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
  7960. '$mod.s = rtl.floatToStr($mod.d,3,2);',
  7961. '$mod.s = rtl.floatToStr(12.456,12,1);',
  7962. '$mod.s = rtl.floatToStr(12.456,12);',
  7963. '$mod.s = ""+$mod.b;',
  7964. '$mod.s = ""+$mod.i;',
  7965. '$mod.s = rtl.floatToStr($mod.d);',
  7966. '$mod.s = ""+$mod.i+$mod.i;',
  7967. '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
  7968. '$mod.s = rtl.floatToStr($mod.d,3,2);',
  7969. '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + $mod.i;',
  7970. '$mod.s = "" + $mod.i + rtl.spaceLeft("" + $mod.i, 5);',
  7971. '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + rtl.spaceLeft("" + $mod.i, 5);',
  7972. '$mod.s = $mod.s + $mod.s;',
  7973. '$mod.s = $mod.s + "foo";',
  7974. '']));
  7975. end;
  7976. procedure TTestModule.TestBaseType_AnsiStringFail;
  7977. begin
  7978. StartProgram(false);
  7979. Add('var s: AnsiString');
  7980. SetExpectedPasResolverError('identifier not found "AnsiString"',PasResolveEval.nIdentifierNotFound);
  7981. ConvertProgram;
  7982. end;
  7983. procedure TTestModule.TestBaseType_WideStringFail;
  7984. begin
  7985. StartProgram(false);
  7986. Add('var s: WideString');
  7987. SetExpectedPasResolverError('identifier not found "WideString"',PasResolveEval.nIdentifierNotFound);
  7988. ConvertProgram;
  7989. end;
  7990. procedure TTestModule.TestBaseType_ShortStringFail;
  7991. begin
  7992. StartProgram(false);
  7993. Add('var s: ShortString');
  7994. SetExpectedPasResolverError('identifier not found "ShortString"',PasResolveEval.nIdentifierNotFound);
  7995. ConvertProgram;
  7996. end;
  7997. procedure TTestModule.TestBaseType_RawByteStringFail;
  7998. begin
  7999. StartProgram(false);
  8000. Add('var s: RawByteString');
  8001. SetExpectedPasResolverError('identifier not found "RawByteString"',PasResolveEval.nIdentifierNotFound);
  8002. ConvertProgram;
  8003. end;
  8004. procedure TTestModule.TestTypeShortstring_Fail;
  8005. begin
  8006. StartProgram(false);
  8007. Add('type t = string[12];');
  8008. Add('var s: t;');
  8009. Add('begin');
  8010. SetExpectedPasResolverError('illegal qualifier "["',nIllegalQualifier);
  8011. ConvertProgram;
  8012. end;
  8013. procedure TTestModule.TestCharSet_Custom;
  8014. begin
  8015. StartProgram(false);
  8016. Add([
  8017. 'type',
  8018. ' TCharRg = ''a''..''z'';',
  8019. ' TSetOfCharRg = set of TCharRg;',
  8020. ' TCharRg2 = ''m''..''p'';',
  8021. 'const',
  8022. ' crg: TCharRg = ''b'';',
  8023. 'var',
  8024. ' c: char;',
  8025. ' crg2: TCharRg2;',
  8026. ' s: TSetOfCharRg;',
  8027. 'begin',
  8028. ' c:=crg;',
  8029. ' crg:=c;',
  8030. ' crg2:=crg;',
  8031. ' if c=crg then ;',
  8032. ' if crg=c then ;',
  8033. ' if crg=crg2 then ;',
  8034. ' if c in s then ;',
  8035. ' if crg2 in s then ;',
  8036. ' c:=default(TCharRg);',
  8037. '']);
  8038. ConvertProgram;
  8039. CheckSource('TestCharSet_Custom',
  8040. LinesToStr([ // statements
  8041. 'this.crg = "b";',
  8042. 'this.c = "";',
  8043. 'this.crg2 = "m";',
  8044. 'this.s = {};',
  8045. '']),
  8046. LinesToStr([ // this.$main
  8047. '$mod.c = $mod.crg;',
  8048. '$mod.crg = $mod.c;',
  8049. '$mod.crg2 = $mod.crg;',
  8050. 'if ($mod.c === $mod.crg) ;',
  8051. 'if ($mod.crg === $mod.c) ;',
  8052. 'if ($mod.crg === $mod.crg2) ;',
  8053. 'if ($mod.c.charCodeAt() in $mod.s) ;',
  8054. 'if ($mod.crg2.charCodeAt() in $mod.s) ;',
  8055. '$mod.c = "a";',
  8056. '']));
  8057. end;
  8058. procedure TTestModule.TestWideChar;
  8059. begin
  8060. StartProgram(false);
  8061. Add([
  8062. 'procedure Fly(var c: char);',
  8063. 'begin',
  8064. 'end;',
  8065. 'procedure Run(var c: widechar);',
  8066. 'begin',
  8067. 'end;',
  8068. 'var',
  8069. ' c: char;',
  8070. ' wc: widechar;',
  8071. ' w: word;',
  8072. 'begin',
  8073. ' Fly(wc);',
  8074. ' Run(c);',
  8075. ' wc:=WideChar(w);',
  8076. ' w:=ord(wc);',
  8077. '']);
  8078. ConvertProgram;
  8079. CheckSource('TestWideChar_VarArg',
  8080. LinesToStr([ // statements
  8081. 'this.Fly = function (c) {',
  8082. '};',
  8083. 'this.Run = function (c) {',
  8084. '};',
  8085. 'this.c = "";',
  8086. 'this.wc = "";',
  8087. 'this.w = 0;',
  8088. '']),
  8089. LinesToStr([ // this.$main
  8090. '$mod.Fly({',
  8091. ' p: $mod,',
  8092. ' get: function () {',
  8093. ' return this.p.wc;',
  8094. ' },',
  8095. ' set: function (v) {',
  8096. ' this.p.wc = v;',
  8097. ' }',
  8098. '});',
  8099. '$mod.Run({',
  8100. ' p: $mod,',
  8101. ' get: function () {',
  8102. ' return this.p.c;',
  8103. ' },',
  8104. ' set: function (v) {',
  8105. ' this.p.c = v;',
  8106. ' }',
  8107. '});',
  8108. '$mod.wc = String.fromCharCode($mod.w);',
  8109. '$mod.w = $mod.wc.charCodeAt();',
  8110. '',
  8111. '']));
  8112. end;
  8113. procedure TTestModule.TestForCharDo;
  8114. begin
  8115. StartProgram(false);
  8116. Add([
  8117. 'var c: char;',
  8118. 'begin',
  8119. ' for c:=''a'' to ''c'' do ;',
  8120. ' for c:=c downto ''a'' do ;',
  8121. ' for c:=''Б'' to ''Я'' do ;',
  8122. '']);
  8123. ConvertProgram;
  8124. CheckSource('TestForCharDo',
  8125. LinesToStr([ // statements
  8126. 'this.c = "";']),
  8127. LinesToStr([ // this.$main
  8128. 'for (var $l = 97; $l <= 99; $l++) $mod.c = String.fromCharCode($l);',
  8129. 'for (var $l1 = $mod.c.charCodeAt(); $l1 >= 97; $l1--) $mod.c = String.fromCharCode($l1);',
  8130. 'for (var $l2 = 1041; $l2 <= 1071; $l2++) $mod.c = String.fromCharCode($l2);',
  8131. '']));
  8132. end;
  8133. procedure TTestModule.TestForCharInDo;
  8134. begin
  8135. StartProgram(false);
  8136. Add([
  8137. 'type',
  8138. ' TSetOfChar = set of char;',
  8139. ' TCharRg = ''a''..''z'';',
  8140. ' TSetOfCharRg = set of TCharRg;',
  8141. 'const Foo = ''foo'';',
  8142. 'var',
  8143. ' c,c2: char;',
  8144. ' s: string;',
  8145. ' a1: array of char;',
  8146. ' a2: array[1..3] of char;',
  8147. ' soc: TSetOfChar;',
  8148. ' socr: TSetOfCharRg;',
  8149. ' cr: TCharRg;',
  8150. 'begin',
  8151. ' for c in foo do ;',
  8152. ' for c in s do ;',
  8153. ' for c in char do ;',
  8154. ' for c in a1 do ;',
  8155. ' for c in a2 do ;',
  8156. ' for c in [''1''..''3''] do ;',
  8157. ' for c in TSetOfChar do ;',
  8158. ' for c in TCharRg do ;',
  8159. ' for c in soc do c2:=c;',
  8160. ' for c in TSetOfCharRg do ;',
  8161. ' for c in socr do ;',
  8162. ' for cr in TCharRg do ;',
  8163. ' for cr in TSetOfCharRg do ;',
  8164. ' for cr in socr do ;',
  8165. '']);
  8166. ConvertProgram;
  8167. CheckSource('TestForCharInDo',
  8168. LinesToStr([ // statements
  8169. 'this.Foo = "foo";',
  8170. 'this.c = "";',
  8171. 'this.c2 = "";',
  8172. 'this.s = "";',
  8173. 'this.a1 = [];',
  8174. 'this.a2 = rtl.arraySetLength(null, "", 3);',
  8175. 'this.soc = {};',
  8176. 'this.socr = {};',
  8177. 'this.cr = "a";',
  8178. '']),
  8179. LinesToStr([ // this.$main
  8180. 'for (var $in = $mod.Foo, $l = 0, $end = $in.length - 1; $l <= $end; $l++) $mod.c = $in.charAt($l);',
  8181. 'for (var $in1 = $mod.s, $l1 = 0, $end1 = $in1.length - 1; $l1 <= $end1; $l1++) $mod.c = $in1.charAt($l1);',
  8182. 'for (var $l2 = 0; $l2 <= 65535; $l2++) $mod.c = String.fromCharCode($l2);',
  8183. 'for (var $in2 = $mod.a1, $l3 = 0, $end2 = rtl.length($in2) - 1; $l3 <= $end2; $l3++) $mod.c = $in2[$l3];',
  8184. 'for (var $in3 = $mod.a2, $l4 = 0, $end3 = rtl.length($in3) - 1; $l4 <= $end3; $l4++) $mod.c = $in3[$l4];',
  8185. 'for (var $l5 = 49; $l5 <= 51; $l5++) $mod.c = String.fromCharCode($l5);',
  8186. 'for (var $l6 = 0; $l6 <= 65535; $l6++) $mod.c = String.fromCharCode($l6);',
  8187. 'for (var $l7 = 97; $l7 <= 122; $l7++) $mod.c = String.fromCharCode($l7);',
  8188. 'for (var $l8 in $mod.soc) {',
  8189. ' $mod.c = String.fromCharCode($l8);',
  8190. ' $mod.c2 = $mod.c;',
  8191. '};',
  8192. 'for (var $l9 = 97; $l9 <= 122; $l9++) $mod.c = String.fromCharCode($l9);',
  8193. 'for (var $l10 in $mod.socr) $mod.c = String.fromCharCode($l10);',
  8194. 'for (var $l11 = 97; $l11 <= 122; $l11++) $mod.cr = String.fromCharCode($l11);',
  8195. 'for (var $l12 = 97; $l12 <= 122; $l12++) $mod.cr = String.fromCharCode($l12);',
  8196. 'for (var $l13 in $mod.socr) $mod.cr = String.fromCharCode($l13);',
  8197. '']));
  8198. end;
  8199. procedure TTestModule.TestProcTwoArgs;
  8200. begin
  8201. StartProgram(false);
  8202. Add('procedure Test(a,b: longint);');
  8203. Add('begin');
  8204. Add('end;');
  8205. Add('begin');
  8206. ConvertProgram;
  8207. CheckSource('TestProcTwoArgs',
  8208. LinesToStr([ // statements
  8209. 'this.Test = function (a,b) {',
  8210. '};'
  8211. ]),
  8212. LinesToStr([ // this.$main
  8213. ''
  8214. ]));
  8215. end;
  8216. procedure TTestModule.TestProc_DefaultValue;
  8217. begin
  8218. StartProgram(false);
  8219. Add('procedure p1(i: longint = 1);');
  8220. Add('begin');
  8221. Add('end;');
  8222. Add('procedure p2(i: longint = 1; c: char = ''a'');');
  8223. Add('begin');
  8224. Add('end;');
  8225. Add('procedure p3(d: double = 1.0; b: boolean = false; s: string = ''abc'');');
  8226. Add('begin');
  8227. Add('end;');
  8228. Add('begin');
  8229. Add(' p1;');
  8230. Add(' p1();');
  8231. Add(' p1(11);');
  8232. Add(' p2;');
  8233. Add(' p2();');
  8234. Add(' p2(12);');
  8235. Add(' p2(13,''b'');');
  8236. Add(' p3();');
  8237. ConvertProgram;
  8238. CheckSource('TestProc_DefaultValue',
  8239. LinesToStr([ // statements
  8240. 'this.p1 = function (i) {',
  8241. '};',
  8242. 'this.p2 = function (i,c) {',
  8243. '};',
  8244. 'this.p3 = function (d,b,s) {',
  8245. '};'
  8246. ]),
  8247. LinesToStr([ // this.$main
  8248. ' $mod.p1(1);',
  8249. ' $mod.p1(1);',
  8250. ' $mod.p1(11);',
  8251. ' $mod.p2(1,"a");',
  8252. ' $mod.p2(1,"a");',
  8253. ' $mod.p2(12,"a");',
  8254. ' $mod.p2(13,"b");',
  8255. ' $mod.p3(1.0,false,"abc");'
  8256. ]));
  8257. end;
  8258. procedure TTestModule.TestFunctionInt;
  8259. begin
  8260. StartProgram(false);
  8261. Add('function MyTest(Bar: longint): longint;');
  8262. Add('begin');
  8263. Add(' Result:=2*bar');
  8264. Add('end;');
  8265. Add('begin');
  8266. ConvertProgram;
  8267. CheckSource('TestFunctionInt',
  8268. LinesToStr([ // statements
  8269. 'this.MyTest = function (Bar) {',
  8270. ' var Result = 0;',
  8271. ' Result = 2*Bar;',
  8272. ' return Result;',
  8273. '};'
  8274. ]),
  8275. LinesToStr([ // this.$main
  8276. ''
  8277. ]));
  8278. end;
  8279. procedure TTestModule.TestFunctionString;
  8280. begin
  8281. StartProgram(false);
  8282. Add('function Test(Bar: string): string;');
  8283. Add('begin');
  8284. Add(' Result:=bar+BAR');
  8285. Add('end;');
  8286. Add('begin');
  8287. ConvertProgram;
  8288. CheckSource('TestFunctionString',
  8289. LinesToStr([ // statements
  8290. 'this.Test = function (Bar) {',
  8291. ' var Result = "";',
  8292. ' Result = Bar+Bar;',
  8293. ' return Result;',
  8294. '};'
  8295. ]),
  8296. LinesToStr([ // this.$main
  8297. ''
  8298. ]));
  8299. end;
  8300. procedure TTestModule.TestIfThen;
  8301. begin
  8302. StartProgram(false);
  8303. Add([
  8304. 'var b: boolean;',
  8305. 'begin',
  8306. ' if b then ;',
  8307. ' if b then else ;']);
  8308. ConvertProgram;
  8309. CheckSource('TestIfThen',
  8310. LinesToStr([ // statements
  8311. 'this.b = false;',
  8312. '']),
  8313. LinesToStr([ // this.$main
  8314. 'if ($mod.b) ;',
  8315. 'if ($mod.b) ;',
  8316. '']));
  8317. end;
  8318. procedure TTestModule.TestForLoop;
  8319. begin
  8320. StartProgram(false);
  8321. Add('var');
  8322. Add(' vI, vJ, vN: longint;');
  8323. Add('begin');
  8324. Add(' VJ:=0;');
  8325. Add(' VN:=3;');
  8326. Add(' for VI:=1 to VN do');
  8327. Add(' begin');
  8328. Add(' VJ:=VJ+VI;');
  8329. Add(' end;');
  8330. ConvertProgram;
  8331. CheckSource('TestForLoop',
  8332. LinesToStr([ // statements
  8333. 'this.vI = 0;',
  8334. 'this.vJ = 0;',
  8335. 'this.vN = 0;'
  8336. ]),
  8337. LinesToStr([ // this.$main
  8338. ' $mod.vJ = 0;',
  8339. ' $mod.vN = 3;',
  8340. ' for (var $l = 1, $end = $mod.vN; $l <= $end; $l++) {',
  8341. ' $mod.vI = $l;',
  8342. ' $mod.vJ = $mod.vJ + $mod.vI;',
  8343. ' };',
  8344. '']));
  8345. end;
  8346. procedure TTestModule.TestForLoopInsideFunction;
  8347. begin
  8348. StartProgram(false);
  8349. Add('function SumNumbers(Count: longint): longint;');
  8350. Add('var');
  8351. Add(' vI, vJ: longint;');
  8352. Add('begin');
  8353. Add(' vj:=0;');
  8354. Add(' for vi:=1 to count do');
  8355. Add(' begin');
  8356. Add(' vj:=vj+vi;');
  8357. Add(' end;');
  8358. Add('end;');
  8359. Add('begin');
  8360. Add(' sumnumbers(3);');
  8361. ConvertProgram;
  8362. CheckSource('TestForLoopInsideFunction',
  8363. LinesToStr([ // statements
  8364. 'this.SumNumbers = function (Count) {',
  8365. ' var Result = 0;',
  8366. ' var vI = 0;',
  8367. ' var vJ = 0;',
  8368. ' vJ = 0;',
  8369. ' for (var $l = 1, $end = Count; $l <= $end; $l++) {',
  8370. ' vI = $l;',
  8371. ' vJ = vJ + vI;',
  8372. ' };',
  8373. ' return Result;',
  8374. '};'
  8375. ]),
  8376. LinesToStr([ // $mod.$main
  8377. ' $mod.SumNumbers(3);'
  8378. ]));
  8379. end;
  8380. procedure TTestModule.TestForLoop_ReadVarAfter;
  8381. begin
  8382. StartProgram(false);
  8383. Add('var');
  8384. Add(' vI: longint;');
  8385. Add('begin');
  8386. Add(' for vi:=1 to 2 do ;');
  8387. Add(' if vi=3 then ;');
  8388. ConvertProgram;
  8389. CheckSource('TestForLoop',
  8390. LinesToStr([ // statements
  8391. 'this.vI = 0;'
  8392. ]),
  8393. LinesToStr([ // this.$main
  8394. ' for ($mod.vI = 1; $mod.vI <= 2; $mod.vI++) ;',
  8395. ' if ($mod.vI===3) ;'
  8396. ]));
  8397. end;
  8398. procedure TTestModule.TestForLoop_Nested;
  8399. begin
  8400. StartProgram(false);
  8401. Add('function SumNumbers(Count: longint): longint;');
  8402. Add('var');
  8403. Add(' vI, vJ, vK: longint;');
  8404. Add('begin');
  8405. Add(' VK:=0;');
  8406. Add(' for VI:=1 to count do');
  8407. Add(' begin');
  8408. Add(' for vj:=1 to vi do');
  8409. Add(' begin');
  8410. Add(' vk:=VK+VI;');
  8411. Add(' end;');
  8412. Add(' end;');
  8413. Add('end;');
  8414. Add('begin');
  8415. Add(' sumnumbers(3);');
  8416. ConvertProgram;
  8417. CheckSource('TestForLoopInFunction',
  8418. LinesToStr([ // statements
  8419. 'this.SumNumbers = function (Count) {',
  8420. ' var Result = 0;',
  8421. ' var vI = 0;',
  8422. ' var vJ = 0;',
  8423. ' var vK = 0;',
  8424. ' vK = 0;',
  8425. ' for (var $l = 1, $end = Count; $l <= $end; $l++) {',
  8426. ' vI = $l;',
  8427. ' for (var $l1 = 1, $end1 = vI; $l1 <= $end1; $l1++) {',
  8428. ' vJ = $l1;',
  8429. ' vK = vK + vI;',
  8430. ' };',
  8431. ' };',
  8432. ' return Result;',
  8433. '};'
  8434. ]),
  8435. LinesToStr([ // $mod.$main
  8436. ' $mod.SumNumbers(3);'
  8437. ]));
  8438. end;
  8439. procedure TTestModule.TestRepeatUntil;
  8440. begin
  8441. StartProgram(false);
  8442. Add('var');
  8443. Add(' vI, vJ, vN: longint;');
  8444. Add('begin');
  8445. Add(' vn:=3;');
  8446. Add(' vj:=0;');
  8447. Add(' VI:=0;');
  8448. Add(' repeat');
  8449. Add(' VI:=vi+1;');
  8450. Add(' vj:=VJ+vI;');
  8451. Add(' until vi>=vn');
  8452. ConvertProgram;
  8453. CheckSource('TestRepeatUntil',
  8454. LinesToStr([ // statements
  8455. 'this.vI = 0;',
  8456. 'this.vJ = 0;',
  8457. 'this.vN = 0;'
  8458. ]),
  8459. LinesToStr([ // $mod.$main
  8460. ' $mod.vN = 3;',
  8461. ' $mod.vJ = 0;',
  8462. ' $mod.vI = 0;',
  8463. ' do{',
  8464. ' $mod.vI = $mod.vI + 1;',
  8465. ' $mod.vJ = $mod.vJ + $mod.vI;',
  8466. ' }while(!($mod.vI>=$mod.vN));'
  8467. ]));
  8468. end;
  8469. procedure TTestModule.TestAsmBlock;
  8470. begin
  8471. StartProgram(false);
  8472. Add([
  8473. 'var',
  8474. ' vI: longint;',
  8475. 'begin',
  8476. ' vi:=1;',
  8477. ' asm',
  8478. ' if (vI===1) {',
  8479. ' vI=2;',
  8480. //' console.log(''end;'');', ToDo
  8481. ' }',
  8482. ' if (vI===2){ vI=3; }',
  8483. ' end;',
  8484. ' VI:=4;']);
  8485. ConvertProgram;
  8486. CheckSource('TestAsmBlock',
  8487. LinesToStr([ // statements
  8488. 'this.vI = 0;'
  8489. ]),
  8490. LinesToStr([ // $mod.$main
  8491. '$mod.vI = 1;',
  8492. 'if (vI===1) {',
  8493. ' vI=2;',
  8494. '}',
  8495. 'if (vI===2){ vI=3; }',
  8496. ';',
  8497. '$mod.vI = 4;'
  8498. ]));
  8499. end;
  8500. procedure TTestModule.TestAsmPas_Impl;
  8501. begin
  8502. StartUnit(false);
  8503. Add('interface');
  8504. Add('const cIntf: longint = 1;');
  8505. Add('var vIntf: longint;');
  8506. Add('implementation');
  8507. Add('const cImpl: longint = 2;');
  8508. Add('var vImpl: longint;');
  8509. Add('procedure DoIt;');
  8510. Add('const cLoc: longint = 3;');
  8511. Add('var vLoc: longint;');
  8512. Add('begin;');
  8513. Add(' asm');
  8514. //Add(' pas(vIntf)=pas(cIntf);');
  8515. //Add(' pas(vImpl)=pas(cImpl);');
  8516. //Add(' pas(vLoc)=pas(cLoc);');
  8517. Add(' end;');
  8518. Add('end;');
  8519. ConvertUnit;
  8520. CheckSource('TestAsmPas_Impl',
  8521. LinesToStr([
  8522. 'var $impl = $mod.$impl;',
  8523. 'this.cIntf = 1;',
  8524. 'this.vIntf = 0;',
  8525. '']),
  8526. '', // this.$init
  8527. LinesToStr([ // implementation
  8528. '$impl.cImpl = 2;',
  8529. '$impl.vImpl = 0;',
  8530. 'var cLoc = 3;',
  8531. '$impl.DoIt = function () {',
  8532. ' var vLoc = 0;',
  8533. '};',
  8534. '']) );
  8535. end;
  8536. procedure TTestModule.TestTryFinally;
  8537. begin
  8538. StartProgram(false);
  8539. Add('var i: longint;');
  8540. Add('begin');
  8541. Add(' try');
  8542. Add(' i:=0; i:=2 div i;');
  8543. Add(' finally');
  8544. Add(' i:=3');
  8545. Add(' end;');
  8546. ConvertProgram;
  8547. CheckSource('TestTryFinally',
  8548. LinesToStr([ // statements
  8549. 'this.i = 0;'
  8550. ]),
  8551. LinesToStr([ // $mod.$main
  8552. 'try {',
  8553. ' $mod.i = 0;',
  8554. ' $mod.i = rtl.trunc(2 / $mod.i);',
  8555. '} finally {',
  8556. ' $mod.i = 3;',
  8557. '};'
  8558. ]));
  8559. end;
  8560. procedure TTestModule.TestTryExcept;
  8561. begin
  8562. StartProgram(false);
  8563. Add([
  8564. 'type',
  8565. ' TObject = class end;',
  8566. ' Exception = class Msg: string; end;',
  8567. ' EInvalidCast = class(Exception) end;',
  8568. 'var vI: longint;',
  8569. 'begin',
  8570. ' try',
  8571. ' vi:=1;',
  8572. ' except',
  8573. ' vi:=2',
  8574. ' end;',
  8575. ' try',
  8576. ' vi:=3;',
  8577. ' except',
  8578. ' raise;',
  8579. ' end;',
  8580. ' try',
  8581. ' VI:=4;',
  8582. ' except',
  8583. ' on einvalidcast do',
  8584. ' raise;',
  8585. ' on E: exception do',
  8586. ' if e.msg='''' then',
  8587. ' raise e;',
  8588. ' else',
  8589. ' vi:=5',
  8590. ' end;',
  8591. ' try',
  8592. ' VI:=6;',
  8593. ' except',
  8594. ' on einvalidcast do ;',
  8595. ' end;',
  8596. '']);
  8597. ConvertProgram;
  8598. CheckSource('TestTryExcept',
  8599. LinesToStr([ // statements
  8600. 'rtl.createClass(this, "TObject", null, function () {',
  8601. ' this.$init = function () {',
  8602. ' };',
  8603. ' this.$final = function () {',
  8604. ' };',
  8605. '});',
  8606. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  8607. ' this.$init = function () {',
  8608. ' $mod.TObject.$init.call(this);',
  8609. ' this.Msg = "";',
  8610. ' };',
  8611. '});',
  8612. 'rtl.createClass(this, "EInvalidCast", this.Exception, function () {',
  8613. '});',
  8614. 'this.vI = 0;'
  8615. ]),
  8616. LinesToStr([ // $mod.$main
  8617. 'try {',
  8618. ' $mod.vI = 1;',
  8619. '} catch ($e) {',
  8620. ' $mod.vI = 2;',
  8621. '};',
  8622. 'try {',
  8623. ' $mod.vI = 3;',
  8624. '} catch ($e) {',
  8625. ' throw $e;',
  8626. '};',
  8627. 'try {',
  8628. ' $mod.vI = 4;',
  8629. '} catch ($e) {',
  8630. ' if ($mod.EInvalidCast.isPrototypeOf($e)){',
  8631. ' throw $e',
  8632. ' } else if ($mod.Exception.isPrototypeOf($e)) {',
  8633. ' var E = $e;',
  8634. ' if (E.Msg === "") throw E;',
  8635. ' } else {',
  8636. ' $mod.vI = 5;',
  8637. ' }',
  8638. '};',
  8639. 'try {',
  8640. ' $mod.vI = 6;',
  8641. '} catch ($e) {',
  8642. ' if ($mod.EInvalidCast.isPrototypeOf($e)){' ,
  8643. ' } else throw $e',
  8644. '};',
  8645. '']));
  8646. end;
  8647. procedure TTestModule.TestTryExcept_ReservedWords;
  8648. begin
  8649. StartProgram(false);
  8650. Add([
  8651. 'type',
  8652. ' TObject = class end;',
  8653. ' Exception = class',
  8654. ' Symbol: string;',
  8655. ' end;',
  8656. 'var &try: longint;',
  8657. 'begin',
  8658. ' try',
  8659. ' &try:=4;',
  8660. ' except',
  8661. ' on Error: exception do',
  8662. ' if errOR.symBol='''' then',
  8663. ' raise ERRor;',
  8664. ' end;',
  8665. '']);
  8666. ConvertProgram;
  8667. CheckSource('TestTryExcept_ReservedWords',
  8668. LinesToStr([ // statements
  8669. 'rtl.createClass(this, "TObject", null, function () {',
  8670. ' this.$init = function () {',
  8671. ' };',
  8672. ' this.$final = function () {',
  8673. ' };',
  8674. '});',
  8675. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  8676. ' this.$init = function () {',
  8677. ' $mod.TObject.$init.call(this);',
  8678. ' this.Symbol = "";',
  8679. ' };',
  8680. '});',
  8681. 'this.Try = 0;',
  8682. '']),
  8683. LinesToStr([ // $mod.$main
  8684. 'try {',
  8685. ' $mod.Try = 4;',
  8686. '} catch ($e) {',
  8687. ' if ($mod.Exception.isPrototypeOf($e)) {',
  8688. ' var error = $e;',
  8689. ' if (error.Symbol === "") throw error;',
  8690. ' } else throw $e',
  8691. '};',
  8692. '']));
  8693. end;
  8694. procedure TTestModule.TestIfThenRaiseElse;
  8695. begin
  8696. StartProgram(false);
  8697. Add([
  8698. 'type',
  8699. ' TObject = class',
  8700. ' constructor Create;',
  8701. ' end;',
  8702. 'constructor TObject.Create;',
  8703. 'begin',
  8704. 'end;',
  8705. 'var b: boolean;',
  8706. 'begin',
  8707. ' if b then',
  8708. ' raise TObject.Create',
  8709. ' else',
  8710. ' b:=false;',
  8711. '']);
  8712. ConvertProgram;
  8713. CheckSource('TestIfThenRaiseElse',
  8714. LinesToStr([ // statements
  8715. 'rtl.createClass(this, "TObject", null, function () {',
  8716. ' this.$init = function () {',
  8717. ' };',
  8718. ' this.$final = function () {',
  8719. ' };',
  8720. ' this.Create = function () {',
  8721. ' return this;',
  8722. ' };',
  8723. '});',
  8724. 'this.b = false;',
  8725. '']),
  8726. LinesToStr([ // $mod.$main
  8727. 'if ($mod.b) {',
  8728. ' throw $mod.TObject.$create("Create")}',
  8729. ' else $mod.b = false;',
  8730. '']));
  8731. end;
  8732. procedure TTestModule.TestCaseOf;
  8733. begin
  8734. StartProgram(false);
  8735. Add([
  8736. 'const e: longint; external name ''$e'';',
  8737. 'var vI: longint;',
  8738. 'begin',
  8739. ' case vi of',
  8740. ' 1: ;',
  8741. ' 2: vi:=3;',
  8742. ' e: ;',
  8743. ' else',
  8744. ' VI:=4',
  8745. ' end;']);
  8746. ConvertProgram;
  8747. CheckSource('TestCaseOf',
  8748. LinesToStr([ // statements
  8749. 'this.vI = 0;'
  8750. ]),
  8751. LinesToStr([ // $mod.$main
  8752. 'var $tmp = $mod.vI;',
  8753. 'if ($tmp === 1) {}',
  8754. 'else if ($tmp === 2) {',
  8755. ' $mod.vI = 3}',
  8756. ' else if ($tmp === $e) {}',
  8757. 'else {',
  8758. ' $mod.vI = 4;',
  8759. '};'
  8760. ]));
  8761. end;
  8762. procedure TTestModule.TestCaseOf_UseSwitch;
  8763. begin
  8764. StartProgram(false);
  8765. Converter.UseSwitchStatement:=true;
  8766. Add('var Vi: longint;');
  8767. Add('begin');
  8768. Add(' case vi of');
  8769. Add(' 1: ;');
  8770. Add(' 2: VI:=3;');
  8771. Add(' else');
  8772. Add(' vi:=4');
  8773. Add(' end;');
  8774. ConvertProgram;
  8775. CheckSource('TestCaseOf_UseSwitch',
  8776. LinesToStr([ // statements
  8777. 'this.Vi = 0;'
  8778. ]),
  8779. LinesToStr([ // $mod.$main
  8780. 'switch ($mod.Vi) {',
  8781. 'case 1:',
  8782. ' break;',
  8783. 'case 2:',
  8784. ' $mod.Vi = 3;',
  8785. ' break;',
  8786. 'default:',
  8787. ' $mod.Vi = 4;',
  8788. '};'
  8789. ]));
  8790. end;
  8791. procedure TTestModule.TestCaseOfNoElse;
  8792. begin
  8793. StartProgram(false);
  8794. Add('var Vi: longint;');
  8795. Add('begin');
  8796. Add(' case vi of');
  8797. Add(' 1: begin vi:=2; VI:=3; end;');
  8798. Add(' end;');
  8799. ConvertProgram;
  8800. CheckSource('TestCaseOfNoElse',
  8801. LinesToStr([ // statements
  8802. 'this.Vi = 0;'
  8803. ]),
  8804. LinesToStr([ // $mod.$main
  8805. 'var $tmp = $mod.Vi;',
  8806. 'if ($tmp === 1) {',
  8807. ' $mod.Vi = 2;',
  8808. ' $mod.Vi = 3;',
  8809. '};'
  8810. ]));
  8811. end;
  8812. procedure TTestModule.TestCaseOfNoElse_UseSwitch;
  8813. begin
  8814. StartProgram(false);
  8815. Converter.UseSwitchStatement:=true;
  8816. Add('var vI: longint;');
  8817. Add('begin');
  8818. Add(' case vi of');
  8819. Add(' 1: begin VI:=2; vi:=3; end;');
  8820. Add(' end;');
  8821. ConvertProgram;
  8822. CheckSource('TestCaseOfNoElse_UseSwitch',
  8823. LinesToStr([ // statements
  8824. 'this.vI = 0;'
  8825. ]),
  8826. LinesToStr([ // $mod.$main
  8827. 'switch ($mod.vI) {',
  8828. 'case 1:',
  8829. ' $mod.vI = 2;',
  8830. ' $mod.vI = 3;',
  8831. ' break;',
  8832. '};'
  8833. ]));
  8834. end;
  8835. procedure TTestModule.TestCaseOfRange;
  8836. begin
  8837. StartProgram(false);
  8838. Add('var vI: longint;');
  8839. Add('begin');
  8840. Add(' case vi of');
  8841. Add(' 1..3: vi:=14;');
  8842. Add(' 4,5: vi:=16;');
  8843. Add(' 6..7,9..10: ;');
  8844. Add(' else ;');
  8845. Add(' end;');
  8846. ConvertProgram;
  8847. CheckSource('TestCaseOfRange',
  8848. LinesToStr([ // statements
  8849. 'this.vI = 0;'
  8850. ]),
  8851. LinesToStr([ // $mod.$main
  8852. 'var $tmp = $mod.vI;',
  8853. 'if (($tmp >= 1) && ($tmp <= 3)){',
  8854. ' $mod.vI = 14',
  8855. '} else if (($tmp === 4) || ($tmp === 5)){',
  8856. ' $mod.vI = 16',
  8857. '} else if ((($tmp >= 6) && ($tmp <= 7)) || (($tmp >= 9) && ($tmp <= 10))) ;'
  8858. ]));
  8859. end;
  8860. procedure TTestModule.TestCaseOfString;
  8861. begin
  8862. StartProgram(false);
  8863. Add([
  8864. 'var s,h: string;',
  8865. 'begin',
  8866. ' case s of',
  8867. ' ''foo'': s:=h;',
  8868. ' ''a''..''z'': h:=s;',
  8869. ' ''ў'', ''ё'': ;',
  8870. ' ''Б''..''Я'': ;',
  8871. ' end;',
  8872. '']);
  8873. ConvertProgram;
  8874. CheckSource('TestCaseOfString',
  8875. LinesToStr([ // statements
  8876. 'this.s = "";',
  8877. 'this.h = "";',
  8878. '']),
  8879. LinesToStr([ // $mod.$main
  8880. 'var $tmp = $mod.s;',
  8881. 'if ($tmp === "foo") {',
  8882. ' $mod.s = $mod.h}',
  8883. ' else if (($tmp.length === 1) && ($tmp >= "a") && ($tmp <= "z")) {',
  8884. ' $mod.h = $mod.s}',
  8885. ' else if (($tmp === "ў") || ($tmp === "ё")) {}',
  8886. ' else if (($tmp.length === 1) && ($tmp >= "Б") && ($tmp <= "Я")) ;',
  8887. '']));
  8888. end;
  8889. procedure TTestModule.TestCaseOfChar;
  8890. begin
  8891. StartProgram(false);
  8892. Add([
  8893. 'var s,h: char;',
  8894. 'begin',
  8895. ' case s of',
  8896. ' ''a''..''z'': h:=s;',
  8897. ' ''ä'': ;',
  8898. ' ''ў'', ''ё'': ;',
  8899. ' ''Б''..''Я'': ;',
  8900. ' end;',
  8901. '']);
  8902. ConvertProgram;
  8903. CheckSource('TestCaseOfString',
  8904. LinesToStr([ // statements
  8905. 'this.s = "";',
  8906. 'this.h = "";',
  8907. '']),
  8908. LinesToStr([ // $mod.$main
  8909. 'var $tmp = $mod.s;',
  8910. 'if (($tmp >= "a") && ($tmp <= "z")) {',
  8911. ' $mod.h = $mod.s}',
  8912. ' else if ($tmp === "ä") {}',
  8913. ' else if (($tmp === "ў") || ($tmp === "ё")) {}',
  8914. ' else if (($tmp >= "Б") && ($tmp <= "Я")) ;',
  8915. '']));
  8916. end;
  8917. procedure TTestModule.TestCaseOfExternalClassConst;
  8918. begin
  8919. StartProgram(false);
  8920. Add([
  8921. '{$modeswitch externalclass}',
  8922. 'type',
  8923. ' TBird = class external name ''Bird''',
  8924. ' const e: longint;',
  8925. ' end;',
  8926. 'var vI: longint;',
  8927. 'begin',
  8928. ' case vi of',
  8929. ' 1: vi:=3;',
  8930. ' TBird.e: ;',
  8931. ' end;']);
  8932. ConvertProgram;
  8933. CheckSource('TestCaseOfExternalClassConst',
  8934. LinesToStr([ // statements
  8935. 'this.vI = 0;'
  8936. ]),
  8937. LinesToStr([ // $mod.$main
  8938. 'var $tmp = $mod.vI;',
  8939. 'if ($tmp === 1) {',
  8940. ' $mod.vI = 3}',
  8941. ' else if ($tmp === Bird.e) ;'
  8942. ]));
  8943. end;
  8944. procedure TTestModule.TestDebugger;
  8945. begin
  8946. StartProgram(false);
  8947. Add([
  8948. 'procedure DoIt;',
  8949. 'begin',
  8950. ' deBugger;',
  8951. ' DeBugger();',
  8952. 'end;',
  8953. 'begin',
  8954. ' Debugger;']);
  8955. ConvertProgram;
  8956. CheckSource('TestDebugger',
  8957. LinesToStr([ // statements
  8958. 'this.DoIt = function () {',
  8959. ' debugger;',
  8960. ' debugger;',
  8961. '};',
  8962. '']),
  8963. LinesToStr([ // $mod.$main
  8964. 'debugger;',
  8965. '']));
  8966. end;
  8967. procedure TTestModule.TestArray_Dynamic;
  8968. begin
  8969. StartProgram(false);
  8970. Add([
  8971. 'type',
  8972. ' TArrayInt = array of longint;',
  8973. 'var',
  8974. ' Arr: TArrayInt;',
  8975. ' i: longint;',
  8976. ' b: boolean;',
  8977. 'begin',
  8978. ' SetLength(arr,3);',
  8979. ' arr[0]:=4;',
  8980. ' arr[1]:=length(arr)+arr[0];',
  8981. ' arr[i]:=5;',
  8982. ' arr[arr[i]]:=arr[6];',
  8983. ' i:=low(arr);',
  8984. ' i:=high(arr);',
  8985. ' b:=Assigned(arr);',
  8986. ' Arr:=default(TArrayInt);']);
  8987. ConvertProgram;
  8988. CheckSource('TestArray_Dynamic',
  8989. LinesToStr([ // statements
  8990. 'this.Arr = [];',
  8991. 'this.i = 0;',
  8992. 'this.b = false;'
  8993. ]),
  8994. LinesToStr([ // $mod.$main
  8995. '$mod.Arr = rtl.arraySetLength($mod.Arr,0,3);',
  8996. '$mod.Arr[0] = 4;',
  8997. '$mod.Arr[1] = rtl.length($mod.Arr) + $mod.Arr[0];',
  8998. '$mod.Arr[$mod.i] = 5;',
  8999. '$mod.Arr[$mod.Arr[$mod.i]] = $mod.Arr[6];',
  9000. '$mod.i = 0;',
  9001. '$mod.i = rtl.length($mod.Arr) - 1;',
  9002. '$mod.b = rtl.length($mod.Arr) > 0;',
  9003. '$mod.Arr = [];',
  9004. '']));
  9005. end;
  9006. procedure TTestModule.TestArray_Dynamic_Nil;
  9007. begin
  9008. StartProgram(false);
  9009. Add('type');
  9010. Add(' TArrayInt = array of longint;');
  9011. Add('var');
  9012. Add(' Arr: TArrayInt;');
  9013. Add('procedure DoIt(const i: TArrayInt; j: TArrayInt); begin end;');
  9014. Add('begin');
  9015. Add(' arr:=nil;');
  9016. Add(' if arr=nil then;');
  9017. Add(' if nil=arr then;');
  9018. Add(' if arr<>nil then;');
  9019. Add(' if nil<>arr then;');
  9020. Add(' DoIt(nil,nil);');
  9021. ConvertProgram;
  9022. CheckSource('TestArray_Dynamic',
  9023. LinesToStr([ // statements
  9024. 'this.Arr = [];',
  9025. 'this.DoIt = function(i,j){',
  9026. '};'
  9027. ]),
  9028. LinesToStr([ // $mod.$main
  9029. '$mod.Arr = [];',
  9030. 'if (rtl.length($mod.Arr) === 0) ;',
  9031. 'if (rtl.length($mod.Arr) === 0) ;',
  9032. 'if (rtl.length($mod.Arr) > 0) ;',
  9033. 'if (rtl.length($mod.Arr) > 0) ;',
  9034. '$mod.DoIt([],[]);',
  9035. '']));
  9036. end;
  9037. procedure TTestModule.TestArray_DynMultiDimensional;
  9038. begin
  9039. StartProgram(false);
  9040. Add([
  9041. 'type',
  9042. ' TArrayInt = array of longint;',
  9043. ' TArrayArrayInt = array of TArrayInt;',
  9044. 'var',
  9045. ' Arr: TArrayInt;',
  9046. ' Arr2: TArrayArrayInt;',
  9047. ' i: longint;',
  9048. 'begin',
  9049. ' arr2:=nil;',
  9050. ' if arr2=nil then;',
  9051. ' if nil=arr2 then;',
  9052. ' i:=low(arr2);',
  9053. ' i:=low(arr2[1]);',
  9054. ' i:=high(arr2);',
  9055. ' i:=high(arr2[2]);',
  9056. ' arr2[3]:=arr;',
  9057. ' arr2[4][5]:=i;',
  9058. ' i:=arr2[6][7];',
  9059. ' arr2[8,9]:=i;',
  9060. ' i:=arr2[10,11];',
  9061. ' SetLength(arr2,14);',
  9062. ' SetLength(arr2[15],16);']);
  9063. ConvertProgram;
  9064. CheckSource('TestArray_Dynamic',
  9065. LinesToStr([ // statements
  9066. 'this.Arr = [];',
  9067. 'this.Arr2 = [];',
  9068. 'this.i = 0;'
  9069. ]),
  9070. LinesToStr([ // $mod.$main
  9071. '$mod.Arr2 = [];',
  9072. 'if (rtl.length($mod.Arr2) === 0) ;',
  9073. 'if (rtl.length($mod.Arr2) === 0) ;',
  9074. '$mod.i = 0;',
  9075. '$mod.i = 0;',
  9076. '$mod.i = rtl.length($mod.Arr2) - 1;',
  9077. '$mod.i = rtl.length($mod.Arr2[2]) - 1;',
  9078. '$mod.Arr2[3] = rtl.arrayRef($mod.Arr);',
  9079. '$mod.Arr2[4][5] = $mod.i;',
  9080. '$mod.i = $mod.Arr2[6][7];',
  9081. '$mod.Arr2[8][9] = $mod.i;',
  9082. '$mod.i = $mod.Arr2[10][11];',
  9083. '$mod.Arr2 = rtl.arraySetLength($mod.Arr2, [], 14);',
  9084. '$mod.Arr2[15] = rtl.arraySetLength($mod.Arr2[15], 0, 16);',
  9085. '']));
  9086. end;
  9087. procedure TTestModule.TestArray_DynamicAssign;
  9088. begin
  9089. StartProgram(false);
  9090. Add([
  9091. 'type',
  9092. ' TArrayInt = array of longint;',
  9093. ' TArrayArrayInt = array of TArrayInt;',
  9094. 'procedure Run(a: TArrayInt; const b: TArrayInt; constref c: TArrayInt);',
  9095. 'begin',
  9096. 'end;',
  9097. 'procedure Fly(var a: TArrayInt);',
  9098. 'begin',
  9099. 'end;',
  9100. 'var',
  9101. ' Arr: TArrayInt;',
  9102. ' Arr2: TArrayArrayInt;',
  9103. 'begin',
  9104. ' arr:=nil;',
  9105. ' arr2:=nil;',
  9106. ' arr2[1]:=nil;',
  9107. ' arr2[2]:=arr;',
  9108. ' Run(arr,arr,arr);',
  9109. ' Fly(arr);',
  9110. ' Run(arr2[4],arr2[5],arr2[6]);',
  9111. ' Fly(arr2[7]);',
  9112. '']);
  9113. ConvertProgram;
  9114. CheckSource('TestArray_DynamicAssign',
  9115. LinesToStr([ // statements
  9116. 'this.Run = function (a, b, c) {',
  9117. '};',
  9118. 'this.Fly = function (a) {',
  9119. '};',
  9120. 'this.Arr = [];',
  9121. 'this.Arr2 = [];',
  9122. '']),
  9123. LinesToStr([ // $mod.$main
  9124. '$mod.Arr = [];',
  9125. '$mod.Arr2 = [];',
  9126. '$mod.Arr2[1] = [];',
  9127. '$mod.Arr2[2] = rtl.arrayRef($mod.Arr);',
  9128. '$mod.Run(rtl.arrayRef($mod.Arr), $mod.Arr, $mod.Arr);',
  9129. '$mod.Fly({',
  9130. ' p: $mod,',
  9131. ' get: function () {',
  9132. ' return this.p.Arr;',
  9133. ' },',
  9134. ' set: function (v) {',
  9135. ' this.p.Arr = v;',
  9136. ' }',
  9137. '});',
  9138. '$mod.Run(rtl.arrayRef($mod.Arr2[4]), $mod.Arr2[5], $mod.Arr2[6]);',
  9139. '$mod.Fly({',
  9140. ' a: 7,',
  9141. ' p: $mod.Arr2,',
  9142. ' get: function () {',
  9143. ' return this.p[this.a];',
  9144. ' },',
  9145. ' set: function (v) {',
  9146. ' this.p[this.a] = v;',
  9147. ' }',
  9148. '});',
  9149. '']));
  9150. end;
  9151. procedure TTestModule.TestArray_StaticInt;
  9152. begin
  9153. StartProgram(false);
  9154. Add('type');
  9155. Add(' TArrayInt = array[2..4] of longint;');
  9156. Add('var');
  9157. Add(' Arr: TArrayInt;');
  9158. Add(' Arr2: TArrayInt = (5,6,7);');
  9159. Add(' i: longint;');
  9160. Add(' b: boolean;');
  9161. Add('begin');
  9162. Add(' arr[2]:=4;');
  9163. Add(' arr[3]:=arr[2]+arr[3];');
  9164. Add(' arr[i]:=5;');
  9165. Add(' arr[arr[i]]:=arr[high(arr)];');
  9166. Add(' i:=low(arr);');
  9167. Add(' i:=high(arr);');
  9168. Add(' b:=arr[2]=arr[3];');
  9169. Add(' arr:=default(TArrayInt);');
  9170. ConvertProgram;
  9171. CheckSource('TestArray_StaticInt',
  9172. LinesToStr([ // statements
  9173. 'this.Arr = rtl.arraySetLength(null,0,3);',
  9174. 'this.Arr2 = [5, 6, 7];',
  9175. 'this.i = 0;',
  9176. 'this.b = false;'
  9177. ]),
  9178. LinesToStr([ // $mod.$main
  9179. '$mod.Arr[0] = 4;',
  9180. '$mod.Arr[1] = $mod.Arr[0] + $mod.Arr[1];',
  9181. '$mod.Arr[$mod.i-2] = 5;',
  9182. '$mod.Arr[$mod.Arr[$mod.i-2]-2] = $mod.Arr[2];',
  9183. '$mod.i = 2;',
  9184. '$mod.i = 4;',
  9185. '$mod.b = $mod.Arr[0] === $mod.Arr[1];',
  9186. '$mod.Arr = rtl.arraySetLength(null,0,3);',
  9187. '']));
  9188. end;
  9189. procedure TTestModule.TestArray_StaticBool;
  9190. begin
  9191. StartProgram(false);
  9192. Add('type');
  9193. Add(' TBools = array[boolean] of boolean;');
  9194. Add(' TBool2 = array[true..true] of boolean;');
  9195. Add('var');
  9196. Add(' Arr: TBools;');
  9197. Add(' Arr2: TBool2;');
  9198. Add(' Arr3: TBools = (true,false);');
  9199. Add(' b: boolean;');
  9200. Add('begin');
  9201. Add(' b:=low(arr);');
  9202. Add(' b:=high(arr);');
  9203. Add(' arr[true]:=false;');
  9204. Add(' arr[false]:=arr[b] or arr[true];');
  9205. Add(' arr[b]:=true;');
  9206. Add(' arr[arr[b]]:=arr[high(arr)];');
  9207. Add(' b:=arr[false]=arr[true];');
  9208. Add(' b:=low(arr2);');
  9209. Add(' b:=high(arr2);');
  9210. Add(' arr2[true]:=true;');
  9211. Add(' arr2[true]:=arr2[true] and arr2[b];');
  9212. Add(' arr2[b]:=false;');
  9213. ConvertProgram;
  9214. CheckSource('TestArray_StaticBool',
  9215. LinesToStr([ // statements
  9216. 'this.Arr = rtl.arraySetLength(null,false,2);',
  9217. 'this.Arr2 = rtl.arraySetLength(null,false,1);',
  9218. 'this.Arr3 = [true, false];',
  9219. 'this.b = false;'
  9220. ]),
  9221. LinesToStr([ // $mod.$main
  9222. '$mod.b = false;',
  9223. '$mod.b = true;',
  9224. '$mod.Arr[1] = false;',
  9225. '$mod.Arr[0] = $mod.Arr[+$mod.b] || $mod.Arr[1];',
  9226. '$mod.Arr[+$mod.b] = true;',
  9227. '$mod.Arr[+$mod.Arr[+$mod.b]] = $mod.Arr[1];',
  9228. '$mod.b = $mod.Arr[0] === $mod.Arr[1];',
  9229. '$mod.b = true;',
  9230. '$mod.b = true;',
  9231. '$mod.Arr2[0] = true;',
  9232. '$mod.Arr2[0] = $mod.Arr2[0] && $mod.Arr2[1-$mod.b];',
  9233. '$mod.Arr2[1-$mod.b] = false;',
  9234. '']));
  9235. end;
  9236. procedure TTestModule.TestArray_StaticChar;
  9237. begin
  9238. StartProgram(false);
  9239. Add([
  9240. 'type',
  9241. ' TChars = array[char] of char;',
  9242. ' TChars2 = array[''a''..''z''] of char;',
  9243. 'var',
  9244. ' Arr: TChars;',
  9245. ' Arr2: TChars2;',
  9246. ' Arr3: array[2..4] of char = (''p'',''a'',''s'');',
  9247. ' Arr4: array[11..13] of char = ''pas'';',
  9248. ' Arr5: array[21..22] of char = ''äö'';',
  9249. ' Arr6: array[31..32] of char = ''ä''+''ö'';',
  9250. ' c: char;',
  9251. ' b: boolean;',
  9252. 'begin',
  9253. ' c:=low(arr);',
  9254. ' c:=high(arr);',
  9255. ' arr[''B'']:=''a'';',
  9256. ' arr[''D'']:=arr[c];',
  9257. ' arr[c]:=arr[''d''];',
  9258. ' arr[arr[c]]:=arr[high(arr)];',
  9259. ' b:=arr[low(arr)]=arr[''e''];',
  9260. ' c:=low(arr2);',
  9261. ' c:=high(arr2);',
  9262. ' arr2[''b'']:=''f'';',
  9263. ' arr2[''a'']:=arr2[c];',
  9264. ' arr2[c]:=arr2[''g''];']);
  9265. ConvertProgram;
  9266. CheckSource('TestArray_StaticChar',
  9267. LinesToStr([ // statements
  9268. 'this.Arr = rtl.arraySetLength(null, "", 65536);',
  9269. 'this.Arr2 = rtl.arraySetLength(null, "", 26);',
  9270. 'this.Arr3 = ["p", "a", "s"];',
  9271. 'this.Arr4 = ["p", "a", "s"];',
  9272. 'this.Arr5 = ["ä", "ö"];',
  9273. 'this.Arr6 = ["ä", "ö"];',
  9274. 'this.c = "";',
  9275. 'this.b = false;',
  9276. '']),
  9277. LinesToStr([ // $mod.$main
  9278. '$mod.c = "\x00";',
  9279. '$mod.c = "\uFFFF";',
  9280. '$mod.Arr[66] = "a";',
  9281. '$mod.Arr[68] = $mod.Arr[$mod.c.charCodeAt()];',
  9282. '$mod.Arr[$mod.c.charCodeAt()] = $mod.Arr[100];',
  9283. '$mod.Arr[$mod.Arr[$mod.c.charCodeAt()].charCodeAt()] = $mod.Arr[65535];',
  9284. '$mod.b = $mod.Arr[0] === $mod.Arr[101];',
  9285. '$mod.c = "a";',
  9286. '$mod.c = "z";',
  9287. '$mod.Arr2[1] = "f";',
  9288. '$mod.Arr2[0] = $mod.Arr2[$mod.c.charCodeAt() - 97];',
  9289. '$mod.Arr2[$mod.c.charCodeAt() - 97] = $mod.Arr2[6];',
  9290. '']));
  9291. end;
  9292. procedure TTestModule.TestArray_StaticMultiDim;
  9293. begin
  9294. StartProgram(false);
  9295. Add([
  9296. 'type',
  9297. ' TArrayInt = array[1..3] of longint;',
  9298. ' TArrayArrayInt = array[5..6] of TArrayInt;',
  9299. ' TArrayArrayArrayInt = array[7..8] of TArrayArrayInt;',
  9300. ' TArrayDim2Int = array[1..2,1..3] of longint;',
  9301. ' TArrayDim3Int = array[1..2,1..3,1..4] of longint;',
  9302. ' TArrayDim4Int = array[1..2,1..3,1..4,1..5] of longint;',
  9303. 'var',
  9304. ' Arr: TArrayInt;',
  9305. ' Arr2: TArrayArrayInt;',
  9306. ' Arr3: array[boolean] of TArrayInt = ((11,12,13),(21,22,23));',
  9307. ' Arr4: TArrayArrayInt;',
  9308. ' ArrDim2: TArrayDim2Int;',
  9309. ' ArrDim3: TArrayDim3Int;',
  9310. ' ArrDim4: TArrayDim4Int;',
  9311. ' i: longint;',
  9312. 'begin',
  9313. ' i:=low(arr);',
  9314. ' i:=low(arr2);',
  9315. ' i:=low(arr2[5]);',
  9316. ' i:=high(arr);',
  9317. ' i:=high(arr2);',
  9318. ' i:=high(arr2[6]);',
  9319. ' arr2[5]:=arr;',
  9320. ' arr2[6][2]:=i;',
  9321. ' i:=arr2[6][3];',
  9322. ' arr2[6,3]:=i;',
  9323. ' i:=arr2[5,2];',
  9324. ' arr2:=arr2;',// clone multi dim static array
  9325. ' arr3:=arr3;',// clone anonymous multi dim static array
  9326. ' arr4:=arr4;',
  9327. ' Arr:=Arr;',
  9328. ' ArrDim2:=ArrDim2;',
  9329. ' ArrDim3:=ArrDim3;',
  9330. ' ArrDim4:=ArrDim4;',
  9331. '']);
  9332. ConvertProgram;
  9333. CheckSource('TestArray_StaticMultiDim',
  9334. LinesToStr([ // statements
  9335. 'this.TArrayArrayInt$clone = function (a) {',
  9336. ' var b = [];',
  9337. ' b.length = 2;',
  9338. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  9339. ' return b;',
  9340. '};',
  9341. 'this.TArrayArrayArrayInt$clone = function (a) {',
  9342. ' var b = [];',
  9343. ' b.length = 2;',
  9344. ' for (var c = 0; c < 2; c++) b[c] = $mod.TArrayArrayInt$clone(a[c]);',
  9345. ' return b;',
  9346. '};',
  9347. 'this.TArrayDim2Int$clone = function (a) {',
  9348. ' var b = [];',
  9349. ' b.length = 2;',
  9350. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  9351. ' return b;',
  9352. '};',
  9353. 'this.TArrayDim3Int$clone = function (a) {',
  9354. ' var b = [];',
  9355. ' b.length = 2;',
  9356. ' for (var c = 0; c < 2; c++) {',
  9357. ' var d = b[c] = [];',
  9358. ' d.length = 3;',
  9359. ' var e = a[c];',
  9360. ' for (var f = 0; f < 3; f++) d[f] = e[f].slice(0);',
  9361. ' };',
  9362. ' return b;',
  9363. '};',
  9364. 'this.TArrayDim4Int$clone = function (a) {',
  9365. ' var b = [];',
  9366. ' b.length = 2;',
  9367. ' for (var c = 0; c < 2; c++) {',
  9368. ' var d = b[c] = [];',
  9369. ' d.length = 3;',
  9370. ' var e = a[c];',
  9371. ' for (var f = 0; f < 3; f++) {',
  9372. ' var g = d[f] = [];',
  9373. ' g.length = 4;',
  9374. ' var h = e[f];',
  9375. ' for (var i = 0; i < 4; i++) g[i] = h[i].slice(0);',
  9376. ' };',
  9377. ' };',
  9378. ' return b;',
  9379. '};',
  9380. 'this.Arr = rtl.arraySetLength(null, 0, 3);',
  9381. 'this.Arr2 = rtl.arraySetLength(null, 0, 2, 3);',
  9382. 'this.Arr3$a$clone = function (a) {',
  9383. ' var b = [];',
  9384. ' b.length = 2;',
  9385. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  9386. ' return b;',
  9387. '};',
  9388. 'this.Arr3 = [[11, 12, 13], [21, 22, 23]];',
  9389. 'this.Arr4 = rtl.arraySetLength(null, 0, 2, 3);',
  9390. 'this.ArrDim2 = rtl.arraySetLength(null, 0, 2, 3);',
  9391. 'this.ArrDim3 = rtl.arraySetLength(null, 0, 2, 3, 4);',
  9392. 'this.ArrDim4 = rtl.arraySetLength(',
  9393. ' null,',
  9394. ' 0,',
  9395. ' 2,',
  9396. ' 3,',
  9397. ' 4,',
  9398. ' 5',
  9399. ');',
  9400. 'this.i = 0;'
  9401. ]),
  9402. LinesToStr([ // $mod.$main
  9403. '$mod.i = 1;',
  9404. '$mod.i = 5;',
  9405. '$mod.i = 1;',
  9406. '$mod.i = 3;',
  9407. '$mod.i = 6;',
  9408. '$mod.i = 3;',
  9409. '$mod.Arr2[0] = $mod.Arr.slice(0);',
  9410. '$mod.Arr2[1][1] = $mod.i;',
  9411. '$mod.i = $mod.Arr2[1][2];',
  9412. '$mod.Arr2[1][2] = $mod.i;',
  9413. '$mod.i = $mod.Arr2[0][1];',
  9414. '$mod.Arr2 = $mod.TArrayArrayInt$clone($mod.Arr2);',
  9415. '$mod.Arr3 = $mod.Arr3$a$clone($mod.Arr3);',
  9416. '$mod.Arr4 = $mod.TArrayArrayInt$clone($mod.Arr4);',
  9417. '$mod.Arr = $mod.Arr.slice(0);',
  9418. '$mod.ArrDim2 = $mod.TArrayDim2Int$clone($mod.ArrDim2);',
  9419. '$mod.ArrDim3 = $mod.TArrayDim3Int$clone($mod.ArrDim3);',
  9420. '$mod.ArrDim4 = $mod.TArrayDim4Int$clone($mod.ArrDim4);',
  9421. '']));
  9422. end;
  9423. procedure TTestModule.TestArray_StaticInFunction;
  9424. begin
  9425. StartProgram(false);
  9426. Add([
  9427. 'const TArrayInt = 3;',
  9428. 'const TArrayArrayInt = 4;',
  9429. 'procedure DoIt;',
  9430. 'type',
  9431. ' TArrayInt = array[1..3] of longint;',
  9432. ' TArrayArrayInt = array[5..6] of TArrayInt;',
  9433. 'var',
  9434. ' Arr: TArrayInt;',
  9435. ' Arr2: TArrayArrayInt;',
  9436. ' Arr3: array[boolean] of TArrayInt = ((11,12,13),(21,22,23));',
  9437. ' i: longint;',
  9438. 'begin',
  9439. ' arr2[5]:=arr;',
  9440. ' arr2:=arr2;',// clone multi dim static array
  9441. ' arr3:=arr3;',// clone multi dim anonymous static array
  9442. 'end;',
  9443. 'begin',
  9444. '']);
  9445. ConvertProgram;
  9446. CheckSource('TestArray_StaticInFunction',
  9447. LinesToStr([ // statements
  9448. 'this.TArrayInt = 3;',
  9449. 'this.TArrayArrayInt = 4;',
  9450. 'var TArrayArrayInt$1$clone = function (a) {',
  9451. ' var b = [];',
  9452. ' b.length = 2;',
  9453. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  9454. ' return b;',
  9455. '};',
  9456. 'var Arr3$a$clone = function (a) {',
  9457. ' var b = [];',
  9458. ' b.length = 2;',
  9459. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  9460. ' return b;',
  9461. '};',
  9462. 'this.DoIt = function () {',
  9463. ' var Arr = rtl.arraySetLength(null, 0, 3);',
  9464. ' var Arr2 = rtl.arraySetLength(null, 0, 2, 3);',
  9465. ' var Arr3 = [[11, 12, 13], [21, 22, 23]];',
  9466. ' var i = 0;',
  9467. ' Arr2[0] = Arr.slice(0);',
  9468. ' Arr2 = TArrayArrayInt$1$clone(Arr2);',
  9469. ' Arr3 = Arr3$a$clone(Arr3);',
  9470. '};',
  9471. '']),
  9472. LinesToStr([ // $mod.$main
  9473. '']));
  9474. end;
  9475. procedure TTestModule.TestArray_StaticMultiDimEqualNotImplemented;
  9476. begin
  9477. StartProgram(false);
  9478. Add([
  9479. 'type',
  9480. ' TArrayInt = array[1..3,1..2] of longint;',
  9481. 'var',
  9482. ' a,b: TArrayInt;',
  9483. 'begin',
  9484. ' if a=b then ;',
  9485. '']);
  9486. SetExpectedPasResolverError('compare static array is not supported',
  9487. nXIsNotSupported);
  9488. ConvertProgram;
  9489. end;
  9490. procedure TTestModule.TestArrayOfRecord;
  9491. begin
  9492. StartProgram(false);
  9493. Add([
  9494. 'type',
  9495. ' TRec = record',
  9496. ' Int: longint;',
  9497. ' end;',
  9498. ' TArrayRec = array of TRec;',
  9499. 'procedure DoIt(vd: TRec; const vc: TRec; var vv: TRec);',
  9500. 'begin',
  9501. 'end;',
  9502. 'var',
  9503. ' Arr: TArrayRec;',
  9504. ' r: TRec;',
  9505. ' i: longint;',
  9506. 'begin',
  9507. ' SetLength(arr,3);',
  9508. ' arr[0].int:=4;',
  9509. ' arr[1].int:=length(arr)+arr[2].int;',
  9510. ' arr[arr[i].int].int:=arr[5].int;',
  9511. ' arr[7]:=r;',
  9512. ' r:=arr[8];',
  9513. ' i:=low(arr);',
  9514. ' i:=high(arr);',
  9515. ' DoIt(Arr[9],Arr[10],Arr[11]);']);
  9516. ConvertProgram;
  9517. CheckSource('TestArrayOfRecord',
  9518. LinesToStr([ // statements
  9519. 'rtl.recNewT(this, "TRec", function () {',
  9520. ' this.Int = 0;',
  9521. ' this.$eq = function (b) {',
  9522. ' return this.Int === b.Int;',
  9523. ' };',
  9524. ' this.$assign = function (s) {',
  9525. ' this.Int = s.Int;',
  9526. ' return this;',
  9527. ' };',
  9528. '});',
  9529. 'this.DoIt = function (vd, vc, vv) {',
  9530. '};',
  9531. 'this.Arr = [];',
  9532. 'this.r = this.TRec.$new();',
  9533. 'this.i = 0;'
  9534. ]),
  9535. LinesToStr([ // $mod.$main
  9536. '$mod.Arr = rtl.arraySetLength($mod.Arr,$mod.TRec,3);',
  9537. '$mod.Arr[0].Int = 4;',
  9538. '$mod.Arr[1].Int = rtl.length($mod.Arr)+$mod.Arr[2].Int;',
  9539. '$mod.Arr[$mod.Arr[$mod.i].Int].Int = $mod.Arr[5].Int;',
  9540. '$mod.Arr[7].$assign($mod.r);',
  9541. '$mod.r.$assign($mod.Arr[8]);',
  9542. '$mod.i = 0;',
  9543. '$mod.i = rtl.length($mod.Arr)-1;',
  9544. '$mod.DoIt($mod.TRec.$clone($mod.Arr[9]), $mod.Arr[10], $mod.Arr[11]);',
  9545. '']));
  9546. end;
  9547. procedure TTestModule.TestArray_StaticRecord;
  9548. begin
  9549. StartProgram(false);
  9550. Add([
  9551. 'type',
  9552. ' TRec = record',
  9553. ' Int: longint;',
  9554. ' end;',
  9555. ' TArrayRec = array[1..2] of TRec;',
  9556. 'var',
  9557. ' Arr: TArrayRec;',
  9558. 'begin',
  9559. ' arr[1].int:=length(arr)+low(arr)+high(arr);',
  9560. '']);
  9561. ConvertProgram;
  9562. CheckSource('TestArray_StaticRecord',
  9563. LinesToStr([ // statements
  9564. 'rtl.recNewT(this, "TRec", function () {',
  9565. ' this.Int = 0;',
  9566. ' this.$eq = function (b) {',
  9567. ' return this.Int === b.Int;',
  9568. ' };',
  9569. ' this.$assign = function (s) {',
  9570. ' this.Int = s.Int;',
  9571. ' return this;',
  9572. ' };',
  9573. '});',
  9574. 'this.TArrayRec$clone = function (a) {',
  9575. ' var b = [];',
  9576. ' b.length = 2;',
  9577. ' for (var c = 0; c < 2; c++) b[c] = $mod.TRec.$clone(a[c]);',
  9578. ' return b;',
  9579. '};',
  9580. 'this.Arr = rtl.arraySetLength(null, this.TRec, 2);',
  9581. '']),
  9582. LinesToStr([ // $mod.$main
  9583. '$mod.Arr[0].Int = 2 + 1 + 2;']));
  9584. end;
  9585. procedure TTestModule.TestArrayOfSet;
  9586. begin
  9587. StartProgram(false);
  9588. Add([
  9589. 'type',
  9590. ' TFlag = (big,small);',
  9591. ' TSetOfFlag = set of tflag;',
  9592. ' TArrayFlag = array of TSetOfFlag;',
  9593. 'procedure DoIt(const a: Tarrayflag);',
  9594. 'begin',
  9595. 'end;',
  9596. 'var',
  9597. ' f: TFlag;',
  9598. ' s: TSetOfFlag;',
  9599. ' Arr: TArrayFlag;',
  9600. ' i: longint;',
  9601. 'begin',
  9602. ' SetLength(arr,3);',
  9603. ' arr[0]:=s;',
  9604. ' arr[1]:=[big];',
  9605. ' arr[2]:=[big]+s;',
  9606. ' arr[3]:=s+[big];',
  9607. ' arr[4]:=arr[5];',
  9608. ' s:=arr[6];',
  9609. ' i:=low(arr);',
  9610. ' i:=high(arr);',
  9611. ' DoIt(arr);',
  9612. ' DoIt([s]);',
  9613. ' DoIt([[],s]);',
  9614. ' DoIt([s,[]]);',
  9615. '']);
  9616. ConvertProgram;
  9617. CheckSource('TestArrayOfSet',
  9618. LinesToStr([ // statements
  9619. 'this.TFlag = {',
  9620. ' "0": "big",',
  9621. ' big: 0,',
  9622. ' "1": "small",',
  9623. ' small: 1',
  9624. '};',
  9625. 'this.DoIt = function (a) {',
  9626. '};',
  9627. 'this.f = 0;',
  9628. 'this.s = {};',
  9629. 'this.Arr = [];',
  9630. 'this.i = 0;',
  9631. '']),
  9632. LinesToStr([ // $mod.$main
  9633. '$mod.Arr = rtl.arraySetLength($mod.Arr, {}, 3);',
  9634. '$mod.Arr[0] = rtl.refSet($mod.s);',
  9635. '$mod.Arr[1] = rtl.createSet($mod.TFlag.big);',
  9636. '$mod.Arr[2] = rtl.unionSet(rtl.createSet($mod.TFlag.big), $mod.s);',
  9637. '$mod.Arr[3] = rtl.unionSet($mod.s, rtl.createSet($mod.TFlag.big));',
  9638. '$mod.Arr[4] = rtl.refSet($mod.Arr[5]);',
  9639. '$mod.s = rtl.refSet($mod.Arr[6]);',
  9640. '$mod.i = 0;',
  9641. '$mod.i = rtl.length($mod.Arr) - 1;',
  9642. '$mod.DoIt($mod.Arr);',
  9643. '$mod.DoIt([rtl.refSet($mod.s)]);',
  9644. '$mod.DoIt([{}, rtl.refSet($mod.s)]);',
  9645. '$mod.DoIt([rtl.refSet($mod.s), {}]);',
  9646. '']));
  9647. end;
  9648. procedure TTestModule.TestArray_DynAsParam;
  9649. begin
  9650. StartProgram(false);
  9651. Add([
  9652. 'type integer = longint;',
  9653. 'type TArrInt = array of integer;',
  9654. 'procedure DoIt(vG: TArrInt; const vH: TArrInt; var vI: TArrInt);',
  9655. 'var vJ: TArrInt;',
  9656. 'begin',
  9657. ' vg:=vg;',
  9658. ' vj:=vh;',
  9659. ' vi:=vi;',
  9660. ' doit(vg,vg,vg);',
  9661. ' doit(vh,vh,vj);',
  9662. ' doit(vi,vi,vi);',
  9663. ' doit(vj,vj,vj);',
  9664. 'end;',
  9665. 'var i: TArrInt;',
  9666. 'begin',
  9667. ' doit(i,i,i);']);
  9668. ConvertProgram;
  9669. CheckSource('TestArray_DynAsParams',
  9670. LinesToStr([ // statements
  9671. 'this.DoIt = function (vG,vH,vI) {',
  9672. ' var vJ = [];',
  9673. ' vG = rtl.arrayRef(vG);',
  9674. ' vJ = rtl.arrayRef(vH);',
  9675. ' vI.set(rtl.arrayRef(vI.get()));',
  9676. ' $mod.DoIt(rtl.arrayRef(vG), vG, {',
  9677. ' get: function () {',
  9678. ' return vG;',
  9679. ' },',
  9680. ' set: function (v) {',
  9681. ' vG = v;',
  9682. ' }',
  9683. ' });',
  9684. ' $mod.DoIt(rtl.arrayRef(vH), vH, {',
  9685. ' get: function () {',
  9686. ' return vJ;',
  9687. ' },',
  9688. ' set: function (v) {',
  9689. ' vJ = v;',
  9690. ' }',
  9691. ' });',
  9692. ' $mod.DoIt(rtl.arrayRef(vI.get()), vI.get(), vI);',
  9693. ' $mod.DoIt(rtl.arrayRef(vJ), vJ, {',
  9694. ' get: function () {',
  9695. ' return vJ;',
  9696. ' },',
  9697. ' set: function (v) {',
  9698. ' vJ = v;',
  9699. ' }',
  9700. ' });',
  9701. '};',
  9702. 'this.i = [];'
  9703. ]),
  9704. LinesToStr([
  9705. '$mod.DoIt(rtl.arrayRef($mod.i),$mod.i,{',
  9706. ' p: $mod,',
  9707. ' get: function () {',
  9708. ' return this.p.i;',
  9709. ' },',
  9710. ' set: function (v) {',
  9711. ' this.p.i = v;',
  9712. ' }',
  9713. '});'
  9714. ]));
  9715. end;
  9716. procedure TTestModule.TestArray_StaticAsParam;
  9717. begin
  9718. StartProgram(false);
  9719. Add([
  9720. 'type integer = longint;',
  9721. 'type TArrInt = array[1..2] of integer;',
  9722. 'procedure DoIt(vG: TArrInt; const vH: TArrInt; var vI: TArrInt);',
  9723. 'var vJ: TArrInt;',
  9724. 'begin',
  9725. ' vg:=vg;',
  9726. ' vj:=vh;',
  9727. ' vi:=vi;',
  9728. ' doit(vg,vg,vg);',
  9729. ' doit(vh,vh,vj);',
  9730. ' doit(vi,vi,vi);',
  9731. ' doit(vj,vj,vj);',
  9732. 'end;',
  9733. 'var i: TArrInt;',
  9734. 'begin',
  9735. ' doit(i,i,i);']);
  9736. ConvertProgram;
  9737. CheckSource('TestArray_StaticAsParams',
  9738. LinesToStr([ // statements
  9739. 'this.DoIt = function (vG,vH,vI) {',
  9740. ' var vJ = rtl.arraySetLength(null, 0, 2);',
  9741. ' vG = vG.slice(0);',
  9742. ' vJ = vH.slice(0);',
  9743. ' vI.set(vI.get().slice(0));',
  9744. ' $mod.DoIt(vG.slice(0), vG, {',
  9745. ' get: function () {',
  9746. ' return vG;',
  9747. ' },',
  9748. ' set: function (v) {',
  9749. ' vG = v;',
  9750. ' }',
  9751. ' });',
  9752. ' $mod.DoIt(vH.slice(0), vH, {',
  9753. ' get: function () {',
  9754. ' return vJ;',
  9755. ' },',
  9756. ' set: function (v) {',
  9757. ' vJ = v;',
  9758. ' }',
  9759. ' });',
  9760. ' $mod.DoIt(vI.get().slice(0), vI.get(), vI);',
  9761. ' $mod.DoIt(vJ.slice(0), vJ, {',
  9762. ' get: function () {',
  9763. ' return vJ;',
  9764. ' },',
  9765. ' set: function (v) {',
  9766. ' vJ = v;',
  9767. ' }',
  9768. ' });',
  9769. '};',
  9770. 'this.i = rtl.arraySetLength(null, 0, 2);'
  9771. ]),
  9772. LinesToStr([
  9773. '$mod.DoIt($mod.i.slice(0),$mod.i,{',
  9774. ' p: $mod,',
  9775. ' get: function () {',
  9776. ' return this.p.i;',
  9777. ' },',
  9778. ' set: function (v) {',
  9779. ' this.p.i = v;',
  9780. ' }',
  9781. '});'
  9782. ]));
  9783. end;
  9784. procedure TTestModule.TestArrayElement_AsParams;
  9785. begin
  9786. StartProgram(false);
  9787. Add('type integer = longint;');
  9788. Add('type TArrayInt = array of integer;');
  9789. Add('procedure DoIt(vG: Integer; const vH: Integer; var vI: Integer);');
  9790. Add('var vJ: tarrayint;');
  9791. Add('begin');
  9792. Add(' vi:=vi;');
  9793. Add(' doit(vi,vi,vi);');
  9794. Add(' doit(vj[1+1],vj[1+2],vj[1+3]);');
  9795. Add('end;');
  9796. Add('var a: TArrayInt;');
  9797. Add('begin');
  9798. Add(' doit(a[1+4],a[1+5],a[1+6]);');
  9799. ConvertProgram;
  9800. CheckSource('TestArrayElement_AsParams',
  9801. LinesToStr([ // statements
  9802. 'this.DoIt = function (vG,vH,vI) {',
  9803. ' var vJ = [];',
  9804. ' vI.set(vI.get());',
  9805. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  9806. ' $mod.DoIt(vJ[1+1], vJ[1+2], {',
  9807. ' a:1+3,',
  9808. ' p:vJ,',
  9809. ' get: function () {',
  9810. ' return this.p[this.a];',
  9811. ' },',
  9812. ' set: function (v) {',
  9813. ' this.p[this.a] = v;',
  9814. ' }',
  9815. ' });',
  9816. '};',
  9817. 'this.a = [];'
  9818. ]),
  9819. LinesToStr([
  9820. '$mod.DoIt($mod.a[1+4],$mod.a[1+5],{',
  9821. ' a: 1+6,',
  9822. ' p: $mod.a,',
  9823. ' get: function () {',
  9824. ' return this.p[this.a];',
  9825. ' },',
  9826. ' set: function (v) {',
  9827. ' this.p[this.a] = v;',
  9828. ' }',
  9829. '});'
  9830. ]));
  9831. end;
  9832. procedure TTestModule.TestArrayElementFromFuncResult_AsParams;
  9833. begin
  9834. StartProgram(false);
  9835. Add('type Integer = longint;');
  9836. Add('type TArrayInt = array of integer;');
  9837. Add('function GetArr(vB: integer = 0): tarrayint;');
  9838. Add('begin');
  9839. Add('end;');
  9840. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  9841. Add('begin');
  9842. Add('end;');
  9843. Add('begin');
  9844. Add(' doit(getarr[1+1],getarr[1+2],getarr[1+3]);');
  9845. Add(' doit(getarr()[2+1],getarr()[2+2],getarr()[2+3]);');
  9846. Add(' doit(getarr(7)[3+1],getarr(8)[3+2],getarr(9)[3+3]);');
  9847. ConvertProgram;
  9848. CheckSource('TestArrayElementFromFuncResult_AsParams',
  9849. LinesToStr([ // statements
  9850. 'this.GetArr = function (vB) {',
  9851. ' var Result = [];',
  9852. ' return Result;',
  9853. '};',
  9854. 'this.DoIt = function (vG,vH,vI) {',
  9855. '};'
  9856. ]),
  9857. LinesToStr([
  9858. '$mod.DoIt($mod.GetArr(0)[1+1],$mod.GetArr(0)[1+2],{',
  9859. ' a: 1+3,',
  9860. ' p: $mod.GetArr(0),',
  9861. ' get: function () {',
  9862. ' return this.p[this.a];',
  9863. ' },',
  9864. ' set: function (v) {',
  9865. ' this.p[this.a] = v;',
  9866. ' }',
  9867. '});',
  9868. '$mod.DoIt($mod.GetArr(0)[2+1],$mod.GetArr(0)[2+2],{',
  9869. ' a: 2+3,',
  9870. ' p: $mod.GetArr(0),',
  9871. ' get: function () {',
  9872. ' return this.p[this.a];',
  9873. ' },',
  9874. ' set: function (v) {',
  9875. ' this.p[this.a] = v;',
  9876. ' }',
  9877. '});',
  9878. '$mod.DoIt($mod.GetArr(7)[3+1],$mod.GetArr(8)[3+2],{',
  9879. ' a: 3+3,',
  9880. ' p: $mod.GetArr(9),',
  9881. ' get: function () {',
  9882. ' return this.p[this.a];',
  9883. ' },',
  9884. ' set: function (v) {',
  9885. ' this.p[this.a] = v;',
  9886. ' }',
  9887. '});',
  9888. '']));
  9889. end;
  9890. procedure TTestModule.TestArrayEnumTypeRange;
  9891. begin
  9892. StartProgram(false);
  9893. Add([
  9894. 'type',
  9895. ' TEnum = (red,blue);',
  9896. ' TEnumArray = array[TEnum] of longint;',
  9897. 'var',
  9898. ' e: TEnum;',
  9899. ' i: longint;',
  9900. ' a: TEnumArray;',
  9901. ' numbers: TEnumArray = (1,2);',
  9902. ' names: array[TEnum] of string = (''red'',''blue'');',
  9903. 'begin',
  9904. ' e:=low(a);',
  9905. ' e:=high(a);',
  9906. ' i:=a[red];',
  9907. ' a[e]:=a[e];']);
  9908. ConvertProgram;
  9909. CheckSource('TestArrayEnumTypeRange',
  9910. LinesToStr([ // statements
  9911. ' this.TEnum = {',
  9912. ' "0": "red",',
  9913. ' red: 0,',
  9914. ' "1": "blue",',
  9915. ' blue: 1',
  9916. '};',
  9917. 'this.e = 0;',
  9918. 'this.i = 0;',
  9919. 'this.a = rtl.arraySetLength(null,0,2);',
  9920. 'this.numbers = [1, 2];',
  9921. 'this.names = ["red", "blue"];',
  9922. '']),
  9923. LinesToStr([ // $mod.$main
  9924. '$mod.e = $mod.TEnum.red;',
  9925. '$mod.e = $mod.TEnum.blue;',
  9926. '$mod.i = $mod.a[$mod.TEnum.red];',
  9927. '$mod.a[$mod.e] = $mod.a[$mod.e];',
  9928. '']));
  9929. end;
  9930. procedure TTestModule.TestArray_SetLengthOutArg;
  9931. begin
  9932. StartProgram(false);
  9933. Add([
  9934. 'type TArrInt = array of longint;',
  9935. 'procedure DoIt(out a: TArrInt);',
  9936. 'begin',
  9937. ' SetLength(a,2);',
  9938. 'end;',
  9939. 'begin',
  9940. '']);
  9941. ConvertProgram;
  9942. CheckSource('TestArray_SetLengthOutArg',
  9943. LinesToStr([ // statements
  9944. 'this.DoIt = function (a) {',
  9945. ' a.set(rtl.arraySetLength(a.get(), 0, 2));',
  9946. '};',
  9947. '']),
  9948. LinesToStr([
  9949. '']));
  9950. end;
  9951. procedure TTestModule.TestArray_SetLengthProperty;
  9952. begin
  9953. StartProgram(false);
  9954. Add('type');
  9955. Add(' TArrInt = array of longint;');
  9956. Add(' TObject = class');
  9957. Add(' function GetColors: TArrInt; external name ''GetColors'';');
  9958. Add(' procedure SetColors(const Value: TArrInt); external name ''SetColors'';');
  9959. Add(' property Colors: TArrInt read GetColors write SetColors;');
  9960. Add(' end;');
  9961. Add('var Obj: TObject;');
  9962. Add('begin');
  9963. Add(' SetLength(Obj.Colors,2);');
  9964. ConvertProgram;
  9965. CheckSource('TestArray_SetLengthProperty',
  9966. LinesToStr([ // statements
  9967. 'rtl.createClass(this, "TObject", null, function () {',
  9968. ' this.$init = function () {',
  9969. ' };',
  9970. ' this.$final = function () {',
  9971. ' };',
  9972. '});',
  9973. 'this.Obj = null;',
  9974. '']),
  9975. LinesToStr([
  9976. '$mod.Obj.SetColors(rtl.arraySetLength($mod.Obj.GetColors(), 0, 2));',
  9977. '']));
  9978. end;
  9979. procedure TTestModule.TestArray_SetLengthMultiDim;
  9980. begin
  9981. StartProgram(false);
  9982. Add([
  9983. 'type',
  9984. ' TArrArrInt = array of array of longint;',
  9985. ' TArrStaInt = array of array[1..2] of longint;',
  9986. 'var',
  9987. ' a: TArrArrInt;',
  9988. ' b: TArrStaInt;',
  9989. 'begin',
  9990. ' SetLength(a,2);',
  9991. ' SetLength(a,3,4);',
  9992. ' SetLength(b,5);',
  9993. '']);
  9994. ConvertProgram;
  9995. CheckSource('TestArray_SetLengthMultiDim',
  9996. LinesToStr([ // statements
  9997. 'this.a = [];',
  9998. 'this.b = [];',
  9999. '']),
  10000. LinesToStr([
  10001. '$mod.a = rtl.arraySetLength($mod.a, [], 2);',
  10002. '$mod.a = rtl.arraySetLength($mod.a, 0, 3, 4);',
  10003. '$mod.b = rtl.arraySetLength($mod.b, 0, 5, "s", 2);',
  10004. '']));
  10005. end;
  10006. procedure TTestModule.TestArray_SetLengthDynOfStatic;
  10007. begin
  10008. StartProgram(false);
  10009. Add([
  10010. 'type',
  10011. ' TStaArr1 = array[1..3] of boolean;',
  10012. //' TStaArr2 = array[5..6] of TStaArr1;',
  10013. ' TDynArr1StaArr1 = array of TStaArr1;',
  10014. //' TDynArr1StaArr2 = array of TStaArr2;',
  10015. ' TDynArr2StaArr1 = array of TDynArr1StaArr1;',
  10016. //' TDynArr2StaArr2 = array of TDynArr1StaArr2;',
  10017. 'var',
  10018. ' DynArr1StaArr1: TDynArr1StaArr1;',
  10019. //' DynArr1StaArr2: TDynArr1StaArr1;',
  10020. ' DynArr2StaArr1: TDynArr2StaArr1;',
  10021. //' DynArr2StaArr2: TDynArr2StaArr2;',
  10022. 'begin',
  10023. ' SetLength(DynArr1StaArr1,11);',
  10024. ' SetLength(DynArr2StaArr1,12);',
  10025. ' SetLength(DynArr2StaArr1[13],14);',
  10026. ' SetLength(DynArr2StaArr1,15,16);',
  10027. //' SetLength(DynArr1StaArr2,21);',
  10028. //' SetLength(DynArr2StaArr2,22);',
  10029. //' SetLength(DynArr2StaArr2[23],24);',
  10030. //' SetLength(DynArr2StaArr2,25,26);',
  10031. '']);
  10032. ConvertProgram;
  10033. CheckSource('TestArray_DynOfStatic',
  10034. LinesToStr([ // statements
  10035. 'this.DynArr1StaArr1 = [];',
  10036. 'this.DynArr2StaArr1 = [];',
  10037. '']),
  10038. LinesToStr([ // $mod.$main
  10039. '$mod.DynArr1StaArr1 = rtl.arraySetLength($mod.DynArr1StaArr1, false, 11, "s", 3);',
  10040. '$mod.DynArr2StaArr1 = rtl.arraySetLength($mod.DynArr2StaArr1, [], 12);',
  10041. '$mod.DynArr2StaArr1[13] = rtl.arraySetLength($mod.DynArr2StaArr1[13], false, 14, "s", 3);',
  10042. '$mod.DynArr2StaArr1 = rtl.arraySetLength(',
  10043. ' $mod.DynArr2StaArr1,',
  10044. ' false,',
  10045. ' 15,',
  10046. ' 16,',
  10047. ' "s",',
  10048. ' 3',
  10049. ');',
  10050. '']));
  10051. end;
  10052. procedure TTestModule.TestArray_OpenArrayOfString;
  10053. begin
  10054. StartProgram(false);
  10055. Add('procedure DoIt(const a: array of String);');
  10056. Add('var');
  10057. Add(' i: longint;');
  10058. Add(' s: string;');
  10059. Add('begin');
  10060. Add(' for i:=low(a) to high(a) do s:=a[length(a)-i-1];');
  10061. Add('end;');
  10062. Add('var s: string;');
  10063. Add('begin');
  10064. Add(' DoIt([]);');
  10065. Add(' DoIt([s,''foo'','''',s+s]);');
  10066. ConvertProgram;
  10067. CheckSource('TestArray_OpenArrayOfString',
  10068. LinesToStr([ // statements
  10069. 'this.DoIt = function (a) {',
  10070. ' var i = 0;',
  10071. ' var s = "";',
  10072. ' for (var $l = 0, $end = rtl.length(a) - 1; $l <= $end; $l++) {',
  10073. ' i = $l;',
  10074. ' s = a[rtl.length(a) - i - 1];',
  10075. ' };',
  10076. '};',
  10077. 'this.s = "";',
  10078. '']),
  10079. LinesToStr([
  10080. '$mod.DoIt([]);',
  10081. '$mod.DoIt([$mod.s, "foo", "", $mod.s + $mod.s]);',
  10082. '']));
  10083. end;
  10084. procedure TTestModule.TestArray_ArrayOfCharAssignString;
  10085. begin
  10086. StartProgram(false);
  10087. Add([
  10088. 'type TArr = array of char;',
  10089. 'var',
  10090. ' c: char;',
  10091. ' s: string;',
  10092. ' a: TArr;',
  10093. 'procedure Run(const a: array of char);',
  10094. 'begin',
  10095. ' Run(c);',
  10096. ' Run(s);',
  10097. 'end;',
  10098. 'begin',
  10099. ' a:=c;',
  10100. ' a:=s;',
  10101. ' a:=#13;',
  10102. ' a:=''Foo'';',
  10103. ' Run(c);',
  10104. ' Run(s);',
  10105. '']);
  10106. ConvertProgram;
  10107. CheckSource('TestArray_ArrayOfCharAssignString',
  10108. LinesToStr([ // statements
  10109. 'this.c = "";',
  10110. 'this.s = "";',
  10111. 'this.a = [];',
  10112. 'this.Run = function (a) {',
  10113. ' $mod.Run($mod.c.split(""));',
  10114. ' $mod.Run($mod.s.split(""));',
  10115. '};',
  10116. '']),
  10117. LinesToStr([
  10118. '$mod.a = $mod.c.split("");',
  10119. '$mod.a = $mod.s.split("");',
  10120. '$mod.a = "\r".split("");',
  10121. '$mod.a = "Foo".split("");',
  10122. '$mod.Run($mod.c.split(""));',
  10123. '$mod.Run($mod.s.split(""));',
  10124. '']));
  10125. end;
  10126. procedure TTestModule.TestArray_ConstRef;
  10127. begin
  10128. StartProgram(false);
  10129. Add([
  10130. 'type TArr = array of word;',
  10131. 'procedure Run(constref a: TArr);',
  10132. 'begin',
  10133. 'end;',
  10134. 'procedure Fly(a: TArr; var b: TArr; out c: TArr; const d: TArr; constref e: TArr);',
  10135. 'var l: TArr;',
  10136. 'begin',
  10137. ' Run(l);',
  10138. ' Run(a);',
  10139. ' Run(b);',
  10140. ' Run(c);',
  10141. ' Run(d);',
  10142. ' Run(e);',
  10143. 'end;',
  10144. 'begin',
  10145. '']);
  10146. ConvertProgram;
  10147. CheckResolverUnexpectedHints();
  10148. CheckSource('TestArray_ConstRef',
  10149. LinesToStr([ // statements
  10150. 'this.Run = function (a) {',
  10151. '};',
  10152. 'this.Fly = function (a, b, c, d, e) {',
  10153. ' var l = [];',
  10154. ' $mod.Run(l);',
  10155. ' $mod.Run(a);',
  10156. ' $mod.Run(b.get());',
  10157. ' $mod.Run(c.get());',
  10158. ' $mod.Run(d);',
  10159. ' $mod.Run(e);',
  10160. '};',
  10161. '']),
  10162. LinesToStr([
  10163. '']));
  10164. end;
  10165. procedure TTestModule.TestArray_Concat;
  10166. begin
  10167. StartProgram(false);
  10168. Add([
  10169. 'type',
  10170. ' integer = longint;',
  10171. ' TFlag = (big,small);',
  10172. ' TFlags = set of TFlag;',
  10173. ' TRec = record',
  10174. ' i: integer;',
  10175. ' end;',
  10176. ' TArrInt = array of integer;',
  10177. ' TArrRec = array of TRec;',
  10178. ' TArrFlag = array of TFlag;',
  10179. ' TArrSet = array of TFlags;',
  10180. ' TArrJSValue = array of jsvalue;',
  10181. 'var',
  10182. ' ArrInt: tarrint;',
  10183. ' ArrRec: tarrrec;',
  10184. ' ArrFlag: tarrflag;',
  10185. ' ArrSet: tarrset;',
  10186. ' ArrJSValue: tarrjsvalue;',
  10187. 'begin',
  10188. ' arrint:=concat(arrint);',
  10189. ' arrint:=concat(arrint,arrint);',
  10190. ' arrint:=concat(arrint,arrint,arrint);',
  10191. ' arrrec:=concat(arrrec);',
  10192. ' arrrec:=concat(arrrec,arrrec);',
  10193. ' arrrec:=concat(arrrec,arrrec,arrrec);',
  10194. ' arrset:=concat(arrset);',
  10195. ' arrset:=concat(arrset,arrset);',
  10196. ' arrset:=concat(arrset,arrset,arrset);',
  10197. ' arrjsvalue:=concat(arrjsvalue);',
  10198. ' arrjsvalue:=concat(arrjsvalue,arrjsvalue);',
  10199. ' arrjsvalue:=concat(arrjsvalue,arrjsvalue,arrjsvalue);',
  10200. ' arrint:=concat([1],arrint);',
  10201. ' arrflag:=concat([big]);',
  10202. ' arrflag:=concat([big],arrflag);',
  10203. ' arrflag:=concat(arrflag,[small]);',
  10204. '']);
  10205. ConvertProgram;
  10206. CheckSource('TestArray_Concat',
  10207. LinesToStr([ // statements
  10208. 'this.TFlag = {',
  10209. ' "0": "big",',
  10210. ' big: 0,',
  10211. ' "1": "small",',
  10212. ' small: 1',
  10213. '};',
  10214. 'rtl.recNewT(this, "TRec", function () {',
  10215. ' this.i = 0;',
  10216. ' this.$eq = function (b) {',
  10217. ' return this.i === b.i;',
  10218. ' };',
  10219. ' this.$assign = function (s) {',
  10220. ' this.i = s.i;',
  10221. ' return this;',
  10222. ' };',
  10223. '});',
  10224. 'this.ArrInt = [];',
  10225. 'this.ArrRec = [];',
  10226. 'this.ArrFlag = [];',
  10227. 'this.ArrSet = [];',
  10228. 'this.ArrJSValue = [];',
  10229. '']),
  10230. LinesToStr([ // $mod.$main
  10231. '$mod.ArrInt = rtl.arrayRef($mod.ArrInt);',
  10232. '$mod.ArrInt = rtl.arrayConcatN($mod.ArrInt, $mod.ArrInt);',
  10233. '$mod.ArrInt = rtl.arrayConcatN($mod.ArrInt, $mod.ArrInt, $mod.ArrInt);',
  10234. '$mod.ArrRec = rtl.arrayRef($mod.ArrRec);',
  10235. '$mod.ArrRec = rtl.arrayConcat($mod.TRec, $mod.ArrRec, $mod.ArrRec);',
  10236. '$mod.ArrRec = rtl.arrayConcat($mod.TRec, $mod.ArrRec, $mod.ArrRec, $mod.ArrRec);',
  10237. '$mod.ArrSet = rtl.arrayRef($mod.ArrSet);',
  10238. '$mod.ArrSet = rtl.arrayConcat("refSet", $mod.ArrSet, $mod.ArrSet);',
  10239. '$mod.ArrSet = rtl.arrayConcat("refSet", $mod.ArrSet, $mod.ArrSet, $mod.ArrSet);',
  10240. '$mod.ArrJSValue = rtl.arrayRef($mod.ArrJSValue);',
  10241. '$mod.ArrJSValue = rtl.arrayConcatN($mod.ArrJSValue, $mod.ArrJSValue);',
  10242. '$mod.ArrJSValue = rtl.arrayConcatN($mod.ArrJSValue, $mod.ArrJSValue, $mod.ArrJSValue);',
  10243. '$mod.ArrInt = rtl.arrayConcatN([1], $mod.ArrInt);',
  10244. '$mod.ArrFlag = [$mod.TFlag.big];',
  10245. '$mod.ArrFlag = rtl.arrayConcatN([$mod.TFlag.big], $mod.ArrFlag);',
  10246. '$mod.ArrFlag = rtl.arrayConcatN($mod.ArrFlag, [$mod.TFlag.small]);',
  10247. '']));
  10248. end;
  10249. procedure TTestModule.TestArray_Copy;
  10250. begin
  10251. StartProgram(false);
  10252. Add([
  10253. 'type',
  10254. ' integer = longint;',
  10255. ' TFlag = (big,small);',
  10256. ' TFlags = set of TFlag;',
  10257. ' TRec = record',
  10258. ' i: integer;',
  10259. ' end;',
  10260. ' TArrInt = array of integer;',
  10261. ' TArrRec = array of TRec;',
  10262. ' TArrSet = array of TFlags;',
  10263. ' TArrJSValue = array of jsvalue;',
  10264. 'var',
  10265. ' ArrInt: tarrint;',
  10266. ' ArrRec: tarrrec;',
  10267. ' ArrSet: tarrset;',
  10268. ' ArrJSValue: tarrjsvalue;',
  10269. 'begin',
  10270. ' arrint:=copy(arrint);',
  10271. ' arrint:=copy(arrint,2);',
  10272. ' arrint:=copy(arrint,3,4);',
  10273. ' arrint:=copy([1,1],1,2);',
  10274. ' arrrec:=copy(arrrec);',
  10275. ' arrrec:=copy(arrrec,5);',
  10276. ' arrrec:=copy(arrrec,6,7);',
  10277. ' arrset:=copy(arrset);',
  10278. ' arrset:=copy(arrset,8);',
  10279. ' arrset:=copy(arrset,9,10);',
  10280. ' arrjsvalue:=copy(arrjsvalue);',
  10281. ' arrjsvalue:=copy(arrjsvalue,11);',
  10282. ' arrjsvalue:=copy(arrjsvalue,12,13);',
  10283. ' ']);
  10284. ConvertProgram;
  10285. CheckSource('TestArray_Copy',
  10286. LinesToStr([ // statements
  10287. 'this.TFlag = {',
  10288. ' "0": "big",',
  10289. ' big: 0,',
  10290. ' "1": "small",',
  10291. ' small: 1',
  10292. '};',
  10293. 'rtl.recNewT(this, "TRec", function () {',
  10294. ' this.i = 0;',
  10295. ' this.$eq = function (b) {',
  10296. ' return this.i === b.i;',
  10297. ' };',
  10298. ' this.$assign = function (s) {',
  10299. ' this.i = s.i;',
  10300. ' return this;',
  10301. ' };',
  10302. '});',
  10303. 'this.ArrInt = [];',
  10304. 'this.ArrRec = [];',
  10305. 'this.ArrSet = [];',
  10306. 'this.ArrJSValue = [];',
  10307. '']),
  10308. LinesToStr([ // $mod.$main
  10309. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 0);',
  10310. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 2);',
  10311. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 3, 4);',
  10312. '$mod.ArrInt = rtl.arrayCopy(0, [1, 1], 1, 2);',
  10313. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 0);',
  10314. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 5);',
  10315. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 6, 7);',
  10316. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 0);',
  10317. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 8);',
  10318. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 9, 10);',
  10319. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 0);',
  10320. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 11);',
  10321. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 12, 13);',
  10322. '']));
  10323. end;
  10324. procedure TTestModule.TestArray_InsertDelete;
  10325. begin
  10326. StartProgram(false);
  10327. Add([
  10328. 'type',
  10329. ' integer = longint;',
  10330. ' TFlag = (big,small);',
  10331. ' TFlags = set of TFlag;',
  10332. ' TRec = record',
  10333. ' i: integer;',
  10334. ' end;',
  10335. ' TArrInt = array of integer;',
  10336. ' TArrRec = array of TRec;',
  10337. ' TArrSet = array of TFlags;',
  10338. ' TArrJSValue = array of jsvalue;',
  10339. ' TArrArrInt = array of TArrInt;',
  10340. 'var',
  10341. ' ArrInt: tarrint;',
  10342. ' ArrRec: tarrrec;',
  10343. ' ArrSet: tarrset;',
  10344. ' ArrJSValue: tarrjsvalue;',
  10345. ' ArrArrInt: TArrArrInt;',
  10346. 'begin',
  10347. ' Insert(1,arrint,2);',
  10348. ' Insert(arrint[3],arrint,4);',
  10349. ' Insert(arrrec[5],arrrec,6);',
  10350. ' Insert(arrset[7],arrset,7);',
  10351. ' Insert(arrjsvalue[8],arrjsvalue,9);',
  10352. ' Insert(10,arrjsvalue,11);',
  10353. ' Insert([23],arrarrint,22);',
  10354. ' Delete(arrint,12,13);',
  10355. ' Delete(arrrec,14,15);',
  10356. ' Delete(arrset,17,18);',
  10357. ' Delete(arrjsvalue,19,10);']);
  10358. ConvertProgram;
  10359. CheckSource('TestArray_InsertDelete',
  10360. LinesToStr([ // statements
  10361. 'this.TFlag = {',
  10362. ' "0": "big",',
  10363. ' big: 0,',
  10364. ' "1": "small",',
  10365. ' small: 1',
  10366. '};',
  10367. 'rtl.recNewT(this, "TRec", function () {',
  10368. ' this.i = 0;',
  10369. ' this.$eq = function (b) {',
  10370. ' return this.i === b.i;',
  10371. ' };',
  10372. ' this.$assign = function (s) {',
  10373. ' this.i = s.i;',
  10374. ' return this;',
  10375. ' };',
  10376. '});',
  10377. 'this.ArrInt = [];',
  10378. 'this.ArrRec = [];',
  10379. 'this.ArrSet = [];',
  10380. 'this.ArrJSValue = [];',
  10381. 'this.ArrArrInt = [];',
  10382. '']),
  10383. LinesToStr([ // $mod.$main
  10384. '$mod.ArrInt = rtl.arrayInsert(1, $mod.ArrInt, 2);',
  10385. '$mod.ArrInt = rtl.arrayInsert($mod.ArrInt[3], $mod.ArrInt, 4);',
  10386. '$mod.ArrRec = rtl.arrayInsert($mod.ArrRec[5], $mod.ArrRec, 6);',
  10387. '$mod.ArrSet = rtl.arrayInsert($mod.ArrSet[7], $mod.ArrSet, 7);',
  10388. '$mod.ArrJSValue = rtl.arrayInsert($mod.ArrJSValue[8], $mod.ArrJSValue, 9);',
  10389. '$mod.ArrJSValue = rtl.arrayInsert(10, $mod.ArrJSValue, 11);',
  10390. '$mod.ArrArrInt = rtl.arrayInsert([23], $mod.ArrArrInt, 22);',
  10391. '$mod.ArrInt.splice(12, 13);',
  10392. '$mod.ArrRec.splice(14, 15);',
  10393. '$mod.ArrSet.splice(17, 18);',
  10394. '$mod.ArrJSValue.splice(19, 10);',
  10395. '']));
  10396. end;
  10397. procedure TTestModule.TestArray_DynArrayConstObjFPC;
  10398. begin
  10399. Parser.Options:=Parser.Options+[po_cassignments];
  10400. StartProgram(false);
  10401. Add([
  10402. '{$modeswitch arrayoperators}',
  10403. 'type',
  10404. ' integer = longint;',
  10405. ' TArrInt = array of integer;',
  10406. ' TArrStr = array of string;',
  10407. 'const',
  10408. ' Ints: TArrInt = (1,2,3);',
  10409. ' Aliases: TarrStr = (''foo'',''b'');',
  10410. ' OneInt: TArrInt = (7);',
  10411. ' OneStr: array of integer = (7);',
  10412. ' Chars: array of char = ''aoc'';',
  10413. ' Names: array of string = (''a'',''foo'');',
  10414. ' NameCount = low(Names)+high(Names)+length(Names);',
  10415. 'var i: integer;',
  10416. 'begin',
  10417. ' Ints:=[];',
  10418. ' Ints:=[1,1];',
  10419. ' Ints:=[1]+[2];',
  10420. ' Ints:=[2];',
  10421. ' Ints:=[]+ints;',
  10422. ' Ints:=Ints+[];',
  10423. ' Ints:=Ints+OneInt;',
  10424. ' Ints:=Ints+[1,1];',
  10425. ' Ints:=[i,i]+Ints;',
  10426. ' Ints:=[1]+[i]+[3];',
  10427. '']);
  10428. ConvertProgram;
  10429. CheckSource('TestArray_DynArrayConstObjFPC',
  10430. LinesToStr([ // statements
  10431. 'this.Ints = [1, 2, 3];',
  10432. 'this.Aliases = ["foo", "b"];',
  10433. 'this.OneInt = [7];',
  10434. 'this.OneStr = [7];',
  10435. 'this.Chars = ["a", "o", "c"];',
  10436. 'this.Names = ["a", "foo"];',
  10437. 'this.NameCount = 0 + (rtl.length(this.Names) - 1) + rtl.length(this.Names);',
  10438. 'this.i = 0;',
  10439. '']),
  10440. LinesToStr([ // $mod.$main
  10441. '$mod.Ints = [];',
  10442. '$mod.Ints = [1, 1];',
  10443. '$mod.Ints = rtl.arrayConcatN([1], [2]);',
  10444. '$mod.Ints = [2];',
  10445. '$mod.Ints = rtl.arrayConcatN([], $mod.Ints);',
  10446. '$mod.Ints = rtl.arrayConcatN($mod.Ints, []);',
  10447. '$mod.Ints = rtl.arrayConcatN($mod.Ints, $mod.OneInt);',
  10448. '$mod.Ints = rtl.arrayConcatN($mod.Ints, [1, 1]);',
  10449. '$mod.Ints = rtl.arrayConcatN([$mod.i, $mod.i], $mod.Ints);',
  10450. '$mod.Ints = rtl.arrayConcatN(rtl.arrayConcatN([1], [$mod.i]), [3]);',
  10451. '']));
  10452. end;
  10453. procedure TTestModule.TestArray_DynArrayConstDelphi;
  10454. begin
  10455. StartProgram(false);
  10456. // Note: const c = [1,1]; defines a set!
  10457. Add([
  10458. '{$mode delphi}',
  10459. 'type',
  10460. ' integer = longint;',
  10461. ' TArrInt = array of integer;',
  10462. ' TArrStr = array of string;',
  10463. 'const',
  10464. ' Ints: TArrInt = [1,1,2];',
  10465. ' Aliases: TarrStr = [''foo'',''b''];',
  10466. ' OneInt: TArrInt = [7];',
  10467. ' OneStr: array of integer = [7]+[8];',
  10468. ' Chars: array of char = ''aoc'';',
  10469. ' Names: array of string = [''a'',''a''];',
  10470. ' NameCount = low(Names)+high(Names)+length(Names);',
  10471. 'begin',
  10472. '']);
  10473. ConvertProgram;
  10474. CheckSource('TestArray_DynArrayConstDelphi',
  10475. LinesToStr([ // statements
  10476. 'this.Ints = [1, 1, 2];',
  10477. 'this.Aliases = ["foo", "b"];',
  10478. 'this.OneInt = [7];',
  10479. 'this.OneStr = rtl.arrayConcatN([7],[8]);',
  10480. 'this.Chars = ["a", "o", "c"];',
  10481. 'this.Names = ["a", "a"];',
  10482. 'this.NameCount = 0 + (rtl.length(this.Names) - 1) + rtl.length(this.Names);',
  10483. '']),
  10484. LinesToStr([ // $mod.$main
  10485. '']));
  10486. end;
  10487. procedure TTestModule.TestArray_ArrayLitAsParam;
  10488. begin
  10489. StartProgram(false);
  10490. Add([
  10491. '{$modeswitch arrayoperators}',
  10492. 'type',
  10493. ' integer = longint;',
  10494. ' TArrInt = array of integer;',
  10495. ' TArrSet = array of (red,green,blue);',
  10496. 'procedure DoOpenInt(const a: array of integer); forward;',
  10497. 'procedure DoInt(const a: TArrInt);',
  10498. 'begin',
  10499. ' DoInt(a+[1]);',
  10500. ' DoInt([1]+a);',
  10501. ' DoOpenInt(a);',
  10502. ' DoOpenInt(a+[1]);',
  10503. ' DoOpenInt([1]+a);',
  10504. 'end;',
  10505. 'procedure DoOpenInt(const a: array of integer);',
  10506. 'begin',
  10507. ' DoOpenInt(a+[1]);',
  10508. ' DoOpenInt([1]+a);',
  10509. ' DoInt(a);',
  10510. ' DoInt(a+[1]);',
  10511. ' DoInt([1]+a);',
  10512. 'end;',
  10513. 'procedure DoSet(const a: TArrSet);',
  10514. 'begin',
  10515. ' DoSet(a+[red]);',
  10516. ' DoSet([blue]+a);',
  10517. 'end;',
  10518. 'var',
  10519. ' i: TArrInt;',
  10520. ' s: TArrSet;',
  10521. 'begin',
  10522. ' DoInt([1]);',
  10523. ' DoInt([1]+[2]);',
  10524. ' DoInt(i+[1]);',
  10525. ' DoInt([1]+i);',
  10526. ' DoOpenInt([1]);',
  10527. ' DoOpenInt([1]+[2]);',
  10528. ' DoOpenInt(i+[1]);',
  10529. ' DoOpenInt([1]+i);',
  10530. ' DoSet([red]);',
  10531. ' DoSet([blue]+[green]);',
  10532. ' DoSet(s+[blue]);',
  10533. ' DoSet([red]+s);',
  10534. '']);
  10535. ConvertProgram;
  10536. CheckSource('TestArray_ArrayLitAsParam',
  10537. LinesToStr([ // statements
  10538. 'this.TArrSet$a = {',
  10539. ' "0": "red",',
  10540. ' red: 0,',
  10541. ' "1": "green",',
  10542. ' green: 1,',
  10543. ' "2": "blue",',
  10544. ' blue: 2',
  10545. '};',
  10546. 'this.DoInt = function (a) {',
  10547. ' $mod.DoInt(rtl.arrayConcatN(a, [1]));',
  10548. ' $mod.DoInt(rtl.arrayConcatN([1], a));',
  10549. ' $mod.DoOpenInt(a);',
  10550. ' $mod.DoOpenInt(rtl.arrayConcatN(a, [1]));',
  10551. ' $mod.DoOpenInt(rtl.arrayConcatN([1], a));',
  10552. '};',
  10553. 'this.DoOpenInt = function (a) {',
  10554. ' $mod.DoOpenInt(rtl.arrayConcatN(a, [1]));',
  10555. ' $mod.DoOpenInt(rtl.arrayConcatN([1], a));',
  10556. ' $mod.DoInt(a);',
  10557. ' $mod.DoInt(rtl.arrayConcatN(a, [1]));',
  10558. ' $mod.DoInt(rtl.arrayConcatN([1], a));',
  10559. '};',
  10560. 'this.DoSet = function (a) {',
  10561. ' $mod.DoSet(rtl.arrayConcatN(a, [$mod.TArrSet$a.red]));',
  10562. ' $mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.blue], a));',
  10563. '};',
  10564. 'this.i = [];',
  10565. 'this.s = [];',
  10566. '']),
  10567. LinesToStr([ // $mod.$main
  10568. '$mod.DoInt([1]);',
  10569. '$mod.DoInt(rtl.arrayConcatN([1], [2]));',
  10570. '$mod.DoInt(rtl.arrayConcatN($mod.i, [1]));',
  10571. '$mod.DoInt(rtl.arrayConcatN([1], $mod.i));',
  10572. '$mod.DoOpenInt([1]);',
  10573. '$mod.DoOpenInt(rtl.arrayConcatN([1], [2]));',
  10574. '$mod.DoOpenInt(rtl.arrayConcatN($mod.i, [1]));',
  10575. '$mod.DoOpenInt(rtl.arrayConcatN([1], $mod.i));',
  10576. '$mod.DoSet([$mod.TArrSet$a.red]);',
  10577. '$mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.blue], [$mod.TArrSet$a.green]));',
  10578. '$mod.DoSet(rtl.arrayConcatN($mod.s, [$mod.TArrSet$a.blue]));',
  10579. '$mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.red], $mod.s));',
  10580. '']));
  10581. end;
  10582. procedure TTestModule.TestArray_ArrayLitMultiDimAsParam;
  10583. begin
  10584. StartProgram(false);
  10585. Add([
  10586. '{$modeswitch arrayoperators}',
  10587. 'type',
  10588. ' integer = longint;',
  10589. ' TArrInt = array of integer;',
  10590. ' TArrArrInt = array of TArrInt;',
  10591. 'procedure DoInt(const a: TArrArrInt);',
  10592. 'begin',
  10593. ' DoInt(a+[[1]]);',
  10594. ' DoInt([[1]]+a);',
  10595. ' DoInt(a);',
  10596. 'end;',
  10597. 'var',
  10598. ' i: TArrInt;',
  10599. ' a: TArrArrInt;',
  10600. 'begin',
  10601. ' a:=[[1]];',
  10602. ' a:=[i];',
  10603. ' a:=a+[i];',
  10604. ' a:=[i]+a;',
  10605. ' a:=[[1]+i];',
  10606. ' a:=[[1]+[2]];',
  10607. ' a:=[i+[2]];',
  10608. ' DoInt([[1]]);',
  10609. ' DoInt([[1]+[2],[3,4],[5]]);',
  10610. ' DoInt([i+[1]]+a);',
  10611. ' DoInt([i]+a);',
  10612. '']);
  10613. ConvertProgram;
  10614. CheckSource('TestArray_ArrayLitMultiDimAsParam',
  10615. LinesToStr([ // statements
  10616. 'this.DoInt = function (a) {',
  10617. ' $mod.DoInt(rtl.arrayConcatN(a, [[1]]));',
  10618. ' $mod.DoInt(rtl.arrayConcatN([[1]], a));',
  10619. ' $mod.DoInt(a);',
  10620. '};',
  10621. 'this.i = [];',
  10622. 'this.a = [];',
  10623. '']),
  10624. LinesToStr([ // $mod.$main
  10625. '$mod.a = [[1]];',
  10626. '$mod.a = [$mod.i];',
  10627. '$mod.a = rtl.arrayConcatN($mod.a, [$mod.i]);',
  10628. '$mod.a = rtl.arrayConcatN([$mod.i], $mod.a);',
  10629. '$mod.a = [rtl.arrayConcatN([1], $mod.i)];',
  10630. '$mod.a = [rtl.arrayConcatN([1], [2])];',
  10631. '$mod.a = [rtl.arrayConcatN($mod.i, [2])];',
  10632. '$mod.DoInt([[1]]);',
  10633. '$mod.DoInt([rtl.arrayConcatN([1], [2]), [3, 4], [5]]);',
  10634. '$mod.DoInt(rtl.arrayConcatN([rtl.arrayConcatN($mod.i, [1])], $mod.a));',
  10635. '$mod.DoInt(rtl.arrayConcatN([$mod.i], $mod.a));',
  10636. '']));
  10637. end;
  10638. procedure TTestModule.TestArray_ArrayLitStaticAsParam;
  10639. begin
  10640. StartProgram(false);
  10641. Add([
  10642. '{$modeswitch arrayoperators}',
  10643. 'type',
  10644. ' integer = longint;',
  10645. ' TArrInt = array[1..2] of integer;',
  10646. ' TArrArrInt = array of TArrInt;',
  10647. 'procedure DoInt(const a: TArrArrInt);',
  10648. 'begin',
  10649. ' DoInt(a+[[1,2]]);',
  10650. ' DoInt([[1,2]]+a);',
  10651. ' DoInt(a);',
  10652. 'end;',
  10653. 'var',
  10654. ' i: TArrInt;',
  10655. ' a: TArrArrInt;',
  10656. 'begin',
  10657. ' a:=[[1,1]];',
  10658. ' a:=[i];',
  10659. ' a:=a+[i];',
  10660. ' a:=[i]+a;',
  10661. ' DoInt([[1,1]]);',
  10662. ' DoInt([[1,2],[3,4]]);',
  10663. '']);
  10664. ConvertProgram;
  10665. CheckSource('TestArray_ArrayLitStaticAsParam',
  10666. LinesToStr([ // statements
  10667. 'this.DoInt = function (a) {',
  10668. ' $mod.DoInt(rtl.arrayConcatN(a, [[1, 2]]));',
  10669. ' $mod.DoInt(rtl.arrayConcatN([[1, 2]], a));',
  10670. ' $mod.DoInt(a);',
  10671. '};',
  10672. 'this.i = rtl.arraySetLength(null, 0, 2);',
  10673. 'this.a = [];',
  10674. '']),
  10675. LinesToStr([ // $mod.$main
  10676. '$mod.a = [[1, 1]];',
  10677. '$mod.a = [$mod.i.slice(0)];',
  10678. '$mod.a = rtl.arrayConcatN($mod.a, [$mod.i.slice(0)]);',
  10679. '$mod.a = rtl.arrayConcatN([$mod.i.slice(0)], $mod.a);',
  10680. '$mod.DoInt([[1, 1]]);',
  10681. '$mod.DoInt([[1, 2], [3, 4]]);',
  10682. '']));
  10683. end;
  10684. procedure TTestModule.TestArray_ForInArrOfString;
  10685. begin
  10686. StartProgram(false);
  10687. Add([
  10688. 'type',
  10689. 'type',
  10690. ' TMonthNameArray = array [1..12] of string;',
  10691. ' TMonthNames = TMonthNameArray;',
  10692. ' TObject = class',
  10693. ' private',
  10694. ' function GetLongMonthNames: TMonthNames; virtual; abstract;',
  10695. ' public',
  10696. ' Property LongMonthNames : TMonthNames Read GetLongMonthNames;',
  10697. ' end;',
  10698. 'var',
  10699. ' f: TObject;',
  10700. ' Month: string;',
  10701. ' Names: array of string = (''a'',''foo'',''bar'');',
  10702. ' i: longint;',
  10703. 'begin',
  10704. ' for Month in f.LongMonthNames do ;',
  10705. ' for Month in Names do ;',
  10706. ' for i:=low(Names) to high(Names) do ;',
  10707. '']);
  10708. ConvertProgram;
  10709. CheckSource('TestArray_ForInArrOfString',
  10710. LinesToStr([ // statements
  10711. 'rtl.createClass(this, "TObject", null, function () {',
  10712. ' this.$init = function () {',
  10713. ' };',
  10714. ' this.$final = function () {',
  10715. ' };',
  10716. '});',
  10717. 'this.f = null;',
  10718. 'this.Month = "";',
  10719. 'this.Names = ["a", "foo", "bar"];',
  10720. 'this.i = 0;',
  10721. '']),
  10722. LinesToStr([ // $mod.$main
  10723. 'for (var $in = $mod.f.GetLongMonthNames(), $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) $mod.Month = $in[$l];',
  10724. 'for (var $in1 = $mod.Names, $l1 = 0, $end1 = rtl.length($in1) - 1; $l1 <= $end1; $l1++) $mod.Month = $in1[$l1];',
  10725. 'for (var $l2 = 0, $end2 = rtl.length($mod.Names) - 1; $l2 <= $end2; $l2++) $mod.i = $l2;',
  10726. '']));
  10727. end;
  10728. procedure TTestModule.TestExternalClass_TypeCastArrayToExternalClass;
  10729. begin
  10730. StartProgram(false);
  10731. Add([
  10732. '{$modeswitch externalclass}',
  10733. 'type',
  10734. ' TJSObject = class external name ''Object''',
  10735. ' end;',
  10736. ' TJSArray = class external name ''Array''',
  10737. ' class function isArray(Value: JSValue) : boolean;',
  10738. ' function concat() : TJSArray; varargs;',
  10739. ' end;',
  10740. 'var',
  10741. ' aObj: TJSArray;',
  10742. ' a: array of longint;',
  10743. ' o: TJSObject;',
  10744. 'begin',
  10745. ' if TJSArray.isArray(65) then ;',
  10746. ' aObj:=TJSArray(a).concat(a);',
  10747. ' o:=TJSObject(a);']);
  10748. ConvertProgram;
  10749. CheckSource('TestExternalClass_TypeCastArrayToExternalClass',
  10750. LinesToStr([ // statements
  10751. 'this.aObj = null;',
  10752. 'this.a = [];',
  10753. 'this.o = null;',
  10754. '']),
  10755. LinesToStr([ // $mod.$main
  10756. 'if (Array.isArray(65)) ;',
  10757. '$mod.aObj = $mod.a.concat($mod.a);',
  10758. '$mod.o = $mod.a;',
  10759. '']));
  10760. end;
  10761. procedure TTestModule.TestExternalClass_TypeCastArrayFromExternalClass;
  10762. begin
  10763. StartProgram(false);
  10764. Add([
  10765. '{$modeswitch externalclass}',
  10766. 'type',
  10767. ' TArrStr = array of string;',
  10768. ' TJSArray = class external name ''Array''',
  10769. ' end;',
  10770. ' TJSObject = class external name ''Object''',
  10771. ' end;',
  10772. 'var',
  10773. ' aObj: TJSArray;',
  10774. ' a: TArrStr;',
  10775. ' jo: TJSObject;',
  10776. 'begin',
  10777. ' a:=TArrStr(aObj);',
  10778. ' TArrStr(aObj)[1]:=TArrStr(aObj)[2];',
  10779. ' a:=TarrStr(jo);',
  10780. '']);
  10781. ConvertProgram;
  10782. CheckSource('TestExternalClass_TypeCastArrayFromExternalClass',
  10783. LinesToStr([ // statements
  10784. 'this.aObj = null;',
  10785. 'this.a = [];',
  10786. 'this.jo = null;',
  10787. '']),
  10788. LinesToStr([ // $mod.$main
  10789. '$mod.a = $mod.aObj;',
  10790. '$mod.aObj[1] = $mod.aObj[2];',
  10791. '$mod.a = $mod.jo;',
  10792. '']));
  10793. end;
  10794. procedure TTestModule.TestArrayOfConst_TVarRec;
  10795. begin
  10796. StartProgram(true,[supTVarRec]);
  10797. Add([
  10798. 'procedure Say(args: array of const);',
  10799. 'var',
  10800. ' i: longint;',
  10801. ' v: TVarRec;',
  10802. 'begin',
  10803. ' for i:=low(args) to high(args) do begin',
  10804. ' v:=args[i];',
  10805. ' case v.vtype of',
  10806. ' vtInteger: if length(args)=args[i].vInteger then ;',
  10807. ' end;',
  10808. ' end;',
  10809. ' for v in args do ;',
  10810. ' args:=nil;',
  10811. ' SetLength(args,2);',
  10812. 'end;',
  10813. 'begin']);
  10814. ConvertProgram;
  10815. CheckSource('TestArrayOfConst_TVarRec',
  10816. LinesToStr([ // statements
  10817. 'this.Say = function (args) {',
  10818. ' var i = 0;',
  10819. ' var v = pas.system.TVarRec.$new();',
  10820. ' for (var $l = 0, $end = rtl.length(args) - 1; $l <= $end; $l++) {',
  10821. ' i = $l;',
  10822. ' v.$assign(args[i]);',
  10823. ' var $tmp = v.VType;',
  10824. ' if ($tmp === 0) if (rtl.length(args) === args[i].VJSValue) ;',
  10825. ' };',
  10826. ' for (var $in = args, $l1 = 0, $end1 = rtl.length($in) - 1; $l1 <= $end1; $l1++) v = $in[$l1];',
  10827. ' args = [];',
  10828. ' args = rtl.arraySetLength(args, pas.system.TVarRec, 2);',
  10829. '};',
  10830. '']),
  10831. LinesToStr([ // $mod.$main
  10832. ]));
  10833. end;
  10834. procedure TTestModule.TestArrayOfConst_PassBaseTypes;
  10835. begin
  10836. StartProgram(true,[supTVarRec]);
  10837. Add([
  10838. 'procedure Say(args: array of const);',
  10839. 'begin',
  10840. ' Say(args);',
  10841. 'end;',
  10842. 'var',
  10843. ' p: Pointer;',
  10844. ' j: jsvalue;',
  10845. ' c: currency;',
  10846. 'begin',
  10847. ' Say([]);',
  10848. ' Say([1]);',
  10849. ' Say([''c'',''foo'',nil,true,1.3,p,j,c]);',
  10850. '']);
  10851. ConvertProgram;
  10852. CheckSource('TestArrayOfConst_PassBaseTypes',
  10853. LinesToStr([ // statements
  10854. 'this.Say = function (args) {',
  10855. ' $mod.Say(args);',
  10856. '};',
  10857. 'this.p = null;',
  10858. 'this.j = undefined;',
  10859. 'this.c = 0;',
  10860. '']),
  10861. LinesToStr([ // $mod.$main
  10862. '$mod.Say([]);',
  10863. '$mod.Say(pas.system.VarRecs(0, 1));',
  10864. '$mod.Say(pas.system.VarRecs(',
  10865. ' 9,',
  10866. ' "c",',
  10867. ' 18,',
  10868. ' "foo",',
  10869. ' 5,',
  10870. ' null,',
  10871. ' 1,',
  10872. ' true,',
  10873. ' 3,',
  10874. ' 1.3,',
  10875. ' 5,',
  10876. ' $mod.p,',
  10877. ' 20,',
  10878. ' $mod.j,',
  10879. ' 12,',
  10880. ' $mod.c',
  10881. ' ));',
  10882. '']));
  10883. end;
  10884. procedure TTestModule.TestArrayOfConst_PassObj;
  10885. begin
  10886. StartProgram(true,[supTVarRec]);
  10887. Add([
  10888. '{$interfaces corba}',
  10889. 'type',
  10890. ' TObject = class',
  10891. ' end;',
  10892. ' TClass = class of TObject;',
  10893. ' IUnknown = interface',
  10894. ' end;',
  10895. 'procedure Say(args: array of const);',
  10896. 'begin',
  10897. 'end;',
  10898. 'var',
  10899. ' o: TObject;',
  10900. ' c: TClass;',
  10901. ' i: IUnknown;',
  10902. 'begin',
  10903. ' Say([o,c,TObject]);',
  10904. ' Say([nil,i]);',
  10905. '']);
  10906. ConvertProgram;
  10907. CheckSource('TestArrayOfConst_PassObj',
  10908. LinesToStr([ // statements
  10909. 'rtl.createClass(this, "TObject", null, function () {',
  10910. ' this.$init = function () {',
  10911. ' };',
  10912. ' this.$final = function () {',
  10913. ' };',
  10914. '});',
  10915. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  10916. 'this.Say = function (args) {',
  10917. '};',
  10918. 'this.o = null;',
  10919. 'this.c = null;',
  10920. 'this.i = null;',
  10921. '']),
  10922. LinesToStr([ // $mod.$main
  10923. '$mod.Say(pas.system.VarRecs(',
  10924. ' 7,',
  10925. ' $mod.o,',
  10926. ' 8,',
  10927. ' $mod.c,',
  10928. ' 8,',
  10929. ' $mod.TObject',
  10930. '));',
  10931. '$mod.Say(pas.system.VarRecs(5, null, 14, $mod.i));',
  10932. '']));
  10933. end;
  10934. procedure TTestModule.TestRecord_Empty;
  10935. begin
  10936. StartProgram(false);
  10937. Add([
  10938. 'type',
  10939. ' TRecA = record',
  10940. ' end;',
  10941. 'var a,b: TRecA;',
  10942. 'begin',
  10943. ' if a=b then ;']);
  10944. ConvertProgram;
  10945. CheckSource('TestRecord_Empty',
  10946. LinesToStr([ // statements
  10947. 'rtl.recNewT(this, "TRecA", function () {',
  10948. ' this.$eq = function (b) {',
  10949. ' return true;',
  10950. ' };',
  10951. ' this.$assign = function (s) {',
  10952. ' return this;',
  10953. ' };',
  10954. '});',
  10955. 'this.a = this.TRecA.$new();',
  10956. 'this.b = this.TRecA.$new();',
  10957. '']),
  10958. LinesToStr([ // $mod.$main
  10959. 'if ($mod.a.$eq($mod.b)) ;'
  10960. ]));
  10961. end;
  10962. procedure TTestModule.TestRecord_Var;
  10963. begin
  10964. StartProgram(false);
  10965. Add('type');
  10966. Add(' TRecA = record');
  10967. Add(' Bold: longint;');
  10968. Add(' end;');
  10969. Add('var Rec: TRecA;');
  10970. Add('begin');
  10971. Add(' rec.bold:=123');
  10972. ConvertProgram;
  10973. CheckSource('TestRecord_Var',
  10974. LinesToStr([ // statements
  10975. 'rtl.recNewT(this, "TRecA", function () {',
  10976. ' this.Bold = 0;',
  10977. ' this.$eq = function (b) {',
  10978. ' return this.Bold === b.Bold;',
  10979. ' };',
  10980. ' this.$assign = function (s) {',
  10981. ' this.Bold = s.Bold;',
  10982. ' return this;',
  10983. ' };',
  10984. '});',
  10985. 'this.Rec = this.TRecA.$new();',
  10986. '']),
  10987. LinesToStr([ // $mod.$main
  10988. '$mod.Rec.Bold = 123;'
  10989. ]));
  10990. end;
  10991. procedure TTestModule.TestRecord_VarExternal;
  10992. begin
  10993. StartProgram(false);
  10994. Add([
  10995. '{$modeswitch externalclass}',
  10996. 'type',
  10997. ' TRecA = record',
  10998. ' i: byte;',
  10999. ' length_: longint external name ''length'';',
  11000. ' end;',
  11001. 'var Rec: TRecA;',
  11002. 'begin',
  11003. ' rec.length_ := rec.length_',
  11004. '']);
  11005. ConvertProgram;
  11006. CheckSource('TestRecord_VarExternal',
  11007. LinesToStr([ // statements
  11008. 'rtl.recNewT(this, "TRecA", function () {',
  11009. ' this.i = 0;',
  11010. ' this.$eq = function (b) {',
  11011. ' return (this.i === b.i) && (this.length === b.length);',
  11012. ' };',
  11013. ' this.$assign = function (s) {',
  11014. ' this.i = s.i;',
  11015. ' this.length = s.length;',
  11016. ' return this;',
  11017. ' };',
  11018. '});',
  11019. 'this.Rec = this.TRecA.$new();',
  11020. '']),
  11021. LinesToStr([ // $mod.$main
  11022. '$mod.Rec.length = $mod.Rec.length;'
  11023. ]));
  11024. end;
  11025. procedure TTestModule.TestRecord_WithDo;
  11026. begin
  11027. StartProgram(false);
  11028. Add('type');
  11029. Add(' TRec = record');
  11030. Add(' vI: longint;');
  11031. Add(' end;');
  11032. Add('var');
  11033. Add(' Int: longint;');
  11034. Add(' r: TRec;');
  11035. Add('begin');
  11036. Add(' with r do');
  11037. Add(' int:=vi;');
  11038. Add(' with r do begin');
  11039. Add(' int:=vi;');
  11040. Add(' vi:=int;');
  11041. Add(' end;');
  11042. ConvertProgram;
  11043. CheckSource('TestWithRecordDo',
  11044. LinesToStr([ // statements
  11045. 'rtl.recNewT(this, "TRec", function () {',
  11046. ' this.vI = 0;',
  11047. ' this.$eq = function (b) {',
  11048. ' return this.vI === b.vI;',
  11049. ' };',
  11050. ' this.$assign = function (s) {',
  11051. ' this.vI = s.vI;',
  11052. ' return this;',
  11053. ' };',
  11054. '});',
  11055. 'this.Int = 0;',
  11056. 'this.r = this.TRec.$new();',
  11057. '']),
  11058. LinesToStr([ // $mod.$main
  11059. 'var $with = $mod.r;',
  11060. '$mod.Int = $with.vI;',
  11061. 'var $with1 = $mod.r;',
  11062. '$mod.Int = $with1.vI;',
  11063. '$with1.vI = $mod.Int;'
  11064. ]));
  11065. end;
  11066. procedure TTestModule.TestRecord_Assign;
  11067. begin
  11068. StartProgram(false);
  11069. Add([
  11070. 'type',
  11071. ' TEnum = (red,green);',
  11072. ' TEnums = set of TEnum;',
  11073. ' TSmallRec = record',
  11074. ' N: longint;',
  11075. ' end;',
  11076. ' TBigRec = record',
  11077. ' Int: longint;',
  11078. ' D: double;',
  11079. ' Arr: array of longint;',
  11080. ' Arr2: array[1..2] of longint;',
  11081. ' Small: TSmallRec;',
  11082. ' Enums: TEnums;',
  11083. ' end;',
  11084. 'var',
  11085. ' r, s: TBigRec;',
  11086. 'begin',
  11087. ' r:=s;',
  11088. ' r:=default(TBigRec);',
  11089. ' r:=default(s);',
  11090. '']);
  11091. ConvertProgram;
  11092. CheckSource('TestRecord_Assign',
  11093. LinesToStr([ // statements
  11094. 'this.TEnum = {',
  11095. ' "0": "red",',
  11096. ' red: 0,',
  11097. ' "1": "green",',
  11098. ' green: 1',
  11099. '};',
  11100. 'rtl.recNewT(this, "TSmallRec", function () {',
  11101. ' this.N = 0;',
  11102. ' this.$eq = function (b) {',
  11103. ' return this.N === b.N;',
  11104. ' };',
  11105. ' this.$assign = function (s) {',
  11106. ' this.N = s.N;',
  11107. ' return this;',
  11108. ' };',
  11109. '});',
  11110. 'rtl.recNewT(this, "TBigRec", function () {',
  11111. ' this.Int = 0;',
  11112. ' this.D = 0.0;',
  11113. ' this.$new = function () {',
  11114. ' var r = Object.create(this);',
  11115. ' r.Arr = [];',
  11116. ' r.Arr2 = rtl.arraySetLength(null, 0, 2);',
  11117. ' r.Small = $mod.TSmallRec.$new();',
  11118. ' r.Enums = {};',
  11119. ' return r;',
  11120. ' };',
  11121. ' this.$eq = function (b) {',
  11122. ' 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);',
  11123. ' };',
  11124. ' this.$assign = function (s) {',
  11125. ' this.Int = s.Int;',
  11126. ' this.D = s.D;',
  11127. ' this.Arr = rtl.arrayRef(s.Arr);',
  11128. ' this.Arr2 = s.Arr2.slice(0);',
  11129. ' this.Small.$assign(s.Small);',
  11130. ' this.Enums = rtl.refSet(s.Enums);',
  11131. ' return this;',
  11132. ' };',
  11133. '});',
  11134. 'this.r = this.TBigRec.$new();',
  11135. 'this.s = this.TBigRec.$new();',
  11136. '']),
  11137. LinesToStr([ // $mod.$main
  11138. '$mod.r.$assign($mod.s);',
  11139. '$mod.r.$assign($mod.TBigRec.$new());',
  11140. '$mod.r.$assign($mod.TBigRec.$new());',
  11141. '']));
  11142. end;
  11143. procedure TTestModule.TestRecord_AsParams;
  11144. begin
  11145. StartProgram(false);
  11146. Add([
  11147. 'type',
  11148. ' integer = longint;',
  11149. ' TRecord = record',
  11150. ' i: integer;',
  11151. ' end;',
  11152. 'procedure DoIt(vD: TRecord; const vC: TRecord; var vV: TRecord; var U);',
  11153. 'var vL: TRecord;',
  11154. 'begin',
  11155. ' vd:=vd;',
  11156. ' vd.i:=vd.i;',
  11157. ' vl:=vc;',
  11158. ' vv:=vv;',
  11159. ' vv.i:=vv.i;',
  11160. ' U:=vl;',
  11161. ' U:=vd;',
  11162. ' U:=vc;',
  11163. ' U:=vv;',
  11164. ' vl:=TRecord(U);',
  11165. ' vd:=TRecord(U);',
  11166. ' vv:=TRecord(U);',
  11167. ' doit(vd,vd,vd,vd);',
  11168. ' doit(vc,vc,vl,vl);',
  11169. ' doit(vv,vv,vv,vv);',
  11170. ' doit(vl,vl,vl,vl);',
  11171. ' TRecord(U).i:=3;',
  11172. 'end;',
  11173. 'var i: TRecord;',
  11174. 'begin',
  11175. ' doit(i,i,i,i);',
  11176. '']);
  11177. ConvertProgram;
  11178. CheckSource('TestRecord_AsParams',
  11179. LinesToStr([ // statements
  11180. 'rtl.recNewT(this, "TRecord", function () {',
  11181. ' this.i = 0;',
  11182. ' this.$eq = function (b) {',
  11183. ' return this.i === b.i;',
  11184. ' };',
  11185. ' this.$assign = function (s) {',
  11186. ' this.i = s.i;',
  11187. ' return this;',
  11188. ' };',
  11189. '});',
  11190. 'this.DoIt = function (vD, vC, vV, U) {',
  11191. ' var vL = $mod.TRecord.$new();',
  11192. ' vD.$assign(vD);',
  11193. ' vD.i = vD.i;',
  11194. ' vL.$assign(vC);',
  11195. ' vV.$assign(vV);',
  11196. ' vV.i = vV.i;',
  11197. ' U.$assign(vL);',
  11198. ' U.$assign(vD);',
  11199. ' U.$assign(vC);',
  11200. ' U.$assign(vV);',
  11201. ' vL.$assign(U);',
  11202. ' vD.$assign(U);',
  11203. ' vV.$assign(U);',
  11204. ' $mod.DoIt($mod.TRecord.$clone(vD), vD, vD, vD);',
  11205. ' $mod.DoIt($mod.TRecord.$clone(vC), vC, vL, vL);',
  11206. ' $mod.DoIt($mod.TRecord.$clone(vV), vV, vV, vV);',
  11207. ' $mod.DoIt($mod.TRecord.$clone(vL), vL, vL, vL);',
  11208. ' U.i = 3;',
  11209. '};',
  11210. 'this.i = this.TRecord.$new();'
  11211. ]),
  11212. LinesToStr([
  11213. '$mod.DoIt($mod.TRecord.$clone($mod.i), $mod.i, $mod.i, $mod.i);',
  11214. '']));
  11215. end;
  11216. procedure TTestModule.TestRecord_ConstRef;
  11217. begin
  11218. StartProgram(false);
  11219. Add([
  11220. 'type TRec = record i: word; end;',
  11221. 'procedure Run(constref a: TRec);',
  11222. 'begin',
  11223. 'end;',
  11224. 'procedure Fly(a: TRec; var b: TRec; out c: TRec; const d: TRec; constref e: TRec);',
  11225. 'var l: TRec;',
  11226. 'begin',
  11227. ' Run(l);',
  11228. ' Run(a);',
  11229. ' Run(b);',
  11230. ' Run(c);',
  11231. ' Run(d);',
  11232. ' Run(e);',
  11233. 'end;',
  11234. 'begin',
  11235. '']);
  11236. ConvertProgram;
  11237. CheckResolverUnexpectedHints();
  11238. CheckSource('TestRecord_ConstRef',
  11239. LinesToStr([ // statements
  11240. 'rtl.recNewT(this, "TRec", function () {',
  11241. ' this.i = 0;',
  11242. ' this.$eq = function (b) {',
  11243. ' return this.i === b.i;',
  11244. ' };',
  11245. ' this.$assign = function (s) {',
  11246. ' this.i = s.i;',
  11247. ' return this;',
  11248. ' };',
  11249. '});',
  11250. 'this.Run = function (a) {',
  11251. '};',
  11252. 'this.Fly = function (a, b, c, d, e) {',
  11253. ' var l = $mod.TRec.$new();',
  11254. ' $mod.Run(l);',
  11255. ' $mod.Run(a);',
  11256. ' $mod.Run(b);',
  11257. ' $mod.Run(c);',
  11258. ' $mod.Run(d);',
  11259. ' $mod.Run(e);',
  11260. '};',
  11261. '']),
  11262. LinesToStr([
  11263. '']));
  11264. end;
  11265. procedure TTestModule.TestRecordElement_AsParams;
  11266. begin
  11267. StartProgram(false);
  11268. Add('type');
  11269. Add(' integer = longint;');
  11270. Add(' TRecord = record');
  11271. Add(' i: integer;');
  11272. Add(' end;');
  11273. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  11274. Add('var vJ: TRecord;');
  11275. Add('begin');
  11276. Add(' doit(vj.i,vj.i,vj.i);');
  11277. Add('end;');
  11278. Add('var r: TRecord;');
  11279. Add('begin');
  11280. Add(' doit(r.i,r.i,r.i);');
  11281. ConvertProgram;
  11282. CheckSource('TestRecordElement_AsParams',
  11283. LinesToStr([ // statements
  11284. 'rtl.recNewT(this, "TRecord", function () {',
  11285. ' this.i = 0;',
  11286. ' this.$eq = function (b) {',
  11287. ' return this.i === b.i;',
  11288. ' };',
  11289. ' this.$assign = function (s) {',
  11290. ' this.i = s.i;',
  11291. ' return this;',
  11292. ' };',
  11293. '});',
  11294. 'this.DoIt = function (vG,vH,vI) {',
  11295. ' var vJ = $mod.TRecord.$new();',
  11296. ' $mod.DoIt(vJ.i, vJ.i, {',
  11297. ' p: vJ,',
  11298. ' get: function () {',
  11299. ' return this.p.i;',
  11300. ' },',
  11301. ' set: function (v) {',
  11302. ' this.p.i = v;',
  11303. ' }',
  11304. ' });',
  11305. '};',
  11306. 'this.r = this.TRecord.$new();'
  11307. ]),
  11308. LinesToStr([
  11309. '$mod.DoIt($mod.r.i,$mod.r.i,{',
  11310. ' p: $mod.r,',
  11311. ' get: function () {',
  11312. ' return this.p.i;',
  11313. ' },',
  11314. ' set: function (v) {',
  11315. ' this.p.i = v;',
  11316. ' }',
  11317. '});'
  11318. ]));
  11319. end;
  11320. procedure TTestModule.TestRecordElementFromFuncResult_AsParams;
  11321. begin
  11322. StartProgram(false);
  11323. Add('type');
  11324. Add(' integer = longint;');
  11325. Add(' TRecord = record');
  11326. Add(' i: integer;');
  11327. Add(' end;');
  11328. Add('function GetRec(vB: integer = 0): TRecord;');
  11329. Add('begin');
  11330. Add('end;');
  11331. Add('procedure DoIt(vG: integer; const vH: integer);');
  11332. Add('begin');
  11333. Add('end;');
  11334. Add('begin');
  11335. Add(' doit(getrec.i,getrec.i);');
  11336. Add(' doit(getrec().i,getrec().i);');
  11337. Add(' doit(getrec(1).i,getrec(2).i);');
  11338. ConvertProgram;
  11339. CheckSource('TestRecordElementFromFuncResult_AsParams',
  11340. LinesToStr([ // statements
  11341. 'rtl.recNewT(this, "TRecord", function () {',
  11342. ' this.i = 0;',
  11343. ' this.$eq = function (b) {',
  11344. ' return this.i === b.i;',
  11345. ' };',
  11346. ' this.$assign = function (s) {',
  11347. ' this.i = s.i;',
  11348. ' return this;',
  11349. ' };',
  11350. '});',
  11351. 'this.GetRec = function (vB) {',
  11352. ' var Result = $mod.TRecord.$new();',
  11353. ' return Result;',
  11354. '};',
  11355. 'this.DoIt = function (vG, vH) {',
  11356. '};',
  11357. '']),
  11358. LinesToStr([
  11359. '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
  11360. '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
  11361. '$mod.DoIt($mod.GetRec(1).i,$mod.GetRec(2).i);',
  11362. '']));
  11363. end;
  11364. procedure TTestModule.TestRecordElementFromWith_AsParams;
  11365. begin
  11366. StartProgram(false);
  11367. Add('type');
  11368. Add(' integer = longint;');
  11369. Add(' TRecord = record');
  11370. Add(' i: integer;');
  11371. Add(' end;');
  11372. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  11373. Add('begin');
  11374. Add('end;');
  11375. Add('var r: trecord;');
  11376. Add('begin');
  11377. Add(' with r do ');
  11378. Add(' doit(i,i,i);');
  11379. ConvertProgram;
  11380. CheckSource('TestRecordElementFromWith_AsParams',
  11381. LinesToStr([ // statements
  11382. 'rtl.recNewT(this, "TRecord", function () {',
  11383. ' this.i = 0;',
  11384. ' this.$eq = function (b) {',
  11385. ' return this.i === b.i;',
  11386. ' };',
  11387. ' this.$assign = function (s) {',
  11388. ' this.i = s.i;',
  11389. ' return this;',
  11390. ' };',
  11391. '});',
  11392. 'this.DoIt = function (vG,vH,vI) {',
  11393. '};',
  11394. 'this.r = this.TRecord.$new();'
  11395. ]),
  11396. LinesToStr([
  11397. 'var $with = $mod.r;',
  11398. '$mod.DoIt($with.i,$with.i,{',
  11399. ' p: $with,',
  11400. ' get: function () {',
  11401. ' return this.p.i;',
  11402. ' },',
  11403. ' set: function (v) {',
  11404. ' this.p.i = v;',
  11405. ' }',
  11406. '});',
  11407. '']));
  11408. end;
  11409. procedure TTestModule.TestRecord_Equal;
  11410. begin
  11411. StartProgram(false);
  11412. Add('type');
  11413. Add(' integer = longint;');
  11414. Add(' TFlag = (red,blue);');
  11415. Add(' TFlags = set of TFlag;');
  11416. Add(' TProc = procedure;');
  11417. Add(' TRecord = record');
  11418. Add(' i: integer;');
  11419. Add(' Event: TProc;');
  11420. Add(' f: TFlags;');
  11421. Add(' end;');
  11422. Add(' TNested = record');
  11423. Add(' r: TRecord;');
  11424. Add(' end;');
  11425. Add('var');
  11426. Add(' b: boolean;');
  11427. Add(' r,s: trecord;');
  11428. Add('begin');
  11429. Add(' b:=r=s;');
  11430. Add(' b:=r<>s;');
  11431. ConvertProgram;
  11432. CheckSource('TestRecord_Equal',
  11433. LinesToStr([ // statements
  11434. 'this.TFlag = {',
  11435. ' "0": "red",',
  11436. ' red: 0,',
  11437. ' "1": "blue",',
  11438. ' blue: 1',
  11439. '};',
  11440. 'rtl.recNewT(this, "TRecord", function () {',
  11441. ' this.i = 0;',
  11442. ' this.Event = null;',
  11443. ' this.$new = function () {',
  11444. ' var r = Object.create(this);',
  11445. ' r.f = {};',
  11446. ' return r;',
  11447. ' };',
  11448. ' this.$eq = function (b) {',
  11449. ' return (this.i === b.i) && rtl.eqCallback(this.Event, b.Event) && rtl.eqSet(this.f, b.f);',
  11450. ' };',
  11451. ' this.$assign = function (s) {',
  11452. ' this.i = s.i;',
  11453. ' this.Event = s.Event;',
  11454. ' this.f = rtl.refSet(s.f);',
  11455. ' return this;',
  11456. ' };',
  11457. '});',
  11458. 'rtl.recNewT(this, "TNested", function () {',
  11459. ' this.$new = function () {',
  11460. ' var r = Object.create(this);',
  11461. ' r.r = $mod.TRecord.$new();',
  11462. ' return r;',
  11463. ' };',
  11464. ' this.$eq = function (b) {',
  11465. ' return this.r.$eq(b.r);',
  11466. ' };',
  11467. ' this.$assign = function (s) {',
  11468. ' this.r.$assign(s.r);',
  11469. ' return this;',
  11470. ' };',
  11471. '});',
  11472. 'this.b = false;',
  11473. 'this.r = this.TRecord.$new();',
  11474. 'this.s = this.TRecord.$new();',
  11475. '']),
  11476. LinesToStr([
  11477. '$mod.b = $mod.r.$eq($mod.s);',
  11478. '$mod.b = !$mod.r.$eq($mod.s);',
  11479. '']));
  11480. end;
  11481. procedure TTestModule.TestRecord_JSValue;
  11482. begin
  11483. StartProgram(false);
  11484. Add([
  11485. 'type',
  11486. ' TRecord = record',
  11487. ' i: longint;',
  11488. ' end;',
  11489. 'procedure Fly(d: jsvalue; const c: jsvalue);',
  11490. 'begin',
  11491. 'end;',
  11492. 'procedure Run(d: TRecord; const c: TRecord; var v: TRecord);',
  11493. 'begin',
  11494. ' if jsvalue(d) then ;',
  11495. ' if jsvalue(c) then ;',
  11496. ' if jsvalue(v) then ;',
  11497. 'end;',
  11498. 'var',
  11499. ' Jv: jsvalue;',
  11500. ' Rec: trecord;',
  11501. 'begin',
  11502. ' rec:=trecord(jv);',
  11503. ' jv:=rec;',
  11504. ' Fly(rec,rec);',
  11505. ' Fly(@rec,@rec);',
  11506. ' if jsvalue(Rec) then ;',
  11507. ' Run(trecord(jv),trecord(jv),rec);',
  11508. '']);
  11509. ConvertProgram;
  11510. CheckSource('TestRecord_JSValue',
  11511. LinesToStr([ // statements
  11512. 'rtl.recNewT(this, "TRecord", function () {',
  11513. ' this.i = 0;',
  11514. ' this.$eq = function (b) {',
  11515. ' return this.i === b.i;',
  11516. ' };',
  11517. ' this.$assign = function (s) {',
  11518. ' this.i = s.i;',
  11519. ' return this;',
  11520. ' };',
  11521. '});',
  11522. 'this.Fly = function (d, c) {',
  11523. '};',
  11524. 'this.Run = function (d, c, v) {',
  11525. ' if (d) ;',
  11526. ' if (c) ;',
  11527. ' if (v) ;',
  11528. '};',
  11529. 'this.Jv = undefined;',
  11530. 'this.Rec = this.TRecord.$new();',
  11531. '']),
  11532. LinesToStr([
  11533. '$mod.Rec.$assign(rtl.getObject($mod.Jv));',
  11534. '$mod.Jv = $mod.Rec;',
  11535. '$mod.Fly($mod.TRecord.$clone($mod.Rec), $mod.Rec);',
  11536. '$mod.Fly($mod.Rec, $mod.Rec);',
  11537. 'if ($mod.Rec) ;',
  11538. '$mod.Run($mod.TRecord.$clone(rtl.getObject($mod.Jv)), rtl.getObject($mod.Jv), $mod.Rec);',
  11539. '']));
  11540. end;
  11541. procedure TTestModule.TestRecord_VariantFail;
  11542. begin
  11543. StartProgram(false);
  11544. Add([
  11545. 'type',
  11546. ' TRec = record',
  11547. ' case word of',
  11548. ' 0: (b0, b1: Byte);',
  11549. ' 1: (i: word);',
  11550. ' end;',
  11551. 'begin']);
  11552. SetExpectedPasResolverError('variant record is not supported',
  11553. nXIsNotSupported);
  11554. ConvertProgram;
  11555. end;
  11556. procedure TTestModule.TestRecord_FieldArray;
  11557. begin
  11558. StartProgram(false);
  11559. Add([
  11560. 'type',
  11561. ' TArrInt = array[3..4] of longint;',
  11562. ' TArrArrInt = array[3..4] of longint;',
  11563. ' TRec = record',
  11564. ' a: array of longint;',
  11565. ' s: array[1..2] of longint;',
  11566. ' m: array[1..2,3..4] of longint;',
  11567. ' o: TArrArrInt;',
  11568. ' end;',
  11569. 'begin']);
  11570. ConvertProgram;
  11571. CheckSource('TestRecord_FieldArray',
  11572. LinesToStr([ // statements
  11573. 'rtl.recNewT(this, "TRec", function () {',
  11574. ' this.m$a$clone = function (a) {',
  11575. ' var b = [];',
  11576. ' b.length = 2;',
  11577. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  11578. ' return b;',
  11579. ' };',
  11580. ' this.$new = function () {',
  11581. ' var r = Object.create(this);',
  11582. ' r.a = [];',
  11583. ' r.s = rtl.arraySetLength(null, 0, 2);',
  11584. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  11585. ' r.o = rtl.arraySetLength(null, 0, 2);',
  11586. ' return r;',
  11587. ' };',
  11588. ' this.$eq = function (b) {',
  11589. ' return (this.a === b.a) && rtl.arrayEq(this.s, b.s) && rtl.arrayEq(this.m, b.m) && rtl.arrayEq(this.o, b.o);',
  11590. ' };',
  11591. ' this.$assign = function (s) {',
  11592. ' this.a = rtl.arrayRef(s.a);',
  11593. ' this.s = s.s.slice(0);',
  11594. ' this.m = this.m$a$clone(s.m);',
  11595. ' this.o = s.o.slice(0);',
  11596. ' return this;',
  11597. ' };',
  11598. '});',
  11599. '']),
  11600. LinesToStr([ // $mod.$main
  11601. '']));
  11602. end;
  11603. procedure TTestModule.TestRecord_Const;
  11604. begin
  11605. StartProgram(false);
  11606. Add([
  11607. 'type',
  11608. ' TArrInt = array[3..4] of longint;',
  11609. ' TPoint = record x,y: longint; end;',
  11610. ' TRec = record',
  11611. ' i: longint;',
  11612. ' a: array of longint;',
  11613. ' s: array[1..2] of longint;',
  11614. ' m: array[1..2,3..4] of longint;',
  11615. ' p: TPoint;',
  11616. ' end;',
  11617. ' TPoints = array of TPoint;',
  11618. 'const',
  11619. ' r: TRec = (',
  11620. ' i:1;',
  11621. ' a:(2,3);',
  11622. ' s:(4,5);',
  11623. ' m:( (11,12), (13,14) );',
  11624. ' p: (x:21; y:22)',
  11625. ' );',
  11626. ' p: TPoints = ( (x:1;y:2), (x:3;y:4) );',
  11627. 'begin']);
  11628. ConvertProgram;
  11629. CheckSource('TestRecord_Const',
  11630. LinesToStr([ // statements
  11631. 'rtl.recNewT(this, "TPoint", function () {',
  11632. ' this.x = 0;',
  11633. ' this.y = 0;',
  11634. ' this.$eq = function (b) {',
  11635. ' return (this.x === b.x) && (this.y === b.y);',
  11636. ' };',
  11637. ' this.$assign = function (s) {',
  11638. ' this.x = s.x;',
  11639. ' this.y = s.y;',
  11640. ' return this;',
  11641. ' };',
  11642. '});',
  11643. 'rtl.recNewT(this, "TRec", function () {',
  11644. ' this.i = 0;',
  11645. ' this.m$a$clone = function (a) {',
  11646. ' var b = [];',
  11647. ' b.length = 2;',
  11648. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  11649. ' return b;',
  11650. ' };',
  11651. ' this.$new = function () {',
  11652. ' var r = Object.create(this);',
  11653. ' r.a = [];',
  11654. ' r.s = rtl.arraySetLength(null, 0, 2);',
  11655. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  11656. ' r.p = $mod.TPoint.$new();',
  11657. ' return r;',
  11658. ' };',
  11659. ' this.$eq = function (b) {',
  11660. ' 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);',
  11661. ' };',
  11662. ' this.$assign = function (s) {',
  11663. ' this.i = s.i;',
  11664. ' this.a = rtl.arrayRef(s.a);',
  11665. ' this.s = s.s.slice(0);',
  11666. ' this.m = this.m$a$clone(s.m);',
  11667. ' this.p.$assign(s.p);',
  11668. ' return this;',
  11669. ' };',
  11670. '});',
  11671. 'this.r = this.TRec.$clone({',
  11672. ' i: 1,',
  11673. ' a: [2, 3],',
  11674. ' s: [4, 5],',
  11675. ' m: [[11, 12], [13, 14]],',
  11676. ' p: this.TPoint.$clone({',
  11677. ' x: 21,',
  11678. ' y: 22',
  11679. ' })',
  11680. '});',
  11681. 'this.p = [this.TPoint.$clone({',
  11682. ' x: 1,',
  11683. ' y: 2',
  11684. '}), this.TPoint.$clone({',
  11685. ' x: 3,',
  11686. ' y: 4',
  11687. '})];',
  11688. '']),
  11689. LinesToStr([ // $mod.$main
  11690. '']));
  11691. end;
  11692. procedure TTestModule.TestRecord_TypecastFail;
  11693. begin
  11694. StartProgram(false);
  11695. Add([
  11696. 'type',
  11697. ' TPoint = record x,y: longint; end;',
  11698. ' TRec = record l: longint end;',
  11699. 'var p: TPoint;',
  11700. 'begin',
  11701. ' if TRec(p).l=2 then ;']);
  11702. SetExpectedPasResolverError('Illegal type conversion: "TPoint" to "record TRec"',
  11703. nIllegalTypeConversionTo);
  11704. ConvertProgram;
  11705. end;
  11706. procedure TTestModule.TestRecord_InFunction;
  11707. begin
  11708. StartProgram(false);
  11709. Add([
  11710. 'var TPoint: longint = 3;',
  11711. 'procedure DoIt;',
  11712. 'type',
  11713. ' TPoint = record x,y: longint; end;',
  11714. ' TPoints = array of TPoint;',
  11715. 'var',
  11716. ' r: TPoint;',
  11717. ' p: TPoints;',
  11718. 'begin',
  11719. ' SetLength(p,2);',
  11720. 'end;',
  11721. 'begin']);
  11722. ConvertProgram;
  11723. CheckSource('TestRecord_InFunction',
  11724. LinesToStr([ // statements
  11725. 'this.TPoint = 3;',
  11726. 'var TPoint$1 = rtl.recNewT(null, "", function () {',
  11727. ' this.x = 0;',
  11728. ' this.y = 0;',
  11729. ' this.$eq = function (b) {',
  11730. ' return (this.x === b.x) && (this.y === b.y);',
  11731. ' };',
  11732. ' this.$assign = function (s) {',
  11733. ' this.x = s.x;',
  11734. ' this.y = s.y;',
  11735. ' return this;',
  11736. ' };',
  11737. '});',
  11738. 'this.DoIt = function () {',
  11739. ' var r = TPoint$1.$new();',
  11740. ' var p = [];',
  11741. ' p = rtl.arraySetLength(p, TPoint$1, 2);',
  11742. '};',
  11743. '']),
  11744. LinesToStr([ // $mod.$main
  11745. '']));
  11746. end;
  11747. procedure TTestModule.TestRecord_AnonymousFail;
  11748. begin
  11749. StartProgram(false);
  11750. Add([
  11751. 'var',
  11752. ' r: record x: word end;',
  11753. 'begin']);
  11754. SetExpectedPasResolverError('not yet implemented: :TPasRecordType [20190408224556] "anonymous record type"',
  11755. nNotYetImplemented);
  11756. ConvertProgram;
  11757. end;
  11758. procedure TTestModule.TestAdvRecord_Function;
  11759. begin
  11760. StartProgram(false);
  11761. Parser.Options:=Parser.Options+[po_cassignments];
  11762. Add([
  11763. '{$modeswitch AdvancedRecords}',
  11764. 'type',
  11765. ' TPoint = record',
  11766. ' x,y: word;',
  11767. ' function Add(const apt: TPoint): TPoint;',
  11768. ' end;',
  11769. 'function TPoint.Add(const apt: TPoint): TPoint;',
  11770. 'begin',
  11771. ' Result:=Self;',
  11772. ' Result.x+=apt.x;',
  11773. ' Result.y:=Result.y+apt.y;',
  11774. ' Self:=apt;',
  11775. 'end;',
  11776. 'var p,q: TPoint;',
  11777. 'begin',
  11778. ' p.add(q);',
  11779. ' p:=default(TPoint);',
  11780. ' p:=q;',
  11781. '']);
  11782. ConvertProgram;
  11783. CheckSource('TestAdvRecord_Function',
  11784. LinesToStr([ // statements
  11785. 'rtl.recNewT(this, "TPoint", function () {',
  11786. ' this.x = 0;',
  11787. ' this.y = 0;',
  11788. ' this.$eq = function (b) {',
  11789. ' return (this.x === b.x) && (this.y === b.y);',
  11790. ' };',
  11791. ' this.$assign = function (s) {',
  11792. ' this.x = s.x;',
  11793. ' this.y = s.y;',
  11794. ' return this;',
  11795. ' };',
  11796. ' this.Add = function (apt) {',
  11797. ' var Result = $mod.TPoint.$new();',
  11798. ' Result.$assign(this);',
  11799. ' Result.x += apt.x;',
  11800. ' Result.y = Result.y + apt.y;',
  11801. ' this.$assign(apt);',
  11802. ' return Result;',
  11803. ' };',
  11804. '});',
  11805. 'this.p = this.TPoint.$new();',
  11806. 'this.q = this.TPoint.$new();',
  11807. '']),
  11808. LinesToStr([ // $mod.$main
  11809. '$mod.p.Add($mod.q);',
  11810. '$mod.p.$assign($mod.TPoint.$new());',
  11811. '$mod.p.$assign($mod.q);',
  11812. '']));
  11813. end;
  11814. procedure TTestModule.TestAdvRecord_Property;
  11815. begin
  11816. StartProgram(false);
  11817. Add([
  11818. '{$modeswitch AdvancedRecords}',
  11819. 'type',
  11820. ' TPoint = record',
  11821. ' x,y: word;',
  11822. ' strict private',
  11823. ' function GetSize: longword;',
  11824. ' procedure SetSize(Value: longword);',
  11825. ' public',
  11826. ' property Size: longword read GetSize write SetSize;',
  11827. ' property Left: word read x write y;',
  11828. ' end;',
  11829. 'procedure SetSize(Value: longword); begin end;',// check auto rename
  11830. 'function TPoint.GetSize: longword;',
  11831. 'begin',
  11832. ' x:=y;',
  11833. ' Size:=Size;',
  11834. ' Left:=Left;',
  11835. 'end;',
  11836. 'procedure TPoint.SetSize(Value: longword);',
  11837. 'begin',
  11838. 'end;',
  11839. 'var p,q: TPoint;',
  11840. 'begin',
  11841. ' p.Size:=q.Size;',
  11842. ' p.Left:=q.Left;',
  11843. '']);
  11844. ConvertProgram;
  11845. CheckSource('TestAdvRecord_Property',
  11846. LinesToStr([ // statements
  11847. 'rtl.recNewT(this, "TPoint", function () {',
  11848. ' this.x = 0;',
  11849. ' this.y = 0;',
  11850. ' this.$eq = function (b) {',
  11851. ' return (this.x === b.x) && (this.y === b.y);',
  11852. ' };',
  11853. ' this.$assign = function (s) {',
  11854. ' this.x = s.x;',
  11855. ' this.y = s.y;',
  11856. ' return this;',
  11857. ' };',
  11858. ' this.GetSize = function () {',
  11859. ' var Result = 0;',
  11860. ' this.x = this.y;',
  11861. ' this.SetSize(this.GetSize());',
  11862. ' this.y = this.x;',
  11863. ' return Result;',
  11864. ' };',
  11865. ' this.SetSize = function (Value) {',
  11866. ' };',
  11867. '});',
  11868. 'this.SetSize = function (Value) {',
  11869. '};',
  11870. 'this.p = this.TPoint.$new();',
  11871. 'this.q = this.TPoint.$new();',
  11872. '']),
  11873. LinesToStr([ // $mod.$main
  11874. '$mod.p.SetSize($mod.q.GetSize());',
  11875. '$mod.p.y = $mod.q.x;',
  11876. '']));
  11877. end;
  11878. procedure TTestModule.TestAdvRecord_PropertyDefault;
  11879. begin
  11880. StartProgram(false);
  11881. Add([
  11882. '{$modeswitch AdvancedRecords}',
  11883. 'type',
  11884. ' TPoint = record',
  11885. ' strict private',
  11886. ' function GetItems(Index: word): word;',
  11887. ' procedure SetItems(Index: word; Value: word);',
  11888. ' public',
  11889. ' property Items[Index: word]: word read GetItems write SetItems; default;',
  11890. ' end;',
  11891. 'function TPoint.GetItems(Index: word): word;',
  11892. 'begin',
  11893. ' Items[index]:=Items[index];',
  11894. ' self.Items[index]:=self.Items[index];',
  11895. 'end;',
  11896. 'procedure TPoint.SetItems(Index: word; Value: word);',
  11897. 'begin',
  11898. 'end;',
  11899. 'var p: TPoint;',
  11900. 'begin',
  11901. ' p[1]:=p[2];',
  11902. ' p.Items[3]:=p.Items[4];',
  11903. '']);
  11904. ConvertProgram;
  11905. CheckSource('TestAdvRecord_PropertyDefault',
  11906. LinesToStr([ // statements
  11907. 'rtl.recNewT(this, "TPoint", function () {',
  11908. ' this.$eq = function (b) {',
  11909. ' return true;',
  11910. ' };',
  11911. ' this.$assign = function (s) {',
  11912. ' return this;',
  11913. ' };',
  11914. ' this.GetItems = function (Index) {',
  11915. ' var Result = 0;',
  11916. ' this.SetItems(Index, this.GetItems(Index));',
  11917. ' this.SetItems(Index, this.GetItems(Index));',
  11918. ' return Result;',
  11919. ' };',
  11920. ' this.SetItems = function (Index, Value) {',
  11921. ' };',
  11922. '});',
  11923. 'this.p = this.TPoint.$new();',
  11924. '']),
  11925. LinesToStr([ // $mod.$main
  11926. '$mod.p.SetItems(1, $mod.p.GetItems(2));',
  11927. '$mod.p.SetItems(3, $mod.p.GetItems(4));',
  11928. '']));
  11929. end;
  11930. procedure TTestModule.TestAdvRecord_Property_ClassMethod;
  11931. begin
  11932. StartProgram(false);
  11933. Add([
  11934. '{$modeswitch AdvancedRecords}',
  11935. 'type',
  11936. ' TRec = record',
  11937. ' class var',
  11938. ' Fx: longint;',
  11939. ' Fy: longint;',
  11940. ' class function GetInt: longint; static;',
  11941. ' class procedure SetInt(Value: longint); static;',
  11942. ' class procedure DoIt; static;',
  11943. ' class property IntA: longint read Fx write Fy;',
  11944. ' class property IntB: longint read GetInt write SetInt;',
  11945. ' end;',
  11946. 'class function trec.getint: longint;',
  11947. 'begin',
  11948. ' result:=fx;',
  11949. 'end;',
  11950. 'class procedure trec.setint(value: longint);',
  11951. 'begin',
  11952. 'end;',
  11953. 'class procedure trec.doit;',
  11954. 'begin',
  11955. ' IntA:=IntA+1;',
  11956. ' IntB:=IntB+1;',
  11957. 'end;',
  11958. 'var r: trec;',
  11959. 'begin',
  11960. ' trec.inta:=trec.inta+1;',
  11961. ' if trec.intb=2 then;',
  11962. ' trec.intb:=trec.intb+2;',
  11963. ' trec.setint(trec.inta);',
  11964. ' r.inta:=r.inta+1;',
  11965. ' if r.intb=2 then;',
  11966. ' r.intb:=r.intb+2;',
  11967. ' r.setint(r.inta);']);
  11968. ConvertProgram;
  11969. CheckSource('TestAdvRecord_Property_ClassMethod',
  11970. LinesToStr([ // statements
  11971. 'rtl.recNewT(this, "TRec", function () {',
  11972. ' this.Fx = 0;',
  11973. ' this.Fy = 0;',
  11974. ' this.$eq = function (b) {',
  11975. ' return true;',
  11976. ' };',
  11977. ' this.$assign = function (s) {',
  11978. ' return this;',
  11979. ' };',
  11980. ' this.GetInt = function () {',
  11981. ' var Result = 0;',
  11982. ' Result = $mod.TRec.Fx;',
  11983. ' return Result;',
  11984. ' };',
  11985. ' this.SetInt = function (Value) {',
  11986. ' };',
  11987. ' this.DoIt = function () {',
  11988. ' $mod.TRec.Fy = $mod.TRec.Fx + 1;',
  11989. ' $mod.TRec.SetInt($mod.TRec.GetInt() + 1);',
  11990. ' };',
  11991. '}, true);',
  11992. 'this.r = this.TRec.$new();',
  11993. '']),
  11994. LinesToStr([ // $mod.$main
  11995. '$mod.TRec.Fy = $mod.TRec.Fx + 1;',
  11996. 'if ($mod.TRec.GetInt() === 2) ;',
  11997. '$mod.TRec.SetInt($mod.TRec.GetInt() + 2);',
  11998. '$mod.TRec.SetInt($mod.TRec.Fx);',
  11999. '$mod.TRec.Fy = $mod.r.Fx + 1;',
  12000. 'if ($mod.TRec.GetInt() === 2) ;',
  12001. '$mod.TRec.SetInt($mod.TRec.GetInt() + 2);',
  12002. '$mod.TRec.SetInt($mod.r.Fx);',
  12003. '']));
  12004. end;
  12005. procedure TTestModule.TestAdvRecord_Const;
  12006. begin
  12007. StartProgram(false);
  12008. Add([
  12009. '{$modeswitch AdvancedRecords}',
  12010. 'type',
  12011. ' TArrInt = array[3..4] of longint;',
  12012. ' TPoint = record',
  12013. ' x,y: longint;',
  12014. ' class var Count: nativeint;',
  12015. ' end;',
  12016. ' TRec = record',
  12017. ' i: longint;',
  12018. ' a: array of longint;',
  12019. ' s: array[1..2] of longint;',
  12020. ' m: array[1..2,3..4] of longint;',
  12021. ' p: TPoint;',
  12022. ' end;',
  12023. ' TPoints = array of TPoint;',
  12024. 'const',
  12025. ' r: TRec = (',
  12026. ' i:1;',
  12027. ' a:(2,3);',
  12028. ' s:(4,5);',
  12029. ' m:( (11,12), (13,14) );',
  12030. ' p: (x:21)',
  12031. ' );',
  12032. ' p: TPoints = ( (x:1;y:2), (x:3;y:4) );',
  12033. 'begin']);
  12034. ConvertProgram;
  12035. CheckSource('TestAdvRecord_Const',
  12036. LinesToStr([ // statements
  12037. 'rtl.recNewT(this, "TPoint", function () {',
  12038. ' this.x = 0;',
  12039. ' this.y = 0;',
  12040. ' this.Count = 0;',
  12041. ' this.$eq = function (b) {',
  12042. ' return (this.x === b.x) && (this.y === b.y);',
  12043. ' };',
  12044. ' this.$assign = function (s) {',
  12045. ' this.x = s.x;',
  12046. ' this.y = s.y;',
  12047. ' return this;',
  12048. ' };',
  12049. '}, true);',
  12050. 'rtl.recNewT(this, "TRec", function () {',
  12051. ' this.i = 0;',
  12052. ' this.m$a$clone = function (a) {',
  12053. ' var b = [];',
  12054. ' b.length = 2;',
  12055. ' for (var c = 0; c < 2; c++) b[c] = a[c].slice(0);',
  12056. ' return b;',
  12057. ' };',
  12058. ' this.$new = function () {',
  12059. ' var r = Object.create(this);',
  12060. ' r.a = [];',
  12061. ' r.s = rtl.arraySetLength(null, 0, 2);',
  12062. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  12063. ' r.p = $mod.TPoint.$new();',
  12064. ' return r;',
  12065. ' };',
  12066. ' this.$eq = function (b) {',
  12067. ' 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);',
  12068. ' };',
  12069. ' this.$assign = function (s) {',
  12070. ' this.i = s.i;',
  12071. ' this.a = rtl.arrayRef(s.a);',
  12072. ' this.s = s.s.slice(0);',
  12073. ' this.m = this.m$a$clone(s.m);',
  12074. ' this.p.$assign(s.p);',
  12075. ' return this;',
  12076. ' };',
  12077. '});',
  12078. 'this.r = this.TRec.$clone({',
  12079. ' i: 1,',
  12080. ' a: [2, 3],',
  12081. ' s: [4, 5],',
  12082. ' m: [[11, 12], [13, 14]],',
  12083. ' p: this.TPoint.$clone({',
  12084. ' x: 21,',
  12085. ' y: 0',
  12086. ' })',
  12087. '});',
  12088. 'this.p = [this.TPoint.$clone({',
  12089. ' x: 1,',
  12090. ' y: 2',
  12091. '}), this.TPoint.$clone({',
  12092. ' x: 3,',
  12093. ' y: 4',
  12094. '})];',
  12095. '']),
  12096. LinesToStr([ // $mod.$main
  12097. '']));
  12098. end;
  12099. procedure TTestModule.TestAdvRecord_ExternalField;
  12100. begin
  12101. StartProgram(false);
  12102. Add([
  12103. '{$modeswitch AdvancedRecords}',
  12104. '{$modeswitch externalclass}',
  12105. 'type',
  12106. ' TCar = record',
  12107. ' public',
  12108. ' Intern: longint external name ''$Intern'';',
  12109. ' Intern2: longint external name ''$Intern2'';',
  12110. ' Bracket: longint external name ''["A B"]'';',
  12111. ' procedure DoIt;',
  12112. ' end;',
  12113. 'procedure tcar.doit;',
  12114. 'begin',
  12115. ' Intern:=Intern+1;',
  12116. ' Intern2:=Intern2+2;',
  12117. ' Bracket:=Bracket+3;',
  12118. 'end;',
  12119. 'var Rec: TCar = (intern: 11; intern2: 12; bracket: 13);',
  12120. 'begin',
  12121. ' Rec.intern:=Rec.intern+1;',
  12122. ' Rec.intern2:=Rec.intern2+2;',
  12123. ' Rec.Bracket:=Rec.Bracket+3;',
  12124. ' with Rec do begin',
  12125. ' intern:=intern+1;',
  12126. ' intern2:=intern2+2;',
  12127. ' Bracket:=Bracket+3;',
  12128. ' end;']);
  12129. ConvertProgram;
  12130. CheckSource('TestAdvRecord_ExternalField',
  12131. LinesToStr([ // statements
  12132. 'rtl.recNewT(this, "TCar", function () {',
  12133. ' this.$eq = function (b) {',
  12134. ' return (this.$Intern === b.$Intern) && (this.$Intern2 === b.$Intern2) && (this["A B"] === b["A B"]);',
  12135. ' };',
  12136. ' this.$assign = function (s) {',
  12137. ' this.$Intern = s.$Intern;',
  12138. ' this.$Intern2 = s.$Intern2;',
  12139. ' this["A B"] = s["A B"];',
  12140. ' return this;',
  12141. ' };',
  12142. ' this.DoIt = function () {',
  12143. ' this.$Intern = this.$Intern + 1;',
  12144. ' this.$Intern2 = this.$Intern2 + 2;',
  12145. ' this["A B"] = this["A B"] + 3;',
  12146. ' };',
  12147. '});',
  12148. 'this.Rec = this.TCar.$clone({',
  12149. ' $Intern: 11,',
  12150. ' $Intern2: 12,',
  12151. ' "A B": 13',
  12152. '});',
  12153. '']),
  12154. LinesToStr([ // $mod.$main
  12155. '$mod.Rec.$Intern = $mod.Rec.$Intern + 1;',
  12156. '$mod.Rec.$Intern2 = $mod.Rec.$Intern2 + 2;',
  12157. '$mod.Rec["A B"] = $mod.Rec["A B"] + 3;',
  12158. 'var $with = $mod.Rec;',
  12159. '$with.$Intern = $with.$Intern + 1;',
  12160. '$with.$Intern2 = $with.$Intern2 + 2;',
  12161. '$with["A B"] = $with["A B"] + 3;',
  12162. '']));
  12163. end;
  12164. procedure TTestModule.TestAdvRecord_SubRecord;
  12165. begin
  12166. StartProgram(false);
  12167. Add([
  12168. '{$modeswitch AdvancedRecords}',
  12169. 'type',
  12170. ' TRec = record',
  12171. ' type',
  12172. ' TPoint = record',
  12173. ' x,y: longint;',
  12174. ' class var Count: nativeint;',
  12175. ' procedure DoIt;',
  12176. ' class procedure DoThat; static;',
  12177. ' end;',
  12178. ' var',
  12179. ' i: longint;',
  12180. ' p: TPoint;',
  12181. ' procedure DoSome;',
  12182. ' end;',
  12183. 'const',
  12184. ' r: TRec = (',
  12185. ' i:1;',
  12186. ' p: (x:21;y:22)',
  12187. ' );',
  12188. 'procedure TRec.DoSome;',
  12189. 'begin',
  12190. ' p.x:=p.y+1;',
  12191. ' p.Count:=p.Count+2;',
  12192. 'end;',
  12193. 'procedure TRec.TPoint.DoIt;',
  12194. 'begin',
  12195. ' Count:=Count+3;',
  12196. 'end;',
  12197. 'class procedure TRec.TPoint.DoThat;',
  12198. 'begin',
  12199. ' Count:=Count+4;',
  12200. 'end;',
  12201. 'begin']);
  12202. ConvertProgram;
  12203. CheckSource('TestAdvRecord_SubRecord',
  12204. LinesToStr([ // statements
  12205. 'rtl.recNewT(this, "TRec", function () {',
  12206. ' rtl.recNewT(this, "TPoint", function () {',
  12207. ' this.x = 0;',
  12208. ' this.y = 0;',
  12209. ' this.Count = 0;',
  12210. ' this.$eq = function (b) {',
  12211. ' return (this.x === b.x) && (this.y === b.y);',
  12212. ' };',
  12213. ' this.$assign = function (s) {',
  12214. ' this.x = s.x;',
  12215. ' this.y = s.y;',
  12216. ' return this;',
  12217. ' };',
  12218. ' this.DoIt = function () {',
  12219. ' $mod.TRec.TPoint.Count = this.Count + 3;',
  12220. ' };',
  12221. ' this.DoThat = function () {',
  12222. ' $mod.TRec.TPoint.Count = $mod.TRec.TPoint.Count + 4;',
  12223. ' };',
  12224. ' }, true);',
  12225. ' this.i = 0;',
  12226. ' this.$new = function () {',
  12227. ' var r = Object.create(this);',
  12228. ' r.p = this.TPoint.$new();',
  12229. ' return r;',
  12230. ' };',
  12231. ' this.$eq = function (b) {',
  12232. ' return (this.i === b.i) && this.p.$eq(b.p);',
  12233. ' };',
  12234. ' this.$assign = function (s) {',
  12235. ' this.i = s.i;',
  12236. ' this.p.$assign(s.p);',
  12237. ' return this;',
  12238. ' };',
  12239. ' this.DoSome = function () {',
  12240. ' this.p.x = this.p.y + 1;',
  12241. ' this.TPoint.Count = this.p.Count + 2;',
  12242. ' };',
  12243. '}, true);',
  12244. 'this.r = this.TRec.$clone({',
  12245. ' i: 1,',
  12246. ' p: this.TRec.TPoint.$clone({',
  12247. ' x: 21,',
  12248. ' y: 22',
  12249. ' })',
  12250. '});',
  12251. '']),
  12252. LinesToStr([ // $mod.$main
  12253. '']));
  12254. end;
  12255. procedure TTestModule.TestAdvRecord_SubClass;
  12256. begin
  12257. StartProgram(false);
  12258. Add([
  12259. '{$modeswitch AdvancedRecords}',
  12260. 'type',
  12261. ' TObject = class end;',
  12262. ' TPoint = record',
  12263. ' type',
  12264. ' TBird = class',
  12265. ' procedure DoIt;',
  12266. ' class procedure Glob;',
  12267. ' end;',
  12268. ' procedure DoIt(b: TBird);',
  12269. ' end;',
  12270. 'procedure TPoint.TBird.DoIt;',
  12271. 'begin',
  12272. ' doit;',
  12273. ' self.doit;',
  12274. ' glob;',
  12275. ' self.glob;',
  12276. 'end;',
  12277. 'class procedure TPoint.TBird.Glob;',
  12278. 'begin',
  12279. ' glob;',
  12280. ' self.glob;',
  12281. 'end;',
  12282. 'procedure TPoint.DoIt(b: TBird);',
  12283. 'begin',
  12284. ' b.doit;',
  12285. ' b.glob;',
  12286. ' TBird.glob;',
  12287. 'end;',
  12288. 'begin',
  12289. '']);
  12290. ConvertProgram;
  12291. CheckSource('TestAdvRecord_SubClass',
  12292. LinesToStr([ // statements
  12293. 'rtl.createClass(this, "TObject", null, function () {',
  12294. ' this.$init = function () {',
  12295. ' };',
  12296. ' this.$final = function () {',
  12297. ' };',
  12298. '});',
  12299. 'rtl.recNewT(this, "TPoint", function () {',
  12300. ' rtl.createClass(this, "TBird", $mod.TObject, function () {',
  12301. ' this.DoIt = function () {',
  12302. ' this.DoIt();',
  12303. ' this.DoIt();',
  12304. ' this.$class.Glob();',
  12305. ' this.$class.Glob();',
  12306. ' };',
  12307. ' this.Glob = function () {',
  12308. ' this.Glob();',
  12309. ' this.Glob();',
  12310. ' };',
  12311. ' }, "TPoint.TBird");',
  12312. ' this.$eq = function (b) {',
  12313. ' return true;',
  12314. ' };',
  12315. ' this.$assign = function (s) {',
  12316. ' return this;',
  12317. ' };',
  12318. ' this.DoIt = function (b) {',
  12319. ' b.DoIt();',
  12320. ' b.$class.Glob();',
  12321. ' this.TBird.Glob();',
  12322. ' };',
  12323. '}, true);',
  12324. '']),
  12325. LinesToStr([ // $mod.$main
  12326. '']));
  12327. end;
  12328. procedure TTestModule.TestAdvRecord_SubInterfaceFail;
  12329. begin
  12330. StartProgram(false);
  12331. Add([
  12332. '{$modeswitch AdvancedRecords}',
  12333. 'type',
  12334. ' IUnknown = interface end;',
  12335. ' TPoint = record',
  12336. ' type IBird = interface end;',
  12337. ' end;',
  12338. 'begin',
  12339. '']);
  12340. SetExpectedPasResolverError('not yet implemented: IBird:TPasClassType [20190105143752] "interface inside record"',
  12341. nNotYetImplemented);
  12342. ParseProgram;
  12343. end;
  12344. procedure TTestModule.TestAdvRecord_Constructor;
  12345. begin
  12346. StartProgram(false);
  12347. Add([
  12348. '{$modeswitch AdvancedRecords}',
  12349. 'type',
  12350. ' TPoint = record',
  12351. ' x,y: longint;',
  12352. ' class procedure Run(w: longint = 13); static;',
  12353. ' constructor Create(ax: longint; ay: longint = -1);',
  12354. ' end;',
  12355. 'class procedure tpoint.run(w: longint);',
  12356. 'begin',
  12357. ' run;',
  12358. ' run();',
  12359. 'end;',
  12360. 'constructor tpoint.create(ax,ay: longint);',
  12361. 'begin',
  12362. ' x:=ax;',
  12363. ' self.y:=ay;',
  12364. ' run;',
  12365. ' run(ax);',
  12366. 'end;',
  12367. 'var r: TPoint;',
  12368. 'begin',
  12369. ' r:=TPoint.Create(1,2);',
  12370. ' with TPoint do r:=Create(1,2);',
  12371. ' r.Create(3);',
  12372. ' r:=r.Create(4);',
  12373. '']);
  12374. ConvertProgram;
  12375. CheckSource('TestAdvRecord_Constructor',
  12376. LinesToStr([ // statements
  12377. 'rtl.recNewT(this, "TPoint", function () {',
  12378. ' this.x = 0;',
  12379. ' this.y = 0;',
  12380. ' this.$eq = function (b) {',
  12381. ' return (this.x === b.x) && (this.y === b.y);',
  12382. ' };',
  12383. ' this.$assign = function (s) {',
  12384. ' this.x = s.x;',
  12385. ' this.y = s.y;',
  12386. ' return this;',
  12387. ' };',
  12388. ' this.Run = function (w) {',
  12389. ' $mod.TPoint.Run(13);',
  12390. ' $mod.TPoint.Run(13);',
  12391. ' };',
  12392. ' this.Create = function (ax, ay) {',
  12393. ' this.x = ax;',
  12394. ' this.y = ay;',
  12395. ' this.Run(13);',
  12396. ' this.Run(ax);',
  12397. ' return this;',
  12398. ' };',
  12399. '});',
  12400. 'this.r = this.TPoint.$new();',
  12401. '']),
  12402. LinesToStr([ // $mod.$main
  12403. '$mod.r.$assign($mod.TPoint.$new().Create(1, 2));',
  12404. 'var $with = $mod.TPoint;',
  12405. '$mod.r.$assign($with.$new().Create(1, 2));',
  12406. '$mod.r.Create(3, -1);',
  12407. '$mod.r.$assign($mod.r.Create(4, -1));',
  12408. '']));
  12409. end;
  12410. procedure TTestModule.TestAdvRecord_ClassConstructor_Program;
  12411. begin
  12412. StartProgram(false);
  12413. Add([
  12414. '{$modeswitch AdvancedRecords}',
  12415. 'type',
  12416. ' TPoint = record',
  12417. ' class var x: longint;',
  12418. ' class procedure Fly; static;',
  12419. ' class constructor Init;',
  12420. ' end;',
  12421. 'var count: word;',
  12422. 'class procedure Tpoint.Fly;',
  12423. 'begin',
  12424. 'end;',
  12425. 'class constructor tpoint.init;',
  12426. 'begin',
  12427. ' count:=count+1;',
  12428. ' x:=x+3;',
  12429. ' tpoint.x:=tpoint.x+4;',
  12430. ' fly;',
  12431. ' tpoint.fly;',
  12432. 'end;',
  12433. 'var r: TPoint;',
  12434. 'begin',
  12435. ' r.x:=r.x+10;',
  12436. ' r.Fly;',
  12437. ' r.Fly();',
  12438. '']);
  12439. ConvertProgram;
  12440. CheckSource('TestAdvRecord_ClassConstructor_Program',
  12441. LinesToStr([ // statements
  12442. 'rtl.recNewT(this, "TPoint", function () {',
  12443. ' this.x = 0;',
  12444. ' this.$eq = function (b) {',
  12445. ' return true;',
  12446. ' };',
  12447. ' this.$assign = function (s) {',
  12448. ' return this;',
  12449. ' };',
  12450. ' this.Fly = function () {',
  12451. ' };',
  12452. '}, true);',
  12453. 'this.count = 0;',
  12454. 'this.r = this.TPoint.$new();',
  12455. '']),
  12456. LinesToStr([ // $mod.$main
  12457. '(function () {',
  12458. ' $mod.count = $mod.count + 1;',
  12459. ' $mod.TPoint.x = $mod.TPoint.x + 3;',
  12460. ' $mod.TPoint.x = $mod.TPoint.x + 4;',
  12461. ' $mod.TPoint.Fly();',
  12462. ' $mod.TPoint.Fly();',
  12463. '})();',
  12464. '$mod.TPoint.x = $mod.r.x + 10;',
  12465. '$mod.TPoint.Fly();',
  12466. '$mod.TPoint.Fly();',
  12467. '']));
  12468. end;
  12469. procedure TTestModule.TestAdvRecord_ClassConstructor_Unit;
  12470. begin
  12471. StartUnit(false);
  12472. Add([
  12473. 'interface',
  12474. '{$modeswitch AdvancedRecords}',
  12475. 'type',
  12476. ' TPoint = record',
  12477. ' class var x: longint;',
  12478. ' class procedure Fly; static;',
  12479. ' class constructor Init;',
  12480. ' end;',
  12481. 'implementation',
  12482. 'var count: word;',
  12483. 'class procedure Tpoint.Fly;',
  12484. 'begin',
  12485. 'end;',
  12486. 'class constructor tpoint.init;',
  12487. 'begin',
  12488. ' count:=count+1;',
  12489. ' x:=3;',
  12490. ' tpoint.x:=4;',
  12491. ' fly;',
  12492. ' tpoint.fly;',
  12493. 'end;',
  12494. '']);
  12495. ConvertUnit;
  12496. CheckSource('TestAdvRecord_ClassConstructor_Unit',
  12497. LinesToStr([ // statements
  12498. 'var $impl = $mod.$impl;',
  12499. 'rtl.recNewT(this, "TPoint", function () {',
  12500. ' this.x = 0;',
  12501. ' this.$eq = function (b) {',
  12502. ' return true;',
  12503. ' };',
  12504. ' this.$assign = function (s) {',
  12505. ' return this;',
  12506. ' };',
  12507. ' this.Fly = function () {',
  12508. ' };',
  12509. '}, true);',
  12510. '']),
  12511. LinesToStr([ // $mod.$init
  12512. '(function () {',
  12513. ' $impl.count = $impl.count + 1;',
  12514. ' $mod.TPoint.x = 3;',
  12515. ' $mod.TPoint.x = 4;',
  12516. ' $mod.TPoint.Fly();',
  12517. ' $mod.TPoint.Fly();',
  12518. '})();',
  12519. '']),
  12520. LinesToStr([ // $mod.$main
  12521. '$impl.count = 0;',
  12522. '']));
  12523. end;
  12524. procedure TTestModule.TestClass_TObjectDefaultConstructor;
  12525. begin
  12526. StartProgram(false);
  12527. Add(['type',
  12528. ' TObject = class',
  12529. ' public',
  12530. ' constructor Create;',
  12531. ' destructor Destroy;',
  12532. ' end;',
  12533. ' TBird = TObject;',
  12534. 'constructor tobject.create;',
  12535. 'begin end;',
  12536. 'destructor tobject.destroy;',
  12537. 'begin end;',
  12538. 'var Obj: tobject;',
  12539. 'begin',
  12540. ' obj:=tobject.create;',
  12541. ' obj:=tobject.create();',
  12542. ' obj:=tbird.create;',
  12543. ' obj:=tbird.create();',
  12544. ' obj:=obj.create();',
  12545. ' obj.destroy;',
  12546. '']);
  12547. ConvertProgram;
  12548. CheckSource('TestClass_TObjectDefaultConstructor',
  12549. LinesToStr([ // statements
  12550. 'rtl.createClass(this,"TObject",null,function(){',
  12551. ' this.$init = function () {',
  12552. ' };',
  12553. ' this.$final = function () {',
  12554. ' };',
  12555. ' this.Create = function(){',
  12556. ' return this;',
  12557. ' };',
  12558. ' this.Destroy = function(){',
  12559. ' };',
  12560. '});',
  12561. 'this.Obj = null;'
  12562. ]),
  12563. LinesToStr([ // $mod.$main
  12564. '$mod.Obj = $mod.TObject.$create("Create");',
  12565. '$mod.Obj = $mod.TObject.$create("Create");',
  12566. '$mod.Obj = $mod.TObject.$create("Create");',
  12567. '$mod.Obj = $mod.TObject.$create("Create");',
  12568. '$mod.Obj = $mod.Obj.Create();',
  12569. '$mod.Obj.$destroy("Destroy");',
  12570. '']));
  12571. end;
  12572. procedure TTestModule.TestClass_TObjectConstructorWithParams;
  12573. begin
  12574. StartProgram(false);
  12575. Add('type');
  12576. Add(' TObject = class');
  12577. Add(' public');
  12578. Add(' constructor Create(Par: longint);');
  12579. Add(' end;');
  12580. Add('constructor tobject.create(par: longint);');
  12581. Add('begin end;');
  12582. Add('var Obj: tobject;');
  12583. Add('begin');
  12584. Add(' obj:=tobject.create(3);');
  12585. ConvertProgram;
  12586. CheckSource('TestClass_TObjectConstructorWithParams',
  12587. LinesToStr([ // statements
  12588. 'rtl.createClass(this,"TObject",null,function(){',
  12589. ' this.$init = function () {',
  12590. ' };',
  12591. ' this.$final = function () {',
  12592. ' };',
  12593. ' this.Create = function(Par){',
  12594. ' return this;',
  12595. ' };',
  12596. '});',
  12597. 'this.Obj = null;'
  12598. ]),
  12599. LinesToStr([ // $mod.$main
  12600. '$mod.Obj = $mod.TObject.$create("Create",[3]);'
  12601. ]));
  12602. end;
  12603. procedure TTestModule.TestClass_TObjectConstructorWithDefaultParam;
  12604. begin
  12605. StartProgram(false);
  12606. Add('type');
  12607. Add(' TObject = class');
  12608. Add(' public');
  12609. Add(' constructor Create;');
  12610. Add(' end;');
  12611. Add(' TTest = class(TObject)');
  12612. Add(' public');
  12613. Add(' constructor Create(const Par: longint = 1);');
  12614. Add(' end;');
  12615. Add('constructor tobject.create;');
  12616. Add('begin end;');
  12617. Add('constructor ttest.create(const par: longint);');
  12618. Add('begin end;');
  12619. Add('var t: ttest;');
  12620. Add('begin');
  12621. Add(' t:=ttest.create;');
  12622. Add(' t:=ttest.create(2);');
  12623. ConvertProgram;
  12624. CheckSource('TestClass_TObjectConstructorWithDefaultParam',
  12625. LinesToStr([ // statements
  12626. 'rtl.createClass(this,"TObject",null,function(){',
  12627. ' this.$init = function () {',
  12628. ' };',
  12629. ' this.$final = function () {',
  12630. ' };',
  12631. ' this.Create = function(){',
  12632. ' return this;',
  12633. ' };',
  12634. '});',
  12635. 'rtl.createClass(this, "TTest", this.TObject, function () {',
  12636. ' this.Create$1 = function (Par) {',
  12637. ' return this;',
  12638. ' };',
  12639. '});',
  12640. 'this.t = null;'
  12641. ]),
  12642. LinesToStr([ // $mod.$main
  12643. '$mod.t = $mod.TTest.$create("Create$1", [1]);',
  12644. '$mod.t = $mod.TTest.$create("Create$1", [2]);'
  12645. ]));
  12646. end;
  12647. procedure TTestModule.TestClass_Var;
  12648. begin
  12649. StartProgram(false);
  12650. Add([
  12651. 'type',
  12652. ' TObject = class',
  12653. ' public',
  12654. ' vI: longint;',
  12655. ' constructor Create(Par: longint);',
  12656. ' end;',
  12657. 'constructor tobject.create(par: longint);',
  12658. 'begin',
  12659. ' vi:=par+3',
  12660. 'end;',
  12661. 'var Obj: tobject;',
  12662. 'begin',
  12663. ' obj:=tobject.create(4);',
  12664. ' obj.vi:=obj.VI+5;']);
  12665. ConvertProgram;
  12666. CheckSource('TestClass_Var',
  12667. LinesToStr([ // statements
  12668. 'rtl.createClass(this,"TObject",null,function(){',
  12669. ' this.$init = function () {',
  12670. ' this.vI = 0;',
  12671. ' };',
  12672. ' this.$final = function () {',
  12673. ' };',
  12674. ' this.Create = function(Par){',
  12675. ' this.vI = Par+3;',
  12676. ' return this;',
  12677. ' };',
  12678. '});',
  12679. 'this.Obj = null;'
  12680. ]),
  12681. LinesToStr([ // $mod.$main
  12682. '$mod.Obj = $mod.TObject.$create("Create",[4]);',
  12683. '$mod.Obj.vI = $mod.Obj.vI + 5;'
  12684. ]));
  12685. end;
  12686. procedure TTestModule.TestClass_Method;
  12687. begin
  12688. StartProgram(false);
  12689. Add('type');
  12690. Add(' TObject = class');
  12691. Add(' public');
  12692. Add(' vI: longint;');
  12693. Add(' Sub: TObject;');
  12694. Add(' constructor Create;');
  12695. Add(' function GetIt(Par: longint): tobject;');
  12696. Add(' end;');
  12697. Add('constructor tobject.create; begin end;');
  12698. Add('function tobject.getit(par: longint): tobject;');
  12699. Add('begin');
  12700. Add(' Self.vi:=par+3;');
  12701. Add(' Result:=self.sub;');
  12702. Add('end;');
  12703. Add('var Obj: tobject;');
  12704. Add('begin');
  12705. Add(' obj:=tobject.create;');
  12706. Add(' obj.getit(4);');
  12707. Add(' obj.sub.sub:=nil;');
  12708. Add(' obj.sub.getit(5);');
  12709. Add(' obj.sub.getit(6).SUB:=nil;');
  12710. Add(' obj.sub.getit(7).GETIT(8);');
  12711. Add(' obj.sub.getit(9).SuB.getit(10);');
  12712. ConvertProgram;
  12713. CheckSource('TestClass_Method',
  12714. LinesToStr([ // statements
  12715. 'rtl.createClass(this,"TObject",null,function(){',
  12716. ' this.$init = function () {',
  12717. ' this.vI = 0;',
  12718. ' this.Sub = null;',
  12719. ' };',
  12720. ' this.$final = function () {',
  12721. ' this.Sub = undefined;',
  12722. ' };',
  12723. ' this.Create = function(){',
  12724. ' return this;',
  12725. ' };',
  12726. ' this.GetIt = function(Par){',
  12727. ' var Result = null;',
  12728. ' this.vI = Par + 3;',
  12729. ' Result = this.Sub;',
  12730. ' return Result;',
  12731. ' };',
  12732. '});',
  12733. 'this.Obj = null;'
  12734. ]),
  12735. LinesToStr([ // $mod.$main
  12736. '$mod.Obj = $mod.TObject.$create("Create");',
  12737. '$mod.Obj.GetIt(4);',
  12738. '$mod.Obj.Sub.Sub=null;',
  12739. '$mod.Obj.Sub.GetIt(5);',
  12740. '$mod.Obj.Sub.GetIt(6).Sub=null;',
  12741. '$mod.Obj.Sub.GetIt(7).GetIt(8);',
  12742. '$mod.Obj.Sub.GetIt(9).Sub.GetIt(10);'
  12743. ]));
  12744. end;
  12745. procedure TTestModule.TestClass_Implementation;
  12746. begin
  12747. StartUnit(false);
  12748. Add([
  12749. 'interface',
  12750. 'type',
  12751. ' TObject = class',
  12752. ' constructor Create;',
  12753. ' end;',
  12754. 'implementation',
  12755. 'type',
  12756. ' TIntClass = class',
  12757. ' constructor Create; reintroduce;',
  12758. ' class procedure DoGlob;',
  12759. ' end;',
  12760. 'constructor tintclass.create;',
  12761. 'begin',
  12762. ' inherited;',
  12763. ' inherited create;',
  12764. ' doglob;',
  12765. 'end;',
  12766. 'class procedure tintclass.doglob;',
  12767. 'begin',
  12768. 'end;',
  12769. 'constructor tobject.create;',
  12770. 'var',
  12771. ' iC: tintclass;',
  12772. 'begin',
  12773. ' ic:=tintclass.create;',
  12774. ' tintclass.doglob;',
  12775. ' ic.doglob;',
  12776. 'end;',
  12777. 'initialization',
  12778. ' tintclass.doglob;',
  12779. '']);
  12780. ConvertUnit;
  12781. CheckSource('TestClass_Implementation',
  12782. LinesToStr([ // statements
  12783. 'var $impl = $mod.$impl;',
  12784. 'rtl.createClass(this, "TObject", null, function () {',
  12785. ' this.$init = function () {',
  12786. ' };',
  12787. ' this.$final = function () {',
  12788. ' };',
  12789. ' this.Create = function () {',
  12790. ' var iC = null;',
  12791. ' iC = $impl.TIntClass.$create("Create$1");',
  12792. ' $impl.TIntClass.DoGlob();',
  12793. ' iC.$class.DoGlob();',
  12794. ' return this;',
  12795. ' };',
  12796. '});',
  12797. '']),
  12798. LinesToStr([ // $mod.$main
  12799. '$impl.TIntClass.DoGlob();',
  12800. '']),
  12801. LinesToStr([
  12802. 'rtl.createClass($impl, "TIntClass", $mod.TObject, function () {',
  12803. ' this.Create$1 = function () {',
  12804. ' $mod.TObject.Create.call(this);',
  12805. ' $mod.TObject.Create.call(this);',
  12806. ' this.$class.DoGlob();',
  12807. ' return this;',
  12808. ' };',
  12809. ' this.DoGlob = function () {',
  12810. ' };',
  12811. '});',
  12812. '']));
  12813. end;
  12814. procedure TTestModule.TestClass_Inheritance;
  12815. begin
  12816. StartProgram(false);
  12817. Add('type');
  12818. Add(' TObject = class');
  12819. Add(' public');
  12820. Add(' constructor Create;');
  12821. Add(' end;');
  12822. Add(' TClassA = class');
  12823. Add(' end;');
  12824. Add(' TClassB = class(TObject)');
  12825. Add(' procedure ProcB;');
  12826. Add(' end;');
  12827. Add('constructor tobject.create; begin end;');
  12828. Add('procedure tclassb.procb; begin end;');
  12829. Add('var');
  12830. Add(' oO: TObject;');
  12831. Add(' oA: TClassA;');
  12832. Add(' oB: TClassB;');
  12833. Add('begin');
  12834. Add(' oO:=tobject.Create;');
  12835. Add(' oA:=tclassa.Create;');
  12836. Add(' ob:=tclassb.Create;');
  12837. Add(' if oo is tclassa then ;');
  12838. Add(' ob:=oo as tclassb;');
  12839. Add(' (oo as tclassb).procb;');
  12840. ConvertProgram;
  12841. CheckSource('TestClass_Inheritance',
  12842. LinesToStr([ // statements
  12843. 'rtl.createClass(this,"TObject",null,function(){',
  12844. ' this.$init = function () {',
  12845. ' };',
  12846. ' this.$final = function () {',
  12847. ' };',
  12848. ' this.Create = function () {',
  12849. ' return this;',
  12850. ' };',
  12851. '});',
  12852. 'rtl.createClass(this,"TClassA",this.TObject,function(){',
  12853. '});',
  12854. 'rtl.createClass(this,"TClassB",this.TObject,function(){',
  12855. ' this.ProcB = function () {',
  12856. ' };',
  12857. '});',
  12858. 'this.oO = null;',
  12859. 'this.oA = null;',
  12860. 'this.oB = null;'
  12861. ]),
  12862. LinesToStr([ // $mod.$main
  12863. '$mod.oO = $mod.TObject.$create("Create");',
  12864. '$mod.oA = $mod.TClassA.$create("Create");',
  12865. '$mod.oB = $mod.TClassB.$create("Create");',
  12866. 'if ($mod.TClassA.isPrototypeOf($mod.oO));',
  12867. '$mod.oB = rtl.as($mod.oO, $mod.TClassB);',
  12868. 'rtl.as($mod.oO, $mod.TClassB).ProcB();'
  12869. ]));
  12870. end;
  12871. procedure TTestModule.TestClass_TypeAlias;
  12872. begin
  12873. StartProgram(false);
  12874. Add([
  12875. '{$interfaces corba}',
  12876. 'type',
  12877. ' IObject = interface',
  12878. ' end;',
  12879. ' IBird = type IObject;',
  12880. ' TObject = class',
  12881. ' end;',
  12882. ' TBird = type TObject;',
  12883. 'var',
  12884. ' oObj: TObject;',
  12885. ' oBird: TBird;',
  12886. ' IntfObj: IObject;',
  12887. ' IntfBird: IBird;',
  12888. 'begin',
  12889. ' oObj:=oBird;',
  12890. '']);
  12891. ConvertProgram;
  12892. CheckSource('TestClass_TypeAlias',
  12893. LinesToStr([ // statements
  12894. 'rtl.createInterface(this, "IObject", "{B92D5841-6F2A-306A-8000-000000000000}", [], null);',
  12895. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-387B-AE88-F10981585074}", [], this.IObject);',
  12896. 'rtl.createClass(this, "TObject", null, function () {',
  12897. ' this.$init = function () {',
  12898. ' };',
  12899. ' this.$final = function () {',
  12900. ' };',
  12901. '});',
  12902. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  12903. '});',
  12904. 'this.oObj = null;',
  12905. 'this.oBird = null;',
  12906. 'this.IntfObj = null;',
  12907. 'this.IntfBird = null;',
  12908. '']),
  12909. LinesToStr([ // $mod.$main
  12910. '$mod.oObj = $mod.oBird;',
  12911. '']));
  12912. end;
  12913. procedure TTestModule.TestClass_AbstractMethod;
  12914. begin
  12915. StartProgram(false);
  12916. Add('type');
  12917. Add(' TObject = class');
  12918. Add(' public');
  12919. Add(' procedure DoIt; virtual; abstract;');
  12920. Add(' end;');
  12921. Add('begin');
  12922. ConvertProgram;
  12923. CheckSource('TestClass_AbstractMethod',
  12924. LinesToStr([ // statements
  12925. 'rtl.createClass(this,"TObject",null,function(){',
  12926. ' this.$init = function () {',
  12927. ' };',
  12928. ' this.$final = function () {',
  12929. ' };',
  12930. '});'
  12931. ]),
  12932. LinesToStr([ // this.$main
  12933. ''
  12934. ]));
  12935. end;
  12936. procedure TTestModule.TestClass_CallInherited_ProcNoParams;
  12937. begin
  12938. StartProgram(false);
  12939. Add([
  12940. 'type',
  12941. ' TObject = class',
  12942. ' procedure DoAbstract; virtual; abstract;',
  12943. ' procedure DoVirtual; virtual;',
  12944. ' procedure DoIt;',
  12945. ' end;',
  12946. ' TA = class',
  12947. ' procedure doabstract; override;',
  12948. ' procedure dovirtual; override;',
  12949. ' procedure DoSome;',
  12950. ' end;',
  12951. 'procedure tobject.dovirtual;',
  12952. 'begin',
  12953. ' inherited; // call non existing ancestor -> ignore silently',
  12954. 'end;',
  12955. 'procedure tobject.doit;',
  12956. 'begin',
  12957. 'end;',
  12958. 'procedure ta.doabstract;',
  12959. 'begin',
  12960. ' inherited dovirtual; // call TObject.DoVirtual',
  12961. 'end;',
  12962. 'procedure ta.dovirtual;',
  12963. 'begin',
  12964. ' inherited; // call TObject.DoVirtual',
  12965. ' inherited dovirtual; // call TObject.DoVirtual',
  12966. ' inherited dovirtual(); // call TObject.DoVirtual',
  12967. ' doit;',
  12968. ' doit();',
  12969. 'end;',
  12970. 'procedure ta.dosome;',
  12971. 'begin',
  12972. ' inherited; // call non existing ancestor method -> silently ignore',
  12973. 'end;',
  12974. 'begin']);
  12975. ConvertProgram;
  12976. CheckSource('TestClass_CallInherited_ProcNoParams',
  12977. LinesToStr([ // statements
  12978. 'rtl.createClass(this,"TObject",null,function(){',
  12979. ' this.$init = function () {',
  12980. ' };',
  12981. ' this.$final = function () {',
  12982. ' };',
  12983. ' this.DoVirtual = function () {',
  12984. ' };',
  12985. ' this.DoIt = function () {',
  12986. ' };',
  12987. '});',
  12988. 'rtl.createClass(this, "TA", this.TObject, function () {',
  12989. ' this.DoAbstract = function () {',
  12990. ' $mod.TObject.DoVirtual.call(this);',
  12991. ' };',
  12992. ' this.DoVirtual = function () {',
  12993. ' $mod.TObject.DoVirtual.call(this);',
  12994. ' $mod.TObject.DoVirtual.call(this);',
  12995. ' $mod.TObject.DoVirtual.call(this);',
  12996. ' this.DoIt();',
  12997. ' this.DoIt();',
  12998. ' };',
  12999. ' this.DoSome = function () {',
  13000. ' };',
  13001. '});'
  13002. ]),
  13003. LinesToStr([ // this.$main
  13004. ''
  13005. ]));
  13006. end;
  13007. procedure TTestModule.TestClass_CallInherited_WithParams;
  13008. begin
  13009. StartProgram(false);
  13010. Add([
  13011. 'type',
  13012. ' TObject = class',
  13013. ' procedure DoAbstract(pA: longint; pB: longint = 0); virtual; abstract;',
  13014. ' procedure DoVirtual(pA: longint; pB: longint = 0); virtual;',
  13015. ' procedure DoIt(pA: longint; pB: longint = 0);',
  13016. ' procedure DoIt2(pA: longint = 1; pB: longint = 2);',
  13017. ' function GetIt(pA: longint = 1; pB: longint = 2): longint;',
  13018. ' end;',
  13019. ' TClassA = class',
  13020. ' procedure DoAbstract(pA: longint; pB: longint = 0); override;',
  13021. ' procedure DoVirtual(pA: longint; pB: longint = 0); override;',
  13022. ' function GetIt(pA: longint = 1; pB: longint = 2): longint;',
  13023. ' end;',
  13024. 'procedure tobject.dovirtual(pa: longint; pb: longint = 0);',
  13025. 'begin',
  13026. 'end;',
  13027. 'procedure tobject.doit(pa: longint; pb: longint = 0);',
  13028. 'begin',
  13029. 'end;',
  13030. 'procedure tobject.doit2(pa: longint; pb: longint = 0);',
  13031. 'begin',
  13032. 'end;',
  13033. 'function tobject.getit(pa: longint; pb: longint = 0): longint;',
  13034. 'begin',
  13035. 'end;',
  13036. 'procedure tclassa.doabstract(pa: longint; pb: longint = 0);',
  13037. 'begin',
  13038. ' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)',
  13039. ' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)',
  13040. 'end;',
  13041. 'procedure tclassa.dovirtual(pa: longint; pb: longint = 0);',
  13042. 'begin',
  13043. ' inherited; // call TObject.DoVirtual(pA,pB)',
  13044. ' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)',
  13045. ' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)',
  13046. ' doit(pa,pb);',
  13047. ' doit(pa);',
  13048. ' doit2(pa);',
  13049. ' doit2;',
  13050. 'end;',
  13051. 'function tclassa.getit(pa: longint; pb: longint = 0): longint;',
  13052. 'begin',
  13053. ' pa:=inherited;',
  13054. 'end;',
  13055. 'begin']);
  13056. ConvertProgram;
  13057. CheckSource('TestClass_CallInherited_WithParams',
  13058. LinesToStr([ // statements
  13059. 'rtl.createClass(this,"TObject",null,function(){',
  13060. ' this.$init = function () {',
  13061. ' };',
  13062. ' this.$final = function () {',
  13063. ' };',
  13064. ' this.DoVirtual = function (pA,pB) {',
  13065. ' };',
  13066. ' this.DoIt = function (pA,pB) {',
  13067. ' };',
  13068. ' this.DoIt2 = function (pA,pB) {',
  13069. ' };',
  13070. ' this.GetIt = function (pA, pB) {',
  13071. ' var Result = 0;',
  13072. ' return Result;',
  13073. ' };',
  13074. '});',
  13075. 'rtl.createClass(this, "TClassA", this.TObject, function () {',
  13076. ' this.DoAbstract = function (pA,pB) {',
  13077. ' $mod.TObject.DoVirtual.call(this,pA,pB);',
  13078. ' $mod.TObject.DoVirtual.call(this,pA,0);',
  13079. ' };',
  13080. ' this.DoVirtual = function (pA,pB) {',
  13081. ' $mod.TObject.DoVirtual.apply(this, arguments);',
  13082. ' $mod.TObject.DoVirtual.call(this,pA,pB);',
  13083. ' $mod.TObject.DoVirtual.call(this,pA,0);',
  13084. ' this.DoIt(pA,pB);',
  13085. ' this.DoIt(pA,0);',
  13086. ' this.DoIt2(pA,2);',
  13087. ' this.DoIt2(1,2);',
  13088. ' };',
  13089. ' this.GetIt$1 = function (pA, pB) {',
  13090. ' var Result = 0;',
  13091. ' pA = $mod.TObject.GetIt.apply(this, arguments);',
  13092. ' return Result;',
  13093. ' };',
  13094. '});'
  13095. ]),
  13096. LinesToStr([ // this.$main
  13097. ''
  13098. ]));
  13099. end;
  13100. procedure TTestModule.TestClasS_CallInheritedConstructor;
  13101. begin
  13102. StartProgram(false);
  13103. Add('type');
  13104. Add(' TObject = class');
  13105. Add(' constructor Create; virtual;');
  13106. Add(' constructor CreateWithB(b: boolean);');
  13107. Add(' end;');
  13108. Add(' TA = class');
  13109. Add(' constructor Create; override;');
  13110. Add(' constructor CreateWithC(c: char);');
  13111. Add(' procedure DoIt;');
  13112. Add(' class function DoSome: TObject;');
  13113. Add(' end;');
  13114. Add('constructor tobject.create;');
  13115. Add('begin');
  13116. Add(' inherited; // call non existing ancestor -> ignore silently');
  13117. Add('end;');
  13118. Add('constructor tobject.createwithb(b: boolean);');
  13119. Add('begin');
  13120. Add(' inherited; // call non existing ancestor -> ignore silently');
  13121. Add(' create; // normal call');
  13122. Add('end;');
  13123. Add('constructor ta.create;');
  13124. Add('begin');
  13125. Add(' inherited; // normal call TObject.Create');
  13126. Add(' inherited create; // normal call TObject.Create');
  13127. Add(' inherited createwithb(false); // normal call TObject.CreateWithB');
  13128. Add('end;');
  13129. Add('constructor ta.createwithc(c: char);');
  13130. Add('begin');
  13131. Add(' inherited create; // call TObject.Create');
  13132. Add(' inherited createwithb(true); // call TObject.CreateWithB');
  13133. Add(' doit;');
  13134. Add(' doit();');
  13135. Add(' dosome;');
  13136. Add('end;');
  13137. Add('procedure ta.doit;');
  13138. Add('begin');
  13139. Add(' create; // normal call');
  13140. Add(' createwithb(false); // normal call');
  13141. Add(' createwithc(''c''); // normal call');
  13142. Add('end;');
  13143. Add('class function ta.dosome: TObject;');
  13144. Add('begin');
  13145. Add(' Result:=create; // constructor');
  13146. Add(' Result:=createwithb(true); // constructor');
  13147. Add(' Result:=createwithc(''c''); // constructor');
  13148. Add('end;');
  13149. Add('begin');
  13150. ConvertProgram;
  13151. CheckSource('TestClass_CallInheritedConstructor',
  13152. LinesToStr([ // statements
  13153. 'rtl.createClass(this,"TObject",null,function(){',
  13154. ' this.$init = function () {',
  13155. ' };',
  13156. ' this.$final = function () {',
  13157. ' };',
  13158. ' this.Create = function () {',
  13159. ' return this;',
  13160. ' };',
  13161. ' this.CreateWithB = function (b) {',
  13162. ' this.Create();',
  13163. ' return this;',
  13164. ' };',
  13165. '});',
  13166. 'rtl.createClass(this, "TA", this.TObject, function () {',
  13167. ' this.Create = function () {',
  13168. ' $mod.TObject.Create.call(this);',
  13169. ' $mod.TObject.Create.call(this);',
  13170. ' $mod.TObject.CreateWithB.call(this, false);',
  13171. ' return this;',
  13172. ' };',
  13173. ' this.CreateWithC = function (c) {',
  13174. ' $mod.TObject.Create.call(this);',
  13175. ' $mod.TObject.CreateWithB.call(this, true);',
  13176. ' this.DoIt();',
  13177. ' this.DoIt();',
  13178. ' this.$class.DoSome();',
  13179. ' return this;',
  13180. ' };',
  13181. ' this.DoIt = function () {',
  13182. ' this.Create();',
  13183. ' this.CreateWithB(false);',
  13184. ' this.CreateWithC("c");',
  13185. ' };',
  13186. ' this.DoSome = function () {',
  13187. ' var Result = null;',
  13188. ' Result = this.$create("Create");',
  13189. ' Result = this.$create("CreateWithB", [true]);',
  13190. ' Result = this.$create("CreateWithC", ["c"]);',
  13191. ' return Result;',
  13192. ' };',
  13193. '});'
  13194. ]),
  13195. LinesToStr([ // this.$main
  13196. ''
  13197. ]));
  13198. end;
  13199. procedure TTestModule.TestClass_ClassVar_Assign;
  13200. begin
  13201. StartProgram(false);
  13202. Add([
  13203. 'type',
  13204. ' TObject = class',
  13205. ' public',
  13206. ' class var vI: longint;',
  13207. ' class var Sub: TObject;',
  13208. ' constructor Create;',
  13209. ' class function GetIt(var Par: longint): tobject;',
  13210. ' end;',
  13211. 'constructor tobject.create;',
  13212. 'begin',
  13213. ' vi:=vi+1;',
  13214. ' Self.vi:=Self.vi+1;',
  13215. ' inc(vi);',
  13216. 'end;',
  13217. 'class function tobject.getit(var par: longint): tobject;',
  13218. 'begin',
  13219. ' vi:=vi+3;',
  13220. ' Self.vi:=Self.vi+4;',
  13221. ' inc(vi);',
  13222. ' Result:=self.sub;',
  13223. ' GetIt(vi);',
  13224. 'end;',
  13225. 'var Obj: tobject;',
  13226. 'begin',
  13227. ' obj:=tobject.create;',
  13228. ' tobject.vi:=3;',
  13229. ' if tobject.vi=4 then ;',
  13230. ' tobject.sub:=nil;',
  13231. ' obj.sub:=nil;',
  13232. ' obj.sub.sub:=nil;']);
  13233. ConvertProgram;
  13234. CheckSource('TestClass_ClassVar_Assign',
  13235. LinesToStr([ // statements
  13236. 'rtl.createClass(this,"TObject",null,function(){',
  13237. ' this.vI = 0;',
  13238. ' this.Sub = null;',
  13239. ' this.$init = function () {',
  13240. ' };',
  13241. ' this.$final = function () {',
  13242. ' };',
  13243. ' this.Create = function(){',
  13244. ' $mod.TObject.vI = this.vI+1;',
  13245. ' $mod.TObject.vI = this.vI+1;',
  13246. ' $mod.TObject.vI += 1;',
  13247. ' return this;',
  13248. ' };',
  13249. ' this.GetIt = function(Par){',
  13250. ' var Result = null;',
  13251. ' $mod.TObject.vI = this.vI + 3;',
  13252. ' $mod.TObject.vI = this.vI + 4;',
  13253. ' $mod.TObject.vI += 1;',
  13254. ' Result = this.Sub;',
  13255. ' this.GetIt({',
  13256. ' p: $mod.TObject,',
  13257. ' get: function () {',
  13258. ' return this.p.vI;',
  13259. ' },',
  13260. ' set: function (v) {',
  13261. ' this.p.vI = v;',
  13262. ' }',
  13263. ' });',
  13264. ' return Result;',
  13265. ' };',
  13266. '});',
  13267. 'this.Obj = null;'
  13268. ]),
  13269. LinesToStr([ // $mod.$main
  13270. '$mod.Obj = $mod.TObject.$create("Create");',
  13271. '$mod.TObject.vI = 3;',
  13272. 'if ($mod.TObject.vI === 4);',
  13273. '$mod.TObject.Sub=null;',
  13274. '$mod.TObject.Sub=null;',
  13275. '$mod.TObject.Sub=null;',
  13276. '']));
  13277. end;
  13278. procedure TTestModule.TestClass_CallClassMethod;
  13279. begin
  13280. StartProgram(false);
  13281. Add('type');
  13282. Add(' TObject = class');
  13283. Add(' public');
  13284. Add(' class var vI: longint;');
  13285. Add(' class var Sub: TObject;');
  13286. Add(' constructor Create;');
  13287. Add(' function GetMore(Par: longint): longint;');
  13288. Add(' class function GetIt(Par: longint): tobject;');
  13289. Add(' end;');
  13290. Add('constructor tobject.create;');
  13291. Add('begin');
  13292. Add(' sub:=getit(3);');
  13293. Add(' vi:=getmore(4);');
  13294. Add(' sub:=Self.getit(5);');
  13295. Add(' vi:=Self.getmore(6);');
  13296. Add('end;');
  13297. Add('function tobject.getmore(par: longint): longint;');
  13298. Add('begin');
  13299. Add(' sub:=getit(11);');
  13300. Add(' vi:=getmore(12);');
  13301. Add(' sub:=self.getit(13);');
  13302. Add(' vi:=self.getmore(14);');
  13303. Add('end;');
  13304. Add('class function tobject.getit(par: longint): tobject;');
  13305. Add('begin');
  13306. Add(' sub:=getit(21);');
  13307. Add(' vi:=sub.getmore(22);');
  13308. Add(' sub:=self.getit(23);');
  13309. Add(' vi:=self.sub.getmore(24);');
  13310. Add('end;');
  13311. Add('var Obj: tobject;');
  13312. Add('begin');
  13313. Add(' obj:=tobject.create;');
  13314. Add(' tobject.getit(5);');
  13315. Add(' obj.getit(6);');
  13316. Add(' obj.sub.getit(7);');
  13317. Add(' obj.sub.getit(8).SUB:=nil;');
  13318. Add(' obj.sub.getit(9).GETIT(10);');
  13319. Add(' obj.sub.getit(11).SuB.getit(12);');
  13320. ConvertProgram;
  13321. CheckSource('TestClass_CallClassMethod',
  13322. LinesToStr([ // statements
  13323. 'rtl.createClass(this,"TObject",null,function(){',
  13324. ' this.vI = 0;',
  13325. ' this.Sub = null;',
  13326. ' this.$init = function () {',
  13327. ' };',
  13328. ' this.$final = function () {',
  13329. ' };',
  13330. ' this.Create = function(){',
  13331. ' $mod.TObject.Sub = this.$class.GetIt(3);',
  13332. ' $mod.TObject.vI = this.GetMore(4);',
  13333. ' $mod.TObject.Sub = this.$class.GetIt(5);',
  13334. ' $mod.TObject.vI = this.GetMore(6);',
  13335. ' return this;',
  13336. ' };',
  13337. ' this.GetMore = function(Par){',
  13338. ' var Result = 0;',
  13339. ' $mod.TObject.Sub = this.$class.GetIt(11);',
  13340. ' $mod.TObject.vI = this.GetMore(12);',
  13341. ' $mod.TObject.Sub = this.$class.GetIt(13);',
  13342. ' $mod.TObject.vI = this.GetMore(14);',
  13343. ' return Result;',
  13344. ' };',
  13345. ' this.GetIt = function(Par){',
  13346. ' var Result = null;',
  13347. ' $mod.TObject.Sub = this.GetIt(21);',
  13348. ' $mod.TObject.vI = this.Sub.GetMore(22);',
  13349. ' $mod.TObject.Sub = this.GetIt(23);',
  13350. ' $mod.TObject.vI = this.Sub.GetMore(24);',
  13351. ' return Result;',
  13352. ' };',
  13353. '});',
  13354. 'this.Obj = null;'
  13355. ]),
  13356. LinesToStr([ // $mod.$main
  13357. '$mod.Obj = $mod.TObject.$create("Create");',
  13358. '$mod.TObject.GetIt(5);',
  13359. '$mod.Obj.$class.GetIt(6);',
  13360. '$mod.Obj.Sub.$class.GetIt(7);',
  13361. '$mod.TObject.Sub=null;',
  13362. '$mod.Obj.Sub.$class.GetIt(9).$class.GetIt(10);',
  13363. '$mod.Obj.Sub.$class.GetIt(11).Sub.$class.GetIt(12);',
  13364. '']));
  13365. end;
  13366. procedure TTestModule.TestClass_CallClassMethodStatic;
  13367. begin
  13368. StartProgram(false);
  13369. Add([
  13370. 'type',
  13371. ' TObject = class',
  13372. ' public',
  13373. ' class function Fly: tobject; static;',
  13374. ' end;',
  13375. 'class function tobject.Fly: tobject;',
  13376. 'begin',
  13377. ' Result.Fly;',
  13378. ' Result.Fly();',
  13379. ' Fly;',
  13380. ' Fly();',
  13381. ' Fly.Fly;',
  13382. ' Fly.Fly();',
  13383. 'end;',
  13384. 'var Obj: tobject;',
  13385. 'begin',
  13386. ' obj.Fly;',
  13387. ' obj.Fly();',
  13388. ' with obj do begin',
  13389. ' Fly;',
  13390. ' Fly();',
  13391. ' end;',
  13392. '']);
  13393. ConvertProgram;
  13394. CheckSource('TestClass_CallClassMethodStatic',
  13395. LinesToStr([ // statements
  13396. 'rtl.createClass(this, "TObject", null, function () {',
  13397. ' this.$init = function () {',
  13398. ' };',
  13399. ' this.$final = function () {',
  13400. ' };',
  13401. ' this.Fly = function () {',
  13402. ' var Result = null;',
  13403. ' $mod.TObject.Fly();',
  13404. ' $mod.TObject.Fly();',
  13405. ' $mod.TObject.Fly();',
  13406. ' $mod.TObject.Fly();',
  13407. ' $mod.TObject.Fly();',
  13408. ' $mod.TObject.Fly();',
  13409. ' return Result;',
  13410. ' };',
  13411. '});',
  13412. 'this.Obj = null;'
  13413. ]),
  13414. LinesToStr([ // $mod.$main
  13415. '$mod.TObject.Fly();',
  13416. '$mod.TObject.Fly();',
  13417. 'var $with = $mod.Obj;',
  13418. '$with.Fly();',
  13419. '$with.Fly();',
  13420. '']));
  13421. end;
  13422. procedure TTestModule.TestClass_Property;
  13423. begin
  13424. StartProgram(false);
  13425. Add('type');
  13426. Add(' TObject = class');
  13427. Add(' Fx: longint;');
  13428. Add(' Fy: longint;');
  13429. Add(' function GetInt: longint;');
  13430. Add(' procedure SetInt(Value: longint);');
  13431. Add(' procedure DoIt;');
  13432. Add(' property IntA: longint read Fx write Fy;');
  13433. Add(' property IntB: longint read GetInt write SetInt;');
  13434. Add(' end;');
  13435. Add('function tobject.getint: longint;');
  13436. Add('begin');
  13437. Add(' result:=fx;');
  13438. Add('end;');
  13439. Add('procedure tobject.setint(value: longint);');
  13440. Add('begin');
  13441. Add(' if value=fy then exit;');
  13442. Add(' fy:=value;');
  13443. Add('end;');
  13444. Add('procedure tobject.doit;');
  13445. Add('begin');
  13446. Add(' IntA:=IntA+1;');
  13447. Add(' Self.IntA:=Self.IntA+1;');
  13448. Add(' IntB:=IntB+1;');
  13449. Add(' Self.IntB:=Self.IntB+1;');
  13450. Add('end;');
  13451. Add('var Obj: tobject;');
  13452. Add('begin');
  13453. Add(' obj.inta:=obj.inta+1;');
  13454. Add(' if obj.intb=2 then;');
  13455. Add(' obj.intb:=obj.intb+2;');
  13456. Add(' obj.setint(obj.inta);');
  13457. ConvertProgram;
  13458. CheckSource('TestClass_Property',
  13459. LinesToStr([ // statements
  13460. 'rtl.createClass(this, "TObject", null, function () {',
  13461. ' this.$init = function () {',
  13462. ' this.Fx = 0;',
  13463. ' this.Fy = 0;',
  13464. ' };',
  13465. ' this.$final = function () {',
  13466. ' };',
  13467. ' this.GetInt = function () {',
  13468. ' var Result = 0;',
  13469. ' Result = this.Fx;',
  13470. ' return Result;',
  13471. ' };',
  13472. ' this.SetInt = function (Value) {',
  13473. ' if (Value === this.Fy) return;',
  13474. ' this.Fy = Value;',
  13475. ' };',
  13476. ' this.DoIt = function () {',
  13477. ' this.Fy = this.Fx + 1;',
  13478. ' this.Fy = this.Fx + 1;',
  13479. ' this.SetInt(this.GetInt() + 1);',
  13480. ' this.SetInt(this.GetInt() + 1);',
  13481. ' };',
  13482. '});',
  13483. 'this.Obj = null;'
  13484. ]),
  13485. LinesToStr([ // $mod.$main
  13486. '$mod.Obj.Fy = $mod.Obj.Fx + 1;',
  13487. 'if ($mod.Obj.GetInt() === 2);',
  13488. '$mod.Obj.SetInt($mod.Obj.GetInt() + 2);',
  13489. '$mod.Obj.SetInt($mod.Obj.Fx);'
  13490. ]));
  13491. end;
  13492. procedure TTestModule.TestClass_Property_ClassMethod;
  13493. begin
  13494. StartProgram(false);
  13495. Add([
  13496. 'type',
  13497. ' TObject = class',
  13498. ' class var Fx: longint;',
  13499. ' class var Fy: longint;',
  13500. ' class function GetInt: longint;',
  13501. ' class procedure SetInt(Value: longint);',
  13502. ' end;',
  13503. ' TBird = class',
  13504. ' class procedure DoIt;',
  13505. ' class property IntA: longint read Fx write Fy;',
  13506. ' class property IntB: longint read GetInt write SetInt;',
  13507. ' end;',
  13508. 'class function tobject.getint: longint;',
  13509. 'begin',
  13510. ' result:=fx;',
  13511. 'end;',
  13512. 'class procedure tobject.setint(value: longint);',
  13513. 'begin',
  13514. 'end;',
  13515. 'class procedure tbird.doit;',
  13516. 'begin',
  13517. ' FX:=3;',
  13518. ' IntA:=IntA+1;',
  13519. ' Self.IntA:=Self.IntA+1;',
  13520. ' IntB:=IntB+1;',
  13521. ' Self.IntB:=Self.IntB+1;',
  13522. ' with Self do begin',
  13523. ' FX:=11;',
  13524. ' IntA:=IntA+12;',
  13525. ' IntB:=IntB+13;',
  13526. ' end;',
  13527. 'end;',
  13528. 'var Obj: tbird;',
  13529. 'begin',
  13530. ' tbird.fx:=tbird.fx+1;',
  13531. ' tbird.inta:=tbird.inta+1;',
  13532. ' if tbird.intb=2 then;',
  13533. ' tbird.intb:=tbird.intb+2;',
  13534. ' tbird.setint(tbird.inta);',
  13535. ' obj.inta:=obj.inta+1;',
  13536. ' if obj.intb=2 then;',
  13537. ' obj.intb:=obj.intb+2;',
  13538. ' obj.setint(obj.inta);',
  13539. ' with Tbird do begin',
  13540. ' FX:=FY+1;',
  13541. ' inta:=inta+2;',
  13542. ' intb:=intb+3;',
  13543. ' end;',
  13544. ' with Obj do begin',
  13545. ' FX:=FY+1;',
  13546. ' inta:=inta+2;',
  13547. ' intb:=intb+3;',
  13548. ' end;',
  13549. '']);
  13550. ConvertProgram;
  13551. CheckSource('TestClass_Property_ClassMethod',
  13552. LinesToStr([ // statements
  13553. 'rtl.createClass(this, "TObject", null, function () {',
  13554. ' this.Fx = 0;',
  13555. ' this.Fy = 0;',
  13556. ' this.$init = function () {',
  13557. ' };',
  13558. ' this.$final = function () {',
  13559. ' };',
  13560. ' this.GetInt = function () {',
  13561. ' var Result = 0;',
  13562. ' Result = this.Fx;',
  13563. ' return Result;',
  13564. ' };',
  13565. ' this.SetInt = function (Value) {',
  13566. ' };',
  13567. '});',
  13568. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  13569. ' this.DoIt = function () {',
  13570. ' $mod.TObject.Fx = 3;',
  13571. ' $mod.TObject.Fy = this.Fx + 1;',
  13572. ' $mod.TObject.Fy = this.Fx + 1;',
  13573. ' this.SetInt(this.GetInt() + 1);',
  13574. ' this.SetInt(this.GetInt() + 1);',
  13575. ' $mod.TObject.Fx = 11;',
  13576. ' $mod.TObject.Fy = this.Fx + 12;',
  13577. ' this.SetInt(this.GetInt() + 13);',
  13578. ' };',
  13579. '});',
  13580. 'this.Obj = null;'
  13581. ]),
  13582. LinesToStr([ // $mod.$main
  13583. '$mod.TObject.Fx = $mod.TBird.Fx + 1;',
  13584. '$mod.TObject.Fy = $mod.TBird.Fx + 1;',
  13585. 'if ($mod.TBird.GetInt() === 2);',
  13586. '$mod.TBird.SetInt($mod.TBird.GetInt() + 2);',
  13587. '$mod.TBird.SetInt($mod.TBird.Fx);',
  13588. '$mod.TObject.Fy = $mod.Obj.Fx + 1;',
  13589. 'if ($mod.Obj.$class.GetInt() === 2);',
  13590. '$mod.Obj.$class.SetInt($mod.Obj.$class.GetInt() + 2);',
  13591. '$mod.Obj.$class.SetInt($mod.Obj.Fx);',
  13592. 'var $with = $mod.TBird;',
  13593. '$mod.TObject.Fx = $with.Fy + 1;',
  13594. '$mod.TObject.Fy = $with.Fx + 2;',
  13595. '$with.SetInt($with.GetInt() + 3);',
  13596. 'var $with1 = $mod.Obj;',
  13597. '$mod.TObject.Fx = $with1.Fy + 1;',
  13598. '$mod.TObject.Fy = $with1.Fx + 2;',
  13599. '$with1.$class.SetInt($with1.$class.GetInt() + 3);',
  13600. '']));
  13601. end;
  13602. procedure TTestModule.TestClass_Property_ClassMethodStatic;
  13603. begin
  13604. StartProgram(false);
  13605. Add([
  13606. 'type',
  13607. ' TObject = class',
  13608. ' class function GetInt: longint; static;',
  13609. ' class procedure SetInt(Value: longint); static;',
  13610. ' class function GetItems(Index: word): longint; static;',
  13611. ' class procedure SetItems(Index: word; const Value: longint); static;',
  13612. ' end;',
  13613. ' TBird = class',
  13614. ' class procedure Fly;',
  13615. ' class property IntA: longint read GetInt write SetInt;',
  13616. ' class property Items[Index: word]: longint read GetItems write SetItems;',
  13617. ' end;',
  13618. 'class function tobject.getint: longint;',
  13619. 'begin',
  13620. 'end;',
  13621. 'class procedure tobject.setint(value: longint);',
  13622. 'begin',
  13623. 'end;',
  13624. 'class function tobject.GetItems(Index: word): longint;',
  13625. 'begin',
  13626. 'end;',
  13627. 'class procedure TObject.SetItems(Index: word; const Value: longint);',
  13628. 'begin',
  13629. 'end;',
  13630. 'class procedure tbird.fly;',
  13631. 'var w: longint;',
  13632. 'begin',
  13633. ' inta:=inta+51;',
  13634. ' w:=items[52];',
  13635. ' items[53]:=54;',
  13636. 'end;',
  13637. 'var Obj: tbird;',
  13638. ' i: longint;',
  13639. 'begin',
  13640. ' tbird.inta:=tbird.inta+1;',
  13641. ' i:=tbird.items[2];',
  13642. ' tbird.items[3]:=4;',
  13643. ' obj.inta:=obj.inta+11;',
  13644. ' i:=obj.items[12];',
  13645. ' obj.items[13]:=14;',
  13646. ' with Tbird do begin',
  13647. ' inta:=inta+21;',
  13648. ' i:=items[22];',
  13649. ' items[23]:=24;',
  13650. ' end;',
  13651. ' with Obj do begin',
  13652. ' inta:=inta+31;',
  13653. ' i:=items[32];',
  13654. ' items[33]:=34;',
  13655. ' end;',
  13656. '']);
  13657. ConvertProgram;
  13658. CheckSource('TestClass_Property_ClassMethod',
  13659. LinesToStr([ // statements
  13660. 'rtl.createClass(this, "TObject", null, function () {',
  13661. ' this.$init = function () {',
  13662. ' };',
  13663. ' this.$final = function () {',
  13664. ' };',
  13665. ' this.GetInt = function () {',
  13666. ' var Result = 0;',
  13667. ' return Result;',
  13668. ' };',
  13669. ' this.SetInt = function (Value) {',
  13670. ' };',
  13671. ' this.GetItems = function (Index) {',
  13672. ' var Result = 0;',
  13673. ' return Result;',
  13674. ' };',
  13675. ' this.SetItems = function (Index, Value) {',
  13676. ' };',
  13677. '});',
  13678. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  13679. ' this.Fly = function () {',
  13680. ' var w = 0;',
  13681. ' this.SetInt(this.GetInt() + 51);',
  13682. ' w = this.GetItems(52);',
  13683. ' this.SetItems(53, 54);',
  13684. ' };',
  13685. '});',
  13686. 'this.Obj = null;',
  13687. 'this.i = 0;',
  13688. '']),
  13689. LinesToStr([ // $mod.$main
  13690. '$mod.TObject.SetInt($mod.TObject.GetInt() + 1);',
  13691. '$mod.i = $mod.TObject.GetItems(2);',
  13692. '$mod.TObject.SetItems(3, 4);',
  13693. '$mod.TObject.SetInt($mod.TObject.GetInt() + 11);',
  13694. '$mod.i = $mod.TObject.GetItems(12);',
  13695. '$mod.TObject.SetItems(13, 14);',
  13696. 'var $with = $mod.TBird;',
  13697. '$with.SetInt($with.GetInt() + 21);',
  13698. '$mod.i = $with.GetItems(22);',
  13699. '$with.SetItems(23, 24);',
  13700. 'var $with1 = $mod.Obj;',
  13701. '$with1.SetInt($with1.GetInt() + 31);',
  13702. '$mod.i = $with1.GetItems(32);',
  13703. '$with1.SetItems(33, 34);',
  13704. '']));
  13705. end;
  13706. procedure TTestModule.TestClass_Property_Indexed;
  13707. begin
  13708. StartProgram(false);
  13709. Add([
  13710. 'type',
  13711. ' TObject = class',
  13712. ' FItems: array of longint;',
  13713. ' function GetItems(Index: longint): longint;',
  13714. ' procedure SetItems(Index: longint; Value: longint);',
  13715. ' procedure DoIt;',
  13716. ' property Items[Index: longint]: longint read getitems write setitems;',
  13717. ' end;',
  13718. 'function tobject.getitems(index: longint): longint;',
  13719. 'begin',
  13720. ' Result:=fitems[index];',
  13721. 'end;',
  13722. 'procedure tobject.setitems(index: longint; value: longint);',
  13723. 'begin',
  13724. ' fitems[index]:=value;',
  13725. 'end;',
  13726. 'procedure tobject.doit;',
  13727. 'begin',
  13728. ' items[1]:=2;',
  13729. ' items[3]:=items[4];',
  13730. ' self.items[5]:=self.items[6];',
  13731. ' items[items[7]]:=items[items[8]];',
  13732. 'end;',
  13733. 'var Obj: tobject;',
  13734. 'begin',
  13735. ' obj.Items[11]:=obj.Items[12];',
  13736. '']);
  13737. ConvertProgram;
  13738. CheckSource('TestClass_Property_Indexed',
  13739. LinesToStr([ // statements
  13740. 'rtl.createClass(this, "TObject", null, function () {',
  13741. ' this.$init = function () {',
  13742. ' this.FItems = [];',
  13743. ' };',
  13744. ' this.$final = function () {',
  13745. ' this.FItems = undefined;',
  13746. ' };',
  13747. ' this.GetItems = function (Index) {',
  13748. ' var Result = 0;',
  13749. ' Result = this.FItems[Index];',
  13750. ' return Result;',
  13751. ' };',
  13752. ' this.SetItems = function (Index, Value) {',
  13753. ' this.FItems[Index] = Value;',
  13754. ' };',
  13755. ' this.DoIt = function () {',
  13756. ' this.SetItems(1, 2);',
  13757. ' this.SetItems(3,this.GetItems(4));',
  13758. ' this.SetItems(5,this.GetItems(6));',
  13759. ' this.SetItems(this.GetItems(7), this.GetItems(this.GetItems(8)));',
  13760. ' };',
  13761. '});',
  13762. 'this.Obj = null;'
  13763. ]),
  13764. LinesToStr([ // $mod.$main
  13765. '$mod.Obj.SetItems(11,$mod.Obj.GetItems(12));'
  13766. ]));
  13767. end;
  13768. procedure TTestModule.TestClass_Property_IndexSpec;
  13769. begin
  13770. StartProgram(false);
  13771. Add([
  13772. 'type',
  13773. ' TEnum = (red, blue);',
  13774. ' TObject = class',
  13775. ' function GetIntBool(Index: longint): boolean; virtual; abstract;',
  13776. ' procedure SetIntBool(Index: longint; b: boolean); virtual; abstract;',
  13777. ' function GetEnumBool(Index: TEnum): boolean; virtual; abstract;',
  13778. ' procedure SetEnumBool(Index: TEnum; b: boolean); virtual; abstract;',
  13779. ' function GetStrIntBool(A: String; I: longint): boolean; virtual; abstract;',
  13780. ' procedure SetStrIntBool(A: String; I: longint; b: boolean); virtual; abstract;',
  13781. ' property B1: boolean index 1 read GetIntBool write SetIntBool;',
  13782. ' property B2: boolean index TEnum.blue read GetEnumBool write SetEnumBool;',
  13783. ' property B3: boolean index ord(red) read GetIntBool write SetIntBool;',
  13784. ' property I1[A: String]: boolean index ord(blue) read GetStrIntBool write SetStrIntBool;',
  13785. ' end;',
  13786. 'procedure DoIt(b: boolean); begin end;',
  13787. 'var',
  13788. ' o: TObject;',
  13789. 'begin',
  13790. ' o.B1:=o.B1;',
  13791. ' o.B2:=o.B2;',
  13792. ' o.B3:=o.B3;',
  13793. ' o.I1[''a'']:=o.I1[''b''];',
  13794. ' doit(o.b1);',
  13795. ' doit(o.b2);',
  13796. ' doit(o.i1[''c'']);',
  13797. '']);
  13798. ConvertProgram;
  13799. CheckSource('TestClass_Property_IndexSpec',
  13800. LinesToStr([ // statements
  13801. 'this.TEnum = {',
  13802. ' "0": "red",',
  13803. ' red: 0,',
  13804. ' "1": "blue",',
  13805. ' blue: 1',
  13806. '};',
  13807. 'rtl.createClass(this, "TObject", null, function () {',
  13808. ' this.$init = function () {',
  13809. ' };',
  13810. ' this.$final = function () {',
  13811. ' };',
  13812. '});',
  13813. 'this.DoIt = function (b) {',
  13814. '};',
  13815. 'this.o = null;',
  13816. '']),
  13817. LinesToStr([ // $mod.$main
  13818. '$mod.o.SetIntBool(1, $mod.o.GetIntBool(1));',
  13819. '$mod.o.SetEnumBool($mod.TEnum.blue, $mod.o.GetEnumBool($mod.TEnum.blue));',
  13820. '$mod.o.SetIntBool(0, $mod.o.GetIntBool(0));',
  13821. '$mod.o.SetStrIntBool("a", 1, $mod.o.GetStrIntBool("b", 1));',
  13822. '$mod.DoIt($mod.o.GetIntBool(1));',
  13823. '$mod.DoIt($mod.o.GetEnumBool($mod.TEnum.blue));',
  13824. '$mod.DoIt($mod.o.GetStrIntBool("c", 1));',
  13825. '']));
  13826. end;
  13827. procedure TTestModule.TestClass_PropertyOfTypeArray;
  13828. begin
  13829. StartProgram(false);
  13830. Add('type');
  13831. Add(' TArray = array of longint;');
  13832. Add(' TObject = class');
  13833. Add(' FItems: TArray;');
  13834. Add(' function GetItems: tarray;');
  13835. Add(' procedure SetItems(Value: tarray);');
  13836. Add(' property Items: tarray read getitems write setitems;');
  13837. Add(' procedure SetNumbers(const Value: tarray);');
  13838. Add(' property Numbers: tarray write setnumbers;');
  13839. Add(' end;');
  13840. Add('function tobject.getitems: tarray;');
  13841. Add('begin');
  13842. Add(' Result:=fitems;');
  13843. Add('end;');
  13844. Add('procedure tobject.setitems(value: tarray);');
  13845. Add('begin');
  13846. Add(' fitems:=value;');
  13847. Add(' fitems:=nil;');
  13848. Add(' Items:=nil;');
  13849. Add(' Items:=Items;');
  13850. Add(' Items[1]:=2;');
  13851. Add(' fitems[3]:=Items[4];');
  13852. Add(' Items[5]:=Items[6];');
  13853. Add(' Self.Items[7]:=8;');
  13854. Add(' Self.Items[9]:=Self.Items[10];');
  13855. Add(' Items[Items[11]]:=Items[Items[12]];');
  13856. Add('end;');
  13857. Add('procedure tobject.SetNumbers(const Value: tarray);');
  13858. Add('begin;');
  13859. Add(' Numbers:=nil;');
  13860. Add(' Numbers:=Value;');
  13861. Add(' Self.Numbers:=Value;');
  13862. Add('end;');
  13863. Add('var Obj: tobject;');
  13864. Add('begin');
  13865. Add(' obj.items:=nil;');
  13866. Add(' obj.items:=obj.items;');
  13867. Add(' obj.items[11]:=obj.items[12];');
  13868. ConvertProgram;
  13869. CheckSource('TestClass_PropertyOfTypeArray',
  13870. LinesToStr([ // statements
  13871. 'rtl.createClass(this, "TObject", null, function () {',
  13872. ' this.$init = function () {',
  13873. ' this.FItems = [];',
  13874. ' };',
  13875. ' this.$final = function () {',
  13876. ' this.FItems = undefined;',
  13877. ' };',
  13878. ' this.GetItems = function () {',
  13879. ' var Result = [];',
  13880. ' Result = rtl.arrayRef(this.FItems);',
  13881. ' return Result;',
  13882. ' };',
  13883. ' this.SetItems = function (Value) {',
  13884. ' this.FItems = rtl.arrayRef(Value);',
  13885. ' this.FItems = [];',
  13886. ' this.SetItems([]);',
  13887. ' this.SetItems(rtl.arrayRef(this.GetItems()));',
  13888. ' this.GetItems()[1] = 2;',
  13889. ' this.FItems[3] = this.GetItems()[4];',
  13890. ' this.GetItems()[5] = this.GetItems()[6];',
  13891. ' this.GetItems()[7] = 8;',
  13892. ' this.GetItems()[9] = this.GetItems()[10];',
  13893. ' this.GetItems()[this.GetItems()[11]] = this.GetItems()[this.GetItems()[12]];',
  13894. ' };',
  13895. ' this.SetNumbers = function (Value) {',
  13896. ' this.SetNumbers([]);',
  13897. ' this.SetNumbers(Value);',
  13898. ' this.SetNumbers(Value);',
  13899. ' };',
  13900. '});',
  13901. 'this.Obj = null;'
  13902. ]),
  13903. LinesToStr([ // $mod.$main
  13904. '$mod.Obj.SetItems([]);',
  13905. '$mod.Obj.SetItems($mod.Obj.GetItems());',
  13906. '$mod.Obj.GetItems()[11] = $mod.Obj.GetItems()[12];'
  13907. ]));
  13908. end;
  13909. procedure TTestModule.TestClass_PropertyDefault;
  13910. begin
  13911. StartProgram(false);
  13912. Add([
  13913. 'type',
  13914. ' TArray = array of longint;',
  13915. ' TObject = class',
  13916. ' end;',
  13917. ' TBird = class',
  13918. ' FItems: TArray;',
  13919. ' function GetItems(Index: longint): longint;',
  13920. ' procedure SetItems(Index, Value: longint);',
  13921. ' property Items[Index: longint]: longint read getitems write setitems; default;',
  13922. ' end;',
  13923. 'function TBird.getitems(index: longint): longint;',
  13924. 'begin',
  13925. 'end;',
  13926. 'procedure TBird.setitems(index, value: longint);',
  13927. 'begin',
  13928. ' Self[1]:=2;',
  13929. ' Self[3]:=Self[index];',
  13930. ' Self[index]:=Self[Self[value]];',
  13931. ' Self[Self[4]]:=value;',
  13932. 'end;',
  13933. 'var',
  13934. ' Bird: TBird;',
  13935. ' Obj: TObject;',
  13936. 'begin',
  13937. ' bird[11]:=12;',
  13938. ' bird[13]:=bird[14];',
  13939. ' bird[Bird[15]]:=bird[Bird[15]];',
  13940. ' TBird(obj)[16]:=TBird(obj)[17];',
  13941. ' (obj as tbird)[18]:=19;',
  13942. '']);
  13943. ConvertProgram;
  13944. CheckSource('TestClass_PropertyDefault',
  13945. LinesToStr([ // statements
  13946. 'rtl.createClass(this, "TObject", null, function () {',
  13947. ' this.$init = function () {',
  13948. ' };',
  13949. ' this.$final = function () {',
  13950. ' };',
  13951. '});',
  13952. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  13953. ' this.$init = function () {',
  13954. ' $mod.TObject.$init.call(this);',
  13955. ' this.FItems = [];',
  13956. ' };',
  13957. ' this.$final = function () {',
  13958. ' this.FItems = undefined;',
  13959. ' $mod.TObject.$final.call(this);',
  13960. ' };',
  13961. ' this.GetItems = function (Index) {',
  13962. ' var Result = 0;',
  13963. ' return Result;',
  13964. ' };',
  13965. ' this.SetItems = function (Index, Value) {',
  13966. ' this.SetItems(1, 2);',
  13967. ' this.SetItems(3, this.GetItems(Index));',
  13968. ' this.SetItems(Index, this.GetItems(this.GetItems(Value)));',
  13969. ' this.SetItems(this.GetItems(4), Value);',
  13970. ' };',
  13971. '});',
  13972. 'this.Bird = null;',
  13973. 'this.Obj = null;',
  13974. '']),
  13975. LinesToStr([ // $mod.$main
  13976. '$mod.Bird.SetItems(11, 12);',
  13977. '$mod.Bird.SetItems(13, $mod.Bird.GetItems(14));',
  13978. '$mod.Bird.SetItems($mod.Bird.GetItems(15), $mod.Bird.GetItems($mod.Bird.GetItems(15)));',
  13979. '$mod.Obj.SetItems(16, $mod.Obj.GetItems(17));',
  13980. 'rtl.as($mod.Obj, $mod.TBird).SetItems(18, 19);',
  13981. '']));
  13982. end;
  13983. procedure TTestModule.TestClass_PropertyDefault_TypecastToOtherDefault;
  13984. begin
  13985. StartProgram(false);
  13986. Add([
  13987. 'type',
  13988. ' TObject = class end;',
  13989. ' TAlphaList = class',
  13990. ' function GetAlphas(Index: boolean): Pointer; virtual; abstract;',
  13991. ' procedure SetAlphas(Index: boolean; Value: Pointer); virtual; abstract;',
  13992. ' property Alphas[Index: boolean]: Pointer read getAlphas write setAlphas; default;',
  13993. ' end;',
  13994. ' TBetaList = class',
  13995. ' function GetBetas(Index: longint): Pointer; virtual; abstract;',
  13996. ' procedure SetBetas(Index: longint; Value: Pointer); virtual; abstract;',
  13997. ' property Betas[Index: longint]: Pointer read getBetas write setBetas; default;',
  13998. ' end;',
  13999. ' TBird = class',
  14000. ' procedure DoIt;',
  14001. ' end;',
  14002. 'procedure TBird.DoIt;',
  14003. 'var',
  14004. ' List: TAlphaList;',
  14005. 'begin',
  14006. ' if TBetaList(List[true])[3]=nil then ;',
  14007. ' TBetaList(List[false])[5]:=nil;',
  14008. 'end;',
  14009. 'var',
  14010. ' List: TAlphaList;',
  14011. 'begin',
  14012. ' if TBetaList(List[true])[3]=nil then ;',
  14013. ' TBetaList(List[false])[5]:=nil;',
  14014. '']);
  14015. ConvertProgram;
  14016. CheckSource('TestClass_PropertyDefault_TypecastToOtherDefault',
  14017. LinesToStr([ // statements
  14018. 'rtl.createClass(this, "TObject", null, function () {',
  14019. ' this.$init = function () {',
  14020. ' };',
  14021. ' this.$final = function () {',
  14022. ' };',
  14023. '});',
  14024. 'rtl.createClass(this, "TAlphaList", this.TObject, function () {',
  14025. '});',
  14026. 'rtl.createClass(this, "TBetaList", this.TObject, function () {',
  14027. '});',
  14028. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  14029. ' this.DoIt = function () {',
  14030. ' var List = null;',
  14031. ' if (List.GetAlphas(true).GetBetas(3) === null) ;',
  14032. ' List.GetAlphas(false).SetBetas(5, null);',
  14033. ' };',
  14034. '});',
  14035. 'this.List = null;',
  14036. '']),
  14037. LinesToStr([ // $mod.$main
  14038. 'if ($mod.List.GetAlphas(true).GetBetas(3) === null) ;',
  14039. '$mod.List.GetAlphas(false).SetBetas(5, null);',
  14040. '']));
  14041. end;
  14042. procedure TTestModule.TestClass_PropertyOverride;
  14043. begin
  14044. StartProgram(false);
  14045. Add('type');
  14046. Add(' integer = longint;');
  14047. Add(' TObject = class');
  14048. Add(' FItem: integer;');
  14049. Add(' function GetItem: integer; external name ''GetItem'';');
  14050. Add(' procedure SetItem(Value: integer); external name ''SetItem'';');
  14051. Add(' property Item: integer read getitem write setitem;');
  14052. Add(' end;');
  14053. Add(' TCar = class');
  14054. Add(' FBag: integer;');
  14055. Add(' function GetBag: integer; external name ''GetBag'';');
  14056. Add(' property Item read getbag;');
  14057. Add(' end;');
  14058. Add('var');
  14059. Add(' Obj: tobject;');
  14060. Add(' Car: tcar;');
  14061. Add('begin');
  14062. Add(' Obj.Item:=Obj.Item;');
  14063. Add(' Car.Item:=Car.Item;');
  14064. ConvertProgram;
  14065. CheckSource('TestClass_PropertyOverride',
  14066. LinesToStr([ // statements
  14067. 'rtl.createClass(this, "TObject", null, function () {',
  14068. ' this.$init = function () {',
  14069. ' this.FItem = 0;',
  14070. ' };',
  14071. ' this.$final = function () {',
  14072. ' };',
  14073. '});',
  14074. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  14075. ' this.$init = function () {',
  14076. ' $mod.TObject.$init.call(this);',
  14077. ' this.FBag = 0;',
  14078. ' };',
  14079. '});',
  14080. 'this.Obj = null;',
  14081. 'this.Car = null;',
  14082. '']),
  14083. LinesToStr([ // $mod.$main
  14084. '$mod.Obj.SetItem($mod.Obj.GetItem());',
  14085. '$mod.Car.SetItem($mod.Car.GetBag());',
  14086. '']));
  14087. end;
  14088. procedure TTestModule.TestClass_PropertyIncVisibility;
  14089. begin
  14090. AddModuleWithIntfImplSrc('unit1.pp',
  14091. LinesToStr([
  14092. 'type',
  14093. ' TNumber = longint;',
  14094. ' TInteger = longint;',
  14095. ' TObject = class',
  14096. ' private',
  14097. ' function GetItems(Index: TNumber): TInteger; virtual; abstract;',
  14098. ' procedure SetItems(Index: TInteger; Value: TNumber); virtual; abstract;',
  14099. ' protected',
  14100. ' property Items[Index: TNumber]: longint read GetItems write SetItems;',
  14101. ' end;']),
  14102. LinesToStr([
  14103. '']));
  14104. StartProgram(true);
  14105. Add([
  14106. 'uses unit1;',
  14107. 'type',
  14108. ' TBird = class',
  14109. ' public',
  14110. ' property Items;',
  14111. ' end;',
  14112. 'procedure DoIt(i: TInteger);',
  14113. 'begin',
  14114. 'end;',
  14115. 'var b: TBird;',
  14116. 'begin',
  14117. ' b.Items[1]:=2;',
  14118. ' b.Items[3]:=b.Items[4];',
  14119. ' DoIt(b.Items[5]);',
  14120. '']);
  14121. ConvertProgram;
  14122. CheckSource('TestClass_PropertyIncVisibility',
  14123. LinesToStr([ // statements
  14124. 'rtl.createClass(this, "TBird", pas.unit1.TObject, function () {',
  14125. '});',
  14126. 'this.DoIt = function (i) {',
  14127. '};',
  14128. 'this.b = null;'
  14129. ]),
  14130. LinesToStr([ // $mod.$main
  14131. '$mod.b.SetItems(1, 2);',
  14132. '$mod.b.SetItems(3, $mod.b.GetItems(4));',
  14133. '$mod.DoIt($mod.b.GetItems(5));'
  14134. ]));
  14135. end;
  14136. procedure TTestModule.TestClass_Assigned;
  14137. begin
  14138. StartProgram(false);
  14139. Add('type');
  14140. Add(' TObject = class');
  14141. Add(' end;');
  14142. Add('var');
  14143. Add(' Obj: tobject;');
  14144. Add(' b: boolean;');
  14145. Add('begin');
  14146. Add(' if Assigned(obj) then ;');
  14147. Add(' b:=Assigned(obj) or false;');
  14148. ConvertProgram;
  14149. CheckSource('TestClass_Assigned',
  14150. LinesToStr([ // statements
  14151. 'rtl.createClass(this, "TObject", null, function () {',
  14152. ' this.$init = function () {',
  14153. ' };',
  14154. ' this.$final = function () {',
  14155. ' };',
  14156. '});',
  14157. 'this.Obj = null;',
  14158. 'this.b = false;'
  14159. ]),
  14160. LinesToStr([ // $mod.$main
  14161. 'if ($mod.Obj != null);',
  14162. '$mod.b = ($mod.Obj != null) || false;'
  14163. ]));
  14164. end;
  14165. procedure TTestModule.TestClass_WithClassDoCreate;
  14166. begin
  14167. StartProgram(false);
  14168. Add('type');
  14169. Add(' TObject = class');
  14170. Add(' aBool: boolean;');
  14171. Add(' Arr: array of boolean;');
  14172. Add(' constructor Create;');
  14173. Add(' end;');
  14174. Add('constructor TObject.Create; begin end;');
  14175. Add('var');
  14176. Add(' Obj: tobject;');
  14177. Add(' b: boolean;');
  14178. Add('begin');
  14179. Add(' with tobject.create do begin');
  14180. Add(' b:=abool;');
  14181. Add(' abool:=b;');
  14182. Add(' b:=arr[1];');
  14183. Add(' arr[2]:=b;');
  14184. Add(' end;');
  14185. Add(' with tobject do');
  14186. Add(' obj:=create;');
  14187. Add(' with obj do begin');
  14188. Add(' create;');
  14189. Add(' b:=abool;');
  14190. Add(' abool:=b;');
  14191. Add(' b:=arr[3];');
  14192. Add(' arr[4]:=b;');
  14193. Add(' end;');
  14194. ConvertProgram;
  14195. CheckSource('TestClass_WithClassDoCreate',
  14196. LinesToStr([ // statements
  14197. 'rtl.createClass(this, "TObject", null, function () {',
  14198. ' this.$init = function () {',
  14199. ' this.aBool = false;',
  14200. ' this.Arr = [];',
  14201. ' };',
  14202. ' this.$final = function () {',
  14203. ' this.Arr = undefined;',
  14204. ' };',
  14205. ' this.Create = function () {',
  14206. ' return this;',
  14207. ' };',
  14208. '});',
  14209. 'this.Obj = null;',
  14210. 'this.b = false;'
  14211. ]),
  14212. LinesToStr([ // $mod.$main
  14213. 'var $with = $mod.TObject.$create("Create");',
  14214. '$mod.b = $with.aBool;',
  14215. '$with.aBool = $mod.b;',
  14216. '$mod.b = $with.Arr[1];',
  14217. '$with.Arr[2] = $mod.b;',
  14218. 'var $with1 = $mod.TObject;',
  14219. '$mod.Obj = $with1.$create("Create");',
  14220. 'var $with2 = $mod.Obj;',
  14221. '$with2.Create();',
  14222. '$mod.b = $with2.aBool;',
  14223. '$with2.aBool = $mod.b;',
  14224. '$mod.b = $with2.Arr[3];',
  14225. '$with2.Arr[4] = $mod.b;',
  14226. '']));
  14227. end;
  14228. procedure TTestModule.TestClass_WithClassInstDoProperty;
  14229. begin
  14230. StartProgram(false);
  14231. Add('type');
  14232. Add(' TObject = class');
  14233. Add(' FInt: longint;');
  14234. Add(' constructor Create;');
  14235. Add(' function GetSize: longint;');
  14236. Add(' procedure SetSize(Value: longint);');
  14237. Add(' property Int: longint read FInt write FInt;');
  14238. Add(' property Size: longint read GetSize write SetSize;');
  14239. Add(' end;');
  14240. Add('constructor TObject.Create; begin end;');
  14241. Add('function TObject.GetSize: longint; begin; end;');
  14242. Add('procedure TObject.SetSize(Value: longint); begin; end;');
  14243. Add('var');
  14244. Add(' Obj: tobject;');
  14245. Add(' i: longint;');
  14246. Add('begin');
  14247. Add(' with TObject.Create do begin');
  14248. Add(' i:=int;');
  14249. Add(' int:=i;');
  14250. Add(' i:=size;');
  14251. Add(' size:=i;');
  14252. Add(' end;');
  14253. Add(' with obj do begin');
  14254. Add(' i:=int;');
  14255. Add(' int:=i;');
  14256. Add(' i:=size;');
  14257. Add(' size:=i;');
  14258. Add(' end;');
  14259. ConvertProgram;
  14260. CheckSource('TestClass_WithClassInstDoProperty',
  14261. LinesToStr([ // statements
  14262. 'rtl.createClass(this, "TObject", null, function () {',
  14263. ' this.$init = function () {',
  14264. ' this.FInt = 0;',
  14265. ' };',
  14266. ' this.$final = function () {',
  14267. ' };',
  14268. ' this.Create = function () {',
  14269. ' return this;',
  14270. ' };',
  14271. ' this.GetSize = function () {',
  14272. ' var Result = 0;',
  14273. ' return Result;',
  14274. ' };',
  14275. ' this.SetSize = function (Value) {',
  14276. ' };',
  14277. '});',
  14278. 'this.Obj = null;',
  14279. 'this.i = 0;'
  14280. ]),
  14281. LinesToStr([ // $mod.$main
  14282. 'var $with = $mod.TObject.$create("Create");',
  14283. '$mod.i = $with.FInt;',
  14284. '$with.FInt = $mod.i;',
  14285. '$mod.i = $with.GetSize();',
  14286. '$with.SetSize($mod.i);',
  14287. 'var $with1 = $mod.Obj;',
  14288. '$mod.i = $with1.FInt;',
  14289. '$with1.FInt = $mod.i;',
  14290. '$mod.i = $with1.GetSize();',
  14291. '$with1.SetSize($mod.i);',
  14292. '']));
  14293. end;
  14294. procedure TTestModule.TestClass_WithClassInstDoPropertyWithParams;
  14295. begin
  14296. StartProgram(false);
  14297. Add('type');
  14298. Add(' TObject = class');
  14299. Add(' constructor Create;');
  14300. Add(' function GetItems(Index: longint): longint;');
  14301. Add(' procedure SetItems(Index, Value: longint);');
  14302. Add(' property Items[Index: longint]: longint read GetItems write SetItems;');
  14303. Add(' end;');
  14304. Add('constructor TObject.Create; begin end;');
  14305. Add('function tobject.getitems(index: longint): longint; begin; end;');
  14306. Add('procedure tobject.setitems(index, value: longint); begin; end;');
  14307. Add('var');
  14308. Add(' Obj: tobject;');
  14309. Add(' i: longint;');
  14310. Add('begin');
  14311. Add(' with TObject.Create do begin');
  14312. Add(' i:=Items[1];');
  14313. Add(' Items[2]:=i;');
  14314. Add(' end;');
  14315. Add(' with obj do begin');
  14316. Add(' i:=Items[3];');
  14317. Add(' Items[4]:=i;');
  14318. Add(' end;');
  14319. ConvertProgram;
  14320. CheckSource('TestClass_WithClassInstDoPropertyWithParams',
  14321. LinesToStr([ // statements
  14322. 'rtl.createClass(this, "TObject", null, function () {',
  14323. ' this.$init = function () {',
  14324. ' };',
  14325. ' this.$final = function () {',
  14326. ' };',
  14327. ' this.Create = function () {',
  14328. ' return this;',
  14329. ' };',
  14330. ' this.GetItems = function (Index) {',
  14331. ' var Result = 0;',
  14332. ' return Result;',
  14333. ' };',
  14334. ' this.SetItems = function (Index, Value) {',
  14335. ' };',
  14336. '});',
  14337. 'this.Obj = null;',
  14338. 'this.i = 0;'
  14339. ]),
  14340. LinesToStr([ // $mod.$main
  14341. 'var $with = $mod.TObject.$create("Create");',
  14342. '$mod.i = $with.GetItems(1);',
  14343. '$with.SetItems(2, $mod.i);',
  14344. 'var $with1 = $mod.Obj;',
  14345. '$mod.i = $with1.GetItems(3);',
  14346. '$with1.SetItems(4, $mod.i);',
  14347. '']));
  14348. end;
  14349. procedure TTestModule.TestClass_WithClassInstDoFunc;
  14350. begin
  14351. StartProgram(false);
  14352. Add('type');
  14353. Add(' TObject = class');
  14354. Add(' constructor Create;');
  14355. Add(' function GetSize: longint;');
  14356. Add(' procedure SetSize(Value: longint);');
  14357. Add(' end;');
  14358. Add('constructor TObject.Create; begin end;');
  14359. Add('function TObject.GetSize: longint; begin; end;');
  14360. Add('procedure TObject.SetSize(Value: longint); begin; end;');
  14361. Add('var');
  14362. Add(' Obj: tobject;');
  14363. Add(' i: longint;');
  14364. Add('begin');
  14365. Add(' with TObject.Create do begin');
  14366. Add(' i:=GetSize;');
  14367. Add(' i:=GetSize();');
  14368. Add(' SetSize(i);');
  14369. Add(' end;');
  14370. Add(' with obj do begin');
  14371. Add(' i:=GetSize;');
  14372. Add(' i:=GetSize();');
  14373. Add(' SetSize(i);');
  14374. Add(' end;');
  14375. ConvertProgram;
  14376. CheckSource('TestClass_WithClassInstDoFunc',
  14377. LinesToStr([ // statements
  14378. 'rtl.createClass(this, "TObject", null, function () {',
  14379. ' this.$init = function () {',
  14380. ' };',
  14381. ' this.$final = function () {',
  14382. ' };',
  14383. ' this.Create = function () {',
  14384. ' return this;',
  14385. ' };',
  14386. ' this.GetSize = function () {',
  14387. ' var Result = 0;',
  14388. ' return Result;',
  14389. ' };',
  14390. ' this.SetSize = function (Value) {',
  14391. ' };',
  14392. '});',
  14393. 'this.Obj = null;',
  14394. 'this.i = 0;'
  14395. ]),
  14396. LinesToStr([ // $mod.$main
  14397. 'var $with = $mod.TObject.$create("Create");',
  14398. '$mod.i = $with.GetSize();',
  14399. '$mod.i = $with.GetSize();',
  14400. '$with.SetSize($mod.i);',
  14401. 'var $with1 = $mod.Obj;',
  14402. '$mod.i = $with1.GetSize();',
  14403. '$mod.i = $with1.GetSize();',
  14404. '$with1.SetSize($mod.i);',
  14405. '']));
  14406. end;
  14407. procedure TTestModule.TestClass_TypeCast;
  14408. begin
  14409. StartProgram(false);
  14410. Add('type');
  14411. Add(' TObject = class');
  14412. Add(' Next: TObject;');
  14413. Add(' constructor Create;');
  14414. Add(' end;');
  14415. Add(' TControl = class(TObject)');
  14416. Add(' Arr: array of TObject;');
  14417. Add(' function GetIt(vI: longint = 0): TObject;');
  14418. Add(' end;');
  14419. Add('constructor tobject.create; begin end;');
  14420. Add('function tcontrol.getit(vi: longint = 0): tobject; begin end;');
  14421. Add('var');
  14422. Add(' Obj: tobject;');
  14423. Add('begin');
  14424. Add(' obj:=tcontrol(obj).next;');
  14425. Add(' tcontrol(obj):=nil;');
  14426. Add(' obj:=tcontrol(obj);');
  14427. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit);');
  14428. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit());');
  14429. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit(1));');
  14430. Add(' tcontrol(obj):=tcontrol(tcontrol(tcontrol(obj).getit).arr[2]);');
  14431. Add(' obj:=tcontrol(nil);');
  14432. ConvertProgram;
  14433. CheckSource('TestClass_TypeCast',
  14434. LinesToStr([ // statements
  14435. 'rtl.createClass(this, "TObject", null, function () {',
  14436. ' this.$init = function () {',
  14437. ' this.Next = null;',
  14438. ' };',
  14439. ' this.$final = function () {',
  14440. ' this.Next = undefined;',
  14441. ' };',
  14442. ' this.Create = function () {',
  14443. ' return this;',
  14444. ' };',
  14445. '});',
  14446. 'rtl.createClass(this, "TControl", this.TObject, function () {',
  14447. ' this.$init = function () {',
  14448. ' $mod.TObject.$init.call(this);',
  14449. ' this.Arr = [];',
  14450. ' };',
  14451. ' this.$final = function () {',
  14452. ' this.Arr = undefined;',
  14453. ' $mod.TObject.$final.call(this);',
  14454. ' };',
  14455. ' this.GetIt = function (vI) {',
  14456. ' var Result = null;',
  14457. ' return Result;',
  14458. ' };',
  14459. '});',
  14460. 'this.Obj = null;'
  14461. ]),
  14462. LinesToStr([ // $mod.$main
  14463. '$mod.Obj = $mod.Obj.Next;',
  14464. '$mod.Obj = null;',
  14465. '$mod.Obj = $mod.Obj;',
  14466. '$mod.Obj = $mod.Obj.GetIt(0);',
  14467. '$mod.Obj = $mod.Obj.GetIt(0);',
  14468. '$mod.Obj = $mod.Obj.GetIt(1);',
  14469. '$mod.Obj = $mod.Obj.GetIt(0).Arr[2];',
  14470. '$mod.Obj = null;',
  14471. '']));
  14472. end;
  14473. procedure TTestModule.TestClass_TypeCastUntypedParam;
  14474. begin
  14475. StartProgram(false);
  14476. Add('type');
  14477. Add(' TObject = class end;');
  14478. Add('procedure ProcA(var A);');
  14479. Add('begin');
  14480. Add(' TObject(A):=nil;');
  14481. Add(' TObject(A):=TObject(A);');
  14482. Add(' if TObject(A)=nil then ;');
  14483. Add(' if nil=TObject(A) then ;');
  14484. Add('end;');
  14485. Add('procedure ProcB(out A);');
  14486. Add('begin');
  14487. Add(' TObject(A):=nil;');
  14488. Add(' TObject(A):=TObject(A);');
  14489. Add(' if TObject(A)=nil then ;');
  14490. Add(' if nil=TObject(A) then ;');
  14491. Add('end;');
  14492. Add('procedure ProcC(const A);');
  14493. Add('begin');
  14494. Add(' if TObject(A)=nil then ;');
  14495. Add(' if nil=TObject(A) then ;');
  14496. Add('end;');
  14497. Add('var o: TObject;');
  14498. Add('begin');
  14499. Add(' ProcA(o);');
  14500. Add(' ProcB(o);');
  14501. Add(' ProcC(o);');
  14502. ConvertProgram;
  14503. CheckSource('TestClass_TypeCastUntypedParam',
  14504. LinesToStr([ // statements
  14505. 'rtl.createClass(this, "TObject", null, function () {',
  14506. ' this.$init = function () {',
  14507. ' };',
  14508. ' this.$final = function () {',
  14509. ' };',
  14510. '});',
  14511. 'this.ProcA = function (A) {',
  14512. ' A.set(null);',
  14513. ' A.set(A.get());',
  14514. ' if (A.get() === null);',
  14515. ' if (null === A.get());',
  14516. '};',
  14517. 'this.ProcB = function (A) {',
  14518. ' A.set(null);',
  14519. ' A.set(A.get());',
  14520. ' if (A.get() === null);',
  14521. ' if (null === A.get());',
  14522. '};',
  14523. 'this.ProcC = function (A) {',
  14524. ' if (A === null);',
  14525. ' if (null === A);',
  14526. '};',
  14527. 'this.o = null;',
  14528. '']),
  14529. LinesToStr([ // $mod.$main
  14530. '$mod.ProcA({',
  14531. ' p: $mod,',
  14532. ' get: function () {',
  14533. ' return this.p.o;',
  14534. ' },',
  14535. ' set: function (v) {',
  14536. ' this.p.o = v;',
  14537. ' }',
  14538. '});',
  14539. '$mod.ProcB({',
  14540. ' p: $mod,',
  14541. ' get: function () {',
  14542. ' return this.p.o;',
  14543. ' },',
  14544. ' set: function (v) {',
  14545. ' this.p.o = v;',
  14546. ' }',
  14547. '});',
  14548. '$mod.ProcC($mod.o);',
  14549. '']));
  14550. end;
  14551. procedure TTestModule.TestClass_Overloads;
  14552. begin
  14553. StartProgram(false);
  14554. Add('type');
  14555. Add(' TObject = class');
  14556. Add(' procedure DoIt;');
  14557. Add(' procedure DoIt(vI: longint);');
  14558. Add(' end;');
  14559. Add('procedure TObject.DoIt;');
  14560. Add('begin');
  14561. Add(' DoIt;');
  14562. Add(' DoIt(1);');
  14563. Add('end;');
  14564. Add('procedure TObject.DoIt(vI: longint); begin end;');
  14565. Add('begin');
  14566. ConvertProgram;
  14567. CheckSource('TestClass_Overloads',
  14568. LinesToStr([ // statements
  14569. 'rtl.createClass(this, "TObject", null, function () {',
  14570. ' this.$init = function () {',
  14571. ' };',
  14572. ' this.$final = function () {',
  14573. ' };',
  14574. ' this.DoIt = function () {',
  14575. ' this.DoIt();',
  14576. ' this.DoIt$1(1);',
  14577. ' };',
  14578. ' this.DoIt$1 = function (vI) {',
  14579. ' };',
  14580. '});',
  14581. '']),
  14582. LinesToStr([ // $mod.$main
  14583. '']));
  14584. end;
  14585. procedure TTestModule.TestClass_OverloadsAncestor;
  14586. begin
  14587. StartProgram(false);
  14588. Add('type');
  14589. Add(' TObject = class;');
  14590. Add(' TObject = class');
  14591. Add(' procedure DoIt(vA: longint);');
  14592. Add(' procedure DoIt(vA, vB: longint);');
  14593. Add(' end;');
  14594. Add(' TCar = class;');
  14595. Add(' TCar = class');
  14596. Add(' procedure DoIt(vA: longint);');
  14597. Add(' procedure DoIt(vA, vB: longint);');
  14598. Add(' end;');
  14599. Add('procedure tobject.doit(va: longint);');
  14600. Add('begin');
  14601. Add(' doit(1);');
  14602. Add(' doit(1,2);');
  14603. Add('end;');
  14604. Add('procedure tobject.doit(va, vb: longint); begin end;');
  14605. Add('procedure tcar.doit(va: longint);');
  14606. Add('begin');
  14607. Add(' doit(1);');
  14608. Add(' doit(1,2);');
  14609. Add(' inherited doit(1);');
  14610. Add(' inherited doit(1,2);');
  14611. Add('end;');
  14612. Add('procedure tcar.doit(va, vb: longint); begin end;');
  14613. Add('begin');
  14614. ConvertProgram;
  14615. CheckSource('TestClass_OverloadsAncestor',
  14616. LinesToStr([ // statements
  14617. 'rtl.createClass(this, "TObject", null, function () {',
  14618. ' this.$init = function () {',
  14619. ' };',
  14620. ' this.$final = function () {',
  14621. ' };',
  14622. ' this.DoIt = function (vA) {',
  14623. ' this.DoIt(1);',
  14624. ' this.DoIt$1(1,2);',
  14625. ' };',
  14626. ' this.DoIt$1 = function (vA, vB) {',
  14627. ' };',
  14628. '});',
  14629. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  14630. ' this.DoIt$2 = function (vA) {',
  14631. ' this.DoIt$2(1);',
  14632. ' this.DoIt$3(1, 2);',
  14633. ' $mod.TObject.DoIt.call(this, 1);',
  14634. ' $mod.TObject.DoIt$1.call(this, 1, 2);',
  14635. ' };',
  14636. ' this.DoIt$3 = function (vA, vB) {',
  14637. ' };',
  14638. '});',
  14639. '']),
  14640. LinesToStr([ // $mod.$main
  14641. '']));
  14642. end;
  14643. procedure TTestModule.TestClass_OverloadConstructor;
  14644. begin
  14645. StartProgram(false);
  14646. Add('type');
  14647. Add(' TObject = class');
  14648. Add(' constructor Create(vA: longint);');
  14649. Add(' constructor Create(vA, vB: longint);');
  14650. Add(' end;');
  14651. Add(' TCar = class');
  14652. Add(' constructor Create(vA: longint);');
  14653. Add(' constructor Create(vA, vB: longint);');
  14654. Add(' end;');
  14655. Add('constructor tobject.create(va: longint);');
  14656. Add('begin');
  14657. Add(' create(1);');
  14658. Add(' create(1,2);');
  14659. Add('end;');
  14660. Add('constructor tobject.create(va, vb: longint); begin end;');
  14661. Add('constructor tcar.create(va: longint);');
  14662. Add('begin');
  14663. Add(' create(1);');
  14664. Add(' create(1,2);');
  14665. Add(' inherited create(1);');
  14666. Add(' inherited create(1,2);');
  14667. Add('end;');
  14668. Add('constructor tcar.create(va, vb: longint); begin end;');
  14669. Add('begin');
  14670. Add(' tobject.create(1);');
  14671. Add(' tobject.create(1,2);');
  14672. Add(' tcar.create(1);');
  14673. Add(' tcar.create(1,2);');
  14674. ConvertProgram;
  14675. CheckSource('TestClass_OverloadConstructor',
  14676. LinesToStr([ // statements
  14677. 'rtl.createClass(this, "TObject", null, function () {',
  14678. ' this.$init = function () {',
  14679. ' };',
  14680. ' this.$final = function () {',
  14681. ' };',
  14682. ' this.Create = function (vA) {',
  14683. ' this.Create(1);',
  14684. ' this.Create$1(1,2);',
  14685. ' return this;',
  14686. ' };',
  14687. ' this.Create$1 = function (vA, vB) {',
  14688. ' return this;',
  14689. ' };',
  14690. '});',
  14691. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  14692. ' this.Create$2 = function (vA) {',
  14693. ' this.Create$2(1);',
  14694. ' this.Create$3(1, 2);',
  14695. ' $mod.TObject.Create.call(this, 1);',
  14696. ' $mod.TObject.Create$1.call(this, 1, 2);',
  14697. ' return this;',
  14698. ' };',
  14699. ' this.Create$3 = function (vA, vB) {',
  14700. ' return this;',
  14701. ' };',
  14702. '});',
  14703. '']),
  14704. LinesToStr([ // $mod.$main
  14705. '$mod.TObject.$create("Create", [1]);',
  14706. '$mod.TObject.$create("Create$1", [1, 2]);',
  14707. '$mod.TCar.$create("Create$2", [1]);',
  14708. '$mod.TCar.$create("Create$3", [1, 2]);',
  14709. '']));
  14710. end;
  14711. procedure TTestModule.TestClass_OverloadDelphiOverride;
  14712. begin
  14713. StartProgram(false);
  14714. Add([
  14715. '{$mode delphi}',
  14716. 'type',
  14717. ' TObject = class end;',
  14718. ' TBird = class',
  14719. ' function {#a}GetValue: longint; overload; virtual;',
  14720. ' function {#b}GetValue(AValue: longint): longint; overload; virtual;',
  14721. ' end;',
  14722. ' TEagle = class(TBird)',
  14723. ' function {#c}GetValue: longint; overload; override;',
  14724. ' function {#d}GetValue(AValue: longint): longint; overload; override;',
  14725. ' end;',
  14726. 'function TBird.GetValue: longint;',
  14727. 'begin',
  14728. ' if 3={@a}GetValue then ;',
  14729. ' if 4={@b}GetValue(5) then ;',
  14730. 'end;',
  14731. 'function TBird.GetValue(AValue: longint): longint;',
  14732. 'begin',
  14733. 'end;',
  14734. 'function TEagle.GetValue: longint;',
  14735. 'begin',
  14736. ' if 13={@c}GetValue then ;',
  14737. ' if 14={@d}GetValue(15) then ;',
  14738. ' if 15=inherited {@a}GetValue then ;',
  14739. ' if 16=inherited {@b}GetValue(17) then ;',
  14740. 'end;',
  14741. 'function TEagle.GetValue(AValue: longint): longint;',
  14742. 'begin',
  14743. 'end;',
  14744. 'var',
  14745. ' e: TEagle;',
  14746. 'begin',
  14747. ' if 23=e.{@c}GetValue then ;',
  14748. ' if 24=e.{@d}GetValue(25) then ;']);
  14749. ConvertProgram;
  14750. CheckSource('TestClass_OverloadDelphiOverride',
  14751. LinesToStr([ // statements
  14752. 'rtl.createClass(this, "TObject", null, function () {',
  14753. ' this.$init = function () {',
  14754. ' };',
  14755. ' this.$final = function () {',
  14756. ' };',
  14757. '});',
  14758. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  14759. ' this.GetValue = function () {',
  14760. ' var Result = 0;',
  14761. ' if (3 === this.GetValue()) ;',
  14762. ' if (4 === this.GetValue$1(5)) ;',
  14763. ' return Result;',
  14764. ' };',
  14765. ' this.GetValue$1 = function (AValue) {',
  14766. ' var Result = 0;',
  14767. ' return Result;',
  14768. ' };',
  14769. '});',
  14770. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  14771. ' this.GetValue = function () {',
  14772. ' var Result = 0;',
  14773. ' if (13 === this.GetValue()) ;',
  14774. ' if (14 === this.GetValue$1(15)) ;',
  14775. ' if (15 === $mod.TBird.GetValue.call(this)) ;',
  14776. ' if (16 === $mod.TBird.GetValue$1.call(this, 17)) ;',
  14777. ' return Result;',
  14778. ' };',
  14779. ' this.GetValue$1 = function (AValue) {',
  14780. ' var Result = 0;',
  14781. ' return Result;',
  14782. ' };',
  14783. '});',
  14784. 'this.e = null;',
  14785. '']),
  14786. LinesToStr([ // $mod.$main
  14787. 'if (23 === $mod.e.GetValue()) ;',
  14788. 'if (24 === $mod.e.GetValue$1(25)) ;',
  14789. '']));
  14790. end;
  14791. procedure TTestModule.TestClass_ReintroduceVarDelphi;
  14792. begin
  14793. StartProgram(false);
  14794. Add([
  14795. '{$mode delphi}',
  14796. 'type',
  14797. ' TObject = class end;',
  14798. ' TAnimal = class',
  14799. ' public',
  14800. ' {#animal_a}A: longint;',
  14801. ' function {#animal_b}B: longint;',
  14802. ' end;',
  14803. ' TBird = class(TAnimal)',
  14804. ' public',
  14805. ' {#bird_a}A: double;',
  14806. ' {#bird_b}B: boolean;',
  14807. ' end;',
  14808. ' TEagle = class(TBird)',
  14809. ' public',
  14810. ' function {#eagle_a}A: boolean;',
  14811. ' {#eagle_b}B: double;',
  14812. ' end;',
  14813. 'function TAnimal.B: longint;',
  14814. 'begin',
  14815. 'end;',
  14816. 'function TEagle.A: boolean;',
  14817. 'begin',
  14818. ' {@eagle_b}B:=3.3;',
  14819. ' {@eagle_a}A();',
  14820. ' TBird(Self).{@bird_b}B:=true;',
  14821. ' TAnimal(Self).{@animal_a}A:=17;',
  14822. ' inherited {@bird_b}B:=inherited {bird_a}A>1;', // Delphi allows only inherited <functionname>
  14823. 'end;',
  14824. 'var',
  14825. ' e: TEagle;',
  14826. 'begin',
  14827. ' e.{@eagle_b}B:=5.3;',
  14828. ' if e.{@eagle_a}A then ;',
  14829. '']);
  14830. ConvertProgram;
  14831. CheckSource('TestClass_ReintroduceVarDelphi',
  14832. LinesToStr([ // statements
  14833. 'rtl.createClass(this, "TObject", null, function () {',
  14834. ' this.$init = function () {',
  14835. ' };',
  14836. ' this.$final = function () {',
  14837. ' };',
  14838. '});',
  14839. 'rtl.createClass(this, "TAnimal", this.TObject, function () {',
  14840. ' this.$init = function () {',
  14841. ' $mod.TObject.$init.call(this);',
  14842. ' this.A = 0;',
  14843. ' };',
  14844. ' this.B = function () {',
  14845. ' var Result = 0;',
  14846. ' return Result;',
  14847. ' };',
  14848. '});',
  14849. 'rtl.createClass(this, "TBird", this.TAnimal, function () {',
  14850. ' this.$init = function () {',
  14851. ' $mod.TAnimal.$init.call(this);',
  14852. ' this.A$1 = 0.0;',
  14853. ' this.B$1 = false;',
  14854. ' };',
  14855. '});',
  14856. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  14857. ' this.$init = function () {',
  14858. ' $mod.TBird.$init.call(this);',
  14859. ' this.B$2 = 0.0;',
  14860. ' };',
  14861. ' this.A$2 = function () {',
  14862. ' var Result = false;',
  14863. ' this.B$2 = 3.3;',
  14864. ' this.A$2();',
  14865. ' this.B$1 = true;',
  14866. ' this.A = 17;',
  14867. ' this.B$1 = this.A$1 > 1;',
  14868. ' return Result;',
  14869. ' };',
  14870. '});',
  14871. 'this.e = null;',
  14872. '']),
  14873. LinesToStr([ // $mod.$main
  14874. '$mod.e.B$2 = 5.3;',
  14875. 'if ($mod.e.A$2()) ;',
  14876. '']));
  14877. end;
  14878. procedure TTestModule.TestClass_ReintroducedVar;
  14879. begin
  14880. StartProgram(false);
  14881. Add('type');
  14882. Add(' TObject = class');
  14883. Add(' strict private');
  14884. Add(' Some: longint;');
  14885. Add(' end;');
  14886. Add(' TMobile = class');
  14887. Add(' strict private');
  14888. Add(' Some: string;');
  14889. Add(' end;');
  14890. Add(' TCar = class(tmobile)');
  14891. Add(' procedure Some;');
  14892. Add(' procedure Some(vA: longint);');
  14893. Add(' end;');
  14894. Add('procedure tcar.some;');
  14895. Add('begin');
  14896. Add(' Some;');
  14897. Add(' Some(1);');
  14898. Add('end;');
  14899. Add('procedure tcar.some(va: longint); begin end;');
  14900. Add('begin');
  14901. ConvertProgram;
  14902. CheckSource('TestClass_ReintroducedVar',
  14903. LinesToStr([ // statements
  14904. 'rtl.createClass(this, "TObject", null, function () {',
  14905. ' this.$init = function () {',
  14906. ' this.Some = 0;',
  14907. ' };',
  14908. ' this.$final = function () {',
  14909. ' };',
  14910. '});',
  14911. 'rtl.createClass(this, "TMobile", this.TObject, function () {',
  14912. ' this.$init = function () {',
  14913. ' $mod.TObject.$init.call(this);',
  14914. ' this.Some$1 = "";',
  14915. ' };',
  14916. '});',
  14917. 'rtl.createClass(this, "TCar", this.TMobile, function () {',
  14918. ' this.Some$2 = function () {',
  14919. ' this.Some$2();',
  14920. ' this.Some$3(1);',
  14921. ' };',
  14922. ' this.Some$3 = function (vA) {',
  14923. ' };',
  14924. '});',
  14925. '']),
  14926. LinesToStr([ // $mod.$main
  14927. '']));
  14928. end;
  14929. procedure TTestModule.TestClass_RaiseDescendant;
  14930. begin
  14931. StartProgram(false);
  14932. Add([
  14933. 'type',
  14934. ' TObject = class',
  14935. ' constructor Create(Msg: string);',
  14936. ' end;',
  14937. ' Exception = class',
  14938. ' end;',
  14939. ' EConvertError = class(Exception)',
  14940. ' end;',
  14941. 'constructor TObject.Create(Msg: string); begin end;',
  14942. 'function AssertConv(Msg: string = ''def''): EConvertError; begin end;',
  14943. 'begin',
  14944. ' raise Exception.Create(''Bar1'');',
  14945. ' raise EConvertError.Create(''Bar2'');',
  14946. ' raise AssertConv(''Bar2'');',
  14947. ' raise AssertConv;',
  14948. '']);
  14949. ConvertProgram;
  14950. CheckSource('TestClass_RaiseDescendant',
  14951. LinesToStr([ // statements
  14952. 'rtl.createClass(this, "TObject", null, function () {',
  14953. ' this.$init = function () {',
  14954. ' };',
  14955. ' this.$final = function () {',
  14956. ' };',
  14957. ' this.Create = function (Msg) {',
  14958. ' return this;',
  14959. ' };',
  14960. '});',
  14961. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  14962. '});',
  14963. 'rtl.createClass(this, "EConvertError", this.Exception, function () {',
  14964. '});',
  14965. 'this.AssertConv = function (Msg) {',
  14966. ' var Result = null;',
  14967. ' return Result;',
  14968. '};',
  14969. '']),
  14970. LinesToStr([ // $mod.$main
  14971. 'throw $mod.Exception.$create("Create",["Bar1"]);',
  14972. 'throw $mod.EConvertError.$create("Create",["Bar2"]);',
  14973. 'throw $mod.AssertConv("Bar2");',
  14974. 'throw $mod.AssertConv("def");',
  14975. '']));
  14976. end;
  14977. procedure TTestModule.TestClass_ExternalMethod;
  14978. begin
  14979. AddModuleWithIntfImplSrc('unit2.pas',
  14980. LinesToStr([
  14981. 'type',
  14982. ' TObject = class',
  14983. ' public',
  14984. ' procedure Intern; external name ''$DoIntern'';',
  14985. ' end;',
  14986. '']),
  14987. LinesToStr([
  14988. '']));
  14989. StartUnit(true);
  14990. Add('interface');
  14991. Add('uses unit2;');
  14992. Add('type');
  14993. Add(' TCar = class(TObject)');
  14994. Add(' public');
  14995. Add(' procedure Intern2; external name ''$DoIntern2'';');
  14996. Add(' procedure DoIt;');
  14997. Add(' end;');
  14998. Add('implementation');
  14999. Add('procedure tcar.doit;');
  15000. Add('begin');
  15001. Add(' Intern;');
  15002. Add(' Intern();');
  15003. Add(' Intern2;');
  15004. Add(' Intern2();');
  15005. Add('end;');
  15006. Add('var Obj: TCar;');
  15007. Add('begin');
  15008. Add(' obj.intern;');
  15009. Add(' obj.intern();');
  15010. Add(' obj.intern2;');
  15011. Add(' obj.intern2();');
  15012. Add(' obj.doit;');
  15013. Add(' obj.doit();');
  15014. Add(' with obj do begin');
  15015. Add(' Intern;');
  15016. Add(' Intern();');
  15017. Add(' Intern2;');
  15018. Add(' Intern2();');
  15019. Add(' end;');
  15020. ConvertUnit;
  15021. CheckSource('TestClass_ExternalMethod',
  15022. LinesToStr([
  15023. 'var $impl = $mod.$impl;',
  15024. 'rtl.createClass(this, "TCar", pas.unit2.TObject, function () {',
  15025. ' this.DoIt = function () {',
  15026. ' this.$DoIntern();',
  15027. ' this.$DoIntern();',
  15028. ' this.$DoIntern2();',
  15029. ' this.$DoIntern2();',
  15030. ' };',
  15031. ' });',
  15032. '']),
  15033. LinesToStr([ // this.$init
  15034. '$impl.Obj.$DoIntern();',
  15035. '$impl.Obj.$DoIntern();',
  15036. '$impl.Obj.$DoIntern2();',
  15037. '$impl.Obj.$DoIntern2();',
  15038. '$impl.Obj.DoIt();',
  15039. '$impl.Obj.DoIt();',
  15040. 'var $with = $impl.Obj;',
  15041. '$with.$DoIntern();',
  15042. '$with.$DoIntern();',
  15043. '$with.$DoIntern2();',
  15044. '$with.$DoIntern2();',
  15045. '']),
  15046. LinesToStr([ // implementation
  15047. '$impl.Obj = null;',
  15048. '']) );
  15049. end;
  15050. procedure TTestModule.TestClass_ExternalVirtualNameMismatchFail;
  15051. begin
  15052. StartProgram(false);
  15053. Add('type');
  15054. Add(' TObject = class');
  15055. Add(' procedure DoIt; virtual; external name ''Foo'';');
  15056. Add(' end;');
  15057. Add('begin');
  15058. SetExpectedPasResolverError('Virtual method name must match external',
  15059. nVirtualMethodNameMustMatchExternal);
  15060. ConvertProgram;
  15061. end;
  15062. procedure TTestModule.TestClass_ExternalOverrideFail;
  15063. begin
  15064. StartProgram(false);
  15065. Add('type');
  15066. Add(' TObject = class');
  15067. Add(' procedure DoIt; virtual; external name ''DoIt'';');
  15068. Add(' end;');
  15069. Add(' TCar = class');
  15070. Add(' procedure DoIt; override; external name ''DoIt'';');
  15071. Add(' end;');
  15072. Add('begin');
  15073. SetExpectedPasResolverError('Invalid procedure modifier override,external',
  15074. nInvalidXModifierY);
  15075. ConvertProgram;
  15076. end;
  15077. procedure TTestModule.TestClass_ExternalVar;
  15078. begin
  15079. AddModuleWithIntfImplSrc('unit2.pas',
  15080. LinesToStr([
  15081. '{$modeswitch externalclass}',
  15082. 'type',
  15083. ' TObject = class',
  15084. ' public',
  15085. ' Intern: longint external name ''$Intern'';',
  15086. ' Bracket: longint external name ''["A B"]'';',
  15087. ' end;',
  15088. '']),
  15089. LinesToStr([
  15090. '']));
  15091. StartUnit(true);
  15092. Add([
  15093. 'interface',
  15094. 'uses unit2;',
  15095. '{$modeswitch externalclass}',
  15096. 'type',
  15097. ' TCar = class(tobject)',
  15098. ' public',
  15099. ' Intern2: longint external name ''$Intern2'';',
  15100. ' procedure DoIt;',
  15101. ' end;',
  15102. 'implementation',
  15103. 'procedure tcar.doit;',
  15104. 'begin',
  15105. ' Intern:=Intern+1;',
  15106. ' Intern2:=Intern2+2;',
  15107. ' Bracket:=Bracket+3;',
  15108. 'end;',
  15109. 'var Obj: TCar;',
  15110. 'begin',
  15111. ' obj.intern:=obj.intern+1;',
  15112. ' obj.intern2:=obj.intern2+2;',
  15113. ' obj.Bracket:=obj.Bracket+3;',
  15114. ' with obj do begin',
  15115. ' intern:=intern+1;',
  15116. ' intern2:=intern2+2;',
  15117. ' Bracket:=Bracket+3;',
  15118. ' end;']);
  15119. ConvertUnit;
  15120. CheckSource('TestClass_ExternalVar',
  15121. LinesToStr([
  15122. 'var $impl = $mod.$impl;',
  15123. 'rtl.createClass(this, "TCar", pas.unit2.TObject, function () {',
  15124. ' this.DoIt = function () {',
  15125. ' this.$Intern = this.$Intern + 1;',
  15126. ' this.$Intern2 = this.$Intern2 + 2;',
  15127. ' this["A B"] = this["A B"] + 3;',
  15128. ' };',
  15129. ' });',
  15130. '']),
  15131. LinesToStr([
  15132. '$impl.Obj.$Intern = $impl.Obj.$Intern + 1;',
  15133. '$impl.Obj.$Intern2 = $impl.Obj.$Intern2 + 2;',
  15134. '$impl.Obj["A B"] = $impl.Obj["A B"] + 3;',
  15135. 'var $with = $impl.Obj;',
  15136. '$with.$Intern = $with.$Intern + 1;',
  15137. '$with.$Intern2 = $with.$Intern2 + 2;',
  15138. '$with["A B"] = $with["A B"] + 3;',
  15139. '']),
  15140. LinesToStr([ // implementation
  15141. '$impl.Obj = null;',
  15142. '']));
  15143. end;
  15144. procedure TTestModule.TestClass_Const;
  15145. begin
  15146. StartProgram(false);
  15147. Add([
  15148. 'type',
  15149. ' integer = longint;',
  15150. ' TClass = class of TObject;',
  15151. ' TObject = class',
  15152. ' public',
  15153. ' const cI: integer = 3;',
  15154. ' procedure DoIt;',
  15155. ' class procedure DoMore;',
  15156. ' end;',
  15157. 'procedure tobject.doit;',
  15158. 'begin',
  15159. ' if cI=4 then;',
  15160. ' if 5=cI then;',
  15161. ' if Self.cI=6 then;',
  15162. ' if 7=Self.cI then;',
  15163. ' with Self do begin',
  15164. ' if cI=11 then;',
  15165. ' if 12=cI then;',
  15166. ' end;',
  15167. 'end;',
  15168. 'class procedure tobject.domore;',
  15169. 'begin',
  15170. ' if cI=8 then;',
  15171. ' if Self.cI=9 then;',
  15172. ' if 10=cI then;',
  15173. ' if 11=Self.cI then;',
  15174. ' with Self do begin',
  15175. ' if cI=13 then;',
  15176. ' if 14=cI then;',
  15177. ' end;',
  15178. 'end;',
  15179. 'var',
  15180. ' Obj: TObject;',
  15181. ' Cla: TClass;',
  15182. 'begin',
  15183. ' if TObject.cI=21 then ;',
  15184. ' if Obj.cI=22 then ;',
  15185. ' if Cla.cI=23 then ;',
  15186. ' with obj do if ci=24 then;',
  15187. ' with TObject do if ci=25 then;',
  15188. ' with Cla do if ci=26 then;']);
  15189. ConvertProgram;
  15190. CheckSource('TestClass_Const',
  15191. LinesToStr([
  15192. 'rtl.createClass(this, "TObject", null, function () {',
  15193. ' this.cI = 3;',
  15194. ' this.$init = function () {',
  15195. ' };',
  15196. ' this.$final = function () {',
  15197. ' };',
  15198. ' this.DoIt = function () {',
  15199. ' if (this.cI === 4) ;',
  15200. ' if (5 === this.cI) ;',
  15201. ' if (this.cI === 6) ;',
  15202. ' if (7 === this.cI) ;',
  15203. ' if (this.cI === 11) ;',
  15204. ' if (12 === this.cI) ;',
  15205. ' };',
  15206. ' this.DoMore = function () {',
  15207. ' if (this.cI === 8) ;',
  15208. ' if (this.cI === 9) ;',
  15209. ' if (10 === this.cI) ;',
  15210. ' if (11 === this.cI) ;',
  15211. ' if (this.cI === 13) ;',
  15212. ' if (14 === this.cI) ;',
  15213. ' };',
  15214. '});',
  15215. 'this.Obj = null;',
  15216. 'this.Cla = null;',
  15217. '']),
  15218. LinesToStr([
  15219. 'if ($mod.TObject.cI === 21) ;',
  15220. 'if ($mod.Obj.cI === 22) ;',
  15221. 'if ($mod.Cla.cI === 23) ;',
  15222. 'var $with = $mod.Obj;',
  15223. 'if ($with.cI === 24) ;',
  15224. 'var $with1 = $mod.TObject;',
  15225. 'if ($with1.cI === 25) ;',
  15226. 'var $with2 = $mod.Cla;',
  15227. 'if ($with2.cI === 26) ;',
  15228. '']));
  15229. end;
  15230. procedure TTestModule.TestClass_ConstEnum;
  15231. begin
  15232. StartProgram(false);
  15233. Add([
  15234. 'type',
  15235. ' TEnum = (red,blue);',
  15236. ' TObject = class',
  15237. ' end;',
  15238. ' TAnimal = class',
  15239. ' public',
  15240. ' type TSubEnum = (light,dark);',
  15241. ' const a = high(TEnum);',
  15242. ' const b = high(TSubEnum);',
  15243. ' end;',
  15244. ' TBird = class(TAnimal)',
  15245. ' public',
  15246. ' const c = high(TEnum);',
  15247. ' const d = high(TSubEnum);',
  15248. ' end;',
  15249. ' TAnt = class',
  15250. ' public',
  15251. ' const e = high(TEnum);',
  15252. ' const f = high(TBird.TSubEnum);',
  15253. ' end;',
  15254. 'begin',
  15255. '']);
  15256. ConvertProgram;
  15257. CheckSource('TestClass_ConstEnum',
  15258. LinesToStr([
  15259. 'this.TEnum = {',
  15260. ' "0": "red",',
  15261. ' red: 0,',
  15262. ' "1": "blue",',
  15263. ' blue: 1',
  15264. '};',
  15265. 'rtl.createClass(this, "TObject", null, function () {',
  15266. ' this.$init = function () {',
  15267. ' };',
  15268. ' this.$final = function () {',
  15269. ' };',
  15270. '});',
  15271. 'rtl.createClass(this, "TAnimal", this.TObject, function () {',
  15272. ' this.TSubEnum = {',
  15273. ' "0": "light",',
  15274. ' light: 0,',
  15275. ' "1": "dark",',
  15276. ' dark: 1',
  15277. ' };',
  15278. ' this.a = $mod.TEnum.blue;',
  15279. ' this.b = this.TSubEnum.dark;',
  15280. '});',
  15281. 'rtl.createClass(this, "TBird", this.TAnimal, function () {',
  15282. ' this.c = $mod.TEnum.blue;',
  15283. ' this.d = this.TSubEnum.dark;',
  15284. '});',
  15285. 'rtl.createClass(this, "TAnt", this.TObject, function () {',
  15286. ' this.e = $mod.TEnum.blue;',
  15287. ' this.f = $mod.TAnimal.TSubEnum.dark;',
  15288. '});',
  15289. '']),
  15290. LinesToStr([
  15291. '']));
  15292. end;
  15293. procedure TTestModule.TestClass_LocalConstDuplicate_Prg;
  15294. begin
  15295. StartProgram(false);
  15296. Add([
  15297. 'type',
  15298. ' TObject = class',
  15299. ' const cI: longint = 3;',
  15300. ' procedure Fly;',
  15301. ' procedure Run;',
  15302. ' end;',
  15303. ' TBird = class',
  15304. ' procedure Go;',
  15305. ' end;',
  15306. 'procedure tobject.fly;',
  15307. 'const cI: word = 4;',
  15308. 'begin',
  15309. ' if cI=Self.cI then ;',
  15310. 'end;',
  15311. 'procedure tobject.run;',
  15312. 'const cI: word = 5;',
  15313. 'begin',
  15314. ' if cI=Self.cI then ;',
  15315. 'end;',
  15316. 'procedure tbird.go;',
  15317. 'const cI: word = 6;',
  15318. 'begin',
  15319. ' if cI=Self.cI then ;',
  15320. 'end;',
  15321. 'begin',
  15322. '']);
  15323. ConvertProgram;
  15324. CheckSource('TestClass_LocalConstDuplicate_Prg',
  15325. LinesToStr([
  15326. 'rtl.createClass(this, "TObject", null, function () {',
  15327. ' this.cI = 3;',
  15328. ' this.$init = function () {',
  15329. ' };',
  15330. ' this.$final = function () {',
  15331. ' };',
  15332. ' var cI$1 = 4;',
  15333. ' this.Fly = function () {',
  15334. ' if (cI$1 === this.cI) ;',
  15335. ' };',
  15336. ' var cI$2 = 5;',
  15337. ' this.Run = function () {',
  15338. ' if (cI$2 === this.cI) ;',
  15339. ' };',
  15340. '});',
  15341. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15342. ' var cI$3 = 6;',
  15343. ' this.Go = function () {',
  15344. ' if (cI$3 === this.cI) ;',
  15345. ' };',
  15346. '});',
  15347. '']),
  15348. LinesToStr([
  15349. '']));
  15350. end;
  15351. procedure TTestModule.TestClass_LocalConstDuplicate_Unit;
  15352. begin
  15353. StartUnit(false);
  15354. Add([
  15355. 'interface',
  15356. 'type',
  15357. ' TObject = class',
  15358. ' const cI: longint = 3;',
  15359. ' procedure Fly;',
  15360. ' procedure Run;',
  15361. ' end;',
  15362. ' TBird = class',
  15363. ' procedure Go;',
  15364. ' end;',
  15365. 'implementation',
  15366. 'procedure tobject.fly;',
  15367. 'const cI: word = 4;',
  15368. 'begin',
  15369. ' if cI=Self.cI then ;',
  15370. 'end;',
  15371. 'procedure tobject.run;',
  15372. 'const cI: word = 5;',
  15373. 'begin',
  15374. ' if cI=Self.cI then ;',
  15375. 'end;',
  15376. 'procedure tbird.go;',
  15377. 'const cI: word = 6;',
  15378. 'begin',
  15379. ' if cI=Self.cI then ;',
  15380. 'end;',
  15381. '']);
  15382. ConvertUnit;
  15383. CheckSource('TestClass_LocalConstDuplicate_Unit',
  15384. LinesToStr([
  15385. 'rtl.createClass(this, "TObject", null, function () {',
  15386. ' this.cI = 3;',
  15387. ' this.$init = function () {',
  15388. ' };',
  15389. ' this.$final = function () {',
  15390. ' };',
  15391. ' var cI$1 = 4;',
  15392. ' this.Fly = function () {',
  15393. ' if (cI$1 === this.cI) ;',
  15394. ' };',
  15395. ' var cI$2 = 5;',
  15396. ' this.Run = function () {',
  15397. ' if (cI$2 === this.cI) ;',
  15398. ' };',
  15399. '});',
  15400. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15401. ' var cI$3 = 6;',
  15402. ' this.Go = function () {',
  15403. ' if (cI$3 === this.cI) ;',
  15404. ' };',
  15405. '});',
  15406. '']),
  15407. '',
  15408. '');
  15409. end;
  15410. procedure TTestModule.TestClass_LocalVarSelfFail;
  15411. begin
  15412. StartProgram(false);
  15413. Add([
  15414. 'type',
  15415. ' TObject = class',
  15416. ' constructor Create;',
  15417. ' end;',
  15418. 'constructor tobject.create;',
  15419. 'var self: longint;',
  15420. 'begin',
  15421. 'end',
  15422. 'begin',
  15423. '']);
  15424. SetExpectedPasResolverError('Duplicate identifier "self" at (0)',nDuplicateIdentifier);
  15425. ConvertProgram;
  15426. end;
  15427. procedure TTestModule.TestClass_ArgSelfFail;
  15428. begin
  15429. StartProgram(false);
  15430. Add([
  15431. 'type',
  15432. ' TObject = class',
  15433. ' procedure DoIt(Self: longint);',
  15434. ' end;',
  15435. 'procedure tobject.doit(self: longint);',
  15436. 'begin',
  15437. 'end',
  15438. 'begin',
  15439. '']);
  15440. SetExpectedPasResolverError('Duplicate identifier "Self" at test1.pp(5,24)',nDuplicateIdentifier);
  15441. ConvertProgram;
  15442. end;
  15443. procedure TTestModule.TestClass_NestedProcSelf;
  15444. begin
  15445. StartProgram(false);
  15446. Add([
  15447. 'type',
  15448. ' TObject = class',
  15449. ' Key: longint;',
  15450. ' class var State: longint;',
  15451. ' procedure DoIt;',
  15452. ' function GetSize: longint; virtual; abstract;',
  15453. ' procedure SetSize(Value: longint); virtual; abstract;',
  15454. ' property Size: longint read GetSize write SetSize;',
  15455. ' end;',
  15456. 'procedure tobject.doit;',
  15457. ' procedure Sub;',
  15458. ' begin',
  15459. ' key:=key+2;',
  15460. ' self.key:=self.key+3;',
  15461. ' state:=state+4;',
  15462. ' self.state:=self.state+5;',
  15463. ' tobject.state:=tobject.state+6;',
  15464. ' size:=size+7;',
  15465. ' self.size:=self.size+8;',
  15466. ' end;',
  15467. 'begin',
  15468. ' sub;',
  15469. ' key:=key+12;',
  15470. ' self.key:=self.key+13;',
  15471. ' state:=state+14;',
  15472. ' self.state:=self.state+15;',
  15473. ' tobject.state:=tobject.state+16;',
  15474. ' size:=size+17;',
  15475. ' self.size:=self.size+18;',
  15476. 'end;',
  15477. 'begin',
  15478. '']);
  15479. ConvertProgram;
  15480. CheckSource('TestClass_NestedProcSelf',
  15481. LinesToStr([ // statements
  15482. 'rtl.createClass(this, "TObject", null, function () {',
  15483. ' this.State = 0;',
  15484. ' this.$init = function () {',
  15485. ' this.Key = 0;',
  15486. ' };',
  15487. ' this.$final = function () {',
  15488. ' };',
  15489. ' this.DoIt = function () {',
  15490. ' var $Self = this;',
  15491. ' function Sub() {',
  15492. ' $Self.Key = $Self.Key + 2;',
  15493. ' $Self.Key = $Self.Key + 3;',
  15494. ' $mod.TObject.State = $Self.State + 4;',
  15495. ' $mod.TObject.State = $Self.State + 5;',
  15496. ' $mod.TObject.State = $mod.TObject.State + 6;',
  15497. ' $Self.SetSize($Self.GetSize() + 7);',
  15498. ' $Self.SetSize($Self.GetSize() + 8);',
  15499. ' };',
  15500. ' Sub();',
  15501. ' this.Key = this.Key + 12;',
  15502. ' $Self.Key = $Self.Key + 13;',
  15503. ' $mod.TObject.State = this.State + 14;',
  15504. ' $mod.TObject.State = $Self.State + 15;',
  15505. ' $mod.TObject.State = $mod.TObject.State + 16;',
  15506. ' this.SetSize(this.GetSize() + 17);',
  15507. ' $Self.SetSize($Self.GetSize() + 18);',
  15508. ' };',
  15509. '});',
  15510. '']),
  15511. LinesToStr([ // $mod.$main
  15512. '']));
  15513. end;
  15514. procedure TTestModule.TestClass_NestedProcSelf2;
  15515. begin
  15516. StartProgram(false);
  15517. Add([
  15518. 'type',
  15519. ' TObject = class',
  15520. ' Key: longint;',
  15521. ' class var State: longint;',
  15522. ' function GetSize: longint; virtual; abstract;',
  15523. ' procedure SetSize(Value: longint); virtual; abstract;',
  15524. ' property Size: longint read GetSize write SetSize;',
  15525. ' end;',
  15526. ' TBird = class',
  15527. ' procedure DoIt;',
  15528. ' end;',
  15529. 'procedure tbird.doit;',
  15530. ' procedure Sub;',
  15531. ' begin',
  15532. ' key:=key+2;',
  15533. ' self.key:=self.key+3;',
  15534. ' state:=state+4;',
  15535. ' self.state:=self.state+5;',
  15536. ' tobject.state:=tobject.state+6;',
  15537. ' size:=size+7;',
  15538. ' self.size:=self.size+8;',
  15539. ' end;',
  15540. 'begin',
  15541. ' sub;',
  15542. ' key:=key+12;',
  15543. ' self.key:=self.key+13;',
  15544. ' state:=state+14;',
  15545. ' self.state:=self.state+15;',
  15546. ' tobject.state:=tobject.state+16;',
  15547. ' size:=size+17;',
  15548. ' self.size:=self.size+18;',
  15549. 'end;',
  15550. 'begin',
  15551. '']);
  15552. ConvertProgram;
  15553. CheckSource('TestClass_NestedProcSelf2',
  15554. LinesToStr([ // statements
  15555. 'rtl.createClass(this, "TObject", null, function () {',
  15556. ' this.State = 0;',
  15557. ' this.$init = function () {',
  15558. ' this.Key = 0;',
  15559. ' };',
  15560. ' this.$final = function () {',
  15561. ' };',
  15562. '});',
  15563. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15564. ' this.DoIt = function () {',
  15565. ' var $Self = this;',
  15566. ' function Sub() {',
  15567. ' $Self.Key = $Self.Key + 2;',
  15568. ' $Self.Key = $Self.Key + 3;',
  15569. ' $mod.TObject.State = $Self.State + 4;',
  15570. ' $mod.TObject.State = $Self.State + 5;',
  15571. ' $mod.TObject.State = $mod.TObject.State + 6;',
  15572. ' $Self.SetSize($Self.GetSize() + 7);',
  15573. ' $Self.SetSize($Self.GetSize() + 8);',
  15574. ' };',
  15575. ' Sub();',
  15576. ' this.Key = this.Key + 12;',
  15577. ' $Self.Key = $Self.Key + 13;',
  15578. ' $mod.TObject.State = this.State + 14;',
  15579. ' $mod.TObject.State = $Self.State + 15;',
  15580. ' $mod.TObject.State = $mod.TObject.State + 16;',
  15581. ' this.SetSize(this.GetSize() + 17);',
  15582. ' $Self.SetSize($Self.GetSize() + 18);',
  15583. ' };',
  15584. '});',
  15585. '']),
  15586. LinesToStr([ // $mod.$main
  15587. '']));
  15588. end;
  15589. procedure TTestModule.TestClass_NestedProcClassSelf;
  15590. begin
  15591. StartProgram(false);
  15592. Add([
  15593. 'type',
  15594. ' TObject = class',
  15595. ' class var State: longint;',
  15596. ' class procedure DoIt;',
  15597. ' class function GetSize: longint; virtual; abstract;',
  15598. ' class procedure SetSize(Value: longint); virtual; abstract;',
  15599. ' class property Size: longint read GetSize write SetSize;',
  15600. ' end;',
  15601. 'class procedure tobject.doit;',
  15602. ' procedure Sub;',
  15603. ' begin',
  15604. ' state:=state+2;',
  15605. ' self.state:=self.state+3;',
  15606. ' tobject.state:=tobject.state+4;',
  15607. ' size:=size+5;',
  15608. ' self.size:=self.size+6;',
  15609. ' tobject.size:=tobject.size+7;',
  15610. ' end;',
  15611. 'begin',
  15612. ' sub;',
  15613. ' state:=state+12;',
  15614. ' self.state:=self.state+13;',
  15615. ' tobject.state:=tobject.state+14;',
  15616. ' size:=size+15;',
  15617. ' self.size:=self.size+16;',
  15618. ' tobject.size:=tobject.size+17;',
  15619. 'end;',
  15620. 'begin',
  15621. '']);
  15622. ConvertProgram;
  15623. CheckSource('TestClass_NestedProcClassSelf',
  15624. LinesToStr([ // statements
  15625. 'rtl.createClass(this, "TObject", null, function () {',
  15626. ' this.State = 0;',
  15627. ' this.$init = function () {',
  15628. ' };',
  15629. ' this.$final = function () {',
  15630. ' };',
  15631. ' this.DoIt = function () {',
  15632. ' var $Self = this;',
  15633. ' function Sub() {',
  15634. ' $mod.TObject.State = $Self.State + 2;',
  15635. ' $mod.TObject.State = $Self.State + 3;',
  15636. ' $mod.TObject.State = $mod.TObject.State + 4;',
  15637. ' $Self.SetSize($Self.GetSize() + 5);',
  15638. ' $Self.SetSize($Self.GetSize() + 6);',
  15639. ' $mod.TObject.SetSize($mod.TObject.GetSize() + 7);',
  15640. ' };',
  15641. ' Sub();',
  15642. ' $mod.TObject.State = this.State + 12;',
  15643. ' $mod.TObject.State = $Self.State + 13;',
  15644. ' $mod.TObject.State = $mod.TObject.State + 14;',
  15645. ' this.SetSize(this.GetSize() + 15);',
  15646. ' $Self.SetSize($Self.GetSize() + 16);',
  15647. ' $mod.TObject.SetSize($mod.TObject.GetSize() + 17);',
  15648. ' };',
  15649. '});',
  15650. '']),
  15651. LinesToStr([ // $mod.$main
  15652. '']));
  15653. end;
  15654. procedure TTestModule.TestClass_NestedProcCallInherited;
  15655. begin
  15656. StartProgram(false);
  15657. Add([
  15658. 'type',
  15659. ' TObject = class',
  15660. ' function DoIt(k: boolean): longint; virtual;',
  15661. ' end;',
  15662. ' TBird = class',
  15663. ' function DoIt(k: boolean): longint; override;',
  15664. ' end;',
  15665. 'function tobject.doit(k: boolean): longint;',
  15666. 'begin',
  15667. 'end;',
  15668. 'function tbird.doit(k: boolean): longint;',
  15669. ' procedure Sub;',
  15670. ' begin',
  15671. ' inherited DoIt(true);',
  15672. //' if inherited DoIt(false)=4 then ;',
  15673. ' end;',
  15674. 'begin',
  15675. ' Sub;',
  15676. ' inherited;',
  15677. ' inherited DoIt(true);',
  15678. //' if inherited DoIt(false)=14 then ;',
  15679. 'end;',
  15680. 'begin',
  15681. '']);
  15682. ConvertProgram;
  15683. CheckSource('TestClass_NestedProcCallInherited',
  15684. LinesToStr([ // statements
  15685. 'rtl.createClass(this, "TObject", null, function () {',
  15686. ' this.$init = function () {',
  15687. ' };',
  15688. ' this.$final = function () {',
  15689. ' };',
  15690. ' this.DoIt = function (k) {',
  15691. ' var Result = 0;',
  15692. ' return Result;',
  15693. ' };',
  15694. '});',
  15695. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15696. ' this.DoIt = function (k) {',
  15697. ' var $Self = this;',
  15698. ' var Result = 0;',
  15699. ' function Sub() {',
  15700. ' $mod.TObject.DoIt.call($Self, true);',
  15701. ' };',
  15702. ' Sub();',
  15703. ' $mod.TObject.DoIt.apply(this, arguments);',
  15704. ' $mod.TObject.DoIt.call(this, true);',
  15705. ' return Result;',
  15706. ' };',
  15707. '});',
  15708. '']),
  15709. LinesToStr([ // $mod.$main
  15710. '']));
  15711. end;
  15712. procedure TTestModule.TestClass_TObjectFree;
  15713. begin
  15714. StartProgram(false);
  15715. Add([
  15716. 'type',
  15717. ' TObject = class',
  15718. ' Obj: tobject;',
  15719. ' procedure Free;',
  15720. ' procedure Release;',
  15721. ' end;',
  15722. 'procedure tobject.free;',
  15723. 'begin',
  15724. 'end;',
  15725. 'procedure tobject.release;',
  15726. 'begin',
  15727. ' free;',
  15728. ' if true then free;',
  15729. 'end;',
  15730. 'function DoIt(o: tobject): tobject;',
  15731. 'var l: tobject;',
  15732. 'begin',
  15733. ' o.free;',
  15734. ' o.free();',
  15735. ' l.free;',
  15736. ' l.free();',
  15737. ' o.obj.free;',
  15738. ' o.obj.free();',
  15739. ' with o do obj.free;',
  15740. ' with o do obj.free();',
  15741. ' result.Free;',
  15742. ' result.Free();',
  15743. 'end;',
  15744. 'var o: tobject;',
  15745. ' a: array of tobject;',
  15746. 'begin',
  15747. ' o.free;',
  15748. ' o.obj.free;',
  15749. ' a[1+2].free;',
  15750. '']);
  15751. ConvertProgram;
  15752. CheckSource('TestClass_TObjectFree',
  15753. LinesToStr([ // statements
  15754. 'rtl.createClass(this, "TObject", null, function () {',
  15755. ' this.$init = function () {',
  15756. ' this.Obj = null;',
  15757. ' };',
  15758. ' this.$final = function () {',
  15759. ' this.Obj = undefined;',
  15760. ' };',
  15761. ' this.Free = function () {',
  15762. ' };',
  15763. ' this.Release = function () {',
  15764. ' this.Free();',
  15765. ' if (true) this.Free();',
  15766. ' };',
  15767. '});',
  15768. 'this.DoIt = function (o) {',
  15769. ' var Result = null;',
  15770. ' var l = null;',
  15771. ' o = rtl.freeLoc(o);',
  15772. ' o = rtl.freeLoc(o);',
  15773. ' l = rtl.freeLoc(l);',
  15774. ' l = rtl.freeLoc(l);',
  15775. ' rtl.free(o, "Obj");',
  15776. ' rtl.free(o, "Obj");',
  15777. ' rtl.free(o, "Obj");',
  15778. ' rtl.free(o, "Obj");',
  15779. ' Result = rtl.freeLoc(Result);',
  15780. ' Result = rtl.freeLoc(Result);',
  15781. ' return Result;',
  15782. '};',
  15783. 'this.o = null;',
  15784. 'this.a = [];',
  15785. '']),
  15786. LinesToStr([ // $mod.$main
  15787. 'rtl.free($mod, "o");',
  15788. 'rtl.free($mod.o, "Obj");',
  15789. 'rtl.free($mod.a, 1 + 2);',
  15790. '']));
  15791. end;
  15792. procedure TTestModule.TestClass_TObjectFree_VarArg;
  15793. begin
  15794. StartProgram(false);
  15795. Add([
  15796. 'type',
  15797. ' TObject = class',
  15798. ' Obj: tobject;',
  15799. ' procedure Free;',
  15800. ' end;',
  15801. 'procedure tobject.free;',
  15802. 'begin',
  15803. 'end;',
  15804. 'procedure DoIt(var o: tobject);',
  15805. 'begin',
  15806. ' o.free;',
  15807. ' o.free();',
  15808. 'end;',
  15809. 'begin',
  15810. '']);
  15811. ConvertProgram;
  15812. CheckSource('TestClass_TObjectFree_VarArg',
  15813. LinesToStr([ // statements
  15814. 'rtl.createClass(this, "TObject", null, function () {',
  15815. ' this.$init = function () {',
  15816. ' this.Obj = null;',
  15817. ' };',
  15818. ' this.$final = function () {',
  15819. ' this.Obj = undefined;',
  15820. ' };',
  15821. ' this.Free = function () {',
  15822. ' };',
  15823. '});',
  15824. 'this.DoIt = function (o) {',
  15825. ' o.set(rtl.freeLoc(o.get()));',
  15826. ' o.set(rtl.freeLoc(o.get()));',
  15827. '};',
  15828. '']),
  15829. LinesToStr([ // $mod.$main
  15830. '']));
  15831. end;
  15832. procedure TTestModule.TestClass_TObjectFreeNewInstance;
  15833. begin
  15834. StartProgram(false);
  15835. Add([
  15836. 'type',
  15837. ' TObject = class',
  15838. ' constructor Create;',
  15839. ' procedure Free;',
  15840. ' end;',
  15841. 'constructor TObject.Create; begin end;',
  15842. 'procedure tobject.free; begin end;',
  15843. 'begin',
  15844. ' with tobject.create do free;',
  15845. '']);
  15846. ConvertProgram;
  15847. CheckSource('TestClass_TObjectFreeNewInstance',
  15848. LinesToStr([ // statements
  15849. 'rtl.createClass(this, "TObject", null, function () {',
  15850. ' this.$init = function () {',
  15851. ' };',
  15852. ' this.$final = function () {',
  15853. ' };',
  15854. ' this.Create = function () {',
  15855. ' return this;',
  15856. ' };',
  15857. ' this.Free = function () {',
  15858. ' };',
  15859. '});',
  15860. '']),
  15861. LinesToStr([ // $mod.$main
  15862. 'var $with = $mod.TObject.$create("Create");',
  15863. '$with=rtl.freeLoc($with);',
  15864. '']));
  15865. end;
  15866. procedure TTestModule.TestClass_TObjectFreeLowerCase;
  15867. begin
  15868. StartProgram(false);
  15869. Add([
  15870. 'type',
  15871. ' TObject = class',
  15872. ' destructor Destroy;',
  15873. ' procedure Free;',
  15874. ' end;',
  15875. 'destructor TObject.Destroy; begin end;',
  15876. 'procedure tobject.free; begin end;',
  15877. 'var o: tobject;',
  15878. 'begin',
  15879. ' o.free;',
  15880. '']);
  15881. Converter.UseLowerCase:=true;
  15882. ConvertProgram;
  15883. CheckSource('TestClass_TObjectFreeLowerCase',
  15884. LinesToStr([ // statements
  15885. 'rtl.createClass(this, "tobject", null, function () {',
  15886. ' this.$init = function () {',
  15887. ' };',
  15888. ' this.$final = function () {',
  15889. ' };',
  15890. ' rtl.tObjectDestroy = "destroy";',
  15891. ' this.destroy = function () {',
  15892. ' };',
  15893. ' this.free = function () {',
  15894. ' };',
  15895. '});',
  15896. 'this.o = null;',
  15897. '']),
  15898. LinesToStr([ // $mod.$main
  15899. 'rtl.free($mod, "o");',
  15900. '']));
  15901. end;
  15902. procedure TTestModule.TestClass_TObjectFreeFunctionFail;
  15903. begin
  15904. StartProgram(false);
  15905. Add([
  15906. 'type',
  15907. ' TObject = class',
  15908. ' procedure Free;',
  15909. ' function GetObj: tobject; virtual; abstract;',
  15910. ' end;',
  15911. 'procedure tobject.free;',
  15912. 'begin',
  15913. 'end;',
  15914. 'var o: tobject;',
  15915. 'begin',
  15916. ' o.getobj.free;',
  15917. '']);
  15918. SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
  15919. ConvertProgram;
  15920. end;
  15921. procedure TTestModule.TestClass_TObjectFreePropertyFail;
  15922. begin
  15923. StartProgram(false);
  15924. Add([
  15925. 'type',
  15926. ' TObject = class',
  15927. ' procedure Free;',
  15928. ' FObj: TObject;',
  15929. ' property Obj: tobject read FObj write FObj;',
  15930. ' end;',
  15931. 'procedure tobject.free;',
  15932. 'begin',
  15933. 'end;',
  15934. 'var o: tobject;',
  15935. 'begin',
  15936. ' o.obj.free;',
  15937. '']);
  15938. SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
  15939. ConvertProgram;
  15940. end;
  15941. procedure TTestModule.TestClass_ForIn;
  15942. begin
  15943. StartProgram(false);
  15944. Add([
  15945. 'type',
  15946. ' TObject = class end;',
  15947. ' TItem = TObject;',
  15948. ' TEnumerator = class',
  15949. ' FCurrent: TItem;',
  15950. ' property Current: TItem read FCurrent;',
  15951. ' function MoveNext: boolean;',
  15952. ' end;',
  15953. ' TBird = class',
  15954. ' function GetEnumerator: TEnumerator;',
  15955. ' end;',
  15956. 'function TEnumerator.MoveNext: boolean;',
  15957. 'begin',
  15958. 'end;',
  15959. 'function TBird.GetEnumerator: TEnumerator;',
  15960. 'begin',
  15961. 'end;',
  15962. 'var',
  15963. ' b: TBird;',
  15964. ' i, i2: TItem;',
  15965. 'begin',
  15966. ' for i in b do i2:=i;']);
  15967. ConvertProgram;
  15968. CheckSource('TestClass_ForIn',
  15969. LinesToStr([ // statements
  15970. 'rtl.createClass(this, "TObject", null, function () {',
  15971. ' this.$init = function () {',
  15972. ' };',
  15973. ' this.$final = function () {',
  15974. ' };',
  15975. '});',
  15976. 'rtl.createClass(this, "TEnumerator", this.TObject, function () {',
  15977. ' this.$init = function () {',
  15978. ' $mod.TObject.$init.call(this);',
  15979. ' this.FCurrent = null;',
  15980. ' };',
  15981. ' this.$final = function () {',
  15982. ' this.FCurrent = undefined;',
  15983. ' $mod.TObject.$final.call(this);',
  15984. ' };',
  15985. ' this.MoveNext = function () {',
  15986. ' var Result = false;',
  15987. ' return Result;',
  15988. ' };',
  15989. '});',
  15990. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15991. ' this.GetEnumerator = function () {',
  15992. ' var Result = null;',
  15993. ' return Result;',
  15994. ' };',
  15995. '});',
  15996. 'this.b = null;',
  15997. 'this.i = null;',
  15998. 'this.i2 = null;'
  15999. ]),
  16000. LinesToStr([ // $mod.$main
  16001. 'var $in = $mod.b.GetEnumerator();',
  16002. 'try {',
  16003. ' while ($in.MoveNext()){',
  16004. ' $mod.i = $in.FCurrent;',
  16005. ' $mod.i2 = $mod.i;',
  16006. ' }',
  16007. '} finally {',
  16008. ' $in = rtl.freeLoc($in)',
  16009. '};',
  16010. '']));
  16011. end;
  16012. procedure TTestModule.TestClass_DispatchMessage;
  16013. begin
  16014. StartProgram(false);
  16015. Add([
  16016. 'type',
  16017. ' TObject = class',
  16018. ' {$DispatchField DispInt}',
  16019. ' procedure Dispatch(var Msg); virtual; abstract;',
  16020. ' {$DispatchStrField DispStr}',
  16021. ' procedure DispatchStr(var Msg); virtual; abstract;',
  16022. ' end;',
  16023. ' THopMsg = record',
  16024. ' DispInt: longint;',
  16025. ' end;',
  16026. ' TPutMsg = record',
  16027. ' DispStr: string;',
  16028. ' end;',
  16029. ' TBird = class',
  16030. ' procedure Fly(var Msg); virtual; abstract; message 2;',
  16031. ' procedure Run; overload; virtual; abstract;',
  16032. ' procedure Run(var Msg); overload; message ''Fast'';',
  16033. ' procedure Hop(var Msg: THopMsg); virtual; abstract; message 3;',
  16034. ' procedure Put(var Msg: TPutMsg); virtual; abstract; message ''foo'';',
  16035. ' end;',
  16036. 'procedure TBird.Run(var Msg);',
  16037. 'begin',
  16038. 'end;',
  16039. 'begin',
  16040. '']);
  16041. ConvertProgram;
  16042. CheckSource('TestClass_Message',
  16043. LinesToStr([ // statements
  16044. 'rtl.createClass(this, "TObject", null, function () {',
  16045. ' this.$init = function () {',
  16046. ' };',
  16047. ' this.$final = function () {',
  16048. ' };',
  16049. '});',
  16050. 'rtl.recNewT(this, "THopMsg", function () {',
  16051. ' this.DispInt = 0;',
  16052. ' this.$eq = function (b) {',
  16053. ' return this.DispInt === b.DispInt;',
  16054. ' };',
  16055. ' this.$assign = function (s) {',
  16056. ' this.DispInt = s.DispInt;',
  16057. ' return this;',
  16058. ' };',
  16059. '});',
  16060. 'rtl.recNewT(this, "TPutMsg", function () {',
  16061. ' this.DispStr = "";',
  16062. ' this.$eq = function (b) {',
  16063. ' return this.DispStr === b.DispStr;',
  16064. ' };',
  16065. ' this.$assign = function (s) {',
  16066. ' this.DispStr = s.DispStr;',
  16067. ' return this;',
  16068. ' };',
  16069. '});',
  16070. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16071. ' this.Run$1 = function (Msg) {',
  16072. ' };',
  16073. ' this.$msgint = {',
  16074. ' "2": "Fly",',
  16075. ' "3": "Hop"',
  16076. ' };',
  16077. ' this.$msgstr = {',
  16078. ' Fast: "Run$1",',
  16079. ' foo: "Put"',
  16080. ' };',
  16081. '});',
  16082. '']),
  16083. LinesToStr([ // $mod.$main
  16084. '']));
  16085. end;
  16086. procedure TTestModule.TestClass_Message_DuplicateIntFail;
  16087. begin
  16088. StartProgram(false);
  16089. Add([
  16090. 'type',
  16091. ' TObject = class',
  16092. ' procedure Fly(var Msg); virtual; abstract; message 3;',
  16093. ' procedure Run(var Msg); virtual; abstract; message 1+2;',
  16094. ' end;',
  16095. 'begin',
  16096. '']);
  16097. SetExpectedPasResolverError('Duplicate message id "3" at test1.pp(5,56)',nDuplicateMessageIdXAtY);
  16098. ConvertProgram;
  16099. end;
  16100. procedure TTestModule.TestClass_DispatchMessage_WrongFieldNameFail;
  16101. begin
  16102. StartProgram(false);
  16103. Add([
  16104. 'type',
  16105. ' TObject = class',
  16106. ' {$dispatchfield Msg}',
  16107. ' procedure Dispatch(var Msg); virtual; abstract;',
  16108. ' end;',
  16109. ' TFlyMsg = record',
  16110. ' FlyId: longint;',
  16111. ' end;',
  16112. ' TBird = class',
  16113. ' procedure Fly(var Msg: TFlyMsg); virtual; abstract; message 3;',
  16114. ' end;',
  16115. 'begin',
  16116. '']);
  16117. ConvertProgram;
  16118. CheckHint(mtWarning,nDispatchRequiresX,'Dispatch requires record field "Msg"');
  16119. end;
  16120. procedure TTestModule.TestClassOf_Create;
  16121. begin
  16122. StartProgram(false);
  16123. Add('type');
  16124. Add(' TObject = class');
  16125. Add(' constructor Create;');
  16126. Add(' end;');
  16127. Add(' TClass = class of TObject;');
  16128. Add('constructor tobject.create; begin end;');
  16129. Add('var');
  16130. Add(' Obj: tobject;');
  16131. Add(' C: tclass;');
  16132. Add('begin');
  16133. Add(' obj:=C.create;');
  16134. Add(' with c do obj:=create;');
  16135. ConvertProgram;
  16136. CheckSource('TestClassOf_Create',
  16137. LinesToStr([ // statements
  16138. 'rtl.createClass(this, "TObject", null, function () {',
  16139. ' this.$init = function () {',
  16140. ' };',
  16141. ' this.$final = function () {',
  16142. ' };',
  16143. ' this.Create = function () {',
  16144. ' return this;',
  16145. ' };',
  16146. '});',
  16147. 'this.Obj = null;',
  16148. 'this.C = null;'
  16149. ]),
  16150. LinesToStr([ // $mod.$main
  16151. '$mod.Obj = $mod.C.$create("Create");',
  16152. 'var $with = $mod.C;',
  16153. '$mod.Obj = $with.$create("Create");',
  16154. '']));
  16155. end;
  16156. procedure TTestModule.TestClassOf_Call;
  16157. begin
  16158. StartProgram(false);
  16159. Add('type');
  16160. Add(' TObject = class');
  16161. Add(' class procedure DoIt;');
  16162. Add(' end;');
  16163. Add(' TClass = class of TObject;');
  16164. Add('class procedure tobject.doit; begin end;');
  16165. Add('var');
  16166. Add(' C: tclass;');
  16167. Add('begin');
  16168. Add(' c.doit;');
  16169. Add(' with c do doit;');
  16170. ConvertProgram;
  16171. CheckSource('TestClassOf_Call',
  16172. LinesToStr([ // statements
  16173. 'rtl.createClass(this, "TObject", null, function () {',
  16174. ' this.$init = function () {',
  16175. ' };',
  16176. ' this.$final = function () {',
  16177. ' };',
  16178. ' this.DoIt = function () {',
  16179. ' };',
  16180. '});',
  16181. 'this.C = null;'
  16182. ]),
  16183. LinesToStr([ // $mod.$main
  16184. '$mod.C.DoIt();',
  16185. 'var $with = $mod.C;',
  16186. '$with.DoIt();',
  16187. '']));
  16188. end;
  16189. procedure TTestModule.TestClassOf_Assign;
  16190. begin
  16191. StartProgram(false);
  16192. Add('type');
  16193. Add(' TClass = class of TObject;');
  16194. Add(' TObject = class');
  16195. Add(' ClassType: TClass; ');
  16196. Add(' end;');
  16197. Add('var');
  16198. Add(' Obj: tobject;');
  16199. Add(' C: tclass;');
  16200. Add('begin');
  16201. Add(' c:=nil;');
  16202. Add(' c:=obj.classtype;');
  16203. ConvertProgram;
  16204. CheckSource('TestClassOf_Assign',
  16205. LinesToStr([ // statements
  16206. 'rtl.createClass(this, "TObject", null, function () {',
  16207. ' this.$init = function () {',
  16208. ' this.ClassType = null;',
  16209. ' };',
  16210. ' this.$final = function () {',
  16211. ' this.ClassType = undefined;',
  16212. ' };',
  16213. '});',
  16214. 'this.Obj = null;',
  16215. 'this.C = null;'
  16216. ]),
  16217. LinesToStr([ // $mod.$main
  16218. '$mod.C = null;',
  16219. '$mod.C = $mod.Obj.ClassType;',
  16220. '']));
  16221. end;
  16222. procedure TTestModule.TestClassOf_Is;
  16223. begin
  16224. StartProgram(false);
  16225. Add('type');
  16226. Add(' TClass = class of TObject;');
  16227. Add(' TObject = class');
  16228. Add(' end;');
  16229. Add(' TCar = class');
  16230. Add(' end;');
  16231. Add(' TCars = class of TCar;');
  16232. Add('var');
  16233. Add(' Obj: tobject;');
  16234. Add(' C: tclass;');
  16235. Add(' Cars: tcars;');
  16236. Add('begin');
  16237. Add(' if c is tcar then ;');
  16238. Add(' if c is tcars then ;');
  16239. ConvertProgram;
  16240. CheckSource('TestClassOf_Is',
  16241. LinesToStr([ // statements
  16242. 'rtl.createClass(this, "TObject", null, function () {',
  16243. ' this.$init = function () {',
  16244. ' };',
  16245. ' this.$final = function () {',
  16246. ' };',
  16247. '});',
  16248. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  16249. '});',
  16250. 'this.Obj = null;',
  16251. 'this.C = null;',
  16252. 'this.Cars = null;'
  16253. ]),
  16254. LinesToStr([ // $mod.$main
  16255. 'if(rtl.is($mod.C,$mod.TCar));',
  16256. 'if(rtl.is($mod.C,$mod.TCar));',
  16257. '']));
  16258. end;
  16259. procedure TTestModule.TestClassOf_Compare;
  16260. begin
  16261. StartProgram(false);
  16262. Add('type');
  16263. Add(' TClass = class of TObject;');
  16264. Add(' TObject = class');
  16265. Add(' ClassType: TClass; ');
  16266. Add(' end;');
  16267. Add('var');
  16268. Add(' b: boolean;');
  16269. Add(' Obj: tobject;');
  16270. Add(' C: tclass;');
  16271. Add('begin');
  16272. Add(' b:=c=nil;');
  16273. Add(' b:=nil=c;');
  16274. Add(' b:=c=obj.classtype;');
  16275. Add(' b:=obj.classtype=c;');
  16276. Add(' b:=c=TObject;');
  16277. Add(' b:=TObject=c;');
  16278. Add(' b:=c<>nil;');
  16279. Add(' b:=nil<>c;');
  16280. Add(' b:=c<>obj.classtype;');
  16281. Add(' b:=obj.classtype<>c;');
  16282. Add(' b:=c<>TObject;');
  16283. Add(' b:=TObject<>c;');
  16284. ConvertProgram;
  16285. CheckSource('TestClassOf_Compare',
  16286. LinesToStr([ // statements
  16287. 'rtl.createClass(this, "TObject", null, function () {',
  16288. ' this.$init = function () {',
  16289. ' this.ClassType = null;',
  16290. ' };',
  16291. ' this.$final = function () {',
  16292. ' this.ClassType = undefined;',
  16293. ' };',
  16294. '});',
  16295. 'this.b = false;',
  16296. 'this.Obj = null;',
  16297. 'this.C = null;'
  16298. ]),
  16299. LinesToStr([ // $mod.$main
  16300. '$mod.b = $mod.C === null;',
  16301. '$mod.b = null === $mod.C;',
  16302. '$mod.b = $mod.C === $mod.Obj.ClassType;',
  16303. '$mod.b = $mod.Obj.ClassType === $mod.C;',
  16304. '$mod.b = $mod.C === $mod.TObject;',
  16305. '$mod.b = $mod.TObject === $mod.C;',
  16306. '$mod.b = $mod.C !== null;',
  16307. '$mod.b = null !== $mod.C;',
  16308. '$mod.b = $mod.C !== $mod.Obj.ClassType;',
  16309. '$mod.b = $mod.Obj.ClassType !== $mod.C;',
  16310. '$mod.b = $mod.C !== $mod.TObject;',
  16311. '$mod.b = $mod.TObject !== $mod.C;',
  16312. '']));
  16313. end;
  16314. procedure TTestModule.TestClassOf_ClassVar;
  16315. begin
  16316. StartProgram(false);
  16317. Add('type');
  16318. Add(' TObject = class');
  16319. Add(' class var id: longint;');
  16320. Add(' end;');
  16321. Add(' TClass = class of TObject;');
  16322. Add('var');
  16323. Add(' C: tclass;');
  16324. Add('begin');
  16325. Add(' C.id:=C.id;');
  16326. ConvertProgram;
  16327. CheckSource('TestClassOf_ClassVar',
  16328. LinesToStr([ // statements
  16329. 'rtl.createClass(this, "TObject", null, function () {',
  16330. ' this.id = 0;',
  16331. ' this.$init = function () {',
  16332. ' };',
  16333. ' this.$final = function () {',
  16334. ' };',
  16335. '});',
  16336. 'this.C = null;'
  16337. ]),
  16338. LinesToStr([ // $mod.$main
  16339. '$mod.TObject.id = $mod.C.id;',
  16340. '']));
  16341. end;
  16342. procedure TTestModule.TestClassOf_ClassMethod;
  16343. begin
  16344. StartProgram(false);
  16345. Add('type');
  16346. Add(' TObject = class');
  16347. Add(' class function DoIt(i: longint = 0): longint;');
  16348. Add(' end;');
  16349. Add(' TClass = class of TObject;');
  16350. Add('class function tobject.doit(i: longint = 0): longint; begin end;');
  16351. Add('var');
  16352. Add(' i: longint;');
  16353. Add(' C: tclass;');
  16354. Add('begin');
  16355. Add(' C.DoIt;');
  16356. Add(' C.DoIt();');
  16357. Add(' i:=C.DoIt;');
  16358. Add(' i:=C.DoIt();');
  16359. ConvertProgram;
  16360. CheckSource('TestClassOf_ClassMethod',
  16361. LinesToStr([ // statements
  16362. 'rtl.createClass(this, "TObject", null, function () {',
  16363. ' this.$init = function () {',
  16364. ' };',
  16365. ' this.$final = function () {',
  16366. ' };',
  16367. ' this.DoIt = function (i) {',
  16368. ' var Result = 0;',
  16369. ' return Result;',
  16370. ' };',
  16371. '});',
  16372. 'this.i = 0;',
  16373. 'this.C = null;'
  16374. ]),
  16375. LinesToStr([ // $mod.$main
  16376. '$mod.C.DoIt(0);',
  16377. '$mod.C.DoIt(0);',
  16378. '$mod.i = $mod.C.DoIt(0);',
  16379. '$mod.i = $mod.C.DoIt(0);',
  16380. '']));
  16381. end;
  16382. procedure TTestModule.TestClassOf_ClassProperty;
  16383. begin
  16384. StartProgram(false);
  16385. Add([
  16386. 'type',
  16387. ' TObject = class',
  16388. ' class var FA: longint;',
  16389. ' class function GetA: longint;',
  16390. ' class procedure SetA(Value: longint);',
  16391. ' class property pA: longint read fa write fa;',
  16392. ' class property pB: longint read geta write seta;',
  16393. ' end;',
  16394. ' TObjectClass = class of tobject;',
  16395. 'class function tobject.geta: longint; begin end;',
  16396. 'class procedure tobject.seta(value: longint); begin end;',
  16397. 'var',
  16398. ' b: boolean;',
  16399. ' Obj: tobject;',
  16400. ' Cla: tobjectclass;',
  16401. 'begin',
  16402. ' obj.pa:=obj.pa;',
  16403. ' obj.pb:=obj.pb;',
  16404. ' b:=obj.pa=4;',
  16405. ' b:=obj.pb=obj.pb;',
  16406. ' b:=5=obj.pa;',
  16407. ' cla.pa:=6;',
  16408. ' cla.pa:=cla.pa;',
  16409. ' cla.pb:=cla.pb;',
  16410. ' b:=cla.pa=7;',
  16411. ' b:=cla.pb=cla.pb;',
  16412. ' b:=8=cla.pa;',
  16413. ' tobject.pa:=9;',
  16414. ' tobject.pb:=tobject.pb;',
  16415. ' b:=tobject.pa=10;',
  16416. ' b:=11=tobject.pa;',
  16417. '']);
  16418. ConvertProgram;
  16419. CheckSource('TestClassOf_ClassProperty',
  16420. LinesToStr([ // statements
  16421. 'rtl.createClass(this, "TObject", null, function () {',
  16422. ' this.FA = 0;',
  16423. ' this.$init = function () {',
  16424. ' };',
  16425. ' this.$final = function () {',
  16426. ' };',
  16427. ' this.GetA = function () {',
  16428. ' var Result = 0;',
  16429. ' return Result;',
  16430. ' };',
  16431. ' this.SetA = function (Value) {',
  16432. ' };',
  16433. '});',
  16434. 'this.b = false;',
  16435. 'this.Obj = null;',
  16436. 'this.Cla = null;'
  16437. ]),
  16438. LinesToStr([ // $mod.$main
  16439. '$mod.TObject.FA = $mod.Obj.FA;',
  16440. '$mod.Obj.$class.SetA($mod.Obj.$class.GetA());',
  16441. '$mod.b = $mod.Obj.FA === 4;',
  16442. '$mod.b = $mod.Obj.$class.GetA() === $mod.Obj.$class.GetA();',
  16443. '$mod.b = 5 === $mod.Obj.FA;',
  16444. '$mod.TObject.FA = 6;',
  16445. '$mod.TObject.FA = $mod.Cla.FA;',
  16446. '$mod.Cla.SetA($mod.Cla.GetA());',
  16447. '$mod.b = $mod.Cla.FA === 7;',
  16448. '$mod.b = $mod.Cla.GetA() === $mod.Cla.GetA();',
  16449. '$mod.b = 8 === $mod.Cla.FA;',
  16450. '$mod.TObject.FA = 9;',
  16451. '$mod.TObject.SetA($mod.TObject.GetA());',
  16452. '$mod.b = $mod.TObject.FA === 10;',
  16453. '$mod.b = 11 === $mod.TObject.FA;',
  16454. '']));
  16455. end;
  16456. procedure TTestModule.TestClassOf_ClassMethodSelf;
  16457. begin
  16458. StartProgram(false);
  16459. Add('type');
  16460. Add(' TObject = class');
  16461. Add(' class var GlobalId: longint;');
  16462. Add(' class procedure ProcA;');
  16463. Add(' end;');
  16464. Add('class procedure tobject.proca;');
  16465. Add('var b: boolean;');
  16466. Add('begin');
  16467. Add(' b:=self=nil;');
  16468. Add(' b:=self.globalid=3;');
  16469. Add(' b:=4=self.globalid;');
  16470. Add(' self.globalid:=5;');
  16471. Add(' self.proca;');
  16472. Add('end;');
  16473. Add('begin');
  16474. ConvertProgram;
  16475. CheckSource('TestClassOf_ClassMethodSelf',
  16476. LinesToStr([ // statements
  16477. 'rtl.createClass(this, "TObject", null, function () {',
  16478. ' this.GlobalId = 0;',
  16479. ' this.$init = function () {',
  16480. ' };',
  16481. ' this.$final = function () {',
  16482. ' };',
  16483. ' this.ProcA = function () {',
  16484. ' var b = false;',
  16485. ' b = this === null;',
  16486. ' b = this.GlobalId === 3;',
  16487. ' b = 4 === this.GlobalId;',
  16488. ' $mod.TObject.GlobalId = 5;',
  16489. ' this.ProcA();',
  16490. ' };',
  16491. '});'
  16492. ]),
  16493. LinesToStr([ // $mod.$main
  16494. '']));
  16495. end;
  16496. procedure TTestModule.TestClassOf_TypeCast;
  16497. begin
  16498. StartProgram(false);
  16499. Add('type');
  16500. Add(' TObject = class');
  16501. Add(' class procedure {#TObject_DoIt}DoIt;');
  16502. Add(' end;');
  16503. Add(' TClass = class of TObject;');
  16504. Add(' TMobile = class');
  16505. Add(' class procedure {#TMobile_DoIt}DoIt;');
  16506. Add(' end;');
  16507. Add(' TMobileClass = class of TMobile;');
  16508. Add(' TCar = class(TMobile)');
  16509. Add(' class procedure {#TCar_DoIt}DoIt;');
  16510. Add(' end;');
  16511. Add(' TCarClass = class of TCar;');
  16512. Add('class procedure TObject.DoIt;');
  16513. Add('begin');
  16514. Add(' TClass(Self).{@TObject_DoIt}DoIt;');
  16515. Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
  16516. Add('end;');
  16517. Add('class procedure TMobile.DoIt;');
  16518. Add('begin');
  16519. Add(' TClass(Self).{@TObject_DoIt}DoIt;');
  16520. Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
  16521. Add(' TCarClass(Self).{@TCar_DoIt}DoIt;');
  16522. Add('end;');
  16523. Add('class procedure TCar.DoIt; begin end;');
  16524. Add('var');
  16525. Add(' ObjC: TClass;');
  16526. Add(' MobileC: TMobileClass;');
  16527. Add(' CarC: TCarClass;');
  16528. Add('begin');
  16529. Add(' ObjC.{@TObject_DoIt}DoIt;');
  16530. Add(' MobileC.{@TMobile_DoIt}DoIt;');
  16531. Add(' CarC.{@TCar_DoIt}DoIt;');
  16532. Add(' TClass(ObjC).{@TObject_DoIt}DoIt;');
  16533. Add(' TMobileClass(ObjC).{@TMobile_DoIt}DoIt;');
  16534. Add(' TCarClass(ObjC).{@TCar_DoIt}DoIt;');
  16535. Add(' TClass(MobileC).{@TObject_DoIt}DoIt;');
  16536. Add(' TMobileClass(MobileC).{@TMobile_DoIt}DoIt;');
  16537. Add(' TCarClass(MobileC).{@TCar_DoIt}DoIt;');
  16538. Add(' TClass(CarC).{@TObject_DoIt}DoIt;');
  16539. Add(' TMobileClass(CarC).{@TMobile_DoIt}DoIt;');
  16540. Add(' TCarClass(CarC).{@TCar_DoIt}DoIt;');
  16541. ConvertProgram;
  16542. CheckSource('TestClassOf_TypeCast',
  16543. LinesToStr([ // statements
  16544. 'rtl.createClass(this, "TObject", null, function () {',
  16545. ' this.$init = function () {',
  16546. ' };',
  16547. ' this.$final = function () {',
  16548. ' };',
  16549. ' this.DoIt = function () {',
  16550. ' this.DoIt();',
  16551. ' this.DoIt$1();',
  16552. ' };',
  16553. '});',
  16554. 'rtl.createClass(this, "TMobile", this.TObject, function () {',
  16555. ' this.DoIt$1 = function () {',
  16556. ' this.DoIt();',
  16557. ' this.DoIt$1();',
  16558. ' this.DoIt$2();',
  16559. ' };',
  16560. '});',
  16561. 'rtl.createClass(this, "TCar", this.TMobile, function () {',
  16562. ' this.DoIt$2 = function () {',
  16563. ' };',
  16564. '});',
  16565. 'this.ObjC = null;',
  16566. 'this.MobileC = null;',
  16567. 'this.CarC = null;',
  16568. '']),
  16569. LinesToStr([ // $mod.$main
  16570. '$mod.ObjC.DoIt();',
  16571. '$mod.MobileC.DoIt$1();',
  16572. '$mod.CarC.DoIt$2();',
  16573. '$mod.ObjC.DoIt();',
  16574. '$mod.ObjC.DoIt$1();',
  16575. '$mod.ObjC.DoIt$2();',
  16576. '$mod.MobileC.DoIt();',
  16577. '$mod.MobileC.DoIt$1();',
  16578. '$mod.MobileC.DoIt$2();',
  16579. '$mod.CarC.DoIt();',
  16580. '$mod.CarC.DoIt$1();',
  16581. '$mod.CarC.DoIt$2();',
  16582. '']));
  16583. end;
  16584. procedure TTestModule.TestClassOf_ImplicitFunctionCall;
  16585. begin
  16586. StartProgram(false);
  16587. Add('type');
  16588. Add(' TObject = class');
  16589. Add(' function CurNow: longint; ');
  16590. Add(' class function Now: longint; ');
  16591. Add(' end;');
  16592. Add('function TObject.CurNow: longint; begin end;');
  16593. Add('class function TObject.Now: longint; begin end;');
  16594. Add('var');
  16595. Add(' Obj: tobject;');
  16596. Add(' vI: longint;');
  16597. Add('begin');
  16598. Add(' obj.curnow;');
  16599. Add(' vi:=obj.curnow;');
  16600. Add(' tobject.now;');
  16601. Add(' vi:=tobject.now;');
  16602. ConvertProgram;
  16603. CheckSource('TestClassOf_ImplicitFunctionCall',
  16604. LinesToStr([ // statements
  16605. 'rtl.createClass(this, "TObject", null, function () {',
  16606. ' this.$init = function () {',
  16607. ' };',
  16608. ' this.$final = function () {',
  16609. ' };',
  16610. ' this.CurNow = function () {',
  16611. ' var Result = 0;',
  16612. ' return Result;',
  16613. ' };',
  16614. ' this.Now = function () {',
  16615. ' var Result = 0;',
  16616. ' return Result;',
  16617. ' };',
  16618. '});',
  16619. 'this.Obj = null;',
  16620. 'this.vI = 0;',
  16621. '']),
  16622. LinesToStr([ // $mod.$main
  16623. '$mod.Obj.CurNow();',
  16624. '$mod.vI = $mod.Obj.CurNow();',
  16625. '$mod.TObject.Now();',
  16626. '$mod.vI = $mod.TObject.Now();',
  16627. '']));
  16628. end;
  16629. procedure TTestModule.TestClassOf_Const;
  16630. begin
  16631. StartProgram(false);
  16632. Add([
  16633. 'type',
  16634. ' TObject = class',
  16635. ' end;',
  16636. ' TBird = TObject;',
  16637. ' TBirds = class of TBird;',
  16638. ' TEagles = TBirds;',
  16639. ' THawk = class(TBird);',
  16640. 'const',
  16641. ' Hawk: TEagles = THawk;',
  16642. ' DefaultBirdClasses : Array [1..2] of TEagles = (',
  16643. ' TBird,',
  16644. ' THawk',
  16645. ' );',
  16646. 'begin']);
  16647. ConvertProgram;
  16648. CheckSource('TestClassOf_Const',
  16649. LinesToStr([ // statements
  16650. 'rtl.createClass(this, "TObject", null, function () {',
  16651. ' this.$init = function () {',
  16652. ' };',
  16653. ' this.$final = function () {',
  16654. ' };',
  16655. '});',
  16656. 'rtl.createClass(this, "THawk", this.TObject, function () {',
  16657. '});',
  16658. 'this.Hawk = this.THawk;',
  16659. 'this.DefaultBirdClasses = [this.TObject, this.THawk];',
  16660. '']),
  16661. LinesToStr([ // $mod.$main
  16662. '']));
  16663. end;
  16664. procedure TTestModule.TestNestedClass_Alias;
  16665. begin
  16666. WithTypeInfo:=true;
  16667. StartProgram(false);
  16668. Add([
  16669. 'type',
  16670. ' TObject = class',
  16671. ' type TNested = type longint;',
  16672. ' end;',
  16673. 'type TAlias = type tobject.tnested;',
  16674. 'var i: tobject.tnested = 3;',
  16675. 'var j: TAlias = 4;',
  16676. 'begin',
  16677. ' if typeinfo(TAlias)=nil then ;',
  16678. ' if typeinfo(tobject.tnested)=nil then ;',
  16679. '']);
  16680. ConvertProgram;
  16681. CheckSource('TestNestedClass_Alias',
  16682. LinesToStr([ // statements
  16683. 'rtl.createClass(this, "TObject", null, function () {',
  16684. ' $mod.$rtti.$inherited("TObject.TNested", rtl.longint, {});',
  16685. ' this.$init = function () {',
  16686. ' };',
  16687. ' this.$final = function () {',
  16688. ' };',
  16689. '});',
  16690. 'this.$rtti.$inherited("TAlias", this.$rtti["TObject.TNested"], {});',
  16691. 'this.i = 3;',
  16692. 'this.j = 4;',
  16693. '']),
  16694. LinesToStr([ // $mod.$main
  16695. 'if ($mod.$rtti["TAlias"] === null) ;',
  16696. 'if ($mod.$rtti["TObject.TNested"] === null) ;',
  16697. '']));
  16698. end;
  16699. procedure TTestModule.TestNestedClass_Record;
  16700. begin
  16701. WithTypeInfo:=true;
  16702. StartProgram(false);
  16703. Add([
  16704. 'type',
  16705. ' TObject = class',
  16706. ' type TPoint = record',
  16707. ' x,y: byte;',
  16708. ' end;',
  16709. ' procedure DoIt(t: TPoint);',
  16710. ' end;',
  16711. 'procedure tobject.DoIt(t: TPoint);',
  16712. 'var p: TPoint;',
  16713. 'begin',
  16714. ' t.x:=t.y;',
  16715. ' p:=t;',
  16716. 'end;',
  16717. 'var',
  16718. ' p: tobject.tpoint = (x:2; y:4);',
  16719. ' o: TObject;',
  16720. 'begin',
  16721. ' p:=p;',
  16722. ' o.doit(p);',
  16723. '']);
  16724. ConvertProgram;
  16725. CheckSource('TestNestedClass_Record',
  16726. LinesToStr([ // statements
  16727. 'rtl.createClass(this, "TObject", null, function () {',
  16728. ' rtl.recNewT(this, "TPoint", function () {',
  16729. ' this.x = 0;',
  16730. ' this.y = 0;',
  16731. ' this.$eq = function (b) {',
  16732. ' return (this.x === b.x) && (this.y === b.y);',
  16733. ' };',
  16734. ' this.$assign = function (s) {',
  16735. ' this.x = s.x;',
  16736. ' this.y = s.y;',
  16737. ' return this;',
  16738. ' };',
  16739. ' var $r = $mod.$rtti.$Record("TObject.TPoint", {});',
  16740. ' $r.addField("x", rtl.byte);',
  16741. ' $r.addField("y", rtl.byte);',
  16742. ' });',
  16743. ' this.$init = function () {',
  16744. ' };',
  16745. ' this.$final = function () {',
  16746. ' };',
  16747. ' this.DoIt = function (t) {',
  16748. ' var p = this.TPoint.$new();',
  16749. ' t.x = t.y;',
  16750. ' p.$assign(t);',
  16751. ' };',
  16752. '});',
  16753. 'this.p = this.TObject.TPoint.$clone({',
  16754. ' x: 2,',
  16755. ' y: 4',
  16756. '});',
  16757. 'this.o = null;',
  16758. '']),
  16759. LinesToStr([ // $mod.$main
  16760. '$mod.p.$assign($mod.p);',
  16761. '$mod.o.DoIt($mod.TObject.TPoint.$clone($mod.p));',
  16762. '']));
  16763. end;
  16764. procedure TTestModule.TestNestedClass_Class;
  16765. begin
  16766. WithTypeInfo:=true;
  16767. StartProgram(false);
  16768. Add([
  16769. 'type',
  16770. ' TObject = class end;',
  16771. ' TBird = class',
  16772. ' type TLeg = class',
  16773. ' FId: longint;',
  16774. ' constructor Create;',
  16775. ' function Create(i: longint): TLeg;',
  16776. ' end;',
  16777. ' function DoIt(b: TBird): Tleg;',
  16778. ' end;',
  16779. 'constructor tbird.tleg.create;',
  16780. 'begin',
  16781. ' FId:=3;',
  16782. 'end;',
  16783. 'function tbird.tleg.Create(i: longint): TLeg;',
  16784. 'begin',
  16785. ' Create;',
  16786. ' Result:=TLeg.Create;',
  16787. ' Result:=TBird.TLeg.Create;',
  16788. ' Result:=Create(3);',
  16789. ' FId:=i;',
  16790. 'end;',
  16791. 'function tbird.DoIt(b: tbird): tleg;',
  16792. 'begin',
  16793. ' Result.Create;',
  16794. ' Result:=TLeg.Create;',
  16795. ' Result:=TBird.TLeg.Create;',
  16796. ' Result:=Result.Create(3);',
  16797. 'end;',
  16798. 'var',
  16799. ' b: Tbird.tleg;',
  16800. 'begin',
  16801. ' b.Create;',
  16802. ' b:=TBird.TLeg.Create;',
  16803. ' b:=b.Create(3);',
  16804. '']);
  16805. ConvertProgram;
  16806. CheckSource('TestNestedClass_Class',
  16807. LinesToStr([ // statements
  16808. 'rtl.createClass(this, "TObject", null, function () {',
  16809. ' this.$init = function () {',
  16810. ' };',
  16811. ' this.$final = function () {',
  16812. ' };',
  16813. '});',
  16814. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16815. ' rtl.createClass(this, "TLeg", $mod.TObject, function () {',
  16816. ' this.$init = function () {',
  16817. ' $mod.TObject.$init.call(this);',
  16818. ' this.FId = 0;',
  16819. ' };',
  16820. ' this.Create = function () {',
  16821. ' this.FId = 3;',
  16822. ' return this;',
  16823. ' };',
  16824. ' this.Create$1 = function (i) {',
  16825. ' var Result = null;',
  16826. ' this.Create();',
  16827. ' Result = $mod.TBird.TLeg.$create("Create");',
  16828. ' Result = $mod.TBird.TLeg.$create("Create");',
  16829. ' Result = this.Create$1(3);',
  16830. ' this.FId = i;',
  16831. ' return Result;',
  16832. ' };',
  16833. ' }, "TBird.TLeg");',
  16834. ' this.DoIt = function (b) {',
  16835. ' var Result = null;',
  16836. ' Result.Create();',
  16837. ' Result = this.TLeg.$create("Create");',
  16838. ' Result = $mod.TBird.TLeg.$create("Create");',
  16839. ' Result = Result.Create$1(3);',
  16840. ' return Result;',
  16841. ' };',
  16842. '});',
  16843. 'this.b = null;',
  16844. '']),
  16845. LinesToStr([ // $mod.$main
  16846. '$mod.b.Create();',
  16847. '$mod.b = $mod.TBird.TLeg.$create("Create");',
  16848. '$mod.b = $mod.b.Create$1(3);',
  16849. '']));
  16850. end;
  16851. procedure TTestModule.TestExternalClass_Var;
  16852. begin
  16853. StartProgram(false);
  16854. Add([
  16855. '{$modeswitch externalclass}',
  16856. 'type',
  16857. ' TExtA = class external name ''ExtObj''',
  16858. ' Id: longint external name ''$Id'';',
  16859. ' B: longint;',
  16860. ' end;',
  16861. 'var Obj: TExtA;',
  16862. 'begin',
  16863. ' obj.id:=obj.id+1;',
  16864. ' obj.B:=obj.B+1;']);
  16865. ConvertProgram;
  16866. CheckSource('TestExternalClass_Var',
  16867. LinesToStr([ // statements
  16868. 'this.Obj = null;',
  16869. '']),
  16870. LinesToStr([ // $mod.$main
  16871. '$mod.Obj.$Id = $mod.Obj.$Id + 1;',
  16872. '$mod.Obj.B = $mod.Obj.B + 1;',
  16873. '']));
  16874. end;
  16875. procedure TTestModule.TestExternalClass_Const;
  16876. begin
  16877. StartProgram(false);
  16878. Add([
  16879. '{$modeswitch externalclass}',
  16880. 'type',
  16881. ' TExtA = class external name ''ExtObj''',
  16882. ' const Two: longint = 2;',
  16883. ' const Three = 3;',
  16884. ' const Id: longint;',
  16885. ' end;',
  16886. ' TExtB = class external name ''ExtB''',
  16887. ' A: TExtA;',
  16888. ' end;',
  16889. 'var',
  16890. ' A: texta;',
  16891. ' B: textb;',
  16892. ' i: longint;',
  16893. 'begin',
  16894. ' i:=a.two;',
  16895. ' i:=texta.two;',
  16896. ' i:=a.three;',
  16897. ' i:=texta.three;',
  16898. ' i:=a.id;',
  16899. ' i:=texta.id;',
  16900. '']);
  16901. ConvertProgram;
  16902. CheckSource('TestExternalClass_Const',
  16903. LinesToStr([ // statements
  16904. 'this.A = null;',
  16905. 'this.B = null;',
  16906. 'this.i = 0;',
  16907. '']),
  16908. LinesToStr([ // $mod.$main
  16909. '$mod.i = 2;',
  16910. '$mod.i = 2;',
  16911. '$mod.i = 3;',
  16912. '$mod.i = 3;',
  16913. '$mod.i = $mod.A.Id;',
  16914. '$mod.i = ExtObj.Id;',
  16915. '']));
  16916. end;
  16917. procedure TTestModule.TestExternalClass_Dollar;
  16918. begin
  16919. StartProgram(false);
  16920. Add([
  16921. '{$modeswitch externalclass}',
  16922. 'type',
  16923. ' TExtA = class external name ''$''',
  16924. ' Id: longint external name ''$'';',
  16925. ' function Bla(i: longint): longint; external name ''$'';',
  16926. ' end;',
  16927. 'function dollar(k: longint): longint; external name ''$'';',
  16928. 'var Obj: TExtA;',
  16929. 'begin',
  16930. ' dollar(1);',
  16931. ' obj.id:=obj.id+2;',
  16932. ' obj.Bla(3);',
  16933. '']);
  16934. ConvertProgram;
  16935. CheckSource('TestExternalClass_Dollar',
  16936. LinesToStr([ // statements
  16937. 'this.Obj = null;',
  16938. '']),
  16939. LinesToStr([ // $mod.$main
  16940. '$(1);',
  16941. '$mod.Obj.$ = $mod.Obj.$ + 2;',
  16942. '$mod.Obj.$(3);',
  16943. '']));
  16944. end;
  16945. procedure TTestModule.TestExternalClass_DuplicateVarFail;
  16946. begin
  16947. StartProgram(false);
  16948. Add('{$modeswitch externalclass}');
  16949. Add('type');
  16950. Add(' TExtA = class external name ''ExtA''');
  16951. Add(' Id: longint external name ''$Id'';');
  16952. Add(' end;');
  16953. Add(' TExtB = class external ''lib'' name ''ExtB''(TExtA)');
  16954. Add(' Id: longint;');
  16955. Add(' end;');
  16956. Add('begin');
  16957. SetExpectedPasResolverError('Duplicate identifier "Id" at test1.pp(6,5)',nDuplicateIdentifier);
  16958. ConvertProgram;
  16959. end;
  16960. procedure TTestModule.TestExternalClass_Method;
  16961. begin
  16962. StartProgram(false);
  16963. Add(['{$modeswitch externalclass}',
  16964. 'type',
  16965. ' TExtA = class external name ''ExtObj''',
  16966. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  16967. ' procedure DoSome(Id: longint = 1);',
  16968. ' end;',
  16969. 'var Obj: texta;',
  16970. 'begin',
  16971. ' obj.doit;',
  16972. ' obj.doit();',
  16973. ' obj.doit(2);',
  16974. ' with obj do begin',
  16975. ' doit;',
  16976. ' doit();',
  16977. ' doit(3);',
  16978. ' end;']);
  16979. ConvertProgram;
  16980. CheckSource('TestExternalClass_Method',
  16981. LinesToStr([ // statements
  16982. 'this.Obj = null;',
  16983. '']),
  16984. LinesToStr([ // $mod.$main
  16985. '$mod.Obj.$Execute(1);',
  16986. '$mod.Obj.$Execute(1);',
  16987. '$mod.Obj.$Execute(2);',
  16988. 'var $with = $mod.Obj;',
  16989. '$with.$Execute(1);',
  16990. '$with.$Execute(1);',
  16991. '$with.$Execute(3);',
  16992. '']));
  16993. end;
  16994. procedure TTestModule.TestExternalClass_ClassMethod;
  16995. begin
  16996. StartProgram(false);
  16997. Add([
  16998. '{$modeswitch externalclass}',
  16999. 'type',
  17000. ' TExtA = class external name ''ExtObj''',
  17001. ' class procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  17002. ' end;',
  17003. ' TExtB = TExtA;',
  17004. 'var p: Pointer;',
  17005. 'begin',
  17006. ' texta.doit;',
  17007. ' texta.doit();',
  17008. ' texta.doit(2);',
  17009. ' p:[email protected];',
  17010. ' with texta do begin',
  17011. ' doit;',
  17012. ' doit();',
  17013. ' doit(3);',
  17014. ' p:=@DoIt;',
  17015. ' end;',
  17016. ' textb.doit;',
  17017. ' textb.doit();',
  17018. ' textb.doit(4);',
  17019. ' with textb do begin',
  17020. ' doit;',
  17021. ' doit();',
  17022. ' doit(5);',
  17023. ' end;',
  17024. '']);
  17025. ConvertProgram;
  17026. CheckSource('TestExternalClass_ClassMethod',
  17027. LinesToStr([ // statements
  17028. 'this.p = null;',
  17029. '']),
  17030. LinesToStr([ // $mod.$main
  17031. 'ExtObj.$Execute(1);',
  17032. 'ExtObj.$Execute(1);',
  17033. 'ExtObj.$Execute(2);',
  17034. '$mod.p = rtl.createCallback(ExtObj, "$Execute");',
  17035. 'ExtObj.$Execute(1);',
  17036. 'ExtObj.$Execute(1);',
  17037. 'ExtObj.$Execute(3);',
  17038. '$mod.p = rtl.createCallback(ExtObj, "$Execute");',
  17039. 'ExtObj.$Execute(1);',
  17040. 'ExtObj.$Execute(1);',
  17041. 'ExtObj.$Execute(4);',
  17042. 'ExtObj.$Execute(1);',
  17043. 'ExtObj.$Execute(1);',
  17044. 'ExtObj.$Execute(5);',
  17045. '']));
  17046. end;
  17047. procedure TTestModule.TestExternalClass_ClassMethodStatic;
  17048. begin
  17049. StartProgram(false);
  17050. Add([
  17051. '{$modeswitch externalclass}',
  17052. 'type',
  17053. ' TExtA = class external name ''ExtObj''',
  17054. ' class procedure DoIt(Id: longint = 1); static;',
  17055. ' end;',
  17056. 'var p: Pointer;',
  17057. 'begin',
  17058. ' texta.doit;',
  17059. ' texta.doit();',
  17060. ' texta.doit(2);',
  17061. ' p:[email protected];',
  17062. ' with texta do begin',
  17063. ' doit;',
  17064. ' doit();',
  17065. ' doit(3);',
  17066. ' p:=@DoIt;',
  17067. ' end;',
  17068. '']);
  17069. ConvertProgram;
  17070. CheckSource('TestExternalClass_ClassMethodStatic',
  17071. LinesToStr([ // statements
  17072. 'this.p = null;',
  17073. '']),
  17074. LinesToStr([ // $mod.$main
  17075. 'ExtObj.DoIt(1);',
  17076. 'ExtObj.DoIt(1);',
  17077. 'ExtObj.DoIt(2);',
  17078. '$mod.p = ExtObj.DoIt;',
  17079. 'ExtObj.DoIt(1);',
  17080. 'ExtObj.DoIt(1);',
  17081. 'ExtObj.DoIt(3);',
  17082. '$mod.p = ExtObj.DoIt;',
  17083. '']));
  17084. end;
  17085. procedure TTestModule.TestExternalClass_FunctionResultInTypeCast;
  17086. begin
  17087. StartProgram(false);
  17088. Add([
  17089. '{$modeswitch externalclass}',
  17090. 'type',
  17091. ' TBird = class external name ''Array''',
  17092. ' end;',
  17093. 'function GetPtr: Pointer;',
  17094. 'begin',
  17095. 'end;',
  17096. 'procedure Write(const p);',
  17097. 'begin',
  17098. 'end;',
  17099. 'procedure WriteLn; varargs;',
  17100. 'begin',
  17101. 'end;',
  17102. 'begin',
  17103. ' if TBird(GetPtr)=nil then ;',
  17104. ' Write(GetPtr);',
  17105. ' WriteLn(GetPtr);',
  17106. ' Write(TBird(GetPtr));',
  17107. ' WriteLn(TBird(GetPtr));',
  17108. '']);
  17109. ConvertProgram;
  17110. CheckSource('TestFunctionResultInTypeCast',
  17111. LinesToStr([ // statements
  17112. 'this.GetPtr = function () {',
  17113. ' var Result = null;',
  17114. ' return Result;',
  17115. '};',
  17116. 'this.Write = function (p) {',
  17117. '};',
  17118. 'this.WriteLn = function () {',
  17119. '};',
  17120. '']),
  17121. LinesToStr([
  17122. 'if ($mod.GetPtr() === null) ;',
  17123. '$mod.Write($mod.GetPtr());',
  17124. '$mod.WriteLn($mod.GetPtr());',
  17125. '$mod.Write($mod.GetPtr());',
  17126. '$mod.WriteLn($mod.GetPtr());',
  17127. '']));
  17128. end;
  17129. procedure TTestModule.TestExternalClass_NonExternalOverride;
  17130. begin
  17131. StartProgram(false);
  17132. Add([
  17133. '{$modeswitch externalclass}',
  17134. 'type',
  17135. ' TExtA = class external name ''ExtObjA''',
  17136. ' procedure ProcA; virtual;',
  17137. ' procedure ProcB; virtual;',
  17138. ' end;',
  17139. ' TExtB = class external name ''ExtObjB'' (TExtA)',
  17140. ' end;',
  17141. ' TExtC = class (TExtB)',
  17142. ' procedure ProcA; override;',
  17143. ' end;',
  17144. 'procedure TExtC.ProcA;',
  17145. 'begin',
  17146. ' ProcA;',
  17147. ' Self.ProcA;',
  17148. ' ProcB;',
  17149. ' Self.ProcB;',
  17150. 'end;',
  17151. 'var',
  17152. ' A: texta;',
  17153. ' B: textb;',
  17154. ' C: textc;',
  17155. 'begin',
  17156. ' a.proca;',
  17157. ' b.proca;',
  17158. ' c.proca;']);
  17159. ConvertProgram;
  17160. CheckSource('TestExternalClass_NonExternalOverride',
  17161. LinesToStr([ // statements
  17162. 'rtl.createClassExt(this, "TExtC", ExtObjB, "", function () {',
  17163. ' this.$init = function () {',
  17164. ' };',
  17165. ' this.$final = function () {',
  17166. ' };',
  17167. ' this.ProcA = function () {',
  17168. ' this.ProcA();',
  17169. ' this.ProcA();',
  17170. ' this.ProcB();',
  17171. ' this.ProcB();',
  17172. ' };',
  17173. '});',
  17174. 'this.A = null;',
  17175. 'this.B = null;',
  17176. 'this.C = null;',
  17177. '']),
  17178. LinesToStr([ // $mod.$main
  17179. '$mod.A.ProcA();',
  17180. '$mod.B.ProcA();',
  17181. '$mod.C.ProcA();',
  17182. '']));
  17183. end;
  17184. procedure TTestModule.TestExternalClass_OverloadHint;
  17185. begin
  17186. StartProgram(false);
  17187. Add([
  17188. '{$modeswitch externalclass}',
  17189. 'type',
  17190. ' TExtA = class external name ''ExtObjA''',
  17191. ' procedure DoIt;',
  17192. ' procedure DoIt(i: longint);',
  17193. ' end;',
  17194. 'begin',
  17195. '']);
  17196. ConvertProgram;
  17197. CheckResolverUnexpectedHints(true);
  17198. CheckSource('TestExternalClass_OverloadHint',
  17199. LinesToStr([ // statements
  17200. '']),
  17201. LinesToStr([ // $mod.$main
  17202. '']));
  17203. end;
  17204. procedure TTestModule.TestExternalClass_SameNamePublishedProperty;
  17205. begin
  17206. WithTypeInfo:=true;
  17207. StartProgram(false);
  17208. Add([
  17209. '{$modeswitch externalclass}',
  17210. 'type',
  17211. ' JSwiper = class external name ''Swiper''',
  17212. ' constructor New;',
  17213. ' end;',
  17214. ' TObject = class',
  17215. ' private',
  17216. ' FSwiper: JSwiper;',
  17217. ' published',
  17218. ' property Swiper: JSwiper read FSwiper write FSwiper;',
  17219. ' end;',
  17220. 'begin',
  17221. ' JSwiper.new;',
  17222. '']);
  17223. ConvertProgram;
  17224. CheckSource('TestExternalClass_SameNamePublishedProperty',
  17225. LinesToStr([ // statements
  17226. 'this.$rtti.$ExtClass("JSwiper", {',
  17227. ' jsclass: "Swiper"',
  17228. '});',
  17229. 'rtl.createClass(this, "TObject", null, function () {',
  17230. ' this.$init = function () {',
  17231. ' this.FSwiper = null;',
  17232. ' };',
  17233. ' this.$final = function () {',
  17234. ' this.FSwiper = undefined;',
  17235. ' };',
  17236. ' var $r = this.$rtti;',
  17237. ' $r.addProperty("Swiper", 0, $mod.$rtti["JSwiper"], "FSwiper", "FSwiper");',
  17238. '});',
  17239. '']),
  17240. LinesToStr([ // $mod.$main
  17241. 'new Swiper();',
  17242. '']));
  17243. end;
  17244. procedure TTestModule.TestExternalClass_Property;
  17245. begin
  17246. StartProgram(false);
  17247. Add([
  17248. '{$modeswitch externalclass}',
  17249. 'type',
  17250. ' TExtA = class external name ''ExtA''',
  17251. ' function getYear: longint;',
  17252. ' procedure setYear(Value: longint);',
  17253. ' property Year: longint read getyear write setyear;',
  17254. ' end;',
  17255. ' TExtB = class (TExtA)',
  17256. ' procedure OtherSetYear(Value: longint);',
  17257. ' property year write othersetyear;',
  17258. ' end;',
  17259. 'procedure textb.othersetyear(value: longint);',
  17260. 'begin',
  17261. ' setYear(Value+4);',
  17262. 'end;',
  17263. 'var',
  17264. ' A: texta;',
  17265. ' B: textb;',
  17266. 'begin',
  17267. ' a.year:=a.year+1;',
  17268. ' b.year:=b.year+2;']);
  17269. ConvertProgram;
  17270. CheckSource('TestExternalClass_NonExternalOverride',
  17271. LinesToStr([ // statements
  17272. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  17273. ' this.$init = function () {',
  17274. ' };',
  17275. ' this.$final = function () {',
  17276. ' };',
  17277. ' this.OtherSetYear = function (Value) {',
  17278. ' this.setYear(Value+4);',
  17279. ' };',
  17280. '});',
  17281. 'this.A = null;',
  17282. 'this.B = null;',
  17283. '']),
  17284. LinesToStr([ // $mod.$main
  17285. '$mod.A.setYear($mod.A.getYear()+1);',
  17286. '$mod.B.OtherSetYear($mod.B.getYear()+2);',
  17287. '']));
  17288. end;
  17289. procedure TTestModule.TestExternalClass_PropertyDate;
  17290. begin
  17291. StartProgram(false);
  17292. Add([
  17293. '{$modeswitch externalclass}',
  17294. 'type',
  17295. ' TExtA = class external name ''ExtA''',
  17296. ' end;',
  17297. ' TExtB = class (TExtA)',
  17298. ' FDate: string;',
  17299. ' property Date: string read FDate write FDate;',
  17300. ' property ExtA: string read FDate write FDate;',
  17301. ' end;',
  17302. ' {$M+}',
  17303. ' TObject = class',
  17304. ' FDate: string;',
  17305. ' published',
  17306. ' property Date: string read FDate write FDate;',
  17307. ' property ExtA: string read FDate write FDate;',
  17308. ' end;',
  17309. 'var',
  17310. ' B: textb;',
  17311. ' o: TObject;',
  17312. 'begin',
  17313. ' b.date:=b.exta;',
  17314. ' o.date:=o.exta;']);
  17315. ConvertProgram;
  17316. CheckSource('TestExternalClass_PropertyDate',
  17317. LinesToStr([ // statements
  17318. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  17319. ' this.$init = function () {',
  17320. ' this.FDate = "";',
  17321. ' };',
  17322. ' this.$final = function () {',
  17323. ' };',
  17324. '});',
  17325. 'rtl.createClass(this, "TObject", null, function () {',
  17326. ' this.$init = function () {',
  17327. ' this.FDate = "";',
  17328. ' };',
  17329. ' this.$final = function () {',
  17330. ' };',
  17331. ' var $r = this.$rtti;',
  17332. ' $r.addField("FDate", rtl.string);',
  17333. ' $r.addProperty("Date", 0, rtl.string, "FDate", "FDate");',
  17334. ' $r.addProperty("ExtA", 0, rtl.string, "FDate", "FDate");',
  17335. '});',
  17336. 'this.B = null;',
  17337. 'this.o = null;',
  17338. '']),
  17339. LinesToStr([ // $mod.$main
  17340. '$mod.B.FDate = $mod.B.FDate;',
  17341. '$mod.o.FDate = $mod.o.FDate;',
  17342. '']));
  17343. end;
  17344. procedure TTestModule.TestExternalClass_ClassProperty;
  17345. begin
  17346. StartProgram(false);
  17347. Add('{$modeswitch externalclass}');
  17348. Add('type');
  17349. Add(' TExtA = class external name ''ExtA''');
  17350. Add(' class function getYear: longint;');
  17351. Add(' class procedure setYear(Value: longint);');
  17352. Add(' class property Year: longint read getyear write setyear;');
  17353. Add(' end;');
  17354. Add(' TExtB = class (TExtA)');
  17355. Add(' class function GetCentury: longint;');
  17356. Add(' class procedure SetCentury(Value: longint);');
  17357. Add(' class property Century: longint read getcentury write setcentury;');
  17358. Add(' end;');
  17359. Add('class function textb.getcentury: longint;');
  17360. Add('begin');
  17361. Add('end;');
  17362. Add('class procedure textb.setcentury(value: longint);');
  17363. Add('begin');
  17364. Add(' setyear(value+11);');
  17365. Add(' texta.year:=texta.year+12;');
  17366. Add(' year:=year+13;');
  17367. Add(' textb.century:=textb.century+14;');
  17368. Add(' century:=century+15;');
  17369. Add('end;');
  17370. Add('var');
  17371. Add(' A: texta;');
  17372. Add(' B: textb;');
  17373. Add('begin');
  17374. Add(' texta.year:=texta.year+1;');
  17375. Add(' textb.year:=textb.year+2;');
  17376. Add(' TextA.year:=TextA.year+3;');
  17377. Add(' b.year:=b.year+4;');
  17378. Add(' textb.century:=textb.century+5;');
  17379. Add(' b.century:=b.century+6;');
  17380. ConvertProgram;
  17381. CheckSource('TestExternalClass_ClassProperty',
  17382. LinesToStr([ // statements
  17383. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  17384. ' this.$init = function () {',
  17385. ' };',
  17386. ' this.$final = function () {',
  17387. ' };',
  17388. ' this.GetCentury = function () {',
  17389. ' var Result = 0;',
  17390. ' return Result;',
  17391. ' };',
  17392. ' this.SetCentury = function (Value) {',
  17393. ' this.setYear(Value + 11);',
  17394. ' ExtA.setYear(ExtA.getYear() + 12);',
  17395. ' this.setYear(this.getYear() + 13);',
  17396. ' $mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 14);',
  17397. ' this.SetCentury(this.GetCentury() + 15);',
  17398. ' };',
  17399. '});',
  17400. 'this.A = null;',
  17401. 'this.B = null;',
  17402. '']),
  17403. LinesToStr([ // $mod.$main
  17404. 'ExtA.setYear(ExtA.getYear() + 1);',
  17405. '$mod.TExtB.setYear($mod.TExtB.getYear() + 2);',
  17406. 'ExtA.setYear(ExtA.getYear() + 3);',
  17407. '$mod.B.setYear($mod.B.getYear() + 4);',
  17408. '$mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 5);',
  17409. '$mod.B.$class.SetCentury($mod.B.$class.GetCentury() + 6);',
  17410. '']));
  17411. end;
  17412. procedure TTestModule.TestExternalClass_ClassOf;
  17413. begin
  17414. StartProgram(false);
  17415. Add('{$modeswitch externalclass}');
  17416. Add('type');
  17417. Add(' TExtA = class external name ''ExtA''');
  17418. Add(' procedure ProcA; virtual;');
  17419. Add(' procedure ProcB; virtual;');
  17420. Add(' end;');
  17421. Add(' TExtAClass = class of TExtA;');
  17422. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  17423. Add(' end;');
  17424. Add(' TExtBClass = class of TExtB;');
  17425. Add(' TExtC = class (TExtB)');
  17426. Add(' procedure ProcA; override;');
  17427. Add(' end;');
  17428. Add(' TExtCClass = class of TExtC;');
  17429. Add('procedure TExtC.ProcA; begin end;');
  17430. Add('var');
  17431. Add(' A: texta; ClA: TExtAClass;');
  17432. Add(' B: textb; ClB: TExtBClass;');
  17433. Add(' C: textc; ClC: TExtCClass;');
  17434. Add('begin');
  17435. Add(' ClA:=texta;');
  17436. Add(' ClA:=textb;');
  17437. Add(' ClA:=textc;');
  17438. Add(' ClB:=textb;');
  17439. Add(' ClB:=textc;');
  17440. Add(' ClC:=textc;');
  17441. ConvertProgram;
  17442. CheckSource('TestExternalClass_ClassOf',
  17443. LinesToStr([ // statements
  17444. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  17445. ' this.$init = function () {',
  17446. ' };',
  17447. ' this.$final = function () {',
  17448. ' };',
  17449. ' this.ProcA = function () {',
  17450. ' };',
  17451. '});',
  17452. 'this.A = null;',
  17453. 'this.ClA = null;',
  17454. 'this.B = null;',
  17455. 'this.ClB = null;',
  17456. 'this.C = null;',
  17457. 'this.ClC = null;',
  17458. '']),
  17459. LinesToStr([ // $mod.$main
  17460. '$mod.ClA = ExtA;',
  17461. '$mod.ClA = ExtB;',
  17462. '$mod.ClA = $mod.TExtC;',
  17463. '$mod.ClB = ExtB;',
  17464. '$mod.ClB = $mod.TExtC;',
  17465. '$mod.ClC = $mod.TExtC;',
  17466. '']));
  17467. end;
  17468. procedure TTestModule.TestExternalClass_ClassOtherUnit;
  17469. begin
  17470. AddModuleWithIntfImplSrc('unit2.pas',
  17471. LinesToStr([
  17472. '{$modeswitch externalclass}',
  17473. 'type',
  17474. ' TExtA = class external name ''ExtA''',
  17475. ' class var Id: longint;',
  17476. ' end;',
  17477. '']),
  17478. '');
  17479. StartUnit(true);
  17480. Add('interface');
  17481. Add('uses unit2;');
  17482. Add('implementation');
  17483. Add('begin');
  17484. Add(' unit2.texta.id:=unit2.texta.id+1;');
  17485. ConvertUnit;
  17486. CheckSource('TestExternalClass_ClassOtherUnit',
  17487. LinesToStr([
  17488. '']),
  17489. LinesToStr([
  17490. 'ExtA.Id = ExtA.Id + 1;',
  17491. '']));
  17492. end;
  17493. procedure TTestModule.TestExternalClass_Is;
  17494. begin
  17495. StartProgram(false);
  17496. Add([
  17497. '{$modeswitch externalclass}',
  17498. 'type',
  17499. ' TExtA = class external name ''ExtA''',
  17500. ' end;',
  17501. ' TExtAClass = class of TExtA;',
  17502. ' TExtB = class external name ''ExtB'' (TExtA)',
  17503. ' end;',
  17504. ' TExtBClass = class of TExtB;',
  17505. ' TExtC = class (TExtB)',
  17506. ' end;',
  17507. ' TExtCClass = class of TExtC;',
  17508. 'var',
  17509. ' A: texta; ClA: TExtAClass;',
  17510. ' B: textb; ClB: TExtBClass;',
  17511. ' C: textc; ClC: TExtCClass;',
  17512. 'begin',
  17513. ' if a is textb then ;',
  17514. ' if a is textc then ;',
  17515. ' if b is textc then ;',
  17516. ' if cla is textb then ;',
  17517. ' if cla is textc then ;',
  17518. ' if clb is textc then ;',
  17519. ' try',
  17520. ' except',
  17521. ' on TExtA do ;',
  17522. ' on e: TExtB do ;',
  17523. ' end;',
  17524. '']);
  17525. ConvertProgram;
  17526. CheckSource('TestExternalClass_Is',
  17527. LinesToStr([ // statements
  17528. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  17529. ' this.$init = function () {',
  17530. ' };',
  17531. ' this.$final = function () {',
  17532. ' };',
  17533. '});',
  17534. 'this.A = null;',
  17535. 'this.ClA = null;',
  17536. 'this.B = null;',
  17537. 'this.ClB = null;',
  17538. 'this.C = null;',
  17539. 'this.ClC = null;',
  17540. '']),
  17541. LinesToStr([ // $mod.$main
  17542. 'if (rtl.isExt($mod.A, ExtB)) ;',
  17543. 'if ($mod.TExtC.isPrototypeOf($mod.A)) ;',
  17544. 'if ($mod.TExtC.isPrototypeOf($mod.B)) ;',
  17545. 'if (rtl.isExt($mod.ClA, ExtB)) ;',
  17546. 'if (rtl.is($mod.ClA, $mod.TExtC)) ;',
  17547. 'if (rtl.is($mod.ClB, $mod.TExtC)) ;',
  17548. 'try {} catch ($e) {',
  17549. ' if (rtl.isExt($e,ExtA)) {}',
  17550. ' else if (rtl.isExt($e,ExtB)) {',
  17551. ' var e = $e;',
  17552. ' } else throw $e',
  17553. '};',
  17554. '']));
  17555. end;
  17556. procedure TTestModule.TestExternalClass_As;
  17557. begin
  17558. StartProgram(false);
  17559. Add('{$modeswitch externalclass}');
  17560. Add('type');
  17561. Add(' TExtA = class external name ''ExtA''');
  17562. Add(' end;');
  17563. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  17564. Add(' end;');
  17565. Add(' TExtC = class (TExtB)');
  17566. Add(' end;');
  17567. Add('var');
  17568. Add(' A: texta;');
  17569. Add(' B: textb;');
  17570. Add(' C: textc;');
  17571. Add('begin');
  17572. Add(' b:=a as textb;');
  17573. Add(' c:=a as textc;');
  17574. Add(' c:=b as textc;');
  17575. ConvertProgram;
  17576. CheckSource('TestExternalClass_Is',
  17577. LinesToStr([ // statements
  17578. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  17579. ' this.$init = function () {',
  17580. ' };',
  17581. ' this.$final = function () {',
  17582. ' };',
  17583. '});',
  17584. 'this.A = null;',
  17585. 'this.B = null;',
  17586. 'this.C = null;',
  17587. '']),
  17588. LinesToStr([ // $mod.$main
  17589. '$mod.B = rtl.asExt($mod.A, ExtB);',
  17590. '$mod.C = rtl.as($mod.A, $mod.TExtC);',
  17591. '$mod.C = rtl.as($mod.B, $mod.TExtC);',
  17592. '']));
  17593. end;
  17594. procedure TTestModule.TestExternalClass_DestructorFail;
  17595. begin
  17596. StartProgram(false);
  17597. Add('{$modeswitch externalclass}');
  17598. Add('type');
  17599. Add(' TExtA = class external name ''ExtA''');
  17600. Add(' destructor Free;');
  17601. Add(' end;');
  17602. SetExpectedPasResolverError('Pascal element not supported: destructor',
  17603. nPasElementNotSupported);
  17604. ConvertProgram;
  17605. end;
  17606. procedure TTestModule.TestExternalClass_New;
  17607. begin
  17608. StartProgram(false);
  17609. Add([
  17610. '{$modeswitch externalclass}',
  17611. 'type',
  17612. ' TExtA = class external name ''ExtA''',
  17613. ' constructor New;',
  17614. ' constructor New(i: longint; j: longint = 2);',
  17615. ' end;',
  17616. 'var',
  17617. ' A: texta;',
  17618. 'begin',
  17619. ' a:=texta.new;',
  17620. ' a:=texta(texta.new);',
  17621. ' a:=texta.new();',
  17622. ' a:=texta.new(1);',
  17623. ' with texta do begin',
  17624. ' a:=new;',
  17625. ' a:=new();',
  17626. ' a:=new(2);',
  17627. ' end;',
  17628. ' a:=test1.texta.new;',
  17629. ' a:=test1.texta.new();',
  17630. ' a:=test1.texta.new(3);',
  17631. '']);
  17632. ConvertProgram;
  17633. CheckSource('TestExternalClass_New',
  17634. LinesToStr([ // statements
  17635. 'this.A = null;',
  17636. '']),
  17637. LinesToStr([ // $mod.$main
  17638. '$mod.A = new ExtA();',
  17639. '$mod.A = new ExtA();',
  17640. '$mod.A = new ExtA();',
  17641. '$mod.A = new ExtA(1,2);',
  17642. '$mod.A = new ExtA();',
  17643. '$mod.A = new ExtA();',
  17644. '$mod.A = new ExtA(2,2);',
  17645. '$mod.A = new ExtA();',
  17646. '$mod.A = new ExtA();',
  17647. '$mod.A = new ExtA(3,2);',
  17648. '']));
  17649. end;
  17650. procedure TTestModule.TestExternalClass_ClassOf_New;
  17651. begin
  17652. StartProgram(false);
  17653. Add('{$modeswitch externalclass}');
  17654. Add('type');
  17655. Add(' TExtAClass = class of TExtA;');
  17656. Add(' TExtA = class external name ''ExtA''');
  17657. Add(' C: TExtAClass;');
  17658. Add(' constructor New;');
  17659. Add(' end;');
  17660. Add('var');
  17661. Add(' A: texta;');
  17662. Add(' C: textaclass;');
  17663. Add('begin');
  17664. Add(' a:=c.new;');
  17665. Add(' a:=c.new();');
  17666. Add(' with C do begin');
  17667. Add(' a:=new;');
  17668. Add(' a:=new();');
  17669. Add(' end;');
  17670. Add(' a:=test1.c.new;');
  17671. Add(' a:=test1.c.new();');
  17672. Add(' a:=A.c.new();');
  17673. ConvertProgram;
  17674. CheckSource('TestExternalClass_ClassOf_New',
  17675. LinesToStr([ // statements
  17676. 'this.A = null;',
  17677. 'this.C = null;',
  17678. '']),
  17679. LinesToStr([ // $mod.$main
  17680. '$mod.A = new $mod.C();',
  17681. '$mod.A = new $mod.C();',
  17682. 'var $with = $mod.C;',
  17683. '$mod.A = new $with();',
  17684. '$mod.A = new $with();',
  17685. '$mod.A = new $mod.C();',
  17686. '$mod.A = new $mod.C();',
  17687. '$mod.A = new $mod.A.C();',
  17688. '']));
  17689. end;
  17690. procedure TTestModule.TestExternalClass_FuncClassOf_New;
  17691. begin
  17692. StartProgram(false);
  17693. Add([
  17694. '{$modeswitch externalclass}',
  17695. 'type',
  17696. ' TExtAClass = class of TExtA;',
  17697. ' TExtA = class external name ''ExtA''',
  17698. ' constructor New;',
  17699. ' end;',
  17700. 'function GetCreator: TExtAClass;',
  17701. 'begin',
  17702. ' Result:=TExtA;',
  17703. 'end;',
  17704. 'var',
  17705. ' A: texta;',
  17706. 'begin',
  17707. ' a:=getcreator.new;',
  17708. ' a:=getcreator().new;',
  17709. ' a:=getcreator().new();',
  17710. ' a:=getcreator.new();',
  17711. ' with getcreator do begin',
  17712. ' a:=new;',
  17713. ' a:=new();',
  17714. ' end;']);
  17715. ConvertProgram;
  17716. CheckSource('TestExternalClass_FuncClassOf_New',
  17717. LinesToStr([ // statements
  17718. 'this.GetCreator = function () {',
  17719. ' var Result = null;',
  17720. ' Result = ExtA;',
  17721. ' return Result;',
  17722. '};',
  17723. 'this.A = null;',
  17724. '']),
  17725. LinesToStr([ // $mod.$main
  17726. '$mod.A = new ($mod.GetCreator())();',
  17727. '$mod.A = new ($mod.GetCreator())();',
  17728. '$mod.A = new ($mod.GetCreator())();',
  17729. '$mod.A = new ($mod.GetCreator())();',
  17730. 'var $with = $mod.GetCreator();',
  17731. '$mod.A = new $with();',
  17732. '$mod.A = new $with();',
  17733. '']));
  17734. end;
  17735. procedure TTestModule.TestExternalClass_New_PasClassFail;
  17736. begin
  17737. StartProgram(false);
  17738. Add([
  17739. '{$modeswitch externalclass}',
  17740. 'type',
  17741. ' TExtA = class external name ''ExtA''',
  17742. ' constructor New;',
  17743. ' end;',
  17744. ' TBird = class(TExtA)',
  17745. ' end;',
  17746. 'begin',
  17747. ' TBird.new;',
  17748. '']);
  17749. SetExpectedPasResolverError(sJSNewNotSupported,nJSNewNotSupported);
  17750. ConvertProgram;
  17751. end;
  17752. procedure TTestModule.TestExternalClass_New_PasClassBracketsFail;
  17753. begin
  17754. StartProgram(false);
  17755. Add([
  17756. '{$modeswitch externalclass}',
  17757. 'type',
  17758. ' TExtA = class external name ''ExtA''',
  17759. ' constructor New;',
  17760. ' end;',
  17761. ' TBird = class(TExtA)',
  17762. ' end;',
  17763. 'begin',
  17764. ' TBird.new();',
  17765. '']);
  17766. SetExpectedPasResolverError(sJSNewNotSupported,nJSNewNotSupported);
  17767. ConvertProgram;
  17768. end;
  17769. procedure TTestModule.TestExternalClass_NewExtName;
  17770. begin
  17771. StartProgram(false);
  17772. Add([
  17773. '{$modeswitch externalclass}',
  17774. 'type',
  17775. ' TExtA = class external name ''ExtA''',
  17776. ' constructor New; external name ''Other'';',
  17777. ' constructor New(i: longint; j: longint = 2); external name ''A.B'';',
  17778. ' end;',
  17779. 'var',
  17780. ' A: texta;',
  17781. 'begin',
  17782. ' a:=texta.new;',
  17783. ' a:=texta(texta.new);',
  17784. ' a:=texta.new();',
  17785. ' a:=texta.new(1);',
  17786. ' with texta do begin',
  17787. ' a:=new;',
  17788. ' a:=new();',
  17789. ' a:=new(2);',
  17790. ' end;',
  17791. ' a:=test1.texta.new;',
  17792. ' a:=test1.texta.new();',
  17793. ' a:=test1.texta.new(3);',
  17794. '']);
  17795. ConvertProgram;
  17796. CheckSource('TestExternalClass_NewExtName',
  17797. LinesToStr([ // statements
  17798. 'this.A = null;',
  17799. '']),
  17800. LinesToStr([ // $mod.$main
  17801. '$mod.A = new Other();',
  17802. '$mod.A = new Other();',
  17803. '$mod.A = new Other();',
  17804. '$mod.A = new A.B(1,2);',
  17805. '$mod.A = new Other();',
  17806. '$mod.A = new Other();',
  17807. '$mod.A = new A.B(2,2);',
  17808. '$mod.A = new Other();',
  17809. '$mod.A = new Other();',
  17810. '$mod.A = new A.B(3,2);',
  17811. '']));
  17812. end;
  17813. procedure TTestModule.TestExternalClass_Constructor;
  17814. begin
  17815. StartProgram(false);
  17816. Add([
  17817. '{$modeswitch externalclass}',
  17818. 'type',
  17819. ' TExtA = class external name ''ExtA''',
  17820. ' public type',
  17821. ' TExtB = class external name ''ExtB''',
  17822. ' public type',
  17823. ' TExtC = class external name ''ExtC''',
  17824. ' constructor New;',
  17825. ' constructor New(i: word);',
  17826. ' end;',
  17827. ' end;',
  17828. ' constructor Create;',
  17829. ' constructor Create(i: longint; j: longint = 2);',
  17830. ' end;',
  17831. 'var',
  17832. ' A: texta;',
  17833. ' C: texta.textb.textc;',
  17834. 'begin',
  17835. ' a:=texta.create;',
  17836. ' a:=texta(texta.create);',
  17837. ' a:=texta.create();',
  17838. ' a:=texta.create(1);',
  17839. ' with texta do begin',
  17840. ' a:=create;',
  17841. ' a:=create();',
  17842. ' a:=create(2);',
  17843. ' end;',
  17844. ' a:=test1.texta.create;',
  17845. ' a:=test1.texta.create();',
  17846. ' a:=test1.texta.create(3);',
  17847. ' c:=texta.textb.textc.new;',
  17848. ' c:=texta.textb.textc.new();',
  17849. ' c:=texta.textb.textc.new(4);',
  17850. '']);
  17851. ConvertProgram;
  17852. CheckSource('TestExternalClass_Constructor',
  17853. LinesToStr([ // statements
  17854. 'this.A = null;',
  17855. 'this.C = null;',
  17856. '']),
  17857. LinesToStr([ // $mod.$main
  17858. '$mod.A = new ExtA.Create();',
  17859. '$mod.A = new ExtA.Create();',
  17860. '$mod.A = new ExtA.Create();',
  17861. '$mod.A = new ExtA.Create(1,2);',
  17862. '$mod.A = new ExtA.Create();',
  17863. '$mod.A = new ExtA.Create();',
  17864. '$mod.A = new ExtA.Create(2,2);',
  17865. '$mod.A = new ExtA.Create();',
  17866. '$mod.A = new ExtA.Create();',
  17867. '$mod.A = new ExtA.Create(3,2);',
  17868. '$mod.C = new ExtA.ExtB.ExtC();',
  17869. '$mod.C = new ExtA.ExtB.ExtC();',
  17870. '$mod.C = new ExtA.ExtB.ExtC(4);',
  17871. '']));
  17872. end;
  17873. procedure TTestModule.TestExternalClass_ConstructorBrackets;
  17874. begin
  17875. StartProgram(false);
  17876. Add([
  17877. '{$modeswitch externalclass}',
  17878. 'type',
  17879. ' TExtA = class external name ''ExtA''',
  17880. ' constructor Create; external name ''{}'';',
  17881. ' end;',
  17882. 'var',
  17883. ' A: texta;',
  17884. 'begin',
  17885. ' a:=texta.create;',
  17886. ' a:=texta(texta.create);',
  17887. ' a:=texta.create();',
  17888. ' with texta do begin',
  17889. ' a:=create;',
  17890. ' a:=create();',
  17891. ' end;',
  17892. ' a:=test1.texta.create;',
  17893. ' a:=test1.texta.create();',
  17894. '']);
  17895. ConvertProgram;
  17896. CheckSource('TestExternalClass_ConstructorBrackets',
  17897. LinesToStr([ // statements
  17898. 'this.A = null;',
  17899. '']),
  17900. LinesToStr([ // $mod.$main
  17901. '$mod.A = {};',
  17902. '$mod.A = {};',
  17903. '$mod.A = {};',
  17904. '$mod.A = {};',
  17905. '$mod.A = {};',
  17906. '$mod.A = {};',
  17907. '$mod.A = {};',
  17908. '']));
  17909. end;
  17910. procedure TTestModule.TestExternalClass_LocalConstSameName;
  17911. begin
  17912. StartProgram(false);
  17913. Add('{$modeswitch externalclass}');
  17914. Add('type');
  17915. Add(' TExtA = class external name ''ExtA''');
  17916. Add(' constructor New;');
  17917. Add(' end;');
  17918. Add('function DoIt: longint;');
  17919. Add('const ExtA: longint = 3;');
  17920. Add('begin');
  17921. Add(' Result:=ExtA;');
  17922. Add('end;');
  17923. Add('var');
  17924. Add(' A: texta;');
  17925. Add('begin');
  17926. Add(' a:=texta.new;');
  17927. ConvertProgram;
  17928. CheckSource('TestExternalClass_LocalConstSameName',
  17929. LinesToStr([ // statements
  17930. 'var ExtA$1 = 3;',
  17931. 'this.DoIt = function () {',
  17932. ' var Result = 0;',
  17933. ' Result = ExtA$1;',
  17934. ' return Result;',
  17935. '};',
  17936. 'this.A = null;',
  17937. '']),
  17938. LinesToStr([ // $mod.$main
  17939. '$mod.A = new ExtA();',
  17940. '']));
  17941. end;
  17942. procedure TTestModule.TestExternalClass_ReintroduceOverload;
  17943. begin
  17944. StartProgram(false);
  17945. Add('{$modeswitch externalclass}');
  17946. Add('type');
  17947. Add(' TExtA = class external name ''ExtA''');
  17948. Add(' procedure DoIt;');
  17949. Add(' end;');
  17950. Add(' TMyA = class(TExtA)');
  17951. Add(' procedure DoIt;');
  17952. Add(' end;');
  17953. Add('procedure TMyA.DoIt; begin end;');
  17954. Add('begin');
  17955. ConvertProgram;
  17956. CheckSource('TestExternalClass_ReintroduceOverload',
  17957. LinesToStr([ // statements
  17958. 'rtl.createClassExt(this, "TMyA", ExtA, "", function () {',
  17959. ' this.$init = function () {',
  17960. ' };',
  17961. ' this.$final = function () {',
  17962. ' };',
  17963. ' this.DoIt$1 = function () {',
  17964. ' };',
  17965. '});',
  17966. '']),
  17967. LinesToStr([ // $mod.$main
  17968. '']));
  17969. end;
  17970. procedure TTestModule.TestExternalClass_Inherited;
  17971. begin
  17972. StartProgram(false);
  17973. Add('{$modeswitch externalclass}');
  17974. Add('type');
  17975. Add(' TExtA = class external name ''ExtA''');
  17976. Add(' procedure DoIt(i: longint = 1); virtual;');
  17977. Add(' procedure DoSome(j: longint = 2);');
  17978. Add(' end;');
  17979. Add(' TExtB = class external name ''ExtB''(TExtA)');
  17980. Add(' end;');
  17981. Add(' TMyC = class(TExtB)');
  17982. Add(' procedure DoIt(i: longint = 1); override;');
  17983. Add(' procedure DoSome(j: longint = 2); reintroduce;');
  17984. Add(' end;');
  17985. Add('procedure TMyC.DoIt(i: longint);');
  17986. Add('begin');
  17987. Add(' inherited;');
  17988. Add(' inherited DoIt;');
  17989. Add(' inherited DoIt();');
  17990. Add(' inherited DoIt(3);');
  17991. Add(' inherited DoSome;');
  17992. Add(' inherited DoSome();');
  17993. Add(' inherited DoSome(4);');
  17994. Add('end;');
  17995. Add('procedure TMyC.DoSome(j: longint);');
  17996. Add('begin');
  17997. Add(' inherited;');
  17998. Add('end;');
  17999. Add('begin');
  18000. ConvertProgram;
  18001. CheckSource('TestExternalClass_ReintroduceOverload',
  18002. LinesToStr([ // statements
  18003. 'rtl.createClassExt(this, "TMyC", ExtB, "", function () {',
  18004. ' this.$init = function () {',
  18005. ' };',
  18006. ' this.$final = function () {',
  18007. ' };',
  18008. ' this.DoIt = function (i) {',
  18009. ' ExtB.DoIt.apply(this, arguments);',
  18010. ' ExtB.DoIt.call(this, 1);',
  18011. ' ExtB.DoIt.call(this, 1);',
  18012. ' ExtB.DoIt.call(this, 3);',
  18013. ' ExtB.DoSome.call(this, 2);',
  18014. ' ExtB.DoSome.call(this, 2);',
  18015. ' ExtB.DoSome.call(this, 4);',
  18016. ' };',
  18017. ' this.DoSome$1 = function (j) {',
  18018. ' ExtB.DoSome.apply(this, arguments);',
  18019. ' };',
  18020. '});',
  18021. '']),
  18022. LinesToStr([ // $mod.$main
  18023. '']));
  18024. end;
  18025. procedure TTestModule.TestExternalClass_PascalAncestorFail;
  18026. begin
  18027. StartProgram(false);
  18028. Add('{$modeswitch externalclass}');
  18029. Add('type');
  18030. Add(' TObject = class');
  18031. Add(' end;');
  18032. Add(' TExtA = class external name ''ExtA''(TObject)');
  18033. Add(' end;');
  18034. Add('begin');
  18035. SetExpectedPasResolverError('Ancestor "TObject" is not external',nAncestorIsNotExternal);
  18036. ConvertProgram;
  18037. end;
  18038. procedure TTestModule.TestExternalClass_NewInstance;
  18039. begin
  18040. StartProgram(false);
  18041. Add('{$modeswitch externalclass}');
  18042. Add('type');
  18043. Add(' TExtA = class external name ''ExtA''');
  18044. Add(' end;');
  18045. Add(' TMyB = class(TExtA)');
  18046. Add(' protected');
  18047. Add(' class function NewInstance(fnname: string; const paramarray): TMyB; virtual;');
  18048. Add(' end;');
  18049. Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
  18050. Add('begin end;');
  18051. Add('begin');
  18052. ConvertProgram;
  18053. CheckSource('TestExternalClass_NewInstance',
  18054. LinesToStr([ // statements
  18055. 'rtl.createClassExt(this, "TMyB", ExtA, "NewInstance", function () {',
  18056. ' this.$init = function () {',
  18057. ' };',
  18058. ' this.$final = function () {',
  18059. ' };',
  18060. ' this.NewInstance = function (fnname, paramarray) {',
  18061. ' var Result = null;',
  18062. ' return Result;',
  18063. ' };',
  18064. '});',
  18065. '']),
  18066. LinesToStr([ // $mod.$main
  18067. '']));
  18068. end;
  18069. procedure TTestModule.TestExternalClass_NewInstance_NonVirtualFail;
  18070. begin
  18071. StartProgram(false);
  18072. Add('{$modeswitch externalclass}');
  18073. Add('type');
  18074. Add(' TExtA = class external name ''ExtA''');
  18075. Add(' end;');
  18076. Add(' TMyB = class(TExtA)');
  18077. Add(' protected');
  18078. Add(' class function NewInstance(fnname: string; const paramarray): TMyB;');
  18079. Add(' end;');
  18080. Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
  18081. Add('begin end;');
  18082. Add('begin');
  18083. SetExpectedPasResolverError(sNewInstanceFunctionMustBeVirtual,nNewInstanceFunctionMustBeVirtual);
  18084. ConvertProgram;
  18085. end;
  18086. procedure TTestModule.TestExternalClass_NewInstance_FirstParamNotString_Fail;
  18087. begin
  18088. StartProgram(false);
  18089. Add('{$modeswitch externalclass}');
  18090. Add('type');
  18091. Add(' TExtA = class external name ''ExtA''');
  18092. Add(' end;');
  18093. Add(' TMyB = class(TExtA)');
  18094. Add(' protected');
  18095. Add(' class function NewInstance(fnname: longint; const paramarray): TMyB; virtual;');
  18096. Add(' end;');
  18097. Add('class function TMyB.NewInstance(fnname: longint; const paramarray): TMyB;');
  18098. Add('begin end;');
  18099. Add('begin');
  18100. SetExpectedPasResolverError('Incompatible type arg no. 1: Got "Longint", expected "String"',
  18101. nIncompatibleTypeArgNo);
  18102. ConvertProgram;
  18103. end;
  18104. procedure TTestModule.TestExternalClass_NewInstance_SecondParamTyped_Fail;
  18105. begin
  18106. StartProgram(false);
  18107. Add('{$modeswitch externalclass}');
  18108. Add('type');
  18109. Add(' TExtA = class external name ''ExtA''');
  18110. Add(' end;');
  18111. Add(' TMyB = class(TExtA)');
  18112. Add(' protected');
  18113. Add(' class function NewInstance(fnname: string; const paramarray: string): TMyB; virtual;');
  18114. Add(' end;');
  18115. Add('class function TMyB.NewInstance(fnname: string; const paramarray: string): TMyB;');
  18116. Add('begin end;');
  18117. Add('begin');
  18118. SetExpectedPasResolverError('Incompatible type arg no. 2: Got "type", expected "untyped"',
  18119. nIncompatibleTypeArgNo);
  18120. ConvertProgram;
  18121. end;
  18122. procedure TTestModule.TestExternalClass_JSFunctionPasDescendant;
  18123. begin
  18124. StartProgram(false);
  18125. Add([
  18126. '{$modeswitch externalclass}',
  18127. 'type',
  18128. ' TJSFunction = class external name ''Function''',
  18129. ' end;',
  18130. ' TExtA = class external name ''ExtA''(TJSFunction)',
  18131. ' constructor New(w: word);',
  18132. ' end;',
  18133. ' TBird = class (TExtA)',
  18134. ' public',
  18135. ' Size: word;',
  18136. ' class var Legs: word;',
  18137. ' constructor Create(a: word);',
  18138. ' end;',
  18139. ' TEagle = class (TBird)',
  18140. ' public',
  18141. ' constructor Create(b: word); reintroduce;',
  18142. ' end;',
  18143. 'constructor TBird.Create(a: word);',
  18144. 'begin',
  18145. ' inherited;', // silently ignored
  18146. ' inherited New(a);', // this.$func(a)
  18147. 'end;',
  18148. 'constructor TEagle.Create(b: word);',
  18149. 'begin',
  18150. ' inherited Create(b);',
  18151. 'end;',
  18152. 'var',
  18153. ' Bird: TBird;',
  18154. ' Eagle: TEagle;',
  18155. 'begin',
  18156. ' Bird:=TBird.Create(3);',
  18157. ' Eagle:=TEagle.Create(4);',
  18158. ' Bird.Size:=Bird.Size+5;',
  18159. ' Bird.Legs:=Bird.Legs+6;',
  18160. ' Eagle.Size:=Eagle.Size+5;',
  18161. ' Eagle.Legs:=Eagle.Legs+6;',
  18162. '']);
  18163. ConvertProgram;
  18164. CheckSource('TestExternalClass_JSFunctionPasDescendant',
  18165. LinesToStr([ // statements
  18166. 'rtl.createClassExt(this, "TBird", ExtA, "", function () {',
  18167. ' this.Legs = 0;',
  18168. ' this.$init = function () {',
  18169. ' this.Size = 0;',
  18170. ' };',
  18171. ' this.$final = function () {',
  18172. ' };',
  18173. ' this.Create = function (a) {',
  18174. ' this.$ancestorfunc(a);',
  18175. ' return this;',
  18176. ' };',
  18177. '});',
  18178. 'rtl.createClassExt(this, "TEagle", this.TBird, "", function () {',
  18179. ' this.Create$1 = function (b) {',
  18180. ' $mod.TBird.Create.call(this, b);',
  18181. ' return this;',
  18182. ' };',
  18183. '});',
  18184. 'this.Bird = null;',
  18185. 'this.Eagle = null;',
  18186. '']),
  18187. LinesToStr([ // $mod.$main
  18188. '$mod.Bird = $mod.TBird.$create("Create", [3]);',
  18189. '$mod.Eagle = $mod.TEagle.$create("Create$1", [4]);',
  18190. '$mod.Bird.Size = $mod.Bird.Size + 5;',
  18191. '$mod.TBird.Legs = $mod.Bird.Legs + 6;',
  18192. '$mod.Eagle.Size = $mod.Eagle.Size + 5;',
  18193. '$mod.TBird.Legs = $mod.Eagle.Legs + 6;',
  18194. '']));
  18195. end;
  18196. procedure TTestModule.TestExternalClass_PascalProperty;
  18197. begin
  18198. StartProgram(false);
  18199. Add('{$modeswitch externalclass}');
  18200. Add('type');
  18201. Add(' TJSElement = class;');
  18202. Add(' TJSNotifyEvent = procedure(Sender: TJSElement) of object;');
  18203. Add(' TJSElement = class external name ''ExtA''');
  18204. Add(' end;');
  18205. Add(' TControl = class(TJSElement)');
  18206. Add(' private');
  18207. Add(' FOnClick: TJSNotifyEvent;');
  18208. Add(' property OnClick: TJSNotifyEvent read FOnClick write FOnClick;');
  18209. Add(' procedure Click(Sender: TJSElement);');
  18210. Add(' end;');
  18211. Add('procedure TControl.Click(Sender: TJSElement);');
  18212. Add('begin');
  18213. Add(' OnClick(Self);');
  18214. Add('end;');
  18215. Add('var');
  18216. Add(' Ctrl: TControl;');
  18217. Add('begin');
  18218. Add(' Ctrl.OnClick:[email protected];');
  18219. Add(' Ctrl.OnClick(Ctrl);');
  18220. ConvertProgram;
  18221. CheckSource('TestExternalClass_PascalProperty',
  18222. LinesToStr([ // statements
  18223. 'rtl.createClassExt(this, "TControl", ExtA, "", function () {',
  18224. ' this.$init = function () {',
  18225. ' this.FOnClick = null;',
  18226. ' };',
  18227. ' this.$final = function () {',
  18228. ' this.FOnClick = undefined;',
  18229. ' };',
  18230. ' this.Click = function (Sender) {',
  18231. ' this.FOnClick(this);',
  18232. ' };',
  18233. '});',
  18234. 'this.Ctrl = null;',
  18235. '']),
  18236. LinesToStr([ // $mod.$main
  18237. '$mod.Ctrl.FOnClick = rtl.createCallback($mod.Ctrl, "Click");',
  18238. '$mod.Ctrl.FOnClick($mod.Ctrl);',
  18239. '']));
  18240. end;
  18241. procedure TTestModule.TestExternalClass_TypeCastToRootClass;
  18242. begin
  18243. StartProgram(false);
  18244. Add([
  18245. '{$modeswitch externalclass}',
  18246. 'type',
  18247. ' IUnknown = interface end;',
  18248. ' TObject = class',
  18249. ' end;',
  18250. ' TChild = class',
  18251. ' end;',
  18252. ' TExtRootA = class external name ''ExtRootA''',
  18253. ' end;',
  18254. ' TExtChildA = class external name ''ExtChildA''(TExtRootA)',
  18255. ' end;',
  18256. ' TExtRootB = class external name ''ExtRootB''',
  18257. ' end;',
  18258. ' TExtChildB = class external name ''ExtChildB''(TExtRootB)',
  18259. ' end;',
  18260. ' TExtString = class external name ''String''',
  18261. ' function charAt(aIndex : NativeInt) : string;',
  18262. ' end;',
  18263. 'var',
  18264. ' Obj: TObject;',
  18265. ' Child: TChild;',
  18266. ' RootA: TExtRootA;',
  18267. ' ChildA: TExtChildA;',
  18268. ' RootB: TExtRootB;',
  18269. ' ChildB: TExtChildB;',
  18270. ' i: IUnknown;',
  18271. ' s: string;',
  18272. ' v: jsvalue;',
  18273. 'begin',
  18274. ' obj:=tobject(roota);',
  18275. ' obj:=tobject(childa);',
  18276. ' child:=tchild(tobject(roota));',
  18277. ' roota:=textroota(obj);',
  18278. ' roota:=textroota(child);',
  18279. ' roota:=textroota(rootb);',
  18280. ' roota:=textroota(childb);',
  18281. ' childa:=textchilda(textroota(obj));',
  18282. ' roota:=TExtRootA(i);',
  18283. ' s:=TExtString(s).charAt(7);',
  18284. ' s:=TExtString(v).charAt(8);',
  18285. '']);
  18286. ConvertProgram;
  18287. CheckSource('TestExternalClass_TypeCastToRootClass',
  18288. LinesToStr([ // statements
  18289. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  18290. 'rtl.createClass(this, "TObject", null, function () {',
  18291. ' this.$init = function () {',
  18292. ' };',
  18293. ' this.$final = function () {',
  18294. ' };',
  18295. '});',
  18296. 'rtl.createClass(this, "TChild", this.TObject, function () {',
  18297. '});',
  18298. 'this.Obj = null;',
  18299. 'this.Child = null;',
  18300. 'this.RootA = null;',
  18301. 'this.ChildA = null;',
  18302. 'this.RootB = null;',
  18303. 'this.ChildB = null;',
  18304. 'this.i = null;',
  18305. 'this.s = "";',
  18306. 'this.v = undefined;',
  18307. '']),
  18308. LinesToStr([ // $mod.$main
  18309. '$mod.Obj = $mod.RootA;',
  18310. '$mod.Obj = $mod.ChildA;',
  18311. '$mod.Child = $mod.RootA;',
  18312. '$mod.RootA = $mod.Obj;',
  18313. '$mod.RootA = $mod.Child;',
  18314. '$mod.RootA = $mod.RootB;',
  18315. '$mod.RootA = $mod.ChildB;',
  18316. '$mod.ChildA = $mod.Obj;',
  18317. '$mod.RootA = $mod.i;',
  18318. '$mod.s = $mod.s.charAt(7);',
  18319. '$mod.s = $mod.v.charAt(8);',
  18320. '']));
  18321. end;
  18322. procedure TTestModule.TestExternalClass_TypeCastToJSObject;
  18323. begin
  18324. StartProgram(false);
  18325. Add([
  18326. '{$modeswitch externalclass}',
  18327. 'type',
  18328. ' IUnknown = interface end;',
  18329. ' IBird = interface(IUnknown) end;',
  18330. ' TClass = class of TObject;',
  18331. ' TObject = class',
  18332. ' end;',
  18333. ' TChild = class',
  18334. ' end;',
  18335. ' TJSObject = class external name ''Object''',
  18336. ' end;',
  18337. ' TRec = record end;',
  18338. 'var',
  18339. ' Obj: TObject;',
  18340. ' Child: TChild;',
  18341. ' i: IUnknown;',
  18342. ' Bird: IBird;',
  18343. ' j: TJSObject;',
  18344. ' r: TRec;',
  18345. ' c: TClass;',
  18346. 'begin',
  18347. ' j:=tjsobject(IUnknown);',
  18348. ' j:=tjsobject(IBird);',
  18349. ' j:=tjsobject(TObject);',
  18350. ' j:=tjsobject(TChild);',
  18351. ' j:=tjsobject(TRec);',
  18352. ' j:=tjsobject(Obj);',
  18353. ' j:=tjsobject(Child);',
  18354. ' j:=tjsobject(i);',
  18355. ' j:=tjsobject(Bird);',
  18356. ' j:=tjsobject(r);',
  18357. ' j:=tjsobject(c);',
  18358. '']);
  18359. ConvertProgram;
  18360. CheckSource('TestExternalClass_TypeCastToJSObject',
  18361. LinesToStr([ // statements
  18362. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  18363. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], this.IUnknown);',
  18364. 'rtl.createClass(this, "TObject", null, function () {',
  18365. ' this.$init = function () {',
  18366. ' };',
  18367. ' this.$final = function () {',
  18368. ' };',
  18369. '});',
  18370. 'rtl.createClass(this, "TChild", this.TObject, function () {',
  18371. '});',
  18372. 'rtl.recNewT(this, "TRec", function () {',
  18373. ' this.$eq = function (b) {',
  18374. ' return true;',
  18375. ' };',
  18376. ' this.$assign = function (s) {',
  18377. ' return this;',
  18378. ' };',
  18379. '});',
  18380. 'this.Obj = null;',
  18381. 'this.Child = null;',
  18382. 'this.i = null;',
  18383. 'this.Bird = null;',
  18384. 'this.j = null;',
  18385. 'this.r = this.TRec.$new();',
  18386. 'this.c = null;',
  18387. '']),
  18388. LinesToStr([ // $mod.$main
  18389. '$mod.j = $mod.IUnknown;',
  18390. '$mod.j = $mod.IBird;',
  18391. '$mod.j = $mod.TObject;',
  18392. '$mod.j = $mod.TChild;',
  18393. '$mod.j = $mod.TRec;',
  18394. '$mod.j = $mod.Obj;',
  18395. '$mod.j = $mod.Child;',
  18396. '$mod.j = $mod.i;',
  18397. '$mod.j = $mod.Bird;',
  18398. '$mod.j = $mod.r;',
  18399. '$mod.j = $mod.c;',
  18400. '']));
  18401. end;
  18402. procedure TTestModule.TestExternalClass_TypeCastStringToExternalString;
  18403. begin
  18404. StartProgram(false);
  18405. Add('{$modeswitch externalclass}');
  18406. Add('type');
  18407. Add(' TJSString = class external name ''String''');
  18408. Add(' class function fromCharCode() : string; varargs;');
  18409. Add(' function anchor(const aName : string) : string;');
  18410. Add(' end;');
  18411. Add('var');
  18412. Add(' s: string;');
  18413. Add('begin');
  18414. Add(' s:=TJSString.fromCharCode(65,66);');
  18415. Add(' s:=TJSString(s).anchor(s);');
  18416. Add(' s:=TJSString(''foo'').anchor(s);');
  18417. ConvertProgram;
  18418. CheckSource('TestExternalClass_TypeCastStringToExternalString',
  18419. LinesToStr([ // statements
  18420. 'this.s = "";',
  18421. '']),
  18422. LinesToStr([ // $mod.$main
  18423. '$mod.s = String.fromCharCode(65, 66);',
  18424. '$mod.s = $mod.s.anchor($mod.s);',
  18425. '$mod.s = "foo".anchor($mod.s);',
  18426. '']));
  18427. end;
  18428. procedure TTestModule.TestExternalClass_TypeCastToJSFunction;
  18429. begin
  18430. StartProgram(false);
  18431. Add([
  18432. '{$modeswitch externalclass}',
  18433. 'type',
  18434. ' TJSObject = class external name ''Object'' end;',
  18435. ' TJSFunction = class external name ''Function''',
  18436. ' function bind(thisArg: TJSObject): TJSFunction; varargs;',
  18437. ' function call(thisArg: TJSObject): JSValue; varargs;',
  18438. ' end;',
  18439. ' TObject = class',
  18440. ' procedure DoIt(i: longint);',
  18441. ' end;',
  18442. ' TFuncInt = function(o: TObject): longint;',
  18443. 'function GetIt(o: TObject): longint;',
  18444. ' procedure Sub; begin end;',
  18445. 'var',
  18446. ' f: TJSFunction;',
  18447. ' fi: TFuncInt;',
  18448. 'begin',
  18449. ' fi:=TFuncInt(f);',
  18450. ' f:=TJSFunction(fi);',
  18451. ' f:=TJSFunction(@GetIt);',
  18452. ' f:=TJSFunction(@GetIt).bind(nil,3);',
  18453. ' f:=TJSFunction(@Sub);',
  18454. ' f:=TJSFunction(@o.doit);',
  18455. ' f:=TJSFunction(fi).bind(nil,4)',
  18456. 'end;',
  18457. 'procedure TObject.DoIt(i: longint);',
  18458. ' procedure Sub; begin end;',
  18459. 'var f: TJSFunction;',
  18460. 'begin',
  18461. ' f:=TJSFunction(@DoIt);',
  18462. ' f:=TJSFunction(@DoIt).bind(nil,13);',
  18463. ' f:=TJSFunction(@Sub);',
  18464. ' f:=TJSFunction(@GetIt);',
  18465. 'end;',
  18466. 'begin']);
  18467. ConvertProgram;
  18468. CheckSource('TestExternalClass_TypeCastToJSFunction',
  18469. LinesToStr([ // statements
  18470. 'rtl.createClass(this, "TObject", null, function () {',
  18471. ' this.$init = function () {',
  18472. ' };',
  18473. ' this.$final = function () {',
  18474. ' };',
  18475. ' this.DoIt = function (i) {',
  18476. ' var $Self = this;',
  18477. ' function Sub() {',
  18478. ' };',
  18479. ' var f = null;',
  18480. ' f = this.DoIt;',
  18481. ' f = this.DoIt.bind(null, 13);',
  18482. ' f = Sub;',
  18483. ' f = $mod.GetIt;',
  18484. ' };',
  18485. '});',
  18486. 'this.GetIt = function (o) {',
  18487. ' var Result = 0;',
  18488. ' function Sub() {',
  18489. ' };',
  18490. ' var f = null;',
  18491. ' var fi = null;',
  18492. ' fi = f;',
  18493. ' f = fi;',
  18494. ' f = $mod.GetIt;',
  18495. ' f = $mod.GetIt.bind(null, 3);',
  18496. ' f = Sub;',
  18497. ' f = $mod.TObject.DoIt;',
  18498. ' f = fi.bind(null, 4);',
  18499. ' return Result;',
  18500. '};',
  18501. '']),
  18502. LinesToStr([ // $mod.$main
  18503. '']));
  18504. end;
  18505. procedure TTestModule.TestExternalClass_TypeCastDelphiUnrelated;
  18506. begin
  18507. StartProgram(false);
  18508. Add([
  18509. '{$mode delphi}',
  18510. '{$modeswitch externalclass}',
  18511. 'type',
  18512. ' TJSObject = class external name ''Object'' end;',
  18513. ' TJSWindow = class external name ''Window''(TJSObject)',
  18514. ' procedure Open;',
  18515. ' end;',
  18516. ' TJSEventTarget = class external name ''Event''(TJSObject)',
  18517. ' procedure Execute;',
  18518. ' end;',
  18519. 'procedure Fly;',
  18520. 'var',
  18521. ' w: TJSWindow;',
  18522. ' e: TJSEventTarget;',
  18523. 'begin',
  18524. ' w:=TJSWindow(e);',
  18525. ' e:=TJSEventTarget(w);',
  18526. 'end;',
  18527. 'begin']);
  18528. ConvertProgram;
  18529. CheckSource('TestExternalClass_TypeCastDelphiUnrelated',
  18530. LinesToStr([ // statements
  18531. 'this.Fly = function () {',
  18532. ' var w = null;',
  18533. ' var e = null;',
  18534. ' w = e;',
  18535. ' e = w;',
  18536. '};',
  18537. '']),
  18538. LinesToStr([ // $mod.$main
  18539. '']));
  18540. end;
  18541. procedure TTestModule.TestExternalClass_CallClassFunctionOfInstanceFail;
  18542. begin
  18543. StartProgram(false);
  18544. Add('{$modeswitch externalclass}');
  18545. Add('type');
  18546. Add(' TJSString = class external name ''String''');
  18547. Add(' class function fromCharCode() : string; varargs;');
  18548. Add(' end;');
  18549. Add('var');
  18550. Add(' s: string;');
  18551. Add(' sObj: TJSString;');
  18552. Add('begin');
  18553. Add(' s:=sObj.fromCharCode(65,66);');
  18554. SetExpectedPasResolverError('External class instance cannot access static class function fromCharCode',
  18555. nExternalClassInstanceCannotAccessStaticX);
  18556. ConvertProgram;
  18557. end;
  18558. procedure TTestModule.TestExternalClass_BracketAccessor;
  18559. begin
  18560. StartProgram(false);
  18561. Add([
  18562. '{$modeswitch externalclass}',
  18563. 'type',
  18564. ' TJSArray = class external name ''Array2''',
  18565. ' function GetItems(Index: longint): jsvalue; external name ''[]'';',
  18566. ' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';',
  18567. ' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;',
  18568. ' end;',
  18569. 'procedure DoIt(vI: JSValue; const vJ: jsvalue; var vK: jsvalue; out vL: jsvalue);',
  18570. 'begin end;',
  18571. 'var',
  18572. ' Arr: tjsarray;',
  18573. ' s: string;',
  18574. ' i: longint;',
  18575. ' v: jsvalue;',
  18576. 'begin',
  18577. ' v:=arr[0];',
  18578. ' v:=arr.items[1];',
  18579. ' arr[2]:=s;',
  18580. ' arr.items[3]:=s;',
  18581. ' arr[4]:=i;',
  18582. ' arr[5]:=arr[6];',
  18583. ' arr.items[7]:=arr.items[8];',
  18584. ' with arr do items[9]:=items[10];',
  18585. ' doit(arr[7],arr[8],arr[9],arr[10]);',
  18586. ' with arr do begin',
  18587. ' v:=GetItems(14);',
  18588. ' setitems(15,16);',
  18589. ' end;',
  18590. ' v:=test1.arr.items[17];',
  18591. ' test1.arr.items[18]:=v;',
  18592. '']);
  18593. ConvertProgram;
  18594. CheckSource('TestExternalClass_BracketAccessor',
  18595. LinesToStr([ // statements
  18596. 'this.DoIt = function (vI, vJ, vK, vL) {',
  18597. '};',
  18598. 'this.Arr = null;',
  18599. 'this.s = "";',
  18600. 'this.i = 0;',
  18601. 'this.v = undefined;',
  18602. '']),
  18603. LinesToStr([ // $mod.$main
  18604. '$mod.v = $mod.Arr[0];',
  18605. '$mod.v = $mod.Arr[1];',
  18606. '$mod.Arr[2] = $mod.s;',
  18607. '$mod.Arr[3] = $mod.s;',
  18608. '$mod.Arr[4] = $mod.i;',
  18609. '$mod.Arr[5] = $mod.Arr[6];',
  18610. '$mod.Arr[7] = $mod.Arr[8];',
  18611. 'var $with = $mod.Arr;',
  18612. '$with[9] = $with[10];',
  18613. '$mod.DoIt($mod.Arr[7], $mod.Arr[8], {',
  18614. ' a: 9,',
  18615. ' p: $mod.Arr,',
  18616. ' get: function () {',
  18617. ' return this.p[this.a];',
  18618. ' },',
  18619. ' set: function (v) {',
  18620. ' this.p[this.a] = v;',
  18621. ' }',
  18622. '}, {',
  18623. ' a: 10,',
  18624. ' p: $mod.Arr,',
  18625. ' get: function () {',
  18626. ' return this.p[this.a];',
  18627. ' },',
  18628. ' set: function (v) {',
  18629. ' this.p[this.a] = v;',
  18630. ' }',
  18631. '});',
  18632. 'var $with1 = $mod.Arr;',
  18633. '$mod.v = $with1[14];',
  18634. '$with1[15] = 16;',
  18635. '$mod.v = $mod.Arr[17];',
  18636. '$mod.Arr[18] = $mod.v;',
  18637. '']));
  18638. end;
  18639. procedure TTestModule.TestExternalClass_BracketAccessor_Call;
  18640. begin
  18641. StartProgram(false);
  18642. Add([
  18643. '{$modeswitch externalclass}',
  18644. 'type',
  18645. ' TJSArray = class external name ''Array2''',
  18646. ' function GetItems(Index: longint): jsvalue; external name ''[]'';',
  18647. ' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';',
  18648. ' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;',
  18649. ' end;',
  18650. ' TMyArr = class(TJSArray)',
  18651. ' procedure DoIt;',
  18652. ' end;',
  18653. 'procedure tmyarr.DoIt;',
  18654. 'begin',
  18655. ' Items[1]:=Items[2];',
  18656. ' SetItems(3,getItems(4));',
  18657. 'end;',
  18658. 'var',
  18659. ' Arr: tmyarr;',
  18660. ' s: string;',
  18661. ' i: longint;',
  18662. ' v: jsvalue;',
  18663. 'begin',
  18664. ' v:=arr[0];',
  18665. ' v:=arr.items[1];',
  18666. ' arr[2]:=s;',
  18667. ' arr.items[3]:=s;',
  18668. ' arr[4]:=i;',
  18669. ' arr[5]:=arr[6];',
  18670. ' arr.items[7]:=arr.items[8];',
  18671. ' with arr do items[9]:=items[10];',
  18672. ' with arr do begin',
  18673. ' v:=GetItems(14);',
  18674. ' setitems(15,16);',
  18675. ' end;',
  18676. '']);
  18677. ConvertProgram;
  18678. CheckSource('TestExternalClass_BracketAccessor_Call',
  18679. LinesToStr([ // statements
  18680. 'rtl.createClassExt(this, "TMyArr", Array2, "", function () {',
  18681. ' this.$init = function () {',
  18682. ' };',
  18683. ' this.$final = function () {',
  18684. ' };',
  18685. ' this.DoIt = function () {',
  18686. ' this[1] = this[2];',
  18687. ' this[3] = this[4];',
  18688. ' };',
  18689. '});',
  18690. 'this.Arr = null;',
  18691. 'this.s = "";',
  18692. 'this.i = 0;',
  18693. 'this.v = undefined;',
  18694. '']),
  18695. LinesToStr([ // $mod.$main
  18696. '$mod.v = $mod.Arr[0];',
  18697. '$mod.v = $mod.Arr[1];',
  18698. '$mod.Arr[2] = $mod.s;',
  18699. '$mod.Arr[3] = $mod.s;',
  18700. '$mod.Arr[4] = $mod.i;',
  18701. '$mod.Arr[5] = $mod.Arr[6];',
  18702. '$mod.Arr[7] = $mod.Arr[8];',
  18703. 'var $with = $mod.Arr;',
  18704. '$with[9] = $with[10];',
  18705. 'var $with1 = $mod.Arr;',
  18706. '$mod.v = $with1[14];',
  18707. '$with1[15] = 16;',
  18708. '']));
  18709. end;
  18710. procedure TTestModule.TestExternalClass_BracketAccessor_2ParamsFail;
  18711. begin
  18712. StartProgram(false);
  18713. Add('{$modeswitch externalclass}');
  18714. Add('type');
  18715. Add(' TJSArray = class external name ''Array2''');
  18716. Add(' function GetItems(Index1, Index2: longint): jsvalue; external name ''[]'';');
  18717. Add(' procedure SetItems(Index1, Index2: longint; Value: jsvalue); external name ''[]'';');
  18718. Add(' property Items[Index1, Index2: longint]: jsvalue read GetItems write SetItems; default;');
  18719. Add(' end;');
  18720. Add('begin');
  18721. SetExpectedPasResolverError(sBracketAccessorOfExternalClassMustHaveOneParameter,
  18722. nBracketAccessorOfExternalClassMustHaveOneParameter);
  18723. ConvertProgram;
  18724. end;
  18725. procedure TTestModule.TestExternalClass_BracketAccessor_ReadOnly;
  18726. begin
  18727. StartProgram(false);
  18728. Add('{$modeswitch externalclass}');
  18729. Add('type');
  18730. Add(' TJSArray = class external name ''Array2''');
  18731. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  18732. Add(' property Items[Index: longint]: jsvalue read GetItems; default;');
  18733. Add(' end;');
  18734. Add('procedure DoIt(vI: JSValue; const vJ: jsvalue);');
  18735. Add('begin end;');
  18736. Add('var');
  18737. Add(' Arr: tjsarray;');
  18738. Add(' v: jsvalue;');
  18739. Add('begin');
  18740. Add(' v:=arr[0];');
  18741. Add(' v:=arr.items[1];');
  18742. Add(' with arr do v:=items[2];');
  18743. Add(' doit(arr[3],arr[4]);');
  18744. ConvertProgram;
  18745. CheckSource('TestExternalClass_BracketAccessor_ReadOnly',
  18746. LinesToStr([ // statements
  18747. 'this.DoIt = function (vI, vJ) {',
  18748. '};',
  18749. 'this.Arr = null;',
  18750. 'this.v = undefined;',
  18751. '']),
  18752. LinesToStr([ // $mod.$main
  18753. '$mod.v = $mod.Arr[0];',
  18754. '$mod.v = $mod.Arr[1];',
  18755. 'var $with = $mod.Arr;',
  18756. '$mod.v = $with[2];',
  18757. '$mod.DoIt($mod.Arr[3], $mod.Arr[4]);',
  18758. '']));
  18759. end;
  18760. procedure TTestModule.TestExternalClass_BracketAccessor_WriteOnly;
  18761. begin
  18762. StartProgram(false);
  18763. Add('{$modeswitch externalclass}');
  18764. Add('type');
  18765. Add(' TJSArray = class external name ''Array2''');
  18766. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  18767. Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
  18768. Add(' end;');
  18769. Add('var');
  18770. Add(' Arr: tjsarray;');
  18771. Add(' s: string;');
  18772. Add(' i: longint;');
  18773. Add(' v: jsvalue;');
  18774. Add('begin');
  18775. Add(' arr[2]:=s;');
  18776. Add(' arr.items[3]:=s;');
  18777. Add(' arr[4]:=i;');
  18778. Add(' with arr do items[5]:=i;');
  18779. ConvertProgram;
  18780. CheckSource('TestExternalClass_BracketAccessor_WriteOnly',
  18781. LinesToStr([ // statements
  18782. 'this.Arr = null;',
  18783. 'this.s = "";',
  18784. 'this.i = 0;',
  18785. 'this.v = undefined;',
  18786. '']),
  18787. LinesToStr([ // $mod.$main
  18788. '$mod.Arr[2] = $mod.s;',
  18789. '$mod.Arr[3] = $mod.s;',
  18790. '$mod.Arr[4] = $mod.i;',
  18791. 'var $with = $mod.Arr;',
  18792. '$with[5] = $mod.i;',
  18793. '']));
  18794. end;
  18795. procedure TTestModule.TestExternalClass_BracketAccessor_MultiType;
  18796. begin
  18797. StartProgram(false);
  18798. Add('{$modeswitch externalclass}');
  18799. Add('type');
  18800. Add(' TJSArray = class external name ''Array2''');
  18801. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  18802. Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
  18803. Add(' procedure SetNumbers(Index: longint; Value: longint); external name ''[]'';');
  18804. Add(' property Numbers[Index: longint]: longint write SetNumbers;');
  18805. Add(' end;');
  18806. Add('var');
  18807. Add(' Arr: tjsarray;');
  18808. Add(' s: string;');
  18809. Add(' i: longint;');
  18810. Add(' v: jsvalue;');
  18811. Add('begin');
  18812. Add(' arr[2]:=s;');
  18813. Add(' arr.items[3]:=s;');
  18814. Add(' arr.numbers[4]:=i;');
  18815. Add(' with arr do items[5]:=i;');
  18816. Add(' with arr do numbers[6]:=i;');
  18817. ConvertProgram;
  18818. CheckSource('TestExternalClass_BracketAccessor_MultiType',
  18819. LinesToStr([ // statements
  18820. 'this.Arr = null;',
  18821. 'this.s = "";',
  18822. 'this.i = 0;',
  18823. 'this.v = undefined;',
  18824. '']),
  18825. LinesToStr([ // $mod.$main
  18826. '$mod.Arr[2] = $mod.s;',
  18827. '$mod.Arr[3] = $mod.s;',
  18828. '$mod.Arr[4] = $mod.i;',
  18829. 'var $with = $mod.Arr;',
  18830. '$with[5] = $mod.i;',
  18831. 'var $with1 = $mod.Arr;',
  18832. '$with1[6] = $mod.i;',
  18833. '']));
  18834. end;
  18835. procedure TTestModule.TestExternalClass_BracketAccessor_Index;
  18836. begin
  18837. StartProgram(false);
  18838. Add('{$modeswitch externalclass}');
  18839. Add('type');
  18840. Add(' TJSArray = class external name ''Array2''');
  18841. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  18842. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  18843. Add(' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;');
  18844. Add(' end;');
  18845. Add('var');
  18846. Add(' Arr: tjsarray;');
  18847. Add(' i: longint;');
  18848. Add(' IntArr: array of longint;');
  18849. Add(' v: jsvalue;');
  18850. Add('begin');
  18851. Add(' v:=arr.items[i];');
  18852. Add(' arr[longint(v)]:=arr.items[intarr[0]];');
  18853. Add(' arr.items[intarr[1]]:=arr[IntArr[2]];');
  18854. ConvertProgram;
  18855. CheckSource('TestExternalClass_BracketAccessor_Index',
  18856. LinesToStr([ // statements
  18857. 'this.Arr = null;',
  18858. 'this.i = 0;',
  18859. 'this.IntArr = [];',
  18860. 'this.v = undefined;',
  18861. '']),
  18862. LinesToStr([ // $mod.$main
  18863. '$mod.v = $mod.Arr[$mod.i];',
  18864. '$mod.Arr[rtl.trunc($mod.v)] = $mod.Arr[$mod.IntArr[0]];',
  18865. '$mod.Arr[$mod.IntArr[1]] = $mod.Arr[$mod.IntArr[2]];',
  18866. '']));
  18867. end;
  18868. procedure TTestModule.TestExternalClass_ForInJSObject;
  18869. begin
  18870. StartProgram(false);
  18871. Add([
  18872. '{$modeswitch externalclass}',
  18873. 'type',
  18874. ' TJSObject = class external name ''Object''',
  18875. ' end;',
  18876. 'var',
  18877. ' o: TJSObject;',
  18878. ' key: string;',
  18879. 'begin',
  18880. ' for key in o do',
  18881. ' if key=''abc'' then ;',
  18882. '']);
  18883. ConvertProgram;
  18884. CheckSource('TestExternalClass_ForInJSObject',
  18885. LinesToStr([ // statements
  18886. 'this.o = null;',
  18887. 'this.key = "";',
  18888. '']),
  18889. LinesToStr([ // $mod.$main
  18890. 'for ($mod.key in $mod.o) if ($mod.key === "abc") ;',
  18891. '']));
  18892. end;
  18893. procedure TTestModule.TestExternalClass_ForInJSArray;
  18894. begin
  18895. StartProgram(false);
  18896. Add([
  18897. '{$modeswitch externalclass}',
  18898. 'type',
  18899. ' TJSInt8Array = class external name ''Int8Array''',
  18900. ' private',
  18901. ' flength: NativeInt external name ''length'';',
  18902. ' function getValue(Index: NativeInt): shortint; external name ''[]'';',
  18903. ' public',
  18904. ' property values[Index: NativeInt]: Shortint Read getValue; default;',
  18905. ' property Length: NativeInt read flength;',
  18906. ' end;',
  18907. 'var',
  18908. ' a: TJSInt8Array;',
  18909. ' value: shortint;',
  18910. 'begin',
  18911. ' for value in a do',
  18912. ' if value=3 then ;',
  18913. '']);
  18914. ConvertProgram;
  18915. CheckSource('TestExternalClass_ForInJSArray',
  18916. LinesToStr([ // statements
  18917. 'this.a = null;',
  18918. 'this.value = 0;',
  18919. '']),
  18920. LinesToStr([ // $mod.$main
  18921. 'for (var $in = $mod.a, $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) {',
  18922. ' $mod.value = $in[$l];',
  18923. ' if ($mod.value === 3) ;',
  18924. '};',
  18925. '']));
  18926. end;
  18927. procedure TTestModule.TestExternalClass_IncompatibleArgDuplicateIdentifier;
  18928. begin
  18929. AddModuleWithIntfImplSrc('unit2.pas',
  18930. LinesToStr([
  18931. '{$modeswitch externalclass}',
  18932. 'type',
  18933. ' TJSBufferSource = class external name ''BufferSource''',
  18934. ' end;',
  18935. 'procedure DoIt(s: TJSBufferSource); external name ''DoIt'';',
  18936. '']),
  18937. '');
  18938. AddModuleWithIntfImplSrc('unit3.pas',
  18939. LinesToStr([
  18940. '{$modeswitch externalclass}',
  18941. 'type',
  18942. ' TJSBufferSource = class external name ''BufferSource''',
  18943. ' end;',
  18944. '']),
  18945. '');
  18946. StartUnit(true);
  18947. Add([
  18948. 'interface',
  18949. 'uses unit2, unit3;',
  18950. 'procedure DoSome(s: TJSBufferSource);',
  18951. 'implementation',
  18952. 'procedure DoSome(s: TJSBufferSource);',
  18953. 'begin',
  18954. ' DoIt(s);',
  18955. 'end;',
  18956. '']);
  18957. SetExpectedPasResolverError('Incompatible type arg no. 1: Got "unit3.TJSBufferSource", expected "unit2.TJSBufferSource"',
  18958. nIncompatibleTypeArgNo);
  18959. ConvertUnit;
  18960. end;
  18961. procedure TTestModule.TestClassInterface_Corba;
  18962. begin
  18963. StartProgram(false);
  18964. Add([
  18965. '{$interfaces corba}',
  18966. 'type',
  18967. ' IUnknown = interface;',
  18968. ' IUnknown = interface',
  18969. ' [''{00000000-0000-0000-C000-000000000046}'']',
  18970. ' end;',
  18971. ' IInterface = IUnknown;',
  18972. ' IBird = interface(IInterface)',
  18973. ' function GetSize: longint;',
  18974. ' procedure SetSize(i: longint);',
  18975. ' property Size: longint read GetSize write SetSize;',
  18976. ' procedure DoIt(i: longint);',
  18977. ' end;',
  18978. ' TObject = class',
  18979. ' end;',
  18980. ' TBird = class(TObject,IBird)',
  18981. ' function GetSize: longint; virtual; abstract;',
  18982. ' procedure SetSize(i: longint); virtual; abstract;',
  18983. ' procedure DoIt(i: longint); virtual; abstract;',
  18984. ' end;',
  18985. 'var',
  18986. ' BirdIntf: IBird;',
  18987. 'begin',
  18988. ' BirdIntf.Size:=BirdIntf.Size;',
  18989. '']);
  18990. ConvertProgram;
  18991. CheckSource('TestClassInterface_Corba',
  18992. LinesToStr([ // statements
  18993. 'rtl.createInterface(this, "IUnknown", "{00000000-0000-0000-C000-000000000046}", [], null);',
  18994. 'rtl.createInterface(this, "IBird", "{5BD1A53B-69BB-37EE-AF32-BEFB86D85B03}", ["GetSize", "SetSize", "DoIt"], this.IUnknown);',
  18995. 'rtl.createClass(this, "TObject", null, function () {',
  18996. ' this.$init = function () {',
  18997. ' };',
  18998. ' this.$final = function () {',
  18999. ' };',
  19000. '});',
  19001. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  19002. ' rtl.addIntf(this, $mod.IBird);',
  19003. '});',
  19004. 'this.BirdIntf = null;',
  19005. '']),
  19006. LinesToStr([ // $mod.$main
  19007. ' $mod.BirdIntf.SetSize($mod.BirdIntf.GetSize());',
  19008. '']));
  19009. end;
  19010. procedure TTestModule.TestClassInterface_ProcExternalFail;
  19011. begin
  19012. StartProgram(false);
  19013. Add([
  19014. '{$interfaces corba}',
  19015. 'type',
  19016. ' IUnknown = interface',
  19017. ' procedure DoIt; external name ''foo'';',
  19018. ' end;',
  19019. 'begin']);
  19020. SetExpectedParserError(
  19021. 'Fields are not allowed in interface at token "Identifier external" in file test1.pp at line 6 column 21',
  19022. nParserNoFieldsAllowed);
  19023. ConvertProgram;
  19024. end;
  19025. procedure TTestModule.TestClassInterface_Overloads;
  19026. begin
  19027. StartProgram(false);
  19028. Add([
  19029. '{$interfaces corba}',
  19030. 'type',
  19031. ' integer = longint;',
  19032. ' IUnknown = interface',
  19033. ' procedure DoIt(i: integer);',
  19034. ' procedure DoIt(s: string);',
  19035. ' end;',
  19036. ' IBird = interface(IUnknown)',
  19037. ' procedure DoIt(b: boolean); overload;',
  19038. ' end;',
  19039. ' TObject = class',
  19040. ' end;',
  19041. ' TBird = class(TObject,IBird)',
  19042. ' procedure DoIt(o: TObject);',
  19043. ' procedure DoIt(s: string);',
  19044. ' procedure DoIt(i: integer);',
  19045. ' procedure DoIt(b: boolean);',
  19046. ' end;',
  19047. 'procedure TBird.DoIt(o: TObject); begin end;',
  19048. 'procedure TBird.DoIt(s: string); begin end;',
  19049. 'procedure TBird.DoIt(i: integer); begin end;',
  19050. 'procedure TBird.DoIt(b: boolean); begin end;',
  19051. 'var',
  19052. ' BirdIntf: IBird;',
  19053. 'begin',
  19054. ' BirdIntf.DoIt(3);',
  19055. ' BirdIntf.DoIt(''abc'');',
  19056. ' BirdIntf.DoIt(true);',
  19057. '']);
  19058. ConvertProgram;
  19059. CheckSource('TestClassInterface_Overloads',
  19060. LinesToStr([ // statements
  19061. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDC4-8A2AE2C59400}", ["DoIt", "DoIt$1"], null);',
  19062. 'rtl.createInterface(this, "IBird", "{8285DD5E-EA3E-396E-AE88-000B86AABF05}", ["DoIt$2"], this.IUnknown);',
  19063. 'rtl.createClass(this, "TObject", null, function () {',
  19064. ' this.$init = function () {',
  19065. ' };',
  19066. ' this.$final = function () {',
  19067. ' };',
  19068. '});',
  19069. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  19070. ' this.DoIt = function (o) {',
  19071. ' };',
  19072. ' this.DoIt$1 = function (s) {',
  19073. ' };',
  19074. ' this.DoIt$2 = function (i) {',
  19075. ' };',
  19076. ' this.DoIt$3 = function (b) {',
  19077. ' };',
  19078. ' rtl.addIntf(this, $mod.IBird, {',
  19079. ' DoIt$2: "DoIt$3",',
  19080. ' DoIt: "DoIt$2"',
  19081. ' });',
  19082. '});',
  19083. 'this.BirdIntf = null;',
  19084. '']),
  19085. LinesToStr([ // $mod.$main
  19086. '$mod.BirdIntf.DoIt(3);',
  19087. '$mod.BirdIntf.DoIt$1("abc");',
  19088. '$mod.BirdIntf.DoIt$2(true);',
  19089. '']));
  19090. end;
  19091. procedure TTestModule.TestClassInterface_DuplicateGUIInIntfListFail;
  19092. begin
  19093. StartProgram(false);
  19094. Add([
  19095. '{$interfaces corba}',
  19096. 'type',
  19097. ' IBird = interface',
  19098. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  19099. ' end;',
  19100. ' IDog = interface',
  19101. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  19102. ' end;',
  19103. ' TObject = class(IBird,IDog)',
  19104. ' end;',
  19105. 'begin']);
  19106. SetExpectedPasResolverError('Duplicate GUID {4B3BA825-E0EC-4799-A19C-55F714A07959} in IDog and IBird',
  19107. nDuplicateGUIDXInYZ);
  19108. ConvertProgram;
  19109. end;
  19110. procedure TTestModule.TestClassInterface_DuplicateGUIInAncestorFail;
  19111. begin
  19112. StartProgram(false);
  19113. Add([
  19114. '{$interfaces corba}',
  19115. 'type',
  19116. ' IAnimal = interface',
  19117. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  19118. ' end;',
  19119. ' IBird = interface(IAnimal)',
  19120. ' end;',
  19121. ' IHawk = interface(IBird)',
  19122. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  19123. ' end;',
  19124. 'begin']);
  19125. SetExpectedPasResolverError('Duplicate GUID {4B3BA825-E0EC-4799-A19C-55F714A07959} in IHawk and IAnimal',
  19126. nDuplicateGUIDXInYZ);
  19127. ConvertProgram;
  19128. end;
  19129. procedure TTestModule.TestClassInterface_AncestorImpl;
  19130. begin
  19131. StartProgram(false);
  19132. Add([
  19133. '{$interfaces corba}',
  19134. 'type',
  19135. ' integer = longint;',
  19136. ' IUnknown = interface',
  19137. ' procedure DoIt(i: integer);',
  19138. ' end;',
  19139. ' IBird = interface',
  19140. ' procedure Fly(i: integer);',
  19141. ' end;',
  19142. ' TObject = class(IUnknown)',
  19143. ' procedure DoIt(i: integer);',
  19144. ' end;',
  19145. ' TBird = class(IBird)',
  19146. ' procedure Fly(i: integer);',
  19147. ' end;',
  19148. 'procedure TObject.DoIt(i: integer); begin end;',
  19149. 'procedure TBird.Fly(i: integer); begin end;',
  19150. 'begin',
  19151. '']);
  19152. ConvertProgram;
  19153. CheckSource('TestClassInterface_AncestorIntf',
  19154. LinesToStr([ // statements
  19155. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDC4-8A2800000000}", ["DoIt"], null);',
  19156. 'rtl.createInterface(this, "IBird", "{B92D5841-6264-3AE3-BF20-000000000000}", ["Fly"], null);',
  19157. 'rtl.createClass(this, "TObject", null, function () {',
  19158. ' this.$init = function () {',
  19159. ' };',
  19160. ' this.$final = function () {',
  19161. ' };',
  19162. ' this.DoIt = function (i) {',
  19163. ' };',
  19164. ' rtl.addIntf(this, $mod.IUnknown);',
  19165. '});',
  19166. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  19167. ' this.Fly = function (i) {',
  19168. ' };',
  19169. ' rtl.addIntf(this, $mod.IBird);',
  19170. ' rtl.addIntf(this, $mod.IUnknown);',
  19171. '});',
  19172. '']),
  19173. LinesToStr([ // $mod.$main
  19174. '']));
  19175. end;
  19176. procedure TTestModule.TestClassInterface_ImplReintroduce;
  19177. begin
  19178. StartProgram(false);
  19179. Add([
  19180. '{$interfaces corba}',
  19181. 'type',
  19182. ' integer = longint;',
  19183. ' IBird = interface',
  19184. ' procedure DoIt(i: integer);',
  19185. ' end;',
  19186. ' TObject = class',
  19187. ' procedure DoIt(i: integer);',
  19188. ' end;',
  19189. ' TBird = class(IBird)',
  19190. ' procedure DoIt(i: integer); virtual; reintroduce;',
  19191. ' end;',
  19192. 'procedure TObject.DoIt(i: integer); begin end;',
  19193. 'procedure TBird.DoIt(i: integer); begin end;',
  19194. 'begin',
  19195. '']);
  19196. ConvertProgram;
  19197. CheckSource('TestClassInterface_ImplReintroduce',
  19198. LinesToStr([ // statements
  19199. 'rtl.createInterface(this, "IBird", "{B92D5841-6264-3AE2-8594-000000000000}", ["DoIt"], null);',
  19200. 'rtl.createClass(this, "TObject", null, function () {',
  19201. ' this.$init = function () {',
  19202. ' };',
  19203. ' this.$final = function () {',
  19204. ' };',
  19205. ' this.DoIt = function (i) {',
  19206. ' };',
  19207. '});',
  19208. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  19209. ' this.DoIt$1 = function (i) {',
  19210. ' };',
  19211. ' rtl.addIntf(this, $mod.IBird, {',
  19212. ' DoIt: "DoIt$1"',
  19213. ' });',
  19214. '});',
  19215. '']),
  19216. LinesToStr([ // $mod.$main
  19217. '']));
  19218. end;
  19219. procedure TTestModule.TestClassInterface_MethodResolution;
  19220. begin
  19221. StartProgram(false);
  19222. Add([
  19223. '{$interfaces corba}',
  19224. 'type',
  19225. ' IUnknown = interface',
  19226. ' procedure Walk(i: longint);',
  19227. ' end;',
  19228. ' IBird = interface(IUnknown)',
  19229. ' procedure Walk(b: boolean); overload;',
  19230. ' procedure Fly(s: string);',
  19231. ' end;',
  19232. ' TObject = class',
  19233. ' end;',
  19234. ' TBird = class(TObject,IBird)',
  19235. ' procedure IBird.Fly = Move;',
  19236. ' procedure IBird.Walk = Hop;',
  19237. ' procedure Hop(i: longint);',
  19238. ' procedure Move(s: string);',
  19239. ' procedure Hop(b: boolean);',
  19240. ' end;',
  19241. 'procedure TBird.Move(s: string); begin end;',
  19242. 'procedure TBird.Hop(i: longint); begin end;',
  19243. 'procedure TBird.Hop(b: boolean); begin end;',
  19244. 'var',
  19245. ' BirdIntf: IBird;',
  19246. 'begin',
  19247. ' BirdIntf.Walk(3);',
  19248. ' BirdIntf.Walk(true);',
  19249. ' BirdIntf.Fly(''abc'');',
  19250. '']);
  19251. ConvertProgram;
  19252. CheckSource('TestClassInterface_MethodResolution',
  19253. LinesToStr([ // statements
  19254. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDD7-23D600000000}", ["Walk"], null);',
  19255. 'rtl.createInterface(this, "IBird", "{CF8A4986-80F6-396E-AE88-000B86AAE208}", ["Walk$1", "Fly"], this.IUnknown);',
  19256. 'rtl.createClass(this, "TObject", null, function () {',
  19257. ' this.$init = function () {',
  19258. ' };',
  19259. ' this.$final = function () {',
  19260. ' };',
  19261. '});',
  19262. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  19263. ' this.Hop = function (i) {',
  19264. ' };',
  19265. ' this.Move = function (s) {',
  19266. ' };',
  19267. ' this.Hop$1 = function (b) {',
  19268. ' };',
  19269. ' rtl.addIntf(this, $mod.IBird, {',
  19270. ' Walk$1: "Hop$1",',
  19271. ' Fly: "Move",',
  19272. ' Walk: "Hop"',
  19273. ' });',
  19274. '});',
  19275. 'this.BirdIntf = null;',
  19276. '']),
  19277. LinesToStr([ // $mod.$main
  19278. '$mod.BirdIntf.Walk(3);',
  19279. '$mod.BirdIntf.Walk$1(true);',
  19280. '$mod.BirdIntf.Fly("abc");',
  19281. '']));
  19282. end;
  19283. procedure TTestModule.TestClassInterface_AncestorMoreInterfaces;
  19284. begin
  19285. StartProgram(false);
  19286. Add([
  19287. '{$interfaces com}',
  19288. 'type',
  19289. ' IUnknown = interface',
  19290. ' function _AddRef: longint;',
  19291. ' procedure Walk;',
  19292. ' end;',
  19293. ' IBird = interface end;',
  19294. ' IDog = interface end;',
  19295. ' TObject = class(IBird,IDog)',
  19296. ' function _AddRef: longint; virtual; abstract;',
  19297. ' procedure Walk; virtual; abstract;',
  19298. ' end;',
  19299. ' TBird = class(IUnknown)',
  19300. ' end;',
  19301. 'begin',
  19302. '']);
  19303. ConvertProgram;
  19304. CheckSource('TestClassInterface_COM_AncestorLess',
  19305. LinesToStr([ // statements
  19306. 'rtl.createInterface(this, "IUnknown", "{8F2D5841-758A-322B-BDDF-21CD521DD723}", ["_AddRef", "Walk"], null);',
  19307. 'rtl.createInterface(this, "IBird", "{CCE11D4C-6504-3AEE-AE88-000B86AAE675}", [], this.IUnknown);',
  19308. 'rtl.createInterface(this, "IDog", "{CCE11D4C-6504-3AEE-AE88-000B8E5FC675}", [], this.IUnknown);',
  19309. 'rtl.createClass(this, "TObject", null, function () {',
  19310. ' this.$init = function () {',
  19311. ' };',
  19312. ' this.$final = function () {',
  19313. ' };',
  19314. ' rtl.addIntf(this, $mod.IBird);',
  19315. ' rtl.addIntf(this, $mod.IDog);',
  19316. '});',
  19317. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  19318. ' rtl.addIntf(this, $mod.IUnknown);',
  19319. ' rtl.addIntf(this, $mod.IBird);',
  19320. ' rtl.addIntf(this, $mod.IDog);',
  19321. '});',
  19322. '']),
  19323. LinesToStr([ // $mod.$main
  19324. '']));
  19325. end;
  19326. procedure TTestModule.TestClassInterface_MethodOverride;
  19327. begin
  19328. StartProgram(false);
  19329. Add([
  19330. '{$interfaces corba}',
  19331. 'type',
  19332. ' IUnknown = interface',
  19333. ' [''{D6D98E5B-8A10-4FEC-856A-7BFC847FE74B}'']',
  19334. ' procedure Go;',
  19335. ' end;',
  19336. ' TObject = class(IUnknown)',
  19337. ' procedure Go; virtual; abstract;',
  19338. ' end;',
  19339. ' TBird = class',
  19340. ' procedure Go; override;',
  19341. ' end;',
  19342. ' TCat = class(TObject)',
  19343. ' procedure Go; override;',
  19344. ' end;',
  19345. ' TDog = class(TObject, IUnknown)',
  19346. ' procedure Go; override;',
  19347. ' end;',
  19348. 'procedure TBird.Go; begin end;',
  19349. 'procedure TCat.Go; begin end;',
  19350. 'procedure TDog.Go; begin end;',
  19351. 'begin',
  19352. '']);
  19353. ConvertProgram;
  19354. CheckSource('TestClassInterface_MethodOverride',
  19355. LinesToStr([ // statements
  19356. 'rtl.createInterface(this, "IUnknown", "{D6D98E5B-8A10-4FEC-856A-7BFC847FE74B}", ["Go"], null);',
  19357. 'rtl.createClass(this, "TObject", null, function () {',
  19358. ' this.$init = function () {',
  19359. ' };',
  19360. ' this.$final = function () {',
  19361. ' };',
  19362. ' rtl.addIntf(this, $mod.IUnknown);',
  19363. '});',
  19364. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  19365. ' this.Go = function () {',
  19366. ' };',
  19367. ' rtl.addIntf(this, $mod.IUnknown);',
  19368. '});',
  19369. 'rtl.createClass(this, "TCat", this.TObject, function () {',
  19370. ' this.Go = function () {',
  19371. ' };',
  19372. ' rtl.addIntf(this, $mod.IUnknown);',
  19373. '});',
  19374. 'rtl.createClass(this, "TDog", this.TObject, function () {',
  19375. ' this.Go = function () {',
  19376. ' };',
  19377. ' rtl.addIntf(this, $mod.IUnknown);',
  19378. '});',
  19379. '']),
  19380. LinesToStr([ // $mod.$main
  19381. '']));
  19382. end;
  19383. procedure TTestModule.TestClassInterface_Corba_Delegation;
  19384. begin
  19385. StartProgram(false);
  19386. Add([
  19387. '{$interfaces corba}',
  19388. 'type',
  19389. ' IUnknown = interface',
  19390. ' end;',
  19391. ' IBird = interface(IUnknown)',
  19392. ' procedure Fly(s: string);',
  19393. ' end;',
  19394. ' IEagle = interface(IBird)',
  19395. ' end;',
  19396. ' IDove = interface(IBird)',
  19397. ' end;',
  19398. ' ISwallow = interface(IBird)',
  19399. ' end;',
  19400. ' TObject = class',
  19401. ' end;',
  19402. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  19403. ' procedure Fly(s: string); virtual; abstract;',
  19404. ' end;',
  19405. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  19406. ' FBirdIntf: IBird;',
  19407. ' property BirdIntf: IBird read FBirdIntf implements IBird;',
  19408. ' function GetEagleIntf: IEagle; virtual; abstract;',
  19409. ' property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  19410. ' FDoveObj: TBird;',
  19411. ' property DoveObj: TBird read FDoveObj implements IDove;',
  19412. ' function GetSwallowObj: TBird; virtual; abstract;',
  19413. ' property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  19414. ' end;',
  19415. 'begin',
  19416. '']);
  19417. ConvertProgram;
  19418. CheckSource('TestClassInterface_Corba_Delegation',
  19419. LinesToStr([ // statements
  19420. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19421. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  19422. 'rtl.createInterface(this, "IEagle", "{489289DE-FDE2-34A6-8288-39119022B1B4}", [], this.IBird);',
  19423. 'rtl.createInterface(this, "IDove", "{489289DE-FDE2-34A6-8288-39118EF16074}", [], this.IBird);',
  19424. 'rtl.createInterface(this, "ISwallow", "{B89289DE-FDE2-34A6-8288-3911CBDCB359}", [], this.IBird);',
  19425. 'rtl.createClass(this, "TObject", null, function () {',
  19426. ' this.$init = function () {',
  19427. ' };',
  19428. ' this.$final = function () {',
  19429. ' };',
  19430. '});',
  19431. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  19432. ' rtl.addIntf(this, $mod.IBird);',
  19433. ' rtl.addIntf(this, $mod.IEagle);',
  19434. ' rtl.addIntf(this, $mod.IDove);',
  19435. ' rtl.addIntf(this, $mod.ISwallow);',
  19436. '});',
  19437. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  19438. ' this.$init = function () {',
  19439. ' $mod.TObject.$init.call(this);',
  19440. ' this.FBirdIntf = null;',
  19441. ' this.FDoveObj = null;',
  19442. ' };',
  19443. ' this.$final = function () {',
  19444. ' this.FBirdIntf = undefined;',
  19445. ' this.FDoveObj = undefined;',
  19446. ' $mod.TObject.$final.call(this);',
  19447. ' };',
  19448. ' this.$intfmaps = {',
  19449. ' "{478D080B-C0F6-396E-AE88-000B87785B07}": function () {',
  19450. ' return this.FBirdIntf;',
  19451. ' },',
  19452. ' "{489289DE-FDE2-34A6-8288-39119022B1B4}": function () {',
  19453. ' return this.GetEagleIntf();',
  19454. ' },',
  19455. ' "{489289DE-FDE2-34A6-8288-39118EF16074}": function () {',
  19456. ' return rtl.getIntfT(this.FDoveObj, $mod.IDove);',
  19457. ' },',
  19458. ' "{B89289DE-FDE2-34A6-8288-3911CBDCB359}": function () {',
  19459. ' return rtl.getIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  19460. ' }',
  19461. ' };',
  19462. '});',
  19463. '']),
  19464. LinesToStr([ // $mod.$main
  19465. '']));
  19466. end;
  19467. procedure TTestModule.TestClassInterface_Corba_DelegationStatic;
  19468. begin
  19469. StartProgram(false);
  19470. Add([
  19471. '{$interfaces corba}',
  19472. 'type',
  19473. ' IUnknown = interface',
  19474. ' end;',
  19475. ' IBird = interface(IUnknown)',
  19476. ' procedure Fly(s: string);',
  19477. ' end;',
  19478. ' IEagle = interface(IBird)',
  19479. ' end;',
  19480. ' IDove = interface(IBird)',
  19481. ' end;',
  19482. ' ISwallow = interface(IBird)',
  19483. ' end;',
  19484. ' TObject = class',
  19485. ' end;',
  19486. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  19487. ' procedure Fly(s: string); virtual; abstract;',
  19488. ' end;',
  19489. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  19490. ' private',
  19491. ' class var FBirdIntf: IBird;',
  19492. ' class var FDoveObj: TBird;',
  19493. ' class function GetEagleIntf: IEagle; virtual; abstract;',
  19494. ' class function GetSwallowObj: TBird; virtual; abstract;',
  19495. ' protected',
  19496. ' class property BirdIntf: IBird read FBirdIntf implements IBird;',
  19497. ' class property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  19498. ' class property DoveObj: TBird read FDoveObj implements IDove;',
  19499. ' class property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  19500. ' end;',
  19501. 'begin',
  19502. '']);
  19503. ConvertProgram;
  19504. CheckSource('TestClassInterface_Corba_DelegationStatic',
  19505. LinesToStr([ // statements
  19506. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19507. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  19508. 'rtl.createInterface(this, "IEagle", "{489289DE-FDE2-34A6-8288-39119022B1B4}", [], this.IBird);',
  19509. 'rtl.createInterface(this, "IDove", "{489289DE-FDE2-34A6-8288-39118EF16074}", [], this.IBird);',
  19510. 'rtl.createInterface(this, "ISwallow", "{B89289DE-FDE2-34A6-8288-3911CBDCB359}", [], this.IBird);',
  19511. 'rtl.createClass(this, "TObject", null, function () {',
  19512. ' this.$init = function () {',
  19513. ' };',
  19514. ' this.$final = function () {',
  19515. ' };',
  19516. '});',
  19517. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  19518. ' rtl.addIntf(this, $mod.IBird);',
  19519. ' rtl.addIntf(this, $mod.IEagle);',
  19520. ' rtl.addIntf(this, $mod.IDove);',
  19521. ' rtl.addIntf(this, $mod.ISwallow);',
  19522. '});',
  19523. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  19524. ' this.FBirdIntf = null;',
  19525. ' this.FDoveObj = null;',
  19526. ' this.$intfmaps = {',
  19527. ' "{478D080B-C0F6-396E-AE88-000B87785B07}": function () {',
  19528. ' return this.FBirdIntf;',
  19529. ' },',
  19530. ' "{489289DE-FDE2-34A6-8288-39119022B1B4}": function () {',
  19531. ' return this.GetEagleIntf();',
  19532. ' },',
  19533. ' "{489289DE-FDE2-34A6-8288-39118EF16074}": function () {',
  19534. ' return rtl.getIntfT(this.FDoveObj, $mod.IDove);',
  19535. ' },',
  19536. ' "{B89289DE-FDE2-34A6-8288-3911CBDCB359}": function () {',
  19537. ' return rtl.getIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  19538. ' }',
  19539. ' };',
  19540. '});',
  19541. '']),
  19542. LinesToStr([ // $mod.$main
  19543. '']));
  19544. end;
  19545. procedure TTestModule.TestClassInterface_Corba_Operators;
  19546. begin
  19547. StartProgram(false);
  19548. Add([
  19549. '{$interfaces corba}',
  19550. 'type',
  19551. ' IUnknown = interface',
  19552. ' end;',
  19553. ' IBird = interface(IUnknown)',
  19554. ' function GetItems(Index: longint): longint;',
  19555. ' procedure SetItems(Index: longint; Value: longint);',
  19556. ' property Items[Index: longint]: longint read GetItems write SetItems; default;',
  19557. ' end;',
  19558. ' TObject = class',
  19559. ' end;',
  19560. ' TBird = class(TObject,IBird)',
  19561. ' function GetItems(Index: longint): longint; virtual; abstract;',
  19562. ' procedure SetItems(Index: longint; Value: longint); virtual; abstract;',
  19563. ' end;',
  19564. 'var',
  19565. ' IntfVar: IBird = nil;',
  19566. ' IntfVar2: IBird;',
  19567. ' ObjVar: TBird;',
  19568. ' v: JSValue;',
  19569. 'begin',
  19570. ' IntfVar:=nil;',
  19571. ' IntfVar[3]:=IntfVar[4];',
  19572. ' if Assigned(IntfVar) then ;',
  19573. ' IntfVar:=IntfVar2;',
  19574. ' IntfVar:=ObjVar;',
  19575. ' if IntfVar=IntfVar2 then ;',
  19576. ' if IntfVar<>IntfVar2 then ;',
  19577. ' if IntfVar is IBird then ;',
  19578. ' if IntfVar is TBird then ;',
  19579. ' if ObjVar is IBird then ;',
  19580. ' IntfVar:=IntfVar2 as IBird;',
  19581. ' ObjVar:=IntfVar2 as TBird;',
  19582. ' IntfVar:=ObjVar as IBird;',
  19583. ' IntfVar:=IBird(IntfVar2);',
  19584. ' ObjVar:=TBird(IntfVar);',
  19585. ' IntfVar:=IBird(ObjVar);',
  19586. ' v:=IntfVar;',
  19587. ' IntfVar:=IBird(v);',
  19588. ' if v is IBird then ;',
  19589. ' v:=JSValue(IntfVar);',
  19590. ' v:=IBird;',
  19591. '']);
  19592. ConvertProgram;
  19593. CheckSource('TestClassInterface_Corba_Operators',
  19594. LinesToStr([ // statements
  19595. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19596. 'rtl.createInterface(this, "IBird", "{D53FED90-DE59-3202-B1AE-000B87785B08}", ["GetItems", "SetItems"], this.IUnknown);',
  19597. 'rtl.createClass(this, "TObject", null, function () {',
  19598. ' this.$init = function () {',
  19599. ' };',
  19600. ' this.$final = function () {',
  19601. ' };',
  19602. '});',
  19603. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  19604. ' rtl.addIntf(this, $mod.IBird);',
  19605. '});',
  19606. 'this.IntfVar = null;',
  19607. 'this.IntfVar2 = null;',
  19608. 'this.ObjVar = null;',
  19609. 'this.v = undefined;',
  19610. '']),
  19611. LinesToStr([ // $mod.$main
  19612. '$mod.IntfVar = null;',
  19613. '$mod.IntfVar.SetItems(3, $mod.IntfVar.GetItems(4));',
  19614. 'if ($mod.IntfVar != null) ;',
  19615. '$mod.IntfVar = $mod.IntfVar2;',
  19616. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar,$mod.IBird);',
  19617. 'if ($mod.IntfVar === $mod.IntfVar2) ;',
  19618. 'if ($mod.IntfVar !== $mod.IntfVar2) ;',
  19619. 'if ($mod.IBird.isPrototypeOf($mod.IntfVar)) ;',
  19620. 'if (rtl.intfIsClass($mod.IntfVar, $mod.TBird)) ;',
  19621. 'if (rtl.getIntfT($mod.ObjVar, $mod.IBird) !== null) ;',
  19622. '$mod.IntfVar = rtl.as($mod.IntfVar2, $mod.IBird);',
  19623. '$mod.ObjVar = rtl.intfAsClass($mod.IntfVar2, $mod.TBird);',
  19624. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar, $mod.IBird);',
  19625. '$mod.IntfVar = $mod.IntfVar2;',
  19626. '$mod.ObjVar = rtl.intfToClass($mod.IntfVar, $mod.TBird);',
  19627. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar, $mod.IBird);',
  19628. '$mod.v = $mod.IntfVar;',
  19629. '$mod.IntfVar = rtl.getObject($mod.v);',
  19630. 'if (rtl.isExt($mod.v, $mod.IBird, 1)) ;',
  19631. '$mod.v = $mod.IntfVar;',
  19632. '$mod.v = $mod.IBird;',
  19633. '']));
  19634. end;
  19635. procedure TTestModule.TestClassInterface_Corba_Args;
  19636. begin
  19637. StartProgram(false);
  19638. Add([
  19639. '{$interfaces corba}',
  19640. 'type',
  19641. ' IUnknown = interface',
  19642. ' end;',
  19643. ' IBird = interface(IUnknown)',
  19644. ' end;',
  19645. ' TObject = class',
  19646. ' end;',
  19647. ' TBird = class(TObject,IBird)',
  19648. ' end;',
  19649. 'procedure DoIt(var u; i: IBird; const j: IBird);',
  19650. 'begin',
  19651. ' DoIt(i,i,i);',
  19652. 'end;',
  19653. 'procedure Change(var i: IBird; out j: IBird);',
  19654. 'begin',
  19655. ' DoIt(i,i,i);',
  19656. ' Change(i,i);',
  19657. 'end;',
  19658. 'var',
  19659. ' i: IBird;',
  19660. ' o: TBird;',
  19661. 'begin',
  19662. ' DoIt(i,i,i);',
  19663. ' Change(i,i);',
  19664. ' DoIt(o,o,o);',
  19665. '']);
  19666. ConvertProgram;
  19667. CheckSource('TestClassInterface_Corba_Args',
  19668. LinesToStr([ // statements
  19669. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19670. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], this.IUnknown);',
  19671. 'rtl.createClass(this, "TObject", null, function () {',
  19672. ' this.$init = function () {',
  19673. ' };',
  19674. ' this.$final = function () {',
  19675. ' };',
  19676. '});',
  19677. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  19678. ' rtl.addIntf(this, $mod.IBird);',
  19679. '});',
  19680. 'this.DoIt = function (u, i, j) {',
  19681. ' $mod.DoIt({',
  19682. ' get: function () {',
  19683. ' return i;',
  19684. ' },',
  19685. ' set: function (v) {',
  19686. ' i = v;',
  19687. ' }',
  19688. ' }, i, i);',
  19689. '};',
  19690. 'this.Change = function (i, j) {',
  19691. ' $mod.DoIt(i, i.get(), i.get());',
  19692. ' $mod.Change(i, i);',
  19693. '};',
  19694. 'this.i = null;',
  19695. 'this.o = null;',
  19696. '']),
  19697. LinesToStr([ // $mod.$main
  19698. '$mod.DoIt({',
  19699. ' p: $mod,',
  19700. ' get: function () {',
  19701. ' return this.p.i;',
  19702. ' },',
  19703. ' set: function (v) {',
  19704. ' this.p.i = v;',
  19705. ' }',
  19706. '}, $mod.i, $mod.i);',
  19707. '$mod.Change({',
  19708. ' p: $mod,',
  19709. ' get: function () {',
  19710. ' return this.p.i;',
  19711. ' },',
  19712. ' set: function (v) {',
  19713. ' this.p.i = v;',
  19714. ' }',
  19715. '}, {',
  19716. ' p: $mod,',
  19717. ' get: function () {',
  19718. ' return this.p.i;',
  19719. ' },',
  19720. ' set: function (v) {',
  19721. ' this.p.i = v;',
  19722. ' }',
  19723. '});',
  19724. '$mod.DoIt({',
  19725. ' p: $mod,',
  19726. ' get: function () {',
  19727. ' return this.p.o;',
  19728. ' },',
  19729. ' set: function (v) {',
  19730. ' this.p.o = v;',
  19731. ' }',
  19732. '}, rtl.getIntfT($mod.o, $mod.IBird), rtl.getIntfT($mod.o, $mod.IBird));',
  19733. '']));
  19734. end;
  19735. procedure TTestModule.TestClassInterface_Corba_ForIn;
  19736. begin
  19737. StartProgram(false);
  19738. Add([
  19739. '{$interfaces corba}',
  19740. 'type',
  19741. ' IUnknown = interface end;',
  19742. ' TObject = class',
  19743. ' Id: longint;',
  19744. ' end;',
  19745. ' IEnumerator = interface(IUnknown)',
  19746. ' function GetCurrent: TObject;',
  19747. ' function MoveNext: Boolean;',
  19748. ' property Current: TObject read GetCurrent;',
  19749. ' end;',
  19750. ' IEnumerable = interface(IUnknown)',
  19751. ' function GetEnumerator: IEnumerator;',
  19752. ' end;',
  19753. 'var',
  19754. ' o: TObject;',
  19755. ' i: IEnumerable;',
  19756. 'begin',
  19757. ' for o in i do o.Id:=3;',
  19758. '']);
  19759. ConvertProgram;
  19760. CheckSource('TestClassInterface_Corba_ForIn',
  19761. LinesToStr([ // statements
  19762. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19763. 'rtl.createClass(this, "TObject", null, function () {',
  19764. ' this.$init = function () {',
  19765. ' this.Id = 0;',
  19766. ' };',
  19767. ' this.$final = function () {',
  19768. ' };',
  19769. '});',
  19770. 'rtl.createInterface(this, "IEnumerator", "{95D7745D-ED61-3F13-BBE4-07708161999E}", ["GetCurrent", "MoveNext"], this.IUnknown);',
  19771. 'rtl.createInterface(this, "IEnumerable", "{8CC9D45D-ED7D-3B73-96B6-290B931BB19E}", ["GetEnumerator"], this.IUnknown);',
  19772. 'this.o = null;',
  19773. 'this.i = null;',
  19774. '']),
  19775. LinesToStr([ // $mod.$main
  19776. 'var $in = $mod.i.GetEnumerator();',
  19777. 'while ($in.MoveNext()) {',
  19778. ' $mod.o = $in.GetCurrent();',
  19779. ' $mod.o.Id = 3;',
  19780. '};',
  19781. '']));
  19782. end;
  19783. procedure TTestModule.TestClassInterface_COM_AssignVar;
  19784. begin
  19785. StartProgram(false);
  19786. Add([
  19787. '{$interfaces com}',
  19788. 'type',
  19789. ' IUnknown = interface',
  19790. ' function _AddRef: longint;',
  19791. ' function _Release: longint;',
  19792. ' end;',
  19793. ' TObject = class(IUnknown)',
  19794. ' function _AddRef: longint; virtual; abstract;',
  19795. ' function _Release: longint; virtual; abstract;',
  19796. ' end;',
  19797. 'var',
  19798. ' i: IUnknown;',
  19799. 'procedure DoGlobal(o: TObject);',
  19800. 'begin',
  19801. ' i:=nil;',
  19802. ' i:=o;',
  19803. ' i:=i;',
  19804. 'end;',
  19805. 'procedure DoLocal(o: TObject);',
  19806. 'const k: IUnknown = nil;',
  19807. 'var j: IUnknown;',
  19808. 'begin',
  19809. ' k:=o;',
  19810. ' k:=i;',
  19811. ' j:=o;',
  19812. ' j:=i;',
  19813. 'end;',
  19814. 'var o: TObject;',
  19815. 'begin',
  19816. ' i:=nil;',
  19817. ' i:=o;',
  19818. '']);
  19819. ConvertProgram;
  19820. CheckSource('TestClassInterface_COM_AssignVar',
  19821. LinesToStr([ // statements
  19822. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19823. 'rtl.createClass(this, "TObject", null, function () {',
  19824. ' this.$init = function () {',
  19825. ' };',
  19826. ' this.$final = function () {',
  19827. ' };',
  19828. ' rtl.addIntf(this, $mod.IUnknown);',
  19829. '});',
  19830. 'this.i = null;',
  19831. 'this.DoGlobal = function (o) {',
  19832. ' rtl.setIntfP($mod, "i", null);',
  19833. ' rtl.setIntfP($mod, "i", rtl.queryIntfT(o, $mod.IUnknown), true);',
  19834. ' rtl.setIntfP($mod, "i", $mod.i);',
  19835. '};',
  19836. 'var k = null;',
  19837. 'this.DoLocal = function (o) {',
  19838. ' var j = null;',
  19839. ' try{',
  19840. ' k = rtl.setIntfL(k, rtl.queryIntfT(o, $mod.IUnknown), true);',
  19841. ' k = rtl.setIntfL(k, $mod.i);',
  19842. ' j = rtl.setIntfL(j, rtl.queryIntfT(o, $mod.IUnknown), true);',
  19843. ' j = rtl.setIntfL(j, $mod.i);',
  19844. ' }finally{',
  19845. ' rtl._Release(j);',
  19846. ' };',
  19847. '};',
  19848. 'this.o = null;',
  19849. '']),
  19850. LinesToStr([ // $mod.$main
  19851. 'rtl.setIntfP($mod, "i", null);',
  19852. 'rtl.setIntfP($mod, "i", rtl.queryIntfT($mod.o, $mod.IUnknown), true);',
  19853. '']));
  19854. end;
  19855. procedure TTestModule.TestClassInterface_COM_AssignArg;
  19856. begin
  19857. StartProgram(false);
  19858. Add([
  19859. '{$interfaces com}',
  19860. 'type',
  19861. ' IUnknown = interface',
  19862. ' function _AddRef: longint;',
  19863. ' function _Release: longint;',
  19864. ' end;',
  19865. ' TObject = class(IUnknown)',
  19866. ' function _AddRef: longint; virtual; abstract;',
  19867. ' function _Release: longint; virtual; abstract;',
  19868. ' end;',
  19869. 'procedure DoDefault(i, j: IUnknown);',
  19870. 'begin',
  19871. ' i:=nil;',
  19872. ' i:=j;',
  19873. 'end;',
  19874. 'begin',
  19875. '']);
  19876. ConvertProgram;
  19877. CheckSource('TestClassInterface_COM_AssignArg',
  19878. LinesToStr([ // statements
  19879. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19880. 'rtl.createClass(this, "TObject", null, function () {',
  19881. ' this.$init = function () {',
  19882. ' };',
  19883. ' this.$final = function () {',
  19884. ' };',
  19885. ' rtl.addIntf(this, $mod.IUnknown);',
  19886. '});',
  19887. 'this.DoDefault = function (i, j) {',
  19888. ' rtl._AddRef(i);',
  19889. ' try {',
  19890. ' i = rtl.setIntfL(i, null);',
  19891. ' i = rtl.setIntfL(i, j);',
  19892. ' } finally {',
  19893. ' rtl._Release(i);',
  19894. ' };',
  19895. '};',
  19896. '']),
  19897. LinesToStr([ // $mod.$main
  19898. '']));
  19899. end;
  19900. procedure TTestModule.TestClassInterface_COM_FunctionResult;
  19901. begin
  19902. StartProgram(false);
  19903. Add([
  19904. '{$interfaces com}',
  19905. 'type',
  19906. ' IUnknown = interface',
  19907. ' function _AddRef: longint;',
  19908. ' function _Release: longint;',
  19909. ' end;',
  19910. ' TObject = class(IUnknown)',
  19911. ' function _AddRef: longint; virtual; abstract;',
  19912. ' function _Release: longint; virtual; abstract;',
  19913. ' end;',
  19914. 'function DoDefault(i: IUnknown): IUnknown;',
  19915. 'begin',
  19916. ' Result:=i;',
  19917. ' if Result<>nil then exit;',
  19918. 'end;',
  19919. 'begin',
  19920. '']);
  19921. ConvertProgram;
  19922. CheckSource('TestClassInterface_COM_FunctionResult',
  19923. LinesToStr([ // statements
  19924. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19925. 'rtl.createClass(this, "TObject", null, function () {',
  19926. ' this.$init = function () {',
  19927. ' };',
  19928. ' this.$final = function () {',
  19929. ' };',
  19930. ' rtl.addIntf(this, $mod.IUnknown);',
  19931. '});',
  19932. 'this.DoDefault = function (i) {',
  19933. ' var Result = null;',
  19934. ' var $ok = false;',
  19935. ' try {',
  19936. ' Result = rtl.setIntfL(Result, i);',
  19937. ' if(Result !== null){',
  19938. ' $ok = true;',
  19939. ' return Result;',
  19940. ' };',
  19941. ' $ok = true;',
  19942. ' } finally {',
  19943. ' if(!$ok) rtl._Release(Result);',
  19944. ' };',
  19945. ' return Result;',
  19946. '};',
  19947. '']),
  19948. LinesToStr([ // $mod.$main
  19949. '']));
  19950. end;
  19951. procedure TTestModule.TestClassInterface_COM_InheritedFuncResult;
  19952. begin
  19953. StartProgram(false);
  19954. Add([
  19955. '{$interfaces com}',
  19956. 'type',
  19957. ' IUnknown = interface',
  19958. ' function _AddRef: longint;',
  19959. ' function _Release: longint;',
  19960. ' end;',
  19961. ' TObject = class(IUnknown)',
  19962. ' function _AddRef: longint; virtual; abstract;',
  19963. ' function _Release: longint; virtual; abstract;',
  19964. ' function GetIntf: IUnknown; virtual;',
  19965. ' end;',
  19966. ' TMouse = class',
  19967. ' function GetIntf: IUnknown; override;',
  19968. ' end;',
  19969. 'function TObject.GetIntf: IUnknown; begin end;',
  19970. 'function TMouse.GetIntf: IUnknown;',
  19971. 'var i: IUnknown;',
  19972. 'begin',
  19973. ' inherited;',
  19974. ' inherited GetIntf;',
  19975. ' inherited GetIntf();',
  19976. ' Result:=inherited GetIntf;',
  19977. ' Result:=inherited GetIntf();',
  19978. ' i:=inherited GetIntf;',
  19979. ' i:=inherited GetIntf();',
  19980. 'end;',
  19981. 'begin',
  19982. '']);
  19983. ConvertProgram;
  19984. CheckSource('TestClassInterface_COM_InheritedFuncResult',
  19985. LinesToStr([ // statements
  19986. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19987. 'rtl.createClass(this, "TObject", null, function () {',
  19988. ' this.$init = function () {',
  19989. ' };',
  19990. ' this.$final = function () {',
  19991. ' };',
  19992. ' this.GetIntf = function () {',
  19993. ' var Result = null;',
  19994. ' return Result;',
  19995. ' };',
  19996. ' rtl.addIntf(this, $mod.IUnknown);',
  19997. '});',
  19998. 'rtl.createClass(this, "TMouse", this.TObject, function () {',
  19999. ' this.GetIntf = function () {',
  20000. ' var Result = null;',
  20001. ' var i = null;',
  20002. ' var $ir = rtl.createIntfRefs();',
  20003. ' var $ok = false;',
  20004. ' try {',
  20005. ' $ir.ref(1, $mod.TObject.GetIntf.call(this));',
  20006. ' $ir.ref(2, $mod.TObject.GetIntf.call(this));',
  20007. ' $ir.ref(3, $mod.TObject.GetIntf.call(this));',
  20008. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  20009. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  20010. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  20011. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  20012. ' $ok = true;',
  20013. ' } finally {',
  20014. ' $ir.free();',
  20015. ' rtl._Release(i);',
  20016. ' if (!$ok) rtl._Release(Result);',
  20017. ' };',
  20018. ' return Result;',
  20019. ' };',
  20020. ' rtl.addIntf(this, $mod.IUnknown);',
  20021. '});',
  20022. '']),
  20023. LinesToStr([ // $mod.$main
  20024. '']));
  20025. end;
  20026. procedure TTestModule.TestClassInterface_COM_IsAsTypeCasts;
  20027. begin
  20028. StartProgram(false);
  20029. Add([
  20030. '{$interfaces com}',
  20031. 'type',
  20032. ' IUnknown = interface',
  20033. ' function _AddRef: longint;',
  20034. ' function _Release: longint;',
  20035. ' end;',
  20036. ' TObject = class(IUnknown)',
  20037. ' function _AddRef: longint; virtual; abstract;',
  20038. ' function _Release: longint; virtual; abstract;',
  20039. ' end;',
  20040. 'procedure DoDefault(i, j: IUnknown; o: TObject);',
  20041. 'begin',
  20042. ' if i is IUnknown then ;',
  20043. ' if o is IUnknown then ;',
  20044. ' if i is TObject then ;',
  20045. ' i:=j as IUnknown;',
  20046. ' i:=o as IUnknown;',
  20047. ' o:=j as TObject;',
  20048. ' i:=IUnknown(j);',
  20049. ' i:=IUnknown(o);',
  20050. ' o:=TObject(i);',
  20051. 'end;',
  20052. 'begin',
  20053. '']);
  20054. ConvertProgram;
  20055. CheckSource('TestClassInterface_COM_IsAsTypeCasts',
  20056. LinesToStr([ // statements
  20057. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  20058. 'rtl.createClass(this, "TObject", null, function () {',
  20059. ' this.$init = function () {',
  20060. ' };',
  20061. ' this.$final = function () {',
  20062. ' };',
  20063. ' rtl.addIntf(this, $mod.IUnknown);',
  20064. '});',
  20065. 'this.DoDefault = function (i, j, o) {',
  20066. ' rtl._AddRef(i);',
  20067. ' try {',
  20068. ' if (rtl.intfIsIntfT(i, $mod.IUnknown)) ;',
  20069. ' if (rtl.queryIntfIsT(o, $mod.IUnknown)) ;',
  20070. ' if (rtl.intfIsClass(i, $mod.TObject)) ;',
  20071. ' i = rtl.setIntfL(i, rtl.intfAsIntfT(j, $mod.IUnknown));',
  20072. ' i = rtl.setIntfL(i, rtl.queryIntfT(o, $mod.IUnknown), true);',
  20073. ' o = rtl.intfAsClass(j, $mod.TObject);',
  20074. ' i = rtl.setIntfL(i, j);',
  20075. ' i = rtl.setIntfL(i, rtl.queryIntfT(o, $mod.IUnknown), true);',
  20076. ' o = rtl.intfToClass(i, $mod.TObject);',
  20077. ' } finally {',
  20078. ' rtl._Release(i);',
  20079. ' };',
  20080. '};',
  20081. '']),
  20082. LinesToStr([ // $mod.$main
  20083. '']));
  20084. end;
  20085. procedure TTestModule.TestClassInterface_COM_PassAsArg;
  20086. begin
  20087. StartProgram(false);
  20088. Add([
  20089. '{$interfaces com}',
  20090. 'type',
  20091. ' IUnknown = interface',
  20092. ' function _AddRef: longint;',
  20093. ' function _Release: longint;',
  20094. ' end;',
  20095. ' TObject = class(IUnknown)',
  20096. ' function _AddRef: longint; virtual; abstract;',
  20097. ' function _Release: longint; virtual; abstract;',
  20098. ' end;',
  20099. 'procedure DoIt(v: IUnknown; const j: IUnknown; var k: IUnknown; out l: IUnknown);',
  20100. 'var o: TObject;',
  20101. 'begin',
  20102. ' DoIt(v,v,v,v);',
  20103. ' DoIt(o,o,k,k);',
  20104. 'end;',
  20105. 'procedure DoSome;',
  20106. 'var v: IUnknown;',
  20107. 'begin',
  20108. ' DoIt(v,v,v,v);',
  20109. 'end;',
  20110. 'var i: IUnknown;',
  20111. 'begin',
  20112. ' DoIt(i,i,i,i);',
  20113. '']);
  20114. ConvertProgram;
  20115. CheckSource('TestClassInterface_COM_PassAsArg',
  20116. LinesToStr([ // statements
  20117. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  20118. 'rtl.createClass(this, "TObject", null, function () {',
  20119. ' this.$init = function () {',
  20120. ' };',
  20121. ' this.$final = function () {',
  20122. ' };',
  20123. ' rtl.addIntf(this, $mod.IUnknown);',
  20124. '});',
  20125. 'this.DoIt = function (v, j, k, l) {',
  20126. ' var o = null;',
  20127. ' var $ir = rtl.createIntfRefs();',
  20128. ' rtl._AddRef(v);',
  20129. ' try {',
  20130. ' $mod.DoIt(v, v, {',
  20131. ' get: function () {',
  20132. ' return v;',
  20133. ' },',
  20134. ' set: function (w) {',
  20135. ' v = rtl.setIntfL(v, w);',
  20136. ' }',
  20137. ' }, {',
  20138. ' get: function () {',
  20139. ' return v;',
  20140. ' },',
  20141. ' set: function (w) {',
  20142. ' v = rtl.setIntfL(v, w);',
  20143. ' }',
  20144. ' });',
  20145. ' $mod.DoIt($ir.ref(1, rtl.queryIntfT(o, $mod.IUnknown)), $ir.ref(2, rtl.queryIntfT(o, $mod.IUnknown)), k, k);',
  20146. ' } finally {',
  20147. ' $ir.free();',
  20148. ' rtl._Release(v);',
  20149. ' };',
  20150. '};',
  20151. 'this.DoSome = function () {',
  20152. ' var v = null;',
  20153. ' try {',
  20154. ' $mod.DoIt(v, v, {',
  20155. ' get: function () {',
  20156. ' return v;',
  20157. ' },',
  20158. ' set: function (w) {',
  20159. ' v = rtl.setIntfL(v, w);',
  20160. ' }',
  20161. ' }, {',
  20162. ' get: function () {',
  20163. ' return v;',
  20164. ' },',
  20165. ' set: function (w) {',
  20166. ' v = rtl.setIntfL(v, w);',
  20167. ' }',
  20168. ' });',
  20169. ' } finally {',
  20170. ' rtl._Release(v);',
  20171. ' };',
  20172. '};',
  20173. 'this.i = null;',
  20174. '']),
  20175. LinesToStr([ // $mod.$main
  20176. '$mod.DoIt($mod.i, $mod.i, {',
  20177. ' p: $mod,',
  20178. ' get: function () {',
  20179. ' return this.p.i;',
  20180. ' },',
  20181. ' set: function (v) {',
  20182. ' rtl.setIntfP(this.p, "i", v);',
  20183. ' }',
  20184. '}, {',
  20185. ' p: $mod,',
  20186. ' get: function () {',
  20187. ' return this.p.i;',
  20188. ' },',
  20189. ' set: function (v) {',
  20190. ' rtl.setIntfP(this.p, "i", v);',
  20191. ' }',
  20192. '});',
  20193. '']));
  20194. end;
  20195. procedure TTestModule.TestClassInterface_COM_PassToUntypedParam;
  20196. begin
  20197. StartProgram(false);
  20198. Add([
  20199. '{$interfaces com}',
  20200. 'type',
  20201. ' IUnknown = interface',
  20202. ' function _AddRef: longint;',
  20203. ' function _Release: longint;',
  20204. ' end;',
  20205. ' TObject = class(IUnknown)',
  20206. ' function _AddRef: longint; virtual; abstract;',
  20207. ' function _Release: longint; virtual; abstract;',
  20208. ' end;',
  20209. 'procedure DoIt(out i);',
  20210. 'begin end;',
  20211. 'procedure DoSome;',
  20212. 'var v: IUnknown;',
  20213. 'begin',
  20214. ' DoIt(v);',
  20215. 'end;',
  20216. 'function GetIt: IUnknown;',
  20217. 'begin',
  20218. ' DoIt(Result);',
  20219. 'end;',
  20220. 'var i: IUnknown;',
  20221. 'begin',
  20222. ' DoIt(i);',
  20223. '']);
  20224. ConvertProgram;
  20225. CheckSource('TestClassInterface_COM_PassToUntypedParam',
  20226. LinesToStr([ // statements
  20227. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  20228. 'rtl.createClass(this, "TObject", null, function () {',
  20229. ' this.$init = function () {',
  20230. ' };',
  20231. ' this.$final = function () {',
  20232. ' };',
  20233. ' rtl.addIntf(this, $mod.IUnknown);',
  20234. '});',
  20235. 'this.DoIt = function (i) {',
  20236. '};',
  20237. 'this.DoSome = function () {',
  20238. ' var v = null;',
  20239. ' try {',
  20240. ' $mod.DoIt({',
  20241. ' get: function () {',
  20242. ' return v;',
  20243. ' },',
  20244. ' set: function (w) {',
  20245. ' v = w;',
  20246. ' }',
  20247. ' });',
  20248. ' } finally {',
  20249. ' rtl._Release(v);',
  20250. ' };',
  20251. '};',
  20252. 'this.GetIt = function () {',
  20253. ' var Result = null;',
  20254. ' var $ok = false;',
  20255. ' try {',
  20256. ' $mod.DoIt({',
  20257. ' get: function () {',
  20258. ' return Result;',
  20259. ' },',
  20260. ' set: function (v) {',
  20261. ' Result = v;',
  20262. ' }',
  20263. ' });',
  20264. ' $ok = true;',
  20265. ' } finally {',
  20266. ' if (!$ok) rtl._Release(Result);',
  20267. ' };',
  20268. ' return Result;',
  20269. '};',
  20270. 'this.i = null;',
  20271. '']),
  20272. LinesToStr([ // $mod.$main
  20273. 'try {',
  20274. ' $mod.DoIt({',
  20275. ' p: $mod,',
  20276. ' get: function () {',
  20277. ' return this.p.i;',
  20278. ' },',
  20279. ' set: function (v) {',
  20280. ' this.p.i = v;',
  20281. ' }',
  20282. ' });',
  20283. '} finally {',
  20284. ' rtl._Release($mod.i);',
  20285. '};',
  20286. '']));
  20287. end;
  20288. procedure TTestModule.TestClassInterface_COM_FunctionInExpr;
  20289. begin
  20290. StartProgram(false);
  20291. Add([
  20292. '{$interfaces com}',
  20293. 'type',
  20294. ' IUnknown = interface',
  20295. ' function _AddRef: longint;',
  20296. ' function _Release: longint;',
  20297. ' end;',
  20298. ' TObject = class(IUnknown)',
  20299. ' function _AddRef: longint; virtual; abstract;',
  20300. ' function _Release: longint; virtual; abstract;',
  20301. ' end;',
  20302. 'function GetIt: IUnknown;',
  20303. 'begin',
  20304. 'end;',
  20305. 'procedure DoSome;',
  20306. 'var v: IUnknown;',
  20307. ' i: longint;',
  20308. 'begin',
  20309. ' v:=GetIt;',
  20310. ' v:=GetIt();',
  20311. ' GetIt()._AddRef;',
  20312. ' i:=GetIt()._AddRef;',
  20313. 'end;',
  20314. 'var v: IUnknown;',
  20315. ' i: longint;',
  20316. 'begin',
  20317. ' v:=GetIt;',
  20318. ' v:=GetIt();',
  20319. ' GetIt()._AddRef;',
  20320. ' i:=GetIt()._AddRef;',
  20321. '']);
  20322. ConvertProgram;
  20323. CheckSource('TestClassInterface_COM_FunctionInExpr',
  20324. LinesToStr([ // statements
  20325. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  20326. 'rtl.createClass(this, "TObject", null, function () {',
  20327. ' this.$init = function () {',
  20328. ' };',
  20329. ' this.$final = function () {',
  20330. ' };',
  20331. ' rtl.addIntf(this, $mod.IUnknown);',
  20332. '});',
  20333. 'this.GetIt = function () {',
  20334. ' var Result = null;',
  20335. ' return Result;',
  20336. '};',
  20337. 'this.DoSome = function () {',
  20338. ' var v = null;',
  20339. ' var i = 0;',
  20340. ' var $ir = rtl.createIntfRefs();',
  20341. ' try {',
  20342. ' v = rtl.setIntfL(v, $mod.GetIt(), true);',
  20343. ' v = rtl.setIntfL(v, $mod.GetIt(), true);',
  20344. ' $ir.ref(1, $mod.GetIt())._AddRef();',
  20345. ' i = $ir.ref(2, $mod.GetIt())._AddRef();',
  20346. ' } finally {',
  20347. ' $ir.free();',
  20348. ' rtl._Release(v);',
  20349. ' };',
  20350. '};',
  20351. 'this.v = null;',
  20352. 'this.i = 0;',
  20353. '']),
  20354. LinesToStr([ // $mod.$main
  20355. 'var $ir = rtl.createIntfRefs();',
  20356. 'try {',
  20357. ' rtl.setIntfP($mod, "v", $mod.GetIt(), true);',
  20358. ' rtl.setIntfP($mod, "v", $mod.GetIt(), true);',
  20359. ' $ir.ref(1, $mod.GetIt())._AddRef();',
  20360. ' $mod.i = $ir.ref(2, $mod.GetIt())._AddRef();',
  20361. '} finally {',
  20362. ' $ir.free();',
  20363. '};',
  20364. '']));
  20365. end;
  20366. procedure TTestModule.TestClassInterface_COM_Property;
  20367. begin
  20368. StartProgram(false);
  20369. Add([
  20370. '{$interfaces com}',
  20371. 'type',
  20372. ' IUnknown = interface',
  20373. ' function _AddRef: longint;',
  20374. ' function _Release: longint;',
  20375. ' end;',
  20376. ' TObject = class(IUnknown)',
  20377. ' FAnt: IUnknown;',
  20378. ' function _AddRef: longint; virtual; abstract;',
  20379. ' function _Release: longint; virtual; abstract;',
  20380. ' function GetBird: IUnknown; virtual; abstract;',
  20381. ' procedure SetBird(Value: IUnknown); virtual; abstract;',
  20382. ' function GetItems(Index: longint): IUnknown; virtual; abstract;',
  20383. ' procedure SetItems(Index: longint; Value: IUnknown); virtual; abstract;',
  20384. ' property Ant: IUnknown read FAnt write FAnt;',
  20385. ' property Bird: IUnknown read GetBird write SetBird;',
  20386. ' property Items[Index: longint]: IUnknown read GetItems write SetItems; default;',
  20387. ' end;',
  20388. 'procedure DoIt;',
  20389. 'var',
  20390. ' o: TObject;',
  20391. ' v: IUnknown;',
  20392. 'begin',
  20393. ' v:=o.Ant;',
  20394. ' o.Ant:=v;',
  20395. ' o.Ant:=o.Ant;',
  20396. ' v:=o.Bird;',
  20397. ' o.Bird:=v;',
  20398. ' o.Bird:=o.Bird;',
  20399. ' v:=o.Items[1];',
  20400. ' o.Items[2]:=v;',
  20401. ' o.Items[3]:=o.Items[4];',
  20402. ' v:=o[5];',
  20403. ' o[6]:=v;',
  20404. ' o[7]:=o[8];',
  20405. 'end;',
  20406. 'begin',
  20407. '']);
  20408. ConvertProgram;
  20409. CheckSource('TestClassInterface_COM_Property',
  20410. LinesToStr([ // statements
  20411. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  20412. 'rtl.createClass(this, "TObject", null, function () {',
  20413. ' this.$init = function () {',
  20414. ' this.FAnt = null;',
  20415. ' };',
  20416. ' this.$final = function () {',
  20417. ' rtl.setIntfP(this, "FAnt", null);',
  20418. ' };',
  20419. ' rtl.addIntf(this, $mod.IUnknown);',
  20420. '});',
  20421. 'this.DoIt = function () {',
  20422. ' var o = null;',
  20423. ' var v = null;',
  20424. ' var $ir = rtl.createIntfRefs();',
  20425. ' try {',
  20426. ' v = rtl.setIntfL(v, o.FAnt);',
  20427. ' rtl.setIntfP(o, "FAnt", v);',
  20428. ' rtl.setIntfP(o, "FAnt", o.FAnt);',
  20429. ' v = rtl.setIntfL(v, o.GetBird(), true);',
  20430. ' o.SetBird(v);',
  20431. ' o.SetBird($ir.ref(1, o.GetBird()));',
  20432. ' v = rtl.setIntfL(v, o.GetItems(1), true);',
  20433. ' o.SetItems(2, v);',
  20434. ' o.SetItems(3, $ir.ref(2, o.GetItems(4)));',
  20435. ' v = rtl.setIntfL(v, o.GetItems(5), true);',
  20436. ' o.SetItems(6, v);',
  20437. ' o.SetItems(7, $ir.ref(3, o.GetItems(8)));',
  20438. ' } finally {',
  20439. ' $ir.free();',
  20440. ' rtl._Release(v);',
  20441. ' };',
  20442. '};',
  20443. '']),
  20444. LinesToStr([ // $mod.$main
  20445. '']));
  20446. end;
  20447. procedure TTestModule.TestClassInterface_COM_IntfProperty;
  20448. begin
  20449. StartProgram(false);
  20450. Add([
  20451. '{$interfaces com}',
  20452. 'type',
  20453. ' IUnknown = interface',
  20454. ' function _AddRef: longint;',
  20455. ' function _Release: longint;',
  20456. ' function GetBird: IUnknown;',
  20457. ' procedure SetBird(Value: IUnknown);',
  20458. ' function GetItems(Index: longint): IUnknown;',
  20459. ' procedure SetItems(Index: longint; Value: IUnknown);',
  20460. ' property Bird: IUnknown read GetBird write SetBird;',
  20461. ' property Items[Index: longint]: IUnknown read GetItems write SetItems; default;',
  20462. ' end;',
  20463. ' TObject = class(IUnknown)',
  20464. ' function _AddRef: longint; virtual; abstract;',
  20465. ' function _Release: longint; virtual; abstract;',
  20466. ' function GetBird: IUnknown; virtual; abstract;',
  20467. ' procedure SetBird(Value: IUnknown); virtual; abstract;',
  20468. ' function GetItems(Index: longint): IUnknown; virtual; abstract;',
  20469. ' procedure SetItems(Index: longint; Value: IUnknown); virtual; abstract;',
  20470. ' end;',
  20471. 'procedure DoIt;',
  20472. 'var',
  20473. ' o: TObject;',
  20474. ' v: IUnknown;',
  20475. 'begin',
  20476. ' v:=v.Items[1];',
  20477. ' v.Items[2]:=v;',
  20478. ' v.Items[3]:=v.Items[4];',
  20479. ' v:=v[5];',
  20480. ' v[6]:=v;',
  20481. ' v[7]:=v[8];',
  20482. ' v[9].Bird.Bird:=v;',
  20483. ' v:=v.Bird[10].Bird',
  20484. 'end;',
  20485. 'begin',
  20486. '']);
  20487. ConvertProgram;
  20488. CheckSource('TestClassInterface_COM_IntfProperty',
  20489. LinesToStr([ // statements
  20490. 'rtl.createInterface(this, "IUnknown", "{385F5482-571B-338C-8130-4E97F330543B}", [',
  20491. ' "_AddRef",',
  20492. ' "_Release",',
  20493. ' "GetBird",',
  20494. ' "SetBird",',
  20495. ' "GetItems",',
  20496. ' "SetItems"',
  20497. '], null);',
  20498. 'rtl.createClass(this, "TObject", null, function () {',
  20499. ' this.$init = function () {',
  20500. ' };',
  20501. ' this.$final = function () {',
  20502. ' };',
  20503. ' rtl.addIntf(this, $mod.IUnknown);',
  20504. '});',
  20505. 'this.DoIt = function () {',
  20506. ' var o = null;',
  20507. ' var v = null;',
  20508. ' var $ir = rtl.createIntfRefs();',
  20509. ' try {',
  20510. ' v = rtl.setIntfL(v, v.GetItems(1), true);',
  20511. ' v.SetItems(2, v);',
  20512. ' v.SetItems(3, $ir.ref(1, v.GetItems(4)));',
  20513. ' v = rtl.setIntfL(v, v.GetItems(5), true);',
  20514. ' v.SetItems(6, v);',
  20515. ' v.SetItems(7, $ir.ref(2, v.GetItems(8)));',
  20516. ' $ir.ref(4, $ir.ref(3, v.GetItems(9)).GetBird()).SetBird(v);',
  20517. ' v = rtl.setIntfL(v, $ir.ref(6, $ir.ref(5, v.GetBird()).GetItems(10)).GetBird(), true);',
  20518. ' } finally {',
  20519. ' $ir.free();',
  20520. ' rtl._Release(v);',
  20521. ' };',
  20522. '};',
  20523. '']),
  20524. LinesToStr([ // $mod.$main
  20525. '']));
  20526. end;
  20527. procedure TTestModule.TestClassInterface_COM_Delegation;
  20528. begin
  20529. StartProgram(false);
  20530. Add([
  20531. '{$interfaces com}',
  20532. 'type',
  20533. ' IUnknown = interface',
  20534. ' function _AddRef: longint;',
  20535. ' function _Release: longint;',
  20536. ' end;',
  20537. ' IBird = interface(IUnknown)',
  20538. ' procedure Fly(s: string);',
  20539. ' end;',
  20540. ' IEagle = interface(IBird) end;',
  20541. ' IDove = interface(IBird) end;',
  20542. ' ISwallow = interface(IBird) end;',
  20543. ' TObject = class',
  20544. ' end;',
  20545. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  20546. ' function _AddRef: longint; virtual; abstract;',
  20547. ' function _Release: longint; virtual; abstract;',
  20548. ' procedure Fly(s: string); virtual; abstract;',
  20549. ' end;',
  20550. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  20551. ' function _AddRef: longint; virtual; abstract;',
  20552. ' function _Release: longint; virtual; abstract;',
  20553. ' FBirdIntf: IBird;',
  20554. ' property BirdIntf: IBird read FBirdIntf implements IBird;',
  20555. ' function GetEagleIntf: IEagle; virtual; abstract;',
  20556. ' property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  20557. ' FDoveObj: TBird;',
  20558. ' property DoveObj: TBird read FDoveObj implements IDove;',
  20559. ' function GetSwallowObj: TBird; virtual; abstract;',
  20560. ' property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  20561. ' end;',
  20562. 'begin',
  20563. '']);
  20564. ConvertProgram;
  20565. CheckSource('TestClassInterface_COM_Delegation',
  20566. LinesToStr([ // statements
  20567. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  20568. 'rtl.createInterface(this, "IBird", "{CC440C7F-7623-3DEE-AE88-000B86AAF108}", ["Fly"], this.IUnknown);',
  20569. 'rtl.createInterface(this, "IEagle", "{4B6A41C9-B020-3D7C-B688-96D19022B1B4}", [], this.IBird);',
  20570. 'rtl.createInterface(this, "IDove", "{4B6A41C9-B020-3D7C-B688-96D18EF16074}", [], this.IBird);',
  20571. 'rtl.createInterface(this, "ISwallow", "{BB6A41C9-B020-3D7C-B688-96D1CBDCB359}", [], this.IBird);',
  20572. 'rtl.createClass(this, "TObject", null, function () {',
  20573. ' this.$init = function () {',
  20574. ' };',
  20575. ' this.$final = function () {',
  20576. ' };',
  20577. '});',
  20578. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20579. ' rtl.addIntf(this, $mod.IBird);',
  20580. ' rtl.addIntf(this, $mod.IEagle);',
  20581. ' rtl.addIntf(this, $mod.IDove);',
  20582. ' rtl.addIntf(this, $mod.ISwallow);',
  20583. '});',
  20584. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  20585. ' this.$init = function () {',
  20586. ' $mod.TObject.$init.call(this);',
  20587. ' this.FBirdIntf = null;',
  20588. ' this.FDoveObj = null;',
  20589. ' };',
  20590. ' this.$final = function () {',
  20591. ' rtl.setIntfP(this, "FBirdIntf", null);',
  20592. ' this.FDoveObj = undefined;',
  20593. ' $mod.TObject.$final.call(this);',
  20594. ' };',
  20595. ' this.$intfmaps = {',
  20596. ' "{CC440C7F-7623-3DEE-AE88-000B86AAF108}": function () {',
  20597. ' return rtl._AddRef(this.FBirdIntf);',
  20598. ' },',
  20599. ' "{4B6A41C9-B020-3D7C-B688-96D19022B1B4}": function () {',
  20600. ' return this.GetEagleIntf();',
  20601. ' },',
  20602. ' "{4B6A41C9-B020-3D7C-B688-96D18EF16074}": function () {',
  20603. ' return rtl.queryIntfT(this.FDoveObj, $mod.IDove);',
  20604. ' },',
  20605. ' "{BB6A41C9-B020-3D7C-B688-96D1CBDCB359}": function () {',
  20606. ' return rtl.queryIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  20607. ' }',
  20608. ' };',
  20609. '});',
  20610. '']),
  20611. LinesToStr([ // $mod.$main
  20612. '']));
  20613. end;
  20614. procedure TTestModule.TestClassInterface_COM_With;
  20615. begin
  20616. StartProgram(false);
  20617. Add([
  20618. '{$interfaces com}',
  20619. 'type',
  20620. ' IUnknown = interface',
  20621. ' function _AddRef: longint;',
  20622. ' function _Release: longint;',
  20623. ' function GetAnt: IUnknown;',
  20624. ' property Ant: IUnknown read GetAnt;',
  20625. ' end;',
  20626. ' TObject = class(IUnknown)',
  20627. ' function _AddRef: longint; virtual; abstract;',
  20628. ' function _Release: longint; virtual; abstract;',
  20629. ' function GetAnt: IUnknown; virtual; abstract;',
  20630. ' property Ant: IUnknown read GetAnt;',
  20631. ' end;',
  20632. 'procedure DoIt;',
  20633. 'var',
  20634. ' i: IUnknown;',
  20635. 'begin',
  20636. ' with i do ',
  20637. ' GetAnt;',
  20638. ' with i.Ant, Ant do ',
  20639. ' GetAnt;',
  20640. 'end;',
  20641. 'begin',
  20642. '']);
  20643. ConvertProgram;
  20644. CheckSource('TestClassInterface_COM_With',
  20645. LinesToStr([ // statements
  20646. 'rtl.createInterface(this, "IUnknown", "{D7ADB00D-C6B6-39FB-BDDF-21CD521DDFA9}", ["_AddRef", "_Release", "GetAnt"], null);',
  20647. 'rtl.createClass(this, "TObject", null, function () {',
  20648. ' this.$init = function () {',
  20649. ' };',
  20650. ' this.$final = function () {',
  20651. ' };',
  20652. ' rtl.addIntf(this, $mod.IUnknown);',
  20653. '});',
  20654. 'this.DoIt = function () {',
  20655. ' var i = null;',
  20656. ' var $ir = rtl.createIntfRefs();',
  20657. ' try {',
  20658. ' $ir.ref(1, i.GetAnt());',
  20659. ' var $with = $ir.ref(2, i.GetAnt());',
  20660. ' var $with1 = $ir.ref(3, $with.GetAnt());',
  20661. ' $ir.ref(4, $with1.GetAnt());',
  20662. ' } finally {',
  20663. ' $ir.free();',
  20664. ' };',
  20665. '};',
  20666. '']),
  20667. LinesToStr([ // $mod.$main
  20668. '']));
  20669. end;
  20670. procedure TTestModule.TestClassInterface_COM_ForIn;
  20671. begin
  20672. StartProgram(false);
  20673. Add([
  20674. '{$interfaces com}',
  20675. 'type',
  20676. ' IUnknown = interface end;',
  20677. ' TObject = class',
  20678. ' Id: longint;',
  20679. ' end;',
  20680. ' IEnumerator = interface(IUnknown)',
  20681. ' function GetCurrent: TObject;',
  20682. ' function MoveNext: Boolean;',
  20683. ' property Current: TObject read GetCurrent;',
  20684. ' end;',
  20685. ' IEnumerable = interface(IUnknown)',
  20686. ' function GetEnumerator: IEnumerator;',
  20687. ' end;',
  20688. 'var',
  20689. ' o: TObject;',
  20690. ' i: IEnumerable;',
  20691. 'begin',
  20692. ' for o in i do o.Id:=3;',
  20693. '']);
  20694. ConvertProgram;
  20695. CheckSource('TestClassInterface_COM_ForIn',
  20696. LinesToStr([ // statements
  20697. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  20698. 'rtl.createClass(this, "TObject", null, function () {',
  20699. ' this.$init = function () {',
  20700. ' this.Id = 0;',
  20701. ' };',
  20702. ' this.$final = function () {',
  20703. ' };',
  20704. '});',
  20705. 'rtl.createInterface(this, "IEnumerator", "{95D7745D-ED61-3F13-BBE4-07708161999E}", ["GetCurrent", "MoveNext"], this.IUnknown);',
  20706. 'rtl.createInterface(this, "IEnumerable", "{8CC9D45D-ED7D-3B73-96B6-290B931BB19E}", ["GetEnumerator"], this.IUnknown);',
  20707. 'this.o = null;',
  20708. 'this.i = null;',
  20709. '']),
  20710. LinesToStr([ // $mod.$main
  20711. 'var $in = $mod.i.GetEnumerator();',
  20712. 'try {',
  20713. ' while ($in.MoveNext()) {',
  20714. ' $mod.o = $in.GetCurrent();',
  20715. ' $mod.o.Id = 3;',
  20716. ' }',
  20717. '} finally {',
  20718. ' rtl._Release($in)',
  20719. '};',
  20720. '']));
  20721. end;
  20722. procedure TTestModule.TestClassInterface_COM_ArrayOfIntfFail;
  20723. begin
  20724. StartProgram(false);
  20725. Add([
  20726. '{$interfaces com}',
  20727. 'type',
  20728. ' IUnknown = interface',
  20729. ' function _AddRef: longint;',
  20730. ' function _Release: longint;',
  20731. ' end;',
  20732. ' TObject = class',
  20733. ' end;',
  20734. ' TArrOfIntf = array of IUnknown;',
  20735. 'begin',
  20736. '']);
  20737. SetExpectedPasResolverError('Not supported: array of COM-interface',nNotSupportedX);
  20738. ConvertProgram;
  20739. end;
  20740. procedure TTestModule.TestClassInterface_COM_RecordIntfFail;
  20741. begin
  20742. StartProgram(false);
  20743. Add([
  20744. '{$interfaces com}',
  20745. 'type',
  20746. ' IUnknown = interface',
  20747. ' function _AddRef: longint;',
  20748. ' function _Release: longint;',
  20749. ' end;',
  20750. ' TRec = record',
  20751. ' i: IUnknown;',
  20752. ' end;',
  20753. 'begin',
  20754. '']);
  20755. SetExpectedPasResolverError('Not supported: COM-interface as record member',nNotSupportedX);
  20756. ConvertProgram;
  20757. end;
  20758. procedure TTestModule.TestClassInterface_COM_UnitInitialization;
  20759. begin
  20760. StartUnit(false);
  20761. Add([
  20762. '{$interfaces com}',
  20763. 'interface',
  20764. 'implementation',
  20765. 'type',
  20766. ' IUnknown = interface',
  20767. ' function _AddRef: longint;',
  20768. ' end;',
  20769. ' TObject = class(IUnknown)',
  20770. ' function _AddRef: longint;',
  20771. ' end;',
  20772. 'function TObject._AddRef: longint; begin end;',
  20773. 'var i: IUnknown;',
  20774. ' o: TObject;',
  20775. 'initialization',
  20776. ' i:=nil;',
  20777. ' i:=i;',
  20778. ' i:=o;',
  20779. ' if (o as IUnknown)=nil then ;',
  20780. '']);
  20781. ConvertUnit;
  20782. CheckSource('TestClassInterface_COM_UnitInitialization',
  20783. LinesToStr([ // statements
  20784. 'var $impl = $mod.$impl;',
  20785. '']),
  20786. LinesToStr([ // this.$init
  20787. 'var $ir = rtl.createIntfRefs();',
  20788. 'try {',
  20789. ' rtl.setIntfP($impl, "i", null);',
  20790. ' rtl.setIntfP($impl, "i", $impl.i);',
  20791. ' rtl.setIntfP($impl, "i", rtl.queryIntfT($impl.o, $impl.IUnknown), true);',
  20792. ' if ($ir.ref(1, rtl.queryIntfT($impl.o, $impl.IUnknown)) === null) ;',
  20793. '} finally {',
  20794. ' $ir.free();',
  20795. '};',
  20796. '']),
  20797. LinesToStr([ // implementation
  20798. 'rtl.createInterface($impl, "IUnknown", "{B92D5841-758A-322B-BDDF-21CD52180000}", ["_AddRef"], null);',
  20799. 'rtl.createClass($impl, "TObject", null, function () {',
  20800. ' this.$init = function () {',
  20801. ' };',
  20802. ' this.$final = function () {',
  20803. ' };',
  20804. ' this._AddRef = function () {',
  20805. ' var Result = 0;',
  20806. ' return Result;',
  20807. ' };',
  20808. ' rtl.addIntf(this, $impl.IUnknown);',
  20809. '});',
  20810. '$impl.i = null;',
  20811. '$impl.o = null;',
  20812. ''])
  20813. );
  20814. end;
  20815. procedure TTestModule.TestClassInterface_GUID;
  20816. begin
  20817. StartProgram(false);
  20818. Add([
  20819. '{$interfaces corba}',
  20820. 'type',
  20821. ' IUnknown = interface',
  20822. ' [''{f31db68f-3010-D355-4EBA-CDD4EF4A737C}'']',
  20823. ' end;',
  20824. ' TObject = class end;',
  20825. ' TGUID = record D1, D2, D3, D4: word; end;',
  20826. ' TAliasGUID = TGUID;',
  20827. ' TGUIDString = type string;',
  20828. ' TAliasGUIDString = TGUIDString;',
  20829. 'procedure DoConstGUIDIt(const g: TAliasGUID); overload;',
  20830. 'begin end;',
  20831. 'procedure DoDefGUID(g: TAliasGUID); overload;',
  20832. 'begin end;',
  20833. 'procedure DoStr(const s: TAliasGUIDString); overload;',
  20834. 'begin end;',
  20835. 'var',
  20836. ' i: IUnknown;',
  20837. ' g: TAliasGUID = ''{d91c9af4-3C93-420F-A303-BF5BA82BFD23}'';',
  20838. ' s: TAliasGUIDString;',
  20839. 'begin',
  20840. ' DoConstGUIDIt(IUnknown);',
  20841. ' DoDefGUID(IUnknown);',
  20842. ' DoStr(IUnknown);',
  20843. ' DoConstGUIDIt(i);',
  20844. ' DoDefGUID(i);',
  20845. ' DoStr(i);',
  20846. ' DoConstGUIDIt(''{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}'');',
  20847. ' DoDefGUID(''{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}'');',
  20848. ' DoStr(g);',
  20849. ' g:=i;',
  20850. ' g:=IUnknown;',
  20851. ' g:=''{D91C9AF4-3C93-420F-A303-bf5ba82bfd23}'';',
  20852. ' s:=i;',
  20853. ' s:=IUnknown;',
  20854. ' s:=g;',
  20855. ' if g=i then ;',
  20856. ' if i=g then ;',
  20857. ' if g=IUnknown then ;',
  20858. ' if IUnknown=g then ;',
  20859. ' if s=i then ;',
  20860. ' if i=s then ;',
  20861. ' if s=IUnknown then ;',
  20862. ' if IUnknown=s then ;',
  20863. ' if s=g then ;',
  20864. ' if g=s then ;',
  20865. '']);
  20866. ConvertProgram;
  20867. CheckSource('TestClassInterface_GUID',
  20868. LinesToStr([ // statements
  20869. 'rtl.createInterface(this, "IUnknown", "{F31DB68F-3010-D355-4EBA-CDD4EF4A737C}", [], null);',
  20870. 'rtl.createClass(this, "TObject", null, function () {',
  20871. ' this.$init = function () {',
  20872. ' };',
  20873. ' this.$final = function () {',
  20874. ' };',
  20875. '});',
  20876. 'rtl.recNewT(this, "TGUID", function () {',
  20877. ' this.D1 = 0;',
  20878. ' this.D2 = 0;',
  20879. ' this.D3 = 0;',
  20880. ' this.D4 = 0;',
  20881. ' this.$eq = function (b) {',
  20882. ' return (this.D1 === b.D1) && (this.D2 === b.D2) && (this.D3 === b.D3) && (this.D4 === b.D4);',
  20883. ' };',
  20884. ' this.$assign = function (s) {',
  20885. ' this.D1 = s.D1;',
  20886. ' this.D2 = s.D2;',
  20887. ' this.D3 = s.D3;',
  20888. ' this.D4 = s.D4;',
  20889. ' return this;',
  20890. ' };',
  20891. '});',
  20892. 'this.DoConstGUIDIt = function (g) {',
  20893. '};',
  20894. 'this.DoDefGUID = function (g) {',
  20895. '};',
  20896. 'this.DoStr = function (s) {',
  20897. '};',
  20898. 'this.i = null;',
  20899. 'this.g = this.TGUID.$clone({',
  20900. ' D1: 0xD91C9AF4,',
  20901. ' D2: 0x3C93,',
  20902. ' D3: 0x420F,',
  20903. ' D4: [',
  20904. ' 0xA3,',
  20905. ' 0x03,',
  20906. ' 0xBF,',
  20907. ' 0x5B,',
  20908. ' 0xA8,',
  20909. ' 0x2B,',
  20910. ' 0xFD,',
  20911. ' 0x23',
  20912. ' ]',
  20913. '});',
  20914. 'this.s = "";',
  20915. '']),
  20916. LinesToStr([ // $mod.$main
  20917. '$mod.DoConstGUIDIt(rtl.getIntfGUIDR($mod.IUnknown));',
  20918. '$mod.DoDefGUID($mod.TGUID.$clone(rtl.getIntfGUIDR($mod.IUnknown)));',
  20919. '$mod.DoStr($mod.IUnknown.$guid);',
  20920. '$mod.DoConstGUIDIt(rtl.getIntfGUIDR($mod.i));',
  20921. '$mod.DoDefGUID($mod.TGUID.$clone(rtl.getIntfGUIDR($mod.i)));',
  20922. '$mod.DoStr($mod.i.$guid);',
  20923. '$mod.DoConstGUIDIt(rtl.strToGUIDR("{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}"));',
  20924. '$mod.DoDefGUID(rtl.strToGUIDR("{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}"));',
  20925. '$mod.DoStr(rtl.guidrToStr($mod.g));',
  20926. '$mod.g.$assign(rtl.getIntfGUIDR($mod.i));',
  20927. '$mod.g.$assign(rtl.getIntfGUIDR($mod.IUnknown));',
  20928. '$mod.g.$assign({',
  20929. ' D1: 0xD91C9AF4,',
  20930. ' D2: 0x3C93,',
  20931. ' D3: 0x420F,',
  20932. ' D4: [',
  20933. ' 0xA3,',
  20934. ' 0x03,',
  20935. ' 0xBF,',
  20936. ' 0x5B,',
  20937. ' 0xA8,',
  20938. ' 0x2B,',
  20939. ' 0xFD,',
  20940. ' 0x23',
  20941. ' ]',
  20942. '});',
  20943. '$mod.s = $mod.i.$guid;',
  20944. '$mod.s = $mod.IUnknown.$guid;',
  20945. '$mod.s = rtl.guidrToStr($mod.g);',
  20946. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.i))) ;',
  20947. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.i))) ;',
  20948. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.IUnknown))) ;',
  20949. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.IUnknown))) ;',
  20950. 'if ($mod.s === $mod.i.$guid) ;',
  20951. 'if ($mod.i.$guid === $mod.s) ;',
  20952. 'if ($mod.s === $mod.IUnknown.$guid) ;',
  20953. 'if ($mod.IUnknown.$guid === $mod.s) ;',
  20954. 'if ($mod.g.$eq(rtl.createTGUID($mod.s))) ;',
  20955. 'if ($mod.g.$eq(rtl.createTGUID($mod.s))) ;',
  20956. '']));
  20957. end;
  20958. procedure TTestModule.TestClassInterface_GUIDProperty;
  20959. begin
  20960. StartProgram(false);
  20961. Add([
  20962. '{$interfaces corba}',
  20963. 'type',
  20964. ' IUnknown = interface',
  20965. ' [''{f31db68f-3010-D355-4EBA-CDD4EF4A737C}'']',
  20966. ' end;',
  20967. ' TGUID = record D1, D2, D3, D4: word; end;',
  20968. ' TAliasGUID = TGUID;',
  20969. ' TGUIDString = type string;',
  20970. ' TAliasGUIDString = TGUIDString;',
  20971. ' TObject = class',
  20972. ' function GetG: TAliasGUID; virtual; abstract;',
  20973. ' procedure SetG(const Value: TAliasGUID); virtual; abstract;',
  20974. ' function GetS: TAliasGUIDString; virtual; abstract;',
  20975. ' procedure SetS(const Value: TAliasGUIDString); virtual; abstract;',
  20976. ' property g: TAliasGUID read GetG write SetG;',
  20977. ' property s: TAliasGUIDString read GetS write SetS;',
  20978. ' end;',
  20979. 'var o: TObject;',
  20980. 'begin',
  20981. ' o.g:=IUnknown;',
  20982. ' o.g:=''{D91C9AF4-3C93-420F-A303-bf5ba82bfd23}'';',
  20983. ' o.s:=IUnknown;',
  20984. ' o.s:=o.g;',
  20985. '']);
  20986. ConvertProgram;
  20987. CheckSource('TestClassInterface_GUIDProperty',
  20988. LinesToStr([ // statements
  20989. 'rtl.createInterface(this, "IUnknown", "{F31DB68F-3010-D355-4EBA-CDD4EF4A737C}", [], null);',
  20990. 'rtl.recNewT(this, "TGUID", function () {',
  20991. ' this.D1 = 0;',
  20992. ' this.D2 = 0;',
  20993. ' this.D3 = 0;',
  20994. ' this.D4 = 0;',
  20995. ' this.$eq = function (b) {',
  20996. ' return (this.D1 === b.D1) && (this.D2 === b.D2) && (this.D3 === b.D3) && (this.D4 === b.D4);',
  20997. ' };',
  20998. ' this.$assign = function (s) {',
  20999. ' this.D1 = s.D1;',
  21000. ' this.D2 = s.D2;',
  21001. ' this.D3 = s.D3;',
  21002. ' this.D4 = s.D4;',
  21003. ' return this;',
  21004. ' };',
  21005. '});',
  21006. 'rtl.createClass(this, "TObject", null, function () {',
  21007. ' this.$init = function () {',
  21008. ' };',
  21009. ' this.$final = function () {',
  21010. ' };',
  21011. '});',
  21012. 'this.o = null;',
  21013. '']),
  21014. LinesToStr([ // $mod.$main
  21015. '$mod.o.SetG(rtl.getIntfGUIDR($mod.IUnknown));',
  21016. '$mod.o.SetG({',
  21017. ' D1: 0xD91C9AF4,',
  21018. ' D2: 0x3C93,',
  21019. ' D3: 0x420F,',
  21020. ' D4: [',
  21021. ' 0xA3,',
  21022. ' 0x03,',
  21023. ' 0xBF,',
  21024. ' 0x5B,',
  21025. ' 0xA8,',
  21026. ' 0x2B,',
  21027. ' 0xFD,',
  21028. ' 0x23',
  21029. ' ]',
  21030. '});',
  21031. '$mod.o.SetS($mod.IUnknown.$guid);',
  21032. '$mod.o.SetS(rtl.guidrToStr($mod.o.GetG()));',
  21033. '']));
  21034. end;
  21035. procedure TTestModule.TestClassHelper_ClassVar;
  21036. begin
  21037. StartProgram(false);
  21038. Add([
  21039. 'type',
  21040. ' TObject = class',
  21041. ' end;',
  21042. ' THelper = class helper for TObject',
  21043. ' const',
  21044. ' One = 1;',
  21045. ' Two: word = 2;',
  21046. ' class var',
  21047. ' Glob: word;',
  21048. ' function Foo(w: word): word;',
  21049. ' class function Bar(w: word): word;',
  21050. ' end;',
  21051. 'function THelper.foo(w: word): word;',
  21052. 'begin',
  21053. ' Result:=w;',
  21054. ' Two:=One+w;',
  21055. ' Glob:=Glob;',
  21056. ' Result:=Self.Glob;',
  21057. ' Self.Glob:=Self.Glob;',
  21058. ' with Self do Glob:=Glob;',
  21059. 'end;',
  21060. 'class function THelper.bar(w: word): word;',
  21061. 'begin',
  21062. ' Result:=w;',
  21063. ' Two:=One;',
  21064. ' Glob:=Glob;',
  21065. ' Self.Glob:=Self.Glob;',
  21066. ' with Self do Glob:=Glob;',
  21067. 'end;',
  21068. 'var o: TObject;',
  21069. 'begin',
  21070. ' tobject.two:=tobject.one;',
  21071. ' tobject.Glob:=tobject.Glob;',
  21072. ' with tobject do begin',
  21073. ' two:=one;',
  21074. ' Glob:=Glob;',
  21075. ' end;',
  21076. ' o.two:=o.one;',
  21077. ' o.Glob:=o.Glob;',
  21078. ' with o do begin',
  21079. ' two:=one;',
  21080. ' Glob:=Glob;',
  21081. ' end;',
  21082. '']);
  21083. ConvertProgram;
  21084. CheckSource('TestClassHelper_ClassVar',
  21085. LinesToStr([ // statements
  21086. 'rtl.createClass(this, "TObject", null, function () {',
  21087. ' this.$init = function () {',
  21088. ' };',
  21089. ' this.$final = function () {',
  21090. ' };',
  21091. '});',
  21092. 'rtl.createHelper(this, "THelper", null, function () {',
  21093. ' this.One = 1;',
  21094. ' this.Two = 2;',
  21095. ' this.Glob = 0;',
  21096. ' this.Foo = function (w) {',
  21097. ' var Result = 0;',
  21098. ' Result = w;',
  21099. ' $mod.THelper.Two = 1 + w;',
  21100. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  21101. ' Result = $mod.THelper.Glob;',
  21102. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  21103. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  21104. ' return Result;',
  21105. ' };',
  21106. ' this.Bar = function (w) {',
  21107. ' var Result = 0;',
  21108. ' Result = w;',
  21109. ' $mod.THelper.Two = 1;',
  21110. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  21111. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  21112. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  21113. ' return Result;',
  21114. ' };',
  21115. '});',
  21116. 'this.o = null;',
  21117. '']),
  21118. LinesToStr([ // $mod.$main
  21119. '$mod.THelper.Two = 1;',
  21120. '$mod.THelper.Glob = $mod.THelper.Glob;',
  21121. 'var $with = $mod.TObject;',
  21122. '$mod.THelper.Two = 1;',
  21123. '$mod.THelper.Glob = $mod.THelper.Glob;',
  21124. '$mod.THelper.Two = 1;',
  21125. '$mod.THelper.Glob = $mod.THelper.Glob;',
  21126. 'var $with1 = $mod.o;',
  21127. '$mod.THelper.Two = 1;',
  21128. '$mod.THelper.Glob = $mod.THelper.Glob;',
  21129. '']));
  21130. end;
  21131. procedure TTestModule.TestClassHelper_Method_AccessInstanceFields;
  21132. begin
  21133. StartProgram(false);
  21134. Add([
  21135. 'type',
  21136. ' TObject = class',
  21137. ' FSize: word;',
  21138. ' property Size: word read FSize write FSize;',
  21139. ' end;',
  21140. ' THelper = class helper for TObject',
  21141. ' function Foo(w: word = 1): word;',
  21142. ' end;',
  21143. 'function THelper.foo(w: word): word;',
  21144. 'begin',
  21145. ' Result:=Size;',
  21146. ' Size:=Size+2;',
  21147. ' Self.Size:=Self.Size+3;',
  21148. ' FSize:=FSize+4;',
  21149. ' Self.FSize:=Self.FSize+5;',
  21150. ' with Self do begin',
  21151. ' Size:=Size+6;',
  21152. ' FSize:=FSize+7;',
  21153. ' FSize:=FSize+8;',
  21154. ' end;',
  21155. 'end;',
  21156. 'begin',
  21157. '']);
  21158. ConvertProgram;
  21159. CheckSource('TestClassHelper_Method_AccessInstanceFields',
  21160. LinesToStr([ // statements
  21161. 'rtl.createClass(this, "TObject", null, function () {',
  21162. ' this.$init = function () {',
  21163. ' this.FSize = 0;',
  21164. ' };',
  21165. ' this.$final = function () {',
  21166. ' };',
  21167. '});',
  21168. 'rtl.createHelper(this, "THelper", null, function () {',
  21169. ' this.Foo = function (w) {',
  21170. ' var Result = 0;',
  21171. ' Result = this.FSize;',
  21172. ' this.FSize = this.FSize + 2;',
  21173. ' this.FSize = this.FSize + 3;',
  21174. ' this.FSize = this.FSize + 4;',
  21175. ' this.FSize = this.FSize + 5;',
  21176. ' this.FSize = this.FSize + 6;',
  21177. ' this.FSize = this.FSize + 7;',
  21178. ' this.FSize = this.FSize + 8;',
  21179. ' return Result;',
  21180. ' };',
  21181. '});',
  21182. '']),
  21183. LinesToStr([ // $mod.$main
  21184. '']));
  21185. end;
  21186. procedure TTestModule.TestClassHelper_Method_Call;
  21187. begin
  21188. StartProgram(false);
  21189. Add([
  21190. 'type',
  21191. ' TObject = class',
  21192. ' procedure Run(w: word = 10);',
  21193. ' end;',
  21194. ' THelper = class helper for TObject',
  21195. ' function Foo(w: word = 1): word;',
  21196. ' end;',
  21197. 'procedure TObject.Run(w: word);',
  21198. 'var o: TObject;',
  21199. 'begin',
  21200. ' Foo;',
  21201. ' Foo();',
  21202. ' Foo(2);',
  21203. ' Self.Foo;',
  21204. ' Self.Foo();',
  21205. ' Self.Foo(3);',
  21206. ' with Self do begin',
  21207. ' Foo;',
  21208. ' Foo();',
  21209. ' Foo(4);',
  21210. ' end;',
  21211. ' with o do Foo(5);',
  21212. 'end;',
  21213. 'function THelper.foo(w: word): word;',
  21214. 'begin',
  21215. ' Run;',
  21216. ' Run();',
  21217. ' Run(11);',
  21218. ' Foo;',
  21219. ' Foo();',
  21220. ' Foo(12);',
  21221. ' Self.Foo;',
  21222. ' Self.Foo();',
  21223. ' Self.Foo(13);',
  21224. ' with Self do begin',
  21225. ' Foo;',
  21226. ' Foo();',
  21227. ' Foo(14);',
  21228. ' end;',
  21229. 'end;',
  21230. 'var Obj: TObject;',
  21231. 'begin',
  21232. ' obj.Foo;',
  21233. ' obj.Foo();',
  21234. ' obj.Foo(21);',
  21235. ' with obj do begin',
  21236. ' Foo;',
  21237. ' Foo();',
  21238. ' Foo(22);',
  21239. ' end;',
  21240. '']);
  21241. ConvertProgram;
  21242. CheckSource('TestClassHelper_Method_Call',
  21243. LinesToStr([ // statements
  21244. 'rtl.createClass(this, "TObject", null, function () {',
  21245. ' this.$init = function () {',
  21246. ' };',
  21247. ' this.$final = function () {',
  21248. ' };',
  21249. ' this.Run = function (w) {',
  21250. ' var o = null;',
  21251. ' $mod.THelper.Foo.call(this, 1);',
  21252. ' $mod.THelper.Foo.call(this, 1);',
  21253. ' $mod.THelper.Foo.call(this, 2);',
  21254. ' $mod.THelper.Foo.call(this, 1);',
  21255. ' $mod.THelper.Foo.call(this, 1);',
  21256. ' $mod.THelper.Foo.call(this, 3);',
  21257. ' $mod.THelper.Foo.call(this, 1);',
  21258. ' $mod.THelper.Foo.call(this, 1);',
  21259. ' $mod.THelper.Foo.call(this, 4);',
  21260. ' $mod.THelper.Foo.call(o, 5);',
  21261. ' };',
  21262. '});',
  21263. 'rtl.createHelper(this, "THelper", null, function () {',
  21264. ' this.Foo = function (w) {',
  21265. ' var Result = 0;',
  21266. ' this.Run(10);',
  21267. ' this.Run(10);',
  21268. ' this.Run(11);',
  21269. ' $mod.THelper.Foo.call(this, 1);',
  21270. ' $mod.THelper.Foo.call(this, 1);',
  21271. ' $mod.THelper.Foo.call(this, 12);',
  21272. ' $mod.THelper.Foo.call(this, 1);',
  21273. ' $mod.THelper.Foo.call(this, 1);',
  21274. ' $mod.THelper.Foo.call(this, 13);',
  21275. ' $mod.THelper.Foo.call(this, 1);',
  21276. ' $mod.THelper.Foo.call(this, 1);',
  21277. ' $mod.THelper.Foo.call(this, 14);',
  21278. ' return Result;',
  21279. ' };',
  21280. '});',
  21281. 'this.Obj = null;',
  21282. '']),
  21283. LinesToStr([ // $mod.$main
  21284. '$mod.THelper.Foo.call($mod.Obj, 1);',
  21285. '$mod.THelper.Foo.call($mod.Obj, 1);',
  21286. '$mod.THelper.Foo.call($mod.Obj, 21);',
  21287. 'var $with = $mod.Obj;',
  21288. '$mod.THelper.Foo.call($with, 1);',
  21289. '$mod.THelper.Foo.call($with, 1);',
  21290. '$mod.THelper.Foo.call($with, 22);',
  21291. '']));
  21292. end;
  21293. procedure TTestModule.TestClassHelper_Method_Nested_Call;
  21294. begin
  21295. StartProgram(false);
  21296. Add([
  21297. 'type',
  21298. ' TObject = class',
  21299. ' procedure Run(w: word = 10);',
  21300. ' end;',
  21301. ' THelper = class helper for TObject',
  21302. ' function Foo(w: word = 1): word;',
  21303. ' end;',
  21304. 'procedure TObject.Run(w: word);',
  21305. ' procedure Sub(Self: TObject);',
  21306. ' begin',
  21307. ' Foo;',
  21308. ' Foo();',
  21309. ' Self.Foo;',
  21310. ' Self.Foo();',
  21311. ' with Self do begin',
  21312. ' Foo;',
  21313. ' Foo();',
  21314. ' end;',
  21315. ' end;',
  21316. 'begin',
  21317. 'end;',
  21318. 'function THelper.foo(w: word): word;',
  21319. ' procedure Sub(Self: TObject);',
  21320. ' begin',
  21321. ' Run;',
  21322. ' Run();',
  21323. ' Foo;',
  21324. ' Foo();',
  21325. ' Self.Foo;',
  21326. ' Self.Foo();',
  21327. ' with Self do begin',
  21328. ' Foo;',
  21329. ' Foo();',
  21330. ' end;',
  21331. ' end;',
  21332. 'begin',
  21333. 'end;',
  21334. 'begin',
  21335. '']);
  21336. ConvertProgram;
  21337. CheckSource('TestClassHelper_Method_Nested_Call',
  21338. LinesToStr([ // statements
  21339. 'rtl.createClass(this, "TObject", null, function () {',
  21340. ' this.$init = function () {',
  21341. ' };',
  21342. ' this.$final = function () {',
  21343. ' };',
  21344. ' this.Run = function (w) {',
  21345. ' var $Self = this;',
  21346. ' function Sub(Self) {',
  21347. ' $mod.THelper.Foo.call($Self, 1);',
  21348. ' $mod.THelper.Foo.call($Self, 1);',
  21349. ' $mod.THelper.Foo.call(Self, 1);',
  21350. ' $mod.THelper.Foo.call(Self, 1);',
  21351. ' $mod.THelper.Foo.call(Self, 1);',
  21352. ' $mod.THelper.Foo.call(Self, 1);',
  21353. ' };',
  21354. ' };',
  21355. '});',
  21356. 'rtl.createHelper(this, "THelper", null, function () {',
  21357. ' this.Foo = function (w) {',
  21358. ' var $Self = this;',
  21359. ' var Result = 0;',
  21360. ' function Sub(Self) {',
  21361. ' $Self.Run(10);',
  21362. ' $Self.Run(10);',
  21363. ' $mod.THelper.Foo.call($Self, 1);',
  21364. ' $mod.THelper.Foo.call($Self, 1);',
  21365. ' $mod.THelper.Foo.call(Self, 1);',
  21366. ' $mod.THelper.Foo.call(Self, 1);',
  21367. ' $mod.THelper.Foo.call(Self, 1);',
  21368. ' $mod.THelper.Foo.call(Self, 1);',
  21369. ' };',
  21370. ' return Result;',
  21371. ' };',
  21372. '});',
  21373. '']),
  21374. LinesToStr([ // $mod.$main
  21375. '']));
  21376. end;
  21377. procedure TTestModule.TestClassHelper_ClassMethod_Call;
  21378. begin
  21379. StartProgram(false);
  21380. Add([
  21381. 'type',
  21382. ' TObject = class',
  21383. ' class procedure Run(w: word = 10);',
  21384. ' end;',
  21385. ' THelper = class helper for TObject',
  21386. ' class function Foo(w: word = 1): word;',
  21387. ' end;',
  21388. 'class procedure TObject.Run(w: word);',
  21389. 'begin',
  21390. ' Foo;',
  21391. ' Foo();',
  21392. ' Self.Foo;',
  21393. ' Self.Foo();',
  21394. ' with Self do begin',
  21395. ' Foo;',
  21396. ' Foo();',
  21397. ' end;',
  21398. 'end;',
  21399. 'class function THelper.foo(w: word): word;',
  21400. 'begin',
  21401. ' Run;',
  21402. ' Run();',
  21403. ' Foo;',
  21404. ' Foo();',
  21405. ' Self.Foo;',
  21406. ' Self.Foo();',
  21407. ' with Self do begin',
  21408. ' Foo;',
  21409. ' Foo();',
  21410. ' end;',
  21411. 'end;',
  21412. 'var',
  21413. ' Obj: TObject;',
  21414. 'begin',
  21415. ' obj.Foo;',
  21416. ' obj.Foo();',
  21417. ' with obj do begin',
  21418. ' Foo;',
  21419. ' Foo();',
  21420. ' end;',
  21421. ' tobject.Foo;',
  21422. ' tobject.Foo();',
  21423. ' with tobject do begin',
  21424. ' Foo;',
  21425. ' Foo();',
  21426. ' end;',
  21427. '']);
  21428. ConvertProgram;
  21429. CheckSource('TestClassHelper_ClassMethod_Call',
  21430. LinesToStr([ // statements
  21431. 'rtl.createClass(this, "TObject", null, function () {',
  21432. ' this.$init = function () {',
  21433. ' };',
  21434. ' this.$final = function () {',
  21435. ' };',
  21436. ' this.Run = function (w) {',
  21437. ' $mod.THelper.Foo.call(this, 1);',
  21438. ' $mod.THelper.Foo.call(this, 1);',
  21439. ' $mod.THelper.Foo.call(this, 1);',
  21440. ' $mod.THelper.Foo.call(this, 1);',
  21441. ' $mod.THelper.Foo.call(this, 1);',
  21442. ' $mod.THelper.Foo.call(this, 1);',
  21443. ' };',
  21444. '});',
  21445. 'rtl.createHelper(this, "THelper", null, function () {',
  21446. ' this.Foo = function (w) {',
  21447. ' var Result = 0;',
  21448. ' this.Run(10);',
  21449. ' this.Run(10);',
  21450. ' $mod.THelper.Foo.call(this, 1);',
  21451. ' $mod.THelper.Foo.call(this, 1);',
  21452. ' $mod.THelper.Foo.call(this, 1);',
  21453. ' $mod.THelper.Foo.call(this, 1);',
  21454. ' $mod.THelper.Foo.call(this, 1);',
  21455. ' $mod.THelper.Foo.call(this, 1);',
  21456. ' return Result;',
  21457. ' };',
  21458. '});',
  21459. 'this.Obj = null;',
  21460. '']),
  21461. LinesToStr([ // $mod.$main
  21462. '$mod.THelper.Foo.call($mod.Obj.$class, 1);',
  21463. '$mod.THelper.Foo.call($mod.Obj.$class, 1);',
  21464. 'var $with = $mod.Obj;',
  21465. '$mod.THelper.Foo.call($with.$class, 1);',
  21466. '$mod.THelper.Foo.call($with.$class, 1);',
  21467. '$mod.THelper.Foo.call($mod.TObject, 1);',
  21468. '$mod.THelper.Foo.call($mod.TObject, 1);',
  21469. 'var $with1 = $mod.TObject;',
  21470. '$mod.THelper.Foo.call($mod.TObject, 1);',
  21471. '$mod.THelper.Foo.call($mod.TObject, 1);',
  21472. '']));
  21473. end;
  21474. procedure TTestModule.TestClassHelper_ClassOf;
  21475. begin
  21476. StartProgram(false);
  21477. Add([
  21478. 'type',
  21479. ' TObject = class',
  21480. ' end;',
  21481. ' TClass = class of TObject;',
  21482. ' THelper = class helper for TObject',
  21483. ' class function Foo(w: word = 1): word;',
  21484. ' end;',
  21485. 'class function THelper.foo(w: word): word;',
  21486. 'begin',
  21487. 'end;',
  21488. 'var',
  21489. ' c: TClass;',
  21490. 'begin',
  21491. ' c.Foo;',
  21492. ' c.Foo();',
  21493. ' with c do begin',
  21494. ' Foo;',
  21495. ' Foo();',
  21496. ' end;',
  21497. '']);
  21498. ConvertProgram;
  21499. CheckSource('TestClassHelper_ClassOf',
  21500. LinesToStr([ // statements
  21501. 'rtl.createClass(this, "TObject", null, function () {',
  21502. ' this.$init = function () {',
  21503. ' };',
  21504. ' this.$final = function () {',
  21505. ' };',
  21506. '});',
  21507. 'rtl.createHelper(this, "THelper", null, function () {',
  21508. ' this.Foo = function (w) {',
  21509. ' var Result = 0;',
  21510. ' return Result;',
  21511. ' };',
  21512. '});',
  21513. 'this.c = null;',
  21514. '']),
  21515. LinesToStr([ // $mod.$main
  21516. '$mod.THelper.Foo.call($mod.c, 1);',
  21517. '$mod.THelper.Foo.call($mod.c, 1);',
  21518. 'var $with = $mod.c;',
  21519. '$mod.THelper.Foo.call($with, 1);',
  21520. '$mod.THelper.Foo.call($with, 1);',
  21521. '']));
  21522. end;
  21523. procedure TTestModule.TestClassHelper_MethodRefObjFPC;
  21524. begin
  21525. StartProgram(false);
  21526. Add([
  21527. '{$mode objfpc}',
  21528. 'type',
  21529. ' TObject = class',
  21530. ' procedure DoIt;',
  21531. ' end;',
  21532. ' THelper = class helper for TObject',
  21533. ' procedure Fly(w: word = 1);',
  21534. ' class procedure Glide(w: word = 1);',
  21535. ' class procedure Run(w: word = 1); static;',
  21536. ' end;',
  21537. ' TFly = procedure(w: word) of object;',
  21538. ' TGlide = TFly;',
  21539. ' TRun = procedure(w: word);',
  21540. 'var',
  21541. ' f: TFly;',
  21542. ' g: TGlide;',
  21543. ' r: TRun;',
  21544. 'procedure TObject.DoIt;',
  21545. 'begin',
  21546. ' f:=@fly;',
  21547. ' g:=@glide;',
  21548. ' r:=@run;',
  21549. ' f:[email protected];',
  21550. ' g:[email protected];',
  21551. ' r:[email protected];',
  21552. ' with self do begin',
  21553. ' f:=@fly;',
  21554. ' g:=@glide;',
  21555. ' r:=@run;',
  21556. ' end;',
  21557. 'end;',
  21558. 'procedure THelper.fly(w: word);',
  21559. 'begin',
  21560. ' f:=@fly;',
  21561. ' g:=@glide;',
  21562. ' r:=@run;',
  21563. 'end;',
  21564. 'class procedure THelper.glide(w: word);',
  21565. 'begin',
  21566. ' g:=@glide;',
  21567. ' r:=@run;',
  21568. 'end;',
  21569. 'class procedure THelper.run(w: word);',
  21570. 'begin',
  21571. ' g:=@glide;',
  21572. ' r:=@run;',
  21573. 'end;',
  21574. 'var',
  21575. ' Obj: TObject;',
  21576. 'begin',
  21577. ' f:[email protected];',
  21578. ' g:[email protected];',
  21579. ' r:[email protected];',
  21580. ' with obj do begin',
  21581. ' f:=@fly;',
  21582. ' g:=@glide;',
  21583. ' r:=@run;',
  21584. ' end;',
  21585. ' g:[email protected];',
  21586. ' r:[email protected];',
  21587. ' with tobject do begin',
  21588. ' g:=@glide;',
  21589. ' r:=@run;',
  21590. ' end;',
  21591. '']);
  21592. ConvertProgram;
  21593. CheckSource('TestClassHelper_MethodRefObjFPC',
  21594. LinesToStr([ // statements
  21595. 'rtl.createClass(this, "TObject", null, function () {',
  21596. ' this.$init = function () {',
  21597. ' };',
  21598. ' this.$final = function () {',
  21599. ' };',
  21600. ' this.DoIt = function () {',
  21601. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  21602. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  21603. ' $mod.r = $mod.THelper.Run;',
  21604. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  21605. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  21606. ' $mod.r = $mod.THelper.Run;',
  21607. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  21608. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  21609. ' $mod.r = $mod.THelper.Run;',
  21610. ' };',
  21611. '});',
  21612. 'rtl.createHelper(this, "THelper", null, function () {',
  21613. ' this.Fly = function (w) {',
  21614. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  21615. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  21616. ' $mod.r = $mod.THelper.Run;',
  21617. ' };',
  21618. ' this.Glide = function (w) {',
  21619. ' $mod.g = rtl.createCallback(this, $mod.THelper.Glide);',
  21620. ' $mod.r = $mod.THelper.Run;',
  21621. ' };',
  21622. ' this.Run = function (w) {',
  21623. ' $mod.g = rtl.createCallback($mod.THelper, $mod.THelper.Glide);',
  21624. ' $mod.r = $mod.THelper.Run;',
  21625. ' };',
  21626. '});',
  21627. 'this.f = null;',
  21628. 'this.g = null;',
  21629. 'this.r = null;',
  21630. 'this.Obj = null;',
  21631. '']),
  21632. LinesToStr([ // $mod.$main
  21633. '$mod.f = rtl.createCallback($mod.Obj, $mod.THelper.Fly);',
  21634. '$mod.g = rtl.createCallback($mod.Obj.$class, $mod.THelper.Glide);',
  21635. '$mod.r = $mod.THelper.Run;',
  21636. 'var $with = $mod.Obj;',
  21637. '$mod.f = rtl.createCallback($with, $mod.THelper.Fly);',
  21638. '$mod.g = rtl.createCallback($with.$class, $mod.THelper.Glide);',
  21639. '$mod.r = $mod.THelper.Run;',
  21640. '$mod.g = rtl.createCallback($mod.TObject, $mod.THelper.Glide);',
  21641. '$mod.r = $mod.THelper.Run;',
  21642. 'var $with1 = $mod.TObject;',
  21643. '$mod.g = rtl.createCallback($with1, $mod.THelper.Glide);',
  21644. '$mod.r = $mod.THelper.Run;',
  21645. '']));
  21646. end;
  21647. procedure TTestModule.TestClassHelper_Constructor;
  21648. begin
  21649. StartProgram(false);
  21650. Add([
  21651. 'type',
  21652. ' TObject = class',
  21653. ' constructor Create;',
  21654. ' end;',
  21655. ' TClass = class of TObject;',
  21656. ' THelper = class helper for TObject',
  21657. ' constructor NewHlp(w: word);',
  21658. ' end;',
  21659. 'var',
  21660. ' obj: TObject;',
  21661. ' c: TClass;',
  21662. 'constructor TObject.Create;',
  21663. 'begin',
  21664. ' NewHlp(2);', // normal call
  21665. ' tobject.NewHlp(3);', // new instance
  21666. ' c.newhlp(4);', // new instance
  21667. 'end;',
  21668. 'constructor THelper.NewHlp(w: word);',
  21669. 'begin',
  21670. ' create;', // normal call
  21671. ' tobject.create;', // new instance
  21672. ' NewHlp(2);', // normal call
  21673. ' tobject.NewHlp(3);', // new instance
  21674. ' c.newhlp(4);', // new instance
  21675. 'end;',
  21676. 'begin',
  21677. ' obj.newhlp(2);', // normal call
  21678. ' with Obj do newhlp(12);', // normal call
  21679. ' tobject.newhlp(3);', // new instance
  21680. ' with tobject do newhlp(13);', // new instance
  21681. ' c.newhlp(4);', // new instance
  21682. ' with c do newhlp(14);', // new instance
  21683. '']);
  21684. ConvertProgram;
  21685. CheckSource('TestClassHelper_Constructor',
  21686. LinesToStr([ // statements
  21687. 'rtl.createClass(this, "TObject", null, function () {',
  21688. ' this.$init = function () {',
  21689. ' };',
  21690. ' this.$final = function () {',
  21691. ' };',
  21692. ' this.Create = function () {',
  21693. ' $mod.THelper.NewHlp.call(this, 2);',
  21694. ' $mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  21695. ' $mod.c.$create($mod.THelper.NewHlp, [4]);',
  21696. ' return this;',
  21697. ' };',
  21698. '});',
  21699. 'rtl.createHelper(this, "THelper", null, function () {',
  21700. ' this.NewHlp = function (w) {',
  21701. ' this.Create();',
  21702. ' $mod.TObject.$create("Create");',
  21703. ' $mod.THelper.NewHlp.call(this, 2);',
  21704. ' $mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  21705. ' $mod.c.$create($mod.THelper.NewHlp, [4]);',
  21706. ' return this;',
  21707. ' };',
  21708. '});',
  21709. 'this.obj = null;',
  21710. 'this.c = null;',
  21711. '']),
  21712. LinesToStr([ // $mod.$main
  21713. '$mod.THelper.NewHlp.call($mod.obj, 2);',
  21714. 'var $with = $mod.obj;',
  21715. '$mod.THelper.NewHlp.call($with, 12);',
  21716. '$mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  21717. 'var $with1 = $mod.TObject;',
  21718. '$with1.$create($mod.THelper.NewHlp, [13]);',
  21719. '$mod.c.$create($mod.THelper.NewHlp, [4]);',
  21720. 'var $with2 = $mod.c;',
  21721. '$with2.$create($mod.THelper.NewHlp, [14]);',
  21722. '']));
  21723. end;
  21724. procedure TTestModule.TestClassHelper_InheritedObjFPC;
  21725. begin
  21726. StartProgram(false);
  21727. Add([
  21728. 'type',
  21729. ' TObject = class',
  21730. ' procedure Fly;',
  21731. ' end;',
  21732. ' TObjHelper = class helper for TObject',
  21733. ' procedure Fly;',
  21734. ' end;',
  21735. ' TBird = class',
  21736. ' procedure Fly;',
  21737. ' end;',
  21738. ' TBirdHelper = class helper for TBird',
  21739. ' procedure Fly;',
  21740. ' procedure Walk(w: word);',
  21741. ' end;',
  21742. ' TEagleHelper = class helper(TBirdHelper) for TBird',
  21743. ' procedure Fly;',
  21744. ' procedure Walk(w: word);',
  21745. ' end;',
  21746. 'procedure Tobject.fly;',
  21747. 'begin',
  21748. ' inherited;', // ignore
  21749. 'end;',
  21750. 'procedure Tobjhelper.fly;',
  21751. 'begin',
  21752. ' {@TObject_Fly}inherited;',
  21753. ' inherited {@TObject_Fly}Fly;',
  21754. 'end;',
  21755. 'procedure Tbird.fly;',
  21756. 'begin',
  21757. ' {@TObjHelper_Fly}inherited;',
  21758. ' inherited {@TObjHelper_Fly}Fly;',
  21759. 'end;',
  21760. 'procedure Tbirdhelper.fly;',
  21761. 'begin',
  21762. ' {@TBird_Fly}inherited;',
  21763. ' inherited {@TBird_Fly}Fly;',
  21764. 'end;',
  21765. 'procedure Tbirdhelper.walk(w: word);',
  21766. 'begin',
  21767. 'end;',
  21768. 'procedure teagleHelper.fly;',
  21769. 'begin',
  21770. ' {@TBird_Fly}inherited;',
  21771. ' inherited {@TBird_Fly}Fly;',
  21772. 'end;',
  21773. 'procedure teagleHelper.walk(w: word);',
  21774. 'begin',
  21775. ' {@TBirdHelper_Walk}inherited;',
  21776. ' inherited {@TBirdHelper_Walk}Walk(3);',
  21777. 'end;',
  21778. 'begin',
  21779. '']);
  21780. ConvertProgram;
  21781. CheckSource('TestClassHelper_InheritedObjFPC',
  21782. LinesToStr([ // statements
  21783. 'rtl.createClass(this, "TObject", null, function () {',
  21784. ' this.$init = function () {',
  21785. ' };',
  21786. ' this.$final = function () {',
  21787. ' };',
  21788. ' this.Fly = function () {',
  21789. ' };',
  21790. '});',
  21791. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21792. ' this.Fly = function () {',
  21793. ' $mod.TObject.Fly.call(this);',
  21794. ' $mod.TObject.Fly.call(this);',
  21795. ' };',
  21796. '});',
  21797. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21798. ' this.Fly$1 = function () {',
  21799. ' $mod.TObjHelper.Fly.call(this);',
  21800. ' $mod.TObjHelper.Fly.call(this);',
  21801. ' };',
  21802. '});',
  21803. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  21804. ' this.Fly = function () {',
  21805. ' $mod.TBird.Fly$1.call(this);',
  21806. ' $mod.TBird.Fly$1.call(this);',
  21807. ' };',
  21808. ' this.Walk = function (w) {',
  21809. ' };',
  21810. '});',
  21811. 'rtl.createHelper(this, "TEagleHelper", this.TBirdHelper, function () {',
  21812. ' this.Fly$1 = function () {',
  21813. ' $mod.TBird.Fly$1.call(this);',
  21814. ' $mod.TBird.Fly$1.call(this);',
  21815. ' };',
  21816. ' this.Walk$1 = function (w) {',
  21817. ' $mod.TBirdHelper.Walk.apply(this, arguments);',
  21818. ' $mod.TBirdHelper.Walk.call(this, 3);',
  21819. ' };',
  21820. '});',
  21821. '']),
  21822. LinesToStr([ // $mod.$main
  21823. '']));
  21824. end;
  21825. procedure TTestModule.TestClassHelper_Property;
  21826. begin
  21827. StartProgram(false);
  21828. Add([
  21829. 'type',
  21830. ' TObject = class',
  21831. ' FSize: word;',
  21832. ' function GetSpeed: word;',
  21833. ' procedure SetSpeed(Value: word);',
  21834. ' end;',
  21835. ' TObjHelper = class helper for TObject',
  21836. ' function GetLeft: word;',
  21837. ' procedure SetLeft(Value: word);',
  21838. ' property Size: word read FSize write FSize;',
  21839. ' property Speed: word read GetSpeed write SetSpeed;',
  21840. ' property Left: word read GetLeft write SetLeft;',
  21841. ' end;',
  21842. ' TBird = class',
  21843. ' property NotRight: word read GetLeft write SetLeft;',
  21844. ' procedure DoIt;',
  21845. ' end;',
  21846. 'var',
  21847. ' b: TBird;',
  21848. 'function Tobject.GetSpeed: word;',
  21849. 'begin',
  21850. ' Size:=Size+11;',
  21851. ' Speed:=Speed+12;',
  21852. ' Result:=Left+13;',
  21853. ' Left:=13;',
  21854. ' Left:=Left+13;',
  21855. ' Self.Size:=Self.Size+21;',
  21856. ' Self.Speed:=Self.Speed+22;',
  21857. ' Self.Left:=Self.Left+23;',
  21858. ' with Self do begin',
  21859. ' Size:=Size+31;',
  21860. ' Speed:=Speed+32;',
  21861. ' Left:=Left+33;',
  21862. ' end;',
  21863. 'end;',
  21864. 'procedure Tobject.SetSpeed(Value: word);',
  21865. 'begin',
  21866. 'end;',
  21867. 'function TObjHelper.GetLeft: word;',
  21868. 'begin',
  21869. ' Size:=Size+11;',
  21870. ' Speed:=Speed+12;',
  21871. ' Left:=Left+13;',
  21872. ' Self.Size:=Self.Size+21;',
  21873. ' Self.Speed:=Self.Speed+22;',
  21874. ' Self.Left:=Self.Left+23;',
  21875. ' with Self do begin',
  21876. ' Size:=Size+31;',
  21877. ' Speed:=Speed+32;',
  21878. ' Left:=Left+33;',
  21879. ' end;',
  21880. 'end;',
  21881. 'procedure TObjHelper.SetLeft(Value: word);',
  21882. 'begin',
  21883. 'end;',
  21884. 'procedure TBird.DoIt;',
  21885. 'begin',
  21886. ' NotRight:=NotRight+11;',
  21887. ' Self.NotRight:=Self.NotRight+21;',
  21888. ' with Self do begin',
  21889. ' NotRight:=NotRight+31;',
  21890. ' end;',
  21891. 'end;',
  21892. 'begin',
  21893. ' b.Size:=b.Size+11;',
  21894. ' b.Speed:=b.Speed+12;',
  21895. ' b.Left:=b.Left+13;',
  21896. ' b.NotRight:=b.NotRight+14;',
  21897. ' with b do begin',
  21898. ' Size:=Size+31;',
  21899. ' Speed:=Speed+32;',
  21900. ' Left:=Left+33;',
  21901. ' NotRight:=NotRight+34;',
  21902. ' end;',
  21903. '']);
  21904. ConvertProgram;
  21905. CheckSource('TestClassHelper_Property',
  21906. LinesToStr([ // statements
  21907. 'rtl.createClass(this, "TObject", null, function () {',
  21908. ' this.$init = function () {',
  21909. ' this.FSize = 0;',
  21910. ' };',
  21911. ' this.$final = function () {',
  21912. ' };',
  21913. ' this.GetSpeed = function () {',
  21914. ' var Result = 0;',
  21915. ' this.FSize = this.FSize + 11;',
  21916. ' this.SetSpeed(this.GetSpeed() + 12);',
  21917. ' Result = $mod.TObjHelper.GetLeft.call(this) + 13;',
  21918. ' $mod.TObjHelper.SetLeft.call(this, 13);',
  21919. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  21920. ' this.FSize = this.FSize + 21;',
  21921. ' this.SetSpeed(this.GetSpeed() + 22);',
  21922. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  21923. ' this.FSize = this.FSize + 31;',
  21924. ' this.SetSpeed(this.GetSpeed() + 32);',
  21925. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  21926. ' return Result;',
  21927. ' };',
  21928. ' this.SetSpeed = function (Value) {',
  21929. ' };',
  21930. '});',
  21931. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21932. ' this.GetLeft = function () {',
  21933. ' var Result = 0;',
  21934. ' this.FSize = this.FSize + 11;',
  21935. ' this.SetSpeed(this.GetSpeed() + 12);',
  21936. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  21937. ' this.FSize = this.FSize + 21;',
  21938. ' this.SetSpeed(this.GetSpeed() + 22);',
  21939. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  21940. ' this.FSize = this.FSize + 31;',
  21941. ' this.SetSpeed(this.GetSpeed() + 32);',
  21942. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  21943. ' return Result;',
  21944. ' };',
  21945. ' this.SetLeft = function (Value) {',
  21946. ' };',
  21947. '});',
  21948. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21949. ' this.DoIt = function () {',
  21950. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 11);',
  21951. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 21);',
  21952. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 31);',
  21953. ' };',
  21954. '});',
  21955. 'this.b = null;',
  21956. '']),
  21957. LinesToStr([ // $mod.$main
  21958. '$mod.b.FSize = $mod.b.FSize + 11;',
  21959. '$mod.b.SetSpeed($mod.b.GetSpeed() + 12);',
  21960. '$mod.TObjHelper.SetLeft.call($mod.b, $mod.TObjHelper.GetLeft.call($mod.b) + 13);',
  21961. '$mod.TObjHelper.SetLeft.call($mod.b, $mod.TObjHelper.GetLeft.call($mod.b) + 14);',
  21962. 'var $with = $mod.b;',
  21963. '$with.FSize = $with.FSize + 31;',
  21964. '$with.SetSpeed($with.GetSpeed() + 32);',
  21965. '$mod.TObjHelper.SetLeft.call($with, $mod.TObjHelper.GetLeft.call($with) + 33);',
  21966. '$mod.TObjHelper.SetLeft.call($with, $mod.TObjHelper.GetLeft.call($with) + 34);',
  21967. '']));
  21968. end;
  21969. procedure TTestModule.TestClassHelper_Property_Array;
  21970. begin
  21971. StartProgram(false);
  21972. Add([
  21973. 'type',
  21974. ' TObject = class',
  21975. ' function GetSpeed(Index: boolean): word;',
  21976. ' procedure SetSpeed(Index: boolean; Value: word);',
  21977. ' end;',
  21978. ' TObjHelper = class helper for TObject',
  21979. ' function GetSize(Index: boolean): word;',
  21980. ' procedure SetSize(Index: boolean; Value: word);',
  21981. ' property Size[Index: boolean]: word read GetSize write SetSize;',
  21982. ' property Speed[Index: boolean]: word read GetSpeed write SetSpeed;',
  21983. ' end;',
  21984. ' TBird = class',
  21985. ' property Items[Index: boolean]: word read GetSize write SetSize;',
  21986. ' procedure DoIt;',
  21987. ' end;',
  21988. 'var',
  21989. ' b: TBird;',
  21990. 'function Tobject.GetSpeed(Index: boolean): word;',
  21991. 'begin',
  21992. ' Result:=Size[false];',
  21993. ' Size[true]:=Size[false]+11;',
  21994. ' Speed[true]:=Speed[false]+12;',
  21995. ' Self.Size[true]:=Self.Size[false]+21;',
  21996. ' Self.Speed[true]:=Self.Speed[false]+22;',
  21997. ' with Self do begin',
  21998. ' Size[true]:=Size[false]+31;',
  21999. ' Speed[true]:=Speed[false]+32;',
  22000. ' end;',
  22001. 'end;',
  22002. 'procedure Tobject.SetSpeed(Index: boolean; Value: word);',
  22003. 'begin',
  22004. 'end;',
  22005. 'function TObjHelper.GetSize(Index: boolean): word;',
  22006. 'begin',
  22007. ' Size[true]:=Size[false]+11;',
  22008. ' Speed[true]:=Speed[false]+12;',
  22009. ' Self.Size[true]:=Self.Size[false]+21;',
  22010. ' Self.Speed[true]:=Self.Speed[false]+22;',
  22011. ' with Self do begin',
  22012. ' Size[true]:=Size[false]+31;',
  22013. ' Speed[true]:=Speed[false]+32;',
  22014. ' end;',
  22015. 'end;',
  22016. 'procedure TObjHelper.SetSize(Index: boolean; Value: word);',
  22017. 'begin',
  22018. 'end;',
  22019. 'procedure TBird.DoIt;',
  22020. 'begin',
  22021. ' Items[true]:=Items[false]+11;',
  22022. ' Self.Items[true]:=Self.Items[false]+21;',
  22023. ' with Self do Items[true]:=Items[false]+31;',
  22024. 'end;',
  22025. 'begin',
  22026. ' b.Size[true]:=b.Size[false]+11;',
  22027. ' b.Speed[true]:=b.Speed[false]+12;',
  22028. ' b.Items[true]:=b.Items[false]+13;',
  22029. ' with b do begin',
  22030. ' Size[true]:=Size[false]+21;',
  22031. ' Speed[true]:=Speed[false]+22;',
  22032. ' Items[true]:=Items[false]+23;',
  22033. ' end;',
  22034. '']);
  22035. ConvertProgram;
  22036. CheckSource('TestClassHelper_Property_Array',
  22037. LinesToStr([ // statements
  22038. 'rtl.createClass(this, "TObject", null, function () {',
  22039. ' this.$init = function () {',
  22040. ' };',
  22041. ' this.$final = function () {',
  22042. ' };',
  22043. ' this.GetSpeed = function (Index) {',
  22044. ' var Result = 0;',
  22045. ' Result = $mod.TObjHelper.GetSize.call(this, false);',
  22046. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  22047. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  22048. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  22049. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  22050. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  22051. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  22052. ' return Result;',
  22053. ' };',
  22054. ' this.SetSpeed = function (Index, Value) {',
  22055. ' };',
  22056. '});',
  22057. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  22058. ' this.GetSize = function (Index) {',
  22059. ' var Result = 0;',
  22060. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  22061. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  22062. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  22063. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  22064. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  22065. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  22066. ' return Result;',
  22067. ' };',
  22068. ' this.SetSize = function (Index, Value) {',
  22069. ' };',
  22070. '});',
  22071. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  22072. ' this.DoIt = function () {',
  22073. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  22074. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  22075. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  22076. ' };',
  22077. '});',
  22078. 'this.b = null;',
  22079. '']),
  22080. LinesToStr([ // $mod.$main
  22081. '$mod.TObjHelper.SetSize.call($mod.b, true, $mod.TObjHelper.GetSize.call($mod.b, false) + 11);',
  22082. '$mod.b.SetSpeed(true, $mod.b.GetSpeed(false) + 12);',
  22083. '$mod.TObjHelper.SetSize.call($mod.b, true, $mod.TObjHelper.GetSize.call($mod.b, false) + 13);',
  22084. 'var $with = $mod.b;',
  22085. '$mod.TObjHelper.SetSize.call($with, true, $mod.TObjHelper.GetSize.call($with, false) + 21);',
  22086. '$with.SetSpeed(true, $with.GetSpeed(false) + 22);',
  22087. '$mod.TObjHelper.SetSize.call($with, true, $mod.TObjHelper.GetSize.call($with, false) + 23);',
  22088. '']));
  22089. end;
  22090. procedure TTestModule.TestClassHelper_Property_Array_Default;
  22091. begin
  22092. StartProgram(false);
  22093. Add([
  22094. 'type',
  22095. ' TObject = class',
  22096. ' function GetSpeed(Index: boolean): word;',
  22097. ' procedure SetSpeed(Index: boolean; Value: word);',
  22098. ' end;',
  22099. ' TObjHelper = class helper for TObject',
  22100. ' property Speed[Index: boolean]: word read GetSpeed write SetSpeed; default;',
  22101. ' end;',
  22102. ' TBird = class',
  22103. ' end;',
  22104. ' TBirdHelper = class helper for TBird',
  22105. ' function GetSize(Index: word): boolean;',
  22106. ' procedure SetSize(Index: word; Value: boolean);',
  22107. ' property Size[Index: word]: boolean read GetSize write SetSize; default;',
  22108. ' end;',
  22109. 'function Tobject.GetSpeed(Index: boolean): word;',
  22110. 'begin',
  22111. ' Self[true]:=Self[false]+1;',
  22112. 'end;',
  22113. 'procedure Tobject.SetSpeed(Index: boolean; Value: word);',
  22114. 'begin',
  22115. 'end;',
  22116. 'function TBirdHelper.GetSize(Index: word): boolean;',
  22117. 'begin',
  22118. ' Self[1]:=not Self[2];',
  22119. 'end;',
  22120. 'procedure TBirdHelper.SetSize(Index: word; Value: boolean);',
  22121. 'begin',
  22122. 'end;',
  22123. 'var',
  22124. ' o: TObject;',
  22125. ' b: TBird;',
  22126. 'begin',
  22127. ' o[true]:=o[false]+1;',
  22128. ' b[3]:=not b[4];',
  22129. '']);
  22130. ConvertProgram;
  22131. CheckSource('TestClassHelper_Property_Array_Default',
  22132. LinesToStr([ // statements
  22133. 'rtl.createClass(this, "TObject", null, function () {',
  22134. ' this.$init = function () {',
  22135. ' };',
  22136. ' this.$final = function () {',
  22137. ' };',
  22138. ' this.GetSpeed = function (Index) {',
  22139. ' var Result = 0;',
  22140. ' this.SetSpeed(true, this.GetSpeed(false) + 1);',
  22141. ' return Result;',
  22142. ' };',
  22143. ' this.SetSpeed = function (Index, Value) {',
  22144. ' };',
  22145. '});',
  22146. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  22147. '});',
  22148. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  22149. '});',
  22150. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  22151. ' this.GetSize = function (Index) {',
  22152. ' var Result = false;',
  22153. ' $mod.TBirdHelper.SetSize.call(this, 1, !$mod.TBirdHelper.GetSize.call(this, 2));',
  22154. ' return Result;',
  22155. ' };',
  22156. ' this.SetSize = function (Index, Value) {',
  22157. ' };',
  22158. '});',
  22159. 'this.o = null;',
  22160. 'this.b = null;',
  22161. '']),
  22162. LinesToStr([ // $mod.$main
  22163. '$mod.o.SetSpeed(true, $mod.o.GetSpeed(false) + 1);',
  22164. '$mod.TBirdHelper.SetSize.call($mod.b, 3, !$mod.TBirdHelper.GetSize.call($mod.b, 4));',
  22165. '']));
  22166. end;
  22167. procedure TTestModule.TestClassHelper_Property_Array_DefaultDefault;
  22168. begin
  22169. StartProgram(false);
  22170. Add([
  22171. 'type',
  22172. ' TObject = class',
  22173. ' end;',
  22174. ' TObjHelper = class helper for TObject',
  22175. ' function GetItems(Index: word): TObject;',
  22176. ' procedure SetItems(Index: word; Value: TObject);',
  22177. ' property Items[Index: word]: TObject read GetItems write SetItems; default;',
  22178. ' end;',
  22179. 'function Tobjhelper.GetItems(Index: word): TObject;',
  22180. 'begin',
  22181. ' Self[1][2]:=Self[3][4];',
  22182. 'end;',
  22183. 'procedure Tobjhelper.SetItems(Index: word; Value: TObject);',
  22184. 'begin',
  22185. 'end;',
  22186. 'var',
  22187. ' o: TObject;',
  22188. 'begin',
  22189. ' o[1][2]:=o[3][4];',
  22190. '']);
  22191. ConvertProgram;
  22192. CheckSource('TestClassHelper_Property_Array_DefaultDefault',
  22193. LinesToStr([ // statements
  22194. 'rtl.createClass(this, "TObject", null, function () {',
  22195. ' this.$init = function () {',
  22196. ' };',
  22197. ' this.$final = function () {',
  22198. ' };',
  22199. '});',
  22200. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  22201. ' this.GetItems = function (Index) {',
  22202. ' var Result = null;',
  22203. ' $mod.TObjHelper.SetItems.call($mod.TObjHelper.GetItems.call(this, 1), 2, $mod.TObjHelper.GetItems.call($mod.TObjHelper.GetItems.call(this, 3), 4));',
  22204. ' return Result;',
  22205. ' };',
  22206. ' this.SetItems = function (Index, Value) {',
  22207. ' };',
  22208. '});',
  22209. 'this.o = null;',
  22210. '']),
  22211. LinesToStr([ // $mod.$main
  22212. '$mod.TObjHelper.SetItems.call($mod.TObjHelper.GetItems.call($mod.o, 1), 2, $mod.TObjHelper.GetItems.call($mod.TObjHelper.GetItems.call($mod.o, 3), 4));',
  22213. '']));
  22214. end;
  22215. procedure TTestModule.TestClassHelper_ClassProperty;
  22216. begin
  22217. StartProgram(false);
  22218. Add([
  22219. 'type',
  22220. ' TObject = class',
  22221. ' class var FSize: word;',
  22222. ' class function GetSpeed: word;',
  22223. ' class procedure SetSpeed(Value: word); virtual; abstract;',
  22224. ' end;',
  22225. ' TObjHelper = class helper for TObject',
  22226. ' class function GetLeft: word;',
  22227. ' class procedure SetLeft(Value: word);',
  22228. ' class property Size: word read FSize write FSize;',
  22229. ' class property Speed: word read GetSpeed write SetSpeed;',
  22230. ' class property Left: word read GetLeft write SetLeft;',
  22231. ' end;',
  22232. ' TBird = class',
  22233. ' class property NotRight: word read GetLeft write SetLeft;',
  22234. ' class procedure DoIt;',
  22235. ' end;',
  22236. ' TBirdClass = class of TBird;',
  22237. 'class function Tobject.GetSpeed: word;',
  22238. 'begin',
  22239. ' Size:=Size+11;',
  22240. ' Speed:=Speed+12;',
  22241. ' Left:=Left+13;',
  22242. ' Self.Size:=Self.Size+21;',
  22243. ' Self.Speed:=Self.Speed+22;',
  22244. ' Self.Left:=Self.Left+23;',
  22245. ' with Self do begin',
  22246. ' Size:=Size+31;',
  22247. ' Speed:=Speed+32;',
  22248. ' Left:=Left+33;',
  22249. ' end;',
  22250. 'end;',
  22251. 'class function TObjHelper.GetLeft: word;',
  22252. 'begin',
  22253. ' Size:=Size+11;',
  22254. ' Speed:=Speed+12;',
  22255. ' Left:=Left+13;',
  22256. ' Self.Size:=Self.Size+21;',
  22257. ' Self.Speed:=Self.Speed+22;',
  22258. ' Self.Left:=Self.Left+23;',
  22259. ' with Self do begin',
  22260. ' Size:=Size+31;',
  22261. ' Speed:=Speed+32;',
  22262. ' Left:=Left+33;',
  22263. ' end;',
  22264. 'end;',
  22265. 'class procedure TObjHelper.SetLeft(Value: word);',
  22266. 'begin',
  22267. 'end;',
  22268. 'class procedure TBird.DoIt;',
  22269. 'begin',
  22270. ' NotRight:=NotRight+11;',
  22271. ' Self.NotRight:=Self.NotRight+21;',
  22272. ' with Self do NotRight:=NotRight+31;',
  22273. 'end;',
  22274. 'var',
  22275. ' b: TBird;',
  22276. ' c: TBirdClass;',
  22277. 'begin',
  22278. ' b.Size:=b.Size+11;',
  22279. ' b.Speed:=b.Speed+12;',
  22280. ' b.Left:=b.Left+13;',
  22281. ' b.NotRight:=b.NotRight+14;',
  22282. ' with b do begin',
  22283. ' Size:=Size+31;',
  22284. ' Speed:=Speed+32;',
  22285. ' Left:=Left+33;',
  22286. ' NotRight:=NotRight+34;',
  22287. ' end;',
  22288. ' c.Size:=c.Size+11;',
  22289. ' c.Speed:=c.Speed+12;',
  22290. ' c.Left:=c.Left+13;',
  22291. ' c.NotRight:=c.NotRight+14;',
  22292. ' with c do begin',
  22293. ' Size:=Size+31;',
  22294. ' Speed:=Speed+32;',
  22295. ' Left:=Left+33;',
  22296. ' NotRight:=NotRight+34;',
  22297. ' end;',
  22298. ' tbird.Size:=tbird.Size+11;',
  22299. ' tbird.Speed:=tbird.Speed+12;',
  22300. ' tbird.Left:=tbird.Left+13;',
  22301. ' tbird.NotRight:=tbird.NotRight+14;',
  22302. ' with tbird do begin',
  22303. ' Size:=Size+31;',
  22304. ' Speed:=Speed+32;',
  22305. ' Left:=Left+33;',
  22306. ' NotRight:=NotRight+34;',
  22307. ' end;',
  22308. '']);
  22309. ConvertProgram;
  22310. CheckSource('TestClassHelper_ClassProperty',
  22311. LinesToStr([ // statements
  22312. 'rtl.createClass(this, "TObject", null, function () {',
  22313. ' this.FSize = 0;',
  22314. ' this.$init = function () {',
  22315. ' };',
  22316. ' this.$final = function () {',
  22317. ' };',
  22318. ' this.GetSpeed = function () {',
  22319. ' var Result = 0;',
  22320. ' $mod.TObject.FSize = this.FSize + 11;',
  22321. ' this.SetSpeed(this.GetSpeed() + 12);',
  22322. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  22323. ' $mod.TObject.FSize = this.FSize + 21;',
  22324. ' this.SetSpeed(this.GetSpeed() + 22);',
  22325. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  22326. ' $mod.TObject.FSize = this.FSize + 31;',
  22327. ' this.SetSpeed(this.GetSpeed() + 32);',
  22328. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  22329. ' return Result;',
  22330. ' };',
  22331. '});',
  22332. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  22333. ' this.GetLeft = function () {',
  22334. ' var Result = 0;',
  22335. ' $mod.TObject.FSize = this.FSize + 11;',
  22336. ' this.SetSpeed(this.GetSpeed() + 12);',
  22337. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  22338. ' $mod.TObject.FSize = this.FSize + 21;',
  22339. ' this.SetSpeed(this.GetSpeed() + 22);',
  22340. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  22341. ' $mod.TObject.FSize = this.FSize + 31;',
  22342. ' this.SetSpeed(this.GetSpeed() + 32);',
  22343. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  22344. ' return Result;',
  22345. ' };',
  22346. ' this.SetLeft = function (Value) {',
  22347. ' };',
  22348. '});',
  22349. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  22350. ' this.DoIt = function () {',
  22351. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 11);',
  22352. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 21);',
  22353. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 31);',
  22354. ' };',
  22355. '});',
  22356. 'this.b = null;',
  22357. 'this.c = null;',
  22358. '']),
  22359. LinesToStr([ // $mod.$main
  22360. '$mod.TObject.FSize = $mod.b.FSize + 11;',
  22361. '$mod.b.$class.SetSpeed($mod.b.$class.GetSpeed() + 12);',
  22362. '$mod.TObjHelper.SetLeft.call($mod.b.$class, $mod.TObjHelper.GetLeft.call($mod.b.$class) + 13);',
  22363. '$mod.TObjHelper.SetLeft.call($mod.b.$class, $mod.TObjHelper.GetLeft.call($mod.b.$class) + 14);',
  22364. 'var $with = $mod.b;',
  22365. '$mod.TObject.FSize = $with.FSize + 31;',
  22366. '$with.$class.SetSpeed($with.$class.GetSpeed() + 32);',
  22367. '$mod.TObjHelper.SetLeft.call($with.$class, $mod.TObjHelper.GetLeft.call($with.$class) + 33);',
  22368. '$mod.TObjHelper.SetLeft.call($with.$class, $mod.TObjHelper.GetLeft.call($with.$class) + 34);',
  22369. '$mod.TObject.FSize = $mod.c.FSize + 11;',
  22370. '$mod.c.SetSpeed($mod.c.GetSpeed() + 12);',
  22371. '$mod.TObjHelper.SetLeft.call($mod.c, $mod.TObjHelper.GetLeft.call($mod.c) + 13);',
  22372. '$mod.TObjHelper.SetLeft.call($mod.c, $mod.TObjHelper.GetLeft.call($mod.c) + 14);',
  22373. 'var $with1 = $mod.c;',
  22374. '$mod.TObject.FSize = $with1.FSize + 31;',
  22375. '$with1.SetSpeed($with1.GetSpeed() + 32);',
  22376. '$mod.TObjHelper.SetLeft.call($with1, $mod.TObjHelper.GetLeft.call($with1) + 33);',
  22377. '$mod.TObjHelper.SetLeft.call($with1, $mod.TObjHelper.GetLeft.call($with1) + 34);',
  22378. '$mod.TObject.FSize = $mod.TBird.FSize + 11;',
  22379. '$mod.TBird.SetSpeed($mod.TBird.GetSpeed() + 12);',
  22380. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 13);',
  22381. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 14);',
  22382. 'var $with2 = $mod.TBird;',
  22383. '$mod.TObject.FSize = $with2.FSize + 31;',
  22384. '$with2.SetSpeed($with2.GetSpeed() + 32);',
  22385. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 33);',
  22386. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 34);',
  22387. '']));
  22388. end;
  22389. procedure TTestModule.TestClassHelper_ClassPropertyStatic;
  22390. begin
  22391. StartProgram(false);
  22392. Add([
  22393. 'type',
  22394. ' TObject = class',
  22395. ' class function GetSpeed: word; static;',
  22396. ' class procedure SetSpeed(Value: word); static;',
  22397. ' end;',
  22398. ' TObjHelper = class helper for TObject',
  22399. ' class function GetLeft: word; static;',
  22400. ' class procedure SetLeft(Value: word); static;',
  22401. ' class property Speed: word read GetSpeed write SetSpeed;',
  22402. ' class property Left: word read GetLeft write SetLeft;',
  22403. ' end;',
  22404. ' TBird = class',
  22405. ' class property NotRight: word read GetLeft write SetLeft;',
  22406. ' class procedure DoIt; static;',
  22407. ' class procedure DoSome;',
  22408. ' end;',
  22409. ' TBirdClass = class of TBird;',
  22410. 'class function Tobject.GetSpeed: word;',
  22411. 'begin',
  22412. ' Speed:=Speed+12;',
  22413. ' Left:=Left+13;',
  22414. 'end;',
  22415. 'class procedure TObject.SetSpeed(Value: word);',
  22416. 'begin',
  22417. 'end;',
  22418. 'class function TObjHelper.GetLeft: word;',
  22419. 'begin',
  22420. ' Speed:=Speed+12;',
  22421. ' Left:=Left+13;',
  22422. 'end;',
  22423. 'class procedure TObjHelper.SetLeft(Value: word);',
  22424. 'begin',
  22425. 'end;',
  22426. 'class procedure TBird.DoIt;',
  22427. 'begin',
  22428. ' NotRight:=NotRight+11;',
  22429. 'end;',
  22430. 'class procedure TBird.DoSome;',
  22431. 'begin',
  22432. ' Speed:=Speed+12;',
  22433. ' Left:=Left+13;',
  22434. ' Self.Speed:=Self.Speed+22;',
  22435. ' Self.Left:=Self.Left+23;',
  22436. ' with Self do begin',
  22437. ' Speed:=Speed+32;',
  22438. ' Left:=Left+33;',
  22439. ' end;',
  22440. ' NotRight:=NotRight+11;',
  22441. ' Self.NotRight:=Self.NotRight+21;',
  22442. ' with Self do NotRight:=NotRight+31;',
  22443. 'end;',
  22444. 'var',
  22445. ' b: TBird;',
  22446. ' c: TBirdClass;',
  22447. 'begin',
  22448. ' b.Speed:=b.Speed+12;',
  22449. ' b.Left:=b.Left+13;',
  22450. ' b.NotRight:=b.NotRight+14;',
  22451. ' with b do begin',
  22452. ' Speed:=Speed+32;',
  22453. ' Left:=Left+33;',
  22454. ' NotRight:=NotRight+34;',
  22455. ' end;',
  22456. ' c.Speed:=c.Speed+12;',
  22457. ' c.Left:=c.Left+13;',
  22458. ' c.NotRight:=c.NotRight+14;',
  22459. ' with c do begin',
  22460. ' Speed:=Speed+32;',
  22461. ' Left:=Left+33;',
  22462. ' NotRight:=NotRight+34;',
  22463. ' end;',
  22464. ' tbird.Speed:=tbird.Speed+12;',
  22465. ' tbird.Left:=tbird.Left+13;',
  22466. ' tbird.NotRight:=tbird.NotRight+14;',
  22467. ' with tbird do begin',
  22468. ' Speed:=Speed+32;',
  22469. ' Left:=Left+33;',
  22470. ' NotRight:=NotRight+34;',
  22471. ' end;',
  22472. '']);
  22473. ConvertProgram;
  22474. CheckSource('TestClassHelper_ClassPropertyStatic',
  22475. LinesToStr([ // statements
  22476. 'rtl.createClass(this, "TObject", null, function () {',
  22477. ' this.$init = function () {',
  22478. ' };',
  22479. ' this.$final = function () {',
  22480. ' };',
  22481. ' this.GetSpeed = function () {',
  22482. ' var Result = 0;',
  22483. ' $mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  22484. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  22485. ' return Result;',
  22486. ' };',
  22487. ' this.SetSpeed = function (Value) {',
  22488. ' };',
  22489. '});',
  22490. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  22491. ' this.GetLeft = function () {',
  22492. ' var Result = 0;',
  22493. ' $mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  22494. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  22495. ' return Result;',
  22496. ' };',
  22497. ' this.SetLeft = function (Value) {',
  22498. ' };',
  22499. '});',
  22500. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  22501. ' this.DoIt = function () {',
  22502. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 11);',
  22503. ' };',
  22504. ' this.DoSome = function () {',
  22505. ' this.SetSpeed(this.GetSpeed() + 12);',
  22506. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  22507. ' this.SetSpeed(this.GetSpeed() + 22);',
  22508. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 23);',
  22509. ' this.SetSpeed(this.GetSpeed() + 32);',
  22510. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  22511. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 11);',
  22512. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 21);',
  22513. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 31);',
  22514. ' };',
  22515. '});',
  22516. 'this.b = null;',
  22517. 'this.c = null;',
  22518. '']),
  22519. LinesToStr([ // $mod.$main
  22520. '$mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  22521. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  22522. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  22523. 'var $with = $mod.b;',
  22524. '$with.SetSpeed($with.GetSpeed() + 32);',
  22525. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  22526. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  22527. '$mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  22528. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  22529. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  22530. 'var $with1 = $mod.c;',
  22531. '$with1.SetSpeed($with1.GetSpeed() + 32);',
  22532. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  22533. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  22534. '$mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  22535. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  22536. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  22537. 'var $with2 = $mod.TBird;',
  22538. '$with2.SetSpeed($with2.GetSpeed() + 32);',
  22539. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  22540. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  22541. '']));
  22542. end;
  22543. procedure TTestModule.TestClassHelper_ClassProperty_Array;
  22544. begin
  22545. StartProgram(false);
  22546. Add([
  22547. 'type',
  22548. ' TObject = class',
  22549. ' class function GetSpeed(Index: boolean): word;',
  22550. ' class procedure SetSpeed(Index: boolean; Value: word); virtual; abstract;',
  22551. ' end;',
  22552. ' TObjHelper = class helper for TObject',
  22553. ' class function GetSize(Index: boolean): word;',
  22554. ' class procedure SetSize(Index: boolean; Value: word);',
  22555. ' class property Size[Index: boolean]: word read GetSize write SetSize;',
  22556. ' class property Speed[Index: boolean]: word read GetSpeed write SetSpeed;',
  22557. ' end;',
  22558. ' TBird = class',
  22559. ' class property Items[Index: boolean]: word read GetSize write SetSize;',
  22560. ' class procedure DoIt;',
  22561. ' end;',
  22562. ' TBirdClass = class of TBird;',
  22563. 'class function Tobject.GetSpeed(Index: boolean): word;',
  22564. 'begin',
  22565. ' Size[true]:=Size[false]+11;',
  22566. ' Speed[true]:=Speed[false]+12;',
  22567. ' Self.Size[true]:=Self.Size[false]+21;',
  22568. ' Self.Speed[true]:=Self.Speed[false]+22;',
  22569. ' with Self do begin',
  22570. ' Size[true]:=Size[false]+31;',
  22571. ' Speed[true]:=Speed[false]+32;',
  22572. ' end;',
  22573. 'end;',
  22574. 'class function TObjHelper.GetSize(Index: boolean): word;',
  22575. 'begin',
  22576. ' Size[true]:=Size[false]+11;',
  22577. ' Speed[true]:=Speed[false]+12;',
  22578. ' Self.Size[true]:=Self.Size[false]+21;',
  22579. ' Self.Speed[true]:=Self.Speed[false]+22;',
  22580. ' with Self do begin',
  22581. ' Size[true]:=Size[false]+31;',
  22582. ' Speed[true]:=Speed[false]+32;',
  22583. ' end;',
  22584. 'end;',
  22585. 'class procedure TObjHelper.SetSize(Index: boolean; Value: word);',
  22586. 'begin',
  22587. 'end;',
  22588. 'class procedure TBird.DoIt;',
  22589. 'begin',
  22590. ' Items[true]:=Items[false]+11;',
  22591. ' Self.Items[true]:=Self.Items[false]+21;',
  22592. ' with Self do Items[true]:=Items[false]+31;',
  22593. 'end;',
  22594. 'var',
  22595. ' b: TBird;',
  22596. ' c: TBirdClass;',
  22597. 'begin',
  22598. ' b.Size[true]:=b.Size[false]+11;',
  22599. ' b.Speed[true]:=b.Speed[false]+12;',
  22600. ' b.Items[true]:=b.Items[false]+13;',
  22601. ' with b do begin',
  22602. ' Size[true]:=Size[false]+21;',
  22603. ' Speed[true]:=Speed[false]+22;',
  22604. ' Items[true]:=Items[false]+23;',
  22605. ' end;',
  22606. ' c.Size[true]:=c.Size[false]+11;',
  22607. ' c.Speed[true]:=c.Speed[false]+12;',
  22608. ' c.Items[true]:=c.Items[false]+13;',
  22609. ' with c do begin',
  22610. ' Size[true]:=Size[false]+21;',
  22611. ' Speed[true]:=Speed[false]+22;',
  22612. ' Items[true]:=Items[false]+23;',
  22613. ' end;',
  22614. ' TBird.Size[true]:=TBird.Size[false]+11;',
  22615. ' TBird.Speed[true]:=TBird.Speed[false]+12;',
  22616. ' TBird.Items[true]:=TBird.Items[false]+13;',
  22617. ' with TBird do begin',
  22618. ' Size[true]:=Size[false]+21;',
  22619. ' Speed[true]:=Speed[false]+22;',
  22620. ' Items[true]:=Items[false]+23;',
  22621. ' end;',
  22622. '']);
  22623. ConvertProgram;
  22624. CheckSource('TestClassHelper_ClassProperty_Array',
  22625. LinesToStr([ // statements
  22626. 'rtl.createClass(this, "TObject", null, function () {',
  22627. ' this.$init = function () {',
  22628. ' };',
  22629. ' this.$final = function () {',
  22630. ' };',
  22631. ' this.GetSpeed = function (Index) {',
  22632. ' var Result = 0;',
  22633. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  22634. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  22635. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  22636. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  22637. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  22638. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  22639. ' return Result;',
  22640. ' };',
  22641. '});',
  22642. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  22643. ' this.GetSize = function (Index) {',
  22644. ' var Result = 0;',
  22645. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  22646. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  22647. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  22648. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  22649. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  22650. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  22651. ' return Result;',
  22652. ' };',
  22653. ' this.SetSize = function (Index, Value) {',
  22654. ' };',
  22655. '});',
  22656. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  22657. ' this.DoIt = function () {',
  22658. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  22659. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  22660. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  22661. ' };',
  22662. '});',
  22663. 'this.b = null;',
  22664. 'this.c = null;',
  22665. '']),
  22666. LinesToStr([ // $mod.$main
  22667. '$mod.TObjHelper.SetSize.call($mod.b.$class, true, $mod.TObjHelper.GetSize.call($mod.b.$class, false) + 11);',
  22668. '$mod.b.$class.SetSpeed(true, $mod.b.$class.GetSpeed(false) + 12);',
  22669. '$mod.TObjHelper.SetSize.call($mod.b.$class, true, $mod.TObjHelper.GetSize.call($mod.b.$class, false) + 13);',
  22670. 'var $with = $mod.b;',
  22671. '$mod.TObjHelper.SetSize.call($with.$class, true, $mod.TObjHelper.GetSize.call($with.$class, false) + 21);',
  22672. '$with.$class.SetSpeed(true, $with.$class.GetSpeed(false) + 22);',
  22673. '$mod.TObjHelper.SetSize.call($with.$class, true, $mod.TObjHelper.GetSize.call($with.$class, false) + 23);',
  22674. '$mod.TObjHelper.SetSize.call($mod.c, true, $mod.TObjHelper.GetSize.call($mod.c, false) + 11);',
  22675. '$mod.c.SetSpeed(true, $mod.c.GetSpeed(false) + 12);',
  22676. '$mod.TObjHelper.SetSize.call($mod.c, true, $mod.TObjHelper.GetSize.call($mod.c, false) + 13);',
  22677. 'var $with1 = $mod.c;',
  22678. '$mod.TObjHelper.SetSize.call($with1, true, $mod.TObjHelper.GetSize.call($with1, false) + 21);',
  22679. '$with1.SetSpeed(true, $with1.GetSpeed(false) + 22);',
  22680. '$mod.TObjHelper.SetSize.call($with1, true, $mod.TObjHelper.GetSize.call($with1, false) + 23);',
  22681. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 11);',
  22682. '$mod.TBird.SetSpeed(true, $mod.TBird.GetSpeed(false) + 12);',
  22683. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 13);',
  22684. 'var $with2 = $mod.TBird;',
  22685. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 21);',
  22686. '$with2.SetSpeed(true, $with2.GetSpeed(false) + 22);',
  22687. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 23);',
  22688. '']));
  22689. end;
  22690. procedure TTestModule.TestClassHelper_ForIn;
  22691. begin
  22692. StartProgram(false);
  22693. Add([
  22694. 'type',
  22695. ' TObject = class end;',
  22696. ' TItem = TObject;',
  22697. ' TEnumerator = class',
  22698. ' FCurrent: TItem;',
  22699. ' property Current: TItem read FCurrent;',
  22700. ' function MoveNext: boolean;',
  22701. ' end;',
  22702. ' TBird = class',
  22703. ' end;',
  22704. ' TBirdHelper = class helper for TBird',
  22705. ' function GetEnumerator: TEnumerator;',
  22706. ' end;',
  22707. 'function TEnumerator.MoveNext: boolean;',
  22708. 'begin',
  22709. 'end;',
  22710. 'function TBirdHelper.GetEnumerator: TEnumerator;',
  22711. 'begin',
  22712. 'end;',
  22713. 'var',
  22714. ' b: TBird;',
  22715. ' i, i2: TItem;',
  22716. 'begin',
  22717. ' for i in b do i2:=i;']);
  22718. ConvertProgram;
  22719. CheckSource('TestClassHelper_ForIn',
  22720. LinesToStr([ // statements
  22721. 'rtl.createClass(this, "TObject", null, function () {',
  22722. ' this.$init = function () {',
  22723. ' };',
  22724. ' this.$final = function () {',
  22725. ' };',
  22726. '});',
  22727. 'rtl.createClass(this, "TEnumerator", this.TObject, function () {',
  22728. ' this.$init = function () {',
  22729. ' $mod.TObject.$init.call(this);',
  22730. ' this.FCurrent = null;',
  22731. ' };',
  22732. ' this.$final = function () {',
  22733. ' this.FCurrent = undefined;',
  22734. ' $mod.TObject.$final.call(this);',
  22735. ' };',
  22736. ' this.MoveNext = function () {',
  22737. ' var Result = false;',
  22738. ' return Result;',
  22739. ' };',
  22740. '});',
  22741. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  22742. '});',
  22743. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  22744. ' this.GetEnumerator = function () {',
  22745. ' var Result = null;',
  22746. ' return Result;',
  22747. ' };',
  22748. '});',
  22749. 'this.b = null;',
  22750. 'this.i = null;',
  22751. 'this.i2 = null;'
  22752. ]),
  22753. LinesToStr([ // $mod.$main
  22754. 'var $in = $mod.TBirdHelper.GetEnumerator.call($mod.b);',
  22755. 'try {',
  22756. ' while ($in.MoveNext()){',
  22757. ' $mod.i = $in.FCurrent;',
  22758. ' $mod.i2 = $mod.i;',
  22759. ' }',
  22760. '} finally {',
  22761. ' $in = rtl.freeLoc($in)',
  22762. '};',
  22763. '']));
  22764. end;
  22765. procedure TTestModule.TestClassHelper_PassProperty;
  22766. begin
  22767. StartProgram(false);
  22768. Add([
  22769. 'type',
  22770. ' TObject = class',
  22771. ' FField: TObject;',
  22772. ' property Field: TObject read FField write FField;',
  22773. ' end;',
  22774. ' THelper = class helper for TObject',
  22775. ' procedure Fly;',
  22776. ' class procedure Run;',
  22777. ' class procedure Jump; static;',
  22778. ' end;',
  22779. 'procedure THelper.Fly;',
  22780. 'begin',
  22781. ' Field.Fly;',
  22782. ' Field.Run;',
  22783. ' Field.Jump;',
  22784. ' with Field do begin',
  22785. ' Fly;',
  22786. ' Run;',
  22787. ' Jump;',
  22788. ' end;',
  22789. 'end;',
  22790. 'class procedure THelper.Run;',
  22791. 'begin',
  22792. 'end;',
  22793. 'class procedure THelper.Jump;',
  22794. 'begin',
  22795. 'end;',
  22796. 'var',
  22797. ' b: TObject;',
  22798. 'begin',
  22799. ' b.Field.Fly;',
  22800. ' b.Field.Run;',
  22801. ' b.Field.Jump;',
  22802. ' with b do begin',
  22803. ' Field.Run;',
  22804. ' Field.Fly;',
  22805. ' Field.Jump;',
  22806. ' end;',
  22807. ' with b.Field do begin',
  22808. ' Run;',
  22809. ' Fly;',
  22810. ' Jump;',
  22811. ' end;',
  22812. '']);
  22813. ConvertProgram;
  22814. CheckSource('TestClassHelper_PassProperty',
  22815. LinesToStr([ // statements
  22816. 'rtl.createClass(this, "TObject", null, function () {',
  22817. ' this.$init = function () {',
  22818. ' this.FField = null;',
  22819. ' };',
  22820. ' this.$final = function () {',
  22821. ' this.FField = undefined;',
  22822. ' };',
  22823. '});',
  22824. 'rtl.createHelper(this, "THelper", null, function () {',
  22825. ' this.Fly = function () {',
  22826. ' $mod.THelper.Fly.call(this.FField);',
  22827. ' $mod.THelper.Run.call(this.FField.$class);',
  22828. ' $mod.THelper.Jump();',
  22829. ' var $with = this.FField;',
  22830. ' $mod.THelper.Fly.call($with);',
  22831. ' $mod.THelper.Run.call($with.$class);',
  22832. ' $mod.THelper.Jump();',
  22833. ' };',
  22834. ' this.Run = function () {',
  22835. ' };',
  22836. ' this.Jump = function () {',
  22837. ' };',
  22838. '});',
  22839. 'this.b = null;',
  22840. '']),
  22841. LinesToStr([ // $mod.$main
  22842. '$mod.THelper.Fly.call($mod.b.FField);',
  22843. '$mod.THelper.Run.call($mod.b.FField.$class);',
  22844. '$mod.THelper.Jump();',
  22845. 'var $with = $mod.b;',
  22846. '$mod.THelper.Run.call($with.FField.$class);',
  22847. '$mod.THelper.Fly.call($with.FField);',
  22848. '$mod.THelper.Jump();',
  22849. 'var $with1 = $mod.b.FField;',
  22850. '$mod.THelper.Run.call($with1.$class);',
  22851. '$mod.THelper.Fly.call($with1);',
  22852. '$mod.THelper.Jump();',
  22853. '']));
  22854. end;
  22855. procedure TTestModule.TestExtClassHelper_ClassVar;
  22856. begin
  22857. StartProgram(false);
  22858. Add([
  22859. '{$modeswitch externalclass}',
  22860. 'type',
  22861. ' TExtA = class external name ''ExtObj''',
  22862. ' end;',
  22863. ' THelper = class helper for TExtA',
  22864. ' const',
  22865. ' One = 1;',
  22866. ' Two: word = 2;',
  22867. ' class var',
  22868. ' Glob: word;',
  22869. ' function Foo(w: word): word;',
  22870. ' class function Bar(w: word): word; static;',
  22871. ' end;',
  22872. 'function THelper.foo(w: word): word;',
  22873. 'begin',
  22874. ' Result:=w;',
  22875. ' Two:=One+w;',
  22876. ' Glob:=Glob;',
  22877. ' Result:=Self.Glob;',
  22878. ' Self.Glob:=Self.Glob;',
  22879. ' with Self do Glob:=Glob;',
  22880. 'end;',
  22881. 'class function THelper.bar(w: word): word;',
  22882. 'begin',
  22883. ' Result:=w;',
  22884. ' Two:=One;',
  22885. ' Glob:=Glob;',
  22886. 'end;',
  22887. 'var o: TExtA;',
  22888. 'begin',
  22889. ' texta.two:=texta.one;',
  22890. ' texta.Glob:=texta.Glob;',
  22891. ' with texta do begin',
  22892. ' two:=one;',
  22893. ' Glob:=Glob;',
  22894. ' end;',
  22895. ' o.two:=o.one;',
  22896. ' o.Glob:=o.Glob;',
  22897. ' with o do begin',
  22898. ' two:=one;',
  22899. ' Glob:=Glob;',
  22900. ' end;',
  22901. '']);
  22902. ConvertProgram;
  22903. CheckSource('TestExtClassHelper_ClassVar',
  22904. LinesToStr([ // statements
  22905. 'rtl.createHelper(this, "THelper", null, function () {',
  22906. ' this.One = 1;',
  22907. ' this.Two = 2;',
  22908. ' this.Glob = 0;',
  22909. ' this.Foo = function (w) {',
  22910. ' var Result = 0;',
  22911. ' Result = w;',
  22912. ' $mod.THelper.Two = 1 + w;',
  22913. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22914. ' Result = $mod.THelper.Glob;',
  22915. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22916. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22917. ' return Result;',
  22918. ' };',
  22919. ' this.Bar = function (w) {',
  22920. ' var Result = 0;',
  22921. ' Result = w;',
  22922. ' $mod.THelper.Two = 1;',
  22923. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22924. ' return Result;',
  22925. ' };',
  22926. '});',
  22927. 'this.o = null;',
  22928. '']),
  22929. LinesToStr([ // $mod.$main
  22930. '$mod.THelper.Two = 1;',
  22931. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22932. '$mod.THelper.Two = 1;',
  22933. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22934. '$mod.THelper.Two = 1;',
  22935. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22936. 'var $with = $mod.o;',
  22937. '$mod.THelper.Two = 1;',
  22938. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22939. '']));
  22940. end;
  22941. procedure TTestModule.TestExtClassHelper_Method_Call;
  22942. begin
  22943. StartProgram(false);
  22944. Add([
  22945. '{$modeswitch externalclass}',
  22946. 'type',
  22947. ' TFly = function(w: word): word of object;',
  22948. ' TExtA = class external name ''ExtObj''',
  22949. ' procedure Run(w: word = 10);',
  22950. ' end;',
  22951. ' THelper = class helper for TExtA',
  22952. ' function Foo(w: word = 1): word;',
  22953. ' function Fly(w: word = 2): word; external name ''Fly'';',
  22954. ' end;',
  22955. 'var p: TFly;',
  22956. 'function THelper.foo(w: word): word;',
  22957. 'begin',
  22958. ' Run;',
  22959. ' Run();',
  22960. ' Run(11);',
  22961. ' Foo;',
  22962. ' Foo();',
  22963. ' Foo(12);',
  22964. ' Self.Foo;',
  22965. ' Self.Foo();',
  22966. ' Self.Foo(13);',
  22967. ' Fly;',
  22968. ' Fly();',
  22969. ' with Self do begin',
  22970. ' Foo;',
  22971. ' Foo();',
  22972. ' Foo(14);',
  22973. ' Fly;',
  22974. ' Fly();',
  22975. ' end;',
  22976. ' p:=@Fly;',
  22977. 'end;',
  22978. 'var Obj: TExtA;',
  22979. 'begin',
  22980. ' obj.Foo;',
  22981. ' obj.Foo();',
  22982. ' obj.Foo(21);',
  22983. ' obj.Fly;',
  22984. ' obj.Fly();',
  22985. ' with obj do begin',
  22986. ' Foo;',
  22987. ' Foo();',
  22988. ' Foo(22);',
  22989. ' Fly;',
  22990. ' Fly();',
  22991. ' end;',
  22992. ' p:[email protected];',
  22993. '']);
  22994. ConvertProgram;
  22995. CheckSource('TestExtClassHelper_Method_Call',
  22996. LinesToStr([ // statements
  22997. 'rtl.createHelper(this, "THelper", null, function () {',
  22998. ' this.Foo = function (w) {',
  22999. ' var Result = 0;',
  23000. ' this.Run(10);',
  23001. ' this.Run(10);',
  23002. ' this.Run(11);',
  23003. ' $mod.THelper.Foo.call(this, 1);',
  23004. ' $mod.THelper.Foo.call(this, 1);',
  23005. ' $mod.THelper.Foo.call(this, 12);',
  23006. ' $mod.THelper.Foo.call(this, 1);',
  23007. ' $mod.THelper.Foo.call(this, 1);',
  23008. ' $mod.THelper.Foo.call(this, 13);',
  23009. ' this.Fly(2);',
  23010. ' this.Fly(2);',
  23011. ' $mod.THelper.Foo.call(this, 1);',
  23012. ' $mod.THelper.Foo.call(this, 1);',
  23013. ' $mod.THelper.Foo.call(this, 14);',
  23014. ' this.Fly(2);',
  23015. ' this.Fly(2);',
  23016. ' $mod.p = rtl.createCallback(this, "Fly");',
  23017. ' return Result;',
  23018. ' };',
  23019. '});',
  23020. 'this.p = null;',
  23021. 'this.Obj = null;',
  23022. '']),
  23023. LinesToStr([ // $mod.$main
  23024. '$mod.THelper.Foo.call($mod.Obj, 1);',
  23025. '$mod.THelper.Foo.call($mod.Obj, 1);',
  23026. '$mod.THelper.Foo.call($mod.Obj, 21);',
  23027. '$mod.Obj.Fly(2);',
  23028. '$mod.Obj.Fly(2);',
  23029. 'var $with = $mod.Obj;',
  23030. '$mod.THelper.Foo.call($with, 1);',
  23031. '$mod.THelper.Foo.call($with, 1);',
  23032. '$mod.THelper.Foo.call($with, 22);',
  23033. '$with.Fly(2);',
  23034. '$with.Fly(2);',
  23035. '$mod.p = rtl.createCallback($mod.Obj, "Fly");',
  23036. '']));
  23037. end;
  23038. procedure TTestModule.TestExtClassHelper_ClassMethod_MissingStatic;
  23039. begin
  23040. StartProgram(false);
  23041. Add([
  23042. '{$modeswitch externalclass}',
  23043. 'type',
  23044. ' TExtA = class external name ''ExtObj''',
  23045. ' procedure Run(w: word = 10);',
  23046. ' end;',
  23047. ' THelper = class helper for TExtA',
  23048. ' class procedure Fly;',
  23049. ' end;',
  23050. 'class procedure THelper.Fly;',
  23051. 'begin end;',
  23052. 'begin',
  23053. '']);
  23054. SetExpectedPasResolverError(sHelperClassMethodForExtClassMustBeStatic,
  23055. nHelperClassMethodForExtClassMustBeStatic);
  23056. ConvertProgram;
  23057. end;
  23058. procedure TTestModule.TestRecordHelper_ClassVar;
  23059. begin
  23060. StartProgram(false);
  23061. Add([
  23062. 'type',
  23063. ' TRec = record',
  23064. ' end;',
  23065. ' THelper = record helper for TRec',
  23066. ' const',
  23067. ' One = 1;',
  23068. ' Two: word = 2;',
  23069. ' class var',
  23070. ' Glob: word;',
  23071. ' function Foo(w: word): word;',
  23072. ' class function Bar(w: word): word; static;',
  23073. ' end;',
  23074. 'function THelper.foo(w: word): word;',
  23075. 'begin',
  23076. ' Result:=w;',
  23077. ' Two:=One+w;',
  23078. ' Glob:=Glob;',
  23079. ' Result:=Self.Glob;',
  23080. ' Self.Glob:=Self.Glob;',
  23081. ' with Self do Glob:=Glob;',
  23082. ' Self:=Self;',
  23083. 'end;',
  23084. 'class function THelper.bar(w: word): word;',
  23085. 'begin',
  23086. ' Result:=w;',
  23087. ' Two:=One;',
  23088. ' Glob:=Glob;',
  23089. 'end;',
  23090. 'var r: TRec;',
  23091. 'begin',
  23092. ' trec.two:=trec.one;',
  23093. ' trec.Glob:=trec.Glob;',
  23094. ' with trec do begin',
  23095. ' two:=one;',
  23096. ' Glob:=Glob;',
  23097. ' end;',
  23098. ' r.two:=r.one;',
  23099. ' r.Glob:=r.Glob;',
  23100. ' with r do begin',
  23101. ' two:=one;',
  23102. ' Glob:=Glob;',
  23103. ' end;',
  23104. '']);
  23105. ConvertProgram;
  23106. CheckSource('TestRecordHelper_ClassVar',
  23107. LinesToStr([ // statements
  23108. 'rtl.recNewT(this, "TRec", function () {',
  23109. ' this.$eq = function (b) {',
  23110. ' return true;',
  23111. ' };',
  23112. ' this.$assign = function (s) {',
  23113. ' return this;',
  23114. ' };',
  23115. '});',
  23116. 'rtl.createHelper(this, "THelper", null, function () {',
  23117. ' this.One = 1;',
  23118. ' this.Two = 2;',
  23119. ' this.Glob = 0;',
  23120. ' this.Foo = function (w) {',
  23121. ' var Result = 0;',
  23122. ' Result = w;',
  23123. ' $mod.THelper.Two = 1 + w;',
  23124. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  23125. ' Result = $mod.THelper.Glob;',
  23126. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  23127. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  23128. ' this.$assign(this);',
  23129. ' return Result;',
  23130. ' };',
  23131. ' this.Bar = function (w) {',
  23132. ' var Result = 0;',
  23133. ' Result = w;',
  23134. ' $mod.THelper.Two = 1;',
  23135. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  23136. ' return Result;',
  23137. ' };',
  23138. '});',
  23139. 'this.r = this.TRec.$new();',
  23140. '']),
  23141. LinesToStr([ // $mod.$main
  23142. '$mod.THelper.Two = 1;',
  23143. '$mod.THelper.Glob = $mod.THelper.Glob;',
  23144. 'var $with = $mod.TRec;',
  23145. '$mod.THelper.Two = 1;',
  23146. '$mod.THelper.Glob = $mod.THelper.Glob;',
  23147. '$mod.THelper.Two = 1;',
  23148. '$mod.THelper.Glob = $mod.THelper.Glob;',
  23149. 'var $with1 = $mod.r;',
  23150. '$mod.THelper.Two = 1;',
  23151. '$mod.THelper.Glob = $mod.THelper.Glob;',
  23152. '']));
  23153. end;
  23154. procedure TTestModule.TestRecordHelper_Method_Call;
  23155. begin
  23156. StartProgram(false);
  23157. Add([
  23158. '{$modeswitch AdvancedRecords}',
  23159. 'type',
  23160. ' TRec = record',
  23161. ' procedure Run(w: word = 10);',
  23162. ' end;',
  23163. ' THelper = record helper for TRec',
  23164. ' function Foo(w: word = 1): word;',
  23165. ' end;',
  23166. 'procedure TRec.Run(w: word);',
  23167. 'begin',
  23168. ' Foo;',
  23169. ' Foo();',
  23170. ' Foo(2);',
  23171. ' Self.Foo;',
  23172. ' Self.Foo();',
  23173. ' Self.Foo(3);',
  23174. ' with Self do begin',
  23175. ' Foo;',
  23176. ' Foo();',
  23177. ' Foo(4);',
  23178. ' end;',
  23179. 'end;',
  23180. 'function THelper.foo(w: word): word;',
  23181. 'begin',
  23182. ' Run;',
  23183. ' Run();',
  23184. ' Run(11);',
  23185. ' Foo;',
  23186. ' Foo();',
  23187. ' Foo(12);',
  23188. ' Self.Foo;',
  23189. ' Self.Foo();',
  23190. ' Self.Foo(13);',
  23191. ' with Self do begin',
  23192. ' Foo;',
  23193. ' Foo();',
  23194. ' Foo(14);',
  23195. ' end;',
  23196. 'end;',
  23197. 'var Rec: TRec;',
  23198. 'begin',
  23199. ' Rec.Foo;',
  23200. ' Rec.Foo();',
  23201. ' Rec.Foo(21);',
  23202. ' with Rec do begin',
  23203. ' Foo;',
  23204. ' Foo();',
  23205. ' Foo(22);',
  23206. ' end;',
  23207. '']);
  23208. ConvertProgram;
  23209. CheckSource('TestRecordHelper_Method_Call',
  23210. LinesToStr([ // statements
  23211. 'rtl.recNewT(this, "TRec", function () {',
  23212. ' this.$eq = function (b) {',
  23213. ' return true;',
  23214. ' };',
  23215. ' this.$assign = function (s) {',
  23216. ' return this;',
  23217. ' };',
  23218. ' this.Run = function (w) {',
  23219. ' $mod.THelper.Foo.call(this, 1);',
  23220. ' $mod.THelper.Foo.call(this, 1);',
  23221. ' $mod.THelper.Foo.call(this, 2);',
  23222. ' $mod.THelper.Foo.call(this, 1);',
  23223. ' $mod.THelper.Foo.call(this, 1);',
  23224. ' $mod.THelper.Foo.call(this, 3);',
  23225. ' $mod.THelper.Foo.call(this, 1);',
  23226. ' $mod.THelper.Foo.call(this, 1);',
  23227. ' $mod.THelper.Foo.call(this, 4);',
  23228. ' };',
  23229. '});',
  23230. 'rtl.createHelper(this, "THelper", null, function () {',
  23231. ' this.Foo = function (w) {',
  23232. ' var Result = 0;',
  23233. ' this.Run(10);',
  23234. ' this.Run(10);',
  23235. ' this.Run(11);',
  23236. ' $mod.THelper.Foo.call(this, 1);',
  23237. ' $mod.THelper.Foo.call(this, 1);',
  23238. ' $mod.THelper.Foo.call(this, 12);',
  23239. ' $mod.THelper.Foo.call(this, 1);',
  23240. ' $mod.THelper.Foo.call(this, 1);',
  23241. ' $mod.THelper.Foo.call(this, 13);',
  23242. ' $mod.THelper.Foo.call(this, 1);',
  23243. ' $mod.THelper.Foo.call(this, 1);',
  23244. ' $mod.THelper.Foo.call(this, 14);',
  23245. ' return Result;',
  23246. ' };',
  23247. '});',
  23248. 'this.Rec = this.TRec.$new();',
  23249. '']),
  23250. LinesToStr([ // $mod.$main
  23251. '$mod.THelper.Foo.call($mod.Rec, 1);',
  23252. '$mod.THelper.Foo.call($mod.Rec, 1);',
  23253. '$mod.THelper.Foo.call($mod.Rec, 21);',
  23254. 'var $with = $mod.Rec;',
  23255. '$mod.THelper.Foo.call($with, 1);',
  23256. '$mod.THelper.Foo.call($with, 1);',
  23257. '$mod.THelper.Foo.call($with, 22);',
  23258. '']));
  23259. end;
  23260. procedure TTestModule.TestRecordHelper_Constructor;
  23261. begin
  23262. StartProgram(false);
  23263. Add([
  23264. '{$modeswitch AdvancedRecords}',
  23265. 'type',
  23266. ' TRec = record',
  23267. ' constructor Create(w: word);',
  23268. ' end;',
  23269. ' THelper = record helper for TRec',
  23270. ' constructor NewHlp(w: word);',
  23271. ' end;',
  23272. 'var',
  23273. ' Rec: TRec;',
  23274. 'constructor TRec.Create(w: word);',
  23275. 'begin',
  23276. ' NewHlp(2);', // normal call
  23277. ' trec.NewHlp(3);', // new instance
  23278. 'end;',
  23279. 'constructor THelper.NewHlp(w: word);',
  23280. 'begin',
  23281. ' create(2);', // normal call
  23282. ' trec.create(3);', // new instance
  23283. ' NewHlp(4);', // normal call
  23284. ' trec.NewHlp(5);', // new instance
  23285. 'end;',
  23286. 'begin',
  23287. ' rec.newhlp(2);', // normal call
  23288. ' with rec do newhlp(12);', // normal call
  23289. ' trec.newhlp(3);', // new instance
  23290. ' with trec do newhlp(13);', // new instance
  23291. '']);
  23292. ConvertProgram;
  23293. CheckSource('TestRecordHelper_Constructor',
  23294. LinesToStr([ // statements
  23295. 'rtl.recNewT(this, "TRec", function () {',
  23296. ' this.$eq = function (b) {',
  23297. ' return true;',
  23298. ' };',
  23299. ' this.$assign = function (s) {',
  23300. ' return this;',
  23301. ' };',
  23302. ' this.Create = function (w) {',
  23303. ' $mod.THelper.NewHlp.call(this, 2);',
  23304. ' $mod.THelper.$new("NewHlp", [3]);',
  23305. ' return this;',
  23306. ' };',
  23307. '});',
  23308. 'rtl.createHelper(this, "THelper", null, function () {',
  23309. ' this.NewHlp = function (w) {',
  23310. ' this.Create(2);',
  23311. ' $mod.TRec.$new().Create(3);',
  23312. ' $mod.THelper.NewHlp.call(this, 4);',
  23313. ' $mod.THelper.$new("NewHlp", [5]);',
  23314. ' return this;',
  23315. ' };',
  23316. ' this.$new = function (fn, args) {',
  23317. ' return this[fn].apply($mod.TRec.$new(), args);',
  23318. ' };',
  23319. '});',
  23320. 'this.Rec = this.TRec.$new();',
  23321. '']),
  23322. LinesToStr([ // $mod.$main
  23323. '$mod.THelper.NewHlp.call($mod.Rec, 2);',
  23324. 'var $with = $mod.Rec;',
  23325. '$mod.THelper.NewHlp.call($with, 12);',
  23326. '$mod.THelper.$new("NewHlp", [3]);',
  23327. 'var $with1 = $mod.TRec;',
  23328. '$mod.THelper.$new("NewHlp", [13]);',
  23329. '']));
  23330. end;
  23331. procedure TTestModule.TestTypeHelper_ClassVar;
  23332. begin
  23333. StartProgram(false);
  23334. Add([
  23335. '{$modeswitch typehelpers}',
  23336. 'type',
  23337. ' THelper = type helper for byte',
  23338. ' const',
  23339. ' One = 1;',
  23340. ' Two: word = 2;',
  23341. ' class var',
  23342. ' Glob: word;',
  23343. ' function Foo(w: word): word;',
  23344. ' class function Bar(w: word): word; static;',
  23345. ' end;',
  23346. 'function THelper.foo(w: word): word;',
  23347. 'begin',
  23348. ' Result:=w;',
  23349. ' Two:=One+w;',
  23350. ' Glob:=Glob;',
  23351. ' Result:=Self.Glob;',
  23352. ' Self.Glob:=Self.Glob;',
  23353. ' with Self do Glob:=Glob;',
  23354. 'end;',
  23355. 'class function THelper.bar(w: word): word;',
  23356. 'begin',
  23357. ' Result:=w;',
  23358. ' Two:=One;',
  23359. ' Glob:=Glob;',
  23360. 'end;',
  23361. 'var b: byte;',
  23362. 'begin',
  23363. ' byte.two:=byte.one;',
  23364. ' byte.Glob:=byte.Glob;',
  23365. ' with byte do begin',
  23366. ' two:=one;',
  23367. ' Glob:=Glob;',
  23368. ' end;',
  23369. ' b.two:=b.one;',
  23370. ' b.Glob:=b.Glob;',
  23371. ' with b do begin',
  23372. ' two:=one;',
  23373. ' Glob:=Glob;',
  23374. ' end;',
  23375. '']);
  23376. ConvertProgram;
  23377. CheckSource('TestTypeHelper_ClassVar',
  23378. LinesToStr([ // statements
  23379. 'rtl.createHelper(this, "THelper", null, function () {',
  23380. ' this.One = 1;',
  23381. ' this.Two = 2;',
  23382. ' this.Glob = 0;',
  23383. ' this.Foo = function (w) {',
  23384. ' var Result = 0;',
  23385. ' Result = w;',
  23386. ' $mod.THelper.Two = 1 + w;',
  23387. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  23388. ' Result = $mod.THelper.Glob;',
  23389. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  23390. ' var $with = this.get();',
  23391. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  23392. ' return Result;',
  23393. ' };',
  23394. ' this.Bar = function (w) {',
  23395. ' var Result = 0;',
  23396. ' Result = w;',
  23397. ' $mod.THelper.Two = 1;',
  23398. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  23399. ' return Result;',
  23400. ' };',
  23401. '});',
  23402. 'this.b = 0;',
  23403. '']),
  23404. LinesToStr([ // $mod.$main
  23405. '$mod.THelper.Two = 1;',
  23406. '$mod.THelper.Glob = $mod.THelper.Glob;',
  23407. '$mod.THelper.Two = 1;',
  23408. '$mod.THelper.Glob = $mod.THelper.Glob;',
  23409. '$mod.THelper.Two = 1;',
  23410. '$mod.THelper.Glob = $mod.THelper.Glob;',
  23411. 'var $with = $mod.b;',
  23412. '$mod.THelper.Two = 1;',
  23413. '$mod.THelper.Glob = $mod.THelper.Glob;',
  23414. '']));
  23415. end;
  23416. procedure TTestModule.TestTypeHelper_PassResultElement;
  23417. begin
  23418. StartProgram(false);
  23419. Add([
  23420. '{$modeswitch typehelpers}',
  23421. 'type',
  23422. ' THelper = type helper for word',
  23423. ' procedure DoIt(e: byte = 123);',
  23424. ' class procedure DoSome(e: byte = 456); static;',
  23425. ' end;',
  23426. 'procedure THelper.DoIt(e: byte);',
  23427. 'begin',
  23428. 'end;',
  23429. 'class procedure THelper.DoSome(e: byte);',
  23430. 'begin',
  23431. 'end;',
  23432. 'function Foo(w: word): word;',
  23433. 'begin',
  23434. ' Result.DoIt;',
  23435. ' Result.DoIt();',
  23436. ' Result.DoSome;',
  23437. ' Result.DoSome();',
  23438. ' with Result do begin',
  23439. ' DoIt;',
  23440. ' DoIt();',
  23441. ' DoSome;',
  23442. ' DoSome();',
  23443. ' end;',
  23444. 'end;',
  23445. 'begin',
  23446. '']);
  23447. ConvertProgram;
  23448. CheckSource('TestTypeHelper_PassResultElement',
  23449. LinesToStr([ // statements
  23450. 'rtl.createHelper(this, "THelper", null, function () {',
  23451. ' this.DoIt = function (e) {',
  23452. ' };',
  23453. ' this.DoSome = function (e) {',
  23454. ' };',
  23455. '});',
  23456. 'this.Foo = function (w) {',
  23457. ' var Result = 0;',
  23458. ' $mod.THelper.DoIt.call({',
  23459. ' get: function () {',
  23460. ' return Result;',
  23461. ' },',
  23462. ' set: function (v) {',
  23463. ' Result = v;',
  23464. ' }',
  23465. ' }, 123);',
  23466. ' $mod.THelper.DoIt.call({',
  23467. ' get: function () {',
  23468. ' return Result;',
  23469. ' },',
  23470. ' set: function (v) {',
  23471. ' Result = v;',
  23472. ' }',
  23473. ' }, 123);',
  23474. ' $mod.THelper.DoSome(456);',
  23475. ' $mod.THelper.DoSome(456);',
  23476. ' $mod.THelper.DoIt.call({',
  23477. ' get: function () {',
  23478. ' return Result;',
  23479. ' },',
  23480. ' set: function (v) {',
  23481. ' Result = v;',
  23482. ' }',
  23483. ' }, 123);',
  23484. ' $mod.THelper.DoIt.call({',
  23485. ' get: function () {',
  23486. ' return Result;',
  23487. ' },',
  23488. ' set: function (v) {',
  23489. ' Result = v;',
  23490. ' }',
  23491. ' }, 123);',
  23492. ' $mod.THelper.DoSome(456);',
  23493. ' $mod.THelper.DoSome(456);',
  23494. ' return Result;',
  23495. '};',
  23496. '']),
  23497. LinesToStr([ // $mod.$main
  23498. '']));
  23499. end;
  23500. procedure TTestModule.TestTypeHelper_PassArgs;
  23501. begin
  23502. StartProgram(false);
  23503. Add([
  23504. '{$modeswitch typehelpers}',
  23505. 'type',
  23506. ' THelper = type helper for word',
  23507. ' procedure DoIt(e: byte = 123);',
  23508. ' end;',
  23509. 'procedure THelper.DoIt(e: byte);',
  23510. 'begin',
  23511. 'end;',
  23512. 'procedure FooDefault(a: word);',
  23513. 'begin',
  23514. ' a.DoIt;',
  23515. ' with a do DoIt;',
  23516. 'end;',
  23517. 'procedure FooConst(const a: word);',
  23518. 'begin',
  23519. ' a.DoIt;',
  23520. ' with a do DoIt;',
  23521. 'end;',
  23522. 'procedure FooVar(var a: word);',
  23523. 'begin',
  23524. ' a.DoIt;',
  23525. ' with a do DoIt;',
  23526. 'end;',
  23527. 'begin',
  23528. '']);
  23529. ConvertProgram;
  23530. CheckSource('TestTypeHelper_PassArgs',
  23531. LinesToStr([ // statements
  23532. 'rtl.createHelper(this, "THelper", null, function () {',
  23533. ' this.DoIt = function (e) {',
  23534. ' };',
  23535. '});',
  23536. 'this.FooDefault = function (a) {',
  23537. ' $mod.THelper.DoIt.call({',
  23538. ' get: function () {',
  23539. ' return a;',
  23540. ' },',
  23541. ' set: function (v) {',
  23542. ' a = v;',
  23543. ' }',
  23544. ' }, 123);',
  23545. ' $mod.THelper.DoIt.call({',
  23546. ' get: function () {',
  23547. ' return a;',
  23548. ' },',
  23549. ' set: function (v) {',
  23550. ' a = v;',
  23551. ' }',
  23552. ' }, 123);',
  23553. '};',
  23554. 'this.FooConst = function (a) {',
  23555. ' $mod.THelper.DoIt.call({',
  23556. ' get: function () {',
  23557. ' return a;',
  23558. ' },',
  23559. ' set: function (v) {',
  23560. ' rtl.raiseE("EPropReadOnly");',
  23561. ' }',
  23562. ' }, 123);',
  23563. ' $mod.THelper.DoIt.call({',
  23564. ' get: function () {',
  23565. ' return a;',
  23566. ' },',
  23567. ' set: function () {',
  23568. ' rtl.raiseE("EPropReadOnly");',
  23569. ' }',
  23570. ' }, 123);',
  23571. '};',
  23572. 'this.FooVar = function (a) {',
  23573. ' $mod.THelper.DoIt.call(a, 123);',
  23574. ' var $with = a.get();',
  23575. ' $mod.THelper.DoIt.call(a, 123);',
  23576. '};',
  23577. '']),
  23578. LinesToStr([ // $mod.$main
  23579. '']));
  23580. end;
  23581. procedure TTestModule.TestTypeHelper_PassVarConst;
  23582. begin
  23583. StartProgram(false);
  23584. Add([
  23585. '{$modeswitch typehelpers}',
  23586. 'type',
  23587. ' THelper = type helper for word',
  23588. ' procedure DoIt(e: byte = 123);',
  23589. ' end;',
  23590. 'procedure THelper.DoIt(e: byte);',
  23591. 'begin',
  23592. 'end;',
  23593. 'var a: word;',
  23594. 'const c: word = 2;',
  23595. '{$writeableconst off}',
  23596. 'const r: word = 3;',
  23597. 'begin',
  23598. ' a.DoIt;',
  23599. ' with a do DoIt;',
  23600. ' c.DoIt;',
  23601. ' with c do DoIt;',
  23602. ' r.DoIt;',
  23603. ' with r do DoIt;',
  23604. '']);
  23605. ConvertProgram;
  23606. CheckSource('TestTypeHelper_PassVarConst',
  23607. LinesToStr([ // statements
  23608. 'rtl.createHelper(this, "THelper", null, function () {',
  23609. ' this.DoIt = function (e) {',
  23610. ' };',
  23611. '});',
  23612. 'this.a = 0;',
  23613. 'this.c = 2;',
  23614. 'this.r = 3;',
  23615. '']),
  23616. LinesToStr([ // $mod.$main
  23617. '$mod.THelper.DoIt.call({',
  23618. ' p: $mod,',
  23619. ' get: function () {',
  23620. ' return this.p.a;',
  23621. ' },',
  23622. ' set: function (v) {',
  23623. ' this.p.a = v;',
  23624. ' }',
  23625. '}, 123);',
  23626. 'var $with = $mod.a;',
  23627. '$mod.THelper.DoIt.call({',
  23628. ' get: function () {',
  23629. ' return $with;',
  23630. ' },',
  23631. ' set: function (v) {',
  23632. ' $with = v;',
  23633. ' }',
  23634. '}, 123);',
  23635. '$mod.THelper.DoIt.call({',
  23636. ' p: $mod,',
  23637. ' get: function () {',
  23638. ' return this.p.c;',
  23639. ' },',
  23640. ' set: function (v) {',
  23641. ' this.p.c = v;',
  23642. ' }',
  23643. '}, 123);',
  23644. 'var $with1 = $mod.c;',
  23645. '$mod.THelper.DoIt.call({',
  23646. ' get: function () {',
  23647. ' return $with1;',
  23648. ' },',
  23649. ' set: function (v) {',
  23650. ' $with1 = v;',
  23651. ' }',
  23652. '}, 123);',
  23653. '$mod.THelper.DoIt.call({',
  23654. ' get: function () {',
  23655. ' return 3;',
  23656. ' },',
  23657. ' set: function (v) {',
  23658. ' rtl.raiseE("EPropReadOnly");',
  23659. ' }',
  23660. '}, 123);',
  23661. 'var $with2 = 3;',
  23662. ' $mod.THelper.DoIt.call({',
  23663. ' get: function () {',
  23664. ' return $with2;',
  23665. ' },',
  23666. ' set: function () {',
  23667. ' rtl.raiseE("EPropReadOnly");',
  23668. ' }',
  23669. ' }, 123);',
  23670. '']));
  23671. end;
  23672. procedure TTestModule.TestTypeHelper_PassFuncResult;
  23673. begin
  23674. StartProgram(false);
  23675. Add([
  23676. '{$modeswitch typehelpers}',
  23677. 'type',
  23678. ' THelper = type helper for word',
  23679. ' procedure DoIt(e: byte = 123);',
  23680. ' end;',
  23681. 'procedure THelper.DoIt(e: byte);',
  23682. 'begin',
  23683. 'end;',
  23684. 'function Foo(b: byte = 1): word;',
  23685. 'begin',
  23686. 'end;',
  23687. 'begin',
  23688. ' Foo.DoIt;',
  23689. ' Foo().DoIt;',
  23690. ' with Foo do DoIt;',
  23691. ' with Foo() do DoIt;',
  23692. '']);
  23693. ConvertProgram;
  23694. CheckSource('TestTypeHelper_PassFuncResult',
  23695. LinesToStr([ // statements
  23696. 'rtl.createHelper(this, "THelper", null, function () {',
  23697. ' this.DoIt = function (e) {',
  23698. ' };',
  23699. '});',
  23700. 'this.Foo = function (b) {',
  23701. ' var Result = 0;',
  23702. ' return Result;',
  23703. '};',
  23704. '']),
  23705. LinesToStr([ // $mod.$main
  23706. '$mod.THelper.DoIt.call({',
  23707. ' a: $mod.Foo(1),',
  23708. ' get: function () {',
  23709. ' return this.a;',
  23710. ' },',
  23711. ' set: function (v) {',
  23712. ' this.a = v;',
  23713. ' }',
  23714. '}, 123);',
  23715. '$mod.THelper.DoIt.call({',
  23716. ' a: $mod.Foo(1),',
  23717. ' get: function () {',
  23718. ' return this.a;',
  23719. ' },',
  23720. ' set: function (v) {',
  23721. ' this.a = v;',
  23722. ' }',
  23723. '}, 123);',
  23724. 'var $with = $mod.Foo(1);',
  23725. '$mod.THelper.DoIt.call({',
  23726. ' get: function () {',
  23727. ' return $with;',
  23728. ' },',
  23729. ' set: function (v) {',
  23730. ' $with = v;',
  23731. ' }',
  23732. '}, 123);',
  23733. 'var $with1 = $mod.Foo(1);',
  23734. '$mod.THelper.DoIt.call({',
  23735. ' get: function () {',
  23736. ' return $with1;',
  23737. ' },',
  23738. ' set: function (v) {',
  23739. ' $with1 = v;',
  23740. ' }',
  23741. '}, 123);',
  23742. '']));
  23743. end;
  23744. procedure TTestModule.TestTypeHelper_PassPropertyField;
  23745. begin
  23746. StartProgram(false);
  23747. Add([
  23748. '{$modeswitch typehelpers}',
  23749. 'type',
  23750. ' TObject = class',
  23751. ' FField: word;',
  23752. ' procedure SetField(Value: word);',
  23753. ' property Field: word read FField write SetField;',
  23754. ' end;',
  23755. ' THelper = type helper for word',
  23756. ' procedure Fly;',
  23757. ' class procedure Run; static;',
  23758. ' end;',
  23759. 'procedure TObject.SetField(Value: word);',
  23760. 'begin',
  23761. ' Field.Fly;',
  23762. ' Field.Run;',
  23763. ' Self.Field.Fly;',
  23764. ' Self.Field.Run;',
  23765. ' with Self do begin',
  23766. ' Field.Fly;',
  23767. ' Field.Run;',
  23768. ' end;',
  23769. ' with Self.Field do begin',
  23770. ' Fly;',
  23771. ' Run;',
  23772. ' end;',
  23773. 'end;',
  23774. 'procedure THelper.Fly;',
  23775. 'begin',
  23776. 'end;',
  23777. 'class procedure THelper.Run;',
  23778. 'begin',
  23779. 'end;',
  23780. 'var',
  23781. ' o: TObject;',
  23782. 'begin',
  23783. ' o.Field.Fly;',
  23784. ' o.Field.Run;',
  23785. ' with o do begin',
  23786. ' Field.Fly;',
  23787. ' Field.Run;',
  23788. ' end;',
  23789. ' with o.Field do begin',
  23790. ' Fly;',
  23791. ' Run;',
  23792. ' end;',
  23793. '']);
  23794. ConvertProgram;
  23795. CheckSource('TestTypeHelper_PassPropertyField',
  23796. LinesToStr([ // statements
  23797. 'rtl.createClass(this, "TObject", null, function () {',
  23798. ' this.$init = function () {',
  23799. ' this.FField = 0;',
  23800. ' };',
  23801. ' this.$final = function () {',
  23802. ' };',
  23803. ' this.SetField = function (Value) {',
  23804. ' $mod.THelper.Fly.call({',
  23805. ' p: this,',
  23806. ' get: function () {',
  23807. ' return this.p.FField;',
  23808. ' },',
  23809. ' set: function (v) {',
  23810. ' this.p.FField = v;',
  23811. ' }',
  23812. ' });',
  23813. ' $mod.THelper.Run();',
  23814. ' $mod.THelper.Fly.call({',
  23815. ' p: this,',
  23816. ' get: function () {',
  23817. ' return this.p.FField;',
  23818. ' },',
  23819. ' set: function (v) {',
  23820. ' this.p.FField = v;',
  23821. ' }',
  23822. ' });',
  23823. ' $mod.THelper.Run();',
  23824. ' $mod.THelper.Fly.call({',
  23825. ' p: this,',
  23826. ' get: function () {',
  23827. ' return this.p.FField;',
  23828. ' },',
  23829. ' set: function (v) {',
  23830. ' this.p.FField = v;',
  23831. ' }',
  23832. ' });',
  23833. ' $mod.THelper.Run();',
  23834. ' var $with = this.FField;',
  23835. ' $mod.THelper.Fly.call({',
  23836. ' get: function () {',
  23837. ' return $with;',
  23838. ' },',
  23839. ' set: function (v) {',
  23840. ' $with = v;',
  23841. ' }',
  23842. ' });',
  23843. ' $mod.THelper.Run();',
  23844. ' };',
  23845. '});',
  23846. 'rtl.createHelper(this, "THelper", null, function () {',
  23847. ' this.Fly = function () {',
  23848. ' };',
  23849. ' this.Run = function () {',
  23850. ' };',
  23851. '});',
  23852. 'this.o = null;',
  23853. '']),
  23854. LinesToStr([ // $mod.$main
  23855. '$mod.THelper.Fly.call({',
  23856. ' p: $mod.o,',
  23857. ' get: function () {',
  23858. ' return this.p.FField;',
  23859. ' },',
  23860. ' set: function (v) {',
  23861. ' this.p.FField = v;',
  23862. ' }',
  23863. '});',
  23864. '$mod.THelper.Run();',
  23865. 'var $with = $mod.o;',
  23866. '$mod.THelper.Fly.call({',
  23867. ' p: $with,',
  23868. ' get: function () {',
  23869. ' return this.p.FField;',
  23870. ' },',
  23871. ' set: function (v) {',
  23872. ' this.p.FField = v;',
  23873. ' }',
  23874. '});',
  23875. '$mod.THelper.Run();',
  23876. 'var $with1 = $mod.o.FField;',
  23877. '$mod.THelper.Fly.call({',
  23878. ' get: function () {',
  23879. ' return $with1;',
  23880. ' },',
  23881. ' set: function (v) {',
  23882. ' $with1 = v;',
  23883. ' }',
  23884. '});',
  23885. '$mod.THelper.Run();',
  23886. '']));
  23887. end;
  23888. procedure TTestModule.TestTypeHelper_PassPropertyGetter;
  23889. begin
  23890. StartProgram(false);
  23891. Add([
  23892. '{$modeswitch typehelpers}',
  23893. 'type',
  23894. ' TObject = class',
  23895. ' FField: word;',
  23896. ' function GetField: word;',
  23897. ' property Field: word read GetField write FField;',
  23898. ' end;',
  23899. ' THelper = type helper for word',
  23900. ' procedure Fly;',
  23901. ' class procedure Run; static;',
  23902. ' end;',
  23903. 'function TObject.GetField: word;',
  23904. 'begin',
  23905. ' Field.Fly;',
  23906. ' Field.Run;',
  23907. ' Self.Field.Fly;',
  23908. ' Self.Field.Run;',
  23909. ' with Self do begin',
  23910. ' Field.Fly;',
  23911. ' Field.Run;',
  23912. ' end;',
  23913. ' with Self.Field do begin',
  23914. ' Fly;',
  23915. ' Run;',
  23916. ' end;',
  23917. 'end;',
  23918. 'procedure THelper.Fly;',
  23919. 'begin',
  23920. 'end;',
  23921. 'class procedure THelper.Run;',
  23922. 'begin',
  23923. 'end;',
  23924. 'var',
  23925. ' o: TObject;',
  23926. 'begin',
  23927. ' o.Field.Fly;',
  23928. ' o.Field.Run;',
  23929. ' with o do begin',
  23930. ' Field.Fly;',
  23931. ' Field.Run;',
  23932. ' end;',
  23933. ' with o.Field do begin',
  23934. ' Fly;',
  23935. ' Run;',
  23936. ' end;',
  23937. '']);
  23938. ConvertProgram;
  23939. CheckSource('TestTypeHelper_PassPropertyGetter',
  23940. LinesToStr([ // statements
  23941. 'rtl.createClass(this, "TObject", null, function () {',
  23942. ' this.$init = function () {',
  23943. ' this.FField = 0;',
  23944. ' };',
  23945. ' this.$final = function () {',
  23946. ' };',
  23947. ' this.GetField = function () {',
  23948. ' var Result = 0;',
  23949. ' $mod.THelper.Fly.call({',
  23950. ' p: this.GetField(),',
  23951. ' get: function () {',
  23952. ' return this.p;',
  23953. ' },',
  23954. ' set: function (v) {',
  23955. ' this.p = v;',
  23956. ' }',
  23957. ' });',
  23958. ' $mod.THelper.Run();',
  23959. ' $mod.THelper.Fly.call({',
  23960. ' p: this.GetField(),',
  23961. ' get: function () {',
  23962. ' return this.p;',
  23963. ' },',
  23964. ' set: function (v) {',
  23965. ' this.p = v;',
  23966. ' }',
  23967. ' });',
  23968. ' $mod.THelper.Run();',
  23969. ' $mod.THelper.Fly.call({',
  23970. ' p: this.GetField(),',
  23971. ' get: function () {',
  23972. ' return this.p;',
  23973. ' },',
  23974. ' set: function (v) {',
  23975. ' this.p = v;',
  23976. ' }',
  23977. ' });',
  23978. ' $mod.THelper.Run();',
  23979. ' var $with = this.GetField();',
  23980. ' $mod.THelper.Fly.call({',
  23981. ' get: function () {',
  23982. ' return $with;',
  23983. ' },',
  23984. ' set: function (v) {',
  23985. ' $with = v;',
  23986. ' }',
  23987. ' });',
  23988. ' $mod.THelper.Run();',
  23989. ' return Result;',
  23990. ' };',
  23991. '});',
  23992. 'rtl.createHelper(this, "THelper", null, function () {',
  23993. ' this.Fly = function () {',
  23994. ' };',
  23995. ' this.Run = function () {',
  23996. ' };',
  23997. '});',
  23998. 'this.o = null;',
  23999. '']),
  24000. LinesToStr([ // $mod.$main
  24001. '$mod.THelper.Fly.call({',
  24002. ' p: $mod.o.GetField(),',
  24003. ' get: function () {',
  24004. ' return this.p;',
  24005. ' },',
  24006. ' set: function (v) {',
  24007. ' this.p = v;',
  24008. ' }',
  24009. '});',
  24010. '$mod.THelper.Run();',
  24011. 'var $with = $mod.o;',
  24012. '$mod.THelper.Fly.call({',
  24013. ' p: $with.GetField(),',
  24014. ' get: function () {',
  24015. ' return this.p;',
  24016. ' },',
  24017. ' set: function (v) {',
  24018. ' this.p = v;',
  24019. ' }',
  24020. '});',
  24021. '$mod.THelper.Run();',
  24022. 'var $with1 = $mod.o.GetField();',
  24023. '$mod.THelper.Fly.call({',
  24024. ' get: function () {',
  24025. ' return $with1;',
  24026. ' },',
  24027. ' set: function (v) {',
  24028. ' $with1 = v;',
  24029. ' }',
  24030. '});',
  24031. '$mod.THelper.Run();',
  24032. '']));
  24033. end;
  24034. procedure TTestModule.TestTypeHelper_PassClassPropertyField;
  24035. begin
  24036. StartProgram(false);
  24037. Add([
  24038. '{$modeswitch typehelpers}',
  24039. 'type',
  24040. ' TObject = class',
  24041. ' class var FField: word;',
  24042. ' class procedure SetField(Value: word);',
  24043. ' class property Field: word read FField write SetField;',
  24044. ' end;',
  24045. ' THelper = type helper for word',
  24046. ' procedure Fly(n: byte);',
  24047. ' end;',
  24048. 'class procedure TObject.SetField(Value: word);',
  24049. 'begin',
  24050. ' Field.Fly(1);',
  24051. ' Self.Field.Fly(2);',
  24052. ' with Self do Field.Fly(3);',
  24053. ' with Self.Field do Fly(4);',
  24054. ' TObject.Field.Fly(5);',
  24055. ' with TObject do Field.Fly(6);',
  24056. ' with TObject.Field do Fly(7);',
  24057. 'end;',
  24058. 'procedure THelper.Fly(n: byte);',
  24059. 'begin',
  24060. 'end;',
  24061. 'var',
  24062. ' o: TObject;',
  24063. 'begin',
  24064. ' o.Field.Fly(11);',
  24065. ' with o do Field.Fly(12);',
  24066. ' with o.Field do Fly(13);',
  24067. ' TObject.Field.Fly(14);',
  24068. ' with TObject do Field.Fly(15);',
  24069. ' with TObject.Field do Fly(16);',
  24070. '']);
  24071. ConvertProgram;
  24072. CheckSource('TestTypeHelper_PassClassPropertyField',
  24073. LinesToStr([ // statements
  24074. 'rtl.createClass(this, "TObject", null, function () {',
  24075. ' this.FField = 0;',
  24076. ' this.$init = function () {',
  24077. ' };',
  24078. ' this.$final = function () {',
  24079. ' };',
  24080. ' this.SetField = function (Value) {',
  24081. ' $mod.THelper.Fly.call({',
  24082. ' p: this,',
  24083. ' get: function () {',
  24084. ' return this.p.FField;',
  24085. ' },',
  24086. ' set: function (v) {',
  24087. ' $mod.TObject.FField = v;',
  24088. ' }',
  24089. ' }, 1);',
  24090. ' $mod.THelper.Fly.call({',
  24091. ' p: this,',
  24092. ' get: function () {',
  24093. ' return this.p.FField;',
  24094. ' },',
  24095. ' set: function (v) {',
  24096. ' $mod.TObject.FField = v;',
  24097. ' }',
  24098. ' }, 2);',
  24099. ' $mod.THelper.Fly.call({',
  24100. ' p: this,',
  24101. ' get: function () {',
  24102. ' return this.p.FField;',
  24103. ' },',
  24104. ' set: function (v) {',
  24105. ' $mod.TObject.FField = v;',
  24106. ' }',
  24107. ' }, 3);',
  24108. ' var $with = this.FField;',
  24109. ' $mod.THelper.Fly.call({',
  24110. ' get: function () {',
  24111. ' return $with;',
  24112. ' },',
  24113. ' set: function (v) {',
  24114. ' $with = v;',
  24115. ' }',
  24116. ' }, 4);',
  24117. ' $mod.THelper.Fly.call({',
  24118. ' p: $mod.TObject,',
  24119. ' get: function () {',
  24120. ' return this.p.FField;',
  24121. ' },',
  24122. ' set: function (v) {',
  24123. ' $mod.TObject.FField = v;',
  24124. ' }',
  24125. ' }, 5);',
  24126. ' var $with1 = $mod.TObject;',
  24127. ' $mod.THelper.Fly.call({',
  24128. ' p: $with1,',
  24129. ' get: function () {',
  24130. ' return this.p.FField;',
  24131. ' },',
  24132. ' set: function (v) {',
  24133. ' $mod.TObject.FField = v;',
  24134. ' }',
  24135. ' }, 6);',
  24136. ' var $with2 = $mod.TObject.FField;',
  24137. ' $mod.THelper.Fly.call({',
  24138. ' get: function () {',
  24139. ' return $with2;',
  24140. ' },',
  24141. ' set: function (v) {',
  24142. ' $with2 = v;',
  24143. ' }',
  24144. ' }, 7);',
  24145. ' };',
  24146. '});',
  24147. 'rtl.createHelper(this, "THelper", null, function () {',
  24148. ' this.Fly = function (n) {',
  24149. ' };',
  24150. '});',
  24151. 'this.o = null;',
  24152. '']),
  24153. LinesToStr([ // $mod.$main
  24154. '$mod.THelper.Fly.call({',
  24155. ' p: $mod.o,',
  24156. ' get: function () {',
  24157. ' return this.p.FField;',
  24158. ' },',
  24159. ' set: function (v) {',
  24160. ' $mod.TObject.FField = v;',
  24161. ' }',
  24162. '}, 11);',
  24163. 'var $with = $mod.o;',
  24164. '$mod.THelper.Fly.call({',
  24165. ' p: $with,',
  24166. ' get: function () {',
  24167. ' return this.p.FField;',
  24168. ' },',
  24169. ' set: function (v) {',
  24170. ' $mod.TObject.FField = v;',
  24171. ' }',
  24172. '}, 12);',
  24173. 'var $with1 = $mod.o.FField;',
  24174. '$mod.THelper.Fly.call({',
  24175. ' get: function () {',
  24176. ' return $with1;',
  24177. ' },',
  24178. ' set: function (v) {',
  24179. ' $with1 = v;',
  24180. ' }',
  24181. '}, 13);',
  24182. '$mod.THelper.Fly.call({',
  24183. ' p: $mod.TObject,',
  24184. ' get: function () {',
  24185. ' return this.p.FField;',
  24186. ' },',
  24187. ' set: function (v) {',
  24188. ' $mod.TObject.FField = v;',
  24189. ' }',
  24190. '}, 14);',
  24191. 'var $with2 = $mod.TObject;',
  24192. '$mod.THelper.Fly.call({',
  24193. ' p: $with2,',
  24194. ' get: function () {',
  24195. ' return this.p.FField;',
  24196. ' },',
  24197. ' set: function (v) {',
  24198. ' $mod.TObject.FField = v;',
  24199. ' }',
  24200. '}, 15);',
  24201. 'var $with3 = $mod.TObject.FField;',
  24202. '$mod.THelper.Fly.call({',
  24203. ' get: function () {',
  24204. ' return $with3;',
  24205. ' },',
  24206. ' set: function (v) {',
  24207. ' $with3 = v;',
  24208. ' }',
  24209. '}, 16);',
  24210. '']));
  24211. end;
  24212. procedure TTestModule.TestTypeHelper_PassClassPropertyGetterStatic;
  24213. begin
  24214. StartProgram(false);
  24215. Add([
  24216. '{$modeswitch typehelpers}',
  24217. 'type',
  24218. ' TObject = class',
  24219. ' class var FField: word;',
  24220. ' class function GetField: word; static;',
  24221. ' class property Field: word read GetField write FField;',
  24222. ' end;',
  24223. ' THelper = type helper for word',
  24224. ' procedure Fly(n: byte);',
  24225. ' end;',
  24226. 'class function TObject.GetField: word;',
  24227. 'begin',
  24228. ' Field.Fly(1);',
  24229. ' TObject.Field.Fly(5);',
  24230. ' with TObject do Field.Fly(6);',
  24231. ' with TObject.Field do Fly(7);',
  24232. 'end;',
  24233. 'procedure THelper.Fly(n: byte);',
  24234. 'begin',
  24235. 'end;',
  24236. 'var',
  24237. ' o: TObject;',
  24238. 'begin',
  24239. ' o.Field.Fly(11);',
  24240. ' with o do Field.Fly(12);',
  24241. ' with o.Field do Fly(13);',
  24242. '']);
  24243. ConvertProgram;
  24244. CheckSource('TestTypeHelper_PassClassPropertyGetterStatic',
  24245. LinesToStr([ // statements
  24246. 'rtl.createClass(this, "TObject", null, function () {',
  24247. ' this.FField = 0;',
  24248. ' this.$init = function () {',
  24249. ' };',
  24250. ' this.$final = function () {',
  24251. ' };',
  24252. ' this.GetField = function () {',
  24253. ' var Result = 0;',
  24254. ' $mod.THelper.Fly.call({',
  24255. ' p: $mod.TObject.GetField(),',
  24256. ' get: function () {',
  24257. ' return this.p;',
  24258. ' },',
  24259. ' set: function (v) {',
  24260. ' this.p = v;',
  24261. ' }',
  24262. ' }, 1);',
  24263. ' $mod.THelper.Fly.call({',
  24264. ' p: $mod.TObject.GetField(),',
  24265. ' get: function () {',
  24266. ' return this.p;',
  24267. ' },',
  24268. ' set: function (v) {',
  24269. ' this.p = v;',
  24270. ' }',
  24271. ' }, 5);',
  24272. ' var $with = $mod.TObject;',
  24273. ' $mod.THelper.Fly.call({',
  24274. ' p: $with.GetField(),',
  24275. ' get: function () {',
  24276. ' return this.p;',
  24277. ' },',
  24278. ' set: function (v) {',
  24279. ' this.p = v;',
  24280. ' }',
  24281. ' }, 6);',
  24282. ' var $with1 = $mod.TObject.GetField();',
  24283. ' $mod.THelper.Fly.call({',
  24284. ' get: function () {',
  24285. ' return $with1;',
  24286. ' },',
  24287. ' set: function (v) {',
  24288. ' $with1 = v;',
  24289. ' }',
  24290. ' }, 7);',
  24291. ' return Result;',
  24292. ' };',
  24293. '});',
  24294. 'rtl.createHelper(this, "THelper", null, function () {',
  24295. ' this.Fly = function (n) {',
  24296. ' };',
  24297. '});',
  24298. 'this.o = null;',
  24299. '']),
  24300. LinesToStr([ // $mod.$main
  24301. '$mod.THelper.Fly.call({',
  24302. ' p: $mod.TObject.GetField(),',
  24303. ' get: function () {',
  24304. ' return this.p;',
  24305. ' },',
  24306. ' set: function (v) {',
  24307. ' this.p = v;',
  24308. ' }',
  24309. '}, 11);',
  24310. 'var $with = $mod.o;',
  24311. '$mod.THelper.Fly.call({',
  24312. ' p: $with.GetField(),',
  24313. ' get: function () {',
  24314. ' return this.p;',
  24315. ' },',
  24316. ' set: function (v) {',
  24317. ' this.p = v;',
  24318. ' }',
  24319. '}, 12);',
  24320. 'var $with1 = $mod.TObject.GetField();',
  24321. '$mod.THelper.Fly.call({',
  24322. ' get: function () {',
  24323. ' return $with1;',
  24324. ' },',
  24325. ' set: function (v) {',
  24326. ' $with1 = v;',
  24327. ' }',
  24328. '}, 13);',
  24329. '']));
  24330. end;
  24331. procedure TTestModule.TestTypeHelper_PassClassPropertyGetterNonStatic;
  24332. begin
  24333. StartProgram(false);
  24334. Add([
  24335. '{$modeswitch typehelpers}',
  24336. 'type',
  24337. ' TObject = class',
  24338. ' class var FField: word;',
  24339. ' class function GetField: word;',
  24340. ' class property Field: word read GetField write FField;',
  24341. ' end;',
  24342. ' TClass = class of TObject;',
  24343. ' THelper = type helper for word',
  24344. ' procedure Fly(n: byte);',
  24345. ' end;',
  24346. 'class function TObject.GetField: word;',
  24347. 'begin',
  24348. ' Field.Fly(1);',
  24349. ' Self.Field.Fly(5);',
  24350. ' with Self do Field.Fly(6);',
  24351. ' with Self.Field do Fly(7);',
  24352. 'end;',
  24353. 'procedure THelper.Fly(n: byte);',
  24354. 'begin',
  24355. 'end;',
  24356. 'var',
  24357. ' o: TObject;',
  24358. ' c: TClass;',
  24359. 'begin',
  24360. ' o.Field.Fly(11);',
  24361. ' with o do Field.Fly(12);',
  24362. ' with o.Field do Fly(13);',
  24363. ' c.Field.Fly(14);',
  24364. ' with c do Field.Fly(15);',
  24365. ' with c.Field do Fly(16);',
  24366. '']);
  24367. ConvertProgram;
  24368. CheckSource('TestTypeHelper_PassClassPropertyGetterNonStatic',
  24369. LinesToStr([ // statements
  24370. 'rtl.createClass(this, "TObject", null, function () {',
  24371. ' this.FField = 0;',
  24372. ' this.$init = function () {',
  24373. ' };',
  24374. ' this.$final = function () {',
  24375. ' };',
  24376. ' this.GetField = function () {',
  24377. ' var Result = 0;',
  24378. ' $mod.THelper.Fly.call({',
  24379. ' p: this.GetField(),',
  24380. ' get: function () {',
  24381. ' return this.p;',
  24382. ' },',
  24383. ' set: function (v) {',
  24384. ' this.p = v;',
  24385. ' }',
  24386. ' }, 1);',
  24387. ' $mod.THelper.Fly.call({',
  24388. ' p: this.GetField(),',
  24389. ' get: function () {',
  24390. ' return this.p;',
  24391. ' },',
  24392. ' set: function (v) {',
  24393. ' this.p = v;',
  24394. ' }',
  24395. ' }, 5);',
  24396. ' $mod.THelper.Fly.call({',
  24397. ' p: this.GetField(),',
  24398. ' get: function () {',
  24399. ' return this.p;',
  24400. ' },',
  24401. ' set: function (v) {',
  24402. ' this.p = v;',
  24403. ' }',
  24404. ' }, 6);',
  24405. ' var $with = this.GetField();',
  24406. ' $mod.THelper.Fly.call({',
  24407. ' get: function () {',
  24408. ' return $with;',
  24409. ' },',
  24410. ' set: function (v) {',
  24411. ' $with = v;',
  24412. ' }',
  24413. ' }, 7);',
  24414. ' return Result;',
  24415. ' };',
  24416. '});',
  24417. 'rtl.createHelper(this, "THelper", null, function () {',
  24418. ' this.Fly = function (n) {',
  24419. ' };',
  24420. '});',
  24421. 'this.o = null;',
  24422. 'this.c = null;',
  24423. '']),
  24424. LinesToStr([ // $mod.$main
  24425. '$mod.THelper.Fly.call({',
  24426. ' p: $mod.o.$class.GetField(),',
  24427. ' get: function () {',
  24428. ' return this.p;',
  24429. ' },',
  24430. ' set: function (v) {',
  24431. ' this.p = v;',
  24432. ' }',
  24433. '}, 11);',
  24434. 'var $with = $mod.o;',
  24435. '$mod.THelper.Fly.call({',
  24436. ' p: $with.$class.GetField(),',
  24437. ' get: function () {',
  24438. ' return this.p;',
  24439. ' },',
  24440. ' set: function (v) {',
  24441. ' this.p = v;',
  24442. ' }',
  24443. '}, 12);',
  24444. 'var $with1 = $mod.o.$class.GetField();',
  24445. '$mod.THelper.Fly.call({',
  24446. ' get: function () {',
  24447. ' return $with1;',
  24448. ' },',
  24449. ' set: function (v) {',
  24450. ' $with1 = v;',
  24451. ' }',
  24452. '}, 13);',
  24453. '$mod.THelper.Fly.call({',
  24454. ' p: $mod.c.GetField(),',
  24455. ' get: function () {',
  24456. ' return this.p;',
  24457. ' },',
  24458. ' set: function (v) {',
  24459. ' this.p = v;',
  24460. ' }',
  24461. '}, 14);',
  24462. 'var $with2 = $mod.c;',
  24463. '$mod.THelper.Fly.call({',
  24464. ' p: $with2.GetField(),',
  24465. ' get: function () {',
  24466. ' return this.p;',
  24467. ' },',
  24468. ' set: function (v) {',
  24469. ' this.p = v;',
  24470. ' }',
  24471. '}, 15);',
  24472. 'var $with3 = $mod.c.GetField();',
  24473. '$mod.THelper.Fly.call({',
  24474. ' get: function () {',
  24475. ' return $with3;',
  24476. ' },',
  24477. ' set: function (v) {',
  24478. ' $with3 = v;',
  24479. ' }',
  24480. '}, 16);',
  24481. '']));
  24482. end;
  24483. procedure TTestModule.TestTypeHelper_Property;
  24484. begin
  24485. StartProgram(false);
  24486. Add([
  24487. '{$modeswitch typehelpers}',
  24488. 'type',
  24489. ' THelper = type helper for word',
  24490. ' function GetSize: longint;',
  24491. ' procedure SetSize(Value: longint);',
  24492. ' property Size: longint read GetSize write SetSize;',
  24493. ' end;',
  24494. 'function THelper.GetSize: longint;',
  24495. 'begin',
  24496. ' Result:=Size+1;',
  24497. ' Size:=2;',
  24498. ' Result:=Self.Size+3;',
  24499. ' Self.Size:=4;',
  24500. ' with Self do begin',
  24501. ' Result:=Size+5;',
  24502. ' Size:=6;',
  24503. ' end;',
  24504. 'end;',
  24505. 'procedure THelper.SetSize(Value: longint);',
  24506. 'begin',
  24507. 'end;',
  24508. 'var w: word;',
  24509. 'begin',
  24510. ' w:=w.Size+7;',
  24511. ' w.Size:=w+8;',
  24512. ' with w do begin',
  24513. ' w:=Size+9;',
  24514. ' Size:=w+10;',
  24515. ' end;',
  24516. '']);
  24517. ConvertProgram;
  24518. CheckSource('TestTypeHelper_Property',
  24519. LinesToStr([ // statements
  24520. 'rtl.createHelper(this, "THelper", null, function () {',
  24521. ' this.GetSize = function () {',
  24522. ' var Result = 0;',
  24523. ' Result = $mod.THelper.GetSize.call(this) + 1;',
  24524. ' $mod.THelper.SetSize.call(this, 2);',
  24525. ' Result = $mod.THelper.GetSize.call(this) + 3;',
  24526. ' $mod.THelper.SetSize.call(this, 4);',
  24527. ' var $with = this.get();',
  24528. ' Result = $mod.THelper.GetSize.call(this) + 5;',
  24529. ' $mod.THelper.SetSize.call(this, 6);',
  24530. ' return Result;',
  24531. ' };',
  24532. ' this.SetSize = function (Value) {',
  24533. ' };',
  24534. '});',
  24535. 'this.w = 0;',
  24536. '']),
  24537. LinesToStr([ // $mod.$main
  24538. '$mod.w = $mod.THelper.GetSize.call({',
  24539. ' p: $mod,',
  24540. ' get: function () {',
  24541. ' return this.p.w;',
  24542. ' },',
  24543. ' set: function (v) {',
  24544. ' this.p.w = v;',
  24545. ' }',
  24546. '}) + 7;',
  24547. '$mod.THelper.SetSize.call({',
  24548. ' p: $mod,',
  24549. ' get: function () {',
  24550. ' return this.p.w;',
  24551. ' },',
  24552. ' set: function (v) {',
  24553. ' this.p.w = v;',
  24554. ' }',
  24555. '}, $mod.w + 8);',
  24556. 'var $with = $mod.w;',
  24557. '$mod.w = $mod.THelper.GetSize.call({',
  24558. ' get: function () {',
  24559. ' return $with;',
  24560. ' },',
  24561. ' set: function (v) {',
  24562. ' $with = v;',
  24563. ' }',
  24564. '}) + 9;',
  24565. '$mod.THelper.SetSize.call({',
  24566. ' get: function () {',
  24567. ' return $with;',
  24568. ' },',
  24569. ' set: function (v) {',
  24570. ' $with = v;',
  24571. ' }',
  24572. '}, $mod.w + 10);',
  24573. '']));
  24574. end;
  24575. procedure TTestModule.TestTypeHelper_Property_Array;
  24576. begin
  24577. StartProgram(false);
  24578. Add([
  24579. '{$modeswitch typehelpers}',
  24580. 'type',
  24581. ' THelper = type helper for word',
  24582. ' function GetItems(Index: byte): boolean;',
  24583. ' procedure SetItems(Index: byte; Value: boolean);',
  24584. ' property Items[Index: byte]: boolean read GetItems write SetItems;',
  24585. ' end;',
  24586. 'function THelper.GetItems(Index: byte): boolean;',
  24587. 'begin',
  24588. ' Result:=Items[1];',
  24589. ' Items[2]:=false;',
  24590. ' Result:=Self.Items[3];',
  24591. ' Self.Items[4]:=true;',
  24592. ' with Self do begin',
  24593. ' Result:=Items[5];',
  24594. ' Items[6]:=false;',
  24595. ' end;',
  24596. 'end;',
  24597. 'procedure THelper.SetItems(Index: byte; Value: boolean);',
  24598. 'begin',
  24599. 'end;',
  24600. 'var',
  24601. ' w: word;',
  24602. ' b: boolean;',
  24603. 'begin',
  24604. ' b:=w.Items[1];',
  24605. ' w.Items[2]:=b;',
  24606. ' with w do begin',
  24607. ' b:=Items[3];',
  24608. ' Items[4]:=b;',
  24609. ' end;',
  24610. '']);
  24611. ConvertProgram;
  24612. CheckSource('TestTypeHelper_Property_Array',
  24613. LinesToStr([ // statements
  24614. 'rtl.createHelper(this, "THelper", null, function () {',
  24615. ' this.GetItems = function (Index) {',
  24616. ' var Result = false;',
  24617. ' Result = $mod.THelper.GetItems.call(this, 1);',
  24618. ' $mod.THelper.SetItems.call(this, 2, false);',
  24619. ' Result = $mod.THelper.GetItems.call(this, 3);',
  24620. ' $mod.THelper.SetItems.call(this, 4, true);',
  24621. ' var $with = this.get();',
  24622. ' Result = $mod.THelper.GetItems.call(this, 5);',
  24623. ' $mod.THelper.SetItems.call(this, 6, false);',
  24624. ' return Result;',
  24625. ' };',
  24626. ' this.SetItems = function (Index, Value) {',
  24627. ' };',
  24628. '});',
  24629. 'this.w = 0;',
  24630. 'this.b = false;',
  24631. '']),
  24632. LinesToStr([ // $mod.$main
  24633. '$mod.b = $mod.THelper.GetItems.call({',
  24634. ' p: $mod,',
  24635. ' get: function () {',
  24636. ' return this.p.w;',
  24637. ' },',
  24638. ' set: function (v) {',
  24639. ' this.p.w = v;',
  24640. ' }',
  24641. '}, 1);',
  24642. '$mod.THelper.SetItems.call({',
  24643. ' p: $mod,',
  24644. ' get: function () {',
  24645. ' return this.p.w;',
  24646. ' },',
  24647. ' set: function (v) {',
  24648. ' this.p.w = v;',
  24649. ' }',
  24650. '}, 2, $mod.b);',
  24651. 'var $with = $mod.w;',
  24652. '$mod.b = $mod.THelper.GetItems.call({',
  24653. ' get: function () {',
  24654. ' return $with;',
  24655. ' },',
  24656. ' set: function (v) {',
  24657. ' $with = v;',
  24658. ' }',
  24659. '}, 3);',
  24660. '$mod.THelper.SetItems.call({',
  24661. ' get: function () {',
  24662. ' return $with;',
  24663. ' },',
  24664. ' set: function (v) {',
  24665. ' $with = v;',
  24666. ' }',
  24667. '}, 4, $mod.b);',
  24668. '']));
  24669. end;
  24670. procedure TTestModule.TestTypeHelper_ClassProperty;
  24671. begin
  24672. StartProgram(false);
  24673. Add([
  24674. '{$modeswitch typehelpers}',
  24675. 'type',
  24676. ' THelper = type helper for word',
  24677. ' class function GetSize: longint; static;',
  24678. ' class procedure SetSize(Value: longint); static;',
  24679. ' class property Size: longint read GetSize write SetSize;',
  24680. ' end;',
  24681. 'class function THelper.GetSize: longint;',
  24682. 'begin',
  24683. ' Result:=Size+1;',
  24684. ' Size:=2;',
  24685. 'end;',
  24686. 'class procedure THelper.SetSize(Value: longint);',
  24687. 'begin',
  24688. 'end;',
  24689. 'begin',
  24690. '']);
  24691. ConvertProgram;
  24692. CheckSource('TestTypeHelper_ClassProperty',
  24693. LinesToStr([ // statements
  24694. 'rtl.createHelper(this, "THelper", null, function () {',
  24695. ' this.GetSize = function () {',
  24696. ' var Result = 0;',
  24697. ' Result = $mod.THelper.GetSize() + 1;',
  24698. ' $mod.THelper.SetSize(2);',
  24699. ' return Result;',
  24700. ' };',
  24701. ' this.SetSize = function (Value) {',
  24702. ' };',
  24703. '});',
  24704. '']),
  24705. LinesToStr([ // $mod.$main
  24706. '']));
  24707. end;
  24708. procedure TTestModule.TestTypeHelper_ClassProperty_Array;
  24709. begin
  24710. StartProgram(false);
  24711. Add([
  24712. '{$modeswitch typehelpers}',
  24713. 'type',
  24714. ' THelper = type helper for word',
  24715. ' class function GetItems(Index: byte): boolean; static;',
  24716. ' class procedure SetItems(Index: byte; Value: boolean); static;',
  24717. ' class property Items[Index: byte]: boolean read GetItems write SetItems;',
  24718. ' end;',
  24719. 'class function THelper.GetItems(Index: byte): boolean;',
  24720. 'begin',
  24721. ' Result:=Items[1];',
  24722. ' Items[2]:=false;',
  24723. 'end;',
  24724. 'class procedure THelper.SetItems(Index: byte; Value: boolean);',
  24725. 'begin',
  24726. 'end;',
  24727. 'var',
  24728. ' w: word;',
  24729. ' b: boolean;',
  24730. 'begin',
  24731. ' b:=w.Items[1];',
  24732. ' w.Items[2]:=b;',
  24733. ' with w do begin',
  24734. ' b:=Items[3];',
  24735. ' Items[4]:=b;',
  24736. ' end;',
  24737. '']);
  24738. ConvertProgram;
  24739. CheckSource('TestTypeHelper_ClassProperty_Array',
  24740. LinesToStr([ // statements
  24741. 'rtl.createHelper(this, "THelper", null, function () {',
  24742. ' this.GetItems = function (Index) {',
  24743. ' var Result = false;',
  24744. ' Result = $mod.THelper.GetItems(1);',
  24745. ' $mod.THelper.SetItems(2, false);',
  24746. ' return Result;',
  24747. ' };',
  24748. ' this.SetItems = function (Index, Value) {',
  24749. ' };',
  24750. '});',
  24751. 'this.w = 0;',
  24752. 'this.b = false;',
  24753. '']),
  24754. LinesToStr([ // $mod.$main
  24755. '$mod.b = $mod.THelper.GetItems(1);',
  24756. '$mod.THelper.SetItems(2, $mod.b);',
  24757. 'var $with = $mod.w;',
  24758. '$mod.b = $mod.THelper.GetItems(3);',
  24759. '$mod.THelper.SetItems(4, $mod.b);',
  24760. '']));
  24761. end;
  24762. procedure TTestModule.TestTypeHelper_ClassMethod;
  24763. begin
  24764. StartProgram(false);
  24765. Add([
  24766. '{$modeswitch typehelpers}',
  24767. 'type',
  24768. ' THelper = type helper for word',
  24769. ' class procedure DoStatic; static;',
  24770. ' end;',
  24771. 'class procedure THelper.DoStatic;',
  24772. 'begin',
  24773. ' DoStatic;',
  24774. ' DoStatic();',
  24775. 'end;',
  24776. 'var w: word;',
  24777. 'begin',
  24778. ' w.DoStatic;',
  24779. ' w.DoStatic();',
  24780. '']);
  24781. ConvertProgram;
  24782. CheckSource('TestTypeHelper_ClassMethod',
  24783. LinesToStr([ // statements
  24784. 'rtl.createHelper(this, "THelper", null, function () {',
  24785. ' this.DoStatic = function () {',
  24786. ' $mod.THelper.DoStatic();',
  24787. ' $mod.THelper.DoStatic();',
  24788. ' };',
  24789. '});',
  24790. 'this.w = 0;',
  24791. '']),
  24792. LinesToStr([ // $mod.$main
  24793. '$mod.THelper.DoStatic();',
  24794. '$mod.THelper.DoStatic();',
  24795. '']));
  24796. end;
  24797. procedure TTestModule.TestTypeHelper_ExtClassMethodFail;
  24798. begin
  24799. StartProgram(false);
  24800. Add([
  24801. '{$modeswitch typehelpers}',
  24802. 'type',
  24803. ' THelper = type helper for word',
  24804. ' procedure Run; external name ''Run'';',
  24805. ' end;',
  24806. 'var w: word;',
  24807. 'begin',
  24808. ' w.Run;',
  24809. '']);
  24810. SetExpectedPasResolverError('Not supported: external method in type helper',nNotSupportedX);
  24811. ConvertProgram;
  24812. end;
  24813. procedure TTestModule.TestTypeHelper_Constructor;
  24814. begin
  24815. StartProgram(false);
  24816. Add([
  24817. '{$modeswitch typehelpers}',
  24818. 'type',
  24819. ' THelper = type helper for word',
  24820. ' constructor Init(e: longint);',
  24821. ' end;',
  24822. 'constructor THelper.Init(e: longint);',
  24823. 'begin',
  24824. ' Self:=e;',
  24825. ' Init(e+1);',
  24826. 'end;',
  24827. 'var w: word;',
  24828. 'begin',
  24829. ' w:=word.Init(2);',
  24830. ' w:=w.Init(3);',
  24831. ' with word do w:=Init(4);',
  24832. ' with w do w:=Init(5);',
  24833. '']);
  24834. ConvertProgram;
  24835. CheckSource('TestTypeHelper_Constructor',
  24836. LinesToStr([ // statements
  24837. 'rtl.createHelper(this, "THelper", null, function () {',
  24838. ' this.Init = function (e) {',
  24839. ' this.set(e);',
  24840. ' $mod.THelper.Init.call(this, e + 1);',
  24841. ' return this.get();',
  24842. ' };',
  24843. ' this.$new = function (fn, args) {',
  24844. ' return this[fn].apply({',
  24845. ' p: 0,',
  24846. ' get: function () {',
  24847. ' return this.p;',
  24848. ' },',
  24849. ' set: function (v) {',
  24850. ' this.p = v;',
  24851. ' }',
  24852. ' }, args);',
  24853. ' };',
  24854. '});',
  24855. 'this.w = 0;',
  24856. '']),
  24857. LinesToStr([ // $mod.$main
  24858. '$mod.w = $mod.THelper.$new("Init", [2]);',
  24859. '$mod.w = $mod.THelper.Init.call({',
  24860. ' p: $mod,',
  24861. ' get: function () {',
  24862. ' return this.p.w;',
  24863. ' },',
  24864. ' set: function (v) {',
  24865. ' this.p.w = v;',
  24866. ' }',
  24867. '}, 3);',
  24868. '$mod.w = $mod.THelper.$new("Init", [4]);',
  24869. 'var $with = $mod.w;',
  24870. '$mod.w = $mod.THelper.Init.call({',
  24871. ' get: function () {',
  24872. ' return $with;',
  24873. ' },',
  24874. ' set: function (v) {',
  24875. ' $with = v;',
  24876. ' }',
  24877. '}, 5);',
  24878. '']));
  24879. end;
  24880. procedure TTestModule.TestTypeHelper_Word;
  24881. begin
  24882. StartProgram(false);
  24883. Add([
  24884. '{$modeswitch typehelpers}',
  24885. 'type',
  24886. ' THelper = type helper for word',
  24887. ' procedure DoIt(e: byte = 123);',
  24888. ' end;',
  24889. 'procedure THelper.DoIt(e: byte);',
  24890. 'begin',
  24891. ' Self:=e;',
  24892. ' Self:=Self+1;',
  24893. ' with Self do Doit;',
  24894. 'end;',
  24895. 'begin',
  24896. ' word(3).DoIt;',
  24897. '']);
  24898. ConvertProgram;
  24899. CheckSource('TestTypeHelper_Word',
  24900. LinesToStr([ // statements
  24901. 'rtl.createHelper(this, "THelper", null, function () {',
  24902. ' this.DoIt = function (e) {',
  24903. ' this.set(e);',
  24904. ' this.set(this.get() + 1);',
  24905. ' var $with = this.get();',
  24906. ' $mod.THelper.DoIt.call(this, 123);',
  24907. ' };',
  24908. '});',
  24909. '']),
  24910. LinesToStr([ // $mod.$main
  24911. '$mod.THelper.DoIt.call({',
  24912. ' get: function () {',
  24913. ' return 3;',
  24914. ' },',
  24915. ' set: function (v) {',
  24916. ' rtl.raiseE("EPropReadOnly");',
  24917. ' }',
  24918. '}, 123);',
  24919. '']));
  24920. end;
  24921. procedure TTestModule.TestTypeHelper_Boolean;
  24922. begin
  24923. StartProgram(false);
  24924. Add([
  24925. '{$modeswitch typehelpers}',
  24926. 'type',
  24927. ' Integer = longint;',
  24928. ' THelper = type helper for boolean',
  24929. ' procedure Run(e: wordbool = true);',
  24930. ' end;',
  24931. 'procedure THelper.Run(e: wordbool);',
  24932. 'begin',
  24933. ' Self:=e;',
  24934. ' Self:=not Self;',
  24935. ' with Self do Run;',
  24936. ' if Integer(Self)=0 then ;',
  24937. 'end;',
  24938. 'begin',
  24939. ' boolean(3).Run;',
  24940. '']);
  24941. ConvertProgram;
  24942. CheckSource('TestTypeHelper_Boolean',
  24943. LinesToStr([ // statements
  24944. 'rtl.createHelper(this, "THelper", null, function () {',
  24945. ' this.Run = function (e) {',
  24946. ' this.set(e);',
  24947. ' this.set(!this.get());',
  24948. ' var $with = this.get();',
  24949. ' $mod.THelper.Run.call(this, true);',
  24950. ' if ((this.get() ? 1 : 0) === 0) ;',
  24951. ' };',
  24952. '});',
  24953. '']),
  24954. LinesToStr([ // $mod.$main
  24955. '$mod.THelper.Run.call({',
  24956. ' a: 3 != 0,',
  24957. ' get: function () {',
  24958. ' return this.a;',
  24959. ' },',
  24960. ' set: function (v) {',
  24961. ' rtl.raiseE("EPropReadOnly");',
  24962. ' }',
  24963. '}, true);',
  24964. '']));
  24965. end;
  24966. procedure TTestModule.TestTypeHelper_WordBool;
  24967. begin
  24968. StartProgram(false);
  24969. Add([
  24970. '{$modeswitch typehelpers}',
  24971. 'type',
  24972. ' Integer = longint;',
  24973. ' THelper = type helper for WordBool',
  24974. ' procedure Run(e: wordbool = true);',
  24975. ' end;',
  24976. 'procedure THelper.Run(e: wordbool);',
  24977. 'var i: integer;',
  24978. 'begin',
  24979. ' i:=Integer(Self);',
  24980. 'end;',
  24981. 'var w: wordbool;',
  24982. 'begin',
  24983. ' w.Run;',
  24984. ' wordbool(3).Run;',
  24985. '']);
  24986. ConvertProgram;
  24987. CheckSource('TestTypeHelper_WordBool',
  24988. LinesToStr([ // statements
  24989. 'rtl.createHelper(this, "THelper", null, function () {',
  24990. ' this.Run = function (e) {',
  24991. ' var i = 0;',
  24992. ' i = (this.get() ? 1 : 0);',
  24993. ' };',
  24994. '});',
  24995. 'this.w = false;',
  24996. '']),
  24997. LinesToStr([ // $mod.$main
  24998. '$mod.THelper.Run.call({',
  24999. ' p: $mod,',
  25000. ' get: function () {',
  25001. ' return this.p.w;',
  25002. ' },',
  25003. ' set: function (v) {',
  25004. ' this.p.w = v;',
  25005. ' }',
  25006. '}, true);',
  25007. '$mod.THelper.Run.call({',
  25008. ' a: 3 != 0,',
  25009. ' get: function () {',
  25010. ' return this.a;',
  25011. ' },',
  25012. ' set: function (v) {',
  25013. ' rtl.raiseE("EPropReadOnly");',
  25014. ' }',
  25015. '}, true);',
  25016. '']));
  25017. end;
  25018. procedure TTestModule.TestTypeHelper_Double;
  25019. begin
  25020. StartProgram(false);
  25021. Add([
  25022. '{$modeswitch typehelpers}',
  25023. 'type',
  25024. ' Float = type double;',
  25025. ' THelper = type helper for Float',
  25026. ' const NPI = 3.141592;',
  25027. ' function ToStr: String;',
  25028. ' end;',
  25029. 'function THelper.ToStr: String;',
  25030. 'begin',
  25031. 'end;',
  25032. 'procedure DoIt(s: string);',
  25033. 'begin',
  25034. 'end;',
  25035. 'var f: Float;',
  25036. 'begin',
  25037. ' DoIt(f.toStr);',
  25038. ' DoIt(f.toStr());',
  25039. ' (f*f).toStr;',
  25040. ' DoIt((f*f).toStr);',
  25041. '']);
  25042. ConvertProgram;
  25043. CheckSource('TestTypeHelper_Double',
  25044. LinesToStr([ // statements
  25045. 'rtl.createHelper(this, "THelper", null, function () {',
  25046. ' this.NPI = 3.141592;',
  25047. ' this.ToStr = function () {',
  25048. ' var Result = "";',
  25049. ' return Result;',
  25050. ' };',
  25051. '});',
  25052. 'this.DoIt = function (s) {',
  25053. '};',
  25054. 'this.f = 0.0;',
  25055. '']),
  25056. LinesToStr([ // $mod.$main
  25057. '$mod.DoIt($mod.THelper.ToStr.call({',
  25058. ' p: $mod,',
  25059. ' get: function () {',
  25060. ' return this.p.f;',
  25061. ' },',
  25062. ' set: function (v) {',
  25063. ' this.p.f = v;',
  25064. ' }',
  25065. '}));',
  25066. '$mod.DoIt($mod.THelper.ToStr.call({',
  25067. ' p: $mod,',
  25068. ' get: function () {',
  25069. ' return this.p.f;',
  25070. ' },',
  25071. ' set: function (v) {',
  25072. ' this.p.f = v;',
  25073. ' }',
  25074. '}));',
  25075. '$mod.THelper.ToStr.call({',
  25076. ' a: $mod.f * $mod.f,',
  25077. ' get: function () {',
  25078. ' return this.a;',
  25079. ' },',
  25080. ' set: function (v) {',
  25081. ' rtl.raiseE("EPropReadOnly");',
  25082. ' }',
  25083. '});',
  25084. '$mod.DoIt($mod.THelper.ToStr.call({',
  25085. ' a: $mod.f * $mod.f,',
  25086. ' get: function () {',
  25087. ' return this.a;',
  25088. ' },',
  25089. ' set: function (v) {',
  25090. ' rtl.raiseE("EPropReadOnly");',
  25091. ' }',
  25092. '}));',
  25093. '']));
  25094. end;
  25095. procedure TTestModule.TestTypeHelper_NativeInt;
  25096. begin
  25097. StartProgram(false);
  25098. Add([
  25099. '{$modeswitch typehelpers}',
  25100. 'type',
  25101. ' MaxInt = type nativeint;',
  25102. ' THelperI = type helper for MaxInt',
  25103. ' function ToStr: String;',
  25104. ' end;',
  25105. ' MaxUInt = type nativeuint;',
  25106. ' THelperU = type helper for MaxUInt',
  25107. ' function ToStr: String;',
  25108. ' end;',
  25109. 'function THelperI.ToStr: String;',
  25110. 'begin',
  25111. ' Result:=str(Self);',
  25112. 'end;',
  25113. 'function THelperU.ToStr: String;',
  25114. 'begin',
  25115. ' Result:=str(Self);',
  25116. 'end;',
  25117. 'procedure DoIt(s: string);',
  25118. 'begin',
  25119. 'end;',
  25120. 'var i: MaxInt;',
  25121. 'begin',
  25122. ' DoIt(i.toStr);',
  25123. ' DoIt(i.toStr());',
  25124. ' (i*i).toStr;',
  25125. ' DoIt((i*i).toStr);',
  25126. '']);
  25127. ConvertProgram;
  25128. CheckSource('TestTypeHelper_NativeInt',
  25129. LinesToStr([ // statements
  25130. 'rtl.createHelper(this, "THelperI", null, function () {',
  25131. ' this.ToStr = function () {',
  25132. ' var Result = "";',
  25133. ' Result = "" + this.get();',
  25134. ' return Result;',
  25135. ' };',
  25136. '});',
  25137. 'rtl.createHelper(this, "THelperU", null, function () {',
  25138. ' this.ToStr = function () {',
  25139. ' var Result = "";',
  25140. ' Result = "" + this.get();',
  25141. ' return Result;',
  25142. ' };',
  25143. '});',
  25144. 'this.DoIt = function (s) {',
  25145. '};',
  25146. 'this.i = 0;',
  25147. '']),
  25148. LinesToStr([ // $mod.$main
  25149. '$mod.DoIt($mod.THelperI.ToStr.call({',
  25150. ' p: $mod,',
  25151. ' get: function () {',
  25152. ' return this.p.i;',
  25153. ' },',
  25154. ' set: function (v) {',
  25155. ' this.p.i = v;',
  25156. ' }',
  25157. '}));',
  25158. '$mod.DoIt($mod.THelperI.ToStr.call({',
  25159. ' p: $mod,',
  25160. ' get: function () {',
  25161. ' return this.p.i;',
  25162. ' },',
  25163. ' set: function (v) {',
  25164. ' this.p.i = v;',
  25165. ' }',
  25166. '}));',
  25167. '$mod.THelperI.ToStr.call({',
  25168. ' a: $mod.i * $mod.i,',
  25169. ' get: function () {',
  25170. ' return this.a;',
  25171. ' },',
  25172. ' set: function (v) {',
  25173. ' rtl.raiseE("EPropReadOnly");',
  25174. ' }',
  25175. '});',
  25176. '$mod.DoIt($mod.THelperI.ToStr.call({',
  25177. ' a: $mod.i * $mod.i,',
  25178. ' get: function () {',
  25179. ' return this.a;',
  25180. ' },',
  25181. ' set: function (v) {',
  25182. ' rtl.raiseE("EPropReadOnly");',
  25183. ' }',
  25184. '}));',
  25185. '']));
  25186. end;
  25187. procedure TTestModule.TestTypeHelper_StringChar;
  25188. begin
  25189. StartProgram(false);
  25190. Add([
  25191. '{$modeswitch typehelpers}',
  25192. 'type',
  25193. ' TStringHelper = type helper for string',
  25194. ' procedure DoIt(e: byte = 123);',
  25195. ' end;',
  25196. ' TCharHelper = type helper for char',
  25197. ' procedure Fly;',
  25198. ' end;',
  25199. 'procedure TStringHelper.DoIt(e: byte);',
  25200. 'begin',
  25201. ' Self[1]:=''c'';',
  25202. ' Self[2]:=Self[3];',
  25203. 'end;',
  25204. 'procedure TCharHelper.Fly;',
  25205. 'begin',
  25206. ' Self:=''c'';',
  25207. 'end;',
  25208. 'begin',
  25209. ' ''abc''.DoIt;',
  25210. ' ''xyz''.DoIt();',
  25211. ' ''c''.Fly();',
  25212. '']);
  25213. ConvertProgram;
  25214. CheckSource('TestTypeHelper_StringChar',
  25215. LinesToStr([ // statements
  25216. 'rtl.createHelper(this, "TStringHelper", null, function () {',
  25217. ' this.DoIt = function (e) {',
  25218. ' this.set(rtl.setCharAt(this.get(), 0, "c"));',
  25219. ' this.set(rtl.setCharAt(this.get(), 1, this.get().charAt(2)));',
  25220. ' };',
  25221. '});',
  25222. 'rtl.createHelper(this, "TCharHelper", null, function () {',
  25223. ' this.Fly = function () {',
  25224. ' this.set("c");',
  25225. ' };',
  25226. '});',
  25227. '']),
  25228. LinesToStr([ // $mod.$main
  25229. '$mod.TStringHelper.DoIt.call({',
  25230. ' get: function () {',
  25231. ' return "abc";',
  25232. ' },',
  25233. ' set: function (v) {',
  25234. ' rtl.raiseE("EPropReadOnly");',
  25235. ' }',
  25236. '}, 123);',
  25237. '$mod.TStringHelper.DoIt.call({',
  25238. ' get: function () {',
  25239. ' return "xyz";',
  25240. ' },',
  25241. ' set: function (v) {',
  25242. ' rtl.raiseE("EPropReadOnly");',
  25243. ' }',
  25244. '}, 123);',
  25245. '$mod.TCharHelper.Fly.call({',
  25246. ' get: function () {',
  25247. ' return "c";',
  25248. ' },',
  25249. ' set: function (v) {',
  25250. ' rtl.raiseE("EPropReadOnly");',
  25251. ' }',
  25252. '});',
  25253. '']));
  25254. end;
  25255. procedure TTestModule.TestTypeHelper_JSValue;
  25256. begin
  25257. StartProgram(false);
  25258. Add([
  25259. '{$modeswitch typehelpers}',
  25260. 'type',
  25261. ' TExtValue = type jsvalue;',
  25262. ' THelper = type helper for TExtValue',
  25263. ' function ToStr: String;',
  25264. ' end;',
  25265. 'function THelper.ToStr: String;',
  25266. 'begin',
  25267. 'end;',
  25268. 'var',
  25269. ' s: string;',
  25270. ' v: TExtValue;',
  25271. 'begin',
  25272. ' s:=v.toStr;',
  25273. ' s:=v.toStr();',
  25274. ' TExtValue(s).toStr;',
  25275. '']);
  25276. ConvertProgram;
  25277. CheckSource('TestTypeHelper_JSValue',
  25278. LinesToStr([ // statements
  25279. 'rtl.createHelper(this, "THelper", null, function () {',
  25280. ' this.ToStr = function () {',
  25281. ' var Result = "";',
  25282. ' return Result;',
  25283. ' };',
  25284. '});',
  25285. 'this.s = "";',
  25286. 'this.v = undefined;',
  25287. '']),
  25288. LinesToStr([ // $mod.$main
  25289. '$mod.s = $mod.THelper.ToStr.call({',
  25290. ' p: $mod,',
  25291. ' get: function () {',
  25292. ' return this.p.v;',
  25293. ' },',
  25294. ' set: function (v) {',
  25295. ' this.p.v = v;',
  25296. ' }',
  25297. '});',
  25298. '$mod.s = $mod.THelper.ToStr.call({',
  25299. ' p: $mod,',
  25300. ' get: function () {',
  25301. ' return this.p.v;',
  25302. ' },',
  25303. ' set: function (v) {',
  25304. ' this.p.v = v;',
  25305. ' }',
  25306. '});',
  25307. '$mod.THelper.ToStr.call({',
  25308. ' p: $mod,',
  25309. ' get: function () {',
  25310. ' return this.p.s;',
  25311. ' },',
  25312. ' set: function (v) {',
  25313. ' rtl.raiseE("EPropReadOnly");',
  25314. ' }',
  25315. '});',
  25316. '']));
  25317. end;
  25318. procedure TTestModule.TestTypeHelper_Array;
  25319. begin
  25320. StartProgram(false);
  25321. Add([
  25322. '{$modeswitch typehelpers}',
  25323. 'type',
  25324. ' TArrOfBool = array of boolean;',
  25325. ' TArrOfJS = array of jsvalue;',
  25326. ' THelper = type helper for TArrOfBool',
  25327. ' procedure DoIt(e: byte = 123);',
  25328. ' end;',
  25329. 'procedure THelper.DoIt(e: byte);',
  25330. 'begin',
  25331. ' Self[1]:=true;',
  25332. ' Self[2]:=not Self[3];',
  25333. ' SetLength(Self,4);',
  25334. 'end;',
  25335. 'var',
  25336. ' b: TArrOfBool;',
  25337. ' j: TArrOfJS;',
  25338. 'begin',
  25339. ' b.DoIt;',
  25340. ' TArrOfBool(j).DoIt();',
  25341. '']);
  25342. ConvertProgram;
  25343. CheckSource('TestTypeHelper_Array',
  25344. LinesToStr([ // statements
  25345. 'rtl.createHelper(this, "THelper", null, function () {',
  25346. ' this.DoIt = function (e) {',
  25347. ' this.get()[1] = true;',
  25348. ' this.get()[2] = !this.get()[3];',
  25349. ' this.set(rtl.arraySetLength(this.get(), false, 4));',
  25350. ' };',
  25351. '});',
  25352. 'this.b = [];',
  25353. 'this.j = [];',
  25354. '']),
  25355. LinesToStr([ // $mod.$main
  25356. '$mod.THelper.DoIt.call({',
  25357. ' p: $mod,',
  25358. ' get: function () {',
  25359. ' return this.p.b;',
  25360. ' },',
  25361. ' set: function (v) {',
  25362. ' this.p.b = v;',
  25363. ' }',
  25364. '}, 123);',
  25365. '$mod.THelper.DoIt.call({',
  25366. ' p: $mod,',
  25367. ' get: function () {',
  25368. ' return this.p.j;',
  25369. ' },',
  25370. ' set: function (v) {',
  25371. ' this.p.j = v;',
  25372. ' }',
  25373. '}, 123);',
  25374. '']));
  25375. end;
  25376. procedure TTestModule.TestTypeHelper_EnumType;
  25377. begin
  25378. StartProgram(false);
  25379. Add([
  25380. '{$modeswitch typehelpers}',
  25381. 'type',
  25382. ' TEnum = (red,blue);',
  25383. ' THelper = type helper for TEnum',
  25384. ' procedure DoIt(e: byte = 123);',
  25385. ' class procedure Swing(w: word); static;',
  25386. ' end;',
  25387. 'procedure THelper.DoIt(e: byte);',
  25388. 'begin',
  25389. ' Self:=red;',
  25390. ' Self:=succ(Self);',
  25391. ' with Self do Doit;',
  25392. 'end;',
  25393. 'class procedure THelper.Swing(w: word);',
  25394. 'begin',
  25395. 'end;',
  25396. 'var e: TEnum;',
  25397. 'begin',
  25398. ' e.DoIt;',
  25399. ' red.DoIt;',
  25400. ' TEnum.blue.DoIt;',
  25401. ' TEnum(1).DoIt;',
  25402. ' TEnum.Swing(3);',
  25403. '']);
  25404. ConvertProgram;
  25405. CheckSource('TestTypeHelper_EnumType',
  25406. LinesToStr([ // statements
  25407. 'this.TEnum = {',
  25408. ' "0": "red",',
  25409. ' red: 0,',
  25410. ' "1": "blue",',
  25411. ' blue: 1',
  25412. '};',
  25413. 'rtl.createHelper(this, "THelper", null, function () {',
  25414. ' this.DoIt = function (e) {',
  25415. ' this.set($mod.TEnum.red);',
  25416. ' this.set(this.get() + 1);',
  25417. ' var $with = this.get();',
  25418. ' $mod.THelper.DoIt.call(this, 123);',
  25419. ' };',
  25420. ' this.Swing = function (w) {',
  25421. ' };',
  25422. '});',
  25423. 'this.e = 0;',
  25424. '']),
  25425. LinesToStr([ // $mod.$main
  25426. '$mod.THelper.DoIt.call({',
  25427. ' p: $mod,',
  25428. ' get: function () {',
  25429. ' return this.p.e;',
  25430. ' },',
  25431. ' set: function (v) {',
  25432. ' this.p.e = v;',
  25433. ' }',
  25434. '}, 123);',
  25435. '$mod.THelper.DoIt.call({',
  25436. ' p: $mod.TEnum,',
  25437. ' get: function () {',
  25438. ' return this.p.red;',
  25439. ' },',
  25440. ' set: function (v) {',
  25441. ' rtl.raiseE("EPropReadOnly");',
  25442. ' }',
  25443. '}, 123);',
  25444. '$mod.THelper.DoIt.call({',
  25445. ' p: $mod.TEnum,',
  25446. ' get: function () {',
  25447. ' return this.p.blue;',
  25448. ' },',
  25449. ' set: function (v) {',
  25450. ' rtl.raiseE("EPropReadOnly");',
  25451. ' }',
  25452. '}, 123);',
  25453. '$mod.THelper.DoIt.call({',
  25454. ' get: function () {',
  25455. ' return 1;',
  25456. ' },',
  25457. ' set: function (v) {',
  25458. ' rtl.raiseE("EPropReadOnly");',
  25459. ' }',
  25460. '}, 123);',
  25461. '$mod.THelper.Swing(3);',
  25462. '']));
  25463. end;
  25464. procedure TTestModule.TestTypeHelper_SetType;
  25465. begin
  25466. StartProgram(false);
  25467. Add([
  25468. '{$modeswitch typehelpers}',
  25469. 'type',
  25470. ' TEnum = (red,blue);',
  25471. ' TSetOfEnum = set of TEnum;',
  25472. ' THelper = type helper for TSetOfEnum',
  25473. ' procedure DoIt(e: byte = 123);',
  25474. ' constructor Init(e: TEnum);',
  25475. ' constructor InitEmpty;',
  25476. ' end;',
  25477. 'procedure THelper.DoIt(e: byte);',
  25478. 'begin',
  25479. ' Self:=[];',
  25480. ' Self:=[red];',
  25481. ' Include(Self,blue);',
  25482. 'end;',
  25483. 'constructor THelper.Init(e: TEnum);',
  25484. 'begin',
  25485. ' Self:=[];',
  25486. ' Self:=[e];',
  25487. ' Include(Self,blue);',
  25488. 'end;',
  25489. 'constructor THelper.InitEmpty;',
  25490. 'begin',
  25491. 'end;',
  25492. 'var s: TSetOfEnum;',
  25493. 'begin',
  25494. ' s.DoIt;',
  25495. //' [red].DoIt;',
  25496. //' with s do DoIt;',
  25497. //' with [red,blue] do DoIt;',
  25498. ' s:=TSetOfEnum.Init(blue);',
  25499. ' s:=s.Init(blue);',
  25500. '']);
  25501. ConvertProgram;
  25502. CheckSource('TestTypeHelper_SetType',
  25503. LinesToStr([ // statements
  25504. 'this.TEnum = {',
  25505. ' "0": "red",',
  25506. ' red: 0,',
  25507. ' "1": "blue",',
  25508. ' blue: 1',
  25509. '};',
  25510. 'rtl.createHelper(this, "THelper", null, function () {',
  25511. ' this.DoIt = function (e) {',
  25512. ' this.set({});',
  25513. ' this.set(rtl.createSet($mod.TEnum.red));',
  25514. ' this.set(rtl.includeSet(this.get(), $mod.TEnum.blue));',
  25515. ' };',
  25516. ' this.Init = function (e) {',
  25517. ' this.set({});',
  25518. ' this.set(rtl.createSet(e));',
  25519. ' this.set(rtl.includeSet(this.get(), $mod.TEnum.blue));',
  25520. ' return this.get();',
  25521. ' };',
  25522. ' this.InitEmpty = function () {',
  25523. ' return this.get();',
  25524. ' };',
  25525. ' this.$new = function (fn, args) {',
  25526. ' return this[fn].apply({',
  25527. ' p: {},',
  25528. ' get: function () {',
  25529. ' return this.p;',
  25530. ' },',
  25531. ' set: function (v) {',
  25532. ' this.p = v;',
  25533. ' }',
  25534. ' }, args);',
  25535. ' };',
  25536. '});',
  25537. 'this.s = {};',
  25538. '']),
  25539. LinesToStr([ // $mod.$main
  25540. '$mod.THelper.DoIt.call({',
  25541. ' p: $mod,',
  25542. ' get: function () {',
  25543. ' return this.p.s;',
  25544. ' },',
  25545. ' set: function (v) {',
  25546. ' this.p.s = v;',
  25547. ' }',
  25548. '}, 123);',
  25549. '$mod.s = rtl.refSet($mod.THelper.$new("Init", [$mod.TEnum.blue]));',
  25550. '$mod.s = rtl.refSet($mod.THelper.Init.call({',
  25551. ' p: $mod,',
  25552. ' get: function () {',
  25553. ' return this.p.s;',
  25554. ' },',
  25555. ' set: function (v) {',
  25556. ' this.p.s = v;',
  25557. ' }',
  25558. '}, $mod.TEnum.blue));',
  25559. '']));
  25560. end;
  25561. procedure TTestModule.TestTypeHelper_InterfaceType;
  25562. begin
  25563. StartProgram(false);
  25564. Add([
  25565. '{$interfaces com}',
  25566. '{$modeswitch typehelpers}',
  25567. 'type',
  25568. ' IUnknown = interface',
  25569. ' function _AddRef: longint;',
  25570. ' function _Release: longint;',
  25571. ' end;',
  25572. ' TObject = class(IUnknown)',
  25573. ' function _AddRef: longint; virtual; abstract;',
  25574. ' function _Release: longint; virtual; abstract;',
  25575. ' end;',
  25576. ' THelper = type helper for IUnknown',
  25577. ' procedure Fly(e: byte = 123);',
  25578. ' class procedure Run; static;',
  25579. ' end;',
  25580. 'var',
  25581. ' i: IUnknown;',
  25582. ' o: TObject;',
  25583. 'procedure THelper.Fly(e: byte);',
  25584. 'begin',
  25585. ' i:=Self;',
  25586. ' o:=Self as TObject;',
  25587. ' Self:=nil;',
  25588. ' Self:=i;',
  25589. ' Self:=o;',
  25590. ' with Self do begin',
  25591. ' Fly;',
  25592. ' Fly();',
  25593. ' end;',
  25594. 'end;',
  25595. 'class procedure THelper.Run;',
  25596. 'var l: IUnknown;',
  25597. 'begin',
  25598. ' l.Fly;',
  25599. ' l.Fly();',
  25600. 'end;',
  25601. 'begin',
  25602. ' i.Fly;',
  25603. ' i.Fly();',
  25604. ' i.Run;',
  25605. ' i.Run();',
  25606. ' IUnknown.Run;',
  25607. ' IUnknown.Run();',
  25608. '']);
  25609. ConvertProgram;
  25610. CheckSource('TestTypeHelper_InterfaceType',
  25611. LinesToStr([ // statements
  25612. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  25613. 'rtl.createClass(this, "TObject", null, function () {',
  25614. ' this.$init = function () {',
  25615. ' };',
  25616. ' this.$final = function () {',
  25617. ' };',
  25618. ' rtl.addIntf(this, $mod.IUnknown);',
  25619. '});',
  25620. 'rtl.createHelper(this, "THelper", null, function () {',
  25621. ' this.Fly = function (e) {',
  25622. ' var $ir = rtl.createIntfRefs();',
  25623. ' try {',
  25624. ' rtl.setIntfP($mod, "i", this.get());',
  25625. ' $mod.o = rtl.intfAsClass(this.get(), $mod.TObject);',
  25626. ' this.set(null);',
  25627. ' this.set($mod.i);',
  25628. ' this.set($ir.ref(1, rtl.queryIntfT($mod.o, $mod.IUnknown)));',
  25629. ' var $with = this.get();',
  25630. ' $mod.THelper.Fly.call(this, 123);',
  25631. ' $mod.THelper.Fly.call(this, 123);',
  25632. ' } finally {',
  25633. ' $ir.free();',
  25634. ' };',
  25635. ' };',
  25636. ' this.Run = function () {',
  25637. ' var l = null;',
  25638. ' try {',
  25639. ' $mod.THelper.Fly.call({',
  25640. ' get: function () {',
  25641. ' return l;',
  25642. ' },',
  25643. ' set: function (v) {',
  25644. ' l = rtl.setIntfL(l, v);',
  25645. ' }',
  25646. ' }, 123);',
  25647. ' $mod.THelper.Fly.call({',
  25648. ' get: function () {',
  25649. ' return l;',
  25650. ' },',
  25651. ' set: function (v) {',
  25652. ' l = rtl.setIntfL(l, v);',
  25653. ' }',
  25654. ' }, 123);',
  25655. ' } finally {',
  25656. ' rtl._Release(l);',
  25657. ' };',
  25658. ' };',
  25659. '});',
  25660. 'this.i = null;',
  25661. 'this.o = null;',
  25662. '']),
  25663. LinesToStr([ // $mod.$main
  25664. '$mod.THelper.Fly.call({',
  25665. ' p: $mod,',
  25666. ' get: function () {',
  25667. ' return this.p.i;',
  25668. ' },',
  25669. ' set: function (v) {',
  25670. ' rtl.setIntfP(this.p, "i", v);',
  25671. ' }',
  25672. '}, 123);',
  25673. '$mod.THelper.Fly.call({',
  25674. ' p: $mod,',
  25675. ' get: function () {',
  25676. ' return this.p.i;',
  25677. ' },',
  25678. ' set: function (v) {',
  25679. ' rtl.setIntfP(this.p, "i", v);',
  25680. ' }',
  25681. '}, 123);',
  25682. '$mod.THelper.Run();',
  25683. '$mod.THelper.Run();',
  25684. '$mod.THelper.Run();',
  25685. '$mod.THelper.Run();',
  25686. '']));
  25687. end;
  25688. procedure TTestModule.TestTypeHelper_NestedSelf;
  25689. begin
  25690. StartProgram(false);
  25691. Add([
  25692. '{$modeswitch typehelpers}',
  25693. 'type',
  25694. ' THelper = type helper for string',
  25695. ' procedure Run(Value: string);',
  25696. ' end;',
  25697. 'procedure THelper.Run(Value: string);',
  25698. ' function Sub(i: nativeint): boolean;',
  25699. ' begin',
  25700. ' Result:=Self[i+1]=Value[i];',
  25701. ' end;',
  25702. 'begin',
  25703. ' if Self[3]=Value[4] then ;',
  25704. 'end;',
  25705. 'begin',
  25706. '']);
  25707. ConvertProgram;
  25708. CheckSource('TestTypeHelper_NestedSelf',
  25709. LinesToStr([ // statements
  25710. 'rtl.createHelper(this, "THelper", null, function () {',
  25711. ' this.Run = function (Value) {',
  25712. ' var $Self = this;',
  25713. ' function Sub(i) {',
  25714. ' var Result = false;',
  25715. ' Result = $Self.get().charAt((i + 1) - 1) === Value.charAt(i - 1);',
  25716. ' return Result;',
  25717. ' };',
  25718. ' if ($Self.get().charAt(2) === Value.charAt(3)) ;',
  25719. ' };',
  25720. '});',
  25721. '']),
  25722. LinesToStr([ // $mod.$main
  25723. '']));
  25724. end;
  25725. procedure TTestModule.TestProcType;
  25726. begin
  25727. StartProgram(false);
  25728. Add([
  25729. 'type',
  25730. ' TProcInt = procedure(vI: longint = 1);',
  25731. 'procedure DoIt(vJ: longint);',
  25732. 'begin end;',
  25733. 'var',
  25734. ' b: boolean;',
  25735. ' vP, vQ: tprocint;',
  25736. 'begin',
  25737. ' vp:=nil;',
  25738. ' vp:=vp;',
  25739. ' vp:=@doit;',
  25740. ' vp;',
  25741. ' vp();',
  25742. ' vp(2);',
  25743. ' b:=vp=nil;',
  25744. ' b:=nil=vp;',
  25745. ' b:=vp=vq;',
  25746. ' b:=vp=@doit;',
  25747. ' b:=@doit=vp;',
  25748. ' b:=vp<>nil;',
  25749. ' b:=nil<>vp;',
  25750. ' b:=vp<>vq;',
  25751. ' b:=vp<>@doit;',
  25752. ' b:=@doit<>vp;',
  25753. ' b:=Assigned(vp);',
  25754. ' if Assigned(vp) then ;']);
  25755. ConvertProgram;
  25756. CheckSource('TestProcType',
  25757. LinesToStr([ // statements
  25758. 'this.DoIt = function(vJ) {',
  25759. '};',
  25760. 'this.b = false;',
  25761. 'this.vP = null;',
  25762. 'this.vQ = null;'
  25763. ]),
  25764. LinesToStr([ // $mod.$main
  25765. '$mod.vP = null;',
  25766. '$mod.vP = $mod.vP;',
  25767. '$mod.vP = $mod.DoIt;',
  25768. '$mod.vP(1);',
  25769. '$mod.vP(1);',
  25770. '$mod.vP(2);',
  25771. '$mod.b = $mod.vP === null;',
  25772. '$mod.b = null === $mod.vP;',
  25773. '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
  25774. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  25775. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  25776. '$mod.b = $mod.vP !== null;',
  25777. '$mod.b = null !== $mod.vP;',
  25778. '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
  25779. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  25780. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  25781. '$mod.b = $mod.vP != null;',
  25782. 'if ($mod.vP != null) ;',
  25783. '']));
  25784. end;
  25785. procedure TTestModule.TestProcType_Arg;
  25786. begin
  25787. StartProgram(false);
  25788. Add([
  25789. 'type',
  25790. ' TProcInt = procedure(vI: longint = 1);',
  25791. 'procedure DoIt(vJ: longint); begin end;',
  25792. 'procedure DoSome(vP, vQ: TProcInt);',
  25793. 'var',
  25794. ' b: boolean;',
  25795. 'begin',
  25796. ' vp:=nil;',
  25797. ' vp:=vp;',
  25798. ' vp:=@doit;',
  25799. ' vp;',
  25800. ' vp();',
  25801. ' vp(2);',
  25802. ' b:=vp=nil;',
  25803. ' b:=nil=vp;',
  25804. ' b:=vp=vq;',
  25805. ' b:=vp=@doit;',
  25806. ' b:=@doit=vp;',
  25807. ' b:=vp<>nil;',
  25808. ' b:=nil<>vp;',
  25809. ' b:=vp<>vq;',
  25810. ' b:=vp<>@doit;',
  25811. ' b:=@doit<>vp;',
  25812. ' b:=Assigned(vp);',
  25813. ' if Assigned(vp) then ;',
  25814. 'end;',
  25815. 'begin',
  25816. ' DoSome(@DoIt,nil);']);
  25817. ConvertProgram;
  25818. CheckSource('TestProcType_Arg',
  25819. LinesToStr([ // statements
  25820. 'this.DoIt = function(vJ) {',
  25821. '};',
  25822. 'this.DoSome = function(vP, vQ) {',
  25823. ' var b = false;',
  25824. ' vP = null;',
  25825. ' vP = vP;',
  25826. ' vP = $mod.DoIt;',
  25827. ' vP(1);',
  25828. ' vP(1);',
  25829. ' vP(2);',
  25830. ' b = vP === null;',
  25831. ' b = null === vP;',
  25832. ' b = rtl.eqCallback(vP,vQ);',
  25833. ' b = rtl.eqCallback(vP, $mod.DoIt);',
  25834. ' b = rtl.eqCallback($mod.DoIt, vP);',
  25835. ' b = vP !== null;',
  25836. ' b = null !== vP;',
  25837. ' b = !rtl.eqCallback(vP, vQ);',
  25838. ' b = !rtl.eqCallback(vP, $mod.DoIt);',
  25839. ' b = !rtl.eqCallback($mod.DoIt, vP);',
  25840. ' b = vP != null;',
  25841. ' if (vP != null) ;',
  25842. '};',
  25843. '']),
  25844. LinesToStr([ // $mod.$main
  25845. '$mod.DoSome($mod.DoIt,null);',
  25846. '']));
  25847. end;
  25848. procedure TTestModule.TestProcType_FunctionFPC;
  25849. begin
  25850. StartProgram(false);
  25851. Add('type');
  25852. Add(' TFuncInt = function(vA: longint = 1): longint;');
  25853. Add('function DoIt(vI: longint): longint;');
  25854. Add('begin end;');
  25855. Add('var');
  25856. Add(' b: boolean;');
  25857. Add(' vP, vQ: tfuncint;');
  25858. Add('begin');
  25859. Add(' vp:=nil;');
  25860. Add(' vp:=vp;');
  25861. Add(' vp:=@doit;'); // ok in fpc and delphi
  25862. //Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  25863. Add(' vp;'); // ok in fpc and delphi
  25864. Add(' vp();');
  25865. Add(' vp(2);');
  25866. Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  25867. Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  25868. Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  25869. Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  25870. Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  25871. //Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
  25872. Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
  25873. Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  25874. Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  25875. Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  25876. Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  25877. Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  25878. //Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
  25879. Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
  25880. Add(' b:=Assigned(vp);');
  25881. //Add(' doit(vp);'); // illegal in fpc, ok in delphi
  25882. Add(' doit(vp());'); // ok in fpc and delphi
  25883. Add(' doit(vp(2));'); // ok in fpc and delphi
  25884. ConvertProgram;
  25885. CheckSource('TestProcType_FunctionFPC',
  25886. LinesToStr([ // statements
  25887. 'this.DoIt = function(vI) {',
  25888. ' var Result = 0;',
  25889. ' return Result;',
  25890. '};',
  25891. 'this.b = false;',
  25892. 'this.vP = null;',
  25893. 'this.vQ = null;'
  25894. ]),
  25895. LinesToStr([ // $mod.$main
  25896. '$mod.vP = null;',
  25897. '$mod.vP = $mod.vP;',
  25898. '$mod.vP = $mod.DoIt;',
  25899. '$mod.vP(1);',
  25900. '$mod.vP(1);',
  25901. '$mod.vP(2);',
  25902. '$mod.b = $mod.vP === null;',
  25903. '$mod.b = null === $mod.vP;',
  25904. '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
  25905. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  25906. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  25907. '$mod.b = 4 === $mod.vP(1);',
  25908. '$mod.b = $mod.vP !== null;',
  25909. '$mod.b = null !== $mod.vP;',
  25910. '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
  25911. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  25912. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  25913. '$mod.b = 6 !== $mod.vP(1);',
  25914. '$mod.b = $mod.vP != null;',
  25915. '$mod.DoIt($mod.vP(1));',
  25916. '$mod.DoIt($mod.vP(2));',
  25917. '']));
  25918. end;
  25919. procedure TTestModule.TestProcType_FunctionDelphi;
  25920. begin
  25921. StartProgram(false);
  25922. Add('{$mode Delphi}');
  25923. Add('type');
  25924. Add(' TFuncInt = function(vA: longint = 1): longint;');
  25925. Add('function DoIt(vI: longint): longint;');
  25926. Add('begin end;');
  25927. Add('var');
  25928. Add(' b: boolean;');
  25929. Add(' vP, vQ: tfuncint;');
  25930. Add('begin');
  25931. Add(' vp:=nil;');
  25932. Add(' vp:=vp;');
  25933. Add(' vp:=@doit;'); // ok in fpc and delphi
  25934. Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  25935. Add(' vp;'); // ok in fpc and delphi
  25936. Add(' vp();');
  25937. Add(' vp(2);');
  25938. //Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  25939. //Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  25940. Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  25941. //Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  25942. //Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  25943. Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
  25944. Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
  25945. //Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  25946. //Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  25947. Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  25948. //Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  25949. //Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  25950. Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
  25951. Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
  25952. Add(' b:=Assigned(vp);');
  25953. Add(' doit(vp);'); // illegal in fpc, ok in delphi
  25954. Add(' doit(vp());'); // ok in fpc and delphi
  25955. Add(' doit(vp(2));'); // ok in fpc and delphi *)
  25956. ConvertProgram;
  25957. CheckSource('TestProcType_FunctionDelphi',
  25958. LinesToStr([ // statements
  25959. 'this.DoIt = function(vI) {',
  25960. ' var Result = 0;',
  25961. ' return Result;',
  25962. '};',
  25963. 'this.b = false;',
  25964. 'this.vP = null;',
  25965. 'this.vQ = null;'
  25966. ]),
  25967. LinesToStr([ // $mod.$main
  25968. '$mod.vP = null;',
  25969. '$mod.vP = $mod.vP;',
  25970. '$mod.vP = $mod.DoIt;',
  25971. '$mod.vP = $mod.DoIt;',
  25972. '$mod.vP(1);',
  25973. '$mod.vP(1);',
  25974. '$mod.vP(2);',
  25975. '$mod.b = $mod.vP(1) === $mod.vQ(1);',
  25976. '$mod.b = $mod.vP(1) === 3;',
  25977. '$mod.b = 4 === $mod.vP(1);',
  25978. '$mod.b = $mod.vP(1) !== $mod.vQ(1);',
  25979. '$mod.b = $mod.vP(1) !== 5;',
  25980. '$mod.b = 6 !== $mod.vP(1);',
  25981. '$mod.b = $mod.vP != null;',
  25982. '$mod.DoIt($mod.vP(1));',
  25983. '$mod.DoIt($mod.vP(1));',
  25984. '$mod.DoIt($mod.vP(2));',
  25985. '']));
  25986. end;
  25987. procedure TTestModule.TestProcType_ProcedureDelphi;
  25988. begin
  25989. StartProgram(false);
  25990. Add('{$mode Delphi}');
  25991. Add('type');
  25992. Add(' TProc = procedure;');
  25993. Add('procedure DoIt;');
  25994. Add('begin end;');
  25995. Add('var');
  25996. Add(' b: boolean;');
  25997. Add(' vP, vQ: tproc;');
  25998. Add('begin');
  25999. Add(' vp:=nil;');
  26000. Add(' vp:=vp;');
  26001. Add(' vp:=vq;');
  26002. 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
  26003. Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  26004. //Add(' vp:=@doit;'); // illegal in fpc, ok in delphi (because Delphi treats @F as Pointer), not supported by resolver
  26005. Add(' vp;'); // ok in fpc and delphi
  26006. Add(' vp();');
  26007. // equal
  26008. //Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  26009. Add(' b:=@@vp=nil;'); // ok in fpc delphi mode, ok in delphi
  26010. //Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  26011. Add(' b:=nil=@@vp;'); // ok in fpc delphi mode, ok in delphi
  26012. Add(' b:=@@vp=@@vq;'); // ok in fpc delphi mode, ok in Delphi
  26013. //Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  26014. //Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  26015. Add(' b:=@@vp=@doit;'); // ok in fpc delphi mode, ok in delphi
  26016. //Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  26017. Add(' b:=@doit=@@vp;'); // ok in fpc delphi mode, ok in delphi
  26018. // unequal
  26019. //Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  26020. Add(' b:=@@vp<>nil;'); // ok in fpc mode delphi, ok in delphi
  26021. //Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  26022. Add(' b:=nil<>@@vp;'); // ok in fpc mode delphi, ok in delphi
  26023. //Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  26024. Add(' b:=@@vp<>@@vq;'); // ok in fpc mode delphi, ok in delphi
  26025. //Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  26026. Add(' b:=@@vp<>@doit;'); // ok in fpc mode delphi, illegal in delphi
  26027. //Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  26028. Add(' b:=@doit<>@@vp;'); // ok in fpc mode delphi, illegal in delphi
  26029. Add(' b:=Assigned(vp);');
  26030. ConvertProgram;
  26031. CheckSource('TestProcType_ProcedureDelphi',
  26032. LinesToStr([ // statements
  26033. 'this.DoIt = function() {',
  26034. '};',
  26035. 'this.b = false;',
  26036. 'this.vP = null;',
  26037. 'this.vQ = null;'
  26038. ]),
  26039. LinesToStr([ // $mod.$main
  26040. '$mod.vP = null;',
  26041. '$mod.vP = $mod.vP;',
  26042. '$mod.vP = $mod.vQ;',
  26043. '$mod.vP = $mod.DoIt;',
  26044. '$mod.vP = $mod.DoIt;',
  26045. '$mod.vP();',
  26046. '$mod.vP();',
  26047. '$mod.b = $mod.vP === null;',
  26048. '$mod.b = null === $mod.vP;',
  26049. '$mod.b = rtl.eqCallback($mod.vP, $mod.vQ);',
  26050. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  26051. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  26052. '$mod.b = $mod.vP !== null;',
  26053. '$mod.b = null !== $mod.vP;',
  26054. '$mod.b = !rtl.eqCallback($mod.vP, $mod.vQ);',
  26055. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  26056. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  26057. '$mod.b = $mod.vP != null;',
  26058. '']));
  26059. end;
  26060. procedure TTestModule.TestProcType_AsParam;
  26061. begin
  26062. StartProgram(false);
  26063. Add('type');
  26064. Add(' TFuncInt = function(vA: longint = 1): longint;');
  26065. Add('procedure DoIt(vG: tfuncint; const vH: tfuncint; var vI: tfuncint);');
  26066. Add('var vJ: tfuncint;');
  26067. Add('begin');
  26068. Add(' vg:=vg;');
  26069. Add(' vj:=vh;');
  26070. Add(' vi:=vi;');
  26071. Add(' doit(vg,vg,vg);');
  26072. Add(' doit(vh,vh,vj);');
  26073. Add(' doit(vi,vi,vi);');
  26074. Add(' doit(vj,vj,vj);');
  26075. Add('end;');
  26076. Add('var i: tfuncint;');
  26077. Add('begin');
  26078. Add(' doit(i,i,i);');
  26079. ConvertProgram;
  26080. CheckSource('TestProcType_AsParam',
  26081. LinesToStr([ // statements
  26082. 'this.DoIt = function (vG,vH,vI) {',
  26083. ' var vJ = null;',
  26084. ' vG = vG;',
  26085. ' vJ = vH;',
  26086. ' vI.set(vI.get());',
  26087. ' $mod.DoIt(vG, vG, {',
  26088. ' get: function () {',
  26089. ' return vG;',
  26090. ' },',
  26091. ' set: function (v) {',
  26092. ' vG = v;',
  26093. ' }',
  26094. ' });',
  26095. ' $mod.DoIt(vH, vH, {',
  26096. ' get: function () {',
  26097. ' return vJ;',
  26098. ' },',
  26099. ' set: function (v) {',
  26100. ' vJ = v;',
  26101. ' }',
  26102. ' });',
  26103. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  26104. ' $mod.DoIt(vJ, vJ, {',
  26105. ' get: function () {',
  26106. ' return vJ;',
  26107. ' },',
  26108. ' set: function (v) {',
  26109. ' vJ = v;',
  26110. ' }',
  26111. ' });',
  26112. '};',
  26113. 'this.i = null;'
  26114. ]),
  26115. LinesToStr([
  26116. '$mod.DoIt($mod.i,$mod.i,{',
  26117. ' p: $mod,',
  26118. ' get: function () {',
  26119. ' return this.p.i;',
  26120. ' },',
  26121. ' set: function (v) {',
  26122. ' this.p.i = v;',
  26123. ' }',
  26124. '});'
  26125. ]));
  26126. end;
  26127. procedure TTestModule.TestProcType_MethodFPC;
  26128. begin
  26129. StartProgram(false);
  26130. Add('type');
  26131. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  26132. Add(' TObject = class');
  26133. Add(' function DoIt(vA: longint = 1): longint;');
  26134. Add(' end;');
  26135. Add('function TObject.DoIt(vA: longint = 1): longint;');
  26136. Add('begin');
  26137. Add('end;');
  26138. Add('var');
  26139. Add(' Obj: TObject;');
  26140. Add(' vP: tfuncint;');
  26141. Add(' b: boolean;');
  26142. Add('begin');
  26143. Add(' vp:[email protected];'); // ok in fpc and delphi
  26144. //Add(' vp:=obj.doit;'); // illegal in fpc, ok in delphi
  26145. Add(' vp;'); // ok in fpc and delphi
  26146. Add(' vp();');
  26147. Add(' vp(2);');
  26148. Add(' b:[email protected];'); // ok in fpc, illegal in delphi
  26149. Add(' b:[email protected]=vp;'); // ok in fpc, illegal in delphi
  26150. Add(' b:=vp<>@obj.doit;'); // ok in fpc, illegal in delphi
  26151. Add(' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
  26152. ConvertProgram;
  26153. CheckSource('TestProcType_MethodFPC',
  26154. LinesToStr([ // statements
  26155. 'rtl.createClass(this, "TObject", null, function () {',
  26156. ' this.$init = function () {',
  26157. ' };',
  26158. ' this.$final = function () {',
  26159. ' };',
  26160. ' this.DoIt = function (vA) {',
  26161. ' var Result = 0;',
  26162. ' return Result;',
  26163. ' };',
  26164. '});',
  26165. 'this.Obj = null;',
  26166. 'this.vP = null;',
  26167. 'this.b = false;'
  26168. ]),
  26169. LinesToStr([
  26170. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  26171. '$mod.vP(1);',
  26172. '$mod.vP(1);',
  26173. '$mod.vP(2);',
  26174. '$mod.b = rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
  26175. '$mod.b = rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
  26176. '$mod.b = !rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
  26177. '$mod.b = !rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
  26178. '']));
  26179. end;
  26180. procedure TTestModule.TestProcType_MethodDelphi;
  26181. begin
  26182. StartProgram(false);
  26183. Add([
  26184. '{$mode delphi}',
  26185. 'type',
  26186. ' TFuncInt = function(vA: longint = 1): longint of object;',
  26187. ' TObject = class',
  26188. ' function DoIt(vA: longint = 1): longint;',
  26189. ' end;',
  26190. 'function TObject.DoIt(vA: longint = 1): longint;',
  26191. 'begin',
  26192. 'end;',
  26193. 'var',
  26194. ' Obj: TObject;',
  26195. ' vP: tfuncint;',
  26196. ' b: boolean;',
  26197. 'begin',
  26198. ' vp:[email protected];', // ok in fpc and delphi
  26199. ' vp:=obj.doit;', // illegal in fpc, ok in delphi
  26200. ' vp;', // ok in fpc and delphi
  26201. ' vp();',
  26202. ' vp(2);',
  26203. //' b:[email protected];', // ok in fpc, illegal in delphi
  26204. //' b:[email protected]=vp;', // ok in fpc, illegal in delphi
  26205. //' b:=vp<>@obj.doit;', // ok in fpc, illegal in delphi
  26206. //' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
  26207. '']);
  26208. ConvertProgram;
  26209. CheckSource('TestProcType_MethodDelphi',
  26210. LinesToStr([ // statements
  26211. 'rtl.createClass(this, "TObject", null, function () {',
  26212. ' this.$init = function () {',
  26213. ' };',
  26214. ' this.$final = function () {',
  26215. ' };',
  26216. ' this.DoIt = function (vA) {',
  26217. ' var Result = 0;',
  26218. ' return Result;',
  26219. ' };',
  26220. '});',
  26221. 'this.Obj = null;',
  26222. 'this.vP = null;',
  26223. 'this.b = false;'
  26224. ]),
  26225. LinesToStr([
  26226. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  26227. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  26228. '$mod.vP(1);',
  26229. '$mod.vP(1);',
  26230. '$mod.vP(2);',
  26231. '']));
  26232. end;
  26233. procedure TTestModule.TestProcType_PropertyFPC;
  26234. begin
  26235. StartProgram(false);
  26236. Add('type');
  26237. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  26238. Add(' TObject = class');
  26239. Add(' FOnFoo: TFuncInt;');
  26240. Add(' function DoIt(vA: longint = 1): longint;');
  26241. Add(' function GetFoo: TFuncInt;');
  26242. Add(' procedure SetFoo(const Value: TFuncInt);');
  26243. Add(' function GetEvents(Index: longint): TFuncInt;');
  26244. Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
  26245. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  26246. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  26247. Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
  26248. Add(' end;');
  26249. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  26250. Add('function tobject.getfoo: tfuncint; begin end;');
  26251. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  26252. Add('function tobject.getevents(index: longint): tfuncint; begin end;');
  26253. Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
  26254. Add('var');
  26255. Add(' Obj: TObject;');
  26256. Add(' vP: tfuncint;');
  26257. Add(' b: boolean;');
  26258. Add('begin');
  26259. Add(' obj.onfoo:=nil;');
  26260. Add(' obj.onbar:=nil;');
  26261. Add(' obj.events[1]:=nil;');
  26262. Add(' obj.onfoo:=obj.onfoo;');
  26263. Add(' obj.onbar:=obj.onbar;');
  26264. Add(' obj.events[2]:=obj.events[3];');
  26265. Add(' obj.onfoo:[email protected];');
  26266. Add(' obj.onbar:[email protected];');
  26267. Add(' obj.events[4]:[email protected];');
  26268. //Add(' obj.onfoo:=obj.doit;'); // delphi
  26269. //Add(' obj.onbar:=obj.doit;'); // delphi
  26270. //Add(' obj.events[4]:=obj.doit;'); // delphi
  26271. Add(' obj.onfoo;');
  26272. Add(' obj.onbar;');
  26273. //Add(' obj.events[5];'); ToDo in pasresolver
  26274. Add(' obj.onfoo();');
  26275. Add(' obj.onbar();');
  26276. Add(' obj.events[6]();');
  26277. Add(' b:=obj.onfoo=nil;');
  26278. Add(' b:=obj.onbar=nil;');
  26279. Add(' b:=obj.events[7]=nil;');
  26280. Add(' b:=obj.onfoo<>nil;');
  26281. Add(' b:=obj.onbar<>nil;');
  26282. Add(' b:=obj.events[8]<>nil;');
  26283. Add(' b:=obj.onfoo=vp;');
  26284. Add(' b:=obj.onbar=vp;');
  26285. Add(' b:=obj.events[9]=vp;');
  26286. Add(' b:=obj.onfoo=obj.onfoo;');
  26287. Add(' b:=obj.onbar=obj.onfoo;');
  26288. Add(' b:=obj.events[10]=obj.onfoo;');
  26289. Add(' b:=obj.onfoo<>obj.onfoo;');
  26290. Add(' b:=obj.onbar<>obj.onfoo;');
  26291. Add(' b:=obj.events[11]<>obj.onfoo;');
  26292. Add(' b:[email protected];');
  26293. Add(' b:[email protected];');
  26294. Add(' b:=obj.events[12][email protected];');
  26295. Add(' b:=obj.onfoo<>@obj.doit;');
  26296. Add(' b:=obj.onbar<>@obj.doit;');
  26297. Add(' b:=obj.events[12]<>@obj.doit;');
  26298. Add(' b:=Assigned(obj.onfoo);');
  26299. Add(' b:=Assigned(obj.onbar);');
  26300. Add(' b:=Assigned(obj.events[13]);');
  26301. ConvertProgram;
  26302. CheckSource('TestProcType_PropertyFPC',
  26303. LinesToStr([ // statements
  26304. 'rtl.createClass(this, "TObject", null, function () {',
  26305. ' this.$init = function () {',
  26306. ' this.FOnFoo = null;',
  26307. ' };',
  26308. ' this.$final = function () {',
  26309. ' this.FOnFoo = undefined;',
  26310. ' };',
  26311. ' this.DoIt = function (vA) {',
  26312. ' var Result = 0;',
  26313. ' return Result;',
  26314. ' };',
  26315. 'this.GetFoo = function () {',
  26316. ' var Result = null;',
  26317. ' return Result;',
  26318. '};',
  26319. 'this.SetFoo = function (Value) {',
  26320. '};',
  26321. 'this.GetEvents = function (Index) {',
  26322. ' var Result = null;',
  26323. ' return Result;',
  26324. '};',
  26325. 'this.SetEvents = function (Index, Value) {',
  26326. '};',
  26327. '});',
  26328. 'this.Obj = null;',
  26329. 'this.vP = null;',
  26330. 'this.b = false;'
  26331. ]),
  26332. LinesToStr([
  26333. '$mod.Obj.FOnFoo = null;',
  26334. '$mod.Obj.SetFoo(null);',
  26335. '$mod.Obj.SetEvents(1, null);',
  26336. '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
  26337. '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
  26338. '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
  26339. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  26340. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  26341. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  26342. '$mod.Obj.FOnFoo(1);',
  26343. '$mod.Obj.GetFoo();',
  26344. '$mod.Obj.FOnFoo(1);',
  26345. '$mod.Obj.GetFoo()(1);',
  26346. '$mod.Obj.GetEvents(6)(1);',
  26347. '$mod.b = $mod.Obj.FOnFoo === null;',
  26348. '$mod.b = $mod.Obj.GetFoo() === null;',
  26349. '$mod.b = $mod.Obj.GetEvents(7) === null;',
  26350. '$mod.b = $mod.Obj.FOnFoo !== null;',
  26351. '$mod.b = $mod.Obj.GetFoo() !== null;',
  26352. '$mod.b = $mod.Obj.GetEvents(8) !== null;',
  26353. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.vP);',
  26354. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.vP);',
  26355. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(9), $mod.vP);',
  26356. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
  26357. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
  26358. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(10), $mod.Obj.FOnFoo);',
  26359. '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
  26360. '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
  26361. '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(11), $mod.Obj.FOnFoo);',
  26362. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
  26363. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
  26364. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
  26365. '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
  26366. '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
  26367. '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
  26368. '$mod.b = $mod.Obj.FOnFoo != null;',
  26369. '$mod.b = $mod.Obj.GetFoo() != null;',
  26370. '$mod.b = $mod.Obj.GetEvents(13) != null;',
  26371. '']));
  26372. end;
  26373. procedure TTestModule.TestProcType_PropertyDelphi;
  26374. begin
  26375. StartProgram(false);
  26376. Add('{$mode delphi}');
  26377. Add('type');
  26378. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  26379. Add(' TObject = class');
  26380. Add(' FOnFoo: TFuncInt;');
  26381. Add(' function DoIt(vA: longint = 1): longint;');
  26382. Add(' function GetFoo: TFuncInt;');
  26383. Add(' procedure SetFoo(const Value: TFuncInt);');
  26384. Add(' function GetEvents(Index: longint): TFuncInt;');
  26385. Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
  26386. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  26387. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  26388. Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
  26389. Add(' end;');
  26390. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  26391. Add('function tobject.getfoo: tfuncint; begin end;');
  26392. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  26393. Add('function tobject.getevents(index: longint): tfuncint; begin end;');
  26394. Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
  26395. Add('var');
  26396. Add(' Obj: TObject;');
  26397. Add(' vP: tfuncint;');
  26398. Add(' b: boolean;');
  26399. Add('begin');
  26400. Add(' obj.onfoo:=nil;');
  26401. Add(' obj.onbar:=nil;');
  26402. Add(' obj.events[1]:=nil;');
  26403. Add(' obj.onfoo:=obj.onfoo;');
  26404. Add(' obj.onbar:=obj.onbar;');
  26405. Add(' obj.events[2]:=obj.events[3];');
  26406. Add(' obj.onfoo:[email protected];');
  26407. Add(' obj.onbar:[email protected];');
  26408. Add(' obj.events[4]:[email protected];');
  26409. Add(' obj.onfoo:=obj.doit;'); // delphi
  26410. Add(' obj.onbar:=obj.doit;'); // delphi
  26411. Add(' obj.events[4]:=obj.doit;'); // delphi
  26412. Add(' obj.onfoo;');
  26413. Add(' obj.onbar;');
  26414. //Add(' obj.events[5];'); ToDo in pasresolver
  26415. Add(' obj.onfoo();');
  26416. Add(' obj.onbar();');
  26417. Add(' obj.events[6]();');
  26418. //Add(' b:=obj.onfoo=nil;'); // fpc
  26419. //Add(' b:=obj.onbar=nil;'); // fpc
  26420. //Add(' b:=obj.events[7]=nil;'); // fpc
  26421. //Add(' b:=obj.onfoo<>nil;'); // fpc
  26422. //Add(' b:=obj.onbar<>nil;'); // fpc
  26423. //Add(' b:=obj.events[8]<>nil;'); // fpc
  26424. Add(' b:=obj.onfoo=vp;');
  26425. Add(' b:=obj.onbar=vp;');
  26426. //Add(' b:=obj.events[9]=vp;'); ToDo in pasresolver
  26427. Add(' b:=obj.onfoo=obj.onfoo;');
  26428. Add(' b:=obj.onbar=obj.onfoo;');
  26429. //Add(' b:=obj.events[10]=obj.onfoo;'); // ToDo in pasresolver
  26430. Add(' b:=obj.onfoo<>obj.onfoo;');
  26431. Add(' b:=obj.onbar<>obj.onfoo;');
  26432. //Add(' b:=obj.events[11]<>obj.onfoo;'); // ToDo in pasresolver
  26433. //Add(' b:[email protected];'); // fpc
  26434. //Add(' b:[email protected];'); // fpc
  26435. //Add(' b:=obj.events[12][email protected];'); // fpc
  26436. //Add(' b:=obj.onfoo<>@obj.doit;'); // fpc
  26437. //Add(' b:=obj.onbar<>@obj.doit;'); // fpc
  26438. //Add(' b:=obj.events[12]<>@obj.doit;'); // fpc
  26439. Add(' b:=Assigned(obj.onfoo);');
  26440. Add(' b:=Assigned(obj.onbar);');
  26441. Add(' b:=Assigned(obj.events[13]);');
  26442. ConvertProgram;
  26443. CheckSource('TestProcType_PropertyDelphi',
  26444. LinesToStr([ // statements
  26445. 'rtl.createClass(this, "TObject", null, function () {',
  26446. ' this.$init = function () {',
  26447. ' this.FOnFoo = null;',
  26448. ' };',
  26449. ' this.$final = function () {',
  26450. ' this.FOnFoo = undefined;',
  26451. ' };',
  26452. ' this.DoIt = function (vA) {',
  26453. ' var Result = 0;',
  26454. ' return Result;',
  26455. ' };',
  26456. 'this.GetFoo = function () {',
  26457. ' var Result = null;',
  26458. ' return Result;',
  26459. '};',
  26460. 'this.SetFoo = function (Value) {',
  26461. '};',
  26462. 'this.GetEvents = function (Index) {',
  26463. ' var Result = null;',
  26464. ' return Result;',
  26465. '};',
  26466. 'this.SetEvents = function (Index, Value) {',
  26467. '};',
  26468. '});',
  26469. 'this.Obj = null;',
  26470. 'this.vP = null;',
  26471. 'this.b = false;'
  26472. ]),
  26473. LinesToStr([
  26474. '$mod.Obj.FOnFoo = null;',
  26475. '$mod.Obj.SetFoo(null);',
  26476. '$mod.Obj.SetEvents(1, null);',
  26477. '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
  26478. '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
  26479. '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
  26480. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  26481. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  26482. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  26483. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  26484. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  26485. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  26486. '$mod.Obj.FOnFoo(1);',
  26487. '$mod.Obj.GetFoo();',
  26488. '$mod.Obj.FOnFoo(1);',
  26489. '$mod.Obj.GetFoo()(1);',
  26490. '$mod.Obj.GetEvents(6)(1);',
  26491. '$mod.b = $mod.Obj.FOnFoo(1) === $mod.vP(1);',
  26492. '$mod.b = $mod.Obj.GetFoo() === $mod.vP(1);',
  26493. '$mod.b = $mod.Obj.FOnFoo(1) === $mod.Obj.FOnFoo(1);',
  26494. '$mod.b = $mod.Obj.GetFoo() === $mod.Obj.FOnFoo(1);',
  26495. '$mod.b = $mod.Obj.FOnFoo(1) !== $mod.Obj.FOnFoo(1);',
  26496. '$mod.b = $mod.Obj.GetFoo() !== $mod.Obj.FOnFoo(1);',
  26497. '$mod.b = $mod.Obj.FOnFoo != null;',
  26498. '$mod.b = $mod.Obj.GetFoo() != null;',
  26499. '$mod.b = $mod.Obj.GetEvents(13) != null;',
  26500. '']));
  26501. end;
  26502. procedure TTestModule.TestProcType_WithClassInstDoPropertyFPC;
  26503. begin
  26504. StartProgram(false);
  26505. Add('type');
  26506. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  26507. Add(' TObject = class');
  26508. Add(' FOnFoo: TFuncInt;');
  26509. Add(' function DoIt(vA: longint = 1): longint;');
  26510. Add(' function GetFoo: TFuncInt;');
  26511. Add(' procedure SetFoo(const Value: TFuncInt);');
  26512. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  26513. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  26514. Add(' end;');
  26515. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  26516. Add('function tobject.getfoo: tfuncint; begin end;');
  26517. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  26518. Add('var');
  26519. Add(' Obj: TObject;');
  26520. Add(' vP: tfuncint;');
  26521. Add(' b: boolean;');
  26522. Add('begin');
  26523. Add('with obj do begin');
  26524. Add(' fonfoo:=nil;');
  26525. Add(' onfoo:=nil;');
  26526. Add(' onbar:=nil;');
  26527. Add(' fonfoo:=fonfoo;');
  26528. Add(' onfoo:=onfoo;');
  26529. Add(' onbar:=onbar;');
  26530. Add(' fonfoo:=@doit;');
  26531. Add(' onfoo:=@doit;');
  26532. Add(' onbar:=@doit;');
  26533. //Add(' fonfoo:=doit;'); // delphi
  26534. //Add(' onfoo:=doit;'); // delphi
  26535. //Add(' onbar:=doit;'); // delphi
  26536. Add(' fonfoo;');
  26537. Add(' onfoo;');
  26538. Add(' onbar;');
  26539. Add(' fonfoo();');
  26540. Add(' onfoo();');
  26541. Add(' onbar();');
  26542. Add(' b:=fonfoo=nil;');
  26543. Add(' b:=onfoo=nil;');
  26544. Add(' b:=onbar=nil;');
  26545. Add(' b:=fonfoo<>nil;');
  26546. Add(' b:=onfoo<>nil;');
  26547. Add(' b:=onbar<>nil;');
  26548. Add(' b:=fonfoo=vp;');
  26549. Add(' b:=onfoo=vp;');
  26550. Add(' b:=onbar=vp;');
  26551. Add(' b:=fonfoo=fonfoo;');
  26552. Add(' b:=onfoo=onfoo;');
  26553. Add(' b:=onbar=onfoo;');
  26554. Add(' b:=fonfoo<>fonfoo;');
  26555. Add(' b:=onfoo<>onfoo;');
  26556. Add(' b:=onbar<>onfoo;');
  26557. Add(' b:=fonfoo=@doit;');
  26558. Add(' b:=onfoo=@doit;');
  26559. Add(' b:=onbar=@doit;');
  26560. Add(' b:=fonfoo<>@doit;');
  26561. Add(' b:=onfoo<>@doit;');
  26562. Add(' b:=onbar<>@doit;');
  26563. Add(' b:=Assigned(fonfoo);');
  26564. Add(' b:=Assigned(onfoo);');
  26565. Add(' b:=Assigned(onbar);');
  26566. Add('end;');
  26567. ConvertProgram;
  26568. CheckSource('TestProcType_WithClassInstDoPropertyFPC',
  26569. LinesToStr([ // statements
  26570. 'rtl.createClass(this, "TObject", null, function () {',
  26571. ' this.$init = function () {',
  26572. ' this.FOnFoo = null;',
  26573. ' };',
  26574. ' this.$final = function () {',
  26575. ' this.FOnFoo = undefined;',
  26576. ' };',
  26577. ' this.DoIt = function (vA) {',
  26578. ' var Result = 0;',
  26579. ' return Result;',
  26580. ' };',
  26581. ' this.GetFoo = function () {',
  26582. ' var Result = null;',
  26583. ' return Result;',
  26584. ' };',
  26585. ' this.SetFoo = function (Value) {',
  26586. ' };',
  26587. '});',
  26588. 'this.Obj = null;',
  26589. 'this.vP = null;',
  26590. 'this.b = false;'
  26591. ]),
  26592. LinesToStr([
  26593. 'var $with = $mod.Obj;',
  26594. '$with.FOnFoo = null;',
  26595. '$with.FOnFoo = null;',
  26596. '$with.SetFoo(null);',
  26597. '$with.FOnFoo = $with.FOnFoo;',
  26598. '$with.FOnFoo = $with.FOnFoo;',
  26599. '$with.SetFoo($with.GetFoo());',
  26600. '$with.FOnFoo = rtl.createCallback($with, "DoIt");',
  26601. '$with.FOnFoo = rtl.createCallback($with, "DoIt");',
  26602. '$with.SetFoo(rtl.createCallback($with, "DoIt"));',
  26603. '$with.FOnFoo(1);',
  26604. '$with.FOnFoo(1);',
  26605. '$with.GetFoo();',
  26606. '$with.FOnFoo(1);',
  26607. '$with.FOnFoo(1);',
  26608. '$with.GetFoo()(1);',
  26609. '$mod.b = $with.FOnFoo === null;',
  26610. '$mod.b = $with.FOnFoo === null;',
  26611. '$mod.b = $with.GetFoo() === null;',
  26612. '$mod.b = $with.FOnFoo !== null;',
  26613. '$mod.b = $with.FOnFoo !== null;',
  26614. '$mod.b = $with.GetFoo() !== null;',
  26615. '$mod.b = rtl.eqCallback($with.FOnFoo, $mod.vP);',
  26616. '$mod.b = rtl.eqCallback($with.FOnFoo, $mod.vP);',
  26617. '$mod.b = rtl.eqCallback($with.GetFoo(), $mod.vP);',
  26618. '$mod.b = rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  26619. '$mod.b = rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  26620. '$mod.b = rtl.eqCallback($with.GetFoo(), $with.FOnFoo);',
  26621. '$mod.b = !rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  26622. '$mod.b = !rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  26623. '$mod.b = !rtl.eqCallback($with.GetFoo(), $with.FOnFoo);',
  26624. '$mod.b = rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  26625. '$mod.b = rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  26626. '$mod.b = rtl.eqCallback($with.GetFoo(), rtl.createCallback($with, "DoIt"));',
  26627. '$mod.b = !rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  26628. '$mod.b = !rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  26629. '$mod.b = !rtl.eqCallback($with.GetFoo(), rtl.createCallback($with, "DoIt"));',
  26630. '$mod.b = $with.FOnFoo != null;',
  26631. '$mod.b = $with.FOnFoo != null;',
  26632. '$mod.b = $with.GetFoo() != null;',
  26633. '']));
  26634. end;
  26635. procedure TTestModule.TestProcType_Nested;
  26636. begin
  26637. StartProgram(false);
  26638. Add([
  26639. 'type',
  26640. ' TProcInt = procedure(vI: longint = 1);',
  26641. 'procedure DoIt(vJ: longint);',
  26642. 'var aProc: TProcInt;',
  26643. ' b: boolean;',
  26644. ' procedure Sub(vK: longint);',
  26645. ' var aSub: TProcInt;',
  26646. ' procedure SubSub(vK: longint);',
  26647. ' var aSubSub: TProcInt;',
  26648. ' begin;',
  26649. ' aProc:=@DoIt;',
  26650. ' aSub:=@DoIt;',
  26651. ' aSubSub:=@DoIt;',
  26652. ' aProc:=@Sub;',
  26653. ' aSub:=@Sub;',
  26654. ' aSubSub:=@Sub;',
  26655. ' aProc:=@SubSub;',
  26656. ' aSub:=@SubSub;',
  26657. ' aSubSub:=@SubSub;',
  26658. ' end;',
  26659. ' begin;',
  26660. ' end;',
  26661. 'begin;',
  26662. ' aProc:=@Sub;',
  26663. ' b:=aProc=@Sub;',
  26664. ' b:=@Sub=aProc;',
  26665. 'end;',
  26666. 'begin',
  26667. '']);
  26668. ConvertProgram;
  26669. CheckSource('TestProcType_Nested',
  26670. LinesToStr([ // statements
  26671. 'this.DoIt = function (vJ) {',
  26672. ' var aProc = null;',
  26673. ' var b = false;',
  26674. ' function Sub(vK) {',
  26675. ' var aSub = null;',
  26676. ' function SubSub(vK) {',
  26677. ' var aSubSub = null;',
  26678. ' aProc = $mod.DoIt;',
  26679. ' aSub = $mod.DoIt;',
  26680. ' aSubSub = $mod.DoIt;',
  26681. ' aProc = Sub;',
  26682. ' aSub = Sub;',
  26683. ' aSubSub = Sub;',
  26684. ' aProc = SubSub;',
  26685. ' aSub = SubSub;',
  26686. ' aSubSub = SubSub;',
  26687. ' };',
  26688. ' };',
  26689. ' aProc = Sub;',
  26690. ' b = rtl.eqCallback(aProc, Sub);',
  26691. ' b = rtl.eqCallback(Sub, aProc);',
  26692. '};',
  26693. '']),
  26694. LinesToStr([ // $mod.$main
  26695. '']));
  26696. end;
  26697. procedure TTestModule.TestProcType_NestedOfObject;
  26698. begin
  26699. StartProgram(false);
  26700. Add([
  26701. 'type',
  26702. ' TProcInt = procedure(vI: longint = 1) of object;',
  26703. ' TObject = class',
  26704. ' procedure DoIt(vJ: longint);',
  26705. ' end;',
  26706. 'procedure TObject.DoIt(vJ: longint);',
  26707. 'var aProc: TProcInt;',
  26708. ' b: boolean;',
  26709. ' procedure Sub(vK: longint);',
  26710. ' var aSub: TProcInt;',
  26711. ' procedure SubSub(vK: longint);',
  26712. ' var aSubSub: TProcInt;',
  26713. ' begin;',
  26714. ' aProc:=@DoIt;',
  26715. ' aSub:=@DoIt;',
  26716. ' aSubSub:=@DoIt;',
  26717. ' aProc:=@Sub;',
  26718. ' aSub:=@Sub;',
  26719. ' aSubSub:=@Sub;',
  26720. ' aProc:=@SubSub;',
  26721. ' aSub:=@SubSub;',
  26722. ' aSubSub:=@SubSub;',
  26723. ' end;',
  26724. ' begin;',
  26725. ' end;',
  26726. 'begin;',
  26727. ' aProc:=@Sub;',
  26728. ' b:=aProc=@Sub;',
  26729. ' b:=@Sub=aProc;',
  26730. 'end;',
  26731. 'begin',
  26732. '']);
  26733. ConvertProgram;
  26734. CheckSource('TestProcType_Nested',
  26735. LinesToStr([ // statements
  26736. 'rtl.createClass(this, "TObject", null, function () {',
  26737. ' this.$init = function () {',
  26738. ' };',
  26739. ' this.$final = function () {',
  26740. ' };',
  26741. ' this.DoIt = function (vJ) {',
  26742. ' var $Self = this;',
  26743. ' var aProc = null;',
  26744. ' var b = false;',
  26745. ' function Sub(vK) {',
  26746. ' var aSub = null;',
  26747. ' function SubSub(vK) {',
  26748. ' var aSubSub = null;',
  26749. ' aProc = rtl.createCallback($Self, "DoIt");',
  26750. ' aSub = rtl.createCallback($Self, "DoIt");',
  26751. ' aSubSub = rtl.createCallback($Self, "DoIt");',
  26752. ' aProc = Sub;',
  26753. ' aSub = Sub;',
  26754. ' aSubSub = Sub;',
  26755. ' aProc = SubSub;',
  26756. ' aSub = SubSub;',
  26757. ' aSubSub = SubSub;',
  26758. ' };',
  26759. ' };',
  26760. ' aProc = Sub;',
  26761. ' b = rtl.eqCallback(aProc, Sub);',
  26762. ' b = rtl.eqCallback(Sub, aProc);',
  26763. ' };',
  26764. '});',
  26765. '']),
  26766. LinesToStr([ // $mod.$main
  26767. '']));
  26768. end;
  26769. procedure TTestModule.TestProcType_ReferenceToProc;
  26770. begin
  26771. StartProgram(false);
  26772. Add([
  26773. 'type',
  26774. ' TProcRef = reference to procedure(i: longint = 0);',
  26775. ' TFuncRef = reference to function(i: longint = 0): longint;',
  26776. 'var',
  26777. ' p: TProcRef;',
  26778. ' f: TFuncRef;',
  26779. 'procedure DoIt(i: longint);',
  26780. 'begin',
  26781. 'end;',
  26782. 'function GetIt(i: longint): longint;',
  26783. 'begin',
  26784. ' p:=@DoIt;',
  26785. ' f:=@GetIt;',
  26786. ' f;',
  26787. ' f();',
  26788. ' f(1);',
  26789. 'end;',
  26790. 'begin',
  26791. ' p:=@DoIt;',
  26792. ' f:=@GetIt;',
  26793. ' f;',
  26794. ' f();',
  26795. ' f(1);',
  26796. ' p:=TProcRef(f);',
  26797. '']);
  26798. ConvertProgram;
  26799. CheckSource('TestProcType_ReferenceToProc',
  26800. LinesToStr([ // statements
  26801. 'this.p = null;',
  26802. 'this.f = null;',
  26803. 'this.DoIt = function (i) {',
  26804. '};',
  26805. 'this.GetIt = function (i) {',
  26806. ' var Result = 0;',
  26807. ' $mod.p = $mod.DoIt;',
  26808. ' $mod.f = $mod.GetIt;',
  26809. ' $mod.f(0);',
  26810. ' $mod.f(0);',
  26811. ' $mod.f(1);',
  26812. ' return Result;',
  26813. '};',
  26814. '']),
  26815. LinesToStr([ // $mod.$main
  26816. '$mod.p = $mod.DoIt;',
  26817. '$mod.f = $mod.GetIt;',
  26818. '$mod.f(0);',
  26819. '$mod.f(0);',
  26820. '$mod.f(1);',
  26821. '$mod.p = $mod.f;',
  26822. '']));
  26823. end;
  26824. procedure TTestModule.TestProcType_ReferenceToMethod;
  26825. begin
  26826. StartProgram(false);
  26827. Add([
  26828. 'type',
  26829. ' TFuncRef = reference to function(i: longint = 5): longint;',
  26830. ' TObject = class',
  26831. ' function Grow(s: longint): longint;',
  26832. ' end;',
  26833. 'var',
  26834. ' f: tfuncref;',
  26835. 'function tobject.grow(s: longint): longint;',
  26836. ' function GrowSub(i: longint): longint;',
  26837. ' begin',
  26838. ' f:=@grow;',
  26839. ' f:=@growsub;',
  26840. ' end;',
  26841. 'begin',
  26842. ' f:=@grow;',
  26843. ' f:=@growsub;',
  26844. 'end;',
  26845. 'begin',
  26846. '']);
  26847. ConvertProgram;
  26848. CheckSource('TestProcType_ReferenceToMethod',
  26849. LinesToStr([ // statements
  26850. 'rtl.createClass(this, "TObject", null, function () {',
  26851. ' this.$init = function () {',
  26852. ' };',
  26853. ' this.$final = function () {',
  26854. ' };',
  26855. ' this.Grow = function (s) {',
  26856. ' var $Self = this;',
  26857. ' var Result = 0;',
  26858. ' function GrowSub(i) {',
  26859. ' var Result = 0;',
  26860. ' $mod.f = rtl.createCallback($Self, "Grow");',
  26861. ' $mod.f = GrowSub;',
  26862. ' return Result;',
  26863. ' };',
  26864. ' $mod.f = rtl.createCallback($Self, "Grow");',
  26865. ' $mod.f = GrowSub;',
  26866. ' return Result;',
  26867. ' };',
  26868. '});',
  26869. 'this.f = null;',
  26870. '']),
  26871. LinesToStr([ // $mod.$main
  26872. '']));
  26873. end;
  26874. procedure TTestModule.TestProcType_Typecast;
  26875. begin
  26876. StartProgram(false);
  26877. Add([
  26878. 'type',
  26879. ' TNotifyEvent = procedure(Sender: Pointer) of object;',
  26880. ' TEvent = procedure of object;',
  26881. ' TGetter = function:longint of object;',
  26882. ' TProcA = procedure(i: longint);',
  26883. ' TFuncB = function(i, j: longint): longint;',
  26884. 'procedure DoIt(); varargs; begin end;',
  26885. 'var',
  26886. ' Notify: tnotifyevent;',
  26887. ' Event: tevent;',
  26888. ' Getter: tgetter;',
  26889. ' ProcA: tproca;',
  26890. ' FuncB: tfuncb;',
  26891. ' p: pointer;',
  26892. 'begin',
  26893. ' notify:=tnotifyevent(event);',
  26894. ' event:=tevent(event);',
  26895. ' event:=tevent(notify);',
  26896. ' event:=tevent(getter);',
  26897. ' event:=tevent(proca);',
  26898. ' proca:=tproca(funcb);',
  26899. ' funcb:=tfuncb(funcb);',
  26900. ' funcb:=tfuncb(proca);',
  26901. ' funcb:=tfuncb(getter);',
  26902. ' proca:=tproca(p);',
  26903. ' funcb:=tfuncb(p);',
  26904. ' getter:=tgetter(p);',
  26905. ' p:=pointer(notify);',
  26906. ' p:=notify;',
  26907. ' p:=pointer(proca);',
  26908. ' p:=proca;',
  26909. ' p:=pointer(funcb);',
  26910. ' p:=funcb;',
  26911. ' doit(Pointer(notify),pointer(event),pointer(proca));',
  26912. '']);
  26913. ConvertProgram;
  26914. CheckSource('TestProcType_Typecast',
  26915. LinesToStr([ // statements
  26916. 'this.DoIt = function () {',
  26917. '};',
  26918. 'this.Notify = null;',
  26919. 'this.Event = null;',
  26920. 'this.Getter = null;',
  26921. 'this.ProcA = null;',
  26922. 'this.FuncB = null;',
  26923. 'this.p = null;',
  26924. '']),
  26925. LinesToStr([ // $mod.$main
  26926. '$mod.Notify = $mod.Event;',
  26927. '$mod.Event = $mod.Event;',
  26928. '$mod.Event = $mod.Notify;',
  26929. '$mod.Event = $mod.Getter;',
  26930. '$mod.Event = $mod.ProcA;',
  26931. '$mod.ProcA = $mod.FuncB;',
  26932. '$mod.FuncB = $mod.FuncB;',
  26933. '$mod.FuncB = $mod.ProcA;',
  26934. '$mod.FuncB = $mod.Getter;',
  26935. '$mod.ProcA = $mod.p;',
  26936. '$mod.FuncB = $mod.p;',
  26937. '$mod.Getter = $mod.p;',
  26938. '$mod.p = $mod.Notify;',
  26939. '$mod.p = $mod.Notify;',
  26940. '$mod.p = $mod.ProcA;',
  26941. '$mod.p = $mod.ProcA;',
  26942. '$mod.p = $mod.FuncB;',
  26943. '$mod.p = $mod.FuncB;',
  26944. '$mod.DoIt($mod.Notify, $mod.Event, $mod.ProcA);',
  26945. '']));
  26946. end;
  26947. procedure TTestModule.TestProcType_PassProcToUntyped;
  26948. begin
  26949. StartProgram(false);
  26950. Add([
  26951. 'type',
  26952. ' TEvent = procedure of object;',
  26953. ' TFunc = function: longint;',
  26954. 'procedure DoIt(); varargs; begin end;',
  26955. 'procedure DoSome(const a; var b; p: pointer); begin end;',
  26956. 'var',
  26957. ' Event: tevent;',
  26958. ' Func: TFunc;',
  26959. 'begin',
  26960. ' doit(event,func);',
  26961. ' dosome(event,event,event);',
  26962. ' dosome(func,func,func);',
  26963. '']);
  26964. ConvertProgram;
  26965. CheckSource('TestProcType_PassProcToUntyped',
  26966. LinesToStr([ // statements
  26967. 'this.DoIt = function () {',
  26968. '};',
  26969. 'this.DoSome = function (a, b, p) {',
  26970. '};',
  26971. 'this.Event = null;',
  26972. 'this.Func = null;',
  26973. '']),
  26974. LinesToStr([ // $mod.$main
  26975. '$mod.DoIt($mod.Event, $mod.Func);',
  26976. '$mod.DoSome($mod.Event, {',
  26977. ' p: $mod,',
  26978. ' get: function () {',
  26979. ' return this.p.Event;',
  26980. ' },',
  26981. ' set: function (v) {',
  26982. ' this.p.Event = v;',
  26983. ' }',
  26984. '}, $mod.Event);',
  26985. '$mod.DoSome($mod.Func, {',
  26986. ' p: $mod,',
  26987. ' get: function () {',
  26988. ' return this.p.Func;',
  26989. ' },',
  26990. ' set: function (v) {',
  26991. ' this.p.Func = v;',
  26992. ' }',
  26993. '}, $mod.Func);',
  26994. '']));
  26995. end;
  26996. procedure TTestModule.TestProcType_PassProcToArray;
  26997. begin
  26998. StartProgram(false);
  26999. Add([
  27000. 'type',
  27001. ' TFunc = function: longint;',
  27002. ' TArrFunc = array of TFunc;',
  27003. 'procedure DoIt(Arr: TArrFunc); begin end;',
  27004. 'function GetIt: longint; begin end;',
  27005. 'var',
  27006. ' Func: tfunc;',
  27007. 'begin',
  27008. ' doit([]);',
  27009. ' doit([@GetIt]);',
  27010. ' doit([Func]);',
  27011. '']);
  27012. ConvertProgram;
  27013. CheckSource('TestProcType_PassProcToArray',
  27014. LinesToStr([ // statements
  27015. 'this.DoIt = function (Arr) {',
  27016. '};',
  27017. 'this.GetIt = function () {',
  27018. ' var Result = 0;',
  27019. ' return Result;',
  27020. '};',
  27021. 'this.Func = null;',
  27022. '']),
  27023. LinesToStr([ // $mod.$main
  27024. '$mod.DoIt([]);',
  27025. '$mod.DoIt([$mod.GetIt]);',
  27026. '$mod.DoIt([$mod.Func]);',
  27027. '']));
  27028. end;
  27029. procedure TTestModule.TestProcType_SafeCallObjFPC;
  27030. begin
  27031. StartProgram(false);
  27032. Add([
  27033. '{$modeswitch externalclass}',
  27034. 'type',
  27035. ' TProc = reference to procedure(i: longint); safecall;',
  27036. ' TEvent = procedure(i: longint) of object; safecall;',
  27037. ' TExtA = class external name ''ExtObj''',
  27038. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  27039. ' procedure DoSome(Id: longint = 1);',
  27040. ' procedure SetOnClick(const e: TEvent);',
  27041. ' property OnClick: TEvent write SetOnClick;',
  27042. ' class procedure Fly(Id: longint = 1); static;',
  27043. ' procedure SetOnShow(const p: TProc);',
  27044. ' property OnShow: TProc write SetOnShow;',
  27045. ' end;',
  27046. 'procedure Run(i: longint = 1);',
  27047. 'begin',
  27048. 'end;',
  27049. 'var',
  27050. ' Obj: texta;',
  27051. ' e: TEvent;',
  27052. ' p: TProc;',
  27053. 'begin',
  27054. ' e:=e;',
  27055. ' e:[email protected];',
  27056. ' e:[email protected];',
  27057. ' e:=TEvent(@obj.dosome);', // no safecall
  27058. ' obj.OnClick:[email protected];',
  27059. ' obj.OnClick:[email protected];',
  27060. ' obj.setonclick(@obj.doit);',
  27061. ' obj.setonclick(@obj.dosome);',
  27062. ' p:=@Run;',
  27063. ' p:[email protected];',
  27064. ' obj.OnShow:=@Run;',
  27065. ' obj.OnShow:[email protected];',
  27066. ' obj.setOnShow(@Run);',
  27067. ' obj.setOnShow(@TExtA.Fly);',
  27068. ' with obj do begin',
  27069. ' e:=@doit;',
  27070. ' e:=@dosome;',
  27071. ' OnClick:=@doit;',
  27072. ' OnClick:=@dosome;',
  27073. ' setonclick(@doit);',
  27074. ' setonclick(@dosome);',
  27075. ' OnShow:=@Run;',
  27076. ' setOnShow(@Run);',
  27077. ' end;']);
  27078. ConvertProgram;
  27079. CheckSource('TestProcType_SafeCallObjFPC',
  27080. LinesToStr([ // statements
  27081. 'this.Run = function (i) {',
  27082. '};',
  27083. 'this.Obj = null;',
  27084. 'this.e = null;',
  27085. 'this.p = null;',
  27086. '']),
  27087. LinesToStr([ // $mod.$main
  27088. '$mod.e = $mod.e;',
  27089. '$mod.e = rtl.createSafeCallback($mod.Obj, "$Execute");',
  27090. '$mod.e = rtl.createSafeCallback($mod.Obj, "DoSome");',
  27091. '$mod.e = rtl.createCallback($mod.Obj, "DoSome");',
  27092. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  27093. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  27094. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  27095. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  27096. '$mod.p = rtl.createSafeCallback($mod, "Run");',
  27097. '$mod.p = rtl.createSafeCallback(ExtObj, "Fly");',
  27098. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  27099. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  27100. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  27101. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  27102. 'var $with = $mod.Obj;',
  27103. '$mod.e = rtl.createSafeCallback($with, "$Execute");',
  27104. '$mod.e = rtl.createSafeCallback($with, "DoSome");',
  27105. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  27106. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  27107. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  27108. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  27109. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  27110. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  27111. '']));
  27112. end;
  27113. procedure TTestModule.TestProcType_SafeCallDelphi;
  27114. begin
  27115. StartProgram(false);
  27116. Add([
  27117. '{$mode delphi}',
  27118. '{$modeswitch externalclass}',
  27119. 'type',
  27120. ' TProc = reference to procedure(i: longint); safecall;',
  27121. ' TEvent = procedure(i: longint) of object; safecall;',
  27122. ' TExtA = class external name ''ExtObj''',
  27123. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  27124. ' procedure DoSome(Id: longint = 1);',
  27125. ' procedure SetOnClick(const e: TEvent);',
  27126. ' property OnClick: TEvent write SetOnClick;',
  27127. ' class procedure Fly(Id: longint = 1); static;',
  27128. ' procedure SetOnShow(const p: TProc);',
  27129. ' property OnShow: TProc write SetOnShow;',
  27130. ' end;',
  27131. 'procedure Run(i: longint = 1);',
  27132. 'begin',
  27133. 'end;',
  27134. 'var',
  27135. ' Obj: texta;',
  27136. ' e: TEvent;',
  27137. ' p: TProc;',
  27138. 'begin',
  27139. ' e:=e;',
  27140. ' e:=obj.doit;',
  27141. ' e:=obj.dosome;',
  27142. ' e:=TEvent(@obj.dosome);', // no safecall
  27143. ' obj.OnClick:=obj.doit;',
  27144. ' obj.OnClick:=obj.dosome;',
  27145. ' obj.setonclick(obj.doit);',
  27146. ' obj.setonclick(obj.dosome);',
  27147. ' p:=Run;',
  27148. ' p:=TExtA.Fly;',
  27149. ' obj.OnShow:=Run;',
  27150. ' obj.OnShow:=TExtA.Fly;',
  27151. ' obj.setOnShow(Run);',
  27152. ' obj.setOnShow(TExtA.Fly);',
  27153. ' with obj do begin',
  27154. ' e:=doit;',
  27155. ' e:=dosome;',
  27156. ' OnClick:=doit;',
  27157. ' OnClick:=dosome;',
  27158. ' setonclick(doit);',
  27159. ' setonclick(dosome);',
  27160. ' OnShow:=@Run;',
  27161. ' setOnShow(@Run);',
  27162. ' end;']);
  27163. ConvertProgram;
  27164. CheckSource('TestProcType_SafeCallDelphi',
  27165. LinesToStr([ // statements
  27166. 'this.Run = function (i) {',
  27167. '};',
  27168. 'this.Obj = null;',
  27169. 'this.e = null;',
  27170. 'this.p = null;',
  27171. '']),
  27172. LinesToStr([ // $mod.$main
  27173. '$mod.e = $mod.e;',
  27174. '$mod.e = rtl.createSafeCallback($mod.Obj, "$Execute");',
  27175. '$mod.e = rtl.createSafeCallback($mod.Obj, "DoSome");',
  27176. '$mod.e = rtl.createCallback($mod.Obj, "DoSome");',
  27177. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  27178. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  27179. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  27180. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  27181. '$mod.p = rtl.createSafeCallback($mod, "Run");',
  27182. '$mod.p = rtl.createSafeCallback(ExtObj, "Fly");',
  27183. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  27184. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  27185. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  27186. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  27187. 'var $with = $mod.Obj;',
  27188. '$mod.e = rtl.createSafeCallback($with, "$Execute");',
  27189. '$mod.e = rtl.createSafeCallback($with, "DoSome");',
  27190. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  27191. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  27192. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  27193. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  27194. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  27195. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  27196. '']));
  27197. end;
  27198. procedure TTestModule.TestPointer;
  27199. begin
  27200. StartProgram(false);
  27201. Add(['type',
  27202. ' TObject = class end;',
  27203. ' TClass = class of TObject;',
  27204. ' TArrInt = array of longint;',
  27205. 'const',
  27206. ' n = nil;',
  27207. 'var',
  27208. ' v: jsvalue;',
  27209. ' Obj: tobject;',
  27210. ' C: tclass;',
  27211. ' a: tarrint;',
  27212. ' p: Pointer = nil;',
  27213. ' s: string;',
  27214. 'begin',
  27215. ' p:=p;',
  27216. ' p:=nil;',
  27217. ' if p=nil then;',
  27218. ' if nil=p then;',
  27219. ' if Assigned(p) then;',
  27220. ' p:=Pointer(v);',
  27221. ' p:=obj;',
  27222. ' p:=c;',
  27223. ' p:=a;',
  27224. ' p:=tobject;',
  27225. ' obj:=TObject(p);',
  27226. ' c:=TClass(p);',
  27227. ' a:=TArrInt(p);',
  27228. ' p:=n;',
  27229. ' p:=Pointer(a);',
  27230. ' p:=pointer(s);',
  27231. ' s:=string(p);',
  27232. '']);
  27233. ConvertProgram;
  27234. CheckSource('TestPointer',
  27235. LinesToStr([ // statements
  27236. 'rtl.createClass(this, "TObject", null, function () {',
  27237. ' this.$init = function () {',
  27238. ' };',
  27239. ' this.$final = function () {',
  27240. ' };',
  27241. '});',
  27242. 'this.n = null;',
  27243. 'this.v = undefined;',
  27244. 'this.Obj = null;',
  27245. 'this.C = null;',
  27246. 'this.a = [];',
  27247. 'this.p = null;',
  27248. 'this.s = "";',
  27249. '']),
  27250. LinesToStr([ // $mod.$main
  27251. '$mod.p = $mod.p;',
  27252. '$mod.p = null;',
  27253. 'if ($mod.p === null) ;',
  27254. 'if (null === $mod.p) ;',
  27255. 'if ($mod.p != null) ;',
  27256. '$mod.p = $mod.v;',
  27257. '$mod.p = $mod.Obj;',
  27258. '$mod.p = $mod.C;',
  27259. '$mod.p = $mod.a;',
  27260. '$mod.p = $mod.TObject;',
  27261. '$mod.Obj = $mod.p;',
  27262. '$mod.C = $mod.p;',
  27263. '$mod.a = $mod.p;',
  27264. '$mod.p = null;',
  27265. '$mod.p = $mod.a;',
  27266. '$mod.p = $mod.s;',
  27267. '$mod.s = $mod.p;',
  27268. '']));
  27269. end;
  27270. procedure TTestModule.TestPointer_Proc;
  27271. begin
  27272. StartProgram(false);
  27273. Add('type');
  27274. Add(' TObject = class');
  27275. Add(' procedure DoIt; virtual; abstract;');
  27276. Add(' end;');
  27277. Add('procedure DoSome; begin end;');
  27278. Add('var');
  27279. Add(' o: TObject;');
  27280. Add(' p: Pointer;');
  27281. Add('begin');
  27282. Add(' p:=@DoSome;');
  27283. Add(' p:[email protected];');
  27284. ConvertProgram;
  27285. CheckSource('TestPointer_Proc',
  27286. LinesToStr([ // statements
  27287. 'rtl.createClass(this, "TObject", null, function () {',
  27288. ' this.$init = function () {',
  27289. ' };',
  27290. ' this.$final = function () {',
  27291. ' };',
  27292. '});',
  27293. 'this.DoSome = function () {',
  27294. '};',
  27295. 'this.o = null;',
  27296. 'this.p = null;',
  27297. '']),
  27298. LinesToStr([ // $mod.$main
  27299. '$mod.p = $mod.DoSome;',
  27300. '$mod.p = rtl.createCallback($mod.o, "DoIt");',
  27301. '']));
  27302. end;
  27303. procedure TTestModule.TestPointer_AssignRecordFail;
  27304. begin
  27305. StartProgram(false);
  27306. Add('type');
  27307. Add(' TRec = record end;');
  27308. Add('var');
  27309. Add(' p: Pointer;');
  27310. Add(' r: TRec;');
  27311. Add('begin');
  27312. Add(' p:=r;');
  27313. SetExpectedPasResolverError('Incompatible types: got "TRec" expected "Pointer"',
  27314. nIncompatibleTypesGotExpected);
  27315. ConvertProgram;
  27316. end;
  27317. procedure TTestModule.TestPointer_AssignStaticArrayFail;
  27318. begin
  27319. StartProgram(false);
  27320. Add('type');
  27321. Add(' TArr = array[boolean] of longint;');
  27322. Add('var');
  27323. Add(' p: Pointer;');
  27324. Add(' a: TArr;');
  27325. Add('begin');
  27326. Add(' p:=a;');
  27327. SetExpectedPasResolverError('Incompatible types: got "TArr" expected "Pointer"',
  27328. nIncompatibleTypesGotExpected);
  27329. ConvertProgram;
  27330. end;
  27331. procedure TTestModule.TestPointer_TypeCastJSValueToPointer;
  27332. begin
  27333. StartProgram(false);
  27334. Add([
  27335. 'procedure DoIt(args: array of jsvalue); begin end;',
  27336. 'procedure DoAll; varargs; begin end;',
  27337. 'var',
  27338. ' v: jsvalue;',
  27339. 'begin',
  27340. ' DoIt([pointer(v)]);',
  27341. ' DoAll(pointer(v));',
  27342. '']);
  27343. ConvertProgram;
  27344. CheckSource('TestPointer_TypeCastJSValueToPointer',
  27345. LinesToStr([ // statements
  27346. 'this.DoIt = function (args) {',
  27347. '};',
  27348. 'this.DoAll = function () {',
  27349. '};',
  27350. 'this.v = undefined;',
  27351. '']),
  27352. LinesToStr([ // $mod.$main
  27353. '$mod.DoIt([$mod.v]);',
  27354. '$mod.DoAll($mod.v);',
  27355. '']));
  27356. end;
  27357. procedure TTestModule.TestPointer_NonRecordFail;
  27358. begin
  27359. StartProgram(false);
  27360. Add([
  27361. 'type',
  27362. ' p = ^longint;',
  27363. 'begin',
  27364. '']);
  27365. SetExpectedPasResolverError('Not supported: pointer of Longint',nNotSupportedX);
  27366. ConvertProgram;
  27367. end;
  27368. procedure TTestModule.TestPointer_AnonymousArgTypeFail;
  27369. begin
  27370. StartProgram(false);
  27371. Add([
  27372. 'procedure DoIt(p: ^longint); begin end;',
  27373. 'begin',
  27374. '']);
  27375. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  27376. ConvertProgram;
  27377. end;
  27378. procedure TTestModule.TestPointer_AnonymousVarTypeFail;
  27379. begin
  27380. StartProgram(false);
  27381. Add([
  27382. 'var p: ^longint;',
  27383. 'begin',
  27384. '']);
  27385. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  27386. ConvertProgram;
  27387. end;
  27388. procedure TTestModule.TestPointer_AnonymousResultTypeFail;
  27389. begin
  27390. StartProgram(false);
  27391. Add([
  27392. 'function DoIt: ^longint; begin end;',
  27393. 'begin',
  27394. '']);
  27395. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  27396. ConvertProgram;
  27397. end;
  27398. procedure TTestModule.TestPointer_AddrOperatorFail;
  27399. begin
  27400. StartProgram(false);
  27401. Add([
  27402. 'var i: longint;',
  27403. 'begin',
  27404. ' if @i=nil then ;',
  27405. '']);
  27406. SetExpectedConverterError('illegal qualifier "@" in front of "i:Longint"',nIllegalQualifierInFrontOf);
  27407. ConvertProgram;
  27408. end;
  27409. procedure TTestModule.TestPointer_ArrayParamsFail;
  27410. begin
  27411. StartProgram(false);
  27412. Add([
  27413. 'var',
  27414. ' p: Pointer;',
  27415. 'begin',
  27416. ' p:=p[1];',
  27417. '']);
  27418. SetExpectedPasResolverError('illegal qualifier "[" after "Pointer"',nIllegalQualifierAfter);
  27419. ConvertProgram;
  27420. end;
  27421. procedure TTestModule.TestPointer_PointerAddFail;
  27422. begin
  27423. StartProgram(false);
  27424. Add([
  27425. 'var',
  27426. ' p: Pointer;',
  27427. 'begin',
  27428. ' p:=p+1;',
  27429. '']);
  27430. SetExpectedPasResolverError('Operator is not overloaded: "Pointer" + "Longint"',nOperatorIsNotOverloadedAOpB);
  27431. ConvertProgram;
  27432. end;
  27433. procedure TTestModule.TestPointer_IncPointerFail;
  27434. begin
  27435. StartProgram(false);
  27436. Add([
  27437. 'var',
  27438. ' p: Pointer;',
  27439. 'begin',
  27440. ' inc(p,1);',
  27441. '']);
  27442. SetExpectedPasResolverError('Incompatible type arg no. 1: Got "Pointer", expected "integer"',
  27443. nIncompatibleTypeArgNo);
  27444. ConvertProgram;
  27445. end;
  27446. procedure TTestModule.TestPointer_Record;
  27447. begin
  27448. StartProgram(false);
  27449. Add([
  27450. 'type',
  27451. ' TRec = record x: longint; end;',
  27452. ' PRec = ^TRec;',
  27453. 'var',
  27454. ' r: TRec;',
  27455. ' p: PRec;',
  27456. ' q: ^TRec;',
  27457. ' Ptr: pointer;',
  27458. 'begin',
  27459. ' new(p);',
  27460. ' p:=@r;',
  27461. ' r:=p^;',
  27462. ' r.x:=p^.x;',
  27463. ' p^.x:=r.x;',
  27464. ' if p^.x=3 then ;',
  27465. ' if 4=p^.x then ;',
  27466. ' dispose(p);',
  27467. ' new(q);',
  27468. ' dispose(q);',
  27469. ' Ptr:=p;',
  27470. ' p:=PRec(ptr);',
  27471. '']);
  27472. ConvertProgram;
  27473. CheckSource('TestPointer_Record',
  27474. LinesToStr([ // statements
  27475. 'rtl.recNewT(this, "TRec", function () {',
  27476. ' this.x = 0;',
  27477. ' this.$eq = function (b) {',
  27478. ' return this.x === b.x;',
  27479. ' };',
  27480. ' this.$assign = function (s) {',
  27481. ' this.x = s.x;',
  27482. ' return this;',
  27483. ' };',
  27484. '});',
  27485. 'this.r = this.TRec.$new();',
  27486. 'this.p = null;',
  27487. 'this.q = null;',
  27488. 'this.Ptr = null;',
  27489. '']),
  27490. LinesToStr([ // $mod.$main
  27491. '$mod.p = $mod.TRec.$new();',
  27492. '$mod.p = $mod.r;',
  27493. '$mod.r.$assign($mod.p);',
  27494. '$mod.r.x = $mod.p.x;',
  27495. '$mod.p.x = $mod.r.x;',
  27496. 'if ($mod.p.x === 3) ;',
  27497. 'if (4 === $mod.p.x) ;',
  27498. '$mod.p = null;',
  27499. '$mod.q = $mod.TRec.$new();',
  27500. '$mod.q = null;',
  27501. '$mod.Ptr = $mod.p;',
  27502. '$mod.p = $mod.Ptr;',
  27503. '']));
  27504. end;
  27505. procedure TTestModule.TestPointer_RecordArg;
  27506. begin
  27507. StartProgram(false);
  27508. Add([
  27509. '{$modeswitch autoderef}',
  27510. 'type',
  27511. ' TRec = record x: longint; end;',
  27512. ' PRec = ^TRec;',
  27513. 'function DoIt(const a: PRec; var b: PRec; out c: PRec): TRec;',
  27514. 'begin',
  27515. ' a.x:=a.x;',
  27516. ' a^.x:=a^.x;',
  27517. ' with a^ do',
  27518. ' x:=x;',
  27519. 'end;',
  27520. 'function GetIt(p: PRec): PRec;',
  27521. 'begin',
  27522. ' p.x:=p.x;',
  27523. ' p^.x:=p^.x;',
  27524. ' with p^ do',
  27525. ' x:=x;',
  27526. 'end;',
  27527. 'var',
  27528. ' r: TRec;',
  27529. ' p: PRec;',
  27530. 'begin',
  27531. ' p:=GetIt(p);',
  27532. ' p^:=GetIt(@r)^;',
  27533. ' DoIt(p,p,p);',
  27534. ' DoIt(@r,p,p);',
  27535. '']);
  27536. ConvertProgram;
  27537. CheckSource('TestPointer_RecordArg',
  27538. LinesToStr([ // statements
  27539. 'rtl.recNewT(this, "TRec", function () {',
  27540. ' this.x = 0;',
  27541. ' this.$eq = function (b) {',
  27542. ' return this.x === b.x;',
  27543. ' };',
  27544. ' this.$assign = function (s) {',
  27545. ' this.x = s.x;',
  27546. ' return this;',
  27547. ' };',
  27548. '});',
  27549. 'this.DoIt = function (a, b, c) {',
  27550. ' var Result = $mod.TRec.$new();',
  27551. ' a.x = a.x;',
  27552. ' a.x = a.x;',
  27553. ' a.x = a.x;',
  27554. ' return Result;',
  27555. '};',
  27556. 'this.GetIt = function (p) {',
  27557. ' var Result = null;',
  27558. ' p.x = p.x;',
  27559. ' p.x = p.x;',
  27560. ' p.x = p.x;',
  27561. ' return Result;',
  27562. '};',
  27563. 'this.r = this.TRec.$new();',
  27564. 'this.p = null;',
  27565. '']),
  27566. LinesToStr([ // $mod.$main
  27567. '$mod.p = $mod.GetIt($mod.p);',
  27568. '$mod.p.$assign($mod.GetIt($mod.r));',
  27569. '$mod.DoIt($mod.p, {',
  27570. ' p: $mod,',
  27571. ' get: function () {',
  27572. ' return this.p.p;',
  27573. ' },',
  27574. ' set: function (v) {',
  27575. ' this.p.p = v;',
  27576. ' }',
  27577. '}, {',
  27578. ' p: $mod,',
  27579. ' get: function () {',
  27580. ' return this.p.p;',
  27581. ' },',
  27582. ' set: function (v) {',
  27583. ' this.p.p = v;',
  27584. ' }',
  27585. '});',
  27586. '$mod.DoIt($mod.r, {',
  27587. ' p: $mod,',
  27588. ' get: function () {',
  27589. ' return this.p.p;',
  27590. ' },',
  27591. ' set: function (v) {',
  27592. ' this.p.p = v;',
  27593. ' }',
  27594. '}, {',
  27595. ' p: $mod,',
  27596. ' get: function () {',
  27597. ' return this.p.p;',
  27598. ' },',
  27599. ' set: function (v) {',
  27600. ' this.p.p = v;',
  27601. ' }',
  27602. '});',
  27603. '']));
  27604. end;
  27605. procedure TTestModule.TestJSValue_AssignToJSValue;
  27606. begin
  27607. StartProgram(false);
  27608. Add('var');
  27609. Add(' v: jsvalue;');
  27610. Add(' i: longint;');
  27611. Add(' s: string;');
  27612. Add(' b: boolean;');
  27613. Add(' d: double;');
  27614. Add(' p: pointer;');
  27615. Add('begin');
  27616. Add(' v:=v;');
  27617. Add(' v:=1;');
  27618. Add(' v:=i;');
  27619. Add(' v:='''';');
  27620. Add(' v:=''c'';');
  27621. Add(' v:=''foo'';');
  27622. Add(' v:=s;');
  27623. Add(' v:=false;');
  27624. Add(' v:=true;');
  27625. Add(' v:=b;');
  27626. Add(' v:=0.1;');
  27627. Add(' v:=d;');
  27628. Add(' v:=nil;');
  27629. Add(' v:=p;');
  27630. ConvertProgram;
  27631. CheckSource('TestJSValue_AssignToJSValue',
  27632. LinesToStr([ // statements
  27633. 'this.v = undefined;',
  27634. 'this.i = 0;',
  27635. 'this.s = "";',
  27636. 'this.b = false;',
  27637. 'this.d = 0.0;',
  27638. 'this.p = null;',
  27639. '']),
  27640. LinesToStr([ // $mod.$main
  27641. '$mod.v = $mod.v;',
  27642. '$mod.v = 1;',
  27643. '$mod.v = $mod.i;',
  27644. '$mod.v = "";',
  27645. '$mod.v = "c";',
  27646. '$mod.v = "foo";',
  27647. '$mod.v = $mod.s;',
  27648. '$mod.v = false;',
  27649. '$mod.v = true;',
  27650. '$mod.v = $mod.b;',
  27651. '$mod.v = 0.1;',
  27652. '$mod.v = $mod.d;',
  27653. '$mod.v = null;',
  27654. '$mod.v = $mod.p;',
  27655. '']));
  27656. end;
  27657. procedure TTestModule.TestJSValue_TypeCastToBaseType;
  27658. begin
  27659. StartProgram(false);
  27660. Add('type');
  27661. Add(' integer = longint;');
  27662. Add(' TYesNo = boolean;');
  27663. Add(' TFloat = double;');
  27664. Add(' TCaption = string;');
  27665. Add(' TChar = char;');
  27666. Add('var');
  27667. Add(' v: jsvalue;');
  27668. Add(' i: integer;');
  27669. Add(' s: TCaption;');
  27670. Add(' b: TYesNo;');
  27671. Add(' d: TFloat;');
  27672. Add(' c: char;');
  27673. Add('begin');
  27674. Add(' i:=longint(v);');
  27675. Add(' i:=integer(v);');
  27676. Add(' s:=string(v);');
  27677. Add(' s:=TCaption(v);');
  27678. Add(' b:=boolean(v);');
  27679. Add(' b:=TYesNo(v);');
  27680. Add(' d:=double(v);');
  27681. Add(' d:=TFloat(v);');
  27682. Add(' c:=char(v);');
  27683. Add(' c:=TChar(v);');
  27684. ConvertProgram;
  27685. CheckSource('TestJSValue_TypeCastToBaseType',
  27686. LinesToStr([ // statements
  27687. 'this.v = undefined;',
  27688. 'this.i = 0;',
  27689. 'this.s = "";',
  27690. 'this.b = false;',
  27691. 'this.d = 0.0;',
  27692. 'this.c = "";',
  27693. '']),
  27694. LinesToStr([ // $mod.$main
  27695. '$mod.i = rtl.trunc($mod.v);',
  27696. '$mod.i = rtl.trunc($mod.v);',
  27697. '$mod.s = "" + $mod.v;',
  27698. '$mod.s = "" + $mod.v;',
  27699. '$mod.b = !($mod.v == false);',
  27700. '$mod.b = !($mod.v == false);',
  27701. '$mod.d = rtl.getNumber($mod.v);',
  27702. '$mod.d = rtl.getNumber($mod.v);',
  27703. '$mod.c = rtl.getChar($mod.v);',
  27704. '$mod.c = rtl.getChar($mod.v);',
  27705. '']));
  27706. end;
  27707. procedure TTestModule.TestJSValue_TypecastToJSValue;
  27708. begin
  27709. StartProgram(false);
  27710. Add([
  27711. 'type',
  27712. ' TArr = array of word;',
  27713. ' TRec = record end;',
  27714. ' TSet = set of boolean;',
  27715. 'procedure Fly(v: jsvalue);',
  27716. 'begin',
  27717. 'end;',
  27718. 'var',
  27719. ' a: TArr;',
  27720. ' r: TRec;',
  27721. ' s: TSet;',
  27722. 'begin',
  27723. ' Fly(jsvalue(a));',
  27724. ' Fly(jsvalue(r));',
  27725. ' Fly(jsvalue(s));',
  27726. '']);
  27727. ConvertProgram;
  27728. CheckSource('TestJSValue_TypecastToJSValue',
  27729. LinesToStr([ // statements
  27730. 'rtl.recNewT(this, "TRec", function () {',
  27731. ' this.$eq = function (b) {',
  27732. ' return true;',
  27733. ' };',
  27734. ' this.$assign = function (s) {',
  27735. ' return this;',
  27736. ' };',
  27737. '});',
  27738. 'this.Fly = function (v) {',
  27739. '};',
  27740. 'this.a = [];',
  27741. 'this.r = this.TRec.$new();',
  27742. 'this.s = {};',
  27743. '']),
  27744. LinesToStr([ // $mod.$main
  27745. '$mod.Fly($mod.a);',
  27746. '$mod.Fly($mod.r);',
  27747. '$mod.Fly($mod.s);',
  27748. '']));
  27749. end;
  27750. procedure TTestModule.TestJSValue_Equal;
  27751. begin
  27752. StartProgram(false);
  27753. Add('type');
  27754. Add(' integer = longint;');
  27755. Add(' TYesNo = boolean;');
  27756. Add(' TFloat = double;');
  27757. Add(' TCaption = string;');
  27758. Add(' TChar = char;');
  27759. Add(' TMulti = JSValue;');
  27760. Add('var');
  27761. Add(' v: jsvalue;');
  27762. Add(' i: integer;');
  27763. Add(' s: TCaption;');
  27764. Add(' b: TYesNo;');
  27765. Add(' d: TFloat;');
  27766. Add(' c: char;');
  27767. Add(' m: TMulti;');
  27768. Add('begin');
  27769. Add(' b:=v=v;');
  27770. Add(' b:=v<>v;');
  27771. Add(' b:=v=1;');
  27772. Add(' b:=v<>1;');
  27773. Add(' b:=2=v;');
  27774. Add(' b:=2<>v;');
  27775. Add(' b:=v=i;');
  27776. Add(' b:=i=v;');
  27777. Add(' b:=v=nil;');
  27778. Add(' b:=nil=v;');
  27779. Add(' b:=v=false;');
  27780. Add(' b:=true=v;');
  27781. Add(' b:=v=b;');
  27782. Add(' b:=b=v;');
  27783. Add(' b:=v=s;');
  27784. Add(' b:=s=v;');
  27785. Add(' b:=v=''foo'';');
  27786. Add(' b:=''''=v;');
  27787. Add(' b:=v=d;');
  27788. Add(' b:=d=v;');
  27789. Add(' b:=v=3.4;');
  27790. Add(' b:=5.6=v;');
  27791. Add(' b:=v=c;');
  27792. Add(' b:=c=v;');
  27793. Add(' b:=m=m;');
  27794. Add(' b:=v=m;');
  27795. Add(' b:=m=v;');
  27796. ConvertProgram;
  27797. CheckSource('TestJSValue_Equal',
  27798. LinesToStr([ // statements
  27799. 'this.v = undefined;',
  27800. 'this.i = 0;',
  27801. 'this.s = "";',
  27802. 'this.b = false;',
  27803. 'this.d = 0.0;',
  27804. 'this.c = "";',
  27805. 'this.m = undefined;',
  27806. '']),
  27807. LinesToStr([ // $mod.$main
  27808. '$mod.b = $mod.v == $mod.v;',
  27809. '$mod.b = $mod.v != $mod.v;',
  27810. '$mod.b = $mod.v == 1;',
  27811. '$mod.b = $mod.v != 1;',
  27812. '$mod.b = 2 == $mod.v;',
  27813. '$mod.b = 2 != $mod.v;',
  27814. '$mod.b = $mod.v == $mod.i;',
  27815. '$mod.b = $mod.i == $mod.v;',
  27816. '$mod.b = $mod.v == null;',
  27817. '$mod.b = null == $mod.v;',
  27818. '$mod.b = $mod.v == false;',
  27819. '$mod.b = true == $mod.v;',
  27820. '$mod.b = $mod.v == $mod.b;',
  27821. '$mod.b = $mod.b == $mod.v;',
  27822. '$mod.b = $mod.v == $mod.s;',
  27823. '$mod.b = $mod.s == $mod.v;',
  27824. '$mod.b = $mod.v == "foo";',
  27825. '$mod.b = "" == $mod.v;',
  27826. '$mod.b = $mod.v == $mod.d;',
  27827. '$mod.b = $mod.d == $mod.v;',
  27828. '$mod.b = $mod.v == 3.4;',
  27829. '$mod.b = 5.6 == $mod.v;',
  27830. '$mod.b = $mod.v == $mod.c;',
  27831. '$mod.b = $mod.c == $mod.v;',
  27832. '$mod.b = $mod.m == $mod.m;',
  27833. '$mod.b = $mod.v == $mod.m;',
  27834. '$mod.b = $mod.m == $mod.v;',
  27835. '']));
  27836. end;
  27837. procedure TTestModule.TestJSValue_If;
  27838. begin
  27839. StartProgram(false);
  27840. Add([
  27841. 'procedure Fly(var u);',
  27842. 'begin',
  27843. ' if jsvalue(u) then ;',
  27844. 'end;',
  27845. 'var',
  27846. ' v: jsvalue;',
  27847. 'begin',
  27848. ' if v then ;',
  27849. ' while v do ;',
  27850. ' repeat until v;',
  27851. '']);
  27852. ConvertProgram;
  27853. CheckSource('TestJSValue_If',
  27854. LinesToStr([ // statements
  27855. 'this.Fly = function (u) {',
  27856. ' if (u.get()) ;',
  27857. '};',
  27858. 'this.v = undefined;',
  27859. '']),
  27860. LinesToStr([ // $mod.$main
  27861. 'if ($mod.v) ;',
  27862. 'while($mod.v){',
  27863. '};',
  27864. 'do{',
  27865. '} while(!$mod.v);',
  27866. '']));
  27867. end;
  27868. procedure TTestModule.TestJSValue_Not;
  27869. begin
  27870. StartProgram(false);
  27871. Add([
  27872. 'var',
  27873. ' v: jsvalue;',
  27874. ' b: boolean;',
  27875. 'begin',
  27876. ' b:=not v;',
  27877. ' if not v then ;',
  27878. ' while not v do ;',
  27879. ' repeat until not v;',
  27880. '']);
  27881. ConvertProgram;
  27882. CheckSource('TestJSValue_If',
  27883. LinesToStr([ // statements
  27884. 'this.v = undefined;',
  27885. 'this.b = false;',
  27886. '']),
  27887. LinesToStr([ // $mod.$main
  27888. '$mod.b=!$mod.v;',
  27889. 'if (!$mod.v) ;',
  27890. 'while(!$mod.v){',
  27891. '};',
  27892. 'do{',
  27893. '} while($mod.v);',
  27894. '']));
  27895. end;
  27896. procedure TTestModule.TestJSValue_Enum;
  27897. begin
  27898. StartProgram(false);
  27899. Add('type');
  27900. Add(' TColor = (red, blue);');
  27901. Add(' TRedBlue = TColor;');
  27902. Add('var');
  27903. Add(' v: jsvalue;');
  27904. Add(' e: TColor;');
  27905. Add('begin');
  27906. Add(' v:=e;');
  27907. Add(' v:=TColor(e);');
  27908. Add(' v:=TRedBlue(e);');
  27909. Add(' e:=TColor(v);');
  27910. Add(' e:=TRedBlue(v);');
  27911. ConvertProgram;
  27912. CheckSource('TestJSValue_Enum',
  27913. LinesToStr([ // statements
  27914. 'this.TColor = {',
  27915. ' "0": "red",',
  27916. ' red: 0,',
  27917. ' "1": "blue",',
  27918. ' blue: 1',
  27919. '};',
  27920. 'this.v = undefined;',
  27921. 'this.e = 0;',
  27922. '']),
  27923. LinesToStr([ // $mod.$main
  27924. '$mod.v = $mod.e;',
  27925. '$mod.v = $mod.e;',
  27926. '$mod.v = $mod.e;',
  27927. '$mod.e = $mod.v;',
  27928. '$mod.e = $mod.v;',
  27929. '']));
  27930. end;
  27931. procedure TTestModule.TestJSValue_ClassInstance;
  27932. begin
  27933. StartProgram(false);
  27934. Add([
  27935. 'type',
  27936. ' TObject = class',
  27937. ' end;',
  27938. ' TBirdObject = TObject;',
  27939. 'var',
  27940. ' v: jsvalue;',
  27941. ' o: TObject;',
  27942. 'begin',
  27943. ' v:=o;',
  27944. ' v:=TObject(o);',
  27945. ' v:=TBirdObject(o);',
  27946. ' o:=TObject(v);',
  27947. ' o:=TBirdObject(v);',
  27948. ' if v is TObject then ;',
  27949. '']);
  27950. ConvertProgram;
  27951. CheckSource('TestJSValue_ClassInstance',
  27952. LinesToStr([ // statements
  27953. 'rtl.createClass(this, "TObject", null, function () {',
  27954. ' this.$init = function () {',
  27955. ' };',
  27956. ' this.$final = function () {',
  27957. ' };',
  27958. '});',
  27959. 'this.v = undefined;',
  27960. 'this.o = null;',
  27961. '']),
  27962. LinesToStr([ // $mod.$main
  27963. '$mod.v = $mod.o;',
  27964. '$mod.v = $mod.o;',
  27965. '$mod.v = $mod.o;',
  27966. '$mod.o = rtl.getObject($mod.v);',
  27967. '$mod.o = rtl.getObject($mod.v);',
  27968. 'if (rtl.isExt($mod.v, $mod.TObject, 1)) ;',
  27969. '']));
  27970. end;
  27971. procedure TTestModule.TestJSValue_ClassOf;
  27972. begin
  27973. StartProgram(false);
  27974. Add([
  27975. 'type',
  27976. ' TClass = class of TObject;',
  27977. ' TObject = class',
  27978. ' end;',
  27979. ' TBirds = class of TBird;',
  27980. ' TBird = class(TObject) end;',
  27981. 'var',
  27982. ' v: jsvalue;',
  27983. ' c: TClass;',
  27984. 'begin',
  27985. ' v:=c;',
  27986. ' v:=TObject;',
  27987. ' v:=TClass(c);',
  27988. ' v:=TBirds(c);',
  27989. ' c:=TClass(v);',
  27990. ' c:=TBirds(v);',
  27991. ' if v is TClass then ;',
  27992. '']);
  27993. ConvertProgram;
  27994. CheckSource('TestJSValue_ClassOf',
  27995. LinesToStr([ // statements
  27996. 'rtl.createClass(this, "TObject", null, function () {',
  27997. ' this.$init = function () {',
  27998. ' };',
  27999. ' this.$final = function () {',
  28000. ' };',
  28001. '});',
  28002. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  28003. '});',
  28004. 'this.v = undefined;',
  28005. 'this.c = null;',
  28006. '']),
  28007. LinesToStr([ // $mod.$main
  28008. '$mod.v = $mod.c;',
  28009. '$mod.v = $mod.TObject;',
  28010. '$mod.v = $mod.c;',
  28011. '$mod.v = $mod.c;',
  28012. '$mod.c = rtl.getObject($mod.v);',
  28013. '$mod.c = rtl.getObject($mod.v);',
  28014. 'if (rtl.isExt($mod.v, $mod.TObject, 2)) ;',
  28015. '']));
  28016. end;
  28017. procedure TTestModule.TestJSValue_ArrayOfJSValue;
  28018. begin
  28019. StartProgram(false);
  28020. Add([
  28021. 'type',
  28022. ' integer = longint;',
  28023. ' TArray = array of JSValue;',
  28024. ' TArrgh = tarray;',
  28025. ' TArrInt = array of integer;',
  28026. 'var',
  28027. ' v: jsvalue;',
  28028. ' TheArray: tarray = (1,''2'');',
  28029. ' Arr: tarrgh;',
  28030. ' i: integer;',
  28031. ' ArrInt: tarrint;',
  28032. 'begin',
  28033. ' arr:=thearray;',
  28034. ' thearray:=arr;',
  28035. ' setlength(arr,2);',
  28036. ' setlength(thearray,3);',
  28037. ' arr[4]:=v;',
  28038. ' arr[5]:=length(thearray);',
  28039. ' arr[6]:=nil;',
  28040. ' arr[7]:=thearray[8];',
  28041. ' arr[low(arr)]:=high(thearray);',
  28042. ' arr:=arrint;',
  28043. ' arrInt:=tarrint(arr);',
  28044. ' if TheArray = nil then ;',
  28045. ' if nil = TheArray then ;',
  28046. ' if TheArray <> nil then ;',
  28047. ' if nil <> TheArray then ;',
  28048. '']);
  28049. ConvertProgram;
  28050. CheckSource('TestJSValue_ArrayOfJSValue',
  28051. LinesToStr([ // statements
  28052. 'this.v = undefined;',
  28053. 'this.TheArray = [1, "2"];',
  28054. 'this.Arr = [];',
  28055. 'this.i = 0;',
  28056. 'this.ArrInt = [];',
  28057. '']),
  28058. LinesToStr([ // $mod.$main
  28059. '$mod.Arr = rtl.arrayRef($mod.TheArray);',
  28060. '$mod.TheArray = rtl.arrayRef($mod.Arr);',
  28061. '$mod.Arr = rtl.arraySetLength($mod.Arr,undefined,2);',
  28062. '$mod.TheArray = rtl.arraySetLength($mod.TheArray,undefined,3);',
  28063. '$mod.Arr[4] = $mod.v;',
  28064. '$mod.Arr[5] = rtl.length($mod.TheArray);',
  28065. '$mod.Arr[6] = null;',
  28066. '$mod.Arr[7] = $mod.TheArray[8];',
  28067. '$mod.Arr[0] = rtl.length($mod.TheArray) - 1;',
  28068. '$mod.Arr = rtl.arrayRef($mod.ArrInt);',
  28069. '$mod.ArrInt = $mod.Arr;',
  28070. 'if (rtl.length($mod.TheArray) === 0) ;',
  28071. 'if (rtl.length($mod.TheArray) === 0) ;',
  28072. 'if (rtl.length($mod.TheArray) > 0) ;',
  28073. 'if (rtl.length($mod.TheArray) > 0) ;',
  28074. '']));
  28075. end;
  28076. procedure TTestModule.TestJSValue_ArrayLit;
  28077. begin
  28078. StartProgram(false);
  28079. Add([
  28080. 'type',
  28081. ' TFlag = (big,small);',
  28082. ' TArray = array of JSValue;',
  28083. ' TObject = class end;',
  28084. ' TClass = class of TObject;',
  28085. 'var',
  28086. ' v: jsvalue;',
  28087. ' a: TArray;',
  28088. ' o: TObject;',
  28089. 'begin',
  28090. ' a:=[];',
  28091. ' a:=[1];',
  28092. ' a:=[1,2];',
  28093. ' a:=[big];',
  28094. ' a:=[1,big];',
  28095. ' a:=[o,nil];',
  28096. '']);
  28097. ConvertProgram;
  28098. CheckSource('TestJSValue_ArrayLit',
  28099. LinesToStr([ // statements
  28100. 'this.TFlag = {',
  28101. ' "0": "big",',
  28102. ' big: 0,',
  28103. ' "1": "small",',
  28104. ' small: 1',
  28105. '};',
  28106. 'rtl.createClass(this, "TObject", null, function () {',
  28107. ' this.$init = function () {',
  28108. ' };',
  28109. ' this.$final = function () {',
  28110. ' };',
  28111. '});',
  28112. 'this.v = undefined;',
  28113. 'this.a = [];',
  28114. 'this.o = null;',
  28115. '']),
  28116. LinesToStr([ // $mod.$main
  28117. '$mod.a = [];',
  28118. '$mod.a = [1];',
  28119. '$mod.a = [1, 2];',
  28120. '$mod.a = [$mod.TFlag.big];',
  28121. '$mod.a = [1, $mod.TFlag.big];',
  28122. '$mod.a = [$mod.o, null];',
  28123. '']));
  28124. end;
  28125. procedure TTestModule.TestJSValue_Params;
  28126. begin
  28127. StartProgram(false);
  28128. Add('type');
  28129. Add(' integer = longint;');
  28130. Add(' TYesNo = boolean;');
  28131. Add(' TFloat = double;');
  28132. Add(' TCaption = string;');
  28133. Add(' TChar = char;');
  28134. Add('function DoIt(a: jsvalue; const b: jsvalue; var c: jsvalue; out d: jsvalue): jsvalue;');
  28135. Add('var');
  28136. Add(' l: jsvalue;');
  28137. Add('begin');
  28138. Add(' a:=a;');
  28139. Add(' l:=b;');
  28140. Add(' c:=c;');
  28141. Add(' d:=d;');
  28142. Add(' Result:=l;');
  28143. Add('end;');
  28144. Add('function DoSome(a: jsvalue; const b: jsvalue): jsvalue; begin end;');
  28145. Add('var');
  28146. Add(' v: jsvalue;');
  28147. Add(' i: integer;');
  28148. Add(' b: TYesNo;');
  28149. Add(' d: TFloat;');
  28150. Add(' s: TCaption;');
  28151. Add(' c: TChar;');
  28152. Add('begin');
  28153. Add(' v:=doit(v,v,v,v);');
  28154. Add(' i:=integer(dosome(i,i));');
  28155. Add(' b:=TYesNo(dosome(b,b));');
  28156. Add(' d:=TFloat(dosome(d,d));');
  28157. Add(' s:=TCaption(dosome(s,s));');
  28158. Add(' c:=TChar(dosome(c,c));');
  28159. ConvertProgram;
  28160. CheckSource('TestJSValue_Params',
  28161. LinesToStr([ // statements
  28162. 'this.DoIt = function (a, b, c, d) {',
  28163. ' var Result = undefined;',
  28164. ' var l = undefined;',
  28165. ' a = a;',
  28166. ' l = b;',
  28167. ' c.set(c.get());',
  28168. ' d.set(d.get());',
  28169. ' Result = l;',
  28170. ' return Result;',
  28171. '};',
  28172. 'this.DoSome = function (a, b) {',
  28173. ' var Result = undefined;',
  28174. ' return Result;',
  28175. '};',
  28176. 'this.v = undefined;',
  28177. 'this.i = 0;',
  28178. 'this.b = false;',
  28179. 'this.d = 0.0;',
  28180. 'this.s = "";',
  28181. 'this.c = "";',
  28182. '']),
  28183. LinesToStr([ // $mod.$main
  28184. '$mod.v = $mod.DoIt($mod.v, $mod.v, {',
  28185. ' p: $mod,',
  28186. ' get: function () {',
  28187. ' return this.p.v;',
  28188. ' },',
  28189. ' set: function (v) {',
  28190. ' this.p.v = v;',
  28191. ' }',
  28192. '}, {',
  28193. ' p: $mod,',
  28194. ' get: function () {',
  28195. ' return this.p.v;',
  28196. ' },',
  28197. ' set: function (v) {',
  28198. ' this.p.v = v;',
  28199. ' }',
  28200. '});',
  28201. '$mod.i = rtl.trunc($mod.DoSome($mod.i, $mod.i));',
  28202. '$mod.b = !($mod.DoSome($mod.b, $mod.b) == false);',
  28203. '$mod.d = rtl.getNumber($mod.DoSome($mod.d, $mod.d));',
  28204. '$mod.s = "" + $mod.DoSome($mod.s, $mod.s);',
  28205. '$mod.c = rtl.getChar($mod.DoSome($mod.c, $mod.c));',
  28206. '']));
  28207. end;
  28208. procedure TTestModule.TestJSValue_UntypedParam;
  28209. begin
  28210. StartProgram(false);
  28211. Add('function DoIt(const a; var b; out c): jsvalue;');
  28212. Add('begin');
  28213. Add(' Result:=a;');
  28214. Add(' Result:=b;');
  28215. Add(' Result:=c;');
  28216. Add(' b:=Result;');
  28217. Add(' c:=Result;');
  28218. Add('end;');
  28219. Add('var i: longint;');
  28220. Add('begin');
  28221. Add(' doit(i,i,i);');
  28222. ConvertProgram;
  28223. CheckSource('TestJSValue_UntypedParam',
  28224. LinesToStr([ // statements
  28225. 'this.DoIt = function (a, b, c) {',
  28226. ' var Result = undefined;',
  28227. ' Result = a;',
  28228. ' Result = b.get();',
  28229. ' Result = c.get();',
  28230. ' b.set(Result);',
  28231. ' c.set(Result);',
  28232. ' return Result;',
  28233. '};',
  28234. 'this.i = 0;',
  28235. '']),
  28236. LinesToStr([ // $mod.$main
  28237. '$mod.DoIt($mod.i, {',
  28238. ' p: $mod,',
  28239. ' get: function () {',
  28240. ' return this.p.i;',
  28241. ' },',
  28242. ' set: function (v) {',
  28243. ' this.p.i = v;',
  28244. ' }',
  28245. '}, {',
  28246. ' p: $mod,',
  28247. ' get: function () {',
  28248. ' return this.p.i;',
  28249. ' },',
  28250. ' set: function (v) {',
  28251. ' this.p.i = v;',
  28252. ' }',
  28253. '});',
  28254. '']));
  28255. end;
  28256. procedure TTestModule.TestJSValue_FuncResultType;
  28257. begin
  28258. StartProgram(false);
  28259. Add('type');
  28260. Add(' integer = longint;');
  28261. Add(' TJSValueArray = array of JSValue;');
  28262. Add(' TListSortCompare = function(Item1, Item2: JSValue): Integer;');
  28263. Add('procedure Sort(P: JSValue; aList: TJSValueArray; const Compare: TListSortCompare);');
  28264. Add('begin');
  28265. Add(' while Compare(P,aList[0])>0 do ;');
  28266. Add('end;');
  28267. Add('var');
  28268. Add(' Compare: TListSortCompare;');
  28269. Add(' V: JSValue;');
  28270. Add(' i: integer;');
  28271. Add('begin');
  28272. Add(' if Compare(V,V)>0 then ;');
  28273. Add(' if Compare(i,i)>1 then ;');
  28274. Add(' if Compare(nil,false)>2 then ;');
  28275. Add(' if Compare(1,true)>3 then ;');
  28276. ConvertProgram;
  28277. CheckSource('TestJSValue_UntypedParam',
  28278. LinesToStr([ // statements
  28279. 'this.Sort = function (P, aList, Compare) {',
  28280. ' while (Compare(P, aList[0]) > 0) {',
  28281. ' };',
  28282. '};',
  28283. 'this.Compare = null;',
  28284. 'this.V = undefined;',
  28285. 'this.i = 0;',
  28286. '']),
  28287. LinesToStr([ // $mod.$main
  28288. 'if ($mod.Compare($mod.V, $mod.V) > 0) ;',
  28289. 'if ($mod.Compare($mod.i, $mod.i) > 1) ;',
  28290. 'if ($mod.Compare(null, false) > 2) ;',
  28291. 'if ($mod.Compare(1, true) > 3) ;',
  28292. '']));
  28293. end;
  28294. procedure TTestModule.TestJSValue_ProcType_Assign;
  28295. begin
  28296. StartProgram(false);
  28297. Add('type');
  28298. Add(' integer = longint;');
  28299. Add(' TObject = class');
  28300. Add(' class function GetGlob: integer;');
  28301. Add(' function Getter: integer;');
  28302. Add(' end;');
  28303. Add('class function TObject.GetGlob: integer;');
  28304. Add('var v1: jsvalue;');
  28305. Add('begin');
  28306. Add(' v1:=@GetGlob;');
  28307. Add(' v1:[email protected];');
  28308. Add('end;');
  28309. Add('function TObject.Getter: integer;');
  28310. Add('var v2: jsvalue;');
  28311. Add('begin');
  28312. Add(' v2:=@Getter;');
  28313. Add(' v2:[email protected];');
  28314. Add(' v2:=@GetGlob;');
  28315. Add(' v2:[email protected];');
  28316. Add('end;');
  28317. Add('function GetIt(i: integer): integer;');
  28318. Add('var v3: jsvalue;');
  28319. Add('begin');
  28320. Add(' v3:=@GetIt;');
  28321. Add('end;');
  28322. Add('var');
  28323. Add(' V: JSValue;');
  28324. Add(' o: TObject;');
  28325. Add('begin');
  28326. Add(' v:=@GetIt;');
  28327. Add(' v:[email protected];');
  28328. Add(' v:[email protected];');
  28329. ConvertProgram;
  28330. CheckSource('TestJSValue_ProcType_Assign',
  28331. LinesToStr([ // statements
  28332. 'rtl.createClass(this, "TObject", null, function () {',
  28333. ' this.$init = function () {',
  28334. ' };',
  28335. ' this.$final = function () {',
  28336. ' };',
  28337. ' this.GetGlob = function () {',
  28338. ' var Result = 0;',
  28339. ' var v1 = undefined;',
  28340. ' v1 = rtl.createCallback(this, "GetGlob");',
  28341. ' v1 = rtl.createCallback(this, "GetGlob");',
  28342. ' return Result;',
  28343. ' };',
  28344. ' this.Getter = function () {',
  28345. ' var Result = 0;',
  28346. ' var v2 = undefined;',
  28347. ' v2 = rtl.createCallback(this, "Getter");',
  28348. ' v2 = rtl.createCallback(this, "Getter");',
  28349. ' v2 = rtl.createCallback(this.$class, "GetGlob");',
  28350. ' v2 = rtl.createCallback(this.$class, "GetGlob");',
  28351. ' return Result;',
  28352. ' };',
  28353. '});',
  28354. 'this.GetIt = function (i) {',
  28355. ' var Result = 0;',
  28356. ' var v3 = undefined;',
  28357. ' v3 = $mod.GetIt;',
  28358. ' return Result;',
  28359. '};',
  28360. 'this.V = undefined;',
  28361. 'this.o = null;',
  28362. '']),
  28363. LinesToStr([ // $mod.$main
  28364. '$mod.V = $mod.GetIt;',
  28365. '$mod.V = rtl.createCallback($mod.o, "Getter");',
  28366. '$mod.V = rtl.createCallback($mod.o.$class, "GetGlob");',
  28367. '']));
  28368. end;
  28369. procedure TTestModule.TestJSValue_ProcType_Equal;
  28370. begin
  28371. StartProgram(false);
  28372. Add('type');
  28373. Add(' integer = longint;');
  28374. Add(' TObject = class');
  28375. Add(' class function GetGlob: integer;');
  28376. Add(' function Getter: integer;');
  28377. Add(' end;');
  28378. Add('class function TObject.GetGlob: integer;');
  28379. Add('var v1: jsvalue;');
  28380. Add('begin');
  28381. Add(' if v1=@GetGlob then;');
  28382. Add(' if [email protected] then ;');
  28383. Add('end;');
  28384. Add('function TObject.Getter: integer;');
  28385. Add('var v2: jsvalue;');
  28386. Add('begin');
  28387. Add(' if v2=@Getter then;');
  28388. Add(' if [email protected] then ;');
  28389. Add(' if v2=@GetGlob then;');
  28390. Add(' if [email protected] then;');
  28391. Add('end;');
  28392. Add('function GetIt(i: integer): integer;');
  28393. Add('var v3: jsvalue;');
  28394. Add('begin');
  28395. Add(' if v3=@GetIt then;');
  28396. Add('end;');
  28397. Add('var');
  28398. Add(' V: JSValue;');
  28399. Add(' o: TObject;');
  28400. Add('begin');
  28401. Add(' if v=@GetIt then;');
  28402. Add(' if [email protected] then;');
  28403. Add(' if [email protected] then;');
  28404. Add(' if @GetIt=v then;');
  28405. Add(' if @o.Getter=v then;');
  28406. Add(' if @o.GetGlob=v then;');
  28407. ConvertProgram;
  28408. CheckSource('TestJSValue_ProcType_Equal',
  28409. LinesToStr([ // statements
  28410. 'rtl.createClass(this, "TObject", null, function () {',
  28411. ' this.$init = function () {',
  28412. ' };',
  28413. ' this.$final = function () {',
  28414. ' };',
  28415. ' this.GetGlob = function () {',
  28416. ' var Result = 0;',
  28417. ' var v1 = undefined;',
  28418. ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
  28419. ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
  28420. ' return Result;',
  28421. ' };',
  28422. ' this.Getter = function () {',
  28423. ' var Result = 0;',
  28424. ' var v2 = undefined;',
  28425. ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
  28426. ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
  28427. ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
  28428. ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
  28429. ' return Result;',
  28430. ' };',
  28431. '});',
  28432. 'this.GetIt = function (i) {',
  28433. ' var Result = 0;',
  28434. ' var v3 = undefined;',
  28435. ' if (rtl.eqCallback(v3, $mod.GetIt)) ;',
  28436. ' return Result;',
  28437. '};',
  28438. 'this.V = undefined;',
  28439. 'this.o = null;',
  28440. '']),
  28441. LinesToStr([ // $mod.$main
  28442. 'if (rtl.eqCallback($mod.V, $mod.GetIt)) ;',
  28443. 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o, "Getter"))) ;',
  28444. 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o.$class, "GetGlob"))) ;',
  28445. 'if (rtl.eqCallback($mod.GetIt, $mod.V)) ;',
  28446. 'if (rtl.eqCallback(rtl.createCallback($mod.o, "Getter"), $mod.V)) ;',
  28447. 'if (rtl.eqCallback(rtl.createCallback($mod.o.$class, "GetGlob"), $mod.V)) ;',
  28448. '']));
  28449. end;
  28450. procedure TTestModule.TestJSValue_ProcType_Param;
  28451. begin
  28452. StartProgram(false);
  28453. Add([
  28454. 'type',
  28455. ' variant = jsvalue;',
  28456. ' TArrVariant = array of variant;',
  28457. ' TArrVar2 = TArrVariant;',
  28458. ' TFuncInt = function: longint;',
  28459. 'function GetIt: longint;',
  28460. 'begin',
  28461. 'end;',
  28462. 'procedure DoIt(p: jsvalue; Arr: TArrVar2);',
  28463. 'var v: variant;',
  28464. 'begin',
  28465. ' v:=arr[1];',
  28466. 'end;',
  28467. 'var s: string;',
  28468. 'begin',
  28469. ' DoIt(GetIt,[]);',
  28470. ' DoIt(@GetIt,[]);',
  28471. ' DoIt(1,[s,GetIt]);',
  28472. ' DoIt(1,[s,@GetIt]);',
  28473. '']);
  28474. ConvertProgram;
  28475. CheckSource('TestJSValue_ProcType_Param',
  28476. LinesToStr([ // statements
  28477. 'this.GetIt = function () {',
  28478. ' var Result = 0;',
  28479. ' return Result;',
  28480. '};',
  28481. 'this.DoIt = function (p, Arr) {',
  28482. ' var v = undefined;',
  28483. ' v = Arr[1];',
  28484. '};',
  28485. 'this.s = "";',
  28486. '']),
  28487. LinesToStr([ // $mod.$main
  28488. '$mod.DoIt($mod.GetIt(), []);',
  28489. '$mod.DoIt($mod.GetIt, []);',
  28490. '$mod.DoIt(1, [$mod.s, $mod.GetIt()]);',
  28491. '$mod.DoIt(1, [$mod.s, $mod.GetIt]);',
  28492. '']));
  28493. end;
  28494. procedure TTestModule.TestJSValue_AssignToPointerFail;
  28495. begin
  28496. StartProgram(false);
  28497. Add([
  28498. 'var',
  28499. ' v: JSValue;',
  28500. ' p: Pointer;',
  28501. 'begin',
  28502. ' p:=v;',
  28503. '']);
  28504. SetExpectedPasResolverError('Incompatible types: got "JSValue" expected "Pointer"',
  28505. nIncompatibleTypesGotExpected);
  28506. ConvertProgram;
  28507. end;
  28508. procedure TTestModule.TestJSValue_OverloadDouble;
  28509. begin
  28510. StartProgram(false);
  28511. Add([
  28512. 'type',
  28513. ' integer = longint;',
  28514. ' tdatetime = double;',
  28515. 'procedure DoIt(d: double); begin end;',
  28516. 'procedure DoIt(v: jsvalue); begin end;',
  28517. 'var',
  28518. ' d: double;',
  28519. ' dt: tdatetime;',
  28520. ' i: integer;',
  28521. ' b: byte;',
  28522. ' shi: shortint;',
  28523. ' w: word;',
  28524. ' smi: smallint;',
  28525. ' lw: longword;',
  28526. ' li: longint;',
  28527. ' ni: nativeint;',
  28528. ' nu: nativeuint;',
  28529. 'begin',
  28530. ' DoIt(d);',
  28531. ' DoIt(dt);',
  28532. ' DoIt(i);',
  28533. ' DoIt(b);',
  28534. ' DoIt(shi);',
  28535. ' DoIt(w);',
  28536. ' DoIt(smi);',
  28537. ' DoIt(lw);',
  28538. ' DoIt(li);',
  28539. ' DoIt(ni);',
  28540. ' DoIt(nu);',
  28541. '']);
  28542. ConvertProgram;
  28543. CheckSource('TestJSValue_OverloadDouble',
  28544. LinesToStr([ // statements
  28545. 'this.DoIt = function (d) {',
  28546. '};',
  28547. 'this.DoIt$1 = function (v) {',
  28548. '};',
  28549. 'this.d = 0.0;',
  28550. 'this.dt = 0.0;',
  28551. 'this.i = 0;',
  28552. 'this.b = 0;',
  28553. 'this.shi = 0;',
  28554. 'this.w = 0;',
  28555. 'this.smi = 0;',
  28556. 'this.lw = 0;',
  28557. 'this.li = 0;',
  28558. 'this.ni = 0;',
  28559. 'this.nu = 0;',
  28560. '']),
  28561. LinesToStr([ // $mod.$main
  28562. '$mod.DoIt($mod.d);',
  28563. '$mod.DoIt($mod.dt);',
  28564. '$mod.DoIt$1($mod.i);',
  28565. '$mod.DoIt$1($mod.b);',
  28566. '$mod.DoIt$1($mod.shi);',
  28567. '$mod.DoIt$1($mod.w);',
  28568. '$mod.DoIt$1($mod.smi);',
  28569. '$mod.DoIt$1($mod.lw);',
  28570. '$mod.DoIt$1($mod.li);',
  28571. '$mod.DoIt$1($mod.ni);',
  28572. '$mod.DoIt$1($mod.nu);',
  28573. '']));
  28574. end;
  28575. procedure TTestModule.TestJSValue_OverloadNativeInt;
  28576. begin
  28577. StartProgram(false);
  28578. Add([
  28579. 'type',
  28580. ' integer = longint;',
  28581. ' int53 = nativeint;',
  28582. ' tdatetime = double;',
  28583. 'procedure DoIt(n: nativeint); begin end;',
  28584. 'procedure DoIt(v: jsvalue); begin end;',
  28585. 'var',
  28586. ' d: double;',
  28587. ' dt: tdatetime;',
  28588. ' i: integer;',
  28589. ' b: byte;',
  28590. ' shi: shortint;',
  28591. ' w: word;',
  28592. ' smi: smallint;',
  28593. ' lw: longword;',
  28594. ' li: longint;',
  28595. ' ni: nativeint;',
  28596. ' nu: nativeuint;',
  28597. 'begin',
  28598. ' DoIt(d);',
  28599. ' DoIt(dt);',
  28600. ' DoIt(i);',
  28601. ' DoIt(b);',
  28602. ' DoIt(shi);',
  28603. ' DoIt(w);',
  28604. ' DoIt(smi);',
  28605. ' DoIt(lw);',
  28606. ' DoIt(li);',
  28607. ' DoIt(ni);',
  28608. ' DoIt(nu);',
  28609. '']);
  28610. ConvertProgram;
  28611. CheckSource('TestJSValue_OverloadNativeInt',
  28612. LinesToStr([ // statements
  28613. 'this.DoIt = function (n) {',
  28614. '};',
  28615. 'this.DoIt$1 = function (v) {',
  28616. '};',
  28617. 'this.d = 0.0;',
  28618. 'this.dt = 0.0;',
  28619. 'this.i = 0;',
  28620. 'this.b = 0;',
  28621. 'this.shi = 0;',
  28622. 'this.w = 0;',
  28623. 'this.smi = 0;',
  28624. 'this.lw = 0;',
  28625. 'this.li = 0;',
  28626. 'this.ni = 0;',
  28627. 'this.nu = 0;',
  28628. '']),
  28629. LinesToStr([ // $mod.$main
  28630. '$mod.DoIt$1($mod.d);',
  28631. '$mod.DoIt$1($mod.dt);',
  28632. '$mod.DoIt($mod.i);',
  28633. '$mod.DoIt($mod.b);',
  28634. '$mod.DoIt($mod.shi);',
  28635. '$mod.DoIt($mod.w);',
  28636. '$mod.DoIt($mod.smi);',
  28637. '$mod.DoIt($mod.lw);',
  28638. '$mod.DoIt($mod.li);',
  28639. '$mod.DoIt($mod.ni);',
  28640. '$mod.DoIt($mod.nu);',
  28641. '']));
  28642. end;
  28643. procedure TTestModule.TestJSValue_OverloadWord;
  28644. begin
  28645. StartProgram(false);
  28646. Add([
  28647. 'type',
  28648. ' integer = longint;',
  28649. ' int53 = nativeint;',
  28650. ' tdatetime = double;',
  28651. 'procedure DoIt(w: word); begin end;',
  28652. 'procedure DoIt(v: jsvalue); begin end;',
  28653. 'var',
  28654. ' d: double;',
  28655. ' dt: tdatetime;',
  28656. ' i: integer;',
  28657. ' b: byte;',
  28658. ' shi: shortint;',
  28659. ' w: word;',
  28660. ' smi: smallint;',
  28661. ' lw: longword;',
  28662. ' li: longint;',
  28663. ' ni: nativeint;',
  28664. ' nu: nativeuint;',
  28665. 'begin',
  28666. ' DoIt(d);',
  28667. ' DoIt(dt);',
  28668. ' DoIt(i);',
  28669. ' DoIt(b);',
  28670. ' DoIt(shi);',
  28671. ' DoIt(w);',
  28672. ' DoIt(smi);',
  28673. ' DoIt(lw);',
  28674. ' DoIt(li);',
  28675. ' DoIt(ni);',
  28676. ' DoIt(nu);',
  28677. '']);
  28678. ConvertProgram;
  28679. CheckSource('TestJSValue_OverloadWord',
  28680. LinesToStr([ // statements
  28681. 'this.DoIt = function (w) {',
  28682. '};',
  28683. 'this.DoIt$1 = function (v) {',
  28684. '};',
  28685. 'this.d = 0.0;',
  28686. 'this.dt = 0.0;',
  28687. 'this.i = 0;',
  28688. 'this.b = 0;',
  28689. 'this.shi = 0;',
  28690. 'this.w = 0;',
  28691. 'this.smi = 0;',
  28692. 'this.lw = 0;',
  28693. 'this.li = 0;',
  28694. 'this.ni = 0;',
  28695. 'this.nu = 0;',
  28696. '']),
  28697. LinesToStr([ // $mod.$main
  28698. '$mod.DoIt$1($mod.d);',
  28699. '$mod.DoIt$1($mod.dt);',
  28700. '$mod.DoIt$1($mod.i);',
  28701. '$mod.DoIt($mod.b);',
  28702. '$mod.DoIt($mod.shi);',
  28703. '$mod.DoIt($mod.w);',
  28704. '$mod.DoIt$1($mod.smi);',
  28705. '$mod.DoIt$1($mod.lw);',
  28706. '$mod.DoIt$1($mod.li);',
  28707. '$mod.DoIt$1($mod.ni);',
  28708. '$mod.DoIt$1($mod.nu);',
  28709. '']));
  28710. end;
  28711. procedure TTestModule.TestJSValue_OverloadString;
  28712. begin
  28713. StartProgram(false);
  28714. Add([
  28715. 'type',
  28716. ' uni = string;',
  28717. ' WChar = char;',
  28718. 'procedure DoIt(s: string); begin end;',
  28719. 'procedure DoIt(v: jsvalue); begin end;',
  28720. 'var',
  28721. ' s: string;',
  28722. ' c: char;',
  28723. ' u: uni;',
  28724. 'begin',
  28725. ' DoIt(s);',
  28726. ' DoIt(c);',
  28727. ' DoIt(u);',
  28728. '']);
  28729. ConvertProgram;
  28730. CheckSource('TestJSValue_OverloadString',
  28731. LinesToStr([ // statements
  28732. 'this.DoIt = function (s) {',
  28733. '};',
  28734. 'this.DoIt$1 = function (v) {',
  28735. '};',
  28736. 'this.s = "";',
  28737. 'this.c = "";',
  28738. 'this.u = "";',
  28739. '']),
  28740. LinesToStr([ // $mod.$main
  28741. '$mod.DoIt($mod.s);',
  28742. '$mod.DoIt($mod.c);',
  28743. '$mod.DoIt($mod.u);',
  28744. '']));
  28745. end;
  28746. procedure TTestModule.TestJSValue_OverloadChar;
  28747. begin
  28748. StartProgram(false);
  28749. Add([
  28750. 'type',
  28751. ' uni = string;',
  28752. ' WChar = char;',
  28753. 'procedure DoIt(c: char); begin end;',
  28754. 'procedure DoIt(v: jsvalue); begin end;',
  28755. 'var',
  28756. ' s: string;',
  28757. ' c: char;',
  28758. ' u: uni;',
  28759. 'begin',
  28760. ' DoIt(s);',
  28761. ' DoIt(c);',
  28762. ' DoIt(u);',
  28763. '']);
  28764. ConvertProgram;
  28765. CheckSource('TestJSValue_OverloadChar',
  28766. LinesToStr([ // statements
  28767. 'this.DoIt = function (c) {',
  28768. '};',
  28769. 'this.DoIt$1 = function (v) {',
  28770. '};',
  28771. 'this.s = "";',
  28772. 'this.c = "";',
  28773. 'this.u = "";',
  28774. '']),
  28775. LinesToStr([ // $mod.$main
  28776. '$mod.DoIt$1($mod.s);',
  28777. '$mod.DoIt($mod.c);',
  28778. '$mod.DoIt$1($mod.u);',
  28779. '']));
  28780. end;
  28781. procedure TTestModule.TestJSValue_OverloadPointer;
  28782. begin
  28783. StartProgram(false);
  28784. Add([
  28785. 'type',
  28786. ' TObject = class end;',
  28787. 'procedure DoIt(p: pointer); begin end;',
  28788. 'procedure DoIt(v: jsvalue); begin end;',
  28789. 'var',
  28790. ' o: TObject;',
  28791. 'begin',
  28792. ' DoIt(o);',
  28793. '']);
  28794. ConvertProgram;
  28795. CheckSource('TestJSValue_OverloadPointer',
  28796. LinesToStr([ // statements
  28797. 'rtl.createClass(this, "TObject", null, function () {',
  28798. ' this.$init = function () {',
  28799. ' };',
  28800. ' this.$final = function () {',
  28801. ' };',
  28802. '});',
  28803. 'this.DoIt = function (p) {',
  28804. '};',
  28805. 'this.DoIt$1 = function (v) {',
  28806. '};',
  28807. 'this.o = null;',
  28808. '']),
  28809. LinesToStr([ // $mod.$main
  28810. '$mod.DoIt($mod.o);',
  28811. '']));
  28812. end;
  28813. procedure TTestModule.TestJSValue_ForIn;
  28814. begin
  28815. StartProgram(false);
  28816. Add([
  28817. 'var',
  28818. ' v: JSValue;',
  28819. ' key: string;',
  28820. 'begin',
  28821. ' for key in v do begin',
  28822. ' if key=''abc'' then ;',
  28823. ' end;',
  28824. '']);
  28825. ConvertProgram;
  28826. CheckSource('TestJSValue_ForIn',
  28827. LinesToStr([ // statements
  28828. 'this.v = undefined;',
  28829. 'this.key = "";',
  28830. '']),
  28831. LinesToStr([ // $mod.$main
  28832. 'for ($mod.key in $mod.v) {',
  28833. ' if ($mod.key === "abc") ;',
  28834. '};',
  28835. '']));
  28836. end;
  28837. procedure TTestModule.TestRTTI_IntRange;
  28838. begin
  28839. WithTypeInfo:=true;
  28840. StartProgram(true,[supTypeInfo]);
  28841. Add([
  28842. '{$modeswitch externalclass}',
  28843. 'type',
  28844. ' TGraphicsColor = -$7FFFFFFF-1..$7FFFFFFF;',
  28845. ' TColor = type TGraphicsColor;',
  28846. 'var',
  28847. ' p: TTypeInfo;',
  28848. ' k: TTypeKind;',
  28849. 'begin',
  28850. ' p:=typeinfo(TGraphicsColor);',
  28851. ' p:=typeinfo(TColor);',
  28852. ' k:=GetTypeKind(TGraphicsColor);',
  28853. ' k:=GetTypeKind(TColor);',
  28854. '']);
  28855. ConvertProgram;
  28856. CheckSource('TestRTTI_IntRange',
  28857. LinesToStr([ // statements
  28858. 'this.$rtti.$Int("TGraphicsColor", {',
  28859. ' minvalue: -2147483648,',
  28860. ' maxvalue: 2147483647,',
  28861. ' ordtype: 4',
  28862. '});',
  28863. 'this.$rtti.$inherited("TColor", this.$rtti["TGraphicsColor"], {});',
  28864. 'this.p = null;',
  28865. 'this.k = 0;',
  28866. '']),
  28867. LinesToStr([ // $mod.$main
  28868. '$mod.p = $mod.$rtti["TGraphicsColor"];',
  28869. '$mod.p = $mod.$rtti["TColor"];',
  28870. '$mod.k = 1;',
  28871. '$mod.k = 1;',
  28872. '']));
  28873. end;
  28874. procedure TTestModule.TestRTTI_Double;
  28875. begin
  28876. WithTypeInfo:=true;
  28877. StartProgram(true,[supTypeInfo]);
  28878. Add([
  28879. '{$modeswitch externalclass}',
  28880. 'type',
  28881. ' TFloat = type double;',
  28882. 'var',
  28883. ' p: TTypeInfo;',
  28884. 'begin',
  28885. ' p:=typeinfo(double);',
  28886. ' p:=typeinfo(TFloat);',
  28887. '']);
  28888. ConvertProgram;
  28889. CheckSource('TestRTTI_Double',
  28890. LinesToStr([ // statements
  28891. 'this.$rtti.$inherited("TFloat", rtl.double, {});',
  28892. 'this.p = null;',
  28893. '']),
  28894. LinesToStr([ // $mod.$main
  28895. '$mod.p = rtl.double;',
  28896. '$mod.p = $mod.$rtti["TFloat"];',
  28897. '']));
  28898. end;
  28899. procedure TTestModule.TestRTTI_ProcType;
  28900. begin
  28901. WithTypeInfo:=true;
  28902. StartProgram(false);
  28903. Add('type');
  28904. Add(' TProcA = procedure;');
  28905. Add(' TMethodB = procedure of object;');
  28906. Add(' TProcC = procedure; varargs;');
  28907. Add(' TProcD = procedure(i: longint; const j: string; var c: char; out d: double);');
  28908. Add(' TProcE = function: nativeint;');
  28909. Add(' TProcF = function(const p: TProcA): nativeuint;');
  28910. Add('var p: pointer;');
  28911. Add('begin');
  28912. Add(' p:=typeinfo(tproca);');
  28913. ConvertProgram;
  28914. CheckSource('TestRTTI_ProcType',
  28915. LinesToStr([ // statements
  28916. 'this.$rtti.$ProcVar("TProcA", {',
  28917. ' procsig: rtl.newTIProcSig([])',
  28918. '});',
  28919. 'this.$rtti.$MethodVar("TMethodB", {',
  28920. ' procsig: rtl.newTIProcSig([]),',
  28921. ' methodkind: 0',
  28922. '});',
  28923. 'this.$rtti.$ProcVar("TProcC", {',
  28924. ' procsig: rtl.newTIProcSig([], null, 2)',
  28925. '});',
  28926. 'this.$rtti.$ProcVar("TProcD", {',
  28927. ' procsig: rtl.newTIProcSig([["i", rtl.longint], ["j", rtl.string, 2], ["c", rtl.char, 1], ["d", rtl.double, 4]])',
  28928. '});',
  28929. 'this.$rtti.$ProcVar("TProcE", {',
  28930. ' procsig: rtl.newTIProcSig([], rtl.nativeint)',
  28931. '});',
  28932. 'this.$rtti.$ProcVar("TProcF", {',
  28933. ' procsig: rtl.newTIProcSig([["p", this.$rtti["TProcA"], 2]], rtl.nativeuint)',
  28934. '});',
  28935. 'this.p = null;',
  28936. '']),
  28937. LinesToStr([ // $mod.$main
  28938. '$mod.p = $mod.$rtti["TProcA"];',
  28939. '']));
  28940. end;
  28941. procedure TTestModule.TestRTTI_ProcType_ArgFromOtherUnit;
  28942. begin
  28943. WithTypeInfo:=true;
  28944. AddModuleWithIntfImplSrc('unit2.pas',
  28945. LinesToStr([
  28946. 'type',
  28947. ' TObject = class end;'
  28948. ]),
  28949. '');
  28950. StartUnit(true);
  28951. Add('interface');
  28952. Add('uses unit2;');
  28953. Add('type');
  28954. Add(' TProcA = function(o: tobject): tobject;');
  28955. Add('implementation');
  28956. Add('type');
  28957. Add(' TProcB = function(o: tobject): tobject;');
  28958. Add('var p: Pointer;');
  28959. Add('initialization');
  28960. Add(' p:=typeinfo(tproca);');
  28961. Add(' p:=typeinfo(tprocb);');
  28962. ConvertUnit;
  28963. CheckSource('TestRTTI_ProcType_ArgFromOtherUnit',
  28964. LinesToStr([ // statements
  28965. 'var $impl = $mod.$impl;',
  28966. 'this.$rtti.$ProcVar("TProcA", {',
  28967. ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
  28968. '});',
  28969. '']),
  28970. LinesToStr([ // this.$init
  28971. '$impl.p = $mod.$rtti["TProcA"];',
  28972. '$impl.p = $mod.$rtti["TProcB"];',
  28973. '']),
  28974. LinesToStr([ // implementation
  28975. '$mod.$rtti.$ProcVar("TProcB", {',
  28976. ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
  28977. '});',
  28978. '$impl.p = null;',
  28979. '']) );
  28980. end;
  28981. procedure TTestModule.TestRTTI_EnumAndSetType;
  28982. begin
  28983. WithTypeInfo:=true;
  28984. StartProgram(false);
  28985. Add('type');
  28986. Add(' TFlag = (light,dark);');
  28987. Add(' TFlags = set of TFlag;');
  28988. Add(' TProc = function(f: TFlags): TFlag;');
  28989. Add('var p: pointer;');
  28990. Add('begin');
  28991. Add(' p:=typeinfo(tflag);');
  28992. Add(' p:=typeinfo(tflags);');
  28993. ConvertProgram;
  28994. CheckSource('TestRTTI_EnumAndType',
  28995. LinesToStr([ // statements
  28996. 'this.TFlag = {',
  28997. ' "0": "light",',
  28998. ' light: 0,',
  28999. ' "1": "dark",',
  29000. ' dark: 1',
  29001. '};',
  29002. 'this.$rtti.$Enum("TFlag", {',
  29003. ' minvalue: 0,',
  29004. ' maxvalue: 1,',
  29005. ' ordtype: 1,',
  29006. ' enumtype: this.TFlag',
  29007. '});',
  29008. 'this.$rtti.$Set("TFlags", {',
  29009. ' comptype: this.$rtti["TFlag"]',
  29010. '});',
  29011. 'this.$rtti.$ProcVar("TProc", {',
  29012. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TFlags"]]], this.$rtti["TFlag"])',
  29013. '});',
  29014. 'this.p = null;',
  29015. '']),
  29016. LinesToStr([ // $mod.$main
  29017. '$mod.p = $mod.$rtti["TFlag"];',
  29018. '$mod.p = $mod.$rtti["TFlags"];',
  29019. '']));
  29020. end;
  29021. procedure TTestModule.TestRTTI_EnumRange;
  29022. begin
  29023. WithTypeInfo:=true;
  29024. StartProgram(false);
  29025. Add([
  29026. 'type',
  29027. ' TCol = (red,green,blue);',
  29028. ' TColRg = green..blue;',
  29029. ' TSetOfColRg = set of TColRg;',
  29030. 'var p: pointer;',
  29031. 'begin',
  29032. ' p:=typeinfo(tcolrg);',
  29033. ' p:=typeinfo(tsetofcolrg);',
  29034. '']);
  29035. ConvertProgram;
  29036. end;
  29037. procedure TTestModule.TestRTTI_AnonymousEnumType;
  29038. begin
  29039. WithTypeInfo:=true;
  29040. StartProgram(false);
  29041. Add('type');
  29042. Add(' TFlags = set of (red, green);');
  29043. Add('var');
  29044. Add(' f: TFlags;');
  29045. Add('begin');
  29046. Add(' Include(f,red);');
  29047. ConvertProgram;
  29048. CheckSource('TestRTTI_AnonymousEnumType',
  29049. LinesToStr([ // statements
  29050. 'this.TFlags$a = {',
  29051. ' "0": "red",',
  29052. ' red: 0,',
  29053. ' "1": "green",',
  29054. ' green: 1',
  29055. '};',
  29056. 'this.$rtti.$Enum("TFlags$a", {',
  29057. ' minvalue: 0,',
  29058. ' maxvalue: 1,',
  29059. ' ordtype: 1,',
  29060. ' enumtype: this.TFlags$a',
  29061. '});',
  29062. 'this.$rtti.$Set("TFlags", {',
  29063. ' comptype: this.$rtti["TFlags$a"]',
  29064. '});',
  29065. 'this.f = {};',
  29066. '']),
  29067. LinesToStr([
  29068. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  29069. '']));
  29070. end;
  29071. procedure TTestModule.TestRTTI_StaticArray;
  29072. begin
  29073. WithTypeInfo:=true;
  29074. StartProgram(false);
  29075. Add('type');
  29076. Add(' TFlag = (light,dark);');
  29077. Add(' TFlagNames = array[TFlag] of string;');
  29078. Add(' TBoolNames = array[boolean] of string;');
  29079. Add(' TByteArray = array[1..32768] of byte;');
  29080. Add(' TProc = function(f: TBoolNames): TFlagNames;');
  29081. Add('var p: pointer;');
  29082. Add('begin');
  29083. Add(' p:=typeinfo(TFlagNames);');
  29084. Add(' p:=typeinfo(TBoolNames);');
  29085. ConvertProgram;
  29086. CheckSource('TestRTTI_StaticArray',
  29087. LinesToStr([ // statements
  29088. 'this.TFlag = {',
  29089. ' "0": "light",',
  29090. ' light: 0,',
  29091. ' "1": "dark",',
  29092. ' dark: 1',
  29093. '};',
  29094. 'this.$rtti.$Enum("TFlag", {',
  29095. ' minvalue: 0,',
  29096. ' maxvalue: 1,',
  29097. ' ordtype: 1,',
  29098. ' enumtype: this.TFlag',
  29099. '});',
  29100. 'this.$rtti.$StaticArray("TFlagNames", {',
  29101. ' dims: [2],',
  29102. ' eltype: rtl.string',
  29103. '});',
  29104. 'this.$rtti.$StaticArray("TBoolNames", {',
  29105. ' dims: [2],',
  29106. ' eltype: rtl.string',
  29107. '});',
  29108. 'this.$rtti.$StaticArray("TByteArray", {',
  29109. ' dims: [32768],',
  29110. ' eltype: rtl.byte',
  29111. '});',
  29112. 'this.$rtti.$ProcVar("TProc", {',
  29113. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TBoolNames"]]], this.$rtti["TFlagNames"])',
  29114. '});',
  29115. 'this.p = null;',
  29116. '']),
  29117. LinesToStr([ // $mod.$main
  29118. '$mod.p = $mod.$rtti["TFlagNames"];',
  29119. '$mod.p = $mod.$rtti["TBoolNames"];',
  29120. '']));
  29121. end;
  29122. procedure TTestModule.TestRTTI_DynArray;
  29123. begin
  29124. WithTypeInfo:=true;
  29125. StartProgram(false);
  29126. Add('type');
  29127. Add(' TArrStr = array of string;');
  29128. Add(' TArr2Dim = array of tarrstr;');
  29129. Add(' TProc = function(f: TArrStr): TArr2Dim;');
  29130. Add('var p: pointer;');
  29131. Add('begin');
  29132. Add(' p:=typeinfo(tarrstr);');
  29133. Add(' p:=typeinfo(tarr2dim);');
  29134. ConvertProgram;
  29135. CheckSource('TestRTTI_DynArray',
  29136. LinesToStr([ // statements
  29137. 'this.$rtti.$DynArray("TArrStr", {',
  29138. ' eltype: rtl.string',
  29139. '});',
  29140. 'this.$rtti.$DynArray("TArr2Dim", {',
  29141. ' eltype: this.$rtti["TArrStr"]',
  29142. '});',
  29143. 'this.$rtti.$ProcVar("TProc", {',
  29144. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TArrStr"]]], this.$rtti["TArr2Dim"])',
  29145. '});',
  29146. 'this.p = null;',
  29147. '']),
  29148. LinesToStr([ // $mod.$main
  29149. '$mod.p = $mod.$rtti["TArrStr"];',
  29150. '$mod.p = $mod.$rtti["TArr2Dim"];',
  29151. '']));
  29152. end;
  29153. procedure TTestModule.TestRTTI_ArrayNestedAnonymous;
  29154. begin
  29155. WithTypeInfo:=true;
  29156. StartProgram(false);
  29157. Add('type');
  29158. Add(' TArr = array of array of longint;');
  29159. Add('var a: TArr;');
  29160. Add('begin');
  29161. ConvertProgram;
  29162. CheckSource('TestRTTI_ArrayNestedAnonymous',
  29163. LinesToStr([ // statements
  29164. 'this.$rtti.$DynArray("TArr$a", {',
  29165. ' eltype: rtl.longint',
  29166. '});',
  29167. 'this.$rtti.$DynArray("TArr", {',
  29168. ' eltype: this.$rtti["TArr$a"]',
  29169. '});',
  29170. 'this.a = [];',
  29171. '']),
  29172. LinesToStr([ // $mod.$main
  29173. ]));
  29174. end;
  29175. procedure TTestModule.TestRTTI_PublishedMethodOverloadFail;
  29176. begin
  29177. WithTypeInfo:=true;
  29178. StartProgram(false);
  29179. Add('type');
  29180. Add(' TObject = class');
  29181. Add(' published');
  29182. Add(' procedure Proc; virtual; abstract;');
  29183. Add(' procedure Proc(Sender: tobject); virtual; abstract;');
  29184. Add(' end;');
  29185. Add('begin');
  29186. SetExpectedPasResolverError('Duplicate published method "Proc" at test1.pp(6,19)',
  29187. nDuplicatePublishedMethodXAtY);
  29188. ConvertProgram;
  29189. end;
  29190. procedure TTestModule.TestRTTI_PublishedMethodHideNoHint;
  29191. begin
  29192. WithTypeInfo:=true;
  29193. StartUnit(false);
  29194. Add([
  29195. 'interface',
  29196. 'type',
  29197. ' TObject = class',
  29198. ' end;',
  29199. ' {$M+}',
  29200. ' TBird = class',
  29201. ' procedure Fly;',
  29202. ' end;',
  29203. ' {$M-}',
  29204. 'type',
  29205. ' TEagle = class(TBird)',
  29206. ' procedure Fly;',
  29207. ' end;',
  29208. 'implementation',
  29209. 'procedure TBird.Fly;',
  29210. 'begin',
  29211. 'end;',
  29212. 'procedure TEagle.Fly;',
  29213. 'begin',
  29214. 'end;',
  29215. '']);
  29216. ConvertUnit;
  29217. CheckSource('TestRTTI_PublishedMethodHideNoHint',
  29218. LinesToStr([ // statements
  29219. 'rtl.createClass(this, "TObject", null, function () {',
  29220. ' this.$init = function () {',
  29221. ' };',
  29222. ' this.$final = function () {',
  29223. ' };',
  29224. '});',
  29225. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  29226. ' this.Fly = function () {',
  29227. ' };',
  29228. ' var $r = this.$rtti;',
  29229. ' $r.addMethod("Fly", 0, []);',
  29230. '});',
  29231. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  29232. ' this.Fly = function () {',
  29233. ' };',
  29234. ' var $r = this.$rtti;',
  29235. ' $r.addMethod("Fly", 0, []);',
  29236. '});',
  29237. '']),
  29238. LinesToStr([ // $mod.$main
  29239. ]));
  29240. CheckResolverUnexpectedHints(true);
  29241. end;
  29242. procedure TTestModule.TestRTTI_PublishedMethodExternalFail;
  29243. begin
  29244. WithTypeInfo:=true;
  29245. StartProgram(false);
  29246. Add('type');
  29247. Add(' TObject = class');
  29248. Add(' published');
  29249. Add(' procedure Proc; external name ''foo'';');
  29250. Add(' end;');
  29251. Add('begin');
  29252. SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
  29253. nPublishedNameMustMatchExternal);
  29254. ConvertProgram;
  29255. end;
  29256. procedure TTestModule.TestRTTI_PublishedClassPropertyFail;
  29257. begin
  29258. WithTypeInfo:=true;
  29259. StartProgram(false);
  29260. Add('type');
  29261. Add(' TObject = class');
  29262. Add(' class var FA: longint;');
  29263. Add(' published');
  29264. Add(' class property A: longint read FA;');
  29265. Add(' end;');
  29266. Add('begin');
  29267. SetExpectedPasResolverError('Invalid published property modifier "class"',
  29268. nInvalidXModifierY);
  29269. ConvertProgram;
  29270. end;
  29271. procedure TTestModule.TestRTTI_PublishedClassFieldFail;
  29272. begin
  29273. WithTypeInfo:=true;
  29274. StartProgram(false);
  29275. Add('type');
  29276. Add(' TObject = class');
  29277. Add(' published');
  29278. Add(' class var FA: longint;');
  29279. Add(' end;');
  29280. Add('begin');
  29281. SetExpectedPasResolverError(sSymbolCannotBePublished,
  29282. nSymbolCannotBePublished);
  29283. ConvertProgram;
  29284. end;
  29285. procedure TTestModule.TestRTTI_PublishedFieldExternalFail;
  29286. begin
  29287. WithTypeInfo:=true;
  29288. StartProgram(false);
  29289. Add('{$modeswitch externalclass}');
  29290. Add('type');
  29291. Add(' TObject = class');
  29292. Add(' published');
  29293. Add(' V: longint; external name ''foo'';');
  29294. Add(' end;');
  29295. Add('begin');
  29296. SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
  29297. nPublishedNameMustMatchExternal);
  29298. ConvertProgram;
  29299. end;
  29300. procedure TTestModule.TestRTTI_Class_Field;
  29301. begin
  29302. WithTypeInfo:=true;
  29303. StartProgram(false);
  29304. Add('{$modeswitch externalclass}');
  29305. Add('type');
  29306. Add(' TObject = class');
  29307. Add(' private');
  29308. Add(' FPropA: string;');
  29309. Add(' published');
  29310. Add(' VarLI: longint;');
  29311. Add(' VarC: char;');
  29312. Add(' VarS: string;');
  29313. Add(' VarD: double;');
  29314. Add(' VarB: boolean;');
  29315. Add(' VarLW: longword;');
  29316. Add(' VarSmI: smallint;');
  29317. Add(' VarW: word;');
  29318. Add(' VarShI: shortint;');
  29319. Add(' VarBy: byte;');
  29320. Add(' VarExt: longint external name ''VarExt'';');
  29321. Add(' ArrA, ArrB: array of byte;');
  29322. Add(' end;');
  29323. Add('var p: pointer;');
  29324. Add(' Obj: tobject;');
  29325. Add('begin');
  29326. Add(' p:=typeinfo(tobject);');
  29327. Add(' p:=typeinfo(p);');
  29328. Add(' p:=typeinfo(obj);');
  29329. ConvertProgram;
  29330. CheckSource('TestRTTI_Class_Field',
  29331. LinesToStr([ // statements
  29332. 'rtl.createClass(this, "TObject", null, function () {',
  29333. ' this.$init = function () {',
  29334. ' this.FPropA = "";',
  29335. ' this.VarLI = 0;',
  29336. ' this.VarC = "";',
  29337. ' this.VarS = "";',
  29338. ' this.VarD = 0.0;',
  29339. ' this.VarB = false;',
  29340. ' this.VarLW = 0;',
  29341. ' this.VarSmI = 0;',
  29342. ' this.VarW = 0;',
  29343. ' this.VarShI = 0;',
  29344. ' this.VarBy = 0;',
  29345. ' this.ArrA = [];',
  29346. ' this.ArrB = [];',
  29347. ' };',
  29348. ' this.$final = function () {',
  29349. ' this.ArrA = undefined;',
  29350. ' this.ArrB = undefined;',
  29351. ' };',
  29352. ' var $r = this.$rtti;',
  29353. ' $r.addField("VarLI", rtl.longint);',
  29354. ' $r.addField("VarC", rtl.char);',
  29355. ' $r.addField("VarS", rtl.string);',
  29356. ' $r.addField("VarD", rtl.double);',
  29357. ' $r.addField("VarB", rtl.boolean);',
  29358. ' $r.addField("VarLW", rtl.longword);',
  29359. ' $r.addField("VarSmI", rtl.smallint);',
  29360. ' $r.addField("VarW", rtl.word);',
  29361. ' $r.addField("VarShI", rtl.shortint);',
  29362. ' $r.addField("VarBy", rtl.byte);',
  29363. ' $r.addField("VarExt", rtl.longint);',
  29364. ' $mod.$rtti.$DynArray("TObject.ArrB$a", {',
  29365. ' eltype: rtl.byte',
  29366. ' });',
  29367. ' $r.addField("ArrA", $mod.$rtti["TObject.ArrB$a"]);',
  29368. ' $r.addField("ArrB", $mod.$rtti["TObject.ArrB$a"]);',
  29369. '});',
  29370. 'this.p = null;',
  29371. 'this.Obj = null;',
  29372. '']),
  29373. LinesToStr([ // $mod.$main
  29374. '$mod.p = $mod.$rtti["TObject"];',
  29375. '$mod.p = rtl.pointer;',
  29376. '$mod.p = $mod.Obj.$rtti;',
  29377. '']));
  29378. end;
  29379. procedure TTestModule.TestRTTI_Class_Method;
  29380. begin
  29381. WithTypeInfo:=true;
  29382. StartProgram(false);
  29383. Add([
  29384. 'type',
  29385. ' TObject = class',
  29386. ' private',
  29387. ' procedure Internal; external name ''$intern'';',
  29388. ' published',
  29389. ' procedure Click; virtual; abstract;',
  29390. ' procedure Notify(Sender: TObject); virtual; abstract;',
  29391. ' function GetNotify: boolean; external name ''GetNotify'';',
  29392. ' procedure Println(a,b: longint); varargs; virtual; abstract;',
  29393. ' function Fetch(URL: string): word; async; external name ''Fetch'';',
  29394. ' end;',
  29395. 'begin']);
  29396. ConvertProgram;
  29397. CheckSource('TestRTTI_Class_Method',
  29398. LinesToStr([ // statements
  29399. 'rtl.createClass(this, "TObject", null, function () {',
  29400. ' this.$init = function () {',
  29401. ' };',
  29402. ' this.$final = function () {',
  29403. ' };',
  29404. ' var $r = this.$rtti;',
  29405. ' $r.addMethod("Click", 0, []);',
  29406. ' $r.addMethod("Notify", 0, [["Sender", $r]]);',
  29407. ' $r.addMethod("GetNotify", 1, [], rtl.boolean, 4);',
  29408. ' $r.addMethod("Println", 0, [["a", rtl.longint], ["b", rtl.longint]], null, 2);',
  29409. ' $r.addMethod("Fetch", 1, [["URL", rtl.string]], rtl.word, 20);',
  29410. '});',
  29411. '']),
  29412. LinesToStr([ // $mod.$main
  29413. '']));
  29414. end;
  29415. procedure TTestModule.TestRTTI_Class_MethodArgFlags;
  29416. begin
  29417. WithTypeInfo:=true;
  29418. StartProgram(false);
  29419. Add('type');
  29420. Add(' TObject = class');
  29421. Add(' published');
  29422. Add(' procedure OpenArray(const Args: array of string); virtual; abstract;');
  29423. Add(' procedure ByRef(var Value: longint; out Item: longint); virtual; abstract;');
  29424. Add(' procedure Untyped(var Value; out Item); virtual; abstract;');
  29425. Add(' end;');
  29426. Add('begin');
  29427. ConvertProgram;
  29428. CheckSource('TestRTTI_Class_MethodOpenArray',
  29429. LinesToStr([ // statements
  29430. 'rtl.createClass(this, "TObject", null, function () {',
  29431. ' this.$init = function () {',
  29432. ' };',
  29433. ' this.$final = function () {',
  29434. ' };',
  29435. ' var $r = this.$rtti;',
  29436. '$r.addMethod("OpenArray", 0, [["Args", rtl.string, 10]]);',
  29437. '$r.addMethod("ByRef", 0, [["Value", rtl.longint, 1], ["Item", rtl.longint, 4]]);',
  29438. '$r.addMethod("Untyped", 0, [["Value", null, 1], ["Item", null, 4]]);',
  29439. '});',
  29440. '']),
  29441. LinesToStr([ // $mod.$main
  29442. '']));
  29443. end;
  29444. procedure TTestModule.TestRTTI_Class_Property;
  29445. begin
  29446. WithTypeInfo:=true;
  29447. StartProgram(false);
  29448. Add('{$modeswitch externalclass}');
  29449. Add('type');
  29450. Add(' TObject = class');
  29451. Add(' private');
  29452. Add(' FColor: longint;');
  29453. Add(' FColorStored: boolean;');
  29454. Add(' procedure SetColor(Value: longint); virtual; abstract;');
  29455. Add(' function GetColor: longint; virtual; abstract;');
  29456. Add(' function GetColorStored: boolean; virtual; abstract;');
  29457. Add(' FExtSize: longint external name ''$extSize'';');
  29458. Add(' FExtSizeStored: boolean external name ''$extSizeStored'';');
  29459. Add(' procedure SetExtSize(Value: longint); external name ''$setSize'';');
  29460. Add(' function GetExtSize: longint; external name ''$getSize'';');
  29461. Add(' function GetExtSizeStored: boolean; external name ''$getExtSizeStored'';');
  29462. Add(' published');
  29463. Add(' property ColorA: longint read FColor;');
  29464. Add(' property ColorB: longint write FColor;');
  29465. Add(' property ColorC: longint read GetColor write SetColor;');
  29466. Add(' property ColorD: longint read FColor write FColor stored FColorStored;');
  29467. Add(' property ExtSizeA: longint read FExtSize write FExtSize;');
  29468. Add(' property ExtSizeB: longint read GetExtSize write SetExtSize stored FExtSizeStored;');
  29469. Add(' property ExtSizeC: longint read FExtSize write FExtSize stored GetExtSizeStored;');
  29470. Add(' end;');
  29471. Add('begin');
  29472. ConvertProgram;
  29473. CheckSource('TestRTTI_Class_Property',
  29474. LinesToStr([ // statements
  29475. 'rtl.createClass(this, "TObject", null, function () {',
  29476. ' this.$init = function () {',
  29477. ' this.FColor = 0;',
  29478. ' this.FColorStored = false;',
  29479. ' };',
  29480. ' this.$final = function () {',
  29481. ' };',
  29482. ' var $r = this.$rtti;',
  29483. ' $r.addProperty("ColorA", 0, rtl.longint, "FColor", "");',
  29484. ' $r.addProperty("ColorB", 0, rtl.longint, "", "FColor");',
  29485. ' $r.addProperty("ColorC", 3, rtl.longint, "GetColor", "SetColor");',
  29486. ' $r.addProperty(',
  29487. ' "ColorD",',
  29488. ' 8,',
  29489. ' rtl.longint,',
  29490. ' "FColor",',
  29491. ' "FColor",',
  29492. ' {',
  29493. ' stored: "FColorStored"',
  29494. ' }',
  29495. ' );',
  29496. ' $r.addProperty("ExtSizeA", 0, rtl.longint, "$extSize", "$extSize");',
  29497. ' $r.addProperty(',
  29498. ' "ExtSizeB",',
  29499. ' 11,',
  29500. ' rtl.longint,',
  29501. ' "$getSize",',
  29502. ' "$setSize",',
  29503. ' {',
  29504. ' stored: "$extSizeStored"',
  29505. ' }',
  29506. ' );',
  29507. ' $r.addProperty(',
  29508. ' "ExtSizeC",',
  29509. ' 12,',
  29510. ' rtl.longint,',
  29511. ' "$extSize",',
  29512. ' "$extSize",',
  29513. ' {',
  29514. ' stored: "$getExtSizeStored"',
  29515. ' }',
  29516. ' );',
  29517. '});',
  29518. '']),
  29519. LinesToStr([ // $mod.$main
  29520. '']));
  29521. end;
  29522. procedure TTestModule.TestRTTI_Class_PropertyParams;
  29523. begin
  29524. WithTypeInfo:=true;
  29525. StartProgram(false);
  29526. Add('{$modeswitch externalclass}');
  29527. Add('type');
  29528. Add(' integer = longint;');
  29529. Add(' TObject = class');
  29530. Add(' private');
  29531. Add(' function GetItems(i: integer): tobject; virtual; abstract;');
  29532. Add(' procedure SetItems(i: integer; value: tobject); virtual; abstract;');
  29533. Add(' function GetValues(const i: integer; var b: boolean): char; virtual; abstract;');
  29534. Add(' procedure SetValues(const i: integer; var b: boolean; value: char); virtual; abstract;');
  29535. Add(' published');
  29536. Add(' property Items[Index: integer]: tobject read getitems write setitems;');
  29537. Add(' property Values[const keya: integer; var keyb: boolean]: char read getvalues write setvalues;');
  29538. Add(' end;');
  29539. Add('begin');
  29540. ConvertProgram;
  29541. CheckSource('TestRTTI_Class_PropertyParams',
  29542. LinesToStr([ // statements
  29543. 'rtl.createClass(this, "TObject", null, function () {',
  29544. ' this.$init = function () {',
  29545. ' };',
  29546. ' this.$final = function () {',
  29547. ' };',
  29548. ' var $r = this.$rtti;',
  29549. ' $r.addProperty("Items", 3, $r, "GetItems", "SetItems");',
  29550. ' $r.addProperty("Values", 3, rtl.char, "GetValues", "SetValues");',
  29551. '});',
  29552. '']),
  29553. LinesToStr([ // $mod.$main
  29554. '']));
  29555. end;
  29556. procedure TTestModule.TestRTTI_Class_OtherUnit_TypeAlias;
  29557. begin
  29558. WithTypeInfo:=true;
  29559. AddModuleWithIntfImplSrc('unit1.pas',
  29560. 'type TColor = -5..5;',
  29561. '');
  29562. StartProgram(true);
  29563. Add([
  29564. 'uses unit1;',
  29565. 'type',
  29566. ' TColorAlias = TColor;',
  29567. ' TColorTypeAlias = type TColor;',
  29568. ' TObject = class',
  29569. ' private',
  29570. ' fColor: TColor;',
  29571. ' fAlias: TColorAlias;',
  29572. ' fTypeAlias: TColorTypeAlias;',
  29573. ' published',
  29574. ' property Color: TColor read fcolor;',
  29575. ' property Alias: TColorAlias read falias;',
  29576. ' property TypeAlias: TColorTypeAlias read ftypealias;',
  29577. ' end;',
  29578. 'begin',
  29579. '']);
  29580. ConvertProgram;
  29581. CheckSource('TestRTTI_Class_OtherUnit_TypeAlias',
  29582. LinesToStr([ // statements
  29583. 'this.$rtti.$inherited("TColorTypeAlias", pas.unit1.$rtti["TColor"], {});',
  29584. 'rtl.createClass(this, "TObject", null, function () {',
  29585. ' this.$init = function () {',
  29586. ' this.fColor = 0;',
  29587. ' this.fAlias = 0;',
  29588. ' this.fTypeAlias = 0;',
  29589. ' };',
  29590. ' this.$final = function () {',
  29591. ' };',
  29592. ' var $r = this.$rtti;',
  29593. ' $r.addProperty("Color", 0, pas.unit1.$rtti["TColor"], "fColor", "");',
  29594. ' $r.addProperty("Alias", 0, pas.unit1.$rtti["TColor"], "fAlias", "");',
  29595. ' $r.addProperty("TypeAlias", 0, $mod.$rtti["TColorTypeAlias"], "fTypeAlias", "");',
  29596. '});',
  29597. '']),
  29598. LinesToStr([ // $mod.$main
  29599. '']));
  29600. end;
  29601. procedure TTestModule.TestRTTI_Class_OmitRTTI;
  29602. begin
  29603. WithTypeInfo:=true;
  29604. StartProgram(false);
  29605. Add([
  29606. '{$modeswitch omitrtti}',
  29607. 'type',
  29608. ' TObject = class',
  29609. ' private',
  29610. ' FA: byte;',
  29611. ' published',
  29612. ' property A: byte read FA write FA;',
  29613. ' end;',
  29614. 'begin']);
  29615. ConvertProgram;
  29616. CheckSource('TestRTTI_Class_OmitRTTI',
  29617. LinesToStr([ // statements
  29618. 'rtl.createClass(this, "TObject", null, function () {',
  29619. ' this.$init = function () {',
  29620. ' this.FA = 0;',
  29621. ' };',
  29622. ' this.$final = function () {',
  29623. ' };',
  29624. '});',
  29625. '']),
  29626. LinesToStr([ // $mod.$main
  29627. '']));
  29628. end;
  29629. procedure TTestModule.TestRTTI_Class_Field_AnonymousArrayOfSelfClass;
  29630. begin
  29631. WithTypeInfo:=true;
  29632. StartUnit(true,[supTObject]);
  29633. Add([
  29634. 'interface',
  29635. 'type',
  29636. ' {$M+}',
  29637. ' TBird = class',
  29638. ' published',
  29639. ' Swarm: array of TBird;',
  29640. ' end;',
  29641. 'implementation',
  29642. '']);
  29643. ConvertUnit;
  29644. CheckSource('TestRTTI_Class_Field_AnonymousArrayOfSelfClass',
  29645. LinesToStr([ // statements
  29646. 'rtl.createClass(this, "TBird", pas.system.TObject, function () {',
  29647. ' this.$init = function () {',
  29648. ' pas.system.TObject.$init.call(this);',
  29649. ' this.Swarm = [];',
  29650. ' };',
  29651. ' this.$final = function () {',
  29652. ' this.Swarm = undefined;',
  29653. ' pas.system.TObject.$final.call(this);',
  29654. ' };',
  29655. ' var $r = this.$rtti;',
  29656. ' $mod.$rtti.$DynArray("TBird.Swarm$a", {',
  29657. ' eltype: $r',
  29658. ' });',
  29659. ' $r.addField("Swarm", $mod.$rtti["TBird.Swarm$a"]);',
  29660. '});',
  29661. '']),
  29662. LinesToStr([ // $mod.$main
  29663. '']));
  29664. end;
  29665. procedure TTestModule.TestRTTI_IndexModifier;
  29666. begin
  29667. WithTypeInfo:=true;
  29668. StartProgram(false);
  29669. Add([
  29670. 'type',
  29671. ' TEnum = (red, blue);',
  29672. ' TObject = class',
  29673. ' FB: boolean;',
  29674. ' procedure SetIntBool(Index: longint; b: boolean); virtual; abstract;',
  29675. ' function GetBoolBool(Index: boolean): boolean; virtual; abstract;',
  29676. ' procedure SetBoolBool(Index: boolean; b: boolean); virtual; abstract;',
  29677. ' function GetEnumBool(Index: TEnum): boolean; virtual; abstract;',
  29678. ' function GetStrIntBool(A: String; I: longint): boolean; virtual; abstract;',
  29679. ' procedure SetStrIntBool(A: String; I: longint; b: boolean); virtual; abstract;',
  29680. ' published',
  29681. ' property B1: boolean index 1 read FB write SetIntBool;',
  29682. ' property B2: boolean index TEnum.blue read GetEnumBool write FB;',
  29683. ' property I1[A: String]: boolean index 2 read GetStrIntBool write SetStrIntBool;',
  29684. ' end;',
  29685. 'begin']);
  29686. ConvertProgram;
  29687. CheckSource('TestRTTI_IndexModifier',
  29688. LinesToStr([ // statements
  29689. 'this.TEnum = {',
  29690. ' "0": "red",',
  29691. ' red: 0,',
  29692. ' "1": "blue",',
  29693. ' blue: 1',
  29694. '};',
  29695. 'this.$rtti.$Enum("TEnum", {',
  29696. ' minvalue: 0,',
  29697. ' maxvalue: 1,',
  29698. ' ordtype: 1,',
  29699. ' enumtype: this.TEnum',
  29700. '});',
  29701. 'rtl.createClass(this, "TObject", null, function () {',
  29702. ' this.$init = function () {',
  29703. ' this.FB = false;',
  29704. ' };',
  29705. ' this.$final = function () {',
  29706. ' };',
  29707. ' var $r = this.$rtti;',
  29708. ' $r.addProperty(',
  29709. ' "B1",',
  29710. ' 18,',
  29711. ' rtl.boolean,',
  29712. ' "FB",',
  29713. ' "SetIntBool",',
  29714. ' {',
  29715. ' index: 1',
  29716. ' }',
  29717. ' );',
  29718. ' $r.addProperty(',
  29719. ' "B2",',
  29720. ' 17,',
  29721. ' rtl.boolean,',
  29722. ' "GetEnumBool",',
  29723. ' "FB",',
  29724. ' {',
  29725. ' index: $mod.TEnum.blue',
  29726. ' }',
  29727. ' );',
  29728. ' $r.addProperty(',
  29729. ' "I1",',
  29730. ' 19,',
  29731. ' rtl.boolean,',
  29732. ' "GetStrIntBool",',
  29733. ' "SetStrIntBool",',
  29734. ' {',
  29735. ' index: 2',
  29736. ' }',
  29737. ' );',
  29738. '});',
  29739. '']),
  29740. LinesToStr([ // $mod.$main
  29741. '']));
  29742. end;
  29743. procedure TTestModule.TestRTTI_StoredModifier;
  29744. begin
  29745. WithTypeInfo:=true;
  29746. StartProgram(false);
  29747. Add([
  29748. 'const',
  29749. ' ConstB = true;',
  29750. 'type',
  29751. ' TObject = class',
  29752. ' private',
  29753. ' FB: boolean;',
  29754. ' function IsBStored: boolean; virtual; abstract;',
  29755. ' published',
  29756. ' property BoolA: boolean read FB stored true;',
  29757. ' property BoolB: boolean read FB stored false;',
  29758. ' property BoolC: boolean read FB stored FB;',
  29759. ' property BoolD: boolean read FB stored ConstB;',
  29760. ' property BoolE: boolean read FB stored IsBStored;',
  29761. ' end;',
  29762. 'begin']);
  29763. ConvertProgram;
  29764. CheckSource('TestRTTI_StoredModifier',
  29765. LinesToStr([ // statements
  29766. 'this.ConstB = true;',
  29767. 'rtl.createClass(this, "TObject", null, function () {',
  29768. ' this.$init = function () {',
  29769. ' this.FB = false;',
  29770. ' };',
  29771. ' this.$final = function () {',
  29772. ' };',
  29773. ' var $r = this.$rtti;',
  29774. ' $r.addProperty("BoolA", 0, rtl.boolean, "FB", "");',
  29775. ' $r.addProperty("BoolB", 4, rtl.boolean, "FB", "");',
  29776. ' $r.addProperty(',
  29777. ' "BoolC",',
  29778. ' 8,',
  29779. ' rtl.boolean,',
  29780. ' "FB",',
  29781. ' "",',
  29782. ' {',
  29783. ' stored: "FB"',
  29784. ' }',
  29785. ' );',
  29786. ' $r.addProperty("BoolD", 0, rtl.boolean, "FB", "");',
  29787. ' $r.addProperty(',
  29788. ' "BoolE",',
  29789. ' 12,',
  29790. ' rtl.boolean,',
  29791. ' "FB",',
  29792. ' "",',
  29793. ' {',
  29794. ' stored: "IsBStored"',
  29795. ' }',
  29796. ' );',
  29797. '});',
  29798. '']),
  29799. LinesToStr([ // $mod.$main
  29800. '']));
  29801. end;
  29802. procedure TTestModule.TestRTTI_DefaultValue;
  29803. begin
  29804. WithTypeInfo:=true;
  29805. StartProgram(false);
  29806. Add([
  29807. 'type',
  29808. ' TEnum = (red, blue);',
  29809. 'const',
  29810. ' CB = true or false;',
  29811. ' CI = 1+2;',
  29812. 'type',
  29813. ' TObject = class',
  29814. ' FB: boolean;',
  29815. ' FI: longint;',
  29816. ' FE: TEnum;',
  29817. ' published',
  29818. ' property B1: boolean read FB default true;',
  29819. ' property B2: boolean read FB default CB;',
  29820. ' property B3: boolean read FB default test1.cb;',
  29821. ' property I1: longint read FI default 2;',
  29822. ' property I2: longint read FI default CI;',
  29823. ' property E1: TEnum read FE default red;',
  29824. ' property E2: TEnum read FE default TEnum.blue;',
  29825. ' end;',
  29826. 'begin']);
  29827. ConvertProgram;
  29828. CheckSource('TestRTTI_DefaultValue',
  29829. LinesToStr([ // statements
  29830. 'this.TEnum = {',
  29831. ' "0": "red",',
  29832. ' red: 0,',
  29833. ' "1": "blue",',
  29834. ' blue: 1',
  29835. '};',
  29836. 'this.$rtti.$Enum("TEnum", {',
  29837. ' minvalue: 0,',
  29838. ' maxvalue: 1,',
  29839. ' ordtype: 1,',
  29840. ' enumtype: this.TEnum',
  29841. '});',
  29842. 'this.CB = true || false;',
  29843. 'this.CI = 1 + 2;',
  29844. 'rtl.createClass(this, "TObject", null, function () {',
  29845. ' this.$init = function () {',
  29846. ' this.FB = false;',
  29847. ' this.FI = 0;',
  29848. ' this.FE = 0;',
  29849. ' };',
  29850. ' this.$final = function () {',
  29851. ' };',
  29852. ' var $r = this.$rtti;',
  29853. ' $r.addProperty(',
  29854. ' "B1",',
  29855. ' 0,',
  29856. ' rtl.boolean,',
  29857. ' "FB",',
  29858. ' "",',
  29859. ' {',
  29860. ' Default: true',
  29861. ' }',
  29862. ' );',
  29863. ' $r.addProperty(',
  29864. ' "B2",',
  29865. ' 0,',
  29866. ' rtl.boolean,',
  29867. ' "FB",',
  29868. ' "",',
  29869. ' {',
  29870. ' Default: true',
  29871. ' }',
  29872. ' );',
  29873. ' $r.addProperty(',
  29874. ' "B3",',
  29875. ' 0,',
  29876. ' rtl.boolean,',
  29877. ' "FB",',
  29878. ' "",',
  29879. ' {',
  29880. ' Default: true',
  29881. ' }',
  29882. ' );',
  29883. ' $r.addProperty(',
  29884. ' "I1",',
  29885. ' 0,',
  29886. ' rtl.longint,',
  29887. ' "FI",',
  29888. ' "",',
  29889. ' {',
  29890. ' Default: 2',
  29891. ' }',
  29892. ' );',
  29893. ' $r.addProperty(',
  29894. ' "I2",',
  29895. ' 0,',
  29896. ' rtl.longint,',
  29897. ' "FI",',
  29898. ' "",',
  29899. ' {',
  29900. ' Default: 3',
  29901. ' }',
  29902. ' );',
  29903. ' $r.addProperty(',
  29904. ' "E1",',
  29905. ' 0,',
  29906. ' $mod.$rtti["TEnum"],',
  29907. ' "FE",',
  29908. ' "",',
  29909. ' {',
  29910. ' Default: $mod.TEnum.red',
  29911. ' }',
  29912. ' );',
  29913. ' $r.addProperty(',
  29914. ' "E2",',
  29915. ' 0,',
  29916. ' $mod.$rtti["TEnum"],',
  29917. ' "FE",',
  29918. ' "",',
  29919. ' {',
  29920. ' Default: $mod.TEnum.blue',
  29921. ' }',
  29922. ' );',
  29923. '});',
  29924. '']),
  29925. LinesToStr([ // $mod.$main
  29926. '']));
  29927. end;
  29928. procedure TTestModule.TestRTTI_DefaultValueSet;
  29929. begin
  29930. WithTypeInfo:=true;
  29931. StartProgram(false);
  29932. Add([
  29933. 'type',
  29934. ' TEnum = (red, blue);',
  29935. ' TSet = set of TEnum;',
  29936. 'const',
  29937. ' CSet = [red,blue];',
  29938. 'type',
  29939. ' TObject = class',
  29940. ' FSet: TSet;',
  29941. ' published',
  29942. ' property Set1: TSet read FSet default [];',
  29943. ' property Set2: TSet read FSet default [red];',
  29944. ' property Set3: TSet read FSet default [red,blue];',
  29945. ' property Set4: TSet read FSet default CSet;',
  29946. ' end;',
  29947. 'begin']);
  29948. ConvertProgram;
  29949. CheckSource('TestRTTI_DefaultValueSet',
  29950. LinesToStr([ // statements
  29951. 'this.TEnum = {',
  29952. ' "0": "red",',
  29953. ' red: 0,',
  29954. ' "1": "blue",',
  29955. ' blue: 1',
  29956. '};',
  29957. 'this.$rtti.$Enum("TEnum", {',
  29958. ' minvalue: 0,',
  29959. ' maxvalue: 1,',
  29960. ' ordtype: 1,',
  29961. ' enumtype: this.TEnum',
  29962. '});',
  29963. 'this.$rtti.$Set("TSet", {',
  29964. ' comptype: this.$rtti["TEnum"]',
  29965. '});',
  29966. 'this.CSet = rtl.createSet(this.TEnum.red, this.TEnum.blue);',
  29967. 'rtl.createClass(this, "TObject", null, function () {',
  29968. ' this.$init = function () {',
  29969. ' this.FSet = {};',
  29970. ' };',
  29971. ' this.$final = function () {',
  29972. ' this.FSet = undefined;',
  29973. ' };',
  29974. ' var $r = this.$rtti;',
  29975. ' $r.addProperty(',
  29976. ' "Set1",',
  29977. ' 0,',
  29978. ' $mod.$rtti["TSet"],',
  29979. ' "FSet",',
  29980. ' "",',
  29981. ' {',
  29982. ' Default: {}',
  29983. ' }',
  29984. ' );',
  29985. ' $r.addProperty(',
  29986. ' "Set2",',
  29987. ' 0,',
  29988. ' $mod.$rtti["TSet"],',
  29989. ' "FSet",',
  29990. ' "",',
  29991. ' {',
  29992. ' Default: rtl.createSet($mod.TEnum.red)',
  29993. ' }',
  29994. ' );',
  29995. ' $r.addProperty(',
  29996. ' "Set3",',
  29997. ' 0,',
  29998. ' $mod.$rtti["TSet"],',
  29999. ' "FSet",',
  30000. ' "",',
  30001. ' {',
  30002. ' Default: rtl.createSet($mod.TEnum.red, $mod.TEnum.blue)',
  30003. ' }',
  30004. ' );',
  30005. ' $r.addProperty(',
  30006. ' "Set4",',
  30007. ' 0,',
  30008. ' $mod.$rtti["TSet"],',
  30009. ' "FSet",',
  30010. ' "",',
  30011. ' {',
  30012. ' Default: $mod.CSet',
  30013. ' }',
  30014. ' );',
  30015. '});',
  30016. '']),
  30017. LinesToStr([ // $mod.$main
  30018. '']));
  30019. end;
  30020. procedure TTestModule.TestRTTI_DefaultValueRangeType;
  30021. begin
  30022. WithTypeInfo:=true;
  30023. StartProgram(false);
  30024. Add([
  30025. 'type',
  30026. ' TRg = -1..1;',
  30027. 'const',
  30028. ' l = low(TRg);',
  30029. ' h = high(TRg);',
  30030. 'type',
  30031. ' TObject = class',
  30032. ' FV: TRg;',
  30033. ' published',
  30034. ' property V1: TRg read FV default -1;',
  30035. ' end;',
  30036. 'begin']);
  30037. ConvertProgram;
  30038. CheckSource('TestRTTI_DefaultValueRangeType',
  30039. LinesToStr([ // statements
  30040. 'this.$rtti.$Int("TRg", {',
  30041. ' minvalue: -1,',
  30042. ' maxvalue: 1,',
  30043. ' ordtype: 0',
  30044. '});',
  30045. 'this.l = -1;',
  30046. 'this.h = 1;',
  30047. 'rtl.createClass(this, "TObject", null, function () {',
  30048. ' this.$init = function () {',
  30049. ' this.FV = 0;',
  30050. ' };',
  30051. ' this.$final = function () {',
  30052. ' };',
  30053. ' var $r = this.$rtti;',
  30054. ' $r.addProperty(',
  30055. ' "V1",',
  30056. ' 0,',
  30057. ' $mod.$rtti["TRg"],',
  30058. ' "FV",',
  30059. ' "",',
  30060. ' {',
  30061. ' Default: -1',
  30062. ' }',
  30063. ' );',
  30064. '});',
  30065. '']),
  30066. LinesToStr([ // $mod.$main
  30067. '']));
  30068. end;
  30069. procedure TTestModule.TestRTTI_DefaultValueInherit;
  30070. begin
  30071. WithTypeInfo:=true;
  30072. StartProgram(false);
  30073. Add([
  30074. 'type',
  30075. ' TObject = class',
  30076. ' FA, FB: byte;',
  30077. ' property A: byte read FA default 1;',
  30078. ' property B: byte read FB default 2;',
  30079. ' end;',
  30080. ' TBird = class',
  30081. ' published',
  30082. ' property A;',
  30083. ' property B nodefault;',
  30084. ' end;',
  30085. 'begin']);
  30086. ConvertProgram;
  30087. CheckSource('TestRTTI_DefaultValueInherit',
  30088. LinesToStr([ // statements
  30089. 'rtl.createClass(this, "TObject", null, function () {',
  30090. ' this.$init = function () {',
  30091. ' this.FA = 0;',
  30092. ' this.FB = 0;',
  30093. ' };',
  30094. ' this.$final = function () {',
  30095. ' };',
  30096. '});',
  30097. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  30098. ' var $r = this.$rtti;',
  30099. ' $r.addProperty(',
  30100. ' "A",',
  30101. ' 0,',
  30102. ' rtl.byte,',
  30103. ' "FA",',
  30104. ' "",',
  30105. ' {',
  30106. ' Default: 1',
  30107. ' }',
  30108. ' );',
  30109. ' $r.addProperty("B", 0, rtl.byte, "FB", "");',
  30110. '});',
  30111. '']),
  30112. LinesToStr([ // $mod.$main
  30113. '']));
  30114. end;
  30115. procedure TTestModule.TestRTTI_OverrideMethod;
  30116. begin
  30117. WithTypeInfo:=true;
  30118. StartProgram(false);
  30119. Add('type');
  30120. Add(' TObject = class');
  30121. Add(' published');
  30122. Add(' procedure DoIt; virtual; abstract;');
  30123. Add(' end;');
  30124. Add(' TSky = class');
  30125. Add(' published');
  30126. Add(' procedure DoIt; override;');
  30127. Add(' end;');
  30128. Add('procedure TSky.DoIt; begin end;');
  30129. Add('begin');
  30130. ConvertProgram;
  30131. CheckSource('TestRTTI_OverrideMethod',
  30132. LinesToStr([ // statements
  30133. 'rtl.createClass(this, "TObject", null, function () {',
  30134. ' this.$init = function () {',
  30135. ' };',
  30136. ' this.$final = function () {',
  30137. ' };',
  30138. ' var $r = this.$rtti;',
  30139. ' $r.addMethod("DoIt", 0, []);',
  30140. '});',
  30141. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  30142. ' this.DoIt = function () {',
  30143. ' };',
  30144. '});',
  30145. '']),
  30146. LinesToStr([ // $mod.$main
  30147. '']));
  30148. end;
  30149. procedure TTestModule.TestRTTI_ReintroduceMethod;
  30150. begin
  30151. WithTypeInfo:=true;
  30152. StartProgram(false);
  30153. Add([
  30154. 'type',
  30155. ' TObject = class',
  30156. ' published',
  30157. ' procedure DoIt;',
  30158. ' end;',
  30159. ' TSky = class',
  30160. ' published',
  30161. ' procedure DoIt; reintroduce;',
  30162. ' end;',
  30163. 'procedure TObject.DoIt; begin end;',
  30164. 'procedure TSky.DoIt;',
  30165. 'begin',
  30166. ' inherited DoIt;',
  30167. 'end;',
  30168. 'begin']);
  30169. ConvertProgram;
  30170. CheckSource('TestRTTI_ReintroduceMethod',
  30171. LinesToStr([ // statements
  30172. 'rtl.createClass(this, "TObject", null, function () {',
  30173. ' this.$init = function () {',
  30174. ' };',
  30175. ' this.$final = function () {',
  30176. ' };',
  30177. ' this.DoIt = function () {',
  30178. ' };',
  30179. ' var $r = this.$rtti;',
  30180. ' $r.addMethod("DoIt", 0, []);',
  30181. '});',
  30182. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  30183. ' this.DoIt = function () {',
  30184. ' $mod.TObject.DoIt.call(this);',
  30185. ' };',
  30186. ' var $r = this.$rtti;',
  30187. ' $r.addMethod("DoIt", 0, []);',
  30188. '});',
  30189. '']),
  30190. LinesToStr([ // $mod.$main
  30191. '']));
  30192. end;
  30193. procedure TTestModule.TestRTTI_OverloadProperty;
  30194. begin
  30195. WithTypeInfo:=true;
  30196. StartProgram(false);
  30197. Add('type');
  30198. Add(' TObject = class');
  30199. Add(' protected');
  30200. Add(' FFlag: longint;');
  30201. Add(' published');
  30202. Add(' property Flag: longint read fflag;');
  30203. Add(' end;');
  30204. Add(' TSky = class');
  30205. Add(' published');
  30206. Add(' property FLAG: longint write fflag;');
  30207. Add(' end;');
  30208. Add('begin');
  30209. ConvertProgram;
  30210. CheckSource('TestRTTI_OverrideMethod',
  30211. LinesToStr([ // statements
  30212. 'rtl.createClass(this, "TObject", null, function () {',
  30213. ' this.$init = function () {',
  30214. ' this.FFlag = 0;',
  30215. ' };',
  30216. ' this.$final = function () {',
  30217. ' };',
  30218. ' var $r = this.$rtti;',
  30219. ' $r.addProperty("Flag", 0, rtl.longint, "FFlag", "");',
  30220. '});',
  30221. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  30222. ' var $r = this.$rtti;',
  30223. ' $r.addProperty("Flag", 0, rtl.longint, "", "FFlag");',
  30224. '});',
  30225. '']),
  30226. LinesToStr([ // $mod.$main
  30227. '']));
  30228. end;
  30229. procedure TTestModule.TestRTTI_ClassForward;
  30230. begin
  30231. WithTypeInfo:=true;
  30232. StartProgram(false);
  30233. Add('type');
  30234. Add(' TObject = class end;');
  30235. Add(' tbridge = class;');
  30236. Add(' TProc = function: tbridge;');
  30237. Add(' TOger = class');
  30238. Add(' published');
  30239. Add(' FBridge: tbridge;');
  30240. Add(' procedure SetBridge(Value: tbridge); virtual; abstract;');
  30241. Add(' property Bridge: tbridge read fbridge write setbridge;');
  30242. Add(' end;');
  30243. Add(' TBridge = class');
  30244. Add(' FOger: toger;');
  30245. Add(' end;');
  30246. Add('var p: Pointer;');
  30247. Add(' b: tbridge;');
  30248. Add('begin');
  30249. Add(' p:=typeinfo(tbridge);');
  30250. Add(' p:=typeinfo(b);');
  30251. ConvertProgram;
  30252. CheckSource('TestRTTI_ClassForward',
  30253. LinesToStr([ // statements
  30254. 'rtl.createClass(this, "TObject", null, function () {',
  30255. ' this.$init = function () {',
  30256. ' };',
  30257. ' this.$final = function () {',
  30258. ' };',
  30259. '});',
  30260. 'this.$rtti.$Class("TBridge");',
  30261. 'this.$rtti.$ProcVar("TProc", {',
  30262. ' procsig: rtl.newTIProcSig([], this.$rtti["TBridge"])',
  30263. '});',
  30264. 'rtl.createClass(this, "TOger", this.TObject, function () {',
  30265. ' this.$init = function () {',
  30266. ' $mod.TObject.$init.call(this);',
  30267. ' this.FBridge = null;',
  30268. ' };',
  30269. ' this.$final = function () {',
  30270. ' this.FBridge = undefined;',
  30271. ' $mod.TObject.$final.call(this);',
  30272. ' };',
  30273. ' var $r = this.$rtti;',
  30274. ' $r.addField("FBridge", $mod.$rtti["TBridge"]);',
  30275. ' $r.addMethod("SetBridge", 0, [["Value", $mod.$rtti["TBridge"]]]);',
  30276. ' $r.addProperty("Bridge", 2, $mod.$rtti["TBridge"], "FBridge", "SetBridge");',
  30277. '});',
  30278. 'rtl.createClass(this, "TBridge", this.TObject, function () {',
  30279. ' this.$init = function () {',
  30280. ' $mod.TObject.$init.call(this);',
  30281. ' this.FOger = null;',
  30282. ' };',
  30283. ' this.$final = function () {',
  30284. ' this.FOger = undefined;',
  30285. ' $mod.TObject.$final.call(this);',
  30286. ' };',
  30287. '});',
  30288. 'this.p = null;',
  30289. 'this.b = null;',
  30290. '']),
  30291. LinesToStr([ // $mod.$main
  30292. '$mod.p = $mod.$rtti["TBridge"];',
  30293. '$mod.p = $mod.b.$rtti;',
  30294. '']));
  30295. end;
  30296. procedure TTestModule.TestRTTI_ClassOf;
  30297. begin
  30298. WithTypeInfo:=true;
  30299. StartProgram(false);
  30300. Add('type');
  30301. Add(' TClass = class of tobject;');
  30302. Add(' TProcA = function: TClass;');
  30303. Add(' TObject = class');
  30304. Add(' published');
  30305. Add(' C: tclass;');
  30306. Add(' end;');
  30307. Add(' tfox = class;');
  30308. Add(' TBird = class end;');
  30309. Add(' TBirds = class of tbird;');
  30310. Add(' TFox = class end;');
  30311. Add(' TFoxes = class of tfox;');
  30312. Add(' TCows = class of TCow;');
  30313. Add(' TCow = class;');
  30314. Add(' TCow = class end;');
  30315. Add('begin');
  30316. ConvertProgram;
  30317. CheckSource('TestRTTI_ClassOf',
  30318. LinesToStr([ // statements
  30319. 'this.$rtti.$Class("TObject");',
  30320. 'this.$rtti.$ClassRef("TClass", {',
  30321. ' instancetype: this.$rtti["TObject"]',
  30322. '});',
  30323. 'this.$rtti.$ProcVar("TProcA", {',
  30324. ' procsig: rtl.newTIProcSig([], this.$rtti["TClass"])',
  30325. '});',
  30326. 'rtl.createClass(this, "TObject", null, function () {',
  30327. ' this.$init = function () {',
  30328. ' this.C = null;',
  30329. ' };',
  30330. ' this.$final = function () {',
  30331. ' this.C = undefined;',
  30332. ' };',
  30333. ' var $r = this.$rtti;',
  30334. ' $r.addField("C", $mod.$rtti["TClass"]);',
  30335. '});',
  30336. 'this.$rtti.$Class("TFox");',
  30337. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  30338. '});',
  30339. 'this.$rtti.$ClassRef("TBirds", {',
  30340. ' instancetype: this.$rtti["TBird"]',
  30341. '});',
  30342. 'rtl.createClass(this, "TFox", this.TObject, function () {',
  30343. '});',
  30344. 'this.$rtti.$ClassRef("TFoxes", {',
  30345. ' instancetype: this.$rtti["TFox"]',
  30346. '});',
  30347. 'this.$rtti.$Class("TCow");',
  30348. 'this.$rtti.$ClassRef("TCows", {',
  30349. ' instancetype: this.$rtti["TCow"]',
  30350. '});',
  30351. 'rtl.createClass(this, "TCow", this.TObject, function () {',
  30352. '});',
  30353. '']),
  30354. LinesToStr([ // $mod.$main
  30355. '']));
  30356. end;
  30357. procedure TTestModule.TestRTTI_Record;
  30358. begin
  30359. WithTypeInfo:=true;
  30360. StartProgram(false);
  30361. Add('type');
  30362. Add(' integer = longint;');
  30363. Add(' TPoint = record');
  30364. Add(' x,y: integer;');
  30365. Add(' end;');
  30366. Add('var p: pointer;');
  30367. Add(' r: tpoint;');
  30368. Add('begin');
  30369. Add(' p:=typeinfo(tpoint);');
  30370. Add(' p:=typeinfo(r);');
  30371. Add(' p:=typeinfo(r.x);');
  30372. ConvertProgram;
  30373. CheckSource('TestRTTI_Record',
  30374. LinesToStr([ // statements
  30375. 'rtl.recNewT(this, "TPoint", function () {',
  30376. ' this.x = 0;',
  30377. ' this.y = 0;',
  30378. ' this.$eq = function (b) {',
  30379. ' return (this.x === b.x) && (this.y === b.y);',
  30380. ' };',
  30381. ' this.$assign = function (s) {',
  30382. ' this.x = s.x;',
  30383. ' this.y = s.y;',
  30384. ' return this;',
  30385. ' };',
  30386. ' var $r = $mod.$rtti.$Record("TPoint", {});',
  30387. ' $r.addField("x", rtl.longint);',
  30388. ' $r.addField("y", rtl.longint);',
  30389. '});',
  30390. 'this.p = null;',
  30391. 'this.r = this.TPoint.$new();',
  30392. '']),
  30393. LinesToStr([ // $mod.$main
  30394. '$mod.p = $mod.$rtti["TPoint"];',
  30395. '$mod.p = $mod.$rtti["TPoint"];',
  30396. '$mod.p = rtl.longint;',
  30397. '']));
  30398. end;
  30399. procedure TTestModule.TestRTTI_RecordAnonymousArray;
  30400. begin
  30401. WithTypeInfo:=true;
  30402. StartProgram(false);
  30403. Add('type');
  30404. Add(' TFloatRec = record');
  30405. Add(' c,d: array of char;');
  30406. // Add(' i: array of array of longint;');
  30407. Add(' end;');
  30408. Add('var p: pointer;');
  30409. Add(' r: tfloatrec;');
  30410. Add('begin');
  30411. Add(' p:=typeinfo(tfloatrec);');
  30412. Add(' p:=typeinfo(r);');
  30413. Add(' p:=typeinfo(r.d);');
  30414. ConvertProgram;
  30415. CheckSource('TestRTTI_Record',
  30416. LinesToStr([ // statements
  30417. 'rtl.recNewT(this, "TFloatRec", function () {',
  30418. ' this.$new = function () {',
  30419. ' var r = Object.create(this);',
  30420. ' r.c = [];',
  30421. ' r.d = [];',
  30422. ' return r;',
  30423. ' };',
  30424. ' this.$eq = function (b) {',
  30425. ' return (this.c === b.c) && (this.d === b.d);',
  30426. ' };',
  30427. ' this.$assign = function (s) {',
  30428. ' this.c = rtl.arrayRef(s.c);',
  30429. ' this.d = rtl.arrayRef(s.d);',
  30430. ' return this;',
  30431. ' };',
  30432. ' var $r = $mod.$rtti.$Record("TFloatRec", {});',
  30433. ' $mod.$rtti.$DynArray("TFloatRec.d$a", {',
  30434. ' eltype: rtl.char',
  30435. ' });',
  30436. ' $r.addField("c", $mod.$rtti["TFloatRec.d$a"]);',
  30437. ' $r.addField("d", $mod.$rtti["TFloatRec.d$a"]);',
  30438. '});',
  30439. 'this.p = null;',
  30440. 'this.r = this.TFloatRec.$new();',
  30441. '']),
  30442. LinesToStr([ // $mod.$main
  30443. '$mod.p = $mod.$rtti["TFloatRec"];',
  30444. '$mod.p = $mod.$rtti["TFloatRec"];',
  30445. '$mod.p = $mod.$rtti["TFloatRec.d$a"];',
  30446. '']));
  30447. end;
  30448. procedure TTestModule.TestRTTI_Record_ClassVarType;
  30449. begin
  30450. WithTypeInfo:=true;
  30451. StartProgram(false);
  30452. Add([
  30453. '{$modeswitch AdvancedRecords}',
  30454. 'type',
  30455. ' TPoint = record',
  30456. ' type TProc = procedure(w: word);',
  30457. ' class var p: TProc;',
  30458. ' end;',
  30459. 'begin',
  30460. '']);
  30461. ConvertProgram;
  30462. CheckSource('TestRTTI_Record_ClassVarType',
  30463. LinesToStr([ // statements
  30464. 'rtl.recNewT(this, "TPoint", function () {',
  30465. ' $mod.$rtti.$ProcVar("TPoint.TProc", {',
  30466. ' procsig: rtl.newTIProcSig([["w", rtl.word]])',
  30467. ' });',
  30468. ' this.p = null;',
  30469. ' this.$eq = function (b) {',
  30470. ' return true;',
  30471. ' };',
  30472. ' this.$assign = function (s) {',
  30473. ' return this;',
  30474. ' };',
  30475. ' var $r = $mod.$rtti.$Record("TPoint", {});',
  30476. ' $r.addField("p", $mod.$rtti["TPoint.TProc"]);',
  30477. '}, true);',
  30478. '']),
  30479. LinesToStr([ // $mod.$main
  30480. '']));
  30481. end;
  30482. procedure TTestModule.TestRTTI_LocalTypes;
  30483. begin
  30484. WithTypeInfo:=true;
  30485. StartProgram(false);
  30486. Add([
  30487. 'procedure DoIt;',
  30488. 'type',
  30489. ' integer = longint;',
  30490. ' TPoint = record',
  30491. ' x,y: integer;',
  30492. ' end;',
  30493. 'var p: TPoint;',
  30494. 'begin',
  30495. 'end;',
  30496. 'begin']);
  30497. ConvertProgram;
  30498. CheckSource('TestRTTI_LocalTypes',
  30499. LinesToStr([ // statements
  30500. 'var TPoint = rtl.recNewT(null, "", function () {',
  30501. ' this.x = 0;',
  30502. ' this.y = 0;',
  30503. ' this.$eq = function (b) {',
  30504. ' return (this.x === b.x) && (this.y === b.y);',
  30505. ' };',
  30506. ' this.$assign = function (s) {',
  30507. ' this.x = s.x;',
  30508. ' this.y = s.y;',
  30509. ' return this;',
  30510. ' };',
  30511. '});',
  30512. 'this.DoIt = function () {',
  30513. ' var p = TPoint.$new();',
  30514. '};',
  30515. '']),
  30516. LinesToStr([ // $mod.$main
  30517. '']));
  30518. end;
  30519. procedure TTestModule.TestRTTI_TypeInfo_BaseTypes;
  30520. begin
  30521. WithTypeInfo:=true;
  30522. StartProgram(false);
  30523. Add([
  30524. 'type',
  30525. ' TCaption = string;',
  30526. ' TYesNo = boolean;',
  30527. ' TLetter = char;',
  30528. ' TFloat = double;',
  30529. ' TPtr = pointer;',
  30530. ' TShortInt = shortint;',
  30531. ' TByte = byte;',
  30532. ' TSmallInt = smallint;',
  30533. ' TWord = word;',
  30534. ' TInt32 = longint;',
  30535. ' TDWord = longword;',
  30536. ' TValue = jsvalue;',
  30537. 'var p: TPtr;',
  30538. 'begin',
  30539. ' p:=typeinfo(string);',
  30540. ' p:=typeinfo(tcaption);',
  30541. ' p:=typeinfo(boolean);',
  30542. ' p:=typeinfo(tyesno);',
  30543. ' p:=typeinfo(char);',
  30544. ' p:=typeinfo(tletter);',
  30545. ' p:=typeinfo(double);',
  30546. ' p:=typeinfo(tfloat);',
  30547. ' p:=typeinfo(pointer);',
  30548. ' p:=typeinfo(tptr);',
  30549. ' p:=typeinfo(shortint);',
  30550. ' p:=typeinfo(tshortint);',
  30551. ' p:=typeinfo(byte);',
  30552. ' p:=typeinfo(tbyte);',
  30553. ' p:=typeinfo(smallint);',
  30554. ' p:=typeinfo(tsmallint);',
  30555. ' p:=typeinfo(word);',
  30556. ' p:=typeinfo(tword);',
  30557. ' p:=typeinfo(longword);',
  30558. ' p:=typeinfo(tdword);',
  30559. ' p:=typeinfo(jsvalue);',
  30560. ' p:=typeinfo(tvalue);',
  30561. '']);
  30562. ConvertProgram;
  30563. CheckSource('TestRTTI_TypeInfo_BaseTypes',
  30564. LinesToStr([ // statements
  30565. 'this.p = null;',
  30566. '']),
  30567. LinesToStr([ // $mod.$main
  30568. '$mod.p = rtl.string;',
  30569. '$mod.p = rtl.string;',
  30570. '$mod.p = rtl.boolean;',
  30571. '$mod.p = rtl.boolean;',
  30572. '$mod.p = rtl.char;',
  30573. '$mod.p = rtl.char;',
  30574. '$mod.p = rtl.double;',
  30575. '$mod.p = rtl.double;',
  30576. '$mod.p = rtl.pointer;',
  30577. '$mod.p = rtl.pointer;',
  30578. '$mod.p = rtl.shortint;',
  30579. '$mod.p = rtl.shortint;',
  30580. '$mod.p = rtl.byte;',
  30581. '$mod.p = rtl.byte;',
  30582. '$mod.p = rtl.smallint;',
  30583. '$mod.p = rtl.smallint;',
  30584. '$mod.p = rtl.word;',
  30585. '$mod.p = rtl.word;',
  30586. '$mod.p = rtl.longword;',
  30587. '$mod.p = rtl.longword;',
  30588. '$mod.p = rtl.jsvalue;',
  30589. '$mod.p = rtl.jsvalue;',
  30590. '']));
  30591. end;
  30592. procedure TTestModule.TestRTTI_TypeInfo_Type_BaseTypes;
  30593. begin
  30594. WithTypeInfo:=true;
  30595. StartProgram(false);
  30596. Add([
  30597. 'type',
  30598. ' TCaption = type string;',
  30599. ' TYesNo = type boolean;',
  30600. ' TLetter = type char;',
  30601. ' TFloat = type double;',
  30602. ' TPtr = type pointer;',
  30603. ' TShortInt = type shortint;',
  30604. ' TByte = type byte;',
  30605. ' TSmallInt = type smallint;',
  30606. ' TWord = type word;',
  30607. ' TInt32 = type longint;',
  30608. ' TDWord = type longword;',
  30609. ' TValue = type jsvalue;',
  30610. ' TAliasValue = type TValue;',
  30611. 'var',
  30612. ' p: TPtr;',
  30613. ' a: TAliasValue;',
  30614. 'begin',
  30615. ' p:=typeinfo(tcaption);',
  30616. ' p:=typeinfo(tyesno);',
  30617. ' p:=typeinfo(tletter);',
  30618. ' p:=typeinfo(tfloat);',
  30619. ' p:=typeinfo(tptr);',
  30620. ' p:=typeinfo(tshortint);',
  30621. ' p:=typeinfo(tbyte);',
  30622. ' p:=typeinfo(tsmallint);',
  30623. ' p:=typeinfo(tword);',
  30624. ' p:=typeinfo(tdword);',
  30625. ' p:=typeinfo(tvalue);',
  30626. ' p:=typeinfo(taliasvalue);',
  30627. ' p:=typeinfo(a);',
  30628. '']);
  30629. ConvertProgram;
  30630. CheckSource('TestRTTI_TypeInfo_Type_BaseTypes',
  30631. LinesToStr([ // statements
  30632. 'this.$rtti.$inherited("TCaption", rtl.string, {});',
  30633. 'this.$rtti.$inherited("TYesNo", rtl.boolean, {});',
  30634. 'this.$rtti.$inherited("TLetter", rtl.char, {});',
  30635. 'this.$rtti.$inherited("TFloat", rtl.double, {});',
  30636. 'this.$rtti.$inherited("TPtr", rtl.pointer, {});',
  30637. 'this.$rtti.$inherited("TShortInt", rtl.shortint, {});',
  30638. 'this.$rtti.$inherited("TByte", rtl.byte, {});',
  30639. 'this.$rtti.$inherited("TSmallInt", rtl.smallint, {});',
  30640. 'this.$rtti.$inherited("TWord", rtl.word, {});',
  30641. 'this.$rtti.$inherited("TInt32", rtl.longint, {});',
  30642. 'this.$rtti.$inherited("TDWord", rtl.longword, {});',
  30643. 'this.$rtti.$inherited("TValue", rtl.jsvalue, {});',
  30644. 'this.$rtti.$inherited("TAliasValue", this.$rtti["TValue"], {});',
  30645. 'this.p = null;',
  30646. 'this.a = undefined;',
  30647. '']),
  30648. LinesToStr([ // $mod.$main
  30649. '$mod.p = $mod.$rtti["TCaption"];',
  30650. '$mod.p = $mod.$rtti["TYesNo"];',
  30651. '$mod.p = $mod.$rtti["TLetter"];',
  30652. '$mod.p = $mod.$rtti["TFloat"];',
  30653. '$mod.p = $mod.$rtti["TPtr"];',
  30654. '$mod.p = $mod.$rtti["TShortInt"];',
  30655. '$mod.p = $mod.$rtti["TByte"];',
  30656. '$mod.p = $mod.$rtti["TSmallInt"];',
  30657. '$mod.p = $mod.$rtti["TWord"];',
  30658. '$mod.p = $mod.$rtti["TDWord"];',
  30659. '$mod.p = $mod.$rtti["TValue"];',
  30660. '$mod.p = $mod.$rtti["TAliasValue"];',
  30661. '$mod.p = $mod.$rtti["TAliasValue"];',
  30662. '']));
  30663. end;
  30664. procedure TTestModule.TestRTTI_TypeInfo_LocalFail;
  30665. begin
  30666. WithTypeInfo:=true;
  30667. StartProgram(false);
  30668. Add('procedure DoIt;');
  30669. Add('type');
  30670. Add(' integer = longint;');
  30671. Add(' TPoint = record');
  30672. Add(' x,y: integer;');
  30673. Add(' end;');
  30674. Add('var p: pointer;');
  30675. Add('begin');
  30676. Add(' p:=typeinfo(tpoint);');
  30677. Add('end;');
  30678. Add('begin');
  30679. SetExpectedPasResolverError(sSymbolCannotBePublished,nSymbolCannotBePublished);
  30680. ConvertProgram;
  30681. end;
  30682. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses1;
  30683. begin
  30684. WithTypeInfo:=true;
  30685. StartProgram(true,[supTypeInfo]);
  30686. Add([
  30687. '{$modeswitch externalclass}',
  30688. 'type',
  30689. ' TFlag = (up,down);',
  30690. ' TFlags = set of TFlag;',
  30691. 'var',
  30692. ' ti: TTypeInfo;',
  30693. ' tiInt: TTypeInfoInteger;',
  30694. ' tiEnum: TTypeInfoEnum;',
  30695. ' tiSet: TTypeInfoSet;',
  30696. 'begin',
  30697. ' ti:=typeinfo(string);',
  30698. ' ti:=typeinfo(boolean);',
  30699. ' ti:=typeinfo(char);',
  30700. ' ti:=typeinfo(double);',
  30701. ' tiInt:=typeinfo(shortint);',
  30702. ' tiInt:=typeinfo(byte);',
  30703. ' tiInt:=typeinfo(smallint);',
  30704. ' tiInt:=typeinfo(word);',
  30705. ' tiInt:=typeinfo(longint);',
  30706. ' tiInt:=typeinfo(longword);',
  30707. ' ti:=typeinfo(jsvalue);',
  30708. ' tiEnum:=typeinfo(tflag);',
  30709. ' tiSet:=typeinfo(tflags);']);
  30710. ConvertProgram;
  30711. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses1',
  30712. LinesToStr([ // statements
  30713. 'this.TFlag = {',
  30714. ' "0": "up",',
  30715. ' up: 0,',
  30716. ' "1": "down",',
  30717. ' down: 1',
  30718. '};',
  30719. 'this.$rtti.$Enum("TFlag", {',
  30720. ' minvalue: 0,',
  30721. ' maxvalue: 1,',
  30722. ' ordtype: 1,',
  30723. ' enumtype: this.TFlag',
  30724. '});',
  30725. 'this.$rtti.$Set("TFlags", {',
  30726. ' comptype: this.$rtti["TFlag"]',
  30727. '});',
  30728. 'this.ti = null;',
  30729. 'this.tiInt = null;',
  30730. 'this.tiEnum = null;',
  30731. 'this.tiSet = null;',
  30732. '']),
  30733. LinesToStr([ // $mod.$main
  30734. '$mod.ti = rtl.string;',
  30735. '$mod.ti = rtl.boolean;',
  30736. '$mod.ti = rtl.char;',
  30737. '$mod.ti = rtl.double;',
  30738. '$mod.tiInt = rtl.shortint;',
  30739. '$mod.tiInt = rtl.byte;',
  30740. '$mod.tiInt = rtl.smallint;',
  30741. '$mod.tiInt = rtl.word;',
  30742. '$mod.tiInt = rtl.longint;',
  30743. '$mod.tiInt = rtl.longword;',
  30744. '$mod.ti = rtl.jsvalue;',
  30745. '$mod.tiEnum = $mod.$rtti["TFlag"];',
  30746. '$mod.tiSet = $mod.$rtti["TFlags"];',
  30747. '']));
  30748. end;
  30749. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses2;
  30750. begin
  30751. WithTypeInfo:=true;
  30752. StartProgram(true,[supTypeInfo]);
  30753. Add('{$modeswitch externalclass}');
  30754. Add('type');
  30755. Add(' TStaticArr = array[boolean] of string;');
  30756. Add(' TDynArr = array of string;');
  30757. Add(' TProc = procedure;');
  30758. Add(' TMethod = procedure of object;');
  30759. Add('var');
  30760. Add(' StaticArray: TStaticArr;');
  30761. Add(' tiStaticArray: TTypeInfoStaticArray;');
  30762. Add(' DynArray: TDynArr;');
  30763. Add(' tiDynArray: TTypeInfoDynArray;');
  30764. Add(' ProcVar: TProc;');
  30765. Add(' tiProcVar: TTypeInfoProcVar;');
  30766. Add(' MethodVar: TMethod;');
  30767. Add(' tiMethodVar: TTypeInfoMethodVar;');
  30768. Add('begin');
  30769. Add(' tiStaticArray:=typeinfo(StaticArray);');
  30770. Add(' tiStaticArray:=typeinfo(TStaticArr);');
  30771. Add(' tiDynArray:=typeinfo(DynArray);');
  30772. Add(' tiDynArray:=typeinfo(TDynArr);');
  30773. Add(' tiProcVar:=typeinfo(ProcVar);');
  30774. Add(' tiProcVar:=typeinfo(TProc);');
  30775. Add(' tiMethodVar:=typeinfo(MethodVar);');
  30776. Add(' tiMethodVar:=typeinfo(TMethod);');
  30777. ConvertProgram;
  30778. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses2',
  30779. LinesToStr([ // statements
  30780. 'this.$rtti.$StaticArray("TStaticArr", {',
  30781. ' dims: [2],',
  30782. ' eltype: rtl.string',
  30783. '});',
  30784. 'this.$rtti.$DynArray("TDynArr", {',
  30785. ' eltype: rtl.string',
  30786. '});',
  30787. 'this.$rtti.$ProcVar("TProc", {',
  30788. ' procsig: rtl.newTIProcSig([])',
  30789. '});',
  30790. 'this.$rtti.$MethodVar("TMethod", {',
  30791. ' procsig: rtl.newTIProcSig([]),',
  30792. ' methodkind: 0',
  30793. '});',
  30794. 'this.StaticArray = rtl.arraySetLength(null,"",2);',
  30795. 'this.tiStaticArray = null;',
  30796. 'this.DynArray = [];',
  30797. 'this.tiDynArray = null;',
  30798. 'this.ProcVar = null;',
  30799. 'this.tiProcVar = null;',
  30800. 'this.MethodVar = null;',
  30801. 'this.tiMethodVar = null;',
  30802. '']),
  30803. LinesToStr([ // $mod.$main
  30804. '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
  30805. '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
  30806. '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
  30807. '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
  30808. '$mod.tiProcVar = $mod.$rtti["TProc"];',
  30809. '$mod.tiProcVar = $mod.$rtti["TProc"];',
  30810. '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
  30811. '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
  30812. '']));
  30813. end;
  30814. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses3;
  30815. begin
  30816. WithTypeInfo:=true;
  30817. StartProgram(true,[supTypeInfo]);
  30818. Add('{$modeswitch externalclass}');
  30819. Add('type');
  30820. Add(' TRec = record end;');
  30821. // ToDo: ^TRec
  30822. Add(' TObject = class end;');
  30823. Add(' TClass = class of tobject;');
  30824. Add('var');
  30825. Add(' Rec: trec;');
  30826. Add(' tiRecord: ttypeinforecord;');
  30827. Add(' Obj: tobject;');
  30828. Add(' tiClass: ttypeinfoclass;');
  30829. Add(' aClass: tclass;');
  30830. Add(' tiClassRef: ttypeinfoclassref;');
  30831. // ToDo: ^TRec
  30832. Add(' tiPointer: ttypeinfopointer;');
  30833. Add('begin');
  30834. Add(' tirecord:=typeinfo(trec);');
  30835. Add(' tirecord:=typeinfo(trec);');
  30836. Add(' ticlass:=typeinfo(obj);');
  30837. Add(' ticlass:=typeinfo(tobject);');
  30838. Add(' ticlass:=typeinfo(aclass);');
  30839. Add(' ticlassref:=typeinfo(tclass);');
  30840. ConvertProgram;
  30841. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses3',
  30842. LinesToStr([ // statements
  30843. 'rtl.recNewT(this, "TRec", function () {',
  30844. ' this.$eq = function (b) {',
  30845. ' return true;',
  30846. ' };',
  30847. ' this.$assign = function (s) {',
  30848. ' return this;',
  30849. ' };',
  30850. ' $mod.$rtti.$Record("TRec", {});',
  30851. '});',
  30852. 'rtl.createClass(this, "TObject", null, function () {',
  30853. ' this.$init = function () {',
  30854. ' };',
  30855. ' this.$final = function () {',
  30856. ' };',
  30857. '});',
  30858. 'this.$rtti.$ClassRef("TClass", {',
  30859. ' instancetype: this.$rtti["TObject"]',
  30860. '});',
  30861. 'this.Rec = this.TRec.$new();',
  30862. 'this.tiRecord = null;',
  30863. 'this.Obj = null;',
  30864. 'this.tiClass = null;',
  30865. 'this.aClass = null;',
  30866. 'this.tiClassRef = null;',
  30867. 'this.tiPointer = null;',
  30868. '']),
  30869. LinesToStr([ // $mod.$main
  30870. '$mod.tiRecord = $mod.$rtti["TRec"];',
  30871. '$mod.tiRecord = $mod.$rtti["TRec"];',
  30872. '$mod.tiClass = $mod.Obj.$rtti;',
  30873. '$mod.tiClass = $mod.$rtti["TObject"];',
  30874. '$mod.tiClass = $mod.aClass.$rtti;',
  30875. '$mod.tiClassRef = $mod.$rtti["TClass"];',
  30876. '']));
  30877. end;
  30878. procedure TTestModule.TestRTTI_TypeInfo_FunctionClassType;
  30879. begin
  30880. WithTypeInfo:=true;
  30881. StartProgram(true,[supTypeInfo]);
  30882. Add([
  30883. '{$modeswitch externalclass}',
  30884. 'type',
  30885. ' TClass = class of tobject;',
  30886. ' TObject = class',
  30887. ' function MyClass: TClass;',
  30888. ' class function ClassType: TClass;',
  30889. ' end;',
  30890. 'function TObject.MyClass: TClass;',
  30891. 'var t: TTypeInfoClass;',
  30892. 'begin',
  30893. ' t:=TypeInfo(Self);',
  30894. ' t:=TypeInfo(Result);',
  30895. ' t:=TypeInfo(TObject);',
  30896. 'end;',
  30897. 'class function TObject.ClassType: TClass;',
  30898. 'var t: TTypeInfoClass;',
  30899. 'begin',
  30900. ' t:=TypeInfo(Self);',
  30901. ' t:=TypeInfo(Result);',
  30902. 'end;',
  30903. 'var',
  30904. ' Obj: TObject;',
  30905. ' t: TTypeInfoClass;',
  30906. 'begin',
  30907. ' t:=TypeInfo(TObject.ClassType);',
  30908. ' t:=TypeInfo(Obj.ClassType);',
  30909. ' t:=TypeInfo(Obj.MyClass);',
  30910. '']);
  30911. ConvertProgram;
  30912. CheckSource('TestRTTI_TypeInfo_FunctionClassType',
  30913. LinesToStr([ // statements
  30914. 'this.$rtti.$Class("TObject");',
  30915. 'this.$rtti.$ClassRef("TClass", {',
  30916. ' instancetype: this.$rtti["TObject"]',
  30917. '});',
  30918. 'rtl.createClass(this, "TObject", null, function () {',
  30919. ' this.$init = function () {',
  30920. ' };',
  30921. ' this.$final = function () {',
  30922. ' };',
  30923. ' this.MyClass = function () {',
  30924. ' var Result = null;',
  30925. ' var t = null;',
  30926. ' t = this.$rtti;',
  30927. ' t = Result.$rtti;',
  30928. ' t = $mod.$rtti["TObject"];',
  30929. ' return Result;',
  30930. ' };',
  30931. ' this.ClassType = function () {',
  30932. ' var Result = null;',
  30933. ' var t = null;',
  30934. ' t = this.$rtti;',
  30935. ' t = Result.$rtti;',
  30936. ' return Result;',
  30937. ' };',
  30938. '});',
  30939. 'this.Obj = null;',
  30940. 'this.t = null;',
  30941. '']),
  30942. LinesToStr([ // $mod.$main
  30943. '$mod.t = $mod.TObject.ClassType().$rtti;',
  30944. '$mod.t = $mod.Obj.$class.ClassType().$rtti;',
  30945. '$mod.t = $mod.Obj.MyClass().$rtti;',
  30946. '']));
  30947. end;
  30948. procedure TTestModule.TestRTTI_TypeInfo_MixedUnits_PointerAndClass;
  30949. begin
  30950. WithTypeInfo:=true;
  30951. AddModuleWithIntfImplSrc('typinfo.pas',
  30952. LinesToStr([
  30953. '{$modeswitch externalclass}',
  30954. 'type',
  30955. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  30956. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo) end;',
  30957. '']),
  30958. '');
  30959. AddModuleWithIntfImplSrc('unit2.pas',
  30960. LinesToStr([
  30961. 'uses typinfo;',
  30962. 'type PTypeInfo = TTypeInfo;', // delphi compatibility code
  30963. 'procedure DoPtr(p: PTypeInfo);',
  30964. 'procedure DoInfo(t: TTypeInfo);',
  30965. 'procedure DoInt(t: TTypeInfoInteger);',
  30966. '']),
  30967. LinesToStr([
  30968. 'procedure DoPtr(p: PTypeInfo);',
  30969. 'begin end;',
  30970. 'procedure DoInfo(t: TTypeInfo);',
  30971. 'begin end;',
  30972. 'procedure DoInt(t: TTypeInfoInteger);',
  30973. 'begin end;',
  30974. '']));
  30975. StartUnit(true);
  30976. Add([
  30977. 'interface',
  30978. 'uses unit2;', // does not use unit typinfo
  30979. 'implementation',
  30980. 'var',
  30981. ' i: byte;',
  30982. ' p: pointer;',
  30983. ' t: PTypeInfo;',
  30984. 'initialization',
  30985. ' p:=typeinfo(i);',
  30986. ' t:=typeinfo(i);',
  30987. ' if p=t then ;',
  30988. ' if p=typeinfo(i) then ;',
  30989. ' if typeinfo(i)=p then ;',
  30990. ' if t=typeinfo(i) then ;',
  30991. ' if typeinfo(i)=t then ;',
  30992. ' DoPtr(p);',
  30993. ' DoPtr(t);',
  30994. ' DoPtr(typeinfo(i));',
  30995. ' DoInfo(p);',
  30996. ' DoInfo(t);',
  30997. ' DoInfo(typeinfo(i));',
  30998. ' DoInt(typeinfo(i));',
  30999. '']);
  31000. ConvertUnit;
  31001. CheckSource('TestRTTI_TypeInfo_MixedUnits_PointerAndClass',
  31002. LinesToStr([ // statements
  31003. 'var $impl = $mod.$impl;',
  31004. '']),
  31005. LinesToStr([ // this.$init
  31006. '$impl.p = rtl.byte;',
  31007. '$impl.t = rtl.byte;',
  31008. 'if ($impl.p === $impl.t) ;',
  31009. 'if ($impl.p === rtl.byte) ;',
  31010. 'if (rtl.byte === $impl.p) ;',
  31011. 'if ($impl.t === rtl.byte) ;',
  31012. 'if (rtl.byte === $impl.t) ;',
  31013. 'pas.unit2.DoPtr($impl.p);',
  31014. 'pas.unit2.DoPtr($impl.t);',
  31015. 'pas.unit2.DoPtr(rtl.byte);',
  31016. 'pas.unit2.DoInfo($impl.p);',
  31017. 'pas.unit2.DoInfo($impl.t);',
  31018. 'pas.unit2.DoInfo(rtl.byte);',
  31019. 'pas.unit2.DoInt(rtl.byte);',
  31020. '']),
  31021. LinesToStr([ // implementation
  31022. '$impl.i = 0;',
  31023. '$impl.p = null;',
  31024. '$impl.t = null;',
  31025. '']) );
  31026. end;
  31027. procedure TTestModule.TestRTTI_Interface_Corba;
  31028. begin
  31029. WithTypeInfo:=true;
  31030. StartProgram(true,[supTypeInfo]);
  31031. Add([
  31032. '{$interfaces corba}',
  31033. '{$modeswitch externalclass}',
  31034. 'type',
  31035. ' IUnknown = interface',
  31036. ' end;',
  31037. ' IBird = interface',
  31038. ' function GetItem: longint;',
  31039. ' procedure SetItem(Value: longint);',
  31040. ' property Item: longint read GetItem write SetItem;',
  31041. ' end;',
  31042. 'procedure DoIt(t: TTypeInfoInterface); begin end;',
  31043. 'var',
  31044. ' i: IBird;',
  31045. ' t: TTypeInfoInterface;',
  31046. 'begin',
  31047. ' t:=TypeInfo(IBird);',
  31048. ' t:=TypeInfo(i);',
  31049. ' DoIt(t);',
  31050. ' DoIt(TypeInfo(IBird));',
  31051. '']);
  31052. ConvertProgram;
  31053. CheckSource('TestRTTI_Interface_Corba',
  31054. LinesToStr([ // statements
  31055. 'rtl.createInterface(',
  31056. ' this,',
  31057. ' "IUnknown",',
  31058. ' "{B92D5841-758A-322B-B800-000000000000}",',
  31059. ' [],',
  31060. ' null,',
  31061. ' function () {',
  31062. ' }',
  31063. ');',
  31064. 'rtl.createInterface(',
  31065. ' this,',
  31066. ' "IBird",',
  31067. ' "{D32D5841-6264-3AE3-A2C9-B91CE922C9B9}",',
  31068. ' ["GetItem", "SetItem"],',
  31069. ' null,',
  31070. ' function () {',
  31071. ' var $r = this.$rtti;',
  31072. ' $r.addMethod("GetItem", 1, [], rtl.longint);',
  31073. ' $r.addMethod("SetItem", 0, [["Value", rtl.longint]]);',
  31074. ' $r.addProperty("Item", 3, rtl.longint, "GetItem", "SetItem");',
  31075. ' }',
  31076. ');',
  31077. 'this.DoIt = function (t) {',
  31078. '}; ',
  31079. 'this.i = null;',
  31080. 'this.t = null;',
  31081. '']),
  31082. LinesToStr([ // $mod.$main
  31083. '$mod.t = $mod.$rtti["IBird"];',
  31084. '$mod.t = $mod.i.$rtti;',
  31085. '$mod.DoIt($mod.t);',
  31086. '$mod.DoIt($mod.$rtti["IBird"]);',
  31087. '']));
  31088. end;
  31089. procedure TTestModule.TestRTTI_Interface_COM;
  31090. begin
  31091. WithTypeInfo:=true;
  31092. StartProgram(true,[supTypeInfo]);
  31093. Add([
  31094. '{$interfaces com}',
  31095. '{$modeswitch externalclass}',
  31096. 'type',
  31097. ' TGuid = record end;',
  31098. ' integer = longint;',
  31099. ' IUnknown = interface',
  31100. ' function QueryInterface(const iid: TGuid; out obj): Integer;',
  31101. ' function _AddRef: Integer;',
  31102. ' function _Release: Integer;',
  31103. ' end;',
  31104. ' IBird = interface',
  31105. ' function GetItem: longint;',
  31106. ' procedure SetItem(Value: longint);',
  31107. ' property Item: longint read GetItem write SetItem;',
  31108. ' end;',
  31109. 'var',
  31110. ' i: IBird;',
  31111. ' t: TTypeInfoInterface;',
  31112. 'begin',
  31113. ' t:=TypeInfo(IBird);',
  31114. ' t:=TypeInfo(i);',
  31115. '']);
  31116. ConvertProgram;
  31117. CheckSource('TestRTTI_Interface_COM',
  31118. LinesToStr([ // statements
  31119. 'rtl.recNewT(this, "TGuid", function () {',
  31120. ' this.$eq = function (b) {',
  31121. ' return true;',
  31122. ' };',
  31123. ' this.$assign = function (s) {',
  31124. ' return this;',
  31125. ' };',
  31126. ' $mod.$rtti.$Record("TGuid", {});',
  31127. '});',
  31128. 'rtl.createInterface(',
  31129. ' this,',
  31130. ' "IUnknown",',
  31131. ' "{D7ADB00D-1A9B-3EDC-B123-730E661DDFA9}",',
  31132. ' ["QueryInterface", "_AddRef", "_Release"],',
  31133. ' null,',
  31134. ' function () {',
  31135. ' this.$kind = "com";',
  31136. ' var $r = this.$rtti;',
  31137. ' $r.addMethod("QueryInterface", 1, [["iid", $mod.$rtti["TGuid"], 2], ["obj", null, 4]], rtl.longint);',
  31138. ' $r.addMethod("_AddRef", 1, [], rtl.longint);',
  31139. ' $r.addMethod("_Release", 1, [], rtl.longint);',
  31140. ' }',
  31141. ');',
  31142. 'rtl.createInterface(',
  31143. ' this,',
  31144. ' "IBird",',
  31145. ' "{9CC77572-0E45-3594-9A88-9E8D865C9E0A}",',
  31146. ' ["GetItem", "SetItem"],',
  31147. ' this.IUnknown,',
  31148. ' function () {',
  31149. ' var $r = this.$rtti;',
  31150. ' $r.addMethod("GetItem", 1, [], rtl.longint);',
  31151. ' $r.addMethod("SetItem", 0, [["Value", rtl.longint]]);',
  31152. ' $r.addProperty("Item", 3, rtl.longint, "GetItem", "SetItem");',
  31153. ' }',
  31154. ');',
  31155. 'this.i = null;',
  31156. 'this.t = null;',
  31157. '']),
  31158. LinesToStr([ // $mod.$main
  31159. '$mod.t = $mod.$rtti["IBird"];',
  31160. '$mod.t = $mod.i.$rtti;',
  31161. '']));
  31162. end;
  31163. procedure TTestModule.TestRTTI_ClassHelper;
  31164. begin
  31165. WithTypeInfo:=true;
  31166. StartProgram(true,[supTypeInfo]);
  31167. Add([
  31168. '{$interfaces com}',
  31169. '{$modeswitch externalclass}',
  31170. 'type',
  31171. ' TObject = class',
  31172. ' end;',
  31173. ' THelper = class helper for TObject',
  31174. ' published',
  31175. ' function GetItem: longint;',
  31176. ' property Item: longint read GetItem;',
  31177. ' end;',
  31178. 'function THelper.GetItem: longint;',
  31179. 'begin',
  31180. 'end;',
  31181. 'var',
  31182. ' t: TTypeInfoHelper;',
  31183. 'begin',
  31184. ' t:=TypeInfo(THelper);',
  31185. '']);
  31186. ConvertProgram;
  31187. CheckSource('TestRTTI_ClassHelper',
  31188. LinesToStr([ // statements
  31189. 'rtl.createClass(this, "TObject", null, function () {',
  31190. ' this.$init = function () {',
  31191. ' };',
  31192. ' this.$final = function () {',
  31193. ' };',
  31194. '});',
  31195. 'rtl.createHelper(this, "THelper", null, function () {',
  31196. ' this.GetItem = function () {',
  31197. ' var Result = 0;',
  31198. ' return Result;',
  31199. ' };',
  31200. ' var $r = this.$rtti;',
  31201. ' $r.addMethod("GetItem", 1, [], rtl.longint);',
  31202. ' $r.addProperty("Item", 1, rtl.longint, "GetItem", "");',
  31203. '});',
  31204. 'this.t = null;',
  31205. '']),
  31206. LinesToStr([ // $mod.$main
  31207. '$mod.t = $mod.$rtti["THelper"];',
  31208. '']));
  31209. end;
  31210. procedure TTestModule.TestRTTI_ExternalClass;
  31211. begin
  31212. WithTypeInfo:=true;
  31213. StartProgram(true,[supTypeInfo]);
  31214. Add([
  31215. '{$modeswitch externalclass}',
  31216. 'type',
  31217. ' TJSObject = class external name ''Object''',
  31218. ' end;',
  31219. ' TJSArray = class external name ''Array'' (TJSObject)',
  31220. ' end;',
  31221. 'var',
  31222. ' p: Pointer;',
  31223. ' tc: TTypeInfoExtClass;',
  31224. 'begin',
  31225. ' p:=typeinfo(TJSArray);']);
  31226. ConvertProgram;
  31227. CheckSource('TestRTTI_ExternalClass',
  31228. LinesToStr([ // statements
  31229. 'this.$rtti.$ExtClass("TJSObject", {',
  31230. ' jsclass: "Object"',
  31231. '});',
  31232. 'this.$rtti.$ExtClass("TJSArray", {',
  31233. ' ancestor: this.$rtti["TJSObject"],',
  31234. ' jsclass: "Array"',
  31235. '});',
  31236. 'this.p = null;',
  31237. 'this.tc = null;',
  31238. '']),
  31239. LinesToStr([ // $mod.$main
  31240. '$mod.p = $mod.$rtti["TJSArray"];',
  31241. '']));
  31242. end;
  31243. procedure TTestModule.TestRTTI_Unit;
  31244. begin
  31245. WithTypeInfo:=true;
  31246. AddModuleWithIntfImplSrc('unit2.pas',
  31247. LinesToStr([
  31248. '{$mode delphi}',
  31249. 'type',
  31250. ' TWordArray = array of word;',
  31251. ' TArray<T> = array of T;',
  31252. '']),
  31253. '');
  31254. StartUnit(true,[supTypeInfo,supTInterfacedObject]);
  31255. Add([
  31256. '{$mode delphi}',
  31257. 'interface',
  31258. 'uses unit2;',
  31259. 'type',
  31260. ' IBird = interface',
  31261. ' function Swoop: TWordArray;',
  31262. ' function Glide: TArray<word>;',
  31263. ' end;',
  31264. 'procedure Fly;',
  31265. 'implementation',
  31266. 'procedure Fly;',
  31267. 'var',
  31268. ' ta: tTypeInfoDynArray;',
  31269. ' ti: tTypeInfoInterface;',
  31270. 'begin',
  31271. ' ta:=typeinfo(TWordArray);',
  31272. ' ta:=typeinfo(TArray<word>);',
  31273. ' ti:=typeinfo(IBird);',
  31274. 'end;',
  31275. '']);
  31276. ConvertUnit;
  31277. CheckSource('TestRTTI_ExternalClass',
  31278. LinesToStr([ // statements
  31279. 'rtl.createInterface(',
  31280. ' this,',
  31281. ' "IBird",',
  31282. ' "{3B98AAAC-6116-3E17-AA85-F16786D85B09}",',
  31283. ' ["Swoop", "Glide"],',
  31284. ' pas.system.IUnknown,',
  31285. ' function () {',
  31286. ' var $r = this.$rtti;',
  31287. ' $r.addMethod("Swoop", 1, [], pas.unit2.$rtti["TWordArray"]);',
  31288. ' $r.addMethod("Glide", 1, [], pas.unit2.$rtti["TArray<System.Word>"]);',
  31289. ' }',
  31290. ');',
  31291. 'this.Fly = function () {',
  31292. ' var ta = null;',
  31293. ' var ti = null;',
  31294. ' ta = pas.unit2.$rtti["TWordArray"];',
  31295. ' ta = pas.unit2.$rtti["TArray<System.Word>"];',
  31296. ' ti = $mod.$rtti["IBird"];',
  31297. '};',
  31298. '']),
  31299. LinesToStr([ // $mod.$main
  31300. '']));
  31301. end;
  31302. procedure TTestModule.TestResourcestringProgram;
  31303. begin
  31304. AddModuleWithIntfImplSrc('unit2.pas',
  31305. LinesToStr([
  31306. 'resourcestring Title = ''Nice'';',
  31307. '']),
  31308. '');
  31309. StartProgram(true);
  31310. Add([
  31311. 'uses unit2;',
  31312. 'const Bar = ''bar'';',
  31313. 'resourcestring',
  31314. ' Red = ''red'';',
  31315. ' Foobar = ''fOo''+bar;',
  31316. 'var s: string;',
  31317. ' c: char;',
  31318. 'begin',
  31319. ' s:=red;',
  31320. ' s:=test1.red;',
  31321. ' s:=Title;',
  31322. ' c:=red[1];',
  31323. ' c:=test1.red[2];',
  31324. ' if red=foobar then ;',
  31325. ' if red[3]=red[4] then ;']);
  31326. ConvertProgram;
  31327. CheckSource('TestResourcestringProgram',
  31328. LinesToStr([ // statements
  31329. 'this.Bar = "bar";',
  31330. 'this.s = "";',
  31331. 'this.c = "";',
  31332. '$mod.$resourcestrings = {',
  31333. ' Red: {',
  31334. ' org: "red"',
  31335. ' },',
  31336. ' Foobar: {',
  31337. ' org: "fOobar"',
  31338. ' }',
  31339. '};',
  31340. '']),
  31341. LinesToStr([ // $mod.$main
  31342. '$mod.s = rtl.getResStr($mod, "Red");',
  31343. '$mod.s = rtl.getResStr($mod, "Red");',
  31344. '$mod.s = rtl.getResStr(pas.unit2, "Title");',
  31345. '$mod.c = rtl.getResStr($mod, "Red").charAt(0);',
  31346. '$mod.c = rtl.getResStr($mod, "Red").charAt(1);',
  31347. 'if (rtl.getResStr($mod, "Red") === rtl.getResStr($mod, "Foobar")) ;',
  31348. 'if (rtl.getResStr($mod, "Red").charAt(2) === rtl.getResStr($mod, "Red").charAt(3)) ;',
  31349. '']));
  31350. end;
  31351. procedure TTestModule.TestResourcestringUnit;
  31352. begin
  31353. AddModuleWithIntfImplSrc('unit2.pas',
  31354. LinesToStr([
  31355. 'resourcestring Title = ''Nice'';',
  31356. '']),
  31357. '');
  31358. StartUnit(true);
  31359. Add([
  31360. 'interface',
  31361. 'uses unit2;',
  31362. 'const Red = ''rEd'';',
  31363. 'resourcestring',
  31364. ' Blue = ''blue'';',
  31365. ' NotRed = ''not''+Red;',
  31366. 'var s: string;',
  31367. 'implementation',
  31368. 'resourcestring',
  31369. ' ImplGreen = ''green'';',
  31370. 'initialization',
  31371. ' s:=blue+ImplGreen;',
  31372. ' s:=test1.blue+test1.implgreen;',
  31373. ' s:=blue[1]+implgreen[2];',
  31374. ' s:=Title;',
  31375. '']);
  31376. ConvertUnit;
  31377. CheckSource('TestResourcestringUnit',
  31378. LinesToStr([ // statements
  31379. 'this.Red = "rEd";',
  31380. 'this.s = "";',
  31381. '$mod.$resourcestrings = {',
  31382. ' Blue: {',
  31383. ' org: "blue"',
  31384. ' },',
  31385. ' NotRed: {',
  31386. ' org: "notrEd"',
  31387. ' },',
  31388. ' ImplGreen: {',
  31389. ' org: "green"',
  31390. ' }',
  31391. '};',
  31392. '']),
  31393. LinesToStr([ // $mod.$main
  31394. '$mod.s = rtl.getResStr($mod, "Blue") + rtl.getResStr($mod, "ImplGreen");',
  31395. '$mod.s = rtl.getResStr($mod, "Blue") + rtl.getResStr($mod, "ImplGreen");',
  31396. '$mod.s = rtl.getResStr($mod, "Blue").charAt(0) + rtl.getResStr($mod, "ImplGreen").charAt(1);',
  31397. '$mod.s = rtl.getResStr(pas.unit2, "Title");',
  31398. '']));
  31399. end;
  31400. procedure TTestModule.TestResourcestringImplementation;
  31401. begin
  31402. StartUnit(false);
  31403. Add([
  31404. 'interface',
  31405. 'implementation',
  31406. 'resourcestring',
  31407. ' ImplRed = ''red'';']);
  31408. ConvertUnit;
  31409. CheckSource('TestResourcestringImplementation',
  31410. LinesToStr([ // intf statements
  31411. 'var $impl = $mod.$impl;']),
  31412. LinesToStr([ // $mod.$init
  31413. '']),
  31414. LinesToStr([ // impl statements
  31415. '$mod.$resourcestrings = {',
  31416. ' ImplRed: {',
  31417. ' org: "red"',
  31418. ' }',
  31419. '};',
  31420. '']));
  31421. end;
  31422. procedure TTestModule.TestAttributes_Members;
  31423. begin
  31424. WithTypeInfo:=true;
  31425. StartProgram(false);
  31426. Add([
  31427. '{$modeswitch PrefixedAttributes}',
  31428. 'type',
  31429. ' TObject = class',
  31430. ' constructor Create;',
  31431. ' end;',
  31432. ' TCustomAttribute = class',
  31433. ' constructor Create(Id: word);',
  31434. ' end;',
  31435. ' [Missing]',
  31436. ' TBird = class',
  31437. ' published',
  31438. ' [Tcustom]',
  31439. ' FField: word;',
  31440. ' [tcustom(14)]',
  31441. ' property Size: word read FField;',
  31442. ' [Tcustom(15)]',
  31443. ' procedure Fly; virtual; abstract;',
  31444. ' end;',
  31445. ' TRec = record',
  31446. ' [Tcustom,tcustom(14)]',
  31447. ' Size: word;',
  31448. ' end;',
  31449. 'constructor TObject.Create; begin end;',
  31450. 'constructor TCustomAttribute.Create(Id: word); begin end;',
  31451. 'begin',
  31452. '']);
  31453. ConvertProgram;
  31454. CheckSource('TestAttributes_Members',
  31455. LinesToStr([ // statements
  31456. 'rtl.createClass(this, "TObject", null, function () {',
  31457. ' this.$init = function () {',
  31458. ' };',
  31459. ' this.$final = function () {',
  31460. ' };',
  31461. ' this.Create = function () {',
  31462. ' return this;',
  31463. ' };',
  31464. '});',
  31465. 'rtl.createClass(this, "TCustomAttribute", this.TObject, function () {',
  31466. ' this.Create$1 = function (Id) {',
  31467. ' return this;',
  31468. ' };',
  31469. '});',
  31470. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  31471. ' this.$init = function () {',
  31472. ' $mod.TObject.$init.call(this);',
  31473. ' this.FField = 0;',
  31474. ' };',
  31475. ' var $r = this.$rtti;',
  31476. ' $r.addField("FField", rtl.word, {',
  31477. ' attr: [$mod.TCustomAttribute, "Create"]',
  31478. ' });',
  31479. ' $r.addProperty(',
  31480. ' "Size",',
  31481. ' 0,',
  31482. ' rtl.word,',
  31483. ' "FField",',
  31484. ' "",',
  31485. ' {',
  31486. ' attr: [$mod.TCustomAttribute, "Create$1", [14]]',
  31487. ' }',
  31488. ' );',
  31489. ' $r.addMethod("Fly", 0, [], null, 0, {',
  31490. ' attr: [$mod.TCustomAttribute, "Create$1", [15]]',
  31491. ' });',
  31492. '});',
  31493. 'rtl.recNewT(this, "TRec", function () {',
  31494. ' this.Size = 0;',
  31495. ' this.$eq = function (b) {',
  31496. ' return this.Size === b.Size;',
  31497. ' };',
  31498. ' this.$assign = function (s) {',
  31499. ' this.Size = s.Size;',
  31500. ' return this;',
  31501. ' };',
  31502. ' var $r = $mod.$rtti.$Record("TRec", {});',
  31503. ' $r.addField("Size", rtl.word, {',
  31504. ' attr: [',
  31505. ' $mod.TCustomAttribute,',
  31506. ' "Create",',
  31507. ' $mod.TCustomAttribute,',
  31508. ' "Create$1",',
  31509. ' [14]',
  31510. ' ]',
  31511. ' });',
  31512. '});',
  31513. '']),
  31514. LinesToStr([ // $mod.$main
  31515. '']));
  31516. end;
  31517. procedure TTestModule.TestAttributes_Types;
  31518. begin
  31519. WithTypeInfo:=true;
  31520. StartProgram(false);
  31521. Add([
  31522. '{$modeswitch PrefixedAttributes}',
  31523. 'type',
  31524. ' TObject = class',
  31525. ' constructor Create(Id: word);',
  31526. ' end;',
  31527. ' TCustomAttribute = class',
  31528. ' end;',
  31529. ' [TCustom(1)]',
  31530. ' TMyClass = class',
  31531. ' end;',
  31532. ' [TCustom(11)]',
  31533. ' TMyDescendant = class(TMyClass)',
  31534. ' end;',
  31535. ' [TCustom(2)]',
  31536. ' TRec = record',
  31537. ' end;',
  31538. ' [TCustom(3)]',
  31539. ' TInt = type word;',
  31540. 'constructor TObject.Create(Id: word);',
  31541. 'begin',
  31542. 'end;',
  31543. 'var p: pointer;',
  31544. 'begin',
  31545. ' p:=typeinfo(TMyClass);',
  31546. ' p:=typeinfo(TRec);',
  31547. ' p:=typeinfo(TInt);',
  31548. '']);
  31549. ConvertProgram;
  31550. CheckSource('TestAttributes_Types',
  31551. LinesToStr([ // statements
  31552. 'rtl.createClass(this, "TObject", null, function () {',
  31553. ' this.$init = function () {',
  31554. ' };',
  31555. ' this.$final = function () {',
  31556. ' };',
  31557. ' this.Create = function (Id) {',
  31558. ' return this;',
  31559. ' };',
  31560. '});',
  31561. 'rtl.createClass(this, "TCustomAttribute", this.TObject, function () {',
  31562. '});',
  31563. 'rtl.createClass(this, "TMyClass", this.TObject, function () {',
  31564. ' var $r = this.$rtti;',
  31565. ' $r.attr = [$mod.TCustomAttribute, "Create", [1]];',
  31566. '});',
  31567. 'rtl.createClass(this, "TMyDescendant", this.TMyClass, function () {',
  31568. ' var $r = this.$rtti;',
  31569. ' $r.attr = [$mod.TCustomAttribute, "Create", [11]];',
  31570. '});',
  31571. 'rtl.recNewT(this, "TRec", function () {',
  31572. ' this.$eq = function (b) {',
  31573. ' return true;',
  31574. ' };',
  31575. ' this.$assign = function (s) {',
  31576. ' return this;',
  31577. ' };',
  31578. ' $mod.$rtti.$Record("TRec", {',
  31579. ' attr: [$mod.TCustomAttribute, "Create", [2]]',
  31580. ' });',
  31581. '});',
  31582. 'this.$rtti.$inherited("TInt", rtl.word, {',
  31583. ' attr: [this.TCustomAttribute, "Create", [3]]',
  31584. '});',
  31585. 'this.p = null;',
  31586. '']),
  31587. LinesToStr([ // $mod.$main
  31588. '$mod.p = $mod.$rtti["TMyClass"];',
  31589. '$mod.p = $mod.$rtti["TRec"];',
  31590. '$mod.p = $mod.$rtti["TInt"];',
  31591. '']));
  31592. end;
  31593. procedure TTestModule.TestAttributes_HelperConstructor_Fail;
  31594. begin
  31595. WithTypeInfo:=true;
  31596. StartProgram(false);
  31597. Add([
  31598. '{$modeswitch PrefixedAttributes}',
  31599. 'type',
  31600. ' TObject = class',
  31601. ' constructor Create;',
  31602. ' end;',
  31603. ' TCustomAttribute = class',
  31604. ' end;',
  31605. ' THelper = class helper for TCustomAttribute',
  31606. ' constructor Create(Id: word);',
  31607. ' end;',
  31608. ' [TCustom(3)]',
  31609. ' TMyInt = word;',
  31610. 'constructor TObject.Create; begin end;',
  31611. 'constructor THelper.Create(Id: word); begin end;',
  31612. 'begin',
  31613. ' if typeinfo(TMyInt)=nil then ;']);
  31614. ConvertProgram;
  31615. end;
  31616. procedure TTestModule.TestAssert;
  31617. begin
  31618. StartProgram(false);
  31619. Add([
  31620. 'procedure DoIt;',
  31621. 'var',
  31622. ' b: boolean;',
  31623. ' s: string;',
  31624. 'begin',
  31625. ' {$Assertions on}',
  31626. ' Assert(b);',
  31627. 'end;',
  31628. 'begin',
  31629. ' DoIt;',
  31630. '']);
  31631. ConvertProgram;
  31632. CheckSource('TestAssert',
  31633. LinesToStr([ // statements
  31634. 'this.DoIt = function () {',
  31635. ' var b = false;',
  31636. ' var s = "";',
  31637. ' if (!b) throw "assert failed";',
  31638. '};',
  31639. '']),
  31640. LinesToStr([ // $mod.$main
  31641. '$mod.DoIt();',
  31642. '']));
  31643. end;
  31644. procedure TTestModule.TestAssert_SysUtils;
  31645. begin
  31646. AddModuleWithIntfImplSrc('SysUtils.pas',
  31647. LinesToStr([
  31648. 'type',
  31649. ' TObject = class',
  31650. ' constructor Create;',
  31651. ' end;',
  31652. ' EAssertionFailed = class',
  31653. ' constructor Create(s: string);',
  31654. ' end;',
  31655. '']),
  31656. LinesToStr([
  31657. 'constructor TObject.Create;',
  31658. 'begin end;',
  31659. 'constructor EAssertionFailed.Create(s: string);',
  31660. 'begin end;',
  31661. '']) );
  31662. StartProgram(true);
  31663. Add([
  31664. 'uses sysutils;',
  31665. 'procedure DoIt;',
  31666. 'var',
  31667. ' b: boolean;',
  31668. ' s: string;',
  31669. 'begin',
  31670. ' {$Assertions on}',
  31671. ' Assert(b);',
  31672. ' Assert(b,''msg'');',
  31673. 'end;',
  31674. 'begin',
  31675. ' DoIt;',
  31676. '']);
  31677. ConvertProgram;
  31678. CheckSource('TestAssert_SysUtils',
  31679. LinesToStr([ // statements
  31680. 'this.DoIt = function () {',
  31681. ' var b = false;',
  31682. ' var s = "";',
  31683. ' if (!b) throw pas.SysUtils.EAssertionFailed.$create("Create");',
  31684. ' if (!b) throw pas.SysUtils.EAssertionFailed.$create("Create$1", ["msg"]);',
  31685. '};',
  31686. '']),
  31687. LinesToStr([ // $mod.$main
  31688. '$mod.DoIt();',
  31689. '']));
  31690. end;
  31691. procedure TTestModule.TestObjectChecks;
  31692. begin
  31693. Scanner.CurrentBoolSwitches:=Scanner.CurrentBoolSwitches+[bsObjectChecks];
  31694. StartProgram(false);
  31695. Add([
  31696. 'type',
  31697. ' TObject = class',
  31698. ' procedure DoIt;',
  31699. ' end;',
  31700. ' TClass = class of tobject;',
  31701. ' TBird = class',
  31702. ' end;',
  31703. ' TBirdClass = class of TBird;',
  31704. 'var',
  31705. ' o : TObject;',
  31706. ' c: TClass;',
  31707. ' b: TBird;',
  31708. ' bc: TBirdClass;',
  31709. 'procedure TObject.DoIt;',
  31710. 'begin',
  31711. ' b:=TBird(o);',
  31712. 'end;',
  31713. 'begin',
  31714. ' o.DoIt;',
  31715. ' b:=TBird(o);',
  31716. ' bc:=TBirdClass(c);',
  31717. '']);
  31718. ConvertProgram;
  31719. CheckSource('TestCheckMethodCall',
  31720. LinesToStr([ // statements
  31721. 'rtl.createClass(this, "TObject", null, function () {',
  31722. ' this.$init = function () {',
  31723. ' };',
  31724. ' this.$final = function () {',
  31725. ' };',
  31726. ' this.DoIt = function () {',
  31727. ' rtl.checkMethodCall(this,$mod.TObject);',
  31728. ' $mod.b = rtl.asExt($mod.o, $mod.TBird, 1);',
  31729. ' };',
  31730. '});',
  31731. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  31732. '});',
  31733. 'this.o = null;',
  31734. 'this.c = null;',
  31735. 'this.b = null;',
  31736. 'this.bc = null;',
  31737. '']),
  31738. LinesToStr([ // $mod.$main
  31739. '$mod.o.DoIt();',
  31740. '$mod.b = rtl.asExt($mod.o,$mod.TBird, 1);',
  31741. '$mod.bc = rtl.asExt($mod.c, $mod.TBird, 2);',
  31742. '']));
  31743. end;
  31744. procedure TTestModule.TestOverflowChecks_Int;
  31745. begin
  31746. Scanner.CurrentBoolSwitches:=Scanner.CurrentBoolSwitches+[bsOverflowChecks];
  31747. StartProgram(false);
  31748. Add([
  31749. 'procedure DoIt;',
  31750. 'var',
  31751. ' b: byte;',
  31752. ' n: nativeint;',
  31753. ' u: nativeuint;',
  31754. ' c: currency;',
  31755. 'begin',
  31756. ' n:=n+n;',
  31757. ' n:=n-n;',
  31758. ' n:=n+b;',
  31759. ' n:=b-n;',
  31760. ' n:=n*n;',
  31761. ' n:=n*u;',
  31762. ' c:=c+b;',
  31763. ' c:=b+c;',
  31764. ' c:=c*b;',
  31765. ' c:=b*c;',
  31766. 'end;',
  31767. 'begin',
  31768. '']);
  31769. ConvertProgram;
  31770. CheckSource('TestOverflowChecks_Int',
  31771. LinesToStr([ // statements
  31772. 'this.DoIt = function () {',
  31773. ' var b = 0;',
  31774. ' var n = 0;',
  31775. ' var u = 0;',
  31776. ' var c = 0;',
  31777. ' n = rtl.oc(n + n);',
  31778. ' n = rtl.oc(n - n);',
  31779. ' n = rtl.oc(n + b);',
  31780. ' n = rtl.oc(b - n);',
  31781. ' n = rtl.oc(n * n);',
  31782. ' n = rtl.oc(n * u);',
  31783. ' c = rtl.oc(c + (b * 10000));',
  31784. ' c = rtl.oc((b * 10000) + c);',
  31785. ' c = rtl.oc(c * b);',
  31786. ' c = rtl.oc(b * c);',
  31787. '};',
  31788. '']),
  31789. LinesToStr([ // $mod.$main
  31790. '']));
  31791. end;
  31792. procedure TTestModule.TestRangeChecks_AssignInt;
  31793. begin
  31794. Scanner.Options:=Scanner.Options+[po_CAssignments];
  31795. StartProgram(false);
  31796. Add([
  31797. '{$R+}',
  31798. 'var',
  31799. ' b: byte = 2;',
  31800. ' w: word = 3;',
  31801. 'procedure DoIt(p: byte);',
  31802. 'begin',
  31803. ' b:=w;',
  31804. ' b+=w;',
  31805. ' b:=1;',
  31806. 'end;',
  31807. '{$R-}',
  31808. 'procedure DoSome;',
  31809. 'begin',
  31810. ' DoIt(w);',
  31811. ' b:=w;',
  31812. ' b:=2;',
  31813. 'end;',
  31814. 'begin',
  31815. '{$R+}',
  31816. '']);
  31817. ConvertProgram;
  31818. CheckSource('TestRangeChecks_AssignInt',
  31819. LinesToStr([ // statements
  31820. 'this.b = 2;',
  31821. 'this.w = 3;',
  31822. 'this.DoIt = function (p) {',
  31823. ' rtl.rc(p, 0, 255);',
  31824. ' $mod.b = rtl.rc($mod.w,0,255);',
  31825. ' rtl.rc($mod.b += $mod.w, 0, 255);',
  31826. ' $mod.b = 1;',
  31827. '};',
  31828. 'this.DoSome = function () {',
  31829. ' $mod.DoIt($mod.w);',
  31830. ' $mod.b = $mod.w;',
  31831. ' $mod.b = 2;',
  31832. '};',
  31833. '']),
  31834. LinesToStr([ // $mod.$main
  31835. '']));
  31836. end;
  31837. procedure TTestModule.TestRangeChecks_AssignIntRange;
  31838. begin
  31839. Scanner.Options:=Scanner.Options+[po_CAssignments];
  31840. StartProgram(false);
  31841. Add([
  31842. '{$R+}',
  31843. 'type Ten = 1..10;',
  31844. 'var',
  31845. ' b: Ten = 2;',
  31846. ' w: Ten = 3;',
  31847. 'procedure DoIt(p: Ten);',
  31848. 'begin',
  31849. ' b:=w;',
  31850. ' b+=w;',
  31851. ' b:=1;',
  31852. 'end;',
  31853. '{$R-}',
  31854. 'procedure DoSome;',
  31855. 'begin',
  31856. ' DoIt(w);',
  31857. ' b:=w;',
  31858. ' b:=2;',
  31859. 'end;',
  31860. 'begin',
  31861. '{$R+}',
  31862. '']);
  31863. ConvertProgram;
  31864. CheckSource('TestRangeChecks_AssignIntRange',
  31865. LinesToStr([ // statements
  31866. 'this.b = 2;',
  31867. 'this.w = 3;',
  31868. 'this.DoIt = function (p) {',
  31869. ' rtl.rc(p, 1, 10);',
  31870. ' $mod.b = rtl.rc($mod.w, 1, 10);',
  31871. ' rtl.rc($mod.b += $mod.w, 1, 10);',
  31872. ' $mod.b = 1;',
  31873. '};',
  31874. 'this.DoSome = function () {',
  31875. ' $mod.DoIt($mod.w);',
  31876. ' $mod.b = $mod.w;',
  31877. ' $mod.b = 2;',
  31878. '};',
  31879. '']),
  31880. LinesToStr([ // $mod.$main
  31881. '']));
  31882. end;
  31883. procedure TTestModule.TestRangeChecks_AssignEnum;
  31884. begin
  31885. StartProgram(false);
  31886. Add([
  31887. '{$R+}',
  31888. 'type TEnum = (red,green);',
  31889. 'var',
  31890. ' e: TEnum = red;',
  31891. 'procedure DoIt(p: TEnum);',
  31892. 'begin',
  31893. ' e:=p;',
  31894. ' p:=TEnum(0);',
  31895. ' p:=succ(e);',
  31896. 'end;',
  31897. '{$R-}',
  31898. 'procedure DoSome;',
  31899. 'begin',
  31900. ' DoIt(e);',
  31901. ' e:=TEnum(1);',
  31902. ' e:=pred(e);',
  31903. 'end;',
  31904. 'begin',
  31905. '{$R+}',
  31906. '']);
  31907. ConvertProgram;
  31908. CheckSource('TestRangeChecks_AssignEnum',
  31909. LinesToStr([ // statements
  31910. 'this.TEnum = {',
  31911. ' "0": "red",',
  31912. ' red: 0,',
  31913. ' "1": "green",',
  31914. ' green: 1',
  31915. '};',
  31916. 'this.e = this.TEnum.red;',
  31917. 'this.DoIt = function (p) {',
  31918. ' rtl.rc(p, 0, 1);',
  31919. ' $mod.e = rtl.rc(p, 0, 1);',
  31920. ' p = 0;',
  31921. ' p = rtl.rc($mod.e + 1, 0, 1);',
  31922. '};',
  31923. 'this.DoSome = function () {',
  31924. ' $mod.DoIt($mod.e);',
  31925. ' $mod.e = 1;',
  31926. ' $mod.e = $mod.e - 1;',
  31927. '};',
  31928. '']),
  31929. LinesToStr([ // $mod.$main
  31930. '']));
  31931. end;
  31932. procedure TTestModule.TestRangeChecks_AssignEnumRange;
  31933. begin
  31934. StartProgram(false);
  31935. Add([
  31936. '{$R+}',
  31937. 'type',
  31938. ' TEnum = (red,green);',
  31939. ' TEnumRg = red..green;',
  31940. 'var',
  31941. ' e: TEnumRg = red;',
  31942. 'procedure DoIt(p: TEnumRg);',
  31943. 'begin',
  31944. ' e:=p;',
  31945. ' p:=TEnumRg(0);',
  31946. ' p:=succ(e);',
  31947. 'end;',
  31948. '{$R-}',
  31949. 'procedure DoSome;',
  31950. 'begin',
  31951. ' DoIt(e);',
  31952. ' e:=TEnum(1);',
  31953. ' e:=pred(e);',
  31954. 'end;',
  31955. 'begin',
  31956. '{$R+}',
  31957. '']);
  31958. ConvertProgram;
  31959. CheckSource('TestRangeChecks_AssignEnumRange',
  31960. LinesToStr([ // statements
  31961. 'this.TEnum = {',
  31962. ' "0": "red",',
  31963. ' red: 0,',
  31964. ' "1": "green",',
  31965. ' green: 1',
  31966. '};',
  31967. 'this.e = this.TEnum.red;',
  31968. 'this.DoIt = function (p) {',
  31969. ' rtl.rc(p, 0, 1);',
  31970. ' $mod.e = rtl.rc(p, 0, 1);',
  31971. ' p = 0;',
  31972. ' p = rtl.rc($mod.e + 1, 0, 1);',
  31973. '};',
  31974. 'this.DoSome = function () {',
  31975. ' $mod.DoIt($mod.e);',
  31976. ' $mod.e = 1;',
  31977. ' $mod.e = $mod.e - 1;',
  31978. '};',
  31979. '']),
  31980. LinesToStr([ // $mod.$main
  31981. '']));
  31982. end;
  31983. procedure TTestModule.TestRangeChecks_AssignChar;
  31984. begin
  31985. StartProgram(false);
  31986. Add([
  31987. '{$R+}',
  31988. 'type',
  31989. ' TLetter = char;',
  31990. 'var',
  31991. ' b: TLetter = ''2'';',
  31992. ' w: TLetter = ''3'';',
  31993. 'procedure DoIt(p: TLetter);',
  31994. 'begin',
  31995. ' b:=w;',
  31996. ' b:=''1'';',
  31997. 'end;',
  31998. '{$R-}',
  31999. 'procedure DoSome;',
  32000. 'begin',
  32001. ' DoIt(w);',
  32002. ' b:=w;',
  32003. ' b:=''2'';',
  32004. 'end;',
  32005. 'begin',
  32006. '{$R+}',
  32007. '']);
  32008. ConvertProgram;
  32009. CheckSource('TestRangeChecks_AssignChar',
  32010. LinesToStr([ // statements
  32011. 'this.b = "2";',
  32012. 'this.w = "3";',
  32013. 'this.DoIt = function (p) {',
  32014. ' rtl.rcc(p, 0, 65535);',
  32015. ' $mod.b = rtl.rcc($mod.w, 0, 65535);',
  32016. ' $mod.b = "1";',
  32017. '};',
  32018. 'this.DoSome = function () {',
  32019. ' $mod.DoIt($mod.w);',
  32020. ' $mod.b = $mod.w;',
  32021. ' $mod.b = "2";',
  32022. '};',
  32023. '']),
  32024. LinesToStr([ // $mod.$main
  32025. '']));
  32026. end;
  32027. procedure TTestModule.TestRangeChecks_AssignCharRange;
  32028. begin
  32029. StartProgram(false);
  32030. Add([
  32031. '{$R+}',
  32032. 'type TDigit = ''0''..''9'';',
  32033. 'var',
  32034. ' b: TDigit = ''2'';',
  32035. ' w: TDigit = ''3'';',
  32036. 'procedure DoIt(p: TDigit);',
  32037. 'begin',
  32038. ' b:=w;',
  32039. ' b:=''1'';',
  32040. 'end;',
  32041. '{$R-}',
  32042. 'procedure DoSome;',
  32043. 'begin',
  32044. ' DoIt(w);',
  32045. ' b:=w;',
  32046. ' b:=''2'';',
  32047. 'end;',
  32048. 'begin',
  32049. '{$R+}',
  32050. '']);
  32051. ConvertProgram;
  32052. CheckSource('TestRangeChecks_AssignCharRange',
  32053. LinesToStr([ // statements
  32054. 'this.b = "2";',
  32055. 'this.w = "3";',
  32056. 'this.DoIt = function (p) {',
  32057. ' rtl.rcc(p, 48, 57);',
  32058. ' $mod.b = rtl.rcc($mod.w, 48, 57);',
  32059. ' $mod.b = "1";',
  32060. '};',
  32061. 'this.DoSome = function () {',
  32062. ' $mod.DoIt($mod.w);',
  32063. ' $mod.b = $mod.w;',
  32064. ' $mod.b = "2";',
  32065. '};',
  32066. '']),
  32067. LinesToStr([ // $mod.$main
  32068. '']));
  32069. end;
  32070. procedure TTestModule.TestRangeChecks_ArrayIndex;
  32071. begin
  32072. StartProgram(false);
  32073. Add([
  32074. '{$R+}',
  32075. 'type',
  32076. ' Ten = 1..10;',
  32077. ' TArr = array of Ten;',
  32078. ' TArrArr = array of TArr;',
  32079. ' TArrByte = array[byte] of Ten;',
  32080. ' TArrChar = array[''0''..''9''] of Ten;',
  32081. ' TArrByteChar = array[byte,''0''..''9''] of Ten;',
  32082. ' TObject = class',
  32083. ' A: TArr;',
  32084. ' end;',
  32085. 'procedure DoIt;',
  32086. 'var',
  32087. ' Arr: TArr;',
  32088. ' ArrArr: TArrArr;',
  32089. ' ArrByte: TArrByte;',
  32090. ' ArrChar: TArrChar;',
  32091. ' ArrByteChar: TArrByteChar;',
  32092. ' i: Ten;',
  32093. ' c: char;',
  32094. ' o: tobject;',
  32095. 'begin',
  32096. ' i:=Arr[1];',
  32097. ' i:=ArrByteChar[1,''2''];',
  32098. ' Arr[1]:=Arr[1];',
  32099. ' Arr[i]:=Arr[i];',
  32100. ' ArrByte[3]:=ArrByte[3];',
  32101. ' ArrByte[i]:=ArrByte[i];',
  32102. ' ArrChar[''5'']:=ArrChar[''5''];',
  32103. ' ArrChar[c]:=ArrChar[c];',
  32104. ' ArrByteChar[7,''7'']:=ArrByteChar[7,''7''];',
  32105. ' ArrByteChar[i,c]:=ArrByteChar[i,c];',
  32106. ' o.a[i]:=o.a[i];',
  32107. 'end;',
  32108. 'begin',
  32109. '']);
  32110. ConvertProgram;
  32111. CheckSource('TestRangeChecks_ArrayIndex',
  32112. LinesToStr([ // statements
  32113. 'this.TArrByteChar$clone = function (a) {',
  32114. ' var b = [];',
  32115. ' b.length = 256;',
  32116. ' for (var c = 0; c < 256; c++) b[c] = a[c].slice(0);',
  32117. ' return b;',
  32118. '};',
  32119. 'rtl.createClass(this, "TObject", null, function () {',
  32120. ' this.$init = function () {',
  32121. ' this.A = [];',
  32122. ' };',
  32123. ' this.$final = function () {',
  32124. ' this.A = undefined;',
  32125. ' };',
  32126. '});',
  32127. 'this.DoIt = function () {',
  32128. ' var Arr = [];',
  32129. ' var ArrArr = [];',
  32130. ' var ArrByte = rtl.arraySetLength(null, 0, 256);',
  32131. ' var ArrChar = rtl.arraySetLength(null, 0, 10);',
  32132. ' var ArrByteChar = rtl.arraySetLength(null, 0, 256, 10);',
  32133. ' var i = 0;',
  32134. ' var c = "";',
  32135. ' var o = null;',
  32136. ' i = rtl.rc(Arr[1], 1, 10);',
  32137. ' i = rtl.rc(ArrByteChar[1][2], 1, 10);',
  32138. ' Arr[1] = rtl.rc(Arr[1], 1, 10);',
  32139. ' rtl.rcArrW(Arr, i, rtl.rcArrR(Arr, i));',
  32140. ' ArrByte[3] = rtl.rc(ArrByte[3], 1, 10);',
  32141. ' rtl.rcArrW(ArrByte, i, rtl.rcArrR(ArrByte, i));',
  32142. ' ArrChar[5] = rtl.rc(ArrChar[5], 1, 10);',
  32143. ' rtl.rcArrW(ArrChar, c.charCodeAt() - 48, rtl.rcArrR(ArrChar, c.charCodeAt() - 48));',
  32144. ' ArrByteChar[7][7] = rtl.rc(ArrByteChar[7][7], 1, 10);',
  32145. ' rtl.rcArrW(ArrByteChar, i, c.charCodeAt() - 48, rtl.rcArrR(ArrByteChar, i, c.charCodeAt() - 48));',
  32146. ' rtl.rcArrW(o.A, i, rtl.rcArrR(o.A, i));',
  32147. '};',
  32148. '']),
  32149. LinesToStr([ // $mod.$main
  32150. '']));
  32151. end;
  32152. procedure TTestModule.TestRangeChecks_ArrayOfRecIndex;
  32153. begin
  32154. StartProgram(false);
  32155. Add([
  32156. '{$R+}',
  32157. 'type',
  32158. ' Ten = 1..10;',
  32159. ' TRec = record x: Ten end;',
  32160. ' TArr = array of TRec;',
  32161. ' TArrArr = array of TArr;',
  32162. ' TObject = class',
  32163. ' A: TArr;',
  32164. ' end;',
  32165. 'procedure DoIt;',
  32166. 'var',
  32167. ' Arr: TArr;',
  32168. ' ArrArr: TArrArr;',
  32169. ' i: Ten;',
  32170. ' o: tobject;',
  32171. 'begin',
  32172. ' Arr[1]:=Arr[1];',
  32173. ' Arr[i]:=Arr[i+1];',
  32174. ' o.a[i]:=o.a[i+2];',
  32175. 'end;',
  32176. 'begin',
  32177. '']);
  32178. ConvertProgram;
  32179. CheckSource('TestRangeChecks_ArrayOfRecIndex',
  32180. LinesToStr([ // statements
  32181. 'rtl.recNewT(this, "TRec", function () {',
  32182. ' this.x = 0;',
  32183. ' this.$eq = function (b) {',
  32184. ' return this.x === b.x;',
  32185. ' };',
  32186. ' this.$assign = function (s) {',
  32187. ' this.x = s.x;',
  32188. ' return this;',
  32189. ' };',
  32190. '});',
  32191. 'rtl.createClass(this, "TObject", null, function () {',
  32192. ' this.$init = function () {',
  32193. ' this.A = [];',
  32194. ' };',
  32195. ' this.$final = function () {',
  32196. ' this.A = undefined;',
  32197. ' };',
  32198. '});',
  32199. 'this.DoIt = function () {',
  32200. ' var Arr = [];',
  32201. ' var ArrArr = [];',
  32202. ' var i = 0;',
  32203. ' var o = null;',
  32204. ' Arr[1].$assign(Arr[1]);',
  32205. ' rtl.rcArrR(Arr, i).$assign(rtl.rcArrR(Arr, i + 1));',
  32206. ' rtl.rcArrR(o.A, i).$assign(rtl.rcArrR(o.A, i + 2));',
  32207. '};',
  32208. '']),
  32209. LinesToStr([ // $mod.$main
  32210. '']));
  32211. end;
  32212. procedure TTestModule.TestRangeChecks_StringIndex;
  32213. begin
  32214. StartProgram(false);
  32215. Add([
  32216. 'type',
  32217. ' TObject = class',
  32218. ' S: string;',
  32219. ' end;',
  32220. '{$R+}',
  32221. 'procedure DoIt(var h: string);',
  32222. 'var',
  32223. ' s: string;',
  32224. ' i: longint;',
  32225. ' c: char;',
  32226. ' o: tobject;',
  32227. 'begin',
  32228. ' c:=s[1];',
  32229. ' s[i]:=s[i];',
  32230. ' h[i]:=h[i];',
  32231. ' c:=o.s[i];',
  32232. ' o.s[i]:=c;',
  32233. 'end;',
  32234. 'begin',
  32235. '']);
  32236. ConvertProgram;
  32237. CheckSource('TestRangeChecks_StringIndex',
  32238. LinesToStr([ // statements
  32239. 'rtl.createClass(this, "TObject", null, function () {',
  32240. ' this.$init = function () {',
  32241. ' this.S = "";',
  32242. ' };',
  32243. ' this.$final = function () {',
  32244. ' };',
  32245. '});',
  32246. 'this.DoIt = function (h) {',
  32247. ' var s = "";',
  32248. ' var i = 0;',
  32249. ' var c = "";',
  32250. ' var o = null;',
  32251. ' c = rtl.rcc(rtl.rcCharAt(s, 0), 0, 65535);',
  32252. ' s = rtl.rcSetCharAt(s, i - 1, rtl.rcCharAt(s, i - 1));',
  32253. ' h.set(rtl.rcSetCharAt(h.get(), i - 1, rtl.rcCharAt(h.get(), i - 1)));',
  32254. ' c = rtl.rcc(rtl.rcCharAt(o.S, i - 1), 0, 65535);',
  32255. ' o.S = rtl.rcSetCharAt(o.S, i - 1, c);',
  32256. '};',
  32257. '']),
  32258. LinesToStr([ // $mod.$main
  32259. '']));
  32260. end;
  32261. procedure TTestModule.TestRangeChecks_TypecastInt;
  32262. begin
  32263. StartProgram(false);
  32264. Add([
  32265. '{$R+}',
  32266. 'var',
  32267. ' i: nativeint;',
  32268. ' b: byte;',
  32269. ' sh: shortint;',
  32270. ' w: word;',
  32271. ' sm: smallint;',
  32272. ' lw: longword;',
  32273. ' li: longint;',
  32274. 'begin',
  32275. ' b:=12+byte(i);',
  32276. ' sh:=12+shortint(i);',
  32277. ' w:=12+word(i);',
  32278. ' sm:=12+smallint(i);',
  32279. ' lw:=12+longword(i);',
  32280. ' li:=12+longint(i);',
  32281. '']);
  32282. ConvertProgram;
  32283. CheckSource('TestRangeChecks_TypecastInt',
  32284. LinesToStr([
  32285. 'this.i = 0;',
  32286. 'this.b = 0;',
  32287. 'this.sh = 0;',
  32288. 'this.w = 0;',
  32289. 'this.sm = 0;',
  32290. 'this.lw = 0;',
  32291. 'this.li = 0;',
  32292. '']),
  32293. LinesToStr([
  32294. '$mod.b = rtl.rc(12 + rtl.rc($mod.i, 0, 255), 0, 255);',
  32295. '$mod.sh = rtl.rc(12 + rtl.rc($mod.i, -128, 127), -128, 127);',
  32296. '$mod.w = rtl.rc(12 + rtl.rc($mod.i, 0, 65535), 0, 65535);',
  32297. '$mod.sm = rtl.rc(12 + rtl.rc($mod.i, -32768, 32767), -32768, 32767);',
  32298. '$mod.lw = rtl.rc(12 + rtl.rc($mod.i, 0, 4294967295), 0, 4294967295);',
  32299. '$mod.li = rtl.rc(12 + rtl.rc($mod.i, -2147483648, 2147483647), -2147483648, 2147483647);',
  32300. '']));
  32301. end;
  32302. procedure TTestModule.TestRangeChecks_TypeHelperInt;
  32303. begin
  32304. Scanner.Options:=Scanner.Options+[po_CAssignments];
  32305. StartProgram(false);
  32306. Add([
  32307. '{$modeswitch typehelpers}',
  32308. '{$R+}',
  32309. 'type',
  32310. ' TObject = class',
  32311. ' FSize: byte;',
  32312. ' property Size: byte read FSize;',
  32313. ' end;',
  32314. ' THelper = type helper for byte',
  32315. ' procedure SetIt(w: word);',
  32316. ' end;',
  32317. 'procedure THelper.SetIt(w: word);',
  32318. 'begin',
  32319. ' Self:=w;',
  32320. 'end;',
  32321. 'function GetIt: byte;',
  32322. 'begin',
  32323. ' Result.SetIt(2);',
  32324. 'end;',
  32325. 'var',
  32326. ' b: byte = 3;',
  32327. ' o: TObject;',
  32328. 'begin',
  32329. ' b.SetIt(14);',
  32330. ' with b do SetIt(15);',
  32331. ' o.Size.SetIt(16);',
  32332. '']);
  32333. ConvertProgram;
  32334. CheckSource('TestRangeChecks_AssignInt',
  32335. LinesToStr([ // statements
  32336. 'rtl.createClass(this, "TObject", null, function () {',
  32337. ' this.$init = function () {',
  32338. ' this.FSize = 0;',
  32339. ' };',
  32340. ' this.$final = function () {',
  32341. ' };',
  32342. '});',
  32343. 'rtl.createHelper(this, "THelper", null, function () {',
  32344. ' this.SetIt = function (w) {',
  32345. ' rtl.rc(w, 0, 65535);',
  32346. ' this.set(w);',
  32347. ' };',
  32348. '});',
  32349. 'this.GetIt = function () {',
  32350. ' var Result = 0;',
  32351. ' $mod.THelper.SetIt.call({',
  32352. ' get: function () {',
  32353. ' return Result;',
  32354. ' },',
  32355. ' set: function (v) {',
  32356. ' rtl.rc(v, 0, 255);',
  32357. ' Result = v;',
  32358. ' }',
  32359. ' }, 2);',
  32360. ' return Result;',
  32361. '};',
  32362. 'this.b = 3;',
  32363. 'this.o = null;',
  32364. '']),
  32365. LinesToStr([ // $mod.$main
  32366. '$mod.THelper.SetIt.call({',
  32367. ' p: $mod,',
  32368. ' get: function () {',
  32369. ' return this.p.b;',
  32370. ' },',
  32371. ' set: function (v) {',
  32372. ' rtl.rc(v, 0, 255);',
  32373. ' this.p.b = v;',
  32374. ' }',
  32375. '}, 14);',
  32376. 'var $with = $mod.b;',
  32377. '$mod.THelper.SetIt.call({',
  32378. ' get: function () {',
  32379. ' return $with;',
  32380. ' },',
  32381. ' set: function (v) {',
  32382. ' rtl.rc(v, 0, 255);',
  32383. ' $with = v;',
  32384. ' }',
  32385. '}, 15);',
  32386. '$mod.THelper.SetIt.call({',
  32387. ' p: $mod.o,',
  32388. ' get: function () {',
  32389. ' return this.p.FSize;',
  32390. ' },',
  32391. ' set: function (v) {',
  32392. ' rtl.rc(v, 0, 255);',
  32393. ' this.p.FSize = v;',
  32394. ' }',
  32395. '}, 16);',
  32396. '']));
  32397. end;
  32398. procedure TTestModule.TestAsync_Proc;
  32399. begin
  32400. StartProgram(false);
  32401. Add([
  32402. 'procedure Fly(w: word = 1); async; forward;',
  32403. 'procedure Run(w: word = 2); async;',
  32404. 'begin',
  32405. ' Fly(w);',
  32406. ' Fly;',
  32407. ' await(Fly(w));',
  32408. ' await(Fly);',
  32409. 'end;',
  32410. 'procedure Fly(w: word); ',
  32411. 'begin',
  32412. 'end;',
  32413. 'begin',
  32414. ' Run;',
  32415. ' Run(3);',
  32416. '']);
  32417. CheckResolverUnexpectedHints();
  32418. ConvertProgram;
  32419. CheckSource('TestAsync_Proc',
  32420. LinesToStr([ // statements
  32421. 'this.Run = async function (w) {',
  32422. ' $mod.Fly(w);',
  32423. ' $mod.Fly(1);',
  32424. ' await $mod.Fly(w);',
  32425. ' await $mod.Fly(1);',
  32426. '};',
  32427. 'this.Fly = async function (w) {',
  32428. '};',
  32429. '']),
  32430. LinesToStr([
  32431. '$mod.Run(2);',
  32432. '$mod.Run(3);',
  32433. '']));
  32434. end;
  32435. procedure TTestModule.TestAsync_CallResultIsPromise;
  32436. begin
  32437. StartProgram(false);
  32438. Add([
  32439. '{$modeswitch externalclass}',
  32440. 'type',
  32441. ' TObject = class',
  32442. ' end;',
  32443. ' TJSPromise = class external name ''Promise''',
  32444. ' end;',
  32445. ' TBird = class',
  32446. ' function Fly: word; async; ',
  32447. ' end;',
  32448. 'function TBird.Fly: word; async; ',
  32449. 'begin',
  32450. ' Result:=3;',
  32451. ' Fly:=4+Result;',
  32452. ' if Result=5 then ;',
  32453. ' exit(6);',
  32454. 'end;',
  32455. 'function Run: word; async;',
  32456. 'begin',
  32457. ' Result:=11+Result;',
  32458. ' inc(Result);',
  32459. 'end;',
  32460. 'var',
  32461. ' p: TJSPromise;',
  32462. ' o: TBird;',
  32463. 'begin',
  32464. ' p:=Run;',
  32465. ' p:=Run();',
  32466. ' if Run=p then ;',
  32467. ' if p=Run then ;',
  32468. ' if Run()=p then ;',
  32469. ' if p=Run() then ;',
  32470. ' p:=o.Fly;',
  32471. ' p:=o.Fly();',
  32472. ' if o.Fly=p then ;',
  32473. ' if o.Fly()=p then ;',
  32474. ' with o do begin',
  32475. ' p:=Fly;',
  32476. ' p:=Fly();',
  32477. ' if Fly=p then ;',
  32478. ' if Fly()=p then ;',
  32479. ' end;',
  32480. '']);
  32481. CheckResolverUnexpectedHints();
  32482. ConvertProgram;
  32483. CheckSource('TestAsync_CallResultIsPromise',
  32484. LinesToStr([ // statements
  32485. 'rtl.createClass(this, "TObject", null, function () {',
  32486. ' this.$init = function () {',
  32487. ' };',
  32488. ' this.$final = function () {',
  32489. ' };',
  32490. '});',
  32491. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  32492. ' this.Fly = async function () {',
  32493. ' var Result = 0;',
  32494. ' Result = 3;',
  32495. ' Result = 4 + Result;',
  32496. ' if (Result === 5) ;',
  32497. ' return 6;',
  32498. ' return Result;',
  32499. ' };',
  32500. '});',
  32501. 'this.Run = async function () {',
  32502. ' var Result = 0;',
  32503. ' Result = 11 + Result;',
  32504. ' Result += 1;',
  32505. ' return Result;',
  32506. '};',
  32507. 'this.p = null;',
  32508. 'this.o = null;',
  32509. '']),
  32510. LinesToStr([
  32511. '$mod.p = $mod.Run();',
  32512. '$mod.p = $mod.Run();',
  32513. 'if ($mod.Run() === $mod.p) ;',
  32514. 'if ($mod.p === $mod.Run()) ;',
  32515. 'if ($mod.Run() === $mod.p) ;',
  32516. 'if ($mod.p === $mod.Run()) ;',
  32517. '$mod.p = $mod.o.Fly();',
  32518. '$mod.p = $mod.o.Fly();',
  32519. 'if ($mod.o.Fly() === $mod.p) ;',
  32520. 'if ($mod.o.Fly() === $mod.p) ;',
  32521. 'var $with = $mod.o;',
  32522. '$mod.p = $with.Fly();',
  32523. '$mod.p = $with.Fly();',
  32524. 'if ($with.Fly() === $mod.p) ;',
  32525. 'if ($with.Fly() === $mod.p) ;',
  32526. '']));
  32527. end;
  32528. procedure TTestModule.TestAsync_ConstructorFail;
  32529. begin
  32530. StartProgram(false);
  32531. Add([
  32532. 'type',
  32533. ' TObject = class',
  32534. ' end;',
  32535. ' TBird = class',
  32536. ' constructor Create; async;',
  32537. ' end;',
  32538. 'constructor TBird.Create; async;',
  32539. 'begin',
  32540. 'end;',
  32541. 'begin',
  32542. '']);
  32543. SetExpectedPasResolverError('Invalid constructor modifier async',nInvalidXModifierY);
  32544. ConvertProgram;
  32545. end;
  32546. procedure TTestModule.TestAsync_PropertyGetterFail;
  32547. begin
  32548. StartProgram(false);
  32549. Add([
  32550. 'type',
  32551. ' TObject = class',
  32552. ' end;',
  32553. ' TBird = class',
  32554. ' function GetSize: word; async;',
  32555. ' property Size: word read GetSize;',
  32556. ' end;',
  32557. 'function TBird.GetSize: word; async;',
  32558. 'begin',
  32559. 'end;',
  32560. 'begin',
  32561. '']);
  32562. SetExpectedPasResolverError('Invalid property getter modifier async',nInvalidXModifierY);
  32563. ConvertProgram;
  32564. end;
  32565. procedure TTestModule.TestAwait_NonPromiseWithTypeFail;
  32566. begin
  32567. StartProgram(false);
  32568. Add([
  32569. 'procedure Run; async;',
  32570. 'begin',
  32571. ' await(word,1);',
  32572. 'end;',
  32573. 'begin',
  32574. '']);
  32575. SetExpectedPasResolverError('Incompatible type arg no. 2: Got "Longint", expected "TJSPromise"',nIncompatibleTypeArgNo);
  32576. ConvertProgram;
  32577. end;
  32578. procedure TTestModule.TestAwait_AsyncCallTypeMismatch;
  32579. begin
  32580. StartProgram(false);
  32581. Add([
  32582. 'type',
  32583. ' TObject = class',
  32584. ' end;',
  32585. ' TBird = class',
  32586. ' end;',
  32587. 'function Fly: TObject; async;',
  32588. 'begin',
  32589. 'end;',
  32590. 'procedure Run; async;',
  32591. 'begin',
  32592. ' await(TBird,Fly);',
  32593. 'end;',
  32594. 'begin',
  32595. '']);
  32596. SetExpectedPasResolverError('Incompatible type arg no. 2: Got "TObject", expected "TBird"',nIncompatibleTypeArgNo);
  32597. ConvertProgram;
  32598. end;
  32599. procedure TTestModule.TestAWait_OutsideAsyncFail;
  32600. begin
  32601. StartProgram(false);
  32602. Add([
  32603. 'procedure Crawl(w: double); ',
  32604. 'begin',
  32605. 'end;',
  32606. 'procedure Run(w: double);',
  32607. 'begin',
  32608. ' await(Crawl(w));',
  32609. 'end;',
  32610. 'begin',
  32611. ' Run(1);']);
  32612. SetExpectedPasResolverError(sAWaitOnlyInAsyncProcedure,nAWaitOnlyInAsyncProcedure);
  32613. ConvertProgram;
  32614. end;
  32615. procedure TTestModule.TestAWait_IntegerFail;
  32616. begin
  32617. StartProgram(false);
  32618. Add([
  32619. 'function Run: word;',
  32620. 'begin',
  32621. 'end;',
  32622. 'procedure Fly(w: word); async;',
  32623. 'begin',
  32624. ' await(Run());',
  32625. 'end;',
  32626. 'begin',
  32627. ' Fly(1);']);
  32628. SetExpectedPasResolverError('async function expected, but Result:Word found',nXExpectedButYFound);
  32629. ConvertProgram;
  32630. end;
  32631. procedure TTestModule.TestAWait_ExternalClassPromise;
  32632. begin
  32633. StartProgram(false);
  32634. Add([
  32635. '{$modeswitch externalclass}',
  32636. 'type',
  32637. ' TJSPromise = class external name ''Promise''',
  32638. ' end;',
  32639. ' TJSThenable = class external name ''Thenable''',
  32640. ' end;',
  32641. 'function Fly(w: word): TJSPromise;',
  32642. 'begin',
  32643. 'end;',
  32644. 'function Jump(w: word): word; async;',
  32645. 'begin',
  32646. 'end;',
  32647. 'function Eat(w: word): TJSPromise; async;',
  32648. 'begin',
  32649. 'end;',
  32650. 'function Run(d: double): word; async;',
  32651. 'var',
  32652. ' p: TJSPromise;',
  32653. 'begin',
  32654. ' Result:=await(word,p);', // promise needs type
  32655. ' Result:=await(word,Fly(3));', // promise needs type
  32656. ' Result:=await(Jump(4));', // async non promise must omit the type
  32657. ' Result:=await(word,Jump(5));', // async call can provide fitting type
  32658. ' Result:=await(word,Eat(6));', // promise needs type
  32659. 'end;',
  32660. 'begin',
  32661. '']);
  32662. ConvertProgram;
  32663. CheckSource('TestAWait_ExternalClassPromise',
  32664. LinesToStr([ // statements
  32665. 'this.Fly = function (w) {',
  32666. ' var Result = null;',
  32667. ' return Result;',
  32668. '};',
  32669. 'this.Jump = async function (w) {',
  32670. ' var Result = 0;',
  32671. ' return Result;',
  32672. '};',
  32673. 'this.Eat = async function (w) {',
  32674. ' var Result = null;',
  32675. ' return Result;',
  32676. '};',
  32677. 'this.Run = async function (d) {',
  32678. ' var Result = 0;',
  32679. ' var p = null;',
  32680. ' Result = await p;',
  32681. ' Result = await $mod.Fly(3);',
  32682. ' Result = await $mod.Jump(4);',
  32683. ' Result = await $mod.Jump(5);',
  32684. ' Result = await $mod.Eat(6);',
  32685. ' return Result;',
  32686. '};',
  32687. '']),
  32688. LinesToStr([
  32689. ]));
  32690. CheckResolverUnexpectedHints();
  32691. end;
  32692. procedure TTestModule.TestAWait_JSValue;
  32693. begin
  32694. StartProgram(false);
  32695. Add([
  32696. '{$modeswitch externalclass}',
  32697. 'type',
  32698. ' TJSPromise = class external name ''Promise''',
  32699. ' end;',
  32700. 'function Fly(w: word): jsvalue; async;',
  32701. 'begin',
  32702. 'end;',
  32703. 'function Run(d: jsvalue; var e): word; async;',
  32704. 'begin',
  32705. ' Result:=await(word,d);', // promise needs type
  32706. ' d:=await(Fly(4));', // async non promise must omit the type
  32707. ' Result:=await(word,e);', // promise needs type
  32708. 'end;',
  32709. 'begin',
  32710. '']);
  32711. ConvertProgram;
  32712. CheckSource('TestAWait_JSValue',
  32713. LinesToStr([ // statements
  32714. 'this.Fly = async function (w) {',
  32715. ' var Result = undefined;',
  32716. ' return Result;',
  32717. '};',
  32718. 'this.Run = async function (d, e) {',
  32719. ' var Result = 0;',
  32720. ' Result = await d;',
  32721. ' d = await $mod.Fly(4);',
  32722. ' Result = await e.get();',
  32723. ' return Result;',
  32724. '};',
  32725. '']),
  32726. LinesToStr([
  32727. ]));
  32728. CheckResolverUnexpectedHints();
  32729. end;
  32730. procedure TTestModule.TestAWait_Result;
  32731. begin
  32732. StartProgram(false);
  32733. Add([
  32734. '{$modeswitch externalclass}',
  32735. 'type',
  32736. ' TJSPromise = class external name ''Promise''',
  32737. ' end;',
  32738. 'function Crawl(d: double = 1.3): TJSPromise; ',
  32739. 'begin',
  32740. 'end;',
  32741. 'function Run(d: double = 1.6): word; async;',
  32742. 'begin',
  32743. ' Result:=await(word,Crawl);',
  32744. ' Result:=await(word,Crawl(4.5));',
  32745. ' Result:=await(Run);',
  32746. ' Result:=await(Run(6.7));',
  32747. 'end;',
  32748. 'begin',
  32749. ' Run(1);']);
  32750. ConvertProgram;
  32751. CheckSource('TestAWait_Result',
  32752. LinesToStr([ // statements
  32753. 'this.Crawl = function (d) {',
  32754. ' var Result = null;',
  32755. ' return Result;',
  32756. '};',
  32757. 'this.Run = async function (d) {',
  32758. ' var Result = 0;',
  32759. ' Result = await $mod.Crawl(1.3);',
  32760. ' Result = await $mod.Crawl(4.5);',
  32761. ' Result = await $mod.Run(1.6);',
  32762. ' Result = await $mod.Run(6.7);',
  32763. ' return Result;',
  32764. '};',
  32765. '']),
  32766. LinesToStr([
  32767. '$mod.Run(1);'
  32768. ]));
  32769. CheckResolverUnexpectedHints();
  32770. end;
  32771. procedure TTestModule.TestAWait_ResultPromiseMissingTypeFail;
  32772. begin
  32773. StartProgram(false);
  32774. Add([
  32775. '{$mode objfpc}',
  32776. '{$modeswitch externalclass}',
  32777. 'type',
  32778. ' TJSPromise = class external name ''Promise''',
  32779. ' end;',
  32780. 'function Run: TJSPromise; async;',
  32781. 'begin',
  32782. 'end;',
  32783. 'procedure Fly(w: word); async;',
  32784. 'begin',
  32785. ' await(Run());',
  32786. 'end;',
  32787. 'begin',
  32788. ' Fly(1);']);
  32789. SetExpectedPasResolverError('Wrong number of parameters specified for call to "function await(aType,TJSPromise):aType"',
  32790. nWrongNumberOfParametersForCallTo);
  32791. ConvertProgram;
  32792. end;
  32793. procedure TTestModule.TestAsync_AnonymousProc;
  32794. begin
  32795. StartProgram(false);
  32796. Add([
  32797. '{$mode objfpc}',
  32798. '{$modeswitch externalclass}',
  32799. 'type',
  32800. ' TJSPromise = class external name ''Promise''',
  32801. ' end;',
  32802. 'type',
  32803. ' TFunc = reference to function(x: double): word; async;',
  32804. 'function Crawl(d: double = 1.3): word; async;',
  32805. 'begin',
  32806. 'end;',
  32807. 'var Func: TFunc;',
  32808. 'begin',
  32809. ' Func:=function(c:double):word async begin',
  32810. ' Result:=await(Crawl(c));',
  32811. ' end;',
  32812. ' Func:=function(c:double):word async assembler asm',
  32813. ' end;',
  32814. ' ']);
  32815. ConvertProgram;
  32816. CheckSource('TestAsync_AnonymousProc',
  32817. LinesToStr([ // statements
  32818. 'this.Crawl = async function (d) {',
  32819. ' var Result = 0;',
  32820. ' return Result;',
  32821. '};',
  32822. 'this.Func = null;',
  32823. '']),
  32824. LinesToStr([
  32825. '$mod.Func = async function (c) {',
  32826. ' var Result = 0;',
  32827. ' Result = await $mod.Crawl(c);',
  32828. ' return Result;',
  32829. '};',
  32830. '$mod.Func = async function (c) {',
  32831. '};',
  32832. '']));
  32833. CheckResolverUnexpectedHints();
  32834. end;
  32835. procedure TTestModule.TestAsync_ProcType;
  32836. begin
  32837. StartProgram(false);
  32838. Add([
  32839. '{$mode objfpc}',
  32840. 'type',
  32841. ' TRefFunc = reference to function(x: double = 1.3): word; async;',
  32842. ' TFunc = function(x: double = 1.1): word; async;',
  32843. ' TProc = procedure(x: longint = 7); async;',
  32844. 'function Crawl(d: double): word; async;',
  32845. 'begin',
  32846. 'end;',
  32847. 'procedure Run(e:longint); async;',
  32848. 'begin',
  32849. 'end;',
  32850. 'procedure Fly(p: TProc); async;',
  32851. 'begin',
  32852. ' await(p);',
  32853. ' await(p());',
  32854. 'end;',
  32855. 'var',
  32856. ' RefFunc: TRefFunc;',
  32857. ' Func: TFunc;',
  32858. ' Proc, ProcB: TProc;',
  32859. 'begin',
  32860. ' Func:=@Crawl;',
  32861. ' RefFunc:=@Crawl;',
  32862. ' RefFunc:=function(c:double):word async begin',
  32863. ' Result:=await(RefFunc);',
  32864. ' Result:=await(RefFunc());',
  32865. ' Result:=await(Func);',
  32866. ' Result:=await(Func());',
  32867. ' await(Proc);',
  32868. ' await(Proc());',
  32869. ' await(Proc(13));',
  32870. ' end;',
  32871. ' Proc:=@Run;',
  32872. ' if Proc=ProcB then ;',
  32873. ' ']);
  32874. ConvertProgram;
  32875. CheckResolverUnexpectedHints();
  32876. CheckSource('TestAsync_ProcType',
  32877. LinesToStr([ // statements
  32878. 'this.Crawl = async function (d) {',
  32879. ' var Result = 0;',
  32880. ' return Result;',
  32881. '};',
  32882. 'this.Run = async function (e) {',
  32883. '};',
  32884. 'this.Fly = async function (p) {',
  32885. ' await p(7);',
  32886. ' await p(7);',
  32887. '};',
  32888. 'this.RefFunc = null;',
  32889. 'this.Func = null;',
  32890. 'this.Proc = null;',
  32891. 'this.ProcB = null;',
  32892. '']),
  32893. LinesToStr([
  32894. '$mod.Func = $mod.Crawl;',
  32895. '$mod.RefFunc = $mod.Crawl;',
  32896. '$mod.RefFunc = async function (c) {',
  32897. ' var Result = 0;',
  32898. ' Result = await $mod.RefFunc(1.3);',
  32899. ' Result = await $mod.RefFunc(1.3);',
  32900. ' Result = await $mod.Func(1.1);',
  32901. ' Result = await $mod.Func(1.1);',
  32902. ' await $mod.Proc(7);',
  32903. ' await $mod.Proc(7);',
  32904. ' await $mod.Proc(13);',
  32905. ' return Result;',
  32906. '};',
  32907. '$mod.Proc = $mod.Run;',
  32908. 'if (rtl.eqCallback($mod.Proc, $mod.ProcB)) ;',
  32909. '']));
  32910. end;
  32911. procedure TTestModule.TestAsync_ProcTypeAsyncModMismatchFail;
  32912. begin
  32913. StartProgram(false);
  32914. Add([
  32915. '{$mode objfpc}',
  32916. 'type',
  32917. ' TRefFunc = reference to function(x: double = 1.3): word;',
  32918. 'function Crawl(d: double): word; async;',
  32919. 'begin',
  32920. 'end;',
  32921. 'var',
  32922. ' RefFunc: TRefFunc;',
  32923. 'begin',
  32924. ' RefFunc:=@Crawl;',
  32925. ' ']);
  32926. SetExpectedPasResolverError('procedure type modifier "async" mismatch',nXModifierMismatchY);
  32927. ConvertProgram;
  32928. end;
  32929. procedure TTestModule.TestAsync_Inherited;
  32930. begin
  32931. StartProgram(false);
  32932. Add([
  32933. '{$mode objfpc}',
  32934. '{$modeswitch externalclass}',
  32935. 'type',
  32936. ' TJSPromise = class external name ''Promise''',
  32937. ' end;',
  32938. ' TObject = class',
  32939. ' function Run(w: word = 3): word; async; virtual;',
  32940. ' end;',
  32941. ' TBird = class',
  32942. ' function Run(w: word = 3): word; async; override;',
  32943. ' end;',
  32944. 'function TObject.Run(w: word = 3): word; async;',
  32945. 'begin',
  32946. 'end;',
  32947. 'function TBird.Run(w: word = 3): word;', // async modifier not needed in impl
  32948. 'var p: TJSPromise;',
  32949. 'begin',
  32950. ' p:=inherited;',
  32951. ' p:=inherited Run;',
  32952. ' p:=inherited Run();',
  32953. ' p:=inherited Run(4);',
  32954. ' exit(p);',
  32955. ' exit(inherited);',
  32956. ' exit(inherited Run);',
  32957. ' exit(inherited Run(5));',
  32958. ' exit(6);',
  32959. 'end;',
  32960. 'begin',
  32961. ' ']);
  32962. ConvertProgram;
  32963. CheckSource('TestAsync_Inherited',
  32964. LinesToStr([ // statements
  32965. 'rtl.createClass(this, "TObject", null, function () {',
  32966. ' this.$init = function () {',
  32967. ' };',
  32968. ' this.$final = function () {',
  32969. ' };',
  32970. ' this.Run = async function (w) {',
  32971. ' var Result = 0;',
  32972. ' return Result;',
  32973. ' };',
  32974. '});',
  32975. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  32976. ' this.Run = async function (w) {',
  32977. ' var Result = 0;',
  32978. ' var p = null;',
  32979. ' p = $mod.TObject.Run.apply(this, arguments);',
  32980. ' p = $mod.TObject.Run.call(this, 3);',
  32981. ' p = $mod.TObject.Run.call(this, 3);',
  32982. ' p = $mod.TObject.Run.call(this, 4);',
  32983. ' return p;',
  32984. ' return $mod.TObject.Run.apply(this, arguments);',
  32985. ' return $mod.TObject.Run.call(this, 3);',
  32986. ' return $mod.TObject.Run.call(this, 5);',
  32987. ' return 6;',
  32988. ' return Result;',
  32989. ' };',
  32990. '});',
  32991. '']),
  32992. LinesToStr([
  32993. '']));
  32994. CheckResolverUnexpectedHints();
  32995. end;
  32996. procedure TTestModule.TestAsync_ClassInterface;
  32997. begin
  32998. StartProgram(false);
  32999. Add([
  33000. '{$mode objfpc}',
  33001. '{$modeswitch externalclass}',
  33002. 'type',
  33003. ' TJSPromise = class external name ''Promise''',
  33004. ' end;',
  33005. ' IUnknown = interface',
  33006. ' function _AddRef: longint;',
  33007. ' function _Release: longint;',
  33008. ' end;',
  33009. 'function Say(i: IUnknown): IUnknown; async;',
  33010. 'begin',
  33011. 'end;',
  33012. 'function Run: IUnknown; async;',
  33013. 'begin',
  33014. ' Result:=await(Run);',
  33015. ' Result:=await(Run());',
  33016. ' Result:=await(Run) as IUnknown;',
  33017. ' Result:=await(Say(nil));',
  33018. ' Result:=await(Say(await(Run())));',
  33019. ' Result:=await(Say(await(Run()) as IUnknown));',
  33020. ' Result:=await(Say(await(Run()) as IUnknown)) as IUnknown;',
  33021. 'end;',
  33022. 'procedure Fly;',
  33023. 'var p: TJSPromise;',
  33024. 'begin',
  33025. ' Run;',
  33026. ' Run();',
  33027. ' p:=Run;',
  33028. ' p:=Run();',
  33029. 'end;',
  33030. 'begin',
  33031. ' ']);
  33032. ConvertProgram;
  33033. CheckSource('TestAsync_ClassInterface',
  33034. LinesToStr([ // statements
  33035. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  33036. 'this.Say = async function (i) {',
  33037. ' var Result = null;',
  33038. ' return Result;',
  33039. '};',
  33040. 'this.Run = async function () {',
  33041. ' var Result = null;',
  33042. ' var $ok = false;',
  33043. ' try {',
  33044. ' Result = rtl.setIntfL(Result, await $mod.Run());',
  33045. ' Result = rtl.setIntfL(Result, await $mod.Run());',
  33046. ' Result = rtl.setIntfL(Result, rtl.intfAsIntfT(await $mod.Run(), $mod.IUnknown));',
  33047. ' Result = rtl.setIntfL(Result, await $mod.Say(null));',
  33048. ' Result = rtl.setIntfL(Result, await $mod.Say(await $mod.Run()));',
  33049. ' Result = rtl.setIntfL(Result, await $mod.Say(rtl.intfAsIntfT(await $mod.Run(), $mod.IUnknown)));',
  33050. ' Result = rtl.setIntfL(Result, rtl.intfAsIntfT(await $mod.Say(rtl.intfAsIntfT(await $mod.Run(), $mod.IUnknown)), $mod.IUnknown));',
  33051. ' $ok = true;',
  33052. ' } finally {',
  33053. ' if (!$ok) rtl._Release(Result);',
  33054. ' };',
  33055. ' return Result;',
  33056. '};',
  33057. 'this.Fly = function () {',
  33058. ' var p = null;',
  33059. ' $mod.Run();',
  33060. ' $mod.Run();',
  33061. ' p = $mod.Run();',
  33062. ' p = $mod.Run();',
  33063. '};',
  33064. '']),
  33065. LinesToStr([
  33066. '']));
  33067. CheckResolverUnexpectedHints();
  33068. end;
  33069. procedure TTestModule.TestAsync_ClassInterface_AsyncMissmatchFail;
  33070. begin
  33071. StartProgram(true,[supTInterfacedObject]);
  33072. Add([
  33073. '{$mode objfpc}',
  33074. '{$modeswitch externalclass}',
  33075. 'type',
  33076. ' TJSPromise = class external name ''Promise''',
  33077. ' end;',
  33078. ' IBird = interface',
  33079. ' procedure Run;',
  33080. ' end;',
  33081. ' TBird = class(TInterfacedObject,IBird)',
  33082. ' procedure Run; async;',
  33083. ' end;',
  33084. 'procedure TBird.Run;',
  33085. 'begin',
  33086. 'end;',
  33087. 'begin',
  33088. ' ']);
  33089. SetExpectedPasResolverError('procedure type modifier "async" mismatch',nXModifierMismatchY);
  33090. ConvertProgram;
  33091. end;
  33092. procedure TTestModule.TestLibrary_Empty;
  33093. begin
  33094. StartLibrary(false);
  33095. Add([
  33096. '']);
  33097. ConvertLibrary;
  33098. CheckSource('TestLibrary_Empty',
  33099. LinesToStr([ // statements
  33100. '']),
  33101. LinesToStr([
  33102. '']));
  33103. CheckResolverUnexpectedHints();
  33104. end;
  33105. procedure TTestModule.TestLibrary_ExportFunc;
  33106. begin
  33107. StartLibrary(false);
  33108. Add([
  33109. 'procedure Run(w: word);',
  33110. 'begin',
  33111. 'end;',
  33112. 'exports',
  33113. ' Run;',
  33114. ' run name ''Foo'';',
  33115. ' test1.run name ''Test1Run'';',
  33116. '']);
  33117. ConvertLibrary;
  33118. CheckSource('TestLibrary_ExportFunc',
  33119. LinesToStr([ // statements
  33120. 'this.Run = function (w) {',
  33121. '};',
  33122. 'export { this.Run as Run, this.Run as Foo, this.Run as Test1Run };',
  33123. '']),
  33124. LinesToStr([
  33125. '']));
  33126. CheckResolverUnexpectedHints();
  33127. end;
  33128. procedure TTestModule.TestLibrary_Export_Index_Fail;
  33129. begin
  33130. StartLibrary(false);
  33131. Add([
  33132. 'procedure Run(w: word);',
  33133. 'begin',
  33134. 'end;',
  33135. 'exports',
  33136. ' Run index 3;',
  33137. '']);
  33138. SetExpectedPasResolverError('Not supported: export index',nNotSupportedX);
  33139. ConvertLibrary;
  33140. end;
  33141. procedure TTestModule.TestLibrary_ExportVar;
  33142. begin
  33143. StartLibrary(false);
  33144. Add([
  33145. 'var Wing: word;',
  33146. 'exports',
  33147. ' Wing;',
  33148. '']);
  33149. ConvertLibrary;
  33150. CheckSource('TestLibrary_ExportVar',
  33151. LinesToStr([ // statements
  33152. 'this.Wing = 0;',
  33153. 'export { this.Wing as Wing };',
  33154. '']),
  33155. LinesToStr([
  33156. '']));
  33157. CheckResolverUnexpectedHints();
  33158. end;
  33159. procedure TTestModule.TestLibrary_ExportUnitFunc;
  33160. begin
  33161. end;
  33162. Initialization
  33163. RegisterTests([TTestModule]);
  33164. end.