tcmodules.pas 867 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766247672476824769247702477124772247732477424775247762477724778247792478024781247822478324784247852478624787247882478924790247912479224793247942479524796247972479824799248002480124802248032480424805248062480724808248092481024811248122481324814248152481624817248182481924820248212482224823248242482524826248272482824829248302483124832248332483424835248362483724838248392484024841248422484324844248452484624847248482484924850248512485224853248542485524856248572485824859248602486124862248632486424865248662486724868248692487024871248722487324874248752487624877248782487924880248812488224883248842488524886248872488824889248902489124892248932489424895248962489724898248992490024901249022490324904249052490624907249082490924910249112491224913249142491524916249172491824919249202492124922249232492424925249262492724928249292493024931249322493324934249352493624937249382493924940249412494224943249442494524946249472494824949249502495124952249532495424955249562495724958249592496024961249622496324964249652496624967249682496924970249712497224973249742497524976249772497824979249802498124982249832498424985249862498724988249892499024991249922499324994249952499624997249982499925000250012500225003250042500525006250072500825009250102501125012250132501425015250162501725018250192502025021250222502325024250252502625027250282502925030250312503225033250342503525036250372503825039250402504125042250432504425045250462504725048250492505025051250522505325054250552505625057250582505925060250612506225063250642506525066250672506825069250702507125072250732507425075250762507725078250792508025081250822508325084250852508625087250882508925090250912509225093250942509525096250972509825099251002510125102251032510425105251062510725108251092511025111251122511325114251152511625117251182511925120251212512225123251242512525126251272512825129251302513125132251332513425135251362513725138251392514025141251422514325144251452514625147251482514925150251512515225153251542515525156251572515825159251602516125162251632516425165251662516725168251692517025171251722517325174251752517625177251782517925180251812518225183251842518525186251872518825189251902519125192251932519425195251962519725198251992520025201252022520325204252052520625207252082520925210252112521225213252142521525216252172521825219252202522125222252232522425225252262522725228252292523025231252322523325234252352523625237252382523925240252412524225243252442524525246252472524825249252502525125252252532525425255252562525725258252592526025261252622526325264252652526625267252682526925270252712527225273252742527525276252772527825279252802528125282252832528425285252862528725288252892529025291252922529325294252952529625297252982529925300253012530225303253042530525306253072530825309253102531125312253132531425315253162531725318253192532025321253222532325324253252532625327253282532925330253312533225333253342533525336253372533825339253402534125342253432534425345253462534725348253492535025351253522535325354253552535625357253582535925360253612536225363253642536525366253672536825369253702537125372253732537425375253762537725378253792538025381253822538325384253852538625387253882538925390253912539225393253942539525396253972539825399254002540125402254032540425405254062540725408254092541025411254122541325414254152541625417254182541925420254212542225423254242542525426254272542825429254302543125432254332543425435254362543725438254392544025441254422544325444254452544625447254482544925450254512545225453254542545525456254572545825459254602546125462254632546425465254662546725468254692547025471254722547325474254752547625477254782547925480254812548225483254842548525486254872548825489254902549125492254932549425495254962549725498254992550025501255022550325504255052550625507255082550925510255112551225513255142551525516255172551825519255202552125522255232552425525255262552725528255292553025531255322553325534255352553625537255382553925540255412554225543255442554525546255472554825549255502555125552255532555425555255562555725558255592556025561255622556325564255652556625567255682556925570255712557225573255742557525576255772557825579255802558125582255832558425585255862558725588255892559025591255922559325594255952559625597255982559925600256012560225603256042560525606256072560825609256102561125612256132561425615256162561725618256192562025621256222562325624256252562625627256282562925630256312563225633256342563525636256372563825639256402564125642256432564425645256462564725648256492565025651256522565325654256552565625657256582565925660256612566225663256642566525666256672566825669256702567125672256732567425675256762567725678256792568025681256822568325684256852568625687256882568925690256912569225693256942569525696256972569825699257002570125702257032570425705257062570725708257092571025711257122571325714257152571625717257182571925720257212572225723257242572525726257272572825729257302573125732257332573425735257362573725738257392574025741257422574325744257452574625747257482574925750257512575225753257542575525756257572575825759257602576125762257632576425765257662576725768257692577025771257722577325774257752577625777257782577925780257812578225783257842578525786257872578825789257902579125792257932579425795257962579725798257992580025801258022580325804258052580625807258082580925810258112581225813258142581525816258172581825819258202582125822258232582425825258262582725828258292583025831258322583325834258352583625837258382583925840258412584225843258442584525846258472584825849258502585125852258532585425855258562585725858258592586025861258622586325864258652586625867258682586925870258712587225873258742587525876258772587825879258802588125882258832588425885258862588725888258892589025891258922589325894258952589625897258982589925900259012590225903259042590525906259072590825909259102591125912259132591425915259162591725918259192592025921259222592325924259252592625927259282592925930259312593225933259342593525936259372593825939259402594125942259432594425945259462594725948259492595025951259522595325954259552595625957259582595925960259612596225963259642596525966259672596825969259702597125972259732597425975259762597725978259792598025981259822598325984259852598625987259882598925990259912599225993259942599525996259972599825999260002600126002260032600426005260062600726008260092601026011260122601326014260152601626017260182601926020260212602226023260242602526026260272602826029260302603126032260332603426035260362603726038260392604026041260422604326044260452604626047260482604926050260512605226053260542605526056260572605826059260602606126062260632606426065260662606726068260692607026071260722607326074260752607626077260782607926080260812608226083260842608526086260872608826089260902609126092260932609426095260962609726098260992610026101261022610326104261052610626107261082610926110261112611226113261142611526116261172611826119261202612126122261232612426125261262612726128261292613026131261322613326134261352613626137261382613926140261412614226143261442614526146261472614826149261502615126152261532615426155261562615726158261592616026161261622616326164261652616626167261682616926170261712617226173261742617526176261772617826179261802618126182261832618426185261862618726188261892619026191261922619326194261952619626197261982619926200262012620226203262042620526206262072620826209262102621126212262132621426215262162621726218262192622026221262222622326224262252622626227262282622926230262312623226233262342623526236262372623826239262402624126242262432624426245262462624726248262492625026251262522625326254262552625626257262582625926260262612626226263262642626526266262672626826269262702627126272262732627426275262762627726278262792628026281262822628326284262852628626287262882628926290262912629226293262942629526296262972629826299263002630126302263032630426305263062630726308263092631026311263122631326314263152631626317263182631926320263212632226323263242632526326263272632826329263302633126332263332633426335263362633726338263392634026341263422634326344263452634626347263482634926350263512635226353263542635526356263572635826359263602636126362263632636426365263662636726368263692637026371263722637326374263752637626377263782637926380263812638226383263842638526386263872638826389263902639126392263932639426395263962639726398263992640026401264022640326404264052640626407264082640926410264112641226413264142641526416264172641826419264202642126422264232642426425264262642726428264292643026431264322643326434264352643626437264382643926440264412644226443264442644526446264472644826449264502645126452264532645426455264562645726458264592646026461264622646326464264652646626467264682646926470264712647226473264742647526476264772647826479264802648126482264832648426485264862648726488264892649026491264922649326494264952649626497264982649926500265012650226503265042650526506265072650826509265102651126512265132651426515265162651726518265192652026521265222652326524265252652626527265282652926530265312653226533265342653526536265372653826539265402654126542265432654426545265462654726548265492655026551265522655326554265552655626557265582655926560265612656226563265642656526566265672656826569265702657126572265732657426575265762657726578265792658026581265822658326584265852658626587265882658926590265912659226593265942659526596265972659826599266002660126602266032660426605266062660726608266092661026611266122661326614266152661626617266182661926620266212662226623266242662526626266272662826629266302663126632266332663426635266362663726638266392664026641266422664326644266452664626647266482664926650266512665226653266542665526656266572665826659266602666126662266632666426665266662666726668266692667026671266722667326674266752667626677266782667926680266812668226683266842668526686266872668826689266902669126692266932669426695266962669726698266992670026701267022670326704267052670626707267082670926710267112671226713267142671526716267172671826719267202672126722267232672426725267262672726728267292673026731267322673326734267352673626737267382673926740267412674226743267442674526746267472674826749267502675126752267532675426755267562675726758267592676026761267622676326764267652676626767267682676926770267712677226773267742677526776267772677826779267802678126782267832678426785267862678726788267892679026791267922679326794267952679626797267982679926800268012680226803268042680526806268072680826809268102681126812268132681426815268162681726818268192682026821268222682326824268252682626827268282682926830268312683226833268342683526836268372683826839268402684126842268432684426845268462684726848268492685026851268522685326854268552685626857268582685926860268612686226863268642686526866268672686826869268702687126872268732687426875268762687726878268792688026881268822688326884268852688626887268882688926890268912689226893268942689526896268972689826899269002690126902269032690426905269062690726908269092691026911269122691326914269152691626917269182691926920269212692226923269242692526926269272692826929269302693126932269332693426935269362693726938269392694026941269422694326944269452694626947269482694926950269512695226953269542695526956269572695826959269602696126962269632696426965269662696726968269692697026971269722697326974269752697626977269782697926980269812698226983269842698526986269872698826989269902699126992269932699426995269962699726998269992700027001270022700327004270052700627007270082700927010270112701227013270142701527016270172701827019270202702127022270232702427025270262702727028270292703027031270322703327034270352703627037270382703927040270412704227043270442704527046270472704827049270502705127052270532705427055270562705727058270592706027061270622706327064270652706627067270682706927070270712707227073270742707527076270772707827079270802708127082270832708427085270862708727088270892709027091270922709327094270952709627097270982709927100271012710227103271042710527106271072710827109271102711127112271132711427115271162711727118271192712027121271222712327124271252712627127271282712927130271312713227133271342713527136271372713827139271402714127142271432714427145271462714727148271492715027151271522715327154271552715627157271582715927160271612716227163271642716527166271672716827169271702717127172271732717427175271762717727178271792718027181271822718327184271852718627187271882718927190271912719227193271942719527196271972719827199272002720127202272032720427205272062720727208272092721027211272122721327214272152721627217272182721927220272212722227223272242722527226272272722827229272302723127232272332723427235272362723727238272392724027241272422724327244272452724627247272482724927250272512725227253272542725527256272572725827259272602726127262272632726427265272662726727268272692727027271272722727327274272752727627277272782727927280272812728227283272842728527286272872728827289272902729127292272932729427295272962729727298272992730027301273022730327304273052730627307273082730927310273112731227313273142731527316273172731827319273202732127322273232732427325273262732727328273292733027331273322733327334273352733627337273382733927340273412734227343273442734527346273472734827349273502735127352273532735427355273562735727358273592736027361273622736327364273652736627367273682736927370273712737227373273742737527376273772737827379273802738127382273832738427385273862738727388273892739027391273922739327394273952739627397273982739927400274012740227403274042740527406274072740827409274102741127412274132741427415274162741727418274192742027421274222742327424274252742627427274282742927430274312743227433274342743527436274372743827439274402744127442274432744427445274462744727448274492745027451274522745327454274552745627457274582745927460274612746227463274642746527466274672746827469274702747127472274732747427475274762747727478274792748027481274822748327484274852748627487274882748927490274912749227493274942749527496274972749827499275002750127502275032750427505275062750727508275092751027511275122751327514275152751627517275182751927520275212752227523275242752527526275272752827529275302753127532275332753427535275362753727538275392754027541275422754327544275452754627547275482754927550275512755227553275542755527556275572755827559275602756127562275632756427565275662756727568275692757027571275722757327574275752757627577275782757927580275812758227583275842758527586275872758827589275902759127592275932759427595275962759727598275992760027601276022760327604276052760627607276082760927610276112761227613276142761527616276172761827619276202762127622276232762427625276262762727628276292763027631276322763327634276352763627637276382763927640276412764227643276442764527646276472764827649276502765127652276532765427655276562765727658276592766027661276622766327664276652766627667276682766927670276712767227673276742767527676276772767827679276802768127682276832768427685276862768727688276892769027691276922769327694276952769627697276982769927700277012770227703277042770527706277072770827709277102771127712277132771427715277162771727718277192772027721277222772327724277252772627727277282772927730277312773227733277342773527736277372773827739277402774127742277432774427745277462774727748277492775027751277522775327754277552775627757277582775927760277612776227763277642776527766277672776827769277702777127772277732777427775277762777727778277792778027781277822778327784277852778627787277882778927790277912779227793277942779527796277972779827799278002780127802278032780427805278062780727808278092781027811278122781327814278152781627817278182781927820278212782227823278242782527826278272782827829278302783127832278332783427835278362783727838278392784027841278422784327844278452784627847278482784927850278512785227853278542785527856278572785827859278602786127862278632786427865278662786727868278692787027871278722787327874278752787627877278782787927880278812788227883278842788527886278872788827889278902789127892278932789427895278962789727898278992790027901279022790327904279052790627907279082790927910279112791227913279142791527916279172791827919279202792127922279232792427925279262792727928279292793027931279322793327934279352793627937279382793927940279412794227943279442794527946279472794827949279502795127952279532795427955279562795727958279592796027961279622796327964279652796627967279682796927970279712797227973279742797527976279772797827979279802798127982279832798427985279862798727988279892799027991279922799327994279952799627997279982799928000280012800228003280042800528006280072800828009280102801128012280132801428015280162801728018280192802028021280222802328024280252802628027280282802928030280312803228033280342803528036280372803828039280402804128042280432804428045280462804728048280492805028051280522805328054280552805628057280582805928060280612806228063280642806528066280672806828069280702807128072280732807428075280762807728078280792808028081280822808328084280852808628087280882808928090280912809228093280942809528096280972809828099281002810128102281032810428105281062810728108281092811028111281122811328114281152811628117281182811928120281212812228123281242812528126281272812828129281302813128132281332813428135281362813728138281392814028141281422814328144281452814628147281482814928150281512815228153281542815528156281572815828159281602816128162281632816428165281662816728168281692817028171281722817328174281752817628177281782817928180281812818228183281842818528186281872818828189281902819128192281932819428195281962819728198281992820028201282022820328204282052820628207282082820928210282112821228213282142821528216282172821828219282202822128222282232822428225282262822728228282292823028231282322823328234282352823628237282382823928240282412824228243282442824528246282472824828249282502825128252282532825428255282562825728258282592826028261282622826328264282652826628267282682826928270282712827228273282742827528276282772827828279282802828128282282832828428285282862828728288282892829028291282922829328294282952829628297282982829928300283012830228303283042830528306283072830828309283102831128312283132831428315283162831728318283192832028321283222832328324283252832628327283282832928330283312833228333283342833528336283372833828339283402834128342283432834428345283462834728348283492835028351283522835328354283552835628357283582835928360283612836228363283642836528366283672836828369283702837128372283732837428375283762837728378283792838028381283822838328384283852838628387283882838928390283912839228393283942839528396283972839828399284002840128402284032840428405284062840728408284092841028411284122841328414284152841628417284182841928420284212842228423284242842528426284272842828429284302843128432284332843428435284362843728438284392844028441284422844328444284452844628447284482844928450284512845228453284542845528456284572845828459284602846128462284632846428465284662846728468284692847028471284722847328474284752847628477284782847928480284812848228483284842848528486284872848828489284902849128492284932849428495284962849728498284992850028501285022850328504285052850628507285082850928510285112851228513285142851528516285172851828519285202852128522285232852428525285262852728528285292853028531285322853328534285352853628537285382853928540285412854228543285442854528546285472854828549285502855128552285532855428555285562855728558285592856028561285622856328564285652856628567285682856928570285712857228573285742857528576285772857828579285802858128582285832858428585285862858728588285892859028591285922859328594285952859628597285982859928600286012860228603286042860528606286072860828609286102861128612286132861428615286162861728618286192862028621286222862328624286252862628627286282862928630286312863228633286342863528636286372863828639286402864128642286432864428645286462864728648286492865028651286522865328654286552865628657286582865928660286612866228663286642866528666286672866828669286702867128672286732867428675286762867728678286792868028681286822868328684286852868628687286882868928690286912869228693286942869528696286972869828699287002870128702287032870428705287062870728708287092871028711287122871328714287152871628717287182871928720287212872228723287242872528726287272872828729287302873128732287332873428735287362873728738287392874028741287422874328744287452874628747287482874928750287512875228753287542875528756287572875828759287602876128762287632876428765287662876728768287692877028771287722877328774287752877628777287782877928780287812878228783287842878528786287872878828789287902879128792287932879428795287962879728798287992880028801288022880328804288052880628807288082880928810288112881228813288142881528816288172881828819288202882128822288232882428825288262882728828288292883028831288322883328834288352883628837288382883928840288412884228843288442884528846288472884828849288502885128852288532885428855288562885728858288592886028861288622886328864288652886628867288682886928870288712887228873288742887528876288772887828879288802888128882288832888428885288862888728888288892889028891288922889328894288952889628897288982889928900289012890228903289042890528906289072890828909289102891128912289132891428915289162891728918289192892028921289222892328924289252892628927289282892928930289312893228933289342893528936289372893828939289402894128942289432894428945289462894728948289492895028951289522895328954289552895628957289582895928960289612896228963289642896528966289672896828969289702897128972289732897428975289762897728978289792898028981289822898328984289852898628987289882898928990289912899228993289942899528996289972899828999290002900129002290032900429005290062900729008290092901029011290122901329014290152901629017290182901929020290212902229023290242902529026290272902829029290302903129032290332903429035290362903729038290392904029041290422904329044290452904629047290482904929050290512905229053290542905529056290572905829059290602906129062290632906429065290662906729068290692907029071290722907329074290752907629077290782907929080290812908229083290842908529086290872908829089290902909129092290932909429095290962909729098290992910029101291022910329104291052910629107291082910929110291112911229113291142911529116291172911829119291202912129122291232912429125291262912729128291292913029131291322913329134291352913629137291382913929140291412914229143291442914529146291472914829149291502915129152291532915429155291562915729158291592916029161291622916329164291652916629167291682916929170291712917229173291742917529176291772917829179291802918129182291832918429185291862918729188291892919029191291922919329194291952919629197291982919929200292012920229203292042920529206292072920829209292102921129212292132921429215292162921729218292192922029221292222922329224292252922629227292282922929230292312923229233292342923529236292372923829239292402924129242292432924429245292462924729248292492925029251292522925329254292552925629257292582925929260292612926229263292642926529266292672926829269292702927129272292732927429275292762927729278292792928029281292822928329284292852928629287292882928929290292912929229293292942929529296292972929829299293002930129302293032930429305293062930729308293092931029311293122931329314293152931629317293182931929320293212932229323293242932529326293272932829329293302933129332293332933429335293362933729338293392934029341293422934329344293452934629347293482934929350293512935229353293542935529356293572935829359293602936129362293632936429365293662936729368293692937029371293722937329374293752937629377293782937929380293812938229383293842938529386293872938829389293902939129392293932939429395293962939729398293992940029401294022940329404294052940629407294082940929410294112941229413294142941529416294172941829419294202942129422294232942429425294262942729428294292943029431294322943329434294352943629437294382943929440294412944229443294442944529446294472944829449294502945129452294532945429455294562945729458294592946029461294622946329464294652946629467294682946929470294712947229473294742947529476294772947829479294802948129482294832948429485294862948729488294892949029491294922949329494294952949629497294982949929500295012950229503295042950529506295072950829509295102951129512295132951429515295162951729518295192952029521295222952329524295252952629527295282952929530295312953229533295342953529536295372953829539295402954129542295432954429545295462954729548295492955029551295522955329554295552955629557295582955929560295612956229563295642956529566295672956829569295702957129572295732957429575295762957729578295792958029581295822958329584295852958629587295882958929590295912959229593295942959529596295972959829599296002960129602296032960429605296062960729608296092961029611296122961329614296152961629617296182961929620296212962229623296242962529626296272962829629296302963129632296332963429635296362963729638296392964029641296422964329644296452964629647296482964929650296512965229653296542965529656296572965829659296602966129662296632966429665296662966729668296692967029671296722967329674296752967629677296782967929680296812968229683296842968529686296872968829689296902969129692296932969429695296962969729698296992970029701297022970329704297052970629707297082970929710297112971229713297142971529716297172971829719297202972129722297232972429725297262972729728297292973029731297322973329734297352973629737297382973929740297412974229743297442974529746297472974829749297502975129752297532975429755297562975729758297592976029761297622976329764297652976629767297682976929770297712977229773297742977529776297772977829779297802978129782297832978429785297862978729788297892979029791297922979329794297952979629797297982979929800298012980229803298042980529806298072980829809298102981129812298132981429815298162981729818298192982029821298222982329824298252982629827298282982929830298312983229833298342983529836298372983829839298402984129842298432984429845298462984729848298492985029851298522985329854298552985629857298582985929860298612986229863298642986529866298672986829869298702987129872298732987429875298762987729878298792988029881298822988329884298852988629887298882988929890298912989229893298942989529896298972989829899299002990129902299032990429905299062990729908299092991029911299122991329914299152991629917299182991929920299212992229923299242992529926299272992829929299302993129932299332993429935299362993729938299392994029941299422994329944299452994629947299482994929950299512995229953299542995529956299572995829959299602996129962299632996429965299662996729968299692997029971299722997329974299752997629977299782997929980299812998229983299842998529986299872998829989299902999129992299932999429995299962999729998299993000030001300023000330004300053000630007300083000930010300113001230013300143001530016300173001830019300203002130022300233002430025300263002730028300293003030031300323003330034300353003630037300383003930040300413004230043300443004530046300473004830049300503005130052300533005430055300563005730058300593006030061300623006330064300653006630067300683006930070300713007230073300743007530076300773007830079300803008130082300833008430085300863008730088300893009030091300923009330094300953009630097300983009930100301013010230103301043010530106301073010830109301103011130112301133011430115301163011730118301193012030121301223012330124301253012630127301283012930130301313013230133301343013530136301373013830139301403014130142301433014430145301463014730148301493015030151301523015330154301553015630157301583015930160301613016230163301643016530166301673016830169301703017130172301733017430175301763017730178301793018030181301823018330184301853018630187301883018930190301913019230193301943019530196301973019830199302003020130202302033020430205302063020730208302093021030211302123021330214302153021630217302183021930220302213022230223302243022530226302273022830229302303023130232302333023430235302363023730238302393024030241302423024330244302453024630247302483024930250302513025230253302543025530256302573025830259302603026130262302633026430265302663026730268302693027030271302723027330274302753027630277302783027930280302813028230283302843028530286302873028830289302903029130292302933029430295302963029730298302993030030301303023030330304303053030630307303083030930310303113031230313303143031530316303173031830319303203032130322303233032430325303263032730328303293033030331303323033330334303353033630337303383033930340303413034230343303443034530346303473034830349303503035130352303533035430355303563035730358303593036030361303623036330364303653036630367303683036930370303713037230373303743037530376303773037830379303803038130382303833038430385303863038730388303893039030391303923039330394303953039630397303983039930400304013040230403304043040530406304073040830409304103041130412304133041430415304163041730418304193042030421304223042330424304253042630427304283042930430304313043230433304343043530436304373043830439304403044130442304433044430445304463044730448304493045030451304523045330454304553045630457304583045930460304613046230463304643046530466304673046830469304703047130472304733047430475304763047730478304793048030481304823048330484304853048630487304883048930490304913049230493304943049530496304973049830499305003050130502305033050430505305063050730508305093051030511305123051330514305153051630517305183051930520305213052230523305243052530526305273052830529305303053130532305333053430535305363053730538305393054030541305423054330544305453054630547305483054930550305513055230553305543055530556305573055830559305603056130562305633056430565305663056730568305693057030571305723057330574305753057630577305783057930580305813058230583305843058530586305873058830589305903059130592305933059430595305963059730598305993060030601306023060330604306053060630607306083060930610306113061230613306143061530616306173061830619306203062130622306233062430625306263062730628306293063030631306323063330634306353063630637306383063930640306413064230643306443064530646306473064830649306503065130652306533065430655306563065730658306593066030661306623066330664306653066630667306683066930670306713067230673306743067530676306773067830679306803068130682306833068430685306863068730688306893069030691306923069330694306953069630697306983069930700307013070230703307043070530706307073070830709307103071130712307133071430715307163071730718307193072030721307223072330724307253072630727307283072930730307313073230733307343073530736307373073830739307403074130742307433074430745307463074730748307493075030751307523075330754307553075630757307583075930760307613076230763307643076530766307673076830769307703077130772307733077430775307763077730778307793078030781307823078330784307853078630787307883078930790307913079230793307943079530796307973079830799308003080130802308033080430805308063080730808308093081030811308123081330814308153081630817308183081930820308213082230823308243082530826308273082830829308303083130832308333083430835308363083730838308393084030841308423084330844308453084630847308483084930850308513085230853308543085530856308573085830859308603086130862308633086430865308663086730868308693087030871308723087330874308753087630877308783087930880308813088230883308843088530886308873088830889308903089130892308933089430895308963089730898308993090030901309023090330904309053090630907309083090930910309113091230913309143091530916309173091830919309203092130922309233092430925309263092730928309293093030931309323093330934309353093630937309383093930940309413094230943309443094530946309473094830949309503095130952309533095430955309563095730958309593096030961309623096330964309653096630967309683096930970309713097230973309743097530976309773097830979309803098130982309833098430985309863098730988309893099030991309923099330994309953099630997309983099931000310013100231003310043100531006310073100831009310103101131012310133101431015310163101731018310193102031021310223102331024310253102631027310283102931030310313103231033310343103531036310373103831039310403104131042310433104431045310463104731048310493105031051310523105331054310553105631057310583105931060310613106231063310643106531066310673106831069310703107131072310733107431075310763107731078310793108031081310823108331084310853108631087310883108931090310913109231093310943109531096310973109831099311003110131102311033110431105311063110731108311093111031111311123111331114311153111631117311183111931120311213112231123311243112531126311273112831129311303113131132311333113431135311363113731138311393114031141311423114331144311453114631147311483114931150311513115231153311543115531156311573115831159311603116131162311633116431165311663116731168311693117031171311723117331174311753117631177311783117931180311813118231183311843118531186311873118831189311903119131192311933119431195311963119731198311993120031201312023120331204312053120631207312083120931210312113121231213312143121531216312173121831219312203122131222312233122431225312263122731228312293123031231312323123331234312353123631237312383123931240312413124231243312443124531246312473124831249312503125131252312533125431255312563125731258312593126031261312623126331264312653126631267312683126931270312713127231273312743127531276312773127831279312803128131282312833128431285312863128731288312893129031291312923129331294312953129631297312983129931300313013130231303313043130531306313073130831309313103131131312313133131431315313163131731318313193132031321313223132331324313253132631327313283132931330313313133231333313343133531336313373133831339313403134131342313433134431345313463134731348313493135031351313523135331354313553135631357313583135931360313613136231363313643136531366313673136831369313703137131372313733137431375313763137731378313793138031381313823138331384313853138631387313883138931390313913139231393313943139531396313973139831399314003140131402314033140431405314063140731408314093141031411314123141331414314153141631417314183141931420314213142231423314243142531426314273142831429314303143131432314333143431435314363143731438314393144031441314423144331444314453144631447314483144931450314513145231453314543145531456314573145831459314603146131462314633146431465314663146731468314693147031471314723147331474314753147631477314783147931480314813148231483314843148531486314873148831489314903149131492314933149431495314963149731498314993150031501315023150331504315053150631507315083150931510315113151231513315143151531516315173151831519315203152131522315233152431525315263152731528315293153031531315323153331534315353153631537315383153931540315413154231543315443154531546315473154831549315503155131552315533155431555315563155731558315593156031561315623156331564315653156631567315683156931570315713157231573315743157531576315773157831579315803158131582315833158431585315863158731588315893159031591315923159331594315953159631597315983159931600316013160231603316043160531606316073160831609316103161131612316133161431615316163161731618316193162031621316223162331624316253162631627316283162931630316313163231633316343163531636316373163831639316403164131642316433164431645316463164731648316493165031651316523165331654316553165631657316583165931660316613166231663316643166531666316673166831669316703167131672316733167431675316763167731678316793168031681316823168331684316853168631687316883168931690316913169231693316943169531696316973169831699317003170131702317033170431705317063170731708317093171031711317123171331714317153171631717317183171931720317213172231723317243172531726317273172831729317303173131732317333173431735317363173731738317393174031741317423174331744317453174631747317483174931750317513175231753317543175531756317573175831759317603176131762317633176431765317663176731768317693177031771317723177331774317753177631777317783177931780317813178231783317843178531786317873178831789317903179131792317933179431795317963179731798317993180031801318023180331804318053180631807318083180931810318113181231813318143181531816318173181831819318203182131822318233182431825318263182731828318293183031831318323183331834318353183631837318383183931840318413184231843318443184531846318473184831849318503185131852318533185431855318563185731858318593186031861318623186331864318653186631867318683186931870318713187231873318743187531876318773187831879318803188131882318833188431885318863188731888318893189031891318923189331894318953189631897318983189931900319013190231903319043190531906319073190831909319103191131912319133191431915319163191731918319193192031921319223192331924319253192631927319283192931930319313193231933319343193531936319373193831939319403194131942319433194431945319463194731948319493195031951319523195331954319553195631957319583195931960319613196231963319643196531966319673196831969319703197131972319733197431975319763197731978319793198031981319823198331984319853198631987319883198931990319913199231993319943199531996319973199831999320003200132002320033200432005320063200732008320093201032011320123201332014320153201632017320183201932020320213202232023320243202532026320273202832029320303203132032320333203432035320363203732038320393204032041320423204332044320453204632047320483204932050320513205232053320543205532056320573205832059320603206132062320633206432065320663206732068320693207032071320723207332074320753207632077320783207932080320813208232083320843208532086320873208832089320903209132092320933209432095320963209732098320993210032101321023210332104321053210632107321083210932110321113211232113321143211532116321173211832119321203212132122321233212432125321263212732128321293213032131321323213332134321353213632137321383213932140321413214232143321443214532146321473214832149321503215132152321533215432155321563215732158321593216032161321623216332164321653216632167321683216932170321713217232173321743217532176321773217832179321803218132182321833218432185321863218732188321893219032191321923219332194321953219632197321983219932200322013220232203322043220532206322073220832209322103221132212322133221432215322163221732218322193222032221322223222332224322253222632227322283222932230322313223232233322343223532236322373223832239322403224132242322433224432245322463224732248322493225032251322523225332254322553225632257322583225932260322613226232263322643226532266322673226832269322703227132272322733227432275322763227732278322793228032281322823228332284322853228632287322883228932290322913229232293322943229532296322973229832299323003230132302323033230432305323063230732308323093231032311323123231332314323153231632317323183231932320323213232232323323243232532326323273232832329323303233132332323333233432335323363233732338323393234032341323423234332344323453234632347323483234932350323513235232353323543235532356323573235832359323603236132362323633236432365323663236732368323693237032371323723237332374323753237632377323783237932380323813238232383323843238532386323873238832389323903239132392323933239432395323963239732398323993240032401324023240332404324053240632407324083240932410324113241232413324143241532416324173241832419324203242132422324233242432425324263242732428324293243032431324323243332434324353243632437324383243932440324413244232443324443244532446324473244832449324503245132452324533245432455324563245732458324593246032461324623246332464324653246632467324683246932470324713247232473324743247532476324773247832479324803248132482324833248432485324863248732488324893249032491324923249332494324953249632497324983249932500325013250232503325043250532506325073250832509325103251132512325133251432515325163251732518325193252032521325223252332524325253252632527325283252932530325313253232533325343253532536325373253832539325403254132542325433254432545325463254732548325493255032551325523255332554325553255632557325583255932560325613256232563325643256532566325673256832569325703257132572325733257432575325763257732578325793258032581325823258332584325853258632587325883258932590325913259232593325943259532596325973259832599326003260132602326033260432605326063260732608326093261032611326123261332614326153261632617326183261932620326213262232623326243262532626326273262832629326303263132632326333263432635326363263732638326393264032641326423264332644326453264632647326483264932650326513265232653326543265532656326573265832659326603266132662326633266432665326663266732668326693267032671326723267332674326753267632677326783267932680326813268232683326843268532686326873268832689326903269132692326933269432695326963269732698326993270032701327023270332704327053270632707327083270932710327113271232713327143271532716327173271832719327203272132722327233272432725327263272732728327293273032731327323273332734327353273632737327383273932740327413274232743327443274532746327473274832749327503275132752327533275432755327563275732758327593276032761327623276332764327653276632767327683276932770327713277232773327743277532776327773277832779327803278132782327833278432785327863278732788327893279032791327923279332794327953279632797327983279932800328013280232803328043280532806328073280832809328103281132812328133281432815328163281732818328193282032821328223282332824328253282632827328283282932830328313283232833328343283532836328373283832839328403284132842328433284432845328463284732848328493285032851328523285332854328553285632857328583285932860328613286232863328643286532866328673286832869328703287132872328733287432875328763287732878328793288032881328823288332884328853288632887328883288932890328913289232893328943289532896328973289832899329003290132902329033290432905329063290732908329093291032911329123291332914329153291632917329183291932920329213292232923329243292532926329273292832929329303293132932329333293432935329363293732938329393294032941329423294332944329453294632947329483294932950329513295232953329543295532956329573295832959329603296132962329633296432965329663296732968329693297032971329723297332974329753297632977329783297932980329813298232983329843298532986329873298832989329903299132992329933299432995329963299732998329993300033001330023300333004330053300633007330083300933010330113301233013330143301533016330173301833019330203302133022330233302433025330263302733028330293303033031330323303333034
  1. {
  2. This file is part of the Free Component Library (FCL)
  3. Copyright (c) 2018 by Michael Van Canneyt
  4. Unit tests for Pascal-to-Javascript converter class.
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************
  11. Examples:
  12. ./testpas2js --suite=TTestModule.TestEmptyProgram
  13. ./testpas2js --suite=TTestModule.TestEmptyUnit
  14. }
  15. unit TCModules;
  16. {$mode objfpc}{$H+}
  17. interface
  18. uses
  19. Classes, SysUtils, fpcunit, testregistry, contnrs,
  20. jstree, jswriter, jsbase,
  21. PasTree, PScanner, PasResolver, PParser, PasResolveEval,
  22. FPPas2Js;
  23. const
  24. // default parser+scanner options
  25. po_tcmodules = po_Pas2js+[po_KeepScannerError];
  26. co_tcmodules = [];
  27. type
  28. TSrcMarkerKind = (
  29. mkLabel,
  30. mkResolverReference,
  31. mkDirectReference
  32. );
  33. PSrcMarker = ^TSrcMarker;
  34. TSrcMarker = record
  35. Kind: TSrcMarkerKind;
  36. Filename: string;
  37. Row: integer;
  38. StartCol, EndCol: integer; // token start, end column
  39. Identifier: string;
  40. Next: PSrcMarker;
  41. end;
  42. TSystemUnitPart = (
  43. supTObject,
  44. supTVarRec,
  45. supTypeInfo,
  46. supTInterfacedObject,
  47. supWriteln
  48. );
  49. TSystemUnitParts = set of TSystemUnitPart;
  50. { TTestHintMessage }
  51. TTestHintMessage = class
  52. public
  53. Id: int64;
  54. MsgType: TMessageType;
  55. MsgNumber: integer;
  56. Msg: string;
  57. SourcePos: TPasSourcePos;
  58. end;
  59. { TTestPasParser }
  60. TTestPasParser = Class(TPasParser)
  61. end;
  62. TOnFindUnit = function(const aUnitName: String): TPasModule of object;
  63. { TTestEnginePasResolver }
  64. TTestEnginePasResolver = class(TPas2JsResolver)
  65. private
  66. FFilename: string;
  67. FModule: TPasModule;
  68. FOnFindUnit: TOnFindUnit;
  69. FParser: TTestPasParser;
  70. FStreamResolver: TStreamResolver;
  71. FScanner: TPas2jsPasScanner;
  72. FSource: string;
  73. public
  74. destructor Destroy; override;
  75. function FindUnit(const AName, InFilename: String; NameExpr,
  76. InFileExpr: TPasExpr): TPasModule; override;
  77. procedure UsedInterfacesFinished(Section: TPasSection); override;
  78. property OnFindUnit: TOnFindUnit read FOnFindUnit write FOnFindUnit;
  79. property Filename: string read FFilename write FFilename;
  80. property StreamResolver: TStreamResolver read FStreamResolver write FStreamResolver;
  81. property Scanner: TPas2jsPasScanner read FScanner write FScanner;
  82. property Parser: TTestPasParser read FParser write FParser;
  83. property Source: string read FSource write FSource;
  84. property Module: TPasModule read FModule;
  85. end;
  86. { TCustomTestModule }
  87. TCustomTestModule = Class(TTestCase)
  88. private
  89. FConverter: TPasToJSConverter;
  90. FEngine: TTestEnginePasResolver;
  91. FExpectedErrorClass: ExceptClass;
  92. FExpectedErrorMsg: string;
  93. FExpectedErrorNumber: integer;
  94. FFilename: string;
  95. FFileResolver: TStreamResolver;
  96. FHub: TPas2JSResolverHub;
  97. FJSImplementationUses: TJSArrayLiteral;
  98. FJSInitBody: TJSFunctionBody;
  99. FJSImplentationUses: TJSArrayLiteral;
  100. FJSInterfaceUses: TJSArrayLiteral;
  101. FJSModule: TJSSourceElements;
  102. FJSModuleSrc: TJSSourceElements;
  103. FJSSource: TStringList;
  104. FModule: TPasModule;
  105. FJSModuleCallArgs: TJSArguments;
  106. FModules: TObjectList;// list of TTestEnginePasResolver
  107. FParser: TTestPasParser;
  108. FPasProgram: TPasProgram;
  109. FHintMsgs: TObjectList; // list of TTestHintMessage
  110. FHintMsgsGood: TFPList; // list of TTestHintMessage marked as expected
  111. FJSRegModuleCall: TJSCallExpression;
  112. FScanner: TPas2jsPasScanner;
  113. FSkipTests: boolean;
  114. FSource: TStringList;
  115. FFirstPasStatement: TPasImplBlock;
  116. FWithTypeInfo: boolean;
  117. {$IFDEF EnablePasTreeGlobalRefCount}
  118. FElementRefCountAtSetup: int64;
  119. {$ENDIF}
  120. function GetMsgCount: integer;
  121. function GetMsgs(Index: integer): TTestHintMessage;
  122. function GetResolverCount: integer;
  123. function GetResolvers(Index: integer): TTestEnginePasResolver;
  124. function OnPasResolverFindUnit(const aUnitName: String): TPasModule;
  125. procedure OnParserLog(Sender: TObject; const Msg: String);
  126. procedure OnPasResolverLog(Sender: TObject; const Msg: String);
  127. procedure OnScannerLog(Sender: TObject; const Msg: String);
  128. procedure SetWithTypeInfo(const AValue: boolean);
  129. protected
  130. procedure SetUp; override;
  131. function CreateConverter: TPasToJSConverter; virtual;
  132. function LoadUnit(const aUnitName: String): TPasModule;
  133. procedure InitScanner(aScanner: TPas2jsPasScanner); virtual;
  134. procedure TearDown; override;
  135. Procedure Add(Line: string); virtual;
  136. Procedure Add(const Lines: array of string);
  137. Procedure StartParsing; virtual;
  138. procedure ParseModuleQueue; virtual;
  139. procedure ParseModule; virtual;
  140. procedure ParseProgram; virtual;
  141. procedure ParseUnit; virtual;
  142. protected
  143. function FindModuleWithFilename(aFilename: string): TTestEnginePasResolver; virtual;
  144. function AddModule(aFilename: string): TTestEnginePasResolver; virtual;
  145. function AddModuleWithSrc(aFilename, Src: string): TTestEnginePasResolver; virtual;
  146. function AddModuleWithIntfImplSrc(aFilename, InterfaceSrc,
  147. ImplementationSrc: string): TTestEnginePasResolver; virtual;
  148. procedure AddSystemUnit(Parts: TSystemUnitParts = []); virtual;
  149. procedure StartProgram(NeedSystemUnit: boolean; SystemUnitParts: TSystemUnitParts = []); virtual;
  150. procedure StartUnit(NeedSystemUnit: boolean; SystemUnitParts: TSystemUnitParts = []); virtual;
  151. procedure ConvertModule; virtual;
  152. procedure ConvertProgram; virtual;
  153. procedure ConvertUnit; virtual;
  154. function ConvertJSModuleToString(El: TJSElement): string; virtual;
  155. procedure CheckDottedIdentifier(Msg: string; El: TJSElement; DottedName: string);
  156. function GetDottedIdentifier(El: TJSElement): string;
  157. procedure CheckSource(Msg,Statements: String; InitStatements: string = '';
  158. ImplStatements: string = ''); virtual;
  159. procedure CheckDiff(Msg, Expected, Actual: string); virtual;
  160. procedure CheckUnit(Filename, ExpectedSrc: string); virtual;
  161. procedure CheckHint(MsgType: TMessageType; MsgNumber: integer;
  162. Msg: string; Marker: PSrcMarker = nil); virtual;
  163. procedure CheckResolverUnexpectedHints(WithSourcePos: boolean = false); virtual;
  164. procedure SetExpectedScannerError(Msg: string; MsgNumber: integer);
  165. procedure SetExpectedParserError(Msg: string; MsgNumber: integer);
  166. procedure SetExpectedPasResolverError(Msg: string; MsgNumber: integer);
  167. procedure SetExpectedConverterError(Msg: string; MsgNumber: integer);
  168. function IsErrorExpected(E: Exception): boolean;
  169. procedure HandleScannerError(E: EScannerError);
  170. procedure HandleParserError(E: EParserError);
  171. procedure HandlePasResolveError(E: EPasResolve);
  172. procedure HandlePas2JSError(E: EPas2JS);
  173. procedure HandleException(E: Exception);
  174. procedure FailException(E: Exception);
  175. procedure WriteSources(const aFilename: string; aRow, aCol: integer);
  176. function IndexOfResolver(const Filename: string): integer;
  177. function GetResolver(const Filename: string): TTestEnginePasResolver;
  178. function GetDefaultNamespace: string;
  179. property PasProgram: TPasProgram Read FPasProgram;
  180. property Resolvers[Index: integer]: TTestEnginePasResolver read GetResolvers;
  181. property ResolverCount: integer read GetResolverCount;
  182. property Engine: TTestEnginePasResolver read FEngine;
  183. property Filename: string read FFilename;
  184. Property Module: TPasModule Read FModule;
  185. property FirstPasStatement: TPasImplBlock read FFirstPasStatement;
  186. property Converter: TPasToJSConverter read FConverter;
  187. property JSSource: TStringList read FJSSource;
  188. property JSModule: TJSSourceElements read FJSModule;
  189. property JSRegModuleCall: TJSCallExpression read FJSRegModuleCall;
  190. property JSModuleCallArgs: TJSArguments read FJSModuleCallArgs;
  191. property JSImplementationUses: TJSArrayLiteral read FJSImplementationUses;
  192. property JSInterfaceUses: TJSArrayLiteral read FJSInterfaceUses;
  193. property JSModuleSrc: TJSSourceElements read FJSModuleSrc;
  194. property JSInitBody: TJSFunctionBody read FJSInitBody;
  195. property ExpectedErrorClass: ExceptClass read FExpectedErrorClass write FExpectedErrorClass;
  196. property ExpectedErrorMsg: string read FExpectedErrorMsg write FExpectedErrorMsg;
  197. property ExpectedErrorNumber: integer read FExpectedErrorNumber write FExpectedErrorNumber;
  198. property SkipTests: boolean read FSkipTests write FSkipTests;
  199. public
  200. constructor Create; override;
  201. destructor Destroy; override;
  202. property Hub: TPas2JSResolverHub read FHub;
  203. property Source: TStringList read FSource;
  204. property FileResolver: TStreamResolver read FFileResolver;
  205. property Scanner: TPas2jsPasScanner read FScanner;
  206. property Parser: TTestPasParser read FParser;
  207. property MsgCount: integer read GetMsgCount;
  208. property Msgs[Index: integer]: TTestHintMessage read GetMsgs;
  209. property WithTypeInfo: boolean read FWithTypeInfo write SetWithTypeInfo;
  210. end;
  211. { TTestModule }
  212. TTestModule = class(TCustomTestModule)
  213. Published
  214. Procedure TestReservedWords;
  215. // program, units, includes
  216. Procedure TestEmptyProgram;
  217. Procedure TestEmptyProgramUseStrict;
  218. Procedure TestEmptyUnit;
  219. Procedure TestEmptyUnitUseStrict;
  220. Procedure TestDottedUnitNames;
  221. Procedure TestDottedUnitNameImpl;
  222. Procedure TestDottedUnitExpr;
  223. Procedure Test_ModeFPCFail;
  224. Procedure Test_ModeSwitchCBlocksFail;
  225. Procedure TestUnit_UseSystem;
  226. Procedure TestUnit_Intf1Impl2Intf1;
  227. Procedure TestIncludeVersion;
  228. // vars/const
  229. Procedure TestVarInt;
  230. Procedure TestVarBaseTypes;
  231. Procedure TestBaseTypeSingleFail;
  232. Procedure TestBaseTypeExtendedFail;
  233. Procedure TestConstBaseTypes;
  234. Procedure TestUnitImplVars;
  235. Procedure TestUnitImplConsts;
  236. Procedure TestUnitImplRecord;
  237. Procedure TestRenameJSNameConflict;
  238. Procedure TestLocalConst;
  239. Procedure TestVarExternal;
  240. Procedure TestVarExternalOtherUnit;
  241. Procedure TestVarAbsoluteFail;
  242. Procedure TestConstExternal;
  243. // numbers
  244. Procedure TestDouble;
  245. Procedure TestInteger;
  246. Procedure TestIntegerRange;
  247. Procedure TestIntegerTypecasts;
  248. Procedure TestInteger_BitwiseShrNativeInt;
  249. Procedure TestInteger_BitwiseShlNativeInt;
  250. Procedure TestInteger_SystemFunc;
  251. Procedure TestCurrency;
  252. Procedure TestForBoolDo;
  253. Procedure TestForIntDo;
  254. Procedure TestForIntInDo;
  255. // strings
  256. Procedure TestCharConst;
  257. Procedure TestChar_Compare;
  258. Procedure TestChar_BuiltInProcs;
  259. Procedure TestStringConst;
  260. Procedure TestStringConst_InvalidUTF16;
  261. Procedure TestStringConstSurrogate;
  262. Procedure TestString_Length;
  263. Procedure TestString_Compare;
  264. Procedure TestString_SetLength;
  265. Procedure TestString_CharAt;
  266. Procedure TestStringHMinusFail;
  267. Procedure TestStr;
  268. Procedure TestBaseType_AnsiStringFail;
  269. Procedure TestBaseType_WideStringFail;
  270. Procedure TestBaseType_ShortStringFail;
  271. Procedure TestBaseType_RawByteStringFail;
  272. Procedure TestTypeShortstring_Fail;
  273. Procedure TestCharSet_Custom;
  274. Procedure TestWideChar;
  275. Procedure TestForCharDo;
  276. Procedure TestForCharInDo;
  277. // alias types
  278. Procedure TestAliasTypeRef;
  279. Procedure TestTypeCast_BaseTypes;
  280. Procedure TestTypeCast_AliasBaseTypes;
  281. // functions
  282. Procedure TestEmptyProc;
  283. Procedure TestProcOneParam;
  284. Procedure TestFunctionWithoutParams;
  285. Procedure TestProcedureWithoutParams;
  286. Procedure TestPrgProcVar;
  287. Procedure TestProcTwoArgs;
  288. Procedure TestProc_DefaultValue;
  289. Procedure TestUnitProcVar;
  290. Procedure TestImplProc;
  291. Procedure TestFunctionResult;
  292. Procedure TestNestedProc;
  293. Procedure TestNestedProc_ResultString;
  294. Procedure TestForwardProc;
  295. Procedure TestNestedForwardProc;
  296. Procedure TestAssignFunctionResult;
  297. Procedure TestFunctionResultInCondition;
  298. Procedure TestFunctionResultInForLoop;
  299. Procedure TestFunctionResultInTypeCast;
  300. Procedure TestExit;
  301. Procedure TestExit_ResultInFinally;
  302. Procedure TestBreak;
  303. Procedure TestBreakAsVar;
  304. Procedure TestContinue;
  305. Procedure TestProc_External;
  306. Procedure TestProc_ExternalOtherUnit;
  307. Procedure TestProc_Asm;
  308. Procedure TestProc_Assembler;
  309. Procedure TestProc_VarParam;
  310. Procedure TestProc_VarParamString;
  311. Procedure TestProc_VarParamV;
  312. Procedure TestProc_Overload;
  313. Procedure TestProc_OverloadForward;
  314. Procedure TestProc_OverloadIntfImpl;
  315. Procedure TestProc_OverloadNested;
  316. Procedure TestProc_OverloadNestedForward;
  317. Procedure TestProc_OverloadUnitCycle;
  318. Procedure TestProc_Varargs;
  319. Procedure TestProc_ConstOrder;
  320. Procedure TestProc_DuplicateConst;
  321. Procedure TestProc_LocalVarAbsolute;
  322. Procedure TestProc_LocalVarInit;
  323. Procedure TestProc_ReservedWords;
  324. Procedure TestProc_ConstRefWord;
  325. // anonymous functions
  326. Procedure TestAnonymousProc_Assign_ObjFPC;
  327. Procedure TestAnonymousProc_Assign_Delphi;
  328. Procedure TestAnonymousProc_Arg;
  329. Procedure TestAnonymousProc_Typecast;
  330. Procedure TestAnonymousProc_With;
  331. Procedure TestAnonymousProc_ExceptOn;
  332. Procedure TestAnonymousProc_Nested;
  333. Procedure TestAnonymousProc_NestedAssignResult;
  334. Procedure TestAnonymousProc_Class;
  335. Procedure TestAnonymousProc_ForLoop;
  336. Procedure TestAnonymousProc_AsmDelphi;
  337. // enums, sets
  338. Procedure TestEnum_Name;
  339. Procedure TestEnum_Number;
  340. Procedure TestEnum_ConstFail;
  341. Procedure TestEnum_Functions;
  342. Procedure TestEnumRg_Functions;
  343. Procedure TestEnum_AsParams;
  344. Procedure TestEnumRange_Array;
  345. Procedure TestEnum_ForIn;
  346. Procedure TestEnum_ScopedNumber;
  347. Procedure TestEnum_InFunction;
  348. Procedure TestSet_Enum;
  349. Procedure TestSet_Operators;
  350. Procedure TestSet_Operator_In;
  351. Procedure TestSet_Functions;
  352. Procedure TestSet_PassAsArgClone;
  353. Procedure TestSet_AsParams;
  354. Procedure TestSet_Property;
  355. Procedure TestSet_EnumConst;
  356. Procedure TestSet_IntConst;
  357. Procedure TestSet_AnonymousEnumType;
  358. Procedure TestSet_AnonymousEnumTypeChar; // ToDo
  359. Procedure TestSet_ConstEnum;
  360. Procedure TestSet_ConstChar;
  361. Procedure TestSet_ConstInt;
  362. Procedure TestSet_InFunction;
  363. Procedure TestSet_ForIn;
  364. // statements
  365. Procedure TestNestBegin;
  366. Procedure TestIncDec;
  367. Procedure TestLoHiFpcMode;
  368. Procedure TestLoHiDelphiMode;
  369. Procedure TestAssignments;
  370. Procedure TestArithmeticOperators1;
  371. Procedure TestLogicalOperators;
  372. Procedure TestBitwiseOperators;
  373. Procedure TestBitwiseOperatorsLongword;
  374. Procedure TestFunctionInt;
  375. Procedure TestFunctionString;
  376. Procedure TestIfThen;
  377. Procedure TestForLoop;
  378. Procedure TestForLoopInsideFunction;
  379. Procedure TestForLoop_ReadVarAfter;
  380. Procedure TestForLoop_Nested;
  381. Procedure TestRepeatUntil;
  382. Procedure TestAsmBlock;
  383. Procedure TestAsmPas_Impl; // ToDo
  384. Procedure TestTryFinally;
  385. Procedure TestTryExcept;
  386. Procedure TestTryExcept_ReservedWords;
  387. Procedure TestIfThenRaiseElse;
  388. Procedure TestCaseOf;
  389. Procedure TestCaseOf_UseSwitch;
  390. Procedure TestCaseOfNoElse;
  391. Procedure TestCaseOfNoElse_UseSwitch;
  392. Procedure TestCaseOfRange;
  393. Procedure TestCaseOfString;
  394. Procedure TestCaseOfChar;
  395. Procedure TestCaseOfExternalClassConst;
  396. Procedure TestDebugger;
  397. // arrays
  398. Procedure TestArray_Dynamic;
  399. Procedure TestArray_Dynamic_Nil;
  400. Procedure TestArray_DynMultiDimensional;
  401. Procedure TestArray_DynamicAssign;
  402. Procedure TestArray_StaticInt;
  403. Procedure TestArray_StaticBool;
  404. Procedure TestArray_StaticChar;
  405. Procedure TestArray_StaticMultiDim;
  406. Procedure TestArray_StaticInFunction;
  407. Procedure TestArray_StaticMultiDimEqualNotImplemented;
  408. Procedure TestArrayOfRecord;
  409. Procedure TestArray_StaticRecord;
  410. Procedure TestArrayOfSet;
  411. Procedure TestArray_DynAsParam;
  412. Procedure TestArray_StaticAsParam;
  413. Procedure TestArrayElement_AsParams;
  414. Procedure TestArrayElementFromFuncResult_AsParams;
  415. Procedure TestArrayEnumTypeRange;
  416. Procedure TestArray_SetLengthOutArg;
  417. Procedure TestArray_SetLengthProperty;
  418. Procedure TestArray_SetLengthMultiDim;
  419. Procedure TestArray_SetLengthDynOfStatic;
  420. Procedure TestArray_OpenArrayOfString;
  421. Procedure TestArray_ArrayOfCharAssignString; // ToDo
  422. Procedure TestArray_ConstRef;
  423. Procedure TestArray_Concat;
  424. Procedure TestArray_Copy;
  425. Procedure TestArray_InsertDelete;
  426. Procedure TestArray_DynArrayConstObjFPC;
  427. Procedure TestArray_DynArrayConstDelphi;
  428. Procedure TestArray_ArrayLitAsParam;
  429. Procedure TestArray_ArrayLitMultiDimAsParam;
  430. Procedure TestArray_ArrayLitStaticAsParam;
  431. Procedure TestArray_ForInArrOfString;
  432. Procedure TestExternalClass_TypeCastArrayToExternalClass;
  433. Procedure TestExternalClass_TypeCastArrayFromExternalClass;
  434. Procedure TestArrayOfConst_TVarRec;
  435. Procedure TestArrayOfConst_PassBaseTypes;
  436. Procedure TestArrayOfConst_PassObj;
  437. // record
  438. Procedure TestRecord_Empty;
  439. Procedure TestRecord_Var;
  440. Procedure TestRecord_VarExternal;
  441. Procedure TestRecord_WithDo;
  442. Procedure TestRecord_Assign;
  443. Procedure TestRecord_AsParams;
  444. Procedure TestRecord_ConstRef;
  445. Procedure TestRecordElement_AsParams;
  446. Procedure TestRecordElementFromFuncResult_AsParams;
  447. Procedure TestRecordElementFromWith_AsParams;
  448. Procedure TestRecord_Equal;
  449. Procedure TestRecord_JSValue;
  450. Procedure TestRecord_VariantFail;
  451. Procedure TestRecord_FieldArray;
  452. Procedure TestRecord_Const;
  453. Procedure TestRecord_TypecastFail;
  454. Procedure TestRecord_InFunction;
  455. Procedure TestRecord_AnonymousFail;
  456. // advanced record
  457. Procedure TestAdvRecord_Function;
  458. Procedure TestAdvRecord_Property;
  459. Procedure TestAdvRecord_PropertyDefault;
  460. Procedure TestAdvRecord_Property_ClassMethod;
  461. Procedure TestAdvRecord_Const;
  462. Procedure TestAdvRecord_ExternalField;
  463. Procedure TestAdvRecord_SubRecord;
  464. Procedure TestAdvRecord_SubClass;
  465. Procedure TestAdvRecord_SubInterfaceFail;
  466. Procedure TestAdvRecord_Constructor;
  467. Procedure TestAdvRecord_ClassConstructor_Program;
  468. Procedure TestAdvRecord_ClassConstructor_Unit;
  469. // classes
  470. Procedure TestClass_TObjectDefaultConstructor;
  471. Procedure TestClass_TObjectConstructorWithParams;
  472. Procedure TestClass_TObjectConstructorWithDefaultParam;
  473. Procedure TestClass_Var;
  474. Procedure TestClass_Method;
  475. Procedure TestClass_Implementation;
  476. Procedure TestClass_Inheritance;
  477. Procedure TestClass_TypeAlias;
  478. Procedure TestClass_AbstractMethod;
  479. Procedure TestClass_CallInherited_ProcNoParams;
  480. Procedure TestClass_CallInherited_WithParams;
  481. Procedure TestClasS_CallInheritedConstructor;
  482. Procedure TestClass_ClassVar_Assign;
  483. Procedure TestClass_CallClassMethod;
  484. Procedure TestClass_Property;
  485. Procedure TestClass_Property_ClassMethod;
  486. Procedure TestClass_Property_Indexed;
  487. Procedure TestClass_Property_IndexSpec;
  488. Procedure TestClass_PropertyOfTypeArray;
  489. Procedure TestClass_PropertyDefault;
  490. Procedure TestClass_PropertyDefault_TypecastToOtherDefault;
  491. //Procedure TestClass_PropertyDefault;
  492. Procedure TestClass_PropertyOverride;
  493. Procedure TestClass_PropertyIncVisibility;
  494. Procedure TestClass_Assigned;
  495. Procedure TestClass_WithClassDoCreate;
  496. Procedure TestClass_WithClassInstDoProperty;
  497. Procedure TestClass_WithClassInstDoPropertyWithParams;
  498. Procedure TestClass_WithClassInstDoFunc;
  499. Procedure TestClass_TypeCast;
  500. Procedure TestClass_TypeCastUntypedParam;
  501. Procedure TestClass_Overloads;
  502. Procedure TestClass_OverloadsAncestor;
  503. Procedure TestClass_OverloadConstructor;
  504. Procedure TestClass_OverloadDelphiOverride;
  505. Procedure TestClass_ReintroduceVarDelphi;
  506. Procedure TestClass_ReintroducedVar;
  507. Procedure TestClass_RaiseDescendant;
  508. Procedure TestClass_ExternalMethod;
  509. Procedure TestClass_ExternalVirtualNameMismatchFail;
  510. Procedure TestClass_ExternalOverrideFail;
  511. Procedure TestClass_ExternalVar;
  512. Procedure TestClass_Const;
  513. Procedure TestClass_ConstEnum;
  514. Procedure TestClass_LocalConstDuplicate_Prg;
  515. Procedure TestClass_LocalConstDuplicate_Unit;
  516. // ToDo: Procedure TestAdvRecord_LocalConstDuplicate;
  517. Procedure TestClass_LocalVarSelfFail;
  518. Procedure TestClass_ArgSelfFail;
  519. Procedure TestClass_NestedProcSelf;
  520. Procedure TestClass_NestedProcSelf2;
  521. Procedure TestClass_NestedProcClassSelf;
  522. Procedure TestClass_NestedProcCallInherited;
  523. Procedure TestClass_TObjectFree;
  524. Procedure TestClass_TObjectFree_VarArg;
  525. Procedure TestClass_TObjectFreeNewInstance;
  526. Procedure TestClass_TObjectFreeLowerCase;
  527. Procedure TestClass_TObjectFreeFunctionFail;
  528. Procedure TestClass_TObjectFreePropertyFail;
  529. Procedure TestClass_ForIn;
  530. Procedure TestClass_DispatchMessage;
  531. Procedure TestClass_Message_DuplicateIntFail;
  532. Procedure TestClass_DispatchMessage_WrongFieldNameFail;
  533. // class of
  534. Procedure TestClassOf_Create;
  535. Procedure TestClassOf_Call;
  536. Procedure TestClassOf_Assign;
  537. Procedure TestClassOf_Is;
  538. Procedure TestClassOf_Compare;
  539. Procedure TestClassOf_ClassVar;
  540. Procedure TestClassOf_ClassMethod;
  541. Procedure TestClassOf_ClassProperty;
  542. Procedure TestClassOf_ClassMethodSelf;
  543. Procedure TestClassOf_TypeCast;
  544. Procedure TestClassOf_ImplicitFunctionCall;
  545. Procedure TestClassOf_Const;
  546. // nested class
  547. Procedure TestNestedClass_Alias;
  548. Procedure TestNestedClass_Record;
  549. Procedure TestNestedClass_Class;
  550. // external class
  551. Procedure TestExternalClass_Var;
  552. Procedure TestExternalClass_Const;
  553. Procedure TestExternalClass_Dollar;
  554. Procedure TestExternalClass_DuplicateVarFail;
  555. Procedure TestExternalClass_Method;
  556. Procedure TestExternalClass_ClassMethod;
  557. Procedure TestExternalClass_ClassMethodStatic;
  558. Procedure TestExternalClass_FunctionResultInTypeCast;
  559. Procedure TestExternalClass_NonExternalOverride;
  560. Procedure TestExternalClass_OverloadHint;
  561. Procedure TestExternalClass_SameNamePublishedProperty;
  562. Procedure TestExternalClass_Property;
  563. Procedure TestExternalClass_PropertyDate;
  564. Procedure TestExternalClass_ClassProperty;
  565. Procedure TestExternalClass_ClassOf;
  566. Procedure TestExternalClass_ClassOtherUnit;
  567. Procedure TestExternalClass_Is;
  568. Procedure TestExternalClass_As;
  569. Procedure TestExternalClass_DestructorFail;
  570. Procedure TestExternalClass_New;
  571. Procedure TestExternalClass_ClassOf_New;
  572. Procedure TestExternalClass_FuncClassOf_New;
  573. Procedure TestExternalClass_New_PasClassFail;
  574. Procedure TestExternalClass_New_PasClassBracketsFail;
  575. Procedure TestExternalClass_NewExtName;
  576. Procedure TestExternalClass_Constructor;
  577. Procedure TestExternalClass_ConstructorBrackets;
  578. Procedure TestExternalClass_LocalConstSameName;
  579. Procedure TestExternalClass_ReintroduceOverload;
  580. Procedure TestExternalClass_Inherited;
  581. Procedure TestExternalClass_PascalAncestorFail;
  582. Procedure TestExternalClass_NewInstance;
  583. Procedure TestExternalClass_NewInstance_NonVirtualFail;
  584. Procedure TestExternalClass_NewInstance_FirstParamNotString_Fail;
  585. Procedure TestExternalClass_NewInstance_SecondParamTyped_Fail;
  586. Procedure TestExternalClass_JSFunctionPasDescendant;
  587. Procedure TestExternalClass_PascalProperty;
  588. Procedure TestExternalClass_TypeCastToRootClass;
  589. Procedure TestExternalClass_TypeCastToJSObject;
  590. Procedure TestExternalClass_TypeCastStringToExternalString;
  591. Procedure TestExternalClass_TypeCastToJSFunction;
  592. Procedure TestExternalClass_TypeCastDelphiUnrelated;
  593. Procedure TestExternalClass_CallClassFunctionOfInstanceFail;
  594. Procedure TestExternalClass_BracketAccessor;
  595. Procedure TestExternalClass_BracketAccessor_Call;
  596. Procedure TestExternalClass_BracketAccessor_2ParamsFail;
  597. Procedure TestExternalClass_BracketAccessor_ReadOnly;
  598. Procedure TestExternalClass_BracketAccessor_WriteOnly;
  599. Procedure TestExternalClass_BracketAccessor_MultiType;
  600. Procedure TestExternalClass_BracketAccessor_Index;
  601. Procedure TestExternalClass_ForInJSObject;
  602. Procedure TestExternalClass_ForInJSArray;
  603. Procedure TestExternalClass_IncompatibleArgDuplicateIdentifier;
  604. // class interfaces
  605. Procedure TestClassInterface_Corba;
  606. Procedure TestClassInterface_ProcExternalFail;
  607. Procedure TestClassInterface_Overloads;
  608. Procedure TestClassInterface_DuplicateGUIInIntfListFail;
  609. Procedure TestClassInterface_DuplicateGUIInAncestorFail;
  610. Procedure TestClassInterface_AncestorImpl;
  611. Procedure TestClassInterface_ImplReintroduce;
  612. Procedure TestClassInterface_MethodResolution;
  613. Procedure TestClassInterface_AncestorMoreInterfaces;
  614. Procedure TestClassInterface_MethodOverride;
  615. Procedure TestClassInterface_Corba_Delegation;
  616. Procedure TestClassInterface_Corba_DelegationStatic;
  617. Procedure TestClassInterface_Corba_Operators;
  618. Procedure TestClassInterface_Corba_Args;
  619. Procedure TestClassInterface_Corba_ForIn;
  620. Procedure TestClassInterface_COM_AssignVar;
  621. Procedure TestClassInterface_COM_AssignArg;
  622. Procedure TestClassInterface_COM_FunctionResult;
  623. Procedure TestClassInterface_COM_InheritedFuncResult;
  624. Procedure TestClassInterface_COM_IsAsTypeCasts;
  625. Procedure TestClassInterface_COM_PassAsArg;
  626. Procedure TestClassInterface_COM_PassToUntypedParam;
  627. Procedure TestClassInterface_COM_FunctionInExpr;
  628. Procedure TestClassInterface_COM_Property;
  629. Procedure TestClassInterface_COM_IntfProperty;
  630. Procedure TestClassInterface_COM_Delegation;
  631. Procedure TestClassInterface_COM_With;
  632. Procedure TestClassInterface_COM_ForIn;
  633. Procedure TestClassInterface_COM_ArrayOfIntfFail;
  634. Procedure TestClassInterface_COM_RecordIntfFail;
  635. Procedure TestClassInterface_COM_UnitInitialization;
  636. Procedure TestClassInterface_GUID;
  637. Procedure TestClassInterface_GUIDProperty;
  638. // helpers
  639. Procedure TestClassHelper_ClassVar;
  640. Procedure TestClassHelper_Method_AccessInstanceFields;
  641. Procedure TestClassHelper_Method_Call;
  642. Procedure TestClassHelper_Method_Nested_Call;
  643. Procedure TestClassHelper_ClassMethod_Call;
  644. Procedure TestClassHelper_ClassOf;
  645. Procedure TestClassHelper_MethodRefObjFPC;
  646. Procedure TestClassHelper_Constructor;
  647. Procedure TestClassHelper_InheritedObjFPC;
  648. Procedure TestClassHelper_Property;
  649. Procedure TestClassHelper_Property_Array;
  650. Procedure TestClassHelper_Property_Array_Default;
  651. Procedure TestClassHelper_Property_Array_DefaultDefault;
  652. Procedure TestClassHelper_ClassProperty;
  653. Procedure TestClassHelper_ClassPropertyStatic;
  654. Procedure TestClassHelper_ClassProperty_Array;
  655. Procedure TestClassHelper_ForIn;
  656. Procedure TestClassHelper_PassProperty;
  657. Procedure TestExtClassHelper_ClassVar;
  658. Procedure TestExtClassHelper_Method_Call;
  659. Procedure TestExtClassHelper_ClassMethod_MissingStatic;
  660. Procedure TestRecordHelper_ClassVar;
  661. Procedure TestRecordHelper_Method_Call;
  662. Procedure TestRecordHelper_Constructor;
  663. Procedure TestTypeHelper_ClassVar;
  664. Procedure TestTypeHelper_PassResultElement;
  665. Procedure TestTypeHelper_PassArgs;
  666. Procedure TestTypeHelper_PassVarConst;
  667. Procedure TestTypeHelper_PassFuncResult;
  668. Procedure TestTypeHelper_PassPropertyField;
  669. Procedure TestTypeHelper_PassPropertyGetter;
  670. Procedure TestTypeHelper_PassClassPropertyField;
  671. Procedure TestTypeHelper_PassClassPropertyGetterStatic;
  672. Procedure TestTypeHelper_PassClassPropertyGetterNonStatic;
  673. Procedure TestTypeHelper_Property;
  674. Procedure TestTypeHelper_Property_Array;
  675. Procedure TestTypeHelper_ClassProperty;
  676. Procedure TestTypeHelper_ClassProperty_Array;
  677. Procedure TestTypeHelper_ClassMethod;
  678. Procedure TestTypeHelper_ExtClassMethodFail;
  679. Procedure TestTypeHelper_Constructor;
  680. Procedure TestTypeHelper_Word;
  681. Procedure TestTypeHelper_Boolean;
  682. Procedure TestTypeHelper_WordBool;
  683. Procedure TestTypeHelper_Double;
  684. Procedure TestTypeHelper_NativeInt;
  685. Procedure TestTypeHelper_StringChar;
  686. Procedure TestTypeHelper_JSValue;
  687. Procedure TestTypeHelper_Array;
  688. Procedure TestTypeHelper_EnumType;
  689. Procedure TestTypeHelper_SetType;
  690. Procedure TestTypeHelper_InterfaceType;
  691. Procedure TestTypeHelper_NestedSelf;
  692. // proc types
  693. Procedure TestProcType;
  694. Procedure TestProcType_Arg;
  695. Procedure TestProcType_FunctionFPC;
  696. Procedure TestProcType_FunctionDelphi;
  697. Procedure TestProcType_ProcedureDelphi;
  698. Procedure TestProcType_AsParam;
  699. Procedure TestProcType_MethodFPC;
  700. Procedure TestProcType_MethodDelphi;
  701. Procedure TestProcType_PropertyFPC;
  702. Procedure TestProcType_PropertyDelphi;
  703. Procedure TestProcType_WithClassInstDoPropertyFPC;
  704. Procedure TestProcType_Nested;
  705. Procedure TestProcType_NestedOfObject;
  706. Procedure TestProcType_ReferenceToProc;
  707. Procedure TestProcType_ReferenceToMethod;
  708. Procedure TestProcType_Typecast;
  709. Procedure TestProcType_PassProcToUntyped;
  710. Procedure TestProcType_PassProcToArray;
  711. Procedure TestProcType_SafeCallObjFPC;
  712. Procedure TestProcType_SafeCallDelphi;
  713. // pointer
  714. Procedure TestPointer;
  715. Procedure TestPointer_Proc;
  716. Procedure TestPointer_AssignRecordFail;
  717. Procedure TestPointer_AssignStaticArrayFail;
  718. Procedure TestPointer_TypeCastJSValueToPointer;
  719. Procedure TestPointer_NonRecordFail;
  720. Procedure TestPointer_AnonymousArgTypeFail;
  721. Procedure TestPointer_AnonymousVarTypeFail;
  722. Procedure TestPointer_AnonymousResultTypeFail;
  723. Procedure TestPointer_AddrOperatorFail;
  724. Procedure TestPointer_ArrayParamsFail;
  725. Procedure TestPointer_PointerAddFail;
  726. Procedure TestPointer_IncPointerFail;
  727. Procedure TestPointer_Record;
  728. Procedure TestPointer_RecordArg;
  729. // jsvalue
  730. Procedure TestJSValue_AssignToJSValue;
  731. Procedure TestJSValue_TypeCastToBaseType;
  732. Procedure TestJSValue_TypecastToJSValue;
  733. Procedure TestJSValue_Equal;
  734. Procedure TestJSValue_If;
  735. Procedure TestJSValue_Not;
  736. Procedure TestJSValue_Enum;
  737. Procedure TestJSValue_ClassInstance;
  738. Procedure TestJSValue_ClassOf;
  739. Procedure TestJSValue_ArrayOfJSValue;
  740. Procedure TestJSValue_ArrayLit;
  741. Procedure TestJSValue_Params;
  742. Procedure TestJSValue_UntypedParam;
  743. Procedure TestJSValue_FuncResultType;
  744. Procedure TestJSValue_ProcType_Assign;
  745. Procedure TestJSValue_ProcType_Equal;
  746. Procedure TestJSValue_ProcType_Param;
  747. Procedure TestJSValue_AssignToPointerFail;
  748. Procedure TestJSValue_OverloadDouble;
  749. Procedure TestJSValue_OverloadNativeInt;
  750. Procedure TestJSValue_OverloadWord;
  751. Procedure TestJSValue_OverloadString;
  752. Procedure TestJSValue_OverloadChar;
  753. Procedure TestJSValue_OverloadPointer;
  754. Procedure TestJSValue_ForIn;
  755. // RTTI
  756. Procedure TestRTTI_IntRange;
  757. Procedure TestRTTI_Double;
  758. Procedure TestRTTI_ProcType;
  759. Procedure TestRTTI_ProcType_ArgFromOtherUnit;
  760. Procedure TestRTTI_EnumAndSetType;
  761. Procedure TestRTTI_EnumRange;
  762. Procedure TestRTTI_AnonymousEnumType;
  763. Procedure TestRTTI_StaticArray;
  764. Procedure TestRTTI_DynArray;
  765. Procedure TestRTTI_ArrayNestedAnonymous;
  766. Procedure TestRTTI_PublishedMethodOverloadFail;
  767. Procedure TestRTTI_PublishedMethodExternalFail;
  768. Procedure TestRTTI_PublishedClassPropertyFail;
  769. Procedure TestRTTI_PublishedClassFieldFail;
  770. Procedure TestRTTI_PublishedFieldExternalFail;
  771. Procedure TestRTTI_Class_Field;
  772. Procedure TestRTTI_Class_Method;
  773. Procedure TestRTTI_Class_MethodArgFlags;
  774. Procedure TestRTTI_Class_Property;
  775. Procedure TestRTTI_Class_PropertyParams;
  776. Procedure TestRTTI_Class_OtherUnit_TypeAlias;
  777. Procedure TestRTTI_Class_OmitRTTI;
  778. Procedure TestRTTI_IndexModifier;
  779. Procedure TestRTTI_StoredModifier;
  780. Procedure TestRTTI_DefaultValue;
  781. Procedure TestRTTI_DefaultValueSet;
  782. Procedure TestRTTI_DefaultValueRangeType;
  783. Procedure TestRTTI_DefaultValueInherit;
  784. Procedure TestRTTI_OverrideMethod;
  785. Procedure TestRTTI_ReintroduceMethod;
  786. Procedure TestRTTI_OverloadProperty;
  787. // ToDo: array argument
  788. Procedure TestRTTI_ClassForward;
  789. Procedure TestRTTI_ClassOf;
  790. Procedure TestRTTI_Record;
  791. Procedure TestRTTI_RecordAnonymousArray;
  792. Procedure TestRTTI_Record_ClassVarType;
  793. Procedure TestRTTI_LocalTypes;
  794. Procedure TestRTTI_TypeInfo_BaseTypes;
  795. Procedure TestRTTI_TypeInfo_Type_BaseTypes;
  796. Procedure TestRTTI_TypeInfo_LocalFail;
  797. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses1;
  798. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses2;
  799. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses3;
  800. Procedure TestRTTI_TypeInfo_FunctionClassType;
  801. Procedure TestRTTI_TypeInfo_MixedUnits_PointerAndClass;
  802. Procedure TestRTTI_Interface_Corba;
  803. Procedure TestRTTI_Interface_COM;
  804. Procedure TestRTTI_ClassHelper;
  805. Procedure TestRTTI_ExternalClass;
  806. Procedure TestRTTI_Unit;
  807. // Resourcestring
  808. Procedure TestResourcestringProgram;
  809. Procedure TestResourcestringUnit;
  810. Procedure TestResourcestringImplementation;
  811. // Attributes
  812. Procedure TestAttributes_Members;
  813. Procedure TestAttributes_Types;
  814. Procedure TestAttributes_HelperConstructor_Fail;
  815. // Assertions, checks
  816. procedure TestAssert;
  817. procedure TestAssert_SysUtils;
  818. procedure TestObjectChecks;
  819. procedure TestOverflowChecks_Int;
  820. procedure TestRangeChecks_AssignInt;
  821. procedure TestRangeChecks_AssignIntRange;
  822. procedure TestRangeChecks_AssignEnum;
  823. procedure TestRangeChecks_AssignEnumRange;
  824. procedure TestRangeChecks_AssignChar;
  825. procedure TestRangeChecks_AssignCharRange;
  826. procedure TestRangeChecks_ArrayIndex;
  827. procedure TestRangeChecks_ArrayOfRecIndex;
  828. procedure TestRangeChecks_StringIndex;
  829. procedure TestRangeChecks_TypecastInt;
  830. procedure TestRangeChecks_TypeHelperInt;
  831. // Async/AWait
  832. Procedure TestAsync_Proc;
  833. Procedure TestAsync_CallResultIsPromise;
  834. Procedure TestAsync_ConstructorFail;
  835. Procedure TestAsync_PropertyGetterFail;
  836. Procedure TestAwait_NonPromiseWithTypeFail;
  837. Procedure TestAwait_AsyncCallTypeMismatch;
  838. Procedure TestAWait_OutsideAsyncFail;
  839. Procedure TestAWait_Result;
  840. Procedure TestAWait_ExternalClassPromise;
  841. Procedure TestAsync_AnonymousProc;
  842. Procedure TestAsync_ProcType;
  843. Procedure TestAsync_ProcTypeAsyncModMismatchFail;
  844. Procedure TestAsync_Inherited;
  845. Procedure TestAsync_ClassInterface;
  846. Procedure TestAsync_ClassInterface_AsyncMissmatchFail;
  847. end;
  848. function LinesToStr(Args: array of const): string;
  849. function ExtractFileUnitName(aFilename: string): string;
  850. function JSToStr(El: TJSElement): string;
  851. function CheckSrcDiff(Expected, Actual: string; out Msg: string): boolean;
  852. implementation
  853. function LinesToStr(Args: array of const): string;
  854. var
  855. s: String;
  856. i: Integer;
  857. begin
  858. s:='';
  859. for i:=Low(Args) to High(Args) do
  860. case Args[i].VType of
  861. vtChar: s += Args[i].VChar+LineEnding;
  862. vtString: s += Args[i].VString^+LineEnding;
  863. vtPChar: s += Args[i].VPChar+LineEnding;
  864. vtWideChar: s += AnsiString(Args[i].VWideChar)+LineEnding;
  865. vtPWideChar: s += AnsiString(Args[i].VPWideChar)+LineEnding;
  866. vtAnsiString: s += AnsiString(Args[i].VAnsiString)+LineEnding;
  867. vtWidestring: s += AnsiString(WideString(Args[i].VWideString))+LineEnding;
  868. vtUnicodeString:s += AnsiString(UnicodeString(Args[i].VUnicodeString))+LineEnding;
  869. end;
  870. Result:=s;
  871. end;
  872. function ExtractFileUnitName(aFilename: string): string;
  873. var
  874. p: Integer;
  875. begin
  876. Result:=ExtractFileName(aFilename);
  877. if Result='' then exit;
  878. for p:=length(Result) downto 1 do
  879. case Result[p] of
  880. '/','\': exit;
  881. '.':
  882. begin
  883. Delete(Result,p,length(Result));
  884. exit;
  885. end;
  886. end;
  887. end;
  888. function JSToStr(El: TJSElement): string;
  889. var
  890. aWriter: TBufferWriter;
  891. aJSWriter: TJSWriter;
  892. begin
  893. aJSWriter:=nil;
  894. aWriter:=TBufferWriter.Create(1000);
  895. try
  896. aJSWriter:=TJSWriter.Create(aWriter);
  897. aJSWriter.IndentSize:=2;
  898. aJSWriter.WriteJS(El);
  899. Result:=aWriter.AsString;
  900. finally
  901. aJSWriter.Free;
  902. aWriter.Free;
  903. end;
  904. end;
  905. function CheckSrcDiff(Expected, Actual: string; out Msg: string): boolean;
  906. // search diff, ignore changes in spaces
  907. const
  908. SpaceChars = [#9,#10,#13,' '];
  909. var
  910. ExpectedP, ActualP: PChar;
  911. function FindLineEnd(p: PChar): PChar;
  912. begin
  913. Result:=p;
  914. while not (Result^ in [#0,#10,#13]) do inc(Result);
  915. end;
  916. function FindLineStart(p, MinP: PChar): PChar;
  917. begin
  918. while (p>MinP) and not (p[-1] in [#10,#13]) do dec(p);
  919. Result:=p;
  920. end;
  921. procedure SkipLineEnd(var p: PChar);
  922. begin
  923. if p^ in [#10,#13] then
  924. begin
  925. if (p[1] in [#10,#13]) and (p^<>p[1]) then
  926. inc(p,2)
  927. else
  928. inc(p);
  929. end;
  930. end;
  931. function HasSpecialChar(s: string): boolean;
  932. var
  933. i: Integer;
  934. begin
  935. for i:=1 to length(s) do
  936. if s[i] in [#0..#31,#127..#255] then
  937. exit(true);
  938. Result:=false;
  939. end;
  940. function HashSpecialChars(s: string): string;
  941. var
  942. i: Integer;
  943. begin
  944. Result:='';
  945. for i:=1 to length(s) do
  946. if s[i] in [#0..#31,#127..#255] then
  947. Result:=Result+'#'+hexstr(ord(s[i]),2)
  948. else
  949. Result:=Result+s[i];
  950. end;
  951. procedure DiffFound;
  952. var
  953. ActLineStartP, ActLineEndP, p, StartPos: PChar;
  954. ExpLine, ActLine: String;
  955. i, LineNo, DiffLineNo: Integer;
  956. begin
  957. writeln('Diff found "',Msg,'". Lines:');
  958. // write correct lines
  959. p:=PChar(Expected);
  960. LineNo:=0;
  961. DiffLineNo:=0;
  962. repeat
  963. StartPos:=p;
  964. while not (p^ in [#0,#10,#13]) do inc(p);
  965. ExpLine:=copy(Expected,StartPos-PChar(Expected)+1,p-StartPos);
  966. SkipLineEnd(p);
  967. inc(LineNo);
  968. if (p<=ExpectedP) and (p^<>#0) then
  969. begin
  970. writeln('= ',ExpLine);
  971. end else begin
  972. // diff line
  973. if DiffLineNo=0 then DiffLineNo:=LineNo;
  974. // write actual line
  975. ActLineStartP:=FindLineStart(ActualP,PChar(Actual));
  976. ActLineEndP:=FindLineEnd(ActualP);
  977. ActLine:=copy(Actual,ActLineStartP-PChar(Actual)+1,ActLineEndP-ActLineStartP);
  978. writeln('- ',ActLine);
  979. if HasSpecialChar(ActLine) then
  980. writeln('- ',HashSpecialChars(ActLine));
  981. // write expected line
  982. writeln('+ ',ExpLine);
  983. if HasSpecialChar(ExpLine) then
  984. writeln('- ',HashSpecialChars(ExpLine));
  985. // write empty line with pointer ^
  986. for i:=1 to 2+ExpectedP-StartPos do write(' ');
  987. writeln('^');
  988. Msg:='expected "'+ExpLine+'", but got "'+ActLine+'".';
  989. CheckSrcDiff:=false;
  990. // write up to three following actual lines to get some context
  991. for i:=1 to 3 do begin
  992. ActLineStartP:=ActLineEndP;
  993. SkipLineEnd(ActLineStartP);
  994. if ActLineStartP^=#0 then break;
  995. ActLineEndP:=FindLineEnd(ActLineStartP);
  996. ActLine:=copy(Actual,ActLineStartP-PChar(Actual)+1,ActLineEndP-ActLineStartP);
  997. writeln('~ ',ActLine);
  998. end;
  999. exit;
  1000. end;
  1001. until p^=#0;
  1002. writeln('DiffFound Actual:-----------------------');
  1003. writeln(Actual);
  1004. writeln('DiffFound Expected:---------------------');
  1005. writeln(Expected);
  1006. writeln('DiffFound ------------------------------');
  1007. Msg:='diff found, but lines are the same, internal error';
  1008. CheckSrcDiff:=false;
  1009. end;
  1010. var
  1011. IsSpaceNeeded: Boolean;
  1012. LastChar, Quote: Char;
  1013. begin
  1014. Result:=true;
  1015. Msg:='';
  1016. if Expected='' then Expected:=' ';
  1017. if Actual='' then Actual:=' ';
  1018. ExpectedP:=PChar(Expected);
  1019. ActualP:=PChar(Actual);
  1020. repeat
  1021. //writeln('TTestModule.CheckDiff Exp="',ExpectedP^,'" Act="',ActualP^,'"');
  1022. case ExpectedP^ of
  1023. #0:
  1024. begin
  1025. // check that rest of Actual has only spaces
  1026. while ActualP^ in SpaceChars do inc(ActualP);
  1027. if ActualP^<>#0 then
  1028. begin
  1029. DiffFound;
  1030. exit;
  1031. end;
  1032. exit(true);
  1033. end;
  1034. ' ',#9,#10,#13:
  1035. begin
  1036. // skip space in Expected
  1037. IsSpaceNeeded:=false;
  1038. if ExpectedP>PChar(Expected) then
  1039. LastChar:=ExpectedP[-1]
  1040. else
  1041. LastChar:=#0;
  1042. while ExpectedP^ in SpaceChars do inc(ExpectedP);
  1043. if (LastChar in ['a'..'z','A'..'Z','0'..'9','_','$'])
  1044. and (ExpectedP^ in ['a'..'z','A'..'Z','0'..'9','_','$']) then
  1045. IsSpaceNeeded:=true;
  1046. if IsSpaceNeeded and (not (ActualP^ in SpaceChars)) then
  1047. begin
  1048. DiffFound;
  1049. exit;
  1050. end;
  1051. while ActualP^ in SpaceChars do inc(ActualP);
  1052. end;
  1053. '''','"':
  1054. begin
  1055. while ActualP^ in SpaceChars do inc(ActualP);
  1056. if ExpectedP^<>ActualP^ then
  1057. begin
  1058. DiffFound;
  1059. exit;
  1060. end;
  1061. Quote:=ExpectedP^;
  1062. repeat
  1063. inc(ExpectedP);
  1064. inc(ActualP);
  1065. if ExpectedP^<>ActualP^ then
  1066. begin
  1067. DiffFound;
  1068. exit;
  1069. end;
  1070. if (ExpectedP^ in [#0,#10,#13]) then
  1071. break
  1072. else if (ExpectedP^=Quote) then
  1073. begin
  1074. inc(ExpectedP);
  1075. inc(ActualP);
  1076. break;
  1077. end;
  1078. until false;
  1079. end;
  1080. else
  1081. while ActualP^ in SpaceChars do inc(ActualP);
  1082. if ExpectedP^<>ActualP^ then
  1083. begin
  1084. DiffFound;
  1085. exit;
  1086. end;
  1087. inc(ExpectedP);
  1088. inc(ActualP);
  1089. end;
  1090. until false;
  1091. end;
  1092. { TTestEnginePasResolver }
  1093. destructor TTestEnginePasResolver.Destroy;
  1094. begin
  1095. FreeAndNil(FStreamResolver);
  1096. FreeAndNil(FParser);
  1097. FreeAndNil(FScanner);
  1098. FreeAndNil(FStreamResolver);
  1099. if Module<>nil then
  1100. begin
  1101. Module.Release{$IFDEF CheckPasTreeRefCount}('CreateElement'){$ENDIF};
  1102. FModule:=nil;
  1103. end;
  1104. inherited Destroy;
  1105. end;
  1106. function TTestEnginePasResolver.FindUnit(const AName, InFilename: String;
  1107. NameExpr, InFileExpr: TPasExpr): TPasModule;
  1108. begin
  1109. Result:=nil;
  1110. if InFilename<>'' then
  1111. RaiseNotYetImplemented(20180224101926,InFileExpr,'Use testcase tcunitsearch instead');
  1112. if Assigned(OnFindUnit) then
  1113. Result:=OnFindUnit(AName);
  1114. if NameExpr=nil then ;
  1115. end;
  1116. procedure TTestEnginePasResolver.UsedInterfacesFinished(Section: TPasSection);
  1117. begin
  1118. // do not parse recursively
  1119. // parse via the queue
  1120. if Section=nil then ;
  1121. end;
  1122. { TCustomTestModule }
  1123. function TCustomTestModule.GetMsgCount: integer;
  1124. begin
  1125. Result:=FHintMsgs.Count;
  1126. end;
  1127. function TCustomTestModule.GetMsgs(Index: integer): TTestHintMessage;
  1128. begin
  1129. Result:=TTestHintMessage(FHintMsgs[Index]);
  1130. end;
  1131. function TCustomTestModule.GetResolverCount: integer;
  1132. begin
  1133. Result:=FModules.Count;
  1134. end;
  1135. function TCustomTestModule.GetResolvers(Index: integer
  1136. ): TTestEnginePasResolver;
  1137. begin
  1138. Result:=TTestEnginePasResolver(FModules[Index]);
  1139. end;
  1140. function TCustomTestModule.OnPasResolverFindUnit(const aUnitName: String
  1141. ): TPasModule;
  1142. var
  1143. DefNamespace: String;
  1144. begin
  1145. //writeln('TTestModule.OnPasResolverFindUnit START Unit="',aUnitName,'"');
  1146. if (Pos('.',aUnitName)<1) then
  1147. begin
  1148. DefNamespace:=GetDefaultNamespace;
  1149. if DefNamespace<>'' then
  1150. begin
  1151. Result:=LoadUnit(DefNamespace+'.'+aUnitName);
  1152. if Result<>nil then exit;
  1153. end;
  1154. end;
  1155. Result:=LoadUnit(aUnitName);
  1156. if Result<>nil then exit;
  1157. {$IFDEF VerbosePas2JS}
  1158. writeln('TTestModule.OnPasResolverFindUnit missing unit "',aUnitName,'"');
  1159. {$ENDIF}
  1160. Fail('can''t find unit "'+aUnitName+'"');
  1161. end;
  1162. procedure TCustomTestModule.OnParserLog(Sender: TObject; const Msg: String);
  1163. var
  1164. aParser: TPasParser;
  1165. Item: TTestHintMessage;
  1166. begin
  1167. aParser:=Sender as TPasParser;
  1168. Item:=TTestHintMessage.Create;
  1169. Item.Id:=aParser.LastMsgNumber;
  1170. Item.MsgType:=aParser.LastMsgType;
  1171. Item.MsgNumber:=aParser.LastMsgNumber;
  1172. Item.Msg:=Msg;
  1173. Item.SourcePos:=aParser.Scanner.CurSourcePos;
  1174. {$IFDEF VerbosePas2JS}
  1175. writeln('TCustomTestModule.OnParserLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1176. {$ENDIF}
  1177. FHintMsgs.Add(Item);
  1178. end;
  1179. procedure TCustomTestModule.OnPasResolverLog(Sender: TObject; const Msg: String
  1180. );
  1181. var
  1182. aResolver: TTestEnginePasResolver;
  1183. Item: TTestHintMessage;
  1184. begin
  1185. aResolver:=Sender as TTestEnginePasResolver;
  1186. Item:=TTestHintMessage.Create;
  1187. Item.Id:=aResolver.LastMsgId;
  1188. Item.MsgType:=aResolver.LastMsgType;
  1189. Item.MsgNumber:=aResolver.LastMsgNumber;
  1190. Item.Msg:=Msg;
  1191. Item.SourcePos:=aResolver.LastSourcePos;
  1192. {$IFDEF VerbosePas2JS}
  1193. writeln('TCustomTestModule.OnPasResolverLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1194. {$ENDIF}
  1195. FHintMsgs.Add(Item);
  1196. end;
  1197. procedure TCustomTestModule.OnScannerLog(Sender: TObject; const Msg: String);
  1198. var
  1199. Item: TTestHintMessage;
  1200. aScanner: TPas2jsPasScanner;
  1201. begin
  1202. aScanner:=Sender as TPas2jsPasScanner;
  1203. Item:=TTestHintMessage.Create;
  1204. Item.Id:=aScanner.LastMsgNumber;
  1205. Item.MsgType:=aScanner.LastMsgType;
  1206. Item.MsgNumber:=aScanner.LastMsgNumber;
  1207. Item.Msg:=Msg;
  1208. Item.SourcePos:=aScanner.CurSourcePos;
  1209. {$IFDEF VerbosePas2JS}
  1210. writeln('TCustomTestModule.OnScannerLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1211. {$ENDIF}
  1212. FHintMsgs.Add(Item);
  1213. end;
  1214. procedure TCustomTestModule.SetWithTypeInfo(const AValue: boolean);
  1215. begin
  1216. if FWithTypeInfo=AValue then Exit;
  1217. FWithTypeInfo:=AValue;
  1218. if AValue then
  1219. Converter.Options:=Converter.Options-[coNoTypeInfo]
  1220. else
  1221. Converter.Options:=Converter.Options+[coNoTypeInfo];
  1222. end;
  1223. function TCustomTestModule.LoadUnit(const aUnitName: String): TPasModule;
  1224. var
  1225. i: Integer;
  1226. CurEngine: TTestEnginePasResolver;
  1227. CurUnitName: String;
  1228. begin
  1229. //writeln('TTestModule.FindUnit START Unit="',aUnitName,'"');
  1230. Result:=nil;
  1231. if (Module.ClassType=TPasModule)
  1232. and (CompareText(Module.Name,aUnitName)=0) then
  1233. exit(Module);
  1234. for i:=0 to ResolverCount-1 do
  1235. begin
  1236. CurEngine:=Resolvers[i];
  1237. CurUnitName:=ExtractFileUnitName(CurEngine.Filename);
  1238. //writeln('TTestModule.FindUnit Checking ',i,'/',ResolverCount,' ',CurEngine.Filename,' ',CurUnitName);
  1239. if CompareText(aUnitName,CurUnitName)=0 then
  1240. begin
  1241. Result:=CurEngine.Module;
  1242. if Result<>nil then exit;
  1243. //writeln('TTestModule.FindUnit PARSING unit "',CurEngine.Filename,'"');
  1244. FileResolver.FindSourceFile(aUnitName);
  1245. CurEngine.StreamResolver:=TStreamResolver.Create;
  1246. CurEngine.StreamResolver.OwnsStreams:=True;
  1247. //writeln('TTestModule.FindUnit SOURCE=',CurEngine.Source);
  1248. CurEngine.StreamResolver.AddStream(CurEngine.FileName,TStringStream.Create(CurEngine.Source));
  1249. CurEngine.Scanner:=TPas2jsPasScanner.Create(CurEngine.StreamResolver);
  1250. InitScanner(CurEngine.Scanner);
  1251. CurEngine.Parser:=TTestPasParser.Create(CurEngine.Scanner,CurEngine.StreamResolver,CurEngine);
  1252. CurEngine.Parser.Options:=po_tcmodules;
  1253. if CompareText(CurUnitName,'System')=0 then
  1254. CurEngine.Parser.ImplicitUses.Clear;
  1255. CurEngine.Scanner.OpenFile(CurEngine.Filename);
  1256. try
  1257. CurEngine.Parser.NextToken;
  1258. CurEngine.Parser.ParseUnit(CurEngine.FModule);
  1259. except
  1260. on E: Exception do
  1261. HandleException(E);
  1262. end;
  1263. //writeln('TTestModule.FindUnit END ',CurUnitName);
  1264. Result:=CurEngine.Module;
  1265. exit;
  1266. end;
  1267. end;
  1268. end;
  1269. procedure TCustomTestModule.SetUp;
  1270. begin
  1271. {$IFDEF EnablePasTreeGlobalRefCount}
  1272. FElementRefCountAtSetup:=TPasElement.GlobalRefCount;
  1273. {$ENDIF}
  1274. if FModules<>nil then
  1275. begin
  1276. writeln('TCustomTestModule.SetUp FModules<>nil');
  1277. Halt;
  1278. end;
  1279. inherited SetUp;
  1280. FSkipTests:=false;
  1281. FWithTypeInfo:=false;
  1282. FSource:=TStringList.Create;
  1283. FHub:=TPas2JSResolverHub.Create(Self);
  1284. FModules:=TObjectList.Create(true);
  1285. FFilename:='test1.pp';
  1286. FFileResolver:=TStreamResolver.Create;
  1287. FFileResolver.OwnsStreams:=True;
  1288. FScanner:=TPas2jsPasScanner.Create(FFileResolver);
  1289. InitScanner(FScanner);
  1290. FEngine:=AddModule(Filename);
  1291. FEngine.Scanner:=FScanner;
  1292. FScanner.Resolver:=FEngine;
  1293. FParser:=TTestPasParser.Create(FScanner,FFileResolver,FEngine);
  1294. FParser.OnLog:=@OnParserLog;
  1295. FEngine.Parser:=FParser;
  1296. Parser.Options:=po_tcmodules;
  1297. FModule:=Nil;
  1298. FConverter:=CreateConverter;
  1299. FExpectedErrorClass:=nil;
  1300. end;
  1301. function TCustomTestModule.CreateConverter: TPasToJSConverter;
  1302. var
  1303. Options: TPasToJsConverterOptions;
  1304. begin
  1305. Result:=TPasToJSConverter.Create;
  1306. Options:=co_tcmodules;
  1307. if WithTypeInfo then
  1308. Exclude(Options,coNoTypeInfo)
  1309. else
  1310. Include(Options,coNoTypeInfo);
  1311. Result.Options:=Options;
  1312. Result.Globals:=TPasToJSConverterGlobals.Create(Result);
  1313. end;
  1314. procedure TCustomTestModule.InitScanner(aScanner: TPas2jsPasScanner);
  1315. begin
  1316. aScanner.AllowedModeSwitches:=msAllPas2jsModeSwitches;
  1317. aScanner.ReadOnlyModeSwitches:=msAllPas2jsModeSwitchesReadOnly;
  1318. aScanner.CurrentModeSwitches:=OBJFPCModeSwitches*msAllPas2jsModeSwitches+msAllPas2jsModeSwitchesReadOnly;
  1319. aScanner.AllowedBoolSwitches:=bsAllPas2jsBoolSwitches;
  1320. aScanner.ReadOnlyBoolSwitches:=bsAllPas2jsBoolSwitchesReadOnly;
  1321. aScanner.CurrentBoolSwitches:=bsAllPas2jsBoolSwitchesReadOnly+[bsHints,bsNotes,bsWarnings,bsWriteableConst];
  1322. aScanner.AllowedValueSwitches:=vsAllPas2jsValueSwitches;
  1323. aScanner.ReadOnlyValueSwitches:=vsAllPas2jsValueSwitchesReadOnly;
  1324. aScanner.OnLog:=@OnScannerLog;
  1325. aScanner.CompilerVersion:='Comp.Ver.tcmodules';
  1326. end;
  1327. procedure TCustomTestModule.TearDown;
  1328. {$IFDEF CheckPasTreeRefCount}
  1329. var
  1330. El: TPasElement;
  1331. {$ENDIF}
  1332. var
  1333. i: Integer;
  1334. CurModule: TPasModule;
  1335. begin
  1336. FHintMsgs.Clear;
  1337. FHintMsgsGood.Clear;
  1338. FSkipTests:=false;
  1339. FWithTypeInfo:=false;
  1340. FJSRegModuleCall:=nil;
  1341. FJSModuleCallArgs:=nil;
  1342. FJSImplentationUses:=nil;
  1343. FJSInterfaceUses:=nil;
  1344. FJSModuleSrc:=nil;
  1345. FJSInitBody:=nil;
  1346. FreeAndNil(FJSSource);
  1347. FreeAndNil(FJSModule);
  1348. FreeAndNil(FConverter);
  1349. Engine.Clear;
  1350. FreeAndNil(FSource);
  1351. FreeAndNil(FFileResolver);
  1352. if FModules<>nil then
  1353. begin
  1354. for i:=0 to FModules.Count-1 do
  1355. begin
  1356. CurModule:=TTestEnginePasResolver(FModules[i]).Module;
  1357. if CurModule=nil then continue;
  1358. //writeln('TCustomTestModule.TearDown ReleaseUsedUnits ',CurModule.Name,' ',CurModule.RefCount,' ',CurModule.RefIds.Text);
  1359. CurModule.ReleaseUsedUnits;
  1360. end;
  1361. if FModule<>nil then
  1362. FModule.ReleaseUsedUnits;
  1363. for i:=0 to FModules.Count-1 do
  1364. begin
  1365. CurModule:=TTestEnginePasResolver(FModules[i]).Module;
  1366. if CurModule=nil then continue;
  1367. //writeln('TCustomTestModule.TearDown UsesReleased ',CurModule.Name,' ',CurModule.RefCount,' ',CurModule.RefIds.Text);
  1368. end;
  1369. FreeAndNil(FModules);
  1370. ReleaseAndNil(TPasElement(FModule){$IFDEF CheckPasTreeRefCount},'CreateElement'{$ENDIF});
  1371. FEngine:=nil;
  1372. end;
  1373. FreeAndNil(FHub);
  1374. inherited TearDown;
  1375. {$IFDEF EnablePasTreeGlobalRefCount}
  1376. if FElementRefCountAtSetup<>TPasElement.GlobalRefCount then
  1377. begin
  1378. writeln('TCustomTestModule.TearDown GlobalRefCount Was='+IntToStr(FElementRefCountAtSetup)+' Now='+IntToStr(TPasElement.GlobalRefCount));
  1379. {$IFDEF CheckPasTreeRefCount}
  1380. El:=TPasElement.FirstRefEl;
  1381. while El<>nil do
  1382. begin
  1383. writeln(' ',GetObjName(El),' RefIds.Count=',El.RefIds.Count,':');
  1384. for i:=0 to El.RefIds.Count-1 do
  1385. writeln(' ',El.RefIds[i]);
  1386. El:=El.NextRefEl;
  1387. end;
  1388. {$ENDIF}
  1389. Halt;
  1390. Fail('TCustomTestModule.TearDown Was='+IntToStr(FElementRefCountAtSetup)+' Now='+IntToStr(TPasElement.GlobalRefCount));
  1391. end;
  1392. {$ENDIF}
  1393. end;
  1394. procedure TCustomTestModule.Add(Line: string);
  1395. begin
  1396. Source.Add(Line);
  1397. end;
  1398. procedure TCustomTestModule.Add(const Lines: array of string);
  1399. var
  1400. i: Integer;
  1401. begin
  1402. for i:=low(Lines) to high(Lines) do
  1403. Add(Lines[i]);
  1404. end;
  1405. procedure TCustomTestModule.StartParsing;
  1406. var
  1407. Src: String;
  1408. begin
  1409. Src:=Source.Text;
  1410. FEngine.Source:=Src;
  1411. FileResolver.AddStream(FileName,TStringStream.Create(Src));
  1412. Scanner.OpenFile(FileName);
  1413. Writeln('// Test : ',Self.TestName);
  1414. Writeln(Src);
  1415. end;
  1416. procedure TCustomTestModule.ParseModuleQueue;
  1417. var
  1418. i: Integer;
  1419. CurResolver: TTestEnginePasResolver;
  1420. Found: Boolean;
  1421. Section: TPasSection;
  1422. begin
  1423. // parse til exception or all modules finished
  1424. while not SkipTests do
  1425. begin
  1426. Found:=false;
  1427. for i:=0 to ResolverCount-1 do
  1428. begin
  1429. CurResolver:=Resolvers[i];
  1430. if CurResolver.CurrentParser=nil then continue;
  1431. if not CurResolver.CurrentParser.CanParseContinue(Section) then
  1432. continue;
  1433. CurResolver.Parser.ParseContinue;
  1434. Found:=true;
  1435. break;
  1436. end;
  1437. if not Found then break;
  1438. end;
  1439. for i:=0 to ResolverCount-1 do
  1440. begin
  1441. CurResolver:=Resolvers[i];
  1442. if CurResolver.Parser=nil then
  1443. begin
  1444. if CurResolver.CurrentParser<>nil then
  1445. Fail('TCustomTestModule.ParseModuleQueue '+CurResolver.Filename+' '+GetObjName(CurResolver.Parser)+'=Parser<>CurrentParser='+GetObjName(CurResolver.CurrentParser));
  1446. continue;
  1447. end;
  1448. if CurResolver.Parser.CurModule<>nil then
  1449. Fail('TCustomTestModule.ParseModuleQueue '+CurResolver.Filename+' NOT FINISHED CurModule='+GetObjName(CurResolver.Parser.CurModule));
  1450. end;
  1451. end;
  1452. procedure TCustomTestModule.ParseModule;
  1453. begin
  1454. if SkipTests then exit;
  1455. FFirstPasStatement:=nil;
  1456. try
  1457. StartParsing;
  1458. Parser.ParseMain(FModule);
  1459. ParseModuleQueue;
  1460. except
  1461. on E: Exception do
  1462. HandleException(E);
  1463. end;
  1464. if SkipTests then exit;
  1465. AssertNotNull('Module resulted in Module',Module);
  1466. AssertEquals('modulename',lowercase(ChangeFileExt(FFileName,'')),lowercase(Module.Name));
  1467. TAssert.AssertSame('Has resolver',Engine,Parser.Engine);
  1468. end;
  1469. procedure TCustomTestModule.ParseProgram;
  1470. begin
  1471. if SkipTests then exit;
  1472. ParseModule;
  1473. if SkipTests then exit;
  1474. AssertEquals('Has program',TPasProgram,Module.ClassType);
  1475. FPasProgram:=TPasProgram(Module);
  1476. AssertNotNull('Has program section',PasProgram.ProgramSection);
  1477. AssertNotNull('Has initialization section',PasProgram.InitializationSection);
  1478. if (PasProgram.InitializationSection.Elements.Count>0) then
  1479. if TObject(PasProgram.InitializationSection.Elements[0]) is TPasImplBlock then
  1480. FFirstPasStatement:=TPasImplBlock(PasProgram.InitializationSection.Elements[0]);
  1481. end;
  1482. procedure TCustomTestModule.ParseUnit;
  1483. begin
  1484. if SkipTests then exit;
  1485. ParseModule;
  1486. if SkipTests then exit;
  1487. AssertEquals('Has unit (TPasModule)',TPasModule,Module.ClassType);
  1488. AssertNotNull('Has interface section',Module.InterfaceSection);
  1489. AssertNotNull('Has implementation section',Module.ImplementationSection);
  1490. if (Module.InitializationSection<>nil)
  1491. and (Module.InitializationSection.Elements.Count>0)
  1492. and (TObject(Module.InitializationSection.Elements[0]) is TPasImplBlock) then
  1493. FFirstPasStatement:=TPasImplBlock(Module.InitializationSection.Elements[0]);
  1494. end;
  1495. function TCustomTestModule.FindModuleWithFilename(aFilename: string
  1496. ): TTestEnginePasResolver;
  1497. var
  1498. i: Integer;
  1499. begin
  1500. for i:=0 to ResolverCount-1 do
  1501. if CompareText(Resolvers[i].Filename,aFilename)=0 then
  1502. exit(Resolvers[i]);
  1503. Result:=nil;
  1504. end;
  1505. function TCustomTestModule.AddModule(aFilename: string
  1506. ): TTestEnginePasResolver;
  1507. begin
  1508. //writeln('TTestModuleConverter.AddModule ',aFilename);
  1509. if FindModuleWithFilename(aFilename)<>nil then
  1510. Fail('TTestModuleConverter.AddModule: file "'+aFilename+'" already exists');
  1511. Result:=TTestEnginePasResolver.Create;
  1512. Result.Filename:=aFilename;
  1513. Result.AddObjFPCBuiltInIdentifiers(btAllJSBaseTypes,bfAllJSBaseProcs);
  1514. Result.OnFindUnit:=@OnPasResolverFindUnit;
  1515. Result.OnLog:=@OnPasResolverLog;
  1516. Result.Hub:=Hub;
  1517. FModules.Add(Result);
  1518. end;
  1519. function TCustomTestModule.AddModuleWithSrc(aFilename, Src: string
  1520. ): TTestEnginePasResolver;
  1521. begin
  1522. Result:=AddModule(aFilename);
  1523. Result.Source:=Src;
  1524. end;
  1525. function TCustomTestModule.AddModuleWithIntfImplSrc(aFilename, InterfaceSrc,
  1526. ImplementationSrc: string): TTestEnginePasResolver;
  1527. var
  1528. Src: String;
  1529. begin
  1530. Src:='unit '+ExtractFileUnitName(aFilename)+';'+LineEnding;
  1531. Src+=LineEnding;
  1532. Src+='interface'+LineEnding;
  1533. Src+=LineEnding;
  1534. Src+=InterfaceSrc;
  1535. Src+='implementation'+LineEnding;
  1536. Src+=LineEnding;
  1537. Src+=ImplementationSrc;
  1538. Src+='end.'+LineEnding;
  1539. Result:=AddModuleWithSrc(aFilename,Src);
  1540. end;
  1541. procedure TCustomTestModule.AddSystemUnit(Parts: TSystemUnitParts);
  1542. var
  1543. Intf, Impl: TStringList;
  1544. begin
  1545. Intf:=TStringList.Create;
  1546. if supTInterfacedObject in Parts then Include(Parts,supTObject);
  1547. // unit interface
  1548. if [supTVarRec,supTypeInfo]*Parts<>[] then
  1549. Intf.Add('{$modeswitch externalclass}');
  1550. Intf.Add('type');
  1551. Intf.Add(' integer=longint;');
  1552. Intf.Add(' sizeint=nativeint;');
  1553. //'const',
  1554. //' LineEnding = #10;',
  1555. //' DirectorySeparator = ''/'';',
  1556. //' DriveSeparator = '''';',
  1557. //' AllowDirectorySeparators : set of char = [''\'',''/''];',
  1558. //' AllowDriveSeparators : set of char = [];',
  1559. if supTObject in Parts then
  1560. Intf.AddStrings([
  1561. 'type',
  1562. ' TClass = class of TObject;',
  1563. ' TObject = class',
  1564. ' constructor Create;',
  1565. ' destructor Destroy; virtual;',
  1566. ' class function ClassType: TClass; assembler;',
  1567. ' class function ClassName: String; assembler;',
  1568. ' class function ClassNameIs(const Name: string): boolean;',
  1569. ' class function ClassParent: TClass; assembler;',
  1570. ' class function InheritsFrom(aClass: TClass): boolean; assembler;',
  1571. ' class function UnitName: String; assembler;',
  1572. ' procedure AfterConstruction; virtual;',
  1573. ' procedure BeforeDestruction;virtual;',
  1574. ' function Equals(Obj: TObject): boolean; virtual;',
  1575. ' function ToString: String; virtual;',
  1576. ' end;']);
  1577. if supTInterfacedObject in Parts then
  1578. Intf.AddStrings([
  1579. ' {$Interfaces COM}',
  1580. ' IUnknown = interface',
  1581. ' [''{00000000-0000-0000-C000-000000000046}'']',
  1582. //' function QueryInterface(const iid: TGuid; out obj): Integer;',
  1583. ' function _AddRef: Integer;',
  1584. ' function _Release: Integer;',
  1585. ' end;',
  1586. ' IInterface = IUnknown;',
  1587. ' TInterfacedObject = class(TObject,IUnknown)',
  1588. ' protected',
  1589. ' fRefCount: Integer;',
  1590. ' { implement methods of IUnknown }',
  1591. //' function QueryInterface(const iid: TGuid; out obj): Integer; virtual;',
  1592. ' function _AddRef: Integer; virtual;',
  1593. ' function _Release: Integer; virtual;',
  1594. ' end;',
  1595. ' TInterfacedClass = class of TInterfacedObject;',
  1596. '',
  1597. '']);
  1598. if supTVarRec in Parts then
  1599. Intf.AddStrings([
  1600. 'const',
  1601. ' vtInteger = 0;',
  1602. ' vtBoolean = 1;',
  1603. ' vtJSValue = 19;',
  1604. 'type',
  1605. ' PVarRec = ^TVarRec;',
  1606. ' TVarRec = record',
  1607. ' VType : byte;',
  1608. ' VJSValue: JSValue;',
  1609. ' vInteger: longint external name ''VJSValue'';',
  1610. ' vBoolean: boolean external name ''VJSValue'';',
  1611. ' end;',
  1612. ' TVarRecArray = array of TVarRec;',
  1613. 'function VarRecs: TVarRecArray; varargs;',
  1614. '']);
  1615. if supTypeInfo in Parts then
  1616. begin
  1617. Intf.AddStrings([
  1618. 'type',
  1619. ' TTypeKind = (',
  1620. ' tkUnknown, // 0',
  1621. ' tkInteger, // 1',
  1622. ' tkChar, // 2 in Delphi/FPC tkWChar, tkUChar',
  1623. ' tkString, // 3 in Delphi/FPC tkSString, tkWString or tkUString',
  1624. ' tkEnumeration, // 4',
  1625. ' tkSet, // 5',
  1626. ' tkDouble, // 6',
  1627. ' tkBool, // 7',
  1628. ' tkProcVar, // 8 function or procedure',
  1629. ' tkMethod, // 9 proc var of object',
  1630. ' tkArray, // 10 static array',
  1631. ' tkDynArray, // 11',
  1632. ' tkRecord, // 12',
  1633. ' tkClass, // 13',
  1634. ' tkClassRef, // 14',
  1635. ' tkPointer, // 15',
  1636. ' tkJSValue, // 16',
  1637. ' tkRefToProcVar, // 17 variable of procedure type',
  1638. ' tkInterface, // 18',
  1639. ' //tkObject,',
  1640. ' //tkSString,tkLString,tkAString,tkWString,',
  1641. ' //tkVariant,',
  1642. ' //tkWChar,',
  1643. ' //tkInt64,',
  1644. ' //tkQWord,',
  1645. ' //tkInterfaceRaw,',
  1646. ' //tkUString,tkUChar,',
  1647. ' tkHelper, // 19',
  1648. ' //tkFile,',
  1649. ' tkExtClass // 20',
  1650. ' );',
  1651. ' TTypeKinds = set of TTypeKind;',
  1652. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  1653. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo)',
  1654. ' end;',
  1655. ' TTypeInfoEnum = class external name ''rtl.tTypeInfoEnum''(TTypeInfoInteger) end;',
  1656. ' TTypeInfoSet = class external name ''rtl.tTypeInfoSet''(TTypeInfo) end;',
  1657. ' TTypeInfoStaticArray = class external name ''rtl.tTypeInfoStaticArray''(TTypeInfo) end;',
  1658. ' TTypeInfoDynArray = class external name ''rtl.tTypeInfoDynArray''(TTypeInfo) end;',
  1659. ' TTypeInfoProcVar = class external name ''rtl.tTypeInfoProcVar''(TTypeInfo) end;',
  1660. ' TTypeInfoMethodVar = class external name ''rtl.tTypeInfoMethodVar''(TTypeInfoProcVar) end;',
  1661. ' TTypeInfoClass = class external name ''rtl.tTypeInfoClass''(TTypeInfo) end;',
  1662. ' TTypeInfoClassRef = class external name ''rtl.tTypeInfoClassRef''(TTypeInfo) end;',
  1663. ' TTypeInfoExtClass = class external name ''rtl.tTypeInfoExtClass''(TTypeInfo) end;',
  1664. ' TTypeInfoRecord = class external name ''rtl.tTypeInfoRecord''(TTypeInfo) end;',
  1665. ' TTypeInfoPointer = class external name ''rtl.tTypeInfoPointer''(TTypeInfo) end;',
  1666. ' TTypeInfoHelper = class external name ''rtl.tTypeInfoHelper''(TTypeInfo) end;',
  1667. ' TTypeInfoInterface = class external name ''rtl.tTypeInfoInterface''(TTypeInfo) end;',
  1668. '']);
  1669. end;
  1670. if supWriteln in Parts then
  1671. Intf.Add('procedure writeln; varargs; external name ''console.log'';');
  1672. Intf.Add('var');
  1673. Intf.Add(' ExitCode: Longint = 0;');
  1674. // unit implementation
  1675. Impl:=TStringList.Create;
  1676. if supTObject in Parts then
  1677. Impl.AddStrings([
  1678. '// needed by ClassNameIs, the real SameText is in SysUtils',
  1679. 'function SameText(const s1, s2: String): Boolean; assembler;',
  1680. 'asm',
  1681. 'end;',
  1682. 'constructor TObject.Create; begin end;',
  1683. 'destructor TObject.Destroy; begin end;',
  1684. 'class function TObject.ClassType: TClass; assembler;',
  1685. 'asm',
  1686. 'end;',
  1687. 'class function TObject.ClassName: String; assembler;',
  1688. 'asm',
  1689. 'end;',
  1690. 'class function TObject.ClassNameIs(const Name: string): boolean;',
  1691. 'begin',
  1692. ' Result:=SameText(Name,ClassName);',
  1693. 'end;',
  1694. 'class function TObject.ClassParent: TClass; assembler;',
  1695. 'asm',
  1696. 'end;',
  1697. 'class function TObject.InheritsFrom(aClass: TClass): boolean; assembler;',
  1698. 'asm',
  1699. 'end;',
  1700. 'class function TObject.UnitName: String; assembler;',
  1701. 'asm',
  1702. 'end;',
  1703. 'procedure TObject.AfterConstruction; begin end;',
  1704. 'procedure TObject.BeforeDestruction; begin end;',
  1705. 'function TObject.Equals(Obj: TObject): boolean;',
  1706. 'begin',
  1707. ' Result:=Obj=Self;',
  1708. 'end;',
  1709. 'function TObject.ToString: String;',
  1710. 'begin',
  1711. ' Result:=ClassName;',
  1712. 'end;'
  1713. ]);
  1714. if supTInterfacedObject in Parts then
  1715. Impl.AddStrings([
  1716. //'function TInterfacedObject.QueryInterface(const iid: TGuid; out obj): Integer;',
  1717. //'begin',
  1718. //'end;',
  1719. 'function TInterfacedObject._AddRef: Integer;',
  1720. 'begin',
  1721. 'end;',
  1722. 'function TInterfacedObject._Release: Integer;',
  1723. 'begin',
  1724. 'end;',
  1725. '']);
  1726. if supTVarRec in Parts then
  1727. Impl.AddStrings([
  1728. 'function VarRecs: TVarRecArray; varargs;',
  1729. 'var',
  1730. ' v: PVarRec;',
  1731. 'begin',
  1732. ' v^.VType:=1;',
  1733. ' v^.VJSValue:=2;',
  1734. 'end;',
  1735. '']);
  1736. try
  1737. AddModuleWithIntfImplSrc('system.pp',Intf.Text,Impl.Text);
  1738. finally
  1739. Intf.Free;
  1740. Impl.Free;
  1741. end;
  1742. end;
  1743. procedure TCustomTestModule.StartProgram(NeedSystemUnit: boolean;
  1744. SystemUnitParts: TSystemUnitParts);
  1745. begin
  1746. if NeedSystemUnit then
  1747. AddSystemUnit(SystemUnitParts)
  1748. else
  1749. Parser.ImplicitUses.Clear;
  1750. Add('program '+ExtractFileUnitName(Filename)+';');
  1751. Add('');
  1752. end;
  1753. procedure TCustomTestModule.StartUnit(NeedSystemUnit: boolean;
  1754. SystemUnitParts: TSystemUnitParts);
  1755. begin
  1756. if NeedSystemUnit then
  1757. AddSystemUnit(SystemUnitParts)
  1758. else
  1759. Parser.ImplicitUses.Clear;
  1760. Add('unit Test1;');
  1761. Add('');
  1762. end;
  1763. procedure TCustomTestModule.ConvertModule;
  1764. procedure CheckUsesList(UsesName: String; Arg: TJSArrayLiteralElement;
  1765. out UsesLit: TJSArrayLiteral);
  1766. var
  1767. i: Integer;
  1768. Item: TJSElement;
  1769. Lit: TJSLiteral;
  1770. begin
  1771. UsesLit:=nil;
  1772. AssertNotNull(UsesName+' uses section',Arg.Expr);
  1773. if (Arg.Expr.ClassType=TJSLiteral) and TJSLiteral(Arg.Expr).Value.IsNull then
  1774. exit; // null is ok
  1775. AssertEquals(UsesName+' uses section param is array',TJSArrayLiteral,Arg.Expr.ClassType);
  1776. FJSInterfaceUses:=TJSArrayLiteral(Arg.Expr);
  1777. for i:=0 to FJSInterfaceUses.Elements.Count-1 do
  1778. begin
  1779. Item:=FJSInterfaceUses.Elements.Elements[i].Expr;
  1780. AssertNotNull(UsesName+' uses section item['+IntToStr(i)+'].Expr',Item);
  1781. AssertEquals(UsesName+' uses section item['+IntToStr(i)+'] is lit',TJSLiteral,Item.ClassType);
  1782. Lit:=TJSLiteral(Item);
  1783. AssertEquals(UsesName+' uses section item['+IntToStr(i)+'] is string lit',
  1784. ord(jsbase.jstString),ord(Lit.Value.ValueType));
  1785. end;
  1786. end;
  1787. procedure CheckFunctionParam(ParamName: string; Arg: TJSArrayLiteralElement;
  1788. out Src: TJSSourceElements);
  1789. var
  1790. FunDecl: TJSFunctionDeclarationStatement;
  1791. FunDef: TJSFuncDef;
  1792. FunBody: TJSFunctionBody;
  1793. begin
  1794. Src:=nil;
  1795. AssertNotNull(ParamName,Arg.Expr);
  1796. AssertEquals(ParamName+' Arg.Expr type',TJSFunctionDeclarationStatement,Arg.Expr.ClassType);
  1797. FunDecl:=Arg.Expr as TJSFunctionDeclarationStatement;
  1798. AssertNotNull(ParamName+' FunDecl.AFunction',FunDecl.AFunction);
  1799. AssertEquals(ParamName+' FunDecl.AFunction type',TJSFuncDef,FunDecl.AFunction.ClassType);
  1800. FunDef:=FunDecl.AFunction as TJSFuncDef;
  1801. AssertEquals(ParamName+' name empty','',String(FunDef.Name));
  1802. AssertNotNull(ParamName+' body',FunDef.Body);
  1803. AssertEquals(ParamName+' body type',TJSFunctionBody,FunDef.Body.ClassType);
  1804. FunBody:=FunDef.Body as TJSFunctionBody;
  1805. AssertNotNull(ParamName+' body.A',FunBody.A);
  1806. AssertEquals(ParamName+' body.A type',TJSSourceElements,FunBody.A.ClassType);
  1807. Src:=FunBody.A as TJSSourceElements;
  1808. end;
  1809. var
  1810. ModuleNameExpr: TJSLiteral;
  1811. InitFunction: TJSFunctionDeclarationStatement;
  1812. InitAssign: TJSSimpleAssignStatement;
  1813. InitName: String;
  1814. LastNode: TJSElement;
  1815. Arg: TJSArrayLiteralElement;
  1816. begin
  1817. if SkipTests then exit;
  1818. try
  1819. FJSModule:=FConverter.ConvertPasElement(Module,Engine) as TJSSourceElements;
  1820. except
  1821. on E: Exception do
  1822. HandleException(E);
  1823. end;
  1824. if SkipTests then exit;
  1825. if ExpectedErrorClass<>nil then
  1826. Fail('Missing '+ExpectedErrorClass.ClassName+' error {'+ExpectedErrorMsg+'} ('+IntToStr(ExpectedErrorNumber)+')');
  1827. FJSSource:=TStringList.Create;
  1828. FJSSource.Text:=ConvertJSModuleToString(JSModule);
  1829. {$IFDEF VerbosePas2JS}
  1830. writeln('TTestModule.ConvertModule JS:');
  1831. write(FJSSource.Text);
  1832. {$ENDIF}
  1833. // rtl.module(...
  1834. AssertEquals('jsmodule has one statement - the call',1,JSModule.Statements.Count);
  1835. AssertNotNull('register module call',JSModule.Statements.Nodes[0].Node);
  1836. AssertEquals('register module call',TJSCallExpression,JSModule.Statements.Nodes[0].Node.ClassType);
  1837. FJSRegModuleCall:=JSModule.Statements.Nodes[0].Node as TJSCallExpression;
  1838. AssertNotNull('register module rtl.module expr',JSRegModuleCall.Expr);
  1839. AssertNotNull('register module rtl.module args',JSRegModuleCall.Args);
  1840. AssertEquals('rtl.module args',TJSArguments,JSRegModuleCall.Args.ClassType);
  1841. FJSModuleCallArgs:=JSRegModuleCall.Args as TJSArguments;
  1842. // parameter 'unitname'
  1843. if JSModuleCallArgs.Elements.Count<1 then
  1844. Fail('rtl.module first param unit missing');
  1845. Arg:=JSModuleCallArgs.Elements.Elements[0];
  1846. AssertNotNull('module name param',Arg.Expr);
  1847. ModuleNameExpr:=Arg.Expr as TJSLiteral;
  1848. AssertEquals('module name param is string',ord(jstString),ord(ModuleNameExpr.Value.ValueType));
  1849. if Module is TPasProgram then
  1850. AssertEquals('module name','program',String(ModuleNameExpr.Value.AsString))
  1851. else
  1852. AssertEquals('module name',Module.Name,String(ModuleNameExpr.Value.AsString));
  1853. // main uses section
  1854. if JSModuleCallArgs.Elements.Count<2 then
  1855. Fail('rtl.module second param main uses missing');
  1856. Arg:=JSModuleCallArgs.Elements.Elements[1];
  1857. CheckUsesList('interface',Arg,FJSInterfaceUses);
  1858. // program/library/interface function()
  1859. if JSModuleCallArgs.Elements.Count<3 then
  1860. Fail('rtl.module third param intf-function missing');
  1861. Arg:=JSModuleCallArgs.Elements.Elements[2];
  1862. CheckFunctionParam('module intf-function',Arg,FJSModuleSrc);
  1863. // search for $mod.$init or $mod.$main - the last statement
  1864. if Module is TPasProgram then
  1865. begin
  1866. InitName:='$main';
  1867. AssertEquals('$mod.'+InitName+' function 1',true,JSModuleSrc.Statements.Count>0);
  1868. end
  1869. else
  1870. InitName:='$init';
  1871. FJSInitBody:=nil;
  1872. if JSModuleSrc.Statements.Count>0 then
  1873. begin
  1874. LastNode:=JSModuleSrc.Statements.Nodes[JSModuleSrc.Statements.Count-1].Node;
  1875. if LastNode is TJSSimpleAssignStatement then
  1876. begin
  1877. InitAssign:=LastNode as TJSSimpleAssignStatement;
  1878. if GetDottedIdentifier(InitAssign.LHS)='$mod.'+InitName then
  1879. begin
  1880. InitFunction:=InitAssign.Expr as TJSFunctionDeclarationStatement;
  1881. FJSInitBody:=InitFunction.AFunction.Body as TJSFunctionBody;
  1882. end
  1883. else if Module is TPasProgram then
  1884. CheckDottedIdentifier('init function',InitAssign.LHS,'$mod.'+InitName);
  1885. end;
  1886. end;
  1887. // optional: implementation uses section
  1888. if JSModuleCallArgs.Elements.Count<4 then
  1889. exit;
  1890. Arg:=JSModuleCallArgs.Elements.Elements[3];
  1891. CheckUsesList('implementation',Arg,FJSImplentationUses);
  1892. end;
  1893. procedure TCustomTestModule.ConvertProgram;
  1894. begin
  1895. Add('end.');
  1896. ParseProgram;
  1897. ConvertModule;
  1898. end;
  1899. procedure TCustomTestModule.ConvertUnit;
  1900. begin
  1901. Add('end.');
  1902. ParseUnit;
  1903. ConvertModule;
  1904. end;
  1905. function TCustomTestModule.ConvertJSModuleToString(El: TJSElement): string;
  1906. begin
  1907. Result:=tcmodules.JSToStr(El);
  1908. end;
  1909. procedure TCustomTestModule.CheckDottedIdentifier(Msg: string; El: TJSElement;
  1910. DottedName: string);
  1911. begin
  1912. if DottedName='' then
  1913. begin
  1914. AssertNull(Msg,El);
  1915. end
  1916. else
  1917. begin
  1918. AssertNotNull(Msg,El);
  1919. AssertEquals(Msg,DottedName,GetDottedIdentifier(El));
  1920. end;
  1921. end;
  1922. function TCustomTestModule.GetDottedIdentifier(El: TJSElement): string;
  1923. begin
  1924. if El=nil then
  1925. Result:=''
  1926. else if El is TJSPrimaryExpressionIdent then
  1927. Result:=String(TJSPrimaryExpressionIdent(El).Name)
  1928. else if El is TJSDotMemberExpression then
  1929. Result:=GetDottedIdentifier(TJSDotMemberExpression(El).MExpr)+'.'+String(TJSDotMemberExpression(El).Name)
  1930. else
  1931. AssertEquals('GetDottedIdentifier',TJSPrimaryExpressionIdent,El.ClassType);
  1932. end;
  1933. procedure TCustomTestModule.CheckSource(Msg, Statements: String;
  1934. InitStatements: string; ImplStatements: string);
  1935. var
  1936. ActualSrc, ExpectedSrc, InitName: String;
  1937. begin
  1938. ActualSrc:=JSToStr(JSModuleSrc);
  1939. if coUseStrict in Converter.Options then
  1940. ExpectedSrc:='"use strict";'+LineEnding
  1941. else
  1942. ExpectedSrc:='';
  1943. ExpectedSrc:=ExpectedSrc+'var $mod = this;'+LineEnding;
  1944. ExpectedSrc:=ExpectedSrc+Statements;
  1945. // unit implementation
  1946. if (Trim(ImplStatements)<>'') then
  1947. ExpectedSrc:=ExpectedSrc+LineEnding
  1948. +'$mod.$implcode = function () {'+LineEnding
  1949. +ImplStatements
  1950. +'};'+LineEnding;
  1951. // program main or unit initialization
  1952. if (Module is TPasProgram) or (Trim(InitStatements)<>'') then
  1953. begin
  1954. if Module is TPasProgram then
  1955. InitName:='$main'
  1956. else
  1957. InitName:='$init';
  1958. ExpectedSrc:=ExpectedSrc+LineEnding
  1959. +'$mod.'+InitName+' = function () {'+LineEnding
  1960. +InitStatements
  1961. +'};'+LineEnding;
  1962. end;
  1963. //writeln('TCustomTestModule.CheckSource ExpectedIntf="',ExpectedSrc,'"');
  1964. //writeln('TTestModule.CheckSource InitStatements="',Trim(InitStatements),'"');
  1965. CheckDiff(Msg,ExpectedSrc,ActualSrc);
  1966. end;
  1967. procedure TCustomTestModule.CheckDiff(Msg, Expected, Actual: string);
  1968. // search diff, ignore changes in spaces
  1969. var
  1970. s: string;
  1971. begin
  1972. if CheckSrcDiff(Expected,Actual,s) then exit;
  1973. Fail(Msg+': '+s);
  1974. end;
  1975. procedure TCustomTestModule.CheckUnit(Filename, ExpectedSrc: string);
  1976. var
  1977. aResolver: TTestEnginePasResolver;
  1978. aConverter: TPasToJSConverter;
  1979. aJSModule: TJSSourceElements;
  1980. ActualSrc: String;
  1981. begin
  1982. aResolver:=GetResolver(Filename);
  1983. AssertNotNull('missing resolver of unit '+Filename,aResolver);
  1984. AssertNotNull('missing resolver.module of unit '+Filename,aResolver.Module);
  1985. {$IFDEF VerbosePas2JS}
  1986. writeln('CheckUnit '+Filename+' converting ...');
  1987. {$ENDIF}
  1988. aConverter:=CreateConverter;
  1989. aJSModule:=nil;
  1990. try
  1991. try
  1992. aJSModule:=aConverter.ConvertPasElement(aResolver.Module,aResolver) as TJSSourceElements;
  1993. except
  1994. on E: Exception do
  1995. HandleException(E);
  1996. end;
  1997. ActualSrc:=ConvertJSModuleToString(aJSModule);
  1998. {$IFDEF VerbosePas2JS}
  1999. writeln('TTestModule.CheckUnit ',Filename,' Pas:');
  2000. write(aResolver.Source);
  2001. writeln('TTestModule.CheckUnit ',Filename,' JS:');
  2002. write(ActualSrc);
  2003. {$ENDIF}
  2004. CheckDiff('Converted unit: "'+ChangeFileExt(Filename,'.js')+'"',ExpectedSrc,ActualSrc);
  2005. finally
  2006. aJSModule.Free;
  2007. aConverter.Free;
  2008. end;
  2009. end;
  2010. procedure TCustomTestModule.CheckHint(MsgType: TMessageType;
  2011. MsgNumber: integer; Msg: string; Marker: PSrcMarker);
  2012. var
  2013. i: Integer;
  2014. Item: TTestHintMessage;
  2015. Expected,Actual: string;
  2016. begin
  2017. //writeln('TCustomTestModule.CheckHint MsgCount=',MsgCount);
  2018. for i:=0 to MsgCount-1 do
  2019. begin
  2020. Item:=Msgs[i];
  2021. if (Item.MsgNumber<>MsgNumber) or (Item.Msg<>Msg) then continue;
  2022. if (Marker<>nil) then
  2023. begin
  2024. if Item.SourcePos.Row<>cardinal(Marker^.Row) then continue;
  2025. if (Item.SourcePos.Column<cardinal(Marker^.StartCol))
  2026. or (Item.SourcePos.Column>cardinal(Marker^.EndCol)) then continue;
  2027. end;
  2028. // found
  2029. FHintMsgsGood.Add(Item);
  2030. str(Item.MsgType,Actual);
  2031. str(MsgType,Expected);
  2032. AssertEquals('MsgType',Expected,Actual);
  2033. exit;
  2034. end;
  2035. // needed message missing -> show emitted messages
  2036. WriteSources('',0,0);
  2037. for i:=0 to MsgCount-1 do
  2038. begin
  2039. Item:=Msgs[i];
  2040. write('TCustomTestModule.CheckHint ',i,'/',MsgCount,' ',Item.MsgType,
  2041. ' ('+IntToStr(Item.MsgNumber),')');
  2042. if Marker<>nil then
  2043. write(' '+ExtractFileName(Item.SourcePos.FileName),'(',Item.SourcePos.Row,',',Item.SourcePos.Column,')');
  2044. writeln(' {',Item.Msg,'}');
  2045. end;
  2046. str(MsgType,Expected);
  2047. Actual:='Missing '+Expected+' ('+IntToStr(MsgNumber)+')';
  2048. if Marker<>nil then
  2049. Actual:=Actual+' '+ExtractFileName(Marker^.Filename)+'('+IntToStr(Marker^.Row)+','+IntToStr(Marker^.StartCol)+'..'+IntToStr(Marker^.EndCol)+')';
  2050. Actual:=Actual+' '+Msg;
  2051. Fail(Actual);
  2052. end;
  2053. procedure TCustomTestModule.CheckResolverUnexpectedHints(WithSourcePos: boolean
  2054. );
  2055. var
  2056. i: Integer;
  2057. s, Txt: String;
  2058. Msg: TTestHintMessage;
  2059. begin
  2060. for i:=0 to MsgCount-1 do
  2061. begin
  2062. Msg:=Msgs[i];
  2063. if FHintMsgsGood.IndexOf(Msg)>=0 then continue;
  2064. s:='';
  2065. str(Msg.MsgType,s);
  2066. Txt:='Unexpected resolver message found ['+IntToStr(Msg.Id)+'] '
  2067. +s+': ('+IntToStr(Msg.MsgNumber)+')';
  2068. if WithSourcePos then
  2069. Txt:=Txt+' '+ExtractFileName(Msg.SourcePos.FileName)+'('+IntToStr(Msg.SourcePos.Row)+','+IntToStr(Msg.SourcePos.Column)+')';
  2070. Txt:=Txt+' {'+Msg.Msg+'}';
  2071. Fail(Txt);
  2072. end;
  2073. end;
  2074. procedure TCustomTestModule.SetExpectedScannerError(Msg: string;
  2075. MsgNumber: integer);
  2076. begin
  2077. ExpectedErrorClass:=EScannerError;
  2078. ExpectedErrorMsg:=Msg;
  2079. ExpectedErrorNumber:=MsgNumber;
  2080. end;
  2081. procedure TCustomTestModule.SetExpectedParserError(Msg: string;
  2082. MsgNumber: integer);
  2083. begin
  2084. ExpectedErrorClass:=EParserError;
  2085. ExpectedErrorMsg:=Msg;
  2086. ExpectedErrorNumber:=MsgNumber;
  2087. end;
  2088. procedure TCustomTestModule.SetExpectedPasResolverError(Msg: string;
  2089. MsgNumber: integer);
  2090. begin
  2091. ExpectedErrorClass:=EPasResolve;
  2092. ExpectedErrorMsg:=Msg;
  2093. ExpectedErrorNumber:=MsgNumber;
  2094. end;
  2095. procedure TCustomTestModule.SetExpectedConverterError(Msg: string;
  2096. MsgNumber: integer);
  2097. begin
  2098. ExpectedErrorClass:=EPas2JS;
  2099. ExpectedErrorMsg:=Msg;
  2100. ExpectedErrorNumber:=MsgNumber;
  2101. end;
  2102. function TCustomTestModule.IsErrorExpected(E: Exception): boolean;
  2103. var
  2104. MsgNumber: Integer;
  2105. Msg: String;
  2106. begin
  2107. Result:=false;
  2108. if (ExpectedErrorClass=nil) or (ExpectedErrorClass<>E.ClassType) then exit;
  2109. Msg:=E.Message;
  2110. if E is EPas2JS then
  2111. MsgNumber:=EPas2JS(E).MsgNumber
  2112. else if E is EPasResolve then
  2113. MsgNumber:=EPasResolve(E).MsgNumber
  2114. else if E is EParserError then
  2115. MsgNumber:=Parser.LastMsgNumber
  2116. else if E is EScannerError then
  2117. begin
  2118. MsgNumber:=Scanner.LastMsgNumber;
  2119. Msg:=Scanner.LastMsg;
  2120. end
  2121. else
  2122. MsgNumber:=0;
  2123. Result:=(MsgNumber=ExpectedErrorNumber) and (Msg=ExpectedErrorMsg);
  2124. if Result then
  2125. SkipTests:=true;
  2126. end;
  2127. procedure TCustomTestModule.HandleScannerError(E: EScannerError);
  2128. begin
  2129. if IsErrorExpected(E) then exit;
  2130. WriteSources(Scanner.CurFilename,Scanner.CurRow,Scanner.CurColumn);
  2131. writeln('ERROR: TCustomTestModule.HandleScannerError '+E.ClassName+':'+E.Message
  2132. +' '+Scanner.CurFilename
  2133. +'('+IntToStr(Scanner.CurRow)+','+IntToStr(Scanner.CurColumn)+')');
  2134. FailException(E);
  2135. end;
  2136. procedure TCustomTestModule.HandleParserError(E: EParserError);
  2137. begin
  2138. if IsErrorExpected(E) then exit;
  2139. WriteSources(E.Filename,E.Row,E.Column);
  2140. writeln('ERROR: TCustomTestModule.HandleParserError '+E.ClassName+':'+E.Message
  2141. +' '+E.Filename+'('+IntToStr(E.Row)+','+IntToStr(E.Column)+')'
  2142. +' MainModuleScannerLine="'+Scanner.CurLine+'"'
  2143. );
  2144. FailException(E);
  2145. end;
  2146. procedure TCustomTestModule.HandlePasResolveError(E: EPasResolve);
  2147. var
  2148. P: TPasSourcePos;
  2149. begin
  2150. if IsErrorExpected(E) then exit;
  2151. P:=E.SourcePos;
  2152. WriteSources(P.FileName,P.Row,P.Column);
  2153. writeln('ERROR: TCustomTestModule.HandlePasResolveError '+E.ClassName+':'+E.Message
  2154. +' '+P.FileName+'('+IntToStr(P.Row)+','+IntToStr(P.Column)+')');
  2155. FailException(E);
  2156. end;
  2157. procedure TCustomTestModule.HandlePas2JSError(E: EPas2JS);
  2158. var
  2159. Row, Col: integer;
  2160. begin
  2161. if IsErrorExpected(E) then exit;
  2162. Engine.UnmangleSourceLineNumber(E.PasElement.SourceLinenumber,Row,Col);
  2163. WriteSources(E.PasElement.SourceFilename,Row,Col);
  2164. writeln('ERROR: TCustomTestModule.HandlePas2JSError '+E.ClassName+':'+E.Message
  2165. +' '+E.PasElement.SourceFilename
  2166. +'('+IntToStr(Row)+','+IntToStr(Col)+')');
  2167. FailException(E);
  2168. end;
  2169. procedure TCustomTestModule.HandleException(E: Exception);
  2170. begin
  2171. if E is EScannerError then
  2172. HandleScannerError(EScannerError(E))
  2173. else if E is EParserError then
  2174. HandleParserError(EParserError(E))
  2175. else if E is EPasResolve then
  2176. HandlePasResolveError(EPasResolve(E))
  2177. else if E is EPas2JS then
  2178. HandlePas2JSError(EPas2JS(E))
  2179. else
  2180. begin
  2181. if IsErrorExpected(E) then exit;
  2182. if not (E is EAssertionFailedError) then
  2183. begin
  2184. WriteSources('',0,0);
  2185. writeln('ERROR: TCustomTestModule.HandleException '+E.ClassName+':'+E.Message);
  2186. end;
  2187. FailException(E);
  2188. end;
  2189. end;
  2190. procedure TCustomTestModule.FailException(E: Exception);
  2191. var
  2192. MsgNumber: Integer;
  2193. begin
  2194. if ExpectedErrorClass<>nil then
  2195. begin
  2196. if FExpectedErrorClass=E.ClassType then
  2197. begin
  2198. if E is EPas2JS then
  2199. MsgNumber:=EPas2JS(E).MsgNumber
  2200. else if E is EPasResolve then
  2201. MsgNumber:=EPasResolve(E).MsgNumber
  2202. else if E is EParserError then
  2203. MsgNumber:=Parser.LastMsgNumber
  2204. else if E is EScannerError then
  2205. MsgNumber:=Scanner.LastMsgNumber
  2206. else
  2207. MsgNumber:=0;
  2208. AssertEquals('Expected error message ('+IntToStr(ExpectedErrorNumber)+')','{'+ExpectedErrorMsg+'}','{'+E.Message+'}');
  2209. AssertEquals('Expected {'+ExpectedErrorMsg+'}, but got msg {'+E.Message+'} number',
  2210. ExpectedErrorNumber,MsgNumber);
  2211. end else begin
  2212. AssertEquals('Wrong exception class',ExpectedErrorClass.ClassName,E.ClassName);
  2213. end;
  2214. end;
  2215. Fail(E.Message);
  2216. end;
  2217. procedure TCustomTestModule.WriteSources(const aFilename: string; aRow,
  2218. aCol: integer);
  2219. var
  2220. IsSrc: Boolean;
  2221. i, j: Integer;
  2222. SrcLines: TStringList;
  2223. Line: string;
  2224. aModule: TTestEnginePasResolver;
  2225. begin
  2226. writeln('TCustomTestModule.WriteSources File="',aFilename,'" Row=',aRow,' Col=',aCol);
  2227. for i:=0 to ResolverCount-1 do
  2228. begin
  2229. aModule:=Resolvers[i];
  2230. SrcLines:=TStringList.Create;
  2231. try
  2232. SrcLines.Text:=aModule.Source;
  2233. IsSrc:=ExtractFilename(aModule.Filename)=ExtractFileName(aFilename);
  2234. writeln('Testcode:-File="',aModule.Filename,'"----------------------------------:');
  2235. for j:=1 to SrcLines.Count do
  2236. begin
  2237. Line:=SrcLines[j-1];
  2238. if IsSrc and (j=aRow) then
  2239. begin
  2240. write('*');
  2241. Line:=LeftStr(Line,aCol-1)+'|'+copy(Line,aCol,length(Line));
  2242. end;
  2243. writeln(Format('%:4d: ',[j]),Line);
  2244. end;
  2245. finally
  2246. SrcLines.Free;
  2247. end;
  2248. end;
  2249. end;
  2250. function TCustomTestModule.IndexOfResolver(const Filename: string): integer;
  2251. var
  2252. i: Integer;
  2253. begin
  2254. for i:=0 to ResolverCount-1 do
  2255. if Filename=Resolvers[i].Filename then exit(i);
  2256. Result:=-1;
  2257. end;
  2258. function TCustomTestModule.GetResolver(const Filename: string
  2259. ): TTestEnginePasResolver;
  2260. var
  2261. i: Integer;
  2262. begin
  2263. i:=IndexOfResolver(Filename);
  2264. if i<0 then exit(nil);
  2265. Result:=Resolvers[i];
  2266. end;
  2267. function TCustomTestModule.GetDefaultNamespace: string;
  2268. var
  2269. C: TClass;
  2270. begin
  2271. Result:='';
  2272. if FModule=nil then exit;
  2273. C:=FModule.ClassType;
  2274. if (C=TPasProgram) or (C=TPasLibrary) or (C=TPasPackage) then
  2275. Result:=Engine.DefaultNameSpace;
  2276. end;
  2277. constructor TCustomTestModule.Create;
  2278. begin
  2279. inherited Create;
  2280. FHintMsgs:=TObjectList.Create(true);
  2281. FHintMsgsGood:=TFPList.Create;
  2282. end;
  2283. destructor TCustomTestModule.Destroy;
  2284. begin
  2285. FreeAndNil(FHintMsgs);
  2286. FreeAndNil(FHintMsgsGood);
  2287. inherited Destroy;
  2288. end;
  2289. { TTestModule }
  2290. procedure TTestModule.TestReservedWords;
  2291. var
  2292. i: integer;
  2293. begin
  2294. for i:=low(JSReservedWords) to High(JSReservedWords)-1 do
  2295. if CompareStr(JSReservedWords[i],JSReservedWords[i+1])>=0 then
  2296. Fail('20170203135442 '+JSReservedWords[i]+' >= '+JSReservedWords[i+1]);
  2297. for i:=low(JSReservedGlobalWords) to High(JSReservedGlobalWords)-1 do
  2298. if CompareStr(JSReservedGlobalWords[i],JSReservedGlobalWords[i+1])>=0 then
  2299. Fail('20170203135443 '+JSReservedGlobalWords[i]+' >= '+JSReservedGlobalWords[i+1]);
  2300. end;
  2301. procedure TTestModule.TestEmptyProgram;
  2302. begin
  2303. StartProgram(false);
  2304. Add('begin');
  2305. ConvertProgram;
  2306. CheckSource('TestEmptyProgram','','');
  2307. end;
  2308. procedure TTestModule.TestEmptyProgramUseStrict;
  2309. begin
  2310. Converter.Options:=Converter.Options+[coUseStrict];
  2311. StartProgram(false);
  2312. Add('begin');
  2313. ConvertProgram;
  2314. CheckSource('TestEmptyProgramUseStrict','','');
  2315. end;
  2316. procedure TTestModule.TestEmptyUnit;
  2317. begin
  2318. StartUnit(false);
  2319. Add('interface');
  2320. Add('implementation');
  2321. ConvertUnit;
  2322. CheckSource('TestEmptyUnit',
  2323. LinesToStr([
  2324. ]),
  2325. '');
  2326. end;
  2327. procedure TTestModule.TestEmptyUnitUseStrict;
  2328. begin
  2329. Converter.Options:=Converter.Options+[coUseStrict];
  2330. StartUnit(false);
  2331. Add('interface');
  2332. Add('implementation');
  2333. ConvertUnit;
  2334. CheckSource('TestEmptyUnitUseStrict',
  2335. LinesToStr([
  2336. ''
  2337. ]),
  2338. '');
  2339. end;
  2340. procedure TTestModule.TestDottedUnitNames;
  2341. begin
  2342. AddModuleWithIntfImplSrc('NS1.Unit2.pas',
  2343. LinesToStr([
  2344. 'var iV: longint;'
  2345. ]),
  2346. '');
  2347. FFilename:='ns1.test1.pp';
  2348. StartProgram(true);
  2349. Add('uses unIt2;');
  2350. Add('var');
  2351. Add(' i: longint;');
  2352. Add('begin');
  2353. Add(' i:=iv;');
  2354. Add(' i:=uNit2.iv;');
  2355. Add(' i:=Ns1.TEst1.i;');
  2356. ConvertProgram;
  2357. CheckSource('TestDottedUnitNames',
  2358. LinesToStr([
  2359. 'this.i = 0;',
  2360. '']),
  2361. LinesToStr([ // this.$init
  2362. '$mod.i = pas["NS1.Unit2"].iV;',
  2363. '$mod.i = pas["NS1.Unit2"].iV;',
  2364. '$mod.i = $mod.i;',
  2365. '']) );
  2366. end;
  2367. procedure TTestModule.TestDottedUnitNameImpl;
  2368. begin
  2369. AddModuleWithIntfImplSrc('TEST.UnitA.pas',
  2370. LinesToStr([
  2371. 'type',
  2372. ' TObject = class end;',
  2373. ' TTestA = class',
  2374. ' end;'
  2375. ]),
  2376. LinesToStr(['uses TEST.UnitB;'])
  2377. );
  2378. AddModuleWithIntfImplSrc('TEST.UnitB.pas',
  2379. LinesToStr([
  2380. 'uses TEST.UnitA;',
  2381. 'type TTestB = class(TTestA);'
  2382. ]),
  2383. ''
  2384. );
  2385. StartProgram(true);
  2386. Add('uses TEST.UnitA;');
  2387. Add('begin');
  2388. ConvertProgram;
  2389. CheckSource('TestDottedUnitNameImpl',
  2390. LinesToStr([
  2391. '']),
  2392. LinesToStr([ // this.$init
  2393. '']) );
  2394. CheckUnit('TEST.UnitA.pas',
  2395. LinesToStr([
  2396. 'rtl.module("TEST.UnitA", ["system"], function () {',
  2397. ' var $mod = this;',
  2398. ' rtl.createClass(this, "TObject", null, function () {',
  2399. ' this.$init = function () {',
  2400. ' };',
  2401. ' this.$final = function () {',
  2402. ' };',
  2403. ' });',
  2404. ' rtl.createClass(this, "TTestA", this.TObject, function () {',
  2405. ' });',
  2406. '}, ["TEST.UnitB"]);'
  2407. ]));
  2408. CheckUnit('TEST.UnitB.pas',
  2409. LinesToStr([
  2410. 'rtl.module("TEST.UnitB", ["system","TEST.UnitA"], function () {',
  2411. ' var $mod = this;',
  2412. ' rtl.createClass(this, "TTestB", pas["TEST.UnitA"].TTestA, function () {',
  2413. ' });',
  2414. '});'
  2415. ]));
  2416. end;
  2417. procedure TTestModule.TestDottedUnitExpr;
  2418. begin
  2419. AddModuleWithIntfImplSrc('NS2.SubNs2.Unit2.pas',
  2420. LinesToStr([
  2421. 'procedure DoIt;'
  2422. ]),
  2423. 'procedure DoIt; begin end;');
  2424. FFilename:='Ns1.SubNs1.Test1.pp';
  2425. StartProgram(true);
  2426. Add('uses Ns2.sUbnS2.unIt2;');
  2427. Add('var');
  2428. Add(' i: longint;');
  2429. Add('begin');
  2430. Add(' ns2.subns2.unit2.doit;');
  2431. Add(' i:=Ns1.SubNS1.TEst1.i;');
  2432. ConvertProgram;
  2433. CheckSource('TestDottedUnitExpr',
  2434. LinesToStr([
  2435. 'this.i = 0;',
  2436. '']),
  2437. LinesToStr([ // this.$init
  2438. 'pas["NS2.SubNs2.Unit2"].DoIt();',
  2439. '$mod.i = $mod.i;',
  2440. '']) );
  2441. end;
  2442. procedure TTestModule.Test_ModeFPCFail;
  2443. begin
  2444. StartProgram(false);
  2445. Add('{$mode FPC}');
  2446. Add('begin');
  2447. SetExpectedScannerError('Invalid mode: "FPC"',nErrInvalidMode);
  2448. ConvertProgram;
  2449. end;
  2450. procedure TTestModule.Test_ModeSwitchCBlocksFail;
  2451. begin
  2452. StartProgram(false);
  2453. Add('{$modeswitch cblocks-}');
  2454. Add('begin');
  2455. ConvertProgram;
  2456. CheckHint(mtWarning,nErrInvalidModeSwitch,'Warning: test1.pp(3,23) : Invalid mode switch: "cblocks"');
  2457. CheckResolverUnexpectedHints();
  2458. end;
  2459. procedure TTestModule.TestUnit_UseSystem;
  2460. begin
  2461. StartUnit(true);
  2462. Add([
  2463. 'interface',
  2464. 'var i: integer;',
  2465. 'implementation']);
  2466. ConvertUnit;
  2467. CheckSource('TestUnit_UseSystem',
  2468. LinesToStr([
  2469. 'this.i = 0;',
  2470. '']),
  2471. LinesToStr([
  2472. '']) );
  2473. end;
  2474. procedure TTestModule.TestUnit_Intf1Impl2Intf1;
  2475. begin
  2476. AddModuleWithIntfImplSrc('unit1.pp',
  2477. LinesToStr([
  2478. 'type number = longint;']),
  2479. LinesToStr([
  2480. 'uses test1;',
  2481. 'procedure DoIt;',
  2482. 'begin',
  2483. ' i:=3;',
  2484. 'end;']));
  2485. StartUnit(true);
  2486. Add([
  2487. 'interface',
  2488. 'uses unit1;',
  2489. 'var i: number;',
  2490. 'implementation']);
  2491. ConvertUnit;
  2492. CheckSource('TestUnit_Intf1Impl2Intf1',
  2493. LinesToStr([
  2494. 'this.i = 0;',
  2495. '']),
  2496. LinesToStr([
  2497. '']) );
  2498. end;
  2499. procedure TTestModule.TestIncludeVersion;
  2500. begin
  2501. StartProgram(false);
  2502. Add([
  2503. 'var',
  2504. ' s: string;',
  2505. ' i: word;',
  2506. 'begin',
  2507. ' s:={$I %line%};',
  2508. ' i:={$I %linenum%};',
  2509. ' s:={$I %currentroutine%};',
  2510. ' s:={$I %pas2jsversion%};',
  2511. ' s:={$I %pas2jstarget%};',
  2512. ' s:={$I %pas2jstargetos%};',
  2513. ' s:={$I %pas2jstargetcpu%};',
  2514. ' s:={$I %file%};',
  2515. '']);
  2516. ConvertProgram;
  2517. CheckSource('TestIncludeVersion',
  2518. LinesToStr([
  2519. 'this.s="";',
  2520. 'this.i = 0;']),
  2521. LinesToStr([
  2522. '$mod.s = "7";',
  2523. '$mod.i = 8;',
  2524. '$mod.s = "<anonymous>";',
  2525. '$mod.s = "Comp.Ver.tcmodules";',
  2526. '$mod.s = "Browser";',
  2527. '$mod.s = "Browser";',
  2528. '$mod.s = "ECMAScript5";',
  2529. '$mod.s = "test1.pp";',
  2530. '']));
  2531. end;
  2532. procedure TTestModule.TestVarInt;
  2533. begin
  2534. StartProgram(false);
  2535. Add('var MyI: longint;');
  2536. Add('begin');
  2537. ConvertProgram;
  2538. CheckSource('TestVarInt','this.MyI=0;','');
  2539. end;
  2540. procedure TTestModule.TestVarBaseTypes;
  2541. begin
  2542. StartProgram(false);
  2543. Add('var');
  2544. Add(' i: longint;');
  2545. Add(' s: string;');
  2546. Add(' c: char;');
  2547. Add(' b: boolean;');
  2548. Add(' d: double;');
  2549. Add(' i2: longint = 3;');
  2550. Add(' s2: string = ''foo'';');
  2551. Add(' c2: char = ''4'';');
  2552. Add(' b2: boolean = true;');
  2553. Add(' d2: double = 5.6;');
  2554. Add(' i3: longint = $707;');
  2555. Add(' i4: nativeint = 9007199254740991;');
  2556. Add(' i5: nativeint = -9007199254740991-1;');
  2557. Add(' i6: nativeint = $fffffffffffff;');
  2558. Add(' i7: nativeint = -$fffffffffffff-1;');
  2559. Add(' i8: byte = 00;');
  2560. Add(' u8: nativeuint = $fffffffffffff;');
  2561. Add(' u9: nativeuint = $0000000000000;');
  2562. Add(' u10: nativeuint = $00ff00;');
  2563. Add('begin');
  2564. ConvertProgram;
  2565. CheckSource('TestVarBaseTypes',
  2566. LinesToStr([
  2567. 'this.i = 0;',
  2568. 'this.s = "";',
  2569. 'this.c = "";',
  2570. 'this.b = false;',
  2571. 'this.d = 0.0;',
  2572. 'this.i2 = 3;',
  2573. 'this.s2 = "foo";',
  2574. 'this.c2 = "4";',
  2575. 'this.b2 = true;',
  2576. 'this.d2 = 5.6;',
  2577. 'this.i3 = 0x707;',
  2578. 'this.i4 = 9007199254740991;',
  2579. 'this.i5 = -9007199254740991-1;',
  2580. 'this.i6 = 0xfffffffffffff;',
  2581. 'this.i7 =-0xfffffffffffff-1;',
  2582. 'this.i8 = 0;',
  2583. 'this.u8 = 0xfffffffffffff;',
  2584. 'this.u9 = 0x0;',
  2585. 'this.u10 = 0xff00;'
  2586. ]),
  2587. '');
  2588. end;
  2589. procedure TTestModule.TestBaseTypeSingleFail;
  2590. begin
  2591. StartProgram(false);
  2592. Add('var s: single;');
  2593. SetExpectedPasResolverError('identifier not found "single"',PasResolveEval.nIdentifierNotFound);
  2594. ConvertProgram;
  2595. end;
  2596. procedure TTestModule.TestBaseTypeExtendedFail;
  2597. begin
  2598. StartProgram(false);
  2599. Add('var e: extended;');
  2600. SetExpectedPasResolverError('identifier not found "extended"',PasResolveEval.nIdentifierNotFound);
  2601. ConvertProgram;
  2602. end;
  2603. procedure TTestModule.TestConstBaseTypes;
  2604. begin
  2605. StartProgram(false);
  2606. Add('const');
  2607. Add(' i: longint = 3;');
  2608. Add(' s: string = ''foo'';');
  2609. Add(' c: char = ''4'';');
  2610. Add(' b: boolean = true;');
  2611. Add(' d: double = 5.6;');
  2612. Add(' e = low(word);');
  2613. Add(' f = high(word);');
  2614. Add('begin');
  2615. ConvertProgram;
  2616. CheckSource('TestVarBaseTypes',
  2617. LinesToStr([
  2618. 'this.i=3;',
  2619. 'this.s="foo";',
  2620. 'this.c="4";',
  2621. 'this.b=true;',
  2622. 'this.d=5.6;',
  2623. 'this.e = 0;',
  2624. 'this.f = 65535;'
  2625. ]),
  2626. '');
  2627. end;
  2628. procedure TTestModule.TestAliasTypeRef;
  2629. begin
  2630. StartProgram(false);
  2631. Add('type');
  2632. Add(' a=longint;');
  2633. Add(' b=a;');
  2634. Add('var');
  2635. Add(' c: A;');
  2636. Add(' d: B;');
  2637. Add('begin');
  2638. ConvertProgram;
  2639. CheckSource('TestAliasTypeRef',
  2640. LinesToStr([ // statements
  2641. 'this.c = 0;',
  2642. 'this.d = 0;'
  2643. ]),
  2644. LinesToStr([ // this.$main
  2645. ''
  2646. ]));
  2647. end;
  2648. procedure TTestModule.TestTypeCast_BaseTypes;
  2649. begin
  2650. StartProgram(false);
  2651. Add([
  2652. 'var',
  2653. ' i: longint;',
  2654. ' b: boolean;',
  2655. ' d: double;',
  2656. ' s: string;',
  2657. ' c: char;',
  2658. 'begin',
  2659. ' i:=longint(i);',
  2660. ' i:=longint(b);',
  2661. ' b:=boolean(b);',
  2662. ' b:=boolean(i);',
  2663. ' d:=double(d);',
  2664. ' d:=double(i);',
  2665. ' s:=string(s);',
  2666. ' s:=string(c);',
  2667. ' c:=char(c);',
  2668. ' c:=char(i);',
  2669. ' c:=char(65);',
  2670. ' c:=char(#10);',
  2671. ' c:=char(#$E000);',
  2672. '']);
  2673. ConvertProgram;
  2674. CheckSource('TestAliasTypeRef',
  2675. LinesToStr([ // statements
  2676. 'this.i = 0;',
  2677. 'this.b = false;',
  2678. 'this.d = 0.0;',
  2679. 'this.s = "";',
  2680. 'this.c = "";',
  2681. '']),
  2682. LinesToStr([ // this.$main
  2683. '$mod.i = $mod.i;',
  2684. '$mod.i = ($mod.b ? 1 : 0);',
  2685. '$mod.b = $mod.b;',
  2686. '$mod.b = $mod.i != 0;',
  2687. '$mod.d = $mod.d;',
  2688. '$mod.d = $mod.i;',
  2689. '$mod.s = $mod.s;',
  2690. '$mod.s = $mod.c;',
  2691. '$mod.c = $mod.c;',
  2692. '$mod.c = String.fromCharCode($mod.i);',
  2693. '$mod.c = "A";',
  2694. '$mod.c = "\n";',
  2695. '$mod.c = "";',
  2696. '']));
  2697. end;
  2698. procedure TTestModule.TestTypeCast_AliasBaseTypes;
  2699. begin
  2700. StartProgram(false);
  2701. Add('type');
  2702. Add(' integer = longint;');
  2703. Add(' TYesNo = boolean;');
  2704. Add(' TFloat = double;');
  2705. Add(' TCaption = string;');
  2706. Add(' TChar = char;');
  2707. Add('var');
  2708. Add(' i: integer;');
  2709. Add(' b: TYesNo;');
  2710. Add(' d: TFloat;');
  2711. Add(' s: TCaption;');
  2712. Add(' c: TChar;');
  2713. Add('begin');
  2714. Add(' i:=integer(i);');
  2715. Add(' i:=integer(b);');
  2716. Add(' b:=TYesNo(b);');
  2717. Add(' b:=TYesNo(i);');
  2718. Add(' d:=TFloat(d);');
  2719. Add(' d:=TFloat(i);');
  2720. Add(' s:=TCaption(s);');
  2721. Add(' s:=TCaption(c);');
  2722. Add(' c:=TChar(c);');
  2723. ConvertProgram;
  2724. CheckSource('TestAliasTypeRef',
  2725. LinesToStr([ // statements
  2726. 'this.i = 0;',
  2727. 'this.b = false;',
  2728. 'this.d = 0.0;',
  2729. 'this.s = "";',
  2730. 'this.c = "";',
  2731. '']),
  2732. LinesToStr([ // this.$main
  2733. '$mod.i = $mod.i;',
  2734. '$mod.i = ($mod.b ? 1 : 0);',
  2735. '$mod.b = $mod.b;',
  2736. '$mod.b = $mod.i != 0;',
  2737. '$mod.d = $mod.d;',
  2738. '$mod.d = $mod.i;',
  2739. '$mod.s = $mod.s;',
  2740. '$mod.s = $mod.c;',
  2741. '$mod.c = $mod.c;',
  2742. '']));
  2743. end;
  2744. procedure TTestModule.TestEmptyProc;
  2745. begin
  2746. StartProgram(false);
  2747. Add('procedure Test;');
  2748. Add('begin');
  2749. Add('end;');
  2750. Add('begin');
  2751. ConvertProgram;
  2752. CheckSource('TestEmptyProc',
  2753. LinesToStr([ // statements
  2754. 'this.Test = function () {',
  2755. '};'
  2756. ]),
  2757. LinesToStr([ // this.$main
  2758. ''
  2759. ]));
  2760. end;
  2761. procedure TTestModule.TestProcOneParam;
  2762. begin
  2763. StartProgram(false);
  2764. Add('procedure ProcA(i: longint);');
  2765. Add('begin');
  2766. Add('end;');
  2767. Add('begin');
  2768. Add(' PROCA(3);');
  2769. ConvertProgram;
  2770. CheckSource('TestProcOneParam',
  2771. LinesToStr([ // statements
  2772. 'this.ProcA = function (i) {',
  2773. '};'
  2774. ]),
  2775. LinesToStr([ // this.$main
  2776. '$mod.ProcA(3);'
  2777. ]));
  2778. end;
  2779. procedure TTestModule.TestFunctionWithoutParams;
  2780. begin
  2781. StartProgram(false);
  2782. Add('function FuncA: longint;');
  2783. Add('begin');
  2784. Add('end;');
  2785. Add('var i: longint;');
  2786. Add('begin');
  2787. Add(' I:=FUNCA();');
  2788. Add(' I:=FUNCA;');
  2789. Add(' FUNCA();');
  2790. Add(' FUNCA;');
  2791. ConvertProgram;
  2792. CheckSource('TestProcWithoutParams',
  2793. LinesToStr([ // statements
  2794. 'this.FuncA = function () {',
  2795. ' var Result = 0;',
  2796. ' return Result;',
  2797. '};',
  2798. 'this.i=0;'
  2799. ]),
  2800. LinesToStr([ // this.$main
  2801. '$mod.i=$mod.FuncA();',
  2802. '$mod.i=$mod.FuncA();',
  2803. '$mod.FuncA();',
  2804. '$mod.FuncA();'
  2805. ]));
  2806. end;
  2807. procedure TTestModule.TestProcedureWithoutParams;
  2808. begin
  2809. StartProgram(false);
  2810. Add('procedure ProcA;');
  2811. Add('begin');
  2812. Add('end;');
  2813. Add('begin');
  2814. Add(' PROCA();');
  2815. Add(' PROCA;');
  2816. ConvertProgram;
  2817. CheckSource('TestProcWithoutParams',
  2818. LinesToStr([ // statements
  2819. 'this.ProcA = function () {',
  2820. '};'
  2821. ]),
  2822. LinesToStr([ // this.$main
  2823. '$mod.ProcA();',
  2824. '$mod.ProcA();'
  2825. ]));
  2826. end;
  2827. procedure TTestModule.TestIncDec;
  2828. begin
  2829. StartProgram(false);
  2830. Add([
  2831. 'procedure DoIt(var i: longint);',
  2832. 'begin',
  2833. ' inc(i);',
  2834. ' inc(i,2);',
  2835. 'end;',
  2836. 'var',
  2837. ' Bar: longint;',
  2838. 'begin',
  2839. ' inc(bar);',
  2840. ' inc(bar,2);',
  2841. ' dec(bar);',
  2842. ' dec(bar,3);',
  2843. '']);
  2844. ConvertProgram;
  2845. CheckSource('TestIncDec',
  2846. LinesToStr([ // statements
  2847. 'this.DoIt = function (i) {',
  2848. ' i.set(i.get()+1);',
  2849. ' i.set(i.get()+2);',
  2850. '};',
  2851. 'this.Bar = 0;'
  2852. ]),
  2853. LinesToStr([ // this.$main
  2854. '$mod.Bar+=1;',
  2855. '$mod.Bar+=2;',
  2856. '$mod.Bar-=1;',
  2857. '$mod.Bar-=3;'
  2858. ]));
  2859. end;
  2860. procedure TTestModule.TestLoHiFpcMode;
  2861. begin
  2862. StartProgram(false);
  2863. Add([
  2864. '{$mode objfpc}',
  2865. 'const',
  2866. ' LoByte1 = Lo(Word($1234));',
  2867. ' HiByte1 = Hi(Word($1234));',
  2868. ' LoByte2 = Lo(SmallInt($1234));',
  2869. ' HiByte2 = Hi(SmallInt($1234));',
  2870. ' LoWord1 = Lo($1234CDEF);',
  2871. ' HiWord1 = Hi($1234CDEF);',
  2872. ' LoWord2 = Lo(-$1234CDEF);',
  2873. ' HiWord2 = Hi(-$1234CDEF);',
  2874. ' lo4:byte=lo(byte($34));',
  2875. ' hi4:byte=hi(byte($34));',
  2876. ' lo5:byte=lo(shortint(-$34));',
  2877. ' hi5:byte=hi(shortint(-$34));',
  2878. ' lo6:longword=lo($123456789ABCD);',
  2879. ' hi6:longword=hi($123456789ABCD);',
  2880. ' lo7:longword=lo(-$123456789ABCD);',
  2881. ' hi7:longword=hi(-$123456789ABCD);',
  2882. 'var',
  2883. ' b: Byte;',
  2884. ' ss: shortint;',
  2885. ' w: Word;',
  2886. ' si: SmallInt;',
  2887. ' lw: LongWord;',
  2888. ' li: LongInt;',
  2889. ' b2: Byte;',
  2890. ' ni: nativeint;',
  2891. 'begin',
  2892. ' w := $1234;',
  2893. ' ss := -$12;',
  2894. ' b := lo(ss);',
  2895. ' b := HI(ss);',
  2896. ' b := lo(w);',
  2897. ' b := HI(w);',
  2898. ' b2 := lo(b);',
  2899. ' b2 := hi(b);',
  2900. ' lw := $1234CDEF;',
  2901. ' w := lo(lw);',
  2902. ' w := hi(lw);',
  2903. ' ni := $123456789ABCD;',
  2904. ' lw := lo(ni);',
  2905. ' lw := hi(ni);',
  2906. '']);
  2907. ConvertProgram;
  2908. CheckSource('TestLoHiFpcMode',
  2909. LinesToStr([ // statements
  2910. 'this.LoByte1 = 0x1234 & 0xFF;',
  2911. 'this.HiByte1 = (0x1234 >> 8) & 0xFF;',
  2912. 'this.LoByte2 = 0x1234 & 0xFF;',
  2913. 'this.HiByte2 = (0x1234 >> 8) & 0xFF;',
  2914. 'this.LoWord1 = 0x1234CDEF & 0xFFFF;',
  2915. 'this.HiWord1 = (0x1234CDEF >> 16) & 0xFFFF;',
  2916. 'this.LoWord2 = -0x1234CDEF & 0xFFFF;',
  2917. 'this.HiWord2 = (-0x1234CDEF >> 16) & 0xFFFF;',
  2918. 'this.lo4 = 0x34 & 0xF;',
  2919. 'this.hi4 = (0x34 >> 4) & 0xF;',
  2920. 'this.lo5 = (((-0x34 & 255) << 24) >> 24) & 0xFF;',
  2921. 'this.hi5 = ((((-0x34 & 255) << 24) >> 24) >> 8) & 0xFF;',
  2922. 'this.lo6 = 0x123456789ABCD >>> 0;',
  2923. 'this.hi6 = 74565 >>> 0;',
  2924. 'this.lo7 = -0x123456789ABCD >>> 0;',
  2925. 'this.hi7 = Math.floor(-0x123456789ABCD / 4294967296) >>> 0;',
  2926. 'this.b = 0;',
  2927. 'this.ss = 0;',
  2928. 'this.w = 0;',
  2929. 'this.si = 0;',
  2930. 'this.lw = 0;',
  2931. 'this.li = 0;',
  2932. 'this.b2 = 0;',
  2933. 'this.ni = 0;',
  2934. '']),
  2935. LinesToStr([ // this.$main
  2936. '$mod.w = 0x1234;',
  2937. '$mod.ss = -0x12;',
  2938. '$mod.b = $mod.ss & 0xFF;',
  2939. '$mod.b = ($mod.ss >> 8) & 0xFF;',
  2940. '$mod.b = $mod.w & 0xFF;',
  2941. '$mod.b = ($mod.w >> 8) & 0xFF;',
  2942. '$mod.b2 = $mod.b & 0xF;',
  2943. '$mod.b2 = ($mod.b >> 4) & 0xF;',
  2944. '$mod.lw = 0x1234CDEF;',
  2945. '$mod.w = $mod.lw & 0xFFFF;',
  2946. '$mod.w = ($mod.lw >> 16) & 0xFFFF;',
  2947. '$mod.ni = 0x123456789ABCD;',
  2948. '$mod.lw = $mod.ni >>> 0;',
  2949. '$mod.lw = Math.floor($mod.ni / 4294967296) >>> 0;',
  2950. '']));
  2951. end;
  2952. procedure TTestModule.TestLoHiDelphiMode;
  2953. begin
  2954. StartProgram(false);
  2955. Add([
  2956. '{$mode delphi}',
  2957. 'const',
  2958. ' LoByte1 = Lo(Word($1234));',
  2959. ' HiByte1 = Hi(Word($1234));',
  2960. ' LoByte2 = Lo(SmallInt($1234));',
  2961. ' HiByte2 = Hi(SmallInt($1234));',
  2962. ' LoByte3 = Lo($1234CDEF);',
  2963. ' HiByte3 = Hi($1234CDEF);',
  2964. ' LoByte4 = Lo(-$1234CDEF);',
  2965. ' HiByte4 = Hi(-$1234CDEF);',
  2966. 'var',
  2967. ' b: Byte;',
  2968. ' w: Word;',
  2969. ' si: SmallInt;',
  2970. ' lw: LongWord;',
  2971. ' li: LongInt;',
  2972. 'begin',
  2973. ' w := $1234;',
  2974. ' b := lo(w);',
  2975. ' b := HI(w);',
  2976. ' lw := $1234CDEF;',
  2977. ' b := lo(lw);',
  2978. ' b := hi(lw);',
  2979. '']);
  2980. ConvertProgram;
  2981. CheckSource('TestLoHiDelphiMode',
  2982. LinesToStr([ // statements
  2983. 'this.LoByte1 = 0x1234 & 0xFF;',
  2984. 'this.HiByte1 = (0x1234 >> 8) & 0xFF;',
  2985. 'this.LoByte2 = 0x1234 & 0xFF;',
  2986. 'this.HiByte2 = (0x1234 >> 8) & 0xFF;',
  2987. 'this.LoByte3 = 0x1234CDEF & 0xFF;',
  2988. 'this.HiByte3 = (0x1234CDEF >> 8) & 0xFF;',
  2989. 'this.LoByte4 = -0x1234CDEF & 0xFF;',
  2990. 'this.HiByte4 = (-0x1234CDEF >> 8) & 0xFF;',
  2991. 'this.b = 0;',
  2992. 'this.w = 0;',
  2993. 'this.si = 0;',
  2994. 'this.lw = 0;',
  2995. 'this.li = 0;'
  2996. ]),
  2997. LinesToStr([ // this.$main
  2998. '$mod.w = 0x1234;',
  2999. '$mod.b = $mod.w & 0xFF;',
  3000. '$mod.b = ($mod.w >> 8) & 0xFF;',
  3001. '$mod.lw = 0x1234CDEF;',
  3002. '$mod.b = $mod.lw & 0xFF;',
  3003. '$mod.b = ($mod.lw >> 8) & 0xFF;'
  3004. ]));
  3005. end;
  3006. procedure TTestModule.TestAssignments;
  3007. begin
  3008. StartProgram(false);
  3009. Parser.Options:=Parser.Options+[po_cassignments];
  3010. Add('var');
  3011. Add(' Bar:longint;');
  3012. Add('begin');
  3013. Add(' bar:=3;');
  3014. Add(' bar+=4;');
  3015. Add(' bar-=5;');
  3016. Add(' bar*=6;');
  3017. ConvertProgram;
  3018. CheckSource('TestAssignments',
  3019. LinesToStr([ // statements
  3020. 'this.Bar = 0;'
  3021. ]),
  3022. LinesToStr([ // this.$main
  3023. '$mod.Bar=3;',
  3024. '$mod.Bar+=4;',
  3025. '$mod.Bar-=5;',
  3026. '$mod.Bar*=6;'
  3027. ]));
  3028. end;
  3029. procedure TTestModule.TestArithmeticOperators1;
  3030. begin
  3031. StartProgram(false);
  3032. Add('var');
  3033. Add(' vA,vB,vC:longint;');
  3034. Add('begin');
  3035. Add(' va:=1;');
  3036. Add(' vb:=va+va;');
  3037. Add(' vb:=va div vb;');
  3038. Add(' vb:=va mod vb;');
  3039. Add(' vb:=va+va*vb+va div vb;');
  3040. Add(' vc:=-va;');
  3041. Add(' va:=va-vb;');
  3042. Add(' vb:=va;');
  3043. Add(' if va<vb then vc:=va else vc:=vb;');
  3044. ConvertProgram;
  3045. CheckSource('TestArithmeticOperators1',
  3046. LinesToStr([ // statements
  3047. 'this.vA = 0;',
  3048. 'this.vB = 0;',
  3049. 'this.vC = 0;'
  3050. ]),
  3051. LinesToStr([ // this.$main
  3052. '$mod.vA = 1;',
  3053. '$mod.vB = $mod.vA + $mod.vA;',
  3054. '$mod.vB = rtl.trunc($mod.vA / $mod.vB);',
  3055. '$mod.vB = $mod.vA % $mod.vB;',
  3056. '$mod.vB = $mod.vA + ($mod.vA * $mod.vB) + rtl.trunc($mod.vA / $mod.vB);',
  3057. '$mod.vC = -$mod.vA;',
  3058. '$mod.vA = $mod.vA - $mod.vB;',
  3059. '$mod.vB = $mod.vA;',
  3060. 'if ($mod.vA < $mod.vB){ $mod.vC = $mod.vA } else $mod.vC = $mod.vB;'
  3061. ]));
  3062. end;
  3063. procedure TTestModule.TestLogicalOperators;
  3064. begin
  3065. StartProgram(false);
  3066. Add('var');
  3067. Add(' vA,vB,vC:boolean;');
  3068. Add('begin');
  3069. Add(' va:=vb and vc;');
  3070. Add(' va:=vb or vc;');
  3071. Add(' va:=vb xor vc;');
  3072. Add(' va:=true and vc;');
  3073. Add(' va:=(vb and vc) or (va and vb);');
  3074. Add(' va:=not vb;');
  3075. ConvertProgram;
  3076. CheckSource('TestLogicalOperators',
  3077. LinesToStr([ // statements
  3078. 'this.vA = false;',
  3079. 'this.vB = false;',
  3080. 'this.vC = false;'
  3081. ]),
  3082. LinesToStr([ // this.$main
  3083. '$mod.vA = $mod.vB && $mod.vC;',
  3084. '$mod.vA = $mod.vB || $mod.vC;',
  3085. '$mod.vA = $mod.vB ^ $mod.vC;',
  3086. '$mod.vA = true && $mod.vC;',
  3087. '$mod.vA = ($mod.vB && $mod.vC) || ($mod.vA && $mod.vB);',
  3088. '$mod.vA = !$mod.vB;'
  3089. ]));
  3090. end;
  3091. procedure TTestModule.TestBitwiseOperators;
  3092. begin
  3093. StartProgram(false);
  3094. Add([
  3095. 'var',
  3096. ' vA,vB,vC:longint;',
  3097. ' X,Y,Z: nativeint;',
  3098. 'begin',
  3099. ' va:=vb and vc;',
  3100. ' va:=vb or vc;',
  3101. ' va:=vb xor vc;',
  3102. ' va:=vb shl vc;',
  3103. ' va:=vb shr vc;',
  3104. ' va:=3 and vc;',
  3105. ' va:=(vb and vc) or (va and vb);',
  3106. ' va:=not vb;',
  3107. ' X:=Y and Z;',
  3108. ' X:=Y and va;',
  3109. ' X:=Y or Z;',
  3110. ' X:=Y or va;',
  3111. ' X:=Y xor Z;',
  3112. ' X:=Y xor va;',
  3113. '']);
  3114. ConvertProgram;
  3115. CheckSource('TestBitwiseOperators',
  3116. LinesToStr([ // statements
  3117. 'this.vA = 0;',
  3118. 'this.vB = 0;',
  3119. 'this.vC = 0;',
  3120. 'this.X = 0;',
  3121. 'this.Y = 0;',
  3122. 'this.Z = 0;',
  3123. '']),
  3124. LinesToStr([ // this.$main
  3125. '$mod.vA = $mod.vB & $mod.vC;',
  3126. '$mod.vA = $mod.vB | $mod.vC;',
  3127. '$mod.vA = $mod.vB ^ $mod.vC;',
  3128. '$mod.vA = $mod.vB << $mod.vC;',
  3129. '$mod.vA = $mod.vB >>> $mod.vC;',
  3130. '$mod.vA = 3 & $mod.vC;',
  3131. '$mod.vA = ($mod.vB & $mod.vC) | ($mod.vA & $mod.vB);',
  3132. '$mod.vA = ~$mod.vB;',
  3133. '$mod.X = rtl.and($mod.Y, $mod.Z);',
  3134. '$mod.X = $mod.Y & $mod.vA;',
  3135. '$mod.X = rtl.or($mod.Y, $mod.Z);',
  3136. '$mod.X = rtl.or($mod.Y, $mod.vA);',
  3137. '$mod.X = rtl.xor($mod.Y, $mod.Z);',
  3138. '$mod.X = rtl.xor($mod.Y, $mod.vA);',
  3139. '']));
  3140. end;
  3141. procedure TTestModule.TestBitwiseOperatorsLongword;
  3142. begin
  3143. StartProgram(false);
  3144. Add([
  3145. 'var',
  3146. ' a,b,c:longword;',
  3147. ' i: longint;',
  3148. 'begin',
  3149. ' a:=$12345678;',
  3150. ' b:=$EDCBA987;',
  3151. ' c:=not a;',
  3152. ' c:=a and b;',
  3153. ' c:=a and $ffff0000;',
  3154. ' c:=a or b;',
  3155. ' c:=a or $ff00ff00;',
  3156. ' c:=a xor b;',
  3157. ' c:=a xor $f0f0f0f0;',
  3158. ' c:=a shl 1;',
  3159. ' c:=a shl 16;',
  3160. ' c:=a shl 24;',
  3161. ' c:=a shl b;',
  3162. ' c:=a shr 1;',
  3163. ' c:=a shr 16;',
  3164. ' c:=a shr 24;',
  3165. ' c:=a shr b;',
  3166. ' c:=(b and c) or (a and b);',
  3167. ' c:=i and a;',
  3168. ' c:=i or a;',
  3169. ' c:=i xor a;',
  3170. '']);
  3171. ConvertProgram;
  3172. CheckSource('TestBitwiseOperatorsLongword',
  3173. LinesToStr([ // statements
  3174. 'this.a = 0;',
  3175. 'this.b = 0;',
  3176. 'this.c = 0;',
  3177. 'this.i = 0;',
  3178. '']),
  3179. LinesToStr([ // this.$main
  3180. '$mod.a = 0x12345678;',
  3181. '$mod.b = 0xEDCBA987;',
  3182. '$mod.c = rtl.lw(~$mod.a);',
  3183. '$mod.c = rtl.lw($mod.a & $mod.b);',
  3184. '$mod.c = rtl.lw($mod.a & 0xffff0000);',
  3185. '$mod.c = rtl.lw($mod.a | $mod.b);',
  3186. '$mod.c = rtl.lw($mod.a | 0xff00ff00);',
  3187. '$mod.c = rtl.lw($mod.a ^ $mod.b);',
  3188. '$mod.c = rtl.lw($mod.a ^ 0xf0f0f0f0);',
  3189. '$mod.c = rtl.lw($mod.a << 1);',
  3190. '$mod.c = rtl.lw($mod.a << 16);',
  3191. '$mod.c = rtl.lw($mod.a << 24);',
  3192. '$mod.c = rtl.lw($mod.a << $mod.b);',
  3193. '$mod.c = rtl.lw($mod.a >>> 1);',
  3194. '$mod.c = rtl.lw($mod.a >>> 16);',
  3195. '$mod.c = rtl.lw($mod.a >>> 24);',
  3196. '$mod.c = rtl.lw($mod.a >>> $mod.b);',
  3197. '$mod.c = rtl.lw(rtl.lw($mod.b & $mod.c) | rtl.lw($mod.a & $mod.b));',
  3198. '$mod.c = $mod.i & $mod.a;',
  3199. '$mod.c = $mod.i | $mod.a;',
  3200. '$mod.c = $mod.i ^ $mod.a;',
  3201. '']));
  3202. end;
  3203. procedure TTestModule.TestPrgProcVar;
  3204. begin
  3205. StartProgram(false);
  3206. Add('procedure Proc1;');
  3207. Add('type');
  3208. Add(' t1=longint;');
  3209. Add('var');
  3210. Add(' vA:t1;');
  3211. Add('begin');
  3212. Add('end;');
  3213. Add('begin');
  3214. ConvertProgram;
  3215. CheckSource('TestPrgProcVar',
  3216. LinesToStr([ // statements
  3217. 'this.Proc1 = function () {',
  3218. ' var vA=0;',
  3219. '};'
  3220. ]),
  3221. LinesToStr([ // this.$main
  3222. ''
  3223. ]));
  3224. end;
  3225. procedure TTestModule.TestUnitProcVar;
  3226. begin
  3227. StartUnit(false);
  3228. Add('interface');
  3229. Add('');
  3230. Add('type tA=string; // unit scope');
  3231. Add('procedure Proc1;');
  3232. Add('');
  3233. Add('implementation');
  3234. Add('');
  3235. Add('procedure Proc1;');
  3236. Add('type tA=longint; // local proc scope');
  3237. Add('var v1:tA; // using local tA');
  3238. Add('begin');
  3239. Add('end;');
  3240. Add('var v2:tA; // using interface tA');
  3241. ConvertUnit;
  3242. CheckSource('TestUnitProcVar',
  3243. LinesToStr([ // statements
  3244. 'var $impl = $mod.$impl;',
  3245. 'this.Proc1 = function () {',
  3246. ' var v1 = 0;',
  3247. '};',
  3248. '']),
  3249. // this.$init
  3250. '',
  3251. // implementation
  3252. LinesToStr([
  3253. '$impl.v2 = "";',
  3254. '']));
  3255. end;
  3256. procedure TTestModule.TestImplProc;
  3257. begin
  3258. StartUnit(false);
  3259. Add('interface');
  3260. Add('');
  3261. Add('procedure Proc1;');
  3262. Add('');
  3263. Add('implementation');
  3264. Add('');
  3265. Add('procedure Proc1; begin end;');
  3266. Add('procedure Proc2; begin end;');
  3267. Add('initialization');
  3268. Add(' Proc1;');
  3269. Add(' Proc2;');
  3270. ConvertUnit;
  3271. CheckSource('TestImplProc',
  3272. LinesToStr([ // statements
  3273. 'var $impl = $mod.$impl;',
  3274. 'this.Proc1 = function () {',
  3275. '};',
  3276. '']),
  3277. LinesToStr([ // this.$init
  3278. '$mod.Proc1();',
  3279. '$impl.Proc2();',
  3280. '']),
  3281. LinesToStr([ // implementation
  3282. '$impl.Proc2 = function () {',
  3283. '};',
  3284. ''])
  3285. );
  3286. end;
  3287. procedure TTestModule.TestFunctionResult;
  3288. begin
  3289. StartProgram(false);
  3290. Add('function Func1: longint;');
  3291. Add('begin');
  3292. Add(' Result:=3;');
  3293. Add(' Func1:=4;');
  3294. Add('end;');
  3295. Add('begin');
  3296. ConvertProgram;
  3297. CheckSource('TestFunctionResult',
  3298. LinesToStr([ // statements
  3299. 'this.Func1 = function () {',
  3300. ' var Result = 0;',
  3301. ' Result = 3;',
  3302. ' Result = 4;',
  3303. ' return Result;',
  3304. '};'
  3305. ]),
  3306. '');
  3307. end;
  3308. procedure TTestModule.TestNestedProc;
  3309. begin
  3310. StartProgram(false);
  3311. Add([
  3312. 'var vInUnit: longint;',
  3313. 'function DoIt(pA,pD: longint): longint;',
  3314. 'var',
  3315. ' vB: longint;',
  3316. ' vC: longint;',
  3317. ' function Nesty(pA: longint): longint; ',
  3318. ' var vB: longint;',
  3319. ' begin',
  3320. ' Result:=pa+vb+vc+pd+vInUnit;',
  3321. ' nesty:=3;',
  3322. ' doit:=4;',
  3323. ' exit;',
  3324. ' end;',
  3325. 'begin',
  3326. ' Result:=pa+vb+vc;',
  3327. ' doit:=6;',
  3328. ' exit;',
  3329. 'end;',
  3330. 'begin']);
  3331. ConvertProgram;
  3332. CheckSource('TestNestedProc',
  3333. LinesToStr([ // statements
  3334. 'this.vInUnit = 0;',
  3335. 'this.DoIt = function (pA, pD) {',
  3336. ' var Result = 0;',
  3337. ' var vB = 0;',
  3338. ' var vC = 0;',
  3339. ' function Nesty(pA) {',
  3340. ' var Result$1 = 0;',
  3341. ' var vB = 0;',
  3342. ' Result$1 = pA + vB + vC + pD + $mod.vInUnit;',
  3343. ' Result$1 = 3;',
  3344. ' Result = 4;',
  3345. ' return Result$1;',
  3346. ' return Result$1;',
  3347. ' };',
  3348. ' Result = pA + vB + vC;',
  3349. ' Result = 6;',
  3350. ' return Result;',
  3351. ' return Result;',
  3352. '};'
  3353. ]),
  3354. '');
  3355. end;
  3356. procedure TTestModule.TestNestedProc_ResultString;
  3357. begin
  3358. StartProgram(false);
  3359. Add([
  3360. 'function DoIt: string;',
  3361. ' function Nesty: string; ',
  3362. ' begin',
  3363. ' nesty:=#65#66;',
  3364. ' nesty[1]:=#67;',
  3365. ' doit:=#68;',
  3366. ' doit[2]:=#69;',
  3367. ' end;',
  3368. 'begin',
  3369. ' doit:=#70;',
  3370. ' doit[3]:=#71;',
  3371. 'end;',
  3372. 'begin']);
  3373. ConvertProgram;
  3374. CheckSource('TestNestedProc_ResultString',
  3375. LinesToStr([ // statements
  3376. 'this.DoIt = function () {',
  3377. ' var Result = "";',
  3378. ' function Nesty() {',
  3379. ' var Result$1 = "";',
  3380. ' Result$1 = "AB";',
  3381. ' Result$1 = rtl.setCharAt(Result$1, 0, "C");',
  3382. ' Result = "D";',
  3383. ' Result = rtl.setCharAt(Result, 1, "E");',
  3384. ' return Result$1;',
  3385. ' };',
  3386. ' Result = "F";',
  3387. ' Result = rtl.setCharAt(Result, 2, "G");',
  3388. ' return Result;',
  3389. '};'
  3390. ]),
  3391. '');
  3392. end;
  3393. procedure TTestModule.TestForwardProc;
  3394. begin
  3395. StartProgram(false);
  3396. Add('procedure FuncA(Bar: longint); forward;');
  3397. Add('procedure FuncB(Bar: longint);');
  3398. Add('begin');
  3399. Add(' funca(bar);');
  3400. Add('end;');
  3401. Add('procedure funca(bar: longint);');
  3402. Add('begin');
  3403. Add(' if bar=3 then ;');
  3404. Add('end;');
  3405. Add('begin');
  3406. Add(' funca(4);');
  3407. Add(' funcb(5);');
  3408. ConvertProgram;
  3409. CheckSource('TestForwardProc',
  3410. LinesToStr([ // statements'
  3411. 'this.FuncB = function (Bar) {',
  3412. ' $mod.FuncA(Bar);',
  3413. '};',
  3414. 'this.FuncA = function (Bar) {',
  3415. ' if (Bar === 3);',
  3416. '};'
  3417. ]),
  3418. LinesToStr([
  3419. '$mod.FuncA(4);',
  3420. '$mod.FuncB(5);'
  3421. ])
  3422. );
  3423. end;
  3424. procedure TTestModule.TestNestedForwardProc;
  3425. begin
  3426. StartProgram(false);
  3427. Add('procedure FuncA;');
  3428. Add(' procedure FuncB(i: longint); forward;');
  3429. Add(' procedure FuncC(i: longint);');
  3430. Add(' begin');
  3431. Add(' funcb(i);');
  3432. Add(' end;');
  3433. Add(' procedure FuncB(i: longint);');
  3434. Add(' begin');
  3435. Add(' if i=3 then ;');
  3436. Add(' end;');
  3437. Add('begin');
  3438. Add(' funcc(4)');
  3439. Add('end;');
  3440. Add('begin');
  3441. Add(' funca;');
  3442. ConvertProgram;
  3443. CheckSource('TestNestedForwardProc',
  3444. LinesToStr([ // statements'
  3445. 'this.FuncA = function () {',
  3446. ' function FuncC(i) {',
  3447. ' FuncB(i);',
  3448. ' };',
  3449. ' function FuncB(i) {',
  3450. ' if (i === 3);',
  3451. ' };',
  3452. ' FuncC(4);',
  3453. '};'
  3454. ]),
  3455. LinesToStr([
  3456. '$mod.FuncA();'
  3457. ])
  3458. );
  3459. end;
  3460. procedure TTestModule.TestAssignFunctionResult;
  3461. begin
  3462. StartProgram(false);
  3463. Add('function Func1: longint;');
  3464. Add('begin');
  3465. Add('end;');
  3466. Add('var i: longint;');
  3467. Add('begin');
  3468. Add(' i:=func1();');
  3469. Add(' i:=func1()+func1();');
  3470. ConvertProgram;
  3471. CheckSource('TestAssignFunctionResult',
  3472. LinesToStr([ // statements
  3473. 'this.Func1 = function () {',
  3474. ' var Result = 0;',
  3475. ' return Result;',
  3476. '};',
  3477. 'this.i = 0;'
  3478. ]),
  3479. LinesToStr([
  3480. '$mod.i = $mod.Func1();',
  3481. '$mod.i = $mod.Func1() + $mod.Func1();'
  3482. ]));
  3483. end;
  3484. procedure TTestModule.TestFunctionResultInCondition;
  3485. begin
  3486. StartProgram(false);
  3487. Add('function Func1: longint;');
  3488. Add('begin');
  3489. Add('end;');
  3490. Add('function Func2: boolean;');
  3491. Add('begin');
  3492. Add('end;');
  3493. Add('var i: longint;');
  3494. Add('begin');
  3495. Add(' if func2 then ;');
  3496. Add(' if i=func1() then ;');
  3497. Add(' if i=func1 then ;');
  3498. ConvertProgram;
  3499. CheckSource('TestFunctionResultInCondition',
  3500. LinesToStr([ // statements
  3501. 'this.Func1 = function () {',
  3502. ' var Result = 0;',
  3503. ' return Result;',
  3504. '};',
  3505. 'this.Func2 = function () {',
  3506. ' var Result = false;',
  3507. ' return Result;',
  3508. '};',
  3509. 'this.i = 0;'
  3510. ]),
  3511. LinesToStr([
  3512. 'if ($mod.Func2());',
  3513. 'if ($mod.i === $mod.Func1());',
  3514. 'if ($mod.i === $mod.Func1());'
  3515. ]));
  3516. end;
  3517. procedure TTestModule.TestFunctionResultInForLoop;
  3518. begin
  3519. StartProgram(false);
  3520. Add([
  3521. 'function Func1(a: array of longint): longint;',
  3522. 'begin',
  3523. ' for Result:=High(a) downto Low(a) do if a[Result]=0 then exit;',
  3524. ' for Result in a do if a[Result]=0 then exit;',
  3525. 'end;',
  3526. 'begin',
  3527. ' Func1([1,2,3])']);
  3528. ConvertProgram;
  3529. CheckSource('TestFunctionResultInForLoop',
  3530. LinesToStr([ // statements
  3531. 'this.Func1 = function (a) {',
  3532. ' var Result = 0;',
  3533. ' for (var $l = rtl.length(a) - 1; $l >= 0; $l--) {',
  3534. ' Result = $l;',
  3535. ' if (a[Result] === 0) return Result;',
  3536. ' };',
  3537. ' for (var $in = a, $l1 = 0, $end = rtl.length($in) - 1; $l1 <= $end; $l1++) {',
  3538. ' Result = $in[$l1];',
  3539. ' if (a[Result] === 0) return Result;',
  3540. ' };',
  3541. ' return Result;',
  3542. '};',
  3543. '']),
  3544. LinesToStr([
  3545. '$mod.Func1([1, 2, 3]);'
  3546. ]));
  3547. end;
  3548. procedure TTestModule.TestFunctionResultInTypeCast;
  3549. begin
  3550. StartProgram(false);
  3551. Add([
  3552. 'function GetInt: longint;',
  3553. 'begin',
  3554. 'end;',
  3555. 'begin',
  3556. ' if Byte(GetInt)=0 then ;',
  3557. '']);
  3558. ConvertProgram;
  3559. CheckSource('TestFunctionResultInTypeCast',
  3560. LinesToStr([ // statements
  3561. 'this.GetInt = function () {',
  3562. ' var Result = 0;',
  3563. ' return Result;',
  3564. '};',
  3565. '']),
  3566. LinesToStr([
  3567. 'if (($mod.GetInt() & 255) === 0) ;'
  3568. ]));
  3569. end;
  3570. procedure TTestModule.TestExit;
  3571. begin
  3572. StartProgram(false);
  3573. Add('procedure ProcA;');
  3574. Add('begin');
  3575. Add(' exit;');
  3576. Add('end;');
  3577. Add('function FuncB: longint;');
  3578. Add('begin');
  3579. Add(' exit;');
  3580. Add(' exit(3);');
  3581. Add('end;');
  3582. Add('function FuncC: string;');
  3583. Add('begin');
  3584. Add(' exit;');
  3585. Add(' exit(''a'');');
  3586. Add(' exit(''abc'');');
  3587. Add('end;');
  3588. Add('begin');
  3589. Add(' exit;');
  3590. Add(' exit(1);');
  3591. ConvertProgram;
  3592. CheckSource('TestExit',
  3593. LinesToStr([ // statements
  3594. 'this.ProcA = function () {',
  3595. ' return;',
  3596. '};',
  3597. 'this.FuncB = function () {',
  3598. ' var Result = 0;',
  3599. ' return Result;',
  3600. ' return 3;',
  3601. ' return Result;',
  3602. '};',
  3603. 'this.FuncC = function () {',
  3604. ' var Result = "";',
  3605. ' return Result;',
  3606. ' return "a";',
  3607. ' return "abc";',
  3608. ' return Result;',
  3609. '};'
  3610. ]),
  3611. LinesToStr([
  3612. 'return;',
  3613. 'return 1;',
  3614. '']));
  3615. end;
  3616. procedure TTestModule.TestExit_ResultInFinally;
  3617. begin
  3618. StartProgram(false);
  3619. Add([
  3620. 'function Run: word;',
  3621. 'begin',
  3622. ' try',
  3623. ' exit(3);', // no Result in finally -> use return 3
  3624. ' finally',
  3625. ' end;',
  3626. 'end;',
  3627. 'function Fly: word;',
  3628. 'begin',
  3629. ' try',
  3630. ' exit(3);',
  3631. ' finally',
  3632. ' if Result>0 then ;',
  3633. ' end;',
  3634. 'end;',
  3635. 'function Jump: word;',
  3636. 'begin',
  3637. ' try',
  3638. ' try',
  3639. ' exit(4);',
  3640. ' finally',
  3641. ' end;',
  3642. ' finally',
  3643. ' if Result>0 then ;',
  3644. ' end;',
  3645. 'end;',
  3646. 'begin',
  3647. '']);
  3648. ConvertProgram;
  3649. CheckSource('TestExit_ResultInFinally',
  3650. LinesToStr([ // statements
  3651. 'this.Run = function () {',
  3652. ' var Result = 0;',
  3653. ' try {',
  3654. ' return 3;',
  3655. ' } finally {',
  3656. ' };',
  3657. ' return Result;',
  3658. '};',
  3659. 'this.Fly = function () {',
  3660. ' var Result = 0;',
  3661. ' try {',
  3662. ' Result = 3;',
  3663. ' return Result;',
  3664. ' } finally {',
  3665. ' if (Result > 0) ;',
  3666. ' };',
  3667. ' return Result;',
  3668. '};',
  3669. 'this.Jump = function () {',
  3670. ' var Result = 0;',
  3671. ' try {',
  3672. ' try {',
  3673. ' Result = 4;',
  3674. ' return Result;',
  3675. ' } finally {',
  3676. ' };',
  3677. ' } finally {',
  3678. ' if (Result > 0) ;',
  3679. ' };',
  3680. ' return Result;',
  3681. '};',
  3682. '']),
  3683. LinesToStr([
  3684. '']));
  3685. end;
  3686. procedure TTestModule.TestBreak;
  3687. begin
  3688. StartProgram(false);
  3689. Add([
  3690. 'var',
  3691. ' i: longint;',
  3692. 'begin',
  3693. ' repeat',
  3694. ' break;',
  3695. ' until true;',
  3696. ' while true do',
  3697. ' break;',
  3698. ' for i:=1 to 2 do',
  3699. ' break;']);
  3700. ConvertProgram;
  3701. CheckSource('TestBreak',
  3702. LinesToStr([ // statements
  3703. 'this.i = 0;'
  3704. ]),
  3705. LinesToStr([
  3706. 'do {',
  3707. ' break;',
  3708. '} while (!true);',
  3709. 'while (true) break;',
  3710. 'for ($mod.i = 1; $mod.i <= 2; $mod.i++) break;',
  3711. '']));
  3712. end;
  3713. procedure TTestModule.TestBreakAsVar;
  3714. begin
  3715. StartProgram(false);
  3716. Add([
  3717. 'procedure DoIt(break: boolean);',
  3718. 'begin',
  3719. ' if break then ;',
  3720. 'end;',
  3721. 'var',
  3722. ' break: boolean;',
  3723. 'begin',
  3724. ' if break then ;']);
  3725. ConvertProgram;
  3726. CheckSource('TestBreakAsVar',
  3727. LinesToStr([ // statements
  3728. 'this.DoIt = function (Break) {',
  3729. ' if (Break) ;',
  3730. '};',
  3731. 'this.Break = false;',
  3732. '']),
  3733. LinesToStr([
  3734. 'if($mod.Break) ;',
  3735. '']));
  3736. end;
  3737. procedure TTestModule.TestContinue;
  3738. begin
  3739. StartProgram(false);
  3740. Add('var i: longint;');
  3741. Add('begin');
  3742. Add(' repeat');
  3743. Add(' continue;');
  3744. Add(' until true;');
  3745. Add(' while true do');
  3746. Add(' continue;');
  3747. Add(' for i:=1 to 2 do');
  3748. Add(' continue;');
  3749. ConvertProgram;
  3750. CheckSource('TestContinue',
  3751. LinesToStr([ // statements
  3752. 'this.i = 0;'
  3753. ]),
  3754. LinesToStr([
  3755. 'do {',
  3756. ' continue;',
  3757. '} while (!true);',
  3758. 'while (true) continue;',
  3759. 'for ($mod.i = 1; $mod.i <= 2; $mod.i++) continue;',
  3760. '']));
  3761. end;
  3762. procedure TTestModule.TestProc_External;
  3763. begin
  3764. StartProgram(false);
  3765. Add('procedure Foo; external name ''console.log'';');
  3766. Add('function Bar: longint; external name ''get.item'';');
  3767. Add('function Bla(s: string): longint; external name ''apply.something'';');
  3768. Add('var');
  3769. Add(' i: longint;');
  3770. Add('begin');
  3771. Add(' Foo;');
  3772. Add(' i:=Bar;');
  3773. Add(' i:=Bla(''abc'');');
  3774. ConvertProgram;
  3775. CheckSource('TestProc_External',
  3776. LinesToStr([ // statements
  3777. 'this.i = 0;'
  3778. ]),
  3779. LinesToStr([
  3780. 'console.log();',
  3781. '$mod.i = get.item();',
  3782. '$mod.i = apply.something("abc");'
  3783. ]));
  3784. end;
  3785. procedure TTestModule.TestProc_ExternalOtherUnit;
  3786. begin
  3787. AddModuleWithIntfImplSrc('unit2.pas',
  3788. LinesToStr([
  3789. 'procedure Now; external name ''Date.now'';',
  3790. 'procedure DoIt;'
  3791. ]),
  3792. 'procedure doit; begin end;');
  3793. StartUnit(true);
  3794. Add('interface');
  3795. Add('uses unit2;');
  3796. Add('implementation');
  3797. Add('begin');
  3798. Add(' now;');
  3799. Add(' now();');
  3800. Add(' uNit2.now;');
  3801. Add(' uNit2.now();');
  3802. Add(' doit;');
  3803. Add(' uNit2.doit;');
  3804. ConvertUnit;
  3805. CheckSource('TestProc_ExternalOtherUnit',
  3806. LinesToStr([
  3807. '']),
  3808. LinesToStr([
  3809. 'Date.now();',
  3810. 'Date.now();',
  3811. 'Date.now();',
  3812. 'Date.now();',
  3813. 'pas.unit2.DoIt();',
  3814. 'pas.unit2.DoIt();',
  3815. '']));
  3816. end;
  3817. procedure TTestModule.TestProc_Asm;
  3818. begin
  3819. StartProgram(false);
  3820. Add([
  3821. '{$mode delphi}',
  3822. 'function DoIt: longint;',
  3823. 'begin;',
  3824. ' asm',
  3825. ' { a:{ b:{}, c:[]}, d:''1'' };',
  3826. ' end;',
  3827. ' asm console.log(); end;',
  3828. ' asm',
  3829. ' s = "'' ";',
  3830. ' s = ''" '';',
  3831. ' s = s + "world" + "''";',
  3832. ' // end',
  3833. ' s = ''end'';',
  3834. ' s = "end";',
  3835. ' s = "foo\"bar";',
  3836. ' s = ''a\''b'';',
  3837. ' s = `${expr}\`-"-''-`;',
  3838. ' s = `multi',
  3839. 'line`;',
  3840. ' end;',
  3841. 'end;',
  3842. 'procedure Fly;',
  3843. 'asm',
  3844. ' return;',
  3845. 'end;',
  3846. 'begin']);
  3847. ConvertProgram;
  3848. CheckSource('TestProc_Asm',
  3849. LinesToStr([ // statements
  3850. 'this.DoIt = function () {',
  3851. ' var Result = 0;',
  3852. ' { a:{ b:{}, c:[]}, d:''1'' };',
  3853. ' console.log();',
  3854. ' s = "'' ";',
  3855. ' s = ''" '';',
  3856. ' s = s + "world" + "''";',
  3857. ' // end',
  3858. ' s = ''end'';',
  3859. ' s = "end";',
  3860. ' s = "foo\"bar";',
  3861. ' s = ''a\''b'';',
  3862. ' s = `${expr}\`-"-''-`;',
  3863. ' s = `multi',
  3864. 'line`;',
  3865. ' return Result;',
  3866. '};',
  3867. 'this.Fly = function () {',
  3868. ' return;',
  3869. '};',
  3870. '']),
  3871. LinesToStr([
  3872. ''
  3873. ]));
  3874. end;
  3875. procedure TTestModule.TestProc_Assembler;
  3876. begin
  3877. StartProgram(false);
  3878. Add('function DoIt: longint; assembler;');
  3879. Add('asm');
  3880. Add('{ a:{ b:{}, c:[]}, d:''1'' };');
  3881. Add('end;');
  3882. Add('begin');
  3883. ConvertProgram;
  3884. CheckSource('TestProc_Assembler',
  3885. LinesToStr([ // statements
  3886. 'this.DoIt = function () {',
  3887. ' { a:{ b:{}, c:[]}, d:''1'' };',
  3888. '};'
  3889. ]),
  3890. LinesToStr([
  3891. ''
  3892. ]));
  3893. end;
  3894. procedure TTestModule.TestProc_VarParam;
  3895. begin
  3896. StartProgram(false);
  3897. Add('type integer = longint;');
  3898. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  3899. Add('var vJ: integer;');
  3900. Add('begin');
  3901. Add(' vg:=vg+1;');
  3902. Add(' vj:=vh+2;');
  3903. Add(' vi:=vi+3;');
  3904. Add(' doit(vg,vg,vg);');
  3905. Add(' doit(vh,vh,vj);');
  3906. Add(' doit(vi,vi,vi);');
  3907. Add(' doit(vj,vj,vj);');
  3908. Add('end;');
  3909. Add('var i: integer;');
  3910. Add('begin');
  3911. Add(' doit(i,i,i);');
  3912. ConvertProgram;
  3913. CheckSource('TestProc_VarParam',
  3914. LinesToStr([ // statements
  3915. 'this.DoIt = function (vG,vH,vI) {',
  3916. ' var vJ = 0;',
  3917. ' vG = vG + 1;',
  3918. ' vJ = vH + 2;',
  3919. ' vI.set(vI.get()+3);',
  3920. ' $mod.DoIt(vG, vG, {',
  3921. ' get: function () {',
  3922. ' return vG;',
  3923. ' },',
  3924. ' set: function (v) {',
  3925. ' vG = v;',
  3926. ' }',
  3927. ' });',
  3928. ' $mod.DoIt(vH, vH, {',
  3929. ' get: function () {',
  3930. ' return vJ;',
  3931. ' },',
  3932. ' set: function (v) {',
  3933. ' vJ = v;',
  3934. ' }',
  3935. ' });',
  3936. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  3937. ' $mod.DoIt(vJ, vJ, {',
  3938. ' get: function () {',
  3939. ' return vJ;',
  3940. ' },',
  3941. ' set: function (v) {',
  3942. ' vJ = v;',
  3943. ' }',
  3944. ' });',
  3945. '};',
  3946. 'this.i = 0;'
  3947. ]),
  3948. LinesToStr([
  3949. '$mod.DoIt($mod.i,$mod.i,{',
  3950. ' p: $mod,',
  3951. ' get: function () {',
  3952. ' return this.p.i;',
  3953. ' },',
  3954. ' set: function (v) {',
  3955. ' this.p.i = v;',
  3956. ' }',
  3957. '});'
  3958. ]));
  3959. end;
  3960. procedure TTestModule.TestProc_VarParamString;
  3961. begin
  3962. StartProgram(false);
  3963. Add(['type TCaption = string;',
  3964. 'procedure DoIt(vA: TCaption; var vB: TCaption; out vC: TCaption);',
  3965. 'var c: char;',
  3966. 'begin',
  3967. ' va[1]:=c;',
  3968. ' vb[2]:=c;',
  3969. ' vc[3]:=c;',
  3970. 'end;',
  3971. 'begin']);
  3972. ConvertProgram;
  3973. CheckSource('TestProc_VarParamString',
  3974. LinesToStr([ // statements
  3975. 'this.DoIt = function (vA,vB,vC) {',
  3976. ' var c = "";',
  3977. ' vA = rtl.setCharAt(vA, 0, c);',
  3978. ' vB.set(rtl.setCharAt(vB.get(), 1, c));',
  3979. ' vC.set(rtl.setCharAt(vC.get(), 2, c));',
  3980. '};',
  3981. '']),
  3982. LinesToStr([
  3983. ]));
  3984. end;
  3985. procedure TTestModule.TestProc_VarParamV;
  3986. begin
  3987. StartProgram(false);
  3988. Add([
  3989. 'procedure Inc2(var i: longint);',
  3990. 'begin',
  3991. ' i:=i+2;',
  3992. 'end;',
  3993. 'procedure DoIt(v: longint);',
  3994. 'var p: array of longint;',
  3995. 'begin',
  3996. ' Inc2(v);',
  3997. ' Inc2(p[v]);',
  3998. 'end;',
  3999. 'begin']);
  4000. ConvertProgram;
  4001. CheckSource('TestProc_VarParamV',
  4002. LinesToStr([ // statements
  4003. 'this.Inc2 = function (i) {',
  4004. ' i.set(i.get()+2);',
  4005. '};',
  4006. 'this.DoIt = function (v) {',
  4007. ' var p = [];',
  4008. ' $mod.Inc2({get: function () {',
  4009. ' return v;',
  4010. ' }, set: function (w) {',
  4011. ' v = w;',
  4012. ' }});',
  4013. ' $mod.Inc2({',
  4014. ' a: v,',
  4015. ' p: p,',
  4016. ' get: function () {',
  4017. ' return this.p[this.a];',
  4018. ' },',
  4019. ' set: function (v) {',
  4020. ' this.p[this.a] = v;',
  4021. ' }',
  4022. ' });',
  4023. '};',
  4024. '']),
  4025. LinesToStr([
  4026. '']));
  4027. end;
  4028. procedure TTestModule.TestProc_Overload;
  4029. begin
  4030. StartProgram(false);
  4031. Add('procedure DoIt(vI: longint); begin end;');
  4032. Add('procedure DoIt(vI, vJ: longint); begin end;');
  4033. Add('procedure DoIt(vD: double); begin end;');
  4034. Add('begin');
  4035. Add(' DoIt(1);');
  4036. Add(' DoIt(2,3);');
  4037. Add(' DoIt(4.5);');
  4038. ConvertProgram;
  4039. CheckSource('TestProcedureOverload',
  4040. LinesToStr([ // statements
  4041. 'this.DoIt = function (vI) {',
  4042. '};',
  4043. 'this.DoIt$1 = function (vI, vJ) {',
  4044. '};',
  4045. 'this.DoIt$2 = function (vD) {',
  4046. '};',
  4047. '']),
  4048. LinesToStr([
  4049. '$mod.DoIt(1);',
  4050. '$mod.DoIt$1(2, 3);',
  4051. '$mod.DoIt$2(4.5);',
  4052. '']));
  4053. end;
  4054. procedure TTestModule.TestProc_OverloadForward;
  4055. begin
  4056. StartProgram(false);
  4057. Add('procedure DoIt(vI: longint); forward;');
  4058. Add('procedure DoIt(vI, vJ: longint); begin end;');
  4059. Add('procedure doit(vi: longint); begin end;');
  4060. Add('begin');
  4061. Add(' doit(1);');
  4062. Add(' doit(2,3);');
  4063. ConvertProgram;
  4064. CheckSource('TestProcedureOverloadForward',
  4065. LinesToStr([ // statements
  4066. 'this.DoIt$1 = function (vI, vJ) {',
  4067. '};',
  4068. 'this.DoIt = function (vI) {',
  4069. '};',
  4070. '']),
  4071. LinesToStr([
  4072. '$mod.DoIt(1);',
  4073. '$mod.DoIt$1(2, 3);',
  4074. '']));
  4075. end;
  4076. procedure TTestModule.TestProc_OverloadIntfImpl;
  4077. begin
  4078. StartUnit(false);
  4079. Add('interface');
  4080. Add('procedure DoIt(vI: longint);');
  4081. Add('procedure DoIt(vI, vJ: longint);');
  4082. Add('implementation');
  4083. Add('procedure DoIt(vI, vJ, vK, vL, vM: longint); forward;');
  4084. Add('procedure DoIt(vI, vJ, vK: longint); begin end;');
  4085. Add('procedure DoIt(vi: longint); begin end;');
  4086. Add('procedure DoIt(vI, vJ, vK, vL: longint); begin end;');
  4087. Add('procedure DoIt(vi, vj: longint); begin end;');
  4088. Add('procedure DoIt(vi, vj, vk, vl, vm: longint); begin end;');
  4089. Add('begin');
  4090. Add(' doit(1);');
  4091. Add(' doit(2,3);');
  4092. Add(' doit(4,5,6);');
  4093. Add(' doit(7,8,9,10);');
  4094. Add(' doit(11,12,13,14,15);');
  4095. ConvertUnit;
  4096. CheckSource('TestProcedureOverloadUnit',
  4097. LinesToStr([ // statements
  4098. 'var $impl = $mod.$impl;',
  4099. 'this.DoIt = function (vI) {',
  4100. '};',
  4101. 'this.DoIt$1 = function (vI, vJ) {',
  4102. '};',
  4103. '']),
  4104. LinesToStr([ // this.$init
  4105. '$mod.DoIt(1);',
  4106. '$mod.DoIt$1(2, 3);',
  4107. '$impl.DoIt$3(4,5,6);',
  4108. '$impl.DoIt$4(7,8,9,10);',
  4109. '$impl.DoIt$2(11,12,13,14,15);',
  4110. '']),
  4111. LinesToStr([ // implementation
  4112. '$impl.DoIt$3 = function (vI, vJ, vK) {',
  4113. '};',
  4114. '$impl.DoIt$4 = function (vI, vJ, vK, vL) {',
  4115. '};',
  4116. '$impl.DoIt$2 = function (vI, vJ, vK, vL, vM) {',
  4117. '};',
  4118. '']));
  4119. end;
  4120. procedure TTestModule.TestProc_OverloadNested;
  4121. begin
  4122. StartProgram(false);
  4123. Add([
  4124. 'procedure doit(vA: longint);',
  4125. ' procedure DoIt(vA, vB: longint); overload;',
  4126. ' begin',
  4127. ' doit(1);',
  4128. ' doit(1,2);',
  4129. ' end;',
  4130. ' procedure doit(vA, vB, vC: longint);',
  4131. ' begin',
  4132. ' doit(1);',
  4133. ' doit(1,2);',
  4134. ' doit(1,2,3);',
  4135. ' end;',
  4136. 'begin',
  4137. ' doit(1);',
  4138. ' doit(1,2);',
  4139. ' doit(1,2,3);',
  4140. 'end;',
  4141. 'begin // main',
  4142. ' doit(1);']);
  4143. ConvertProgram;
  4144. CheckSource('TestProcedureOverloadNested',
  4145. LinesToStr([ // statements
  4146. 'this.doit = function (vA) {',
  4147. ' function DoIt$1(vA, vB) {',
  4148. ' $mod.doit(1);',
  4149. ' DoIt$1(1, 2);',
  4150. ' };',
  4151. ' function doit$2(vA, vB, vC) {',
  4152. ' $mod.doit(1);',
  4153. ' DoIt$1(1, 2);',
  4154. ' doit$2(1, 2, 3);',
  4155. ' };',
  4156. ' $mod.doit(1);',
  4157. ' DoIt$1(1, 2);',
  4158. ' doit$2(1, 2, 3);',
  4159. '};',
  4160. '']),
  4161. LinesToStr([
  4162. '$mod.doit(1);',
  4163. '']));
  4164. end;
  4165. procedure TTestModule.TestProc_OverloadNestedForward;
  4166. begin
  4167. StartProgram(false);
  4168. Add([
  4169. 'procedure DoIt(vA: longint); overload; forward;',
  4170. 'procedure DoIt(vB, vC: longint); overload;',
  4171. 'begin // 2 param overload',
  4172. ' doit(1);',
  4173. ' doit(1,2);',
  4174. 'end;',
  4175. 'procedure doit(vA: longint);',
  4176. ' procedure DoIt(vA, vB, vC: longint); overload; forward;',
  4177. ' procedure DoIt(vA, vB, vC, vD: longint); overload;',
  4178. ' begin // 4 param overload',
  4179. ' doit(1);',
  4180. ' doit(1,2);',
  4181. ' doit(1,2,3);',
  4182. ' doit(1,2,3,4);',
  4183. ' end;',
  4184. ' procedure doit(vA, vB, vC: longint);',
  4185. ' procedure DoIt(vA, vB, vC, vD, vE: longint); overload; forward;',
  4186. ' procedure DoIt(vA, vB, vC, vD, vE, vF: longint); overload;',
  4187. ' begin // 6 param overload',
  4188. ' doit(1);',
  4189. ' doit(1,2);',
  4190. ' doit(1,2,3);',
  4191. ' doit(1,2,3,4);',
  4192. ' doit(1,2,3,4,5);',
  4193. ' doit(1,2,3,4,5,6);',
  4194. ' end;',
  4195. ' procedure doit(vA, vB, vC, vD, vE: longint);',
  4196. ' begin // 5 param overload',
  4197. ' doit(1);',
  4198. ' doit(1,2);',
  4199. ' doit(1,2,3);',
  4200. ' doit(1,2,3,4);',
  4201. ' doit(1,2,3,4,5);',
  4202. ' doit(1,2,3,4,5,6);',
  4203. ' end;',
  4204. ' begin // 3 param overload',
  4205. ' doit(1);',
  4206. ' doit(1,2);',
  4207. ' doit(1,2,3);',
  4208. ' doit(1,2,3,4);',
  4209. ' doit(1,2,3,4,5);',
  4210. ' doit(1,2,3,4,5,6);',
  4211. ' end;',
  4212. 'begin // 1 param overload',
  4213. ' doit(1);',
  4214. ' doit(1,2);',
  4215. ' doit(1,2,3);',
  4216. ' doit(1,2,3,4);',
  4217. 'end;',
  4218. 'begin // main',
  4219. ' doit(1);',
  4220. ' doit(1,2);']);
  4221. ConvertProgram;
  4222. CheckSource('TestProc_OverloadNestedForward',
  4223. LinesToStr([ // statements
  4224. 'this.DoIt$1 = function (vB, vC) {',
  4225. ' $mod.DoIt(1);',
  4226. ' $mod.DoIt$1(1, 2);',
  4227. '};',
  4228. 'this.DoIt = function (vA) {',
  4229. ' function DoIt$3(vA, vB, vC, vD) {',
  4230. ' $mod.DoIt(1);',
  4231. ' $mod.DoIt$1(1, 2);',
  4232. ' DoIt$2(1, 2, 3);',
  4233. ' DoIt$3(1, 2, 3, 4);',
  4234. ' };',
  4235. ' function DoIt$2(vA, vB, vC) {',
  4236. ' function DoIt$5(vA, vB, vC, vD, vE, vF) {',
  4237. ' $mod.DoIt(1);',
  4238. ' $mod.DoIt$1(1, 2);',
  4239. ' DoIt$2(1, 2, 3);',
  4240. ' DoIt$3(1, 2, 3, 4);',
  4241. ' DoIt$4(1, 2, 3, 4, 5);',
  4242. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  4243. ' };',
  4244. ' function DoIt$4(vA, vB, vC, vD, vE) {',
  4245. ' $mod.DoIt(1);',
  4246. ' $mod.DoIt$1(1, 2);',
  4247. ' DoIt$2(1, 2, 3);',
  4248. ' DoIt$3(1, 2, 3, 4);',
  4249. ' DoIt$4(1, 2, 3, 4, 5);',
  4250. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  4251. ' };',
  4252. ' $mod.DoIt(1);',
  4253. ' $mod.DoIt$1(1, 2);',
  4254. ' DoIt$2(1, 2, 3);',
  4255. ' DoIt$3(1, 2, 3, 4);',
  4256. ' DoIt$4(1, 2, 3, 4, 5);',
  4257. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  4258. ' };',
  4259. ' $mod.DoIt(1);',
  4260. ' $mod.DoIt$1(1, 2);',
  4261. ' DoIt$2(1, 2, 3);',
  4262. ' DoIt$3(1, 2, 3, 4);',
  4263. '};',
  4264. '']),
  4265. LinesToStr([
  4266. '$mod.DoIt(1);',
  4267. '$mod.DoIt$1(1, 2);',
  4268. '']));
  4269. end;
  4270. procedure TTestModule.TestProc_OverloadUnitCycle;
  4271. begin
  4272. AddModuleWithIntfImplSrc('Unit2.pas',
  4273. LinesToStr([
  4274. 'type',
  4275. ' TObject = class',
  4276. ' procedure DoIt(b: boolean); virtual; abstract;',
  4277. ' procedure DoIt(i: longint); virtual; abstract;',
  4278. ' end;',
  4279. '']),
  4280. 'uses test1;');
  4281. StartUnit(true);
  4282. Add([
  4283. 'interface',
  4284. 'uses unit2;',
  4285. 'type',
  4286. ' TEagle = class(TObject)',
  4287. ' procedure DoIt(b: boolean); override;',
  4288. ' procedure DoIt(i: longint); override;',
  4289. ' end;',
  4290. 'implementation',
  4291. 'procedure TEagle.DoIt(b: boolean); begin end;',
  4292. 'procedure TEagle.DoIt(i: longint); begin end;',
  4293. '']);
  4294. ConvertUnit;
  4295. CheckSource('TestProc_OverloadUnitCycle',
  4296. LinesToStr([ // statements
  4297. 'rtl.createClass(this, "TEagle", pas.Unit2.TObject, function () {',
  4298. ' this.DoIt = function (b) {',
  4299. ' };',
  4300. ' this.DoIt$1 = function (i) {',
  4301. ' };',
  4302. '});',
  4303. '']),
  4304. '',
  4305. LinesToStr([
  4306. '']));
  4307. end;
  4308. procedure TTestModule.TestProc_Varargs;
  4309. begin
  4310. StartProgram(false);
  4311. Add([
  4312. 'procedure ProcA(i:longint); varargs; external name ''ProcA'';',
  4313. 'procedure ProcB; varargs; external name ''ProcB'';',
  4314. 'procedure ProcC(i: longint = 17); varargs; external name ''ProcC'';',
  4315. 'function GetIt: longint; begin end;',
  4316. 'begin',
  4317. ' ProcA(1);',
  4318. ' ProcA(1,2);',
  4319. ' ProcA(1,2.0);',
  4320. ' ProcA(1,2,3);',
  4321. ' ProcA(1,''2'');',
  4322. ' ProcA(2,'''');',
  4323. ' ProcA(3,false);',
  4324. ' ProcB;',
  4325. ' ProcB();',
  4326. ' ProcB(4);',
  4327. ' ProcB(''foo'');',
  4328. ' ProcC;',
  4329. ' ProcC();',
  4330. ' ProcC(4);',
  4331. ' ProcC(5,''foo'');',
  4332. ' ProcB(GetIt);',
  4333. ' ProcB(GetIt());',
  4334. ' ProcB(GetIt,GetIt());']);
  4335. ConvertProgram;
  4336. CheckSource('TestProc_Varargs',
  4337. LinesToStr([ // statements
  4338. 'this.GetIt = function () {',
  4339. ' var Result = 0;',
  4340. ' return Result;',
  4341. '};',
  4342. '']),
  4343. LinesToStr([
  4344. 'ProcA(1);',
  4345. 'ProcA(1, 2);',
  4346. 'ProcA(1, 2.0);',
  4347. 'ProcA(1, 2, 3);',
  4348. 'ProcA(1, "2");',
  4349. 'ProcA(2, "");',
  4350. 'ProcA(3, false);',
  4351. 'ProcB();',
  4352. 'ProcB();',
  4353. 'ProcB(4);',
  4354. 'ProcB("foo");',
  4355. 'ProcC(17);',
  4356. 'ProcC(17);',
  4357. 'ProcC(4);',
  4358. 'ProcC(5, "foo");',
  4359. 'ProcB($mod.GetIt());',
  4360. 'ProcB($mod.GetIt());',
  4361. 'ProcB($mod.GetIt(), $mod.GetIt());',
  4362. '']));
  4363. end;
  4364. procedure TTestModule.TestProc_ConstOrder;
  4365. begin
  4366. StartProgram(false);
  4367. Add([
  4368. 'const A = 3;',
  4369. 'const B = A+1;',
  4370. 'procedure DoIt;',
  4371. 'const C = A+1;',
  4372. 'const D = B+1;',
  4373. 'const E = D+C+B+A;',
  4374. 'begin',
  4375. 'end;',
  4376. 'begin'
  4377. ]);
  4378. ConvertProgram;
  4379. CheckSource('TestProc_ConstOrder',
  4380. LinesToStr([ // statements
  4381. 'this.A = 3;',
  4382. 'this.B = 3 + 1;',
  4383. 'var C = 3 + 1;',
  4384. 'var D = 4 + 1;',
  4385. 'var E = 5 + 4 + 4 + 3;',
  4386. 'this.DoIt = function () {',
  4387. '};',
  4388. '']),
  4389. LinesToStr([
  4390. ''
  4391. ]));
  4392. end;
  4393. procedure TTestModule.TestProc_DuplicateConst;
  4394. begin
  4395. StartProgram(false);
  4396. Add([
  4397. 'const A = 1;',
  4398. 'procedure DoIt;',
  4399. 'const A = 2;',
  4400. ' procedure SubIt;',
  4401. ' const A = 21;',
  4402. ' begin',
  4403. ' end;',
  4404. 'begin',
  4405. 'end;',
  4406. 'procedure DoSome;',
  4407. 'const A = 3;',
  4408. 'begin',
  4409. 'end;',
  4410. 'begin'
  4411. ]);
  4412. ConvertProgram;
  4413. CheckSource('TestProc_DuplicateConst',
  4414. LinesToStr([ // statements
  4415. 'this.A = 1;',
  4416. 'var A$1 = 2;',
  4417. 'var A$2 = 21;',
  4418. 'this.DoIt = function () {',
  4419. ' function SubIt() {',
  4420. ' };',
  4421. '};',
  4422. 'var A$3 = 3;',
  4423. 'this.DoSome = function () {',
  4424. '};',
  4425. '']),
  4426. LinesToStr([
  4427. ''
  4428. ]));
  4429. end;
  4430. procedure TTestModule.TestProc_LocalVarAbsolute;
  4431. begin
  4432. StartProgram(false);
  4433. Add([
  4434. 'type',
  4435. ' TObject = class',
  4436. ' Index: longint;',
  4437. ' procedure DoAbs(Item: pointer);',
  4438. ' end;',
  4439. 'procedure TObject.DoAbs(Item: pointer);',
  4440. 'var',
  4441. ' o: TObject absolute Item;',
  4442. 'begin',
  4443. ' if o.Index<o.Index then o.Index:=o.Index;',
  4444. 'end;',
  4445. 'procedure DoIt(i: longint; p: pointer);',
  4446. 'var',
  4447. ' d: double absolute i;',
  4448. ' s: string absolute d;',
  4449. ' oi: TObject absolute i;',
  4450. ' op: TObject absolute p;',
  4451. 'begin',
  4452. ' if d=d then d:=d;',
  4453. ' if s=s then s:=s;',
  4454. ' if oi.Index<oi.Index then oi.Index:=oi.Index;',
  4455. ' if op.Index=op.Index then op.Index:=op.Index;',
  4456. 'end;',
  4457. 'begin']);
  4458. ConvertProgram;
  4459. CheckSource('TestProc_LocalVarAbsolute',
  4460. LinesToStr([ // statements
  4461. 'rtl.createClass(this, "TObject", null, function () {',
  4462. ' this.$init = function () {',
  4463. ' this.Index = 0;',
  4464. ' };',
  4465. ' this.$final = function () {',
  4466. ' };',
  4467. ' this.DoAbs = function (Item) {',
  4468. ' if (Item.Index < Item.Index) Item.Index = Item.Index;',
  4469. ' };',
  4470. '});',
  4471. 'this.DoIt = function (i, p) {',
  4472. ' if (i === i) i = i;',
  4473. ' if (i === i) i = i;',
  4474. ' if (i.Index < i.Index) i.Index = i.Index;',
  4475. ' if (p.Index === p.Index) p.Index = p.Index;',
  4476. '};'
  4477. ]),
  4478. LinesToStr([
  4479. ]));
  4480. end;
  4481. procedure TTestModule.TestProc_LocalVarInit;
  4482. begin
  4483. StartProgram(false);
  4484. Add([
  4485. 'type TBytes = array of byte;',
  4486. 'procedure DoIt;',
  4487. 'const c = 4;',
  4488. 'var',
  4489. ' b: byte = 1;',
  4490. ' w: word = 2+c;',
  4491. ' p: pointer = nil;',
  4492. ' Buffer: TBytes = nil;',
  4493. 'begin',
  4494. 'end;',
  4495. 'begin']);
  4496. ConvertProgram;
  4497. CheckSource('TestProc_LocalVarInit',
  4498. LinesToStr([ // statements
  4499. 'var c = 4;',
  4500. 'this.DoIt = function () {',
  4501. ' var b = 1;',
  4502. ' var w = 2 + 4;',
  4503. ' var p = null;',
  4504. ' var Buffer = [];',
  4505. '};',
  4506. '']),
  4507. LinesToStr([
  4508. ]));
  4509. end;
  4510. procedure TTestModule.TestProc_ReservedWords;
  4511. begin
  4512. StartProgram(false);
  4513. Add([
  4514. 'procedure Date(ArrayBuffer: longint);',
  4515. 'const',
  4516. ' NaN: longint = 3;',
  4517. 'var',
  4518. ' &Boolean: longint;',
  4519. ' procedure Error(ArrayBuffer: longint);',
  4520. ' begin',
  4521. ' end;',
  4522. 'begin',
  4523. ' Nan:=&bOolean;',
  4524. 'end;',
  4525. 'begin',
  4526. ' Date(1);']);
  4527. ConvertProgram;
  4528. CheckSource('TestProc_ReservedWords',
  4529. LinesToStr([ // statements
  4530. 'var naN = 3;',
  4531. 'this.Date = function (arrayBuffer) {',
  4532. ' var boolean = 0;',
  4533. ' function error(arrayBuffer) {',
  4534. ' };',
  4535. ' naN = boolean;',
  4536. '};',
  4537. '']),
  4538. LinesToStr([
  4539. ' $mod.Date(1);'
  4540. ]));
  4541. end;
  4542. procedure TTestModule.TestProc_ConstRefWord;
  4543. begin
  4544. StartProgram(false);
  4545. Add([
  4546. 'procedure Run(constref w: word);',
  4547. 'var l: word;',
  4548. 'begin',
  4549. ' l:=w;',
  4550. ' Run(w);',
  4551. ' Run(l);',
  4552. 'end;',
  4553. 'procedure Fly(a: word; var b: word; out c: word; const d: word; constref e: word);',
  4554. 'begin',
  4555. ' Run(a);',
  4556. ' Run(b);',
  4557. ' Run(c);',
  4558. ' Run(d);',
  4559. ' Run(e);',
  4560. 'end;',
  4561. 'begin',
  4562. ' Run(1);']);
  4563. ConvertProgram;
  4564. CheckHint(mtWarning,nConstRefNotForXAsConst,'ConstRef not yet implemented for Word. Treating as Const');
  4565. CheckSource('TestProc_ConstRefWord',
  4566. LinesToStr([ // statements
  4567. 'this.Run = function (w) {',
  4568. ' var l = 0;',
  4569. ' l = w;',
  4570. ' $mod.Run(w);',
  4571. ' $mod.Run(l);',
  4572. '};',
  4573. 'this.Fly = function (a, b, c, d, e) {',
  4574. ' $mod.Run(a);',
  4575. ' $mod.Run(b.get());',
  4576. ' $mod.Run(c.get());',
  4577. ' $mod.Run(d);',
  4578. ' $mod.Run(e);',
  4579. '};',
  4580. '']),
  4581. LinesToStr([
  4582. '$mod.Run(1);'
  4583. ]));
  4584. end;
  4585. procedure TTestModule.TestAnonymousProc_Assign_ObjFPC;
  4586. begin
  4587. StartProgram(false);
  4588. Add([
  4589. '{$mode objfpc}',
  4590. 'type',
  4591. ' TFunc = reference to function(x: word): word;',
  4592. 'var Func: TFunc;',
  4593. 'procedure DoIt(a: word);',
  4594. 'begin',
  4595. ' Func:=function(b:word): word',
  4596. ' begin',
  4597. ' Result:=a+b;',
  4598. ' exit(b);',
  4599. ' exit(Result);',
  4600. ' end;',// test semicolon
  4601. ' a:=3;',
  4602. 'end;',
  4603. 'begin',
  4604. ' Func:=function(c:word):word begin',
  4605. ' Result:=3+c;',
  4606. ' exit(c);',
  4607. ' exit(Result);',
  4608. ' end;']);
  4609. ConvertProgram;
  4610. CheckSource('TestAnonymousProc_Assign_ObjFPC',
  4611. LinesToStr([ // statements
  4612. 'this.Func = null;',
  4613. 'this.DoIt = function (a) {',
  4614. ' $mod.Func = function (b) {',
  4615. ' var Result = 0;',
  4616. ' Result = a + b;',
  4617. ' return b;',
  4618. ' return Result;',
  4619. ' return Result;',
  4620. ' };',
  4621. ' a = 3;',
  4622. '};',
  4623. '']),
  4624. LinesToStr([
  4625. '$mod.Func = function (c) {',
  4626. ' var Result = 0;',
  4627. ' Result = 3 + c;',
  4628. ' return c;',
  4629. ' return Result;',
  4630. ' return Result;',
  4631. '};',
  4632. '']));
  4633. end;
  4634. procedure TTestModule.TestAnonymousProc_Assign_Delphi;
  4635. begin
  4636. StartProgram(false);
  4637. Add([
  4638. '{$mode delphi}',
  4639. 'type',
  4640. ' TProc = reference to procedure(x: word);',
  4641. 'procedure DoIt(a: word);',
  4642. 'var Proc: TProc;',
  4643. 'begin',
  4644. ' Proc:=procedure(b:word) begin end;',
  4645. 'end;',
  4646. 'var Proc: TProc;',
  4647. 'begin',
  4648. ' Proc:=procedure(c:word) begin end;',
  4649. '']);
  4650. ConvertProgram;
  4651. CheckSource('TestAnonymousProc_Assign_Delphi',
  4652. LinesToStr([ // statements
  4653. 'this.DoIt = function (a) {',
  4654. ' var Proc = null;',
  4655. ' Proc = function (b) {',
  4656. ' };',
  4657. '};',
  4658. 'this.Proc = null;',
  4659. '']),
  4660. LinesToStr([
  4661. '$mod.Proc = function (c) {',
  4662. '};',
  4663. '']));
  4664. end;
  4665. procedure TTestModule.TestAnonymousProc_Arg;
  4666. begin
  4667. StartProgram(false);
  4668. Add([
  4669. 'type',
  4670. ' TProc = reference to procedure;',
  4671. ' TFunc = reference to function(x: word): word;',
  4672. 'procedure DoMore(f,g: TProc);',
  4673. 'begin',
  4674. 'end;',
  4675. 'procedure DoOdd(v: jsvalue);',
  4676. 'begin',
  4677. 'end;',
  4678. 'procedure DoIt(f: TFunc);',
  4679. 'begin',
  4680. ' DoIt(function(b:word): word',
  4681. ' begin',
  4682. ' Result:=1+b;',
  4683. ' end);',
  4684. ' DoMore(procedure begin end, procedure begin end);',
  4685. ' DoOdd(procedure begin end);',
  4686. 'end;',
  4687. 'begin',
  4688. ' DoMore(procedure begin end,',
  4689. ' procedure assembler asm',
  4690. ' console.log("c");',
  4691. ' end);',
  4692. '']);
  4693. ConvertProgram;
  4694. CheckSource('TestAnonymousProc_Arg',
  4695. LinesToStr([ // statements
  4696. 'this.DoMore = function (f, g) {',
  4697. '};',
  4698. 'this.DoOdd = function (v) {',
  4699. '};',
  4700. 'this.DoIt = function (f) {',
  4701. ' $mod.DoIt(function (b) {',
  4702. ' var Result = 0;',
  4703. ' Result = 1 + b;',
  4704. ' return Result;',
  4705. ' });',
  4706. ' $mod.DoMore(function () {',
  4707. ' }, function () {',
  4708. ' });',
  4709. ' $mod.DoOdd(function () {',
  4710. ' });',
  4711. '};',
  4712. '']),
  4713. LinesToStr([
  4714. '$mod.DoMore(function () {',
  4715. '}, function () {',
  4716. ' console.log("c");',
  4717. '});',
  4718. '']));
  4719. end;
  4720. procedure TTestModule.TestAnonymousProc_Typecast;
  4721. begin
  4722. StartProgram(false);
  4723. Add([
  4724. 'type',
  4725. ' TProc = reference to procedure(w: word);',
  4726. ' TArr = array of word;',
  4727. ' TFuncArr = reference to function: TArr;',
  4728. 'procedure DoIt(p: TProc);',
  4729. 'var',
  4730. ' w: word;',
  4731. ' a: TArr;',
  4732. 'begin',
  4733. ' p:=TProc(procedure(b: smallint) begin end);',
  4734. ' a:=TFuncArr(function: TArr begin end)();',
  4735. ' w:=TFuncArr(function: TArr begin end)()[3];',
  4736. 'end;',
  4737. 'begin']);
  4738. ConvertProgram;
  4739. CheckSource('TestAnonymousProc_Typecast',
  4740. LinesToStr([ // statements
  4741. 'this.DoIt = function (p) {',
  4742. ' var w = 0;',
  4743. ' var a = [];',
  4744. ' p = function (b) {',
  4745. ' };',
  4746. ' a = function () {',
  4747. ' var Result = [];',
  4748. ' return Result;',
  4749. ' }();',
  4750. ' w = function () {',
  4751. ' var Result = [];',
  4752. ' return Result;',
  4753. ' }()[3];',
  4754. '};',
  4755. '']),
  4756. LinesToStr([
  4757. '']));
  4758. end;
  4759. procedure TTestModule.TestAnonymousProc_With;
  4760. begin
  4761. StartProgram(false);
  4762. Add([
  4763. 'type',
  4764. ' TProc = reference to procedure(w: word);',
  4765. ' TObject = class',
  4766. ' b: boolean;',
  4767. ' end;',
  4768. 'var',
  4769. ' p: TProc;',
  4770. ' bird: TObject;',
  4771. 'begin',
  4772. ' with bird do',
  4773. ' p:=procedure(w: word)',
  4774. ' begin',
  4775. ' b:=w>2;',
  4776. ' end;',
  4777. '']);
  4778. ConvertProgram;
  4779. CheckSource('TestAnonymousProc_With',
  4780. LinesToStr([ // statements
  4781. 'rtl.createClass(this, "TObject", null, function () {',
  4782. ' this.$init = function () {',
  4783. ' this.b = false;',
  4784. ' };',
  4785. ' this.$final = function () {',
  4786. ' };',
  4787. '});',
  4788. 'this.p = null;',
  4789. 'this.bird = null;',
  4790. '']),
  4791. LinesToStr([
  4792. 'var $with = $mod.bird;',
  4793. '$mod.p = function (w) {',
  4794. ' $with.b = w > 2;',
  4795. '};',
  4796. '']));
  4797. end;
  4798. procedure TTestModule.TestAnonymousProc_ExceptOn;
  4799. begin
  4800. StartProgram(false);
  4801. Add([
  4802. 'type',
  4803. ' TProc = reference to procedure;',
  4804. ' TObject = class',
  4805. ' b: boolean;',
  4806. ' end;',
  4807. 'procedure DoIt;',
  4808. 'var',
  4809. ' p: TProc;',
  4810. 'begin',
  4811. ' try',
  4812. ' except',
  4813. ' on E: TObject do',
  4814. ' p:=procedure',
  4815. ' begin',
  4816. ' E.b:=true;',
  4817. ' end;',
  4818. ' end;',
  4819. 'end;',
  4820. 'begin']);
  4821. ConvertProgram;
  4822. CheckSource('TestAnonymousProc_ExceptOn',
  4823. LinesToStr([ // statements
  4824. 'rtl.createClass(this, "TObject", null, function () {',
  4825. ' this.$init = function () {',
  4826. ' this.b = false;',
  4827. ' };',
  4828. ' this.$final = function () {',
  4829. ' };',
  4830. '});',
  4831. 'this.DoIt = function () {',
  4832. ' var p = null;',
  4833. ' try {} catch ($e) {',
  4834. ' if ($mod.TObject.isPrototypeOf($e)) {',
  4835. ' var E = $e;',
  4836. ' p = function () {',
  4837. ' E.b = true;',
  4838. ' };',
  4839. ' } else throw $e',
  4840. ' };',
  4841. '};',
  4842. '']),
  4843. LinesToStr([
  4844. '']));
  4845. end;
  4846. procedure TTestModule.TestAnonymousProc_Nested;
  4847. begin
  4848. StartProgram(false);
  4849. Add([
  4850. 'type',
  4851. ' TProc = reference to procedure;',
  4852. ' TObject = class',
  4853. ' i: byte;',
  4854. ' procedure DoIt;',
  4855. ' end;',
  4856. 'procedure TObject.DoIt;',
  4857. 'var',
  4858. ' p: TProc;',
  4859. ' procedure Sub;',
  4860. ' begin',
  4861. ' p:=procedure',
  4862. ' begin',
  4863. ' i:=3;',
  4864. ' Self.i:=4;',
  4865. ' p:=procedure',
  4866. ' procedure SubSub;',
  4867. ' begin',
  4868. ' i:=13;',
  4869. ' Self.i:=14;',
  4870. ' end;',
  4871. ' begin',
  4872. ' i:=13;',
  4873. ' Self.i:=14;',
  4874. ' end;',
  4875. ' end;',
  4876. ' end;',
  4877. 'begin',
  4878. 'end;',
  4879. 'begin']);
  4880. ConvertProgram;
  4881. CheckSource('TestAnonymousProc_Nested',
  4882. LinesToStr([ // statements
  4883. 'rtl.createClass(this, "TObject", null, function () {',
  4884. ' this.$init = function () {',
  4885. ' this.i = 0;',
  4886. ' };',
  4887. ' this.$final = function () {',
  4888. ' };',
  4889. ' this.DoIt = function () {',
  4890. ' var $Self = this;',
  4891. ' var p = null;',
  4892. ' function Sub() {',
  4893. ' p = function () {',
  4894. ' $Self.i = 3;',
  4895. ' $Self.i = 4;',
  4896. ' p = function () {',
  4897. ' function SubSub() {',
  4898. ' $Self.i = 13;',
  4899. ' $Self.i = 14;',
  4900. ' };',
  4901. ' $Self.i = 13;',
  4902. ' $Self.i = 14;',
  4903. ' };',
  4904. ' };',
  4905. ' };',
  4906. ' };',
  4907. '});',
  4908. '']),
  4909. LinesToStr([
  4910. '']));
  4911. end;
  4912. procedure TTestModule.TestAnonymousProc_NestedAssignResult;
  4913. begin
  4914. StartProgram(false);
  4915. Add([
  4916. 'type',
  4917. ' TProc = reference to procedure;',
  4918. 'function DoIt: TProc;',
  4919. ' function Sub: TProc;',
  4920. ' begin',
  4921. ' Result:=procedure',
  4922. ' begin',
  4923. ' Sub:=procedure',
  4924. ' procedure SubSub;',
  4925. ' begin',
  4926. ' Result:=nil;',
  4927. ' Sub:=nil;',
  4928. ' DoIt:=nil;',
  4929. ' end;',
  4930. ' begin',
  4931. ' Result:=nil;',
  4932. ' Sub:=nil;',
  4933. ' DoIt:=nil;',
  4934. ' end;',
  4935. ' end;',
  4936. ' end;',
  4937. 'begin',
  4938. 'end;',
  4939. 'begin']);
  4940. ConvertProgram;
  4941. CheckSource('TestAnonymousProc_NestedAssignResult',
  4942. LinesToStr([ // statements
  4943. 'this.DoIt = function () {',
  4944. ' var Result = null;',
  4945. ' function Sub() {',
  4946. ' var Result$1 = null;',
  4947. ' Result$1 = function () {',
  4948. ' Result$1 = function () {',
  4949. ' function SubSub() {',
  4950. ' Result$1 = null;',
  4951. ' Result$1 = null;',
  4952. ' Result = null;',
  4953. ' };',
  4954. ' Result$1 = null;',
  4955. ' Result$1 = null;',
  4956. ' Result = null;',
  4957. ' };',
  4958. ' };',
  4959. ' return Result$1;',
  4960. ' };',
  4961. ' return Result;',
  4962. '};',
  4963. '']),
  4964. LinesToStr([
  4965. '']));
  4966. end;
  4967. procedure TTestModule.TestAnonymousProc_Class;
  4968. begin
  4969. StartProgram(false);
  4970. Add([
  4971. 'type',
  4972. ' TProc = reference to procedure;',
  4973. ' TEvent = procedure of object;',
  4974. ' TObject = class',
  4975. ' Size: word;',
  4976. ' function GetIt: TProc;',
  4977. ' procedure DoIt; virtual; abstract;',
  4978. ' end;',
  4979. 'function TObject.GetIt: TProc;',
  4980. 'begin',
  4981. ' Result:=procedure',
  4982. ' var p: TEvent;',
  4983. ' begin',
  4984. ' Size:=Size;',
  4985. ' Size:=Self.Size;',
  4986. ' p:=@DoIt;',
  4987. ' p:[email protected];',
  4988. ' end;',
  4989. 'end;',
  4990. 'begin']);
  4991. ConvertProgram;
  4992. CheckSource('TestAnonymousProc_Class',
  4993. LinesToStr([ // statements
  4994. 'rtl.createClass(this, "TObject", null, function () {',
  4995. ' this.$init = function () {',
  4996. ' this.Size = 0;',
  4997. ' };',
  4998. ' this.$final = function () {',
  4999. ' };',
  5000. ' this.GetIt = function () {',
  5001. ' var $Self = this;',
  5002. ' var Result = null;',
  5003. ' Result = function () {',
  5004. ' var p = null;',
  5005. ' $Self.Size = $Self.Size;',
  5006. ' $Self.Size = $Self.Size;',
  5007. ' p = rtl.createCallback($Self, "DoIt");',
  5008. ' p = rtl.createCallback($Self, "DoIt");',
  5009. ' };',
  5010. ' return Result;',
  5011. ' };',
  5012. '});',
  5013. '']),
  5014. LinesToStr([
  5015. '']));
  5016. end;
  5017. procedure TTestModule.TestAnonymousProc_ForLoop;
  5018. begin
  5019. StartProgram(false);
  5020. Add([
  5021. 'type TProc = reference to procedure;',
  5022. 'procedure Foo(p: TProc);',
  5023. 'begin',
  5024. 'end;',
  5025. 'procedure DoIt;',
  5026. 'var i: word;',
  5027. ' a: word;',
  5028. 'begin',
  5029. ' for i:=1 to 10 do begin',
  5030. ' Foo(procedure begin a:=3; end);',
  5031. ' end;',
  5032. 'end;',
  5033. 'begin',
  5034. ' DoIt;']);
  5035. ConvertProgram;
  5036. CheckSource('TestAnonymousProc_ForLoop',
  5037. LinesToStr([ // statements
  5038. 'this.Foo = function (p) {',
  5039. '};',
  5040. 'this.DoIt = function () {',
  5041. ' var i = 0;',
  5042. ' var a = 0;',
  5043. ' for (i = 1; i <= 10; i++) {',
  5044. ' $mod.Foo(function () {',
  5045. ' a = 3;',
  5046. ' });',
  5047. ' };',
  5048. '};',
  5049. '']),
  5050. LinesToStr([
  5051. '$mod.DoIt();'
  5052. ]));
  5053. end;
  5054. procedure TTestModule.TestAnonymousProc_AsmDelphi;
  5055. begin
  5056. StartProgram(false);
  5057. Add([
  5058. '{$mode delphi}',
  5059. 'type',
  5060. ' TProc = reference to procedure;',
  5061. ' TFunc = reference to function(x: word): word;',
  5062. 'procedure Run;',
  5063. 'asm',
  5064. 'end;',
  5065. 'procedure Walk(p: TProc; f: TFunc);',
  5066. 'begin',
  5067. ' Walk(procedure asm end, function(b:word): word asm return 1+b; end);',
  5068. 'end;',
  5069. 'begin',
  5070. ' Walk(procedure',
  5071. ' asm',
  5072. ' console.log("a");',
  5073. ' end,',
  5074. ' function(x: word): word asm',
  5075. ' console.log("c");',
  5076. ' end);',
  5077. '']);
  5078. ConvertProgram;
  5079. CheckSource('TestAnonymousProc_AsmDelphi',
  5080. LinesToStr([ // statements
  5081. 'this.Run = function () {',
  5082. '};',
  5083. 'this.Walk = function (p, f) {',
  5084. ' $mod.Walk(function () {',
  5085. ' }, function (b) {',
  5086. ' return 1+b;',
  5087. ' });',
  5088. '};',
  5089. '']),
  5090. LinesToStr([
  5091. '$mod.Walk(function () {',
  5092. ' console.log("a");',
  5093. '}, function (x) {',
  5094. ' console.log("c");',
  5095. '});',
  5096. '']));
  5097. end;
  5098. procedure TTestModule.TestEnum_Name;
  5099. begin
  5100. StartProgram(false);
  5101. Add('type TMyEnum = (Red, Green, Blue);');
  5102. Add('var e: TMyEnum;');
  5103. Add('var f: TMyEnum = Blue;');
  5104. Add('begin');
  5105. Add(' e:=green;');
  5106. Add(' e:=default(TMyEnum);');
  5107. ConvertProgram;
  5108. CheckSource('TestEnum_Name',
  5109. LinesToStr([ // statements
  5110. 'this.TMyEnum = {',
  5111. ' "0":"Red",',
  5112. ' Red:0,',
  5113. ' "1":"Green",',
  5114. ' Green:1,',
  5115. ' "2":"Blue",',
  5116. ' Blue:2',
  5117. ' };',
  5118. 'this.e = 0;',
  5119. 'this.f = this.TMyEnum.Blue;'
  5120. ]),
  5121. LinesToStr([
  5122. '$mod.e=$mod.TMyEnum.Green;',
  5123. '$mod.e=$mod.TMyEnum.Red;'
  5124. ]));
  5125. end;
  5126. procedure TTestModule.TestEnum_Number;
  5127. begin
  5128. Converter.Options:=Converter.Options+[coEnumNumbers];
  5129. StartProgram(false);
  5130. Add('type TMyEnum = (Red, Green);');
  5131. Add('var');
  5132. Add(' e: TMyEnum;');
  5133. Add(' f: TMyEnum = Green;');
  5134. Add(' i: longint;');
  5135. Add('begin');
  5136. Add(' e:=green;');
  5137. Add(' i:=longint(e);');
  5138. ConvertProgram;
  5139. CheckSource('TestEnumNumber',
  5140. LinesToStr([ // statements
  5141. 'this.TMyEnum = {',
  5142. ' "0":"Red",',
  5143. ' Red:0,',
  5144. ' "1":"Green",',
  5145. ' Green:1',
  5146. ' };',
  5147. 'this.e = 0;',
  5148. 'this.f = 1;',
  5149. 'this.i = 0;'
  5150. ]),
  5151. LinesToStr([
  5152. '$mod.e=1;',
  5153. '$mod.i=$mod.e;'
  5154. ]));
  5155. end;
  5156. procedure TTestModule.TestEnum_ConstFail;
  5157. begin
  5158. StartProgram(false);
  5159. Add([
  5160. 'type TMyEnum = (Red = 100, Green = 101);',
  5161. 'var',
  5162. ' e: TMyEnum;',
  5163. ' f: TMyEnum = Green;',
  5164. 'begin',
  5165. ' e:=green;']);
  5166. SetExpectedPasResolverError('not yet implemented: Red:TPasEnumValue [20180126202434] "enum const"',3002);
  5167. ConvertProgram;
  5168. end;
  5169. procedure TTestModule.TestEnum_Functions;
  5170. begin
  5171. StartProgram(false);
  5172. Add([
  5173. 'type TMyEnum = (Red, Green);',
  5174. 'procedure DoIt(var e: TMyEnum; var i: word);',
  5175. 'var',
  5176. ' v: longint;',
  5177. ' s: string;',
  5178. 'begin',
  5179. ' val(s,e,v);',
  5180. ' val(s,e,i);',
  5181. 'end;',
  5182. 'var',
  5183. ' e: TMyEnum;',
  5184. ' i: longint;',
  5185. ' s: string;',
  5186. ' b: boolean;',
  5187. 'begin',
  5188. ' i:=ord(red);',
  5189. ' i:=ord(green);',
  5190. ' i:=ord(e);',
  5191. ' i:=ord(b);',
  5192. ' e:=low(tmyenum);',
  5193. ' e:=low(e);',
  5194. ' b:=low(boolean);',
  5195. ' e:=high(tmyenum);',
  5196. ' e:=high(e);',
  5197. ' b:=high(boolean);',
  5198. ' e:=pred(green);',
  5199. ' e:=pred(e);',
  5200. ' b:=pred(b);',
  5201. ' e:=succ(red);',
  5202. ' e:=succ(e);',
  5203. ' b:=succ(b);',
  5204. ' e:=tmyenum(1);',
  5205. ' e:=tmyenum(i);',
  5206. ' s:=str(e);',
  5207. ' str(e,s);',
  5208. ' str(red,s);',
  5209. ' s:=str(e:3);',
  5210. ' writestr(s,e:3,red);',
  5211. ' val(s,e,i);',
  5212. ' i:=longint(e);']);
  5213. ConvertProgram;
  5214. CheckSource('TestEnum_Functions',
  5215. LinesToStr([ // statements
  5216. 'this.TMyEnum = {',
  5217. ' "0":"Red",',
  5218. ' Red:0,',
  5219. ' "1":"Green",',
  5220. ' Green:1',
  5221. ' };',
  5222. 'this.DoIt = function (e, i) {',
  5223. ' var v = 0;',
  5224. ' var s = "";',
  5225. ' e.set(rtl.valEnum(s, $mod.TMyEnum, function (w) {',
  5226. ' v = w;',
  5227. ' }));',
  5228. ' e.set(rtl.valEnum(s, $mod.TMyEnum, i.set));',
  5229. '};',
  5230. 'this.e = 0;',
  5231. 'this.i = 0;',
  5232. 'this.s = "";',
  5233. 'this.b = false;',
  5234. '']),
  5235. LinesToStr([
  5236. '$mod.i=$mod.TMyEnum.Red;',
  5237. '$mod.i=$mod.TMyEnum.Green;',
  5238. '$mod.i=$mod.e;',
  5239. '$mod.i=$mod.b+0;',
  5240. '$mod.e=$mod.TMyEnum.Red;',
  5241. '$mod.e=$mod.TMyEnum.Red;',
  5242. '$mod.b=false;',
  5243. '$mod.e=$mod.TMyEnum.Green;',
  5244. '$mod.e=$mod.TMyEnum.Green;',
  5245. '$mod.b=true;',
  5246. '$mod.e=$mod.TMyEnum.Green-1;',
  5247. '$mod.e=$mod.e-1;',
  5248. '$mod.b=false;',
  5249. '$mod.e=$mod.TMyEnum.Red+1;',
  5250. '$mod.e=$mod.e+1;',
  5251. '$mod.b=true;',
  5252. '$mod.e=1;',
  5253. '$mod.e=$mod.i;',
  5254. '$mod.s = $mod.TMyEnum[$mod.e];',
  5255. '$mod.s = $mod.TMyEnum[$mod.e];',
  5256. '$mod.s = $mod.TMyEnum[$mod.TMyEnum.Red];',
  5257. '$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3);',
  5258. '$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3)+$mod.TMyEnum[$mod.TMyEnum.Red];',
  5259. '$mod.e = rtl.valEnum($mod.s, $mod.TMyEnum, function (v) {',
  5260. ' $mod.i = v;',
  5261. '});',
  5262. '$mod.i=$mod.e;',
  5263. '']));
  5264. end;
  5265. procedure TTestModule.TestEnumRg_Functions;
  5266. begin
  5267. StartProgram(false);
  5268. Add([
  5269. 'type',
  5270. ' TEnum = (Red, Green, Blue);',
  5271. ' TEnumRg = Green..Blue;',
  5272. 'procedure DoIt(var e: TEnumRg; var i: word);',
  5273. 'var',
  5274. ' v: longint;',
  5275. ' s: string;',
  5276. 'begin',
  5277. ' val(s,e,v);',
  5278. ' val(s,e,i);',
  5279. 'end;',
  5280. 'var',
  5281. ' e: TEnumRg;',
  5282. ' i: longint;',
  5283. ' s: string;',
  5284. 'begin',
  5285. ' i:=ord(green);',
  5286. ' i:=ord(e);',
  5287. ' e:=low(tenumrg);',
  5288. ' e:=low(e);',
  5289. ' e:=high(tenumrg);',
  5290. ' e:=high(e);',
  5291. ' e:=pred(blue);',
  5292. ' e:=pred(e);',
  5293. ' e:=succ(green);',
  5294. ' e:=succ(e);',
  5295. ' e:=tenumrg(1);',
  5296. ' e:=tenumrg(i);',
  5297. ' s:=str(e);',
  5298. ' str(e,s);',
  5299. ' str(red,s);',
  5300. ' s:=str(e:3);',
  5301. ' writestr(s,e:3,blue);',
  5302. ' val(s,e,i);',
  5303. ' i:=longint(e);']);
  5304. ConvertProgram;
  5305. CheckSource('TestEnumRg_Functions',
  5306. LinesToStr([ // statements
  5307. 'this.TEnum = {',
  5308. ' "0":"Red",',
  5309. ' Red:0,',
  5310. ' "1":"Green",',
  5311. ' Green:1,',
  5312. ' "2":"Blue",',
  5313. ' Blue:2',
  5314. ' };',
  5315. 'this.DoIt = function (e, i) {',
  5316. ' var v = 0;',
  5317. ' var s = "";',
  5318. ' e.set(rtl.valEnum(s, $mod.TEnum, function (w) {',
  5319. ' v = w;',
  5320. ' }));',
  5321. ' e.set(rtl.valEnum(s, $mod.TEnum, i.set));',
  5322. '};',
  5323. 'this.e = this.TEnum.Green;',
  5324. 'this.i = 0;',
  5325. 'this.s = "";',
  5326. '']),
  5327. LinesToStr([
  5328. '$mod.i=$mod.TEnum.Green;',
  5329. '$mod.i=$mod.e;',
  5330. '$mod.e=$mod.TEnum.Green;',
  5331. '$mod.e=$mod.TEnum.Green;',
  5332. '$mod.e=$mod.TEnum.Blue;',
  5333. '$mod.e=$mod.TEnum.Blue;',
  5334. '$mod.e=$mod.TEnum.Blue-1;',
  5335. '$mod.e=$mod.e-1;',
  5336. '$mod.e=$mod.TEnum.Green+1;',
  5337. '$mod.e=$mod.e+1;',
  5338. '$mod.e=1;',
  5339. '$mod.e=$mod.i;',
  5340. '$mod.s = $mod.TEnum[$mod.e];',
  5341. '$mod.s = $mod.TEnum[$mod.e];',
  5342. '$mod.s = $mod.TEnum[$mod.TEnum.Red];',
  5343. '$mod.s = rtl.spaceLeft($mod.TEnum[$mod.e], 3);',
  5344. '$mod.s = rtl.spaceLeft($mod.TEnum[$mod.e], 3)+$mod.TEnum[$mod.TEnum.Blue];',
  5345. '$mod.e = rtl.valEnum($mod.s, $mod.TEnum, function (v) {',
  5346. ' $mod.i = v;',
  5347. '});',
  5348. '$mod.i=$mod.e;',
  5349. '']));
  5350. end;
  5351. procedure TTestModule.TestEnum_AsParams;
  5352. begin
  5353. StartProgram(false);
  5354. Add('type TEnum = (Red,Blue);');
  5355. Add('procedure DoIt(vG: TEnum; const vH: TEnum; var vI: TEnum);');
  5356. Add('var vJ: TEnum;');
  5357. Add('begin');
  5358. Add(' vg:=vg;');
  5359. Add(' vj:=vh;');
  5360. Add(' vi:=vi;');
  5361. Add(' doit(vg,vg,vg);');
  5362. Add(' doit(vh,vh,vj);');
  5363. Add(' doit(vi,vi,vi);');
  5364. Add(' doit(vj,vj,vj);');
  5365. Add('end;');
  5366. Add('var i: TEnum;');
  5367. Add('begin');
  5368. Add(' doit(i,i,i);');
  5369. ConvertProgram;
  5370. CheckSource('TestEnum_AsParams',
  5371. LinesToStr([ // statements
  5372. 'this.TEnum = {',
  5373. ' "0": "Red",',
  5374. ' Red: 0,',
  5375. ' "1": "Blue",',
  5376. ' Blue: 1',
  5377. '};',
  5378. 'this.DoIt = function (vG,vH,vI) {',
  5379. ' var vJ = 0;',
  5380. ' vG = vG;',
  5381. ' vJ = vH;',
  5382. ' vI.set(vI.get());',
  5383. ' $mod.DoIt(vG, vG, {',
  5384. ' get: function () {',
  5385. ' return vG;',
  5386. ' },',
  5387. ' set: function (v) {',
  5388. ' vG = v;',
  5389. ' }',
  5390. ' });',
  5391. ' $mod.DoIt(vH, vH, {',
  5392. ' get: function () {',
  5393. ' return vJ;',
  5394. ' },',
  5395. ' set: function (v) {',
  5396. ' vJ = v;',
  5397. ' }',
  5398. ' });',
  5399. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  5400. ' $mod.DoIt(vJ, vJ, {',
  5401. ' get: function () {',
  5402. ' return vJ;',
  5403. ' },',
  5404. ' set: function (v) {',
  5405. ' vJ = v;',
  5406. ' }',
  5407. ' });',
  5408. '};',
  5409. 'this.i = 0;'
  5410. ]),
  5411. LinesToStr([
  5412. '$mod.DoIt($mod.i,$mod.i,{',
  5413. ' p: $mod,',
  5414. ' get: function () {',
  5415. ' return this.p.i;',
  5416. ' },',
  5417. ' set: function (v) {',
  5418. ' this.p.i = v;',
  5419. ' }',
  5420. '});'
  5421. ]));
  5422. end;
  5423. procedure TTestModule.TestEnumRange_Array;
  5424. begin
  5425. StartProgram(false);
  5426. Add([
  5427. 'type',
  5428. ' TEnum = (Red, Green, Blue);',
  5429. ' TEnumRg = green..blue;',
  5430. ' TArr = array[TEnumRg] of byte;',
  5431. ' TArr2 = array[green..blue] of byte;',
  5432. 'var',
  5433. ' a: TArr;',
  5434. ' b: TArr = (3,4);',
  5435. ' c: TArr2 = (5,6);',
  5436. 'begin',
  5437. ' a[green] := b[blue];',
  5438. ' c[green] := c[blue];',
  5439. '']);
  5440. ConvertProgram;
  5441. CheckSource('TestEnumRange_Array',
  5442. LinesToStr([ // statements
  5443. 'this.TEnum = {',
  5444. ' "0": "Red",',
  5445. ' Red: 0,',
  5446. ' "1": "Green",',
  5447. ' Green: 1,',
  5448. ' "2": "Blue",',
  5449. ' Blue: 2',
  5450. '};',
  5451. 'this.a = rtl.arraySetLength(null, 0, 2);',
  5452. 'this.b = [3, 4];',
  5453. 'this.c = [5, 6];',
  5454. '']),
  5455. LinesToStr([
  5456. ' $mod.a[$mod.TEnum.Green - 1] = $mod.b[$mod.TEnum.Blue - 1];',
  5457. ' $mod.c[$mod.TEnum.Green - 1] = $mod.c[$mod.TEnum.Blue - 1];',
  5458. '']));
  5459. end;
  5460. procedure TTestModule.TestEnum_ForIn;
  5461. begin
  5462. StartProgram(false);
  5463. Add([
  5464. 'type',
  5465. ' TEnum = (Red, Green, Blue);',
  5466. ' TEnumRg = green..blue;',
  5467. ' TArr = array[TEnum] of byte;',
  5468. ' TArrRg = array[TEnumRg] of byte;',
  5469. 'var',
  5470. ' e: TEnum;',
  5471. ' a1: TArr = (3,4,5);',
  5472. ' a2: TArrRg = (11,12);',
  5473. ' b: byte;',
  5474. 'begin',
  5475. ' for e in TEnum do ;',
  5476. ' for e in TEnumRg do ;',
  5477. ' for e in TArr do ;',
  5478. ' for e in TArrRg do ;',
  5479. ' for b in a1 do ;',
  5480. ' for b in a2 do ;',
  5481. '']);
  5482. ConvertProgram;
  5483. CheckSource('TestEnum_ForIn',
  5484. LinesToStr([ // statements
  5485. 'this.TEnum = {',
  5486. ' "0": "Red",',
  5487. ' Red: 0,',
  5488. ' "1": "Green",',
  5489. ' Green: 1,',
  5490. ' "2": "Blue",',
  5491. ' Blue: 2',
  5492. '};',
  5493. 'this.e = 0;',
  5494. 'this.a1 = [3, 4, 5];',
  5495. 'this.a2 = [11, 12];',
  5496. 'this.b = 0;',
  5497. '']),
  5498. LinesToStr([
  5499. ' for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  5500. ' for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  5501. ' for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  5502. ' for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  5503. ' for (var $in = $mod.a1, $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) $mod.b = $in[$l];',
  5504. ' for (var $in1 = $mod.a2, $l1 = 0, $end1 = rtl.length($in1) - 1; $l1 <= $end1; $l1++) $mod.b = $in1[$l1];',
  5505. '']));
  5506. end;
  5507. procedure TTestModule.TestEnum_ScopedNumber;
  5508. begin
  5509. Converter.Options:=Converter.Options+[coEnumNumbers];
  5510. StartProgram(false);
  5511. Add([
  5512. 'type',
  5513. ' TEnum = (Red, Green);',
  5514. 'var',
  5515. ' e: TEnum;',
  5516. 'begin',
  5517. ' e:=TEnum.Green;',
  5518. '']);
  5519. ConvertProgram;
  5520. CheckSource('TestEnum_ScopedNumber',
  5521. LinesToStr([ // statements
  5522. 'this.TEnum = {',
  5523. ' "0": "Red",',
  5524. ' Red: 0,',
  5525. ' "1": "Green",',
  5526. ' Green: 1',
  5527. '};',
  5528. 'this.e = 0;',
  5529. '']),
  5530. LinesToStr([
  5531. '$mod.e = 1;']));
  5532. end;
  5533. procedure TTestModule.TestEnum_InFunction;
  5534. begin
  5535. StartProgram(false);
  5536. Add([
  5537. 'const TEnum = 3;',
  5538. 'procedure DoIt;',
  5539. 'type',
  5540. ' TEnum = (Red, Green, Blue);',
  5541. ' procedure Sub;',
  5542. ' type',
  5543. ' TEnumSub = (Left, Right);',
  5544. ' var',
  5545. ' es: TEnumSub;',
  5546. ' begin',
  5547. ' es:=Left;',
  5548. ' end;',
  5549. 'var',
  5550. ' e, e2: TEnum;',
  5551. 'begin',
  5552. ' if e in [red,blue] then e2:=e;',
  5553. 'end;',
  5554. 'begin']);
  5555. ConvertProgram;
  5556. CheckSource('TestEnum_InFunction',
  5557. LinesToStr([ // statements
  5558. 'this.TEnum = 3;',
  5559. 'var TEnum$1 = {',
  5560. ' "0":"Red",',
  5561. ' Red:0,',
  5562. ' "1":"Green",',
  5563. ' Green:1,',
  5564. ' "2":"Blue",',
  5565. ' Blue:2',
  5566. ' };',
  5567. 'var TEnumSub = {',
  5568. ' "0": "Left",',
  5569. ' Left: 0,',
  5570. ' "1": "Right",',
  5571. ' Right: 1',
  5572. '};',
  5573. 'this.DoIt = function () {',
  5574. ' function Sub() {',
  5575. ' var es = 0;',
  5576. ' es = TEnumSub.Left;',
  5577. ' };',
  5578. ' var e = 0;',
  5579. ' var e2 = 0;',
  5580. ' if (e in rtl.createSet(TEnum$1.Red, TEnum$1.Blue)) e2 = e;',
  5581. '};',
  5582. '']),
  5583. LinesToStr([
  5584. '']));
  5585. end;
  5586. procedure TTestModule.TestSet_Enum;
  5587. begin
  5588. StartProgram(false);
  5589. Add([
  5590. 'type',
  5591. ' TColor = (Red, Green, Blue);',
  5592. ' TColors = set of TColor;',
  5593. 'var',
  5594. ' c: TColor;',
  5595. ' s: TColors;',
  5596. ' t: TColors = [];',
  5597. ' u: TColors = [Red];',
  5598. 'begin',
  5599. ' s:=[];',
  5600. ' s:=[Green];',
  5601. ' s:=[Green,Blue];',
  5602. ' s:=[Red..Blue];',
  5603. ' s:=[Red,Green..Blue];',
  5604. ' s:=[Red,c];',
  5605. ' s:=t;',
  5606. ' s:=default(TColors);',
  5607. '']);
  5608. ConvertProgram;
  5609. CheckSource('TestSet',
  5610. LinesToStr([ // statements
  5611. 'this.TColor = {',
  5612. ' "0":"Red",',
  5613. ' Red:0,',
  5614. ' "1":"Green",',
  5615. ' Green:1,',
  5616. ' "2":"Blue",',
  5617. ' Blue:2',
  5618. ' };',
  5619. 'this.c = 0;',
  5620. 'this.s = {};',
  5621. 'this.t = {};',
  5622. 'this.u = rtl.createSet(this.TColor.Red);'
  5623. ]),
  5624. LinesToStr([
  5625. '$mod.s={};',
  5626. '$mod.s=rtl.createSet($mod.TColor.Green);',
  5627. '$mod.s=rtl.createSet($mod.TColor.Green,$mod.TColor.Blue);',
  5628. '$mod.s=rtl.createSet(null,$mod.TColor.Red,$mod.TColor.Blue);',
  5629. '$mod.s=rtl.createSet($mod.TColor.Red,null,$mod.TColor.Green,$mod.TColor.Blue);',
  5630. '$mod.s=rtl.createSet($mod.TColor.Red,$mod.c);',
  5631. '$mod.s=rtl.refSet($mod.t);',
  5632. '$mod.s={};',
  5633. '']));
  5634. end;
  5635. procedure TTestModule.TestSet_Operators;
  5636. begin
  5637. StartProgram(false);
  5638. Add('type');
  5639. Add(' TColor = (Red, Green, Blue);');
  5640. Add(' TColors = set of tcolor;');
  5641. Add('var');
  5642. Add(' vC: TColor;');
  5643. Add(' vS: TColors;');
  5644. Add(' vT: TColors;');
  5645. Add(' vU: TColors;');
  5646. Add(' B: boolean;');
  5647. Add('begin');
  5648. Add(' include(vs,green);');
  5649. Add(' exclude(vs,vc);');
  5650. Add(' vs:=vt+vu;');
  5651. Add(' vs:=vt+[red];');
  5652. Add(' vs:=[red]+vt;');
  5653. Add(' vs:=[red]+[green];');
  5654. Add(' vs:=vt-vu;');
  5655. Add(' vs:=vt-[red];');
  5656. Add(' vs:=[red]-vt;');
  5657. Add(' vs:=[red]-[green];');
  5658. Add(' vs:=vt*vu;');
  5659. Add(' vs:=vt*[red];');
  5660. Add(' vs:=[red]*vt;');
  5661. Add(' vs:=[red]*[green];');
  5662. Add(' vs:=vt><vu;');
  5663. Add(' vs:=vt><[red];');
  5664. Add(' vs:=[red]><vt;');
  5665. Add(' vs:=[red]><[green];');
  5666. Add(' b:=vt=vu;');
  5667. Add(' b:=vt=[red];');
  5668. Add(' b:=[red]=vt;');
  5669. Add(' b:=[red]=[green];');
  5670. Add(' b:=vt<>vu;');
  5671. Add(' b:=vt<>[red];');
  5672. Add(' b:=[red]<>vt;');
  5673. Add(' b:=[red]<>[green];');
  5674. Add(' b:=vt<=vu;');
  5675. Add(' b:=vt<=[red];');
  5676. Add(' b:=[red]<=vt;');
  5677. Add(' b:=[red]<=[green];');
  5678. Add(' b:=vt>=vu;');
  5679. Add(' b:=vt>=[red];');
  5680. Add(' b:=[red]>=vt;');
  5681. Add(' b:=[red]>=[green];');
  5682. ConvertProgram;
  5683. CheckSource('TestSet_Operators',
  5684. LinesToStr([ // statements
  5685. 'this.TColor = {',
  5686. ' "0":"Red",',
  5687. ' Red:0,',
  5688. ' "1":"Green",',
  5689. ' Green:1,',
  5690. ' "2":"Blue",',
  5691. ' Blue:2',
  5692. ' };',
  5693. 'this.vC = 0;',
  5694. 'this.vS = {};',
  5695. 'this.vT = {};',
  5696. 'this.vU = {};',
  5697. 'this.B = false;'
  5698. ]),
  5699. LinesToStr([
  5700. '$mod.vS = rtl.includeSet($mod.vS,$mod.TColor.Green);',
  5701. '$mod.vS = rtl.excludeSet($mod.vS,$mod.vC);',
  5702. '$mod.vS = rtl.unionSet($mod.vT, $mod.vU);',
  5703. '$mod.vS = rtl.unionSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5704. '$mod.vS = rtl.unionSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5705. '$mod.vS = rtl.unionSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5706. '$mod.vS = rtl.diffSet($mod.vT, $mod.vU);',
  5707. '$mod.vS = rtl.diffSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5708. '$mod.vS = rtl.diffSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5709. '$mod.vS = rtl.diffSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5710. '$mod.vS = rtl.intersectSet($mod.vT, $mod.vU);',
  5711. '$mod.vS = rtl.intersectSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5712. '$mod.vS = rtl.intersectSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5713. '$mod.vS = rtl.intersectSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5714. '$mod.vS = rtl.symDiffSet($mod.vT, $mod.vU);',
  5715. '$mod.vS = rtl.symDiffSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5716. '$mod.vS = rtl.symDiffSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5717. '$mod.vS = rtl.symDiffSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5718. '$mod.B = rtl.eqSet($mod.vT, $mod.vU);',
  5719. '$mod.B = rtl.eqSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5720. '$mod.B = rtl.eqSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5721. '$mod.B = rtl.eqSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5722. '$mod.B = rtl.neSet($mod.vT, $mod.vU);',
  5723. '$mod.B = rtl.neSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5724. '$mod.B = rtl.neSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5725. '$mod.B = rtl.neSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5726. '$mod.B = rtl.leSet($mod.vT, $mod.vU);',
  5727. '$mod.B = rtl.leSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5728. '$mod.B = rtl.leSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5729. '$mod.B = rtl.leSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5730. '$mod.B = rtl.geSet($mod.vT, $mod.vU);',
  5731. '$mod.B = rtl.geSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5732. '$mod.B = rtl.geSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5733. '$mod.B = rtl.geSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5734. '']));
  5735. end;
  5736. procedure TTestModule.TestSet_Operator_In;
  5737. begin
  5738. StartProgram(false);
  5739. Add([
  5740. 'type',
  5741. ' TColor = (Red, Green, Blue);',
  5742. ' TColors = set of tcolor;',
  5743. ' TColorRg = green..blue;',
  5744. 'var',
  5745. ' vC: tcolor;',
  5746. ' vT: tcolors;',
  5747. ' B: boolean;',
  5748. ' rg: TColorRg;',
  5749. 'begin',
  5750. ' b:=red in vt;',
  5751. ' b:=vc in vt;',
  5752. ' b:=green in [red..blue];',
  5753. ' b:=vc in [red..blue];',
  5754. ' ',
  5755. ' if red in vt then ;',
  5756. ' while vC in vt do ;',
  5757. ' repeat',
  5758. ' until vC in vt;',
  5759. ' if rg in [green..blue] then ;',
  5760. '']);
  5761. ConvertProgram;
  5762. CheckSource('TestSet_Operator_In',
  5763. LinesToStr([ // statements
  5764. 'this.TColor = {',
  5765. ' "0":"Red",',
  5766. ' Red:0,',
  5767. ' "1":"Green",',
  5768. ' Green:1,',
  5769. ' "2":"Blue",',
  5770. ' Blue:2',
  5771. ' };',
  5772. 'this.vC = 0;',
  5773. 'this.vT = {};',
  5774. 'this.B = false;',
  5775. 'this.rg = this.TColor.Green;',
  5776. '']),
  5777. LinesToStr([
  5778. '$mod.B = $mod.TColor.Red in $mod.vT;',
  5779. '$mod.B = $mod.vC in $mod.vT;',
  5780. '$mod.B = $mod.TColor.Green in rtl.createSet(null, $mod.TColor.Red, $mod.TColor.Blue);',
  5781. '$mod.B = $mod.vC in rtl.createSet(null, $mod.TColor.Red, $mod.TColor.Blue);',
  5782. 'if ($mod.TColor.Red in $mod.vT) ;',
  5783. 'while ($mod.vC in $mod.vT) {',
  5784. '};',
  5785. 'do {',
  5786. '} while (!($mod.vC in $mod.vT));',
  5787. 'if ($mod.rg in rtl.createSet(null, $mod.TColor.Green, $mod.TColor.Blue)) ;',
  5788. '']));
  5789. end;
  5790. procedure TTestModule.TestSet_Functions;
  5791. begin
  5792. StartProgram(false);
  5793. Add('type');
  5794. Add(' TMyEnum = (Red, Green);');
  5795. Add(' TMyEnums = set of TMyEnum;');
  5796. Add('var');
  5797. Add(' e: TMyEnum;');
  5798. Add(' s: TMyEnums;');
  5799. Add('begin');
  5800. Add(' e:=Low(TMyEnums);');
  5801. Add(' e:=Low(s);');
  5802. Add(' e:=High(TMyEnums);');
  5803. Add(' e:=High(s);');
  5804. ConvertProgram;
  5805. CheckSource('TestSetFunctions',
  5806. LinesToStr([ // statements
  5807. 'this.TMyEnum = {',
  5808. ' "0":"Red",',
  5809. ' Red:0,',
  5810. ' "1":"Green",',
  5811. ' Green:1',
  5812. ' };',
  5813. 'this.e = 0;',
  5814. 'this.s = {};'
  5815. ]),
  5816. LinesToStr([
  5817. '$mod.e=$mod.TMyEnum.Red;',
  5818. '$mod.e=$mod.TMyEnum.Red;',
  5819. '$mod.e=$mod.TMyEnum.Green;',
  5820. '$mod.e=$mod.TMyEnum.Green;',
  5821. '']));
  5822. end;
  5823. procedure TTestModule.TestSet_PassAsArgClone;
  5824. begin
  5825. StartProgram(false);
  5826. Add('type');
  5827. Add(' TMyEnum = (Red, Green);');
  5828. Add(' TMyEnums = set of TMyEnum;');
  5829. Add('procedure DoDefault(s: tmyenums); begin end;');
  5830. Add('procedure DoConst(const s: tmyenums); begin end;');
  5831. Add('var');
  5832. Add(' aSet: tmyenums;');
  5833. Add('begin');
  5834. Add(' dodefault(aset);');
  5835. Add(' doconst(aset);');
  5836. ConvertProgram;
  5837. CheckSource('TestSetFunctions',
  5838. LinesToStr([ // statements
  5839. 'this.TMyEnum = {',
  5840. ' "0":"Red",',
  5841. ' Red:0,',
  5842. ' "1":"Green",',
  5843. ' Green:1',
  5844. ' };',
  5845. 'this.DoDefault = function (s) {',
  5846. '};',
  5847. 'this.DoConst = function (s) {',
  5848. '};',
  5849. 'this.aSet = {};'
  5850. ]),
  5851. LinesToStr([
  5852. '$mod.DoDefault(rtl.refSet($mod.aSet));',
  5853. '$mod.DoConst($mod.aSet);',
  5854. '']));
  5855. end;
  5856. procedure TTestModule.TestSet_AsParams;
  5857. begin
  5858. StartProgram(false);
  5859. Add([
  5860. 'type TEnum = (Red,Blue);',
  5861. 'type TEnums = set of TEnum;',
  5862. 'function DoIt(vG: TEnums; const vH: TEnums; var vI: TEnums): TEnums;',
  5863. 'var vJ: TEnums;',
  5864. 'begin',
  5865. ' Include(vg,red);',
  5866. ' Include(result,blue);',
  5867. ' vg:=vg;',
  5868. ' vj:=vh;',
  5869. ' vi:=vi;',
  5870. ' doit(vg,vg,vg);',
  5871. ' doit(vh,vh,vj);',
  5872. ' doit(vi,vi,vi);',
  5873. ' doit(vj,vj,vj);',
  5874. 'end;',
  5875. 'var i: TEnums;',
  5876. 'begin',
  5877. ' doit(i,i,i);']);
  5878. ConvertProgram;
  5879. CheckSource('TestSet_AsParams',
  5880. LinesToStr([ // statements
  5881. 'this.TEnum = {',
  5882. ' "0": "Red",',
  5883. ' Red: 0,',
  5884. ' "1": "Blue",',
  5885. ' Blue: 1',
  5886. '};',
  5887. 'this.DoIt = function (vG,vH,vI) {',
  5888. ' var Result = {};',
  5889. ' var vJ = {};',
  5890. ' vG = rtl.includeSet(vG, $mod.TEnum.Red);',
  5891. ' Result = rtl.includeSet(Result, $mod.TEnum.Blue);',
  5892. ' vG = rtl.refSet(vG);',
  5893. ' vJ = rtl.refSet(vH);',
  5894. ' vI.set(rtl.refSet(vI.get()));',
  5895. ' $mod.DoIt(rtl.refSet(vG), vG, {',
  5896. ' get: function () {',
  5897. ' return vG;',
  5898. ' },',
  5899. ' set: function (v) {',
  5900. ' vG = v;',
  5901. ' }',
  5902. ' });',
  5903. ' $mod.DoIt(rtl.refSet(vH), vH, {',
  5904. ' get: function () {',
  5905. ' return vJ;',
  5906. ' },',
  5907. ' set: function (v) {',
  5908. ' vJ = v;',
  5909. ' }',
  5910. ' });',
  5911. ' $mod.DoIt(rtl.refSet(vI.get()), vI.get(), vI);',
  5912. ' $mod.DoIt(rtl.refSet(vJ), vJ, {',
  5913. ' get: function () {',
  5914. ' return vJ;',
  5915. ' },',
  5916. ' set: function (v) {',
  5917. ' vJ = v;',
  5918. ' }',
  5919. ' });',
  5920. ' return Result;',
  5921. '};',
  5922. 'this.i = {};'
  5923. ]),
  5924. LinesToStr([
  5925. '$mod.DoIt(rtl.refSet($mod.i),$mod.i,{',
  5926. ' p: $mod,',
  5927. ' get: function () {',
  5928. ' return this.p.i;',
  5929. ' },',
  5930. ' set: function (v) {',
  5931. ' this.p.i = v;',
  5932. ' }',
  5933. '});'
  5934. ]));
  5935. end;
  5936. procedure TTestModule.TestSet_Property;
  5937. begin
  5938. StartProgram(false);
  5939. Add('type');
  5940. Add(' TEnum = (Red,Blue);');
  5941. Add(' TEnums = set of TEnum;');
  5942. Add(' TObject = class');
  5943. Add(' function GetColors: TEnums; external name ''GetColors'';');
  5944. Add(' procedure SetColors(const Value: TEnums); external name ''SetColors'';');
  5945. Add(' property Colors: TEnums read GetColors write SetColors;');
  5946. Add(' end;');
  5947. Add('procedure DoIt(i: TEnums; const j: TEnums; var k: TEnums; out l: TEnums);');
  5948. Add('begin end;');
  5949. Add('var Obj: TObject;');
  5950. Add('begin');
  5951. Add(' Include(Obj.Colors,Red);');
  5952. Add(' Exclude(Obj.Colors,Red);');
  5953. //Add(' DoIt(Obj.Colors,Obj.Colors,Obj.Colors,Obj.Colors);');
  5954. ConvertProgram;
  5955. CheckSource('TestSet_Property',
  5956. LinesToStr([ // statements
  5957. 'this.TEnum = {',
  5958. ' "0": "Red",',
  5959. ' Red: 0,',
  5960. ' "1": "Blue",',
  5961. ' Blue: 1',
  5962. '};',
  5963. 'rtl.createClass(this, "TObject", null, function () {',
  5964. ' this.$init = function () {',
  5965. ' };',
  5966. ' this.$final = function () {',
  5967. ' };',
  5968. '});',
  5969. 'this.DoIt = function (i, j, k, l) {',
  5970. '};',
  5971. 'this.Obj = null;',
  5972. '']),
  5973. LinesToStr([
  5974. '$mod.Obj.SetColors(rtl.includeSet($mod.Obj.GetColors(), $mod.TEnum.Red));',
  5975. '$mod.Obj.SetColors(rtl.excludeSet($mod.Obj.GetColors(), $mod.TEnum.Red));',
  5976. '']));
  5977. end;
  5978. procedure TTestModule.TestSet_EnumConst;
  5979. begin
  5980. StartProgram(false);
  5981. Add([
  5982. 'type',
  5983. ' TEnum = (Red,Blue);',
  5984. ' TEnums = set of TEnum;',
  5985. 'const',
  5986. ' Orange = red;',
  5987. 'var',
  5988. ' Enum: tenum;',
  5989. ' Enums: tenums;',
  5990. 'begin',
  5991. ' Include(enums,orange);',
  5992. ' Exclude(enums,orange);',
  5993. ' if orange in enums then;',
  5994. ' if orange in [orange,red] then;']);
  5995. ConvertProgram;
  5996. CheckSource('TestSet_EnumConst',
  5997. LinesToStr([ // statements
  5998. 'this.TEnum = {',
  5999. ' "0": "Red",',
  6000. ' Red: 0,',
  6001. ' "1": "Blue",',
  6002. ' Blue: 1',
  6003. '};',
  6004. 'this.Orange = this.TEnum.Red;',
  6005. 'this.Enum = 0;',
  6006. 'this.Enums = {};',
  6007. '']),
  6008. LinesToStr([
  6009. '$mod.Enums = rtl.includeSet($mod.Enums, $mod.TEnum.Red);',
  6010. '$mod.Enums = rtl.excludeSet($mod.Enums, $mod.TEnum.Red);',
  6011. 'if ($mod.TEnum.Red in $mod.Enums) ;',
  6012. 'if ($mod.TEnum.Red in rtl.createSet($mod.TEnum.Red, $mod.TEnum.Red)) ;',
  6013. '']));
  6014. end;
  6015. procedure TTestModule.TestSet_IntConst;
  6016. begin
  6017. StartProgram(false);
  6018. Add([
  6019. 'type',
  6020. ' TEnums = set of Byte;',
  6021. 'const',
  6022. ' Orange = 0;',
  6023. 'var',
  6024. ' Enum: byte;',
  6025. ' Enums: tenums;',
  6026. 'begin',
  6027. ' Enums:=[];',
  6028. ' Enums:=[0];',
  6029. ' Enums:=[1..2];',
  6030. //' Include(enums,orange);',
  6031. //' Exclude(enums,orange);',
  6032. ' if orange in enums then;',
  6033. ' if orange in [orange,1] then;']);
  6034. ConvertProgram;
  6035. CheckSource('TestSet_IntConst',
  6036. LinesToStr([ // statements
  6037. 'this.Orange = 0;',
  6038. 'this.Enum = 0;',
  6039. 'this.Enums = {};',
  6040. '']),
  6041. LinesToStr([
  6042. '$mod.Enums = {};',
  6043. '$mod.Enums = rtl.createSet(0);',
  6044. '$mod.Enums = rtl.createSet(null, 1, 2);',
  6045. 'if (0 in $mod.Enums) ;',
  6046. 'if (0 in rtl.createSet(0, 1)) ;',
  6047. '']));
  6048. end;
  6049. procedure TTestModule.TestSet_AnonymousEnumType;
  6050. begin
  6051. StartProgram(false);
  6052. Add('type');
  6053. Add(' TFlags = set of (red, green);');
  6054. Add('const');
  6055. Add(' favorite = red;');
  6056. Add('var');
  6057. Add(' f: TFlags;');
  6058. Add(' i: longint;');
  6059. Add('begin');
  6060. Add(' Include(f,red);');
  6061. Add(' Include(f,favorite);');
  6062. Add(' i:=ord(red);');
  6063. Add(' i:=ord(favorite);');
  6064. Add(' i:=ord(low(TFlags));');
  6065. Add(' i:=ord(low(f));');
  6066. Add(' i:=ord(low(favorite));');
  6067. Add(' i:=ord(high(TFlags));');
  6068. Add(' i:=ord(high(f));');
  6069. Add(' i:=ord(high(favorite));');
  6070. Add(' f:=[green,favorite];');
  6071. ConvertProgram;
  6072. CheckSource('TestSet_AnonymousEnumType',
  6073. LinesToStr([ // statements
  6074. 'this.TFlags$a = {',
  6075. ' "0": "red",',
  6076. ' red: 0,',
  6077. ' "1": "green",',
  6078. ' green: 1',
  6079. '};',
  6080. 'this.favorite = this.TFlags$a.red;',
  6081. 'this.f = {};',
  6082. 'this.i = 0;',
  6083. '']),
  6084. LinesToStr([
  6085. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  6086. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  6087. '$mod.i = $mod.TFlags$a.red;',
  6088. '$mod.i = $mod.TFlags$a.red;',
  6089. '$mod.i = $mod.TFlags$a.red;',
  6090. '$mod.i = $mod.TFlags$a.red;',
  6091. '$mod.i = $mod.TFlags$a.red;',
  6092. '$mod.i = $mod.TFlags$a.green;',
  6093. '$mod.i = $mod.TFlags$a.green;',
  6094. '$mod.i = $mod.TFlags$a.green;',
  6095. '$mod.f = rtl.createSet($mod.TFlags$a.green, $mod.TFlags$a.red);',
  6096. '']));
  6097. end;
  6098. procedure TTestModule.TestSet_AnonymousEnumTypeChar;
  6099. begin
  6100. exit;
  6101. StartProgram(false);
  6102. Add([
  6103. 'type',
  6104. ' TAtoZ = ''A''..''Z'';',
  6105. ' TSetOfAZ = set of TAtoZ;',
  6106. 'var',
  6107. ' c: char;',
  6108. ' a: TAtoZ;',
  6109. ' s: TSetOfAZ = [''P'',''A''];',
  6110. ' i: longint;',
  6111. 'begin',
  6112. ' Include(s,''S'');',
  6113. ' Include(s,c);',
  6114. ' Include(s,a);',
  6115. ' c:=low(TAtoZ);',
  6116. ' i:=ord(low(TAtoZ));',
  6117. ' a:=high(TAtoZ);',
  6118. ' a:=high(TSetOfAtoZ);',
  6119. ' s:=[a,c,''M''];',
  6120. '']);
  6121. ConvertProgram;
  6122. CheckSource('TestSet_AnonymousEnumTypeChar',
  6123. LinesToStr([ // statements
  6124. '']),
  6125. LinesToStr([
  6126. '']));
  6127. end;
  6128. procedure TTestModule.TestSet_ConstEnum;
  6129. begin
  6130. StartProgram(false);
  6131. Add([
  6132. 'type',
  6133. ' TEnum = (red,blue,green);',
  6134. ' TEnums = set of TEnum;',
  6135. 'const',
  6136. ' teAny = [low(TEnum)..high(TEnum)];',
  6137. ' teRedBlue = [low(TEnum)..pred(high(TEnum))];',
  6138. 'var',
  6139. ' e: TEnum;',
  6140. ' s: TEnums;',
  6141. 'begin',
  6142. ' if blue in teAny then;',
  6143. ' if blue in teAny+[e] then;',
  6144. ' if blue in teAny+teRedBlue then;',
  6145. ' if e in [red,blue] then;',
  6146. ' s:=teAny;',
  6147. ' s:=teAny+[e];',
  6148. ' s:=[e]+teAny;',
  6149. ' s:=teAny+teRedBlue;',
  6150. ' s:=teAny+teRedBlue+[e];',
  6151. '']);
  6152. ConvertProgram;
  6153. CheckSource('TestSet_ConstEnum',
  6154. LinesToStr([ // statements
  6155. 'this.TEnum = {',
  6156. ' "0": "red",',
  6157. ' red: 0,',
  6158. ' "1": "blue",',
  6159. ' blue: 1,',
  6160. ' "2": "green",',
  6161. ' green: 2',
  6162. '};',
  6163. 'this.teAny = rtl.createSet(null, this.TEnum.red, this.TEnum.green);',
  6164. 'this.teRedBlue = rtl.createSet(null, this.TEnum.red, this.TEnum.green - 1);',
  6165. 'this.e = 0;',
  6166. 'this.s = {};',
  6167. '']),
  6168. LinesToStr([
  6169. 'if ($mod.TEnum.blue in $mod.teAny) ;',
  6170. 'if ($mod.TEnum.blue in rtl.unionSet($mod.teAny, rtl.createSet($mod.e))) ;',
  6171. 'if ($mod.TEnum.blue in rtl.unionSet($mod.teAny, $mod.teRedBlue)) ;',
  6172. 'if ($mod.e in rtl.createSet($mod.TEnum.red, $mod.TEnum.blue)) ;',
  6173. '$mod.s = rtl.refSet($mod.teAny);',
  6174. '$mod.s = rtl.unionSet($mod.teAny, rtl.createSet($mod.e));',
  6175. '$mod.s = rtl.unionSet(rtl.createSet($mod.e), $mod.teAny);',
  6176. '$mod.s = rtl.unionSet($mod.teAny, $mod.teRedBlue);',
  6177. '$mod.s = rtl.unionSet(rtl.unionSet($mod.teAny, $mod.teRedBlue), rtl.createSet($mod.e));',
  6178. '']));
  6179. end;
  6180. procedure TTestModule.TestSet_ConstChar;
  6181. begin
  6182. StartProgram(false);
  6183. Add([
  6184. 'const',
  6185. ' LowChars = [''a''..''z''];',
  6186. ' Chars = LowChars+[''A''..''Z''];',
  6187. ' sc = [''А'', ''Я''];',
  6188. 'var',
  6189. ' c: char;',
  6190. ' s: string;',
  6191. 'begin',
  6192. ' if c in lowchars then ;',
  6193. ' if ''a'' in lowchars then ;',
  6194. ' if s[1] in lowchars then ;',
  6195. ' if c in chars then ;',
  6196. ' if c in [''a''..''z'',''_''] then ;',
  6197. ' if ''b'' in [''a''..''z'',''_''] then ;',
  6198. ' if ''Я'' in sc then ;',
  6199. ' if 3=ord('' '') then ;',
  6200. '']);
  6201. ConvertProgram;
  6202. CheckSource('TestSet_ConstChar',
  6203. LinesToStr([ // statements
  6204. 'this.LowChars = rtl.createSet(null, 97, 122);',
  6205. 'this.Chars = rtl.unionSet(this.LowChars, rtl.createSet(null, 65, 90));',
  6206. 'this.sc = rtl.createSet(1040, 1071);',
  6207. 'this.c = "";',
  6208. 'this.s = "";',
  6209. '']),
  6210. LinesToStr([
  6211. 'if ($mod.c.charCodeAt() in $mod.LowChars) ;',
  6212. 'if (97 in $mod.LowChars) ;',
  6213. 'if ($mod.s.charCodeAt(0) in $mod.LowChars) ;',
  6214. 'if ($mod.c.charCodeAt() in $mod.Chars) ;',
  6215. 'if ($mod.c.charCodeAt() in rtl.createSet(null, 97, 122, 95)) ;',
  6216. 'if (98 in rtl.createSet(null, 97, 122, 95)) ;',
  6217. 'if (1071 in $mod.sc) ;',
  6218. 'if (3 === 32) ;',
  6219. '']));
  6220. end;
  6221. procedure TTestModule.TestSet_ConstInt;
  6222. begin
  6223. StartProgram(false);
  6224. Add([
  6225. 'const',
  6226. ' Months = [1..12];',
  6227. ' Mirror = [-12..-1]+Months;',
  6228. 'var',
  6229. ' i: smallint;',
  6230. 'begin',
  6231. ' if 3 in Months then;',
  6232. ' if i in Months+[i] then;',
  6233. ' if i in Months+Mirror then;',
  6234. ' if i in [4..6,8] then;',
  6235. '']);
  6236. ConvertProgram;
  6237. CheckSource('TestSet_ConstInt',
  6238. LinesToStr([ // statements
  6239. 'this.Months = rtl.createSet(null, 1, 12);',
  6240. 'this.Mirror = rtl.unionSet(rtl.createSet(null, -12, -1), this.Months);',
  6241. 'this.i = 0;',
  6242. '']),
  6243. LinesToStr([
  6244. 'if (3 in $mod.Months) ;',
  6245. 'if ($mod.i in rtl.unionSet($mod.Months, rtl.createSet($mod.i))) ;',
  6246. 'if ($mod.i in rtl.unionSet($mod.Months, $mod.Mirror)) ;',
  6247. 'if ($mod.i in rtl.createSet(null, 4, 6, 8)) ;',
  6248. '']));
  6249. end;
  6250. procedure TTestModule.TestSet_InFunction;
  6251. begin
  6252. StartProgram(false);
  6253. Add([
  6254. 'const',
  6255. ' TEnum = 3;',
  6256. ' TSetOfEnum = 4;',
  6257. ' TSetOfAno = 5;',
  6258. 'procedure DoIt;',
  6259. 'type',
  6260. ' TEnum = (red, blue);',
  6261. ' TSetOfEnum = set of TEnum;',
  6262. ' TSetOfAno = set of (up,down);',
  6263. 'var',
  6264. ' e: TEnum;',
  6265. ' se: TSetOfEnum;',
  6266. ' sa: TSetOfAno;',
  6267. 'begin',
  6268. ' se:=[e];',
  6269. ' sa:=[up];',
  6270. 'end;',
  6271. 'begin',
  6272. '']);
  6273. ConvertProgram;
  6274. CheckSource('TestSet_InFunction',
  6275. LinesToStr([ // statements
  6276. 'this.TEnum = 3;',
  6277. 'this.TSetOfEnum = 4;',
  6278. 'this.TSetOfAno = 5;',
  6279. 'var TEnum$1 = {',
  6280. ' "0": "red",',
  6281. ' red: 0,',
  6282. ' "1": "blue",',
  6283. ' blue: 1',
  6284. '};',
  6285. 'var TSetOfAno$a = {',
  6286. ' "0": "up",',
  6287. ' up: 0,',
  6288. ' "1": "down",',
  6289. ' down: 1',
  6290. '};',
  6291. 'this.DoIt = function () {',
  6292. ' var e = 0;',
  6293. ' var se = {};',
  6294. ' var sa = {};',
  6295. ' se = rtl.createSet(e);',
  6296. ' sa = rtl.createSet(TSetOfAno$a.up);',
  6297. '};',
  6298. '']),
  6299. LinesToStr([
  6300. '']));
  6301. end;
  6302. procedure TTestModule.TestSet_ForIn;
  6303. begin
  6304. StartProgram(false);
  6305. Add([
  6306. 'type',
  6307. ' TEnum = (Red, Green, Blue);',
  6308. ' TEnumRg = green..blue;',
  6309. ' TSetOfEnum = set of TEnum;',
  6310. ' TSetOfEnumRg = set of TEnumRg;',
  6311. 'var',
  6312. ' e, e2: TEnum;',
  6313. ' er: TEnum;',
  6314. ' s: TSetOfEnum;',
  6315. 'begin',
  6316. ' for e in TSetOfEnum do ;',
  6317. ' for e in TSetOfEnumRg do ;',
  6318. ' for e in [] do e2:=e;',
  6319. ' for e in [red..green] do e2:=e;',
  6320. ' for e in [green,blue] do e2:=e;',
  6321. ' for e in [red,blue] do e2:=e;',
  6322. ' for e in s do e2:=e;',
  6323. ' for er in TSetOfEnumRg do ;',
  6324. '']);
  6325. ConvertProgram;
  6326. CheckSource('TestSet_ForIn',
  6327. LinesToStr([ // statements
  6328. 'this.TEnum = {',
  6329. ' "0":"Red",',
  6330. ' Red:0,',
  6331. ' "1":"Green",',
  6332. ' Green:1,',
  6333. ' "2":"Blue",',
  6334. ' Blue:2',
  6335. ' };',
  6336. 'this.e = 0;',
  6337. 'this.e2 = 0;',
  6338. 'this.er = 0;',
  6339. 'this.s = {};',
  6340. '']),
  6341. LinesToStr([
  6342. 'for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  6343. 'for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  6344. 'for ($mod.e = 0; $mod.e <= 1; $mod.e++) $mod.e2 = $mod.e;',
  6345. 'for ($mod.e = 1; $mod.e <= 2; $mod.e++) $mod.e2 = $mod.e;',
  6346. 'for ($mod.e in rtl.createSet($mod.TEnum.Red, $mod.TEnum.Blue)) $mod.e2 = $mod.e;',
  6347. 'for (var $l in $mod.s){',
  6348. ' $mod.e = +$l;',
  6349. ' $mod.e2 = $mod.e;',
  6350. '};',
  6351. 'for ($mod.er = 1; $mod.er <= 2; $mod.er++) ;',
  6352. '']));
  6353. end;
  6354. procedure TTestModule.TestNestBegin;
  6355. begin
  6356. StartProgram(false);
  6357. Add('begin');
  6358. Add(' begin');
  6359. Add(' begin');
  6360. Add(' end;');
  6361. Add(' begin');
  6362. Add(' if true then ;');
  6363. Add(' end;');
  6364. Add(' end;');
  6365. ConvertProgram;
  6366. CheckSource('TestNestBegin',
  6367. '',
  6368. 'if (true) ;');
  6369. end;
  6370. procedure TTestModule.TestUnitImplVars;
  6371. begin
  6372. StartUnit(false);
  6373. Add('interface');
  6374. Add('implementation');
  6375. Add('var');
  6376. Add(' V1:longint;');
  6377. Add(' V2:longint = 3;');
  6378. Add(' V3:string = ''abc'';');
  6379. ConvertUnit;
  6380. CheckSource('TestUnitImplVars',
  6381. LinesToStr([ // statements
  6382. 'var $impl = $mod.$impl;',
  6383. '']),
  6384. '', // this.$init
  6385. LinesToStr([ // implementation
  6386. '$impl.V1 = 0;',
  6387. '$impl.V2 = 3;',
  6388. '$impl.V3 = "abc";',
  6389. '']) );
  6390. end;
  6391. procedure TTestModule.TestUnitImplConsts;
  6392. begin
  6393. StartUnit(false);
  6394. Add('interface');
  6395. Add('implementation');
  6396. Add('const');
  6397. Add(' v1 = 3;');
  6398. Add(' v2:longint = 4;');
  6399. Add(' v3:string = ''abc'';');
  6400. ConvertUnit;
  6401. CheckSource('TestUnitImplConsts',
  6402. LinesToStr([ // statements
  6403. 'var $impl = $mod.$impl;',
  6404. '']),
  6405. '', // this.$init
  6406. LinesToStr([ // implementation
  6407. '$impl.v1 = 3;',
  6408. '$impl.v2 = 4;',
  6409. '$impl.v3 = "abc";',
  6410. '']) );
  6411. end;
  6412. procedure TTestModule.TestUnitImplRecord;
  6413. begin
  6414. StartUnit(false);
  6415. Add('interface');
  6416. Add('implementation');
  6417. Add('type');
  6418. Add(' TMyRecord = record');
  6419. Add(' i: longint;');
  6420. Add(' end;');
  6421. Add('var aRec: TMyRecord;');
  6422. Add('initialization');
  6423. Add(' arec.i:=3;');
  6424. ConvertUnit;
  6425. CheckSource('TestUnitImplRecord',
  6426. LinesToStr([ // statements
  6427. 'var $impl = $mod.$impl;',
  6428. '']),
  6429. // this.$init
  6430. '$impl.aRec.i = 3;',
  6431. LinesToStr([ // implementation
  6432. 'rtl.recNewT($impl, "TMyRecord", function () {',
  6433. ' this.i = 0;',
  6434. ' this.$eq = function (b) {',
  6435. ' return this.i === b.i;',
  6436. ' };',
  6437. ' this.$assign = function (s) {',
  6438. ' this.i = s.i;',
  6439. ' return this;',
  6440. ' };',
  6441. '});',
  6442. '$impl.aRec = $impl.TMyRecord.$new();',
  6443. '']) );
  6444. end;
  6445. procedure TTestModule.TestRenameJSNameConflict;
  6446. begin
  6447. StartProgram(false);
  6448. Add('var apply: longint;');
  6449. Add('var bind: longint;');
  6450. Add('var call: longint;');
  6451. Add('begin');
  6452. ConvertProgram;
  6453. CheckSource('TestRenameJSNameConflict',
  6454. LinesToStr([ // statements
  6455. 'this.Apply = 0;',
  6456. 'this.Bind = 0;',
  6457. 'this.Call = 0;'
  6458. ]),
  6459. LinesToStr([ // this.$main
  6460. ''
  6461. ]));
  6462. end;
  6463. procedure TTestModule.TestLocalConst;
  6464. begin
  6465. StartProgram(false);
  6466. Add('procedure DoIt;');
  6467. Add('const');
  6468. Add(' cA: longint = 1;');
  6469. Add(' cB = 2;');
  6470. Add(' procedure Sub;');
  6471. Add(' const');
  6472. Add(' csA = 3;');
  6473. Add(' cB: double = 4;');
  6474. Add(' begin');
  6475. Add(' cb:=cb+csa;');
  6476. Add(' ca:=ca+csa+5;');
  6477. Add(' end;');
  6478. Add('begin');
  6479. Add(' ca:=ca+cb+6;');
  6480. Add('end;');
  6481. Add('begin');
  6482. ConvertProgram;
  6483. CheckSource('TestLocalConst',
  6484. LinesToStr([
  6485. 'var cA = 1;',
  6486. 'var cB = 2;',
  6487. 'var csA = 3;',
  6488. 'var cB$1 = 4;',
  6489. 'this.DoIt = function () {',
  6490. ' function Sub() {',
  6491. ' cB$1 = cB$1 + 3;',
  6492. ' cA = cA + 3 + 5;',
  6493. ' };',
  6494. ' cA = cA + 2 + 6;',
  6495. '};'
  6496. ]),
  6497. LinesToStr([
  6498. ]));
  6499. end;
  6500. procedure TTestModule.TestVarExternal;
  6501. begin
  6502. StartProgram(false);
  6503. Add('var');
  6504. Add(' NaN: double; external name ''Global.NaN'';');
  6505. Add(' d: double;');
  6506. Add('begin');
  6507. Add(' d:=NaN;');
  6508. ConvertProgram;
  6509. CheckSource('TestVarExternal',
  6510. LinesToStr([
  6511. 'this.d = 0.0;'
  6512. ]),
  6513. LinesToStr([
  6514. '$mod.d = Global.NaN;'
  6515. ]));
  6516. end;
  6517. procedure TTestModule.TestVarExternalOtherUnit;
  6518. begin
  6519. AddModuleWithIntfImplSrc('unit2.pas',
  6520. LinesToStr([
  6521. 'var NaN: double; external name ''Global.NaN'';',
  6522. 'var iV: longint;'
  6523. ]),
  6524. '');
  6525. StartUnit(true);
  6526. Add('interface');
  6527. Add('uses unit2;');
  6528. Add('implementation');
  6529. Add('var');
  6530. Add(' d: double;');
  6531. Add(' i: longint; external name ''$i'';');
  6532. Add('begin');
  6533. Add(' d:=nan;');
  6534. Add(' d:=uNit2.nan;');
  6535. Add(' d:=test1.d;');
  6536. Add(' i:=iv;');
  6537. Add(' i:=uNit2.iv;');
  6538. Add(' i:=test1.i;');
  6539. ConvertUnit;
  6540. CheckSource('TestVarExternalOtherUnit',
  6541. LinesToStr([
  6542. 'var $impl = $mod.$impl;',
  6543. '']),
  6544. LinesToStr([ // this.$init
  6545. '$impl.d = Global.NaN;',
  6546. '$impl.d = Global.NaN;',
  6547. '$impl.d = $impl.d;',
  6548. '$i = pas.unit2.iV;',
  6549. '$i = pas.unit2.iV;',
  6550. '$i = $i;',
  6551. '']),
  6552. LinesToStr([ // implementation
  6553. '$impl.d = 0.0;',
  6554. '']) );
  6555. end;
  6556. procedure TTestModule.TestVarAbsoluteFail;
  6557. begin
  6558. StartProgram(false);
  6559. Add([
  6560. 'var',
  6561. ' a: longint;',
  6562. ' b: longword absolute a;',
  6563. 'begin']);
  6564. SetExpectedPasResolverError('Invalid variable modifier "absolute"',nInvalidVariableModifier);
  6565. ConvertProgram;
  6566. end;
  6567. procedure TTestModule.TestConstExternal;
  6568. begin
  6569. StartProgram(false);
  6570. Add([
  6571. 'const',
  6572. ' PI: double; external name ''Global.PI'';',
  6573. ' Tau = 2*pi;',
  6574. 'var d: double;',
  6575. 'begin',
  6576. ' d:=pi;',
  6577. ' d:=tau+pi;']);
  6578. ConvertProgram;
  6579. CheckSource('TestConstExternal',
  6580. LinesToStr([
  6581. 'this.Tau = 2*Global.PI;',
  6582. 'this.d = 0.0;'
  6583. ]),
  6584. LinesToStr([
  6585. '$mod.d = Global.PI;',
  6586. '$mod.d = $mod.Tau + Global.PI;'
  6587. ]));
  6588. end;
  6589. procedure TTestModule.TestDouble;
  6590. begin
  6591. StartProgram(false);
  6592. Add([
  6593. 'type',
  6594. ' TDateTime = double;',
  6595. 'const',
  6596. ' a = TDateTime(2.7);',
  6597. ' b = a + TDateTime(1.7);',
  6598. ' c = 0.9 + 0.1;',
  6599. ' f0_1 = 0.1;',
  6600. ' f0_3 = 0.3;',
  6601. ' fn0_1 = -0.1;',
  6602. ' fn0_3 = -0.3;',
  6603. ' fn0_003 = -0.003;',
  6604. ' fn0_123456789 = -0.123456789;',
  6605. ' fn300_0 = -300.0;',
  6606. ' fn123456_0 = -123456.0;',
  6607. ' fn1234567_8 = -1234567.8;',
  6608. ' fn12345678_9 = -12345678.9;',
  6609. ' f1_0En12 = 1E-12;',
  6610. ' fn1_0En12 = -1E-12;',
  6611. ' maxdouble = 1.7e+308;',
  6612. ' mindouble = -1.7e+308;',
  6613. ' MinSafeIntDouble = -$1fffffffffffff;',
  6614. ' MinSafeIntDouble2 = -$20000000000000-1;',
  6615. ' MaxSafeIntDouble = $1fffffffffffff;',
  6616. ' DZeroResolution = 1E-12;',
  6617. ' Minus1 = -1E-12;',
  6618. ' EPS = 1E-9;',
  6619. ' DELTA = 0.001;',
  6620. ' Big = 129.789E+100;',
  6621. ' Test0_15 = 0.15;',
  6622. ' Test999 = 2.9999999999999;',
  6623. ' Test111999 = 211199999999999000.0;',
  6624. ' TestMinus111999 = -211199999999999000.0;',
  6625. 'var',
  6626. ' d: double = b;',
  6627. 'begin',
  6628. ' d:=1.0;',
  6629. ' d:=1.0/3.0;',
  6630. ' d:=1/3;',
  6631. ' d:=5.0E-324;',
  6632. ' d:=1.7E308;',
  6633. ' d:=001.00E00;',
  6634. ' d:=002.00E001;',
  6635. ' d:=003.000E000;',
  6636. ' d:=-004.00E-00;',
  6637. ' d:=-005.00E-001;',
  6638. ' d:=10**3;',
  6639. ' d:=10 mod 3;',
  6640. ' d:=10 div 3;',
  6641. ' d:=c;',
  6642. ' d:=f0_1;',
  6643. ' d:=f0_3;',
  6644. ' d:=fn0_1;',
  6645. ' d:=fn0_3;',
  6646. ' d:=fn0_003;',
  6647. ' d:=fn0_123456789;',
  6648. ' d:=fn300_0;',
  6649. ' d:=fn123456_0;',
  6650. ' d:=fn1234567_8;',
  6651. ' d:=fn12345678_9;',
  6652. ' d:=f1_0En12;',
  6653. ' d:=fn1_0En12;',
  6654. ' d:=maxdouble;',
  6655. ' d:=mindouble;',
  6656. ' d:=MinSafeIntDouble;',
  6657. ' d:=double(MinSafeIntDouble);',
  6658. ' d:=MinSafeIntDouble2;',
  6659. ' d:=double(MinSafeIntDouble2);',
  6660. ' d:=MaxSafeIntDouble;',
  6661. ' d:=default(double);',
  6662. '']);
  6663. ConvertProgram;
  6664. CheckSource('TestDouble',
  6665. LinesToStr([
  6666. 'this.a = 2.7;',
  6667. 'this.b = 2.7 + 1.7;',
  6668. 'this.c = 0.9 + 0.1;',
  6669. 'this.f0_1 = 0.1;',
  6670. 'this.f0_3 = 0.3;',
  6671. 'this.fn0_1 = -0.1;',
  6672. 'this.fn0_3 = -0.3;',
  6673. 'this.fn0_003 = -0.003;',
  6674. 'this.fn0_123456789 = -0.123456789;',
  6675. 'this.fn300_0 = -300.0;',
  6676. 'this.fn123456_0 = -123456.0;',
  6677. 'this.fn1234567_8 = -1234567.8;',
  6678. 'this.fn12345678_9 = -12345678.9;',
  6679. 'this.f1_0En12 = 1E-12;',
  6680. 'this.fn1_0En12 = -1E-12;',
  6681. 'this.maxdouble = 1.7e+308;',
  6682. 'this.mindouble = -1.7e+308;',
  6683. 'this.MinSafeIntDouble = -0x1fffffffffffff;',
  6684. 'this.MinSafeIntDouble2 = -0x20000000000000 - 1;',
  6685. 'this.MaxSafeIntDouble = 0x1fffffffffffff;',
  6686. 'this.DZeroResolution = 1E-12;',
  6687. 'this.Minus1 = -1E-12;',
  6688. 'this.EPS = 1E-9;',
  6689. 'this.DELTA = 0.001;',
  6690. 'this.Big = 129.789E+100;',
  6691. 'this.Test0_15 = 0.15;',
  6692. 'this.Test999 = 2.9999999999999;',
  6693. 'this.Test111999 = 211199999999999000.0;',
  6694. 'this.TestMinus111999 = -211199999999999000.0;',
  6695. 'this.d = 4.4;'
  6696. ]),
  6697. LinesToStr([
  6698. '$mod.d = 1.0;',
  6699. '$mod.d = 1.0 / 3.0;',
  6700. '$mod.d = 1 / 3;',
  6701. '$mod.d = 5.0E-324;',
  6702. '$mod.d = 1.7E308;',
  6703. '$mod.d = 1.00E0;',
  6704. '$mod.d = 2.00E1;',
  6705. '$mod.d = 3.000E0;',
  6706. '$mod.d = -4.00E-0;',
  6707. '$mod.d = -5.00E-1;',
  6708. '$mod.d = Math.pow(10, 3);',
  6709. '$mod.d = 10 % 3;',
  6710. '$mod.d = rtl.trunc(10 / 3);',
  6711. '$mod.d = 1;',
  6712. '$mod.d = 0.1;',
  6713. '$mod.d = 0.3;',
  6714. '$mod.d = -0.1;',
  6715. '$mod.d = -0.3;',
  6716. '$mod.d = -0.003;',
  6717. '$mod.d = -0.123456789;',
  6718. '$mod.d = -300;',
  6719. '$mod.d = -123456;',
  6720. '$mod.d = -1234567.8;',
  6721. '$mod.d = -1.23456789E7;',
  6722. '$mod.d = 1E-12;',
  6723. '$mod.d = -1E-12;',
  6724. '$mod.d = 1.7E308;',
  6725. '$mod.d = -1.7E308;',
  6726. '$mod.d = -9007199254740991;',
  6727. '$mod.d = -9007199254740991;',
  6728. '$mod.d = -9.007199254740992E15;',
  6729. '$mod.d = -9.007199254740992E15;',
  6730. '$mod.d = 9007199254740991;',
  6731. '$mod.d = 0.0;',
  6732. '']));
  6733. end;
  6734. procedure TTestModule.TestInteger;
  6735. begin
  6736. StartProgram(false);
  6737. Add([
  6738. 'const',
  6739. ' MinInt = low(NativeInt);',
  6740. ' MaxInt = high(NativeInt);',
  6741. 'type',
  6742. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  6743. 'const',
  6744. ' a = low(TMyInt)+High(TMyInt);',
  6745. 'var',
  6746. ' i: TMyInt;',
  6747. 'begin',
  6748. ' i:=-MinInt;',
  6749. ' i:=default(TMyInt);',
  6750. ' i:=low(i)+high(i);',
  6751. '']);
  6752. ConvertProgram;
  6753. CheckSource('TestIntegerRange',
  6754. LinesToStr([
  6755. 'this.MinInt = -9007199254740991;',
  6756. 'this.MaxInt = 9007199254740991;',
  6757. 'this.a = -9007199254740991 + 9007199254740991;',
  6758. 'this.i = 0;',
  6759. '']),
  6760. LinesToStr([
  6761. '$mod.i = - -9007199254740991;',
  6762. '$mod.i = -9007199254740991;',
  6763. '$mod.i = -9007199254740991 + 9007199254740991;',
  6764. '']));
  6765. end;
  6766. procedure TTestModule.TestIntegerRange;
  6767. begin
  6768. StartProgram(false);
  6769. Add([
  6770. 'const',
  6771. ' MinInt = -1;',
  6772. ' MaxInt = +1;',
  6773. 'type',
  6774. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  6775. ' TInt2 = 1..3;',
  6776. 'const',
  6777. ' a = low(TMyInt)+High(TMyInt);',
  6778. ' b = low(TInt2)+High(TInt2);',
  6779. ' s1 = [1];',
  6780. ' s2 = [1,2];',
  6781. ' s3 = [1..3];',
  6782. ' s4 = [low(shortint)..high(shortint)];',
  6783. ' s5 = [succ(low(shortint))..pred(high(shortint))];',
  6784. ' s6 = 1 in s2;',
  6785. 'var',
  6786. ' i: TMyInt;',
  6787. ' i2: TInt2;',
  6788. 'begin',
  6789. ' i:=i2;',
  6790. ' i:=default(TMyInt);',
  6791. ' if i=i2 then ;']);
  6792. ConvertProgram;
  6793. CheckSource('TestIntegerRange',
  6794. LinesToStr([
  6795. 'this.MinInt = -1;',
  6796. 'this.MaxInt = +1;',
  6797. 'this.a = -1 + 1;',
  6798. 'this.b = 1 + 3;',
  6799. 'this.s1 = rtl.createSet(1);',
  6800. 'this.s2 = rtl.createSet(1, 2);',
  6801. 'this.s3 = rtl.createSet(null, 1, 3);',
  6802. 'this.s4 = rtl.createSet(null, -128, 127);',
  6803. 'this.s5 = rtl.createSet(null, -128 + 1, 127 - 1);',
  6804. 'this.s6 = 1 in this.s2;',
  6805. 'this.i = 0;',
  6806. 'this.i2 = 0;',
  6807. '']),
  6808. LinesToStr([
  6809. '$mod.i = $mod.i2;',
  6810. '$mod.i = -1;',
  6811. 'if ($mod.i === $mod.i2) ;',
  6812. '']));
  6813. end;
  6814. procedure TTestModule.TestIntegerTypecasts;
  6815. begin
  6816. StartProgram(false);
  6817. Add([
  6818. 'var',
  6819. ' i: nativeint;',
  6820. ' b: byte;',
  6821. ' sh: shortint;',
  6822. ' w: word;',
  6823. ' sm: smallint;',
  6824. ' lw: longword;',
  6825. ' li: longint;',
  6826. 'begin',
  6827. ' b:=byte(i);',
  6828. ' sh:=shortint(i);',
  6829. ' w:=word(i);',
  6830. ' sm:=smallint(i);',
  6831. ' lw:=longword(i);',
  6832. ' li:=longint(i);',
  6833. '']);
  6834. ConvertProgram;
  6835. CheckSource('TestIntegerTypecasts',
  6836. LinesToStr([
  6837. 'this.i = 0;',
  6838. 'this.b = 0;',
  6839. 'this.sh = 0;',
  6840. 'this.w = 0;',
  6841. 'this.sm = 0;',
  6842. 'this.lw = 0;',
  6843. 'this.li = 0;',
  6844. '']),
  6845. LinesToStr([
  6846. '$mod.b = $mod.i & 255;',
  6847. '$mod.sh = (($mod.i & 255) << 24) >> 24;',
  6848. '$mod.w = $mod.i & 65535;',
  6849. '$mod.sm = (($mod.i & 65535) << 16) >> 16;',
  6850. '$mod.lw = $mod.i >>> 0;',
  6851. '$mod.li = $mod.i & 0xFFFFFFFF;',
  6852. '']));
  6853. end;
  6854. procedure TTestModule.TestInteger_BitwiseShrNativeInt;
  6855. begin
  6856. StartProgram(false);
  6857. Add([
  6858. 'var',
  6859. ' i,j: nativeint;',
  6860. 'begin',
  6861. ' i:=i shr 0;',
  6862. ' i:=i shr 1;',
  6863. ' i:=i shr 3;',
  6864. ' i:=i shr 54;',
  6865. ' i:=j shr i;',
  6866. '']);
  6867. ConvertProgram;
  6868. CheckResolverUnexpectedHints;
  6869. CheckSource('TestInteger_BitwiseShrNativeInt',
  6870. LinesToStr([
  6871. 'this.i = 0;',
  6872. 'this.j = 0;',
  6873. '']),
  6874. LinesToStr([
  6875. '$mod.i = $mod.i;',
  6876. '$mod.i = Math.floor($mod.i / 2);',
  6877. '$mod.i = Math.floor($mod.i / 8);',
  6878. '$mod.i = 0;',
  6879. '$mod.i = rtl.shr($mod.j, $mod.i);',
  6880. '']));
  6881. end;
  6882. procedure TTestModule.TestInteger_BitwiseShlNativeInt;
  6883. begin
  6884. StartProgram(false);
  6885. Add([
  6886. 'var',
  6887. ' i: nativeint;',
  6888. 'begin',
  6889. ' i:=i shl 0;',
  6890. ' i:=i shl 54;',
  6891. ' i:=123456789012 shl 1;',
  6892. ' i:=i shl 1;',
  6893. '']);
  6894. ConvertProgram;
  6895. CheckResolverUnexpectedHints;
  6896. CheckSource('TestInteger_BitwiseShrNativeInt',
  6897. LinesToStr([
  6898. 'this.i = 0;',
  6899. '']),
  6900. LinesToStr([
  6901. '$mod.i = $mod.i;',
  6902. '$mod.i = 0;',
  6903. '$mod.i = 246913578024;',
  6904. '$mod.i = rtl.shl($mod.i, 1);',
  6905. '']));
  6906. end;
  6907. procedure TTestModule.TestInteger_SystemFunc;
  6908. begin
  6909. StartProgram(true);
  6910. Add([
  6911. 'var',
  6912. ' i: byte;',
  6913. ' s: string;',
  6914. 'begin',
  6915. ' system.inc(i);',
  6916. ' system.str(i,s);',
  6917. ' s:=system.str(i);',
  6918. ' i:=system.low(i);',
  6919. ' i:=system.high(i);',
  6920. ' i:=system.pred(i);',
  6921. ' i:=system.succ(i);',
  6922. '']);
  6923. ConvertProgram;
  6924. CheckResolverUnexpectedHints;
  6925. CheckSource('TestInteger_SystemFunc',
  6926. LinesToStr([
  6927. 'this.i = 0;',
  6928. 'this.s = "";',
  6929. '']),
  6930. LinesToStr([
  6931. '$mod.i += 1;',
  6932. '$mod.s = "" + $mod.i;',
  6933. '$mod.s = "" + $mod.i;',
  6934. '$mod.i = 0;',
  6935. '$mod.i = 255;',
  6936. '$mod.i = $mod.i - 1;',
  6937. '$mod.i = $mod.i + 1;',
  6938. '']));
  6939. end;
  6940. procedure TTestModule.TestCurrency;
  6941. begin
  6942. StartProgram(false);
  6943. Add([
  6944. 'type',
  6945. ' TCoin = currency;',
  6946. 'const',
  6947. ' a = TCoin(2.7);',
  6948. ' b = a + TCoin(1.7);',
  6949. ' MinSafeIntCurrency: TCoin = -92233720368.5477;',
  6950. ' MaxSafeIntCurrency: TCoin = 92233720368.5477;',
  6951. 'var',
  6952. ' c: TCoin = b;',
  6953. ' i: nativeint;',
  6954. ' d: double;',
  6955. ' j: jsvalue;',
  6956. 'function DoIt(c: currency): currency; begin end;',
  6957. 'function GetIt(d: double): double; begin end;',
  6958. 'procedure Write(v: jsvalue); begin end;',
  6959. 'begin',
  6960. ' c:=1.0;',
  6961. ' c:=0.1;',
  6962. ' c:=1.0/3.0;',
  6963. ' c:=1/3;',
  6964. ' c:=a;',
  6965. ' d:=c;',
  6966. ' c:=d;',
  6967. ' c:=currency(c);',
  6968. ' c:=currency(d);',
  6969. ' d:=double(c);',
  6970. ' c:=i;',
  6971. ' c:=currency(i);',
  6972. //' i:=c;', not allowed
  6973. ' i:=nativeint(c);',
  6974. ' c:=c+a;',
  6975. ' c:=-c-a;',
  6976. ' c:=d+c;',
  6977. ' c:=c+d;',
  6978. ' c:=d-c;',
  6979. ' c:=c-d;',
  6980. ' c:=c*a;',
  6981. ' c:=a*c;',
  6982. ' c:=d*c;',
  6983. ' c:=c*d;',
  6984. ' c:=c/a;',
  6985. ' c:=a/c;',
  6986. ' c:=d/c;',
  6987. ' c:=c/d;',
  6988. ' c:=c**a;',
  6989. ' c:=a**c;',
  6990. ' c:=d**c;',
  6991. ' c:=c**d;',
  6992. ' if c=c then ;',
  6993. ' if c=a then ;',
  6994. ' if a=c then ;',
  6995. ' if d=c then ;',
  6996. ' if c=d then ;',
  6997. ' c:=DoIt(c);',
  6998. ' c:=DoIt(i);',
  6999. ' c:=DoIt(d);',
  7000. ' c:=GetIt(c);',
  7001. ' j:=c;',
  7002. ' Write(c);',
  7003. ' c:=default(currency);',
  7004. ' j:=str(c);',
  7005. ' j:=str(c:0:3);',
  7006. '']);
  7007. ConvertProgram;
  7008. CheckSource('TestCurrency',
  7009. LinesToStr([
  7010. 'this.a = 27000;',
  7011. 'this.b = this.a + 17000;',
  7012. 'this.MinSafeIntCurrency = -92233720368.5477;',
  7013. 'this.MaxSafeIntCurrency = 92233720368.5477;',
  7014. 'this.c = this.b;',
  7015. 'this.i = 0;',
  7016. 'this.d = 0.0;',
  7017. 'this.j = undefined;',
  7018. 'this.DoIt = function (c) {',
  7019. ' var Result = 0;',
  7020. ' return Result;',
  7021. '};',
  7022. 'this.GetIt = function (d) {',
  7023. ' var Result = 0.0;',
  7024. ' return Result;',
  7025. '};',
  7026. 'this.Write = function (v) {',
  7027. '};',
  7028. '']),
  7029. LinesToStr([
  7030. '$mod.c = 10000;',
  7031. '$mod.c = 1000;',
  7032. '$mod.c = rtl.trunc((1.0 / 3.0) * 10000);',
  7033. '$mod.c = rtl.trunc((1 / 3) * 10000);',
  7034. '$mod.c = $mod.a;',
  7035. '$mod.d = $mod.c / 10000;',
  7036. '$mod.c = rtl.trunc($mod.d * 10000);',
  7037. '$mod.c = $mod.c;',
  7038. '$mod.c = $mod.d * 10000;',
  7039. '$mod.d = $mod.c / 10000;',
  7040. '$mod.c = $mod.i * 10000;',
  7041. '$mod.c = $mod.i * 10000;',
  7042. '$mod.i = rtl.trunc($mod.c / 10000);',
  7043. '$mod.c = $mod.c + $mod.a;',
  7044. '$mod.c = -$mod.c - $mod.a;',
  7045. '$mod.c = ($mod.d * 10000) + $mod.c;',
  7046. '$mod.c = $mod.c + ($mod.d * 10000);',
  7047. '$mod.c = ($mod.d * 10000) - $mod.c;',
  7048. '$mod.c = $mod.c - ($mod.d * 10000);',
  7049. '$mod.c = ($mod.c * $mod.a) / 10000;',
  7050. '$mod.c = ($mod.a * $mod.c) / 10000;',
  7051. '$mod.c = $mod.d * $mod.c;',
  7052. '$mod.c = $mod.c * $mod.d;',
  7053. '$mod.c = rtl.trunc(($mod.c / $mod.a) * 10000);',
  7054. '$mod.c = rtl.trunc(($mod.a / $mod.c) * 10000);',
  7055. '$mod.c = rtl.trunc($mod.d / $mod.c);',
  7056. '$mod.c = rtl.trunc($mod.c / $mod.d);',
  7057. '$mod.c = rtl.trunc(Math.pow($mod.c / 10000, $mod.a / 10000) * 10000);',
  7058. '$mod.c = rtl.trunc(Math.pow($mod.a / 10000, $mod.c / 10000) * 10000);',
  7059. '$mod.c = rtl.trunc(Math.pow($mod.d, $mod.c / 10000) * 10000);',
  7060. '$mod.c = rtl.trunc(Math.pow($mod.c / 10000, $mod.d) * 10000);',
  7061. 'if ($mod.c === $mod.c) ;',
  7062. 'if ($mod.c === $mod.a) ;',
  7063. 'if ($mod.a === $mod.c) ;',
  7064. 'if (($mod.d * 10000) === $mod.c) ;',
  7065. 'if ($mod.c === ($mod.d * 10000)) ;',
  7066. '$mod.c = $mod.DoIt($mod.c);',
  7067. '$mod.c = $mod.DoIt($mod.i * 10000);',
  7068. '$mod.c = $mod.DoIt($mod.d * 10000);',
  7069. '$mod.c = rtl.trunc($mod.GetIt($mod.c / 10000) * 10000);',
  7070. '$mod.j = $mod.c / 10000;',
  7071. '$mod.Write($mod.c / 10000);',
  7072. '$mod.c = 0;',
  7073. '$mod.j = rtl.floatToStr($mod.c / 10000);',
  7074. '$mod.j = rtl.floatToStr($mod.c / 10000, 0, 3);',
  7075. '']));
  7076. end;
  7077. procedure TTestModule.TestForBoolDo;
  7078. begin
  7079. StartProgram(false);
  7080. Add([
  7081. 'var b: boolean;',
  7082. 'begin',
  7083. ' for b:=false to true do ;',
  7084. ' for b:=b downto false do ;',
  7085. ' for b in boolean do ;',
  7086. '']);
  7087. ConvertProgram;
  7088. CheckSource('TestForBoolDo',
  7089. LinesToStr([ // statements
  7090. 'this.b = false;']),
  7091. LinesToStr([ // this.$main
  7092. 'for (var $l = 0; $l <= 1; $l++) $mod.b = $l !== 0;',
  7093. 'for (var $l1 = +$mod.b; $l1 >= 0; $l1--) $mod.b = $l1 !== 0;',
  7094. 'for (var $l2 = 0; $l2 <= 1; $l2++) $mod.b = $l2 !== 0;',
  7095. '']));
  7096. end;
  7097. procedure TTestModule.TestForIntDo;
  7098. begin
  7099. StartProgram(false);
  7100. Add([
  7101. 'var i: longint;',
  7102. 'begin',
  7103. ' for i:=3 to 5 do ;',
  7104. ' for i:=i downto 2 do ;',
  7105. ' for i in byte do ;',
  7106. '']);
  7107. ConvertProgram;
  7108. CheckSource('TestForIntDo',
  7109. LinesToStr([ // statements
  7110. 'this.i = 0;']),
  7111. LinesToStr([ // this.$main
  7112. 'for ($mod.i = 3; $mod.i <= 5; $mod.i++) ;',
  7113. 'for (var $l = $mod.i; $l >= 2; $l--) $mod.i = $l;',
  7114. 'for (var $l1 = 0; $l1 <= 255; $l1++) $mod.i = $l1;',
  7115. '']));
  7116. end;
  7117. procedure TTestModule.TestForIntInDo;
  7118. begin
  7119. StartProgram(false);
  7120. Add([
  7121. 'type',
  7122. ' TSetOfInt = set of byte;',
  7123. ' TIntRg = 3..7;',
  7124. ' TSetOfIntRg = set of TIntRg;',
  7125. 'var',
  7126. ' i,i2: longint;',
  7127. ' a1: array of byte;',
  7128. ' a2: array[1..3] of byte;',
  7129. ' soi: TSetOfInt;',
  7130. ' soir: TSetOfIntRg;',
  7131. ' ir: TIntRg;',
  7132. 'begin',
  7133. ' for i in byte do ;',
  7134. ' for i in a1 do ;',
  7135. ' for i in a2 do ;',
  7136. ' for i in [11..13] do ;',
  7137. ' for i in TSetOfInt do ;',
  7138. ' for i in TIntRg do ;',
  7139. ' for i in soi do i2:=i;',
  7140. ' for i in TSetOfIntRg do ;',
  7141. ' for i in soir do ;',
  7142. ' for ir in TIntRg do ;',
  7143. ' for ir in TSetOfIntRg do ;',
  7144. ' for ir in soir do ;',
  7145. '']);
  7146. ConvertProgram;
  7147. CheckSource('TestForIntInDo',
  7148. LinesToStr([ // statements
  7149. 'this.i = 0;',
  7150. 'this.i2 = 0;',
  7151. 'this.a1 = [];',
  7152. 'this.a2 = rtl.arraySetLength(null, 0, 3);',
  7153. 'this.soi = {};',
  7154. 'this.soir = {};',
  7155. 'this.ir = 0;',
  7156. '']),
  7157. LinesToStr([ // this.$main
  7158. 'for (var $l = 0; $l <= 255; $l++) $mod.i = $l;',
  7159. 'for (var $in = $mod.a1, $l1 = 0, $end = rtl.length($in) - 1; $l1 <= $end; $l1++) $mod.i = $in[$l1];',
  7160. 'for (var $in1 = $mod.a2, $l2 = 0, $end1 = rtl.length($in1) - 1; $l2 <= $end1; $l2++) $mod.i = $in1[$l2];',
  7161. 'for (var $l3 = 11; $l3 <= 13; $l3++) $mod.i = $l3;',
  7162. 'for (var $l4 = 0; $l4 <= 255; $l4++) $mod.i = $l4;',
  7163. 'for (var $l5 = 3; $l5 <= 7; $l5++) $mod.i = $l5;',
  7164. 'for (var $l6 in $mod.soi) {',
  7165. ' $mod.i = +$l6;',
  7166. ' $mod.i2 = $mod.i;',
  7167. '};',
  7168. 'for (var $l7 = 3; $l7 <= 7; $l7++) $mod.i = $l7;',
  7169. 'for (var $l8 in $mod.soir) $mod.i = +$l8;',
  7170. 'for (var $l9 = 3; $l9 <= 7; $l9++) $mod.ir = $l9;',
  7171. 'for (var $l10 = 3; $l10 <= 7; $l10++) $mod.ir = $l10;',
  7172. 'for (var $l11 in $mod.soir) $mod.ir = +$l11;',
  7173. '']));
  7174. end;
  7175. procedure TTestModule.TestCharConst;
  7176. begin
  7177. StartProgram(false);
  7178. Add([
  7179. 'const',
  7180. ' a = #$00F3;',
  7181. ' c: char = ''1'';',
  7182. ' wc: widechar = ''ä'';',
  7183. 'begin',
  7184. ' c:=#0;',
  7185. ' c:=#1;',
  7186. ' c:=#9;',
  7187. ' c:=#10;',
  7188. ' c:=#13;',
  7189. ' c:=#31;',
  7190. ' c:=#32;',
  7191. ' c:=#$A;',
  7192. ' c:=#$0A;',
  7193. ' c:=#$b;',
  7194. ' c:=#$0b;',
  7195. ' c:=^A;',
  7196. ' c:=''"'';',
  7197. ' c:=default(char);',
  7198. ' c:=#$00E4;', // ä
  7199. ' c:=''ä'';',
  7200. ' c:=#$E4;', // ä
  7201. ' c:=#$D800;', // invalid UTF-16
  7202. ' c:=#$DFFF;', // invalid UTF-16
  7203. ' c:=#$FFFF;', // last UCS-2
  7204. ' c:=high(c);', // last UCS-2
  7205. ' c:=#269;',
  7206. '']);
  7207. ConvertProgram;
  7208. CheckSource('TestCharConst',
  7209. LinesToStr([
  7210. 'this.a="ó";',
  7211. 'this.c="1";',
  7212. 'this.wc="ä";'
  7213. ]),
  7214. LinesToStr([
  7215. '$mod.c="\x00";',
  7216. '$mod.c="\x01";',
  7217. '$mod.c="\t";',
  7218. '$mod.c="\n";',
  7219. '$mod.c="\r";',
  7220. '$mod.c="\x1F";',
  7221. '$mod.c=" ";',
  7222. '$mod.c="\n";',
  7223. '$mod.c="\n";',
  7224. '$mod.c="\x0B";',
  7225. '$mod.c="\x0B";',
  7226. '$mod.c="\x01";',
  7227. '$mod.c=''"'';',
  7228. '$mod.c="\x00";',
  7229. '$mod.c = "ä";',
  7230. '$mod.c = "ä";',
  7231. '$mod.c = "ä";',
  7232. '$mod.c="\uD800";',
  7233. '$mod.c="\uDFFF";',
  7234. '$mod.c="\uFFFF";',
  7235. '$mod.c="\uFFFF";',
  7236. '$mod.c = "č";',
  7237. '']));
  7238. end;
  7239. procedure TTestModule.TestChar_Compare;
  7240. begin
  7241. StartProgram(false);
  7242. Add('var');
  7243. Add(' c: char;');
  7244. Add(' b: boolean;');
  7245. Add('begin');
  7246. Add(' b:=c=''1'';');
  7247. Add(' b:=''2''=c;');
  7248. Add(' b:=''3''=''4'';');
  7249. Add(' b:=c<>''5'';');
  7250. Add(' b:=''6''<>c;');
  7251. Add(' b:=c>''7'';');
  7252. Add(' b:=''8''>c;');
  7253. Add(' b:=c>=''9'';');
  7254. Add(' b:=''A''>=c;');
  7255. Add(' b:=c<''B'';');
  7256. Add(' b:=''C''<c;');
  7257. Add(' b:=c<=''D'';');
  7258. Add(' b:=''E''<=c;');
  7259. ConvertProgram;
  7260. CheckSource('TestChar_Compare',
  7261. LinesToStr([
  7262. 'this.c="";',
  7263. 'this.b = false;'
  7264. ]),
  7265. LinesToStr([
  7266. '$mod.b = $mod.c === "1";',
  7267. '$mod.b = "2" === $mod.c;',
  7268. '$mod.b = "3" === "4";',
  7269. '$mod.b = $mod.c !== "5";',
  7270. '$mod.b = "6" !== $mod.c;',
  7271. '$mod.b = $mod.c > "7";',
  7272. '$mod.b = "8" > $mod.c;',
  7273. '$mod.b = $mod.c >= "9";',
  7274. '$mod.b = "A" >= $mod.c;',
  7275. '$mod.b = $mod.c < "B";',
  7276. '$mod.b = "C" < $mod.c;',
  7277. '$mod.b = $mod.c <= "D";',
  7278. '$mod.b = "E" <= $mod.c;',
  7279. '']));
  7280. end;
  7281. procedure TTestModule.TestChar_BuiltInProcs;
  7282. begin
  7283. StartProgram(false);
  7284. Add([
  7285. 'var',
  7286. ' c: char;',
  7287. ' i: longint;',
  7288. ' s: string;',
  7289. 'begin',
  7290. ' i:=ord(c);',
  7291. ' i:=ord(s[i]);',
  7292. ' c:=chr(i);',
  7293. ' c:=pred(c);',
  7294. ' c:=succ(c);',
  7295. ' c:=low(c);',
  7296. ' c:=high(c);',
  7297. ' i:=byte(c);',
  7298. ' i:=word(c);',
  7299. ' i:=longint(c);',
  7300. '']);
  7301. ConvertProgram;
  7302. CheckSource('TestChar_BuiltInProcs',
  7303. LinesToStr([
  7304. 'this.c = "";',
  7305. 'this.i = 0;',
  7306. 'this.s = "";'
  7307. ]),
  7308. LinesToStr([
  7309. '$mod.i = $mod.c.charCodeAt();',
  7310. '$mod.i = $mod.s.charCodeAt($mod.i-1);',
  7311. '$mod.c = String.fromCharCode($mod.i);',
  7312. '$mod.c = String.fromCharCode($mod.c.charCodeAt() - 1);',
  7313. '$mod.c = String.fromCharCode($mod.c.charCodeAt() + 1);',
  7314. '$mod.c = "\x00";',
  7315. '$mod.c = "\uFFFF";',
  7316. '$mod.i = $mod.c.charCodeAt() & 255;',
  7317. '$mod.i = $mod.c.charCodeAt();',
  7318. '$mod.i = $mod.c.charCodeAt() & 0xFFFFFFFF;',
  7319. '']));
  7320. end;
  7321. procedure TTestModule.TestStringConst;
  7322. begin
  7323. StartProgram(false);
  7324. Add([
  7325. '{$H+}',
  7326. 'const',
  7327. ' a = #$00F3#$017C;', // first <256, then >=256
  7328. ' b = string(''a'');',
  7329. ' c = string(''ä'');',
  7330. ' d = UnicodeString(''b'');',
  7331. ' e = UnicodeString(''ö'');',
  7332. 'var',
  7333. ' s: string = ''abc'';',
  7334. 'begin',
  7335. ' s:='''';',
  7336. ' s:=#13#10;',
  7337. ' s:=#9''foo'';',
  7338. ' s:=#$A9;',
  7339. ' s:=''foo''#13''bar'';',
  7340. ' s:=''"'';',
  7341. ' s:=''"''''"'';',
  7342. ' s:=#$20AC;', // euro
  7343. ' s:=#$10437;', // outside BMP
  7344. ' s:=''abc''#$20AC;', // ascii,#
  7345. ' s:=''ä''#$20AC;', // non ascii,#
  7346. ' s:=#$20AC''abc'';', // #, ascii
  7347. ' s:=#$20AC''ä'';', // #, non ascii
  7348. ' s:=default(string);',
  7349. ' s:=concat(s);',
  7350. ' s:=concat(s,''a'',s);',
  7351. ' s:=#250#269;',
  7352. //' s:=#$2F804;',
  7353. // ToDo: \uD87E\uDC04 -> \u{2F804}
  7354. '']);
  7355. ConvertProgram;
  7356. CheckSource('TestStringConst',
  7357. LinesToStr([
  7358. 'this.a = "óż";',
  7359. 'this.b = "a";',
  7360. 'this.c = "ä";',
  7361. 'this.d = "b";',
  7362. 'this.e = "ö";',
  7363. 'this.s="abc";',
  7364. '']),
  7365. LinesToStr([
  7366. '$mod.s="";',
  7367. '$mod.s="\r\n";',
  7368. '$mod.s="\tfoo";',
  7369. '$mod.s="©";',
  7370. '$mod.s="foo\rbar";',
  7371. '$mod.s=''"'';',
  7372. '$mod.s=''"\''"'';',
  7373. '$mod.s="€";',
  7374. '$mod.s="'#$F0#$90#$90#$B7'";',
  7375. '$mod.s = "abc€";',
  7376. '$mod.s = "ä€";',
  7377. '$mod.s = "€abc";',
  7378. '$mod.s = "ۊ";',
  7379. '$mod.s="";',
  7380. '$mod.s = $mod.s;',
  7381. '$mod.s = $mod.s.concat("a", $mod.s);',
  7382. '$mod.s = "úč";',
  7383. '']));
  7384. end;
  7385. procedure TTestModule.TestStringConst_InvalidUTF16;
  7386. begin
  7387. StartProgram(false);
  7388. Add([
  7389. 'const',
  7390. ' a: char = #$D87E;',
  7391. ' b: string = #$D87E;',
  7392. ' c: string = #$D87E#43;',
  7393. 'begin',
  7394. ' c:=''abc''#$D87E;',
  7395. ' c:=#0#1#2;',
  7396. ' c:=#127;',
  7397. ' c:=#128;',
  7398. ' c:=#255;',
  7399. ' c:=#256;',
  7400. '']);
  7401. ConvertProgram;
  7402. CheckSource('TestStringConst',
  7403. LinesToStr([
  7404. 'this.a = "\uD87E";',
  7405. 'this.b = "\uD87E";',
  7406. 'this.c = "\uD87E+";',
  7407. '']),
  7408. LinesToStr([
  7409. '$mod.c = "abc\uD87E";',
  7410. '$mod.c = "\x00\x01\x02";',
  7411. '$mod.c = "'#127'";',
  7412. '$mod.c = "'#$c2#$80'";',
  7413. '$mod.c = "'#$c3#$BF'";',
  7414. '$mod.c = "'#$c4#$80'";',
  7415. '']));
  7416. end;
  7417. procedure TTestModule.TestStringConstSurrogate;
  7418. begin
  7419. StartProgram(false);
  7420. Add([
  7421. 'var',
  7422. ' s: string;',
  7423. 'begin',
  7424. ' s:=''😊'';', // 1F60A
  7425. '']);
  7426. ConvertProgram;
  7427. CheckSource('TestStringConstSurrogate',
  7428. LinesToStr([
  7429. 'this.s="";'
  7430. ]),
  7431. LinesToStr([
  7432. '$mod.s="😊";'
  7433. ]));
  7434. end;
  7435. procedure TTestModule.TestString_Length;
  7436. begin
  7437. StartProgram(false);
  7438. Add('const c = ''foo'';');
  7439. Add('var');
  7440. Add(' s: string;');
  7441. Add(' i: longint;');
  7442. Add('begin');
  7443. Add(' i:=length(s);');
  7444. Add(' i:=length(s+s);');
  7445. Add(' i:=length(''abc'');');
  7446. Add(' i:=length(c);');
  7447. ConvertProgram;
  7448. CheckSource('TestString_Length',
  7449. LinesToStr([
  7450. 'this.c = "foo";',
  7451. 'this.s = "";',
  7452. 'this.i = 0;',
  7453. '']),
  7454. LinesToStr([
  7455. '$mod.i = $mod.s.length;',
  7456. '$mod.i = ($mod.s+$mod.s).length;',
  7457. '$mod.i = "abc".length;',
  7458. '$mod.i = $mod.c.length;',
  7459. '']));
  7460. end;
  7461. procedure TTestModule.TestString_Compare;
  7462. begin
  7463. StartProgram(false);
  7464. Add('var');
  7465. Add(' s, t: string;');
  7466. Add(' b: boolean;');
  7467. Add('begin');
  7468. Add(' b:=s=t;');
  7469. Add(' b:=s<>t;');
  7470. Add(' b:=s>t;');
  7471. Add(' b:=s>=t;');
  7472. Add(' b:=s<t;');
  7473. Add(' b:=s<=t;');
  7474. ConvertProgram;
  7475. CheckSource('TestString_Compare',
  7476. LinesToStr([ // statements
  7477. 'this.s = "";',
  7478. 'this.t = "";',
  7479. 'this.b =false;'
  7480. ]),
  7481. LinesToStr([ // this.$main
  7482. '$mod.b = $mod.s === $mod.t;',
  7483. '$mod.b = $mod.s !== $mod.t;',
  7484. '$mod.b = $mod.s > $mod.t;',
  7485. '$mod.b = $mod.s >= $mod.t;',
  7486. '$mod.b = $mod.s < $mod.t;',
  7487. '$mod.b = $mod.s <= $mod.t;',
  7488. '']));
  7489. end;
  7490. procedure TTestModule.TestString_SetLength;
  7491. begin
  7492. StartProgram(false);
  7493. Add([
  7494. 'procedure DoIt(var s: string);',
  7495. 'begin',
  7496. ' SetLength(s,2);',
  7497. 'end;',
  7498. 'var s: string;',
  7499. 'begin',
  7500. ' SetLength(s,3);',
  7501. '']);
  7502. ConvertProgram;
  7503. CheckSource('TestString_SetLength',
  7504. LinesToStr([ // statements
  7505. 'this.DoIt = function (s) {',
  7506. ' s.set(rtl.strSetLength(s.get(), 2));',
  7507. '};',
  7508. 'this.s = "";',
  7509. '']),
  7510. LinesToStr([ // this.$main
  7511. '$mod.s = rtl.strSetLength($mod.s, 3);'
  7512. ]));
  7513. end;
  7514. procedure TTestModule.TestString_CharAt;
  7515. begin
  7516. StartProgram(false);
  7517. Add([
  7518. 'var',
  7519. ' s: string;',
  7520. ' c: char;',
  7521. ' b: boolean;',
  7522. 'begin',
  7523. ' b:= s[1] = c;',
  7524. ' b:= c = s[1];',
  7525. ' b:= c <> s[1];',
  7526. ' b:= c > s[1];',
  7527. ' b:= c >= s[1];',
  7528. ' b:= c < s[2];',
  7529. ' b:= c <= s[1];',
  7530. ' s[1] := c;',
  7531. ' s[2+3] := c;']);
  7532. ConvertProgram;
  7533. CheckSource('TestString_CharAt',
  7534. LinesToStr([ // statements
  7535. 'this.s = "";',
  7536. 'this.c = "";',
  7537. 'this.b = false;'
  7538. ]),
  7539. LinesToStr([ // this.$main
  7540. '$mod.b = $mod.s.charAt(0) === $mod.c;',
  7541. '$mod.b = $mod.c === $mod.s.charAt(0);',
  7542. '$mod.b = $mod.c !== $mod.s.charAt(0);',
  7543. '$mod.b = $mod.c > $mod.s.charAt(0);',
  7544. '$mod.b = $mod.c >= $mod.s.charAt(0);',
  7545. '$mod.b = $mod.c < $mod.s.charAt(1);',
  7546. '$mod.b = $mod.c <= $mod.s.charAt(0);',
  7547. '$mod.s = rtl.setCharAt($mod.s, 0, $mod.c);',
  7548. '$mod.s = rtl.setCharAt($mod.s, (2 + 3) - 1, $mod.c);',
  7549. '']));
  7550. end;
  7551. procedure TTestModule.TestStringHMinusFail;
  7552. begin
  7553. StartProgram(false);
  7554. Add([
  7555. '{$H-}',
  7556. 'var s: string;',
  7557. 'begin']);
  7558. ConvertProgram;
  7559. CheckHint(mtWarning,nWarnIllegalCompilerDirectiveX,'Warning: test1.pp(3,6) : Illegal compiler directive "H-"');
  7560. end;
  7561. procedure TTestModule.TestStr;
  7562. begin
  7563. StartProgram(false);
  7564. Add('var');
  7565. Add(' b: boolean;');
  7566. Add(' i: longint;');
  7567. Add(' d: double;');
  7568. Add(' s: string;');
  7569. Add('begin');
  7570. Add(' str(b,s);');
  7571. Add(' str(i,s);');
  7572. Add(' str(d,s);');
  7573. Add(' str(i:3,s);');
  7574. Add(' str(d:3:2,s);');
  7575. Add(' Str(12.456:12:1,s);');
  7576. Add(' Str(12.456:12,s);');
  7577. Add(' s:=str(b);');
  7578. Add(' s:=str(i);');
  7579. Add(' s:=str(d);');
  7580. Add(' s:=str(i,i);');
  7581. Add(' s:=str(i:3);');
  7582. Add(' s:=str(d:3:2);');
  7583. Add(' s:=str(i:4,i);');
  7584. Add(' s:=str(i,i:5);');
  7585. Add(' s:=str(i:4,i:5);');
  7586. Add(' s:=str(s,s);');
  7587. Add(' s:=str(s,''foo'');');
  7588. ConvertProgram;
  7589. CheckSource('TestStr',
  7590. LinesToStr([ // statements
  7591. 'this.b = false;',
  7592. 'this.i = 0;',
  7593. 'this.d = 0.0;',
  7594. 'this.s = "";',
  7595. '']),
  7596. LinesToStr([ // this.$main
  7597. '$mod.s = ""+$mod.b;',
  7598. '$mod.s = ""+$mod.i;',
  7599. '$mod.s = rtl.floatToStr($mod.d);',
  7600. '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
  7601. '$mod.s = rtl.floatToStr($mod.d,3,2);',
  7602. '$mod.s = rtl.floatToStr(12.456,12,1);',
  7603. '$mod.s = rtl.floatToStr(12.456,12);',
  7604. '$mod.s = ""+$mod.b;',
  7605. '$mod.s = ""+$mod.i;',
  7606. '$mod.s = rtl.floatToStr($mod.d);',
  7607. '$mod.s = ""+$mod.i+$mod.i;',
  7608. '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
  7609. '$mod.s = rtl.floatToStr($mod.d,3,2);',
  7610. '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + $mod.i;',
  7611. '$mod.s = "" + $mod.i + rtl.spaceLeft("" + $mod.i, 5);',
  7612. '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + rtl.spaceLeft("" + $mod.i, 5);',
  7613. '$mod.s = $mod.s + $mod.s;',
  7614. '$mod.s = $mod.s + "foo";',
  7615. '']));
  7616. end;
  7617. procedure TTestModule.TestBaseType_AnsiStringFail;
  7618. begin
  7619. StartProgram(false);
  7620. Add('var s: AnsiString');
  7621. SetExpectedPasResolverError('identifier not found "AnsiString"',PasResolveEval.nIdentifierNotFound);
  7622. ConvertProgram;
  7623. end;
  7624. procedure TTestModule.TestBaseType_WideStringFail;
  7625. begin
  7626. StartProgram(false);
  7627. Add('var s: WideString');
  7628. SetExpectedPasResolverError('identifier not found "WideString"',PasResolveEval.nIdentifierNotFound);
  7629. ConvertProgram;
  7630. end;
  7631. procedure TTestModule.TestBaseType_ShortStringFail;
  7632. begin
  7633. StartProgram(false);
  7634. Add('var s: ShortString');
  7635. SetExpectedPasResolverError('identifier not found "ShortString"',PasResolveEval.nIdentifierNotFound);
  7636. ConvertProgram;
  7637. end;
  7638. procedure TTestModule.TestBaseType_RawByteStringFail;
  7639. begin
  7640. StartProgram(false);
  7641. Add('var s: RawByteString');
  7642. SetExpectedPasResolverError('identifier not found "RawByteString"',PasResolveEval.nIdentifierNotFound);
  7643. ConvertProgram;
  7644. end;
  7645. procedure TTestModule.TestTypeShortstring_Fail;
  7646. begin
  7647. StartProgram(false);
  7648. Add('type t = string[12];');
  7649. Add('var s: t;');
  7650. Add('begin');
  7651. SetExpectedPasResolverError('illegal qualifier "["',nIllegalQualifier);
  7652. ConvertProgram;
  7653. end;
  7654. procedure TTestModule.TestCharSet_Custom;
  7655. begin
  7656. StartProgram(false);
  7657. Add([
  7658. 'type',
  7659. ' TCharRg = ''a''..''z'';',
  7660. ' TSetOfCharRg = set of TCharRg;',
  7661. ' TCharRg2 = ''m''..''p'';',
  7662. 'const',
  7663. ' crg: TCharRg = ''b'';',
  7664. 'var',
  7665. ' c: char;',
  7666. ' crg2: TCharRg2;',
  7667. ' s: TSetOfCharRg;',
  7668. 'begin',
  7669. ' c:=crg;',
  7670. ' crg:=c;',
  7671. ' crg2:=crg;',
  7672. ' if c=crg then ;',
  7673. ' if crg=c then ;',
  7674. ' if crg=crg2 then ;',
  7675. ' if c in s then ;',
  7676. ' if crg2 in s then ;',
  7677. ' c:=default(TCharRg);',
  7678. '']);
  7679. ConvertProgram;
  7680. CheckSource('TestCharSet_Custom',
  7681. LinesToStr([ // statements
  7682. 'this.crg = "b";',
  7683. 'this.c = "";',
  7684. 'this.crg2 = "m";',
  7685. 'this.s = {};',
  7686. '']),
  7687. LinesToStr([ // this.$main
  7688. '$mod.c = $mod.crg;',
  7689. '$mod.crg = $mod.c;',
  7690. '$mod.crg2 = $mod.crg;',
  7691. 'if ($mod.c === $mod.crg) ;',
  7692. 'if ($mod.crg === $mod.c) ;',
  7693. 'if ($mod.crg === $mod.crg2) ;',
  7694. 'if ($mod.c.charCodeAt() in $mod.s) ;',
  7695. 'if ($mod.crg2.charCodeAt() in $mod.s) ;',
  7696. '$mod.c = "a";',
  7697. '']));
  7698. end;
  7699. procedure TTestModule.TestWideChar;
  7700. begin
  7701. StartProgram(false);
  7702. Add([
  7703. 'procedure Fly(var c: char);',
  7704. 'begin',
  7705. 'end;',
  7706. 'procedure Run(var c: widechar);',
  7707. 'begin',
  7708. 'end;',
  7709. 'var',
  7710. ' c: char;',
  7711. ' wc: widechar;',
  7712. ' w: word;',
  7713. 'begin',
  7714. ' Fly(wc);',
  7715. ' Run(c);',
  7716. ' wc:=WideChar(w);',
  7717. ' w:=ord(wc);',
  7718. '']);
  7719. ConvertProgram;
  7720. CheckSource('TestWideChar_VarArg',
  7721. LinesToStr([ // statements
  7722. 'this.Fly = function (c) {',
  7723. '};',
  7724. 'this.Run = function (c) {',
  7725. '};',
  7726. 'this.c = "";',
  7727. 'this.wc = "";',
  7728. 'this.w = 0;',
  7729. '']),
  7730. LinesToStr([ // this.$main
  7731. '$mod.Fly({',
  7732. ' p: $mod,',
  7733. ' get: function () {',
  7734. ' return this.p.wc;',
  7735. ' },',
  7736. ' set: function (v) {',
  7737. ' this.p.wc = v;',
  7738. ' }',
  7739. '});',
  7740. '$mod.Run({',
  7741. ' p: $mod,',
  7742. ' get: function () {',
  7743. ' return this.p.c;',
  7744. ' },',
  7745. ' set: function (v) {',
  7746. ' this.p.c = v;',
  7747. ' }',
  7748. '});',
  7749. '$mod.wc = String.fromCharCode($mod.w);',
  7750. '$mod.w = $mod.wc.charCodeAt();',
  7751. '',
  7752. '']));
  7753. end;
  7754. procedure TTestModule.TestForCharDo;
  7755. begin
  7756. StartProgram(false);
  7757. Add([
  7758. 'var c: char;',
  7759. 'begin',
  7760. ' for c:=''a'' to ''c'' do ;',
  7761. ' for c:=c downto ''a'' do ;',
  7762. ' for c:=''Б'' to ''Я'' do ;',
  7763. '']);
  7764. ConvertProgram;
  7765. CheckSource('TestForCharDo',
  7766. LinesToStr([ // statements
  7767. 'this.c = "";']),
  7768. LinesToStr([ // this.$main
  7769. 'for (var $l = 97; $l <= 99; $l++) $mod.c = String.fromCharCode($l);',
  7770. 'for (var $l1 = $mod.c.charCodeAt(); $l1 >= 97; $l1--) $mod.c = String.fromCharCode($l1);',
  7771. 'for (var $l2 = 1041; $l2 <= 1071; $l2++) $mod.c = String.fromCharCode($l2);',
  7772. '']));
  7773. end;
  7774. procedure TTestModule.TestForCharInDo;
  7775. begin
  7776. StartProgram(false);
  7777. Add([
  7778. 'type',
  7779. ' TSetOfChar = set of char;',
  7780. ' TCharRg = ''a''..''z'';',
  7781. ' TSetOfCharRg = set of TCharRg;',
  7782. 'const Foo = ''foo'';',
  7783. 'var',
  7784. ' c,c2: char;',
  7785. ' s: string;',
  7786. ' a1: array of char;',
  7787. ' a2: array[1..3] of char;',
  7788. ' soc: TSetOfChar;',
  7789. ' socr: TSetOfCharRg;',
  7790. ' cr: TCharRg;',
  7791. 'begin',
  7792. ' for c in foo do ;',
  7793. ' for c in s do ;',
  7794. ' for c in char do ;',
  7795. ' for c in a1 do ;',
  7796. ' for c in a2 do ;',
  7797. ' for c in [''1''..''3''] do ;',
  7798. ' for c in TSetOfChar do ;',
  7799. ' for c in TCharRg do ;',
  7800. ' for c in soc do c2:=c;',
  7801. ' for c in TSetOfCharRg do ;',
  7802. ' for c in socr do ;',
  7803. ' for cr in TCharRg do ;',
  7804. ' for cr in TSetOfCharRg do ;',
  7805. ' for cr in socr do ;',
  7806. '']);
  7807. ConvertProgram;
  7808. CheckSource('TestForCharInDo',
  7809. LinesToStr([ // statements
  7810. 'this.Foo = "foo";',
  7811. 'this.c = "";',
  7812. 'this.c2 = "";',
  7813. 'this.s = "";',
  7814. 'this.a1 = [];',
  7815. 'this.a2 = rtl.arraySetLength(null, "", 3);',
  7816. 'this.soc = {};',
  7817. 'this.socr = {};',
  7818. 'this.cr = "a";',
  7819. '']),
  7820. LinesToStr([ // this.$main
  7821. 'for (var $in = $mod.Foo, $l = 0, $end = $in.length - 1; $l <= $end; $l++) $mod.c = $in.charAt($l);',
  7822. 'for (var $in1 = $mod.s, $l1 = 0, $end1 = $in1.length - 1; $l1 <= $end1; $l1++) $mod.c = $in1.charAt($l1);',
  7823. 'for (var $l2 = 0; $l2 <= 65535; $l2++) $mod.c = String.fromCharCode($l2);',
  7824. 'for (var $in2 = $mod.a1, $l3 = 0, $end2 = rtl.length($in2) - 1; $l3 <= $end2; $l3++) $mod.c = $in2[$l3];',
  7825. 'for (var $in3 = $mod.a2, $l4 = 0, $end3 = rtl.length($in3) - 1; $l4 <= $end3; $l4++) $mod.c = $in3[$l4];',
  7826. 'for (var $l5 = 49; $l5 <= 51; $l5++) $mod.c = String.fromCharCode($l5);',
  7827. 'for (var $l6 = 0; $l6 <= 65535; $l6++) $mod.c = String.fromCharCode($l6);',
  7828. 'for (var $l7 = 97; $l7 <= 122; $l7++) $mod.c = String.fromCharCode($l7);',
  7829. 'for (var $l8 in $mod.soc) {',
  7830. ' $mod.c = String.fromCharCode($l8);',
  7831. ' $mod.c2 = $mod.c;',
  7832. '};',
  7833. 'for (var $l9 = 97; $l9 <= 122; $l9++) $mod.c = String.fromCharCode($l9);',
  7834. 'for (var $l10 in $mod.socr) $mod.c = String.fromCharCode($l10);',
  7835. 'for (var $l11 = 97; $l11 <= 122; $l11++) $mod.cr = String.fromCharCode($l11);',
  7836. 'for (var $l12 = 97; $l12 <= 122; $l12++) $mod.cr = String.fromCharCode($l12);',
  7837. 'for (var $l13 in $mod.socr) $mod.cr = String.fromCharCode($l13);',
  7838. '']));
  7839. end;
  7840. procedure TTestModule.TestProcTwoArgs;
  7841. begin
  7842. StartProgram(false);
  7843. Add('procedure Test(a,b: longint);');
  7844. Add('begin');
  7845. Add('end;');
  7846. Add('begin');
  7847. ConvertProgram;
  7848. CheckSource('TestProcTwoArgs',
  7849. LinesToStr([ // statements
  7850. 'this.Test = function (a,b) {',
  7851. '};'
  7852. ]),
  7853. LinesToStr([ // this.$main
  7854. ''
  7855. ]));
  7856. end;
  7857. procedure TTestModule.TestProc_DefaultValue;
  7858. begin
  7859. StartProgram(false);
  7860. Add('procedure p1(i: longint = 1);');
  7861. Add('begin');
  7862. Add('end;');
  7863. Add('procedure p2(i: longint = 1; c: char = ''a'');');
  7864. Add('begin');
  7865. Add('end;');
  7866. Add('procedure p3(d: double = 1.0; b: boolean = false; s: string = ''abc'');');
  7867. Add('begin');
  7868. Add('end;');
  7869. Add('begin');
  7870. Add(' p1;');
  7871. Add(' p1();');
  7872. Add(' p1(11);');
  7873. Add(' p2;');
  7874. Add(' p2();');
  7875. Add(' p2(12);');
  7876. Add(' p2(13,''b'');');
  7877. Add(' p3();');
  7878. ConvertProgram;
  7879. CheckSource('TestProc_DefaultValue',
  7880. LinesToStr([ // statements
  7881. 'this.p1 = function (i) {',
  7882. '};',
  7883. 'this.p2 = function (i,c) {',
  7884. '};',
  7885. 'this.p3 = function (d,b,s) {',
  7886. '};'
  7887. ]),
  7888. LinesToStr([ // this.$main
  7889. ' $mod.p1(1);',
  7890. ' $mod.p1(1);',
  7891. ' $mod.p1(11);',
  7892. ' $mod.p2(1,"a");',
  7893. ' $mod.p2(1,"a");',
  7894. ' $mod.p2(12,"a");',
  7895. ' $mod.p2(13,"b");',
  7896. ' $mod.p3(1.0,false,"abc");'
  7897. ]));
  7898. end;
  7899. procedure TTestModule.TestFunctionInt;
  7900. begin
  7901. StartProgram(false);
  7902. Add('function MyTest(Bar: longint): longint;');
  7903. Add('begin');
  7904. Add(' Result:=2*bar');
  7905. Add('end;');
  7906. Add('begin');
  7907. ConvertProgram;
  7908. CheckSource('TestFunctionInt',
  7909. LinesToStr([ // statements
  7910. 'this.MyTest = function (Bar) {',
  7911. ' var Result = 0;',
  7912. ' Result = 2*Bar;',
  7913. ' return Result;',
  7914. '};'
  7915. ]),
  7916. LinesToStr([ // this.$main
  7917. ''
  7918. ]));
  7919. end;
  7920. procedure TTestModule.TestFunctionString;
  7921. begin
  7922. StartProgram(false);
  7923. Add('function Test(Bar: string): string;');
  7924. Add('begin');
  7925. Add(' Result:=bar+BAR');
  7926. Add('end;');
  7927. Add('begin');
  7928. ConvertProgram;
  7929. CheckSource('TestFunctionString',
  7930. LinesToStr([ // statements
  7931. 'this.Test = function (Bar) {',
  7932. ' var Result = "";',
  7933. ' Result = Bar+Bar;',
  7934. ' return Result;',
  7935. '};'
  7936. ]),
  7937. LinesToStr([ // this.$main
  7938. ''
  7939. ]));
  7940. end;
  7941. procedure TTestModule.TestIfThen;
  7942. begin
  7943. StartProgram(false);
  7944. Add([
  7945. 'var b: boolean;',
  7946. 'begin',
  7947. ' if b then ;',
  7948. ' if b then else ;']);
  7949. ConvertProgram;
  7950. CheckSource('TestIfThen',
  7951. LinesToStr([ // statements
  7952. 'this.b = false;',
  7953. '']),
  7954. LinesToStr([ // this.$main
  7955. 'if ($mod.b) ;',
  7956. 'if ($mod.b) ;',
  7957. '']));
  7958. end;
  7959. procedure TTestModule.TestForLoop;
  7960. begin
  7961. StartProgram(false);
  7962. Add('var');
  7963. Add(' vI, vJ, vN: longint;');
  7964. Add('begin');
  7965. Add(' VJ:=0;');
  7966. Add(' VN:=3;');
  7967. Add(' for VI:=1 to VN do');
  7968. Add(' begin');
  7969. Add(' VJ:=VJ+VI;');
  7970. Add(' end;');
  7971. ConvertProgram;
  7972. CheckSource('TestForLoop',
  7973. LinesToStr([ // statements
  7974. 'this.vI = 0;',
  7975. 'this.vJ = 0;',
  7976. 'this.vN = 0;'
  7977. ]),
  7978. LinesToStr([ // this.$main
  7979. ' $mod.vJ = 0;',
  7980. ' $mod.vN = 3;',
  7981. ' for (var $l = 1, $end = $mod.vN; $l <= $end; $l++) {',
  7982. ' $mod.vI = $l;',
  7983. ' $mod.vJ = $mod.vJ + $mod.vI;',
  7984. ' };',
  7985. '']));
  7986. end;
  7987. procedure TTestModule.TestForLoopInsideFunction;
  7988. begin
  7989. StartProgram(false);
  7990. Add('function SumNumbers(Count: longint): longint;');
  7991. Add('var');
  7992. Add(' vI, vJ: longint;');
  7993. Add('begin');
  7994. Add(' vj:=0;');
  7995. Add(' for vi:=1 to count do');
  7996. Add(' begin');
  7997. Add(' vj:=vj+vi;');
  7998. Add(' end;');
  7999. Add('end;');
  8000. Add('begin');
  8001. Add(' sumnumbers(3);');
  8002. ConvertProgram;
  8003. CheckSource('TestForLoopInsideFunction',
  8004. LinesToStr([ // statements
  8005. 'this.SumNumbers = function (Count) {',
  8006. ' var Result = 0;',
  8007. ' var vI = 0;',
  8008. ' var vJ = 0;',
  8009. ' vJ = 0;',
  8010. ' for (var $l = 1, $end = Count; $l <= $end; $l++) {',
  8011. ' vI = $l;',
  8012. ' vJ = vJ + vI;',
  8013. ' };',
  8014. ' return Result;',
  8015. '};'
  8016. ]),
  8017. LinesToStr([ // $mod.$main
  8018. ' $mod.SumNumbers(3);'
  8019. ]));
  8020. end;
  8021. procedure TTestModule.TestForLoop_ReadVarAfter;
  8022. begin
  8023. StartProgram(false);
  8024. Add('var');
  8025. Add(' vI: longint;');
  8026. Add('begin');
  8027. Add(' for vi:=1 to 2 do ;');
  8028. Add(' if vi=3 then ;');
  8029. ConvertProgram;
  8030. CheckSource('TestForLoop',
  8031. LinesToStr([ // statements
  8032. 'this.vI = 0;'
  8033. ]),
  8034. LinesToStr([ // this.$main
  8035. ' for ($mod.vI = 1; $mod.vI <= 2; $mod.vI++) ;',
  8036. ' if ($mod.vI===3) ;'
  8037. ]));
  8038. end;
  8039. procedure TTestModule.TestForLoop_Nested;
  8040. begin
  8041. StartProgram(false);
  8042. Add('function SumNumbers(Count: longint): longint;');
  8043. Add('var');
  8044. Add(' vI, vJ, vK: longint;');
  8045. Add('begin');
  8046. Add(' VK:=0;');
  8047. Add(' for VI:=1 to count do');
  8048. Add(' begin');
  8049. Add(' for vj:=1 to vi do');
  8050. Add(' begin');
  8051. Add(' vk:=VK+VI;');
  8052. Add(' end;');
  8053. Add(' end;');
  8054. Add('end;');
  8055. Add('begin');
  8056. Add(' sumnumbers(3);');
  8057. ConvertProgram;
  8058. CheckSource('TestForLoopInFunction',
  8059. LinesToStr([ // statements
  8060. 'this.SumNumbers = function (Count) {',
  8061. ' var Result = 0;',
  8062. ' var vI = 0;',
  8063. ' var vJ = 0;',
  8064. ' var vK = 0;',
  8065. ' vK = 0;',
  8066. ' for (var $l = 1, $end = Count; $l <= $end; $l++) {',
  8067. ' vI = $l;',
  8068. ' for (var $l1 = 1, $end1 = vI; $l1 <= $end1; $l1++) {',
  8069. ' vJ = $l1;',
  8070. ' vK = vK + vI;',
  8071. ' };',
  8072. ' };',
  8073. ' return Result;',
  8074. '};'
  8075. ]),
  8076. LinesToStr([ // $mod.$main
  8077. ' $mod.SumNumbers(3);'
  8078. ]));
  8079. end;
  8080. procedure TTestModule.TestRepeatUntil;
  8081. begin
  8082. StartProgram(false);
  8083. Add('var');
  8084. Add(' vI, vJ, vN: longint;');
  8085. Add('begin');
  8086. Add(' vn:=3;');
  8087. Add(' vj:=0;');
  8088. Add(' VI:=0;');
  8089. Add(' repeat');
  8090. Add(' VI:=vi+1;');
  8091. Add(' vj:=VJ+vI;');
  8092. Add(' until vi>=vn');
  8093. ConvertProgram;
  8094. CheckSource('TestRepeatUntil',
  8095. LinesToStr([ // statements
  8096. 'this.vI = 0;',
  8097. 'this.vJ = 0;',
  8098. 'this.vN = 0;'
  8099. ]),
  8100. LinesToStr([ // $mod.$main
  8101. ' $mod.vN = 3;',
  8102. ' $mod.vJ = 0;',
  8103. ' $mod.vI = 0;',
  8104. ' do{',
  8105. ' $mod.vI = $mod.vI + 1;',
  8106. ' $mod.vJ = $mod.vJ + $mod.vI;',
  8107. ' }while(!($mod.vI>=$mod.vN));'
  8108. ]));
  8109. end;
  8110. procedure TTestModule.TestAsmBlock;
  8111. begin
  8112. StartProgram(false);
  8113. Add([
  8114. 'var',
  8115. ' vI: longint;',
  8116. 'begin',
  8117. ' vi:=1;',
  8118. ' asm',
  8119. ' if (vI===1) {',
  8120. ' vI=2;',
  8121. //' console.log(''end;'');', ToDo
  8122. ' }',
  8123. ' if (vI===2){ vI=3; }',
  8124. ' end;',
  8125. ' VI:=4;']);
  8126. ConvertProgram;
  8127. CheckSource('TestAsmBlock',
  8128. LinesToStr([ // statements
  8129. 'this.vI = 0;'
  8130. ]),
  8131. LinesToStr([ // $mod.$main
  8132. '$mod.vI = 1;',
  8133. 'if (vI===1) {',
  8134. ' vI=2;',
  8135. '}',
  8136. 'if (vI===2){ vI=3; }',
  8137. ';',
  8138. '$mod.vI = 4;'
  8139. ]));
  8140. end;
  8141. procedure TTestModule.TestAsmPas_Impl;
  8142. begin
  8143. StartUnit(false);
  8144. Add('interface');
  8145. Add('const cIntf: longint = 1;');
  8146. Add('var vIntf: longint;');
  8147. Add('implementation');
  8148. Add('const cImpl: longint = 2;');
  8149. Add('var vImpl: longint;');
  8150. Add('procedure DoIt;');
  8151. Add('const cLoc: longint = 3;');
  8152. Add('var vLoc: longint;');
  8153. Add('begin;');
  8154. Add(' asm');
  8155. //Add(' pas(vIntf)=pas(cIntf);');
  8156. //Add(' pas(vImpl)=pas(cImpl);');
  8157. //Add(' pas(vLoc)=pas(cLoc);');
  8158. Add(' end;');
  8159. Add('end;');
  8160. ConvertUnit;
  8161. CheckSource('TestAsmPas_Impl',
  8162. LinesToStr([
  8163. 'var $impl = $mod.$impl;',
  8164. 'this.cIntf = 1;',
  8165. 'this.vIntf = 0;',
  8166. '']),
  8167. '', // this.$init
  8168. LinesToStr([ // implementation
  8169. '$impl.cImpl = 2;',
  8170. '$impl.vImpl = 0;',
  8171. 'var cLoc = 3;',
  8172. '$impl.DoIt = function () {',
  8173. ' var vLoc = 0;',
  8174. '};',
  8175. '']) );
  8176. end;
  8177. procedure TTestModule.TestTryFinally;
  8178. begin
  8179. StartProgram(false);
  8180. Add('var i: longint;');
  8181. Add('begin');
  8182. Add(' try');
  8183. Add(' i:=0; i:=2 div i;');
  8184. Add(' finally');
  8185. Add(' i:=3');
  8186. Add(' end;');
  8187. ConvertProgram;
  8188. CheckSource('TestTryFinally',
  8189. LinesToStr([ // statements
  8190. 'this.i = 0;'
  8191. ]),
  8192. LinesToStr([ // $mod.$main
  8193. 'try {',
  8194. ' $mod.i = 0;',
  8195. ' $mod.i = rtl.trunc(2 / $mod.i);',
  8196. '} finally {',
  8197. ' $mod.i = 3;',
  8198. '};'
  8199. ]));
  8200. end;
  8201. procedure TTestModule.TestTryExcept;
  8202. begin
  8203. StartProgram(false);
  8204. Add([
  8205. 'type',
  8206. ' TObject = class end;',
  8207. ' Exception = class Msg: string; end;',
  8208. ' EInvalidCast = class(Exception) end;',
  8209. 'var vI: longint;',
  8210. 'begin',
  8211. ' try',
  8212. ' vi:=1;',
  8213. ' except',
  8214. ' vi:=2',
  8215. ' end;',
  8216. ' try',
  8217. ' vi:=3;',
  8218. ' except',
  8219. ' raise;',
  8220. ' end;',
  8221. ' try',
  8222. ' VI:=4;',
  8223. ' except',
  8224. ' on einvalidcast do',
  8225. ' raise;',
  8226. ' on E: exception do',
  8227. ' if e.msg='''' then',
  8228. ' raise e;',
  8229. ' else',
  8230. ' vi:=5',
  8231. ' end;',
  8232. ' try',
  8233. ' VI:=6;',
  8234. ' except',
  8235. ' on einvalidcast do ;',
  8236. ' end;',
  8237. '']);
  8238. ConvertProgram;
  8239. CheckSource('TestTryExcept',
  8240. LinesToStr([ // statements
  8241. 'rtl.createClass(this, "TObject", null, function () {',
  8242. ' this.$init = function () {',
  8243. ' };',
  8244. ' this.$final = function () {',
  8245. ' };',
  8246. '});',
  8247. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  8248. ' this.$init = function () {',
  8249. ' $mod.TObject.$init.call(this);',
  8250. ' this.Msg = "";',
  8251. ' };',
  8252. '});',
  8253. 'rtl.createClass(this, "EInvalidCast", this.Exception, function () {',
  8254. '});',
  8255. 'this.vI = 0;'
  8256. ]),
  8257. LinesToStr([ // $mod.$main
  8258. 'try {',
  8259. ' $mod.vI = 1;',
  8260. '} catch ($e) {',
  8261. ' $mod.vI = 2;',
  8262. '};',
  8263. 'try {',
  8264. ' $mod.vI = 3;',
  8265. '} catch ($e) {',
  8266. ' throw $e;',
  8267. '};',
  8268. 'try {',
  8269. ' $mod.vI = 4;',
  8270. '} catch ($e) {',
  8271. ' if ($mod.EInvalidCast.isPrototypeOf($e)){',
  8272. ' throw $e',
  8273. ' } else if ($mod.Exception.isPrototypeOf($e)) {',
  8274. ' var E = $e;',
  8275. ' if (E.Msg === "") throw E;',
  8276. ' } else {',
  8277. ' $mod.vI = 5;',
  8278. ' }',
  8279. '};',
  8280. 'try {',
  8281. ' $mod.vI = 6;',
  8282. '} catch ($e) {',
  8283. ' if ($mod.EInvalidCast.isPrototypeOf($e)){' ,
  8284. ' } else throw $e',
  8285. '};',
  8286. '']));
  8287. end;
  8288. procedure TTestModule.TestTryExcept_ReservedWords;
  8289. begin
  8290. StartProgram(false);
  8291. Add([
  8292. 'type',
  8293. ' TObject = class end;',
  8294. ' Exception = class',
  8295. ' Symbol: string;',
  8296. ' end;',
  8297. 'var &try: longint;',
  8298. 'begin',
  8299. ' try',
  8300. ' &try:=4;',
  8301. ' except',
  8302. ' on Error: exception do',
  8303. ' if errOR.symBol='''' then',
  8304. ' raise ERRor;',
  8305. ' end;',
  8306. '']);
  8307. ConvertProgram;
  8308. CheckSource('TestTryExcept_ReservedWords',
  8309. LinesToStr([ // statements
  8310. 'rtl.createClass(this, "TObject", null, function () {',
  8311. ' this.$init = function () {',
  8312. ' };',
  8313. ' this.$final = function () {',
  8314. ' };',
  8315. '});',
  8316. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  8317. ' this.$init = function () {',
  8318. ' $mod.TObject.$init.call(this);',
  8319. ' this.Symbol = "";',
  8320. ' };',
  8321. '});',
  8322. 'this.Try = 0;',
  8323. '']),
  8324. LinesToStr([ // $mod.$main
  8325. 'try {',
  8326. ' $mod.Try = 4;',
  8327. '} catch ($e) {',
  8328. ' if ($mod.Exception.isPrototypeOf($e)) {',
  8329. ' var error = $e;',
  8330. ' if (error.Symbol === "") throw error;',
  8331. ' } else throw $e',
  8332. '};',
  8333. '']));
  8334. end;
  8335. procedure TTestModule.TestIfThenRaiseElse;
  8336. begin
  8337. StartProgram(false);
  8338. Add([
  8339. 'type',
  8340. ' TObject = class',
  8341. ' constructor Create;',
  8342. ' end;',
  8343. 'constructor TObject.Create;',
  8344. 'begin',
  8345. 'end;',
  8346. 'var b: boolean;',
  8347. 'begin',
  8348. ' if b then',
  8349. ' raise TObject.Create',
  8350. ' else',
  8351. ' b:=false;',
  8352. '']);
  8353. ConvertProgram;
  8354. CheckSource('TestIfThenRaiseElse',
  8355. LinesToStr([ // statements
  8356. 'rtl.createClass(this, "TObject", null, function () {',
  8357. ' this.$init = function () {',
  8358. ' };',
  8359. ' this.$final = function () {',
  8360. ' };',
  8361. ' this.Create = function () {',
  8362. ' return this;',
  8363. ' };',
  8364. '});',
  8365. 'this.b = false;',
  8366. '']),
  8367. LinesToStr([ // $mod.$main
  8368. 'if ($mod.b) {',
  8369. ' throw $mod.TObject.$create("Create")}',
  8370. ' else $mod.b = false;',
  8371. '']));
  8372. end;
  8373. procedure TTestModule.TestCaseOf;
  8374. begin
  8375. StartProgram(false);
  8376. Add([
  8377. 'const e: longint; external name ''$e'';',
  8378. 'var vI: longint;',
  8379. 'begin',
  8380. ' case vi of',
  8381. ' 1: ;',
  8382. ' 2: vi:=3;',
  8383. ' e: ;',
  8384. ' else',
  8385. ' VI:=4',
  8386. ' end;']);
  8387. ConvertProgram;
  8388. CheckSource('TestCaseOf',
  8389. LinesToStr([ // statements
  8390. 'this.vI = 0;'
  8391. ]),
  8392. LinesToStr([ // $mod.$main
  8393. 'var $tmp = $mod.vI;',
  8394. 'if ($tmp === 1) {}',
  8395. 'else if ($tmp === 2) {',
  8396. ' $mod.vI = 3}',
  8397. ' else if ($tmp === $e) {}',
  8398. 'else {',
  8399. ' $mod.vI = 4;',
  8400. '};'
  8401. ]));
  8402. end;
  8403. procedure TTestModule.TestCaseOf_UseSwitch;
  8404. begin
  8405. StartProgram(false);
  8406. Converter.UseSwitchStatement:=true;
  8407. Add('var Vi: longint;');
  8408. Add('begin');
  8409. Add(' case vi of');
  8410. Add(' 1: ;');
  8411. Add(' 2: VI:=3;');
  8412. Add(' else');
  8413. Add(' vi:=4');
  8414. Add(' end;');
  8415. ConvertProgram;
  8416. CheckSource('TestCaseOf_UseSwitch',
  8417. LinesToStr([ // statements
  8418. 'this.Vi = 0;'
  8419. ]),
  8420. LinesToStr([ // $mod.$main
  8421. 'switch ($mod.Vi) {',
  8422. 'case 1:',
  8423. ' break;',
  8424. 'case 2:',
  8425. ' $mod.Vi = 3;',
  8426. ' break;',
  8427. 'default:',
  8428. ' $mod.Vi = 4;',
  8429. '};'
  8430. ]));
  8431. end;
  8432. procedure TTestModule.TestCaseOfNoElse;
  8433. begin
  8434. StartProgram(false);
  8435. Add('var Vi: longint;');
  8436. Add('begin');
  8437. Add(' case vi of');
  8438. Add(' 1: begin vi:=2; VI:=3; end;');
  8439. Add(' end;');
  8440. ConvertProgram;
  8441. CheckSource('TestCaseOfNoElse',
  8442. LinesToStr([ // statements
  8443. 'this.Vi = 0;'
  8444. ]),
  8445. LinesToStr([ // $mod.$main
  8446. 'var $tmp = $mod.Vi;',
  8447. 'if ($tmp === 1) {',
  8448. ' $mod.Vi = 2;',
  8449. ' $mod.Vi = 3;',
  8450. '};'
  8451. ]));
  8452. end;
  8453. procedure TTestModule.TestCaseOfNoElse_UseSwitch;
  8454. begin
  8455. StartProgram(false);
  8456. Converter.UseSwitchStatement:=true;
  8457. Add('var vI: longint;');
  8458. Add('begin');
  8459. Add(' case vi of');
  8460. Add(' 1: begin VI:=2; vi:=3; end;');
  8461. Add(' end;');
  8462. ConvertProgram;
  8463. CheckSource('TestCaseOfNoElse_UseSwitch',
  8464. LinesToStr([ // statements
  8465. 'this.vI = 0;'
  8466. ]),
  8467. LinesToStr([ // $mod.$main
  8468. 'switch ($mod.vI) {',
  8469. 'case 1:',
  8470. ' $mod.vI = 2;',
  8471. ' $mod.vI = 3;',
  8472. ' break;',
  8473. '};'
  8474. ]));
  8475. end;
  8476. procedure TTestModule.TestCaseOfRange;
  8477. begin
  8478. StartProgram(false);
  8479. Add('var vI: longint;');
  8480. Add('begin');
  8481. Add(' case vi of');
  8482. Add(' 1..3: vi:=14;');
  8483. Add(' 4,5: vi:=16;');
  8484. Add(' 6..7,9..10: ;');
  8485. Add(' else ;');
  8486. Add(' end;');
  8487. ConvertProgram;
  8488. CheckSource('TestCaseOfRange',
  8489. LinesToStr([ // statements
  8490. 'this.vI = 0;'
  8491. ]),
  8492. LinesToStr([ // $mod.$main
  8493. 'var $tmp = $mod.vI;',
  8494. 'if (($tmp >= 1) && ($tmp <= 3)){',
  8495. ' $mod.vI = 14',
  8496. '} else if (($tmp === 4) || ($tmp === 5)){',
  8497. ' $mod.vI = 16',
  8498. '} else if ((($tmp >= 6) && ($tmp <= 7)) || (($tmp >= 9) && ($tmp <= 10))) ;'
  8499. ]));
  8500. end;
  8501. procedure TTestModule.TestCaseOfString;
  8502. begin
  8503. StartProgram(false);
  8504. Add([
  8505. 'var s,h: string;',
  8506. 'begin',
  8507. ' case s of',
  8508. ' ''foo'': s:=h;',
  8509. ' ''a''..''z'': h:=s;',
  8510. ' ''ў'', ''ё'': ;',
  8511. ' ''Б''..''Я'': ;',
  8512. ' end;',
  8513. '']);
  8514. ConvertProgram;
  8515. CheckSource('TestCaseOfString',
  8516. LinesToStr([ // statements
  8517. 'this.s = "";',
  8518. 'this.h = "";',
  8519. '']),
  8520. LinesToStr([ // $mod.$main
  8521. 'var $tmp = $mod.s;',
  8522. 'if ($tmp === "foo") {',
  8523. ' $mod.s = $mod.h}',
  8524. ' else if (($tmp.length === 1) && ($tmp >= "a") && ($tmp <= "z")) {',
  8525. ' $mod.h = $mod.s}',
  8526. ' else if (($tmp === "ў") || ($tmp === "ё")) {}',
  8527. ' else if (($tmp.length === 1) && ($tmp >= "Б") && ($tmp <= "Я")) ;',
  8528. '']));
  8529. end;
  8530. procedure TTestModule.TestCaseOfChar;
  8531. begin
  8532. StartProgram(false);
  8533. Add([
  8534. 'var s,h: char;',
  8535. 'begin',
  8536. ' case s of',
  8537. ' ''a''..''z'': h:=s;',
  8538. ' ''ä'': ;',
  8539. ' ''ў'', ''ё'': ;',
  8540. ' ''Б''..''Я'': ;',
  8541. ' end;',
  8542. '']);
  8543. ConvertProgram;
  8544. CheckSource('TestCaseOfString',
  8545. LinesToStr([ // statements
  8546. 'this.s = "";',
  8547. 'this.h = "";',
  8548. '']),
  8549. LinesToStr([ // $mod.$main
  8550. 'var $tmp = $mod.s;',
  8551. 'if (($tmp >= "a") && ($tmp <= "z")) {',
  8552. ' $mod.h = $mod.s}',
  8553. ' else if ($tmp === "ä") {}',
  8554. ' else if (($tmp === "ў") || ($tmp === "ё")) {}',
  8555. ' else if (($tmp >= "Б") && ($tmp <= "Я")) ;',
  8556. '']));
  8557. end;
  8558. procedure TTestModule.TestCaseOfExternalClassConst;
  8559. begin
  8560. StartProgram(false);
  8561. Add([
  8562. '{$modeswitch externalclass}',
  8563. 'type',
  8564. ' TBird = class external name ''Bird''',
  8565. ' const e: longint;',
  8566. ' end;',
  8567. 'var vI: longint;',
  8568. 'begin',
  8569. ' case vi of',
  8570. ' 1: vi:=3;',
  8571. ' TBird.e: ;',
  8572. ' end;']);
  8573. ConvertProgram;
  8574. CheckSource('TestCaseOfExternalClassConst',
  8575. LinesToStr([ // statements
  8576. 'this.vI = 0;'
  8577. ]),
  8578. LinesToStr([ // $mod.$main
  8579. 'var $tmp = $mod.vI;',
  8580. 'if ($tmp === 1) {',
  8581. ' $mod.vI = 3}',
  8582. ' else if ($tmp === Bird.e) ;'
  8583. ]));
  8584. end;
  8585. procedure TTestModule.TestDebugger;
  8586. begin
  8587. StartProgram(false);
  8588. Add([
  8589. 'procedure DoIt;',
  8590. 'begin',
  8591. ' deBugger;',
  8592. ' DeBugger();',
  8593. 'end;',
  8594. 'begin',
  8595. ' Debugger;']);
  8596. ConvertProgram;
  8597. CheckSource('TestDebugger',
  8598. LinesToStr([ // statements
  8599. 'this.DoIt = function () {',
  8600. ' debugger;',
  8601. ' debugger;',
  8602. '};',
  8603. '']),
  8604. LinesToStr([ // $mod.$main
  8605. 'debugger;',
  8606. '']));
  8607. end;
  8608. procedure TTestModule.TestArray_Dynamic;
  8609. begin
  8610. StartProgram(false);
  8611. Add([
  8612. 'type',
  8613. ' TArrayInt = array of longint;',
  8614. 'var',
  8615. ' Arr: TArrayInt;',
  8616. ' i: longint;',
  8617. ' b: boolean;',
  8618. 'begin',
  8619. ' SetLength(arr,3);',
  8620. ' arr[0]:=4;',
  8621. ' arr[1]:=length(arr)+arr[0];',
  8622. ' arr[i]:=5;',
  8623. ' arr[arr[i]]:=arr[6];',
  8624. ' i:=low(arr);',
  8625. ' i:=high(arr);',
  8626. ' b:=Assigned(arr);',
  8627. ' Arr:=default(TArrayInt);']);
  8628. ConvertProgram;
  8629. CheckSource('TestArray_Dynamic',
  8630. LinesToStr([ // statements
  8631. 'this.Arr = [];',
  8632. 'this.i = 0;',
  8633. 'this.b = false;'
  8634. ]),
  8635. LinesToStr([ // $mod.$main
  8636. '$mod.Arr = rtl.arraySetLength($mod.Arr,0,3);',
  8637. '$mod.Arr[0] = 4;',
  8638. '$mod.Arr[1] = rtl.length($mod.Arr) + $mod.Arr[0];',
  8639. '$mod.Arr[$mod.i] = 5;',
  8640. '$mod.Arr[$mod.Arr[$mod.i]] = $mod.Arr[6];',
  8641. '$mod.i = 0;',
  8642. '$mod.i = rtl.length($mod.Arr) - 1;',
  8643. '$mod.b = rtl.length($mod.Arr) > 0;',
  8644. '$mod.Arr = [];',
  8645. '']));
  8646. end;
  8647. procedure TTestModule.TestArray_Dynamic_Nil;
  8648. begin
  8649. StartProgram(false);
  8650. Add('type');
  8651. Add(' TArrayInt = array of longint;');
  8652. Add('var');
  8653. Add(' Arr: TArrayInt;');
  8654. Add('procedure DoIt(const i: TArrayInt; j: TArrayInt); begin end;');
  8655. Add('begin');
  8656. Add(' arr:=nil;');
  8657. Add(' if arr=nil then;');
  8658. Add(' if nil=arr then;');
  8659. Add(' if arr<>nil then;');
  8660. Add(' if nil<>arr then;');
  8661. Add(' DoIt(nil,nil);');
  8662. ConvertProgram;
  8663. CheckSource('TestArray_Dynamic',
  8664. LinesToStr([ // statements
  8665. 'this.Arr = [];',
  8666. 'this.DoIt = function(i,j){',
  8667. '};'
  8668. ]),
  8669. LinesToStr([ // $mod.$main
  8670. '$mod.Arr = [];',
  8671. 'if (rtl.length($mod.Arr) === 0) ;',
  8672. 'if (rtl.length($mod.Arr) === 0) ;',
  8673. 'if (rtl.length($mod.Arr) > 0) ;',
  8674. 'if (rtl.length($mod.Arr) > 0) ;',
  8675. '$mod.DoIt([],[]);',
  8676. '']));
  8677. end;
  8678. procedure TTestModule.TestArray_DynMultiDimensional;
  8679. begin
  8680. StartProgram(false);
  8681. Add([
  8682. 'type',
  8683. ' TArrayInt = array of longint;',
  8684. ' TArrayArrayInt = array of TArrayInt;',
  8685. 'var',
  8686. ' Arr: TArrayInt;',
  8687. ' Arr2: TArrayArrayInt;',
  8688. ' i: longint;',
  8689. 'begin',
  8690. ' arr2:=nil;',
  8691. ' if arr2=nil then;',
  8692. ' if nil=arr2 then;',
  8693. ' i:=low(arr2);',
  8694. ' i:=low(arr2[1]);',
  8695. ' i:=high(arr2);',
  8696. ' i:=high(arr2[2]);',
  8697. ' arr2[3]:=arr;',
  8698. ' arr2[4][5]:=i;',
  8699. ' i:=arr2[6][7];',
  8700. ' arr2[8,9]:=i;',
  8701. ' i:=arr2[10,11];',
  8702. ' SetLength(arr2,14);',
  8703. ' SetLength(arr2[15],16);']);
  8704. ConvertProgram;
  8705. CheckSource('TestArray_Dynamic',
  8706. LinesToStr([ // statements
  8707. 'this.Arr = [];',
  8708. 'this.Arr2 = [];',
  8709. 'this.i = 0;'
  8710. ]),
  8711. LinesToStr([ // $mod.$main
  8712. '$mod.Arr2 = [];',
  8713. 'if (rtl.length($mod.Arr2) === 0) ;',
  8714. 'if (rtl.length($mod.Arr2) === 0) ;',
  8715. '$mod.i = 0;',
  8716. '$mod.i = 0;',
  8717. '$mod.i = rtl.length($mod.Arr2) - 1;',
  8718. '$mod.i = rtl.length($mod.Arr2[2]) - 1;',
  8719. '$mod.Arr2[3] = rtl.arrayRef($mod.Arr);',
  8720. '$mod.Arr2[4][5] = $mod.i;',
  8721. '$mod.i = $mod.Arr2[6][7];',
  8722. '$mod.Arr2[8][9] = $mod.i;',
  8723. '$mod.i = $mod.Arr2[10][11];',
  8724. '$mod.Arr2 = rtl.arraySetLength($mod.Arr2, [], 14);',
  8725. '$mod.Arr2[15] = rtl.arraySetLength($mod.Arr2[15], 0, 16);',
  8726. '']));
  8727. end;
  8728. procedure TTestModule.TestArray_DynamicAssign;
  8729. begin
  8730. StartProgram(false);
  8731. Add([
  8732. 'type',
  8733. ' TArrayInt = array of longint;',
  8734. ' TArrayArrayInt = array of TArrayInt;',
  8735. 'procedure Run(a: TArrayInt; const b: TArrayInt; constref c: TArrayInt);',
  8736. 'begin',
  8737. 'end;',
  8738. 'procedure Fly(var a: TArrayInt);',
  8739. 'begin',
  8740. 'end;',
  8741. 'var',
  8742. ' Arr: TArrayInt;',
  8743. ' Arr2: TArrayArrayInt;',
  8744. 'begin',
  8745. ' arr:=nil;',
  8746. ' arr2:=nil;',
  8747. ' arr2[1]:=nil;',
  8748. ' arr2[2]:=arr;',
  8749. ' Run(arr,arr,arr);',
  8750. ' Fly(arr);',
  8751. ' Run(arr2[4],arr2[5],arr2[6]);',
  8752. ' Fly(arr2[7]);',
  8753. '']);
  8754. ConvertProgram;
  8755. CheckSource('TestArray_DynamicAssign',
  8756. LinesToStr([ // statements
  8757. 'this.Run = function (a, b, c) {',
  8758. '};',
  8759. 'this.Fly = function (a) {',
  8760. '};',
  8761. 'this.Arr = [];',
  8762. 'this.Arr2 = [];',
  8763. '']),
  8764. LinesToStr([ // $mod.$main
  8765. '$mod.Arr = [];',
  8766. '$mod.Arr2 = [];',
  8767. '$mod.Arr2[1] = [];',
  8768. '$mod.Arr2[2] = rtl.arrayRef($mod.Arr);',
  8769. '$mod.Run(rtl.arrayRef($mod.Arr), $mod.Arr, $mod.Arr);',
  8770. '$mod.Fly({',
  8771. ' p: $mod,',
  8772. ' get: function () {',
  8773. ' return this.p.Arr;',
  8774. ' },',
  8775. ' set: function (v) {',
  8776. ' this.p.Arr = v;',
  8777. ' }',
  8778. '});',
  8779. '$mod.Run(rtl.arrayRef($mod.Arr2[4]), $mod.Arr2[5], $mod.Arr2[6]);',
  8780. '$mod.Fly({',
  8781. ' a: 7,',
  8782. ' p: $mod.Arr2,',
  8783. ' get: function () {',
  8784. ' return this.p[this.a];',
  8785. ' },',
  8786. ' set: function (v) {',
  8787. ' this.p[this.a] = v;',
  8788. ' }',
  8789. '});',
  8790. '']));
  8791. end;
  8792. procedure TTestModule.TestArray_StaticInt;
  8793. begin
  8794. StartProgram(false);
  8795. Add('type');
  8796. Add(' TArrayInt = array[2..4] of longint;');
  8797. Add('var');
  8798. Add(' Arr: TArrayInt;');
  8799. Add(' Arr2: TArrayInt = (5,6,7);');
  8800. Add(' i: longint;');
  8801. Add(' b: boolean;');
  8802. Add('begin');
  8803. Add(' arr[2]:=4;');
  8804. Add(' arr[3]:=arr[2]+arr[3];');
  8805. Add(' arr[i]:=5;');
  8806. Add(' arr[arr[i]]:=arr[high(arr)];');
  8807. Add(' i:=low(arr);');
  8808. Add(' i:=high(arr);');
  8809. Add(' b:=arr[2]=arr[3];');
  8810. Add(' arr:=default(TArrayInt);');
  8811. ConvertProgram;
  8812. CheckSource('TestArray_StaticInt',
  8813. LinesToStr([ // statements
  8814. 'this.Arr = rtl.arraySetLength(null,0,3);',
  8815. 'this.Arr2 = [5, 6, 7];',
  8816. 'this.i = 0;',
  8817. 'this.b = false;'
  8818. ]),
  8819. LinesToStr([ // $mod.$main
  8820. '$mod.Arr[0] = 4;',
  8821. '$mod.Arr[1] = $mod.Arr[0] + $mod.Arr[1];',
  8822. '$mod.Arr[$mod.i-2] = 5;',
  8823. '$mod.Arr[$mod.Arr[$mod.i-2]-2] = $mod.Arr[2];',
  8824. '$mod.i = 2;',
  8825. '$mod.i = 4;',
  8826. '$mod.b = $mod.Arr[0] === $mod.Arr[1];',
  8827. '$mod.Arr = rtl.arraySetLength(null,0,3);',
  8828. '']));
  8829. end;
  8830. procedure TTestModule.TestArray_StaticBool;
  8831. begin
  8832. StartProgram(false);
  8833. Add('type');
  8834. Add(' TBools = array[boolean] of boolean;');
  8835. Add(' TBool2 = array[true..true] of boolean;');
  8836. Add('var');
  8837. Add(' Arr: TBools;');
  8838. Add(' Arr2: TBool2;');
  8839. Add(' Arr3: TBools = (true,false);');
  8840. Add(' b: boolean;');
  8841. Add('begin');
  8842. Add(' b:=low(arr);');
  8843. Add(' b:=high(arr);');
  8844. Add(' arr[true]:=false;');
  8845. Add(' arr[false]:=arr[b] or arr[true];');
  8846. Add(' arr[b]:=true;');
  8847. Add(' arr[arr[b]]:=arr[high(arr)];');
  8848. Add(' b:=arr[false]=arr[true];');
  8849. Add(' b:=low(arr2);');
  8850. Add(' b:=high(arr2);');
  8851. Add(' arr2[true]:=true;');
  8852. Add(' arr2[true]:=arr2[true] and arr2[b];');
  8853. Add(' arr2[b]:=false;');
  8854. ConvertProgram;
  8855. CheckSource('TestArray_StaticBool',
  8856. LinesToStr([ // statements
  8857. 'this.Arr = rtl.arraySetLength(null,false,2);',
  8858. 'this.Arr2 = rtl.arraySetLength(null,false,1);',
  8859. 'this.Arr3 = [true, false];',
  8860. 'this.b = false;'
  8861. ]),
  8862. LinesToStr([ // $mod.$main
  8863. '$mod.b = false;',
  8864. '$mod.b = true;',
  8865. '$mod.Arr[1] = false;',
  8866. '$mod.Arr[0] = $mod.Arr[+$mod.b] || $mod.Arr[1];',
  8867. '$mod.Arr[+$mod.b] = true;',
  8868. '$mod.Arr[+$mod.Arr[+$mod.b]] = $mod.Arr[1];',
  8869. '$mod.b = $mod.Arr[0] === $mod.Arr[1];',
  8870. '$mod.b = true;',
  8871. '$mod.b = true;',
  8872. '$mod.Arr2[0] = true;',
  8873. '$mod.Arr2[0] = $mod.Arr2[0] && $mod.Arr2[1-$mod.b];',
  8874. '$mod.Arr2[1-$mod.b] = false;',
  8875. '']));
  8876. end;
  8877. procedure TTestModule.TestArray_StaticChar;
  8878. begin
  8879. StartProgram(false);
  8880. Add([
  8881. 'type',
  8882. ' TChars = array[char] of char;',
  8883. ' TChars2 = array[''a''..''z''] of char;',
  8884. 'var',
  8885. ' Arr: TChars;',
  8886. ' Arr2: TChars2;',
  8887. ' Arr3: array[2..4] of char = (''p'',''a'',''s'');',
  8888. ' Arr4: array[11..13] of char = ''pas'';',
  8889. ' Arr5: array[21..22] of char = ''äö'';',
  8890. ' Arr6: array[31..32] of char = ''ä''+''ö'';',
  8891. ' c: char;',
  8892. ' b: boolean;',
  8893. 'begin',
  8894. ' c:=low(arr);',
  8895. ' c:=high(arr);',
  8896. ' arr[''B'']:=''a'';',
  8897. ' arr[''D'']:=arr[c];',
  8898. ' arr[c]:=arr[''d''];',
  8899. ' arr[arr[c]]:=arr[high(arr)];',
  8900. ' b:=arr[low(arr)]=arr[''e''];',
  8901. ' c:=low(arr2);',
  8902. ' c:=high(arr2);',
  8903. ' arr2[''b'']:=''f'';',
  8904. ' arr2[''a'']:=arr2[c];',
  8905. ' arr2[c]:=arr2[''g''];']);
  8906. ConvertProgram;
  8907. CheckSource('TestArray_StaticChar',
  8908. LinesToStr([ // statements
  8909. 'this.Arr = rtl.arraySetLength(null, "", 65536);',
  8910. 'this.Arr2 = rtl.arraySetLength(null, "", 26);',
  8911. 'this.Arr3 = ["p", "a", "s"];',
  8912. 'this.Arr4 = ["p", "a", "s"];',
  8913. 'this.Arr5 = ["ä", "ö"];',
  8914. 'this.Arr6 = ["ä", "ö"];',
  8915. 'this.c = "";',
  8916. 'this.b = false;',
  8917. '']),
  8918. LinesToStr([ // $mod.$main
  8919. '$mod.c = "\x00";',
  8920. '$mod.c = "\uFFFF";',
  8921. '$mod.Arr[66] = "a";',
  8922. '$mod.Arr[68] = $mod.Arr[$mod.c.charCodeAt()];',
  8923. '$mod.Arr[$mod.c.charCodeAt()] = $mod.Arr[100];',
  8924. '$mod.Arr[$mod.Arr[$mod.c.charCodeAt()].charCodeAt()] = $mod.Arr[65535];',
  8925. '$mod.b = $mod.Arr[0] === $mod.Arr[101];',
  8926. '$mod.c = "a";',
  8927. '$mod.c = "z";',
  8928. '$mod.Arr2[1] = "f";',
  8929. '$mod.Arr2[0] = $mod.Arr2[$mod.c.charCodeAt() - 97];',
  8930. '$mod.Arr2[$mod.c.charCodeAt() - 97] = $mod.Arr2[6];',
  8931. '']));
  8932. end;
  8933. procedure TTestModule.TestArray_StaticMultiDim;
  8934. begin
  8935. StartProgram(false);
  8936. Add([
  8937. 'type',
  8938. ' TArrayInt = array[1..3] of longint;',
  8939. ' TArrayArrayInt = array[5..6] of TArrayInt;',
  8940. 'var',
  8941. ' Arr: TArrayInt;',
  8942. ' Arr2: TArrayArrayInt;',
  8943. ' Arr3: array[boolean] of TArrayInt = ((11,12,13),(21,22,23));',
  8944. ' i: longint;',
  8945. 'begin',
  8946. ' i:=low(arr);',
  8947. ' i:=low(arr2);',
  8948. ' i:=low(arr2[5]);',
  8949. ' i:=high(arr);',
  8950. ' i:=high(arr2);',
  8951. ' i:=high(arr2[6]);',
  8952. ' arr2[5]:=arr;',
  8953. ' arr2[6][2]:=i;',
  8954. ' i:=arr2[6][3];',
  8955. ' arr2[6,3]:=i;',
  8956. ' i:=arr2[5,2];',
  8957. ' arr2:=arr2;',// clone multi dim static array
  8958. //' arr3:=arr3;',// clone anonymous multi dim static array
  8959. '']);
  8960. ConvertProgram;
  8961. CheckSource('TestArray_StaticMultiDim',
  8962. LinesToStr([ // statements
  8963. 'this.TArrayArrayInt$clone = function (a) {',
  8964. ' var r = [];',
  8965. ' for (var i = 0; i < 2; i++) r.push(a[i].slice(0));',
  8966. ' return r;',
  8967. '};',
  8968. 'this.Arr = rtl.arraySetLength(null, 0, 3);',
  8969. 'this.Arr2 = rtl.arraySetLength(null, 0, 2, 3);',
  8970. 'this.Arr3 = [[11, 12, 13], [21, 22, 23]];',
  8971. 'this.i = 0;'
  8972. ]),
  8973. LinesToStr([ // $mod.$main
  8974. '$mod.i = 1;',
  8975. '$mod.i = 5;',
  8976. '$mod.i = 1;',
  8977. '$mod.i = 3;',
  8978. '$mod.i = 6;',
  8979. '$mod.i = 3;',
  8980. '$mod.Arr2[0] = $mod.Arr.slice(0);',
  8981. '$mod.Arr2[1][1] = $mod.i;',
  8982. '$mod.i = $mod.Arr2[1][2];',
  8983. '$mod.Arr2[1][2] = $mod.i;',
  8984. '$mod.i = $mod.Arr2[0][1];',
  8985. '$mod.Arr2 = $mod.TArrayArrayInt$clone($mod.Arr2);',
  8986. '']));
  8987. end;
  8988. procedure TTestModule.TestArray_StaticInFunction;
  8989. begin
  8990. StartProgram(false);
  8991. Add([
  8992. 'const TArrayInt = 3;',
  8993. 'const TArrayArrayInt = 4;',
  8994. 'procedure DoIt;',
  8995. 'type',
  8996. ' TArrayInt = array[1..3] of longint;',
  8997. ' TArrayArrayInt = array[5..6] of TArrayInt;',
  8998. 'var',
  8999. ' Arr: TArrayInt;',
  9000. ' Arr2: TArrayArrayInt;',
  9001. ' Arr3: array[boolean] of TArrayInt = ((11,12,13),(21,22,23));',
  9002. ' i: longint;',
  9003. 'begin',
  9004. ' arr2[5]:=arr;',
  9005. ' arr2:=arr2;',// clone multi dim static array
  9006. 'end;',
  9007. 'begin',
  9008. '']);
  9009. ConvertProgram;
  9010. CheckSource('TestArray_StaticInFunction',
  9011. LinesToStr([ // statements
  9012. 'this.TArrayInt = 3;',
  9013. 'this.TArrayArrayInt = 4;',
  9014. 'var TArrayArrayInt$1$clone = function (a) {',
  9015. ' var r = [];',
  9016. ' for (var i = 0; i < 2; i++) r.push(a[i].slice(0));',
  9017. ' return r;',
  9018. '};',
  9019. 'this.DoIt = function () {',
  9020. ' var Arr = rtl.arraySetLength(null, 0, 3);',
  9021. ' var Arr2 = rtl.arraySetLength(null, 0, 2, 3);',
  9022. ' var Arr3 = [[11, 12, 13], [21, 22, 23]];',
  9023. ' var i = 0;',
  9024. ' Arr2[0] = Arr.slice(0);',
  9025. ' Arr2 = TArrayArrayInt$1$clone(Arr2);',
  9026. '};',
  9027. '']),
  9028. LinesToStr([ // $mod.$main
  9029. '']));
  9030. end;
  9031. procedure TTestModule.TestArray_StaticMultiDimEqualNotImplemented;
  9032. begin
  9033. StartProgram(false);
  9034. Add([
  9035. 'type',
  9036. ' TArrayInt = array[1..3,1..2] of longint;',
  9037. 'var',
  9038. ' a,b: TArrayInt;',
  9039. 'begin',
  9040. ' if a=b then ;',
  9041. '']);
  9042. SetExpectedPasResolverError('compare static array is not supported',
  9043. nXIsNotSupported);
  9044. ConvertProgram;
  9045. end;
  9046. procedure TTestModule.TestArrayOfRecord;
  9047. begin
  9048. StartProgram(false);
  9049. Add([
  9050. 'type',
  9051. ' TRec = record',
  9052. ' Int: longint;',
  9053. ' end;',
  9054. ' TArrayRec = array of TRec;',
  9055. 'procedure DoIt(vd: TRec; const vc: TRec; var vv: TRec);',
  9056. 'begin',
  9057. 'end;',
  9058. 'var',
  9059. ' Arr: TArrayRec;',
  9060. ' r: TRec;',
  9061. ' i: longint;',
  9062. 'begin',
  9063. ' SetLength(arr,3);',
  9064. ' arr[0].int:=4;',
  9065. ' arr[1].int:=length(arr)+arr[2].int;',
  9066. ' arr[arr[i].int].int:=arr[5].int;',
  9067. ' arr[7]:=r;',
  9068. ' r:=arr[8];',
  9069. ' i:=low(arr);',
  9070. ' i:=high(arr);',
  9071. ' DoIt(Arr[9],Arr[10],Arr[11]);']);
  9072. ConvertProgram;
  9073. CheckSource('TestArrayOfRecord',
  9074. LinesToStr([ // statements
  9075. 'rtl.recNewT(this, "TRec", function () {',
  9076. ' this.Int = 0;',
  9077. ' this.$eq = function (b) {',
  9078. ' return this.Int === b.Int;',
  9079. ' };',
  9080. ' this.$assign = function (s) {',
  9081. ' this.Int = s.Int;',
  9082. ' return this;',
  9083. ' };',
  9084. '});',
  9085. 'this.DoIt = function (vd, vc, vv) {',
  9086. '};',
  9087. 'this.Arr = [];',
  9088. 'this.r = this.TRec.$new();',
  9089. 'this.i = 0;'
  9090. ]),
  9091. LinesToStr([ // $mod.$main
  9092. '$mod.Arr = rtl.arraySetLength($mod.Arr,$mod.TRec,3);',
  9093. '$mod.Arr[0].Int = 4;',
  9094. '$mod.Arr[1].Int = rtl.length($mod.Arr)+$mod.Arr[2].Int;',
  9095. '$mod.Arr[$mod.Arr[$mod.i].Int].Int = $mod.Arr[5].Int;',
  9096. '$mod.Arr[7].$assign($mod.r);',
  9097. '$mod.r.$assign($mod.Arr[8]);',
  9098. '$mod.i = 0;',
  9099. '$mod.i = rtl.length($mod.Arr)-1;',
  9100. '$mod.DoIt($mod.TRec.$clone($mod.Arr[9]), $mod.Arr[10], $mod.Arr[11]);',
  9101. '']));
  9102. end;
  9103. procedure TTestModule.TestArray_StaticRecord;
  9104. begin
  9105. StartProgram(false);
  9106. Add([
  9107. 'type',
  9108. ' TRec = record',
  9109. ' Int: longint;',
  9110. ' end;',
  9111. ' TArrayRec = array[1..2] of TRec;',
  9112. 'var',
  9113. ' Arr: TArrayRec;',
  9114. 'begin',
  9115. ' arr[1].int:=length(arr)+low(arr)+high(arr);',
  9116. '']);
  9117. ConvertProgram;
  9118. CheckSource('TestArray_StaticRecord',
  9119. LinesToStr([ // statements
  9120. 'rtl.recNewT(this, "TRec", function () {',
  9121. ' this.Int = 0;',
  9122. ' this.$eq = function (b) {',
  9123. ' return this.Int === b.Int;',
  9124. ' };',
  9125. ' this.$assign = function (s) {',
  9126. ' this.Int = s.Int;',
  9127. ' return this;',
  9128. ' };',
  9129. '});',
  9130. 'this.TArrayRec$clone = function (a) {',
  9131. ' var r = [];',
  9132. ' for (var i = 0; i < 2; i++) r.push($mod.TRec.$clone(a[i]));',
  9133. ' return r;',
  9134. '};',
  9135. 'this.Arr = rtl.arraySetLength(null, this.TRec, 2);',
  9136. '']),
  9137. LinesToStr([ // $mod.$main
  9138. '$mod.Arr[0].Int = 2 + 1 + 2;']));
  9139. end;
  9140. procedure TTestModule.TestArrayOfSet;
  9141. begin
  9142. StartProgram(false);
  9143. Add([
  9144. 'type',
  9145. ' TFlag = (big,small);',
  9146. ' TSetOfFlag = set of tflag;',
  9147. ' TArrayFlag = array of TSetOfFlag;',
  9148. 'procedure DoIt(const a: Tarrayflag);',
  9149. 'begin',
  9150. 'end;',
  9151. 'var',
  9152. ' f: TFlag;',
  9153. ' s: TSetOfFlag;',
  9154. ' Arr: TArrayFlag;',
  9155. ' i: longint;',
  9156. 'begin',
  9157. ' SetLength(arr,3);',
  9158. ' arr[0]:=s;',
  9159. ' arr[1]:=[big];',
  9160. ' arr[2]:=[big]+s;',
  9161. ' arr[3]:=s+[big];',
  9162. ' arr[4]:=arr[5];',
  9163. ' s:=arr[6];',
  9164. ' i:=low(arr);',
  9165. ' i:=high(arr);',
  9166. ' DoIt(arr);',
  9167. ' DoIt([s]);',
  9168. ' DoIt([[],s]);',
  9169. ' DoIt([s,[]]);',
  9170. '']);
  9171. ConvertProgram;
  9172. CheckSource('TestArrayOfSet',
  9173. LinesToStr([ // statements
  9174. 'this.TFlag = {',
  9175. ' "0": "big",',
  9176. ' big: 0,',
  9177. ' "1": "small",',
  9178. ' small: 1',
  9179. '};',
  9180. 'this.DoIt = function (a) {',
  9181. '};',
  9182. 'this.f = 0;',
  9183. 'this.s = {};',
  9184. 'this.Arr = [];',
  9185. 'this.i = 0;',
  9186. '']),
  9187. LinesToStr([ // $mod.$main
  9188. '$mod.Arr = rtl.arraySetLength($mod.Arr, {}, 3);',
  9189. '$mod.Arr[0] = rtl.refSet($mod.s);',
  9190. '$mod.Arr[1] = rtl.createSet($mod.TFlag.big);',
  9191. '$mod.Arr[2] = rtl.unionSet(rtl.createSet($mod.TFlag.big), $mod.s);',
  9192. '$mod.Arr[3] = rtl.unionSet($mod.s, rtl.createSet($mod.TFlag.big));',
  9193. '$mod.Arr[4] = rtl.refSet($mod.Arr[5]);',
  9194. '$mod.s = rtl.refSet($mod.Arr[6]);',
  9195. '$mod.i = 0;',
  9196. '$mod.i = rtl.length($mod.Arr) - 1;',
  9197. '$mod.DoIt($mod.Arr);',
  9198. '$mod.DoIt([rtl.refSet($mod.s)]);',
  9199. '$mod.DoIt([{}, rtl.refSet($mod.s)]);',
  9200. '$mod.DoIt([rtl.refSet($mod.s), {}]);',
  9201. '']));
  9202. end;
  9203. procedure TTestModule.TestArray_DynAsParam;
  9204. begin
  9205. StartProgram(false);
  9206. Add([
  9207. 'type integer = longint;',
  9208. 'type TArrInt = array of integer;',
  9209. 'procedure DoIt(vG: TArrInt; const vH: TArrInt; var vI: TArrInt);',
  9210. 'var vJ: TArrInt;',
  9211. 'begin',
  9212. ' vg:=vg;',
  9213. ' vj:=vh;',
  9214. ' vi:=vi;',
  9215. ' doit(vg,vg,vg);',
  9216. ' doit(vh,vh,vj);',
  9217. ' doit(vi,vi,vi);',
  9218. ' doit(vj,vj,vj);',
  9219. 'end;',
  9220. 'var i: TArrInt;',
  9221. 'begin',
  9222. ' doit(i,i,i);']);
  9223. ConvertProgram;
  9224. CheckSource('TestArray_DynAsParams',
  9225. LinesToStr([ // statements
  9226. 'this.DoIt = function (vG,vH,vI) {',
  9227. ' var vJ = [];',
  9228. ' vG = rtl.arrayRef(vG);',
  9229. ' vJ = rtl.arrayRef(vH);',
  9230. ' vI.set(rtl.arrayRef(vI.get()));',
  9231. ' $mod.DoIt(rtl.arrayRef(vG), vG, {',
  9232. ' get: function () {',
  9233. ' return vG;',
  9234. ' },',
  9235. ' set: function (v) {',
  9236. ' vG = v;',
  9237. ' }',
  9238. ' });',
  9239. ' $mod.DoIt(rtl.arrayRef(vH), vH, {',
  9240. ' get: function () {',
  9241. ' return vJ;',
  9242. ' },',
  9243. ' set: function (v) {',
  9244. ' vJ = v;',
  9245. ' }',
  9246. ' });',
  9247. ' $mod.DoIt(rtl.arrayRef(vI.get()), vI.get(), vI);',
  9248. ' $mod.DoIt(rtl.arrayRef(vJ), vJ, {',
  9249. ' get: function () {',
  9250. ' return vJ;',
  9251. ' },',
  9252. ' set: function (v) {',
  9253. ' vJ = v;',
  9254. ' }',
  9255. ' });',
  9256. '};',
  9257. 'this.i = [];'
  9258. ]),
  9259. LinesToStr([
  9260. '$mod.DoIt(rtl.arrayRef($mod.i),$mod.i,{',
  9261. ' p: $mod,',
  9262. ' get: function () {',
  9263. ' return this.p.i;',
  9264. ' },',
  9265. ' set: function (v) {',
  9266. ' this.p.i = v;',
  9267. ' }',
  9268. '});'
  9269. ]));
  9270. end;
  9271. procedure TTestModule.TestArray_StaticAsParam;
  9272. begin
  9273. StartProgram(false);
  9274. Add([
  9275. 'type integer = longint;',
  9276. 'type TArrInt = array[1..2] of integer;',
  9277. 'procedure DoIt(vG: TArrInt; const vH: TArrInt; var vI: TArrInt);',
  9278. 'var vJ: TArrInt;',
  9279. 'begin',
  9280. ' vg:=vg;',
  9281. ' vj:=vh;',
  9282. ' vi:=vi;',
  9283. ' doit(vg,vg,vg);',
  9284. ' doit(vh,vh,vj);',
  9285. ' doit(vi,vi,vi);',
  9286. ' doit(vj,vj,vj);',
  9287. 'end;',
  9288. 'var i: TArrInt;',
  9289. 'begin',
  9290. ' doit(i,i,i);']);
  9291. ConvertProgram;
  9292. CheckSource('TestArray_StaticAsParams',
  9293. LinesToStr([ // statements
  9294. 'this.DoIt = function (vG,vH,vI) {',
  9295. ' var vJ = rtl.arraySetLength(null, 0, 2);',
  9296. ' vG = vG.slice(0);',
  9297. ' vJ = vH.slice(0);',
  9298. ' vI.set(vI.get().slice(0));',
  9299. ' $mod.DoIt(vG.slice(0), vG, {',
  9300. ' get: function () {',
  9301. ' return vG;',
  9302. ' },',
  9303. ' set: function (v) {',
  9304. ' vG = v;',
  9305. ' }',
  9306. ' });',
  9307. ' $mod.DoIt(vH.slice(0), vH, {',
  9308. ' get: function () {',
  9309. ' return vJ;',
  9310. ' },',
  9311. ' set: function (v) {',
  9312. ' vJ = v;',
  9313. ' }',
  9314. ' });',
  9315. ' $mod.DoIt(vI.get().slice(0), vI.get(), vI);',
  9316. ' $mod.DoIt(vJ.slice(0), vJ, {',
  9317. ' get: function () {',
  9318. ' return vJ;',
  9319. ' },',
  9320. ' set: function (v) {',
  9321. ' vJ = v;',
  9322. ' }',
  9323. ' });',
  9324. '};',
  9325. 'this.i = rtl.arraySetLength(null, 0, 2);'
  9326. ]),
  9327. LinesToStr([
  9328. '$mod.DoIt($mod.i.slice(0),$mod.i,{',
  9329. ' p: $mod,',
  9330. ' get: function () {',
  9331. ' return this.p.i;',
  9332. ' },',
  9333. ' set: function (v) {',
  9334. ' this.p.i = v;',
  9335. ' }',
  9336. '});'
  9337. ]));
  9338. end;
  9339. procedure TTestModule.TestArrayElement_AsParams;
  9340. begin
  9341. StartProgram(false);
  9342. Add('type integer = longint;');
  9343. Add('type TArrayInt = array of integer;');
  9344. Add('procedure DoIt(vG: Integer; const vH: Integer; var vI: Integer);');
  9345. Add('var vJ: tarrayint;');
  9346. Add('begin');
  9347. Add(' vi:=vi;');
  9348. Add(' doit(vi,vi,vi);');
  9349. Add(' doit(vj[1+1],vj[1+2],vj[1+3]);');
  9350. Add('end;');
  9351. Add('var a: TArrayInt;');
  9352. Add('begin');
  9353. Add(' doit(a[1+4],a[1+5],a[1+6]);');
  9354. ConvertProgram;
  9355. CheckSource('TestArrayElement_AsParams',
  9356. LinesToStr([ // statements
  9357. 'this.DoIt = function (vG,vH,vI) {',
  9358. ' var vJ = [];',
  9359. ' vI.set(vI.get());',
  9360. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  9361. ' $mod.DoIt(vJ[1+1], vJ[1+2], {',
  9362. ' a:1+3,',
  9363. ' p:vJ,',
  9364. ' get: function () {',
  9365. ' return this.p[this.a];',
  9366. ' },',
  9367. ' set: function (v) {',
  9368. ' this.p[this.a] = v;',
  9369. ' }',
  9370. ' });',
  9371. '};',
  9372. 'this.a = [];'
  9373. ]),
  9374. LinesToStr([
  9375. '$mod.DoIt($mod.a[1+4],$mod.a[1+5],{',
  9376. ' a: 1+6,',
  9377. ' p: $mod.a,',
  9378. ' get: function () {',
  9379. ' return this.p[this.a];',
  9380. ' },',
  9381. ' set: function (v) {',
  9382. ' this.p[this.a] = v;',
  9383. ' }',
  9384. '});'
  9385. ]));
  9386. end;
  9387. procedure TTestModule.TestArrayElementFromFuncResult_AsParams;
  9388. begin
  9389. StartProgram(false);
  9390. Add('type Integer = longint;');
  9391. Add('type TArrayInt = array of integer;');
  9392. Add('function GetArr(vB: integer = 0): tarrayint;');
  9393. Add('begin');
  9394. Add('end;');
  9395. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  9396. Add('begin');
  9397. Add('end;');
  9398. Add('begin');
  9399. Add(' doit(getarr[1+1],getarr[1+2],getarr[1+3]);');
  9400. Add(' doit(getarr()[2+1],getarr()[2+2],getarr()[2+3]);');
  9401. Add(' doit(getarr(7)[3+1],getarr(8)[3+2],getarr(9)[3+3]);');
  9402. ConvertProgram;
  9403. CheckSource('TestArrayElementFromFuncResult_AsParams',
  9404. LinesToStr([ // statements
  9405. 'this.GetArr = function (vB) {',
  9406. ' var Result = [];',
  9407. ' return Result;',
  9408. '};',
  9409. 'this.DoIt = function (vG,vH,vI) {',
  9410. '};'
  9411. ]),
  9412. LinesToStr([
  9413. '$mod.DoIt($mod.GetArr(0)[1+1],$mod.GetArr(0)[1+2],{',
  9414. ' a: 1+3,',
  9415. ' p: $mod.GetArr(0),',
  9416. ' get: function () {',
  9417. ' return this.p[this.a];',
  9418. ' },',
  9419. ' set: function (v) {',
  9420. ' this.p[this.a] = v;',
  9421. ' }',
  9422. '});',
  9423. '$mod.DoIt($mod.GetArr(0)[2+1],$mod.GetArr(0)[2+2],{',
  9424. ' a: 2+3,',
  9425. ' p: $mod.GetArr(0),',
  9426. ' get: function () {',
  9427. ' return this.p[this.a];',
  9428. ' },',
  9429. ' set: function (v) {',
  9430. ' this.p[this.a] = v;',
  9431. ' }',
  9432. '});',
  9433. '$mod.DoIt($mod.GetArr(7)[3+1],$mod.GetArr(8)[3+2],{',
  9434. ' a: 3+3,',
  9435. ' p: $mod.GetArr(9),',
  9436. ' get: function () {',
  9437. ' return this.p[this.a];',
  9438. ' },',
  9439. ' set: function (v) {',
  9440. ' this.p[this.a] = v;',
  9441. ' }',
  9442. '});',
  9443. '']));
  9444. end;
  9445. procedure TTestModule.TestArrayEnumTypeRange;
  9446. begin
  9447. StartProgram(false);
  9448. Add([
  9449. 'type',
  9450. ' TEnum = (red,blue);',
  9451. ' TEnumArray = array[TEnum] of longint;',
  9452. 'var',
  9453. ' e: TEnum;',
  9454. ' i: longint;',
  9455. ' a: TEnumArray;',
  9456. ' numbers: TEnumArray = (1,2);',
  9457. ' names: array[TEnum] of string = (''red'',''blue'');',
  9458. 'begin',
  9459. ' e:=low(a);',
  9460. ' e:=high(a);',
  9461. ' i:=a[red];',
  9462. ' a[e]:=a[e];']);
  9463. ConvertProgram;
  9464. CheckSource('TestArrayEnumTypeRange',
  9465. LinesToStr([ // statements
  9466. ' this.TEnum = {',
  9467. ' "0": "red",',
  9468. ' red: 0,',
  9469. ' "1": "blue",',
  9470. ' blue: 1',
  9471. '};',
  9472. 'this.e = 0;',
  9473. 'this.i = 0;',
  9474. 'this.a = rtl.arraySetLength(null,0,2);',
  9475. 'this.numbers = [1, 2];',
  9476. 'this.names = ["red", "blue"];',
  9477. '']),
  9478. LinesToStr([ // $mod.$main
  9479. '$mod.e = $mod.TEnum.red;',
  9480. '$mod.e = $mod.TEnum.blue;',
  9481. '$mod.i = $mod.a[$mod.TEnum.red];',
  9482. '$mod.a[$mod.e] = $mod.a[$mod.e];',
  9483. '']));
  9484. end;
  9485. procedure TTestModule.TestArray_SetLengthOutArg;
  9486. begin
  9487. StartProgram(false);
  9488. Add([
  9489. 'type TArrInt = array of longint;',
  9490. 'procedure DoIt(out a: TArrInt);',
  9491. 'begin',
  9492. ' SetLength(a,2);',
  9493. 'end;',
  9494. 'begin',
  9495. '']);
  9496. ConvertProgram;
  9497. CheckSource('TestArray_SetLengthOutArg',
  9498. LinesToStr([ // statements
  9499. 'this.DoIt = function (a) {',
  9500. ' a.set(rtl.arraySetLength(a.get(), 0, 2));',
  9501. '};',
  9502. '']),
  9503. LinesToStr([
  9504. '']));
  9505. end;
  9506. procedure TTestModule.TestArray_SetLengthProperty;
  9507. begin
  9508. StartProgram(false);
  9509. Add('type');
  9510. Add(' TArrInt = array of longint;');
  9511. Add(' TObject = class');
  9512. Add(' function GetColors: TArrInt; external name ''GetColors'';');
  9513. Add(' procedure SetColors(const Value: TArrInt); external name ''SetColors'';');
  9514. Add(' property Colors: TArrInt read GetColors write SetColors;');
  9515. Add(' end;');
  9516. Add('var Obj: TObject;');
  9517. Add('begin');
  9518. Add(' SetLength(Obj.Colors,2);');
  9519. ConvertProgram;
  9520. CheckSource('TestArray_SetLengthProperty',
  9521. LinesToStr([ // statements
  9522. 'rtl.createClass(this, "TObject", null, function () {',
  9523. ' this.$init = function () {',
  9524. ' };',
  9525. ' this.$final = function () {',
  9526. ' };',
  9527. '});',
  9528. 'this.Obj = null;',
  9529. '']),
  9530. LinesToStr([
  9531. '$mod.Obj.SetColors(rtl.arraySetLength($mod.Obj.GetColors(), 0, 2));',
  9532. '']));
  9533. end;
  9534. procedure TTestModule.TestArray_SetLengthMultiDim;
  9535. begin
  9536. StartProgram(false);
  9537. Add([
  9538. 'type',
  9539. ' TArrArrInt = array of array of longint;',
  9540. ' TArrStaInt = array of array[1..2] of longint;',
  9541. 'var',
  9542. ' a: TArrArrInt;',
  9543. ' b: TArrStaInt;',
  9544. 'begin',
  9545. ' SetLength(a,2);',
  9546. ' SetLength(a,3,4);',
  9547. ' SetLength(b,5);',
  9548. '']);
  9549. ConvertProgram;
  9550. CheckSource('TestArray_SetLengthMultiDim',
  9551. LinesToStr([ // statements
  9552. 'this.a = [];',
  9553. 'this.b = [];',
  9554. '']),
  9555. LinesToStr([
  9556. '$mod.a = rtl.arraySetLength($mod.a, [], 2);',
  9557. '$mod.a = rtl.arraySetLength($mod.a, 0, 3, 4);',
  9558. '$mod.b = rtl.arraySetLength($mod.b, 0, 5, "s", 2);',
  9559. '']));
  9560. end;
  9561. procedure TTestModule.TestArray_SetLengthDynOfStatic;
  9562. begin
  9563. StartProgram(false);
  9564. Add([
  9565. 'type',
  9566. ' TStaArr1 = array[1..3] of boolean;',
  9567. //' TStaArr2 = array[5..6] of TStaArr1;',
  9568. ' TDynArr1StaArr1 = array of TStaArr1;',
  9569. //' TDynArr1StaArr2 = array of TStaArr2;',
  9570. ' TDynArr2StaArr1 = array of TDynArr1StaArr1;',
  9571. //' TDynArr2StaArr2 = array of TDynArr1StaArr2;',
  9572. 'var',
  9573. ' DynArr1StaArr1: TDynArr1StaArr1;',
  9574. //' DynArr1StaArr2: TDynArr1StaArr1;',
  9575. ' DynArr2StaArr1: TDynArr2StaArr1;',
  9576. //' DynArr2StaArr2: TDynArr2StaArr2;',
  9577. 'begin',
  9578. ' SetLength(DynArr1StaArr1,11);',
  9579. ' SetLength(DynArr2StaArr1,12);',
  9580. ' SetLength(DynArr2StaArr1[13],14);',
  9581. ' SetLength(DynArr2StaArr1,15,16);',
  9582. //' SetLength(DynArr1StaArr2,21);',
  9583. //' SetLength(DynArr2StaArr2,22);',
  9584. //' SetLength(DynArr2StaArr2[23],24);',
  9585. //' SetLength(DynArr2StaArr2,25,26);',
  9586. '']);
  9587. ConvertProgram;
  9588. CheckSource('TestArray_DynOfStatic',
  9589. LinesToStr([ // statements
  9590. 'this.DynArr1StaArr1 = [];',
  9591. 'this.DynArr2StaArr1 = [];',
  9592. '']),
  9593. LinesToStr([ // $mod.$main
  9594. '$mod.DynArr1StaArr1 = rtl.arraySetLength($mod.DynArr1StaArr1, false, 11, "s", 3);',
  9595. '$mod.DynArr2StaArr1 = rtl.arraySetLength($mod.DynArr2StaArr1, [], 12);',
  9596. '$mod.DynArr2StaArr1[13] = rtl.arraySetLength($mod.DynArr2StaArr1[13], false, 14, "s", 3);',
  9597. '$mod.DynArr2StaArr1 = rtl.arraySetLength(',
  9598. ' $mod.DynArr2StaArr1,',
  9599. ' false,',
  9600. ' 15,',
  9601. ' 16,',
  9602. ' "s",',
  9603. ' 3',
  9604. ');',
  9605. '']));
  9606. end;
  9607. procedure TTestModule.TestArray_OpenArrayOfString;
  9608. begin
  9609. StartProgram(false);
  9610. Add('procedure DoIt(const a: array of String);');
  9611. Add('var');
  9612. Add(' i: longint;');
  9613. Add(' s: string;');
  9614. Add('begin');
  9615. Add(' for i:=low(a) to high(a) do s:=a[length(a)-i-1];');
  9616. Add('end;');
  9617. Add('var s: string;');
  9618. Add('begin');
  9619. Add(' DoIt([]);');
  9620. Add(' DoIt([s,''foo'','''',s+s]);');
  9621. ConvertProgram;
  9622. CheckSource('TestArray_OpenArrayOfString',
  9623. LinesToStr([ // statements
  9624. 'this.DoIt = function (a) {',
  9625. ' var i = 0;',
  9626. ' var s = "";',
  9627. ' for (var $l = 0, $end = rtl.length(a) - 1; $l <= $end; $l++) {',
  9628. ' i = $l;',
  9629. ' s = a[rtl.length(a) - i - 1];',
  9630. ' };',
  9631. '};',
  9632. 'this.s = "";',
  9633. '']),
  9634. LinesToStr([
  9635. '$mod.DoIt([]);',
  9636. '$mod.DoIt([$mod.s, "foo", "", $mod.s + $mod.s]);',
  9637. '']));
  9638. end;
  9639. procedure TTestModule.TestArray_ArrayOfCharAssignString;
  9640. begin
  9641. StartProgram(false);
  9642. Add([
  9643. 'type TArr = array of char;',
  9644. 'var',
  9645. ' c: char;',
  9646. ' s: string;',
  9647. ' a: TArr;',
  9648. 'procedure Run(const a: array of char);',
  9649. 'begin',
  9650. ' Run(c);',
  9651. ' Run(s);',
  9652. 'end;',
  9653. 'begin',
  9654. ' a:=c;',
  9655. ' a:=s;',
  9656. ' a:=#13;',
  9657. ' a:=''Foo'';',
  9658. ' Run(c);',
  9659. ' Run(s);',
  9660. '']);
  9661. ConvertProgram;
  9662. CheckSource('TestArray_ArrayOfCharAssignString',
  9663. LinesToStr([ // statements
  9664. 'this.c = "";',
  9665. 'this.s = "";',
  9666. 'this.a = [];',
  9667. 'this.Run = function (a) {',
  9668. ' $mod.Run($mod.c.split(""));',
  9669. ' $mod.Run($mod.s.split(""));',
  9670. '};',
  9671. '']),
  9672. LinesToStr([
  9673. '$mod.a = $mod.c.split("");',
  9674. '$mod.a = $mod.s.split("");',
  9675. '$mod.a = "\r".split("");',
  9676. '$mod.a = "Foo".split("");',
  9677. '$mod.Run($mod.c.split(""));',
  9678. '$mod.Run($mod.s.split(""));',
  9679. '']));
  9680. end;
  9681. procedure TTestModule.TestArray_ConstRef;
  9682. begin
  9683. StartProgram(false);
  9684. Add([
  9685. 'type TArr = array of word;',
  9686. 'procedure Run(constref a: TArr);',
  9687. 'begin',
  9688. 'end;',
  9689. 'procedure Fly(a: TArr; var b: TArr; out c: TArr; const d: TArr; constref e: TArr);',
  9690. 'var l: TArr;',
  9691. 'begin',
  9692. ' Run(l);',
  9693. ' Run(a);',
  9694. ' Run(b);',
  9695. ' Run(c);',
  9696. ' Run(d);',
  9697. ' Run(e);',
  9698. 'end;',
  9699. 'begin',
  9700. '']);
  9701. ConvertProgram;
  9702. CheckResolverUnexpectedHints();
  9703. CheckSource('TestArray_ConstRef',
  9704. LinesToStr([ // statements
  9705. 'this.Run = function (a) {',
  9706. '};',
  9707. 'this.Fly = function (a, b, c, d, e) {',
  9708. ' var l = [];',
  9709. ' $mod.Run(l);',
  9710. ' $mod.Run(a);',
  9711. ' $mod.Run(b.get());',
  9712. ' $mod.Run(c.get());',
  9713. ' $mod.Run(d);',
  9714. ' $mod.Run(e);',
  9715. '};',
  9716. '']),
  9717. LinesToStr([
  9718. '']));
  9719. end;
  9720. procedure TTestModule.TestArray_Concat;
  9721. begin
  9722. StartProgram(false);
  9723. Add([
  9724. 'type',
  9725. ' integer = longint;',
  9726. ' TFlag = (big,small);',
  9727. ' TFlags = set of TFlag;',
  9728. ' TRec = record',
  9729. ' i: integer;',
  9730. ' end;',
  9731. ' TArrInt = array of integer;',
  9732. ' TArrRec = array of TRec;',
  9733. ' TArrFlag = array of TFlag;',
  9734. ' TArrSet = array of TFlags;',
  9735. ' TArrJSValue = array of jsvalue;',
  9736. 'var',
  9737. ' ArrInt: tarrint;',
  9738. ' ArrRec: tarrrec;',
  9739. ' ArrFlag: tarrflag;',
  9740. ' ArrSet: tarrset;',
  9741. ' ArrJSValue: tarrjsvalue;',
  9742. 'begin',
  9743. ' arrint:=concat(arrint);',
  9744. ' arrint:=concat(arrint,arrint);',
  9745. ' arrint:=concat(arrint,arrint,arrint);',
  9746. ' arrrec:=concat(arrrec);',
  9747. ' arrrec:=concat(arrrec,arrrec);',
  9748. ' arrrec:=concat(arrrec,arrrec,arrrec);',
  9749. ' arrset:=concat(arrset);',
  9750. ' arrset:=concat(arrset,arrset);',
  9751. ' arrset:=concat(arrset,arrset,arrset);',
  9752. ' arrjsvalue:=concat(arrjsvalue);',
  9753. ' arrjsvalue:=concat(arrjsvalue,arrjsvalue);',
  9754. ' arrjsvalue:=concat(arrjsvalue,arrjsvalue,arrjsvalue);',
  9755. ' arrint:=concat([1],arrint);',
  9756. ' arrflag:=concat([big]);',
  9757. ' arrflag:=concat([big],arrflag);',
  9758. ' arrflag:=concat(arrflag,[small]);',
  9759. '']);
  9760. ConvertProgram;
  9761. CheckSource('TestArray_Concat',
  9762. LinesToStr([ // statements
  9763. 'this.TFlag = {',
  9764. ' "0": "big",',
  9765. ' big: 0,',
  9766. ' "1": "small",',
  9767. ' small: 1',
  9768. '};',
  9769. 'rtl.recNewT(this, "TRec", function () {',
  9770. ' this.i = 0;',
  9771. ' this.$eq = function (b) {',
  9772. ' return this.i === b.i;',
  9773. ' };',
  9774. ' this.$assign = function (s) {',
  9775. ' this.i = s.i;',
  9776. ' return this;',
  9777. ' };',
  9778. '});',
  9779. 'this.ArrInt = [];',
  9780. 'this.ArrRec = [];',
  9781. 'this.ArrFlag = [];',
  9782. 'this.ArrSet = [];',
  9783. 'this.ArrJSValue = [];',
  9784. '']),
  9785. LinesToStr([ // $mod.$main
  9786. '$mod.ArrInt = rtl.arrayRef($mod.ArrInt);',
  9787. '$mod.ArrInt = rtl.arrayConcatN($mod.ArrInt, $mod.ArrInt);',
  9788. '$mod.ArrInt = rtl.arrayConcatN($mod.ArrInt, $mod.ArrInt, $mod.ArrInt);',
  9789. '$mod.ArrRec = rtl.arrayRef($mod.ArrRec);',
  9790. '$mod.ArrRec = rtl.arrayConcat($mod.TRec, $mod.ArrRec, $mod.ArrRec);',
  9791. '$mod.ArrRec = rtl.arrayConcat($mod.TRec, $mod.ArrRec, $mod.ArrRec, $mod.ArrRec);',
  9792. '$mod.ArrSet = rtl.arrayRef($mod.ArrSet);',
  9793. '$mod.ArrSet = rtl.arrayConcat("refSet", $mod.ArrSet, $mod.ArrSet);',
  9794. '$mod.ArrSet = rtl.arrayConcat("refSet", $mod.ArrSet, $mod.ArrSet, $mod.ArrSet);',
  9795. '$mod.ArrJSValue = rtl.arrayRef($mod.ArrJSValue);',
  9796. '$mod.ArrJSValue = rtl.arrayConcatN($mod.ArrJSValue, $mod.ArrJSValue);',
  9797. '$mod.ArrJSValue = rtl.arrayConcatN($mod.ArrJSValue, $mod.ArrJSValue, $mod.ArrJSValue);',
  9798. '$mod.ArrInt = rtl.arrayConcatN([1], $mod.ArrInt);',
  9799. '$mod.ArrFlag = [$mod.TFlag.big];',
  9800. '$mod.ArrFlag = rtl.arrayConcatN([$mod.TFlag.big], $mod.ArrFlag);',
  9801. '$mod.ArrFlag = rtl.arrayConcatN($mod.ArrFlag, [$mod.TFlag.small]);',
  9802. '']));
  9803. end;
  9804. procedure TTestModule.TestArray_Copy;
  9805. begin
  9806. StartProgram(false);
  9807. Add([
  9808. 'type',
  9809. ' integer = longint;',
  9810. ' TFlag = (big,small);',
  9811. ' TFlags = set of TFlag;',
  9812. ' TRec = record',
  9813. ' i: integer;',
  9814. ' end;',
  9815. ' TArrInt = array of integer;',
  9816. ' TArrRec = array of TRec;',
  9817. ' TArrSet = array of TFlags;',
  9818. ' TArrJSValue = array of jsvalue;',
  9819. 'var',
  9820. ' ArrInt: tarrint;',
  9821. ' ArrRec: tarrrec;',
  9822. ' ArrSet: tarrset;',
  9823. ' ArrJSValue: tarrjsvalue;',
  9824. 'begin',
  9825. ' arrint:=copy(arrint);',
  9826. ' arrint:=copy(arrint,2);',
  9827. ' arrint:=copy(arrint,3,4);',
  9828. ' arrint:=copy([1,1],1,2);',
  9829. ' arrrec:=copy(arrrec);',
  9830. ' arrrec:=copy(arrrec,5);',
  9831. ' arrrec:=copy(arrrec,6,7);',
  9832. ' arrset:=copy(arrset);',
  9833. ' arrset:=copy(arrset,8);',
  9834. ' arrset:=copy(arrset,9,10);',
  9835. ' arrjsvalue:=copy(arrjsvalue);',
  9836. ' arrjsvalue:=copy(arrjsvalue,11);',
  9837. ' arrjsvalue:=copy(arrjsvalue,12,13);',
  9838. ' ']);
  9839. ConvertProgram;
  9840. CheckSource('TestArray_Copy',
  9841. LinesToStr([ // statements
  9842. 'this.TFlag = {',
  9843. ' "0": "big",',
  9844. ' big: 0,',
  9845. ' "1": "small",',
  9846. ' small: 1',
  9847. '};',
  9848. 'rtl.recNewT(this, "TRec", function () {',
  9849. ' this.i = 0;',
  9850. ' this.$eq = function (b) {',
  9851. ' return this.i === b.i;',
  9852. ' };',
  9853. ' this.$assign = function (s) {',
  9854. ' this.i = s.i;',
  9855. ' return this;',
  9856. ' };',
  9857. '});',
  9858. 'this.ArrInt = [];',
  9859. 'this.ArrRec = [];',
  9860. 'this.ArrSet = [];',
  9861. 'this.ArrJSValue = [];',
  9862. '']),
  9863. LinesToStr([ // $mod.$main
  9864. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 0);',
  9865. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 2);',
  9866. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 3, 4);',
  9867. '$mod.ArrInt = rtl.arrayCopy(0, [1, 1], 1, 2);',
  9868. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 0);',
  9869. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 5);',
  9870. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 6, 7);',
  9871. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 0);',
  9872. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 8);',
  9873. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 9, 10);',
  9874. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 0);',
  9875. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 11);',
  9876. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 12, 13);',
  9877. '']));
  9878. end;
  9879. procedure TTestModule.TestArray_InsertDelete;
  9880. begin
  9881. StartProgram(false);
  9882. Add([
  9883. 'type',
  9884. ' integer = longint;',
  9885. ' TFlag = (big,small);',
  9886. ' TFlags = set of TFlag;',
  9887. ' TRec = record',
  9888. ' i: integer;',
  9889. ' end;',
  9890. ' TArrInt = array of integer;',
  9891. ' TArrRec = array of TRec;',
  9892. ' TArrSet = array of TFlags;',
  9893. ' TArrJSValue = array of jsvalue;',
  9894. ' TArrArrInt = array of TArrInt;',
  9895. 'var',
  9896. ' ArrInt: tarrint;',
  9897. ' ArrRec: tarrrec;',
  9898. ' ArrSet: tarrset;',
  9899. ' ArrJSValue: tarrjsvalue;',
  9900. ' ArrArrInt: TArrArrInt;',
  9901. 'begin',
  9902. ' Insert(1,arrint,2);',
  9903. ' Insert(arrint[3],arrint,4);',
  9904. ' Insert(arrrec[5],arrrec,6);',
  9905. ' Insert(arrset[7],arrset,7);',
  9906. ' Insert(arrjsvalue[8],arrjsvalue,9);',
  9907. ' Insert(10,arrjsvalue,11);',
  9908. ' Insert([23],arrarrint,22);',
  9909. ' Delete(arrint,12,13);',
  9910. ' Delete(arrrec,14,15);',
  9911. ' Delete(arrset,17,18);',
  9912. ' Delete(arrjsvalue,19,10);']);
  9913. ConvertProgram;
  9914. CheckSource('TestArray_InsertDelete',
  9915. LinesToStr([ // statements
  9916. 'this.TFlag = {',
  9917. ' "0": "big",',
  9918. ' big: 0,',
  9919. ' "1": "small",',
  9920. ' small: 1',
  9921. '};',
  9922. 'rtl.recNewT(this, "TRec", function () {',
  9923. ' this.i = 0;',
  9924. ' this.$eq = function (b) {',
  9925. ' return this.i === b.i;',
  9926. ' };',
  9927. ' this.$assign = function (s) {',
  9928. ' this.i = s.i;',
  9929. ' return this;',
  9930. ' };',
  9931. '});',
  9932. 'this.ArrInt = [];',
  9933. 'this.ArrRec = [];',
  9934. 'this.ArrSet = [];',
  9935. 'this.ArrJSValue = [];',
  9936. 'this.ArrArrInt = [];',
  9937. '']),
  9938. LinesToStr([ // $mod.$main
  9939. '$mod.ArrInt.splice(2, 0, 1);',
  9940. '$mod.ArrInt.splice(4, 0, $mod.ArrInt[3]);',
  9941. '$mod.ArrRec.splice(6, 0, $mod.ArrRec[5]);',
  9942. '$mod.ArrSet.splice(7, 0, $mod.ArrSet[7]);',
  9943. '$mod.ArrJSValue.splice(9, 0, $mod.ArrJSValue[8]);',
  9944. '$mod.ArrJSValue.splice(11, 0, 10);',
  9945. '$mod.ArrArrInt.splice(22, 0, [23]);',
  9946. '$mod.ArrInt.splice(12, 13);',
  9947. '$mod.ArrRec.splice(14, 15);',
  9948. '$mod.ArrSet.splice(17, 18);',
  9949. '$mod.ArrJSValue.splice(19, 10);',
  9950. '']));
  9951. end;
  9952. procedure TTestModule.TestArray_DynArrayConstObjFPC;
  9953. begin
  9954. Parser.Options:=Parser.Options+[po_cassignments];
  9955. StartProgram(false);
  9956. Add([
  9957. '{$modeswitch arrayoperators}',
  9958. 'type',
  9959. ' integer = longint;',
  9960. ' TArrInt = array of integer;',
  9961. ' TArrStr = array of string;',
  9962. 'const',
  9963. ' Ints: TArrInt = (1,2,3);',
  9964. ' Aliases: TarrStr = (''foo'',''b'');',
  9965. ' OneInt: TArrInt = (7);',
  9966. ' OneStr: array of integer = (7);',
  9967. ' Chars: array of char = ''aoc'';',
  9968. ' Names: array of string = (''a'',''foo'');',
  9969. ' NameCount = low(Names)+high(Names)+length(Names);',
  9970. 'var i: integer;',
  9971. 'begin',
  9972. ' Ints:=[];',
  9973. ' Ints:=[1,1];',
  9974. ' Ints:=[1]+[2];',
  9975. ' Ints:=[2];',
  9976. ' Ints:=[]+ints;',
  9977. ' Ints:=Ints+[];',
  9978. ' Ints:=Ints+OneInt;',
  9979. ' Ints:=Ints+[1,1];',
  9980. ' Ints:=[i,i]+Ints;',
  9981. ' Ints:=[1]+[i]+[3];',
  9982. '']);
  9983. ConvertProgram;
  9984. CheckSource('TestArray_DynArrayConstObjFPC',
  9985. LinesToStr([ // statements
  9986. 'this.Ints = [1, 2, 3];',
  9987. 'this.Aliases = ["foo", "b"];',
  9988. 'this.OneInt = [7];',
  9989. 'this.OneStr = [7];',
  9990. 'this.Chars = ["a", "o", "c"];',
  9991. 'this.Names = ["a", "foo"];',
  9992. 'this.NameCount = 0 + (rtl.length(this.Names) - 1) + rtl.length(this.Names);',
  9993. 'this.i = 0;',
  9994. '']),
  9995. LinesToStr([ // $mod.$main
  9996. '$mod.Ints = [];',
  9997. '$mod.Ints = [1, 1];',
  9998. '$mod.Ints = rtl.arrayConcatN([1], [2]);',
  9999. '$mod.Ints = [2];',
  10000. '$mod.Ints = rtl.arrayConcatN([], $mod.Ints);',
  10001. '$mod.Ints = rtl.arrayConcatN($mod.Ints, []);',
  10002. '$mod.Ints = rtl.arrayConcatN($mod.Ints, $mod.OneInt);',
  10003. '$mod.Ints = rtl.arrayConcatN($mod.Ints, [1, 1]);',
  10004. '$mod.Ints = rtl.arrayConcatN([$mod.i, $mod.i], $mod.Ints);',
  10005. '$mod.Ints = rtl.arrayConcatN(rtl.arrayConcatN([1], [$mod.i]), [3]);',
  10006. '']));
  10007. end;
  10008. procedure TTestModule.TestArray_DynArrayConstDelphi;
  10009. begin
  10010. StartProgram(false);
  10011. // Note: const c = [1,1]; defines a set!
  10012. Add([
  10013. '{$mode delphi}',
  10014. 'type',
  10015. ' integer = longint;',
  10016. ' TArrInt = array of integer;',
  10017. ' TArrStr = array of string;',
  10018. 'const',
  10019. ' Ints: TArrInt = [1,1,2];',
  10020. ' Aliases: TarrStr = [''foo'',''b''];',
  10021. ' OneInt: TArrInt = [7];',
  10022. ' OneStr: array of integer = [7]+[8];',
  10023. ' Chars: array of char = ''aoc'';',
  10024. ' Names: array of string = [''a'',''a''];',
  10025. ' NameCount = low(Names)+high(Names)+length(Names);',
  10026. 'begin',
  10027. '']);
  10028. ConvertProgram;
  10029. CheckSource('TestArray_DynArrayConstDelphi',
  10030. LinesToStr([ // statements
  10031. 'this.Ints = [1, 1, 2];',
  10032. 'this.Aliases = ["foo", "b"];',
  10033. 'this.OneInt = [7];',
  10034. 'this.OneStr = rtl.arrayConcatN([7],[8]);',
  10035. 'this.Chars = ["a", "o", "c"];',
  10036. 'this.Names = ["a", "a"];',
  10037. 'this.NameCount = 0 + (rtl.length(this.Names) - 1) + rtl.length(this.Names);',
  10038. '']),
  10039. LinesToStr([ // $mod.$main
  10040. '']));
  10041. end;
  10042. procedure TTestModule.TestArray_ArrayLitAsParam;
  10043. begin
  10044. StartProgram(false);
  10045. Add([
  10046. '{$modeswitch arrayoperators}',
  10047. 'type',
  10048. ' integer = longint;',
  10049. ' TArrInt = array of integer;',
  10050. ' TArrSet = array of (red,green,blue);',
  10051. 'procedure DoOpenInt(const a: array of integer); forward;',
  10052. 'procedure DoInt(const a: TArrInt);',
  10053. 'begin',
  10054. ' DoInt(a+[1]);',
  10055. ' DoInt([1]+a);',
  10056. ' DoOpenInt(a);',
  10057. ' DoOpenInt(a+[1]);',
  10058. ' DoOpenInt([1]+a);',
  10059. 'end;',
  10060. 'procedure DoOpenInt(const a: array of integer);',
  10061. 'begin',
  10062. ' DoOpenInt(a+[1]);',
  10063. ' DoOpenInt([1]+a);',
  10064. ' DoInt(a);',
  10065. ' DoInt(a+[1]);',
  10066. ' DoInt([1]+a);',
  10067. 'end;',
  10068. 'procedure DoSet(const a: TArrSet);',
  10069. 'begin',
  10070. ' DoSet(a+[red]);',
  10071. ' DoSet([blue]+a);',
  10072. 'end;',
  10073. 'var',
  10074. ' i: TArrInt;',
  10075. ' s: TArrSet;',
  10076. 'begin',
  10077. ' DoInt([1]);',
  10078. ' DoInt([1]+[2]);',
  10079. ' DoInt(i+[1]);',
  10080. ' DoInt([1]+i);',
  10081. ' DoOpenInt([1]);',
  10082. ' DoOpenInt([1]+[2]);',
  10083. ' DoOpenInt(i+[1]);',
  10084. ' DoOpenInt([1]+i);',
  10085. ' DoSet([red]);',
  10086. ' DoSet([blue]+[green]);',
  10087. ' DoSet(s+[blue]);',
  10088. ' DoSet([red]+s);',
  10089. '']);
  10090. ConvertProgram;
  10091. CheckSource('TestArray_ArrayLitAsParam',
  10092. LinesToStr([ // statements
  10093. 'this.TArrSet$a = {',
  10094. ' "0": "red",',
  10095. ' red: 0,',
  10096. ' "1": "green",',
  10097. ' green: 1,',
  10098. ' "2": "blue",',
  10099. ' blue: 2',
  10100. '};',
  10101. 'this.DoInt = function (a) {',
  10102. ' $mod.DoInt(rtl.arrayConcatN(a, [1]));',
  10103. ' $mod.DoInt(rtl.arrayConcatN([1], a));',
  10104. ' $mod.DoOpenInt(a);',
  10105. ' $mod.DoOpenInt(rtl.arrayConcatN(a, [1]));',
  10106. ' $mod.DoOpenInt(rtl.arrayConcatN([1], a));',
  10107. '};',
  10108. 'this.DoOpenInt = function (a) {',
  10109. ' $mod.DoOpenInt(rtl.arrayConcatN(a, [1]));',
  10110. ' $mod.DoOpenInt(rtl.arrayConcatN([1], a));',
  10111. ' $mod.DoInt(a);',
  10112. ' $mod.DoInt(rtl.arrayConcatN(a, [1]));',
  10113. ' $mod.DoInt(rtl.arrayConcatN([1], a));',
  10114. '};',
  10115. 'this.DoSet = function (a) {',
  10116. ' $mod.DoSet(rtl.arrayConcatN(a, [$mod.TArrSet$a.red]));',
  10117. ' $mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.blue], a));',
  10118. '};',
  10119. 'this.i = [];',
  10120. 'this.s = [];',
  10121. '']),
  10122. LinesToStr([ // $mod.$main
  10123. '$mod.DoInt([1]);',
  10124. '$mod.DoInt(rtl.arrayConcatN([1], [2]));',
  10125. '$mod.DoInt(rtl.arrayConcatN($mod.i, [1]));',
  10126. '$mod.DoInt(rtl.arrayConcatN([1], $mod.i));',
  10127. '$mod.DoOpenInt([1]);',
  10128. '$mod.DoOpenInt(rtl.arrayConcatN([1], [2]));',
  10129. '$mod.DoOpenInt(rtl.arrayConcatN($mod.i, [1]));',
  10130. '$mod.DoOpenInt(rtl.arrayConcatN([1], $mod.i));',
  10131. '$mod.DoSet([$mod.TArrSet$a.red]);',
  10132. '$mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.blue], [$mod.TArrSet$a.green]));',
  10133. '$mod.DoSet(rtl.arrayConcatN($mod.s, [$mod.TArrSet$a.blue]));',
  10134. '$mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.red], $mod.s));',
  10135. '']));
  10136. end;
  10137. procedure TTestModule.TestArray_ArrayLitMultiDimAsParam;
  10138. begin
  10139. StartProgram(false);
  10140. Add([
  10141. '{$modeswitch arrayoperators}',
  10142. 'type',
  10143. ' integer = longint;',
  10144. ' TArrInt = array of integer;',
  10145. ' TArrArrInt = array of TArrInt;',
  10146. 'procedure DoInt(const a: TArrArrInt);',
  10147. 'begin',
  10148. ' DoInt(a+[[1]]);',
  10149. ' DoInt([[1]]+a);',
  10150. ' DoInt(a);',
  10151. 'end;',
  10152. 'var',
  10153. ' i: TArrInt;',
  10154. ' a: TArrArrInt;',
  10155. 'begin',
  10156. ' a:=[[1]];',
  10157. ' a:=[i];',
  10158. ' a:=a+[i];',
  10159. ' a:=[i]+a;',
  10160. ' a:=[[1]+i];',
  10161. ' a:=[[1]+[2]];',
  10162. ' a:=[i+[2]];',
  10163. ' DoInt([[1]]);',
  10164. ' DoInt([[1]+[2],[3,4],[5]]);',
  10165. ' DoInt([i+[1]]+a);',
  10166. ' DoInt([i]+a);',
  10167. '']);
  10168. ConvertProgram;
  10169. CheckSource('TestArray_ArrayLitMultiDimAsParam',
  10170. LinesToStr([ // statements
  10171. 'this.DoInt = function (a) {',
  10172. ' $mod.DoInt(rtl.arrayConcatN(a, [[1]]));',
  10173. ' $mod.DoInt(rtl.arrayConcatN([[1]], a));',
  10174. ' $mod.DoInt(a);',
  10175. '};',
  10176. 'this.i = [];',
  10177. 'this.a = [];',
  10178. '']),
  10179. LinesToStr([ // $mod.$main
  10180. '$mod.a = [[1]];',
  10181. '$mod.a = [$mod.i];',
  10182. '$mod.a = rtl.arrayConcatN($mod.a, [$mod.i]);',
  10183. '$mod.a = rtl.arrayConcatN([$mod.i], $mod.a);',
  10184. '$mod.a = [rtl.arrayConcatN([1], $mod.i)];',
  10185. '$mod.a = [rtl.arrayConcatN([1], [2])];',
  10186. '$mod.a = [rtl.arrayConcatN($mod.i, [2])];',
  10187. '$mod.DoInt([[1]]);',
  10188. '$mod.DoInt([rtl.arrayConcatN([1], [2]), [3, 4], [5]]);',
  10189. '$mod.DoInt(rtl.arrayConcatN([rtl.arrayConcatN($mod.i, [1])], $mod.a));',
  10190. '$mod.DoInt(rtl.arrayConcatN([$mod.i], $mod.a));',
  10191. '']));
  10192. end;
  10193. procedure TTestModule.TestArray_ArrayLitStaticAsParam;
  10194. begin
  10195. StartProgram(false);
  10196. Add([
  10197. '{$modeswitch arrayoperators}',
  10198. 'type',
  10199. ' integer = longint;',
  10200. ' TArrInt = array[1..2] of integer;',
  10201. ' TArrArrInt = array of TArrInt;',
  10202. 'procedure DoInt(const a: TArrArrInt);',
  10203. 'begin',
  10204. ' DoInt(a+[[1,2]]);',
  10205. ' DoInt([[1,2]]+a);',
  10206. ' DoInt(a);',
  10207. 'end;',
  10208. 'var',
  10209. ' i: TArrInt;',
  10210. ' a: TArrArrInt;',
  10211. 'begin',
  10212. ' a:=[[1,1]];',
  10213. ' a:=[i];',
  10214. ' a:=a+[i];',
  10215. ' a:=[i]+a;',
  10216. ' DoInt([[1,1]]);',
  10217. ' DoInt([[1,2],[3,4]]);',
  10218. '']);
  10219. ConvertProgram;
  10220. CheckSource('TestArray_ArrayLitStaticAsParam',
  10221. LinesToStr([ // statements
  10222. 'this.DoInt = function (a) {',
  10223. ' $mod.DoInt(rtl.arrayConcatN(a, [[1, 2]]));',
  10224. ' $mod.DoInt(rtl.arrayConcatN([[1, 2]], a));',
  10225. ' $mod.DoInt(a);',
  10226. '};',
  10227. 'this.i = rtl.arraySetLength(null, 0, 2);',
  10228. 'this.a = [];',
  10229. '']),
  10230. LinesToStr([ // $mod.$main
  10231. '$mod.a = [[1, 1]];',
  10232. '$mod.a = [$mod.i.slice(0)];',
  10233. '$mod.a = rtl.arrayConcatN($mod.a, [$mod.i.slice(0)]);',
  10234. '$mod.a = rtl.arrayConcatN([$mod.i.slice(0)], $mod.a);',
  10235. '$mod.DoInt([[1, 1]]);',
  10236. '$mod.DoInt([[1, 2], [3, 4]]);',
  10237. '']));
  10238. end;
  10239. procedure TTestModule.TestArray_ForInArrOfString;
  10240. begin
  10241. StartProgram(false);
  10242. Add([
  10243. 'type',
  10244. 'type',
  10245. ' TMonthNameArray = array [1..12] of string;',
  10246. ' TMonthNames = TMonthNameArray;',
  10247. ' TObject = class',
  10248. ' private',
  10249. ' function GetLongMonthNames: TMonthNames; virtual; abstract;',
  10250. ' public',
  10251. ' Property LongMonthNames : TMonthNames Read GetLongMonthNames;',
  10252. ' end;',
  10253. 'var',
  10254. ' f: TObject;',
  10255. ' Month: string;',
  10256. ' Names: array of string = (''a'',''foo'',''bar'');',
  10257. ' i: longint;',
  10258. 'begin',
  10259. ' for Month in f.LongMonthNames do ;',
  10260. ' for Month in Names do ;',
  10261. ' for i:=low(Names) to high(Names) do ;',
  10262. '']);
  10263. ConvertProgram;
  10264. CheckSource('TestArray_ForInArrOfString',
  10265. LinesToStr([ // statements
  10266. 'rtl.createClass(this, "TObject", null, function () {',
  10267. ' this.$init = function () {',
  10268. ' };',
  10269. ' this.$final = function () {',
  10270. ' };',
  10271. '});',
  10272. 'this.f = null;',
  10273. 'this.Month = "";',
  10274. 'this.Names = ["a", "foo", "bar"];',
  10275. 'this.i = 0;',
  10276. '']),
  10277. LinesToStr([ // $mod.$main
  10278. 'for (var $in = $mod.f.GetLongMonthNames(), $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) $mod.Month = $in[$l];',
  10279. 'for (var $in1 = $mod.Names, $l1 = 0, $end1 = rtl.length($in1) - 1; $l1 <= $end1; $l1++) $mod.Month = $in1[$l1];',
  10280. 'for (var $l2 = 0, $end2 = rtl.length($mod.Names) - 1; $l2 <= $end2; $l2++) $mod.i = $l2;',
  10281. '']));
  10282. end;
  10283. procedure TTestModule.TestExternalClass_TypeCastArrayToExternalClass;
  10284. begin
  10285. StartProgram(false);
  10286. Add([
  10287. '{$modeswitch externalclass}',
  10288. 'type',
  10289. ' TJSObject = class external name ''Object''',
  10290. ' end;',
  10291. ' TJSArray = class external name ''Array''',
  10292. ' class function isArray(Value: JSValue) : boolean;',
  10293. ' function concat() : TJSArray; varargs;',
  10294. ' end;',
  10295. 'var',
  10296. ' aObj: TJSArray;',
  10297. ' a: array of longint;',
  10298. ' o: TJSObject;',
  10299. 'begin',
  10300. ' if TJSArray.isArray(65) then ;',
  10301. ' aObj:=TJSArray(a).concat(a);',
  10302. ' o:=TJSObject(a);']);
  10303. ConvertProgram;
  10304. CheckSource('TestExternalClass_TypeCastArrayToExternalClass',
  10305. LinesToStr([ // statements
  10306. 'this.aObj = null;',
  10307. 'this.a = [];',
  10308. 'this.o = null;',
  10309. '']),
  10310. LinesToStr([ // $mod.$main
  10311. 'if (Array.isArray(65)) ;',
  10312. '$mod.aObj = $mod.a.concat($mod.a);',
  10313. '$mod.o = $mod.a;',
  10314. '']));
  10315. end;
  10316. procedure TTestModule.TestExternalClass_TypeCastArrayFromExternalClass;
  10317. begin
  10318. StartProgram(false);
  10319. Add([
  10320. '{$modeswitch externalclass}',
  10321. 'type',
  10322. ' TArrStr = array of string;',
  10323. ' TJSArray = class external name ''Array''',
  10324. ' end;',
  10325. ' TJSObject = class external name ''Object''',
  10326. ' end;',
  10327. 'var',
  10328. ' aObj: TJSArray;',
  10329. ' a: TArrStr;',
  10330. ' jo: TJSObject;',
  10331. 'begin',
  10332. ' a:=TArrStr(aObj);',
  10333. ' TArrStr(aObj)[1]:=TArrStr(aObj)[2];',
  10334. ' a:=TarrStr(jo);',
  10335. '']);
  10336. ConvertProgram;
  10337. CheckSource('TestExternalClass_TypeCastArrayFromExternalClass',
  10338. LinesToStr([ // statements
  10339. 'this.aObj = null;',
  10340. 'this.a = [];',
  10341. 'this.jo = null;',
  10342. '']),
  10343. LinesToStr([ // $mod.$main
  10344. '$mod.a = $mod.aObj;',
  10345. '$mod.aObj[1] = $mod.aObj[2];',
  10346. '$mod.a = $mod.jo;',
  10347. '']));
  10348. end;
  10349. procedure TTestModule.TestArrayOfConst_TVarRec;
  10350. begin
  10351. StartProgram(true,[supTVarRec]);
  10352. Add([
  10353. 'procedure Say(args: array of const);',
  10354. 'var',
  10355. ' i: longint;',
  10356. ' v: TVarRec;',
  10357. 'begin',
  10358. ' for i:=low(args) to high(args) do begin',
  10359. ' v:=args[i];',
  10360. ' case v.vtype of',
  10361. ' vtInteger: if length(args)=args[i].vInteger then ;',
  10362. ' end;',
  10363. ' end;',
  10364. ' for v in args do ;',
  10365. ' args:=nil;',
  10366. ' SetLength(args,2);',
  10367. 'end;',
  10368. 'begin']);
  10369. ConvertProgram;
  10370. CheckSource('TestArrayOfConst_TVarRec',
  10371. LinesToStr([ // statements
  10372. 'this.Say = function (args) {',
  10373. ' var i = 0;',
  10374. ' var v = pas.system.TVarRec.$new();',
  10375. ' for (var $l = 0, $end = rtl.length(args) - 1; $l <= $end; $l++) {',
  10376. ' i = $l;',
  10377. ' v.$assign(args[i]);',
  10378. ' var $tmp = v.VType;',
  10379. ' if ($tmp === 0) if (rtl.length(args) === args[i].VJSValue) ;',
  10380. ' };',
  10381. ' for (var $in = args, $l1 = 0, $end1 = rtl.length($in) - 1; $l1 <= $end1; $l1++) v = $in[$l1];',
  10382. ' args = [];',
  10383. ' args = rtl.arraySetLength(args, pas.system.TVarRec, 2);',
  10384. '};',
  10385. '']),
  10386. LinesToStr([ // $mod.$main
  10387. ]));
  10388. end;
  10389. procedure TTestModule.TestArrayOfConst_PassBaseTypes;
  10390. begin
  10391. StartProgram(true,[supTVarRec]);
  10392. Add([
  10393. 'procedure Say(args: array of const);',
  10394. 'begin',
  10395. ' Say(args);',
  10396. 'end;',
  10397. 'var',
  10398. ' p: Pointer;',
  10399. ' j: jsvalue;',
  10400. ' c: currency;',
  10401. 'begin',
  10402. ' Say([]);',
  10403. ' Say([1]);',
  10404. ' Say([''c'',''foo'',nil,true,1.3,p,j,c]);',
  10405. '']);
  10406. ConvertProgram;
  10407. CheckSource('TestArrayOfConst_PassBaseTypes',
  10408. LinesToStr([ // statements
  10409. 'this.Say = function (args) {',
  10410. ' $mod.Say(args);',
  10411. '};',
  10412. 'this.p = null;',
  10413. 'this.j = undefined;',
  10414. 'this.c = 0;',
  10415. '']),
  10416. LinesToStr([ // $mod.$main
  10417. '$mod.Say([]);',
  10418. '$mod.Say(pas.system.VarRecs(0, 1));',
  10419. '$mod.Say(pas.system.VarRecs(',
  10420. ' 9,',
  10421. ' "c",',
  10422. ' 18,',
  10423. ' "foo",',
  10424. ' 5,',
  10425. ' null,',
  10426. ' 1,',
  10427. ' true,',
  10428. ' 3,',
  10429. ' 1.3,',
  10430. ' 5,',
  10431. ' $mod.p,',
  10432. ' 20,',
  10433. ' $mod.j,',
  10434. ' 12,',
  10435. ' $mod.c',
  10436. ' ));',
  10437. '']));
  10438. end;
  10439. procedure TTestModule.TestArrayOfConst_PassObj;
  10440. begin
  10441. StartProgram(true,[supTVarRec]);
  10442. Add([
  10443. '{$interfaces corba}',
  10444. 'type',
  10445. ' TObject = class',
  10446. ' end;',
  10447. ' TClass = class of TObject;',
  10448. ' IUnknown = interface',
  10449. ' end;',
  10450. 'procedure Say(args: array of const);',
  10451. 'begin',
  10452. 'end;',
  10453. 'var',
  10454. ' o: TObject;',
  10455. ' c: TClass;',
  10456. ' i: IUnknown;',
  10457. 'begin',
  10458. ' Say([o,c,TObject]);',
  10459. ' Say([nil,i]);',
  10460. '']);
  10461. ConvertProgram;
  10462. CheckSource('TestArrayOfConst_PassObj',
  10463. LinesToStr([ // statements
  10464. 'rtl.createClass(this, "TObject", null, function () {',
  10465. ' this.$init = function () {',
  10466. ' };',
  10467. ' this.$final = function () {',
  10468. ' };',
  10469. '});',
  10470. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  10471. 'this.Say = function (args) {',
  10472. '};',
  10473. 'this.o = null;',
  10474. 'this.c = null;',
  10475. 'this.i = null;',
  10476. '']),
  10477. LinesToStr([ // $mod.$main
  10478. '$mod.Say(pas.system.VarRecs(',
  10479. ' 7,',
  10480. ' $mod.o,',
  10481. ' 8,',
  10482. ' $mod.c,',
  10483. ' 8,',
  10484. ' $mod.TObject',
  10485. '));',
  10486. '$mod.Say(pas.system.VarRecs(5, null, 14, $mod.i));',
  10487. '']));
  10488. end;
  10489. procedure TTestModule.TestRecord_Empty;
  10490. begin
  10491. StartProgram(false);
  10492. Add([
  10493. 'type',
  10494. ' TRecA = record',
  10495. ' end;',
  10496. 'var a,b: TRecA;',
  10497. 'begin',
  10498. ' if a=b then ;']);
  10499. ConvertProgram;
  10500. CheckSource('TestRecord_Empty',
  10501. LinesToStr([ // statements
  10502. 'rtl.recNewT(this, "TRecA", function () {',
  10503. ' this.$eq = function (b) {',
  10504. ' return true;',
  10505. ' };',
  10506. ' this.$assign = function (s) {',
  10507. ' return this;',
  10508. ' };',
  10509. '});',
  10510. 'this.a = this.TRecA.$new();',
  10511. 'this.b = this.TRecA.$new();',
  10512. '']),
  10513. LinesToStr([ // $mod.$main
  10514. 'if ($mod.a.$eq($mod.b)) ;'
  10515. ]));
  10516. end;
  10517. procedure TTestModule.TestRecord_Var;
  10518. begin
  10519. StartProgram(false);
  10520. Add('type');
  10521. Add(' TRecA = record');
  10522. Add(' Bold: longint;');
  10523. Add(' end;');
  10524. Add('var Rec: TRecA;');
  10525. Add('begin');
  10526. Add(' rec.bold:=123');
  10527. ConvertProgram;
  10528. CheckSource('TestRecord_Var',
  10529. LinesToStr([ // statements
  10530. 'rtl.recNewT(this, "TRecA", function () {',
  10531. ' this.Bold = 0;',
  10532. ' this.$eq = function (b) {',
  10533. ' return this.Bold === b.Bold;',
  10534. ' };',
  10535. ' this.$assign = function (s) {',
  10536. ' this.Bold = s.Bold;',
  10537. ' return this;',
  10538. ' };',
  10539. '});',
  10540. 'this.Rec = this.TRecA.$new();',
  10541. '']),
  10542. LinesToStr([ // $mod.$main
  10543. '$mod.Rec.Bold = 123;'
  10544. ]));
  10545. end;
  10546. procedure TTestModule.TestRecord_VarExternal;
  10547. begin
  10548. StartProgram(false);
  10549. Add([
  10550. '{$modeswitch externalclass}',
  10551. 'type',
  10552. ' TRecA = record',
  10553. ' i: byte;',
  10554. ' length_: longint external name ''length'';',
  10555. ' end;',
  10556. 'var Rec: TRecA;',
  10557. 'begin',
  10558. ' rec.length_ := rec.length_',
  10559. '']);
  10560. ConvertProgram;
  10561. CheckSource('TestRecord_VarExternal',
  10562. LinesToStr([ // statements
  10563. 'rtl.recNewT(this, "TRecA", function () {',
  10564. ' this.i = 0;',
  10565. ' this.$eq = function (b) {',
  10566. ' return (this.i === b.i) && (this.length === b.length);',
  10567. ' };',
  10568. ' this.$assign = function (s) {',
  10569. ' this.i = s.i;',
  10570. ' this.length = s.length;',
  10571. ' return this;',
  10572. ' };',
  10573. '});',
  10574. 'this.Rec = this.TRecA.$new();',
  10575. '']),
  10576. LinesToStr([ // $mod.$main
  10577. '$mod.Rec.length = $mod.Rec.length;'
  10578. ]));
  10579. end;
  10580. procedure TTestModule.TestRecord_WithDo;
  10581. begin
  10582. StartProgram(false);
  10583. Add('type');
  10584. Add(' TRec = record');
  10585. Add(' vI: longint;');
  10586. Add(' end;');
  10587. Add('var');
  10588. Add(' Int: longint;');
  10589. Add(' r: TRec;');
  10590. Add('begin');
  10591. Add(' with r do');
  10592. Add(' int:=vi;');
  10593. Add(' with r do begin');
  10594. Add(' int:=vi;');
  10595. Add(' vi:=int;');
  10596. Add(' end;');
  10597. ConvertProgram;
  10598. CheckSource('TestWithRecordDo',
  10599. LinesToStr([ // statements
  10600. 'rtl.recNewT(this, "TRec", function () {',
  10601. ' this.vI = 0;',
  10602. ' this.$eq = function (b) {',
  10603. ' return this.vI === b.vI;',
  10604. ' };',
  10605. ' this.$assign = function (s) {',
  10606. ' this.vI = s.vI;',
  10607. ' return this;',
  10608. ' };',
  10609. '});',
  10610. 'this.Int = 0;',
  10611. 'this.r = this.TRec.$new();',
  10612. '']),
  10613. LinesToStr([ // $mod.$main
  10614. 'var $with = $mod.r;',
  10615. '$mod.Int = $with.vI;',
  10616. 'var $with1 = $mod.r;',
  10617. '$mod.Int = $with1.vI;',
  10618. '$with1.vI = $mod.Int;'
  10619. ]));
  10620. end;
  10621. procedure TTestModule.TestRecord_Assign;
  10622. begin
  10623. StartProgram(false);
  10624. Add('type');
  10625. Add(' TEnum = (red,green);');
  10626. Add(' TEnums = set of TEnum;');
  10627. Add(' TSmallRec = record');
  10628. Add(' N: longint;');
  10629. Add(' end;');
  10630. Add(' TBigRec = record');
  10631. Add(' Int: longint;');
  10632. Add(' D: double;');
  10633. Add(' Arr: array of longint;');
  10634. Add(' Arr2: array[1..2] of longint;');
  10635. Add(' Small: TSmallRec;');
  10636. Add(' Enums: TEnums;');
  10637. Add(' end;');
  10638. Add('var');
  10639. Add(' r, s: TBigRec;');
  10640. Add('begin');
  10641. Add(' r:=s;');
  10642. Add(' r:=default(TBigRec);');
  10643. Add(' r:=default(s);');
  10644. ConvertProgram;
  10645. CheckSource('TestRecord_Assign',
  10646. LinesToStr([ // statements
  10647. 'this.TEnum = {',
  10648. ' "0": "red",',
  10649. ' red: 0,',
  10650. ' "1": "green",',
  10651. ' green: 1',
  10652. '};',
  10653. 'rtl.recNewT(this, "TSmallRec", function () {',
  10654. ' this.N = 0;',
  10655. ' this.$eq = function (b) {',
  10656. ' return this.N === b.N;',
  10657. ' };',
  10658. ' this.$assign = function (s) {',
  10659. ' this.N = s.N;',
  10660. ' return this;',
  10661. ' };',
  10662. '});',
  10663. 'rtl.recNewT(this, "TBigRec", function () {',
  10664. ' this.Int = 0;',
  10665. ' this.D = 0.0;',
  10666. ' this.$new = function () {',
  10667. ' var r = Object.create(this);',
  10668. ' r.Arr = [];',
  10669. ' r.Arr2 = rtl.arraySetLength(null, 0, 2);',
  10670. ' r.Small = $mod.TSmallRec.$new();',
  10671. ' r.Enums = {};',
  10672. ' return r;',
  10673. ' };',
  10674. ' this.$eq = function (b) {',
  10675. ' 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);',
  10676. ' };',
  10677. ' this.$assign = function (s) {',
  10678. ' this.Int = s.Int;',
  10679. ' this.D = s.D;',
  10680. ' this.Arr = rtl.arrayRef(s.Arr);',
  10681. ' this.Arr2 = s.Arr2.slice(0);',
  10682. ' this.Small.$assign(s.Small);',
  10683. ' this.Enums = rtl.refSet(s.Enums);',
  10684. ' return this;',
  10685. ' };',
  10686. '});',
  10687. 'this.r = this.TBigRec.$new();',
  10688. 'this.s = this.TBigRec.$new();',
  10689. '']),
  10690. LinesToStr([ // $mod.$main
  10691. '$mod.r.$assign($mod.s);',
  10692. '$mod.r.$assign($mod.TBigRec.$new());',
  10693. '$mod.r.$assign($mod.TBigRec.$new());',
  10694. '']));
  10695. end;
  10696. procedure TTestModule.TestRecord_AsParams;
  10697. begin
  10698. StartProgram(false);
  10699. Add([
  10700. 'type',
  10701. ' integer = longint;',
  10702. ' TRecord = record',
  10703. ' i: integer;',
  10704. ' end;',
  10705. 'procedure DoIt(vD: TRecord; const vC: TRecord; var vV: TRecord; var U);',
  10706. 'var vL: TRecord;',
  10707. 'begin',
  10708. ' vd:=vd;',
  10709. ' vd.i:=vd.i;',
  10710. ' vl:=vc;',
  10711. ' vv:=vv;',
  10712. ' vv.i:=vv.i;',
  10713. ' U:=vl;',
  10714. ' U:=vd;',
  10715. ' U:=vc;',
  10716. ' U:=vv;',
  10717. ' vl:=TRecord(U);',
  10718. ' vd:=TRecord(U);',
  10719. ' vv:=TRecord(U);',
  10720. ' doit(vd,vd,vd,vd);',
  10721. ' doit(vc,vc,vl,vl);',
  10722. ' doit(vv,vv,vv,vv);',
  10723. ' doit(vl,vl,vl,vl);',
  10724. ' TRecord(U).i:=3;',
  10725. 'end;',
  10726. 'var i: TRecord;',
  10727. 'begin',
  10728. ' doit(i,i,i,i);',
  10729. '']);
  10730. ConvertProgram;
  10731. CheckSource('TestRecord_AsParams',
  10732. LinesToStr([ // statements
  10733. 'rtl.recNewT(this, "TRecord", function () {',
  10734. ' this.i = 0;',
  10735. ' this.$eq = function (b) {',
  10736. ' return this.i === b.i;',
  10737. ' };',
  10738. ' this.$assign = function (s) {',
  10739. ' this.i = s.i;',
  10740. ' return this;',
  10741. ' };',
  10742. '});',
  10743. 'this.DoIt = function (vD, vC, vV, U) {',
  10744. ' var vL = $mod.TRecord.$new();',
  10745. ' vD.$assign(vD);',
  10746. ' vD.i = vD.i;',
  10747. ' vL.$assign(vC);',
  10748. ' vV.$assign(vV);',
  10749. ' vV.i = vV.i;',
  10750. ' U.$assign(vL);',
  10751. ' U.$assign(vD);',
  10752. ' U.$assign(vC);',
  10753. ' U.$assign(vV);',
  10754. ' vL.$assign(U);',
  10755. ' vD.$assign(U);',
  10756. ' vV.$assign(U);',
  10757. ' $mod.DoIt($mod.TRecord.$clone(vD), vD, vD, vD);',
  10758. ' $mod.DoIt($mod.TRecord.$clone(vC), vC, vL, vL);',
  10759. ' $mod.DoIt($mod.TRecord.$clone(vV), vV, vV, vV);',
  10760. ' $mod.DoIt($mod.TRecord.$clone(vL), vL, vL, vL);',
  10761. ' U.i = 3;',
  10762. '};',
  10763. 'this.i = this.TRecord.$new();'
  10764. ]),
  10765. LinesToStr([
  10766. '$mod.DoIt($mod.TRecord.$clone($mod.i), $mod.i, $mod.i, $mod.i);',
  10767. '']));
  10768. end;
  10769. procedure TTestModule.TestRecord_ConstRef;
  10770. begin
  10771. StartProgram(false);
  10772. Add([
  10773. 'type TRec = record i: word; end;',
  10774. 'procedure Run(constref a: TRec);',
  10775. 'begin',
  10776. 'end;',
  10777. 'procedure Fly(a: TRec; var b: TRec; out c: TRec; const d: TRec; constref e: TRec);',
  10778. 'var l: TRec;',
  10779. 'begin',
  10780. ' Run(l);',
  10781. ' Run(a);',
  10782. ' Run(b);',
  10783. ' Run(c);',
  10784. ' Run(d);',
  10785. ' Run(e);',
  10786. 'end;',
  10787. 'begin',
  10788. '']);
  10789. ConvertProgram;
  10790. CheckResolverUnexpectedHints();
  10791. CheckSource('TestRecord_ConstRef',
  10792. LinesToStr([ // statements
  10793. 'rtl.recNewT(this, "TRec", function () {',
  10794. ' this.i = 0;',
  10795. ' this.$eq = function (b) {',
  10796. ' return this.i === b.i;',
  10797. ' };',
  10798. ' this.$assign = function (s) {',
  10799. ' this.i = s.i;',
  10800. ' return this;',
  10801. ' };',
  10802. '});',
  10803. 'this.Run = function (a) {',
  10804. '};',
  10805. 'this.Fly = function (a, b, c, d, e) {',
  10806. ' var l = $mod.TRec.$new();',
  10807. ' $mod.Run(l);',
  10808. ' $mod.Run(a);',
  10809. ' $mod.Run(b);',
  10810. ' $mod.Run(c);',
  10811. ' $mod.Run(d);',
  10812. ' $mod.Run(e);',
  10813. '};',
  10814. '']),
  10815. LinesToStr([
  10816. '']));
  10817. end;
  10818. procedure TTestModule.TestRecordElement_AsParams;
  10819. begin
  10820. StartProgram(false);
  10821. Add('type');
  10822. Add(' integer = longint;');
  10823. Add(' TRecord = record');
  10824. Add(' i: integer;');
  10825. Add(' end;');
  10826. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  10827. Add('var vJ: TRecord;');
  10828. Add('begin');
  10829. Add(' doit(vj.i,vj.i,vj.i);');
  10830. Add('end;');
  10831. Add('var r: TRecord;');
  10832. Add('begin');
  10833. Add(' doit(r.i,r.i,r.i);');
  10834. ConvertProgram;
  10835. CheckSource('TestRecordElement_AsParams',
  10836. LinesToStr([ // statements
  10837. 'rtl.recNewT(this, "TRecord", function () {',
  10838. ' this.i = 0;',
  10839. ' this.$eq = function (b) {',
  10840. ' return this.i === b.i;',
  10841. ' };',
  10842. ' this.$assign = function (s) {',
  10843. ' this.i = s.i;',
  10844. ' return this;',
  10845. ' };',
  10846. '});',
  10847. 'this.DoIt = function (vG,vH,vI) {',
  10848. ' var vJ = $mod.TRecord.$new();',
  10849. ' $mod.DoIt(vJ.i, vJ.i, {',
  10850. ' p: vJ,',
  10851. ' get: function () {',
  10852. ' return this.p.i;',
  10853. ' },',
  10854. ' set: function (v) {',
  10855. ' this.p.i = v;',
  10856. ' }',
  10857. ' });',
  10858. '};',
  10859. 'this.r = this.TRecord.$new();'
  10860. ]),
  10861. LinesToStr([
  10862. '$mod.DoIt($mod.r.i,$mod.r.i,{',
  10863. ' p: $mod.r,',
  10864. ' get: function () {',
  10865. ' return this.p.i;',
  10866. ' },',
  10867. ' set: function (v) {',
  10868. ' this.p.i = v;',
  10869. ' }',
  10870. '});'
  10871. ]));
  10872. end;
  10873. procedure TTestModule.TestRecordElementFromFuncResult_AsParams;
  10874. begin
  10875. StartProgram(false);
  10876. Add('type');
  10877. Add(' integer = longint;');
  10878. Add(' TRecord = record');
  10879. Add(' i: integer;');
  10880. Add(' end;');
  10881. Add('function GetRec(vB: integer = 0): TRecord;');
  10882. Add('begin');
  10883. Add('end;');
  10884. Add('procedure DoIt(vG: integer; const vH: integer);');
  10885. Add('begin');
  10886. Add('end;');
  10887. Add('begin');
  10888. Add(' doit(getrec.i,getrec.i);');
  10889. Add(' doit(getrec().i,getrec().i);');
  10890. Add(' doit(getrec(1).i,getrec(2).i);');
  10891. ConvertProgram;
  10892. CheckSource('TestRecordElementFromFuncResult_AsParams',
  10893. LinesToStr([ // statements
  10894. 'rtl.recNewT(this, "TRecord", function () {',
  10895. ' this.i = 0;',
  10896. ' this.$eq = function (b) {',
  10897. ' return this.i === b.i;',
  10898. ' };',
  10899. ' this.$assign = function (s) {',
  10900. ' this.i = s.i;',
  10901. ' return this;',
  10902. ' };',
  10903. '});',
  10904. 'this.GetRec = function (vB) {',
  10905. ' var Result = $mod.TRecord.$new();',
  10906. ' return Result;',
  10907. '};',
  10908. 'this.DoIt = function (vG, vH) {',
  10909. '};',
  10910. '']),
  10911. LinesToStr([
  10912. '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
  10913. '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
  10914. '$mod.DoIt($mod.GetRec(1).i,$mod.GetRec(2).i);',
  10915. '']));
  10916. end;
  10917. procedure TTestModule.TestRecordElementFromWith_AsParams;
  10918. begin
  10919. StartProgram(false);
  10920. Add('type');
  10921. Add(' integer = longint;');
  10922. Add(' TRecord = record');
  10923. Add(' i: integer;');
  10924. Add(' end;');
  10925. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  10926. Add('begin');
  10927. Add('end;');
  10928. Add('var r: trecord;');
  10929. Add('begin');
  10930. Add(' with r do ');
  10931. Add(' doit(i,i,i);');
  10932. ConvertProgram;
  10933. CheckSource('TestRecordElementFromWith_AsParams',
  10934. LinesToStr([ // statements
  10935. 'rtl.recNewT(this, "TRecord", function () {',
  10936. ' this.i = 0;',
  10937. ' this.$eq = function (b) {',
  10938. ' return this.i === b.i;',
  10939. ' };',
  10940. ' this.$assign = function (s) {',
  10941. ' this.i = s.i;',
  10942. ' return this;',
  10943. ' };',
  10944. '});',
  10945. 'this.DoIt = function (vG,vH,vI) {',
  10946. '};',
  10947. 'this.r = this.TRecord.$new();'
  10948. ]),
  10949. LinesToStr([
  10950. 'var $with = $mod.r;',
  10951. '$mod.DoIt($with.i,$with.i,{',
  10952. ' p: $with,',
  10953. ' get: function () {',
  10954. ' return this.p.i;',
  10955. ' },',
  10956. ' set: function (v) {',
  10957. ' this.p.i = v;',
  10958. ' }',
  10959. '});',
  10960. '']));
  10961. end;
  10962. procedure TTestModule.TestRecord_Equal;
  10963. begin
  10964. StartProgram(false);
  10965. Add('type');
  10966. Add(' integer = longint;');
  10967. Add(' TFlag = (red,blue);');
  10968. Add(' TFlags = set of TFlag;');
  10969. Add(' TProc = procedure;');
  10970. Add(' TRecord = record');
  10971. Add(' i: integer;');
  10972. Add(' Event: TProc;');
  10973. Add(' f: TFlags;');
  10974. Add(' end;');
  10975. Add(' TNested = record');
  10976. Add(' r: TRecord;');
  10977. Add(' end;');
  10978. Add('var');
  10979. Add(' b: boolean;');
  10980. Add(' r,s: trecord;');
  10981. Add('begin');
  10982. Add(' b:=r=s;');
  10983. Add(' b:=r<>s;');
  10984. ConvertProgram;
  10985. CheckSource('TestRecord_Equal',
  10986. LinesToStr([ // statements
  10987. 'this.TFlag = {',
  10988. ' "0": "red",',
  10989. ' red: 0,',
  10990. ' "1": "blue",',
  10991. ' blue: 1',
  10992. '};',
  10993. 'rtl.recNewT(this, "TRecord", function () {',
  10994. ' this.i = 0;',
  10995. ' this.Event = null;',
  10996. ' this.$new = function () {',
  10997. ' var r = Object.create(this);',
  10998. ' r.f = {};',
  10999. ' return r;',
  11000. ' };',
  11001. ' this.$eq = function (b) {',
  11002. ' return (this.i === b.i) && rtl.eqCallback(this.Event, b.Event) && rtl.eqSet(this.f, b.f);',
  11003. ' };',
  11004. ' this.$assign = function (s) {',
  11005. ' this.i = s.i;',
  11006. ' this.Event = s.Event;',
  11007. ' this.f = rtl.refSet(s.f);',
  11008. ' return this;',
  11009. ' };',
  11010. '});',
  11011. 'rtl.recNewT(this, "TNested", function () {',
  11012. ' this.$new = function () {',
  11013. ' var r = Object.create(this);',
  11014. ' r.r = $mod.TRecord.$new();',
  11015. ' return r;',
  11016. ' };',
  11017. ' this.$eq = function (b) {',
  11018. ' return this.r.$eq(b.r);',
  11019. ' };',
  11020. ' this.$assign = function (s) {',
  11021. ' this.r.$assign(s.r);',
  11022. ' return this;',
  11023. ' };',
  11024. '});',
  11025. 'this.b = false;',
  11026. 'this.r = this.TRecord.$new();',
  11027. 'this.s = this.TRecord.$new();',
  11028. '']),
  11029. LinesToStr([
  11030. '$mod.b = $mod.r.$eq($mod.s);',
  11031. '$mod.b = !$mod.r.$eq($mod.s);',
  11032. '']));
  11033. end;
  11034. procedure TTestModule.TestRecord_JSValue;
  11035. begin
  11036. StartProgram(false);
  11037. Add([
  11038. 'type',
  11039. ' TRecord = record',
  11040. ' i: longint;',
  11041. ' end;',
  11042. 'procedure Fly(d: jsvalue; const c: jsvalue);',
  11043. 'begin',
  11044. 'end;',
  11045. 'procedure Run(d: TRecord; const c: TRecord; var v: TRecord);',
  11046. 'begin',
  11047. ' if jsvalue(d) then ;',
  11048. ' if jsvalue(c) then ;',
  11049. ' if jsvalue(v) then ;',
  11050. 'end;',
  11051. 'var',
  11052. ' Jv: jsvalue;',
  11053. ' Rec: trecord;',
  11054. 'begin',
  11055. ' rec:=trecord(jv);',
  11056. ' jv:=rec;',
  11057. ' Fly(rec,rec);',
  11058. ' Fly(@rec,@rec);',
  11059. ' if jsvalue(Rec) then ;',
  11060. ' Run(trecord(jv),trecord(jv),rec);',
  11061. '']);
  11062. ConvertProgram;
  11063. CheckSource('TestRecord_JSValue',
  11064. LinesToStr([ // statements
  11065. 'rtl.recNewT(this, "TRecord", function () {',
  11066. ' this.i = 0;',
  11067. ' this.$eq = function (b) {',
  11068. ' return this.i === b.i;',
  11069. ' };',
  11070. ' this.$assign = function (s) {',
  11071. ' this.i = s.i;',
  11072. ' return this;',
  11073. ' };',
  11074. '});',
  11075. 'this.Fly = function (d, c) {',
  11076. '};',
  11077. 'this.Run = function (d, c, v) {',
  11078. ' if (d) ;',
  11079. ' if (c) ;',
  11080. ' if (v) ;',
  11081. '};',
  11082. 'this.Jv = undefined;',
  11083. 'this.Rec = this.TRecord.$new();',
  11084. '']),
  11085. LinesToStr([
  11086. '$mod.Rec.$assign(rtl.getObject($mod.Jv));',
  11087. '$mod.Jv = $mod.Rec;',
  11088. '$mod.Fly($mod.TRecord.$clone($mod.Rec), $mod.Rec);',
  11089. '$mod.Fly($mod.Rec, $mod.Rec);',
  11090. 'if ($mod.Rec) ;',
  11091. '$mod.Run($mod.TRecord.$clone(rtl.getObject($mod.Jv)), rtl.getObject($mod.Jv), $mod.Rec);',
  11092. '']));
  11093. end;
  11094. procedure TTestModule.TestRecord_VariantFail;
  11095. begin
  11096. StartProgram(false);
  11097. Add([
  11098. 'type',
  11099. ' TRec = record',
  11100. ' case word of',
  11101. ' 0: (b0, b1: Byte);',
  11102. ' 1: (i: word);',
  11103. ' end;',
  11104. 'begin']);
  11105. SetExpectedPasResolverError('variant record is not supported',
  11106. nXIsNotSupported);
  11107. ConvertProgram;
  11108. end;
  11109. procedure TTestModule.TestRecord_FieldArray;
  11110. begin
  11111. StartProgram(false);
  11112. Add([
  11113. 'type',
  11114. ' TArrInt = array[3..4] of longint;',
  11115. ' TArrArrInt = array[3..4] of longint;',
  11116. ' TRec = record',
  11117. ' a: array of longint;',
  11118. ' s: array[1..2] of longint;',
  11119. ' m: array[1..2,3..4] of longint;',
  11120. ' o: TArrArrInt;',
  11121. ' end;',
  11122. 'begin']);
  11123. ConvertProgram;
  11124. CheckSource('TestRecord_FieldArray',
  11125. LinesToStr([ // statements
  11126. 'rtl.recNewT(this, "TRec", function () {',
  11127. ' this.$new = function () {',
  11128. ' var r = Object.create(this);',
  11129. ' r.a = [];',
  11130. ' r.s = rtl.arraySetLength(null, 0, 2);',
  11131. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  11132. ' r.o = rtl.arraySetLength(null, 0, 2);',
  11133. ' return r;',
  11134. ' };',
  11135. ' this.$eq = function (b) {',
  11136. ' return (this.a === b.a) && rtl.arrayEq(this.s, b.s) && rtl.arrayEq(this.m, b.m) && rtl.arrayEq(this.o, b.o);',
  11137. ' };',
  11138. ' this.$assign = function (s) {',
  11139. ' this.a = rtl.arrayRef(s.a);',
  11140. ' this.s = s.s.slice(0);',
  11141. ' this.m = s.m.slice(0);',
  11142. ' this.o = s.o.slice(0);',
  11143. ' return this;',
  11144. ' };',
  11145. '});',
  11146. '']),
  11147. LinesToStr([ // $mod.$main
  11148. '']));
  11149. end;
  11150. procedure TTestModule.TestRecord_Const;
  11151. begin
  11152. StartProgram(false);
  11153. Add([
  11154. 'type',
  11155. ' TArrInt = array[3..4] of longint;',
  11156. ' TPoint = record x,y: longint; end;',
  11157. ' TRec = record',
  11158. ' i: longint;',
  11159. ' a: array of longint;',
  11160. ' s: array[1..2] of longint;',
  11161. ' m: array[1..2,3..4] of longint;',
  11162. ' p: TPoint;',
  11163. ' end;',
  11164. ' TPoints = array of TPoint;',
  11165. 'const',
  11166. ' r: TRec = (',
  11167. ' i:1;',
  11168. ' a:(2,3);',
  11169. ' s:(4,5);',
  11170. ' m:( (11,12), (13,14) );',
  11171. ' p: (x:21; y:22)',
  11172. ' );',
  11173. ' p: TPoints = ( (x:1;y:2), (x:3;y:4) );',
  11174. 'begin']);
  11175. ConvertProgram;
  11176. CheckSource('TestRecord_Const',
  11177. LinesToStr([ // statements
  11178. 'rtl.recNewT(this, "TPoint", function () {',
  11179. ' this.x = 0;',
  11180. ' this.y = 0;',
  11181. ' this.$eq = function (b) {',
  11182. ' return (this.x === b.x) && (this.y === b.y);',
  11183. ' };',
  11184. ' this.$assign = function (s) {',
  11185. ' this.x = s.x;',
  11186. ' this.y = s.y;',
  11187. ' return this;',
  11188. ' };',
  11189. '});',
  11190. 'rtl.recNewT(this, "TRec", function () {',
  11191. ' this.i = 0;',
  11192. ' this.$new = function () {',
  11193. ' var r = Object.create(this);',
  11194. ' r.a = [];',
  11195. ' r.s = rtl.arraySetLength(null, 0, 2);',
  11196. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  11197. ' r.p = $mod.TPoint.$new();',
  11198. ' return r;',
  11199. ' };',
  11200. ' this.$eq = function (b) {',
  11201. ' 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);',
  11202. ' };',
  11203. ' this.$assign = function (s) {',
  11204. ' this.i = s.i;',
  11205. ' this.a = rtl.arrayRef(s.a);',
  11206. ' this.s = s.s.slice(0);',
  11207. ' this.m = s.m.slice(0);',
  11208. ' this.p.$assign(s.p);',
  11209. ' return this;',
  11210. ' };',
  11211. '});',
  11212. 'this.r = this.TRec.$clone({',
  11213. ' i: 1,',
  11214. ' a: [2, 3],',
  11215. ' s: [4, 5],',
  11216. ' m: [[11, 12], [13, 14]],',
  11217. ' p: this.TPoint.$clone({',
  11218. ' x: 21,',
  11219. ' y: 22',
  11220. ' })',
  11221. '});',
  11222. 'this.p = [this.TPoint.$clone({',
  11223. ' x: 1,',
  11224. ' y: 2',
  11225. '}), this.TPoint.$clone({',
  11226. ' x: 3,',
  11227. ' y: 4',
  11228. '})];',
  11229. '']),
  11230. LinesToStr([ // $mod.$main
  11231. '']));
  11232. end;
  11233. procedure TTestModule.TestRecord_TypecastFail;
  11234. begin
  11235. StartProgram(false);
  11236. Add([
  11237. 'type',
  11238. ' TPoint = record x,y: longint; end;',
  11239. ' TRec = record l: longint end;',
  11240. 'var p: TPoint;',
  11241. 'begin',
  11242. ' if TRec(p).l=2 then ;']);
  11243. SetExpectedPasResolverError('Illegal type conversion: "TPoint" to "record TRec"',
  11244. nIllegalTypeConversionTo);
  11245. ConvertProgram;
  11246. end;
  11247. procedure TTestModule.TestRecord_InFunction;
  11248. begin
  11249. StartProgram(false);
  11250. Add([
  11251. 'var TPoint: longint = 3;',
  11252. 'procedure DoIt;',
  11253. 'type',
  11254. ' TPoint = record x,y: longint; end;',
  11255. ' TPoints = array of TPoint;',
  11256. 'var',
  11257. ' r: TPoint;',
  11258. ' p: TPoints;',
  11259. 'begin',
  11260. ' SetLength(p,2);',
  11261. 'end;',
  11262. 'begin']);
  11263. ConvertProgram;
  11264. CheckSource('TestRecord_InFunction',
  11265. LinesToStr([ // statements
  11266. 'this.TPoint = 3;',
  11267. 'var TPoint$1 = rtl.recNewT(null, "", function () {',
  11268. ' this.x = 0;',
  11269. ' this.y = 0;',
  11270. ' this.$eq = function (b) {',
  11271. ' return (this.x === b.x) && (this.y === b.y);',
  11272. ' };',
  11273. ' this.$assign = function (s) {',
  11274. ' this.x = s.x;',
  11275. ' this.y = s.y;',
  11276. ' return this;',
  11277. ' };',
  11278. '});',
  11279. 'this.DoIt = function () {',
  11280. ' var r = TPoint$1.$new();',
  11281. ' var p = [];',
  11282. ' p = rtl.arraySetLength(p, TPoint$1, 2);',
  11283. '};',
  11284. '']),
  11285. LinesToStr([ // $mod.$main
  11286. '']));
  11287. end;
  11288. procedure TTestModule.TestRecord_AnonymousFail;
  11289. begin
  11290. StartProgram(false);
  11291. Add([
  11292. 'var',
  11293. ' r: record x: word end;',
  11294. 'begin']);
  11295. SetExpectedPasResolverError('not yet implemented: :TPasRecordType [20190408224556] "anonymous record type"',
  11296. nNotYetImplemented);
  11297. ConvertProgram;
  11298. end;
  11299. procedure TTestModule.TestAdvRecord_Function;
  11300. begin
  11301. StartProgram(false);
  11302. Parser.Options:=Parser.Options+[po_cassignments];
  11303. Add([
  11304. '{$modeswitch AdvancedRecords}',
  11305. 'type',
  11306. ' TPoint = record',
  11307. ' x,y: word;',
  11308. ' function Add(const apt: TPoint): TPoint;',
  11309. ' end;',
  11310. 'function TPoint.Add(const apt: TPoint): TPoint;',
  11311. 'begin',
  11312. ' Result:=Self;',
  11313. ' Result.x+=apt.x;',
  11314. ' Result.y:=Result.y+apt.y;',
  11315. ' Self:=apt;',
  11316. 'end;',
  11317. 'var p,q: TPoint;',
  11318. 'begin',
  11319. ' p.add(q);',
  11320. ' p:=default(TPoint);',
  11321. ' p:=q;',
  11322. '']);
  11323. ConvertProgram;
  11324. CheckSource('TestAdvRecord_Function',
  11325. LinesToStr([ // statements
  11326. 'rtl.recNewT(this, "TPoint", function () {',
  11327. ' this.x = 0;',
  11328. ' this.y = 0;',
  11329. ' this.$eq = function (b) {',
  11330. ' return (this.x === b.x) && (this.y === b.y);',
  11331. ' };',
  11332. ' this.$assign = function (s) {',
  11333. ' this.x = s.x;',
  11334. ' this.y = s.y;',
  11335. ' return this;',
  11336. ' };',
  11337. ' this.Add = function (apt) {',
  11338. ' var Result = $mod.TPoint.$new();',
  11339. ' Result.$assign(this);',
  11340. ' Result.x += apt.x;',
  11341. ' Result.y = Result.y + apt.y;',
  11342. ' this.$assign(apt);',
  11343. ' return Result;',
  11344. ' };',
  11345. '});',
  11346. 'this.p = this.TPoint.$new();',
  11347. 'this.q = this.TPoint.$new();',
  11348. '']),
  11349. LinesToStr([ // $mod.$main
  11350. '$mod.p.Add($mod.q);',
  11351. '$mod.p.$assign($mod.TPoint.$new());',
  11352. '$mod.p.$assign($mod.q);',
  11353. '']));
  11354. end;
  11355. procedure TTestModule.TestAdvRecord_Property;
  11356. begin
  11357. StartProgram(false);
  11358. Add([
  11359. '{$modeswitch AdvancedRecords}',
  11360. 'type',
  11361. ' TPoint = record',
  11362. ' x,y: word;',
  11363. ' strict private',
  11364. ' function GetSize: longword;',
  11365. ' procedure SetSize(Value: longword);',
  11366. ' public',
  11367. ' property Size: longword read GetSize write SetSize;',
  11368. ' property Left: word read x write y;',
  11369. ' end;',
  11370. 'procedure SetSize(Value: longword); begin end;',// check auto rename
  11371. 'function TPoint.GetSize: longword;',
  11372. 'begin',
  11373. ' x:=y;',
  11374. ' Size:=Size;',
  11375. ' Left:=Left;',
  11376. 'end;',
  11377. 'procedure TPoint.SetSize(Value: longword);',
  11378. 'begin',
  11379. 'end;',
  11380. 'var p,q: TPoint;',
  11381. 'begin',
  11382. ' p.Size:=q.Size;',
  11383. ' p.Left:=q.Left;',
  11384. '']);
  11385. ConvertProgram;
  11386. CheckSource('TestAdvRecord_Property',
  11387. LinesToStr([ // statements
  11388. 'rtl.recNewT(this, "TPoint", function () {',
  11389. ' this.x = 0;',
  11390. ' this.y = 0;',
  11391. ' this.$eq = function (b) {',
  11392. ' return (this.x === b.x) && (this.y === b.y);',
  11393. ' };',
  11394. ' this.$assign = function (s) {',
  11395. ' this.x = s.x;',
  11396. ' this.y = s.y;',
  11397. ' return this;',
  11398. ' };',
  11399. ' this.GetSize = function () {',
  11400. ' var Result = 0;',
  11401. ' this.x = this.y;',
  11402. ' this.SetSize(this.GetSize());',
  11403. ' this.y = this.x;',
  11404. ' return Result;',
  11405. ' };',
  11406. ' this.SetSize = function (Value) {',
  11407. ' };',
  11408. '});',
  11409. 'this.SetSize = function (Value) {',
  11410. '};',
  11411. 'this.p = this.TPoint.$new();',
  11412. 'this.q = this.TPoint.$new();',
  11413. '']),
  11414. LinesToStr([ // $mod.$main
  11415. '$mod.p.SetSize($mod.q.GetSize());',
  11416. '$mod.p.y = $mod.q.x;',
  11417. '']));
  11418. end;
  11419. procedure TTestModule.TestAdvRecord_PropertyDefault;
  11420. begin
  11421. StartProgram(false);
  11422. Add([
  11423. '{$modeswitch AdvancedRecords}',
  11424. 'type',
  11425. ' TPoint = record',
  11426. ' strict private',
  11427. ' function GetItems(Index: word): word;',
  11428. ' procedure SetItems(Index: word; Value: word);',
  11429. ' public',
  11430. ' property Items[Index: word]: word read GetItems write SetItems; default;',
  11431. ' end;',
  11432. 'function TPoint.GetItems(Index: word): word;',
  11433. 'begin',
  11434. ' Items[index]:=Items[index];',
  11435. ' self.Items[index]:=self.Items[index];',
  11436. 'end;',
  11437. 'procedure TPoint.SetItems(Index: word; Value: word);',
  11438. 'begin',
  11439. 'end;',
  11440. 'var p: TPoint;',
  11441. 'begin',
  11442. ' p[1]:=p[2];',
  11443. ' p.Items[3]:=p.Items[4];',
  11444. '']);
  11445. ConvertProgram;
  11446. CheckSource('TestAdvRecord_PropertyDefault',
  11447. LinesToStr([ // statements
  11448. 'rtl.recNewT(this, "TPoint", function () {',
  11449. ' this.$eq = function (b) {',
  11450. ' return true;',
  11451. ' };',
  11452. ' this.$assign = function (s) {',
  11453. ' return this;',
  11454. ' };',
  11455. ' this.GetItems = function (Index) {',
  11456. ' var Result = 0;',
  11457. ' this.SetItems(Index, this.GetItems(Index));',
  11458. ' this.SetItems(Index, this.GetItems(Index));',
  11459. ' return Result;',
  11460. ' };',
  11461. ' this.SetItems = function (Index, Value) {',
  11462. ' };',
  11463. '});',
  11464. 'this.p = this.TPoint.$new();',
  11465. '']),
  11466. LinesToStr([ // $mod.$main
  11467. '$mod.p.SetItems(1, $mod.p.GetItems(2));',
  11468. '$mod.p.SetItems(3, $mod.p.GetItems(4));',
  11469. '']));
  11470. end;
  11471. procedure TTestModule.TestAdvRecord_Property_ClassMethod;
  11472. begin
  11473. StartProgram(false);
  11474. Add([
  11475. '{$modeswitch AdvancedRecords}',
  11476. 'type',
  11477. ' TRec = record',
  11478. ' class var',
  11479. ' Fx: longint;',
  11480. ' Fy: longint;',
  11481. ' class function GetInt: longint; static;',
  11482. ' class procedure SetInt(Value: longint); static;',
  11483. ' class procedure DoIt; static;',
  11484. ' class property IntA: longint read Fx write Fy;',
  11485. ' class property IntB: longint read GetInt write SetInt;',
  11486. ' end;',
  11487. 'class function trec.getint: longint;',
  11488. 'begin',
  11489. ' result:=fx;',
  11490. 'end;',
  11491. 'class procedure trec.setint(value: longint);',
  11492. 'begin',
  11493. 'end;',
  11494. 'class procedure trec.doit;',
  11495. 'begin',
  11496. ' IntA:=IntA+1;',
  11497. ' IntB:=IntB+1;',
  11498. 'end;',
  11499. 'var r: trec;',
  11500. 'begin',
  11501. ' trec.inta:=trec.inta+1;',
  11502. ' if trec.intb=2 then;',
  11503. ' trec.intb:=trec.intb+2;',
  11504. ' trec.setint(trec.inta);',
  11505. ' r.inta:=r.inta+1;',
  11506. ' if r.intb=2 then;',
  11507. ' r.intb:=r.intb+2;',
  11508. ' r.setint(r.inta);']);
  11509. ConvertProgram;
  11510. CheckSource('TestAdvRecord_Property_ClassMethod',
  11511. LinesToStr([ // statements
  11512. 'rtl.recNewT(this, "TRec", function () {',
  11513. ' this.Fx = 0;',
  11514. ' this.Fy = 0;',
  11515. ' this.$eq = function (b) {',
  11516. ' return true;',
  11517. ' };',
  11518. ' this.$assign = function (s) {',
  11519. ' return this;',
  11520. ' };',
  11521. ' this.GetInt = function () {',
  11522. ' var Result = 0;',
  11523. ' Result = $mod.TRec.Fx;',
  11524. ' return Result;',
  11525. ' };',
  11526. ' this.SetInt = function (Value) {',
  11527. ' };',
  11528. ' this.DoIt = function () {',
  11529. ' $mod.TRec.Fy = $mod.TRec.Fx + 1;',
  11530. ' $mod.TRec.SetInt($mod.TRec.GetInt() + 1);',
  11531. ' };',
  11532. '}, true);',
  11533. 'this.r = this.TRec.$new();',
  11534. '']),
  11535. LinesToStr([ // $mod.$main
  11536. '$mod.TRec.Fy = $mod.TRec.Fx + 1;',
  11537. 'if ($mod.TRec.GetInt() === 2) ;',
  11538. '$mod.TRec.SetInt($mod.TRec.GetInt() + 2);',
  11539. '$mod.TRec.SetInt($mod.TRec.Fx);',
  11540. '$mod.TRec.Fy = $mod.r.Fx + 1;',
  11541. 'if ($mod.r.GetInt() === 2) ;',
  11542. '$mod.r.SetInt($mod.r.GetInt() + 2);',
  11543. '$mod.r.SetInt($mod.r.Fx);',
  11544. '']));
  11545. end;
  11546. procedure TTestModule.TestAdvRecord_Const;
  11547. begin
  11548. StartProgram(false);
  11549. Add([
  11550. '{$modeswitch AdvancedRecords}',
  11551. 'type',
  11552. ' TArrInt = array[3..4] of longint;',
  11553. ' TPoint = record',
  11554. ' x,y: longint;',
  11555. ' class var Count: nativeint;',
  11556. ' end;',
  11557. ' TRec = record',
  11558. ' i: longint;',
  11559. ' a: array of longint;',
  11560. ' s: array[1..2] of longint;',
  11561. ' m: array[1..2,3..4] of longint;',
  11562. ' p: TPoint;',
  11563. ' end;',
  11564. ' TPoints = array of TPoint;',
  11565. 'const',
  11566. ' r: TRec = (',
  11567. ' i:1;',
  11568. ' a:(2,3);',
  11569. ' s:(4,5);',
  11570. ' m:( (11,12), (13,14) );',
  11571. ' p: (x:21)',
  11572. ' );',
  11573. ' p: TPoints = ( (x:1;y:2), (x:3;y:4) );',
  11574. 'begin']);
  11575. ConvertProgram;
  11576. CheckSource('TestAdvRecord_Const',
  11577. LinesToStr([ // statements
  11578. 'rtl.recNewT(this, "TPoint", function () {',
  11579. ' this.x = 0;',
  11580. ' this.y = 0;',
  11581. ' this.Count = 0;',
  11582. ' this.$eq = function (b) {',
  11583. ' return (this.x === b.x) && (this.y === b.y);',
  11584. ' };',
  11585. ' this.$assign = function (s) {',
  11586. ' this.x = s.x;',
  11587. ' this.y = s.y;',
  11588. ' return this;',
  11589. ' };',
  11590. '}, true);',
  11591. 'rtl.recNewT(this, "TRec", function () {',
  11592. ' this.i = 0;',
  11593. ' this.$new = function () {',
  11594. ' var r = Object.create(this);',
  11595. ' r.a = [];',
  11596. ' r.s = rtl.arraySetLength(null, 0, 2);',
  11597. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  11598. ' r.p = $mod.TPoint.$new();',
  11599. ' return r;',
  11600. ' };',
  11601. ' this.$eq = function (b) {',
  11602. ' 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);',
  11603. ' };',
  11604. ' this.$assign = function (s) {',
  11605. ' this.i = s.i;',
  11606. ' this.a = rtl.arrayRef(s.a);',
  11607. ' this.s = s.s.slice(0);',
  11608. ' this.m = s.m.slice(0);',
  11609. ' this.p.$assign(s.p);',
  11610. ' return this;',
  11611. ' };',
  11612. '});',
  11613. 'this.r = this.TRec.$clone({',
  11614. ' i: 1,',
  11615. ' a: [2, 3],',
  11616. ' s: [4, 5],',
  11617. ' m: [[11, 12], [13, 14]],',
  11618. ' p: this.TPoint.$clone({',
  11619. ' x: 21,',
  11620. ' y: 0',
  11621. ' })',
  11622. '});',
  11623. 'this.p = [this.TPoint.$clone({',
  11624. ' x: 1,',
  11625. ' y: 2',
  11626. '}), this.TPoint.$clone({',
  11627. ' x: 3,',
  11628. ' y: 4',
  11629. '})];',
  11630. '']),
  11631. LinesToStr([ // $mod.$main
  11632. '']));
  11633. end;
  11634. procedure TTestModule.TestAdvRecord_ExternalField;
  11635. begin
  11636. StartProgram(false);
  11637. Add([
  11638. '{$modeswitch AdvancedRecords}',
  11639. '{$modeswitch externalclass}',
  11640. 'type',
  11641. ' TCar = record',
  11642. ' public',
  11643. ' Intern: longint external name ''$Intern'';',
  11644. ' Intern2: longint external name ''$Intern2'';',
  11645. ' Bracket: longint external name ''["A B"]'';',
  11646. ' procedure DoIt;',
  11647. ' end;',
  11648. 'procedure tcar.doit;',
  11649. 'begin',
  11650. ' Intern:=Intern+1;',
  11651. ' Intern2:=Intern2+2;',
  11652. ' Bracket:=Bracket+3;',
  11653. 'end;',
  11654. 'var Rec: TCar = (intern: 11; intern2: 12; bracket: 13);',
  11655. 'begin',
  11656. ' Rec.intern:=Rec.intern+1;',
  11657. ' Rec.intern2:=Rec.intern2+2;',
  11658. ' Rec.Bracket:=Rec.Bracket+3;',
  11659. ' with Rec do begin',
  11660. ' intern:=intern+1;',
  11661. ' intern2:=intern2+2;',
  11662. ' Bracket:=Bracket+3;',
  11663. ' end;']);
  11664. ConvertProgram;
  11665. CheckSource('TestAdvRecord_ExternalField',
  11666. LinesToStr([ // statements
  11667. 'rtl.recNewT(this, "TCar", function () {',
  11668. ' this.$eq = function (b) {',
  11669. ' return (this.$Intern === b.$Intern) && (this.$Intern2 === b.$Intern2) && (this["A B"] === b["A B"]);',
  11670. ' };',
  11671. ' this.$assign = function (s) {',
  11672. ' this.$Intern = s.$Intern;',
  11673. ' this.$Intern2 = s.$Intern2;',
  11674. ' this["A B"] = s["A B"];',
  11675. ' return this;',
  11676. ' };',
  11677. ' this.DoIt = function () {',
  11678. ' this.$Intern = this.$Intern + 1;',
  11679. ' this.$Intern2 = this.$Intern2 + 2;',
  11680. ' this["A B"] = this["A B"] + 3;',
  11681. ' };',
  11682. '});',
  11683. 'this.Rec = this.TCar.$clone({',
  11684. ' $Intern: 11,',
  11685. ' $Intern2: 12,',
  11686. ' "A B": 13',
  11687. '});',
  11688. '']),
  11689. LinesToStr([ // $mod.$main
  11690. '$mod.Rec.$Intern = $mod.Rec.$Intern + 1;',
  11691. '$mod.Rec.$Intern2 = $mod.Rec.$Intern2 + 2;',
  11692. '$mod.Rec["A B"] = $mod.Rec["A B"] + 3;',
  11693. 'var $with = $mod.Rec;',
  11694. '$with.$Intern = $with.$Intern + 1;',
  11695. '$with.$Intern2 = $with.$Intern2 + 2;',
  11696. '$with["A B"] = $with["A B"] + 3;',
  11697. '']));
  11698. end;
  11699. procedure TTestModule.TestAdvRecord_SubRecord;
  11700. begin
  11701. StartProgram(false);
  11702. Add([
  11703. '{$modeswitch AdvancedRecords}',
  11704. 'type',
  11705. ' TRec = record',
  11706. ' type',
  11707. ' TPoint = record',
  11708. ' x,y: longint;',
  11709. ' class var Count: nativeint;',
  11710. ' procedure DoIt;',
  11711. ' class procedure DoThat; static;',
  11712. ' end;',
  11713. ' var',
  11714. ' i: longint;',
  11715. ' p: TPoint;',
  11716. ' procedure DoSome;',
  11717. ' end;',
  11718. 'const',
  11719. ' r: TRec = (',
  11720. ' i:1;',
  11721. ' p: (x:21;y:22)',
  11722. ' );',
  11723. 'procedure TRec.DoSome;',
  11724. 'begin',
  11725. ' p.x:=p.y+1;',
  11726. ' p.Count:=p.Count+2;',
  11727. 'end;',
  11728. 'procedure TRec.TPoint.DoIt;',
  11729. 'begin',
  11730. ' Count:=Count+3;',
  11731. 'end;',
  11732. 'class procedure TRec.TPoint.DoThat;',
  11733. 'begin',
  11734. ' Count:=Count+4;',
  11735. 'end;',
  11736. 'begin']);
  11737. ConvertProgram;
  11738. CheckSource('TestAdvRecord_SubRecord',
  11739. LinesToStr([ // statements
  11740. 'rtl.recNewT(this, "TRec", function () {',
  11741. ' rtl.recNewT(this, "TPoint", function () {',
  11742. ' this.x = 0;',
  11743. ' this.y = 0;',
  11744. ' this.Count = 0;',
  11745. ' this.$eq = function (b) {',
  11746. ' return (this.x === b.x) && (this.y === b.y);',
  11747. ' };',
  11748. ' this.$assign = function (s) {',
  11749. ' this.x = s.x;',
  11750. ' this.y = s.y;',
  11751. ' return this;',
  11752. ' };',
  11753. ' this.DoIt = function () {',
  11754. ' $mod.TRec.TPoint.Count = this.Count + 3;',
  11755. ' };',
  11756. ' this.DoThat = function () {',
  11757. ' $mod.TRec.TPoint.Count = $mod.TRec.TPoint.Count + 4;',
  11758. ' };',
  11759. ' }, true);',
  11760. ' this.i = 0;',
  11761. ' this.$new = function () {',
  11762. ' var r = Object.create(this);',
  11763. ' r.p = this.TPoint.$new();',
  11764. ' return r;',
  11765. ' };',
  11766. ' this.$eq = function (b) {',
  11767. ' return (this.i === b.i) && this.p.$eq(b.p);',
  11768. ' };',
  11769. ' this.$assign = function (s) {',
  11770. ' this.i = s.i;',
  11771. ' this.p.$assign(s.p);',
  11772. ' return this;',
  11773. ' };',
  11774. ' this.DoSome = function () {',
  11775. ' this.p.x = this.p.y + 1;',
  11776. ' this.TPoint.Count = this.p.Count + 2;',
  11777. ' };',
  11778. '}, true);',
  11779. 'this.r = this.TRec.$clone({',
  11780. ' i: 1,',
  11781. ' p: this.TRec.TPoint.$clone({',
  11782. ' x: 21,',
  11783. ' y: 22',
  11784. ' })',
  11785. '});',
  11786. '']),
  11787. LinesToStr([ // $mod.$main
  11788. '']));
  11789. end;
  11790. procedure TTestModule.TestAdvRecord_SubClass;
  11791. begin
  11792. StartProgram(false);
  11793. Add([
  11794. '{$modeswitch AdvancedRecords}',
  11795. 'type',
  11796. ' TObject = class end;',
  11797. ' TPoint = record',
  11798. ' type',
  11799. ' TBird = class',
  11800. ' procedure DoIt;',
  11801. ' class procedure Glob;',
  11802. ' end;',
  11803. ' procedure DoIt(b: TBird);',
  11804. ' end;',
  11805. 'procedure TPoint.TBird.DoIt;',
  11806. 'begin',
  11807. ' doit;',
  11808. ' self.doit;',
  11809. ' glob;',
  11810. ' self.glob;',
  11811. 'end;',
  11812. 'class procedure TPoint.TBird.Glob;',
  11813. 'begin',
  11814. ' glob;',
  11815. ' self.glob;',
  11816. 'end;',
  11817. 'procedure TPoint.DoIt(b: TBird);',
  11818. 'begin',
  11819. ' b.doit;',
  11820. ' b.glob;',
  11821. ' TBird.glob;',
  11822. 'end;',
  11823. 'begin',
  11824. '']);
  11825. ConvertProgram;
  11826. CheckSource('TestAdvRecord_SubClass',
  11827. LinesToStr([ // statements
  11828. 'rtl.createClass(this, "TObject", null, function () {',
  11829. ' this.$init = function () {',
  11830. ' };',
  11831. ' this.$final = function () {',
  11832. ' };',
  11833. '});',
  11834. 'rtl.recNewT(this, "TPoint", function () {',
  11835. ' rtl.createClass(this, "TBird", $mod.TObject, function () {',
  11836. ' this.DoIt = function () {',
  11837. ' this.DoIt();',
  11838. ' this.DoIt();',
  11839. ' this.$class.Glob();',
  11840. ' this.$class.Glob();',
  11841. ' };',
  11842. ' this.Glob = function () {',
  11843. ' this.Glob();',
  11844. ' this.Glob();',
  11845. ' };',
  11846. ' }, "TPoint.TBird");',
  11847. ' this.$eq = function (b) {',
  11848. ' return true;',
  11849. ' };',
  11850. ' this.$assign = function (s) {',
  11851. ' return this;',
  11852. ' };',
  11853. ' this.DoIt = function (b) {',
  11854. ' b.DoIt();',
  11855. ' b.$class.Glob();',
  11856. ' this.TBird.Glob();',
  11857. ' };',
  11858. '}, true);',
  11859. '']),
  11860. LinesToStr([ // $mod.$main
  11861. '']));
  11862. end;
  11863. procedure TTestModule.TestAdvRecord_SubInterfaceFail;
  11864. begin
  11865. StartProgram(false);
  11866. Add([
  11867. '{$modeswitch AdvancedRecords}',
  11868. 'type',
  11869. ' IUnknown = interface end;',
  11870. ' TPoint = record',
  11871. ' type IBird = interface end;',
  11872. ' end;',
  11873. 'begin',
  11874. '']);
  11875. SetExpectedPasResolverError('not yet implemented: IBird:TPasClassType [20190105143752] "interface inside record"',
  11876. nNotYetImplemented);
  11877. ParseProgram;
  11878. end;
  11879. procedure TTestModule.TestAdvRecord_Constructor;
  11880. begin
  11881. StartProgram(false);
  11882. Add([
  11883. '{$modeswitch AdvancedRecords}',
  11884. 'type',
  11885. ' TPoint = record',
  11886. ' x,y: longint;',
  11887. ' constructor Create(ax: longint; ay: longint = -1);',
  11888. ' end;',
  11889. 'constructor tpoint.create(ax,ay: longint);',
  11890. 'begin',
  11891. ' x:=ax;',
  11892. ' self.y:=ay;',
  11893. 'end;',
  11894. 'var r: TPoint;',
  11895. 'begin',
  11896. ' r:=TPoint.Create(1,2);',
  11897. ' with TPoint do r:=Create(1,2);',
  11898. ' r.Create(3);',
  11899. ' r:=r.Create(4);',
  11900. '']);
  11901. ConvertProgram;
  11902. CheckSource('TestAdvRecord_Constructor',
  11903. LinesToStr([ // statements
  11904. 'rtl.recNewT(this, "TPoint", function () {',
  11905. ' this.x = 0;',
  11906. ' this.y = 0;',
  11907. ' this.$eq = function (b) {',
  11908. ' return (this.x === b.x) && (this.y === b.y);',
  11909. ' };',
  11910. ' this.$assign = function (s) {',
  11911. ' this.x = s.x;',
  11912. ' this.y = s.y;',
  11913. ' return this;',
  11914. ' };',
  11915. ' this.Create = function (ax, ay) {',
  11916. ' this.x = ax;',
  11917. ' this.y = ay;',
  11918. ' return this;',
  11919. ' };',
  11920. '}, true);',
  11921. 'this.r = this.TPoint.$new();',
  11922. '']),
  11923. LinesToStr([ // $mod.$main
  11924. '$mod.r.$assign($mod.TPoint.$new().Create(1, 2));',
  11925. 'var $with = $mod.TPoint;',
  11926. '$mod.r.$assign($with.$new().Create(1, 2));',
  11927. '$mod.r.Create(3, -1);',
  11928. '$mod.r.$assign($mod.r.Create(4, -1));',
  11929. '']));
  11930. end;
  11931. procedure TTestModule.TestAdvRecord_ClassConstructor_Program;
  11932. begin
  11933. StartProgram(false);
  11934. Add([
  11935. '{$modeswitch AdvancedRecords}',
  11936. 'type',
  11937. ' TPoint = record',
  11938. ' class var x: longint;',
  11939. ' class procedure Fly; static;',
  11940. ' class constructor Init;',
  11941. ' end;',
  11942. 'var count: word;',
  11943. 'class procedure Tpoint.Fly;',
  11944. 'begin',
  11945. 'end;',
  11946. 'class constructor tpoint.init;',
  11947. 'begin',
  11948. ' count:=count+1;',
  11949. ' x:=x+3;',
  11950. ' tpoint.x:=tpoint.x+4;',
  11951. ' fly;',
  11952. ' tpoint.fly;',
  11953. 'end;',
  11954. 'var r: TPoint;',
  11955. 'begin',
  11956. ' r.x:=r.x+10;',
  11957. ' r.Fly;',
  11958. ' r.Fly();',
  11959. '']);
  11960. ConvertProgram;
  11961. CheckSource('TestAdvRecord_ClassConstructor_Program',
  11962. LinesToStr([ // statements
  11963. 'rtl.recNewT(this, "TPoint", function () {',
  11964. ' this.x = 0;',
  11965. ' this.$eq = function (b) {',
  11966. ' return true;',
  11967. ' };',
  11968. ' this.$assign = function (s) {',
  11969. ' return this;',
  11970. ' };',
  11971. ' this.Fly = function () {',
  11972. ' };',
  11973. '}, true);',
  11974. 'this.count = 0;',
  11975. 'this.r = this.TPoint.$new();',
  11976. '']),
  11977. LinesToStr([ // $mod.$main
  11978. '(function () {',
  11979. ' $mod.count = $mod.count + 1;',
  11980. ' $mod.TPoint.x = $mod.TPoint.x + 3;',
  11981. ' $mod.TPoint.x = $mod.TPoint.x + 4;',
  11982. ' $mod.TPoint.Fly();',
  11983. ' $mod.TPoint.Fly();',
  11984. '})();',
  11985. '$mod.TPoint.x = $mod.r.x + 10;',
  11986. '$mod.r.Fly();',
  11987. '$mod.r.Fly();',
  11988. '']));
  11989. end;
  11990. procedure TTestModule.TestAdvRecord_ClassConstructor_Unit;
  11991. begin
  11992. StartUnit(false);
  11993. Add([
  11994. 'interface',
  11995. '{$modeswitch AdvancedRecords}',
  11996. 'type',
  11997. ' TPoint = record',
  11998. ' class var x: longint;',
  11999. ' class procedure Fly; static;',
  12000. ' class constructor Init;',
  12001. ' end;',
  12002. 'implementation',
  12003. 'var count: word;',
  12004. 'class procedure Tpoint.Fly;',
  12005. 'begin',
  12006. 'end;',
  12007. 'class constructor tpoint.init;',
  12008. 'begin',
  12009. ' count:=count+1;',
  12010. ' x:=3;',
  12011. ' tpoint.x:=4;',
  12012. ' fly;',
  12013. ' tpoint.fly;',
  12014. 'end;',
  12015. '']);
  12016. ConvertUnit;
  12017. CheckSource('TestAdvRecord_ClassConstructor_Unit',
  12018. LinesToStr([ // statements
  12019. 'var $impl = $mod.$impl;',
  12020. 'rtl.recNewT(this, "TPoint", function () {',
  12021. ' this.x = 0;',
  12022. ' this.$eq = function (b) {',
  12023. ' return true;',
  12024. ' };',
  12025. ' this.$assign = function (s) {',
  12026. ' return this;',
  12027. ' };',
  12028. ' this.Fly = function () {',
  12029. ' };',
  12030. '}, true);',
  12031. '']),
  12032. LinesToStr([ // $mod.$init
  12033. '(function () {',
  12034. ' $impl.count = $impl.count + 1;',
  12035. ' $mod.TPoint.x = 3;',
  12036. ' $mod.TPoint.x = 4;',
  12037. ' $mod.TPoint.Fly();',
  12038. ' $mod.TPoint.Fly();',
  12039. '})();',
  12040. '']),
  12041. LinesToStr([ // $mod.$main
  12042. '$impl.count = 0;',
  12043. '']));
  12044. end;
  12045. procedure TTestModule.TestClass_TObjectDefaultConstructor;
  12046. begin
  12047. StartProgram(false);
  12048. Add(['type',
  12049. ' TObject = class',
  12050. ' public',
  12051. ' constructor Create;',
  12052. ' destructor Destroy;',
  12053. ' end;',
  12054. ' TBird = TObject;',
  12055. 'constructor tobject.create;',
  12056. 'begin end;',
  12057. 'destructor tobject.destroy;',
  12058. 'begin end;',
  12059. 'var Obj: tobject;',
  12060. 'begin',
  12061. ' obj:=tobject.create;',
  12062. ' obj:=tobject.create();',
  12063. ' obj:=tbird.create;',
  12064. ' obj:=tbird.create();',
  12065. ' obj:=obj.create();',
  12066. ' obj.destroy;',
  12067. '']);
  12068. ConvertProgram;
  12069. CheckSource('TestClass_TObjectDefaultConstructor',
  12070. LinesToStr([ // statements
  12071. 'rtl.createClass(this,"TObject",null,function(){',
  12072. ' this.$init = function () {',
  12073. ' };',
  12074. ' this.$final = function () {',
  12075. ' };',
  12076. ' this.Create = function(){',
  12077. ' return this;',
  12078. ' };',
  12079. ' this.Destroy = function(){',
  12080. ' };',
  12081. '});',
  12082. 'this.Obj = null;'
  12083. ]),
  12084. LinesToStr([ // $mod.$main
  12085. '$mod.Obj = $mod.TObject.$create("Create");',
  12086. '$mod.Obj = $mod.TObject.$create("Create");',
  12087. '$mod.Obj = $mod.TObject.$create("Create");',
  12088. '$mod.Obj = $mod.TObject.$create("Create");',
  12089. '$mod.Obj = $mod.Obj.Create();',
  12090. '$mod.Obj.$destroy("Destroy");',
  12091. '']));
  12092. end;
  12093. procedure TTestModule.TestClass_TObjectConstructorWithParams;
  12094. begin
  12095. StartProgram(false);
  12096. Add('type');
  12097. Add(' TObject = class');
  12098. Add(' public');
  12099. Add(' constructor Create(Par: longint);');
  12100. Add(' end;');
  12101. Add('constructor tobject.create(par: longint);');
  12102. Add('begin end;');
  12103. Add('var Obj: tobject;');
  12104. Add('begin');
  12105. Add(' obj:=tobject.create(3);');
  12106. ConvertProgram;
  12107. CheckSource('TestClass_TObjectConstructorWithParams',
  12108. LinesToStr([ // statements
  12109. 'rtl.createClass(this,"TObject",null,function(){',
  12110. ' this.$init = function () {',
  12111. ' };',
  12112. ' this.$final = function () {',
  12113. ' };',
  12114. ' this.Create = function(Par){',
  12115. ' return this;',
  12116. ' };',
  12117. '});',
  12118. 'this.Obj = null;'
  12119. ]),
  12120. LinesToStr([ // $mod.$main
  12121. '$mod.Obj = $mod.TObject.$create("Create",[3]);'
  12122. ]));
  12123. end;
  12124. procedure TTestModule.TestClass_TObjectConstructorWithDefaultParam;
  12125. begin
  12126. StartProgram(false);
  12127. Add('type');
  12128. Add(' TObject = class');
  12129. Add(' public');
  12130. Add(' constructor Create;');
  12131. Add(' end;');
  12132. Add(' TTest = class(TObject)');
  12133. Add(' public');
  12134. Add(' constructor Create(const Par: longint = 1);');
  12135. Add(' end;');
  12136. Add('constructor tobject.create;');
  12137. Add('begin end;');
  12138. Add('constructor ttest.create(const par: longint);');
  12139. Add('begin end;');
  12140. Add('var t: ttest;');
  12141. Add('begin');
  12142. Add(' t:=ttest.create;');
  12143. Add(' t:=ttest.create(2);');
  12144. ConvertProgram;
  12145. CheckSource('TestClass_TObjectConstructorWithDefaultParam',
  12146. LinesToStr([ // statements
  12147. 'rtl.createClass(this,"TObject",null,function(){',
  12148. ' this.$init = function () {',
  12149. ' };',
  12150. ' this.$final = function () {',
  12151. ' };',
  12152. ' this.Create = function(){',
  12153. ' return this;',
  12154. ' };',
  12155. '});',
  12156. 'rtl.createClass(this, "TTest", this.TObject, function () {',
  12157. ' this.Create$1 = function (Par) {',
  12158. ' return this;',
  12159. ' };',
  12160. '});',
  12161. 'this.t = null;'
  12162. ]),
  12163. LinesToStr([ // $mod.$main
  12164. '$mod.t = $mod.TTest.$create("Create$1", [1]);',
  12165. '$mod.t = $mod.TTest.$create("Create$1", [2]);'
  12166. ]));
  12167. end;
  12168. procedure TTestModule.TestClass_Var;
  12169. begin
  12170. StartProgram(false);
  12171. Add([
  12172. 'type',
  12173. ' TObject = class',
  12174. ' public',
  12175. ' vI: longint;',
  12176. ' constructor Create(Par: longint);',
  12177. ' end;',
  12178. 'constructor tobject.create(par: longint);',
  12179. 'begin',
  12180. ' vi:=par+3',
  12181. 'end;',
  12182. 'var Obj: tobject;',
  12183. 'begin',
  12184. ' obj:=tobject.create(4);',
  12185. ' obj.vi:=obj.VI+5;']);
  12186. ConvertProgram;
  12187. CheckSource('TestClass_Var',
  12188. LinesToStr([ // statements
  12189. 'rtl.createClass(this,"TObject",null,function(){',
  12190. ' this.$init = function () {',
  12191. ' this.vI = 0;',
  12192. ' };',
  12193. ' this.$final = function () {',
  12194. ' };',
  12195. ' this.Create = function(Par){',
  12196. ' this.vI = Par+3;',
  12197. ' return this;',
  12198. ' };',
  12199. '});',
  12200. 'this.Obj = null;'
  12201. ]),
  12202. LinesToStr([ // $mod.$main
  12203. '$mod.Obj = $mod.TObject.$create("Create",[4]);',
  12204. '$mod.Obj.vI = $mod.Obj.vI + 5;'
  12205. ]));
  12206. end;
  12207. procedure TTestModule.TestClass_Method;
  12208. begin
  12209. StartProgram(false);
  12210. Add('type');
  12211. Add(' TObject = class');
  12212. Add(' public');
  12213. Add(' vI: longint;');
  12214. Add(' Sub: TObject;');
  12215. Add(' constructor Create;');
  12216. Add(' function GetIt(Par: longint): tobject;');
  12217. Add(' end;');
  12218. Add('constructor tobject.create; begin end;');
  12219. Add('function tobject.getit(par: longint): tobject;');
  12220. Add('begin');
  12221. Add(' Self.vi:=par+3;');
  12222. Add(' Result:=self.sub;');
  12223. Add('end;');
  12224. Add('var Obj: tobject;');
  12225. Add('begin');
  12226. Add(' obj:=tobject.create;');
  12227. Add(' obj.getit(4);');
  12228. Add(' obj.sub.sub:=nil;');
  12229. Add(' obj.sub.getit(5);');
  12230. Add(' obj.sub.getit(6).SUB:=nil;');
  12231. Add(' obj.sub.getit(7).GETIT(8);');
  12232. Add(' obj.sub.getit(9).SuB.getit(10);');
  12233. ConvertProgram;
  12234. CheckSource('TestClass_Method',
  12235. LinesToStr([ // statements
  12236. 'rtl.createClass(this,"TObject",null,function(){',
  12237. ' this.$init = function () {',
  12238. ' this.vI = 0;',
  12239. ' this.Sub = null;',
  12240. ' };',
  12241. ' this.$final = function () {',
  12242. ' this.Sub = undefined;',
  12243. ' };',
  12244. ' this.Create = function(){',
  12245. ' return this;',
  12246. ' };',
  12247. ' this.GetIt = function(Par){',
  12248. ' var Result = null;',
  12249. ' this.vI = Par + 3;',
  12250. ' Result = this.Sub;',
  12251. ' return Result;',
  12252. ' };',
  12253. '});',
  12254. 'this.Obj = null;'
  12255. ]),
  12256. LinesToStr([ // $mod.$main
  12257. '$mod.Obj = $mod.TObject.$create("Create");',
  12258. '$mod.Obj.GetIt(4);',
  12259. '$mod.Obj.Sub.Sub=null;',
  12260. '$mod.Obj.Sub.GetIt(5);',
  12261. '$mod.Obj.Sub.GetIt(6).Sub=null;',
  12262. '$mod.Obj.Sub.GetIt(7).GetIt(8);',
  12263. '$mod.Obj.Sub.GetIt(9).Sub.GetIt(10);'
  12264. ]));
  12265. end;
  12266. procedure TTestModule.TestClass_Implementation;
  12267. begin
  12268. StartUnit(false);
  12269. Add([
  12270. 'interface',
  12271. 'type',
  12272. ' TObject = class',
  12273. ' constructor Create;',
  12274. ' end;',
  12275. 'implementation',
  12276. 'type',
  12277. ' TIntClass = class',
  12278. ' constructor Create; reintroduce;',
  12279. ' class procedure DoGlob;',
  12280. ' end;',
  12281. 'constructor tintclass.create;',
  12282. 'begin',
  12283. ' inherited;',
  12284. ' inherited create;',
  12285. ' doglob;',
  12286. 'end;',
  12287. 'class procedure tintclass.doglob;',
  12288. 'begin',
  12289. 'end;',
  12290. 'constructor tobject.create;',
  12291. 'var',
  12292. ' iC: tintclass;',
  12293. 'begin',
  12294. ' ic:=tintclass.create;',
  12295. ' tintclass.doglob;',
  12296. ' ic.doglob;',
  12297. 'end;',
  12298. 'initialization',
  12299. ' tintclass.doglob;',
  12300. '']);
  12301. ConvertUnit;
  12302. CheckSource('TestClass_Implementation',
  12303. LinesToStr([ // statements
  12304. 'var $impl = $mod.$impl;',
  12305. 'rtl.createClass(this, "TObject", null, function () {',
  12306. ' this.$init = function () {',
  12307. ' };',
  12308. ' this.$final = function () {',
  12309. ' };',
  12310. ' this.Create = function () {',
  12311. ' var iC = null;',
  12312. ' iC = $impl.TIntClass.$create("Create$1");',
  12313. ' $impl.TIntClass.DoGlob();',
  12314. ' iC.$class.DoGlob();',
  12315. ' return this;',
  12316. ' };',
  12317. '});',
  12318. '']),
  12319. LinesToStr([ // $mod.$main
  12320. '$impl.TIntClass.DoGlob();',
  12321. '']),
  12322. LinesToStr([
  12323. 'rtl.createClass($impl, "TIntClass", $mod.TObject, function () {',
  12324. ' this.Create$1 = function () {',
  12325. ' $mod.TObject.Create.call(this);',
  12326. ' $mod.TObject.Create.call(this);',
  12327. ' this.$class.DoGlob();',
  12328. ' return this;',
  12329. ' };',
  12330. ' this.DoGlob = function () {',
  12331. ' };',
  12332. '});',
  12333. '']));
  12334. end;
  12335. procedure TTestModule.TestClass_Inheritance;
  12336. begin
  12337. StartProgram(false);
  12338. Add('type');
  12339. Add(' TObject = class');
  12340. Add(' public');
  12341. Add(' constructor Create;');
  12342. Add(' end;');
  12343. Add(' TClassA = class');
  12344. Add(' end;');
  12345. Add(' TClassB = class(TObject)');
  12346. Add(' procedure ProcB;');
  12347. Add(' end;');
  12348. Add('constructor tobject.create; begin end;');
  12349. Add('procedure tclassb.procb; begin end;');
  12350. Add('var');
  12351. Add(' oO: TObject;');
  12352. Add(' oA: TClassA;');
  12353. Add(' oB: TClassB;');
  12354. Add('begin');
  12355. Add(' oO:=tobject.Create;');
  12356. Add(' oA:=tclassa.Create;');
  12357. Add(' ob:=tclassb.Create;');
  12358. Add(' if oo is tclassa then ;');
  12359. Add(' ob:=oo as tclassb;');
  12360. Add(' (oo as tclassb).procb;');
  12361. ConvertProgram;
  12362. CheckSource('TestClass_Inheritance',
  12363. LinesToStr([ // statements
  12364. 'rtl.createClass(this,"TObject",null,function(){',
  12365. ' this.$init = function () {',
  12366. ' };',
  12367. ' this.$final = function () {',
  12368. ' };',
  12369. ' this.Create = function () {',
  12370. ' return this;',
  12371. ' };',
  12372. '});',
  12373. 'rtl.createClass(this,"TClassA",this.TObject,function(){',
  12374. '});',
  12375. 'rtl.createClass(this,"TClassB",this.TObject,function(){',
  12376. ' this.ProcB = function () {',
  12377. ' };',
  12378. '});',
  12379. 'this.oO = null;',
  12380. 'this.oA = null;',
  12381. 'this.oB = null;'
  12382. ]),
  12383. LinesToStr([ // $mod.$main
  12384. '$mod.oO = $mod.TObject.$create("Create");',
  12385. '$mod.oA = $mod.TClassA.$create("Create");',
  12386. '$mod.oB = $mod.TClassB.$create("Create");',
  12387. 'if ($mod.TClassA.isPrototypeOf($mod.oO));',
  12388. '$mod.oB = rtl.as($mod.oO, $mod.TClassB);',
  12389. 'rtl.as($mod.oO, $mod.TClassB).ProcB();'
  12390. ]));
  12391. end;
  12392. procedure TTestModule.TestClass_TypeAlias;
  12393. begin
  12394. StartProgram(false);
  12395. Add([
  12396. '{$interfaces corba}',
  12397. 'type',
  12398. ' IObject = interface',
  12399. ' end;',
  12400. ' IBird = type IObject;',
  12401. ' TObject = class',
  12402. ' end;',
  12403. ' TBird = type TObject;',
  12404. 'var',
  12405. ' oObj: TObject;',
  12406. ' oBird: TBird;',
  12407. ' IntfObj: IObject;',
  12408. ' IntfBird: IBird;',
  12409. 'begin',
  12410. ' oObj:=oBird;',
  12411. '']);
  12412. ConvertProgram;
  12413. CheckSource('TestClass_TypeAlias',
  12414. LinesToStr([ // statements
  12415. 'rtl.createInterface(this, "IObject", "{B92D5841-6F2A-306A-8000-000000000000}", [], null);',
  12416. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-387B-AE88-F10981585074}", [], this.IObject);',
  12417. 'rtl.createClass(this, "TObject", null, function () {',
  12418. ' this.$init = function () {',
  12419. ' };',
  12420. ' this.$final = function () {',
  12421. ' };',
  12422. '});',
  12423. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  12424. '});',
  12425. 'this.oObj = null;',
  12426. 'this.oBird = null;',
  12427. 'this.IntfObj = null;',
  12428. 'this.IntfBird = null;',
  12429. '']),
  12430. LinesToStr([ // $mod.$main
  12431. '$mod.oObj = $mod.oBird;',
  12432. '']));
  12433. end;
  12434. procedure TTestModule.TestClass_AbstractMethod;
  12435. begin
  12436. StartProgram(false);
  12437. Add('type');
  12438. Add(' TObject = class');
  12439. Add(' public');
  12440. Add(' procedure DoIt; virtual; abstract;');
  12441. Add(' end;');
  12442. Add('begin');
  12443. ConvertProgram;
  12444. CheckSource('TestClass_AbstractMethod',
  12445. LinesToStr([ // statements
  12446. 'rtl.createClass(this,"TObject",null,function(){',
  12447. ' this.$init = function () {',
  12448. ' };',
  12449. ' this.$final = function () {',
  12450. ' };',
  12451. '});'
  12452. ]),
  12453. LinesToStr([ // this.$main
  12454. ''
  12455. ]));
  12456. end;
  12457. procedure TTestModule.TestClass_CallInherited_ProcNoParams;
  12458. begin
  12459. StartProgram(false);
  12460. Add([
  12461. 'type',
  12462. ' TObject = class',
  12463. ' procedure DoAbstract; virtual; abstract;',
  12464. ' procedure DoVirtual; virtual;',
  12465. ' procedure DoIt;',
  12466. ' end;',
  12467. ' TA = class',
  12468. ' procedure doabstract; override;',
  12469. ' procedure dovirtual; override;',
  12470. ' procedure DoSome;',
  12471. ' end;',
  12472. 'procedure tobject.dovirtual;',
  12473. 'begin',
  12474. ' inherited; // call non existing ancestor -> ignore silently',
  12475. 'end;',
  12476. 'procedure tobject.doit;',
  12477. 'begin',
  12478. 'end;',
  12479. 'procedure ta.doabstract;',
  12480. 'begin',
  12481. ' inherited dovirtual; // call TObject.DoVirtual',
  12482. 'end;',
  12483. 'procedure ta.dovirtual;',
  12484. 'begin',
  12485. ' inherited; // call TObject.DoVirtual',
  12486. ' inherited dovirtual; // call TObject.DoVirtual',
  12487. ' inherited dovirtual(); // call TObject.DoVirtual',
  12488. ' doit;',
  12489. ' doit();',
  12490. 'end;',
  12491. 'procedure ta.dosome;',
  12492. 'begin',
  12493. ' inherited; // call non existing ancestor method -> silently ignore',
  12494. 'end;',
  12495. 'begin']);
  12496. ConvertProgram;
  12497. CheckSource('TestClass_CallInherited_ProcNoParams',
  12498. LinesToStr([ // statements
  12499. 'rtl.createClass(this,"TObject",null,function(){',
  12500. ' this.$init = function () {',
  12501. ' };',
  12502. ' this.$final = function () {',
  12503. ' };',
  12504. ' this.DoVirtual = function () {',
  12505. ' };',
  12506. ' this.DoIt = function () {',
  12507. ' };',
  12508. '});',
  12509. 'rtl.createClass(this, "TA", this.TObject, function () {',
  12510. ' this.DoAbstract = function () {',
  12511. ' $mod.TObject.DoVirtual.call(this);',
  12512. ' };',
  12513. ' this.DoVirtual = function () {',
  12514. ' $mod.TObject.DoVirtual.call(this);',
  12515. ' $mod.TObject.DoVirtual.call(this);',
  12516. ' $mod.TObject.DoVirtual.call(this);',
  12517. ' this.DoIt();',
  12518. ' this.DoIt();',
  12519. ' };',
  12520. ' this.DoSome = function () {',
  12521. ' };',
  12522. '});'
  12523. ]),
  12524. LinesToStr([ // this.$main
  12525. ''
  12526. ]));
  12527. end;
  12528. procedure TTestModule.TestClass_CallInherited_WithParams;
  12529. begin
  12530. StartProgram(false);
  12531. Add([
  12532. 'type',
  12533. ' TObject = class',
  12534. ' procedure DoAbstract(pA: longint; pB: longint = 0); virtual; abstract;',
  12535. ' procedure DoVirtual(pA: longint; pB: longint = 0); virtual;',
  12536. ' procedure DoIt(pA: longint; pB: longint = 0);',
  12537. ' procedure DoIt2(pA: longint = 1; pB: longint = 2);',
  12538. ' function GetIt(pA: longint = 1; pB: longint = 2): longint;',
  12539. ' end;',
  12540. ' TClassA = class',
  12541. ' procedure DoAbstract(pA: longint; pB: longint = 0); override;',
  12542. ' procedure DoVirtual(pA: longint; pB: longint = 0); override;',
  12543. ' function GetIt(pA: longint = 1; pB: longint = 2): longint;',
  12544. ' end;',
  12545. 'procedure tobject.dovirtual(pa: longint; pb: longint = 0);',
  12546. 'begin',
  12547. 'end;',
  12548. 'procedure tobject.doit(pa: longint; pb: longint = 0);',
  12549. 'begin',
  12550. 'end;',
  12551. 'procedure tobject.doit2(pa: longint; pb: longint = 0);',
  12552. 'begin',
  12553. 'end;',
  12554. 'function tobject.getit(pa: longint; pb: longint = 0): longint;',
  12555. 'begin',
  12556. 'end;',
  12557. 'procedure tclassa.doabstract(pa: longint; pb: longint = 0);',
  12558. 'begin',
  12559. ' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)',
  12560. ' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)',
  12561. 'end;',
  12562. 'procedure tclassa.dovirtual(pa: longint; pb: longint = 0);',
  12563. 'begin',
  12564. ' inherited; // call TObject.DoVirtual(pA,pB)',
  12565. ' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)',
  12566. ' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)',
  12567. ' doit(pa,pb);',
  12568. ' doit(pa);',
  12569. ' doit2(pa);',
  12570. ' doit2;',
  12571. 'end;',
  12572. 'function tclassa.getit(pa: longint; pb: longint = 0): longint;',
  12573. 'begin',
  12574. ' pa:=inherited;',
  12575. 'end;',
  12576. 'begin']);
  12577. ConvertProgram;
  12578. CheckSource('TestClass_CallInherited_WithParams',
  12579. LinesToStr([ // statements
  12580. 'rtl.createClass(this,"TObject",null,function(){',
  12581. ' this.$init = function () {',
  12582. ' };',
  12583. ' this.$final = function () {',
  12584. ' };',
  12585. ' this.DoVirtual = function (pA,pB) {',
  12586. ' };',
  12587. ' this.DoIt = function (pA,pB) {',
  12588. ' };',
  12589. ' this.DoIt2 = function (pA,pB) {',
  12590. ' };',
  12591. ' this.GetIt = function (pA, pB) {',
  12592. ' var Result = 0;',
  12593. ' return Result;',
  12594. ' };',
  12595. '});',
  12596. 'rtl.createClass(this, "TClassA", this.TObject, function () {',
  12597. ' this.DoAbstract = function (pA,pB) {',
  12598. ' $mod.TObject.DoVirtual.call(this,pA,pB);',
  12599. ' $mod.TObject.DoVirtual.call(this,pA,0);',
  12600. ' };',
  12601. ' this.DoVirtual = function (pA,pB) {',
  12602. ' $mod.TObject.DoVirtual.apply(this, arguments);',
  12603. ' $mod.TObject.DoVirtual.call(this,pA,pB);',
  12604. ' $mod.TObject.DoVirtual.call(this,pA,0);',
  12605. ' this.DoIt(pA,pB);',
  12606. ' this.DoIt(pA,0);',
  12607. ' this.DoIt2(pA,2);',
  12608. ' this.DoIt2(1,2);',
  12609. ' };',
  12610. ' this.GetIt$1 = function (pA, pB) {',
  12611. ' var Result = 0;',
  12612. ' pA = $mod.TObject.GetIt.apply(this, arguments);',
  12613. ' return Result;',
  12614. ' };',
  12615. '});'
  12616. ]),
  12617. LinesToStr([ // this.$main
  12618. ''
  12619. ]));
  12620. end;
  12621. procedure TTestModule.TestClasS_CallInheritedConstructor;
  12622. begin
  12623. StartProgram(false);
  12624. Add('type');
  12625. Add(' TObject = class');
  12626. Add(' constructor Create; virtual;');
  12627. Add(' constructor CreateWithB(b: boolean);');
  12628. Add(' end;');
  12629. Add(' TA = class');
  12630. Add(' constructor Create; override;');
  12631. Add(' constructor CreateWithC(c: char);');
  12632. Add(' procedure DoIt;');
  12633. Add(' class function DoSome: TObject;');
  12634. Add(' end;');
  12635. Add('constructor tobject.create;');
  12636. Add('begin');
  12637. Add(' inherited; // call non existing ancestor -> ignore silently');
  12638. Add('end;');
  12639. Add('constructor tobject.createwithb(b: boolean);');
  12640. Add('begin');
  12641. Add(' inherited; // call non existing ancestor -> ignore silently');
  12642. Add(' create; // normal call');
  12643. Add('end;');
  12644. Add('constructor ta.create;');
  12645. Add('begin');
  12646. Add(' inherited; // normal call TObject.Create');
  12647. Add(' inherited create; // normal call TObject.Create');
  12648. Add(' inherited createwithb(false); // normal call TObject.CreateWithB');
  12649. Add('end;');
  12650. Add('constructor ta.createwithc(c: char);');
  12651. Add('begin');
  12652. Add(' inherited create; // call TObject.Create');
  12653. Add(' inherited createwithb(true); // call TObject.CreateWithB');
  12654. Add(' doit;');
  12655. Add(' doit();');
  12656. Add(' dosome;');
  12657. Add('end;');
  12658. Add('procedure ta.doit;');
  12659. Add('begin');
  12660. Add(' create; // normal call');
  12661. Add(' createwithb(false); // normal call');
  12662. Add(' createwithc(''c''); // normal call');
  12663. Add('end;');
  12664. Add('class function ta.dosome: TObject;');
  12665. Add('begin');
  12666. Add(' Result:=create; // constructor');
  12667. Add(' Result:=createwithb(true); // constructor');
  12668. Add(' Result:=createwithc(''c''); // constructor');
  12669. Add('end;');
  12670. Add('begin');
  12671. ConvertProgram;
  12672. CheckSource('TestClass_CallInheritedConstructor',
  12673. LinesToStr([ // statements
  12674. 'rtl.createClass(this,"TObject",null,function(){',
  12675. ' this.$init = function () {',
  12676. ' };',
  12677. ' this.$final = function () {',
  12678. ' };',
  12679. ' this.Create = function () {',
  12680. ' return this;',
  12681. ' };',
  12682. ' this.CreateWithB = function (b) {',
  12683. ' this.Create();',
  12684. ' return this;',
  12685. ' };',
  12686. '});',
  12687. 'rtl.createClass(this, "TA", this.TObject, function () {',
  12688. ' this.Create = function () {',
  12689. ' $mod.TObject.Create.call(this);',
  12690. ' $mod.TObject.Create.call(this);',
  12691. ' $mod.TObject.CreateWithB.call(this, false);',
  12692. ' return this;',
  12693. ' };',
  12694. ' this.CreateWithC = function (c) {',
  12695. ' $mod.TObject.Create.call(this);',
  12696. ' $mod.TObject.CreateWithB.call(this, true);',
  12697. ' this.DoIt();',
  12698. ' this.DoIt();',
  12699. ' this.$class.DoSome();',
  12700. ' return this;',
  12701. ' };',
  12702. ' this.DoIt = function () {',
  12703. ' this.Create();',
  12704. ' this.CreateWithB(false);',
  12705. ' this.CreateWithC("c");',
  12706. ' };',
  12707. ' this.DoSome = function () {',
  12708. ' var Result = null;',
  12709. ' Result = this.$create("Create");',
  12710. ' Result = this.$create("CreateWithB", [true]);',
  12711. ' Result = this.$create("CreateWithC", ["c"]);',
  12712. ' return Result;',
  12713. ' };',
  12714. '});'
  12715. ]),
  12716. LinesToStr([ // this.$main
  12717. ''
  12718. ]));
  12719. end;
  12720. procedure TTestModule.TestClass_ClassVar_Assign;
  12721. begin
  12722. StartProgram(false);
  12723. Add([
  12724. 'type',
  12725. ' TObject = class',
  12726. ' public',
  12727. ' class var vI: longint;',
  12728. ' class var Sub: TObject;',
  12729. ' constructor Create;',
  12730. ' class function GetIt(var Par: longint): tobject;',
  12731. ' end;',
  12732. 'constructor tobject.create;',
  12733. 'begin',
  12734. ' vi:=vi+1;',
  12735. ' Self.vi:=Self.vi+1;',
  12736. ' inc(vi);',
  12737. 'end;',
  12738. 'class function tobject.getit(var par: longint): tobject;',
  12739. 'begin',
  12740. ' vi:=vi+3;',
  12741. ' Self.vi:=Self.vi+4;',
  12742. ' inc(vi);',
  12743. ' Result:=self.sub;',
  12744. ' GetIt(vi);',
  12745. 'end;',
  12746. 'var Obj: tobject;',
  12747. 'begin',
  12748. ' obj:=tobject.create;',
  12749. ' tobject.vi:=3;',
  12750. ' if tobject.vi=4 then ;',
  12751. ' tobject.sub:=nil;',
  12752. ' obj.sub:=nil;',
  12753. ' obj.sub.sub:=nil;']);
  12754. ConvertProgram;
  12755. CheckSource('TestClass_ClassVar_Assign',
  12756. LinesToStr([ // statements
  12757. 'rtl.createClass(this,"TObject",null,function(){',
  12758. ' this.vI = 0;',
  12759. ' this.Sub = null;',
  12760. ' this.$init = function () {',
  12761. ' };',
  12762. ' this.$final = function () {',
  12763. ' };',
  12764. ' this.Create = function(){',
  12765. ' $mod.TObject.vI = this.vI+1;',
  12766. ' $mod.TObject.vI = this.vI+1;',
  12767. ' $mod.TObject.vI += 1;',
  12768. ' return this;',
  12769. ' };',
  12770. ' this.GetIt = function(Par){',
  12771. ' var Result = null;',
  12772. ' $mod.TObject.vI = this.vI + 3;',
  12773. ' $mod.TObject.vI = this.vI + 4;',
  12774. ' $mod.TObject.vI += 1;',
  12775. ' Result = this.Sub;',
  12776. ' this.GetIt({',
  12777. ' p: $mod.TObject,',
  12778. ' get: function () {',
  12779. ' return this.p.vI;',
  12780. ' },',
  12781. ' set: function (v) {',
  12782. ' this.p.vI = v;',
  12783. ' }',
  12784. ' });',
  12785. ' return Result;',
  12786. ' };',
  12787. '});',
  12788. 'this.Obj = null;'
  12789. ]),
  12790. LinesToStr([ // $mod.$main
  12791. '$mod.Obj = $mod.TObject.$create("Create");',
  12792. '$mod.TObject.vI = 3;',
  12793. 'if ($mod.TObject.vI === 4);',
  12794. '$mod.TObject.Sub=null;',
  12795. '$mod.TObject.Sub=null;',
  12796. '$mod.TObject.Sub=null;',
  12797. '']));
  12798. end;
  12799. procedure TTestModule.TestClass_CallClassMethod;
  12800. begin
  12801. StartProgram(false);
  12802. Add('type');
  12803. Add(' TObject = class');
  12804. Add(' public');
  12805. Add(' class var vI: longint;');
  12806. Add(' class var Sub: TObject;');
  12807. Add(' constructor Create;');
  12808. Add(' function GetMore(Par: longint): longint;');
  12809. Add(' class function GetIt(Par: longint): tobject;');
  12810. Add(' end;');
  12811. Add('constructor tobject.create;');
  12812. Add('begin');
  12813. Add(' sub:=getit(3);');
  12814. Add(' vi:=getmore(4);');
  12815. Add(' sub:=Self.getit(5);');
  12816. Add(' vi:=Self.getmore(6);');
  12817. Add('end;');
  12818. Add('function tobject.getmore(par: longint): longint;');
  12819. Add('begin');
  12820. Add(' sub:=getit(11);');
  12821. Add(' vi:=getmore(12);');
  12822. Add(' sub:=self.getit(13);');
  12823. Add(' vi:=self.getmore(14);');
  12824. Add('end;');
  12825. Add('class function tobject.getit(par: longint): tobject;');
  12826. Add('begin');
  12827. Add(' sub:=getit(21);');
  12828. Add(' vi:=sub.getmore(22);');
  12829. Add(' sub:=self.getit(23);');
  12830. Add(' vi:=self.sub.getmore(24);');
  12831. Add('end;');
  12832. Add('var Obj: tobject;');
  12833. Add('begin');
  12834. Add(' obj:=tobject.create;');
  12835. Add(' tobject.getit(5);');
  12836. Add(' obj.getit(6);');
  12837. Add(' obj.sub.getit(7);');
  12838. Add(' obj.sub.getit(8).SUB:=nil;');
  12839. Add(' obj.sub.getit(9).GETIT(10);');
  12840. Add(' obj.sub.getit(11).SuB.getit(12);');
  12841. ConvertProgram;
  12842. CheckSource('TestClass_CallClassMethod',
  12843. LinesToStr([ // statements
  12844. 'rtl.createClass(this,"TObject",null,function(){',
  12845. ' this.vI = 0;',
  12846. ' this.Sub = null;',
  12847. ' this.$init = function () {',
  12848. ' };',
  12849. ' this.$final = function () {',
  12850. ' };',
  12851. ' this.Create = function(){',
  12852. ' $mod.TObject.Sub = this.$class.GetIt(3);',
  12853. ' $mod.TObject.vI = this.GetMore(4);',
  12854. ' $mod.TObject.Sub = this.$class.GetIt(5);',
  12855. ' $mod.TObject.vI = this.GetMore(6);',
  12856. ' return this;',
  12857. ' };',
  12858. ' this.GetMore = function(Par){',
  12859. ' var Result = 0;',
  12860. ' $mod.TObject.Sub = this.$class.GetIt(11);',
  12861. ' $mod.TObject.vI = this.GetMore(12);',
  12862. ' $mod.TObject.Sub = this.$class.GetIt(13);',
  12863. ' $mod.TObject.vI = this.GetMore(14);',
  12864. ' return Result;',
  12865. ' };',
  12866. ' this.GetIt = function(Par){',
  12867. ' var Result = null;',
  12868. ' $mod.TObject.Sub = this.GetIt(21);',
  12869. ' $mod.TObject.vI = this.Sub.GetMore(22);',
  12870. ' $mod.TObject.Sub = this.GetIt(23);',
  12871. ' $mod.TObject.vI = this.Sub.GetMore(24);',
  12872. ' return Result;',
  12873. ' };',
  12874. '});',
  12875. 'this.Obj = null;'
  12876. ]),
  12877. LinesToStr([ // $mod.$main
  12878. '$mod.Obj = $mod.TObject.$create("Create");',
  12879. '$mod.TObject.GetIt(5);',
  12880. '$mod.Obj.$class.GetIt(6);',
  12881. '$mod.Obj.Sub.$class.GetIt(7);',
  12882. '$mod.TObject.Sub=null;',
  12883. '$mod.Obj.Sub.$class.GetIt(9).$class.GetIt(10);',
  12884. '$mod.Obj.Sub.$class.GetIt(11).Sub.$class.GetIt(12);',
  12885. '']));
  12886. end;
  12887. procedure TTestModule.TestClass_Property;
  12888. begin
  12889. StartProgram(false);
  12890. Add('type');
  12891. Add(' TObject = class');
  12892. Add(' Fx: longint;');
  12893. Add(' Fy: longint;');
  12894. Add(' function GetInt: longint;');
  12895. Add(' procedure SetInt(Value: longint);');
  12896. Add(' procedure DoIt;');
  12897. Add(' property IntA: longint read Fx write Fy;');
  12898. Add(' property IntB: longint read GetInt write SetInt;');
  12899. Add(' end;');
  12900. Add('function tobject.getint: longint;');
  12901. Add('begin');
  12902. Add(' result:=fx;');
  12903. Add('end;');
  12904. Add('procedure tobject.setint(value: longint);');
  12905. Add('begin');
  12906. Add(' if value=fy then exit;');
  12907. Add(' fy:=value;');
  12908. Add('end;');
  12909. Add('procedure tobject.doit;');
  12910. Add('begin');
  12911. Add(' IntA:=IntA+1;');
  12912. Add(' Self.IntA:=Self.IntA+1;');
  12913. Add(' IntB:=IntB+1;');
  12914. Add(' Self.IntB:=Self.IntB+1;');
  12915. Add('end;');
  12916. Add('var Obj: tobject;');
  12917. Add('begin');
  12918. Add(' obj.inta:=obj.inta+1;');
  12919. Add(' if obj.intb=2 then;');
  12920. Add(' obj.intb:=obj.intb+2;');
  12921. Add(' obj.setint(obj.inta);');
  12922. ConvertProgram;
  12923. CheckSource('TestClass_Property',
  12924. LinesToStr([ // statements
  12925. 'rtl.createClass(this, "TObject", null, function () {',
  12926. ' this.$init = function () {',
  12927. ' this.Fx = 0;',
  12928. ' this.Fy = 0;',
  12929. ' };',
  12930. ' this.$final = function () {',
  12931. ' };',
  12932. ' this.GetInt = function () {',
  12933. ' var Result = 0;',
  12934. ' Result = this.Fx;',
  12935. ' return Result;',
  12936. ' };',
  12937. ' this.SetInt = function (Value) {',
  12938. ' if (Value === this.Fy) return;',
  12939. ' this.Fy = Value;',
  12940. ' };',
  12941. ' this.DoIt = function () {',
  12942. ' this.Fy = this.Fx + 1;',
  12943. ' this.Fy = this.Fx + 1;',
  12944. ' this.SetInt(this.GetInt() + 1);',
  12945. ' this.SetInt(this.GetInt() + 1);',
  12946. ' };',
  12947. '});',
  12948. 'this.Obj = null;'
  12949. ]),
  12950. LinesToStr([ // $mod.$main
  12951. '$mod.Obj.Fy = $mod.Obj.Fx + 1;',
  12952. 'if ($mod.Obj.GetInt() === 2);',
  12953. '$mod.Obj.SetInt($mod.Obj.GetInt() + 2);',
  12954. '$mod.Obj.SetInt($mod.Obj.Fx);'
  12955. ]));
  12956. end;
  12957. procedure TTestModule.TestClass_Property_ClassMethod;
  12958. begin
  12959. StartProgram(false);
  12960. Add([
  12961. 'type',
  12962. ' TObject = class',
  12963. ' class var Fx: longint;',
  12964. ' class var Fy: longint;',
  12965. ' class function GetInt: longint;',
  12966. ' class procedure SetInt(Value: longint);',
  12967. ' end;',
  12968. ' TBird = class',
  12969. ' class procedure DoIt;',
  12970. ' class property IntA: longint read Fx write Fy;',
  12971. ' class property IntB: longint read GetInt write SetInt;',
  12972. ' end;',
  12973. 'class function tobject.getint: longint;',
  12974. 'begin',
  12975. ' result:=fx;',
  12976. 'end;',
  12977. 'class procedure tobject.setint(value: longint);',
  12978. 'begin',
  12979. 'end;',
  12980. 'class procedure tbird.doit;',
  12981. 'begin',
  12982. ' FX:=3;',
  12983. ' IntA:=IntA+1;',
  12984. ' Self.IntA:=Self.IntA+1;',
  12985. ' IntB:=IntB+1;',
  12986. ' Self.IntB:=Self.IntB+1;',
  12987. ' with Self do begin',
  12988. ' FX:=11;',
  12989. ' IntA:=IntA+12;',
  12990. ' IntB:=IntB+13;',
  12991. ' end;',
  12992. 'end;',
  12993. 'var Obj: tbird;',
  12994. 'begin',
  12995. ' tbird.fx:=tbird.fx+1;',
  12996. ' tbird.inta:=tbird.inta+1;',
  12997. ' if tbird.intb=2 then;',
  12998. ' tbird.intb:=tbird.intb+2;',
  12999. ' tbird.setint(tbird.inta);',
  13000. ' obj.inta:=obj.inta+1;',
  13001. ' if obj.intb=2 then;',
  13002. ' obj.intb:=obj.intb+2;',
  13003. ' obj.setint(obj.inta);',
  13004. ' with Tbird do begin',
  13005. ' FX:=FY+1;',
  13006. ' inta:=inta+2;',
  13007. ' intb:=intb+3;',
  13008. ' end;',
  13009. ' with Obj do begin',
  13010. ' FX:=FY+1;',
  13011. ' inta:=inta+2;',
  13012. ' intb:=intb+3;',
  13013. ' end;',
  13014. '']);
  13015. ConvertProgram;
  13016. CheckSource('TestClass_Property_ClassMethod',
  13017. LinesToStr([ // statements
  13018. 'rtl.createClass(this, "TObject", null, function () {',
  13019. ' this.Fx = 0;',
  13020. ' this.Fy = 0;',
  13021. ' this.$init = function () {',
  13022. ' };',
  13023. ' this.$final = function () {',
  13024. ' };',
  13025. ' this.GetInt = function () {',
  13026. ' var Result = 0;',
  13027. ' Result = this.Fx;',
  13028. ' return Result;',
  13029. ' };',
  13030. ' this.SetInt = function (Value) {',
  13031. ' };',
  13032. '});',
  13033. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  13034. ' this.DoIt = function () {',
  13035. ' $mod.TObject.Fx = 3;',
  13036. ' $mod.TObject.Fy = this.Fx + 1;',
  13037. ' $mod.TObject.Fy = this.Fx + 1;',
  13038. ' this.SetInt(this.GetInt() + 1);',
  13039. ' this.SetInt(this.GetInt() + 1);',
  13040. ' $mod.TObject.Fx = 11;',
  13041. ' $mod.TObject.Fy = this.Fx + 12;',
  13042. ' this.SetInt(this.GetInt() + 13);',
  13043. ' };',
  13044. '});',
  13045. 'this.Obj = null;'
  13046. ]),
  13047. LinesToStr([ // $mod.$main
  13048. '$mod.TObject.Fx = $mod.TBird.Fx + 1;',
  13049. '$mod.TObject.Fy = $mod.TBird.Fx + 1;',
  13050. 'if ($mod.TBird.GetInt() === 2);',
  13051. '$mod.TBird.SetInt($mod.TBird.GetInt() + 2);',
  13052. '$mod.TBird.SetInt($mod.TBird.Fx);',
  13053. '$mod.TObject.Fy = $mod.Obj.Fx + 1;',
  13054. 'if ($mod.Obj.$class.GetInt() === 2);',
  13055. '$mod.Obj.$class.SetInt($mod.Obj.$class.GetInt() + 2);',
  13056. '$mod.Obj.$class.SetInt($mod.Obj.Fx);',
  13057. 'var $with = $mod.TBird;',
  13058. '$mod.TObject.Fx = $with.Fy + 1;',
  13059. '$mod.TObject.Fy = $with.Fx + 2;',
  13060. '$with.SetInt($with.GetInt() + 3);',
  13061. 'var $with1 = $mod.Obj;',
  13062. '$mod.TObject.Fx = $with1.Fy + 1;',
  13063. '$mod.TObject.Fy = $with1.Fx + 2;',
  13064. '$with1.$class.SetInt($with1.$class.GetInt() + 3);',
  13065. '']));
  13066. end;
  13067. procedure TTestModule.TestClass_Property_Indexed;
  13068. begin
  13069. StartProgram(false);
  13070. Add([
  13071. 'type',
  13072. ' TObject = class',
  13073. ' FItems: array of longint;',
  13074. ' function GetItems(Index: longint): longint;',
  13075. ' procedure SetItems(Index: longint; Value: longint);',
  13076. ' procedure DoIt;',
  13077. ' property Items[Index: longint]: longint read getitems write setitems;',
  13078. ' end;',
  13079. 'function tobject.getitems(index: longint): longint;',
  13080. 'begin',
  13081. ' Result:=fitems[index];',
  13082. 'end;',
  13083. 'procedure tobject.setitems(index: longint; value: longint);',
  13084. 'begin',
  13085. ' fitems[index]:=value;',
  13086. 'end;',
  13087. 'procedure tobject.doit;',
  13088. 'begin',
  13089. ' items[1]:=2;',
  13090. ' items[3]:=items[4];',
  13091. ' self.items[5]:=self.items[6];',
  13092. ' items[items[7]]:=items[items[8]];',
  13093. 'end;',
  13094. 'var Obj: tobject;',
  13095. 'begin',
  13096. ' obj.Items[11]:=obj.Items[12];',
  13097. '']);
  13098. ConvertProgram;
  13099. CheckSource('TestClass_Property_Indexed',
  13100. LinesToStr([ // statements
  13101. 'rtl.createClass(this, "TObject", null, function () {',
  13102. ' this.$init = function () {',
  13103. ' this.FItems = [];',
  13104. ' };',
  13105. ' this.$final = function () {',
  13106. ' this.FItems = undefined;',
  13107. ' };',
  13108. ' this.GetItems = function (Index) {',
  13109. ' var Result = 0;',
  13110. ' Result = this.FItems[Index];',
  13111. ' return Result;',
  13112. ' };',
  13113. ' this.SetItems = function (Index, Value) {',
  13114. ' this.FItems[Index] = Value;',
  13115. ' };',
  13116. ' this.DoIt = function () {',
  13117. ' this.SetItems(1, 2);',
  13118. ' this.SetItems(3,this.GetItems(4));',
  13119. ' this.SetItems(5,this.GetItems(6));',
  13120. ' this.SetItems(this.GetItems(7), this.GetItems(this.GetItems(8)));',
  13121. ' };',
  13122. '});',
  13123. 'this.Obj = null;'
  13124. ]),
  13125. LinesToStr([ // $mod.$main
  13126. '$mod.Obj.SetItems(11,$mod.Obj.GetItems(12));'
  13127. ]));
  13128. end;
  13129. procedure TTestModule.TestClass_Property_IndexSpec;
  13130. begin
  13131. StartProgram(false);
  13132. Add([
  13133. 'type',
  13134. ' TEnum = (red, blue);',
  13135. ' TObject = class',
  13136. ' function GetIntBool(Index: longint): boolean; virtual; abstract;',
  13137. ' procedure SetIntBool(Index: longint; b: boolean); virtual; abstract;',
  13138. ' function GetEnumBool(Index: TEnum): boolean; virtual; abstract;',
  13139. ' procedure SetEnumBool(Index: TEnum; b: boolean); virtual; abstract;',
  13140. ' function GetStrIntBool(A: String; I: longint): boolean; virtual; abstract;',
  13141. ' procedure SetStrIntBool(A: String; I: longint; b: boolean); virtual; abstract;',
  13142. ' property B1: boolean index 1 read GetIntBool write SetIntBool;',
  13143. ' property B2: boolean index TEnum.blue read GetEnumBool write SetEnumBool;',
  13144. ' property B3: boolean index ord(red) read GetIntBool write SetIntBool;',
  13145. ' property I1[A: String]: boolean index ord(blue) read GetStrIntBool write SetStrIntBool;',
  13146. ' end;',
  13147. 'procedure DoIt(b: boolean); begin end;',
  13148. 'var',
  13149. ' o: TObject;',
  13150. 'begin',
  13151. ' o.B1:=o.B1;',
  13152. ' o.B2:=o.B2;',
  13153. ' o.B3:=o.B3;',
  13154. ' o.I1[''a'']:=o.I1[''b''];',
  13155. ' doit(o.b1);',
  13156. ' doit(o.b2);',
  13157. ' doit(o.i1[''c'']);',
  13158. '']);
  13159. ConvertProgram;
  13160. CheckSource('TestClass_Property_IndexSpec',
  13161. LinesToStr([ // statements
  13162. 'this.TEnum = {',
  13163. ' "0": "red",',
  13164. ' red: 0,',
  13165. ' "1": "blue",',
  13166. ' blue: 1',
  13167. '};',
  13168. 'rtl.createClass(this, "TObject", null, function () {',
  13169. ' this.$init = function () {',
  13170. ' };',
  13171. ' this.$final = function () {',
  13172. ' };',
  13173. '});',
  13174. 'this.DoIt = function (b) {',
  13175. '};',
  13176. 'this.o = null;',
  13177. '']),
  13178. LinesToStr([ // $mod.$main
  13179. '$mod.o.SetIntBool(1, $mod.o.GetIntBool(1));',
  13180. '$mod.o.SetEnumBool($mod.TEnum.blue, $mod.o.GetEnumBool($mod.TEnum.blue));',
  13181. '$mod.o.SetIntBool(0, $mod.o.GetIntBool(0));',
  13182. '$mod.o.SetStrIntBool("a", 1, $mod.o.GetStrIntBool("b", 1));',
  13183. '$mod.DoIt($mod.o.GetIntBool(1));',
  13184. '$mod.DoIt($mod.o.GetEnumBool($mod.TEnum.blue));',
  13185. '$mod.DoIt($mod.o.GetStrIntBool("c", 1));',
  13186. '']));
  13187. end;
  13188. procedure TTestModule.TestClass_PropertyOfTypeArray;
  13189. begin
  13190. StartProgram(false);
  13191. Add('type');
  13192. Add(' TArray = array of longint;');
  13193. Add(' TObject = class');
  13194. Add(' FItems: TArray;');
  13195. Add(' function GetItems: tarray;');
  13196. Add(' procedure SetItems(Value: tarray);');
  13197. Add(' property Items: tarray read getitems write setitems;');
  13198. Add(' procedure SetNumbers(const Value: tarray);');
  13199. Add(' property Numbers: tarray write setnumbers;');
  13200. Add(' end;');
  13201. Add('function tobject.getitems: tarray;');
  13202. Add('begin');
  13203. Add(' Result:=fitems;');
  13204. Add('end;');
  13205. Add('procedure tobject.setitems(value: tarray);');
  13206. Add('begin');
  13207. Add(' fitems:=value;');
  13208. Add(' fitems:=nil;');
  13209. Add(' Items:=nil;');
  13210. Add(' Items:=Items;');
  13211. Add(' Items[1]:=2;');
  13212. Add(' fitems[3]:=Items[4];');
  13213. Add(' Items[5]:=Items[6];');
  13214. Add(' Self.Items[7]:=8;');
  13215. Add(' Self.Items[9]:=Self.Items[10];');
  13216. Add(' Items[Items[11]]:=Items[Items[12]];');
  13217. Add('end;');
  13218. Add('procedure tobject.SetNumbers(const Value: tarray);');
  13219. Add('begin;');
  13220. Add(' Numbers:=nil;');
  13221. Add(' Numbers:=Value;');
  13222. Add(' Self.Numbers:=Value;');
  13223. Add('end;');
  13224. Add('var Obj: tobject;');
  13225. Add('begin');
  13226. Add(' obj.items:=nil;');
  13227. Add(' obj.items:=obj.items;');
  13228. Add(' obj.items[11]:=obj.items[12];');
  13229. ConvertProgram;
  13230. CheckSource('TestClass_PropertyOfTypeArray',
  13231. LinesToStr([ // statements
  13232. 'rtl.createClass(this, "TObject", null, function () {',
  13233. ' this.$init = function () {',
  13234. ' this.FItems = [];',
  13235. ' };',
  13236. ' this.$final = function () {',
  13237. ' this.FItems = undefined;',
  13238. ' };',
  13239. ' this.GetItems = function () {',
  13240. ' var Result = [];',
  13241. ' Result = rtl.arrayRef(this.FItems);',
  13242. ' return Result;',
  13243. ' };',
  13244. ' this.SetItems = function (Value) {',
  13245. ' this.FItems = rtl.arrayRef(Value);',
  13246. ' this.FItems = [];',
  13247. ' this.SetItems([]);',
  13248. ' this.SetItems(rtl.arrayRef(this.GetItems()));',
  13249. ' this.GetItems()[1] = 2;',
  13250. ' this.FItems[3] = this.GetItems()[4];',
  13251. ' this.GetItems()[5] = this.GetItems()[6];',
  13252. ' this.GetItems()[7] = 8;',
  13253. ' this.GetItems()[9] = this.GetItems()[10];',
  13254. ' this.GetItems()[this.GetItems()[11]] = this.GetItems()[this.GetItems()[12]];',
  13255. ' };',
  13256. ' this.SetNumbers = function (Value) {',
  13257. ' this.SetNumbers([]);',
  13258. ' this.SetNumbers(Value);',
  13259. ' this.SetNumbers(Value);',
  13260. ' };',
  13261. '});',
  13262. 'this.Obj = null;'
  13263. ]),
  13264. LinesToStr([ // $mod.$main
  13265. '$mod.Obj.SetItems([]);',
  13266. '$mod.Obj.SetItems($mod.Obj.GetItems());',
  13267. '$mod.Obj.GetItems()[11] = $mod.Obj.GetItems()[12];'
  13268. ]));
  13269. end;
  13270. procedure TTestModule.TestClass_PropertyDefault;
  13271. begin
  13272. StartProgram(false);
  13273. Add([
  13274. 'type',
  13275. ' TArray = array of longint;',
  13276. ' TObject = class',
  13277. ' end;',
  13278. ' TBird = class',
  13279. ' FItems: TArray;',
  13280. ' function GetItems(Index: longint): longint;',
  13281. ' procedure SetItems(Index, Value: longint);',
  13282. ' property Items[Index: longint]: longint read getitems write setitems; default;',
  13283. ' end;',
  13284. 'function TBird.getitems(index: longint): longint;',
  13285. 'begin',
  13286. 'end;',
  13287. 'procedure TBird.setitems(index, value: longint);',
  13288. 'begin',
  13289. ' Self[1]:=2;',
  13290. ' Self[3]:=Self[index];',
  13291. ' Self[index]:=Self[Self[value]];',
  13292. ' Self[Self[4]]:=value;',
  13293. 'end;',
  13294. 'var',
  13295. ' Bird: TBird;',
  13296. ' Obj: TObject;',
  13297. 'begin',
  13298. ' bird[11]:=12;',
  13299. ' bird[13]:=bird[14];',
  13300. ' bird[Bird[15]]:=bird[Bird[15]];',
  13301. ' TBird(obj)[16]:=TBird(obj)[17];',
  13302. ' (obj as tbird)[18]:=19;',
  13303. '']);
  13304. ConvertProgram;
  13305. CheckSource('TestClass_PropertyDefault',
  13306. LinesToStr([ // statements
  13307. 'rtl.createClass(this, "TObject", null, function () {',
  13308. ' this.$init = function () {',
  13309. ' };',
  13310. ' this.$final = function () {',
  13311. ' };',
  13312. '});',
  13313. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  13314. ' this.$init = function () {',
  13315. ' $mod.TObject.$init.call(this);',
  13316. ' this.FItems = [];',
  13317. ' };',
  13318. ' this.$final = function () {',
  13319. ' this.FItems = undefined;',
  13320. ' $mod.TObject.$final.call(this);',
  13321. ' };',
  13322. ' this.GetItems = function (Index) {',
  13323. ' var Result = 0;',
  13324. ' return Result;',
  13325. ' };',
  13326. ' this.SetItems = function (Index, Value) {',
  13327. ' this.SetItems(1, 2);',
  13328. ' this.SetItems(3, this.GetItems(Index));',
  13329. ' this.SetItems(Index, this.GetItems(this.GetItems(Value)));',
  13330. ' this.SetItems(this.GetItems(4), Value);',
  13331. ' };',
  13332. '});',
  13333. 'this.Bird = null;',
  13334. 'this.Obj = null;',
  13335. '']),
  13336. LinesToStr([ // $mod.$main
  13337. '$mod.Bird.SetItems(11, 12);',
  13338. '$mod.Bird.SetItems(13, $mod.Bird.GetItems(14));',
  13339. '$mod.Bird.SetItems($mod.Bird.GetItems(15), $mod.Bird.GetItems($mod.Bird.GetItems(15)));',
  13340. '$mod.Obj.SetItems(16, $mod.Obj.GetItems(17));',
  13341. 'rtl.as($mod.Obj, $mod.TBird).SetItems(18, 19);',
  13342. '']));
  13343. end;
  13344. procedure TTestModule.TestClass_PropertyDefault_TypecastToOtherDefault;
  13345. begin
  13346. StartProgram(false);
  13347. Add([
  13348. 'type',
  13349. ' TObject = class end;',
  13350. ' TAlphaList = class',
  13351. ' function GetAlphas(Index: boolean): Pointer; virtual; abstract;',
  13352. ' procedure SetAlphas(Index: boolean; Value: Pointer); virtual; abstract;',
  13353. ' property Alphas[Index: boolean]: Pointer read getAlphas write setAlphas; default;',
  13354. ' end;',
  13355. ' TBetaList = class',
  13356. ' function GetBetas(Index: longint): Pointer; virtual; abstract;',
  13357. ' procedure SetBetas(Index: longint; Value: Pointer); virtual; abstract;',
  13358. ' property Betas[Index: longint]: Pointer read getBetas write setBetas; default;',
  13359. ' end;',
  13360. ' TBird = class',
  13361. ' procedure DoIt;',
  13362. ' end;',
  13363. 'procedure TBird.DoIt;',
  13364. 'var',
  13365. ' List: TAlphaList;',
  13366. 'begin',
  13367. ' if TBetaList(List[true])[3]=nil then ;',
  13368. ' TBetaList(List[false])[5]:=nil;',
  13369. 'end;',
  13370. 'var',
  13371. ' List: TAlphaList;',
  13372. 'begin',
  13373. ' if TBetaList(List[true])[3]=nil then ;',
  13374. ' TBetaList(List[false])[5]:=nil;',
  13375. '']);
  13376. ConvertProgram;
  13377. CheckSource('TestClass_PropertyDefault_TypecastToOtherDefault',
  13378. LinesToStr([ // statements
  13379. 'rtl.createClass(this, "TObject", null, function () {',
  13380. ' this.$init = function () {',
  13381. ' };',
  13382. ' this.$final = function () {',
  13383. ' };',
  13384. '});',
  13385. 'rtl.createClass(this, "TAlphaList", this.TObject, function () {',
  13386. '});',
  13387. 'rtl.createClass(this, "TBetaList", this.TObject, function () {',
  13388. '});',
  13389. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  13390. ' this.DoIt = function () {',
  13391. ' var List = null;',
  13392. ' if (List.GetAlphas(true).GetBetas(3) === null) ;',
  13393. ' List.GetAlphas(false).SetBetas(5, null);',
  13394. ' };',
  13395. '});',
  13396. 'this.List = null;',
  13397. '']),
  13398. LinesToStr([ // $mod.$main
  13399. 'if ($mod.List.GetAlphas(true).GetBetas(3) === null) ;',
  13400. '$mod.List.GetAlphas(false).SetBetas(5, null);',
  13401. '']));
  13402. end;
  13403. procedure TTestModule.TestClass_PropertyOverride;
  13404. begin
  13405. StartProgram(false);
  13406. Add('type');
  13407. Add(' integer = longint;');
  13408. Add(' TObject = class');
  13409. Add(' FItem: integer;');
  13410. Add(' function GetItem: integer; external name ''GetItem'';');
  13411. Add(' procedure SetItem(Value: integer); external name ''SetItem'';');
  13412. Add(' property Item: integer read getitem write setitem;');
  13413. Add(' end;');
  13414. Add(' TCar = class');
  13415. Add(' FBag: integer;');
  13416. Add(' function GetBag: integer; external name ''GetBag'';');
  13417. Add(' property Item read getbag;');
  13418. Add(' end;');
  13419. Add('var');
  13420. Add(' Obj: tobject;');
  13421. Add(' Car: tcar;');
  13422. Add('begin');
  13423. Add(' Obj.Item:=Obj.Item;');
  13424. Add(' Car.Item:=Car.Item;');
  13425. ConvertProgram;
  13426. CheckSource('TestClass_PropertyOverride',
  13427. LinesToStr([ // statements
  13428. 'rtl.createClass(this, "TObject", null, function () {',
  13429. ' this.$init = function () {',
  13430. ' this.FItem = 0;',
  13431. ' };',
  13432. ' this.$final = function () {',
  13433. ' };',
  13434. '});',
  13435. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  13436. ' this.$init = function () {',
  13437. ' $mod.TObject.$init.call(this);',
  13438. ' this.FBag = 0;',
  13439. ' };',
  13440. '});',
  13441. 'this.Obj = null;',
  13442. 'this.Car = null;',
  13443. '']),
  13444. LinesToStr([ // $mod.$main
  13445. '$mod.Obj.SetItem($mod.Obj.GetItem());',
  13446. '$mod.Car.SetItem($mod.Car.GetBag());',
  13447. '']));
  13448. end;
  13449. procedure TTestModule.TestClass_PropertyIncVisibility;
  13450. begin
  13451. AddModuleWithIntfImplSrc('unit1.pp',
  13452. LinesToStr([
  13453. 'type',
  13454. ' TNumber = longint;',
  13455. ' TInteger = longint;',
  13456. ' TObject = class',
  13457. ' private',
  13458. ' function GetItems(Index: TNumber): TInteger; virtual; abstract;',
  13459. ' procedure SetItems(Index: TInteger; Value: TNumber); virtual; abstract;',
  13460. ' protected',
  13461. ' property Items[Index: TNumber]: longint read GetItems write SetItems;',
  13462. ' end;']),
  13463. LinesToStr([
  13464. '']));
  13465. StartProgram(true);
  13466. Add([
  13467. 'uses unit1;',
  13468. 'type',
  13469. ' TBird = class',
  13470. ' public',
  13471. ' property Items;',
  13472. ' end;',
  13473. 'procedure DoIt(i: TInteger);',
  13474. 'begin',
  13475. 'end;',
  13476. 'var b: TBird;',
  13477. 'begin',
  13478. ' b.Items[1]:=2;',
  13479. ' b.Items[3]:=b.Items[4];',
  13480. ' DoIt(b.Items[5]);',
  13481. '']);
  13482. ConvertProgram;
  13483. CheckSource('TestClass_PropertyIncVisibility',
  13484. LinesToStr([ // statements
  13485. 'rtl.createClass(this, "TBird", pas.unit1.TObject, function () {',
  13486. '});',
  13487. 'this.DoIt = function (i) {',
  13488. '};',
  13489. 'this.b = null;'
  13490. ]),
  13491. LinesToStr([ // $mod.$main
  13492. '$mod.b.SetItems(1, 2);',
  13493. '$mod.b.SetItems(3, $mod.b.GetItems(4));',
  13494. '$mod.DoIt($mod.b.GetItems(5));'
  13495. ]));
  13496. end;
  13497. procedure TTestModule.TestClass_Assigned;
  13498. begin
  13499. StartProgram(false);
  13500. Add('type');
  13501. Add(' TObject = class');
  13502. Add(' end;');
  13503. Add('var');
  13504. Add(' Obj: tobject;');
  13505. Add(' b: boolean;');
  13506. Add('begin');
  13507. Add(' if Assigned(obj) then ;');
  13508. Add(' b:=Assigned(obj) or false;');
  13509. ConvertProgram;
  13510. CheckSource('TestClass_Assigned',
  13511. LinesToStr([ // statements
  13512. 'rtl.createClass(this, "TObject", null, function () {',
  13513. ' this.$init = function () {',
  13514. ' };',
  13515. ' this.$final = function () {',
  13516. ' };',
  13517. '});',
  13518. 'this.Obj = null;',
  13519. 'this.b = false;'
  13520. ]),
  13521. LinesToStr([ // $mod.$main
  13522. 'if ($mod.Obj != null);',
  13523. '$mod.b = ($mod.Obj != null) || false;'
  13524. ]));
  13525. end;
  13526. procedure TTestModule.TestClass_WithClassDoCreate;
  13527. begin
  13528. StartProgram(false);
  13529. Add('type');
  13530. Add(' TObject = class');
  13531. Add(' aBool: boolean;');
  13532. Add(' Arr: array of boolean;');
  13533. Add(' constructor Create;');
  13534. Add(' end;');
  13535. Add('constructor TObject.Create; begin end;');
  13536. Add('var');
  13537. Add(' Obj: tobject;');
  13538. Add(' b: boolean;');
  13539. Add('begin');
  13540. Add(' with tobject.create do begin');
  13541. Add(' b:=abool;');
  13542. Add(' abool:=b;');
  13543. Add(' b:=arr[1];');
  13544. Add(' arr[2]:=b;');
  13545. Add(' end;');
  13546. Add(' with tobject do');
  13547. Add(' obj:=create;');
  13548. Add(' with obj do begin');
  13549. Add(' create;');
  13550. Add(' b:=abool;');
  13551. Add(' abool:=b;');
  13552. Add(' b:=arr[3];');
  13553. Add(' arr[4]:=b;');
  13554. Add(' end;');
  13555. ConvertProgram;
  13556. CheckSource('TestClass_WithClassDoCreate',
  13557. LinesToStr([ // statements
  13558. 'rtl.createClass(this, "TObject", null, function () {',
  13559. ' this.$init = function () {',
  13560. ' this.aBool = false;',
  13561. ' this.Arr = [];',
  13562. ' };',
  13563. ' this.$final = function () {',
  13564. ' this.Arr = undefined;',
  13565. ' };',
  13566. ' this.Create = function () {',
  13567. ' return this;',
  13568. ' };',
  13569. '});',
  13570. 'this.Obj = null;',
  13571. 'this.b = false;'
  13572. ]),
  13573. LinesToStr([ // $mod.$main
  13574. 'var $with = $mod.TObject.$create("Create");',
  13575. '$mod.b = $with.aBool;',
  13576. '$with.aBool = $mod.b;',
  13577. '$mod.b = $with.Arr[1];',
  13578. '$with.Arr[2] = $mod.b;',
  13579. 'var $with1 = $mod.TObject;',
  13580. '$mod.Obj = $with1.$create("Create");',
  13581. 'var $with2 = $mod.Obj;',
  13582. '$with2.Create();',
  13583. '$mod.b = $with2.aBool;',
  13584. '$with2.aBool = $mod.b;',
  13585. '$mod.b = $with2.Arr[3];',
  13586. '$with2.Arr[4] = $mod.b;',
  13587. '']));
  13588. end;
  13589. procedure TTestModule.TestClass_WithClassInstDoProperty;
  13590. begin
  13591. StartProgram(false);
  13592. Add('type');
  13593. Add(' TObject = class');
  13594. Add(' FInt: longint;');
  13595. Add(' constructor Create;');
  13596. Add(' function GetSize: longint;');
  13597. Add(' procedure SetSize(Value: longint);');
  13598. Add(' property Int: longint read FInt write FInt;');
  13599. Add(' property Size: longint read GetSize write SetSize;');
  13600. Add(' end;');
  13601. Add('constructor TObject.Create; begin end;');
  13602. Add('function TObject.GetSize: longint; begin; end;');
  13603. Add('procedure TObject.SetSize(Value: longint); begin; end;');
  13604. Add('var');
  13605. Add(' Obj: tobject;');
  13606. Add(' i: longint;');
  13607. Add('begin');
  13608. Add(' with TObject.Create do begin');
  13609. Add(' i:=int;');
  13610. Add(' int:=i;');
  13611. Add(' i:=size;');
  13612. Add(' size:=i;');
  13613. Add(' end;');
  13614. Add(' with obj do begin');
  13615. Add(' i:=int;');
  13616. Add(' int:=i;');
  13617. Add(' i:=size;');
  13618. Add(' size:=i;');
  13619. Add(' end;');
  13620. ConvertProgram;
  13621. CheckSource('TestClass_WithClassInstDoProperty',
  13622. LinesToStr([ // statements
  13623. 'rtl.createClass(this, "TObject", null, function () {',
  13624. ' this.$init = function () {',
  13625. ' this.FInt = 0;',
  13626. ' };',
  13627. ' this.$final = function () {',
  13628. ' };',
  13629. ' this.Create = function () {',
  13630. ' return this;',
  13631. ' };',
  13632. ' this.GetSize = function () {',
  13633. ' var Result = 0;',
  13634. ' return Result;',
  13635. ' };',
  13636. ' this.SetSize = function (Value) {',
  13637. ' };',
  13638. '});',
  13639. 'this.Obj = null;',
  13640. 'this.i = 0;'
  13641. ]),
  13642. LinesToStr([ // $mod.$main
  13643. 'var $with = $mod.TObject.$create("Create");',
  13644. '$mod.i = $with.FInt;',
  13645. '$with.FInt = $mod.i;',
  13646. '$mod.i = $with.GetSize();',
  13647. '$with.SetSize($mod.i);',
  13648. 'var $with1 = $mod.Obj;',
  13649. '$mod.i = $with1.FInt;',
  13650. '$with1.FInt = $mod.i;',
  13651. '$mod.i = $with1.GetSize();',
  13652. '$with1.SetSize($mod.i);',
  13653. '']));
  13654. end;
  13655. procedure TTestModule.TestClass_WithClassInstDoPropertyWithParams;
  13656. begin
  13657. StartProgram(false);
  13658. Add('type');
  13659. Add(' TObject = class');
  13660. Add(' constructor Create;');
  13661. Add(' function GetItems(Index: longint): longint;');
  13662. Add(' procedure SetItems(Index, Value: longint);');
  13663. Add(' property Items[Index: longint]: longint read GetItems write SetItems;');
  13664. Add(' end;');
  13665. Add('constructor TObject.Create; begin end;');
  13666. Add('function tobject.getitems(index: longint): longint; begin; end;');
  13667. Add('procedure tobject.setitems(index, value: longint); begin; end;');
  13668. Add('var');
  13669. Add(' Obj: tobject;');
  13670. Add(' i: longint;');
  13671. Add('begin');
  13672. Add(' with TObject.Create do begin');
  13673. Add(' i:=Items[1];');
  13674. Add(' Items[2]:=i;');
  13675. Add(' end;');
  13676. Add(' with obj do begin');
  13677. Add(' i:=Items[3];');
  13678. Add(' Items[4]:=i;');
  13679. Add(' end;');
  13680. ConvertProgram;
  13681. CheckSource('TestClass_WithClassInstDoPropertyWithParams',
  13682. LinesToStr([ // statements
  13683. 'rtl.createClass(this, "TObject", null, function () {',
  13684. ' this.$init = function () {',
  13685. ' };',
  13686. ' this.$final = function () {',
  13687. ' };',
  13688. ' this.Create = function () {',
  13689. ' return this;',
  13690. ' };',
  13691. ' this.GetItems = function (Index) {',
  13692. ' var Result = 0;',
  13693. ' return Result;',
  13694. ' };',
  13695. ' this.SetItems = function (Index, Value) {',
  13696. ' };',
  13697. '});',
  13698. 'this.Obj = null;',
  13699. 'this.i = 0;'
  13700. ]),
  13701. LinesToStr([ // $mod.$main
  13702. 'var $with = $mod.TObject.$create("Create");',
  13703. '$mod.i = $with.GetItems(1);',
  13704. '$with.SetItems(2, $mod.i);',
  13705. 'var $with1 = $mod.Obj;',
  13706. '$mod.i = $with1.GetItems(3);',
  13707. '$with1.SetItems(4, $mod.i);',
  13708. '']));
  13709. end;
  13710. procedure TTestModule.TestClass_WithClassInstDoFunc;
  13711. begin
  13712. StartProgram(false);
  13713. Add('type');
  13714. Add(' TObject = class');
  13715. Add(' constructor Create;');
  13716. Add(' function GetSize: longint;');
  13717. Add(' procedure SetSize(Value: longint);');
  13718. Add(' end;');
  13719. Add('constructor TObject.Create; begin end;');
  13720. Add('function TObject.GetSize: longint; begin; end;');
  13721. Add('procedure TObject.SetSize(Value: longint); begin; end;');
  13722. Add('var');
  13723. Add(' Obj: tobject;');
  13724. Add(' i: longint;');
  13725. Add('begin');
  13726. Add(' with TObject.Create do begin');
  13727. Add(' i:=GetSize;');
  13728. Add(' i:=GetSize();');
  13729. Add(' SetSize(i);');
  13730. Add(' end;');
  13731. Add(' with obj do begin');
  13732. Add(' i:=GetSize;');
  13733. Add(' i:=GetSize();');
  13734. Add(' SetSize(i);');
  13735. Add(' end;');
  13736. ConvertProgram;
  13737. CheckSource('TestClass_WithClassInstDoFunc',
  13738. LinesToStr([ // statements
  13739. 'rtl.createClass(this, "TObject", null, function () {',
  13740. ' this.$init = function () {',
  13741. ' };',
  13742. ' this.$final = function () {',
  13743. ' };',
  13744. ' this.Create = function () {',
  13745. ' return this;',
  13746. ' };',
  13747. ' this.GetSize = function () {',
  13748. ' var Result = 0;',
  13749. ' return Result;',
  13750. ' };',
  13751. ' this.SetSize = function (Value) {',
  13752. ' };',
  13753. '});',
  13754. 'this.Obj = null;',
  13755. 'this.i = 0;'
  13756. ]),
  13757. LinesToStr([ // $mod.$main
  13758. 'var $with = $mod.TObject.$create("Create");',
  13759. '$mod.i = $with.GetSize();',
  13760. '$mod.i = $with.GetSize();',
  13761. '$with.SetSize($mod.i);',
  13762. 'var $with1 = $mod.Obj;',
  13763. '$mod.i = $with1.GetSize();',
  13764. '$mod.i = $with1.GetSize();',
  13765. '$with1.SetSize($mod.i);',
  13766. '']));
  13767. end;
  13768. procedure TTestModule.TestClass_TypeCast;
  13769. begin
  13770. StartProgram(false);
  13771. Add('type');
  13772. Add(' TObject = class');
  13773. Add(' Next: TObject;');
  13774. Add(' constructor Create;');
  13775. Add(' end;');
  13776. Add(' TControl = class(TObject)');
  13777. Add(' Arr: array of TObject;');
  13778. Add(' function GetIt(vI: longint = 0): TObject;');
  13779. Add(' end;');
  13780. Add('constructor tobject.create; begin end;');
  13781. Add('function tcontrol.getit(vi: longint = 0): tobject; begin end;');
  13782. Add('var');
  13783. Add(' Obj: tobject;');
  13784. Add('begin');
  13785. Add(' obj:=tcontrol(obj).next;');
  13786. Add(' tcontrol(obj):=nil;');
  13787. Add(' obj:=tcontrol(obj);');
  13788. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit);');
  13789. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit());');
  13790. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit(1));');
  13791. Add(' tcontrol(obj):=tcontrol(tcontrol(tcontrol(obj).getit).arr[2]);');
  13792. Add(' obj:=tcontrol(nil);');
  13793. ConvertProgram;
  13794. CheckSource('TestClass_TypeCast',
  13795. LinesToStr([ // statements
  13796. 'rtl.createClass(this, "TObject", null, function () {',
  13797. ' this.$init = function () {',
  13798. ' this.Next = null;',
  13799. ' };',
  13800. ' this.$final = function () {',
  13801. ' this.Next = undefined;',
  13802. ' };',
  13803. ' this.Create = function () {',
  13804. ' return this;',
  13805. ' };',
  13806. '});',
  13807. 'rtl.createClass(this, "TControl", this.TObject, function () {',
  13808. ' this.$init = function () {',
  13809. ' $mod.TObject.$init.call(this);',
  13810. ' this.Arr = [];',
  13811. ' };',
  13812. ' this.$final = function () {',
  13813. ' this.Arr = undefined;',
  13814. ' $mod.TObject.$final.call(this);',
  13815. ' };',
  13816. ' this.GetIt = function (vI) {',
  13817. ' var Result = null;',
  13818. ' return Result;',
  13819. ' };',
  13820. '});',
  13821. 'this.Obj = null;'
  13822. ]),
  13823. LinesToStr([ // $mod.$main
  13824. '$mod.Obj = $mod.Obj.Next;',
  13825. '$mod.Obj = null;',
  13826. '$mod.Obj = $mod.Obj;',
  13827. '$mod.Obj = $mod.Obj.GetIt(0);',
  13828. '$mod.Obj = $mod.Obj.GetIt(0);',
  13829. '$mod.Obj = $mod.Obj.GetIt(1);',
  13830. '$mod.Obj = $mod.Obj.GetIt(0).Arr[2];',
  13831. '$mod.Obj = null;',
  13832. '']));
  13833. end;
  13834. procedure TTestModule.TestClass_TypeCastUntypedParam;
  13835. begin
  13836. StartProgram(false);
  13837. Add('type');
  13838. Add(' TObject = class end;');
  13839. Add('procedure ProcA(var A);');
  13840. Add('begin');
  13841. Add(' TObject(A):=nil;');
  13842. Add(' TObject(A):=TObject(A);');
  13843. Add(' if TObject(A)=nil then ;');
  13844. Add(' if nil=TObject(A) then ;');
  13845. Add('end;');
  13846. Add('procedure ProcB(out A);');
  13847. Add('begin');
  13848. Add(' TObject(A):=nil;');
  13849. Add(' TObject(A):=TObject(A);');
  13850. Add(' if TObject(A)=nil then ;');
  13851. Add(' if nil=TObject(A) then ;');
  13852. Add('end;');
  13853. Add('procedure ProcC(const A);');
  13854. Add('begin');
  13855. Add(' if TObject(A)=nil then ;');
  13856. Add(' if nil=TObject(A) then ;');
  13857. Add('end;');
  13858. Add('var o: TObject;');
  13859. Add('begin');
  13860. Add(' ProcA(o);');
  13861. Add(' ProcB(o);');
  13862. Add(' ProcC(o);');
  13863. ConvertProgram;
  13864. CheckSource('TestClass_TypeCastUntypedParam',
  13865. LinesToStr([ // statements
  13866. 'rtl.createClass(this, "TObject", null, function () {',
  13867. ' this.$init = function () {',
  13868. ' };',
  13869. ' this.$final = function () {',
  13870. ' };',
  13871. '});',
  13872. 'this.ProcA = function (A) {',
  13873. ' A.set(null);',
  13874. ' A.set(A.get());',
  13875. ' if (A.get() === null);',
  13876. ' if (null === A.get());',
  13877. '};',
  13878. 'this.ProcB = function (A) {',
  13879. ' A.set(null);',
  13880. ' A.set(A.get());',
  13881. ' if (A.get() === null);',
  13882. ' if (null === A.get());',
  13883. '};',
  13884. 'this.ProcC = function (A) {',
  13885. ' if (A === null);',
  13886. ' if (null === A);',
  13887. '};',
  13888. 'this.o = null;',
  13889. '']),
  13890. LinesToStr([ // $mod.$main
  13891. '$mod.ProcA({',
  13892. ' p: $mod,',
  13893. ' get: function () {',
  13894. ' return this.p.o;',
  13895. ' },',
  13896. ' set: function (v) {',
  13897. ' this.p.o = v;',
  13898. ' }',
  13899. '});',
  13900. '$mod.ProcB({',
  13901. ' p: $mod,',
  13902. ' get: function () {',
  13903. ' return this.p.o;',
  13904. ' },',
  13905. ' set: function (v) {',
  13906. ' this.p.o = v;',
  13907. ' }',
  13908. '});',
  13909. '$mod.ProcC($mod.o);',
  13910. '']));
  13911. end;
  13912. procedure TTestModule.TestClass_Overloads;
  13913. begin
  13914. StartProgram(false);
  13915. Add('type');
  13916. Add(' TObject = class');
  13917. Add(' procedure DoIt;');
  13918. Add(' procedure DoIt(vI: longint);');
  13919. Add(' end;');
  13920. Add('procedure TObject.DoIt;');
  13921. Add('begin');
  13922. Add(' DoIt;');
  13923. Add(' DoIt(1);');
  13924. Add('end;');
  13925. Add('procedure TObject.DoIt(vI: longint); begin end;');
  13926. Add('begin');
  13927. ConvertProgram;
  13928. CheckSource('TestClass_Overloads',
  13929. LinesToStr([ // statements
  13930. 'rtl.createClass(this, "TObject", null, function () {',
  13931. ' this.$init = function () {',
  13932. ' };',
  13933. ' this.$final = function () {',
  13934. ' };',
  13935. ' this.DoIt = function () {',
  13936. ' this.DoIt();',
  13937. ' this.DoIt$1(1);',
  13938. ' };',
  13939. ' this.DoIt$1 = function (vI) {',
  13940. ' };',
  13941. '});',
  13942. '']),
  13943. LinesToStr([ // $mod.$main
  13944. '']));
  13945. end;
  13946. procedure TTestModule.TestClass_OverloadsAncestor;
  13947. begin
  13948. StartProgram(false);
  13949. Add('type');
  13950. Add(' TObject = class;');
  13951. Add(' TObject = class');
  13952. Add(' procedure DoIt(vA: longint);');
  13953. Add(' procedure DoIt(vA, vB: longint);');
  13954. Add(' end;');
  13955. Add(' TCar = class;');
  13956. Add(' TCar = class');
  13957. Add(' procedure DoIt(vA: longint);');
  13958. Add(' procedure DoIt(vA, vB: longint);');
  13959. Add(' end;');
  13960. Add('procedure tobject.doit(va: longint);');
  13961. Add('begin');
  13962. Add(' doit(1);');
  13963. Add(' doit(1,2);');
  13964. Add('end;');
  13965. Add('procedure tobject.doit(va, vb: longint); begin end;');
  13966. Add('procedure tcar.doit(va: longint);');
  13967. Add('begin');
  13968. Add(' doit(1);');
  13969. Add(' doit(1,2);');
  13970. Add(' inherited doit(1);');
  13971. Add(' inherited doit(1,2);');
  13972. Add('end;');
  13973. Add('procedure tcar.doit(va, vb: longint); begin end;');
  13974. Add('begin');
  13975. ConvertProgram;
  13976. CheckSource('TestClass_OverloadsAncestor',
  13977. LinesToStr([ // statements
  13978. 'rtl.createClass(this, "TObject", null, function () {',
  13979. ' this.$init = function () {',
  13980. ' };',
  13981. ' this.$final = function () {',
  13982. ' };',
  13983. ' this.DoIt = function (vA) {',
  13984. ' this.DoIt(1);',
  13985. ' this.DoIt$1(1,2);',
  13986. ' };',
  13987. ' this.DoIt$1 = function (vA, vB) {',
  13988. ' };',
  13989. '});',
  13990. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  13991. ' this.DoIt$2 = function (vA) {',
  13992. ' this.DoIt$2(1);',
  13993. ' this.DoIt$3(1, 2);',
  13994. ' $mod.TObject.DoIt.call(this, 1);',
  13995. ' $mod.TObject.DoIt$1.call(this, 1, 2);',
  13996. ' };',
  13997. ' this.DoIt$3 = function (vA, vB) {',
  13998. ' };',
  13999. '});',
  14000. '']),
  14001. LinesToStr([ // $mod.$main
  14002. '']));
  14003. end;
  14004. procedure TTestModule.TestClass_OverloadConstructor;
  14005. begin
  14006. StartProgram(false);
  14007. Add('type');
  14008. Add(' TObject = class');
  14009. Add(' constructor Create(vA: longint);');
  14010. Add(' constructor Create(vA, vB: longint);');
  14011. Add(' end;');
  14012. Add(' TCar = class');
  14013. Add(' constructor Create(vA: longint);');
  14014. Add(' constructor Create(vA, vB: longint);');
  14015. Add(' end;');
  14016. Add('constructor tobject.create(va: longint);');
  14017. Add('begin');
  14018. Add(' create(1);');
  14019. Add(' create(1,2);');
  14020. Add('end;');
  14021. Add('constructor tobject.create(va, vb: longint); begin end;');
  14022. Add('constructor tcar.create(va: longint);');
  14023. Add('begin');
  14024. Add(' create(1);');
  14025. Add(' create(1,2);');
  14026. Add(' inherited create(1);');
  14027. Add(' inherited create(1,2);');
  14028. Add('end;');
  14029. Add('constructor tcar.create(va, vb: longint); begin end;');
  14030. Add('begin');
  14031. Add(' tobject.create(1);');
  14032. Add(' tobject.create(1,2);');
  14033. Add(' tcar.create(1);');
  14034. Add(' tcar.create(1,2);');
  14035. ConvertProgram;
  14036. CheckSource('TestClass_OverloadConstructor',
  14037. LinesToStr([ // statements
  14038. 'rtl.createClass(this, "TObject", null, function () {',
  14039. ' this.$init = function () {',
  14040. ' };',
  14041. ' this.$final = function () {',
  14042. ' };',
  14043. ' this.Create = function (vA) {',
  14044. ' this.Create(1);',
  14045. ' this.Create$1(1,2);',
  14046. ' return this;',
  14047. ' };',
  14048. ' this.Create$1 = function (vA, vB) {',
  14049. ' return this;',
  14050. ' };',
  14051. '});',
  14052. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  14053. ' this.Create$2 = function (vA) {',
  14054. ' this.Create$2(1);',
  14055. ' this.Create$3(1, 2);',
  14056. ' $mod.TObject.Create.call(this, 1);',
  14057. ' $mod.TObject.Create$1.call(this, 1, 2);',
  14058. ' return this;',
  14059. ' };',
  14060. ' this.Create$3 = function (vA, vB) {',
  14061. ' return this;',
  14062. ' };',
  14063. '});',
  14064. '']),
  14065. LinesToStr([ // $mod.$main
  14066. '$mod.TObject.$create("Create", [1]);',
  14067. '$mod.TObject.$create("Create$1", [1, 2]);',
  14068. '$mod.TCar.$create("Create$2", [1]);',
  14069. '$mod.TCar.$create("Create$3", [1, 2]);',
  14070. '']));
  14071. end;
  14072. procedure TTestModule.TestClass_OverloadDelphiOverride;
  14073. begin
  14074. StartProgram(false);
  14075. Add([
  14076. '{$mode delphi}',
  14077. 'type',
  14078. ' TObject = class end;',
  14079. ' TBird = class',
  14080. ' function {#a}GetValue: longint; overload; virtual;',
  14081. ' function {#b}GetValue(AValue: longint): longint; overload; virtual;',
  14082. ' end;',
  14083. ' TEagle = class(TBird)',
  14084. ' function {#c}GetValue: longint; overload; override;',
  14085. ' function {#d}GetValue(AValue: longint): longint; overload; override;',
  14086. ' end;',
  14087. 'function TBird.GetValue: longint;',
  14088. 'begin',
  14089. ' if 3={@a}GetValue then ;',
  14090. ' if 4={@b}GetValue(5) then ;',
  14091. 'end;',
  14092. 'function TBird.GetValue(AValue: longint): longint;',
  14093. 'begin',
  14094. 'end;',
  14095. 'function TEagle.GetValue: longint;',
  14096. 'begin',
  14097. ' if 13={@c}GetValue then ;',
  14098. ' if 14={@d}GetValue(15) then ;',
  14099. ' if 15=inherited {@a}GetValue then ;',
  14100. ' if 16=inherited {@b}GetValue(17) then ;',
  14101. 'end;',
  14102. 'function TEagle.GetValue(AValue: longint): longint;',
  14103. 'begin',
  14104. 'end;',
  14105. 'var',
  14106. ' e: TEagle;',
  14107. 'begin',
  14108. ' if 23=e.{@c}GetValue then ;',
  14109. ' if 24=e.{@d}GetValue(25) then ;']);
  14110. ConvertProgram;
  14111. CheckSource('TestClass_OverloadDelphiOverride',
  14112. LinesToStr([ // statements
  14113. 'rtl.createClass(this, "TObject", null, function () {',
  14114. ' this.$init = function () {',
  14115. ' };',
  14116. ' this.$final = function () {',
  14117. ' };',
  14118. '});',
  14119. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  14120. ' this.GetValue = function () {',
  14121. ' var Result = 0;',
  14122. ' if (3 === this.GetValue()) ;',
  14123. ' if (4 === this.GetValue$1(5)) ;',
  14124. ' return Result;',
  14125. ' };',
  14126. ' this.GetValue$1 = function (AValue) {',
  14127. ' var Result = 0;',
  14128. ' return Result;',
  14129. ' };',
  14130. '});',
  14131. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  14132. ' this.GetValue = function () {',
  14133. ' var Result = 0;',
  14134. ' if (13 === this.GetValue()) ;',
  14135. ' if (14 === this.GetValue$1(15)) ;',
  14136. ' if (15 === $mod.TBird.GetValue.call(this)) ;',
  14137. ' if (16 === $mod.TBird.GetValue$1.call(this, 17)) ;',
  14138. ' return Result;',
  14139. ' };',
  14140. ' this.GetValue$1 = function (AValue) {',
  14141. ' var Result = 0;',
  14142. ' return Result;',
  14143. ' };',
  14144. '});',
  14145. 'this.e = null;',
  14146. '']),
  14147. LinesToStr([ // $mod.$main
  14148. 'if (23 === $mod.e.GetValue()) ;',
  14149. 'if (24 === $mod.e.GetValue$1(25)) ;',
  14150. '']));
  14151. end;
  14152. procedure TTestModule.TestClass_ReintroduceVarDelphi;
  14153. begin
  14154. StartProgram(false);
  14155. Add([
  14156. '{$mode delphi}',
  14157. 'type',
  14158. ' TObject = class end;',
  14159. ' TAnimal = class',
  14160. ' public',
  14161. ' {#animal_a}A: longint;',
  14162. ' function {#animal_b}B: longint;',
  14163. ' end;',
  14164. ' TBird = class(TAnimal)',
  14165. ' public',
  14166. ' {#bird_a}A: double;',
  14167. ' {#bird_b}B: boolean;',
  14168. ' end;',
  14169. ' TEagle = class(TBird)',
  14170. ' public',
  14171. ' function {#eagle_a}A: boolean;',
  14172. ' {#eagle_b}B: double;',
  14173. ' end;',
  14174. 'function TAnimal.B: longint;',
  14175. 'begin',
  14176. 'end;',
  14177. 'function TEagle.A: boolean;',
  14178. 'begin',
  14179. ' {@eagle_b}B:=3.3;',
  14180. ' {@eagle_a}A();',
  14181. ' TBird(Self).{@bird_b}B:=true;',
  14182. ' TAnimal(Self).{@animal_a}A:=17;',
  14183. ' inherited {@bird_b}B:=inherited {bird_a}A>1;', // Delphi allows only inherited <functionname>
  14184. 'end;',
  14185. 'var',
  14186. ' e: TEagle;',
  14187. 'begin',
  14188. ' e.{@eagle_b}B:=5.3;',
  14189. ' if e.{@eagle_a}A then ;',
  14190. '']);
  14191. ConvertProgram;
  14192. CheckSource('TestClass_ReintroduceVarDelphi',
  14193. LinesToStr([ // statements
  14194. 'rtl.createClass(this, "TObject", null, function () {',
  14195. ' this.$init = function () {',
  14196. ' };',
  14197. ' this.$final = function () {',
  14198. ' };',
  14199. '});',
  14200. 'rtl.createClass(this, "TAnimal", this.TObject, function () {',
  14201. ' this.$init = function () {',
  14202. ' $mod.TObject.$init.call(this);',
  14203. ' this.A = 0;',
  14204. ' };',
  14205. ' this.B = function () {',
  14206. ' var Result = 0;',
  14207. ' return Result;',
  14208. ' };',
  14209. '});',
  14210. 'rtl.createClass(this, "TBird", this.TAnimal, function () {',
  14211. ' this.$init = function () {',
  14212. ' $mod.TAnimal.$init.call(this);',
  14213. ' this.A$1 = 0.0;',
  14214. ' this.B$1 = false;',
  14215. ' };',
  14216. '});',
  14217. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  14218. ' this.$init = function () {',
  14219. ' $mod.TBird.$init.call(this);',
  14220. ' this.B$2 = 0.0;',
  14221. ' };',
  14222. ' this.A$2 = function () {',
  14223. ' var Result = false;',
  14224. ' this.B$2 = 3.3;',
  14225. ' this.A$2();',
  14226. ' this.B$1 = true;',
  14227. ' this.A = 17;',
  14228. ' this.B$1 = this.A$1 > 1;',
  14229. ' return Result;',
  14230. ' };',
  14231. '});',
  14232. 'this.e = null;',
  14233. '']),
  14234. LinesToStr([ // $mod.$main
  14235. '$mod.e.B$2 = 5.3;',
  14236. 'if ($mod.e.A$2()) ;',
  14237. '']));
  14238. end;
  14239. procedure TTestModule.TestClass_ReintroducedVar;
  14240. begin
  14241. StartProgram(false);
  14242. Add('type');
  14243. Add(' TObject = class');
  14244. Add(' strict private');
  14245. Add(' Some: longint;');
  14246. Add(' end;');
  14247. Add(' TMobile = class');
  14248. Add(' strict private');
  14249. Add(' Some: string;');
  14250. Add(' end;');
  14251. Add(' TCar = class(tmobile)');
  14252. Add(' procedure Some;');
  14253. Add(' procedure Some(vA: longint);');
  14254. Add(' end;');
  14255. Add('procedure tcar.some;');
  14256. Add('begin');
  14257. Add(' Some;');
  14258. Add(' Some(1);');
  14259. Add('end;');
  14260. Add('procedure tcar.some(va: longint); begin end;');
  14261. Add('begin');
  14262. ConvertProgram;
  14263. CheckSource('TestClass_ReintroducedVar',
  14264. LinesToStr([ // statements
  14265. 'rtl.createClass(this, "TObject", null, function () {',
  14266. ' this.$init = function () {',
  14267. ' this.Some = 0;',
  14268. ' };',
  14269. ' this.$final = function () {',
  14270. ' };',
  14271. '});',
  14272. 'rtl.createClass(this, "TMobile", this.TObject, function () {',
  14273. ' this.$init = function () {',
  14274. ' $mod.TObject.$init.call(this);',
  14275. ' this.Some$1 = "";',
  14276. ' };',
  14277. '});',
  14278. 'rtl.createClass(this, "TCar", this.TMobile, function () {',
  14279. ' this.Some$2 = function () {',
  14280. ' this.Some$2();',
  14281. ' this.Some$3(1);',
  14282. ' };',
  14283. ' this.Some$3 = function (vA) {',
  14284. ' };',
  14285. '});',
  14286. '']),
  14287. LinesToStr([ // $mod.$main
  14288. '']));
  14289. end;
  14290. procedure TTestModule.TestClass_RaiseDescendant;
  14291. begin
  14292. StartProgram(false);
  14293. Add([
  14294. 'type',
  14295. ' TObject = class',
  14296. ' constructor Create(Msg: string);',
  14297. ' end;',
  14298. ' Exception = class',
  14299. ' end;',
  14300. ' EConvertError = class(Exception)',
  14301. ' end;',
  14302. 'constructor TObject.Create(Msg: string); begin end;',
  14303. 'function AssertConv(Msg: string = ''def''): EConvertError; begin end;',
  14304. 'begin',
  14305. ' raise Exception.Create(''Bar1'');',
  14306. ' raise EConvertError.Create(''Bar2'');',
  14307. ' raise AssertConv(''Bar2'');',
  14308. ' raise AssertConv;',
  14309. '']);
  14310. ConvertProgram;
  14311. CheckSource('TestClass_RaiseDescendant',
  14312. LinesToStr([ // statements
  14313. 'rtl.createClass(this, "TObject", null, function () {',
  14314. ' this.$init = function () {',
  14315. ' };',
  14316. ' this.$final = function () {',
  14317. ' };',
  14318. ' this.Create = function (Msg) {',
  14319. ' return this;',
  14320. ' };',
  14321. '});',
  14322. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  14323. '});',
  14324. 'rtl.createClass(this, "EConvertError", this.Exception, function () {',
  14325. '});',
  14326. 'this.AssertConv = function (Msg) {',
  14327. ' var Result = null;',
  14328. ' return Result;',
  14329. '};',
  14330. '']),
  14331. LinesToStr([ // $mod.$main
  14332. 'throw $mod.Exception.$create("Create",["Bar1"]);',
  14333. 'throw $mod.EConvertError.$create("Create",["Bar2"]);',
  14334. 'throw $mod.AssertConv("Bar2");',
  14335. 'throw $mod.AssertConv("def");',
  14336. '']));
  14337. end;
  14338. procedure TTestModule.TestClass_ExternalMethod;
  14339. begin
  14340. AddModuleWithIntfImplSrc('unit2.pas',
  14341. LinesToStr([
  14342. 'type',
  14343. ' TObject = class',
  14344. ' public',
  14345. ' procedure Intern; external name ''$DoIntern'';',
  14346. ' end;',
  14347. '']),
  14348. LinesToStr([
  14349. '']));
  14350. StartUnit(true);
  14351. Add('interface');
  14352. Add('uses unit2;');
  14353. Add('type');
  14354. Add(' TCar = class(TObject)');
  14355. Add(' public');
  14356. Add(' procedure Intern2; external name ''$DoIntern2'';');
  14357. Add(' procedure DoIt;');
  14358. Add(' end;');
  14359. Add('implementation');
  14360. Add('procedure tcar.doit;');
  14361. Add('begin');
  14362. Add(' Intern;');
  14363. Add(' Intern();');
  14364. Add(' Intern2;');
  14365. Add(' Intern2();');
  14366. Add('end;');
  14367. Add('var Obj: TCar;');
  14368. Add('begin');
  14369. Add(' obj.intern;');
  14370. Add(' obj.intern();');
  14371. Add(' obj.intern2;');
  14372. Add(' obj.intern2();');
  14373. Add(' obj.doit;');
  14374. Add(' obj.doit();');
  14375. Add(' with obj do begin');
  14376. Add(' Intern;');
  14377. Add(' Intern();');
  14378. Add(' Intern2;');
  14379. Add(' Intern2();');
  14380. Add(' end;');
  14381. ConvertUnit;
  14382. CheckSource('TestClass_ExternalMethod',
  14383. LinesToStr([
  14384. 'var $impl = $mod.$impl;',
  14385. 'rtl.createClass(this, "TCar", pas.unit2.TObject, function () {',
  14386. ' this.DoIt = function () {',
  14387. ' this.$DoIntern();',
  14388. ' this.$DoIntern();',
  14389. ' this.$DoIntern2();',
  14390. ' this.$DoIntern2();',
  14391. ' };',
  14392. ' });',
  14393. '']),
  14394. LinesToStr([ // this.$init
  14395. '$impl.Obj.$DoIntern();',
  14396. '$impl.Obj.$DoIntern();',
  14397. '$impl.Obj.$DoIntern2();',
  14398. '$impl.Obj.$DoIntern2();',
  14399. '$impl.Obj.DoIt();',
  14400. '$impl.Obj.DoIt();',
  14401. 'var $with = $impl.Obj;',
  14402. '$with.$DoIntern();',
  14403. '$with.$DoIntern();',
  14404. '$with.$DoIntern2();',
  14405. '$with.$DoIntern2();',
  14406. '']),
  14407. LinesToStr([ // implementation
  14408. '$impl.Obj = null;',
  14409. '']) );
  14410. end;
  14411. procedure TTestModule.TestClass_ExternalVirtualNameMismatchFail;
  14412. begin
  14413. StartProgram(false);
  14414. Add('type');
  14415. Add(' TObject = class');
  14416. Add(' procedure DoIt; virtual; external name ''Foo'';');
  14417. Add(' end;');
  14418. Add('begin');
  14419. SetExpectedPasResolverError('Virtual method name must match external',
  14420. nVirtualMethodNameMustMatchExternal);
  14421. ConvertProgram;
  14422. end;
  14423. procedure TTestModule.TestClass_ExternalOverrideFail;
  14424. begin
  14425. StartProgram(false);
  14426. Add('type');
  14427. Add(' TObject = class');
  14428. Add(' procedure DoIt; virtual; external name ''DoIt'';');
  14429. Add(' end;');
  14430. Add(' TCar = class');
  14431. Add(' procedure DoIt; override; external name ''DoIt'';');
  14432. Add(' end;');
  14433. Add('begin');
  14434. SetExpectedPasResolverError('Invalid procedure modifier override,external',
  14435. nInvalidXModifierY);
  14436. ConvertProgram;
  14437. end;
  14438. procedure TTestModule.TestClass_ExternalVar;
  14439. begin
  14440. AddModuleWithIntfImplSrc('unit2.pas',
  14441. LinesToStr([
  14442. '{$modeswitch externalclass}',
  14443. 'type',
  14444. ' TObject = class',
  14445. ' public',
  14446. ' Intern: longint external name ''$Intern'';',
  14447. ' Bracket: longint external name ''["A B"]'';',
  14448. ' end;',
  14449. '']),
  14450. LinesToStr([
  14451. '']));
  14452. StartUnit(true);
  14453. Add([
  14454. 'interface',
  14455. 'uses unit2;',
  14456. '{$modeswitch externalclass}',
  14457. 'type',
  14458. ' TCar = class(tobject)',
  14459. ' public',
  14460. ' Intern2: longint external name ''$Intern2'';',
  14461. ' procedure DoIt;',
  14462. ' end;',
  14463. 'implementation',
  14464. 'procedure tcar.doit;',
  14465. 'begin',
  14466. ' Intern:=Intern+1;',
  14467. ' Intern2:=Intern2+2;',
  14468. ' Bracket:=Bracket+3;',
  14469. 'end;',
  14470. 'var Obj: TCar;',
  14471. 'begin',
  14472. ' obj.intern:=obj.intern+1;',
  14473. ' obj.intern2:=obj.intern2+2;',
  14474. ' obj.Bracket:=obj.Bracket+3;',
  14475. ' with obj do begin',
  14476. ' intern:=intern+1;',
  14477. ' intern2:=intern2+2;',
  14478. ' Bracket:=Bracket+3;',
  14479. ' end;']);
  14480. ConvertUnit;
  14481. CheckSource('TestClass_ExternalVar',
  14482. LinesToStr([
  14483. 'var $impl = $mod.$impl;',
  14484. 'rtl.createClass(this, "TCar", pas.unit2.TObject, function () {',
  14485. ' this.DoIt = function () {',
  14486. ' this.$Intern = this.$Intern + 1;',
  14487. ' this.$Intern2 = this.$Intern2 + 2;',
  14488. ' this["A B"] = this["A B"] + 3;',
  14489. ' };',
  14490. ' });',
  14491. '']),
  14492. LinesToStr([
  14493. '$impl.Obj.$Intern = $impl.Obj.$Intern + 1;',
  14494. '$impl.Obj.$Intern2 = $impl.Obj.$Intern2 + 2;',
  14495. '$impl.Obj["A B"] = $impl.Obj["A B"] + 3;',
  14496. 'var $with = $impl.Obj;',
  14497. '$with.$Intern = $with.$Intern + 1;',
  14498. '$with.$Intern2 = $with.$Intern2 + 2;',
  14499. '$with["A B"] = $with["A B"] + 3;',
  14500. '']),
  14501. LinesToStr([ // implementation
  14502. '$impl.Obj = null;',
  14503. '']));
  14504. end;
  14505. procedure TTestModule.TestClass_Const;
  14506. begin
  14507. StartProgram(false);
  14508. Add([
  14509. 'type',
  14510. ' integer = longint;',
  14511. ' TClass = class of TObject;',
  14512. ' TObject = class',
  14513. ' public',
  14514. ' const cI: integer = 3;',
  14515. ' procedure DoIt;',
  14516. ' class procedure DoMore;',
  14517. ' end;',
  14518. 'procedure tobject.doit;',
  14519. 'begin',
  14520. ' if cI=4 then;',
  14521. ' if 5=cI then;',
  14522. ' if Self.cI=6 then;',
  14523. ' if 7=Self.cI then;',
  14524. ' with Self do begin',
  14525. ' if cI=11 then;',
  14526. ' if 12=cI then;',
  14527. ' end;',
  14528. 'end;',
  14529. 'class procedure tobject.domore;',
  14530. 'begin',
  14531. ' if cI=8 then;',
  14532. ' if Self.cI=9 then;',
  14533. ' if 10=cI then;',
  14534. ' if 11=Self.cI then;',
  14535. ' with Self do begin',
  14536. ' if cI=13 then;',
  14537. ' if 14=cI then;',
  14538. ' end;',
  14539. 'end;',
  14540. 'var',
  14541. ' Obj: TObject;',
  14542. ' Cla: TClass;',
  14543. 'begin',
  14544. ' if TObject.cI=21 then ;',
  14545. ' if Obj.cI=22 then ;',
  14546. ' if Cla.cI=23 then ;',
  14547. ' with obj do if ci=24 then;',
  14548. ' with TObject do if ci=25 then;',
  14549. ' with Cla do if ci=26 then;']);
  14550. ConvertProgram;
  14551. CheckSource('TestClass_Const',
  14552. LinesToStr([
  14553. 'rtl.createClass(this, "TObject", null, function () {',
  14554. ' this.cI = 3;',
  14555. ' this.$init = function () {',
  14556. ' };',
  14557. ' this.$final = function () {',
  14558. ' };',
  14559. ' this.DoIt = function () {',
  14560. ' if (this.cI === 4) ;',
  14561. ' if (5 === this.cI) ;',
  14562. ' if (this.cI === 6) ;',
  14563. ' if (7 === this.cI) ;',
  14564. ' if (this.cI === 11) ;',
  14565. ' if (12 === this.cI) ;',
  14566. ' };',
  14567. ' this.DoMore = function () {',
  14568. ' if (this.cI === 8) ;',
  14569. ' if (this.cI === 9) ;',
  14570. ' if (10 === this.cI) ;',
  14571. ' if (11 === this.cI) ;',
  14572. ' if (this.cI === 13) ;',
  14573. ' if (14 === this.cI) ;',
  14574. ' };',
  14575. '});',
  14576. 'this.Obj = null;',
  14577. 'this.Cla = null;',
  14578. '']),
  14579. LinesToStr([
  14580. 'if ($mod.TObject.cI === 21) ;',
  14581. 'if ($mod.Obj.cI === 22) ;',
  14582. 'if ($mod.Cla.cI === 23) ;',
  14583. 'var $with = $mod.Obj;',
  14584. 'if ($with.cI === 24) ;',
  14585. 'var $with1 = $mod.TObject;',
  14586. 'if ($with1.cI === 25) ;',
  14587. 'var $with2 = $mod.Cla;',
  14588. 'if ($with2.cI === 26) ;',
  14589. '']));
  14590. end;
  14591. procedure TTestModule.TestClass_ConstEnum;
  14592. begin
  14593. StartProgram(false);
  14594. Add([
  14595. 'type',
  14596. ' TEnum = (red,blue);',
  14597. ' TObject = class',
  14598. ' end;',
  14599. ' TAnimal = class',
  14600. ' public',
  14601. ' type TSubEnum = (light,dark);',
  14602. ' const a = high(TEnum);',
  14603. ' const b = high(TSubEnum);',
  14604. ' end;',
  14605. ' TBird = class(TAnimal)',
  14606. ' public',
  14607. ' const c = high(TEnum);',
  14608. ' const d = high(TSubEnum);',
  14609. ' end;',
  14610. ' TAnt = class',
  14611. ' public',
  14612. ' const e = high(TEnum);',
  14613. ' const f = high(TBird.TSubEnum);',
  14614. ' end;',
  14615. 'begin',
  14616. '']);
  14617. ConvertProgram;
  14618. CheckSource('TestClass_ConstEnum',
  14619. LinesToStr([
  14620. 'this.TEnum = {',
  14621. ' "0": "red",',
  14622. ' red: 0,',
  14623. ' "1": "blue",',
  14624. ' blue: 1',
  14625. '};',
  14626. 'rtl.createClass(this, "TObject", null, function () {',
  14627. ' this.$init = function () {',
  14628. ' };',
  14629. ' this.$final = function () {',
  14630. ' };',
  14631. '});',
  14632. 'rtl.createClass(this, "TAnimal", this.TObject, function () {',
  14633. ' this.TSubEnum = {',
  14634. ' "0": "light",',
  14635. ' light: 0,',
  14636. ' "1": "dark",',
  14637. ' dark: 1',
  14638. ' };',
  14639. ' this.a = $mod.TEnum.blue;',
  14640. ' this.b = this.TSubEnum.dark;',
  14641. '});',
  14642. 'rtl.createClass(this, "TBird", this.TAnimal, function () {',
  14643. ' this.c = $mod.TEnum.blue;',
  14644. ' this.d = this.TSubEnum.dark;',
  14645. '});',
  14646. 'rtl.createClass(this, "TAnt", this.TObject, function () {',
  14647. ' this.e = $mod.TEnum.blue;',
  14648. ' this.f = $mod.TAnimal.TSubEnum.dark;',
  14649. '});',
  14650. '']),
  14651. LinesToStr([
  14652. '']));
  14653. end;
  14654. procedure TTestModule.TestClass_LocalConstDuplicate_Prg;
  14655. begin
  14656. StartProgram(false);
  14657. Add([
  14658. 'type',
  14659. ' TObject = class',
  14660. ' const cI: longint = 3;',
  14661. ' procedure Fly;',
  14662. ' procedure Run;',
  14663. ' end;',
  14664. ' TBird = class',
  14665. ' procedure Go;',
  14666. ' end;',
  14667. 'procedure tobject.fly;',
  14668. 'const cI: word = 4;',
  14669. 'begin',
  14670. ' if cI=Self.cI then ;',
  14671. 'end;',
  14672. 'procedure tobject.run;',
  14673. 'const cI: word = 5;',
  14674. 'begin',
  14675. ' if cI=Self.cI then ;',
  14676. 'end;',
  14677. 'procedure tbird.go;',
  14678. 'const cI: word = 6;',
  14679. 'begin',
  14680. ' if cI=Self.cI then ;',
  14681. 'end;',
  14682. 'begin',
  14683. '']);
  14684. ConvertProgram;
  14685. CheckSource('TestClass_LocalConstDuplicate_Prg',
  14686. LinesToStr([
  14687. 'rtl.createClass(this, "TObject", null, function () {',
  14688. ' this.cI = 3;',
  14689. ' this.$init = function () {',
  14690. ' };',
  14691. ' this.$final = function () {',
  14692. ' };',
  14693. ' var cI$1 = 4;',
  14694. ' this.Fly = function () {',
  14695. ' if (cI$1 === this.cI) ;',
  14696. ' };',
  14697. ' var cI$2 = 5;',
  14698. ' this.Run = function () {',
  14699. ' if (cI$2 === this.cI) ;',
  14700. ' };',
  14701. '});',
  14702. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  14703. ' var cI$3 = 6;',
  14704. ' this.Go = function () {',
  14705. ' if (cI$3 === this.cI) ;',
  14706. ' };',
  14707. '});',
  14708. '']),
  14709. LinesToStr([
  14710. '']));
  14711. end;
  14712. procedure TTestModule.TestClass_LocalConstDuplicate_Unit;
  14713. begin
  14714. StartUnit(false);
  14715. Add([
  14716. 'interface',
  14717. 'type',
  14718. ' TObject = class',
  14719. ' const cI: longint = 3;',
  14720. ' procedure Fly;',
  14721. ' procedure Run;',
  14722. ' end;',
  14723. ' TBird = class',
  14724. ' procedure Go;',
  14725. ' end;',
  14726. 'implementation',
  14727. 'procedure tobject.fly;',
  14728. 'const cI: word = 4;',
  14729. 'begin',
  14730. ' if cI=Self.cI then ;',
  14731. 'end;',
  14732. 'procedure tobject.run;',
  14733. 'const cI: word = 5;',
  14734. 'begin',
  14735. ' if cI=Self.cI then ;',
  14736. 'end;',
  14737. 'procedure tbird.go;',
  14738. 'const cI: word = 6;',
  14739. 'begin',
  14740. ' if cI=Self.cI then ;',
  14741. 'end;',
  14742. '']);
  14743. ConvertUnit;
  14744. CheckSource('TestClass_LocalConstDuplicate_Unit',
  14745. LinesToStr([
  14746. 'rtl.createClass(this, "TObject", null, function () {',
  14747. ' this.cI = 3;',
  14748. ' this.$init = function () {',
  14749. ' };',
  14750. ' this.$final = function () {',
  14751. ' };',
  14752. ' var cI$1 = 4;',
  14753. ' this.Fly = function () {',
  14754. ' if (cI$1 === this.cI) ;',
  14755. ' };',
  14756. ' var cI$2 = 5;',
  14757. ' this.Run = function () {',
  14758. ' if (cI$2 === this.cI) ;',
  14759. ' };',
  14760. '});',
  14761. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  14762. ' var cI$3 = 6;',
  14763. ' this.Go = function () {',
  14764. ' if (cI$3 === this.cI) ;',
  14765. ' };',
  14766. '});',
  14767. '']),
  14768. '',
  14769. '');
  14770. end;
  14771. procedure TTestModule.TestClass_LocalVarSelfFail;
  14772. begin
  14773. StartProgram(false);
  14774. Add([
  14775. 'type',
  14776. ' TObject = class',
  14777. ' constructor Create;',
  14778. ' end;',
  14779. 'constructor tobject.create;',
  14780. 'var self: longint;',
  14781. 'begin',
  14782. 'end',
  14783. 'begin',
  14784. '']);
  14785. SetExpectedPasResolverError('Duplicate identifier "self" at (0)',nDuplicateIdentifier);
  14786. ConvertProgram;
  14787. end;
  14788. procedure TTestModule.TestClass_ArgSelfFail;
  14789. begin
  14790. StartProgram(false);
  14791. Add([
  14792. 'type',
  14793. ' TObject = class',
  14794. ' procedure DoIt(Self: longint);',
  14795. ' end;',
  14796. 'procedure tobject.doit(self: longint);',
  14797. 'begin',
  14798. 'end',
  14799. 'begin',
  14800. '']);
  14801. SetExpectedPasResolverError('Duplicate identifier "Self" at test1.pp(5,24)',nDuplicateIdentifier);
  14802. ConvertProgram;
  14803. end;
  14804. procedure TTestModule.TestClass_NestedProcSelf;
  14805. begin
  14806. StartProgram(false);
  14807. Add([
  14808. 'type',
  14809. ' TObject = class',
  14810. ' Key: longint;',
  14811. ' class var State: longint;',
  14812. ' procedure DoIt;',
  14813. ' function GetSize: longint; virtual; abstract;',
  14814. ' procedure SetSize(Value: longint); virtual; abstract;',
  14815. ' property Size: longint read GetSize write SetSize;',
  14816. ' end;',
  14817. 'procedure tobject.doit;',
  14818. ' procedure Sub;',
  14819. ' begin',
  14820. ' key:=key+2;',
  14821. ' self.key:=self.key+3;',
  14822. ' state:=state+4;',
  14823. ' self.state:=self.state+5;',
  14824. ' tobject.state:=tobject.state+6;',
  14825. ' size:=size+7;',
  14826. ' self.size:=self.size+8;',
  14827. ' end;',
  14828. 'begin',
  14829. ' sub;',
  14830. ' key:=key+12;',
  14831. ' self.key:=self.key+13;',
  14832. ' state:=state+14;',
  14833. ' self.state:=self.state+15;',
  14834. ' tobject.state:=tobject.state+16;',
  14835. ' size:=size+17;',
  14836. ' self.size:=self.size+18;',
  14837. 'end;',
  14838. 'begin',
  14839. '']);
  14840. ConvertProgram;
  14841. CheckSource('TestClass_NestedProcSelf',
  14842. LinesToStr([ // statements
  14843. 'rtl.createClass(this, "TObject", null, function () {',
  14844. ' this.State = 0;',
  14845. ' this.$init = function () {',
  14846. ' this.Key = 0;',
  14847. ' };',
  14848. ' this.$final = function () {',
  14849. ' };',
  14850. ' this.DoIt = function () {',
  14851. ' var $Self = this;',
  14852. ' function Sub() {',
  14853. ' $Self.Key = $Self.Key + 2;',
  14854. ' $Self.Key = $Self.Key + 3;',
  14855. ' $mod.TObject.State = $Self.State + 4;',
  14856. ' $mod.TObject.State = $Self.State + 5;',
  14857. ' $mod.TObject.State = $mod.TObject.State + 6;',
  14858. ' $Self.SetSize($Self.GetSize() + 7);',
  14859. ' $Self.SetSize($Self.GetSize() + 8);',
  14860. ' };',
  14861. ' Sub();',
  14862. ' this.Key = this.Key + 12;',
  14863. ' $Self.Key = $Self.Key + 13;',
  14864. ' $mod.TObject.State = this.State + 14;',
  14865. ' $mod.TObject.State = $Self.State + 15;',
  14866. ' $mod.TObject.State = $mod.TObject.State + 16;',
  14867. ' this.SetSize(this.GetSize() + 17);',
  14868. ' $Self.SetSize($Self.GetSize() + 18);',
  14869. ' };',
  14870. '});',
  14871. '']),
  14872. LinesToStr([ // $mod.$main
  14873. '']));
  14874. end;
  14875. procedure TTestModule.TestClass_NestedProcSelf2;
  14876. begin
  14877. StartProgram(false);
  14878. Add([
  14879. 'type',
  14880. ' TObject = class',
  14881. ' Key: longint;',
  14882. ' class var State: longint;',
  14883. ' function GetSize: longint; virtual; abstract;',
  14884. ' procedure SetSize(Value: longint); virtual; abstract;',
  14885. ' property Size: longint read GetSize write SetSize;',
  14886. ' end;',
  14887. ' TBird = class',
  14888. ' procedure DoIt;',
  14889. ' end;',
  14890. 'procedure tbird.doit;',
  14891. ' procedure Sub;',
  14892. ' begin',
  14893. ' key:=key+2;',
  14894. ' self.key:=self.key+3;',
  14895. ' state:=state+4;',
  14896. ' self.state:=self.state+5;',
  14897. ' tobject.state:=tobject.state+6;',
  14898. ' size:=size+7;',
  14899. ' self.size:=self.size+8;',
  14900. ' end;',
  14901. 'begin',
  14902. ' sub;',
  14903. ' key:=key+12;',
  14904. ' self.key:=self.key+13;',
  14905. ' state:=state+14;',
  14906. ' self.state:=self.state+15;',
  14907. ' tobject.state:=tobject.state+16;',
  14908. ' size:=size+17;',
  14909. ' self.size:=self.size+18;',
  14910. 'end;',
  14911. 'begin',
  14912. '']);
  14913. ConvertProgram;
  14914. CheckSource('TestClass_NestedProcSelf2',
  14915. LinesToStr([ // statements
  14916. 'rtl.createClass(this, "TObject", null, function () {',
  14917. ' this.State = 0;',
  14918. ' this.$init = function () {',
  14919. ' this.Key = 0;',
  14920. ' };',
  14921. ' this.$final = function () {',
  14922. ' };',
  14923. '});',
  14924. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  14925. ' this.DoIt = function () {',
  14926. ' var $Self = this;',
  14927. ' function Sub() {',
  14928. ' $Self.Key = $Self.Key + 2;',
  14929. ' $Self.Key = $Self.Key + 3;',
  14930. ' $mod.TObject.State = $Self.State + 4;',
  14931. ' $mod.TObject.State = $Self.State + 5;',
  14932. ' $mod.TObject.State = $mod.TObject.State + 6;',
  14933. ' $Self.SetSize($Self.GetSize() + 7);',
  14934. ' $Self.SetSize($Self.GetSize() + 8);',
  14935. ' };',
  14936. ' Sub();',
  14937. ' this.Key = this.Key + 12;',
  14938. ' $Self.Key = $Self.Key + 13;',
  14939. ' $mod.TObject.State = this.State + 14;',
  14940. ' $mod.TObject.State = $Self.State + 15;',
  14941. ' $mod.TObject.State = $mod.TObject.State + 16;',
  14942. ' this.SetSize(this.GetSize() + 17);',
  14943. ' $Self.SetSize($Self.GetSize() + 18);',
  14944. ' };',
  14945. '});',
  14946. '']),
  14947. LinesToStr([ // $mod.$main
  14948. '']));
  14949. end;
  14950. procedure TTestModule.TestClass_NestedProcClassSelf;
  14951. begin
  14952. StartProgram(false);
  14953. Add([
  14954. 'type',
  14955. ' TObject = class',
  14956. ' class var State: longint;',
  14957. ' class procedure DoIt;',
  14958. ' class function GetSize: longint; virtual; abstract;',
  14959. ' class procedure SetSize(Value: longint); virtual; abstract;',
  14960. ' class property Size: longint read GetSize write SetSize;',
  14961. ' end;',
  14962. 'class procedure tobject.doit;',
  14963. ' procedure Sub;',
  14964. ' begin',
  14965. ' state:=state+2;',
  14966. ' self.state:=self.state+3;',
  14967. ' tobject.state:=tobject.state+4;',
  14968. ' size:=size+5;',
  14969. ' self.size:=self.size+6;',
  14970. ' tobject.size:=tobject.size+7;',
  14971. ' end;',
  14972. 'begin',
  14973. ' sub;',
  14974. ' state:=state+12;',
  14975. ' self.state:=self.state+13;',
  14976. ' tobject.state:=tobject.state+14;',
  14977. ' size:=size+15;',
  14978. ' self.size:=self.size+16;',
  14979. ' tobject.size:=tobject.size+17;',
  14980. 'end;',
  14981. 'begin',
  14982. '']);
  14983. ConvertProgram;
  14984. CheckSource('TestClass_NestedProcClassSelf',
  14985. LinesToStr([ // statements
  14986. 'rtl.createClass(this, "TObject", null, function () {',
  14987. ' this.State = 0;',
  14988. ' this.$init = function () {',
  14989. ' };',
  14990. ' this.$final = function () {',
  14991. ' };',
  14992. ' this.DoIt = function () {',
  14993. ' var $Self = this;',
  14994. ' function Sub() {',
  14995. ' $mod.TObject.State = $Self.State + 2;',
  14996. ' $mod.TObject.State = $Self.State + 3;',
  14997. ' $mod.TObject.State = $mod.TObject.State + 4;',
  14998. ' $Self.SetSize($Self.GetSize() + 5);',
  14999. ' $Self.SetSize($Self.GetSize() + 6);',
  15000. ' $mod.TObject.SetSize($mod.TObject.GetSize() + 7);',
  15001. ' };',
  15002. ' Sub();',
  15003. ' $mod.TObject.State = this.State + 12;',
  15004. ' $mod.TObject.State = $Self.State + 13;',
  15005. ' $mod.TObject.State = $mod.TObject.State + 14;',
  15006. ' this.SetSize(this.GetSize() + 15);',
  15007. ' $Self.SetSize($Self.GetSize() + 16);',
  15008. ' $mod.TObject.SetSize($mod.TObject.GetSize() + 17);',
  15009. ' };',
  15010. '});',
  15011. '']),
  15012. LinesToStr([ // $mod.$main
  15013. '']));
  15014. end;
  15015. procedure TTestModule.TestClass_NestedProcCallInherited;
  15016. begin
  15017. StartProgram(false);
  15018. Add([
  15019. 'type',
  15020. ' TObject = class',
  15021. ' function DoIt(k: boolean): longint; virtual;',
  15022. ' end;',
  15023. ' TBird = class',
  15024. ' function DoIt(k: boolean): longint; override;',
  15025. ' end;',
  15026. 'function tobject.doit(k: boolean): longint;',
  15027. 'begin',
  15028. 'end;',
  15029. 'function tbird.doit(k: boolean): longint;',
  15030. ' procedure Sub;',
  15031. ' begin',
  15032. ' inherited DoIt(true);',
  15033. //' if inherited DoIt(false)=4 then ;',
  15034. ' end;',
  15035. 'begin',
  15036. ' Sub;',
  15037. ' inherited;',
  15038. ' inherited DoIt(true);',
  15039. //' if inherited DoIt(false)=14 then ;',
  15040. 'end;',
  15041. 'begin',
  15042. '']);
  15043. ConvertProgram;
  15044. CheckSource('TestClass_NestedProcCallInherited',
  15045. LinesToStr([ // statements
  15046. 'rtl.createClass(this, "TObject", null, function () {',
  15047. ' this.$init = function () {',
  15048. ' };',
  15049. ' this.$final = function () {',
  15050. ' };',
  15051. ' this.DoIt = function (k) {',
  15052. ' var Result = 0;',
  15053. ' return Result;',
  15054. ' };',
  15055. '});',
  15056. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15057. ' this.DoIt = function (k) {',
  15058. ' var $Self = this;',
  15059. ' var Result = 0;',
  15060. ' function Sub() {',
  15061. ' $mod.TObject.DoIt.call($Self, true);',
  15062. ' };',
  15063. ' Sub();',
  15064. ' $mod.TObject.DoIt.apply(this, arguments);',
  15065. ' $mod.TObject.DoIt.call(this, true);',
  15066. ' return Result;',
  15067. ' };',
  15068. '});',
  15069. '']),
  15070. LinesToStr([ // $mod.$main
  15071. '']));
  15072. end;
  15073. procedure TTestModule.TestClass_TObjectFree;
  15074. begin
  15075. StartProgram(false);
  15076. Add([
  15077. 'type',
  15078. ' TObject = class',
  15079. ' Obj: tobject;',
  15080. ' procedure Free;',
  15081. ' procedure Release;',
  15082. ' end;',
  15083. 'procedure tobject.free;',
  15084. 'begin',
  15085. 'end;',
  15086. 'procedure tobject.release;',
  15087. 'begin',
  15088. ' free;',
  15089. ' if true then free;',
  15090. 'end;',
  15091. 'function DoIt(o: tobject): tobject;',
  15092. 'var l: tobject;',
  15093. 'begin',
  15094. ' o.free;',
  15095. ' o.free();',
  15096. ' l.free;',
  15097. ' l.free();',
  15098. ' o.obj.free;',
  15099. ' o.obj.free();',
  15100. ' with o do obj.free;',
  15101. ' with o do obj.free();',
  15102. ' result.Free;',
  15103. ' result.Free();',
  15104. 'end;',
  15105. 'var o: tobject;',
  15106. ' a: array of tobject;',
  15107. 'begin',
  15108. ' o.free;',
  15109. ' o.obj.free;',
  15110. ' a[1+2].free;',
  15111. '']);
  15112. ConvertProgram;
  15113. CheckSource('TestClass_TObjectFree',
  15114. LinesToStr([ // statements
  15115. 'rtl.createClass(this, "TObject", null, function () {',
  15116. ' this.$init = function () {',
  15117. ' this.Obj = null;',
  15118. ' };',
  15119. ' this.$final = function () {',
  15120. ' this.Obj = undefined;',
  15121. ' };',
  15122. ' this.Free = function () {',
  15123. ' };',
  15124. ' this.Release = function () {',
  15125. ' this.Free();',
  15126. ' if (true) this.Free();',
  15127. ' };',
  15128. '});',
  15129. 'this.DoIt = function (o) {',
  15130. ' var Result = null;',
  15131. ' var l = null;',
  15132. ' o = rtl.freeLoc(o);',
  15133. ' o = rtl.freeLoc(o);',
  15134. ' l = rtl.freeLoc(l);',
  15135. ' l = rtl.freeLoc(l);',
  15136. ' rtl.free(o, "Obj");',
  15137. ' rtl.free(o, "Obj");',
  15138. ' rtl.free(o, "Obj");',
  15139. ' rtl.free(o, "Obj");',
  15140. ' Result = rtl.freeLoc(Result);',
  15141. ' Result = rtl.freeLoc(Result);',
  15142. ' return Result;',
  15143. '};',
  15144. 'this.o = null;',
  15145. 'this.a = [];',
  15146. '']),
  15147. LinesToStr([ // $mod.$main
  15148. 'rtl.free($mod, "o");',
  15149. 'rtl.free($mod.o, "Obj");',
  15150. 'rtl.free($mod.a, 1 + 2);',
  15151. '']));
  15152. end;
  15153. procedure TTestModule.TestClass_TObjectFree_VarArg;
  15154. begin
  15155. StartProgram(false);
  15156. Add([
  15157. 'type',
  15158. ' TObject = class',
  15159. ' Obj: tobject;',
  15160. ' procedure Free;',
  15161. ' end;',
  15162. 'procedure tobject.free;',
  15163. 'begin',
  15164. 'end;',
  15165. 'procedure DoIt(var o: tobject);',
  15166. 'begin',
  15167. ' o.free;',
  15168. ' o.free();',
  15169. 'end;',
  15170. 'begin',
  15171. '']);
  15172. ConvertProgram;
  15173. CheckSource('TestClass_TObjectFree_VarArg',
  15174. LinesToStr([ // statements
  15175. 'rtl.createClass(this, "TObject", null, function () {',
  15176. ' this.$init = function () {',
  15177. ' this.Obj = null;',
  15178. ' };',
  15179. ' this.$final = function () {',
  15180. ' this.Obj = undefined;',
  15181. ' };',
  15182. ' this.Free = function () {',
  15183. ' };',
  15184. '});',
  15185. 'this.DoIt = function (o) {',
  15186. ' o.set(rtl.freeLoc(o.get()));',
  15187. ' o.set(rtl.freeLoc(o.get()));',
  15188. '};',
  15189. '']),
  15190. LinesToStr([ // $mod.$main
  15191. '']));
  15192. end;
  15193. procedure TTestModule.TestClass_TObjectFreeNewInstance;
  15194. begin
  15195. StartProgram(false);
  15196. Add([
  15197. 'type',
  15198. ' TObject = class',
  15199. ' constructor Create;',
  15200. ' procedure Free;',
  15201. ' end;',
  15202. 'constructor TObject.Create; begin end;',
  15203. 'procedure tobject.free; begin end;',
  15204. 'begin',
  15205. ' with tobject.create do free;',
  15206. '']);
  15207. ConvertProgram;
  15208. CheckSource('TestClass_TObjectFreeNewInstance',
  15209. LinesToStr([ // statements
  15210. 'rtl.createClass(this, "TObject", null, function () {',
  15211. ' this.$init = function () {',
  15212. ' };',
  15213. ' this.$final = function () {',
  15214. ' };',
  15215. ' this.Create = function () {',
  15216. ' return this;',
  15217. ' };',
  15218. ' this.Free = function () {',
  15219. ' };',
  15220. '});',
  15221. '']),
  15222. LinesToStr([ // $mod.$main
  15223. 'var $with = $mod.TObject.$create("Create");',
  15224. '$with=rtl.freeLoc($with);',
  15225. '']));
  15226. end;
  15227. procedure TTestModule.TestClass_TObjectFreeLowerCase;
  15228. begin
  15229. StartProgram(false);
  15230. Add([
  15231. 'type',
  15232. ' TObject = class',
  15233. ' destructor Destroy;',
  15234. ' procedure Free;',
  15235. ' end;',
  15236. 'destructor TObject.Destroy; begin end;',
  15237. 'procedure tobject.free; begin end;',
  15238. 'var o: tobject;',
  15239. 'begin',
  15240. ' o.free;',
  15241. '']);
  15242. Converter.UseLowerCase:=true;
  15243. ConvertProgram;
  15244. CheckSource('TestClass_TObjectFreeLowerCase',
  15245. LinesToStr([ // statements
  15246. 'rtl.createClass(this, "tobject", null, function () {',
  15247. ' this.$init = function () {',
  15248. ' };',
  15249. ' this.$final = function () {',
  15250. ' };',
  15251. ' rtl.tObjectDestroy = "destroy";',
  15252. ' this.destroy = function () {',
  15253. ' };',
  15254. ' this.free = function () {',
  15255. ' };',
  15256. '});',
  15257. 'this.o = null;',
  15258. '']),
  15259. LinesToStr([ // $mod.$main
  15260. 'rtl.free($mod, "o");',
  15261. '']));
  15262. end;
  15263. procedure TTestModule.TestClass_TObjectFreeFunctionFail;
  15264. begin
  15265. StartProgram(false);
  15266. Add([
  15267. 'type',
  15268. ' TObject = class',
  15269. ' procedure Free;',
  15270. ' function GetObj: tobject; virtual; abstract;',
  15271. ' end;',
  15272. 'procedure tobject.free;',
  15273. 'begin',
  15274. 'end;',
  15275. 'var o: tobject;',
  15276. 'begin',
  15277. ' o.getobj.free;',
  15278. '']);
  15279. SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
  15280. ConvertProgram;
  15281. end;
  15282. procedure TTestModule.TestClass_TObjectFreePropertyFail;
  15283. begin
  15284. StartProgram(false);
  15285. Add([
  15286. 'type',
  15287. ' TObject = class',
  15288. ' procedure Free;',
  15289. ' FObj: TObject;',
  15290. ' property Obj: tobject read FObj write FObj;',
  15291. ' end;',
  15292. 'procedure tobject.free;',
  15293. 'begin',
  15294. 'end;',
  15295. 'var o: tobject;',
  15296. 'begin',
  15297. ' o.obj.free;',
  15298. '']);
  15299. SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
  15300. ConvertProgram;
  15301. end;
  15302. procedure TTestModule.TestClass_ForIn;
  15303. begin
  15304. StartProgram(false);
  15305. Add([
  15306. 'type',
  15307. ' TObject = class end;',
  15308. ' TItem = TObject;',
  15309. ' TEnumerator = class',
  15310. ' FCurrent: TItem;',
  15311. ' property Current: TItem read FCurrent;',
  15312. ' function MoveNext: boolean;',
  15313. ' end;',
  15314. ' TBird = class',
  15315. ' function GetEnumerator: TEnumerator;',
  15316. ' end;',
  15317. 'function TEnumerator.MoveNext: boolean;',
  15318. 'begin',
  15319. 'end;',
  15320. 'function TBird.GetEnumerator: TEnumerator;',
  15321. 'begin',
  15322. 'end;',
  15323. 'var',
  15324. ' b: TBird;',
  15325. ' i, i2: TItem;',
  15326. 'begin',
  15327. ' for i in b do i2:=i;']);
  15328. ConvertProgram;
  15329. CheckSource('TestClass_ForIn',
  15330. LinesToStr([ // statements
  15331. 'rtl.createClass(this, "TObject", null, function () {',
  15332. ' this.$init = function () {',
  15333. ' };',
  15334. ' this.$final = function () {',
  15335. ' };',
  15336. '});',
  15337. 'rtl.createClass(this, "TEnumerator", this.TObject, function () {',
  15338. ' this.$init = function () {',
  15339. ' $mod.TObject.$init.call(this);',
  15340. ' this.FCurrent = null;',
  15341. ' };',
  15342. ' this.$final = function () {',
  15343. ' this.FCurrent = undefined;',
  15344. ' $mod.TObject.$final.call(this);',
  15345. ' };',
  15346. ' this.MoveNext = function () {',
  15347. ' var Result = false;',
  15348. ' return Result;',
  15349. ' };',
  15350. '});',
  15351. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15352. ' this.GetEnumerator = function () {',
  15353. ' var Result = null;',
  15354. ' return Result;',
  15355. ' };',
  15356. '});',
  15357. 'this.b = null;',
  15358. 'this.i = null;',
  15359. 'this.i2 = null;'
  15360. ]),
  15361. LinesToStr([ // $mod.$main
  15362. 'var $in = $mod.b.GetEnumerator();',
  15363. 'try {',
  15364. ' while ($in.MoveNext()){',
  15365. ' $mod.i = $in.FCurrent;',
  15366. ' $mod.i2 = $mod.i;',
  15367. ' }',
  15368. '} finally {',
  15369. ' $in = rtl.freeLoc($in)',
  15370. '};',
  15371. '']));
  15372. end;
  15373. procedure TTestModule.TestClass_DispatchMessage;
  15374. begin
  15375. StartProgram(false);
  15376. Add([
  15377. 'type',
  15378. ' TObject = class',
  15379. ' {$DispatchField DispInt}',
  15380. ' procedure Dispatch(var Msg); virtual; abstract;',
  15381. ' {$DispatchStrField DispStr}',
  15382. ' procedure DispatchStr(var Msg); virtual; abstract;',
  15383. ' end;',
  15384. ' THopMsg = record',
  15385. ' DispInt: longint;',
  15386. ' end;',
  15387. ' TPutMsg = record',
  15388. ' DispStr: string;',
  15389. ' end;',
  15390. ' TBird = class',
  15391. ' procedure Fly(var Msg); virtual; abstract; message 2;',
  15392. ' procedure Run; overload; virtual; abstract;',
  15393. ' procedure Run(var Msg); overload; message ''Fast'';',
  15394. ' procedure Hop(var Msg: THopMsg); virtual; abstract; message 3;',
  15395. ' procedure Put(var Msg: TPutMsg); virtual; abstract; message ''foo'';',
  15396. ' end;',
  15397. 'procedure TBird.Run(var Msg);',
  15398. 'begin',
  15399. 'end;',
  15400. 'begin',
  15401. '']);
  15402. ConvertProgram;
  15403. CheckSource('TestClass_Message',
  15404. LinesToStr([ // statements
  15405. 'rtl.createClass(this, "TObject", null, function () {',
  15406. ' this.$init = function () {',
  15407. ' };',
  15408. ' this.$final = function () {',
  15409. ' };',
  15410. '});',
  15411. 'rtl.recNewT(this, "THopMsg", function () {',
  15412. ' this.DispInt = 0;',
  15413. ' this.$eq = function (b) {',
  15414. ' return this.DispInt === b.DispInt;',
  15415. ' };',
  15416. ' this.$assign = function (s) {',
  15417. ' this.DispInt = s.DispInt;',
  15418. ' return this;',
  15419. ' };',
  15420. '});',
  15421. 'rtl.recNewT(this, "TPutMsg", function () {',
  15422. ' this.DispStr = "";',
  15423. ' this.$eq = function (b) {',
  15424. ' return this.DispStr === b.DispStr;',
  15425. ' };',
  15426. ' this.$assign = function (s) {',
  15427. ' this.DispStr = s.DispStr;',
  15428. ' return this;',
  15429. ' };',
  15430. '});',
  15431. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15432. ' this.Run$1 = function (Msg) {',
  15433. ' };',
  15434. ' this.$msgint = {',
  15435. ' "2": "Fly",',
  15436. ' "3": "Hop"',
  15437. ' };',
  15438. ' this.$msgstr = {',
  15439. ' Fast: "Run$1",',
  15440. ' foo: "Put"',
  15441. ' };',
  15442. '});',
  15443. '']),
  15444. LinesToStr([ // $mod.$main
  15445. '']));
  15446. end;
  15447. procedure TTestModule.TestClass_Message_DuplicateIntFail;
  15448. begin
  15449. StartProgram(false);
  15450. Add([
  15451. 'type',
  15452. ' TObject = class',
  15453. ' procedure Fly(var Msg); virtual; abstract; message 3;',
  15454. ' procedure Run(var Msg); virtual; abstract; message 1+2;',
  15455. ' end;',
  15456. 'begin',
  15457. '']);
  15458. SetExpectedPasResolverError('Duplicate message id "3" at test1.pp(5,56)',nDuplicateMessageIdXAtY);
  15459. ConvertProgram;
  15460. end;
  15461. procedure TTestModule.TestClass_DispatchMessage_WrongFieldNameFail;
  15462. begin
  15463. StartProgram(false);
  15464. Add([
  15465. 'type',
  15466. ' TObject = class',
  15467. ' {$dispatchfield Msg}',
  15468. ' procedure Dispatch(var Msg); virtual; abstract;',
  15469. ' end;',
  15470. ' TFlyMsg = record',
  15471. ' FlyId: longint;',
  15472. ' end;',
  15473. ' TBird = class',
  15474. ' procedure Fly(var Msg: TFlyMsg); virtual; abstract; message 3;',
  15475. ' end;',
  15476. 'begin',
  15477. '']);
  15478. ConvertProgram;
  15479. CheckHint(mtWarning,nDispatchRequiresX,'Dispatch requires record field "Msg"');
  15480. end;
  15481. procedure TTestModule.TestClassOf_Create;
  15482. begin
  15483. StartProgram(false);
  15484. Add('type');
  15485. Add(' TObject = class');
  15486. Add(' constructor Create;');
  15487. Add(' end;');
  15488. Add(' TClass = class of TObject;');
  15489. Add('constructor tobject.create; begin end;');
  15490. Add('var');
  15491. Add(' Obj: tobject;');
  15492. Add(' C: tclass;');
  15493. Add('begin');
  15494. Add(' obj:=C.create;');
  15495. Add(' with c do obj:=create;');
  15496. ConvertProgram;
  15497. CheckSource('TestClassOf_Create',
  15498. LinesToStr([ // statements
  15499. 'rtl.createClass(this, "TObject", null, function () {',
  15500. ' this.$init = function () {',
  15501. ' };',
  15502. ' this.$final = function () {',
  15503. ' };',
  15504. ' this.Create = function () {',
  15505. ' return this;',
  15506. ' };',
  15507. '});',
  15508. 'this.Obj = null;',
  15509. 'this.C = null;'
  15510. ]),
  15511. LinesToStr([ // $mod.$main
  15512. '$mod.Obj = $mod.C.$create("Create");',
  15513. 'var $with = $mod.C;',
  15514. '$mod.Obj = $with.$create("Create");',
  15515. '']));
  15516. end;
  15517. procedure TTestModule.TestClassOf_Call;
  15518. begin
  15519. StartProgram(false);
  15520. Add('type');
  15521. Add(' TObject = class');
  15522. Add(' class procedure DoIt;');
  15523. Add(' end;');
  15524. Add(' TClass = class of TObject;');
  15525. Add('class procedure tobject.doit; begin end;');
  15526. Add('var');
  15527. Add(' C: tclass;');
  15528. Add('begin');
  15529. Add(' c.doit;');
  15530. Add(' with c do doit;');
  15531. ConvertProgram;
  15532. CheckSource('TestClassOf_Call',
  15533. LinesToStr([ // statements
  15534. 'rtl.createClass(this, "TObject", null, function () {',
  15535. ' this.$init = function () {',
  15536. ' };',
  15537. ' this.$final = function () {',
  15538. ' };',
  15539. ' this.DoIt = function () {',
  15540. ' };',
  15541. '});',
  15542. 'this.C = null;'
  15543. ]),
  15544. LinesToStr([ // $mod.$main
  15545. '$mod.C.DoIt();',
  15546. 'var $with = $mod.C;',
  15547. '$with.DoIt();',
  15548. '']));
  15549. end;
  15550. procedure TTestModule.TestClassOf_Assign;
  15551. begin
  15552. StartProgram(false);
  15553. Add('type');
  15554. Add(' TClass = class of TObject;');
  15555. Add(' TObject = class');
  15556. Add(' ClassType: TClass; ');
  15557. Add(' end;');
  15558. Add('var');
  15559. Add(' Obj: tobject;');
  15560. Add(' C: tclass;');
  15561. Add('begin');
  15562. Add(' c:=nil;');
  15563. Add(' c:=obj.classtype;');
  15564. ConvertProgram;
  15565. CheckSource('TestClassOf_Assign',
  15566. LinesToStr([ // statements
  15567. 'rtl.createClass(this, "TObject", null, function () {',
  15568. ' this.$init = function () {',
  15569. ' this.ClassType = null;',
  15570. ' };',
  15571. ' this.$final = function () {',
  15572. ' this.ClassType = undefined;',
  15573. ' };',
  15574. '});',
  15575. 'this.Obj = null;',
  15576. 'this.C = null;'
  15577. ]),
  15578. LinesToStr([ // $mod.$main
  15579. '$mod.C = null;',
  15580. '$mod.C = $mod.Obj.ClassType;',
  15581. '']));
  15582. end;
  15583. procedure TTestModule.TestClassOf_Is;
  15584. begin
  15585. StartProgram(false);
  15586. Add('type');
  15587. Add(' TClass = class of TObject;');
  15588. Add(' TObject = class');
  15589. Add(' end;');
  15590. Add(' TCar = class');
  15591. Add(' end;');
  15592. Add(' TCars = class of TCar;');
  15593. Add('var');
  15594. Add(' Obj: tobject;');
  15595. Add(' C: tclass;');
  15596. Add(' Cars: tcars;');
  15597. Add('begin');
  15598. Add(' if c is tcar then ;');
  15599. Add(' if c is tcars then ;');
  15600. ConvertProgram;
  15601. CheckSource('TestClassOf_Is',
  15602. LinesToStr([ // statements
  15603. 'rtl.createClass(this, "TObject", null, function () {',
  15604. ' this.$init = function () {',
  15605. ' };',
  15606. ' this.$final = function () {',
  15607. ' };',
  15608. '});',
  15609. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  15610. '});',
  15611. 'this.Obj = null;',
  15612. 'this.C = null;',
  15613. 'this.Cars = null;'
  15614. ]),
  15615. LinesToStr([ // $mod.$main
  15616. 'if(rtl.is($mod.C,$mod.TCar));',
  15617. 'if(rtl.is($mod.C,$mod.TCar));',
  15618. '']));
  15619. end;
  15620. procedure TTestModule.TestClassOf_Compare;
  15621. begin
  15622. StartProgram(false);
  15623. Add('type');
  15624. Add(' TClass = class of TObject;');
  15625. Add(' TObject = class');
  15626. Add(' ClassType: TClass; ');
  15627. Add(' end;');
  15628. Add('var');
  15629. Add(' b: boolean;');
  15630. Add(' Obj: tobject;');
  15631. Add(' C: tclass;');
  15632. Add('begin');
  15633. Add(' b:=c=nil;');
  15634. Add(' b:=nil=c;');
  15635. Add(' b:=c=obj.classtype;');
  15636. Add(' b:=obj.classtype=c;');
  15637. Add(' b:=c=TObject;');
  15638. Add(' b:=TObject=c;');
  15639. Add(' b:=c<>nil;');
  15640. Add(' b:=nil<>c;');
  15641. Add(' b:=c<>obj.classtype;');
  15642. Add(' b:=obj.classtype<>c;');
  15643. Add(' b:=c<>TObject;');
  15644. Add(' b:=TObject<>c;');
  15645. ConvertProgram;
  15646. CheckSource('TestClassOf_Compare',
  15647. LinesToStr([ // statements
  15648. 'rtl.createClass(this, "TObject", null, function () {',
  15649. ' this.$init = function () {',
  15650. ' this.ClassType = null;',
  15651. ' };',
  15652. ' this.$final = function () {',
  15653. ' this.ClassType = undefined;',
  15654. ' };',
  15655. '});',
  15656. 'this.b = false;',
  15657. 'this.Obj = null;',
  15658. 'this.C = null;'
  15659. ]),
  15660. LinesToStr([ // $mod.$main
  15661. '$mod.b = $mod.C === null;',
  15662. '$mod.b = null === $mod.C;',
  15663. '$mod.b = $mod.C === $mod.Obj.ClassType;',
  15664. '$mod.b = $mod.Obj.ClassType === $mod.C;',
  15665. '$mod.b = $mod.C === $mod.TObject;',
  15666. '$mod.b = $mod.TObject === $mod.C;',
  15667. '$mod.b = $mod.C !== null;',
  15668. '$mod.b = null !== $mod.C;',
  15669. '$mod.b = $mod.C !== $mod.Obj.ClassType;',
  15670. '$mod.b = $mod.Obj.ClassType !== $mod.C;',
  15671. '$mod.b = $mod.C !== $mod.TObject;',
  15672. '$mod.b = $mod.TObject !== $mod.C;',
  15673. '']));
  15674. end;
  15675. procedure TTestModule.TestClassOf_ClassVar;
  15676. begin
  15677. StartProgram(false);
  15678. Add('type');
  15679. Add(' TObject = class');
  15680. Add(' class var id: longint;');
  15681. Add(' end;');
  15682. Add(' TClass = class of TObject;');
  15683. Add('var');
  15684. Add(' C: tclass;');
  15685. Add('begin');
  15686. Add(' C.id:=C.id;');
  15687. ConvertProgram;
  15688. CheckSource('TestClassOf_ClassVar',
  15689. LinesToStr([ // statements
  15690. 'rtl.createClass(this, "TObject", null, function () {',
  15691. ' this.id = 0;',
  15692. ' this.$init = function () {',
  15693. ' };',
  15694. ' this.$final = function () {',
  15695. ' };',
  15696. '});',
  15697. 'this.C = null;'
  15698. ]),
  15699. LinesToStr([ // $mod.$main
  15700. '$mod.TObject.id = $mod.C.id;',
  15701. '']));
  15702. end;
  15703. procedure TTestModule.TestClassOf_ClassMethod;
  15704. begin
  15705. StartProgram(false);
  15706. Add('type');
  15707. Add(' TObject = class');
  15708. Add(' class function DoIt(i: longint = 0): longint;');
  15709. Add(' end;');
  15710. Add(' TClass = class of TObject;');
  15711. Add('class function tobject.doit(i: longint = 0): longint; begin end;');
  15712. Add('var');
  15713. Add(' i: longint;');
  15714. Add(' C: tclass;');
  15715. Add('begin');
  15716. Add(' C.DoIt;');
  15717. Add(' C.DoIt();');
  15718. Add(' i:=C.DoIt;');
  15719. Add(' i:=C.DoIt();');
  15720. ConvertProgram;
  15721. CheckSource('TestClassOf_ClassMethod',
  15722. LinesToStr([ // statements
  15723. 'rtl.createClass(this, "TObject", null, function () {',
  15724. ' this.$init = function () {',
  15725. ' };',
  15726. ' this.$final = function () {',
  15727. ' };',
  15728. ' this.DoIt = function (i) {',
  15729. ' var Result = 0;',
  15730. ' return Result;',
  15731. ' };',
  15732. '});',
  15733. 'this.i = 0;',
  15734. 'this.C = null;'
  15735. ]),
  15736. LinesToStr([ // $mod.$main
  15737. '$mod.C.DoIt(0);',
  15738. '$mod.C.DoIt(0);',
  15739. '$mod.i = $mod.C.DoIt(0);',
  15740. '$mod.i = $mod.C.DoIt(0);',
  15741. '']));
  15742. end;
  15743. procedure TTestModule.TestClassOf_ClassProperty;
  15744. begin
  15745. StartProgram(false);
  15746. Add([
  15747. 'type',
  15748. ' TObject = class',
  15749. ' class var FA: longint;',
  15750. ' class function GetA: longint;',
  15751. ' class procedure SetA(Value: longint);',
  15752. ' class property pA: longint read fa write fa;',
  15753. ' class property pB: longint read geta write seta;',
  15754. ' end;',
  15755. ' TObjectClass = class of tobject;',
  15756. 'class function tobject.geta: longint; begin end;',
  15757. 'class procedure tobject.seta(value: longint); begin end;',
  15758. 'var',
  15759. ' b: boolean;',
  15760. ' Obj: tobject;',
  15761. ' Cla: tobjectclass;',
  15762. 'begin',
  15763. ' obj.pa:=obj.pa;',
  15764. ' obj.pb:=obj.pb;',
  15765. ' b:=obj.pa=4;',
  15766. ' b:=obj.pb=obj.pb;',
  15767. ' b:=5=obj.pa;',
  15768. ' cla.pa:=6;',
  15769. ' cla.pa:=cla.pa;',
  15770. ' cla.pb:=cla.pb;',
  15771. ' b:=cla.pa=7;',
  15772. ' b:=cla.pb=cla.pb;',
  15773. ' b:=8=cla.pa;',
  15774. ' tobject.pa:=9;',
  15775. ' tobject.pb:=tobject.pb;',
  15776. ' b:=tobject.pa=10;',
  15777. ' b:=11=tobject.pa;',
  15778. '']);
  15779. ConvertProgram;
  15780. CheckSource('TestClassOf_ClassProperty',
  15781. LinesToStr([ // statements
  15782. 'rtl.createClass(this, "TObject", null, function () {',
  15783. ' this.FA = 0;',
  15784. ' this.$init = function () {',
  15785. ' };',
  15786. ' this.$final = function () {',
  15787. ' };',
  15788. ' this.GetA = function () {',
  15789. ' var Result = 0;',
  15790. ' return Result;',
  15791. ' };',
  15792. ' this.SetA = function (Value) {',
  15793. ' };',
  15794. '});',
  15795. 'this.b = false;',
  15796. 'this.Obj = null;',
  15797. 'this.Cla = null;'
  15798. ]),
  15799. LinesToStr([ // $mod.$main
  15800. '$mod.TObject.FA = $mod.Obj.FA;',
  15801. '$mod.Obj.$class.SetA($mod.Obj.$class.GetA());',
  15802. '$mod.b = $mod.Obj.FA === 4;',
  15803. '$mod.b = $mod.Obj.$class.GetA() === $mod.Obj.$class.GetA();',
  15804. '$mod.b = 5 === $mod.Obj.FA;',
  15805. '$mod.TObject.FA = 6;',
  15806. '$mod.TObject.FA = $mod.Cla.FA;',
  15807. '$mod.Cla.SetA($mod.Cla.GetA());',
  15808. '$mod.b = $mod.Cla.FA === 7;',
  15809. '$mod.b = $mod.Cla.GetA() === $mod.Cla.GetA();',
  15810. '$mod.b = 8 === $mod.Cla.FA;',
  15811. '$mod.TObject.FA = 9;',
  15812. '$mod.TObject.SetA($mod.TObject.GetA());',
  15813. '$mod.b = $mod.TObject.FA === 10;',
  15814. '$mod.b = 11 === $mod.TObject.FA;',
  15815. '']));
  15816. end;
  15817. procedure TTestModule.TestClassOf_ClassMethodSelf;
  15818. begin
  15819. StartProgram(false);
  15820. Add('type');
  15821. Add(' TObject = class');
  15822. Add(' class var GlobalId: longint;');
  15823. Add(' class procedure ProcA;');
  15824. Add(' end;');
  15825. Add('class procedure tobject.proca;');
  15826. Add('var b: boolean;');
  15827. Add('begin');
  15828. Add(' b:=self=nil;');
  15829. Add(' b:=self.globalid=3;');
  15830. Add(' b:=4=self.globalid;');
  15831. Add(' self.globalid:=5;');
  15832. Add(' self.proca;');
  15833. Add('end;');
  15834. Add('begin');
  15835. ConvertProgram;
  15836. CheckSource('TestClassOf_ClassMethodSelf',
  15837. LinesToStr([ // statements
  15838. 'rtl.createClass(this, "TObject", null, function () {',
  15839. ' this.GlobalId = 0;',
  15840. ' this.$init = function () {',
  15841. ' };',
  15842. ' this.$final = function () {',
  15843. ' };',
  15844. ' this.ProcA = function () {',
  15845. ' var b = false;',
  15846. ' b = this === null;',
  15847. ' b = this.GlobalId === 3;',
  15848. ' b = 4 === this.GlobalId;',
  15849. ' $mod.TObject.GlobalId = 5;',
  15850. ' this.ProcA();',
  15851. ' };',
  15852. '});'
  15853. ]),
  15854. LinesToStr([ // $mod.$main
  15855. '']));
  15856. end;
  15857. procedure TTestModule.TestClassOf_TypeCast;
  15858. begin
  15859. StartProgram(false);
  15860. Add('type');
  15861. Add(' TObject = class');
  15862. Add(' class procedure {#TObject_DoIt}DoIt;');
  15863. Add(' end;');
  15864. Add(' TClass = class of TObject;');
  15865. Add(' TMobile = class');
  15866. Add(' class procedure {#TMobile_DoIt}DoIt;');
  15867. Add(' end;');
  15868. Add(' TMobileClass = class of TMobile;');
  15869. Add(' TCar = class(TMobile)');
  15870. Add(' class procedure {#TCar_DoIt}DoIt;');
  15871. Add(' end;');
  15872. Add(' TCarClass = class of TCar;');
  15873. Add('class procedure TObject.DoIt;');
  15874. Add('begin');
  15875. Add(' TClass(Self).{@TObject_DoIt}DoIt;');
  15876. Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
  15877. Add('end;');
  15878. Add('class procedure TMobile.DoIt;');
  15879. Add('begin');
  15880. Add(' TClass(Self).{@TObject_DoIt}DoIt;');
  15881. Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
  15882. Add(' TCarClass(Self).{@TCar_DoIt}DoIt;');
  15883. Add('end;');
  15884. Add('class procedure TCar.DoIt; begin end;');
  15885. Add('var');
  15886. Add(' ObjC: TClass;');
  15887. Add(' MobileC: TMobileClass;');
  15888. Add(' CarC: TCarClass;');
  15889. Add('begin');
  15890. Add(' ObjC.{@TObject_DoIt}DoIt;');
  15891. Add(' MobileC.{@TMobile_DoIt}DoIt;');
  15892. Add(' CarC.{@TCar_DoIt}DoIt;');
  15893. Add(' TClass(ObjC).{@TObject_DoIt}DoIt;');
  15894. Add(' TMobileClass(ObjC).{@TMobile_DoIt}DoIt;');
  15895. Add(' TCarClass(ObjC).{@TCar_DoIt}DoIt;');
  15896. Add(' TClass(MobileC).{@TObject_DoIt}DoIt;');
  15897. Add(' TMobileClass(MobileC).{@TMobile_DoIt}DoIt;');
  15898. Add(' TCarClass(MobileC).{@TCar_DoIt}DoIt;');
  15899. Add(' TClass(CarC).{@TObject_DoIt}DoIt;');
  15900. Add(' TMobileClass(CarC).{@TMobile_DoIt}DoIt;');
  15901. Add(' TCarClass(CarC).{@TCar_DoIt}DoIt;');
  15902. ConvertProgram;
  15903. CheckSource('TestClassOf_TypeCast',
  15904. LinesToStr([ // statements
  15905. 'rtl.createClass(this, "TObject", null, function () {',
  15906. ' this.$init = function () {',
  15907. ' };',
  15908. ' this.$final = function () {',
  15909. ' };',
  15910. ' this.DoIt = function () {',
  15911. ' this.DoIt();',
  15912. ' this.DoIt$1();',
  15913. ' };',
  15914. '});',
  15915. 'rtl.createClass(this, "TMobile", this.TObject, function () {',
  15916. ' this.DoIt$1 = function () {',
  15917. ' this.DoIt();',
  15918. ' this.DoIt$1();',
  15919. ' this.DoIt$2();',
  15920. ' };',
  15921. '});',
  15922. 'rtl.createClass(this, "TCar", this.TMobile, function () {',
  15923. ' this.DoIt$2 = function () {',
  15924. ' };',
  15925. '});',
  15926. 'this.ObjC = null;',
  15927. 'this.MobileC = null;',
  15928. 'this.CarC = null;',
  15929. '']),
  15930. LinesToStr([ // $mod.$main
  15931. '$mod.ObjC.DoIt();',
  15932. '$mod.MobileC.DoIt$1();',
  15933. '$mod.CarC.DoIt$2();',
  15934. '$mod.ObjC.DoIt();',
  15935. '$mod.ObjC.DoIt$1();',
  15936. '$mod.ObjC.DoIt$2();',
  15937. '$mod.MobileC.DoIt();',
  15938. '$mod.MobileC.DoIt$1();',
  15939. '$mod.MobileC.DoIt$2();',
  15940. '$mod.CarC.DoIt();',
  15941. '$mod.CarC.DoIt$1();',
  15942. '$mod.CarC.DoIt$2();',
  15943. '']));
  15944. end;
  15945. procedure TTestModule.TestClassOf_ImplicitFunctionCall;
  15946. begin
  15947. StartProgram(false);
  15948. Add('type');
  15949. Add(' TObject = class');
  15950. Add(' function CurNow: longint; ');
  15951. Add(' class function Now: longint; ');
  15952. Add(' end;');
  15953. Add('function TObject.CurNow: longint; begin end;');
  15954. Add('class function TObject.Now: longint; begin end;');
  15955. Add('var');
  15956. Add(' Obj: tobject;');
  15957. Add(' vI: longint;');
  15958. Add('begin');
  15959. Add(' obj.curnow;');
  15960. Add(' vi:=obj.curnow;');
  15961. Add(' tobject.now;');
  15962. Add(' vi:=tobject.now;');
  15963. ConvertProgram;
  15964. CheckSource('TestClassOf_ImplicitFunctionCall',
  15965. LinesToStr([ // statements
  15966. 'rtl.createClass(this, "TObject", null, function () {',
  15967. ' this.$init = function () {',
  15968. ' };',
  15969. ' this.$final = function () {',
  15970. ' };',
  15971. ' this.CurNow = function () {',
  15972. ' var Result = 0;',
  15973. ' return Result;',
  15974. ' };',
  15975. ' this.Now = function () {',
  15976. ' var Result = 0;',
  15977. ' return Result;',
  15978. ' };',
  15979. '});',
  15980. 'this.Obj = null;',
  15981. 'this.vI = 0;',
  15982. '']),
  15983. LinesToStr([ // $mod.$main
  15984. '$mod.Obj.CurNow();',
  15985. '$mod.vI = $mod.Obj.CurNow();',
  15986. '$mod.TObject.Now();',
  15987. '$mod.vI = $mod.TObject.Now();',
  15988. '']));
  15989. end;
  15990. procedure TTestModule.TestClassOf_Const;
  15991. begin
  15992. StartProgram(false);
  15993. Add([
  15994. 'type',
  15995. ' TObject = class',
  15996. ' end;',
  15997. ' TBird = TObject;',
  15998. ' TBirds = class of TBird;',
  15999. ' TEagles = TBirds;',
  16000. ' THawk = class(TBird);',
  16001. 'const',
  16002. ' Hawk: TEagles = THawk;',
  16003. ' DefaultBirdClasses : Array [1..2] of TEagles = (',
  16004. ' TBird,',
  16005. ' THawk',
  16006. ' );',
  16007. 'begin']);
  16008. ConvertProgram;
  16009. CheckSource('TestClassOf_Const',
  16010. LinesToStr([ // statements
  16011. 'rtl.createClass(this, "TObject", null, function () {',
  16012. ' this.$init = function () {',
  16013. ' };',
  16014. ' this.$final = function () {',
  16015. ' };',
  16016. '});',
  16017. 'rtl.createClass(this, "THawk", this.TObject, function () {',
  16018. '});',
  16019. 'this.Hawk = this.THawk;',
  16020. 'this.DefaultBirdClasses = [this.TObject, this.THawk];',
  16021. '']),
  16022. LinesToStr([ // $mod.$main
  16023. '']));
  16024. end;
  16025. procedure TTestModule.TestNestedClass_Alias;
  16026. begin
  16027. WithTypeInfo:=true;
  16028. StartProgram(false);
  16029. Add([
  16030. 'type',
  16031. ' TObject = class',
  16032. ' type TNested = type longint;',
  16033. ' end;',
  16034. 'type TAlias = type tobject.tnested;',
  16035. 'var i: tobject.tnested = 3;',
  16036. 'var j: TAlias = 4;',
  16037. 'begin',
  16038. ' if typeinfo(TAlias)=nil then ;',
  16039. ' if typeinfo(tobject.tnested)=nil then ;',
  16040. '']);
  16041. ConvertProgram;
  16042. CheckSource('TestNestedClass_Alias',
  16043. LinesToStr([ // statements
  16044. 'rtl.createClass(this, "TObject", null, function () {',
  16045. ' $mod.$rtti.$inherited("TObject.TNested", rtl.longint, {});',
  16046. ' this.$init = function () {',
  16047. ' };',
  16048. ' this.$final = function () {',
  16049. ' };',
  16050. '});',
  16051. 'this.$rtti.$inherited("TAlias", this.$rtti["TObject.TNested"], {});',
  16052. 'this.i = 3;',
  16053. 'this.j = 4;',
  16054. '']),
  16055. LinesToStr([ // $mod.$main
  16056. 'if ($mod.$rtti["TAlias"] === null) ;',
  16057. 'if ($mod.$rtti["TObject.TNested"] === null) ;',
  16058. '']));
  16059. end;
  16060. procedure TTestModule.TestNestedClass_Record;
  16061. begin
  16062. WithTypeInfo:=true;
  16063. StartProgram(false);
  16064. Add([
  16065. 'type',
  16066. ' TObject = class',
  16067. ' type TPoint = record',
  16068. ' x,y: byte;',
  16069. ' end;',
  16070. ' procedure DoIt(t: TPoint);',
  16071. ' end;',
  16072. 'procedure tobject.DoIt(t: TPoint);',
  16073. 'var p: TPoint;',
  16074. 'begin',
  16075. ' t.x:=t.y;',
  16076. ' p:=t;',
  16077. 'end;',
  16078. 'var',
  16079. ' p: tobject.tpoint = (x:2; y:4);',
  16080. ' o: TObject;',
  16081. 'begin',
  16082. ' p:=p;',
  16083. ' o.doit(p);',
  16084. '']);
  16085. ConvertProgram;
  16086. CheckSource('TestNestedClass_Record',
  16087. LinesToStr([ // statements
  16088. 'rtl.createClass(this, "TObject", null, function () {',
  16089. ' rtl.recNewT(this, "TPoint", function () {',
  16090. ' this.x = 0;',
  16091. ' this.y = 0;',
  16092. ' this.$eq = function (b) {',
  16093. ' return (this.x === b.x) && (this.y === b.y);',
  16094. ' };',
  16095. ' this.$assign = function (s) {',
  16096. ' this.x = s.x;',
  16097. ' this.y = s.y;',
  16098. ' return this;',
  16099. ' };',
  16100. ' var $r = $mod.$rtti.$Record("TObject.TPoint", {});',
  16101. ' $r.addField("x", rtl.byte);',
  16102. ' $r.addField("y", rtl.byte);',
  16103. ' });',
  16104. ' this.$init = function () {',
  16105. ' };',
  16106. ' this.$final = function () {',
  16107. ' };',
  16108. ' this.DoIt = function (t) {',
  16109. ' var p = this.TPoint.$new();',
  16110. ' t.x = t.y;',
  16111. ' p.$assign(t);',
  16112. ' };',
  16113. '});',
  16114. 'this.p = this.TObject.TPoint.$clone({',
  16115. ' x: 2,',
  16116. ' y: 4',
  16117. '});',
  16118. 'this.o = null;',
  16119. '']),
  16120. LinesToStr([ // $mod.$main
  16121. '$mod.p.$assign($mod.p);',
  16122. '$mod.o.DoIt($mod.TObject.TPoint.$clone($mod.p));',
  16123. '']));
  16124. end;
  16125. procedure TTestModule.TestNestedClass_Class;
  16126. begin
  16127. WithTypeInfo:=true;
  16128. StartProgram(false);
  16129. Add([
  16130. 'type',
  16131. ' TObject = class end;',
  16132. ' TBird = class',
  16133. ' type TLeg = class',
  16134. ' FId: longint;',
  16135. ' constructor Create;',
  16136. ' function Create(i: longint): TLeg;',
  16137. ' end;',
  16138. ' function DoIt(b: TBird): Tleg;',
  16139. ' end;',
  16140. 'constructor tbird.tleg.create;',
  16141. 'begin',
  16142. ' FId:=3;',
  16143. 'end;',
  16144. 'function tbird.tleg.Create(i: longint): TLeg;',
  16145. 'begin',
  16146. ' Create;',
  16147. ' Result:=TLeg.Create;',
  16148. ' Result:=TBird.TLeg.Create;',
  16149. ' Result:=Create(3);',
  16150. ' FId:=i;',
  16151. 'end;',
  16152. 'function tbird.DoIt(b: tbird): tleg;',
  16153. 'begin',
  16154. ' Result.Create;',
  16155. ' Result:=TLeg.Create;',
  16156. ' Result:=TBird.TLeg.Create;',
  16157. ' Result:=Result.Create(3);',
  16158. 'end;',
  16159. 'var',
  16160. ' b: Tbird.tleg;',
  16161. 'begin',
  16162. ' b.Create;',
  16163. ' b:=TBird.TLeg.Create;',
  16164. ' b:=b.Create(3);',
  16165. '']);
  16166. ConvertProgram;
  16167. CheckSource('TestNestedClass_Class',
  16168. LinesToStr([ // statements
  16169. 'rtl.createClass(this, "TObject", null, function () {',
  16170. ' this.$init = function () {',
  16171. ' };',
  16172. ' this.$final = function () {',
  16173. ' };',
  16174. '});',
  16175. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16176. ' rtl.createClass(this, "TLeg", $mod.TObject, function () {',
  16177. ' this.$init = function () {',
  16178. ' $mod.TObject.$init.call(this);',
  16179. ' this.FId = 0;',
  16180. ' };',
  16181. ' this.Create = function () {',
  16182. ' this.FId = 3;',
  16183. ' return this;',
  16184. ' };',
  16185. ' this.Create$1 = function (i) {',
  16186. ' var Result = null;',
  16187. ' this.Create();',
  16188. ' Result = $mod.TBird.TLeg.$create("Create");',
  16189. ' Result = $mod.TBird.TLeg.$create("Create");',
  16190. ' Result = this.Create$1(3);',
  16191. ' this.FId = i;',
  16192. ' return Result;',
  16193. ' };',
  16194. ' }, "TBird.TLeg");',
  16195. ' this.DoIt = function (b) {',
  16196. ' var Result = null;',
  16197. ' Result.Create();',
  16198. ' Result = this.TLeg.$create("Create");',
  16199. ' Result = $mod.TBird.TLeg.$create("Create");',
  16200. ' Result = Result.Create$1(3);',
  16201. ' return Result;',
  16202. ' };',
  16203. '});',
  16204. 'this.b = null;',
  16205. '']),
  16206. LinesToStr([ // $mod.$main
  16207. '$mod.b.Create();',
  16208. '$mod.b = $mod.TBird.TLeg.$create("Create");',
  16209. '$mod.b = $mod.b.Create$1(3);',
  16210. '']));
  16211. end;
  16212. procedure TTestModule.TestExternalClass_Var;
  16213. begin
  16214. StartProgram(false);
  16215. Add([
  16216. '{$modeswitch externalclass}',
  16217. 'type',
  16218. ' TExtA = class external name ''ExtObj''',
  16219. ' Id: longint external name ''$Id'';',
  16220. ' B: longint;',
  16221. ' end;',
  16222. 'var Obj: TExtA;',
  16223. 'begin',
  16224. ' obj.id:=obj.id+1;',
  16225. ' obj.B:=obj.B+1;']);
  16226. ConvertProgram;
  16227. CheckSource('TestExternalClass_Var',
  16228. LinesToStr([ // statements
  16229. 'this.Obj = null;',
  16230. '']),
  16231. LinesToStr([ // $mod.$main
  16232. '$mod.Obj.$Id = $mod.Obj.$Id + 1;',
  16233. '$mod.Obj.B = $mod.Obj.B + 1;',
  16234. '']));
  16235. end;
  16236. procedure TTestModule.TestExternalClass_Const;
  16237. begin
  16238. StartProgram(false);
  16239. Add([
  16240. '{$modeswitch externalclass}',
  16241. 'type',
  16242. ' TExtA = class external name ''ExtObj''',
  16243. ' const Two: longint = 2;',
  16244. ' const Three = 3;',
  16245. ' const Id: longint;',
  16246. ' end;',
  16247. ' TExtB = class external name ''ExtB''',
  16248. ' A: TExtA;',
  16249. ' end;',
  16250. 'var',
  16251. ' A: texta;',
  16252. ' B: textb;',
  16253. ' i: longint;',
  16254. 'begin',
  16255. ' i:=a.two;',
  16256. ' i:=texta.two;',
  16257. ' i:=a.three;',
  16258. ' i:=texta.three;',
  16259. ' i:=a.id;',
  16260. ' i:=texta.id;',
  16261. '']);
  16262. ConvertProgram;
  16263. CheckSource('TestExternalClass_Const',
  16264. LinesToStr([ // statements
  16265. 'this.A = null;',
  16266. 'this.B = null;',
  16267. 'this.i = 0;',
  16268. '']),
  16269. LinesToStr([ // $mod.$main
  16270. '$mod.i = 2;',
  16271. '$mod.i = 2;',
  16272. '$mod.i = 3;',
  16273. '$mod.i = 3;',
  16274. '$mod.i = $mod.A.Id;',
  16275. '$mod.i = ExtObj.Id;',
  16276. '']));
  16277. end;
  16278. procedure TTestModule.TestExternalClass_Dollar;
  16279. begin
  16280. StartProgram(false);
  16281. Add([
  16282. '{$modeswitch externalclass}',
  16283. 'type',
  16284. ' TExtA = class external name ''$''',
  16285. ' Id: longint external name ''$'';',
  16286. ' function Bla(i: longint): longint; external name ''$'';',
  16287. ' end;',
  16288. 'function dollar(k: longint): longint; external name ''$'';',
  16289. 'var Obj: TExtA;',
  16290. 'begin',
  16291. ' dollar(1);',
  16292. ' obj.id:=obj.id+2;',
  16293. ' obj.Bla(3);',
  16294. '']);
  16295. ConvertProgram;
  16296. CheckSource('TestExternalClass_Dollar',
  16297. LinesToStr([ // statements
  16298. 'this.Obj = null;',
  16299. '']),
  16300. LinesToStr([ // $mod.$main
  16301. '$(1);',
  16302. '$mod.Obj.$ = $mod.Obj.$ + 2;',
  16303. '$mod.Obj.$(3);',
  16304. '']));
  16305. end;
  16306. procedure TTestModule.TestExternalClass_DuplicateVarFail;
  16307. begin
  16308. StartProgram(false);
  16309. Add('{$modeswitch externalclass}');
  16310. Add('type');
  16311. Add(' TExtA = class external name ''ExtA''');
  16312. Add(' Id: longint external name ''$Id'';');
  16313. Add(' end;');
  16314. Add(' TExtB = class external ''lib'' name ''ExtB''(TExtA)');
  16315. Add(' Id: longint;');
  16316. Add(' end;');
  16317. Add('begin');
  16318. SetExpectedPasResolverError('Duplicate identifier "Id" at test1.pp(6,5)',nDuplicateIdentifier);
  16319. ConvertProgram;
  16320. end;
  16321. procedure TTestModule.TestExternalClass_Method;
  16322. begin
  16323. StartProgram(false);
  16324. Add(['{$modeswitch externalclass}',
  16325. 'type',
  16326. ' TExtA = class external name ''ExtObj''',
  16327. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  16328. ' procedure DoSome(Id: longint = 1);',
  16329. ' end;',
  16330. 'var Obj: texta;',
  16331. 'begin',
  16332. ' obj.doit;',
  16333. ' obj.doit();',
  16334. ' obj.doit(2);',
  16335. ' with obj do begin',
  16336. ' doit;',
  16337. ' doit();',
  16338. ' doit(3);',
  16339. ' end;']);
  16340. ConvertProgram;
  16341. CheckSource('TestExternalClass_Method',
  16342. LinesToStr([ // statements
  16343. 'this.Obj = null;',
  16344. '']),
  16345. LinesToStr([ // $mod.$main
  16346. '$mod.Obj.$Execute(1);',
  16347. '$mod.Obj.$Execute(1);',
  16348. '$mod.Obj.$Execute(2);',
  16349. 'var $with = $mod.Obj;',
  16350. '$with.$Execute(1);',
  16351. '$with.$Execute(1);',
  16352. '$with.$Execute(3);',
  16353. '']));
  16354. end;
  16355. procedure TTestModule.TestExternalClass_ClassMethod;
  16356. begin
  16357. StartProgram(false);
  16358. Add([
  16359. '{$modeswitch externalclass}',
  16360. 'type',
  16361. ' TExtA = class external name ''ExtObj''',
  16362. ' class procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  16363. ' end;',
  16364. ' TExtB = TExtA;',
  16365. 'var p: Pointer;',
  16366. 'begin',
  16367. ' texta.doit;',
  16368. ' texta.doit();',
  16369. ' texta.doit(2);',
  16370. ' p:[email protected];',
  16371. ' with texta do begin',
  16372. ' doit;',
  16373. ' doit();',
  16374. ' doit(3);',
  16375. ' p:=@DoIt;',
  16376. ' end;',
  16377. ' textb.doit;',
  16378. ' textb.doit();',
  16379. ' textb.doit(4);',
  16380. ' with textb do begin',
  16381. ' doit;',
  16382. ' doit();',
  16383. ' doit(5);',
  16384. ' end;',
  16385. '']);
  16386. ConvertProgram;
  16387. CheckSource('TestExternalClass_ClassMethod',
  16388. LinesToStr([ // statements
  16389. 'this.p = null;',
  16390. '']),
  16391. LinesToStr([ // $mod.$main
  16392. 'ExtObj.$Execute(1);',
  16393. 'ExtObj.$Execute(1);',
  16394. 'ExtObj.$Execute(2);',
  16395. '$mod.p = rtl.createCallback(ExtObj, "$Execute");',
  16396. 'ExtObj.$Execute(1);',
  16397. 'ExtObj.$Execute(1);',
  16398. 'ExtObj.$Execute(3);',
  16399. '$mod.p = rtl.createCallback(ExtObj, "$Execute");',
  16400. 'ExtObj.$Execute(1);',
  16401. 'ExtObj.$Execute(1);',
  16402. 'ExtObj.$Execute(4);',
  16403. 'ExtObj.$Execute(1);',
  16404. 'ExtObj.$Execute(1);',
  16405. 'ExtObj.$Execute(5);',
  16406. '']));
  16407. end;
  16408. procedure TTestModule.TestExternalClass_ClassMethodStatic;
  16409. begin
  16410. StartProgram(false);
  16411. Add([
  16412. '{$modeswitch externalclass}',
  16413. 'type',
  16414. ' TExtA = class external name ''ExtObj''',
  16415. ' class procedure DoIt(Id: longint = 1); static;',
  16416. ' end;',
  16417. 'var p: Pointer;',
  16418. 'begin',
  16419. ' texta.doit;',
  16420. ' texta.doit();',
  16421. ' texta.doit(2);',
  16422. ' p:[email protected];',
  16423. ' with texta do begin',
  16424. ' doit;',
  16425. ' doit();',
  16426. ' doit(3);',
  16427. ' p:=@DoIt;',
  16428. ' end;',
  16429. '']);
  16430. ConvertProgram;
  16431. CheckSource('TestExternalClass_ClassMethodStatic',
  16432. LinesToStr([ // statements
  16433. 'this.p = null;',
  16434. '']),
  16435. LinesToStr([ // $mod.$main
  16436. 'ExtObj.DoIt(1);',
  16437. 'ExtObj.DoIt(1);',
  16438. 'ExtObj.DoIt(2);',
  16439. '$mod.p = ExtObj.DoIt;',
  16440. 'ExtObj.DoIt(1);',
  16441. 'ExtObj.DoIt(1);',
  16442. 'ExtObj.DoIt(3);',
  16443. '$mod.p = ExtObj.DoIt;',
  16444. '']));
  16445. end;
  16446. procedure TTestModule.TestExternalClass_FunctionResultInTypeCast;
  16447. begin
  16448. StartProgram(false);
  16449. Add([
  16450. '{$modeswitch externalclass}',
  16451. 'type',
  16452. ' TBird = class external name ''Array''',
  16453. ' end;',
  16454. 'function GetPtr: Pointer;',
  16455. 'begin',
  16456. 'end;',
  16457. 'procedure Write(const p);',
  16458. 'begin',
  16459. 'end;',
  16460. 'procedure WriteLn; varargs;',
  16461. 'begin',
  16462. 'end;',
  16463. 'begin',
  16464. ' if TBird(GetPtr)=nil then ;',
  16465. ' Write(GetPtr);',
  16466. ' WriteLn(GetPtr);',
  16467. ' Write(TBird(GetPtr));',
  16468. ' WriteLn(TBird(GetPtr));',
  16469. '']);
  16470. ConvertProgram;
  16471. CheckSource('TestFunctionResultInTypeCast',
  16472. LinesToStr([ // statements
  16473. 'this.GetPtr = function () {',
  16474. ' var Result = null;',
  16475. ' return Result;',
  16476. '};',
  16477. 'this.Write = function (p) {',
  16478. '};',
  16479. 'this.WriteLn = function () {',
  16480. '};',
  16481. '']),
  16482. LinesToStr([
  16483. 'if ($mod.GetPtr() === null) ;',
  16484. '$mod.Write($mod.GetPtr());',
  16485. '$mod.WriteLn($mod.GetPtr());',
  16486. '$mod.Write($mod.GetPtr());',
  16487. '$mod.WriteLn($mod.GetPtr());',
  16488. '']));
  16489. end;
  16490. procedure TTestModule.TestExternalClass_NonExternalOverride;
  16491. begin
  16492. StartProgram(false);
  16493. Add([
  16494. '{$modeswitch externalclass}',
  16495. 'type',
  16496. ' TExtA = class external name ''ExtObjA''',
  16497. ' procedure ProcA; virtual;',
  16498. ' procedure ProcB; virtual;',
  16499. ' end;',
  16500. ' TExtB = class external name ''ExtObjB'' (TExtA)',
  16501. ' end;',
  16502. ' TExtC = class (TExtB)',
  16503. ' procedure ProcA; override;',
  16504. ' end;',
  16505. 'procedure TExtC.ProcA;',
  16506. 'begin',
  16507. ' ProcA;',
  16508. ' Self.ProcA;',
  16509. ' ProcB;',
  16510. ' Self.ProcB;',
  16511. 'end;',
  16512. 'var',
  16513. ' A: texta;',
  16514. ' B: textb;',
  16515. ' C: textc;',
  16516. 'begin',
  16517. ' a.proca;',
  16518. ' b.proca;',
  16519. ' c.proca;']);
  16520. ConvertProgram;
  16521. CheckSource('TestExternalClass_NonExternalOverride',
  16522. LinesToStr([ // statements
  16523. 'rtl.createClassExt(this, "TExtC", ExtObjB, "", function () {',
  16524. ' this.$init = function () {',
  16525. ' };',
  16526. ' this.$final = function () {',
  16527. ' };',
  16528. ' this.ProcA = function () {',
  16529. ' this.ProcA();',
  16530. ' this.ProcA();',
  16531. ' this.ProcB();',
  16532. ' this.ProcB();',
  16533. ' };',
  16534. '});',
  16535. 'this.A = null;',
  16536. 'this.B = null;',
  16537. 'this.C = null;',
  16538. '']),
  16539. LinesToStr([ // $mod.$main
  16540. '$mod.A.ProcA();',
  16541. '$mod.B.ProcA();',
  16542. '$mod.C.ProcA();',
  16543. '']));
  16544. end;
  16545. procedure TTestModule.TestExternalClass_OverloadHint;
  16546. begin
  16547. StartProgram(false);
  16548. Add([
  16549. '{$modeswitch externalclass}',
  16550. 'type',
  16551. ' TExtA = class external name ''ExtObjA''',
  16552. ' procedure DoIt;',
  16553. ' procedure DoIt(i: longint);',
  16554. ' end;',
  16555. 'begin',
  16556. '']);
  16557. ConvertProgram;
  16558. CheckResolverUnexpectedHints(true);
  16559. CheckSource('TestExternalClass_OverloadHint',
  16560. LinesToStr([ // statements
  16561. '']),
  16562. LinesToStr([ // $mod.$main
  16563. '']));
  16564. end;
  16565. procedure TTestModule.TestExternalClass_SameNamePublishedProperty;
  16566. begin
  16567. WithTypeInfo:=true;
  16568. StartProgram(false);
  16569. Add([
  16570. '{$modeswitch externalclass}',
  16571. 'type',
  16572. ' JSwiper = class external name ''Swiper''',
  16573. ' constructor New;',
  16574. ' end;',
  16575. ' TObject = class',
  16576. ' private',
  16577. ' FSwiper: JSwiper;',
  16578. ' published',
  16579. ' property Swiper: JSwiper read FSwiper write FSwiper;',
  16580. ' end;',
  16581. 'begin',
  16582. ' JSwiper.new;',
  16583. '']);
  16584. ConvertProgram;
  16585. CheckSource('TestExternalClass_SameNamePublishedProperty',
  16586. LinesToStr([ // statements
  16587. 'this.$rtti.$ExtClass("JSwiper", {',
  16588. ' jsclass: "Swiper"',
  16589. '});',
  16590. 'rtl.createClass(this, "TObject", null, function () {',
  16591. ' this.$init = function () {',
  16592. ' this.FSwiper = null;',
  16593. ' };',
  16594. ' this.$final = function () {',
  16595. ' this.FSwiper = undefined;',
  16596. ' };',
  16597. ' var $r = this.$rtti;',
  16598. ' $r.addProperty("Swiper", 0, $mod.$rtti["JSwiper"], "FSwiper", "FSwiper");',
  16599. '});',
  16600. '']),
  16601. LinesToStr([ // $mod.$main
  16602. 'new Swiper();',
  16603. '']));
  16604. end;
  16605. procedure TTestModule.TestExternalClass_Property;
  16606. begin
  16607. StartProgram(false);
  16608. Add([
  16609. '{$modeswitch externalclass}',
  16610. 'type',
  16611. ' TExtA = class external name ''ExtA''',
  16612. ' function getYear: longint;',
  16613. ' procedure setYear(Value: longint);',
  16614. ' property Year: longint read getyear write setyear;',
  16615. ' end;',
  16616. ' TExtB = class (TExtA)',
  16617. ' procedure OtherSetYear(Value: longint);',
  16618. ' property year write othersetyear;',
  16619. ' end;',
  16620. 'procedure textb.othersetyear(value: longint);',
  16621. 'begin',
  16622. ' setYear(Value+4);',
  16623. 'end;',
  16624. 'var',
  16625. ' A: texta;',
  16626. ' B: textb;',
  16627. 'begin',
  16628. ' a.year:=a.year+1;',
  16629. ' b.year:=b.year+2;']);
  16630. ConvertProgram;
  16631. CheckSource('TestExternalClass_NonExternalOverride',
  16632. LinesToStr([ // statements
  16633. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  16634. ' this.$init = function () {',
  16635. ' };',
  16636. ' this.$final = function () {',
  16637. ' };',
  16638. ' this.OtherSetYear = function (Value) {',
  16639. ' this.setYear(Value+4);',
  16640. ' };',
  16641. '});',
  16642. 'this.A = null;',
  16643. 'this.B = null;',
  16644. '']),
  16645. LinesToStr([ // $mod.$main
  16646. '$mod.A.setYear($mod.A.getYear()+1);',
  16647. '$mod.B.OtherSetYear($mod.B.getYear()+2);',
  16648. '']));
  16649. end;
  16650. procedure TTestModule.TestExternalClass_PropertyDate;
  16651. begin
  16652. StartProgram(false);
  16653. Add([
  16654. '{$modeswitch externalclass}',
  16655. 'type',
  16656. ' TExtA = class external name ''ExtA''',
  16657. ' end;',
  16658. ' TExtB = class (TExtA)',
  16659. ' FDate: string;',
  16660. ' property Date: string read FDate write FDate;',
  16661. ' property ExtA: string read FDate write FDate;',
  16662. ' end;',
  16663. ' {$M+}',
  16664. ' TObject = class',
  16665. ' FDate: string;',
  16666. ' published',
  16667. ' property Date: string read FDate write FDate;',
  16668. ' property ExtA: string read FDate write FDate;',
  16669. ' end;',
  16670. 'var',
  16671. ' B: textb;',
  16672. ' o: TObject;',
  16673. 'begin',
  16674. ' b.date:=b.exta;',
  16675. ' o.date:=o.exta;']);
  16676. ConvertProgram;
  16677. CheckSource('TestExternalClass_PropertyDate',
  16678. LinesToStr([ // statements
  16679. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  16680. ' this.$init = function () {',
  16681. ' this.FDate = "";',
  16682. ' };',
  16683. ' this.$final = function () {',
  16684. ' };',
  16685. '});',
  16686. 'rtl.createClass(this, "TObject", null, function () {',
  16687. ' this.$init = function () {',
  16688. ' this.FDate = "";',
  16689. ' };',
  16690. ' this.$final = function () {',
  16691. ' };',
  16692. ' var $r = this.$rtti;',
  16693. ' $r.addField("FDate", rtl.string);',
  16694. ' $r.addProperty("Date", 0, rtl.string, "FDate", "FDate");',
  16695. ' $r.addProperty("ExtA", 0, rtl.string, "FDate", "FDate");',
  16696. '});',
  16697. 'this.B = null;',
  16698. 'this.o = null;',
  16699. '']),
  16700. LinesToStr([ // $mod.$main
  16701. '$mod.B.FDate = $mod.B.FDate;',
  16702. '$mod.o.FDate = $mod.o.FDate;',
  16703. '']));
  16704. end;
  16705. procedure TTestModule.TestExternalClass_ClassProperty;
  16706. begin
  16707. StartProgram(false);
  16708. Add('{$modeswitch externalclass}');
  16709. Add('type');
  16710. Add(' TExtA = class external name ''ExtA''');
  16711. Add(' class function getYear: longint;');
  16712. Add(' class procedure setYear(Value: longint);');
  16713. Add(' class property Year: longint read getyear write setyear;');
  16714. Add(' end;');
  16715. Add(' TExtB = class (TExtA)');
  16716. Add(' class function GetCentury: longint;');
  16717. Add(' class procedure SetCentury(Value: longint);');
  16718. Add(' class property Century: longint read getcentury write setcentury;');
  16719. Add(' end;');
  16720. Add('class function textb.getcentury: longint;');
  16721. Add('begin');
  16722. Add('end;');
  16723. Add('class procedure textb.setcentury(value: longint);');
  16724. Add('begin');
  16725. Add(' setyear(value+11);');
  16726. Add(' texta.year:=texta.year+12;');
  16727. Add(' year:=year+13;');
  16728. Add(' textb.century:=textb.century+14;');
  16729. Add(' century:=century+15;');
  16730. Add('end;');
  16731. Add('var');
  16732. Add(' A: texta;');
  16733. Add(' B: textb;');
  16734. Add('begin');
  16735. Add(' texta.year:=texta.year+1;');
  16736. Add(' textb.year:=textb.year+2;');
  16737. Add(' TextA.year:=TextA.year+3;');
  16738. Add(' b.year:=b.year+4;');
  16739. Add(' textb.century:=textb.century+5;');
  16740. Add(' b.century:=b.century+6;');
  16741. ConvertProgram;
  16742. CheckSource('TestExternalClass_ClassProperty',
  16743. LinesToStr([ // statements
  16744. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  16745. ' this.$init = function () {',
  16746. ' };',
  16747. ' this.$final = function () {',
  16748. ' };',
  16749. ' this.GetCentury = function () {',
  16750. ' var Result = 0;',
  16751. ' return Result;',
  16752. ' };',
  16753. ' this.SetCentury = function (Value) {',
  16754. ' this.setYear(Value + 11);',
  16755. ' ExtA.setYear(ExtA.getYear() + 12);',
  16756. ' this.setYear(this.getYear() + 13);',
  16757. ' $mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 14);',
  16758. ' this.SetCentury(this.GetCentury() + 15);',
  16759. ' };',
  16760. '});',
  16761. 'this.A = null;',
  16762. 'this.B = null;',
  16763. '']),
  16764. LinesToStr([ // $mod.$main
  16765. 'ExtA.setYear(ExtA.getYear() + 1);',
  16766. '$mod.TExtB.setYear($mod.TExtB.getYear() + 2);',
  16767. 'ExtA.setYear(ExtA.getYear() + 3);',
  16768. '$mod.B.setYear($mod.B.getYear() + 4);',
  16769. '$mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 5);',
  16770. '$mod.B.$class.SetCentury($mod.B.$class.GetCentury() + 6);',
  16771. '']));
  16772. end;
  16773. procedure TTestModule.TestExternalClass_ClassOf;
  16774. begin
  16775. StartProgram(false);
  16776. Add('{$modeswitch externalclass}');
  16777. Add('type');
  16778. Add(' TExtA = class external name ''ExtA''');
  16779. Add(' procedure ProcA; virtual;');
  16780. Add(' procedure ProcB; virtual;');
  16781. Add(' end;');
  16782. Add(' TExtAClass = class of TExtA;');
  16783. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  16784. Add(' end;');
  16785. Add(' TExtBClass = class of TExtB;');
  16786. Add(' TExtC = class (TExtB)');
  16787. Add(' procedure ProcA; override;');
  16788. Add(' end;');
  16789. Add(' TExtCClass = class of TExtC;');
  16790. Add('procedure TExtC.ProcA; begin end;');
  16791. Add('var');
  16792. Add(' A: texta; ClA: TExtAClass;');
  16793. Add(' B: textb; ClB: TExtBClass;');
  16794. Add(' C: textc; ClC: TExtCClass;');
  16795. Add('begin');
  16796. Add(' ClA:=texta;');
  16797. Add(' ClA:=textb;');
  16798. Add(' ClA:=textc;');
  16799. Add(' ClB:=textb;');
  16800. Add(' ClB:=textc;');
  16801. Add(' ClC:=textc;');
  16802. ConvertProgram;
  16803. CheckSource('TestExternalClass_ClassOf',
  16804. LinesToStr([ // statements
  16805. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  16806. ' this.$init = function () {',
  16807. ' };',
  16808. ' this.$final = function () {',
  16809. ' };',
  16810. ' this.ProcA = function () {',
  16811. ' };',
  16812. '});',
  16813. 'this.A = null;',
  16814. 'this.ClA = null;',
  16815. 'this.B = null;',
  16816. 'this.ClB = null;',
  16817. 'this.C = null;',
  16818. 'this.ClC = null;',
  16819. '']),
  16820. LinesToStr([ // $mod.$main
  16821. '$mod.ClA = ExtA;',
  16822. '$mod.ClA = ExtB;',
  16823. '$mod.ClA = $mod.TExtC;',
  16824. '$mod.ClB = ExtB;',
  16825. '$mod.ClB = $mod.TExtC;',
  16826. '$mod.ClC = $mod.TExtC;',
  16827. '']));
  16828. end;
  16829. procedure TTestModule.TestExternalClass_ClassOtherUnit;
  16830. begin
  16831. AddModuleWithIntfImplSrc('unit2.pas',
  16832. LinesToStr([
  16833. '{$modeswitch externalclass}',
  16834. 'type',
  16835. ' TExtA = class external name ''ExtA''',
  16836. ' class var Id: longint;',
  16837. ' end;',
  16838. '']),
  16839. '');
  16840. StartUnit(true);
  16841. Add('interface');
  16842. Add('uses unit2;');
  16843. Add('implementation');
  16844. Add('begin');
  16845. Add(' unit2.texta.id:=unit2.texta.id+1;');
  16846. ConvertUnit;
  16847. CheckSource('TestExternalClass_ClassOtherUnit',
  16848. LinesToStr([
  16849. '']),
  16850. LinesToStr([
  16851. 'ExtA.Id = ExtA.Id + 1;',
  16852. '']));
  16853. end;
  16854. procedure TTestModule.TestExternalClass_Is;
  16855. begin
  16856. StartProgram(false);
  16857. Add([
  16858. '{$modeswitch externalclass}',
  16859. 'type',
  16860. ' TExtA = class external name ''ExtA''',
  16861. ' end;',
  16862. ' TExtAClass = class of TExtA;',
  16863. ' TExtB = class external name ''ExtB'' (TExtA)',
  16864. ' end;',
  16865. ' TExtBClass = class of TExtB;',
  16866. ' TExtC = class (TExtB)',
  16867. ' end;',
  16868. ' TExtCClass = class of TExtC;',
  16869. 'var',
  16870. ' A: texta; ClA: TExtAClass;',
  16871. ' B: textb; ClB: TExtBClass;',
  16872. ' C: textc; ClC: TExtCClass;',
  16873. 'begin',
  16874. ' if a is textb then ;',
  16875. ' if a is textc then ;',
  16876. ' if b is textc then ;',
  16877. ' if cla is textb then ;',
  16878. ' if cla is textc then ;',
  16879. ' if clb is textc then ;',
  16880. ' try',
  16881. ' except',
  16882. ' on TExtA do ;',
  16883. ' on e: TExtB do ;',
  16884. ' end;',
  16885. '']);
  16886. ConvertProgram;
  16887. CheckSource('TestExternalClass_Is',
  16888. LinesToStr([ // statements
  16889. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  16890. ' this.$init = function () {',
  16891. ' };',
  16892. ' this.$final = function () {',
  16893. ' };',
  16894. '});',
  16895. 'this.A = null;',
  16896. 'this.ClA = null;',
  16897. 'this.B = null;',
  16898. 'this.ClB = null;',
  16899. 'this.C = null;',
  16900. 'this.ClC = null;',
  16901. '']),
  16902. LinesToStr([ // $mod.$main
  16903. 'if (rtl.isExt($mod.A, ExtB)) ;',
  16904. 'if ($mod.TExtC.isPrototypeOf($mod.A)) ;',
  16905. 'if ($mod.TExtC.isPrototypeOf($mod.B)) ;',
  16906. 'if (rtl.isExt($mod.ClA, ExtB)) ;',
  16907. 'if (rtl.is($mod.ClA, $mod.TExtC)) ;',
  16908. 'if (rtl.is($mod.ClB, $mod.TExtC)) ;',
  16909. 'try {} catch ($e) {',
  16910. ' if (rtl.isExt($e,ExtA)) {}',
  16911. ' else if (rtl.isExt($e,ExtB)) {',
  16912. ' var e = $e;',
  16913. ' } else throw $e',
  16914. '};',
  16915. '']));
  16916. end;
  16917. procedure TTestModule.TestExternalClass_As;
  16918. begin
  16919. StartProgram(false);
  16920. Add('{$modeswitch externalclass}');
  16921. Add('type');
  16922. Add(' TExtA = class external name ''ExtA''');
  16923. Add(' end;');
  16924. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  16925. Add(' end;');
  16926. Add(' TExtC = class (TExtB)');
  16927. Add(' end;');
  16928. Add('var');
  16929. Add(' A: texta;');
  16930. Add(' B: textb;');
  16931. Add(' C: textc;');
  16932. Add('begin');
  16933. Add(' b:=a as textb;');
  16934. Add(' c:=a as textc;');
  16935. Add(' c:=b as textc;');
  16936. ConvertProgram;
  16937. CheckSource('TestExternalClass_Is',
  16938. LinesToStr([ // statements
  16939. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  16940. ' this.$init = function () {',
  16941. ' };',
  16942. ' this.$final = function () {',
  16943. ' };',
  16944. '});',
  16945. 'this.A = null;',
  16946. 'this.B = null;',
  16947. 'this.C = null;',
  16948. '']),
  16949. LinesToStr([ // $mod.$main
  16950. '$mod.B = rtl.asExt($mod.A, ExtB);',
  16951. '$mod.C = rtl.as($mod.A, $mod.TExtC);',
  16952. '$mod.C = rtl.as($mod.B, $mod.TExtC);',
  16953. '']));
  16954. end;
  16955. procedure TTestModule.TestExternalClass_DestructorFail;
  16956. begin
  16957. StartProgram(false);
  16958. Add('{$modeswitch externalclass}');
  16959. Add('type');
  16960. Add(' TExtA = class external name ''ExtA''');
  16961. Add(' destructor Free;');
  16962. Add(' end;');
  16963. SetExpectedPasResolverError('Pascal element not supported: destructor',
  16964. nPasElementNotSupported);
  16965. ConvertProgram;
  16966. end;
  16967. procedure TTestModule.TestExternalClass_New;
  16968. begin
  16969. StartProgram(false);
  16970. Add([
  16971. '{$modeswitch externalclass}',
  16972. 'type',
  16973. ' TExtA = class external name ''ExtA''',
  16974. ' constructor New;',
  16975. ' constructor New(i: longint; j: longint = 2);',
  16976. ' end;',
  16977. 'var',
  16978. ' A: texta;',
  16979. 'begin',
  16980. ' a:=texta.new;',
  16981. ' a:=texta(texta.new);',
  16982. ' a:=texta.new();',
  16983. ' a:=texta.new(1);',
  16984. ' with texta do begin',
  16985. ' a:=new;',
  16986. ' a:=new();',
  16987. ' a:=new(2);',
  16988. ' end;',
  16989. ' a:=test1.texta.new;',
  16990. ' a:=test1.texta.new();',
  16991. ' a:=test1.texta.new(3);',
  16992. '']);
  16993. ConvertProgram;
  16994. CheckSource('TestExternalClass_New',
  16995. LinesToStr([ // statements
  16996. 'this.A = null;',
  16997. '']),
  16998. LinesToStr([ // $mod.$main
  16999. '$mod.A = new ExtA();',
  17000. '$mod.A = new ExtA();',
  17001. '$mod.A = new ExtA();',
  17002. '$mod.A = new ExtA(1,2);',
  17003. '$mod.A = new ExtA();',
  17004. '$mod.A = new ExtA();',
  17005. '$mod.A = new ExtA(2,2);',
  17006. '$mod.A = new ExtA();',
  17007. '$mod.A = new ExtA();',
  17008. '$mod.A = new ExtA(3,2);',
  17009. '']));
  17010. end;
  17011. procedure TTestModule.TestExternalClass_ClassOf_New;
  17012. begin
  17013. StartProgram(false);
  17014. Add('{$modeswitch externalclass}');
  17015. Add('type');
  17016. Add(' TExtAClass = class of TExtA;');
  17017. Add(' TExtA = class external name ''ExtA''');
  17018. Add(' C: TExtAClass;');
  17019. Add(' constructor New;');
  17020. Add(' end;');
  17021. Add('var');
  17022. Add(' A: texta;');
  17023. Add(' C: textaclass;');
  17024. Add('begin');
  17025. Add(' a:=c.new;');
  17026. Add(' a:=c.new();');
  17027. Add(' with C do begin');
  17028. Add(' a:=new;');
  17029. Add(' a:=new();');
  17030. Add(' end;');
  17031. Add(' a:=test1.c.new;');
  17032. Add(' a:=test1.c.new();');
  17033. Add(' a:=A.c.new();');
  17034. ConvertProgram;
  17035. CheckSource('TestExternalClass_ClassOf_New',
  17036. LinesToStr([ // statements
  17037. 'this.A = null;',
  17038. 'this.C = null;',
  17039. '']),
  17040. LinesToStr([ // $mod.$main
  17041. '$mod.A = new $mod.C();',
  17042. '$mod.A = new $mod.C();',
  17043. 'var $with = $mod.C;',
  17044. '$mod.A = new $with();',
  17045. '$mod.A = new $with();',
  17046. '$mod.A = new $mod.C();',
  17047. '$mod.A = new $mod.C();',
  17048. '$mod.A = new $mod.A.C();',
  17049. '']));
  17050. end;
  17051. procedure TTestModule.TestExternalClass_FuncClassOf_New;
  17052. begin
  17053. StartProgram(false);
  17054. Add([
  17055. '{$modeswitch externalclass}',
  17056. 'type',
  17057. ' TExtAClass = class of TExtA;',
  17058. ' TExtA = class external name ''ExtA''',
  17059. ' constructor New;',
  17060. ' end;',
  17061. 'function GetCreator: TExtAClass;',
  17062. 'begin',
  17063. ' Result:=TExtA;',
  17064. 'end;',
  17065. 'var',
  17066. ' A: texta;',
  17067. 'begin',
  17068. ' a:=getcreator.new;',
  17069. ' a:=getcreator().new;',
  17070. ' a:=getcreator().new();',
  17071. ' a:=getcreator.new();',
  17072. ' with getcreator do begin',
  17073. ' a:=new;',
  17074. ' a:=new();',
  17075. ' end;']);
  17076. ConvertProgram;
  17077. CheckSource('TestExternalClass_FuncClassOf_New',
  17078. LinesToStr([ // statements
  17079. 'this.GetCreator = function () {',
  17080. ' var Result = null;',
  17081. ' Result = ExtA;',
  17082. ' return Result;',
  17083. '};',
  17084. 'this.A = null;',
  17085. '']),
  17086. LinesToStr([ // $mod.$main
  17087. '$mod.A = new ($mod.GetCreator())();',
  17088. '$mod.A = new ($mod.GetCreator())();',
  17089. '$mod.A = new ($mod.GetCreator())();',
  17090. '$mod.A = new ($mod.GetCreator())();',
  17091. 'var $with = $mod.GetCreator();',
  17092. '$mod.A = new $with();',
  17093. '$mod.A = new $with();',
  17094. '']));
  17095. end;
  17096. procedure TTestModule.TestExternalClass_New_PasClassFail;
  17097. begin
  17098. StartProgram(false);
  17099. Add([
  17100. '{$modeswitch externalclass}',
  17101. 'type',
  17102. ' TExtA = class external name ''ExtA''',
  17103. ' constructor New;',
  17104. ' end;',
  17105. ' TBird = class(TExtA)',
  17106. ' end;',
  17107. 'begin',
  17108. ' TBird.new;',
  17109. '']);
  17110. SetExpectedPasResolverError(sJSNewNotSupported,nJSNewNotSupported);
  17111. ConvertProgram;
  17112. end;
  17113. procedure TTestModule.TestExternalClass_New_PasClassBracketsFail;
  17114. begin
  17115. StartProgram(false);
  17116. Add([
  17117. '{$modeswitch externalclass}',
  17118. 'type',
  17119. ' TExtA = class external name ''ExtA''',
  17120. ' constructor New;',
  17121. ' end;',
  17122. ' TBird = class(TExtA)',
  17123. ' end;',
  17124. 'begin',
  17125. ' TBird.new();',
  17126. '']);
  17127. SetExpectedPasResolverError(sJSNewNotSupported,nJSNewNotSupported);
  17128. ConvertProgram;
  17129. end;
  17130. procedure TTestModule.TestExternalClass_NewExtName;
  17131. begin
  17132. StartProgram(false);
  17133. Add([
  17134. '{$modeswitch externalclass}',
  17135. 'type',
  17136. ' TExtA = class external name ''ExtA''',
  17137. ' constructor New; external name ''Other'';',
  17138. ' constructor New(i: longint; j: longint = 2); external name ''A.B'';',
  17139. ' end;',
  17140. 'var',
  17141. ' A: texta;',
  17142. 'begin',
  17143. ' a:=texta.new;',
  17144. ' a:=texta(texta.new);',
  17145. ' a:=texta.new();',
  17146. ' a:=texta.new(1);',
  17147. ' with texta do begin',
  17148. ' a:=new;',
  17149. ' a:=new();',
  17150. ' a:=new(2);',
  17151. ' end;',
  17152. ' a:=test1.texta.new;',
  17153. ' a:=test1.texta.new();',
  17154. ' a:=test1.texta.new(3);',
  17155. '']);
  17156. ConvertProgram;
  17157. CheckSource('TestExternalClass_NewExtName',
  17158. LinesToStr([ // statements
  17159. 'this.A = null;',
  17160. '']),
  17161. LinesToStr([ // $mod.$main
  17162. '$mod.A = new Other();',
  17163. '$mod.A = new Other();',
  17164. '$mod.A = new Other();',
  17165. '$mod.A = new A.B(1,2);',
  17166. '$mod.A = new Other();',
  17167. '$mod.A = new Other();',
  17168. '$mod.A = new A.B(2,2);',
  17169. '$mod.A = new Other();',
  17170. '$mod.A = new Other();',
  17171. '$mod.A = new A.B(3,2);',
  17172. '']));
  17173. end;
  17174. procedure TTestModule.TestExternalClass_Constructor;
  17175. begin
  17176. StartProgram(false);
  17177. Add([
  17178. '{$modeswitch externalclass}',
  17179. 'type',
  17180. ' TExtA = class external name ''ExtA''',
  17181. ' constructor Create;',
  17182. ' constructor Create(i: longint; j: longint = 2);',
  17183. ' end;',
  17184. 'var',
  17185. ' A: texta;',
  17186. 'begin',
  17187. ' a:=texta.create;',
  17188. ' a:=texta(texta.create);',
  17189. ' a:=texta.create();',
  17190. ' a:=texta.create(1);',
  17191. ' with texta do begin',
  17192. ' a:=create;',
  17193. ' a:=create();',
  17194. ' a:=create(2);',
  17195. ' end;',
  17196. ' a:=test1.texta.create;',
  17197. ' a:=test1.texta.create();',
  17198. ' a:=test1.texta.create(3);',
  17199. '']);
  17200. ConvertProgram;
  17201. CheckSource('TestExternalClass_Constructor',
  17202. LinesToStr([ // statements
  17203. 'this.A = null;',
  17204. '']),
  17205. LinesToStr([ // $mod.$main
  17206. '$mod.A = new ExtA.Create();',
  17207. '$mod.A = new ExtA.Create();',
  17208. '$mod.A = new ExtA.Create();',
  17209. '$mod.A = new ExtA.Create(1,2);',
  17210. '$mod.A = new ExtA.Create();',
  17211. '$mod.A = new ExtA.Create();',
  17212. '$mod.A = new ExtA.Create(2,2);',
  17213. '$mod.A = new ExtA.Create();',
  17214. '$mod.A = new ExtA.Create();',
  17215. '$mod.A = new ExtA.Create(3,2);',
  17216. '']));
  17217. end;
  17218. procedure TTestModule.TestExternalClass_ConstructorBrackets;
  17219. begin
  17220. StartProgram(false);
  17221. Add([
  17222. '{$modeswitch externalclass}',
  17223. 'type',
  17224. ' TExtA = class external name ''ExtA''',
  17225. ' constructor Create; external name ''{}'';',
  17226. ' end;',
  17227. 'var',
  17228. ' A: texta;',
  17229. 'begin',
  17230. ' a:=texta.create;',
  17231. ' a:=texta(texta.create);',
  17232. ' a:=texta.create();',
  17233. ' with texta do begin',
  17234. ' a:=create;',
  17235. ' a:=create();',
  17236. ' end;',
  17237. ' a:=test1.texta.create;',
  17238. ' a:=test1.texta.create();',
  17239. '']);
  17240. ConvertProgram;
  17241. CheckSource('TestExternalClass_ConstructorBrackets',
  17242. LinesToStr([ // statements
  17243. 'this.A = null;',
  17244. '']),
  17245. LinesToStr([ // $mod.$main
  17246. '$mod.A = {};',
  17247. '$mod.A = {};',
  17248. '$mod.A = {};',
  17249. '$mod.A = {};',
  17250. '$mod.A = {};',
  17251. '$mod.A = {};',
  17252. '$mod.A = {};',
  17253. '']));
  17254. end;
  17255. procedure TTestModule.TestExternalClass_LocalConstSameName;
  17256. begin
  17257. StartProgram(false);
  17258. Add('{$modeswitch externalclass}');
  17259. Add('type');
  17260. Add(' TExtA = class external name ''ExtA''');
  17261. Add(' constructor New;');
  17262. Add(' end;');
  17263. Add('function DoIt: longint;');
  17264. Add('const ExtA: longint = 3;');
  17265. Add('begin');
  17266. Add(' Result:=ExtA;');
  17267. Add('end;');
  17268. Add('var');
  17269. Add(' A: texta;');
  17270. Add('begin');
  17271. Add(' a:=texta.new;');
  17272. ConvertProgram;
  17273. CheckSource('TestExternalClass_LocalConstSameName',
  17274. LinesToStr([ // statements
  17275. 'var ExtA$1 = 3;',
  17276. 'this.DoIt = function () {',
  17277. ' var Result = 0;',
  17278. ' Result = ExtA$1;',
  17279. ' return Result;',
  17280. '};',
  17281. 'this.A = null;',
  17282. '']),
  17283. LinesToStr([ // $mod.$main
  17284. '$mod.A = new ExtA();',
  17285. '']));
  17286. end;
  17287. procedure TTestModule.TestExternalClass_ReintroduceOverload;
  17288. begin
  17289. StartProgram(false);
  17290. Add('{$modeswitch externalclass}');
  17291. Add('type');
  17292. Add(' TExtA = class external name ''ExtA''');
  17293. Add(' procedure DoIt;');
  17294. Add(' end;');
  17295. Add(' TMyA = class(TExtA)');
  17296. Add(' procedure DoIt;');
  17297. Add(' end;');
  17298. Add('procedure TMyA.DoIt; begin end;');
  17299. Add('begin');
  17300. ConvertProgram;
  17301. CheckSource('TestExternalClass_ReintroduceOverload',
  17302. LinesToStr([ // statements
  17303. 'rtl.createClassExt(this, "TMyA", ExtA, "", function () {',
  17304. ' this.$init = function () {',
  17305. ' };',
  17306. ' this.$final = function () {',
  17307. ' };',
  17308. ' this.DoIt$1 = function () {',
  17309. ' };',
  17310. '});',
  17311. '']),
  17312. LinesToStr([ // $mod.$main
  17313. '']));
  17314. end;
  17315. procedure TTestModule.TestExternalClass_Inherited;
  17316. begin
  17317. StartProgram(false);
  17318. Add('{$modeswitch externalclass}');
  17319. Add('type');
  17320. Add(' TExtA = class external name ''ExtA''');
  17321. Add(' procedure DoIt(i: longint = 1); virtual;');
  17322. Add(' procedure DoSome(j: longint = 2);');
  17323. Add(' end;');
  17324. Add(' TExtB = class external name ''ExtB''(TExtA)');
  17325. Add(' end;');
  17326. Add(' TMyC = class(TExtB)');
  17327. Add(' procedure DoIt(i: longint = 1); override;');
  17328. Add(' procedure DoSome(j: longint = 2); reintroduce;');
  17329. Add(' end;');
  17330. Add('procedure TMyC.DoIt(i: longint);');
  17331. Add('begin');
  17332. Add(' inherited;');
  17333. Add(' inherited DoIt;');
  17334. Add(' inherited DoIt();');
  17335. Add(' inherited DoIt(3);');
  17336. Add(' inherited DoSome;');
  17337. Add(' inherited DoSome();');
  17338. Add(' inherited DoSome(4);');
  17339. Add('end;');
  17340. Add('procedure TMyC.DoSome(j: longint);');
  17341. Add('begin');
  17342. Add(' inherited;');
  17343. Add('end;');
  17344. Add('begin');
  17345. ConvertProgram;
  17346. CheckSource('TestExternalClass_ReintroduceOverload',
  17347. LinesToStr([ // statements
  17348. 'rtl.createClassExt(this, "TMyC", ExtB, "", function () {',
  17349. ' this.$init = function () {',
  17350. ' };',
  17351. ' this.$final = function () {',
  17352. ' };',
  17353. ' this.DoIt = function (i) {',
  17354. ' ExtB.DoIt.apply(this, arguments);',
  17355. ' ExtB.DoIt.call(this, 1);',
  17356. ' ExtB.DoIt.call(this, 1);',
  17357. ' ExtB.DoIt.call(this, 3);',
  17358. ' ExtB.DoSome.call(this, 2);',
  17359. ' ExtB.DoSome.call(this, 2);',
  17360. ' ExtB.DoSome.call(this, 4);',
  17361. ' };',
  17362. ' this.DoSome$1 = function (j) {',
  17363. ' ExtB.DoSome.apply(this, arguments);',
  17364. ' };',
  17365. '});',
  17366. '']),
  17367. LinesToStr([ // $mod.$main
  17368. '']));
  17369. end;
  17370. procedure TTestModule.TestExternalClass_PascalAncestorFail;
  17371. begin
  17372. StartProgram(false);
  17373. Add('{$modeswitch externalclass}');
  17374. Add('type');
  17375. Add(' TObject = class');
  17376. Add(' end;');
  17377. Add(' TExtA = class external name ''ExtA''(TObject)');
  17378. Add(' end;');
  17379. Add('begin');
  17380. SetExpectedPasResolverError('Ancestor "TObject" is not external',nAncestorIsNotExternal);
  17381. ConvertProgram;
  17382. end;
  17383. procedure TTestModule.TestExternalClass_NewInstance;
  17384. begin
  17385. StartProgram(false);
  17386. Add('{$modeswitch externalclass}');
  17387. Add('type');
  17388. Add(' TExtA = class external name ''ExtA''');
  17389. Add(' end;');
  17390. Add(' TMyB = class(TExtA)');
  17391. Add(' protected');
  17392. Add(' class function NewInstance(fnname: string; const paramarray): TMyB; virtual;');
  17393. Add(' end;');
  17394. Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
  17395. Add('begin end;');
  17396. Add('begin');
  17397. ConvertProgram;
  17398. CheckSource('TestExternalClass_NewInstance',
  17399. LinesToStr([ // statements
  17400. 'rtl.createClassExt(this, "TMyB", ExtA, "NewInstance", function () {',
  17401. ' this.$init = function () {',
  17402. ' };',
  17403. ' this.$final = function () {',
  17404. ' };',
  17405. ' this.NewInstance = function (fnname, paramarray) {',
  17406. ' var Result = null;',
  17407. ' return Result;',
  17408. ' };',
  17409. '});',
  17410. '']),
  17411. LinesToStr([ // $mod.$main
  17412. '']));
  17413. end;
  17414. procedure TTestModule.TestExternalClass_NewInstance_NonVirtualFail;
  17415. begin
  17416. StartProgram(false);
  17417. Add('{$modeswitch externalclass}');
  17418. Add('type');
  17419. Add(' TExtA = class external name ''ExtA''');
  17420. Add(' end;');
  17421. Add(' TMyB = class(TExtA)');
  17422. Add(' protected');
  17423. Add(' class function NewInstance(fnname: string; const paramarray): TMyB;');
  17424. Add(' end;');
  17425. Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
  17426. Add('begin end;');
  17427. Add('begin');
  17428. SetExpectedPasResolverError(sNewInstanceFunctionMustBeVirtual,nNewInstanceFunctionMustBeVirtual);
  17429. ConvertProgram;
  17430. end;
  17431. procedure TTestModule.TestExternalClass_NewInstance_FirstParamNotString_Fail;
  17432. begin
  17433. StartProgram(false);
  17434. Add('{$modeswitch externalclass}');
  17435. Add('type');
  17436. Add(' TExtA = class external name ''ExtA''');
  17437. Add(' end;');
  17438. Add(' TMyB = class(TExtA)');
  17439. Add(' protected');
  17440. Add(' class function NewInstance(fnname: longint; const paramarray): TMyB; virtual;');
  17441. Add(' end;');
  17442. Add('class function TMyB.NewInstance(fnname: longint; const paramarray): TMyB;');
  17443. Add('begin end;');
  17444. Add('begin');
  17445. SetExpectedPasResolverError('Incompatible type arg no. 1: Got "Longint", expected "String"',
  17446. nIncompatibleTypeArgNo);
  17447. ConvertProgram;
  17448. end;
  17449. procedure TTestModule.TestExternalClass_NewInstance_SecondParamTyped_Fail;
  17450. begin
  17451. StartProgram(false);
  17452. Add('{$modeswitch externalclass}');
  17453. Add('type');
  17454. Add(' TExtA = class external name ''ExtA''');
  17455. Add(' end;');
  17456. Add(' TMyB = class(TExtA)');
  17457. Add(' protected');
  17458. Add(' class function NewInstance(fnname: string; const paramarray: string): TMyB; virtual;');
  17459. Add(' end;');
  17460. Add('class function TMyB.NewInstance(fnname: string; const paramarray: string): TMyB;');
  17461. Add('begin end;');
  17462. Add('begin');
  17463. SetExpectedPasResolverError('Incompatible type arg no. 2: Got "type", expected "untyped"',
  17464. nIncompatibleTypeArgNo);
  17465. ConvertProgram;
  17466. end;
  17467. procedure TTestModule.TestExternalClass_JSFunctionPasDescendant;
  17468. begin
  17469. StartProgram(false);
  17470. Add([
  17471. '{$modeswitch externalclass}',
  17472. 'type',
  17473. ' TJSFunction = class external name ''Function''',
  17474. ' end;',
  17475. ' TExtA = class external name ''ExtA''(TJSFunction)',
  17476. ' constructor New(w: word);',
  17477. ' end;',
  17478. ' TBird = class (TExtA)',
  17479. ' public',
  17480. ' Size: word;',
  17481. ' class var Legs: word;',
  17482. ' constructor Create(a: word);',
  17483. ' end;',
  17484. ' TEagle = class (TBird)',
  17485. ' public',
  17486. ' constructor Create(b: word); reintroduce;',
  17487. ' end;',
  17488. 'constructor TBird.Create(a: word);',
  17489. 'begin',
  17490. ' inherited;', // silently ignored
  17491. ' inherited New(a);', // this.$func(a)
  17492. 'end;',
  17493. 'constructor TEagle.Create(b: word);',
  17494. 'begin',
  17495. ' inherited Create(b);',
  17496. 'end;',
  17497. 'var',
  17498. ' Bird: TBird;',
  17499. ' Eagle: TEagle;',
  17500. 'begin',
  17501. ' Bird:=TBird.Create(3);',
  17502. ' Eagle:=TEagle.Create(4);',
  17503. ' Bird.Size:=Bird.Size+5;',
  17504. ' Bird.Legs:=Bird.Legs+6;',
  17505. ' Eagle.Size:=Eagle.Size+5;',
  17506. ' Eagle.Legs:=Eagle.Legs+6;',
  17507. '']);
  17508. ConvertProgram;
  17509. CheckSource('TestExternalClass_JSFunctionPasDescendant',
  17510. LinesToStr([ // statements
  17511. 'rtl.createClassExt(this, "TBird", ExtA, "", function () {',
  17512. ' this.Legs = 0;',
  17513. ' this.$init = function () {',
  17514. ' this.Size = 0;',
  17515. ' };',
  17516. ' this.$final = function () {',
  17517. ' };',
  17518. ' this.Create = function (a) {',
  17519. ' this.$ancestorfunc(a);',
  17520. ' return this;',
  17521. ' };',
  17522. '});',
  17523. 'rtl.createClassExt(this, "TEagle", this.TBird, "", function () {',
  17524. ' this.Create$1 = function (b) {',
  17525. ' $mod.TBird.Create.call(this, b);',
  17526. ' return this;',
  17527. ' };',
  17528. '});',
  17529. 'this.Bird = null;',
  17530. 'this.Eagle = null;',
  17531. '']),
  17532. LinesToStr([ // $mod.$main
  17533. '$mod.Bird = $mod.TBird.$create("Create", [3]);',
  17534. '$mod.Eagle = $mod.TEagle.$create("Create$1", [4]);',
  17535. '$mod.Bird.Size = $mod.Bird.Size + 5;',
  17536. '$mod.TBird.Legs = $mod.Bird.Legs + 6;',
  17537. '$mod.Eagle.Size = $mod.Eagle.Size + 5;',
  17538. '$mod.TBird.Legs = $mod.Eagle.Legs + 6;',
  17539. '']));
  17540. end;
  17541. procedure TTestModule.TestExternalClass_PascalProperty;
  17542. begin
  17543. StartProgram(false);
  17544. Add('{$modeswitch externalclass}');
  17545. Add('type');
  17546. Add(' TJSElement = class;');
  17547. Add(' TJSNotifyEvent = procedure(Sender: TJSElement) of object;');
  17548. Add(' TJSElement = class external name ''ExtA''');
  17549. Add(' end;');
  17550. Add(' TControl = class(TJSElement)');
  17551. Add(' private');
  17552. Add(' FOnClick: TJSNotifyEvent;');
  17553. Add(' property OnClick: TJSNotifyEvent read FOnClick write FOnClick;');
  17554. Add(' procedure Click(Sender: TJSElement);');
  17555. Add(' end;');
  17556. Add('procedure TControl.Click(Sender: TJSElement);');
  17557. Add('begin');
  17558. Add(' OnClick(Self);');
  17559. Add('end;');
  17560. Add('var');
  17561. Add(' Ctrl: TControl;');
  17562. Add('begin');
  17563. Add(' Ctrl.OnClick:[email protected];');
  17564. Add(' Ctrl.OnClick(Ctrl);');
  17565. ConvertProgram;
  17566. CheckSource('TestExternalClass_PascalProperty',
  17567. LinesToStr([ // statements
  17568. 'rtl.createClassExt(this, "TControl", ExtA, "", function () {',
  17569. ' this.$init = function () {',
  17570. ' this.FOnClick = null;',
  17571. ' };',
  17572. ' this.$final = function () {',
  17573. ' this.FOnClick = undefined;',
  17574. ' };',
  17575. ' this.Click = function (Sender) {',
  17576. ' this.FOnClick(this);',
  17577. ' };',
  17578. '});',
  17579. 'this.Ctrl = null;',
  17580. '']),
  17581. LinesToStr([ // $mod.$main
  17582. '$mod.Ctrl.FOnClick = rtl.createCallback($mod.Ctrl, "Click");',
  17583. '$mod.Ctrl.FOnClick($mod.Ctrl);',
  17584. '']));
  17585. end;
  17586. procedure TTestModule.TestExternalClass_TypeCastToRootClass;
  17587. begin
  17588. StartProgram(false);
  17589. Add([
  17590. '{$modeswitch externalclass}',
  17591. 'type',
  17592. ' IUnknown = interface end;',
  17593. ' TObject = class',
  17594. ' end;',
  17595. ' TChild = class',
  17596. ' end;',
  17597. ' TExtRootA = class external name ''ExtRootA''',
  17598. ' end;',
  17599. ' TExtChildA = class external name ''ExtChildA''(TExtRootA)',
  17600. ' end;',
  17601. ' TExtRootB = class external name ''ExtRootB''',
  17602. ' end;',
  17603. ' TExtChildB = class external name ''ExtChildB''(TExtRootB)',
  17604. ' end;',
  17605. 'var',
  17606. ' Obj: TObject;',
  17607. ' Child: TChild;',
  17608. ' RootA: TExtRootA;',
  17609. ' ChildA: TExtChildA;',
  17610. ' RootB: TExtRootB;',
  17611. ' ChildB: TExtChildB;',
  17612. ' i: IUnknown;',
  17613. 'begin',
  17614. ' obj:=tobject(roota);',
  17615. ' obj:=tobject(childa);',
  17616. ' child:=tchild(tobject(roota));',
  17617. ' roota:=textroota(obj);',
  17618. ' roota:=textroota(child);',
  17619. ' roota:=textroota(rootb);',
  17620. ' roota:=textroota(childb);',
  17621. ' childa:=textchilda(textroota(obj));',
  17622. ' roota:=TExtRootA(i)',
  17623. '']);
  17624. ConvertProgram;
  17625. CheckSource('TestExternalClass_TypeCastToRootClass',
  17626. LinesToStr([ // statements
  17627. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  17628. 'rtl.createClass(this, "TObject", null, function () {',
  17629. ' this.$init = function () {',
  17630. ' };',
  17631. ' this.$final = function () {',
  17632. ' };',
  17633. '});',
  17634. 'rtl.createClass(this, "TChild", this.TObject, function () {',
  17635. '});',
  17636. 'this.Obj = null;',
  17637. 'this.Child = null;',
  17638. 'this.RootA = null;',
  17639. 'this.ChildA = null;',
  17640. 'this.RootB = null;',
  17641. 'this.ChildB = null;',
  17642. 'this.i = null;',
  17643. '']),
  17644. LinesToStr([ // $mod.$main
  17645. '$mod.Obj = $mod.RootA;',
  17646. '$mod.Obj = $mod.ChildA;',
  17647. '$mod.Child = $mod.RootA;',
  17648. '$mod.RootA = $mod.Obj;',
  17649. '$mod.RootA = $mod.Child;',
  17650. '$mod.RootA = $mod.RootB;',
  17651. '$mod.RootA = $mod.ChildB;',
  17652. '$mod.ChildA = $mod.Obj;',
  17653. '$mod.RootA = $mod.i;',
  17654. '']));
  17655. end;
  17656. procedure TTestModule.TestExternalClass_TypeCastToJSObject;
  17657. begin
  17658. StartProgram(false);
  17659. Add([
  17660. '{$modeswitch externalclass}',
  17661. 'type',
  17662. ' IUnknown = interface end;',
  17663. ' IBird = interface(IUnknown) end;',
  17664. ' TClass = class of TObject;',
  17665. ' TObject = class',
  17666. ' end;',
  17667. ' TChild = class',
  17668. ' end;',
  17669. ' TJSObject = class external name ''Object''',
  17670. ' end;',
  17671. ' TRec = record end;',
  17672. 'var',
  17673. ' Obj: TObject;',
  17674. ' Child: TChild;',
  17675. ' i: IUnknown;',
  17676. ' Bird: IBird;',
  17677. ' j: TJSObject;',
  17678. ' r: TRec;',
  17679. ' c: TClass;',
  17680. 'begin',
  17681. ' j:=tjsobject(IUnknown);',
  17682. ' j:=tjsobject(IBird);',
  17683. ' j:=tjsobject(TObject);',
  17684. ' j:=tjsobject(TChild);',
  17685. ' j:=tjsobject(TRec);',
  17686. ' j:=tjsobject(Obj);',
  17687. ' j:=tjsobject(Child);',
  17688. ' j:=tjsobject(i);',
  17689. ' j:=tjsobject(Bird);',
  17690. ' j:=tjsobject(r);',
  17691. ' j:=tjsobject(c);',
  17692. '']);
  17693. ConvertProgram;
  17694. CheckSource('TestExternalClass_TypeCastToJSObject',
  17695. LinesToStr([ // statements
  17696. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  17697. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], this.IUnknown);',
  17698. 'rtl.createClass(this, "TObject", null, function () {',
  17699. ' this.$init = function () {',
  17700. ' };',
  17701. ' this.$final = function () {',
  17702. ' };',
  17703. '});',
  17704. 'rtl.createClass(this, "TChild", this.TObject, function () {',
  17705. '});',
  17706. 'rtl.recNewT(this, "TRec", function () {',
  17707. ' this.$eq = function (b) {',
  17708. ' return true;',
  17709. ' };',
  17710. ' this.$assign = function (s) {',
  17711. ' return this;',
  17712. ' };',
  17713. '});',
  17714. 'this.Obj = null;',
  17715. 'this.Child = null;',
  17716. 'this.i = null;',
  17717. 'this.Bird = null;',
  17718. 'this.j = null;',
  17719. 'this.r = this.TRec.$new();',
  17720. 'this.c = null;',
  17721. '']),
  17722. LinesToStr([ // $mod.$main
  17723. '$mod.j = $mod.IUnknown;',
  17724. '$mod.j = $mod.IBird;',
  17725. '$mod.j = $mod.TObject;',
  17726. '$mod.j = $mod.TChild;',
  17727. '$mod.j = $mod.TRec;',
  17728. '$mod.j = $mod.Obj;',
  17729. '$mod.j = $mod.Child;',
  17730. '$mod.j = $mod.i;',
  17731. '$mod.j = $mod.Bird;',
  17732. '$mod.j = $mod.r;',
  17733. '$mod.j = $mod.c;',
  17734. '']));
  17735. end;
  17736. procedure TTestModule.TestExternalClass_TypeCastStringToExternalString;
  17737. begin
  17738. StartProgram(false);
  17739. Add('{$modeswitch externalclass}');
  17740. Add('type');
  17741. Add(' TJSString = class external name ''String''');
  17742. Add(' class function fromCharCode() : string; varargs;');
  17743. Add(' function anchor(const aName : string) : string;');
  17744. Add(' end;');
  17745. Add('var');
  17746. Add(' s: string;');
  17747. Add('begin');
  17748. Add(' s:=TJSString.fromCharCode(65,66);');
  17749. Add(' s:=TJSString(s).anchor(s);');
  17750. Add(' s:=TJSString(''foo'').anchor(s);');
  17751. ConvertProgram;
  17752. CheckSource('TestExternalClass_TypeCastStringToExternalString',
  17753. LinesToStr([ // statements
  17754. 'this.s = "";',
  17755. '']),
  17756. LinesToStr([ // $mod.$main
  17757. '$mod.s = String.fromCharCode(65, 66);',
  17758. '$mod.s = $mod.s.anchor($mod.s);',
  17759. '$mod.s = "foo".anchor($mod.s);',
  17760. '']));
  17761. end;
  17762. procedure TTestModule.TestExternalClass_TypeCastToJSFunction;
  17763. begin
  17764. StartProgram(false);
  17765. Add([
  17766. '{$modeswitch externalclass}',
  17767. 'type',
  17768. ' TJSObject = class external name ''Object'' end;',
  17769. ' TJSFunction = class external name ''Function''',
  17770. ' function bind(thisArg: TJSObject): TJSFunction; varargs;',
  17771. ' function call(thisArg: TJSObject): JSValue; varargs;',
  17772. ' end;',
  17773. ' TObject = class',
  17774. ' procedure DoIt(i: longint);',
  17775. ' end;',
  17776. ' TFuncInt = function(o: TObject): longint;',
  17777. 'function GetIt(o: TObject): longint;',
  17778. ' procedure Sub; begin end;',
  17779. 'var',
  17780. ' f: TJSFunction;',
  17781. ' fi: TFuncInt;',
  17782. 'begin',
  17783. ' fi:=TFuncInt(f);',
  17784. ' f:=TJSFunction(fi);',
  17785. ' f:=TJSFunction(@GetIt);',
  17786. ' f:=TJSFunction(@GetIt).bind(nil,3);',
  17787. ' f:=TJSFunction(@Sub);',
  17788. ' f:=TJSFunction(@o.doit);',
  17789. ' f:=TJSFunction(fi).bind(nil,4)',
  17790. 'end;',
  17791. 'procedure TObject.DoIt(i: longint);',
  17792. ' procedure Sub; begin end;',
  17793. 'var f: TJSFunction;',
  17794. 'begin',
  17795. ' f:=TJSFunction(@DoIt);',
  17796. ' f:=TJSFunction(@DoIt).bind(nil,13);',
  17797. ' f:=TJSFunction(@Sub);',
  17798. ' f:=TJSFunction(@GetIt);',
  17799. 'end;',
  17800. 'begin']);
  17801. ConvertProgram;
  17802. CheckSource('TestExternalClass_TypeCastToJSFunction',
  17803. LinesToStr([ // statements
  17804. 'rtl.createClass(this, "TObject", null, function () {',
  17805. ' this.$init = function () {',
  17806. ' };',
  17807. ' this.$final = function () {',
  17808. ' };',
  17809. ' this.DoIt = function (i) {',
  17810. ' var $Self = this;',
  17811. ' function Sub() {',
  17812. ' };',
  17813. ' var f = null;',
  17814. ' f = this.DoIt;',
  17815. ' f = this.DoIt.bind(null, 13);',
  17816. ' f = Sub;',
  17817. ' f = $mod.GetIt;',
  17818. ' };',
  17819. '});',
  17820. 'this.GetIt = function (o) {',
  17821. ' var Result = 0;',
  17822. ' function Sub() {',
  17823. ' };',
  17824. ' var f = null;',
  17825. ' var fi = null;',
  17826. ' fi = f;',
  17827. ' f = fi;',
  17828. ' f = $mod.GetIt;',
  17829. ' f = $mod.GetIt.bind(null, 3);',
  17830. ' f = Sub;',
  17831. ' f = $mod.TObject.DoIt;',
  17832. ' f = fi.bind(null, 4);',
  17833. ' return Result;',
  17834. '};',
  17835. '']),
  17836. LinesToStr([ // $mod.$main
  17837. '']));
  17838. end;
  17839. procedure TTestModule.TestExternalClass_TypeCastDelphiUnrelated;
  17840. begin
  17841. StartProgram(false);
  17842. Add([
  17843. '{$mode delphi}',
  17844. '{$modeswitch externalclass}',
  17845. 'type',
  17846. ' TJSObject = class external name ''Object'' end;',
  17847. ' TJSWindow = class external name ''Window''(TJSObject)',
  17848. ' procedure Open;',
  17849. ' end;',
  17850. ' TJSEventTarget = class external name ''Event''(TJSObject)',
  17851. ' procedure Execute;',
  17852. ' end;',
  17853. 'procedure Fly;',
  17854. 'var',
  17855. ' w: TJSWindow;',
  17856. ' e: TJSEventTarget;',
  17857. 'begin',
  17858. ' w:=TJSWindow(e);',
  17859. ' e:=TJSEventTarget(w);',
  17860. 'end;',
  17861. 'begin']);
  17862. ConvertProgram;
  17863. CheckSource('TestExternalClass_TypeCastDelphiUnrelated',
  17864. LinesToStr([ // statements
  17865. 'this.Fly = function () {',
  17866. ' var w = null;',
  17867. ' var e = null;',
  17868. ' w = e;',
  17869. ' e = w;',
  17870. '};',
  17871. '']),
  17872. LinesToStr([ // $mod.$main
  17873. '']));
  17874. end;
  17875. procedure TTestModule.TestExternalClass_CallClassFunctionOfInstanceFail;
  17876. begin
  17877. StartProgram(false);
  17878. Add('{$modeswitch externalclass}');
  17879. Add('type');
  17880. Add(' TJSString = class external name ''String''');
  17881. Add(' class function fromCharCode() : string; varargs;');
  17882. Add(' end;');
  17883. Add('var');
  17884. Add(' s: string;');
  17885. Add(' sObj: TJSString;');
  17886. Add('begin');
  17887. Add(' s:=sObj.fromCharCode(65,66);');
  17888. SetExpectedPasResolverError('External class instance cannot access static class function fromCharCode',
  17889. nExternalClassInstanceCannotAccessStaticX);
  17890. ConvertProgram;
  17891. end;
  17892. procedure TTestModule.TestExternalClass_BracketAccessor;
  17893. begin
  17894. StartProgram(false);
  17895. Add([
  17896. '{$modeswitch externalclass}',
  17897. 'type',
  17898. ' TJSArray = class external name ''Array2''',
  17899. ' function GetItems(Index: longint): jsvalue; external name ''[]'';',
  17900. ' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';',
  17901. ' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;',
  17902. ' end;',
  17903. 'procedure DoIt(vI: JSValue; const vJ: jsvalue; var vK: jsvalue; out vL: jsvalue);',
  17904. 'begin end;',
  17905. 'var',
  17906. ' Arr: tjsarray;',
  17907. ' s: string;',
  17908. ' i: longint;',
  17909. ' v: jsvalue;',
  17910. 'begin',
  17911. ' v:=arr[0];',
  17912. ' v:=arr.items[1];',
  17913. ' arr[2]:=s;',
  17914. ' arr.items[3]:=s;',
  17915. ' arr[4]:=i;',
  17916. ' arr[5]:=arr[6];',
  17917. ' arr.items[7]:=arr.items[8];',
  17918. ' with arr do items[9]:=items[10];',
  17919. ' doit(arr[7],arr[8],arr[9],arr[10]);',
  17920. ' with arr do begin',
  17921. ' v:=GetItems(14);',
  17922. ' setitems(15,16);',
  17923. ' end;',
  17924. ' v:=test1.arr.items[17];',
  17925. ' test1.arr.items[18]:=v;',
  17926. '']);
  17927. ConvertProgram;
  17928. CheckSource('TestExternalClass_BracketAccessor',
  17929. LinesToStr([ // statements
  17930. 'this.DoIt = function (vI, vJ, vK, vL) {',
  17931. '};',
  17932. 'this.Arr = null;',
  17933. 'this.s = "";',
  17934. 'this.i = 0;',
  17935. 'this.v = undefined;',
  17936. '']),
  17937. LinesToStr([ // $mod.$main
  17938. '$mod.v = $mod.Arr[0];',
  17939. '$mod.v = $mod.Arr[1];',
  17940. '$mod.Arr[2] = $mod.s;',
  17941. '$mod.Arr[3] = $mod.s;',
  17942. '$mod.Arr[4] = $mod.i;',
  17943. '$mod.Arr[5] = $mod.Arr[6];',
  17944. '$mod.Arr[7] = $mod.Arr[8];',
  17945. 'var $with = $mod.Arr;',
  17946. '$with[9] = $with[10];',
  17947. '$mod.DoIt($mod.Arr[7], $mod.Arr[8], {',
  17948. ' a: 9,',
  17949. ' p: $mod.Arr,',
  17950. ' get: function () {',
  17951. ' return this.p[this.a];',
  17952. ' },',
  17953. ' set: function (v) {',
  17954. ' this.p[this.a] = v;',
  17955. ' }',
  17956. '}, {',
  17957. ' a: 10,',
  17958. ' p: $mod.Arr,',
  17959. ' get: function () {',
  17960. ' return this.p[this.a];',
  17961. ' },',
  17962. ' set: function (v) {',
  17963. ' this.p[this.a] = v;',
  17964. ' }',
  17965. '});',
  17966. 'var $with1 = $mod.Arr;',
  17967. '$mod.v = $with1[14];',
  17968. '$with1[15] = 16;',
  17969. '$mod.v = $mod.Arr[17];',
  17970. '$mod.Arr[18] = $mod.v;',
  17971. '']));
  17972. end;
  17973. procedure TTestModule.TestExternalClass_BracketAccessor_Call;
  17974. begin
  17975. StartProgram(false);
  17976. Add([
  17977. '{$modeswitch externalclass}',
  17978. 'type',
  17979. ' TJSArray = class external name ''Array2''',
  17980. ' function GetItems(Index: longint): jsvalue; external name ''[]'';',
  17981. ' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';',
  17982. ' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;',
  17983. ' end;',
  17984. ' TMyArr = class(TJSArray)',
  17985. ' procedure DoIt;',
  17986. ' end;',
  17987. 'procedure tmyarr.DoIt;',
  17988. 'begin',
  17989. ' Items[1]:=Items[2];',
  17990. ' SetItems(3,getItems(4));',
  17991. 'end;',
  17992. 'var',
  17993. ' Arr: tmyarr;',
  17994. ' s: string;',
  17995. ' i: longint;',
  17996. ' v: jsvalue;',
  17997. 'begin',
  17998. ' v:=arr[0];',
  17999. ' v:=arr.items[1];',
  18000. ' arr[2]:=s;',
  18001. ' arr.items[3]:=s;',
  18002. ' arr[4]:=i;',
  18003. ' arr[5]:=arr[6];',
  18004. ' arr.items[7]:=arr.items[8];',
  18005. ' with arr do items[9]:=items[10];',
  18006. ' with arr do begin',
  18007. ' v:=GetItems(14);',
  18008. ' setitems(15,16);',
  18009. ' end;',
  18010. '']);
  18011. ConvertProgram;
  18012. CheckSource('TestExternalClass_BracketAccessor_Call',
  18013. LinesToStr([ // statements
  18014. 'rtl.createClassExt(this, "TMyArr", Array2, "", function () {',
  18015. ' this.$init = function () {',
  18016. ' };',
  18017. ' this.$final = function () {',
  18018. ' };',
  18019. ' this.DoIt = function () {',
  18020. ' this[1] = this[2];',
  18021. ' this[3] = this[4];',
  18022. ' };',
  18023. '});',
  18024. 'this.Arr = null;',
  18025. 'this.s = "";',
  18026. 'this.i = 0;',
  18027. 'this.v = undefined;',
  18028. '']),
  18029. LinesToStr([ // $mod.$main
  18030. '$mod.v = $mod.Arr[0];',
  18031. '$mod.v = $mod.Arr[1];',
  18032. '$mod.Arr[2] = $mod.s;',
  18033. '$mod.Arr[3] = $mod.s;',
  18034. '$mod.Arr[4] = $mod.i;',
  18035. '$mod.Arr[5] = $mod.Arr[6];',
  18036. '$mod.Arr[7] = $mod.Arr[8];',
  18037. 'var $with = $mod.Arr;',
  18038. '$with[9] = $with[10];',
  18039. 'var $with1 = $mod.Arr;',
  18040. '$mod.v = $with1[14];',
  18041. '$with1[15] = 16;',
  18042. '']));
  18043. end;
  18044. procedure TTestModule.TestExternalClass_BracketAccessor_2ParamsFail;
  18045. begin
  18046. StartProgram(false);
  18047. Add('{$modeswitch externalclass}');
  18048. Add('type');
  18049. Add(' TJSArray = class external name ''Array2''');
  18050. Add(' function GetItems(Index1, Index2: longint): jsvalue; external name ''[]'';');
  18051. Add(' procedure SetItems(Index1, Index2: longint; Value: jsvalue); external name ''[]'';');
  18052. Add(' property Items[Index1, Index2: longint]: jsvalue read GetItems write SetItems; default;');
  18053. Add(' end;');
  18054. Add('begin');
  18055. SetExpectedPasResolverError(sBracketAccessorOfExternalClassMustHaveOneParameter,
  18056. nBracketAccessorOfExternalClassMustHaveOneParameter);
  18057. ConvertProgram;
  18058. end;
  18059. procedure TTestModule.TestExternalClass_BracketAccessor_ReadOnly;
  18060. begin
  18061. StartProgram(false);
  18062. Add('{$modeswitch externalclass}');
  18063. Add('type');
  18064. Add(' TJSArray = class external name ''Array2''');
  18065. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  18066. Add(' property Items[Index: longint]: jsvalue read GetItems; default;');
  18067. Add(' end;');
  18068. Add('procedure DoIt(vI: JSValue; const vJ: jsvalue);');
  18069. Add('begin end;');
  18070. Add('var');
  18071. Add(' Arr: tjsarray;');
  18072. Add(' v: jsvalue;');
  18073. Add('begin');
  18074. Add(' v:=arr[0];');
  18075. Add(' v:=arr.items[1];');
  18076. Add(' with arr do v:=items[2];');
  18077. Add(' doit(arr[3],arr[4]);');
  18078. ConvertProgram;
  18079. CheckSource('TestExternalClass_BracketAccessor_ReadOnly',
  18080. LinesToStr([ // statements
  18081. 'this.DoIt = function (vI, vJ) {',
  18082. '};',
  18083. 'this.Arr = null;',
  18084. 'this.v = undefined;',
  18085. '']),
  18086. LinesToStr([ // $mod.$main
  18087. '$mod.v = $mod.Arr[0];',
  18088. '$mod.v = $mod.Arr[1];',
  18089. 'var $with = $mod.Arr;',
  18090. '$mod.v = $with[2];',
  18091. '$mod.DoIt($mod.Arr[3], $mod.Arr[4]);',
  18092. '']));
  18093. end;
  18094. procedure TTestModule.TestExternalClass_BracketAccessor_WriteOnly;
  18095. begin
  18096. StartProgram(false);
  18097. Add('{$modeswitch externalclass}');
  18098. Add('type');
  18099. Add(' TJSArray = class external name ''Array2''');
  18100. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  18101. Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
  18102. Add(' end;');
  18103. Add('var');
  18104. Add(' Arr: tjsarray;');
  18105. Add(' s: string;');
  18106. Add(' i: longint;');
  18107. Add(' v: jsvalue;');
  18108. Add('begin');
  18109. Add(' arr[2]:=s;');
  18110. Add(' arr.items[3]:=s;');
  18111. Add(' arr[4]:=i;');
  18112. Add(' with arr do items[5]:=i;');
  18113. ConvertProgram;
  18114. CheckSource('TestExternalClass_BracketAccessor_WriteOnly',
  18115. LinesToStr([ // statements
  18116. 'this.Arr = null;',
  18117. 'this.s = "";',
  18118. 'this.i = 0;',
  18119. 'this.v = undefined;',
  18120. '']),
  18121. LinesToStr([ // $mod.$main
  18122. '$mod.Arr[2] = $mod.s;',
  18123. '$mod.Arr[3] = $mod.s;',
  18124. '$mod.Arr[4] = $mod.i;',
  18125. 'var $with = $mod.Arr;',
  18126. '$with[5] = $mod.i;',
  18127. '']));
  18128. end;
  18129. procedure TTestModule.TestExternalClass_BracketAccessor_MultiType;
  18130. begin
  18131. StartProgram(false);
  18132. Add('{$modeswitch externalclass}');
  18133. Add('type');
  18134. Add(' TJSArray = class external name ''Array2''');
  18135. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  18136. Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
  18137. Add(' procedure SetNumbers(Index: longint; Value: longint); external name ''[]'';');
  18138. Add(' property Numbers[Index: longint]: longint write SetNumbers;');
  18139. Add(' end;');
  18140. Add('var');
  18141. Add(' Arr: tjsarray;');
  18142. Add(' s: string;');
  18143. Add(' i: longint;');
  18144. Add(' v: jsvalue;');
  18145. Add('begin');
  18146. Add(' arr[2]:=s;');
  18147. Add(' arr.items[3]:=s;');
  18148. Add(' arr.numbers[4]:=i;');
  18149. Add(' with arr do items[5]:=i;');
  18150. Add(' with arr do numbers[6]:=i;');
  18151. ConvertProgram;
  18152. CheckSource('TestExternalClass_BracketAccessor_MultiType',
  18153. LinesToStr([ // statements
  18154. 'this.Arr = null;',
  18155. 'this.s = "";',
  18156. 'this.i = 0;',
  18157. 'this.v = undefined;',
  18158. '']),
  18159. LinesToStr([ // $mod.$main
  18160. '$mod.Arr[2] = $mod.s;',
  18161. '$mod.Arr[3] = $mod.s;',
  18162. '$mod.Arr[4] = $mod.i;',
  18163. 'var $with = $mod.Arr;',
  18164. '$with[5] = $mod.i;',
  18165. 'var $with1 = $mod.Arr;',
  18166. '$with1[6] = $mod.i;',
  18167. '']));
  18168. end;
  18169. procedure TTestModule.TestExternalClass_BracketAccessor_Index;
  18170. begin
  18171. StartProgram(false);
  18172. Add('{$modeswitch externalclass}');
  18173. Add('type');
  18174. Add(' TJSArray = class external name ''Array2''');
  18175. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  18176. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  18177. Add(' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;');
  18178. Add(' end;');
  18179. Add('var');
  18180. Add(' Arr: tjsarray;');
  18181. Add(' i: longint;');
  18182. Add(' IntArr: array of longint;');
  18183. Add(' v: jsvalue;');
  18184. Add('begin');
  18185. Add(' v:=arr.items[i];');
  18186. Add(' arr[longint(v)]:=arr.items[intarr[0]];');
  18187. Add(' arr.items[intarr[1]]:=arr[IntArr[2]];');
  18188. ConvertProgram;
  18189. CheckSource('TestExternalClass_BracketAccessor_Index',
  18190. LinesToStr([ // statements
  18191. 'this.Arr = null;',
  18192. 'this.i = 0;',
  18193. 'this.IntArr = [];',
  18194. 'this.v = undefined;',
  18195. '']),
  18196. LinesToStr([ // $mod.$main
  18197. '$mod.v = $mod.Arr[$mod.i];',
  18198. '$mod.Arr[rtl.trunc($mod.v)] = $mod.Arr[$mod.IntArr[0]];',
  18199. '$mod.Arr[$mod.IntArr[1]] = $mod.Arr[$mod.IntArr[2]];',
  18200. '']));
  18201. end;
  18202. procedure TTestModule.TestExternalClass_ForInJSObject;
  18203. begin
  18204. StartProgram(false);
  18205. Add([
  18206. '{$modeswitch externalclass}',
  18207. 'type',
  18208. ' TJSObject = class external name ''Object''',
  18209. ' end;',
  18210. 'var',
  18211. ' o: TJSObject;',
  18212. ' key: string;',
  18213. 'begin',
  18214. ' for key in o do',
  18215. ' if key=''abc'' then ;',
  18216. '']);
  18217. ConvertProgram;
  18218. CheckSource('TestExternalClass_ForInJSObject',
  18219. LinesToStr([ // statements
  18220. 'this.o = null;',
  18221. 'this.key = "";',
  18222. '']),
  18223. LinesToStr([ // $mod.$main
  18224. 'for ($mod.key in $mod.o) if ($mod.key === "abc") ;',
  18225. '']));
  18226. end;
  18227. procedure TTestModule.TestExternalClass_ForInJSArray;
  18228. begin
  18229. StartProgram(false);
  18230. Add([
  18231. '{$modeswitch externalclass}',
  18232. 'type',
  18233. ' TJSInt8Array = class external name ''Int8Array''',
  18234. ' private',
  18235. ' flength: NativeInt external name ''length'';',
  18236. ' function getValue(Index: NativeInt): shortint; external name ''[]'';',
  18237. ' public',
  18238. ' property values[Index: NativeInt]: Shortint Read getValue; default;',
  18239. ' property Length: NativeInt read flength;',
  18240. ' end;',
  18241. 'var',
  18242. ' a: TJSInt8Array;',
  18243. ' value: shortint;',
  18244. 'begin',
  18245. ' for value in a do',
  18246. ' if value=3 then ;',
  18247. '']);
  18248. ConvertProgram;
  18249. CheckSource('TestExternalClass_ForInJSArray',
  18250. LinesToStr([ // statements
  18251. 'this.a = null;',
  18252. 'this.value = 0;',
  18253. '']),
  18254. LinesToStr([ // $mod.$main
  18255. 'for (var $in = $mod.a, $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) {',
  18256. ' $mod.value = $in[$l];',
  18257. ' if ($mod.value === 3) ;',
  18258. '};',
  18259. '']));
  18260. end;
  18261. procedure TTestModule.TestExternalClass_IncompatibleArgDuplicateIdentifier;
  18262. begin
  18263. AddModuleWithIntfImplSrc('unit2.pas',
  18264. LinesToStr([
  18265. '{$modeswitch externalclass}',
  18266. 'type',
  18267. ' TJSBufferSource = class external name ''BufferSource''',
  18268. ' end;',
  18269. 'procedure DoIt(s: TJSBufferSource); external name ''DoIt'';',
  18270. '']),
  18271. '');
  18272. AddModuleWithIntfImplSrc('unit3.pas',
  18273. LinesToStr([
  18274. '{$modeswitch externalclass}',
  18275. 'type',
  18276. ' TJSBufferSource = class external name ''BufferSource''',
  18277. ' end;',
  18278. '']),
  18279. '');
  18280. StartUnit(true);
  18281. Add([
  18282. 'interface',
  18283. 'uses unit2, unit3;',
  18284. 'procedure DoSome(s: TJSBufferSource);',
  18285. 'implementation',
  18286. 'procedure DoSome(s: TJSBufferSource);',
  18287. 'begin',
  18288. ' DoIt(s);',
  18289. 'end;',
  18290. '']);
  18291. SetExpectedPasResolverError('Incompatible type arg no. 1: Got "unit3.TJSBufferSource", expected "unit2.TJSBufferSource"',
  18292. nIncompatibleTypeArgNo);
  18293. ConvertUnit;
  18294. end;
  18295. procedure TTestModule.TestClassInterface_Corba;
  18296. begin
  18297. StartProgram(false);
  18298. Add([
  18299. '{$interfaces corba}',
  18300. 'type',
  18301. ' IUnknown = interface;',
  18302. ' IUnknown = interface',
  18303. ' [''{00000000-0000-0000-C000-000000000046}'']',
  18304. ' end;',
  18305. ' IInterface = IUnknown;',
  18306. ' IBird = interface(IInterface)',
  18307. ' function GetSize: longint;',
  18308. ' procedure SetSize(i: longint);',
  18309. ' property Size: longint read GetSize write SetSize;',
  18310. ' procedure DoIt(i: longint);',
  18311. ' end;',
  18312. ' TObject = class',
  18313. ' end;',
  18314. ' TBird = class(TObject,IBird)',
  18315. ' function GetSize: longint; virtual; abstract;',
  18316. ' procedure SetSize(i: longint); virtual; abstract;',
  18317. ' procedure DoIt(i: longint); virtual; abstract;',
  18318. ' end;',
  18319. 'var',
  18320. ' BirdIntf: IBird;',
  18321. 'begin',
  18322. ' BirdIntf.Size:=BirdIntf.Size;',
  18323. '']);
  18324. ConvertProgram;
  18325. CheckSource('TestClassInterface_Corba',
  18326. LinesToStr([ // statements
  18327. 'rtl.createInterface(this, "IUnknown", "{00000000-0000-0000-C000-000000000046}", [], null);',
  18328. 'rtl.createInterface(this, "IBird", "{5BD1A53B-69BB-37EE-AF32-BEFB86D85B03}", ["GetSize", "SetSize", "DoIt"], this.IUnknown);',
  18329. 'rtl.createClass(this, "TObject", null, function () {',
  18330. ' this.$init = function () {',
  18331. ' };',
  18332. ' this.$final = function () {',
  18333. ' };',
  18334. '});',
  18335. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18336. ' rtl.addIntf(this, $mod.IBird);',
  18337. '});',
  18338. 'this.BirdIntf = null;',
  18339. '']),
  18340. LinesToStr([ // $mod.$main
  18341. ' $mod.BirdIntf.SetSize($mod.BirdIntf.GetSize());',
  18342. '']));
  18343. end;
  18344. procedure TTestModule.TestClassInterface_ProcExternalFail;
  18345. begin
  18346. StartProgram(false);
  18347. Add([
  18348. '{$interfaces corba}',
  18349. 'type',
  18350. ' IUnknown = interface',
  18351. ' procedure DoIt; external name ''foo'';',
  18352. ' end;',
  18353. 'begin']);
  18354. SetExpectedParserError(
  18355. 'Fields are not allowed in interface at token "Identifier external" in file test1.pp at line 6 column 21',
  18356. nParserNoFieldsAllowed);
  18357. ConvertProgram;
  18358. end;
  18359. procedure TTestModule.TestClassInterface_Overloads;
  18360. begin
  18361. StartProgram(false);
  18362. Add([
  18363. '{$interfaces corba}',
  18364. 'type',
  18365. ' integer = longint;',
  18366. ' IUnknown = interface',
  18367. ' procedure DoIt(i: integer);',
  18368. ' procedure DoIt(s: string);',
  18369. ' end;',
  18370. ' IBird = interface(IUnknown)',
  18371. ' procedure DoIt(b: boolean); overload;',
  18372. ' end;',
  18373. ' TObject = class',
  18374. ' end;',
  18375. ' TBird = class(TObject,IBird)',
  18376. ' procedure DoIt(o: TObject);',
  18377. ' procedure DoIt(s: string);',
  18378. ' procedure DoIt(i: integer);',
  18379. ' procedure DoIt(b: boolean);',
  18380. ' end;',
  18381. 'procedure TBird.DoIt(o: TObject); begin end;',
  18382. 'procedure TBird.DoIt(s: string); begin end;',
  18383. 'procedure TBird.DoIt(i: integer); begin end;',
  18384. 'procedure TBird.DoIt(b: boolean); begin end;',
  18385. 'var',
  18386. ' BirdIntf: IBird;',
  18387. 'begin',
  18388. ' BirdIntf.DoIt(3);',
  18389. ' BirdIntf.DoIt(''abc'');',
  18390. ' BirdIntf.DoIt(true);',
  18391. '']);
  18392. ConvertProgram;
  18393. CheckSource('TestClassInterface_Overloads',
  18394. LinesToStr([ // statements
  18395. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDC4-8A2AE2C59400}", ["DoIt", "DoIt$1"], null);',
  18396. 'rtl.createInterface(this, "IBird", "{8285DD5E-EA3E-396E-AE88-000B86AABF05}", ["DoIt$2"], this.IUnknown);',
  18397. 'rtl.createClass(this, "TObject", null, function () {',
  18398. ' this.$init = function () {',
  18399. ' };',
  18400. ' this.$final = function () {',
  18401. ' };',
  18402. '});',
  18403. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18404. ' this.DoIt = function (o) {',
  18405. ' };',
  18406. ' this.DoIt$1 = function (s) {',
  18407. ' };',
  18408. ' this.DoIt$2 = function (i) {',
  18409. ' };',
  18410. ' this.DoIt$3 = function (b) {',
  18411. ' };',
  18412. ' rtl.addIntf(this, $mod.IBird, {',
  18413. ' DoIt$2: "DoIt$3",',
  18414. ' DoIt: "DoIt$2"',
  18415. ' });',
  18416. '});',
  18417. 'this.BirdIntf = null;',
  18418. '']),
  18419. LinesToStr([ // $mod.$main
  18420. '$mod.BirdIntf.DoIt(3);',
  18421. '$mod.BirdIntf.DoIt$1("abc");',
  18422. '$mod.BirdIntf.DoIt$2(true);',
  18423. '']));
  18424. end;
  18425. procedure TTestModule.TestClassInterface_DuplicateGUIInIntfListFail;
  18426. begin
  18427. StartProgram(false);
  18428. Add([
  18429. '{$interfaces corba}',
  18430. 'type',
  18431. ' IBird = interface',
  18432. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  18433. ' end;',
  18434. ' IDog = interface',
  18435. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  18436. ' end;',
  18437. ' TObject = class(IBird,IDog)',
  18438. ' end;',
  18439. 'begin']);
  18440. SetExpectedPasResolverError('Duplicate GUID {4B3BA825-E0EC-4799-A19C-55F714A07959} in IDog and IBird',
  18441. nDuplicateGUIDXInYZ);
  18442. ConvertProgram;
  18443. end;
  18444. procedure TTestModule.TestClassInterface_DuplicateGUIInAncestorFail;
  18445. begin
  18446. StartProgram(false);
  18447. Add([
  18448. '{$interfaces corba}',
  18449. 'type',
  18450. ' IAnimal = interface',
  18451. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  18452. ' end;',
  18453. ' IBird = interface(IAnimal)',
  18454. ' end;',
  18455. ' IHawk = interface(IBird)',
  18456. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  18457. ' end;',
  18458. 'begin']);
  18459. SetExpectedPasResolverError('Duplicate GUID {4B3BA825-E0EC-4799-A19C-55F714A07959} in IHawk and IAnimal',
  18460. nDuplicateGUIDXInYZ);
  18461. ConvertProgram;
  18462. end;
  18463. procedure TTestModule.TestClassInterface_AncestorImpl;
  18464. begin
  18465. StartProgram(false);
  18466. Add([
  18467. '{$interfaces corba}',
  18468. 'type',
  18469. ' integer = longint;',
  18470. ' IUnknown = interface',
  18471. ' procedure DoIt(i: integer);',
  18472. ' end;',
  18473. ' IBird = interface',
  18474. ' procedure Fly(i: integer);',
  18475. ' end;',
  18476. ' TObject = class(IUnknown)',
  18477. ' procedure DoIt(i: integer);',
  18478. ' end;',
  18479. ' TBird = class(IBird)',
  18480. ' procedure Fly(i: integer);',
  18481. ' end;',
  18482. 'procedure TObject.DoIt(i: integer); begin end;',
  18483. 'procedure TBird.Fly(i: integer); begin end;',
  18484. 'begin',
  18485. '']);
  18486. ConvertProgram;
  18487. CheckSource('TestClassInterface_AncestorIntf',
  18488. LinesToStr([ // statements
  18489. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDC4-8A2800000000}", ["DoIt"], null);',
  18490. 'rtl.createInterface(this, "IBird", "{B92D5841-6264-3AE3-BF20-000000000000}", ["Fly"], null);',
  18491. 'rtl.createClass(this, "TObject", null, function () {',
  18492. ' this.$init = function () {',
  18493. ' };',
  18494. ' this.$final = function () {',
  18495. ' };',
  18496. ' this.DoIt = function (i) {',
  18497. ' };',
  18498. ' rtl.addIntf(this, $mod.IUnknown);',
  18499. '});',
  18500. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18501. ' this.Fly = function (i) {',
  18502. ' };',
  18503. ' rtl.addIntf(this, $mod.IBird);',
  18504. ' rtl.addIntf(this, $mod.IUnknown);',
  18505. '});',
  18506. '']),
  18507. LinesToStr([ // $mod.$main
  18508. '']));
  18509. end;
  18510. procedure TTestModule.TestClassInterface_ImplReintroduce;
  18511. begin
  18512. StartProgram(false);
  18513. Add([
  18514. '{$interfaces corba}',
  18515. 'type',
  18516. ' integer = longint;',
  18517. ' IBird = interface',
  18518. ' procedure DoIt(i: integer);',
  18519. ' end;',
  18520. ' TObject = class',
  18521. ' procedure DoIt(i: integer);',
  18522. ' end;',
  18523. ' TBird = class(IBird)',
  18524. ' procedure DoIt(i: integer); virtual; reintroduce;',
  18525. ' end;',
  18526. 'procedure TObject.DoIt(i: integer); begin end;',
  18527. 'procedure TBird.DoIt(i: integer); begin end;',
  18528. 'begin',
  18529. '']);
  18530. ConvertProgram;
  18531. CheckSource('TestClassInterface_ImplReintroduce',
  18532. LinesToStr([ // statements
  18533. 'rtl.createInterface(this, "IBird", "{B92D5841-6264-3AE2-8594-000000000000}", ["DoIt"], null);',
  18534. 'rtl.createClass(this, "TObject", null, function () {',
  18535. ' this.$init = function () {',
  18536. ' };',
  18537. ' this.$final = function () {',
  18538. ' };',
  18539. ' this.DoIt = function (i) {',
  18540. ' };',
  18541. '});',
  18542. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18543. ' this.DoIt$1 = function (i) {',
  18544. ' };',
  18545. ' rtl.addIntf(this, $mod.IBird, {',
  18546. ' DoIt: "DoIt$1"',
  18547. ' });',
  18548. '});',
  18549. '']),
  18550. LinesToStr([ // $mod.$main
  18551. '']));
  18552. end;
  18553. procedure TTestModule.TestClassInterface_MethodResolution;
  18554. begin
  18555. StartProgram(false);
  18556. Add([
  18557. '{$interfaces corba}',
  18558. 'type',
  18559. ' IUnknown = interface',
  18560. ' procedure Walk(i: longint);',
  18561. ' end;',
  18562. ' IBird = interface(IUnknown)',
  18563. ' procedure Walk(b: boolean); overload;',
  18564. ' procedure Fly(s: string);',
  18565. ' end;',
  18566. ' TObject = class',
  18567. ' end;',
  18568. ' TBird = class(TObject,IBird)',
  18569. ' procedure IBird.Fly = Move;',
  18570. ' procedure IBird.Walk = Hop;',
  18571. ' procedure Hop(i: longint);',
  18572. ' procedure Move(s: string);',
  18573. ' procedure Hop(b: boolean);',
  18574. ' end;',
  18575. 'procedure TBird.Move(s: string); begin end;',
  18576. 'procedure TBird.Hop(i: longint); begin end;',
  18577. 'procedure TBird.Hop(b: boolean); begin end;',
  18578. 'var',
  18579. ' BirdIntf: IBird;',
  18580. 'begin',
  18581. ' BirdIntf.Walk(3);',
  18582. ' BirdIntf.Walk(true);',
  18583. ' BirdIntf.Fly(''abc'');',
  18584. '']);
  18585. ConvertProgram;
  18586. CheckSource('TestClassInterface_MethodResolution',
  18587. LinesToStr([ // statements
  18588. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDD7-23D600000000}", ["Walk"], null);',
  18589. 'rtl.createInterface(this, "IBird", "{CF8A4986-80F6-396E-AE88-000B86AAE208}", ["Walk$1", "Fly"], this.IUnknown);',
  18590. 'rtl.createClass(this, "TObject", null, function () {',
  18591. ' this.$init = function () {',
  18592. ' };',
  18593. ' this.$final = function () {',
  18594. ' };',
  18595. '});',
  18596. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18597. ' this.Hop = function (i) {',
  18598. ' };',
  18599. ' this.Move = function (s) {',
  18600. ' };',
  18601. ' this.Hop$1 = function (b) {',
  18602. ' };',
  18603. ' rtl.addIntf(this, $mod.IBird, {',
  18604. ' Walk$1: "Hop$1",',
  18605. ' Fly: "Move",',
  18606. ' Walk: "Hop"',
  18607. ' });',
  18608. '});',
  18609. 'this.BirdIntf = null;',
  18610. '']),
  18611. LinesToStr([ // $mod.$main
  18612. '$mod.BirdIntf.Walk(3);',
  18613. '$mod.BirdIntf.Walk$1(true);',
  18614. '$mod.BirdIntf.Fly("abc");',
  18615. '']));
  18616. end;
  18617. procedure TTestModule.TestClassInterface_AncestorMoreInterfaces;
  18618. begin
  18619. StartProgram(false);
  18620. Add([
  18621. '{$interfaces com}',
  18622. 'type',
  18623. ' IUnknown = interface',
  18624. ' function _AddRef: longint;',
  18625. ' procedure Walk;',
  18626. ' end;',
  18627. ' IBird = interface end;',
  18628. ' IDog = interface end;',
  18629. ' TObject = class(IBird,IDog)',
  18630. ' function _AddRef: longint; virtual; abstract;',
  18631. ' procedure Walk; virtual; abstract;',
  18632. ' end;',
  18633. ' TBird = class(IUnknown)',
  18634. ' end;',
  18635. 'begin',
  18636. '']);
  18637. ConvertProgram;
  18638. CheckSource('TestClassInterface_COM_AncestorLess',
  18639. LinesToStr([ // statements
  18640. 'rtl.createInterface(this, "IUnknown", "{8F2D5841-758A-322B-BDDF-21CD521DD723}", ["_AddRef", "Walk"], null);',
  18641. 'rtl.createInterface(this, "IBird", "{CCE11D4C-6504-3AEE-AE88-000B86AAE675}", [], this.IUnknown);',
  18642. 'rtl.createInterface(this, "IDog", "{CCE11D4C-6504-3AEE-AE88-000B8E5FC675}", [], this.IUnknown);',
  18643. 'rtl.createClass(this, "TObject", null, function () {',
  18644. ' this.$init = function () {',
  18645. ' };',
  18646. ' this.$final = function () {',
  18647. ' };',
  18648. ' rtl.addIntf(this, $mod.IBird);',
  18649. ' rtl.addIntf(this, $mod.IDog);',
  18650. '});',
  18651. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18652. ' rtl.addIntf(this, $mod.IUnknown);',
  18653. ' rtl.addIntf(this, $mod.IBird);',
  18654. ' rtl.addIntf(this, $mod.IDog);',
  18655. '});',
  18656. '']),
  18657. LinesToStr([ // $mod.$main
  18658. '']));
  18659. end;
  18660. procedure TTestModule.TestClassInterface_MethodOverride;
  18661. begin
  18662. StartProgram(false);
  18663. Add([
  18664. '{$interfaces corba}',
  18665. 'type',
  18666. ' IUnknown = interface',
  18667. ' [''{D6D98E5B-8A10-4FEC-856A-7BFC847FE74B}'']',
  18668. ' procedure Go;',
  18669. ' end;',
  18670. ' TObject = class(IUnknown)',
  18671. ' procedure Go; virtual; abstract;',
  18672. ' end;',
  18673. ' TBird = class',
  18674. ' procedure Go; override;',
  18675. ' end;',
  18676. ' TCat = class(TObject)',
  18677. ' procedure Go; override;',
  18678. ' end;',
  18679. ' TDog = class(TObject, IUnknown)',
  18680. ' procedure Go; override;',
  18681. ' end;',
  18682. 'procedure TBird.Go; begin end;',
  18683. 'procedure TCat.Go; begin end;',
  18684. 'procedure TDog.Go; begin end;',
  18685. 'begin',
  18686. '']);
  18687. ConvertProgram;
  18688. CheckSource('TestClassInterface_MethodOverride',
  18689. LinesToStr([ // statements
  18690. 'rtl.createInterface(this, "IUnknown", "{D6D98E5B-8A10-4FEC-856A-7BFC847FE74B}", ["Go"], null);',
  18691. 'rtl.createClass(this, "TObject", null, function () {',
  18692. ' this.$init = function () {',
  18693. ' };',
  18694. ' this.$final = function () {',
  18695. ' };',
  18696. ' rtl.addIntf(this, $mod.IUnknown);',
  18697. '});',
  18698. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18699. ' this.Go = function () {',
  18700. ' };',
  18701. ' rtl.addIntf(this, $mod.IUnknown);',
  18702. '});',
  18703. 'rtl.createClass(this, "TCat", this.TObject, function () {',
  18704. ' this.Go = function () {',
  18705. ' };',
  18706. ' rtl.addIntf(this, $mod.IUnknown);',
  18707. '});',
  18708. 'rtl.createClass(this, "TDog", this.TObject, function () {',
  18709. ' this.Go = function () {',
  18710. ' };',
  18711. ' rtl.addIntf(this, $mod.IUnknown);',
  18712. '});',
  18713. '']),
  18714. LinesToStr([ // $mod.$main
  18715. '']));
  18716. end;
  18717. procedure TTestModule.TestClassInterface_Corba_Delegation;
  18718. begin
  18719. StartProgram(false);
  18720. Add([
  18721. '{$interfaces corba}',
  18722. 'type',
  18723. ' IUnknown = interface',
  18724. ' end;',
  18725. ' IBird = interface(IUnknown)',
  18726. ' procedure Fly(s: string);',
  18727. ' end;',
  18728. ' IEagle = interface(IBird)',
  18729. ' end;',
  18730. ' IDove = interface(IBird)',
  18731. ' end;',
  18732. ' ISwallow = interface(IBird)',
  18733. ' end;',
  18734. ' TObject = class',
  18735. ' end;',
  18736. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  18737. ' procedure Fly(s: string); virtual; abstract;',
  18738. ' end;',
  18739. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  18740. ' FBirdIntf: IBird;',
  18741. ' property BirdIntf: IBird read FBirdIntf implements IBird;',
  18742. ' function GetEagleIntf: IEagle; virtual; abstract;',
  18743. ' property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  18744. ' FDoveObj: TBird;',
  18745. ' property DoveObj: TBird read FDoveObj implements IDove;',
  18746. ' function GetSwallowObj: TBird; virtual; abstract;',
  18747. ' property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  18748. ' end;',
  18749. 'begin',
  18750. '']);
  18751. ConvertProgram;
  18752. CheckSource('TestClassInterface_Corba_Delegation',
  18753. LinesToStr([ // statements
  18754. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  18755. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  18756. 'rtl.createInterface(this, "IEagle", "{489289DE-FDE2-34A6-8288-39119022B1B4}", [], this.IBird);',
  18757. 'rtl.createInterface(this, "IDove", "{489289DE-FDE2-34A6-8288-39118EF16074}", [], this.IBird);',
  18758. 'rtl.createInterface(this, "ISwallow", "{B89289DE-FDE2-34A6-8288-3911CBDCB359}", [], this.IBird);',
  18759. 'rtl.createClass(this, "TObject", null, function () {',
  18760. ' this.$init = function () {',
  18761. ' };',
  18762. ' this.$final = function () {',
  18763. ' };',
  18764. '});',
  18765. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18766. ' rtl.addIntf(this, $mod.IBird);',
  18767. ' rtl.addIntf(this, $mod.IEagle);',
  18768. ' rtl.addIntf(this, $mod.IDove);',
  18769. ' rtl.addIntf(this, $mod.ISwallow);',
  18770. '});',
  18771. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  18772. ' this.$init = function () {',
  18773. ' $mod.TObject.$init.call(this);',
  18774. ' this.FBirdIntf = null;',
  18775. ' this.FDoveObj = null;',
  18776. ' };',
  18777. ' this.$final = function () {',
  18778. ' this.FBirdIntf = undefined;',
  18779. ' this.FDoveObj = undefined;',
  18780. ' $mod.TObject.$final.call(this);',
  18781. ' };',
  18782. ' this.$intfmaps = {',
  18783. ' "{478D080B-C0F6-396E-AE88-000B87785B07}": function () {',
  18784. ' return this.FBirdIntf;',
  18785. ' },',
  18786. ' "{489289DE-FDE2-34A6-8288-39119022B1B4}": function () {',
  18787. ' return this.GetEagleIntf();',
  18788. ' },',
  18789. ' "{489289DE-FDE2-34A6-8288-39118EF16074}": function () {',
  18790. ' return rtl.getIntfT(this.FDoveObj, $mod.IDove);',
  18791. ' },',
  18792. ' "{B89289DE-FDE2-34A6-8288-3911CBDCB359}": function () {',
  18793. ' return rtl.getIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  18794. ' }',
  18795. ' };',
  18796. '});',
  18797. '']),
  18798. LinesToStr([ // $mod.$main
  18799. '']));
  18800. end;
  18801. procedure TTestModule.TestClassInterface_Corba_DelegationStatic;
  18802. begin
  18803. StartProgram(false);
  18804. Add([
  18805. '{$interfaces corba}',
  18806. 'type',
  18807. ' IUnknown = interface',
  18808. ' end;',
  18809. ' IBird = interface(IUnknown)',
  18810. ' procedure Fly(s: string);',
  18811. ' end;',
  18812. ' IEagle = interface(IBird)',
  18813. ' end;',
  18814. ' IDove = interface(IBird)',
  18815. ' end;',
  18816. ' ISwallow = interface(IBird)',
  18817. ' end;',
  18818. ' TObject = class',
  18819. ' end;',
  18820. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  18821. ' procedure Fly(s: string); virtual; abstract;',
  18822. ' end;',
  18823. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  18824. ' private',
  18825. ' class var FBirdIntf: IBird;',
  18826. ' class var FDoveObj: TBird;',
  18827. ' class function GetEagleIntf: IEagle; virtual; abstract;',
  18828. ' class function GetSwallowObj: TBird; virtual; abstract;',
  18829. ' protected',
  18830. ' class property BirdIntf: IBird read FBirdIntf implements IBird;',
  18831. ' class property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  18832. ' class property DoveObj: TBird read FDoveObj implements IDove;',
  18833. ' class property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  18834. ' end;',
  18835. 'begin',
  18836. '']);
  18837. ConvertProgram;
  18838. CheckSource('TestClassInterface_Corba_DelegationStatic',
  18839. LinesToStr([ // statements
  18840. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  18841. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  18842. 'rtl.createInterface(this, "IEagle", "{489289DE-FDE2-34A6-8288-39119022B1B4}", [], this.IBird);',
  18843. 'rtl.createInterface(this, "IDove", "{489289DE-FDE2-34A6-8288-39118EF16074}", [], this.IBird);',
  18844. 'rtl.createInterface(this, "ISwallow", "{B89289DE-FDE2-34A6-8288-3911CBDCB359}", [], this.IBird);',
  18845. 'rtl.createClass(this, "TObject", null, function () {',
  18846. ' this.$init = function () {',
  18847. ' };',
  18848. ' this.$final = function () {',
  18849. ' };',
  18850. '});',
  18851. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18852. ' rtl.addIntf(this, $mod.IBird);',
  18853. ' rtl.addIntf(this, $mod.IEagle);',
  18854. ' rtl.addIntf(this, $mod.IDove);',
  18855. ' rtl.addIntf(this, $mod.ISwallow);',
  18856. '});',
  18857. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  18858. ' this.FBirdIntf = null;',
  18859. ' this.FDoveObj = null;',
  18860. ' this.$intfmaps = {',
  18861. ' "{478D080B-C0F6-396E-AE88-000B87785B07}": function () {',
  18862. ' return this.FBirdIntf;',
  18863. ' },',
  18864. ' "{489289DE-FDE2-34A6-8288-39119022B1B4}": function () {',
  18865. ' return this.GetEagleIntf();',
  18866. ' },',
  18867. ' "{489289DE-FDE2-34A6-8288-39118EF16074}": function () {',
  18868. ' return rtl.getIntfT(this.FDoveObj, $mod.IDove);',
  18869. ' },',
  18870. ' "{B89289DE-FDE2-34A6-8288-3911CBDCB359}": function () {',
  18871. ' return rtl.getIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  18872. ' }',
  18873. ' };',
  18874. '});',
  18875. '']),
  18876. LinesToStr([ // $mod.$main
  18877. '']));
  18878. end;
  18879. procedure TTestModule.TestClassInterface_Corba_Operators;
  18880. begin
  18881. StartProgram(false);
  18882. Add([
  18883. '{$interfaces corba}',
  18884. 'type',
  18885. ' IUnknown = interface',
  18886. ' end;',
  18887. ' IBird = interface(IUnknown)',
  18888. ' function GetItems(Index: longint): longint;',
  18889. ' procedure SetItems(Index: longint; Value: longint);',
  18890. ' property Items[Index: longint]: longint read GetItems write SetItems; default;',
  18891. ' end;',
  18892. ' TObject = class',
  18893. ' end;',
  18894. ' TBird = class(TObject,IBird)',
  18895. ' function GetItems(Index: longint): longint; virtual; abstract;',
  18896. ' procedure SetItems(Index: longint; Value: longint); virtual; abstract;',
  18897. ' end;',
  18898. 'var',
  18899. ' IntfVar: IBird = nil;',
  18900. ' IntfVar2: IBird;',
  18901. ' ObjVar: TBird;',
  18902. ' v: JSValue;',
  18903. 'begin',
  18904. ' IntfVar:=nil;',
  18905. ' IntfVar[3]:=IntfVar[4];',
  18906. ' if Assigned(IntfVar) then ;',
  18907. ' IntfVar:=IntfVar2;',
  18908. ' IntfVar:=ObjVar;',
  18909. ' if IntfVar=IntfVar2 then ;',
  18910. ' if IntfVar<>IntfVar2 then ;',
  18911. ' if IntfVar is IBird then ;',
  18912. ' if IntfVar is TBird then ;',
  18913. ' if ObjVar is IBird then ;',
  18914. ' IntfVar:=IntfVar2 as IBird;',
  18915. ' ObjVar:=IntfVar2 as TBird;',
  18916. ' IntfVar:=ObjVar as IBird;',
  18917. ' IntfVar:=IBird(IntfVar2);',
  18918. ' ObjVar:=TBird(IntfVar);',
  18919. ' IntfVar:=IBird(ObjVar);',
  18920. ' v:=IntfVar;',
  18921. ' IntfVar:=IBird(v);',
  18922. ' if v is IBird then ;',
  18923. ' v:=JSValue(IntfVar);',
  18924. ' v:=IBird;',
  18925. '']);
  18926. ConvertProgram;
  18927. CheckSource('TestClassInterface_Corba_Operators',
  18928. LinesToStr([ // statements
  18929. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  18930. 'rtl.createInterface(this, "IBird", "{D53FED90-DE59-3202-B1AE-000B87785B08}", ["GetItems", "SetItems"], this.IUnknown);',
  18931. 'rtl.createClass(this, "TObject", null, function () {',
  18932. ' this.$init = function () {',
  18933. ' };',
  18934. ' this.$final = function () {',
  18935. ' };',
  18936. '});',
  18937. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18938. ' rtl.addIntf(this, $mod.IBird);',
  18939. '});',
  18940. 'this.IntfVar = null;',
  18941. 'this.IntfVar2 = null;',
  18942. 'this.ObjVar = null;',
  18943. 'this.v = undefined;',
  18944. '']),
  18945. LinesToStr([ // $mod.$main
  18946. '$mod.IntfVar = null;',
  18947. '$mod.IntfVar.SetItems(3, $mod.IntfVar.GetItems(4));',
  18948. 'if ($mod.IntfVar != null) ;',
  18949. '$mod.IntfVar = $mod.IntfVar2;',
  18950. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar,$mod.IBird);',
  18951. 'if ($mod.IntfVar === $mod.IntfVar2) ;',
  18952. 'if ($mod.IntfVar !== $mod.IntfVar2) ;',
  18953. 'if ($mod.IBird.isPrototypeOf($mod.IntfVar)) ;',
  18954. 'if (rtl.intfIsClass($mod.IntfVar, $mod.TBird)) ;',
  18955. 'if (rtl.getIntfT($mod.ObjVar, $mod.IBird) !== null) ;',
  18956. '$mod.IntfVar = rtl.as($mod.IntfVar2, $mod.IBird);',
  18957. '$mod.ObjVar = rtl.intfAsClass($mod.IntfVar2, $mod.TBird);',
  18958. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar, $mod.IBird);',
  18959. '$mod.IntfVar = $mod.IntfVar2;',
  18960. '$mod.ObjVar = rtl.intfToClass($mod.IntfVar, $mod.TBird);',
  18961. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar, $mod.IBird);',
  18962. '$mod.v = $mod.IntfVar;',
  18963. '$mod.IntfVar = rtl.getObject($mod.v);',
  18964. 'if (rtl.isExt($mod.v, $mod.IBird, 1)) ;',
  18965. '$mod.v = $mod.IntfVar;',
  18966. '$mod.v = $mod.IBird;',
  18967. '']));
  18968. end;
  18969. procedure TTestModule.TestClassInterface_Corba_Args;
  18970. begin
  18971. StartProgram(false);
  18972. Add([
  18973. '{$interfaces corba}',
  18974. 'type',
  18975. ' IUnknown = interface',
  18976. ' end;',
  18977. ' IBird = interface(IUnknown)',
  18978. ' end;',
  18979. ' TObject = class',
  18980. ' end;',
  18981. ' TBird = class(TObject,IBird)',
  18982. ' end;',
  18983. 'procedure DoIt(var u; i: IBird; const j: IBird);',
  18984. 'begin',
  18985. ' DoIt(i,i,i);',
  18986. 'end;',
  18987. 'procedure Change(var i: IBird; out j: IBird);',
  18988. 'begin',
  18989. ' DoIt(i,i,i);',
  18990. ' Change(i,i);',
  18991. 'end;',
  18992. 'var',
  18993. ' i: IBird;',
  18994. ' o: TBird;',
  18995. 'begin',
  18996. ' DoIt(i,i,i);',
  18997. ' Change(i,i);',
  18998. ' DoIt(o,o,o);',
  18999. '']);
  19000. ConvertProgram;
  19001. CheckSource('TestClassInterface_Corba_Args',
  19002. LinesToStr([ // statements
  19003. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19004. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], this.IUnknown);',
  19005. 'rtl.createClass(this, "TObject", null, function () {',
  19006. ' this.$init = function () {',
  19007. ' };',
  19008. ' this.$final = function () {',
  19009. ' };',
  19010. '});',
  19011. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  19012. ' rtl.addIntf(this, $mod.IBird);',
  19013. '});',
  19014. 'this.DoIt = function (u, i, j) {',
  19015. ' $mod.DoIt({',
  19016. ' get: function () {',
  19017. ' return i;',
  19018. ' },',
  19019. ' set: function (v) {',
  19020. ' i = v;',
  19021. ' }',
  19022. ' }, i, i);',
  19023. '};',
  19024. 'this.Change = function (i, j) {',
  19025. ' $mod.DoIt(i, i.get(), i.get());',
  19026. ' $mod.Change(i, i);',
  19027. '};',
  19028. 'this.i = null;',
  19029. 'this.o = null;',
  19030. '']),
  19031. LinesToStr([ // $mod.$main
  19032. '$mod.DoIt({',
  19033. ' p: $mod,',
  19034. ' get: function () {',
  19035. ' return this.p.i;',
  19036. ' },',
  19037. ' set: function (v) {',
  19038. ' this.p.i = v;',
  19039. ' }',
  19040. '}, $mod.i, $mod.i);',
  19041. '$mod.Change({',
  19042. ' p: $mod,',
  19043. ' get: function () {',
  19044. ' return this.p.i;',
  19045. ' },',
  19046. ' set: function (v) {',
  19047. ' this.p.i = v;',
  19048. ' }',
  19049. '}, {',
  19050. ' p: $mod,',
  19051. ' get: function () {',
  19052. ' return this.p.i;',
  19053. ' },',
  19054. ' set: function (v) {',
  19055. ' this.p.i = v;',
  19056. ' }',
  19057. '});',
  19058. '$mod.DoIt({',
  19059. ' p: $mod,',
  19060. ' get: function () {',
  19061. ' return this.p.o;',
  19062. ' },',
  19063. ' set: function (v) {',
  19064. ' this.p.o = v;',
  19065. ' }',
  19066. '}, rtl.getIntfT($mod.o, $mod.IBird), rtl.getIntfT($mod.o, $mod.IBird));',
  19067. '']));
  19068. end;
  19069. procedure TTestModule.TestClassInterface_Corba_ForIn;
  19070. begin
  19071. StartProgram(false);
  19072. Add([
  19073. '{$interfaces corba}',
  19074. 'type',
  19075. ' IUnknown = interface end;',
  19076. ' TObject = class',
  19077. ' Id: longint;',
  19078. ' end;',
  19079. ' IEnumerator = interface(IUnknown)',
  19080. ' function GetCurrent: TObject;',
  19081. ' function MoveNext: Boolean;',
  19082. ' property Current: TObject read GetCurrent;',
  19083. ' end;',
  19084. ' IEnumerable = interface(IUnknown)',
  19085. ' function GetEnumerator: IEnumerator;',
  19086. ' end;',
  19087. 'var',
  19088. ' o: TObject;',
  19089. ' i: IEnumerable;',
  19090. 'begin',
  19091. ' for o in i do o.Id:=3;',
  19092. '']);
  19093. ConvertProgram;
  19094. CheckSource('TestClassInterface_Corba_ForIn',
  19095. LinesToStr([ // statements
  19096. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19097. 'rtl.createClass(this, "TObject", null, function () {',
  19098. ' this.$init = function () {',
  19099. ' this.Id = 0;',
  19100. ' };',
  19101. ' this.$final = function () {',
  19102. ' };',
  19103. '});',
  19104. 'rtl.createInterface(this, "IEnumerator", "{95D7745D-ED61-3F13-BBE4-07708161999E}", ["GetCurrent", "MoveNext"], this.IUnknown);',
  19105. 'rtl.createInterface(this, "IEnumerable", "{8CC9D45D-ED7D-3B73-96B6-290B931BB19E}", ["GetEnumerator"], this.IUnknown);',
  19106. 'this.o = null;',
  19107. 'this.i = null;',
  19108. '']),
  19109. LinesToStr([ // $mod.$main
  19110. 'var $in = $mod.i.GetEnumerator();',
  19111. 'while ($in.MoveNext()) {',
  19112. ' $mod.o = $in.GetCurrent();',
  19113. ' $mod.o.Id = 3;',
  19114. '};',
  19115. '']));
  19116. end;
  19117. procedure TTestModule.TestClassInterface_COM_AssignVar;
  19118. begin
  19119. StartProgram(false);
  19120. Add([
  19121. '{$interfaces com}',
  19122. 'type',
  19123. ' IUnknown = interface',
  19124. ' function _AddRef: longint;',
  19125. ' function _Release: longint;',
  19126. ' end;',
  19127. ' TObject = class(IUnknown)',
  19128. ' function _AddRef: longint; virtual; abstract;',
  19129. ' function _Release: longint; virtual; abstract;',
  19130. ' end;',
  19131. 'var',
  19132. ' i: IUnknown;',
  19133. 'procedure DoGlobal(o: TObject);',
  19134. 'begin',
  19135. ' i:=nil;',
  19136. ' i:=o;',
  19137. ' i:=i;',
  19138. 'end;',
  19139. 'procedure DoLocal(o: TObject);',
  19140. 'const k: IUnknown = nil;',
  19141. 'var j: IUnknown;',
  19142. 'begin',
  19143. ' k:=o;',
  19144. ' k:=i;',
  19145. ' j:=o;',
  19146. ' j:=i;',
  19147. 'end;',
  19148. 'var o: TObject;',
  19149. 'begin',
  19150. ' i:=nil;',
  19151. ' i:=o;',
  19152. '']);
  19153. ConvertProgram;
  19154. CheckSource('TestClassInterface_COM_AssignVar',
  19155. LinesToStr([ // statements
  19156. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19157. 'rtl.createClass(this, "TObject", null, function () {',
  19158. ' this.$init = function () {',
  19159. ' };',
  19160. ' this.$final = function () {',
  19161. ' };',
  19162. ' rtl.addIntf(this, $mod.IUnknown);',
  19163. '});',
  19164. 'this.i = null;',
  19165. 'this.DoGlobal = function (o) {',
  19166. ' rtl.setIntfP($mod, "i", null);',
  19167. ' rtl.setIntfP($mod, "i", rtl.queryIntfT(o, $mod.IUnknown), true);',
  19168. ' rtl.setIntfP($mod, "i", $mod.i);',
  19169. '};',
  19170. 'var k = null;',
  19171. 'this.DoLocal = function (o) {',
  19172. ' var j = null;',
  19173. ' try{',
  19174. ' k = rtl.setIntfL(k, rtl.queryIntfT(o, $mod.IUnknown), true);',
  19175. ' k = rtl.setIntfL(k, $mod.i);',
  19176. ' j = rtl.setIntfL(j, rtl.queryIntfT(o, $mod.IUnknown), true);',
  19177. ' j = rtl.setIntfL(j, $mod.i);',
  19178. ' }finally{',
  19179. ' rtl._Release(j);',
  19180. ' };',
  19181. '};',
  19182. 'this.o = null;',
  19183. '']),
  19184. LinesToStr([ // $mod.$main
  19185. 'rtl.setIntfP($mod, "i", null);',
  19186. 'rtl.setIntfP($mod, "i", rtl.queryIntfT($mod.o, $mod.IUnknown), true);',
  19187. '']));
  19188. end;
  19189. procedure TTestModule.TestClassInterface_COM_AssignArg;
  19190. begin
  19191. StartProgram(false);
  19192. Add([
  19193. '{$interfaces com}',
  19194. 'type',
  19195. ' IUnknown = interface',
  19196. ' function _AddRef: longint;',
  19197. ' function _Release: longint;',
  19198. ' end;',
  19199. ' TObject = class(IUnknown)',
  19200. ' function _AddRef: longint; virtual; abstract;',
  19201. ' function _Release: longint; virtual; abstract;',
  19202. ' end;',
  19203. 'procedure DoDefault(i, j: IUnknown);',
  19204. 'begin',
  19205. ' i:=nil;',
  19206. ' i:=j;',
  19207. 'end;',
  19208. 'begin',
  19209. '']);
  19210. ConvertProgram;
  19211. CheckSource('TestClassInterface_COM_AssignArg',
  19212. LinesToStr([ // statements
  19213. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19214. 'rtl.createClass(this, "TObject", null, function () {',
  19215. ' this.$init = function () {',
  19216. ' };',
  19217. ' this.$final = function () {',
  19218. ' };',
  19219. ' rtl.addIntf(this, $mod.IUnknown);',
  19220. '});',
  19221. 'this.DoDefault = function (i, j) {',
  19222. ' rtl._AddRef(i);',
  19223. ' try {',
  19224. ' i = rtl.setIntfL(i, null);',
  19225. ' i = rtl.setIntfL(i, j);',
  19226. ' } finally {',
  19227. ' rtl._Release(i);',
  19228. ' };',
  19229. '};',
  19230. '']),
  19231. LinesToStr([ // $mod.$main
  19232. '']));
  19233. end;
  19234. procedure TTestModule.TestClassInterface_COM_FunctionResult;
  19235. begin
  19236. StartProgram(false);
  19237. Add([
  19238. '{$interfaces com}',
  19239. 'type',
  19240. ' IUnknown = interface',
  19241. ' function _AddRef: longint;',
  19242. ' function _Release: longint;',
  19243. ' end;',
  19244. ' TObject = class(IUnknown)',
  19245. ' function _AddRef: longint; virtual; abstract;',
  19246. ' function _Release: longint; virtual; abstract;',
  19247. ' end;',
  19248. 'function DoDefault(i: IUnknown): IUnknown;',
  19249. 'begin',
  19250. ' Result:=i;',
  19251. ' if Result<>nil then exit;',
  19252. 'end;',
  19253. 'begin',
  19254. '']);
  19255. ConvertProgram;
  19256. CheckSource('TestClassInterface_COM_FunctionResult',
  19257. LinesToStr([ // statements
  19258. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19259. 'rtl.createClass(this, "TObject", null, function () {',
  19260. ' this.$init = function () {',
  19261. ' };',
  19262. ' this.$final = function () {',
  19263. ' };',
  19264. ' rtl.addIntf(this, $mod.IUnknown);',
  19265. '});',
  19266. 'this.DoDefault = function (i) {',
  19267. ' var Result = null;',
  19268. ' var $ok = false;',
  19269. ' try {',
  19270. ' Result = rtl.setIntfL(Result, i);',
  19271. ' if(Result !== null){',
  19272. ' $ok = true;',
  19273. ' return Result;',
  19274. ' };',
  19275. ' $ok = true;',
  19276. ' } finally {',
  19277. ' if(!$ok) rtl._Release(Result);',
  19278. ' };',
  19279. ' return Result;',
  19280. '};',
  19281. '']),
  19282. LinesToStr([ // $mod.$main
  19283. '']));
  19284. end;
  19285. procedure TTestModule.TestClassInterface_COM_InheritedFuncResult;
  19286. begin
  19287. StartProgram(false);
  19288. Add([
  19289. '{$interfaces com}',
  19290. 'type',
  19291. ' IUnknown = interface',
  19292. ' function _AddRef: longint;',
  19293. ' function _Release: longint;',
  19294. ' end;',
  19295. ' TObject = class(IUnknown)',
  19296. ' function _AddRef: longint; virtual; abstract;',
  19297. ' function _Release: longint; virtual; abstract;',
  19298. ' function GetIntf: IUnknown; virtual;',
  19299. ' end;',
  19300. ' TMouse = class',
  19301. ' function GetIntf: IUnknown; override;',
  19302. ' end;',
  19303. 'function TObject.GetIntf: IUnknown; begin end;',
  19304. 'function TMouse.GetIntf: IUnknown;',
  19305. 'var i: IUnknown;',
  19306. 'begin',
  19307. ' inherited;',
  19308. ' inherited GetIntf;',
  19309. ' inherited GetIntf();',
  19310. ' Result:=inherited GetIntf;',
  19311. ' Result:=inherited GetIntf();',
  19312. ' i:=inherited GetIntf;',
  19313. ' i:=inherited GetIntf();',
  19314. 'end;',
  19315. 'begin',
  19316. '']);
  19317. ConvertProgram;
  19318. CheckSource('TestClassInterface_COM_InheritedFuncResult',
  19319. LinesToStr([ // statements
  19320. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19321. 'rtl.createClass(this, "TObject", null, function () {',
  19322. ' this.$init = function () {',
  19323. ' };',
  19324. ' this.$final = function () {',
  19325. ' };',
  19326. ' this.GetIntf = function () {',
  19327. ' var Result = null;',
  19328. ' return Result;',
  19329. ' };',
  19330. ' rtl.addIntf(this, $mod.IUnknown);',
  19331. '});',
  19332. 'rtl.createClass(this, "TMouse", this.TObject, function () {',
  19333. ' this.GetIntf = function () {',
  19334. ' var Result = null;',
  19335. ' var i = null;',
  19336. ' var $ir = rtl.createIntfRefs();',
  19337. ' var $ok = false;',
  19338. ' try {',
  19339. ' $ir.ref(1, $mod.TObject.GetIntf.call(this));',
  19340. ' $ir.ref(2, $mod.TObject.GetIntf.call(this));',
  19341. ' $ir.ref(3, $mod.TObject.GetIntf.call(this));',
  19342. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  19343. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  19344. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  19345. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  19346. ' $ok = true;',
  19347. ' } finally {',
  19348. ' $ir.free();',
  19349. ' rtl._Release(i);',
  19350. ' if (!$ok) rtl._Release(Result);',
  19351. ' };',
  19352. ' return Result;',
  19353. ' };',
  19354. ' rtl.addIntf(this, $mod.IUnknown);',
  19355. '});',
  19356. '']),
  19357. LinesToStr([ // $mod.$main
  19358. '']));
  19359. end;
  19360. procedure TTestModule.TestClassInterface_COM_IsAsTypeCasts;
  19361. begin
  19362. StartProgram(false);
  19363. Add([
  19364. '{$interfaces com}',
  19365. 'type',
  19366. ' IUnknown = interface',
  19367. ' function _AddRef: longint;',
  19368. ' function _Release: longint;',
  19369. ' end;',
  19370. ' TObject = class(IUnknown)',
  19371. ' function _AddRef: longint; virtual; abstract;',
  19372. ' function _Release: longint; virtual; abstract;',
  19373. ' end;',
  19374. 'procedure DoDefault(i, j: IUnknown; o: TObject);',
  19375. 'begin',
  19376. ' if i is IUnknown then ;',
  19377. ' if o is IUnknown then ;',
  19378. ' if i is TObject then ;',
  19379. ' i:=j as IUnknown;',
  19380. ' i:=o as IUnknown;',
  19381. ' o:=j as TObject;',
  19382. ' i:=IUnknown(j);',
  19383. ' i:=IUnknown(o);',
  19384. ' o:=TObject(i);',
  19385. 'end;',
  19386. 'begin',
  19387. '']);
  19388. ConvertProgram;
  19389. CheckSource('TestClassInterface_COM_IsAsTypeCasts',
  19390. LinesToStr([ // statements
  19391. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19392. 'rtl.createClass(this, "TObject", null, function () {',
  19393. ' this.$init = function () {',
  19394. ' };',
  19395. ' this.$final = function () {',
  19396. ' };',
  19397. ' rtl.addIntf(this, $mod.IUnknown);',
  19398. '});',
  19399. 'this.DoDefault = function (i, j, o) {',
  19400. ' rtl._AddRef(i);',
  19401. ' try {',
  19402. ' if (rtl.intfIsIntfT(i, $mod.IUnknown)) ;',
  19403. ' if (rtl.queryIntfIsT(o, $mod.IUnknown)) ;',
  19404. ' if (rtl.intfIsClass(i, $mod.TObject)) ;',
  19405. ' i = rtl.setIntfL(i, rtl.intfAsIntfT(j, $mod.IUnknown));',
  19406. ' i = rtl.setIntfL(i, rtl.queryIntfT(o, $mod.IUnknown), true);',
  19407. ' o = rtl.intfAsClass(j, $mod.TObject);',
  19408. ' i = rtl.setIntfL(i, j);',
  19409. ' i = rtl.setIntfL(i, rtl.queryIntfT(o, $mod.IUnknown), true);',
  19410. ' o = rtl.intfToClass(i, $mod.TObject);',
  19411. ' } finally {',
  19412. ' rtl._Release(i);',
  19413. ' };',
  19414. '};',
  19415. '']),
  19416. LinesToStr([ // $mod.$main
  19417. '']));
  19418. end;
  19419. procedure TTestModule.TestClassInterface_COM_PassAsArg;
  19420. begin
  19421. StartProgram(false);
  19422. Add([
  19423. '{$interfaces com}',
  19424. 'type',
  19425. ' IUnknown = interface',
  19426. ' function _AddRef: longint;',
  19427. ' function _Release: longint;',
  19428. ' end;',
  19429. ' TObject = class(IUnknown)',
  19430. ' function _AddRef: longint; virtual; abstract;',
  19431. ' function _Release: longint; virtual; abstract;',
  19432. ' end;',
  19433. 'procedure DoIt(v: IUnknown; const j: IUnknown; var k: IUnknown; out l: IUnknown);',
  19434. 'var o: TObject;',
  19435. 'begin',
  19436. ' DoIt(v,v,v,v);',
  19437. ' DoIt(o,o,k,k);',
  19438. 'end;',
  19439. 'procedure DoSome;',
  19440. 'var v: IUnknown;',
  19441. 'begin',
  19442. ' DoIt(v,v,v,v);',
  19443. 'end;',
  19444. 'var i: IUnknown;',
  19445. 'begin',
  19446. ' DoIt(i,i,i,i);',
  19447. '']);
  19448. ConvertProgram;
  19449. CheckSource('TestClassInterface_COM_PassAsArg',
  19450. LinesToStr([ // statements
  19451. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19452. 'rtl.createClass(this, "TObject", null, function () {',
  19453. ' this.$init = function () {',
  19454. ' };',
  19455. ' this.$final = function () {',
  19456. ' };',
  19457. ' rtl.addIntf(this, $mod.IUnknown);',
  19458. '});',
  19459. 'this.DoIt = function (v, j, k, l) {',
  19460. ' var o = null;',
  19461. ' var $ir = rtl.createIntfRefs();',
  19462. ' rtl._AddRef(v);',
  19463. ' try {',
  19464. ' $mod.DoIt(v, v, {',
  19465. ' get: function () {',
  19466. ' return v;',
  19467. ' },',
  19468. ' set: function (w) {',
  19469. ' v = rtl.setIntfL(v, w);',
  19470. ' }',
  19471. ' }, {',
  19472. ' get: function () {',
  19473. ' return v;',
  19474. ' },',
  19475. ' set: function (w) {',
  19476. ' v = rtl.setIntfL(v, w);',
  19477. ' }',
  19478. ' });',
  19479. ' $mod.DoIt($ir.ref(1, rtl.queryIntfT(o, $mod.IUnknown)), $ir.ref(2, rtl.queryIntfT(o, $mod.IUnknown)), k, k);',
  19480. ' } finally {',
  19481. ' $ir.free();',
  19482. ' rtl._Release(v);',
  19483. ' };',
  19484. '};',
  19485. 'this.DoSome = function () {',
  19486. ' var v = null;',
  19487. ' try {',
  19488. ' $mod.DoIt(v, v, {',
  19489. ' get: function () {',
  19490. ' return v;',
  19491. ' },',
  19492. ' set: function (w) {',
  19493. ' v = rtl.setIntfL(v, w);',
  19494. ' }',
  19495. ' }, {',
  19496. ' get: function () {',
  19497. ' return v;',
  19498. ' },',
  19499. ' set: function (w) {',
  19500. ' v = rtl.setIntfL(v, w);',
  19501. ' }',
  19502. ' });',
  19503. ' } finally {',
  19504. ' rtl._Release(v);',
  19505. ' };',
  19506. '};',
  19507. 'this.i = null;',
  19508. '']),
  19509. LinesToStr([ // $mod.$main
  19510. '$mod.DoIt($mod.i, $mod.i, {',
  19511. ' p: $mod,',
  19512. ' get: function () {',
  19513. ' return this.p.i;',
  19514. ' },',
  19515. ' set: function (v) {',
  19516. ' rtl.setIntfP(this.p, "i", v);',
  19517. ' }',
  19518. '}, {',
  19519. ' p: $mod,',
  19520. ' get: function () {',
  19521. ' return this.p.i;',
  19522. ' },',
  19523. ' set: function (v) {',
  19524. ' rtl.setIntfP(this.p, "i", v);',
  19525. ' }',
  19526. '});',
  19527. '']));
  19528. end;
  19529. procedure TTestModule.TestClassInterface_COM_PassToUntypedParam;
  19530. begin
  19531. StartProgram(false);
  19532. Add([
  19533. '{$interfaces com}',
  19534. 'type',
  19535. ' IUnknown = interface',
  19536. ' function _AddRef: longint;',
  19537. ' function _Release: longint;',
  19538. ' end;',
  19539. ' TObject = class(IUnknown)',
  19540. ' function _AddRef: longint; virtual; abstract;',
  19541. ' function _Release: longint; virtual; abstract;',
  19542. ' end;',
  19543. 'procedure DoIt(out i);',
  19544. 'begin end;',
  19545. 'procedure DoSome;',
  19546. 'var v: IUnknown;',
  19547. 'begin',
  19548. ' DoIt(v);',
  19549. 'end;',
  19550. 'function GetIt: IUnknown;',
  19551. 'begin',
  19552. ' DoIt(Result);',
  19553. 'end;',
  19554. 'var i: IUnknown;',
  19555. 'begin',
  19556. ' DoIt(i);',
  19557. '']);
  19558. ConvertProgram;
  19559. CheckSource('TestClassInterface_COM_PassToUntypedParam',
  19560. LinesToStr([ // statements
  19561. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19562. 'rtl.createClass(this, "TObject", null, function () {',
  19563. ' this.$init = function () {',
  19564. ' };',
  19565. ' this.$final = function () {',
  19566. ' };',
  19567. ' rtl.addIntf(this, $mod.IUnknown);',
  19568. '});',
  19569. 'this.DoIt = function (i) {',
  19570. '};',
  19571. 'this.DoSome = function () {',
  19572. ' var v = null;',
  19573. ' try {',
  19574. ' $mod.DoIt({',
  19575. ' get: function () {',
  19576. ' return v;',
  19577. ' },',
  19578. ' set: function (w) {',
  19579. ' v = w;',
  19580. ' }',
  19581. ' });',
  19582. ' } finally {',
  19583. ' rtl._Release(v);',
  19584. ' };',
  19585. '};',
  19586. 'this.GetIt = function () {',
  19587. ' var Result = null;',
  19588. ' var $ok = false;',
  19589. ' try {',
  19590. ' $mod.DoIt({',
  19591. ' get: function () {',
  19592. ' return Result;',
  19593. ' },',
  19594. ' set: function (v) {',
  19595. ' Result = v;',
  19596. ' }',
  19597. ' });',
  19598. ' $ok = true;',
  19599. ' } finally {',
  19600. ' if (!$ok) rtl._Release(Result);',
  19601. ' };',
  19602. ' return Result;',
  19603. '};',
  19604. 'this.i = null;',
  19605. '']),
  19606. LinesToStr([ // $mod.$main
  19607. 'try {',
  19608. ' $mod.DoIt({',
  19609. ' p: $mod,',
  19610. ' get: function () {',
  19611. ' return this.p.i;',
  19612. ' },',
  19613. ' set: function (v) {',
  19614. ' this.p.i = v;',
  19615. ' }',
  19616. ' });',
  19617. '} finally {',
  19618. ' rtl._Release($mod.i);',
  19619. '};',
  19620. '']));
  19621. end;
  19622. procedure TTestModule.TestClassInterface_COM_FunctionInExpr;
  19623. begin
  19624. StartProgram(false);
  19625. Add([
  19626. '{$interfaces com}',
  19627. 'type',
  19628. ' IUnknown = interface',
  19629. ' function _AddRef: longint;',
  19630. ' function _Release: longint;',
  19631. ' end;',
  19632. ' TObject = class(IUnknown)',
  19633. ' function _AddRef: longint; virtual; abstract;',
  19634. ' function _Release: longint; virtual; abstract;',
  19635. ' end;',
  19636. 'function GetIt: IUnknown;',
  19637. 'begin',
  19638. 'end;',
  19639. 'procedure DoSome;',
  19640. 'var v: IUnknown;',
  19641. ' i: longint;',
  19642. 'begin',
  19643. ' v:=GetIt;',
  19644. ' v:=GetIt();',
  19645. ' GetIt()._AddRef;',
  19646. ' i:=GetIt()._AddRef;',
  19647. 'end;',
  19648. 'var v: IUnknown;',
  19649. ' i: longint;',
  19650. 'begin',
  19651. ' v:=GetIt;',
  19652. ' v:=GetIt();',
  19653. ' GetIt()._AddRef;',
  19654. ' i:=GetIt()._AddRef;',
  19655. '']);
  19656. ConvertProgram;
  19657. CheckSource('TestClassInterface_COM_FunctionInExpr',
  19658. LinesToStr([ // statements
  19659. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19660. 'rtl.createClass(this, "TObject", null, function () {',
  19661. ' this.$init = function () {',
  19662. ' };',
  19663. ' this.$final = function () {',
  19664. ' };',
  19665. ' rtl.addIntf(this, $mod.IUnknown);',
  19666. '});',
  19667. 'this.GetIt = function () {',
  19668. ' var Result = null;',
  19669. ' return Result;',
  19670. '};',
  19671. 'this.DoSome = function () {',
  19672. ' var v = null;',
  19673. ' var i = 0;',
  19674. ' var $ir = rtl.createIntfRefs();',
  19675. ' try {',
  19676. ' v = rtl.setIntfL(v, $mod.GetIt(), true);',
  19677. ' v = rtl.setIntfL(v, $mod.GetIt(), true);',
  19678. ' $ir.ref(1, $mod.GetIt())._AddRef();',
  19679. ' i = $ir.ref(2, $mod.GetIt())._AddRef();',
  19680. ' } finally {',
  19681. ' $ir.free();',
  19682. ' rtl._Release(v);',
  19683. ' };',
  19684. '};',
  19685. 'this.v = null;',
  19686. 'this.i = 0;',
  19687. '']),
  19688. LinesToStr([ // $mod.$main
  19689. 'var $ir = rtl.createIntfRefs();',
  19690. 'try {',
  19691. ' rtl.setIntfP($mod, "v", $mod.GetIt(), true);',
  19692. ' rtl.setIntfP($mod, "v", $mod.GetIt(), true);',
  19693. ' $ir.ref(1, $mod.GetIt())._AddRef();',
  19694. ' $mod.i = $ir.ref(2, $mod.GetIt())._AddRef();',
  19695. '} finally {',
  19696. ' $ir.free();',
  19697. '};',
  19698. '']));
  19699. end;
  19700. procedure TTestModule.TestClassInterface_COM_Property;
  19701. begin
  19702. StartProgram(false);
  19703. Add([
  19704. '{$interfaces com}',
  19705. 'type',
  19706. ' IUnknown = interface',
  19707. ' function _AddRef: longint;',
  19708. ' function _Release: longint;',
  19709. ' end;',
  19710. ' TObject = class(IUnknown)',
  19711. ' FAnt: IUnknown;',
  19712. ' function _AddRef: longint; virtual; abstract;',
  19713. ' function _Release: longint; virtual; abstract;',
  19714. ' function GetBird: IUnknown; virtual; abstract;',
  19715. ' procedure SetBird(Value: IUnknown); virtual; abstract;',
  19716. ' function GetItems(Index: longint): IUnknown; virtual; abstract;',
  19717. ' procedure SetItems(Index: longint; Value: IUnknown); virtual; abstract;',
  19718. ' property Ant: IUnknown read FAnt write FAnt;',
  19719. ' property Bird: IUnknown read GetBird write SetBird;',
  19720. ' property Items[Index: longint]: IUnknown read GetItems write SetItems; default;',
  19721. ' end;',
  19722. 'procedure DoIt;',
  19723. 'var',
  19724. ' o: TObject;',
  19725. ' v: IUnknown;',
  19726. 'begin',
  19727. ' v:=o.Ant;',
  19728. ' o.Ant:=v;',
  19729. ' o.Ant:=o.Ant;',
  19730. ' v:=o.Bird;',
  19731. ' o.Bird:=v;',
  19732. ' o.Bird:=o.Bird;',
  19733. ' v:=o.Items[1];',
  19734. ' o.Items[2]:=v;',
  19735. ' o.Items[3]:=o.Items[4];',
  19736. ' v:=o[5];',
  19737. ' o[6]:=v;',
  19738. ' o[7]:=o[8];',
  19739. 'end;',
  19740. 'begin',
  19741. '']);
  19742. ConvertProgram;
  19743. CheckSource('TestClassInterface_COM_Property',
  19744. LinesToStr([ // statements
  19745. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19746. 'rtl.createClass(this, "TObject", null, function () {',
  19747. ' this.$init = function () {',
  19748. ' this.FAnt = null;',
  19749. ' };',
  19750. ' this.$final = function () {',
  19751. ' this.FAnt = undefined;',
  19752. ' };',
  19753. ' rtl.addIntf(this, $mod.IUnknown);',
  19754. '});',
  19755. 'this.DoIt = function () {',
  19756. ' var o = null;',
  19757. ' var v = null;',
  19758. ' var $ir = rtl.createIntfRefs();',
  19759. ' try {',
  19760. ' v = rtl.setIntfL(v, o.FAnt);',
  19761. ' rtl.setIntfP(o, "FAnt", v);',
  19762. ' rtl.setIntfP(o, "FAnt", o.FAnt);',
  19763. ' v = rtl.setIntfL(v, o.GetBird(), true);',
  19764. ' o.SetBird(v);',
  19765. ' o.SetBird($ir.ref(1, o.GetBird()));',
  19766. ' v = rtl.setIntfL(v, o.GetItems(1), true);',
  19767. ' o.SetItems(2, v);',
  19768. ' o.SetItems(3, $ir.ref(2, o.GetItems(4)));',
  19769. ' v = rtl.setIntfL(v, o.GetItems(5), true);',
  19770. ' o.SetItems(6, v);',
  19771. ' o.SetItems(7, $ir.ref(3, o.GetItems(8)));',
  19772. ' } finally {',
  19773. ' $ir.free();',
  19774. ' rtl._Release(v);',
  19775. ' };',
  19776. '};',
  19777. '']),
  19778. LinesToStr([ // $mod.$main
  19779. '']));
  19780. end;
  19781. procedure TTestModule.TestClassInterface_COM_IntfProperty;
  19782. begin
  19783. StartProgram(false);
  19784. Add([
  19785. '{$interfaces com}',
  19786. 'type',
  19787. ' IUnknown = interface',
  19788. ' function _AddRef: longint;',
  19789. ' function _Release: longint;',
  19790. ' function GetBird: IUnknown;',
  19791. ' procedure SetBird(Value: IUnknown);',
  19792. ' function GetItems(Index: longint): IUnknown;',
  19793. ' procedure SetItems(Index: longint; Value: IUnknown);',
  19794. ' property Bird: IUnknown read GetBird write SetBird;',
  19795. ' property Items[Index: longint]: IUnknown read GetItems write SetItems; default;',
  19796. ' end;',
  19797. ' TObject = class(IUnknown)',
  19798. ' function _AddRef: longint; virtual; abstract;',
  19799. ' function _Release: longint; virtual; abstract;',
  19800. ' function GetBird: IUnknown; virtual; abstract;',
  19801. ' procedure SetBird(Value: IUnknown); virtual; abstract;',
  19802. ' function GetItems(Index: longint): IUnknown; virtual; abstract;',
  19803. ' procedure SetItems(Index: longint; Value: IUnknown); virtual; abstract;',
  19804. ' end;',
  19805. 'procedure DoIt;',
  19806. 'var',
  19807. ' o: TObject;',
  19808. ' v: IUnknown;',
  19809. 'begin',
  19810. ' v:=v.Items[1];',
  19811. ' v.Items[2]:=v;',
  19812. ' v.Items[3]:=v.Items[4];',
  19813. ' v:=v[5];',
  19814. ' v[6]:=v;',
  19815. ' v[7]:=v[8];',
  19816. ' v[9].Bird.Bird:=v;',
  19817. ' v:=v.Bird[10].Bird',
  19818. 'end;',
  19819. 'begin',
  19820. '']);
  19821. ConvertProgram;
  19822. CheckSource('TestClassInterface_COM_IntfProperty',
  19823. LinesToStr([ // statements
  19824. 'rtl.createInterface(this, "IUnknown", "{385F5482-571B-338C-8130-4E97F330543B}", [',
  19825. ' "_AddRef",',
  19826. ' "_Release",',
  19827. ' "GetBird",',
  19828. ' "SetBird",',
  19829. ' "GetItems",',
  19830. ' "SetItems"',
  19831. '], null);',
  19832. 'rtl.createClass(this, "TObject", null, function () {',
  19833. ' this.$init = function () {',
  19834. ' };',
  19835. ' this.$final = function () {',
  19836. ' };',
  19837. ' rtl.addIntf(this, $mod.IUnknown);',
  19838. '});',
  19839. 'this.DoIt = function () {',
  19840. ' var o = null;',
  19841. ' var v = null;',
  19842. ' var $ir = rtl.createIntfRefs();',
  19843. ' try {',
  19844. ' v = rtl.setIntfL(v, v.GetItems(1), true);',
  19845. ' v.SetItems(2, v);',
  19846. ' v.SetItems(3, $ir.ref(1, v.GetItems(4)));',
  19847. ' v = rtl.setIntfL(v, v.GetItems(5), true);',
  19848. ' v.SetItems(6, v);',
  19849. ' v.SetItems(7, $ir.ref(2, v.GetItems(8)));',
  19850. ' $ir.ref(4, $ir.ref(3, v.GetItems(9)).GetBird()).SetBird(v);',
  19851. ' v = rtl.setIntfL(v, $ir.ref(6, $ir.ref(5, v.GetBird()).GetItems(10)).GetBird(), true);',
  19852. ' } finally {',
  19853. ' $ir.free();',
  19854. ' rtl._Release(v);',
  19855. ' };',
  19856. '};',
  19857. '']),
  19858. LinesToStr([ // $mod.$main
  19859. '']));
  19860. end;
  19861. procedure TTestModule.TestClassInterface_COM_Delegation;
  19862. begin
  19863. StartProgram(false);
  19864. Add([
  19865. '{$interfaces com}',
  19866. 'type',
  19867. ' IUnknown = interface',
  19868. ' function _AddRef: longint;',
  19869. ' function _Release: longint;',
  19870. ' end;',
  19871. ' IBird = interface(IUnknown)',
  19872. ' procedure Fly(s: string);',
  19873. ' end;',
  19874. ' IEagle = interface(IBird) end;',
  19875. ' IDove = interface(IBird) end;',
  19876. ' ISwallow = interface(IBird) end;',
  19877. ' TObject = class',
  19878. ' end;',
  19879. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  19880. ' function _AddRef: longint; virtual; abstract;',
  19881. ' function _Release: longint; virtual; abstract;',
  19882. ' procedure Fly(s: string); virtual; abstract;',
  19883. ' end;',
  19884. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  19885. ' function _AddRef: longint; virtual; abstract;',
  19886. ' function _Release: longint; virtual; abstract;',
  19887. ' FBirdIntf: IBird;',
  19888. ' property BirdIntf: IBird read FBirdIntf implements IBird;',
  19889. ' function GetEagleIntf: IEagle; virtual; abstract;',
  19890. ' property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  19891. ' FDoveObj: TBird;',
  19892. ' property DoveObj: TBird read FDoveObj implements IDove;',
  19893. ' function GetSwallowObj: TBird; virtual; abstract;',
  19894. ' property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  19895. ' end;',
  19896. 'begin',
  19897. '']);
  19898. ConvertProgram;
  19899. CheckSource('TestClassInterface_COM_Delegation',
  19900. LinesToStr([ // statements
  19901. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19902. 'rtl.createInterface(this, "IBird", "{CC440C7F-7623-3DEE-AE88-000B86AAF108}", ["Fly"], this.IUnknown);',
  19903. 'rtl.createInterface(this, "IEagle", "{4B6A41C9-B020-3D7C-B688-96D19022B1B4}", [], this.IBird);',
  19904. 'rtl.createInterface(this, "IDove", "{4B6A41C9-B020-3D7C-B688-96D18EF16074}", [], this.IBird);',
  19905. 'rtl.createInterface(this, "ISwallow", "{BB6A41C9-B020-3D7C-B688-96D1CBDCB359}", [], this.IBird);',
  19906. 'rtl.createClass(this, "TObject", null, function () {',
  19907. ' this.$init = function () {',
  19908. ' };',
  19909. ' this.$final = function () {',
  19910. ' };',
  19911. '});',
  19912. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  19913. ' rtl.addIntf(this, $mod.IBird);',
  19914. ' rtl.addIntf(this, $mod.IEagle);',
  19915. ' rtl.addIntf(this, $mod.IDove);',
  19916. ' rtl.addIntf(this, $mod.ISwallow);',
  19917. '});',
  19918. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  19919. ' this.$init = function () {',
  19920. ' $mod.TObject.$init.call(this);',
  19921. ' this.FBirdIntf = null;',
  19922. ' this.FDoveObj = null;',
  19923. ' };',
  19924. ' this.$final = function () {',
  19925. ' this.FBirdIntf = undefined;',
  19926. ' this.FDoveObj = undefined;',
  19927. ' $mod.TObject.$final.call(this);',
  19928. ' };',
  19929. ' this.$intfmaps = {',
  19930. ' "{CC440C7F-7623-3DEE-AE88-000B86AAF108}": function () {',
  19931. ' return rtl._AddRef(this.FBirdIntf);',
  19932. ' },',
  19933. ' "{4B6A41C9-B020-3D7C-B688-96D19022B1B4}": function () {',
  19934. ' return this.GetEagleIntf();',
  19935. ' },',
  19936. ' "{4B6A41C9-B020-3D7C-B688-96D18EF16074}": function () {',
  19937. ' return rtl.queryIntfT(this.FDoveObj, $mod.IDove);',
  19938. ' },',
  19939. ' "{BB6A41C9-B020-3D7C-B688-96D1CBDCB359}": function () {',
  19940. ' return rtl.queryIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  19941. ' }',
  19942. ' };',
  19943. '});',
  19944. '']),
  19945. LinesToStr([ // $mod.$main
  19946. '']));
  19947. end;
  19948. procedure TTestModule.TestClassInterface_COM_With;
  19949. begin
  19950. StartProgram(false);
  19951. Add([
  19952. '{$interfaces com}',
  19953. 'type',
  19954. ' IUnknown = interface',
  19955. ' function _AddRef: longint;',
  19956. ' function _Release: longint;',
  19957. ' function GetAnt: IUnknown;',
  19958. ' property Ant: IUnknown read GetAnt;',
  19959. ' end;',
  19960. ' TObject = class(IUnknown)',
  19961. ' function _AddRef: longint; virtual; abstract;',
  19962. ' function _Release: longint; virtual; abstract;',
  19963. ' function GetAnt: IUnknown; virtual; abstract;',
  19964. ' property Ant: IUnknown read GetAnt;',
  19965. ' end;',
  19966. 'procedure DoIt;',
  19967. 'var',
  19968. ' i: IUnknown;',
  19969. 'begin',
  19970. ' with i do ',
  19971. ' GetAnt;',
  19972. ' with i.Ant, Ant do ',
  19973. ' GetAnt;',
  19974. 'end;',
  19975. 'begin',
  19976. '']);
  19977. ConvertProgram;
  19978. CheckSource('TestClassInterface_COM_With',
  19979. LinesToStr([ // statements
  19980. 'rtl.createInterface(this, "IUnknown", "{D7ADB00D-C6B6-39FB-BDDF-21CD521DDFA9}", ["_AddRef", "_Release", "GetAnt"], null);',
  19981. 'rtl.createClass(this, "TObject", null, function () {',
  19982. ' this.$init = function () {',
  19983. ' };',
  19984. ' this.$final = function () {',
  19985. ' };',
  19986. ' rtl.addIntf(this, $mod.IUnknown);',
  19987. '});',
  19988. 'this.DoIt = function () {',
  19989. ' var i = null;',
  19990. ' var $ir = rtl.createIntfRefs();',
  19991. ' try {',
  19992. ' $ir.ref(1, i.GetAnt());',
  19993. ' var $with = $ir.ref(2, i.GetAnt());',
  19994. ' var $with1 = $ir.ref(3, $with.GetAnt());',
  19995. ' $ir.ref(4, $with1.GetAnt());',
  19996. ' } finally {',
  19997. ' $ir.free();',
  19998. ' };',
  19999. '};',
  20000. '']),
  20001. LinesToStr([ // $mod.$main
  20002. '']));
  20003. end;
  20004. procedure TTestModule.TestClassInterface_COM_ForIn;
  20005. begin
  20006. StartProgram(false);
  20007. Add([
  20008. '{$interfaces com}',
  20009. 'type',
  20010. ' IUnknown = interface end;',
  20011. ' TObject = class',
  20012. ' Id: longint;',
  20013. ' end;',
  20014. ' IEnumerator = interface(IUnknown)',
  20015. ' function GetCurrent: TObject;',
  20016. ' function MoveNext: Boolean;',
  20017. ' property Current: TObject read GetCurrent;',
  20018. ' end;',
  20019. ' IEnumerable = interface(IUnknown)',
  20020. ' function GetEnumerator: IEnumerator;',
  20021. ' end;',
  20022. 'var',
  20023. ' o: TObject;',
  20024. ' i: IEnumerable;',
  20025. 'begin',
  20026. ' for o in i do o.Id:=3;',
  20027. '']);
  20028. ConvertProgram;
  20029. CheckSource('TestClassInterface_COM_ForIn',
  20030. LinesToStr([ // statements
  20031. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  20032. 'rtl.createClass(this, "TObject", null, function () {',
  20033. ' this.$init = function () {',
  20034. ' this.Id = 0;',
  20035. ' };',
  20036. ' this.$final = function () {',
  20037. ' };',
  20038. '});',
  20039. 'rtl.createInterface(this, "IEnumerator", "{95D7745D-ED61-3F13-BBE4-07708161999E}", ["GetCurrent", "MoveNext"], this.IUnknown);',
  20040. 'rtl.createInterface(this, "IEnumerable", "{8CC9D45D-ED7D-3B73-96B6-290B931BB19E}", ["GetEnumerator"], this.IUnknown);',
  20041. 'this.o = null;',
  20042. 'this.i = null;',
  20043. '']),
  20044. LinesToStr([ // $mod.$main
  20045. 'var $in = $mod.i.GetEnumerator();',
  20046. 'try {',
  20047. ' while ($in.MoveNext()) {',
  20048. ' $mod.o = $in.GetCurrent();',
  20049. ' $mod.o.Id = 3;',
  20050. ' }',
  20051. '} finally {',
  20052. ' rtl._Release($in)',
  20053. '};',
  20054. '']));
  20055. end;
  20056. procedure TTestModule.TestClassInterface_COM_ArrayOfIntfFail;
  20057. begin
  20058. StartProgram(false);
  20059. Add([
  20060. '{$interfaces com}',
  20061. 'type',
  20062. ' IUnknown = interface',
  20063. ' function _AddRef: longint;',
  20064. ' function _Release: longint;',
  20065. ' end;',
  20066. ' TObject = class',
  20067. ' end;',
  20068. ' TArrOfIntf = array of IUnknown;',
  20069. 'begin',
  20070. '']);
  20071. SetExpectedPasResolverError('Not supported: array of COM-interface',nNotSupportedX);
  20072. ConvertProgram;
  20073. end;
  20074. procedure TTestModule.TestClassInterface_COM_RecordIntfFail;
  20075. begin
  20076. StartProgram(false);
  20077. Add([
  20078. '{$interfaces com}',
  20079. 'type',
  20080. ' IUnknown = interface',
  20081. ' function _AddRef: longint;',
  20082. ' function _Release: longint;',
  20083. ' end;',
  20084. ' TRec = record',
  20085. ' i: IUnknown;',
  20086. ' end;',
  20087. 'begin',
  20088. '']);
  20089. SetExpectedPasResolverError('Not supported: COM-interface as record member',nNotSupportedX);
  20090. ConvertProgram;
  20091. end;
  20092. procedure TTestModule.TestClassInterface_COM_UnitInitialization;
  20093. begin
  20094. StartUnit(false);
  20095. Add([
  20096. '{$interfaces com}',
  20097. 'interface',
  20098. 'implementation',
  20099. 'type',
  20100. ' IUnknown = interface',
  20101. ' function _AddRef: longint;',
  20102. ' end;',
  20103. ' TObject = class(IUnknown)',
  20104. ' function _AddRef: longint;',
  20105. ' end;',
  20106. 'function TObject._AddRef: longint; begin end;',
  20107. 'var i: IUnknown;',
  20108. ' o: TObject;',
  20109. 'initialization',
  20110. ' i:=nil;',
  20111. ' i:=i;',
  20112. ' i:=o;',
  20113. ' if (o as IUnknown)=nil then ;',
  20114. '']);
  20115. ConvertUnit;
  20116. CheckSource('TestClassInterface_COM_UnitInitialization',
  20117. LinesToStr([ // statements
  20118. 'var $impl = $mod.$impl;',
  20119. '']),
  20120. LinesToStr([ // this.$init
  20121. 'var $ir = rtl.createIntfRefs();',
  20122. 'try {',
  20123. ' rtl.setIntfP($impl, "i", null);',
  20124. ' rtl.setIntfP($impl, "i", $impl.i);',
  20125. ' rtl.setIntfP($impl, "i", rtl.queryIntfT($impl.o, $impl.IUnknown), true);',
  20126. ' if ($ir.ref(1, rtl.queryIntfT($impl.o, $impl.IUnknown)) === null) ;',
  20127. '} finally {',
  20128. ' $ir.free();',
  20129. '};',
  20130. '']),
  20131. LinesToStr([ // implementation
  20132. 'rtl.createInterface($impl, "IUnknown", "{B92D5841-758A-322B-BDDF-21CD52180000}", ["_AddRef"], null);',
  20133. 'rtl.createClass($impl, "TObject", null, function () {',
  20134. ' this.$init = function () {',
  20135. ' };',
  20136. ' this.$final = function () {',
  20137. ' };',
  20138. ' this._AddRef = function () {',
  20139. ' var Result = 0;',
  20140. ' return Result;',
  20141. ' };',
  20142. ' rtl.addIntf(this, $impl.IUnknown);',
  20143. '});',
  20144. '$impl.i = null;',
  20145. '$impl.o = null;',
  20146. ''])
  20147. );
  20148. end;
  20149. procedure TTestModule.TestClassInterface_GUID;
  20150. begin
  20151. StartProgram(false);
  20152. Add([
  20153. '{$interfaces corba}',
  20154. 'type',
  20155. ' IUnknown = interface',
  20156. ' [''{f31db68f-3010-D355-4EBA-CDD4EF4A737C}'']',
  20157. ' end;',
  20158. ' TObject = class end;',
  20159. ' TGUID = record D1, D2, D3, D4: word; end;',
  20160. ' TAliasGUID = TGUID;',
  20161. ' TGUIDString = type string;',
  20162. ' TAliasGUIDString = TGUIDString;',
  20163. 'procedure DoConstGUIDIt(const g: TAliasGUID); overload;',
  20164. 'begin end;',
  20165. 'procedure DoDefGUID(g: TAliasGUID); overload;',
  20166. 'begin end;',
  20167. 'procedure DoStr(const s: TAliasGUIDString); overload;',
  20168. 'begin end;',
  20169. 'var',
  20170. ' i: IUnknown;',
  20171. ' g: TAliasGUID = ''{d91c9af4-3C93-420F-A303-BF5BA82BFD23}'';',
  20172. ' s: TAliasGUIDString;',
  20173. 'begin',
  20174. ' DoConstGUIDIt(IUnknown);',
  20175. ' DoDefGUID(IUnknown);',
  20176. ' DoStr(IUnknown);',
  20177. ' DoConstGUIDIt(i);',
  20178. ' DoDefGUID(i);',
  20179. ' DoStr(i);',
  20180. ' DoConstGUIDIt(''{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}'');',
  20181. ' DoDefGUID(''{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}'');',
  20182. ' DoStr(g);',
  20183. ' g:=i;',
  20184. ' g:=IUnknown;',
  20185. ' g:=''{D91C9AF4-3C93-420F-A303-bf5ba82bfd23}'';',
  20186. ' s:=i;',
  20187. ' s:=IUnknown;',
  20188. ' s:=g;',
  20189. ' if g=i then ;',
  20190. ' if i=g then ;',
  20191. ' if g=IUnknown then ;',
  20192. ' if IUnknown=g then ;',
  20193. ' if s=i then ;',
  20194. ' if i=s then ;',
  20195. ' if s=IUnknown then ;',
  20196. ' if IUnknown=s then ;',
  20197. ' if s=g then ;',
  20198. ' if g=s then ;',
  20199. '']);
  20200. ConvertProgram;
  20201. CheckSource('TestClassInterface_GUID',
  20202. LinesToStr([ // statements
  20203. 'rtl.createInterface(this, "IUnknown", "{F31DB68F-3010-D355-4EBA-CDD4EF4A737C}", [], null);',
  20204. 'rtl.createClass(this, "TObject", null, function () {',
  20205. ' this.$init = function () {',
  20206. ' };',
  20207. ' this.$final = function () {',
  20208. ' };',
  20209. '});',
  20210. 'rtl.recNewT(this, "TGUID", function () {',
  20211. ' this.D1 = 0;',
  20212. ' this.D2 = 0;',
  20213. ' this.D3 = 0;',
  20214. ' this.D4 = 0;',
  20215. ' this.$eq = function (b) {',
  20216. ' return (this.D1 === b.D1) && (this.D2 === b.D2) && (this.D3 === b.D3) && (this.D4 === b.D4);',
  20217. ' };',
  20218. ' this.$assign = function (s) {',
  20219. ' this.D1 = s.D1;',
  20220. ' this.D2 = s.D2;',
  20221. ' this.D3 = s.D3;',
  20222. ' this.D4 = s.D4;',
  20223. ' return this;',
  20224. ' };',
  20225. '});',
  20226. 'this.DoConstGUIDIt = function (g) {',
  20227. '};',
  20228. 'this.DoDefGUID = function (g) {',
  20229. '};',
  20230. 'this.DoStr = function (s) {',
  20231. '};',
  20232. 'this.i = null;',
  20233. 'this.g = this.TGUID.$clone({',
  20234. ' D1: 0xD91C9AF4,',
  20235. ' D2: 0x3C93,',
  20236. ' D3: 0x420F,',
  20237. ' D4: [',
  20238. ' 0xA3,',
  20239. ' 0x03,',
  20240. ' 0xBF,',
  20241. ' 0x5B,',
  20242. ' 0xA8,',
  20243. ' 0x2B,',
  20244. ' 0xFD,',
  20245. ' 0x23',
  20246. ' ]',
  20247. '});',
  20248. 'this.s = "";',
  20249. '']),
  20250. LinesToStr([ // $mod.$main
  20251. '$mod.DoConstGUIDIt(rtl.getIntfGUIDR($mod.IUnknown));',
  20252. '$mod.DoDefGUID($mod.TGUID.$clone(rtl.getIntfGUIDR($mod.IUnknown)));',
  20253. '$mod.DoStr($mod.IUnknown.$guid);',
  20254. '$mod.DoConstGUIDIt(rtl.getIntfGUIDR($mod.i));',
  20255. '$mod.DoDefGUID($mod.TGUID.$clone(rtl.getIntfGUIDR($mod.i)));',
  20256. '$mod.DoStr($mod.i.$guid);',
  20257. '$mod.DoConstGUIDIt(rtl.strToGUIDR("{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}"));',
  20258. '$mod.DoDefGUID(rtl.strToGUIDR("{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}"));',
  20259. '$mod.DoStr(rtl.guidrToStr($mod.g));',
  20260. '$mod.g.$assign(rtl.getIntfGUIDR($mod.i));',
  20261. '$mod.g.$assign(rtl.getIntfGUIDR($mod.IUnknown));',
  20262. '$mod.g.$assign({',
  20263. ' D1: 0xD91C9AF4,',
  20264. ' D2: 0x3C93,',
  20265. ' D3: 0x420F,',
  20266. ' D4: [',
  20267. ' 0xA3,',
  20268. ' 0x03,',
  20269. ' 0xBF,',
  20270. ' 0x5B,',
  20271. ' 0xA8,',
  20272. ' 0x2B,',
  20273. ' 0xFD,',
  20274. ' 0x23',
  20275. ' ]',
  20276. '});',
  20277. '$mod.s = $mod.i.$guid;',
  20278. '$mod.s = $mod.IUnknown.$guid;',
  20279. '$mod.s = rtl.guidrToStr($mod.g);',
  20280. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.i))) ;',
  20281. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.i))) ;',
  20282. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.IUnknown))) ;',
  20283. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.IUnknown))) ;',
  20284. 'if ($mod.s === $mod.i.$guid) ;',
  20285. 'if ($mod.i.$guid === $mod.s) ;',
  20286. 'if ($mod.s === $mod.IUnknown.$guid) ;',
  20287. 'if ($mod.IUnknown.$guid === $mod.s) ;',
  20288. 'if ($mod.g.$eq(rtl.createTGUID($mod.s))) ;',
  20289. 'if ($mod.g.$eq(rtl.createTGUID($mod.s))) ;',
  20290. '']));
  20291. end;
  20292. procedure TTestModule.TestClassInterface_GUIDProperty;
  20293. begin
  20294. StartProgram(false);
  20295. Add([
  20296. '{$interfaces corba}',
  20297. 'type',
  20298. ' IUnknown = interface',
  20299. ' [''{f31db68f-3010-D355-4EBA-CDD4EF4A737C}'']',
  20300. ' end;',
  20301. ' TGUID = record D1, D2, D3, D4: word; end;',
  20302. ' TAliasGUID = TGUID;',
  20303. ' TGUIDString = type string;',
  20304. ' TAliasGUIDString = TGUIDString;',
  20305. ' TObject = class',
  20306. ' function GetG: TAliasGUID; virtual; abstract;',
  20307. ' procedure SetG(const Value: TAliasGUID); virtual; abstract;',
  20308. ' function GetS: TAliasGUIDString; virtual; abstract;',
  20309. ' procedure SetS(const Value: TAliasGUIDString); virtual; abstract;',
  20310. ' property g: TAliasGUID read GetG write SetG;',
  20311. ' property s: TAliasGUIDString read GetS write SetS;',
  20312. ' end;',
  20313. 'var o: TObject;',
  20314. 'begin',
  20315. ' o.g:=IUnknown;',
  20316. ' o.g:=''{D91C9AF4-3C93-420F-A303-bf5ba82bfd23}'';',
  20317. ' o.s:=IUnknown;',
  20318. ' o.s:=o.g;',
  20319. '']);
  20320. ConvertProgram;
  20321. CheckSource('TestClassInterface_GUIDProperty',
  20322. LinesToStr([ // statements
  20323. 'rtl.createInterface(this, "IUnknown", "{F31DB68F-3010-D355-4EBA-CDD4EF4A737C}", [], null);',
  20324. 'rtl.recNewT(this, "TGUID", function () {',
  20325. ' this.D1 = 0;',
  20326. ' this.D2 = 0;',
  20327. ' this.D3 = 0;',
  20328. ' this.D4 = 0;',
  20329. ' this.$eq = function (b) {',
  20330. ' return (this.D1 === b.D1) && (this.D2 === b.D2) && (this.D3 === b.D3) && (this.D4 === b.D4);',
  20331. ' };',
  20332. ' this.$assign = function (s) {',
  20333. ' this.D1 = s.D1;',
  20334. ' this.D2 = s.D2;',
  20335. ' this.D3 = s.D3;',
  20336. ' this.D4 = s.D4;',
  20337. ' return this;',
  20338. ' };',
  20339. '});',
  20340. 'rtl.createClass(this, "TObject", null, function () {',
  20341. ' this.$init = function () {',
  20342. ' };',
  20343. ' this.$final = function () {',
  20344. ' };',
  20345. '});',
  20346. 'this.o = null;',
  20347. '']),
  20348. LinesToStr([ // $mod.$main
  20349. '$mod.o.SetG(rtl.getIntfGUIDR($mod.IUnknown));',
  20350. '$mod.o.SetG({',
  20351. ' D1: 0xD91C9AF4,',
  20352. ' D2: 0x3C93,',
  20353. ' D3: 0x420F,',
  20354. ' D4: [',
  20355. ' 0xA3,',
  20356. ' 0x03,',
  20357. ' 0xBF,',
  20358. ' 0x5B,',
  20359. ' 0xA8,',
  20360. ' 0x2B,',
  20361. ' 0xFD,',
  20362. ' 0x23',
  20363. ' ]',
  20364. '});',
  20365. '$mod.o.SetS($mod.IUnknown.$guid);',
  20366. '$mod.o.SetS(rtl.guidrToStr($mod.o.GetG()));',
  20367. '']));
  20368. end;
  20369. procedure TTestModule.TestClassHelper_ClassVar;
  20370. begin
  20371. StartProgram(false);
  20372. Add([
  20373. 'type',
  20374. ' TObject = class',
  20375. ' end;',
  20376. ' THelper = class helper for TObject',
  20377. ' const',
  20378. ' One = 1;',
  20379. ' Two: word = 2;',
  20380. ' class var',
  20381. ' Glob: word;',
  20382. ' function Foo(w: word): word;',
  20383. ' class function Bar(w: word): word;',
  20384. ' end;',
  20385. 'function THelper.foo(w: word): word;',
  20386. 'begin',
  20387. ' Result:=w;',
  20388. ' Two:=One+w;',
  20389. ' Glob:=Glob;',
  20390. ' Result:=Self.Glob;',
  20391. ' Self.Glob:=Self.Glob;',
  20392. ' with Self do Glob:=Glob;',
  20393. 'end;',
  20394. 'class function THelper.bar(w: word): word;',
  20395. 'begin',
  20396. ' Result:=w;',
  20397. ' Two:=One;',
  20398. ' Glob:=Glob;',
  20399. ' Self.Glob:=Self.Glob;',
  20400. ' with Self do Glob:=Glob;',
  20401. 'end;',
  20402. 'var o: TObject;',
  20403. 'begin',
  20404. ' tobject.two:=tobject.one;',
  20405. ' tobject.Glob:=tobject.Glob;',
  20406. ' with tobject do begin',
  20407. ' two:=one;',
  20408. ' Glob:=Glob;',
  20409. ' end;',
  20410. ' o.two:=o.one;',
  20411. ' o.Glob:=o.Glob;',
  20412. ' with o do begin',
  20413. ' two:=one;',
  20414. ' Glob:=Glob;',
  20415. ' end;',
  20416. '']);
  20417. ConvertProgram;
  20418. CheckSource('TestClassHelper_ClassVar',
  20419. LinesToStr([ // statements
  20420. 'rtl.createClass(this, "TObject", null, function () {',
  20421. ' this.$init = function () {',
  20422. ' };',
  20423. ' this.$final = function () {',
  20424. ' };',
  20425. '});',
  20426. 'rtl.createHelper(this, "THelper", null, function () {',
  20427. ' this.One = 1;',
  20428. ' this.Two = 2;',
  20429. ' this.Glob = 0;',
  20430. ' this.Foo = function (w) {',
  20431. ' var Result = 0;',
  20432. ' Result = w;',
  20433. ' $mod.THelper.Two = 1 + w;',
  20434. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20435. ' Result = $mod.THelper.Glob;',
  20436. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20437. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20438. ' return Result;',
  20439. ' };',
  20440. ' this.Bar = function (w) {',
  20441. ' var Result = 0;',
  20442. ' Result = w;',
  20443. ' $mod.THelper.Two = 1;',
  20444. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20445. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20446. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20447. ' return Result;',
  20448. ' };',
  20449. '});',
  20450. 'this.o = null;',
  20451. '']),
  20452. LinesToStr([ // $mod.$main
  20453. '$mod.THelper.Two = 1;',
  20454. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20455. 'var $with = $mod.TObject;',
  20456. '$mod.THelper.Two = 1;',
  20457. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20458. '$mod.THelper.Two = 1;',
  20459. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20460. 'var $with1 = $mod.o;',
  20461. '$mod.THelper.Two = 1;',
  20462. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20463. '']));
  20464. end;
  20465. procedure TTestModule.TestClassHelper_Method_AccessInstanceFields;
  20466. begin
  20467. StartProgram(false);
  20468. Add([
  20469. 'type',
  20470. ' TObject = class',
  20471. ' FSize: word;',
  20472. ' property Size: word read FSize write FSize;',
  20473. ' end;',
  20474. ' THelper = class helper for TObject',
  20475. ' function Foo(w: word = 1): word;',
  20476. ' end;',
  20477. 'function THelper.foo(w: word): word;',
  20478. 'begin',
  20479. ' Result:=Size;',
  20480. ' Size:=Size+2;',
  20481. ' Self.Size:=Self.Size+3;',
  20482. ' FSize:=FSize+4;',
  20483. ' Self.FSize:=Self.FSize+5;',
  20484. ' with Self do begin',
  20485. ' Size:=Size+6;',
  20486. ' FSize:=FSize+7;',
  20487. ' FSize:=FSize+8;',
  20488. ' end;',
  20489. 'end;',
  20490. 'begin',
  20491. '']);
  20492. ConvertProgram;
  20493. CheckSource('TestClassHelper_Method_AccessInstanceFields',
  20494. LinesToStr([ // statements
  20495. 'rtl.createClass(this, "TObject", null, function () {',
  20496. ' this.$init = function () {',
  20497. ' this.FSize = 0;',
  20498. ' };',
  20499. ' this.$final = function () {',
  20500. ' };',
  20501. '});',
  20502. 'rtl.createHelper(this, "THelper", null, function () {',
  20503. ' this.Foo = function (w) {',
  20504. ' var Result = 0;',
  20505. ' Result = this.FSize;',
  20506. ' this.FSize = this.FSize + 2;',
  20507. ' this.FSize = this.FSize + 3;',
  20508. ' this.FSize = this.FSize + 4;',
  20509. ' this.FSize = this.FSize + 5;',
  20510. ' this.FSize = this.FSize + 6;',
  20511. ' this.FSize = this.FSize + 7;',
  20512. ' this.FSize = this.FSize + 8;',
  20513. ' return Result;',
  20514. ' };',
  20515. '});',
  20516. '']),
  20517. LinesToStr([ // $mod.$main
  20518. '']));
  20519. end;
  20520. procedure TTestModule.TestClassHelper_Method_Call;
  20521. begin
  20522. StartProgram(false);
  20523. Add([
  20524. 'type',
  20525. ' TObject = class',
  20526. ' procedure Run(w: word = 10);',
  20527. ' end;',
  20528. ' THelper = class helper for TObject',
  20529. ' function Foo(w: word = 1): word;',
  20530. ' end;',
  20531. 'procedure TObject.Run(w: word);',
  20532. 'var o: TObject;',
  20533. 'begin',
  20534. ' Foo;',
  20535. ' Foo();',
  20536. ' Foo(2);',
  20537. ' Self.Foo;',
  20538. ' Self.Foo();',
  20539. ' Self.Foo(3);',
  20540. ' with Self do begin',
  20541. ' Foo;',
  20542. ' Foo();',
  20543. ' Foo(4);',
  20544. ' end;',
  20545. ' with o do Foo(5);',
  20546. 'end;',
  20547. 'function THelper.foo(w: word): word;',
  20548. 'begin',
  20549. ' Run;',
  20550. ' Run();',
  20551. ' Run(11);',
  20552. ' Foo;',
  20553. ' Foo();',
  20554. ' Foo(12);',
  20555. ' Self.Foo;',
  20556. ' Self.Foo();',
  20557. ' Self.Foo(13);',
  20558. ' with Self do begin',
  20559. ' Foo;',
  20560. ' Foo();',
  20561. ' Foo(14);',
  20562. ' end;',
  20563. 'end;',
  20564. 'var Obj: TObject;',
  20565. 'begin',
  20566. ' obj.Foo;',
  20567. ' obj.Foo();',
  20568. ' obj.Foo(21);',
  20569. ' with obj do begin',
  20570. ' Foo;',
  20571. ' Foo();',
  20572. ' Foo(22);',
  20573. ' end;',
  20574. '']);
  20575. ConvertProgram;
  20576. CheckSource('TestClassHelper_Method_Call',
  20577. LinesToStr([ // statements
  20578. 'rtl.createClass(this, "TObject", null, function () {',
  20579. ' this.$init = function () {',
  20580. ' };',
  20581. ' this.$final = function () {',
  20582. ' };',
  20583. ' this.Run = function (w) {',
  20584. ' var o = null;',
  20585. ' $mod.THelper.Foo.call(this, 1);',
  20586. ' $mod.THelper.Foo.call(this, 1);',
  20587. ' $mod.THelper.Foo.call(this, 2);',
  20588. ' $mod.THelper.Foo.call(this, 1);',
  20589. ' $mod.THelper.Foo.call(this, 1);',
  20590. ' $mod.THelper.Foo.call(this, 3);',
  20591. ' $mod.THelper.Foo.call(this, 1);',
  20592. ' $mod.THelper.Foo.call(this, 1);',
  20593. ' $mod.THelper.Foo.call(this, 4);',
  20594. ' $mod.THelper.Foo.call(o, 5);',
  20595. ' };',
  20596. '});',
  20597. 'rtl.createHelper(this, "THelper", null, function () {',
  20598. ' this.Foo = function (w) {',
  20599. ' var Result = 0;',
  20600. ' this.Run(10);',
  20601. ' this.Run(10);',
  20602. ' this.Run(11);',
  20603. ' $mod.THelper.Foo.call(this, 1);',
  20604. ' $mod.THelper.Foo.call(this, 1);',
  20605. ' $mod.THelper.Foo.call(this, 12);',
  20606. ' $mod.THelper.Foo.call(this, 1);',
  20607. ' $mod.THelper.Foo.call(this, 1);',
  20608. ' $mod.THelper.Foo.call(this, 13);',
  20609. ' $mod.THelper.Foo.call(this, 1);',
  20610. ' $mod.THelper.Foo.call(this, 1);',
  20611. ' $mod.THelper.Foo.call(this, 14);',
  20612. ' return Result;',
  20613. ' };',
  20614. '});',
  20615. 'this.Obj = null;',
  20616. '']),
  20617. LinesToStr([ // $mod.$main
  20618. '$mod.THelper.Foo.call($mod.Obj, 1);',
  20619. '$mod.THelper.Foo.call($mod.Obj, 1);',
  20620. '$mod.THelper.Foo.call($mod.Obj, 21);',
  20621. 'var $with = $mod.Obj;',
  20622. '$mod.THelper.Foo.call($with, 1);',
  20623. '$mod.THelper.Foo.call($with, 1);',
  20624. '$mod.THelper.Foo.call($with, 22);',
  20625. '']));
  20626. end;
  20627. procedure TTestModule.TestClassHelper_Method_Nested_Call;
  20628. begin
  20629. StartProgram(false);
  20630. Add([
  20631. 'type',
  20632. ' TObject = class',
  20633. ' procedure Run(w: word = 10);',
  20634. ' end;',
  20635. ' THelper = class helper for TObject',
  20636. ' function Foo(w: word = 1): word;',
  20637. ' end;',
  20638. 'procedure TObject.Run(w: word);',
  20639. ' procedure Sub(Self: TObject);',
  20640. ' begin',
  20641. ' Foo;',
  20642. ' Foo();',
  20643. ' Self.Foo;',
  20644. ' Self.Foo();',
  20645. ' with Self do begin',
  20646. ' Foo;',
  20647. ' Foo();',
  20648. ' end;',
  20649. ' end;',
  20650. 'begin',
  20651. 'end;',
  20652. 'function THelper.foo(w: word): word;',
  20653. ' procedure Sub(Self: TObject);',
  20654. ' begin',
  20655. ' Run;',
  20656. ' Run();',
  20657. ' Foo;',
  20658. ' Foo();',
  20659. ' Self.Foo;',
  20660. ' Self.Foo();',
  20661. ' with Self do begin',
  20662. ' Foo;',
  20663. ' Foo();',
  20664. ' end;',
  20665. ' end;',
  20666. 'begin',
  20667. 'end;',
  20668. 'begin',
  20669. '']);
  20670. ConvertProgram;
  20671. CheckSource('TestClassHelper_Method_Nested_Call',
  20672. LinesToStr([ // statements
  20673. 'rtl.createClass(this, "TObject", null, function () {',
  20674. ' this.$init = function () {',
  20675. ' };',
  20676. ' this.$final = function () {',
  20677. ' };',
  20678. ' this.Run = function (w) {',
  20679. ' var $Self = this;',
  20680. ' function Sub(Self) {',
  20681. ' $mod.THelper.Foo.call($Self, 1);',
  20682. ' $mod.THelper.Foo.call($Self, 1);',
  20683. ' $mod.THelper.Foo.call(Self, 1);',
  20684. ' $mod.THelper.Foo.call(Self, 1);',
  20685. ' $mod.THelper.Foo.call(Self, 1);',
  20686. ' $mod.THelper.Foo.call(Self, 1);',
  20687. ' };',
  20688. ' };',
  20689. '});',
  20690. 'rtl.createHelper(this, "THelper", null, function () {',
  20691. ' this.Foo = function (w) {',
  20692. ' var $Self = this;',
  20693. ' var Result = 0;',
  20694. ' function Sub(Self) {',
  20695. ' $Self.Run(10);',
  20696. ' $Self.Run(10);',
  20697. ' $mod.THelper.Foo.call($Self, 1);',
  20698. ' $mod.THelper.Foo.call($Self, 1);',
  20699. ' $mod.THelper.Foo.call(Self, 1);',
  20700. ' $mod.THelper.Foo.call(Self, 1);',
  20701. ' $mod.THelper.Foo.call(Self, 1);',
  20702. ' $mod.THelper.Foo.call(Self, 1);',
  20703. ' };',
  20704. ' return Result;',
  20705. ' };',
  20706. '});',
  20707. '']),
  20708. LinesToStr([ // $mod.$main
  20709. '']));
  20710. end;
  20711. procedure TTestModule.TestClassHelper_ClassMethod_Call;
  20712. begin
  20713. StartProgram(false);
  20714. Add([
  20715. 'type',
  20716. ' TObject = class',
  20717. ' class procedure Run(w: word = 10);',
  20718. ' end;',
  20719. ' THelper = class helper for TObject',
  20720. ' class function Foo(w: word = 1): word;',
  20721. ' end;',
  20722. 'class procedure TObject.Run(w: word);',
  20723. 'begin',
  20724. ' Foo;',
  20725. ' Foo();',
  20726. ' Self.Foo;',
  20727. ' Self.Foo();',
  20728. ' with Self do begin',
  20729. ' Foo;',
  20730. ' Foo();',
  20731. ' end;',
  20732. 'end;',
  20733. 'class function THelper.foo(w: word): word;',
  20734. 'begin',
  20735. ' Run;',
  20736. ' Run();',
  20737. ' Foo;',
  20738. ' Foo();',
  20739. ' Self.Foo;',
  20740. ' Self.Foo();',
  20741. ' with Self do begin',
  20742. ' Foo;',
  20743. ' Foo();',
  20744. ' end;',
  20745. 'end;',
  20746. 'var',
  20747. ' Obj: TObject;',
  20748. 'begin',
  20749. ' obj.Foo;',
  20750. ' obj.Foo();',
  20751. ' with obj do begin',
  20752. ' Foo;',
  20753. ' Foo();',
  20754. ' end;',
  20755. ' tobject.Foo;',
  20756. ' tobject.Foo();',
  20757. ' with tobject do begin',
  20758. ' Foo;',
  20759. ' Foo();',
  20760. ' end;',
  20761. '']);
  20762. ConvertProgram;
  20763. CheckSource('TestClassHelper_ClassMethod_Call',
  20764. LinesToStr([ // statements
  20765. 'rtl.createClass(this, "TObject", null, function () {',
  20766. ' this.$init = function () {',
  20767. ' };',
  20768. ' this.$final = function () {',
  20769. ' };',
  20770. ' this.Run = function (w) {',
  20771. ' $mod.THelper.Foo.call(this, 1);',
  20772. ' $mod.THelper.Foo.call(this, 1);',
  20773. ' $mod.THelper.Foo.call(this, 1);',
  20774. ' $mod.THelper.Foo.call(this, 1);',
  20775. ' $mod.THelper.Foo.call(this, 1);',
  20776. ' $mod.THelper.Foo.call(this, 1);',
  20777. ' };',
  20778. '});',
  20779. 'rtl.createHelper(this, "THelper", null, function () {',
  20780. ' this.Foo = function (w) {',
  20781. ' var Result = 0;',
  20782. ' this.Run(10);',
  20783. ' this.Run(10);',
  20784. ' $mod.THelper.Foo.call(this, 1);',
  20785. ' $mod.THelper.Foo.call(this, 1);',
  20786. ' $mod.THelper.Foo.call(this, 1);',
  20787. ' $mod.THelper.Foo.call(this, 1);',
  20788. ' $mod.THelper.Foo.call(this, 1);',
  20789. ' $mod.THelper.Foo.call(this, 1);',
  20790. ' return Result;',
  20791. ' };',
  20792. '});',
  20793. 'this.Obj = null;',
  20794. '']),
  20795. LinesToStr([ // $mod.$main
  20796. '$mod.THelper.Foo.call($mod.Obj.$class, 1);',
  20797. '$mod.THelper.Foo.call($mod.Obj.$class, 1);',
  20798. 'var $with = $mod.Obj;',
  20799. '$mod.THelper.Foo.call($with.$class, 1);',
  20800. '$mod.THelper.Foo.call($with.$class, 1);',
  20801. '$mod.THelper.Foo.call($mod.TObject, 1);',
  20802. '$mod.THelper.Foo.call($mod.TObject, 1);',
  20803. 'var $with1 = $mod.TObject;',
  20804. '$mod.THelper.Foo.call($mod.TObject, 1);',
  20805. '$mod.THelper.Foo.call($mod.TObject, 1);',
  20806. '']));
  20807. end;
  20808. procedure TTestModule.TestClassHelper_ClassOf;
  20809. begin
  20810. StartProgram(false);
  20811. Add([
  20812. 'type',
  20813. ' TObject = class',
  20814. ' end;',
  20815. ' TClass = class of TObject;',
  20816. ' THelper = class helper for TObject',
  20817. ' class function Foo(w: word = 1): word;',
  20818. ' end;',
  20819. 'class function THelper.foo(w: word): word;',
  20820. 'begin',
  20821. 'end;',
  20822. 'var',
  20823. ' c: TClass;',
  20824. 'begin',
  20825. ' c.Foo;',
  20826. ' c.Foo();',
  20827. ' with c do begin',
  20828. ' Foo;',
  20829. ' Foo();',
  20830. ' end;',
  20831. '']);
  20832. ConvertProgram;
  20833. CheckSource('TestClassHelper_ClassOf',
  20834. LinesToStr([ // statements
  20835. 'rtl.createClass(this, "TObject", null, function () {',
  20836. ' this.$init = function () {',
  20837. ' };',
  20838. ' this.$final = function () {',
  20839. ' };',
  20840. '});',
  20841. 'rtl.createHelper(this, "THelper", null, function () {',
  20842. ' this.Foo = function (w) {',
  20843. ' var Result = 0;',
  20844. ' return Result;',
  20845. ' };',
  20846. '});',
  20847. 'this.c = null;',
  20848. '']),
  20849. LinesToStr([ // $mod.$main
  20850. '$mod.THelper.Foo.call($mod.c, 1);',
  20851. '$mod.THelper.Foo.call($mod.c, 1);',
  20852. 'var $with = $mod.c;',
  20853. '$mod.THelper.Foo.call($with, 1);',
  20854. '$mod.THelper.Foo.call($with, 1);',
  20855. '']));
  20856. end;
  20857. procedure TTestModule.TestClassHelper_MethodRefObjFPC;
  20858. begin
  20859. StartProgram(false);
  20860. Add([
  20861. '{$mode objfpc}',
  20862. 'type',
  20863. ' TObject = class',
  20864. ' procedure DoIt;',
  20865. ' end;',
  20866. ' THelper = class helper for TObject',
  20867. ' procedure Fly(w: word = 1);',
  20868. ' class procedure Glide(w: word = 1);',
  20869. ' class procedure Run(w: word = 1); static;',
  20870. ' end;',
  20871. ' TFly = procedure(w: word) of object;',
  20872. ' TGlide = TFly;',
  20873. ' TRun = procedure(w: word);',
  20874. 'var',
  20875. ' f: TFly;',
  20876. ' g: TGlide;',
  20877. ' r: TRun;',
  20878. 'procedure TObject.DoIt;',
  20879. 'begin',
  20880. ' f:=@fly;',
  20881. ' g:=@glide;',
  20882. ' r:=@run;',
  20883. ' f:[email protected];',
  20884. ' g:[email protected];',
  20885. ' r:[email protected];',
  20886. ' with self do begin',
  20887. ' f:=@fly;',
  20888. ' g:=@glide;',
  20889. ' r:=@run;',
  20890. ' end;',
  20891. 'end;',
  20892. 'procedure THelper.fly(w: word);',
  20893. 'begin',
  20894. ' f:=@fly;',
  20895. ' g:=@glide;',
  20896. ' r:=@run;',
  20897. 'end;',
  20898. 'class procedure THelper.glide(w: word);',
  20899. 'begin',
  20900. ' g:=@glide;',
  20901. ' r:=@run;',
  20902. 'end;',
  20903. 'class procedure THelper.run(w: word);',
  20904. 'begin',
  20905. ' g:=@glide;',
  20906. ' r:=@run;',
  20907. 'end;',
  20908. 'var',
  20909. ' Obj: TObject;',
  20910. 'begin',
  20911. ' f:[email protected];',
  20912. ' g:[email protected];',
  20913. ' r:[email protected];',
  20914. ' with obj do begin',
  20915. ' f:=@fly;',
  20916. ' g:=@glide;',
  20917. ' r:=@run;',
  20918. ' end;',
  20919. ' g:[email protected];',
  20920. ' r:[email protected];',
  20921. ' with tobject do begin',
  20922. ' g:=@glide;',
  20923. ' r:=@run;',
  20924. ' end;',
  20925. '']);
  20926. ConvertProgram;
  20927. CheckSource('TestClassHelper_MethodRefObjFPC',
  20928. LinesToStr([ // statements
  20929. 'rtl.createClass(this, "TObject", null, function () {',
  20930. ' this.$init = function () {',
  20931. ' };',
  20932. ' this.$final = function () {',
  20933. ' };',
  20934. ' this.DoIt = function () {',
  20935. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  20936. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  20937. ' $mod.r = $mod.THelper.Run;',
  20938. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  20939. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  20940. ' $mod.r = $mod.THelper.Run;',
  20941. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  20942. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  20943. ' $mod.r = $mod.THelper.Run;',
  20944. ' };',
  20945. '});',
  20946. 'rtl.createHelper(this, "THelper", null, function () {',
  20947. ' this.Fly = function (w) {',
  20948. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  20949. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  20950. ' $mod.r = $mod.THelper.Run;',
  20951. ' };',
  20952. ' this.Glide = function (w) {',
  20953. ' $mod.g = rtl.createCallback(this, $mod.THelper.Glide);',
  20954. ' $mod.r = $mod.THelper.Run;',
  20955. ' };',
  20956. ' this.Run = function (w) {',
  20957. ' $mod.g = rtl.createCallback($mod.THelper, $mod.THelper.Glide);',
  20958. ' $mod.r = $mod.THelper.Run;',
  20959. ' };',
  20960. '});',
  20961. 'this.f = null;',
  20962. 'this.g = null;',
  20963. 'this.r = null;',
  20964. 'this.Obj = null;',
  20965. '']),
  20966. LinesToStr([ // $mod.$main
  20967. '$mod.f = rtl.createCallback($mod.Obj, $mod.THelper.Fly);',
  20968. '$mod.g = rtl.createCallback($mod.Obj.$class, $mod.THelper.Glide);',
  20969. '$mod.r = $mod.THelper.Run;',
  20970. 'var $with = $mod.Obj;',
  20971. '$mod.f = rtl.createCallback($with, $mod.THelper.Fly);',
  20972. '$mod.g = rtl.createCallback($with.$class, $mod.THelper.Glide);',
  20973. '$mod.r = $mod.THelper.Run;',
  20974. '$mod.g = rtl.createCallback($mod.TObject, $mod.THelper.Glide);',
  20975. '$mod.r = $mod.THelper.Run;',
  20976. 'var $with1 = $mod.TObject;',
  20977. '$mod.g = rtl.createCallback($with1, $mod.THelper.Glide);',
  20978. '$mod.r = $mod.THelper.Run;',
  20979. '']));
  20980. end;
  20981. procedure TTestModule.TestClassHelper_Constructor;
  20982. begin
  20983. StartProgram(false);
  20984. Add([
  20985. 'type',
  20986. ' TObject = class',
  20987. ' constructor Create;',
  20988. ' end;',
  20989. ' TClass = class of TObject;',
  20990. ' THelper = class helper for TObject',
  20991. ' constructor NewHlp(w: word);',
  20992. ' end;',
  20993. 'var',
  20994. ' obj: TObject;',
  20995. ' c: TClass;',
  20996. 'constructor TObject.Create;',
  20997. 'begin',
  20998. ' NewHlp(2);', // normal call
  20999. ' tobject.NewHlp(3);', // new instance
  21000. ' c.newhlp(4);', // new instance
  21001. 'end;',
  21002. 'constructor THelper.NewHlp(w: word);',
  21003. 'begin',
  21004. ' create;', // normal call
  21005. ' tobject.create;', // new instance
  21006. ' NewHlp(2);', // normal call
  21007. ' tobject.NewHlp(3);', // new instance
  21008. ' c.newhlp(4);', // new instance
  21009. 'end;',
  21010. 'begin',
  21011. ' obj.newhlp(2);', // normal call
  21012. ' with Obj do newhlp(12);', // normal call
  21013. ' tobject.newhlp(3);', // new instance
  21014. ' with tobject do newhlp(13);', // new instance
  21015. ' c.newhlp(4);', // new instance
  21016. ' with c do newhlp(14);', // new instance
  21017. '']);
  21018. ConvertProgram;
  21019. CheckSource('TestClassHelper_Constructor',
  21020. LinesToStr([ // statements
  21021. 'rtl.createClass(this, "TObject", null, function () {',
  21022. ' this.$init = function () {',
  21023. ' };',
  21024. ' this.$final = function () {',
  21025. ' };',
  21026. ' this.Create = function () {',
  21027. ' $mod.THelper.NewHlp.call(this, 2);',
  21028. ' $mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  21029. ' $mod.c.$create($mod.THelper.NewHlp, [4]);',
  21030. ' return this;',
  21031. ' };',
  21032. '});',
  21033. 'rtl.createHelper(this, "THelper", null, function () {',
  21034. ' this.NewHlp = function (w) {',
  21035. ' this.Create();',
  21036. ' $mod.TObject.$create("Create");',
  21037. ' $mod.THelper.NewHlp.call(this, 2);',
  21038. ' $mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  21039. ' $mod.c.$create($mod.THelper.NewHlp, [4]);',
  21040. ' return this;',
  21041. ' };',
  21042. '});',
  21043. 'this.obj = null;',
  21044. 'this.c = null;',
  21045. '']),
  21046. LinesToStr([ // $mod.$main
  21047. '$mod.THelper.NewHlp.call($mod.obj, 2);',
  21048. 'var $with = $mod.obj;',
  21049. '$mod.THelper.NewHlp.call($with, 12);',
  21050. '$mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  21051. 'var $with1 = $mod.TObject;',
  21052. '$with1.$create($mod.THelper.NewHlp, [13]);',
  21053. '$mod.c.$create($mod.THelper.NewHlp, [4]);',
  21054. 'var $with2 = $mod.c;',
  21055. '$with2.$create($mod.THelper.NewHlp, [14]);',
  21056. '']));
  21057. end;
  21058. procedure TTestModule.TestClassHelper_InheritedObjFPC;
  21059. begin
  21060. StartProgram(false);
  21061. Add([
  21062. 'type',
  21063. ' TObject = class',
  21064. ' procedure Fly;',
  21065. ' end;',
  21066. ' TObjHelper = class helper for TObject',
  21067. ' procedure Fly;',
  21068. ' end;',
  21069. ' TBird = class',
  21070. ' procedure Fly;',
  21071. ' end;',
  21072. ' TBirdHelper = class helper for TBird',
  21073. ' procedure Fly;',
  21074. ' procedure Walk(w: word);',
  21075. ' end;',
  21076. ' TEagleHelper = class helper(TBirdHelper) for TBird',
  21077. ' procedure Fly;',
  21078. ' procedure Walk(w: word);',
  21079. ' end;',
  21080. 'procedure Tobject.fly;',
  21081. 'begin',
  21082. ' inherited;', // ignore
  21083. 'end;',
  21084. 'procedure Tobjhelper.fly;',
  21085. 'begin',
  21086. ' {@TObject_Fly}inherited;',
  21087. ' inherited {@TObject_Fly}Fly;',
  21088. 'end;',
  21089. 'procedure Tbird.fly;',
  21090. 'begin',
  21091. ' {@TObjHelper_Fly}inherited;',
  21092. ' inherited {@TObjHelper_Fly}Fly;',
  21093. 'end;',
  21094. 'procedure Tbirdhelper.fly;',
  21095. 'begin',
  21096. ' {@TBird_Fly}inherited;',
  21097. ' inherited {@TBird_Fly}Fly;',
  21098. 'end;',
  21099. 'procedure Tbirdhelper.walk(w: word);',
  21100. 'begin',
  21101. 'end;',
  21102. 'procedure teagleHelper.fly;',
  21103. 'begin',
  21104. ' {@TBird_Fly}inherited;',
  21105. ' inherited {@TBird_Fly}Fly;',
  21106. 'end;',
  21107. 'procedure teagleHelper.walk(w: word);',
  21108. 'begin',
  21109. ' {@TBirdHelper_Walk}inherited;',
  21110. ' inherited {@TBirdHelper_Walk}Walk(3);',
  21111. 'end;',
  21112. 'begin',
  21113. '']);
  21114. ConvertProgram;
  21115. CheckSource('TestClassHelper_InheritedObjFPC',
  21116. LinesToStr([ // statements
  21117. 'rtl.createClass(this, "TObject", null, function () {',
  21118. ' this.$init = function () {',
  21119. ' };',
  21120. ' this.$final = function () {',
  21121. ' };',
  21122. ' this.Fly = function () {',
  21123. ' };',
  21124. '});',
  21125. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21126. ' this.Fly = function () {',
  21127. ' $mod.TObject.Fly.call(this);',
  21128. ' $mod.TObject.Fly.call(this);',
  21129. ' };',
  21130. '});',
  21131. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21132. ' this.Fly$1 = function () {',
  21133. ' $mod.TObjHelper.Fly.call(this);',
  21134. ' $mod.TObjHelper.Fly.call(this);',
  21135. ' };',
  21136. '});',
  21137. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  21138. ' this.Fly = function () {',
  21139. ' $mod.TBird.Fly$1.call(this);',
  21140. ' $mod.TBird.Fly$1.call(this);',
  21141. ' };',
  21142. ' this.Walk = function (w) {',
  21143. ' };',
  21144. '});',
  21145. 'rtl.createHelper(this, "TEagleHelper", this.TBirdHelper, function () {',
  21146. ' this.Fly$1 = function () {',
  21147. ' $mod.TBird.Fly$1.call(this);',
  21148. ' $mod.TBird.Fly$1.call(this);',
  21149. ' };',
  21150. ' this.Walk$1 = function (w) {',
  21151. ' $mod.TBirdHelper.Walk.apply(this, arguments);',
  21152. ' $mod.TBirdHelper.Walk.call(this, 3);',
  21153. ' };',
  21154. '});',
  21155. '']),
  21156. LinesToStr([ // $mod.$main
  21157. '']));
  21158. end;
  21159. procedure TTestModule.TestClassHelper_Property;
  21160. begin
  21161. StartProgram(false);
  21162. Add([
  21163. 'type',
  21164. ' TObject = class',
  21165. ' FSize: word;',
  21166. ' function GetSpeed: word;',
  21167. ' procedure SetSpeed(Value: word);',
  21168. ' end;',
  21169. ' TObjHelper = class helper for TObject',
  21170. ' function GetLeft: word;',
  21171. ' procedure SetLeft(Value: word);',
  21172. ' property Size: word read FSize write FSize;',
  21173. ' property Speed: word read GetSpeed write SetSpeed;',
  21174. ' property Left: word read GetLeft write SetLeft;',
  21175. ' end;',
  21176. ' TBird = class',
  21177. ' property NotRight: word read GetLeft write SetLeft;',
  21178. ' procedure DoIt;',
  21179. ' end;',
  21180. 'var',
  21181. ' b: TBird;',
  21182. 'function Tobject.GetSpeed: word;',
  21183. 'begin',
  21184. ' Size:=Size+11;',
  21185. ' Speed:=Speed+12;',
  21186. ' Result:=Left+13;',
  21187. ' Left:=13;',
  21188. ' Left:=Left+13;',
  21189. ' Self.Size:=Self.Size+21;',
  21190. ' Self.Speed:=Self.Speed+22;',
  21191. ' Self.Left:=Self.Left+23;',
  21192. ' with Self do begin',
  21193. ' Size:=Size+31;',
  21194. ' Speed:=Speed+32;',
  21195. ' Left:=Left+33;',
  21196. ' end;',
  21197. 'end;',
  21198. 'procedure Tobject.SetSpeed(Value: word);',
  21199. 'begin',
  21200. 'end;',
  21201. 'function TObjHelper.GetLeft: word;',
  21202. 'begin',
  21203. ' Size:=Size+11;',
  21204. ' Speed:=Speed+12;',
  21205. ' Left:=Left+13;',
  21206. ' Self.Size:=Self.Size+21;',
  21207. ' Self.Speed:=Self.Speed+22;',
  21208. ' Self.Left:=Self.Left+23;',
  21209. ' with Self do begin',
  21210. ' Size:=Size+31;',
  21211. ' Speed:=Speed+32;',
  21212. ' Left:=Left+33;',
  21213. ' end;',
  21214. 'end;',
  21215. 'procedure TObjHelper.SetLeft(Value: word);',
  21216. 'begin',
  21217. 'end;',
  21218. 'procedure TBird.DoIt;',
  21219. 'begin',
  21220. ' NotRight:=NotRight+11;',
  21221. ' Self.NotRight:=Self.NotRight+21;',
  21222. ' with Self do begin',
  21223. ' NotRight:=NotRight+31;',
  21224. ' end;',
  21225. 'end;',
  21226. 'begin',
  21227. ' b.Size:=b.Size+11;',
  21228. ' b.Speed:=b.Speed+12;',
  21229. ' b.Left:=b.Left+13;',
  21230. ' b.NotRight:=b.NotRight+14;',
  21231. ' with b do begin',
  21232. ' Size:=Size+31;',
  21233. ' Speed:=Speed+32;',
  21234. ' Left:=Left+33;',
  21235. ' NotRight:=NotRight+34;',
  21236. ' end;',
  21237. '']);
  21238. ConvertProgram;
  21239. CheckSource('TestClassHelper_Property',
  21240. LinesToStr([ // statements
  21241. 'rtl.createClass(this, "TObject", null, function () {',
  21242. ' this.$init = function () {',
  21243. ' this.FSize = 0;',
  21244. ' };',
  21245. ' this.$final = function () {',
  21246. ' };',
  21247. ' this.GetSpeed = function () {',
  21248. ' var Result = 0;',
  21249. ' this.FSize = this.FSize + 11;',
  21250. ' this.SetSpeed(this.GetSpeed() + 12);',
  21251. ' Result = $mod.TObjHelper.GetLeft.call(this) + 13;',
  21252. ' $mod.TObjHelper.SetLeft.call(this, 13);',
  21253. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  21254. ' this.FSize = this.FSize + 21;',
  21255. ' this.SetSpeed(this.GetSpeed() + 22);',
  21256. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  21257. ' this.FSize = this.FSize + 31;',
  21258. ' this.SetSpeed(this.GetSpeed() + 32);',
  21259. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  21260. ' return Result;',
  21261. ' };',
  21262. ' this.SetSpeed = function (Value) {',
  21263. ' };',
  21264. '});',
  21265. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21266. ' this.GetLeft = function () {',
  21267. ' var Result = 0;',
  21268. ' this.FSize = this.FSize + 11;',
  21269. ' this.SetSpeed(this.GetSpeed() + 12);',
  21270. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  21271. ' this.FSize = this.FSize + 21;',
  21272. ' this.SetSpeed(this.GetSpeed() + 22);',
  21273. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  21274. ' this.FSize = this.FSize + 31;',
  21275. ' this.SetSpeed(this.GetSpeed() + 32);',
  21276. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  21277. ' return Result;',
  21278. ' };',
  21279. ' this.SetLeft = function (Value) {',
  21280. ' };',
  21281. '});',
  21282. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21283. ' this.DoIt = function () {',
  21284. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 11);',
  21285. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 21);',
  21286. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 31);',
  21287. ' };',
  21288. '});',
  21289. 'this.b = null;',
  21290. '']),
  21291. LinesToStr([ // $mod.$main
  21292. '$mod.b.FSize = $mod.b.FSize + 11;',
  21293. '$mod.b.SetSpeed($mod.b.GetSpeed() + 12);',
  21294. '$mod.TObjHelper.SetLeft.call($mod.b, $mod.TObjHelper.GetLeft.call($mod.b) + 13);',
  21295. '$mod.TObjHelper.SetLeft.call($mod.b, $mod.TObjHelper.GetLeft.call($mod.b) + 14);',
  21296. 'var $with = $mod.b;',
  21297. '$with.FSize = $with.FSize + 31;',
  21298. '$with.SetSpeed($with.GetSpeed() + 32);',
  21299. '$mod.TObjHelper.SetLeft.call($with, $mod.TObjHelper.GetLeft.call($with) + 33);',
  21300. '$mod.TObjHelper.SetLeft.call($with, $mod.TObjHelper.GetLeft.call($with) + 34);',
  21301. '']));
  21302. end;
  21303. procedure TTestModule.TestClassHelper_Property_Array;
  21304. begin
  21305. StartProgram(false);
  21306. Add([
  21307. 'type',
  21308. ' TObject = class',
  21309. ' function GetSpeed(Index: boolean): word;',
  21310. ' procedure SetSpeed(Index: boolean; Value: word);',
  21311. ' end;',
  21312. ' TObjHelper = class helper for TObject',
  21313. ' function GetSize(Index: boolean): word;',
  21314. ' procedure SetSize(Index: boolean; Value: word);',
  21315. ' property Size[Index: boolean]: word read GetSize write SetSize;',
  21316. ' property Speed[Index: boolean]: word read GetSpeed write SetSpeed;',
  21317. ' end;',
  21318. ' TBird = class',
  21319. ' property Items[Index: boolean]: word read GetSize write SetSize;',
  21320. ' procedure DoIt;',
  21321. ' end;',
  21322. 'var',
  21323. ' b: TBird;',
  21324. 'function Tobject.GetSpeed(Index: boolean): word;',
  21325. 'begin',
  21326. ' Result:=Size[false];',
  21327. ' Size[true]:=Size[false]+11;',
  21328. ' Speed[true]:=Speed[false]+12;',
  21329. ' Self.Size[true]:=Self.Size[false]+21;',
  21330. ' Self.Speed[true]:=Self.Speed[false]+22;',
  21331. ' with Self do begin',
  21332. ' Size[true]:=Size[false]+31;',
  21333. ' Speed[true]:=Speed[false]+32;',
  21334. ' end;',
  21335. 'end;',
  21336. 'procedure Tobject.SetSpeed(Index: boolean; Value: word);',
  21337. 'begin',
  21338. 'end;',
  21339. 'function TObjHelper.GetSize(Index: boolean): word;',
  21340. 'begin',
  21341. ' Size[true]:=Size[false]+11;',
  21342. ' Speed[true]:=Speed[false]+12;',
  21343. ' Self.Size[true]:=Self.Size[false]+21;',
  21344. ' Self.Speed[true]:=Self.Speed[false]+22;',
  21345. ' with Self do begin',
  21346. ' Size[true]:=Size[false]+31;',
  21347. ' Speed[true]:=Speed[false]+32;',
  21348. ' end;',
  21349. 'end;',
  21350. 'procedure TObjHelper.SetSize(Index: boolean; Value: word);',
  21351. 'begin',
  21352. 'end;',
  21353. 'procedure TBird.DoIt;',
  21354. 'begin',
  21355. ' Items[true]:=Items[false]+11;',
  21356. ' Self.Items[true]:=Self.Items[false]+21;',
  21357. ' with Self do Items[true]:=Items[false]+31;',
  21358. 'end;',
  21359. 'begin',
  21360. ' b.Size[true]:=b.Size[false]+11;',
  21361. ' b.Speed[true]:=b.Speed[false]+12;',
  21362. ' b.Items[true]:=b.Items[false]+13;',
  21363. ' with b do begin',
  21364. ' Size[true]:=Size[false]+21;',
  21365. ' Speed[true]:=Speed[false]+22;',
  21366. ' Items[true]:=Items[false]+23;',
  21367. ' end;',
  21368. '']);
  21369. ConvertProgram;
  21370. CheckSource('TestClassHelper_Property_Array',
  21371. LinesToStr([ // statements
  21372. 'rtl.createClass(this, "TObject", null, function () {',
  21373. ' this.$init = function () {',
  21374. ' };',
  21375. ' this.$final = function () {',
  21376. ' };',
  21377. ' this.GetSpeed = function (Index) {',
  21378. ' var Result = 0;',
  21379. ' Result = $mod.TObjHelper.GetSize.call(this, false);',
  21380. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  21381. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  21382. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  21383. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  21384. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  21385. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  21386. ' return Result;',
  21387. ' };',
  21388. ' this.SetSpeed = function (Index, Value) {',
  21389. ' };',
  21390. '});',
  21391. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21392. ' this.GetSize = function (Index) {',
  21393. ' var Result = 0;',
  21394. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  21395. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  21396. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  21397. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  21398. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  21399. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  21400. ' return Result;',
  21401. ' };',
  21402. ' this.SetSize = function (Index, Value) {',
  21403. ' };',
  21404. '});',
  21405. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21406. ' this.DoIt = function () {',
  21407. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  21408. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  21409. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  21410. ' };',
  21411. '});',
  21412. 'this.b = null;',
  21413. '']),
  21414. LinesToStr([ // $mod.$main
  21415. '$mod.TObjHelper.SetSize.call($mod.b, true, $mod.TObjHelper.GetSize.call($mod.b, false) + 11);',
  21416. '$mod.b.SetSpeed(true, $mod.b.GetSpeed(false) + 12);',
  21417. '$mod.TObjHelper.SetSize.call($mod.b, true, $mod.TObjHelper.GetSize.call($mod.b, false) + 13);',
  21418. 'var $with = $mod.b;',
  21419. '$mod.TObjHelper.SetSize.call($with, true, $mod.TObjHelper.GetSize.call($with, false) + 21);',
  21420. '$with.SetSpeed(true, $with.GetSpeed(false) + 22);',
  21421. '$mod.TObjHelper.SetSize.call($with, true, $mod.TObjHelper.GetSize.call($with, false) + 23);',
  21422. '']));
  21423. end;
  21424. procedure TTestModule.TestClassHelper_Property_Array_Default;
  21425. begin
  21426. StartProgram(false);
  21427. Add([
  21428. 'type',
  21429. ' TObject = class',
  21430. ' function GetSpeed(Index: boolean): word;',
  21431. ' procedure SetSpeed(Index: boolean; Value: word);',
  21432. ' end;',
  21433. ' TObjHelper = class helper for TObject',
  21434. ' property Speed[Index: boolean]: word read GetSpeed write SetSpeed; default;',
  21435. ' end;',
  21436. ' TBird = class',
  21437. ' end;',
  21438. ' TBirdHelper = class helper for TBird',
  21439. ' function GetSize(Index: word): boolean;',
  21440. ' procedure SetSize(Index: word; Value: boolean);',
  21441. ' property Size[Index: word]: boolean read GetSize write SetSize; default;',
  21442. ' end;',
  21443. 'function Tobject.GetSpeed(Index: boolean): word;',
  21444. 'begin',
  21445. ' Self[true]:=Self[false]+1;',
  21446. 'end;',
  21447. 'procedure Tobject.SetSpeed(Index: boolean; Value: word);',
  21448. 'begin',
  21449. 'end;',
  21450. 'function TBirdHelper.GetSize(Index: word): boolean;',
  21451. 'begin',
  21452. ' Self[1]:=not Self[2];',
  21453. 'end;',
  21454. 'procedure TBirdHelper.SetSize(Index: word; Value: boolean);',
  21455. 'begin',
  21456. 'end;',
  21457. 'var',
  21458. ' o: TObject;',
  21459. ' b: TBird;',
  21460. 'begin',
  21461. ' o[true]:=o[false]+1;',
  21462. ' b[3]:=not b[4];',
  21463. '']);
  21464. ConvertProgram;
  21465. CheckSource('TestClassHelper_Property_Array_Default',
  21466. LinesToStr([ // statements
  21467. 'rtl.createClass(this, "TObject", null, function () {',
  21468. ' this.$init = function () {',
  21469. ' };',
  21470. ' this.$final = function () {',
  21471. ' };',
  21472. ' this.GetSpeed = function (Index) {',
  21473. ' var Result = 0;',
  21474. ' this.SetSpeed(true, this.GetSpeed(false) + 1);',
  21475. ' return Result;',
  21476. ' };',
  21477. ' this.SetSpeed = function (Index, Value) {',
  21478. ' };',
  21479. '});',
  21480. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21481. '});',
  21482. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21483. '});',
  21484. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  21485. ' this.GetSize = function (Index) {',
  21486. ' var Result = false;',
  21487. ' $mod.TBirdHelper.SetSize.call(this, 1, !$mod.TBirdHelper.GetSize.call(this, 2));',
  21488. ' return Result;',
  21489. ' };',
  21490. ' this.SetSize = function (Index, Value) {',
  21491. ' };',
  21492. '});',
  21493. 'this.o = null;',
  21494. 'this.b = null;',
  21495. '']),
  21496. LinesToStr([ // $mod.$main
  21497. '$mod.o.SetSpeed(true, $mod.o.GetSpeed(false) + 1);',
  21498. '$mod.TBirdHelper.SetSize.call($mod.b, 3, !$mod.TBirdHelper.GetSize.call($mod.b, 4));',
  21499. '']));
  21500. end;
  21501. procedure TTestModule.TestClassHelper_Property_Array_DefaultDefault;
  21502. begin
  21503. StartProgram(false);
  21504. Add([
  21505. 'type',
  21506. ' TObject = class',
  21507. ' end;',
  21508. ' TObjHelper = class helper for TObject',
  21509. ' function GetItems(Index: word): TObject;',
  21510. ' procedure SetItems(Index: word; Value: TObject);',
  21511. ' property Items[Index: word]: TObject read GetItems write SetItems; default;',
  21512. ' end;',
  21513. 'function Tobjhelper.GetItems(Index: word): TObject;',
  21514. 'begin',
  21515. ' Self[1][2]:=Self[3][4];',
  21516. 'end;',
  21517. 'procedure Tobjhelper.SetItems(Index: word; Value: TObject);',
  21518. 'begin',
  21519. 'end;',
  21520. 'var',
  21521. ' o: TObject;',
  21522. 'begin',
  21523. ' o[1][2]:=o[3][4];',
  21524. '']);
  21525. ConvertProgram;
  21526. CheckSource('TestClassHelper_Property_Array_DefaultDefault',
  21527. LinesToStr([ // statements
  21528. 'rtl.createClass(this, "TObject", null, function () {',
  21529. ' this.$init = function () {',
  21530. ' };',
  21531. ' this.$final = function () {',
  21532. ' };',
  21533. '});',
  21534. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21535. ' this.GetItems = function (Index) {',
  21536. ' var Result = null;',
  21537. ' $mod.TObjHelper.SetItems.call($mod.TObjHelper.GetItems.call(this, 1), 2, $mod.TObjHelper.GetItems.call($mod.TObjHelper.GetItems.call(this, 3), 4));',
  21538. ' return Result;',
  21539. ' };',
  21540. ' this.SetItems = function (Index, Value) {',
  21541. ' };',
  21542. '});',
  21543. 'this.o = null;',
  21544. '']),
  21545. LinesToStr([ // $mod.$main
  21546. '$mod.TObjHelper.SetItems.call($mod.TObjHelper.GetItems.call($mod.o, 1), 2, $mod.TObjHelper.GetItems.call($mod.TObjHelper.GetItems.call($mod.o, 3), 4));',
  21547. '']));
  21548. end;
  21549. procedure TTestModule.TestClassHelper_ClassProperty;
  21550. begin
  21551. StartProgram(false);
  21552. Add([
  21553. 'type',
  21554. ' TObject = class',
  21555. ' class var FSize: word;',
  21556. ' class function GetSpeed: word;',
  21557. ' class procedure SetSpeed(Value: word); virtual; abstract;',
  21558. ' end;',
  21559. ' TObjHelper = class helper for TObject',
  21560. ' class function GetLeft: word;',
  21561. ' class procedure SetLeft(Value: word);',
  21562. ' class property Size: word read FSize write FSize;',
  21563. ' class property Speed: word read GetSpeed write SetSpeed;',
  21564. ' class property Left: word read GetLeft write SetLeft;',
  21565. ' end;',
  21566. ' TBird = class',
  21567. ' class property NotRight: word read GetLeft write SetLeft;',
  21568. ' class procedure DoIt;',
  21569. ' end;',
  21570. ' TBirdClass = class of TBird;',
  21571. 'class function Tobject.GetSpeed: word;',
  21572. 'begin',
  21573. ' Size:=Size+11;',
  21574. ' Speed:=Speed+12;',
  21575. ' Left:=Left+13;',
  21576. ' Self.Size:=Self.Size+21;',
  21577. ' Self.Speed:=Self.Speed+22;',
  21578. ' Self.Left:=Self.Left+23;',
  21579. ' with Self do begin',
  21580. ' Size:=Size+31;',
  21581. ' Speed:=Speed+32;',
  21582. ' Left:=Left+33;',
  21583. ' end;',
  21584. 'end;',
  21585. 'class function TObjHelper.GetLeft: word;',
  21586. 'begin',
  21587. ' Size:=Size+11;',
  21588. ' Speed:=Speed+12;',
  21589. ' Left:=Left+13;',
  21590. ' Self.Size:=Self.Size+21;',
  21591. ' Self.Speed:=Self.Speed+22;',
  21592. ' Self.Left:=Self.Left+23;',
  21593. ' with Self do begin',
  21594. ' Size:=Size+31;',
  21595. ' Speed:=Speed+32;',
  21596. ' Left:=Left+33;',
  21597. ' end;',
  21598. 'end;',
  21599. 'class procedure TObjHelper.SetLeft(Value: word);',
  21600. 'begin',
  21601. 'end;',
  21602. 'class procedure TBird.DoIt;',
  21603. 'begin',
  21604. ' NotRight:=NotRight+11;',
  21605. ' Self.NotRight:=Self.NotRight+21;',
  21606. ' with Self do NotRight:=NotRight+31;',
  21607. 'end;',
  21608. 'var',
  21609. ' b: TBird;',
  21610. ' c: TBirdClass;',
  21611. 'begin',
  21612. ' b.Size:=b.Size+11;',
  21613. ' b.Speed:=b.Speed+12;',
  21614. ' b.Left:=b.Left+13;',
  21615. ' b.NotRight:=b.NotRight+14;',
  21616. ' with b do begin',
  21617. ' Size:=Size+31;',
  21618. ' Speed:=Speed+32;',
  21619. ' Left:=Left+33;',
  21620. ' NotRight:=NotRight+34;',
  21621. ' end;',
  21622. ' c.Size:=c.Size+11;',
  21623. ' c.Speed:=c.Speed+12;',
  21624. ' c.Left:=c.Left+13;',
  21625. ' c.NotRight:=c.NotRight+14;',
  21626. ' with c do begin',
  21627. ' Size:=Size+31;',
  21628. ' Speed:=Speed+32;',
  21629. ' Left:=Left+33;',
  21630. ' NotRight:=NotRight+34;',
  21631. ' end;',
  21632. ' tbird.Size:=tbird.Size+11;',
  21633. ' tbird.Speed:=tbird.Speed+12;',
  21634. ' tbird.Left:=tbird.Left+13;',
  21635. ' tbird.NotRight:=tbird.NotRight+14;',
  21636. ' with tbird do begin',
  21637. ' Size:=Size+31;',
  21638. ' Speed:=Speed+32;',
  21639. ' Left:=Left+33;',
  21640. ' NotRight:=NotRight+34;',
  21641. ' end;',
  21642. '']);
  21643. ConvertProgram;
  21644. CheckSource('TestClassHelper_ClassProperty',
  21645. LinesToStr([ // statements
  21646. 'rtl.createClass(this, "TObject", null, function () {',
  21647. ' this.FSize = 0;',
  21648. ' this.$init = function () {',
  21649. ' };',
  21650. ' this.$final = function () {',
  21651. ' };',
  21652. ' this.GetSpeed = function () {',
  21653. ' var Result = 0;',
  21654. ' $mod.TObject.FSize = this.FSize + 11;',
  21655. ' this.SetSpeed(this.GetSpeed() + 12);',
  21656. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  21657. ' $mod.TObject.FSize = this.FSize + 21;',
  21658. ' this.SetSpeed(this.GetSpeed() + 22);',
  21659. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  21660. ' $mod.TObject.FSize = this.FSize + 31;',
  21661. ' this.SetSpeed(this.GetSpeed() + 32);',
  21662. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  21663. ' return Result;',
  21664. ' };',
  21665. '});',
  21666. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21667. ' this.GetLeft = function () {',
  21668. ' var Result = 0;',
  21669. ' $mod.TObject.FSize = this.FSize + 11;',
  21670. ' this.SetSpeed(this.GetSpeed() + 12);',
  21671. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  21672. ' $mod.TObject.FSize = this.FSize + 21;',
  21673. ' this.SetSpeed(this.GetSpeed() + 22);',
  21674. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  21675. ' $mod.TObject.FSize = this.FSize + 31;',
  21676. ' this.SetSpeed(this.GetSpeed() + 32);',
  21677. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  21678. ' return Result;',
  21679. ' };',
  21680. ' this.SetLeft = function (Value) {',
  21681. ' };',
  21682. '});',
  21683. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21684. ' this.DoIt = function () {',
  21685. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 11);',
  21686. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 21);',
  21687. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 31);',
  21688. ' };',
  21689. '});',
  21690. 'this.b = null;',
  21691. 'this.c = null;',
  21692. '']),
  21693. LinesToStr([ // $mod.$main
  21694. '$mod.TObject.FSize = $mod.b.FSize + 11;',
  21695. '$mod.b.$class.SetSpeed($mod.b.$class.GetSpeed() + 12);',
  21696. '$mod.TObjHelper.SetLeft.call($mod.b.$class, $mod.TObjHelper.GetLeft.call($mod.b.$class) + 13);',
  21697. '$mod.TObjHelper.SetLeft.call($mod.b.$class, $mod.TObjHelper.GetLeft.call($mod.b.$class) + 14);',
  21698. 'var $with = $mod.b;',
  21699. '$mod.TObject.FSize = $with.FSize + 31;',
  21700. '$with.$class.SetSpeed($with.$class.GetSpeed() + 32);',
  21701. '$mod.TObjHelper.SetLeft.call($with.$class, $mod.TObjHelper.GetLeft.call($with.$class) + 33);',
  21702. '$mod.TObjHelper.SetLeft.call($with.$class, $mod.TObjHelper.GetLeft.call($with.$class) + 34);',
  21703. '$mod.TObject.FSize = $mod.c.FSize + 11;',
  21704. '$mod.c.SetSpeed($mod.c.GetSpeed() + 12);',
  21705. '$mod.TObjHelper.SetLeft.call($mod.c, $mod.TObjHelper.GetLeft.call($mod.c) + 13);',
  21706. '$mod.TObjHelper.SetLeft.call($mod.c, $mod.TObjHelper.GetLeft.call($mod.c) + 14);',
  21707. 'var $with1 = $mod.c;',
  21708. '$mod.TObject.FSize = $with1.FSize + 31;',
  21709. '$with1.SetSpeed($with1.GetSpeed() + 32);',
  21710. '$mod.TObjHelper.SetLeft.call($with1, $mod.TObjHelper.GetLeft.call($with1) + 33);',
  21711. '$mod.TObjHelper.SetLeft.call($with1, $mod.TObjHelper.GetLeft.call($with1) + 34);',
  21712. '$mod.TObject.FSize = $mod.TBird.FSize + 11;',
  21713. '$mod.TBird.SetSpeed($mod.TBird.GetSpeed() + 12);',
  21714. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 13);',
  21715. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 14);',
  21716. 'var $with2 = $mod.TBird;',
  21717. '$mod.TObject.FSize = $with2.FSize + 31;',
  21718. '$with2.SetSpeed($with2.GetSpeed() + 32);',
  21719. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 33);',
  21720. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 34);',
  21721. '']));
  21722. end;
  21723. procedure TTestModule.TestClassHelper_ClassPropertyStatic;
  21724. begin
  21725. StartProgram(false);
  21726. Add([
  21727. 'type',
  21728. ' TObject = class',
  21729. ' class function GetSpeed: word; static;',
  21730. ' class procedure SetSpeed(Value: word); static;',
  21731. ' end;',
  21732. ' TObjHelper = class helper for TObject',
  21733. ' class function GetLeft: word; static;',
  21734. ' class procedure SetLeft(Value: word); static;',
  21735. ' class property Speed: word read GetSpeed write SetSpeed;',
  21736. ' class property Left: word read GetLeft write SetLeft;',
  21737. ' end;',
  21738. ' TBird = class',
  21739. ' class property NotRight: word read GetLeft write SetLeft;',
  21740. ' class procedure DoIt; static;',
  21741. ' class procedure DoSome;',
  21742. ' end;',
  21743. ' TBirdClass = class of TBird;',
  21744. 'class function Tobject.GetSpeed: word;',
  21745. 'begin',
  21746. ' Speed:=Speed+12;',
  21747. ' Left:=Left+13;',
  21748. 'end;',
  21749. 'class procedure TObject.SetSpeed(Value: word);',
  21750. 'begin',
  21751. 'end;',
  21752. 'class function TObjHelper.GetLeft: word;',
  21753. 'begin',
  21754. ' Speed:=Speed+12;',
  21755. ' Left:=Left+13;',
  21756. 'end;',
  21757. 'class procedure TObjHelper.SetLeft(Value: word);',
  21758. 'begin',
  21759. 'end;',
  21760. 'class procedure TBird.DoIt;',
  21761. 'begin',
  21762. ' NotRight:=NotRight+11;',
  21763. 'end;',
  21764. 'class procedure TBird.DoSome;',
  21765. 'begin',
  21766. ' Speed:=Speed+12;',
  21767. ' Left:=Left+13;',
  21768. ' Self.Speed:=Self.Speed+22;',
  21769. ' Self.Left:=Self.Left+23;',
  21770. ' with Self do begin',
  21771. ' Speed:=Speed+32;',
  21772. ' Left:=Left+33;',
  21773. ' end;',
  21774. ' NotRight:=NotRight+11;',
  21775. ' Self.NotRight:=Self.NotRight+21;',
  21776. ' with Self do NotRight:=NotRight+31;',
  21777. 'end;',
  21778. 'var',
  21779. ' b: TBird;',
  21780. ' c: TBirdClass;',
  21781. 'begin',
  21782. ' b.Speed:=b.Speed+12;',
  21783. ' b.Left:=b.Left+13;',
  21784. ' b.NotRight:=b.NotRight+14;',
  21785. ' with b do begin',
  21786. ' Speed:=Speed+32;',
  21787. ' Left:=Left+33;',
  21788. ' NotRight:=NotRight+34;',
  21789. ' end;',
  21790. ' c.Speed:=c.Speed+12;',
  21791. ' c.Left:=c.Left+13;',
  21792. ' c.NotRight:=c.NotRight+14;',
  21793. ' with c do begin',
  21794. ' Speed:=Speed+32;',
  21795. ' Left:=Left+33;',
  21796. ' NotRight:=NotRight+34;',
  21797. ' end;',
  21798. ' tbird.Speed:=tbird.Speed+12;',
  21799. ' tbird.Left:=tbird.Left+13;',
  21800. ' tbird.NotRight:=tbird.NotRight+14;',
  21801. ' with tbird do begin',
  21802. ' Speed:=Speed+32;',
  21803. ' Left:=Left+33;',
  21804. ' NotRight:=NotRight+34;',
  21805. ' end;',
  21806. '']);
  21807. ConvertProgram;
  21808. CheckSource('TestClassHelper_ClassPropertyStatic',
  21809. LinesToStr([ // statements
  21810. 'rtl.createClass(this, "TObject", null, function () {',
  21811. ' this.$init = function () {',
  21812. ' };',
  21813. ' this.$final = function () {',
  21814. ' };',
  21815. ' this.GetSpeed = function () {',
  21816. ' var Result = 0;',
  21817. ' $mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  21818. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  21819. ' return Result;',
  21820. ' };',
  21821. ' this.SetSpeed = function (Value) {',
  21822. ' };',
  21823. '});',
  21824. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21825. ' this.GetLeft = function () {',
  21826. ' var Result = 0;',
  21827. ' $mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  21828. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  21829. ' return Result;',
  21830. ' };',
  21831. ' this.SetLeft = function (Value) {',
  21832. ' };',
  21833. '});',
  21834. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21835. ' this.DoIt = function () {',
  21836. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 11);',
  21837. ' };',
  21838. ' this.DoSome = function () {',
  21839. ' this.SetSpeed(this.GetSpeed() + 12);',
  21840. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  21841. ' this.SetSpeed(this.GetSpeed() + 22);',
  21842. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 23);',
  21843. ' this.SetSpeed(this.GetSpeed() + 32);',
  21844. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  21845. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 11);',
  21846. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 21);',
  21847. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 31);',
  21848. ' };',
  21849. '});',
  21850. 'this.b = null;',
  21851. 'this.c = null;',
  21852. '']),
  21853. LinesToStr([ // $mod.$main
  21854. '$mod.b.SetSpeed($mod.b.GetSpeed() + 12);',
  21855. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  21856. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  21857. 'var $with = $mod.b;',
  21858. '$with.SetSpeed($with.GetSpeed() + 32);',
  21859. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  21860. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  21861. '$mod.c.SetSpeed($mod.c.GetSpeed() + 12);',
  21862. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  21863. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  21864. 'var $with1 = $mod.c;',
  21865. '$with1.SetSpeed($with1.GetSpeed() + 32);',
  21866. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  21867. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  21868. '$mod.TBird.SetSpeed($mod.TBird.GetSpeed() + 12);',
  21869. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  21870. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  21871. 'var $with2 = $mod.TBird;',
  21872. '$with2.SetSpeed($with2.GetSpeed() + 32);',
  21873. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  21874. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  21875. '']));
  21876. end;
  21877. procedure TTestModule.TestClassHelper_ClassProperty_Array;
  21878. begin
  21879. StartProgram(false);
  21880. Add([
  21881. 'type',
  21882. ' TObject = class',
  21883. ' class function GetSpeed(Index: boolean): word;',
  21884. ' class procedure SetSpeed(Index: boolean; Value: word); virtual; abstract;',
  21885. ' end;',
  21886. ' TObjHelper = class helper for TObject',
  21887. ' class function GetSize(Index: boolean): word;',
  21888. ' class procedure SetSize(Index: boolean; Value: word);',
  21889. ' class property Size[Index: boolean]: word read GetSize write SetSize;',
  21890. ' class property Speed[Index: boolean]: word read GetSpeed write SetSpeed;',
  21891. ' end;',
  21892. ' TBird = class',
  21893. ' class property Items[Index: boolean]: word read GetSize write SetSize;',
  21894. ' class procedure DoIt;',
  21895. ' end;',
  21896. ' TBirdClass = class of TBird;',
  21897. 'class function Tobject.GetSpeed(Index: boolean): word;',
  21898. 'begin',
  21899. ' Size[true]:=Size[false]+11;',
  21900. ' Speed[true]:=Speed[false]+12;',
  21901. ' Self.Size[true]:=Self.Size[false]+21;',
  21902. ' Self.Speed[true]:=Self.Speed[false]+22;',
  21903. ' with Self do begin',
  21904. ' Size[true]:=Size[false]+31;',
  21905. ' Speed[true]:=Speed[false]+32;',
  21906. ' end;',
  21907. 'end;',
  21908. 'class function TObjHelper.GetSize(Index: boolean): word;',
  21909. 'begin',
  21910. ' Size[true]:=Size[false]+11;',
  21911. ' Speed[true]:=Speed[false]+12;',
  21912. ' Self.Size[true]:=Self.Size[false]+21;',
  21913. ' Self.Speed[true]:=Self.Speed[false]+22;',
  21914. ' with Self do begin',
  21915. ' Size[true]:=Size[false]+31;',
  21916. ' Speed[true]:=Speed[false]+32;',
  21917. ' end;',
  21918. 'end;',
  21919. 'class procedure TObjHelper.SetSize(Index: boolean; Value: word);',
  21920. 'begin',
  21921. 'end;',
  21922. 'class procedure TBird.DoIt;',
  21923. 'begin',
  21924. ' Items[true]:=Items[false]+11;',
  21925. ' Self.Items[true]:=Self.Items[false]+21;',
  21926. ' with Self do Items[true]:=Items[false]+31;',
  21927. 'end;',
  21928. 'var',
  21929. ' b: TBird;',
  21930. ' c: TBirdClass;',
  21931. 'begin',
  21932. ' b.Size[true]:=b.Size[false]+11;',
  21933. ' b.Speed[true]:=b.Speed[false]+12;',
  21934. ' b.Items[true]:=b.Items[false]+13;',
  21935. ' with b do begin',
  21936. ' Size[true]:=Size[false]+21;',
  21937. ' Speed[true]:=Speed[false]+22;',
  21938. ' Items[true]:=Items[false]+23;',
  21939. ' end;',
  21940. ' c.Size[true]:=c.Size[false]+11;',
  21941. ' c.Speed[true]:=c.Speed[false]+12;',
  21942. ' c.Items[true]:=c.Items[false]+13;',
  21943. ' with c do begin',
  21944. ' Size[true]:=Size[false]+21;',
  21945. ' Speed[true]:=Speed[false]+22;',
  21946. ' Items[true]:=Items[false]+23;',
  21947. ' end;',
  21948. ' TBird.Size[true]:=TBird.Size[false]+11;',
  21949. ' TBird.Speed[true]:=TBird.Speed[false]+12;',
  21950. ' TBird.Items[true]:=TBird.Items[false]+13;',
  21951. ' with TBird do begin',
  21952. ' Size[true]:=Size[false]+21;',
  21953. ' Speed[true]:=Speed[false]+22;',
  21954. ' Items[true]:=Items[false]+23;',
  21955. ' end;',
  21956. '']);
  21957. ConvertProgram;
  21958. CheckSource('TestClassHelper_ClassProperty_Array',
  21959. LinesToStr([ // statements
  21960. 'rtl.createClass(this, "TObject", null, function () {',
  21961. ' this.$init = function () {',
  21962. ' };',
  21963. ' this.$final = function () {',
  21964. ' };',
  21965. ' this.GetSpeed = function (Index) {',
  21966. ' var Result = 0;',
  21967. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  21968. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  21969. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  21970. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  21971. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  21972. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  21973. ' return Result;',
  21974. ' };',
  21975. '});',
  21976. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21977. ' this.GetSize = function (Index) {',
  21978. ' var Result = 0;',
  21979. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  21980. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  21981. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  21982. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  21983. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  21984. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  21985. ' return Result;',
  21986. ' };',
  21987. ' this.SetSize = function (Index, Value) {',
  21988. ' };',
  21989. '});',
  21990. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21991. ' this.DoIt = function () {',
  21992. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  21993. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  21994. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  21995. ' };',
  21996. '});',
  21997. 'this.b = null;',
  21998. 'this.c = null;',
  21999. '']),
  22000. LinesToStr([ // $mod.$main
  22001. '$mod.TObjHelper.SetSize.call($mod.b.$class, true, $mod.TObjHelper.GetSize.call($mod.b.$class, false) + 11);',
  22002. '$mod.b.$class.SetSpeed(true, $mod.b.$class.GetSpeed(false) + 12);',
  22003. '$mod.TObjHelper.SetSize.call($mod.b.$class, true, $mod.TObjHelper.GetSize.call($mod.b.$class, false) + 13);',
  22004. 'var $with = $mod.b;',
  22005. '$mod.TObjHelper.SetSize.call($with.$class, true, $mod.TObjHelper.GetSize.call($with.$class, false) + 21);',
  22006. '$with.$class.SetSpeed(true, $with.$class.GetSpeed(false) + 22);',
  22007. '$mod.TObjHelper.SetSize.call($with.$class, true, $mod.TObjHelper.GetSize.call($with.$class, false) + 23);',
  22008. '$mod.TObjHelper.SetSize.call($mod.c, true, $mod.TObjHelper.GetSize.call($mod.c, false) + 11);',
  22009. '$mod.c.SetSpeed(true, $mod.c.GetSpeed(false) + 12);',
  22010. '$mod.TObjHelper.SetSize.call($mod.c, true, $mod.TObjHelper.GetSize.call($mod.c, false) + 13);',
  22011. 'var $with1 = $mod.c;',
  22012. '$mod.TObjHelper.SetSize.call($with1, true, $mod.TObjHelper.GetSize.call($with1, false) + 21);',
  22013. '$with1.SetSpeed(true, $with1.GetSpeed(false) + 22);',
  22014. '$mod.TObjHelper.SetSize.call($with1, true, $mod.TObjHelper.GetSize.call($with1, false) + 23);',
  22015. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 11);',
  22016. '$mod.TBird.SetSpeed(true, $mod.TBird.GetSpeed(false) + 12);',
  22017. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 13);',
  22018. 'var $with2 = $mod.TBird;',
  22019. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 21);',
  22020. '$with2.SetSpeed(true, $with2.GetSpeed(false) + 22);',
  22021. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 23);',
  22022. '']));
  22023. end;
  22024. procedure TTestModule.TestClassHelper_ForIn;
  22025. begin
  22026. StartProgram(false);
  22027. Add([
  22028. 'type',
  22029. ' TObject = class end;',
  22030. ' TItem = TObject;',
  22031. ' TEnumerator = class',
  22032. ' FCurrent: TItem;',
  22033. ' property Current: TItem read FCurrent;',
  22034. ' function MoveNext: boolean;',
  22035. ' end;',
  22036. ' TBird = class',
  22037. ' end;',
  22038. ' TBirdHelper = class helper for TBird',
  22039. ' function GetEnumerator: TEnumerator;',
  22040. ' end;',
  22041. 'function TEnumerator.MoveNext: boolean;',
  22042. 'begin',
  22043. 'end;',
  22044. 'function TBirdHelper.GetEnumerator: TEnumerator;',
  22045. 'begin',
  22046. 'end;',
  22047. 'var',
  22048. ' b: TBird;',
  22049. ' i, i2: TItem;',
  22050. 'begin',
  22051. ' for i in b do i2:=i;']);
  22052. ConvertProgram;
  22053. CheckSource('TestClassHelper_ForIn',
  22054. LinesToStr([ // statements
  22055. 'rtl.createClass(this, "TObject", null, function () {',
  22056. ' this.$init = function () {',
  22057. ' };',
  22058. ' this.$final = function () {',
  22059. ' };',
  22060. '});',
  22061. 'rtl.createClass(this, "TEnumerator", this.TObject, function () {',
  22062. ' this.$init = function () {',
  22063. ' $mod.TObject.$init.call(this);',
  22064. ' this.FCurrent = null;',
  22065. ' };',
  22066. ' this.$final = function () {',
  22067. ' this.FCurrent = undefined;',
  22068. ' $mod.TObject.$final.call(this);',
  22069. ' };',
  22070. ' this.MoveNext = function () {',
  22071. ' var Result = false;',
  22072. ' return Result;',
  22073. ' };',
  22074. '});',
  22075. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  22076. '});',
  22077. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  22078. ' this.GetEnumerator = function () {',
  22079. ' var Result = null;',
  22080. ' return Result;',
  22081. ' };',
  22082. '});',
  22083. 'this.b = null;',
  22084. 'this.i = null;',
  22085. 'this.i2 = null;'
  22086. ]),
  22087. LinesToStr([ // $mod.$main
  22088. 'var $in = $mod.TBirdHelper.GetEnumerator.call($mod.b);',
  22089. 'try {',
  22090. ' while ($in.MoveNext()){',
  22091. ' $mod.i = $in.FCurrent;',
  22092. ' $mod.i2 = $mod.i;',
  22093. ' }',
  22094. '} finally {',
  22095. ' $in = rtl.freeLoc($in)',
  22096. '};',
  22097. '']));
  22098. end;
  22099. procedure TTestModule.TestClassHelper_PassProperty;
  22100. begin
  22101. StartProgram(false);
  22102. Add([
  22103. 'type',
  22104. ' TObject = class',
  22105. ' FField: TObject;',
  22106. ' property Field: TObject read FField write FField;',
  22107. ' end;',
  22108. ' THelper = class helper for TObject',
  22109. ' procedure Fly;',
  22110. ' class procedure Run;',
  22111. ' class procedure Jump; static;',
  22112. ' end;',
  22113. 'procedure THelper.Fly;',
  22114. 'begin',
  22115. ' Field.Fly;',
  22116. ' Field.Run;',
  22117. ' Field.Jump;',
  22118. ' with Field do begin',
  22119. ' Fly;',
  22120. ' Run;',
  22121. ' Jump;',
  22122. ' end;',
  22123. 'end;',
  22124. 'class procedure THelper.Run;',
  22125. 'begin',
  22126. 'end;',
  22127. 'class procedure THelper.Jump;',
  22128. 'begin',
  22129. 'end;',
  22130. 'var',
  22131. ' b: TObject;',
  22132. 'begin',
  22133. ' b.Field.Fly;',
  22134. ' b.Field.Run;',
  22135. ' b.Field.Jump;',
  22136. ' with b do begin',
  22137. ' Field.Run;',
  22138. ' Field.Fly;',
  22139. ' Field.Jump;',
  22140. ' end;',
  22141. ' with b.Field do begin',
  22142. ' Run;',
  22143. ' Fly;',
  22144. ' Jump;',
  22145. ' end;',
  22146. '']);
  22147. ConvertProgram;
  22148. CheckSource('TestClassHelper_PassProperty',
  22149. LinesToStr([ // statements
  22150. 'rtl.createClass(this, "TObject", null, function () {',
  22151. ' this.$init = function () {',
  22152. ' this.FField = null;',
  22153. ' };',
  22154. ' this.$final = function () {',
  22155. ' this.FField = undefined;',
  22156. ' };',
  22157. '});',
  22158. 'rtl.createHelper(this, "THelper", null, function () {',
  22159. ' this.Fly = function () {',
  22160. ' $mod.THelper.Fly.call(this.FField);',
  22161. ' $mod.THelper.Run.call(this.FField.$class);',
  22162. ' $mod.THelper.Jump();',
  22163. ' var $with = this.FField;',
  22164. ' $mod.THelper.Fly.call($with);',
  22165. ' $mod.THelper.Run.call($with.$class);',
  22166. ' $mod.THelper.Jump();',
  22167. ' };',
  22168. ' this.Run = function () {',
  22169. ' };',
  22170. ' this.Jump = function () {',
  22171. ' };',
  22172. '});',
  22173. 'this.b = null;',
  22174. '']),
  22175. LinesToStr([ // $mod.$main
  22176. '$mod.THelper.Fly.call($mod.b.FField);',
  22177. '$mod.THelper.Run.call($mod.b.FField.$class);',
  22178. '$mod.THelper.Jump();',
  22179. 'var $with = $mod.b;',
  22180. '$mod.THelper.Run.call($with.FField.$class);',
  22181. '$mod.THelper.Fly.call($with.FField);',
  22182. '$mod.THelper.Jump();',
  22183. 'var $with1 = $mod.b.FField;',
  22184. '$mod.THelper.Run.call($with1.$class);',
  22185. '$mod.THelper.Fly.call($with1);',
  22186. '$mod.THelper.Jump();',
  22187. '']));
  22188. end;
  22189. procedure TTestModule.TestExtClassHelper_ClassVar;
  22190. begin
  22191. StartProgram(false);
  22192. Add([
  22193. '{$modeswitch externalclass}',
  22194. 'type',
  22195. ' TExtA = class external name ''ExtObj''',
  22196. ' end;',
  22197. ' THelper = class helper for TExtA',
  22198. ' const',
  22199. ' One = 1;',
  22200. ' Two: word = 2;',
  22201. ' class var',
  22202. ' Glob: word;',
  22203. ' function Foo(w: word): word;',
  22204. ' class function Bar(w: word): word; static;',
  22205. ' end;',
  22206. 'function THelper.foo(w: word): word;',
  22207. 'begin',
  22208. ' Result:=w;',
  22209. ' Two:=One+w;',
  22210. ' Glob:=Glob;',
  22211. ' Result:=Self.Glob;',
  22212. ' Self.Glob:=Self.Glob;',
  22213. ' with Self do Glob:=Glob;',
  22214. 'end;',
  22215. 'class function THelper.bar(w: word): word;',
  22216. 'begin',
  22217. ' Result:=w;',
  22218. ' Two:=One;',
  22219. ' Glob:=Glob;',
  22220. 'end;',
  22221. 'var o: TExtA;',
  22222. 'begin',
  22223. ' texta.two:=texta.one;',
  22224. ' texta.Glob:=texta.Glob;',
  22225. ' with texta do begin',
  22226. ' two:=one;',
  22227. ' Glob:=Glob;',
  22228. ' end;',
  22229. ' o.two:=o.one;',
  22230. ' o.Glob:=o.Glob;',
  22231. ' with o do begin',
  22232. ' two:=one;',
  22233. ' Glob:=Glob;',
  22234. ' end;',
  22235. '']);
  22236. ConvertProgram;
  22237. CheckSource('TestExtClassHelper_ClassVar',
  22238. LinesToStr([ // statements
  22239. 'rtl.createHelper(this, "THelper", null, function () {',
  22240. ' this.One = 1;',
  22241. ' this.Two = 2;',
  22242. ' this.Glob = 0;',
  22243. ' this.Foo = function (w) {',
  22244. ' var Result = 0;',
  22245. ' Result = w;',
  22246. ' $mod.THelper.Two = 1 + w;',
  22247. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22248. ' Result = $mod.THelper.Glob;',
  22249. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22250. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22251. ' return Result;',
  22252. ' };',
  22253. ' this.Bar = function (w) {',
  22254. ' var Result = 0;',
  22255. ' Result = w;',
  22256. ' $mod.THelper.Two = 1;',
  22257. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22258. ' return Result;',
  22259. ' };',
  22260. '});',
  22261. 'this.o = null;',
  22262. '']),
  22263. LinesToStr([ // $mod.$main
  22264. '$mod.THelper.Two = 1;',
  22265. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22266. '$mod.THelper.Two = 1;',
  22267. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22268. '$mod.THelper.Two = 1;',
  22269. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22270. 'var $with = $mod.o;',
  22271. '$mod.THelper.Two = 1;',
  22272. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22273. '']));
  22274. end;
  22275. procedure TTestModule.TestExtClassHelper_Method_Call;
  22276. begin
  22277. StartProgram(false);
  22278. Add([
  22279. '{$modeswitch externalclass}',
  22280. 'type',
  22281. ' TFly = function(w: word): word of object;',
  22282. ' TExtA = class external name ''ExtObj''',
  22283. ' procedure Run(w: word = 10);',
  22284. ' end;',
  22285. ' THelper = class helper for TExtA',
  22286. ' function Foo(w: word = 1): word;',
  22287. ' function Fly(w: word = 2): word; external name ''Fly'';',
  22288. ' end;',
  22289. 'var p: TFly;',
  22290. 'function THelper.foo(w: word): word;',
  22291. 'begin',
  22292. ' Run;',
  22293. ' Run();',
  22294. ' Run(11);',
  22295. ' Foo;',
  22296. ' Foo();',
  22297. ' Foo(12);',
  22298. ' Self.Foo;',
  22299. ' Self.Foo();',
  22300. ' Self.Foo(13);',
  22301. ' Fly;',
  22302. ' Fly();',
  22303. ' with Self do begin',
  22304. ' Foo;',
  22305. ' Foo();',
  22306. ' Foo(14);',
  22307. ' Fly;',
  22308. ' Fly();',
  22309. ' end;',
  22310. ' p:=@Fly;',
  22311. 'end;',
  22312. 'var Obj: TExtA;',
  22313. 'begin',
  22314. ' obj.Foo;',
  22315. ' obj.Foo();',
  22316. ' obj.Foo(21);',
  22317. ' obj.Fly;',
  22318. ' obj.Fly();',
  22319. ' with obj do begin',
  22320. ' Foo;',
  22321. ' Foo();',
  22322. ' Foo(22);',
  22323. ' Fly;',
  22324. ' Fly();',
  22325. ' end;',
  22326. ' p:[email protected];',
  22327. '']);
  22328. ConvertProgram;
  22329. CheckSource('TestExtClassHelper_Method_Call',
  22330. LinesToStr([ // statements
  22331. 'rtl.createHelper(this, "THelper", null, function () {',
  22332. ' this.Foo = function (w) {',
  22333. ' var Result = 0;',
  22334. ' this.Run(10);',
  22335. ' this.Run(10);',
  22336. ' this.Run(11);',
  22337. ' $mod.THelper.Foo.call(this, 1);',
  22338. ' $mod.THelper.Foo.call(this, 1);',
  22339. ' $mod.THelper.Foo.call(this, 12);',
  22340. ' $mod.THelper.Foo.call(this, 1);',
  22341. ' $mod.THelper.Foo.call(this, 1);',
  22342. ' $mod.THelper.Foo.call(this, 13);',
  22343. ' this.Fly(2);',
  22344. ' this.Fly(2);',
  22345. ' $mod.THelper.Foo.call(this, 1);',
  22346. ' $mod.THelper.Foo.call(this, 1);',
  22347. ' $mod.THelper.Foo.call(this, 14);',
  22348. ' this.Fly(2);',
  22349. ' this.Fly(2);',
  22350. ' $mod.p = rtl.createCallback(this, "Fly");',
  22351. ' return Result;',
  22352. ' };',
  22353. '});',
  22354. 'this.p = null;',
  22355. 'this.Obj = null;',
  22356. '']),
  22357. LinesToStr([ // $mod.$main
  22358. '$mod.THelper.Foo.call($mod.Obj, 1);',
  22359. '$mod.THelper.Foo.call($mod.Obj, 1);',
  22360. '$mod.THelper.Foo.call($mod.Obj, 21);',
  22361. '$mod.Obj.Fly(2);',
  22362. '$mod.Obj.Fly(2);',
  22363. 'var $with = $mod.Obj;',
  22364. '$mod.THelper.Foo.call($with, 1);',
  22365. '$mod.THelper.Foo.call($with, 1);',
  22366. '$mod.THelper.Foo.call($with, 22);',
  22367. '$with.Fly(2);',
  22368. '$with.Fly(2);',
  22369. '$mod.p = rtl.createCallback($mod.Obj, "Fly");',
  22370. '']));
  22371. end;
  22372. procedure TTestModule.TestExtClassHelper_ClassMethod_MissingStatic;
  22373. begin
  22374. StartProgram(false);
  22375. Add([
  22376. '{$modeswitch externalclass}',
  22377. 'type',
  22378. ' TExtA = class external name ''ExtObj''',
  22379. ' procedure Run(w: word = 10);',
  22380. ' end;',
  22381. ' THelper = class helper for TExtA',
  22382. ' class procedure Fly;',
  22383. ' end;',
  22384. 'class procedure THelper.Fly;',
  22385. 'begin end;',
  22386. 'begin',
  22387. '']);
  22388. SetExpectedPasResolverError(sHelperClassMethodForExtClassMustBeStatic,
  22389. nHelperClassMethodForExtClassMustBeStatic);
  22390. ConvertProgram;
  22391. end;
  22392. procedure TTestModule.TestRecordHelper_ClassVar;
  22393. begin
  22394. StartProgram(false);
  22395. Add([
  22396. 'type',
  22397. ' TRec = record',
  22398. ' end;',
  22399. ' THelper = record helper for TRec',
  22400. ' const',
  22401. ' One = 1;',
  22402. ' Two: word = 2;',
  22403. ' class var',
  22404. ' Glob: word;',
  22405. ' function Foo(w: word): word;',
  22406. ' class function Bar(w: word): word; static;',
  22407. ' end;',
  22408. 'function THelper.foo(w: word): word;',
  22409. 'begin',
  22410. ' Result:=w;',
  22411. ' Two:=One+w;',
  22412. ' Glob:=Glob;',
  22413. ' Result:=Self.Glob;',
  22414. ' Self.Glob:=Self.Glob;',
  22415. ' with Self do Glob:=Glob;',
  22416. ' Self:=Self;',
  22417. 'end;',
  22418. 'class function THelper.bar(w: word): word;',
  22419. 'begin',
  22420. ' Result:=w;',
  22421. ' Two:=One;',
  22422. ' Glob:=Glob;',
  22423. 'end;',
  22424. 'var r: TRec;',
  22425. 'begin',
  22426. ' trec.two:=trec.one;',
  22427. ' trec.Glob:=trec.Glob;',
  22428. ' with trec do begin',
  22429. ' two:=one;',
  22430. ' Glob:=Glob;',
  22431. ' end;',
  22432. ' r.two:=r.one;',
  22433. ' r.Glob:=r.Glob;',
  22434. ' with r do begin',
  22435. ' two:=one;',
  22436. ' Glob:=Glob;',
  22437. ' end;',
  22438. '']);
  22439. ConvertProgram;
  22440. CheckSource('TestRecordHelper_ClassVar',
  22441. LinesToStr([ // statements
  22442. 'rtl.recNewT(this, "TRec", function () {',
  22443. ' this.$eq = function (b) {',
  22444. ' return true;',
  22445. ' };',
  22446. ' this.$assign = function (s) {',
  22447. ' return this;',
  22448. ' };',
  22449. '});',
  22450. 'rtl.createHelper(this, "THelper", null, function () {',
  22451. ' this.One = 1;',
  22452. ' this.Two = 2;',
  22453. ' this.Glob = 0;',
  22454. ' this.Foo = function (w) {',
  22455. ' var Result = 0;',
  22456. ' Result = w;',
  22457. ' $mod.THelper.Two = 1 + w;',
  22458. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22459. ' Result = $mod.THelper.Glob;',
  22460. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22461. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22462. ' this.$assign(this);',
  22463. ' return Result;',
  22464. ' };',
  22465. ' this.Bar = function (w) {',
  22466. ' var Result = 0;',
  22467. ' Result = w;',
  22468. ' $mod.THelper.Two = 1;',
  22469. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22470. ' return Result;',
  22471. ' };',
  22472. '});',
  22473. 'this.r = this.TRec.$new();',
  22474. '']),
  22475. LinesToStr([ // $mod.$main
  22476. '$mod.THelper.Two = 1;',
  22477. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22478. 'var $with = $mod.TRec;',
  22479. '$mod.THelper.Two = 1;',
  22480. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22481. '$mod.THelper.Two = 1;',
  22482. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22483. 'var $with1 = $mod.r;',
  22484. '$mod.THelper.Two = 1;',
  22485. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22486. '']));
  22487. end;
  22488. procedure TTestModule.TestRecordHelper_Method_Call;
  22489. begin
  22490. StartProgram(false);
  22491. Add([
  22492. '{$modeswitch AdvancedRecords}',
  22493. 'type',
  22494. ' TRec = record',
  22495. ' procedure Run(w: word = 10);',
  22496. ' end;',
  22497. ' THelper = record helper for TRec',
  22498. ' function Foo(w: word = 1): word;',
  22499. ' end;',
  22500. 'procedure TRec.Run(w: word);',
  22501. 'begin',
  22502. ' Foo;',
  22503. ' Foo();',
  22504. ' Foo(2);',
  22505. ' Self.Foo;',
  22506. ' Self.Foo();',
  22507. ' Self.Foo(3);',
  22508. ' with Self do begin',
  22509. ' Foo;',
  22510. ' Foo();',
  22511. ' Foo(4);',
  22512. ' end;',
  22513. 'end;',
  22514. 'function THelper.foo(w: word): word;',
  22515. 'begin',
  22516. ' Run;',
  22517. ' Run();',
  22518. ' Run(11);',
  22519. ' Foo;',
  22520. ' Foo();',
  22521. ' Foo(12);',
  22522. ' Self.Foo;',
  22523. ' Self.Foo();',
  22524. ' Self.Foo(13);',
  22525. ' with Self do begin',
  22526. ' Foo;',
  22527. ' Foo();',
  22528. ' Foo(14);',
  22529. ' end;',
  22530. 'end;',
  22531. 'var Rec: TRec;',
  22532. 'begin',
  22533. ' Rec.Foo;',
  22534. ' Rec.Foo();',
  22535. ' Rec.Foo(21);',
  22536. ' with Rec do begin',
  22537. ' Foo;',
  22538. ' Foo();',
  22539. ' Foo(22);',
  22540. ' end;',
  22541. '']);
  22542. ConvertProgram;
  22543. CheckSource('TestRecordHelper_Method_Call',
  22544. LinesToStr([ // statements
  22545. 'rtl.recNewT(this, "TRec", function () {',
  22546. ' this.$eq = function (b) {',
  22547. ' return true;',
  22548. ' };',
  22549. ' this.$assign = function (s) {',
  22550. ' return this;',
  22551. ' };',
  22552. ' this.Run = function (w) {',
  22553. ' $mod.THelper.Foo.call(this, 1);',
  22554. ' $mod.THelper.Foo.call(this, 1);',
  22555. ' $mod.THelper.Foo.call(this, 2);',
  22556. ' $mod.THelper.Foo.call(this, 1);',
  22557. ' $mod.THelper.Foo.call(this, 1);',
  22558. ' $mod.THelper.Foo.call(this, 3);',
  22559. ' $mod.THelper.Foo.call(this, 1);',
  22560. ' $mod.THelper.Foo.call(this, 1);',
  22561. ' $mod.THelper.Foo.call(this, 4);',
  22562. ' };',
  22563. '});',
  22564. 'rtl.createHelper(this, "THelper", null, function () {',
  22565. ' this.Foo = function (w) {',
  22566. ' var Result = 0;',
  22567. ' this.Run(10);',
  22568. ' this.Run(10);',
  22569. ' this.Run(11);',
  22570. ' $mod.THelper.Foo.call(this, 1);',
  22571. ' $mod.THelper.Foo.call(this, 1);',
  22572. ' $mod.THelper.Foo.call(this, 12);',
  22573. ' $mod.THelper.Foo.call(this, 1);',
  22574. ' $mod.THelper.Foo.call(this, 1);',
  22575. ' $mod.THelper.Foo.call(this, 13);',
  22576. ' $mod.THelper.Foo.call(this, 1);',
  22577. ' $mod.THelper.Foo.call(this, 1);',
  22578. ' $mod.THelper.Foo.call(this, 14);',
  22579. ' return Result;',
  22580. ' };',
  22581. '});',
  22582. 'this.Rec = this.TRec.$new();',
  22583. '']),
  22584. LinesToStr([ // $mod.$main
  22585. '$mod.THelper.Foo.call($mod.Rec, 1);',
  22586. '$mod.THelper.Foo.call($mod.Rec, 1);',
  22587. '$mod.THelper.Foo.call($mod.Rec, 21);',
  22588. 'var $with = $mod.Rec;',
  22589. '$mod.THelper.Foo.call($with, 1);',
  22590. '$mod.THelper.Foo.call($with, 1);',
  22591. '$mod.THelper.Foo.call($with, 22);',
  22592. '']));
  22593. end;
  22594. procedure TTestModule.TestRecordHelper_Constructor;
  22595. begin
  22596. StartProgram(false);
  22597. Add([
  22598. '{$modeswitch AdvancedRecords}',
  22599. 'type',
  22600. ' TRec = record',
  22601. ' constructor Create(w: word);',
  22602. ' end;',
  22603. ' THelper = record helper for TRec',
  22604. ' constructor NewHlp(w: word);',
  22605. ' end;',
  22606. 'var',
  22607. ' Rec: TRec;',
  22608. 'constructor TRec.Create(w: word);',
  22609. 'begin',
  22610. ' NewHlp(2);', // normal call
  22611. ' trec.NewHlp(3);', // new instance
  22612. 'end;',
  22613. 'constructor THelper.NewHlp(w: word);',
  22614. 'begin',
  22615. ' create(2);', // normal call
  22616. ' trec.create(3);', // new instance
  22617. ' NewHlp(4);', // normal call
  22618. ' trec.NewHlp(5);', // new instance
  22619. 'end;',
  22620. 'begin',
  22621. ' rec.newhlp(2);', // normal call
  22622. ' with rec do newhlp(12);', // normal call
  22623. ' trec.newhlp(3);', // new instance
  22624. ' with trec do newhlp(13);', // new instance
  22625. '']);
  22626. ConvertProgram;
  22627. CheckSource('TestRecordHelper_Constructor',
  22628. LinesToStr([ // statements
  22629. 'rtl.recNewT(this, "TRec", function () {',
  22630. ' this.$eq = function (b) {',
  22631. ' return true;',
  22632. ' };',
  22633. ' this.$assign = function (s) {',
  22634. ' return this;',
  22635. ' };',
  22636. ' this.Create = function (w) {',
  22637. ' $mod.THelper.NewHlp.call(this, 2);',
  22638. ' $mod.THelper.$new("NewHlp", [3]);',
  22639. ' return this;',
  22640. ' };',
  22641. '}, true);',
  22642. 'rtl.createHelper(this, "THelper", null, function () {',
  22643. ' this.NewHlp = function (w) {',
  22644. ' this.Create(2);',
  22645. ' $mod.TRec.$new().Create(3);',
  22646. ' $mod.THelper.NewHlp.call(this, 4);',
  22647. ' $mod.THelper.$new("NewHlp", [5]);',
  22648. ' return this;',
  22649. ' };',
  22650. ' this.$new = function (fn, args) {',
  22651. ' return this[fn].apply($mod.TRec.$new(), args);',
  22652. ' };',
  22653. '});',
  22654. 'this.Rec = this.TRec.$new();',
  22655. '']),
  22656. LinesToStr([ // $mod.$main
  22657. '$mod.THelper.NewHlp.call($mod.Rec, 2);',
  22658. 'var $with = $mod.Rec;',
  22659. '$mod.THelper.NewHlp.call($with, 12);',
  22660. '$mod.THelper.$new("NewHlp", [3]);',
  22661. 'var $with1 = $mod.TRec;',
  22662. '$mod.THelper.$new("NewHlp", [13]);',
  22663. '']));
  22664. end;
  22665. procedure TTestModule.TestTypeHelper_ClassVar;
  22666. begin
  22667. StartProgram(false);
  22668. Add([
  22669. '{$modeswitch typehelpers}',
  22670. 'type',
  22671. ' THelper = type helper for byte',
  22672. ' const',
  22673. ' One = 1;',
  22674. ' Two: word = 2;',
  22675. ' class var',
  22676. ' Glob: word;',
  22677. ' function Foo(w: word): word;',
  22678. ' class function Bar(w: word): word; static;',
  22679. ' end;',
  22680. 'function THelper.foo(w: word): word;',
  22681. 'begin',
  22682. ' Result:=w;',
  22683. ' Two:=One+w;',
  22684. ' Glob:=Glob;',
  22685. ' Result:=Self.Glob;',
  22686. ' Self.Glob:=Self.Glob;',
  22687. ' with Self do Glob:=Glob;',
  22688. 'end;',
  22689. 'class function THelper.bar(w: word): word;',
  22690. 'begin',
  22691. ' Result:=w;',
  22692. ' Two:=One;',
  22693. ' Glob:=Glob;',
  22694. 'end;',
  22695. 'var b: byte;',
  22696. 'begin',
  22697. ' byte.two:=byte.one;',
  22698. ' byte.Glob:=byte.Glob;',
  22699. ' with byte do begin',
  22700. ' two:=one;',
  22701. ' Glob:=Glob;',
  22702. ' end;',
  22703. ' b.two:=b.one;',
  22704. ' b.Glob:=b.Glob;',
  22705. ' with b do begin',
  22706. ' two:=one;',
  22707. ' Glob:=Glob;',
  22708. ' end;',
  22709. '']);
  22710. ConvertProgram;
  22711. CheckSource('TestTypeHelper_ClassVar',
  22712. LinesToStr([ // statements
  22713. 'rtl.createHelper(this, "THelper", null, function () {',
  22714. ' this.One = 1;',
  22715. ' this.Two = 2;',
  22716. ' this.Glob = 0;',
  22717. ' this.Foo = function (w) {',
  22718. ' var Result = 0;',
  22719. ' Result = w;',
  22720. ' $mod.THelper.Two = 1 + w;',
  22721. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22722. ' Result = $mod.THelper.Glob;',
  22723. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22724. ' var $with = this.get();',
  22725. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22726. ' return Result;',
  22727. ' };',
  22728. ' this.Bar = function (w) {',
  22729. ' var Result = 0;',
  22730. ' Result = w;',
  22731. ' $mod.THelper.Two = 1;',
  22732. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22733. ' return Result;',
  22734. ' };',
  22735. '});',
  22736. 'this.b = 0;',
  22737. '']),
  22738. LinesToStr([ // $mod.$main
  22739. '$mod.THelper.Two = 1;',
  22740. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22741. '$mod.THelper.Two = 1;',
  22742. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22743. '$mod.THelper.Two = 1;',
  22744. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22745. 'var $with = $mod.b;',
  22746. '$mod.THelper.Two = 1;',
  22747. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22748. '']));
  22749. end;
  22750. procedure TTestModule.TestTypeHelper_PassResultElement;
  22751. begin
  22752. StartProgram(false);
  22753. Add([
  22754. '{$modeswitch typehelpers}',
  22755. 'type',
  22756. ' THelper = type helper for word',
  22757. ' procedure DoIt(e: byte = 123);',
  22758. ' class procedure DoSome(e: byte = 456); static;',
  22759. ' end;',
  22760. 'procedure THelper.DoIt(e: byte);',
  22761. 'begin',
  22762. 'end;',
  22763. 'class procedure THelper.DoSome(e: byte);',
  22764. 'begin',
  22765. 'end;',
  22766. 'function Foo(w: word): word;',
  22767. 'begin',
  22768. ' Result.DoIt;',
  22769. ' Result.DoIt();',
  22770. ' Result.DoSome;',
  22771. ' Result.DoSome();',
  22772. ' with Result do begin',
  22773. ' DoIt;',
  22774. ' DoIt();',
  22775. ' DoSome;',
  22776. ' DoSome();',
  22777. ' end;',
  22778. 'end;',
  22779. 'begin',
  22780. '']);
  22781. ConvertProgram;
  22782. CheckSource('TestTypeHelper_PassResultElement',
  22783. LinesToStr([ // statements
  22784. 'rtl.createHelper(this, "THelper", null, function () {',
  22785. ' this.DoIt = function (e) {',
  22786. ' };',
  22787. ' this.DoSome = function (e) {',
  22788. ' };',
  22789. '});',
  22790. 'this.Foo = function (w) {',
  22791. ' var Result = 0;',
  22792. ' $mod.THelper.DoIt.call({',
  22793. ' get: function () {',
  22794. ' return Result;',
  22795. ' },',
  22796. ' set: function (v) {',
  22797. ' Result = v;',
  22798. ' }',
  22799. ' }, 123);',
  22800. ' $mod.THelper.DoIt.call({',
  22801. ' get: function () {',
  22802. ' return Result;',
  22803. ' },',
  22804. ' set: function (v) {',
  22805. ' Result = v;',
  22806. ' }',
  22807. ' }, 123);',
  22808. ' $mod.THelper.DoSome(456);',
  22809. ' $mod.THelper.DoSome(456);',
  22810. ' $mod.THelper.DoIt.call({',
  22811. ' get: function () {',
  22812. ' return Result;',
  22813. ' },',
  22814. ' set: function (v) {',
  22815. ' Result = v;',
  22816. ' }',
  22817. ' }, 123);',
  22818. ' $mod.THelper.DoIt.call({',
  22819. ' get: function () {',
  22820. ' return Result;',
  22821. ' },',
  22822. ' set: function (v) {',
  22823. ' Result = v;',
  22824. ' }',
  22825. ' }, 123);',
  22826. ' $mod.THelper.DoSome(456);',
  22827. ' $mod.THelper.DoSome(456);',
  22828. ' return Result;',
  22829. '};',
  22830. '']),
  22831. LinesToStr([ // $mod.$main
  22832. '']));
  22833. end;
  22834. procedure TTestModule.TestTypeHelper_PassArgs;
  22835. begin
  22836. StartProgram(false);
  22837. Add([
  22838. '{$modeswitch typehelpers}',
  22839. 'type',
  22840. ' THelper = type helper for word',
  22841. ' procedure DoIt(e: byte = 123);',
  22842. ' end;',
  22843. 'procedure THelper.DoIt(e: byte);',
  22844. 'begin',
  22845. 'end;',
  22846. 'procedure FooDefault(a: word);',
  22847. 'begin',
  22848. ' a.DoIt;',
  22849. ' with a do DoIt;',
  22850. 'end;',
  22851. 'procedure FooConst(const a: word);',
  22852. 'begin',
  22853. ' a.DoIt;',
  22854. ' with a do DoIt;',
  22855. 'end;',
  22856. 'procedure FooVar(var a: word);',
  22857. 'begin',
  22858. ' a.DoIt;',
  22859. ' with a do DoIt;',
  22860. 'end;',
  22861. 'begin',
  22862. '']);
  22863. ConvertProgram;
  22864. CheckSource('TestTypeHelper_PassArgs',
  22865. LinesToStr([ // statements
  22866. 'rtl.createHelper(this, "THelper", null, function () {',
  22867. ' this.DoIt = function (e) {',
  22868. ' };',
  22869. '});',
  22870. 'this.FooDefault = function (a) {',
  22871. ' $mod.THelper.DoIt.call({',
  22872. ' get: function () {',
  22873. ' return a;',
  22874. ' },',
  22875. ' set: function (v) {',
  22876. ' a = v;',
  22877. ' }',
  22878. ' }, 123);',
  22879. ' $mod.THelper.DoIt.call({',
  22880. ' get: function () {',
  22881. ' return a;',
  22882. ' },',
  22883. ' set: function (v) {',
  22884. ' a = v;',
  22885. ' }',
  22886. ' }, 123);',
  22887. '};',
  22888. 'this.FooConst = function (a) {',
  22889. ' $mod.THelper.DoIt.call({',
  22890. ' get: function () {',
  22891. ' return a;',
  22892. ' },',
  22893. ' set: function (v) {',
  22894. ' rtl.raiseE("EPropReadOnly");',
  22895. ' }',
  22896. ' }, 123);',
  22897. ' $mod.THelper.DoIt.call({',
  22898. ' get: function () {',
  22899. ' return a;',
  22900. ' },',
  22901. ' set: function () {',
  22902. ' rtl.raiseE("EPropReadOnly");',
  22903. ' }',
  22904. ' }, 123);',
  22905. '};',
  22906. 'this.FooVar = function (a) {',
  22907. ' $mod.THelper.DoIt.call(a, 123);',
  22908. ' var $with = a.get();',
  22909. ' $mod.THelper.DoIt.call(a, 123);',
  22910. '};',
  22911. '']),
  22912. LinesToStr([ // $mod.$main
  22913. '']));
  22914. end;
  22915. procedure TTestModule.TestTypeHelper_PassVarConst;
  22916. begin
  22917. StartProgram(false);
  22918. Add([
  22919. '{$modeswitch typehelpers}',
  22920. 'type',
  22921. ' THelper = type helper for word',
  22922. ' procedure DoIt(e: byte = 123);',
  22923. ' end;',
  22924. 'procedure THelper.DoIt(e: byte);',
  22925. 'begin',
  22926. 'end;',
  22927. 'var a: word;',
  22928. 'const c: word = 2;',
  22929. '{$writeableconst off}',
  22930. 'const r: word = 3;',
  22931. 'begin',
  22932. ' a.DoIt;',
  22933. ' with a do DoIt;',
  22934. ' c.DoIt;',
  22935. ' with c do DoIt;',
  22936. ' r.DoIt;',
  22937. ' with r do DoIt;',
  22938. '']);
  22939. ConvertProgram;
  22940. CheckSource('TestTypeHelper_PassVarConst',
  22941. LinesToStr([ // statements
  22942. 'rtl.createHelper(this, "THelper", null, function () {',
  22943. ' this.DoIt = function (e) {',
  22944. ' };',
  22945. '});',
  22946. 'this.a = 0;',
  22947. 'this.c = 2;',
  22948. 'this.r = 3;',
  22949. '']),
  22950. LinesToStr([ // $mod.$main
  22951. '$mod.THelper.DoIt.call({',
  22952. ' p: $mod,',
  22953. ' get: function () {',
  22954. ' return this.p.a;',
  22955. ' },',
  22956. ' set: function (v) {',
  22957. ' this.p.a = v;',
  22958. ' }',
  22959. '}, 123);',
  22960. 'var $with = $mod.a;',
  22961. '$mod.THelper.DoIt.call({',
  22962. ' get: function () {',
  22963. ' return $with;',
  22964. ' },',
  22965. ' set: function (v) {',
  22966. ' $with = v;',
  22967. ' }',
  22968. '}, 123);',
  22969. '$mod.THelper.DoIt.call({',
  22970. ' p: $mod,',
  22971. ' get: function () {',
  22972. ' return this.p.c;',
  22973. ' },',
  22974. ' set: function (v) {',
  22975. ' this.p.c = v;',
  22976. ' }',
  22977. '}, 123);',
  22978. 'var $with1 = $mod.c;',
  22979. '$mod.THelper.DoIt.call({',
  22980. ' get: function () {',
  22981. ' return $with1;',
  22982. ' },',
  22983. ' set: function (v) {',
  22984. ' $with1 = v;',
  22985. ' }',
  22986. '}, 123);',
  22987. '$mod.THelper.DoIt.call({',
  22988. ' get: function () {',
  22989. ' return 3;',
  22990. ' },',
  22991. ' set: function (v) {',
  22992. ' rtl.raiseE("EPropReadOnly");',
  22993. ' }',
  22994. '}, 123);',
  22995. 'var $with2 = 3;',
  22996. ' $mod.THelper.DoIt.call({',
  22997. ' get: function () {',
  22998. ' return $with2;',
  22999. ' },',
  23000. ' set: function () {',
  23001. ' rtl.raiseE("EPropReadOnly");',
  23002. ' }',
  23003. ' }, 123);',
  23004. '']));
  23005. end;
  23006. procedure TTestModule.TestTypeHelper_PassFuncResult;
  23007. begin
  23008. StartProgram(false);
  23009. Add([
  23010. '{$modeswitch typehelpers}',
  23011. 'type',
  23012. ' THelper = type helper for word',
  23013. ' procedure DoIt(e: byte = 123);',
  23014. ' end;',
  23015. 'procedure THelper.DoIt(e: byte);',
  23016. 'begin',
  23017. 'end;',
  23018. 'function Foo(b: byte = 1): word;',
  23019. 'begin',
  23020. 'end;',
  23021. 'begin',
  23022. ' Foo.DoIt;',
  23023. ' Foo().DoIt;',
  23024. ' with Foo do DoIt;',
  23025. ' with Foo() do DoIt;',
  23026. '']);
  23027. ConvertProgram;
  23028. CheckSource('TestTypeHelper_PassFuncResult',
  23029. LinesToStr([ // statements
  23030. 'rtl.createHelper(this, "THelper", null, function () {',
  23031. ' this.DoIt = function (e) {',
  23032. ' };',
  23033. '});',
  23034. 'this.Foo = function (b) {',
  23035. ' var Result = 0;',
  23036. ' return Result;',
  23037. '};',
  23038. '']),
  23039. LinesToStr([ // $mod.$main
  23040. '$mod.THelper.DoIt.call({',
  23041. ' a: $mod.Foo(1),',
  23042. ' get: function () {',
  23043. ' return this.a;',
  23044. ' },',
  23045. ' set: function (v) {',
  23046. ' this.a = v;',
  23047. ' }',
  23048. '}, 123);',
  23049. '$mod.THelper.DoIt.call({',
  23050. ' a: $mod.Foo(1),',
  23051. ' get: function () {',
  23052. ' return this.a;',
  23053. ' },',
  23054. ' set: function (v) {',
  23055. ' this.a = v;',
  23056. ' }',
  23057. '}, 123);',
  23058. 'var $with = $mod.Foo(1);',
  23059. '$mod.THelper.DoIt.call({',
  23060. ' get: function () {',
  23061. ' return $with;',
  23062. ' },',
  23063. ' set: function (v) {',
  23064. ' $with = v;',
  23065. ' }',
  23066. '}, 123);',
  23067. 'var $with1 = $mod.Foo(1);',
  23068. '$mod.THelper.DoIt.call({',
  23069. ' get: function () {',
  23070. ' return $with1;',
  23071. ' },',
  23072. ' set: function (v) {',
  23073. ' $with1 = v;',
  23074. ' }',
  23075. '}, 123);',
  23076. '']));
  23077. end;
  23078. procedure TTestModule.TestTypeHelper_PassPropertyField;
  23079. begin
  23080. StartProgram(false);
  23081. Add([
  23082. '{$modeswitch typehelpers}',
  23083. 'type',
  23084. ' TObject = class',
  23085. ' FField: word;',
  23086. ' procedure SetField(Value: word);',
  23087. ' property Field: word read FField write SetField;',
  23088. ' end;',
  23089. ' THelper = type helper for word',
  23090. ' procedure Fly;',
  23091. ' class procedure Run; static;',
  23092. ' end;',
  23093. 'procedure TObject.SetField(Value: word);',
  23094. 'begin',
  23095. ' Field.Fly;',
  23096. ' Field.Run;',
  23097. ' Self.Field.Fly;',
  23098. ' Self.Field.Run;',
  23099. ' with Self do begin',
  23100. ' Field.Fly;',
  23101. ' Field.Run;',
  23102. ' end;',
  23103. ' with Self.Field do begin',
  23104. ' Fly;',
  23105. ' Run;',
  23106. ' end;',
  23107. 'end;',
  23108. 'procedure THelper.Fly;',
  23109. 'begin',
  23110. 'end;',
  23111. 'class procedure THelper.Run;',
  23112. 'begin',
  23113. 'end;',
  23114. 'var',
  23115. ' o: TObject;',
  23116. 'begin',
  23117. ' o.Field.Fly;',
  23118. ' o.Field.Run;',
  23119. ' with o do begin',
  23120. ' Field.Fly;',
  23121. ' Field.Run;',
  23122. ' end;',
  23123. ' with o.Field do begin',
  23124. ' Fly;',
  23125. ' Run;',
  23126. ' end;',
  23127. '']);
  23128. ConvertProgram;
  23129. CheckSource('TestTypeHelper_PassPropertyField',
  23130. LinesToStr([ // statements
  23131. 'rtl.createClass(this, "TObject", null, function () {',
  23132. ' this.$init = function () {',
  23133. ' this.FField = 0;',
  23134. ' };',
  23135. ' this.$final = function () {',
  23136. ' };',
  23137. ' this.SetField = function (Value) {',
  23138. ' $mod.THelper.Fly.call({',
  23139. ' p: this,',
  23140. ' get: function () {',
  23141. ' return this.p.FField;',
  23142. ' },',
  23143. ' set: function (v) {',
  23144. ' this.p.FField = v;',
  23145. ' }',
  23146. ' });',
  23147. ' $mod.THelper.Run();',
  23148. ' $mod.THelper.Fly.call({',
  23149. ' p: this,',
  23150. ' get: function () {',
  23151. ' return this.p.FField;',
  23152. ' },',
  23153. ' set: function (v) {',
  23154. ' this.p.FField = v;',
  23155. ' }',
  23156. ' });',
  23157. ' $mod.THelper.Run();',
  23158. ' $mod.THelper.Fly.call({',
  23159. ' p: this,',
  23160. ' get: function () {',
  23161. ' return this.p.FField;',
  23162. ' },',
  23163. ' set: function (v) {',
  23164. ' this.p.FField = v;',
  23165. ' }',
  23166. ' });',
  23167. ' $mod.THelper.Run();',
  23168. ' var $with = this.FField;',
  23169. ' $mod.THelper.Fly.call({',
  23170. ' get: function () {',
  23171. ' return $with;',
  23172. ' },',
  23173. ' set: function (v) {',
  23174. ' $with = v;',
  23175. ' }',
  23176. ' });',
  23177. ' $mod.THelper.Run();',
  23178. ' };',
  23179. '});',
  23180. 'rtl.createHelper(this, "THelper", null, function () {',
  23181. ' this.Fly = function () {',
  23182. ' };',
  23183. ' this.Run = function () {',
  23184. ' };',
  23185. '});',
  23186. 'this.o = null;',
  23187. '']),
  23188. LinesToStr([ // $mod.$main
  23189. '$mod.THelper.Fly.call({',
  23190. ' p: $mod.o,',
  23191. ' get: function () {',
  23192. ' return this.p.FField;',
  23193. ' },',
  23194. ' set: function (v) {',
  23195. ' this.p.FField = v;',
  23196. ' }',
  23197. '});',
  23198. '$mod.THelper.Run();',
  23199. 'var $with = $mod.o;',
  23200. '$mod.THelper.Fly.call({',
  23201. ' p: $with,',
  23202. ' get: function () {',
  23203. ' return this.p.FField;',
  23204. ' },',
  23205. ' set: function (v) {',
  23206. ' this.p.FField = v;',
  23207. ' }',
  23208. '});',
  23209. '$mod.THelper.Run();',
  23210. 'var $with1 = $mod.o.FField;',
  23211. '$mod.THelper.Fly.call({',
  23212. ' get: function () {',
  23213. ' return $with1;',
  23214. ' },',
  23215. ' set: function (v) {',
  23216. ' $with1 = v;',
  23217. ' }',
  23218. '});',
  23219. '$mod.THelper.Run();',
  23220. '']));
  23221. end;
  23222. procedure TTestModule.TestTypeHelper_PassPropertyGetter;
  23223. begin
  23224. StartProgram(false);
  23225. Add([
  23226. '{$modeswitch typehelpers}',
  23227. 'type',
  23228. ' TObject = class',
  23229. ' FField: word;',
  23230. ' function GetField: word;',
  23231. ' property Field: word read GetField write FField;',
  23232. ' end;',
  23233. ' THelper = type helper for word',
  23234. ' procedure Fly;',
  23235. ' class procedure Run; static;',
  23236. ' end;',
  23237. 'function TObject.GetField: word;',
  23238. 'begin',
  23239. ' Field.Fly;',
  23240. ' Field.Run;',
  23241. ' Self.Field.Fly;',
  23242. ' Self.Field.Run;',
  23243. ' with Self do begin',
  23244. ' Field.Fly;',
  23245. ' Field.Run;',
  23246. ' end;',
  23247. ' with Self.Field do begin',
  23248. ' Fly;',
  23249. ' Run;',
  23250. ' end;',
  23251. 'end;',
  23252. 'procedure THelper.Fly;',
  23253. 'begin',
  23254. 'end;',
  23255. 'class procedure THelper.Run;',
  23256. 'begin',
  23257. 'end;',
  23258. 'var',
  23259. ' o: TObject;',
  23260. 'begin',
  23261. ' o.Field.Fly;',
  23262. ' o.Field.Run;',
  23263. ' with o do begin',
  23264. ' Field.Fly;',
  23265. ' Field.Run;',
  23266. ' end;',
  23267. ' with o.Field do begin',
  23268. ' Fly;',
  23269. ' Run;',
  23270. ' end;',
  23271. '']);
  23272. ConvertProgram;
  23273. CheckSource('TestTypeHelper_PassPropertyGetter',
  23274. LinesToStr([ // statements
  23275. 'rtl.createClass(this, "TObject", null, function () {',
  23276. ' this.$init = function () {',
  23277. ' this.FField = 0;',
  23278. ' };',
  23279. ' this.$final = function () {',
  23280. ' };',
  23281. ' this.GetField = function () {',
  23282. ' var Result = 0;',
  23283. ' $mod.THelper.Fly.call({',
  23284. ' p: this.GetField(),',
  23285. ' get: function () {',
  23286. ' return this.p;',
  23287. ' },',
  23288. ' set: function (v) {',
  23289. ' this.p = v;',
  23290. ' }',
  23291. ' });',
  23292. ' $mod.THelper.Run();',
  23293. ' $mod.THelper.Fly.call({',
  23294. ' p: this.GetField(),',
  23295. ' get: function () {',
  23296. ' return this.p;',
  23297. ' },',
  23298. ' set: function (v) {',
  23299. ' this.p = v;',
  23300. ' }',
  23301. ' });',
  23302. ' $mod.THelper.Run();',
  23303. ' $mod.THelper.Fly.call({',
  23304. ' p: this.GetField(),',
  23305. ' get: function () {',
  23306. ' return this.p;',
  23307. ' },',
  23308. ' set: function (v) {',
  23309. ' this.p = v;',
  23310. ' }',
  23311. ' });',
  23312. ' $mod.THelper.Run();',
  23313. ' var $with = this.GetField();',
  23314. ' $mod.THelper.Fly.call({',
  23315. ' get: function () {',
  23316. ' return $with;',
  23317. ' },',
  23318. ' set: function (v) {',
  23319. ' $with = v;',
  23320. ' }',
  23321. ' });',
  23322. ' $mod.THelper.Run();',
  23323. ' return Result;',
  23324. ' };',
  23325. '});',
  23326. 'rtl.createHelper(this, "THelper", null, function () {',
  23327. ' this.Fly = function () {',
  23328. ' };',
  23329. ' this.Run = function () {',
  23330. ' };',
  23331. '});',
  23332. 'this.o = null;',
  23333. '']),
  23334. LinesToStr([ // $mod.$main
  23335. '$mod.THelper.Fly.call({',
  23336. ' p: $mod.o.GetField(),',
  23337. ' get: function () {',
  23338. ' return this.p;',
  23339. ' },',
  23340. ' set: function (v) {',
  23341. ' this.p = v;',
  23342. ' }',
  23343. '});',
  23344. '$mod.THelper.Run();',
  23345. 'var $with = $mod.o;',
  23346. '$mod.THelper.Fly.call({',
  23347. ' p: $with.GetField(),',
  23348. ' get: function () {',
  23349. ' return this.p;',
  23350. ' },',
  23351. ' set: function (v) {',
  23352. ' this.p = v;',
  23353. ' }',
  23354. '});',
  23355. '$mod.THelper.Run();',
  23356. 'var $with1 = $mod.o.GetField();',
  23357. '$mod.THelper.Fly.call({',
  23358. ' get: function () {',
  23359. ' return $with1;',
  23360. ' },',
  23361. ' set: function (v) {',
  23362. ' $with1 = v;',
  23363. ' }',
  23364. '});',
  23365. '$mod.THelper.Run();',
  23366. '']));
  23367. end;
  23368. procedure TTestModule.TestTypeHelper_PassClassPropertyField;
  23369. begin
  23370. StartProgram(false);
  23371. Add([
  23372. '{$modeswitch typehelpers}',
  23373. 'type',
  23374. ' TObject = class',
  23375. ' class var FField: word;',
  23376. ' class procedure SetField(Value: word);',
  23377. ' class property Field: word read FField write SetField;',
  23378. ' end;',
  23379. ' THelper = type helper for word',
  23380. ' procedure Fly(n: byte);',
  23381. ' end;',
  23382. 'class procedure TObject.SetField(Value: word);',
  23383. 'begin',
  23384. ' Field.Fly(1);',
  23385. ' Self.Field.Fly(2);',
  23386. ' with Self do Field.Fly(3);',
  23387. ' with Self.Field do Fly(4);',
  23388. ' TObject.Field.Fly(5);',
  23389. ' with TObject do Field.Fly(6);',
  23390. ' with TObject.Field do Fly(7);',
  23391. 'end;',
  23392. 'procedure THelper.Fly(n: byte);',
  23393. 'begin',
  23394. 'end;',
  23395. 'var',
  23396. ' o: TObject;',
  23397. 'begin',
  23398. ' o.Field.Fly(11);',
  23399. ' with o do Field.Fly(12);',
  23400. ' with o.Field do Fly(13);',
  23401. ' TObject.Field.Fly(14);',
  23402. ' with TObject do Field.Fly(15);',
  23403. ' with TObject.Field do Fly(16);',
  23404. '']);
  23405. ConvertProgram;
  23406. CheckSource('TestTypeHelper_PassClassPropertyField',
  23407. LinesToStr([ // statements
  23408. 'rtl.createClass(this, "TObject", null, function () {',
  23409. ' this.FField = 0;',
  23410. ' this.$init = function () {',
  23411. ' };',
  23412. ' this.$final = function () {',
  23413. ' };',
  23414. ' this.SetField = function (Value) {',
  23415. ' $mod.THelper.Fly.call({',
  23416. ' p: this,',
  23417. ' get: function () {',
  23418. ' return this.p.FField;',
  23419. ' },',
  23420. ' set: function (v) {',
  23421. ' $mod.TObject.FField = v;',
  23422. ' }',
  23423. ' }, 1);',
  23424. ' $mod.THelper.Fly.call({',
  23425. ' p: this,',
  23426. ' get: function () {',
  23427. ' return this.p.FField;',
  23428. ' },',
  23429. ' set: function (v) {',
  23430. ' $mod.TObject.FField = v;',
  23431. ' }',
  23432. ' }, 2);',
  23433. ' $mod.THelper.Fly.call({',
  23434. ' p: this,',
  23435. ' get: function () {',
  23436. ' return this.p.FField;',
  23437. ' },',
  23438. ' set: function (v) {',
  23439. ' $mod.TObject.FField = v;',
  23440. ' }',
  23441. ' }, 3);',
  23442. ' var $with = this.FField;',
  23443. ' $mod.THelper.Fly.call({',
  23444. ' get: function () {',
  23445. ' return $with;',
  23446. ' },',
  23447. ' set: function (v) {',
  23448. ' $with = v;',
  23449. ' }',
  23450. ' }, 4);',
  23451. ' $mod.THelper.Fly.call({',
  23452. ' p: $mod.TObject,',
  23453. ' get: function () {',
  23454. ' return this.p.FField;',
  23455. ' },',
  23456. ' set: function (v) {',
  23457. ' $mod.TObject.FField = v;',
  23458. ' }',
  23459. ' }, 5);',
  23460. ' var $with1 = $mod.TObject;',
  23461. ' $mod.THelper.Fly.call({',
  23462. ' p: $with1,',
  23463. ' get: function () {',
  23464. ' return this.p.FField;',
  23465. ' },',
  23466. ' set: function (v) {',
  23467. ' $mod.TObject.FField = v;',
  23468. ' }',
  23469. ' }, 6);',
  23470. ' var $with2 = $mod.TObject.FField;',
  23471. ' $mod.THelper.Fly.call({',
  23472. ' get: function () {',
  23473. ' return $with2;',
  23474. ' },',
  23475. ' set: function (v) {',
  23476. ' $with2 = v;',
  23477. ' }',
  23478. ' }, 7);',
  23479. ' };',
  23480. '});',
  23481. 'rtl.createHelper(this, "THelper", null, function () {',
  23482. ' this.Fly = function (n) {',
  23483. ' };',
  23484. '});',
  23485. 'this.o = null;',
  23486. '']),
  23487. LinesToStr([ // $mod.$main
  23488. '$mod.THelper.Fly.call({',
  23489. ' p: $mod.o,',
  23490. ' get: function () {',
  23491. ' return this.p.FField;',
  23492. ' },',
  23493. ' set: function (v) {',
  23494. ' $mod.TObject.FField = v;',
  23495. ' }',
  23496. '}, 11);',
  23497. 'var $with = $mod.o;',
  23498. '$mod.THelper.Fly.call({',
  23499. ' p: $with,',
  23500. ' get: function () {',
  23501. ' return this.p.FField;',
  23502. ' },',
  23503. ' set: function (v) {',
  23504. ' $mod.TObject.FField = v;',
  23505. ' }',
  23506. '}, 12);',
  23507. 'var $with1 = $mod.o.FField;',
  23508. '$mod.THelper.Fly.call({',
  23509. ' get: function () {',
  23510. ' return $with1;',
  23511. ' },',
  23512. ' set: function (v) {',
  23513. ' $with1 = v;',
  23514. ' }',
  23515. '}, 13);',
  23516. '$mod.THelper.Fly.call({',
  23517. ' p: $mod.TObject,',
  23518. ' get: function () {',
  23519. ' return this.p.FField;',
  23520. ' },',
  23521. ' set: function (v) {',
  23522. ' $mod.TObject.FField = v;',
  23523. ' }',
  23524. '}, 14);',
  23525. 'var $with2 = $mod.TObject;',
  23526. '$mod.THelper.Fly.call({',
  23527. ' p: $with2,',
  23528. ' get: function () {',
  23529. ' return this.p.FField;',
  23530. ' },',
  23531. ' set: function (v) {',
  23532. ' $mod.TObject.FField = v;',
  23533. ' }',
  23534. '}, 15);',
  23535. 'var $with3 = $mod.TObject.FField;',
  23536. '$mod.THelper.Fly.call({',
  23537. ' get: function () {',
  23538. ' return $with3;',
  23539. ' },',
  23540. ' set: function (v) {',
  23541. ' $with3 = v;',
  23542. ' }',
  23543. '}, 16);',
  23544. '']));
  23545. end;
  23546. procedure TTestModule.TestTypeHelper_PassClassPropertyGetterStatic;
  23547. begin
  23548. StartProgram(false);
  23549. Add([
  23550. '{$modeswitch typehelpers}',
  23551. 'type',
  23552. ' TObject = class',
  23553. ' class var FField: word;',
  23554. ' class function GetField: word; static;',
  23555. ' class property Field: word read GetField write FField;',
  23556. ' end;',
  23557. ' THelper = type helper for word',
  23558. ' procedure Fly(n: byte);',
  23559. ' end;',
  23560. 'class function TObject.GetField: word;',
  23561. 'begin',
  23562. ' Field.Fly(1);',
  23563. ' TObject.Field.Fly(5);',
  23564. ' with TObject do Field.Fly(6);',
  23565. ' with TObject.Field do Fly(7);',
  23566. 'end;',
  23567. 'procedure THelper.Fly(n: byte);',
  23568. 'begin',
  23569. 'end;',
  23570. 'var',
  23571. ' o: TObject;',
  23572. 'begin',
  23573. ' o.Field.Fly(11);',
  23574. ' with o do Field.Fly(12);',
  23575. ' with o.Field do Fly(13);',
  23576. '']);
  23577. ConvertProgram;
  23578. CheckSource('TestTypeHelper_PassClassPropertyGetterStatic',
  23579. LinesToStr([ // statements
  23580. 'rtl.createClass(this, "TObject", null, function () {',
  23581. ' this.FField = 0;',
  23582. ' this.$init = function () {',
  23583. ' };',
  23584. ' this.$final = function () {',
  23585. ' };',
  23586. ' this.GetField = function () {',
  23587. ' var Result = 0;',
  23588. ' $mod.THelper.Fly.call({',
  23589. ' p: $mod.TObject.GetField(),',
  23590. ' get: function () {',
  23591. ' return this.p;',
  23592. ' },',
  23593. ' set: function (v) {',
  23594. ' this.p = v;',
  23595. ' }',
  23596. ' }, 1);',
  23597. ' $mod.THelper.Fly.call({',
  23598. ' p: $mod.TObject.GetField(),',
  23599. ' get: function () {',
  23600. ' return this.p;',
  23601. ' },',
  23602. ' set: function (v) {',
  23603. ' this.p = v;',
  23604. ' }',
  23605. ' }, 5);',
  23606. ' var $with = $mod.TObject;',
  23607. ' $mod.THelper.Fly.call({',
  23608. ' p: $with.GetField(),',
  23609. ' get: function () {',
  23610. ' return this.p;',
  23611. ' },',
  23612. ' set: function (v) {',
  23613. ' this.p = v;',
  23614. ' }',
  23615. ' }, 6);',
  23616. ' var $with1 = $mod.TObject.GetField();',
  23617. ' $mod.THelper.Fly.call({',
  23618. ' get: function () {',
  23619. ' return $with1;',
  23620. ' },',
  23621. ' set: function (v) {',
  23622. ' $with1 = v;',
  23623. ' }',
  23624. ' }, 7);',
  23625. ' return Result;',
  23626. ' };',
  23627. '});',
  23628. 'rtl.createHelper(this, "THelper", null, function () {',
  23629. ' this.Fly = function (n) {',
  23630. ' };',
  23631. '});',
  23632. 'this.o = null;',
  23633. '']),
  23634. LinesToStr([ // $mod.$main
  23635. '$mod.THelper.Fly.call({',
  23636. ' p: $mod.o.GetField(),',
  23637. ' get: function () {',
  23638. ' return this.p;',
  23639. ' },',
  23640. ' set: function (v) {',
  23641. ' this.p = v;',
  23642. ' }',
  23643. '}, 11);',
  23644. 'var $with = $mod.o;',
  23645. '$mod.THelper.Fly.call({',
  23646. ' p: $with.GetField(),',
  23647. ' get: function () {',
  23648. ' return this.p;',
  23649. ' },',
  23650. ' set: function (v) {',
  23651. ' this.p = v;',
  23652. ' }',
  23653. '}, 12);',
  23654. 'var $with1 = $mod.o.GetField();',
  23655. '$mod.THelper.Fly.call({',
  23656. ' get: function () {',
  23657. ' return $with1;',
  23658. ' },',
  23659. ' set: function (v) {',
  23660. ' $with1 = v;',
  23661. ' }',
  23662. '}, 13);',
  23663. '']));
  23664. end;
  23665. procedure TTestModule.TestTypeHelper_PassClassPropertyGetterNonStatic;
  23666. begin
  23667. StartProgram(false);
  23668. Add([
  23669. '{$modeswitch typehelpers}',
  23670. 'type',
  23671. ' TObject = class',
  23672. ' class var FField: word;',
  23673. ' class function GetField: word;',
  23674. ' class property Field: word read GetField write FField;',
  23675. ' end;',
  23676. ' TClass = class of TObject;',
  23677. ' THelper = type helper for word',
  23678. ' procedure Fly(n: byte);',
  23679. ' end;',
  23680. 'class function TObject.GetField: word;',
  23681. 'begin',
  23682. ' Field.Fly(1);',
  23683. ' Self.Field.Fly(5);',
  23684. ' with Self do Field.Fly(6);',
  23685. ' with Self.Field do Fly(7);',
  23686. 'end;',
  23687. 'procedure THelper.Fly(n: byte);',
  23688. 'begin',
  23689. 'end;',
  23690. 'var',
  23691. ' o: TObject;',
  23692. ' c: TClass;',
  23693. 'begin',
  23694. ' o.Field.Fly(11);',
  23695. ' with o do Field.Fly(12);',
  23696. ' with o.Field do Fly(13);',
  23697. ' c.Field.Fly(14);',
  23698. ' with c do Field.Fly(15);',
  23699. ' with c.Field do Fly(16);',
  23700. '']);
  23701. ConvertProgram;
  23702. CheckSource('TestTypeHelper_PassClassPropertyGetterNonStatic',
  23703. LinesToStr([ // statements
  23704. 'rtl.createClass(this, "TObject", null, function () {',
  23705. ' this.FField = 0;',
  23706. ' this.$init = function () {',
  23707. ' };',
  23708. ' this.$final = function () {',
  23709. ' };',
  23710. ' this.GetField = function () {',
  23711. ' var Result = 0;',
  23712. ' $mod.THelper.Fly.call({',
  23713. ' p: this.GetField(),',
  23714. ' get: function () {',
  23715. ' return this.p;',
  23716. ' },',
  23717. ' set: function (v) {',
  23718. ' this.p = v;',
  23719. ' }',
  23720. ' }, 1);',
  23721. ' $mod.THelper.Fly.call({',
  23722. ' p: this.GetField(),',
  23723. ' get: function () {',
  23724. ' return this.p;',
  23725. ' },',
  23726. ' set: function (v) {',
  23727. ' this.p = v;',
  23728. ' }',
  23729. ' }, 5);',
  23730. ' $mod.THelper.Fly.call({',
  23731. ' p: this.GetField(),',
  23732. ' get: function () {',
  23733. ' return this.p;',
  23734. ' },',
  23735. ' set: function (v) {',
  23736. ' this.p = v;',
  23737. ' }',
  23738. ' }, 6);',
  23739. ' var $with = this.GetField();',
  23740. ' $mod.THelper.Fly.call({',
  23741. ' get: function () {',
  23742. ' return $with;',
  23743. ' },',
  23744. ' set: function (v) {',
  23745. ' $with = v;',
  23746. ' }',
  23747. ' }, 7);',
  23748. ' return Result;',
  23749. ' };',
  23750. '});',
  23751. 'rtl.createHelper(this, "THelper", null, function () {',
  23752. ' this.Fly = function (n) {',
  23753. ' };',
  23754. '});',
  23755. 'this.o = null;',
  23756. 'this.c = null;',
  23757. '']),
  23758. LinesToStr([ // $mod.$main
  23759. '$mod.THelper.Fly.call({',
  23760. ' p: $mod.o.$class.GetField(),',
  23761. ' get: function () {',
  23762. ' return this.p;',
  23763. ' },',
  23764. ' set: function (v) {',
  23765. ' this.p = v;',
  23766. ' }',
  23767. '}, 11);',
  23768. 'var $with = $mod.o;',
  23769. '$mod.THelper.Fly.call({',
  23770. ' p: $with.$class.GetField(),',
  23771. ' get: function () {',
  23772. ' return this.p;',
  23773. ' },',
  23774. ' set: function (v) {',
  23775. ' this.p = v;',
  23776. ' }',
  23777. '}, 12);',
  23778. 'var $with1 = $mod.o.$class.GetField();',
  23779. '$mod.THelper.Fly.call({',
  23780. ' get: function () {',
  23781. ' return $with1;',
  23782. ' },',
  23783. ' set: function (v) {',
  23784. ' $with1 = v;',
  23785. ' }',
  23786. '}, 13);',
  23787. '$mod.THelper.Fly.call({',
  23788. ' p: $mod.c.GetField(),',
  23789. ' get: function () {',
  23790. ' return this.p;',
  23791. ' },',
  23792. ' set: function (v) {',
  23793. ' this.p = v;',
  23794. ' }',
  23795. '}, 14);',
  23796. 'var $with2 = $mod.c;',
  23797. '$mod.THelper.Fly.call({',
  23798. ' p: $with2.GetField(),',
  23799. ' get: function () {',
  23800. ' return this.p;',
  23801. ' },',
  23802. ' set: function (v) {',
  23803. ' this.p = v;',
  23804. ' }',
  23805. '}, 15);',
  23806. 'var $with3 = $mod.c.GetField();',
  23807. '$mod.THelper.Fly.call({',
  23808. ' get: function () {',
  23809. ' return $with3;',
  23810. ' },',
  23811. ' set: function (v) {',
  23812. ' $with3 = v;',
  23813. ' }',
  23814. '}, 16);',
  23815. '']));
  23816. end;
  23817. procedure TTestModule.TestTypeHelper_Property;
  23818. begin
  23819. StartProgram(false);
  23820. Add([
  23821. '{$modeswitch typehelpers}',
  23822. 'type',
  23823. ' THelper = type helper for word',
  23824. ' function GetSize: longint;',
  23825. ' procedure SetSize(Value: longint);',
  23826. ' property Size: longint read GetSize write SetSize;',
  23827. ' end;',
  23828. 'function THelper.GetSize: longint;',
  23829. 'begin',
  23830. ' Result:=Size+1;',
  23831. ' Size:=2;',
  23832. ' Result:=Self.Size+3;',
  23833. ' Self.Size:=4;',
  23834. ' with Self do begin',
  23835. ' Result:=Size+5;',
  23836. ' Size:=6;',
  23837. ' end;',
  23838. 'end;',
  23839. 'procedure THelper.SetSize(Value: longint);',
  23840. 'begin',
  23841. 'end;',
  23842. 'var w: word;',
  23843. 'begin',
  23844. ' w:=w.Size+7;',
  23845. ' w.Size:=w+8;',
  23846. ' with w do begin',
  23847. ' w:=Size+9;',
  23848. ' Size:=w+10;',
  23849. ' end;',
  23850. '']);
  23851. ConvertProgram;
  23852. CheckSource('TestTypeHelper_Property',
  23853. LinesToStr([ // statements
  23854. 'rtl.createHelper(this, "THelper", null, function () {',
  23855. ' this.GetSize = function () {',
  23856. ' var Result = 0;',
  23857. ' Result = $mod.THelper.GetSize.call(this) + 1;',
  23858. ' $mod.THelper.SetSize.call(this, 2);',
  23859. ' Result = $mod.THelper.GetSize.call(this) + 3;',
  23860. ' $mod.THelper.SetSize.call(this, 4);',
  23861. ' var $with = this.get();',
  23862. ' Result = $mod.THelper.GetSize.call(this) + 5;',
  23863. ' $mod.THelper.SetSize.call(this, 6);',
  23864. ' return Result;',
  23865. ' };',
  23866. ' this.SetSize = function (Value) {',
  23867. ' };',
  23868. '});',
  23869. 'this.w = 0;',
  23870. '']),
  23871. LinesToStr([ // $mod.$main
  23872. '$mod.w = $mod.THelper.GetSize.call({',
  23873. ' p: $mod,',
  23874. ' get: function () {',
  23875. ' return this.p.w;',
  23876. ' },',
  23877. ' set: function (v) {',
  23878. ' this.p.w = v;',
  23879. ' }',
  23880. '}) + 7;',
  23881. '$mod.THelper.SetSize.call({',
  23882. ' p: $mod,',
  23883. ' get: function () {',
  23884. ' return this.p.w;',
  23885. ' },',
  23886. ' set: function (v) {',
  23887. ' this.p.w = v;',
  23888. ' }',
  23889. '}, $mod.w + 8);',
  23890. 'var $with = $mod.w;',
  23891. '$mod.w = $mod.THelper.GetSize.call({',
  23892. ' get: function () {',
  23893. ' return $with;',
  23894. ' },',
  23895. ' set: function (v) {',
  23896. ' $with = v;',
  23897. ' }',
  23898. '}) + 9;',
  23899. '$mod.THelper.SetSize.call({',
  23900. ' get: function () {',
  23901. ' return $with;',
  23902. ' },',
  23903. ' set: function (v) {',
  23904. ' $with = v;',
  23905. ' }',
  23906. '}, $mod.w + 10);',
  23907. '']));
  23908. end;
  23909. procedure TTestModule.TestTypeHelper_Property_Array;
  23910. begin
  23911. StartProgram(false);
  23912. Add([
  23913. '{$modeswitch typehelpers}',
  23914. 'type',
  23915. ' THelper = type helper for word',
  23916. ' function GetItems(Index: byte): boolean;',
  23917. ' procedure SetItems(Index: byte; Value: boolean);',
  23918. ' property Items[Index: byte]: boolean read GetItems write SetItems;',
  23919. ' end;',
  23920. 'function THelper.GetItems(Index: byte): boolean;',
  23921. 'begin',
  23922. ' Result:=Items[1];',
  23923. ' Items[2]:=false;',
  23924. ' Result:=Self.Items[3];',
  23925. ' Self.Items[4]:=true;',
  23926. ' with Self do begin',
  23927. ' Result:=Items[5];',
  23928. ' Items[6]:=false;',
  23929. ' end;',
  23930. 'end;',
  23931. 'procedure THelper.SetItems(Index: byte; Value: boolean);',
  23932. 'begin',
  23933. 'end;',
  23934. 'var',
  23935. ' w: word;',
  23936. ' b: boolean;',
  23937. 'begin',
  23938. ' b:=w.Items[1];',
  23939. ' w.Items[2]:=b;',
  23940. ' with w do begin',
  23941. ' b:=Items[3];',
  23942. ' Items[4]:=b;',
  23943. ' end;',
  23944. '']);
  23945. ConvertProgram;
  23946. CheckSource('TestTypeHelper_Property_Array',
  23947. LinesToStr([ // statements
  23948. 'rtl.createHelper(this, "THelper", null, function () {',
  23949. ' this.GetItems = function (Index) {',
  23950. ' var Result = false;',
  23951. ' Result = $mod.THelper.GetItems.call(this, 1);',
  23952. ' $mod.THelper.SetItems.call(this, 2, false);',
  23953. ' Result = $mod.THelper.GetItems.call(this, 3);',
  23954. ' $mod.THelper.SetItems.call(this, 4, true);',
  23955. ' var $with = this.get();',
  23956. ' Result = $mod.THelper.GetItems.call(this, 5);',
  23957. ' $mod.THelper.SetItems.call(this, 6, false);',
  23958. ' return Result;',
  23959. ' };',
  23960. ' this.SetItems = function (Index, Value) {',
  23961. ' };',
  23962. '});',
  23963. 'this.w = 0;',
  23964. 'this.b = false;',
  23965. '']),
  23966. LinesToStr([ // $mod.$main
  23967. '$mod.b = $mod.THelper.GetItems.call({',
  23968. ' p: $mod,',
  23969. ' get: function () {',
  23970. ' return this.p.w;',
  23971. ' },',
  23972. ' set: function (v) {',
  23973. ' this.p.w = v;',
  23974. ' }',
  23975. '}, 1);',
  23976. '$mod.THelper.SetItems.call({',
  23977. ' p: $mod,',
  23978. ' get: function () {',
  23979. ' return this.p.w;',
  23980. ' },',
  23981. ' set: function (v) {',
  23982. ' this.p.w = v;',
  23983. ' }',
  23984. '}, 2, $mod.b);',
  23985. 'var $with = $mod.w;',
  23986. '$mod.b = $mod.THelper.GetItems.call({',
  23987. ' get: function () {',
  23988. ' return $with;',
  23989. ' },',
  23990. ' set: function (v) {',
  23991. ' $with = v;',
  23992. ' }',
  23993. '}, 3);',
  23994. '$mod.THelper.SetItems.call({',
  23995. ' get: function () {',
  23996. ' return $with;',
  23997. ' },',
  23998. ' set: function (v) {',
  23999. ' $with = v;',
  24000. ' }',
  24001. '}, 4, $mod.b);',
  24002. '']));
  24003. end;
  24004. procedure TTestModule.TestTypeHelper_ClassProperty;
  24005. begin
  24006. StartProgram(false);
  24007. Add([
  24008. '{$modeswitch typehelpers}',
  24009. 'type',
  24010. ' THelper = type helper for word',
  24011. ' class function GetSize: longint; static;',
  24012. ' class procedure SetSize(Value: longint); static;',
  24013. ' class property Size: longint read GetSize write SetSize;',
  24014. ' end;',
  24015. 'class function THelper.GetSize: longint;',
  24016. 'begin',
  24017. ' Result:=Size+1;',
  24018. ' Size:=2;',
  24019. 'end;',
  24020. 'class procedure THelper.SetSize(Value: longint);',
  24021. 'begin',
  24022. 'end;',
  24023. 'begin',
  24024. '']);
  24025. ConvertProgram;
  24026. CheckSource('TestTypeHelper_ClassProperty',
  24027. LinesToStr([ // statements
  24028. 'rtl.createHelper(this, "THelper", null, function () {',
  24029. ' this.GetSize = function () {',
  24030. ' var Result = 0;',
  24031. ' Result = $mod.THelper.GetSize() + 1;',
  24032. ' $mod.THelper.SetSize(2);',
  24033. ' return Result;',
  24034. ' };',
  24035. ' this.SetSize = function (Value) {',
  24036. ' };',
  24037. '});',
  24038. '']),
  24039. LinesToStr([ // $mod.$main
  24040. '']));
  24041. end;
  24042. procedure TTestModule.TestTypeHelper_ClassProperty_Array;
  24043. begin
  24044. StartProgram(false);
  24045. Add([
  24046. '{$modeswitch typehelpers}',
  24047. 'type',
  24048. ' THelper = type helper for word',
  24049. ' class function GetItems(Index: byte): boolean; static;',
  24050. ' class procedure SetItems(Index: byte; Value: boolean); static;',
  24051. ' class property Items[Index: byte]: boolean read GetItems write SetItems;',
  24052. ' end;',
  24053. 'class function THelper.GetItems(Index: byte): boolean;',
  24054. 'begin',
  24055. ' Result:=Items[1];',
  24056. ' Items[2]:=false;',
  24057. 'end;',
  24058. 'class procedure THelper.SetItems(Index: byte; Value: boolean);',
  24059. 'begin',
  24060. 'end;',
  24061. 'var',
  24062. ' w: word;',
  24063. ' b: boolean;',
  24064. 'begin',
  24065. ' b:=w.Items[1];',
  24066. ' w.Items[2]:=b;',
  24067. ' with w do begin',
  24068. ' b:=Items[3];',
  24069. ' Items[4]:=b;',
  24070. ' end;',
  24071. '']);
  24072. ConvertProgram;
  24073. CheckSource('TestTypeHelper_ClassProperty_Array',
  24074. LinesToStr([ // statements
  24075. 'rtl.createHelper(this, "THelper", null, function () {',
  24076. ' this.GetItems = function (Index) {',
  24077. ' var Result = false;',
  24078. ' Result = $mod.THelper.GetItems(1);',
  24079. ' $mod.THelper.SetItems(2, false);',
  24080. ' return Result;',
  24081. ' };',
  24082. ' this.SetItems = function (Index, Value) {',
  24083. ' };',
  24084. '});',
  24085. 'this.w = 0;',
  24086. 'this.b = false;',
  24087. '']),
  24088. LinesToStr([ // $mod.$main
  24089. '$mod.b = $mod.THelper.GetItems(1);',
  24090. '$mod.THelper.SetItems(2, $mod.b);',
  24091. 'var $with = $mod.w;',
  24092. '$mod.b = $mod.THelper.GetItems(3);',
  24093. '$mod.THelper.SetItems(4, $mod.b);',
  24094. '']));
  24095. end;
  24096. procedure TTestModule.TestTypeHelper_ClassMethod;
  24097. begin
  24098. StartProgram(false);
  24099. Add([
  24100. '{$modeswitch typehelpers}',
  24101. 'type',
  24102. ' THelper = type helper for word',
  24103. ' class procedure DoStatic; static;',
  24104. ' end;',
  24105. 'class procedure THelper.DoStatic;',
  24106. 'begin',
  24107. ' DoStatic;',
  24108. ' DoStatic();',
  24109. 'end;',
  24110. 'var w: word;',
  24111. 'begin',
  24112. ' w.DoStatic;',
  24113. ' w.DoStatic();',
  24114. '']);
  24115. ConvertProgram;
  24116. CheckSource('TestTypeHelper_ClassMethod',
  24117. LinesToStr([ // statements
  24118. 'rtl.createHelper(this, "THelper", null, function () {',
  24119. ' this.DoStatic = function () {',
  24120. ' $mod.THelper.DoStatic();',
  24121. ' $mod.THelper.DoStatic();',
  24122. ' };',
  24123. '});',
  24124. 'this.w = 0;',
  24125. '']),
  24126. LinesToStr([ // $mod.$main
  24127. '$mod.THelper.DoStatic();',
  24128. '$mod.THelper.DoStatic();',
  24129. '']));
  24130. end;
  24131. procedure TTestModule.TestTypeHelper_ExtClassMethodFail;
  24132. begin
  24133. StartProgram(false);
  24134. Add([
  24135. '{$modeswitch typehelpers}',
  24136. 'type',
  24137. ' THelper = type helper for word',
  24138. ' procedure Run; external name ''Run'';',
  24139. ' end;',
  24140. 'var w: word;',
  24141. 'begin',
  24142. ' w.Run;',
  24143. '']);
  24144. SetExpectedPasResolverError('Not supported: external method in type helper',nNotSupportedX);
  24145. ConvertProgram;
  24146. end;
  24147. procedure TTestModule.TestTypeHelper_Constructor;
  24148. begin
  24149. StartProgram(false);
  24150. Add([
  24151. '{$modeswitch typehelpers}',
  24152. 'type',
  24153. ' THelper = type helper for word',
  24154. ' constructor Init(e: longint);',
  24155. ' end;',
  24156. 'constructor THelper.Init(e: longint);',
  24157. 'begin',
  24158. ' Self:=e;',
  24159. ' Init(e+1);',
  24160. 'end;',
  24161. 'var w: word;',
  24162. 'begin',
  24163. ' w:=word.Init(2);',
  24164. ' w:=w.Init(3);',
  24165. ' with word do w:=Init(4);',
  24166. ' with w do w:=Init(5);',
  24167. '']);
  24168. ConvertProgram;
  24169. CheckSource('TestTypeHelper_Constructor',
  24170. LinesToStr([ // statements
  24171. 'rtl.createHelper(this, "THelper", null, function () {',
  24172. ' this.Init = function (e) {',
  24173. ' this.set(e);',
  24174. ' $mod.THelper.Init.call(this, e + 1);',
  24175. ' return this.get();',
  24176. ' };',
  24177. ' this.$new = function (fn, args) {',
  24178. ' return this[fn].apply({',
  24179. ' p: 0,',
  24180. ' get: function () {',
  24181. ' return this.p;',
  24182. ' },',
  24183. ' set: function (v) {',
  24184. ' this.p = v;',
  24185. ' }',
  24186. ' }, args);',
  24187. ' };',
  24188. '});',
  24189. 'this.w = 0;',
  24190. '']),
  24191. LinesToStr([ // $mod.$main
  24192. '$mod.w = $mod.THelper.$new("Init", [2]);',
  24193. '$mod.w = $mod.THelper.Init.call({',
  24194. ' p: $mod,',
  24195. ' get: function () {',
  24196. ' return this.p.w;',
  24197. ' },',
  24198. ' set: function (v) {',
  24199. ' this.p.w = v;',
  24200. ' }',
  24201. '}, 3);',
  24202. '$mod.w = $mod.THelper.$new("Init", [4]);',
  24203. 'var $with = $mod.w;',
  24204. '$mod.w = $mod.THelper.Init.call({',
  24205. ' get: function () {',
  24206. ' return $with;',
  24207. ' },',
  24208. ' set: function (v) {',
  24209. ' $with = v;',
  24210. ' }',
  24211. '}, 5);',
  24212. '']));
  24213. end;
  24214. procedure TTestModule.TestTypeHelper_Word;
  24215. begin
  24216. StartProgram(false);
  24217. Add([
  24218. '{$modeswitch typehelpers}',
  24219. 'type',
  24220. ' THelper = type helper for word',
  24221. ' procedure DoIt(e: byte = 123);',
  24222. ' end;',
  24223. 'procedure THelper.DoIt(e: byte);',
  24224. 'begin',
  24225. ' Self:=e;',
  24226. ' Self:=Self+1;',
  24227. ' with Self do Doit;',
  24228. 'end;',
  24229. 'begin',
  24230. ' word(3).DoIt;',
  24231. '']);
  24232. ConvertProgram;
  24233. CheckSource('TestTypeHelper_Word',
  24234. LinesToStr([ // statements
  24235. 'rtl.createHelper(this, "THelper", null, function () {',
  24236. ' this.DoIt = function (e) {',
  24237. ' this.set(e);',
  24238. ' this.set(this.get() + 1);',
  24239. ' var $with = this.get();',
  24240. ' $mod.THelper.DoIt.call(this, 123);',
  24241. ' };',
  24242. '});',
  24243. '']),
  24244. LinesToStr([ // $mod.$main
  24245. '$mod.THelper.DoIt.call({',
  24246. ' get: function () {',
  24247. ' return 3;',
  24248. ' },',
  24249. ' set: function (v) {',
  24250. ' rtl.raiseE("EPropReadOnly");',
  24251. ' }',
  24252. '}, 123);',
  24253. '']));
  24254. end;
  24255. procedure TTestModule.TestTypeHelper_Boolean;
  24256. begin
  24257. StartProgram(false);
  24258. Add([
  24259. '{$modeswitch typehelpers}',
  24260. 'type',
  24261. ' Integer = longint;',
  24262. ' THelper = type helper for boolean',
  24263. ' procedure Run(e: wordbool = true);',
  24264. ' end;',
  24265. 'procedure THelper.Run(e: wordbool);',
  24266. 'begin',
  24267. ' Self:=e;',
  24268. ' Self:=not Self;',
  24269. ' with Self do Run;',
  24270. ' if Integer(Self)=0 then ;',
  24271. 'end;',
  24272. 'begin',
  24273. ' boolean(3).Run;',
  24274. '']);
  24275. ConvertProgram;
  24276. CheckSource('TestTypeHelper_Boolean',
  24277. LinesToStr([ // statements
  24278. 'rtl.createHelper(this, "THelper", null, function () {',
  24279. ' this.Run = function (e) {',
  24280. ' this.set(e);',
  24281. ' this.set(!this.get());',
  24282. ' var $with = this.get();',
  24283. ' $mod.THelper.Run.call(this, true);',
  24284. ' if ((this.get() ? 1 : 0) === 0) ;',
  24285. ' };',
  24286. '});',
  24287. '']),
  24288. LinesToStr([ // $mod.$main
  24289. '$mod.THelper.Run.call({',
  24290. ' a: 3 != 0,',
  24291. ' get: function () {',
  24292. ' return this.a;',
  24293. ' },',
  24294. ' set: function (v) {',
  24295. ' rtl.raiseE("EPropReadOnly");',
  24296. ' }',
  24297. '}, true);',
  24298. '']));
  24299. end;
  24300. procedure TTestModule.TestTypeHelper_WordBool;
  24301. begin
  24302. StartProgram(false);
  24303. Add([
  24304. '{$modeswitch typehelpers}',
  24305. 'type',
  24306. ' Integer = longint;',
  24307. ' THelper = type helper for WordBool',
  24308. ' procedure Run(e: wordbool = true);',
  24309. ' end;',
  24310. 'procedure THelper.Run(e: wordbool);',
  24311. 'var i: integer;',
  24312. 'begin',
  24313. ' i:=Integer(Self);',
  24314. 'end;',
  24315. 'var w: wordbool;',
  24316. 'begin',
  24317. ' w.Run;',
  24318. ' wordbool(3).Run;',
  24319. '']);
  24320. ConvertProgram;
  24321. CheckSource('TestTypeHelper_WordBool',
  24322. LinesToStr([ // statements
  24323. 'rtl.createHelper(this, "THelper", null, function () {',
  24324. ' this.Run = function (e) {',
  24325. ' var i = 0;',
  24326. ' i = (this.get() ? 1 : 0);',
  24327. ' };',
  24328. '});',
  24329. 'this.w = false;',
  24330. '']),
  24331. LinesToStr([ // $mod.$main
  24332. '$mod.THelper.Run.call({',
  24333. ' p: $mod,',
  24334. ' get: function () {',
  24335. ' return this.p.w;',
  24336. ' },',
  24337. ' set: function (v) {',
  24338. ' this.p.w = v;',
  24339. ' }',
  24340. '}, true);',
  24341. '$mod.THelper.Run.call({',
  24342. ' a: 3 != 0,',
  24343. ' get: function () {',
  24344. ' return this.a;',
  24345. ' },',
  24346. ' set: function (v) {',
  24347. ' rtl.raiseE("EPropReadOnly");',
  24348. ' }',
  24349. '}, true);',
  24350. '']));
  24351. end;
  24352. procedure TTestModule.TestTypeHelper_Double;
  24353. begin
  24354. StartProgram(false);
  24355. Add([
  24356. '{$modeswitch typehelpers}',
  24357. 'type',
  24358. ' Float = type double;',
  24359. ' THelper = type helper for Float',
  24360. ' const NPI = 3.141592;',
  24361. ' function ToStr: String;',
  24362. ' end;',
  24363. 'function THelper.ToStr: String;',
  24364. 'begin',
  24365. 'end;',
  24366. 'procedure DoIt(s: string);',
  24367. 'begin',
  24368. 'end;',
  24369. 'var f: Float;',
  24370. 'begin',
  24371. ' DoIt(f.toStr);',
  24372. ' DoIt(f.toStr());',
  24373. ' (f*f).toStr;',
  24374. ' DoIt((f*f).toStr);',
  24375. '']);
  24376. ConvertProgram;
  24377. CheckSource('TestTypeHelper_Double',
  24378. LinesToStr([ // statements
  24379. 'rtl.createHelper(this, "THelper", null, function () {',
  24380. ' this.NPI = 3.141592;',
  24381. ' this.ToStr = function () {',
  24382. ' var Result = "";',
  24383. ' return Result;',
  24384. ' };',
  24385. '});',
  24386. 'this.DoIt = function (s) {',
  24387. '};',
  24388. 'this.f = 0.0;',
  24389. '']),
  24390. LinesToStr([ // $mod.$main
  24391. '$mod.DoIt($mod.THelper.ToStr.call({',
  24392. ' p: $mod,',
  24393. ' get: function () {',
  24394. ' return this.p.f;',
  24395. ' },',
  24396. ' set: function (v) {',
  24397. ' this.p.f = v;',
  24398. ' }',
  24399. '}));',
  24400. '$mod.DoIt($mod.THelper.ToStr.call({',
  24401. ' p: $mod,',
  24402. ' get: function () {',
  24403. ' return this.p.f;',
  24404. ' },',
  24405. ' set: function (v) {',
  24406. ' this.p.f = v;',
  24407. ' }',
  24408. '}));',
  24409. '$mod.THelper.ToStr.call({',
  24410. ' a: $mod.f * $mod.f,',
  24411. ' get: function () {',
  24412. ' return this.a;',
  24413. ' },',
  24414. ' set: function (v) {',
  24415. ' rtl.raiseE("EPropReadOnly");',
  24416. ' }',
  24417. '});',
  24418. '$mod.DoIt($mod.THelper.ToStr.call({',
  24419. ' a: $mod.f * $mod.f,',
  24420. ' get: function () {',
  24421. ' return this.a;',
  24422. ' },',
  24423. ' set: function (v) {',
  24424. ' rtl.raiseE("EPropReadOnly");',
  24425. ' }',
  24426. '}));',
  24427. '']));
  24428. end;
  24429. procedure TTestModule.TestTypeHelper_NativeInt;
  24430. begin
  24431. StartProgram(false);
  24432. Add([
  24433. '{$modeswitch typehelpers}',
  24434. 'type',
  24435. ' MaxInt = type nativeint;',
  24436. ' THelperI = type helper for MaxInt',
  24437. ' function ToStr: String;',
  24438. ' end;',
  24439. ' MaxUInt = type nativeuint;',
  24440. ' THelperU = type helper for MaxUInt',
  24441. ' function ToStr: String;',
  24442. ' end;',
  24443. 'function THelperI.ToStr: String;',
  24444. 'begin',
  24445. ' Result:=str(Self);',
  24446. 'end;',
  24447. 'function THelperU.ToStr: String;',
  24448. 'begin',
  24449. ' Result:=str(Self);',
  24450. 'end;',
  24451. 'procedure DoIt(s: string);',
  24452. 'begin',
  24453. 'end;',
  24454. 'var i: MaxInt;',
  24455. 'begin',
  24456. ' DoIt(i.toStr);',
  24457. ' DoIt(i.toStr());',
  24458. ' (i*i).toStr;',
  24459. ' DoIt((i*i).toStr);',
  24460. '']);
  24461. ConvertProgram;
  24462. CheckSource('TestTypeHelper_NativeInt',
  24463. LinesToStr([ // statements
  24464. 'rtl.createHelper(this, "THelperI", null, function () {',
  24465. ' this.ToStr = function () {',
  24466. ' var Result = "";',
  24467. ' Result = "" + this.get();',
  24468. ' return Result;',
  24469. ' };',
  24470. '});',
  24471. 'rtl.createHelper(this, "THelperU", null, function () {',
  24472. ' this.ToStr = function () {',
  24473. ' var Result = "";',
  24474. ' Result = "" + this.get();',
  24475. ' return Result;',
  24476. ' };',
  24477. '});',
  24478. 'this.DoIt = function (s) {',
  24479. '};',
  24480. 'this.i = 0;',
  24481. '']),
  24482. LinesToStr([ // $mod.$main
  24483. '$mod.DoIt($mod.THelperI.ToStr.call({',
  24484. ' p: $mod,',
  24485. ' get: function () {',
  24486. ' return this.p.i;',
  24487. ' },',
  24488. ' set: function (v) {',
  24489. ' this.p.i = v;',
  24490. ' }',
  24491. '}));',
  24492. '$mod.DoIt($mod.THelperI.ToStr.call({',
  24493. ' p: $mod,',
  24494. ' get: function () {',
  24495. ' return this.p.i;',
  24496. ' },',
  24497. ' set: function (v) {',
  24498. ' this.p.i = v;',
  24499. ' }',
  24500. '}));',
  24501. '$mod.THelperI.ToStr.call({',
  24502. ' a: $mod.i * $mod.i,',
  24503. ' get: function () {',
  24504. ' return this.a;',
  24505. ' },',
  24506. ' set: function (v) {',
  24507. ' rtl.raiseE("EPropReadOnly");',
  24508. ' }',
  24509. '});',
  24510. '$mod.DoIt($mod.THelperI.ToStr.call({',
  24511. ' a: $mod.i * $mod.i,',
  24512. ' get: function () {',
  24513. ' return this.a;',
  24514. ' },',
  24515. ' set: function (v) {',
  24516. ' rtl.raiseE("EPropReadOnly");',
  24517. ' }',
  24518. '}));',
  24519. '']));
  24520. end;
  24521. procedure TTestModule.TestTypeHelper_StringChar;
  24522. begin
  24523. StartProgram(false);
  24524. Add([
  24525. '{$modeswitch typehelpers}',
  24526. 'type',
  24527. ' TStringHelper = type helper for string',
  24528. ' procedure DoIt(e: byte = 123);',
  24529. ' end;',
  24530. ' TCharHelper = type helper for char',
  24531. ' procedure Fly;',
  24532. ' end;',
  24533. 'procedure TStringHelper.DoIt(e: byte);',
  24534. 'begin',
  24535. ' Self[1]:=''c'';',
  24536. ' Self[2]:=Self[3];',
  24537. 'end;',
  24538. 'procedure TCharHelper.Fly;',
  24539. 'begin',
  24540. ' Self:=''c'';',
  24541. 'end;',
  24542. 'begin',
  24543. ' ''abc''.DoIt;',
  24544. ' ''xyz''.DoIt();',
  24545. ' ''c''.Fly();',
  24546. '']);
  24547. ConvertProgram;
  24548. CheckSource('TestTypeHelper_StringChar',
  24549. LinesToStr([ // statements
  24550. 'rtl.createHelper(this, "TStringHelper", null, function () {',
  24551. ' this.DoIt = function (e) {',
  24552. ' this.set(rtl.setCharAt(this.get(), 0, "c"));',
  24553. ' this.set(rtl.setCharAt(this.get(), 1, this.get().charAt(2)));',
  24554. ' };',
  24555. '});',
  24556. 'rtl.createHelper(this, "TCharHelper", null, function () {',
  24557. ' this.Fly = function () {',
  24558. ' this.set("c");',
  24559. ' };',
  24560. '});',
  24561. '']),
  24562. LinesToStr([ // $mod.$main
  24563. '$mod.TStringHelper.DoIt.call({',
  24564. ' get: function () {',
  24565. ' return "abc";',
  24566. ' },',
  24567. ' set: function (v) {',
  24568. ' rtl.raiseE("EPropReadOnly");',
  24569. ' }',
  24570. '}, 123);',
  24571. '$mod.TStringHelper.DoIt.call({',
  24572. ' get: function () {',
  24573. ' return "xyz";',
  24574. ' },',
  24575. ' set: function (v) {',
  24576. ' rtl.raiseE("EPropReadOnly");',
  24577. ' }',
  24578. '}, 123);',
  24579. '$mod.TCharHelper.Fly.call({',
  24580. ' get: function () {',
  24581. ' return "c";',
  24582. ' },',
  24583. ' set: function (v) {',
  24584. ' rtl.raiseE("EPropReadOnly");',
  24585. ' }',
  24586. '});',
  24587. '']));
  24588. end;
  24589. procedure TTestModule.TestTypeHelper_JSValue;
  24590. begin
  24591. StartProgram(false);
  24592. Add([
  24593. '{$modeswitch typehelpers}',
  24594. 'type',
  24595. ' TExtValue = type jsvalue;',
  24596. ' THelper = type helper for TExtValue',
  24597. ' function ToStr: String;',
  24598. ' end;',
  24599. 'function THelper.ToStr: String;',
  24600. 'begin',
  24601. 'end;',
  24602. 'var',
  24603. ' s: string;',
  24604. ' v: TExtValue;',
  24605. 'begin',
  24606. ' s:=v.toStr;',
  24607. ' s:=v.toStr();',
  24608. ' TExtValue(s).toStr;',
  24609. '']);
  24610. ConvertProgram;
  24611. CheckSource('TestTypeHelper_JSValue',
  24612. LinesToStr([ // statements
  24613. 'rtl.createHelper(this, "THelper", null, function () {',
  24614. ' this.ToStr = function () {',
  24615. ' var Result = "";',
  24616. ' return Result;',
  24617. ' };',
  24618. '});',
  24619. 'this.s = "";',
  24620. 'this.v = undefined;',
  24621. '']),
  24622. LinesToStr([ // $mod.$main
  24623. '$mod.s = $mod.THelper.ToStr.call({',
  24624. ' p: $mod,',
  24625. ' get: function () {',
  24626. ' return this.p.v;',
  24627. ' },',
  24628. ' set: function (v) {',
  24629. ' this.p.v = v;',
  24630. ' }',
  24631. '});',
  24632. '$mod.s = $mod.THelper.ToStr.call({',
  24633. ' p: $mod,',
  24634. ' get: function () {',
  24635. ' return this.p.v;',
  24636. ' },',
  24637. ' set: function (v) {',
  24638. ' this.p.v = v;',
  24639. ' }',
  24640. '});',
  24641. '$mod.THelper.ToStr.call({',
  24642. ' p: $mod,',
  24643. ' get: function () {',
  24644. ' return this.p.s;',
  24645. ' },',
  24646. ' set: function (v) {',
  24647. ' rtl.raiseE("EPropReadOnly");',
  24648. ' }',
  24649. '});',
  24650. '']));
  24651. end;
  24652. procedure TTestModule.TestTypeHelper_Array;
  24653. begin
  24654. StartProgram(false);
  24655. Add([
  24656. '{$modeswitch typehelpers}',
  24657. 'type',
  24658. ' TArrOfBool = array of boolean;',
  24659. ' TArrOfJS = array of jsvalue;',
  24660. ' THelper = type helper for TArrOfBool',
  24661. ' procedure DoIt(e: byte = 123);',
  24662. ' end;',
  24663. 'procedure THelper.DoIt(e: byte);',
  24664. 'begin',
  24665. ' Self[1]:=true;',
  24666. ' Self[2]:=not Self[3];',
  24667. ' SetLength(Self,4);',
  24668. 'end;',
  24669. 'var',
  24670. ' b: TArrOfBool;',
  24671. ' j: TArrOfJS;',
  24672. 'begin',
  24673. ' b.DoIt;',
  24674. ' TArrOfBool(j).DoIt();',
  24675. '']);
  24676. ConvertProgram;
  24677. CheckSource('TestTypeHelper_Array',
  24678. LinesToStr([ // statements
  24679. 'rtl.createHelper(this, "THelper", null, function () {',
  24680. ' this.DoIt = function (e) {',
  24681. ' this.get()[1] = true;',
  24682. ' this.get()[2] = !this.get()[3];',
  24683. ' this.set(rtl.arraySetLength(this.get(), false, 4));',
  24684. ' };',
  24685. '});',
  24686. 'this.b = [];',
  24687. 'this.j = [];',
  24688. '']),
  24689. LinesToStr([ // $mod.$main
  24690. '$mod.THelper.DoIt.call({',
  24691. ' p: $mod,',
  24692. ' get: function () {',
  24693. ' return this.p.b;',
  24694. ' },',
  24695. ' set: function (v) {',
  24696. ' this.p.b = v;',
  24697. ' }',
  24698. '}, 123);',
  24699. '$mod.THelper.DoIt.call({',
  24700. ' p: $mod,',
  24701. ' get: function () {',
  24702. ' return this.p.j;',
  24703. ' },',
  24704. ' set: function (v) {',
  24705. ' this.p.j = v;',
  24706. ' }',
  24707. '}, 123);',
  24708. '']));
  24709. end;
  24710. procedure TTestModule.TestTypeHelper_EnumType;
  24711. begin
  24712. StartProgram(false);
  24713. Add([
  24714. '{$modeswitch typehelpers}',
  24715. 'type',
  24716. ' TEnum = (red,blue);',
  24717. ' THelper = type helper for TEnum',
  24718. ' procedure DoIt(e: byte = 123);',
  24719. ' class procedure Swing(w: word); static;',
  24720. ' end;',
  24721. 'procedure THelper.DoIt(e: byte);',
  24722. 'begin',
  24723. ' Self:=red;',
  24724. ' Self:=succ(Self);',
  24725. ' with Self do Doit;',
  24726. 'end;',
  24727. 'class procedure THelper.Swing(w: word);',
  24728. 'begin',
  24729. 'end;',
  24730. 'var e: TEnum;',
  24731. 'begin',
  24732. ' e.DoIt;',
  24733. ' red.DoIt;',
  24734. ' TEnum.blue.DoIt;',
  24735. ' TEnum(1).DoIt;',
  24736. ' TEnum.Swing(3);',
  24737. '']);
  24738. ConvertProgram;
  24739. CheckSource('TestTypeHelper_EnumType',
  24740. LinesToStr([ // statements
  24741. 'this.TEnum = {',
  24742. ' "0": "red",',
  24743. ' red: 0,',
  24744. ' "1": "blue",',
  24745. ' blue: 1',
  24746. '};',
  24747. 'rtl.createHelper(this, "THelper", null, function () {',
  24748. ' this.DoIt = function (e) {',
  24749. ' this.set($mod.TEnum.red);',
  24750. ' this.set(this.get() + 1);',
  24751. ' var $with = this.get();',
  24752. ' $mod.THelper.DoIt.call(this, 123);',
  24753. ' };',
  24754. ' this.Swing = function (w) {',
  24755. ' };',
  24756. '});',
  24757. 'this.e = 0;',
  24758. '']),
  24759. LinesToStr([ // $mod.$main
  24760. '$mod.THelper.DoIt.call({',
  24761. ' p: $mod,',
  24762. ' get: function () {',
  24763. ' return this.p.e;',
  24764. ' },',
  24765. ' set: function (v) {',
  24766. ' this.p.e = v;',
  24767. ' }',
  24768. '}, 123);',
  24769. '$mod.THelper.DoIt.call({',
  24770. ' p: $mod.TEnum,',
  24771. ' get: function () {',
  24772. ' return this.p.red;',
  24773. ' },',
  24774. ' set: function (v) {',
  24775. ' rtl.raiseE("EPropReadOnly");',
  24776. ' }',
  24777. '}, 123);',
  24778. '$mod.THelper.DoIt.call({',
  24779. ' p: $mod.TEnum,',
  24780. ' get: function () {',
  24781. ' return this.p.blue;',
  24782. ' },',
  24783. ' set: function (v) {',
  24784. ' rtl.raiseE("EPropReadOnly");',
  24785. ' }',
  24786. '}, 123);',
  24787. '$mod.THelper.DoIt.call({',
  24788. ' get: function () {',
  24789. ' return 1;',
  24790. ' },',
  24791. ' set: function (v) {',
  24792. ' rtl.raiseE("EPropReadOnly");',
  24793. ' }',
  24794. '}, 123);',
  24795. '$mod.THelper.Swing(3);',
  24796. '']));
  24797. end;
  24798. procedure TTestModule.TestTypeHelper_SetType;
  24799. begin
  24800. StartProgram(false);
  24801. Add([
  24802. '{$modeswitch typehelpers}',
  24803. 'type',
  24804. ' TEnum = (red,blue);',
  24805. ' TSetOfEnum = set of TEnum;',
  24806. ' THelper = type helper for TSetOfEnum',
  24807. ' procedure DoIt(e: byte = 123);',
  24808. ' constructor Init(e: TEnum);',
  24809. ' constructor InitEmpty;',
  24810. ' end;',
  24811. 'procedure THelper.DoIt(e: byte);',
  24812. 'begin',
  24813. ' Self:=[];',
  24814. ' Self:=[red];',
  24815. ' Include(Self,blue);',
  24816. 'end;',
  24817. 'constructor THelper.Init(e: TEnum);',
  24818. 'begin',
  24819. ' Self:=[];',
  24820. ' Self:=[e];',
  24821. ' Include(Self,blue);',
  24822. 'end;',
  24823. 'constructor THelper.InitEmpty;',
  24824. 'begin',
  24825. 'end;',
  24826. 'var s: TSetOfEnum;',
  24827. 'begin',
  24828. ' s.DoIt;',
  24829. //' [red].DoIt;',
  24830. //' with s do DoIt;',
  24831. //' with [red,blue] do DoIt;',
  24832. ' s:=TSetOfEnum.Init(blue);',
  24833. ' s:=s.Init(blue);',
  24834. '']);
  24835. ConvertProgram;
  24836. CheckSource('TestTypeHelper_SetType',
  24837. LinesToStr([ // statements
  24838. 'this.TEnum = {',
  24839. ' "0": "red",',
  24840. ' red: 0,',
  24841. ' "1": "blue",',
  24842. ' blue: 1',
  24843. '};',
  24844. 'rtl.createHelper(this, "THelper", null, function () {',
  24845. ' this.DoIt = function (e) {',
  24846. ' this.set({});',
  24847. ' this.set(rtl.createSet($mod.TEnum.red));',
  24848. ' this.set(rtl.includeSet(this.get(), $mod.TEnum.blue));',
  24849. ' };',
  24850. ' this.Init = function (e) {',
  24851. ' this.set({});',
  24852. ' this.set(rtl.createSet(e));',
  24853. ' this.set(rtl.includeSet(this.get(), $mod.TEnum.blue));',
  24854. ' return this.get();',
  24855. ' };',
  24856. ' this.InitEmpty = function () {',
  24857. ' return this.get();',
  24858. ' };',
  24859. ' this.$new = function (fn, args) {',
  24860. ' return this[fn].apply({',
  24861. ' p: {},',
  24862. ' get: function () {',
  24863. ' return this.p;',
  24864. ' },',
  24865. ' set: function (v) {',
  24866. ' this.p = v;',
  24867. ' }',
  24868. ' }, args);',
  24869. ' };',
  24870. '});',
  24871. 'this.s = {};',
  24872. '']),
  24873. LinesToStr([ // $mod.$main
  24874. '$mod.THelper.DoIt.call({',
  24875. ' p: $mod,',
  24876. ' get: function () {',
  24877. ' return this.p.s;',
  24878. ' },',
  24879. ' set: function (v) {',
  24880. ' this.p.s = v;',
  24881. ' }',
  24882. '}, 123);',
  24883. '$mod.s = rtl.refSet($mod.THelper.$new("Init", [$mod.TEnum.blue]));',
  24884. '$mod.s = rtl.refSet($mod.THelper.Init.call({',
  24885. ' p: $mod,',
  24886. ' get: function () {',
  24887. ' return this.p.s;',
  24888. ' },',
  24889. ' set: function (v) {',
  24890. ' this.p.s = v;',
  24891. ' }',
  24892. '}, $mod.TEnum.blue));',
  24893. '']));
  24894. end;
  24895. procedure TTestModule.TestTypeHelper_InterfaceType;
  24896. begin
  24897. StartProgram(false);
  24898. Add([
  24899. '{$interfaces com}',
  24900. '{$modeswitch typehelpers}',
  24901. 'type',
  24902. ' IUnknown = interface',
  24903. ' function _AddRef: longint;',
  24904. ' function _Release: longint;',
  24905. ' end;',
  24906. ' TObject = class(IUnknown)',
  24907. ' function _AddRef: longint; virtual; abstract;',
  24908. ' function _Release: longint; virtual; abstract;',
  24909. ' end;',
  24910. ' THelper = type helper for IUnknown',
  24911. ' procedure Fly(e: byte = 123);',
  24912. ' class procedure Run; static;',
  24913. ' end;',
  24914. 'var',
  24915. ' i: IUnknown;',
  24916. ' o: TObject;',
  24917. 'procedure THelper.Fly(e: byte);',
  24918. 'begin',
  24919. ' i:=Self;',
  24920. ' o:=Self as TObject;',
  24921. ' Self:=nil;',
  24922. ' Self:=i;',
  24923. ' Self:=o;',
  24924. ' with Self do begin',
  24925. ' Fly;',
  24926. ' Fly();',
  24927. ' end;',
  24928. 'end;',
  24929. 'class procedure THelper.Run;',
  24930. 'var l: IUnknown;',
  24931. 'begin',
  24932. ' l.Fly;',
  24933. ' l.Fly();',
  24934. 'end;',
  24935. 'begin',
  24936. ' i.Fly;',
  24937. ' i.Fly();',
  24938. ' i.Run;',
  24939. ' i.Run();',
  24940. ' IUnknown.Run;',
  24941. ' IUnknown.Run();',
  24942. '']);
  24943. ConvertProgram;
  24944. CheckSource('TestTypeHelper_InterfaceType',
  24945. LinesToStr([ // statements
  24946. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  24947. 'rtl.createClass(this, "TObject", null, function () {',
  24948. ' this.$init = function () {',
  24949. ' };',
  24950. ' this.$final = function () {',
  24951. ' };',
  24952. ' rtl.addIntf(this, $mod.IUnknown);',
  24953. '});',
  24954. 'rtl.createHelper(this, "THelper", null, function () {',
  24955. ' this.Fly = function (e) {',
  24956. ' var $ir = rtl.createIntfRefs();',
  24957. ' try {',
  24958. ' rtl.setIntfP($mod, "i", this.get());',
  24959. ' $mod.o = rtl.intfAsClass(this.get(), $mod.TObject);',
  24960. ' this.set(null);',
  24961. ' this.set($mod.i);',
  24962. ' this.set($ir.ref(1, rtl.queryIntfT($mod.o, $mod.IUnknown)));',
  24963. ' var $with = this.get();',
  24964. ' $mod.THelper.Fly.call(this, 123);',
  24965. ' $mod.THelper.Fly.call(this, 123);',
  24966. ' } finally {',
  24967. ' $ir.free();',
  24968. ' };',
  24969. ' };',
  24970. ' this.Run = function () {',
  24971. ' var l = null;',
  24972. ' try {',
  24973. ' $mod.THelper.Fly.call({',
  24974. ' get: function () {',
  24975. ' return l;',
  24976. ' },',
  24977. ' set: function (v) {',
  24978. ' l = rtl.setIntfL(l, v);',
  24979. ' }',
  24980. ' }, 123);',
  24981. ' $mod.THelper.Fly.call({',
  24982. ' get: function () {',
  24983. ' return l;',
  24984. ' },',
  24985. ' set: function (v) {',
  24986. ' l = rtl.setIntfL(l, v);',
  24987. ' }',
  24988. ' }, 123);',
  24989. ' } finally {',
  24990. ' rtl._Release(l);',
  24991. ' };',
  24992. ' };',
  24993. '});',
  24994. 'this.i = null;',
  24995. 'this.o = null;',
  24996. '']),
  24997. LinesToStr([ // $mod.$main
  24998. '$mod.THelper.Fly.call({',
  24999. ' p: $mod,',
  25000. ' get: function () {',
  25001. ' return this.p.i;',
  25002. ' },',
  25003. ' set: function (v) {',
  25004. ' rtl.setIntfP(this.p, "i", v);',
  25005. ' }',
  25006. '}, 123);',
  25007. '$mod.THelper.Fly.call({',
  25008. ' p: $mod,',
  25009. ' get: function () {',
  25010. ' return this.p.i;',
  25011. ' },',
  25012. ' set: function (v) {',
  25013. ' rtl.setIntfP(this.p, "i", v);',
  25014. ' }',
  25015. '}, 123);',
  25016. '$mod.THelper.Run();',
  25017. '$mod.THelper.Run();',
  25018. '$mod.THelper.Run();',
  25019. '$mod.THelper.Run();',
  25020. '']));
  25021. end;
  25022. procedure TTestModule.TestTypeHelper_NestedSelf;
  25023. begin
  25024. StartProgram(false);
  25025. Add([
  25026. '{$modeswitch typehelpers}',
  25027. 'type',
  25028. ' THelper = type helper for string',
  25029. ' procedure Run(Value: string);',
  25030. ' end;',
  25031. 'procedure THelper.Run(Value: string);',
  25032. ' function Sub(i: nativeint): boolean;',
  25033. ' begin',
  25034. ' Result:=Self[i+1]=Value[i];',
  25035. ' end;',
  25036. 'begin',
  25037. ' if Self[3]=Value[4] then ;',
  25038. 'end;',
  25039. 'begin',
  25040. '']);
  25041. ConvertProgram;
  25042. CheckSource('TestTypeHelper_NestedSelf',
  25043. LinesToStr([ // statements
  25044. 'rtl.createHelper(this, "THelper", null, function () {',
  25045. ' this.Run = function (Value) {',
  25046. ' var $Self = this;',
  25047. ' function Sub(i) {',
  25048. ' var Result = false;',
  25049. ' Result = $Self.get().charAt((i + 1) - 1) === Value.charAt(i - 1);',
  25050. ' return Result;',
  25051. ' };',
  25052. ' if ($Self.get().charAt(2) === Value.charAt(3)) ;',
  25053. ' };',
  25054. '});',
  25055. '']),
  25056. LinesToStr([ // $mod.$main
  25057. '']));
  25058. end;
  25059. procedure TTestModule.TestProcType;
  25060. begin
  25061. StartProgram(false);
  25062. Add([
  25063. 'type',
  25064. ' TProcInt = procedure(vI: longint = 1);',
  25065. 'procedure DoIt(vJ: longint);',
  25066. 'begin end;',
  25067. 'var',
  25068. ' b: boolean;',
  25069. ' vP, vQ: tprocint;',
  25070. 'begin',
  25071. ' vp:=nil;',
  25072. ' vp:=vp;',
  25073. ' vp:=@doit;',
  25074. ' vp;',
  25075. ' vp();',
  25076. ' vp(2);',
  25077. ' b:=vp=nil;',
  25078. ' b:=nil=vp;',
  25079. ' b:=vp=vq;',
  25080. ' b:=vp=@doit;',
  25081. ' b:=@doit=vp;',
  25082. ' b:=vp<>nil;',
  25083. ' b:=nil<>vp;',
  25084. ' b:=vp<>vq;',
  25085. ' b:=vp<>@doit;',
  25086. ' b:=@doit<>vp;',
  25087. ' b:=Assigned(vp);',
  25088. ' if Assigned(vp) then ;']);
  25089. ConvertProgram;
  25090. CheckSource('TestProcType',
  25091. LinesToStr([ // statements
  25092. 'this.DoIt = function(vJ) {',
  25093. '};',
  25094. 'this.b = false;',
  25095. 'this.vP = null;',
  25096. 'this.vQ = null;'
  25097. ]),
  25098. LinesToStr([ // $mod.$main
  25099. '$mod.vP = null;',
  25100. '$mod.vP = $mod.vP;',
  25101. '$mod.vP = $mod.DoIt;',
  25102. '$mod.vP(1);',
  25103. '$mod.vP(1);',
  25104. '$mod.vP(2);',
  25105. '$mod.b = $mod.vP === null;',
  25106. '$mod.b = null === $mod.vP;',
  25107. '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
  25108. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  25109. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  25110. '$mod.b = $mod.vP !== null;',
  25111. '$mod.b = null !== $mod.vP;',
  25112. '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
  25113. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  25114. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  25115. '$mod.b = $mod.vP != null;',
  25116. 'if ($mod.vP != null) ;',
  25117. '']));
  25118. end;
  25119. procedure TTestModule.TestProcType_Arg;
  25120. begin
  25121. StartProgram(false);
  25122. Add([
  25123. 'type',
  25124. ' TProcInt = procedure(vI: longint = 1);',
  25125. 'procedure DoIt(vJ: longint); begin end;',
  25126. 'procedure DoSome(vP, vQ: TProcInt);',
  25127. 'var',
  25128. ' b: boolean;',
  25129. 'begin',
  25130. ' vp:=nil;',
  25131. ' vp:=vp;',
  25132. ' vp:=@doit;',
  25133. ' vp;',
  25134. ' vp();',
  25135. ' vp(2);',
  25136. ' b:=vp=nil;',
  25137. ' b:=nil=vp;',
  25138. ' b:=vp=vq;',
  25139. ' b:=vp=@doit;',
  25140. ' b:=@doit=vp;',
  25141. ' b:=vp<>nil;',
  25142. ' b:=nil<>vp;',
  25143. ' b:=vp<>vq;',
  25144. ' b:=vp<>@doit;',
  25145. ' b:=@doit<>vp;',
  25146. ' b:=Assigned(vp);',
  25147. ' if Assigned(vp) then ;',
  25148. 'end;',
  25149. 'begin',
  25150. ' DoSome(@DoIt,nil);']);
  25151. ConvertProgram;
  25152. CheckSource('TestProcType_Arg',
  25153. LinesToStr([ // statements
  25154. 'this.DoIt = function(vJ) {',
  25155. '};',
  25156. 'this.DoSome = function(vP, vQ) {',
  25157. ' var b = false;',
  25158. ' vP = null;',
  25159. ' vP = vP;',
  25160. ' vP = $mod.DoIt;',
  25161. ' vP(1);',
  25162. ' vP(1);',
  25163. ' vP(2);',
  25164. ' b = vP === null;',
  25165. ' b = null === vP;',
  25166. ' b = rtl.eqCallback(vP,vQ);',
  25167. ' b = rtl.eqCallback(vP, $mod.DoIt);',
  25168. ' b = rtl.eqCallback($mod.DoIt, vP);',
  25169. ' b = vP !== null;',
  25170. ' b = null !== vP;',
  25171. ' b = !rtl.eqCallback(vP, vQ);',
  25172. ' b = !rtl.eqCallback(vP, $mod.DoIt);',
  25173. ' b = !rtl.eqCallback($mod.DoIt, vP);',
  25174. ' b = vP != null;',
  25175. ' if (vP != null) ;',
  25176. '};',
  25177. '']),
  25178. LinesToStr([ // $mod.$main
  25179. '$mod.DoSome($mod.DoIt,null);',
  25180. '']));
  25181. end;
  25182. procedure TTestModule.TestProcType_FunctionFPC;
  25183. begin
  25184. StartProgram(false);
  25185. Add('type');
  25186. Add(' TFuncInt = function(vA: longint = 1): longint;');
  25187. Add('function DoIt(vI: longint): longint;');
  25188. Add('begin end;');
  25189. Add('var');
  25190. Add(' b: boolean;');
  25191. Add(' vP, vQ: tfuncint;');
  25192. Add('begin');
  25193. Add(' vp:=nil;');
  25194. Add(' vp:=vp;');
  25195. Add(' vp:=@doit;'); // ok in fpc and delphi
  25196. //Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  25197. Add(' vp;'); // ok in fpc and delphi
  25198. Add(' vp();');
  25199. Add(' vp(2);');
  25200. Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  25201. Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  25202. Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  25203. Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  25204. Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  25205. //Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
  25206. Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
  25207. Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  25208. Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  25209. Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  25210. Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  25211. Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  25212. //Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
  25213. Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
  25214. Add(' b:=Assigned(vp);');
  25215. //Add(' doit(vp);'); // illegal in fpc, ok in delphi
  25216. Add(' doit(vp());'); // ok in fpc and delphi
  25217. Add(' doit(vp(2));'); // ok in fpc and delphi
  25218. ConvertProgram;
  25219. CheckSource('TestProcType_FunctionFPC',
  25220. LinesToStr([ // statements
  25221. 'this.DoIt = function(vI) {',
  25222. ' var Result = 0;',
  25223. ' return Result;',
  25224. '};',
  25225. 'this.b = false;',
  25226. 'this.vP = null;',
  25227. 'this.vQ = null;'
  25228. ]),
  25229. LinesToStr([ // $mod.$main
  25230. '$mod.vP = null;',
  25231. '$mod.vP = $mod.vP;',
  25232. '$mod.vP = $mod.DoIt;',
  25233. '$mod.vP(1);',
  25234. '$mod.vP(1);',
  25235. '$mod.vP(2);',
  25236. '$mod.b = $mod.vP === null;',
  25237. '$mod.b = null === $mod.vP;',
  25238. '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
  25239. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  25240. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  25241. '$mod.b = 4 === $mod.vP(1);',
  25242. '$mod.b = $mod.vP !== null;',
  25243. '$mod.b = null !== $mod.vP;',
  25244. '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
  25245. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  25246. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  25247. '$mod.b = 6 !== $mod.vP(1);',
  25248. '$mod.b = $mod.vP != null;',
  25249. '$mod.DoIt($mod.vP(1));',
  25250. '$mod.DoIt($mod.vP(2));',
  25251. '']));
  25252. end;
  25253. procedure TTestModule.TestProcType_FunctionDelphi;
  25254. begin
  25255. StartProgram(false);
  25256. Add('{$mode Delphi}');
  25257. Add('type');
  25258. Add(' TFuncInt = function(vA: longint = 1): longint;');
  25259. Add('function DoIt(vI: longint): longint;');
  25260. Add('begin end;');
  25261. Add('var');
  25262. Add(' b: boolean;');
  25263. Add(' vP, vQ: tfuncint;');
  25264. Add('begin');
  25265. Add(' vp:=nil;');
  25266. Add(' vp:=vp;');
  25267. Add(' vp:=@doit;'); // ok in fpc and delphi
  25268. Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  25269. Add(' vp;'); // ok in fpc and delphi
  25270. Add(' vp();');
  25271. Add(' vp(2);');
  25272. //Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  25273. //Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  25274. Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  25275. //Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  25276. //Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  25277. Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
  25278. Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
  25279. //Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  25280. //Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  25281. Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  25282. //Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  25283. //Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  25284. Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
  25285. Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
  25286. Add(' b:=Assigned(vp);');
  25287. Add(' doit(vp);'); // illegal in fpc, ok in delphi
  25288. Add(' doit(vp());'); // ok in fpc and delphi
  25289. Add(' doit(vp(2));'); // ok in fpc and delphi *)
  25290. ConvertProgram;
  25291. CheckSource('TestProcType_FunctionDelphi',
  25292. LinesToStr([ // statements
  25293. 'this.DoIt = function(vI) {',
  25294. ' var Result = 0;',
  25295. ' return Result;',
  25296. '};',
  25297. 'this.b = false;',
  25298. 'this.vP = null;',
  25299. 'this.vQ = null;'
  25300. ]),
  25301. LinesToStr([ // $mod.$main
  25302. '$mod.vP = null;',
  25303. '$mod.vP = $mod.vP;',
  25304. '$mod.vP = $mod.DoIt;',
  25305. '$mod.vP = $mod.DoIt;',
  25306. '$mod.vP(1);',
  25307. '$mod.vP(1);',
  25308. '$mod.vP(2);',
  25309. '$mod.b = $mod.vP(1) === $mod.vQ(1);',
  25310. '$mod.b = $mod.vP(1) === 3;',
  25311. '$mod.b = 4 === $mod.vP(1);',
  25312. '$mod.b = $mod.vP(1) !== $mod.vQ(1);',
  25313. '$mod.b = $mod.vP(1) !== 5;',
  25314. '$mod.b = 6 !== $mod.vP(1);',
  25315. '$mod.b = $mod.vP != null;',
  25316. '$mod.DoIt($mod.vP(1));',
  25317. '$mod.DoIt($mod.vP(1));',
  25318. '$mod.DoIt($mod.vP(2));',
  25319. '']));
  25320. end;
  25321. procedure TTestModule.TestProcType_ProcedureDelphi;
  25322. begin
  25323. StartProgram(false);
  25324. Add('{$mode Delphi}');
  25325. Add('type');
  25326. Add(' TProc = procedure;');
  25327. Add('procedure DoIt;');
  25328. Add('begin end;');
  25329. Add('var');
  25330. Add(' b: boolean;');
  25331. Add(' vP, vQ: tproc;');
  25332. Add('begin');
  25333. Add(' vp:=nil;');
  25334. Add(' vp:=vp;');
  25335. Add(' vp:=vq;');
  25336. 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
  25337. Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  25338. //Add(' vp:=@doit;'); // illegal in fpc, ok in delphi (because Delphi treats @F as Pointer), not supported by resolver
  25339. Add(' vp;'); // ok in fpc and delphi
  25340. Add(' vp();');
  25341. // equal
  25342. //Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  25343. Add(' b:=@@vp=nil;'); // ok in fpc delphi mode, ok in delphi
  25344. //Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  25345. Add(' b:=nil=@@vp;'); // ok in fpc delphi mode, ok in delphi
  25346. Add(' b:=@@vp=@@vq;'); // ok in fpc delphi mode, ok in Delphi
  25347. //Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  25348. //Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  25349. Add(' b:=@@vp=@doit;'); // ok in fpc delphi mode, ok in delphi
  25350. //Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  25351. Add(' b:=@doit=@@vp;'); // ok in fpc delphi mode, ok in delphi
  25352. // unequal
  25353. //Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  25354. Add(' b:=@@vp<>nil;'); // ok in fpc mode delphi, ok in delphi
  25355. //Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  25356. Add(' b:=nil<>@@vp;'); // ok in fpc mode delphi, ok in delphi
  25357. //Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  25358. Add(' b:=@@vp<>@@vq;'); // ok in fpc mode delphi, ok in delphi
  25359. //Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  25360. Add(' b:=@@vp<>@doit;'); // ok in fpc mode delphi, illegal in delphi
  25361. //Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  25362. Add(' b:=@doit<>@@vp;'); // ok in fpc mode delphi, illegal in delphi
  25363. Add(' b:=Assigned(vp);');
  25364. ConvertProgram;
  25365. CheckSource('TestProcType_ProcedureDelphi',
  25366. LinesToStr([ // statements
  25367. 'this.DoIt = function() {',
  25368. '};',
  25369. 'this.b = false;',
  25370. 'this.vP = null;',
  25371. 'this.vQ = null;'
  25372. ]),
  25373. LinesToStr([ // $mod.$main
  25374. '$mod.vP = null;',
  25375. '$mod.vP = $mod.vP;',
  25376. '$mod.vP = $mod.vQ;',
  25377. '$mod.vP = $mod.DoIt;',
  25378. '$mod.vP = $mod.DoIt;',
  25379. '$mod.vP();',
  25380. '$mod.vP();',
  25381. '$mod.b = $mod.vP === null;',
  25382. '$mod.b = null === $mod.vP;',
  25383. '$mod.b = rtl.eqCallback($mod.vP, $mod.vQ);',
  25384. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  25385. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  25386. '$mod.b = $mod.vP !== null;',
  25387. '$mod.b = null !== $mod.vP;',
  25388. '$mod.b = !rtl.eqCallback($mod.vP, $mod.vQ);',
  25389. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  25390. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  25391. '$mod.b = $mod.vP != null;',
  25392. '']));
  25393. end;
  25394. procedure TTestModule.TestProcType_AsParam;
  25395. begin
  25396. StartProgram(false);
  25397. Add('type');
  25398. Add(' TFuncInt = function(vA: longint = 1): longint;');
  25399. Add('procedure DoIt(vG: tfuncint; const vH: tfuncint; var vI: tfuncint);');
  25400. Add('var vJ: tfuncint;');
  25401. Add('begin');
  25402. Add(' vg:=vg;');
  25403. Add(' vj:=vh;');
  25404. Add(' vi:=vi;');
  25405. Add(' doit(vg,vg,vg);');
  25406. Add(' doit(vh,vh,vj);');
  25407. Add(' doit(vi,vi,vi);');
  25408. Add(' doit(vj,vj,vj);');
  25409. Add('end;');
  25410. Add('var i: tfuncint;');
  25411. Add('begin');
  25412. Add(' doit(i,i,i);');
  25413. ConvertProgram;
  25414. CheckSource('TestProcType_AsParam',
  25415. LinesToStr([ // statements
  25416. 'this.DoIt = function (vG,vH,vI) {',
  25417. ' var vJ = null;',
  25418. ' vG = vG;',
  25419. ' vJ = vH;',
  25420. ' vI.set(vI.get());',
  25421. ' $mod.DoIt(vG, vG, {',
  25422. ' get: function () {',
  25423. ' return vG;',
  25424. ' },',
  25425. ' set: function (v) {',
  25426. ' vG = v;',
  25427. ' }',
  25428. ' });',
  25429. ' $mod.DoIt(vH, vH, {',
  25430. ' get: function () {',
  25431. ' return vJ;',
  25432. ' },',
  25433. ' set: function (v) {',
  25434. ' vJ = v;',
  25435. ' }',
  25436. ' });',
  25437. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  25438. ' $mod.DoIt(vJ, vJ, {',
  25439. ' get: function () {',
  25440. ' return vJ;',
  25441. ' },',
  25442. ' set: function (v) {',
  25443. ' vJ = v;',
  25444. ' }',
  25445. ' });',
  25446. '};',
  25447. 'this.i = null;'
  25448. ]),
  25449. LinesToStr([
  25450. '$mod.DoIt($mod.i,$mod.i,{',
  25451. ' p: $mod,',
  25452. ' get: function () {',
  25453. ' return this.p.i;',
  25454. ' },',
  25455. ' set: function (v) {',
  25456. ' this.p.i = v;',
  25457. ' }',
  25458. '});'
  25459. ]));
  25460. end;
  25461. procedure TTestModule.TestProcType_MethodFPC;
  25462. begin
  25463. StartProgram(false);
  25464. Add('type');
  25465. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  25466. Add(' TObject = class');
  25467. Add(' function DoIt(vA: longint = 1): longint;');
  25468. Add(' end;');
  25469. Add('function TObject.DoIt(vA: longint = 1): longint;');
  25470. Add('begin');
  25471. Add('end;');
  25472. Add('var');
  25473. Add(' Obj: TObject;');
  25474. Add(' vP: tfuncint;');
  25475. Add(' b: boolean;');
  25476. Add('begin');
  25477. Add(' vp:[email protected];'); // ok in fpc and delphi
  25478. //Add(' vp:=obj.doit;'); // illegal in fpc, ok in delphi
  25479. Add(' vp;'); // ok in fpc and delphi
  25480. Add(' vp();');
  25481. Add(' vp(2);');
  25482. Add(' b:[email protected];'); // ok in fpc, illegal in delphi
  25483. Add(' b:[email protected]=vp;'); // ok in fpc, illegal in delphi
  25484. Add(' b:=vp<>@obj.doit;'); // ok in fpc, illegal in delphi
  25485. Add(' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
  25486. ConvertProgram;
  25487. CheckSource('TestProcType_MethodFPC',
  25488. LinesToStr([ // statements
  25489. 'rtl.createClass(this, "TObject", null, function () {',
  25490. ' this.$init = function () {',
  25491. ' };',
  25492. ' this.$final = function () {',
  25493. ' };',
  25494. ' this.DoIt = function (vA) {',
  25495. ' var Result = 0;',
  25496. ' return Result;',
  25497. ' };',
  25498. '});',
  25499. 'this.Obj = null;',
  25500. 'this.vP = null;',
  25501. 'this.b = false;'
  25502. ]),
  25503. LinesToStr([
  25504. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  25505. '$mod.vP(1);',
  25506. '$mod.vP(1);',
  25507. '$mod.vP(2);',
  25508. '$mod.b = rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
  25509. '$mod.b = rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
  25510. '$mod.b = !rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
  25511. '$mod.b = !rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
  25512. '']));
  25513. end;
  25514. procedure TTestModule.TestProcType_MethodDelphi;
  25515. begin
  25516. StartProgram(false);
  25517. Add([
  25518. '{$mode delphi}',
  25519. 'type',
  25520. ' TFuncInt = function(vA: longint = 1): longint of object;',
  25521. ' TObject = class',
  25522. ' function DoIt(vA: longint = 1): longint;',
  25523. ' end;',
  25524. 'function TObject.DoIt(vA: longint = 1): longint;',
  25525. 'begin',
  25526. 'end;',
  25527. 'var',
  25528. ' Obj: TObject;',
  25529. ' vP: tfuncint;',
  25530. ' b: boolean;',
  25531. 'begin',
  25532. ' vp:[email protected];', // ok in fpc and delphi
  25533. ' vp:=obj.doit;', // illegal in fpc, ok in delphi
  25534. ' vp;', // ok in fpc and delphi
  25535. ' vp();',
  25536. ' vp(2);',
  25537. //' b:[email protected];', // ok in fpc, illegal in delphi
  25538. //' b:[email protected]=vp;', // ok in fpc, illegal in delphi
  25539. //' b:=vp<>@obj.doit;', // ok in fpc, illegal in delphi
  25540. //' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
  25541. '']);
  25542. ConvertProgram;
  25543. CheckSource('TestProcType_MethodDelphi',
  25544. LinesToStr([ // statements
  25545. 'rtl.createClass(this, "TObject", null, function () {',
  25546. ' this.$init = function () {',
  25547. ' };',
  25548. ' this.$final = function () {',
  25549. ' };',
  25550. ' this.DoIt = function (vA) {',
  25551. ' var Result = 0;',
  25552. ' return Result;',
  25553. ' };',
  25554. '});',
  25555. 'this.Obj = null;',
  25556. 'this.vP = null;',
  25557. 'this.b = false;'
  25558. ]),
  25559. LinesToStr([
  25560. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  25561. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  25562. '$mod.vP(1);',
  25563. '$mod.vP(1);',
  25564. '$mod.vP(2);',
  25565. '']));
  25566. end;
  25567. procedure TTestModule.TestProcType_PropertyFPC;
  25568. begin
  25569. StartProgram(false);
  25570. Add('type');
  25571. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  25572. Add(' TObject = class');
  25573. Add(' FOnFoo: TFuncInt;');
  25574. Add(' function DoIt(vA: longint = 1): longint;');
  25575. Add(' function GetFoo: TFuncInt;');
  25576. Add(' procedure SetFoo(const Value: TFuncInt);');
  25577. Add(' function GetEvents(Index: longint): TFuncInt;');
  25578. Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
  25579. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  25580. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  25581. Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
  25582. Add(' end;');
  25583. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  25584. Add('function tobject.getfoo: tfuncint; begin end;');
  25585. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  25586. Add('function tobject.getevents(index: longint): tfuncint; begin end;');
  25587. Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
  25588. Add('var');
  25589. Add(' Obj: TObject;');
  25590. Add(' vP: tfuncint;');
  25591. Add(' b: boolean;');
  25592. Add('begin');
  25593. Add(' obj.onfoo:=nil;');
  25594. Add(' obj.onbar:=nil;');
  25595. Add(' obj.events[1]:=nil;');
  25596. Add(' obj.onfoo:=obj.onfoo;');
  25597. Add(' obj.onbar:=obj.onbar;');
  25598. Add(' obj.events[2]:=obj.events[3];');
  25599. Add(' obj.onfoo:[email protected];');
  25600. Add(' obj.onbar:[email protected];');
  25601. Add(' obj.events[4]:[email protected];');
  25602. //Add(' obj.onfoo:=obj.doit;'); // delphi
  25603. //Add(' obj.onbar:=obj.doit;'); // delphi
  25604. //Add(' obj.events[4]:=obj.doit;'); // delphi
  25605. Add(' obj.onfoo;');
  25606. Add(' obj.onbar;');
  25607. //Add(' obj.events[5];'); ToDo in pasresolver
  25608. Add(' obj.onfoo();');
  25609. Add(' obj.onbar();');
  25610. Add(' obj.events[6]();');
  25611. Add(' b:=obj.onfoo=nil;');
  25612. Add(' b:=obj.onbar=nil;');
  25613. Add(' b:=obj.events[7]=nil;');
  25614. Add(' b:=obj.onfoo<>nil;');
  25615. Add(' b:=obj.onbar<>nil;');
  25616. Add(' b:=obj.events[8]<>nil;');
  25617. Add(' b:=obj.onfoo=vp;');
  25618. Add(' b:=obj.onbar=vp;');
  25619. Add(' b:=obj.events[9]=vp;');
  25620. Add(' b:=obj.onfoo=obj.onfoo;');
  25621. Add(' b:=obj.onbar=obj.onfoo;');
  25622. Add(' b:=obj.events[10]=obj.onfoo;');
  25623. Add(' b:=obj.onfoo<>obj.onfoo;');
  25624. Add(' b:=obj.onbar<>obj.onfoo;');
  25625. Add(' b:=obj.events[11]<>obj.onfoo;');
  25626. Add(' b:[email protected];');
  25627. Add(' b:[email protected];');
  25628. Add(' b:=obj.events[12][email protected];');
  25629. Add(' b:=obj.onfoo<>@obj.doit;');
  25630. Add(' b:=obj.onbar<>@obj.doit;');
  25631. Add(' b:=obj.events[12]<>@obj.doit;');
  25632. Add(' b:=Assigned(obj.onfoo);');
  25633. Add(' b:=Assigned(obj.onbar);');
  25634. Add(' b:=Assigned(obj.events[13]);');
  25635. ConvertProgram;
  25636. CheckSource('TestProcType_PropertyFPC',
  25637. LinesToStr([ // statements
  25638. 'rtl.createClass(this, "TObject", null, function () {',
  25639. ' this.$init = function () {',
  25640. ' this.FOnFoo = null;',
  25641. ' };',
  25642. ' this.$final = function () {',
  25643. ' this.FOnFoo = undefined;',
  25644. ' };',
  25645. ' this.DoIt = function (vA) {',
  25646. ' var Result = 0;',
  25647. ' return Result;',
  25648. ' };',
  25649. 'this.GetFoo = function () {',
  25650. ' var Result = null;',
  25651. ' return Result;',
  25652. '};',
  25653. 'this.SetFoo = function (Value) {',
  25654. '};',
  25655. 'this.GetEvents = function (Index) {',
  25656. ' var Result = null;',
  25657. ' return Result;',
  25658. '};',
  25659. 'this.SetEvents = function (Index, Value) {',
  25660. '};',
  25661. '});',
  25662. 'this.Obj = null;',
  25663. 'this.vP = null;',
  25664. 'this.b = false;'
  25665. ]),
  25666. LinesToStr([
  25667. '$mod.Obj.FOnFoo = null;',
  25668. '$mod.Obj.SetFoo(null);',
  25669. '$mod.Obj.SetEvents(1, null);',
  25670. '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
  25671. '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
  25672. '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
  25673. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  25674. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  25675. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  25676. '$mod.Obj.FOnFoo(1);',
  25677. '$mod.Obj.GetFoo();',
  25678. '$mod.Obj.FOnFoo(1);',
  25679. '$mod.Obj.GetFoo()(1);',
  25680. '$mod.Obj.GetEvents(6)(1);',
  25681. '$mod.b = $mod.Obj.FOnFoo === null;',
  25682. '$mod.b = $mod.Obj.GetFoo() === null;',
  25683. '$mod.b = $mod.Obj.GetEvents(7) === null;',
  25684. '$mod.b = $mod.Obj.FOnFoo !== null;',
  25685. '$mod.b = $mod.Obj.GetFoo() !== null;',
  25686. '$mod.b = $mod.Obj.GetEvents(8) !== null;',
  25687. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.vP);',
  25688. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.vP);',
  25689. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(9), $mod.vP);',
  25690. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
  25691. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
  25692. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(10), $mod.Obj.FOnFoo);',
  25693. '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
  25694. '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
  25695. '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(11), $mod.Obj.FOnFoo);',
  25696. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
  25697. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
  25698. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
  25699. '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
  25700. '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
  25701. '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
  25702. '$mod.b = $mod.Obj.FOnFoo != null;',
  25703. '$mod.b = $mod.Obj.GetFoo() != null;',
  25704. '$mod.b = $mod.Obj.GetEvents(13) != null;',
  25705. '']));
  25706. end;
  25707. procedure TTestModule.TestProcType_PropertyDelphi;
  25708. begin
  25709. StartProgram(false);
  25710. Add('{$mode delphi}');
  25711. Add('type');
  25712. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  25713. Add(' TObject = class');
  25714. Add(' FOnFoo: TFuncInt;');
  25715. Add(' function DoIt(vA: longint = 1): longint;');
  25716. Add(' function GetFoo: TFuncInt;');
  25717. Add(' procedure SetFoo(const Value: TFuncInt);');
  25718. Add(' function GetEvents(Index: longint): TFuncInt;');
  25719. Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
  25720. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  25721. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  25722. Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
  25723. Add(' end;');
  25724. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  25725. Add('function tobject.getfoo: tfuncint; begin end;');
  25726. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  25727. Add('function tobject.getevents(index: longint): tfuncint; begin end;');
  25728. Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
  25729. Add('var');
  25730. Add(' Obj: TObject;');
  25731. Add(' vP: tfuncint;');
  25732. Add(' b: boolean;');
  25733. Add('begin');
  25734. Add(' obj.onfoo:=nil;');
  25735. Add(' obj.onbar:=nil;');
  25736. Add(' obj.events[1]:=nil;');
  25737. Add(' obj.onfoo:=obj.onfoo;');
  25738. Add(' obj.onbar:=obj.onbar;');
  25739. Add(' obj.events[2]:=obj.events[3];');
  25740. Add(' obj.onfoo:[email protected];');
  25741. Add(' obj.onbar:[email protected];');
  25742. Add(' obj.events[4]:[email protected];');
  25743. Add(' obj.onfoo:=obj.doit;'); // delphi
  25744. Add(' obj.onbar:=obj.doit;'); // delphi
  25745. Add(' obj.events[4]:=obj.doit;'); // delphi
  25746. Add(' obj.onfoo;');
  25747. Add(' obj.onbar;');
  25748. //Add(' obj.events[5];'); ToDo in pasresolver
  25749. Add(' obj.onfoo();');
  25750. Add(' obj.onbar();');
  25751. Add(' obj.events[6]();');
  25752. //Add(' b:=obj.onfoo=nil;'); // fpc
  25753. //Add(' b:=obj.onbar=nil;'); // fpc
  25754. //Add(' b:=obj.events[7]=nil;'); // fpc
  25755. //Add(' b:=obj.onfoo<>nil;'); // fpc
  25756. //Add(' b:=obj.onbar<>nil;'); // fpc
  25757. //Add(' b:=obj.events[8]<>nil;'); // fpc
  25758. Add(' b:=obj.onfoo=vp;');
  25759. Add(' b:=obj.onbar=vp;');
  25760. //Add(' b:=obj.events[9]=vp;'); ToDo in pasresolver
  25761. Add(' b:=obj.onfoo=obj.onfoo;');
  25762. Add(' b:=obj.onbar=obj.onfoo;');
  25763. //Add(' b:=obj.events[10]=obj.onfoo;'); // ToDo in pasresolver
  25764. Add(' b:=obj.onfoo<>obj.onfoo;');
  25765. Add(' b:=obj.onbar<>obj.onfoo;');
  25766. //Add(' b:=obj.events[11]<>obj.onfoo;'); // ToDo in pasresolver
  25767. //Add(' b:[email protected];'); // fpc
  25768. //Add(' b:[email protected];'); // fpc
  25769. //Add(' b:=obj.events[12][email protected];'); // fpc
  25770. //Add(' b:=obj.onfoo<>@obj.doit;'); // fpc
  25771. //Add(' b:=obj.onbar<>@obj.doit;'); // fpc
  25772. //Add(' b:=obj.events[12]<>@obj.doit;'); // fpc
  25773. Add(' b:=Assigned(obj.onfoo);');
  25774. Add(' b:=Assigned(obj.onbar);');
  25775. Add(' b:=Assigned(obj.events[13]);');
  25776. ConvertProgram;
  25777. CheckSource('TestProcType_PropertyDelphi',
  25778. LinesToStr([ // statements
  25779. 'rtl.createClass(this, "TObject", null, function () {',
  25780. ' this.$init = function () {',
  25781. ' this.FOnFoo = null;',
  25782. ' };',
  25783. ' this.$final = function () {',
  25784. ' this.FOnFoo = undefined;',
  25785. ' };',
  25786. ' this.DoIt = function (vA) {',
  25787. ' var Result = 0;',
  25788. ' return Result;',
  25789. ' };',
  25790. 'this.GetFoo = function () {',
  25791. ' var Result = null;',
  25792. ' return Result;',
  25793. '};',
  25794. 'this.SetFoo = function (Value) {',
  25795. '};',
  25796. 'this.GetEvents = function (Index) {',
  25797. ' var Result = null;',
  25798. ' return Result;',
  25799. '};',
  25800. 'this.SetEvents = function (Index, Value) {',
  25801. '};',
  25802. '});',
  25803. 'this.Obj = null;',
  25804. 'this.vP = null;',
  25805. 'this.b = false;'
  25806. ]),
  25807. LinesToStr([
  25808. '$mod.Obj.FOnFoo = null;',
  25809. '$mod.Obj.SetFoo(null);',
  25810. '$mod.Obj.SetEvents(1, null);',
  25811. '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
  25812. '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
  25813. '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
  25814. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  25815. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  25816. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  25817. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  25818. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  25819. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  25820. '$mod.Obj.FOnFoo(1);',
  25821. '$mod.Obj.GetFoo();',
  25822. '$mod.Obj.FOnFoo(1);',
  25823. '$mod.Obj.GetFoo()(1);',
  25824. '$mod.Obj.GetEvents(6)(1);',
  25825. '$mod.b = $mod.Obj.FOnFoo(1) === $mod.vP(1);',
  25826. '$mod.b = $mod.Obj.GetFoo() === $mod.vP(1);',
  25827. '$mod.b = $mod.Obj.FOnFoo(1) === $mod.Obj.FOnFoo(1);',
  25828. '$mod.b = $mod.Obj.GetFoo() === $mod.Obj.FOnFoo(1);',
  25829. '$mod.b = $mod.Obj.FOnFoo(1) !== $mod.Obj.FOnFoo(1);',
  25830. '$mod.b = $mod.Obj.GetFoo() !== $mod.Obj.FOnFoo(1);',
  25831. '$mod.b = $mod.Obj.FOnFoo != null;',
  25832. '$mod.b = $mod.Obj.GetFoo() != null;',
  25833. '$mod.b = $mod.Obj.GetEvents(13) != null;',
  25834. '']));
  25835. end;
  25836. procedure TTestModule.TestProcType_WithClassInstDoPropertyFPC;
  25837. begin
  25838. StartProgram(false);
  25839. Add('type');
  25840. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  25841. Add(' TObject = class');
  25842. Add(' FOnFoo: TFuncInt;');
  25843. Add(' function DoIt(vA: longint = 1): longint;');
  25844. Add(' function GetFoo: TFuncInt;');
  25845. Add(' procedure SetFoo(const Value: TFuncInt);');
  25846. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  25847. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  25848. Add(' end;');
  25849. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  25850. Add('function tobject.getfoo: tfuncint; begin end;');
  25851. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  25852. Add('var');
  25853. Add(' Obj: TObject;');
  25854. Add(' vP: tfuncint;');
  25855. Add(' b: boolean;');
  25856. Add('begin');
  25857. Add('with obj do begin');
  25858. Add(' fonfoo:=nil;');
  25859. Add(' onfoo:=nil;');
  25860. Add(' onbar:=nil;');
  25861. Add(' fonfoo:=fonfoo;');
  25862. Add(' onfoo:=onfoo;');
  25863. Add(' onbar:=onbar;');
  25864. Add(' fonfoo:=@doit;');
  25865. Add(' onfoo:=@doit;');
  25866. Add(' onbar:=@doit;');
  25867. //Add(' fonfoo:=doit;'); // delphi
  25868. //Add(' onfoo:=doit;'); // delphi
  25869. //Add(' onbar:=doit;'); // delphi
  25870. Add(' fonfoo;');
  25871. Add(' onfoo;');
  25872. Add(' onbar;');
  25873. Add(' fonfoo();');
  25874. Add(' onfoo();');
  25875. Add(' onbar();');
  25876. Add(' b:=fonfoo=nil;');
  25877. Add(' b:=onfoo=nil;');
  25878. Add(' b:=onbar=nil;');
  25879. Add(' b:=fonfoo<>nil;');
  25880. Add(' b:=onfoo<>nil;');
  25881. Add(' b:=onbar<>nil;');
  25882. Add(' b:=fonfoo=vp;');
  25883. Add(' b:=onfoo=vp;');
  25884. Add(' b:=onbar=vp;');
  25885. Add(' b:=fonfoo=fonfoo;');
  25886. Add(' b:=onfoo=onfoo;');
  25887. Add(' b:=onbar=onfoo;');
  25888. Add(' b:=fonfoo<>fonfoo;');
  25889. Add(' b:=onfoo<>onfoo;');
  25890. Add(' b:=onbar<>onfoo;');
  25891. Add(' b:=fonfoo=@doit;');
  25892. Add(' b:=onfoo=@doit;');
  25893. Add(' b:=onbar=@doit;');
  25894. Add(' b:=fonfoo<>@doit;');
  25895. Add(' b:=onfoo<>@doit;');
  25896. Add(' b:=onbar<>@doit;');
  25897. Add(' b:=Assigned(fonfoo);');
  25898. Add(' b:=Assigned(onfoo);');
  25899. Add(' b:=Assigned(onbar);');
  25900. Add('end;');
  25901. ConvertProgram;
  25902. CheckSource('TestProcType_WithClassInstDoPropertyFPC',
  25903. LinesToStr([ // statements
  25904. 'rtl.createClass(this, "TObject", null, function () {',
  25905. ' this.$init = function () {',
  25906. ' this.FOnFoo = null;',
  25907. ' };',
  25908. ' this.$final = function () {',
  25909. ' this.FOnFoo = undefined;',
  25910. ' };',
  25911. ' this.DoIt = function (vA) {',
  25912. ' var Result = 0;',
  25913. ' return Result;',
  25914. ' };',
  25915. ' this.GetFoo = function () {',
  25916. ' var Result = null;',
  25917. ' return Result;',
  25918. ' };',
  25919. ' this.SetFoo = function (Value) {',
  25920. ' };',
  25921. '});',
  25922. 'this.Obj = null;',
  25923. 'this.vP = null;',
  25924. 'this.b = false;'
  25925. ]),
  25926. LinesToStr([
  25927. 'var $with = $mod.Obj;',
  25928. '$with.FOnFoo = null;',
  25929. '$with.FOnFoo = null;',
  25930. '$with.SetFoo(null);',
  25931. '$with.FOnFoo = $with.FOnFoo;',
  25932. '$with.FOnFoo = $with.FOnFoo;',
  25933. '$with.SetFoo($with.GetFoo());',
  25934. '$with.FOnFoo = rtl.createCallback($with, "DoIt");',
  25935. '$with.FOnFoo = rtl.createCallback($with, "DoIt");',
  25936. '$with.SetFoo(rtl.createCallback($with, "DoIt"));',
  25937. '$with.FOnFoo(1);',
  25938. '$with.FOnFoo(1);',
  25939. '$with.GetFoo();',
  25940. '$with.FOnFoo(1);',
  25941. '$with.FOnFoo(1);',
  25942. '$with.GetFoo()(1);',
  25943. '$mod.b = $with.FOnFoo === null;',
  25944. '$mod.b = $with.FOnFoo === null;',
  25945. '$mod.b = $with.GetFoo() === null;',
  25946. '$mod.b = $with.FOnFoo !== null;',
  25947. '$mod.b = $with.FOnFoo !== null;',
  25948. '$mod.b = $with.GetFoo() !== null;',
  25949. '$mod.b = rtl.eqCallback($with.FOnFoo, $mod.vP);',
  25950. '$mod.b = rtl.eqCallback($with.FOnFoo, $mod.vP);',
  25951. '$mod.b = rtl.eqCallback($with.GetFoo(), $mod.vP);',
  25952. '$mod.b = rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  25953. '$mod.b = rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  25954. '$mod.b = rtl.eqCallback($with.GetFoo(), $with.FOnFoo);',
  25955. '$mod.b = !rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  25956. '$mod.b = !rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  25957. '$mod.b = !rtl.eqCallback($with.GetFoo(), $with.FOnFoo);',
  25958. '$mod.b = rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  25959. '$mod.b = rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  25960. '$mod.b = rtl.eqCallback($with.GetFoo(), rtl.createCallback($with, "DoIt"));',
  25961. '$mod.b = !rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  25962. '$mod.b = !rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  25963. '$mod.b = !rtl.eqCallback($with.GetFoo(), rtl.createCallback($with, "DoIt"));',
  25964. '$mod.b = $with.FOnFoo != null;',
  25965. '$mod.b = $with.FOnFoo != null;',
  25966. '$mod.b = $with.GetFoo() != null;',
  25967. '']));
  25968. end;
  25969. procedure TTestModule.TestProcType_Nested;
  25970. begin
  25971. StartProgram(false);
  25972. Add([
  25973. 'type',
  25974. ' TProcInt = procedure(vI: longint = 1);',
  25975. 'procedure DoIt(vJ: longint);',
  25976. 'var aProc: TProcInt;',
  25977. ' b: boolean;',
  25978. ' procedure Sub(vK: longint);',
  25979. ' var aSub: TProcInt;',
  25980. ' procedure SubSub(vK: longint);',
  25981. ' var aSubSub: TProcInt;',
  25982. ' begin;',
  25983. ' aProc:=@DoIt;',
  25984. ' aSub:=@DoIt;',
  25985. ' aSubSub:=@DoIt;',
  25986. ' aProc:=@Sub;',
  25987. ' aSub:=@Sub;',
  25988. ' aSubSub:=@Sub;',
  25989. ' aProc:=@SubSub;',
  25990. ' aSub:=@SubSub;',
  25991. ' aSubSub:=@SubSub;',
  25992. ' end;',
  25993. ' begin;',
  25994. ' end;',
  25995. 'begin;',
  25996. ' aProc:=@Sub;',
  25997. ' b:=aProc=@Sub;',
  25998. ' b:=@Sub=aProc;',
  25999. 'end;',
  26000. 'begin',
  26001. '']);
  26002. ConvertProgram;
  26003. CheckSource('TestProcType_Nested',
  26004. LinesToStr([ // statements
  26005. 'this.DoIt = function (vJ) {',
  26006. ' var aProc = null;',
  26007. ' var b = false;',
  26008. ' function Sub(vK) {',
  26009. ' var aSub = null;',
  26010. ' function SubSub(vK) {',
  26011. ' var aSubSub = null;',
  26012. ' aProc = $mod.DoIt;',
  26013. ' aSub = $mod.DoIt;',
  26014. ' aSubSub = $mod.DoIt;',
  26015. ' aProc = Sub;',
  26016. ' aSub = Sub;',
  26017. ' aSubSub = Sub;',
  26018. ' aProc = SubSub;',
  26019. ' aSub = SubSub;',
  26020. ' aSubSub = SubSub;',
  26021. ' };',
  26022. ' };',
  26023. ' aProc = Sub;',
  26024. ' b = rtl.eqCallback(aProc, Sub);',
  26025. ' b = rtl.eqCallback(Sub, aProc);',
  26026. '};',
  26027. '']),
  26028. LinesToStr([ // $mod.$main
  26029. '']));
  26030. end;
  26031. procedure TTestModule.TestProcType_NestedOfObject;
  26032. begin
  26033. StartProgram(false);
  26034. Add([
  26035. 'type',
  26036. ' TProcInt = procedure(vI: longint = 1) of object;',
  26037. ' TObject = class',
  26038. ' procedure DoIt(vJ: longint);',
  26039. ' end;',
  26040. 'procedure TObject.DoIt(vJ: longint);',
  26041. 'var aProc: TProcInt;',
  26042. ' b: boolean;',
  26043. ' procedure Sub(vK: longint);',
  26044. ' var aSub: TProcInt;',
  26045. ' procedure SubSub(vK: longint);',
  26046. ' var aSubSub: TProcInt;',
  26047. ' begin;',
  26048. ' aProc:=@DoIt;',
  26049. ' aSub:=@DoIt;',
  26050. ' aSubSub:=@DoIt;',
  26051. ' aProc:=@Sub;',
  26052. ' aSub:=@Sub;',
  26053. ' aSubSub:=@Sub;',
  26054. ' aProc:=@SubSub;',
  26055. ' aSub:=@SubSub;',
  26056. ' aSubSub:=@SubSub;',
  26057. ' end;',
  26058. ' begin;',
  26059. ' end;',
  26060. 'begin;',
  26061. ' aProc:=@Sub;',
  26062. ' b:=aProc=@Sub;',
  26063. ' b:=@Sub=aProc;',
  26064. 'end;',
  26065. 'begin',
  26066. '']);
  26067. ConvertProgram;
  26068. CheckSource('TestProcType_Nested',
  26069. LinesToStr([ // statements
  26070. 'rtl.createClass(this, "TObject", null, function () {',
  26071. ' this.$init = function () {',
  26072. ' };',
  26073. ' this.$final = function () {',
  26074. ' };',
  26075. ' this.DoIt = function (vJ) {',
  26076. ' var $Self = this;',
  26077. ' var aProc = null;',
  26078. ' var b = false;',
  26079. ' function Sub(vK) {',
  26080. ' var aSub = null;',
  26081. ' function SubSub(vK) {',
  26082. ' var aSubSub = null;',
  26083. ' aProc = rtl.createCallback($Self, "DoIt");',
  26084. ' aSub = rtl.createCallback($Self, "DoIt");',
  26085. ' aSubSub = rtl.createCallback($Self, "DoIt");',
  26086. ' aProc = Sub;',
  26087. ' aSub = Sub;',
  26088. ' aSubSub = Sub;',
  26089. ' aProc = SubSub;',
  26090. ' aSub = SubSub;',
  26091. ' aSubSub = SubSub;',
  26092. ' };',
  26093. ' };',
  26094. ' aProc = Sub;',
  26095. ' b = rtl.eqCallback(aProc, Sub);',
  26096. ' b = rtl.eqCallback(Sub, aProc);',
  26097. ' };',
  26098. '});',
  26099. '']),
  26100. LinesToStr([ // $mod.$main
  26101. '']));
  26102. end;
  26103. procedure TTestModule.TestProcType_ReferenceToProc;
  26104. begin
  26105. StartProgram(false);
  26106. Add([
  26107. 'type',
  26108. ' TProcRef = reference to procedure(i: longint = 0);',
  26109. ' TFuncRef = reference to function(i: longint = 0): longint;',
  26110. 'var',
  26111. ' p: TProcRef;',
  26112. ' f: TFuncRef;',
  26113. 'procedure DoIt(i: longint);',
  26114. 'begin',
  26115. 'end;',
  26116. 'function GetIt(i: longint): longint;',
  26117. 'begin',
  26118. ' p:=@DoIt;',
  26119. ' f:=@GetIt;',
  26120. ' f;',
  26121. ' f();',
  26122. ' f(1);',
  26123. 'end;',
  26124. 'begin',
  26125. ' p:=@DoIt;',
  26126. ' f:=@GetIt;',
  26127. ' f;',
  26128. ' f();',
  26129. ' f(1);',
  26130. ' p:=TProcRef(f);',
  26131. '']);
  26132. ConvertProgram;
  26133. CheckSource('TestProcType_ReferenceToProc',
  26134. LinesToStr([ // statements
  26135. 'this.p = null;',
  26136. 'this.f = null;',
  26137. 'this.DoIt = function (i) {',
  26138. '};',
  26139. 'this.GetIt = function (i) {',
  26140. ' var Result = 0;',
  26141. ' $mod.p = $mod.DoIt;',
  26142. ' $mod.f = $mod.GetIt;',
  26143. ' $mod.f(0);',
  26144. ' $mod.f(0);',
  26145. ' $mod.f(1);',
  26146. ' return Result;',
  26147. '};',
  26148. '']),
  26149. LinesToStr([ // $mod.$main
  26150. '$mod.p = $mod.DoIt;',
  26151. '$mod.f = $mod.GetIt;',
  26152. '$mod.f(0);',
  26153. '$mod.f(0);',
  26154. '$mod.f(1);',
  26155. '$mod.p = $mod.f;',
  26156. '']));
  26157. end;
  26158. procedure TTestModule.TestProcType_ReferenceToMethod;
  26159. begin
  26160. StartProgram(false);
  26161. Add([
  26162. 'type',
  26163. ' TFuncRef = reference to function(i: longint = 5): longint;',
  26164. ' TObject = class',
  26165. ' function Grow(s: longint): longint;',
  26166. ' end;',
  26167. 'var',
  26168. ' f: tfuncref;',
  26169. 'function tobject.grow(s: longint): longint;',
  26170. ' function GrowSub(i: longint): longint;',
  26171. ' begin',
  26172. ' f:=@grow;',
  26173. ' f:=@growsub;',
  26174. ' end;',
  26175. 'begin',
  26176. ' f:=@grow;',
  26177. ' f:=@growsub;',
  26178. 'end;',
  26179. 'begin',
  26180. '']);
  26181. ConvertProgram;
  26182. CheckSource('TestProcType_ReferenceToMethod',
  26183. LinesToStr([ // statements
  26184. 'rtl.createClass(this, "TObject", null, function () {',
  26185. ' this.$init = function () {',
  26186. ' };',
  26187. ' this.$final = function () {',
  26188. ' };',
  26189. ' this.Grow = function (s) {',
  26190. ' var $Self = this;',
  26191. ' var Result = 0;',
  26192. ' function GrowSub(i) {',
  26193. ' var Result = 0;',
  26194. ' $mod.f = rtl.createCallback($Self, "Grow");',
  26195. ' $mod.f = GrowSub;',
  26196. ' return Result;',
  26197. ' };',
  26198. ' $mod.f = rtl.createCallback($Self, "Grow");',
  26199. ' $mod.f = GrowSub;',
  26200. ' return Result;',
  26201. ' };',
  26202. '});',
  26203. 'this.f = null;',
  26204. '']),
  26205. LinesToStr([ // $mod.$main
  26206. '']));
  26207. end;
  26208. procedure TTestModule.TestProcType_Typecast;
  26209. begin
  26210. StartProgram(false);
  26211. Add([
  26212. 'type',
  26213. ' TNotifyEvent = procedure(Sender: Pointer) of object;',
  26214. ' TEvent = procedure of object;',
  26215. ' TGetter = function:longint of object;',
  26216. ' TProcA = procedure(i: longint);',
  26217. ' TFuncB = function(i, j: longint): longint;',
  26218. 'procedure DoIt(); varargs; begin end;',
  26219. 'var',
  26220. ' Notify: tnotifyevent;',
  26221. ' Event: tevent;',
  26222. ' Getter: tgetter;',
  26223. ' ProcA: tproca;',
  26224. ' FuncB: tfuncb;',
  26225. ' p: pointer;',
  26226. 'begin',
  26227. ' notify:=tnotifyevent(event);',
  26228. ' event:=tevent(event);',
  26229. ' event:=tevent(notify);',
  26230. ' event:=tevent(getter);',
  26231. ' event:=tevent(proca);',
  26232. ' proca:=tproca(funcb);',
  26233. ' funcb:=tfuncb(funcb);',
  26234. ' funcb:=tfuncb(proca);',
  26235. ' funcb:=tfuncb(getter);',
  26236. ' proca:=tproca(p);',
  26237. ' funcb:=tfuncb(p);',
  26238. ' getter:=tgetter(p);',
  26239. ' p:=pointer(notify);',
  26240. ' p:=notify;',
  26241. ' p:=pointer(proca);',
  26242. ' p:=proca;',
  26243. ' p:=pointer(funcb);',
  26244. ' p:=funcb;',
  26245. ' doit(Pointer(notify),pointer(event),pointer(proca));',
  26246. '']);
  26247. ConvertProgram;
  26248. CheckSource('TestProcType_Typecast',
  26249. LinesToStr([ // statements
  26250. 'this.DoIt = function () {',
  26251. '};',
  26252. 'this.Notify = null;',
  26253. 'this.Event = null;',
  26254. 'this.Getter = null;',
  26255. 'this.ProcA = null;',
  26256. 'this.FuncB = null;',
  26257. 'this.p = null;',
  26258. '']),
  26259. LinesToStr([ // $mod.$main
  26260. '$mod.Notify = $mod.Event;',
  26261. '$mod.Event = $mod.Event;',
  26262. '$mod.Event = $mod.Notify;',
  26263. '$mod.Event = $mod.Getter;',
  26264. '$mod.Event = $mod.ProcA;',
  26265. '$mod.ProcA = $mod.FuncB;',
  26266. '$mod.FuncB = $mod.FuncB;',
  26267. '$mod.FuncB = $mod.ProcA;',
  26268. '$mod.FuncB = $mod.Getter;',
  26269. '$mod.ProcA = $mod.p;',
  26270. '$mod.FuncB = $mod.p;',
  26271. '$mod.Getter = $mod.p;',
  26272. '$mod.p = $mod.Notify;',
  26273. '$mod.p = $mod.Notify;',
  26274. '$mod.p = $mod.ProcA;',
  26275. '$mod.p = $mod.ProcA;',
  26276. '$mod.p = $mod.FuncB;',
  26277. '$mod.p = $mod.FuncB;',
  26278. '$mod.DoIt($mod.Notify, $mod.Event, $mod.ProcA);',
  26279. '']));
  26280. end;
  26281. procedure TTestModule.TestProcType_PassProcToUntyped;
  26282. begin
  26283. StartProgram(false);
  26284. Add([
  26285. 'type',
  26286. ' TEvent = procedure of object;',
  26287. ' TFunc = function: longint;',
  26288. 'procedure DoIt(); varargs; begin end;',
  26289. 'procedure DoSome(const a; var b; p: pointer); begin end;',
  26290. 'var',
  26291. ' Event: tevent;',
  26292. ' Func: TFunc;',
  26293. 'begin',
  26294. ' doit(event,func);',
  26295. ' dosome(event,event,event);',
  26296. ' dosome(func,func,func);',
  26297. '']);
  26298. ConvertProgram;
  26299. CheckSource('TestProcType_PassProcToUntyped',
  26300. LinesToStr([ // statements
  26301. 'this.DoIt = function () {',
  26302. '};',
  26303. 'this.DoSome = function (a, b, p) {',
  26304. '};',
  26305. 'this.Event = null;',
  26306. 'this.Func = null;',
  26307. '']),
  26308. LinesToStr([ // $mod.$main
  26309. '$mod.DoIt($mod.Event, $mod.Func);',
  26310. '$mod.DoSome($mod.Event, {',
  26311. ' p: $mod,',
  26312. ' get: function () {',
  26313. ' return this.p.Event;',
  26314. ' },',
  26315. ' set: function (v) {',
  26316. ' this.p.Event = v;',
  26317. ' }',
  26318. '}, $mod.Event);',
  26319. '$mod.DoSome($mod.Func, {',
  26320. ' p: $mod,',
  26321. ' get: function () {',
  26322. ' return this.p.Func;',
  26323. ' },',
  26324. ' set: function (v) {',
  26325. ' this.p.Func = v;',
  26326. ' }',
  26327. '}, $mod.Func);',
  26328. '']));
  26329. end;
  26330. procedure TTestModule.TestProcType_PassProcToArray;
  26331. begin
  26332. StartProgram(false);
  26333. Add([
  26334. 'type',
  26335. ' TFunc = function: longint;',
  26336. ' TArrFunc = array of TFunc;',
  26337. 'procedure DoIt(Arr: TArrFunc); begin end;',
  26338. 'function GetIt: longint; begin end;',
  26339. 'var',
  26340. ' Func: tfunc;',
  26341. 'begin',
  26342. ' doit([]);',
  26343. ' doit([@GetIt]);',
  26344. ' doit([Func]);',
  26345. '']);
  26346. ConvertProgram;
  26347. CheckSource('TestProcType_PassProcToArray',
  26348. LinesToStr([ // statements
  26349. 'this.DoIt = function (Arr) {',
  26350. '};',
  26351. 'this.GetIt = function () {',
  26352. ' var Result = 0;',
  26353. ' return Result;',
  26354. '};',
  26355. 'this.Func = null;',
  26356. '']),
  26357. LinesToStr([ // $mod.$main
  26358. '$mod.DoIt([]);',
  26359. '$mod.DoIt([$mod.GetIt]);',
  26360. '$mod.DoIt([$mod.Func]);',
  26361. '']));
  26362. end;
  26363. procedure TTestModule.TestProcType_SafeCallObjFPC;
  26364. begin
  26365. StartProgram(false);
  26366. Add([
  26367. '{$modeswitch externalclass}',
  26368. 'type',
  26369. ' TProc = reference to procedure(i: longint); safecall;',
  26370. ' TEvent = procedure(i: longint) of object; safecall;',
  26371. ' TExtA = class external name ''ExtObj''',
  26372. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  26373. ' procedure DoSome(Id: longint = 1);',
  26374. ' procedure SetOnClick(const e: TEvent);',
  26375. ' property OnClick: TEvent write SetOnClick;',
  26376. ' class procedure Fly(Id: longint = 1); static;',
  26377. ' procedure SetOnShow(const p: TProc);',
  26378. ' property OnShow: TProc write SetOnShow;',
  26379. ' end;',
  26380. 'procedure Run(i: longint = 1);',
  26381. 'begin',
  26382. 'end;',
  26383. 'var',
  26384. ' Obj: texta;',
  26385. ' e: TEvent;',
  26386. ' p: TProc;',
  26387. 'begin',
  26388. ' e:=e;',
  26389. ' e:[email protected];',
  26390. ' e:[email protected];',
  26391. ' e:=TEvent(@obj.dosome);', // no safecall
  26392. ' obj.OnClick:[email protected];',
  26393. ' obj.OnClick:[email protected];',
  26394. ' obj.setonclick(@obj.doit);',
  26395. ' obj.setonclick(@obj.dosome);',
  26396. ' p:=@Run;',
  26397. ' p:[email protected];',
  26398. ' obj.OnShow:=@Run;',
  26399. ' obj.OnShow:[email protected];',
  26400. ' obj.setOnShow(@Run);',
  26401. ' obj.setOnShow(@TExtA.Fly);',
  26402. ' with obj do begin',
  26403. ' e:=@doit;',
  26404. ' e:=@dosome;',
  26405. ' OnClick:=@doit;',
  26406. ' OnClick:=@dosome;',
  26407. ' setonclick(@doit);',
  26408. ' setonclick(@dosome);',
  26409. ' OnShow:=@Run;',
  26410. ' setOnShow(@Run);',
  26411. ' end;']);
  26412. ConvertProgram;
  26413. CheckSource('TestProcType_SafeCallObjFPC',
  26414. LinesToStr([ // statements
  26415. 'this.Run = function (i) {',
  26416. '};',
  26417. 'this.Obj = null;',
  26418. 'this.e = null;',
  26419. 'this.p = null;',
  26420. '']),
  26421. LinesToStr([ // $mod.$main
  26422. '$mod.e = $mod.e;',
  26423. '$mod.e = rtl.createSafeCallback($mod.Obj, "$Execute");',
  26424. '$mod.e = rtl.createSafeCallback($mod.Obj, "DoSome");',
  26425. '$mod.e = rtl.createCallback($mod.Obj, "DoSome");',
  26426. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  26427. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  26428. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  26429. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  26430. '$mod.p = rtl.createSafeCallback($mod, "Run");',
  26431. '$mod.p = rtl.createSafeCallback(ExtObj, "Fly");',
  26432. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26433. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  26434. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26435. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  26436. 'var $with = $mod.Obj;',
  26437. '$mod.e = rtl.createSafeCallback($with, "$Execute");',
  26438. '$mod.e = rtl.createSafeCallback($with, "DoSome");',
  26439. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  26440. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  26441. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  26442. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  26443. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26444. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26445. '']));
  26446. end;
  26447. procedure TTestModule.TestProcType_SafeCallDelphi;
  26448. begin
  26449. StartProgram(false);
  26450. Add([
  26451. '{$mode delphi}',
  26452. '{$modeswitch externalclass}',
  26453. 'type',
  26454. ' TProc = reference to procedure(i: longint); safecall;',
  26455. ' TEvent = procedure(i: longint) of object; safecall;',
  26456. ' TExtA = class external name ''ExtObj''',
  26457. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  26458. ' procedure DoSome(Id: longint = 1);',
  26459. ' procedure SetOnClick(const e: TEvent);',
  26460. ' property OnClick: TEvent write SetOnClick;',
  26461. ' class procedure Fly(Id: longint = 1); static;',
  26462. ' procedure SetOnShow(const p: TProc);',
  26463. ' property OnShow: TProc write SetOnShow;',
  26464. ' end;',
  26465. 'procedure Run(i: longint = 1);',
  26466. 'begin',
  26467. 'end;',
  26468. 'var',
  26469. ' Obj: texta;',
  26470. ' e: TEvent;',
  26471. ' p: TProc;',
  26472. 'begin',
  26473. ' e:=e;',
  26474. ' e:=obj.doit;',
  26475. ' e:=obj.dosome;',
  26476. ' e:=TEvent(@obj.dosome);', // no safecall
  26477. ' obj.OnClick:=obj.doit;',
  26478. ' obj.OnClick:=obj.dosome;',
  26479. ' obj.setonclick(obj.doit);',
  26480. ' obj.setonclick(obj.dosome);',
  26481. ' p:=Run;',
  26482. ' p:=TExtA.Fly;',
  26483. ' obj.OnShow:=Run;',
  26484. ' obj.OnShow:=TExtA.Fly;',
  26485. ' obj.setOnShow(Run);',
  26486. ' obj.setOnShow(TExtA.Fly);',
  26487. ' with obj do begin',
  26488. ' e:=doit;',
  26489. ' e:=dosome;',
  26490. ' OnClick:=doit;',
  26491. ' OnClick:=dosome;',
  26492. ' setonclick(doit);',
  26493. ' setonclick(dosome);',
  26494. ' OnShow:=@Run;',
  26495. ' setOnShow(@Run);',
  26496. ' end;']);
  26497. ConvertProgram;
  26498. CheckSource('TestProcType_SafeCallDelphi',
  26499. LinesToStr([ // statements
  26500. 'this.Run = function (i) {',
  26501. '};',
  26502. 'this.Obj = null;',
  26503. 'this.e = null;',
  26504. 'this.p = null;',
  26505. '']),
  26506. LinesToStr([ // $mod.$main
  26507. '$mod.e = $mod.e;',
  26508. '$mod.e = rtl.createSafeCallback($mod.Obj, "$Execute");',
  26509. '$mod.e = rtl.createSafeCallback($mod.Obj, "DoSome");',
  26510. '$mod.e = rtl.createCallback($mod.Obj, "DoSome");',
  26511. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  26512. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  26513. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  26514. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  26515. '$mod.p = rtl.createSafeCallback($mod, "Run");',
  26516. '$mod.p = rtl.createSafeCallback(ExtObj, "Fly");',
  26517. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26518. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  26519. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26520. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  26521. 'var $with = $mod.Obj;',
  26522. '$mod.e = rtl.createSafeCallback($with, "$Execute");',
  26523. '$mod.e = rtl.createSafeCallback($with, "DoSome");',
  26524. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  26525. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  26526. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  26527. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  26528. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26529. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26530. '']));
  26531. end;
  26532. procedure TTestModule.TestPointer;
  26533. begin
  26534. StartProgram(false);
  26535. Add(['type',
  26536. ' TObject = class end;',
  26537. ' TClass = class of TObject;',
  26538. ' TArrInt = array of longint;',
  26539. 'const',
  26540. ' n = nil;',
  26541. 'var',
  26542. ' v: jsvalue;',
  26543. ' Obj: tobject;',
  26544. ' C: tclass;',
  26545. ' a: tarrint;',
  26546. ' p: Pointer = nil;',
  26547. ' s: string;',
  26548. 'begin',
  26549. ' p:=p;',
  26550. ' p:=nil;',
  26551. ' if p=nil then;',
  26552. ' if nil=p then;',
  26553. ' if Assigned(p) then;',
  26554. ' p:=Pointer(v);',
  26555. ' p:=obj;',
  26556. ' p:=c;',
  26557. ' p:=a;',
  26558. ' p:=tobject;',
  26559. ' obj:=TObject(p);',
  26560. ' c:=TClass(p);',
  26561. ' a:=TArrInt(p);',
  26562. ' p:=n;',
  26563. ' p:=Pointer(a);',
  26564. ' p:=pointer(s);',
  26565. ' s:=string(p);',
  26566. '']);
  26567. ConvertProgram;
  26568. CheckSource('TestPointer',
  26569. LinesToStr([ // statements
  26570. 'rtl.createClass(this, "TObject", null, function () {',
  26571. ' this.$init = function () {',
  26572. ' };',
  26573. ' this.$final = function () {',
  26574. ' };',
  26575. '});',
  26576. 'this.n = null;',
  26577. 'this.v = undefined;',
  26578. 'this.Obj = null;',
  26579. 'this.C = null;',
  26580. 'this.a = [];',
  26581. 'this.p = null;',
  26582. 'this.s = "";',
  26583. '']),
  26584. LinesToStr([ // $mod.$main
  26585. '$mod.p = $mod.p;',
  26586. '$mod.p = null;',
  26587. 'if ($mod.p === null) ;',
  26588. 'if (null === $mod.p) ;',
  26589. 'if ($mod.p != null) ;',
  26590. '$mod.p = $mod.v;',
  26591. '$mod.p = $mod.Obj;',
  26592. '$mod.p = $mod.C;',
  26593. '$mod.p = $mod.a;',
  26594. '$mod.p = $mod.TObject;',
  26595. '$mod.Obj = $mod.p;',
  26596. '$mod.C = $mod.p;',
  26597. '$mod.a = $mod.p;',
  26598. '$mod.p = null;',
  26599. '$mod.p = $mod.a;',
  26600. '$mod.p = $mod.s;',
  26601. '$mod.s = $mod.p;',
  26602. '']));
  26603. end;
  26604. procedure TTestModule.TestPointer_Proc;
  26605. begin
  26606. StartProgram(false);
  26607. Add('type');
  26608. Add(' TObject = class');
  26609. Add(' procedure DoIt; virtual; abstract;');
  26610. Add(' end;');
  26611. Add('procedure DoSome; begin end;');
  26612. Add('var');
  26613. Add(' o: TObject;');
  26614. Add(' p: Pointer;');
  26615. Add('begin');
  26616. Add(' p:=@DoSome;');
  26617. Add(' p:[email protected];');
  26618. ConvertProgram;
  26619. CheckSource('TestPointer_Proc',
  26620. LinesToStr([ // statements
  26621. 'rtl.createClass(this, "TObject", null, function () {',
  26622. ' this.$init = function () {',
  26623. ' };',
  26624. ' this.$final = function () {',
  26625. ' };',
  26626. '});',
  26627. 'this.DoSome = function () {',
  26628. '};',
  26629. 'this.o = null;',
  26630. 'this.p = null;',
  26631. '']),
  26632. LinesToStr([ // $mod.$main
  26633. '$mod.p = $mod.DoSome;',
  26634. '$mod.p = rtl.createCallback($mod.o, "DoIt");',
  26635. '']));
  26636. end;
  26637. procedure TTestModule.TestPointer_AssignRecordFail;
  26638. begin
  26639. StartProgram(false);
  26640. Add('type');
  26641. Add(' TRec = record end;');
  26642. Add('var');
  26643. Add(' p: Pointer;');
  26644. Add(' r: TRec;');
  26645. Add('begin');
  26646. Add(' p:=r;');
  26647. SetExpectedPasResolverError('Incompatible types: got "TRec" expected "Pointer"',
  26648. nIncompatibleTypesGotExpected);
  26649. ConvertProgram;
  26650. end;
  26651. procedure TTestModule.TestPointer_AssignStaticArrayFail;
  26652. begin
  26653. StartProgram(false);
  26654. Add('type');
  26655. Add(' TArr = array[boolean] of longint;');
  26656. Add('var');
  26657. Add(' p: Pointer;');
  26658. Add(' a: TArr;');
  26659. Add('begin');
  26660. Add(' p:=a;');
  26661. SetExpectedPasResolverError('Incompatible types: got "TArr" expected "Pointer"',
  26662. nIncompatibleTypesGotExpected);
  26663. ConvertProgram;
  26664. end;
  26665. procedure TTestModule.TestPointer_TypeCastJSValueToPointer;
  26666. begin
  26667. StartProgram(false);
  26668. Add([
  26669. 'procedure DoIt(args: array of jsvalue); begin end;',
  26670. 'procedure DoAll; varargs; begin end;',
  26671. 'var',
  26672. ' v: jsvalue;',
  26673. 'begin',
  26674. ' DoIt([pointer(v)]);',
  26675. ' DoAll(pointer(v));',
  26676. '']);
  26677. ConvertProgram;
  26678. CheckSource('TestPointer_TypeCastJSValueToPointer',
  26679. LinesToStr([ // statements
  26680. 'this.DoIt = function (args) {',
  26681. '};',
  26682. 'this.DoAll = function () {',
  26683. '};',
  26684. 'this.v = undefined;',
  26685. '']),
  26686. LinesToStr([ // $mod.$main
  26687. '$mod.DoIt([$mod.v]);',
  26688. '$mod.DoAll($mod.v);',
  26689. '']));
  26690. end;
  26691. procedure TTestModule.TestPointer_NonRecordFail;
  26692. begin
  26693. StartProgram(false);
  26694. Add([
  26695. 'type',
  26696. ' p = ^longint;',
  26697. 'begin',
  26698. '']);
  26699. SetExpectedPasResolverError('Not supported: pointer of Longint',nNotSupportedX);
  26700. ConvertProgram;
  26701. end;
  26702. procedure TTestModule.TestPointer_AnonymousArgTypeFail;
  26703. begin
  26704. StartProgram(false);
  26705. Add([
  26706. 'procedure DoIt(p: ^longint); begin end;',
  26707. 'begin',
  26708. '']);
  26709. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  26710. ConvertProgram;
  26711. end;
  26712. procedure TTestModule.TestPointer_AnonymousVarTypeFail;
  26713. begin
  26714. StartProgram(false);
  26715. Add([
  26716. 'var p: ^longint;',
  26717. 'begin',
  26718. '']);
  26719. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  26720. ConvertProgram;
  26721. end;
  26722. procedure TTestModule.TestPointer_AnonymousResultTypeFail;
  26723. begin
  26724. StartProgram(false);
  26725. Add([
  26726. 'function DoIt: ^longint; begin end;',
  26727. 'begin',
  26728. '']);
  26729. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  26730. ConvertProgram;
  26731. end;
  26732. procedure TTestModule.TestPointer_AddrOperatorFail;
  26733. begin
  26734. StartProgram(false);
  26735. Add([
  26736. 'var i: longint;',
  26737. 'begin',
  26738. ' if @i=nil then ;',
  26739. '']);
  26740. SetExpectedConverterError('illegal qualifier "@" in front of "i:Longint"',nIllegalQualifierInFrontOf);
  26741. ConvertProgram;
  26742. end;
  26743. procedure TTestModule.TestPointer_ArrayParamsFail;
  26744. begin
  26745. StartProgram(false);
  26746. Add([
  26747. 'var',
  26748. ' p: Pointer;',
  26749. 'begin',
  26750. ' p:=p[1];',
  26751. '']);
  26752. SetExpectedPasResolverError('illegal qualifier "[" after "Pointer"',nIllegalQualifierAfter);
  26753. ConvertProgram;
  26754. end;
  26755. procedure TTestModule.TestPointer_PointerAddFail;
  26756. begin
  26757. StartProgram(false);
  26758. Add([
  26759. 'var',
  26760. ' p: Pointer;',
  26761. 'begin',
  26762. ' p:=p+1;',
  26763. '']);
  26764. SetExpectedPasResolverError('Operator is not overloaded: "Pointer" + "Longint"',nOperatorIsNotOverloadedAOpB);
  26765. ConvertProgram;
  26766. end;
  26767. procedure TTestModule.TestPointer_IncPointerFail;
  26768. begin
  26769. StartProgram(false);
  26770. Add([
  26771. 'var',
  26772. ' p: Pointer;',
  26773. 'begin',
  26774. ' inc(p,1);',
  26775. '']);
  26776. SetExpectedPasResolverError('Incompatible type arg no. 1: Got "Pointer", expected "integer"',
  26777. nIncompatibleTypeArgNo);
  26778. ConvertProgram;
  26779. end;
  26780. procedure TTestModule.TestPointer_Record;
  26781. begin
  26782. StartProgram(false);
  26783. Add([
  26784. 'type',
  26785. ' TRec = record x: longint; end;',
  26786. ' PRec = ^TRec;',
  26787. 'var',
  26788. ' r: TRec;',
  26789. ' p: PRec;',
  26790. ' q: ^TRec;',
  26791. ' Ptr: pointer;',
  26792. 'begin',
  26793. ' new(p);',
  26794. ' p:=@r;',
  26795. ' r:=p^;',
  26796. ' r.x:=p^.x;',
  26797. ' p^.x:=r.x;',
  26798. ' if p^.x=3 then ;',
  26799. ' if 4=p^.x then ;',
  26800. ' dispose(p);',
  26801. ' new(q);',
  26802. ' dispose(q);',
  26803. ' Ptr:=p;',
  26804. ' p:=PRec(ptr);',
  26805. '']);
  26806. ConvertProgram;
  26807. CheckSource('TestPointer_Record',
  26808. LinesToStr([ // statements
  26809. 'rtl.recNewT(this, "TRec", function () {',
  26810. ' this.x = 0;',
  26811. ' this.$eq = function (b) {',
  26812. ' return this.x === b.x;',
  26813. ' };',
  26814. ' this.$assign = function (s) {',
  26815. ' this.x = s.x;',
  26816. ' return this;',
  26817. ' };',
  26818. '});',
  26819. 'this.r = this.TRec.$new();',
  26820. 'this.p = null;',
  26821. 'this.q = null;',
  26822. 'this.Ptr = null;',
  26823. '']),
  26824. LinesToStr([ // $mod.$main
  26825. '$mod.p = $mod.TRec.$new();',
  26826. '$mod.p = $mod.r;',
  26827. '$mod.r.$assign($mod.p);',
  26828. '$mod.r.x = $mod.p.x;',
  26829. '$mod.p.x = $mod.r.x;',
  26830. 'if ($mod.p.x === 3) ;',
  26831. 'if (4 === $mod.p.x) ;',
  26832. '$mod.p = null;',
  26833. '$mod.q = $mod.TRec.$new();',
  26834. '$mod.q = null;',
  26835. '$mod.Ptr = $mod.p;',
  26836. '$mod.p = $mod.Ptr;',
  26837. '']));
  26838. end;
  26839. procedure TTestModule.TestPointer_RecordArg;
  26840. begin
  26841. StartProgram(false);
  26842. Add([
  26843. '{$modeswitch autoderef}',
  26844. 'type',
  26845. ' TRec = record x: longint; end;',
  26846. ' PRec = ^TRec;',
  26847. 'function DoIt(const a: PRec; var b: PRec; out c: PRec): TRec;',
  26848. 'begin',
  26849. ' a.x:=a.x;',
  26850. ' a^.x:=a^.x;',
  26851. ' with a^ do',
  26852. ' x:=x;',
  26853. 'end;',
  26854. 'function GetIt(p: PRec): PRec;',
  26855. 'begin',
  26856. ' p.x:=p.x;',
  26857. ' p^.x:=p^.x;',
  26858. ' with p^ do',
  26859. ' x:=x;',
  26860. 'end;',
  26861. 'var',
  26862. ' r: TRec;',
  26863. ' p: PRec;',
  26864. 'begin',
  26865. ' p:=GetIt(p);',
  26866. ' p^:=GetIt(@r)^;',
  26867. ' DoIt(p,p,p);',
  26868. ' DoIt(@r,p,p);',
  26869. '']);
  26870. ConvertProgram;
  26871. CheckSource('TestPointer_RecordArg',
  26872. LinesToStr([ // statements
  26873. 'rtl.recNewT(this, "TRec", function () {',
  26874. ' this.x = 0;',
  26875. ' this.$eq = function (b) {',
  26876. ' return this.x === b.x;',
  26877. ' };',
  26878. ' this.$assign = function (s) {',
  26879. ' this.x = s.x;',
  26880. ' return this;',
  26881. ' };',
  26882. '});',
  26883. 'this.DoIt = function (a, b, c) {',
  26884. ' var Result = $mod.TRec.$new();',
  26885. ' a.x = a.x;',
  26886. ' a.x = a.x;',
  26887. ' a.x = a.x;',
  26888. ' return Result;',
  26889. '};',
  26890. 'this.GetIt = function (p) {',
  26891. ' var Result = null;',
  26892. ' p.x = p.x;',
  26893. ' p.x = p.x;',
  26894. ' p.x = p.x;',
  26895. ' return Result;',
  26896. '};',
  26897. 'this.r = this.TRec.$new();',
  26898. 'this.p = null;',
  26899. '']),
  26900. LinesToStr([ // $mod.$main
  26901. '$mod.p = $mod.GetIt($mod.p);',
  26902. '$mod.p.$assign($mod.GetIt($mod.r));',
  26903. '$mod.DoIt($mod.p, {',
  26904. ' p: $mod,',
  26905. ' get: function () {',
  26906. ' return this.p.p;',
  26907. ' },',
  26908. ' set: function (v) {',
  26909. ' this.p.p = v;',
  26910. ' }',
  26911. '}, {',
  26912. ' p: $mod,',
  26913. ' get: function () {',
  26914. ' return this.p.p;',
  26915. ' },',
  26916. ' set: function (v) {',
  26917. ' this.p.p = v;',
  26918. ' }',
  26919. '});',
  26920. '$mod.DoIt($mod.r, {',
  26921. ' p: $mod,',
  26922. ' get: function () {',
  26923. ' return this.p.p;',
  26924. ' },',
  26925. ' set: function (v) {',
  26926. ' this.p.p = v;',
  26927. ' }',
  26928. '}, {',
  26929. ' p: $mod,',
  26930. ' get: function () {',
  26931. ' return this.p.p;',
  26932. ' },',
  26933. ' set: function (v) {',
  26934. ' this.p.p = v;',
  26935. ' }',
  26936. '});',
  26937. '']));
  26938. end;
  26939. procedure TTestModule.TestJSValue_AssignToJSValue;
  26940. begin
  26941. StartProgram(false);
  26942. Add('var');
  26943. Add(' v: jsvalue;');
  26944. Add(' i: longint;');
  26945. Add(' s: string;');
  26946. Add(' b: boolean;');
  26947. Add(' d: double;');
  26948. Add(' p: pointer;');
  26949. Add('begin');
  26950. Add(' v:=v;');
  26951. Add(' v:=1;');
  26952. Add(' v:=i;');
  26953. Add(' v:='''';');
  26954. Add(' v:=''c'';');
  26955. Add(' v:=''foo'';');
  26956. Add(' v:=s;');
  26957. Add(' v:=false;');
  26958. Add(' v:=true;');
  26959. Add(' v:=b;');
  26960. Add(' v:=0.1;');
  26961. Add(' v:=d;');
  26962. Add(' v:=nil;');
  26963. Add(' v:=p;');
  26964. ConvertProgram;
  26965. CheckSource('TestJSValue_AssignToJSValue',
  26966. LinesToStr([ // statements
  26967. 'this.v = undefined;',
  26968. 'this.i = 0;',
  26969. 'this.s = "";',
  26970. 'this.b = false;',
  26971. 'this.d = 0.0;',
  26972. 'this.p = null;',
  26973. '']),
  26974. LinesToStr([ // $mod.$main
  26975. '$mod.v = $mod.v;',
  26976. '$mod.v = 1;',
  26977. '$mod.v = $mod.i;',
  26978. '$mod.v = "";',
  26979. '$mod.v = "c";',
  26980. '$mod.v = "foo";',
  26981. '$mod.v = $mod.s;',
  26982. '$mod.v = false;',
  26983. '$mod.v = true;',
  26984. '$mod.v = $mod.b;',
  26985. '$mod.v = 0.1;',
  26986. '$mod.v = $mod.d;',
  26987. '$mod.v = null;',
  26988. '$mod.v = $mod.p;',
  26989. '']));
  26990. end;
  26991. procedure TTestModule.TestJSValue_TypeCastToBaseType;
  26992. begin
  26993. StartProgram(false);
  26994. Add('type');
  26995. Add(' integer = longint;');
  26996. Add(' TYesNo = boolean;');
  26997. Add(' TFloat = double;');
  26998. Add(' TCaption = string;');
  26999. Add(' TChar = char;');
  27000. Add('var');
  27001. Add(' v: jsvalue;');
  27002. Add(' i: integer;');
  27003. Add(' s: TCaption;');
  27004. Add(' b: TYesNo;');
  27005. Add(' d: TFloat;');
  27006. Add(' c: char;');
  27007. Add('begin');
  27008. Add(' i:=longint(v);');
  27009. Add(' i:=integer(v);');
  27010. Add(' s:=string(v);');
  27011. Add(' s:=TCaption(v);');
  27012. Add(' b:=boolean(v);');
  27013. Add(' b:=TYesNo(v);');
  27014. Add(' d:=double(v);');
  27015. Add(' d:=TFloat(v);');
  27016. Add(' c:=char(v);');
  27017. Add(' c:=TChar(v);');
  27018. ConvertProgram;
  27019. CheckSource('TestJSValue_TypeCastToBaseType',
  27020. LinesToStr([ // statements
  27021. 'this.v = undefined;',
  27022. 'this.i = 0;',
  27023. 'this.s = "";',
  27024. 'this.b = false;',
  27025. 'this.d = 0.0;',
  27026. 'this.c = "";',
  27027. '']),
  27028. LinesToStr([ // $mod.$main
  27029. '$mod.i = rtl.trunc($mod.v);',
  27030. '$mod.i = rtl.trunc($mod.v);',
  27031. '$mod.s = "" + $mod.v;',
  27032. '$mod.s = "" + $mod.v;',
  27033. '$mod.b = !($mod.v == false);',
  27034. '$mod.b = !($mod.v == false);',
  27035. '$mod.d = rtl.getNumber($mod.v);',
  27036. '$mod.d = rtl.getNumber($mod.v);',
  27037. '$mod.c = rtl.getChar($mod.v);',
  27038. '$mod.c = rtl.getChar($mod.v);',
  27039. '']));
  27040. end;
  27041. procedure TTestModule.TestJSValue_TypecastToJSValue;
  27042. begin
  27043. StartProgram(false);
  27044. Add([
  27045. 'type',
  27046. ' TArr = array of word;',
  27047. ' TRec = record end;',
  27048. ' TSet = set of boolean;',
  27049. 'procedure Fly(v: jsvalue);',
  27050. 'begin',
  27051. 'end;',
  27052. 'var',
  27053. ' a: TArr;',
  27054. ' r: TRec;',
  27055. ' s: TSet;',
  27056. 'begin',
  27057. ' Fly(jsvalue(a));',
  27058. ' Fly(jsvalue(r));',
  27059. ' Fly(jsvalue(s));',
  27060. '']);
  27061. ConvertProgram;
  27062. CheckSource('TestJSValue_TypecastToJSValue',
  27063. LinesToStr([ // statements
  27064. 'rtl.recNewT(this, "TRec", function () {',
  27065. ' this.$eq = function (b) {',
  27066. ' return true;',
  27067. ' };',
  27068. ' this.$assign = function (s) {',
  27069. ' return this;',
  27070. ' };',
  27071. '});',
  27072. 'this.Fly = function (v) {',
  27073. '};',
  27074. 'this.a = [];',
  27075. 'this.r = this.TRec.$new();',
  27076. 'this.s = {};',
  27077. '']),
  27078. LinesToStr([ // $mod.$main
  27079. '$mod.Fly($mod.a);',
  27080. '$mod.Fly($mod.r);',
  27081. '$mod.Fly($mod.s);',
  27082. '']));
  27083. end;
  27084. procedure TTestModule.TestJSValue_Equal;
  27085. begin
  27086. StartProgram(false);
  27087. Add('type');
  27088. Add(' integer = longint;');
  27089. Add(' TYesNo = boolean;');
  27090. Add(' TFloat = double;');
  27091. Add(' TCaption = string;');
  27092. Add(' TChar = char;');
  27093. Add(' TMulti = JSValue;');
  27094. Add('var');
  27095. Add(' v: jsvalue;');
  27096. Add(' i: integer;');
  27097. Add(' s: TCaption;');
  27098. Add(' b: TYesNo;');
  27099. Add(' d: TFloat;');
  27100. Add(' c: char;');
  27101. Add(' m: TMulti;');
  27102. Add('begin');
  27103. Add(' b:=v=v;');
  27104. Add(' b:=v<>v;');
  27105. Add(' b:=v=1;');
  27106. Add(' b:=v<>1;');
  27107. Add(' b:=2=v;');
  27108. Add(' b:=2<>v;');
  27109. Add(' b:=v=i;');
  27110. Add(' b:=i=v;');
  27111. Add(' b:=v=nil;');
  27112. Add(' b:=nil=v;');
  27113. Add(' b:=v=false;');
  27114. Add(' b:=true=v;');
  27115. Add(' b:=v=b;');
  27116. Add(' b:=b=v;');
  27117. Add(' b:=v=s;');
  27118. Add(' b:=s=v;');
  27119. Add(' b:=v=''foo'';');
  27120. Add(' b:=''''=v;');
  27121. Add(' b:=v=d;');
  27122. Add(' b:=d=v;');
  27123. Add(' b:=v=3.4;');
  27124. Add(' b:=5.6=v;');
  27125. Add(' b:=v=c;');
  27126. Add(' b:=c=v;');
  27127. Add(' b:=m=m;');
  27128. Add(' b:=v=m;');
  27129. Add(' b:=m=v;');
  27130. ConvertProgram;
  27131. CheckSource('TestJSValue_Equal',
  27132. LinesToStr([ // statements
  27133. 'this.v = undefined;',
  27134. 'this.i = 0;',
  27135. 'this.s = "";',
  27136. 'this.b = false;',
  27137. 'this.d = 0.0;',
  27138. 'this.c = "";',
  27139. 'this.m = undefined;',
  27140. '']),
  27141. LinesToStr([ // $mod.$main
  27142. '$mod.b = $mod.v == $mod.v;',
  27143. '$mod.b = $mod.v != $mod.v;',
  27144. '$mod.b = $mod.v == 1;',
  27145. '$mod.b = $mod.v != 1;',
  27146. '$mod.b = 2 == $mod.v;',
  27147. '$mod.b = 2 != $mod.v;',
  27148. '$mod.b = $mod.v == $mod.i;',
  27149. '$mod.b = $mod.i == $mod.v;',
  27150. '$mod.b = $mod.v == null;',
  27151. '$mod.b = null == $mod.v;',
  27152. '$mod.b = $mod.v == false;',
  27153. '$mod.b = true == $mod.v;',
  27154. '$mod.b = $mod.v == $mod.b;',
  27155. '$mod.b = $mod.b == $mod.v;',
  27156. '$mod.b = $mod.v == $mod.s;',
  27157. '$mod.b = $mod.s == $mod.v;',
  27158. '$mod.b = $mod.v == "foo";',
  27159. '$mod.b = "" == $mod.v;',
  27160. '$mod.b = $mod.v == $mod.d;',
  27161. '$mod.b = $mod.d == $mod.v;',
  27162. '$mod.b = $mod.v == 3.4;',
  27163. '$mod.b = 5.6 == $mod.v;',
  27164. '$mod.b = $mod.v == $mod.c;',
  27165. '$mod.b = $mod.c == $mod.v;',
  27166. '$mod.b = $mod.m == $mod.m;',
  27167. '$mod.b = $mod.v == $mod.m;',
  27168. '$mod.b = $mod.m == $mod.v;',
  27169. '']));
  27170. end;
  27171. procedure TTestModule.TestJSValue_If;
  27172. begin
  27173. StartProgram(false);
  27174. Add([
  27175. 'procedure Fly(var u);',
  27176. 'begin',
  27177. ' if jsvalue(u) then ;',
  27178. 'end;',
  27179. 'var',
  27180. ' v: jsvalue;',
  27181. 'begin',
  27182. ' if v then ;',
  27183. ' while v do ;',
  27184. ' repeat until v;',
  27185. '']);
  27186. ConvertProgram;
  27187. CheckSource('TestJSValue_If',
  27188. LinesToStr([ // statements
  27189. 'this.Fly = function (u) {',
  27190. ' if (u.get()) ;',
  27191. '};',
  27192. 'this.v = undefined;',
  27193. '']),
  27194. LinesToStr([ // $mod.$main
  27195. 'if ($mod.v) ;',
  27196. 'while($mod.v){',
  27197. '};',
  27198. 'do{',
  27199. '} while(!$mod.v);',
  27200. '']));
  27201. end;
  27202. procedure TTestModule.TestJSValue_Not;
  27203. begin
  27204. StartProgram(false);
  27205. Add([
  27206. 'var',
  27207. ' v: jsvalue;',
  27208. ' b: boolean;',
  27209. 'begin',
  27210. ' b:=not v;',
  27211. ' if not v then ;',
  27212. ' while not v do ;',
  27213. ' repeat until not v;',
  27214. '']);
  27215. ConvertProgram;
  27216. CheckSource('TestJSValue_If',
  27217. LinesToStr([ // statements
  27218. 'this.v = undefined;',
  27219. 'this.b = false;',
  27220. '']),
  27221. LinesToStr([ // $mod.$main
  27222. '$mod.b=!$mod.v;',
  27223. 'if (!$mod.v) ;',
  27224. 'while(!$mod.v){',
  27225. '};',
  27226. 'do{',
  27227. '} while($mod.v);',
  27228. '']));
  27229. end;
  27230. procedure TTestModule.TestJSValue_Enum;
  27231. begin
  27232. StartProgram(false);
  27233. Add('type');
  27234. Add(' TColor = (red, blue);');
  27235. Add(' TRedBlue = TColor;');
  27236. Add('var');
  27237. Add(' v: jsvalue;');
  27238. Add(' e: TColor;');
  27239. Add('begin');
  27240. Add(' v:=e;');
  27241. Add(' v:=TColor(e);');
  27242. Add(' v:=TRedBlue(e);');
  27243. Add(' e:=TColor(v);');
  27244. Add(' e:=TRedBlue(v);');
  27245. ConvertProgram;
  27246. CheckSource('TestJSValue_Enum',
  27247. LinesToStr([ // statements
  27248. 'this.TColor = {',
  27249. ' "0": "red",',
  27250. ' red: 0,',
  27251. ' "1": "blue",',
  27252. ' blue: 1',
  27253. '};',
  27254. 'this.v = undefined;',
  27255. 'this.e = 0;',
  27256. '']),
  27257. LinesToStr([ // $mod.$main
  27258. '$mod.v = $mod.e;',
  27259. '$mod.v = $mod.e;',
  27260. '$mod.v = $mod.e;',
  27261. '$mod.e = $mod.v;',
  27262. '$mod.e = $mod.v;',
  27263. '']));
  27264. end;
  27265. procedure TTestModule.TestJSValue_ClassInstance;
  27266. begin
  27267. StartProgram(false);
  27268. Add([
  27269. 'type',
  27270. ' TObject = class',
  27271. ' end;',
  27272. ' TBirdObject = TObject;',
  27273. 'var',
  27274. ' v: jsvalue;',
  27275. ' o: TObject;',
  27276. 'begin',
  27277. ' v:=o;',
  27278. ' v:=TObject(o);',
  27279. ' v:=TBirdObject(o);',
  27280. ' o:=TObject(v);',
  27281. ' o:=TBirdObject(v);',
  27282. ' if v is TObject then ;',
  27283. '']);
  27284. ConvertProgram;
  27285. CheckSource('TestJSValue_ClassInstance',
  27286. LinesToStr([ // statements
  27287. 'rtl.createClass(this, "TObject", null, function () {',
  27288. ' this.$init = function () {',
  27289. ' };',
  27290. ' this.$final = function () {',
  27291. ' };',
  27292. '});',
  27293. 'this.v = undefined;',
  27294. 'this.o = null;',
  27295. '']),
  27296. LinesToStr([ // $mod.$main
  27297. '$mod.v = $mod.o;',
  27298. '$mod.v = $mod.o;',
  27299. '$mod.v = $mod.o;',
  27300. '$mod.o = rtl.getObject($mod.v);',
  27301. '$mod.o = rtl.getObject($mod.v);',
  27302. 'if (rtl.isExt($mod.v, $mod.TObject, 1)) ;',
  27303. '']));
  27304. end;
  27305. procedure TTestModule.TestJSValue_ClassOf;
  27306. begin
  27307. StartProgram(false);
  27308. Add([
  27309. 'type',
  27310. ' TClass = class of TObject;',
  27311. ' TObject = class',
  27312. ' end;',
  27313. ' TBirds = class of TBird;',
  27314. ' TBird = class(TObject) end;',
  27315. 'var',
  27316. ' v: jsvalue;',
  27317. ' c: TClass;',
  27318. 'begin',
  27319. ' v:=c;',
  27320. ' v:=TObject;',
  27321. ' v:=TClass(c);',
  27322. ' v:=TBirds(c);',
  27323. ' c:=TClass(v);',
  27324. ' c:=TBirds(v);',
  27325. ' if v is TClass then ;',
  27326. '']);
  27327. ConvertProgram;
  27328. CheckSource('TestJSValue_ClassOf',
  27329. LinesToStr([ // statements
  27330. 'rtl.createClass(this, "TObject", null, function () {',
  27331. ' this.$init = function () {',
  27332. ' };',
  27333. ' this.$final = function () {',
  27334. ' };',
  27335. '});',
  27336. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  27337. '});',
  27338. 'this.v = undefined;',
  27339. 'this.c = null;',
  27340. '']),
  27341. LinesToStr([ // $mod.$main
  27342. '$mod.v = $mod.c;',
  27343. '$mod.v = $mod.TObject;',
  27344. '$mod.v = $mod.c;',
  27345. '$mod.v = $mod.c;',
  27346. '$mod.c = rtl.getObject($mod.v);',
  27347. '$mod.c = rtl.getObject($mod.v);',
  27348. 'if (rtl.isExt($mod.v, $mod.TObject, 2)) ;',
  27349. '']));
  27350. end;
  27351. procedure TTestModule.TestJSValue_ArrayOfJSValue;
  27352. begin
  27353. StartProgram(false);
  27354. Add([
  27355. 'type',
  27356. ' integer = longint;',
  27357. ' TArray = array of JSValue;',
  27358. ' TArrgh = tarray;',
  27359. ' TArrInt = array of integer;',
  27360. 'var',
  27361. ' v: jsvalue;',
  27362. ' TheArray: tarray = (1,''2'');',
  27363. ' Arr: tarrgh;',
  27364. ' i: integer;',
  27365. ' ArrInt: tarrint;',
  27366. 'begin',
  27367. ' arr:=thearray;',
  27368. ' thearray:=arr;',
  27369. ' setlength(arr,2);',
  27370. ' setlength(thearray,3);',
  27371. ' arr[4]:=v;',
  27372. ' arr[5]:=length(thearray);',
  27373. ' arr[6]:=nil;',
  27374. ' arr[7]:=thearray[8];',
  27375. ' arr[low(arr)]:=high(thearray);',
  27376. ' arr:=arrint;',
  27377. ' arrInt:=tarrint(arr);',
  27378. ' if TheArray = nil then ;',
  27379. ' if nil = TheArray then ;',
  27380. ' if TheArray <> nil then ;',
  27381. ' if nil <> TheArray then ;',
  27382. '']);
  27383. ConvertProgram;
  27384. CheckSource('TestJSValue_ArrayOfJSValue',
  27385. LinesToStr([ // statements
  27386. 'this.v = undefined;',
  27387. 'this.TheArray = [1, "2"];',
  27388. 'this.Arr = [];',
  27389. 'this.i = 0;',
  27390. 'this.ArrInt = [];',
  27391. '']),
  27392. LinesToStr([ // $mod.$main
  27393. '$mod.Arr = rtl.arrayRef($mod.TheArray);',
  27394. '$mod.TheArray = rtl.arrayRef($mod.Arr);',
  27395. '$mod.Arr = rtl.arraySetLength($mod.Arr,undefined,2);',
  27396. '$mod.TheArray = rtl.arraySetLength($mod.TheArray,undefined,3);',
  27397. '$mod.Arr[4] = $mod.v;',
  27398. '$mod.Arr[5] = rtl.length($mod.TheArray);',
  27399. '$mod.Arr[6] = null;',
  27400. '$mod.Arr[7] = $mod.TheArray[8];',
  27401. '$mod.Arr[0] = rtl.length($mod.TheArray) - 1;',
  27402. '$mod.Arr = rtl.arrayRef($mod.ArrInt);',
  27403. '$mod.ArrInt = $mod.Arr;',
  27404. 'if (rtl.length($mod.TheArray) === 0) ;',
  27405. 'if (rtl.length($mod.TheArray) === 0) ;',
  27406. 'if (rtl.length($mod.TheArray) > 0) ;',
  27407. 'if (rtl.length($mod.TheArray) > 0) ;',
  27408. '']));
  27409. end;
  27410. procedure TTestModule.TestJSValue_ArrayLit;
  27411. begin
  27412. StartProgram(false);
  27413. Add([
  27414. 'type',
  27415. ' TFlag = (big,small);',
  27416. ' TArray = array of JSValue;',
  27417. ' TObject = class end;',
  27418. ' TClass = class of TObject;',
  27419. 'var',
  27420. ' v: jsvalue;',
  27421. ' a: TArray;',
  27422. ' o: TObject;',
  27423. 'begin',
  27424. ' a:=[];',
  27425. ' a:=[1];',
  27426. ' a:=[1,2];',
  27427. ' a:=[big];',
  27428. ' a:=[1,big];',
  27429. ' a:=[o,nil];',
  27430. '']);
  27431. ConvertProgram;
  27432. CheckSource('TestJSValue_ArrayLit',
  27433. LinesToStr([ // statements
  27434. 'this.TFlag = {',
  27435. ' "0": "big",',
  27436. ' big: 0,',
  27437. ' "1": "small",',
  27438. ' small: 1',
  27439. '};',
  27440. 'rtl.createClass(this, "TObject", null, function () {',
  27441. ' this.$init = function () {',
  27442. ' };',
  27443. ' this.$final = function () {',
  27444. ' };',
  27445. '});',
  27446. 'this.v = undefined;',
  27447. 'this.a = [];',
  27448. 'this.o = null;',
  27449. '']),
  27450. LinesToStr([ // $mod.$main
  27451. '$mod.a = [];',
  27452. '$mod.a = [1];',
  27453. '$mod.a = [1, 2];',
  27454. '$mod.a = [$mod.TFlag.big];',
  27455. '$mod.a = [1, $mod.TFlag.big];',
  27456. '$mod.a = [$mod.o, null];',
  27457. '']));
  27458. end;
  27459. procedure TTestModule.TestJSValue_Params;
  27460. begin
  27461. StartProgram(false);
  27462. Add('type');
  27463. Add(' integer = longint;');
  27464. Add(' TYesNo = boolean;');
  27465. Add(' TFloat = double;');
  27466. Add(' TCaption = string;');
  27467. Add(' TChar = char;');
  27468. Add('function DoIt(a: jsvalue; const b: jsvalue; var c: jsvalue; out d: jsvalue): jsvalue;');
  27469. Add('var');
  27470. Add(' l: jsvalue;');
  27471. Add('begin');
  27472. Add(' a:=a;');
  27473. Add(' l:=b;');
  27474. Add(' c:=c;');
  27475. Add(' d:=d;');
  27476. Add(' Result:=l;');
  27477. Add('end;');
  27478. Add('function DoSome(a: jsvalue; const b: jsvalue): jsvalue; begin end;');
  27479. Add('var');
  27480. Add(' v: jsvalue;');
  27481. Add(' i: integer;');
  27482. Add(' b: TYesNo;');
  27483. Add(' d: TFloat;');
  27484. Add(' s: TCaption;');
  27485. Add(' c: TChar;');
  27486. Add('begin');
  27487. Add(' v:=doit(v,v,v,v);');
  27488. Add(' i:=integer(dosome(i,i));');
  27489. Add(' b:=TYesNo(dosome(b,b));');
  27490. Add(' d:=TFloat(dosome(d,d));');
  27491. Add(' s:=TCaption(dosome(s,s));');
  27492. Add(' c:=TChar(dosome(c,c));');
  27493. ConvertProgram;
  27494. CheckSource('TestJSValue_Params',
  27495. LinesToStr([ // statements
  27496. 'this.DoIt = function (a, b, c, d) {',
  27497. ' var Result = undefined;',
  27498. ' var l = undefined;',
  27499. ' a = a;',
  27500. ' l = b;',
  27501. ' c.set(c.get());',
  27502. ' d.set(d.get());',
  27503. ' Result = l;',
  27504. ' return Result;',
  27505. '};',
  27506. 'this.DoSome = function (a, b) {',
  27507. ' var Result = undefined;',
  27508. ' return Result;',
  27509. '};',
  27510. 'this.v = undefined;',
  27511. 'this.i = 0;',
  27512. 'this.b = false;',
  27513. 'this.d = 0.0;',
  27514. 'this.s = "";',
  27515. 'this.c = "";',
  27516. '']),
  27517. LinesToStr([ // $mod.$main
  27518. '$mod.v = $mod.DoIt($mod.v, $mod.v, {',
  27519. ' p: $mod,',
  27520. ' get: function () {',
  27521. ' return this.p.v;',
  27522. ' },',
  27523. ' set: function (v) {',
  27524. ' this.p.v = v;',
  27525. ' }',
  27526. '}, {',
  27527. ' p: $mod,',
  27528. ' get: function () {',
  27529. ' return this.p.v;',
  27530. ' },',
  27531. ' set: function (v) {',
  27532. ' this.p.v = v;',
  27533. ' }',
  27534. '});',
  27535. '$mod.i = rtl.trunc($mod.DoSome($mod.i, $mod.i));',
  27536. '$mod.b = !($mod.DoSome($mod.b, $mod.b) == false);',
  27537. '$mod.d = rtl.getNumber($mod.DoSome($mod.d, $mod.d));',
  27538. '$mod.s = "" + $mod.DoSome($mod.s, $mod.s);',
  27539. '$mod.c = rtl.getChar($mod.DoSome($mod.c, $mod.c));',
  27540. '']));
  27541. end;
  27542. procedure TTestModule.TestJSValue_UntypedParam;
  27543. begin
  27544. StartProgram(false);
  27545. Add('function DoIt(const a; var b; out c): jsvalue;');
  27546. Add('begin');
  27547. Add(' Result:=a;');
  27548. Add(' Result:=b;');
  27549. Add(' Result:=c;');
  27550. Add(' b:=Result;');
  27551. Add(' c:=Result;');
  27552. Add('end;');
  27553. Add('var i: longint;');
  27554. Add('begin');
  27555. Add(' doit(i,i,i);');
  27556. ConvertProgram;
  27557. CheckSource('TestJSValue_UntypedParam',
  27558. LinesToStr([ // statements
  27559. 'this.DoIt = function (a, b, c) {',
  27560. ' var Result = undefined;',
  27561. ' Result = a;',
  27562. ' Result = b.get();',
  27563. ' Result = c.get();',
  27564. ' b.set(Result);',
  27565. ' c.set(Result);',
  27566. ' return Result;',
  27567. '};',
  27568. 'this.i = 0;',
  27569. '']),
  27570. LinesToStr([ // $mod.$main
  27571. '$mod.DoIt($mod.i, {',
  27572. ' p: $mod,',
  27573. ' get: function () {',
  27574. ' return this.p.i;',
  27575. ' },',
  27576. ' set: function (v) {',
  27577. ' this.p.i = v;',
  27578. ' }',
  27579. '}, {',
  27580. ' p: $mod,',
  27581. ' get: function () {',
  27582. ' return this.p.i;',
  27583. ' },',
  27584. ' set: function (v) {',
  27585. ' this.p.i = v;',
  27586. ' }',
  27587. '});',
  27588. '']));
  27589. end;
  27590. procedure TTestModule.TestJSValue_FuncResultType;
  27591. begin
  27592. StartProgram(false);
  27593. Add('type');
  27594. Add(' integer = longint;');
  27595. Add(' TJSValueArray = array of JSValue;');
  27596. Add(' TListSortCompare = function(Item1, Item2: JSValue): Integer;');
  27597. Add('procedure Sort(P: JSValue; aList: TJSValueArray; const Compare: TListSortCompare);');
  27598. Add('begin');
  27599. Add(' while Compare(P,aList[0])>0 do ;');
  27600. Add('end;');
  27601. Add('var');
  27602. Add(' Compare: TListSortCompare;');
  27603. Add(' V: JSValue;');
  27604. Add(' i: integer;');
  27605. Add('begin');
  27606. Add(' if Compare(V,V)>0 then ;');
  27607. Add(' if Compare(i,i)>1 then ;');
  27608. Add(' if Compare(nil,false)>2 then ;');
  27609. Add(' if Compare(1,true)>3 then ;');
  27610. ConvertProgram;
  27611. CheckSource('TestJSValue_UntypedParam',
  27612. LinesToStr([ // statements
  27613. 'this.Sort = function (P, aList, Compare) {',
  27614. ' while (Compare(P, aList[0]) > 0) {',
  27615. ' };',
  27616. '};',
  27617. 'this.Compare = null;',
  27618. 'this.V = undefined;',
  27619. 'this.i = 0;',
  27620. '']),
  27621. LinesToStr([ // $mod.$main
  27622. 'if ($mod.Compare($mod.V, $mod.V) > 0) ;',
  27623. 'if ($mod.Compare($mod.i, $mod.i) > 1) ;',
  27624. 'if ($mod.Compare(null, false) > 2) ;',
  27625. 'if ($mod.Compare(1, true) > 3) ;',
  27626. '']));
  27627. end;
  27628. procedure TTestModule.TestJSValue_ProcType_Assign;
  27629. begin
  27630. StartProgram(false);
  27631. Add('type');
  27632. Add(' integer = longint;');
  27633. Add(' TObject = class');
  27634. Add(' class function GetGlob: integer;');
  27635. Add(' function Getter: integer;');
  27636. Add(' end;');
  27637. Add('class function TObject.GetGlob: integer;');
  27638. Add('var v1: jsvalue;');
  27639. Add('begin');
  27640. Add(' v1:=@GetGlob;');
  27641. Add(' v1:[email protected];');
  27642. Add('end;');
  27643. Add('function TObject.Getter: integer;');
  27644. Add('var v2: jsvalue;');
  27645. Add('begin');
  27646. Add(' v2:=@Getter;');
  27647. Add(' v2:[email protected];');
  27648. Add(' v2:=@GetGlob;');
  27649. Add(' v2:[email protected];');
  27650. Add('end;');
  27651. Add('function GetIt(i: integer): integer;');
  27652. Add('var v3: jsvalue;');
  27653. Add('begin');
  27654. Add(' v3:=@GetIt;');
  27655. Add('end;');
  27656. Add('var');
  27657. Add(' V: JSValue;');
  27658. Add(' o: TObject;');
  27659. Add('begin');
  27660. Add(' v:=@GetIt;');
  27661. Add(' v:[email protected];');
  27662. Add(' v:[email protected];');
  27663. ConvertProgram;
  27664. CheckSource('TestJSValue_ProcType_Assign',
  27665. LinesToStr([ // statements
  27666. 'rtl.createClass(this, "TObject", null, function () {',
  27667. ' this.$init = function () {',
  27668. ' };',
  27669. ' this.$final = function () {',
  27670. ' };',
  27671. ' this.GetGlob = function () {',
  27672. ' var Result = 0;',
  27673. ' var v1 = undefined;',
  27674. ' v1 = rtl.createCallback(this, "GetGlob");',
  27675. ' v1 = rtl.createCallback(this, "GetGlob");',
  27676. ' return Result;',
  27677. ' };',
  27678. ' this.Getter = function () {',
  27679. ' var Result = 0;',
  27680. ' var v2 = undefined;',
  27681. ' v2 = rtl.createCallback(this, "Getter");',
  27682. ' v2 = rtl.createCallback(this, "Getter");',
  27683. ' v2 = rtl.createCallback(this.$class, "GetGlob");',
  27684. ' v2 = rtl.createCallback(this.$class, "GetGlob");',
  27685. ' return Result;',
  27686. ' };',
  27687. '});',
  27688. 'this.GetIt = function (i) {',
  27689. ' var Result = 0;',
  27690. ' var v3 = undefined;',
  27691. ' v3 = $mod.GetIt;',
  27692. ' return Result;',
  27693. '};',
  27694. 'this.V = undefined;',
  27695. 'this.o = null;',
  27696. '']),
  27697. LinesToStr([ // $mod.$main
  27698. '$mod.V = $mod.GetIt;',
  27699. '$mod.V = rtl.createCallback($mod.o, "Getter");',
  27700. '$mod.V = rtl.createCallback($mod.o.$class, "GetGlob");',
  27701. '']));
  27702. end;
  27703. procedure TTestModule.TestJSValue_ProcType_Equal;
  27704. begin
  27705. StartProgram(false);
  27706. Add('type');
  27707. Add(' integer = longint;');
  27708. Add(' TObject = class');
  27709. Add(' class function GetGlob: integer;');
  27710. Add(' function Getter: integer;');
  27711. Add(' end;');
  27712. Add('class function TObject.GetGlob: integer;');
  27713. Add('var v1: jsvalue;');
  27714. Add('begin');
  27715. Add(' if v1=@GetGlob then;');
  27716. Add(' if [email protected] then ;');
  27717. Add('end;');
  27718. Add('function TObject.Getter: integer;');
  27719. Add('var v2: jsvalue;');
  27720. Add('begin');
  27721. Add(' if v2=@Getter then;');
  27722. Add(' if [email protected] then ;');
  27723. Add(' if v2=@GetGlob then;');
  27724. Add(' if [email protected] then;');
  27725. Add('end;');
  27726. Add('function GetIt(i: integer): integer;');
  27727. Add('var v3: jsvalue;');
  27728. Add('begin');
  27729. Add(' if v3=@GetIt then;');
  27730. Add('end;');
  27731. Add('var');
  27732. Add(' V: JSValue;');
  27733. Add(' o: TObject;');
  27734. Add('begin');
  27735. Add(' if v=@GetIt then;');
  27736. Add(' if [email protected] then;');
  27737. Add(' if [email protected] then;');
  27738. Add(' if @GetIt=v then;');
  27739. Add(' if @o.Getter=v then;');
  27740. Add(' if @o.GetGlob=v then;');
  27741. ConvertProgram;
  27742. CheckSource('TestJSValue_ProcType_Equal',
  27743. LinesToStr([ // statements
  27744. 'rtl.createClass(this, "TObject", null, function () {',
  27745. ' this.$init = function () {',
  27746. ' };',
  27747. ' this.$final = function () {',
  27748. ' };',
  27749. ' this.GetGlob = function () {',
  27750. ' var Result = 0;',
  27751. ' var v1 = undefined;',
  27752. ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
  27753. ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
  27754. ' return Result;',
  27755. ' };',
  27756. ' this.Getter = function () {',
  27757. ' var Result = 0;',
  27758. ' var v2 = undefined;',
  27759. ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
  27760. ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
  27761. ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
  27762. ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
  27763. ' return Result;',
  27764. ' };',
  27765. '});',
  27766. 'this.GetIt = function (i) {',
  27767. ' var Result = 0;',
  27768. ' var v3 = undefined;',
  27769. ' if (rtl.eqCallback(v3, $mod.GetIt)) ;',
  27770. ' return Result;',
  27771. '};',
  27772. 'this.V = undefined;',
  27773. 'this.o = null;',
  27774. '']),
  27775. LinesToStr([ // $mod.$main
  27776. 'if (rtl.eqCallback($mod.V, $mod.GetIt)) ;',
  27777. 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o, "Getter"))) ;',
  27778. 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o.$class, "GetGlob"))) ;',
  27779. 'if (rtl.eqCallback($mod.GetIt, $mod.V)) ;',
  27780. 'if (rtl.eqCallback(rtl.createCallback($mod.o, "Getter"), $mod.V)) ;',
  27781. 'if (rtl.eqCallback(rtl.createCallback($mod.o.$class, "GetGlob"), $mod.V)) ;',
  27782. '']));
  27783. end;
  27784. procedure TTestModule.TestJSValue_ProcType_Param;
  27785. begin
  27786. StartProgram(false);
  27787. Add([
  27788. 'type',
  27789. ' variant = jsvalue;',
  27790. ' TArrVariant = array of variant;',
  27791. ' TArrVar2 = TArrVariant;',
  27792. ' TFuncInt = function: longint;',
  27793. 'function GetIt: longint;',
  27794. 'begin',
  27795. 'end;',
  27796. 'procedure DoIt(p: jsvalue; Arr: TArrVar2);',
  27797. 'var v: variant;',
  27798. 'begin',
  27799. ' v:=arr[1];',
  27800. 'end;',
  27801. 'var s: string;',
  27802. 'begin',
  27803. ' DoIt(GetIt,[]);',
  27804. ' DoIt(@GetIt,[]);',
  27805. ' DoIt(1,[s,GetIt]);',
  27806. ' DoIt(1,[s,@GetIt]);',
  27807. '']);
  27808. ConvertProgram;
  27809. CheckSource('TestJSValue_ProcType_Param',
  27810. LinesToStr([ // statements
  27811. 'this.GetIt = function () {',
  27812. ' var Result = 0;',
  27813. ' return Result;',
  27814. '};',
  27815. 'this.DoIt = function (p, Arr) {',
  27816. ' var v = undefined;',
  27817. ' v = Arr[1];',
  27818. '};',
  27819. 'this.s = "";',
  27820. '']),
  27821. LinesToStr([ // $mod.$main
  27822. '$mod.DoIt($mod.GetIt(), []);',
  27823. '$mod.DoIt($mod.GetIt, []);',
  27824. '$mod.DoIt(1, [$mod.s, $mod.GetIt()]);',
  27825. '$mod.DoIt(1, [$mod.s, $mod.GetIt]);',
  27826. '']));
  27827. end;
  27828. procedure TTestModule.TestJSValue_AssignToPointerFail;
  27829. begin
  27830. StartProgram(false);
  27831. Add([
  27832. 'var',
  27833. ' v: JSValue;',
  27834. ' p: Pointer;',
  27835. 'begin',
  27836. ' p:=v;',
  27837. '']);
  27838. SetExpectedPasResolverError('Incompatible types: got "JSValue" expected "Pointer"',
  27839. nIncompatibleTypesGotExpected);
  27840. ConvertProgram;
  27841. end;
  27842. procedure TTestModule.TestJSValue_OverloadDouble;
  27843. begin
  27844. StartProgram(false);
  27845. Add([
  27846. 'type',
  27847. ' integer = longint;',
  27848. ' tdatetime = double;',
  27849. 'procedure DoIt(d: double); begin end;',
  27850. 'procedure DoIt(v: jsvalue); begin end;',
  27851. 'var',
  27852. ' d: double;',
  27853. ' dt: tdatetime;',
  27854. ' i: integer;',
  27855. ' b: byte;',
  27856. ' shi: shortint;',
  27857. ' w: word;',
  27858. ' smi: smallint;',
  27859. ' lw: longword;',
  27860. ' li: longint;',
  27861. ' ni: nativeint;',
  27862. ' nu: nativeuint;',
  27863. 'begin',
  27864. ' DoIt(d);',
  27865. ' DoIt(dt);',
  27866. ' DoIt(i);',
  27867. ' DoIt(b);',
  27868. ' DoIt(shi);',
  27869. ' DoIt(w);',
  27870. ' DoIt(smi);',
  27871. ' DoIt(lw);',
  27872. ' DoIt(li);',
  27873. ' DoIt(ni);',
  27874. ' DoIt(nu);',
  27875. '']);
  27876. ConvertProgram;
  27877. CheckSource('TestJSValue_OverloadDouble',
  27878. LinesToStr([ // statements
  27879. 'this.DoIt = function (d) {',
  27880. '};',
  27881. 'this.DoIt$1 = function (v) {',
  27882. '};',
  27883. 'this.d = 0.0;',
  27884. 'this.dt = 0.0;',
  27885. 'this.i = 0;',
  27886. 'this.b = 0;',
  27887. 'this.shi = 0;',
  27888. 'this.w = 0;',
  27889. 'this.smi = 0;',
  27890. 'this.lw = 0;',
  27891. 'this.li = 0;',
  27892. 'this.ni = 0;',
  27893. 'this.nu = 0;',
  27894. '']),
  27895. LinesToStr([ // $mod.$main
  27896. '$mod.DoIt($mod.d);',
  27897. '$mod.DoIt($mod.dt);',
  27898. '$mod.DoIt$1($mod.i);',
  27899. '$mod.DoIt$1($mod.b);',
  27900. '$mod.DoIt$1($mod.shi);',
  27901. '$mod.DoIt$1($mod.w);',
  27902. '$mod.DoIt$1($mod.smi);',
  27903. '$mod.DoIt$1($mod.lw);',
  27904. '$mod.DoIt$1($mod.li);',
  27905. '$mod.DoIt$1($mod.ni);',
  27906. '$mod.DoIt$1($mod.nu);',
  27907. '']));
  27908. end;
  27909. procedure TTestModule.TestJSValue_OverloadNativeInt;
  27910. begin
  27911. StartProgram(false);
  27912. Add([
  27913. 'type',
  27914. ' integer = longint;',
  27915. ' int53 = nativeint;',
  27916. ' tdatetime = double;',
  27917. 'procedure DoIt(n: nativeint); begin end;',
  27918. 'procedure DoIt(v: jsvalue); begin end;',
  27919. 'var',
  27920. ' d: double;',
  27921. ' dt: tdatetime;',
  27922. ' i: integer;',
  27923. ' b: byte;',
  27924. ' shi: shortint;',
  27925. ' w: word;',
  27926. ' smi: smallint;',
  27927. ' lw: longword;',
  27928. ' li: longint;',
  27929. ' ni: nativeint;',
  27930. ' nu: nativeuint;',
  27931. 'begin',
  27932. ' DoIt(d);',
  27933. ' DoIt(dt);',
  27934. ' DoIt(i);',
  27935. ' DoIt(b);',
  27936. ' DoIt(shi);',
  27937. ' DoIt(w);',
  27938. ' DoIt(smi);',
  27939. ' DoIt(lw);',
  27940. ' DoIt(li);',
  27941. ' DoIt(ni);',
  27942. ' DoIt(nu);',
  27943. '']);
  27944. ConvertProgram;
  27945. CheckSource('TestJSValue_OverloadNativeInt',
  27946. LinesToStr([ // statements
  27947. 'this.DoIt = function (n) {',
  27948. '};',
  27949. 'this.DoIt$1 = function (v) {',
  27950. '};',
  27951. 'this.d = 0.0;',
  27952. 'this.dt = 0.0;',
  27953. 'this.i = 0;',
  27954. 'this.b = 0;',
  27955. 'this.shi = 0;',
  27956. 'this.w = 0;',
  27957. 'this.smi = 0;',
  27958. 'this.lw = 0;',
  27959. 'this.li = 0;',
  27960. 'this.ni = 0;',
  27961. 'this.nu = 0;',
  27962. '']),
  27963. LinesToStr([ // $mod.$main
  27964. '$mod.DoIt$1($mod.d);',
  27965. '$mod.DoIt$1($mod.dt);',
  27966. '$mod.DoIt($mod.i);',
  27967. '$mod.DoIt($mod.b);',
  27968. '$mod.DoIt($mod.shi);',
  27969. '$mod.DoIt($mod.w);',
  27970. '$mod.DoIt($mod.smi);',
  27971. '$mod.DoIt($mod.lw);',
  27972. '$mod.DoIt($mod.li);',
  27973. '$mod.DoIt($mod.ni);',
  27974. '$mod.DoIt($mod.nu);',
  27975. '']));
  27976. end;
  27977. procedure TTestModule.TestJSValue_OverloadWord;
  27978. begin
  27979. StartProgram(false);
  27980. Add([
  27981. 'type',
  27982. ' integer = longint;',
  27983. ' int53 = nativeint;',
  27984. ' tdatetime = double;',
  27985. 'procedure DoIt(w: word); begin end;',
  27986. 'procedure DoIt(v: jsvalue); begin end;',
  27987. 'var',
  27988. ' d: double;',
  27989. ' dt: tdatetime;',
  27990. ' i: integer;',
  27991. ' b: byte;',
  27992. ' shi: shortint;',
  27993. ' w: word;',
  27994. ' smi: smallint;',
  27995. ' lw: longword;',
  27996. ' li: longint;',
  27997. ' ni: nativeint;',
  27998. ' nu: nativeuint;',
  27999. 'begin',
  28000. ' DoIt(d);',
  28001. ' DoIt(dt);',
  28002. ' DoIt(i);',
  28003. ' DoIt(b);',
  28004. ' DoIt(shi);',
  28005. ' DoIt(w);',
  28006. ' DoIt(smi);',
  28007. ' DoIt(lw);',
  28008. ' DoIt(li);',
  28009. ' DoIt(ni);',
  28010. ' DoIt(nu);',
  28011. '']);
  28012. ConvertProgram;
  28013. CheckSource('TestJSValue_OverloadWord',
  28014. LinesToStr([ // statements
  28015. 'this.DoIt = function (w) {',
  28016. '};',
  28017. 'this.DoIt$1 = function (v) {',
  28018. '};',
  28019. 'this.d = 0.0;',
  28020. 'this.dt = 0.0;',
  28021. 'this.i = 0;',
  28022. 'this.b = 0;',
  28023. 'this.shi = 0;',
  28024. 'this.w = 0;',
  28025. 'this.smi = 0;',
  28026. 'this.lw = 0;',
  28027. 'this.li = 0;',
  28028. 'this.ni = 0;',
  28029. 'this.nu = 0;',
  28030. '']),
  28031. LinesToStr([ // $mod.$main
  28032. '$mod.DoIt$1($mod.d);',
  28033. '$mod.DoIt$1($mod.dt);',
  28034. '$mod.DoIt$1($mod.i);',
  28035. '$mod.DoIt($mod.b);',
  28036. '$mod.DoIt($mod.shi);',
  28037. '$mod.DoIt($mod.w);',
  28038. '$mod.DoIt$1($mod.smi);',
  28039. '$mod.DoIt$1($mod.lw);',
  28040. '$mod.DoIt$1($mod.li);',
  28041. '$mod.DoIt$1($mod.ni);',
  28042. '$mod.DoIt$1($mod.nu);',
  28043. '']));
  28044. end;
  28045. procedure TTestModule.TestJSValue_OverloadString;
  28046. begin
  28047. StartProgram(false);
  28048. Add([
  28049. 'type',
  28050. ' uni = string;',
  28051. ' WChar = char;',
  28052. 'procedure DoIt(s: string); begin end;',
  28053. 'procedure DoIt(v: jsvalue); begin end;',
  28054. 'var',
  28055. ' s: string;',
  28056. ' c: char;',
  28057. ' u: uni;',
  28058. 'begin',
  28059. ' DoIt(s);',
  28060. ' DoIt(c);',
  28061. ' DoIt(u);',
  28062. '']);
  28063. ConvertProgram;
  28064. CheckSource('TestJSValue_OverloadString',
  28065. LinesToStr([ // statements
  28066. 'this.DoIt = function (s) {',
  28067. '};',
  28068. 'this.DoIt$1 = function (v) {',
  28069. '};',
  28070. 'this.s = "";',
  28071. 'this.c = "";',
  28072. 'this.u = "";',
  28073. '']),
  28074. LinesToStr([ // $mod.$main
  28075. '$mod.DoIt($mod.s);',
  28076. '$mod.DoIt($mod.c);',
  28077. '$mod.DoIt($mod.u);',
  28078. '']));
  28079. end;
  28080. procedure TTestModule.TestJSValue_OverloadChar;
  28081. begin
  28082. StartProgram(false);
  28083. Add([
  28084. 'type',
  28085. ' uni = string;',
  28086. ' WChar = char;',
  28087. 'procedure DoIt(c: char); begin end;',
  28088. 'procedure DoIt(v: jsvalue); begin end;',
  28089. 'var',
  28090. ' s: string;',
  28091. ' c: char;',
  28092. ' u: uni;',
  28093. 'begin',
  28094. ' DoIt(s);',
  28095. ' DoIt(c);',
  28096. ' DoIt(u);',
  28097. '']);
  28098. ConvertProgram;
  28099. CheckSource('TestJSValue_OverloadChar',
  28100. LinesToStr([ // statements
  28101. 'this.DoIt = function (c) {',
  28102. '};',
  28103. 'this.DoIt$1 = function (v) {',
  28104. '};',
  28105. 'this.s = "";',
  28106. 'this.c = "";',
  28107. 'this.u = "";',
  28108. '']),
  28109. LinesToStr([ // $mod.$main
  28110. '$mod.DoIt$1($mod.s);',
  28111. '$mod.DoIt($mod.c);',
  28112. '$mod.DoIt$1($mod.u);',
  28113. '']));
  28114. end;
  28115. procedure TTestModule.TestJSValue_OverloadPointer;
  28116. begin
  28117. StartProgram(false);
  28118. Add([
  28119. 'type',
  28120. ' TObject = class end;',
  28121. 'procedure DoIt(p: pointer); begin end;',
  28122. 'procedure DoIt(v: jsvalue); begin end;',
  28123. 'var',
  28124. ' o: TObject;',
  28125. 'begin',
  28126. ' DoIt(o);',
  28127. '']);
  28128. ConvertProgram;
  28129. CheckSource('TestJSValue_OverloadPointer',
  28130. LinesToStr([ // statements
  28131. 'rtl.createClass(this, "TObject", null, function () {',
  28132. ' this.$init = function () {',
  28133. ' };',
  28134. ' this.$final = function () {',
  28135. ' };',
  28136. '});',
  28137. 'this.DoIt = function (p) {',
  28138. '};',
  28139. 'this.DoIt$1 = function (v) {',
  28140. '};',
  28141. 'this.o = null;',
  28142. '']),
  28143. LinesToStr([ // $mod.$main
  28144. '$mod.DoIt($mod.o);',
  28145. '']));
  28146. end;
  28147. procedure TTestModule.TestJSValue_ForIn;
  28148. begin
  28149. StartProgram(false);
  28150. Add([
  28151. 'var',
  28152. ' v: JSValue;',
  28153. ' key: string;',
  28154. 'begin',
  28155. ' for key in v do begin',
  28156. ' if key=''abc'' then ;',
  28157. ' end;',
  28158. '']);
  28159. ConvertProgram;
  28160. CheckSource('TestJSValue_ForIn',
  28161. LinesToStr([ // statements
  28162. 'this.v = undefined;',
  28163. 'this.key = "";',
  28164. '']),
  28165. LinesToStr([ // $mod.$main
  28166. 'for ($mod.key in $mod.v) {',
  28167. ' if ($mod.key === "abc") ;',
  28168. '};',
  28169. '']));
  28170. end;
  28171. procedure TTestModule.TestRTTI_IntRange;
  28172. begin
  28173. WithTypeInfo:=true;
  28174. StartProgram(true,[supTypeInfo]);
  28175. Add([
  28176. '{$modeswitch externalclass}',
  28177. 'type',
  28178. ' TGraphicsColor = -$7FFFFFFF-1..$7FFFFFFF;',
  28179. ' TColor = type TGraphicsColor;',
  28180. 'var',
  28181. ' p: TTypeInfo;',
  28182. ' k: TTypeKind;',
  28183. 'begin',
  28184. ' p:=typeinfo(TGraphicsColor);',
  28185. ' p:=typeinfo(TColor);',
  28186. ' k:=GetTypeKind(TGraphicsColor);',
  28187. ' k:=GetTypeKind(TColor);',
  28188. '']);
  28189. ConvertProgram;
  28190. CheckSource('TestRTTI_IntRange',
  28191. LinesToStr([ // statements
  28192. 'this.$rtti.$Int("TGraphicsColor", {',
  28193. ' minvalue: -2147483648,',
  28194. ' maxvalue: 2147483647,',
  28195. ' ordtype: 4',
  28196. '});',
  28197. 'this.$rtti.$inherited("TColor", this.$rtti["TGraphicsColor"], {});',
  28198. 'this.p = null;',
  28199. 'this.k = 0;',
  28200. '']),
  28201. LinesToStr([ // $mod.$main
  28202. '$mod.p = $mod.$rtti["TGraphicsColor"];',
  28203. '$mod.p = $mod.$rtti["TColor"];',
  28204. '$mod.k = 1;',
  28205. '$mod.k = 1;',
  28206. '']));
  28207. end;
  28208. procedure TTestModule.TestRTTI_Double;
  28209. begin
  28210. WithTypeInfo:=true;
  28211. StartProgram(true,[supTypeInfo]);
  28212. Add([
  28213. '{$modeswitch externalclass}',
  28214. 'type',
  28215. ' TFloat = type double;',
  28216. 'var',
  28217. ' p: TTypeInfo;',
  28218. 'begin',
  28219. ' p:=typeinfo(double);',
  28220. ' p:=typeinfo(TFloat);',
  28221. '']);
  28222. ConvertProgram;
  28223. CheckSource('TestRTTI_Double',
  28224. LinesToStr([ // statements
  28225. 'this.$rtti.$inherited("TFloat", rtl.double, {});',
  28226. 'this.p = null;',
  28227. '']),
  28228. LinesToStr([ // $mod.$main
  28229. '$mod.p = rtl.double;',
  28230. '$mod.p = $mod.$rtti["TFloat"];',
  28231. '']));
  28232. end;
  28233. procedure TTestModule.TestRTTI_ProcType;
  28234. begin
  28235. WithTypeInfo:=true;
  28236. StartProgram(false);
  28237. Add('type');
  28238. Add(' TProcA = procedure;');
  28239. Add(' TMethodB = procedure of object;');
  28240. Add(' TProcC = procedure; varargs;');
  28241. Add(' TProcD = procedure(i: longint; const j: string; var c: char; out d: double);');
  28242. Add(' TProcE = function: nativeint;');
  28243. Add(' TProcF = function(const p: TProcA): nativeuint;');
  28244. Add('var p: pointer;');
  28245. Add('begin');
  28246. Add(' p:=typeinfo(tproca);');
  28247. ConvertProgram;
  28248. CheckSource('TestRTTI_ProcType',
  28249. LinesToStr([ // statements
  28250. 'this.$rtti.$ProcVar("TProcA", {',
  28251. ' procsig: rtl.newTIProcSig(null)',
  28252. '});',
  28253. 'this.$rtti.$MethodVar("TMethodB", {',
  28254. ' procsig: rtl.newTIProcSig(null),',
  28255. ' methodkind: 0',
  28256. '});',
  28257. 'this.$rtti.$ProcVar("TProcC", {',
  28258. ' procsig: rtl.newTIProcSig(null, 2)',
  28259. '});',
  28260. 'this.$rtti.$ProcVar("TProcD", {',
  28261. ' procsig: rtl.newTIProcSig([["i", rtl.longint], ["j", rtl.string, 2], ["c", rtl.char, 1], ["d", rtl.double, 4]])',
  28262. '});',
  28263. 'this.$rtti.$ProcVar("TProcE", {',
  28264. ' procsig: rtl.newTIProcSig(null, rtl.nativeint)',
  28265. '});',
  28266. 'this.$rtti.$ProcVar("TProcF", {',
  28267. ' procsig: rtl.newTIProcSig([["p", this.$rtti["TProcA"], 2]], rtl.nativeuint)',
  28268. '});',
  28269. 'this.p = null;',
  28270. '']),
  28271. LinesToStr([ // $mod.$main
  28272. '$mod.p = $mod.$rtti["TProcA"];',
  28273. '']));
  28274. end;
  28275. procedure TTestModule.TestRTTI_ProcType_ArgFromOtherUnit;
  28276. begin
  28277. WithTypeInfo:=true;
  28278. AddModuleWithIntfImplSrc('unit2.pas',
  28279. LinesToStr([
  28280. 'type',
  28281. ' TObject = class end;'
  28282. ]),
  28283. '');
  28284. StartUnit(true);
  28285. Add('interface');
  28286. Add('uses unit2;');
  28287. Add('type');
  28288. Add(' TProcA = function(o: tobject): tobject;');
  28289. Add('implementation');
  28290. Add('type');
  28291. Add(' TProcB = function(o: tobject): tobject;');
  28292. Add('var p: Pointer;');
  28293. Add('initialization');
  28294. Add(' p:=typeinfo(tproca);');
  28295. Add(' p:=typeinfo(tprocb);');
  28296. ConvertUnit;
  28297. CheckSource('TestRTTI_ProcType_ArgFromOtherUnit',
  28298. LinesToStr([ // statements
  28299. 'var $impl = $mod.$impl;',
  28300. 'this.$rtti.$ProcVar("TProcA", {',
  28301. ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
  28302. '});',
  28303. '']),
  28304. LinesToStr([ // this.$init
  28305. '$impl.p = $mod.$rtti["TProcA"];',
  28306. '$impl.p = $mod.$rtti["TProcB"];',
  28307. '']),
  28308. LinesToStr([ // implementation
  28309. '$mod.$rtti.$ProcVar("TProcB", {',
  28310. ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
  28311. '});',
  28312. '$impl.p = null;',
  28313. '']) );
  28314. end;
  28315. procedure TTestModule.TestRTTI_EnumAndSetType;
  28316. begin
  28317. WithTypeInfo:=true;
  28318. StartProgram(false);
  28319. Add('type');
  28320. Add(' TFlag = (light,dark);');
  28321. Add(' TFlags = set of TFlag;');
  28322. Add(' TProc = function(f: TFlags): TFlag;');
  28323. Add('var p: pointer;');
  28324. Add('begin');
  28325. Add(' p:=typeinfo(tflag);');
  28326. Add(' p:=typeinfo(tflags);');
  28327. ConvertProgram;
  28328. CheckSource('TestRTTI_EnumAndType',
  28329. LinesToStr([ // statements
  28330. 'this.TFlag = {',
  28331. ' "0": "light",',
  28332. ' light: 0,',
  28333. ' "1": "dark",',
  28334. ' dark: 1',
  28335. '};',
  28336. 'this.$rtti.$Enum("TFlag", {',
  28337. ' minvalue: 0,',
  28338. ' maxvalue: 1,',
  28339. ' ordtype: 1,',
  28340. ' enumtype: this.TFlag',
  28341. '});',
  28342. 'this.$rtti.$Set("TFlags", {',
  28343. ' comptype: this.$rtti["TFlag"]',
  28344. '});',
  28345. 'this.$rtti.$ProcVar("TProc", {',
  28346. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TFlags"]]], this.$rtti["TFlag"])',
  28347. '});',
  28348. 'this.p = null;',
  28349. '']),
  28350. LinesToStr([ // $mod.$main
  28351. '$mod.p = $mod.$rtti["TFlag"];',
  28352. '$mod.p = $mod.$rtti["TFlags"];',
  28353. '']));
  28354. end;
  28355. procedure TTestModule.TestRTTI_EnumRange;
  28356. begin
  28357. WithTypeInfo:=true;
  28358. StartProgram(false);
  28359. Add([
  28360. 'type',
  28361. ' TCol = (red,green,blue);',
  28362. ' TColRg = green..blue;',
  28363. ' TSetOfColRg = set of TColRg;',
  28364. 'var p: pointer;',
  28365. 'begin',
  28366. ' p:=typeinfo(tcolrg);',
  28367. ' p:=typeinfo(tsetofcolrg);',
  28368. '']);
  28369. ConvertProgram;
  28370. end;
  28371. procedure TTestModule.TestRTTI_AnonymousEnumType;
  28372. begin
  28373. WithTypeInfo:=true;
  28374. StartProgram(false);
  28375. Add('type');
  28376. Add(' TFlags = set of (red, green);');
  28377. Add('var');
  28378. Add(' f: TFlags;');
  28379. Add('begin');
  28380. Add(' Include(f,red);');
  28381. ConvertProgram;
  28382. CheckSource('TestRTTI_AnonymousEnumType',
  28383. LinesToStr([ // statements
  28384. 'this.TFlags$a = {',
  28385. ' "0": "red",',
  28386. ' red: 0,',
  28387. ' "1": "green",',
  28388. ' green: 1',
  28389. '};',
  28390. 'this.$rtti.$Enum("TFlags$a", {',
  28391. ' minvalue: 0,',
  28392. ' maxvalue: 1,',
  28393. ' ordtype: 1,',
  28394. ' enumtype: this.TFlags$a',
  28395. '});',
  28396. 'this.$rtti.$Set("TFlags", {',
  28397. ' comptype: this.$rtti["TFlags$a"]',
  28398. '});',
  28399. 'this.f = {};',
  28400. '']),
  28401. LinesToStr([
  28402. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  28403. '']));
  28404. end;
  28405. procedure TTestModule.TestRTTI_StaticArray;
  28406. begin
  28407. WithTypeInfo:=true;
  28408. StartProgram(false);
  28409. Add('type');
  28410. Add(' TFlag = (light,dark);');
  28411. Add(' TFlagNames = array[TFlag] of string;');
  28412. Add(' TBoolNames = array[boolean] of string;');
  28413. Add(' TByteArray = array[1..32768] of byte;');
  28414. Add(' TProc = function(f: TBoolNames): TFlagNames;');
  28415. Add('var p: pointer;');
  28416. Add('begin');
  28417. Add(' p:=typeinfo(TFlagNames);');
  28418. Add(' p:=typeinfo(TBoolNames);');
  28419. ConvertProgram;
  28420. CheckSource('TestRTTI_StaticArray',
  28421. LinesToStr([ // statements
  28422. 'this.TFlag = {',
  28423. ' "0": "light",',
  28424. ' light: 0,',
  28425. ' "1": "dark",',
  28426. ' dark: 1',
  28427. '};',
  28428. 'this.$rtti.$Enum("TFlag", {',
  28429. ' minvalue: 0,',
  28430. ' maxvalue: 1,',
  28431. ' ordtype: 1,',
  28432. ' enumtype: this.TFlag',
  28433. '});',
  28434. 'this.$rtti.$StaticArray("TFlagNames", {',
  28435. ' dims: [2],',
  28436. ' eltype: rtl.string',
  28437. '});',
  28438. 'this.$rtti.$StaticArray("TBoolNames", {',
  28439. ' dims: [2],',
  28440. ' eltype: rtl.string',
  28441. '});',
  28442. 'this.$rtti.$StaticArray("TByteArray", {',
  28443. ' dims: [32768],',
  28444. ' eltype: rtl.byte',
  28445. '});',
  28446. 'this.$rtti.$ProcVar("TProc", {',
  28447. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TBoolNames"]]], this.$rtti["TFlagNames"])',
  28448. '});',
  28449. 'this.p = null;',
  28450. '']),
  28451. LinesToStr([ // $mod.$main
  28452. '$mod.p = $mod.$rtti["TFlagNames"];',
  28453. '$mod.p = $mod.$rtti["TBoolNames"];',
  28454. '']));
  28455. end;
  28456. procedure TTestModule.TestRTTI_DynArray;
  28457. begin
  28458. WithTypeInfo:=true;
  28459. StartProgram(false);
  28460. Add('type');
  28461. Add(' TArrStr = array of string;');
  28462. Add(' TArr2Dim = array of tarrstr;');
  28463. Add(' TProc = function(f: TArrStr): TArr2Dim;');
  28464. Add('var p: pointer;');
  28465. Add('begin');
  28466. Add(' p:=typeinfo(tarrstr);');
  28467. Add(' p:=typeinfo(tarr2dim);');
  28468. ConvertProgram;
  28469. CheckSource('TestRTTI_DynArray',
  28470. LinesToStr([ // statements
  28471. 'this.$rtti.$DynArray("TArrStr", {',
  28472. ' eltype: rtl.string',
  28473. '});',
  28474. 'this.$rtti.$DynArray("TArr2Dim", {',
  28475. ' eltype: this.$rtti["TArrStr"]',
  28476. '});',
  28477. 'this.$rtti.$ProcVar("TProc", {',
  28478. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TArrStr"]]], this.$rtti["TArr2Dim"])',
  28479. '});',
  28480. 'this.p = null;',
  28481. '']),
  28482. LinesToStr([ // $mod.$main
  28483. '$mod.p = $mod.$rtti["TArrStr"];',
  28484. '$mod.p = $mod.$rtti["TArr2Dim"];',
  28485. '']));
  28486. end;
  28487. procedure TTestModule.TestRTTI_ArrayNestedAnonymous;
  28488. begin
  28489. WithTypeInfo:=true;
  28490. StartProgram(false);
  28491. Add('type');
  28492. Add(' TArr = array of array of longint;');
  28493. Add('var a: TArr;');
  28494. Add('begin');
  28495. ConvertProgram;
  28496. CheckSource('TestRTTI_ArrayNestedAnonymous',
  28497. LinesToStr([ // statements
  28498. 'this.$rtti.$DynArray("TArr$a", {',
  28499. ' eltype: rtl.longint',
  28500. '});',
  28501. 'this.$rtti.$DynArray("TArr", {',
  28502. ' eltype: this.$rtti["TArr$a"]',
  28503. '});',
  28504. 'this.a = [];',
  28505. '']),
  28506. LinesToStr([ // $mod.$main
  28507. ]));
  28508. end;
  28509. procedure TTestModule.TestRTTI_PublishedMethodOverloadFail;
  28510. begin
  28511. WithTypeInfo:=true;
  28512. StartProgram(false);
  28513. Add('type');
  28514. Add(' TObject = class');
  28515. Add(' published');
  28516. Add(' procedure Proc; virtual; abstract;');
  28517. Add(' procedure Proc(Sender: tobject); virtual; abstract;');
  28518. Add(' end;');
  28519. Add('begin');
  28520. SetExpectedPasResolverError('Duplicate published method "Proc" at test1.pp(6,19)',
  28521. nDuplicatePublishedMethodXAtY);
  28522. ConvertProgram;
  28523. end;
  28524. procedure TTestModule.TestRTTI_PublishedMethodExternalFail;
  28525. begin
  28526. WithTypeInfo:=true;
  28527. StartProgram(false);
  28528. Add('type');
  28529. Add(' TObject = class');
  28530. Add(' published');
  28531. Add(' procedure Proc; external name ''foo'';');
  28532. Add(' end;');
  28533. Add('begin');
  28534. SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
  28535. nPublishedNameMustMatchExternal);
  28536. ConvertProgram;
  28537. end;
  28538. procedure TTestModule.TestRTTI_PublishedClassPropertyFail;
  28539. begin
  28540. WithTypeInfo:=true;
  28541. StartProgram(false);
  28542. Add('type');
  28543. Add(' TObject = class');
  28544. Add(' class var FA: longint;');
  28545. Add(' published');
  28546. Add(' class property A: longint read FA;');
  28547. Add(' end;');
  28548. Add('begin');
  28549. SetExpectedPasResolverError('Invalid published property modifier "class"',
  28550. nInvalidXModifierY);
  28551. ConvertProgram;
  28552. end;
  28553. procedure TTestModule.TestRTTI_PublishedClassFieldFail;
  28554. begin
  28555. WithTypeInfo:=true;
  28556. StartProgram(false);
  28557. Add('type');
  28558. Add(' TObject = class');
  28559. Add(' published');
  28560. Add(' class var FA: longint;');
  28561. Add(' end;');
  28562. Add('begin');
  28563. SetExpectedPasResolverError(sSymbolCannotBePublished,
  28564. nSymbolCannotBePublished);
  28565. ConvertProgram;
  28566. end;
  28567. procedure TTestModule.TestRTTI_PublishedFieldExternalFail;
  28568. begin
  28569. WithTypeInfo:=true;
  28570. StartProgram(false);
  28571. Add('{$modeswitch externalclass}');
  28572. Add('type');
  28573. Add(' TObject = class');
  28574. Add(' published');
  28575. Add(' V: longint; external name ''foo'';');
  28576. Add(' end;');
  28577. Add('begin');
  28578. SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
  28579. nPublishedNameMustMatchExternal);
  28580. ConvertProgram;
  28581. end;
  28582. procedure TTestModule.TestRTTI_Class_Field;
  28583. begin
  28584. WithTypeInfo:=true;
  28585. StartProgram(false);
  28586. Add('{$modeswitch externalclass}');
  28587. Add('type');
  28588. Add(' TObject = class');
  28589. Add(' private');
  28590. Add(' FPropA: string;');
  28591. Add(' published');
  28592. Add(' VarLI: longint;');
  28593. Add(' VarC: char;');
  28594. Add(' VarS: string;');
  28595. Add(' VarD: double;');
  28596. Add(' VarB: boolean;');
  28597. Add(' VarLW: longword;');
  28598. Add(' VarSmI: smallint;');
  28599. Add(' VarW: word;');
  28600. Add(' VarShI: shortint;');
  28601. Add(' VarBy: byte;');
  28602. Add(' VarExt: longint external name ''VarExt'';');
  28603. Add(' ArrA, ArrB: array of byte;');
  28604. Add(' end;');
  28605. Add('var p: pointer;');
  28606. Add(' Obj: tobject;');
  28607. Add('begin');
  28608. Add(' p:=typeinfo(tobject);');
  28609. Add(' p:=typeinfo(p);');
  28610. Add(' p:=typeinfo(obj);');
  28611. ConvertProgram;
  28612. CheckSource('TestRTTI_Class_Field',
  28613. LinesToStr([ // statements
  28614. 'rtl.createClass(this, "TObject", null, function () {',
  28615. ' this.$init = function () {',
  28616. ' this.FPropA = "";',
  28617. ' this.VarLI = 0;',
  28618. ' this.VarC = "";',
  28619. ' this.VarS = "";',
  28620. ' this.VarD = 0.0;',
  28621. ' this.VarB = false;',
  28622. ' this.VarLW = 0;',
  28623. ' this.VarSmI = 0;',
  28624. ' this.VarW = 0;',
  28625. ' this.VarShI = 0;',
  28626. ' this.VarBy = 0;',
  28627. ' this.ArrA = [];',
  28628. ' this.ArrB = [];',
  28629. ' };',
  28630. ' this.$final = function () {',
  28631. ' this.ArrA = undefined;',
  28632. ' this.ArrB = undefined;',
  28633. ' };',
  28634. ' var $r = this.$rtti;',
  28635. ' $r.addField("VarLI", rtl.longint);',
  28636. ' $r.addField("VarC", rtl.char);',
  28637. ' $r.addField("VarS", rtl.string);',
  28638. ' $r.addField("VarD", rtl.double);',
  28639. ' $r.addField("VarB", rtl.boolean);',
  28640. ' $r.addField("VarLW", rtl.longword);',
  28641. ' $r.addField("VarSmI", rtl.smallint);',
  28642. ' $r.addField("VarW", rtl.word);',
  28643. ' $r.addField("VarShI", rtl.shortint);',
  28644. ' $r.addField("VarBy", rtl.byte);',
  28645. ' $r.addField("VarExt", rtl.longint);',
  28646. ' $mod.$rtti.$DynArray("TObject.ArrB$a", {',
  28647. ' eltype: rtl.byte',
  28648. ' });',
  28649. ' $r.addField("ArrA", $mod.$rtti["TObject.ArrB$a"]);',
  28650. ' $r.addField("ArrB", $mod.$rtti["TObject.ArrB$a"]);',
  28651. '});',
  28652. 'this.p = null;',
  28653. 'this.Obj = null;',
  28654. '']),
  28655. LinesToStr([ // $mod.$main
  28656. '$mod.p = $mod.$rtti["TObject"];',
  28657. '$mod.p = rtl.pointer;',
  28658. '$mod.p = $mod.Obj.$rtti;',
  28659. '']));
  28660. end;
  28661. procedure TTestModule.TestRTTI_Class_Method;
  28662. begin
  28663. WithTypeInfo:=true;
  28664. StartProgram(false);
  28665. Add('type');
  28666. Add(' TObject = class');
  28667. Add(' private');
  28668. Add(' procedure Internal; external name ''$intern'';');
  28669. Add(' published');
  28670. Add(' procedure Click; virtual; abstract;');
  28671. Add(' procedure Notify(Sender: TObject); virtual; abstract;');
  28672. Add(' function GetNotify: boolean; external name ''GetNotify'';');
  28673. Add(' procedure Println(a,b: longint); varargs; virtual; abstract;');
  28674. Add(' end;');
  28675. Add('begin');
  28676. ConvertProgram;
  28677. CheckSource('TestRTTI_Class_Method',
  28678. LinesToStr([ // statements
  28679. 'rtl.createClass(this, "TObject", null, function () {',
  28680. ' this.$init = function () {',
  28681. ' };',
  28682. ' this.$final = function () {',
  28683. ' };',
  28684. ' var $r = this.$rtti;',
  28685. ' $r.addMethod("Click", 0, null);',
  28686. ' $r.addMethod("Notify", 0, [["Sender", $r]]);',
  28687. ' $r.addMethod("GetNotify", 1, null, rtl.boolean,{flags: 4});',
  28688. ' $r.addMethod("Println", 0, [["a", rtl.longint], ["b", rtl.longint]], null, {',
  28689. ' flags: 2',
  28690. ' });',
  28691. '});',
  28692. '']),
  28693. LinesToStr([ // $mod.$main
  28694. '']));
  28695. end;
  28696. procedure TTestModule.TestRTTI_Class_MethodArgFlags;
  28697. begin
  28698. WithTypeInfo:=true;
  28699. StartProgram(false);
  28700. Add('type');
  28701. Add(' TObject = class');
  28702. Add(' published');
  28703. Add(' procedure OpenArray(const Args: array of string); virtual; abstract;');
  28704. Add(' procedure ByRef(var Value: longint; out Item: longint); virtual; abstract;');
  28705. Add(' procedure Untyped(var Value; out Item); virtual; abstract;');
  28706. Add(' end;');
  28707. Add('begin');
  28708. ConvertProgram;
  28709. CheckSource('TestRTTI_Class_MethodOpenArray',
  28710. LinesToStr([ // statements
  28711. 'rtl.createClass(this, "TObject", null, function () {',
  28712. ' this.$init = function () {',
  28713. ' };',
  28714. ' this.$final = function () {',
  28715. ' };',
  28716. ' var $r = this.$rtti;',
  28717. '$r.addMethod("OpenArray", 0, [["Args", rtl.string, 10]]);',
  28718. '$r.addMethod("ByRef", 0, [["Value", rtl.longint, 1], ["Item", rtl.longint, 4]]);',
  28719. '$r.addMethod("Untyped", 0, [["Value", null, 1], ["Item", null, 4]]);',
  28720. '});',
  28721. '']),
  28722. LinesToStr([ // $mod.$main
  28723. '']));
  28724. end;
  28725. procedure TTestModule.TestRTTI_Class_Property;
  28726. begin
  28727. WithTypeInfo:=true;
  28728. StartProgram(false);
  28729. Add('{$modeswitch externalclass}');
  28730. Add('type');
  28731. Add(' TObject = class');
  28732. Add(' private');
  28733. Add(' FColor: longint;');
  28734. Add(' FColorStored: boolean;');
  28735. Add(' procedure SetColor(Value: longint); virtual; abstract;');
  28736. Add(' function GetColor: longint; virtual; abstract;');
  28737. Add(' function GetColorStored: boolean; virtual; abstract;');
  28738. Add(' FExtSize: longint external name ''$extSize'';');
  28739. Add(' FExtSizeStored: boolean external name ''$extSizeStored'';');
  28740. Add(' procedure SetExtSize(Value: longint); external name ''$setSize'';');
  28741. Add(' function GetExtSize: longint; external name ''$getSize'';');
  28742. Add(' function GetExtSizeStored: boolean; external name ''$getExtSizeStored'';');
  28743. Add(' published');
  28744. Add(' property ColorA: longint read FColor;');
  28745. Add(' property ColorB: longint write FColor;');
  28746. Add(' property ColorC: longint read GetColor write SetColor;');
  28747. Add(' property ColorD: longint read FColor write FColor stored FColorStored;');
  28748. Add(' property ExtSizeA: longint read FExtSize write FExtSize;');
  28749. Add(' property ExtSizeB: longint read GetExtSize write SetExtSize stored FExtSizeStored;');
  28750. Add(' property ExtSizeC: longint read FExtSize write FExtSize stored GetExtSizeStored;');
  28751. Add(' end;');
  28752. Add('begin');
  28753. ConvertProgram;
  28754. CheckSource('TestRTTI_Class_Property',
  28755. LinesToStr([ // statements
  28756. 'rtl.createClass(this, "TObject", null, function () {',
  28757. ' this.$init = function () {',
  28758. ' this.FColor = 0;',
  28759. ' this.FColorStored = false;',
  28760. ' };',
  28761. ' this.$final = function () {',
  28762. ' };',
  28763. ' var $r = this.$rtti;',
  28764. ' $r.addProperty("ColorA", 0, rtl.longint, "FColor", "");',
  28765. ' $r.addProperty("ColorB", 0, rtl.longint, "", "FColor");',
  28766. ' $r.addProperty("ColorC", 3, rtl.longint, "GetColor", "SetColor");',
  28767. ' $r.addProperty(',
  28768. ' "ColorD",',
  28769. ' 8,',
  28770. ' rtl.longint,',
  28771. ' "FColor",',
  28772. ' "FColor",',
  28773. ' {',
  28774. ' stored: "FColorStored"',
  28775. ' }',
  28776. ' );',
  28777. ' $r.addProperty("ExtSizeA", 0, rtl.longint, "$extSize", "$extSize");',
  28778. ' $r.addProperty(',
  28779. ' "ExtSizeB",',
  28780. ' 11,',
  28781. ' rtl.longint,',
  28782. ' "$getSize",',
  28783. ' "$setSize",',
  28784. ' {',
  28785. ' stored: "$extSizeStored"',
  28786. ' }',
  28787. ' );',
  28788. ' $r.addProperty(',
  28789. ' "ExtSizeC",',
  28790. ' 12,',
  28791. ' rtl.longint,',
  28792. ' "$extSize",',
  28793. ' "$extSize",',
  28794. ' {',
  28795. ' stored: "$getExtSizeStored"',
  28796. ' }',
  28797. ' );',
  28798. '});',
  28799. '']),
  28800. LinesToStr([ // $mod.$main
  28801. '']));
  28802. end;
  28803. procedure TTestModule.TestRTTI_Class_PropertyParams;
  28804. begin
  28805. WithTypeInfo:=true;
  28806. StartProgram(false);
  28807. Add('{$modeswitch externalclass}');
  28808. Add('type');
  28809. Add(' integer = longint;');
  28810. Add(' TObject = class');
  28811. Add(' private');
  28812. Add(' function GetItems(i: integer): tobject; virtual; abstract;');
  28813. Add(' procedure SetItems(i: integer; value: tobject); virtual; abstract;');
  28814. Add(' function GetValues(const i: integer; var b: boolean): char; virtual; abstract;');
  28815. Add(' procedure SetValues(const i: integer; var b: boolean; value: char); virtual; abstract;');
  28816. Add(' published');
  28817. Add(' property Items[Index: integer]: tobject read getitems write setitems;');
  28818. Add(' property Values[const keya: integer; var keyb: boolean]: char read getvalues write setvalues;');
  28819. Add(' end;');
  28820. Add('begin');
  28821. ConvertProgram;
  28822. CheckSource('TestRTTI_Class_PropertyParams',
  28823. LinesToStr([ // statements
  28824. 'rtl.createClass(this, "TObject", null, function () {',
  28825. ' this.$init = function () {',
  28826. ' };',
  28827. ' this.$final = function () {',
  28828. ' };',
  28829. ' var $r = this.$rtti;',
  28830. ' $r.addProperty("Items", 3, $r, "GetItems", "SetItems");',
  28831. ' $r.addProperty("Values", 3, rtl.char, "GetValues", "SetValues");',
  28832. '});',
  28833. '']),
  28834. LinesToStr([ // $mod.$main
  28835. '']));
  28836. end;
  28837. procedure TTestModule.TestRTTI_Class_OtherUnit_TypeAlias;
  28838. begin
  28839. WithTypeInfo:=true;
  28840. AddModuleWithIntfImplSrc('unit1.pas',
  28841. 'type TColor = -5..5;',
  28842. '');
  28843. StartProgram(true);
  28844. Add([
  28845. 'uses unit1;',
  28846. 'type',
  28847. ' TColorAlias = TColor;',
  28848. ' TColorTypeAlias = type TColor;',
  28849. ' TObject = class',
  28850. ' private',
  28851. ' fColor: TColor;',
  28852. ' fAlias: TColorAlias;',
  28853. ' fTypeAlias: TColorTypeAlias;',
  28854. ' published',
  28855. ' property Color: TColor read fcolor;',
  28856. ' property Alias: TColorAlias read falias;',
  28857. ' property TypeAlias: TColorTypeAlias read ftypealias;',
  28858. ' end;',
  28859. 'begin',
  28860. '']);
  28861. ConvertProgram;
  28862. CheckSource('TestRTTI_Class_OtherUnit_TypeAlias',
  28863. LinesToStr([ // statements
  28864. 'this.$rtti.$inherited("TColorTypeAlias", pas.unit1.$rtti["TColor"], {});',
  28865. 'rtl.createClass(this, "TObject", null, function () {',
  28866. ' this.$init = function () {',
  28867. ' this.fColor = 0;',
  28868. ' this.fAlias = 0;',
  28869. ' this.fTypeAlias = 0;',
  28870. ' };',
  28871. ' this.$final = function () {',
  28872. ' };',
  28873. ' var $r = this.$rtti;',
  28874. ' $r.addProperty("Color", 0, pas.unit1.$rtti["TColor"], "fColor", "");',
  28875. ' $r.addProperty("Alias", 0, pas.unit1.$rtti["TColor"], "fAlias", "");',
  28876. ' $r.addProperty("TypeAlias", 0, $mod.$rtti["TColorTypeAlias"], "fTypeAlias", "");',
  28877. '});',
  28878. '']),
  28879. LinesToStr([ // $mod.$main
  28880. '']));
  28881. end;
  28882. procedure TTestModule.TestRTTI_Class_OmitRTTI;
  28883. begin
  28884. WithTypeInfo:=true;
  28885. StartProgram(false);
  28886. Add([
  28887. '{$modeswitch omitrtti}',
  28888. 'type',
  28889. ' TObject = class',
  28890. ' private',
  28891. ' FA: byte;',
  28892. ' published',
  28893. ' property A: byte read FA write FA;',
  28894. ' end;',
  28895. 'begin']);
  28896. ConvertProgram;
  28897. CheckSource('TestRTTI_Class_OmitRTTI',
  28898. LinesToStr([ // statements
  28899. 'rtl.createClass(this, "TObject", null, function () {',
  28900. ' this.$init = function () {',
  28901. ' this.FA = 0;',
  28902. ' };',
  28903. ' this.$final = function () {',
  28904. ' };',
  28905. '});',
  28906. '']),
  28907. LinesToStr([ // $mod.$main
  28908. '']));
  28909. end;
  28910. procedure TTestModule.TestRTTI_IndexModifier;
  28911. begin
  28912. WithTypeInfo:=true;
  28913. StartProgram(false);
  28914. Add([
  28915. 'type',
  28916. ' TEnum = (red, blue);',
  28917. ' TObject = class',
  28918. ' FB: boolean;',
  28919. ' procedure SetIntBool(Index: longint; b: boolean); virtual; abstract;',
  28920. ' function GetBoolBool(Index: boolean): boolean; virtual; abstract;',
  28921. ' procedure SetBoolBool(Index: boolean; b: boolean); virtual; abstract;',
  28922. ' function GetEnumBool(Index: TEnum): boolean; virtual; abstract;',
  28923. ' function GetStrIntBool(A: String; I: longint): boolean; virtual; abstract;',
  28924. ' procedure SetStrIntBool(A: String; I: longint; b: boolean); virtual; abstract;',
  28925. ' published',
  28926. ' property B1: boolean index 1 read FB write SetIntBool;',
  28927. ' property B2: boolean index TEnum.blue read GetEnumBool write FB;',
  28928. ' property I1[A: String]: boolean index 2 read GetStrIntBool write SetStrIntBool;',
  28929. ' end;',
  28930. 'begin']);
  28931. ConvertProgram;
  28932. CheckSource('TestRTTI_IndexModifier',
  28933. LinesToStr([ // statements
  28934. 'this.TEnum = {',
  28935. ' "0": "red",',
  28936. ' red: 0,',
  28937. ' "1": "blue",',
  28938. ' blue: 1',
  28939. '};',
  28940. 'this.$rtti.$Enum("TEnum", {',
  28941. ' minvalue: 0,',
  28942. ' maxvalue: 1,',
  28943. ' ordtype: 1,',
  28944. ' enumtype: this.TEnum',
  28945. '});',
  28946. 'rtl.createClass(this, "TObject", null, function () {',
  28947. ' this.$init = function () {',
  28948. ' this.FB = false;',
  28949. ' };',
  28950. ' this.$final = function () {',
  28951. ' };',
  28952. ' var $r = this.$rtti;',
  28953. ' $r.addProperty(',
  28954. ' "B1",',
  28955. ' 18,',
  28956. ' rtl.boolean,',
  28957. ' "FB",',
  28958. ' "SetIntBool",',
  28959. ' {',
  28960. ' index: 1',
  28961. ' }',
  28962. ' );',
  28963. ' $r.addProperty(',
  28964. ' "B2",',
  28965. ' 17,',
  28966. ' rtl.boolean,',
  28967. ' "GetEnumBool",',
  28968. ' "FB",',
  28969. ' {',
  28970. ' index: $mod.TEnum.blue',
  28971. ' }',
  28972. ' );',
  28973. ' $r.addProperty(',
  28974. ' "I1",',
  28975. ' 19,',
  28976. ' rtl.boolean,',
  28977. ' "GetStrIntBool",',
  28978. ' "SetStrIntBool",',
  28979. ' {',
  28980. ' index: 2',
  28981. ' }',
  28982. ' );',
  28983. '});',
  28984. '']),
  28985. LinesToStr([ // $mod.$main
  28986. '']));
  28987. end;
  28988. procedure TTestModule.TestRTTI_StoredModifier;
  28989. begin
  28990. WithTypeInfo:=true;
  28991. StartProgram(false);
  28992. Add([
  28993. 'const',
  28994. ' ConstB = true;',
  28995. 'type',
  28996. ' TObject = class',
  28997. ' private',
  28998. ' FB: boolean;',
  28999. ' function IsBStored: boolean; virtual; abstract;',
  29000. ' published',
  29001. ' property BoolA: boolean read FB stored true;',
  29002. ' property BoolB: boolean read FB stored false;',
  29003. ' property BoolC: boolean read FB stored FB;',
  29004. ' property BoolD: boolean read FB stored ConstB;',
  29005. ' property BoolE: boolean read FB stored IsBStored;',
  29006. ' end;',
  29007. 'begin']);
  29008. ConvertProgram;
  29009. CheckSource('TestRTTI_StoredModifier',
  29010. LinesToStr([ // statements
  29011. 'this.ConstB = true;',
  29012. 'rtl.createClass(this, "TObject", null, function () {',
  29013. ' this.$init = function () {',
  29014. ' this.FB = false;',
  29015. ' };',
  29016. ' this.$final = function () {',
  29017. ' };',
  29018. ' var $r = this.$rtti;',
  29019. ' $r.addProperty("BoolA", 0, rtl.boolean, "FB", "");',
  29020. ' $r.addProperty("BoolB", 4, rtl.boolean, "FB", "");',
  29021. ' $r.addProperty(',
  29022. ' "BoolC",',
  29023. ' 8,',
  29024. ' rtl.boolean,',
  29025. ' "FB",',
  29026. ' "",',
  29027. ' {',
  29028. ' stored: "FB"',
  29029. ' }',
  29030. ' );',
  29031. ' $r.addProperty("BoolD", 0, rtl.boolean, "FB", "");',
  29032. ' $r.addProperty(',
  29033. ' "BoolE",',
  29034. ' 12,',
  29035. ' rtl.boolean,',
  29036. ' "FB",',
  29037. ' "",',
  29038. ' {',
  29039. ' stored: "IsBStored"',
  29040. ' }',
  29041. ' );',
  29042. '});',
  29043. '']),
  29044. LinesToStr([ // $mod.$main
  29045. '']));
  29046. end;
  29047. procedure TTestModule.TestRTTI_DefaultValue;
  29048. begin
  29049. WithTypeInfo:=true;
  29050. StartProgram(false);
  29051. Add([
  29052. 'type',
  29053. ' TEnum = (red, blue);',
  29054. 'const',
  29055. ' CB = true or false;',
  29056. ' CI = 1+2;',
  29057. 'type',
  29058. ' TObject = class',
  29059. ' FB: boolean;',
  29060. ' FI: longint;',
  29061. ' FE: TEnum;',
  29062. ' published',
  29063. ' property B1: boolean read FB default true;',
  29064. ' property B2: boolean read FB default CB;',
  29065. ' property B3: boolean read FB default test1.cb;',
  29066. ' property I1: longint read FI default 2;',
  29067. ' property I2: longint read FI default CI;',
  29068. ' property E1: TEnum read FE default red;',
  29069. ' property E2: TEnum read FE default TEnum.blue;',
  29070. ' end;',
  29071. 'begin']);
  29072. ConvertProgram;
  29073. CheckSource('TestRTTI_DefaultValue',
  29074. LinesToStr([ // statements
  29075. 'this.TEnum = {',
  29076. ' "0": "red",',
  29077. ' red: 0,',
  29078. ' "1": "blue",',
  29079. ' blue: 1',
  29080. '};',
  29081. 'this.$rtti.$Enum("TEnum", {',
  29082. ' minvalue: 0,',
  29083. ' maxvalue: 1,',
  29084. ' ordtype: 1,',
  29085. ' enumtype: this.TEnum',
  29086. '});',
  29087. 'this.CB = true || false;',
  29088. 'this.CI = 1 + 2;',
  29089. 'rtl.createClass(this, "TObject", null, function () {',
  29090. ' this.$init = function () {',
  29091. ' this.FB = false;',
  29092. ' this.FI = 0;',
  29093. ' this.FE = 0;',
  29094. ' };',
  29095. ' this.$final = function () {',
  29096. ' };',
  29097. ' var $r = this.$rtti;',
  29098. ' $r.addProperty(',
  29099. ' "B1",',
  29100. ' 0,',
  29101. ' rtl.boolean,',
  29102. ' "FB",',
  29103. ' "",',
  29104. ' {',
  29105. ' Default: true',
  29106. ' }',
  29107. ' );',
  29108. ' $r.addProperty(',
  29109. ' "B2",',
  29110. ' 0,',
  29111. ' rtl.boolean,',
  29112. ' "FB",',
  29113. ' "",',
  29114. ' {',
  29115. ' Default: true',
  29116. ' }',
  29117. ' );',
  29118. ' $r.addProperty(',
  29119. ' "B3",',
  29120. ' 0,',
  29121. ' rtl.boolean,',
  29122. ' "FB",',
  29123. ' "",',
  29124. ' {',
  29125. ' Default: true',
  29126. ' }',
  29127. ' );',
  29128. ' $r.addProperty(',
  29129. ' "I1",',
  29130. ' 0,',
  29131. ' rtl.longint,',
  29132. ' "FI",',
  29133. ' "",',
  29134. ' {',
  29135. ' Default: 2',
  29136. ' }',
  29137. ' );',
  29138. ' $r.addProperty(',
  29139. ' "I2",',
  29140. ' 0,',
  29141. ' rtl.longint,',
  29142. ' "FI",',
  29143. ' "",',
  29144. ' {',
  29145. ' Default: 3',
  29146. ' }',
  29147. ' );',
  29148. ' $r.addProperty(',
  29149. ' "E1",',
  29150. ' 0,',
  29151. ' $mod.$rtti["TEnum"],',
  29152. ' "FE",',
  29153. ' "",',
  29154. ' {',
  29155. ' Default: $mod.TEnum.red',
  29156. ' }',
  29157. ' );',
  29158. ' $r.addProperty(',
  29159. ' "E2",',
  29160. ' 0,',
  29161. ' $mod.$rtti["TEnum"],',
  29162. ' "FE",',
  29163. ' "",',
  29164. ' {',
  29165. ' Default: $mod.TEnum.blue',
  29166. ' }',
  29167. ' );',
  29168. '});',
  29169. '']),
  29170. LinesToStr([ // $mod.$main
  29171. '']));
  29172. end;
  29173. procedure TTestModule.TestRTTI_DefaultValueSet;
  29174. begin
  29175. WithTypeInfo:=true;
  29176. StartProgram(false);
  29177. Add([
  29178. 'type',
  29179. ' TEnum = (red, blue);',
  29180. ' TSet = set of TEnum;',
  29181. 'const',
  29182. ' CSet = [red,blue];',
  29183. 'type',
  29184. ' TObject = class',
  29185. ' FSet: TSet;',
  29186. ' published',
  29187. ' property Set1: TSet read FSet default [];',
  29188. ' property Set2: TSet read FSet default [red];',
  29189. ' property Set3: TSet read FSet default [red,blue];',
  29190. ' property Set4: TSet read FSet default CSet;',
  29191. ' end;',
  29192. 'begin']);
  29193. ConvertProgram;
  29194. CheckSource('TestRTTI_DefaultValueSet',
  29195. LinesToStr([ // statements
  29196. 'this.TEnum = {',
  29197. ' "0": "red",',
  29198. ' red: 0,',
  29199. ' "1": "blue",',
  29200. ' blue: 1',
  29201. '};',
  29202. 'this.$rtti.$Enum("TEnum", {',
  29203. ' minvalue: 0,',
  29204. ' maxvalue: 1,',
  29205. ' ordtype: 1,',
  29206. ' enumtype: this.TEnum',
  29207. '});',
  29208. 'this.$rtti.$Set("TSet", {',
  29209. ' comptype: this.$rtti["TEnum"]',
  29210. '});',
  29211. 'this.CSet = rtl.createSet(this.TEnum.red, this.TEnum.blue);',
  29212. 'rtl.createClass(this, "TObject", null, function () {',
  29213. ' this.$init = function () {',
  29214. ' this.FSet = {};',
  29215. ' };',
  29216. ' this.$final = function () {',
  29217. ' this.FSet = undefined;',
  29218. ' };',
  29219. ' var $r = this.$rtti;',
  29220. ' $r.addProperty(',
  29221. ' "Set1",',
  29222. ' 0,',
  29223. ' $mod.$rtti["TSet"],',
  29224. ' "FSet",',
  29225. ' "",',
  29226. ' {',
  29227. ' Default: {}',
  29228. ' }',
  29229. ' );',
  29230. ' $r.addProperty(',
  29231. ' "Set2",',
  29232. ' 0,',
  29233. ' $mod.$rtti["TSet"],',
  29234. ' "FSet",',
  29235. ' "",',
  29236. ' {',
  29237. ' Default: rtl.createSet($mod.TEnum.red)',
  29238. ' }',
  29239. ' );',
  29240. ' $r.addProperty(',
  29241. ' "Set3",',
  29242. ' 0,',
  29243. ' $mod.$rtti["TSet"],',
  29244. ' "FSet",',
  29245. ' "",',
  29246. ' {',
  29247. ' Default: rtl.createSet($mod.TEnum.red, $mod.TEnum.blue)',
  29248. ' }',
  29249. ' );',
  29250. ' $r.addProperty(',
  29251. ' "Set4",',
  29252. ' 0,',
  29253. ' $mod.$rtti["TSet"],',
  29254. ' "FSet",',
  29255. ' "",',
  29256. ' {',
  29257. ' Default: $mod.CSet',
  29258. ' }',
  29259. ' );',
  29260. '});',
  29261. '']),
  29262. LinesToStr([ // $mod.$main
  29263. '']));
  29264. end;
  29265. procedure TTestModule.TestRTTI_DefaultValueRangeType;
  29266. begin
  29267. WithTypeInfo:=true;
  29268. StartProgram(false);
  29269. Add([
  29270. 'type',
  29271. ' TRg = -1..1;',
  29272. 'const',
  29273. ' l = low(TRg);',
  29274. ' h = high(TRg);',
  29275. 'type',
  29276. ' TObject = class',
  29277. ' FV: TRg;',
  29278. ' published',
  29279. ' property V1: TRg read FV default -1;',
  29280. ' end;',
  29281. 'begin']);
  29282. ConvertProgram;
  29283. CheckSource('TestRTTI_DefaultValueRangeType',
  29284. LinesToStr([ // statements
  29285. 'this.$rtti.$Int("TRg", {',
  29286. ' minvalue: -1,',
  29287. ' maxvalue: 1,',
  29288. ' ordtype: 0',
  29289. '});',
  29290. 'this.l = -1;',
  29291. 'this.h = 1;',
  29292. 'rtl.createClass(this, "TObject", null, function () {',
  29293. ' this.$init = function () {',
  29294. ' this.FV = 0;',
  29295. ' };',
  29296. ' this.$final = function () {',
  29297. ' };',
  29298. ' var $r = this.$rtti;',
  29299. ' $r.addProperty(',
  29300. ' "V1",',
  29301. ' 0,',
  29302. ' $mod.$rtti["TRg"],',
  29303. ' "FV",',
  29304. ' "",',
  29305. ' {',
  29306. ' Default: -1',
  29307. ' }',
  29308. ' );',
  29309. '});',
  29310. '']),
  29311. LinesToStr([ // $mod.$main
  29312. '']));
  29313. end;
  29314. procedure TTestModule.TestRTTI_DefaultValueInherit;
  29315. begin
  29316. WithTypeInfo:=true;
  29317. StartProgram(false);
  29318. Add([
  29319. 'type',
  29320. ' TObject = class',
  29321. ' FA, FB: byte;',
  29322. ' property A: byte read FA default 1;',
  29323. ' property B: byte read FB default 2;',
  29324. ' end;',
  29325. ' TBird = class',
  29326. ' published',
  29327. ' property A;',
  29328. ' property B nodefault;',
  29329. ' end;',
  29330. 'begin']);
  29331. ConvertProgram;
  29332. CheckSource('TestRTTI_DefaultValueInherit',
  29333. LinesToStr([ // statements
  29334. 'rtl.createClass(this, "TObject", null, function () {',
  29335. ' this.$init = function () {',
  29336. ' this.FA = 0;',
  29337. ' this.FB = 0;',
  29338. ' };',
  29339. ' this.$final = function () {',
  29340. ' };',
  29341. '});',
  29342. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  29343. ' var $r = this.$rtti;',
  29344. ' $r.addProperty(',
  29345. ' "A",',
  29346. ' 0,',
  29347. ' rtl.byte,',
  29348. ' "FA",',
  29349. ' "",',
  29350. ' {',
  29351. ' Default: 1',
  29352. ' }',
  29353. ' );',
  29354. ' $r.addProperty("B", 0, rtl.byte, "FB", "");',
  29355. '});',
  29356. '']),
  29357. LinesToStr([ // $mod.$main
  29358. '']));
  29359. end;
  29360. procedure TTestModule.TestRTTI_OverrideMethod;
  29361. begin
  29362. WithTypeInfo:=true;
  29363. StartProgram(false);
  29364. Add('type');
  29365. Add(' TObject = class');
  29366. Add(' published');
  29367. Add(' procedure DoIt; virtual; abstract;');
  29368. Add(' end;');
  29369. Add(' TSky = class');
  29370. Add(' published');
  29371. Add(' procedure DoIt; override;');
  29372. Add(' end;');
  29373. Add('procedure TSky.DoIt; begin end;');
  29374. Add('begin');
  29375. ConvertProgram;
  29376. CheckSource('TestRTTI_OverrideMethod',
  29377. LinesToStr([ // statements
  29378. 'rtl.createClass(this, "TObject", null, function () {',
  29379. ' this.$init = function () {',
  29380. ' };',
  29381. ' this.$final = function () {',
  29382. ' };',
  29383. ' var $r = this.$rtti;',
  29384. ' $r.addMethod("DoIt", 0, null);',
  29385. '});',
  29386. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  29387. ' this.DoIt = function () {',
  29388. ' };',
  29389. '});',
  29390. '']),
  29391. LinesToStr([ // $mod.$main
  29392. '']));
  29393. end;
  29394. procedure TTestModule.TestRTTI_ReintroduceMethod;
  29395. begin
  29396. WithTypeInfo:=true;
  29397. StartProgram(false);
  29398. Add([
  29399. 'type',
  29400. ' TObject = class',
  29401. ' published',
  29402. ' procedure DoIt;',
  29403. ' end;',
  29404. ' TSky = class',
  29405. ' published',
  29406. ' procedure DoIt; reintroduce;',
  29407. ' end;',
  29408. 'procedure TObject.DoIt; begin end;',
  29409. 'procedure TSky.DoIt;',
  29410. 'begin',
  29411. ' inherited DoIt;',
  29412. 'end;',
  29413. 'begin']);
  29414. ConvertProgram;
  29415. CheckSource('TestRTTI_ReintroduceMethod',
  29416. LinesToStr([ // statements
  29417. 'rtl.createClass(this, "TObject", null, function () {',
  29418. ' this.$init = function () {',
  29419. ' };',
  29420. ' this.$final = function () {',
  29421. ' };',
  29422. ' this.DoIt = function () {',
  29423. ' };',
  29424. ' var $r = this.$rtti;',
  29425. ' $r.addMethod("DoIt", 0, null);',
  29426. '});',
  29427. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  29428. ' this.DoIt = function () {',
  29429. ' $mod.TObject.DoIt.call(this);',
  29430. ' };',
  29431. ' var $r = this.$rtti;',
  29432. ' $r.addMethod("DoIt", 0, null);',
  29433. '});',
  29434. '']),
  29435. LinesToStr([ // $mod.$main
  29436. '']));
  29437. end;
  29438. procedure TTestModule.TestRTTI_OverloadProperty;
  29439. begin
  29440. WithTypeInfo:=true;
  29441. StartProgram(false);
  29442. Add('type');
  29443. Add(' TObject = class');
  29444. Add(' protected');
  29445. Add(' FFlag: longint;');
  29446. Add(' published');
  29447. Add(' property Flag: longint read fflag;');
  29448. Add(' end;');
  29449. Add(' TSky = class');
  29450. Add(' published');
  29451. Add(' property FLAG: longint write fflag;');
  29452. Add(' end;');
  29453. Add('begin');
  29454. ConvertProgram;
  29455. CheckSource('TestRTTI_OverrideMethod',
  29456. LinesToStr([ // statements
  29457. 'rtl.createClass(this, "TObject", null, function () {',
  29458. ' this.$init = function () {',
  29459. ' this.FFlag = 0;',
  29460. ' };',
  29461. ' this.$final = function () {',
  29462. ' };',
  29463. ' var $r = this.$rtti;',
  29464. ' $r.addProperty("Flag", 0, rtl.longint, "FFlag", "");',
  29465. '});',
  29466. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  29467. ' var $r = this.$rtti;',
  29468. ' $r.addProperty("Flag", 0, rtl.longint, "", "FFlag");',
  29469. '});',
  29470. '']),
  29471. LinesToStr([ // $mod.$main
  29472. '']));
  29473. end;
  29474. procedure TTestModule.TestRTTI_ClassForward;
  29475. begin
  29476. WithTypeInfo:=true;
  29477. StartProgram(false);
  29478. Add('type');
  29479. Add(' TObject = class end;');
  29480. Add(' tbridge = class;');
  29481. Add(' TProc = function: tbridge;');
  29482. Add(' TOger = class');
  29483. Add(' published');
  29484. Add(' FBridge: tbridge;');
  29485. Add(' procedure SetBridge(Value: tbridge); virtual; abstract;');
  29486. Add(' property Bridge: tbridge read fbridge write setbridge;');
  29487. Add(' end;');
  29488. Add(' TBridge = class');
  29489. Add(' FOger: toger;');
  29490. Add(' end;');
  29491. Add('var p: Pointer;');
  29492. Add(' b: tbridge;');
  29493. Add('begin');
  29494. Add(' p:=typeinfo(tbridge);');
  29495. Add(' p:=typeinfo(b);');
  29496. ConvertProgram;
  29497. CheckSource('TestRTTI_ClassForward',
  29498. LinesToStr([ // statements
  29499. 'rtl.createClass(this, "TObject", null, function () {',
  29500. ' this.$init = function () {',
  29501. ' };',
  29502. ' this.$final = function () {',
  29503. ' };',
  29504. '});',
  29505. 'this.$rtti.$Class("TBridge");',
  29506. 'this.$rtti.$ProcVar("TProc", {',
  29507. ' procsig: rtl.newTIProcSig(null, this.$rtti["TBridge"])',
  29508. '});',
  29509. 'rtl.createClass(this, "TOger", this.TObject, function () {',
  29510. ' this.$init = function () {',
  29511. ' $mod.TObject.$init.call(this);',
  29512. ' this.FBridge = null;',
  29513. ' };',
  29514. ' this.$final = function () {',
  29515. ' this.FBridge = undefined;',
  29516. ' $mod.TObject.$final.call(this);',
  29517. ' };',
  29518. ' var $r = this.$rtti;',
  29519. ' $r.addField("FBridge", $mod.$rtti["TBridge"]);',
  29520. ' $r.addMethod("SetBridge", 0, [["Value", $mod.$rtti["TBridge"]]]);',
  29521. ' $r.addProperty("Bridge", 2, $mod.$rtti["TBridge"], "FBridge", "SetBridge");',
  29522. '});',
  29523. 'rtl.createClass(this, "TBridge", this.TObject, function () {',
  29524. ' this.$init = function () {',
  29525. ' $mod.TObject.$init.call(this);',
  29526. ' this.FOger = null;',
  29527. ' };',
  29528. ' this.$final = function () {',
  29529. ' this.FOger = undefined;',
  29530. ' $mod.TObject.$final.call(this);',
  29531. ' };',
  29532. '});',
  29533. 'this.p = null;',
  29534. 'this.b = null;',
  29535. '']),
  29536. LinesToStr([ // $mod.$main
  29537. '$mod.p = $mod.$rtti["TBridge"];',
  29538. '$mod.p = $mod.b.$rtti;',
  29539. '']));
  29540. end;
  29541. procedure TTestModule.TestRTTI_ClassOf;
  29542. begin
  29543. WithTypeInfo:=true;
  29544. StartProgram(false);
  29545. Add('type');
  29546. Add(' TClass = class of tobject;');
  29547. Add(' TProcA = function: TClass;');
  29548. Add(' TObject = class');
  29549. Add(' published');
  29550. Add(' C: tclass;');
  29551. Add(' end;');
  29552. Add(' tfox = class;');
  29553. Add(' TBird = class end;');
  29554. Add(' TBirds = class of tbird;');
  29555. Add(' TFox = class end;');
  29556. Add(' TFoxes = class of tfox;');
  29557. Add(' TCows = class of TCow;');
  29558. Add(' TCow = class;');
  29559. Add(' TCow = class end;');
  29560. Add('begin');
  29561. ConvertProgram;
  29562. CheckSource('TestRTTI_ClassOf',
  29563. LinesToStr([ // statements
  29564. 'this.$rtti.$Class("TObject");',
  29565. 'this.$rtti.$ClassRef("TClass", {',
  29566. ' instancetype: this.$rtti["TObject"]',
  29567. '});',
  29568. 'this.$rtti.$ProcVar("TProcA", {',
  29569. ' procsig: rtl.newTIProcSig(null, this.$rtti["TClass"])',
  29570. '});',
  29571. 'rtl.createClass(this, "TObject", null, function () {',
  29572. ' this.$init = function () {',
  29573. ' this.C = null;',
  29574. ' };',
  29575. ' this.$final = function () {',
  29576. ' this.C = undefined;',
  29577. ' };',
  29578. ' var $r = this.$rtti;',
  29579. ' $r.addField("C", $mod.$rtti["TClass"]);',
  29580. '});',
  29581. 'this.$rtti.$Class("TFox");',
  29582. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  29583. '});',
  29584. 'this.$rtti.$ClassRef("TBirds", {',
  29585. ' instancetype: this.$rtti["TBird"]',
  29586. '});',
  29587. 'rtl.createClass(this, "TFox", this.TObject, function () {',
  29588. '});',
  29589. 'this.$rtti.$ClassRef("TFoxes", {',
  29590. ' instancetype: this.$rtti["TFox"]',
  29591. '});',
  29592. 'this.$rtti.$Class("TCow");',
  29593. 'this.$rtti.$ClassRef("TCows", {',
  29594. ' instancetype: this.$rtti["TCow"]',
  29595. '});',
  29596. 'rtl.createClass(this, "TCow", this.TObject, function () {',
  29597. '});',
  29598. '']),
  29599. LinesToStr([ // $mod.$main
  29600. '']));
  29601. end;
  29602. procedure TTestModule.TestRTTI_Record;
  29603. begin
  29604. WithTypeInfo:=true;
  29605. StartProgram(false);
  29606. Add('type');
  29607. Add(' integer = longint;');
  29608. Add(' TPoint = record');
  29609. Add(' x,y: integer;');
  29610. Add(' end;');
  29611. Add('var p: pointer;');
  29612. Add(' r: tpoint;');
  29613. Add('begin');
  29614. Add(' p:=typeinfo(tpoint);');
  29615. Add(' p:=typeinfo(r);');
  29616. Add(' p:=typeinfo(r.x);');
  29617. ConvertProgram;
  29618. CheckSource('TestRTTI_Record',
  29619. LinesToStr([ // statements
  29620. 'rtl.recNewT(this, "TPoint", function () {',
  29621. ' this.x = 0;',
  29622. ' this.y = 0;',
  29623. ' this.$eq = function (b) {',
  29624. ' return (this.x === b.x) && (this.y === b.y);',
  29625. ' };',
  29626. ' this.$assign = function (s) {',
  29627. ' this.x = s.x;',
  29628. ' this.y = s.y;',
  29629. ' return this;',
  29630. ' };',
  29631. ' var $r = $mod.$rtti.$Record("TPoint", {});',
  29632. ' $r.addField("x", rtl.longint);',
  29633. ' $r.addField("y", rtl.longint);',
  29634. '});',
  29635. 'this.p = null;',
  29636. 'this.r = this.TPoint.$new();',
  29637. '']),
  29638. LinesToStr([ // $mod.$main
  29639. '$mod.p = $mod.$rtti["TPoint"];',
  29640. '$mod.p = $mod.$rtti["TPoint"];',
  29641. '$mod.p = rtl.longint;',
  29642. '']));
  29643. end;
  29644. procedure TTestModule.TestRTTI_RecordAnonymousArray;
  29645. begin
  29646. WithTypeInfo:=true;
  29647. StartProgram(false);
  29648. Add('type');
  29649. Add(' TFloatRec = record');
  29650. Add(' c,d: array of char;');
  29651. // Add(' i: array of array of longint;');
  29652. Add(' end;');
  29653. Add('var p: pointer;');
  29654. Add(' r: tfloatrec;');
  29655. Add('begin');
  29656. Add(' p:=typeinfo(tfloatrec);');
  29657. Add(' p:=typeinfo(r);');
  29658. Add(' p:=typeinfo(r.d);');
  29659. ConvertProgram;
  29660. CheckSource('TestRTTI_Record',
  29661. LinesToStr([ // statements
  29662. 'rtl.recNewT(this, "TFloatRec", function () {',
  29663. ' this.$new = function () {',
  29664. ' var r = Object.create(this);',
  29665. ' r.c = [];',
  29666. ' r.d = [];',
  29667. ' return r;',
  29668. ' };',
  29669. ' this.$eq = function (b) {',
  29670. ' return (this.c === b.c) && (this.d === b.d);',
  29671. ' };',
  29672. ' this.$assign = function (s) {',
  29673. ' this.c = rtl.arrayRef(s.c);',
  29674. ' this.d = rtl.arrayRef(s.d);',
  29675. ' return this;',
  29676. ' };',
  29677. ' $mod.$rtti.$DynArray("TFloatRec.d$a", {',
  29678. ' eltype: rtl.char',
  29679. ' });',
  29680. ' var $r = $mod.$rtti.$Record("TFloatRec", {});',
  29681. ' $r.addField("c", $mod.$rtti["TFloatRec.d$a"]);',
  29682. ' $r.addField("d", $mod.$rtti["TFloatRec.d$a"]);',
  29683. '});',
  29684. 'this.p = null;',
  29685. 'this.r = this.TFloatRec.$new();',
  29686. '']),
  29687. LinesToStr([ // $mod.$main
  29688. '$mod.p = $mod.$rtti["TFloatRec"];',
  29689. '$mod.p = $mod.$rtti["TFloatRec"];',
  29690. '$mod.p = $mod.$rtti["TFloatRec.d$a"];',
  29691. '']));
  29692. end;
  29693. procedure TTestModule.TestRTTI_Record_ClassVarType;
  29694. begin
  29695. WithTypeInfo:=true;
  29696. StartProgram(false);
  29697. Add([
  29698. '{$modeswitch AdvancedRecords}',
  29699. 'type',
  29700. ' TPoint = record',
  29701. ' type TProc = procedure(w: word);',
  29702. ' class var p: TProc;',
  29703. ' end;',
  29704. 'begin',
  29705. '']);
  29706. ConvertProgram;
  29707. CheckSource('TestRTTI_Record_ClassVarType',
  29708. LinesToStr([ // statements
  29709. 'rtl.recNewT(this, "TPoint", function () {',
  29710. ' $mod.$rtti.$ProcVar("TPoint.TProc", {',
  29711. ' procsig: rtl.newTIProcSig([["w", rtl.word]])',
  29712. ' });',
  29713. ' this.p = null;',
  29714. ' this.$eq = function (b) {',
  29715. ' return true;',
  29716. ' };',
  29717. ' this.$assign = function (s) {',
  29718. ' return this;',
  29719. ' };',
  29720. ' var $r = $mod.$rtti.$Record("TPoint", {});',
  29721. ' $r.addField("p", $mod.$rtti["TPoint.TProc"]);',
  29722. '}, true);',
  29723. '']),
  29724. LinesToStr([ // $mod.$main
  29725. '']));
  29726. end;
  29727. procedure TTestModule.TestRTTI_LocalTypes;
  29728. begin
  29729. WithTypeInfo:=true;
  29730. StartProgram(false);
  29731. Add([
  29732. 'procedure DoIt;',
  29733. 'type',
  29734. ' integer = longint;',
  29735. ' TPoint = record',
  29736. ' x,y: integer;',
  29737. ' end;',
  29738. 'var p: TPoint;',
  29739. 'begin',
  29740. 'end;',
  29741. 'begin']);
  29742. ConvertProgram;
  29743. CheckSource('TestRTTI_LocalTypes',
  29744. LinesToStr([ // statements
  29745. 'var TPoint = rtl.recNewT(null, "", function () {',
  29746. ' this.x = 0;',
  29747. ' this.y = 0;',
  29748. ' this.$eq = function (b) {',
  29749. ' return (this.x === b.x) && (this.y === b.y);',
  29750. ' };',
  29751. ' this.$assign = function (s) {',
  29752. ' this.x = s.x;',
  29753. ' this.y = s.y;',
  29754. ' return this;',
  29755. ' };',
  29756. '});',
  29757. 'this.DoIt = function () {',
  29758. ' var p = TPoint.$new();',
  29759. '};',
  29760. '']),
  29761. LinesToStr([ // $mod.$main
  29762. '']));
  29763. end;
  29764. procedure TTestModule.TestRTTI_TypeInfo_BaseTypes;
  29765. begin
  29766. WithTypeInfo:=true;
  29767. StartProgram(false);
  29768. Add([
  29769. 'type',
  29770. ' TCaption = string;',
  29771. ' TYesNo = boolean;',
  29772. ' TLetter = char;',
  29773. ' TFloat = double;',
  29774. ' TPtr = pointer;',
  29775. ' TShortInt = shortint;',
  29776. ' TByte = byte;',
  29777. ' TSmallInt = smallint;',
  29778. ' TWord = word;',
  29779. ' TInt32 = longint;',
  29780. ' TDWord = longword;',
  29781. ' TValue = jsvalue;',
  29782. 'var p: TPtr;',
  29783. 'begin',
  29784. ' p:=typeinfo(string);',
  29785. ' p:=typeinfo(tcaption);',
  29786. ' p:=typeinfo(boolean);',
  29787. ' p:=typeinfo(tyesno);',
  29788. ' p:=typeinfo(char);',
  29789. ' p:=typeinfo(tletter);',
  29790. ' p:=typeinfo(double);',
  29791. ' p:=typeinfo(tfloat);',
  29792. ' p:=typeinfo(pointer);',
  29793. ' p:=typeinfo(tptr);',
  29794. ' p:=typeinfo(shortint);',
  29795. ' p:=typeinfo(tshortint);',
  29796. ' p:=typeinfo(byte);',
  29797. ' p:=typeinfo(tbyte);',
  29798. ' p:=typeinfo(smallint);',
  29799. ' p:=typeinfo(tsmallint);',
  29800. ' p:=typeinfo(word);',
  29801. ' p:=typeinfo(tword);',
  29802. ' p:=typeinfo(longword);',
  29803. ' p:=typeinfo(tdword);',
  29804. ' p:=typeinfo(jsvalue);',
  29805. ' p:=typeinfo(tvalue);',
  29806. '']);
  29807. ConvertProgram;
  29808. CheckSource('TestRTTI_TypeInfo_BaseTypes',
  29809. LinesToStr([ // statements
  29810. 'this.p = null;',
  29811. '']),
  29812. LinesToStr([ // $mod.$main
  29813. '$mod.p = rtl.string;',
  29814. '$mod.p = rtl.string;',
  29815. '$mod.p = rtl.boolean;',
  29816. '$mod.p = rtl.boolean;',
  29817. '$mod.p = rtl.char;',
  29818. '$mod.p = rtl.char;',
  29819. '$mod.p = rtl.double;',
  29820. '$mod.p = rtl.double;',
  29821. '$mod.p = rtl.pointer;',
  29822. '$mod.p = rtl.pointer;',
  29823. '$mod.p = rtl.shortint;',
  29824. '$mod.p = rtl.shortint;',
  29825. '$mod.p = rtl.byte;',
  29826. '$mod.p = rtl.byte;',
  29827. '$mod.p = rtl.smallint;',
  29828. '$mod.p = rtl.smallint;',
  29829. '$mod.p = rtl.word;',
  29830. '$mod.p = rtl.word;',
  29831. '$mod.p = rtl.longword;',
  29832. '$mod.p = rtl.longword;',
  29833. '$mod.p = rtl.jsvalue;',
  29834. '$mod.p = rtl.jsvalue;',
  29835. '']));
  29836. end;
  29837. procedure TTestModule.TestRTTI_TypeInfo_Type_BaseTypes;
  29838. begin
  29839. WithTypeInfo:=true;
  29840. StartProgram(false);
  29841. Add([
  29842. 'type',
  29843. ' TCaption = type string;',
  29844. ' TYesNo = type boolean;',
  29845. ' TLetter = type char;',
  29846. ' TFloat = type double;',
  29847. ' TPtr = type pointer;',
  29848. ' TShortInt = type shortint;',
  29849. ' TByte = type byte;',
  29850. ' TSmallInt = type smallint;',
  29851. ' TWord = type word;',
  29852. ' TInt32 = type longint;',
  29853. ' TDWord = type longword;',
  29854. ' TValue = type jsvalue;',
  29855. ' TAliasValue = type TValue;',
  29856. 'var',
  29857. ' p: TPtr;',
  29858. ' a: TAliasValue;',
  29859. 'begin',
  29860. ' p:=typeinfo(tcaption);',
  29861. ' p:=typeinfo(tyesno);',
  29862. ' p:=typeinfo(tletter);',
  29863. ' p:=typeinfo(tfloat);',
  29864. ' p:=typeinfo(tptr);',
  29865. ' p:=typeinfo(tshortint);',
  29866. ' p:=typeinfo(tbyte);',
  29867. ' p:=typeinfo(tsmallint);',
  29868. ' p:=typeinfo(tword);',
  29869. ' p:=typeinfo(tdword);',
  29870. ' p:=typeinfo(tvalue);',
  29871. ' p:=typeinfo(taliasvalue);',
  29872. ' p:=typeinfo(a);',
  29873. '']);
  29874. ConvertProgram;
  29875. CheckSource('TestRTTI_TypeInfo_Type_BaseTypes',
  29876. LinesToStr([ // statements
  29877. 'this.$rtti.$inherited("TCaption", rtl.string, {});',
  29878. 'this.$rtti.$inherited("TYesNo", rtl.boolean, {});',
  29879. 'this.$rtti.$inherited("TLetter", rtl.char, {});',
  29880. 'this.$rtti.$inherited("TFloat", rtl.double, {});',
  29881. 'this.$rtti.$inherited("TPtr", rtl.pointer, {});',
  29882. 'this.$rtti.$inherited("TShortInt", rtl.shortint, {});',
  29883. 'this.$rtti.$inherited("TByte", rtl.byte, {});',
  29884. 'this.$rtti.$inherited("TSmallInt", rtl.smallint, {});',
  29885. 'this.$rtti.$inherited("TWord", rtl.word, {});',
  29886. 'this.$rtti.$inherited("TInt32", rtl.longint, {});',
  29887. 'this.$rtti.$inherited("TDWord", rtl.longword, {});',
  29888. 'this.$rtti.$inherited("TValue", rtl.jsvalue, {});',
  29889. 'this.$rtti.$inherited("TAliasValue", this.$rtti["TValue"], {});',
  29890. 'this.p = null;',
  29891. 'this.a = undefined;',
  29892. '']),
  29893. LinesToStr([ // $mod.$main
  29894. '$mod.p = $mod.$rtti["TCaption"];',
  29895. '$mod.p = $mod.$rtti["TYesNo"];',
  29896. '$mod.p = $mod.$rtti["TLetter"];',
  29897. '$mod.p = $mod.$rtti["TFloat"];',
  29898. '$mod.p = $mod.$rtti["TPtr"];',
  29899. '$mod.p = $mod.$rtti["TShortInt"];',
  29900. '$mod.p = $mod.$rtti["TByte"];',
  29901. '$mod.p = $mod.$rtti["TSmallInt"];',
  29902. '$mod.p = $mod.$rtti["TWord"];',
  29903. '$mod.p = $mod.$rtti["TDWord"];',
  29904. '$mod.p = $mod.$rtti["TValue"];',
  29905. '$mod.p = $mod.$rtti["TAliasValue"];',
  29906. '$mod.p = $mod.$rtti["TAliasValue"];',
  29907. '']));
  29908. end;
  29909. procedure TTestModule.TestRTTI_TypeInfo_LocalFail;
  29910. begin
  29911. WithTypeInfo:=true;
  29912. StartProgram(false);
  29913. Add('procedure DoIt;');
  29914. Add('type');
  29915. Add(' integer = longint;');
  29916. Add(' TPoint = record');
  29917. Add(' x,y: integer;');
  29918. Add(' end;');
  29919. Add('var p: pointer;');
  29920. Add('begin');
  29921. Add(' p:=typeinfo(tpoint);');
  29922. Add('end;');
  29923. Add('begin');
  29924. SetExpectedPasResolverError(sSymbolCannotBePublished,nSymbolCannotBePublished);
  29925. ConvertProgram;
  29926. end;
  29927. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses1;
  29928. begin
  29929. WithTypeInfo:=true;
  29930. StartProgram(true,[supTypeInfo]);
  29931. Add([
  29932. '{$modeswitch externalclass}',
  29933. 'type',
  29934. ' TFlag = (up,down);',
  29935. ' TFlags = set of TFlag;',
  29936. 'var',
  29937. ' ti: TTypeInfo;',
  29938. ' tiInt: TTypeInfoInteger;',
  29939. ' tiEnum: TTypeInfoEnum;',
  29940. ' tiSet: TTypeInfoSet;',
  29941. 'begin',
  29942. ' ti:=typeinfo(string);',
  29943. ' ti:=typeinfo(boolean);',
  29944. ' ti:=typeinfo(char);',
  29945. ' ti:=typeinfo(double);',
  29946. ' tiInt:=typeinfo(shortint);',
  29947. ' tiInt:=typeinfo(byte);',
  29948. ' tiInt:=typeinfo(smallint);',
  29949. ' tiInt:=typeinfo(word);',
  29950. ' tiInt:=typeinfo(longint);',
  29951. ' tiInt:=typeinfo(longword);',
  29952. ' ti:=typeinfo(jsvalue);',
  29953. ' tiEnum:=typeinfo(tflag);',
  29954. ' tiSet:=typeinfo(tflags);']);
  29955. ConvertProgram;
  29956. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses1',
  29957. LinesToStr([ // statements
  29958. 'this.TFlag = {',
  29959. ' "0": "up",',
  29960. ' up: 0,',
  29961. ' "1": "down",',
  29962. ' down: 1',
  29963. '};',
  29964. 'this.$rtti.$Enum("TFlag", {',
  29965. ' minvalue: 0,',
  29966. ' maxvalue: 1,',
  29967. ' ordtype: 1,',
  29968. ' enumtype: this.TFlag',
  29969. '});',
  29970. 'this.$rtti.$Set("TFlags", {',
  29971. ' comptype: this.$rtti["TFlag"]',
  29972. '});',
  29973. 'this.ti = null;',
  29974. 'this.tiInt = null;',
  29975. 'this.tiEnum = null;',
  29976. 'this.tiSet = null;',
  29977. '']),
  29978. LinesToStr([ // $mod.$main
  29979. '$mod.ti = rtl.string;',
  29980. '$mod.ti = rtl.boolean;',
  29981. '$mod.ti = rtl.char;',
  29982. '$mod.ti = rtl.double;',
  29983. '$mod.tiInt = rtl.shortint;',
  29984. '$mod.tiInt = rtl.byte;',
  29985. '$mod.tiInt = rtl.smallint;',
  29986. '$mod.tiInt = rtl.word;',
  29987. '$mod.tiInt = rtl.longint;',
  29988. '$mod.tiInt = rtl.longword;',
  29989. '$mod.ti = rtl.jsvalue;',
  29990. '$mod.tiEnum = $mod.$rtti["TFlag"];',
  29991. '$mod.tiSet = $mod.$rtti["TFlags"];',
  29992. '']));
  29993. end;
  29994. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses2;
  29995. begin
  29996. WithTypeInfo:=true;
  29997. StartProgram(true,[supTypeInfo]);
  29998. Add('{$modeswitch externalclass}');
  29999. Add('type');
  30000. Add(' TStaticArr = array[boolean] of string;');
  30001. Add(' TDynArr = array of string;');
  30002. Add(' TProc = procedure;');
  30003. Add(' TMethod = procedure of object;');
  30004. Add('var');
  30005. Add(' StaticArray: TStaticArr;');
  30006. Add(' tiStaticArray: TTypeInfoStaticArray;');
  30007. Add(' DynArray: TDynArr;');
  30008. Add(' tiDynArray: TTypeInfoDynArray;');
  30009. Add(' ProcVar: TProc;');
  30010. Add(' tiProcVar: TTypeInfoProcVar;');
  30011. Add(' MethodVar: TMethod;');
  30012. Add(' tiMethodVar: TTypeInfoMethodVar;');
  30013. Add('begin');
  30014. Add(' tiStaticArray:=typeinfo(StaticArray);');
  30015. Add(' tiStaticArray:=typeinfo(TStaticArr);');
  30016. Add(' tiDynArray:=typeinfo(DynArray);');
  30017. Add(' tiDynArray:=typeinfo(TDynArr);');
  30018. Add(' tiProcVar:=typeinfo(ProcVar);');
  30019. Add(' tiProcVar:=typeinfo(TProc);');
  30020. Add(' tiMethodVar:=typeinfo(MethodVar);');
  30021. Add(' tiMethodVar:=typeinfo(TMethod);');
  30022. ConvertProgram;
  30023. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses2',
  30024. LinesToStr([ // statements
  30025. 'this.$rtti.$StaticArray("TStaticArr", {',
  30026. ' dims: [2],',
  30027. ' eltype: rtl.string',
  30028. '});',
  30029. 'this.$rtti.$DynArray("TDynArr", {',
  30030. ' eltype: rtl.string',
  30031. '});',
  30032. 'this.$rtti.$ProcVar("TProc", {',
  30033. ' procsig: rtl.newTIProcSig(null)',
  30034. '});',
  30035. 'this.$rtti.$MethodVar("TMethod", {',
  30036. ' procsig: rtl.newTIProcSig(null),',
  30037. ' methodkind: 0',
  30038. '});',
  30039. 'this.StaticArray = rtl.arraySetLength(null,"",2);',
  30040. 'this.tiStaticArray = null;',
  30041. 'this.DynArray = [];',
  30042. 'this.tiDynArray = null;',
  30043. 'this.ProcVar = null;',
  30044. 'this.tiProcVar = null;',
  30045. 'this.MethodVar = null;',
  30046. 'this.tiMethodVar = null;',
  30047. '']),
  30048. LinesToStr([ // $mod.$main
  30049. '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
  30050. '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
  30051. '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
  30052. '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
  30053. '$mod.tiProcVar = $mod.$rtti["TProc"];',
  30054. '$mod.tiProcVar = $mod.$rtti["TProc"];',
  30055. '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
  30056. '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
  30057. '']));
  30058. end;
  30059. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses3;
  30060. begin
  30061. WithTypeInfo:=true;
  30062. StartProgram(true,[supTypeInfo]);
  30063. Add('{$modeswitch externalclass}');
  30064. Add('type');
  30065. Add(' TRec = record end;');
  30066. // ToDo: ^TRec
  30067. Add(' TObject = class end;');
  30068. Add(' TClass = class of tobject;');
  30069. Add('var');
  30070. Add(' Rec: trec;');
  30071. Add(' tiRecord: ttypeinforecord;');
  30072. Add(' Obj: tobject;');
  30073. Add(' tiClass: ttypeinfoclass;');
  30074. Add(' aClass: tclass;');
  30075. Add(' tiClassRef: ttypeinfoclassref;');
  30076. // ToDo: ^TRec
  30077. Add(' tiPointer: ttypeinfopointer;');
  30078. Add('begin');
  30079. Add(' tirecord:=typeinfo(trec);');
  30080. Add(' tirecord:=typeinfo(trec);');
  30081. Add(' ticlass:=typeinfo(obj);');
  30082. Add(' ticlass:=typeinfo(tobject);');
  30083. Add(' ticlass:=typeinfo(aclass);');
  30084. Add(' ticlassref:=typeinfo(tclass);');
  30085. ConvertProgram;
  30086. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses3',
  30087. LinesToStr([ // statements
  30088. 'rtl.recNewT(this, "TRec", function () {',
  30089. ' this.$eq = function (b) {',
  30090. ' return true;',
  30091. ' };',
  30092. ' this.$assign = function (s) {',
  30093. ' return this;',
  30094. ' };',
  30095. ' $mod.$rtti.$Record("TRec", {});',
  30096. '});',
  30097. 'rtl.createClass(this, "TObject", null, function () {',
  30098. ' this.$init = function () {',
  30099. ' };',
  30100. ' this.$final = function () {',
  30101. ' };',
  30102. '});',
  30103. 'this.$rtti.$ClassRef("TClass", {',
  30104. ' instancetype: this.$rtti["TObject"]',
  30105. '});',
  30106. 'this.Rec = this.TRec.$new();',
  30107. 'this.tiRecord = null;',
  30108. 'this.Obj = null;',
  30109. 'this.tiClass = null;',
  30110. 'this.aClass = null;',
  30111. 'this.tiClassRef = null;',
  30112. 'this.tiPointer = null;',
  30113. '']),
  30114. LinesToStr([ // $mod.$main
  30115. '$mod.tiRecord = $mod.$rtti["TRec"];',
  30116. '$mod.tiRecord = $mod.$rtti["TRec"];',
  30117. '$mod.tiClass = $mod.Obj.$rtti;',
  30118. '$mod.tiClass = $mod.$rtti["TObject"];',
  30119. '$mod.tiClass = $mod.aClass.$rtti;',
  30120. '$mod.tiClassRef = $mod.$rtti["TClass"];',
  30121. '']));
  30122. end;
  30123. procedure TTestModule.TestRTTI_TypeInfo_FunctionClassType;
  30124. begin
  30125. WithTypeInfo:=true;
  30126. StartProgram(true,[supTypeInfo]);
  30127. Add([
  30128. '{$modeswitch externalclass}',
  30129. 'type',
  30130. ' TClass = class of tobject;',
  30131. ' TObject = class',
  30132. ' function MyClass: TClass;',
  30133. ' class function ClassType: TClass;',
  30134. ' end;',
  30135. 'function TObject.MyClass: TClass;',
  30136. 'var t: TTypeInfoClass;',
  30137. 'begin',
  30138. ' t:=TypeInfo(Self);',
  30139. ' t:=TypeInfo(Result);',
  30140. ' t:=TypeInfo(TObject);',
  30141. 'end;',
  30142. 'class function TObject.ClassType: TClass;',
  30143. 'var t: TTypeInfoClass;',
  30144. 'begin',
  30145. ' t:=TypeInfo(Self);',
  30146. ' t:=TypeInfo(Result);',
  30147. 'end;',
  30148. 'var',
  30149. ' Obj: TObject;',
  30150. ' t: TTypeInfoClass;',
  30151. 'begin',
  30152. ' t:=TypeInfo(TObject.ClassType);',
  30153. ' t:=TypeInfo(Obj.ClassType);',
  30154. ' t:=TypeInfo(Obj.MyClass);',
  30155. '']);
  30156. ConvertProgram;
  30157. CheckSource('TestRTTI_TypeInfo_FunctionClassType',
  30158. LinesToStr([ // statements
  30159. 'this.$rtti.$Class("TObject");',
  30160. 'this.$rtti.$ClassRef("TClass", {',
  30161. ' instancetype: this.$rtti["TObject"]',
  30162. '});',
  30163. 'rtl.createClass(this, "TObject", null, function () {',
  30164. ' this.$init = function () {',
  30165. ' };',
  30166. ' this.$final = function () {',
  30167. ' };',
  30168. ' this.MyClass = function () {',
  30169. ' var Result = null;',
  30170. ' var t = null;',
  30171. ' t = this.$rtti;',
  30172. ' t = Result.$rtti;',
  30173. ' t = $mod.$rtti["TObject"];',
  30174. ' return Result;',
  30175. ' };',
  30176. ' this.ClassType = function () {',
  30177. ' var Result = null;',
  30178. ' var t = null;',
  30179. ' t = this.$rtti;',
  30180. ' t = Result.$rtti;',
  30181. ' return Result;',
  30182. ' };',
  30183. '});',
  30184. 'this.Obj = null;',
  30185. 'this.t = null;',
  30186. '']),
  30187. LinesToStr([ // $mod.$main
  30188. '$mod.t = $mod.TObject.ClassType().$rtti;',
  30189. '$mod.t = $mod.Obj.$class.ClassType().$rtti;',
  30190. '$mod.t = $mod.Obj.MyClass().$rtti;',
  30191. '']));
  30192. end;
  30193. procedure TTestModule.TestRTTI_TypeInfo_MixedUnits_PointerAndClass;
  30194. begin
  30195. WithTypeInfo:=true;
  30196. AddModuleWithIntfImplSrc('typinfo.pas',
  30197. LinesToStr([
  30198. '{$modeswitch externalclass}',
  30199. 'type',
  30200. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  30201. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo) end;',
  30202. '']),
  30203. '');
  30204. AddModuleWithIntfImplSrc('unit2.pas',
  30205. LinesToStr([
  30206. 'uses typinfo;',
  30207. 'type PTypeInfo = TTypeInfo;', // delphi compatibility code
  30208. 'procedure DoPtr(p: PTypeInfo);',
  30209. 'procedure DoInfo(t: TTypeInfo);',
  30210. 'procedure DoInt(t: TTypeInfoInteger);',
  30211. '']),
  30212. LinesToStr([
  30213. 'procedure DoPtr(p: PTypeInfo);',
  30214. 'begin end;',
  30215. 'procedure DoInfo(t: TTypeInfo);',
  30216. 'begin end;',
  30217. 'procedure DoInt(t: TTypeInfoInteger);',
  30218. 'begin end;',
  30219. '']));
  30220. StartUnit(true);
  30221. Add([
  30222. 'interface',
  30223. 'uses unit2;', // does not use unit typinfo
  30224. 'implementation',
  30225. 'var',
  30226. ' i: byte;',
  30227. ' p: pointer;',
  30228. ' t: PTypeInfo;',
  30229. 'initialization',
  30230. ' p:=typeinfo(i);',
  30231. ' t:=typeinfo(i);',
  30232. ' if p=t then ;',
  30233. ' if p=typeinfo(i) then ;',
  30234. ' if typeinfo(i)=p then ;',
  30235. ' if t=typeinfo(i) then ;',
  30236. ' if typeinfo(i)=t then ;',
  30237. ' DoPtr(p);',
  30238. ' DoPtr(t);',
  30239. ' DoPtr(typeinfo(i));',
  30240. ' DoInfo(p);',
  30241. ' DoInfo(t);',
  30242. ' DoInfo(typeinfo(i));',
  30243. ' DoInt(typeinfo(i));',
  30244. '']);
  30245. ConvertUnit;
  30246. CheckSource('TestRTTI_TypeInfo_MixedUnits_PointerAndClass',
  30247. LinesToStr([ // statements
  30248. 'var $impl = $mod.$impl;',
  30249. '']),
  30250. LinesToStr([ // this.$init
  30251. '$impl.p = rtl.byte;',
  30252. '$impl.t = rtl.byte;',
  30253. 'if ($impl.p === $impl.t) ;',
  30254. 'if ($impl.p === rtl.byte) ;',
  30255. 'if (rtl.byte === $impl.p) ;',
  30256. 'if ($impl.t === rtl.byte) ;',
  30257. 'if (rtl.byte === $impl.t) ;',
  30258. 'pas.unit2.DoPtr($impl.p);',
  30259. 'pas.unit2.DoPtr($impl.t);',
  30260. 'pas.unit2.DoPtr(rtl.byte);',
  30261. 'pas.unit2.DoInfo($impl.p);',
  30262. 'pas.unit2.DoInfo($impl.t);',
  30263. 'pas.unit2.DoInfo(rtl.byte);',
  30264. 'pas.unit2.DoInt(rtl.byte);',
  30265. '']),
  30266. LinesToStr([ // implementation
  30267. '$impl.i = 0;',
  30268. '$impl.p = null;',
  30269. '$impl.t = null;',
  30270. '']) );
  30271. end;
  30272. procedure TTestModule.TestRTTI_Interface_Corba;
  30273. begin
  30274. WithTypeInfo:=true;
  30275. StartProgram(true,[supTypeInfo]);
  30276. Add([
  30277. '{$interfaces corba}',
  30278. '{$modeswitch externalclass}',
  30279. 'type',
  30280. ' IUnknown = interface',
  30281. ' end;',
  30282. ' IBird = interface',
  30283. ' function GetItem: longint;',
  30284. ' procedure SetItem(Value: longint);',
  30285. ' property Item: longint read GetItem write SetItem;',
  30286. ' end;',
  30287. 'procedure DoIt(t: TTypeInfoInterface); begin end;',
  30288. 'var',
  30289. ' i: IBird;',
  30290. ' t: TTypeInfoInterface;',
  30291. 'begin',
  30292. ' t:=TypeInfo(IBird);',
  30293. ' t:=TypeInfo(i);',
  30294. ' DoIt(t);',
  30295. ' DoIt(TypeInfo(IBird));',
  30296. '']);
  30297. ConvertProgram;
  30298. CheckSource('TestRTTI_Interface_Corba',
  30299. LinesToStr([ // statements
  30300. 'rtl.createInterface(',
  30301. ' this,',
  30302. ' "IUnknown",',
  30303. ' "{B92D5841-758A-322B-B800-000000000000}",',
  30304. ' [],',
  30305. ' null,',
  30306. ' function () {',
  30307. ' }',
  30308. ');',
  30309. 'rtl.createInterface(',
  30310. ' this,',
  30311. ' "IBird",',
  30312. ' "{D32D5841-6264-3AE3-A2C9-B91CE922C9B9}",',
  30313. ' ["GetItem", "SetItem"],',
  30314. ' null,',
  30315. ' function () {',
  30316. ' var $r = this.$rtti;',
  30317. ' $r.addMethod("GetItem", 1, null, rtl.longint);',
  30318. ' $r.addMethod("SetItem", 0, [["Value", rtl.longint]]);',
  30319. ' $r.addProperty("Item", 3, rtl.longint, "GetItem", "SetItem");',
  30320. ' }',
  30321. ');',
  30322. 'this.DoIt = function (t) {',
  30323. '}; ',
  30324. 'this.i = null;',
  30325. 'this.t = null;',
  30326. '']),
  30327. LinesToStr([ // $mod.$main
  30328. '$mod.t = $mod.$rtti["IBird"];',
  30329. '$mod.t = $mod.i.$rtti;',
  30330. '$mod.DoIt($mod.t);',
  30331. '$mod.DoIt($mod.$rtti["IBird"]);',
  30332. '']));
  30333. end;
  30334. procedure TTestModule.TestRTTI_Interface_COM;
  30335. begin
  30336. WithTypeInfo:=true;
  30337. StartProgram(true,[supTypeInfo]);
  30338. Add([
  30339. '{$interfaces com}',
  30340. '{$modeswitch externalclass}',
  30341. 'type',
  30342. ' TGuid = record end;',
  30343. ' integer = longint;',
  30344. ' IUnknown = interface',
  30345. ' function QueryInterface(const iid: TGuid; out obj): Integer;',
  30346. ' function _AddRef: Integer;',
  30347. ' function _Release: Integer;',
  30348. ' end;',
  30349. ' IBird = interface',
  30350. ' function GetItem: longint;',
  30351. ' procedure SetItem(Value: longint);',
  30352. ' property Item: longint read GetItem write SetItem;',
  30353. ' end;',
  30354. 'var',
  30355. ' i: IBird;',
  30356. ' t: TTypeInfoInterface;',
  30357. 'begin',
  30358. ' t:=TypeInfo(IBird);',
  30359. ' t:=TypeInfo(i);',
  30360. '']);
  30361. ConvertProgram;
  30362. CheckSource('TestRTTI_Interface_COM',
  30363. LinesToStr([ // statements
  30364. 'rtl.recNewT(this, "TGuid", function () {',
  30365. ' this.$eq = function (b) {',
  30366. ' return true;',
  30367. ' };',
  30368. ' this.$assign = function (s) {',
  30369. ' return this;',
  30370. ' };',
  30371. ' $mod.$rtti.$Record("TGuid", {});',
  30372. '});',
  30373. 'rtl.createInterface(',
  30374. ' this,',
  30375. ' "IUnknown",',
  30376. ' "{D7ADB00D-1A9B-3EDC-B123-730E661DDFA9}",',
  30377. ' ["QueryInterface", "_AddRef", "_Release"],',
  30378. ' null,',
  30379. ' function () {',
  30380. ' this.$kind = "com";',
  30381. ' var $r = this.$rtti;',
  30382. ' $r.addMethod("QueryInterface", 1, [["iid", $mod.$rtti["TGuid"], 2], ["obj", null, 4]], rtl.longint);',
  30383. ' $r.addMethod("_AddRef", 1, null, rtl.longint);',
  30384. ' $r.addMethod("_Release", 1, null, rtl.longint);',
  30385. ' }',
  30386. ');',
  30387. 'rtl.createInterface(',
  30388. ' this,',
  30389. ' "IBird",',
  30390. ' "{9CC77572-0E45-3594-9A88-9E8D865C9E0A}",',
  30391. ' ["GetItem", "SetItem"],',
  30392. ' this.IUnknown,',
  30393. ' function () {',
  30394. ' var $r = this.$rtti;',
  30395. ' $r.addMethod("GetItem", 1, null, rtl.longint);',
  30396. ' $r.addMethod("SetItem", 0, [["Value", rtl.longint]]);',
  30397. ' $r.addProperty("Item", 3, rtl.longint, "GetItem", "SetItem");',
  30398. ' }',
  30399. ');',
  30400. 'this.i = null;',
  30401. 'this.t = null;',
  30402. '']),
  30403. LinesToStr([ // $mod.$main
  30404. '$mod.t = $mod.$rtti["IBird"];',
  30405. '$mod.t = $mod.i.$rtti;',
  30406. '']));
  30407. end;
  30408. procedure TTestModule.TestRTTI_ClassHelper;
  30409. begin
  30410. WithTypeInfo:=true;
  30411. StartProgram(true,[supTypeInfo]);
  30412. Add([
  30413. '{$interfaces com}',
  30414. '{$modeswitch externalclass}',
  30415. 'type',
  30416. ' TObject = class',
  30417. ' end;',
  30418. ' THelper = class helper for TObject',
  30419. ' published',
  30420. ' function GetItem: longint;',
  30421. ' property Item: longint read GetItem;',
  30422. ' end;',
  30423. 'function THelper.GetItem: longint;',
  30424. 'begin',
  30425. 'end;',
  30426. 'var',
  30427. ' t: TTypeInfoHelper;',
  30428. 'begin',
  30429. ' t:=TypeInfo(THelper);',
  30430. '']);
  30431. ConvertProgram;
  30432. CheckSource('TestRTTI_ClassHelper',
  30433. LinesToStr([ // statements
  30434. 'rtl.createClass(this, "TObject", null, function () {',
  30435. ' this.$init = function () {',
  30436. ' };',
  30437. ' this.$final = function () {',
  30438. ' };',
  30439. '});',
  30440. 'rtl.createHelper(this, "THelper", null, function () {',
  30441. ' this.GetItem = function () {',
  30442. ' var Result = 0;',
  30443. ' return Result;',
  30444. ' };',
  30445. ' var $r = this.$rtti;',
  30446. ' $r.addMethod("GetItem", 1, null, rtl.longint);',
  30447. ' $r.addProperty("Item", 1, rtl.longint, "GetItem", "");',
  30448. '});',
  30449. 'this.t = null;',
  30450. '']),
  30451. LinesToStr([ // $mod.$main
  30452. '$mod.t = $mod.$rtti["THelper"];',
  30453. '']));
  30454. end;
  30455. procedure TTestModule.TestRTTI_ExternalClass;
  30456. begin
  30457. WithTypeInfo:=true;
  30458. StartProgram(true,[supTypeInfo]);
  30459. Add([
  30460. '{$modeswitch externalclass}',
  30461. 'type',
  30462. ' TJSObject = class external name ''Object''',
  30463. ' end;',
  30464. ' TJSArray = class external name ''Array'' (TJSObject)',
  30465. ' end;',
  30466. 'var',
  30467. ' p: Pointer;',
  30468. ' tc: TTypeInfoExtClass;',
  30469. 'begin',
  30470. ' p:=typeinfo(TJSArray);']);
  30471. ConvertProgram;
  30472. CheckSource('TestRTTI_ExternalClass',
  30473. LinesToStr([ // statements
  30474. 'this.$rtti.$ExtClass("TJSObject", {',
  30475. ' jsclass: "Object"',
  30476. '});',
  30477. 'this.$rtti.$ExtClass("TJSArray", {',
  30478. ' ancestor: this.$rtti["TJSObject"],',
  30479. ' jsclass: "Array"',
  30480. '});',
  30481. 'this.p = null;',
  30482. 'this.tc = null;',
  30483. '']),
  30484. LinesToStr([ // $mod.$main
  30485. '$mod.p = $mod.$rtti["TJSArray"];',
  30486. '']));
  30487. end;
  30488. procedure TTestModule.TestRTTI_Unit;
  30489. begin
  30490. WithTypeInfo:=true;
  30491. AddModuleWithIntfImplSrc('unit2.pas',
  30492. LinesToStr([
  30493. '{$mode delphi}',
  30494. 'type',
  30495. ' TWordArray = array of word;',
  30496. ' TArray<T> = array of T;',
  30497. '']),
  30498. '');
  30499. StartUnit(true,[supTypeInfo,supTInterfacedObject]);
  30500. Add([
  30501. '{$mode delphi}',
  30502. 'interface',
  30503. 'uses unit2;',
  30504. 'type',
  30505. ' IBird = interface',
  30506. ' function Swoop: TWordArray;',
  30507. ' function Glide: TArray<word>;',
  30508. ' end;',
  30509. 'procedure Fly;',
  30510. 'implementation',
  30511. 'procedure Fly;',
  30512. 'var',
  30513. ' ta: tTypeInfoDynArray;',
  30514. ' ti: tTypeInfoInterface;',
  30515. 'begin',
  30516. ' ta:=typeinfo(TWordArray);',
  30517. ' ta:=typeinfo(TArray<word>);',
  30518. ' ti:=typeinfo(IBird);',
  30519. 'end;',
  30520. '']);
  30521. ConvertUnit;
  30522. CheckSource('TestRTTI_ExternalClass',
  30523. LinesToStr([ // statements
  30524. 'rtl.createInterface(',
  30525. ' this,',
  30526. ' "IBird",',
  30527. ' "{3B98AAAC-6116-3E17-AA85-F16786D85B09}",',
  30528. ' ["Swoop", "Glide"],',
  30529. ' pas.system.IUnknown,',
  30530. ' function () {',
  30531. ' var $r = this.$rtti;',
  30532. ' $r.addMethod("Swoop", 1, null, pas.unit2.$rtti["TWordArray"]);',
  30533. ' $r.addMethod("Glide", 1, null, pas.unit2.$rtti["TArray<System.Word>"]);',
  30534. ' }',
  30535. ');',
  30536. 'this.Fly = function () {',
  30537. ' var ta = null;',
  30538. ' var ti = null;',
  30539. ' ta = pas.unit2.$rtti["TWordArray"];',
  30540. ' ta = pas.unit2.$rtti["TArray<System.Word>"];',
  30541. ' ti = $mod.$rtti["IBird"];',
  30542. '};',
  30543. '']),
  30544. LinesToStr([ // $mod.$main
  30545. '']));
  30546. end;
  30547. procedure TTestModule.TestResourcestringProgram;
  30548. begin
  30549. AddModuleWithIntfImplSrc('unit2.pas',
  30550. LinesToStr([
  30551. 'resourcestring Title = ''Nice'';',
  30552. '']),
  30553. '');
  30554. StartProgram(true);
  30555. Add([
  30556. 'uses unit2;',
  30557. 'const Bar = ''bar'';',
  30558. 'resourcestring',
  30559. ' Red = ''red'';',
  30560. ' Foobar = ''fOo''+bar;',
  30561. 'var s: string;',
  30562. ' c: char;',
  30563. 'begin',
  30564. ' s:=red;',
  30565. ' s:=test1.red;',
  30566. ' s:=Title;',
  30567. ' c:=red[1];',
  30568. ' c:=test1.red[2];',
  30569. ' if red=foobar then ;',
  30570. ' if red[3]=red[4] then ;']);
  30571. ConvertProgram;
  30572. CheckSource('TestResourcestringProgram',
  30573. LinesToStr([ // statements
  30574. 'this.Bar = "bar";',
  30575. 'this.s = "";',
  30576. 'this.c = "";',
  30577. '$mod.$resourcestrings = {',
  30578. ' Red: {',
  30579. ' org: "red"',
  30580. ' },',
  30581. ' Foobar: {',
  30582. ' org: "fOobar"',
  30583. ' }',
  30584. '};',
  30585. '']),
  30586. LinesToStr([ // $mod.$main
  30587. '$mod.s = rtl.getResStr($mod, "Red");',
  30588. '$mod.s = rtl.getResStr($mod, "Red");',
  30589. '$mod.s = rtl.getResStr(pas.unit2, "Title");',
  30590. '$mod.c = rtl.getResStr($mod, "Red").charAt(0);',
  30591. '$mod.c = rtl.getResStr($mod, "Red").charAt(1);',
  30592. 'if (rtl.getResStr($mod, "Red") === rtl.getResStr($mod, "Foobar")) ;',
  30593. 'if (rtl.getResStr($mod, "Red").charAt(2) === rtl.getResStr($mod, "Red").charAt(3)) ;',
  30594. '']));
  30595. end;
  30596. procedure TTestModule.TestResourcestringUnit;
  30597. begin
  30598. AddModuleWithIntfImplSrc('unit2.pas',
  30599. LinesToStr([
  30600. 'resourcestring Title = ''Nice'';',
  30601. '']),
  30602. '');
  30603. StartUnit(true);
  30604. Add([
  30605. 'interface',
  30606. 'uses unit2;',
  30607. 'const Red = ''rEd'';',
  30608. 'resourcestring',
  30609. ' Blue = ''blue'';',
  30610. ' NotRed = ''not''+Red;',
  30611. 'var s: string;',
  30612. 'implementation',
  30613. 'resourcestring',
  30614. ' ImplGreen = ''green'';',
  30615. 'initialization',
  30616. ' s:=blue+ImplGreen;',
  30617. ' s:=test1.blue+test1.implgreen;',
  30618. ' s:=blue[1]+implgreen[2];',
  30619. ' s:=Title;',
  30620. '']);
  30621. ConvertUnit;
  30622. CheckSource('TestResourcestringUnit',
  30623. LinesToStr([ // statements
  30624. 'this.Red = "rEd";',
  30625. 'this.s = "";',
  30626. '$mod.$resourcestrings = {',
  30627. ' Blue: {',
  30628. ' org: "blue"',
  30629. ' },',
  30630. ' NotRed: {',
  30631. ' org: "notrEd"',
  30632. ' },',
  30633. ' ImplGreen: {',
  30634. ' org: "green"',
  30635. ' }',
  30636. '};',
  30637. '']),
  30638. LinesToStr([ // $mod.$main
  30639. '$mod.s = rtl.getResStr($mod, "Blue") + rtl.getResStr($mod, "ImplGreen");',
  30640. '$mod.s = rtl.getResStr($mod, "Blue") + rtl.getResStr($mod, "ImplGreen");',
  30641. '$mod.s = rtl.getResStr($mod, "Blue").charAt(0) + rtl.getResStr($mod, "ImplGreen").charAt(1);',
  30642. '$mod.s = rtl.getResStr(pas.unit2, "Title");',
  30643. '']));
  30644. end;
  30645. procedure TTestModule.TestResourcestringImplementation;
  30646. begin
  30647. StartUnit(false);
  30648. Add([
  30649. 'interface',
  30650. 'implementation',
  30651. 'resourcestring',
  30652. ' ImplRed = ''red'';']);
  30653. ConvertUnit;
  30654. CheckSource('TestResourcestringImplementation',
  30655. LinesToStr([ // intf statements
  30656. 'var $impl = $mod.$impl;']),
  30657. LinesToStr([ // $mod.$init
  30658. '']),
  30659. LinesToStr([ // impl statements
  30660. '$mod.$resourcestrings = {',
  30661. ' ImplRed: {',
  30662. ' org: "red"',
  30663. ' }',
  30664. '};',
  30665. '']));
  30666. end;
  30667. procedure TTestModule.TestAttributes_Members;
  30668. begin
  30669. WithTypeInfo:=true;
  30670. StartProgram(false);
  30671. Add([
  30672. '{$modeswitch PrefixedAttributes}',
  30673. 'type',
  30674. ' TObject = class',
  30675. ' constructor Create;',
  30676. ' end;',
  30677. ' TCustomAttribute = class',
  30678. ' constructor Create(Id: word);',
  30679. ' end;',
  30680. ' [Missing]',
  30681. ' TBird = class',
  30682. ' published',
  30683. ' [Tcustom]',
  30684. ' FField: word;',
  30685. ' [tcustom(14)]',
  30686. ' property Size: word read FField;',
  30687. ' [Tcustom(15)]',
  30688. ' procedure Fly; virtual; abstract;',
  30689. ' end;',
  30690. ' TRec = record',
  30691. ' [Tcustom,tcustom(14)]',
  30692. ' Size: word;',
  30693. ' end;',
  30694. 'constructor TObject.Create; begin end;',
  30695. 'constructor TCustomAttribute.Create(Id: word); begin end;',
  30696. 'begin',
  30697. '']);
  30698. ConvertProgram;
  30699. CheckSource('TestAttributes_Members',
  30700. LinesToStr([ // statements
  30701. 'rtl.createClass(this, "TObject", null, function () {',
  30702. ' this.$init = function () {',
  30703. ' };',
  30704. ' this.$final = function () {',
  30705. ' };',
  30706. ' this.Create = function () {',
  30707. ' return this;',
  30708. ' };',
  30709. '});',
  30710. 'rtl.createClass(this, "TCustomAttribute", this.TObject, function () {',
  30711. ' this.Create$1 = function (Id) {',
  30712. ' return this;',
  30713. ' };',
  30714. '});',
  30715. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  30716. ' this.$init = function () {',
  30717. ' $mod.TObject.$init.call(this);',
  30718. ' this.FField = 0;',
  30719. ' };',
  30720. ' var $r = this.$rtti;',
  30721. ' $r.addField("FField", rtl.word, {',
  30722. ' attr: [$mod.TCustomAttribute, "Create"]',
  30723. ' });',
  30724. ' $r.addProperty(',
  30725. ' "Size",',
  30726. ' 0,',
  30727. ' rtl.word,',
  30728. ' "FField",',
  30729. ' "",',
  30730. ' {',
  30731. ' attr: [$mod.TCustomAttribute, "Create$1", [14]]',
  30732. ' }',
  30733. ' );',
  30734. ' $r.addMethod("Fly", 0, null, null, {',
  30735. ' attr: [$mod.TCustomAttribute, "Create$1", [15]]',
  30736. ' });',
  30737. '});',
  30738. 'rtl.recNewT(this, "TRec", function () {',
  30739. ' this.Size = 0;',
  30740. ' this.$eq = function (b) {',
  30741. ' return this.Size === b.Size;',
  30742. ' };',
  30743. ' this.$assign = function (s) {',
  30744. ' this.Size = s.Size;',
  30745. ' return this;',
  30746. ' };',
  30747. ' var $r = $mod.$rtti.$Record("TRec", {});',
  30748. ' $r.addField("Size", rtl.word, {',
  30749. ' attr: [',
  30750. ' $mod.TCustomAttribute,',
  30751. ' "Create",',
  30752. ' $mod.TCustomAttribute,',
  30753. ' "Create$1",',
  30754. ' [14]',
  30755. ' ]',
  30756. ' });',
  30757. '});',
  30758. '']),
  30759. LinesToStr([ // $mod.$main
  30760. '']));
  30761. end;
  30762. procedure TTestModule.TestAttributes_Types;
  30763. begin
  30764. WithTypeInfo:=true;
  30765. StartProgram(false);
  30766. Add([
  30767. '{$modeswitch PrefixedAttributes}',
  30768. 'type',
  30769. ' TObject = class',
  30770. ' constructor Create(Id: word);',
  30771. ' end;',
  30772. ' TCustomAttribute = class',
  30773. ' end;',
  30774. ' [TCustom(1)]',
  30775. ' TMyClass = class',
  30776. ' end;',
  30777. ' [TCustom(2)]',
  30778. ' TRec = record',
  30779. ' end;',
  30780. ' [TCustom(3)]',
  30781. ' TInt = type word;',
  30782. 'constructor TObject.Create(Id: word);',
  30783. 'begin',
  30784. 'end;',
  30785. 'var p: pointer;',
  30786. 'begin',
  30787. ' p:=typeinfo(TMyClass);',
  30788. ' p:=typeinfo(TRec);',
  30789. ' p:=typeinfo(TInt);',
  30790. '']);
  30791. ConvertProgram;
  30792. CheckSource('TestAttributes_Types',
  30793. LinesToStr([ // statements
  30794. 'rtl.createClass(this, "TObject", null, function () {',
  30795. ' this.$init = function () {',
  30796. ' };',
  30797. ' this.$final = function () {',
  30798. ' };',
  30799. ' this.Create = function (Id) {',
  30800. ' return this;',
  30801. ' };',
  30802. '});',
  30803. 'rtl.createClass(this, "TCustomAttribute", this.TObject, function () {',
  30804. '});',
  30805. 'rtl.createClass(this, "TMyClass", this.TObject, function () {',
  30806. ' var $r = this.$rtti;',
  30807. ' $r.attr = [$mod.TCustomAttribute, "Create", [1]];',
  30808. '});',
  30809. 'rtl.recNewT(this, "TRec", function () {',
  30810. ' this.$eq = function (b) {',
  30811. ' return true;',
  30812. ' };',
  30813. ' this.$assign = function (s) {',
  30814. ' return this;',
  30815. ' };',
  30816. ' $mod.$rtti.$Record("TRec", {',
  30817. ' attr: [$mod.TCustomAttribute, "Create", [2]]',
  30818. ' });',
  30819. '});',
  30820. 'this.$rtti.$inherited("TInt", rtl.word, {',
  30821. ' attr: [this.TCustomAttribute, "Create", [3]]',
  30822. '});',
  30823. 'this.p = null;',
  30824. '']),
  30825. LinesToStr([ // $mod.$main
  30826. '$mod.p = $mod.$rtti["TMyClass"];',
  30827. '$mod.p = $mod.$rtti["TRec"];',
  30828. '$mod.p = $mod.$rtti["TInt"];',
  30829. '']));
  30830. end;
  30831. procedure TTestModule.TestAttributes_HelperConstructor_Fail;
  30832. begin
  30833. WithTypeInfo:=true;
  30834. StartProgram(false);
  30835. Add([
  30836. '{$modeswitch PrefixedAttributes}',
  30837. 'type',
  30838. ' TObject = class',
  30839. ' constructor Create;',
  30840. ' end;',
  30841. ' TCustomAttribute = class',
  30842. ' end;',
  30843. ' THelper = class helper for TCustomAttribute',
  30844. ' constructor Create(Id: word);',
  30845. ' end;',
  30846. ' [TCustom(3)]',
  30847. ' TMyInt = word;',
  30848. 'constructor TObject.Create; begin end;',
  30849. 'constructor THelper.Create(Id: word); begin end;',
  30850. 'begin',
  30851. ' if typeinfo(TMyInt)=nil then ;']);
  30852. ConvertProgram;
  30853. end;
  30854. procedure TTestModule.TestAssert;
  30855. begin
  30856. StartProgram(false);
  30857. Add([
  30858. 'procedure DoIt;',
  30859. 'var',
  30860. ' b: boolean;',
  30861. ' s: string;',
  30862. 'begin',
  30863. ' {$Assertions on}',
  30864. ' Assert(b);',
  30865. 'end;',
  30866. 'begin',
  30867. ' DoIt;',
  30868. '']);
  30869. ConvertProgram;
  30870. CheckSource('TestAssert',
  30871. LinesToStr([ // statements
  30872. 'this.DoIt = function () {',
  30873. ' var b = false;',
  30874. ' var s = "";',
  30875. ' if (!b) throw "assert failed";',
  30876. '};',
  30877. '']),
  30878. LinesToStr([ // $mod.$main
  30879. '$mod.DoIt();',
  30880. '']));
  30881. end;
  30882. procedure TTestModule.TestAssert_SysUtils;
  30883. begin
  30884. AddModuleWithIntfImplSrc('SysUtils.pas',
  30885. LinesToStr([
  30886. 'type',
  30887. ' TObject = class',
  30888. ' constructor Create;',
  30889. ' end;',
  30890. ' EAssertionFailed = class',
  30891. ' constructor Create(s: string);',
  30892. ' end;',
  30893. '']),
  30894. LinesToStr([
  30895. 'constructor TObject.Create;',
  30896. 'begin end;',
  30897. 'constructor EAssertionFailed.Create(s: string);',
  30898. 'begin end;',
  30899. '']) );
  30900. StartProgram(true);
  30901. Add([
  30902. 'uses sysutils;',
  30903. 'procedure DoIt;',
  30904. 'var',
  30905. ' b: boolean;',
  30906. ' s: string;',
  30907. 'begin',
  30908. ' {$Assertions on}',
  30909. ' Assert(b);',
  30910. ' Assert(b,''msg'');',
  30911. 'end;',
  30912. 'begin',
  30913. ' DoIt;',
  30914. '']);
  30915. ConvertProgram;
  30916. CheckSource('TestAssert_SysUtils',
  30917. LinesToStr([ // statements
  30918. 'this.DoIt = function () {',
  30919. ' var b = false;',
  30920. ' var s = "";',
  30921. ' if (!b) throw pas.SysUtils.EAssertionFailed.$create("Create");',
  30922. ' if (!b) throw pas.SysUtils.EAssertionFailed.$create("Create$1", ["msg"]);',
  30923. '};',
  30924. '']),
  30925. LinesToStr([ // $mod.$main
  30926. '$mod.DoIt();',
  30927. '']));
  30928. end;
  30929. procedure TTestModule.TestObjectChecks;
  30930. begin
  30931. Scanner.CurrentBoolSwitches:=Scanner.CurrentBoolSwitches+[bsObjectChecks];
  30932. StartProgram(false);
  30933. Add([
  30934. 'type',
  30935. ' TObject = class',
  30936. ' procedure DoIt;',
  30937. ' end;',
  30938. ' TClass = class of tobject;',
  30939. ' TBird = class',
  30940. ' end;',
  30941. ' TBirdClass = class of TBird;',
  30942. 'var',
  30943. ' o : TObject;',
  30944. ' c: TClass;',
  30945. ' b: TBird;',
  30946. ' bc: TBirdClass;',
  30947. 'procedure TObject.DoIt;',
  30948. 'begin',
  30949. ' b:=TBird(o);',
  30950. 'end;',
  30951. 'begin',
  30952. ' o.DoIt;',
  30953. ' b:=TBird(o);',
  30954. ' bc:=TBirdClass(c);',
  30955. '']);
  30956. ConvertProgram;
  30957. CheckSource('TestCheckMethodCall',
  30958. LinesToStr([ // statements
  30959. 'rtl.createClass(this, "TObject", null, function () {',
  30960. ' this.$init = function () {',
  30961. ' };',
  30962. ' this.$final = function () {',
  30963. ' };',
  30964. ' this.DoIt = function () {',
  30965. ' rtl.checkMethodCall(this,$mod.TObject);',
  30966. ' $mod.b = rtl.asExt($mod.o, $mod.TBird, 1);',
  30967. ' };',
  30968. '});',
  30969. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  30970. '});',
  30971. 'this.o = null;',
  30972. 'this.c = null;',
  30973. 'this.b = null;',
  30974. 'this.bc = null;',
  30975. '']),
  30976. LinesToStr([ // $mod.$main
  30977. '$mod.o.DoIt();',
  30978. '$mod.b = rtl.asExt($mod.o,$mod.TBird, 1);',
  30979. '$mod.bc = rtl.asExt($mod.c, $mod.TBird, 2);',
  30980. '']));
  30981. end;
  30982. procedure TTestModule.TestOverflowChecks_Int;
  30983. begin
  30984. Scanner.CurrentBoolSwitches:=Scanner.CurrentBoolSwitches+[bsOverflowChecks];
  30985. StartProgram(false);
  30986. Add([
  30987. 'procedure DoIt;',
  30988. 'var',
  30989. ' b: byte;',
  30990. ' n: nativeint;',
  30991. ' u: nativeuint;',
  30992. ' c: currency;',
  30993. 'begin',
  30994. ' n:=n+n;',
  30995. ' n:=n-n;',
  30996. ' n:=n+b;',
  30997. ' n:=b-n;',
  30998. ' n:=n*n;',
  30999. ' n:=n*u;',
  31000. ' c:=c+b;',
  31001. ' c:=b+c;',
  31002. ' c:=c*b;',
  31003. ' c:=b*c;',
  31004. 'end;',
  31005. 'begin',
  31006. '']);
  31007. ConvertProgram;
  31008. CheckSource('TestOverflowChecks_Int',
  31009. LinesToStr([ // statements
  31010. 'this.DoIt = function () {',
  31011. ' var b = 0;',
  31012. ' var n = 0;',
  31013. ' var u = 0;',
  31014. ' var c = 0;',
  31015. ' n = rtl.oc(n + n);',
  31016. ' n = rtl.oc(n - n);',
  31017. ' n = rtl.oc(n + b);',
  31018. ' n = rtl.oc(b - n);',
  31019. ' n = rtl.oc(n * n);',
  31020. ' n = rtl.oc(n * u);',
  31021. ' c = rtl.oc(c + (b * 10000));',
  31022. ' c = rtl.oc((b * 10000) + c);',
  31023. ' c = rtl.oc(c * b);',
  31024. ' c = rtl.oc(b * c);',
  31025. '};',
  31026. '']),
  31027. LinesToStr([ // $mod.$main
  31028. '']));
  31029. end;
  31030. procedure TTestModule.TestRangeChecks_AssignInt;
  31031. begin
  31032. Scanner.Options:=Scanner.Options+[po_CAssignments];
  31033. StartProgram(false);
  31034. Add([
  31035. '{$R+}',
  31036. 'var',
  31037. ' b: byte = 2;',
  31038. ' w: word = 3;',
  31039. 'procedure DoIt(p: byte);',
  31040. 'begin',
  31041. ' b:=w;',
  31042. ' b+=w;',
  31043. ' b:=1;',
  31044. 'end;',
  31045. '{$R-}',
  31046. 'procedure DoSome;',
  31047. 'begin',
  31048. ' DoIt(w);',
  31049. ' b:=w;',
  31050. ' b:=2;',
  31051. 'end;',
  31052. 'begin',
  31053. '{$R+}',
  31054. '']);
  31055. ConvertProgram;
  31056. CheckSource('TestRangeChecks_AssignInt',
  31057. LinesToStr([ // statements
  31058. 'this.b = 2;',
  31059. 'this.w = 3;',
  31060. 'this.DoIt = function (p) {',
  31061. ' rtl.rc(p, 0, 255);',
  31062. ' $mod.b = rtl.rc($mod.w,0,255);',
  31063. ' rtl.rc($mod.b += $mod.w, 0, 255);',
  31064. ' $mod.b = 1;',
  31065. '};',
  31066. 'this.DoSome = function () {',
  31067. ' $mod.DoIt($mod.w);',
  31068. ' $mod.b = $mod.w;',
  31069. ' $mod.b = 2;',
  31070. '};',
  31071. '']),
  31072. LinesToStr([ // $mod.$main
  31073. '']));
  31074. end;
  31075. procedure TTestModule.TestRangeChecks_AssignIntRange;
  31076. begin
  31077. Scanner.Options:=Scanner.Options+[po_CAssignments];
  31078. StartProgram(false);
  31079. Add([
  31080. '{$R+}',
  31081. 'type Ten = 1..10;',
  31082. 'var',
  31083. ' b: Ten = 2;',
  31084. ' w: Ten = 3;',
  31085. 'procedure DoIt(p: Ten);',
  31086. 'begin',
  31087. ' b:=w;',
  31088. ' b+=w;',
  31089. ' b:=1;',
  31090. 'end;',
  31091. '{$R-}',
  31092. 'procedure DoSome;',
  31093. 'begin',
  31094. ' DoIt(w);',
  31095. ' b:=w;',
  31096. ' b:=2;',
  31097. 'end;',
  31098. 'begin',
  31099. '{$R+}',
  31100. '']);
  31101. ConvertProgram;
  31102. CheckSource('TestRangeChecks_AssignIntRange',
  31103. LinesToStr([ // statements
  31104. 'this.b = 2;',
  31105. 'this.w = 3;',
  31106. 'this.DoIt = function (p) {',
  31107. ' rtl.rc(p, 1, 10);',
  31108. ' $mod.b = rtl.rc($mod.w, 1, 10);',
  31109. ' rtl.rc($mod.b += $mod.w, 1, 10);',
  31110. ' $mod.b = 1;',
  31111. '};',
  31112. 'this.DoSome = function () {',
  31113. ' $mod.DoIt($mod.w);',
  31114. ' $mod.b = $mod.w;',
  31115. ' $mod.b = 2;',
  31116. '};',
  31117. '']),
  31118. LinesToStr([ // $mod.$main
  31119. '']));
  31120. end;
  31121. procedure TTestModule.TestRangeChecks_AssignEnum;
  31122. begin
  31123. StartProgram(false);
  31124. Add([
  31125. '{$R+}',
  31126. 'type TEnum = (red,green);',
  31127. 'var',
  31128. ' e: TEnum = red;',
  31129. 'procedure DoIt(p: TEnum);',
  31130. 'begin',
  31131. ' e:=p;',
  31132. ' p:=TEnum(0);',
  31133. ' p:=succ(e);',
  31134. 'end;',
  31135. '{$R-}',
  31136. 'procedure DoSome;',
  31137. 'begin',
  31138. ' DoIt(e);',
  31139. ' e:=TEnum(1);',
  31140. ' e:=pred(e);',
  31141. 'end;',
  31142. 'begin',
  31143. '{$R+}',
  31144. '']);
  31145. ConvertProgram;
  31146. CheckSource('TestRangeChecks_AssignEnum',
  31147. LinesToStr([ // statements
  31148. 'this.TEnum = {',
  31149. ' "0": "red",',
  31150. ' red: 0,',
  31151. ' "1": "green",',
  31152. ' green: 1',
  31153. '};',
  31154. 'this.e = this.TEnum.red;',
  31155. 'this.DoIt = function (p) {',
  31156. ' rtl.rc(p, 0, 1);',
  31157. ' $mod.e = rtl.rc(p, 0, 1);',
  31158. ' p = 0;',
  31159. ' p = rtl.rc($mod.e + 1, 0, 1);',
  31160. '};',
  31161. 'this.DoSome = function () {',
  31162. ' $mod.DoIt($mod.e);',
  31163. ' $mod.e = 1;',
  31164. ' $mod.e = $mod.e - 1;',
  31165. '};',
  31166. '']),
  31167. LinesToStr([ // $mod.$main
  31168. '']));
  31169. end;
  31170. procedure TTestModule.TestRangeChecks_AssignEnumRange;
  31171. begin
  31172. StartProgram(false);
  31173. Add([
  31174. '{$R+}',
  31175. 'type',
  31176. ' TEnum = (red,green);',
  31177. ' TEnumRg = red..green;',
  31178. 'var',
  31179. ' e: TEnumRg = red;',
  31180. 'procedure DoIt(p: TEnumRg);',
  31181. 'begin',
  31182. ' e:=p;',
  31183. ' p:=TEnumRg(0);',
  31184. ' p:=succ(e);',
  31185. 'end;',
  31186. '{$R-}',
  31187. 'procedure DoSome;',
  31188. 'begin',
  31189. ' DoIt(e);',
  31190. ' e:=TEnum(1);',
  31191. ' e:=pred(e);',
  31192. 'end;',
  31193. 'begin',
  31194. '{$R+}',
  31195. '']);
  31196. ConvertProgram;
  31197. CheckSource('TestRangeChecks_AssignEnumRange',
  31198. LinesToStr([ // statements
  31199. 'this.TEnum = {',
  31200. ' "0": "red",',
  31201. ' red: 0,',
  31202. ' "1": "green",',
  31203. ' green: 1',
  31204. '};',
  31205. 'this.e = this.TEnum.red;',
  31206. 'this.DoIt = function (p) {',
  31207. ' rtl.rc(p, 0, 1);',
  31208. ' $mod.e = rtl.rc(p, 0, 1);',
  31209. ' p = 0;',
  31210. ' p = rtl.rc($mod.e + 1, 0, 1);',
  31211. '};',
  31212. 'this.DoSome = function () {',
  31213. ' $mod.DoIt($mod.e);',
  31214. ' $mod.e = 1;',
  31215. ' $mod.e = $mod.e - 1;',
  31216. '};',
  31217. '']),
  31218. LinesToStr([ // $mod.$main
  31219. '']));
  31220. end;
  31221. procedure TTestModule.TestRangeChecks_AssignChar;
  31222. begin
  31223. StartProgram(false);
  31224. Add([
  31225. '{$R+}',
  31226. 'type',
  31227. ' TLetter = char;',
  31228. 'var',
  31229. ' b: TLetter = ''2'';',
  31230. ' w: TLetter = ''3'';',
  31231. 'procedure DoIt(p: TLetter);',
  31232. 'begin',
  31233. ' b:=w;',
  31234. ' b:=''1'';',
  31235. 'end;',
  31236. '{$R-}',
  31237. 'procedure DoSome;',
  31238. 'begin',
  31239. ' DoIt(w);',
  31240. ' b:=w;',
  31241. ' b:=''2'';',
  31242. 'end;',
  31243. 'begin',
  31244. '{$R+}',
  31245. '']);
  31246. ConvertProgram;
  31247. CheckSource('TestRangeChecks_AssignChar',
  31248. LinesToStr([ // statements
  31249. 'this.b = "2";',
  31250. 'this.w = "3";',
  31251. 'this.DoIt = function (p) {',
  31252. ' rtl.rcc(p, 0, 65535);',
  31253. ' $mod.b = rtl.rcc($mod.w, 0, 65535);',
  31254. ' $mod.b = "1";',
  31255. '};',
  31256. 'this.DoSome = function () {',
  31257. ' $mod.DoIt($mod.w);',
  31258. ' $mod.b = $mod.w;',
  31259. ' $mod.b = "2";',
  31260. '};',
  31261. '']),
  31262. LinesToStr([ // $mod.$main
  31263. '']));
  31264. end;
  31265. procedure TTestModule.TestRangeChecks_AssignCharRange;
  31266. begin
  31267. StartProgram(false);
  31268. Add([
  31269. '{$R+}',
  31270. 'type TDigit = ''0''..''9'';',
  31271. 'var',
  31272. ' b: TDigit = ''2'';',
  31273. ' w: TDigit = ''3'';',
  31274. 'procedure DoIt(p: TDigit);',
  31275. 'begin',
  31276. ' b:=w;',
  31277. ' b:=''1'';',
  31278. 'end;',
  31279. '{$R-}',
  31280. 'procedure DoSome;',
  31281. 'begin',
  31282. ' DoIt(w);',
  31283. ' b:=w;',
  31284. ' b:=''2'';',
  31285. 'end;',
  31286. 'begin',
  31287. '{$R+}',
  31288. '']);
  31289. ConvertProgram;
  31290. CheckSource('TestRangeChecks_AssignCharRange',
  31291. LinesToStr([ // statements
  31292. 'this.b = "2";',
  31293. 'this.w = "3";',
  31294. 'this.DoIt = function (p) {',
  31295. ' rtl.rcc(p, 48, 57);',
  31296. ' $mod.b = rtl.rcc($mod.w, 48, 57);',
  31297. ' $mod.b = "1";',
  31298. '};',
  31299. 'this.DoSome = function () {',
  31300. ' $mod.DoIt($mod.w);',
  31301. ' $mod.b = $mod.w;',
  31302. ' $mod.b = "2";',
  31303. '};',
  31304. '']),
  31305. LinesToStr([ // $mod.$main
  31306. '']));
  31307. end;
  31308. procedure TTestModule.TestRangeChecks_ArrayIndex;
  31309. begin
  31310. StartProgram(false);
  31311. Add([
  31312. '{$R+}',
  31313. 'type',
  31314. ' Ten = 1..10;',
  31315. ' TArr = array of Ten;',
  31316. ' TArrArr = array of TArr;',
  31317. ' TArrByte = array[byte] of Ten;',
  31318. ' TArrChar = array[''0''..''9''] of Ten;',
  31319. ' TArrByteChar = array[byte,''0''..''9''] of Ten;',
  31320. ' TObject = class',
  31321. ' A: TArr;',
  31322. ' end;',
  31323. 'procedure DoIt;',
  31324. 'var',
  31325. ' Arr: TArr;',
  31326. ' ArrArr: TArrArr;',
  31327. ' ArrByte: TArrByte;',
  31328. ' ArrChar: TArrChar;',
  31329. ' ArrByteChar: TArrByteChar;',
  31330. ' i: Ten;',
  31331. ' c: char;',
  31332. ' o: tobject;',
  31333. 'begin',
  31334. ' i:=Arr[1];',
  31335. ' i:=ArrByteChar[1,''2''];',
  31336. ' Arr[1]:=Arr[1];',
  31337. ' Arr[i]:=Arr[i];',
  31338. ' ArrByte[3]:=ArrByte[3];',
  31339. ' ArrByte[i]:=ArrByte[i];',
  31340. ' ArrChar[''5'']:=ArrChar[''5''];',
  31341. ' ArrChar[c]:=ArrChar[c];',
  31342. ' ArrByteChar[7,''7'']:=ArrByteChar[7,''7''];',
  31343. ' ArrByteChar[i,c]:=ArrByteChar[i,c];',
  31344. ' o.a[i]:=o.a[i];',
  31345. 'end;',
  31346. 'begin',
  31347. '']);
  31348. ConvertProgram;
  31349. CheckSource('TestRangeChecks_ArrayIndex',
  31350. LinesToStr([ // statements
  31351. 'rtl.createClass(this, "TObject", null, function () {',
  31352. ' this.$init = function () {',
  31353. ' this.A = [];',
  31354. ' };',
  31355. ' this.$final = function () {',
  31356. ' this.A = undefined;',
  31357. ' };',
  31358. '});',
  31359. 'this.DoIt = function () {',
  31360. ' var Arr = [];',
  31361. ' var ArrArr = [];',
  31362. ' var ArrByte = rtl.arraySetLength(null, 0, 256);',
  31363. ' var ArrChar = rtl.arraySetLength(null, 0, 10);',
  31364. ' var ArrByteChar = rtl.arraySetLength(null, 0, 256, 10);',
  31365. ' var i = 0;',
  31366. ' var c = "";',
  31367. ' var o = null;',
  31368. ' i = rtl.rc(Arr[1], 1, 10);',
  31369. ' i = rtl.rc(ArrByteChar[1][2], 1, 10);',
  31370. ' Arr[1] = rtl.rc(Arr[1], 1, 10);',
  31371. ' rtl.rcArrW(Arr, i, rtl.rcArrR(Arr, i));',
  31372. ' ArrByte[3] = rtl.rc(ArrByte[3], 1, 10);',
  31373. ' rtl.rcArrW(ArrByte, i, rtl.rcArrR(ArrByte, i));',
  31374. ' ArrChar[5] = rtl.rc(ArrChar[5], 1, 10);',
  31375. ' rtl.rcArrW(ArrChar, c.charCodeAt() - 48, rtl.rcArrR(ArrChar, c.charCodeAt() - 48));',
  31376. ' ArrByteChar[7][7] = rtl.rc(ArrByteChar[7][7], 1, 10);',
  31377. ' rtl.rcArrW(ArrByteChar, i, c.charCodeAt() - 48, rtl.rcArrR(ArrByteChar, i, c.charCodeAt() - 48));',
  31378. ' rtl.rcArrW(o.A, i, rtl.rcArrR(o.A, i));',
  31379. '};',
  31380. '']),
  31381. LinesToStr([ // $mod.$main
  31382. '']));
  31383. end;
  31384. procedure TTestModule.TestRangeChecks_ArrayOfRecIndex;
  31385. begin
  31386. StartProgram(false);
  31387. Add([
  31388. '{$R+}',
  31389. 'type',
  31390. ' Ten = 1..10;',
  31391. ' TRec = record x: Ten end;',
  31392. ' TArr = array of TRec;',
  31393. ' TArrArr = array of TArr;',
  31394. ' TObject = class',
  31395. ' A: TArr;',
  31396. ' end;',
  31397. 'procedure DoIt;',
  31398. 'var',
  31399. ' Arr: TArr;',
  31400. ' ArrArr: TArrArr;',
  31401. ' i: Ten;',
  31402. ' o: tobject;',
  31403. 'begin',
  31404. ' Arr[1]:=Arr[1];',
  31405. ' Arr[i]:=Arr[i+1];',
  31406. ' o.a[i]:=o.a[i+2];',
  31407. 'end;',
  31408. 'begin',
  31409. '']);
  31410. ConvertProgram;
  31411. CheckSource('TestRangeChecks_ArrayOfRecIndex',
  31412. LinesToStr([ // statements
  31413. 'rtl.recNewT(this, "TRec", function () {',
  31414. ' this.x = 0;',
  31415. ' this.$eq = function (b) {',
  31416. ' return this.x === b.x;',
  31417. ' };',
  31418. ' this.$assign = function (s) {',
  31419. ' this.x = s.x;',
  31420. ' return this;',
  31421. ' };',
  31422. '});',
  31423. 'rtl.createClass(this, "TObject", null, function () {',
  31424. ' this.$init = function () {',
  31425. ' this.A = [];',
  31426. ' };',
  31427. ' this.$final = function () {',
  31428. ' this.A = undefined;',
  31429. ' };',
  31430. '});',
  31431. 'this.DoIt = function () {',
  31432. ' var Arr = [];',
  31433. ' var ArrArr = [];',
  31434. ' var i = 0;',
  31435. ' var o = null;',
  31436. ' Arr[1].$assign(Arr[1]);',
  31437. ' rtl.rcArrR(Arr, i).$assign(rtl.rcArrR(Arr, i + 1));',
  31438. ' rtl.rcArrR(o.A, i).$assign(rtl.rcArrR(o.A, i + 2));',
  31439. '};',
  31440. '']),
  31441. LinesToStr([ // $mod.$main
  31442. '']));
  31443. end;
  31444. procedure TTestModule.TestRangeChecks_StringIndex;
  31445. begin
  31446. StartProgram(false);
  31447. Add([
  31448. 'type',
  31449. ' TObject = class',
  31450. ' S: string;',
  31451. ' end;',
  31452. '{$R+}',
  31453. 'procedure DoIt(var h: string);',
  31454. 'var',
  31455. ' s: string;',
  31456. ' i: longint;',
  31457. ' c: char;',
  31458. ' o: tobject;',
  31459. 'begin',
  31460. ' c:=s[1];',
  31461. ' s[i]:=s[i];',
  31462. ' h[i]:=h[i];',
  31463. ' c:=o.s[i];',
  31464. ' o.s[i]:=c;',
  31465. 'end;',
  31466. 'begin',
  31467. '']);
  31468. ConvertProgram;
  31469. CheckSource('TestRangeChecks_StringIndex',
  31470. LinesToStr([ // statements
  31471. 'rtl.createClass(this, "TObject", null, function () {',
  31472. ' this.$init = function () {',
  31473. ' this.S = "";',
  31474. ' };',
  31475. ' this.$final = function () {',
  31476. ' };',
  31477. '});',
  31478. 'this.DoIt = function (h) {',
  31479. ' var s = "";',
  31480. ' var i = 0;',
  31481. ' var c = "";',
  31482. ' var o = null;',
  31483. ' c = rtl.rcc(rtl.rcCharAt(s, 0), 0, 65535);',
  31484. ' s = rtl.rcSetCharAt(s, i - 1, rtl.rcCharAt(s, i - 1));',
  31485. ' h.set(rtl.rcSetCharAt(h.get(), i - 1, rtl.rcCharAt(h.get(), i - 1)));',
  31486. ' c = rtl.rcc(rtl.rcCharAt(o.S, i - 1), 0, 65535);',
  31487. ' o.S = rtl.rcSetCharAt(o.S, i - 1, c);',
  31488. '};',
  31489. '']),
  31490. LinesToStr([ // $mod.$main
  31491. '']));
  31492. end;
  31493. procedure TTestModule.TestRangeChecks_TypecastInt;
  31494. begin
  31495. StartProgram(false);
  31496. Add([
  31497. '{$R+}',
  31498. 'var',
  31499. ' i: nativeint;',
  31500. ' b: byte;',
  31501. ' sh: shortint;',
  31502. ' w: word;',
  31503. ' sm: smallint;',
  31504. ' lw: longword;',
  31505. ' li: longint;',
  31506. 'begin',
  31507. ' b:=12+byte(i);',
  31508. ' sh:=12+shortint(i);',
  31509. ' w:=12+word(i);',
  31510. ' sm:=12+smallint(i);',
  31511. ' lw:=12+longword(i);',
  31512. ' li:=12+longint(i);',
  31513. '']);
  31514. ConvertProgram;
  31515. CheckSource('TestRangeChecks_TypecastInt',
  31516. LinesToStr([
  31517. 'this.i = 0;',
  31518. 'this.b = 0;',
  31519. 'this.sh = 0;',
  31520. 'this.w = 0;',
  31521. 'this.sm = 0;',
  31522. 'this.lw = 0;',
  31523. 'this.li = 0;',
  31524. '']),
  31525. LinesToStr([
  31526. '$mod.b = rtl.rc(12 + rtl.rc($mod.i, 0, 255), 0, 255);',
  31527. '$mod.sh = rtl.rc(12 + rtl.rc($mod.i, -128, 127), -128, 127);',
  31528. '$mod.w = rtl.rc(12 + rtl.rc($mod.i, 0, 65535), 0, 65535);',
  31529. '$mod.sm = rtl.rc(12 + rtl.rc($mod.i, -32768, 32767), -32768, 32767);',
  31530. '$mod.lw = rtl.rc(12 + rtl.rc($mod.i, 0, 4294967295), 0, 4294967295);',
  31531. '$mod.li = rtl.rc(12 + rtl.rc($mod.i, -2147483648, 2147483647), -2147483648, 2147483647);',
  31532. '']));
  31533. end;
  31534. procedure TTestModule.TestRangeChecks_TypeHelperInt;
  31535. begin
  31536. Scanner.Options:=Scanner.Options+[po_CAssignments];
  31537. StartProgram(false);
  31538. Add([
  31539. '{$modeswitch typehelpers}',
  31540. '{$R+}',
  31541. 'type',
  31542. ' TObject = class',
  31543. ' FSize: byte;',
  31544. ' property Size: byte read FSize;',
  31545. ' end;',
  31546. ' THelper = type helper for byte',
  31547. ' procedure SetIt(w: word);',
  31548. ' end;',
  31549. 'procedure THelper.SetIt(w: word);',
  31550. 'begin',
  31551. ' Self:=w;',
  31552. 'end;',
  31553. 'function GetIt: byte;',
  31554. 'begin',
  31555. ' Result.SetIt(2);',
  31556. 'end;',
  31557. 'var',
  31558. ' b: byte = 3;',
  31559. ' o: TObject;',
  31560. 'begin',
  31561. ' b.SetIt(14);',
  31562. ' with b do SetIt(15);',
  31563. ' o.Size.SetIt(16);',
  31564. '']);
  31565. ConvertProgram;
  31566. CheckSource('TestRangeChecks_AssignInt',
  31567. LinesToStr([ // statements
  31568. 'rtl.createClass(this, "TObject", null, function () {',
  31569. ' this.$init = function () {',
  31570. ' this.FSize = 0;',
  31571. ' };',
  31572. ' this.$final = function () {',
  31573. ' };',
  31574. '});',
  31575. 'rtl.createHelper(this, "THelper", null, function () {',
  31576. ' this.SetIt = function (w) {',
  31577. ' rtl.rc(w, 0, 65535);',
  31578. ' this.set(w);',
  31579. ' };',
  31580. '});',
  31581. 'this.GetIt = function () {',
  31582. ' var Result = 0;',
  31583. ' $mod.THelper.SetIt.call({',
  31584. ' get: function () {',
  31585. ' return Result;',
  31586. ' },',
  31587. ' set: function (v) {',
  31588. ' rtl.rc(v, 0, 255);',
  31589. ' Result = v;',
  31590. ' }',
  31591. ' }, 2);',
  31592. ' return Result;',
  31593. '};',
  31594. 'this.b = 3;',
  31595. 'this.o = null;',
  31596. '']),
  31597. LinesToStr([ // $mod.$main
  31598. '$mod.THelper.SetIt.call({',
  31599. ' p: $mod,',
  31600. ' get: function () {',
  31601. ' return this.p.b;',
  31602. ' },',
  31603. ' set: function (v) {',
  31604. ' rtl.rc(v, 0, 255);',
  31605. ' this.p.b = v;',
  31606. ' }',
  31607. '}, 14);',
  31608. 'var $with = $mod.b;',
  31609. '$mod.THelper.SetIt.call({',
  31610. ' get: function () {',
  31611. ' return $with;',
  31612. ' },',
  31613. ' set: function (v) {',
  31614. ' rtl.rc(v, 0, 255);',
  31615. ' $with = v;',
  31616. ' }',
  31617. '}, 15);',
  31618. '$mod.THelper.SetIt.call({',
  31619. ' p: $mod.o,',
  31620. ' get: function () {',
  31621. ' return this.p.FSize;',
  31622. ' },',
  31623. ' set: function (v) {',
  31624. ' rtl.rc(v, 0, 255);',
  31625. ' this.p.FSize = v;',
  31626. ' }',
  31627. '}, 16);',
  31628. '']));
  31629. end;
  31630. procedure TTestModule.TestAsync_Proc;
  31631. begin
  31632. StartProgram(false);
  31633. Add([
  31634. 'procedure Fly(w: word = 1); async; forward;',
  31635. 'procedure Run(w: word = 2); async;',
  31636. 'begin',
  31637. ' Fly(w);',
  31638. ' Fly;',
  31639. ' await(Fly(w));',
  31640. ' await(Fly);',
  31641. 'end;',
  31642. 'procedure Fly(w: word); ',
  31643. 'begin',
  31644. 'end;',
  31645. 'begin',
  31646. ' Run;',
  31647. ' Run(3);',
  31648. '']);
  31649. CheckResolverUnexpectedHints();
  31650. ConvertProgram;
  31651. CheckSource('TestAsync_Proc',
  31652. LinesToStr([ // statements
  31653. 'this.Run = async function (w) {',
  31654. ' $mod.Fly(w);',
  31655. ' $mod.Fly(1);',
  31656. ' await $mod.Fly(w);',
  31657. ' await $mod.Fly(1);',
  31658. '};',
  31659. 'this.Fly = async function (w) {',
  31660. '};',
  31661. '']),
  31662. LinesToStr([
  31663. '$mod.Run(2);',
  31664. '$mod.Run(3);',
  31665. '']));
  31666. end;
  31667. procedure TTestModule.TestAsync_CallResultIsPromise;
  31668. begin
  31669. StartProgram(false);
  31670. Add([
  31671. '{$modeswitch externalclass}',
  31672. 'type',
  31673. ' TObject = class',
  31674. ' end;',
  31675. ' TJSPromise = class external name ''Promise''',
  31676. ' end;',
  31677. ' TBird = class',
  31678. ' function Fly: word; async; ',
  31679. ' end;',
  31680. 'function TBird.Fly: word; async; ',
  31681. 'begin',
  31682. ' Result:=3;',
  31683. ' Fly:=4+Result;',
  31684. ' if Result=5 then ;',
  31685. ' exit(6);',
  31686. 'end;',
  31687. 'function Run: word; async;',
  31688. 'begin',
  31689. ' Result:=11+Result;',
  31690. ' inc(Result);',
  31691. 'end;',
  31692. 'var',
  31693. ' p: TJSPromise;',
  31694. ' o: TBird;',
  31695. 'begin',
  31696. ' p:=Run;',
  31697. ' p:=Run();',
  31698. ' if Run=p then ;',
  31699. ' if p=Run then ;',
  31700. ' if Run()=p then ;',
  31701. ' if p=Run() then ;',
  31702. ' p:=o.Fly;',
  31703. ' p:=o.Fly();',
  31704. ' if o.Fly=p then ;',
  31705. ' if o.Fly()=p then ;',
  31706. ' with o do begin',
  31707. ' p:=Fly;',
  31708. ' p:=Fly();',
  31709. ' if Fly=p then ;',
  31710. ' if Fly()=p then ;',
  31711. ' end;',
  31712. '']);
  31713. CheckResolverUnexpectedHints();
  31714. ConvertProgram;
  31715. CheckSource('TestAsync_CallResultIsPromise',
  31716. LinesToStr([ // statements
  31717. 'rtl.createClass(this, "TObject", null, function () {',
  31718. ' this.$init = function () {',
  31719. ' };',
  31720. ' this.$final = function () {',
  31721. ' };',
  31722. '});',
  31723. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  31724. ' this.Fly = async function () {',
  31725. ' var Result = 0;',
  31726. ' Result = 3;',
  31727. ' Result = 4 + Result;',
  31728. ' if (Result === 5) ;',
  31729. ' return 6;',
  31730. ' return Result;',
  31731. ' };',
  31732. '});',
  31733. 'this.Run = async function () {',
  31734. ' var Result = 0;',
  31735. ' Result = 11 + Result;',
  31736. ' Result += 1;',
  31737. ' return Result;',
  31738. '};',
  31739. 'this.p = null;',
  31740. 'this.o = null;',
  31741. '']),
  31742. LinesToStr([
  31743. '$mod.p = $mod.Run();',
  31744. '$mod.p = $mod.Run();',
  31745. 'if ($mod.Run() === $mod.p) ;',
  31746. 'if ($mod.p === $mod.Run()) ;',
  31747. 'if ($mod.Run() === $mod.p) ;',
  31748. 'if ($mod.p === $mod.Run()) ;',
  31749. '$mod.p = $mod.o.Fly();',
  31750. '$mod.p = $mod.o.Fly();',
  31751. 'if ($mod.o.Fly() === $mod.p) ;',
  31752. 'if ($mod.o.Fly() === $mod.p) ;',
  31753. 'var $with = $mod.o;',
  31754. '$mod.p = $with.Fly();',
  31755. '$mod.p = $with.Fly();',
  31756. 'if ($with.Fly() === $mod.p) ;',
  31757. 'if ($with.Fly() === $mod.p) ;',
  31758. '']));
  31759. end;
  31760. procedure TTestModule.TestAsync_ConstructorFail;
  31761. begin
  31762. StartProgram(false);
  31763. Add([
  31764. 'type',
  31765. ' TObject = class',
  31766. ' end;',
  31767. ' TBird = class',
  31768. ' constructor Create; async;',
  31769. ' end;',
  31770. 'constructor TBird.Create; async;',
  31771. 'begin',
  31772. 'end;',
  31773. 'begin',
  31774. '']);
  31775. SetExpectedPasResolverError('Invalid constructor modifier async',nInvalidXModifierY);
  31776. ConvertProgram;
  31777. end;
  31778. procedure TTestModule.TestAsync_PropertyGetterFail;
  31779. begin
  31780. StartProgram(false);
  31781. Add([
  31782. 'type',
  31783. ' TObject = class',
  31784. ' end;',
  31785. ' TBird = class',
  31786. ' function GetSize: word; async;',
  31787. ' property Size: word read GetSize;',
  31788. ' end;',
  31789. 'function TBird.GetSize: word; async;',
  31790. 'begin',
  31791. 'end;',
  31792. 'begin',
  31793. '']);
  31794. SetExpectedPasResolverError('Invalid property getter modifier async',nInvalidXModifierY);
  31795. ConvertProgram;
  31796. end;
  31797. procedure TTestModule.TestAwait_NonPromiseWithTypeFail;
  31798. begin
  31799. StartProgram(false);
  31800. Add([
  31801. 'procedure Run; async;',
  31802. 'begin',
  31803. ' await(word,1);',
  31804. 'end;',
  31805. 'begin',
  31806. '']);
  31807. SetExpectedPasResolverError('Incompatible type arg no. 2: Got "Longint", expected "TJSPromise"',nIncompatibleTypeArgNo);
  31808. ConvertProgram;
  31809. end;
  31810. procedure TTestModule.TestAwait_AsyncCallTypeMismatch;
  31811. begin
  31812. StartProgram(false);
  31813. Add([
  31814. 'type',
  31815. ' TObject = class',
  31816. ' end;',
  31817. ' TBird = class',
  31818. ' end;',
  31819. 'function Fly: TObject; async;',
  31820. 'begin',
  31821. 'end;',
  31822. 'procedure Run; async;',
  31823. 'begin',
  31824. ' await(TBird,Fly);',
  31825. 'end;',
  31826. 'begin',
  31827. '']);
  31828. SetExpectedPasResolverError('Incompatible type arg no. 2: Got "TObject", expected "TBird"',nIncompatibleTypeArgNo);
  31829. ConvertProgram;
  31830. end;
  31831. procedure TTestModule.TestAWait_OutsideAsyncFail;
  31832. begin
  31833. StartProgram(false);
  31834. Add([
  31835. 'procedure Crawl(w: double); ',
  31836. 'begin',
  31837. 'end;',
  31838. 'procedure Run(w: double);',
  31839. 'begin',
  31840. ' await(Crawl(w));',
  31841. 'end;',
  31842. 'begin',
  31843. ' Run(1);']);
  31844. SetExpectedPasResolverError(sAWaitOnlyInAsyncProcedure,nAWaitOnlyInAsyncProcedure);
  31845. ConvertProgram;
  31846. end;
  31847. procedure TTestModule.TestAWait_Result;
  31848. begin
  31849. StartProgram(false);
  31850. Add([
  31851. '{$modeswitch externalclass}',
  31852. 'type',
  31853. ' TJSPromise = class external name ''Promise''',
  31854. ' end;',
  31855. 'function Crawl(d: double = 1.3): word; ',
  31856. 'begin',
  31857. 'end;',
  31858. 'function Run(d: double = 1.6): word; async;',
  31859. 'begin',
  31860. ' Result:=await(1);',
  31861. ' Result:=await(Crawl);',
  31862. ' Result:=await(Crawl(4.5));',
  31863. ' Result:=await(Run);',
  31864. ' Result:=await(Run(6.7));',
  31865. 'end;',
  31866. 'begin',
  31867. ' Run(1);']);
  31868. ConvertProgram;
  31869. CheckSource('TestAWait_Result',
  31870. LinesToStr([ // statements
  31871. 'this.Crawl = function (d) {',
  31872. ' var Result = 0;',
  31873. ' return Result;',
  31874. '};',
  31875. 'this.Run = async function (d) {',
  31876. ' var Result = 0;',
  31877. ' Result = await 1;',
  31878. ' Result = await $mod.Crawl(1.3);',
  31879. ' Result = await $mod.Crawl(4.5);',
  31880. ' Result = await $mod.Run(1.6);',
  31881. ' Result = await $mod.Run(6.7);',
  31882. ' return Result;',
  31883. '};',
  31884. '']),
  31885. LinesToStr([
  31886. '$mod.Run(1);'
  31887. ]));
  31888. SetExpectedPasResolverError('Await without promise',nAwaitWithoutPromise);
  31889. end;
  31890. procedure TTestModule.TestAWait_ExternalClassPromise;
  31891. begin
  31892. StartProgram(false);
  31893. Add([
  31894. '{$modeswitch externalclass}',
  31895. 'type',
  31896. ' TJSPromise = class external name ''Promise''',
  31897. ' end;',
  31898. 'function Fly(w: word): TJSPromise;',
  31899. 'begin',
  31900. 'end;',
  31901. 'function Jump(w: word): word; async;',
  31902. 'begin',
  31903. 'end;',
  31904. 'function Eat(w: word): TJSPromise; async;',
  31905. 'begin',
  31906. 'end;',
  31907. 'function Run(d: double): word; async;',
  31908. 'var',
  31909. ' p: TJSPromise;',
  31910. 'begin',
  31911. ' Result:=await(word,p);', // promise needs type
  31912. ' Result:=await(word,Fly(3));', // promise needs type
  31913. ' Result:=await(Jump(4));', // async non promise must omit the type
  31914. ' Result:=await(word,Jump(5));', // async call can provide fitting type
  31915. ' Result:=await(word,Eat(6));', // promise needs type
  31916. 'end;',
  31917. 'begin',
  31918. '']);
  31919. ConvertProgram;
  31920. CheckSource('TestAWait_ExternalClassPromise',
  31921. LinesToStr([ // statements
  31922. 'this.Fly = function (w) {',
  31923. ' var Result = null;',
  31924. ' return Result;',
  31925. '};',
  31926. 'this.Jump = async function (w) {',
  31927. ' var Result = 0;',
  31928. ' return Result;',
  31929. '};',
  31930. 'this.Eat = async function (w) {',
  31931. ' var Result = null;',
  31932. ' return Result;',
  31933. '};',
  31934. 'this.Run = async function (d) {',
  31935. ' var Result = 0;',
  31936. ' var p = null;',
  31937. ' Result = await p;',
  31938. ' Result = await $mod.Fly(3);',
  31939. ' Result = await $mod.Jump(4);',
  31940. ' Result = await $mod.Jump(5);',
  31941. ' Result = await $mod.Eat(6);',
  31942. ' return Result;',
  31943. '};',
  31944. '']),
  31945. LinesToStr([
  31946. ]));
  31947. CheckResolverUnexpectedHints();
  31948. end;
  31949. procedure TTestModule.TestAsync_AnonymousProc;
  31950. begin
  31951. StartProgram(false);
  31952. Add([
  31953. '{$mode objfpc}',
  31954. '{$modeswitch externalclass}',
  31955. 'type',
  31956. ' TJSPromise = class external name ''Promise''',
  31957. ' end;',
  31958. 'type',
  31959. ' TFunc = reference to function(x: double): word; async;',
  31960. 'function Crawl(d: double = 1.3): word; async;',
  31961. 'begin',
  31962. 'end;',
  31963. 'var Func: TFunc;',
  31964. 'begin',
  31965. ' Func:=function(c:double):word async begin',
  31966. ' Result:=await(Crawl(c));',
  31967. ' end;',
  31968. ' Func:=function(c:double):word async assembler asm',
  31969. ' end;',
  31970. ' ']);
  31971. ConvertProgram;
  31972. CheckSource('TestAsync_AnonymousProc',
  31973. LinesToStr([ // statements
  31974. 'this.Crawl = async function (d) {',
  31975. ' var Result = 0;',
  31976. ' return Result;',
  31977. '};',
  31978. 'this.Func = null;',
  31979. '']),
  31980. LinesToStr([
  31981. '$mod.Func = async function (c) {',
  31982. ' var Result = 0;',
  31983. ' Result = await $mod.Crawl(c);',
  31984. ' return Result;',
  31985. '};',
  31986. '$mod.Func = async function (c) {',
  31987. '};',
  31988. '']));
  31989. CheckResolverUnexpectedHints();
  31990. end;
  31991. procedure TTestModule.TestAsync_ProcType;
  31992. begin
  31993. StartProgram(false);
  31994. Add([
  31995. '{$mode objfpc}',
  31996. 'type',
  31997. ' TRefFunc = reference to function(x: double = 1.3): word; async;',
  31998. ' TFunc = function(x: double = 1.1): word; async;',
  31999. ' TProc = procedure(x: longint = 7); async;',
  32000. 'function Crawl(d: double): word; async;',
  32001. 'begin',
  32002. 'end;',
  32003. 'procedure Run(e:longint); async;',
  32004. 'begin',
  32005. 'end;',
  32006. 'procedure Fly(p: TProc); async;',
  32007. 'begin',
  32008. ' await(p);',
  32009. ' await(p());',
  32010. 'end;',
  32011. 'var',
  32012. ' RefFunc: TRefFunc;',
  32013. ' Func: TFunc;',
  32014. ' Proc, ProcB: TProc;',
  32015. 'begin',
  32016. ' Func:=@Crawl;',
  32017. ' RefFunc:=@Crawl;',
  32018. ' RefFunc:=function(c:double):word async begin',
  32019. ' Result:=await(RefFunc);',
  32020. ' Result:=await(RefFunc());',
  32021. ' Result:=await(Func);',
  32022. ' Result:=await(Func());',
  32023. ' await(Proc);',
  32024. ' await(Proc());',
  32025. ' await(Proc(13));',
  32026. ' end;',
  32027. ' Proc:=@Run;',
  32028. ' if Proc=ProcB then ;',
  32029. ' ']);
  32030. ConvertProgram;
  32031. CheckResolverUnexpectedHints();
  32032. CheckSource('TestAsync_ProcType',
  32033. LinesToStr([ // statements
  32034. 'this.Crawl = async function (d) {',
  32035. ' var Result = 0;',
  32036. ' return Result;',
  32037. '};',
  32038. 'this.Run = async function (e) {',
  32039. '};',
  32040. 'this.Fly = async function (p) {',
  32041. ' await p(7);',
  32042. ' await p(7);',
  32043. '};',
  32044. 'this.RefFunc = null;',
  32045. 'this.Func = null;',
  32046. 'this.Proc = null;',
  32047. 'this.ProcB = null;',
  32048. '']),
  32049. LinesToStr([
  32050. '$mod.Func = $mod.Crawl;',
  32051. '$mod.RefFunc = $mod.Crawl;',
  32052. '$mod.RefFunc = async function (c) {',
  32053. ' var Result = 0;',
  32054. ' Result = await $mod.RefFunc(1.3);',
  32055. ' Result = await $mod.RefFunc(1.3);',
  32056. ' Result = await $mod.Func(1.1);',
  32057. ' Result = await $mod.Func(1.1);',
  32058. ' await $mod.Proc(7);',
  32059. ' await $mod.Proc(7);',
  32060. ' await $mod.Proc(13);',
  32061. ' return Result;',
  32062. '};',
  32063. '$mod.Proc = $mod.Run;',
  32064. 'if (rtl.eqCallback($mod.Proc, $mod.ProcB)) ;',
  32065. '']));
  32066. end;
  32067. procedure TTestModule.TestAsync_ProcTypeAsyncModMismatchFail;
  32068. begin
  32069. StartProgram(false);
  32070. Add([
  32071. '{$mode objfpc}',
  32072. 'type',
  32073. ' TRefFunc = reference to function(x: double = 1.3): word;',
  32074. 'function Crawl(d: double): word; async;',
  32075. 'begin',
  32076. 'end;',
  32077. 'var',
  32078. ' RefFunc: TRefFunc;',
  32079. 'begin',
  32080. ' RefFunc:=@Crawl;',
  32081. ' ']);
  32082. SetExpectedPasResolverError('procedure type modifier "async" mismatch',nXModifierMismatchY);
  32083. ConvertProgram;
  32084. end;
  32085. procedure TTestModule.TestAsync_Inherited;
  32086. begin
  32087. StartProgram(false);
  32088. Add([
  32089. '{$mode objfpc}',
  32090. '{$modeswitch externalclass}',
  32091. 'type',
  32092. ' TJSPromise = class external name ''Promise''',
  32093. ' end;',
  32094. ' TObject = class',
  32095. ' function Run(w: word = 3): word; async; virtual;',
  32096. ' end;',
  32097. ' TBird = class',
  32098. ' function Run(w: word = 3): word; async; override;',
  32099. ' end;',
  32100. 'function TObject.Run(w: word = 3): word; async;',
  32101. 'begin',
  32102. 'end;',
  32103. 'function TBird.Run(w: word = 3): word;', // async modifier not needed in impl
  32104. 'var p: TJSPromise;',
  32105. 'begin',
  32106. ' p:=inherited;',
  32107. ' p:=inherited Run;',
  32108. ' p:=inherited Run();',
  32109. ' p:=inherited Run(4);',
  32110. ' exit(p);',
  32111. ' exit(inherited);',
  32112. ' exit(inherited Run);',
  32113. ' exit(inherited Run(5));',
  32114. ' exit(6);',
  32115. 'end;',
  32116. 'begin',
  32117. ' ']);
  32118. ConvertProgram;
  32119. CheckSource('TestAsync_Inherited',
  32120. LinesToStr([ // statements
  32121. 'rtl.createClass(this, "TObject", null, function () {',
  32122. ' this.$init = function () {',
  32123. ' };',
  32124. ' this.$final = function () {',
  32125. ' };',
  32126. ' this.Run = async function (w) {',
  32127. ' var Result = 0;',
  32128. ' return Result;',
  32129. ' };',
  32130. '});',
  32131. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  32132. ' this.Run = async function (w) {',
  32133. ' var Result = 0;',
  32134. ' var p = null;',
  32135. ' p = $mod.TObject.Run.apply(this, arguments);',
  32136. ' p = $mod.TObject.Run.call(this, 3);',
  32137. ' p = $mod.TObject.Run.call(this, 3);',
  32138. ' p = $mod.TObject.Run.call(this, 4);',
  32139. ' return p;',
  32140. ' return $mod.TObject.Run.apply(this, arguments);',
  32141. ' return $mod.TObject.Run.call(this, 3);',
  32142. ' return $mod.TObject.Run.call(this, 5);',
  32143. ' return 6;',
  32144. ' return Result;',
  32145. ' };',
  32146. '});',
  32147. '']),
  32148. LinesToStr([
  32149. '']));
  32150. CheckResolverUnexpectedHints();
  32151. end;
  32152. procedure TTestModule.TestAsync_ClassInterface;
  32153. begin
  32154. StartProgram(false);
  32155. Add([
  32156. '{$mode objfpc}',
  32157. '{$modeswitch externalclass}',
  32158. 'type',
  32159. ' TJSPromise = class external name ''Promise''',
  32160. ' end;',
  32161. ' IUnknown = interface',
  32162. ' function _AddRef: longint;',
  32163. ' function _Release: longint;',
  32164. ' end;',
  32165. 'function Say(i: IUnknown): IUnknown; async;',
  32166. 'begin',
  32167. 'end;',
  32168. 'function Run: IUnknown; async;',
  32169. 'begin',
  32170. ' Result:=await(Run);',
  32171. ' Result:=await(Run());',
  32172. ' Result:=await(Run) as IUnknown;',
  32173. ' Result:=await(Say(nil));',
  32174. ' Result:=await(Say(await(Run())));',
  32175. ' Result:=await(Say(await(Run()) as IUnknown));',
  32176. ' Result:=await(Say(await(Run()) as IUnknown)) as IUnknown;',
  32177. 'end;',
  32178. 'procedure Fly;',
  32179. 'var p: TJSPromise;',
  32180. 'begin',
  32181. ' Run;',
  32182. ' Run();',
  32183. ' p:=Run;',
  32184. ' p:=Run();',
  32185. 'end;',
  32186. 'begin',
  32187. ' ']);
  32188. ConvertProgram;
  32189. CheckSource('TestAsync_ClassInterface',
  32190. LinesToStr([ // statements
  32191. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  32192. 'this.Say = async function (i) {',
  32193. ' var Result = null;',
  32194. ' return Result;',
  32195. '};',
  32196. 'this.Run = async function () {',
  32197. ' var Result = null;',
  32198. ' var $ok = false;',
  32199. ' try {',
  32200. ' Result = rtl.setIntfL(Result, await $mod.Run());',
  32201. ' Result = rtl.setIntfL(Result, await $mod.Run());',
  32202. ' Result = rtl.setIntfL(Result, rtl.intfAsIntfT(await $mod.Run(), $mod.IUnknown));',
  32203. ' Result = rtl.setIntfL(Result, await $mod.Say(null));',
  32204. ' Result = rtl.setIntfL(Result, await $mod.Say(await $mod.Run()));',
  32205. ' Result = rtl.setIntfL(Result, await $mod.Say(rtl.intfAsIntfT(await $mod.Run(), $mod.IUnknown)));',
  32206. ' Result = rtl.setIntfL(Result, rtl.intfAsIntfT(await $mod.Say(rtl.intfAsIntfT(await $mod.Run(), $mod.IUnknown)), $mod.IUnknown));',
  32207. ' $ok = true;',
  32208. ' } finally {',
  32209. ' if (!$ok) rtl._Release(Result);',
  32210. ' };',
  32211. ' return Result;',
  32212. '};',
  32213. 'this.Fly = function () {',
  32214. ' var p = null;',
  32215. ' $mod.Run();',
  32216. ' $mod.Run();',
  32217. ' p = $mod.Run();',
  32218. ' p = $mod.Run();',
  32219. '};',
  32220. '']),
  32221. LinesToStr([
  32222. '']));
  32223. CheckResolverUnexpectedHints();
  32224. end;
  32225. procedure TTestModule.TestAsync_ClassInterface_AsyncMissmatchFail;
  32226. begin
  32227. StartProgram(true,[supTInterfacedObject]);
  32228. Add([
  32229. '{$mode objfpc}',
  32230. '{$modeswitch externalclass}',
  32231. 'type',
  32232. ' TJSPromise = class external name ''Promise''',
  32233. ' end;',
  32234. ' IBird = interface',
  32235. ' procedure Run;',
  32236. ' end;',
  32237. ' TBird = class(TInterfacedObject,IBird)',
  32238. ' procedure Run; async;',
  32239. ' end;',
  32240. 'procedure TBird.Run;',
  32241. 'begin',
  32242. 'end;',
  32243. 'begin',
  32244. ' ']);
  32245. SetExpectedPasResolverError('procedure type modifier "async" mismatch',nXModifierMismatchY);
  32246. ConvertProgram;
  32247. end;
  32248. Initialization
  32249. RegisterTests([TTestModule]);
  32250. end.