tcmodules.pas 878 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702237032370423705237062370723708237092371023711237122371323714237152371623717237182371923720237212372223723237242372523726237272372823729237302373123732237332373423735237362373723738237392374023741237422374323744237452374623747237482374923750237512375223753237542375523756237572375823759237602376123762237632376423765237662376723768237692377023771237722377323774237752377623777237782377923780237812378223783237842378523786237872378823789237902379123792237932379423795237962379723798237992380023801238022380323804238052380623807238082380923810238112381223813238142381523816238172381823819238202382123822238232382423825238262382723828238292383023831238322383323834238352383623837238382383923840238412384223843238442384523846238472384823849238502385123852238532385423855238562385723858238592386023861238622386323864238652386623867238682386923870238712387223873238742387523876238772387823879238802388123882238832388423885238862388723888238892389023891238922389323894238952389623897238982389923900239012390223903239042390523906239072390823909239102391123912239132391423915239162391723918239192392023921239222392323924239252392623927239282392923930239312393223933239342393523936239372393823939239402394123942239432394423945239462394723948239492395023951239522395323954239552395623957239582395923960239612396223963239642396523966239672396823969239702397123972239732397423975239762397723978239792398023981239822398323984239852398623987239882398923990239912399223993239942399523996239972399823999240002400124002240032400424005240062400724008240092401024011240122401324014240152401624017240182401924020240212402224023240242402524026240272402824029240302403124032240332403424035240362403724038240392404024041240422404324044240452404624047240482404924050240512405224053240542405524056240572405824059240602406124062240632406424065240662406724068240692407024071240722407324074240752407624077240782407924080240812408224083240842408524086240872408824089240902409124092240932409424095240962409724098240992410024101241022410324104241052410624107241082410924110241112411224113241142411524116241172411824119241202412124122241232412424125241262412724128241292413024131241322413324134241352413624137241382413924140241412414224143241442414524146241472414824149241502415124152241532415424155241562415724158241592416024161241622416324164241652416624167241682416924170241712417224173241742417524176241772417824179241802418124182241832418424185241862418724188241892419024191241922419324194241952419624197241982419924200242012420224203242042420524206242072420824209242102421124212242132421424215242162421724218242192422024221242222422324224242252422624227242282422924230242312423224233242342423524236242372423824239242402424124242242432424424245242462424724248242492425024251242522425324254242552425624257242582425924260242612426224263242642426524266242672426824269242702427124272242732427424275242762427724278242792428024281242822428324284242852428624287242882428924290242912429224293242942429524296242972429824299243002430124302243032430424305243062430724308243092431024311243122431324314243152431624317243182431924320243212432224323243242432524326243272432824329243302433124332243332433424335243362433724338243392434024341243422434324344243452434624347243482434924350243512435224353243542435524356243572435824359243602436124362243632436424365243662436724368243692437024371243722437324374243752437624377243782437924380243812438224383243842438524386243872438824389243902439124392243932439424395243962439724398243992440024401244022440324404244052440624407244082440924410244112441224413244142441524416244172441824419244202442124422244232442424425244262442724428244292443024431244322443324434244352443624437244382443924440244412444224443244442444524446244472444824449244502445124452244532445424455244562445724458244592446024461244622446324464244652446624467244682446924470244712447224473244742447524476244772447824479244802448124482244832448424485244862448724488244892449024491244922449324494244952449624497244982449924500245012450224503245042450524506245072450824509245102451124512245132451424515245162451724518245192452024521245222452324524245252452624527245282452924530245312453224533245342453524536245372453824539245402454124542245432454424545245462454724548245492455024551245522455324554245552455624557245582455924560245612456224563245642456524566245672456824569245702457124572245732457424575245762457724578245792458024581245822458324584245852458624587245882458924590245912459224593245942459524596245972459824599246002460124602246032460424605246062460724608246092461024611246122461324614246152461624617246182461924620246212462224623246242462524626246272462824629246302463124632246332463424635246362463724638246392464024641246422464324644246452464624647246482464924650246512465224653246542465524656246572465824659246602466124662246632466424665246662466724668246692467024671246722467324674246752467624677246782467924680246812468224683246842468524686246872468824689246902469124692246932469424695246962469724698246992470024701247022470324704247052470624707247082470924710247112471224713247142471524716247172471824719247202472124722247232472424725247262472724728247292473024731247322473324734247352473624737247382473924740247412474224743247442474524746247472474824749247502475124752247532475424755247562475724758247592476024761247622476324764247652476624767247682476924770247712477224773247742477524776247772477824779247802478124782247832478424785247862478724788247892479024791247922479324794247952479624797247982479924800248012480224803248042480524806248072480824809248102481124812248132481424815248162481724818248192482024821248222482324824248252482624827248282482924830248312483224833248342483524836248372483824839248402484124842248432484424845248462484724848248492485024851248522485324854248552485624857248582485924860248612486224863248642486524866248672486824869248702487124872248732487424875248762487724878248792488024881248822488324884248852488624887248882488924890248912489224893248942489524896248972489824899249002490124902249032490424905249062490724908249092491024911249122491324914249152491624917249182491924920249212492224923249242492524926249272492824929249302493124932249332493424935249362493724938249392494024941249422494324944249452494624947249482494924950249512495224953249542495524956249572495824959249602496124962249632496424965249662496724968249692497024971249722497324974249752497624977249782497924980249812498224983249842498524986249872498824989249902499124992249932499424995249962499724998249992500025001250022500325004250052500625007250082500925010250112501225013250142501525016250172501825019250202502125022250232502425025250262502725028250292503025031250322503325034250352503625037250382503925040250412504225043250442504525046250472504825049250502505125052250532505425055250562505725058250592506025061250622506325064250652506625067250682506925070250712507225073250742507525076250772507825079250802508125082250832508425085250862508725088250892509025091250922509325094250952509625097250982509925100251012510225103251042510525106251072510825109251102511125112251132511425115251162511725118251192512025121251222512325124251252512625127251282512925130251312513225133251342513525136251372513825139251402514125142251432514425145251462514725148251492515025151251522515325154251552515625157251582515925160251612516225163251642516525166251672516825169251702517125172251732517425175251762517725178251792518025181251822518325184251852518625187251882518925190251912519225193251942519525196251972519825199252002520125202252032520425205252062520725208252092521025211252122521325214252152521625217252182521925220252212522225223252242522525226252272522825229252302523125232252332523425235252362523725238252392524025241252422524325244252452524625247252482524925250252512525225253252542525525256252572525825259252602526125262252632526425265252662526725268252692527025271252722527325274252752527625277252782527925280252812528225283252842528525286252872528825289252902529125292252932529425295252962529725298252992530025301253022530325304253052530625307253082530925310253112531225313253142531525316253172531825319253202532125322253232532425325253262532725328253292533025331253322533325334253352533625337253382533925340253412534225343253442534525346253472534825349253502535125352253532535425355253562535725358253592536025361253622536325364253652536625367253682536925370253712537225373253742537525376253772537825379253802538125382253832538425385253862538725388253892539025391253922539325394253952539625397253982539925400254012540225403254042540525406254072540825409254102541125412254132541425415254162541725418254192542025421254222542325424254252542625427254282542925430254312543225433254342543525436254372543825439254402544125442254432544425445254462544725448254492545025451254522545325454254552545625457254582545925460254612546225463254642546525466254672546825469254702547125472254732547425475254762547725478254792548025481254822548325484254852548625487254882548925490254912549225493254942549525496254972549825499255002550125502255032550425505255062550725508255092551025511255122551325514255152551625517255182551925520255212552225523255242552525526255272552825529255302553125532255332553425535255362553725538255392554025541255422554325544255452554625547255482554925550255512555225553255542555525556255572555825559255602556125562255632556425565255662556725568255692557025571255722557325574255752557625577255782557925580255812558225583255842558525586255872558825589255902559125592255932559425595255962559725598255992560025601256022560325604256052560625607256082560925610256112561225613256142561525616256172561825619256202562125622256232562425625256262562725628256292563025631256322563325634256352563625637256382563925640256412564225643256442564525646256472564825649256502565125652256532565425655256562565725658256592566025661256622566325664256652566625667256682566925670256712567225673256742567525676256772567825679256802568125682256832568425685256862568725688256892569025691256922569325694256952569625697256982569925700257012570225703257042570525706257072570825709257102571125712257132571425715257162571725718257192572025721257222572325724257252572625727257282572925730257312573225733257342573525736257372573825739257402574125742257432574425745257462574725748257492575025751257522575325754257552575625757257582575925760257612576225763257642576525766257672576825769257702577125772257732577425775257762577725778257792578025781257822578325784257852578625787257882578925790257912579225793257942579525796257972579825799258002580125802258032580425805258062580725808258092581025811258122581325814258152581625817258182581925820258212582225823258242582525826258272582825829258302583125832258332583425835258362583725838258392584025841258422584325844258452584625847258482584925850258512585225853258542585525856258572585825859258602586125862258632586425865258662586725868258692587025871258722587325874258752587625877258782587925880258812588225883258842588525886258872588825889258902589125892258932589425895258962589725898258992590025901259022590325904259052590625907259082590925910259112591225913259142591525916259172591825919259202592125922259232592425925259262592725928259292593025931259322593325934259352593625937259382593925940259412594225943259442594525946259472594825949259502595125952259532595425955259562595725958259592596025961259622596325964259652596625967259682596925970259712597225973259742597525976259772597825979259802598125982259832598425985259862598725988259892599025991259922599325994259952599625997259982599926000260012600226003260042600526006260072600826009260102601126012260132601426015260162601726018260192602026021260222602326024260252602626027260282602926030260312603226033260342603526036260372603826039260402604126042260432604426045260462604726048260492605026051260522605326054260552605626057260582605926060260612606226063260642606526066260672606826069260702607126072260732607426075260762607726078260792608026081260822608326084260852608626087260882608926090260912609226093260942609526096260972609826099261002610126102261032610426105261062610726108261092611026111261122611326114261152611626117261182611926120261212612226123261242612526126261272612826129261302613126132261332613426135261362613726138261392614026141261422614326144261452614626147261482614926150261512615226153261542615526156261572615826159261602616126162261632616426165261662616726168261692617026171261722617326174261752617626177261782617926180261812618226183261842618526186261872618826189261902619126192261932619426195261962619726198261992620026201262022620326204262052620626207262082620926210262112621226213262142621526216262172621826219262202622126222262232622426225262262622726228262292623026231262322623326234262352623626237262382623926240262412624226243262442624526246262472624826249262502625126252262532625426255262562625726258262592626026261262622626326264262652626626267262682626926270262712627226273262742627526276262772627826279262802628126282262832628426285262862628726288262892629026291262922629326294262952629626297262982629926300263012630226303263042630526306263072630826309263102631126312263132631426315263162631726318263192632026321263222632326324263252632626327263282632926330263312633226333263342633526336263372633826339263402634126342263432634426345263462634726348263492635026351263522635326354263552635626357263582635926360263612636226363263642636526366263672636826369263702637126372263732637426375263762637726378263792638026381263822638326384263852638626387263882638926390263912639226393263942639526396263972639826399264002640126402264032640426405264062640726408264092641026411264122641326414264152641626417264182641926420264212642226423264242642526426264272642826429264302643126432264332643426435264362643726438264392644026441264422644326444264452644626447264482644926450264512645226453264542645526456264572645826459264602646126462264632646426465264662646726468264692647026471264722647326474264752647626477264782647926480264812648226483264842648526486264872648826489264902649126492264932649426495264962649726498264992650026501265022650326504265052650626507265082650926510265112651226513265142651526516265172651826519265202652126522265232652426525265262652726528265292653026531265322653326534265352653626537265382653926540265412654226543265442654526546265472654826549265502655126552265532655426555265562655726558265592656026561265622656326564265652656626567265682656926570265712657226573265742657526576265772657826579265802658126582265832658426585265862658726588265892659026591265922659326594265952659626597265982659926600266012660226603266042660526606266072660826609266102661126612266132661426615266162661726618266192662026621266222662326624266252662626627266282662926630266312663226633266342663526636266372663826639266402664126642266432664426645266462664726648266492665026651266522665326654266552665626657266582665926660266612666226663266642666526666266672666826669266702667126672266732667426675266762667726678266792668026681266822668326684266852668626687266882668926690266912669226693266942669526696266972669826699267002670126702267032670426705267062670726708267092671026711267122671326714267152671626717267182671926720267212672226723267242672526726267272672826729267302673126732267332673426735267362673726738267392674026741267422674326744267452674626747267482674926750267512675226753267542675526756267572675826759267602676126762267632676426765267662676726768267692677026771267722677326774267752677626777267782677926780267812678226783267842678526786267872678826789267902679126792267932679426795267962679726798267992680026801268022680326804268052680626807268082680926810268112681226813268142681526816268172681826819268202682126822268232682426825268262682726828268292683026831268322683326834268352683626837268382683926840268412684226843268442684526846268472684826849268502685126852268532685426855268562685726858268592686026861268622686326864268652686626867268682686926870268712687226873268742687526876268772687826879268802688126882268832688426885268862688726888268892689026891268922689326894268952689626897268982689926900269012690226903269042690526906269072690826909269102691126912269132691426915269162691726918269192692026921269222692326924269252692626927269282692926930269312693226933269342693526936269372693826939269402694126942269432694426945269462694726948269492695026951269522695326954269552695626957269582695926960269612696226963269642696526966269672696826969269702697126972269732697426975269762697726978269792698026981269822698326984269852698626987269882698926990269912699226993269942699526996269972699826999270002700127002270032700427005270062700727008270092701027011270122701327014270152701627017270182701927020270212702227023270242702527026270272702827029270302703127032270332703427035270362703727038270392704027041270422704327044270452704627047270482704927050270512705227053270542705527056270572705827059270602706127062270632706427065270662706727068270692707027071270722707327074270752707627077270782707927080270812708227083270842708527086270872708827089270902709127092270932709427095270962709727098270992710027101271022710327104271052710627107271082710927110271112711227113271142711527116271172711827119271202712127122271232712427125271262712727128271292713027131271322713327134271352713627137271382713927140271412714227143271442714527146271472714827149271502715127152271532715427155271562715727158271592716027161271622716327164271652716627167271682716927170271712717227173271742717527176271772717827179271802718127182271832718427185271862718727188271892719027191271922719327194271952719627197271982719927200272012720227203272042720527206272072720827209272102721127212272132721427215272162721727218272192722027221272222722327224272252722627227272282722927230272312723227233272342723527236272372723827239272402724127242272432724427245272462724727248272492725027251272522725327254272552725627257272582725927260272612726227263272642726527266272672726827269272702727127272272732727427275272762727727278272792728027281272822728327284272852728627287272882728927290272912729227293272942729527296272972729827299273002730127302273032730427305273062730727308273092731027311273122731327314273152731627317273182731927320273212732227323273242732527326273272732827329273302733127332273332733427335273362733727338273392734027341273422734327344273452734627347273482734927350273512735227353273542735527356273572735827359273602736127362273632736427365273662736727368273692737027371273722737327374273752737627377273782737927380273812738227383273842738527386273872738827389273902739127392273932739427395273962739727398273992740027401274022740327404274052740627407274082740927410274112741227413274142741527416274172741827419274202742127422274232742427425274262742727428274292743027431274322743327434274352743627437274382743927440274412744227443274442744527446274472744827449274502745127452274532745427455274562745727458274592746027461274622746327464274652746627467274682746927470274712747227473274742747527476274772747827479274802748127482274832748427485274862748727488274892749027491274922749327494274952749627497274982749927500275012750227503275042750527506275072750827509275102751127512275132751427515275162751727518275192752027521275222752327524275252752627527275282752927530275312753227533275342753527536275372753827539275402754127542275432754427545275462754727548275492755027551275522755327554275552755627557275582755927560275612756227563275642756527566275672756827569275702757127572275732757427575275762757727578275792758027581275822758327584275852758627587275882758927590275912759227593275942759527596275972759827599276002760127602276032760427605276062760727608276092761027611276122761327614276152761627617276182761927620276212762227623276242762527626276272762827629276302763127632276332763427635276362763727638276392764027641276422764327644276452764627647276482764927650276512765227653276542765527656276572765827659276602766127662276632766427665276662766727668276692767027671276722767327674276752767627677276782767927680276812768227683276842768527686276872768827689276902769127692276932769427695276962769727698276992770027701277022770327704277052770627707277082770927710277112771227713277142771527716277172771827719277202772127722277232772427725277262772727728277292773027731277322773327734277352773627737277382773927740277412774227743277442774527746277472774827749277502775127752277532775427755277562775727758277592776027761277622776327764277652776627767277682776927770277712777227773277742777527776277772777827779277802778127782277832778427785277862778727788277892779027791277922779327794277952779627797277982779927800278012780227803278042780527806278072780827809278102781127812278132781427815278162781727818278192782027821278222782327824278252782627827278282782927830278312783227833278342783527836278372783827839278402784127842278432784427845278462784727848278492785027851278522785327854278552785627857278582785927860278612786227863278642786527866278672786827869278702787127872278732787427875278762787727878278792788027881278822788327884278852788627887278882788927890278912789227893278942789527896278972789827899279002790127902279032790427905279062790727908279092791027911279122791327914279152791627917279182791927920279212792227923279242792527926279272792827929279302793127932279332793427935279362793727938279392794027941279422794327944279452794627947279482794927950279512795227953279542795527956279572795827959279602796127962279632796427965279662796727968279692797027971279722797327974279752797627977279782797927980279812798227983279842798527986279872798827989279902799127992279932799427995279962799727998279992800028001280022800328004280052800628007280082800928010280112801228013280142801528016280172801828019280202802128022280232802428025280262802728028280292803028031280322803328034280352803628037280382803928040280412804228043280442804528046280472804828049280502805128052280532805428055280562805728058280592806028061280622806328064280652806628067280682806928070280712807228073280742807528076280772807828079280802808128082280832808428085280862808728088280892809028091280922809328094280952809628097280982809928100281012810228103281042810528106281072810828109281102811128112281132811428115281162811728118281192812028121281222812328124281252812628127281282812928130281312813228133281342813528136281372813828139281402814128142281432814428145281462814728148281492815028151281522815328154281552815628157281582815928160281612816228163281642816528166281672816828169281702817128172281732817428175281762817728178281792818028181281822818328184281852818628187281882818928190281912819228193281942819528196281972819828199282002820128202282032820428205282062820728208282092821028211282122821328214282152821628217282182821928220282212822228223282242822528226282272822828229282302823128232282332823428235282362823728238282392824028241282422824328244282452824628247282482824928250282512825228253282542825528256282572825828259282602826128262282632826428265282662826728268282692827028271282722827328274282752827628277282782827928280282812828228283282842828528286282872828828289282902829128292282932829428295282962829728298282992830028301283022830328304283052830628307283082830928310283112831228313283142831528316283172831828319283202832128322283232832428325283262832728328283292833028331283322833328334283352833628337283382833928340283412834228343283442834528346283472834828349283502835128352283532835428355283562835728358283592836028361283622836328364283652836628367283682836928370283712837228373283742837528376283772837828379283802838128382283832838428385283862838728388283892839028391283922839328394283952839628397283982839928400284012840228403284042840528406284072840828409284102841128412284132841428415284162841728418284192842028421284222842328424284252842628427284282842928430284312843228433284342843528436284372843828439284402844128442284432844428445284462844728448284492845028451284522845328454284552845628457284582845928460284612846228463284642846528466284672846828469284702847128472284732847428475284762847728478284792848028481284822848328484284852848628487284882848928490284912849228493284942849528496284972849828499285002850128502285032850428505285062850728508285092851028511285122851328514285152851628517285182851928520285212852228523285242852528526285272852828529285302853128532285332853428535285362853728538285392854028541285422854328544285452854628547285482854928550285512855228553285542855528556285572855828559285602856128562285632856428565285662856728568285692857028571285722857328574285752857628577285782857928580285812858228583285842858528586285872858828589285902859128592285932859428595285962859728598285992860028601286022860328604286052860628607286082860928610286112861228613286142861528616286172861828619286202862128622286232862428625286262862728628286292863028631286322863328634286352863628637286382863928640286412864228643286442864528646286472864828649286502865128652286532865428655286562865728658286592866028661286622866328664286652866628667286682866928670286712867228673286742867528676286772867828679286802868128682286832868428685286862868728688286892869028691286922869328694286952869628697286982869928700287012870228703287042870528706287072870828709287102871128712287132871428715287162871728718287192872028721287222872328724287252872628727287282872928730287312873228733287342873528736287372873828739287402874128742287432874428745287462874728748287492875028751287522875328754287552875628757287582875928760287612876228763287642876528766287672876828769287702877128772287732877428775287762877728778287792878028781287822878328784287852878628787287882878928790287912879228793287942879528796287972879828799288002880128802288032880428805288062880728808288092881028811288122881328814288152881628817288182881928820288212882228823288242882528826288272882828829288302883128832288332883428835288362883728838288392884028841288422884328844288452884628847288482884928850288512885228853288542885528856288572885828859288602886128862288632886428865288662886728868288692887028871288722887328874288752887628877288782887928880288812888228883288842888528886288872888828889288902889128892288932889428895288962889728898288992890028901289022890328904289052890628907289082890928910289112891228913289142891528916289172891828919289202892128922289232892428925289262892728928289292893028931289322893328934289352893628937289382893928940289412894228943289442894528946289472894828949289502895128952289532895428955289562895728958289592896028961289622896328964289652896628967289682896928970289712897228973289742897528976289772897828979289802898128982289832898428985289862898728988289892899028991289922899328994289952899628997289982899929000290012900229003290042900529006290072900829009290102901129012290132901429015290162901729018290192902029021290222902329024290252902629027290282902929030290312903229033290342903529036290372903829039290402904129042290432904429045290462904729048290492905029051290522905329054290552905629057290582905929060290612906229063290642906529066290672906829069290702907129072290732907429075290762907729078290792908029081290822908329084290852908629087290882908929090290912909229093290942909529096290972909829099291002910129102291032910429105291062910729108291092911029111291122911329114291152911629117291182911929120291212912229123291242912529126291272912829129291302913129132291332913429135291362913729138291392914029141291422914329144291452914629147291482914929150291512915229153291542915529156291572915829159291602916129162291632916429165291662916729168291692917029171291722917329174291752917629177291782917929180291812918229183291842918529186291872918829189291902919129192291932919429195291962919729198291992920029201292022920329204292052920629207292082920929210292112921229213292142921529216292172921829219292202922129222292232922429225292262922729228292292923029231292322923329234292352923629237292382923929240292412924229243292442924529246292472924829249292502925129252292532925429255292562925729258292592926029261292622926329264292652926629267292682926929270292712927229273292742927529276292772927829279292802928129282292832928429285292862928729288292892929029291292922929329294292952929629297292982929929300293012930229303293042930529306293072930829309293102931129312293132931429315293162931729318293192932029321293222932329324293252932629327293282932929330293312933229333293342933529336293372933829339293402934129342293432934429345293462934729348293492935029351293522935329354293552935629357293582935929360293612936229363293642936529366293672936829369293702937129372293732937429375293762937729378293792938029381293822938329384293852938629387293882938929390293912939229393293942939529396293972939829399294002940129402294032940429405294062940729408294092941029411294122941329414294152941629417294182941929420294212942229423294242942529426294272942829429294302943129432294332943429435294362943729438294392944029441294422944329444294452944629447294482944929450294512945229453294542945529456294572945829459294602946129462294632946429465294662946729468294692947029471294722947329474294752947629477294782947929480294812948229483294842948529486294872948829489294902949129492294932949429495294962949729498294992950029501295022950329504295052950629507295082950929510295112951229513295142951529516295172951829519295202952129522295232952429525295262952729528295292953029531295322953329534295352953629537295382953929540295412954229543295442954529546295472954829549295502955129552295532955429555295562955729558295592956029561295622956329564295652956629567295682956929570295712957229573295742957529576295772957829579295802958129582295832958429585295862958729588295892959029591295922959329594295952959629597295982959929600296012960229603296042960529606296072960829609296102961129612296132961429615296162961729618296192962029621296222962329624296252962629627296282962929630296312963229633296342963529636296372963829639296402964129642296432964429645296462964729648296492965029651296522965329654296552965629657296582965929660296612966229663296642966529666296672966829669296702967129672296732967429675296762967729678296792968029681296822968329684296852968629687296882968929690296912969229693296942969529696296972969829699297002970129702297032970429705297062970729708297092971029711297122971329714297152971629717297182971929720297212972229723297242972529726297272972829729297302973129732297332973429735297362973729738297392974029741297422974329744297452974629747297482974929750297512975229753297542975529756297572975829759297602976129762297632976429765297662976729768297692977029771297722977329774297752977629777297782977929780297812978229783297842978529786297872978829789297902979129792297932979429795297962979729798297992980029801298022980329804298052980629807298082980929810298112981229813298142981529816298172981829819298202982129822298232982429825298262982729828298292983029831298322983329834298352983629837298382983929840298412984229843298442984529846298472984829849298502985129852298532985429855298562985729858298592986029861298622986329864298652986629867298682986929870298712987229873298742987529876298772987829879298802988129882298832988429885298862988729888298892989029891298922989329894298952989629897298982989929900299012990229903299042990529906299072990829909299102991129912299132991429915299162991729918299192992029921299222992329924299252992629927299282992929930299312993229933299342993529936299372993829939299402994129942299432994429945299462994729948299492995029951299522995329954299552995629957299582995929960299612996229963299642996529966299672996829969299702997129972299732997429975299762997729978299792998029981299822998329984299852998629987299882998929990299912999229993299942999529996299972999829999300003000130002300033000430005300063000730008300093001030011300123001330014300153001630017300183001930020300213002230023300243002530026300273002830029300303003130032300333003430035300363003730038300393004030041300423004330044300453004630047300483004930050300513005230053300543005530056300573005830059300603006130062300633006430065300663006730068300693007030071300723007330074300753007630077300783007930080300813008230083300843008530086300873008830089300903009130092300933009430095300963009730098300993010030101301023010330104301053010630107301083010930110301113011230113301143011530116301173011830119301203012130122301233012430125301263012730128301293013030131301323013330134301353013630137301383013930140301413014230143301443014530146301473014830149301503015130152301533015430155301563015730158301593016030161301623016330164301653016630167301683016930170301713017230173301743017530176301773017830179301803018130182301833018430185301863018730188301893019030191301923019330194301953019630197301983019930200302013020230203302043020530206302073020830209302103021130212302133021430215302163021730218302193022030221302223022330224302253022630227302283022930230302313023230233302343023530236302373023830239302403024130242302433024430245302463024730248302493025030251302523025330254302553025630257302583025930260302613026230263302643026530266302673026830269302703027130272302733027430275302763027730278302793028030281302823028330284302853028630287302883028930290302913029230293302943029530296302973029830299303003030130302303033030430305303063030730308303093031030311303123031330314303153031630317303183031930320303213032230323303243032530326303273032830329303303033130332303333033430335303363033730338303393034030341303423034330344303453034630347303483034930350303513035230353303543035530356303573035830359303603036130362303633036430365303663036730368303693037030371303723037330374303753037630377303783037930380303813038230383303843038530386303873038830389303903039130392303933039430395303963039730398303993040030401304023040330404304053040630407304083040930410304113041230413304143041530416304173041830419304203042130422304233042430425304263042730428304293043030431304323043330434304353043630437304383043930440304413044230443304443044530446304473044830449304503045130452304533045430455304563045730458304593046030461304623046330464304653046630467304683046930470304713047230473304743047530476304773047830479304803048130482304833048430485304863048730488304893049030491304923049330494304953049630497304983049930500305013050230503305043050530506305073050830509305103051130512305133051430515305163051730518305193052030521305223052330524305253052630527305283052930530305313053230533305343053530536305373053830539305403054130542305433054430545305463054730548305493055030551305523055330554305553055630557305583055930560305613056230563305643056530566305673056830569305703057130572305733057430575305763057730578305793058030581305823058330584305853058630587305883058930590305913059230593305943059530596305973059830599306003060130602306033060430605306063060730608306093061030611306123061330614306153061630617306183061930620306213062230623306243062530626306273062830629306303063130632306333063430635306363063730638306393064030641306423064330644306453064630647306483064930650306513065230653306543065530656306573065830659306603066130662306633066430665306663066730668306693067030671306723067330674306753067630677306783067930680306813068230683306843068530686306873068830689306903069130692306933069430695306963069730698306993070030701307023070330704307053070630707307083070930710307113071230713307143071530716307173071830719307203072130722307233072430725307263072730728307293073030731307323073330734307353073630737307383073930740307413074230743307443074530746307473074830749307503075130752307533075430755307563075730758307593076030761307623076330764307653076630767307683076930770307713077230773307743077530776307773077830779307803078130782307833078430785307863078730788307893079030791307923079330794307953079630797307983079930800308013080230803308043080530806308073080830809308103081130812308133081430815308163081730818308193082030821308223082330824308253082630827308283082930830308313083230833308343083530836308373083830839308403084130842308433084430845308463084730848308493085030851308523085330854308553085630857308583085930860308613086230863308643086530866308673086830869308703087130872308733087430875308763087730878308793088030881308823088330884308853088630887308883088930890308913089230893308943089530896308973089830899309003090130902309033090430905309063090730908309093091030911309123091330914309153091630917309183091930920309213092230923309243092530926309273092830929309303093130932309333093430935309363093730938309393094030941309423094330944309453094630947309483094930950309513095230953309543095530956309573095830959309603096130962309633096430965309663096730968309693097030971309723097330974309753097630977309783097930980309813098230983309843098530986309873098830989309903099130992309933099430995309963099730998309993100031001310023100331004310053100631007310083100931010310113101231013310143101531016310173101831019310203102131022310233102431025310263102731028310293103031031310323103331034310353103631037310383103931040310413104231043310443104531046310473104831049310503105131052310533105431055310563105731058310593106031061310623106331064310653106631067310683106931070310713107231073310743107531076310773107831079310803108131082310833108431085310863108731088310893109031091310923109331094310953109631097310983109931100311013110231103311043110531106311073110831109311103111131112311133111431115311163111731118311193112031121311223112331124311253112631127311283112931130311313113231133311343113531136311373113831139311403114131142311433114431145311463114731148311493115031151311523115331154311553115631157311583115931160311613116231163311643116531166311673116831169311703117131172311733117431175311763117731178311793118031181311823118331184311853118631187311883118931190311913119231193311943119531196311973119831199312003120131202312033120431205312063120731208312093121031211312123121331214312153121631217312183121931220312213122231223312243122531226312273122831229312303123131232312333123431235312363123731238312393124031241312423124331244312453124631247312483124931250312513125231253312543125531256312573125831259312603126131262312633126431265312663126731268312693127031271312723127331274312753127631277312783127931280312813128231283312843128531286312873128831289312903129131292312933129431295312963129731298312993130031301313023130331304313053130631307313083130931310313113131231313313143131531316313173131831319313203132131322313233132431325313263132731328313293133031331313323133331334313353133631337313383133931340313413134231343313443134531346313473134831349313503135131352313533135431355313563135731358313593136031361313623136331364313653136631367313683136931370313713137231373313743137531376313773137831379313803138131382313833138431385313863138731388313893139031391313923139331394313953139631397313983139931400314013140231403314043140531406314073140831409314103141131412314133141431415314163141731418314193142031421314223142331424314253142631427314283142931430314313143231433314343143531436314373143831439314403144131442314433144431445314463144731448314493145031451314523145331454314553145631457314583145931460314613146231463314643146531466314673146831469314703147131472314733147431475314763147731478314793148031481314823148331484314853148631487314883148931490314913149231493314943149531496314973149831499315003150131502315033150431505315063150731508315093151031511315123151331514315153151631517315183151931520315213152231523315243152531526315273152831529315303153131532315333153431535315363153731538315393154031541315423154331544315453154631547315483154931550315513155231553315543155531556315573155831559315603156131562315633156431565315663156731568315693157031571315723157331574315753157631577315783157931580315813158231583315843158531586315873158831589315903159131592315933159431595315963159731598315993160031601316023160331604316053160631607316083160931610316113161231613316143161531616316173161831619316203162131622316233162431625316263162731628316293163031631316323163331634316353163631637316383163931640316413164231643316443164531646316473164831649316503165131652316533165431655316563165731658316593166031661316623166331664316653166631667316683166931670316713167231673316743167531676316773167831679316803168131682316833168431685316863168731688316893169031691316923169331694316953169631697316983169931700317013170231703317043170531706317073170831709317103171131712317133171431715317163171731718317193172031721317223172331724317253172631727317283172931730317313173231733317343173531736317373173831739317403174131742317433174431745317463174731748317493175031751317523175331754317553175631757317583175931760317613176231763317643176531766317673176831769317703177131772317733177431775317763177731778317793178031781317823178331784317853178631787317883178931790317913179231793317943179531796317973179831799318003180131802318033180431805318063180731808318093181031811318123181331814318153181631817318183181931820318213182231823318243182531826318273182831829318303183131832318333183431835318363183731838318393184031841318423184331844318453184631847318483184931850318513185231853318543185531856318573185831859318603186131862318633186431865318663186731868318693187031871318723187331874318753187631877318783187931880318813188231883318843188531886318873188831889318903189131892318933189431895318963189731898318993190031901319023190331904319053190631907319083190931910319113191231913319143191531916319173191831919319203192131922319233192431925319263192731928319293193031931319323193331934319353193631937319383193931940319413194231943319443194531946319473194831949319503195131952319533195431955319563195731958319593196031961319623196331964319653196631967319683196931970319713197231973319743197531976319773197831979319803198131982319833198431985319863198731988319893199031991319923199331994319953199631997319983199932000320013200232003320043200532006320073200832009320103201132012320133201432015320163201732018320193202032021320223202332024320253202632027320283202932030320313203232033320343203532036320373203832039320403204132042320433204432045320463204732048320493205032051320523205332054320553205632057320583205932060320613206232063320643206532066320673206832069320703207132072320733207432075320763207732078320793208032081320823208332084320853208632087320883208932090320913209232093320943209532096320973209832099321003210132102321033210432105321063210732108321093211032111321123211332114321153211632117321183211932120321213212232123321243212532126321273212832129321303213132132321333213432135321363213732138321393214032141321423214332144321453214632147321483214932150321513215232153321543215532156321573215832159321603216132162321633216432165321663216732168321693217032171321723217332174321753217632177321783217932180321813218232183321843218532186321873218832189321903219132192321933219432195321963219732198321993220032201322023220332204322053220632207322083220932210322113221232213322143221532216322173221832219322203222132222322233222432225322263222732228322293223032231322323223332234322353223632237322383223932240322413224232243322443224532246322473224832249322503225132252322533225432255322563225732258322593226032261322623226332264322653226632267322683226932270322713227232273322743227532276322773227832279322803228132282322833228432285322863228732288322893229032291322923229332294322953229632297322983229932300323013230232303323043230532306323073230832309323103231132312323133231432315323163231732318323193232032321323223232332324323253232632327323283232932330323313233232333323343233532336323373233832339323403234132342323433234432345323463234732348323493235032351323523235332354323553235632357323583235932360323613236232363323643236532366323673236832369323703237132372323733237432375323763237732378323793238032381323823238332384323853238632387323883238932390323913239232393323943239532396323973239832399324003240132402324033240432405324063240732408324093241032411324123241332414324153241632417324183241932420324213242232423324243242532426324273242832429324303243132432324333243432435324363243732438324393244032441324423244332444324453244632447324483244932450324513245232453324543245532456324573245832459324603246132462324633246432465324663246732468324693247032471324723247332474324753247632477324783247932480324813248232483324843248532486324873248832489324903249132492324933249432495324963249732498324993250032501325023250332504325053250632507325083250932510325113251232513325143251532516325173251832519325203252132522325233252432525325263252732528325293253032531325323253332534325353253632537325383253932540325413254232543325443254532546325473254832549325503255132552325533255432555325563255732558325593256032561325623256332564325653256632567325683256932570325713257232573325743257532576325773257832579325803258132582325833258432585325863258732588325893259032591325923259332594325953259632597325983259932600326013260232603326043260532606326073260832609326103261132612326133261432615326163261732618326193262032621326223262332624326253262632627326283262932630326313263232633326343263532636326373263832639326403264132642326433264432645326463264732648326493265032651326523265332654326553265632657326583265932660326613266232663326643266532666326673266832669326703267132672326733267432675326763267732678326793268032681326823268332684326853268632687326883268932690326913269232693326943269532696326973269832699327003270132702327033270432705327063270732708327093271032711327123271332714327153271632717327183271932720327213272232723327243272532726327273272832729327303273132732327333273432735327363273732738327393274032741327423274332744327453274632747327483274932750327513275232753327543275532756327573275832759327603276132762327633276432765327663276732768327693277032771327723277332774327753277632777327783277932780327813278232783327843278532786327873278832789327903279132792327933279432795327963279732798327993280032801328023280332804328053280632807328083280932810328113281232813328143281532816328173281832819328203282132822328233282432825328263282732828328293283032831328323283332834328353283632837328383283932840328413284232843328443284532846328473284832849328503285132852328533285432855328563285732858328593286032861328623286332864328653286632867328683286932870328713287232873328743287532876328773287832879328803288132882328833288432885328863288732888328893289032891328923289332894328953289632897328983289932900329013290232903329043290532906329073290832909329103291132912329133291432915329163291732918329193292032921329223292332924329253292632927329283292932930329313293232933329343293532936329373293832939329403294132942329433294432945329463294732948329493295032951329523295332954329553295632957329583295932960329613296232963329643296532966329673296832969329703297132972329733297432975329763297732978329793298032981329823298332984329853298632987329883298932990329913299232993329943299532996329973299832999330003300133002330033300433005330063300733008330093301033011330123301333014330153301633017330183301933020330213302233023330243302533026330273302833029330303303133032330333303433035330363303733038330393304033041330423304333044330453304633047330483304933050330513305233053330543305533056330573305833059330603306133062330633306433065330663306733068330693307033071330723307333074330753307633077330783307933080330813308233083330843308533086330873308833089330903309133092330933309433095330963309733098330993310033101331023310333104331053310633107331083310933110331113311233113331143311533116331173311833119331203312133122331233312433125331263312733128331293313033131331323313333134331353313633137331383313933140331413314233143331443314533146331473314833149331503315133152331533315433155331563315733158331593316033161331623316333164331653316633167331683316933170331713317233173331743317533176331773317833179331803318133182331833318433185331863318733188331893319033191331923319333194331953319633197331983319933200332013320233203332043320533206332073320833209332103321133212332133321433215332163321733218332193322033221332223322333224332253322633227332283322933230332313323233233332343323533236332373323833239332403324133242332433324433245332463324733248332493325033251332523325333254332553325633257332583325933260332613326233263332643326533266332673326833269332703327133272332733327433275332763327733278332793328033281332823328333284332853328633287332883328933290332913329233293332943329533296332973329833299333003330133302333033330433305333063330733308333093331033311333123331333314333153331633317333183331933320333213332233323333243332533326333273332833329333303333133332333333333433335333363333733338333393334033341333423334333344333453334633347333483334933350333513335233353333543335533356333573335833359333603336133362333633336433365333663336733368333693337033371333723337333374333753337633377333783337933380333813338233383333843338533386333873338833389333903339133392333933339433395333963339733398333993340033401334023340333404334053340633407334083340933410334113341233413334143341533416334173341833419334203342133422334233342433425334263342733428334293343033431334323343333434334353343633437334383343933440334413344233443334443344533446334473344833449334503345133452334533345433455334563345733458334593346033461334623346333464334653346633467334683346933470334713347233473334743347533476334773347833479334803348133482334833348433485334863348733488334893349033491
  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_AsmSubBlock;
  309. Procedure TestProc_Assembler;
  310. Procedure TestProc_VarParam;
  311. Procedure TestProc_VarParamString;
  312. Procedure TestProc_VarParamV;
  313. Procedure TestProc_Overload;
  314. Procedure TestProc_OverloadForward;
  315. Procedure TestProc_OverloadIntfImpl;
  316. Procedure TestProc_OverloadNested;
  317. Procedure TestProc_OverloadNestedForward;
  318. Procedure TestProc_OverloadUnitCycle;
  319. Procedure TestProc_Varargs;
  320. Procedure TestProc_ConstOrder;
  321. Procedure TestProc_DuplicateConst;
  322. Procedure TestProc_LocalVarAbsolute;
  323. Procedure TestProc_LocalVarInit;
  324. Procedure TestProc_ReservedWords;
  325. Procedure TestProc_ConstRefWord;
  326. // anonymous functions
  327. Procedure TestAnonymousProc_Assign_ObjFPC;
  328. Procedure TestAnonymousProc_Assign_Delphi;
  329. Procedure TestAnonymousProc_Arg;
  330. Procedure TestAnonymousProc_Typecast;
  331. Procedure TestAnonymousProc_With;
  332. Procedure TestAnonymousProc_ExceptOn;
  333. Procedure TestAnonymousProc_Nested;
  334. Procedure TestAnonymousProc_NestedAssignResult;
  335. Procedure TestAnonymousProc_Class;
  336. Procedure TestAnonymousProc_ForLoop;
  337. Procedure TestAnonymousProc_AsmDelphi;
  338. // enums, sets
  339. Procedure TestEnum_Name;
  340. Procedure TestEnum_Number;
  341. Procedure TestEnum_ConstFail;
  342. Procedure TestEnum_Functions;
  343. Procedure TestEnumRg_Functions;
  344. Procedure TestEnum_AsParams;
  345. Procedure TestEnumRange_Array;
  346. Procedure TestEnum_ForIn;
  347. Procedure TestEnum_ScopedNumber;
  348. Procedure TestEnum_InFunction;
  349. Procedure TestEnum_Name_Anonymous_Unit;
  350. Procedure TestSet_Enum;
  351. Procedure TestSet_Operators;
  352. Procedure TestSet_Operator_In;
  353. Procedure TestSet_Functions;
  354. Procedure TestSet_PassAsArgClone;
  355. Procedure TestSet_AsParams;
  356. Procedure TestSet_Property;
  357. Procedure TestSet_EnumConst;
  358. Procedure TestSet_IntConst;
  359. Procedure TestSet_IntRange;
  360. Procedure TestSet_AnonymousEnumType;
  361. Procedure TestSet_AnonymousEnumTypeChar; // ToDo
  362. Procedure TestSet_ConstEnum;
  363. Procedure TestSet_ConstChar;
  364. Procedure TestSet_ConstInt;
  365. Procedure TestSet_InFunction;
  366. Procedure TestSet_ForIn;
  367. // statements
  368. Procedure TestNestBegin;
  369. Procedure TestIncDec;
  370. Procedure TestLoHiFpcMode;
  371. Procedure TestLoHiDelphiMode;
  372. Procedure TestAssignments;
  373. Procedure TestArithmeticOperators1;
  374. Procedure TestMultiAdd;
  375. Procedure TestLogicalOperators;
  376. Procedure TestBitwiseOperators;
  377. Procedure TestBitwiseOperatorsLongword;
  378. Procedure TestFunctionInt;
  379. Procedure TestFunctionString;
  380. Procedure TestIfThen;
  381. Procedure TestForLoop;
  382. Procedure TestForLoopInsideFunction;
  383. Procedure TestForLoop_ReadVarAfter;
  384. Procedure TestForLoop_Nested;
  385. Procedure TestRepeatUntil;
  386. Procedure TestAsmBlock;
  387. Procedure TestAsmPas_Impl; // ToDo
  388. Procedure TestTryFinally;
  389. Procedure TestTryExcept;
  390. Procedure TestTryExcept_ReservedWords;
  391. Procedure TestIfThenRaiseElse;
  392. Procedure TestCaseOf;
  393. Procedure TestCaseOf_UseSwitch;
  394. Procedure TestCaseOfNoElse;
  395. Procedure TestCaseOfNoElse_UseSwitch;
  396. Procedure TestCaseOfRange;
  397. Procedure TestCaseOfString;
  398. Procedure TestCaseOfChar;
  399. Procedure TestCaseOfExternalClassConst;
  400. Procedure TestDebugger;
  401. // arrays
  402. Procedure TestArray_Dynamic;
  403. Procedure TestArray_Dynamic_Nil;
  404. Procedure TestArray_DynMultiDimensional;
  405. Procedure TestArray_DynamicAssign;
  406. Procedure TestArray_StaticInt;
  407. Procedure TestArray_StaticBool;
  408. Procedure TestArray_StaticChar;
  409. Procedure TestArray_StaticMultiDim;
  410. Procedure TestArray_StaticInFunction;
  411. Procedure TestArray_StaticMultiDimEqualNotImplemented;
  412. Procedure TestArrayOfRecord;
  413. Procedure TestArray_StaticRecord;
  414. Procedure TestArrayOfSet;
  415. Procedure TestArray_DynAsParam;
  416. Procedure TestArray_StaticAsParam;
  417. Procedure TestArrayElement_AsParams;
  418. Procedure TestArrayElementFromFuncResult_AsParams;
  419. Procedure TestArrayEnumTypeRange;
  420. Procedure TestArray_SetLengthOutArg;
  421. Procedure TestArray_SetLengthProperty;
  422. Procedure TestArray_SetLengthMultiDim;
  423. Procedure TestArray_SetLengthDynOfStatic;
  424. Procedure TestArray_OpenArrayOfString;
  425. Procedure TestArray_ArrayOfCharAssignString; // ToDo
  426. Procedure TestArray_ConstRef;
  427. Procedure TestArray_Concat;
  428. Procedure TestArray_Copy;
  429. Procedure TestArray_InsertDelete;
  430. Procedure TestArray_DynArrayConstObjFPC;
  431. Procedure TestArray_DynArrayConstDelphi;
  432. Procedure TestArray_ArrayLitAsParam;
  433. Procedure TestArray_ArrayLitMultiDimAsParam;
  434. Procedure TestArray_ArrayLitStaticAsParam;
  435. Procedure TestArray_ForInArrOfString;
  436. Procedure TestExternalClass_TypeCastArrayToExternalClass;
  437. Procedure TestExternalClass_TypeCastArrayFromExternalClass;
  438. Procedure TestArrayOfConst_TVarRec;
  439. Procedure TestArrayOfConst_PassBaseTypes;
  440. Procedure TestArrayOfConst_PassObj;
  441. // record
  442. Procedure TestRecord_Empty;
  443. Procedure TestRecord_Var;
  444. Procedure TestRecord_VarExternal;
  445. Procedure TestRecord_WithDo;
  446. Procedure TestRecord_Assign;
  447. Procedure TestRecord_AsParams;
  448. Procedure TestRecord_ConstRef;
  449. Procedure TestRecordElement_AsParams;
  450. Procedure TestRecordElementFromFuncResult_AsParams;
  451. Procedure TestRecordElementFromWith_AsParams;
  452. Procedure TestRecord_Equal;
  453. Procedure TestRecord_JSValue;
  454. Procedure TestRecord_VariantFail;
  455. Procedure TestRecord_FieldArray;
  456. Procedure TestRecord_Const;
  457. Procedure TestRecord_TypecastFail;
  458. Procedure TestRecord_InFunction;
  459. Procedure TestRecord_AnonymousFail;
  460. // advanced record
  461. Procedure TestAdvRecord_Function;
  462. Procedure TestAdvRecord_Property;
  463. Procedure TestAdvRecord_PropertyDefault;
  464. Procedure TestAdvRecord_Property_ClassMethod;
  465. Procedure TestAdvRecord_Const;
  466. Procedure TestAdvRecord_ExternalField;
  467. Procedure TestAdvRecord_SubRecord;
  468. Procedure TestAdvRecord_SubClass;
  469. Procedure TestAdvRecord_SubInterfaceFail;
  470. Procedure TestAdvRecord_Constructor;
  471. Procedure TestAdvRecord_ClassConstructor_Program;
  472. Procedure TestAdvRecord_ClassConstructor_Unit;
  473. // classes
  474. Procedure TestClass_TObjectDefaultConstructor;
  475. Procedure TestClass_TObjectConstructorWithParams;
  476. Procedure TestClass_TObjectConstructorWithDefaultParam;
  477. Procedure TestClass_Var;
  478. Procedure TestClass_Method;
  479. Procedure TestClass_Implementation;
  480. Procedure TestClass_Inheritance;
  481. Procedure TestClass_TypeAlias;
  482. Procedure TestClass_AbstractMethod;
  483. Procedure TestClass_CallInherited_ProcNoParams;
  484. Procedure TestClass_CallInherited_WithParams;
  485. Procedure TestClasS_CallInheritedConstructor;
  486. Procedure TestClass_ClassVar_Assign;
  487. Procedure TestClass_CallClassMethod;
  488. Procedure TestClass_CallClassMethodStatic; // ToDo
  489. Procedure TestClass_Property;
  490. Procedure TestClass_Property_ClassMethod;
  491. Procedure TestClass_Property_Indexed;
  492. Procedure TestClass_Property_IndexSpec;
  493. Procedure TestClass_PropertyOfTypeArray;
  494. Procedure TestClass_PropertyDefault;
  495. Procedure TestClass_PropertyDefault_TypecastToOtherDefault;
  496. //Procedure TestClass_PropertyDefault;
  497. Procedure TestClass_PropertyOverride;
  498. Procedure TestClass_PropertyIncVisibility;
  499. Procedure TestClass_Assigned;
  500. Procedure TestClass_WithClassDoCreate;
  501. Procedure TestClass_WithClassInstDoProperty;
  502. Procedure TestClass_WithClassInstDoPropertyWithParams;
  503. Procedure TestClass_WithClassInstDoFunc;
  504. Procedure TestClass_TypeCast;
  505. Procedure TestClass_TypeCastUntypedParam;
  506. Procedure TestClass_Overloads;
  507. Procedure TestClass_OverloadsAncestor;
  508. Procedure TestClass_OverloadConstructor;
  509. Procedure TestClass_OverloadDelphiOverride;
  510. Procedure TestClass_ReintroduceVarDelphi;
  511. Procedure TestClass_ReintroducedVar;
  512. Procedure TestClass_RaiseDescendant;
  513. Procedure TestClass_ExternalMethod;
  514. Procedure TestClass_ExternalVirtualNameMismatchFail;
  515. Procedure TestClass_ExternalOverrideFail;
  516. Procedure TestClass_ExternalVar;
  517. Procedure TestClass_Const;
  518. Procedure TestClass_ConstEnum;
  519. Procedure TestClass_LocalConstDuplicate_Prg;
  520. Procedure TestClass_LocalConstDuplicate_Unit;
  521. // ToDo: Procedure TestAdvRecord_LocalConstDuplicate;
  522. Procedure TestClass_LocalVarSelfFail;
  523. Procedure TestClass_ArgSelfFail;
  524. Procedure TestClass_NestedProcSelf;
  525. Procedure TestClass_NestedProcSelf2;
  526. Procedure TestClass_NestedProcClassSelf;
  527. Procedure TestClass_NestedProcCallInherited;
  528. Procedure TestClass_TObjectFree;
  529. Procedure TestClass_TObjectFree_VarArg;
  530. Procedure TestClass_TObjectFreeNewInstance;
  531. Procedure TestClass_TObjectFreeLowerCase;
  532. Procedure TestClass_TObjectFreeFunctionFail;
  533. Procedure TestClass_TObjectFreePropertyFail;
  534. Procedure TestClass_ForIn;
  535. Procedure TestClass_DispatchMessage;
  536. Procedure TestClass_Message_DuplicateIntFail;
  537. Procedure TestClass_DispatchMessage_WrongFieldNameFail;
  538. // class of
  539. Procedure TestClassOf_Create;
  540. Procedure TestClassOf_Call;
  541. Procedure TestClassOf_Assign;
  542. Procedure TestClassOf_Is;
  543. Procedure TestClassOf_Compare;
  544. Procedure TestClassOf_ClassVar;
  545. Procedure TestClassOf_ClassMethod;
  546. Procedure TestClassOf_ClassProperty;
  547. Procedure TestClassOf_ClassMethodSelf;
  548. Procedure TestClassOf_TypeCast;
  549. Procedure TestClassOf_ImplicitFunctionCall;
  550. Procedure TestClassOf_Const;
  551. // nested class
  552. Procedure TestNestedClass_Alias;
  553. Procedure TestNestedClass_Record;
  554. Procedure TestNestedClass_Class;
  555. // external class
  556. Procedure TestExternalClass_Var;
  557. Procedure TestExternalClass_Const;
  558. Procedure TestExternalClass_Dollar;
  559. Procedure TestExternalClass_DuplicateVarFail;
  560. Procedure TestExternalClass_Method;
  561. Procedure TestExternalClass_ClassMethod;
  562. Procedure TestExternalClass_ClassMethodStatic;
  563. Procedure TestExternalClass_FunctionResultInTypeCast;
  564. Procedure TestExternalClass_NonExternalOverride;
  565. Procedure TestExternalClass_OverloadHint;
  566. Procedure TestExternalClass_SameNamePublishedProperty;
  567. Procedure TestExternalClass_Property;
  568. Procedure TestExternalClass_PropertyDate;
  569. Procedure TestExternalClass_ClassProperty;
  570. Procedure TestExternalClass_ClassOf;
  571. Procedure TestExternalClass_ClassOtherUnit;
  572. Procedure TestExternalClass_Is;
  573. Procedure TestExternalClass_As;
  574. Procedure TestExternalClass_DestructorFail;
  575. Procedure TestExternalClass_New;
  576. Procedure TestExternalClass_ClassOf_New;
  577. Procedure TestExternalClass_FuncClassOf_New;
  578. Procedure TestExternalClass_New_PasClassFail;
  579. Procedure TestExternalClass_New_PasClassBracketsFail;
  580. Procedure TestExternalClass_NewExtName;
  581. Procedure TestExternalClass_Constructor;
  582. Procedure TestExternalClass_ConstructorBrackets;
  583. Procedure TestExternalClass_LocalConstSameName;
  584. Procedure TestExternalClass_ReintroduceOverload;
  585. Procedure TestExternalClass_Inherited;
  586. Procedure TestExternalClass_PascalAncestorFail;
  587. Procedure TestExternalClass_NewInstance;
  588. Procedure TestExternalClass_NewInstance_NonVirtualFail;
  589. Procedure TestExternalClass_NewInstance_FirstParamNotString_Fail;
  590. Procedure TestExternalClass_NewInstance_SecondParamTyped_Fail;
  591. Procedure TestExternalClass_JSFunctionPasDescendant;
  592. Procedure TestExternalClass_PascalProperty;
  593. Procedure TestExternalClass_TypeCastToRootClass;
  594. Procedure TestExternalClass_TypeCastToJSObject;
  595. Procedure TestExternalClass_TypeCastStringToExternalString;
  596. Procedure TestExternalClass_TypeCastToJSFunction;
  597. Procedure TestExternalClass_TypeCastDelphiUnrelated;
  598. Procedure TestExternalClass_CallClassFunctionOfInstanceFail;
  599. Procedure TestExternalClass_BracketAccessor;
  600. Procedure TestExternalClass_BracketAccessor_Call;
  601. Procedure TestExternalClass_BracketAccessor_2ParamsFail;
  602. Procedure TestExternalClass_BracketAccessor_ReadOnly;
  603. Procedure TestExternalClass_BracketAccessor_WriteOnly;
  604. Procedure TestExternalClass_BracketAccessor_MultiType;
  605. Procedure TestExternalClass_BracketAccessor_Index;
  606. Procedure TestExternalClass_ForInJSObject;
  607. Procedure TestExternalClass_ForInJSArray;
  608. Procedure TestExternalClass_IncompatibleArgDuplicateIdentifier;
  609. // class interfaces
  610. Procedure TestClassInterface_Corba;
  611. Procedure TestClassInterface_ProcExternalFail;
  612. Procedure TestClassInterface_Overloads;
  613. Procedure TestClassInterface_DuplicateGUIInIntfListFail;
  614. Procedure TestClassInterface_DuplicateGUIInAncestorFail;
  615. Procedure TestClassInterface_AncestorImpl;
  616. Procedure TestClassInterface_ImplReintroduce;
  617. Procedure TestClassInterface_MethodResolution;
  618. Procedure TestClassInterface_AncestorMoreInterfaces;
  619. Procedure TestClassInterface_MethodOverride;
  620. Procedure TestClassInterface_Corba_Delegation;
  621. Procedure TestClassInterface_Corba_DelegationStatic;
  622. Procedure TestClassInterface_Corba_Operators;
  623. Procedure TestClassInterface_Corba_Args;
  624. Procedure TestClassInterface_Corba_ForIn;
  625. Procedure TestClassInterface_COM_AssignVar;
  626. Procedure TestClassInterface_COM_AssignArg;
  627. Procedure TestClassInterface_COM_FunctionResult;
  628. Procedure TestClassInterface_COM_InheritedFuncResult;
  629. Procedure TestClassInterface_COM_IsAsTypeCasts;
  630. Procedure TestClassInterface_COM_PassAsArg;
  631. Procedure TestClassInterface_COM_PassToUntypedParam;
  632. Procedure TestClassInterface_COM_FunctionInExpr;
  633. Procedure TestClassInterface_COM_Property;
  634. Procedure TestClassInterface_COM_IntfProperty;
  635. Procedure TestClassInterface_COM_Delegation;
  636. Procedure TestClassInterface_COM_With;
  637. Procedure TestClassInterface_COM_ForIn;
  638. Procedure TestClassInterface_COM_ArrayOfIntfFail;
  639. Procedure TestClassInterface_COM_RecordIntfFail;
  640. Procedure TestClassInterface_COM_UnitInitialization;
  641. Procedure TestClassInterface_GUID;
  642. Procedure TestClassInterface_GUIDProperty;
  643. // helpers
  644. Procedure TestClassHelper_ClassVar;
  645. Procedure TestClassHelper_Method_AccessInstanceFields;
  646. Procedure TestClassHelper_Method_Call;
  647. Procedure TestClassHelper_Method_Nested_Call;
  648. Procedure TestClassHelper_ClassMethod_Call;
  649. Procedure TestClassHelper_ClassOf;
  650. Procedure TestClassHelper_MethodRefObjFPC;
  651. Procedure TestClassHelper_Constructor;
  652. Procedure TestClassHelper_InheritedObjFPC;
  653. Procedure TestClassHelper_Property;
  654. Procedure TestClassHelper_Property_Array;
  655. Procedure TestClassHelper_Property_Array_Default;
  656. Procedure TestClassHelper_Property_Array_DefaultDefault;
  657. Procedure TestClassHelper_ClassProperty;
  658. Procedure TestClassHelper_ClassPropertyStatic;
  659. Procedure TestClassHelper_ClassProperty_Array;
  660. Procedure TestClassHelper_ForIn;
  661. Procedure TestClassHelper_PassProperty;
  662. Procedure TestExtClassHelper_ClassVar;
  663. Procedure TestExtClassHelper_Method_Call;
  664. Procedure TestExtClassHelper_ClassMethod_MissingStatic;
  665. Procedure TestRecordHelper_ClassVar;
  666. Procedure TestRecordHelper_Method_Call;
  667. Procedure TestRecordHelper_Constructor;
  668. Procedure TestTypeHelper_ClassVar;
  669. Procedure TestTypeHelper_PassResultElement;
  670. Procedure TestTypeHelper_PassArgs;
  671. Procedure TestTypeHelper_PassVarConst;
  672. Procedure TestTypeHelper_PassFuncResult;
  673. Procedure TestTypeHelper_PassPropertyField;
  674. Procedure TestTypeHelper_PassPropertyGetter;
  675. Procedure TestTypeHelper_PassClassPropertyField;
  676. Procedure TestTypeHelper_PassClassPropertyGetterStatic;
  677. Procedure TestTypeHelper_PassClassPropertyGetterNonStatic;
  678. Procedure TestTypeHelper_Property;
  679. Procedure TestTypeHelper_Property_Array;
  680. Procedure TestTypeHelper_ClassProperty;
  681. Procedure TestTypeHelper_ClassProperty_Array;
  682. Procedure TestTypeHelper_ClassMethod;
  683. Procedure TestTypeHelper_ExtClassMethodFail;
  684. Procedure TestTypeHelper_Constructor;
  685. Procedure TestTypeHelper_Word;
  686. Procedure TestTypeHelper_Boolean;
  687. Procedure TestTypeHelper_WordBool;
  688. Procedure TestTypeHelper_Double;
  689. Procedure TestTypeHelper_NativeInt;
  690. Procedure TestTypeHelper_StringChar;
  691. Procedure TestTypeHelper_JSValue;
  692. Procedure TestTypeHelper_Array;
  693. Procedure TestTypeHelper_EnumType;
  694. Procedure TestTypeHelper_SetType;
  695. Procedure TestTypeHelper_InterfaceType;
  696. Procedure TestTypeHelper_NestedSelf;
  697. // proc types
  698. Procedure TestProcType;
  699. Procedure TestProcType_Arg;
  700. Procedure TestProcType_FunctionFPC;
  701. Procedure TestProcType_FunctionDelphi;
  702. Procedure TestProcType_ProcedureDelphi;
  703. Procedure TestProcType_AsParam;
  704. Procedure TestProcType_MethodFPC;
  705. Procedure TestProcType_MethodDelphi;
  706. Procedure TestProcType_PropertyFPC;
  707. Procedure TestProcType_PropertyDelphi;
  708. Procedure TestProcType_WithClassInstDoPropertyFPC;
  709. Procedure TestProcType_Nested;
  710. Procedure TestProcType_NestedOfObject;
  711. Procedure TestProcType_ReferenceToProc;
  712. Procedure TestProcType_ReferenceToMethod;
  713. Procedure TestProcType_Typecast;
  714. Procedure TestProcType_PassProcToUntyped;
  715. Procedure TestProcType_PassProcToArray;
  716. Procedure TestProcType_SafeCallObjFPC;
  717. Procedure TestProcType_SafeCallDelphi;
  718. // pointer
  719. Procedure TestPointer;
  720. Procedure TestPointer_Proc;
  721. Procedure TestPointer_AssignRecordFail;
  722. Procedure TestPointer_AssignStaticArrayFail;
  723. Procedure TestPointer_TypeCastJSValueToPointer;
  724. Procedure TestPointer_NonRecordFail;
  725. Procedure TestPointer_AnonymousArgTypeFail;
  726. Procedure TestPointer_AnonymousVarTypeFail;
  727. Procedure TestPointer_AnonymousResultTypeFail;
  728. Procedure TestPointer_AddrOperatorFail;
  729. Procedure TestPointer_ArrayParamsFail;
  730. Procedure TestPointer_PointerAddFail;
  731. Procedure TestPointer_IncPointerFail;
  732. Procedure TestPointer_Record;
  733. Procedure TestPointer_RecordArg;
  734. // jsvalue
  735. Procedure TestJSValue_AssignToJSValue;
  736. Procedure TestJSValue_TypeCastToBaseType;
  737. Procedure TestJSValue_TypecastToJSValue;
  738. Procedure TestJSValue_Equal;
  739. Procedure TestJSValue_If;
  740. Procedure TestJSValue_Not;
  741. Procedure TestJSValue_Enum;
  742. Procedure TestJSValue_ClassInstance;
  743. Procedure TestJSValue_ClassOf;
  744. Procedure TestJSValue_ArrayOfJSValue;
  745. Procedure TestJSValue_ArrayLit;
  746. Procedure TestJSValue_Params;
  747. Procedure TestJSValue_UntypedParam;
  748. Procedure TestJSValue_FuncResultType;
  749. Procedure TestJSValue_ProcType_Assign;
  750. Procedure TestJSValue_ProcType_Equal;
  751. Procedure TestJSValue_ProcType_Param;
  752. Procedure TestJSValue_AssignToPointerFail;
  753. Procedure TestJSValue_OverloadDouble;
  754. Procedure TestJSValue_OverloadNativeInt;
  755. Procedure TestJSValue_OverloadWord;
  756. Procedure TestJSValue_OverloadString;
  757. Procedure TestJSValue_OverloadChar;
  758. Procedure TestJSValue_OverloadPointer;
  759. Procedure TestJSValue_ForIn;
  760. // RTTI
  761. Procedure TestRTTI_IntRange;
  762. Procedure TestRTTI_Double;
  763. Procedure TestRTTI_ProcType;
  764. Procedure TestRTTI_ProcType_ArgFromOtherUnit;
  765. Procedure TestRTTI_EnumAndSetType;
  766. Procedure TestRTTI_EnumRange;
  767. Procedure TestRTTI_AnonymousEnumType;
  768. Procedure TestRTTI_StaticArray;
  769. Procedure TestRTTI_DynArray;
  770. Procedure TestRTTI_ArrayNestedAnonymous;
  771. Procedure TestRTTI_PublishedMethodOverloadFail;
  772. Procedure TestRTTI_PublishedMethodHideNoHint;
  773. Procedure TestRTTI_PublishedMethodExternalFail;
  774. Procedure TestRTTI_PublishedClassPropertyFail;
  775. Procedure TestRTTI_PublishedClassFieldFail;
  776. Procedure TestRTTI_PublishedFieldExternalFail;
  777. Procedure TestRTTI_Class_Field;
  778. Procedure TestRTTI_Class_Method;
  779. Procedure TestRTTI_Class_MethodArgFlags;
  780. Procedure TestRTTI_Class_Property;
  781. Procedure TestRTTI_Class_PropertyParams;
  782. Procedure TestRTTI_Class_OtherUnit_TypeAlias;
  783. Procedure TestRTTI_Class_OmitRTTI;
  784. Procedure TestRTTI_Class_Field_AnonymousArrayOfSelfClass;
  785. Procedure TestRTTI_IndexModifier;
  786. Procedure TestRTTI_StoredModifier;
  787. Procedure TestRTTI_DefaultValue;
  788. Procedure TestRTTI_DefaultValueSet;
  789. Procedure TestRTTI_DefaultValueRangeType;
  790. Procedure TestRTTI_DefaultValueInherit;
  791. Procedure TestRTTI_OverrideMethod;
  792. Procedure TestRTTI_ReintroduceMethod;
  793. Procedure TestRTTI_OverloadProperty;
  794. // ToDo: array argument
  795. Procedure TestRTTI_ClassForward;
  796. Procedure TestRTTI_ClassOf;
  797. Procedure TestRTTI_Record;
  798. Procedure TestRTTI_RecordAnonymousArray;
  799. Procedure TestRTTI_Record_ClassVarType;
  800. Procedure TestRTTI_LocalTypes;
  801. Procedure TestRTTI_TypeInfo_BaseTypes;
  802. Procedure TestRTTI_TypeInfo_Type_BaseTypes;
  803. Procedure TestRTTI_TypeInfo_LocalFail;
  804. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses1;
  805. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses2;
  806. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses3;
  807. Procedure TestRTTI_TypeInfo_FunctionClassType;
  808. Procedure TestRTTI_TypeInfo_MixedUnits_PointerAndClass;
  809. Procedure TestRTTI_Interface_Corba;
  810. Procedure TestRTTI_Interface_COM;
  811. Procedure TestRTTI_ClassHelper;
  812. Procedure TestRTTI_ExternalClass;
  813. Procedure TestRTTI_Unit;
  814. // Resourcestring
  815. Procedure TestResourcestringProgram;
  816. Procedure TestResourcestringUnit;
  817. Procedure TestResourcestringImplementation;
  818. // Attributes
  819. Procedure TestAttributes_Members;
  820. Procedure TestAttributes_Types;
  821. Procedure TestAttributes_HelperConstructor_Fail;
  822. // Assertions, checks
  823. procedure TestAssert;
  824. procedure TestAssert_SysUtils;
  825. procedure TestObjectChecks;
  826. procedure TestOverflowChecks_Int;
  827. procedure TestRangeChecks_AssignInt;
  828. procedure TestRangeChecks_AssignIntRange;
  829. procedure TestRangeChecks_AssignEnum;
  830. procedure TestRangeChecks_AssignEnumRange;
  831. procedure TestRangeChecks_AssignChar;
  832. procedure TestRangeChecks_AssignCharRange;
  833. procedure TestRangeChecks_ArrayIndex;
  834. procedure TestRangeChecks_ArrayOfRecIndex;
  835. procedure TestRangeChecks_StringIndex;
  836. procedure TestRangeChecks_TypecastInt;
  837. procedure TestRangeChecks_TypeHelperInt;
  838. // Async/AWait
  839. Procedure TestAsync_Proc;
  840. Procedure TestAsync_CallResultIsPromise;
  841. Procedure TestAsync_ConstructorFail;
  842. Procedure TestAsync_PropertyGetterFail;
  843. Procedure TestAwait_NonPromiseWithTypeFail;
  844. Procedure TestAwait_AsyncCallTypeMismatch;
  845. Procedure TestAWait_OutsideAsyncFail;
  846. Procedure TestAWait_IntegerFail;
  847. Procedure TestAWait_ExternalClassPromise;
  848. Procedure TestAWait_JSValue;
  849. Procedure TestAWait_Result;
  850. Procedure TestAWait_ResultPromiseMissingTypeFail; // await(AsyncCallResultPromise) needs T
  851. Procedure TestAsync_AnonymousProc;
  852. Procedure TestAsync_ProcType;
  853. Procedure TestAsync_ProcTypeAsyncModMismatchFail;
  854. Procedure TestAsync_Inherited;
  855. Procedure TestAsync_ClassInterface;
  856. Procedure TestAsync_ClassInterface_AsyncMissmatchFail;
  857. end;
  858. function LinesToStr(Args: array of const): string;
  859. function ExtractFileUnitName(aFilename: string): string;
  860. function JSToStr(El: TJSElement): string;
  861. function CheckSrcDiff(Expected, Actual: string; out Msg: string): boolean;
  862. implementation
  863. function LinesToStr(Args: array of const): string;
  864. var
  865. s: String;
  866. i: Integer;
  867. begin
  868. s:='';
  869. for i:=Low(Args) to High(Args) do
  870. case Args[i].VType of
  871. vtChar: s += Args[i].VChar+LineEnding;
  872. vtString: s += Args[i].VString^+LineEnding;
  873. vtPChar: s += Args[i].VPChar+LineEnding;
  874. vtWideChar: s += AnsiString(Args[i].VWideChar)+LineEnding;
  875. vtPWideChar: s += AnsiString(Args[i].VPWideChar)+LineEnding;
  876. vtAnsiString: s += AnsiString(Args[i].VAnsiString)+LineEnding;
  877. vtWidestring: s += AnsiString(WideString(Args[i].VWideString))+LineEnding;
  878. vtUnicodeString:s += AnsiString(UnicodeString(Args[i].VUnicodeString))+LineEnding;
  879. end;
  880. Result:=s;
  881. end;
  882. function ExtractFileUnitName(aFilename: string): string;
  883. var
  884. p: Integer;
  885. begin
  886. Result:=ExtractFileName(aFilename);
  887. if Result='' then exit;
  888. for p:=length(Result) downto 1 do
  889. case Result[p] of
  890. '/','\': exit;
  891. '.':
  892. begin
  893. Delete(Result,p,length(Result));
  894. exit;
  895. end;
  896. end;
  897. end;
  898. function JSToStr(El: TJSElement): string;
  899. var
  900. aWriter: TBufferWriter;
  901. aJSWriter: TJSWriter;
  902. begin
  903. aJSWriter:=nil;
  904. aWriter:=TBufferWriter.Create(1000);
  905. try
  906. aJSWriter:=TJSWriter.Create(aWriter);
  907. aJSWriter.IndentSize:=2;
  908. aJSWriter.WriteJS(El);
  909. Result:=aWriter.AsString;
  910. finally
  911. aJSWriter.Free;
  912. aWriter.Free;
  913. end;
  914. end;
  915. function CheckSrcDiff(Expected, Actual: string; out Msg: string): boolean;
  916. // search diff, ignore changes in spaces
  917. const
  918. SpaceChars = [#9,#10,#13,' '];
  919. var
  920. ExpectedP, ActualP: PChar;
  921. function FindLineEnd(p: PChar): PChar;
  922. begin
  923. Result:=p;
  924. while not (Result^ in [#0,#10,#13]) do inc(Result);
  925. end;
  926. function FindLineStart(p, MinP: PChar): PChar;
  927. begin
  928. while (p>MinP) and not (p[-1] in [#10,#13]) do dec(p);
  929. Result:=p;
  930. end;
  931. procedure SkipLineEnd(var p: PChar);
  932. begin
  933. if p^ in [#10,#13] then
  934. begin
  935. if (p[1] in [#10,#13]) and (p^<>p[1]) then
  936. inc(p,2)
  937. else
  938. inc(p);
  939. end;
  940. end;
  941. function HasSpecialChar(s: string): boolean;
  942. var
  943. i: Integer;
  944. begin
  945. for i:=1 to length(s) do
  946. if s[i] in [#0..#31,#127..#255] then
  947. exit(true);
  948. Result:=false;
  949. end;
  950. function HashSpecialChars(s: string): string;
  951. var
  952. i: Integer;
  953. begin
  954. Result:='';
  955. for i:=1 to length(s) do
  956. if s[i] in [#0..#31,#127..#255] then
  957. Result:=Result+'#'+hexstr(ord(s[i]),2)
  958. else
  959. Result:=Result+s[i];
  960. end;
  961. procedure DiffFound;
  962. var
  963. ActLineStartP, ActLineEndP, p, StartPos: PChar;
  964. ExpLine, ActLine: String;
  965. i, LineNo, DiffLineNo: Integer;
  966. begin
  967. writeln('Diff found "',Msg,'". Lines:');
  968. // write correct lines
  969. p:=PChar(Expected);
  970. LineNo:=0;
  971. DiffLineNo:=0;
  972. repeat
  973. StartPos:=p;
  974. while not (p^ in [#0,#10,#13]) do inc(p);
  975. ExpLine:=copy(Expected,StartPos-PChar(Expected)+1,p-StartPos);
  976. SkipLineEnd(p);
  977. inc(LineNo);
  978. if (p<=ExpectedP) and (p^<>#0) then
  979. begin
  980. writeln('= ',ExpLine);
  981. end else begin
  982. // diff line
  983. if DiffLineNo=0 then DiffLineNo:=LineNo;
  984. // write actual line
  985. ActLineStartP:=FindLineStart(ActualP,PChar(Actual));
  986. ActLineEndP:=FindLineEnd(ActualP);
  987. ActLine:=copy(Actual,ActLineStartP-PChar(Actual)+1,ActLineEndP-ActLineStartP);
  988. writeln('- ',ActLine);
  989. if HasSpecialChar(ActLine) then
  990. writeln('- ',HashSpecialChars(ActLine));
  991. // write expected line
  992. writeln('+ ',ExpLine);
  993. if HasSpecialChar(ExpLine) then
  994. writeln('- ',HashSpecialChars(ExpLine));
  995. // write empty line with pointer ^
  996. for i:=1 to 2+ExpectedP-StartPos do write(' ');
  997. writeln('^');
  998. Msg:='expected "'+ExpLine+'", but got "'+ActLine+'".';
  999. CheckSrcDiff:=false;
  1000. // write up to three following actual lines to get some context
  1001. for i:=1 to 3 do begin
  1002. ActLineStartP:=ActLineEndP;
  1003. SkipLineEnd(ActLineStartP);
  1004. if ActLineStartP^=#0 then break;
  1005. ActLineEndP:=FindLineEnd(ActLineStartP);
  1006. ActLine:=copy(Actual,ActLineStartP-PChar(Actual)+1,ActLineEndP-ActLineStartP);
  1007. writeln('~ ',ActLine);
  1008. end;
  1009. exit;
  1010. end;
  1011. until p^=#0;
  1012. writeln('DiffFound Actual:-----------------------');
  1013. writeln(Actual);
  1014. writeln('DiffFound Expected:---------------------');
  1015. writeln(Expected);
  1016. writeln('DiffFound ------------------------------');
  1017. Msg:='diff found, but lines are the same, internal error';
  1018. CheckSrcDiff:=false;
  1019. end;
  1020. var
  1021. IsSpaceNeeded: Boolean;
  1022. LastChar, Quote: Char;
  1023. begin
  1024. Result:=true;
  1025. Msg:='';
  1026. if Expected='' then Expected:=' ';
  1027. if Actual='' then Actual:=' ';
  1028. ExpectedP:=PChar(Expected);
  1029. ActualP:=PChar(Actual);
  1030. repeat
  1031. //writeln('TTestModule.CheckDiff Exp="',ExpectedP^,'" Act="',ActualP^,'"');
  1032. case ExpectedP^ of
  1033. #0:
  1034. begin
  1035. // check that rest of Actual has only spaces
  1036. while ActualP^ in SpaceChars do inc(ActualP);
  1037. if ActualP^<>#0 then
  1038. begin
  1039. DiffFound;
  1040. exit;
  1041. end;
  1042. exit(true);
  1043. end;
  1044. ' ',#9,#10,#13:
  1045. begin
  1046. // skip space in Expected
  1047. IsSpaceNeeded:=false;
  1048. if ExpectedP>PChar(Expected) then
  1049. LastChar:=ExpectedP[-1]
  1050. else
  1051. LastChar:=#0;
  1052. while ExpectedP^ in SpaceChars do inc(ExpectedP);
  1053. if (LastChar in ['a'..'z','A'..'Z','0'..'9','_','$'])
  1054. and (ExpectedP^ in ['a'..'z','A'..'Z','0'..'9','_','$']) then
  1055. IsSpaceNeeded:=true;
  1056. if IsSpaceNeeded and (not (ActualP^ in SpaceChars)) then
  1057. begin
  1058. DiffFound;
  1059. exit;
  1060. end;
  1061. while ActualP^ in SpaceChars do inc(ActualP);
  1062. end;
  1063. '''','"':
  1064. begin
  1065. while ActualP^ in SpaceChars do inc(ActualP);
  1066. if ExpectedP^<>ActualP^ then
  1067. begin
  1068. DiffFound;
  1069. exit;
  1070. end;
  1071. Quote:=ExpectedP^;
  1072. repeat
  1073. inc(ExpectedP);
  1074. inc(ActualP);
  1075. if ExpectedP^<>ActualP^ then
  1076. begin
  1077. DiffFound;
  1078. exit;
  1079. end;
  1080. if (ExpectedP^ in [#0,#10,#13]) then
  1081. break
  1082. else if (ExpectedP^=Quote) then
  1083. begin
  1084. inc(ExpectedP);
  1085. inc(ActualP);
  1086. break;
  1087. end;
  1088. until false;
  1089. end;
  1090. else
  1091. while ActualP^ in SpaceChars do inc(ActualP);
  1092. if ExpectedP^<>ActualP^ then
  1093. begin
  1094. DiffFound;
  1095. exit;
  1096. end;
  1097. inc(ExpectedP);
  1098. inc(ActualP);
  1099. end;
  1100. until false;
  1101. end;
  1102. { TTestEnginePasResolver }
  1103. destructor TTestEnginePasResolver.Destroy;
  1104. begin
  1105. FreeAndNil(FStreamResolver);
  1106. FreeAndNil(FParser);
  1107. FreeAndNil(FScanner);
  1108. FreeAndNil(FStreamResolver);
  1109. if Module<>nil then
  1110. begin
  1111. Module.Release{$IFDEF CheckPasTreeRefCount}('CreateElement'){$ENDIF};
  1112. FModule:=nil;
  1113. end;
  1114. inherited Destroy;
  1115. end;
  1116. function TTestEnginePasResolver.FindUnit(const AName, InFilename: String;
  1117. NameExpr, InFileExpr: TPasExpr): TPasModule;
  1118. begin
  1119. Result:=nil;
  1120. if InFilename<>'' then
  1121. RaiseNotYetImplemented(20180224101926,InFileExpr,'Use testcase tcunitsearch instead');
  1122. if Assigned(OnFindUnit) then
  1123. Result:=OnFindUnit(AName);
  1124. if NameExpr=nil then ;
  1125. end;
  1126. procedure TTestEnginePasResolver.UsedInterfacesFinished(Section: TPasSection);
  1127. begin
  1128. // do not parse recursively
  1129. // parse via the queue
  1130. if Section=nil then ;
  1131. end;
  1132. { TCustomTestModule }
  1133. function TCustomTestModule.GetMsgCount: integer;
  1134. begin
  1135. Result:=FHintMsgs.Count;
  1136. end;
  1137. function TCustomTestModule.GetMsgs(Index: integer): TTestHintMessage;
  1138. begin
  1139. Result:=TTestHintMessage(FHintMsgs[Index]);
  1140. end;
  1141. function TCustomTestModule.GetResolverCount: integer;
  1142. begin
  1143. Result:=FModules.Count;
  1144. end;
  1145. function TCustomTestModule.GetResolvers(Index: integer
  1146. ): TTestEnginePasResolver;
  1147. begin
  1148. Result:=TTestEnginePasResolver(FModules[Index]);
  1149. end;
  1150. function TCustomTestModule.OnPasResolverFindUnit(const aUnitName: String
  1151. ): TPasModule;
  1152. var
  1153. DefNamespace: String;
  1154. begin
  1155. //writeln('TTestModule.OnPasResolverFindUnit START Unit="',aUnitName,'"');
  1156. if (Pos('.',aUnitName)<1) then
  1157. begin
  1158. DefNamespace:=GetDefaultNamespace;
  1159. if DefNamespace<>'' then
  1160. begin
  1161. Result:=LoadUnit(DefNamespace+'.'+aUnitName);
  1162. if Result<>nil then exit;
  1163. end;
  1164. end;
  1165. Result:=LoadUnit(aUnitName);
  1166. if Result<>nil then exit;
  1167. {$IFDEF VerbosePas2JS}
  1168. writeln('TTestModule.OnPasResolverFindUnit missing unit "',aUnitName,'"');
  1169. {$ENDIF}
  1170. Fail('can''t find unit "'+aUnitName+'"');
  1171. end;
  1172. procedure TCustomTestModule.OnParserLog(Sender: TObject; const Msg: String);
  1173. var
  1174. aParser: TPasParser;
  1175. Item: TTestHintMessage;
  1176. begin
  1177. aParser:=Sender as TPasParser;
  1178. Item:=TTestHintMessage.Create;
  1179. Item.Id:=aParser.LastMsgNumber;
  1180. Item.MsgType:=aParser.LastMsgType;
  1181. Item.MsgNumber:=aParser.LastMsgNumber;
  1182. Item.Msg:=Msg;
  1183. Item.SourcePos:=aParser.Scanner.CurSourcePos;
  1184. {$IFDEF VerbosePas2JS}
  1185. writeln('TCustomTestModule.OnParserLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1186. {$ENDIF}
  1187. FHintMsgs.Add(Item);
  1188. end;
  1189. procedure TCustomTestModule.OnPasResolverLog(Sender: TObject; const Msg: String
  1190. );
  1191. var
  1192. aResolver: TTestEnginePasResolver;
  1193. Item: TTestHintMessage;
  1194. begin
  1195. aResolver:=Sender as TTestEnginePasResolver;
  1196. Item:=TTestHintMessage.Create;
  1197. Item.Id:=aResolver.LastMsgId;
  1198. Item.MsgType:=aResolver.LastMsgType;
  1199. Item.MsgNumber:=aResolver.LastMsgNumber;
  1200. Item.Msg:=Msg;
  1201. Item.SourcePos:=aResolver.LastSourcePos;
  1202. {$IFDEF VerbosePas2JS}
  1203. writeln('TCustomTestModule.OnPasResolverLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1204. {$ENDIF}
  1205. FHintMsgs.Add(Item);
  1206. end;
  1207. procedure TCustomTestModule.OnScannerLog(Sender: TObject; const Msg: String);
  1208. var
  1209. Item: TTestHintMessage;
  1210. aScanner: TPas2jsPasScanner;
  1211. begin
  1212. aScanner:=Sender as TPas2jsPasScanner;
  1213. Item:=TTestHintMessage.Create;
  1214. Item.Id:=aScanner.LastMsgNumber;
  1215. Item.MsgType:=aScanner.LastMsgType;
  1216. Item.MsgNumber:=aScanner.LastMsgNumber;
  1217. Item.Msg:=Msg;
  1218. Item.SourcePos:=aScanner.CurSourcePos;
  1219. {$IFDEF VerbosePas2JS}
  1220. writeln('TCustomTestModule.OnScannerLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1221. {$ENDIF}
  1222. FHintMsgs.Add(Item);
  1223. end;
  1224. procedure TCustomTestModule.SetWithTypeInfo(const AValue: boolean);
  1225. begin
  1226. if FWithTypeInfo=AValue then Exit;
  1227. FWithTypeInfo:=AValue;
  1228. if AValue then
  1229. Converter.Options:=Converter.Options-[coNoTypeInfo]
  1230. else
  1231. Converter.Options:=Converter.Options+[coNoTypeInfo];
  1232. end;
  1233. function TCustomTestModule.LoadUnit(const aUnitName: String): TPasModule;
  1234. var
  1235. i: Integer;
  1236. CurEngine: TTestEnginePasResolver;
  1237. CurUnitName: String;
  1238. begin
  1239. //writeln('TTestModule.FindUnit START Unit="',aUnitName,'"');
  1240. Result:=nil;
  1241. if (Module.ClassType=TPasModule)
  1242. and (CompareText(Module.Name,aUnitName)=0) then
  1243. exit(Module);
  1244. for i:=0 to ResolverCount-1 do
  1245. begin
  1246. CurEngine:=Resolvers[i];
  1247. CurUnitName:=ExtractFileUnitName(CurEngine.Filename);
  1248. //writeln('TTestModule.FindUnit Checking ',i,'/',ResolverCount,' ',CurEngine.Filename,' ',CurUnitName);
  1249. if CompareText(aUnitName,CurUnitName)=0 then
  1250. begin
  1251. Result:=CurEngine.Module;
  1252. if Result<>nil then exit;
  1253. //writeln('TTestModule.FindUnit PARSING unit "',CurEngine.Filename,'"');
  1254. FileResolver.FindSourceFile(aUnitName);
  1255. CurEngine.StreamResolver:=TStreamResolver.Create;
  1256. CurEngine.StreamResolver.OwnsStreams:=True;
  1257. //writeln('TTestModule.FindUnit SOURCE=',CurEngine.Source);
  1258. CurEngine.StreamResolver.AddStream(CurEngine.FileName,TStringStream.Create(CurEngine.Source));
  1259. CurEngine.Scanner:=TPas2jsPasScanner.Create(CurEngine.StreamResolver);
  1260. InitScanner(CurEngine.Scanner);
  1261. CurEngine.Parser:=TTestPasParser.Create(CurEngine.Scanner,CurEngine.StreamResolver,CurEngine);
  1262. CurEngine.Parser.Options:=po_tcmodules;
  1263. if CompareText(CurUnitName,'System')=0 then
  1264. CurEngine.Parser.ImplicitUses.Clear;
  1265. CurEngine.Scanner.OpenFile(CurEngine.Filename);
  1266. try
  1267. CurEngine.Parser.NextToken;
  1268. CurEngine.Parser.ParseUnit(CurEngine.FModule);
  1269. except
  1270. on E: Exception do
  1271. HandleException(E);
  1272. end;
  1273. //writeln('TTestModule.FindUnit END ',CurUnitName);
  1274. Result:=CurEngine.Module;
  1275. exit;
  1276. end;
  1277. end;
  1278. end;
  1279. procedure TCustomTestModule.SetUp;
  1280. begin
  1281. {$IFDEF EnablePasTreeGlobalRefCount}
  1282. FElementRefCountAtSetup:=TPasElement.GlobalRefCount;
  1283. {$ENDIF}
  1284. if FModules<>nil then
  1285. begin
  1286. writeln('TCustomTestModule.SetUp FModules<>nil');
  1287. Halt;
  1288. end;
  1289. inherited SetUp;
  1290. FSkipTests:=false;
  1291. FWithTypeInfo:=false;
  1292. FSource:=TStringList.Create;
  1293. FHub:=TPas2JSResolverHub.Create(Self);
  1294. FModules:=TObjectList.Create(true);
  1295. FFilename:='test1.pp';
  1296. FFileResolver:=TStreamResolver.Create;
  1297. FFileResolver.OwnsStreams:=True;
  1298. FScanner:=TPas2jsPasScanner.Create(FFileResolver);
  1299. InitScanner(FScanner);
  1300. FEngine:=AddModule(Filename);
  1301. FEngine.Scanner:=FScanner;
  1302. FScanner.Resolver:=FEngine;
  1303. FParser:=TTestPasParser.Create(FScanner,FFileResolver,FEngine);
  1304. FParser.OnLog:=@OnParserLog;
  1305. FEngine.Parser:=FParser;
  1306. Parser.Options:=po_tcmodules;
  1307. FModule:=Nil;
  1308. FConverter:=CreateConverter;
  1309. FExpectedErrorClass:=nil;
  1310. end;
  1311. function TCustomTestModule.CreateConverter: TPasToJSConverter;
  1312. var
  1313. Options: TPasToJsConverterOptions;
  1314. begin
  1315. Result:=TPasToJSConverter.Create;
  1316. Options:=co_tcmodules;
  1317. if WithTypeInfo then
  1318. Exclude(Options,coNoTypeInfo)
  1319. else
  1320. Include(Options,coNoTypeInfo);
  1321. Result.Options:=Options;
  1322. Result.Globals:=TPasToJSConverterGlobals.Create(Result);
  1323. end;
  1324. procedure TCustomTestModule.InitScanner(aScanner: TPas2jsPasScanner);
  1325. begin
  1326. aScanner.AllowedModeSwitches:=msAllPas2jsModeSwitches;
  1327. aScanner.ReadOnlyModeSwitches:=msAllPas2jsModeSwitchesReadOnly;
  1328. aScanner.CurrentModeSwitches:=OBJFPCModeSwitches*msAllPas2jsModeSwitches+msAllPas2jsModeSwitchesReadOnly;
  1329. aScanner.AllowedBoolSwitches:=bsAllPas2jsBoolSwitches;
  1330. aScanner.ReadOnlyBoolSwitches:=bsAllPas2jsBoolSwitchesReadOnly;
  1331. aScanner.CurrentBoolSwitches:=bsAllPas2jsBoolSwitchesReadOnly+[bsHints,bsNotes,bsWarnings,bsWriteableConst];
  1332. aScanner.AllowedValueSwitches:=vsAllPas2jsValueSwitches;
  1333. aScanner.ReadOnlyValueSwitches:=vsAllPas2jsValueSwitchesReadOnly;
  1334. aScanner.OnLog:=@OnScannerLog;
  1335. aScanner.CompilerVersion:='Comp.Ver.tcmodules';
  1336. end;
  1337. procedure TCustomTestModule.TearDown;
  1338. {$IFDEF CheckPasTreeRefCount}
  1339. var
  1340. El: TPasElement;
  1341. {$ENDIF}
  1342. var
  1343. i: Integer;
  1344. CurModule: TPasModule;
  1345. begin
  1346. FHintMsgs.Clear;
  1347. FHintMsgsGood.Clear;
  1348. FSkipTests:=false;
  1349. FWithTypeInfo:=false;
  1350. FJSRegModuleCall:=nil;
  1351. FJSModuleCallArgs:=nil;
  1352. FJSImplentationUses:=nil;
  1353. FJSInterfaceUses:=nil;
  1354. FJSModuleSrc:=nil;
  1355. FJSInitBody:=nil;
  1356. FreeAndNil(FJSSource);
  1357. FreeAndNil(FJSModule);
  1358. FreeAndNil(FConverter);
  1359. Engine.Clear;
  1360. FreeAndNil(FSource);
  1361. FreeAndNil(FFileResolver);
  1362. if FModules<>nil then
  1363. begin
  1364. for i:=0 to FModules.Count-1 do
  1365. begin
  1366. CurModule:=TTestEnginePasResolver(FModules[i]).Module;
  1367. if CurModule=nil then continue;
  1368. //writeln('TCustomTestModule.TearDown ReleaseUsedUnits ',CurModule.Name,' ',CurModule.RefCount,' ',CurModule.RefIds.Text);
  1369. CurModule.ReleaseUsedUnits;
  1370. end;
  1371. if FModule<>nil then
  1372. FModule.ReleaseUsedUnits;
  1373. for i:=0 to FModules.Count-1 do
  1374. begin
  1375. CurModule:=TTestEnginePasResolver(FModules[i]).Module;
  1376. if CurModule=nil then continue;
  1377. //writeln('TCustomTestModule.TearDown UsesReleased ',CurModule.Name,' ',CurModule.RefCount,' ',CurModule.RefIds.Text);
  1378. end;
  1379. FreeAndNil(FModules);
  1380. ReleaseAndNil(TPasElement(FModule){$IFDEF CheckPasTreeRefCount},'CreateElement'{$ENDIF});
  1381. FEngine:=nil;
  1382. end;
  1383. FreeAndNil(FHub);
  1384. inherited TearDown;
  1385. {$IFDEF EnablePasTreeGlobalRefCount}
  1386. if FElementRefCountAtSetup<>TPasElement.GlobalRefCount then
  1387. begin
  1388. writeln('TCustomTestModule.TearDown GlobalRefCount Was='+IntToStr(FElementRefCountAtSetup)+' Now='+IntToStr(TPasElement.GlobalRefCount));
  1389. {$IFDEF CheckPasTreeRefCount}
  1390. El:=TPasElement.FirstRefEl;
  1391. while El<>nil do
  1392. begin
  1393. writeln(' ',GetObjName(El),' RefIds.Count=',El.RefIds.Count,':');
  1394. for i:=0 to El.RefIds.Count-1 do
  1395. writeln(' ',El.RefIds[i]);
  1396. El:=El.NextRefEl;
  1397. end;
  1398. {$ENDIF}
  1399. Halt;
  1400. Fail('TCustomTestModule.TearDown Was='+IntToStr(FElementRefCountAtSetup)+' Now='+IntToStr(TPasElement.GlobalRefCount));
  1401. end;
  1402. {$ENDIF}
  1403. end;
  1404. procedure TCustomTestModule.Add(Line: string);
  1405. begin
  1406. Source.Add(Line);
  1407. end;
  1408. procedure TCustomTestModule.Add(const Lines: array of string);
  1409. var
  1410. i: Integer;
  1411. begin
  1412. for i:=low(Lines) to high(Lines) do
  1413. Add(Lines[i]);
  1414. end;
  1415. procedure TCustomTestModule.StartParsing;
  1416. var
  1417. Src: String;
  1418. begin
  1419. Src:=Source.Text;
  1420. FEngine.Source:=Src;
  1421. FileResolver.AddStream(FileName,TStringStream.Create(Src));
  1422. Scanner.OpenFile(FileName);
  1423. Writeln('// Test : ',Self.TestName);
  1424. Writeln(Src);
  1425. end;
  1426. procedure TCustomTestModule.ParseModuleQueue;
  1427. var
  1428. i: Integer;
  1429. CurResolver: TTestEnginePasResolver;
  1430. Found: Boolean;
  1431. Section: TPasSection;
  1432. begin
  1433. // parse til exception or all modules finished
  1434. while not SkipTests do
  1435. begin
  1436. Found:=false;
  1437. for i:=0 to ResolverCount-1 do
  1438. begin
  1439. CurResolver:=Resolvers[i];
  1440. if CurResolver.CurrentParser=nil then continue;
  1441. if not CurResolver.CurrentParser.CanParseContinue(Section) then
  1442. continue;
  1443. CurResolver.Parser.ParseContinue;
  1444. Found:=true;
  1445. break;
  1446. end;
  1447. if not Found then break;
  1448. end;
  1449. for i:=0 to ResolverCount-1 do
  1450. begin
  1451. CurResolver:=Resolvers[i];
  1452. if CurResolver.Parser=nil then
  1453. begin
  1454. if CurResolver.CurrentParser<>nil then
  1455. Fail('TCustomTestModule.ParseModuleQueue '+CurResolver.Filename+' '+GetObjName(CurResolver.Parser)+'=Parser<>CurrentParser='+GetObjName(CurResolver.CurrentParser));
  1456. continue;
  1457. end;
  1458. if CurResolver.Parser.CurModule<>nil then
  1459. Fail('TCustomTestModule.ParseModuleQueue '+CurResolver.Filename+' NOT FINISHED CurModule='+GetObjName(CurResolver.Parser.CurModule));
  1460. end;
  1461. end;
  1462. procedure TCustomTestModule.ParseModule;
  1463. begin
  1464. if SkipTests then exit;
  1465. FFirstPasStatement:=nil;
  1466. try
  1467. StartParsing;
  1468. Parser.ParseMain(FModule);
  1469. ParseModuleQueue;
  1470. except
  1471. on E: Exception do
  1472. HandleException(E);
  1473. end;
  1474. if SkipTests then exit;
  1475. AssertNotNull('Module resulted in Module',Module);
  1476. AssertEquals('modulename',lowercase(ChangeFileExt(FFileName,'')),lowercase(Module.Name));
  1477. TAssert.AssertSame('Has resolver',Engine,Parser.Engine);
  1478. end;
  1479. procedure TCustomTestModule.ParseProgram;
  1480. begin
  1481. if SkipTests then exit;
  1482. ParseModule;
  1483. if SkipTests then exit;
  1484. AssertEquals('Has program',TPasProgram,Module.ClassType);
  1485. FPasProgram:=TPasProgram(Module);
  1486. AssertNotNull('Has program section',PasProgram.ProgramSection);
  1487. AssertNotNull('Has initialization section',PasProgram.InitializationSection);
  1488. if (PasProgram.InitializationSection.Elements.Count>0) then
  1489. if TObject(PasProgram.InitializationSection.Elements[0]) is TPasImplBlock then
  1490. FFirstPasStatement:=TPasImplBlock(PasProgram.InitializationSection.Elements[0]);
  1491. end;
  1492. procedure TCustomTestModule.ParseUnit;
  1493. begin
  1494. if SkipTests then exit;
  1495. ParseModule;
  1496. if SkipTests then exit;
  1497. AssertEquals('Has unit (TPasModule)',TPasModule,Module.ClassType);
  1498. AssertNotNull('Has interface section',Module.InterfaceSection);
  1499. AssertNotNull('Has implementation section',Module.ImplementationSection);
  1500. if (Module.InitializationSection<>nil)
  1501. and (Module.InitializationSection.Elements.Count>0)
  1502. and (TObject(Module.InitializationSection.Elements[0]) is TPasImplBlock) then
  1503. FFirstPasStatement:=TPasImplBlock(Module.InitializationSection.Elements[0]);
  1504. end;
  1505. function TCustomTestModule.FindModuleWithFilename(aFilename: string
  1506. ): TTestEnginePasResolver;
  1507. var
  1508. i: Integer;
  1509. begin
  1510. for i:=0 to ResolverCount-1 do
  1511. if CompareText(Resolvers[i].Filename,aFilename)=0 then
  1512. exit(Resolvers[i]);
  1513. Result:=nil;
  1514. end;
  1515. function TCustomTestModule.AddModule(aFilename: string
  1516. ): TTestEnginePasResolver;
  1517. begin
  1518. //writeln('TTestModuleConverter.AddModule ',aFilename);
  1519. if FindModuleWithFilename(aFilename)<>nil then
  1520. Fail('TTestModuleConverter.AddModule: file "'+aFilename+'" already exists');
  1521. Result:=TTestEnginePasResolver.Create;
  1522. Result.Filename:=aFilename;
  1523. Result.AddObjFPCBuiltInIdentifiers(btAllJSBaseTypes,bfAllJSBaseProcs);
  1524. Result.OnFindUnit:=@OnPasResolverFindUnit;
  1525. Result.OnLog:=@OnPasResolverLog;
  1526. Result.Hub:=Hub;
  1527. FModules.Add(Result);
  1528. end;
  1529. function TCustomTestModule.AddModuleWithSrc(aFilename, Src: string
  1530. ): TTestEnginePasResolver;
  1531. begin
  1532. Result:=AddModule(aFilename);
  1533. Result.Source:=Src;
  1534. end;
  1535. function TCustomTestModule.AddModuleWithIntfImplSrc(aFilename, InterfaceSrc,
  1536. ImplementationSrc: string): TTestEnginePasResolver;
  1537. var
  1538. Src: String;
  1539. begin
  1540. Src:='unit '+ExtractFileUnitName(aFilename)+';'+LineEnding;
  1541. Src+=LineEnding;
  1542. Src+='interface'+LineEnding;
  1543. Src+=LineEnding;
  1544. Src+=InterfaceSrc;
  1545. Src+='implementation'+LineEnding;
  1546. Src+=LineEnding;
  1547. Src+=ImplementationSrc;
  1548. Src+='end.'+LineEnding;
  1549. Result:=AddModuleWithSrc(aFilename,Src);
  1550. end;
  1551. procedure TCustomTestModule.AddSystemUnit(Parts: TSystemUnitParts);
  1552. var
  1553. Intf, Impl: TStringList;
  1554. begin
  1555. Intf:=TStringList.Create;
  1556. if supTInterfacedObject in Parts then Include(Parts,supTObject);
  1557. // unit interface
  1558. if [supTVarRec,supTypeInfo]*Parts<>[] then
  1559. Intf.Add('{$modeswitch externalclass}');
  1560. Intf.Add('type');
  1561. Intf.Add(' integer=longint;');
  1562. Intf.Add(' sizeint=nativeint;');
  1563. //'const',
  1564. //' LineEnding = #10;',
  1565. //' DirectorySeparator = ''/'';',
  1566. //' DriveSeparator = '''';',
  1567. //' AllowDirectorySeparators : set of char = [''\'',''/''];',
  1568. //' AllowDriveSeparators : set of char = [];',
  1569. if supTObject in Parts then
  1570. Intf.AddStrings([
  1571. 'type',
  1572. ' TClass = class of TObject;',
  1573. ' TObject = class',
  1574. ' constructor Create;',
  1575. ' destructor Destroy; virtual;',
  1576. ' class function ClassType: TClass; assembler;',
  1577. ' class function ClassName: String; assembler;',
  1578. ' class function ClassNameIs(const Name: string): boolean;',
  1579. ' class function ClassParent: TClass; assembler;',
  1580. ' class function InheritsFrom(aClass: TClass): boolean; assembler;',
  1581. ' class function UnitName: String; assembler;',
  1582. ' procedure AfterConstruction; virtual;',
  1583. ' procedure BeforeDestruction;virtual;',
  1584. ' function Equals(Obj: TObject): boolean; virtual;',
  1585. ' function ToString: String; virtual;',
  1586. ' end;']);
  1587. if supTInterfacedObject in Parts then
  1588. Intf.AddStrings([
  1589. ' {$Interfaces COM}',
  1590. ' IUnknown = interface',
  1591. ' [''{00000000-0000-0000-C000-000000000046}'']',
  1592. //' function QueryInterface(const iid: TGuid; out obj): Integer;',
  1593. ' function _AddRef: Integer;',
  1594. ' function _Release: Integer;',
  1595. ' end;',
  1596. ' IInterface = IUnknown;',
  1597. ' TInterfacedObject = class(TObject,IUnknown)',
  1598. ' protected',
  1599. ' fRefCount: Integer;',
  1600. ' { implement methods of IUnknown }',
  1601. //' function QueryInterface(const iid: TGuid; out obj): Integer; virtual;',
  1602. ' function _AddRef: Integer; virtual;',
  1603. ' function _Release: Integer; virtual;',
  1604. ' end;',
  1605. ' TInterfacedClass = class of TInterfacedObject;',
  1606. '',
  1607. '']);
  1608. if supTVarRec in Parts then
  1609. Intf.AddStrings([
  1610. 'const',
  1611. ' vtInteger = 0;',
  1612. ' vtBoolean = 1;',
  1613. ' vtJSValue = 19;',
  1614. 'type',
  1615. ' PVarRec = ^TVarRec;',
  1616. ' TVarRec = record',
  1617. ' VType : byte;',
  1618. ' VJSValue: JSValue;',
  1619. ' vInteger: longint external name ''VJSValue'';',
  1620. ' vBoolean: boolean external name ''VJSValue'';',
  1621. ' end;',
  1622. ' TVarRecArray = array of TVarRec;',
  1623. 'function VarRecs: TVarRecArray; varargs;',
  1624. '']);
  1625. if supTypeInfo in Parts then
  1626. begin
  1627. Intf.AddStrings([
  1628. 'type',
  1629. ' TTypeKind = (',
  1630. ' tkUnknown, // 0',
  1631. ' tkInteger, // 1',
  1632. ' tkChar, // 2 in Delphi/FPC tkWChar, tkUChar',
  1633. ' tkString, // 3 in Delphi/FPC tkSString, tkWString or tkUString',
  1634. ' tkEnumeration, // 4',
  1635. ' tkSet, // 5',
  1636. ' tkDouble, // 6',
  1637. ' tkBool, // 7',
  1638. ' tkProcVar, // 8 function or procedure',
  1639. ' tkMethod, // 9 proc var of object',
  1640. ' tkArray, // 10 static array',
  1641. ' tkDynArray, // 11',
  1642. ' tkRecord, // 12',
  1643. ' tkClass, // 13',
  1644. ' tkClassRef, // 14',
  1645. ' tkPointer, // 15',
  1646. ' tkJSValue, // 16',
  1647. ' tkRefToProcVar, // 17 variable of procedure type',
  1648. ' tkInterface, // 18',
  1649. ' //tkObject,',
  1650. ' //tkSString,tkLString,tkAString,tkWString,',
  1651. ' //tkVariant,',
  1652. ' //tkWChar,',
  1653. ' //tkInt64,',
  1654. ' //tkQWord,',
  1655. ' //tkInterfaceRaw,',
  1656. ' //tkUString,tkUChar,',
  1657. ' tkHelper, // 19',
  1658. ' //tkFile,',
  1659. ' tkExtClass // 20',
  1660. ' );',
  1661. ' TTypeKinds = set of TTypeKind;',
  1662. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  1663. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo)',
  1664. ' end;',
  1665. ' TTypeInfoEnum = class external name ''rtl.tTypeInfoEnum''(TTypeInfoInteger) end;',
  1666. ' TTypeInfoSet = class external name ''rtl.tTypeInfoSet''(TTypeInfo) end;',
  1667. ' TTypeInfoStaticArray = class external name ''rtl.tTypeInfoStaticArray''(TTypeInfo) end;',
  1668. ' TTypeInfoDynArray = class external name ''rtl.tTypeInfoDynArray''(TTypeInfo) end;',
  1669. ' TTypeInfoProcVar = class external name ''rtl.tTypeInfoProcVar''(TTypeInfo) end;',
  1670. ' TTypeInfoMethodVar = class external name ''rtl.tTypeInfoMethodVar''(TTypeInfoProcVar) end;',
  1671. ' TTypeInfoClass = class external name ''rtl.tTypeInfoClass''(TTypeInfo) end;',
  1672. ' TTypeInfoClassRef = class external name ''rtl.tTypeInfoClassRef''(TTypeInfo) end;',
  1673. ' TTypeInfoExtClass = class external name ''rtl.tTypeInfoExtClass''(TTypeInfo) end;',
  1674. ' TTypeInfoRecord = class external name ''rtl.tTypeInfoRecord''(TTypeInfo) end;',
  1675. ' TTypeInfoPointer = class external name ''rtl.tTypeInfoPointer''(TTypeInfo) end;',
  1676. ' TTypeInfoHelper = class external name ''rtl.tTypeInfoHelper''(TTypeInfo) end;',
  1677. ' TTypeInfoInterface = class external name ''rtl.tTypeInfoInterface''(TTypeInfo) end;',
  1678. '']);
  1679. end;
  1680. if supWriteln in Parts then
  1681. Intf.Add('procedure writeln; varargs; external name ''console.log'';');
  1682. Intf.Add('var');
  1683. Intf.Add(' ExitCode: Longint = 0;');
  1684. // unit implementation
  1685. Impl:=TStringList.Create;
  1686. if supTObject in Parts then
  1687. Impl.AddStrings([
  1688. '// needed by ClassNameIs, the real SameText is in SysUtils',
  1689. 'function SameText(const s1, s2: String): Boolean; assembler;',
  1690. 'asm',
  1691. 'end;',
  1692. 'constructor TObject.Create; begin end;',
  1693. 'destructor TObject.Destroy; begin end;',
  1694. 'class function TObject.ClassType: TClass; assembler;',
  1695. 'asm',
  1696. 'end;',
  1697. 'class function TObject.ClassName: String; assembler;',
  1698. 'asm',
  1699. 'end;',
  1700. 'class function TObject.ClassNameIs(const Name: string): boolean;',
  1701. 'begin',
  1702. ' Result:=SameText(Name,ClassName);',
  1703. 'end;',
  1704. 'class function TObject.ClassParent: TClass; assembler;',
  1705. 'asm',
  1706. 'end;',
  1707. 'class function TObject.InheritsFrom(aClass: TClass): boolean; assembler;',
  1708. 'asm',
  1709. 'end;',
  1710. 'class function TObject.UnitName: String; assembler;',
  1711. 'asm',
  1712. 'end;',
  1713. 'procedure TObject.AfterConstruction; begin end;',
  1714. 'procedure TObject.BeforeDestruction; begin end;',
  1715. 'function TObject.Equals(Obj: TObject): boolean;',
  1716. 'begin',
  1717. ' Result:=Obj=Self;',
  1718. 'end;',
  1719. 'function TObject.ToString: String;',
  1720. 'begin',
  1721. ' Result:=ClassName;',
  1722. 'end;'
  1723. ]);
  1724. if supTInterfacedObject in Parts then
  1725. Impl.AddStrings([
  1726. //'function TInterfacedObject.QueryInterface(const iid: TGuid; out obj): Integer;',
  1727. //'begin',
  1728. //'end;',
  1729. 'function TInterfacedObject._AddRef: Integer;',
  1730. 'begin',
  1731. 'end;',
  1732. 'function TInterfacedObject._Release: Integer;',
  1733. 'begin',
  1734. 'end;',
  1735. '']);
  1736. if supTVarRec in Parts then
  1737. Impl.AddStrings([
  1738. 'function VarRecs: TVarRecArray; varargs;',
  1739. 'var',
  1740. ' v: PVarRec;',
  1741. 'begin',
  1742. ' v^.VType:=1;',
  1743. ' v^.VJSValue:=2;',
  1744. 'end;',
  1745. '']);
  1746. try
  1747. AddModuleWithIntfImplSrc('system.pp',Intf.Text,Impl.Text);
  1748. finally
  1749. Intf.Free;
  1750. Impl.Free;
  1751. end;
  1752. end;
  1753. procedure TCustomTestModule.StartProgram(NeedSystemUnit: boolean;
  1754. SystemUnitParts: TSystemUnitParts);
  1755. begin
  1756. if NeedSystemUnit then
  1757. AddSystemUnit(SystemUnitParts)
  1758. else
  1759. Parser.ImplicitUses.Clear;
  1760. Add('program '+ExtractFileUnitName(Filename)+';');
  1761. Add('');
  1762. end;
  1763. procedure TCustomTestModule.StartUnit(NeedSystemUnit: boolean;
  1764. SystemUnitParts: TSystemUnitParts);
  1765. begin
  1766. if NeedSystemUnit then
  1767. AddSystemUnit(SystemUnitParts)
  1768. else
  1769. Parser.ImplicitUses.Clear;
  1770. Add('unit Test1;');
  1771. Add('');
  1772. end;
  1773. procedure TCustomTestModule.ConvertModule;
  1774. procedure CheckUsesList(UsesName: String; Arg: TJSArrayLiteralElement;
  1775. out UsesLit: TJSArrayLiteral);
  1776. var
  1777. i: Integer;
  1778. Item: TJSElement;
  1779. Lit: TJSLiteral;
  1780. begin
  1781. UsesLit:=nil;
  1782. AssertNotNull(UsesName+' uses section',Arg.Expr);
  1783. if (Arg.Expr.ClassType=TJSLiteral) and TJSLiteral(Arg.Expr).Value.IsNull then
  1784. exit; // null is ok
  1785. AssertEquals(UsesName+' uses section param is array',TJSArrayLiteral,Arg.Expr.ClassType);
  1786. FJSInterfaceUses:=TJSArrayLiteral(Arg.Expr);
  1787. for i:=0 to FJSInterfaceUses.Elements.Count-1 do
  1788. begin
  1789. Item:=FJSInterfaceUses.Elements.Elements[i].Expr;
  1790. AssertNotNull(UsesName+' uses section item['+IntToStr(i)+'].Expr',Item);
  1791. AssertEquals(UsesName+' uses section item['+IntToStr(i)+'] is lit',TJSLiteral,Item.ClassType);
  1792. Lit:=TJSLiteral(Item);
  1793. AssertEquals(UsesName+' uses section item['+IntToStr(i)+'] is string lit',
  1794. ord(jsbase.jstString),ord(Lit.Value.ValueType));
  1795. end;
  1796. end;
  1797. procedure CheckFunctionParam(ParamName: string; Arg: TJSArrayLiteralElement;
  1798. out Src: TJSSourceElements);
  1799. var
  1800. FunDecl: TJSFunctionDeclarationStatement;
  1801. FunDef: TJSFuncDef;
  1802. FunBody: TJSFunctionBody;
  1803. begin
  1804. Src:=nil;
  1805. AssertNotNull(ParamName,Arg.Expr);
  1806. AssertEquals(ParamName+' Arg.Expr type',TJSFunctionDeclarationStatement,Arg.Expr.ClassType);
  1807. FunDecl:=Arg.Expr as TJSFunctionDeclarationStatement;
  1808. AssertNotNull(ParamName+' FunDecl.AFunction',FunDecl.AFunction);
  1809. AssertEquals(ParamName+' FunDecl.AFunction type',TJSFuncDef,FunDecl.AFunction.ClassType);
  1810. FunDef:=FunDecl.AFunction as TJSFuncDef;
  1811. AssertEquals(ParamName+' name empty','',String(FunDef.Name));
  1812. AssertNotNull(ParamName+' body',FunDef.Body);
  1813. AssertEquals(ParamName+' body type',TJSFunctionBody,FunDef.Body.ClassType);
  1814. FunBody:=FunDef.Body as TJSFunctionBody;
  1815. AssertNotNull(ParamName+' body.A',FunBody.A);
  1816. AssertEquals(ParamName+' body.A type',TJSSourceElements,FunBody.A.ClassType);
  1817. Src:=FunBody.A as TJSSourceElements;
  1818. end;
  1819. var
  1820. ModuleNameExpr: TJSLiteral;
  1821. InitFunction: TJSFunctionDeclarationStatement;
  1822. InitAssign: TJSSimpleAssignStatement;
  1823. InitName: String;
  1824. LastNode: TJSElement;
  1825. Arg: TJSArrayLiteralElement;
  1826. begin
  1827. if SkipTests then exit;
  1828. try
  1829. FJSModule:=FConverter.ConvertPasElement(Module,Engine) as TJSSourceElements;
  1830. except
  1831. on E: Exception do
  1832. HandleException(E);
  1833. end;
  1834. if SkipTests then exit;
  1835. if ExpectedErrorClass<>nil then
  1836. Fail('Missing '+ExpectedErrorClass.ClassName+' error {'+ExpectedErrorMsg+'} ('+IntToStr(ExpectedErrorNumber)+')');
  1837. FJSSource:=TStringList.Create;
  1838. FJSSource.Text:=ConvertJSModuleToString(JSModule);
  1839. {$IFDEF VerbosePas2JS}
  1840. writeln('TTestModule.ConvertModule JS:');
  1841. write(FJSSource.Text);
  1842. {$ENDIF}
  1843. // rtl.module(...
  1844. AssertEquals('jsmodule has one statement - the call',1,JSModule.Statements.Count);
  1845. AssertNotNull('register module call',JSModule.Statements.Nodes[0].Node);
  1846. AssertEquals('register module call',TJSCallExpression,JSModule.Statements.Nodes[0].Node.ClassType);
  1847. FJSRegModuleCall:=JSModule.Statements.Nodes[0].Node as TJSCallExpression;
  1848. AssertNotNull('register module rtl.module expr',JSRegModuleCall.Expr);
  1849. AssertNotNull('register module rtl.module args',JSRegModuleCall.Args);
  1850. AssertEquals('rtl.module args',TJSArguments,JSRegModuleCall.Args.ClassType);
  1851. FJSModuleCallArgs:=JSRegModuleCall.Args as TJSArguments;
  1852. // parameter 'unitname'
  1853. if JSModuleCallArgs.Elements.Count<1 then
  1854. Fail('rtl.module first param unit missing');
  1855. Arg:=JSModuleCallArgs.Elements.Elements[0];
  1856. AssertNotNull('module name param',Arg.Expr);
  1857. ModuleNameExpr:=Arg.Expr as TJSLiteral;
  1858. AssertEquals('module name param is string',ord(jstString),ord(ModuleNameExpr.Value.ValueType));
  1859. if Module is TPasProgram then
  1860. AssertEquals('module name','program',String(ModuleNameExpr.Value.AsString))
  1861. else
  1862. AssertEquals('module name',Module.Name,String(ModuleNameExpr.Value.AsString));
  1863. // main uses section
  1864. if JSModuleCallArgs.Elements.Count<2 then
  1865. Fail('rtl.module second param main uses missing');
  1866. Arg:=JSModuleCallArgs.Elements.Elements[1];
  1867. CheckUsesList('interface',Arg,FJSInterfaceUses);
  1868. // program/library/interface function()
  1869. if JSModuleCallArgs.Elements.Count<3 then
  1870. Fail('rtl.module third param intf-function missing');
  1871. Arg:=JSModuleCallArgs.Elements.Elements[2];
  1872. CheckFunctionParam('module intf-function',Arg,FJSModuleSrc);
  1873. // search for $mod.$init or $mod.$main - the last statement
  1874. if Module is TPasProgram then
  1875. begin
  1876. InitName:='$main';
  1877. AssertEquals('$mod.'+InitName+' function 1',true,JSModuleSrc.Statements.Count>0);
  1878. end
  1879. else
  1880. InitName:='$init';
  1881. FJSInitBody:=nil;
  1882. if JSModuleSrc.Statements.Count>0 then
  1883. begin
  1884. LastNode:=JSModuleSrc.Statements.Nodes[JSModuleSrc.Statements.Count-1].Node;
  1885. if LastNode is TJSSimpleAssignStatement then
  1886. begin
  1887. InitAssign:=LastNode as TJSSimpleAssignStatement;
  1888. if GetDottedIdentifier(InitAssign.LHS)='$mod.'+InitName then
  1889. begin
  1890. InitFunction:=InitAssign.Expr as TJSFunctionDeclarationStatement;
  1891. FJSInitBody:=InitFunction.AFunction.Body as TJSFunctionBody;
  1892. end
  1893. else if Module is TPasProgram then
  1894. CheckDottedIdentifier('init function',InitAssign.LHS,'$mod.'+InitName);
  1895. end;
  1896. end;
  1897. // optional: implementation uses section
  1898. if JSModuleCallArgs.Elements.Count<4 then
  1899. exit;
  1900. Arg:=JSModuleCallArgs.Elements.Elements[3];
  1901. CheckUsesList('implementation',Arg,FJSImplentationUses);
  1902. end;
  1903. procedure TCustomTestModule.ConvertProgram;
  1904. begin
  1905. Add('end.');
  1906. ParseProgram;
  1907. ConvertModule;
  1908. end;
  1909. procedure TCustomTestModule.ConvertUnit;
  1910. begin
  1911. Add('end.');
  1912. ParseUnit;
  1913. ConvertModule;
  1914. end;
  1915. function TCustomTestModule.ConvertJSModuleToString(El: TJSElement): string;
  1916. begin
  1917. Result:=tcmodules.JSToStr(El);
  1918. end;
  1919. procedure TCustomTestModule.CheckDottedIdentifier(Msg: string; El: TJSElement;
  1920. DottedName: string);
  1921. begin
  1922. if DottedName='' then
  1923. begin
  1924. AssertNull(Msg,El);
  1925. end
  1926. else
  1927. begin
  1928. AssertNotNull(Msg,El);
  1929. AssertEquals(Msg,DottedName,GetDottedIdentifier(El));
  1930. end;
  1931. end;
  1932. function TCustomTestModule.GetDottedIdentifier(El: TJSElement): string;
  1933. begin
  1934. if El=nil then
  1935. Result:=''
  1936. else if El is TJSPrimaryExpressionIdent then
  1937. Result:=String(TJSPrimaryExpressionIdent(El).Name)
  1938. else if El is TJSDotMemberExpression then
  1939. Result:=GetDottedIdentifier(TJSDotMemberExpression(El).MExpr)+'.'+String(TJSDotMemberExpression(El).Name)
  1940. else
  1941. AssertEquals('GetDottedIdentifier',TJSPrimaryExpressionIdent,El.ClassType);
  1942. end;
  1943. procedure TCustomTestModule.CheckSource(Msg, Statements: String;
  1944. InitStatements: string; ImplStatements: string);
  1945. var
  1946. ActualSrc, ExpectedSrc, InitName: String;
  1947. begin
  1948. ActualSrc:=JSToStr(JSModuleSrc);
  1949. if coUseStrict in Converter.Options then
  1950. ExpectedSrc:='"use strict";'+LineEnding
  1951. else
  1952. ExpectedSrc:='';
  1953. ExpectedSrc:=ExpectedSrc+'var $mod = this;'+LineEnding;
  1954. ExpectedSrc:=ExpectedSrc+Statements;
  1955. // unit implementation
  1956. if (Trim(ImplStatements)<>'') then
  1957. ExpectedSrc:=ExpectedSrc+LineEnding
  1958. +'$mod.$implcode = function () {'+LineEnding
  1959. +ImplStatements
  1960. +'};'+LineEnding;
  1961. // program main or unit initialization
  1962. if (Module is TPasProgram) or (Trim(InitStatements)<>'') then
  1963. begin
  1964. if Module is TPasProgram then
  1965. InitName:='$main'
  1966. else
  1967. InitName:='$init';
  1968. ExpectedSrc:=ExpectedSrc+LineEnding
  1969. +'$mod.'+InitName+' = function () {'+LineEnding
  1970. +InitStatements
  1971. +'};'+LineEnding;
  1972. end;
  1973. //writeln('TCustomTestModule.CheckSource ExpectedIntf="',ExpectedSrc,'"');
  1974. //writeln('TTestModule.CheckSource InitStatements="',Trim(InitStatements),'"');
  1975. CheckDiff(Msg,ExpectedSrc,ActualSrc);
  1976. end;
  1977. procedure TCustomTestModule.CheckDiff(Msg, Expected, Actual: string);
  1978. // search diff, ignore changes in spaces
  1979. var
  1980. s: string;
  1981. begin
  1982. if CheckSrcDiff(Expected,Actual,s) then exit;
  1983. Fail(Msg+': '+s);
  1984. end;
  1985. procedure TCustomTestModule.CheckUnit(Filename, ExpectedSrc: string);
  1986. var
  1987. aResolver: TTestEnginePasResolver;
  1988. aConverter: TPasToJSConverter;
  1989. aJSModule: TJSSourceElements;
  1990. ActualSrc: String;
  1991. begin
  1992. aResolver:=GetResolver(Filename);
  1993. AssertNotNull('missing resolver of unit '+Filename,aResolver);
  1994. AssertNotNull('missing resolver.module of unit '+Filename,aResolver.Module);
  1995. {$IFDEF VerbosePas2JS}
  1996. writeln('CheckUnit '+Filename+' converting ...');
  1997. {$ENDIF}
  1998. aConverter:=CreateConverter;
  1999. aJSModule:=nil;
  2000. try
  2001. try
  2002. aJSModule:=aConverter.ConvertPasElement(aResolver.Module,aResolver) as TJSSourceElements;
  2003. except
  2004. on E: Exception do
  2005. HandleException(E);
  2006. end;
  2007. ActualSrc:=ConvertJSModuleToString(aJSModule);
  2008. {$IFDEF VerbosePas2JS}
  2009. writeln('TTestModule.CheckUnit ',Filename,' Pas:');
  2010. write(aResolver.Source);
  2011. writeln('TTestModule.CheckUnit ',Filename,' JS:');
  2012. write(ActualSrc);
  2013. {$ENDIF}
  2014. CheckDiff('Converted unit: "'+ChangeFileExt(Filename,'.js')+'"',ExpectedSrc,ActualSrc);
  2015. finally
  2016. aJSModule.Free;
  2017. aConverter.Free;
  2018. end;
  2019. end;
  2020. procedure TCustomTestModule.CheckHint(MsgType: TMessageType;
  2021. MsgNumber: integer; Msg: string; Marker: PSrcMarker);
  2022. var
  2023. i: Integer;
  2024. Item: TTestHintMessage;
  2025. Expected,Actual: string;
  2026. begin
  2027. //writeln('TCustomTestModule.CheckHint MsgCount=',MsgCount);
  2028. for i:=0 to MsgCount-1 do
  2029. begin
  2030. Item:=Msgs[i];
  2031. if (Item.MsgNumber<>MsgNumber) or (Item.Msg<>Msg) then continue;
  2032. if (Marker<>nil) then
  2033. begin
  2034. if Item.SourcePos.Row<>cardinal(Marker^.Row) then continue;
  2035. if (Item.SourcePos.Column<cardinal(Marker^.StartCol))
  2036. or (Item.SourcePos.Column>cardinal(Marker^.EndCol)) then continue;
  2037. end;
  2038. // found
  2039. FHintMsgsGood.Add(Item);
  2040. str(Item.MsgType,Actual);
  2041. str(MsgType,Expected);
  2042. AssertEquals('MsgType',Expected,Actual);
  2043. exit;
  2044. end;
  2045. // needed message missing -> show emitted messages
  2046. WriteSources('',0,0);
  2047. for i:=0 to MsgCount-1 do
  2048. begin
  2049. Item:=Msgs[i];
  2050. write('TCustomTestModule.CheckHint ',i,'/',MsgCount,' ',Item.MsgType,
  2051. ' ('+IntToStr(Item.MsgNumber),')');
  2052. if Marker<>nil then
  2053. write(' '+ExtractFileName(Item.SourcePos.FileName),'(',Item.SourcePos.Row,',',Item.SourcePos.Column,')');
  2054. writeln(' {',Item.Msg,'}');
  2055. end;
  2056. str(MsgType,Expected);
  2057. Actual:='Missing '+Expected+' ('+IntToStr(MsgNumber)+')';
  2058. if Marker<>nil then
  2059. Actual:=Actual+' '+ExtractFileName(Marker^.Filename)+'('+IntToStr(Marker^.Row)+','+IntToStr(Marker^.StartCol)+'..'+IntToStr(Marker^.EndCol)+')';
  2060. Actual:=Actual+' '+Msg;
  2061. Fail(Actual);
  2062. end;
  2063. procedure TCustomTestModule.CheckResolverUnexpectedHints(WithSourcePos: boolean
  2064. );
  2065. var
  2066. i: Integer;
  2067. s, Txt: String;
  2068. Msg: TTestHintMessage;
  2069. begin
  2070. for i:=0 to MsgCount-1 do
  2071. begin
  2072. Msg:=Msgs[i];
  2073. if FHintMsgsGood.IndexOf(Msg)>=0 then continue;
  2074. s:='';
  2075. str(Msg.MsgType,s);
  2076. Txt:='Unexpected resolver message found ['+IntToStr(Msg.Id)+'] '
  2077. +s+': ('+IntToStr(Msg.MsgNumber)+')';
  2078. if WithSourcePos then
  2079. Txt:=Txt+' '+ExtractFileName(Msg.SourcePos.FileName)+'('+IntToStr(Msg.SourcePos.Row)+','+IntToStr(Msg.SourcePos.Column)+')';
  2080. Txt:=Txt+' {'+Msg.Msg+'}';
  2081. Fail(Txt);
  2082. end;
  2083. end;
  2084. procedure TCustomTestModule.SetExpectedScannerError(Msg: string;
  2085. MsgNumber: integer);
  2086. begin
  2087. ExpectedErrorClass:=EScannerError;
  2088. ExpectedErrorMsg:=Msg;
  2089. ExpectedErrorNumber:=MsgNumber;
  2090. end;
  2091. procedure TCustomTestModule.SetExpectedParserError(Msg: string;
  2092. MsgNumber: integer);
  2093. begin
  2094. ExpectedErrorClass:=EParserError;
  2095. ExpectedErrorMsg:=Msg;
  2096. ExpectedErrorNumber:=MsgNumber;
  2097. end;
  2098. procedure TCustomTestModule.SetExpectedPasResolverError(Msg: string;
  2099. MsgNumber: integer);
  2100. begin
  2101. ExpectedErrorClass:=EPasResolve;
  2102. ExpectedErrorMsg:=Msg;
  2103. ExpectedErrorNumber:=MsgNumber;
  2104. end;
  2105. procedure TCustomTestModule.SetExpectedConverterError(Msg: string;
  2106. MsgNumber: integer);
  2107. begin
  2108. ExpectedErrorClass:=EPas2JS;
  2109. ExpectedErrorMsg:=Msg;
  2110. ExpectedErrorNumber:=MsgNumber;
  2111. end;
  2112. function TCustomTestModule.IsErrorExpected(E: Exception): boolean;
  2113. var
  2114. MsgNumber: Integer;
  2115. Msg: String;
  2116. begin
  2117. Result:=false;
  2118. if (ExpectedErrorClass=nil) or (ExpectedErrorClass<>E.ClassType) then exit;
  2119. Msg:=E.Message;
  2120. if E is EPas2JS then
  2121. MsgNumber:=EPas2JS(E).MsgNumber
  2122. else if E is EPasResolve then
  2123. MsgNumber:=EPasResolve(E).MsgNumber
  2124. else if E is EParserError then
  2125. MsgNumber:=Parser.LastMsgNumber
  2126. else if E is EScannerError then
  2127. begin
  2128. MsgNumber:=Scanner.LastMsgNumber;
  2129. Msg:=Scanner.LastMsg;
  2130. end
  2131. else
  2132. MsgNumber:=0;
  2133. Result:=(MsgNumber=ExpectedErrorNumber) and (Msg=ExpectedErrorMsg);
  2134. if Result then
  2135. SkipTests:=true;
  2136. end;
  2137. procedure TCustomTestModule.HandleScannerError(E: EScannerError);
  2138. begin
  2139. if IsErrorExpected(E) then exit;
  2140. WriteSources(Scanner.CurFilename,Scanner.CurRow,Scanner.CurColumn);
  2141. writeln('ERROR: TCustomTestModule.HandleScannerError '+E.ClassName+':'+E.Message
  2142. +' '+Scanner.CurFilename
  2143. +'('+IntToStr(Scanner.CurRow)+','+IntToStr(Scanner.CurColumn)+')');
  2144. FailException(E);
  2145. end;
  2146. procedure TCustomTestModule.HandleParserError(E: EParserError);
  2147. begin
  2148. if IsErrorExpected(E) then exit;
  2149. WriteSources(E.Filename,E.Row,E.Column);
  2150. writeln('ERROR: TCustomTestModule.HandleParserError '+E.ClassName+':'+E.Message
  2151. +' '+E.Filename+'('+IntToStr(E.Row)+','+IntToStr(E.Column)+')'
  2152. +' MainModuleScannerLine="'+Scanner.CurLine+'"'
  2153. );
  2154. FailException(E);
  2155. end;
  2156. procedure TCustomTestModule.HandlePasResolveError(E: EPasResolve);
  2157. var
  2158. P: TPasSourcePos;
  2159. begin
  2160. if IsErrorExpected(E) then exit;
  2161. P:=E.SourcePos;
  2162. WriteSources(P.FileName,P.Row,P.Column);
  2163. writeln('ERROR: TCustomTestModule.HandlePasResolveError '+E.ClassName+':'+E.Message
  2164. +' '+P.FileName+'('+IntToStr(P.Row)+','+IntToStr(P.Column)+')');
  2165. FailException(E);
  2166. end;
  2167. procedure TCustomTestModule.HandlePas2JSError(E: EPas2JS);
  2168. var
  2169. Row, Col: integer;
  2170. begin
  2171. if IsErrorExpected(E) then exit;
  2172. Engine.UnmangleSourceLineNumber(E.PasElement.SourceLinenumber,Row,Col);
  2173. WriteSources(E.PasElement.SourceFilename,Row,Col);
  2174. writeln('ERROR: TCustomTestModule.HandlePas2JSError '+E.ClassName+':'+E.Message
  2175. +' '+E.PasElement.SourceFilename
  2176. +'('+IntToStr(Row)+','+IntToStr(Col)+')');
  2177. FailException(E);
  2178. end;
  2179. procedure TCustomTestModule.HandleException(E: Exception);
  2180. begin
  2181. if E is EScannerError then
  2182. HandleScannerError(EScannerError(E))
  2183. else if E is EParserError then
  2184. HandleParserError(EParserError(E))
  2185. else if E is EPasResolve then
  2186. HandlePasResolveError(EPasResolve(E))
  2187. else if E is EPas2JS then
  2188. HandlePas2JSError(EPas2JS(E))
  2189. else
  2190. begin
  2191. if IsErrorExpected(E) then exit;
  2192. if not (E is EAssertionFailedError) then
  2193. begin
  2194. WriteSources('',0,0);
  2195. writeln('ERROR: TCustomTestModule.HandleException '+E.ClassName+':'+E.Message);
  2196. end;
  2197. FailException(E);
  2198. end;
  2199. end;
  2200. procedure TCustomTestModule.FailException(E: Exception);
  2201. var
  2202. MsgNumber: Integer;
  2203. begin
  2204. if ExpectedErrorClass<>nil then
  2205. begin
  2206. if FExpectedErrorClass=E.ClassType then
  2207. begin
  2208. if E is EPas2JS then
  2209. MsgNumber:=EPas2JS(E).MsgNumber
  2210. else if E is EPasResolve then
  2211. MsgNumber:=EPasResolve(E).MsgNumber
  2212. else if E is EParserError then
  2213. MsgNumber:=Parser.LastMsgNumber
  2214. else if E is EScannerError then
  2215. MsgNumber:=Scanner.LastMsgNumber
  2216. else
  2217. MsgNumber:=0;
  2218. AssertEquals('Expected error message ('+IntToStr(ExpectedErrorNumber)+')','{'+ExpectedErrorMsg+'}','{'+E.Message+'}');
  2219. AssertEquals('Expected {'+ExpectedErrorMsg+'}, but got msg {'+E.Message+'} number',
  2220. ExpectedErrorNumber,MsgNumber);
  2221. end else begin
  2222. AssertEquals('Wrong exception class',ExpectedErrorClass.ClassName,E.ClassName);
  2223. end;
  2224. end;
  2225. Fail(E.Message);
  2226. end;
  2227. procedure TCustomTestModule.WriteSources(const aFilename: string; aRow,
  2228. aCol: integer);
  2229. var
  2230. IsSrc: Boolean;
  2231. i, j: Integer;
  2232. SrcLines: TStringList;
  2233. Line: string;
  2234. aModule: TTestEnginePasResolver;
  2235. begin
  2236. writeln('TCustomTestModule.WriteSources File="',aFilename,'" Row=',aRow,' Col=',aCol);
  2237. for i:=0 to ResolverCount-1 do
  2238. begin
  2239. aModule:=Resolvers[i];
  2240. SrcLines:=TStringList.Create;
  2241. try
  2242. SrcLines.Text:=aModule.Source;
  2243. IsSrc:=ExtractFilename(aModule.Filename)=ExtractFileName(aFilename);
  2244. writeln('Testcode:-File="',aModule.Filename,'"----------------------------------:');
  2245. for j:=1 to SrcLines.Count do
  2246. begin
  2247. Line:=SrcLines[j-1];
  2248. if IsSrc and (j=aRow) then
  2249. begin
  2250. write('*');
  2251. Line:=LeftStr(Line,aCol-1)+'|'+copy(Line,aCol,length(Line));
  2252. end;
  2253. writeln(Format('%:4d: ',[j]),Line);
  2254. end;
  2255. finally
  2256. SrcLines.Free;
  2257. end;
  2258. end;
  2259. end;
  2260. function TCustomTestModule.IndexOfResolver(const Filename: string): integer;
  2261. var
  2262. i: Integer;
  2263. begin
  2264. for i:=0 to ResolverCount-1 do
  2265. if Filename=Resolvers[i].Filename then exit(i);
  2266. Result:=-1;
  2267. end;
  2268. function TCustomTestModule.GetResolver(const Filename: string
  2269. ): TTestEnginePasResolver;
  2270. var
  2271. i: Integer;
  2272. begin
  2273. i:=IndexOfResolver(Filename);
  2274. if i<0 then exit(nil);
  2275. Result:=Resolvers[i];
  2276. end;
  2277. function TCustomTestModule.GetDefaultNamespace: string;
  2278. var
  2279. C: TClass;
  2280. begin
  2281. Result:='';
  2282. if FModule=nil then exit;
  2283. C:=FModule.ClassType;
  2284. if (C=TPasProgram) or (C=TPasLibrary) or (C=TPasPackage) then
  2285. Result:=Engine.DefaultNameSpace;
  2286. end;
  2287. constructor TCustomTestModule.Create;
  2288. begin
  2289. inherited Create;
  2290. FHintMsgs:=TObjectList.Create(true);
  2291. FHintMsgsGood:=TFPList.Create;
  2292. end;
  2293. destructor TCustomTestModule.Destroy;
  2294. begin
  2295. FreeAndNil(FHintMsgs);
  2296. FreeAndNil(FHintMsgsGood);
  2297. inherited Destroy;
  2298. end;
  2299. { TTestModule }
  2300. procedure TTestModule.TestReservedWords;
  2301. var
  2302. i: integer;
  2303. begin
  2304. for i:=low(JSReservedWords) to High(JSReservedWords)-1 do
  2305. if CompareStr(JSReservedWords[i],JSReservedWords[i+1])>=0 then
  2306. Fail('20170203135442 '+JSReservedWords[i]+' >= '+JSReservedWords[i+1]);
  2307. for i:=low(JSReservedGlobalWords) to High(JSReservedGlobalWords)-1 do
  2308. if CompareStr(JSReservedGlobalWords[i],JSReservedGlobalWords[i+1])>=0 then
  2309. Fail('20170203135443 '+JSReservedGlobalWords[i]+' >= '+JSReservedGlobalWords[i+1]);
  2310. end;
  2311. procedure TTestModule.TestEmptyProgram;
  2312. begin
  2313. StartProgram(false);
  2314. Add('begin');
  2315. ConvertProgram;
  2316. CheckSource('TestEmptyProgram','','');
  2317. end;
  2318. procedure TTestModule.TestEmptyProgramUseStrict;
  2319. begin
  2320. Converter.Options:=Converter.Options+[coUseStrict];
  2321. StartProgram(false);
  2322. Add('begin');
  2323. ConvertProgram;
  2324. CheckSource('TestEmptyProgramUseStrict','','');
  2325. end;
  2326. procedure TTestModule.TestEmptyUnit;
  2327. begin
  2328. StartUnit(false);
  2329. Add('interface');
  2330. Add('implementation');
  2331. ConvertUnit;
  2332. CheckSource('TestEmptyUnit',
  2333. LinesToStr([
  2334. ]),
  2335. '');
  2336. end;
  2337. procedure TTestModule.TestEmptyUnitUseStrict;
  2338. begin
  2339. Converter.Options:=Converter.Options+[coUseStrict];
  2340. StartUnit(false);
  2341. Add('interface');
  2342. Add('implementation');
  2343. ConvertUnit;
  2344. CheckSource('TestEmptyUnitUseStrict',
  2345. LinesToStr([
  2346. ''
  2347. ]),
  2348. '');
  2349. end;
  2350. procedure TTestModule.TestDottedUnitNames;
  2351. begin
  2352. AddModuleWithIntfImplSrc('NS1.Unit2.pas',
  2353. LinesToStr([
  2354. 'var iV: longint;'
  2355. ]),
  2356. '');
  2357. FFilename:='ns1.test1.pp';
  2358. StartProgram(true);
  2359. Add('uses unIt2;');
  2360. Add('var');
  2361. Add(' i: longint;');
  2362. Add('begin');
  2363. Add(' i:=iv;');
  2364. Add(' i:=uNit2.iv;');
  2365. Add(' i:=Ns1.TEst1.i;');
  2366. ConvertProgram;
  2367. CheckSource('TestDottedUnitNames',
  2368. LinesToStr([
  2369. 'this.i = 0;',
  2370. '']),
  2371. LinesToStr([ // this.$init
  2372. '$mod.i = pas["NS1.Unit2"].iV;',
  2373. '$mod.i = pas["NS1.Unit2"].iV;',
  2374. '$mod.i = $mod.i;',
  2375. '']) );
  2376. end;
  2377. procedure TTestModule.TestDottedUnitNameImpl;
  2378. begin
  2379. AddModuleWithIntfImplSrc('TEST.UnitA.pas',
  2380. LinesToStr([
  2381. 'type',
  2382. ' TObject = class end;',
  2383. ' TTestA = class',
  2384. ' end;'
  2385. ]),
  2386. LinesToStr(['uses TEST.UnitB;'])
  2387. );
  2388. AddModuleWithIntfImplSrc('TEST.UnitB.pas',
  2389. LinesToStr([
  2390. 'uses TEST.UnitA;',
  2391. 'type TTestB = class(TTestA);'
  2392. ]),
  2393. ''
  2394. );
  2395. StartProgram(true);
  2396. Add('uses TEST.UnitA;');
  2397. Add('begin');
  2398. ConvertProgram;
  2399. CheckSource('TestDottedUnitNameImpl',
  2400. LinesToStr([
  2401. '']),
  2402. LinesToStr([ // this.$init
  2403. '']) );
  2404. CheckUnit('TEST.UnitA.pas',
  2405. LinesToStr([
  2406. 'rtl.module("TEST.UnitA", ["system"], function () {',
  2407. ' var $mod = this;',
  2408. ' rtl.createClass(this, "TObject", null, function () {',
  2409. ' this.$init = function () {',
  2410. ' };',
  2411. ' this.$final = function () {',
  2412. ' };',
  2413. ' });',
  2414. ' rtl.createClass(this, "TTestA", this.TObject, function () {',
  2415. ' });',
  2416. '}, ["TEST.UnitB"]);'
  2417. ]));
  2418. CheckUnit('TEST.UnitB.pas',
  2419. LinesToStr([
  2420. 'rtl.module("TEST.UnitB", ["system","TEST.UnitA"], function () {',
  2421. ' var $mod = this;',
  2422. ' rtl.createClass(this, "TTestB", pas["TEST.UnitA"].TTestA, function () {',
  2423. ' });',
  2424. '});'
  2425. ]));
  2426. end;
  2427. procedure TTestModule.TestDottedUnitExpr;
  2428. begin
  2429. AddModuleWithIntfImplSrc('NS2.SubNs2.Unit2.pas',
  2430. LinesToStr([
  2431. 'procedure DoIt;'
  2432. ]),
  2433. 'procedure DoIt; begin end;');
  2434. FFilename:='Ns1.SubNs1.Test1.pp';
  2435. StartProgram(true);
  2436. Add('uses Ns2.sUbnS2.unIt2;');
  2437. Add('var');
  2438. Add(' i: longint;');
  2439. Add('begin');
  2440. Add(' ns2.subns2.unit2.doit;');
  2441. Add(' i:=Ns1.SubNS1.TEst1.i;');
  2442. ConvertProgram;
  2443. CheckSource('TestDottedUnitExpr',
  2444. LinesToStr([
  2445. 'this.i = 0;',
  2446. '']),
  2447. LinesToStr([ // this.$init
  2448. 'pas["NS2.SubNs2.Unit2"].DoIt();',
  2449. '$mod.i = $mod.i;',
  2450. '']) );
  2451. end;
  2452. procedure TTestModule.Test_ModeFPCFail;
  2453. begin
  2454. StartProgram(false);
  2455. Add('{$mode FPC}');
  2456. Add('begin');
  2457. SetExpectedScannerError('Invalid mode: "FPC"',nErrInvalidMode);
  2458. ConvertProgram;
  2459. end;
  2460. procedure TTestModule.Test_ModeSwitchCBlocksFail;
  2461. begin
  2462. StartProgram(false);
  2463. Add('{$modeswitch cblocks-}');
  2464. Add('begin');
  2465. ConvertProgram;
  2466. CheckHint(mtWarning,nErrInvalidModeSwitch,'Warning: test1.pp(3,23) : Invalid mode switch: "cblocks"');
  2467. CheckResolverUnexpectedHints();
  2468. end;
  2469. procedure TTestModule.TestUnit_UseSystem;
  2470. begin
  2471. StartUnit(true);
  2472. Add([
  2473. 'interface',
  2474. 'var i: integer;',
  2475. 'implementation']);
  2476. ConvertUnit;
  2477. CheckSource('TestUnit_UseSystem',
  2478. LinesToStr([
  2479. 'this.i = 0;',
  2480. '']),
  2481. LinesToStr([
  2482. '']) );
  2483. end;
  2484. procedure TTestModule.TestUnit_Intf1Impl2Intf1;
  2485. begin
  2486. AddModuleWithIntfImplSrc('unit1.pp',
  2487. LinesToStr([
  2488. 'type number = longint;']),
  2489. LinesToStr([
  2490. 'uses test1;',
  2491. 'procedure DoIt;',
  2492. 'begin',
  2493. ' i:=3;',
  2494. 'end;']));
  2495. StartUnit(true);
  2496. Add([
  2497. 'interface',
  2498. 'uses unit1;',
  2499. 'var i: number;',
  2500. 'implementation']);
  2501. ConvertUnit;
  2502. CheckSource('TestUnit_Intf1Impl2Intf1',
  2503. LinesToStr([
  2504. 'this.i = 0;',
  2505. '']),
  2506. LinesToStr([
  2507. '']) );
  2508. end;
  2509. procedure TTestModule.TestIncludeVersion;
  2510. begin
  2511. StartProgram(false);
  2512. Add([
  2513. 'var',
  2514. ' s: string;',
  2515. ' i: word;',
  2516. 'begin',
  2517. ' s:={$I %line%};',
  2518. ' i:={$I %linenum%};',
  2519. ' s:={$I %currentroutine%};',
  2520. ' s:={$I %pas2jsversion%};',
  2521. ' s:={$I %pas2jstarget%};',
  2522. ' s:={$I %pas2jstargetos%};',
  2523. ' s:={$I %pas2jstargetcpu%};',
  2524. ' s:={$I %file%};',
  2525. '']);
  2526. ConvertProgram;
  2527. CheckSource('TestIncludeVersion',
  2528. LinesToStr([
  2529. 'this.s="";',
  2530. 'this.i = 0;']),
  2531. LinesToStr([
  2532. '$mod.s = "7";',
  2533. '$mod.i = 8;',
  2534. '$mod.s = "<anonymous>";',
  2535. '$mod.s = "Comp.Ver.tcmodules";',
  2536. '$mod.s = "Browser";',
  2537. '$mod.s = "Browser";',
  2538. '$mod.s = "ECMAScript5";',
  2539. '$mod.s = "test1.pp";',
  2540. '']));
  2541. end;
  2542. procedure TTestModule.TestVarInt;
  2543. begin
  2544. StartProgram(false);
  2545. Add('var MyI: longint;');
  2546. Add('begin');
  2547. ConvertProgram;
  2548. CheckSource('TestVarInt','this.MyI=0;','');
  2549. end;
  2550. procedure TTestModule.TestVarBaseTypes;
  2551. begin
  2552. StartProgram(false);
  2553. Add('var');
  2554. Add(' i: longint;');
  2555. Add(' s: string;');
  2556. Add(' c: char;');
  2557. Add(' b: boolean;');
  2558. Add(' d: double;');
  2559. Add(' i2: longint = 3;');
  2560. Add(' s2: string = ''foo'';');
  2561. Add(' c2: char = ''4'';');
  2562. Add(' b2: boolean = true;');
  2563. Add(' d2: double = 5.6;');
  2564. Add(' i3: longint = $707;');
  2565. Add(' i4: nativeint = 9007199254740991;');
  2566. Add(' i5: nativeint = -9007199254740991-1;');
  2567. Add(' i6: nativeint = $fffffffffffff;');
  2568. Add(' i7: nativeint = -$fffffffffffff-1;');
  2569. Add(' i8: byte = 00;');
  2570. Add(' u8: nativeuint = $fffffffffffff;');
  2571. Add(' u9: nativeuint = $0000000000000;');
  2572. Add(' u10: nativeuint = $00ff00;');
  2573. Add('begin');
  2574. ConvertProgram;
  2575. CheckSource('TestVarBaseTypes',
  2576. LinesToStr([
  2577. 'this.i = 0;',
  2578. 'this.s = "";',
  2579. 'this.c = "";',
  2580. 'this.b = false;',
  2581. 'this.d = 0.0;',
  2582. 'this.i2 = 3;',
  2583. 'this.s2 = "foo";',
  2584. 'this.c2 = "4";',
  2585. 'this.b2 = true;',
  2586. 'this.d2 = 5.6;',
  2587. 'this.i3 = 0x707;',
  2588. 'this.i4 = 9007199254740991;',
  2589. 'this.i5 = -9007199254740991-1;',
  2590. 'this.i6 = 0xfffffffffffff;',
  2591. 'this.i7 =-0xfffffffffffff-1;',
  2592. 'this.i8 = 0;',
  2593. 'this.u8 = 0xfffffffffffff;',
  2594. 'this.u9 = 0x0;',
  2595. 'this.u10 = 0xff00;'
  2596. ]),
  2597. '');
  2598. end;
  2599. procedure TTestModule.TestBaseTypeSingleFail;
  2600. begin
  2601. StartProgram(false);
  2602. Add('var s: single;');
  2603. SetExpectedPasResolverError('identifier not found "single"',PasResolveEval.nIdentifierNotFound);
  2604. ConvertProgram;
  2605. end;
  2606. procedure TTestModule.TestBaseTypeExtendedFail;
  2607. begin
  2608. StartProgram(false);
  2609. Add('var e: extended;');
  2610. SetExpectedPasResolverError('identifier not found "extended"',PasResolveEval.nIdentifierNotFound);
  2611. ConvertProgram;
  2612. end;
  2613. procedure TTestModule.TestConstBaseTypes;
  2614. begin
  2615. StartProgram(false);
  2616. Add('const');
  2617. Add(' i: longint = 3;');
  2618. Add(' s: string = ''foo'';');
  2619. Add(' c: char = ''4'';');
  2620. Add(' b: boolean = true;');
  2621. Add(' d: double = 5.6;');
  2622. Add(' e = low(word);');
  2623. Add(' f = high(word);');
  2624. Add('begin');
  2625. ConvertProgram;
  2626. CheckSource('TestVarBaseTypes',
  2627. LinesToStr([
  2628. 'this.i=3;',
  2629. 'this.s="foo";',
  2630. 'this.c="4";',
  2631. 'this.b=true;',
  2632. 'this.d=5.6;',
  2633. 'this.e = 0;',
  2634. 'this.f = 65535;'
  2635. ]),
  2636. '');
  2637. end;
  2638. procedure TTestModule.TestAliasTypeRef;
  2639. begin
  2640. StartProgram(false);
  2641. Add('type');
  2642. Add(' a=longint;');
  2643. Add(' b=a;');
  2644. Add('var');
  2645. Add(' c: A;');
  2646. Add(' d: B;');
  2647. Add('begin');
  2648. ConvertProgram;
  2649. CheckSource('TestAliasTypeRef',
  2650. LinesToStr([ // statements
  2651. 'this.c = 0;',
  2652. 'this.d = 0;'
  2653. ]),
  2654. LinesToStr([ // this.$main
  2655. ''
  2656. ]));
  2657. end;
  2658. procedure TTestModule.TestTypeCast_BaseTypes;
  2659. begin
  2660. StartProgram(false);
  2661. Add([
  2662. 'var',
  2663. ' i: longint;',
  2664. ' b: boolean;',
  2665. ' d: double;',
  2666. ' s: string;',
  2667. ' c: char;',
  2668. 'begin',
  2669. ' i:=longint(i);',
  2670. ' i:=longint(b);',
  2671. ' b:=boolean(b);',
  2672. ' b:=boolean(i);',
  2673. ' d:=double(d);',
  2674. ' d:=double(i);',
  2675. ' s:=string(s);',
  2676. ' s:=string(c);',
  2677. ' c:=char(c);',
  2678. ' c:=char(i);',
  2679. ' c:=char(65);',
  2680. ' c:=char(#10);',
  2681. ' c:=char(#$E000);',
  2682. '']);
  2683. ConvertProgram;
  2684. CheckSource('TestAliasTypeRef',
  2685. LinesToStr([ // statements
  2686. 'this.i = 0;',
  2687. 'this.b = false;',
  2688. 'this.d = 0.0;',
  2689. 'this.s = "";',
  2690. 'this.c = "";',
  2691. '']),
  2692. LinesToStr([ // this.$main
  2693. '$mod.i = $mod.i;',
  2694. '$mod.i = ($mod.b ? 1 : 0);',
  2695. '$mod.b = $mod.b;',
  2696. '$mod.b = $mod.i != 0;',
  2697. '$mod.d = $mod.d;',
  2698. '$mod.d = $mod.i;',
  2699. '$mod.s = $mod.s;',
  2700. '$mod.s = $mod.c;',
  2701. '$mod.c = $mod.c;',
  2702. '$mod.c = String.fromCharCode($mod.i);',
  2703. '$mod.c = "A";',
  2704. '$mod.c = "\n";',
  2705. '$mod.c = "";',
  2706. '']));
  2707. end;
  2708. procedure TTestModule.TestTypeCast_AliasBaseTypes;
  2709. begin
  2710. StartProgram(false);
  2711. Add('type');
  2712. Add(' integer = longint;');
  2713. Add(' TYesNo = boolean;');
  2714. Add(' TFloat = double;');
  2715. Add(' TCaption = string;');
  2716. Add(' TChar = char;');
  2717. Add('var');
  2718. Add(' i: integer;');
  2719. Add(' b: TYesNo;');
  2720. Add(' d: TFloat;');
  2721. Add(' s: TCaption;');
  2722. Add(' c: TChar;');
  2723. Add('begin');
  2724. Add(' i:=integer(i);');
  2725. Add(' i:=integer(b);');
  2726. Add(' b:=TYesNo(b);');
  2727. Add(' b:=TYesNo(i);');
  2728. Add(' d:=TFloat(d);');
  2729. Add(' d:=TFloat(i);');
  2730. Add(' s:=TCaption(s);');
  2731. Add(' s:=TCaption(c);');
  2732. Add(' c:=TChar(c);');
  2733. ConvertProgram;
  2734. CheckSource('TestAliasTypeRef',
  2735. LinesToStr([ // statements
  2736. 'this.i = 0;',
  2737. 'this.b = false;',
  2738. 'this.d = 0.0;',
  2739. 'this.s = "";',
  2740. 'this.c = "";',
  2741. '']),
  2742. LinesToStr([ // this.$main
  2743. '$mod.i = $mod.i;',
  2744. '$mod.i = ($mod.b ? 1 : 0);',
  2745. '$mod.b = $mod.b;',
  2746. '$mod.b = $mod.i != 0;',
  2747. '$mod.d = $mod.d;',
  2748. '$mod.d = $mod.i;',
  2749. '$mod.s = $mod.s;',
  2750. '$mod.s = $mod.c;',
  2751. '$mod.c = $mod.c;',
  2752. '']));
  2753. end;
  2754. procedure TTestModule.TestEmptyProc;
  2755. begin
  2756. StartProgram(false);
  2757. Add('procedure Test;');
  2758. Add('begin');
  2759. Add('end;');
  2760. Add('begin');
  2761. ConvertProgram;
  2762. CheckSource('TestEmptyProc',
  2763. LinesToStr([ // statements
  2764. 'this.Test = function () {',
  2765. '};'
  2766. ]),
  2767. LinesToStr([ // this.$main
  2768. ''
  2769. ]));
  2770. end;
  2771. procedure TTestModule.TestProcOneParam;
  2772. begin
  2773. StartProgram(false);
  2774. Add('procedure ProcA(i: longint);');
  2775. Add('begin');
  2776. Add('end;');
  2777. Add('begin');
  2778. Add(' PROCA(3);');
  2779. ConvertProgram;
  2780. CheckSource('TestProcOneParam',
  2781. LinesToStr([ // statements
  2782. 'this.ProcA = function (i) {',
  2783. '};'
  2784. ]),
  2785. LinesToStr([ // this.$main
  2786. '$mod.ProcA(3);'
  2787. ]));
  2788. end;
  2789. procedure TTestModule.TestFunctionWithoutParams;
  2790. begin
  2791. StartProgram(false);
  2792. Add('function FuncA: longint;');
  2793. Add('begin');
  2794. Add('end;');
  2795. Add('var i: longint;');
  2796. Add('begin');
  2797. Add(' I:=FUNCA();');
  2798. Add(' I:=FUNCA;');
  2799. Add(' FUNCA();');
  2800. Add(' FUNCA;');
  2801. ConvertProgram;
  2802. CheckSource('TestProcWithoutParams',
  2803. LinesToStr([ // statements
  2804. 'this.FuncA = function () {',
  2805. ' var Result = 0;',
  2806. ' return Result;',
  2807. '};',
  2808. 'this.i=0;'
  2809. ]),
  2810. LinesToStr([ // this.$main
  2811. '$mod.i=$mod.FuncA();',
  2812. '$mod.i=$mod.FuncA();',
  2813. '$mod.FuncA();',
  2814. '$mod.FuncA();'
  2815. ]));
  2816. end;
  2817. procedure TTestModule.TestProcedureWithoutParams;
  2818. begin
  2819. StartProgram(false);
  2820. Add('procedure ProcA;');
  2821. Add('begin');
  2822. Add('end;');
  2823. Add('begin');
  2824. Add(' PROCA();');
  2825. Add(' PROCA;');
  2826. ConvertProgram;
  2827. CheckSource('TestProcWithoutParams',
  2828. LinesToStr([ // statements
  2829. 'this.ProcA = function () {',
  2830. '};'
  2831. ]),
  2832. LinesToStr([ // this.$main
  2833. '$mod.ProcA();',
  2834. '$mod.ProcA();'
  2835. ]));
  2836. end;
  2837. procedure TTestModule.TestIncDec;
  2838. begin
  2839. StartProgram(false);
  2840. Add([
  2841. 'procedure DoIt(var i: longint);',
  2842. 'begin',
  2843. ' inc(i);',
  2844. ' inc(i,2);',
  2845. 'end;',
  2846. 'var',
  2847. ' Bar: longint;',
  2848. 'begin',
  2849. ' inc(bar);',
  2850. ' inc(bar,2);',
  2851. ' dec(bar);',
  2852. ' dec(bar,3);',
  2853. '']);
  2854. ConvertProgram;
  2855. CheckSource('TestIncDec',
  2856. LinesToStr([ // statements
  2857. 'this.DoIt = function (i) {',
  2858. ' i.set(i.get()+1);',
  2859. ' i.set(i.get()+2);',
  2860. '};',
  2861. 'this.Bar = 0;'
  2862. ]),
  2863. LinesToStr([ // this.$main
  2864. '$mod.Bar+=1;',
  2865. '$mod.Bar+=2;',
  2866. '$mod.Bar-=1;',
  2867. '$mod.Bar-=3;'
  2868. ]));
  2869. end;
  2870. procedure TTestModule.TestLoHiFpcMode;
  2871. begin
  2872. StartProgram(false);
  2873. Add([
  2874. '{$mode objfpc}',
  2875. 'const',
  2876. ' LoByte1 = Lo(Word($1234));',
  2877. ' HiByte1 = Hi(Word($1234));',
  2878. ' LoByte2 = Lo(SmallInt($1234));',
  2879. ' HiByte2 = Hi(SmallInt($1234));',
  2880. ' LoWord1 = Lo($1234CDEF);',
  2881. ' HiWord1 = Hi($1234CDEF);',
  2882. ' LoWord2 = Lo(-$1234CDEF);',
  2883. ' HiWord2 = Hi(-$1234CDEF);',
  2884. ' lo4:byte=lo(byte($34));',
  2885. ' hi4:byte=hi(byte($34));',
  2886. ' lo5:byte=lo(shortint(-$34));',
  2887. ' hi5:byte=hi(shortint(-$34));',
  2888. ' lo6:longword=lo($123456789ABCD);',
  2889. ' hi6:longword=hi($123456789ABCD);',
  2890. ' lo7:longword=lo(-$123456789ABCD);',
  2891. ' hi7:longword=hi(-$123456789ABCD);',
  2892. 'var',
  2893. ' b: Byte;',
  2894. ' ss: shortint;',
  2895. ' w: Word;',
  2896. ' si: SmallInt;',
  2897. ' lw: LongWord;',
  2898. ' li: LongInt;',
  2899. ' b2: Byte;',
  2900. ' ni: nativeint;',
  2901. 'begin',
  2902. ' w := $1234;',
  2903. ' ss := -$12;',
  2904. ' b := lo(ss);',
  2905. ' b := HI(ss);',
  2906. ' b := lo(w);',
  2907. ' b := HI(w);',
  2908. ' b2 := lo(b);',
  2909. ' b2 := hi(b);',
  2910. ' lw := $1234CDEF;',
  2911. ' w := lo(lw);',
  2912. ' w := hi(lw);',
  2913. ' ni := $123456789ABCD;',
  2914. ' lw := lo(ni);',
  2915. ' lw := hi(ni);',
  2916. '']);
  2917. ConvertProgram;
  2918. CheckSource('TestLoHiFpcMode',
  2919. LinesToStr([ // statements
  2920. 'this.LoByte1 = 0x1234 & 0xFF;',
  2921. 'this.HiByte1 = (0x1234 >> 8) & 0xFF;',
  2922. 'this.LoByte2 = 0x1234 & 0xFF;',
  2923. 'this.HiByte2 = (0x1234 >> 8) & 0xFF;',
  2924. 'this.LoWord1 = 0x1234CDEF & 0xFFFF;',
  2925. 'this.HiWord1 = (0x1234CDEF >> 16) & 0xFFFF;',
  2926. 'this.LoWord2 = -0x1234CDEF & 0xFFFF;',
  2927. 'this.HiWord2 = (-0x1234CDEF >> 16) & 0xFFFF;',
  2928. 'this.lo4 = 0x34 & 0xF;',
  2929. 'this.hi4 = (0x34 >> 4) & 0xF;',
  2930. 'this.lo5 = (((-0x34 & 255) << 24) >> 24) & 0xFF;',
  2931. 'this.hi5 = ((((-0x34 & 255) << 24) >> 24) >> 8) & 0xFF;',
  2932. 'this.lo6 = 0x123456789ABCD >>> 0;',
  2933. 'this.hi6 = 74565 >>> 0;',
  2934. 'this.lo7 = -0x123456789ABCD >>> 0;',
  2935. 'this.hi7 = Math.floor(-0x123456789ABCD / 4294967296) >>> 0;',
  2936. 'this.b = 0;',
  2937. 'this.ss = 0;',
  2938. 'this.w = 0;',
  2939. 'this.si = 0;',
  2940. 'this.lw = 0;',
  2941. 'this.li = 0;',
  2942. 'this.b2 = 0;',
  2943. 'this.ni = 0;',
  2944. '']),
  2945. LinesToStr([ // this.$main
  2946. '$mod.w = 0x1234;',
  2947. '$mod.ss = -0x12;',
  2948. '$mod.b = $mod.ss & 0xFF;',
  2949. '$mod.b = ($mod.ss >> 8) & 0xFF;',
  2950. '$mod.b = $mod.w & 0xFF;',
  2951. '$mod.b = ($mod.w >> 8) & 0xFF;',
  2952. '$mod.b2 = $mod.b & 0xF;',
  2953. '$mod.b2 = ($mod.b >> 4) & 0xF;',
  2954. '$mod.lw = 0x1234CDEF;',
  2955. '$mod.w = $mod.lw & 0xFFFF;',
  2956. '$mod.w = ($mod.lw >> 16) & 0xFFFF;',
  2957. '$mod.ni = 0x123456789ABCD;',
  2958. '$mod.lw = $mod.ni >>> 0;',
  2959. '$mod.lw = Math.floor($mod.ni / 4294967296) >>> 0;',
  2960. '']));
  2961. end;
  2962. procedure TTestModule.TestLoHiDelphiMode;
  2963. begin
  2964. StartProgram(false);
  2965. Add([
  2966. '{$mode delphi}',
  2967. 'const',
  2968. ' LoByte1 = Lo(Word($1234));',
  2969. ' HiByte1 = Hi(Word($1234));',
  2970. ' LoByte2 = Lo(SmallInt($1234));',
  2971. ' HiByte2 = Hi(SmallInt($1234));',
  2972. ' LoByte3 = Lo($1234CDEF);',
  2973. ' HiByte3 = Hi($1234CDEF);',
  2974. ' LoByte4 = Lo(-$1234CDEF);',
  2975. ' HiByte4 = Hi(-$1234CDEF);',
  2976. 'var',
  2977. ' b: Byte;',
  2978. ' w: Word;',
  2979. ' si: SmallInt;',
  2980. ' lw: LongWord;',
  2981. ' li: LongInt;',
  2982. 'begin',
  2983. ' w := $1234;',
  2984. ' b := lo(w);',
  2985. ' b := HI(w);',
  2986. ' lw := $1234CDEF;',
  2987. ' b := lo(lw);',
  2988. ' b := hi(lw);',
  2989. '']);
  2990. ConvertProgram;
  2991. CheckSource('TestLoHiDelphiMode',
  2992. LinesToStr([ // statements
  2993. 'this.LoByte1 = 0x1234 & 0xFF;',
  2994. 'this.HiByte1 = (0x1234 >> 8) & 0xFF;',
  2995. 'this.LoByte2 = 0x1234 & 0xFF;',
  2996. 'this.HiByte2 = (0x1234 >> 8) & 0xFF;',
  2997. 'this.LoByte3 = 0x1234CDEF & 0xFF;',
  2998. 'this.HiByte3 = (0x1234CDEF >> 8) & 0xFF;',
  2999. 'this.LoByte4 = -0x1234CDEF & 0xFF;',
  3000. 'this.HiByte4 = (-0x1234CDEF >> 8) & 0xFF;',
  3001. 'this.b = 0;',
  3002. 'this.w = 0;',
  3003. 'this.si = 0;',
  3004. 'this.lw = 0;',
  3005. 'this.li = 0;'
  3006. ]),
  3007. LinesToStr([ // this.$main
  3008. '$mod.w = 0x1234;',
  3009. '$mod.b = $mod.w & 0xFF;',
  3010. '$mod.b = ($mod.w >> 8) & 0xFF;',
  3011. '$mod.lw = 0x1234CDEF;',
  3012. '$mod.b = $mod.lw & 0xFF;',
  3013. '$mod.b = ($mod.lw >> 8) & 0xFF;'
  3014. ]));
  3015. end;
  3016. procedure TTestModule.TestAssignments;
  3017. begin
  3018. StartProgram(false);
  3019. Parser.Options:=Parser.Options+[po_cassignments];
  3020. Add('var');
  3021. Add(' Bar:longint;');
  3022. Add('begin');
  3023. Add(' bar:=3;');
  3024. Add(' bar+=4;');
  3025. Add(' bar-=5;');
  3026. Add(' bar*=6;');
  3027. ConvertProgram;
  3028. CheckSource('TestAssignments',
  3029. LinesToStr([ // statements
  3030. 'this.Bar = 0;'
  3031. ]),
  3032. LinesToStr([ // this.$main
  3033. '$mod.Bar=3;',
  3034. '$mod.Bar+=4;',
  3035. '$mod.Bar-=5;',
  3036. '$mod.Bar*=6;'
  3037. ]));
  3038. end;
  3039. procedure TTestModule.TestArithmeticOperators1;
  3040. begin
  3041. StartProgram(false);
  3042. Add('var');
  3043. Add(' vA,vB,vC:longint;');
  3044. Add('begin');
  3045. Add(' va:=1;');
  3046. Add(' vb:=va+va;');
  3047. Add(' vb:=va div vb;');
  3048. Add(' vb:=va mod vb;');
  3049. Add(' vb:=va+va*vb+va div vb;');
  3050. Add(' vc:=-va;');
  3051. Add(' va:=va-vb;');
  3052. Add(' vb:=va;');
  3053. Add(' if va<vb then vc:=va else vc:=vb;');
  3054. ConvertProgram;
  3055. CheckSource('TestArithmeticOperators1',
  3056. LinesToStr([ // statements
  3057. 'this.vA = 0;',
  3058. 'this.vB = 0;',
  3059. 'this.vC = 0;'
  3060. ]),
  3061. LinesToStr([ // this.$main
  3062. '$mod.vA = 1;',
  3063. '$mod.vB = $mod.vA + $mod.vA;',
  3064. '$mod.vB = rtl.trunc($mod.vA / $mod.vB);',
  3065. '$mod.vB = $mod.vA % $mod.vB;',
  3066. '$mod.vB = $mod.vA + ($mod.vA * $mod.vB) + rtl.trunc($mod.vA / $mod.vB);',
  3067. '$mod.vC = -$mod.vA;',
  3068. '$mod.vA = $mod.vA - $mod.vB;',
  3069. '$mod.vB = $mod.vA;',
  3070. 'if ($mod.vA < $mod.vB){ $mod.vC = $mod.vA } else $mod.vC = $mod.vB;'
  3071. ]));
  3072. end;
  3073. procedure TTestModule.TestMultiAdd;
  3074. begin
  3075. StartProgram(false);
  3076. Add([
  3077. 'function Fly: string; external name ''fly'';',
  3078. 'function TryEncodeDate(Year, Month, Day: Word): Boolean;',
  3079. 'var',
  3080. ' Date: double;',
  3081. 'begin',
  3082. ' Result:=(Year>0) and (Year<10000) and',
  3083. ' (Month >= 1) and (Month<=12) and',
  3084. ' (Day>0) and (Day<=31);',
  3085. ' Date := (146097*Year) SHR 2 + (1461*Year) SHR 2 + (153*LongWord(Month)+2) DIV 5 + LongWord(Day);',
  3086. 'end;',
  3087. 'var s: string;',
  3088. 'begin',
  3089. ' s:=''a''+''b''+''c''+''d'';',
  3090. ' s:=s+Fly+''e'';',
  3091. ' s:=Fly+Fly+Fly;',
  3092. '']);
  3093. ConvertProgram;
  3094. CheckSource('TestMultiAdd',
  3095. LinesToStr([ // statements
  3096. 'this.TryEncodeDate = function (Year, Month, Day) {',
  3097. ' var Result = false;',
  3098. ' var date = 0.0;',
  3099. ' Result = (Year > 0) && (Year < 10000) && (Month >= 1) && (Month <= 12) && (Day > 0) && (Day <= 31);',
  3100. ' date = ((146097 * Year) >>> 2) + ((1461 * Year) >>> 2) + rtl.trunc(((153 * Month) + 2) / 5) + Day;',
  3101. ' return Result;',
  3102. '};',
  3103. 'this.s = "";',
  3104. '']),
  3105. LinesToStr([ // this.$main
  3106. '$mod.s = "a" + "b" + "c" + "d";',
  3107. '$mod.s = $mod.s + fly() + "e";',
  3108. '$mod.s = fly() + fly() + fly();',
  3109. '']));
  3110. end;
  3111. procedure TTestModule.TestLogicalOperators;
  3112. begin
  3113. StartProgram(false);
  3114. Add('var');
  3115. Add(' vA,vB,vC:boolean;');
  3116. Add('begin');
  3117. Add(' va:=vb and vc;');
  3118. Add(' va:=vb or vc;');
  3119. Add(' va:=vb xor vc;');
  3120. Add(' va:=true and vc;');
  3121. Add(' va:=(vb and vc) or (va and vb);');
  3122. Add(' va:=not vb;');
  3123. ConvertProgram;
  3124. CheckSource('TestLogicalOperators',
  3125. LinesToStr([ // statements
  3126. 'this.vA = false;',
  3127. 'this.vB = false;',
  3128. 'this.vC = false;'
  3129. ]),
  3130. LinesToStr([ // this.$main
  3131. '$mod.vA = $mod.vB && $mod.vC;',
  3132. '$mod.vA = $mod.vB || $mod.vC;',
  3133. '$mod.vA = $mod.vB ^ $mod.vC;',
  3134. '$mod.vA = true && $mod.vC;',
  3135. '$mod.vA = ($mod.vB && $mod.vC) || ($mod.vA && $mod.vB);',
  3136. '$mod.vA = !$mod.vB;'
  3137. ]));
  3138. end;
  3139. procedure TTestModule.TestBitwiseOperators;
  3140. begin
  3141. StartProgram(false);
  3142. Add([
  3143. 'var',
  3144. ' vA,vB,vC:longint;',
  3145. ' X,Y,Z: nativeint;',
  3146. 'begin',
  3147. ' va:=vb and vc;',
  3148. ' va:=vb or vc;',
  3149. ' va:=vb xor vc;',
  3150. ' va:=vb shl vc;',
  3151. ' va:=vb shr vc;',
  3152. ' va:=3 and vc;',
  3153. ' va:=(vb and vc) or (va and vb);',
  3154. ' va:=not vb;',
  3155. ' X:=Y and Z;',
  3156. ' X:=Y and va;',
  3157. ' X:=Y or Z;',
  3158. ' X:=Y or va;',
  3159. ' X:=Y xor Z;',
  3160. ' X:=Y xor va;',
  3161. '']);
  3162. ConvertProgram;
  3163. CheckSource('TestBitwiseOperators',
  3164. LinesToStr([ // statements
  3165. 'this.vA = 0;',
  3166. 'this.vB = 0;',
  3167. 'this.vC = 0;',
  3168. 'this.X = 0;',
  3169. 'this.Y = 0;',
  3170. 'this.Z = 0;',
  3171. '']),
  3172. LinesToStr([ // this.$main
  3173. '$mod.vA = $mod.vB & $mod.vC;',
  3174. '$mod.vA = $mod.vB | $mod.vC;',
  3175. '$mod.vA = $mod.vB ^ $mod.vC;',
  3176. '$mod.vA = $mod.vB << $mod.vC;',
  3177. '$mod.vA = $mod.vB >>> $mod.vC;',
  3178. '$mod.vA = 3 & $mod.vC;',
  3179. '$mod.vA = ($mod.vB & $mod.vC) | ($mod.vA & $mod.vB);',
  3180. '$mod.vA = ~$mod.vB;',
  3181. '$mod.X = rtl.and($mod.Y, $mod.Z);',
  3182. '$mod.X = $mod.Y & $mod.vA;',
  3183. '$mod.X = rtl.or($mod.Y, $mod.Z);',
  3184. '$mod.X = rtl.or($mod.Y, $mod.vA);',
  3185. '$mod.X = rtl.xor($mod.Y, $mod.Z);',
  3186. '$mod.X = rtl.xor($mod.Y, $mod.vA);',
  3187. '']));
  3188. end;
  3189. procedure TTestModule.TestBitwiseOperatorsLongword;
  3190. begin
  3191. StartProgram(false);
  3192. Add([
  3193. 'var',
  3194. ' a,b,c:longword;',
  3195. ' i: longint;',
  3196. 'begin',
  3197. ' a:=$12345678;',
  3198. ' b:=$EDCBA987;',
  3199. ' c:=not a;',
  3200. ' c:=a and b;',
  3201. ' c:=a and $ffff0000;',
  3202. ' c:=a or b;',
  3203. ' c:=a or $ff00ff00;',
  3204. ' c:=a xor b;',
  3205. ' c:=a xor $f0f0f0f0;',
  3206. ' c:=a shl 1;',
  3207. ' c:=a shl 16;',
  3208. ' c:=a shl 24;',
  3209. ' c:=a shl b;',
  3210. ' c:=a shr 1;',
  3211. ' c:=a shr 16;',
  3212. ' c:=a shr 24;',
  3213. ' c:=a shr b;',
  3214. ' c:=(b and c) or (a and b);',
  3215. ' c:=i and a;',
  3216. ' c:=i or a;',
  3217. ' c:=i xor a;',
  3218. '']);
  3219. ConvertProgram;
  3220. CheckSource('TestBitwiseOperatorsLongword',
  3221. LinesToStr([ // statements
  3222. 'this.a = 0;',
  3223. 'this.b = 0;',
  3224. 'this.c = 0;',
  3225. 'this.i = 0;',
  3226. '']),
  3227. LinesToStr([ // this.$main
  3228. '$mod.a = 0x12345678;',
  3229. '$mod.b = 0xEDCBA987;',
  3230. '$mod.c = rtl.lw(~$mod.a);',
  3231. '$mod.c = rtl.lw($mod.a & $mod.b);',
  3232. '$mod.c = rtl.lw($mod.a & 0xffff0000);',
  3233. '$mod.c = rtl.lw($mod.a | $mod.b);',
  3234. '$mod.c = rtl.lw($mod.a | 0xff00ff00);',
  3235. '$mod.c = rtl.lw($mod.a ^ $mod.b);',
  3236. '$mod.c = rtl.lw($mod.a ^ 0xf0f0f0f0);',
  3237. '$mod.c = rtl.lw($mod.a << 1);',
  3238. '$mod.c = rtl.lw($mod.a << 16);',
  3239. '$mod.c = rtl.lw($mod.a << 24);',
  3240. '$mod.c = rtl.lw($mod.a << $mod.b);',
  3241. '$mod.c = rtl.lw($mod.a >>> 1);',
  3242. '$mod.c = rtl.lw($mod.a >>> 16);',
  3243. '$mod.c = rtl.lw($mod.a >>> 24);',
  3244. '$mod.c = rtl.lw($mod.a >>> $mod.b);',
  3245. '$mod.c = rtl.lw(rtl.lw($mod.b & $mod.c) | rtl.lw($mod.a & $mod.b));',
  3246. '$mod.c = $mod.i & $mod.a;',
  3247. '$mod.c = $mod.i | $mod.a;',
  3248. '$mod.c = $mod.i ^ $mod.a;',
  3249. '']));
  3250. end;
  3251. procedure TTestModule.TestPrgProcVar;
  3252. begin
  3253. StartProgram(false);
  3254. Add('procedure Proc1;');
  3255. Add('type');
  3256. Add(' t1=longint;');
  3257. Add('var');
  3258. Add(' vA:t1;');
  3259. Add('begin');
  3260. Add('end;');
  3261. Add('begin');
  3262. ConvertProgram;
  3263. CheckSource('TestPrgProcVar',
  3264. LinesToStr([ // statements
  3265. 'this.Proc1 = function () {',
  3266. ' var vA=0;',
  3267. '};'
  3268. ]),
  3269. LinesToStr([ // this.$main
  3270. ''
  3271. ]));
  3272. end;
  3273. procedure TTestModule.TestUnitProcVar;
  3274. begin
  3275. StartUnit(false);
  3276. Add('interface');
  3277. Add('');
  3278. Add('type tA=string; // unit scope');
  3279. Add('procedure Proc1;');
  3280. Add('');
  3281. Add('implementation');
  3282. Add('');
  3283. Add('procedure Proc1;');
  3284. Add('type tA=longint; // local proc scope');
  3285. Add('var v1:tA; // using local tA');
  3286. Add('begin');
  3287. Add('end;');
  3288. Add('var v2:tA; // using interface tA');
  3289. ConvertUnit;
  3290. CheckSource('TestUnitProcVar',
  3291. LinesToStr([ // statements
  3292. 'var $impl = $mod.$impl;',
  3293. 'this.Proc1 = function () {',
  3294. ' var v1 = 0;',
  3295. '};',
  3296. '']),
  3297. // this.$init
  3298. '',
  3299. // implementation
  3300. LinesToStr([
  3301. '$impl.v2 = "";',
  3302. '']));
  3303. end;
  3304. procedure TTestModule.TestImplProc;
  3305. begin
  3306. StartUnit(false);
  3307. Add('interface');
  3308. Add('');
  3309. Add('procedure Proc1;');
  3310. Add('');
  3311. Add('implementation');
  3312. Add('');
  3313. Add('procedure Proc1; begin end;');
  3314. Add('procedure Proc2; begin end;');
  3315. Add('initialization');
  3316. Add(' Proc1;');
  3317. Add(' Proc2;');
  3318. ConvertUnit;
  3319. CheckSource('TestImplProc',
  3320. LinesToStr([ // statements
  3321. 'var $impl = $mod.$impl;',
  3322. 'this.Proc1 = function () {',
  3323. '};',
  3324. '']),
  3325. LinesToStr([ // this.$init
  3326. '$mod.Proc1();',
  3327. '$impl.Proc2();',
  3328. '']),
  3329. LinesToStr([ // implementation
  3330. '$impl.Proc2 = function () {',
  3331. '};',
  3332. ''])
  3333. );
  3334. end;
  3335. procedure TTestModule.TestFunctionResult;
  3336. begin
  3337. StartProgram(false);
  3338. Add('function Func1: longint;');
  3339. Add('begin');
  3340. Add(' Result:=3;');
  3341. Add(' Func1:=4;');
  3342. Add('end;');
  3343. Add('begin');
  3344. ConvertProgram;
  3345. CheckSource('TestFunctionResult',
  3346. LinesToStr([ // statements
  3347. 'this.Func1 = function () {',
  3348. ' var Result = 0;',
  3349. ' Result = 3;',
  3350. ' Result = 4;',
  3351. ' return Result;',
  3352. '};'
  3353. ]),
  3354. '');
  3355. end;
  3356. procedure TTestModule.TestNestedProc;
  3357. begin
  3358. StartProgram(false);
  3359. Add([
  3360. 'var vInUnit: longint;',
  3361. 'function DoIt(pA,pD: longint): longint;',
  3362. 'var',
  3363. ' vB: longint;',
  3364. ' vC: longint;',
  3365. ' function Nesty(pA: longint): longint; ',
  3366. ' var vB: longint;',
  3367. ' begin',
  3368. ' Result:=pa+vb+vc+pd+vInUnit;',
  3369. ' nesty:=3;',
  3370. ' doit:=4;',
  3371. ' exit;',
  3372. ' end;',
  3373. 'begin',
  3374. ' Result:=pa+vb+vc;',
  3375. ' doit:=6;',
  3376. ' exit;',
  3377. 'end;',
  3378. 'begin']);
  3379. ConvertProgram;
  3380. CheckSource('TestNestedProc',
  3381. LinesToStr([ // statements
  3382. 'this.vInUnit = 0;',
  3383. 'this.DoIt = function (pA, pD) {',
  3384. ' var Result = 0;',
  3385. ' var vB = 0;',
  3386. ' var vC = 0;',
  3387. ' function Nesty(pA) {',
  3388. ' var Result$1 = 0;',
  3389. ' var vB = 0;',
  3390. ' Result$1 = pA + vB + vC + pD + $mod.vInUnit;',
  3391. ' Result$1 = 3;',
  3392. ' Result = 4;',
  3393. ' return Result$1;',
  3394. ' return Result$1;',
  3395. ' };',
  3396. ' Result = pA + vB + vC;',
  3397. ' Result = 6;',
  3398. ' return Result;',
  3399. ' return Result;',
  3400. '};'
  3401. ]),
  3402. '');
  3403. end;
  3404. procedure TTestModule.TestNestedProc_ResultString;
  3405. begin
  3406. StartProgram(false);
  3407. Add([
  3408. 'function DoIt: string;',
  3409. ' function Nesty: string; ',
  3410. ' begin',
  3411. ' nesty:=#65#66;',
  3412. ' nesty[1]:=#67;',
  3413. ' doit:=#68;',
  3414. ' doit[2]:=#69;',
  3415. ' end;',
  3416. 'begin',
  3417. ' doit:=#70;',
  3418. ' doit[3]:=#71;',
  3419. 'end;',
  3420. 'begin']);
  3421. ConvertProgram;
  3422. CheckSource('TestNestedProc_ResultString',
  3423. LinesToStr([ // statements
  3424. 'this.DoIt = function () {',
  3425. ' var Result = "";',
  3426. ' function Nesty() {',
  3427. ' var Result$1 = "";',
  3428. ' Result$1 = "AB";',
  3429. ' Result$1 = rtl.setCharAt(Result$1, 0, "C");',
  3430. ' Result = "D";',
  3431. ' Result = rtl.setCharAt(Result, 1, "E");',
  3432. ' return Result$1;',
  3433. ' };',
  3434. ' Result = "F";',
  3435. ' Result = rtl.setCharAt(Result, 2, "G");',
  3436. ' return Result;',
  3437. '};'
  3438. ]),
  3439. '');
  3440. end;
  3441. procedure TTestModule.TestForwardProc;
  3442. begin
  3443. StartProgram(false);
  3444. Add('procedure FuncA(Bar: longint); forward;');
  3445. Add('procedure FuncB(Bar: longint);');
  3446. Add('begin');
  3447. Add(' funca(bar);');
  3448. Add('end;');
  3449. Add('procedure funca(bar: longint);');
  3450. Add('begin');
  3451. Add(' if bar=3 then ;');
  3452. Add('end;');
  3453. Add('begin');
  3454. Add(' funca(4);');
  3455. Add(' funcb(5);');
  3456. ConvertProgram;
  3457. CheckSource('TestForwardProc',
  3458. LinesToStr([ // statements'
  3459. 'this.FuncB = function (Bar) {',
  3460. ' $mod.FuncA(Bar);',
  3461. '};',
  3462. 'this.FuncA = function (Bar) {',
  3463. ' if (Bar === 3);',
  3464. '};'
  3465. ]),
  3466. LinesToStr([
  3467. '$mod.FuncA(4);',
  3468. '$mod.FuncB(5);'
  3469. ])
  3470. );
  3471. end;
  3472. procedure TTestModule.TestNestedForwardProc;
  3473. begin
  3474. StartProgram(false);
  3475. Add('procedure FuncA;');
  3476. Add(' procedure FuncB(i: longint); forward;');
  3477. Add(' procedure FuncC(i: longint);');
  3478. Add(' begin');
  3479. Add(' funcb(i);');
  3480. Add(' end;');
  3481. Add(' procedure FuncB(i: longint);');
  3482. Add(' begin');
  3483. Add(' if i=3 then ;');
  3484. Add(' end;');
  3485. Add('begin');
  3486. Add(' funcc(4)');
  3487. Add('end;');
  3488. Add('begin');
  3489. Add(' funca;');
  3490. ConvertProgram;
  3491. CheckSource('TestNestedForwardProc',
  3492. LinesToStr([ // statements'
  3493. 'this.FuncA = function () {',
  3494. ' function FuncC(i) {',
  3495. ' FuncB(i);',
  3496. ' };',
  3497. ' function FuncB(i) {',
  3498. ' if (i === 3);',
  3499. ' };',
  3500. ' FuncC(4);',
  3501. '};'
  3502. ]),
  3503. LinesToStr([
  3504. '$mod.FuncA();'
  3505. ])
  3506. );
  3507. end;
  3508. procedure TTestModule.TestAssignFunctionResult;
  3509. begin
  3510. StartProgram(false);
  3511. Add('function Func1: longint;');
  3512. Add('begin');
  3513. Add('end;');
  3514. Add('var i: longint;');
  3515. Add('begin');
  3516. Add(' i:=func1();');
  3517. Add(' i:=func1()+func1();');
  3518. ConvertProgram;
  3519. CheckSource('TestAssignFunctionResult',
  3520. LinesToStr([ // statements
  3521. 'this.Func1 = function () {',
  3522. ' var Result = 0;',
  3523. ' return Result;',
  3524. '};',
  3525. 'this.i = 0;'
  3526. ]),
  3527. LinesToStr([
  3528. '$mod.i = $mod.Func1();',
  3529. '$mod.i = $mod.Func1() + $mod.Func1();'
  3530. ]));
  3531. end;
  3532. procedure TTestModule.TestFunctionResultInCondition;
  3533. begin
  3534. StartProgram(false);
  3535. Add('function Func1: longint;');
  3536. Add('begin');
  3537. Add('end;');
  3538. Add('function Func2: boolean;');
  3539. Add('begin');
  3540. Add('end;');
  3541. Add('var i: longint;');
  3542. Add('begin');
  3543. Add(' if func2 then ;');
  3544. Add(' if i=func1() then ;');
  3545. Add(' if i=func1 then ;');
  3546. ConvertProgram;
  3547. CheckSource('TestFunctionResultInCondition',
  3548. LinesToStr([ // statements
  3549. 'this.Func1 = function () {',
  3550. ' var Result = 0;',
  3551. ' return Result;',
  3552. '};',
  3553. 'this.Func2 = function () {',
  3554. ' var Result = false;',
  3555. ' return Result;',
  3556. '};',
  3557. 'this.i = 0;'
  3558. ]),
  3559. LinesToStr([
  3560. 'if ($mod.Func2());',
  3561. 'if ($mod.i === $mod.Func1());',
  3562. 'if ($mod.i === $mod.Func1());'
  3563. ]));
  3564. end;
  3565. procedure TTestModule.TestFunctionResultInForLoop;
  3566. begin
  3567. StartProgram(false);
  3568. Add([
  3569. 'function Func1(a: array of longint): longint;',
  3570. 'begin',
  3571. ' for Result:=High(a) downto Low(a) do if a[Result]=0 then exit;',
  3572. ' for Result in a do if a[Result]=0 then exit;',
  3573. 'end;',
  3574. 'begin',
  3575. ' Func1([1,2,3])']);
  3576. ConvertProgram;
  3577. CheckSource('TestFunctionResultInForLoop',
  3578. LinesToStr([ // statements
  3579. 'this.Func1 = function (a) {',
  3580. ' var Result = 0;',
  3581. ' for (var $l = rtl.length(a) - 1; $l >= 0; $l--) {',
  3582. ' Result = $l;',
  3583. ' if (a[Result] === 0) return Result;',
  3584. ' };',
  3585. ' for (var $in = a, $l1 = 0, $end = rtl.length($in) - 1; $l1 <= $end; $l1++) {',
  3586. ' Result = $in[$l1];',
  3587. ' if (a[Result] === 0) return Result;',
  3588. ' };',
  3589. ' return Result;',
  3590. '};',
  3591. '']),
  3592. LinesToStr([
  3593. '$mod.Func1([1, 2, 3]);'
  3594. ]));
  3595. end;
  3596. procedure TTestModule.TestFunctionResultInTypeCast;
  3597. begin
  3598. StartProgram(false);
  3599. Add([
  3600. 'function GetInt: longint;',
  3601. 'begin',
  3602. 'end;',
  3603. 'begin',
  3604. ' if Byte(GetInt)=0 then ;',
  3605. '']);
  3606. ConvertProgram;
  3607. CheckSource('TestFunctionResultInTypeCast',
  3608. LinesToStr([ // statements
  3609. 'this.GetInt = function () {',
  3610. ' var Result = 0;',
  3611. ' return Result;',
  3612. '};',
  3613. '']),
  3614. LinesToStr([
  3615. 'if (($mod.GetInt() & 255) === 0) ;'
  3616. ]));
  3617. end;
  3618. procedure TTestModule.TestExit;
  3619. begin
  3620. StartProgram(false);
  3621. Add('procedure ProcA;');
  3622. Add('begin');
  3623. Add(' exit;');
  3624. Add('end;');
  3625. Add('function FuncB: longint;');
  3626. Add('begin');
  3627. Add(' exit;');
  3628. Add(' exit(3);');
  3629. Add('end;');
  3630. Add('function FuncC: string;');
  3631. Add('begin');
  3632. Add(' exit;');
  3633. Add(' exit(''a'');');
  3634. Add(' exit(''abc'');');
  3635. Add('end;');
  3636. Add('begin');
  3637. Add(' exit;');
  3638. Add(' exit(1);');
  3639. ConvertProgram;
  3640. CheckSource('TestExit',
  3641. LinesToStr([ // statements
  3642. 'this.ProcA = function () {',
  3643. ' return;',
  3644. '};',
  3645. 'this.FuncB = function () {',
  3646. ' var Result = 0;',
  3647. ' return Result;',
  3648. ' return 3;',
  3649. ' return Result;',
  3650. '};',
  3651. 'this.FuncC = function () {',
  3652. ' var Result = "";',
  3653. ' return Result;',
  3654. ' return "a";',
  3655. ' return "abc";',
  3656. ' return Result;',
  3657. '};'
  3658. ]),
  3659. LinesToStr([
  3660. 'return;',
  3661. 'return 1;',
  3662. '']));
  3663. end;
  3664. procedure TTestModule.TestExit_ResultInFinally;
  3665. begin
  3666. StartProgram(false);
  3667. Add([
  3668. 'function Run: word;',
  3669. 'begin',
  3670. ' try',
  3671. ' exit(3);', // no Result in finally -> use return 3
  3672. ' finally',
  3673. ' end;',
  3674. 'end;',
  3675. 'function Fly: word;',
  3676. 'begin',
  3677. ' try',
  3678. ' exit(3);',
  3679. ' finally',
  3680. ' if Result>0 then ;',
  3681. ' end;',
  3682. 'end;',
  3683. 'function Jump: word;',
  3684. 'begin',
  3685. ' try',
  3686. ' try',
  3687. ' exit(4);',
  3688. ' finally',
  3689. ' end;',
  3690. ' finally',
  3691. ' if Result>0 then ;',
  3692. ' end;',
  3693. 'end;',
  3694. 'begin',
  3695. '']);
  3696. ConvertProgram;
  3697. CheckSource('TestExit_ResultInFinally',
  3698. LinesToStr([ // statements
  3699. 'this.Run = function () {',
  3700. ' var Result = 0;',
  3701. ' try {',
  3702. ' return 3;',
  3703. ' } finally {',
  3704. ' };',
  3705. ' return Result;',
  3706. '};',
  3707. 'this.Fly = function () {',
  3708. ' var Result = 0;',
  3709. ' try {',
  3710. ' Result = 3;',
  3711. ' return Result;',
  3712. ' } finally {',
  3713. ' if (Result > 0) ;',
  3714. ' };',
  3715. ' return Result;',
  3716. '};',
  3717. 'this.Jump = function () {',
  3718. ' var Result = 0;',
  3719. ' try {',
  3720. ' try {',
  3721. ' Result = 4;',
  3722. ' return Result;',
  3723. ' } finally {',
  3724. ' };',
  3725. ' } finally {',
  3726. ' if (Result > 0) ;',
  3727. ' };',
  3728. ' return Result;',
  3729. '};',
  3730. '']),
  3731. LinesToStr([
  3732. '']));
  3733. end;
  3734. procedure TTestModule.TestBreak;
  3735. begin
  3736. StartProgram(false);
  3737. Add([
  3738. 'var',
  3739. ' i: longint;',
  3740. 'begin',
  3741. ' repeat',
  3742. ' break;',
  3743. ' until true;',
  3744. ' while true do',
  3745. ' break;',
  3746. ' for i:=1 to 2 do',
  3747. ' break;']);
  3748. ConvertProgram;
  3749. CheckSource('TestBreak',
  3750. LinesToStr([ // statements
  3751. 'this.i = 0;'
  3752. ]),
  3753. LinesToStr([
  3754. 'do {',
  3755. ' break;',
  3756. '} while (!true);',
  3757. 'while (true) break;',
  3758. 'for ($mod.i = 1; $mod.i <= 2; $mod.i++) break;',
  3759. '']));
  3760. end;
  3761. procedure TTestModule.TestBreakAsVar;
  3762. begin
  3763. StartProgram(false);
  3764. Add([
  3765. 'procedure DoIt(break: boolean);',
  3766. 'begin',
  3767. ' if break then ;',
  3768. 'end;',
  3769. 'var',
  3770. ' break: boolean;',
  3771. 'begin',
  3772. ' if break then ;']);
  3773. ConvertProgram;
  3774. CheckSource('TestBreakAsVar',
  3775. LinesToStr([ // statements
  3776. 'this.DoIt = function (Break) {',
  3777. ' if (Break) ;',
  3778. '};',
  3779. 'this.Break = false;',
  3780. '']),
  3781. LinesToStr([
  3782. 'if($mod.Break) ;',
  3783. '']));
  3784. end;
  3785. procedure TTestModule.TestContinue;
  3786. begin
  3787. StartProgram(false);
  3788. Add('var i: longint;');
  3789. Add('begin');
  3790. Add(' repeat');
  3791. Add(' continue;');
  3792. Add(' until true;');
  3793. Add(' while true do');
  3794. Add(' continue;');
  3795. Add(' for i:=1 to 2 do');
  3796. Add(' continue;');
  3797. ConvertProgram;
  3798. CheckSource('TestContinue',
  3799. LinesToStr([ // statements
  3800. 'this.i = 0;'
  3801. ]),
  3802. LinesToStr([
  3803. 'do {',
  3804. ' continue;',
  3805. '} while (!true);',
  3806. 'while (true) continue;',
  3807. 'for ($mod.i = 1; $mod.i <= 2; $mod.i++) continue;',
  3808. '']));
  3809. end;
  3810. procedure TTestModule.TestProc_External;
  3811. begin
  3812. StartProgram(false);
  3813. Add('procedure Foo; external name ''console.log'';');
  3814. Add('function Bar: longint; external name ''get.item'';');
  3815. Add('function Bla(s: string): longint; external name ''apply.something'';');
  3816. Add('var');
  3817. Add(' i: longint;');
  3818. Add('begin');
  3819. Add(' Foo;');
  3820. Add(' i:=Bar;');
  3821. Add(' i:=Bla(''abc'');');
  3822. ConvertProgram;
  3823. CheckSource('TestProc_External',
  3824. LinesToStr([ // statements
  3825. 'this.i = 0;'
  3826. ]),
  3827. LinesToStr([
  3828. 'console.log();',
  3829. '$mod.i = get.item();',
  3830. '$mod.i = apply.something("abc");'
  3831. ]));
  3832. end;
  3833. procedure TTestModule.TestProc_ExternalOtherUnit;
  3834. begin
  3835. AddModuleWithIntfImplSrc('unit2.pas',
  3836. LinesToStr([
  3837. 'procedure Now; external name ''Date.now'';',
  3838. 'procedure DoIt;'
  3839. ]),
  3840. 'procedure doit; begin end;');
  3841. StartUnit(true);
  3842. Add('interface');
  3843. Add('uses unit2;');
  3844. Add('implementation');
  3845. Add('begin');
  3846. Add(' now;');
  3847. Add(' now();');
  3848. Add(' uNit2.now;');
  3849. Add(' uNit2.now();');
  3850. Add(' doit;');
  3851. Add(' uNit2.doit;');
  3852. ConvertUnit;
  3853. CheckSource('TestProc_ExternalOtherUnit',
  3854. LinesToStr([
  3855. '']),
  3856. LinesToStr([
  3857. 'Date.now();',
  3858. 'Date.now();',
  3859. 'Date.now();',
  3860. 'Date.now();',
  3861. 'pas.unit2.DoIt();',
  3862. 'pas.unit2.DoIt();',
  3863. '']));
  3864. end;
  3865. procedure TTestModule.TestProc_Asm;
  3866. begin
  3867. StartProgram(false);
  3868. Add([
  3869. '{$mode delphi}',
  3870. 'function DoIt: longint;',
  3871. 'begin;',
  3872. ' asm',
  3873. ' { a:{ b:{}, c:[]}, d:''1'' };',
  3874. ' end;',
  3875. ' asm console.log(); end;',
  3876. ' asm',
  3877. ' s = "'' ";',
  3878. ' s = ''" '';',
  3879. ' s = s + "world" + "''";',
  3880. ' // end',
  3881. ' s = ''end'';',
  3882. ' s = "end";',
  3883. ' s = "foo\"bar";',
  3884. ' s = ''a\''b'';',
  3885. ' s = `${expr}\`-"-''-`;',
  3886. ' s = `multi',
  3887. 'line`;',
  3888. ' end;',
  3889. 'end;',
  3890. 'procedure Fly;',
  3891. 'asm',
  3892. ' return;',
  3893. 'end;',
  3894. 'begin']);
  3895. ConvertProgram;
  3896. CheckSource('TestProc_Asm',
  3897. LinesToStr([ // statements
  3898. 'this.DoIt = function () {',
  3899. ' var Result = 0;',
  3900. ' { a:{ b:{}, c:[]}, d:''1'' };',
  3901. ' console.log();',
  3902. ' s = "'' ";',
  3903. ' s = ''" '';',
  3904. ' s = s + "world" + "''";',
  3905. ' // end',
  3906. ' s = ''end'';',
  3907. ' s = "end";',
  3908. ' s = "foo\"bar";',
  3909. ' s = ''a\''b'';',
  3910. ' s = `${expr}\`-"-''-`;',
  3911. ' s = `multi',
  3912. 'line`;',
  3913. ' return Result;',
  3914. '};',
  3915. 'this.Fly = function () {',
  3916. ' return;',
  3917. '};',
  3918. '']),
  3919. LinesToStr([
  3920. ''
  3921. ]));
  3922. end;
  3923. procedure TTestModule.TestProc_AsmSubBlock;
  3924. begin
  3925. StartProgram(true,[supTObject]);
  3926. Add([
  3927. '{$mode delphi}',
  3928. 'type',
  3929. ' TBird = class end;',
  3930. 'procedure Run(w: word);',
  3931. 'begin;',
  3932. ' if true then asm console.log(); end;',
  3933. ' if w>3 then asm',
  3934. ' var a = w+1;',
  3935. ' w = a+3;',
  3936. ' end;',
  3937. ' while (w>7) do asm',
  3938. ' w+=3; w*=2;',
  3939. ' end;',
  3940. ' try',
  3941. ' except',
  3942. ' on E: TBird do',
  3943. ' asm console.log(E); end;',
  3944. ' on E: TObject do',
  3945. ' asm var i=3; i--; end;',
  3946. ' else asm Fly; High; end;',
  3947. ' end;',
  3948. 'end;',
  3949. 'begin']);
  3950. ConvertProgram;
  3951. CheckSource('TestProc_AsmSubBlock',
  3952. LinesToStr([ // statements
  3953. 'rtl.createClass(this, "TBird", pas.system.TObject, function () {',
  3954. '});',
  3955. 'this.Run = function (w) {',
  3956. ' if (true) console.log();',
  3957. ' if (w > 3) {',
  3958. ' var a = w+1;',
  3959. ' w = a+3;',
  3960. ' };',
  3961. ' while (w > 7) {',
  3962. ' w+=3; w*=2;',
  3963. ' };',
  3964. ' try {} catch ($e) {',
  3965. ' if ($mod.TBird.isPrototypeOf($e)) {',
  3966. ' var E = $e;',
  3967. ' console.log(E);',
  3968. ' } else if (pas.system.TObject.isPrototypeOf($e)) {',
  3969. ' var E = $e;',
  3970. ' var i=3; i--;',
  3971. ' } else {',
  3972. ' Fly; High;',
  3973. ' }',
  3974. ' };',
  3975. '};',
  3976. '']),
  3977. LinesToStr([
  3978. ''
  3979. ]));
  3980. end;
  3981. procedure TTestModule.TestProc_Assembler;
  3982. begin
  3983. StartProgram(false);
  3984. Add('function DoIt: longint; assembler;');
  3985. Add('asm');
  3986. Add('{ a:{ b:{}, c:[]}, d:''1'' };');
  3987. Add('end;');
  3988. Add('begin');
  3989. ConvertProgram;
  3990. CheckSource('TestProc_Assembler',
  3991. LinesToStr([ // statements
  3992. 'this.DoIt = function () {',
  3993. ' { a:{ b:{}, c:[]}, d:''1'' };',
  3994. '};'
  3995. ]),
  3996. LinesToStr([
  3997. ''
  3998. ]));
  3999. end;
  4000. procedure TTestModule.TestProc_VarParam;
  4001. begin
  4002. StartProgram(false);
  4003. Add('type integer = longint;');
  4004. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  4005. Add('var vJ: integer;');
  4006. Add('begin');
  4007. Add(' vg:=vg+1;');
  4008. Add(' vj:=vh+2;');
  4009. Add(' vi:=vi+3;');
  4010. Add(' doit(vg,vg,vg);');
  4011. Add(' doit(vh,vh,vj);');
  4012. Add(' doit(vi,vi,vi);');
  4013. Add(' doit(vj,vj,vj);');
  4014. Add('end;');
  4015. Add('var i: integer;');
  4016. Add('begin');
  4017. Add(' doit(i,i,i);');
  4018. ConvertProgram;
  4019. CheckSource('TestProc_VarParam',
  4020. LinesToStr([ // statements
  4021. 'this.DoIt = function (vG,vH,vI) {',
  4022. ' var vJ = 0;',
  4023. ' vG = vG + 1;',
  4024. ' vJ = vH + 2;',
  4025. ' vI.set(vI.get()+3);',
  4026. ' $mod.DoIt(vG, vG, {',
  4027. ' get: function () {',
  4028. ' return vG;',
  4029. ' },',
  4030. ' set: function (v) {',
  4031. ' vG = v;',
  4032. ' }',
  4033. ' });',
  4034. ' $mod.DoIt(vH, vH, {',
  4035. ' get: function () {',
  4036. ' return vJ;',
  4037. ' },',
  4038. ' set: function (v) {',
  4039. ' vJ = v;',
  4040. ' }',
  4041. ' });',
  4042. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  4043. ' $mod.DoIt(vJ, vJ, {',
  4044. ' get: function () {',
  4045. ' return vJ;',
  4046. ' },',
  4047. ' set: function (v) {',
  4048. ' vJ = v;',
  4049. ' }',
  4050. ' });',
  4051. '};',
  4052. 'this.i = 0;'
  4053. ]),
  4054. LinesToStr([
  4055. '$mod.DoIt($mod.i,$mod.i,{',
  4056. ' p: $mod,',
  4057. ' get: function () {',
  4058. ' return this.p.i;',
  4059. ' },',
  4060. ' set: function (v) {',
  4061. ' this.p.i = v;',
  4062. ' }',
  4063. '});'
  4064. ]));
  4065. end;
  4066. procedure TTestModule.TestProc_VarParamString;
  4067. begin
  4068. StartProgram(false);
  4069. Add(['type TCaption = string;',
  4070. 'procedure DoIt(vA: TCaption; var vB: TCaption; out vC: TCaption);',
  4071. 'var c: char;',
  4072. 'begin',
  4073. ' va[1]:=c;',
  4074. ' vb[2]:=c;',
  4075. ' vc[3]:=c;',
  4076. 'end;',
  4077. 'begin']);
  4078. ConvertProgram;
  4079. CheckSource('TestProc_VarParamString',
  4080. LinesToStr([ // statements
  4081. 'this.DoIt = function (vA,vB,vC) {',
  4082. ' var c = "";',
  4083. ' vA = rtl.setCharAt(vA, 0, c);',
  4084. ' vB.set(rtl.setCharAt(vB.get(), 1, c));',
  4085. ' vC.set(rtl.setCharAt(vC.get(), 2, c));',
  4086. '};',
  4087. '']),
  4088. LinesToStr([
  4089. ]));
  4090. end;
  4091. procedure TTestModule.TestProc_VarParamV;
  4092. begin
  4093. StartProgram(false);
  4094. Add([
  4095. 'procedure Inc2(var i: longint);',
  4096. 'begin',
  4097. ' i:=i+2;',
  4098. 'end;',
  4099. 'procedure DoIt(v: longint);',
  4100. 'var p: array of longint;',
  4101. 'begin',
  4102. ' Inc2(v);',
  4103. ' Inc2(p[v]);',
  4104. 'end;',
  4105. 'begin']);
  4106. ConvertProgram;
  4107. CheckSource('TestProc_VarParamV',
  4108. LinesToStr([ // statements
  4109. 'this.Inc2 = function (i) {',
  4110. ' i.set(i.get()+2);',
  4111. '};',
  4112. 'this.DoIt = function (v) {',
  4113. ' var p = [];',
  4114. ' $mod.Inc2({get: function () {',
  4115. ' return v;',
  4116. ' }, set: function (w) {',
  4117. ' v = w;',
  4118. ' }});',
  4119. ' $mod.Inc2({',
  4120. ' a: v,',
  4121. ' p: p,',
  4122. ' get: function () {',
  4123. ' return this.p[this.a];',
  4124. ' },',
  4125. ' set: function (v) {',
  4126. ' this.p[this.a] = v;',
  4127. ' }',
  4128. ' });',
  4129. '};',
  4130. '']),
  4131. LinesToStr([
  4132. '']));
  4133. end;
  4134. procedure TTestModule.TestProc_Overload;
  4135. begin
  4136. StartProgram(false);
  4137. Add('procedure DoIt(vI: longint); begin end;');
  4138. Add('procedure DoIt(vI, vJ: longint); begin end;');
  4139. Add('procedure DoIt(vD: double); begin end;');
  4140. Add('begin');
  4141. Add(' DoIt(1);');
  4142. Add(' DoIt(2,3);');
  4143. Add(' DoIt(4.5);');
  4144. ConvertProgram;
  4145. CheckSource('TestProcedureOverload',
  4146. LinesToStr([ // statements
  4147. 'this.DoIt = function (vI) {',
  4148. '};',
  4149. 'this.DoIt$1 = function (vI, vJ) {',
  4150. '};',
  4151. 'this.DoIt$2 = function (vD) {',
  4152. '};',
  4153. '']),
  4154. LinesToStr([
  4155. '$mod.DoIt(1);',
  4156. '$mod.DoIt$1(2, 3);',
  4157. '$mod.DoIt$2(4.5);',
  4158. '']));
  4159. end;
  4160. procedure TTestModule.TestProc_OverloadForward;
  4161. begin
  4162. StartProgram(false);
  4163. Add('procedure DoIt(vI: longint); forward;');
  4164. Add('procedure DoIt(vI, vJ: longint); begin end;');
  4165. Add('procedure doit(vi: longint); begin end;');
  4166. Add('begin');
  4167. Add(' doit(1);');
  4168. Add(' doit(2,3);');
  4169. ConvertProgram;
  4170. CheckSource('TestProcedureOverloadForward',
  4171. LinesToStr([ // statements
  4172. 'this.DoIt$1 = function (vI, vJ) {',
  4173. '};',
  4174. 'this.DoIt = function (vI) {',
  4175. '};',
  4176. '']),
  4177. LinesToStr([
  4178. '$mod.DoIt(1);',
  4179. '$mod.DoIt$1(2, 3);',
  4180. '']));
  4181. end;
  4182. procedure TTestModule.TestProc_OverloadIntfImpl;
  4183. begin
  4184. StartUnit(false);
  4185. Add('interface');
  4186. Add('procedure DoIt(vI: longint);');
  4187. Add('procedure DoIt(vI, vJ: longint);');
  4188. Add('implementation');
  4189. Add('procedure DoIt(vI, vJ, vK, vL, vM: longint); forward;');
  4190. Add('procedure DoIt(vI, vJ, vK: longint); begin end;');
  4191. Add('procedure DoIt(vi: longint); begin end;');
  4192. Add('procedure DoIt(vI, vJ, vK, vL: longint); begin end;');
  4193. Add('procedure DoIt(vi, vj: longint); begin end;');
  4194. Add('procedure DoIt(vi, vj, vk, vl, vm: longint); begin end;');
  4195. Add('begin');
  4196. Add(' doit(1);');
  4197. Add(' doit(2,3);');
  4198. Add(' doit(4,5,6);');
  4199. Add(' doit(7,8,9,10);');
  4200. Add(' doit(11,12,13,14,15);');
  4201. ConvertUnit;
  4202. CheckSource('TestProcedureOverloadUnit',
  4203. LinesToStr([ // statements
  4204. 'var $impl = $mod.$impl;',
  4205. 'this.DoIt = function (vI) {',
  4206. '};',
  4207. 'this.DoIt$1 = function (vI, vJ) {',
  4208. '};',
  4209. '']),
  4210. LinesToStr([ // this.$init
  4211. '$mod.DoIt(1);',
  4212. '$mod.DoIt$1(2, 3);',
  4213. '$impl.DoIt$3(4,5,6);',
  4214. '$impl.DoIt$4(7,8,9,10);',
  4215. '$impl.DoIt$2(11,12,13,14,15);',
  4216. '']),
  4217. LinesToStr([ // implementation
  4218. '$impl.DoIt$3 = function (vI, vJ, vK) {',
  4219. '};',
  4220. '$impl.DoIt$4 = function (vI, vJ, vK, vL) {',
  4221. '};',
  4222. '$impl.DoIt$2 = function (vI, vJ, vK, vL, vM) {',
  4223. '};',
  4224. '']));
  4225. end;
  4226. procedure TTestModule.TestProc_OverloadNested;
  4227. begin
  4228. StartProgram(false);
  4229. Add([
  4230. 'procedure doit(vA: longint);',
  4231. ' procedure DoIt(vA, vB: longint); overload;',
  4232. ' begin',
  4233. ' doit(1);',
  4234. ' doit(1,2);',
  4235. ' end;',
  4236. ' procedure doit(vA, vB, vC: longint);',
  4237. ' begin',
  4238. ' doit(1);',
  4239. ' doit(1,2);',
  4240. ' doit(1,2,3);',
  4241. ' end;',
  4242. 'begin',
  4243. ' doit(1);',
  4244. ' doit(1,2);',
  4245. ' doit(1,2,3);',
  4246. 'end;',
  4247. 'begin // main',
  4248. ' doit(1);']);
  4249. ConvertProgram;
  4250. CheckSource('TestProcedureOverloadNested',
  4251. LinesToStr([ // statements
  4252. 'this.doit = function (vA) {',
  4253. ' function DoIt$1(vA, vB) {',
  4254. ' $mod.doit(1);',
  4255. ' DoIt$1(1, 2);',
  4256. ' };',
  4257. ' function doit$2(vA, vB, vC) {',
  4258. ' $mod.doit(1);',
  4259. ' DoIt$1(1, 2);',
  4260. ' doit$2(1, 2, 3);',
  4261. ' };',
  4262. ' $mod.doit(1);',
  4263. ' DoIt$1(1, 2);',
  4264. ' doit$2(1, 2, 3);',
  4265. '};',
  4266. '']),
  4267. LinesToStr([
  4268. '$mod.doit(1);',
  4269. '']));
  4270. end;
  4271. procedure TTestModule.TestProc_OverloadNestedForward;
  4272. begin
  4273. StartProgram(false);
  4274. Add([
  4275. 'procedure DoIt(vA: longint); overload; forward;',
  4276. 'procedure DoIt(vB, vC: longint); overload;',
  4277. 'begin // 2 param overload',
  4278. ' doit(1);',
  4279. ' doit(1,2);',
  4280. 'end;',
  4281. 'procedure doit(vA: longint);',
  4282. ' procedure DoIt(vA, vB, vC: longint); overload; forward;',
  4283. ' procedure DoIt(vA, vB, vC, vD: longint); overload;',
  4284. ' begin // 4 param overload',
  4285. ' doit(1);',
  4286. ' doit(1,2);',
  4287. ' doit(1,2,3);',
  4288. ' doit(1,2,3,4);',
  4289. ' end;',
  4290. ' procedure doit(vA, vB, vC: longint);',
  4291. ' procedure DoIt(vA, vB, vC, vD, vE: longint); overload; forward;',
  4292. ' procedure DoIt(vA, vB, vC, vD, vE, vF: longint); overload;',
  4293. ' begin // 6 param overload',
  4294. ' doit(1);',
  4295. ' doit(1,2);',
  4296. ' doit(1,2,3);',
  4297. ' doit(1,2,3,4);',
  4298. ' doit(1,2,3,4,5);',
  4299. ' doit(1,2,3,4,5,6);',
  4300. ' end;',
  4301. ' procedure doit(vA, vB, vC, vD, vE: longint);',
  4302. ' begin // 5 param overload',
  4303. ' doit(1);',
  4304. ' doit(1,2);',
  4305. ' doit(1,2,3);',
  4306. ' doit(1,2,3,4);',
  4307. ' doit(1,2,3,4,5);',
  4308. ' doit(1,2,3,4,5,6);',
  4309. ' end;',
  4310. ' begin // 3 param overload',
  4311. ' doit(1);',
  4312. ' doit(1,2);',
  4313. ' doit(1,2,3);',
  4314. ' doit(1,2,3,4);',
  4315. ' doit(1,2,3,4,5);',
  4316. ' doit(1,2,3,4,5,6);',
  4317. ' end;',
  4318. 'begin // 1 param overload',
  4319. ' doit(1);',
  4320. ' doit(1,2);',
  4321. ' doit(1,2,3);',
  4322. ' doit(1,2,3,4);',
  4323. 'end;',
  4324. 'begin // main',
  4325. ' doit(1);',
  4326. ' doit(1,2);']);
  4327. ConvertProgram;
  4328. CheckSource('TestProc_OverloadNestedForward',
  4329. LinesToStr([ // statements
  4330. 'this.DoIt$1 = function (vB, vC) {',
  4331. ' $mod.DoIt(1);',
  4332. ' $mod.DoIt$1(1, 2);',
  4333. '};',
  4334. 'this.DoIt = function (vA) {',
  4335. ' function DoIt$3(vA, vB, vC, vD) {',
  4336. ' $mod.DoIt(1);',
  4337. ' $mod.DoIt$1(1, 2);',
  4338. ' DoIt$2(1, 2, 3);',
  4339. ' DoIt$3(1, 2, 3, 4);',
  4340. ' };',
  4341. ' function DoIt$2(vA, vB, vC) {',
  4342. ' function DoIt$5(vA, vB, vC, vD, vE, vF) {',
  4343. ' $mod.DoIt(1);',
  4344. ' $mod.DoIt$1(1, 2);',
  4345. ' DoIt$2(1, 2, 3);',
  4346. ' DoIt$3(1, 2, 3, 4);',
  4347. ' DoIt$4(1, 2, 3, 4, 5);',
  4348. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  4349. ' };',
  4350. ' function DoIt$4(vA, vB, vC, vD, vE) {',
  4351. ' $mod.DoIt(1);',
  4352. ' $mod.DoIt$1(1, 2);',
  4353. ' DoIt$2(1, 2, 3);',
  4354. ' DoIt$3(1, 2, 3, 4);',
  4355. ' DoIt$4(1, 2, 3, 4, 5);',
  4356. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  4357. ' };',
  4358. ' $mod.DoIt(1);',
  4359. ' $mod.DoIt$1(1, 2);',
  4360. ' DoIt$2(1, 2, 3);',
  4361. ' DoIt$3(1, 2, 3, 4);',
  4362. ' DoIt$4(1, 2, 3, 4, 5);',
  4363. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  4364. ' };',
  4365. ' $mod.DoIt(1);',
  4366. ' $mod.DoIt$1(1, 2);',
  4367. ' DoIt$2(1, 2, 3);',
  4368. ' DoIt$3(1, 2, 3, 4);',
  4369. '};',
  4370. '']),
  4371. LinesToStr([
  4372. '$mod.DoIt(1);',
  4373. '$mod.DoIt$1(1, 2);',
  4374. '']));
  4375. end;
  4376. procedure TTestModule.TestProc_OverloadUnitCycle;
  4377. begin
  4378. AddModuleWithIntfImplSrc('Unit2.pas',
  4379. LinesToStr([
  4380. 'type',
  4381. ' TObject = class',
  4382. ' procedure DoIt(b: boolean); virtual; abstract;',
  4383. ' procedure DoIt(i: longint); virtual; abstract;',
  4384. ' end;',
  4385. '']),
  4386. 'uses test1;');
  4387. StartUnit(true);
  4388. Add([
  4389. 'interface',
  4390. 'uses unit2;',
  4391. 'type',
  4392. ' TEagle = class(TObject)',
  4393. ' procedure DoIt(b: boolean); override;',
  4394. ' procedure DoIt(i: longint); override;',
  4395. ' end;',
  4396. 'implementation',
  4397. 'procedure TEagle.DoIt(b: boolean); begin end;',
  4398. 'procedure TEagle.DoIt(i: longint); begin end;',
  4399. '']);
  4400. ConvertUnit;
  4401. CheckSource('TestProc_OverloadUnitCycle',
  4402. LinesToStr([ // statements
  4403. 'rtl.createClass(this, "TEagle", pas.Unit2.TObject, function () {',
  4404. ' this.DoIt = function (b) {',
  4405. ' };',
  4406. ' this.DoIt$1 = function (i) {',
  4407. ' };',
  4408. '});',
  4409. '']),
  4410. '',
  4411. LinesToStr([
  4412. '']));
  4413. end;
  4414. procedure TTestModule.TestProc_Varargs;
  4415. begin
  4416. StartProgram(false);
  4417. Add([
  4418. 'procedure ProcA(i:longint); varargs; external name ''ProcA'';',
  4419. 'procedure ProcB; varargs; external name ''ProcB'';',
  4420. 'procedure ProcC(i: longint = 17); varargs; external name ''ProcC'';',
  4421. 'function GetIt: longint; begin end;',
  4422. 'begin',
  4423. ' ProcA(1);',
  4424. ' ProcA(1,2);',
  4425. ' ProcA(1,2.0);',
  4426. ' ProcA(1,2,3);',
  4427. ' ProcA(1,''2'');',
  4428. ' ProcA(2,'''');',
  4429. ' ProcA(3,false);',
  4430. ' ProcB;',
  4431. ' ProcB();',
  4432. ' ProcB(4);',
  4433. ' ProcB(''foo'');',
  4434. ' ProcC;',
  4435. ' ProcC();',
  4436. ' ProcC(4);',
  4437. ' ProcC(5,''foo'');',
  4438. ' ProcB(GetIt);',
  4439. ' ProcB(GetIt());',
  4440. ' ProcB(GetIt,GetIt());']);
  4441. ConvertProgram;
  4442. CheckSource('TestProc_Varargs',
  4443. LinesToStr([ // statements
  4444. 'this.GetIt = function () {',
  4445. ' var Result = 0;',
  4446. ' return Result;',
  4447. '};',
  4448. '']),
  4449. LinesToStr([
  4450. 'ProcA(1);',
  4451. 'ProcA(1, 2);',
  4452. 'ProcA(1, 2.0);',
  4453. 'ProcA(1, 2, 3);',
  4454. 'ProcA(1, "2");',
  4455. 'ProcA(2, "");',
  4456. 'ProcA(3, false);',
  4457. 'ProcB();',
  4458. 'ProcB();',
  4459. 'ProcB(4);',
  4460. 'ProcB("foo");',
  4461. 'ProcC(17);',
  4462. 'ProcC(17);',
  4463. 'ProcC(4);',
  4464. 'ProcC(5, "foo");',
  4465. 'ProcB($mod.GetIt());',
  4466. 'ProcB($mod.GetIt());',
  4467. 'ProcB($mod.GetIt(), $mod.GetIt());',
  4468. '']));
  4469. end;
  4470. procedure TTestModule.TestProc_ConstOrder;
  4471. begin
  4472. StartProgram(false);
  4473. Add([
  4474. 'const A = 3;',
  4475. 'const B = A+1;',
  4476. 'procedure DoIt;',
  4477. 'const C = A+1;',
  4478. 'const D = B+1;',
  4479. 'const E = D+C+B+A;',
  4480. 'begin',
  4481. 'end;',
  4482. 'begin'
  4483. ]);
  4484. ConvertProgram;
  4485. CheckSource('TestProc_ConstOrder',
  4486. LinesToStr([ // statements
  4487. 'this.A = 3;',
  4488. 'this.B = 3 + 1;',
  4489. 'var C = 3 + 1;',
  4490. 'var D = 4 + 1;',
  4491. 'var E = 5 + 4 + 4 + 3;',
  4492. 'this.DoIt = function () {',
  4493. '};',
  4494. '']),
  4495. LinesToStr([
  4496. ''
  4497. ]));
  4498. end;
  4499. procedure TTestModule.TestProc_DuplicateConst;
  4500. begin
  4501. StartProgram(false);
  4502. Add([
  4503. 'const A = 1;',
  4504. 'procedure DoIt;',
  4505. 'const A = 2;',
  4506. ' procedure SubIt;',
  4507. ' const A = 21;',
  4508. ' begin',
  4509. ' end;',
  4510. 'begin',
  4511. 'end;',
  4512. 'procedure DoSome;',
  4513. 'const A = 3;',
  4514. 'begin',
  4515. 'end;',
  4516. 'begin'
  4517. ]);
  4518. ConvertProgram;
  4519. CheckSource('TestProc_DuplicateConst',
  4520. LinesToStr([ // statements
  4521. 'this.A = 1;',
  4522. 'var A$1 = 2;',
  4523. 'var A$2 = 21;',
  4524. 'this.DoIt = function () {',
  4525. ' function SubIt() {',
  4526. ' };',
  4527. '};',
  4528. 'var A$3 = 3;',
  4529. 'this.DoSome = function () {',
  4530. '};',
  4531. '']),
  4532. LinesToStr([
  4533. ''
  4534. ]));
  4535. end;
  4536. procedure TTestModule.TestProc_LocalVarAbsolute;
  4537. begin
  4538. StartProgram(false);
  4539. Add([
  4540. 'type',
  4541. ' TObject = class',
  4542. ' Index: longint;',
  4543. ' procedure DoAbs(Item: pointer);',
  4544. ' end;',
  4545. 'procedure TObject.DoAbs(Item: pointer);',
  4546. 'var',
  4547. ' o: TObject absolute Item;',
  4548. 'begin',
  4549. ' if o.Index<o.Index then o.Index:=o.Index;',
  4550. 'end;',
  4551. 'procedure DoIt(i: longint; p: pointer);',
  4552. 'var',
  4553. ' d: double absolute i;',
  4554. ' s: string absolute d;',
  4555. ' oi: TObject absolute i;',
  4556. ' op: TObject absolute p;',
  4557. 'begin',
  4558. ' if d=d then d:=d;',
  4559. ' if s=s then s:=s;',
  4560. ' if oi.Index<oi.Index then oi.Index:=oi.Index;',
  4561. ' if op.Index=op.Index then op.Index:=op.Index;',
  4562. 'end;',
  4563. 'begin']);
  4564. ConvertProgram;
  4565. CheckSource('TestProc_LocalVarAbsolute',
  4566. LinesToStr([ // statements
  4567. 'rtl.createClass(this, "TObject", null, function () {',
  4568. ' this.$init = function () {',
  4569. ' this.Index = 0;',
  4570. ' };',
  4571. ' this.$final = function () {',
  4572. ' };',
  4573. ' this.DoAbs = function (Item) {',
  4574. ' if (Item.Index < Item.Index) Item.Index = Item.Index;',
  4575. ' };',
  4576. '});',
  4577. 'this.DoIt = function (i, p) {',
  4578. ' if (i === i) i = i;',
  4579. ' if (i === i) i = i;',
  4580. ' if (i.Index < i.Index) i.Index = i.Index;',
  4581. ' if (p.Index === p.Index) p.Index = p.Index;',
  4582. '};'
  4583. ]),
  4584. LinesToStr([
  4585. ]));
  4586. end;
  4587. procedure TTestModule.TestProc_LocalVarInit;
  4588. begin
  4589. StartProgram(false);
  4590. Add([
  4591. 'type TBytes = array of byte;',
  4592. 'procedure DoIt;',
  4593. 'const c = 4;',
  4594. 'var',
  4595. ' b: byte = 1;',
  4596. ' w: word = 2+c;',
  4597. ' p: pointer = nil;',
  4598. ' Buffer: TBytes = nil;',
  4599. 'begin',
  4600. 'end;',
  4601. 'begin']);
  4602. ConvertProgram;
  4603. CheckSource('TestProc_LocalVarInit',
  4604. LinesToStr([ // statements
  4605. 'var c = 4;',
  4606. 'this.DoIt = function () {',
  4607. ' var b = 1;',
  4608. ' var w = 2 + 4;',
  4609. ' var p = null;',
  4610. ' var Buffer = [];',
  4611. '};',
  4612. '']),
  4613. LinesToStr([
  4614. ]));
  4615. end;
  4616. procedure TTestModule.TestProc_ReservedWords;
  4617. begin
  4618. StartProgram(false);
  4619. Add([
  4620. 'procedure Date(ArrayBuffer: longint);',
  4621. 'const',
  4622. ' NaN: longint = 3;',
  4623. 'var',
  4624. ' &Boolean: longint;',
  4625. ' procedure Error(ArrayBuffer: longint);',
  4626. ' begin',
  4627. ' end;',
  4628. 'begin',
  4629. ' Nan:=&bOolean;',
  4630. 'end;',
  4631. 'begin',
  4632. ' Date(1);']);
  4633. ConvertProgram;
  4634. CheckSource('TestProc_ReservedWords',
  4635. LinesToStr([ // statements
  4636. 'var naN = 3;',
  4637. 'this.Date = function (arrayBuffer) {',
  4638. ' var boolean = 0;',
  4639. ' function error(arrayBuffer) {',
  4640. ' };',
  4641. ' naN = boolean;',
  4642. '};',
  4643. '']),
  4644. LinesToStr([
  4645. ' $mod.Date(1);'
  4646. ]));
  4647. end;
  4648. procedure TTestModule.TestProc_ConstRefWord;
  4649. begin
  4650. StartProgram(false);
  4651. Add([
  4652. 'procedure Run(constref w: word);',
  4653. 'var l: word;',
  4654. 'begin',
  4655. ' l:=w;',
  4656. ' Run(w);',
  4657. ' Run(l);',
  4658. 'end;',
  4659. 'procedure Fly(a: word; var b: word; out c: word; const d: word; constref e: word);',
  4660. 'begin',
  4661. ' Run(a);',
  4662. ' Run(b);',
  4663. ' Run(c);',
  4664. ' Run(d);',
  4665. ' Run(e);',
  4666. 'end;',
  4667. 'begin',
  4668. ' Run(1);']);
  4669. ConvertProgram;
  4670. CheckHint(mtWarning,nConstRefNotForXAsConst,'ConstRef not yet implemented for Word. Treating as Const');
  4671. CheckSource('TestProc_ConstRefWord',
  4672. LinesToStr([ // statements
  4673. 'this.Run = function (w) {',
  4674. ' var l = 0;',
  4675. ' l = w;',
  4676. ' $mod.Run(w);',
  4677. ' $mod.Run(l);',
  4678. '};',
  4679. 'this.Fly = function (a, b, c, d, e) {',
  4680. ' $mod.Run(a);',
  4681. ' $mod.Run(b.get());',
  4682. ' $mod.Run(c.get());',
  4683. ' $mod.Run(d);',
  4684. ' $mod.Run(e);',
  4685. '};',
  4686. '']),
  4687. LinesToStr([
  4688. '$mod.Run(1);'
  4689. ]));
  4690. end;
  4691. procedure TTestModule.TestAnonymousProc_Assign_ObjFPC;
  4692. begin
  4693. StartProgram(false);
  4694. Add([
  4695. '{$mode objfpc}',
  4696. 'type',
  4697. ' TFunc = reference to function(x: word): word;',
  4698. 'var Func: TFunc;',
  4699. 'procedure DoIt(a: word);',
  4700. 'begin',
  4701. ' Func:=function(b:word): word',
  4702. ' begin',
  4703. ' Result:=a+b;',
  4704. ' exit(b);',
  4705. ' exit(Result);',
  4706. ' end;',// test semicolon
  4707. ' a:=3;',
  4708. 'end;',
  4709. 'begin',
  4710. ' Func:=function(c:word):word begin',
  4711. ' Result:=3+c;',
  4712. ' exit(c);',
  4713. ' exit(Result);',
  4714. ' end;']);
  4715. ConvertProgram;
  4716. CheckSource('TestAnonymousProc_Assign_ObjFPC',
  4717. LinesToStr([ // statements
  4718. 'this.Func = null;',
  4719. 'this.DoIt = function (a) {',
  4720. ' $mod.Func = function (b) {',
  4721. ' var Result = 0;',
  4722. ' Result = a + b;',
  4723. ' return b;',
  4724. ' return Result;',
  4725. ' return Result;',
  4726. ' };',
  4727. ' a = 3;',
  4728. '};',
  4729. '']),
  4730. LinesToStr([
  4731. '$mod.Func = function (c) {',
  4732. ' var Result = 0;',
  4733. ' Result = 3 + c;',
  4734. ' return c;',
  4735. ' return Result;',
  4736. ' return Result;',
  4737. '};',
  4738. '']));
  4739. end;
  4740. procedure TTestModule.TestAnonymousProc_Assign_Delphi;
  4741. begin
  4742. StartProgram(false);
  4743. Add([
  4744. '{$mode delphi}',
  4745. 'type',
  4746. ' TProc = reference to procedure(x: word);',
  4747. 'procedure DoIt(a: word);',
  4748. 'var Proc: TProc;',
  4749. 'begin',
  4750. ' Proc:=procedure(b:word) begin end;',
  4751. 'end;',
  4752. 'var Proc: TProc;',
  4753. 'begin',
  4754. ' Proc:=procedure(c:word) begin end;',
  4755. '']);
  4756. ConvertProgram;
  4757. CheckSource('TestAnonymousProc_Assign_Delphi',
  4758. LinesToStr([ // statements
  4759. 'this.DoIt = function (a) {',
  4760. ' var Proc = null;',
  4761. ' Proc = function (b) {',
  4762. ' };',
  4763. '};',
  4764. 'this.Proc = null;',
  4765. '']),
  4766. LinesToStr([
  4767. '$mod.Proc = function (c) {',
  4768. '};',
  4769. '']));
  4770. end;
  4771. procedure TTestModule.TestAnonymousProc_Arg;
  4772. begin
  4773. StartProgram(false);
  4774. Add([
  4775. 'type',
  4776. ' TProc = reference to procedure;',
  4777. ' TFunc = reference to function(x: word): word;',
  4778. 'procedure DoMore(f,g: TProc);',
  4779. 'begin',
  4780. 'end;',
  4781. 'procedure DoOdd(v: jsvalue);',
  4782. 'begin',
  4783. 'end;',
  4784. 'procedure DoIt(f: TFunc);',
  4785. 'begin',
  4786. ' DoIt(function(b:word): word',
  4787. ' begin',
  4788. ' Result:=1+b;',
  4789. ' end);',
  4790. ' DoMore(procedure begin end, procedure begin end);',
  4791. ' DoOdd(procedure begin end);',
  4792. 'end;',
  4793. 'begin',
  4794. ' DoMore(procedure begin end,',
  4795. ' procedure assembler asm',
  4796. ' console.log("c");',
  4797. ' end);',
  4798. '']);
  4799. ConvertProgram;
  4800. CheckSource('TestAnonymousProc_Arg',
  4801. LinesToStr([ // statements
  4802. 'this.DoMore = function (f, g) {',
  4803. '};',
  4804. 'this.DoOdd = function (v) {',
  4805. '};',
  4806. 'this.DoIt = function (f) {',
  4807. ' $mod.DoIt(function (b) {',
  4808. ' var Result = 0;',
  4809. ' Result = 1 + b;',
  4810. ' return Result;',
  4811. ' });',
  4812. ' $mod.DoMore(function () {',
  4813. ' }, function () {',
  4814. ' });',
  4815. ' $mod.DoOdd(function () {',
  4816. ' });',
  4817. '};',
  4818. '']),
  4819. LinesToStr([
  4820. '$mod.DoMore(function () {',
  4821. '}, function () {',
  4822. ' console.log("c");',
  4823. '});',
  4824. '']));
  4825. end;
  4826. procedure TTestModule.TestAnonymousProc_Typecast;
  4827. begin
  4828. StartProgram(false);
  4829. Add([
  4830. 'type',
  4831. ' TProc = reference to procedure(w: word);',
  4832. ' TArr = array of word;',
  4833. ' TFuncArr = reference to function: TArr;',
  4834. 'procedure DoIt(p: TProc);',
  4835. 'var',
  4836. ' w: word;',
  4837. ' a: TArr;',
  4838. 'begin',
  4839. ' p:=TProc(procedure(b: smallint) begin end);',
  4840. ' a:=TFuncArr(function: TArr begin end)();',
  4841. ' w:=TFuncArr(function: TArr begin end)()[3];',
  4842. 'end;',
  4843. 'begin']);
  4844. ConvertProgram;
  4845. CheckSource('TestAnonymousProc_Typecast',
  4846. LinesToStr([ // statements
  4847. 'this.DoIt = function (p) {',
  4848. ' var w = 0;',
  4849. ' var a = [];',
  4850. ' p = function (b) {',
  4851. ' };',
  4852. ' a = function () {',
  4853. ' var Result = [];',
  4854. ' return Result;',
  4855. ' }();',
  4856. ' w = function () {',
  4857. ' var Result = [];',
  4858. ' return Result;',
  4859. ' }()[3];',
  4860. '};',
  4861. '']),
  4862. LinesToStr([
  4863. '']));
  4864. end;
  4865. procedure TTestModule.TestAnonymousProc_With;
  4866. begin
  4867. StartProgram(false);
  4868. Add([
  4869. 'type',
  4870. ' TProc = reference to procedure(w: word);',
  4871. ' TObject = class',
  4872. ' b: boolean;',
  4873. ' end;',
  4874. 'var',
  4875. ' p: TProc;',
  4876. ' bird: TObject;',
  4877. 'begin',
  4878. ' with bird do',
  4879. ' p:=procedure(w: word)',
  4880. ' begin',
  4881. ' b:=w>2;',
  4882. ' end;',
  4883. '']);
  4884. ConvertProgram;
  4885. CheckSource('TestAnonymousProc_With',
  4886. LinesToStr([ // statements
  4887. 'rtl.createClass(this, "TObject", null, function () {',
  4888. ' this.$init = function () {',
  4889. ' this.b = false;',
  4890. ' };',
  4891. ' this.$final = function () {',
  4892. ' };',
  4893. '});',
  4894. 'this.p = null;',
  4895. 'this.bird = null;',
  4896. '']),
  4897. LinesToStr([
  4898. 'var $with = $mod.bird;',
  4899. '$mod.p = function (w) {',
  4900. ' $with.b = w > 2;',
  4901. '};',
  4902. '']));
  4903. end;
  4904. procedure TTestModule.TestAnonymousProc_ExceptOn;
  4905. begin
  4906. StartProgram(false);
  4907. Add([
  4908. 'type',
  4909. ' TProc = reference to procedure;',
  4910. ' TObject = class',
  4911. ' b: boolean;',
  4912. ' end;',
  4913. 'procedure DoIt;',
  4914. 'var',
  4915. ' p: TProc;',
  4916. 'begin',
  4917. ' try',
  4918. ' except',
  4919. ' on E: TObject do',
  4920. ' p:=procedure',
  4921. ' begin',
  4922. ' E.b:=true;',
  4923. ' end;',
  4924. ' end;',
  4925. 'end;',
  4926. 'begin']);
  4927. ConvertProgram;
  4928. CheckSource('TestAnonymousProc_ExceptOn',
  4929. LinesToStr([ // statements
  4930. 'rtl.createClass(this, "TObject", null, function () {',
  4931. ' this.$init = function () {',
  4932. ' this.b = false;',
  4933. ' };',
  4934. ' this.$final = function () {',
  4935. ' };',
  4936. '});',
  4937. 'this.DoIt = function () {',
  4938. ' var p = null;',
  4939. ' try {} catch ($e) {',
  4940. ' if ($mod.TObject.isPrototypeOf($e)) {',
  4941. ' var E = $e;',
  4942. ' p = function () {',
  4943. ' E.b = true;',
  4944. ' };',
  4945. ' } else throw $e',
  4946. ' };',
  4947. '};',
  4948. '']),
  4949. LinesToStr([
  4950. '']));
  4951. end;
  4952. procedure TTestModule.TestAnonymousProc_Nested;
  4953. begin
  4954. StartProgram(false);
  4955. Add([
  4956. 'type',
  4957. ' TProc = reference to procedure;',
  4958. ' TObject = class',
  4959. ' i: byte;',
  4960. ' procedure DoIt;',
  4961. ' end;',
  4962. 'procedure TObject.DoIt;',
  4963. 'var',
  4964. ' p: TProc;',
  4965. ' procedure Sub;',
  4966. ' begin',
  4967. ' p:=procedure',
  4968. ' begin',
  4969. ' i:=3;',
  4970. ' Self.i:=4;',
  4971. ' p:=procedure',
  4972. ' procedure SubSub;',
  4973. ' begin',
  4974. ' i:=13;',
  4975. ' Self.i:=14;',
  4976. ' end;',
  4977. ' begin',
  4978. ' i:=13;',
  4979. ' Self.i:=14;',
  4980. ' end;',
  4981. ' end;',
  4982. ' end;',
  4983. 'begin',
  4984. 'end;',
  4985. 'begin']);
  4986. ConvertProgram;
  4987. CheckSource('TestAnonymousProc_Nested',
  4988. LinesToStr([ // statements
  4989. 'rtl.createClass(this, "TObject", null, function () {',
  4990. ' this.$init = function () {',
  4991. ' this.i = 0;',
  4992. ' };',
  4993. ' this.$final = function () {',
  4994. ' };',
  4995. ' this.DoIt = function () {',
  4996. ' var $Self = this;',
  4997. ' var p = null;',
  4998. ' function Sub() {',
  4999. ' p = function () {',
  5000. ' $Self.i = 3;',
  5001. ' $Self.i = 4;',
  5002. ' p = function () {',
  5003. ' function SubSub() {',
  5004. ' $Self.i = 13;',
  5005. ' $Self.i = 14;',
  5006. ' };',
  5007. ' $Self.i = 13;',
  5008. ' $Self.i = 14;',
  5009. ' };',
  5010. ' };',
  5011. ' };',
  5012. ' };',
  5013. '});',
  5014. '']),
  5015. LinesToStr([
  5016. '']));
  5017. end;
  5018. procedure TTestModule.TestAnonymousProc_NestedAssignResult;
  5019. begin
  5020. StartProgram(false);
  5021. Add([
  5022. 'type',
  5023. ' TProc = reference to procedure;',
  5024. 'function DoIt: TProc;',
  5025. ' function Sub: TProc;',
  5026. ' begin',
  5027. ' Result:=procedure',
  5028. ' begin',
  5029. ' Sub:=procedure',
  5030. ' procedure SubSub;',
  5031. ' begin',
  5032. ' Result:=nil;',
  5033. ' Sub:=nil;',
  5034. ' DoIt:=nil;',
  5035. ' end;',
  5036. ' begin',
  5037. ' Result:=nil;',
  5038. ' Sub:=nil;',
  5039. ' DoIt:=nil;',
  5040. ' end;',
  5041. ' end;',
  5042. ' end;',
  5043. 'begin',
  5044. 'end;',
  5045. 'begin']);
  5046. ConvertProgram;
  5047. CheckSource('TestAnonymousProc_NestedAssignResult',
  5048. LinesToStr([ // statements
  5049. 'this.DoIt = function () {',
  5050. ' var Result = null;',
  5051. ' function Sub() {',
  5052. ' var Result$1 = null;',
  5053. ' Result$1 = function () {',
  5054. ' Result$1 = function () {',
  5055. ' function SubSub() {',
  5056. ' Result$1 = null;',
  5057. ' Result$1 = null;',
  5058. ' Result = null;',
  5059. ' };',
  5060. ' Result$1 = null;',
  5061. ' Result$1 = null;',
  5062. ' Result = null;',
  5063. ' };',
  5064. ' };',
  5065. ' return Result$1;',
  5066. ' };',
  5067. ' return Result;',
  5068. '};',
  5069. '']),
  5070. LinesToStr([
  5071. '']));
  5072. end;
  5073. procedure TTestModule.TestAnonymousProc_Class;
  5074. begin
  5075. StartProgram(false);
  5076. Add([
  5077. 'type',
  5078. ' TProc = reference to procedure;',
  5079. ' TEvent = procedure of object;',
  5080. ' TObject = class',
  5081. ' Size: word;',
  5082. ' function GetIt: TProc;',
  5083. ' procedure DoIt; virtual; abstract;',
  5084. ' end;',
  5085. 'function TObject.GetIt: TProc;',
  5086. 'begin',
  5087. ' Result:=procedure',
  5088. ' var p: TEvent;',
  5089. ' begin',
  5090. ' Size:=Size;',
  5091. ' Size:=Self.Size;',
  5092. ' p:=@DoIt;',
  5093. ' p:[email protected];',
  5094. ' end;',
  5095. 'end;',
  5096. 'begin']);
  5097. ConvertProgram;
  5098. CheckSource('TestAnonymousProc_Class',
  5099. LinesToStr([ // statements
  5100. 'rtl.createClass(this, "TObject", null, function () {',
  5101. ' this.$init = function () {',
  5102. ' this.Size = 0;',
  5103. ' };',
  5104. ' this.$final = function () {',
  5105. ' };',
  5106. ' this.GetIt = function () {',
  5107. ' var $Self = this;',
  5108. ' var Result = null;',
  5109. ' Result = function () {',
  5110. ' var p = null;',
  5111. ' $Self.Size = $Self.Size;',
  5112. ' $Self.Size = $Self.Size;',
  5113. ' p = rtl.createCallback($Self, "DoIt");',
  5114. ' p = rtl.createCallback($Self, "DoIt");',
  5115. ' };',
  5116. ' return Result;',
  5117. ' };',
  5118. '});',
  5119. '']),
  5120. LinesToStr([
  5121. '']));
  5122. end;
  5123. procedure TTestModule.TestAnonymousProc_ForLoop;
  5124. begin
  5125. StartProgram(false);
  5126. Add([
  5127. 'type TProc = reference to procedure;',
  5128. 'procedure Foo(p: TProc);',
  5129. 'begin',
  5130. 'end;',
  5131. 'procedure DoIt;',
  5132. 'var i: word;',
  5133. ' a: word;',
  5134. 'begin',
  5135. ' for i:=1 to 10 do begin',
  5136. ' Foo(procedure begin a:=3; end);',
  5137. ' end;',
  5138. 'end;',
  5139. 'begin',
  5140. ' DoIt;']);
  5141. ConvertProgram;
  5142. CheckSource('TestAnonymousProc_ForLoop',
  5143. LinesToStr([ // statements
  5144. 'this.Foo = function (p) {',
  5145. '};',
  5146. 'this.DoIt = function () {',
  5147. ' var i = 0;',
  5148. ' var a = 0;',
  5149. ' for (i = 1; i <= 10; i++) {',
  5150. ' $mod.Foo(function () {',
  5151. ' a = 3;',
  5152. ' });',
  5153. ' };',
  5154. '};',
  5155. '']),
  5156. LinesToStr([
  5157. '$mod.DoIt();'
  5158. ]));
  5159. end;
  5160. procedure TTestModule.TestAnonymousProc_AsmDelphi;
  5161. begin
  5162. StartProgram(false);
  5163. Add([
  5164. '{$mode delphi}',
  5165. 'type',
  5166. ' TProc = reference to procedure;',
  5167. ' TFunc = reference to function(x: word): word;',
  5168. 'procedure Run;',
  5169. 'asm',
  5170. 'end;',
  5171. 'procedure Walk(p: TProc; f: TFunc);',
  5172. 'begin',
  5173. ' Walk(procedure asm end, function(b:word): word asm return 1+b; end);',
  5174. 'end;',
  5175. 'begin',
  5176. ' Walk(procedure',
  5177. ' asm',
  5178. ' console.log("a");',
  5179. ' end,',
  5180. ' function(x: word): word asm',
  5181. ' console.log("c");',
  5182. ' end);',
  5183. '']);
  5184. ConvertProgram;
  5185. CheckSource('TestAnonymousProc_AsmDelphi',
  5186. LinesToStr([ // statements
  5187. 'this.Run = function () {',
  5188. '};',
  5189. 'this.Walk = function (p, f) {',
  5190. ' $mod.Walk(function () {',
  5191. ' }, function (b) {',
  5192. ' return 1+b;',
  5193. ' });',
  5194. '};',
  5195. '']),
  5196. LinesToStr([
  5197. '$mod.Walk(function () {',
  5198. ' console.log("a");',
  5199. '}, function (x) {',
  5200. ' console.log("c");',
  5201. '});',
  5202. '']));
  5203. end;
  5204. procedure TTestModule.TestEnum_Name;
  5205. begin
  5206. StartProgram(false);
  5207. Add('type TMyEnum = (Red, Green, Blue);');
  5208. Add('var e: TMyEnum;');
  5209. Add('var f: TMyEnum = Blue;');
  5210. Add('begin');
  5211. Add(' e:=green;');
  5212. Add(' e:=default(TMyEnum);');
  5213. ConvertProgram;
  5214. CheckSource('TestEnum_Name',
  5215. LinesToStr([ // statements
  5216. 'this.TMyEnum = {',
  5217. ' "0":"Red",',
  5218. ' Red:0,',
  5219. ' "1":"Green",',
  5220. ' Green:1,',
  5221. ' "2":"Blue",',
  5222. ' Blue:2',
  5223. ' };',
  5224. 'this.e = 0;',
  5225. 'this.f = this.TMyEnum.Blue;'
  5226. ]),
  5227. LinesToStr([
  5228. '$mod.e=$mod.TMyEnum.Green;',
  5229. '$mod.e=$mod.TMyEnum.Red;'
  5230. ]));
  5231. end;
  5232. procedure TTestModule.TestEnum_Number;
  5233. begin
  5234. Converter.Options:=Converter.Options+[coEnumNumbers];
  5235. StartProgram(false);
  5236. Add('type TMyEnum = (Red, Green);');
  5237. Add('var');
  5238. Add(' e: TMyEnum;');
  5239. Add(' f: TMyEnum = Green;');
  5240. Add(' i: longint;');
  5241. Add('begin');
  5242. Add(' e:=green;');
  5243. Add(' i:=longint(e);');
  5244. ConvertProgram;
  5245. CheckSource('TestEnumNumber',
  5246. LinesToStr([ // statements
  5247. 'this.TMyEnum = {',
  5248. ' "0":"Red",',
  5249. ' Red:0,',
  5250. ' "1":"Green",',
  5251. ' Green:1',
  5252. ' };',
  5253. 'this.e = 0;',
  5254. 'this.f = 1;',
  5255. 'this.i = 0;'
  5256. ]),
  5257. LinesToStr([
  5258. '$mod.e=1;',
  5259. '$mod.i=$mod.e;'
  5260. ]));
  5261. end;
  5262. procedure TTestModule.TestEnum_ConstFail;
  5263. begin
  5264. StartProgram(false);
  5265. Add([
  5266. 'type TMyEnum = (Red = 100, Green = 101);',
  5267. 'var',
  5268. ' e: TMyEnum;',
  5269. ' f: TMyEnum = Green;',
  5270. 'begin',
  5271. ' e:=green;']);
  5272. SetExpectedPasResolverError('not yet implemented: Red:TPasEnumValue [20180126202434] "enum const"',3002);
  5273. ConvertProgram;
  5274. end;
  5275. procedure TTestModule.TestEnum_Functions;
  5276. begin
  5277. StartProgram(false);
  5278. Add([
  5279. 'type TMyEnum = (Red, Green);',
  5280. 'procedure DoIt(var e: TMyEnum; var i: word);',
  5281. 'var',
  5282. ' v: longint;',
  5283. ' s: string;',
  5284. 'begin',
  5285. ' val(s,e,v);',
  5286. ' val(s,e,i);',
  5287. 'end;',
  5288. 'var',
  5289. ' e: TMyEnum;',
  5290. ' i: longint;',
  5291. ' s: string;',
  5292. ' b: boolean;',
  5293. 'begin',
  5294. ' i:=ord(red);',
  5295. ' i:=ord(green);',
  5296. ' i:=ord(e);',
  5297. ' i:=ord(b);',
  5298. ' e:=low(tmyenum);',
  5299. ' e:=low(e);',
  5300. ' b:=low(boolean);',
  5301. ' e:=high(tmyenum);',
  5302. ' e:=high(e);',
  5303. ' b:=high(boolean);',
  5304. ' e:=pred(green);',
  5305. ' e:=pred(e);',
  5306. ' b:=pred(b);',
  5307. ' e:=succ(red);',
  5308. ' e:=succ(e);',
  5309. ' b:=succ(b);',
  5310. ' e:=tmyenum(1);',
  5311. ' e:=tmyenum(i);',
  5312. ' s:=str(e);',
  5313. ' str(e,s);',
  5314. ' str(red,s);',
  5315. ' s:=str(e:3);',
  5316. ' writestr(s,e:3,red);',
  5317. ' val(s,e,i);',
  5318. ' i:=longint(e);']);
  5319. ConvertProgram;
  5320. CheckSource('TestEnum_Functions',
  5321. LinesToStr([ // statements
  5322. 'this.TMyEnum = {',
  5323. ' "0":"Red",',
  5324. ' Red:0,',
  5325. ' "1":"Green",',
  5326. ' Green:1',
  5327. ' };',
  5328. 'this.DoIt = function (e, i) {',
  5329. ' var v = 0;',
  5330. ' var s = "";',
  5331. ' e.set(rtl.valEnum(s, $mod.TMyEnum, function (w) {',
  5332. ' v = w;',
  5333. ' }));',
  5334. ' e.set(rtl.valEnum(s, $mod.TMyEnum, i.set));',
  5335. '};',
  5336. 'this.e = 0;',
  5337. 'this.i = 0;',
  5338. 'this.s = "";',
  5339. 'this.b = false;',
  5340. '']),
  5341. LinesToStr([
  5342. '$mod.i=$mod.TMyEnum.Red;',
  5343. '$mod.i=$mod.TMyEnum.Green;',
  5344. '$mod.i=$mod.e;',
  5345. '$mod.i=$mod.b+0;',
  5346. '$mod.e=$mod.TMyEnum.Red;',
  5347. '$mod.e=$mod.TMyEnum.Red;',
  5348. '$mod.b=false;',
  5349. '$mod.e=$mod.TMyEnum.Green;',
  5350. '$mod.e=$mod.TMyEnum.Green;',
  5351. '$mod.b=true;',
  5352. '$mod.e=$mod.TMyEnum.Green-1;',
  5353. '$mod.e=$mod.e-1;',
  5354. '$mod.b=false;',
  5355. '$mod.e=$mod.TMyEnum.Red+1;',
  5356. '$mod.e=$mod.e+1;',
  5357. '$mod.b=true;',
  5358. '$mod.e=1;',
  5359. '$mod.e=$mod.i;',
  5360. '$mod.s = $mod.TMyEnum[$mod.e];',
  5361. '$mod.s = $mod.TMyEnum[$mod.e];',
  5362. '$mod.s = $mod.TMyEnum[$mod.TMyEnum.Red];',
  5363. '$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3);',
  5364. '$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3)+$mod.TMyEnum[$mod.TMyEnum.Red];',
  5365. '$mod.e = rtl.valEnum($mod.s, $mod.TMyEnum, function (v) {',
  5366. ' $mod.i = v;',
  5367. '});',
  5368. '$mod.i=$mod.e;',
  5369. '']));
  5370. end;
  5371. procedure TTestModule.TestEnumRg_Functions;
  5372. begin
  5373. StartProgram(false);
  5374. Add([
  5375. 'type',
  5376. ' TEnum = (Red, Green, Blue);',
  5377. ' TEnumRg = Green..Blue;',
  5378. 'procedure DoIt(var e: TEnumRg; var i: word);',
  5379. 'var',
  5380. ' v: longint;',
  5381. ' s: string;',
  5382. 'begin',
  5383. ' val(s,e,v);',
  5384. ' val(s,e,i);',
  5385. 'end;',
  5386. 'var',
  5387. ' e: TEnumRg;',
  5388. ' i: longint;',
  5389. ' s: string;',
  5390. 'begin',
  5391. ' i:=ord(green);',
  5392. ' i:=ord(e);',
  5393. ' e:=low(tenumrg);',
  5394. ' e:=low(e);',
  5395. ' e:=high(tenumrg);',
  5396. ' e:=high(e);',
  5397. ' e:=pred(blue);',
  5398. ' e:=pred(e);',
  5399. ' e:=succ(green);',
  5400. ' e:=succ(e);',
  5401. ' e:=tenumrg(1);',
  5402. ' e:=tenumrg(i);',
  5403. ' s:=str(e);',
  5404. ' str(e,s);',
  5405. ' str(red,s);',
  5406. ' s:=str(e:3);',
  5407. ' writestr(s,e:3,blue);',
  5408. ' val(s,e,i);',
  5409. ' i:=longint(e);']);
  5410. ConvertProgram;
  5411. CheckSource('TestEnumRg_Functions',
  5412. LinesToStr([ // statements
  5413. 'this.TEnum = {',
  5414. ' "0":"Red",',
  5415. ' Red:0,',
  5416. ' "1":"Green",',
  5417. ' Green:1,',
  5418. ' "2":"Blue",',
  5419. ' Blue:2',
  5420. ' };',
  5421. 'this.DoIt = function (e, i) {',
  5422. ' var v = 0;',
  5423. ' var s = "";',
  5424. ' e.set(rtl.valEnum(s, $mod.TEnum, function (w) {',
  5425. ' v = w;',
  5426. ' }));',
  5427. ' e.set(rtl.valEnum(s, $mod.TEnum, i.set));',
  5428. '};',
  5429. 'this.e = this.TEnum.Green;',
  5430. 'this.i = 0;',
  5431. 'this.s = "";',
  5432. '']),
  5433. LinesToStr([
  5434. '$mod.i=$mod.TEnum.Green;',
  5435. '$mod.i=$mod.e;',
  5436. '$mod.e=$mod.TEnum.Green;',
  5437. '$mod.e=$mod.TEnum.Green;',
  5438. '$mod.e=$mod.TEnum.Blue;',
  5439. '$mod.e=$mod.TEnum.Blue;',
  5440. '$mod.e=$mod.TEnum.Blue-1;',
  5441. '$mod.e=$mod.e-1;',
  5442. '$mod.e=$mod.TEnum.Green+1;',
  5443. '$mod.e=$mod.e+1;',
  5444. '$mod.e=1;',
  5445. '$mod.e=$mod.i;',
  5446. '$mod.s = $mod.TEnum[$mod.e];',
  5447. '$mod.s = $mod.TEnum[$mod.e];',
  5448. '$mod.s = $mod.TEnum[$mod.TEnum.Red];',
  5449. '$mod.s = rtl.spaceLeft($mod.TEnum[$mod.e], 3);',
  5450. '$mod.s = rtl.spaceLeft($mod.TEnum[$mod.e], 3)+$mod.TEnum[$mod.TEnum.Blue];',
  5451. '$mod.e = rtl.valEnum($mod.s, $mod.TEnum, function (v) {',
  5452. ' $mod.i = v;',
  5453. '});',
  5454. '$mod.i=$mod.e;',
  5455. '']));
  5456. end;
  5457. procedure TTestModule.TestEnum_AsParams;
  5458. begin
  5459. StartProgram(false);
  5460. Add('type TEnum = (Red,Blue);');
  5461. Add('procedure DoIt(vG: TEnum; const vH: TEnum; var vI: TEnum);');
  5462. Add('var vJ: TEnum;');
  5463. Add('begin');
  5464. Add(' vg:=vg;');
  5465. Add(' vj:=vh;');
  5466. Add(' vi:=vi;');
  5467. Add(' doit(vg,vg,vg);');
  5468. Add(' doit(vh,vh,vj);');
  5469. Add(' doit(vi,vi,vi);');
  5470. Add(' doit(vj,vj,vj);');
  5471. Add('end;');
  5472. Add('var i: TEnum;');
  5473. Add('begin');
  5474. Add(' doit(i,i,i);');
  5475. ConvertProgram;
  5476. CheckSource('TestEnum_AsParams',
  5477. LinesToStr([ // statements
  5478. 'this.TEnum = {',
  5479. ' "0": "Red",',
  5480. ' Red: 0,',
  5481. ' "1": "Blue",',
  5482. ' Blue: 1',
  5483. '};',
  5484. 'this.DoIt = function (vG,vH,vI) {',
  5485. ' var vJ = 0;',
  5486. ' vG = vG;',
  5487. ' vJ = vH;',
  5488. ' vI.set(vI.get());',
  5489. ' $mod.DoIt(vG, vG, {',
  5490. ' get: function () {',
  5491. ' return vG;',
  5492. ' },',
  5493. ' set: function (v) {',
  5494. ' vG = v;',
  5495. ' }',
  5496. ' });',
  5497. ' $mod.DoIt(vH, vH, {',
  5498. ' get: function () {',
  5499. ' return vJ;',
  5500. ' },',
  5501. ' set: function (v) {',
  5502. ' vJ = v;',
  5503. ' }',
  5504. ' });',
  5505. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  5506. ' $mod.DoIt(vJ, vJ, {',
  5507. ' get: function () {',
  5508. ' return vJ;',
  5509. ' },',
  5510. ' set: function (v) {',
  5511. ' vJ = v;',
  5512. ' }',
  5513. ' });',
  5514. '};',
  5515. 'this.i = 0;'
  5516. ]),
  5517. LinesToStr([
  5518. '$mod.DoIt($mod.i,$mod.i,{',
  5519. ' p: $mod,',
  5520. ' get: function () {',
  5521. ' return this.p.i;',
  5522. ' },',
  5523. ' set: function (v) {',
  5524. ' this.p.i = v;',
  5525. ' }',
  5526. '});'
  5527. ]));
  5528. end;
  5529. procedure TTestModule.TestEnumRange_Array;
  5530. begin
  5531. StartProgram(false);
  5532. Add([
  5533. 'type',
  5534. ' TEnum = (Red, Green, Blue);',
  5535. ' TEnumRg = green..blue;',
  5536. ' TArr = array[TEnumRg] of byte;',
  5537. ' TArr2 = array[green..blue] of byte;',
  5538. 'var',
  5539. ' a: TArr;',
  5540. ' b: TArr = (3,4);',
  5541. ' c: TArr2 = (5,6);',
  5542. 'begin',
  5543. ' a[green] := b[blue];',
  5544. ' c[green] := c[blue];',
  5545. '']);
  5546. ConvertProgram;
  5547. CheckSource('TestEnumRange_Array',
  5548. LinesToStr([ // statements
  5549. 'this.TEnum = {',
  5550. ' "0": "Red",',
  5551. ' Red: 0,',
  5552. ' "1": "Green",',
  5553. ' Green: 1,',
  5554. ' "2": "Blue",',
  5555. ' Blue: 2',
  5556. '};',
  5557. 'this.a = rtl.arraySetLength(null, 0, 2);',
  5558. 'this.b = [3, 4];',
  5559. 'this.c = [5, 6];',
  5560. '']),
  5561. LinesToStr([
  5562. ' $mod.a[$mod.TEnum.Green - 1] = $mod.b[$mod.TEnum.Blue - 1];',
  5563. ' $mod.c[$mod.TEnum.Green - 1] = $mod.c[$mod.TEnum.Blue - 1];',
  5564. '']));
  5565. end;
  5566. procedure TTestModule.TestEnum_ForIn;
  5567. begin
  5568. StartProgram(false);
  5569. Add([
  5570. 'type',
  5571. ' TEnum = (Red, Green, Blue);',
  5572. ' TEnumRg = green..blue;',
  5573. ' TArr = array[TEnum] of byte;',
  5574. ' TArrRg = array[TEnumRg] of byte;',
  5575. 'var',
  5576. ' e: TEnum;',
  5577. ' a1: TArr = (3,4,5);',
  5578. ' a2: TArrRg = (11,12);',
  5579. ' b: byte;',
  5580. 'begin',
  5581. ' for e in TEnum do ;',
  5582. ' for e in TEnumRg do ;',
  5583. ' for e in TArr do ;',
  5584. ' for e in TArrRg do ;',
  5585. ' for b in a1 do ;',
  5586. ' for b in a2 do ;',
  5587. '']);
  5588. ConvertProgram;
  5589. CheckSource('TestEnum_ForIn',
  5590. LinesToStr([ // statements
  5591. 'this.TEnum = {',
  5592. ' "0": "Red",',
  5593. ' Red: 0,',
  5594. ' "1": "Green",',
  5595. ' Green: 1,',
  5596. ' "2": "Blue",',
  5597. ' Blue: 2',
  5598. '};',
  5599. 'this.e = 0;',
  5600. 'this.a1 = [3, 4, 5];',
  5601. 'this.a2 = [11, 12];',
  5602. 'this.b = 0;',
  5603. '']),
  5604. LinesToStr([
  5605. ' for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  5606. ' for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  5607. ' for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  5608. ' for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  5609. ' for (var $in = $mod.a1, $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) $mod.b = $in[$l];',
  5610. ' for (var $in1 = $mod.a2, $l1 = 0, $end1 = rtl.length($in1) - 1; $l1 <= $end1; $l1++) $mod.b = $in1[$l1];',
  5611. '']));
  5612. end;
  5613. procedure TTestModule.TestEnum_ScopedNumber;
  5614. begin
  5615. Converter.Options:=Converter.Options+[coEnumNumbers];
  5616. StartProgram(false);
  5617. Add([
  5618. 'type',
  5619. ' TEnum = (Red, Green);',
  5620. 'var',
  5621. ' e: TEnum;',
  5622. 'begin',
  5623. ' e:=TEnum.Green;',
  5624. '']);
  5625. ConvertProgram;
  5626. CheckSource('TestEnum_ScopedNumber',
  5627. LinesToStr([ // statements
  5628. 'this.TEnum = {',
  5629. ' "0": "Red",',
  5630. ' Red: 0,',
  5631. ' "1": "Green",',
  5632. ' Green: 1',
  5633. '};',
  5634. 'this.e = 0;',
  5635. '']),
  5636. LinesToStr([
  5637. '$mod.e = 1;']));
  5638. end;
  5639. procedure TTestModule.TestEnum_InFunction;
  5640. begin
  5641. StartProgram(false);
  5642. Add([
  5643. 'const TEnum = 3;',
  5644. 'procedure DoIt;',
  5645. 'type',
  5646. ' TEnum = (Red, Green, Blue);',
  5647. ' procedure Sub;',
  5648. ' type',
  5649. ' TEnumSub = (Left, Right);',
  5650. ' var',
  5651. ' es: TEnumSub;',
  5652. ' begin',
  5653. ' es:=Left;',
  5654. ' end;',
  5655. 'var',
  5656. ' e, e2: TEnum;',
  5657. 'begin',
  5658. ' if e in [red,blue] then e2:=e;',
  5659. 'end;',
  5660. 'begin']);
  5661. ConvertProgram;
  5662. CheckSource('TestEnum_InFunction',
  5663. LinesToStr([ // statements
  5664. 'this.TEnum = 3;',
  5665. 'var TEnum$1 = {',
  5666. ' "0":"Red",',
  5667. ' Red:0,',
  5668. ' "1":"Green",',
  5669. ' Green:1,',
  5670. ' "2":"Blue",',
  5671. ' Blue:2',
  5672. ' };',
  5673. 'var TEnumSub = {',
  5674. ' "0": "Left",',
  5675. ' Left: 0,',
  5676. ' "1": "Right",',
  5677. ' Right: 1',
  5678. '};',
  5679. 'this.DoIt = function () {',
  5680. ' function Sub() {',
  5681. ' var es = 0;',
  5682. ' es = TEnumSub.Left;',
  5683. ' };',
  5684. ' var e = 0;',
  5685. ' var e2 = 0;',
  5686. ' if (e in rtl.createSet(TEnum$1.Red, TEnum$1.Blue)) e2 = e;',
  5687. '};',
  5688. '']),
  5689. LinesToStr([
  5690. '']));
  5691. end;
  5692. procedure TTestModule.TestEnum_Name_Anonymous_Unit;
  5693. begin
  5694. StartUnit(true);
  5695. Add([
  5696. 'interface',
  5697. 'var color: (red, green);',
  5698. 'implementation',
  5699. 'initialization',
  5700. ' color:=green;',
  5701. '']);
  5702. ConvertUnit;
  5703. CheckSource('TestEnum_Name_Anonymous_Unit',
  5704. LinesToStr([
  5705. 'this.color$a = {',
  5706. ' "0": "red",',
  5707. ' red: 0,',
  5708. ' "1": "green",',
  5709. ' green: 1',
  5710. '};',
  5711. 'this.color = 0;',
  5712. '']),
  5713. LinesToStr([ // this.$init
  5714. '$mod.color = $mod.color$a.green;',
  5715. '']),
  5716. LinesToStr([ // implementation
  5717. '']) );
  5718. end;
  5719. procedure TTestModule.TestSet_Enum;
  5720. begin
  5721. StartProgram(false);
  5722. Add([
  5723. 'type',
  5724. ' TColor = (Red, Green, Blue);',
  5725. ' TColors = set of TColor;',
  5726. 'var',
  5727. ' c: TColor;',
  5728. ' s: TColors;',
  5729. ' t: TColors = [];',
  5730. ' u: TColors = [Red];',
  5731. 'begin',
  5732. ' s:=[];',
  5733. ' s:=[Green];',
  5734. ' s:=[Green,Blue];',
  5735. ' s:=[Red..Blue];',
  5736. ' s:=[Red,Green..Blue];',
  5737. ' s:=[Red,c];',
  5738. ' s:=t;',
  5739. ' s:=default(TColors);',
  5740. '']);
  5741. ConvertProgram;
  5742. CheckSource('TestSet',
  5743. LinesToStr([ // statements
  5744. 'this.TColor = {',
  5745. ' "0":"Red",',
  5746. ' Red:0,',
  5747. ' "1":"Green",',
  5748. ' Green:1,',
  5749. ' "2":"Blue",',
  5750. ' Blue:2',
  5751. ' };',
  5752. 'this.c = 0;',
  5753. 'this.s = {};',
  5754. 'this.t = {};',
  5755. 'this.u = rtl.createSet(this.TColor.Red);'
  5756. ]),
  5757. LinesToStr([
  5758. '$mod.s={};',
  5759. '$mod.s=rtl.createSet($mod.TColor.Green);',
  5760. '$mod.s=rtl.createSet($mod.TColor.Green,$mod.TColor.Blue);',
  5761. '$mod.s=rtl.createSet(null,$mod.TColor.Red,$mod.TColor.Blue);',
  5762. '$mod.s=rtl.createSet($mod.TColor.Red,null,$mod.TColor.Green,$mod.TColor.Blue);',
  5763. '$mod.s=rtl.createSet($mod.TColor.Red,$mod.c);',
  5764. '$mod.s=rtl.refSet($mod.t);',
  5765. '$mod.s={};',
  5766. '']));
  5767. end;
  5768. procedure TTestModule.TestSet_Operators;
  5769. begin
  5770. StartProgram(false);
  5771. Add('type');
  5772. Add(' TColor = (Red, Green, Blue);');
  5773. Add(' TColors = set of tcolor;');
  5774. Add('var');
  5775. Add(' vC: TColor;');
  5776. Add(' vS: TColors;');
  5777. Add(' vT: TColors;');
  5778. Add(' vU: TColors;');
  5779. Add(' B: boolean;');
  5780. Add('begin');
  5781. Add(' include(vs,green);');
  5782. Add(' exclude(vs,vc);');
  5783. Add(' vs:=vt+vu;');
  5784. Add(' vs:=vt+[red];');
  5785. Add(' vs:=[red]+vt;');
  5786. Add(' vs:=[red]+[green];');
  5787. Add(' vs:=vt-vu;');
  5788. Add(' vs:=vt-[red];');
  5789. Add(' vs:=[red]-vt;');
  5790. Add(' vs:=[red]-[green];');
  5791. Add(' vs:=vt*vu;');
  5792. Add(' vs:=vt*[red];');
  5793. Add(' vs:=[red]*vt;');
  5794. Add(' vs:=[red]*[green];');
  5795. Add(' vs:=vt><vu;');
  5796. Add(' vs:=vt><[red];');
  5797. Add(' vs:=[red]><vt;');
  5798. Add(' vs:=[red]><[green];');
  5799. Add(' b:=vt=vu;');
  5800. Add(' b:=vt=[red];');
  5801. Add(' b:=[red]=vt;');
  5802. Add(' b:=[red]=[green];');
  5803. Add(' b:=vt<>vu;');
  5804. Add(' b:=vt<>[red];');
  5805. Add(' b:=[red]<>vt;');
  5806. Add(' b:=[red]<>[green];');
  5807. Add(' b:=vt<=vu;');
  5808. Add(' b:=vt<=[red];');
  5809. Add(' b:=[red]<=vt;');
  5810. Add(' b:=[red]<=[green];');
  5811. Add(' b:=vt>=vu;');
  5812. Add(' b:=vt>=[red];');
  5813. Add(' b:=[red]>=vt;');
  5814. Add(' b:=[red]>=[green];');
  5815. ConvertProgram;
  5816. CheckSource('TestSet_Operators',
  5817. LinesToStr([ // statements
  5818. 'this.TColor = {',
  5819. ' "0":"Red",',
  5820. ' Red:0,',
  5821. ' "1":"Green",',
  5822. ' Green:1,',
  5823. ' "2":"Blue",',
  5824. ' Blue:2',
  5825. ' };',
  5826. 'this.vC = 0;',
  5827. 'this.vS = {};',
  5828. 'this.vT = {};',
  5829. 'this.vU = {};',
  5830. 'this.B = false;'
  5831. ]),
  5832. LinesToStr([
  5833. '$mod.vS = rtl.includeSet($mod.vS,$mod.TColor.Green);',
  5834. '$mod.vS = rtl.excludeSet($mod.vS,$mod.vC);',
  5835. '$mod.vS = rtl.unionSet($mod.vT, $mod.vU);',
  5836. '$mod.vS = rtl.unionSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5837. '$mod.vS = rtl.unionSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5838. '$mod.vS = rtl.unionSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5839. '$mod.vS = rtl.diffSet($mod.vT, $mod.vU);',
  5840. '$mod.vS = rtl.diffSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5841. '$mod.vS = rtl.diffSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5842. '$mod.vS = rtl.diffSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5843. '$mod.vS = rtl.intersectSet($mod.vT, $mod.vU);',
  5844. '$mod.vS = rtl.intersectSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5845. '$mod.vS = rtl.intersectSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5846. '$mod.vS = rtl.intersectSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5847. '$mod.vS = rtl.symDiffSet($mod.vT, $mod.vU);',
  5848. '$mod.vS = rtl.symDiffSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5849. '$mod.vS = rtl.symDiffSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5850. '$mod.vS = rtl.symDiffSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5851. '$mod.B = rtl.eqSet($mod.vT, $mod.vU);',
  5852. '$mod.B = rtl.eqSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5853. '$mod.B = rtl.eqSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5854. '$mod.B = rtl.eqSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5855. '$mod.B = rtl.neSet($mod.vT, $mod.vU);',
  5856. '$mod.B = rtl.neSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5857. '$mod.B = rtl.neSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5858. '$mod.B = rtl.neSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5859. '$mod.B = rtl.leSet($mod.vT, $mod.vU);',
  5860. '$mod.B = rtl.leSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5861. '$mod.B = rtl.leSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5862. '$mod.B = rtl.leSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5863. '$mod.B = rtl.geSet($mod.vT, $mod.vU);',
  5864. '$mod.B = rtl.geSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5865. '$mod.B = rtl.geSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5866. '$mod.B = rtl.geSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5867. '']));
  5868. end;
  5869. procedure TTestModule.TestSet_Operator_In;
  5870. begin
  5871. StartProgram(false);
  5872. Add([
  5873. 'type',
  5874. ' TColor = (Red, Green, Blue);',
  5875. ' TColors = set of tcolor;',
  5876. ' TColorRg = green..blue;',
  5877. 'var',
  5878. ' vC: tcolor;',
  5879. ' vT: tcolors;',
  5880. ' B: boolean;',
  5881. ' rg: TColorRg;',
  5882. 'begin',
  5883. ' b:=red in vt;',
  5884. ' b:=vc in vt;',
  5885. ' b:=green in [red..blue];',
  5886. ' b:=vc in [red..blue];',
  5887. ' ',
  5888. ' if red in vt then ;',
  5889. ' while vC in vt do ;',
  5890. ' repeat',
  5891. ' until vC in vt;',
  5892. ' if rg in [green..blue] then ;',
  5893. '']);
  5894. ConvertProgram;
  5895. CheckSource('TestSet_Operator_In',
  5896. LinesToStr([ // statements
  5897. 'this.TColor = {',
  5898. ' "0":"Red",',
  5899. ' Red:0,',
  5900. ' "1":"Green",',
  5901. ' Green:1,',
  5902. ' "2":"Blue",',
  5903. ' Blue:2',
  5904. ' };',
  5905. 'this.vC = 0;',
  5906. 'this.vT = {};',
  5907. 'this.B = false;',
  5908. 'this.rg = this.TColor.Green;',
  5909. '']),
  5910. LinesToStr([
  5911. '$mod.B = $mod.TColor.Red in $mod.vT;',
  5912. '$mod.B = $mod.vC in $mod.vT;',
  5913. '$mod.B = $mod.TColor.Green in rtl.createSet(null, $mod.TColor.Red, $mod.TColor.Blue);',
  5914. '$mod.B = $mod.vC in rtl.createSet(null, $mod.TColor.Red, $mod.TColor.Blue);',
  5915. 'if ($mod.TColor.Red in $mod.vT) ;',
  5916. 'while ($mod.vC in $mod.vT) {',
  5917. '};',
  5918. 'do {',
  5919. '} while (!($mod.vC in $mod.vT));',
  5920. 'if ($mod.rg in rtl.createSet(null, $mod.TColor.Green, $mod.TColor.Blue)) ;',
  5921. '']));
  5922. end;
  5923. procedure TTestModule.TestSet_Functions;
  5924. begin
  5925. StartProgram(false);
  5926. Add('type');
  5927. Add(' TMyEnum = (Red, Green);');
  5928. Add(' TMyEnums = set of TMyEnum;');
  5929. Add('var');
  5930. Add(' e: TMyEnum;');
  5931. Add(' s: TMyEnums;');
  5932. Add('begin');
  5933. Add(' e:=Low(TMyEnums);');
  5934. Add(' e:=Low(s);');
  5935. Add(' e:=High(TMyEnums);');
  5936. Add(' e:=High(s);');
  5937. ConvertProgram;
  5938. CheckSource('TestSetFunctions',
  5939. LinesToStr([ // statements
  5940. 'this.TMyEnum = {',
  5941. ' "0":"Red",',
  5942. ' Red:0,',
  5943. ' "1":"Green",',
  5944. ' Green:1',
  5945. ' };',
  5946. 'this.e = 0;',
  5947. 'this.s = {};'
  5948. ]),
  5949. LinesToStr([
  5950. '$mod.e=$mod.TMyEnum.Red;',
  5951. '$mod.e=$mod.TMyEnum.Red;',
  5952. '$mod.e=$mod.TMyEnum.Green;',
  5953. '$mod.e=$mod.TMyEnum.Green;',
  5954. '']));
  5955. end;
  5956. procedure TTestModule.TestSet_PassAsArgClone;
  5957. begin
  5958. StartProgram(false);
  5959. Add('type');
  5960. Add(' TMyEnum = (Red, Green);');
  5961. Add(' TMyEnums = set of TMyEnum;');
  5962. Add('procedure DoDefault(s: tmyenums); begin end;');
  5963. Add('procedure DoConst(const s: tmyenums); begin end;');
  5964. Add('var');
  5965. Add(' aSet: tmyenums;');
  5966. Add('begin');
  5967. Add(' dodefault(aset);');
  5968. Add(' doconst(aset);');
  5969. ConvertProgram;
  5970. CheckSource('TestSetFunctions',
  5971. LinesToStr([ // statements
  5972. 'this.TMyEnum = {',
  5973. ' "0":"Red",',
  5974. ' Red:0,',
  5975. ' "1":"Green",',
  5976. ' Green:1',
  5977. ' };',
  5978. 'this.DoDefault = function (s) {',
  5979. '};',
  5980. 'this.DoConst = function (s) {',
  5981. '};',
  5982. 'this.aSet = {};'
  5983. ]),
  5984. LinesToStr([
  5985. '$mod.DoDefault(rtl.refSet($mod.aSet));',
  5986. '$mod.DoConst($mod.aSet);',
  5987. '']));
  5988. end;
  5989. procedure TTestModule.TestSet_AsParams;
  5990. begin
  5991. StartProgram(false);
  5992. Add([
  5993. 'type TEnum = (Red,Blue);',
  5994. 'type TEnums = set of TEnum;',
  5995. 'function DoIt(vG: TEnums; const vH: TEnums; var vI: TEnums): TEnums;',
  5996. 'var vJ: TEnums;',
  5997. 'begin',
  5998. ' Include(vg,red);',
  5999. ' Include(result,blue);',
  6000. ' vg:=vg;',
  6001. ' vj:=vh;',
  6002. ' vi:=vi;',
  6003. ' doit(vg,vg,vg);',
  6004. ' doit(vh,vh,vj);',
  6005. ' doit(vi,vi,vi);',
  6006. ' doit(vj,vj,vj);',
  6007. 'end;',
  6008. 'var i: TEnums;',
  6009. 'begin',
  6010. ' doit(i,i,i);']);
  6011. ConvertProgram;
  6012. CheckSource('TestSet_AsParams',
  6013. LinesToStr([ // statements
  6014. 'this.TEnum = {',
  6015. ' "0": "Red",',
  6016. ' Red: 0,',
  6017. ' "1": "Blue",',
  6018. ' Blue: 1',
  6019. '};',
  6020. 'this.DoIt = function (vG,vH,vI) {',
  6021. ' var Result = {};',
  6022. ' var vJ = {};',
  6023. ' vG = rtl.includeSet(vG, $mod.TEnum.Red);',
  6024. ' Result = rtl.includeSet(Result, $mod.TEnum.Blue);',
  6025. ' vG = rtl.refSet(vG);',
  6026. ' vJ = rtl.refSet(vH);',
  6027. ' vI.set(rtl.refSet(vI.get()));',
  6028. ' $mod.DoIt(rtl.refSet(vG), vG, {',
  6029. ' get: function () {',
  6030. ' return vG;',
  6031. ' },',
  6032. ' set: function (v) {',
  6033. ' vG = v;',
  6034. ' }',
  6035. ' });',
  6036. ' $mod.DoIt(rtl.refSet(vH), vH, {',
  6037. ' get: function () {',
  6038. ' return vJ;',
  6039. ' },',
  6040. ' set: function (v) {',
  6041. ' vJ = v;',
  6042. ' }',
  6043. ' });',
  6044. ' $mod.DoIt(rtl.refSet(vI.get()), vI.get(), vI);',
  6045. ' $mod.DoIt(rtl.refSet(vJ), vJ, {',
  6046. ' get: function () {',
  6047. ' return vJ;',
  6048. ' },',
  6049. ' set: function (v) {',
  6050. ' vJ = v;',
  6051. ' }',
  6052. ' });',
  6053. ' return Result;',
  6054. '};',
  6055. 'this.i = {};'
  6056. ]),
  6057. LinesToStr([
  6058. '$mod.DoIt(rtl.refSet($mod.i),$mod.i,{',
  6059. ' p: $mod,',
  6060. ' get: function () {',
  6061. ' return this.p.i;',
  6062. ' },',
  6063. ' set: function (v) {',
  6064. ' this.p.i = v;',
  6065. ' }',
  6066. '});'
  6067. ]));
  6068. end;
  6069. procedure TTestModule.TestSet_Property;
  6070. begin
  6071. StartProgram(false);
  6072. Add('type');
  6073. Add(' TEnum = (Red,Blue);');
  6074. Add(' TEnums = set of TEnum;');
  6075. Add(' TObject = class');
  6076. Add(' function GetColors: TEnums; external name ''GetColors'';');
  6077. Add(' procedure SetColors(const Value: TEnums); external name ''SetColors'';');
  6078. Add(' property Colors: TEnums read GetColors write SetColors;');
  6079. Add(' end;');
  6080. Add('procedure DoIt(i: TEnums; const j: TEnums; var k: TEnums; out l: TEnums);');
  6081. Add('begin end;');
  6082. Add('var Obj: TObject;');
  6083. Add('begin');
  6084. Add(' Include(Obj.Colors,Red);');
  6085. Add(' Exclude(Obj.Colors,Red);');
  6086. //Add(' DoIt(Obj.Colors,Obj.Colors,Obj.Colors,Obj.Colors);');
  6087. ConvertProgram;
  6088. CheckSource('TestSet_Property',
  6089. LinesToStr([ // statements
  6090. 'this.TEnum = {',
  6091. ' "0": "Red",',
  6092. ' Red: 0,',
  6093. ' "1": "Blue",',
  6094. ' Blue: 1',
  6095. '};',
  6096. 'rtl.createClass(this, "TObject", null, function () {',
  6097. ' this.$init = function () {',
  6098. ' };',
  6099. ' this.$final = function () {',
  6100. ' };',
  6101. '});',
  6102. 'this.DoIt = function (i, j, k, l) {',
  6103. '};',
  6104. 'this.Obj = null;',
  6105. '']),
  6106. LinesToStr([
  6107. '$mod.Obj.SetColors(rtl.includeSet($mod.Obj.GetColors(), $mod.TEnum.Red));',
  6108. '$mod.Obj.SetColors(rtl.excludeSet($mod.Obj.GetColors(), $mod.TEnum.Red));',
  6109. '']));
  6110. end;
  6111. procedure TTestModule.TestSet_EnumConst;
  6112. begin
  6113. StartProgram(false);
  6114. Add([
  6115. 'type',
  6116. ' TEnum = (Red,Blue);',
  6117. ' TEnums = set of TEnum;',
  6118. 'const',
  6119. ' Orange = red;',
  6120. 'var',
  6121. ' Enum: tenum;',
  6122. ' Enums: tenums;',
  6123. 'begin',
  6124. ' Include(enums,orange);',
  6125. ' Exclude(enums,orange);',
  6126. ' if orange in enums then;',
  6127. ' if orange in [orange,red] then;']);
  6128. ConvertProgram;
  6129. CheckSource('TestSet_EnumConst',
  6130. LinesToStr([ // statements
  6131. 'this.TEnum = {',
  6132. ' "0": "Red",',
  6133. ' Red: 0,',
  6134. ' "1": "Blue",',
  6135. ' Blue: 1',
  6136. '};',
  6137. 'this.Orange = this.TEnum.Red;',
  6138. 'this.Enum = 0;',
  6139. 'this.Enums = {};',
  6140. '']),
  6141. LinesToStr([
  6142. '$mod.Enums = rtl.includeSet($mod.Enums, $mod.TEnum.Red);',
  6143. '$mod.Enums = rtl.excludeSet($mod.Enums, $mod.TEnum.Red);',
  6144. 'if ($mod.TEnum.Red in $mod.Enums) ;',
  6145. 'if ($mod.TEnum.Red in rtl.createSet($mod.TEnum.Red, $mod.TEnum.Red)) ;',
  6146. '']));
  6147. end;
  6148. procedure TTestModule.TestSet_IntConst;
  6149. begin
  6150. StartProgram(false);
  6151. Add([
  6152. 'type',
  6153. ' TEnums = set of Byte;',
  6154. 'const',
  6155. ' Orange = 0;',
  6156. 'var',
  6157. ' Enum: byte;',
  6158. ' Enums: tenums;',
  6159. 'begin',
  6160. ' Enums:=[];',
  6161. ' Enums:=[0];',
  6162. ' Enums:=[1..2];',
  6163. //' Include(enums,orange);',
  6164. //' Exclude(enums,orange);',
  6165. ' if orange in enums then;',
  6166. ' if orange in [orange,1] then;']);
  6167. ConvertProgram;
  6168. CheckSource('TestSet_IntConst',
  6169. LinesToStr([ // statements
  6170. 'this.Orange = 0;',
  6171. 'this.Enum = 0;',
  6172. 'this.Enums = {};',
  6173. '']),
  6174. LinesToStr([
  6175. '$mod.Enums = {};',
  6176. '$mod.Enums = rtl.createSet(0);',
  6177. '$mod.Enums = rtl.createSet(null, 1, 2);',
  6178. 'if (0 in $mod.Enums) ;',
  6179. 'if (0 in rtl.createSet(0, 1)) ;',
  6180. '']));
  6181. end;
  6182. procedure TTestModule.TestSet_IntRange;
  6183. begin
  6184. StartProgram(false);
  6185. Add([
  6186. 'type',
  6187. ' TRange = 1..3;',
  6188. ' TEnums = set of TRange;',
  6189. 'const',
  6190. ' Orange = 2;',
  6191. 'var',
  6192. ' Enum: byte;',
  6193. ' Enums: TEnums;',
  6194. 'begin',
  6195. ' Enums:=[];',
  6196. ' Enums:=[1];',
  6197. ' Enums:=[2..3];',
  6198. ' Include(enums,orange);',
  6199. ' Exclude(enums,orange);',
  6200. ' if orange in enums then;',
  6201. ' if orange in [orange,1] then;']);
  6202. ConvertProgram;
  6203. CheckSource('TestSet_IntRange',
  6204. LinesToStr([ // statements
  6205. 'this.Orange = 2;',
  6206. 'this.Enum = 0;',
  6207. 'this.Enums = {};',
  6208. '']),
  6209. LinesToStr([
  6210. '$mod.Enums = {};',
  6211. '$mod.Enums = rtl.createSet(1);',
  6212. '$mod.Enums = rtl.createSet(null, 2, 3);',
  6213. '$mod.Enums = rtl.includeSet($mod.Enums, 2);',
  6214. '$mod.Enums = rtl.excludeSet($mod.Enums, 2);',
  6215. 'if (2 in $mod.Enums) ;',
  6216. 'if (2 in rtl.createSet(2, 1)) ;',
  6217. '']));
  6218. end;
  6219. procedure TTestModule.TestSet_AnonymousEnumType;
  6220. begin
  6221. StartProgram(false);
  6222. Add('type');
  6223. Add(' TFlags = set of (red, green);');
  6224. Add('const');
  6225. Add(' favorite = red;');
  6226. Add('var');
  6227. Add(' f: TFlags;');
  6228. Add(' i: longint;');
  6229. Add('begin');
  6230. Add(' Include(f,red);');
  6231. Add(' Include(f,favorite);');
  6232. Add(' i:=ord(red);');
  6233. Add(' i:=ord(favorite);');
  6234. Add(' i:=ord(low(TFlags));');
  6235. Add(' i:=ord(low(f));');
  6236. Add(' i:=ord(low(favorite));');
  6237. Add(' i:=ord(high(TFlags));');
  6238. Add(' i:=ord(high(f));');
  6239. Add(' i:=ord(high(favorite));');
  6240. Add(' f:=[green,favorite];');
  6241. ConvertProgram;
  6242. CheckSource('TestSet_AnonymousEnumType',
  6243. LinesToStr([ // statements
  6244. 'this.TFlags$a = {',
  6245. ' "0": "red",',
  6246. ' red: 0,',
  6247. ' "1": "green",',
  6248. ' green: 1',
  6249. '};',
  6250. 'this.favorite = this.TFlags$a.red;',
  6251. 'this.f = {};',
  6252. 'this.i = 0;',
  6253. '']),
  6254. LinesToStr([
  6255. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  6256. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  6257. '$mod.i = $mod.TFlags$a.red;',
  6258. '$mod.i = $mod.TFlags$a.red;',
  6259. '$mod.i = $mod.TFlags$a.red;',
  6260. '$mod.i = $mod.TFlags$a.red;',
  6261. '$mod.i = $mod.TFlags$a.red;',
  6262. '$mod.i = $mod.TFlags$a.green;',
  6263. '$mod.i = $mod.TFlags$a.green;',
  6264. '$mod.i = $mod.TFlags$a.green;',
  6265. '$mod.f = rtl.createSet($mod.TFlags$a.green, $mod.TFlags$a.red);',
  6266. '']));
  6267. end;
  6268. procedure TTestModule.TestSet_AnonymousEnumTypeChar;
  6269. begin
  6270. exit;
  6271. StartProgram(false);
  6272. Add([
  6273. 'type',
  6274. ' TAtoZ = ''A''..''Z'';',
  6275. ' TSetOfAZ = set of TAtoZ;',
  6276. 'var',
  6277. ' c: char;',
  6278. ' a: TAtoZ;',
  6279. ' s: TSetOfAZ = [''P'',''A''];',
  6280. ' i: longint;',
  6281. 'begin',
  6282. ' Include(s,''S'');',
  6283. ' Include(s,c);',
  6284. ' Include(s,a);',
  6285. ' c:=low(TAtoZ);',
  6286. ' i:=ord(low(TAtoZ));',
  6287. ' a:=high(TAtoZ);',
  6288. ' a:=high(TSetOfAtoZ);',
  6289. ' s:=[a,c,''M''];',
  6290. '']);
  6291. ConvertProgram;
  6292. CheckSource('TestSet_AnonymousEnumTypeChar',
  6293. LinesToStr([ // statements
  6294. '']),
  6295. LinesToStr([
  6296. '']));
  6297. end;
  6298. procedure TTestModule.TestSet_ConstEnum;
  6299. begin
  6300. StartProgram(false);
  6301. Add([
  6302. 'type',
  6303. ' TEnum = (red,blue,green);',
  6304. ' TEnums = set of TEnum;',
  6305. 'const',
  6306. ' teAny = [low(TEnum)..high(TEnum)];',
  6307. ' teRedBlue = [low(TEnum)..pred(high(TEnum))];',
  6308. 'var',
  6309. ' e: TEnum;',
  6310. ' s: TEnums;',
  6311. 'begin',
  6312. ' if blue in teAny then;',
  6313. ' if blue in teAny+[e] then;',
  6314. ' if blue in teAny+teRedBlue then;',
  6315. ' if e in [red,blue] then;',
  6316. ' s:=teAny;',
  6317. ' s:=teAny+[e];',
  6318. ' s:=[e]+teAny;',
  6319. ' s:=teAny+teRedBlue;',
  6320. ' s:=teAny+teRedBlue+[e];',
  6321. '']);
  6322. ConvertProgram;
  6323. CheckSource('TestSet_ConstEnum',
  6324. LinesToStr([ // statements
  6325. 'this.TEnum = {',
  6326. ' "0": "red",',
  6327. ' red: 0,',
  6328. ' "1": "blue",',
  6329. ' blue: 1,',
  6330. ' "2": "green",',
  6331. ' green: 2',
  6332. '};',
  6333. 'this.teAny = rtl.createSet(null, this.TEnum.red, this.TEnum.green);',
  6334. 'this.teRedBlue = rtl.createSet(null, this.TEnum.red, this.TEnum.green - 1);',
  6335. 'this.e = 0;',
  6336. 'this.s = {};',
  6337. '']),
  6338. LinesToStr([
  6339. 'if ($mod.TEnum.blue in $mod.teAny) ;',
  6340. 'if ($mod.TEnum.blue in rtl.unionSet($mod.teAny, rtl.createSet($mod.e))) ;',
  6341. 'if ($mod.TEnum.blue in rtl.unionSet($mod.teAny, $mod.teRedBlue)) ;',
  6342. 'if ($mod.e in rtl.createSet($mod.TEnum.red, $mod.TEnum.blue)) ;',
  6343. '$mod.s = rtl.refSet($mod.teAny);',
  6344. '$mod.s = rtl.unionSet($mod.teAny, rtl.createSet($mod.e));',
  6345. '$mod.s = rtl.unionSet(rtl.createSet($mod.e), $mod.teAny);',
  6346. '$mod.s = rtl.unionSet($mod.teAny, $mod.teRedBlue);',
  6347. '$mod.s = rtl.unionSet(rtl.unionSet($mod.teAny, $mod.teRedBlue), rtl.createSet($mod.e));',
  6348. '']));
  6349. end;
  6350. procedure TTestModule.TestSet_ConstChar;
  6351. begin
  6352. StartProgram(false);
  6353. Add([
  6354. 'const',
  6355. ' LowChars = [''a''..''z''];',
  6356. ' Chars = LowChars+[''A''..''Z''];',
  6357. ' sc = [''А'', ''Я''];',
  6358. 'var',
  6359. ' c: char;',
  6360. ' s: string;',
  6361. 'begin',
  6362. ' if c in lowchars then ;',
  6363. ' if ''a'' in lowchars then ;',
  6364. ' if s[1] in lowchars then ;',
  6365. ' if c in chars then ;',
  6366. ' if c in [''a''..''z'',''_''] then ;',
  6367. ' if ''b'' in [''a''..''z'',''_''] then ;',
  6368. ' if ''Я'' in sc then ;',
  6369. ' if 3=ord('' '') then ;',
  6370. '']);
  6371. ConvertProgram;
  6372. CheckSource('TestSet_ConstChar',
  6373. LinesToStr([ // statements
  6374. 'this.LowChars = rtl.createSet(null, 97, 122);',
  6375. 'this.Chars = rtl.unionSet(this.LowChars, rtl.createSet(null, 65, 90));',
  6376. 'this.sc = rtl.createSet(1040, 1071);',
  6377. 'this.c = "";',
  6378. 'this.s = "";',
  6379. '']),
  6380. LinesToStr([
  6381. 'if ($mod.c.charCodeAt() in $mod.LowChars) ;',
  6382. 'if (97 in $mod.LowChars) ;',
  6383. 'if ($mod.s.charCodeAt(0) in $mod.LowChars) ;',
  6384. 'if ($mod.c.charCodeAt() in $mod.Chars) ;',
  6385. 'if ($mod.c.charCodeAt() in rtl.createSet(null, 97, 122, 95)) ;',
  6386. 'if (98 in rtl.createSet(null, 97, 122, 95)) ;',
  6387. 'if (1071 in $mod.sc) ;',
  6388. 'if (3 === 32) ;',
  6389. '']));
  6390. end;
  6391. procedure TTestModule.TestSet_ConstInt;
  6392. begin
  6393. StartProgram(false);
  6394. Add([
  6395. 'const',
  6396. ' Months = [1..12];',
  6397. ' Mirror = [-12..-1]+Months;',
  6398. 'var',
  6399. ' i: smallint;',
  6400. 'begin',
  6401. ' if 3 in Months then;',
  6402. ' if i in Months+[i] then;',
  6403. ' if i in Months+Mirror then;',
  6404. ' if i in [4..6,8] then;',
  6405. '']);
  6406. ConvertProgram;
  6407. CheckSource('TestSet_ConstInt',
  6408. LinesToStr([ // statements
  6409. 'this.Months = rtl.createSet(null, 1, 12);',
  6410. 'this.Mirror = rtl.unionSet(rtl.createSet(null, -12, -1), this.Months);',
  6411. 'this.i = 0;',
  6412. '']),
  6413. LinesToStr([
  6414. 'if (3 in $mod.Months) ;',
  6415. 'if ($mod.i in rtl.unionSet($mod.Months, rtl.createSet($mod.i))) ;',
  6416. 'if ($mod.i in rtl.unionSet($mod.Months, $mod.Mirror)) ;',
  6417. 'if ($mod.i in rtl.createSet(null, 4, 6, 8)) ;',
  6418. '']));
  6419. end;
  6420. procedure TTestModule.TestSet_InFunction;
  6421. begin
  6422. StartProgram(false);
  6423. Add([
  6424. 'const',
  6425. ' TEnum = 3;',
  6426. ' TSetOfEnum = 4;',
  6427. ' TSetOfAno = 5;',
  6428. 'procedure DoIt;',
  6429. 'type',
  6430. ' TEnum = (red, blue);',
  6431. ' TSetOfEnum = set of TEnum;',
  6432. ' TSetOfAno = set of (up,down);',
  6433. 'var',
  6434. ' e: TEnum;',
  6435. ' se: TSetOfEnum;',
  6436. ' sa: TSetOfAno;',
  6437. 'begin',
  6438. ' se:=[e];',
  6439. ' sa:=[up];',
  6440. 'end;',
  6441. 'begin',
  6442. '']);
  6443. ConvertProgram;
  6444. CheckSource('TestSet_InFunction',
  6445. LinesToStr([ // statements
  6446. 'this.TEnum = 3;',
  6447. 'this.TSetOfEnum = 4;',
  6448. 'this.TSetOfAno = 5;',
  6449. 'var TEnum$1 = {',
  6450. ' "0": "red",',
  6451. ' red: 0,',
  6452. ' "1": "blue",',
  6453. ' blue: 1',
  6454. '};',
  6455. 'var TSetOfAno$a = {',
  6456. ' "0": "up",',
  6457. ' up: 0,',
  6458. ' "1": "down",',
  6459. ' down: 1',
  6460. '};',
  6461. 'this.DoIt = function () {',
  6462. ' var e = 0;',
  6463. ' var se = {};',
  6464. ' var sa = {};',
  6465. ' se = rtl.createSet(e);',
  6466. ' sa = rtl.createSet(TSetOfAno$a.up);',
  6467. '};',
  6468. '']),
  6469. LinesToStr([
  6470. '']));
  6471. end;
  6472. procedure TTestModule.TestSet_ForIn;
  6473. begin
  6474. StartProgram(false);
  6475. Add([
  6476. 'type',
  6477. ' TEnum = (Red, Green, Blue);',
  6478. ' TEnumRg = green..blue;',
  6479. ' TSetOfEnum = set of TEnum;',
  6480. ' TSetOfEnumRg = set of TEnumRg;',
  6481. 'var',
  6482. ' e, e2: TEnum;',
  6483. ' er: TEnum;',
  6484. ' s: TSetOfEnum;',
  6485. 'begin',
  6486. ' for e in TSetOfEnum do ;',
  6487. ' for e in TSetOfEnumRg do ;',
  6488. ' for e in [] do e2:=e;',
  6489. ' for e in [red..green] do e2:=e;',
  6490. ' for e in [green,blue] do e2:=e;',
  6491. ' for e in [red,blue] do e2:=e;',
  6492. ' for e in s do e2:=e;',
  6493. ' for er in TSetOfEnumRg do ;',
  6494. '']);
  6495. ConvertProgram;
  6496. CheckSource('TestSet_ForIn',
  6497. LinesToStr([ // statements
  6498. 'this.TEnum = {',
  6499. ' "0":"Red",',
  6500. ' Red:0,',
  6501. ' "1":"Green",',
  6502. ' Green:1,',
  6503. ' "2":"Blue",',
  6504. ' Blue:2',
  6505. ' };',
  6506. 'this.e = 0;',
  6507. 'this.e2 = 0;',
  6508. 'this.er = 0;',
  6509. 'this.s = {};',
  6510. '']),
  6511. LinesToStr([
  6512. 'for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  6513. 'for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  6514. 'for ($mod.e = 0; $mod.e <= 1; $mod.e++) $mod.e2 = $mod.e;',
  6515. 'for ($mod.e = 1; $mod.e <= 2; $mod.e++) $mod.e2 = $mod.e;',
  6516. 'for ($mod.e in rtl.createSet($mod.TEnum.Red, $mod.TEnum.Blue)) $mod.e2 = $mod.e;',
  6517. 'for (var $l in $mod.s){',
  6518. ' $mod.e = +$l;',
  6519. ' $mod.e2 = $mod.e;',
  6520. '};',
  6521. 'for ($mod.er = 1; $mod.er <= 2; $mod.er++) ;',
  6522. '']));
  6523. end;
  6524. procedure TTestModule.TestNestBegin;
  6525. begin
  6526. StartProgram(false);
  6527. Add('begin');
  6528. Add(' begin');
  6529. Add(' begin');
  6530. Add(' end;');
  6531. Add(' begin');
  6532. Add(' if true then ;');
  6533. Add(' end;');
  6534. Add(' end;');
  6535. ConvertProgram;
  6536. CheckSource('TestNestBegin',
  6537. '',
  6538. 'if (true) ;');
  6539. end;
  6540. procedure TTestModule.TestUnitImplVars;
  6541. begin
  6542. StartUnit(false);
  6543. Add('interface');
  6544. Add('implementation');
  6545. Add('var');
  6546. Add(' V1:longint;');
  6547. Add(' V2:longint = 3;');
  6548. Add(' V3:string = ''abc'';');
  6549. ConvertUnit;
  6550. CheckSource('TestUnitImplVars',
  6551. LinesToStr([ // statements
  6552. 'var $impl = $mod.$impl;',
  6553. '']),
  6554. '', // this.$init
  6555. LinesToStr([ // implementation
  6556. '$impl.V1 = 0;',
  6557. '$impl.V2 = 3;',
  6558. '$impl.V3 = "abc";',
  6559. '']) );
  6560. end;
  6561. procedure TTestModule.TestUnitImplConsts;
  6562. begin
  6563. StartUnit(false);
  6564. Add('interface');
  6565. Add('implementation');
  6566. Add('const');
  6567. Add(' v1 = 3;');
  6568. Add(' v2:longint = 4;');
  6569. Add(' v3:string = ''abc'';');
  6570. ConvertUnit;
  6571. CheckSource('TestUnitImplConsts',
  6572. LinesToStr([ // statements
  6573. 'var $impl = $mod.$impl;',
  6574. '']),
  6575. '', // this.$init
  6576. LinesToStr([ // implementation
  6577. '$impl.v1 = 3;',
  6578. '$impl.v2 = 4;',
  6579. '$impl.v3 = "abc";',
  6580. '']) );
  6581. end;
  6582. procedure TTestModule.TestUnitImplRecord;
  6583. begin
  6584. StartUnit(false);
  6585. Add('interface');
  6586. Add('implementation');
  6587. Add('type');
  6588. Add(' TMyRecord = record');
  6589. Add(' i: longint;');
  6590. Add(' end;');
  6591. Add('var aRec: TMyRecord;');
  6592. Add('initialization');
  6593. Add(' arec.i:=3;');
  6594. ConvertUnit;
  6595. CheckSource('TestUnitImplRecord',
  6596. LinesToStr([ // statements
  6597. 'var $impl = $mod.$impl;',
  6598. '']),
  6599. // this.$init
  6600. '$impl.aRec.i = 3;',
  6601. LinesToStr([ // implementation
  6602. 'rtl.recNewT($impl, "TMyRecord", function () {',
  6603. ' this.i = 0;',
  6604. ' this.$eq = function (b) {',
  6605. ' return this.i === b.i;',
  6606. ' };',
  6607. ' this.$assign = function (s) {',
  6608. ' this.i = s.i;',
  6609. ' return this;',
  6610. ' };',
  6611. '});',
  6612. '$impl.aRec = $impl.TMyRecord.$new();',
  6613. '']) );
  6614. end;
  6615. procedure TTestModule.TestRenameJSNameConflict;
  6616. begin
  6617. StartProgram(false);
  6618. Add('var apply: longint;');
  6619. Add('var bind: longint;');
  6620. Add('var call: longint;');
  6621. Add('begin');
  6622. ConvertProgram;
  6623. CheckSource('TestRenameJSNameConflict',
  6624. LinesToStr([ // statements
  6625. 'this.Apply = 0;',
  6626. 'this.Bind = 0;',
  6627. 'this.Call = 0;'
  6628. ]),
  6629. LinesToStr([ // this.$main
  6630. ''
  6631. ]));
  6632. end;
  6633. procedure TTestModule.TestLocalConst;
  6634. begin
  6635. StartProgram(false);
  6636. Add('procedure DoIt;');
  6637. Add('const');
  6638. Add(' cA: longint = 1;');
  6639. Add(' cB = 2;');
  6640. Add(' procedure Sub;');
  6641. Add(' const');
  6642. Add(' csA = 3;');
  6643. Add(' cB: double = 4;');
  6644. Add(' begin');
  6645. Add(' cb:=cb+csa;');
  6646. Add(' ca:=ca+csa+5;');
  6647. Add(' end;');
  6648. Add('begin');
  6649. Add(' ca:=ca+cb+6;');
  6650. Add('end;');
  6651. Add('begin');
  6652. ConvertProgram;
  6653. CheckSource('TestLocalConst',
  6654. LinesToStr([
  6655. 'var cA = 1;',
  6656. 'var cB = 2;',
  6657. 'var csA = 3;',
  6658. 'var cB$1 = 4;',
  6659. 'this.DoIt = function () {',
  6660. ' function Sub() {',
  6661. ' cB$1 = cB$1 + 3;',
  6662. ' cA = cA + 3 + 5;',
  6663. ' };',
  6664. ' cA = cA + 2 + 6;',
  6665. '};'
  6666. ]),
  6667. LinesToStr([
  6668. ]));
  6669. end;
  6670. procedure TTestModule.TestVarExternal;
  6671. begin
  6672. StartProgram(false);
  6673. Add('var');
  6674. Add(' NaN: double; external name ''Global.NaN'';');
  6675. Add(' d: double;');
  6676. Add('begin');
  6677. Add(' d:=NaN;');
  6678. ConvertProgram;
  6679. CheckSource('TestVarExternal',
  6680. LinesToStr([
  6681. 'this.d = 0.0;'
  6682. ]),
  6683. LinesToStr([
  6684. '$mod.d = Global.NaN;'
  6685. ]));
  6686. end;
  6687. procedure TTestModule.TestVarExternalOtherUnit;
  6688. begin
  6689. AddModuleWithIntfImplSrc('unit2.pas',
  6690. LinesToStr([
  6691. 'var NaN: double; external name ''Global.NaN'';',
  6692. 'var iV: longint;'
  6693. ]),
  6694. '');
  6695. StartUnit(true);
  6696. Add('interface');
  6697. Add('uses unit2;');
  6698. Add('implementation');
  6699. Add('var');
  6700. Add(' d: double;');
  6701. Add(' i: longint; external name ''$i'';');
  6702. Add('begin');
  6703. Add(' d:=nan;');
  6704. Add(' d:=uNit2.nan;');
  6705. Add(' d:=test1.d;');
  6706. Add(' i:=iv;');
  6707. Add(' i:=uNit2.iv;');
  6708. Add(' i:=test1.i;');
  6709. ConvertUnit;
  6710. CheckSource('TestVarExternalOtherUnit',
  6711. LinesToStr([
  6712. 'var $impl = $mod.$impl;',
  6713. '']),
  6714. LinesToStr([ // this.$init
  6715. '$impl.d = Global.NaN;',
  6716. '$impl.d = Global.NaN;',
  6717. '$impl.d = $impl.d;',
  6718. '$i = pas.unit2.iV;',
  6719. '$i = pas.unit2.iV;',
  6720. '$i = $i;',
  6721. '']),
  6722. LinesToStr([ // implementation
  6723. '$impl.d = 0.0;',
  6724. '']) );
  6725. end;
  6726. procedure TTestModule.TestVarAbsoluteFail;
  6727. begin
  6728. StartProgram(false);
  6729. Add([
  6730. 'var',
  6731. ' a: longint;',
  6732. ' b: longword absolute a;',
  6733. 'begin']);
  6734. SetExpectedPasResolverError('Invalid variable modifier "absolute"',nInvalidVariableModifier);
  6735. ConvertProgram;
  6736. end;
  6737. procedure TTestModule.TestConstExternal;
  6738. begin
  6739. StartProgram(false);
  6740. Add([
  6741. 'const',
  6742. ' PI: double; external name ''Global.PI'';',
  6743. ' Tau = 2*pi;',
  6744. 'var d: double;',
  6745. 'begin',
  6746. ' d:=pi;',
  6747. ' d:=tau+pi;']);
  6748. ConvertProgram;
  6749. CheckSource('TestConstExternal',
  6750. LinesToStr([
  6751. 'this.Tau = 2*Global.PI;',
  6752. 'this.d = 0.0;'
  6753. ]),
  6754. LinesToStr([
  6755. '$mod.d = Global.PI;',
  6756. '$mod.d = $mod.Tau + Global.PI;'
  6757. ]));
  6758. end;
  6759. procedure TTestModule.TestDouble;
  6760. begin
  6761. StartProgram(false);
  6762. Add([
  6763. 'type',
  6764. ' TDateTime = double;',
  6765. 'const',
  6766. ' a = TDateTime(2.7);',
  6767. ' b = a + TDateTime(1.7);',
  6768. ' c = 0.9 + 0.1;',
  6769. ' f0_1 = 0.1;',
  6770. ' f0_3 = 0.3;',
  6771. ' fn0_1 = -0.1;',
  6772. ' fn0_3 = -0.3;',
  6773. ' fn0_003 = -0.003;',
  6774. ' fn0_123456789 = -0.123456789;',
  6775. ' fn300_0 = -300.0;',
  6776. ' fn123456_0 = -123456.0;',
  6777. ' fn1234567_8 = -1234567.8;',
  6778. ' fn12345678_9 = -12345678.9;',
  6779. ' f1_0En12 = 1E-12;',
  6780. ' fn1_0En12 = -1E-12;',
  6781. ' maxdouble = 1.7e+308;',
  6782. ' mindouble = -1.7e+308;',
  6783. ' MinSafeIntDouble = -$1fffffffffffff;',
  6784. ' MinSafeIntDouble2 = -$20000000000000-1;',
  6785. ' MaxSafeIntDouble = $1fffffffffffff;',
  6786. ' DZeroResolution = 1E-12;',
  6787. ' Minus1 = -1E-12;',
  6788. ' EPS = 1E-9;',
  6789. ' DELTA = 0.001;',
  6790. ' Big = 129.789E+100;',
  6791. ' Test0_15 = 0.15;',
  6792. ' Test999 = 2.9999999999999;',
  6793. ' Test111999 = 211199999999999000.0;',
  6794. ' TestMinus111999 = -211199999999999000.0;',
  6795. 'var',
  6796. ' d: double = b;',
  6797. 'begin',
  6798. ' d:=1.0;',
  6799. ' d:=1.0/3.0;',
  6800. ' d:=1/3;',
  6801. ' d:=5.0E-324;',
  6802. ' d:=1.7E308;',
  6803. ' d:=001.00E00;',
  6804. ' d:=002.00E001;',
  6805. ' d:=003.000E000;',
  6806. ' d:=-004.00E-00;',
  6807. ' d:=-005.00E-001;',
  6808. ' d:=10**3;',
  6809. ' d:=10 mod 3;',
  6810. ' d:=10 div 3;',
  6811. ' d:=c;',
  6812. ' d:=f0_1;',
  6813. ' d:=f0_3;',
  6814. ' d:=fn0_1;',
  6815. ' d:=fn0_3;',
  6816. ' d:=fn0_003;',
  6817. ' d:=fn0_123456789;',
  6818. ' d:=fn300_0;',
  6819. ' d:=fn123456_0;',
  6820. ' d:=fn1234567_8;',
  6821. ' d:=fn12345678_9;',
  6822. ' d:=f1_0En12;',
  6823. ' d:=fn1_0En12;',
  6824. ' d:=maxdouble;',
  6825. ' d:=mindouble;',
  6826. ' d:=MinSafeIntDouble;',
  6827. ' d:=double(MinSafeIntDouble);',
  6828. ' d:=MinSafeIntDouble2;',
  6829. ' d:=double(MinSafeIntDouble2);',
  6830. ' d:=MaxSafeIntDouble;',
  6831. ' d:=default(double);',
  6832. '']);
  6833. ConvertProgram;
  6834. CheckSource('TestDouble',
  6835. LinesToStr([
  6836. 'this.a = 2.7;',
  6837. 'this.b = 2.7 + 1.7;',
  6838. 'this.c = 0.9 + 0.1;',
  6839. 'this.f0_1 = 0.1;',
  6840. 'this.f0_3 = 0.3;',
  6841. 'this.fn0_1 = -0.1;',
  6842. 'this.fn0_3 = -0.3;',
  6843. 'this.fn0_003 = -0.003;',
  6844. 'this.fn0_123456789 = -0.123456789;',
  6845. 'this.fn300_0 = -300.0;',
  6846. 'this.fn123456_0 = -123456.0;',
  6847. 'this.fn1234567_8 = -1234567.8;',
  6848. 'this.fn12345678_9 = -12345678.9;',
  6849. 'this.f1_0En12 = 1E-12;',
  6850. 'this.fn1_0En12 = -1E-12;',
  6851. 'this.maxdouble = 1.7e+308;',
  6852. 'this.mindouble = -1.7e+308;',
  6853. 'this.MinSafeIntDouble = -0x1fffffffffffff;',
  6854. 'this.MinSafeIntDouble2 = -0x20000000000000 - 1;',
  6855. 'this.MaxSafeIntDouble = 0x1fffffffffffff;',
  6856. 'this.DZeroResolution = 1E-12;',
  6857. 'this.Minus1 = -1E-12;',
  6858. 'this.EPS = 1E-9;',
  6859. 'this.DELTA = 0.001;',
  6860. 'this.Big = 129.789E+100;',
  6861. 'this.Test0_15 = 0.15;',
  6862. 'this.Test999 = 2.9999999999999;',
  6863. 'this.Test111999 = 211199999999999000.0;',
  6864. 'this.TestMinus111999 = -211199999999999000.0;',
  6865. 'this.d = 4.4;'
  6866. ]),
  6867. LinesToStr([
  6868. '$mod.d = 1.0;',
  6869. '$mod.d = 1.0 / 3.0;',
  6870. '$mod.d = 1 / 3;',
  6871. '$mod.d = 5.0E-324;',
  6872. '$mod.d = 1.7E308;',
  6873. '$mod.d = 1.00E0;',
  6874. '$mod.d = 2.00E1;',
  6875. '$mod.d = 3.000E0;',
  6876. '$mod.d = -4.00E-0;',
  6877. '$mod.d = -5.00E-1;',
  6878. '$mod.d = Math.pow(10, 3);',
  6879. '$mod.d = 10 % 3;',
  6880. '$mod.d = rtl.trunc(10 / 3);',
  6881. '$mod.d = 1;',
  6882. '$mod.d = 0.1;',
  6883. '$mod.d = 0.3;',
  6884. '$mod.d = -0.1;',
  6885. '$mod.d = -0.3;',
  6886. '$mod.d = -0.003;',
  6887. '$mod.d = -0.123456789;',
  6888. '$mod.d = -300;',
  6889. '$mod.d = -123456;',
  6890. '$mod.d = -1234567.8;',
  6891. '$mod.d = -1.23456789E7;',
  6892. '$mod.d = 1E-12;',
  6893. '$mod.d = -1E-12;',
  6894. '$mod.d = 1.7E308;',
  6895. '$mod.d = -1.7E308;',
  6896. '$mod.d = -9007199254740991;',
  6897. '$mod.d = -9007199254740991;',
  6898. '$mod.d = -9.007199254740992E15;',
  6899. '$mod.d = -9.007199254740992E15;',
  6900. '$mod.d = 9007199254740991;',
  6901. '$mod.d = 0.0;',
  6902. '']));
  6903. end;
  6904. procedure TTestModule.TestInteger;
  6905. begin
  6906. StartProgram(false);
  6907. Add([
  6908. 'const',
  6909. ' MinInt = low(NativeInt);',
  6910. ' MaxInt = high(NativeInt);',
  6911. 'type',
  6912. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  6913. 'const',
  6914. ' a = low(TMyInt)+High(TMyInt);',
  6915. 'var',
  6916. ' i: TMyInt;',
  6917. 'begin',
  6918. ' i:=-MinInt;',
  6919. ' i:=default(TMyInt);',
  6920. ' i:=low(i)+high(i);',
  6921. '']);
  6922. ConvertProgram;
  6923. CheckSource('TestIntegerRange',
  6924. LinesToStr([
  6925. 'this.MinInt = -9007199254740991;',
  6926. 'this.MaxInt = 9007199254740991;',
  6927. 'this.a = -9007199254740991 + 9007199254740991;',
  6928. 'this.i = 0;',
  6929. '']),
  6930. LinesToStr([
  6931. '$mod.i = - -9007199254740991;',
  6932. '$mod.i = -9007199254740991;',
  6933. '$mod.i = -9007199254740991 + 9007199254740991;',
  6934. '']));
  6935. end;
  6936. procedure TTestModule.TestIntegerRange;
  6937. begin
  6938. StartProgram(false);
  6939. Add([
  6940. 'const',
  6941. ' MinInt = -1;',
  6942. ' MaxInt = +1;',
  6943. 'type',
  6944. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  6945. ' TInt2 = 1..3;',
  6946. 'const',
  6947. ' a = low(TMyInt)+High(TMyInt);',
  6948. ' b = low(TInt2)+High(TInt2);',
  6949. ' s1 = [1];',
  6950. ' s2 = [1,2];',
  6951. ' s3 = [1..3];',
  6952. ' s4 = [low(shortint)..high(shortint)];',
  6953. ' s5 = [succ(low(shortint))..pred(high(shortint))];',
  6954. ' s6 = 1 in s2;',
  6955. 'var',
  6956. ' i: TMyInt;',
  6957. ' i2: TInt2;',
  6958. 'begin',
  6959. ' i:=i2;',
  6960. ' i:=default(TMyInt);',
  6961. ' if i=i2 then ;',
  6962. ' i:=ord(i2);',
  6963. '']);
  6964. ConvertProgram;
  6965. CheckSource('TestIntegerRange',
  6966. LinesToStr([
  6967. 'this.MinInt = -1;',
  6968. 'this.MaxInt = +1;',
  6969. 'this.a = -1 + 1;',
  6970. 'this.b = 1 + 3;',
  6971. 'this.s1 = rtl.createSet(1);',
  6972. 'this.s2 = rtl.createSet(1, 2);',
  6973. 'this.s3 = rtl.createSet(null, 1, 3);',
  6974. 'this.s4 = rtl.createSet(null, -128, 127);',
  6975. 'this.s5 = rtl.createSet(null, -128 + 1, 127 - 1);',
  6976. 'this.s6 = 1 in this.s2;',
  6977. 'this.i = 0;',
  6978. 'this.i2 = 0;',
  6979. '']),
  6980. LinesToStr([
  6981. '$mod.i = $mod.i2;',
  6982. '$mod.i = -1;',
  6983. 'if ($mod.i === $mod.i2) ;',
  6984. '$mod.i = $mod.i2;',
  6985. '']));
  6986. end;
  6987. procedure TTestModule.TestIntegerTypecasts;
  6988. begin
  6989. StartProgram(false);
  6990. Add([
  6991. 'var',
  6992. ' i: nativeint;',
  6993. ' b: byte;',
  6994. ' sh: shortint;',
  6995. ' w: word;',
  6996. ' sm: smallint;',
  6997. ' lw: longword;',
  6998. ' li: longint;',
  6999. 'begin',
  7000. ' b:=byte(i);',
  7001. ' sh:=shortint(i);',
  7002. ' w:=word(i);',
  7003. ' sm:=smallint(i);',
  7004. ' lw:=longword(i);',
  7005. ' li:=longint(i);',
  7006. '']);
  7007. ConvertProgram;
  7008. CheckSource('TestIntegerTypecasts',
  7009. LinesToStr([
  7010. 'this.i = 0;',
  7011. 'this.b = 0;',
  7012. 'this.sh = 0;',
  7013. 'this.w = 0;',
  7014. 'this.sm = 0;',
  7015. 'this.lw = 0;',
  7016. 'this.li = 0;',
  7017. '']),
  7018. LinesToStr([
  7019. '$mod.b = $mod.i & 255;',
  7020. '$mod.sh = (($mod.i & 255) << 24) >> 24;',
  7021. '$mod.w = $mod.i & 65535;',
  7022. '$mod.sm = (($mod.i & 65535) << 16) >> 16;',
  7023. '$mod.lw = $mod.i >>> 0;',
  7024. '$mod.li = $mod.i & 0xFFFFFFFF;',
  7025. '']));
  7026. end;
  7027. procedure TTestModule.TestInteger_BitwiseShrNativeInt;
  7028. begin
  7029. StartProgram(false);
  7030. Add([
  7031. 'var',
  7032. ' i,j: nativeint;',
  7033. 'begin',
  7034. ' i:=i shr 0;',
  7035. ' i:=i shr 1;',
  7036. ' i:=i shr 3;',
  7037. ' i:=i shr 54;',
  7038. ' i:=j shr i;',
  7039. '']);
  7040. ConvertProgram;
  7041. CheckResolverUnexpectedHints;
  7042. CheckSource('TestInteger_BitwiseShrNativeInt',
  7043. LinesToStr([
  7044. 'this.i = 0;',
  7045. 'this.j = 0;',
  7046. '']),
  7047. LinesToStr([
  7048. '$mod.i = $mod.i;',
  7049. '$mod.i = Math.floor($mod.i / 2);',
  7050. '$mod.i = Math.floor($mod.i / 8);',
  7051. '$mod.i = 0;',
  7052. '$mod.i = rtl.shr($mod.j, $mod.i);',
  7053. '']));
  7054. end;
  7055. procedure TTestModule.TestInteger_BitwiseShlNativeInt;
  7056. begin
  7057. StartProgram(false);
  7058. Add([
  7059. 'var',
  7060. ' i: nativeint;',
  7061. 'begin',
  7062. ' i:=i shl 0;',
  7063. ' i:=i shl 54;',
  7064. ' i:=123456789012 shl 1;',
  7065. ' i:=i shl 1;',
  7066. '']);
  7067. ConvertProgram;
  7068. CheckResolverUnexpectedHints;
  7069. CheckSource('TestInteger_BitwiseShrNativeInt',
  7070. LinesToStr([
  7071. 'this.i = 0;',
  7072. '']),
  7073. LinesToStr([
  7074. '$mod.i = $mod.i;',
  7075. '$mod.i = 0;',
  7076. '$mod.i = 246913578024;',
  7077. '$mod.i = rtl.shl($mod.i, 1);',
  7078. '']));
  7079. end;
  7080. procedure TTestModule.TestInteger_SystemFunc;
  7081. begin
  7082. StartProgram(true);
  7083. Add([
  7084. 'var',
  7085. ' i: byte;',
  7086. ' s: string;',
  7087. 'begin',
  7088. ' system.inc(i);',
  7089. ' system.str(i,s);',
  7090. ' s:=system.str(i);',
  7091. ' i:=system.low(i);',
  7092. ' i:=system.high(i);',
  7093. ' i:=system.pred(i);',
  7094. ' i:=system.succ(i);',
  7095. ' i:=system.ord(i);',
  7096. '']);
  7097. ConvertProgram;
  7098. CheckResolverUnexpectedHints;
  7099. CheckSource('TestInteger_SystemFunc',
  7100. LinesToStr([
  7101. 'this.i = 0;',
  7102. 'this.s = "";',
  7103. '']),
  7104. LinesToStr([
  7105. '$mod.i += 1;',
  7106. '$mod.s = "" + $mod.i;',
  7107. '$mod.s = "" + $mod.i;',
  7108. '$mod.i = 0;',
  7109. '$mod.i = 255;',
  7110. '$mod.i = $mod.i - 1;',
  7111. '$mod.i = $mod.i + 1;',
  7112. '$mod.i = $mod.i;',
  7113. '']));
  7114. end;
  7115. procedure TTestModule.TestCurrency;
  7116. begin
  7117. StartProgram(false);
  7118. Add([
  7119. 'type',
  7120. ' TCoin = currency;',
  7121. 'const',
  7122. ' a = TCoin(2.7);',
  7123. ' b = a + TCoin(1.7);',
  7124. ' MinSafeIntCurrency: TCoin = -92233720368.5477;',
  7125. ' MaxSafeIntCurrency: TCoin = 92233720368.5477;',
  7126. 'var',
  7127. ' c: TCoin = b;',
  7128. ' i: nativeint;',
  7129. ' d: double;',
  7130. ' j: jsvalue;',
  7131. 'function DoIt(c: currency): currency; begin end;',
  7132. 'function GetIt(d: double): double; begin end;',
  7133. 'procedure Write(v: jsvalue); begin end;',
  7134. 'begin',
  7135. ' c:=1.0;',
  7136. ' c:=0.1;',
  7137. ' c:=1.0/3.0;',
  7138. ' c:=1/3;',
  7139. ' c:=a;',
  7140. ' d:=c;',
  7141. ' c:=d;',
  7142. ' c:=currency(c);',
  7143. ' c:=currency(d);',
  7144. ' d:=double(c);',
  7145. ' c:=i;',
  7146. ' c:=currency(i);',
  7147. //' i:=c;', not allowed
  7148. ' i:=nativeint(c);',
  7149. ' c:=c+a;',
  7150. ' c:=-c-a;',
  7151. ' c:=d+c;',
  7152. ' c:=c+d;',
  7153. ' c:=d-c;',
  7154. ' c:=c-d;',
  7155. ' c:=c*a;',
  7156. ' c:=a*c;',
  7157. ' c:=d*c;',
  7158. ' c:=c*d;',
  7159. ' c:=c/a;',
  7160. ' c:=a/c;',
  7161. ' c:=d/c;',
  7162. ' c:=c/d;',
  7163. ' c:=c**a;',
  7164. ' c:=a**c;',
  7165. ' c:=d**c;',
  7166. ' c:=c**d;',
  7167. ' if c=c then ;',
  7168. ' if c=a then ;',
  7169. ' if a=c then ;',
  7170. ' if d=c then ;',
  7171. ' if c=d then ;',
  7172. ' c:=DoIt(c);',
  7173. ' c:=DoIt(i);',
  7174. ' c:=DoIt(d);',
  7175. ' c:=GetIt(c);',
  7176. ' j:=c;',
  7177. ' Write(c);',
  7178. ' c:=default(currency);',
  7179. ' j:=str(c);',
  7180. ' j:=str(c:0:3);',
  7181. '']);
  7182. ConvertProgram;
  7183. CheckSource('TestCurrency',
  7184. LinesToStr([
  7185. 'this.a = 27000;',
  7186. 'this.b = this.a + 17000;',
  7187. 'this.MinSafeIntCurrency = -92233720368.5477;',
  7188. 'this.MaxSafeIntCurrency = 92233720368.5477;',
  7189. 'this.c = this.b;',
  7190. 'this.i = 0;',
  7191. 'this.d = 0.0;',
  7192. 'this.j = undefined;',
  7193. 'this.DoIt = function (c) {',
  7194. ' var Result = 0;',
  7195. ' return Result;',
  7196. '};',
  7197. 'this.GetIt = function (d) {',
  7198. ' var Result = 0.0;',
  7199. ' return Result;',
  7200. '};',
  7201. 'this.Write = function (v) {',
  7202. '};',
  7203. '']),
  7204. LinesToStr([
  7205. '$mod.c = 10000;',
  7206. '$mod.c = 1000;',
  7207. '$mod.c = rtl.trunc((1.0 / 3.0) * 10000);',
  7208. '$mod.c = rtl.trunc((1 / 3) * 10000);',
  7209. '$mod.c = $mod.a;',
  7210. '$mod.d = $mod.c / 10000;',
  7211. '$mod.c = rtl.trunc($mod.d * 10000);',
  7212. '$mod.c = $mod.c;',
  7213. '$mod.c = $mod.d * 10000;',
  7214. '$mod.d = $mod.c / 10000;',
  7215. '$mod.c = $mod.i * 10000;',
  7216. '$mod.c = $mod.i * 10000;',
  7217. '$mod.i = rtl.trunc($mod.c / 10000);',
  7218. '$mod.c = $mod.c + $mod.a;',
  7219. '$mod.c = -$mod.c - $mod.a;',
  7220. '$mod.c = ($mod.d * 10000) + $mod.c;',
  7221. '$mod.c = $mod.c + ($mod.d * 10000);',
  7222. '$mod.c = ($mod.d * 10000) - $mod.c;',
  7223. '$mod.c = $mod.c - ($mod.d * 10000);',
  7224. '$mod.c = ($mod.c * $mod.a) / 10000;',
  7225. '$mod.c = ($mod.a * $mod.c) / 10000;',
  7226. '$mod.c = $mod.d * $mod.c;',
  7227. '$mod.c = $mod.c * $mod.d;',
  7228. '$mod.c = rtl.trunc(($mod.c / $mod.a) * 10000);',
  7229. '$mod.c = rtl.trunc(($mod.a / $mod.c) * 10000);',
  7230. '$mod.c = rtl.trunc($mod.d / $mod.c);',
  7231. '$mod.c = rtl.trunc($mod.c / $mod.d);',
  7232. '$mod.c = rtl.trunc(Math.pow($mod.c / 10000, $mod.a / 10000) * 10000);',
  7233. '$mod.c = rtl.trunc(Math.pow($mod.a / 10000, $mod.c / 10000) * 10000);',
  7234. '$mod.c = rtl.trunc(Math.pow($mod.d, $mod.c / 10000) * 10000);',
  7235. '$mod.c = rtl.trunc(Math.pow($mod.c / 10000, $mod.d) * 10000);',
  7236. 'if ($mod.c === $mod.c) ;',
  7237. 'if ($mod.c === $mod.a) ;',
  7238. 'if ($mod.a === $mod.c) ;',
  7239. 'if (($mod.d * 10000) === $mod.c) ;',
  7240. 'if ($mod.c === ($mod.d * 10000)) ;',
  7241. '$mod.c = $mod.DoIt($mod.c);',
  7242. '$mod.c = $mod.DoIt($mod.i * 10000);',
  7243. '$mod.c = $mod.DoIt($mod.d * 10000);',
  7244. '$mod.c = rtl.trunc($mod.GetIt($mod.c / 10000) * 10000);',
  7245. '$mod.j = $mod.c / 10000;',
  7246. '$mod.Write($mod.c / 10000);',
  7247. '$mod.c = 0;',
  7248. '$mod.j = rtl.floatToStr($mod.c / 10000);',
  7249. '$mod.j = rtl.floatToStr($mod.c / 10000, 0, 3);',
  7250. '']));
  7251. end;
  7252. procedure TTestModule.TestForBoolDo;
  7253. begin
  7254. StartProgram(false);
  7255. Add([
  7256. 'var b: boolean;',
  7257. 'begin',
  7258. ' for b:=false to true do ;',
  7259. ' for b:=b downto false do ;',
  7260. ' for b in boolean do ;',
  7261. '']);
  7262. ConvertProgram;
  7263. CheckSource('TestForBoolDo',
  7264. LinesToStr([ // statements
  7265. 'this.b = false;']),
  7266. LinesToStr([ // this.$main
  7267. 'for (var $l = 0; $l <= 1; $l++) $mod.b = $l !== 0;',
  7268. 'for (var $l1 = +$mod.b; $l1 >= 0; $l1--) $mod.b = $l1 !== 0;',
  7269. 'for (var $l2 = 0; $l2 <= 1; $l2++) $mod.b = $l2 !== 0;',
  7270. '']));
  7271. end;
  7272. procedure TTestModule.TestForIntDo;
  7273. begin
  7274. StartProgram(false);
  7275. Add([
  7276. 'var i: longint;',
  7277. 'begin',
  7278. ' for i:=3 to 5 do ;',
  7279. ' for i:=i downto 2 do ;',
  7280. ' for i in byte do ;',
  7281. '']);
  7282. ConvertProgram;
  7283. CheckSource('TestForIntDo',
  7284. LinesToStr([ // statements
  7285. 'this.i = 0;']),
  7286. LinesToStr([ // this.$main
  7287. 'for ($mod.i = 3; $mod.i <= 5; $mod.i++) ;',
  7288. 'for (var $l = $mod.i; $l >= 2; $l--) $mod.i = $l;',
  7289. 'for (var $l1 = 0; $l1 <= 255; $l1++) $mod.i = $l1;',
  7290. '']));
  7291. end;
  7292. procedure TTestModule.TestForIntInDo;
  7293. begin
  7294. StartProgram(false);
  7295. Add([
  7296. 'type',
  7297. ' TSetOfInt = set of byte;',
  7298. ' TIntRg = 3..7;',
  7299. ' TSetOfIntRg = set of TIntRg;',
  7300. 'var',
  7301. ' i,i2: longint;',
  7302. ' a1: array of byte;',
  7303. ' a2: array[1..3] of byte;',
  7304. ' soi: TSetOfInt;',
  7305. ' soir: TSetOfIntRg;',
  7306. ' ir: TIntRg;',
  7307. 'begin',
  7308. ' for i in byte do ;',
  7309. ' for i in a1 do ;',
  7310. ' for i in a2 do ;',
  7311. ' for i in [11..13] do ;',
  7312. ' for i in TSetOfInt do ;',
  7313. ' for i in TIntRg do ;',
  7314. ' for i in soi do i2:=i;',
  7315. ' for i in TSetOfIntRg do ;',
  7316. ' for i in soir do ;',
  7317. ' for ir in TIntRg do ;',
  7318. ' for ir in TSetOfIntRg do ;',
  7319. ' for ir in soir do ;',
  7320. '']);
  7321. ConvertProgram;
  7322. CheckSource('TestForIntInDo',
  7323. LinesToStr([ // statements
  7324. 'this.i = 0;',
  7325. 'this.i2 = 0;',
  7326. 'this.a1 = [];',
  7327. 'this.a2 = rtl.arraySetLength(null, 0, 3);',
  7328. 'this.soi = {};',
  7329. 'this.soir = {};',
  7330. 'this.ir = 0;',
  7331. '']),
  7332. LinesToStr([ // this.$main
  7333. 'for (var $l = 0; $l <= 255; $l++) $mod.i = $l;',
  7334. 'for (var $in = $mod.a1, $l1 = 0, $end = rtl.length($in) - 1; $l1 <= $end; $l1++) $mod.i = $in[$l1];',
  7335. 'for (var $in1 = $mod.a2, $l2 = 0, $end1 = rtl.length($in1) - 1; $l2 <= $end1; $l2++) $mod.i = $in1[$l2];',
  7336. 'for (var $l3 = 11; $l3 <= 13; $l3++) $mod.i = $l3;',
  7337. 'for (var $l4 = 0; $l4 <= 255; $l4++) $mod.i = $l4;',
  7338. 'for (var $l5 = 3; $l5 <= 7; $l5++) $mod.i = $l5;',
  7339. 'for (var $l6 in $mod.soi) {',
  7340. ' $mod.i = +$l6;',
  7341. ' $mod.i2 = $mod.i;',
  7342. '};',
  7343. 'for (var $l7 = 3; $l7 <= 7; $l7++) $mod.i = $l7;',
  7344. 'for (var $l8 in $mod.soir) $mod.i = +$l8;',
  7345. 'for (var $l9 = 3; $l9 <= 7; $l9++) $mod.ir = $l9;',
  7346. 'for (var $l10 = 3; $l10 <= 7; $l10++) $mod.ir = $l10;',
  7347. 'for (var $l11 in $mod.soir) $mod.ir = +$l11;',
  7348. '']));
  7349. end;
  7350. procedure TTestModule.TestCharConst;
  7351. begin
  7352. StartProgram(false);
  7353. Add([
  7354. 'const',
  7355. ' a = #$00F3;',
  7356. ' c: char = ''1'';',
  7357. ' wc: widechar = ''ä'';',
  7358. 'begin',
  7359. ' c:=#0;',
  7360. ' c:=#1;',
  7361. ' c:=#9;',
  7362. ' c:=#10;',
  7363. ' c:=#13;',
  7364. ' c:=#31;',
  7365. ' c:=#32;',
  7366. ' c:=#$A;',
  7367. ' c:=#$0A;',
  7368. ' c:=#$b;',
  7369. ' c:=#$0b;',
  7370. ' c:=^A;',
  7371. ' c:=''"'';',
  7372. ' c:=default(char);',
  7373. ' c:=#$00E4;', // ä
  7374. ' c:=''ä'';',
  7375. ' c:=#$E4;', // ä
  7376. ' c:=#$D800;', // invalid UTF-16
  7377. ' c:=#$DFFF;', // invalid UTF-16
  7378. ' c:=#$FFFF;', // last UCS-2
  7379. ' c:=high(c);', // last UCS-2
  7380. ' c:=#269;',
  7381. '']);
  7382. ConvertProgram;
  7383. CheckSource('TestCharConst',
  7384. LinesToStr([
  7385. 'this.a="ó";',
  7386. 'this.c="1";',
  7387. 'this.wc="ä";'
  7388. ]),
  7389. LinesToStr([
  7390. '$mod.c="\x00";',
  7391. '$mod.c="\x01";',
  7392. '$mod.c="\t";',
  7393. '$mod.c="\n";',
  7394. '$mod.c="\r";',
  7395. '$mod.c="\x1F";',
  7396. '$mod.c=" ";',
  7397. '$mod.c="\n";',
  7398. '$mod.c="\n";',
  7399. '$mod.c="\x0B";',
  7400. '$mod.c="\x0B";',
  7401. '$mod.c="\x01";',
  7402. '$mod.c=''"'';',
  7403. '$mod.c="\x00";',
  7404. '$mod.c = "ä";',
  7405. '$mod.c = "ä";',
  7406. '$mod.c = "ä";',
  7407. '$mod.c="\uD800";',
  7408. '$mod.c="\uDFFF";',
  7409. '$mod.c="\uFFFF";',
  7410. '$mod.c="\uFFFF";',
  7411. '$mod.c = "č";',
  7412. '']));
  7413. end;
  7414. procedure TTestModule.TestChar_Compare;
  7415. begin
  7416. StartProgram(false);
  7417. Add('var');
  7418. Add(' c: char;');
  7419. Add(' b: boolean;');
  7420. Add('begin');
  7421. Add(' b:=c=''1'';');
  7422. Add(' b:=''2''=c;');
  7423. Add(' b:=''3''=''4'';');
  7424. Add(' b:=c<>''5'';');
  7425. Add(' b:=''6''<>c;');
  7426. Add(' b:=c>''7'';');
  7427. Add(' b:=''8''>c;');
  7428. Add(' b:=c>=''9'';');
  7429. Add(' b:=''A''>=c;');
  7430. Add(' b:=c<''B'';');
  7431. Add(' b:=''C''<c;');
  7432. Add(' b:=c<=''D'';');
  7433. Add(' b:=''E''<=c;');
  7434. ConvertProgram;
  7435. CheckSource('TestChar_Compare',
  7436. LinesToStr([
  7437. 'this.c="";',
  7438. 'this.b = false;'
  7439. ]),
  7440. LinesToStr([
  7441. '$mod.b = $mod.c === "1";',
  7442. '$mod.b = "2" === $mod.c;',
  7443. '$mod.b = "3" === "4";',
  7444. '$mod.b = $mod.c !== "5";',
  7445. '$mod.b = "6" !== $mod.c;',
  7446. '$mod.b = $mod.c > "7";',
  7447. '$mod.b = "8" > $mod.c;',
  7448. '$mod.b = $mod.c >= "9";',
  7449. '$mod.b = "A" >= $mod.c;',
  7450. '$mod.b = $mod.c < "B";',
  7451. '$mod.b = "C" < $mod.c;',
  7452. '$mod.b = $mod.c <= "D";',
  7453. '$mod.b = "E" <= $mod.c;',
  7454. '']));
  7455. end;
  7456. procedure TTestModule.TestChar_BuiltInProcs;
  7457. begin
  7458. StartProgram(false);
  7459. Add([
  7460. 'var',
  7461. ' c: char;',
  7462. ' i: longint;',
  7463. ' s: string;',
  7464. 'begin',
  7465. ' i:=ord(c);',
  7466. ' i:=ord(s[i]);',
  7467. ' c:=chr(i);',
  7468. ' c:=pred(c);',
  7469. ' c:=succ(c);',
  7470. ' c:=low(c);',
  7471. ' c:=high(c);',
  7472. ' i:=byte(c);',
  7473. ' i:=word(c);',
  7474. ' i:=longint(c);',
  7475. '']);
  7476. ConvertProgram;
  7477. CheckSource('TestChar_BuiltInProcs',
  7478. LinesToStr([
  7479. 'this.c = "";',
  7480. 'this.i = 0;',
  7481. 'this.s = "";'
  7482. ]),
  7483. LinesToStr([
  7484. '$mod.i = $mod.c.charCodeAt();',
  7485. '$mod.i = $mod.s.charCodeAt($mod.i-1);',
  7486. '$mod.c = String.fromCharCode($mod.i);',
  7487. '$mod.c = String.fromCharCode($mod.c.charCodeAt() - 1);',
  7488. '$mod.c = String.fromCharCode($mod.c.charCodeAt() + 1);',
  7489. '$mod.c = "\x00";',
  7490. '$mod.c = "\uFFFF";',
  7491. '$mod.i = $mod.c.charCodeAt() & 255;',
  7492. '$mod.i = $mod.c.charCodeAt();',
  7493. '$mod.i = $mod.c.charCodeAt() & 0xFFFFFFFF;',
  7494. '']));
  7495. end;
  7496. procedure TTestModule.TestStringConst;
  7497. begin
  7498. StartProgram(false);
  7499. Add([
  7500. '{$H+}',
  7501. 'const',
  7502. ' a = #$00F3#$017C;', // first <256, then >=256
  7503. ' b = string(''a'');',
  7504. ' c = string(''ä'');',
  7505. ' d = UnicodeString(''b'');',
  7506. ' e = UnicodeString(''ö'');',
  7507. 'var',
  7508. ' s: string = ''abc'';',
  7509. 'begin',
  7510. ' s:='''';',
  7511. ' s:=#13#10;',
  7512. ' s:=#9''foo'';',
  7513. ' s:=#$A9;',
  7514. ' s:=''foo''#13''bar'';',
  7515. ' s:=''"'';',
  7516. ' s:=''"''''"'';',
  7517. ' s:=#$20AC;', // euro
  7518. ' s:=#$10437;', // outside BMP
  7519. ' s:=''abc''#$20AC;', // ascii,#
  7520. ' s:=''ä''#$20AC;', // non ascii,#
  7521. ' s:=#$20AC''abc'';', // #, ascii
  7522. ' s:=#$20AC''ä'';', // #, non ascii
  7523. ' s:=default(string);',
  7524. ' s:=concat(s);',
  7525. ' s:=concat(s,''a'',s);',
  7526. ' s:=#250#269;',
  7527. //' s:=#$2F804;',
  7528. // ToDo: \uD87E\uDC04 -> \u{2F804}
  7529. '']);
  7530. ConvertProgram;
  7531. CheckSource('TestStringConst',
  7532. LinesToStr([
  7533. 'this.a = "óż";',
  7534. 'this.b = "a";',
  7535. 'this.c = "ä";',
  7536. 'this.d = "b";',
  7537. 'this.e = "ö";',
  7538. 'this.s="abc";',
  7539. '']),
  7540. LinesToStr([
  7541. '$mod.s="";',
  7542. '$mod.s="\r\n";',
  7543. '$mod.s="\tfoo";',
  7544. '$mod.s="©";',
  7545. '$mod.s="foo\rbar";',
  7546. '$mod.s=''"'';',
  7547. '$mod.s=''"\''"'';',
  7548. '$mod.s="€";',
  7549. '$mod.s="'#$F0#$90#$90#$B7'";',
  7550. '$mod.s = "abc€";',
  7551. '$mod.s = "ä€";',
  7552. '$mod.s = "€abc";',
  7553. '$mod.s = "ۊ";',
  7554. '$mod.s="";',
  7555. '$mod.s = $mod.s;',
  7556. '$mod.s = $mod.s.concat("a", $mod.s);',
  7557. '$mod.s = "úč";',
  7558. '']));
  7559. end;
  7560. procedure TTestModule.TestStringConst_InvalidUTF16;
  7561. begin
  7562. StartProgram(false);
  7563. Add([
  7564. 'const',
  7565. ' a: char = #$D87E;',
  7566. ' b: string = #$D87E;',
  7567. ' c: string = #$D87E#43;',
  7568. 'begin',
  7569. ' c:=''abc''#$D87E;',
  7570. ' c:=#0#1#2;',
  7571. ' c:=#127;',
  7572. ' c:=#128;',
  7573. ' c:=#255;',
  7574. ' c:=#256;',
  7575. '']);
  7576. ConvertProgram;
  7577. CheckSource('TestStringConst',
  7578. LinesToStr([
  7579. 'this.a = "\uD87E";',
  7580. 'this.b = "\uD87E";',
  7581. 'this.c = "\uD87E+";',
  7582. '']),
  7583. LinesToStr([
  7584. '$mod.c = "abc\uD87E";',
  7585. '$mod.c = "\x00\x01\x02";',
  7586. '$mod.c = "'#127'";',
  7587. '$mod.c = "'#$c2#$80'";',
  7588. '$mod.c = "'#$c3#$BF'";',
  7589. '$mod.c = "'#$c4#$80'";',
  7590. '']));
  7591. end;
  7592. procedure TTestModule.TestStringConstSurrogate;
  7593. begin
  7594. StartProgram(false);
  7595. Add([
  7596. 'var',
  7597. ' s: string;',
  7598. 'begin',
  7599. ' s:=''😊'';', // 1F60A
  7600. '']);
  7601. ConvertProgram;
  7602. CheckSource('TestStringConstSurrogate',
  7603. LinesToStr([
  7604. 'this.s="";'
  7605. ]),
  7606. LinesToStr([
  7607. '$mod.s="😊";'
  7608. ]));
  7609. end;
  7610. procedure TTestModule.TestString_Length;
  7611. begin
  7612. StartProgram(false);
  7613. Add('const c = ''foo'';');
  7614. Add('var');
  7615. Add(' s: string;');
  7616. Add(' i: longint;');
  7617. Add('begin');
  7618. Add(' i:=length(s);');
  7619. Add(' i:=length(s+s);');
  7620. Add(' i:=length(''abc'');');
  7621. Add(' i:=length(c);');
  7622. ConvertProgram;
  7623. CheckSource('TestString_Length',
  7624. LinesToStr([
  7625. 'this.c = "foo";',
  7626. 'this.s = "";',
  7627. 'this.i = 0;',
  7628. '']),
  7629. LinesToStr([
  7630. '$mod.i = $mod.s.length;',
  7631. '$mod.i = ($mod.s+$mod.s).length;',
  7632. '$mod.i = "abc".length;',
  7633. '$mod.i = $mod.c.length;',
  7634. '']));
  7635. end;
  7636. procedure TTestModule.TestString_Compare;
  7637. begin
  7638. StartProgram(false);
  7639. Add('var');
  7640. Add(' s, t: string;');
  7641. Add(' b: boolean;');
  7642. Add('begin');
  7643. Add(' b:=s=t;');
  7644. Add(' b:=s<>t;');
  7645. Add(' b:=s>t;');
  7646. Add(' b:=s>=t;');
  7647. Add(' b:=s<t;');
  7648. Add(' b:=s<=t;');
  7649. ConvertProgram;
  7650. CheckSource('TestString_Compare',
  7651. LinesToStr([ // statements
  7652. 'this.s = "";',
  7653. 'this.t = "";',
  7654. 'this.b =false;'
  7655. ]),
  7656. LinesToStr([ // this.$main
  7657. '$mod.b = $mod.s === $mod.t;',
  7658. '$mod.b = $mod.s !== $mod.t;',
  7659. '$mod.b = $mod.s > $mod.t;',
  7660. '$mod.b = $mod.s >= $mod.t;',
  7661. '$mod.b = $mod.s < $mod.t;',
  7662. '$mod.b = $mod.s <= $mod.t;',
  7663. '']));
  7664. end;
  7665. procedure TTestModule.TestString_SetLength;
  7666. begin
  7667. StartProgram(false);
  7668. Add([
  7669. 'procedure DoIt(var s: string);',
  7670. 'begin',
  7671. ' SetLength(s,2);',
  7672. 'end;',
  7673. 'var s: string;',
  7674. 'begin',
  7675. ' SetLength(s,3);',
  7676. '']);
  7677. ConvertProgram;
  7678. CheckSource('TestString_SetLength',
  7679. LinesToStr([ // statements
  7680. 'this.DoIt = function (s) {',
  7681. ' s.set(rtl.strSetLength(s.get(), 2));',
  7682. '};',
  7683. 'this.s = "";',
  7684. '']),
  7685. LinesToStr([ // this.$main
  7686. '$mod.s = rtl.strSetLength($mod.s, 3);'
  7687. ]));
  7688. end;
  7689. procedure TTestModule.TestString_CharAt;
  7690. begin
  7691. StartProgram(false);
  7692. Add([
  7693. 'var',
  7694. ' s: string;',
  7695. ' c: char;',
  7696. ' b: boolean;',
  7697. 'begin',
  7698. ' b:= s[1] = c;',
  7699. ' b:= c = s[1];',
  7700. ' b:= c <> s[1];',
  7701. ' b:= c > s[1];',
  7702. ' b:= c >= s[1];',
  7703. ' b:= c < s[2];',
  7704. ' b:= c <= s[1];',
  7705. ' s[1] := c;',
  7706. ' s[2+3] := c;']);
  7707. ConvertProgram;
  7708. CheckSource('TestString_CharAt',
  7709. LinesToStr([ // statements
  7710. 'this.s = "";',
  7711. 'this.c = "";',
  7712. 'this.b = false;'
  7713. ]),
  7714. LinesToStr([ // this.$main
  7715. '$mod.b = $mod.s.charAt(0) === $mod.c;',
  7716. '$mod.b = $mod.c === $mod.s.charAt(0);',
  7717. '$mod.b = $mod.c !== $mod.s.charAt(0);',
  7718. '$mod.b = $mod.c > $mod.s.charAt(0);',
  7719. '$mod.b = $mod.c >= $mod.s.charAt(0);',
  7720. '$mod.b = $mod.c < $mod.s.charAt(1);',
  7721. '$mod.b = $mod.c <= $mod.s.charAt(0);',
  7722. '$mod.s = rtl.setCharAt($mod.s, 0, $mod.c);',
  7723. '$mod.s = rtl.setCharAt($mod.s, (2 + 3) - 1, $mod.c);',
  7724. '']));
  7725. end;
  7726. procedure TTestModule.TestStringHMinusFail;
  7727. begin
  7728. StartProgram(false);
  7729. Add([
  7730. '{$H-}',
  7731. 'var s: string;',
  7732. 'begin']);
  7733. ConvertProgram;
  7734. CheckHint(mtWarning,nWarnIllegalCompilerDirectiveX,'Warning: test1.pp(3,6) : Illegal compiler directive "H-"');
  7735. end;
  7736. procedure TTestModule.TestStr;
  7737. begin
  7738. StartProgram(false);
  7739. Add('var');
  7740. Add(' b: boolean;');
  7741. Add(' i: longint;');
  7742. Add(' d: double;');
  7743. Add(' s: string;');
  7744. Add('begin');
  7745. Add(' str(b,s);');
  7746. Add(' str(i,s);');
  7747. Add(' str(d,s);');
  7748. Add(' str(i:3,s);');
  7749. Add(' str(d:3:2,s);');
  7750. Add(' Str(12.456:12:1,s);');
  7751. Add(' Str(12.456:12,s);');
  7752. Add(' s:=str(b);');
  7753. Add(' s:=str(i);');
  7754. Add(' s:=str(d);');
  7755. Add(' s:=str(i,i);');
  7756. Add(' s:=str(i:3);');
  7757. Add(' s:=str(d:3:2);');
  7758. Add(' s:=str(i:4,i);');
  7759. Add(' s:=str(i,i:5);');
  7760. Add(' s:=str(i:4,i:5);');
  7761. Add(' s:=str(s,s);');
  7762. Add(' s:=str(s,''foo'');');
  7763. ConvertProgram;
  7764. CheckSource('TestStr',
  7765. LinesToStr([ // statements
  7766. 'this.b = false;',
  7767. 'this.i = 0;',
  7768. 'this.d = 0.0;',
  7769. 'this.s = "";',
  7770. '']),
  7771. LinesToStr([ // this.$main
  7772. '$mod.s = ""+$mod.b;',
  7773. '$mod.s = ""+$mod.i;',
  7774. '$mod.s = rtl.floatToStr($mod.d);',
  7775. '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
  7776. '$mod.s = rtl.floatToStr($mod.d,3,2);',
  7777. '$mod.s = rtl.floatToStr(12.456,12,1);',
  7778. '$mod.s = rtl.floatToStr(12.456,12);',
  7779. '$mod.s = ""+$mod.b;',
  7780. '$mod.s = ""+$mod.i;',
  7781. '$mod.s = rtl.floatToStr($mod.d);',
  7782. '$mod.s = ""+$mod.i+$mod.i;',
  7783. '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
  7784. '$mod.s = rtl.floatToStr($mod.d,3,2);',
  7785. '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + $mod.i;',
  7786. '$mod.s = "" + $mod.i + rtl.spaceLeft("" + $mod.i, 5);',
  7787. '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + rtl.spaceLeft("" + $mod.i, 5);',
  7788. '$mod.s = $mod.s + $mod.s;',
  7789. '$mod.s = $mod.s + "foo";',
  7790. '']));
  7791. end;
  7792. procedure TTestModule.TestBaseType_AnsiStringFail;
  7793. begin
  7794. StartProgram(false);
  7795. Add('var s: AnsiString');
  7796. SetExpectedPasResolverError('identifier not found "AnsiString"',PasResolveEval.nIdentifierNotFound);
  7797. ConvertProgram;
  7798. end;
  7799. procedure TTestModule.TestBaseType_WideStringFail;
  7800. begin
  7801. StartProgram(false);
  7802. Add('var s: WideString');
  7803. SetExpectedPasResolverError('identifier not found "WideString"',PasResolveEval.nIdentifierNotFound);
  7804. ConvertProgram;
  7805. end;
  7806. procedure TTestModule.TestBaseType_ShortStringFail;
  7807. begin
  7808. StartProgram(false);
  7809. Add('var s: ShortString');
  7810. SetExpectedPasResolverError('identifier not found "ShortString"',PasResolveEval.nIdentifierNotFound);
  7811. ConvertProgram;
  7812. end;
  7813. procedure TTestModule.TestBaseType_RawByteStringFail;
  7814. begin
  7815. StartProgram(false);
  7816. Add('var s: RawByteString');
  7817. SetExpectedPasResolverError('identifier not found "RawByteString"',PasResolveEval.nIdentifierNotFound);
  7818. ConvertProgram;
  7819. end;
  7820. procedure TTestModule.TestTypeShortstring_Fail;
  7821. begin
  7822. StartProgram(false);
  7823. Add('type t = string[12];');
  7824. Add('var s: t;');
  7825. Add('begin');
  7826. SetExpectedPasResolverError('illegal qualifier "["',nIllegalQualifier);
  7827. ConvertProgram;
  7828. end;
  7829. procedure TTestModule.TestCharSet_Custom;
  7830. begin
  7831. StartProgram(false);
  7832. Add([
  7833. 'type',
  7834. ' TCharRg = ''a''..''z'';',
  7835. ' TSetOfCharRg = set of TCharRg;',
  7836. ' TCharRg2 = ''m''..''p'';',
  7837. 'const',
  7838. ' crg: TCharRg = ''b'';',
  7839. 'var',
  7840. ' c: char;',
  7841. ' crg2: TCharRg2;',
  7842. ' s: TSetOfCharRg;',
  7843. 'begin',
  7844. ' c:=crg;',
  7845. ' crg:=c;',
  7846. ' crg2:=crg;',
  7847. ' if c=crg then ;',
  7848. ' if crg=c then ;',
  7849. ' if crg=crg2 then ;',
  7850. ' if c in s then ;',
  7851. ' if crg2 in s then ;',
  7852. ' c:=default(TCharRg);',
  7853. '']);
  7854. ConvertProgram;
  7855. CheckSource('TestCharSet_Custom',
  7856. LinesToStr([ // statements
  7857. 'this.crg = "b";',
  7858. 'this.c = "";',
  7859. 'this.crg2 = "m";',
  7860. 'this.s = {};',
  7861. '']),
  7862. LinesToStr([ // this.$main
  7863. '$mod.c = $mod.crg;',
  7864. '$mod.crg = $mod.c;',
  7865. '$mod.crg2 = $mod.crg;',
  7866. 'if ($mod.c === $mod.crg) ;',
  7867. 'if ($mod.crg === $mod.c) ;',
  7868. 'if ($mod.crg === $mod.crg2) ;',
  7869. 'if ($mod.c.charCodeAt() in $mod.s) ;',
  7870. 'if ($mod.crg2.charCodeAt() in $mod.s) ;',
  7871. '$mod.c = "a";',
  7872. '']));
  7873. end;
  7874. procedure TTestModule.TestWideChar;
  7875. begin
  7876. StartProgram(false);
  7877. Add([
  7878. 'procedure Fly(var c: char);',
  7879. 'begin',
  7880. 'end;',
  7881. 'procedure Run(var c: widechar);',
  7882. 'begin',
  7883. 'end;',
  7884. 'var',
  7885. ' c: char;',
  7886. ' wc: widechar;',
  7887. ' w: word;',
  7888. 'begin',
  7889. ' Fly(wc);',
  7890. ' Run(c);',
  7891. ' wc:=WideChar(w);',
  7892. ' w:=ord(wc);',
  7893. '']);
  7894. ConvertProgram;
  7895. CheckSource('TestWideChar_VarArg',
  7896. LinesToStr([ // statements
  7897. 'this.Fly = function (c) {',
  7898. '};',
  7899. 'this.Run = function (c) {',
  7900. '};',
  7901. 'this.c = "";',
  7902. 'this.wc = "";',
  7903. 'this.w = 0;',
  7904. '']),
  7905. LinesToStr([ // this.$main
  7906. '$mod.Fly({',
  7907. ' p: $mod,',
  7908. ' get: function () {',
  7909. ' return this.p.wc;',
  7910. ' },',
  7911. ' set: function (v) {',
  7912. ' this.p.wc = v;',
  7913. ' }',
  7914. '});',
  7915. '$mod.Run({',
  7916. ' p: $mod,',
  7917. ' get: function () {',
  7918. ' return this.p.c;',
  7919. ' },',
  7920. ' set: function (v) {',
  7921. ' this.p.c = v;',
  7922. ' }',
  7923. '});',
  7924. '$mod.wc = String.fromCharCode($mod.w);',
  7925. '$mod.w = $mod.wc.charCodeAt();',
  7926. '',
  7927. '']));
  7928. end;
  7929. procedure TTestModule.TestForCharDo;
  7930. begin
  7931. StartProgram(false);
  7932. Add([
  7933. 'var c: char;',
  7934. 'begin',
  7935. ' for c:=''a'' to ''c'' do ;',
  7936. ' for c:=c downto ''a'' do ;',
  7937. ' for c:=''Б'' to ''Я'' do ;',
  7938. '']);
  7939. ConvertProgram;
  7940. CheckSource('TestForCharDo',
  7941. LinesToStr([ // statements
  7942. 'this.c = "";']),
  7943. LinesToStr([ // this.$main
  7944. 'for (var $l = 97; $l <= 99; $l++) $mod.c = String.fromCharCode($l);',
  7945. 'for (var $l1 = $mod.c.charCodeAt(); $l1 >= 97; $l1--) $mod.c = String.fromCharCode($l1);',
  7946. 'for (var $l2 = 1041; $l2 <= 1071; $l2++) $mod.c = String.fromCharCode($l2);',
  7947. '']));
  7948. end;
  7949. procedure TTestModule.TestForCharInDo;
  7950. begin
  7951. StartProgram(false);
  7952. Add([
  7953. 'type',
  7954. ' TSetOfChar = set of char;',
  7955. ' TCharRg = ''a''..''z'';',
  7956. ' TSetOfCharRg = set of TCharRg;',
  7957. 'const Foo = ''foo'';',
  7958. 'var',
  7959. ' c,c2: char;',
  7960. ' s: string;',
  7961. ' a1: array of char;',
  7962. ' a2: array[1..3] of char;',
  7963. ' soc: TSetOfChar;',
  7964. ' socr: TSetOfCharRg;',
  7965. ' cr: TCharRg;',
  7966. 'begin',
  7967. ' for c in foo do ;',
  7968. ' for c in s do ;',
  7969. ' for c in char do ;',
  7970. ' for c in a1 do ;',
  7971. ' for c in a2 do ;',
  7972. ' for c in [''1''..''3''] do ;',
  7973. ' for c in TSetOfChar do ;',
  7974. ' for c in TCharRg do ;',
  7975. ' for c in soc do c2:=c;',
  7976. ' for c in TSetOfCharRg do ;',
  7977. ' for c in socr do ;',
  7978. ' for cr in TCharRg do ;',
  7979. ' for cr in TSetOfCharRg do ;',
  7980. ' for cr in socr do ;',
  7981. '']);
  7982. ConvertProgram;
  7983. CheckSource('TestForCharInDo',
  7984. LinesToStr([ // statements
  7985. 'this.Foo = "foo";',
  7986. 'this.c = "";',
  7987. 'this.c2 = "";',
  7988. 'this.s = "";',
  7989. 'this.a1 = [];',
  7990. 'this.a2 = rtl.arraySetLength(null, "", 3);',
  7991. 'this.soc = {};',
  7992. 'this.socr = {};',
  7993. 'this.cr = "a";',
  7994. '']),
  7995. LinesToStr([ // this.$main
  7996. 'for (var $in = $mod.Foo, $l = 0, $end = $in.length - 1; $l <= $end; $l++) $mod.c = $in.charAt($l);',
  7997. 'for (var $in1 = $mod.s, $l1 = 0, $end1 = $in1.length - 1; $l1 <= $end1; $l1++) $mod.c = $in1.charAt($l1);',
  7998. 'for (var $l2 = 0; $l2 <= 65535; $l2++) $mod.c = String.fromCharCode($l2);',
  7999. 'for (var $in2 = $mod.a1, $l3 = 0, $end2 = rtl.length($in2) - 1; $l3 <= $end2; $l3++) $mod.c = $in2[$l3];',
  8000. 'for (var $in3 = $mod.a2, $l4 = 0, $end3 = rtl.length($in3) - 1; $l4 <= $end3; $l4++) $mod.c = $in3[$l4];',
  8001. 'for (var $l5 = 49; $l5 <= 51; $l5++) $mod.c = String.fromCharCode($l5);',
  8002. 'for (var $l6 = 0; $l6 <= 65535; $l6++) $mod.c = String.fromCharCode($l6);',
  8003. 'for (var $l7 = 97; $l7 <= 122; $l7++) $mod.c = String.fromCharCode($l7);',
  8004. 'for (var $l8 in $mod.soc) {',
  8005. ' $mod.c = String.fromCharCode($l8);',
  8006. ' $mod.c2 = $mod.c;',
  8007. '};',
  8008. 'for (var $l9 = 97; $l9 <= 122; $l9++) $mod.c = String.fromCharCode($l9);',
  8009. 'for (var $l10 in $mod.socr) $mod.c = String.fromCharCode($l10);',
  8010. 'for (var $l11 = 97; $l11 <= 122; $l11++) $mod.cr = String.fromCharCode($l11);',
  8011. 'for (var $l12 = 97; $l12 <= 122; $l12++) $mod.cr = String.fromCharCode($l12);',
  8012. 'for (var $l13 in $mod.socr) $mod.cr = String.fromCharCode($l13);',
  8013. '']));
  8014. end;
  8015. procedure TTestModule.TestProcTwoArgs;
  8016. begin
  8017. StartProgram(false);
  8018. Add('procedure Test(a,b: longint);');
  8019. Add('begin');
  8020. Add('end;');
  8021. Add('begin');
  8022. ConvertProgram;
  8023. CheckSource('TestProcTwoArgs',
  8024. LinesToStr([ // statements
  8025. 'this.Test = function (a,b) {',
  8026. '};'
  8027. ]),
  8028. LinesToStr([ // this.$main
  8029. ''
  8030. ]));
  8031. end;
  8032. procedure TTestModule.TestProc_DefaultValue;
  8033. begin
  8034. StartProgram(false);
  8035. Add('procedure p1(i: longint = 1);');
  8036. Add('begin');
  8037. Add('end;');
  8038. Add('procedure p2(i: longint = 1; c: char = ''a'');');
  8039. Add('begin');
  8040. Add('end;');
  8041. Add('procedure p3(d: double = 1.0; b: boolean = false; s: string = ''abc'');');
  8042. Add('begin');
  8043. Add('end;');
  8044. Add('begin');
  8045. Add(' p1;');
  8046. Add(' p1();');
  8047. Add(' p1(11);');
  8048. Add(' p2;');
  8049. Add(' p2();');
  8050. Add(' p2(12);');
  8051. Add(' p2(13,''b'');');
  8052. Add(' p3();');
  8053. ConvertProgram;
  8054. CheckSource('TestProc_DefaultValue',
  8055. LinesToStr([ // statements
  8056. 'this.p1 = function (i) {',
  8057. '};',
  8058. 'this.p2 = function (i,c) {',
  8059. '};',
  8060. 'this.p3 = function (d,b,s) {',
  8061. '};'
  8062. ]),
  8063. LinesToStr([ // this.$main
  8064. ' $mod.p1(1);',
  8065. ' $mod.p1(1);',
  8066. ' $mod.p1(11);',
  8067. ' $mod.p2(1,"a");',
  8068. ' $mod.p2(1,"a");',
  8069. ' $mod.p2(12,"a");',
  8070. ' $mod.p2(13,"b");',
  8071. ' $mod.p3(1.0,false,"abc");'
  8072. ]));
  8073. end;
  8074. procedure TTestModule.TestFunctionInt;
  8075. begin
  8076. StartProgram(false);
  8077. Add('function MyTest(Bar: longint): longint;');
  8078. Add('begin');
  8079. Add(' Result:=2*bar');
  8080. Add('end;');
  8081. Add('begin');
  8082. ConvertProgram;
  8083. CheckSource('TestFunctionInt',
  8084. LinesToStr([ // statements
  8085. 'this.MyTest = function (Bar) {',
  8086. ' var Result = 0;',
  8087. ' Result = 2*Bar;',
  8088. ' return Result;',
  8089. '};'
  8090. ]),
  8091. LinesToStr([ // this.$main
  8092. ''
  8093. ]));
  8094. end;
  8095. procedure TTestModule.TestFunctionString;
  8096. begin
  8097. StartProgram(false);
  8098. Add('function Test(Bar: string): string;');
  8099. Add('begin');
  8100. Add(' Result:=bar+BAR');
  8101. Add('end;');
  8102. Add('begin');
  8103. ConvertProgram;
  8104. CheckSource('TestFunctionString',
  8105. LinesToStr([ // statements
  8106. 'this.Test = function (Bar) {',
  8107. ' var Result = "";',
  8108. ' Result = Bar+Bar;',
  8109. ' return Result;',
  8110. '};'
  8111. ]),
  8112. LinesToStr([ // this.$main
  8113. ''
  8114. ]));
  8115. end;
  8116. procedure TTestModule.TestIfThen;
  8117. begin
  8118. StartProgram(false);
  8119. Add([
  8120. 'var b: boolean;',
  8121. 'begin',
  8122. ' if b then ;',
  8123. ' if b then else ;']);
  8124. ConvertProgram;
  8125. CheckSource('TestIfThen',
  8126. LinesToStr([ // statements
  8127. 'this.b = false;',
  8128. '']),
  8129. LinesToStr([ // this.$main
  8130. 'if ($mod.b) ;',
  8131. 'if ($mod.b) ;',
  8132. '']));
  8133. end;
  8134. procedure TTestModule.TestForLoop;
  8135. begin
  8136. StartProgram(false);
  8137. Add('var');
  8138. Add(' vI, vJ, vN: longint;');
  8139. Add('begin');
  8140. Add(' VJ:=0;');
  8141. Add(' VN:=3;');
  8142. Add(' for VI:=1 to VN do');
  8143. Add(' begin');
  8144. Add(' VJ:=VJ+VI;');
  8145. Add(' end;');
  8146. ConvertProgram;
  8147. CheckSource('TestForLoop',
  8148. LinesToStr([ // statements
  8149. 'this.vI = 0;',
  8150. 'this.vJ = 0;',
  8151. 'this.vN = 0;'
  8152. ]),
  8153. LinesToStr([ // this.$main
  8154. ' $mod.vJ = 0;',
  8155. ' $mod.vN = 3;',
  8156. ' for (var $l = 1, $end = $mod.vN; $l <= $end; $l++) {',
  8157. ' $mod.vI = $l;',
  8158. ' $mod.vJ = $mod.vJ + $mod.vI;',
  8159. ' };',
  8160. '']));
  8161. end;
  8162. procedure TTestModule.TestForLoopInsideFunction;
  8163. begin
  8164. StartProgram(false);
  8165. Add('function SumNumbers(Count: longint): longint;');
  8166. Add('var');
  8167. Add(' vI, vJ: longint;');
  8168. Add('begin');
  8169. Add(' vj:=0;');
  8170. Add(' for vi:=1 to count do');
  8171. Add(' begin');
  8172. Add(' vj:=vj+vi;');
  8173. Add(' end;');
  8174. Add('end;');
  8175. Add('begin');
  8176. Add(' sumnumbers(3);');
  8177. ConvertProgram;
  8178. CheckSource('TestForLoopInsideFunction',
  8179. LinesToStr([ // statements
  8180. 'this.SumNumbers = function (Count) {',
  8181. ' var Result = 0;',
  8182. ' var vI = 0;',
  8183. ' var vJ = 0;',
  8184. ' vJ = 0;',
  8185. ' for (var $l = 1, $end = Count; $l <= $end; $l++) {',
  8186. ' vI = $l;',
  8187. ' vJ = vJ + vI;',
  8188. ' };',
  8189. ' return Result;',
  8190. '};'
  8191. ]),
  8192. LinesToStr([ // $mod.$main
  8193. ' $mod.SumNumbers(3);'
  8194. ]));
  8195. end;
  8196. procedure TTestModule.TestForLoop_ReadVarAfter;
  8197. begin
  8198. StartProgram(false);
  8199. Add('var');
  8200. Add(' vI: longint;');
  8201. Add('begin');
  8202. Add(' for vi:=1 to 2 do ;');
  8203. Add(' if vi=3 then ;');
  8204. ConvertProgram;
  8205. CheckSource('TestForLoop',
  8206. LinesToStr([ // statements
  8207. 'this.vI = 0;'
  8208. ]),
  8209. LinesToStr([ // this.$main
  8210. ' for ($mod.vI = 1; $mod.vI <= 2; $mod.vI++) ;',
  8211. ' if ($mod.vI===3) ;'
  8212. ]));
  8213. end;
  8214. procedure TTestModule.TestForLoop_Nested;
  8215. begin
  8216. StartProgram(false);
  8217. Add('function SumNumbers(Count: longint): longint;');
  8218. Add('var');
  8219. Add(' vI, vJ, vK: longint;');
  8220. Add('begin');
  8221. Add(' VK:=0;');
  8222. Add(' for VI:=1 to count do');
  8223. Add(' begin');
  8224. Add(' for vj:=1 to vi do');
  8225. Add(' begin');
  8226. Add(' vk:=VK+VI;');
  8227. Add(' end;');
  8228. Add(' end;');
  8229. Add('end;');
  8230. Add('begin');
  8231. Add(' sumnumbers(3);');
  8232. ConvertProgram;
  8233. CheckSource('TestForLoopInFunction',
  8234. LinesToStr([ // statements
  8235. 'this.SumNumbers = function (Count) {',
  8236. ' var Result = 0;',
  8237. ' var vI = 0;',
  8238. ' var vJ = 0;',
  8239. ' var vK = 0;',
  8240. ' vK = 0;',
  8241. ' for (var $l = 1, $end = Count; $l <= $end; $l++) {',
  8242. ' vI = $l;',
  8243. ' for (var $l1 = 1, $end1 = vI; $l1 <= $end1; $l1++) {',
  8244. ' vJ = $l1;',
  8245. ' vK = vK + vI;',
  8246. ' };',
  8247. ' };',
  8248. ' return Result;',
  8249. '};'
  8250. ]),
  8251. LinesToStr([ // $mod.$main
  8252. ' $mod.SumNumbers(3);'
  8253. ]));
  8254. end;
  8255. procedure TTestModule.TestRepeatUntil;
  8256. begin
  8257. StartProgram(false);
  8258. Add('var');
  8259. Add(' vI, vJ, vN: longint;');
  8260. Add('begin');
  8261. Add(' vn:=3;');
  8262. Add(' vj:=0;');
  8263. Add(' VI:=0;');
  8264. Add(' repeat');
  8265. Add(' VI:=vi+1;');
  8266. Add(' vj:=VJ+vI;');
  8267. Add(' until vi>=vn');
  8268. ConvertProgram;
  8269. CheckSource('TestRepeatUntil',
  8270. LinesToStr([ // statements
  8271. 'this.vI = 0;',
  8272. 'this.vJ = 0;',
  8273. 'this.vN = 0;'
  8274. ]),
  8275. LinesToStr([ // $mod.$main
  8276. ' $mod.vN = 3;',
  8277. ' $mod.vJ = 0;',
  8278. ' $mod.vI = 0;',
  8279. ' do{',
  8280. ' $mod.vI = $mod.vI + 1;',
  8281. ' $mod.vJ = $mod.vJ + $mod.vI;',
  8282. ' }while(!($mod.vI>=$mod.vN));'
  8283. ]));
  8284. end;
  8285. procedure TTestModule.TestAsmBlock;
  8286. begin
  8287. StartProgram(false);
  8288. Add([
  8289. 'var',
  8290. ' vI: longint;',
  8291. 'begin',
  8292. ' vi:=1;',
  8293. ' asm',
  8294. ' if (vI===1) {',
  8295. ' vI=2;',
  8296. //' console.log(''end;'');', ToDo
  8297. ' }',
  8298. ' if (vI===2){ vI=3; }',
  8299. ' end;',
  8300. ' VI:=4;']);
  8301. ConvertProgram;
  8302. CheckSource('TestAsmBlock',
  8303. LinesToStr([ // statements
  8304. 'this.vI = 0;'
  8305. ]),
  8306. LinesToStr([ // $mod.$main
  8307. '$mod.vI = 1;',
  8308. 'if (vI===1) {',
  8309. ' vI=2;',
  8310. '}',
  8311. 'if (vI===2){ vI=3; }',
  8312. ';',
  8313. '$mod.vI = 4;'
  8314. ]));
  8315. end;
  8316. procedure TTestModule.TestAsmPas_Impl;
  8317. begin
  8318. StartUnit(false);
  8319. Add('interface');
  8320. Add('const cIntf: longint = 1;');
  8321. Add('var vIntf: longint;');
  8322. Add('implementation');
  8323. Add('const cImpl: longint = 2;');
  8324. Add('var vImpl: longint;');
  8325. Add('procedure DoIt;');
  8326. Add('const cLoc: longint = 3;');
  8327. Add('var vLoc: longint;');
  8328. Add('begin;');
  8329. Add(' asm');
  8330. //Add(' pas(vIntf)=pas(cIntf);');
  8331. //Add(' pas(vImpl)=pas(cImpl);');
  8332. //Add(' pas(vLoc)=pas(cLoc);');
  8333. Add(' end;');
  8334. Add('end;');
  8335. ConvertUnit;
  8336. CheckSource('TestAsmPas_Impl',
  8337. LinesToStr([
  8338. 'var $impl = $mod.$impl;',
  8339. 'this.cIntf = 1;',
  8340. 'this.vIntf = 0;',
  8341. '']),
  8342. '', // this.$init
  8343. LinesToStr([ // implementation
  8344. '$impl.cImpl = 2;',
  8345. '$impl.vImpl = 0;',
  8346. 'var cLoc = 3;',
  8347. '$impl.DoIt = function () {',
  8348. ' var vLoc = 0;',
  8349. '};',
  8350. '']) );
  8351. end;
  8352. procedure TTestModule.TestTryFinally;
  8353. begin
  8354. StartProgram(false);
  8355. Add('var i: longint;');
  8356. Add('begin');
  8357. Add(' try');
  8358. Add(' i:=0; i:=2 div i;');
  8359. Add(' finally');
  8360. Add(' i:=3');
  8361. Add(' end;');
  8362. ConvertProgram;
  8363. CheckSource('TestTryFinally',
  8364. LinesToStr([ // statements
  8365. 'this.i = 0;'
  8366. ]),
  8367. LinesToStr([ // $mod.$main
  8368. 'try {',
  8369. ' $mod.i = 0;',
  8370. ' $mod.i = rtl.trunc(2 / $mod.i);',
  8371. '} finally {',
  8372. ' $mod.i = 3;',
  8373. '};'
  8374. ]));
  8375. end;
  8376. procedure TTestModule.TestTryExcept;
  8377. begin
  8378. StartProgram(false);
  8379. Add([
  8380. 'type',
  8381. ' TObject = class end;',
  8382. ' Exception = class Msg: string; end;',
  8383. ' EInvalidCast = class(Exception) end;',
  8384. 'var vI: longint;',
  8385. 'begin',
  8386. ' try',
  8387. ' vi:=1;',
  8388. ' except',
  8389. ' vi:=2',
  8390. ' end;',
  8391. ' try',
  8392. ' vi:=3;',
  8393. ' except',
  8394. ' raise;',
  8395. ' end;',
  8396. ' try',
  8397. ' VI:=4;',
  8398. ' except',
  8399. ' on einvalidcast do',
  8400. ' raise;',
  8401. ' on E: exception do',
  8402. ' if e.msg='''' then',
  8403. ' raise e;',
  8404. ' else',
  8405. ' vi:=5',
  8406. ' end;',
  8407. ' try',
  8408. ' VI:=6;',
  8409. ' except',
  8410. ' on einvalidcast do ;',
  8411. ' end;',
  8412. '']);
  8413. ConvertProgram;
  8414. CheckSource('TestTryExcept',
  8415. LinesToStr([ // statements
  8416. 'rtl.createClass(this, "TObject", null, function () {',
  8417. ' this.$init = function () {',
  8418. ' };',
  8419. ' this.$final = function () {',
  8420. ' };',
  8421. '});',
  8422. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  8423. ' this.$init = function () {',
  8424. ' $mod.TObject.$init.call(this);',
  8425. ' this.Msg = "";',
  8426. ' };',
  8427. '});',
  8428. 'rtl.createClass(this, "EInvalidCast", this.Exception, function () {',
  8429. '});',
  8430. 'this.vI = 0;'
  8431. ]),
  8432. LinesToStr([ // $mod.$main
  8433. 'try {',
  8434. ' $mod.vI = 1;',
  8435. '} catch ($e) {',
  8436. ' $mod.vI = 2;',
  8437. '};',
  8438. 'try {',
  8439. ' $mod.vI = 3;',
  8440. '} catch ($e) {',
  8441. ' throw $e;',
  8442. '};',
  8443. 'try {',
  8444. ' $mod.vI = 4;',
  8445. '} catch ($e) {',
  8446. ' if ($mod.EInvalidCast.isPrototypeOf($e)){',
  8447. ' throw $e',
  8448. ' } else if ($mod.Exception.isPrototypeOf($e)) {',
  8449. ' var E = $e;',
  8450. ' if (E.Msg === "") throw E;',
  8451. ' } else {',
  8452. ' $mod.vI = 5;',
  8453. ' }',
  8454. '};',
  8455. 'try {',
  8456. ' $mod.vI = 6;',
  8457. '} catch ($e) {',
  8458. ' if ($mod.EInvalidCast.isPrototypeOf($e)){' ,
  8459. ' } else throw $e',
  8460. '};',
  8461. '']));
  8462. end;
  8463. procedure TTestModule.TestTryExcept_ReservedWords;
  8464. begin
  8465. StartProgram(false);
  8466. Add([
  8467. 'type',
  8468. ' TObject = class end;',
  8469. ' Exception = class',
  8470. ' Symbol: string;',
  8471. ' end;',
  8472. 'var &try: longint;',
  8473. 'begin',
  8474. ' try',
  8475. ' &try:=4;',
  8476. ' except',
  8477. ' on Error: exception do',
  8478. ' if errOR.symBol='''' then',
  8479. ' raise ERRor;',
  8480. ' end;',
  8481. '']);
  8482. ConvertProgram;
  8483. CheckSource('TestTryExcept_ReservedWords',
  8484. LinesToStr([ // statements
  8485. 'rtl.createClass(this, "TObject", null, function () {',
  8486. ' this.$init = function () {',
  8487. ' };',
  8488. ' this.$final = function () {',
  8489. ' };',
  8490. '});',
  8491. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  8492. ' this.$init = function () {',
  8493. ' $mod.TObject.$init.call(this);',
  8494. ' this.Symbol = "";',
  8495. ' };',
  8496. '});',
  8497. 'this.Try = 0;',
  8498. '']),
  8499. LinesToStr([ // $mod.$main
  8500. 'try {',
  8501. ' $mod.Try = 4;',
  8502. '} catch ($e) {',
  8503. ' if ($mod.Exception.isPrototypeOf($e)) {',
  8504. ' var error = $e;',
  8505. ' if (error.Symbol === "") throw error;',
  8506. ' } else throw $e',
  8507. '};',
  8508. '']));
  8509. end;
  8510. procedure TTestModule.TestIfThenRaiseElse;
  8511. begin
  8512. StartProgram(false);
  8513. Add([
  8514. 'type',
  8515. ' TObject = class',
  8516. ' constructor Create;',
  8517. ' end;',
  8518. 'constructor TObject.Create;',
  8519. 'begin',
  8520. 'end;',
  8521. 'var b: boolean;',
  8522. 'begin',
  8523. ' if b then',
  8524. ' raise TObject.Create',
  8525. ' else',
  8526. ' b:=false;',
  8527. '']);
  8528. ConvertProgram;
  8529. CheckSource('TestIfThenRaiseElse',
  8530. LinesToStr([ // statements
  8531. 'rtl.createClass(this, "TObject", null, function () {',
  8532. ' this.$init = function () {',
  8533. ' };',
  8534. ' this.$final = function () {',
  8535. ' };',
  8536. ' this.Create = function () {',
  8537. ' return this;',
  8538. ' };',
  8539. '});',
  8540. 'this.b = false;',
  8541. '']),
  8542. LinesToStr([ // $mod.$main
  8543. 'if ($mod.b) {',
  8544. ' throw $mod.TObject.$create("Create")}',
  8545. ' else $mod.b = false;',
  8546. '']));
  8547. end;
  8548. procedure TTestModule.TestCaseOf;
  8549. begin
  8550. StartProgram(false);
  8551. Add([
  8552. 'const e: longint; external name ''$e'';',
  8553. 'var vI: longint;',
  8554. 'begin',
  8555. ' case vi of',
  8556. ' 1: ;',
  8557. ' 2: vi:=3;',
  8558. ' e: ;',
  8559. ' else',
  8560. ' VI:=4',
  8561. ' end;']);
  8562. ConvertProgram;
  8563. CheckSource('TestCaseOf',
  8564. LinesToStr([ // statements
  8565. 'this.vI = 0;'
  8566. ]),
  8567. LinesToStr([ // $mod.$main
  8568. 'var $tmp = $mod.vI;',
  8569. 'if ($tmp === 1) {}',
  8570. 'else if ($tmp === 2) {',
  8571. ' $mod.vI = 3}',
  8572. ' else if ($tmp === $e) {}',
  8573. 'else {',
  8574. ' $mod.vI = 4;',
  8575. '};'
  8576. ]));
  8577. end;
  8578. procedure TTestModule.TestCaseOf_UseSwitch;
  8579. begin
  8580. StartProgram(false);
  8581. Converter.UseSwitchStatement:=true;
  8582. Add('var Vi: longint;');
  8583. Add('begin');
  8584. Add(' case vi of');
  8585. Add(' 1: ;');
  8586. Add(' 2: VI:=3;');
  8587. Add(' else');
  8588. Add(' vi:=4');
  8589. Add(' end;');
  8590. ConvertProgram;
  8591. CheckSource('TestCaseOf_UseSwitch',
  8592. LinesToStr([ // statements
  8593. 'this.Vi = 0;'
  8594. ]),
  8595. LinesToStr([ // $mod.$main
  8596. 'switch ($mod.Vi) {',
  8597. 'case 1:',
  8598. ' break;',
  8599. 'case 2:',
  8600. ' $mod.Vi = 3;',
  8601. ' break;',
  8602. 'default:',
  8603. ' $mod.Vi = 4;',
  8604. '};'
  8605. ]));
  8606. end;
  8607. procedure TTestModule.TestCaseOfNoElse;
  8608. begin
  8609. StartProgram(false);
  8610. Add('var Vi: longint;');
  8611. Add('begin');
  8612. Add(' case vi of');
  8613. Add(' 1: begin vi:=2; VI:=3; end;');
  8614. Add(' end;');
  8615. ConvertProgram;
  8616. CheckSource('TestCaseOfNoElse',
  8617. LinesToStr([ // statements
  8618. 'this.Vi = 0;'
  8619. ]),
  8620. LinesToStr([ // $mod.$main
  8621. 'var $tmp = $mod.Vi;',
  8622. 'if ($tmp === 1) {',
  8623. ' $mod.Vi = 2;',
  8624. ' $mod.Vi = 3;',
  8625. '};'
  8626. ]));
  8627. end;
  8628. procedure TTestModule.TestCaseOfNoElse_UseSwitch;
  8629. begin
  8630. StartProgram(false);
  8631. Converter.UseSwitchStatement:=true;
  8632. Add('var vI: longint;');
  8633. Add('begin');
  8634. Add(' case vi of');
  8635. Add(' 1: begin VI:=2; vi:=3; end;');
  8636. Add(' end;');
  8637. ConvertProgram;
  8638. CheckSource('TestCaseOfNoElse_UseSwitch',
  8639. LinesToStr([ // statements
  8640. 'this.vI = 0;'
  8641. ]),
  8642. LinesToStr([ // $mod.$main
  8643. 'switch ($mod.vI) {',
  8644. 'case 1:',
  8645. ' $mod.vI = 2;',
  8646. ' $mod.vI = 3;',
  8647. ' break;',
  8648. '};'
  8649. ]));
  8650. end;
  8651. procedure TTestModule.TestCaseOfRange;
  8652. begin
  8653. StartProgram(false);
  8654. Add('var vI: longint;');
  8655. Add('begin');
  8656. Add(' case vi of');
  8657. Add(' 1..3: vi:=14;');
  8658. Add(' 4,5: vi:=16;');
  8659. Add(' 6..7,9..10: ;');
  8660. Add(' else ;');
  8661. Add(' end;');
  8662. ConvertProgram;
  8663. CheckSource('TestCaseOfRange',
  8664. LinesToStr([ // statements
  8665. 'this.vI = 0;'
  8666. ]),
  8667. LinesToStr([ // $mod.$main
  8668. 'var $tmp = $mod.vI;',
  8669. 'if (($tmp >= 1) && ($tmp <= 3)){',
  8670. ' $mod.vI = 14',
  8671. '} else if (($tmp === 4) || ($tmp === 5)){',
  8672. ' $mod.vI = 16',
  8673. '} else if ((($tmp >= 6) && ($tmp <= 7)) || (($tmp >= 9) && ($tmp <= 10))) ;'
  8674. ]));
  8675. end;
  8676. procedure TTestModule.TestCaseOfString;
  8677. begin
  8678. StartProgram(false);
  8679. Add([
  8680. 'var s,h: string;',
  8681. 'begin',
  8682. ' case s of',
  8683. ' ''foo'': s:=h;',
  8684. ' ''a''..''z'': h:=s;',
  8685. ' ''ў'', ''ё'': ;',
  8686. ' ''Б''..''Я'': ;',
  8687. ' end;',
  8688. '']);
  8689. ConvertProgram;
  8690. CheckSource('TestCaseOfString',
  8691. LinesToStr([ // statements
  8692. 'this.s = "";',
  8693. 'this.h = "";',
  8694. '']),
  8695. LinesToStr([ // $mod.$main
  8696. 'var $tmp = $mod.s;',
  8697. 'if ($tmp === "foo") {',
  8698. ' $mod.s = $mod.h}',
  8699. ' else if (($tmp.length === 1) && ($tmp >= "a") && ($tmp <= "z")) {',
  8700. ' $mod.h = $mod.s}',
  8701. ' else if (($tmp === "ў") || ($tmp === "ё")) {}',
  8702. ' else if (($tmp.length === 1) && ($tmp >= "Б") && ($tmp <= "Я")) ;',
  8703. '']));
  8704. end;
  8705. procedure TTestModule.TestCaseOfChar;
  8706. begin
  8707. StartProgram(false);
  8708. Add([
  8709. 'var s,h: char;',
  8710. 'begin',
  8711. ' case s of',
  8712. ' ''a''..''z'': h:=s;',
  8713. ' ''ä'': ;',
  8714. ' ''ў'', ''ё'': ;',
  8715. ' ''Б''..''Я'': ;',
  8716. ' end;',
  8717. '']);
  8718. ConvertProgram;
  8719. CheckSource('TestCaseOfString',
  8720. LinesToStr([ // statements
  8721. 'this.s = "";',
  8722. 'this.h = "";',
  8723. '']),
  8724. LinesToStr([ // $mod.$main
  8725. 'var $tmp = $mod.s;',
  8726. 'if (($tmp >= "a") && ($tmp <= "z")) {',
  8727. ' $mod.h = $mod.s}',
  8728. ' else if ($tmp === "ä") {}',
  8729. ' else if (($tmp === "ў") || ($tmp === "ё")) {}',
  8730. ' else if (($tmp >= "Б") && ($tmp <= "Я")) ;',
  8731. '']));
  8732. end;
  8733. procedure TTestModule.TestCaseOfExternalClassConst;
  8734. begin
  8735. StartProgram(false);
  8736. Add([
  8737. '{$modeswitch externalclass}',
  8738. 'type',
  8739. ' TBird = class external name ''Bird''',
  8740. ' const e: longint;',
  8741. ' end;',
  8742. 'var vI: longint;',
  8743. 'begin',
  8744. ' case vi of',
  8745. ' 1: vi:=3;',
  8746. ' TBird.e: ;',
  8747. ' end;']);
  8748. ConvertProgram;
  8749. CheckSource('TestCaseOfExternalClassConst',
  8750. LinesToStr([ // statements
  8751. 'this.vI = 0;'
  8752. ]),
  8753. LinesToStr([ // $mod.$main
  8754. 'var $tmp = $mod.vI;',
  8755. 'if ($tmp === 1) {',
  8756. ' $mod.vI = 3}',
  8757. ' else if ($tmp === Bird.e) ;'
  8758. ]));
  8759. end;
  8760. procedure TTestModule.TestDebugger;
  8761. begin
  8762. StartProgram(false);
  8763. Add([
  8764. 'procedure DoIt;',
  8765. 'begin',
  8766. ' deBugger;',
  8767. ' DeBugger();',
  8768. 'end;',
  8769. 'begin',
  8770. ' Debugger;']);
  8771. ConvertProgram;
  8772. CheckSource('TestDebugger',
  8773. LinesToStr([ // statements
  8774. 'this.DoIt = function () {',
  8775. ' debugger;',
  8776. ' debugger;',
  8777. '};',
  8778. '']),
  8779. LinesToStr([ // $mod.$main
  8780. 'debugger;',
  8781. '']));
  8782. end;
  8783. procedure TTestModule.TestArray_Dynamic;
  8784. begin
  8785. StartProgram(false);
  8786. Add([
  8787. 'type',
  8788. ' TArrayInt = array of longint;',
  8789. 'var',
  8790. ' Arr: TArrayInt;',
  8791. ' i: longint;',
  8792. ' b: boolean;',
  8793. 'begin',
  8794. ' SetLength(arr,3);',
  8795. ' arr[0]:=4;',
  8796. ' arr[1]:=length(arr)+arr[0];',
  8797. ' arr[i]:=5;',
  8798. ' arr[arr[i]]:=arr[6];',
  8799. ' i:=low(arr);',
  8800. ' i:=high(arr);',
  8801. ' b:=Assigned(arr);',
  8802. ' Arr:=default(TArrayInt);']);
  8803. ConvertProgram;
  8804. CheckSource('TestArray_Dynamic',
  8805. LinesToStr([ // statements
  8806. 'this.Arr = [];',
  8807. 'this.i = 0;',
  8808. 'this.b = false;'
  8809. ]),
  8810. LinesToStr([ // $mod.$main
  8811. '$mod.Arr = rtl.arraySetLength($mod.Arr,0,3);',
  8812. '$mod.Arr[0] = 4;',
  8813. '$mod.Arr[1] = rtl.length($mod.Arr) + $mod.Arr[0];',
  8814. '$mod.Arr[$mod.i] = 5;',
  8815. '$mod.Arr[$mod.Arr[$mod.i]] = $mod.Arr[6];',
  8816. '$mod.i = 0;',
  8817. '$mod.i = rtl.length($mod.Arr) - 1;',
  8818. '$mod.b = rtl.length($mod.Arr) > 0;',
  8819. '$mod.Arr = [];',
  8820. '']));
  8821. end;
  8822. procedure TTestModule.TestArray_Dynamic_Nil;
  8823. begin
  8824. StartProgram(false);
  8825. Add('type');
  8826. Add(' TArrayInt = array of longint;');
  8827. Add('var');
  8828. Add(' Arr: TArrayInt;');
  8829. Add('procedure DoIt(const i: TArrayInt; j: TArrayInt); begin end;');
  8830. Add('begin');
  8831. Add(' arr:=nil;');
  8832. Add(' if arr=nil then;');
  8833. Add(' if nil=arr then;');
  8834. Add(' if arr<>nil then;');
  8835. Add(' if nil<>arr then;');
  8836. Add(' DoIt(nil,nil);');
  8837. ConvertProgram;
  8838. CheckSource('TestArray_Dynamic',
  8839. LinesToStr([ // statements
  8840. 'this.Arr = [];',
  8841. 'this.DoIt = function(i,j){',
  8842. '};'
  8843. ]),
  8844. LinesToStr([ // $mod.$main
  8845. '$mod.Arr = [];',
  8846. 'if (rtl.length($mod.Arr) === 0) ;',
  8847. 'if (rtl.length($mod.Arr) === 0) ;',
  8848. 'if (rtl.length($mod.Arr) > 0) ;',
  8849. 'if (rtl.length($mod.Arr) > 0) ;',
  8850. '$mod.DoIt([],[]);',
  8851. '']));
  8852. end;
  8853. procedure TTestModule.TestArray_DynMultiDimensional;
  8854. begin
  8855. StartProgram(false);
  8856. Add([
  8857. 'type',
  8858. ' TArrayInt = array of longint;',
  8859. ' TArrayArrayInt = array of TArrayInt;',
  8860. 'var',
  8861. ' Arr: TArrayInt;',
  8862. ' Arr2: TArrayArrayInt;',
  8863. ' i: longint;',
  8864. 'begin',
  8865. ' arr2:=nil;',
  8866. ' if arr2=nil then;',
  8867. ' if nil=arr2 then;',
  8868. ' i:=low(arr2);',
  8869. ' i:=low(arr2[1]);',
  8870. ' i:=high(arr2);',
  8871. ' i:=high(arr2[2]);',
  8872. ' arr2[3]:=arr;',
  8873. ' arr2[4][5]:=i;',
  8874. ' i:=arr2[6][7];',
  8875. ' arr2[8,9]:=i;',
  8876. ' i:=arr2[10,11];',
  8877. ' SetLength(arr2,14);',
  8878. ' SetLength(arr2[15],16);']);
  8879. ConvertProgram;
  8880. CheckSource('TestArray_Dynamic',
  8881. LinesToStr([ // statements
  8882. 'this.Arr = [];',
  8883. 'this.Arr2 = [];',
  8884. 'this.i = 0;'
  8885. ]),
  8886. LinesToStr([ // $mod.$main
  8887. '$mod.Arr2 = [];',
  8888. 'if (rtl.length($mod.Arr2) === 0) ;',
  8889. 'if (rtl.length($mod.Arr2) === 0) ;',
  8890. '$mod.i = 0;',
  8891. '$mod.i = 0;',
  8892. '$mod.i = rtl.length($mod.Arr2) - 1;',
  8893. '$mod.i = rtl.length($mod.Arr2[2]) - 1;',
  8894. '$mod.Arr2[3] = rtl.arrayRef($mod.Arr);',
  8895. '$mod.Arr2[4][5] = $mod.i;',
  8896. '$mod.i = $mod.Arr2[6][7];',
  8897. '$mod.Arr2[8][9] = $mod.i;',
  8898. '$mod.i = $mod.Arr2[10][11];',
  8899. '$mod.Arr2 = rtl.arraySetLength($mod.Arr2, [], 14);',
  8900. '$mod.Arr2[15] = rtl.arraySetLength($mod.Arr2[15], 0, 16);',
  8901. '']));
  8902. end;
  8903. procedure TTestModule.TestArray_DynamicAssign;
  8904. begin
  8905. StartProgram(false);
  8906. Add([
  8907. 'type',
  8908. ' TArrayInt = array of longint;',
  8909. ' TArrayArrayInt = array of TArrayInt;',
  8910. 'procedure Run(a: TArrayInt; const b: TArrayInt; constref c: TArrayInt);',
  8911. 'begin',
  8912. 'end;',
  8913. 'procedure Fly(var a: TArrayInt);',
  8914. 'begin',
  8915. 'end;',
  8916. 'var',
  8917. ' Arr: TArrayInt;',
  8918. ' Arr2: TArrayArrayInt;',
  8919. 'begin',
  8920. ' arr:=nil;',
  8921. ' arr2:=nil;',
  8922. ' arr2[1]:=nil;',
  8923. ' arr2[2]:=arr;',
  8924. ' Run(arr,arr,arr);',
  8925. ' Fly(arr);',
  8926. ' Run(arr2[4],arr2[5],arr2[6]);',
  8927. ' Fly(arr2[7]);',
  8928. '']);
  8929. ConvertProgram;
  8930. CheckSource('TestArray_DynamicAssign',
  8931. LinesToStr([ // statements
  8932. 'this.Run = function (a, b, c) {',
  8933. '};',
  8934. 'this.Fly = function (a) {',
  8935. '};',
  8936. 'this.Arr = [];',
  8937. 'this.Arr2 = [];',
  8938. '']),
  8939. LinesToStr([ // $mod.$main
  8940. '$mod.Arr = [];',
  8941. '$mod.Arr2 = [];',
  8942. '$mod.Arr2[1] = [];',
  8943. '$mod.Arr2[2] = rtl.arrayRef($mod.Arr);',
  8944. '$mod.Run(rtl.arrayRef($mod.Arr), $mod.Arr, $mod.Arr);',
  8945. '$mod.Fly({',
  8946. ' p: $mod,',
  8947. ' get: function () {',
  8948. ' return this.p.Arr;',
  8949. ' },',
  8950. ' set: function (v) {',
  8951. ' this.p.Arr = v;',
  8952. ' }',
  8953. '});',
  8954. '$mod.Run(rtl.arrayRef($mod.Arr2[4]), $mod.Arr2[5], $mod.Arr2[6]);',
  8955. '$mod.Fly({',
  8956. ' a: 7,',
  8957. ' p: $mod.Arr2,',
  8958. ' get: function () {',
  8959. ' return this.p[this.a];',
  8960. ' },',
  8961. ' set: function (v) {',
  8962. ' this.p[this.a] = v;',
  8963. ' }',
  8964. '});',
  8965. '']));
  8966. end;
  8967. procedure TTestModule.TestArray_StaticInt;
  8968. begin
  8969. StartProgram(false);
  8970. Add('type');
  8971. Add(' TArrayInt = array[2..4] of longint;');
  8972. Add('var');
  8973. Add(' Arr: TArrayInt;');
  8974. Add(' Arr2: TArrayInt = (5,6,7);');
  8975. Add(' i: longint;');
  8976. Add(' b: boolean;');
  8977. Add('begin');
  8978. Add(' arr[2]:=4;');
  8979. Add(' arr[3]:=arr[2]+arr[3];');
  8980. Add(' arr[i]:=5;');
  8981. Add(' arr[arr[i]]:=arr[high(arr)];');
  8982. Add(' i:=low(arr);');
  8983. Add(' i:=high(arr);');
  8984. Add(' b:=arr[2]=arr[3];');
  8985. Add(' arr:=default(TArrayInt);');
  8986. ConvertProgram;
  8987. CheckSource('TestArray_StaticInt',
  8988. LinesToStr([ // statements
  8989. 'this.Arr = rtl.arraySetLength(null,0,3);',
  8990. 'this.Arr2 = [5, 6, 7];',
  8991. 'this.i = 0;',
  8992. 'this.b = false;'
  8993. ]),
  8994. LinesToStr([ // $mod.$main
  8995. '$mod.Arr[0] = 4;',
  8996. '$mod.Arr[1] = $mod.Arr[0] + $mod.Arr[1];',
  8997. '$mod.Arr[$mod.i-2] = 5;',
  8998. '$mod.Arr[$mod.Arr[$mod.i-2]-2] = $mod.Arr[2];',
  8999. '$mod.i = 2;',
  9000. '$mod.i = 4;',
  9001. '$mod.b = $mod.Arr[0] === $mod.Arr[1];',
  9002. '$mod.Arr = rtl.arraySetLength(null,0,3);',
  9003. '']));
  9004. end;
  9005. procedure TTestModule.TestArray_StaticBool;
  9006. begin
  9007. StartProgram(false);
  9008. Add('type');
  9009. Add(' TBools = array[boolean] of boolean;');
  9010. Add(' TBool2 = array[true..true] of boolean;');
  9011. Add('var');
  9012. Add(' Arr: TBools;');
  9013. Add(' Arr2: TBool2;');
  9014. Add(' Arr3: TBools = (true,false);');
  9015. Add(' b: boolean;');
  9016. Add('begin');
  9017. Add(' b:=low(arr);');
  9018. Add(' b:=high(arr);');
  9019. Add(' arr[true]:=false;');
  9020. Add(' arr[false]:=arr[b] or arr[true];');
  9021. Add(' arr[b]:=true;');
  9022. Add(' arr[arr[b]]:=arr[high(arr)];');
  9023. Add(' b:=arr[false]=arr[true];');
  9024. Add(' b:=low(arr2);');
  9025. Add(' b:=high(arr2);');
  9026. Add(' arr2[true]:=true;');
  9027. Add(' arr2[true]:=arr2[true] and arr2[b];');
  9028. Add(' arr2[b]:=false;');
  9029. ConvertProgram;
  9030. CheckSource('TestArray_StaticBool',
  9031. LinesToStr([ // statements
  9032. 'this.Arr = rtl.arraySetLength(null,false,2);',
  9033. 'this.Arr2 = rtl.arraySetLength(null,false,1);',
  9034. 'this.Arr3 = [true, false];',
  9035. 'this.b = false;'
  9036. ]),
  9037. LinesToStr([ // $mod.$main
  9038. '$mod.b = false;',
  9039. '$mod.b = true;',
  9040. '$mod.Arr[1] = false;',
  9041. '$mod.Arr[0] = $mod.Arr[+$mod.b] || $mod.Arr[1];',
  9042. '$mod.Arr[+$mod.b] = true;',
  9043. '$mod.Arr[+$mod.Arr[+$mod.b]] = $mod.Arr[1];',
  9044. '$mod.b = $mod.Arr[0] === $mod.Arr[1];',
  9045. '$mod.b = true;',
  9046. '$mod.b = true;',
  9047. '$mod.Arr2[0] = true;',
  9048. '$mod.Arr2[0] = $mod.Arr2[0] && $mod.Arr2[1-$mod.b];',
  9049. '$mod.Arr2[1-$mod.b] = false;',
  9050. '']));
  9051. end;
  9052. procedure TTestModule.TestArray_StaticChar;
  9053. begin
  9054. StartProgram(false);
  9055. Add([
  9056. 'type',
  9057. ' TChars = array[char] of char;',
  9058. ' TChars2 = array[''a''..''z''] of char;',
  9059. 'var',
  9060. ' Arr: TChars;',
  9061. ' Arr2: TChars2;',
  9062. ' Arr3: array[2..4] of char = (''p'',''a'',''s'');',
  9063. ' Arr4: array[11..13] of char = ''pas'';',
  9064. ' Arr5: array[21..22] of char = ''äö'';',
  9065. ' Arr6: array[31..32] of char = ''ä''+''ö'';',
  9066. ' c: char;',
  9067. ' b: boolean;',
  9068. 'begin',
  9069. ' c:=low(arr);',
  9070. ' c:=high(arr);',
  9071. ' arr[''B'']:=''a'';',
  9072. ' arr[''D'']:=arr[c];',
  9073. ' arr[c]:=arr[''d''];',
  9074. ' arr[arr[c]]:=arr[high(arr)];',
  9075. ' b:=arr[low(arr)]=arr[''e''];',
  9076. ' c:=low(arr2);',
  9077. ' c:=high(arr2);',
  9078. ' arr2[''b'']:=''f'';',
  9079. ' arr2[''a'']:=arr2[c];',
  9080. ' arr2[c]:=arr2[''g''];']);
  9081. ConvertProgram;
  9082. CheckSource('TestArray_StaticChar',
  9083. LinesToStr([ // statements
  9084. 'this.Arr = rtl.arraySetLength(null, "", 65536);',
  9085. 'this.Arr2 = rtl.arraySetLength(null, "", 26);',
  9086. 'this.Arr3 = ["p", "a", "s"];',
  9087. 'this.Arr4 = ["p", "a", "s"];',
  9088. 'this.Arr5 = ["ä", "ö"];',
  9089. 'this.Arr6 = ["ä", "ö"];',
  9090. 'this.c = "";',
  9091. 'this.b = false;',
  9092. '']),
  9093. LinesToStr([ // $mod.$main
  9094. '$mod.c = "\x00";',
  9095. '$mod.c = "\uFFFF";',
  9096. '$mod.Arr[66] = "a";',
  9097. '$mod.Arr[68] = $mod.Arr[$mod.c.charCodeAt()];',
  9098. '$mod.Arr[$mod.c.charCodeAt()] = $mod.Arr[100];',
  9099. '$mod.Arr[$mod.Arr[$mod.c.charCodeAt()].charCodeAt()] = $mod.Arr[65535];',
  9100. '$mod.b = $mod.Arr[0] === $mod.Arr[101];',
  9101. '$mod.c = "a";',
  9102. '$mod.c = "z";',
  9103. '$mod.Arr2[1] = "f";',
  9104. '$mod.Arr2[0] = $mod.Arr2[$mod.c.charCodeAt() - 97];',
  9105. '$mod.Arr2[$mod.c.charCodeAt() - 97] = $mod.Arr2[6];',
  9106. '']));
  9107. end;
  9108. procedure TTestModule.TestArray_StaticMultiDim;
  9109. begin
  9110. StartProgram(false);
  9111. Add([
  9112. 'type',
  9113. ' TArrayInt = array[1..3] of longint;',
  9114. ' TArrayArrayInt = array[5..6] of TArrayInt;',
  9115. 'var',
  9116. ' Arr: TArrayInt;',
  9117. ' Arr2: TArrayArrayInt;',
  9118. ' Arr3: array[boolean] of TArrayInt = ((11,12,13),(21,22,23));',
  9119. ' i: longint;',
  9120. 'begin',
  9121. ' i:=low(arr);',
  9122. ' i:=low(arr2);',
  9123. ' i:=low(arr2[5]);',
  9124. ' i:=high(arr);',
  9125. ' i:=high(arr2);',
  9126. ' i:=high(arr2[6]);',
  9127. ' arr2[5]:=arr;',
  9128. ' arr2[6][2]:=i;',
  9129. ' i:=arr2[6][3];',
  9130. ' arr2[6,3]:=i;',
  9131. ' i:=arr2[5,2];',
  9132. ' arr2:=arr2;',// clone multi dim static array
  9133. ' arr3:=arr3;',// clone anonymous multi dim static array
  9134. '']);
  9135. ConvertProgram;
  9136. CheckSource('TestArray_StaticMultiDim',
  9137. LinesToStr([ // statements
  9138. 'this.TArrayArrayInt$clone = function (a) {',
  9139. ' var r = [];',
  9140. ' for (var i = 0; i < 2; i++) r.push(a[i].slice(0));',
  9141. ' return r;',
  9142. '};',
  9143. 'this.Arr = rtl.arraySetLength(null, 0, 3);',
  9144. 'this.Arr2 = rtl.arraySetLength(null, 0, 2, 3);',
  9145. 'this.Arr3$a$clone = function (a) {',
  9146. ' var r = [];',
  9147. ' for (var i = 0; i < 2; i++) r.push(a[i].slice(0));',
  9148. ' return r;',
  9149. '};',
  9150. 'this.Arr3 = [[11, 12, 13], [21, 22, 23]];',
  9151. 'this.i = 0;'
  9152. ]),
  9153. LinesToStr([ // $mod.$main
  9154. '$mod.i = 1;',
  9155. '$mod.i = 5;',
  9156. '$mod.i = 1;',
  9157. '$mod.i = 3;',
  9158. '$mod.i = 6;',
  9159. '$mod.i = 3;',
  9160. '$mod.Arr2[0] = $mod.Arr.slice(0);',
  9161. '$mod.Arr2[1][1] = $mod.i;',
  9162. '$mod.i = $mod.Arr2[1][2];',
  9163. '$mod.Arr2[1][2] = $mod.i;',
  9164. '$mod.i = $mod.Arr2[0][1];',
  9165. '$mod.Arr2 = $mod.TArrayArrayInt$clone($mod.Arr2);',
  9166. '$mod.Arr3 = $mod.Arr3$a$clone($mod.Arr3);',
  9167. '']));
  9168. end;
  9169. procedure TTestModule.TestArray_StaticInFunction;
  9170. begin
  9171. StartProgram(false);
  9172. Add([
  9173. 'const TArrayInt = 3;',
  9174. 'const TArrayArrayInt = 4;',
  9175. 'procedure DoIt;',
  9176. 'type',
  9177. ' TArrayInt = array[1..3] of longint;',
  9178. ' TArrayArrayInt = array[5..6] of TArrayInt;',
  9179. 'var',
  9180. ' Arr: TArrayInt;',
  9181. ' Arr2: TArrayArrayInt;',
  9182. ' Arr3: array[boolean] of TArrayInt = ((11,12,13),(21,22,23));',
  9183. ' i: longint;',
  9184. 'begin',
  9185. ' arr2[5]:=arr;',
  9186. ' arr2:=arr2;',// clone multi dim static array
  9187. ' arr3:=arr3;',// clone multi dim anonymous static array
  9188. 'end;',
  9189. 'begin',
  9190. '']);
  9191. ConvertProgram;
  9192. CheckSource('TestArray_StaticInFunction',
  9193. LinesToStr([ // statements
  9194. 'this.TArrayInt = 3;',
  9195. 'this.TArrayArrayInt = 4;',
  9196. 'var TArrayArrayInt$1$clone = function (a) {',
  9197. ' var r = [];',
  9198. ' for (var i = 0; i < 2; i++) r.push(a[i].slice(0));',
  9199. ' return r;',
  9200. '};',
  9201. 'var Arr3$a$clone = function (a) {',
  9202. ' var r = [];',
  9203. ' for (var i = 0; i < 2; i++) r.push(a[i].slice(0));',
  9204. ' return r;',
  9205. '};',
  9206. 'this.DoIt = function () {',
  9207. ' var Arr = rtl.arraySetLength(null, 0, 3);',
  9208. ' var Arr2 = rtl.arraySetLength(null, 0, 2, 3);',
  9209. ' var Arr3 = [[11, 12, 13], [21, 22, 23]];',
  9210. ' var i = 0;',
  9211. ' Arr2[0] = Arr.slice(0);',
  9212. ' Arr2 = TArrayArrayInt$1$clone(Arr2);',
  9213. ' Arr3 = Arr3$a$clone(Arr3);',
  9214. '};',
  9215. '']),
  9216. LinesToStr([ // $mod.$main
  9217. '']));
  9218. end;
  9219. procedure TTestModule.TestArray_StaticMultiDimEqualNotImplemented;
  9220. begin
  9221. StartProgram(false);
  9222. Add([
  9223. 'type',
  9224. ' TArrayInt = array[1..3,1..2] of longint;',
  9225. 'var',
  9226. ' a,b: TArrayInt;',
  9227. 'begin',
  9228. ' if a=b then ;',
  9229. '']);
  9230. SetExpectedPasResolverError('compare static array is not supported',
  9231. nXIsNotSupported);
  9232. ConvertProgram;
  9233. end;
  9234. procedure TTestModule.TestArrayOfRecord;
  9235. begin
  9236. StartProgram(false);
  9237. Add([
  9238. 'type',
  9239. ' TRec = record',
  9240. ' Int: longint;',
  9241. ' end;',
  9242. ' TArrayRec = array of TRec;',
  9243. 'procedure DoIt(vd: TRec; const vc: TRec; var vv: TRec);',
  9244. 'begin',
  9245. 'end;',
  9246. 'var',
  9247. ' Arr: TArrayRec;',
  9248. ' r: TRec;',
  9249. ' i: longint;',
  9250. 'begin',
  9251. ' SetLength(arr,3);',
  9252. ' arr[0].int:=4;',
  9253. ' arr[1].int:=length(arr)+arr[2].int;',
  9254. ' arr[arr[i].int].int:=arr[5].int;',
  9255. ' arr[7]:=r;',
  9256. ' r:=arr[8];',
  9257. ' i:=low(arr);',
  9258. ' i:=high(arr);',
  9259. ' DoIt(Arr[9],Arr[10],Arr[11]);']);
  9260. ConvertProgram;
  9261. CheckSource('TestArrayOfRecord',
  9262. LinesToStr([ // statements
  9263. 'rtl.recNewT(this, "TRec", function () {',
  9264. ' this.Int = 0;',
  9265. ' this.$eq = function (b) {',
  9266. ' return this.Int === b.Int;',
  9267. ' };',
  9268. ' this.$assign = function (s) {',
  9269. ' this.Int = s.Int;',
  9270. ' return this;',
  9271. ' };',
  9272. '});',
  9273. 'this.DoIt = function (vd, vc, vv) {',
  9274. '};',
  9275. 'this.Arr = [];',
  9276. 'this.r = this.TRec.$new();',
  9277. 'this.i = 0;'
  9278. ]),
  9279. LinesToStr([ // $mod.$main
  9280. '$mod.Arr = rtl.arraySetLength($mod.Arr,$mod.TRec,3);',
  9281. '$mod.Arr[0].Int = 4;',
  9282. '$mod.Arr[1].Int = rtl.length($mod.Arr)+$mod.Arr[2].Int;',
  9283. '$mod.Arr[$mod.Arr[$mod.i].Int].Int = $mod.Arr[5].Int;',
  9284. '$mod.Arr[7].$assign($mod.r);',
  9285. '$mod.r.$assign($mod.Arr[8]);',
  9286. '$mod.i = 0;',
  9287. '$mod.i = rtl.length($mod.Arr)-1;',
  9288. '$mod.DoIt($mod.TRec.$clone($mod.Arr[9]), $mod.Arr[10], $mod.Arr[11]);',
  9289. '']));
  9290. end;
  9291. procedure TTestModule.TestArray_StaticRecord;
  9292. begin
  9293. StartProgram(false);
  9294. Add([
  9295. 'type',
  9296. ' TRec = record',
  9297. ' Int: longint;',
  9298. ' end;',
  9299. ' TArrayRec = array[1..2] of TRec;',
  9300. 'var',
  9301. ' Arr: TArrayRec;',
  9302. 'begin',
  9303. ' arr[1].int:=length(arr)+low(arr)+high(arr);',
  9304. '']);
  9305. ConvertProgram;
  9306. CheckSource('TestArray_StaticRecord',
  9307. LinesToStr([ // statements
  9308. 'rtl.recNewT(this, "TRec", function () {',
  9309. ' this.Int = 0;',
  9310. ' this.$eq = function (b) {',
  9311. ' return this.Int === b.Int;',
  9312. ' };',
  9313. ' this.$assign = function (s) {',
  9314. ' this.Int = s.Int;',
  9315. ' return this;',
  9316. ' };',
  9317. '});',
  9318. 'this.TArrayRec$clone = function (a) {',
  9319. ' var r = [];',
  9320. ' for (var i = 0; i < 2; i++) r.push($mod.TRec.$clone(a[i]));',
  9321. ' return r;',
  9322. '};',
  9323. 'this.Arr = rtl.arraySetLength(null, this.TRec, 2);',
  9324. '']),
  9325. LinesToStr([ // $mod.$main
  9326. '$mod.Arr[0].Int = 2 + 1 + 2;']));
  9327. end;
  9328. procedure TTestModule.TestArrayOfSet;
  9329. begin
  9330. StartProgram(false);
  9331. Add([
  9332. 'type',
  9333. ' TFlag = (big,small);',
  9334. ' TSetOfFlag = set of tflag;',
  9335. ' TArrayFlag = array of TSetOfFlag;',
  9336. 'procedure DoIt(const a: Tarrayflag);',
  9337. 'begin',
  9338. 'end;',
  9339. 'var',
  9340. ' f: TFlag;',
  9341. ' s: TSetOfFlag;',
  9342. ' Arr: TArrayFlag;',
  9343. ' i: longint;',
  9344. 'begin',
  9345. ' SetLength(arr,3);',
  9346. ' arr[0]:=s;',
  9347. ' arr[1]:=[big];',
  9348. ' arr[2]:=[big]+s;',
  9349. ' arr[3]:=s+[big];',
  9350. ' arr[4]:=arr[5];',
  9351. ' s:=arr[6];',
  9352. ' i:=low(arr);',
  9353. ' i:=high(arr);',
  9354. ' DoIt(arr);',
  9355. ' DoIt([s]);',
  9356. ' DoIt([[],s]);',
  9357. ' DoIt([s,[]]);',
  9358. '']);
  9359. ConvertProgram;
  9360. CheckSource('TestArrayOfSet',
  9361. LinesToStr([ // statements
  9362. 'this.TFlag = {',
  9363. ' "0": "big",',
  9364. ' big: 0,',
  9365. ' "1": "small",',
  9366. ' small: 1',
  9367. '};',
  9368. 'this.DoIt = function (a) {',
  9369. '};',
  9370. 'this.f = 0;',
  9371. 'this.s = {};',
  9372. 'this.Arr = [];',
  9373. 'this.i = 0;',
  9374. '']),
  9375. LinesToStr([ // $mod.$main
  9376. '$mod.Arr = rtl.arraySetLength($mod.Arr, {}, 3);',
  9377. '$mod.Arr[0] = rtl.refSet($mod.s);',
  9378. '$mod.Arr[1] = rtl.createSet($mod.TFlag.big);',
  9379. '$mod.Arr[2] = rtl.unionSet(rtl.createSet($mod.TFlag.big), $mod.s);',
  9380. '$mod.Arr[3] = rtl.unionSet($mod.s, rtl.createSet($mod.TFlag.big));',
  9381. '$mod.Arr[4] = rtl.refSet($mod.Arr[5]);',
  9382. '$mod.s = rtl.refSet($mod.Arr[6]);',
  9383. '$mod.i = 0;',
  9384. '$mod.i = rtl.length($mod.Arr) - 1;',
  9385. '$mod.DoIt($mod.Arr);',
  9386. '$mod.DoIt([rtl.refSet($mod.s)]);',
  9387. '$mod.DoIt([{}, rtl.refSet($mod.s)]);',
  9388. '$mod.DoIt([rtl.refSet($mod.s), {}]);',
  9389. '']));
  9390. end;
  9391. procedure TTestModule.TestArray_DynAsParam;
  9392. begin
  9393. StartProgram(false);
  9394. Add([
  9395. 'type integer = longint;',
  9396. 'type TArrInt = array of integer;',
  9397. 'procedure DoIt(vG: TArrInt; const vH: TArrInt; var vI: TArrInt);',
  9398. 'var vJ: TArrInt;',
  9399. 'begin',
  9400. ' vg:=vg;',
  9401. ' vj:=vh;',
  9402. ' vi:=vi;',
  9403. ' doit(vg,vg,vg);',
  9404. ' doit(vh,vh,vj);',
  9405. ' doit(vi,vi,vi);',
  9406. ' doit(vj,vj,vj);',
  9407. 'end;',
  9408. 'var i: TArrInt;',
  9409. 'begin',
  9410. ' doit(i,i,i);']);
  9411. ConvertProgram;
  9412. CheckSource('TestArray_DynAsParams',
  9413. LinesToStr([ // statements
  9414. 'this.DoIt = function (vG,vH,vI) {',
  9415. ' var vJ = [];',
  9416. ' vG = rtl.arrayRef(vG);',
  9417. ' vJ = rtl.arrayRef(vH);',
  9418. ' vI.set(rtl.arrayRef(vI.get()));',
  9419. ' $mod.DoIt(rtl.arrayRef(vG), vG, {',
  9420. ' get: function () {',
  9421. ' return vG;',
  9422. ' },',
  9423. ' set: function (v) {',
  9424. ' vG = v;',
  9425. ' }',
  9426. ' });',
  9427. ' $mod.DoIt(rtl.arrayRef(vH), vH, {',
  9428. ' get: function () {',
  9429. ' return vJ;',
  9430. ' },',
  9431. ' set: function (v) {',
  9432. ' vJ = v;',
  9433. ' }',
  9434. ' });',
  9435. ' $mod.DoIt(rtl.arrayRef(vI.get()), vI.get(), vI);',
  9436. ' $mod.DoIt(rtl.arrayRef(vJ), vJ, {',
  9437. ' get: function () {',
  9438. ' return vJ;',
  9439. ' },',
  9440. ' set: function (v) {',
  9441. ' vJ = v;',
  9442. ' }',
  9443. ' });',
  9444. '};',
  9445. 'this.i = [];'
  9446. ]),
  9447. LinesToStr([
  9448. '$mod.DoIt(rtl.arrayRef($mod.i),$mod.i,{',
  9449. ' p: $mod,',
  9450. ' get: function () {',
  9451. ' return this.p.i;',
  9452. ' },',
  9453. ' set: function (v) {',
  9454. ' this.p.i = v;',
  9455. ' }',
  9456. '});'
  9457. ]));
  9458. end;
  9459. procedure TTestModule.TestArray_StaticAsParam;
  9460. begin
  9461. StartProgram(false);
  9462. Add([
  9463. 'type integer = longint;',
  9464. 'type TArrInt = array[1..2] of integer;',
  9465. 'procedure DoIt(vG: TArrInt; const vH: TArrInt; var vI: TArrInt);',
  9466. 'var vJ: TArrInt;',
  9467. 'begin',
  9468. ' vg:=vg;',
  9469. ' vj:=vh;',
  9470. ' vi:=vi;',
  9471. ' doit(vg,vg,vg);',
  9472. ' doit(vh,vh,vj);',
  9473. ' doit(vi,vi,vi);',
  9474. ' doit(vj,vj,vj);',
  9475. 'end;',
  9476. 'var i: TArrInt;',
  9477. 'begin',
  9478. ' doit(i,i,i);']);
  9479. ConvertProgram;
  9480. CheckSource('TestArray_StaticAsParams',
  9481. LinesToStr([ // statements
  9482. 'this.DoIt = function (vG,vH,vI) {',
  9483. ' var vJ = rtl.arraySetLength(null, 0, 2);',
  9484. ' vG = vG.slice(0);',
  9485. ' vJ = vH.slice(0);',
  9486. ' vI.set(vI.get().slice(0));',
  9487. ' $mod.DoIt(vG.slice(0), vG, {',
  9488. ' get: function () {',
  9489. ' return vG;',
  9490. ' },',
  9491. ' set: function (v) {',
  9492. ' vG = v;',
  9493. ' }',
  9494. ' });',
  9495. ' $mod.DoIt(vH.slice(0), vH, {',
  9496. ' get: function () {',
  9497. ' return vJ;',
  9498. ' },',
  9499. ' set: function (v) {',
  9500. ' vJ = v;',
  9501. ' }',
  9502. ' });',
  9503. ' $mod.DoIt(vI.get().slice(0), vI.get(), vI);',
  9504. ' $mod.DoIt(vJ.slice(0), vJ, {',
  9505. ' get: function () {',
  9506. ' return vJ;',
  9507. ' },',
  9508. ' set: function (v) {',
  9509. ' vJ = v;',
  9510. ' }',
  9511. ' });',
  9512. '};',
  9513. 'this.i = rtl.arraySetLength(null, 0, 2);'
  9514. ]),
  9515. LinesToStr([
  9516. '$mod.DoIt($mod.i.slice(0),$mod.i,{',
  9517. ' p: $mod,',
  9518. ' get: function () {',
  9519. ' return this.p.i;',
  9520. ' },',
  9521. ' set: function (v) {',
  9522. ' this.p.i = v;',
  9523. ' }',
  9524. '});'
  9525. ]));
  9526. end;
  9527. procedure TTestModule.TestArrayElement_AsParams;
  9528. begin
  9529. StartProgram(false);
  9530. Add('type integer = longint;');
  9531. Add('type TArrayInt = array of integer;');
  9532. Add('procedure DoIt(vG: Integer; const vH: Integer; var vI: Integer);');
  9533. Add('var vJ: tarrayint;');
  9534. Add('begin');
  9535. Add(' vi:=vi;');
  9536. Add(' doit(vi,vi,vi);');
  9537. Add(' doit(vj[1+1],vj[1+2],vj[1+3]);');
  9538. Add('end;');
  9539. Add('var a: TArrayInt;');
  9540. Add('begin');
  9541. Add(' doit(a[1+4],a[1+5],a[1+6]);');
  9542. ConvertProgram;
  9543. CheckSource('TestArrayElement_AsParams',
  9544. LinesToStr([ // statements
  9545. 'this.DoIt = function (vG,vH,vI) {',
  9546. ' var vJ = [];',
  9547. ' vI.set(vI.get());',
  9548. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  9549. ' $mod.DoIt(vJ[1+1], vJ[1+2], {',
  9550. ' a:1+3,',
  9551. ' p:vJ,',
  9552. ' get: function () {',
  9553. ' return this.p[this.a];',
  9554. ' },',
  9555. ' set: function (v) {',
  9556. ' this.p[this.a] = v;',
  9557. ' }',
  9558. ' });',
  9559. '};',
  9560. 'this.a = [];'
  9561. ]),
  9562. LinesToStr([
  9563. '$mod.DoIt($mod.a[1+4],$mod.a[1+5],{',
  9564. ' a: 1+6,',
  9565. ' p: $mod.a,',
  9566. ' get: function () {',
  9567. ' return this.p[this.a];',
  9568. ' },',
  9569. ' set: function (v) {',
  9570. ' this.p[this.a] = v;',
  9571. ' }',
  9572. '});'
  9573. ]));
  9574. end;
  9575. procedure TTestModule.TestArrayElementFromFuncResult_AsParams;
  9576. begin
  9577. StartProgram(false);
  9578. Add('type Integer = longint;');
  9579. Add('type TArrayInt = array of integer;');
  9580. Add('function GetArr(vB: integer = 0): tarrayint;');
  9581. Add('begin');
  9582. Add('end;');
  9583. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  9584. Add('begin');
  9585. Add('end;');
  9586. Add('begin');
  9587. Add(' doit(getarr[1+1],getarr[1+2],getarr[1+3]);');
  9588. Add(' doit(getarr()[2+1],getarr()[2+2],getarr()[2+3]);');
  9589. Add(' doit(getarr(7)[3+1],getarr(8)[3+2],getarr(9)[3+3]);');
  9590. ConvertProgram;
  9591. CheckSource('TestArrayElementFromFuncResult_AsParams',
  9592. LinesToStr([ // statements
  9593. 'this.GetArr = function (vB) {',
  9594. ' var Result = [];',
  9595. ' return Result;',
  9596. '};',
  9597. 'this.DoIt = function (vG,vH,vI) {',
  9598. '};'
  9599. ]),
  9600. LinesToStr([
  9601. '$mod.DoIt($mod.GetArr(0)[1+1],$mod.GetArr(0)[1+2],{',
  9602. ' a: 1+3,',
  9603. ' p: $mod.GetArr(0),',
  9604. ' get: function () {',
  9605. ' return this.p[this.a];',
  9606. ' },',
  9607. ' set: function (v) {',
  9608. ' this.p[this.a] = v;',
  9609. ' }',
  9610. '});',
  9611. '$mod.DoIt($mod.GetArr(0)[2+1],$mod.GetArr(0)[2+2],{',
  9612. ' a: 2+3,',
  9613. ' p: $mod.GetArr(0),',
  9614. ' get: function () {',
  9615. ' return this.p[this.a];',
  9616. ' },',
  9617. ' set: function (v) {',
  9618. ' this.p[this.a] = v;',
  9619. ' }',
  9620. '});',
  9621. '$mod.DoIt($mod.GetArr(7)[3+1],$mod.GetArr(8)[3+2],{',
  9622. ' a: 3+3,',
  9623. ' p: $mod.GetArr(9),',
  9624. ' get: function () {',
  9625. ' return this.p[this.a];',
  9626. ' },',
  9627. ' set: function (v) {',
  9628. ' this.p[this.a] = v;',
  9629. ' }',
  9630. '});',
  9631. '']));
  9632. end;
  9633. procedure TTestModule.TestArrayEnumTypeRange;
  9634. begin
  9635. StartProgram(false);
  9636. Add([
  9637. 'type',
  9638. ' TEnum = (red,blue);',
  9639. ' TEnumArray = array[TEnum] of longint;',
  9640. 'var',
  9641. ' e: TEnum;',
  9642. ' i: longint;',
  9643. ' a: TEnumArray;',
  9644. ' numbers: TEnumArray = (1,2);',
  9645. ' names: array[TEnum] of string = (''red'',''blue'');',
  9646. 'begin',
  9647. ' e:=low(a);',
  9648. ' e:=high(a);',
  9649. ' i:=a[red];',
  9650. ' a[e]:=a[e];']);
  9651. ConvertProgram;
  9652. CheckSource('TestArrayEnumTypeRange',
  9653. LinesToStr([ // statements
  9654. ' this.TEnum = {',
  9655. ' "0": "red",',
  9656. ' red: 0,',
  9657. ' "1": "blue",',
  9658. ' blue: 1',
  9659. '};',
  9660. 'this.e = 0;',
  9661. 'this.i = 0;',
  9662. 'this.a = rtl.arraySetLength(null,0,2);',
  9663. 'this.numbers = [1, 2];',
  9664. 'this.names = ["red", "blue"];',
  9665. '']),
  9666. LinesToStr([ // $mod.$main
  9667. '$mod.e = $mod.TEnum.red;',
  9668. '$mod.e = $mod.TEnum.blue;',
  9669. '$mod.i = $mod.a[$mod.TEnum.red];',
  9670. '$mod.a[$mod.e] = $mod.a[$mod.e];',
  9671. '']));
  9672. end;
  9673. procedure TTestModule.TestArray_SetLengthOutArg;
  9674. begin
  9675. StartProgram(false);
  9676. Add([
  9677. 'type TArrInt = array of longint;',
  9678. 'procedure DoIt(out a: TArrInt);',
  9679. 'begin',
  9680. ' SetLength(a,2);',
  9681. 'end;',
  9682. 'begin',
  9683. '']);
  9684. ConvertProgram;
  9685. CheckSource('TestArray_SetLengthOutArg',
  9686. LinesToStr([ // statements
  9687. 'this.DoIt = function (a) {',
  9688. ' a.set(rtl.arraySetLength(a.get(), 0, 2));',
  9689. '};',
  9690. '']),
  9691. LinesToStr([
  9692. '']));
  9693. end;
  9694. procedure TTestModule.TestArray_SetLengthProperty;
  9695. begin
  9696. StartProgram(false);
  9697. Add('type');
  9698. Add(' TArrInt = array of longint;');
  9699. Add(' TObject = class');
  9700. Add(' function GetColors: TArrInt; external name ''GetColors'';');
  9701. Add(' procedure SetColors(const Value: TArrInt); external name ''SetColors'';');
  9702. Add(' property Colors: TArrInt read GetColors write SetColors;');
  9703. Add(' end;');
  9704. Add('var Obj: TObject;');
  9705. Add('begin');
  9706. Add(' SetLength(Obj.Colors,2);');
  9707. ConvertProgram;
  9708. CheckSource('TestArray_SetLengthProperty',
  9709. LinesToStr([ // statements
  9710. 'rtl.createClass(this, "TObject", null, function () {',
  9711. ' this.$init = function () {',
  9712. ' };',
  9713. ' this.$final = function () {',
  9714. ' };',
  9715. '});',
  9716. 'this.Obj = null;',
  9717. '']),
  9718. LinesToStr([
  9719. '$mod.Obj.SetColors(rtl.arraySetLength($mod.Obj.GetColors(), 0, 2));',
  9720. '']));
  9721. end;
  9722. procedure TTestModule.TestArray_SetLengthMultiDim;
  9723. begin
  9724. StartProgram(false);
  9725. Add([
  9726. 'type',
  9727. ' TArrArrInt = array of array of longint;',
  9728. ' TArrStaInt = array of array[1..2] of longint;',
  9729. 'var',
  9730. ' a: TArrArrInt;',
  9731. ' b: TArrStaInt;',
  9732. 'begin',
  9733. ' SetLength(a,2);',
  9734. ' SetLength(a,3,4);',
  9735. ' SetLength(b,5);',
  9736. '']);
  9737. ConvertProgram;
  9738. CheckSource('TestArray_SetLengthMultiDim',
  9739. LinesToStr([ // statements
  9740. 'this.a = [];',
  9741. 'this.b = [];',
  9742. '']),
  9743. LinesToStr([
  9744. '$mod.a = rtl.arraySetLength($mod.a, [], 2);',
  9745. '$mod.a = rtl.arraySetLength($mod.a, 0, 3, 4);',
  9746. '$mod.b = rtl.arraySetLength($mod.b, 0, 5, "s", 2);',
  9747. '']));
  9748. end;
  9749. procedure TTestModule.TestArray_SetLengthDynOfStatic;
  9750. begin
  9751. StartProgram(false);
  9752. Add([
  9753. 'type',
  9754. ' TStaArr1 = array[1..3] of boolean;',
  9755. //' TStaArr2 = array[5..6] of TStaArr1;',
  9756. ' TDynArr1StaArr1 = array of TStaArr1;',
  9757. //' TDynArr1StaArr2 = array of TStaArr2;',
  9758. ' TDynArr2StaArr1 = array of TDynArr1StaArr1;',
  9759. //' TDynArr2StaArr2 = array of TDynArr1StaArr2;',
  9760. 'var',
  9761. ' DynArr1StaArr1: TDynArr1StaArr1;',
  9762. //' DynArr1StaArr2: TDynArr1StaArr1;',
  9763. ' DynArr2StaArr1: TDynArr2StaArr1;',
  9764. //' DynArr2StaArr2: TDynArr2StaArr2;',
  9765. 'begin',
  9766. ' SetLength(DynArr1StaArr1,11);',
  9767. ' SetLength(DynArr2StaArr1,12);',
  9768. ' SetLength(DynArr2StaArr1[13],14);',
  9769. ' SetLength(DynArr2StaArr1,15,16);',
  9770. //' SetLength(DynArr1StaArr2,21);',
  9771. //' SetLength(DynArr2StaArr2,22);',
  9772. //' SetLength(DynArr2StaArr2[23],24);',
  9773. //' SetLength(DynArr2StaArr2,25,26);',
  9774. '']);
  9775. ConvertProgram;
  9776. CheckSource('TestArray_DynOfStatic',
  9777. LinesToStr([ // statements
  9778. 'this.DynArr1StaArr1 = [];',
  9779. 'this.DynArr2StaArr1 = [];',
  9780. '']),
  9781. LinesToStr([ // $mod.$main
  9782. '$mod.DynArr1StaArr1 = rtl.arraySetLength($mod.DynArr1StaArr1, false, 11, "s", 3);',
  9783. '$mod.DynArr2StaArr1 = rtl.arraySetLength($mod.DynArr2StaArr1, [], 12);',
  9784. '$mod.DynArr2StaArr1[13] = rtl.arraySetLength($mod.DynArr2StaArr1[13], false, 14, "s", 3);',
  9785. '$mod.DynArr2StaArr1 = rtl.arraySetLength(',
  9786. ' $mod.DynArr2StaArr1,',
  9787. ' false,',
  9788. ' 15,',
  9789. ' 16,',
  9790. ' "s",',
  9791. ' 3',
  9792. ');',
  9793. '']));
  9794. end;
  9795. procedure TTestModule.TestArray_OpenArrayOfString;
  9796. begin
  9797. StartProgram(false);
  9798. Add('procedure DoIt(const a: array of String);');
  9799. Add('var');
  9800. Add(' i: longint;');
  9801. Add(' s: string;');
  9802. Add('begin');
  9803. Add(' for i:=low(a) to high(a) do s:=a[length(a)-i-1];');
  9804. Add('end;');
  9805. Add('var s: string;');
  9806. Add('begin');
  9807. Add(' DoIt([]);');
  9808. Add(' DoIt([s,''foo'','''',s+s]);');
  9809. ConvertProgram;
  9810. CheckSource('TestArray_OpenArrayOfString',
  9811. LinesToStr([ // statements
  9812. 'this.DoIt = function (a) {',
  9813. ' var i = 0;',
  9814. ' var s = "";',
  9815. ' for (var $l = 0, $end = rtl.length(a) - 1; $l <= $end; $l++) {',
  9816. ' i = $l;',
  9817. ' s = a[rtl.length(a) - i - 1];',
  9818. ' };',
  9819. '};',
  9820. 'this.s = "";',
  9821. '']),
  9822. LinesToStr([
  9823. '$mod.DoIt([]);',
  9824. '$mod.DoIt([$mod.s, "foo", "", $mod.s + $mod.s]);',
  9825. '']));
  9826. end;
  9827. procedure TTestModule.TestArray_ArrayOfCharAssignString;
  9828. begin
  9829. StartProgram(false);
  9830. Add([
  9831. 'type TArr = array of char;',
  9832. 'var',
  9833. ' c: char;',
  9834. ' s: string;',
  9835. ' a: TArr;',
  9836. 'procedure Run(const a: array of char);',
  9837. 'begin',
  9838. ' Run(c);',
  9839. ' Run(s);',
  9840. 'end;',
  9841. 'begin',
  9842. ' a:=c;',
  9843. ' a:=s;',
  9844. ' a:=#13;',
  9845. ' a:=''Foo'';',
  9846. ' Run(c);',
  9847. ' Run(s);',
  9848. '']);
  9849. ConvertProgram;
  9850. CheckSource('TestArray_ArrayOfCharAssignString',
  9851. LinesToStr([ // statements
  9852. 'this.c = "";',
  9853. 'this.s = "";',
  9854. 'this.a = [];',
  9855. 'this.Run = function (a) {',
  9856. ' $mod.Run($mod.c.split(""));',
  9857. ' $mod.Run($mod.s.split(""));',
  9858. '};',
  9859. '']),
  9860. LinesToStr([
  9861. '$mod.a = $mod.c.split("");',
  9862. '$mod.a = $mod.s.split("");',
  9863. '$mod.a = "\r".split("");',
  9864. '$mod.a = "Foo".split("");',
  9865. '$mod.Run($mod.c.split(""));',
  9866. '$mod.Run($mod.s.split(""));',
  9867. '']));
  9868. end;
  9869. procedure TTestModule.TestArray_ConstRef;
  9870. begin
  9871. StartProgram(false);
  9872. Add([
  9873. 'type TArr = array of word;',
  9874. 'procedure Run(constref a: TArr);',
  9875. 'begin',
  9876. 'end;',
  9877. 'procedure Fly(a: TArr; var b: TArr; out c: TArr; const d: TArr; constref e: TArr);',
  9878. 'var l: TArr;',
  9879. 'begin',
  9880. ' Run(l);',
  9881. ' Run(a);',
  9882. ' Run(b);',
  9883. ' Run(c);',
  9884. ' Run(d);',
  9885. ' Run(e);',
  9886. 'end;',
  9887. 'begin',
  9888. '']);
  9889. ConvertProgram;
  9890. CheckResolverUnexpectedHints();
  9891. CheckSource('TestArray_ConstRef',
  9892. LinesToStr([ // statements
  9893. 'this.Run = function (a) {',
  9894. '};',
  9895. 'this.Fly = function (a, b, c, d, e) {',
  9896. ' var l = [];',
  9897. ' $mod.Run(l);',
  9898. ' $mod.Run(a);',
  9899. ' $mod.Run(b.get());',
  9900. ' $mod.Run(c.get());',
  9901. ' $mod.Run(d);',
  9902. ' $mod.Run(e);',
  9903. '};',
  9904. '']),
  9905. LinesToStr([
  9906. '']));
  9907. end;
  9908. procedure TTestModule.TestArray_Concat;
  9909. begin
  9910. StartProgram(false);
  9911. Add([
  9912. 'type',
  9913. ' integer = longint;',
  9914. ' TFlag = (big,small);',
  9915. ' TFlags = set of TFlag;',
  9916. ' TRec = record',
  9917. ' i: integer;',
  9918. ' end;',
  9919. ' TArrInt = array of integer;',
  9920. ' TArrRec = array of TRec;',
  9921. ' TArrFlag = array of TFlag;',
  9922. ' TArrSet = array of TFlags;',
  9923. ' TArrJSValue = array of jsvalue;',
  9924. 'var',
  9925. ' ArrInt: tarrint;',
  9926. ' ArrRec: tarrrec;',
  9927. ' ArrFlag: tarrflag;',
  9928. ' ArrSet: tarrset;',
  9929. ' ArrJSValue: tarrjsvalue;',
  9930. 'begin',
  9931. ' arrint:=concat(arrint);',
  9932. ' arrint:=concat(arrint,arrint);',
  9933. ' arrint:=concat(arrint,arrint,arrint);',
  9934. ' arrrec:=concat(arrrec);',
  9935. ' arrrec:=concat(arrrec,arrrec);',
  9936. ' arrrec:=concat(arrrec,arrrec,arrrec);',
  9937. ' arrset:=concat(arrset);',
  9938. ' arrset:=concat(arrset,arrset);',
  9939. ' arrset:=concat(arrset,arrset,arrset);',
  9940. ' arrjsvalue:=concat(arrjsvalue);',
  9941. ' arrjsvalue:=concat(arrjsvalue,arrjsvalue);',
  9942. ' arrjsvalue:=concat(arrjsvalue,arrjsvalue,arrjsvalue);',
  9943. ' arrint:=concat([1],arrint);',
  9944. ' arrflag:=concat([big]);',
  9945. ' arrflag:=concat([big],arrflag);',
  9946. ' arrflag:=concat(arrflag,[small]);',
  9947. '']);
  9948. ConvertProgram;
  9949. CheckSource('TestArray_Concat',
  9950. LinesToStr([ // statements
  9951. 'this.TFlag = {',
  9952. ' "0": "big",',
  9953. ' big: 0,',
  9954. ' "1": "small",',
  9955. ' small: 1',
  9956. '};',
  9957. 'rtl.recNewT(this, "TRec", function () {',
  9958. ' this.i = 0;',
  9959. ' this.$eq = function (b) {',
  9960. ' return this.i === b.i;',
  9961. ' };',
  9962. ' this.$assign = function (s) {',
  9963. ' this.i = s.i;',
  9964. ' return this;',
  9965. ' };',
  9966. '});',
  9967. 'this.ArrInt = [];',
  9968. 'this.ArrRec = [];',
  9969. 'this.ArrFlag = [];',
  9970. 'this.ArrSet = [];',
  9971. 'this.ArrJSValue = [];',
  9972. '']),
  9973. LinesToStr([ // $mod.$main
  9974. '$mod.ArrInt = rtl.arrayRef($mod.ArrInt);',
  9975. '$mod.ArrInt = rtl.arrayConcatN($mod.ArrInt, $mod.ArrInt);',
  9976. '$mod.ArrInt = rtl.arrayConcatN($mod.ArrInt, $mod.ArrInt, $mod.ArrInt);',
  9977. '$mod.ArrRec = rtl.arrayRef($mod.ArrRec);',
  9978. '$mod.ArrRec = rtl.arrayConcat($mod.TRec, $mod.ArrRec, $mod.ArrRec);',
  9979. '$mod.ArrRec = rtl.arrayConcat($mod.TRec, $mod.ArrRec, $mod.ArrRec, $mod.ArrRec);',
  9980. '$mod.ArrSet = rtl.arrayRef($mod.ArrSet);',
  9981. '$mod.ArrSet = rtl.arrayConcat("refSet", $mod.ArrSet, $mod.ArrSet);',
  9982. '$mod.ArrSet = rtl.arrayConcat("refSet", $mod.ArrSet, $mod.ArrSet, $mod.ArrSet);',
  9983. '$mod.ArrJSValue = rtl.arrayRef($mod.ArrJSValue);',
  9984. '$mod.ArrJSValue = rtl.arrayConcatN($mod.ArrJSValue, $mod.ArrJSValue);',
  9985. '$mod.ArrJSValue = rtl.arrayConcatN($mod.ArrJSValue, $mod.ArrJSValue, $mod.ArrJSValue);',
  9986. '$mod.ArrInt = rtl.arrayConcatN([1], $mod.ArrInt);',
  9987. '$mod.ArrFlag = [$mod.TFlag.big];',
  9988. '$mod.ArrFlag = rtl.arrayConcatN([$mod.TFlag.big], $mod.ArrFlag);',
  9989. '$mod.ArrFlag = rtl.arrayConcatN($mod.ArrFlag, [$mod.TFlag.small]);',
  9990. '']));
  9991. end;
  9992. procedure TTestModule.TestArray_Copy;
  9993. begin
  9994. StartProgram(false);
  9995. Add([
  9996. 'type',
  9997. ' integer = longint;',
  9998. ' TFlag = (big,small);',
  9999. ' TFlags = set of TFlag;',
  10000. ' TRec = record',
  10001. ' i: integer;',
  10002. ' end;',
  10003. ' TArrInt = array of integer;',
  10004. ' TArrRec = array of TRec;',
  10005. ' TArrSet = array of TFlags;',
  10006. ' TArrJSValue = array of jsvalue;',
  10007. 'var',
  10008. ' ArrInt: tarrint;',
  10009. ' ArrRec: tarrrec;',
  10010. ' ArrSet: tarrset;',
  10011. ' ArrJSValue: tarrjsvalue;',
  10012. 'begin',
  10013. ' arrint:=copy(arrint);',
  10014. ' arrint:=copy(arrint,2);',
  10015. ' arrint:=copy(arrint,3,4);',
  10016. ' arrint:=copy([1,1],1,2);',
  10017. ' arrrec:=copy(arrrec);',
  10018. ' arrrec:=copy(arrrec,5);',
  10019. ' arrrec:=copy(arrrec,6,7);',
  10020. ' arrset:=copy(arrset);',
  10021. ' arrset:=copy(arrset,8);',
  10022. ' arrset:=copy(arrset,9,10);',
  10023. ' arrjsvalue:=copy(arrjsvalue);',
  10024. ' arrjsvalue:=copy(arrjsvalue,11);',
  10025. ' arrjsvalue:=copy(arrjsvalue,12,13);',
  10026. ' ']);
  10027. ConvertProgram;
  10028. CheckSource('TestArray_Copy',
  10029. LinesToStr([ // statements
  10030. 'this.TFlag = {',
  10031. ' "0": "big",',
  10032. ' big: 0,',
  10033. ' "1": "small",',
  10034. ' small: 1',
  10035. '};',
  10036. 'rtl.recNewT(this, "TRec", function () {',
  10037. ' this.i = 0;',
  10038. ' this.$eq = function (b) {',
  10039. ' return this.i === b.i;',
  10040. ' };',
  10041. ' this.$assign = function (s) {',
  10042. ' this.i = s.i;',
  10043. ' return this;',
  10044. ' };',
  10045. '});',
  10046. 'this.ArrInt = [];',
  10047. 'this.ArrRec = [];',
  10048. 'this.ArrSet = [];',
  10049. 'this.ArrJSValue = [];',
  10050. '']),
  10051. LinesToStr([ // $mod.$main
  10052. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 0);',
  10053. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 2);',
  10054. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 3, 4);',
  10055. '$mod.ArrInt = rtl.arrayCopy(0, [1, 1], 1, 2);',
  10056. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 0);',
  10057. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 5);',
  10058. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 6, 7);',
  10059. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 0);',
  10060. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 8);',
  10061. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 9, 10);',
  10062. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 0);',
  10063. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 11);',
  10064. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 12, 13);',
  10065. '']));
  10066. end;
  10067. procedure TTestModule.TestArray_InsertDelete;
  10068. begin
  10069. StartProgram(false);
  10070. Add([
  10071. 'type',
  10072. ' integer = longint;',
  10073. ' TFlag = (big,small);',
  10074. ' TFlags = set of TFlag;',
  10075. ' TRec = record',
  10076. ' i: integer;',
  10077. ' end;',
  10078. ' TArrInt = array of integer;',
  10079. ' TArrRec = array of TRec;',
  10080. ' TArrSet = array of TFlags;',
  10081. ' TArrJSValue = array of jsvalue;',
  10082. ' TArrArrInt = array of TArrInt;',
  10083. 'var',
  10084. ' ArrInt: tarrint;',
  10085. ' ArrRec: tarrrec;',
  10086. ' ArrSet: tarrset;',
  10087. ' ArrJSValue: tarrjsvalue;',
  10088. ' ArrArrInt: TArrArrInt;',
  10089. 'begin',
  10090. ' Insert(1,arrint,2);',
  10091. ' Insert(arrint[3],arrint,4);',
  10092. ' Insert(arrrec[5],arrrec,6);',
  10093. ' Insert(arrset[7],arrset,7);',
  10094. ' Insert(arrjsvalue[8],arrjsvalue,9);',
  10095. ' Insert(10,arrjsvalue,11);',
  10096. ' Insert([23],arrarrint,22);',
  10097. ' Delete(arrint,12,13);',
  10098. ' Delete(arrrec,14,15);',
  10099. ' Delete(arrset,17,18);',
  10100. ' Delete(arrjsvalue,19,10);']);
  10101. ConvertProgram;
  10102. CheckSource('TestArray_InsertDelete',
  10103. LinesToStr([ // statements
  10104. 'this.TFlag = {',
  10105. ' "0": "big",',
  10106. ' big: 0,',
  10107. ' "1": "small",',
  10108. ' small: 1',
  10109. '};',
  10110. 'rtl.recNewT(this, "TRec", function () {',
  10111. ' this.i = 0;',
  10112. ' this.$eq = function (b) {',
  10113. ' return this.i === b.i;',
  10114. ' };',
  10115. ' this.$assign = function (s) {',
  10116. ' this.i = s.i;',
  10117. ' return this;',
  10118. ' };',
  10119. '});',
  10120. 'this.ArrInt = [];',
  10121. 'this.ArrRec = [];',
  10122. 'this.ArrSet = [];',
  10123. 'this.ArrJSValue = [];',
  10124. 'this.ArrArrInt = [];',
  10125. '']),
  10126. LinesToStr([ // $mod.$main
  10127. '$mod.ArrInt = rtl.arrayInsert(1, $mod.ArrInt, 2);',
  10128. '$mod.ArrInt = rtl.arrayInsert($mod.ArrInt[3], $mod.ArrInt, 4);',
  10129. '$mod.ArrRec = rtl.arrayInsert($mod.ArrRec[5], $mod.ArrRec, 6);',
  10130. '$mod.ArrSet = rtl.arrayInsert($mod.ArrSet[7], $mod.ArrSet, 7);',
  10131. '$mod.ArrJSValue = rtl.arrayInsert($mod.ArrJSValue[8], $mod.ArrJSValue, 9);',
  10132. '$mod.ArrJSValue = rtl.arrayInsert(10, $mod.ArrJSValue, 11);',
  10133. '$mod.ArrArrInt = rtl.arrayInsert([23], $mod.ArrArrInt, 22);',
  10134. '$mod.ArrInt.splice(12, 13);',
  10135. '$mod.ArrRec.splice(14, 15);',
  10136. '$mod.ArrSet.splice(17, 18);',
  10137. '$mod.ArrJSValue.splice(19, 10);',
  10138. '']));
  10139. end;
  10140. procedure TTestModule.TestArray_DynArrayConstObjFPC;
  10141. begin
  10142. Parser.Options:=Parser.Options+[po_cassignments];
  10143. StartProgram(false);
  10144. Add([
  10145. '{$modeswitch arrayoperators}',
  10146. 'type',
  10147. ' integer = longint;',
  10148. ' TArrInt = array of integer;',
  10149. ' TArrStr = array of string;',
  10150. 'const',
  10151. ' Ints: TArrInt = (1,2,3);',
  10152. ' Aliases: TarrStr = (''foo'',''b'');',
  10153. ' OneInt: TArrInt = (7);',
  10154. ' OneStr: array of integer = (7);',
  10155. ' Chars: array of char = ''aoc'';',
  10156. ' Names: array of string = (''a'',''foo'');',
  10157. ' NameCount = low(Names)+high(Names)+length(Names);',
  10158. 'var i: integer;',
  10159. 'begin',
  10160. ' Ints:=[];',
  10161. ' Ints:=[1,1];',
  10162. ' Ints:=[1]+[2];',
  10163. ' Ints:=[2];',
  10164. ' Ints:=[]+ints;',
  10165. ' Ints:=Ints+[];',
  10166. ' Ints:=Ints+OneInt;',
  10167. ' Ints:=Ints+[1,1];',
  10168. ' Ints:=[i,i]+Ints;',
  10169. ' Ints:=[1]+[i]+[3];',
  10170. '']);
  10171. ConvertProgram;
  10172. CheckSource('TestArray_DynArrayConstObjFPC',
  10173. LinesToStr([ // statements
  10174. 'this.Ints = [1, 2, 3];',
  10175. 'this.Aliases = ["foo", "b"];',
  10176. 'this.OneInt = [7];',
  10177. 'this.OneStr = [7];',
  10178. 'this.Chars = ["a", "o", "c"];',
  10179. 'this.Names = ["a", "foo"];',
  10180. 'this.NameCount = 0 + (rtl.length(this.Names) - 1) + rtl.length(this.Names);',
  10181. 'this.i = 0;',
  10182. '']),
  10183. LinesToStr([ // $mod.$main
  10184. '$mod.Ints = [];',
  10185. '$mod.Ints = [1, 1];',
  10186. '$mod.Ints = rtl.arrayConcatN([1], [2]);',
  10187. '$mod.Ints = [2];',
  10188. '$mod.Ints = rtl.arrayConcatN([], $mod.Ints);',
  10189. '$mod.Ints = rtl.arrayConcatN($mod.Ints, []);',
  10190. '$mod.Ints = rtl.arrayConcatN($mod.Ints, $mod.OneInt);',
  10191. '$mod.Ints = rtl.arrayConcatN($mod.Ints, [1, 1]);',
  10192. '$mod.Ints = rtl.arrayConcatN([$mod.i, $mod.i], $mod.Ints);',
  10193. '$mod.Ints = rtl.arrayConcatN(rtl.arrayConcatN([1], [$mod.i]), [3]);',
  10194. '']));
  10195. end;
  10196. procedure TTestModule.TestArray_DynArrayConstDelphi;
  10197. begin
  10198. StartProgram(false);
  10199. // Note: const c = [1,1]; defines a set!
  10200. Add([
  10201. '{$mode delphi}',
  10202. 'type',
  10203. ' integer = longint;',
  10204. ' TArrInt = array of integer;',
  10205. ' TArrStr = array of string;',
  10206. 'const',
  10207. ' Ints: TArrInt = [1,1,2];',
  10208. ' Aliases: TarrStr = [''foo'',''b''];',
  10209. ' OneInt: TArrInt = [7];',
  10210. ' OneStr: array of integer = [7]+[8];',
  10211. ' Chars: array of char = ''aoc'';',
  10212. ' Names: array of string = [''a'',''a''];',
  10213. ' NameCount = low(Names)+high(Names)+length(Names);',
  10214. 'begin',
  10215. '']);
  10216. ConvertProgram;
  10217. CheckSource('TestArray_DynArrayConstDelphi',
  10218. LinesToStr([ // statements
  10219. 'this.Ints = [1, 1, 2];',
  10220. 'this.Aliases = ["foo", "b"];',
  10221. 'this.OneInt = [7];',
  10222. 'this.OneStr = rtl.arrayConcatN([7],[8]);',
  10223. 'this.Chars = ["a", "o", "c"];',
  10224. 'this.Names = ["a", "a"];',
  10225. 'this.NameCount = 0 + (rtl.length(this.Names) - 1) + rtl.length(this.Names);',
  10226. '']),
  10227. LinesToStr([ // $mod.$main
  10228. '']));
  10229. end;
  10230. procedure TTestModule.TestArray_ArrayLitAsParam;
  10231. begin
  10232. StartProgram(false);
  10233. Add([
  10234. '{$modeswitch arrayoperators}',
  10235. 'type',
  10236. ' integer = longint;',
  10237. ' TArrInt = array of integer;',
  10238. ' TArrSet = array of (red,green,blue);',
  10239. 'procedure DoOpenInt(const a: array of integer); forward;',
  10240. 'procedure DoInt(const a: TArrInt);',
  10241. 'begin',
  10242. ' DoInt(a+[1]);',
  10243. ' DoInt([1]+a);',
  10244. ' DoOpenInt(a);',
  10245. ' DoOpenInt(a+[1]);',
  10246. ' DoOpenInt([1]+a);',
  10247. 'end;',
  10248. 'procedure DoOpenInt(const a: array of integer);',
  10249. 'begin',
  10250. ' DoOpenInt(a+[1]);',
  10251. ' DoOpenInt([1]+a);',
  10252. ' DoInt(a);',
  10253. ' DoInt(a+[1]);',
  10254. ' DoInt([1]+a);',
  10255. 'end;',
  10256. 'procedure DoSet(const a: TArrSet);',
  10257. 'begin',
  10258. ' DoSet(a+[red]);',
  10259. ' DoSet([blue]+a);',
  10260. 'end;',
  10261. 'var',
  10262. ' i: TArrInt;',
  10263. ' s: TArrSet;',
  10264. 'begin',
  10265. ' DoInt([1]);',
  10266. ' DoInt([1]+[2]);',
  10267. ' DoInt(i+[1]);',
  10268. ' DoInt([1]+i);',
  10269. ' DoOpenInt([1]);',
  10270. ' DoOpenInt([1]+[2]);',
  10271. ' DoOpenInt(i+[1]);',
  10272. ' DoOpenInt([1]+i);',
  10273. ' DoSet([red]);',
  10274. ' DoSet([blue]+[green]);',
  10275. ' DoSet(s+[blue]);',
  10276. ' DoSet([red]+s);',
  10277. '']);
  10278. ConvertProgram;
  10279. CheckSource('TestArray_ArrayLitAsParam',
  10280. LinesToStr([ // statements
  10281. 'this.TArrSet$a = {',
  10282. ' "0": "red",',
  10283. ' red: 0,',
  10284. ' "1": "green",',
  10285. ' green: 1,',
  10286. ' "2": "blue",',
  10287. ' blue: 2',
  10288. '};',
  10289. 'this.DoInt = function (a) {',
  10290. ' $mod.DoInt(rtl.arrayConcatN(a, [1]));',
  10291. ' $mod.DoInt(rtl.arrayConcatN([1], a));',
  10292. ' $mod.DoOpenInt(a);',
  10293. ' $mod.DoOpenInt(rtl.arrayConcatN(a, [1]));',
  10294. ' $mod.DoOpenInt(rtl.arrayConcatN([1], a));',
  10295. '};',
  10296. 'this.DoOpenInt = function (a) {',
  10297. ' $mod.DoOpenInt(rtl.arrayConcatN(a, [1]));',
  10298. ' $mod.DoOpenInt(rtl.arrayConcatN([1], a));',
  10299. ' $mod.DoInt(a);',
  10300. ' $mod.DoInt(rtl.arrayConcatN(a, [1]));',
  10301. ' $mod.DoInt(rtl.arrayConcatN([1], a));',
  10302. '};',
  10303. 'this.DoSet = function (a) {',
  10304. ' $mod.DoSet(rtl.arrayConcatN(a, [$mod.TArrSet$a.red]));',
  10305. ' $mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.blue], a));',
  10306. '};',
  10307. 'this.i = [];',
  10308. 'this.s = [];',
  10309. '']),
  10310. LinesToStr([ // $mod.$main
  10311. '$mod.DoInt([1]);',
  10312. '$mod.DoInt(rtl.arrayConcatN([1], [2]));',
  10313. '$mod.DoInt(rtl.arrayConcatN($mod.i, [1]));',
  10314. '$mod.DoInt(rtl.arrayConcatN([1], $mod.i));',
  10315. '$mod.DoOpenInt([1]);',
  10316. '$mod.DoOpenInt(rtl.arrayConcatN([1], [2]));',
  10317. '$mod.DoOpenInt(rtl.arrayConcatN($mod.i, [1]));',
  10318. '$mod.DoOpenInt(rtl.arrayConcatN([1], $mod.i));',
  10319. '$mod.DoSet([$mod.TArrSet$a.red]);',
  10320. '$mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.blue], [$mod.TArrSet$a.green]));',
  10321. '$mod.DoSet(rtl.arrayConcatN($mod.s, [$mod.TArrSet$a.blue]));',
  10322. '$mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.red], $mod.s));',
  10323. '']));
  10324. end;
  10325. procedure TTestModule.TestArray_ArrayLitMultiDimAsParam;
  10326. begin
  10327. StartProgram(false);
  10328. Add([
  10329. '{$modeswitch arrayoperators}',
  10330. 'type',
  10331. ' integer = longint;',
  10332. ' TArrInt = array of integer;',
  10333. ' TArrArrInt = array of TArrInt;',
  10334. 'procedure DoInt(const a: TArrArrInt);',
  10335. 'begin',
  10336. ' DoInt(a+[[1]]);',
  10337. ' DoInt([[1]]+a);',
  10338. ' DoInt(a);',
  10339. 'end;',
  10340. 'var',
  10341. ' i: TArrInt;',
  10342. ' a: TArrArrInt;',
  10343. 'begin',
  10344. ' a:=[[1]];',
  10345. ' a:=[i];',
  10346. ' a:=a+[i];',
  10347. ' a:=[i]+a;',
  10348. ' a:=[[1]+i];',
  10349. ' a:=[[1]+[2]];',
  10350. ' a:=[i+[2]];',
  10351. ' DoInt([[1]]);',
  10352. ' DoInt([[1]+[2],[3,4],[5]]);',
  10353. ' DoInt([i+[1]]+a);',
  10354. ' DoInt([i]+a);',
  10355. '']);
  10356. ConvertProgram;
  10357. CheckSource('TestArray_ArrayLitMultiDimAsParam',
  10358. LinesToStr([ // statements
  10359. 'this.DoInt = function (a) {',
  10360. ' $mod.DoInt(rtl.arrayConcatN(a, [[1]]));',
  10361. ' $mod.DoInt(rtl.arrayConcatN([[1]], a));',
  10362. ' $mod.DoInt(a);',
  10363. '};',
  10364. 'this.i = [];',
  10365. 'this.a = [];',
  10366. '']),
  10367. LinesToStr([ // $mod.$main
  10368. '$mod.a = [[1]];',
  10369. '$mod.a = [$mod.i];',
  10370. '$mod.a = rtl.arrayConcatN($mod.a, [$mod.i]);',
  10371. '$mod.a = rtl.arrayConcatN([$mod.i], $mod.a);',
  10372. '$mod.a = [rtl.arrayConcatN([1], $mod.i)];',
  10373. '$mod.a = [rtl.arrayConcatN([1], [2])];',
  10374. '$mod.a = [rtl.arrayConcatN($mod.i, [2])];',
  10375. '$mod.DoInt([[1]]);',
  10376. '$mod.DoInt([rtl.arrayConcatN([1], [2]), [3, 4], [5]]);',
  10377. '$mod.DoInt(rtl.arrayConcatN([rtl.arrayConcatN($mod.i, [1])], $mod.a));',
  10378. '$mod.DoInt(rtl.arrayConcatN([$mod.i], $mod.a));',
  10379. '']));
  10380. end;
  10381. procedure TTestModule.TestArray_ArrayLitStaticAsParam;
  10382. begin
  10383. StartProgram(false);
  10384. Add([
  10385. '{$modeswitch arrayoperators}',
  10386. 'type',
  10387. ' integer = longint;',
  10388. ' TArrInt = array[1..2] of integer;',
  10389. ' TArrArrInt = array of TArrInt;',
  10390. 'procedure DoInt(const a: TArrArrInt);',
  10391. 'begin',
  10392. ' DoInt(a+[[1,2]]);',
  10393. ' DoInt([[1,2]]+a);',
  10394. ' DoInt(a);',
  10395. 'end;',
  10396. 'var',
  10397. ' i: TArrInt;',
  10398. ' a: TArrArrInt;',
  10399. 'begin',
  10400. ' a:=[[1,1]];',
  10401. ' a:=[i];',
  10402. ' a:=a+[i];',
  10403. ' a:=[i]+a;',
  10404. ' DoInt([[1,1]]);',
  10405. ' DoInt([[1,2],[3,4]]);',
  10406. '']);
  10407. ConvertProgram;
  10408. CheckSource('TestArray_ArrayLitStaticAsParam',
  10409. LinesToStr([ // statements
  10410. 'this.DoInt = function (a) {',
  10411. ' $mod.DoInt(rtl.arrayConcatN(a, [[1, 2]]));',
  10412. ' $mod.DoInt(rtl.arrayConcatN([[1, 2]], a));',
  10413. ' $mod.DoInt(a);',
  10414. '};',
  10415. 'this.i = rtl.arraySetLength(null, 0, 2);',
  10416. 'this.a = [];',
  10417. '']),
  10418. LinesToStr([ // $mod.$main
  10419. '$mod.a = [[1, 1]];',
  10420. '$mod.a = [$mod.i.slice(0)];',
  10421. '$mod.a = rtl.arrayConcatN($mod.a, [$mod.i.slice(0)]);',
  10422. '$mod.a = rtl.arrayConcatN([$mod.i.slice(0)], $mod.a);',
  10423. '$mod.DoInt([[1, 1]]);',
  10424. '$mod.DoInt([[1, 2], [3, 4]]);',
  10425. '']));
  10426. end;
  10427. procedure TTestModule.TestArray_ForInArrOfString;
  10428. begin
  10429. StartProgram(false);
  10430. Add([
  10431. 'type',
  10432. 'type',
  10433. ' TMonthNameArray = array [1..12] of string;',
  10434. ' TMonthNames = TMonthNameArray;',
  10435. ' TObject = class',
  10436. ' private',
  10437. ' function GetLongMonthNames: TMonthNames; virtual; abstract;',
  10438. ' public',
  10439. ' Property LongMonthNames : TMonthNames Read GetLongMonthNames;',
  10440. ' end;',
  10441. 'var',
  10442. ' f: TObject;',
  10443. ' Month: string;',
  10444. ' Names: array of string = (''a'',''foo'',''bar'');',
  10445. ' i: longint;',
  10446. 'begin',
  10447. ' for Month in f.LongMonthNames do ;',
  10448. ' for Month in Names do ;',
  10449. ' for i:=low(Names) to high(Names) do ;',
  10450. '']);
  10451. ConvertProgram;
  10452. CheckSource('TestArray_ForInArrOfString',
  10453. LinesToStr([ // statements
  10454. 'rtl.createClass(this, "TObject", null, function () {',
  10455. ' this.$init = function () {',
  10456. ' };',
  10457. ' this.$final = function () {',
  10458. ' };',
  10459. '});',
  10460. 'this.f = null;',
  10461. 'this.Month = "";',
  10462. 'this.Names = ["a", "foo", "bar"];',
  10463. 'this.i = 0;',
  10464. '']),
  10465. LinesToStr([ // $mod.$main
  10466. 'for (var $in = $mod.f.GetLongMonthNames(), $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) $mod.Month = $in[$l];',
  10467. 'for (var $in1 = $mod.Names, $l1 = 0, $end1 = rtl.length($in1) - 1; $l1 <= $end1; $l1++) $mod.Month = $in1[$l1];',
  10468. 'for (var $l2 = 0, $end2 = rtl.length($mod.Names) - 1; $l2 <= $end2; $l2++) $mod.i = $l2;',
  10469. '']));
  10470. end;
  10471. procedure TTestModule.TestExternalClass_TypeCastArrayToExternalClass;
  10472. begin
  10473. StartProgram(false);
  10474. Add([
  10475. '{$modeswitch externalclass}',
  10476. 'type',
  10477. ' TJSObject = class external name ''Object''',
  10478. ' end;',
  10479. ' TJSArray = class external name ''Array''',
  10480. ' class function isArray(Value: JSValue) : boolean;',
  10481. ' function concat() : TJSArray; varargs;',
  10482. ' end;',
  10483. 'var',
  10484. ' aObj: TJSArray;',
  10485. ' a: array of longint;',
  10486. ' o: TJSObject;',
  10487. 'begin',
  10488. ' if TJSArray.isArray(65) then ;',
  10489. ' aObj:=TJSArray(a).concat(a);',
  10490. ' o:=TJSObject(a);']);
  10491. ConvertProgram;
  10492. CheckSource('TestExternalClass_TypeCastArrayToExternalClass',
  10493. LinesToStr([ // statements
  10494. 'this.aObj = null;',
  10495. 'this.a = [];',
  10496. 'this.o = null;',
  10497. '']),
  10498. LinesToStr([ // $mod.$main
  10499. 'if (Array.isArray(65)) ;',
  10500. '$mod.aObj = $mod.a.concat($mod.a);',
  10501. '$mod.o = $mod.a;',
  10502. '']));
  10503. end;
  10504. procedure TTestModule.TestExternalClass_TypeCastArrayFromExternalClass;
  10505. begin
  10506. StartProgram(false);
  10507. Add([
  10508. '{$modeswitch externalclass}',
  10509. 'type',
  10510. ' TArrStr = array of string;',
  10511. ' TJSArray = class external name ''Array''',
  10512. ' end;',
  10513. ' TJSObject = class external name ''Object''',
  10514. ' end;',
  10515. 'var',
  10516. ' aObj: TJSArray;',
  10517. ' a: TArrStr;',
  10518. ' jo: TJSObject;',
  10519. 'begin',
  10520. ' a:=TArrStr(aObj);',
  10521. ' TArrStr(aObj)[1]:=TArrStr(aObj)[2];',
  10522. ' a:=TarrStr(jo);',
  10523. '']);
  10524. ConvertProgram;
  10525. CheckSource('TestExternalClass_TypeCastArrayFromExternalClass',
  10526. LinesToStr([ // statements
  10527. 'this.aObj = null;',
  10528. 'this.a = [];',
  10529. 'this.jo = null;',
  10530. '']),
  10531. LinesToStr([ // $mod.$main
  10532. '$mod.a = $mod.aObj;',
  10533. '$mod.aObj[1] = $mod.aObj[2];',
  10534. '$mod.a = $mod.jo;',
  10535. '']));
  10536. end;
  10537. procedure TTestModule.TestArrayOfConst_TVarRec;
  10538. begin
  10539. StartProgram(true,[supTVarRec]);
  10540. Add([
  10541. 'procedure Say(args: array of const);',
  10542. 'var',
  10543. ' i: longint;',
  10544. ' v: TVarRec;',
  10545. 'begin',
  10546. ' for i:=low(args) to high(args) do begin',
  10547. ' v:=args[i];',
  10548. ' case v.vtype of',
  10549. ' vtInteger: if length(args)=args[i].vInteger then ;',
  10550. ' end;',
  10551. ' end;',
  10552. ' for v in args do ;',
  10553. ' args:=nil;',
  10554. ' SetLength(args,2);',
  10555. 'end;',
  10556. 'begin']);
  10557. ConvertProgram;
  10558. CheckSource('TestArrayOfConst_TVarRec',
  10559. LinesToStr([ // statements
  10560. 'this.Say = function (args) {',
  10561. ' var i = 0;',
  10562. ' var v = pas.system.TVarRec.$new();',
  10563. ' for (var $l = 0, $end = rtl.length(args) - 1; $l <= $end; $l++) {',
  10564. ' i = $l;',
  10565. ' v.$assign(args[i]);',
  10566. ' var $tmp = v.VType;',
  10567. ' if ($tmp === 0) if (rtl.length(args) === args[i].VJSValue) ;',
  10568. ' };',
  10569. ' for (var $in = args, $l1 = 0, $end1 = rtl.length($in) - 1; $l1 <= $end1; $l1++) v = $in[$l1];',
  10570. ' args = [];',
  10571. ' args = rtl.arraySetLength(args, pas.system.TVarRec, 2);',
  10572. '};',
  10573. '']),
  10574. LinesToStr([ // $mod.$main
  10575. ]));
  10576. end;
  10577. procedure TTestModule.TestArrayOfConst_PassBaseTypes;
  10578. begin
  10579. StartProgram(true,[supTVarRec]);
  10580. Add([
  10581. 'procedure Say(args: array of const);',
  10582. 'begin',
  10583. ' Say(args);',
  10584. 'end;',
  10585. 'var',
  10586. ' p: Pointer;',
  10587. ' j: jsvalue;',
  10588. ' c: currency;',
  10589. 'begin',
  10590. ' Say([]);',
  10591. ' Say([1]);',
  10592. ' Say([''c'',''foo'',nil,true,1.3,p,j,c]);',
  10593. '']);
  10594. ConvertProgram;
  10595. CheckSource('TestArrayOfConst_PassBaseTypes',
  10596. LinesToStr([ // statements
  10597. 'this.Say = function (args) {',
  10598. ' $mod.Say(args);',
  10599. '};',
  10600. 'this.p = null;',
  10601. 'this.j = undefined;',
  10602. 'this.c = 0;',
  10603. '']),
  10604. LinesToStr([ // $mod.$main
  10605. '$mod.Say([]);',
  10606. '$mod.Say(pas.system.VarRecs(0, 1));',
  10607. '$mod.Say(pas.system.VarRecs(',
  10608. ' 9,',
  10609. ' "c",',
  10610. ' 18,',
  10611. ' "foo",',
  10612. ' 5,',
  10613. ' null,',
  10614. ' 1,',
  10615. ' true,',
  10616. ' 3,',
  10617. ' 1.3,',
  10618. ' 5,',
  10619. ' $mod.p,',
  10620. ' 20,',
  10621. ' $mod.j,',
  10622. ' 12,',
  10623. ' $mod.c',
  10624. ' ));',
  10625. '']));
  10626. end;
  10627. procedure TTestModule.TestArrayOfConst_PassObj;
  10628. begin
  10629. StartProgram(true,[supTVarRec]);
  10630. Add([
  10631. '{$interfaces corba}',
  10632. 'type',
  10633. ' TObject = class',
  10634. ' end;',
  10635. ' TClass = class of TObject;',
  10636. ' IUnknown = interface',
  10637. ' end;',
  10638. 'procedure Say(args: array of const);',
  10639. 'begin',
  10640. 'end;',
  10641. 'var',
  10642. ' o: TObject;',
  10643. ' c: TClass;',
  10644. ' i: IUnknown;',
  10645. 'begin',
  10646. ' Say([o,c,TObject]);',
  10647. ' Say([nil,i]);',
  10648. '']);
  10649. ConvertProgram;
  10650. CheckSource('TestArrayOfConst_PassObj',
  10651. LinesToStr([ // statements
  10652. 'rtl.createClass(this, "TObject", null, function () {',
  10653. ' this.$init = function () {',
  10654. ' };',
  10655. ' this.$final = function () {',
  10656. ' };',
  10657. '});',
  10658. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  10659. 'this.Say = function (args) {',
  10660. '};',
  10661. 'this.o = null;',
  10662. 'this.c = null;',
  10663. 'this.i = null;',
  10664. '']),
  10665. LinesToStr([ // $mod.$main
  10666. '$mod.Say(pas.system.VarRecs(',
  10667. ' 7,',
  10668. ' $mod.o,',
  10669. ' 8,',
  10670. ' $mod.c,',
  10671. ' 8,',
  10672. ' $mod.TObject',
  10673. '));',
  10674. '$mod.Say(pas.system.VarRecs(5, null, 14, $mod.i));',
  10675. '']));
  10676. end;
  10677. procedure TTestModule.TestRecord_Empty;
  10678. begin
  10679. StartProgram(false);
  10680. Add([
  10681. 'type',
  10682. ' TRecA = record',
  10683. ' end;',
  10684. 'var a,b: TRecA;',
  10685. 'begin',
  10686. ' if a=b then ;']);
  10687. ConvertProgram;
  10688. CheckSource('TestRecord_Empty',
  10689. LinesToStr([ // statements
  10690. 'rtl.recNewT(this, "TRecA", function () {',
  10691. ' this.$eq = function (b) {',
  10692. ' return true;',
  10693. ' };',
  10694. ' this.$assign = function (s) {',
  10695. ' return this;',
  10696. ' };',
  10697. '});',
  10698. 'this.a = this.TRecA.$new();',
  10699. 'this.b = this.TRecA.$new();',
  10700. '']),
  10701. LinesToStr([ // $mod.$main
  10702. 'if ($mod.a.$eq($mod.b)) ;'
  10703. ]));
  10704. end;
  10705. procedure TTestModule.TestRecord_Var;
  10706. begin
  10707. StartProgram(false);
  10708. Add('type');
  10709. Add(' TRecA = record');
  10710. Add(' Bold: longint;');
  10711. Add(' end;');
  10712. Add('var Rec: TRecA;');
  10713. Add('begin');
  10714. Add(' rec.bold:=123');
  10715. ConvertProgram;
  10716. CheckSource('TestRecord_Var',
  10717. LinesToStr([ // statements
  10718. 'rtl.recNewT(this, "TRecA", function () {',
  10719. ' this.Bold = 0;',
  10720. ' this.$eq = function (b) {',
  10721. ' return this.Bold === b.Bold;',
  10722. ' };',
  10723. ' this.$assign = function (s) {',
  10724. ' this.Bold = s.Bold;',
  10725. ' return this;',
  10726. ' };',
  10727. '});',
  10728. 'this.Rec = this.TRecA.$new();',
  10729. '']),
  10730. LinesToStr([ // $mod.$main
  10731. '$mod.Rec.Bold = 123;'
  10732. ]));
  10733. end;
  10734. procedure TTestModule.TestRecord_VarExternal;
  10735. begin
  10736. StartProgram(false);
  10737. Add([
  10738. '{$modeswitch externalclass}',
  10739. 'type',
  10740. ' TRecA = record',
  10741. ' i: byte;',
  10742. ' length_: longint external name ''length'';',
  10743. ' end;',
  10744. 'var Rec: TRecA;',
  10745. 'begin',
  10746. ' rec.length_ := rec.length_',
  10747. '']);
  10748. ConvertProgram;
  10749. CheckSource('TestRecord_VarExternal',
  10750. LinesToStr([ // statements
  10751. 'rtl.recNewT(this, "TRecA", function () {',
  10752. ' this.i = 0;',
  10753. ' this.$eq = function (b) {',
  10754. ' return (this.i === b.i) && (this.length === b.length);',
  10755. ' };',
  10756. ' this.$assign = function (s) {',
  10757. ' this.i = s.i;',
  10758. ' this.length = s.length;',
  10759. ' return this;',
  10760. ' };',
  10761. '});',
  10762. 'this.Rec = this.TRecA.$new();',
  10763. '']),
  10764. LinesToStr([ // $mod.$main
  10765. '$mod.Rec.length = $mod.Rec.length;'
  10766. ]));
  10767. end;
  10768. procedure TTestModule.TestRecord_WithDo;
  10769. begin
  10770. StartProgram(false);
  10771. Add('type');
  10772. Add(' TRec = record');
  10773. Add(' vI: longint;');
  10774. Add(' end;');
  10775. Add('var');
  10776. Add(' Int: longint;');
  10777. Add(' r: TRec;');
  10778. Add('begin');
  10779. Add(' with r do');
  10780. Add(' int:=vi;');
  10781. Add(' with r do begin');
  10782. Add(' int:=vi;');
  10783. Add(' vi:=int;');
  10784. Add(' end;');
  10785. ConvertProgram;
  10786. CheckSource('TestWithRecordDo',
  10787. LinesToStr([ // statements
  10788. 'rtl.recNewT(this, "TRec", function () {',
  10789. ' this.vI = 0;',
  10790. ' this.$eq = function (b) {',
  10791. ' return this.vI === b.vI;',
  10792. ' };',
  10793. ' this.$assign = function (s) {',
  10794. ' this.vI = s.vI;',
  10795. ' return this;',
  10796. ' };',
  10797. '});',
  10798. 'this.Int = 0;',
  10799. 'this.r = this.TRec.$new();',
  10800. '']),
  10801. LinesToStr([ // $mod.$main
  10802. 'var $with = $mod.r;',
  10803. '$mod.Int = $with.vI;',
  10804. 'var $with1 = $mod.r;',
  10805. '$mod.Int = $with1.vI;',
  10806. '$with1.vI = $mod.Int;'
  10807. ]));
  10808. end;
  10809. procedure TTestModule.TestRecord_Assign;
  10810. begin
  10811. StartProgram(false);
  10812. Add([
  10813. 'type',
  10814. ' TEnum = (red,green);',
  10815. ' TEnums = set of TEnum;',
  10816. ' TSmallRec = record',
  10817. ' N: longint;',
  10818. ' end;',
  10819. ' TBigRec = record',
  10820. ' Int: longint;',
  10821. ' D: double;',
  10822. ' Arr: array of longint;',
  10823. ' Arr2: array[1..2] of longint;',
  10824. ' Small: TSmallRec;',
  10825. ' Enums: TEnums;',
  10826. ' end;',
  10827. 'var',
  10828. ' r, s: TBigRec;',
  10829. 'begin',
  10830. ' r:=s;',
  10831. ' r:=default(TBigRec);',
  10832. ' r:=default(s);',
  10833. '']);
  10834. ConvertProgram;
  10835. CheckSource('TestRecord_Assign',
  10836. LinesToStr([ // statements
  10837. 'this.TEnum = {',
  10838. ' "0": "red",',
  10839. ' red: 0,',
  10840. ' "1": "green",',
  10841. ' green: 1',
  10842. '};',
  10843. 'rtl.recNewT(this, "TSmallRec", function () {',
  10844. ' this.N = 0;',
  10845. ' this.$eq = function (b) {',
  10846. ' return this.N === b.N;',
  10847. ' };',
  10848. ' this.$assign = function (s) {',
  10849. ' this.N = s.N;',
  10850. ' return this;',
  10851. ' };',
  10852. '});',
  10853. 'rtl.recNewT(this, "TBigRec", function () {',
  10854. ' this.Int = 0;',
  10855. ' this.D = 0.0;',
  10856. ' this.$new = function () {',
  10857. ' var r = Object.create(this);',
  10858. ' r.Arr = [];',
  10859. ' r.Arr2 = rtl.arraySetLength(null, 0, 2);',
  10860. ' r.Small = $mod.TSmallRec.$new();',
  10861. ' r.Enums = {};',
  10862. ' return r;',
  10863. ' };',
  10864. ' this.$eq = function (b) {',
  10865. ' 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);',
  10866. ' };',
  10867. ' this.$assign = function (s) {',
  10868. ' this.Int = s.Int;',
  10869. ' this.D = s.D;',
  10870. ' this.Arr = rtl.arrayRef(s.Arr);',
  10871. ' this.Arr2 = s.Arr2.slice(0);',
  10872. ' this.Small.$assign(s.Small);',
  10873. ' this.Enums = rtl.refSet(s.Enums);',
  10874. ' return this;',
  10875. ' };',
  10876. '});',
  10877. 'this.r = this.TBigRec.$new();',
  10878. 'this.s = this.TBigRec.$new();',
  10879. '']),
  10880. LinesToStr([ // $mod.$main
  10881. '$mod.r.$assign($mod.s);',
  10882. '$mod.r.$assign($mod.TBigRec.$new());',
  10883. '$mod.r.$assign($mod.TBigRec.$new());',
  10884. '']));
  10885. end;
  10886. procedure TTestModule.TestRecord_AsParams;
  10887. begin
  10888. StartProgram(false);
  10889. Add([
  10890. 'type',
  10891. ' integer = longint;',
  10892. ' TRecord = record',
  10893. ' i: integer;',
  10894. ' end;',
  10895. 'procedure DoIt(vD: TRecord; const vC: TRecord; var vV: TRecord; var U);',
  10896. 'var vL: TRecord;',
  10897. 'begin',
  10898. ' vd:=vd;',
  10899. ' vd.i:=vd.i;',
  10900. ' vl:=vc;',
  10901. ' vv:=vv;',
  10902. ' vv.i:=vv.i;',
  10903. ' U:=vl;',
  10904. ' U:=vd;',
  10905. ' U:=vc;',
  10906. ' U:=vv;',
  10907. ' vl:=TRecord(U);',
  10908. ' vd:=TRecord(U);',
  10909. ' vv:=TRecord(U);',
  10910. ' doit(vd,vd,vd,vd);',
  10911. ' doit(vc,vc,vl,vl);',
  10912. ' doit(vv,vv,vv,vv);',
  10913. ' doit(vl,vl,vl,vl);',
  10914. ' TRecord(U).i:=3;',
  10915. 'end;',
  10916. 'var i: TRecord;',
  10917. 'begin',
  10918. ' doit(i,i,i,i);',
  10919. '']);
  10920. ConvertProgram;
  10921. CheckSource('TestRecord_AsParams',
  10922. LinesToStr([ // statements
  10923. 'rtl.recNewT(this, "TRecord", function () {',
  10924. ' this.i = 0;',
  10925. ' this.$eq = function (b) {',
  10926. ' return this.i === b.i;',
  10927. ' };',
  10928. ' this.$assign = function (s) {',
  10929. ' this.i = s.i;',
  10930. ' return this;',
  10931. ' };',
  10932. '});',
  10933. 'this.DoIt = function (vD, vC, vV, U) {',
  10934. ' var vL = $mod.TRecord.$new();',
  10935. ' vD.$assign(vD);',
  10936. ' vD.i = vD.i;',
  10937. ' vL.$assign(vC);',
  10938. ' vV.$assign(vV);',
  10939. ' vV.i = vV.i;',
  10940. ' U.$assign(vL);',
  10941. ' U.$assign(vD);',
  10942. ' U.$assign(vC);',
  10943. ' U.$assign(vV);',
  10944. ' vL.$assign(U);',
  10945. ' vD.$assign(U);',
  10946. ' vV.$assign(U);',
  10947. ' $mod.DoIt($mod.TRecord.$clone(vD), vD, vD, vD);',
  10948. ' $mod.DoIt($mod.TRecord.$clone(vC), vC, vL, vL);',
  10949. ' $mod.DoIt($mod.TRecord.$clone(vV), vV, vV, vV);',
  10950. ' $mod.DoIt($mod.TRecord.$clone(vL), vL, vL, vL);',
  10951. ' U.i = 3;',
  10952. '};',
  10953. 'this.i = this.TRecord.$new();'
  10954. ]),
  10955. LinesToStr([
  10956. '$mod.DoIt($mod.TRecord.$clone($mod.i), $mod.i, $mod.i, $mod.i);',
  10957. '']));
  10958. end;
  10959. procedure TTestModule.TestRecord_ConstRef;
  10960. begin
  10961. StartProgram(false);
  10962. Add([
  10963. 'type TRec = record i: word; end;',
  10964. 'procedure Run(constref a: TRec);',
  10965. 'begin',
  10966. 'end;',
  10967. 'procedure Fly(a: TRec; var b: TRec; out c: TRec; const d: TRec; constref e: TRec);',
  10968. 'var l: TRec;',
  10969. 'begin',
  10970. ' Run(l);',
  10971. ' Run(a);',
  10972. ' Run(b);',
  10973. ' Run(c);',
  10974. ' Run(d);',
  10975. ' Run(e);',
  10976. 'end;',
  10977. 'begin',
  10978. '']);
  10979. ConvertProgram;
  10980. CheckResolverUnexpectedHints();
  10981. CheckSource('TestRecord_ConstRef',
  10982. LinesToStr([ // statements
  10983. 'rtl.recNewT(this, "TRec", function () {',
  10984. ' this.i = 0;',
  10985. ' this.$eq = function (b) {',
  10986. ' return this.i === b.i;',
  10987. ' };',
  10988. ' this.$assign = function (s) {',
  10989. ' this.i = s.i;',
  10990. ' return this;',
  10991. ' };',
  10992. '});',
  10993. 'this.Run = function (a) {',
  10994. '};',
  10995. 'this.Fly = function (a, b, c, d, e) {',
  10996. ' var l = $mod.TRec.$new();',
  10997. ' $mod.Run(l);',
  10998. ' $mod.Run(a);',
  10999. ' $mod.Run(b);',
  11000. ' $mod.Run(c);',
  11001. ' $mod.Run(d);',
  11002. ' $mod.Run(e);',
  11003. '};',
  11004. '']),
  11005. LinesToStr([
  11006. '']));
  11007. end;
  11008. procedure TTestModule.TestRecordElement_AsParams;
  11009. begin
  11010. StartProgram(false);
  11011. Add('type');
  11012. Add(' integer = longint;');
  11013. Add(' TRecord = record');
  11014. Add(' i: integer;');
  11015. Add(' end;');
  11016. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  11017. Add('var vJ: TRecord;');
  11018. Add('begin');
  11019. Add(' doit(vj.i,vj.i,vj.i);');
  11020. Add('end;');
  11021. Add('var r: TRecord;');
  11022. Add('begin');
  11023. Add(' doit(r.i,r.i,r.i);');
  11024. ConvertProgram;
  11025. CheckSource('TestRecordElement_AsParams',
  11026. LinesToStr([ // statements
  11027. 'rtl.recNewT(this, "TRecord", function () {',
  11028. ' this.i = 0;',
  11029. ' this.$eq = function (b) {',
  11030. ' return this.i === b.i;',
  11031. ' };',
  11032. ' this.$assign = function (s) {',
  11033. ' this.i = s.i;',
  11034. ' return this;',
  11035. ' };',
  11036. '});',
  11037. 'this.DoIt = function (vG,vH,vI) {',
  11038. ' var vJ = $mod.TRecord.$new();',
  11039. ' $mod.DoIt(vJ.i, vJ.i, {',
  11040. ' p: vJ,',
  11041. ' get: function () {',
  11042. ' return this.p.i;',
  11043. ' },',
  11044. ' set: function (v) {',
  11045. ' this.p.i = v;',
  11046. ' }',
  11047. ' });',
  11048. '};',
  11049. 'this.r = this.TRecord.$new();'
  11050. ]),
  11051. LinesToStr([
  11052. '$mod.DoIt($mod.r.i,$mod.r.i,{',
  11053. ' p: $mod.r,',
  11054. ' get: function () {',
  11055. ' return this.p.i;',
  11056. ' },',
  11057. ' set: function (v) {',
  11058. ' this.p.i = v;',
  11059. ' }',
  11060. '});'
  11061. ]));
  11062. end;
  11063. procedure TTestModule.TestRecordElementFromFuncResult_AsParams;
  11064. begin
  11065. StartProgram(false);
  11066. Add('type');
  11067. Add(' integer = longint;');
  11068. Add(' TRecord = record');
  11069. Add(' i: integer;');
  11070. Add(' end;');
  11071. Add('function GetRec(vB: integer = 0): TRecord;');
  11072. Add('begin');
  11073. Add('end;');
  11074. Add('procedure DoIt(vG: integer; const vH: integer);');
  11075. Add('begin');
  11076. Add('end;');
  11077. Add('begin');
  11078. Add(' doit(getrec.i,getrec.i);');
  11079. Add(' doit(getrec().i,getrec().i);');
  11080. Add(' doit(getrec(1).i,getrec(2).i);');
  11081. ConvertProgram;
  11082. CheckSource('TestRecordElementFromFuncResult_AsParams',
  11083. LinesToStr([ // statements
  11084. 'rtl.recNewT(this, "TRecord", function () {',
  11085. ' this.i = 0;',
  11086. ' this.$eq = function (b) {',
  11087. ' return this.i === b.i;',
  11088. ' };',
  11089. ' this.$assign = function (s) {',
  11090. ' this.i = s.i;',
  11091. ' return this;',
  11092. ' };',
  11093. '});',
  11094. 'this.GetRec = function (vB) {',
  11095. ' var Result = $mod.TRecord.$new();',
  11096. ' return Result;',
  11097. '};',
  11098. 'this.DoIt = function (vG, vH) {',
  11099. '};',
  11100. '']),
  11101. LinesToStr([
  11102. '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
  11103. '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
  11104. '$mod.DoIt($mod.GetRec(1).i,$mod.GetRec(2).i);',
  11105. '']));
  11106. end;
  11107. procedure TTestModule.TestRecordElementFromWith_AsParams;
  11108. begin
  11109. StartProgram(false);
  11110. Add('type');
  11111. Add(' integer = longint;');
  11112. Add(' TRecord = record');
  11113. Add(' i: integer;');
  11114. Add(' end;');
  11115. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  11116. Add('begin');
  11117. Add('end;');
  11118. Add('var r: trecord;');
  11119. Add('begin');
  11120. Add(' with r do ');
  11121. Add(' doit(i,i,i);');
  11122. ConvertProgram;
  11123. CheckSource('TestRecordElementFromWith_AsParams',
  11124. LinesToStr([ // statements
  11125. 'rtl.recNewT(this, "TRecord", function () {',
  11126. ' this.i = 0;',
  11127. ' this.$eq = function (b) {',
  11128. ' return this.i === b.i;',
  11129. ' };',
  11130. ' this.$assign = function (s) {',
  11131. ' this.i = s.i;',
  11132. ' return this;',
  11133. ' };',
  11134. '});',
  11135. 'this.DoIt = function (vG,vH,vI) {',
  11136. '};',
  11137. 'this.r = this.TRecord.$new();'
  11138. ]),
  11139. LinesToStr([
  11140. 'var $with = $mod.r;',
  11141. '$mod.DoIt($with.i,$with.i,{',
  11142. ' p: $with,',
  11143. ' get: function () {',
  11144. ' return this.p.i;',
  11145. ' },',
  11146. ' set: function (v) {',
  11147. ' this.p.i = v;',
  11148. ' }',
  11149. '});',
  11150. '']));
  11151. end;
  11152. procedure TTestModule.TestRecord_Equal;
  11153. begin
  11154. StartProgram(false);
  11155. Add('type');
  11156. Add(' integer = longint;');
  11157. Add(' TFlag = (red,blue);');
  11158. Add(' TFlags = set of TFlag;');
  11159. Add(' TProc = procedure;');
  11160. Add(' TRecord = record');
  11161. Add(' i: integer;');
  11162. Add(' Event: TProc;');
  11163. Add(' f: TFlags;');
  11164. Add(' end;');
  11165. Add(' TNested = record');
  11166. Add(' r: TRecord;');
  11167. Add(' end;');
  11168. Add('var');
  11169. Add(' b: boolean;');
  11170. Add(' r,s: trecord;');
  11171. Add('begin');
  11172. Add(' b:=r=s;');
  11173. Add(' b:=r<>s;');
  11174. ConvertProgram;
  11175. CheckSource('TestRecord_Equal',
  11176. LinesToStr([ // statements
  11177. 'this.TFlag = {',
  11178. ' "0": "red",',
  11179. ' red: 0,',
  11180. ' "1": "blue",',
  11181. ' blue: 1',
  11182. '};',
  11183. 'rtl.recNewT(this, "TRecord", function () {',
  11184. ' this.i = 0;',
  11185. ' this.Event = null;',
  11186. ' this.$new = function () {',
  11187. ' var r = Object.create(this);',
  11188. ' r.f = {};',
  11189. ' return r;',
  11190. ' };',
  11191. ' this.$eq = function (b) {',
  11192. ' return (this.i === b.i) && rtl.eqCallback(this.Event, b.Event) && rtl.eqSet(this.f, b.f);',
  11193. ' };',
  11194. ' this.$assign = function (s) {',
  11195. ' this.i = s.i;',
  11196. ' this.Event = s.Event;',
  11197. ' this.f = rtl.refSet(s.f);',
  11198. ' return this;',
  11199. ' };',
  11200. '});',
  11201. 'rtl.recNewT(this, "TNested", function () {',
  11202. ' this.$new = function () {',
  11203. ' var r = Object.create(this);',
  11204. ' r.r = $mod.TRecord.$new();',
  11205. ' return r;',
  11206. ' };',
  11207. ' this.$eq = function (b) {',
  11208. ' return this.r.$eq(b.r);',
  11209. ' };',
  11210. ' this.$assign = function (s) {',
  11211. ' this.r.$assign(s.r);',
  11212. ' return this;',
  11213. ' };',
  11214. '});',
  11215. 'this.b = false;',
  11216. 'this.r = this.TRecord.$new();',
  11217. 'this.s = this.TRecord.$new();',
  11218. '']),
  11219. LinesToStr([
  11220. '$mod.b = $mod.r.$eq($mod.s);',
  11221. '$mod.b = !$mod.r.$eq($mod.s);',
  11222. '']));
  11223. end;
  11224. procedure TTestModule.TestRecord_JSValue;
  11225. begin
  11226. StartProgram(false);
  11227. Add([
  11228. 'type',
  11229. ' TRecord = record',
  11230. ' i: longint;',
  11231. ' end;',
  11232. 'procedure Fly(d: jsvalue; const c: jsvalue);',
  11233. 'begin',
  11234. 'end;',
  11235. 'procedure Run(d: TRecord; const c: TRecord; var v: TRecord);',
  11236. 'begin',
  11237. ' if jsvalue(d) then ;',
  11238. ' if jsvalue(c) then ;',
  11239. ' if jsvalue(v) then ;',
  11240. 'end;',
  11241. 'var',
  11242. ' Jv: jsvalue;',
  11243. ' Rec: trecord;',
  11244. 'begin',
  11245. ' rec:=trecord(jv);',
  11246. ' jv:=rec;',
  11247. ' Fly(rec,rec);',
  11248. ' Fly(@rec,@rec);',
  11249. ' if jsvalue(Rec) then ;',
  11250. ' Run(trecord(jv),trecord(jv),rec);',
  11251. '']);
  11252. ConvertProgram;
  11253. CheckSource('TestRecord_JSValue',
  11254. LinesToStr([ // statements
  11255. 'rtl.recNewT(this, "TRecord", function () {',
  11256. ' this.i = 0;',
  11257. ' this.$eq = function (b) {',
  11258. ' return this.i === b.i;',
  11259. ' };',
  11260. ' this.$assign = function (s) {',
  11261. ' this.i = s.i;',
  11262. ' return this;',
  11263. ' };',
  11264. '});',
  11265. 'this.Fly = function (d, c) {',
  11266. '};',
  11267. 'this.Run = function (d, c, v) {',
  11268. ' if (d) ;',
  11269. ' if (c) ;',
  11270. ' if (v) ;',
  11271. '};',
  11272. 'this.Jv = undefined;',
  11273. 'this.Rec = this.TRecord.$new();',
  11274. '']),
  11275. LinesToStr([
  11276. '$mod.Rec.$assign(rtl.getObject($mod.Jv));',
  11277. '$mod.Jv = $mod.Rec;',
  11278. '$mod.Fly($mod.TRecord.$clone($mod.Rec), $mod.Rec);',
  11279. '$mod.Fly($mod.Rec, $mod.Rec);',
  11280. 'if ($mod.Rec) ;',
  11281. '$mod.Run($mod.TRecord.$clone(rtl.getObject($mod.Jv)), rtl.getObject($mod.Jv), $mod.Rec);',
  11282. '']));
  11283. end;
  11284. procedure TTestModule.TestRecord_VariantFail;
  11285. begin
  11286. StartProgram(false);
  11287. Add([
  11288. 'type',
  11289. ' TRec = record',
  11290. ' case word of',
  11291. ' 0: (b0, b1: Byte);',
  11292. ' 1: (i: word);',
  11293. ' end;',
  11294. 'begin']);
  11295. SetExpectedPasResolverError('variant record is not supported',
  11296. nXIsNotSupported);
  11297. ConvertProgram;
  11298. end;
  11299. procedure TTestModule.TestRecord_FieldArray;
  11300. begin
  11301. StartProgram(false);
  11302. Add([
  11303. 'type',
  11304. ' TArrInt = array[3..4] of longint;',
  11305. ' TArrArrInt = array[3..4] of longint;',
  11306. ' TRec = record',
  11307. ' a: array of longint;',
  11308. ' s: array[1..2] of longint;',
  11309. ' m: array[1..2,3..4] of longint;',
  11310. ' o: TArrArrInt;',
  11311. ' end;',
  11312. 'begin']);
  11313. ConvertProgram;
  11314. CheckSource('TestRecord_FieldArray',
  11315. LinesToStr([ // statements
  11316. 'rtl.recNewT(this, "TRec", function () {',
  11317. ' this.$new = function () {',
  11318. ' var r = Object.create(this);',
  11319. ' r.a = [];',
  11320. ' r.s = rtl.arraySetLength(null, 0, 2);',
  11321. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  11322. ' r.o = rtl.arraySetLength(null, 0, 2);',
  11323. ' return r;',
  11324. ' };',
  11325. ' this.$eq = function (b) {',
  11326. ' return (this.a === b.a) && rtl.arrayEq(this.s, b.s) && rtl.arrayEq(this.m, b.m) && rtl.arrayEq(this.o, b.o);',
  11327. ' };',
  11328. ' this.$assign = function (s) {',
  11329. ' this.a = rtl.arrayRef(s.a);',
  11330. ' this.s = s.s.slice(0);',
  11331. ' this.m = s.m.slice(0);',
  11332. ' this.o = s.o.slice(0);',
  11333. ' return this;',
  11334. ' };',
  11335. '});',
  11336. '']),
  11337. LinesToStr([ // $mod.$main
  11338. '']));
  11339. end;
  11340. procedure TTestModule.TestRecord_Const;
  11341. begin
  11342. StartProgram(false);
  11343. Add([
  11344. 'type',
  11345. ' TArrInt = array[3..4] of longint;',
  11346. ' TPoint = record x,y: longint; end;',
  11347. ' TRec = record',
  11348. ' i: longint;',
  11349. ' a: array of longint;',
  11350. ' s: array[1..2] of longint;',
  11351. ' m: array[1..2,3..4] of longint;',
  11352. ' p: TPoint;',
  11353. ' end;',
  11354. ' TPoints = array of TPoint;',
  11355. 'const',
  11356. ' r: TRec = (',
  11357. ' i:1;',
  11358. ' a:(2,3);',
  11359. ' s:(4,5);',
  11360. ' m:( (11,12), (13,14) );',
  11361. ' p: (x:21; y:22)',
  11362. ' );',
  11363. ' p: TPoints = ( (x:1;y:2), (x:3;y:4) );',
  11364. 'begin']);
  11365. ConvertProgram;
  11366. CheckSource('TestRecord_Const',
  11367. LinesToStr([ // statements
  11368. 'rtl.recNewT(this, "TPoint", function () {',
  11369. ' this.x = 0;',
  11370. ' this.y = 0;',
  11371. ' this.$eq = function (b) {',
  11372. ' return (this.x === b.x) && (this.y === b.y);',
  11373. ' };',
  11374. ' this.$assign = function (s) {',
  11375. ' this.x = s.x;',
  11376. ' this.y = s.y;',
  11377. ' return this;',
  11378. ' };',
  11379. '});',
  11380. 'rtl.recNewT(this, "TRec", function () {',
  11381. ' this.i = 0;',
  11382. ' this.$new = function () {',
  11383. ' var r = Object.create(this);',
  11384. ' r.a = [];',
  11385. ' r.s = rtl.arraySetLength(null, 0, 2);',
  11386. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  11387. ' r.p = $mod.TPoint.$new();',
  11388. ' return r;',
  11389. ' };',
  11390. ' this.$eq = function (b) {',
  11391. ' 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);',
  11392. ' };',
  11393. ' this.$assign = function (s) {',
  11394. ' this.i = s.i;',
  11395. ' this.a = rtl.arrayRef(s.a);',
  11396. ' this.s = s.s.slice(0);',
  11397. ' this.m = s.m.slice(0);',
  11398. ' this.p.$assign(s.p);',
  11399. ' return this;',
  11400. ' };',
  11401. '});',
  11402. 'this.r = this.TRec.$clone({',
  11403. ' i: 1,',
  11404. ' a: [2, 3],',
  11405. ' s: [4, 5],',
  11406. ' m: [[11, 12], [13, 14]],',
  11407. ' p: this.TPoint.$clone({',
  11408. ' x: 21,',
  11409. ' y: 22',
  11410. ' })',
  11411. '});',
  11412. 'this.p = [this.TPoint.$clone({',
  11413. ' x: 1,',
  11414. ' y: 2',
  11415. '}), this.TPoint.$clone({',
  11416. ' x: 3,',
  11417. ' y: 4',
  11418. '})];',
  11419. '']),
  11420. LinesToStr([ // $mod.$main
  11421. '']));
  11422. end;
  11423. procedure TTestModule.TestRecord_TypecastFail;
  11424. begin
  11425. StartProgram(false);
  11426. Add([
  11427. 'type',
  11428. ' TPoint = record x,y: longint; end;',
  11429. ' TRec = record l: longint end;',
  11430. 'var p: TPoint;',
  11431. 'begin',
  11432. ' if TRec(p).l=2 then ;']);
  11433. SetExpectedPasResolverError('Illegal type conversion: "TPoint" to "record TRec"',
  11434. nIllegalTypeConversionTo);
  11435. ConvertProgram;
  11436. end;
  11437. procedure TTestModule.TestRecord_InFunction;
  11438. begin
  11439. StartProgram(false);
  11440. Add([
  11441. 'var TPoint: longint = 3;',
  11442. 'procedure DoIt;',
  11443. 'type',
  11444. ' TPoint = record x,y: longint; end;',
  11445. ' TPoints = array of TPoint;',
  11446. 'var',
  11447. ' r: TPoint;',
  11448. ' p: TPoints;',
  11449. 'begin',
  11450. ' SetLength(p,2);',
  11451. 'end;',
  11452. 'begin']);
  11453. ConvertProgram;
  11454. CheckSource('TestRecord_InFunction',
  11455. LinesToStr([ // statements
  11456. 'this.TPoint = 3;',
  11457. 'var TPoint$1 = rtl.recNewT(null, "", function () {',
  11458. ' this.x = 0;',
  11459. ' this.y = 0;',
  11460. ' this.$eq = function (b) {',
  11461. ' return (this.x === b.x) && (this.y === b.y);',
  11462. ' };',
  11463. ' this.$assign = function (s) {',
  11464. ' this.x = s.x;',
  11465. ' this.y = s.y;',
  11466. ' return this;',
  11467. ' };',
  11468. '});',
  11469. 'this.DoIt = function () {',
  11470. ' var r = TPoint$1.$new();',
  11471. ' var p = [];',
  11472. ' p = rtl.arraySetLength(p, TPoint$1, 2);',
  11473. '};',
  11474. '']),
  11475. LinesToStr([ // $mod.$main
  11476. '']));
  11477. end;
  11478. procedure TTestModule.TestRecord_AnonymousFail;
  11479. begin
  11480. StartProgram(false);
  11481. Add([
  11482. 'var',
  11483. ' r: record x: word end;',
  11484. 'begin']);
  11485. SetExpectedPasResolverError('not yet implemented: :TPasRecordType [20190408224556] "anonymous record type"',
  11486. nNotYetImplemented);
  11487. ConvertProgram;
  11488. end;
  11489. procedure TTestModule.TestAdvRecord_Function;
  11490. begin
  11491. StartProgram(false);
  11492. Parser.Options:=Parser.Options+[po_cassignments];
  11493. Add([
  11494. '{$modeswitch AdvancedRecords}',
  11495. 'type',
  11496. ' TPoint = record',
  11497. ' x,y: word;',
  11498. ' function Add(const apt: TPoint): TPoint;',
  11499. ' end;',
  11500. 'function TPoint.Add(const apt: TPoint): TPoint;',
  11501. 'begin',
  11502. ' Result:=Self;',
  11503. ' Result.x+=apt.x;',
  11504. ' Result.y:=Result.y+apt.y;',
  11505. ' Self:=apt;',
  11506. 'end;',
  11507. 'var p,q: TPoint;',
  11508. 'begin',
  11509. ' p.add(q);',
  11510. ' p:=default(TPoint);',
  11511. ' p:=q;',
  11512. '']);
  11513. ConvertProgram;
  11514. CheckSource('TestAdvRecord_Function',
  11515. LinesToStr([ // statements
  11516. 'rtl.recNewT(this, "TPoint", function () {',
  11517. ' this.x = 0;',
  11518. ' this.y = 0;',
  11519. ' this.$eq = function (b) {',
  11520. ' return (this.x === b.x) && (this.y === b.y);',
  11521. ' };',
  11522. ' this.$assign = function (s) {',
  11523. ' this.x = s.x;',
  11524. ' this.y = s.y;',
  11525. ' return this;',
  11526. ' };',
  11527. ' this.Add = function (apt) {',
  11528. ' var Result = $mod.TPoint.$new();',
  11529. ' Result.$assign(this);',
  11530. ' Result.x += apt.x;',
  11531. ' Result.y = Result.y + apt.y;',
  11532. ' this.$assign(apt);',
  11533. ' return Result;',
  11534. ' };',
  11535. '});',
  11536. 'this.p = this.TPoint.$new();',
  11537. 'this.q = this.TPoint.$new();',
  11538. '']),
  11539. LinesToStr([ // $mod.$main
  11540. '$mod.p.Add($mod.q);',
  11541. '$mod.p.$assign($mod.TPoint.$new());',
  11542. '$mod.p.$assign($mod.q);',
  11543. '']));
  11544. end;
  11545. procedure TTestModule.TestAdvRecord_Property;
  11546. begin
  11547. StartProgram(false);
  11548. Add([
  11549. '{$modeswitch AdvancedRecords}',
  11550. 'type',
  11551. ' TPoint = record',
  11552. ' x,y: word;',
  11553. ' strict private',
  11554. ' function GetSize: longword;',
  11555. ' procedure SetSize(Value: longword);',
  11556. ' public',
  11557. ' property Size: longword read GetSize write SetSize;',
  11558. ' property Left: word read x write y;',
  11559. ' end;',
  11560. 'procedure SetSize(Value: longword); begin end;',// check auto rename
  11561. 'function TPoint.GetSize: longword;',
  11562. 'begin',
  11563. ' x:=y;',
  11564. ' Size:=Size;',
  11565. ' Left:=Left;',
  11566. 'end;',
  11567. 'procedure TPoint.SetSize(Value: longword);',
  11568. 'begin',
  11569. 'end;',
  11570. 'var p,q: TPoint;',
  11571. 'begin',
  11572. ' p.Size:=q.Size;',
  11573. ' p.Left:=q.Left;',
  11574. '']);
  11575. ConvertProgram;
  11576. CheckSource('TestAdvRecord_Property',
  11577. LinesToStr([ // statements
  11578. 'rtl.recNewT(this, "TPoint", function () {',
  11579. ' this.x = 0;',
  11580. ' this.y = 0;',
  11581. ' this.$eq = function (b) {',
  11582. ' return (this.x === b.x) && (this.y === b.y);',
  11583. ' };',
  11584. ' this.$assign = function (s) {',
  11585. ' this.x = s.x;',
  11586. ' this.y = s.y;',
  11587. ' return this;',
  11588. ' };',
  11589. ' this.GetSize = function () {',
  11590. ' var Result = 0;',
  11591. ' this.x = this.y;',
  11592. ' this.SetSize(this.GetSize());',
  11593. ' this.y = this.x;',
  11594. ' return Result;',
  11595. ' };',
  11596. ' this.SetSize = function (Value) {',
  11597. ' };',
  11598. '});',
  11599. 'this.SetSize = function (Value) {',
  11600. '};',
  11601. 'this.p = this.TPoint.$new();',
  11602. 'this.q = this.TPoint.$new();',
  11603. '']),
  11604. LinesToStr([ // $mod.$main
  11605. '$mod.p.SetSize($mod.q.GetSize());',
  11606. '$mod.p.y = $mod.q.x;',
  11607. '']));
  11608. end;
  11609. procedure TTestModule.TestAdvRecord_PropertyDefault;
  11610. begin
  11611. StartProgram(false);
  11612. Add([
  11613. '{$modeswitch AdvancedRecords}',
  11614. 'type',
  11615. ' TPoint = record',
  11616. ' strict private',
  11617. ' function GetItems(Index: word): word;',
  11618. ' procedure SetItems(Index: word; Value: word);',
  11619. ' public',
  11620. ' property Items[Index: word]: word read GetItems write SetItems; default;',
  11621. ' end;',
  11622. 'function TPoint.GetItems(Index: word): word;',
  11623. 'begin',
  11624. ' Items[index]:=Items[index];',
  11625. ' self.Items[index]:=self.Items[index];',
  11626. 'end;',
  11627. 'procedure TPoint.SetItems(Index: word; Value: word);',
  11628. 'begin',
  11629. 'end;',
  11630. 'var p: TPoint;',
  11631. 'begin',
  11632. ' p[1]:=p[2];',
  11633. ' p.Items[3]:=p.Items[4];',
  11634. '']);
  11635. ConvertProgram;
  11636. CheckSource('TestAdvRecord_PropertyDefault',
  11637. LinesToStr([ // statements
  11638. 'rtl.recNewT(this, "TPoint", function () {',
  11639. ' this.$eq = function (b) {',
  11640. ' return true;',
  11641. ' };',
  11642. ' this.$assign = function (s) {',
  11643. ' return this;',
  11644. ' };',
  11645. ' this.GetItems = function (Index) {',
  11646. ' var Result = 0;',
  11647. ' this.SetItems(Index, this.GetItems(Index));',
  11648. ' this.SetItems(Index, this.GetItems(Index));',
  11649. ' return Result;',
  11650. ' };',
  11651. ' this.SetItems = function (Index, Value) {',
  11652. ' };',
  11653. '});',
  11654. 'this.p = this.TPoint.$new();',
  11655. '']),
  11656. LinesToStr([ // $mod.$main
  11657. '$mod.p.SetItems(1, $mod.p.GetItems(2));',
  11658. '$mod.p.SetItems(3, $mod.p.GetItems(4));',
  11659. '']));
  11660. end;
  11661. procedure TTestModule.TestAdvRecord_Property_ClassMethod;
  11662. begin
  11663. StartProgram(false);
  11664. Add([
  11665. '{$modeswitch AdvancedRecords}',
  11666. 'type',
  11667. ' TRec = record',
  11668. ' class var',
  11669. ' Fx: longint;',
  11670. ' Fy: longint;',
  11671. ' class function GetInt: longint; static;',
  11672. ' class procedure SetInt(Value: longint); static;',
  11673. ' class procedure DoIt; static;',
  11674. ' class property IntA: longint read Fx write Fy;',
  11675. ' class property IntB: longint read GetInt write SetInt;',
  11676. ' end;',
  11677. 'class function trec.getint: longint;',
  11678. 'begin',
  11679. ' result:=fx;',
  11680. 'end;',
  11681. 'class procedure trec.setint(value: longint);',
  11682. 'begin',
  11683. 'end;',
  11684. 'class procedure trec.doit;',
  11685. 'begin',
  11686. ' IntA:=IntA+1;',
  11687. ' IntB:=IntB+1;',
  11688. 'end;',
  11689. 'var r: trec;',
  11690. 'begin',
  11691. ' trec.inta:=trec.inta+1;',
  11692. ' if trec.intb=2 then;',
  11693. ' trec.intb:=trec.intb+2;',
  11694. ' trec.setint(trec.inta);',
  11695. ' r.inta:=r.inta+1;',
  11696. ' if r.intb=2 then;',
  11697. ' r.intb:=r.intb+2;',
  11698. ' r.setint(r.inta);']);
  11699. ConvertProgram;
  11700. CheckSource('TestAdvRecord_Property_ClassMethod',
  11701. LinesToStr([ // statements
  11702. 'rtl.recNewT(this, "TRec", function () {',
  11703. ' this.Fx = 0;',
  11704. ' this.Fy = 0;',
  11705. ' this.$eq = function (b) {',
  11706. ' return true;',
  11707. ' };',
  11708. ' this.$assign = function (s) {',
  11709. ' return this;',
  11710. ' };',
  11711. ' this.GetInt = function () {',
  11712. ' var Result = 0;',
  11713. ' Result = $mod.TRec.Fx;',
  11714. ' return Result;',
  11715. ' };',
  11716. ' this.SetInt = function (Value) {',
  11717. ' };',
  11718. ' this.DoIt = function () {',
  11719. ' $mod.TRec.Fy = $mod.TRec.Fx + 1;',
  11720. ' $mod.TRec.SetInt($mod.TRec.GetInt() + 1);',
  11721. ' };',
  11722. '}, true);',
  11723. 'this.r = this.TRec.$new();',
  11724. '']),
  11725. LinesToStr([ // $mod.$main
  11726. '$mod.TRec.Fy = $mod.TRec.Fx + 1;',
  11727. 'if ($mod.TRec.GetInt() === 2) ;',
  11728. '$mod.TRec.SetInt($mod.TRec.GetInt() + 2);',
  11729. '$mod.TRec.SetInt($mod.TRec.Fx);',
  11730. '$mod.TRec.Fy = $mod.r.Fx + 1;',
  11731. 'if ($mod.TRec.GetInt() === 2) ;',
  11732. '$mod.TRec.SetInt($mod.TRec.GetInt() + 2);',
  11733. '$mod.TRec.SetInt($mod.r.Fx);',
  11734. '']));
  11735. end;
  11736. procedure TTestModule.TestAdvRecord_Const;
  11737. begin
  11738. StartProgram(false);
  11739. Add([
  11740. '{$modeswitch AdvancedRecords}',
  11741. 'type',
  11742. ' TArrInt = array[3..4] of longint;',
  11743. ' TPoint = record',
  11744. ' x,y: longint;',
  11745. ' class var Count: nativeint;',
  11746. ' end;',
  11747. ' TRec = record',
  11748. ' i: longint;',
  11749. ' a: array of longint;',
  11750. ' s: array[1..2] of longint;',
  11751. ' m: array[1..2,3..4] of longint;',
  11752. ' p: TPoint;',
  11753. ' end;',
  11754. ' TPoints = array of TPoint;',
  11755. 'const',
  11756. ' r: TRec = (',
  11757. ' i:1;',
  11758. ' a:(2,3);',
  11759. ' s:(4,5);',
  11760. ' m:( (11,12), (13,14) );',
  11761. ' p: (x:21)',
  11762. ' );',
  11763. ' p: TPoints = ( (x:1;y:2), (x:3;y:4) );',
  11764. 'begin']);
  11765. ConvertProgram;
  11766. CheckSource('TestAdvRecord_Const',
  11767. LinesToStr([ // statements
  11768. 'rtl.recNewT(this, "TPoint", function () {',
  11769. ' this.x = 0;',
  11770. ' this.y = 0;',
  11771. ' this.Count = 0;',
  11772. ' this.$eq = function (b) {',
  11773. ' return (this.x === b.x) && (this.y === b.y);',
  11774. ' };',
  11775. ' this.$assign = function (s) {',
  11776. ' this.x = s.x;',
  11777. ' this.y = s.y;',
  11778. ' return this;',
  11779. ' };',
  11780. '}, true);',
  11781. 'rtl.recNewT(this, "TRec", function () {',
  11782. ' this.i = 0;',
  11783. ' this.$new = function () {',
  11784. ' var r = Object.create(this);',
  11785. ' r.a = [];',
  11786. ' r.s = rtl.arraySetLength(null, 0, 2);',
  11787. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  11788. ' r.p = $mod.TPoint.$new();',
  11789. ' return r;',
  11790. ' };',
  11791. ' this.$eq = function (b) {',
  11792. ' 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);',
  11793. ' };',
  11794. ' this.$assign = function (s) {',
  11795. ' this.i = s.i;',
  11796. ' this.a = rtl.arrayRef(s.a);',
  11797. ' this.s = s.s.slice(0);',
  11798. ' this.m = s.m.slice(0);',
  11799. ' this.p.$assign(s.p);',
  11800. ' return this;',
  11801. ' };',
  11802. '});',
  11803. 'this.r = this.TRec.$clone({',
  11804. ' i: 1,',
  11805. ' a: [2, 3],',
  11806. ' s: [4, 5],',
  11807. ' m: [[11, 12], [13, 14]],',
  11808. ' p: this.TPoint.$clone({',
  11809. ' x: 21,',
  11810. ' y: 0',
  11811. ' })',
  11812. '});',
  11813. 'this.p = [this.TPoint.$clone({',
  11814. ' x: 1,',
  11815. ' y: 2',
  11816. '}), this.TPoint.$clone({',
  11817. ' x: 3,',
  11818. ' y: 4',
  11819. '})];',
  11820. '']),
  11821. LinesToStr([ // $mod.$main
  11822. '']));
  11823. end;
  11824. procedure TTestModule.TestAdvRecord_ExternalField;
  11825. begin
  11826. StartProgram(false);
  11827. Add([
  11828. '{$modeswitch AdvancedRecords}',
  11829. '{$modeswitch externalclass}',
  11830. 'type',
  11831. ' TCar = record',
  11832. ' public',
  11833. ' Intern: longint external name ''$Intern'';',
  11834. ' Intern2: longint external name ''$Intern2'';',
  11835. ' Bracket: longint external name ''["A B"]'';',
  11836. ' procedure DoIt;',
  11837. ' end;',
  11838. 'procedure tcar.doit;',
  11839. 'begin',
  11840. ' Intern:=Intern+1;',
  11841. ' Intern2:=Intern2+2;',
  11842. ' Bracket:=Bracket+3;',
  11843. 'end;',
  11844. 'var Rec: TCar = (intern: 11; intern2: 12; bracket: 13);',
  11845. 'begin',
  11846. ' Rec.intern:=Rec.intern+1;',
  11847. ' Rec.intern2:=Rec.intern2+2;',
  11848. ' Rec.Bracket:=Rec.Bracket+3;',
  11849. ' with Rec do begin',
  11850. ' intern:=intern+1;',
  11851. ' intern2:=intern2+2;',
  11852. ' Bracket:=Bracket+3;',
  11853. ' end;']);
  11854. ConvertProgram;
  11855. CheckSource('TestAdvRecord_ExternalField',
  11856. LinesToStr([ // statements
  11857. 'rtl.recNewT(this, "TCar", function () {',
  11858. ' this.$eq = function (b) {',
  11859. ' return (this.$Intern === b.$Intern) && (this.$Intern2 === b.$Intern2) && (this["A B"] === b["A B"]);',
  11860. ' };',
  11861. ' this.$assign = function (s) {',
  11862. ' this.$Intern = s.$Intern;',
  11863. ' this.$Intern2 = s.$Intern2;',
  11864. ' this["A B"] = s["A B"];',
  11865. ' return this;',
  11866. ' };',
  11867. ' this.DoIt = function () {',
  11868. ' this.$Intern = this.$Intern + 1;',
  11869. ' this.$Intern2 = this.$Intern2 + 2;',
  11870. ' this["A B"] = this["A B"] + 3;',
  11871. ' };',
  11872. '});',
  11873. 'this.Rec = this.TCar.$clone({',
  11874. ' $Intern: 11,',
  11875. ' $Intern2: 12,',
  11876. ' "A B": 13',
  11877. '});',
  11878. '']),
  11879. LinesToStr([ // $mod.$main
  11880. '$mod.Rec.$Intern = $mod.Rec.$Intern + 1;',
  11881. '$mod.Rec.$Intern2 = $mod.Rec.$Intern2 + 2;',
  11882. '$mod.Rec["A B"] = $mod.Rec["A B"] + 3;',
  11883. 'var $with = $mod.Rec;',
  11884. '$with.$Intern = $with.$Intern + 1;',
  11885. '$with.$Intern2 = $with.$Intern2 + 2;',
  11886. '$with["A B"] = $with["A B"] + 3;',
  11887. '']));
  11888. end;
  11889. procedure TTestModule.TestAdvRecord_SubRecord;
  11890. begin
  11891. StartProgram(false);
  11892. Add([
  11893. '{$modeswitch AdvancedRecords}',
  11894. 'type',
  11895. ' TRec = record',
  11896. ' type',
  11897. ' TPoint = record',
  11898. ' x,y: longint;',
  11899. ' class var Count: nativeint;',
  11900. ' procedure DoIt;',
  11901. ' class procedure DoThat; static;',
  11902. ' end;',
  11903. ' var',
  11904. ' i: longint;',
  11905. ' p: TPoint;',
  11906. ' procedure DoSome;',
  11907. ' end;',
  11908. 'const',
  11909. ' r: TRec = (',
  11910. ' i:1;',
  11911. ' p: (x:21;y:22)',
  11912. ' );',
  11913. 'procedure TRec.DoSome;',
  11914. 'begin',
  11915. ' p.x:=p.y+1;',
  11916. ' p.Count:=p.Count+2;',
  11917. 'end;',
  11918. 'procedure TRec.TPoint.DoIt;',
  11919. 'begin',
  11920. ' Count:=Count+3;',
  11921. 'end;',
  11922. 'class procedure TRec.TPoint.DoThat;',
  11923. 'begin',
  11924. ' Count:=Count+4;',
  11925. 'end;',
  11926. 'begin']);
  11927. ConvertProgram;
  11928. CheckSource('TestAdvRecord_SubRecord',
  11929. LinesToStr([ // statements
  11930. 'rtl.recNewT(this, "TRec", function () {',
  11931. ' rtl.recNewT(this, "TPoint", function () {',
  11932. ' this.x = 0;',
  11933. ' this.y = 0;',
  11934. ' this.Count = 0;',
  11935. ' this.$eq = function (b) {',
  11936. ' return (this.x === b.x) && (this.y === b.y);',
  11937. ' };',
  11938. ' this.$assign = function (s) {',
  11939. ' this.x = s.x;',
  11940. ' this.y = s.y;',
  11941. ' return this;',
  11942. ' };',
  11943. ' this.DoIt = function () {',
  11944. ' $mod.TRec.TPoint.Count = this.Count + 3;',
  11945. ' };',
  11946. ' this.DoThat = function () {',
  11947. ' $mod.TRec.TPoint.Count = $mod.TRec.TPoint.Count + 4;',
  11948. ' };',
  11949. ' }, true);',
  11950. ' this.i = 0;',
  11951. ' this.$new = function () {',
  11952. ' var r = Object.create(this);',
  11953. ' r.p = this.TPoint.$new();',
  11954. ' return r;',
  11955. ' };',
  11956. ' this.$eq = function (b) {',
  11957. ' return (this.i === b.i) && this.p.$eq(b.p);',
  11958. ' };',
  11959. ' this.$assign = function (s) {',
  11960. ' this.i = s.i;',
  11961. ' this.p.$assign(s.p);',
  11962. ' return this;',
  11963. ' };',
  11964. ' this.DoSome = function () {',
  11965. ' this.p.x = this.p.y + 1;',
  11966. ' this.TPoint.Count = this.p.Count + 2;',
  11967. ' };',
  11968. '}, true);',
  11969. 'this.r = this.TRec.$clone({',
  11970. ' i: 1,',
  11971. ' p: this.TRec.TPoint.$clone({',
  11972. ' x: 21,',
  11973. ' y: 22',
  11974. ' })',
  11975. '});',
  11976. '']),
  11977. LinesToStr([ // $mod.$main
  11978. '']));
  11979. end;
  11980. procedure TTestModule.TestAdvRecord_SubClass;
  11981. begin
  11982. StartProgram(false);
  11983. Add([
  11984. '{$modeswitch AdvancedRecords}',
  11985. 'type',
  11986. ' TObject = class end;',
  11987. ' TPoint = record',
  11988. ' type',
  11989. ' TBird = class',
  11990. ' procedure DoIt;',
  11991. ' class procedure Glob;',
  11992. ' end;',
  11993. ' procedure DoIt(b: TBird);',
  11994. ' end;',
  11995. 'procedure TPoint.TBird.DoIt;',
  11996. 'begin',
  11997. ' doit;',
  11998. ' self.doit;',
  11999. ' glob;',
  12000. ' self.glob;',
  12001. 'end;',
  12002. 'class procedure TPoint.TBird.Glob;',
  12003. 'begin',
  12004. ' glob;',
  12005. ' self.glob;',
  12006. 'end;',
  12007. 'procedure TPoint.DoIt(b: TBird);',
  12008. 'begin',
  12009. ' b.doit;',
  12010. ' b.glob;',
  12011. ' TBird.glob;',
  12012. 'end;',
  12013. 'begin',
  12014. '']);
  12015. ConvertProgram;
  12016. CheckSource('TestAdvRecord_SubClass',
  12017. LinesToStr([ // statements
  12018. 'rtl.createClass(this, "TObject", null, function () {',
  12019. ' this.$init = function () {',
  12020. ' };',
  12021. ' this.$final = function () {',
  12022. ' };',
  12023. '});',
  12024. 'rtl.recNewT(this, "TPoint", function () {',
  12025. ' rtl.createClass(this, "TBird", $mod.TObject, function () {',
  12026. ' this.DoIt = function () {',
  12027. ' this.DoIt();',
  12028. ' this.DoIt();',
  12029. ' this.$class.Glob();',
  12030. ' this.$class.Glob();',
  12031. ' };',
  12032. ' this.Glob = function () {',
  12033. ' this.Glob();',
  12034. ' this.Glob();',
  12035. ' };',
  12036. ' }, "TPoint.TBird");',
  12037. ' this.$eq = function (b) {',
  12038. ' return true;',
  12039. ' };',
  12040. ' this.$assign = function (s) {',
  12041. ' return this;',
  12042. ' };',
  12043. ' this.DoIt = function (b) {',
  12044. ' b.DoIt();',
  12045. ' b.$class.Glob();',
  12046. ' this.TBird.Glob();',
  12047. ' };',
  12048. '}, true);',
  12049. '']),
  12050. LinesToStr([ // $mod.$main
  12051. '']));
  12052. end;
  12053. procedure TTestModule.TestAdvRecord_SubInterfaceFail;
  12054. begin
  12055. StartProgram(false);
  12056. Add([
  12057. '{$modeswitch AdvancedRecords}',
  12058. 'type',
  12059. ' IUnknown = interface end;',
  12060. ' TPoint = record',
  12061. ' type IBird = interface end;',
  12062. ' end;',
  12063. 'begin',
  12064. '']);
  12065. SetExpectedPasResolverError('not yet implemented: IBird:TPasClassType [20190105143752] "interface inside record"',
  12066. nNotYetImplemented);
  12067. ParseProgram;
  12068. end;
  12069. procedure TTestModule.TestAdvRecord_Constructor;
  12070. begin
  12071. StartProgram(false);
  12072. Add([
  12073. '{$modeswitch AdvancedRecords}',
  12074. 'type',
  12075. ' TPoint = record',
  12076. ' x,y: longint;',
  12077. ' class procedure Run(w: longint = 13); static;',
  12078. ' constructor Create(ax: longint; ay: longint = -1);',
  12079. ' end;',
  12080. 'class procedure tpoint.run(w: longint);',
  12081. 'begin',
  12082. ' run;',
  12083. ' run();',
  12084. 'end;',
  12085. 'constructor tpoint.create(ax,ay: longint);',
  12086. 'begin',
  12087. ' x:=ax;',
  12088. ' self.y:=ay;',
  12089. ' run;',
  12090. ' run(ax);',
  12091. 'end;',
  12092. 'var r: TPoint;',
  12093. 'begin',
  12094. ' r:=TPoint.Create(1,2);',
  12095. ' with TPoint do r:=Create(1,2);',
  12096. ' r.Create(3);',
  12097. ' r:=r.Create(4);',
  12098. '']);
  12099. ConvertProgram;
  12100. CheckSource('TestAdvRecord_Constructor',
  12101. LinesToStr([ // statements
  12102. 'rtl.recNewT(this, "TPoint", function () {',
  12103. ' this.x = 0;',
  12104. ' this.y = 0;',
  12105. ' this.$eq = function (b) {',
  12106. ' return (this.x === b.x) && (this.y === b.y);',
  12107. ' };',
  12108. ' this.$assign = function (s) {',
  12109. ' this.x = s.x;',
  12110. ' this.y = s.y;',
  12111. ' return this;',
  12112. ' };',
  12113. ' this.Run = function (w) {',
  12114. ' $mod.TPoint.Run(13);',
  12115. ' $mod.TPoint.Run(13);',
  12116. ' };',
  12117. ' this.Create = function (ax, ay) {',
  12118. ' this.x = ax;',
  12119. ' this.y = ay;',
  12120. ' this.Run(13);',
  12121. ' this.Run(ax);',
  12122. ' return this;',
  12123. ' };',
  12124. '});',
  12125. 'this.r = this.TPoint.$new();',
  12126. '']),
  12127. LinesToStr([ // $mod.$main
  12128. '$mod.r.$assign($mod.TPoint.$new().Create(1, 2));',
  12129. 'var $with = $mod.TPoint;',
  12130. '$mod.r.$assign($with.$new().Create(1, 2));',
  12131. '$mod.r.Create(3, -1);',
  12132. '$mod.r.$assign($mod.r.Create(4, -1));',
  12133. '']));
  12134. end;
  12135. procedure TTestModule.TestAdvRecord_ClassConstructor_Program;
  12136. begin
  12137. StartProgram(false);
  12138. Add([
  12139. '{$modeswitch AdvancedRecords}',
  12140. 'type',
  12141. ' TPoint = record',
  12142. ' class var x: longint;',
  12143. ' class procedure Fly; static;',
  12144. ' class constructor Init;',
  12145. ' end;',
  12146. 'var count: word;',
  12147. 'class procedure Tpoint.Fly;',
  12148. 'begin',
  12149. 'end;',
  12150. 'class constructor tpoint.init;',
  12151. 'begin',
  12152. ' count:=count+1;',
  12153. ' x:=x+3;',
  12154. ' tpoint.x:=tpoint.x+4;',
  12155. ' fly;',
  12156. ' tpoint.fly;',
  12157. 'end;',
  12158. 'var r: TPoint;',
  12159. 'begin',
  12160. ' r.x:=r.x+10;',
  12161. ' r.Fly;',
  12162. ' r.Fly();',
  12163. '']);
  12164. ConvertProgram;
  12165. CheckSource('TestAdvRecord_ClassConstructor_Program',
  12166. LinesToStr([ // statements
  12167. 'rtl.recNewT(this, "TPoint", function () {',
  12168. ' this.x = 0;',
  12169. ' this.$eq = function (b) {',
  12170. ' return true;',
  12171. ' };',
  12172. ' this.$assign = function (s) {',
  12173. ' return this;',
  12174. ' };',
  12175. ' this.Fly = function () {',
  12176. ' };',
  12177. '}, true);',
  12178. 'this.count = 0;',
  12179. 'this.r = this.TPoint.$new();',
  12180. '']),
  12181. LinesToStr([ // $mod.$main
  12182. '(function () {',
  12183. ' $mod.count = $mod.count + 1;',
  12184. ' $mod.TPoint.x = $mod.TPoint.x + 3;',
  12185. ' $mod.TPoint.x = $mod.TPoint.x + 4;',
  12186. ' $mod.TPoint.Fly();',
  12187. ' $mod.TPoint.Fly();',
  12188. '})();',
  12189. '$mod.TPoint.x = $mod.r.x + 10;',
  12190. '$mod.TPoint.Fly();',
  12191. '$mod.TPoint.Fly();',
  12192. '']));
  12193. end;
  12194. procedure TTestModule.TestAdvRecord_ClassConstructor_Unit;
  12195. begin
  12196. StartUnit(false);
  12197. Add([
  12198. 'interface',
  12199. '{$modeswitch AdvancedRecords}',
  12200. 'type',
  12201. ' TPoint = record',
  12202. ' class var x: longint;',
  12203. ' class procedure Fly; static;',
  12204. ' class constructor Init;',
  12205. ' end;',
  12206. 'implementation',
  12207. 'var count: word;',
  12208. 'class procedure Tpoint.Fly;',
  12209. 'begin',
  12210. 'end;',
  12211. 'class constructor tpoint.init;',
  12212. 'begin',
  12213. ' count:=count+1;',
  12214. ' x:=3;',
  12215. ' tpoint.x:=4;',
  12216. ' fly;',
  12217. ' tpoint.fly;',
  12218. 'end;',
  12219. '']);
  12220. ConvertUnit;
  12221. CheckSource('TestAdvRecord_ClassConstructor_Unit',
  12222. LinesToStr([ // statements
  12223. 'var $impl = $mod.$impl;',
  12224. 'rtl.recNewT(this, "TPoint", function () {',
  12225. ' this.x = 0;',
  12226. ' this.$eq = function (b) {',
  12227. ' return true;',
  12228. ' };',
  12229. ' this.$assign = function (s) {',
  12230. ' return this;',
  12231. ' };',
  12232. ' this.Fly = function () {',
  12233. ' };',
  12234. '}, true);',
  12235. '']),
  12236. LinesToStr([ // $mod.$init
  12237. '(function () {',
  12238. ' $impl.count = $impl.count + 1;',
  12239. ' $mod.TPoint.x = 3;',
  12240. ' $mod.TPoint.x = 4;',
  12241. ' $mod.TPoint.Fly();',
  12242. ' $mod.TPoint.Fly();',
  12243. '})();',
  12244. '']),
  12245. LinesToStr([ // $mod.$main
  12246. '$impl.count = 0;',
  12247. '']));
  12248. end;
  12249. procedure TTestModule.TestClass_TObjectDefaultConstructor;
  12250. begin
  12251. StartProgram(false);
  12252. Add(['type',
  12253. ' TObject = class',
  12254. ' public',
  12255. ' constructor Create;',
  12256. ' destructor Destroy;',
  12257. ' end;',
  12258. ' TBird = TObject;',
  12259. 'constructor tobject.create;',
  12260. 'begin end;',
  12261. 'destructor tobject.destroy;',
  12262. 'begin end;',
  12263. 'var Obj: tobject;',
  12264. 'begin',
  12265. ' obj:=tobject.create;',
  12266. ' obj:=tobject.create();',
  12267. ' obj:=tbird.create;',
  12268. ' obj:=tbird.create();',
  12269. ' obj:=obj.create();',
  12270. ' obj.destroy;',
  12271. '']);
  12272. ConvertProgram;
  12273. CheckSource('TestClass_TObjectDefaultConstructor',
  12274. LinesToStr([ // statements
  12275. 'rtl.createClass(this,"TObject",null,function(){',
  12276. ' this.$init = function () {',
  12277. ' };',
  12278. ' this.$final = function () {',
  12279. ' };',
  12280. ' this.Create = function(){',
  12281. ' return this;',
  12282. ' };',
  12283. ' this.Destroy = function(){',
  12284. ' };',
  12285. '});',
  12286. 'this.Obj = null;'
  12287. ]),
  12288. LinesToStr([ // $mod.$main
  12289. '$mod.Obj = $mod.TObject.$create("Create");',
  12290. '$mod.Obj = $mod.TObject.$create("Create");',
  12291. '$mod.Obj = $mod.TObject.$create("Create");',
  12292. '$mod.Obj = $mod.TObject.$create("Create");',
  12293. '$mod.Obj = $mod.Obj.Create();',
  12294. '$mod.Obj.$destroy("Destroy");',
  12295. '']));
  12296. end;
  12297. procedure TTestModule.TestClass_TObjectConstructorWithParams;
  12298. begin
  12299. StartProgram(false);
  12300. Add('type');
  12301. Add(' TObject = class');
  12302. Add(' public');
  12303. Add(' constructor Create(Par: longint);');
  12304. Add(' end;');
  12305. Add('constructor tobject.create(par: longint);');
  12306. Add('begin end;');
  12307. Add('var Obj: tobject;');
  12308. Add('begin');
  12309. Add(' obj:=tobject.create(3);');
  12310. ConvertProgram;
  12311. CheckSource('TestClass_TObjectConstructorWithParams',
  12312. LinesToStr([ // statements
  12313. 'rtl.createClass(this,"TObject",null,function(){',
  12314. ' this.$init = function () {',
  12315. ' };',
  12316. ' this.$final = function () {',
  12317. ' };',
  12318. ' this.Create = function(Par){',
  12319. ' return this;',
  12320. ' };',
  12321. '});',
  12322. 'this.Obj = null;'
  12323. ]),
  12324. LinesToStr([ // $mod.$main
  12325. '$mod.Obj = $mod.TObject.$create("Create",[3]);'
  12326. ]));
  12327. end;
  12328. procedure TTestModule.TestClass_TObjectConstructorWithDefaultParam;
  12329. begin
  12330. StartProgram(false);
  12331. Add('type');
  12332. Add(' TObject = class');
  12333. Add(' public');
  12334. Add(' constructor Create;');
  12335. Add(' end;');
  12336. Add(' TTest = class(TObject)');
  12337. Add(' public');
  12338. Add(' constructor Create(const Par: longint = 1);');
  12339. Add(' end;');
  12340. Add('constructor tobject.create;');
  12341. Add('begin end;');
  12342. Add('constructor ttest.create(const par: longint);');
  12343. Add('begin end;');
  12344. Add('var t: ttest;');
  12345. Add('begin');
  12346. Add(' t:=ttest.create;');
  12347. Add(' t:=ttest.create(2);');
  12348. ConvertProgram;
  12349. CheckSource('TestClass_TObjectConstructorWithDefaultParam',
  12350. LinesToStr([ // statements
  12351. 'rtl.createClass(this,"TObject",null,function(){',
  12352. ' this.$init = function () {',
  12353. ' };',
  12354. ' this.$final = function () {',
  12355. ' };',
  12356. ' this.Create = function(){',
  12357. ' return this;',
  12358. ' };',
  12359. '});',
  12360. 'rtl.createClass(this, "TTest", this.TObject, function () {',
  12361. ' this.Create$1 = function (Par) {',
  12362. ' return this;',
  12363. ' };',
  12364. '});',
  12365. 'this.t = null;'
  12366. ]),
  12367. LinesToStr([ // $mod.$main
  12368. '$mod.t = $mod.TTest.$create("Create$1", [1]);',
  12369. '$mod.t = $mod.TTest.$create("Create$1", [2]);'
  12370. ]));
  12371. end;
  12372. procedure TTestModule.TestClass_Var;
  12373. begin
  12374. StartProgram(false);
  12375. Add([
  12376. 'type',
  12377. ' TObject = class',
  12378. ' public',
  12379. ' vI: longint;',
  12380. ' constructor Create(Par: longint);',
  12381. ' end;',
  12382. 'constructor tobject.create(par: longint);',
  12383. 'begin',
  12384. ' vi:=par+3',
  12385. 'end;',
  12386. 'var Obj: tobject;',
  12387. 'begin',
  12388. ' obj:=tobject.create(4);',
  12389. ' obj.vi:=obj.VI+5;']);
  12390. ConvertProgram;
  12391. CheckSource('TestClass_Var',
  12392. LinesToStr([ // statements
  12393. 'rtl.createClass(this,"TObject",null,function(){',
  12394. ' this.$init = function () {',
  12395. ' this.vI = 0;',
  12396. ' };',
  12397. ' this.$final = function () {',
  12398. ' };',
  12399. ' this.Create = function(Par){',
  12400. ' this.vI = Par+3;',
  12401. ' return this;',
  12402. ' };',
  12403. '});',
  12404. 'this.Obj = null;'
  12405. ]),
  12406. LinesToStr([ // $mod.$main
  12407. '$mod.Obj = $mod.TObject.$create("Create",[4]);',
  12408. '$mod.Obj.vI = $mod.Obj.vI + 5;'
  12409. ]));
  12410. end;
  12411. procedure TTestModule.TestClass_Method;
  12412. begin
  12413. StartProgram(false);
  12414. Add('type');
  12415. Add(' TObject = class');
  12416. Add(' public');
  12417. Add(' vI: longint;');
  12418. Add(' Sub: TObject;');
  12419. Add(' constructor Create;');
  12420. Add(' function GetIt(Par: longint): tobject;');
  12421. Add(' end;');
  12422. Add('constructor tobject.create; begin end;');
  12423. Add('function tobject.getit(par: longint): tobject;');
  12424. Add('begin');
  12425. Add(' Self.vi:=par+3;');
  12426. Add(' Result:=self.sub;');
  12427. Add('end;');
  12428. Add('var Obj: tobject;');
  12429. Add('begin');
  12430. Add(' obj:=tobject.create;');
  12431. Add(' obj.getit(4);');
  12432. Add(' obj.sub.sub:=nil;');
  12433. Add(' obj.sub.getit(5);');
  12434. Add(' obj.sub.getit(6).SUB:=nil;');
  12435. Add(' obj.sub.getit(7).GETIT(8);');
  12436. Add(' obj.sub.getit(9).SuB.getit(10);');
  12437. ConvertProgram;
  12438. CheckSource('TestClass_Method',
  12439. LinesToStr([ // statements
  12440. 'rtl.createClass(this,"TObject",null,function(){',
  12441. ' this.$init = function () {',
  12442. ' this.vI = 0;',
  12443. ' this.Sub = null;',
  12444. ' };',
  12445. ' this.$final = function () {',
  12446. ' this.Sub = undefined;',
  12447. ' };',
  12448. ' this.Create = function(){',
  12449. ' return this;',
  12450. ' };',
  12451. ' this.GetIt = function(Par){',
  12452. ' var Result = null;',
  12453. ' this.vI = Par + 3;',
  12454. ' Result = this.Sub;',
  12455. ' return Result;',
  12456. ' };',
  12457. '});',
  12458. 'this.Obj = null;'
  12459. ]),
  12460. LinesToStr([ // $mod.$main
  12461. '$mod.Obj = $mod.TObject.$create("Create");',
  12462. '$mod.Obj.GetIt(4);',
  12463. '$mod.Obj.Sub.Sub=null;',
  12464. '$mod.Obj.Sub.GetIt(5);',
  12465. '$mod.Obj.Sub.GetIt(6).Sub=null;',
  12466. '$mod.Obj.Sub.GetIt(7).GetIt(8);',
  12467. '$mod.Obj.Sub.GetIt(9).Sub.GetIt(10);'
  12468. ]));
  12469. end;
  12470. procedure TTestModule.TestClass_Implementation;
  12471. begin
  12472. StartUnit(false);
  12473. Add([
  12474. 'interface',
  12475. 'type',
  12476. ' TObject = class',
  12477. ' constructor Create;',
  12478. ' end;',
  12479. 'implementation',
  12480. 'type',
  12481. ' TIntClass = class',
  12482. ' constructor Create; reintroduce;',
  12483. ' class procedure DoGlob;',
  12484. ' end;',
  12485. 'constructor tintclass.create;',
  12486. 'begin',
  12487. ' inherited;',
  12488. ' inherited create;',
  12489. ' doglob;',
  12490. 'end;',
  12491. 'class procedure tintclass.doglob;',
  12492. 'begin',
  12493. 'end;',
  12494. 'constructor tobject.create;',
  12495. 'var',
  12496. ' iC: tintclass;',
  12497. 'begin',
  12498. ' ic:=tintclass.create;',
  12499. ' tintclass.doglob;',
  12500. ' ic.doglob;',
  12501. 'end;',
  12502. 'initialization',
  12503. ' tintclass.doglob;',
  12504. '']);
  12505. ConvertUnit;
  12506. CheckSource('TestClass_Implementation',
  12507. LinesToStr([ // statements
  12508. 'var $impl = $mod.$impl;',
  12509. 'rtl.createClass(this, "TObject", null, function () {',
  12510. ' this.$init = function () {',
  12511. ' };',
  12512. ' this.$final = function () {',
  12513. ' };',
  12514. ' this.Create = function () {',
  12515. ' var iC = null;',
  12516. ' iC = $impl.TIntClass.$create("Create$1");',
  12517. ' $impl.TIntClass.DoGlob();',
  12518. ' iC.$class.DoGlob();',
  12519. ' return this;',
  12520. ' };',
  12521. '});',
  12522. '']),
  12523. LinesToStr([ // $mod.$main
  12524. '$impl.TIntClass.DoGlob();',
  12525. '']),
  12526. LinesToStr([
  12527. 'rtl.createClass($impl, "TIntClass", $mod.TObject, function () {',
  12528. ' this.Create$1 = function () {',
  12529. ' $mod.TObject.Create.call(this);',
  12530. ' $mod.TObject.Create.call(this);',
  12531. ' this.$class.DoGlob();',
  12532. ' return this;',
  12533. ' };',
  12534. ' this.DoGlob = function () {',
  12535. ' };',
  12536. '});',
  12537. '']));
  12538. end;
  12539. procedure TTestModule.TestClass_Inheritance;
  12540. begin
  12541. StartProgram(false);
  12542. Add('type');
  12543. Add(' TObject = class');
  12544. Add(' public');
  12545. Add(' constructor Create;');
  12546. Add(' end;');
  12547. Add(' TClassA = class');
  12548. Add(' end;');
  12549. Add(' TClassB = class(TObject)');
  12550. Add(' procedure ProcB;');
  12551. Add(' end;');
  12552. Add('constructor tobject.create; begin end;');
  12553. Add('procedure tclassb.procb; begin end;');
  12554. Add('var');
  12555. Add(' oO: TObject;');
  12556. Add(' oA: TClassA;');
  12557. Add(' oB: TClassB;');
  12558. Add('begin');
  12559. Add(' oO:=tobject.Create;');
  12560. Add(' oA:=tclassa.Create;');
  12561. Add(' ob:=tclassb.Create;');
  12562. Add(' if oo is tclassa then ;');
  12563. Add(' ob:=oo as tclassb;');
  12564. Add(' (oo as tclassb).procb;');
  12565. ConvertProgram;
  12566. CheckSource('TestClass_Inheritance',
  12567. LinesToStr([ // statements
  12568. 'rtl.createClass(this,"TObject",null,function(){',
  12569. ' this.$init = function () {',
  12570. ' };',
  12571. ' this.$final = function () {',
  12572. ' };',
  12573. ' this.Create = function () {',
  12574. ' return this;',
  12575. ' };',
  12576. '});',
  12577. 'rtl.createClass(this,"TClassA",this.TObject,function(){',
  12578. '});',
  12579. 'rtl.createClass(this,"TClassB",this.TObject,function(){',
  12580. ' this.ProcB = function () {',
  12581. ' };',
  12582. '});',
  12583. 'this.oO = null;',
  12584. 'this.oA = null;',
  12585. 'this.oB = null;'
  12586. ]),
  12587. LinesToStr([ // $mod.$main
  12588. '$mod.oO = $mod.TObject.$create("Create");',
  12589. '$mod.oA = $mod.TClassA.$create("Create");',
  12590. '$mod.oB = $mod.TClassB.$create("Create");',
  12591. 'if ($mod.TClassA.isPrototypeOf($mod.oO));',
  12592. '$mod.oB = rtl.as($mod.oO, $mod.TClassB);',
  12593. 'rtl.as($mod.oO, $mod.TClassB).ProcB();'
  12594. ]));
  12595. end;
  12596. procedure TTestModule.TestClass_TypeAlias;
  12597. begin
  12598. StartProgram(false);
  12599. Add([
  12600. '{$interfaces corba}',
  12601. 'type',
  12602. ' IObject = interface',
  12603. ' end;',
  12604. ' IBird = type IObject;',
  12605. ' TObject = class',
  12606. ' end;',
  12607. ' TBird = type TObject;',
  12608. 'var',
  12609. ' oObj: TObject;',
  12610. ' oBird: TBird;',
  12611. ' IntfObj: IObject;',
  12612. ' IntfBird: IBird;',
  12613. 'begin',
  12614. ' oObj:=oBird;',
  12615. '']);
  12616. ConvertProgram;
  12617. CheckSource('TestClass_TypeAlias',
  12618. LinesToStr([ // statements
  12619. 'rtl.createInterface(this, "IObject", "{B92D5841-6F2A-306A-8000-000000000000}", [], null);',
  12620. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-387B-AE88-F10981585074}", [], this.IObject);',
  12621. 'rtl.createClass(this, "TObject", null, function () {',
  12622. ' this.$init = function () {',
  12623. ' };',
  12624. ' this.$final = function () {',
  12625. ' };',
  12626. '});',
  12627. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  12628. '});',
  12629. 'this.oObj = null;',
  12630. 'this.oBird = null;',
  12631. 'this.IntfObj = null;',
  12632. 'this.IntfBird = null;',
  12633. '']),
  12634. LinesToStr([ // $mod.$main
  12635. '$mod.oObj = $mod.oBird;',
  12636. '']));
  12637. end;
  12638. procedure TTestModule.TestClass_AbstractMethod;
  12639. begin
  12640. StartProgram(false);
  12641. Add('type');
  12642. Add(' TObject = class');
  12643. Add(' public');
  12644. Add(' procedure DoIt; virtual; abstract;');
  12645. Add(' end;');
  12646. Add('begin');
  12647. ConvertProgram;
  12648. CheckSource('TestClass_AbstractMethod',
  12649. LinesToStr([ // statements
  12650. 'rtl.createClass(this,"TObject",null,function(){',
  12651. ' this.$init = function () {',
  12652. ' };',
  12653. ' this.$final = function () {',
  12654. ' };',
  12655. '});'
  12656. ]),
  12657. LinesToStr([ // this.$main
  12658. ''
  12659. ]));
  12660. end;
  12661. procedure TTestModule.TestClass_CallInherited_ProcNoParams;
  12662. begin
  12663. StartProgram(false);
  12664. Add([
  12665. 'type',
  12666. ' TObject = class',
  12667. ' procedure DoAbstract; virtual; abstract;',
  12668. ' procedure DoVirtual; virtual;',
  12669. ' procedure DoIt;',
  12670. ' end;',
  12671. ' TA = class',
  12672. ' procedure doabstract; override;',
  12673. ' procedure dovirtual; override;',
  12674. ' procedure DoSome;',
  12675. ' end;',
  12676. 'procedure tobject.dovirtual;',
  12677. 'begin',
  12678. ' inherited; // call non existing ancestor -> ignore silently',
  12679. 'end;',
  12680. 'procedure tobject.doit;',
  12681. 'begin',
  12682. 'end;',
  12683. 'procedure ta.doabstract;',
  12684. 'begin',
  12685. ' inherited dovirtual; // call TObject.DoVirtual',
  12686. 'end;',
  12687. 'procedure ta.dovirtual;',
  12688. 'begin',
  12689. ' inherited; // call TObject.DoVirtual',
  12690. ' inherited dovirtual; // call TObject.DoVirtual',
  12691. ' inherited dovirtual(); // call TObject.DoVirtual',
  12692. ' doit;',
  12693. ' doit();',
  12694. 'end;',
  12695. 'procedure ta.dosome;',
  12696. 'begin',
  12697. ' inherited; // call non existing ancestor method -> silently ignore',
  12698. 'end;',
  12699. 'begin']);
  12700. ConvertProgram;
  12701. CheckSource('TestClass_CallInherited_ProcNoParams',
  12702. LinesToStr([ // statements
  12703. 'rtl.createClass(this,"TObject",null,function(){',
  12704. ' this.$init = function () {',
  12705. ' };',
  12706. ' this.$final = function () {',
  12707. ' };',
  12708. ' this.DoVirtual = function () {',
  12709. ' };',
  12710. ' this.DoIt = function () {',
  12711. ' };',
  12712. '});',
  12713. 'rtl.createClass(this, "TA", this.TObject, function () {',
  12714. ' this.DoAbstract = function () {',
  12715. ' $mod.TObject.DoVirtual.call(this);',
  12716. ' };',
  12717. ' this.DoVirtual = function () {',
  12718. ' $mod.TObject.DoVirtual.call(this);',
  12719. ' $mod.TObject.DoVirtual.call(this);',
  12720. ' $mod.TObject.DoVirtual.call(this);',
  12721. ' this.DoIt();',
  12722. ' this.DoIt();',
  12723. ' };',
  12724. ' this.DoSome = function () {',
  12725. ' };',
  12726. '});'
  12727. ]),
  12728. LinesToStr([ // this.$main
  12729. ''
  12730. ]));
  12731. end;
  12732. procedure TTestModule.TestClass_CallInherited_WithParams;
  12733. begin
  12734. StartProgram(false);
  12735. Add([
  12736. 'type',
  12737. ' TObject = class',
  12738. ' procedure DoAbstract(pA: longint; pB: longint = 0); virtual; abstract;',
  12739. ' procedure DoVirtual(pA: longint; pB: longint = 0); virtual;',
  12740. ' procedure DoIt(pA: longint; pB: longint = 0);',
  12741. ' procedure DoIt2(pA: longint = 1; pB: longint = 2);',
  12742. ' function GetIt(pA: longint = 1; pB: longint = 2): longint;',
  12743. ' end;',
  12744. ' TClassA = class',
  12745. ' procedure DoAbstract(pA: longint; pB: longint = 0); override;',
  12746. ' procedure DoVirtual(pA: longint; pB: longint = 0); override;',
  12747. ' function GetIt(pA: longint = 1; pB: longint = 2): longint;',
  12748. ' end;',
  12749. 'procedure tobject.dovirtual(pa: longint; pb: longint = 0);',
  12750. 'begin',
  12751. 'end;',
  12752. 'procedure tobject.doit(pa: longint; pb: longint = 0);',
  12753. 'begin',
  12754. 'end;',
  12755. 'procedure tobject.doit2(pa: longint; pb: longint = 0);',
  12756. 'begin',
  12757. 'end;',
  12758. 'function tobject.getit(pa: longint; pb: longint = 0): longint;',
  12759. 'begin',
  12760. 'end;',
  12761. 'procedure tclassa.doabstract(pa: longint; pb: longint = 0);',
  12762. 'begin',
  12763. ' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)',
  12764. ' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)',
  12765. 'end;',
  12766. 'procedure tclassa.dovirtual(pa: longint; pb: longint = 0);',
  12767. 'begin',
  12768. ' inherited; // call TObject.DoVirtual(pA,pB)',
  12769. ' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)',
  12770. ' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)',
  12771. ' doit(pa,pb);',
  12772. ' doit(pa);',
  12773. ' doit2(pa);',
  12774. ' doit2;',
  12775. 'end;',
  12776. 'function tclassa.getit(pa: longint; pb: longint = 0): longint;',
  12777. 'begin',
  12778. ' pa:=inherited;',
  12779. 'end;',
  12780. 'begin']);
  12781. ConvertProgram;
  12782. CheckSource('TestClass_CallInherited_WithParams',
  12783. LinesToStr([ // statements
  12784. 'rtl.createClass(this,"TObject",null,function(){',
  12785. ' this.$init = function () {',
  12786. ' };',
  12787. ' this.$final = function () {',
  12788. ' };',
  12789. ' this.DoVirtual = function (pA,pB) {',
  12790. ' };',
  12791. ' this.DoIt = function (pA,pB) {',
  12792. ' };',
  12793. ' this.DoIt2 = function (pA,pB) {',
  12794. ' };',
  12795. ' this.GetIt = function (pA, pB) {',
  12796. ' var Result = 0;',
  12797. ' return Result;',
  12798. ' };',
  12799. '});',
  12800. 'rtl.createClass(this, "TClassA", this.TObject, function () {',
  12801. ' this.DoAbstract = function (pA,pB) {',
  12802. ' $mod.TObject.DoVirtual.call(this,pA,pB);',
  12803. ' $mod.TObject.DoVirtual.call(this,pA,0);',
  12804. ' };',
  12805. ' this.DoVirtual = function (pA,pB) {',
  12806. ' $mod.TObject.DoVirtual.apply(this, arguments);',
  12807. ' $mod.TObject.DoVirtual.call(this,pA,pB);',
  12808. ' $mod.TObject.DoVirtual.call(this,pA,0);',
  12809. ' this.DoIt(pA,pB);',
  12810. ' this.DoIt(pA,0);',
  12811. ' this.DoIt2(pA,2);',
  12812. ' this.DoIt2(1,2);',
  12813. ' };',
  12814. ' this.GetIt$1 = function (pA, pB) {',
  12815. ' var Result = 0;',
  12816. ' pA = $mod.TObject.GetIt.apply(this, arguments);',
  12817. ' return Result;',
  12818. ' };',
  12819. '});'
  12820. ]),
  12821. LinesToStr([ // this.$main
  12822. ''
  12823. ]));
  12824. end;
  12825. procedure TTestModule.TestClasS_CallInheritedConstructor;
  12826. begin
  12827. StartProgram(false);
  12828. Add('type');
  12829. Add(' TObject = class');
  12830. Add(' constructor Create; virtual;');
  12831. Add(' constructor CreateWithB(b: boolean);');
  12832. Add(' end;');
  12833. Add(' TA = class');
  12834. Add(' constructor Create; override;');
  12835. Add(' constructor CreateWithC(c: char);');
  12836. Add(' procedure DoIt;');
  12837. Add(' class function DoSome: TObject;');
  12838. Add(' end;');
  12839. Add('constructor tobject.create;');
  12840. Add('begin');
  12841. Add(' inherited; // call non existing ancestor -> ignore silently');
  12842. Add('end;');
  12843. Add('constructor tobject.createwithb(b: boolean);');
  12844. Add('begin');
  12845. Add(' inherited; // call non existing ancestor -> ignore silently');
  12846. Add(' create; // normal call');
  12847. Add('end;');
  12848. Add('constructor ta.create;');
  12849. Add('begin');
  12850. Add(' inherited; // normal call TObject.Create');
  12851. Add(' inherited create; // normal call TObject.Create');
  12852. Add(' inherited createwithb(false); // normal call TObject.CreateWithB');
  12853. Add('end;');
  12854. Add('constructor ta.createwithc(c: char);');
  12855. Add('begin');
  12856. Add(' inherited create; // call TObject.Create');
  12857. Add(' inherited createwithb(true); // call TObject.CreateWithB');
  12858. Add(' doit;');
  12859. Add(' doit();');
  12860. Add(' dosome;');
  12861. Add('end;');
  12862. Add('procedure ta.doit;');
  12863. Add('begin');
  12864. Add(' create; // normal call');
  12865. Add(' createwithb(false); // normal call');
  12866. Add(' createwithc(''c''); // normal call');
  12867. Add('end;');
  12868. Add('class function ta.dosome: TObject;');
  12869. Add('begin');
  12870. Add(' Result:=create; // constructor');
  12871. Add(' Result:=createwithb(true); // constructor');
  12872. Add(' Result:=createwithc(''c''); // constructor');
  12873. Add('end;');
  12874. Add('begin');
  12875. ConvertProgram;
  12876. CheckSource('TestClass_CallInheritedConstructor',
  12877. LinesToStr([ // statements
  12878. 'rtl.createClass(this,"TObject",null,function(){',
  12879. ' this.$init = function () {',
  12880. ' };',
  12881. ' this.$final = function () {',
  12882. ' };',
  12883. ' this.Create = function () {',
  12884. ' return this;',
  12885. ' };',
  12886. ' this.CreateWithB = function (b) {',
  12887. ' this.Create();',
  12888. ' return this;',
  12889. ' };',
  12890. '});',
  12891. 'rtl.createClass(this, "TA", this.TObject, function () {',
  12892. ' this.Create = function () {',
  12893. ' $mod.TObject.Create.call(this);',
  12894. ' $mod.TObject.Create.call(this);',
  12895. ' $mod.TObject.CreateWithB.call(this, false);',
  12896. ' return this;',
  12897. ' };',
  12898. ' this.CreateWithC = function (c) {',
  12899. ' $mod.TObject.Create.call(this);',
  12900. ' $mod.TObject.CreateWithB.call(this, true);',
  12901. ' this.DoIt();',
  12902. ' this.DoIt();',
  12903. ' this.$class.DoSome();',
  12904. ' return this;',
  12905. ' };',
  12906. ' this.DoIt = function () {',
  12907. ' this.Create();',
  12908. ' this.CreateWithB(false);',
  12909. ' this.CreateWithC("c");',
  12910. ' };',
  12911. ' this.DoSome = function () {',
  12912. ' var Result = null;',
  12913. ' Result = this.$create("Create");',
  12914. ' Result = this.$create("CreateWithB", [true]);',
  12915. ' Result = this.$create("CreateWithC", ["c"]);',
  12916. ' return Result;',
  12917. ' };',
  12918. '});'
  12919. ]),
  12920. LinesToStr([ // this.$main
  12921. ''
  12922. ]));
  12923. end;
  12924. procedure TTestModule.TestClass_ClassVar_Assign;
  12925. begin
  12926. StartProgram(false);
  12927. Add([
  12928. 'type',
  12929. ' TObject = class',
  12930. ' public',
  12931. ' class var vI: longint;',
  12932. ' class var Sub: TObject;',
  12933. ' constructor Create;',
  12934. ' class function GetIt(var Par: longint): tobject;',
  12935. ' end;',
  12936. 'constructor tobject.create;',
  12937. 'begin',
  12938. ' vi:=vi+1;',
  12939. ' Self.vi:=Self.vi+1;',
  12940. ' inc(vi);',
  12941. 'end;',
  12942. 'class function tobject.getit(var par: longint): tobject;',
  12943. 'begin',
  12944. ' vi:=vi+3;',
  12945. ' Self.vi:=Self.vi+4;',
  12946. ' inc(vi);',
  12947. ' Result:=self.sub;',
  12948. ' GetIt(vi);',
  12949. 'end;',
  12950. 'var Obj: tobject;',
  12951. 'begin',
  12952. ' obj:=tobject.create;',
  12953. ' tobject.vi:=3;',
  12954. ' if tobject.vi=4 then ;',
  12955. ' tobject.sub:=nil;',
  12956. ' obj.sub:=nil;',
  12957. ' obj.sub.sub:=nil;']);
  12958. ConvertProgram;
  12959. CheckSource('TestClass_ClassVar_Assign',
  12960. LinesToStr([ // statements
  12961. 'rtl.createClass(this,"TObject",null,function(){',
  12962. ' this.vI = 0;',
  12963. ' this.Sub = null;',
  12964. ' this.$init = function () {',
  12965. ' };',
  12966. ' this.$final = function () {',
  12967. ' };',
  12968. ' this.Create = function(){',
  12969. ' $mod.TObject.vI = this.vI+1;',
  12970. ' $mod.TObject.vI = this.vI+1;',
  12971. ' $mod.TObject.vI += 1;',
  12972. ' return this;',
  12973. ' };',
  12974. ' this.GetIt = function(Par){',
  12975. ' var Result = null;',
  12976. ' $mod.TObject.vI = this.vI + 3;',
  12977. ' $mod.TObject.vI = this.vI + 4;',
  12978. ' $mod.TObject.vI += 1;',
  12979. ' Result = this.Sub;',
  12980. ' this.GetIt({',
  12981. ' p: $mod.TObject,',
  12982. ' get: function () {',
  12983. ' return this.p.vI;',
  12984. ' },',
  12985. ' set: function (v) {',
  12986. ' this.p.vI = v;',
  12987. ' }',
  12988. ' });',
  12989. ' return Result;',
  12990. ' };',
  12991. '});',
  12992. 'this.Obj = null;'
  12993. ]),
  12994. LinesToStr([ // $mod.$main
  12995. '$mod.Obj = $mod.TObject.$create("Create");',
  12996. '$mod.TObject.vI = 3;',
  12997. 'if ($mod.TObject.vI === 4);',
  12998. '$mod.TObject.Sub=null;',
  12999. '$mod.TObject.Sub=null;',
  13000. '$mod.TObject.Sub=null;',
  13001. '']));
  13002. end;
  13003. procedure TTestModule.TestClass_CallClassMethod;
  13004. begin
  13005. StartProgram(false);
  13006. Add('type');
  13007. Add(' TObject = class');
  13008. Add(' public');
  13009. Add(' class var vI: longint;');
  13010. Add(' class var Sub: TObject;');
  13011. Add(' constructor Create;');
  13012. Add(' function GetMore(Par: longint): longint;');
  13013. Add(' class function GetIt(Par: longint): tobject;');
  13014. Add(' end;');
  13015. Add('constructor tobject.create;');
  13016. Add('begin');
  13017. Add(' sub:=getit(3);');
  13018. Add(' vi:=getmore(4);');
  13019. Add(' sub:=Self.getit(5);');
  13020. Add(' vi:=Self.getmore(6);');
  13021. Add('end;');
  13022. Add('function tobject.getmore(par: longint): longint;');
  13023. Add('begin');
  13024. Add(' sub:=getit(11);');
  13025. Add(' vi:=getmore(12);');
  13026. Add(' sub:=self.getit(13);');
  13027. Add(' vi:=self.getmore(14);');
  13028. Add('end;');
  13029. Add('class function tobject.getit(par: longint): tobject;');
  13030. Add('begin');
  13031. Add(' sub:=getit(21);');
  13032. Add(' vi:=sub.getmore(22);');
  13033. Add(' sub:=self.getit(23);');
  13034. Add(' vi:=self.sub.getmore(24);');
  13035. Add('end;');
  13036. Add('var Obj: tobject;');
  13037. Add('begin');
  13038. Add(' obj:=tobject.create;');
  13039. Add(' tobject.getit(5);');
  13040. Add(' obj.getit(6);');
  13041. Add(' obj.sub.getit(7);');
  13042. Add(' obj.sub.getit(8).SUB:=nil;');
  13043. Add(' obj.sub.getit(9).GETIT(10);');
  13044. Add(' obj.sub.getit(11).SuB.getit(12);');
  13045. ConvertProgram;
  13046. CheckSource('TestClass_CallClassMethod',
  13047. LinesToStr([ // statements
  13048. 'rtl.createClass(this,"TObject",null,function(){',
  13049. ' this.vI = 0;',
  13050. ' this.Sub = null;',
  13051. ' this.$init = function () {',
  13052. ' };',
  13053. ' this.$final = function () {',
  13054. ' };',
  13055. ' this.Create = function(){',
  13056. ' $mod.TObject.Sub = this.$class.GetIt(3);',
  13057. ' $mod.TObject.vI = this.GetMore(4);',
  13058. ' $mod.TObject.Sub = this.$class.GetIt(5);',
  13059. ' $mod.TObject.vI = this.GetMore(6);',
  13060. ' return this;',
  13061. ' };',
  13062. ' this.GetMore = function(Par){',
  13063. ' var Result = 0;',
  13064. ' $mod.TObject.Sub = this.$class.GetIt(11);',
  13065. ' $mod.TObject.vI = this.GetMore(12);',
  13066. ' $mod.TObject.Sub = this.$class.GetIt(13);',
  13067. ' $mod.TObject.vI = this.GetMore(14);',
  13068. ' return Result;',
  13069. ' };',
  13070. ' this.GetIt = function(Par){',
  13071. ' var Result = null;',
  13072. ' $mod.TObject.Sub = this.GetIt(21);',
  13073. ' $mod.TObject.vI = this.Sub.GetMore(22);',
  13074. ' $mod.TObject.Sub = this.GetIt(23);',
  13075. ' $mod.TObject.vI = this.Sub.GetMore(24);',
  13076. ' return Result;',
  13077. ' };',
  13078. '});',
  13079. 'this.Obj = null;'
  13080. ]),
  13081. LinesToStr([ // $mod.$main
  13082. '$mod.Obj = $mod.TObject.$create("Create");',
  13083. '$mod.TObject.GetIt(5);',
  13084. '$mod.Obj.$class.GetIt(6);',
  13085. '$mod.Obj.Sub.$class.GetIt(7);',
  13086. '$mod.TObject.Sub=null;',
  13087. '$mod.Obj.Sub.$class.GetIt(9).$class.GetIt(10);',
  13088. '$mod.Obj.Sub.$class.GetIt(11).Sub.$class.GetIt(12);',
  13089. '']));
  13090. end;
  13091. procedure TTestModule.TestClass_CallClassMethodStatic;
  13092. begin
  13093. StartProgram(false);
  13094. Add([
  13095. 'type',
  13096. ' TObject = class',
  13097. ' public',
  13098. ' class function Fly: tobject; static;',
  13099. ' end;',
  13100. 'class function tobject.Fly: tobject;',
  13101. 'begin',
  13102. ' Result.Fly;',
  13103. ' Result.Fly();',
  13104. ' Fly;',
  13105. ' Fly();',
  13106. ' Fly.Fly;',
  13107. ' Fly.Fly();',
  13108. 'end;',
  13109. 'var Obj: tobject;',
  13110. 'begin',
  13111. ' obj.Fly;',
  13112. ' obj.Fly();',
  13113. ' with obj do begin',
  13114. ' Fly;',
  13115. ' Fly();',
  13116. ' end;',
  13117. '']);
  13118. ConvertProgram;
  13119. CheckSource('TestClass_CallClassMethodStatic',
  13120. LinesToStr([ // statements
  13121. 'rtl.createClass(this, "TObject", null, function () {',
  13122. ' this.$init = function () {',
  13123. ' };',
  13124. ' this.$final = function () {',
  13125. ' };',
  13126. ' this.Fly = function () {',
  13127. ' var Result = null;',
  13128. ' $mod.TObject.Fly();',
  13129. ' $mod.TObject.Fly();',
  13130. ' $mod.TObject.Fly();',
  13131. ' $mod.TObject.Fly();',
  13132. ' $mod.TObject.Fly();',
  13133. ' $mod.TObject.Fly();',
  13134. ' return Result;',
  13135. ' };',
  13136. '});',
  13137. 'this.Obj = null;'
  13138. ]),
  13139. LinesToStr([ // $mod.$main
  13140. '$mod.TObject.Fly();',
  13141. '$mod.TObject.Fly();',
  13142. 'var $with = $mod.Obj;',
  13143. '$with.Fly();',
  13144. '$with.Fly();',
  13145. '']));
  13146. end;
  13147. procedure TTestModule.TestClass_Property;
  13148. begin
  13149. StartProgram(false);
  13150. Add('type');
  13151. Add(' TObject = class');
  13152. Add(' Fx: longint;');
  13153. Add(' Fy: longint;');
  13154. Add(' function GetInt: longint;');
  13155. Add(' procedure SetInt(Value: longint);');
  13156. Add(' procedure DoIt;');
  13157. Add(' property IntA: longint read Fx write Fy;');
  13158. Add(' property IntB: longint read GetInt write SetInt;');
  13159. Add(' end;');
  13160. Add('function tobject.getint: longint;');
  13161. Add('begin');
  13162. Add(' result:=fx;');
  13163. Add('end;');
  13164. Add('procedure tobject.setint(value: longint);');
  13165. Add('begin');
  13166. Add(' if value=fy then exit;');
  13167. Add(' fy:=value;');
  13168. Add('end;');
  13169. Add('procedure tobject.doit;');
  13170. Add('begin');
  13171. Add(' IntA:=IntA+1;');
  13172. Add(' Self.IntA:=Self.IntA+1;');
  13173. Add(' IntB:=IntB+1;');
  13174. Add(' Self.IntB:=Self.IntB+1;');
  13175. Add('end;');
  13176. Add('var Obj: tobject;');
  13177. Add('begin');
  13178. Add(' obj.inta:=obj.inta+1;');
  13179. Add(' if obj.intb=2 then;');
  13180. Add(' obj.intb:=obj.intb+2;');
  13181. Add(' obj.setint(obj.inta);');
  13182. ConvertProgram;
  13183. CheckSource('TestClass_Property',
  13184. LinesToStr([ // statements
  13185. 'rtl.createClass(this, "TObject", null, function () {',
  13186. ' this.$init = function () {',
  13187. ' this.Fx = 0;',
  13188. ' this.Fy = 0;',
  13189. ' };',
  13190. ' this.$final = function () {',
  13191. ' };',
  13192. ' this.GetInt = function () {',
  13193. ' var Result = 0;',
  13194. ' Result = this.Fx;',
  13195. ' return Result;',
  13196. ' };',
  13197. ' this.SetInt = function (Value) {',
  13198. ' if (Value === this.Fy) return;',
  13199. ' this.Fy = Value;',
  13200. ' };',
  13201. ' this.DoIt = function () {',
  13202. ' this.Fy = this.Fx + 1;',
  13203. ' this.Fy = this.Fx + 1;',
  13204. ' this.SetInt(this.GetInt() + 1);',
  13205. ' this.SetInt(this.GetInt() + 1);',
  13206. ' };',
  13207. '});',
  13208. 'this.Obj = null;'
  13209. ]),
  13210. LinesToStr([ // $mod.$main
  13211. '$mod.Obj.Fy = $mod.Obj.Fx + 1;',
  13212. 'if ($mod.Obj.GetInt() === 2);',
  13213. '$mod.Obj.SetInt($mod.Obj.GetInt() + 2);',
  13214. '$mod.Obj.SetInt($mod.Obj.Fx);'
  13215. ]));
  13216. end;
  13217. procedure TTestModule.TestClass_Property_ClassMethod;
  13218. begin
  13219. StartProgram(false);
  13220. Add([
  13221. 'type',
  13222. ' TObject = class',
  13223. ' class var Fx: longint;',
  13224. ' class var Fy: longint;',
  13225. ' class function GetInt: longint;',
  13226. ' class procedure SetInt(Value: longint);',
  13227. ' end;',
  13228. ' TBird = class',
  13229. ' class procedure DoIt;',
  13230. ' class property IntA: longint read Fx write Fy;',
  13231. ' class property IntB: longint read GetInt write SetInt;',
  13232. ' end;',
  13233. 'class function tobject.getint: longint;',
  13234. 'begin',
  13235. ' result:=fx;',
  13236. 'end;',
  13237. 'class procedure tobject.setint(value: longint);',
  13238. 'begin',
  13239. 'end;',
  13240. 'class procedure tbird.doit;',
  13241. 'begin',
  13242. ' FX:=3;',
  13243. ' IntA:=IntA+1;',
  13244. ' Self.IntA:=Self.IntA+1;',
  13245. ' IntB:=IntB+1;',
  13246. ' Self.IntB:=Self.IntB+1;',
  13247. ' with Self do begin',
  13248. ' FX:=11;',
  13249. ' IntA:=IntA+12;',
  13250. ' IntB:=IntB+13;',
  13251. ' end;',
  13252. 'end;',
  13253. 'var Obj: tbird;',
  13254. 'begin',
  13255. ' tbird.fx:=tbird.fx+1;',
  13256. ' tbird.inta:=tbird.inta+1;',
  13257. ' if tbird.intb=2 then;',
  13258. ' tbird.intb:=tbird.intb+2;',
  13259. ' tbird.setint(tbird.inta);',
  13260. ' obj.inta:=obj.inta+1;',
  13261. ' if obj.intb=2 then;',
  13262. ' obj.intb:=obj.intb+2;',
  13263. ' obj.setint(obj.inta);',
  13264. ' with Tbird do begin',
  13265. ' FX:=FY+1;',
  13266. ' inta:=inta+2;',
  13267. ' intb:=intb+3;',
  13268. ' end;',
  13269. ' with Obj do begin',
  13270. ' FX:=FY+1;',
  13271. ' inta:=inta+2;',
  13272. ' intb:=intb+3;',
  13273. ' end;',
  13274. '']);
  13275. ConvertProgram;
  13276. CheckSource('TestClass_Property_ClassMethod',
  13277. LinesToStr([ // statements
  13278. 'rtl.createClass(this, "TObject", null, function () {',
  13279. ' this.Fx = 0;',
  13280. ' this.Fy = 0;',
  13281. ' this.$init = function () {',
  13282. ' };',
  13283. ' this.$final = function () {',
  13284. ' };',
  13285. ' this.GetInt = function () {',
  13286. ' var Result = 0;',
  13287. ' Result = this.Fx;',
  13288. ' return Result;',
  13289. ' };',
  13290. ' this.SetInt = function (Value) {',
  13291. ' };',
  13292. '});',
  13293. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  13294. ' this.DoIt = function () {',
  13295. ' $mod.TObject.Fx = 3;',
  13296. ' $mod.TObject.Fy = this.Fx + 1;',
  13297. ' $mod.TObject.Fy = this.Fx + 1;',
  13298. ' this.SetInt(this.GetInt() + 1);',
  13299. ' this.SetInt(this.GetInt() + 1);',
  13300. ' $mod.TObject.Fx = 11;',
  13301. ' $mod.TObject.Fy = this.Fx + 12;',
  13302. ' this.SetInt(this.GetInt() + 13);',
  13303. ' };',
  13304. '});',
  13305. 'this.Obj = null;'
  13306. ]),
  13307. LinesToStr([ // $mod.$main
  13308. '$mod.TObject.Fx = $mod.TBird.Fx + 1;',
  13309. '$mod.TObject.Fy = $mod.TBird.Fx + 1;',
  13310. 'if ($mod.TBird.GetInt() === 2);',
  13311. '$mod.TBird.SetInt($mod.TBird.GetInt() + 2);',
  13312. '$mod.TBird.SetInt($mod.TBird.Fx);',
  13313. '$mod.TObject.Fy = $mod.Obj.Fx + 1;',
  13314. 'if ($mod.Obj.$class.GetInt() === 2);',
  13315. '$mod.Obj.$class.SetInt($mod.Obj.$class.GetInt() + 2);',
  13316. '$mod.Obj.$class.SetInt($mod.Obj.Fx);',
  13317. 'var $with = $mod.TBird;',
  13318. '$mod.TObject.Fx = $with.Fy + 1;',
  13319. '$mod.TObject.Fy = $with.Fx + 2;',
  13320. '$with.SetInt($with.GetInt() + 3);',
  13321. 'var $with1 = $mod.Obj;',
  13322. '$mod.TObject.Fx = $with1.Fy + 1;',
  13323. '$mod.TObject.Fy = $with1.Fx + 2;',
  13324. '$with1.$class.SetInt($with1.$class.GetInt() + 3);',
  13325. '']));
  13326. end;
  13327. procedure TTestModule.TestClass_Property_Indexed;
  13328. begin
  13329. StartProgram(false);
  13330. Add([
  13331. 'type',
  13332. ' TObject = class',
  13333. ' FItems: array of longint;',
  13334. ' function GetItems(Index: longint): longint;',
  13335. ' procedure SetItems(Index: longint; Value: longint);',
  13336. ' procedure DoIt;',
  13337. ' property Items[Index: longint]: longint read getitems write setitems;',
  13338. ' end;',
  13339. 'function tobject.getitems(index: longint): longint;',
  13340. 'begin',
  13341. ' Result:=fitems[index];',
  13342. 'end;',
  13343. 'procedure tobject.setitems(index: longint; value: longint);',
  13344. 'begin',
  13345. ' fitems[index]:=value;',
  13346. 'end;',
  13347. 'procedure tobject.doit;',
  13348. 'begin',
  13349. ' items[1]:=2;',
  13350. ' items[3]:=items[4];',
  13351. ' self.items[5]:=self.items[6];',
  13352. ' items[items[7]]:=items[items[8]];',
  13353. 'end;',
  13354. 'var Obj: tobject;',
  13355. 'begin',
  13356. ' obj.Items[11]:=obj.Items[12];',
  13357. '']);
  13358. ConvertProgram;
  13359. CheckSource('TestClass_Property_Indexed',
  13360. LinesToStr([ // statements
  13361. 'rtl.createClass(this, "TObject", null, function () {',
  13362. ' this.$init = function () {',
  13363. ' this.FItems = [];',
  13364. ' };',
  13365. ' this.$final = function () {',
  13366. ' this.FItems = undefined;',
  13367. ' };',
  13368. ' this.GetItems = function (Index) {',
  13369. ' var Result = 0;',
  13370. ' Result = this.FItems[Index];',
  13371. ' return Result;',
  13372. ' };',
  13373. ' this.SetItems = function (Index, Value) {',
  13374. ' this.FItems[Index] = Value;',
  13375. ' };',
  13376. ' this.DoIt = function () {',
  13377. ' this.SetItems(1, 2);',
  13378. ' this.SetItems(3,this.GetItems(4));',
  13379. ' this.SetItems(5,this.GetItems(6));',
  13380. ' this.SetItems(this.GetItems(7), this.GetItems(this.GetItems(8)));',
  13381. ' };',
  13382. '});',
  13383. 'this.Obj = null;'
  13384. ]),
  13385. LinesToStr([ // $mod.$main
  13386. '$mod.Obj.SetItems(11,$mod.Obj.GetItems(12));'
  13387. ]));
  13388. end;
  13389. procedure TTestModule.TestClass_Property_IndexSpec;
  13390. begin
  13391. StartProgram(false);
  13392. Add([
  13393. 'type',
  13394. ' TEnum = (red, blue);',
  13395. ' TObject = class',
  13396. ' function GetIntBool(Index: longint): boolean; virtual; abstract;',
  13397. ' procedure SetIntBool(Index: longint; b: boolean); virtual; abstract;',
  13398. ' function GetEnumBool(Index: TEnum): boolean; virtual; abstract;',
  13399. ' procedure SetEnumBool(Index: TEnum; b: boolean); virtual; abstract;',
  13400. ' function GetStrIntBool(A: String; I: longint): boolean; virtual; abstract;',
  13401. ' procedure SetStrIntBool(A: String; I: longint; b: boolean); virtual; abstract;',
  13402. ' property B1: boolean index 1 read GetIntBool write SetIntBool;',
  13403. ' property B2: boolean index TEnum.blue read GetEnumBool write SetEnumBool;',
  13404. ' property B3: boolean index ord(red) read GetIntBool write SetIntBool;',
  13405. ' property I1[A: String]: boolean index ord(blue) read GetStrIntBool write SetStrIntBool;',
  13406. ' end;',
  13407. 'procedure DoIt(b: boolean); begin end;',
  13408. 'var',
  13409. ' o: TObject;',
  13410. 'begin',
  13411. ' o.B1:=o.B1;',
  13412. ' o.B2:=o.B2;',
  13413. ' o.B3:=o.B3;',
  13414. ' o.I1[''a'']:=o.I1[''b''];',
  13415. ' doit(o.b1);',
  13416. ' doit(o.b2);',
  13417. ' doit(o.i1[''c'']);',
  13418. '']);
  13419. ConvertProgram;
  13420. CheckSource('TestClass_Property_IndexSpec',
  13421. LinesToStr([ // statements
  13422. 'this.TEnum = {',
  13423. ' "0": "red",',
  13424. ' red: 0,',
  13425. ' "1": "blue",',
  13426. ' blue: 1',
  13427. '};',
  13428. 'rtl.createClass(this, "TObject", null, function () {',
  13429. ' this.$init = function () {',
  13430. ' };',
  13431. ' this.$final = function () {',
  13432. ' };',
  13433. '});',
  13434. 'this.DoIt = function (b) {',
  13435. '};',
  13436. 'this.o = null;',
  13437. '']),
  13438. LinesToStr([ // $mod.$main
  13439. '$mod.o.SetIntBool(1, $mod.o.GetIntBool(1));',
  13440. '$mod.o.SetEnumBool($mod.TEnum.blue, $mod.o.GetEnumBool($mod.TEnum.blue));',
  13441. '$mod.o.SetIntBool(0, $mod.o.GetIntBool(0));',
  13442. '$mod.o.SetStrIntBool("a", 1, $mod.o.GetStrIntBool("b", 1));',
  13443. '$mod.DoIt($mod.o.GetIntBool(1));',
  13444. '$mod.DoIt($mod.o.GetEnumBool($mod.TEnum.blue));',
  13445. '$mod.DoIt($mod.o.GetStrIntBool("c", 1));',
  13446. '']));
  13447. end;
  13448. procedure TTestModule.TestClass_PropertyOfTypeArray;
  13449. begin
  13450. StartProgram(false);
  13451. Add('type');
  13452. Add(' TArray = array of longint;');
  13453. Add(' TObject = class');
  13454. Add(' FItems: TArray;');
  13455. Add(' function GetItems: tarray;');
  13456. Add(' procedure SetItems(Value: tarray);');
  13457. Add(' property Items: tarray read getitems write setitems;');
  13458. Add(' procedure SetNumbers(const Value: tarray);');
  13459. Add(' property Numbers: tarray write setnumbers;');
  13460. Add(' end;');
  13461. Add('function tobject.getitems: tarray;');
  13462. Add('begin');
  13463. Add(' Result:=fitems;');
  13464. Add('end;');
  13465. Add('procedure tobject.setitems(value: tarray);');
  13466. Add('begin');
  13467. Add(' fitems:=value;');
  13468. Add(' fitems:=nil;');
  13469. Add(' Items:=nil;');
  13470. Add(' Items:=Items;');
  13471. Add(' Items[1]:=2;');
  13472. Add(' fitems[3]:=Items[4];');
  13473. Add(' Items[5]:=Items[6];');
  13474. Add(' Self.Items[7]:=8;');
  13475. Add(' Self.Items[9]:=Self.Items[10];');
  13476. Add(' Items[Items[11]]:=Items[Items[12]];');
  13477. Add('end;');
  13478. Add('procedure tobject.SetNumbers(const Value: tarray);');
  13479. Add('begin;');
  13480. Add(' Numbers:=nil;');
  13481. Add(' Numbers:=Value;');
  13482. Add(' Self.Numbers:=Value;');
  13483. Add('end;');
  13484. Add('var Obj: tobject;');
  13485. Add('begin');
  13486. Add(' obj.items:=nil;');
  13487. Add(' obj.items:=obj.items;');
  13488. Add(' obj.items[11]:=obj.items[12];');
  13489. ConvertProgram;
  13490. CheckSource('TestClass_PropertyOfTypeArray',
  13491. LinesToStr([ // statements
  13492. 'rtl.createClass(this, "TObject", null, function () {',
  13493. ' this.$init = function () {',
  13494. ' this.FItems = [];',
  13495. ' };',
  13496. ' this.$final = function () {',
  13497. ' this.FItems = undefined;',
  13498. ' };',
  13499. ' this.GetItems = function () {',
  13500. ' var Result = [];',
  13501. ' Result = rtl.arrayRef(this.FItems);',
  13502. ' return Result;',
  13503. ' };',
  13504. ' this.SetItems = function (Value) {',
  13505. ' this.FItems = rtl.arrayRef(Value);',
  13506. ' this.FItems = [];',
  13507. ' this.SetItems([]);',
  13508. ' this.SetItems(rtl.arrayRef(this.GetItems()));',
  13509. ' this.GetItems()[1] = 2;',
  13510. ' this.FItems[3] = this.GetItems()[4];',
  13511. ' this.GetItems()[5] = this.GetItems()[6];',
  13512. ' this.GetItems()[7] = 8;',
  13513. ' this.GetItems()[9] = this.GetItems()[10];',
  13514. ' this.GetItems()[this.GetItems()[11]] = this.GetItems()[this.GetItems()[12]];',
  13515. ' };',
  13516. ' this.SetNumbers = function (Value) {',
  13517. ' this.SetNumbers([]);',
  13518. ' this.SetNumbers(Value);',
  13519. ' this.SetNumbers(Value);',
  13520. ' };',
  13521. '});',
  13522. 'this.Obj = null;'
  13523. ]),
  13524. LinesToStr([ // $mod.$main
  13525. '$mod.Obj.SetItems([]);',
  13526. '$mod.Obj.SetItems($mod.Obj.GetItems());',
  13527. '$mod.Obj.GetItems()[11] = $mod.Obj.GetItems()[12];'
  13528. ]));
  13529. end;
  13530. procedure TTestModule.TestClass_PropertyDefault;
  13531. begin
  13532. StartProgram(false);
  13533. Add([
  13534. 'type',
  13535. ' TArray = array of longint;',
  13536. ' TObject = class',
  13537. ' end;',
  13538. ' TBird = class',
  13539. ' FItems: TArray;',
  13540. ' function GetItems(Index: longint): longint;',
  13541. ' procedure SetItems(Index, Value: longint);',
  13542. ' property Items[Index: longint]: longint read getitems write setitems; default;',
  13543. ' end;',
  13544. 'function TBird.getitems(index: longint): longint;',
  13545. 'begin',
  13546. 'end;',
  13547. 'procedure TBird.setitems(index, value: longint);',
  13548. 'begin',
  13549. ' Self[1]:=2;',
  13550. ' Self[3]:=Self[index];',
  13551. ' Self[index]:=Self[Self[value]];',
  13552. ' Self[Self[4]]:=value;',
  13553. 'end;',
  13554. 'var',
  13555. ' Bird: TBird;',
  13556. ' Obj: TObject;',
  13557. 'begin',
  13558. ' bird[11]:=12;',
  13559. ' bird[13]:=bird[14];',
  13560. ' bird[Bird[15]]:=bird[Bird[15]];',
  13561. ' TBird(obj)[16]:=TBird(obj)[17];',
  13562. ' (obj as tbird)[18]:=19;',
  13563. '']);
  13564. ConvertProgram;
  13565. CheckSource('TestClass_PropertyDefault',
  13566. LinesToStr([ // statements
  13567. 'rtl.createClass(this, "TObject", null, function () {',
  13568. ' this.$init = function () {',
  13569. ' };',
  13570. ' this.$final = function () {',
  13571. ' };',
  13572. '});',
  13573. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  13574. ' this.$init = function () {',
  13575. ' $mod.TObject.$init.call(this);',
  13576. ' this.FItems = [];',
  13577. ' };',
  13578. ' this.$final = function () {',
  13579. ' this.FItems = undefined;',
  13580. ' $mod.TObject.$final.call(this);',
  13581. ' };',
  13582. ' this.GetItems = function (Index) {',
  13583. ' var Result = 0;',
  13584. ' return Result;',
  13585. ' };',
  13586. ' this.SetItems = function (Index, Value) {',
  13587. ' this.SetItems(1, 2);',
  13588. ' this.SetItems(3, this.GetItems(Index));',
  13589. ' this.SetItems(Index, this.GetItems(this.GetItems(Value)));',
  13590. ' this.SetItems(this.GetItems(4), Value);',
  13591. ' };',
  13592. '});',
  13593. 'this.Bird = null;',
  13594. 'this.Obj = null;',
  13595. '']),
  13596. LinesToStr([ // $mod.$main
  13597. '$mod.Bird.SetItems(11, 12);',
  13598. '$mod.Bird.SetItems(13, $mod.Bird.GetItems(14));',
  13599. '$mod.Bird.SetItems($mod.Bird.GetItems(15), $mod.Bird.GetItems($mod.Bird.GetItems(15)));',
  13600. '$mod.Obj.SetItems(16, $mod.Obj.GetItems(17));',
  13601. 'rtl.as($mod.Obj, $mod.TBird).SetItems(18, 19);',
  13602. '']));
  13603. end;
  13604. procedure TTestModule.TestClass_PropertyDefault_TypecastToOtherDefault;
  13605. begin
  13606. StartProgram(false);
  13607. Add([
  13608. 'type',
  13609. ' TObject = class end;',
  13610. ' TAlphaList = class',
  13611. ' function GetAlphas(Index: boolean): Pointer; virtual; abstract;',
  13612. ' procedure SetAlphas(Index: boolean; Value: Pointer); virtual; abstract;',
  13613. ' property Alphas[Index: boolean]: Pointer read getAlphas write setAlphas; default;',
  13614. ' end;',
  13615. ' TBetaList = class',
  13616. ' function GetBetas(Index: longint): Pointer; virtual; abstract;',
  13617. ' procedure SetBetas(Index: longint; Value: Pointer); virtual; abstract;',
  13618. ' property Betas[Index: longint]: Pointer read getBetas write setBetas; default;',
  13619. ' end;',
  13620. ' TBird = class',
  13621. ' procedure DoIt;',
  13622. ' end;',
  13623. 'procedure TBird.DoIt;',
  13624. 'var',
  13625. ' List: TAlphaList;',
  13626. 'begin',
  13627. ' if TBetaList(List[true])[3]=nil then ;',
  13628. ' TBetaList(List[false])[5]:=nil;',
  13629. 'end;',
  13630. 'var',
  13631. ' List: TAlphaList;',
  13632. 'begin',
  13633. ' if TBetaList(List[true])[3]=nil then ;',
  13634. ' TBetaList(List[false])[5]:=nil;',
  13635. '']);
  13636. ConvertProgram;
  13637. CheckSource('TestClass_PropertyDefault_TypecastToOtherDefault',
  13638. LinesToStr([ // statements
  13639. 'rtl.createClass(this, "TObject", null, function () {',
  13640. ' this.$init = function () {',
  13641. ' };',
  13642. ' this.$final = function () {',
  13643. ' };',
  13644. '});',
  13645. 'rtl.createClass(this, "TAlphaList", this.TObject, function () {',
  13646. '});',
  13647. 'rtl.createClass(this, "TBetaList", this.TObject, function () {',
  13648. '});',
  13649. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  13650. ' this.DoIt = function () {',
  13651. ' var List = null;',
  13652. ' if (List.GetAlphas(true).GetBetas(3) === null) ;',
  13653. ' List.GetAlphas(false).SetBetas(5, null);',
  13654. ' };',
  13655. '});',
  13656. 'this.List = null;',
  13657. '']),
  13658. LinesToStr([ // $mod.$main
  13659. 'if ($mod.List.GetAlphas(true).GetBetas(3) === null) ;',
  13660. '$mod.List.GetAlphas(false).SetBetas(5, null);',
  13661. '']));
  13662. end;
  13663. procedure TTestModule.TestClass_PropertyOverride;
  13664. begin
  13665. StartProgram(false);
  13666. Add('type');
  13667. Add(' integer = longint;');
  13668. Add(' TObject = class');
  13669. Add(' FItem: integer;');
  13670. Add(' function GetItem: integer; external name ''GetItem'';');
  13671. Add(' procedure SetItem(Value: integer); external name ''SetItem'';');
  13672. Add(' property Item: integer read getitem write setitem;');
  13673. Add(' end;');
  13674. Add(' TCar = class');
  13675. Add(' FBag: integer;');
  13676. Add(' function GetBag: integer; external name ''GetBag'';');
  13677. Add(' property Item read getbag;');
  13678. Add(' end;');
  13679. Add('var');
  13680. Add(' Obj: tobject;');
  13681. Add(' Car: tcar;');
  13682. Add('begin');
  13683. Add(' Obj.Item:=Obj.Item;');
  13684. Add(' Car.Item:=Car.Item;');
  13685. ConvertProgram;
  13686. CheckSource('TestClass_PropertyOverride',
  13687. LinesToStr([ // statements
  13688. 'rtl.createClass(this, "TObject", null, function () {',
  13689. ' this.$init = function () {',
  13690. ' this.FItem = 0;',
  13691. ' };',
  13692. ' this.$final = function () {',
  13693. ' };',
  13694. '});',
  13695. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  13696. ' this.$init = function () {',
  13697. ' $mod.TObject.$init.call(this);',
  13698. ' this.FBag = 0;',
  13699. ' };',
  13700. '});',
  13701. 'this.Obj = null;',
  13702. 'this.Car = null;',
  13703. '']),
  13704. LinesToStr([ // $mod.$main
  13705. '$mod.Obj.SetItem($mod.Obj.GetItem());',
  13706. '$mod.Car.SetItem($mod.Car.GetBag());',
  13707. '']));
  13708. end;
  13709. procedure TTestModule.TestClass_PropertyIncVisibility;
  13710. begin
  13711. AddModuleWithIntfImplSrc('unit1.pp',
  13712. LinesToStr([
  13713. 'type',
  13714. ' TNumber = longint;',
  13715. ' TInteger = longint;',
  13716. ' TObject = class',
  13717. ' private',
  13718. ' function GetItems(Index: TNumber): TInteger; virtual; abstract;',
  13719. ' procedure SetItems(Index: TInteger; Value: TNumber); virtual; abstract;',
  13720. ' protected',
  13721. ' property Items[Index: TNumber]: longint read GetItems write SetItems;',
  13722. ' end;']),
  13723. LinesToStr([
  13724. '']));
  13725. StartProgram(true);
  13726. Add([
  13727. 'uses unit1;',
  13728. 'type',
  13729. ' TBird = class',
  13730. ' public',
  13731. ' property Items;',
  13732. ' end;',
  13733. 'procedure DoIt(i: TInteger);',
  13734. 'begin',
  13735. 'end;',
  13736. 'var b: TBird;',
  13737. 'begin',
  13738. ' b.Items[1]:=2;',
  13739. ' b.Items[3]:=b.Items[4];',
  13740. ' DoIt(b.Items[5]);',
  13741. '']);
  13742. ConvertProgram;
  13743. CheckSource('TestClass_PropertyIncVisibility',
  13744. LinesToStr([ // statements
  13745. 'rtl.createClass(this, "TBird", pas.unit1.TObject, function () {',
  13746. '});',
  13747. 'this.DoIt = function (i) {',
  13748. '};',
  13749. 'this.b = null;'
  13750. ]),
  13751. LinesToStr([ // $mod.$main
  13752. '$mod.b.SetItems(1, 2);',
  13753. '$mod.b.SetItems(3, $mod.b.GetItems(4));',
  13754. '$mod.DoIt($mod.b.GetItems(5));'
  13755. ]));
  13756. end;
  13757. procedure TTestModule.TestClass_Assigned;
  13758. begin
  13759. StartProgram(false);
  13760. Add('type');
  13761. Add(' TObject = class');
  13762. Add(' end;');
  13763. Add('var');
  13764. Add(' Obj: tobject;');
  13765. Add(' b: boolean;');
  13766. Add('begin');
  13767. Add(' if Assigned(obj) then ;');
  13768. Add(' b:=Assigned(obj) or false;');
  13769. ConvertProgram;
  13770. CheckSource('TestClass_Assigned',
  13771. LinesToStr([ // statements
  13772. 'rtl.createClass(this, "TObject", null, function () {',
  13773. ' this.$init = function () {',
  13774. ' };',
  13775. ' this.$final = function () {',
  13776. ' };',
  13777. '});',
  13778. 'this.Obj = null;',
  13779. 'this.b = false;'
  13780. ]),
  13781. LinesToStr([ // $mod.$main
  13782. 'if ($mod.Obj != null);',
  13783. '$mod.b = ($mod.Obj != null) || false;'
  13784. ]));
  13785. end;
  13786. procedure TTestModule.TestClass_WithClassDoCreate;
  13787. begin
  13788. StartProgram(false);
  13789. Add('type');
  13790. Add(' TObject = class');
  13791. Add(' aBool: boolean;');
  13792. Add(' Arr: array of boolean;');
  13793. Add(' constructor Create;');
  13794. Add(' end;');
  13795. Add('constructor TObject.Create; begin end;');
  13796. Add('var');
  13797. Add(' Obj: tobject;');
  13798. Add(' b: boolean;');
  13799. Add('begin');
  13800. Add(' with tobject.create do begin');
  13801. Add(' b:=abool;');
  13802. Add(' abool:=b;');
  13803. Add(' b:=arr[1];');
  13804. Add(' arr[2]:=b;');
  13805. Add(' end;');
  13806. Add(' with tobject do');
  13807. Add(' obj:=create;');
  13808. Add(' with obj do begin');
  13809. Add(' create;');
  13810. Add(' b:=abool;');
  13811. Add(' abool:=b;');
  13812. Add(' b:=arr[3];');
  13813. Add(' arr[4]:=b;');
  13814. Add(' end;');
  13815. ConvertProgram;
  13816. CheckSource('TestClass_WithClassDoCreate',
  13817. LinesToStr([ // statements
  13818. 'rtl.createClass(this, "TObject", null, function () {',
  13819. ' this.$init = function () {',
  13820. ' this.aBool = false;',
  13821. ' this.Arr = [];',
  13822. ' };',
  13823. ' this.$final = function () {',
  13824. ' this.Arr = undefined;',
  13825. ' };',
  13826. ' this.Create = function () {',
  13827. ' return this;',
  13828. ' };',
  13829. '});',
  13830. 'this.Obj = null;',
  13831. 'this.b = false;'
  13832. ]),
  13833. LinesToStr([ // $mod.$main
  13834. 'var $with = $mod.TObject.$create("Create");',
  13835. '$mod.b = $with.aBool;',
  13836. '$with.aBool = $mod.b;',
  13837. '$mod.b = $with.Arr[1];',
  13838. '$with.Arr[2] = $mod.b;',
  13839. 'var $with1 = $mod.TObject;',
  13840. '$mod.Obj = $with1.$create("Create");',
  13841. 'var $with2 = $mod.Obj;',
  13842. '$with2.Create();',
  13843. '$mod.b = $with2.aBool;',
  13844. '$with2.aBool = $mod.b;',
  13845. '$mod.b = $with2.Arr[3];',
  13846. '$with2.Arr[4] = $mod.b;',
  13847. '']));
  13848. end;
  13849. procedure TTestModule.TestClass_WithClassInstDoProperty;
  13850. begin
  13851. StartProgram(false);
  13852. Add('type');
  13853. Add(' TObject = class');
  13854. Add(' FInt: longint;');
  13855. Add(' constructor Create;');
  13856. Add(' function GetSize: longint;');
  13857. Add(' procedure SetSize(Value: longint);');
  13858. Add(' property Int: longint read FInt write FInt;');
  13859. Add(' property Size: longint read GetSize write SetSize;');
  13860. Add(' end;');
  13861. Add('constructor TObject.Create; begin end;');
  13862. Add('function TObject.GetSize: longint; begin; end;');
  13863. Add('procedure TObject.SetSize(Value: longint); begin; end;');
  13864. Add('var');
  13865. Add(' Obj: tobject;');
  13866. Add(' i: longint;');
  13867. Add('begin');
  13868. Add(' with TObject.Create do begin');
  13869. Add(' i:=int;');
  13870. Add(' int:=i;');
  13871. Add(' i:=size;');
  13872. Add(' size:=i;');
  13873. Add(' end;');
  13874. Add(' with obj do begin');
  13875. Add(' i:=int;');
  13876. Add(' int:=i;');
  13877. Add(' i:=size;');
  13878. Add(' size:=i;');
  13879. Add(' end;');
  13880. ConvertProgram;
  13881. CheckSource('TestClass_WithClassInstDoProperty',
  13882. LinesToStr([ // statements
  13883. 'rtl.createClass(this, "TObject", null, function () {',
  13884. ' this.$init = function () {',
  13885. ' this.FInt = 0;',
  13886. ' };',
  13887. ' this.$final = function () {',
  13888. ' };',
  13889. ' this.Create = function () {',
  13890. ' return this;',
  13891. ' };',
  13892. ' this.GetSize = function () {',
  13893. ' var Result = 0;',
  13894. ' return Result;',
  13895. ' };',
  13896. ' this.SetSize = function (Value) {',
  13897. ' };',
  13898. '});',
  13899. 'this.Obj = null;',
  13900. 'this.i = 0;'
  13901. ]),
  13902. LinesToStr([ // $mod.$main
  13903. 'var $with = $mod.TObject.$create("Create");',
  13904. '$mod.i = $with.FInt;',
  13905. '$with.FInt = $mod.i;',
  13906. '$mod.i = $with.GetSize();',
  13907. '$with.SetSize($mod.i);',
  13908. 'var $with1 = $mod.Obj;',
  13909. '$mod.i = $with1.FInt;',
  13910. '$with1.FInt = $mod.i;',
  13911. '$mod.i = $with1.GetSize();',
  13912. '$with1.SetSize($mod.i);',
  13913. '']));
  13914. end;
  13915. procedure TTestModule.TestClass_WithClassInstDoPropertyWithParams;
  13916. begin
  13917. StartProgram(false);
  13918. Add('type');
  13919. Add(' TObject = class');
  13920. Add(' constructor Create;');
  13921. Add(' function GetItems(Index: longint): longint;');
  13922. Add(' procedure SetItems(Index, Value: longint);');
  13923. Add(' property Items[Index: longint]: longint read GetItems write SetItems;');
  13924. Add(' end;');
  13925. Add('constructor TObject.Create; begin end;');
  13926. Add('function tobject.getitems(index: longint): longint; begin; end;');
  13927. Add('procedure tobject.setitems(index, value: longint); begin; end;');
  13928. Add('var');
  13929. Add(' Obj: tobject;');
  13930. Add(' i: longint;');
  13931. Add('begin');
  13932. Add(' with TObject.Create do begin');
  13933. Add(' i:=Items[1];');
  13934. Add(' Items[2]:=i;');
  13935. Add(' end;');
  13936. Add(' with obj do begin');
  13937. Add(' i:=Items[3];');
  13938. Add(' Items[4]:=i;');
  13939. Add(' end;');
  13940. ConvertProgram;
  13941. CheckSource('TestClass_WithClassInstDoPropertyWithParams',
  13942. LinesToStr([ // statements
  13943. 'rtl.createClass(this, "TObject", null, function () {',
  13944. ' this.$init = function () {',
  13945. ' };',
  13946. ' this.$final = function () {',
  13947. ' };',
  13948. ' this.Create = function () {',
  13949. ' return this;',
  13950. ' };',
  13951. ' this.GetItems = function (Index) {',
  13952. ' var Result = 0;',
  13953. ' return Result;',
  13954. ' };',
  13955. ' this.SetItems = function (Index, Value) {',
  13956. ' };',
  13957. '});',
  13958. 'this.Obj = null;',
  13959. 'this.i = 0;'
  13960. ]),
  13961. LinesToStr([ // $mod.$main
  13962. 'var $with = $mod.TObject.$create("Create");',
  13963. '$mod.i = $with.GetItems(1);',
  13964. '$with.SetItems(2, $mod.i);',
  13965. 'var $with1 = $mod.Obj;',
  13966. '$mod.i = $with1.GetItems(3);',
  13967. '$with1.SetItems(4, $mod.i);',
  13968. '']));
  13969. end;
  13970. procedure TTestModule.TestClass_WithClassInstDoFunc;
  13971. begin
  13972. StartProgram(false);
  13973. Add('type');
  13974. Add(' TObject = class');
  13975. Add(' constructor Create;');
  13976. Add(' function GetSize: longint;');
  13977. Add(' procedure SetSize(Value: longint);');
  13978. Add(' end;');
  13979. Add('constructor TObject.Create; begin end;');
  13980. Add('function TObject.GetSize: longint; begin; end;');
  13981. Add('procedure TObject.SetSize(Value: longint); begin; end;');
  13982. Add('var');
  13983. Add(' Obj: tobject;');
  13984. Add(' i: longint;');
  13985. Add('begin');
  13986. Add(' with TObject.Create do begin');
  13987. Add(' i:=GetSize;');
  13988. Add(' i:=GetSize();');
  13989. Add(' SetSize(i);');
  13990. Add(' end;');
  13991. Add(' with obj do begin');
  13992. Add(' i:=GetSize;');
  13993. Add(' i:=GetSize();');
  13994. Add(' SetSize(i);');
  13995. Add(' end;');
  13996. ConvertProgram;
  13997. CheckSource('TestClass_WithClassInstDoFunc',
  13998. LinesToStr([ // statements
  13999. 'rtl.createClass(this, "TObject", null, function () {',
  14000. ' this.$init = function () {',
  14001. ' };',
  14002. ' this.$final = function () {',
  14003. ' };',
  14004. ' this.Create = function () {',
  14005. ' return this;',
  14006. ' };',
  14007. ' this.GetSize = function () {',
  14008. ' var Result = 0;',
  14009. ' return Result;',
  14010. ' };',
  14011. ' this.SetSize = function (Value) {',
  14012. ' };',
  14013. '});',
  14014. 'this.Obj = null;',
  14015. 'this.i = 0;'
  14016. ]),
  14017. LinesToStr([ // $mod.$main
  14018. 'var $with = $mod.TObject.$create("Create");',
  14019. '$mod.i = $with.GetSize();',
  14020. '$mod.i = $with.GetSize();',
  14021. '$with.SetSize($mod.i);',
  14022. 'var $with1 = $mod.Obj;',
  14023. '$mod.i = $with1.GetSize();',
  14024. '$mod.i = $with1.GetSize();',
  14025. '$with1.SetSize($mod.i);',
  14026. '']));
  14027. end;
  14028. procedure TTestModule.TestClass_TypeCast;
  14029. begin
  14030. StartProgram(false);
  14031. Add('type');
  14032. Add(' TObject = class');
  14033. Add(' Next: TObject;');
  14034. Add(' constructor Create;');
  14035. Add(' end;');
  14036. Add(' TControl = class(TObject)');
  14037. Add(' Arr: array of TObject;');
  14038. Add(' function GetIt(vI: longint = 0): TObject;');
  14039. Add(' end;');
  14040. Add('constructor tobject.create; begin end;');
  14041. Add('function tcontrol.getit(vi: longint = 0): tobject; begin end;');
  14042. Add('var');
  14043. Add(' Obj: tobject;');
  14044. Add('begin');
  14045. Add(' obj:=tcontrol(obj).next;');
  14046. Add(' tcontrol(obj):=nil;');
  14047. Add(' obj:=tcontrol(obj);');
  14048. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit);');
  14049. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit());');
  14050. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit(1));');
  14051. Add(' tcontrol(obj):=tcontrol(tcontrol(tcontrol(obj).getit).arr[2]);');
  14052. Add(' obj:=tcontrol(nil);');
  14053. ConvertProgram;
  14054. CheckSource('TestClass_TypeCast',
  14055. LinesToStr([ // statements
  14056. 'rtl.createClass(this, "TObject", null, function () {',
  14057. ' this.$init = function () {',
  14058. ' this.Next = null;',
  14059. ' };',
  14060. ' this.$final = function () {',
  14061. ' this.Next = undefined;',
  14062. ' };',
  14063. ' this.Create = function () {',
  14064. ' return this;',
  14065. ' };',
  14066. '});',
  14067. 'rtl.createClass(this, "TControl", this.TObject, function () {',
  14068. ' this.$init = function () {',
  14069. ' $mod.TObject.$init.call(this);',
  14070. ' this.Arr = [];',
  14071. ' };',
  14072. ' this.$final = function () {',
  14073. ' this.Arr = undefined;',
  14074. ' $mod.TObject.$final.call(this);',
  14075. ' };',
  14076. ' this.GetIt = function (vI) {',
  14077. ' var Result = null;',
  14078. ' return Result;',
  14079. ' };',
  14080. '});',
  14081. 'this.Obj = null;'
  14082. ]),
  14083. LinesToStr([ // $mod.$main
  14084. '$mod.Obj = $mod.Obj.Next;',
  14085. '$mod.Obj = null;',
  14086. '$mod.Obj = $mod.Obj;',
  14087. '$mod.Obj = $mod.Obj.GetIt(0);',
  14088. '$mod.Obj = $mod.Obj.GetIt(0);',
  14089. '$mod.Obj = $mod.Obj.GetIt(1);',
  14090. '$mod.Obj = $mod.Obj.GetIt(0).Arr[2];',
  14091. '$mod.Obj = null;',
  14092. '']));
  14093. end;
  14094. procedure TTestModule.TestClass_TypeCastUntypedParam;
  14095. begin
  14096. StartProgram(false);
  14097. Add('type');
  14098. Add(' TObject = class end;');
  14099. Add('procedure ProcA(var A);');
  14100. Add('begin');
  14101. Add(' TObject(A):=nil;');
  14102. Add(' TObject(A):=TObject(A);');
  14103. Add(' if TObject(A)=nil then ;');
  14104. Add(' if nil=TObject(A) then ;');
  14105. Add('end;');
  14106. Add('procedure ProcB(out A);');
  14107. Add('begin');
  14108. Add(' TObject(A):=nil;');
  14109. Add(' TObject(A):=TObject(A);');
  14110. Add(' if TObject(A)=nil then ;');
  14111. Add(' if nil=TObject(A) then ;');
  14112. Add('end;');
  14113. Add('procedure ProcC(const A);');
  14114. Add('begin');
  14115. Add(' if TObject(A)=nil then ;');
  14116. Add(' if nil=TObject(A) then ;');
  14117. Add('end;');
  14118. Add('var o: TObject;');
  14119. Add('begin');
  14120. Add(' ProcA(o);');
  14121. Add(' ProcB(o);');
  14122. Add(' ProcC(o);');
  14123. ConvertProgram;
  14124. CheckSource('TestClass_TypeCastUntypedParam',
  14125. LinesToStr([ // statements
  14126. 'rtl.createClass(this, "TObject", null, function () {',
  14127. ' this.$init = function () {',
  14128. ' };',
  14129. ' this.$final = function () {',
  14130. ' };',
  14131. '});',
  14132. 'this.ProcA = function (A) {',
  14133. ' A.set(null);',
  14134. ' A.set(A.get());',
  14135. ' if (A.get() === null);',
  14136. ' if (null === A.get());',
  14137. '};',
  14138. 'this.ProcB = function (A) {',
  14139. ' A.set(null);',
  14140. ' A.set(A.get());',
  14141. ' if (A.get() === null);',
  14142. ' if (null === A.get());',
  14143. '};',
  14144. 'this.ProcC = function (A) {',
  14145. ' if (A === null);',
  14146. ' if (null === A);',
  14147. '};',
  14148. 'this.o = null;',
  14149. '']),
  14150. LinesToStr([ // $mod.$main
  14151. '$mod.ProcA({',
  14152. ' p: $mod,',
  14153. ' get: function () {',
  14154. ' return this.p.o;',
  14155. ' },',
  14156. ' set: function (v) {',
  14157. ' this.p.o = v;',
  14158. ' }',
  14159. '});',
  14160. '$mod.ProcB({',
  14161. ' p: $mod,',
  14162. ' get: function () {',
  14163. ' return this.p.o;',
  14164. ' },',
  14165. ' set: function (v) {',
  14166. ' this.p.o = v;',
  14167. ' }',
  14168. '});',
  14169. '$mod.ProcC($mod.o);',
  14170. '']));
  14171. end;
  14172. procedure TTestModule.TestClass_Overloads;
  14173. begin
  14174. StartProgram(false);
  14175. Add('type');
  14176. Add(' TObject = class');
  14177. Add(' procedure DoIt;');
  14178. Add(' procedure DoIt(vI: longint);');
  14179. Add(' end;');
  14180. Add('procedure TObject.DoIt;');
  14181. Add('begin');
  14182. Add(' DoIt;');
  14183. Add(' DoIt(1);');
  14184. Add('end;');
  14185. Add('procedure TObject.DoIt(vI: longint); begin end;');
  14186. Add('begin');
  14187. ConvertProgram;
  14188. CheckSource('TestClass_Overloads',
  14189. LinesToStr([ // statements
  14190. 'rtl.createClass(this, "TObject", null, function () {',
  14191. ' this.$init = function () {',
  14192. ' };',
  14193. ' this.$final = function () {',
  14194. ' };',
  14195. ' this.DoIt = function () {',
  14196. ' this.DoIt();',
  14197. ' this.DoIt$1(1);',
  14198. ' };',
  14199. ' this.DoIt$1 = function (vI) {',
  14200. ' };',
  14201. '});',
  14202. '']),
  14203. LinesToStr([ // $mod.$main
  14204. '']));
  14205. end;
  14206. procedure TTestModule.TestClass_OverloadsAncestor;
  14207. begin
  14208. StartProgram(false);
  14209. Add('type');
  14210. Add(' TObject = class;');
  14211. Add(' TObject = class');
  14212. Add(' procedure DoIt(vA: longint);');
  14213. Add(' procedure DoIt(vA, vB: longint);');
  14214. Add(' end;');
  14215. Add(' TCar = class;');
  14216. Add(' TCar = class');
  14217. Add(' procedure DoIt(vA: longint);');
  14218. Add(' procedure DoIt(vA, vB: longint);');
  14219. Add(' end;');
  14220. Add('procedure tobject.doit(va: longint);');
  14221. Add('begin');
  14222. Add(' doit(1);');
  14223. Add(' doit(1,2);');
  14224. Add('end;');
  14225. Add('procedure tobject.doit(va, vb: longint); begin end;');
  14226. Add('procedure tcar.doit(va: longint);');
  14227. Add('begin');
  14228. Add(' doit(1);');
  14229. Add(' doit(1,2);');
  14230. Add(' inherited doit(1);');
  14231. Add(' inherited doit(1,2);');
  14232. Add('end;');
  14233. Add('procedure tcar.doit(va, vb: longint); begin end;');
  14234. Add('begin');
  14235. ConvertProgram;
  14236. CheckSource('TestClass_OverloadsAncestor',
  14237. LinesToStr([ // statements
  14238. 'rtl.createClass(this, "TObject", null, function () {',
  14239. ' this.$init = function () {',
  14240. ' };',
  14241. ' this.$final = function () {',
  14242. ' };',
  14243. ' this.DoIt = function (vA) {',
  14244. ' this.DoIt(1);',
  14245. ' this.DoIt$1(1,2);',
  14246. ' };',
  14247. ' this.DoIt$1 = function (vA, vB) {',
  14248. ' };',
  14249. '});',
  14250. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  14251. ' this.DoIt$2 = function (vA) {',
  14252. ' this.DoIt$2(1);',
  14253. ' this.DoIt$3(1, 2);',
  14254. ' $mod.TObject.DoIt.call(this, 1);',
  14255. ' $mod.TObject.DoIt$1.call(this, 1, 2);',
  14256. ' };',
  14257. ' this.DoIt$3 = function (vA, vB) {',
  14258. ' };',
  14259. '});',
  14260. '']),
  14261. LinesToStr([ // $mod.$main
  14262. '']));
  14263. end;
  14264. procedure TTestModule.TestClass_OverloadConstructor;
  14265. begin
  14266. StartProgram(false);
  14267. Add('type');
  14268. Add(' TObject = class');
  14269. Add(' constructor Create(vA: longint);');
  14270. Add(' constructor Create(vA, vB: longint);');
  14271. Add(' end;');
  14272. Add(' TCar = class');
  14273. Add(' constructor Create(vA: longint);');
  14274. Add(' constructor Create(vA, vB: longint);');
  14275. Add(' end;');
  14276. Add('constructor tobject.create(va: longint);');
  14277. Add('begin');
  14278. Add(' create(1);');
  14279. Add(' create(1,2);');
  14280. Add('end;');
  14281. Add('constructor tobject.create(va, vb: longint); begin end;');
  14282. Add('constructor tcar.create(va: longint);');
  14283. Add('begin');
  14284. Add(' create(1);');
  14285. Add(' create(1,2);');
  14286. Add(' inherited create(1);');
  14287. Add(' inherited create(1,2);');
  14288. Add('end;');
  14289. Add('constructor tcar.create(va, vb: longint); begin end;');
  14290. Add('begin');
  14291. Add(' tobject.create(1);');
  14292. Add(' tobject.create(1,2);');
  14293. Add(' tcar.create(1);');
  14294. Add(' tcar.create(1,2);');
  14295. ConvertProgram;
  14296. CheckSource('TestClass_OverloadConstructor',
  14297. LinesToStr([ // statements
  14298. 'rtl.createClass(this, "TObject", null, function () {',
  14299. ' this.$init = function () {',
  14300. ' };',
  14301. ' this.$final = function () {',
  14302. ' };',
  14303. ' this.Create = function (vA) {',
  14304. ' this.Create(1);',
  14305. ' this.Create$1(1,2);',
  14306. ' return this;',
  14307. ' };',
  14308. ' this.Create$1 = function (vA, vB) {',
  14309. ' return this;',
  14310. ' };',
  14311. '});',
  14312. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  14313. ' this.Create$2 = function (vA) {',
  14314. ' this.Create$2(1);',
  14315. ' this.Create$3(1, 2);',
  14316. ' $mod.TObject.Create.call(this, 1);',
  14317. ' $mod.TObject.Create$1.call(this, 1, 2);',
  14318. ' return this;',
  14319. ' };',
  14320. ' this.Create$3 = function (vA, vB) {',
  14321. ' return this;',
  14322. ' };',
  14323. '});',
  14324. '']),
  14325. LinesToStr([ // $mod.$main
  14326. '$mod.TObject.$create("Create", [1]);',
  14327. '$mod.TObject.$create("Create$1", [1, 2]);',
  14328. '$mod.TCar.$create("Create$2", [1]);',
  14329. '$mod.TCar.$create("Create$3", [1, 2]);',
  14330. '']));
  14331. end;
  14332. procedure TTestModule.TestClass_OverloadDelphiOverride;
  14333. begin
  14334. StartProgram(false);
  14335. Add([
  14336. '{$mode delphi}',
  14337. 'type',
  14338. ' TObject = class end;',
  14339. ' TBird = class',
  14340. ' function {#a}GetValue: longint; overload; virtual;',
  14341. ' function {#b}GetValue(AValue: longint): longint; overload; virtual;',
  14342. ' end;',
  14343. ' TEagle = class(TBird)',
  14344. ' function {#c}GetValue: longint; overload; override;',
  14345. ' function {#d}GetValue(AValue: longint): longint; overload; override;',
  14346. ' end;',
  14347. 'function TBird.GetValue: longint;',
  14348. 'begin',
  14349. ' if 3={@a}GetValue then ;',
  14350. ' if 4={@b}GetValue(5) then ;',
  14351. 'end;',
  14352. 'function TBird.GetValue(AValue: longint): longint;',
  14353. 'begin',
  14354. 'end;',
  14355. 'function TEagle.GetValue: longint;',
  14356. 'begin',
  14357. ' if 13={@c}GetValue then ;',
  14358. ' if 14={@d}GetValue(15) then ;',
  14359. ' if 15=inherited {@a}GetValue then ;',
  14360. ' if 16=inherited {@b}GetValue(17) then ;',
  14361. 'end;',
  14362. 'function TEagle.GetValue(AValue: longint): longint;',
  14363. 'begin',
  14364. 'end;',
  14365. 'var',
  14366. ' e: TEagle;',
  14367. 'begin',
  14368. ' if 23=e.{@c}GetValue then ;',
  14369. ' if 24=e.{@d}GetValue(25) then ;']);
  14370. ConvertProgram;
  14371. CheckSource('TestClass_OverloadDelphiOverride',
  14372. LinesToStr([ // statements
  14373. 'rtl.createClass(this, "TObject", null, function () {',
  14374. ' this.$init = function () {',
  14375. ' };',
  14376. ' this.$final = function () {',
  14377. ' };',
  14378. '});',
  14379. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  14380. ' this.GetValue = function () {',
  14381. ' var Result = 0;',
  14382. ' if (3 === this.GetValue()) ;',
  14383. ' if (4 === this.GetValue$1(5)) ;',
  14384. ' return Result;',
  14385. ' };',
  14386. ' this.GetValue$1 = function (AValue) {',
  14387. ' var Result = 0;',
  14388. ' return Result;',
  14389. ' };',
  14390. '});',
  14391. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  14392. ' this.GetValue = function () {',
  14393. ' var Result = 0;',
  14394. ' if (13 === this.GetValue()) ;',
  14395. ' if (14 === this.GetValue$1(15)) ;',
  14396. ' if (15 === $mod.TBird.GetValue.call(this)) ;',
  14397. ' if (16 === $mod.TBird.GetValue$1.call(this, 17)) ;',
  14398. ' return Result;',
  14399. ' };',
  14400. ' this.GetValue$1 = function (AValue) {',
  14401. ' var Result = 0;',
  14402. ' return Result;',
  14403. ' };',
  14404. '});',
  14405. 'this.e = null;',
  14406. '']),
  14407. LinesToStr([ // $mod.$main
  14408. 'if (23 === $mod.e.GetValue()) ;',
  14409. 'if (24 === $mod.e.GetValue$1(25)) ;',
  14410. '']));
  14411. end;
  14412. procedure TTestModule.TestClass_ReintroduceVarDelphi;
  14413. begin
  14414. StartProgram(false);
  14415. Add([
  14416. '{$mode delphi}',
  14417. 'type',
  14418. ' TObject = class end;',
  14419. ' TAnimal = class',
  14420. ' public',
  14421. ' {#animal_a}A: longint;',
  14422. ' function {#animal_b}B: longint;',
  14423. ' end;',
  14424. ' TBird = class(TAnimal)',
  14425. ' public',
  14426. ' {#bird_a}A: double;',
  14427. ' {#bird_b}B: boolean;',
  14428. ' end;',
  14429. ' TEagle = class(TBird)',
  14430. ' public',
  14431. ' function {#eagle_a}A: boolean;',
  14432. ' {#eagle_b}B: double;',
  14433. ' end;',
  14434. 'function TAnimal.B: longint;',
  14435. 'begin',
  14436. 'end;',
  14437. 'function TEagle.A: boolean;',
  14438. 'begin',
  14439. ' {@eagle_b}B:=3.3;',
  14440. ' {@eagle_a}A();',
  14441. ' TBird(Self).{@bird_b}B:=true;',
  14442. ' TAnimal(Self).{@animal_a}A:=17;',
  14443. ' inherited {@bird_b}B:=inherited {bird_a}A>1;', // Delphi allows only inherited <functionname>
  14444. 'end;',
  14445. 'var',
  14446. ' e: TEagle;',
  14447. 'begin',
  14448. ' e.{@eagle_b}B:=5.3;',
  14449. ' if e.{@eagle_a}A then ;',
  14450. '']);
  14451. ConvertProgram;
  14452. CheckSource('TestClass_ReintroduceVarDelphi',
  14453. LinesToStr([ // statements
  14454. 'rtl.createClass(this, "TObject", null, function () {',
  14455. ' this.$init = function () {',
  14456. ' };',
  14457. ' this.$final = function () {',
  14458. ' };',
  14459. '});',
  14460. 'rtl.createClass(this, "TAnimal", this.TObject, function () {',
  14461. ' this.$init = function () {',
  14462. ' $mod.TObject.$init.call(this);',
  14463. ' this.A = 0;',
  14464. ' };',
  14465. ' this.B = function () {',
  14466. ' var Result = 0;',
  14467. ' return Result;',
  14468. ' };',
  14469. '});',
  14470. 'rtl.createClass(this, "TBird", this.TAnimal, function () {',
  14471. ' this.$init = function () {',
  14472. ' $mod.TAnimal.$init.call(this);',
  14473. ' this.A$1 = 0.0;',
  14474. ' this.B$1 = false;',
  14475. ' };',
  14476. '});',
  14477. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  14478. ' this.$init = function () {',
  14479. ' $mod.TBird.$init.call(this);',
  14480. ' this.B$2 = 0.0;',
  14481. ' };',
  14482. ' this.A$2 = function () {',
  14483. ' var Result = false;',
  14484. ' this.B$2 = 3.3;',
  14485. ' this.A$2();',
  14486. ' this.B$1 = true;',
  14487. ' this.A = 17;',
  14488. ' this.B$1 = this.A$1 > 1;',
  14489. ' return Result;',
  14490. ' };',
  14491. '});',
  14492. 'this.e = null;',
  14493. '']),
  14494. LinesToStr([ // $mod.$main
  14495. '$mod.e.B$2 = 5.3;',
  14496. 'if ($mod.e.A$2()) ;',
  14497. '']));
  14498. end;
  14499. procedure TTestModule.TestClass_ReintroducedVar;
  14500. begin
  14501. StartProgram(false);
  14502. Add('type');
  14503. Add(' TObject = class');
  14504. Add(' strict private');
  14505. Add(' Some: longint;');
  14506. Add(' end;');
  14507. Add(' TMobile = class');
  14508. Add(' strict private');
  14509. Add(' Some: string;');
  14510. Add(' end;');
  14511. Add(' TCar = class(tmobile)');
  14512. Add(' procedure Some;');
  14513. Add(' procedure Some(vA: longint);');
  14514. Add(' end;');
  14515. Add('procedure tcar.some;');
  14516. Add('begin');
  14517. Add(' Some;');
  14518. Add(' Some(1);');
  14519. Add('end;');
  14520. Add('procedure tcar.some(va: longint); begin end;');
  14521. Add('begin');
  14522. ConvertProgram;
  14523. CheckSource('TestClass_ReintroducedVar',
  14524. LinesToStr([ // statements
  14525. 'rtl.createClass(this, "TObject", null, function () {',
  14526. ' this.$init = function () {',
  14527. ' this.Some = 0;',
  14528. ' };',
  14529. ' this.$final = function () {',
  14530. ' };',
  14531. '});',
  14532. 'rtl.createClass(this, "TMobile", this.TObject, function () {',
  14533. ' this.$init = function () {',
  14534. ' $mod.TObject.$init.call(this);',
  14535. ' this.Some$1 = "";',
  14536. ' };',
  14537. '});',
  14538. 'rtl.createClass(this, "TCar", this.TMobile, function () {',
  14539. ' this.Some$2 = function () {',
  14540. ' this.Some$2();',
  14541. ' this.Some$3(1);',
  14542. ' };',
  14543. ' this.Some$3 = function (vA) {',
  14544. ' };',
  14545. '});',
  14546. '']),
  14547. LinesToStr([ // $mod.$main
  14548. '']));
  14549. end;
  14550. procedure TTestModule.TestClass_RaiseDescendant;
  14551. begin
  14552. StartProgram(false);
  14553. Add([
  14554. 'type',
  14555. ' TObject = class',
  14556. ' constructor Create(Msg: string);',
  14557. ' end;',
  14558. ' Exception = class',
  14559. ' end;',
  14560. ' EConvertError = class(Exception)',
  14561. ' end;',
  14562. 'constructor TObject.Create(Msg: string); begin end;',
  14563. 'function AssertConv(Msg: string = ''def''): EConvertError; begin end;',
  14564. 'begin',
  14565. ' raise Exception.Create(''Bar1'');',
  14566. ' raise EConvertError.Create(''Bar2'');',
  14567. ' raise AssertConv(''Bar2'');',
  14568. ' raise AssertConv;',
  14569. '']);
  14570. ConvertProgram;
  14571. CheckSource('TestClass_RaiseDescendant',
  14572. LinesToStr([ // statements
  14573. 'rtl.createClass(this, "TObject", null, function () {',
  14574. ' this.$init = function () {',
  14575. ' };',
  14576. ' this.$final = function () {',
  14577. ' };',
  14578. ' this.Create = function (Msg) {',
  14579. ' return this;',
  14580. ' };',
  14581. '});',
  14582. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  14583. '});',
  14584. 'rtl.createClass(this, "EConvertError", this.Exception, function () {',
  14585. '});',
  14586. 'this.AssertConv = function (Msg) {',
  14587. ' var Result = null;',
  14588. ' return Result;',
  14589. '};',
  14590. '']),
  14591. LinesToStr([ // $mod.$main
  14592. 'throw $mod.Exception.$create("Create",["Bar1"]);',
  14593. 'throw $mod.EConvertError.$create("Create",["Bar2"]);',
  14594. 'throw $mod.AssertConv("Bar2");',
  14595. 'throw $mod.AssertConv("def");',
  14596. '']));
  14597. end;
  14598. procedure TTestModule.TestClass_ExternalMethod;
  14599. begin
  14600. AddModuleWithIntfImplSrc('unit2.pas',
  14601. LinesToStr([
  14602. 'type',
  14603. ' TObject = class',
  14604. ' public',
  14605. ' procedure Intern; external name ''$DoIntern'';',
  14606. ' end;',
  14607. '']),
  14608. LinesToStr([
  14609. '']));
  14610. StartUnit(true);
  14611. Add('interface');
  14612. Add('uses unit2;');
  14613. Add('type');
  14614. Add(' TCar = class(TObject)');
  14615. Add(' public');
  14616. Add(' procedure Intern2; external name ''$DoIntern2'';');
  14617. Add(' procedure DoIt;');
  14618. Add(' end;');
  14619. Add('implementation');
  14620. Add('procedure tcar.doit;');
  14621. Add('begin');
  14622. Add(' Intern;');
  14623. Add(' Intern();');
  14624. Add(' Intern2;');
  14625. Add(' Intern2();');
  14626. Add('end;');
  14627. Add('var Obj: TCar;');
  14628. Add('begin');
  14629. Add(' obj.intern;');
  14630. Add(' obj.intern();');
  14631. Add(' obj.intern2;');
  14632. Add(' obj.intern2();');
  14633. Add(' obj.doit;');
  14634. Add(' obj.doit();');
  14635. Add(' with obj do begin');
  14636. Add(' Intern;');
  14637. Add(' Intern();');
  14638. Add(' Intern2;');
  14639. Add(' Intern2();');
  14640. Add(' end;');
  14641. ConvertUnit;
  14642. CheckSource('TestClass_ExternalMethod',
  14643. LinesToStr([
  14644. 'var $impl = $mod.$impl;',
  14645. 'rtl.createClass(this, "TCar", pas.unit2.TObject, function () {',
  14646. ' this.DoIt = function () {',
  14647. ' this.$DoIntern();',
  14648. ' this.$DoIntern();',
  14649. ' this.$DoIntern2();',
  14650. ' this.$DoIntern2();',
  14651. ' };',
  14652. ' });',
  14653. '']),
  14654. LinesToStr([ // this.$init
  14655. '$impl.Obj.$DoIntern();',
  14656. '$impl.Obj.$DoIntern();',
  14657. '$impl.Obj.$DoIntern2();',
  14658. '$impl.Obj.$DoIntern2();',
  14659. '$impl.Obj.DoIt();',
  14660. '$impl.Obj.DoIt();',
  14661. 'var $with = $impl.Obj;',
  14662. '$with.$DoIntern();',
  14663. '$with.$DoIntern();',
  14664. '$with.$DoIntern2();',
  14665. '$with.$DoIntern2();',
  14666. '']),
  14667. LinesToStr([ // implementation
  14668. '$impl.Obj = null;',
  14669. '']) );
  14670. end;
  14671. procedure TTestModule.TestClass_ExternalVirtualNameMismatchFail;
  14672. begin
  14673. StartProgram(false);
  14674. Add('type');
  14675. Add(' TObject = class');
  14676. Add(' procedure DoIt; virtual; external name ''Foo'';');
  14677. Add(' end;');
  14678. Add('begin');
  14679. SetExpectedPasResolverError('Virtual method name must match external',
  14680. nVirtualMethodNameMustMatchExternal);
  14681. ConvertProgram;
  14682. end;
  14683. procedure TTestModule.TestClass_ExternalOverrideFail;
  14684. begin
  14685. StartProgram(false);
  14686. Add('type');
  14687. Add(' TObject = class');
  14688. Add(' procedure DoIt; virtual; external name ''DoIt'';');
  14689. Add(' end;');
  14690. Add(' TCar = class');
  14691. Add(' procedure DoIt; override; external name ''DoIt'';');
  14692. Add(' end;');
  14693. Add('begin');
  14694. SetExpectedPasResolverError('Invalid procedure modifier override,external',
  14695. nInvalidXModifierY);
  14696. ConvertProgram;
  14697. end;
  14698. procedure TTestModule.TestClass_ExternalVar;
  14699. begin
  14700. AddModuleWithIntfImplSrc('unit2.pas',
  14701. LinesToStr([
  14702. '{$modeswitch externalclass}',
  14703. 'type',
  14704. ' TObject = class',
  14705. ' public',
  14706. ' Intern: longint external name ''$Intern'';',
  14707. ' Bracket: longint external name ''["A B"]'';',
  14708. ' end;',
  14709. '']),
  14710. LinesToStr([
  14711. '']));
  14712. StartUnit(true);
  14713. Add([
  14714. 'interface',
  14715. 'uses unit2;',
  14716. '{$modeswitch externalclass}',
  14717. 'type',
  14718. ' TCar = class(tobject)',
  14719. ' public',
  14720. ' Intern2: longint external name ''$Intern2'';',
  14721. ' procedure DoIt;',
  14722. ' end;',
  14723. 'implementation',
  14724. 'procedure tcar.doit;',
  14725. 'begin',
  14726. ' Intern:=Intern+1;',
  14727. ' Intern2:=Intern2+2;',
  14728. ' Bracket:=Bracket+3;',
  14729. 'end;',
  14730. 'var Obj: TCar;',
  14731. 'begin',
  14732. ' obj.intern:=obj.intern+1;',
  14733. ' obj.intern2:=obj.intern2+2;',
  14734. ' obj.Bracket:=obj.Bracket+3;',
  14735. ' with obj do begin',
  14736. ' intern:=intern+1;',
  14737. ' intern2:=intern2+2;',
  14738. ' Bracket:=Bracket+3;',
  14739. ' end;']);
  14740. ConvertUnit;
  14741. CheckSource('TestClass_ExternalVar',
  14742. LinesToStr([
  14743. 'var $impl = $mod.$impl;',
  14744. 'rtl.createClass(this, "TCar", pas.unit2.TObject, function () {',
  14745. ' this.DoIt = function () {',
  14746. ' this.$Intern = this.$Intern + 1;',
  14747. ' this.$Intern2 = this.$Intern2 + 2;',
  14748. ' this["A B"] = this["A B"] + 3;',
  14749. ' };',
  14750. ' });',
  14751. '']),
  14752. LinesToStr([
  14753. '$impl.Obj.$Intern = $impl.Obj.$Intern + 1;',
  14754. '$impl.Obj.$Intern2 = $impl.Obj.$Intern2 + 2;',
  14755. '$impl.Obj["A B"] = $impl.Obj["A B"] + 3;',
  14756. 'var $with = $impl.Obj;',
  14757. '$with.$Intern = $with.$Intern + 1;',
  14758. '$with.$Intern2 = $with.$Intern2 + 2;',
  14759. '$with["A B"] = $with["A B"] + 3;',
  14760. '']),
  14761. LinesToStr([ // implementation
  14762. '$impl.Obj = null;',
  14763. '']));
  14764. end;
  14765. procedure TTestModule.TestClass_Const;
  14766. begin
  14767. StartProgram(false);
  14768. Add([
  14769. 'type',
  14770. ' integer = longint;',
  14771. ' TClass = class of TObject;',
  14772. ' TObject = class',
  14773. ' public',
  14774. ' const cI: integer = 3;',
  14775. ' procedure DoIt;',
  14776. ' class procedure DoMore;',
  14777. ' end;',
  14778. 'procedure tobject.doit;',
  14779. 'begin',
  14780. ' if cI=4 then;',
  14781. ' if 5=cI then;',
  14782. ' if Self.cI=6 then;',
  14783. ' if 7=Self.cI then;',
  14784. ' with Self do begin',
  14785. ' if cI=11 then;',
  14786. ' if 12=cI then;',
  14787. ' end;',
  14788. 'end;',
  14789. 'class procedure tobject.domore;',
  14790. 'begin',
  14791. ' if cI=8 then;',
  14792. ' if Self.cI=9 then;',
  14793. ' if 10=cI then;',
  14794. ' if 11=Self.cI then;',
  14795. ' with Self do begin',
  14796. ' if cI=13 then;',
  14797. ' if 14=cI then;',
  14798. ' end;',
  14799. 'end;',
  14800. 'var',
  14801. ' Obj: TObject;',
  14802. ' Cla: TClass;',
  14803. 'begin',
  14804. ' if TObject.cI=21 then ;',
  14805. ' if Obj.cI=22 then ;',
  14806. ' if Cla.cI=23 then ;',
  14807. ' with obj do if ci=24 then;',
  14808. ' with TObject do if ci=25 then;',
  14809. ' with Cla do if ci=26 then;']);
  14810. ConvertProgram;
  14811. CheckSource('TestClass_Const',
  14812. LinesToStr([
  14813. 'rtl.createClass(this, "TObject", null, function () {',
  14814. ' this.cI = 3;',
  14815. ' this.$init = function () {',
  14816. ' };',
  14817. ' this.$final = function () {',
  14818. ' };',
  14819. ' this.DoIt = function () {',
  14820. ' if (this.cI === 4) ;',
  14821. ' if (5 === this.cI) ;',
  14822. ' if (this.cI === 6) ;',
  14823. ' if (7 === this.cI) ;',
  14824. ' if (this.cI === 11) ;',
  14825. ' if (12 === this.cI) ;',
  14826. ' };',
  14827. ' this.DoMore = function () {',
  14828. ' if (this.cI === 8) ;',
  14829. ' if (this.cI === 9) ;',
  14830. ' if (10 === this.cI) ;',
  14831. ' if (11 === this.cI) ;',
  14832. ' if (this.cI === 13) ;',
  14833. ' if (14 === this.cI) ;',
  14834. ' };',
  14835. '});',
  14836. 'this.Obj = null;',
  14837. 'this.Cla = null;',
  14838. '']),
  14839. LinesToStr([
  14840. 'if ($mod.TObject.cI === 21) ;',
  14841. 'if ($mod.Obj.cI === 22) ;',
  14842. 'if ($mod.Cla.cI === 23) ;',
  14843. 'var $with = $mod.Obj;',
  14844. 'if ($with.cI === 24) ;',
  14845. 'var $with1 = $mod.TObject;',
  14846. 'if ($with1.cI === 25) ;',
  14847. 'var $with2 = $mod.Cla;',
  14848. 'if ($with2.cI === 26) ;',
  14849. '']));
  14850. end;
  14851. procedure TTestModule.TestClass_ConstEnum;
  14852. begin
  14853. StartProgram(false);
  14854. Add([
  14855. 'type',
  14856. ' TEnum = (red,blue);',
  14857. ' TObject = class',
  14858. ' end;',
  14859. ' TAnimal = class',
  14860. ' public',
  14861. ' type TSubEnum = (light,dark);',
  14862. ' const a = high(TEnum);',
  14863. ' const b = high(TSubEnum);',
  14864. ' end;',
  14865. ' TBird = class(TAnimal)',
  14866. ' public',
  14867. ' const c = high(TEnum);',
  14868. ' const d = high(TSubEnum);',
  14869. ' end;',
  14870. ' TAnt = class',
  14871. ' public',
  14872. ' const e = high(TEnum);',
  14873. ' const f = high(TBird.TSubEnum);',
  14874. ' end;',
  14875. 'begin',
  14876. '']);
  14877. ConvertProgram;
  14878. CheckSource('TestClass_ConstEnum',
  14879. LinesToStr([
  14880. 'this.TEnum = {',
  14881. ' "0": "red",',
  14882. ' red: 0,',
  14883. ' "1": "blue",',
  14884. ' blue: 1',
  14885. '};',
  14886. 'rtl.createClass(this, "TObject", null, function () {',
  14887. ' this.$init = function () {',
  14888. ' };',
  14889. ' this.$final = function () {',
  14890. ' };',
  14891. '});',
  14892. 'rtl.createClass(this, "TAnimal", this.TObject, function () {',
  14893. ' this.TSubEnum = {',
  14894. ' "0": "light",',
  14895. ' light: 0,',
  14896. ' "1": "dark",',
  14897. ' dark: 1',
  14898. ' };',
  14899. ' this.a = $mod.TEnum.blue;',
  14900. ' this.b = this.TSubEnum.dark;',
  14901. '});',
  14902. 'rtl.createClass(this, "TBird", this.TAnimal, function () {',
  14903. ' this.c = $mod.TEnum.blue;',
  14904. ' this.d = this.TSubEnum.dark;',
  14905. '});',
  14906. 'rtl.createClass(this, "TAnt", this.TObject, function () {',
  14907. ' this.e = $mod.TEnum.blue;',
  14908. ' this.f = $mod.TAnimal.TSubEnum.dark;',
  14909. '});',
  14910. '']),
  14911. LinesToStr([
  14912. '']));
  14913. end;
  14914. procedure TTestModule.TestClass_LocalConstDuplicate_Prg;
  14915. begin
  14916. StartProgram(false);
  14917. Add([
  14918. 'type',
  14919. ' TObject = class',
  14920. ' const cI: longint = 3;',
  14921. ' procedure Fly;',
  14922. ' procedure Run;',
  14923. ' end;',
  14924. ' TBird = class',
  14925. ' procedure Go;',
  14926. ' end;',
  14927. 'procedure tobject.fly;',
  14928. 'const cI: word = 4;',
  14929. 'begin',
  14930. ' if cI=Self.cI then ;',
  14931. 'end;',
  14932. 'procedure tobject.run;',
  14933. 'const cI: word = 5;',
  14934. 'begin',
  14935. ' if cI=Self.cI then ;',
  14936. 'end;',
  14937. 'procedure tbird.go;',
  14938. 'const cI: word = 6;',
  14939. 'begin',
  14940. ' if cI=Self.cI then ;',
  14941. 'end;',
  14942. 'begin',
  14943. '']);
  14944. ConvertProgram;
  14945. CheckSource('TestClass_LocalConstDuplicate_Prg',
  14946. LinesToStr([
  14947. 'rtl.createClass(this, "TObject", null, function () {',
  14948. ' this.cI = 3;',
  14949. ' this.$init = function () {',
  14950. ' };',
  14951. ' this.$final = function () {',
  14952. ' };',
  14953. ' var cI$1 = 4;',
  14954. ' this.Fly = function () {',
  14955. ' if (cI$1 === this.cI) ;',
  14956. ' };',
  14957. ' var cI$2 = 5;',
  14958. ' this.Run = function () {',
  14959. ' if (cI$2 === this.cI) ;',
  14960. ' };',
  14961. '});',
  14962. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  14963. ' var cI$3 = 6;',
  14964. ' this.Go = function () {',
  14965. ' if (cI$3 === this.cI) ;',
  14966. ' };',
  14967. '});',
  14968. '']),
  14969. LinesToStr([
  14970. '']));
  14971. end;
  14972. procedure TTestModule.TestClass_LocalConstDuplicate_Unit;
  14973. begin
  14974. StartUnit(false);
  14975. Add([
  14976. 'interface',
  14977. 'type',
  14978. ' TObject = class',
  14979. ' const cI: longint = 3;',
  14980. ' procedure Fly;',
  14981. ' procedure Run;',
  14982. ' end;',
  14983. ' TBird = class',
  14984. ' procedure Go;',
  14985. ' end;',
  14986. 'implementation',
  14987. 'procedure tobject.fly;',
  14988. 'const cI: word = 4;',
  14989. 'begin',
  14990. ' if cI=Self.cI then ;',
  14991. 'end;',
  14992. 'procedure tobject.run;',
  14993. 'const cI: word = 5;',
  14994. 'begin',
  14995. ' if cI=Self.cI then ;',
  14996. 'end;',
  14997. 'procedure tbird.go;',
  14998. 'const cI: word = 6;',
  14999. 'begin',
  15000. ' if cI=Self.cI then ;',
  15001. 'end;',
  15002. '']);
  15003. ConvertUnit;
  15004. CheckSource('TestClass_LocalConstDuplicate_Unit',
  15005. LinesToStr([
  15006. 'rtl.createClass(this, "TObject", null, function () {',
  15007. ' this.cI = 3;',
  15008. ' this.$init = function () {',
  15009. ' };',
  15010. ' this.$final = function () {',
  15011. ' };',
  15012. ' var cI$1 = 4;',
  15013. ' this.Fly = function () {',
  15014. ' if (cI$1 === this.cI) ;',
  15015. ' };',
  15016. ' var cI$2 = 5;',
  15017. ' this.Run = function () {',
  15018. ' if (cI$2 === this.cI) ;',
  15019. ' };',
  15020. '});',
  15021. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15022. ' var cI$3 = 6;',
  15023. ' this.Go = function () {',
  15024. ' if (cI$3 === this.cI) ;',
  15025. ' };',
  15026. '});',
  15027. '']),
  15028. '',
  15029. '');
  15030. end;
  15031. procedure TTestModule.TestClass_LocalVarSelfFail;
  15032. begin
  15033. StartProgram(false);
  15034. Add([
  15035. 'type',
  15036. ' TObject = class',
  15037. ' constructor Create;',
  15038. ' end;',
  15039. 'constructor tobject.create;',
  15040. 'var self: longint;',
  15041. 'begin',
  15042. 'end',
  15043. 'begin',
  15044. '']);
  15045. SetExpectedPasResolverError('Duplicate identifier "self" at (0)',nDuplicateIdentifier);
  15046. ConvertProgram;
  15047. end;
  15048. procedure TTestModule.TestClass_ArgSelfFail;
  15049. begin
  15050. StartProgram(false);
  15051. Add([
  15052. 'type',
  15053. ' TObject = class',
  15054. ' procedure DoIt(Self: longint);',
  15055. ' end;',
  15056. 'procedure tobject.doit(self: longint);',
  15057. 'begin',
  15058. 'end',
  15059. 'begin',
  15060. '']);
  15061. SetExpectedPasResolverError('Duplicate identifier "Self" at test1.pp(5,24)',nDuplicateIdentifier);
  15062. ConvertProgram;
  15063. end;
  15064. procedure TTestModule.TestClass_NestedProcSelf;
  15065. begin
  15066. StartProgram(false);
  15067. Add([
  15068. 'type',
  15069. ' TObject = class',
  15070. ' Key: longint;',
  15071. ' class var State: longint;',
  15072. ' procedure DoIt;',
  15073. ' function GetSize: longint; virtual; abstract;',
  15074. ' procedure SetSize(Value: longint); virtual; abstract;',
  15075. ' property Size: longint read GetSize write SetSize;',
  15076. ' end;',
  15077. 'procedure tobject.doit;',
  15078. ' procedure Sub;',
  15079. ' begin',
  15080. ' key:=key+2;',
  15081. ' self.key:=self.key+3;',
  15082. ' state:=state+4;',
  15083. ' self.state:=self.state+5;',
  15084. ' tobject.state:=tobject.state+6;',
  15085. ' size:=size+7;',
  15086. ' self.size:=self.size+8;',
  15087. ' end;',
  15088. 'begin',
  15089. ' sub;',
  15090. ' key:=key+12;',
  15091. ' self.key:=self.key+13;',
  15092. ' state:=state+14;',
  15093. ' self.state:=self.state+15;',
  15094. ' tobject.state:=tobject.state+16;',
  15095. ' size:=size+17;',
  15096. ' self.size:=self.size+18;',
  15097. 'end;',
  15098. 'begin',
  15099. '']);
  15100. ConvertProgram;
  15101. CheckSource('TestClass_NestedProcSelf',
  15102. LinesToStr([ // statements
  15103. 'rtl.createClass(this, "TObject", null, function () {',
  15104. ' this.State = 0;',
  15105. ' this.$init = function () {',
  15106. ' this.Key = 0;',
  15107. ' };',
  15108. ' this.$final = function () {',
  15109. ' };',
  15110. ' this.DoIt = function () {',
  15111. ' var $Self = this;',
  15112. ' function Sub() {',
  15113. ' $Self.Key = $Self.Key + 2;',
  15114. ' $Self.Key = $Self.Key + 3;',
  15115. ' $mod.TObject.State = $Self.State + 4;',
  15116. ' $mod.TObject.State = $Self.State + 5;',
  15117. ' $mod.TObject.State = $mod.TObject.State + 6;',
  15118. ' $Self.SetSize($Self.GetSize() + 7);',
  15119. ' $Self.SetSize($Self.GetSize() + 8);',
  15120. ' };',
  15121. ' Sub();',
  15122. ' this.Key = this.Key + 12;',
  15123. ' $Self.Key = $Self.Key + 13;',
  15124. ' $mod.TObject.State = this.State + 14;',
  15125. ' $mod.TObject.State = $Self.State + 15;',
  15126. ' $mod.TObject.State = $mod.TObject.State + 16;',
  15127. ' this.SetSize(this.GetSize() + 17);',
  15128. ' $Self.SetSize($Self.GetSize() + 18);',
  15129. ' };',
  15130. '});',
  15131. '']),
  15132. LinesToStr([ // $mod.$main
  15133. '']));
  15134. end;
  15135. procedure TTestModule.TestClass_NestedProcSelf2;
  15136. begin
  15137. StartProgram(false);
  15138. Add([
  15139. 'type',
  15140. ' TObject = class',
  15141. ' Key: longint;',
  15142. ' class var State: longint;',
  15143. ' function GetSize: longint; virtual; abstract;',
  15144. ' procedure SetSize(Value: longint); virtual; abstract;',
  15145. ' property Size: longint read GetSize write SetSize;',
  15146. ' end;',
  15147. ' TBird = class',
  15148. ' procedure DoIt;',
  15149. ' end;',
  15150. 'procedure tbird.doit;',
  15151. ' procedure Sub;',
  15152. ' begin',
  15153. ' key:=key+2;',
  15154. ' self.key:=self.key+3;',
  15155. ' state:=state+4;',
  15156. ' self.state:=self.state+5;',
  15157. ' tobject.state:=tobject.state+6;',
  15158. ' size:=size+7;',
  15159. ' self.size:=self.size+8;',
  15160. ' end;',
  15161. 'begin',
  15162. ' sub;',
  15163. ' key:=key+12;',
  15164. ' self.key:=self.key+13;',
  15165. ' state:=state+14;',
  15166. ' self.state:=self.state+15;',
  15167. ' tobject.state:=tobject.state+16;',
  15168. ' size:=size+17;',
  15169. ' self.size:=self.size+18;',
  15170. 'end;',
  15171. 'begin',
  15172. '']);
  15173. ConvertProgram;
  15174. CheckSource('TestClass_NestedProcSelf2',
  15175. LinesToStr([ // statements
  15176. 'rtl.createClass(this, "TObject", null, function () {',
  15177. ' this.State = 0;',
  15178. ' this.$init = function () {',
  15179. ' this.Key = 0;',
  15180. ' };',
  15181. ' this.$final = function () {',
  15182. ' };',
  15183. '});',
  15184. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15185. ' this.DoIt = function () {',
  15186. ' var $Self = this;',
  15187. ' function Sub() {',
  15188. ' $Self.Key = $Self.Key + 2;',
  15189. ' $Self.Key = $Self.Key + 3;',
  15190. ' $mod.TObject.State = $Self.State + 4;',
  15191. ' $mod.TObject.State = $Self.State + 5;',
  15192. ' $mod.TObject.State = $mod.TObject.State + 6;',
  15193. ' $Self.SetSize($Self.GetSize() + 7);',
  15194. ' $Self.SetSize($Self.GetSize() + 8);',
  15195. ' };',
  15196. ' Sub();',
  15197. ' this.Key = this.Key + 12;',
  15198. ' $Self.Key = $Self.Key + 13;',
  15199. ' $mod.TObject.State = this.State + 14;',
  15200. ' $mod.TObject.State = $Self.State + 15;',
  15201. ' $mod.TObject.State = $mod.TObject.State + 16;',
  15202. ' this.SetSize(this.GetSize() + 17);',
  15203. ' $Self.SetSize($Self.GetSize() + 18);',
  15204. ' };',
  15205. '});',
  15206. '']),
  15207. LinesToStr([ // $mod.$main
  15208. '']));
  15209. end;
  15210. procedure TTestModule.TestClass_NestedProcClassSelf;
  15211. begin
  15212. StartProgram(false);
  15213. Add([
  15214. 'type',
  15215. ' TObject = class',
  15216. ' class var State: longint;',
  15217. ' class procedure DoIt;',
  15218. ' class function GetSize: longint; virtual; abstract;',
  15219. ' class procedure SetSize(Value: longint); virtual; abstract;',
  15220. ' class property Size: longint read GetSize write SetSize;',
  15221. ' end;',
  15222. 'class procedure tobject.doit;',
  15223. ' procedure Sub;',
  15224. ' begin',
  15225. ' state:=state+2;',
  15226. ' self.state:=self.state+3;',
  15227. ' tobject.state:=tobject.state+4;',
  15228. ' size:=size+5;',
  15229. ' self.size:=self.size+6;',
  15230. ' tobject.size:=tobject.size+7;',
  15231. ' end;',
  15232. 'begin',
  15233. ' sub;',
  15234. ' state:=state+12;',
  15235. ' self.state:=self.state+13;',
  15236. ' tobject.state:=tobject.state+14;',
  15237. ' size:=size+15;',
  15238. ' self.size:=self.size+16;',
  15239. ' tobject.size:=tobject.size+17;',
  15240. 'end;',
  15241. 'begin',
  15242. '']);
  15243. ConvertProgram;
  15244. CheckSource('TestClass_NestedProcClassSelf',
  15245. LinesToStr([ // statements
  15246. 'rtl.createClass(this, "TObject", null, function () {',
  15247. ' this.State = 0;',
  15248. ' this.$init = function () {',
  15249. ' };',
  15250. ' this.$final = function () {',
  15251. ' };',
  15252. ' this.DoIt = function () {',
  15253. ' var $Self = this;',
  15254. ' function Sub() {',
  15255. ' $mod.TObject.State = $Self.State + 2;',
  15256. ' $mod.TObject.State = $Self.State + 3;',
  15257. ' $mod.TObject.State = $mod.TObject.State + 4;',
  15258. ' $Self.SetSize($Self.GetSize() + 5);',
  15259. ' $Self.SetSize($Self.GetSize() + 6);',
  15260. ' $mod.TObject.SetSize($mod.TObject.GetSize() + 7);',
  15261. ' };',
  15262. ' Sub();',
  15263. ' $mod.TObject.State = this.State + 12;',
  15264. ' $mod.TObject.State = $Self.State + 13;',
  15265. ' $mod.TObject.State = $mod.TObject.State + 14;',
  15266. ' this.SetSize(this.GetSize() + 15);',
  15267. ' $Self.SetSize($Self.GetSize() + 16);',
  15268. ' $mod.TObject.SetSize($mod.TObject.GetSize() + 17);',
  15269. ' };',
  15270. '});',
  15271. '']),
  15272. LinesToStr([ // $mod.$main
  15273. '']));
  15274. end;
  15275. procedure TTestModule.TestClass_NestedProcCallInherited;
  15276. begin
  15277. StartProgram(false);
  15278. Add([
  15279. 'type',
  15280. ' TObject = class',
  15281. ' function DoIt(k: boolean): longint; virtual;',
  15282. ' end;',
  15283. ' TBird = class',
  15284. ' function DoIt(k: boolean): longint; override;',
  15285. ' end;',
  15286. 'function tobject.doit(k: boolean): longint;',
  15287. 'begin',
  15288. 'end;',
  15289. 'function tbird.doit(k: boolean): longint;',
  15290. ' procedure Sub;',
  15291. ' begin',
  15292. ' inherited DoIt(true);',
  15293. //' if inherited DoIt(false)=4 then ;',
  15294. ' end;',
  15295. 'begin',
  15296. ' Sub;',
  15297. ' inherited;',
  15298. ' inherited DoIt(true);',
  15299. //' if inherited DoIt(false)=14 then ;',
  15300. 'end;',
  15301. 'begin',
  15302. '']);
  15303. ConvertProgram;
  15304. CheckSource('TestClass_NestedProcCallInherited',
  15305. LinesToStr([ // statements
  15306. 'rtl.createClass(this, "TObject", null, function () {',
  15307. ' this.$init = function () {',
  15308. ' };',
  15309. ' this.$final = function () {',
  15310. ' };',
  15311. ' this.DoIt = function (k) {',
  15312. ' var Result = 0;',
  15313. ' return Result;',
  15314. ' };',
  15315. '});',
  15316. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15317. ' this.DoIt = function (k) {',
  15318. ' var $Self = this;',
  15319. ' var Result = 0;',
  15320. ' function Sub() {',
  15321. ' $mod.TObject.DoIt.call($Self, true);',
  15322. ' };',
  15323. ' Sub();',
  15324. ' $mod.TObject.DoIt.apply(this, arguments);',
  15325. ' $mod.TObject.DoIt.call(this, true);',
  15326. ' return Result;',
  15327. ' };',
  15328. '});',
  15329. '']),
  15330. LinesToStr([ // $mod.$main
  15331. '']));
  15332. end;
  15333. procedure TTestModule.TestClass_TObjectFree;
  15334. begin
  15335. StartProgram(false);
  15336. Add([
  15337. 'type',
  15338. ' TObject = class',
  15339. ' Obj: tobject;',
  15340. ' procedure Free;',
  15341. ' procedure Release;',
  15342. ' end;',
  15343. 'procedure tobject.free;',
  15344. 'begin',
  15345. 'end;',
  15346. 'procedure tobject.release;',
  15347. 'begin',
  15348. ' free;',
  15349. ' if true then free;',
  15350. 'end;',
  15351. 'function DoIt(o: tobject): tobject;',
  15352. 'var l: tobject;',
  15353. 'begin',
  15354. ' o.free;',
  15355. ' o.free();',
  15356. ' l.free;',
  15357. ' l.free();',
  15358. ' o.obj.free;',
  15359. ' o.obj.free();',
  15360. ' with o do obj.free;',
  15361. ' with o do obj.free();',
  15362. ' result.Free;',
  15363. ' result.Free();',
  15364. 'end;',
  15365. 'var o: tobject;',
  15366. ' a: array of tobject;',
  15367. 'begin',
  15368. ' o.free;',
  15369. ' o.obj.free;',
  15370. ' a[1+2].free;',
  15371. '']);
  15372. ConvertProgram;
  15373. CheckSource('TestClass_TObjectFree',
  15374. LinesToStr([ // statements
  15375. 'rtl.createClass(this, "TObject", null, function () {',
  15376. ' this.$init = function () {',
  15377. ' this.Obj = null;',
  15378. ' };',
  15379. ' this.$final = function () {',
  15380. ' this.Obj = undefined;',
  15381. ' };',
  15382. ' this.Free = function () {',
  15383. ' };',
  15384. ' this.Release = function () {',
  15385. ' this.Free();',
  15386. ' if (true) this.Free();',
  15387. ' };',
  15388. '});',
  15389. 'this.DoIt = function (o) {',
  15390. ' var Result = null;',
  15391. ' var l = null;',
  15392. ' o = rtl.freeLoc(o);',
  15393. ' o = rtl.freeLoc(o);',
  15394. ' l = rtl.freeLoc(l);',
  15395. ' l = rtl.freeLoc(l);',
  15396. ' rtl.free(o, "Obj");',
  15397. ' rtl.free(o, "Obj");',
  15398. ' rtl.free(o, "Obj");',
  15399. ' rtl.free(o, "Obj");',
  15400. ' Result = rtl.freeLoc(Result);',
  15401. ' Result = rtl.freeLoc(Result);',
  15402. ' return Result;',
  15403. '};',
  15404. 'this.o = null;',
  15405. 'this.a = [];',
  15406. '']),
  15407. LinesToStr([ // $mod.$main
  15408. 'rtl.free($mod, "o");',
  15409. 'rtl.free($mod.o, "Obj");',
  15410. 'rtl.free($mod.a, 1 + 2);',
  15411. '']));
  15412. end;
  15413. procedure TTestModule.TestClass_TObjectFree_VarArg;
  15414. begin
  15415. StartProgram(false);
  15416. Add([
  15417. 'type',
  15418. ' TObject = class',
  15419. ' Obj: tobject;',
  15420. ' procedure Free;',
  15421. ' end;',
  15422. 'procedure tobject.free;',
  15423. 'begin',
  15424. 'end;',
  15425. 'procedure DoIt(var o: tobject);',
  15426. 'begin',
  15427. ' o.free;',
  15428. ' o.free();',
  15429. 'end;',
  15430. 'begin',
  15431. '']);
  15432. ConvertProgram;
  15433. CheckSource('TestClass_TObjectFree_VarArg',
  15434. LinesToStr([ // statements
  15435. 'rtl.createClass(this, "TObject", null, function () {',
  15436. ' this.$init = function () {',
  15437. ' this.Obj = null;',
  15438. ' };',
  15439. ' this.$final = function () {',
  15440. ' this.Obj = undefined;',
  15441. ' };',
  15442. ' this.Free = function () {',
  15443. ' };',
  15444. '});',
  15445. 'this.DoIt = function (o) {',
  15446. ' o.set(rtl.freeLoc(o.get()));',
  15447. ' o.set(rtl.freeLoc(o.get()));',
  15448. '};',
  15449. '']),
  15450. LinesToStr([ // $mod.$main
  15451. '']));
  15452. end;
  15453. procedure TTestModule.TestClass_TObjectFreeNewInstance;
  15454. begin
  15455. StartProgram(false);
  15456. Add([
  15457. 'type',
  15458. ' TObject = class',
  15459. ' constructor Create;',
  15460. ' procedure Free;',
  15461. ' end;',
  15462. 'constructor TObject.Create; begin end;',
  15463. 'procedure tobject.free; begin end;',
  15464. 'begin',
  15465. ' with tobject.create do free;',
  15466. '']);
  15467. ConvertProgram;
  15468. CheckSource('TestClass_TObjectFreeNewInstance',
  15469. LinesToStr([ // statements
  15470. 'rtl.createClass(this, "TObject", null, function () {',
  15471. ' this.$init = function () {',
  15472. ' };',
  15473. ' this.$final = function () {',
  15474. ' };',
  15475. ' this.Create = function () {',
  15476. ' return this;',
  15477. ' };',
  15478. ' this.Free = function () {',
  15479. ' };',
  15480. '});',
  15481. '']),
  15482. LinesToStr([ // $mod.$main
  15483. 'var $with = $mod.TObject.$create("Create");',
  15484. '$with=rtl.freeLoc($with);',
  15485. '']));
  15486. end;
  15487. procedure TTestModule.TestClass_TObjectFreeLowerCase;
  15488. begin
  15489. StartProgram(false);
  15490. Add([
  15491. 'type',
  15492. ' TObject = class',
  15493. ' destructor Destroy;',
  15494. ' procedure Free;',
  15495. ' end;',
  15496. 'destructor TObject.Destroy; begin end;',
  15497. 'procedure tobject.free; begin end;',
  15498. 'var o: tobject;',
  15499. 'begin',
  15500. ' o.free;',
  15501. '']);
  15502. Converter.UseLowerCase:=true;
  15503. ConvertProgram;
  15504. CheckSource('TestClass_TObjectFreeLowerCase',
  15505. LinesToStr([ // statements
  15506. 'rtl.createClass(this, "tobject", null, function () {',
  15507. ' this.$init = function () {',
  15508. ' };',
  15509. ' this.$final = function () {',
  15510. ' };',
  15511. ' rtl.tObjectDestroy = "destroy";',
  15512. ' this.destroy = function () {',
  15513. ' };',
  15514. ' this.free = function () {',
  15515. ' };',
  15516. '});',
  15517. 'this.o = null;',
  15518. '']),
  15519. LinesToStr([ // $mod.$main
  15520. 'rtl.free($mod, "o");',
  15521. '']));
  15522. end;
  15523. procedure TTestModule.TestClass_TObjectFreeFunctionFail;
  15524. begin
  15525. StartProgram(false);
  15526. Add([
  15527. 'type',
  15528. ' TObject = class',
  15529. ' procedure Free;',
  15530. ' function GetObj: tobject; virtual; abstract;',
  15531. ' end;',
  15532. 'procedure tobject.free;',
  15533. 'begin',
  15534. 'end;',
  15535. 'var o: tobject;',
  15536. 'begin',
  15537. ' o.getobj.free;',
  15538. '']);
  15539. SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
  15540. ConvertProgram;
  15541. end;
  15542. procedure TTestModule.TestClass_TObjectFreePropertyFail;
  15543. begin
  15544. StartProgram(false);
  15545. Add([
  15546. 'type',
  15547. ' TObject = class',
  15548. ' procedure Free;',
  15549. ' FObj: TObject;',
  15550. ' property Obj: tobject read FObj write FObj;',
  15551. ' end;',
  15552. 'procedure tobject.free;',
  15553. 'begin',
  15554. 'end;',
  15555. 'var o: tobject;',
  15556. 'begin',
  15557. ' o.obj.free;',
  15558. '']);
  15559. SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
  15560. ConvertProgram;
  15561. end;
  15562. procedure TTestModule.TestClass_ForIn;
  15563. begin
  15564. StartProgram(false);
  15565. Add([
  15566. 'type',
  15567. ' TObject = class end;',
  15568. ' TItem = TObject;',
  15569. ' TEnumerator = class',
  15570. ' FCurrent: TItem;',
  15571. ' property Current: TItem read FCurrent;',
  15572. ' function MoveNext: boolean;',
  15573. ' end;',
  15574. ' TBird = class',
  15575. ' function GetEnumerator: TEnumerator;',
  15576. ' end;',
  15577. 'function TEnumerator.MoveNext: boolean;',
  15578. 'begin',
  15579. 'end;',
  15580. 'function TBird.GetEnumerator: TEnumerator;',
  15581. 'begin',
  15582. 'end;',
  15583. 'var',
  15584. ' b: TBird;',
  15585. ' i, i2: TItem;',
  15586. 'begin',
  15587. ' for i in b do i2:=i;']);
  15588. ConvertProgram;
  15589. CheckSource('TestClass_ForIn',
  15590. LinesToStr([ // statements
  15591. 'rtl.createClass(this, "TObject", null, function () {',
  15592. ' this.$init = function () {',
  15593. ' };',
  15594. ' this.$final = function () {',
  15595. ' };',
  15596. '});',
  15597. 'rtl.createClass(this, "TEnumerator", this.TObject, function () {',
  15598. ' this.$init = function () {',
  15599. ' $mod.TObject.$init.call(this);',
  15600. ' this.FCurrent = null;',
  15601. ' };',
  15602. ' this.$final = function () {',
  15603. ' this.FCurrent = undefined;',
  15604. ' $mod.TObject.$final.call(this);',
  15605. ' };',
  15606. ' this.MoveNext = function () {',
  15607. ' var Result = false;',
  15608. ' return Result;',
  15609. ' };',
  15610. '});',
  15611. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15612. ' this.GetEnumerator = function () {',
  15613. ' var Result = null;',
  15614. ' return Result;',
  15615. ' };',
  15616. '});',
  15617. 'this.b = null;',
  15618. 'this.i = null;',
  15619. 'this.i2 = null;'
  15620. ]),
  15621. LinesToStr([ // $mod.$main
  15622. 'var $in = $mod.b.GetEnumerator();',
  15623. 'try {',
  15624. ' while ($in.MoveNext()){',
  15625. ' $mod.i = $in.FCurrent;',
  15626. ' $mod.i2 = $mod.i;',
  15627. ' }',
  15628. '} finally {',
  15629. ' $in = rtl.freeLoc($in)',
  15630. '};',
  15631. '']));
  15632. end;
  15633. procedure TTestModule.TestClass_DispatchMessage;
  15634. begin
  15635. StartProgram(false);
  15636. Add([
  15637. 'type',
  15638. ' TObject = class',
  15639. ' {$DispatchField DispInt}',
  15640. ' procedure Dispatch(var Msg); virtual; abstract;',
  15641. ' {$DispatchStrField DispStr}',
  15642. ' procedure DispatchStr(var Msg); virtual; abstract;',
  15643. ' end;',
  15644. ' THopMsg = record',
  15645. ' DispInt: longint;',
  15646. ' end;',
  15647. ' TPutMsg = record',
  15648. ' DispStr: string;',
  15649. ' end;',
  15650. ' TBird = class',
  15651. ' procedure Fly(var Msg); virtual; abstract; message 2;',
  15652. ' procedure Run; overload; virtual; abstract;',
  15653. ' procedure Run(var Msg); overload; message ''Fast'';',
  15654. ' procedure Hop(var Msg: THopMsg); virtual; abstract; message 3;',
  15655. ' procedure Put(var Msg: TPutMsg); virtual; abstract; message ''foo'';',
  15656. ' end;',
  15657. 'procedure TBird.Run(var Msg);',
  15658. 'begin',
  15659. 'end;',
  15660. 'begin',
  15661. '']);
  15662. ConvertProgram;
  15663. CheckSource('TestClass_Message',
  15664. LinesToStr([ // statements
  15665. 'rtl.createClass(this, "TObject", null, function () {',
  15666. ' this.$init = function () {',
  15667. ' };',
  15668. ' this.$final = function () {',
  15669. ' };',
  15670. '});',
  15671. 'rtl.recNewT(this, "THopMsg", function () {',
  15672. ' this.DispInt = 0;',
  15673. ' this.$eq = function (b) {',
  15674. ' return this.DispInt === b.DispInt;',
  15675. ' };',
  15676. ' this.$assign = function (s) {',
  15677. ' this.DispInt = s.DispInt;',
  15678. ' return this;',
  15679. ' };',
  15680. '});',
  15681. 'rtl.recNewT(this, "TPutMsg", function () {',
  15682. ' this.DispStr = "";',
  15683. ' this.$eq = function (b) {',
  15684. ' return this.DispStr === b.DispStr;',
  15685. ' };',
  15686. ' this.$assign = function (s) {',
  15687. ' this.DispStr = s.DispStr;',
  15688. ' return this;',
  15689. ' };',
  15690. '});',
  15691. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15692. ' this.Run$1 = function (Msg) {',
  15693. ' };',
  15694. ' this.$msgint = {',
  15695. ' "2": "Fly",',
  15696. ' "3": "Hop"',
  15697. ' };',
  15698. ' this.$msgstr = {',
  15699. ' Fast: "Run$1",',
  15700. ' foo: "Put"',
  15701. ' };',
  15702. '});',
  15703. '']),
  15704. LinesToStr([ // $mod.$main
  15705. '']));
  15706. end;
  15707. procedure TTestModule.TestClass_Message_DuplicateIntFail;
  15708. begin
  15709. StartProgram(false);
  15710. Add([
  15711. 'type',
  15712. ' TObject = class',
  15713. ' procedure Fly(var Msg); virtual; abstract; message 3;',
  15714. ' procedure Run(var Msg); virtual; abstract; message 1+2;',
  15715. ' end;',
  15716. 'begin',
  15717. '']);
  15718. SetExpectedPasResolverError('Duplicate message id "3" at test1.pp(5,56)',nDuplicateMessageIdXAtY);
  15719. ConvertProgram;
  15720. end;
  15721. procedure TTestModule.TestClass_DispatchMessage_WrongFieldNameFail;
  15722. begin
  15723. StartProgram(false);
  15724. Add([
  15725. 'type',
  15726. ' TObject = class',
  15727. ' {$dispatchfield Msg}',
  15728. ' procedure Dispatch(var Msg); virtual; abstract;',
  15729. ' end;',
  15730. ' TFlyMsg = record',
  15731. ' FlyId: longint;',
  15732. ' end;',
  15733. ' TBird = class',
  15734. ' procedure Fly(var Msg: TFlyMsg); virtual; abstract; message 3;',
  15735. ' end;',
  15736. 'begin',
  15737. '']);
  15738. ConvertProgram;
  15739. CheckHint(mtWarning,nDispatchRequiresX,'Dispatch requires record field "Msg"');
  15740. end;
  15741. procedure TTestModule.TestClassOf_Create;
  15742. begin
  15743. StartProgram(false);
  15744. Add('type');
  15745. Add(' TObject = class');
  15746. Add(' constructor Create;');
  15747. Add(' end;');
  15748. Add(' TClass = class of TObject;');
  15749. Add('constructor tobject.create; begin end;');
  15750. Add('var');
  15751. Add(' Obj: tobject;');
  15752. Add(' C: tclass;');
  15753. Add('begin');
  15754. Add(' obj:=C.create;');
  15755. Add(' with c do obj:=create;');
  15756. ConvertProgram;
  15757. CheckSource('TestClassOf_Create',
  15758. LinesToStr([ // statements
  15759. 'rtl.createClass(this, "TObject", null, function () {',
  15760. ' this.$init = function () {',
  15761. ' };',
  15762. ' this.$final = function () {',
  15763. ' };',
  15764. ' this.Create = function () {',
  15765. ' return this;',
  15766. ' };',
  15767. '});',
  15768. 'this.Obj = null;',
  15769. 'this.C = null;'
  15770. ]),
  15771. LinesToStr([ // $mod.$main
  15772. '$mod.Obj = $mod.C.$create("Create");',
  15773. 'var $with = $mod.C;',
  15774. '$mod.Obj = $with.$create("Create");',
  15775. '']));
  15776. end;
  15777. procedure TTestModule.TestClassOf_Call;
  15778. begin
  15779. StartProgram(false);
  15780. Add('type');
  15781. Add(' TObject = class');
  15782. Add(' class procedure DoIt;');
  15783. Add(' end;');
  15784. Add(' TClass = class of TObject;');
  15785. Add('class procedure tobject.doit; begin end;');
  15786. Add('var');
  15787. Add(' C: tclass;');
  15788. Add('begin');
  15789. Add(' c.doit;');
  15790. Add(' with c do doit;');
  15791. ConvertProgram;
  15792. CheckSource('TestClassOf_Call',
  15793. LinesToStr([ // statements
  15794. 'rtl.createClass(this, "TObject", null, function () {',
  15795. ' this.$init = function () {',
  15796. ' };',
  15797. ' this.$final = function () {',
  15798. ' };',
  15799. ' this.DoIt = function () {',
  15800. ' };',
  15801. '});',
  15802. 'this.C = null;'
  15803. ]),
  15804. LinesToStr([ // $mod.$main
  15805. '$mod.C.DoIt();',
  15806. 'var $with = $mod.C;',
  15807. '$with.DoIt();',
  15808. '']));
  15809. end;
  15810. procedure TTestModule.TestClassOf_Assign;
  15811. begin
  15812. StartProgram(false);
  15813. Add('type');
  15814. Add(' TClass = class of TObject;');
  15815. Add(' TObject = class');
  15816. Add(' ClassType: TClass; ');
  15817. Add(' end;');
  15818. Add('var');
  15819. Add(' Obj: tobject;');
  15820. Add(' C: tclass;');
  15821. Add('begin');
  15822. Add(' c:=nil;');
  15823. Add(' c:=obj.classtype;');
  15824. ConvertProgram;
  15825. CheckSource('TestClassOf_Assign',
  15826. LinesToStr([ // statements
  15827. 'rtl.createClass(this, "TObject", null, function () {',
  15828. ' this.$init = function () {',
  15829. ' this.ClassType = null;',
  15830. ' };',
  15831. ' this.$final = function () {',
  15832. ' this.ClassType = undefined;',
  15833. ' };',
  15834. '});',
  15835. 'this.Obj = null;',
  15836. 'this.C = null;'
  15837. ]),
  15838. LinesToStr([ // $mod.$main
  15839. '$mod.C = null;',
  15840. '$mod.C = $mod.Obj.ClassType;',
  15841. '']));
  15842. end;
  15843. procedure TTestModule.TestClassOf_Is;
  15844. begin
  15845. StartProgram(false);
  15846. Add('type');
  15847. Add(' TClass = class of TObject;');
  15848. Add(' TObject = class');
  15849. Add(' end;');
  15850. Add(' TCar = class');
  15851. Add(' end;');
  15852. Add(' TCars = class of TCar;');
  15853. Add('var');
  15854. Add(' Obj: tobject;');
  15855. Add(' C: tclass;');
  15856. Add(' Cars: tcars;');
  15857. Add('begin');
  15858. Add(' if c is tcar then ;');
  15859. Add(' if c is tcars then ;');
  15860. ConvertProgram;
  15861. CheckSource('TestClassOf_Is',
  15862. LinesToStr([ // statements
  15863. 'rtl.createClass(this, "TObject", null, function () {',
  15864. ' this.$init = function () {',
  15865. ' };',
  15866. ' this.$final = function () {',
  15867. ' };',
  15868. '});',
  15869. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  15870. '});',
  15871. 'this.Obj = null;',
  15872. 'this.C = null;',
  15873. 'this.Cars = null;'
  15874. ]),
  15875. LinesToStr([ // $mod.$main
  15876. 'if(rtl.is($mod.C,$mod.TCar));',
  15877. 'if(rtl.is($mod.C,$mod.TCar));',
  15878. '']));
  15879. end;
  15880. procedure TTestModule.TestClassOf_Compare;
  15881. begin
  15882. StartProgram(false);
  15883. Add('type');
  15884. Add(' TClass = class of TObject;');
  15885. Add(' TObject = class');
  15886. Add(' ClassType: TClass; ');
  15887. Add(' end;');
  15888. Add('var');
  15889. Add(' b: boolean;');
  15890. Add(' Obj: tobject;');
  15891. Add(' C: tclass;');
  15892. Add('begin');
  15893. Add(' b:=c=nil;');
  15894. Add(' b:=nil=c;');
  15895. Add(' b:=c=obj.classtype;');
  15896. Add(' b:=obj.classtype=c;');
  15897. Add(' b:=c=TObject;');
  15898. Add(' b:=TObject=c;');
  15899. Add(' b:=c<>nil;');
  15900. Add(' b:=nil<>c;');
  15901. Add(' b:=c<>obj.classtype;');
  15902. Add(' b:=obj.classtype<>c;');
  15903. Add(' b:=c<>TObject;');
  15904. Add(' b:=TObject<>c;');
  15905. ConvertProgram;
  15906. CheckSource('TestClassOf_Compare',
  15907. LinesToStr([ // statements
  15908. 'rtl.createClass(this, "TObject", null, function () {',
  15909. ' this.$init = function () {',
  15910. ' this.ClassType = null;',
  15911. ' };',
  15912. ' this.$final = function () {',
  15913. ' this.ClassType = undefined;',
  15914. ' };',
  15915. '});',
  15916. 'this.b = false;',
  15917. 'this.Obj = null;',
  15918. 'this.C = null;'
  15919. ]),
  15920. LinesToStr([ // $mod.$main
  15921. '$mod.b = $mod.C === null;',
  15922. '$mod.b = null === $mod.C;',
  15923. '$mod.b = $mod.C === $mod.Obj.ClassType;',
  15924. '$mod.b = $mod.Obj.ClassType === $mod.C;',
  15925. '$mod.b = $mod.C === $mod.TObject;',
  15926. '$mod.b = $mod.TObject === $mod.C;',
  15927. '$mod.b = $mod.C !== null;',
  15928. '$mod.b = null !== $mod.C;',
  15929. '$mod.b = $mod.C !== $mod.Obj.ClassType;',
  15930. '$mod.b = $mod.Obj.ClassType !== $mod.C;',
  15931. '$mod.b = $mod.C !== $mod.TObject;',
  15932. '$mod.b = $mod.TObject !== $mod.C;',
  15933. '']));
  15934. end;
  15935. procedure TTestModule.TestClassOf_ClassVar;
  15936. begin
  15937. StartProgram(false);
  15938. Add('type');
  15939. Add(' TObject = class');
  15940. Add(' class var id: longint;');
  15941. Add(' end;');
  15942. Add(' TClass = class of TObject;');
  15943. Add('var');
  15944. Add(' C: tclass;');
  15945. Add('begin');
  15946. Add(' C.id:=C.id;');
  15947. ConvertProgram;
  15948. CheckSource('TestClassOf_ClassVar',
  15949. LinesToStr([ // statements
  15950. 'rtl.createClass(this, "TObject", null, function () {',
  15951. ' this.id = 0;',
  15952. ' this.$init = function () {',
  15953. ' };',
  15954. ' this.$final = function () {',
  15955. ' };',
  15956. '});',
  15957. 'this.C = null;'
  15958. ]),
  15959. LinesToStr([ // $mod.$main
  15960. '$mod.TObject.id = $mod.C.id;',
  15961. '']));
  15962. end;
  15963. procedure TTestModule.TestClassOf_ClassMethod;
  15964. begin
  15965. StartProgram(false);
  15966. Add('type');
  15967. Add(' TObject = class');
  15968. Add(' class function DoIt(i: longint = 0): longint;');
  15969. Add(' end;');
  15970. Add(' TClass = class of TObject;');
  15971. Add('class function tobject.doit(i: longint = 0): longint; begin end;');
  15972. Add('var');
  15973. Add(' i: longint;');
  15974. Add(' C: tclass;');
  15975. Add('begin');
  15976. Add(' C.DoIt;');
  15977. Add(' C.DoIt();');
  15978. Add(' i:=C.DoIt;');
  15979. Add(' i:=C.DoIt();');
  15980. ConvertProgram;
  15981. CheckSource('TestClassOf_ClassMethod',
  15982. LinesToStr([ // statements
  15983. 'rtl.createClass(this, "TObject", null, function () {',
  15984. ' this.$init = function () {',
  15985. ' };',
  15986. ' this.$final = function () {',
  15987. ' };',
  15988. ' this.DoIt = function (i) {',
  15989. ' var Result = 0;',
  15990. ' return Result;',
  15991. ' };',
  15992. '});',
  15993. 'this.i = 0;',
  15994. 'this.C = null;'
  15995. ]),
  15996. LinesToStr([ // $mod.$main
  15997. '$mod.C.DoIt(0);',
  15998. '$mod.C.DoIt(0);',
  15999. '$mod.i = $mod.C.DoIt(0);',
  16000. '$mod.i = $mod.C.DoIt(0);',
  16001. '']));
  16002. end;
  16003. procedure TTestModule.TestClassOf_ClassProperty;
  16004. begin
  16005. StartProgram(false);
  16006. Add([
  16007. 'type',
  16008. ' TObject = class',
  16009. ' class var FA: longint;',
  16010. ' class function GetA: longint;',
  16011. ' class procedure SetA(Value: longint);',
  16012. ' class property pA: longint read fa write fa;',
  16013. ' class property pB: longint read geta write seta;',
  16014. ' end;',
  16015. ' TObjectClass = class of tobject;',
  16016. 'class function tobject.geta: longint; begin end;',
  16017. 'class procedure tobject.seta(value: longint); begin end;',
  16018. 'var',
  16019. ' b: boolean;',
  16020. ' Obj: tobject;',
  16021. ' Cla: tobjectclass;',
  16022. 'begin',
  16023. ' obj.pa:=obj.pa;',
  16024. ' obj.pb:=obj.pb;',
  16025. ' b:=obj.pa=4;',
  16026. ' b:=obj.pb=obj.pb;',
  16027. ' b:=5=obj.pa;',
  16028. ' cla.pa:=6;',
  16029. ' cla.pa:=cla.pa;',
  16030. ' cla.pb:=cla.pb;',
  16031. ' b:=cla.pa=7;',
  16032. ' b:=cla.pb=cla.pb;',
  16033. ' b:=8=cla.pa;',
  16034. ' tobject.pa:=9;',
  16035. ' tobject.pb:=tobject.pb;',
  16036. ' b:=tobject.pa=10;',
  16037. ' b:=11=tobject.pa;',
  16038. '']);
  16039. ConvertProgram;
  16040. CheckSource('TestClassOf_ClassProperty',
  16041. LinesToStr([ // statements
  16042. 'rtl.createClass(this, "TObject", null, function () {',
  16043. ' this.FA = 0;',
  16044. ' this.$init = function () {',
  16045. ' };',
  16046. ' this.$final = function () {',
  16047. ' };',
  16048. ' this.GetA = function () {',
  16049. ' var Result = 0;',
  16050. ' return Result;',
  16051. ' };',
  16052. ' this.SetA = function (Value) {',
  16053. ' };',
  16054. '});',
  16055. 'this.b = false;',
  16056. 'this.Obj = null;',
  16057. 'this.Cla = null;'
  16058. ]),
  16059. LinesToStr([ // $mod.$main
  16060. '$mod.TObject.FA = $mod.Obj.FA;',
  16061. '$mod.Obj.$class.SetA($mod.Obj.$class.GetA());',
  16062. '$mod.b = $mod.Obj.FA === 4;',
  16063. '$mod.b = $mod.Obj.$class.GetA() === $mod.Obj.$class.GetA();',
  16064. '$mod.b = 5 === $mod.Obj.FA;',
  16065. '$mod.TObject.FA = 6;',
  16066. '$mod.TObject.FA = $mod.Cla.FA;',
  16067. '$mod.Cla.SetA($mod.Cla.GetA());',
  16068. '$mod.b = $mod.Cla.FA === 7;',
  16069. '$mod.b = $mod.Cla.GetA() === $mod.Cla.GetA();',
  16070. '$mod.b = 8 === $mod.Cla.FA;',
  16071. '$mod.TObject.FA = 9;',
  16072. '$mod.TObject.SetA($mod.TObject.GetA());',
  16073. '$mod.b = $mod.TObject.FA === 10;',
  16074. '$mod.b = 11 === $mod.TObject.FA;',
  16075. '']));
  16076. end;
  16077. procedure TTestModule.TestClassOf_ClassMethodSelf;
  16078. begin
  16079. StartProgram(false);
  16080. Add('type');
  16081. Add(' TObject = class');
  16082. Add(' class var GlobalId: longint;');
  16083. Add(' class procedure ProcA;');
  16084. Add(' end;');
  16085. Add('class procedure tobject.proca;');
  16086. Add('var b: boolean;');
  16087. Add('begin');
  16088. Add(' b:=self=nil;');
  16089. Add(' b:=self.globalid=3;');
  16090. Add(' b:=4=self.globalid;');
  16091. Add(' self.globalid:=5;');
  16092. Add(' self.proca;');
  16093. Add('end;');
  16094. Add('begin');
  16095. ConvertProgram;
  16096. CheckSource('TestClassOf_ClassMethodSelf',
  16097. LinesToStr([ // statements
  16098. 'rtl.createClass(this, "TObject", null, function () {',
  16099. ' this.GlobalId = 0;',
  16100. ' this.$init = function () {',
  16101. ' };',
  16102. ' this.$final = function () {',
  16103. ' };',
  16104. ' this.ProcA = function () {',
  16105. ' var b = false;',
  16106. ' b = this === null;',
  16107. ' b = this.GlobalId === 3;',
  16108. ' b = 4 === this.GlobalId;',
  16109. ' $mod.TObject.GlobalId = 5;',
  16110. ' this.ProcA();',
  16111. ' };',
  16112. '});'
  16113. ]),
  16114. LinesToStr([ // $mod.$main
  16115. '']));
  16116. end;
  16117. procedure TTestModule.TestClassOf_TypeCast;
  16118. begin
  16119. StartProgram(false);
  16120. Add('type');
  16121. Add(' TObject = class');
  16122. Add(' class procedure {#TObject_DoIt}DoIt;');
  16123. Add(' end;');
  16124. Add(' TClass = class of TObject;');
  16125. Add(' TMobile = class');
  16126. Add(' class procedure {#TMobile_DoIt}DoIt;');
  16127. Add(' end;');
  16128. Add(' TMobileClass = class of TMobile;');
  16129. Add(' TCar = class(TMobile)');
  16130. Add(' class procedure {#TCar_DoIt}DoIt;');
  16131. Add(' end;');
  16132. Add(' TCarClass = class of TCar;');
  16133. Add('class procedure TObject.DoIt;');
  16134. Add('begin');
  16135. Add(' TClass(Self).{@TObject_DoIt}DoIt;');
  16136. Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
  16137. Add('end;');
  16138. Add('class procedure TMobile.DoIt;');
  16139. Add('begin');
  16140. Add(' TClass(Self).{@TObject_DoIt}DoIt;');
  16141. Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
  16142. Add(' TCarClass(Self).{@TCar_DoIt}DoIt;');
  16143. Add('end;');
  16144. Add('class procedure TCar.DoIt; begin end;');
  16145. Add('var');
  16146. Add(' ObjC: TClass;');
  16147. Add(' MobileC: TMobileClass;');
  16148. Add(' CarC: TCarClass;');
  16149. Add('begin');
  16150. Add(' ObjC.{@TObject_DoIt}DoIt;');
  16151. Add(' MobileC.{@TMobile_DoIt}DoIt;');
  16152. Add(' CarC.{@TCar_DoIt}DoIt;');
  16153. Add(' TClass(ObjC).{@TObject_DoIt}DoIt;');
  16154. Add(' TMobileClass(ObjC).{@TMobile_DoIt}DoIt;');
  16155. Add(' TCarClass(ObjC).{@TCar_DoIt}DoIt;');
  16156. Add(' TClass(MobileC).{@TObject_DoIt}DoIt;');
  16157. Add(' TMobileClass(MobileC).{@TMobile_DoIt}DoIt;');
  16158. Add(' TCarClass(MobileC).{@TCar_DoIt}DoIt;');
  16159. Add(' TClass(CarC).{@TObject_DoIt}DoIt;');
  16160. Add(' TMobileClass(CarC).{@TMobile_DoIt}DoIt;');
  16161. Add(' TCarClass(CarC).{@TCar_DoIt}DoIt;');
  16162. ConvertProgram;
  16163. CheckSource('TestClassOf_TypeCast',
  16164. LinesToStr([ // statements
  16165. 'rtl.createClass(this, "TObject", null, function () {',
  16166. ' this.$init = function () {',
  16167. ' };',
  16168. ' this.$final = function () {',
  16169. ' };',
  16170. ' this.DoIt = function () {',
  16171. ' this.DoIt();',
  16172. ' this.DoIt$1();',
  16173. ' };',
  16174. '});',
  16175. 'rtl.createClass(this, "TMobile", this.TObject, function () {',
  16176. ' this.DoIt$1 = function () {',
  16177. ' this.DoIt();',
  16178. ' this.DoIt$1();',
  16179. ' this.DoIt$2();',
  16180. ' };',
  16181. '});',
  16182. 'rtl.createClass(this, "TCar", this.TMobile, function () {',
  16183. ' this.DoIt$2 = function () {',
  16184. ' };',
  16185. '});',
  16186. 'this.ObjC = null;',
  16187. 'this.MobileC = null;',
  16188. 'this.CarC = null;',
  16189. '']),
  16190. LinesToStr([ // $mod.$main
  16191. '$mod.ObjC.DoIt();',
  16192. '$mod.MobileC.DoIt$1();',
  16193. '$mod.CarC.DoIt$2();',
  16194. '$mod.ObjC.DoIt();',
  16195. '$mod.ObjC.DoIt$1();',
  16196. '$mod.ObjC.DoIt$2();',
  16197. '$mod.MobileC.DoIt();',
  16198. '$mod.MobileC.DoIt$1();',
  16199. '$mod.MobileC.DoIt$2();',
  16200. '$mod.CarC.DoIt();',
  16201. '$mod.CarC.DoIt$1();',
  16202. '$mod.CarC.DoIt$2();',
  16203. '']));
  16204. end;
  16205. procedure TTestModule.TestClassOf_ImplicitFunctionCall;
  16206. begin
  16207. StartProgram(false);
  16208. Add('type');
  16209. Add(' TObject = class');
  16210. Add(' function CurNow: longint; ');
  16211. Add(' class function Now: longint; ');
  16212. Add(' end;');
  16213. Add('function TObject.CurNow: longint; begin end;');
  16214. Add('class function TObject.Now: longint; begin end;');
  16215. Add('var');
  16216. Add(' Obj: tobject;');
  16217. Add(' vI: longint;');
  16218. Add('begin');
  16219. Add(' obj.curnow;');
  16220. Add(' vi:=obj.curnow;');
  16221. Add(' tobject.now;');
  16222. Add(' vi:=tobject.now;');
  16223. ConvertProgram;
  16224. CheckSource('TestClassOf_ImplicitFunctionCall',
  16225. LinesToStr([ // statements
  16226. 'rtl.createClass(this, "TObject", null, function () {',
  16227. ' this.$init = function () {',
  16228. ' };',
  16229. ' this.$final = function () {',
  16230. ' };',
  16231. ' this.CurNow = function () {',
  16232. ' var Result = 0;',
  16233. ' return Result;',
  16234. ' };',
  16235. ' this.Now = function () {',
  16236. ' var Result = 0;',
  16237. ' return Result;',
  16238. ' };',
  16239. '});',
  16240. 'this.Obj = null;',
  16241. 'this.vI = 0;',
  16242. '']),
  16243. LinesToStr([ // $mod.$main
  16244. '$mod.Obj.CurNow();',
  16245. '$mod.vI = $mod.Obj.CurNow();',
  16246. '$mod.TObject.Now();',
  16247. '$mod.vI = $mod.TObject.Now();',
  16248. '']));
  16249. end;
  16250. procedure TTestModule.TestClassOf_Const;
  16251. begin
  16252. StartProgram(false);
  16253. Add([
  16254. 'type',
  16255. ' TObject = class',
  16256. ' end;',
  16257. ' TBird = TObject;',
  16258. ' TBirds = class of TBird;',
  16259. ' TEagles = TBirds;',
  16260. ' THawk = class(TBird);',
  16261. 'const',
  16262. ' Hawk: TEagles = THawk;',
  16263. ' DefaultBirdClasses : Array [1..2] of TEagles = (',
  16264. ' TBird,',
  16265. ' THawk',
  16266. ' );',
  16267. 'begin']);
  16268. ConvertProgram;
  16269. CheckSource('TestClassOf_Const',
  16270. LinesToStr([ // statements
  16271. 'rtl.createClass(this, "TObject", null, function () {',
  16272. ' this.$init = function () {',
  16273. ' };',
  16274. ' this.$final = function () {',
  16275. ' };',
  16276. '});',
  16277. 'rtl.createClass(this, "THawk", this.TObject, function () {',
  16278. '});',
  16279. 'this.Hawk = this.THawk;',
  16280. 'this.DefaultBirdClasses = [this.TObject, this.THawk];',
  16281. '']),
  16282. LinesToStr([ // $mod.$main
  16283. '']));
  16284. end;
  16285. procedure TTestModule.TestNestedClass_Alias;
  16286. begin
  16287. WithTypeInfo:=true;
  16288. StartProgram(false);
  16289. Add([
  16290. 'type',
  16291. ' TObject = class',
  16292. ' type TNested = type longint;',
  16293. ' end;',
  16294. 'type TAlias = type tobject.tnested;',
  16295. 'var i: tobject.tnested = 3;',
  16296. 'var j: TAlias = 4;',
  16297. 'begin',
  16298. ' if typeinfo(TAlias)=nil then ;',
  16299. ' if typeinfo(tobject.tnested)=nil then ;',
  16300. '']);
  16301. ConvertProgram;
  16302. CheckSource('TestNestedClass_Alias',
  16303. LinesToStr([ // statements
  16304. 'rtl.createClass(this, "TObject", null, function () {',
  16305. ' $mod.$rtti.$inherited("TObject.TNested", rtl.longint, {});',
  16306. ' this.$init = function () {',
  16307. ' };',
  16308. ' this.$final = function () {',
  16309. ' };',
  16310. '});',
  16311. 'this.$rtti.$inherited("TAlias", this.$rtti["TObject.TNested"], {});',
  16312. 'this.i = 3;',
  16313. 'this.j = 4;',
  16314. '']),
  16315. LinesToStr([ // $mod.$main
  16316. 'if ($mod.$rtti["TAlias"] === null) ;',
  16317. 'if ($mod.$rtti["TObject.TNested"] === null) ;',
  16318. '']));
  16319. end;
  16320. procedure TTestModule.TestNestedClass_Record;
  16321. begin
  16322. WithTypeInfo:=true;
  16323. StartProgram(false);
  16324. Add([
  16325. 'type',
  16326. ' TObject = class',
  16327. ' type TPoint = record',
  16328. ' x,y: byte;',
  16329. ' end;',
  16330. ' procedure DoIt(t: TPoint);',
  16331. ' end;',
  16332. 'procedure tobject.DoIt(t: TPoint);',
  16333. 'var p: TPoint;',
  16334. 'begin',
  16335. ' t.x:=t.y;',
  16336. ' p:=t;',
  16337. 'end;',
  16338. 'var',
  16339. ' p: tobject.tpoint = (x:2; y:4);',
  16340. ' o: TObject;',
  16341. 'begin',
  16342. ' p:=p;',
  16343. ' o.doit(p);',
  16344. '']);
  16345. ConvertProgram;
  16346. CheckSource('TestNestedClass_Record',
  16347. LinesToStr([ // statements
  16348. 'rtl.createClass(this, "TObject", null, function () {',
  16349. ' rtl.recNewT(this, "TPoint", function () {',
  16350. ' this.x = 0;',
  16351. ' this.y = 0;',
  16352. ' this.$eq = function (b) {',
  16353. ' return (this.x === b.x) && (this.y === b.y);',
  16354. ' };',
  16355. ' this.$assign = function (s) {',
  16356. ' this.x = s.x;',
  16357. ' this.y = s.y;',
  16358. ' return this;',
  16359. ' };',
  16360. ' var $r = $mod.$rtti.$Record("TObject.TPoint", {});',
  16361. ' $r.addField("x", rtl.byte);',
  16362. ' $r.addField("y", rtl.byte);',
  16363. ' });',
  16364. ' this.$init = function () {',
  16365. ' };',
  16366. ' this.$final = function () {',
  16367. ' };',
  16368. ' this.DoIt = function (t) {',
  16369. ' var p = this.TPoint.$new();',
  16370. ' t.x = t.y;',
  16371. ' p.$assign(t);',
  16372. ' };',
  16373. '});',
  16374. 'this.p = this.TObject.TPoint.$clone({',
  16375. ' x: 2,',
  16376. ' y: 4',
  16377. '});',
  16378. 'this.o = null;',
  16379. '']),
  16380. LinesToStr([ // $mod.$main
  16381. '$mod.p.$assign($mod.p);',
  16382. '$mod.o.DoIt($mod.TObject.TPoint.$clone($mod.p));',
  16383. '']));
  16384. end;
  16385. procedure TTestModule.TestNestedClass_Class;
  16386. begin
  16387. WithTypeInfo:=true;
  16388. StartProgram(false);
  16389. Add([
  16390. 'type',
  16391. ' TObject = class end;',
  16392. ' TBird = class',
  16393. ' type TLeg = class',
  16394. ' FId: longint;',
  16395. ' constructor Create;',
  16396. ' function Create(i: longint): TLeg;',
  16397. ' end;',
  16398. ' function DoIt(b: TBird): Tleg;',
  16399. ' end;',
  16400. 'constructor tbird.tleg.create;',
  16401. 'begin',
  16402. ' FId:=3;',
  16403. 'end;',
  16404. 'function tbird.tleg.Create(i: longint): TLeg;',
  16405. 'begin',
  16406. ' Create;',
  16407. ' Result:=TLeg.Create;',
  16408. ' Result:=TBird.TLeg.Create;',
  16409. ' Result:=Create(3);',
  16410. ' FId:=i;',
  16411. 'end;',
  16412. 'function tbird.DoIt(b: tbird): tleg;',
  16413. 'begin',
  16414. ' Result.Create;',
  16415. ' Result:=TLeg.Create;',
  16416. ' Result:=TBird.TLeg.Create;',
  16417. ' Result:=Result.Create(3);',
  16418. 'end;',
  16419. 'var',
  16420. ' b: Tbird.tleg;',
  16421. 'begin',
  16422. ' b.Create;',
  16423. ' b:=TBird.TLeg.Create;',
  16424. ' b:=b.Create(3);',
  16425. '']);
  16426. ConvertProgram;
  16427. CheckSource('TestNestedClass_Class',
  16428. LinesToStr([ // statements
  16429. 'rtl.createClass(this, "TObject", null, function () {',
  16430. ' this.$init = function () {',
  16431. ' };',
  16432. ' this.$final = function () {',
  16433. ' };',
  16434. '});',
  16435. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16436. ' rtl.createClass(this, "TLeg", $mod.TObject, function () {',
  16437. ' this.$init = function () {',
  16438. ' $mod.TObject.$init.call(this);',
  16439. ' this.FId = 0;',
  16440. ' };',
  16441. ' this.Create = function () {',
  16442. ' this.FId = 3;',
  16443. ' return this;',
  16444. ' };',
  16445. ' this.Create$1 = function (i) {',
  16446. ' var Result = null;',
  16447. ' this.Create();',
  16448. ' Result = $mod.TBird.TLeg.$create("Create");',
  16449. ' Result = $mod.TBird.TLeg.$create("Create");',
  16450. ' Result = this.Create$1(3);',
  16451. ' this.FId = i;',
  16452. ' return Result;',
  16453. ' };',
  16454. ' }, "TBird.TLeg");',
  16455. ' this.DoIt = function (b) {',
  16456. ' var Result = null;',
  16457. ' Result.Create();',
  16458. ' Result = this.TLeg.$create("Create");',
  16459. ' Result = $mod.TBird.TLeg.$create("Create");',
  16460. ' Result = Result.Create$1(3);',
  16461. ' return Result;',
  16462. ' };',
  16463. '});',
  16464. 'this.b = null;',
  16465. '']),
  16466. LinesToStr([ // $mod.$main
  16467. '$mod.b.Create();',
  16468. '$mod.b = $mod.TBird.TLeg.$create("Create");',
  16469. '$mod.b = $mod.b.Create$1(3);',
  16470. '']));
  16471. end;
  16472. procedure TTestModule.TestExternalClass_Var;
  16473. begin
  16474. StartProgram(false);
  16475. Add([
  16476. '{$modeswitch externalclass}',
  16477. 'type',
  16478. ' TExtA = class external name ''ExtObj''',
  16479. ' Id: longint external name ''$Id'';',
  16480. ' B: longint;',
  16481. ' end;',
  16482. 'var Obj: TExtA;',
  16483. 'begin',
  16484. ' obj.id:=obj.id+1;',
  16485. ' obj.B:=obj.B+1;']);
  16486. ConvertProgram;
  16487. CheckSource('TestExternalClass_Var',
  16488. LinesToStr([ // statements
  16489. 'this.Obj = null;',
  16490. '']),
  16491. LinesToStr([ // $mod.$main
  16492. '$mod.Obj.$Id = $mod.Obj.$Id + 1;',
  16493. '$mod.Obj.B = $mod.Obj.B + 1;',
  16494. '']));
  16495. end;
  16496. procedure TTestModule.TestExternalClass_Const;
  16497. begin
  16498. StartProgram(false);
  16499. Add([
  16500. '{$modeswitch externalclass}',
  16501. 'type',
  16502. ' TExtA = class external name ''ExtObj''',
  16503. ' const Two: longint = 2;',
  16504. ' const Three = 3;',
  16505. ' const Id: longint;',
  16506. ' end;',
  16507. ' TExtB = class external name ''ExtB''',
  16508. ' A: TExtA;',
  16509. ' end;',
  16510. 'var',
  16511. ' A: texta;',
  16512. ' B: textb;',
  16513. ' i: longint;',
  16514. 'begin',
  16515. ' i:=a.two;',
  16516. ' i:=texta.two;',
  16517. ' i:=a.three;',
  16518. ' i:=texta.three;',
  16519. ' i:=a.id;',
  16520. ' i:=texta.id;',
  16521. '']);
  16522. ConvertProgram;
  16523. CheckSource('TestExternalClass_Const',
  16524. LinesToStr([ // statements
  16525. 'this.A = null;',
  16526. 'this.B = null;',
  16527. 'this.i = 0;',
  16528. '']),
  16529. LinesToStr([ // $mod.$main
  16530. '$mod.i = 2;',
  16531. '$mod.i = 2;',
  16532. '$mod.i = 3;',
  16533. '$mod.i = 3;',
  16534. '$mod.i = $mod.A.Id;',
  16535. '$mod.i = ExtObj.Id;',
  16536. '']));
  16537. end;
  16538. procedure TTestModule.TestExternalClass_Dollar;
  16539. begin
  16540. StartProgram(false);
  16541. Add([
  16542. '{$modeswitch externalclass}',
  16543. 'type',
  16544. ' TExtA = class external name ''$''',
  16545. ' Id: longint external name ''$'';',
  16546. ' function Bla(i: longint): longint; external name ''$'';',
  16547. ' end;',
  16548. 'function dollar(k: longint): longint; external name ''$'';',
  16549. 'var Obj: TExtA;',
  16550. 'begin',
  16551. ' dollar(1);',
  16552. ' obj.id:=obj.id+2;',
  16553. ' obj.Bla(3);',
  16554. '']);
  16555. ConvertProgram;
  16556. CheckSource('TestExternalClass_Dollar',
  16557. LinesToStr([ // statements
  16558. 'this.Obj = null;',
  16559. '']),
  16560. LinesToStr([ // $mod.$main
  16561. '$(1);',
  16562. '$mod.Obj.$ = $mod.Obj.$ + 2;',
  16563. '$mod.Obj.$(3);',
  16564. '']));
  16565. end;
  16566. procedure TTestModule.TestExternalClass_DuplicateVarFail;
  16567. begin
  16568. StartProgram(false);
  16569. Add('{$modeswitch externalclass}');
  16570. Add('type');
  16571. Add(' TExtA = class external name ''ExtA''');
  16572. Add(' Id: longint external name ''$Id'';');
  16573. Add(' end;');
  16574. Add(' TExtB = class external ''lib'' name ''ExtB''(TExtA)');
  16575. Add(' Id: longint;');
  16576. Add(' end;');
  16577. Add('begin');
  16578. SetExpectedPasResolverError('Duplicate identifier "Id" at test1.pp(6,5)',nDuplicateIdentifier);
  16579. ConvertProgram;
  16580. end;
  16581. procedure TTestModule.TestExternalClass_Method;
  16582. begin
  16583. StartProgram(false);
  16584. Add(['{$modeswitch externalclass}',
  16585. 'type',
  16586. ' TExtA = class external name ''ExtObj''',
  16587. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  16588. ' procedure DoSome(Id: longint = 1);',
  16589. ' end;',
  16590. 'var Obj: texta;',
  16591. 'begin',
  16592. ' obj.doit;',
  16593. ' obj.doit();',
  16594. ' obj.doit(2);',
  16595. ' with obj do begin',
  16596. ' doit;',
  16597. ' doit();',
  16598. ' doit(3);',
  16599. ' end;']);
  16600. ConvertProgram;
  16601. CheckSource('TestExternalClass_Method',
  16602. LinesToStr([ // statements
  16603. 'this.Obj = null;',
  16604. '']),
  16605. LinesToStr([ // $mod.$main
  16606. '$mod.Obj.$Execute(1);',
  16607. '$mod.Obj.$Execute(1);',
  16608. '$mod.Obj.$Execute(2);',
  16609. 'var $with = $mod.Obj;',
  16610. '$with.$Execute(1);',
  16611. '$with.$Execute(1);',
  16612. '$with.$Execute(3);',
  16613. '']));
  16614. end;
  16615. procedure TTestModule.TestExternalClass_ClassMethod;
  16616. begin
  16617. StartProgram(false);
  16618. Add([
  16619. '{$modeswitch externalclass}',
  16620. 'type',
  16621. ' TExtA = class external name ''ExtObj''',
  16622. ' class procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  16623. ' end;',
  16624. ' TExtB = TExtA;',
  16625. 'var p: Pointer;',
  16626. 'begin',
  16627. ' texta.doit;',
  16628. ' texta.doit();',
  16629. ' texta.doit(2);',
  16630. ' p:[email protected];',
  16631. ' with texta do begin',
  16632. ' doit;',
  16633. ' doit();',
  16634. ' doit(3);',
  16635. ' p:=@DoIt;',
  16636. ' end;',
  16637. ' textb.doit;',
  16638. ' textb.doit();',
  16639. ' textb.doit(4);',
  16640. ' with textb do begin',
  16641. ' doit;',
  16642. ' doit();',
  16643. ' doit(5);',
  16644. ' end;',
  16645. '']);
  16646. ConvertProgram;
  16647. CheckSource('TestExternalClass_ClassMethod',
  16648. LinesToStr([ // statements
  16649. 'this.p = null;',
  16650. '']),
  16651. LinesToStr([ // $mod.$main
  16652. 'ExtObj.$Execute(1);',
  16653. 'ExtObj.$Execute(1);',
  16654. 'ExtObj.$Execute(2);',
  16655. '$mod.p = rtl.createCallback(ExtObj, "$Execute");',
  16656. 'ExtObj.$Execute(1);',
  16657. 'ExtObj.$Execute(1);',
  16658. 'ExtObj.$Execute(3);',
  16659. '$mod.p = rtl.createCallback(ExtObj, "$Execute");',
  16660. 'ExtObj.$Execute(1);',
  16661. 'ExtObj.$Execute(1);',
  16662. 'ExtObj.$Execute(4);',
  16663. 'ExtObj.$Execute(1);',
  16664. 'ExtObj.$Execute(1);',
  16665. 'ExtObj.$Execute(5);',
  16666. '']));
  16667. end;
  16668. procedure TTestModule.TestExternalClass_ClassMethodStatic;
  16669. begin
  16670. StartProgram(false);
  16671. Add([
  16672. '{$modeswitch externalclass}',
  16673. 'type',
  16674. ' TExtA = class external name ''ExtObj''',
  16675. ' class procedure DoIt(Id: longint = 1); static;',
  16676. ' end;',
  16677. 'var p: Pointer;',
  16678. 'begin',
  16679. ' texta.doit;',
  16680. ' texta.doit();',
  16681. ' texta.doit(2);',
  16682. ' p:[email protected];',
  16683. ' with texta do begin',
  16684. ' doit;',
  16685. ' doit();',
  16686. ' doit(3);',
  16687. ' p:=@DoIt;',
  16688. ' end;',
  16689. '']);
  16690. ConvertProgram;
  16691. CheckSource('TestExternalClass_ClassMethodStatic',
  16692. LinesToStr([ // statements
  16693. 'this.p = null;',
  16694. '']),
  16695. LinesToStr([ // $mod.$main
  16696. 'ExtObj.DoIt(1);',
  16697. 'ExtObj.DoIt(1);',
  16698. 'ExtObj.DoIt(2);',
  16699. '$mod.p = ExtObj.DoIt;',
  16700. 'ExtObj.DoIt(1);',
  16701. 'ExtObj.DoIt(1);',
  16702. 'ExtObj.DoIt(3);',
  16703. '$mod.p = ExtObj.DoIt;',
  16704. '']));
  16705. end;
  16706. procedure TTestModule.TestExternalClass_FunctionResultInTypeCast;
  16707. begin
  16708. StartProgram(false);
  16709. Add([
  16710. '{$modeswitch externalclass}',
  16711. 'type',
  16712. ' TBird = class external name ''Array''',
  16713. ' end;',
  16714. 'function GetPtr: Pointer;',
  16715. 'begin',
  16716. 'end;',
  16717. 'procedure Write(const p);',
  16718. 'begin',
  16719. 'end;',
  16720. 'procedure WriteLn; varargs;',
  16721. 'begin',
  16722. 'end;',
  16723. 'begin',
  16724. ' if TBird(GetPtr)=nil then ;',
  16725. ' Write(GetPtr);',
  16726. ' WriteLn(GetPtr);',
  16727. ' Write(TBird(GetPtr));',
  16728. ' WriteLn(TBird(GetPtr));',
  16729. '']);
  16730. ConvertProgram;
  16731. CheckSource('TestFunctionResultInTypeCast',
  16732. LinesToStr([ // statements
  16733. 'this.GetPtr = function () {',
  16734. ' var Result = null;',
  16735. ' return Result;',
  16736. '};',
  16737. 'this.Write = function (p) {',
  16738. '};',
  16739. 'this.WriteLn = function () {',
  16740. '};',
  16741. '']),
  16742. LinesToStr([
  16743. 'if ($mod.GetPtr() === null) ;',
  16744. '$mod.Write($mod.GetPtr());',
  16745. '$mod.WriteLn($mod.GetPtr());',
  16746. '$mod.Write($mod.GetPtr());',
  16747. '$mod.WriteLn($mod.GetPtr());',
  16748. '']));
  16749. end;
  16750. procedure TTestModule.TestExternalClass_NonExternalOverride;
  16751. begin
  16752. StartProgram(false);
  16753. Add([
  16754. '{$modeswitch externalclass}',
  16755. 'type',
  16756. ' TExtA = class external name ''ExtObjA''',
  16757. ' procedure ProcA; virtual;',
  16758. ' procedure ProcB; virtual;',
  16759. ' end;',
  16760. ' TExtB = class external name ''ExtObjB'' (TExtA)',
  16761. ' end;',
  16762. ' TExtC = class (TExtB)',
  16763. ' procedure ProcA; override;',
  16764. ' end;',
  16765. 'procedure TExtC.ProcA;',
  16766. 'begin',
  16767. ' ProcA;',
  16768. ' Self.ProcA;',
  16769. ' ProcB;',
  16770. ' Self.ProcB;',
  16771. 'end;',
  16772. 'var',
  16773. ' A: texta;',
  16774. ' B: textb;',
  16775. ' C: textc;',
  16776. 'begin',
  16777. ' a.proca;',
  16778. ' b.proca;',
  16779. ' c.proca;']);
  16780. ConvertProgram;
  16781. CheckSource('TestExternalClass_NonExternalOverride',
  16782. LinesToStr([ // statements
  16783. 'rtl.createClassExt(this, "TExtC", ExtObjB, "", function () {',
  16784. ' this.$init = function () {',
  16785. ' };',
  16786. ' this.$final = function () {',
  16787. ' };',
  16788. ' this.ProcA = function () {',
  16789. ' this.ProcA();',
  16790. ' this.ProcA();',
  16791. ' this.ProcB();',
  16792. ' this.ProcB();',
  16793. ' };',
  16794. '});',
  16795. 'this.A = null;',
  16796. 'this.B = null;',
  16797. 'this.C = null;',
  16798. '']),
  16799. LinesToStr([ // $mod.$main
  16800. '$mod.A.ProcA();',
  16801. '$mod.B.ProcA();',
  16802. '$mod.C.ProcA();',
  16803. '']));
  16804. end;
  16805. procedure TTestModule.TestExternalClass_OverloadHint;
  16806. begin
  16807. StartProgram(false);
  16808. Add([
  16809. '{$modeswitch externalclass}',
  16810. 'type',
  16811. ' TExtA = class external name ''ExtObjA''',
  16812. ' procedure DoIt;',
  16813. ' procedure DoIt(i: longint);',
  16814. ' end;',
  16815. 'begin',
  16816. '']);
  16817. ConvertProgram;
  16818. CheckResolverUnexpectedHints(true);
  16819. CheckSource('TestExternalClass_OverloadHint',
  16820. LinesToStr([ // statements
  16821. '']),
  16822. LinesToStr([ // $mod.$main
  16823. '']));
  16824. end;
  16825. procedure TTestModule.TestExternalClass_SameNamePublishedProperty;
  16826. begin
  16827. WithTypeInfo:=true;
  16828. StartProgram(false);
  16829. Add([
  16830. '{$modeswitch externalclass}',
  16831. 'type',
  16832. ' JSwiper = class external name ''Swiper''',
  16833. ' constructor New;',
  16834. ' end;',
  16835. ' TObject = class',
  16836. ' private',
  16837. ' FSwiper: JSwiper;',
  16838. ' published',
  16839. ' property Swiper: JSwiper read FSwiper write FSwiper;',
  16840. ' end;',
  16841. 'begin',
  16842. ' JSwiper.new;',
  16843. '']);
  16844. ConvertProgram;
  16845. CheckSource('TestExternalClass_SameNamePublishedProperty',
  16846. LinesToStr([ // statements
  16847. 'this.$rtti.$ExtClass("JSwiper", {',
  16848. ' jsclass: "Swiper"',
  16849. '});',
  16850. 'rtl.createClass(this, "TObject", null, function () {',
  16851. ' this.$init = function () {',
  16852. ' this.FSwiper = null;',
  16853. ' };',
  16854. ' this.$final = function () {',
  16855. ' this.FSwiper = undefined;',
  16856. ' };',
  16857. ' var $r = this.$rtti;',
  16858. ' $r.addProperty("Swiper", 0, $mod.$rtti["JSwiper"], "FSwiper", "FSwiper");',
  16859. '});',
  16860. '']),
  16861. LinesToStr([ // $mod.$main
  16862. 'new Swiper();',
  16863. '']));
  16864. end;
  16865. procedure TTestModule.TestExternalClass_Property;
  16866. begin
  16867. StartProgram(false);
  16868. Add([
  16869. '{$modeswitch externalclass}',
  16870. 'type',
  16871. ' TExtA = class external name ''ExtA''',
  16872. ' function getYear: longint;',
  16873. ' procedure setYear(Value: longint);',
  16874. ' property Year: longint read getyear write setyear;',
  16875. ' end;',
  16876. ' TExtB = class (TExtA)',
  16877. ' procedure OtherSetYear(Value: longint);',
  16878. ' property year write othersetyear;',
  16879. ' end;',
  16880. 'procedure textb.othersetyear(value: longint);',
  16881. 'begin',
  16882. ' setYear(Value+4);',
  16883. 'end;',
  16884. 'var',
  16885. ' A: texta;',
  16886. ' B: textb;',
  16887. 'begin',
  16888. ' a.year:=a.year+1;',
  16889. ' b.year:=b.year+2;']);
  16890. ConvertProgram;
  16891. CheckSource('TestExternalClass_NonExternalOverride',
  16892. LinesToStr([ // statements
  16893. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  16894. ' this.$init = function () {',
  16895. ' };',
  16896. ' this.$final = function () {',
  16897. ' };',
  16898. ' this.OtherSetYear = function (Value) {',
  16899. ' this.setYear(Value+4);',
  16900. ' };',
  16901. '});',
  16902. 'this.A = null;',
  16903. 'this.B = null;',
  16904. '']),
  16905. LinesToStr([ // $mod.$main
  16906. '$mod.A.setYear($mod.A.getYear()+1);',
  16907. '$mod.B.OtherSetYear($mod.B.getYear()+2);',
  16908. '']));
  16909. end;
  16910. procedure TTestModule.TestExternalClass_PropertyDate;
  16911. begin
  16912. StartProgram(false);
  16913. Add([
  16914. '{$modeswitch externalclass}',
  16915. 'type',
  16916. ' TExtA = class external name ''ExtA''',
  16917. ' end;',
  16918. ' TExtB = class (TExtA)',
  16919. ' FDate: string;',
  16920. ' property Date: string read FDate write FDate;',
  16921. ' property ExtA: string read FDate write FDate;',
  16922. ' end;',
  16923. ' {$M+}',
  16924. ' TObject = class',
  16925. ' FDate: string;',
  16926. ' published',
  16927. ' property Date: string read FDate write FDate;',
  16928. ' property ExtA: string read FDate write FDate;',
  16929. ' end;',
  16930. 'var',
  16931. ' B: textb;',
  16932. ' o: TObject;',
  16933. 'begin',
  16934. ' b.date:=b.exta;',
  16935. ' o.date:=o.exta;']);
  16936. ConvertProgram;
  16937. CheckSource('TestExternalClass_PropertyDate',
  16938. LinesToStr([ // statements
  16939. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  16940. ' this.$init = function () {',
  16941. ' this.FDate = "";',
  16942. ' };',
  16943. ' this.$final = function () {',
  16944. ' };',
  16945. '});',
  16946. 'rtl.createClass(this, "TObject", null, function () {',
  16947. ' this.$init = function () {',
  16948. ' this.FDate = "";',
  16949. ' };',
  16950. ' this.$final = function () {',
  16951. ' };',
  16952. ' var $r = this.$rtti;',
  16953. ' $r.addField("FDate", rtl.string);',
  16954. ' $r.addProperty("Date", 0, rtl.string, "FDate", "FDate");',
  16955. ' $r.addProperty("ExtA", 0, rtl.string, "FDate", "FDate");',
  16956. '});',
  16957. 'this.B = null;',
  16958. 'this.o = null;',
  16959. '']),
  16960. LinesToStr([ // $mod.$main
  16961. '$mod.B.FDate = $mod.B.FDate;',
  16962. '$mod.o.FDate = $mod.o.FDate;',
  16963. '']));
  16964. end;
  16965. procedure TTestModule.TestExternalClass_ClassProperty;
  16966. begin
  16967. StartProgram(false);
  16968. Add('{$modeswitch externalclass}');
  16969. Add('type');
  16970. Add(' TExtA = class external name ''ExtA''');
  16971. Add(' class function getYear: longint;');
  16972. Add(' class procedure setYear(Value: longint);');
  16973. Add(' class property Year: longint read getyear write setyear;');
  16974. Add(' end;');
  16975. Add(' TExtB = class (TExtA)');
  16976. Add(' class function GetCentury: longint;');
  16977. Add(' class procedure SetCentury(Value: longint);');
  16978. Add(' class property Century: longint read getcentury write setcentury;');
  16979. Add(' end;');
  16980. Add('class function textb.getcentury: longint;');
  16981. Add('begin');
  16982. Add('end;');
  16983. Add('class procedure textb.setcentury(value: longint);');
  16984. Add('begin');
  16985. Add(' setyear(value+11);');
  16986. Add(' texta.year:=texta.year+12;');
  16987. Add(' year:=year+13;');
  16988. Add(' textb.century:=textb.century+14;');
  16989. Add(' century:=century+15;');
  16990. Add('end;');
  16991. Add('var');
  16992. Add(' A: texta;');
  16993. Add(' B: textb;');
  16994. Add('begin');
  16995. Add(' texta.year:=texta.year+1;');
  16996. Add(' textb.year:=textb.year+2;');
  16997. Add(' TextA.year:=TextA.year+3;');
  16998. Add(' b.year:=b.year+4;');
  16999. Add(' textb.century:=textb.century+5;');
  17000. Add(' b.century:=b.century+6;');
  17001. ConvertProgram;
  17002. CheckSource('TestExternalClass_ClassProperty',
  17003. LinesToStr([ // statements
  17004. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  17005. ' this.$init = function () {',
  17006. ' };',
  17007. ' this.$final = function () {',
  17008. ' };',
  17009. ' this.GetCentury = function () {',
  17010. ' var Result = 0;',
  17011. ' return Result;',
  17012. ' };',
  17013. ' this.SetCentury = function (Value) {',
  17014. ' this.setYear(Value + 11);',
  17015. ' ExtA.setYear(ExtA.getYear() + 12);',
  17016. ' this.setYear(this.getYear() + 13);',
  17017. ' $mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 14);',
  17018. ' this.SetCentury(this.GetCentury() + 15);',
  17019. ' };',
  17020. '});',
  17021. 'this.A = null;',
  17022. 'this.B = null;',
  17023. '']),
  17024. LinesToStr([ // $mod.$main
  17025. 'ExtA.setYear(ExtA.getYear() + 1);',
  17026. '$mod.TExtB.setYear($mod.TExtB.getYear() + 2);',
  17027. 'ExtA.setYear(ExtA.getYear() + 3);',
  17028. '$mod.B.setYear($mod.B.getYear() + 4);',
  17029. '$mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 5);',
  17030. '$mod.B.$class.SetCentury($mod.B.$class.GetCentury() + 6);',
  17031. '']));
  17032. end;
  17033. procedure TTestModule.TestExternalClass_ClassOf;
  17034. begin
  17035. StartProgram(false);
  17036. Add('{$modeswitch externalclass}');
  17037. Add('type');
  17038. Add(' TExtA = class external name ''ExtA''');
  17039. Add(' procedure ProcA; virtual;');
  17040. Add(' procedure ProcB; virtual;');
  17041. Add(' end;');
  17042. Add(' TExtAClass = class of TExtA;');
  17043. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  17044. Add(' end;');
  17045. Add(' TExtBClass = class of TExtB;');
  17046. Add(' TExtC = class (TExtB)');
  17047. Add(' procedure ProcA; override;');
  17048. Add(' end;');
  17049. Add(' TExtCClass = class of TExtC;');
  17050. Add('procedure TExtC.ProcA; begin end;');
  17051. Add('var');
  17052. Add(' A: texta; ClA: TExtAClass;');
  17053. Add(' B: textb; ClB: TExtBClass;');
  17054. Add(' C: textc; ClC: TExtCClass;');
  17055. Add('begin');
  17056. Add(' ClA:=texta;');
  17057. Add(' ClA:=textb;');
  17058. Add(' ClA:=textc;');
  17059. Add(' ClB:=textb;');
  17060. Add(' ClB:=textc;');
  17061. Add(' ClC:=textc;');
  17062. ConvertProgram;
  17063. CheckSource('TestExternalClass_ClassOf',
  17064. LinesToStr([ // statements
  17065. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  17066. ' this.$init = function () {',
  17067. ' };',
  17068. ' this.$final = function () {',
  17069. ' };',
  17070. ' this.ProcA = function () {',
  17071. ' };',
  17072. '});',
  17073. 'this.A = null;',
  17074. 'this.ClA = null;',
  17075. 'this.B = null;',
  17076. 'this.ClB = null;',
  17077. 'this.C = null;',
  17078. 'this.ClC = null;',
  17079. '']),
  17080. LinesToStr([ // $mod.$main
  17081. '$mod.ClA = ExtA;',
  17082. '$mod.ClA = ExtB;',
  17083. '$mod.ClA = $mod.TExtC;',
  17084. '$mod.ClB = ExtB;',
  17085. '$mod.ClB = $mod.TExtC;',
  17086. '$mod.ClC = $mod.TExtC;',
  17087. '']));
  17088. end;
  17089. procedure TTestModule.TestExternalClass_ClassOtherUnit;
  17090. begin
  17091. AddModuleWithIntfImplSrc('unit2.pas',
  17092. LinesToStr([
  17093. '{$modeswitch externalclass}',
  17094. 'type',
  17095. ' TExtA = class external name ''ExtA''',
  17096. ' class var Id: longint;',
  17097. ' end;',
  17098. '']),
  17099. '');
  17100. StartUnit(true);
  17101. Add('interface');
  17102. Add('uses unit2;');
  17103. Add('implementation');
  17104. Add('begin');
  17105. Add(' unit2.texta.id:=unit2.texta.id+1;');
  17106. ConvertUnit;
  17107. CheckSource('TestExternalClass_ClassOtherUnit',
  17108. LinesToStr([
  17109. '']),
  17110. LinesToStr([
  17111. 'ExtA.Id = ExtA.Id + 1;',
  17112. '']));
  17113. end;
  17114. procedure TTestModule.TestExternalClass_Is;
  17115. begin
  17116. StartProgram(false);
  17117. Add([
  17118. '{$modeswitch externalclass}',
  17119. 'type',
  17120. ' TExtA = class external name ''ExtA''',
  17121. ' end;',
  17122. ' TExtAClass = class of TExtA;',
  17123. ' TExtB = class external name ''ExtB'' (TExtA)',
  17124. ' end;',
  17125. ' TExtBClass = class of TExtB;',
  17126. ' TExtC = class (TExtB)',
  17127. ' end;',
  17128. ' TExtCClass = class of TExtC;',
  17129. 'var',
  17130. ' A: texta; ClA: TExtAClass;',
  17131. ' B: textb; ClB: TExtBClass;',
  17132. ' C: textc; ClC: TExtCClass;',
  17133. 'begin',
  17134. ' if a is textb then ;',
  17135. ' if a is textc then ;',
  17136. ' if b is textc then ;',
  17137. ' if cla is textb then ;',
  17138. ' if cla is textc then ;',
  17139. ' if clb is textc then ;',
  17140. ' try',
  17141. ' except',
  17142. ' on TExtA do ;',
  17143. ' on e: TExtB do ;',
  17144. ' end;',
  17145. '']);
  17146. ConvertProgram;
  17147. CheckSource('TestExternalClass_Is',
  17148. LinesToStr([ // statements
  17149. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  17150. ' this.$init = function () {',
  17151. ' };',
  17152. ' this.$final = function () {',
  17153. ' };',
  17154. '});',
  17155. 'this.A = null;',
  17156. 'this.ClA = null;',
  17157. 'this.B = null;',
  17158. 'this.ClB = null;',
  17159. 'this.C = null;',
  17160. 'this.ClC = null;',
  17161. '']),
  17162. LinesToStr([ // $mod.$main
  17163. 'if (rtl.isExt($mod.A, ExtB)) ;',
  17164. 'if ($mod.TExtC.isPrototypeOf($mod.A)) ;',
  17165. 'if ($mod.TExtC.isPrototypeOf($mod.B)) ;',
  17166. 'if (rtl.isExt($mod.ClA, ExtB)) ;',
  17167. 'if (rtl.is($mod.ClA, $mod.TExtC)) ;',
  17168. 'if (rtl.is($mod.ClB, $mod.TExtC)) ;',
  17169. 'try {} catch ($e) {',
  17170. ' if (rtl.isExt($e,ExtA)) {}',
  17171. ' else if (rtl.isExt($e,ExtB)) {',
  17172. ' var e = $e;',
  17173. ' } else throw $e',
  17174. '};',
  17175. '']));
  17176. end;
  17177. procedure TTestModule.TestExternalClass_As;
  17178. begin
  17179. StartProgram(false);
  17180. Add('{$modeswitch externalclass}');
  17181. Add('type');
  17182. Add(' TExtA = class external name ''ExtA''');
  17183. Add(' end;');
  17184. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  17185. Add(' end;');
  17186. Add(' TExtC = class (TExtB)');
  17187. Add(' end;');
  17188. Add('var');
  17189. Add(' A: texta;');
  17190. Add(' B: textb;');
  17191. Add(' C: textc;');
  17192. Add('begin');
  17193. Add(' b:=a as textb;');
  17194. Add(' c:=a as textc;');
  17195. Add(' c:=b as textc;');
  17196. ConvertProgram;
  17197. CheckSource('TestExternalClass_Is',
  17198. LinesToStr([ // statements
  17199. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  17200. ' this.$init = function () {',
  17201. ' };',
  17202. ' this.$final = function () {',
  17203. ' };',
  17204. '});',
  17205. 'this.A = null;',
  17206. 'this.B = null;',
  17207. 'this.C = null;',
  17208. '']),
  17209. LinesToStr([ // $mod.$main
  17210. '$mod.B = rtl.asExt($mod.A, ExtB);',
  17211. '$mod.C = rtl.as($mod.A, $mod.TExtC);',
  17212. '$mod.C = rtl.as($mod.B, $mod.TExtC);',
  17213. '']));
  17214. end;
  17215. procedure TTestModule.TestExternalClass_DestructorFail;
  17216. begin
  17217. StartProgram(false);
  17218. Add('{$modeswitch externalclass}');
  17219. Add('type');
  17220. Add(' TExtA = class external name ''ExtA''');
  17221. Add(' destructor Free;');
  17222. Add(' end;');
  17223. SetExpectedPasResolverError('Pascal element not supported: destructor',
  17224. nPasElementNotSupported);
  17225. ConvertProgram;
  17226. end;
  17227. procedure TTestModule.TestExternalClass_New;
  17228. begin
  17229. StartProgram(false);
  17230. Add([
  17231. '{$modeswitch externalclass}',
  17232. 'type',
  17233. ' TExtA = class external name ''ExtA''',
  17234. ' constructor New;',
  17235. ' constructor New(i: longint; j: longint = 2);',
  17236. ' end;',
  17237. 'var',
  17238. ' A: texta;',
  17239. 'begin',
  17240. ' a:=texta.new;',
  17241. ' a:=texta(texta.new);',
  17242. ' a:=texta.new();',
  17243. ' a:=texta.new(1);',
  17244. ' with texta do begin',
  17245. ' a:=new;',
  17246. ' a:=new();',
  17247. ' a:=new(2);',
  17248. ' end;',
  17249. ' a:=test1.texta.new;',
  17250. ' a:=test1.texta.new();',
  17251. ' a:=test1.texta.new(3);',
  17252. '']);
  17253. ConvertProgram;
  17254. CheckSource('TestExternalClass_New',
  17255. LinesToStr([ // statements
  17256. 'this.A = null;',
  17257. '']),
  17258. LinesToStr([ // $mod.$main
  17259. '$mod.A = new ExtA();',
  17260. '$mod.A = new ExtA();',
  17261. '$mod.A = new ExtA();',
  17262. '$mod.A = new ExtA(1,2);',
  17263. '$mod.A = new ExtA();',
  17264. '$mod.A = new ExtA();',
  17265. '$mod.A = new ExtA(2,2);',
  17266. '$mod.A = new ExtA();',
  17267. '$mod.A = new ExtA();',
  17268. '$mod.A = new ExtA(3,2);',
  17269. '']));
  17270. end;
  17271. procedure TTestModule.TestExternalClass_ClassOf_New;
  17272. begin
  17273. StartProgram(false);
  17274. Add('{$modeswitch externalclass}');
  17275. Add('type');
  17276. Add(' TExtAClass = class of TExtA;');
  17277. Add(' TExtA = class external name ''ExtA''');
  17278. Add(' C: TExtAClass;');
  17279. Add(' constructor New;');
  17280. Add(' end;');
  17281. Add('var');
  17282. Add(' A: texta;');
  17283. Add(' C: textaclass;');
  17284. Add('begin');
  17285. Add(' a:=c.new;');
  17286. Add(' a:=c.new();');
  17287. Add(' with C do begin');
  17288. Add(' a:=new;');
  17289. Add(' a:=new();');
  17290. Add(' end;');
  17291. Add(' a:=test1.c.new;');
  17292. Add(' a:=test1.c.new();');
  17293. Add(' a:=A.c.new();');
  17294. ConvertProgram;
  17295. CheckSource('TestExternalClass_ClassOf_New',
  17296. LinesToStr([ // statements
  17297. 'this.A = null;',
  17298. 'this.C = null;',
  17299. '']),
  17300. LinesToStr([ // $mod.$main
  17301. '$mod.A = new $mod.C();',
  17302. '$mod.A = new $mod.C();',
  17303. 'var $with = $mod.C;',
  17304. '$mod.A = new $with();',
  17305. '$mod.A = new $with();',
  17306. '$mod.A = new $mod.C();',
  17307. '$mod.A = new $mod.C();',
  17308. '$mod.A = new $mod.A.C();',
  17309. '']));
  17310. end;
  17311. procedure TTestModule.TestExternalClass_FuncClassOf_New;
  17312. begin
  17313. StartProgram(false);
  17314. Add([
  17315. '{$modeswitch externalclass}',
  17316. 'type',
  17317. ' TExtAClass = class of TExtA;',
  17318. ' TExtA = class external name ''ExtA''',
  17319. ' constructor New;',
  17320. ' end;',
  17321. 'function GetCreator: TExtAClass;',
  17322. 'begin',
  17323. ' Result:=TExtA;',
  17324. 'end;',
  17325. 'var',
  17326. ' A: texta;',
  17327. 'begin',
  17328. ' a:=getcreator.new;',
  17329. ' a:=getcreator().new;',
  17330. ' a:=getcreator().new();',
  17331. ' a:=getcreator.new();',
  17332. ' with getcreator do begin',
  17333. ' a:=new;',
  17334. ' a:=new();',
  17335. ' end;']);
  17336. ConvertProgram;
  17337. CheckSource('TestExternalClass_FuncClassOf_New',
  17338. LinesToStr([ // statements
  17339. 'this.GetCreator = function () {',
  17340. ' var Result = null;',
  17341. ' Result = ExtA;',
  17342. ' return Result;',
  17343. '};',
  17344. 'this.A = null;',
  17345. '']),
  17346. LinesToStr([ // $mod.$main
  17347. '$mod.A = new ($mod.GetCreator())();',
  17348. '$mod.A = new ($mod.GetCreator())();',
  17349. '$mod.A = new ($mod.GetCreator())();',
  17350. '$mod.A = new ($mod.GetCreator())();',
  17351. 'var $with = $mod.GetCreator();',
  17352. '$mod.A = new $with();',
  17353. '$mod.A = new $with();',
  17354. '']));
  17355. end;
  17356. procedure TTestModule.TestExternalClass_New_PasClassFail;
  17357. begin
  17358. StartProgram(false);
  17359. Add([
  17360. '{$modeswitch externalclass}',
  17361. 'type',
  17362. ' TExtA = class external name ''ExtA''',
  17363. ' constructor New;',
  17364. ' end;',
  17365. ' TBird = class(TExtA)',
  17366. ' end;',
  17367. 'begin',
  17368. ' TBird.new;',
  17369. '']);
  17370. SetExpectedPasResolverError(sJSNewNotSupported,nJSNewNotSupported);
  17371. ConvertProgram;
  17372. end;
  17373. procedure TTestModule.TestExternalClass_New_PasClassBracketsFail;
  17374. begin
  17375. StartProgram(false);
  17376. Add([
  17377. '{$modeswitch externalclass}',
  17378. 'type',
  17379. ' TExtA = class external name ''ExtA''',
  17380. ' constructor New;',
  17381. ' end;',
  17382. ' TBird = class(TExtA)',
  17383. ' end;',
  17384. 'begin',
  17385. ' TBird.new();',
  17386. '']);
  17387. SetExpectedPasResolverError(sJSNewNotSupported,nJSNewNotSupported);
  17388. ConvertProgram;
  17389. end;
  17390. procedure TTestModule.TestExternalClass_NewExtName;
  17391. begin
  17392. StartProgram(false);
  17393. Add([
  17394. '{$modeswitch externalclass}',
  17395. 'type',
  17396. ' TExtA = class external name ''ExtA''',
  17397. ' constructor New; external name ''Other'';',
  17398. ' constructor New(i: longint; j: longint = 2); external name ''A.B'';',
  17399. ' end;',
  17400. 'var',
  17401. ' A: texta;',
  17402. 'begin',
  17403. ' a:=texta.new;',
  17404. ' a:=texta(texta.new);',
  17405. ' a:=texta.new();',
  17406. ' a:=texta.new(1);',
  17407. ' with texta do begin',
  17408. ' a:=new;',
  17409. ' a:=new();',
  17410. ' a:=new(2);',
  17411. ' end;',
  17412. ' a:=test1.texta.new;',
  17413. ' a:=test1.texta.new();',
  17414. ' a:=test1.texta.new(3);',
  17415. '']);
  17416. ConvertProgram;
  17417. CheckSource('TestExternalClass_NewExtName',
  17418. LinesToStr([ // statements
  17419. 'this.A = null;',
  17420. '']),
  17421. LinesToStr([ // $mod.$main
  17422. '$mod.A = new Other();',
  17423. '$mod.A = new Other();',
  17424. '$mod.A = new Other();',
  17425. '$mod.A = new A.B(1,2);',
  17426. '$mod.A = new Other();',
  17427. '$mod.A = new Other();',
  17428. '$mod.A = new A.B(2,2);',
  17429. '$mod.A = new Other();',
  17430. '$mod.A = new Other();',
  17431. '$mod.A = new A.B(3,2);',
  17432. '']));
  17433. end;
  17434. procedure TTestModule.TestExternalClass_Constructor;
  17435. begin
  17436. StartProgram(false);
  17437. Add([
  17438. '{$modeswitch externalclass}',
  17439. 'type',
  17440. ' TExtA = class external name ''ExtA''',
  17441. ' public type',
  17442. ' TExtB = class external name ''ExtB''',
  17443. ' public type',
  17444. ' TExtC = class external name ''ExtC''',
  17445. ' constructor New;',
  17446. ' constructor New(i: word);',
  17447. ' end;',
  17448. ' end;',
  17449. ' constructor Create;',
  17450. ' constructor Create(i: longint; j: longint = 2);',
  17451. ' end;',
  17452. 'var',
  17453. ' A: texta;',
  17454. ' C: texta.textb.textc;',
  17455. 'begin',
  17456. ' a:=texta.create;',
  17457. ' a:=texta(texta.create);',
  17458. ' a:=texta.create();',
  17459. ' a:=texta.create(1);',
  17460. ' with texta do begin',
  17461. ' a:=create;',
  17462. ' a:=create();',
  17463. ' a:=create(2);',
  17464. ' end;',
  17465. ' a:=test1.texta.create;',
  17466. ' a:=test1.texta.create();',
  17467. ' a:=test1.texta.create(3);',
  17468. ' c:=texta.textb.textc.new;',
  17469. ' c:=texta.textb.textc.new();',
  17470. ' c:=texta.textb.textc.new(4);',
  17471. '']);
  17472. ConvertProgram;
  17473. CheckSource('TestExternalClass_Constructor',
  17474. LinesToStr([ // statements
  17475. 'this.A = null;',
  17476. 'this.C = null;',
  17477. '']),
  17478. LinesToStr([ // $mod.$main
  17479. '$mod.A = new ExtA.Create();',
  17480. '$mod.A = new ExtA.Create();',
  17481. '$mod.A = new ExtA.Create();',
  17482. '$mod.A = new ExtA.Create(1,2);',
  17483. '$mod.A = new ExtA.Create();',
  17484. '$mod.A = new ExtA.Create();',
  17485. '$mod.A = new ExtA.Create(2,2);',
  17486. '$mod.A = new ExtA.Create();',
  17487. '$mod.A = new ExtA.Create();',
  17488. '$mod.A = new ExtA.Create(3,2);',
  17489. '$mod.C = new ExtA.ExtB.ExtC();',
  17490. '$mod.C = new ExtA.ExtB.ExtC();',
  17491. '$mod.C = new ExtA.ExtB.ExtC(4);',
  17492. '']));
  17493. end;
  17494. procedure TTestModule.TestExternalClass_ConstructorBrackets;
  17495. begin
  17496. StartProgram(false);
  17497. Add([
  17498. '{$modeswitch externalclass}',
  17499. 'type',
  17500. ' TExtA = class external name ''ExtA''',
  17501. ' constructor Create; external name ''{}'';',
  17502. ' end;',
  17503. 'var',
  17504. ' A: texta;',
  17505. 'begin',
  17506. ' a:=texta.create;',
  17507. ' a:=texta(texta.create);',
  17508. ' a:=texta.create();',
  17509. ' with texta do begin',
  17510. ' a:=create;',
  17511. ' a:=create();',
  17512. ' end;',
  17513. ' a:=test1.texta.create;',
  17514. ' a:=test1.texta.create();',
  17515. '']);
  17516. ConvertProgram;
  17517. CheckSource('TestExternalClass_ConstructorBrackets',
  17518. LinesToStr([ // statements
  17519. 'this.A = null;',
  17520. '']),
  17521. LinesToStr([ // $mod.$main
  17522. '$mod.A = {};',
  17523. '$mod.A = {};',
  17524. '$mod.A = {};',
  17525. '$mod.A = {};',
  17526. '$mod.A = {};',
  17527. '$mod.A = {};',
  17528. '$mod.A = {};',
  17529. '']));
  17530. end;
  17531. procedure TTestModule.TestExternalClass_LocalConstSameName;
  17532. begin
  17533. StartProgram(false);
  17534. Add('{$modeswitch externalclass}');
  17535. Add('type');
  17536. Add(' TExtA = class external name ''ExtA''');
  17537. Add(' constructor New;');
  17538. Add(' end;');
  17539. Add('function DoIt: longint;');
  17540. Add('const ExtA: longint = 3;');
  17541. Add('begin');
  17542. Add(' Result:=ExtA;');
  17543. Add('end;');
  17544. Add('var');
  17545. Add(' A: texta;');
  17546. Add('begin');
  17547. Add(' a:=texta.new;');
  17548. ConvertProgram;
  17549. CheckSource('TestExternalClass_LocalConstSameName',
  17550. LinesToStr([ // statements
  17551. 'var ExtA$1 = 3;',
  17552. 'this.DoIt = function () {',
  17553. ' var Result = 0;',
  17554. ' Result = ExtA$1;',
  17555. ' return Result;',
  17556. '};',
  17557. 'this.A = null;',
  17558. '']),
  17559. LinesToStr([ // $mod.$main
  17560. '$mod.A = new ExtA();',
  17561. '']));
  17562. end;
  17563. procedure TTestModule.TestExternalClass_ReintroduceOverload;
  17564. begin
  17565. StartProgram(false);
  17566. Add('{$modeswitch externalclass}');
  17567. Add('type');
  17568. Add(' TExtA = class external name ''ExtA''');
  17569. Add(' procedure DoIt;');
  17570. Add(' end;');
  17571. Add(' TMyA = class(TExtA)');
  17572. Add(' procedure DoIt;');
  17573. Add(' end;');
  17574. Add('procedure TMyA.DoIt; begin end;');
  17575. Add('begin');
  17576. ConvertProgram;
  17577. CheckSource('TestExternalClass_ReintroduceOverload',
  17578. LinesToStr([ // statements
  17579. 'rtl.createClassExt(this, "TMyA", ExtA, "", function () {',
  17580. ' this.$init = function () {',
  17581. ' };',
  17582. ' this.$final = function () {',
  17583. ' };',
  17584. ' this.DoIt$1 = function () {',
  17585. ' };',
  17586. '});',
  17587. '']),
  17588. LinesToStr([ // $mod.$main
  17589. '']));
  17590. end;
  17591. procedure TTestModule.TestExternalClass_Inherited;
  17592. begin
  17593. StartProgram(false);
  17594. Add('{$modeswitch externalclass}');
  17595. Add('type');
  17596. Add(' TExtA = class external name ''ExtA''');
  17597. Add(' procedure DoIt(i: longint = 1); virtual;');
  17598. Add(' procedure DoSome(j: longint = 2);');
  17599. Add(' end;');
  17600. Add(' TExtB = class external name ''ExtB''(TExtA)');
  17601. Add(' end;');
  17602. Add(' TMyC = class(TExtB)');
  17603. Add(' procedure DoIt(i: longint = 1); override;');
  17604. Add(' procedure DoSome(j: longint = 2); reintroduce;');
  17605. Add(' end;');
  17606. Add('procedure TMyC.DoIt(i: longint);');
  17607. Add('begin');
  17608. Add(' inherited;');
  17609. Add(' inherited DoIt;');
  17610. Add(' inherited DoIt();');
  17611. Add(' inherited DoIt(3);');
  17612. Add(' inherited DoSome;');
  17613. Add(' inherited DoSome();');
  17614. Add(' inherited DoSome(4);');
  17615. Add('end;');
  17616. Add('procedure TMyC.DoSome(j: longint);');
  17617. Add('begin');
  17618. Add(' inherited;');
  17619. Add('end;');
  17620. Add('begin');
  17621. ConvertProgram;
  17622. CheckSource('TestExternalClass_ReintroduceOverload',
  17623. LinesToStr([ // statements
  17624. 'rtl.createClassExt(this, "TMyC", ExtB, "", function () {',
  17625. ' this.$init = function () {',
  17626. ' };',
  17627. ' this.$final = function () {',
  17628. ' };',
  17629. ' this.DoIt = function (i) {',
  17630. ' ExtB.DoIt.apply(this, arguments);',
  17631. ' ExtB.DoIt.call(this, 1);',
  17632. ' ExtB.DoIt.call(this, 1);',
  17633. ' ExtB.DoIt.call(this, 3);',
  17634. ' ExtB.DoSome.call(this, 2);',
  17635. ' ExtB.DoSome.call(this, 2);',
  17636. ' ExtB.DoSome.call(this, 4);',
  17637. ' };',
  17638. ' this.DoSome$1 = function (j) {',
  17639. ' ExtB.DoSome.apply(this, arguments);',
  17640. ' };',
  17641. '});',
  17642. '']),
  17643. LinesToStr([ // $mod.$main
  17644. '']));
  17645. end;
  17646. procedure TTestModule.TestExternalClass_PascalAncestorFail;
  17647. begin
  17648. StartProgram(false);
  17649. Add('{$modeswitch externalclass}');
  17650. Add('type');
  17651. Add(' TObject = class');
  17652. Add(' end;');
  17653. Add(' TExtA = class external name ''ExtA''(TObject)');
  17654. Add(' end;');
  17655. Add('begin');
  17656. SetExpectedPasResolverError('Ancestor "TObject" is not external',nAncestorIsNotExternal);
  17657. ConvertProgram;
  17658. end;
  17659. procedure TTestModule.TestExternalClass_NewInstance;
  17660. begin
  17661. StartProgram(false);
  17662. Add('{$modeswitch externalclass}');
  17663. Add('type');
  17664. Add(' TExtA = class external name ''ExtA''');
  17665. Add(' end;');
  17666. Add(' TMyB = class(TExtA)');
  17667. Add(' protected');
  17668. Add(' class function NewInstance(fnname: string; const paramarray): TMyB; virtual;');
  17669. Add(' end;');
  17670. Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
  17671. Add('begin end;');
  17672. Add('begin');
  17673. ConvertProgram;
  17674. CheckSource('TestExternalClass_NewInstance',
  17675. LinesToStr([ // statements
  17676. 'rtl.createClassExt(this, "TMyB", ExtA, "NewInstance", function () {',
  17677. ' this.$init = function () {',
  17678. ' };',
  17679. ' this.$final = function () {',
  17680. ' };',
  17681. ' this.NewInstance = function (fnname, paramarray) {',
  17682. ' var Result = null;',
  17683. ' return Result;',
  17684. ' };',
  17685. '});',
  17686. '']),
  17687. LinesToStr([ // $mod.$main
  17688. '']));
  17689. end;
  17690. procedure TTestModule.TestExternalClass_NewInstance_NonVirtualFail;
  17691. begin
  17692. StartProgram(false);
  17693. Add('{$modeswitch externalclass}');
  17694. Add('type');
  17695. Add(' TExtA = class external name ''ExtA''');
  17696. Add(' end;');
  17697. Add(' TMyB = class(TExtA)');
  17698. Add(' protected');
  17699. Add(' class function NewInstance(fnname: string; const paramarray): TMyB;');
  17700. Add(' end;');
  17701. Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
  17702. Add('begin end;');
  17703. Add('begin');
  17704. SetExpectedPasResolverError(sNewInstanceFunctionMustBeVirtual,nNewInstanceFunctionMustBeVirtual);
  17705. ConvertProgram;
  17706. end;
  17707. procedure TTestModule.TestExternalClass_NewInstance_FirstParamNotString_Fail;
  17708. begin
  17709. StartProgram(false);
  17710. Add('{$modeswitch externalclass}');
  17711. Add('type');
  17712. Add(' TExtA = class external name ''ExtA''');
  17713. Add(' end;');
  17714. Add(' TMyB = class(TExtA)');
  17715. Add(' protected');
  17716. Add(' class function NewInstance(fnname: longint; const paramarray): TMyB; virtual;');
  17717. Add(' end;');
  17718. Add('class function TMyB.NewInstance(fnname: longint; const paramarray): TMyB;');
  17719. Add('begin end;');
  17720. Add('begin');
  17721. SetExpectedPasResolverError('Incompatible type arg no. 1: Got "Longint", expected "String"',
  17722. nIncompatibleTypeArgNo);
  17723. ConvertProgram;
  17724. end;
  17725. procedure TTestModule.TestExternalClass_NewInstance_SecondParamTyped_Fail;
  17726. begin
  17727. StartProgram(false);
  17728. Add('{$modeswitch externalclass}');
  17729. Add('type');
  17730. Add(' TExtA = class external name ''ExtA''');
  17731. Add(' end;');
  17732. Add(' TMyB = class(TExtA)');
  17733. Add(' protected');
  17734. Add(' class function NewInstance(fnname: string; const paramarray: string): TMyB; virtual;');
  17735. Add(' end;');
  17736. Add('class function TMyB.NewInstance(fnname: string; const paramarray: string): TMyB;');
  17737. Add('begin end;');
  17738. Add('begin');
  17739. SetExpectedPasResolverError('Incompatible type arg no. 2: Got "type", expected "untyped"',
  17740. nIncompatibleTypeArgNo);
  17741. ConvertProgram;
  17742. end;
  17743. procedure TTestModule.TestExternalClass_JSFunctionPasDescendant;
  17744. begin
  17745. StartProgram(false);
  17746. Add([
  17747. '{$modeswitch externalclass}',
  17748. 'type',
  17749. ' TJSFunction = class external name ''Function''',
  17750. ' end;',
  17751. ' TExtA = class external name ''ExtA''(TJSFunction)',
  17752. ' constructor New(w: word);',
  17753. ' end;',
  17754. ' TBird = class (TExtA)',
  17755. ' public',
  17756. ' Size: word;',
  17757. ' class var Legs: word;',
  17758. ' constructor Create(a: word);',
  17759. ' end;',
  17760. ' TEagle = class (TBird)',
  17761. ' public',
  17762. ' constructor Create(b: word); reintroduce;',
  17763. ' end;',
  17764. 'constructor TBird.Create(a: word);',
  17765. 'begin',
  17766. ' inherited;', // silently ignored
  17767. ' inherited New(a);', // this.$func(a)
  17768. 'end;',
  17769. 'constructor TEagle.Create(b: word);',
  17770. 'begin',
  17771. ' inherited Create(b);',
  17772. 'end;',
  17773. 'var',
  17774. ' Bird: TBird;',
  17775. ' Eagle: TEagle;',
  17776. 'begin',
  17777. ' Bird:=TBird.Create(3);',
  17778. ' Eagle:=TEagle.Create(4);',
  17779. ' Bird.Size:=Bird.Size+5;',
  17780. ' Bird.Legs:=Bird.Legs+6;',
  17781. ' Eagle.Size:=Eagle.Size+5;',
  17782. ' Eagle.Legs:=Eagle.Legs+6;',
  17783. '']);
  17784. ConvertProgram;
  17785. CheckSource('TestExternalClass_JSFunctionPasDescendant',
  17786. LinesToStr([ // statements
  17787. 'rtl.createClassExt(this, "TBird", ExtA, "", function () {',
  17788. ' this.Legs = 0;',
  17789. ' this.$init = function () {',
  17790. ' this.Size = 0;',
  17791. ' };',
  17792. ' this.$final = function () {',
  17793. ' };',
  17794. ' this.Create = function (a) {',
  17795. ' this.$ancestorfunc(a);',
  17796. ' return this;',
  17797. ' };',
  17798. '});',
  17799. 'rtl.createClassExt(this, "TEagle", this.TBird, "", function () {',
  17800. ' this.Create$1 = function (b) {',
  17801. ' $mod.TBird.Create.call(this, b);',
  17802. ' return this;',
  17803. ' };',
  17804. '});',
  17805. 'this.Bird = null;',
  17806. 'this.Eagle = null;',
  17807. '']),
  17808. LinesToStr([ // $mod.$main
  17809. '$mod.Bird = $mod.TBird.$create("Create", [3]);',
  17810. '$mod.Eagle = $mod.TEagle.$create("Create$1", [4]);',
  17811. '$mod.Bird.Size = $mod.Bird.Size + 5;',
  17812. '$mod.TBird.Legs = $mod.Bird.Legs + 6;',
  17813. '$mod.Eagle.Size = $mod.Eagle.Size + 5;',
  17814. '$mod.TBird.Legs = $mod.Eagle.Legs + 6;',
  17815. '']));
  17816. end;
  17817. procedure TTestModule.TestExternalClass_PascalProperty;
  17818. begin
  17819. StartProgram(false);
  17820. Add('{$modeswitch externalclass}');
  17821. Add('type');
  17822. Add(' TJSElement = class;');
  17823. Add(' TJSNotifyEvent = procedure(Sender: TJSElement) of object;');
  17824. Add(' TJSElement = class external name ''ExtA''');
  17825. Add(' end;');
  17826. Add(' TControl = class(TJSElement)');
  17827. Add(' private');
  17828. Add(' FOnClick: TJSNotifyEvent;');
  17829. Add(' property OnClick: TJSNotifyEvent read FOnClick write FOnClick;');
  17830. Add(' procedure Click(Sender: TJSElement);');
  17831. Add(' end;');
  17832. Add('procedure TControl.Click(Sender: TJSElement);');
  17833. Add('begin');
  17834. Add(' OnClick(Self);');
  17835. Add('end;');
  17836. Add('var');
  17837. Add(' Ctrl: TControl;');
  17838. Add('begin');
  17839. Add(' Ctrl.OnClick:[email protected];');
  17840. Add(' Ctrl.OnClick(Ctrl);');
  17841. ConvertProgram;
  17842. CheckSource('TestExternalClass_PascalProperty',
  17843. LinesToStr([ // statements
  17844. 'rtl.createClassExt(this, "TControl", ExtA, "", function () {',
  17845. ' this.$init = function () {',
  17846. ' this.FOnClick = null;',
  17847. ' };',
  17848. ' this.$final = function () {',
  17849. ' this.FOnClick = undefined;',
  17850. ' };',
  17851. ' this.Click = function (Sender) {',
  17852. ' this.FOnClick(this);',
  17853. ' };',
  17854. '});',
  17855. 'this.Ctrl = null;',
  17856. '']),
  17857. LinesToStr([ // $mod.$main
  17858. '$mod.Ctrl.FOnClick = rtl.createCallback($mod.Ctrl, "Click");',
  17859. '$mod.Ctrl.FOnClick($mod.Ctrl);',
  17860. '']));
  17861. end;
  17862. procedure TTestModule.TestExternalClass_TypeCastToRootClass;
  17863. begin
  17864. StartProgram(false);
  17865. Add([
  17866. '{$modeswitch externalclass}',
  17867. 'type',
  17868. ' IUnknown = interface end;',
  17869. ' TObject = class',
  17870. ' end;',
  17871. ' TChild = class',
  17872. ' end;',
  17873. ' TExtRootA = class external name ''ExtRootA''',
  17874. ' end;',
  17875. ' TExtChildA = class external name ''ExtChildA''(TExtRootA)',
  17876. ' end;',
  17877. ' TExtRootB = class external name ''ExtRootB''',
  17878. ' end;',
  17879. ' TExtChildB = class external name ''ExtChildB''(TExtRootB)',
  17880. ' end;',
  17881. 'var',
  17882. ' Obj: TObject;',
  17883. ' Child: TChild;',
  17884. ' RootA: TExtRootA;',
  17885. ' ChildA: TExtChildA;',
  17886. ' RootB: TExtRootB;',
  17887. ' ChildB: TExtChildB;',
  17888. ' i: IUnknown;',
  17889. 'begin',
  17890. ' obj:=tobject(roota);',
  17891. ' obj:=tobject(childa);',
  17892. ' child:=tchild(tobject(roota));',
  17893. ' roota:=textroota(obj);',
  17894. ' roota:=textroota(child);',
  17895. ' roota:=textroota(rootb);',
  17896. ' roota:=textroota(childb);',
  17897. ' childa:=textchilda(textroota(obj));',
  17898. ' roota:=TExtRootA(i)',
  17899. '']);
  17900. ConvertProgram;
  17901. CheckSource('TestExternalClass_TypeCastToRootClass',
  17902. LinesToStr([ // statements
  17903. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  17904. 'rtl.createClass(this, "TObject", null, function () {',
  17905. ' this.$init = function () {',
  17906. ' };',
  17907. ' this.$final = function () {',
  17908. ' };',
  17909. '});',
  17910. 'rtl.createClass(this, "TChild", this.TObject, function () {',
  17911. '});',
  17912. 'this.Obj = null;',
  17913. 'this.Child = null;',
  17914. 'this.RootA = null;',
  17915. 'this.ChildA = null;',
  17916. 'this.RootB = null;',
  17917. 'this.ChildB = null;',
  17918. 'this.i = null;',
  17919. '']),
  17920. LinesToStr([ // $mod.$main
  17921. '$mod.Obj = $mod.RootA;',
  17922. '$mod.Obj = $mod.ChildA;',
  17923. '$mod.Child = $mod.RootA;',
  17924. '$mod.RootA = $mod.Obj;',
  17925. '$mod.RootA = $mod.Child;',
  17926. '$mod.RootA = $mod.RootB;',
  17927. '$mod.RootA = $mod.ChildB;',
  17928. '$mod.ChildA = $mod.Obj;',
  17929. '$mod.RootA = $mod.i;',
  17930. '']));
  17931. end;
  17932. procedure TTestModule.TestExternalClass_TypeCastToJSObject;
  17933. begin
  17934. StartProgram(false);
  17935. Add([
  17936. '{$modeswitch externalclass}',
  17937. 'type',
  17938. ' IUnknown = interface end;',
  17939. ' IBird = interface(IUnknown) end;',
  17940. ' TClass = class of TObject;',
  17941. ' TObject = class',
  17942. ' end;',
  17943. ' TChild = class',
  17944. ' end;',
  17945. ' TJSObject = class external name ''Object''',
  17946. ' end;',
  17947. ' TRec = record end;',
  17948. 'var',
  17949. ' Obj: TObject;',
  17950. ' Child: TChild;',
  17951. ' i: IUnknown;',
  17952. ' Bird: IBird;',
  17953. ' j: TJSObject;',
  17954. ' r: TRec;',
  17955. ' c: TClass;',
  17956. 'begin',
  17957. ' j:=tjsobject(IUnknown);',
  17958. ' j:=tjsobject(IBird);',
  17959. ' j:=tjsobject(TObject);',
  17960. ' j:=tjsobject(TChild);',
  17961. ' j:=tjsobject(TRec);',
  17962. ' j:=tjsobject(Obj);',
  17963. ' j:=tjsobject(Child);',
  17964. ' j:=tjsobject(i);',
  17965. ' j:=tjsobject(Bird);',
  17966. ' j:=tjsobject(r);',
  17967. ' j:=tjsobject(c);',
  17968. '']);
  17969. ConvertProgram;
  17970. CheckSource('TestExternalClass_TypeCastToJSObject',
  17971. LinesToStr([ // statements
  17972. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  17973. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], this.IUnknown);',
  17974. 'rtl.createClass(this, "TObject", null, function () {',
  17975. ' this.$init = function () {',
  17976. ' };',
  17977. ' this.$final = function () {',
  17978. ' };',
  17979. '});',
  17980. 'rtl.createClass(this, "TChild", this.TObject, function () {',
  17981. '});',
  17982. 'rtl.recNewT(this, "TRec", function () {',
  17983. ' this.$eq = function (b) {',
  17984. ' return true;',
  17985. ' };',
  17986. ' this.$assign = function (s) {',
  17987. ' return this;',
  17988. ' };',
  17989. '});',
  17990. 'this.Obj = null;',
  17991. 'this.Child = null;',
  17992. 'this.i = null;',
  17993. 'this.Bird = null;',
  17994. 'this.j = null;',
  17995. 'this.r = this.TRec.$new();',
  17996. 'this.c = null;',
  17997. '']),
  17998. LinesToStr([ // $mod.$main
  17999. '$mod.j = $mod.IUnknown;',
  18000. '$mod.j = $mod.IBird;',
  18001. '$mod.j = $mod.TObject;',
  18002. '$mod.j = $mod.TChild;',
  18003. '$mod.j = $mod.TRec;',
  18004. '$mod.j = $mod.Obj;',
  18005. '$mod.j = $mod.Child;',
  18006. '$mod.j = $mod.i;',
  18007. '$mod.j = $mod.Bird;',
  18008. '$mod.j = $mod.r;',
  18009. '$mod.j = $mod.c;',
  18010. '']));
  18011. end;
  18012. procedure TTestModule.TestExternalClass_TypeCastStringToExternalString;
  18013. begin
  18014. StartProgram(false);
  18015. Add('{$modeswitch externalclass}');
  18016. Add('type');
  18017. Add(' TJSString = class external name ''String''');
  18018. Add(' class function fromCharCode() : string; varargs;');
  18019. Add(' function anchor(const aName : string) : string;');
  18020. Add(' end;');
  18021. Add('var');
  18022. Add(' s: string;');
  18023. Add('begin');
  18024. Add(' s:=TJSString.fromCharCode(65,66);');
  18025. Add(' s:=TJSString(s).anchor(s);');
  18026. Add(' s:=TJSString(''foo'').anchor(s);');
  18027. ConvertProgram;
  18028. CheckSource('TestExternalClass_TypeCastStringToExternalString',
  18029. LinesToStr([ // statements
  18030. 'this.s = "";',
  18031. '']),
  18032. LinesToStr([ // $mod.$main
  18033. '$mod.s = String.fromCharCode(65, 66);',
  18034. '$mod.s = $mod.s.anchor($mod.s);',
  18035. '$mod.s = "foo".anchor($mod.s);',
  18036. '']));
  18037. end;
  18038. procedure TTestModule.TestExternalClass_TypeCastToJSFunction;
  18039. begin
  18040. StartProgram(false);
  18041. Add([
  18042. '{$modeswitch externalclass}',
  18043. 'type',
  18044. ' TJSObject = class external name ''Object'' end;',
  18045. ' TJSFunction = class external name ''Function''',
  18046. ' function bind(thisArg: TJSObject): TJSFunction; varargs;',
  18047. ' function call(thisArg: TJSObject): JSValue; varargs;',
  18048. ' end;',
  18049. ' TObject = class',
  18050. ' procedure DoIt(i: longint);',
  18051. ' end;',
  18052. ' TFuncInt = function(o: TObject): longint;',
  18053. 'function GetIt(o: TObject): longint;',
  18054. ' procedure Sub; begin end;',
  18055. 'var',
  18056. ' f: TJSFunction;',
  18057. ' fi: TFuncInt;',
  18058. 'begin',
  18059. ' fi:=TFuncInt(f);',
  18060. ' f:=TJSFunction(fi);',
  18061. ' f:=TJSFunction(@GetIt);',
  18062. ' f:=TJSFunction(@GetIt).bind(nil,3);',
  18063. ' f:=TJSFunction(@Sub);',
  18064. ' f:=TJSFunction(@o.doit);',
  18065. ' f:=TJSFunction(fi).bind(nil,4)',
  18066. 'end;',
  18067. 'procedure TObject.DoIt(i: longint);',
  18068. ' procedure Sub; begin end;',
  18069. 'var f: TJSFunction;',
  18070. 'begin',
  18071. ' f:=TJSFunction(@DoIt);',
  18072. ' f:=TJSFunction(@DoIt).bind(nil,13);',
  18073. ' f:=TJSFunction(@Sub);',
  18074. ' f:=TJSFunction(@GetIt);',
  18075. 'end;',
  18076. 'begin']);
  18077. ConvertProgram;
  18078. CheckSource('TestExternalClass_TypeCastToJSFunction',
  18079. LinesToStr([ // statements
  18080. 'rtl.createClass(this, "TObject", null, function () {',
  18081. ' this.$init = function () {',
  18082. ' };',
  18083. ' this.$final = function () {',
  18084. ' };',
  18085. ' this.DoIt = function (i) {',
  18086. ' var $Self = this;',
  18087. ' function Sub() {',
  18088. ' };',
  18089. ' var f = null;',
  18090. ' f = this.DoIt;',
  18091. ' f = this.DoIt.bind(null, 13);',
  18092. ' f = Sub;',
  18093. ' f = $mod.GetIt;',
  18094. ' };',
  18095. '});',
  18096. 'this.GetIt = function (o) {',
  18097. ' var Result = 0;',
  18098. ' function Sub() {',
  18099. ' };',
  18100. ' var f = null;',
  18101. ' var fi = null;',
  18102. ' fi = f;',
  18103. ' f = fi;',
  18104. ' f = $mod.GetIt;',
  18105. ' f = $mod.GetIt.bind(null, 3);',
  18106. ' f = Sub;',
  18107. ' f = $mod.TObject.DoIt;',
  18108. ' f = fi.bind(null, 4);',
  18109. ' return Result;',
  18110. '};',
  18111. '']),
  18112. LinesToStr([ // $mod.$main
  18113. '']));
  18114. end;
  18115. procedure TTestModule.TestExternalClass_TypeCastDelphiUnrelated;
  18116. begin
  18117. StartProgram(false);
  18118. Add([
  18119. '{$mode delphi}',
  18120. '{$modeswitch externalclass}',
  18121. 'type',
  18122. ' TJSObject = class external name ''Object'' end;',
  18123. ' TJSWindow = class external name ''Window''(TJSObject)',
  18124. ' procedure Open;',
  18125. ' end;',
  18126. ' TJSEventTarget = class external name ''Event''(TJSObject)',
  18127. ' procedure Execute;',
  18128. ' end;',
  18129. 'procedure Fly;',
  18130. 'var',
  18131. ' w: TJSWindow;',
  18132. ' e: TJSEventTarget;',
  18133. 'begin',
  18134. ' w:=TJSWindow(e);',
  18135. ' e:=TJSEventTarget(w);',
  18136. 'end;',
  18137. 'begin']);
  18138. ConvertProgram;
  18139. CheckSource('TestExternalClass_TypeCastDelphiUnrelated',
  18140. LinesToStr([ // statements
  18141. 'this.Fly = function () {',
  18142. ' var w = null;',
  18143. ' var e = null;',
  18144. ' w = e;',
  18145. ' e = w;',
  18146. '};',
  18147. '']),
  18148. LinesToStr([ // $mod.$main
  18149. '']));
  18150. end;
  18151. procedure TTestModule.TestExternalClass_CallClassFunctionOfInstanceFail;
  18152. begin
  18153. StartProgram(false);
  18154. Add('{$modeswitch externalclass}');
  18155. Add('type');
  18156. Add(' TJSString = class external name ''String''');
  18157. Add(' class function fromCharCode() : string; varargs;');
  18158. Add(' end;');
  18159. Add('var');
  18160. Add(' s: string;');
  18161. Add(' sObj: TJSString;');
  18162. Add('begin');
  18163. Add(' s:=sObj.fromCharCode(65,66);');
  18164. SetExpectedPasResolverError('External class instance cannot access static class function fromCharCode',
  18165. nExternalClassInstanceCannotAccessStaticX);
  18166. ConvertProgram;
  18167. end;
  18168. procedure TTestModule.TestExternalClass_BracketAccessor;
  18169. begin
  18170. StartProgram(false);
  18171. Add([
  18172. '{$modeswitch externalclass}',
  18173. 'type',
  18174. ' TJSArray = class external name ''Array2''',
  18175. ' function GetItems(Index: longint): jsvalue; external name ''[]'';',
  18176. ' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';',
  18177. ' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;',
  18178. ' end;',
  18179. 'procedure DoIt(vI: JSValue; const vJ: jsvalue; var vK: jsvalue; out vL: jsvalue);',
  18180. 'begin end;',
  18181. 'var',
  18182. ' Arr: tjsarray;',
  18183. ' s: string;',
  18184. ' i: longint;',
  18185. ' v: jsvalue;',
  18186. 'begin',
  18187. ' v:=arr[0];',
  18188. ' v:=arr.items[1];',
  18189. ' arr[2]:=s;',
  18190. ' arr.items[3]:=s;',
  18191. ' arr[4]:=i;',
  18192. ' arr[5]:=arr[6];',
  18193. ' arr.items[7]:=arr.items[8];',
  18194. ' with arr do items[9]:=items[10];',
  18195. ' doit(arr[7],arr[8],arr[9],arr[10]);',
  18196. ' with arr do begin',
  18197. ' v:=GetItems(14);',
  18198. ' setitems(15,16);',
  18199. ' end;',
  18200. ' v:=test1.arr.items[17];',
  18201. ' test1.arr.items[18]:=v;',
  18202. '']);
  18203. ConvertProgram;
  18204. CheckSource('TestExternalClass_BracketAccessor',
  18205. LinesToStr([ // statements
  18206. 'this.DoIt = function (vI, vJ, vK, vL) {',
  18207. '};',
  18208. 'this.Arr = null;',
  18209. 'this.s = "";',
  18210. 'this.i = 0;',
  18211. 'this.v = undefined;',
  18212. '']),
  18213. LinesToStr([ // $mod.$main
  18214. '$mod.v = $mod.Arr[0];',
  18215. '$mod.v = $mod.Arr[1];',
  18216. '$mod.Arr[2] = $mod.s;',
  18217. '$mod.Arr[3] = $mod.s;',
  18218. '$mod.Arr[4] = $mod.i;',
  18219. '$mod.Arr[5] = $mod.Arr[6];',
  18220. '$mod.Arr[7] = $mod.Arr[8];',
  18221. 'var $with = $mod.Arr;',
  18222. '$with[9] = $with[10];',
  18223. '$mod.DoIt($mod.Arr[7], $mod.Arr[8], {',
  18224. ' a: 9,',
  18225. ' p: $mod.Arr,',
  18226. ' get: function () {',
  18227. ' return this.p[this.a];',
  18228. ' },',
  18229. ' set: function (v) {',
  18230. ' this.p[this.a] = v;',
  18231. ' }',
  18232. '}, {',
  18233. ' a: 10,',
  18234. ' p: $mod.Arr,',
  18235. ' get: function () {',
  18236. ' return this.p[this.a];',
  18237. ' },',
  18238. ' set: function (v) {',
  18239. ' this.p[this.a] = v;',
  18240. ' }',
  18241. '});',
  18242. 'var $with1 = $mod.Arr;',
  18243. '$mod.v = $with1[14];',
  18244. '$with1[15] = 16;',
  18245. '$mod.v = $mod.Arr[17];',
  18246. '$mod.Arr[18] = $mod.v;',
  18247. '']));
  18248. end;
  18249. procedure TTestModule.TestExternalClass_BracketAccessor_Call;
  18250. begin
  18251. StartProgram(false);
  18252. Add([
  18253. '{$modeswitch externalclass}',
  18254. 'type',
  18255. ' TJSArray = class external name ''Array2''',
  18256. ' function GetItems(Index: longint): jsvalue; external name ''[]'';',
  18257. ' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';',
  18258. ' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;',
  18259. ' end;',
  18260. ' TMyArr = class(TJSArray)',
  18261. ' procedure DoIt;',
  18262. ' end;',
  18263. 'procedure tmyarr.DoIt;',
  18264. 'begin',
  18265. ' Items[1]:=Items[2];',
  18266. ' SetItems(3,getItems(4));',
  18267. 'end;',
  18268. 'var',
  18269. ' Arr: tmyarr;',
  18270. ' s: string;',
  18271. ' i: longint;',
  18272. ' v: jsvalue;',
  18273. 'begin',
  18274. ' v:=arr[0];',
  18275. ' v:=arr.items[1];',
  18276. ' arr[2]:=s;',
  18277. ' arr.items[3]:=s;',
  18278. ' arr[4]:=i;',
  18279. ' arr[5]:=arr[6];',
  18280. ' arr.items[7]:=arr.items[8];',
  18281. ' with arr do items[9]:=items[10];',
  18282. ' with arr do begin',
  18283. ' v:=GetItems(14);',
  18284. ' setitems(15,16);',
  18285. ' end;',
  18286. '']);
  18287. ConvertProgram;
  18288. CheckSource('TestExternalClass_BracketAccessor_Call',
  18289. LinesToStr([ // statements
  18290. 'rtl.createClassExt(this, "TMyArr", Array2, "", function () {',
  18291. ' this.$init = function () {',
  18292. ' };',
  18293. ' this.$final = function () {',
  18294. ' };',
  18295. ' this.DoIt = function () {',
  18296. ' this[1] = this[2];',
  18297. ' this[3] = this[4];',
  18298. ' };',
  18299. '});',
  18300. 'this.Arr = null;',
  18301. 'this.s = "";',
  18302. 'this.i = 0;',
  18303. 'this.v = undefined;',
  18304. '']),
  18305. LinesToStr([ // $mod.$main
  18306. '$mod.v = $mod.Arr[0];',
  18307. '$mod.v = $mod.Arr[1];',
  18308. '$mod.Arr[2] = $mod.s;',
  18309. '$mod.Arr[3] = $mod.s;',
  18310. '$mod.Arr[4] = $mod.i;',
  18311. '$mod.Arr[5] = $mod.Arr[6];',
  18312. '$mod.Arr[7] = $mod.Arr[8];',
  18313. 'var $with = $mod.Arr;',
  18314. '$with[9] = $with[10];',
  18315. 'var $with1 = $mod.Arr;',
  18316. '$mod.v = $with1[14];',
  18317. '$with1[15] = 16;',
  18318. '']));
  18319. end;
  18320. procedure TTestModule.TestExternalClass_BracketAccessor_2ParamsFail;
  18321. begin
  18322. StartProgram(false);
  18323. Add('{$modeswitch externalclass}');
  18324. Add('type');
  18325. Add(' TJSArray = class external name ''Array2''');
  18326. Add(' function GetItems(Index1, Index2: longint): jsvalue; external name ''[]'';');
  18327. Add(' procedure SetItems(Index1, Index2: longint; Value: jsvalue); external name ''[]'';');
  18328. Add(' property Items[Index1, Index2: longint]: jsvalue read GetItems write SetItems; default;');
  18329. Add(' end;');
  18330. Add('begin');
  18331. SetExpectedPasResolverError(sBracketAccessorOfExternalClassMustHaveOneParameter,
  18332. nBracketAccessorOfExternalClassMustHaveOneParameter);
  18333. ConvertProgram;
  18334. end;
  18335. procedure TTestModule.TestExternalClass_BracketAccessor_ReadOnly;
  18336. begin
  18337. StartProgram(false);
  18338. Add('{$modeswitch externalclass}');
  18339. Add('type');
  18340. Add(' TJSArray = class external name ''Array2''');
  18341. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  18342. Add(' property Items[Index: longint]: jsvalue read GetItems; default;');
  18343. Add(' end;');
  18344. Add('procedure DoIt(vI: JSValue; const vJ: jsvalue);');
  18345. Add('begin end;');
  18346. Add('var');
  18347. Add(' Arr: tjsarray;');
  18348. Add(' v: jsvalue;');
  18349. Add('begin');
  18350. Add(' v:=arr[0];');
  18351. Add(' v:=arr.items[1];');
  18352. Add(' with arr do v:=items[2];');
  18353. Add(' doit(arr[3],arr[4]);');
  18354. ConvertProgram;
  18355. CheckSource('TestExternalClass_BracketAccessor_ReadOnly',
  18356. LinesToStr([ // statements
  18357. 'this.DoIt = function (vI, vJ) {',
  18358. '};',
  18359. 'this.Arr = null;',
  18360. 'this.v = undefined;',
  18361. '']),
  18362. LinesToStr([ // $mod.$main
  18363. '$mod.v = $mod.Arr[0];',
  18364. '$mod.v = $mod.Arr[1];',
  18365. 'var $with = $mod.Arr;',
  18366. '$mod.v = $with[2];',
  18367. '$mod.DoIt($mod.Arr[3], $mod.Arr[4]);',
  18368. '']));
  18369. end;
  18370. procedure TTestModule.TestExternalClass_BracketAccessor_WriteOnly;
  18371. begin
  18372. StartProgram(false);
  18373. Add('{$modeswitch externalclass}');
  18374. Add('type');
  18375. Add(' TJSArray = class external name ''Array2''');
  18376. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  18377. Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
  18378. Add(' end;');
  18379. Add('var');
  18380. Add(' Arr: tjsarray;');
  18381. Add(' s: string;');
  18382. Add(' i: longint;');
  18383. Add(' v: jsvalue;');
  18384. Add('begin');
  18385. Add(' arr[2]:=s;');
  18386. Add(' arr.items[3]:=s;');
  18387. Add(' arr[4]:=i;');
  18388. Add(' with arr do items[5]:=i;');
  18389. ConvertProgram;
  18390. CheckSource('TestExternalClass_BracketAccessor_WriteOnly',
  18391. LinesToStr([ // statements
  18392. 'this.Arr = null;',
  18393. 'this.s = "";',
  18394. 'this.i = 0;',
  18395. 'this.v = undefined;',
  18396. '']),
  18397. LinesToStr([ // $mod.$main
  18398. '$mod.Arr[2] = $mod.s;',
  18399. '$mod.Arr[3] = $mod.s;',
  18400. '$mod.Arr[4] = $mod.i;',
  18401. 'var $with = $mod.Arr;',
  18402. '$with[5] = $mod.i;',
  18403. '']));
  18404. end;
  18405. procedure TTestModule.TestExternalClass_BracketAccessor_MultiType;
  18406. begin
  18407. StartProgram(false);
  18408. Add('{$modeswitch externalclass}');
  18409. Add('type');
  18410. Add(' TJSArray = class external name ''Array2''');
  18411. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  18412. Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
  18413. Add(' procedure SetNumbers(Index: longint; Value: longint); external name ''[]'';');
  18414. Add(' property Numbers[Index: longint]: longint write SetNumbers;');
  18415. Add(' end;');
  18416. Add('var');
  18417. Add(' Arr: tjsarray;');
  18418. Add(' s: string;');
  18419. Add(' i: longint;');
  18420. Add(' v: jsvalue;');
  18421. Add('begin');
  18422. Add(' arr[2]:=s;');
  18423. Add(' arr.items[3]:=s;');
  18424. Add(' arr.numbers[4]:=i;');
  18425. Add(' with arr do items[5]:=i;');
  18426. Add(' with arr do numbers[6]:=i;');
  18427. ConvertProgram;
  18428. CheckSource('TestExternalClass_BracketAccessor_MultiType',
  18429. LinesToStr([ // statements
  18430. 'this.Arr = null;',
  18431. 'this.s = "";',
  18432. 'this.i = 0;',
  18433. 'this.v = undefined;',
  18434. '']),
  18435. LinesToStr([ // $mod.$main
  18436. '$mod.Arr[2] = $mod.s;',
  18437. '$mod.Arr[3] = $mod.s;',
  18438. '$mod.Arr[4] = $mod.i;',
  18439. 'var $with = $mod.Arr;',
  18440. '$with[5] = $mod.i;',
  18441. 'var $with1 = $mod.Arr;',
  18442. '$with1[6] = $mod.i;',
  18443. '']));
  18444. end;
  18445. procedure TTestModule.TestExternalClass_BracketAccessor_Index;
  18446. begin
  18447. StartProgram(false);
  18448. Add('{$modeswitch externalclass}');
  18449. Add('type');
  18450. Add(' TJSArray = class external name ''Array2''');
  18451. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  18452. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  18453. Add(' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;');
  18454. Add(' end;');
  18455. Add('var');
  18456. Add(' Arr: tjsarray;');
  18457. Add(' i: longint;');
  18458. Add(' IntArr: array of longint;');
  18459. Add(' v: jsvalue;');
  18460. Add('begin');
  18461. Add(' v:=arr.items[i];');
  18462. Add(' arr[longint(v)]:=arr.items[intarr[0]];');
  18463. Add(' arr.items[intarr[1]]:=arr[IntArr[2]];');
  18464. ConvertProgram;
  18465. CheckSource('TestExternalClass_BracketAccessor_Index',
  18466. LinesToStr([ // statements
  18467. 'this.Arr = null;',
  18468. 'this.i = 0;',
  18469. 'this.IntArr = [];',
  18470. 'this.v = undefined;',
  18471. '']),
  18472. LinesToStr([ // $mod.$main
  18473. '$mod.v = $mod.Arr[$mod.i];',
  18474. '$mod.Arr[rtl.trunc($mod.v)] = $mod.Arr[$mod.IntArr[0]];',
  18475. '$mod.Arr[$mod.IntArr[1]] = $mod.Arr[$mod.IntArr[2]];',
  18476. '']));
  18477. end;
  18478. procedure TTestModule.TestExternalClass_ForInJSObject;
  18479. begin
  18480. StartProgram(false);
  18481. Add([
  18482. '{$modeswitch externalclass}',
  18483. 'type',
  18484. ' TJSObject = class external name ''Object''',
  18485. ' end;',
  18486. 'var',
  18487. ' o: TJSObject;',
  18488. ' key: string;',
  18489. 'begin',
  18490. ' for key in o do',
  18491. ' if key=''abc'' then ;',
  18492. '']);
  18493. ConvertProgram;
  18494. CheckSource('TestExternalClass_ForInJSObject',
  18495. LinesToStr([ // statements
  18496. 'this.o = null;',
  18497. 'this.key = "";',
  18498. '']),
  18499. LinesToStr([ // $mod.$main
  18500. 'for ($mod.key in $mod.o) if ($mod.key === "abc") ;',
  18501. '']));
  18502. end;
  18503. procedure TTestModule.TestExternalClass_ForInJSArray;
  18504. begin
  18505. StartProgram(false);
  18506. Add([
  18507. '{$modeswitch externalclass}',
  18508. 'type',
  18509. ' TJSInt8Array = class external name ''Int8Array''',
  18510. ' private',
  18511. ' flength: NativeInt external name ''length'';',
  18512. ' function getValue(Index: NativeInt): shortint; external name ''[]'';',
  18513. ' public',
  18514. ' property values[Index: NativeInt]: Shortint Read getValue; default;',
  18515. ' property Length: NativeInt read flength;',
  18516. ' end;',
  18517. 'var',
  18518. ' a: TJSInt8Array;',
  18519. ' value: shortint;',
  18520. 'begin',
  18521. ' for value in a do',
  18522. ' if value=3 then ;',
  18523. '']);
  18524. ConvertProgram;
  18525. CheckSource('TestExternalClass_ForInJSArray',
  18526. LinesToStr([ // statements
  18527. 'this.a = null;',
  18528. 'this.value = 0;',
  18529. '']),
  18530. LinesToStr([ // $mod.$main
  18531. 'for (var $in = $mod.a, $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) {',
  18532. ' $mod.value = $in[$l];',
  18533. ' if ($mod.value === 3) ;',
  18534. '};',
  18535. '']));
  18536. end;
  18537. procedure TTestModule.TestExternalClass_IncompatibleArgDuplicateIdentifier;
  18538. begin
  18539. AddModuleWithIntfImplSrc('unit2.pas',
  18540. LinesToStr([
  18541. '{$modeswitch externalclass}',
  18542. 'type',
  18543. ' TJSBufferSource = class external name ''BufferSource''',
  18544. ' end;',
  18545. 'procedure DoIt(s: TJSBufferSource); external name ''DoIt'';',
  18546. '']),
  18547. '');
  18548. AddModuleWithIntfImplSrc('unit3.pas',
  18549. LinesToStr([
  18550. '{$modeswitch externalclass}',
  18551. 'type',
  18552. ' TJSBufferSource = class external name ''BufferSource''',
  18553. ' end;',
  18554. '']),
  18555. '');
  18556. StartUnit(true);
  18557. Add([
  18558. 'interface',
  18559. 'uses unit2, unit3;',
  18560. 'procedure DoSome(s: TJSBufferSource);',
  18561. 'implementation',
  18562. 'procedure DoSome(s: TJSBufferSource);',
  18563. 'begin',
  18564. ' DoIt(s);',
  18565. 'end;',
  18566. '']);
  18567. SetExpectedPasResolverError('Incompatible type arg no. 1: Got "unit3.TJSBufferSource", expected "unit2.TJSBufferSource"',
  18568. nIncompatibleTypeArgNo);
  18569. ConvertUnit;
  18570. end;
  18571. procedure TTestModule.TestClassInterface_Corba;
  18572. begin
  18573. StartProgram(false);
  18574. Add([
  18575. '{$interfaces corba}',
  18576. 'type',
  18577. ' IUnknown = interface;',
  18578. ' IUnknown = interface',
  18579. ' [''{00000000-0000-0000-C000-000000000046}'']',
  18580. ' end;',
  18581. ' IInterface = IUnknown;',
  18582. ' IBird = interface(IInterface)',
  18583. ' function GetSize: longint;',
  18584. ' procedure SetSize(i: longint);',
  18585. ' property Size: longint read GetSize write SetSize;',
  18586. ' procedure DoIt(i: longint);',
  18587. ' end;',
  18588. ' TObject = class',
  18589. ' end;',
  18590. ' TBird = class(TObject,IBird)',
  18591. ' function GetSize: longint; virtual; abstract;',
  18592. ' procedure SetSize(i: longint); virtual; abstract;',
  18593. ' procedure DoIt(i: longint); virtual; abstract;',
  18594. ' end;',
  18595. 'var',
  18596. ' BirdIntf: IBird;',
  18597. 'begin',
  18598. ' BirdIntf.Size:=BirdIntf.Size;',
  18599. '']);
  18600. ConvertProgram;
  18601. CheckSource('TestClassInterface_Corba',
  18602. LinesToStr([ // statements
  18603. 'rtl.createInterface(this, "IUnknown", "{00000000-0000-0000-C000-000000000046}", [], null);',
  18604. 'rtl.createInterface(this, "IBird", "{5BD1A53B-69BB-37EE-AF32-BEFB86D85B03}", ["GetSize", "SetSize", "DoIt"], this.IUnknown);',
  18605. 'rtl.createClass(this, "TObject", null, function () {',
  18606. ' this.$init = function () {',
  18607. ' };',
  18608. ' this.$final = function () {',
  18609. ' };',
  18610. '});',
  18611. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18612. ' rtl.addIntf(this, $mod.IBird);',
  18613. '});',
  18614. 'this.BirdIntf = null;',
  18615. '']),
  18616. LinesToStr([ // $mod.$main
  18617. ' $mod.BirdIntf.SetSize($mod.BirdIntf.GetSize());',
  18618. '']));
  18619. end;
  18620. procedure TTestModule.TestClassInterface_ProcExternalFail;
  18621. begin
  18622. StartProgram(false);
  18623. Add([
  18624. '{$interfaces corba}',
  18625. 'type',
  18626. ' IUnknown = interface',
  18627. ' procedure DoIt; external name ''foo'';',
  18628. ' end;',
  18629. 'begin']);
  18630. SetExpectedParserError(
  18631. 'Fields are not allowed in interface at token "Identifier external" in file test1.pp at line 6 column 21',
  18632. nParserNoFieldsAllowed);
  18633. ConvertProgram;
  18634. end;
  18635. procedure TTestModule.TestClassInterface_Overloads;
  18636. begin
  18637. StartProgram(false);
  18638. Add([
  18639. '{$interfaces corba}',
  18640. 'type',
  18641. ' integer = longint;',
  18642. ' IUnknown = interface',
  18643. ' procedure DoIt(i: integer);',
  18644. ' procedure DoIt(s: string);',
  18645. ' end;',
  18646. ' IBird = interface(IUnknown)',
  18647. ' procedure DoIt(b: boolean); overload;',
  18648. ' end;',
  18649. ' TObject = class',
  18650. ' end;',
  18651. ' TBird = class(TObject,IBird)',
  18652. ' procedure DoIt(o: TObject);',
  18653. ' procedure DoIt(s: string);',
  18654. ' procedure DoIt(i: integer);',
  18655. ' procedure DoIt(b: boolean);',
  18656. ' end;',
  18657. 'procedure TBird.DoIt(o: TObject); begin end;',
  18658. 'procedure TBird.DoIt(s: string); begin end;',
  18659. 'procedure TBird.DoIt(i: integer); begin end;',
  18660. 'procedure TBird.DoIt(b: boolean); begin end;',
  18661. 'var',
  18662. ' BirdIntf: IBird;',
  18663. 'begin',
  18664. ' BirdIntf.DoIt(3);',
  18665. ' BirdIntf.DoIt(''abc'');',
  18666. ' BirdIntf.DoIt(true);',
  18667. '']);
  18668. ConvertProgram;
  18669. CheckSource('TestClassInterface_Overloads',
  18670. LinesToStr([ // statements
  18671. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDC4-8A2AE2C59400}", ["DoIt", "DoIt$1"], null);',
  18672. 'rtl.createInterface(this, "IBird", "{8285DD5E-EA3E-396E-AE88-000B86AABF05}", ["DoIt$2"], this.IUnknown);',
  18673. 'rtl.createClass(this, "TObject", null, function () {',
  18674. ' this.$init = function () {',
  18675. ' };',
  18676. ' this.$final = function () {',
  18677. ' };',
  18678. '});',
  18679. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18680. ' this.DoIt = function (o) {',
  18681. ' };',
  18682. ' this.DoIt$1 = function (s) {',
  18683. ' };',
  18684. ' this.DoIt$2 = function (i) {',
  18685. ' };',
  18686. ' this.DoIt$3 = function (b) {',
  18687. ' };',
  18688. ' rtl.addIntf(this, $mod.IBird, {',
  18689. ' DoIt$2: "DoIt$3",',
  18690. ' DoIt: "DoIt$2"',
  18691. ' });',
  18692. '});',
  18693. 'this.BirdIntf = null;',
  18694. '']),
  18695. LinesToStr([ // $mod.$main
  18696. '$mod.BirdIntf.DoIt(3);',
  18697. '$mod.BirdIntf.DoIt$1("abc");',
  18698. '$mod.BirdIntf.DoIt$2(true);',
  18699. '']));
  18700. end;
  18701. procedure TTestModule.TestClassInterface_DuplicateGUIInIntfListFail;
  18702. begin
  18703. StartProgram(false);
  18704. Add([
  18705. '{$interfaces corba}',
  18706. 'type',
  18707. ' IBird = interface',
  18708. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  18709. ' end;',
  18710. ' IDog = interface',
  18711. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  18712. ' end;',
  18713. ' TObject = class(IBird,IDog)',
  18714. ' end;',
  18715. 'begin']);
  18716. SetExpectedPasResolverError('Duplicate GUID {4B3BA825-E0EC-4799-A19C-55F714A07959} in IDog and IBird',
  18717. nDuplicateGUIDXInYZ);
  18718. ConvertProgram;
  18719. end;
  18720. procedure TTestModule.TestClassInterface_DuplicateGUIInAncestorFail;
  18721. begin
  18722. StartProgram(false);
  18723. Add([
  18724. '{$interfaces corba}',
  18725. 'type',
  18726. ' IAnimal = interface',
  18727. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  18728. ' end;',
  18729. ' IBird = interface(IAnimal)',
  18730. ' end;',
  18731. ' IHawk = interface(IBird)',
  18732. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  18733. ' end;',
  18734. 'begin']);
  18735. SetExpectedPasResolverError('Duplicate GUID {4B3BA825-E0EC-4799-A19C-55F714A07959} in IHawk and IAnimal',
  18736. nDuplicateGUIDXInYZ);
  18737. ConvertProgram;
  18738. end;
  18739. procedure TTestModule.TestClassInterface_AncestorImpl;
  18740. begin
  18741. StartProgram(false);
  18742. Add([
  18743. '{$interfaces corba}',
  18744. 'type',
  18745. ' integer = longint;',
  18746. ' IUnknown = interface',
  18747. ' procedure DoIt(i: integer);',
  18748. ' end;',
  18749. ' IBird = interface',
  18750. ' procedure Fly(i: integer);',
  18751. ' end;',
  18752. ' TObject = class(IUnknown)',
  18753. ' procedure DoIt(i: integer);',
  18754. ' end;',
  18755. ' TBird = class(IBird)',
  18756. ' procedure Fly(i: integer);',
  18757. ' end;',
  18758. 'procedure TObject.DoIt(i: integer); begin end;',
  18759. 'procedure TBird.Fly(i: integer); begin end;',
  18760. 'begin',
  18761. '']);
  18762. ConvertProgram;
  18763. CheckSource('TestClassInterface_AncestorIntf',
  18764. LinesToStr([ // statements
  18765. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDC4-8A2800000000}", ["DoIt"], null);',
  18766. 'rtl.createInterface(this, "IBird", "{B92D5841-6264-3AE3-BF20-000000000000}", ["Fly"], null);',
  18767. 'rtl.createClass(this, "TObject", null, function () {',
  18768. ' this.$init = function () {',
  18769. ' };',
  18770. ' this.$final = function () {',
  18771. ' };',
  18772. ' this.DoIt = function (i) {',
  18773. ' };',
  18774. ' rtl.addIntf(this, $mod.IUnknown);',
  18775. '});',
  18776. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18777. ' this.Fly = function (i) {',
  18778. ' };',
  18779. ' rtl.addIntf(this, $mod.IBird);',
  18780. ' rtl.addIntf(this, $mod.IUnknown);',
  18781. '});',
  18782. '']),
  18783. LinesToStr([ // $mod.$main
  18784. '']));
  18785. end;
  18786. procedure TTestModule.TestClassInterface_ImplReintroduce;
  18787. begin
  18788. StartProgram(false);
  18789. Add([
  18790. '{$interfaces corba}',
  18791. 'type',
  18792. ' integer = longint;',
  18793. ' IBird = interface',
  18794. ' procedure DoIt(i: integer);',
  18795. ' end;',
  18796. ' TObject = class',
  18797. ' procedure DoIt(i: integer);',
  18798. ' end;',
  18799. ' TBird = class(IBird)',
  18800. ' procedure DoIt(i: integer); virtual; reintroduce;',
  18801. ' end;',
  18802. 'procedure TObject.DoIt(i: integer); begin end;',
  18803. 'procedure TBird.DoIt(i: integer); begin end;',
  18804. 'begin',
  18805. '']);
  18806. ConvertProgram;
  18807. CheckSource('TestClassInterface_ImplReintroduce',
  18808. LinesToStr([ // statements
  18809. 'rtl.createInterface(this, "IBird", "{B92D5841-6264-3AE2-8594-000000000000}", ["DoIt"], null);',
  18810. 'rtl.createClass(this, "TObject", null, function () {',
  18811. ' this.$init = function () {',
  18812. ' };',
  18813. ' this.$final = function () {',
  18814. ' };',
  18815. ' this.DoIt = function (i) {',
  18816. ' };',
  18817. '});',
  18818. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18819. ' this.DoIt$1 = function (i) {',
  18820. ' };',
  18821. ' rtl.addIntf(this, $mod.IBird, {',
  18822. ' DoIt: "DoIt$1"',
  18823. ' });',
  18824. '});',
  18825. '']),
  18826. LinesToStr([ // $mod.$main
  18827. '']));
  18828. end;
  18829. procedure TTestModule.TestClassInterface_MethodResolution;
  18830. begin
  18831. StartProgram(false);
  18832. Add([
  18833. '{$interfaces corba}',
  18834. 'type',
  18835. ' IUnknown = interface',
  18836. ' procedure Walk(i: longint);',
  18837. ' end;',
  18838. ' IBird = interface(IUnknown)',
  18839. ' procedure Walk(b: boolean); overload;',
  18840. ' procedure Fly(s: string);',
  18841. ' end;',
  18842. ' TObject = class',
  18843. ' end;',
  18844. ' TBird = class(TObject,IBird)',
  18845. ' procedure IBird.Fly = Move;',
  18846. ' procedure IBird.Walk = Hop;',
  18847. ' procedure Hop(i: longint);',
  18848. ' procedure Move(s: string);',
  18849. ' procedure Hop(b: boolean);',
  18850. ' end;',
  18851. 'procedure TBird.Move(s: string); begin end;',
  18852. 'procedure TBird.Hop(i: longint); begin end;',
  18853. 'procedure TBird.Hop(b: boolean); begin end;',
  18854. 'var',
  18855. ' BirdIntf: IBird;',
  18856. 'begin',
  18857. ' BirdIntf.Walk(3);',
  18858. ' BirdIntf.Walk(true);',
  18859. ' BirdIntf.Fly(''abc'');',
  18860. '']);
  18861. ConvertProgram;
  18862. CheckSource('TestClassInterface_MethodResolution',
  18863. LinesToStr([ // statements
  18864. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDD7-23D600000000}", ["Walk"], null);',
  18865. 'rtl.createInterface(this, "IBird", "{CF8A4986-80F6-396E-AE88-000B86AAE208}", ["Walk$1", "Fly"], this.IUnknown);',
  18866. 'rtl.createClass(this, "TObject", null, function () {',
  18867. ' this.$init = function () {',
  18868. ' };',
  18869. ' this.$final = function () {',
  18870. ' };',
  18871. '});',
  18872. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18873. ' this.Hop = function (i) {',
  18874. ' };',
  18875. ' this.Move = function (s) {',
  18876. ' };',
  18877. ' this.Hop$1 = function (b) {',
  18878. ' };',
  18879. ' rtl.addIntf(this, $mod.IBird, {',
  18880. ' Walk$1: "Hop$1",',
  18881. ' Fly: "Move",',
  18882. ' Walk: "Hop"',
  18883. ' });',
  18884. '});',
  18885. 'this.BirdIntf = null;',
  18886. '']),
  18887. LinesToStr([ // $mod.$main
  18888. '$mod.BirdIntf.Walk(3);',
  18889. '$mod.BirdIntf.Walk$1(true);',
  18890. '$mod.BirdIntf.Fly("abc");',
  18891. '']));
  18892. end;
  18893. procedure TTestModule.TestClassInterface_AncestorMoreInterfaces;
  18894. begin
  18895. StartProgram(false);
  18896. Add([
  18897. '{$interfaces com}',
  18898. 'type',
  18899. ' IUnknown = interface',
  18900. ' function _AddRef: longint;',
  18901. ' procedure Walk;',
  18902. ' end;',
  18903. ' IBird = interface end;',
  18904. ' IDog = interface end;',
  18905. ' TObject = class(IBird,IDog)',
  18906. ' function _AddRef: longint; virtual; abstract;',
  18907. ' procedure Walk; virtual; abstract;',
  18908. ' end;',
  18909. ' TBird = class(IUnknown)',
  18910. ' end;',
  18911. 'begin',
  18912. '']);
  18913. ConvertProgram;
  18914. CheckSource('TestClassInterface_COM_AncestorLess',
  18915. LinesToStr([ // statements
  18916. 'rtl.createInterface(this, "IUnknown", "{8F2D5841-758A-322B-BDDF-21CD521DD723}", ["_AddRef", "Walk"], null);',
  18917. 'rtl.createInterface(this, "IBird", "{CCE11D4C-6504-3AEE-AE88-000B86AAE675}", [], this.IUnknown);',
  18918. 'rtl.createInterface(this, "IDog", "{CCE11D4C-6504-3AEE-AE88-000B8E5FC675}", [], this.IUnknown);',
  18919. 'rtl.createClass(this, "TObject", null, function () {',
  18920. ' this.$init = function () {',
  18921. ' };',
  18922. ' this.$final = function () {',
  18923. ' };',
  18924. ' rtl.addIntf(this, $mod.IBird);',
  18925. ' rtl.addIntf(this, $mod.IDog);',
  18926. '});',
  18927. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18928. ' rtl.addIntf(this, $mod.IUnknown);',
  18929. ' rtl.addIntf(this, $mod.IBird);',
  18930. ' rtl.addIntf(this, $mod.IDog);',
  18931. '});',
  18932. '']),
  18933. LinesToStr([ // $mod.$main
  18934. '']));
  18935. end;
  18936. procedure TTestModule.TestClassInterface_MethodOverride;
  18937. begin
  18938. StartProgram(false);
  18939. Add([
  18940. '{$interfaces corba}',
  18941. 'type',
  18942. ' IUnknown = interface',
  18943. ' [''{D6D98E5B-8A10-4FEC-856A-7BFC847FE74B}'']',
  18944. ' procedure Go;',
  18945. ' end;',
  18946. ' TObject = class(IUnknown)',
  18947. ' procedure Go; virtual; abstract;',
  18948. ' end;',
  18949. ' TBird = class',
  18950. ' procedure Go; override;',
  18951. ' end;',
  18952. ' TCat = class(TObject)',
  18953. ' procedure Go; override;',
  18954. ' end;',
  18955. ' TDog = class(TObject, IUnknown)',
  18956. ' procedure Go; override;',
  18957. ' end;',
  18958. 'procedure TBird.Go; begin end;',
  18959. 'procedure TCat.Go; begin end;',
  18960. 'procedure TDog.Go; begin end;',
  18961. 'begin',
  18962. '']);
  18963. ConvertProgram;
  18964. CheckSource('TestClassInterface_MethodOverride',
  18965. LinesToStr([ // statements
  18966. 'rtl.createInterface(this, "IUnknown", "{D6D98E5B-8A10-4FEC-856A-7BFC847FE74B}", ["Go"], null);',
  18967. 'rtl.createClass(this, "TObject", null, function () {',
  18968. ' this.$init = function () {',
  18969. ' };',
  18970. ' this.$final = function () {',
  18971. ' };',
  18972. ' rtl.addIntf(this, $mod.IUnknown);',
  18973. '});',
  18974. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18975. ' this.Go = function () {',
  18976. ' };',
  18977. ' rtl.addIntf(this, $mod.IUnknown);',
  18978. '});',
  18979. 'rtl.createClass(this, "TCat", this.TObject, function () {',
  18980. ' this.Go = function () {',
  18981. ' };',
  18982. ' rtl.addIntf(this, $mod.IUnknown);',
  18983. '});',
  18984. 'rtl.createClass(this, "TDog", this.TObject, function () {',
  18985. ' this.Go = function () {',
  18986. ' };',
  18987. ' rtl.addIntf(this, $mod.IUnknown);',
  18988. '});',
  18989. '']),
  18990. LinesToStr([ // $mod.$main
  18991. '']));
  18992. end;
  18993. procedure TTestModule.TestClassInterface_Corba_Delegation;
  18994. begin
  18995. StartProgram(false);
  18996. Add([
  18997. '{$interfaces corba}',
  18998. 'type',
  18999. ' IUnknown = interface',
  19000. ' end;',
  19001. ' IBird = interface(IUnknown)',
  19002. ' procedure Fly(s: string);',
  19003. ' end;',
  19004. ' IEagle = interface(IBird)',
  19005. ' end;',
  19006. ' IDove = interface(IBird)',
  19007. ' end;',
  19008. ' ISwallow = interface(IBird)',
  19009. ' end;',
  19010. ' TObject = class',
  19011. ' end;',
  19012. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  19013. ' procedure Fly(s: string); virtual; abstract;',
  19014. ' end;',
  19015. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  19016. ' FBirdIntf: IBird;',
  19017. ' property BirdIntf: IBird read FBirdIntf implements IBird;',
  19018. ' function GetEagleIntf: IEagle; virtual; abstract;',
  19019. ' property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  19020. ' FDoveObj: TBird;',
  19021. ' property DoveObj: TBird read FDoveObj implements IDove;',
  19022. ' function GetSwallowObj: TBird; virtual; abstract;',
  19023. ' property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  19024. ' end;',
  19025. 'begin',
  19026. '']);
  19027. ConvertProgram;
  19028. CheckSource('TestClassInterface_Corba_Delegation',
  19029. LinesToStr([ // statements
  19030. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19031. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  19032. 'rtl.createInterface(this, "IEagle", "{489289DE-FDE2-34A6-8288-39119022B1B4}", [], this.IBird);',
  19033. 'rtl.createInterface(this, "IDove", "{489289DE-FDE2-34A6-8288-39118EF16074}", [], this.IBird);',
  19034. 'rtl.createInterface(this, "ISwallow", "{B89289DE-FDE2-34A6-8288-3911CBDCB359}", [], this.IBird);',
  19035. 'rtl.createClass(this, "TObject", null, function () {',
  19036. ' this.$init = function () {',
  19037. ' };',
  19038. ' this.$final = function () {',
  19039. ' };',
  19040. '});',
  19041. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  19042. ' rtl.addIntf(this, $mod.IBird);',
  19043. ' rtl.addIntf(this, $mod.IEagle);',
  19044. ' rtl.addIntf(this, $mod.IDove);',
  19045. ' rtl.addIntf(this, $mod.ISwallow);',
  19046. '});',
  19047. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  19048. ' this.$init = function () {',
  19049. ' $mod.TObject.$init.call(this);',
  19050. ' this.FBirdIntf = null;',
  19051. ' this.FDoveObj = null;',
  19052. ' };',
  19053. ' this.$final = function () {',
  19054. ' this.FBirdIntf = undefined;',
  19055. ' this.FDoveObj = undefined;',
  19056. ' $mod.TObject.$final.call(this);',
  19057. ' };',
  19058. ' this.$intfmaps = {',
  19059. ' "{478D080B-C0F6-396E-AE88-000B87785B07}": function () {',
  19060. ' return this.FBirdIntf;',
  19061. ' },',
  19062. ' "{489289DE-FDE2-34A6-8288-39119022B1B4}": function () {',
  19063. ' return this.GetEagleIntf();',
  19064. ' },',
  19065. ' "{489289DE-FDE2-34A6-8288-39118EF16074}": function () {',
  19066. ' return rtl.getIntfT(this.FDoveObj, $mod.IDove);',
  19067. ' },',
  19068. ' "{B89289DE-FDE2-34A6-8288-3911CBDCB359}": function () {',
  19069. ' return rtl.getIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  19070. ' }',
  19071. ' };',
  19072. '});',
  19073. '']),
  19074. LinesToStr([ // $mod.$main
  19075. '']));
  19076. end;
  19077. procedure TTestModule.TestClassInterface_Corba_DelegationStatic;
  19078. begin
  19079. StartProgram(false);
  19080. Add([
  19081. '{$interfaces corba}',
  19082. 'type',
  19083. ' IUnknown = interface',
  19084. ' end;',
  19085. ' IBird = interface(IUnknown)',
  19086. ' procedure Fly(s: string);',
  19087. ' end;',
  19088. ' IEagle = interface(IBird)',
  19089. ' end;',
  19090. ' IDove = interface(IBird)',
  19091. ' end;',
  19092. ' ISwallow = interface(IBird)',
  19093. ' end;',
  19094. ' TObject = class',
  19095. ' end;',
  19096. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  19097. ' procedure Fly(s: string); virtual; abstract;',
  19098. ' end;',
  19099. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  19100. ' private',
  19101. ' class var FBirdIntf: IBird;',
  19102. ' class var FDoveObj: TBird;',
  19103. ' class function GetEagleIntf: IEagle; virtual; abstract;',
  19104. ' class function GetSwallowObj: TBird; virtual; abstract;',
  19105. ' protected',
  19106. ' class property BirdIntf: IBird read FBirdIntf implements IBird;',
  19107. ' class property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  19108. ' class property DoveObj: TBird read FDoveObj implements IDove;',
  19109. ' class property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  19110. ' end;',
  19111. 'begin',
  19112. '']);
  19113. ConvertProgram;
  19114. CheckSource('TestClassInterface_Corba_DelegationStatic',
  19115. LinesToStr([ // statements
  19116. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19117. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  19118. 'rtl.createInterface(this, "IEagle", "{489289DE-FDE2-34A6-8288-39119022B1B4}", [], this.IBird);',
  19119. 'rtl.createInterface(this, "IDove", "{489289DE-FDE2-34A6-8288-39118EF16074}", [], this.IBird);',
  19120. 'rtl.createInterface(this, "ISwallow", "{B89289DE-FDE2-34A6-8288-3911CBDCB359}", [], this.IBird);',
  19121. 'rtl.createClass(this, "TObject", null, function () {',
  19122. ' this.$init = function () {',
  19123. ' };',
  19124. ' this.$final = function () {',
  19125. ' };',
  19126. '});',
  19127. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  19128. ' rtl.addIntf(this, $mod.IBird);',
  19129. ' rtl.addIntf(this, $mod.IEagle);',
  19130. ' rtl.addIntf(this, $mod.IDove);',
  19131. ' rtl.addIntf(this, $mod.ISwallow);',
  19132. '});',
  19133. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  19134. ' this.FBirdIntf = null;',
  19135. ' this.FDoveObj = null;',
  19136. ' this.$intfmaps = {',
  19137. ' "{478D080B-C0F6-396E-AE88-000B87785B07}": function () {',
  19138. ' return this.FBirdIntf;',
  19139. ' },',
  19140. ' "{489289DE-FDE2-34A6-8288-39119022B1B4}": function () {',
  19141. ' return this.GetEagleIntf();',
  19142. ' },',
  19143. ' "{489289DE-FDE2-34A6-8288-39118EF16074}": function () {',
  19144. ' return rtl.getIntfT(this.FDoveObj, $mod.IDove);',
  19145. ' },',
  19146. ' "{B89289DE-FDE2-34A6-8288-3911CBDCB359}": function () {',
  19147. ' return rtl.getIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  19148. ' }',
  19149. ' };',
  19150. '});',
  19151. '']),
  19152. LinesToStr([ // $mod.$main
  19153. '']));
  19154. end;
  19155. procedure TTestModule.TestClassInterface_Corba_Operators;
  19156. begin
  19157. StartProgram(false);
  19158. Add([
  19159. '{$interfaces corba}',
  19160. 'type',
  19161. ' IUnknown = interface',
  19162. ' end;',
  19163. ' IBird = interface(IUnknown)',
  19164. ' function GetItems(Index: longint): longint;',
  19165. ' procedure SetItems(Index: longint; Value: longint);',
  19166. ' property Items[Index: longint]: longint read GetItems write SetItems; default;',
  19167. ' end;',
  19168. ' TObject = class',
  19169. ' end;',
  19170. ' TBird = class(TObject,IBird)',
  19171. ' function GetItems(Index: longint): longint; virtual; abstract;',
  19172. ' procedure SetItems(Index: longint; Value: longint); virtual; abstract;',
  19173. ' end;',
  19174. 'var',
  19175. ' IntfVar: IBird = nil;',
  19176. ' IntfVar2: IBird;',
  19177. ' ObjVar: TBird;',
  19178. ' v: JSValue;',
  19179. 'begin',
  19180. ' IntfVar:=nil;',
  19181. ' IntfVar[3]:=IntfVar[4];',
  19182. ' if Assigned(IntfVar) then ;',
  19183. ' IntfVar:=IntfVar2;',
  19184. ' IntfVar:=ObjVar;',
  19185. ' if IntfVar=IntfVar2 then ;',
  19186. ' if IntfVar<>IntfVar2 then ;',
  19187. ' if IntfVar is IBird then ;',
  19188. ' if IntfVar is TBird then ;',
  19189. ' if ObjVar is IBird then ;',
  19190. ' IntfVar:=IntfVar2 as IBird;',
  19191. ' ObjVar:=IntfVar2 as TBird;',
  19192. ' IntfVar:=ObjVar as IBird;',
  19193. ' IntfVar:=IBird(IntfVar2);',
  19194. ' ObjVar:=TBird(IntfVar);',
  19195. ' IntfVar:=IBird(ObjVar);',
  19196. ' v:=IntfVar;',
  19197. ' IntfVar:=IBird(v);',
  19198. ' if v is IBird then ;',
  19199. ' v:=JSValue(IntfVar);',
  19200. ' v:=IBird;',
  19201. '']);
  19202. ConvertProgram;
  19203. CheckSource('TestClassInterface_Corba_Operators',
  19204. LinesToStr([ // statements
  19205. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19206. 'rtl.createInterface(this, "IBird", "{D53FED90-DE59-3202-B1AE-000B87785B08}", ["GetItems", "SetItems"], this.IUnknown);',
  19207. 'rtl.createClass(this, "TObject", null, function () {',
  19208. ' this.$init = function () {',
  19209. ' };',
  19210. ' this.$final = function () {',
  19211. ' };',
  19212. '});',
  19213. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  19214. ' rtl.addIntf(this, $mod.IBird);',
  19215. '});',
  19216. 'this.IntfVar = null;',
  19217. 'this.IntfVar2 = null;',
  19218. 'this.ObjVar = null;',
  19219. 'this.v = undefined;',
  19220. '']),
  19221. LinesToStr([ // $mod.$main
  19222. '$mod.IntfVar = null;',
  19223. '$mod.IntfVar.SetItems(3, $mod.IntfVar.GetItems(4));',
  19224. 'if ($mod.IntfVar != null) ;',
  19225. '$mod.IntfVar = $mod.IntfVar2;',
  19226. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar,$mod.IBird);',
  19227. 'if ($mod.IntfVar === $mod.IntfVar2) ;',
  19228. 'if ($mod.IntfVar !== $mod.IntfVar2) ;',
  19229. 'if ($mod.IBird.isPrototypeOf($mod.IntfVar)) ;',
  19230. 'if (rtl.intfIsClass($mod.IntfVar, $mod.TBird)) ;',
  19231. 'if (rtl.getIntfT($mod.ObjVar, $mod.IBird) !== null) ;',
  19232. '$mod.IntfVar = rtl.as($mod.IntfVar2, $mod.IBird);',
  19233. '$mod.ObjVar = rtl.intfAsClass($mod.IntfVar2, $mod.TBird);',
  19234. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar, $mod.IBird);',
  19235. '$mod.IntfVar = $mod.IntfVar2;',
  19236. '$mod.ObjVar = rtl.intfToClass($mod.IntfVar, $mod.TBird);',
  19237. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar, $mod.IBird);',
  19238. '$mod.v = $mod.IntfVar;',
  19239. '$mod.IntfVar = rtl.getObject($mod.v);',
  19240. 'if (rtl.isExt($mod.v, $mod.IBird, 1)) ;',
  19241. '$mod.v = $mod.IntfVar;',
  19242. '$mod.v = $mod.IBird;',
  19243. '']));
  19244. end;
  19245. procedure TTestModule.TestClassInterface_Corba_Args;
  19246. begin
  19247. StartProgram(false);
  19248. Add([
  19249. '{$interfaces corba}',
  19250. 'type',
  19251. ' IUnknown = interface',
  19252. ' end;',
  19253. ' IBird = interface(IUnknown)',
  19254. ' end;',
  19255. ' TObject = class',
  19256. ' end;',
  19257. ' TBird = class(TObject,IBird)',
  19258. ' end;',
  19259. 'procedure DoIt(var u; i: IBird; const j: IBird);',
  19260. 'begin',
  19261. ' DoIt(i,i,i);',
  19262. 'end;',
  19263. 'procedure Change(var i: IBird; out j: IBird);',
  19264. 'begin',
  19265. ' DoIt(i,i,i);',
  19266. ' Change(i,i);',
  19267. 'end;',
  19268. 'var',
  19269. ' i: IBird;',
  19270. ' o: TBird;',
  19271. 'begin',
  19272. ' DoIt(i,i,i);',
  19273. ' Change(i,i);',
  19274. ' DoIt(o,o,o);',
  19275. '']);
  19276. ConvertProgram;
  19277. CheckSource('TestClassInterface_Corba_Args',
  19278. LinesToStr([ // statements
  19279. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19280. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], this.IUnknown);',
  19281. 'rtl.createClass(this, "TObject", null, function () {',
  19282. ' this.$init = function () {',
  19283. ' };',
  19284. ' this.$final = function () {',
  19285. ' };',
  19286. '});',
  19287. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  19288. ' rtl.addIntf(this, $mod.IBird);',
  19289. '});',
  19290. 'this.DoIt = function (u, i, j) {',
  19291. ' $mod.DoIt({',
  19292. ' get: function () {',
  19293. ' return i;',
  19294. ' },',
  19295. ' set: function (v) {',
  19296. ' i = v;',
  19297. ' }',
  19298. ' }, i, i);',
  19299. '};',
  19300. 'this.Change = function (i, j) {',
  19301. ' $mod.DoIt(i, i.get(), i.get());',
  19302. ' $mod.Change(i, i);',
  19303. '};',
  19304. 'this.i = null;',
  19305. 'this.o = null;',
  19306. '']),
  19307. LinesToStr([ // $mod.$main
  19308. '$mod.DoIt({',
  19309. ' p: $mod,',
  19310. ' get: function () {',
  19311. ' return this.p.i;',
  19312. ' },',
  19313. ' set: function (v) {',
  19314. ' this.p.i = v;',
  19315. ' }',
  19316. '}, $mod.i, $mod.i);',
  19317. '$mod.Change({',
  19318. ' p: $mod,',
  19319. ' get: function () {',
  19320. ' return this.p.i;',
  19321. ' },',
  19322. ' set: function (v) {',
  19323. ' this.p.i = v;',
  19324. ' }',
  19325. '}, {',
  19326. ' p: $mod,',
  19327. ' get: function () {',
  19328. ' return this.p.i;',
  19329. ' },',
  19330. ' set: function (v) {',
  19331. ' this.p.i = v;',
  19332. ' }',
  19333. '});',
  19334. '$mod.DoIt({',
  19335. ' p: $mod,',
  19336. ' get: function () {',
  19337. ' return this.p.o;',
  19338. ' },',
  19339. ' set: function (v) {',
  19340. ' this.p.o = v;',
  19341. ' }',
  19342. '}, rtl.getIntfT($mod.o, $mod.IBird), rtl.getIntfT($mod.o, $mod.IBird));',
  19343. '']));
  19344. end;
  19345. procedure TTestModule.TestClassInterface_Corba_ForIn;
  19346. begin
  19347. StartProgram(false);
  19348. Add([
  19349. '{$interfaces corba}',
  19350. 'type',
  19351. ' IUnknown = interface end;',
  19352. ' TObject = class',
  19353. ' Id: longint;',
  19354. ' end;',
  19355. ' IEnumerator = interface(IUnknown)',
  19356. ' function GetCurrent: TObject;',
  19357. ' function MoveNext: Boolean;',
  19358. ' property Current: TObject read GetCurrent;',
  19359. ' end;',
  19360. ' IEnumerable = interface(IUnknown)',
  19361. ' function GetEnumerator: IEnumerator;',
  19362. ' end;',
  19363. 'var',
  19364. ' o: TObject;',
  19365. ' i: IEnumerable;',
  19366. 'begin',
  19367. ' for o in i do o.Id:=3;',
  19368. '']);
  19369. ConvertProgram;
  19370. CheckSource('TestClassInterface_Corba_ForIn',
  19371. LinesToStr([ // statements
  19372. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19373. 'rtl.createClass(this, "TObject", null, function () {',
  19374. ' this.$init = function () {',
  19375. ' this.Id = 0;',
  19376. ' };',
  19377. ' this.$final = function () {',
  19378. ' };',
  19379. '});',
  19380. 'rtl.createInterface(this, "IEnumerator", "{95D7745D-ED61-3F13-BBE4-07708161999E}", ["GetCurrent", "MoveNext"], this.IUnknown);',
  19381. 'rtl.createInterface(this, "IEnumerable", "{8CC9D45D-ED7D-3B73-96B6-290B931BB19E}", ["GetEnumerator"], this.IUnknown);',
  19382. 'this.o = null;',
  19383. 'this.i = null;',
  19384. '']),
  19385. LinesToStr([ // $mod.$main
  19386. 'var $in = $mod.i.GetEnumerator();',
  19387. 'while ($in.MoveNext()) {',
  19388. ' $mod.o = $in.GetCurrent();',
  19389. ' $mod.o.Id = 3;',
  19390. '};',
  19391. '']));
  19392. end;
  19393. procedure TTestModule.TestClassInterface_COM_AssignVar;
  19394. begin
  19395. StartProgram(false);
  19396. Add([
  19397. '{$interfaces com}',
  19398. 'type',
  19399. ' IUnknown = interface',
  19400. ' function _AddRef: longint;',
  19401. ' function _Release: longint;',
  19402. ' end;',
  19403. ' TObject = class(IUnknown)',
  19404. ' function _AddRef: longint; virtual; abstract;',
  19405. ' function _Release: longint; virtual; abstract;',
  19406. ' end;',
  19407. 'var',
  19408. ' i: IUnknown;',
  19409. 'procedure DoGlobal(o: TObject);',
  19410. 'begin',
  19411. ' i:=nil;',
  19412. ' i:=o;',
  19413. ' i:=i;',
  19414. 'end;',
  19415. 'procedure DoLocal(o: TObject);',
  19416. 'const k: IUnknown = nil;',
  19417. 'var j: IUnknown;',
  19418. 'begin',
  19419. ' k:=o;',
  19420. ' k:=i;',
  19421. ' j:=o;',
  19422. ' j:=i;',
  19423. 'end;',
  19424. 'var o: TObject;',
  19425. 'begin',
  19426. ' i:=nil;',
  19427. ' i:=o;',
  19428. '']);
  19429. ConvertProgram;
  19430. CheckSource('TestClassInterface_COM_AssignVar',
  19431. LinesToStr([ // statements
  19432. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19433. 'rtl.createClass(this, "TObject", null, function () {',
  19434. ' this.$init = function () {',
  19435. ' };',
  19436. ' this.$final = function () {',
  19437. ' };',
  19438. ' rtl.addIntf(this, $mod.IUnknown);',
  19439. '});',
  19440. 'this.i = null;',
  19441. 'this.DoGlobal = function (o) {',
  19442. ' rtl.setIntfP($mod, "i", null);',
  19443. ' rtl.setIntfP($mod, "i", rtl.queryIntfT(o, $mod.IUnknown), true);',
  19444. ' rtl.setIntfP($mod, "i", $mod.i);',
  19445. '};',
  19446. 'var k = null;',
  19447. 'this.DoLocal = function (o) {',
  19448. ' var j = null;',
  19449. ' try{',
  19450. ' k = rtl.setIntfL(k, rtl.queryIntfT(o, $mod.IUnknown), true);',
  19451. ' k = rtl.setIntfL(k, $mod.i);',
  19452. ' j = rtl.setIntfL(j, rtl.queryIntfT(o, $mod.IUnknown), true);',
  19453. ' j = rtl.setIntfL(j, $mod.i);',
  19454. ' }finally{',
  19455. ' rtl._Release(j);',
  19456. ' };',
  19457. '};',
  19458. 'this.o = null;',
  19459. '']),
  19460. LinesToStr([ // $mod.$main
  19461. 'rtl.setIntfP($mod, "i", null);',
  19462. 'rtl.setIntfP($mod, "i", rtl.queryIntfT($mod.o, $mod.IUnknown), true);',
  19463. '']));
  19464. end;
  19465. procedure TTestModule.TestClassInterface_COM_AssignArg;
  19466. begin
  19467. StartProgram(false);
  19468. Add([
  19469. '{$interfaces com}',
  19470. 'type',
  19471. ' IUnknown = interface',
  19472. ' function _AddRef: longint;',
  19473. ' function _Release: longint;',
  19474. ' end;',
  19475. ' TObject = class(IUnknown)',
  19476. ' function _AddRef: longint; virtual; abstract;',
  19477. ' function _Release: longint; virtual; abstract;',
  19478. ' end;',
  19479. 'procedure DoDefault(i, j: IUnknown);',
  19480. 'begin',
  19481. ' i:=nil;',
  19482. ' i:=j;',
  19483. 'end;',
  19484. 'begin',
  19485. '']);
  19486. ConvertProgram;
  19487. CheckSource('TestClassInterface_COM_AssignArg',
  19488. LinesToStr([ // statements
  19489. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19490. 'rtl.createClass(this, "TObject", null, function () {',
  19491. ' this.$init = function () {',
  19492. ' };',
  19493. ' this.$final = function () {',
  19494. ' };',
  19495. ' rtl.addIntf(this, $mod.IUnknown);',
  19496. '});',
  19497. 'this.DoDefault = function (i, j) {',
  19498. ' rtl._AddRef(i);',
  19499. ' try {',
  19500. ' i = rtl.setIntfL(i, null);',
  19501. ' i = rtl.setIntfL(i, j);',
  19502. ' } finally {',
  19503. ' rtl._Release(i);',
  19504. ' };',
  19505. '};',
  19506. '']),
  19507. LinesToStr([ // $mod.$main
  19508. '']));
  19509. end;
  19510. procedure TTestModule.TestClassInterface_COM_FunctionResult;
  19511. begin
  19512. StartProgram(false);
  19513. Add([
  19514. '{$interfaces com}',
  19515. 'type',
  19516. ' IUnknown = interface',
  19517. ' function _AddRef: longint;',
  19518. ' function _Release: longint;',
  19519. ' end;',
  19520. ' TObject = class(IUnknown)',
  19521. ' function _AddRef: longint; virtual; abstract;',
  19522. ' function _Release: longint; virtual; abstract;',
  19523. ' end;',
  19524. 'function DoDefault(i: IUnknown): IUnknown;',
  19525. 'begin',
  19526. ' Result:=i;',
  19527. ' if Result<>nil then exit;',
  19528. 'end;',
  19529. 'begin',
  19530. '']);
  19531. ConvertProgram;
  19532. CheckSource('TestClassInterface_COM_FunctionResult',
  19533. LinesToStr([ // statements
  19534. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19535. 'rtl.createClass(this, "TObject", null, function () {',
  19536. ' this.$init = function () {',
  19537. ' };',
  19538. ' this.$final = function () {',
  19539. ' };',
  19540. ' rtl.addIntf(this, $mod.IUnknown);',
  19541. '});',
  19542. 'this.DoDefault = function (i) {',
  19543. ' var Result = null;',
  19544. ' var $ok = false;',
  19545. ' try {',
  19546. ' Result = rtl.setIntfL(Result, i);',
  19547. ' if(Result !== null){',
  19548. ' $ok = true;',
  19549. ' return Result;',
  19550. ' };',
  19551. ' $ok = true;',
  19552. ' } finally {',
  19553. ' if(!$ok) rtl._Release(Result);',
  19554. ' };',
  19555. ' return Result;',
  19556. '};',
  19557. '']),
  19558. LinesToStr([ // $mod.$main
  19559. '']));
  19560. end;
  19561. procedure TTestModule.TestClassInterface_COM_InheritedFuncResult;
  19562. begin
  19563. StartProgram(false);
  19564. Add([
  19565. '{$interfaces com}',
  19566. 'type',
  19567. ' IUnknown = interface',
  19568. ' function _AddRef: longint;',
  19569. ' function _Release: longint;',
  19570. ' end;',
  19571. ' TObject = class(IUnknown)',
  19572. ' function _AddRef: longint; virtual; abstract;',
  19573. ' function _Release: longint; virtual; abstract;',
  19574. ' function GetIntf: IUnknown; virtual;',
  19575. ' end;',
  19576. ' TMouse = class',
  19577. ' function GetIntf: IUnknown; override;',
  19578. ' end;',
  19579. 'function TObject.GetIntf: IUnknown; begin end;',
  19580. 'function TMouse.GetIntf: IUnknown;',
  19581. 'var i: IUnknown;',
  19582. 'begin',
  19583. ' inherited;',
  19584. ' inherited GetIntf;',
  19585. ' inherited GetIntf();',
  19586. ' Result:=inherited GetIntf;',
  19587. ' Result:=inherited GetIntf();',
  19588. ' i:=inherited GetIntf;',
  19589. ' i:=inherited GetIntf();',
  19590. 'end;',
  19591. 'begin',
  19592. '']);
  19593. ConvertProgram;
  19594. CheckSource('TestClassInterface_COM_InheritedFuncResult',
  19595. LinesToStr([ // statements
  19596. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19597. 'rtl.createClass(this, "TObject", null, function () {',
  19598. ' this.$init = function () {',
  19599. ' };',
  19600. ' this.$final = function () {',
  19601. ' };',
  19602. ' this.GetIntf = function () {',
  19603. ' var Result = null;',
  19604. ' return Result;',
  19605. ' };',
  19606. ' rtl.addIntf(this, $mod.IUnknown);',
  19607. '});',
  19608. 'rtl.createClass(this, "TMouse", this.TObject, function () {',
  19609. ' this.GetIntf = function () {',
  19610. ' var Result = null;',
  19611. ' var i = null;',
  19612. ' var $ir = rtl.createIntfRefs();',
  19613. ' var $ok = false;',
  19614. ' try {',
  19615. ' $ir.ref(1, $mod.TObject.GetIntf.call(this));',
  19616. ' $ir.ref(2, $mod.TObject.GetIntf.call(this));',
  19617. ' $ir.ref(3, $mod.TObject.GetIntf.call(this));',
  19618. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  19619. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  19620. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  19621. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  19622. ' $ok = true;',
  19623. ' } finally {',
  19624. ' $ir.free();',
  19625. ' rtl._Release(i);',
  19626. ' if (!$ok) rtl._Release(Result);',
  19627. ' };',
  19628. ' return Result;',
  19629. ' };',
  19630. ' rtl.addIntf(this, $mod.IUnknown);',
  19631. '});',
  19632. '']),
  19633. LinesToStr([ // $mod.$main
  19634. '']));
  19635. end;
  19636. procedure TTestModule.TestClassInterface_COM_IsAsTypeCasts;
  19637. begin
  19638. StartProgram(false);
  19639. Add([
  19640. '{$interfaces com}',
  19641. 'type',
  19642. ' IUnknown = interface',
  19643. ' function _AddRef: longint;',
  19644. ' function _Release: longint;',
  19645. ' end;',
  19646. ' TObject = class(IUnknown)',
  19647. ' function _AddRef: longint; virtual; abstract;',
  19648. ' function _Release: longint; virtual; abstract;',
  19649. ' end;',
  19650. 'procedure DoDefault(i, j: IUnknown; o: TObject);',
  19651. 'begin',
  19652. ' if i is IUnknown then ;',
  19653. ' if o is IUnknown then ;',
  19654. ' if i is TObject then ;',
  19655. ' i:=j as IUnknown;',
  19656. ' i:=o as IUnknown;',
  19657. ' o:=j as TObject;',
  19658. ' i:=IUnknown(j);',
  19659. ' i:=IUnknown(o);',
  19660. ' o:=TObject(i);',
  19661. 'end;',
  19662. 'begin',
  19663. '']);
  19664. ConvertProgram;
  19665. CheckSource('TestClassInterface_COM_IsAsTypeCasts',
  19666. LinesToStr([ // statements
  19667. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19668. 'rtl.createClass(this, "TObject", null, function () {',
  19669. ' this.$init = function () {',
  19670. ' };',
  19671. ' this.$final = function () {',
  19672. ' };',
  19673. ' rtl.addIntf(this, $mod.IUnknown);',
  19674. '});',
  19675. 'this.DoDefault = function (i, j, o) {',
  19676. ' rtl._AddRef(i);',
  19677. ' try {',
  19678. ' if (rtl.intfIsIntfT(i, $mod.IUnknown)) ;',
  19679. ' if (rtl.queryIntfIsT(o, $mod.IUnknown)) ;',
  19680. ' if (rtl.intfIsClass(i, $mod.TObject)) ;',
  19681. ' i = rtl.setIntfL(i, rtl.intfAsIntfT(j, $mod.IUnknown));',
  19682. ' i = rtl.setIntfL(i, rtl.queryIntfT(o, $mod.IUnknown), true);',
  19683. ' o = rtl.intfAsClass(j, $mod.TObject);',
  19684. ' i = rtl.setIntfL(i, j);',
  19685. ' i = rtl.setIntfL(i, rtl.queryIntfT(o, $mod.IUnknown), true);',
  19686. ' o = rtl.intfToClass(i, $mod.TObject);',
  19687. ' } finally {',
  19688. ' rtl._Release(i);',
  19689. ' };',
  19690. '};',
  19691. '']),
  19692. LinesToStr([ // $mod.$main
  19693. '']));
  19694. end;
  19695. procedure TTestModule.TestClassInterface_COM_PassAsArg;
  19696. begin
  19697. StartProgram(false);
  19698. Add([
  19699. '{$interfaces com}',
  19700. 'type',
  19701. ' IUnknown = interface',
  19702. ' function _AddRef: longint;',
  19703. ' function _Release: longint;',
  19704. ' end;',
  19705. ' TObject = class(IUnknown)',
  19706. ' function _AddRef: longint; virtual; abstract;',
  19707. ' function _Release: longint; virtual; abstract;',
  19708. ' end;',
  19709. 'procedure DoIt(v: IUnknown; const j: IUnknown; var k: IUnknown; out l: IUnknown);',
  19710. 'var o: TObject;',
  19711. 'begin',
  19712. ' DoIt(v,v,v,v);',
  19713. ' DoIt(o,o,k,k);',
  19714. 'end;',
  19715. 'procedure DoSome;',
  19716. 'var v: IUnknown;',
  19717. 'begin',
  19718. ' DoIt(v,v,v,v);',
  19719. 'end;',
  19720. 'var i: IUnknown;',
  19721. 'begin',
  19722. ' DoIt(i,i,i,i);',
  19723. '']);
  19724. ConvertProgram;
  19725. CheckSource('TestClassInterface_COM_PassAsArg',
  19726. LinesToStr([ // statements
  19727. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19728. 'rtl.createClass(this, "TObject", null, function () {',
  19729. ' this.$init = function () {',
  19730. ' };',
  19731. ' this.$final = function () {',
  19732. ' };',
  19733. ' rtl.addIntf(this, $mod.IUnknown);',
  19734. '});',
  19735. 'this.DoIt = function (v, j, k, l) {',
  19736. ' var o = null;',
  19737. ' var $ir = rtl.createIntfRefs();',
  19738. ' rtl._AddRef(v);',
  19739. ' try {',
  19740. ' $mod.DoIt(v, v, {',
  19741. ' get: function () {',
  19742. ' return v;',
  19743. ' },',
  19744. ' set: function (w) {',
  19745. ' v = rtl.setIntfL(v, w);',
  19746. ' }',
  19747. ' }, {',
  19748. ' get: function () {',
  19749. ' return v;',
  19750. ' },',
  19751. ' set: function (w) {',
  19752. ' v = rtl.setIntfL(v, w);',
  19753. ' }',
  19754. ' });',
  19755. ' $mod.DoIt($ir.ref(1, rtl.queryIntfT(o, $mod.IUnknown)), $ir.ref(2, rtl.queryIntfT(o, $mod.IUnknown)), k, k);',
  19756. ' } finally {',
  19757. ' $ir.free();',
  19758. ' rtl._Release(v);',
  19759. ' };',
  19760. '};',
  19761. 'this.DoSome = function () {',
  19762. ' var v = null;',
  19763. ' try {',
  19764. ' $mod.DoIt(v, v, {',
  19765. ' get: function () {',
  19766. ' return v;',
  19767. ' },',
  19768. ' set: function (w) {',
  19769. ' v = rtl.setIntfL(v, w);',
  19770. ' }',
  19771. ' }, {',
  19772. ' get: function () {',
  19773. ' return v;',
  19774. ' },',
  19775. ' set: function (w) {',
  19776. ' v = rtl.setIntfL(v, w);',
  19777. ' }',
  19778. ' });',
  19779. ' } finally {',
  19780. ' rtl._Release(v);',
  19781. ' };',
  19782. '};',
  19783. 'this.i = null;',
  19784. '']),
  19785. LinesToStr([ // $mod.$main
  19786. '$mod.DoIt($mod.i, $mod.i, {',
  19787. ' p: $mod,',
  19788. ' get: function () {',
  19789. ' return this.p.i;',
  19790. ' },',
  19791. ' set: function (v) {',
  19792. ' rtl.setIntfP(this.p, "i", v);',
  19793. ' }',
  19794. '}, {',
  19795. ' p: $mod,',
  19796. ' get: function () {',
  19797. ' return this.p.i;',
  19798. ' },',
  19799. ' set: function (v) {',
  19800. ' rtl.setIntfP(this.p, "i", v);',
  19801. ' }',
  19802. '});',
  19803. '']));
  19804. end;
  19805. procedure TTestModule.TestClassInterface_COM_PassToUntypedParam;
  19806. begin
  19807. StartProgram(false);
  19808. Add([
  19809. '{$interfaces com}',
  19810. 'type',
  19811. ' IUnknown = interface',
  19812. ' function _AddRef: longint;',
  19813. ' function _Release: longint;',
  19814. ' end;',
  19815. ' TObject = class(IUnknown)',
  19816. ' function _AddRef: longint; virtual; abstract;',
  19817. ' function _Release: longint; virtual; abstract;',
  19818. ' end;',
  19819. 'procedure DoIt(out i);',
  19820. 'begin end;',
  19821. 'procedure DoSome;',
  19822. 'var v: IUnknown;',
  19823. 'begin',
  19824. ' DoIt(v);',
  19825. 'end;',
  19826. 'function GetIt: IUnknown;',
  19827. 'begin',
  19828. ' DoIt(Result);',
  19829. 'end;',
  19830. 'var i: IUnknown;',
  19831. 'begin',
  19832. ' DoIt(i);',
  19833. '']);
  19834. ConvertProgram;
  19835. CheckSource('TestClassInterface_COM_PassToUntypedParam',
  19836. LinesToStr([ // statements
  19837. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19838. 'rtl.createClass(this, "TObject", null, function () {',
  19839. ' this.$init = function () {',
  19840. ' };',
  19841. ' this.$final = function () {',
  19842. ' };',
  19843. ' rtl.addIntf(this, $mod.IUnknown);',
  19844. '});',
  19845. 'this.DoIt = function (i) {',
  19846. '};',
  19847. 'this.DoSome = function () {',
  19848. ' var v = null;',
  19849. ' try {',
  19850. ' $mod.DoIt({',
  19851. ' get: function () {',
  19852. ' return v;',
  19853. ' },',
  19854. ' set: function (w) {',
  19855. ' v = w;',
  19856. ' }',
  19857. ' });',
  19858. ' } finally {',
  19859. ' rtl._Release(v);',
  19860. ' };',
  19861. '};',
  19862. 'this.GetIt = function () {',
  19863. ' var Result = null;',
  19864. ' var $ok = false;',
  19865. ' try {',
  19866. ' $mod.DoIt({',
  19867. ' get: function () {',
  19868. ' return Result;',
  19869. ' },',
  19870. ' set: function (v) {',
  19871. ' Result = v;',
  19872. ' }',
  19873. ' });',
  19874. ' $ok = true;',
  19875. ' } finally {',
  19876. ' if (!$ok) rtl._Release(Result);',
  19877. ' };',
  19878. ' return Result;',
  19879. '};',
  19880. 'this.i = null;',
  19881. '']),
  19882. LinesToStr([ // $mod.$main
  19883. 'try {',
  19884. ' $mod.DoIt({',
  19885. ' p: $mod,',
  19886. ' get: function () {',
  19887. ' return this.p.i;',
  19888. ' },',
  19889. ' set: function (v) {',
  19890. ' this.p.i = v;',
  19891. ' }',
  19892. ' });',
  19893. '} finally {',
  19894. ' rtl._Release($mod.i);',
  19895. '};',
  19896. '']));
  19897. end;
  19898. procedure TTestModule.TestClassInterface_COM_FunctionInExpr;
  19899. begin
  19900. StartProgram(false);
  19901. Add([
  19902. '{$interfaces com}',
  19903. 'type',
  19904. ' IUnknown = interface',
  19905. ' function _AddRef: longint;',
  19906. ' function _Release: longint;',
  19907. ' end;',
  19908. ' TObject = class(IUnknown)',
  19909. ' function _AddRef: longint; virtual; abstract;',
  19910. ' function _Release: longint; virtual; abstract;',
  19911. ' end;',
  19912. 'function GetIt: IUnknown;',
  19913. 'begin',
  19914. 'end;',
  19915. 'procedure DoSome;',
  19916. 'var v: IUnknown;',
  19917. ' i: longint;',
  19918. 'begin',
  19919. ' v:=GetIt;',
  19920. ' v:=GetIt();',
  19921. ' GetIt()._AddRef;',
  19922. ' i:=GetIt()._AddRef;',
  19923. 'end;',
  19924. 'var v: IUnknown;',
  19925. ' i: longint;',
  19926. 'begin',
  19927. ' v:=GetIt;',
  19928. ' v:=GetIt();',
  19929. ' GetIt()._AddRef;',
  19930. ' i:=GetIt()._AddRef;',
  19931. '']);
  19932. ConvertProgram;
  19933. CheckSource('TestClassInterface_COM_FunctionInExpr',
  19934. LinesToStr([ // statements
  19935. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19936. 'rtl.createClass(this, "TObject", null, function () {',
  19937. ' this.$init = function () {',
  19938. ' };',
  19939. ' this.$final = function () {',
  19940. ' };',
  19941. ' rtl.addIntf(this, $mod.IUnknown);',
  19942. '});',
  19943. 'this.GetIt = function () {',
  19944. ' var Result = null;',
  19945. ' return Result;',
  19946. '};',
  19947. 'this.DoSome = function () {',
  19948. ' var v = null;',
  19949. ' var i = 0;',
  19950. ' var $ir = rtl.createIntfRefs();',
  19951. ' try {',
  19952. ' v = rtl.setIntfL(v, $mod.GetIt(), true);',
  19953. ' v = rtl.setIntfL(v, $mod.GetIt(), true);',
  19954. ' $ir.ref(1, $mod.GetIt())._AddRef();',
  19955. ' i = $ir.ref(2, $mod.GetIt())._AddRef();',
  19956. ' } finally {',
  19957. ' $ir.free();',
  19958. ' rtl._Release(v);',
  19959. ' };',
  19960. '};',
  19961. 'this.v = null;',
  19962. 'this.i = 0;',
  19963. '']),
  19964. LinesToStr([ // $mod.$main
  19965. 'var $ir = rtl.createIntfRefs();',
  19966. 'try {',
  19967. ' rtl.setIntfP($mod, "v", $mod.GetIt(), true);',
  19968. ' rtl.setIntfP($mod, "v", $mod.GetIt(), true);',
  19969. ' $ir.ref(1, $mod.GetIt())._AddRef();',
  19970. ' $mod.i = $ir.ref(2, $mod.GetIt())._AddRef();',
  19971. '} finally {',
  19972. ' $ir.free();',
  19973. '};',
  19974. '']));
  19975. end;
  19976. procedure TTestModule.TestClassInterface_COM_Property;
  19977. begin
  19978. StartProgram(false);
  19979. Add([
  19980. '{$interfaces com}',
  19981. 'type',
  19982. ' IUnknown = interface',
  19983. ' function _AddRef: longint;',
  19984. ' function _Release: longint;',
  19985. ' end;',
  19986. ' TObject = class(IUnknown)',
  19987. ' FAnt: IUnknown;',
  19988. ' function _AddRef: longint; virtual; abstract;',
  19989. ' function _Release: longint; virtual; abstract;',
  19990. ' function GetBird: IUnknown; virtual; abstract;',
  19991. ' procedure SetBird(Value: IUnknown); virtual; abstract;',
  19992. ' function GetItems(Index: longint): IUnknown; virtual; abstract;',
  19993. ' procedure SetItems(Index: longint; Value: IUnknown); virtual; abstract;',
  19994. ' property Ant: IUnknown read FAnt write FAnt;',
  19995. ' property Bird: IUnknown read GetBird write SetBird;',
  19996. ' property Items[Index: longint]: IUnknown read GetItems write SetItems; default;',
  19997. ' end;',
  19998. 'procedure DoIt;',
  19999. 'var',
  20000. ' o: TObject;',
  20001. ' v: IUnknown;',
  20002. 'begin',
  20003. ' v:=o.Ant;',
  20004. ' o.Ant:=v;',
  20005. ' o.Ant:=o.Ant;',
  20006. ' v:=o.Bird;',
  20007. ' o.Bird:=v;',
  20008. ' o.Bird:=o.Bird;',
  20009. ' v:=o.Items[1];',
  20010. ' o.Items[2]:=v;',
  20011. ' o.Items[3]:=o.Items[4];',
  20012. ' v:=o[5];',
  20013. ' o[6]:=v;',
  20014. ' o[7]:=o[8];',
  20015. 'end;',
  20016. 'begin',
  20017. '']);
  20018. ConvertProgram;
  20019. CheckSource('TestClassInterface_COM_Property',
  20020. LinesToStr([ // statements
  20021. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  20022. 'rtl.createClass(this, "TObject", null, function () {',
  20023. ' this.$init = function () {',
  20024. ' this.FAnt = null;',
  20025. ' };',
  20026. ' this.$final = function () {',
  20027. ' this.FAnt = undefined;',
  20028. ' };',
  20029. ' rtl.addIntf(this, $mod.IUnknown);',
  20030. '});',
  20031. 'this.DoIt = function () {',
  20032. ' var o = null;',
  20033. ' var v = null;',
  20034. ' var $ir = rtl.createIntfRefs();',
  20035. ' try {',
  20036. ' v = rtl.setIntfL(v, o.FAnt);',
  20037. ' rtl.setIntfP(o, "FAnt", v);',
  20038. ' rtl.setIntfP(o, "FAnt", o.FAnt);',
  20039. ' v = rtl.setIntfL(v, o.GetBird(), true);',
  20040. ' o.SetBird(v);',
  20041. ' o.SetBird($ir.ref(1, o.GetBird()));',
  20042. ' v = rtl.setIntfL(v, o.GetItems(1), true);',
  20043. ' o.SetItems(2, v);',
  20044. ' o.SetItems(3, $ir.ref(2, o.GetItems(4)));',
  20045. ' v = rtl.setIntfL(v, o.GetItems(5), true);',
  20046. ' o.SetItems(6, v);',
  20047. ' o.SetItems(7, $ir.ref(3, o.GetItems(8)));',
  20048. ' } finally {',
  20049. ' $ir.free();',
  20050. ' rtl._Release(v);',
  20051. ' };',
  20052. '};',
  20053. '']),
  20054. LinesToStr([ // $mod.$main
  20055. '']));
  20056. end;
  20057. procedure TTestModule.TestClassInterface_COM_IntfProperty;
  20058. begin
  20059. StartProgram(false);
  20060. Add([
  20061. '{$interfaces com}',
  20062. 'type',
  20063. ' IUnknown = interface',
  20064. ' function _AddRef: longint;',
  20065. ' function _Release: longint;',
  20066. ' function GetBird: IUnknown;',
  20067. ' procedure SetBird(Value: IUnknown);',
  20068. ' function GetItems(Index: longint): IUnknown;',
  20069. ' procedure SetItems(Index: longint; Value: IUnknown);',
  20070. ' property Bird: IUnknown read GetBird write SetBird;',
  20071. ' property Items[Index: longint]: IUnknown read GetItems write SetItems; default;',
  20072. ' end;',
  20073. ' TObject = class(IUnknown)',
  20074. ' function _AddRef: longint; virtual; abstract;',
  20075. ' function _Release: longint; virtual; abstract;',
  20076. ' function GetBird: IUnknown; virtual; abstract;',
  20077. ' procedure SetBird(Value: IUnknown); virtual; abstract;',
  20078. ' function GetItems(Index: longint): IUnknown; virtual; abstract;',
  20079. ' procedure SetItems(Index: longint; Value: IUnknown); virtual; abstract;',
  20080. ' end;',
  20081. 'procedure DoIt;',
  20082. 'var',
  20083. ' o: TObject;',
  20084. ' v: IUnknown;',
  20085. 'begin',
  20086. ' v:=v.Items[1];',
  20087. ' v.Items[2]:=v;',
  20088. ' v.Items[3]:=v.Items[4];',
  20089. ' v:=v[5];',
  20090. ' v[6]:=v;',
  20091. ' v[7]:=v[8];',
  20092. ' v[9].Bird.Bird:=v;',
  20093. ' v:=v.Bird[10].Bird',
  20094. 'end;',
  20095. 'begin',
  20096. '']);
  20097. ConvertProgram;
  20098. CheckSource('TestClassInterface_COM_IntfProperty',
  20099. LinesToStr([ // statements
  20100. 'rtl.createInterface(this, "IUnknown", "{385F5482-571B-338C-8130-4E97F330543B}", [',
  20101. ' "_AddRef",',
  20102. ' "_Release",',
  20103. ' "GetBird",',
  20104. ' "SetBird",',
  20105. ' "GetItems",',
  20106. ' "SetItems"',
  20107. '], null);',
  20108. 'rtl.createClass(this, "TObject", null, function () {',
  20109. ' this.$init = function () {',
  20110. ' };',
  20111. ' this.$final = function () {',
  20112. ' };',
  20113. ' rtl.addIntf(this, $mod.IUnknown);',
  20114. '});',
  20115. 'this.DoIt = function () {',
  20116. ' var o = null;',
  20117. ' var v = null;',
  20118. ' var $ir = rtl.createIntfRefs();',
  20119. ' try {',
  20120. ' v = rtl.setIntfL(v, v.GetItems(1), true);',
  20121. ' v.SetItems(2, v);',
  20122. ' v.SetItems(3, $ir.ref(1, v.GetItems(4)));',
  20123. ' v = rtl.setIntfL(v, v.GetItems(5), true);',
  20124. ' v.SetItems(6, v);',
  20125. ' v.SetItems(7, $ir.ref(2, v.GetItems(8)));',
  20126. ' $ir.ref(4, $ir.ref(3, v.GetItems(9)).GetBird()).SetBird(v);',
  20127. ' v = rtl.setIntfL(v, $ir.ref(6, $ir.ref(5, v.GetBird()).GetItems(10)).GetBird(), true);',
  20128. ' } finally {',
  20129. ' $ir.free();',
  20130. ' rtl._Release(v);',
  20131. ' };',
  20132. '};',
  20133. '']),
  20134. LinesToStr([ // $mod.$main
  20135. '']));
  20136. end;
  20137. procedure TTestModule.TestClassInterface_COM_Delegation;
  20138. begin
  20139. StartProgram(false);
  20140. Add([
  20141. '{$interfaces com}',
  20142. 'type',
  20143. ' IUnknown = interface',
  20144. ' function _AddRef: longint;',
  20145. ' function _Release: longint;',
  20146. ' end;',
  20147. ' IBird = interface(IUnknown)',
  20148. ' procedure Fly(s: string);',
  20149. ' end;',
  20150. ' IEagle = interface(IBird) end;',
  20151. ' IDove = interface(IBird) end;',
  20152. ' ISwallow = interface(IBird) end;',
  20153. ' TObject = class',
  20154. ' end;',
  20155. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  20156. ' function _AddRef: longint; virtual; abstract;',
  20157. ' function _Release: longint; virtual; abstract;',
  20158. ' procedure Fly(s: string); virtual; abstract;',
  20159. ' end;',
  20160. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  20161. ' function _AddRef: longint; virtual; abstract;',
  20162. ' function _Release: longint; virtual; abstract;',
  20163. ' FBirdIntf: IBird;',
  20164. ' property BirdIntf: IBird read FBirdIntf implements IBird;',
  20165. ' function GetEagleIntf: IEagle; virtual; abstract;',
  20166. ' property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  20167. ' FDoveObj: TBird;',
  20168. ' property DoveObj: TBird read FDoveObj implements IDove;',
  20169. ' function GetSwallowObj: TBird; virtual; abstract;',
  20170. ' property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  20171. ' end;',
  20172. 'begin',
  20173. '']);
  20174. ConvertProgram;
  20175. CheckSource('TestClassInterface_COM_Delegation',
  20176. LinesToStr([ // statements
  20177. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  20178. 'rtl.createInterface(this, "IBird", "{CC440C7F-7623-3DEE-AE88-000B86AAF108}", ["Fly"], this.IUnknown);',
  20179. 'rtl.createInterface(this, "IEagle", "{4B6A41C9-B020-3D7C-B688-96D19022B1B4}", [], this.IBird);',
  20180. 'rtl.createInterface(this, "IDove", "{4B6A41C9-B020-3D7C-B688-96D18EF16074}", [], this.IBird);',
  20181. 'rtl.createInterface(this, "ISwallow", "{BB6A41C9-B020-3D7C-B688-96D1CBDCB359}", [], this.IBird);',
  20182. 'rtl.createClass(this, "TObject", null, function () {',
  20183. ' this.$init = function () {',
  20184. ' };',
  20185. ' this.$final = function () {',
  20186. ' };',
  20187. '});',
  20188. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20189. ' rtl.addIntf(this, $mod.IBird);',
  20190. ' rtl.addIntf(this, $mod.IEagle);',
  20191. ' rtl.addIntf(this, $mod.IDove);',
  20192. ' rtl.addIntf(this, $mod.ISwallow);',
  20193. '});',
  20194. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  20195. ' this.$init = function () {',
  20196. ' $mod.TObject.$init.call(this);',
  20197. ' this.FBirdIntf = null;',
  20198. ' this.FDoveObj = null;',
  20199. ' };',
  20200. ' this.$final = function () {',
  20201. ' this.FBirdIntf = undefined;',
  20202. ' this.FDoveObj = undefined;',
  20203. ' $mod.TObject.$final.call(this);',
  20204. ' };',
  20205. ' this.$intfmaps = {',
  20206. ' "{CC440C7F-7623-3DEE-AE88-000B86AAF108}": function () {',
  20207. ' return rtl._AddRef(this.FBirdIntf);',
  20208. ' },',
  20209. ' "{4B6A41C9-B020-3D7C-B688-96D19022B1B4}": function () {',
  20210. ' return this.GetEagleIntf();',
  20211. ' },',
  20212. ' "{4B6A41C9-B020-3D7C-B688-96D18EF16074}": function () {',
  20213. ' return rtl.queryIntfT(this.FDoveObj, $mod.IDove);',
  20214. ' },',
  20215. ' "{BB6A41C9-B020-3D7C-B688-96D1CBDCB359}": function () {',
  20216. ' return rtl.queryIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  20217. ' }',
  20218. ' };',
  20219. '});',
  20220. '']),
  20221. LinesToStr([ // $mod.$main
  20222. '']));
  20223. end;
  20224. procedure TTestModule.TestClassInterface_COM_With;
  20225. begin
  20226. StartProgram(false);
  20227. Add([
  20228. '{$interfaces com}',
  20229. 'type',
  20230. ' IUnknown = interface',
  20231. ' function _AddRef: longint;',
  20232. ' function _Release: longint;',
  20233. ' function GetAnt: IUnknown;',
  20234. ' property Ant: IUnknown read GetAnt;',
  20235. ' end;',
  20236. ' TObject = class(IUnknown)',
  20237. ' function _AddRef: longint; virtual; abstract;',
  20238. ' function _Release: longint; virtual; abstract;',
  20239. ' function GetAnt: IUnknown; virtual; abstract;',
  20240. ' property Ant: IUnknown read GetAnt;',
  20241. ' end;',
  20242. 'procedure DoIt;',
  20243. 'var',
  20244. ' i: IUnknown;',
  20245. 'begin',
  20246. ' with i do ',
  20247. ' GetAnt;',
  20248. ' with i.Ant, Ant do ',
  20249. ' GetAnt;',
  20250. 'end;',
  20251. 'begin',
  20252. '']);
  20253. ConvertProgram;
  20254. CheckSource('TestClassInterface_COM_With',
  20255. LinesToStr([ // statements
  20256. 'rtl.createInterface(this, "IUnknown", "{D7ADB00D-C6B6-39FB-BDDF-21CD521DDFA9}", ["_AddRef", "_Release", "GetAnt"], null);',
  20257. 'rtl.createClass(this, "TObject", null, function () {',
  20258. ' this.$init = function () {',
  20259. ' };',
  20260. ' this.$final = function () {',
  20261. ' };',
  20262. ' rtl.addIntf(this, $mod.IUnknown);',
  20263. '});',
  20264. 'this.DoIt = function () {',
  20265. ' var i = null;',
  20266. ' var $ir = rtl.createIntfRefs();',
  20267. ' try {',
  20268. ' $ir.ref(1, i.GetAnt());',
  20269. ' var $with = $ir.ref(2, i.GetAnt());',
  20270. ' var $with1 = $ir.ref(3, $with.GetAnt());',
  20271. ' $ir.ref(4, $with1.GetAnt());',
  20272. ' } finally {',
  20273. ' $ir.free();',
  20274. ' };',
  20275. '};',
  20276. '']),
  20277. LinesToStr([ // $mod.$main
  20278. '']));
  20279. end;
  20280. procedure TTestModule.TestClassInterface_COM_ForIn;
  20281. begin
  20282. StartProgram(false);
  20283. Add([
  20284. '{$interfaces com}',
  20285. 'type',
  20286. ' IUnknown = interface end;',
  20287. ' TObject = class',
  20288. ' Id: longint;',
  20289. ' end;',
  20290. ' IEnumerator = interface(IUnknown)',
  20291. ' function GetCurrent: TObject;',
  20292. ' function MoveNext: Boolean;',
  20293. ' property Current: TObject read GetCurrent;',
  20294. ' end;',
  20295. ' IEnumerable = interface(IUnknown)',
  20296. ' function GetEnumerator: IEnumerator;',
  20297. ' end;',
  20298. 'var',
  20299. ' o: TObject;',
  20300. ' i: IEnumerable;',
  20301. 'begin',
  20302. ' for o in i do o.Id:=3;',
  20303. '']);
  20304. ConvertProgram;
  20305. CheckSource('TestClassInterface_COM_ForIn',
  20306. LinesToStr([ // statements
  20307. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  20308. 'rtl.createClass(this, "TObject", null, function () {',
  20309. ' this.$init = function () {',
  20310. ' this.Id = 0;',
  20311. ' };',
  20312. ' this.$final = function () {',
  20313. ' };',
  20314. '});',
  20315. 'rtl.createInterface(this, "IEnumerator", "{95D7745D-ED61-3F13-BBE4-07708161999E}", ["GetCurrent", "MoveNext"], this.IUnknown);',
  20316. 'rtl.createInterface(this, "IEnumerable", "{8CC9D45D-ED7D-3B73-96B6-290B931BB19E}", ["GetEnumerator"], this.IUnknown);',
  20317. 'this.o = null;',
  20318. 'this.i = null;',
  20319. '']),
  20320. LinesToStr([ // $mod.$main
  20321. 'var $in = $mod.i.GetEnumerator();',
  20322. 'try {',
  20323. ' while ($in.MoveNext()) {',
  20324. ' $mod.o = $in.GetCurrent();',
  20325. ' $mod.o.Id = 3;',
  20326. ' }',
  20327. '} finally {',
  20328. ' rtl._Release($in)',
  20329. '};',
  20330. '']));
  20331. end;
  20332. procedure TTestModule.TestClassInterface_COM_ArrayOfIntfFail;
  20333. begin
  20334. StartProgram(false);
  20335. Add([
  20336. '{$interfaces com}',
  20337. 'type',
  20338. ' IUnknown = interface',
  20339. ' function _AddRef: longint;',
  20340. ' function _Release: longint;',
  20341. ' end;',
  20342. ' TObject = class',
  20343. ' end;',
  20344. ' TArrOfIntf = array of IUnknown;',
  20345. 'begin',
  20346. '']);
  20347. SetExpectedPasResolverError('Not supported: array of COM-interface',nNotSupportedX);
  20348. ConvertProgram;
  20349. end;
  20350. procedure TTestModule.TestClassInterface_COM_RecordIntfFail;
  20351. begin
  20352. StartProgram(false);
  20353. Add([
  20354. '{$interfaces com}',
  20355. 'type',
  20356. ' IUnknown = interface',
  20357. ' function _AddRef: longint;',
  20358. ' function _Release: longint;',
  20359. ' end;',
  20360. ' TRec = record',
  20361. ' i: IUnknown;',
  20362. ' end;',
  20363. 'begin',
  20364. '']);
  20365. SetExpectedPasResolverError('Not supported: COM-interface as record member',nNotSupportedX);
  20366. ConvertProgram;
  20367. end;
  20368. procedure TTestModule.TestClassInterface_COM_UnitInitialization;
  20369. begin
  20370. StartUnit(false);
  20371. Add([
  20372. '{$interfaces com}',
  20373. 'interface',
  20374. 'implementation',
  20375. 'type',
  20376. ' IUnknown = interface',
  20377. ' function _AddRef: longint;',
  20378. ' end;',
  20379. ' TObject = class(IUnknown)',
  20380. ' function _AddRef: longint;',
  20381. ' end;',
  20382. 'function TObject._AddRef: longint; begin end;',
  20383. 'var i: IUnknown;',
  20384. ' o: TObject;',
  20385. 'initialization',
  20386. ' i:=nil;',
  20387. ' i:=i;',
  20388. ' i:=o;',
  20389. ' if (o as IUnknown)=nil then ;',
  20390. '']);
  20391. ConvertUnit;
  20392. CheckSource('TestClassInterface_COM_UnitInitialization',
  20393. LinesToStr([ // statements
  20394. 'var $impl = $mod.$impl;',
  20395. '']),
  20396. LinesToStr([ // this.$init
  20397. 'var $ir = rtl.createIntfRefs();',
  20398. 'try {',
  20399. ' rtl.setIntfP($impl, "i", null);',
  20400. ' rtl.setIntfP($impl, "i", $impl.i);',
  20401. ' rtl.setIntfP($impl, "i", rtl.queryIntfT($impl.o, $impl.IUnknown), true);',
  20402. ' if ($ir.ref(1, rtl.queryIntfT($impl.o, $impl.IUnknown)) === null) ;',
  20403. '} finally {',
  20404. ' $ir.free();',
  20405. '};',
  20406. '']),
  20407. LinesToStr([ // implementation
  20408. 'rtl.createInterface($impl, "IUnknown", "{B92D5841-758A-322B-BDDF-21CD52180000}", ["_AddRef"], null);',
  20409. 'rtl.createClass($impl, "TObject", null, function () {',
  20410. ' this.$init = function () {',
  20411. ' };',
  20412. ' this.$final = function () {',
  20413. ' };',
  20414. ' this._AddRef = function () {',
  20415. ' var Result = 0;',
  20416. ' return Result;',
  20417. ' };',
  20418. ' rtl.addIntf(this, $impl.IUnknown);',
  20419. '});',
  20420. '$impl.i = null;',
  20421. '$impl.o = null;',
  20422. ''])
  20423. );
  20424. end;
  20425. procedure TTestModule.TestClassInterface_GUID;
  20426. begin
  20427. StartProgram(false);
  20428. Add([
  20429. '{$interfaces corba}',
  20430. 'type',
  20431. ' IUnknown = interface',
  20432. ' [''{f31db68f-3010-D355-4EBA-CDD4EF4A737C}'']',
  20433. ' end;',
  20434. ' TObject = class end;',
  20435. ' TGUID = record D1, D2, D3, D4: word; end;',
  20436. ' TAliasGUID = TGUID;',
  20437. ' TGUIDString = type string;',
  20438. ' TAliasGUIDString = TGUIDString;',
  20439. 'procedure DoConstGUIDIt(const g: TAliasGUID); overload;',
  20440. 'begin end;',
  20441. 'procedure DoDefGUID(g: TAliasGUID); overload;',
  20442. 'begin end;',
  20443. 'procedure DoStr(const s: TAliasGUIDString); overload;',
  20444. 'begin end;',
  20445. 'var',
  20446. ' i: IUnknown;',
  20447. ' g: TAliasGUID = ''{d91c9af4-3C93-420F-A303-BF5BA82BFD23}'';',
  20448. ' s: TAliasGUIDString;',
  20449. 'begin',
  20450. ' DoConstGUIDIt(IUnknown);',
  20451. ' DoDefGUID(IUnknown);',
  20452. ' DoStr(IUnknown);',
  20453. ' DoConstGUIDIt(i);',
  20454. ' DoDefGUID(i);',
  20455. ' DoStr(i);',
  20456. ' DoConstGUIDIt(''{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}'');',
  20457. ' DoDefGUID(''{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}'');',
  20458. ' DoStr(g);',
  20459. ' g:=i;',
  20460. ' g:=IUnknown;',
  20461. ' g:=''{D91C9AF4-3C93-420F-A303-bf5ba82bfd23}'';',
  20462. ' s:=i;',
  20463. ' s:=IUnknown;',
  20464. ' s:=g;',
  20465. ' if g=i then ;',
  20466. ' if i=g then ;',
  20467. ' if g=IUnknown then ;',
  20468. ' if IUnknown=g then ;',
  20469. ' if s=i then ;',
  20470. ' if i=s then ;',
  20471. ' if s=IUnknown then ;',
  20472. ' if IUnknown=s then ;',
  20473. ' if s=g then ;',
  20474. ' if g=s then ;',
  20475. '']);
  20476. ConvertProgram;
  20477. CheckSource('TestClassInterface_GUID',
  20478. LinesToStr([ // statements
  20479. 'rtl.createInterface(this, "IUnknown", "{F31DB68F-3010-D355-4EBA-CDD4EF4A737C}", [], null);',
  20480. 'rtl.createClass(this, "TObject", null, function () {',
  20481. ' this.$init = function () {',
  20482. ' };',
  20483. ' this.$final = function () {',
  20484. ' };',
  20485. '});',
  20486. 'rtl.recNewT(this, "TGUID", function () {',
  20487. ' this.D1 = 0;',
  20488. ' this.D2 = 0;',
  20489. ' this.D3 = 0;',
  20490. ' this.D4 = 0;',
  20491. ' this.$eq = function (b) {',
  20492. ' return (this.D1 === b.D1) && (this.D2 === b.D2) && (this.D3 === b.D3) && (this.D4 === b.D4);',
  20493. ' };',
  20494. ' this.$assign = function (s) {',
  20495. ' this.D1 = s.D1;',
  20496. ' this.D2 = s.D2;',
  20497. ' this.D3 = s.D3;',
  20498. ' this.D4 = s.D4;',
  20499. ' return this;',
  20500. ' };',
  20501. '});',
  20502. 'this.DoConstGUIDIt = function (g) {',
  20503. '};',
  20504. 'this.DoDefGUID = function (g) {',
  20505. '};',
  20506. 'this.DoStr = function (s) {',
  20507. '};',
  20508. 'this.i = null;',
  20509. 'this.g = this.TGUID.$clone({',
  20510. ' D1: 0xD91C9AF4,',
  20511. ' D2: 0x3C93,',
  20512. ' D3: 0x420F,',
  20513. ' D4: [',
  20514. ' 0xA3,',
  20515. ' 0x03,',
  20516. ' 0xBF,',
  20517. ' 0x5B,',
  20518. ' 0xA8,',
  20519. ' 0x2B,',
  20520. ' 0xFD,',
  20521. ' 0x23',
  20522. ' ]',
  20523. '});',
  20524. 'this.s = "";',
  20525. '']),
  20526. LinesToStr([ // $mod.$main
  20527. '$mod.DoConstGUIDIt(rtl.getIntfGUIDR($mod.IUnknown));',
  20528. '$mod.DoDefGUID($mod.TGUID.$clone(rtl.getIntfGUIDR($mod.IUnknown)));',
  20529. '$mod.DoStr($mod.IUnknown.$guid);',
  20530. '$mod.DoConstGUIDIt(rtl.getIntfGUIDR($mod.i));',
  20531. '$mod.DoDefGUID($mod.TGUID.$clone(rtl.getIntfGUIDR($mod.i)));',
  20532. '$mod.DoStr($mod.i.$guid);',
  20533. '$mod.DoConstGUIDIt(rtl.strToGUIDR("{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}"));',
  20534. '$mod.DoDefGUID(rtl.strToGUIDR("{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}"));',
  20535. '$mod.DoStr(rtl.guidrToStr($mod.g));',
  20536. '$mod.g.$assign(rtl.getIntfGUIDR($mod.i));',
  20537. '$mod.g.$assign(rtl.getIntfGUIDR($mod.IUnknown));',
  20538. '$mod.g.$assign({',
  20539. ' D1: 0xD91C9AF4,',
  20540. ' D2: 0x3C93,',
  20541. ' D3: 0x420F,',
  20542. ' D4: [',
  20543. ' 0xA3,',
  20544. ' 0x03,',
  20545. ' 0xBF,',
  20546. ' 0x5B,',
  20547. ' 0xA8,',
  20548. ' 0x2B,',
  20549. ' 0xFD,',
  20550. ' 0x23',
  20551. ' ]',
  20552. '});',
  20553. '$mod.s = $mod.i.$guid;',
  20554. '$mod.s = $mod.IUnknown.$guid;',
  20555. '$mod.s = rtl.guidrToStr($mod.g);',
  20556. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.i))) ;',
  20557. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.i))) ;',
  20558. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.IUnknown))) ;',
  20559. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.IUnknown))) ;',
  20560. 'if ($mod.s === $mod.i.$guid) ;',
  20561. 'if ($mod.i.$guid === $mod.s) ;',
  20562. 'if ($mod.s === $mod.IUnknown.$guid) ;',
  20563. 'if ($mod.IUnknown.$guid === $mod.s) ;',
  20564. 'if ($mod.g.$eq(rtl.createTGUID($mod.s))) ;',
  20565. 'if ($mod.g.$eq(rtl.createTGUID($mod.s))) ;',
  20566. '']));
  20567. end;
  20568. procedure TTestModule.TestClassInterface_GUIDProperty;
  20569. begin
  20570. StartProgram(false);
  20571. Add([
  20572. '{$interfaces corba}',
  20573. 'type',
  20574. ' IUnknown = interface',
  20575. ' [''{f31db68f-3010-D355-4EBA-CDD4EF4A737C}'']',
  20576. ' end;',
  20577. ' TGUID = record D1, D2, D3, D4: word; end;',
  20578. ' TAliasGUID = TGUID;',
  20579. ' TGUIDString = type string;',
  20580. ' TAliasGUIDString = TGUIDString;',
  20581. ' TObject = class',
  20582. ' function GetG: TAliasGUID; virtual; abstract;',
  20583. ' procedure SetG(const Value: TAliasGUID); virtual; abstract;',
  20584. ' function GetS: TAliasGUIDString; virtual; abstract;',
  20585. ' procedure SetS(const Value: TAliasGUIDString); virtual; abstract;',
  20586. ' property g: TAliasGUID read GetG write SetG;',
  20587. ' property s: TAliasGUIDString read GetS write SetS;',
  20588. ' end;',
  20589. 'var o: TObject;',
  20590. 'begin',
  20591. ' o.g:=IUnknown;',
  20592. ' o.g:=''{D91C9AF4-3C93-420F-A303-bf5ba82bfd23}'';',
  20593. ' o.s:=IUnknown;',
  20594. ' o.s:=o.g;',
  20595. '']);
  20596. ConvertProgram;
  20597. CheckSource('TestClassInterface_GUIDProperty',
  20598. LinesToStr([ // statements
  20599. 'rtl.createInterface(this, "IUnknown", "{F31DB68F-3010-D355-4EBA-CDD4EF4A737C}", [], null);',
  20600. 'rtl.recNewT(this, "TGUID", function () {',
  20601. ' this.D1 = 0;',
  20602. ' this.D2 = 0;',
  20603. ' this.D3 = 0;',
  20604. ' this.D4 = 0;',
  20605. ' this.$eq = function (b) {',
  20606. ' return (this.D1 === b.D1) && (this.D2 === b.D2) && (this.D3 === b.D3) && (this.D4 === b.D4);',
  20607. ' };',
  20608. ' this.$assign = function (s) {',
  20609. ' this.D1 = s.D1;',
  20610. ' this.D2 = s.D2;',
  20611. ' this.D3 = s.D3;',
  20612. ' this.D4 = s.D4;',
  20613. ' return this;',
  20614. ' };',
  20615. '});',
  20616. 'rtl.createClass(this, "TObject", null, function () {',
  20617. ' this.$init = function () {',
  20618. ' };',
  20619. ' this.$final = function () {',
  20620. ' };',
  20621. '});',
  20622. 'this.o = null;',
  20623. '']),
  20624. LinesToStr([ // $mod.$main
  20625. '$mod.o.SetG(rtl.getIntfGUIDR($mod.IUnknown));',
  20626. '$mod.o.SetG({',
  20627. ' D1: 0xD91C9AF4,',
  20628. ' D2: 0x3C93,',
  20629. ' D3: 0x420F,',
  20630. ' D4: [',
  20631. ' 0xA3,',
  20632. ' 0x03,',
  20633. ' 0xBF,',
  20634. ' 0x5B,',
  20635. ' 0xA8,',
  20636. ' 0x2B,',
  20637. ' 0xFD,',
  20638. ' 0x23',
  20639. ' ]',
  20640. '});',
  20641. '$mod.o.SetS($mod.IUnknown.$guid);',
  20642. '$mod.o.SetS(rtl.guidrToStr($mod.o.GetG()));',
  20643. '']));
  20644. end;
  20645. procedure TTestModule.TestClassHelper_ClassVar;
  20646. begin
  20647. StartProgram(false);
  20648. Add([
  20649. 'type',
  20650. ' TObject = class',
  20651. ' end;',
  20652. ' THelper = class helper for TObject',
  20653. ' const',
  20654. ' One = 1;',
  20655. ' Two: word = 2;',
  20656. ' class var',
  20657. ' Glob: word;',
  20658. ' function Foo(w: word): word;',
  20659. ' class function Bar(w: word): word;',
  20660. ' end;',
  20661. 'function THelper.foo(w: word): word;',
  20662. 'begin',
  20663. ' Result:=w;',
  20664. ' Two:=One+w;',
  20665. ' Glob:=Glob;',
  20666. ' Result:=Self.Glob;',
  20667. ' Self.Glob:=Self.Glob;',
  20668. ' with Self do Glob:=Glob;',
  20669. 'end;',
  20670. 'class function THelper.bar(w: word): word;',
  20671. 'begin',
  20672. ' Result:=w;',
  20673. ' Two:=One;',
  20674. ' Glob:=Glob;',
  20675. ' Self.Glob:=Self.Glob;',
  20676. ' with Self do Glob:=Glob;',
  20677. 'end;',
  20678. 'var o: TObject;',
  20679. 'begin',
  20680. ' tobject.two:=tobject.one;',
  20681. ' tobject.Glob:=tobject.Glob;',
  20682. ' with tobject do begin',
  20683. ' two:=one;',
  20684. ' Glob:=Glob;',
  20685. ' end;',
  20686. ' o.two:=o.one;',
  20687. ' o.Glob:=o.Glob;',
  20688. ' with o do begin',
  20689. ' two:=one;',
  20690. ' Glob:=Glob;',
  20691. ' end;',
  20692. '']);
  20693. ConvertProgram;
  20694. CheckSource('TestClassHelper_ClassVar',
  20695. LinesToStr([ // statements
  20696. 'rtl.createClass(this, "TObject", null, function () {',
  20697. ' this.$init = function () {',
  20698. ' };',
  20699. ' this.$final = function () {',
  20700. ' };',
  20701. '});',
  20702. 'rtl.createHelper(this, "THelper", null, function () {',
  20703. ' this.One = 1;',
  20704. ' this.Two = 2;',
  20705. ' this.Glob = 0;',
  20706. ' this.Foo = function (w) {',
  20707. ' var Result = 0;',
  20708. ' Result = w;',
  20709. ' $mod.THelper.Two = 1 + w;',
  20710. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20711. ' Result = $mod.THelper.Glob;',
  20712. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20713. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20714. ' return Result;',
  20715. ' };',
  20716. ' this.Bar = function (w) {',
  20717. ' var Result = 0;',
  20718. ' Result = w;',
  20719. ' $mod.THelper.Two = 1;',
  20720. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20721. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20722. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20723. ' return Result;',
  20724. ' };',
  20725. '});',
  20726. 'this.o = null;',
  20727. '']),
  20728. LinesToStr([ // $mod.$main
  20729. '$mod.THelper.Two = 1;',
  20730. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20731. 'var $with = $mod.TObject;',
  20732. '$mod.THelper.Two = 1;',
  20733. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20734. '$mod.THelper.Two = 1;',
  20735. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20736. 'var $with1 = $mod.o;',
  20737. '$mod.THelper.Two = 1;',
  20738. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20739. '']));
  20740. end;
  20741. procedure TTestModule.TestClassHelper_Method_AccessInstanceFields;
  20742. begin
  20743. StartProgram(false);
  20744. Add([
  20745. 'type',
  20746. ' TObject = class',
  20747. ' FSize: word;',
  20748. ' property Size: word read FSize write FSize;',
  20749. ' end;',
  20750. ' THelper = class helper for TObject',
  20751. ' function Foo(w: word = 1): word;',
  20752. ' end;',
  20753. 'function THelper.foo(w: word): word;',
  20754. 'begin',
  20755. ' Result:=Size;',
  20756. ' Size:=Size+2;',
  20757. ' Self.Size:=Self.Size+3;',
  20758. ' FSize:=FSize+4;',
  20759. ' Self.FSize:=Self.FSize+5;',
  20760. ' with Self do begin',
  20761. ' Size:=Size+6;',
  20762. ' FSize:=FSize+7;',
  20763. ' FSize:=FSize+8;',
  20764. ' end;',
  20765. 'end;',
  20766. 'begin',
  20767. '']);
  20768. ConvertProgram;
  20769. CheckSource('TestClassHelper_Method_AccessInstanceFields',
  20770. LinesToStr([ // statements
  20771. 'rtl.createClass(this, "TObject", null, function () {',
  20772. ' this.$init = function () {',
  20773. ' this.FSize = 0;',
  20774. ' };',
  20775. ' this.$final = function () {',
  20776. ' };',
  20777. '});',
  20778. 'rtl.createHelper(this, "THelper", null, function () {',
  20779. ' this.Foo = function (w) {',
  20780. ' var Result = 0;',
  20781. ' Result = this.FSize;',
  20782. ' this.FSize = this.FSize + 2;',
  20783. ' this.FSize = this.FSize + 3;',
  20784. ' this.FSize = this.FSize + 4;',
  20785. ' this.FSize = this.FSize + 5;',
  20786. ' this.FSize = this.FSize + 6;',
  20787. ' this.FSize = this.FSize + 7;',
  20788. ' this.FSize = this.FSize + 8;',
  20789. ' return Result;',
  20790. ' };',
  20791. '});',
  20792. '']),
  20793. LinesToStr([ // $mod.$main
  20794. '']));
  20795. end;
  20796. procedure TTestModule.TestClassHelper_Method_Call;
  20797. begin
  20798. StartProgram(false);
  20799. Add([
  20800. 'type',
  20801. ' TObject = class',
  20802. ' procedure Run(w: word = 10);',
  20803. ' end;',
  20804. ' THelper = class helper for TObject',
  20805. ' function Foo(w: word = 1): word;',
  20806. ' end;',
  20807. 'procedure TObject.Run(w: word);',
  20808. 'var o: TObject;',
  20809. 'begin',
  20810. ' Foo;',
  20811. ' Foo();',
  20812. ' Foo(2);',
  20813. ' Self.Foo;',
  20814. ' Self.Foo();',
  20815. ' Self.Foo(3);',
  20816. ' with Self do begin',
  20817. ' Foo;',
  20818. ' Foo();',
  20819. ' Foo(4);',
  20820. ' end;',
  20821. ' with o do Foo(5);',
  20822. 'end;',
  20823. 'function THelper.foo(w: word): word;',
  20824. 'begin',
  20825. ' Run;',
  20826. ' Run();',
  20827. ' Run(11);',
  20828. ' Foo;',
  20829. ' Foo();',
  20830. ' Foo(12);',
  20831. ' Self.Foo;',
  20832. ' Self.Foo();',
  20833. ' Self.Foo(13);',
  20834. ' with Self do begin',
  20835. ' Foo;',
  20836. ' Foo();',
  20837. ' Foo(14);',
  20838. ' end;',
  20839. 'end;',
  20840. 'var Obj: TObject;',
  20841. 'begin',
  20842. ' obj.Foo;',
  20843. ' obj.Foo();',
  20844. ' obj.Foo(21);',
  20845. ' with obj do begin',
  20846. ' Foo;',
  20847. ' Foo();',
  20848. ' Foo(22);',
  20849. ' end;',
  20850. '']);
  20851. ConvertProgram;
  20852. CheckSource('TestClassHelper_Method_Call',
  20853. LinesToStr([ // statements
  20854. 'rtl.createClass(this, "TObject", null, function () {',
  20855. ' this.$init = function () {',
  20856. ' };',
  20857. ' this.$final = function () {',
  20858. ' };',
  20859. ' this.Run = function (w) {',
  20860. ' var o = null;',
  20861. ' $mod.THelper.Foo.call(this, 1);',
  20862. ' $mod.THelper.Foo.call(this, 1);',
  20863. ' $mod.THelper.Foo.call(this, 2);',
  20864. ' $mod.THelper.Foo.call(this, 1);',
  20865. ' $mod.THelper.Foo.call(this, 1);',
  20866. ' $mod.THelper.Foo.call(this, 3);',
  20867. ' $mod.THelper.Foo.call(this, 1);',
  20868. ' $mod.THelper.Foo.call(this, 1);',
  20869. ' $mod.THelper.Foo.call(this, 4);',
  20870. ' $mod.THelper.Foo.call(o, 5);',
  20871. ' };',
  20872. '});',
  20873. 'rtl.createHelper(this, "THelper", null, function () {',
  20874. ' this.Foo = function (w) {',
  20875. ' var Result = 0;',
  20876. ' this.Run(10);',
  20877. ' this.Run(10);',
  20878. ' this.Run(11);',
  20879. ' $mod.THelper.Foo.call(this, 1);',
  20880. ' $mod.THelper.Foo.call(this, 1);',
  20881. ' $mod.THelper.Foo.call(this, 12);',
  20882. ' $mod.THelper.Foo.call(this, 1);',
  20883. ' $mod.THelper.Foo.call(this, 1);',
  20884. ' $mod.THelper.Foo.call(this, 13);',
  20885. ' $mod.THelper.Foo.call(this, 1);',
  20886. ' $mod.THelper.Foo.call(this, 1);',
  20887. ' $mod.THelper.Foo.call(this, 14);',
  20888. ' return Result;',
  20889. ' };',
  20890. '});',
  20891. 'this.Obj = null;',
  20892. '']),
  20893. LinesToStr([ // $mod.$main
  20894. '$mod.THelper.Foo.call($mod.Obj, 1);',
  20895. '$mod.THelper.Foo.call($mod.Obj, 1);',
  20896. '$mod.THelper.Foo.call($mod.Obj, 21);',
  20897. 'var $with = $mod.Obj;',
  20898. '$mod.THelper.Foo.call($with, 1);',
  20899. '$mod.THelper.Foo.call($with, 1);',
  20900. '$mod.THelper.Foo.call($with, 22);',
  20901. '']));
  20902. end;
  20903. procedure TTestModule.TestClassHelper_Method_Nested_Call;
  20904. begin
  20905. StartProgram(false);
  20906. Add([
  20907. 'type',
  20908. ' TObject = class',
  20909. ' procedure Run(w: word = 10);',
  20910. ' end;',
  20911. ' THelper = class helper for TObject',
  20912. ' function Foo(w: word = 1): word;',
  20913. ' end;',
  20914. 'procedure TObject.Run(w: word);',
  20915. ' procedure Sub(Self: TObject);',
  20916. ' begin',
  20917. ' Foo;',
  20918. ' Foo();',
  20919. ' Self.Foo;',
  20920. ' Self.Foo();',
  20921. ' with Self do begin',
  20922. ' Foo;',
  20923. ' Foo();',
  20924. ' end;',
  20925. ' end;',
  20926. 'begin',
  20927. 'end;',
  20928. 'function THelper.foo(w: word): word;',
  20929. ' procedure Sub(Self: TObject);',
  20930. ' begin',
  20931. ' Run;',
  20932. ' Run();',
  20933. ' Foo;',
  20934. ' Foo();',
  20935. ' Self.Foo;',
  20936. ' Self.Foo();',
  20937. ' with Self do begin',
  20938. ' Foo;',
  20939. ' Foo();',
  20940. ' end;',
  20941. ' end;',
  20942. 'begin',
  20943. 'end;',
  20944. 'begin',
  20945. '']);
  20946. ConvertProgram;
  20947. CheckSource('TestClassHelper_Method_Nested_Call',
  20948. LinesToStr([ // statements
  20949. 'rtl.createClass(this, "TObject", null, function () {',
  20950. ' this.$init = function () {',
  20951. ' };',
  20952. ' this.$final = function () {',
  20953. ' };',
  20954. ' this.Run = function (w) {',
  20955. ' var $Self = this;',
  20956. ' function Sub(Self) {',
  20957. ' $mod.THelper.Foo.call($Self, 1);',
  20958. ' $mod.THelper.Foo.call($Self, 1);',
  20959. ' $mod.THelper.Foo.call(Self, 1);',
  20960. ' $mod.THelper.Foo.call(Self, 1);',
  20961. ' $mod.THelper.Foo.call(Self, 1);',
  20962. ' $mod.THelper.Foo.call(Self, 1);',
  20963. ' };',
  20964. ' };',
  20965. '});',
  20966. 'rtl.createHelper(this, "THelper", null, function () {',
  20967. ' this.Foo = function (w) {',
  20968. ' var $Self = this;',
  20969. ' var Result = 0;',
  20970. ' function Sub(Self) {',
  20971. ' $Self.Run(10);',
  20972. ' $Self.Run(10);',
  20973. ' $mod.THelper.Foo.call($Self, 1);',
  20974. ' $mod.THelper.Foo.call($Self, 1);',
  20975. ' $mod.THelper.Foo.call(Self, 1);',
  20976. ' $mod.THelper.Foo.call(Self, 1);',
  20977. ' $mod.THelper.Foo.call(Self, 1);',
  20978. ' $mod.THelper.Foo.call(Self, 1);',
  20979. ' };',
  20980. ' return Result;',
  20981. ' };',
  20982. '});',
  20983. '']),
  20984. LinesToStr([ // $mod.$main
  20985. '']));
  20986. end;
  20987. procedure TTestModule.TestClassHelper_ClassMethod_Call;
  20988. begin
  20989. StartProgram(false);
  20990. Add([
  20991. 'type',
  20992. ' TObject = class',
  20993. ' class procedure Run(w: word = 10);',
  20994. ' end;',
  20995. ' THelper = class helper for TObject',
  20996. ' class function Foo(w: word = 1): word;',
  20997. ' end;',
  20998. 'class procedure TObject.Run(w: word);',
  20999. 'begin',
  21000. ' Foo;',
  21001. ' Foo();',
  21002. ' Self.Foo;',
  21003. ' Self.Foo();',
  21004. ' with Self do begin',
  21005. ' Foo;',
  21006. ' Foo();',
  21007. ' end;',
  21008. 'end;',
  21009. 'class function THelper.foo(w: word): word;',
  21010. 'begin',
  21011. ' Run;',
  21012. ' Run();',
  21013. ' Foo;',
  21014. ' Foo();',
  21015. ' Self.Foo;',
  21016. ' Self.Foo();',
  21017. ' with Self do begin',
  21018. ' Foo;',
  21019. ' Foo();',
  21020. ' end;',
  21021. 'end;',
  21022. 'var',
  21023. ' Obj: TObject;',
  21024. 'begin',
  21025. ' obj.Foo;',
  21026. ' obj.Foo();',
  21027. ' with obj do begin',
  21028. ' Foo;',
  21029. ' Foo();',
  21030. ' end;',
  21031. ' tobject.Foo;',
  21032. ' tobject.Foo();',
  21033. ' with tobject do begin',
  21034. ' Foo;',
  21035. ' Foo();',
  21036. ' end;',
  21037. '']);
  21038. ConvertProgram;
  21039. CheckSource('TestClassHelper_ClassMethod_Call',
  21040. LinesToStr([ // statements
  21041. 'rtl.createClass(this, "TObject", null, function () {',
  21042. ' this.$init = function () {',
  21043. ' };',
  21044. ' this.$final = function () {',
  21045. ' };',
  21046. ' this.Run = function (w) {',
  21047. ' $mod.THelper.Foo.call(this, 1);',
  21048. ' $mod.THelper.Foo.call(this, 1);',
  21049. ' $mod.THelper.Foo.call(this, 1);',
  21050. ' $mod.THelper.Foo.call(this, 1);',
  21051. ' $mod.THelper.Foo.call(this, 1);',
  21052. ' $mod.THelper.Foo.call(this, 1);',
  21053. ' };',
  21054. '});',
  21055. 'rtl.createHelper(this, "THelper", null, function () {',
  21056. ' this.Foo = function (w) {',
  21057. ' var Result = 0;',
  21058. ' this.Run(10);',
  21059. ' this.Run(10);',
  21060. ' $mod.THelper.Foo.call(this, 1);',
  21061. ' $mod.THelper.Foo.call(this, 1);',
  21062. ' $mod.THelper.Foo.call(this, 1);',
  21063. ' $mod.THelper.Foo.call(this, 1);',
  21064. ' $mod.THelper.Foo.call(this, 1);',
  21065. ' $mod.THelper.Foo.call(this, 1);',
  21066. ' return Result;',
  21067. ' };',
  21068. '});',
  21069. 'this.Obj = null;',
  21070. '']),
  21071. LinesToStr([ // $mod.$main
  21072. '$mod.THelper.Foo.call($mod.Obj.$class, 1);',
  21073. '$mod.THelper.Foo.call($mod.Obj.$class, 1);',
  21074. 'var $with = $mod.Obj;',
  21075. '$mod.THelper.Foo.call($with.$class, 1);',
  21076. '$mod.THelper.Foo.call($with.$class, 1);',
  21077. '$mod.THelper.Foo.call($mod.TObject, 1);',
  21078. '$mod.THelper.Foo.call($mod.TObject, 1);',
  21079. 'var $with1 = $mod.TObject;',
  21080. '$mod.THelper.Foo.call($mod.TObject, 1);',
  21081. '$mod.THelper.Foo.call($mod.TObject, 1);',
  21082. '']));
  21083. end;
  21084. procedure TTestModule.TestClassHelper_ClassOf;
  21085. begin
  21086. StartProgram(false);
  21087. Add([
  21088. 'type',
  21089. ' TObject = class',
  21090. ' end;',
  21091. ' TClass = class of TObject;',
  21092. ' THelper = class helper for TObject',
  21093. ' class function Foo(w: word = 1): word;',
  21094. ' end;',
  21095. 'class function THelper.foo(w: word): word;',
  21096. 'begin',
  21097. 'end;',
  21098. 'var',
  21099. ' c: TClass;',
  21100. 'begin',
  21101. ' c.Foo;',
  21102. ' c.Foo();',
  21103. ' with c do begin',
  21104. ' Foo;',
  21105. ' Foo();',
  21106. ' end;',
  21107. '']);
  21108. ConvertProgram;
  21109. CheckSource('TestClassHelper_ClassOf',
  21110. LinesToStr([ // statements
  21111. 'rtl.createClass(this, "TObject", null, function () {',
  21112. ' this.$init = function () {',
  21113. ' };',
  21114. ' this.$final = function () {',
  21115. ' };',
  21116. '});',
  21117. 'rtl.createHelper(this, "THelper", null, function () {',
  21118. ' this.Foo = function (w) {',
  21119. ' var Result = 0;',
  21120. ' return Result;',
  21121. ' };',
  21122. '});',
  21123. 'this.c = null;',
  21124. '']),
  21125. LinesToStr([ // $mod.$main
  21126. '$mod.THelper.Foo.call($mod.c, 1);',
  21127. '$mod.THelper.Foo.call($mod.c, 1);',
  21128. 'var $with = $mod.c;',
  21129. '$mod.THelper.Foo.call($with, 1);',
  21130. '$mod.THelper.Foo.call($with, 1);',
  21131. '']));
  21132. end;
  21133. procedure TTestModule.TestClassHelper_MethodRefObjFPC;
  21134. begin
  21135. StartProgram(false);
  21136. Add([
  21137. '{$mode objfpc}',
  21138. 'type',
  21139. ' TObject = class',
  21140. ' procedure DoIt;',
  21141. ' end;',
  21142. ' THelper = class helper for TObject',
  21143. ' procedure Fly(w: word = 1);',
  21144. ' class procedure Glide(w: word = 1);',
  21145. ' class procedure Run(w: word = 1); static;',
  21146. ' end;',
  21147. ' TFly = procedure(w: word) of object;',
  21148. ' TGlide = TFly;',
  21149. ' TRun = procedure(w: word);',
  21150. 'var',
  21151. ' f: TFly;',
  21152. ' g: TGlide;',
  21153. ' r: TRun;',
  21154. 'procedure TObject.DoIt;',
  21155. 'begin',
  21156. ' f:=@fly;',
  21157. ' g:=@glide;',
  21158. ' r:=@run;',
  21159. ' f:[email protected];',
  21160. ' g:[email protected];',
  21161. ' r:[email protected];',
  21162. ' with self do begin',
  21163. ' f:=@fly;',
  21164. ' g:=@glide;',
  21165. ' r:=@run;',
  21166. ' end;',
  21167. 'end;',
  21168. 'procedure THelper.fly(w: word);',
  21169. 'begin',
  21170. ' f:=@fly;',
  21171. ' g:=@glide;',
  21172. ' r:=@run;',
  21173. 'end;',
  21174. 'class procedure THelper.glide(w: word);',
  21175. 'begin',
  21176. ' g:=@glide;',
  21177. ' r:=@run;',
  21178. 'end;',
  21179. 'class procedure THelper.run(w: word);',
  21180. 'begin',
  21181. ' g:=@glide;',
  21182. ' r:=@run;',
  21183. 'end;',
  21184. 'var',
  21185. ' Obj: TObject;',
  21186. 'begin',
  21187. ' f:[email protected];',
  21188. ' g:[email protected];',
  21189. ' r:[email protected];',
  21190. ' with obj do begin',
  21191. ' f:=@fly;',
  21192. ' g:=@glide;',
  21193. ' r:=@run;',
  21194. ' end;',
  21195. ' g:[email protected];',
  21196. ' r:[email protected];',
  21197. ' with tobject do begin',
  21198. ' g:=@glide;',
  21199. ' r:=@run;',
  21200. ' end;',
  21201. '']);
  21202. ConvertProgram;
  21203. CheckSource('TestClassHelper_MethodRefObjFPC',
  21204. LinesToStr([ // statements
  21205. 'rtl.createClass(this, "TObject", null, function () {',
  21206. ' this.$init = function () {',
  21207. ' };',
  21208. ' this.$final = function () {',
  21209. ' };',
  21210. ' this.DoIt = function () {',
  21211. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  21212. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  21213. ' $mod.r = $mod.THelper.Run;',
  21214. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  21215. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  21216. ' $mod.r = $mod.THelper.Run;',
  21217. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  21218. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  21219. ' $mod.r = $mod.THelper.Run;',
  21220. ' };',
  21221. '});',
  21222. 'rtl.createHelper(this, "THelper", null, function () {',
  21223. ' this.Fly = function (w) {',
  21224. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  21225. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  21226. ' $mod.r = $mod.THelper.Run;',
  21227. ' };',
  21228. ' this.Glide = function (w) {',
  21229. ' $mod.g = rtl.createCallback(this, $mod.THelper.Glide);',
  21230. ' $mod.r = $mod.THelper.Run;',
  21231. ' };',
  21232. ' this.Run = function (w) {',
  21233. ' $mod.g = rtl.createCallback($mod.THelper, $mod.THelper.Glide);',
  21234. ' $mod.r = $mod.THelper.Run;',
  21235. ' };',
  21236. '});',
  21237. 'this.f = null;',
  21238. 'this.g = null;',
  21239. 'this.r = null;',
  21240. 'this.Obj = null;',
  21241. '']),
  21242. LinesToStr([ // $mod.$main
  21243. '$mod.f = rtl.createCallback($mod.Obj, $mod.THelper.Fly);',
  21244. '$mod.g = rtl.createCallback($mod.Obj.$class, $mod.THelper.Glide);',
  21245. '$mod.r = $mod.THelper.Run;',
  21246. 'var $with = $mod.Obj;',
  21247. '$mod.f = rtl.createCallback($with, $mod.THelper.Fly);',
  21248. '$mod.g = rtl.createCallback($with.$class, $mod.THelper.Glide);',
  21249. '$mod.r = $mod.THelper.Run;',
  21250. '$mod.g = rtl.createCallback($mod.TObject, $mod.THelper.Glide);',
  21251. '$mod.r = $mod.THelper.Run;',
  21252. 'var $with1 = $mod.TObject;',
  21253. '$mod.g = rtl.createCallback($with1, $mod.THelper.Glide);',
  21254. '$mod.r = $mod.THelper.Run;',
  21255. '']));
  21256. end;
  21257. procedure TTestModule.TestClassHelper_Constructor;
  21258. begin
  21259. StartProgram(false);
  21260. Add([
  21261. 'type',
  21262. ' TObject = class',
  21263. ' constructor Create;',
  21264. ' end;',
  21265. ' TClass = class of TObject;',
  21266. ' THelper = class helper for TObject',
  21267. ' constructor NewHlp(w: word);',
  21268. ' end;',
  21269. 'var',
  21270. ' obj: TObject;',
  21271. ' c: TClass;',
  21272. 'constructor TObject.Create;',
  21273. 'begin',
  21274. ' NewHlp(2);', // normal call
  21275. ' tobject.NewHlp(3);', // new instance
  21276. ' c.newhlp(4);', // new instance
  21277. 'end;',
  21278. 'constructor THelper.NewHlp(w: word);',
  21279. 'begin',
  21280. ' create;', // normal call
  21281. ' tobject.create;', // new instance
  21282. ' NewHlp(2);', // normal call
  21283. ' tobject.NewHlp(3);', // new instance
  21284. ' c.newhlp(4);', // new instance
  21285. 'end;',
  21286. 'begin',
  21287. ' obj.newhlp(2);', // normal call
  21288. ' with Obj do newhlp(12);', // normal call
  21289. ' tobject.newhlp(3);', // new instance
  21290. ' with tobject do newhlp(13);', // new instance
  21291. ' c.newhlp(4);', // new instance
  21292. ' with c do newhlp(14);', // new instance
  21293. '']);
  21294. ConvertProgram;
  21295. CheckSource('TestClassHelper_Constructor',
  21296. LinesToStr([ // statements
  21297. 'rtl.createClass(this, "TObject", null, function () {',
  21298. ' this.$init = function () {',
  21299. ' };',
  21300. ' this.$final = function () {',
  21301. ' };',
  21302. ' this.Create = function () {',
  21303. ' $mod.THelper.NewHlp.call(this, 2);',
  21304. ' $mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  21305. ' $mod.c.$create($mod.THelper.NewHlp, [4]);',
  21306. ' return this;',
  21307. ' };',
  21308. '});',
  21309. 'rtl.createHelper(this, "THelper", null, function () {',
  21310. ' this.NewHlp = function (w) {',
  21311. ' this.Create();',
  21312. ' $mod.TObject.$create("Create");',
  21313. ' $mod.THelper.NewHlp.call(this, 2);',
  21314. ' $mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  21315. ' $mod.c.$create($mod.THelper.NewHlp, [4]);',
  21316. ' return this;',
  21317. ' };',
  21318. '});',
  21319. 'this.obj = null;',
  21320. 'this.c = null;',
  21321. '']),
  21322. LinesToStr([ // $mod.$main
  21323. '$mod.THelper.NewHlp.call($mod.obj, 2);',
  21324. 'var $with = $mod.obj;',
  21325. '$mod.THelper.NewHlp.call($with, 12);',
  21326. '$mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  21327. 'var $with1 = $mod.TObject;',
  21328. '$with1.$create($mod.THelper.NewHlp, [13]);',
  21329. '$mod.c.$create($mod.THelper.NewHlp, [4]);',
  21330. 'var $with2 = $mod.c;',
  21331. '$with2.$create($mod.THelper.NewHlp, [14]);',
  21332. '']));
  21333. end;
  21334. procedure TTestModule.TestClassHelper_InheritedObjFPC;
  21335. begin
  21336. StartProgram(false);
  21337. Add([
  21338. 'type',
  21339. ' TObject = class',
  21340. ' procedure Fly;',
  21341. ' end;',
  21342. ' TObjHelper = class helper for TObject',
  21343. ' procedure Fly;',
  21344. ' end;',
  21345. ' TBird = class',
  21346. ' procedure Fly;',
  21347. ' end;',
  21348. ' TBirdHelper = class helper for TBird',
  21349. ' procedure Fly;',
  21350. ' procedure Walk(w: word);',
  21351. ' end;',
  21352. ' TEagleHelper = class helper(TBirdHelper) for TBird',
  21353. ' procedure Fly;',
  21354. ' procedure Walk(w: word);',
  21355. ' end;',
  21356. 'procedure Tobject.fly;',
  21357. 'begin',
  21358. ' inherited;', // ignore
  21359. 'end;',
  21360. 'procedure Tobjhelper.fly;',
  21361. 'begin',
  21362. ' {@TObject_Fly}inherited;',
  21363. ' inherited {@TObject_Fly}Fly;',
  21364. 'end;',
  21365. 'procedure Tbird.fly;',
  21366. 'begin',
  21367. ' {@TObjHelper_Fly}inherited;',
  21368. ' inherited {@TObjHelper_Fly}Fly;',
  21369. 'end;',
  21370. 'procedure Tbirdhelper.fly;',
  21371. 'begin',
  21372. ' {@TBird_Fly}inherited;',
  21373. ' inherited {@TBird_Fly}Fly;',
  21374. 'end;',
  21375. 'procedure Tbirdhelper.walk(w: word);',
  21376. 'begin',
  21377. 'end;',
  21378. 'procedure teagleHelper.fly;',
  21379. 'begin',
  21380. ' {@TBird_Fly}inherited;',
  21381. ' inherited {@TBird_Fly}Fly;',
  21382. 'end;',
  21383. 'procedure teagleHelper.walk(w: word);',
  21384. 'begin',
  21385. ' {@TBirdHelper_Walk}inherited;',
  21386. ' inherited {@TBirdHelper_Walk}Walk(3);',
  21387. 'end;',
  21388. 'begin',
  21389. '']);
  21390. ConvertProgram;
  21391. CheckSource('TestClassHelper_InheritedObjFPC',
  21392. LinesToStr([ // statements
  21393. 'rtl.createClass(this, "TObject", null, function () {',
  21394. ' this.$init = function () {',
  21395. ' };',
  21396. ' this.$final = function () {',
  21397. ' };',
  21398. ' this.Fly = function () {',
  21399. ' };',
  21400. '});',
  21401. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21402. ' this.Fly = function () {',
  21403. ' $mod.TObject.Fly.call(this);',
  21404. ' $mod.TObject.Fly.call(this);',
  21405. ' };',
  21406. '});',
  21407. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21408. ' this.Fly$1 = function () {',
  21409. ' $mod.TObjHelper.Fly.call(this);',
  21410. ' $mod.TObjHelper.Fly.call(this);',
  21411. ' };',
  21412. '});',
  21413. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  21414. ' this.Fly = function () {',
  21415. ' $mod.TBird.Fly$1.call(this);',
  21416. ' $mod.TBird.Fly$1.call(this);',
  21417. ' };',
  21418. ' this.Walk = function (w) {',
  21419. ' };',
  21420. '});',
  21421. 'rtl.createHelper(this, "TEagleHelper", this.TBirdHelper, function () {',
  21422. ' this.Fly$1 = function () {',
  21423. ' $mod.TBird.Fly$1.call(this);',
  21424. ' $mod.TBird.Fly$1.call(this);',
  21425. ' };',
  21426. ' this.Walk$1 = function (w) {',
  21427. ' $mod.TBirdHelper.Walk.apply(this, arguments);',
  21428. ' $mod.TBirdHelper.Walk.call(this, 3);',
  21429. ' };',
  21430. '});',
  21431. '']),
  21432. LinesToStr([ // $mod.$main
  21433. '']));
  21434. end;
  21435. procedure TTestModule.TestClassHelper_Property;
  21436. begin
  21437. StartProgram(false);
  21438. Add([
  21439. 'type',
  21440. ' TObject = class',
  21441. ' FSize: word;',
  21442. ' function GetSpeed: word;',
  21443. ' procedure SetSpeed(Value: word);',
  21444. ' end;',
  21445. ' TObjHelper = class helper for TObject',
  21446. ' function GetLeft: word;',
  21447. ' procedure SetLeft(Value: word);',
  21448. ' property Size: word read FSize write FSize;',
  21449. ' property Speed: word read GetSpeed write SetSpeed;',
  21450. ' property Left: word read GetLeft write SetLeft;',
  21451. ' end;',
  21452. ' TBird = class',
  21453. ' property NotRight: word read GetLeft write SetLeft;',
  21454. ' procedure DoIt;',
  21455. ' end;',
  21456. 'var',
  21457. ' b: TBird;',
  21458. 'function Tobject.GetSpeed: word;',
  21459. 'begin',
  21460. ' Size:=Size+11;',
  21461. ' Speed:=Speed+12;',
  21462. ' Result:=Left+13;',
  21463. ' Left:=13;',
  21464. ' Left:=Left+13;',
  21465. ' Self.Size:=Self.Size+21;',
  21466. ' Self.Speed:=Self.Speed+22;',
  21467. ' Self.Left:=Self.Left+23;',
  21468. ' with Self do begin',
  21469. ' Size:=Size+31;',
  21470. ' Speed:=Speed+32;',
  21471. ' Left:=Left+33;',
  21472. ' end;',
  21473. 'end;',
  21474. 'procedure Tobject.SetSpeed(Value: word);',
  21475. 'begin',
  21476. 'end;',
  21477. 'function TObjHelper.GetLeft: word;',
  21478. 'begin',
  21479. ' Size:=Size+11;',
  21480. ' Speed:=Speed+12;',
  21481. ' Left:=Left+13;',
  21482. ' Self.Size:=Self.Size+21;',
  21483. ' Self.Speed:=Self.Speed+22;',
  21484. ' Self.Left:=Self.Left+23;',
  21485. ' with Self do begin',
  21486. ' Size:=Size+31;',
  21487. ' Speed:=Speed+32;',
  21488. ' Left:=Left+33;',
  21489. ' end;',
  21490. 'end;',
  21491. 'procedure TObjHelper.SetLeft(Value: word);',
  21492. 'begin',
  21493. 'end;',
  21494. 'procedure TBird.DoIt;',
  21495. 'begin',
  21496. ' NotRight:=NotRight+11;',
  21497. ' Self.NotRight:=Self.NotRight+21;',
  21498. ' with Self do begin',
  21499. ' NotRight:=NotRight+31;',
  21500. ' end;',
  21501. 'end;',
  21502. 'begin',
  21503. ' b.Size:=b.Size+11;',
  21504. ' b.Speed:=b.Speed+12;',
  21505. ' b.Left:=b.Left+13;',
  21506. ' b.NotRight:=b.NotRight+14;',
  21507. ' with b do begin',
  21508. ' Size:=Size+31;',
  21509. ' Speed:=Speed+32;',
  21510. ' Left:=Left+33;',
  21511. ' NotRight:=NotRight+34;',
  21512. ' end;',
  21513. '']);
  21514. ConvertProgram;
  21515. CheckSource('TestClassHelper_Property',
  21516. LinesToStr([ // statements
  21517. 'rtl.createClass(this, "TObject", null, function () {',
  21518. ' this.$init = function () {',
  21519. ' this.FSize = 0;',
  21520. ' };',
  21521. ' this.$final = function () {',
  21522. ' };',
  21523. ' this.GetSpeed = function () {',
  21524. ' var Result = 0;',
  21525. ' this.FSize = this.FSize + 11;',
  21526. ' this.SetSpeed(this.GetSpeed() + 12);',
  21527. ' Result = $mod.TObjHelper.GetLeft.call(this) + 13;',
  21528. ' $mod.TObjHelper.SetLeft.call(this, 13);',
  21529. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  21530. ' this.FSize = this.FSize + 21;',
  21531. ' this.SetSpeed(this.GetSpeed() + 22);',
  21532. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  21533. ' this.FSize = this.FSize + 31;',
  21534. ' this.SetSpeed(this.GetSpeed() + 32);',
  21535. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  21536. ' return Result;',
  21537. ' };',
  21538. ' this.SetSpeed = function (Value) {',
  21539. ' };',
  21540. '});',
  21541. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21542. ' this.GetLeft = function () {',
  21543. ' var Result = 0;',
  21544. ' this.FSize = this.FSize + 11;',
  21545. ' this.SetSpeed(this.GetSpeed() + 12);',
  21546. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  21547. ' this.FSize = this.FSize + 21;',
  21548. ' this.SetSpeed(this.GetSpeed() + 22);',
  21549. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  21550. ' this.FSize = this.FSize + 31;',
  21551. ' this.SetSpeed(this.GetSpeed() + 32);',
  21552. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  21553. ' return Result;',
  21554. ' };',
  21555. ' this.SetLeft = function (Value) {',
  21556. ' };',
  21557. '});',
  21558. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21559. ' this.DoIt = function () {',
  21560. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 11);',
  21561. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 21);',
  21562. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 31);',
  21563. ' };',
  21564. '});',
  21565. 'this.b = null;',
  21566. '']),
  21567. LinesToStr([ // $mod.$main
  21568. '$mod.b.FSize = $mod.b.FSize + 11;',
  21569. '$mod.b.SetSpeed($mod.b.GetSpeed() + 12);',
  21570. '$mod.TObjHelper.SetLeft.call($mod.b, $mod.TObjHelper.GetLeft.call($mod.b) + 13);',
  21571. '$mod.TObjHelper.SetLeft.call($mod.b, $mod.TObjHelper.GetLeft.call($mod.b) + 14);',
  21572. 'var $with = $mod.b;',
  21573. '$with.FSize = $with.FSize + 31;',
  21574. '$with.SetSpeed($with.GetSpeed() + 32);',
  21575. '$mod.TObjHelper.SetLeft.call($with, $mod.TObjHelper.GetLeft.call($with) + 33);',
  21576. '$mod.TObjHelper.SetLeft.call($with, $mod.TObjHelper.GetLeft.call($with) + 34);',
  21577. '']));
  21578. end;
  21579. procedure TTestModule.TestClassHelper_Property_Array;
  21580. begin
  21581. StartProgram(false);
  21582. Add([
  21583. 'type',
  21584. ' TObject = class',
  21585. ' function GetSpeed(Index: boolean): word;',
  21586. ' procedure SetSpeed(Index: boolean; Value: word);',
  21587. ' end;',
  21588. ' TObjHelper = class helper for TObject',
  21589. ' function GetSize(Index: boolean): word;',
  21590. ' procedure SetSize(Index: boolean; Value: word);',
  21591. ' property Size[Index: boolean]: word read GetSize write SetSize;',
  21592. ' property Speed[Index: boolean]: word read GetSpeed write SetSpeed;',
  21593. ' end;',
  21594. ' TBird = class',
  21595. ' property Items[Index: boolean]: word read GetSize write SetSize;',
  21596. ' procedure DoIt;',
  21597. ' end;',
  21598. 'var',
  21599. ' b: TBird;',
  21600. 'function Tobject.GetSpeed(Index: boolean): word;',
  21601. 'begin',
  21602. ' Result:=Size[false];',
  21603. ' Size[true]:=Size[false]+11;',
  21604. ' Speed[true]:=Speed[false]+12;',
  21605. ' Self.Size[true]:=Self.Size[false]+21;',
  21606. ' Self.Speed[true]:=Self.Speed[false]+22;',
  21607. ' with Self do begin',
  21608. ' Size[true]:=Size[false]+31;',
  21609. ' Speed[true]:=Speed[false]+32;',
  21610. ' end;',
  21611. 'end;',
  21612. 'procedure Tobject.SetSpeed(Index: boolean; Value: word);',
  21613. 'begin',
  21614. 'end;',
  21615. 'function TObjHelper.GetSize(Index: boolean): word;',
  21616. 'begin',
  21617. ' Size[true]:=Size[false]+11;',
  21618. ' Speed[true]:=Speed[false]+12;',
  21619. ' Self.Size[true]:=Self.Size[false]+21;',
  21620. ' Self.Speed[true]:=Self.Speed[false]+22;',
  21621. ' with Self do begin',
  21622. ' Size[true]:=Size[false]+31;',
  21623. ' Speed[true]:=Speed[false]+32;',
  21624. ' end;',
  21625. 'end;',
  21626. 'procedure TObjHelper.SetSize(Index: boolean; Value: word);',
  21627. 'begin',
  21628. 'end;',
  21629. 'procedure TBird.DoIt;',
  21630. 'begin',
  21631. ' Items[true]:=Items[false]+11;',
  21632. ' Self.Items[true]:=Self.Items[false]+21;',
  21633. ' with Self do Items[true]:=Items[false]+31;',
  21634. 'end;',
  21635. 'begin',
  21636. ' b.Size[true]:=b.Size[false]+11;',
  21637. ' b.Speed[true]:=b.Speed[false]+12;',
  21638. ' b.Items[true]:=b.Items[false]+13;',
  21639. ' with b do begin',
  21640. ' Size[true]:=Size[false]+21;',
  21641. ' Speed[true]:=Speed[false]+22;',
  21642. ' Items[true]:=Items[false]+23;',
  21643. ' end;',
  21644. '']);
  21645. ConvertProgram;
  21646. CheckSource('TestClassHelper_Property_Array',
  21647. LinesToStr([ // statements
  21648. 'rtl.createClass(this, "TObject", null, function () {',
  21649. ' this.$init = function () {',
  21650. ' };',
  21651. ' this.$final = function () {',
  21652. ' };',
  21653. ' this.GetSpeed = function (Index) {',
  21654. ' var Result = 0;',
  21655. ' Result = $mod.TObjHelper.GetSize.call(this, false);',
  21656. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  21657. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  21658. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  21659. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  21660. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  21661. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  21662. ' return Result;',
  21663. ' };',
  21664. ' this.SetSpeed = function (Index, Value) {',
  21665. ' };',
  21666. '});',
  21667. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21668. ' this.GetSize = function (Index) {',
  21669. ' var Result = 0;',
  21670. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  21671. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  21672. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  21673. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  21674. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  21675. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  21676. ' return Result;',
  21677. ' };',
  21678. ' this.SetSize = function (Index, Value) {',
  21679. ' };',
  21680. '});',
  21681. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21682. ' this.DoIt = function () {',
  21683. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  21684. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  21685. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  21686. ' };',
  21687. '});',
  21688. 'this.b = null;',
  21689. '']),
  21690. LinesToStr([ // $mod.$main
  21691. '$mod.TObjHelper.SetSize.call($mod.b, true, $mod.TObjHelper.GetSize.call($mod.b, false) + 11);',
  21692. '$mod.b.SetSpeed(true, $mod.b.GetSpeed(false) + 12);',
  21693. '$mod.TObjHelper.SetSize.call($mod.b, true, $mod.TObjHelper.GetSize.call($mod.b, false) + 13);',
  21694. 'var $with = $mod.b;',
  21695. '$mod.TObjHelper.SetSize.call($with, true, $mod.TObjHelper.GetSize.call($with, false) + 21);',
  21696. '$with.SetSpeed(true, $with.GetSpeed(false) + 22);',
  21697. '$mod.TObjHelper.SetSize.call($with, true, $mod.TObjHelper.GetSize.call($with, false) + 23);',
  21698. '']));
  21699. end;
  21700. procedure TTestModule.TestClassHelper_Property_Array_Default;
  21701. begin
  21702. StartProgram(false);
  21703. Add([
  21704. 'type',
  21705. ' TObject = class',
  21706. ' function GetSpeed(Index: boolean): word;',
  21707. ' procedure SetSpeed(Index: boolean; Value: word);',
  21708. ' end;',
  21709. ' TObjHelper = class helper for TObject',
  21710. ' property Speed[Index: boolean]: word read GetSpeed write SetSpeed; default;',
  21711. ' end;',
  21712. ' TBird = class',
  21713. ' end;',
  21714. ' TBirdHelper = class helper for TBird',
  21715. ' function GetSize(Index: word): boolean;',
  21716. ' procedure SetSize(Index: word; Value: boolean);',
  21717. ' property Size[Index: word]: boolean read GetSize write SetSize; default;',
  21718. ' end;',
  21719. 'function Tobject.GetSpeed(Index: boolean): word;',
  21720. 'begin',
  21721. ' Self[true]:=Self[false]+1;',
  21722. 'end;',
  21723. 'procedure Tobject.SetSpeed(Index: boolean; Value: word);',
  21724. 'begin',
  21725. 'end;',
  21726. 'function TBirdHelper.GetSize(Index: word): boolean;',
  21727. 'begin',
  21728. ' Self[1]:=not Self[2];',
  21729. 'end;',
  21730. 'procedure TBirdHelper.SetSize(Index: word; Value: boolean);',
  21731. 'begin',
  21732. 'end;',
  21733. 'var',
  21734. ' o: TObject;',
  21735. ' b: TBird;',
  21736. 'begin',
  21737. ' o[true]:=o[false]+1;',
  21738. ' b[3]:=not b[4];',
  21739. '']);
  21740. ConvertProgram;
  21741. CheckSource('TestClassHelper_Property_Array_Default',
  21742. LinesToStr([ // statements
  21743. 'rtl.createClass(this, "TObject", null, function () {',
  21744. ' this.$init = function () {',
  21745. ' };',
  21746. ' this.$final = function () {',
  21747. ' };',
  21748. ' this.GetSpeed = function (Index) {',
  21749. ' var Result = 0;',
  21750. ' this.SetSpeed(true, this.GetSpeed(false) + 1);',
  21751. ' return Result;',
  21752. ' };',
  21753. ' this.SetSpeed = function (Index, Value) {',
  21754. ' };',
  21755. '});',
  21756. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21757. '});',
  21758. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21759. '});',
  21760. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  21761. ' this.GetSize = function (Index) {',
  21762. ' var Result = false;',
  21763. ' $mod.TBirdHelper.SetSize.call(this, 1, !$mod.TBirdHelper.GetSize.call(this, 2));',
  21764. ' return Result;',
  21765. ' };',
  21766. ' this.SetSize = function (Index, Value) {',
  21767. ' };',
  21768. '});',
  21769. 'this.o = null;',
  21770. 'this.b = null;',
  21771. '']),
  21772. LinesToStr([ // $mod.$main
  21773. '$mod.o.SetSpeed(true, $mod.o.GetSpeed(false) + 1);',
  21774. '$mod.TBirdHelper.SetSize.call($mod.b, 3, !$mod.TBirdHelper.GetSize.call($mod.b, 4));',
  21775. '']));
  21776. end;
  21777. procedure TTestModule.TestClassHelper_Property_Array_DefaultDefault;
  21778. begin
  21779. StartProgram(false);
  21780. Add([
  21781. 'type',
  21782. ' TObject = class',
  21783. ' end;',
  21784. ' TObjHelper = class helper for TObject',
  21785. ' function GetItems(Index: word): TObject;',
  21786. ' procedure SetItems(Index: word; Value: TObject);',
  21787. ' property Items[Index: word]: TObject read GetItems write SetItems; default;',
  21788. ' end;',
  21789. 'function Tobjhelper.GetItems(Index: word): TObject;',
  21790. 'begin',
  21791. ' Self[1][2]:=Self[3][4];',
  21792. 'end;',
  21793. 'procedure Tobjhelper.SetItems(Index: word; Value: TObject);',
  21794. 'begin',
  21795. 'end;',
  21796. 'var',
  21797. ' o: TObject;',
  21798. 'begin',
  21799. ' o[1][2]:=o[3][4];',
  21800. '']);
  21801. ConvertProgram;
  21802. CheckSource('TestClassHelper_Property_Array_DefaultDefault',
  21803. LinesToStr([ // statements
  21804. 'rtl.createClass(this, "TObject", null, function () {',
  21805. ' this.$init = function () {',
  21806. ' };',
  21807. ' this.$final = function () {',
  21808. ' };',
  21809. '});',
  21810. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21811. ' this.GetItems = function (Index) {',
  21812. ' var Result = null;',
  21813. ' $mod.TObjHelper.SetItems.call($mod.TObjHelper.GetItems.call(this, 1), 2, $mod.TObjHelper.GetItems.call($mod.TObjHelper.GetItems.call(this, 3), 4));',
  21814. ' return Result;',
  21815. ' };',
  21816. ' this.SetItems = function (Index, Value) {',
  21817. ' };',
  21818. '});',
  21819. 'this.o = null;',
  21820. '']),
  21821. LinesToStr([ // $mod.$main
  21822. '$mod.TObjHelper.SetItems.call($mod.TObjHelper.GetItems.call($mod.o, 1), 2, $mod.TObjHelper.GetItems.call($mod.TObjHelper.GetItems.call($mod.o, 3), 4));',
  21823. '']));
  21824. end;
  21825. procedure TTestModule.TestClassHelper_ClassProperty;
  21826. begin
  21827. StartProgram(false);
  21828. Add([
  21829. 'type',
  21830. ' TObject = class',
  21831. ' class var FSize: word;',
  21832. ' class function GetSpeed: word;',
  21833. ' class procedure SetSpeed(Value: word); virtual; abstract;',
  21834. ' end;',
  21835. ' TObjHelper = class helper for TObject',
  21836. ' class function GetLeft: word;',
  21837. ' class procedure SetLeft(Value: word);',
  21838. ' class property Size: word read FSize write FSize;',
  21839. ' class property Speed: word read GetSpeed write SetSpeed;',
  21840. ' class property Left: word read GetLeft write SetLeft;',
  21841. ' end;',
  21842. ' TBird = class',
  21843. ' class property NotRight: word read GetLeft write SetLeft;',
  21844. ' class procedure DoIt;',
  21845. ' end;',
  21846. ' TBirdClass = class of TBird;',
  21847. 'class function Tobject.GetSpeed: word;',
  21848. 'begin',
  21849. ' Size:=Size+11;',
  21850. ' Speed:=Speed+12;',
  21851. ' Left:=Left+13;',
  21852. ' Self.Size:=Self.Size+21;',
  21853. ' Self.Speed:=Self.Speed+22;',
  21854. ' Self.Left:=Self.Left+23;',
  21855. ' with Self do begin',
  21856. ' Size:=Size+31;',
  21857. ' Speed:=Speed+32;',
  21858. ' Left:=Left+33;',
  21859. ' end;',
  21860. 'end;',
  21861. 'class function TObjHelper.GetLeft: word;',
  21862. 'begin',
  21863. ' Size:=Size+11;',
  21864. ' Speed:=Speed+12;',
  21865. ' Left:=Left+13;',
  21866. ' Self.Size:=Self.Size+21;',
  21867. ' Self.Speed:=Self.Speed+22;',
  21868. ' Self.Left:=Self.Left+23;',
  21869. ' with Self do begin',
  21870. ' Size:=Size+31;',
  21871. ' Speed:=Speed+32;',
  21872. ' Left:=Left+33;',
  21873. ' end;',
  21874. 'end;',
  21875. 'class procedure TObjHelper.SetLeft(Value: word);',
  21876. 'begin',
  21877. 'end;',
  21878. 'class procedure TBird.DoIt;',
  21879. 'begin',
  21880. ' NotRight:=NotRight+11;',
  21881. ' Self.NotRight:=Self.NotRight+21;',
  21882. ' with Self do NotRight:=NotRight+31;',
  21883. 'end;',
  21884. 'var',
  21885. ' b: TBird;',
  21886. ' c: TBirdClass;',
  21887. 'begin',
  21888. ' b.Size:=b.Size+11;',
  21889. ' b.Speed:=b.Speed+12;',
  21890. ' b.Left:=b.Left+13;',
  21891. ' b.NotRight:=b.NotRight+14;',
  21892. ' with b do begin',
  21893. ' Size:=Size+31;',
  21894. ' Speed:=Speed+32;',
  21895. ' Left:=Left+33;',
  21896. ' NotRight:=NotRight+34;',
  21897. ' end;',
  21898. ' c.Size:=c.Size+11;',
  21899. ' c.Speed:=c.Speed+12;',
  21900. ' c.Left:=c.Left+13;',
  21901. ' c.NotRight:=c.NotRight+14;',
  21902. ' with c do begin',
  21903. ' Size:=Size+31;',
  21904. ' Speed:=Speed+32;',
  21905. ' Left:=Left+33;',
  21906. ' NotRight:=NotRight+34;',
  21907. ' end;',
  21908. ' tbird.Size:=tbird.Size+11;',
  21909. ' tbird.Speed:=tbird.Speed+12;',
  21910. ' tbird.Left:=tbird.Left+13;',
  21911. ' tbird.NotRight:=tbird.NotRight+14;',
  21912. ' with tbird do begin',
  21913. ' Size:=Size+31;',
  21914. ' Speed:=Speed+32;',
  21915. ' Left:=Left+33;',
  21916. ' NotRight:=NotRight+34;',
  21917. ' end;',
  21918. '']);
  21919. ConvertProgram;
  21920. CheckSource('TestClassHelper_ClassProperty',
  21921. LinesToStr([ // statements
  21922. 'rtl.createClass(this, "TObject", null, function () {',
  21923. ' this.FSize = 0;',
  21924. ' this.$init = function () {',
  21925. ' };',
  21926. ' this.$final = function () {',
  21927. ' };',
  21928. ' this.GetSpeed = function () {',
  21929. ' var Result = 0;',
  21930. ' $mod.TObject.FSize = this.FSize + 11;',
  21931. ' this.SetSpeed(this.GetSpeed() + 12);',
  21932. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  21933. ' $mod.TObject.FSize = this.FSize + 21;',
  21934. ' this.SetSpeed(this.GetSpeed() + 22);',
  21935. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  21936. ' $mod.TObject.FSize = this.FSize + 31;',
  21937. ' this.SetSpeed(this.GetSpeed() + 32);',
  21938. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  21939. ' return Result;',
  21940. ' };',
  21941. '});',
  21942. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21943. ' this.GetLeft = function () {',
  21944. ' var Result = 0;',
  21945. ' $mod.TObject.FSize = this.FSize + 11;',
  21946. ' this.SetSpeed(this.GetSpeed() + 12);',
  21947. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  21948. ' $mod.TObject.FSize = this.FSize + 21;',
  21949. ' this.SetSpeed(this.GetSpeed() + 22);',
  21950. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  21951. ' $mod.TObject.FSize = this.FSize + 31;',
  21952. ' this.SetSpeed(this.GetSpeed() + 32);',
  21953. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  21954. ' return Result;',
  21955. ' };',
  21956. ' this.SetLeft = function (Value) {',
  21957. ' };',
  21958. '});',
  21959. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21960. ' this.DoIt = function () {',
  21961. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 11);',
  21962. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 21);',
  21963. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 31);',
  21964. ' };',
  21965. '});',
  21966. 'this.b = null;',
  21967. 'this.c = null;',
  21968. '']),
  21969. LinesToStr([ // $mod.$main
  21970. '$mod.TObject.FSize = $mod.b.FSize + 11;',
  21971. '$mod.b.$class.SetSpeed($mod.b.$class.GetSpeed() + 12);',
  21972. '$mod.TObjHelper.SetLeft.call($mod.b.$class, $mod.TObjHelper.GetLeft.call($mod.b.$class) + 13);',
  21973. '$mod.TObjHelper.SetLeft.call($mod.b.$class, $mod.TObjHelper.GetLeft.call($mod.b.$class) + 14);',
  21974. 'var $with = $mod.b;',
  21975. '$mod.TObject.FSize = $with.FSize + 31;',
  21976. '$with.$class.SetSpeed($with.$class.GetSpeed() + 32);',
  21977. '$mod.TObjHelper.SetLeft.call($with.$class, $mod.TObjHelper.GetLeft.call($with.$class) + 33);',
  21978. '$mod.TObjHelper.SetLeft.call($with.$class, $mod.TObjHelper.GetLeft.call($with.$class) + 34);',
  21979. '$mod.TObject.FSize = $mod.c.FSize + 11;',
  21980. '$mod.c.SetSpeed($mod.c.GetSpeed() + 12);',
  21981. '$mod.TObjHelper.SetLeft.call($mod.c, $mod.TObjHelper.GetLeft.call($mod.c) + 13);',
  21982. '$mod.TObjHelper.SetLeft.call($mod.c, $mod.TObjHelper.GetLeft.call($mod.c) + 14);',
  21983. 'var $with1 = $mod.c;',
  21984. '$mod.TObject.FSize = $with1.FSize + 31;',
  21985. '$with1.SetSpeed($with1.GetSpeed() + 32);',
  21986. '$mod.TObjHelper.SetLeft.call($with1, $mod.TObjHelper.GetLeft.call($with1) + 33);',
  21987. '$mod.TObjHelper.SetLeft.call($with1, $mod.TObjHelper.GetLeft.call($with1) + 34);',
  21988. '$mod.TObject.FSize = $mod.TBird.FSize + 11;',
  21989. '$mod.TBird.SetSpeed($mod.TBird.GetSpeed() + 12);',
  21990. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 13);',
  21991. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 14);',
  21992. 'var $with2 = $mod.TBird;',
  21993. '$mod.TObject.FSize = $with2.FSize + 31;',
  21994. '$with2.SetSpeed($with2.GetSpeed() + 32);',
  21995. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 33);',
  21996. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 34);',
  21997. '']));
  21998. end;
  21999. procedure TTestModule.TestClassHelper_ClassPropertyStatic;
  22000. begin
  22001. StartProgram(false);
  22002. Add([
  22003. 'type',
  22004. ' TObject = class',
  22005. ' class function GetSpeed: word; static;',
  22006. ' class procedure SetSpeed(Value: word); static;',
  22007. ' end;',
  22008. ' TObjHelper = class helper for TObject',
  22009. ' class function GetLeft: word; static;',
  22010. ' class procedure SetLeft(Value: word); static;',
  22011. ' class property Speed: word read GetSpeed write SetSpeed;',
  22012. ' class property Left: word read GetLeft write SetLeft;',
  22013. ' end;',
  22014. ' TBird = class',
  22015. ' class property NotRight: word read GetLeft write SetLeft;',
  22016. ' class procedure DoIt; static;',
  22017. ' class procedure DoSome;',
  22018. ' end;',
  22019. ' TBirdClass = class of TBird;',
  22020. 'class function Tobject.GetSpeed: word;',
  22021. 'begin',
  22022. ' Speed:=Speed+12;',
  22023. ' Left:=Left+13;',
  22024. 'end;',
  22025. 'class procedure TObject.SetSpeed(Value: word);',
  22026. 'begin',
  22027. 'end;',
  22028. 'class function TObjHelper.GetLeft: word;',
  22029. 'begin',
  22030. ' Speed:=Speed+12;',
  22031. ' Left:=Left+13;',
  22032. 'end;',
  22033. 'class procedure TObjHelper.SetLeft(Value: word);',
  22034. 'begin',
  22035. 'end;',
  22036. 'class procedure TBird.DoIt;',
  22037. 'begin',
  22038. ' NotRight:=NotRight+11;',
  22039. 'end;',
  22040. 'class procedure TBird.DoSome;',
  22041. 'begin',
  22042. ' Speed:=Speed+12;',
  22043. ' Left:=Left+13;',
  22044. ' Self.Speed:=Self.Speed+22;',
  22045. ' Self.Left:=Self.Left+23;',
  22046. ' with Self do begin',
  22047. ' Speed:=Speed+32;',
  22048. ' Left:=Left+33;',
  22049. ' end;',
  22050. ' NotRight:=NotRight+11;',
  22051. ' Self.NotRight:=Self.NotRight+21;',
  22052. ' with Self do NotRight:=NotRight+31;',
  22053. 'end;',
  22054. 'var',
  22055. ' b: TBird;',
  22056. ' c: TBirdClass;',
  22057. 'begin',
  22058. ' b.Speed:=b.Speed+12;',
  22059. ' b.Left:=b.Left+13;',
  22060. ' b.NotRight:=b.NotRight+14;',
  22061. ' with b do begin',
  22062. ' Speed:=Speed+32;',
  22063. ' Left:=Left+33;',
  22064. ' NotRight:=NotRight+34;',
  22065. ' end;',
  22066. ' c.Speed:=c.Speed+12;',
  22067. ' c.Left:=c.Left+13;',
  22068. ' c.NotRight:=c.NotRight+14;',
  22069. ' with c do begin',
  22070. ' Speed:=Speed+32;',
  22071. ' Left:=Left+33;',
  22072. ' NotRight:=NotRight+34;',
  22073. ' end;',
  22074. ' tbird.Speed:=tbird.Speed+12;',
  22075. ' tbird.Left:=tbird.Left+13;',
  22076. ' tbird.NotRight:=tbird.NotRight+14;',
  22077. ' with tbird do begin',
  22078. ' Speed:=Speed+32;',
  22079. ' Left:=Left+33;',
  22080. ' NotRight:=NotRight+34;',
  22081. ' end;',
  22082. '']);
  22083. ConvertProgram;
  22084. CheckSource('TestClassHelper_ClassPropertyStatic',
  22085. LinesToStr([ // statements
  22086. 'rtl.createClass(this, "TObject", null, function () {',
  22087. ' this.$init = function () {',
  22088. ' };',
  22089. ' this.$final = function () {',
  22090. ' };',
  22091. ' this.GetSpeed = function () {',
  22092. ' var Result = 0;',
  22093. ' $mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  22094. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  22095. ' return Result;',
  22096. ' };',
  22097. ' this.SetSpeed = function (Value) {',
  22098. ' };',
  22099. '});',
  22100. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  22101. ' this.GetLeft = function () {',
  22102. ' var Result = 0;',
  22103. ' $mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  22104. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  22105. ' return Result;',
  22106. ' };',
  22107. ' this.SetLeft = function (Value) {',
  22108. ' };',
  22109. '});',
  22110. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  22111. ' this.DoIt = function () {',
  22112. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 11);',
  22113. ' };',
  22114. ' this.DoSome = function () {',
  22115. ' this.SetSpeed(this.GetSpeed() + 12);',
  22116. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  22117. ' this.SetSpeed(this.GetSpeed() + 22);',
  22118. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 23);',
  22119. ' this.SetSpeed(this.GetSpeed() + 32);',
  22120. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  22121. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 11);',
  22122. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 21);',
  22123. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 31);',
  22124. ' };',
  22125. '});',
  22126. 'this.b = null;',
  22127. 'this.c = null;',
  22128. '']),
  22129. LinesToStr([ // $mod.$main
  22130. '$mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  22131. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  22132. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  22133. 'var $with = $mod.b;',
  22134. '$with.SetSpeed($with.GetSpeed() + 32);',
  22135. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  22136. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  22137. '$mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  22138. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  22139. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  22140. 'var $with1 = $mod.c;',
  22141. '$with1.SetSpeed($with1.GetSpeed() + 32);',
  22142. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  22143. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  22144. '$mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  22145. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  22146. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  22147. 'var $with2 = $mod.TBird;',
  22148. '$with2.SetSpeed($with2.GetSpeed() + 32);',
  22149. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  22150. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  22151. '']));
  22152. end;
  22153. procedure TTestModule.TestClassHelper_ClassProperty_Array;
  22154. begin
  22155. StartProgram(false);
  22156. Add([
  22157. 'type',
  22158. ' TObject = class',
  22159. ' class function GetSpeed(Index: boolean): word;',
  22160. ' class procedure SetSpeed(Index: boolean; Value: word); virtual; abstract;',
  22161. ' end;',
  22162. ' TObjHelper = class helper for TObject',
  22163. ' class function GetSize(Index: boolean): word;',
  22164. ' class procedure SetSize(Index: boolean; Value: word);',
  22165. ' class property Size[Index: boolean]: word read GetSize write SetSize;',
  22166. ' class property Speed[Index: boolean]: word read GetSpeed write SetSpeed;',
  22167. ' end;',
  22168. ' TBird = class',
  22169. ' class property Items[Index: boolean]: word read GetSize write SetSize;',
  22170. ' class procedure DoIt;',
  22171. ' end;',
  22172. ' TBirdClass = class of TBird;',
  22173. 'class function Tobject.GetSpeed(Index: boolean): word;',
  22174. 'begin',
  22175. ' Size[true]:=Size[false]+11;',
  22176. ' Speed[true]:=Speed[false]+12;',
  22177. ' Self.Size[true]:=Self.Size[false]+21;',
  22178. ' Self.Speed[true]:=Self.Speed[false]+22;',
  22179. ' with Self do begin',
  22180. ' Size[true]:=Size[false]+31;',
  22181. ' Speed[true]:=Speed[false]+32;',
  22182. ' end;',
  22183. 'end;',
  22184. 'class function TObjHelper.GetSize(Index: boolean): word;',
  22185. 'begin',
  22186. ' Size[true]:=Size[false]+11;',
  22187. ' Speed[true]:=Speed[false]+12;',
  22188. ' Self.Size[true]:=Self.Size[false]+21;',
  22189. ' Self.Speed[true]:=Self.Speed[false]+22;',
  22190. ' with Self do begin',
  22191. ' Size[true]:=Size[false]+31;',
  22192. ' Speed[true]:=Speed[false]+32;',
  22193. ' end;',
  22194. 'end;',
  22195. 'class procedure TObjHelper.SetSize(Index: boolean; Value: word);',
  22196. 'begin',
  22197. 'end;',
  22198. 'class procedure TBird.DoIt;',
  22199. 'begin',
  22200. ' Items[true]:=Items[false]+11;',
  22201. ' Self.Items[true]:=Self.Items[false]+21;',
  22202. ' with Self do Items[true]:=Items[false]+31;',
  22203. 'end;',
  22204. 'var',
  22205. ' b: TBird;',
  22206. ' c: TBirdClass;',
  22207. 'begin',
  22208. ' b.Size[true]:=b.Size[false]+11;',
  22209. ' b.Speed[true]:=b.Speed[false]+12;',
  22210. ' b.Items[true]:=b.Items[false]+13;',
  22211. ' with b do begin',
  22212. ' Size[true]:=Size[false]+21;',
  22213. ' Speed[true]:=Speed[false]+22;',
  22214. ' Items[true]:=Items[false]+23;',
  22215. ' end;',
  22216. ' c.Size[true]:=c.Size[false]+11;',
  22217. ' c.Speed[true]:=c.Speed[false]+12;',
  22218. ' c.Items[true]:=c.Items[false]+13;',
  22219. ' with c do begin',
  22220. ' Size[true]:=Size[false]+21;',
  22221. ' Speed[true]:=Speed[false]+22;',
  22222. ' Items[true]:=Items[false]+23;',
  22223. ' end;',
  22224. ' TBird.Size[true]:=TBird.Size[false]+11;',
  22225. ' TBird.Speed[true]:=TBird.Speed[false]+12;',
  22226. ' TBird.Items[true]:=TBird.Items[false]+13;',
  22227. ' with TBird do begin',
  22228. ' Size[true]:=Size[false]+21;',
  22229. ' Speed[true]:=Speed[false]+22;',
  22230. ' Items[true]:=Items[false]+23;',
  22231. ' end;',
  22232. '']);
  22233. ConvertProgram;
  22234. CheckSource('TestClassHelper_ClassProperty_Array',
  22235. LinesToStr([ // statements
  22236. 'rtl.createClass(this, "TObject", null, function () {',
  22237. ' this.$init = function () {',
  22238. ' };',
  22239. ' this.$final = function () {',
  22240. ' };',
  22241. ' this.GetSpeed = function (Index) {',
  22242. ' var Result = 0;',
  22243. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  22244. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  22245. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  22246. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  22247. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  22248. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  22249. ' return Result;',
  22250. ' };',
  22251. '});',
  22252. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  22253. ' this.GetSize = function (Index) {',
  22254. ' var Result = 0;',
  22255. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  22256. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  22257. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  22258. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  22259. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  22260. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  22261. ' return Result;',
  22262. ' };',
  22263. ' this.SetSize = function (Index, Value) {',
  22264. ' };',
  22265. '});',
  22266. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  22267. ' this.DoIt = function () {',
  22268. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  22269. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  22270. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  22271. ' };',
  22272. '});',
  22273. 'this.b = null;',
  22274. 'this.c = null;',
  22275. '']),
  22276. LinesToStr([ // $mod.$main
  22277. '$mod.TObjHelper.SetSize.call($mod.b.$class, true, $mod.TObjHelper.GetSize.call($mod.b.$class, false) + 11);',
  22278. '$mod.b.$class.SetSpeed(true, $mod.b.$class.GetSpeed(false) + 12);',
  22279. '$mod.TObjHelper.SetSize.call($mod.b.$class, true, $mod.TObjHelper.GetSize.call($mod.b.$class, false) + 13);',
  22280. 'var $with = $mod.b;',
  22281. '$mod.TObjHelper.SetSize.call($with.$class, true, $mod.TObjHelper.GetSize.call($with.$class, false) + 21);',
  22282. '$with.$class.SetSpeed(true, $with.$class.GetSpeed(false) + 22);',
  22283. '$mod.TObjHelper.SetSize.call($with.$class, true, $mod.TObjHelper.GetSize.call($with.$class, false) + 23);',
  22284. '$mod.TObjHelper.SetSize.call($mod.c, true, $mod.TObjHelper.GetSize.call($mod.c, false) + 11);',
  22285. '$mod.c.SetSpeed(true, $mod.c.GetSpeed(false) + 12);',
  22286. '$mod.TObjHelper.SetSize.call($mod.c, true, $mod.TObjHelper.GetSize.call($mod.c, false) + 13);',
  22287. 'var $with1 = $mod.c;',
  22288. '$mod.TObjHelper.SetSize.call($with1, true, $mod.TObjHelper.GetSize.call($with1, false) + 21);',
  22289. '$with1.SetSpeed(true, $with1.GetSpeed(false) + 22);',
  22290. '$mod.TObjHelper.SetSize.call($with1, true, $mod.TObjHelper.GetSize.call($with1, false) + 23);',
  22291. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 11);',
  22292. '$mod.TBird.SetSpeed(true, $mod.TBird.GetSpeed(false) + 12);',
  22293. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 13);',
  22294. 'var $with2 = $mod.TBird;',
  22295. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 21);',
  22296. '$with2.SetSpeed(true, $with2.GetSpeed(false) + 22);',
  22297. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 23);',
  22298. '']));
  22299. end;
  22300. procedure TTestModule.TestClassHelper_ForIn;
  22301. begin
  22302. StartProgram(false);
  22303. Add([
  22304. 'type',
  22305. ' TObject = class end;',
  22306. ' TItem = TObject;',
  22307. ' TEnumerator = class',
  22308. ' FCurrent: TItem;',
  22309. ' property Current: TItem read FCurrent;',
  22310. ' function MoveNext: boolean;',
  22311. ' end;',
  22312. ' TBird = class',
  22313. ' end;',
  22314. ' TBirdHelper = class helper for TBird',
  22315. ' function GetEnumerator: TEnumerator;',
  22316. ' end;',
  22317. 'function TEnumerator.MoveNext: boolean;',
  22318. 'begin',
  22319. 'end;',
  22320. 'function TBirdHelper.GetEnumerator: TEnumerator;',
  22321. 'begin',
  22322. 'end;',
  22323. 'var',
  22324. ' b: TBird;',
  22325. ' i, i2: TItem;',
  22326. 'begin',
  22327. ' for i in b do i2:=i;']);
  22328. ConvertProgram;
  22329. CheckSource('TestClassHelper_ForIn',
  22330. LinesToStr([ // statements
  22331. 'rtl.createClass(this, "TObject", null, function () {',
  22332. ' this.$init = function () {',
  22333. ' };',
  22334. ' this.$final = function () {',
  22335. ' };',
  22336. '});',
  22337. 'rtl.createClass(this, "TEnumerator", this.TObject, function () {',
  22338. ' this.$init = function () {',
  22339. ' $mod.TObject.$init.call(this);',
  22340. ' this.FCurrent = null;',
  22341. ' };',
  22342. ' this.$final = function () {',
  22343. ' this.FCurrent = undefined;',
  22344. ' $mod.TObject.$final.call(this);',
  22345. ' };',
  22346. ' this.MoveNext = function () {',
  22347. ' var Result = false;',
  22348. ' return Result;',
  22349. ' };',
  22350. '});',
  22351. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  22352. '});',
  22353. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  22354. ' this.GetEnumerator = function () {',
  22355. ' var Result = null;',
  22356. ' return Result;',
  22357. ' };',
  22358. '});',
  22359. 'this.b = null;',
  22360. 'this.i = null;',
  22361. 'this.i2 = null;'
  22362. ]),
  22363. LinesToStr([ // $mod.$main
  22364. 'var $in = $mod.TBirdHelper.GetEnumerator.call($mod.b);',
  22365. 'try {',
  22366. ' while ($in.MoveNext()){',
  22367. ' $mod.i = $in.FCurrent;',
  22368. ' $mod.i2 = $mod.i;',
  22369. ' }',
  22370. '} finally {',
  22371. ' $in = rtl.freeLoc($in)',
  22372. '};',
  22373. '']));
  22374. end;
  22375. procedure TTestModule.TestClassHelper_PassProperty;
  22376. begin
  22377. StartProgram(false);
  22378. Add([
  22379. 'type',
  22380. ' TObject = class',
  22381. ' FField: TObject;',
  22382. ' property Field: TObject read FField write FField;',
  22383. ' end;',
  22384. ' THelper = class helper for TObject',
  22385. ' procedure Fly;',
  22386. ' class procedure Run;',
  22387. ' class procedure Jump; static;',
  22388. ' end;',
  22389. 'procedure THelper.Fly;',
  22390. 'begin',
  22391. ' Field.Fly;',
  22392. ' Field.Run;',
  22393. ' Field.Jump;',
  22394. ' with Field do begin',
  22395. ' Fly;',
  22396. ' Run;',
  22397. ' Jump;',
  22398. ' end;',
  22399. 'end;',
  22400. 'class procedure THelper.Run;',
  22401. 'begin',
  22402. 'end;',
  22403. 'class procedure THelper.Jump;',
  22404. 'begin',
  22405. 'end;',
  22406. 'var',
  22407. ' b: TObject;',
  22408. 'begin',
  22409. ' b.Field.Fly;',
  22410. ' b.Field.Run;',
  22411. ' b.Field.Jump;',
  22412. ' with b do begin',
  22413. ' Field.Run;',
  22414. ' Field.Fly;',
  22415. ' Field.Jump;',
  22416. ' end;',
  22417. ' with b.Field do begin',
  22418. ' Run;',
  22419. ' Fly;',
  22420. ' Jump;',
  22421. ' end;',
  22422. '']);
  22423. ConvertProgram;
  22424. CheckSource('TestClassHelper_PassProperty',
  22425. LinesToStr([ // statements
  22426. 'rtl.createClass(this, "TObject", null, function () {',
  22427. ' this.$init = function () {',
  22428. ' this.FField = null;',
  22429. ' };',
  22430. ' this.$final = function () {',
  22431. ' this.FField = undefined;',
  22432. ' };',
  22433. '});',
  22434. 'rtl.createHelper(this, "THelper", null, function () {',
  22435. ' this.Fly = function () {',
  22436. ' $mod.THelper.Fly.call(this.FField);',
  22437. ' $mod.THelper.Run.call(this.FField.$class);',
  22438. ' $mod.THelper.Jump();',
  22439. ' var $with = this.FField;',
  22440. ' $mod.THelper.Fly.call($with);',
  22441. ' $mod.THelper.Run.call($with.$class);',
  22442. ' $mod.THelper.Jump();',
  22443. ' };',
  22444. ' this.Run = function () {',
  22445. ' };',
  22446. ' this.Jump = function () {',
  22447. ' };',
  22448. '});',
  22449. 'this.b = null;',
  22450. '']),
  22451. LinesToStr([ // $mod.$main
  22452. '$mod.THelper.Fly.call($mod.b.FField);',
  22453. '$mod.THelper.Run.call($mod.b.FField.$class);',
  22454. '$mod.THelper.Jump();',
  22455. 'var $with = $mod.b;',
  22456. '$mod.THelper.Run.call($with.FField.$class);',
  22457. '$mod.THelper.Fly.call($with.FField);',
  22458. '$mod.THelper.Jump();',
  22459. 'var $with1 = $mod.b.FField;',
  22460. '$mod.THelper.Run.call($with1.$class);',
  22461. '$mod.THelper.Fly.call($with1);',
  22462. '$mod.THelper.Jump();',
  22463. '']));
  22464. end;
  22465. procedure TTestModule.TestExtClassHelper_ClassVar;
  22466. begin
  22467. StartProgram(false);
  22468. Add([
  22469. '{$modeswitch externalclass}',
  22470. 'type',
  22471. ' TExtA = class external name ''ExtObj''',
  22472. ' end;',
  22473. ' THelper = class helper for TExtA',
  22474. ' const',
  22475. ' One = 1;',
  22476. ' Two: word = 2;',
  22477. ' class var',
  22478. ' Glob: word;',
  22479. ' function Foo(w: word): word;',
  22480. ' class function Bar(w: word): word; static;',
  22481. ' end;',
  22482. 'function THelper.foo(w: word): word;',
  22483. 'begin',
  22484. ' Result:=w;',
  22485. ' Two:=One+w;',
  22486. ' Glob:=Glob;',
  22487. ' Result:=Self.Glob;',
  22488. ' Self.Glob:=Self.Glob;',
  22489. ' with Self do Glob:=Glob;',
  22490. 'end;',
  22491. 'class function THelper.bar(w: word): word;',
  22492. 'begin',
  22493. ' Result:=w;',
  22494. ' Two:=One;',
  22495. ' Glob:=Glob;',
  22496. 'end;',
  22497. 'var o: TExtA;',
  22498. 'begin',
  22499. ' texta.two:=texta.one;',
  22500. ' texta.Glob:=texta.Glob;',
  22501. ' with texta do begin',
  22502. ' two:=one;',
  22503. ' Glob:=Glob;',
  22504. ' end;',
  22505. ' o.two:=o.one;',
  22506. ' o.Glob:=o.Glob;',
  22507. ' with o do begin',
  22508. ' two:=one;',
  22509. ' Glob:=Glob;',
  22510. ' end;',
  22511. '']);
  22512. ConvertProgram;
  22513. CheckSource('TestExtClassHelper_ClassVar',
  22514. LinesToStr([ // statements
  22515. 'rtl.createHelper(this, "THelper", null, function () {',
  22516. ' this.One = 1;',
  22517. ' this.Two = 2;',
  22518. ' this.Glob = 0;',
  22519. ' this.Foo = function (w) {',
  22520. ' var Result = 0;',
  22521. ' Result = w;',
  22522. ' $mod.THelper.Two = 1 + w;',
  22523. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22524. ' Result = $mod.THelper.Glob;',
  22525. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22526. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22527. ' return Result;',
  22528. ' };',
  22529. ' this.Bar = function (w) {',
  22530. ' var Result = 0;',
  22531. ' Result = w;',
  22532. ' $mod.THelper.Two = 1;',
  22533. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22534. ' return Result;',
  22535. ' };',
  22536. '});',
  22537. 'this.o = null;',
  22538. '']),
  22539. LinesToStr([ // $mod.$main
  22540. '$mod.THelper.Two = 1;',
  22541. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22542. '$mod.THelper.Two = 1;',
  22543. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22544. '$mod.THelper.Two = 1;',
  22545. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22546. 'var $with = $mod.o;',
  22547. '$mod.THelper.Two = 1;',
  22548. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22549. '']));
  22550. end;
  22551. procedure TTestModule.TestExtClassHelper_Method_Call;
  22552. begin
  22553. StartProgram(false);
  22554. Add([
  22555. '{$modeswitch externalclass}',
  22556. 'type',
  22557. ' TFly = function(w: word): word of object;',
  22558. ' TExtA = class external name ''ExtObj''',
  22559. ' procedure Run(w: word = 10);',
  22560. ' end;',
  22561. ' THelper = class helper for TExtA',
  22562. ' function Foo(w: word = 1): word;',
  22563. ' function Fly(w: word = 2): word; external name ''Fly'';',
  22564. ' end;',
  22565. 'var p: TFly;',
  22566. 'function THelper.foo(w: word): word;',
  22567. 'begin',
  22568. ' Run;',
  22569. ' Run();',
  22570. ' Run(11);',
  22571. ' Foo;',
  22572. ' Foo();',
  22573. ' Foo(12);',
  22574. ' Self.Foo;',
  22575. ' Self.Foo();',
  22576. ' Self.Foo(13);',
  22577. ' Fly;',
  22578. ' Fly();',
  22579. ' with Self do begin',
  22580. ' Foo;',
  22581. ' Foo();',
  22582. ' Foo(14);',
  22583. ' Fly;',
  22584. ' Fly();',
  22585. ' end;',
  22586. ' p:=@Fly;',
  22587. 'end;',
  22588. 'var Obj: TExtA;',
  22589. 'begin',
  22590. ' obj.Foo;',
  22591. ' obj.Foo();',
  22592. ' obj.Foo(21);',
  22593. ' obj.Fly;',
  22594. ' obj.Fly();',
  22595. ' with obj do begin',
  22596. ' Foo;',
  22597. ' Foo();',
  22598. ' Foo(22);',
  22599. ' Fly;',
  22600. ' Fly();',
  22601. ' end;',
  22602. ' p:[email protected];',
  22603. '']);
  22604. ConvertProgram;
  22605. CheckSource('TestExtClassHelper_Method_Call',
  22606. LinesToStr([ // statements
  22607. 'rtl.createHelper(this, "THelper", null, function () {',
  22608. ' this.Foo = function (w) {',
  22609. ' var Result = 0;',
  22610. ' this.Run(10);',
  22611. ' this.Run(10);',
  22612. ' this.Run(11);',
  22613. ' $mod.THelper.Foo.call(this, 1);',
  22614. ' $mod.THelper.Foo.call(this, 1);',
  22615. ' $mod.THelper.Foo.call(this, 12);',
  22616. ' $mod.THelper.Foo.call(this, 1);',
  22617. ' $mod.THelper.Foo.call(this, 1);',
  22618. ' $mod.THelper.Foo.call(this, 13);',
  22619. ' this.Fly(2);',
  22620. ' this.Fly(2);',
  22621. ' $mod.THelper.Foo.call(this, 1);',
  22622. ' $mod.THelper.Foo.call(this, 1);',
  22623. ' $mod.THelper.Foo.call(this, 14);',
  22624. ' this.Fly(2);',
  22625. ' this.Fly(2);',
  22626. ' $mod.p = rtl.createCallback(this, "Fly");',
  22627. ' return Result;',
  22628. ' };',
  22629. '});',
  22630. 'this.p = null;',
  22631. 'this.Obj = null;',
  22632. '']),
  22633. LinesToStr([ // $mod.$main
  22634. '$mod.THelper.Foo.call($mod.Obj, 1);',
  22635. '$mod.THelper.Foo.call($mod.Obj, 1);',
  22636. '$mod.THelper.Foo.call($mod.Obj, 21);',
  22637. '$mod.Obj.Fly(2);',
  22638. '$mod.Obj.Fly(2);',
  22639. 'var $with = $mod.Obj;',
  22640. '$mod.THelper.Foo.call($with, 1);',
  22641. '$mod.THelper.Foo.call($with, 1);',
  22642. '$mod.THelper.Foo.call($with, 22);',
  22643. '$with.Fly(2);',
  22644. '$with.Fly(2);',
  22645. '$mod.p = rtl.createCallback($mod.Obj, "Fly");',
  22646. '']));
  22647. end;
  22648. procedure TTestModule.TestExtClassHelper_ClassMethod_MissingStatic;
  22649. begin
  22650. StartProgram(false);
  22651. Add([
  22652. '{$modeswitch externalclass}',
  22653. 'type',
  22654. ' TExtA = class external name ''ExtObj''',
  22655. ' procedure Run(w: word = 10);',
  22656. ' end;',
  22657. ' THelper = class helper for TExtA',
  22658. ' class procedure Fly;',
  22659. ' end;',
  22660. 'class procedure THelper.Fly;',
  22661. 'begin end;',
  22662. 'begin',
  22663. '']);
  22664. SetExpectedPasResolverError(sHelperClassMethodForExtClassMustBeStatic,
  22665. nHelperClassMethodForExtClassMustBeStatic);
  22666. ConvertProgram;
  22667. end;
  22668. procedure TTestModule.TestRecordHelper_ClassVar;
  22669. begin
  22670. StartProgram(false);
  22671. Add([
  22672. 'type',
  22673. ' TRec = record',
  22674. ' end;',
  22675. ' THelper = record helper for TRec',
  22676. ' const',
  22677. ' One = 1;',
  22678. ' Two: word = 2;',
  22679. ' class var',
  22680. ' Glob: word;',
  22681. ' function Foo(w: word): word;',
  22682. ' class function Bar(w: word): word; static;',
  22683. ' end;',
  22684. 'function THelper.foo(w: word): word;',
  22685. 'begin',
  22686. ' Result:=w;',
  22687. ' Two:=One+w;',
  22688. ' Glob:=Glob;',
  22689. ' Result:=Self.Glob;',
  22690. ' Self.Glob:=Self.Glob;',
  22691. ' with Self do Glob:=Glob;',
  22692. ' Self:=Self;',
  22693. 'end;',
  22694. 'class function THelper.bar(w: word): word;',
  22695. 'begin',
  22696. ' Result:=w;',
  22697. ' Two:=One;',
  22698. ' Glob:=Glob;',
  22699. 'end;',
  22700. 'var r: TRec;',
  22701. 'begin',
  22702. ' trec.two:=trec.one;',
  22703. ' trec.Glob:=trec.Glob;',
  22704. ' with trec do begin',
  22705. ' two:=one;',
  22706. ' Glob:=Glob;',
  22707. ' end;',
  22708. ' r.two:=r.one;',
  22709. ' r.Glob:=r.Glob;',
  22710. ' with r do begin',
  22711. ' two:=one;',
  22712. ' Glob:=Glob;',
  22713. ' end;',
  22714. '']);
  22715. ConvertProgram;
  22716. CheckSource('TestRecordHelper_ClassVar',
  22717. LinesToStr([ // statements
  22718. 'rtl.recNewT(this, "TRec", function () {',
  22719. ' this.$eq = function (b) {',
  22720. ' return true;',
  22721. ' };',
  22722. ' this.$assign = function (s) {',
  22723. ' return this;',
  22724. ' };',
  22725. '});',
  22726. 'rtl.createHelper(this, "THelper", null, function () {',
  22727. ' this.One = 1;',
  22728. ' this.Two = 2;',
  22729. ' this.Glob = 0;',
  22730. ' this.Foo = function (w) {',
  22731. ' var Result = 0;',
  22732. ' Result = w;',
  22733. ' $mod.THelper.Two = 1 + w;',
  22734. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22735. ' Result = $mod.THelper.Glob;',
  22736. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22737. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22738. ' this.$assign(this);',
  22739. ' return Result;',
  22740. ' };',
  22741. ' this.Bar = function (w) {',
  22742. ' var Result = 0;',
  22743. ' Result = w;',
  22744. ' $mod.THelper.Two = 1;',
  22745. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22746. ' return Result;',
  22747. ' };',
  22748. '});',
  22749. 'this.r = this.TRec.$new();',
  22750. '']),
  22751. LinesToStr([ // $mod.$main
  22752. '$mod.THelper.Two = 1;',
  22753. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22754. 'var $with = $mod.TRec;',
  22755. '$mod.THelper.Two = 1;',
  22756. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22757. '$mod.THelper.Two = 1;',
  22758. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22759. 'var $with1 = $mod.r;',
  22760. '$mod.THelper.Two = 1;',
  22761. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22762. '']));
  22763. end;
  22764. procedure TTestModule.TestRecordHelper_Method_Call;
  22765. begin
  22766. StartProgram(false);
  22767. Add([
  22768. '{$modeswitch AdvancedRecords}',
  22769. 'type',
  22770. ' TRec = record',
  22771. ' procedure Run(w: word = 10);',
  22772. ' end;',
  22773. ' THelper = record helper for TRec',
  22774. ' function Foo(w: word = 1): word;',
  22775. ' end;',
  22776. 'procedure TRec.Run(w: word);',
  22777. 'begin',
  22778. ' Foo;',
  22779. ' Foo();',
  22780. ' Foo(2);',
  22781. ' Self.Foo;',
  22782. ' Self.Foo();',
  22783. ' Self.Foo(3);',
  22784. ' with Self do begin',
  22785. ' Foo;',
  22786. ' Foo();',
  22787. ' Foo(4);',
  22788. ' end;',
  22789. 'end;',
  22790. 'function THelper.foo(w: word): word;',
  22791. 'begin',
  22792. ' Run;',
  22793. ' Run();',
  22794. ' Run(11);',
  22795. ' Foo;',
  22796. ' Foo();',
  22797. ' Foo(12);',
  22798. ' Self.Foo;',
  22799. ' Self.Foo();',
  22800. ' Self.Foo(13);',
  22801. ' with Self do begin',
  22802. ' Foo;',
  22803. ' Foo();',
  22804. ' Foo(14);',
  22805. ' end;',
  22806. 'end;',
  22807. 'var Rec: TRec;',
  22808. 'begin',
  22809. ' Rec.Foo;',
  22810. ' Rec.Foo();',
  22811. ' Rec.Foo(21);',
  22812. ' with Rec do begin',
  22813. ' Foo;',
  22814. ' Foo();',
  22815. ' Foo(22);',
  22816. ' end;',
  22817. '']);
  22818. ConvertProgram;
  22819. CheckSource('TestRecordHelper_Method_Call',
  22820. LinesToStr([ // statements
  22821. 'rtl.recNewT(this, "TRec", function () {',
  22822. ' this.$eq = function (b) {',
  22823. ' return true;',
  22824. ' };',
  22825. ' this.$assign = function (s) {',
  22826. ' return this;',
  22827. ' };',
  22828. ' this.Run = function (w) {',
  22829. ' $mod.THelper.Foo.call(this, 1);',
  22830. ' $mod.THelper.Foo.call(this, 1);',
  22831. ' $mod.THelper.Foo.call(this, 2);',
  22832. ' $mod.THelper.Foo.call(this, 1);',
  22833. ' $mod.THelper.Foo.call(this, 1);',
  22834. ' $mod.THelper.Foo.call(this, 3);',
  22835. ' $mod.THelper.Foo.call(this, 1);',
  22836. ' $mod.THelper.Foo.call(this, 1);',
  22837. ' $mod.THelper.Foo.call(this, 4);',
  22838. ' };',
  22839. '});',
  22840. 'rtl.createHelper(this, "THelper", null, function () {',
  22841. ' this.Foo = function (w) {',
  22842. ' var Result = 0;',
  22843. ' this.Run(10);',
  22844. ' this.Run(10);',
  22845. ' this.Run(11);',
  22846. ' $mod.THelper.Foo.call(this, 1);',
  22847. ' $mod.THelper.Foo.call(this, 1);',
  22848. ' $mod.THelper.Foo.call(this, 12);',
  22849. ' $mod.THelper.Foo.call(this, 1);',
  22850. ' $mod.THelper.Foo.call(this, 1);',
  22851. ' $mod.THelper.Foo.call(this, 13);',
  22852. ' $mod.THelper.Foo.call(this, 1);',
  22853. ' $mod.THelper.Foo.call(this, 1);',
  22854. ' $mod.THelper.Foo.call(this, 14);',
  22855. ' return Result;',
  22856. ' };',
  22857. '});',
  22858. 'this.Rec = this.TRec.$new();',
  22859. '']),
  22860. LinesToStr([ // $mod.$main
  22861. '$mod.THelper.Foo.call($mod.Rec, 1);',
  22862. '$mod.THelper.Foo.call($mod.Rec, 1);',
  22863. '$mod.THelper.Foo.call($mod.Rec, 21);',
  22864. 'var $with = $mod.Rec;',
  22865. '$mod.THelper.Foo.call($with, 1);',
  22866. '$mod.THelper.Foo.call($with, 1);',
  22867. '$mod.THelper.Foo.call($with, 22);',
  22868. '']));
  22869. end;
  22870. procedure TTestModule.TestRecordHelper_Constructor;
  22871. begin
  22872. StartProgram(false);
  22873. Add([
  22874. '{$modeswitch AdvancedRecords}',
  22875. 'type',
  22876. ' TRec = record',
  22877. ' constructor Create(w: word);',
  22878. ' end;',
  22879. ' THelper = record helper for TRec',
  22880. ' constructor NewHlp(w: word);',
  22881. ' end;',
  22882. 'var',
  22883. ' Rec: TRec;',
  22884. 'constructor TRec.Create(w: word);',
  22885. 'begin',
  22886. ' NewHlp(2);', // normal call
  22887. ' trec.NewHlp(3);', // new instance
  22888. 'end;',
  22889. 'constructor THelper.NewHlp(w: word);',
  22890. 'begin',
  22891. ' create(2);', // normal call
  22892. ' trec.create(3);', // new instance
  22893. ' NewHlp(4);', // normal call
  22894. ' trec.NewHlp(5);', // new instance
  22895. 'end;',
  22896. 'begin',
  22897. ' rec.newhlp(2);', // normal call
  22898. ' with rec do newhlp(12);', // normal call
  22899. ' trec.newhlp(3);', // new instance
  22900. ' with trec do newhlp(13);', // new instance
  22901. '']);
  22902. ConvertProgram;
  22903. CheckSource('TestRecordHelper_Constructor',
  22904. LinesToStr([ // statements
  22905. 'rtl.recNewT(this, "TRec", function () {',
  22906. ' this.$eq = function (b) {',
  22907. ' return true;',
  22908. ' };',
  22909. ' this.$assign = function (s) {',
  22910. ' return this;',
  22911. ' };',
  22912. ' this.Create = function (w) {',
  22913. ' $mod.THelper.NewHlp.call(this, 2);',
  22914. ' $mod.THelper.$new("NewHlp", [3]);',
  22915. ' return this;',
  22916. ' };',
  22917. '});',
  22918. 'rtl.createHelper(this, "THelper", null, function () {',
  22919. ' this.NewHlp = function (w) {',
  22920. ' this.Create(2);',
  22921. ' $mod.TRec.$new().Create(3);',
  22922. ' $mod.THelper.NewHlp.call(this, 4);',
  22923. ' $mod.THelper.$new("NewHlp", [5]);',
  22924. ' return this;',
  22925. ' };',
  22926. ' this.$new = function (fn, args) {',
  22927. ' return this[fn].apply($mod.TRec.$new(), args);',
  22928. ' };',
  22929. '});',
  22930. 'this.Rec = this.TRec.$new();',
  22931. '']),
  22932. LinesToStr([ // $mod.$main
  22933. '$mod.THelper.NewHlp.call($mod.Rec, 2);',
  22934. 'var $with = $mod.Rec;',
  22935. '$mod.THelper.NewHlp.call($with, 12);',
  22936. '$mod.THelper.$new("NewHlp", [3]);',
  22937. 'var $with1 = $mod.TRec;',
  22938. '$mod.THelper.$new("NewHlp", [13]);',
  22939. '']));
  22940. end;
  22941. procedure TTestModule.TestTypeHelper_ClassVar;
  22942. begin
  22943. StartProgram(false);
  22944. Add([
  22945. '{$modeswitch typehelpers}',
  22946. 'type',
  22947. ' THelper = type helper for byte',
  22948. ' const',
  22949. ' One = 1;',
  22950. ' Two: word = 2;',
  22951. ' class var',
  22952. ' Glob: word;',
  22953. ' function Foo(w: word): word;',
  22954. ' class function Bar(w: word): word; static;',
  22955. ' end;',
  22956. 'function THelper.foo(w: word): word;',
  22957. 'begin',
  22958. ' Result:=w;',
  22959. ' Two:=One+w;',
  22960. ' Glob:=Glob;',
  22961. ' Result:=Self.Glob;',
  22962. ' Self.Glob:=Self.Glob;',
  22963. ' with Self do Glob:=Glob;',
  22964. 'end;',
  22965. 'class function THelper.bar(w: word): word;',
  22966. 'begin',
  22967. ' Result:=w;',
  22968. ' Two:=One;',
  22969. ' Glob:=Glob;',
  22970. 'end;',
  22971. 'var b: byte;',
  22972. 'begin',
  22973. ' byte.two:=byte.one;',
  22974. ' byte.Glob:=byte.Glob;',
  22975. ' with byte do begin',
  22976. ' two:=one;',
  22977. ' Glob:=Glob;',
  22978. ' end;',
  22979. ' b.two:=b.one;',
  22980. ' b.Glob:=b.Glob;',
  22981. ' with b do begin',
  22982. ' two:=one;',
  22983. ' Glob:=Glob;',
  22984. ' end;',
  22985. '']);
  22986. ConvertProgram;
  22987. CheckSource('TestTypeHelper_ClassVar',
  22988. LinesToStr([ // statements
  22989. 'rtl.createHelper(this, "THelper", null, function () {',
  22990. ' this.One = 1;',
  22991. ' this.Two = 2;',
  22992. ' this.Glob = 0;',
  22993. ' this.Foo = function (w) {',
  22994. ' var Result = 0;',
  22995. ' Result = w;',
  22996. ' $mod.THelper.Two = 1 + w;',
  22997. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22998. ' Result = $mod.THelper.Glob;',
  22999. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  23000. ' var $with = this.get();',
  23001. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  23002. ' return Result;',
  23003. ' };',
  23004. ' this.Bar = function (w) {',
  23005. ' var Result = 0;',
  23006. ' Result = w;',
  23007. ' $mod.THelper.Two = 1;',
  23008. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  23009. ' return Result;',
  23010. ' };',
  23011. '});',
  23012. 'this.b = 0;',
  23013. '']),
  23014. LinesToStr([ // $mod.$main
  23015. '$mod.THelper.Two = 1;',
  23016. '$mod.THelper.Glob = $mod.THelper.Glob;',
  23017. '$mod.THelper.Two = 1;',
  23018. '$mod.THelper.Glob = $mod.THelper.Glob;',
  23019. '$mod.THelper.Two = 1;',
  23020. '$mod.THelper.Glob = $mod.THelper.Glob;',
  23021. 'var $with = $mod.b;',
  23022. '$mod.THelper.Two = 1;',
  23023. '$mod.THelper.Glob = $mod.THelper.Glob;',
  23024. '']));
  23025. end;
  23026. procedure TTestModule.TestTypeHelper_PassResultElement;
  23027. begin
  23028. StartProgram(false);
  23029. Add([
  23030. '{$modeswitch typehelpers}',
  23031. 'type',
  23032. ' THelper = type helper for word',
  23033. ' procedure DoIt(e: byte = 123);',
  23034. ' class procedure DoSome(e: byte = 456); static;',
  23035. ' end;',
  23036. 'procedure THelper.DoIt(e: byte);',
  23037. 'begin',
  23038. 'end;',
  23039. 'class procedure THelper.DoSome(e: byte);',
  23040. 'begin',
  23041. 'end;',
  23042. 'function Foo(w: word): word;',
  23043. 'begin',
  23044. ' Result.DoIt;',
  23045. ' Result.DoIt();',
  23046. ' Result.DoSome;',
  23047. ' Result.DoSome();',
  23048. ' with Result do begin',
  23049. ' DoIt;',
  23050. ' DoIt();',
  23051. ' DoSome;',
  23052. ' DoSome();',
  23053. ' end;',
  23054. 'end;',
  23055. 'begin',
  23056. '']);
  23057. ConvertProgram;
  23058. CheckSource('TestTypeHelper_PassResultElement',
  23059. LinesToStr([ // statements
  23060. 'rtl.createHelper(this, "THelper", null, function () {',
  23061. ' this.DoIt = function (e) {',
  23062. ' };',
  23063. ' this.DoSome = function (e) {',
  23064. ' };',
  23065. '});',
  23066. 'this.Foo = function (w) {',
  23067. ' var Result = 0;',
  23068. ' $mod.THelper.DoIt.call({',
  23069. ' get: function () {',
  23070. ' return Result;',
  23071. ' },',
  23072. ' set: function (v) {',
  23073. ' Result = v;',
  23074. ' }',
  23075. ' }, 123);',
  23076. ' $mod.THelper.DoIt.call({',
  23077. ' get: function () {',
  23078. ' return Result;',
  23079. ' },',
  23080. ' set: function (v) {',
  23081. ' Result = v;',
  23082. ' }',
  23083. ' }, 123);',
  23084. ' $mod.THelper.DoSome(456);',
  23085. ' $mod.THelper.DoSome(456);',
  23086. ' $mod.THelper.DoIt.call({',
  23087. ' get: function () {',
  23088. ' return Result;',
  23089. ' },',
  23090. ' set: function (v) {',
  23091. ' Result = v;',
  23092. ' }',
  23093. ' }, 123);',
  23094. ' $mod.THelper.DoIt.call({',
  23095. ' get: function () {',
  23096. ' return Result;',
  23097. ' },',
  23098. ' set: function (v) {',
  23099. ' Result = v;',
  23100. ' }',
  23101. ' }, 123);',
  23102. ' $mod.THelper.DoSome(456);',
  23103. ' $mod.THelper.DoSome(456);',
  23104. ' return Result;',
  23105. '};',
  23106. '']),
  23107. LinesToStr([ // $mod.$main
  23108. '']));
  23109. end;
  23110. procedure TTestModule.TestTypeHelper_PassArgs;
  23111. begin
  23112. StartProgram(false);
  23113. Add([
  23114. '{$modeswitch typehelpers}',
  23115. 'type',
  23116. ' THelper = type helper for word',
  23117. ' procedure DoIt(e: byte = 123);',
  23118. ' end;',
  23119. 'procedure THelper.DoIt(e: byte);',
  23120. 'begin',
  23121. 'end;',
  23122. 'procedure FooDefault(a: word);',
  23123. 'begin',
  23124. ' a.DoIt;',
  23125. ' with a do DoIt;',
  23126. 'end;',
  23127. 'procedure FooConst(const a: word);',
  23128. 'begin',
  23129. ' a.DoIt;',
  23130. ' with a do DoIt;',
  23131. 'end;',
  23132. 'procedure FooVar(var a: word);',
  23133. 'begin',
  23134. ' a.DoIt;',
  23135. ' with a do DoIt;',
  23136. 'end;',
  23137. 'begin',
  23138. '']);
  23139. ConvertProgram;
  23140. CheckSource('TestTypeHelper_PassArgs',
  23141. LinesToStr([ // statements
  23142. 'rtl.createHelper(this, "THelper", null, function () {',
  23143. ' this.DoIt = function (e) {',
  23144. ' };',
  23145. '});',
  23146. 'this.FooDefault = function (a) {',
  23147. ' $mod.THelper.DoIt.call({',
  23148. ' get: function () {',
  23149. ' return a;',
  23150. ' },',
  23151. ' set: function (v) {',
  23152. ' a = v;',
  23153. ' }',
  23154. ' }, 123);',
  23155. ' $mod.THelper.DoIt.call({',
  23156. ' get: function () {',
  23157. ' return a;',
  23158. ' },',
  23159. ' set: function (v) {',
  23160. ' a = v;',
  23161. ' }',
  23162. ' }, 123);',
  23163. '};',
  23164. 'this.FooConst = function (a) {',
  23165. ' $mod.THelper.DoIt.call({',
  23166. ' get: function () {',
  23167. ' return a;',
  23168. ' },',
  23169. ' set: function (v) {',
  23170. ' rtl.raiseE("EPropReadOnly");',
  23171. ' }',
  23172. ' }, 123);',
  23173. ' $mod.THelper.DoIt.call({',
  23174. ' get: function () {',
  23175. ' return a;',
  23176. ' },',
  23177. ' set: function () {',
  23178. ' rtl.raiseE("EPropReadOnly");',
  23179. ' }',
  23180. ' }, 123);',
  23181. '};',
  23182. 'this.FooVar = function (a) {',
  23183. ' $mod.THelper.DoIt.call(a, 123);',
  23184. ' var $with = a.get();',
  23185. ' $mod.THelper.DoIt.call(a, 123);',
  23186. '};',
  23187. '']),
  23188. LinesToStr([ // $mod.$main
  23189. '']));
  23190. end;
  23191. procedure TTestModule.TestTypeHelper_PassVarConst;
  23192. begin
  23193. StartProgram(false);
  23194. Add([
  23195. '{$modeswitch typehelpers}',
  23196. 'type',
  23197. ' THelper = type helper for word',
  23198. ' procedure DoIt(e: byte = 123);',
  23199. ' end;',
  23200. 'procedure THelper.DoIt(e: byte);',
  23201. 'begin',
  23202. 'end;',
  23203. 'var a: word;',
  23204. 'const c: word = 2;',
  23205. '{$writeableconst off}',
  23206. 'const r: word = 3;',
  23207. 'begin',
  23208. ' a.DoIt;',
  23209. ' with a do DoIt;',
  23210. ' c.DoIt;',
  23211. ' with c do DoIt;',
  23212. ' r.DoIt;',
  23213. ' with r do DoIt;',
  23214. '']);
  23215. ConvertProgram;
  23216. CheckSource('TestTypeHelper_PassVarConst',
  23217. LinesToStr([ // statements
  23218. 'rtl.createHelper(this, "THelper", null, function () {',
  23219. ' this.DoIt = function (e) {',
  23220. ' };',
  23221. '});',
  23222. 'this.a = 0;',
  23223. 'this.c = 2;',
  23224. 'this.r = 3;',
  23225. '']),
  23226. LinesToStr([ // $mod.$main
  23227. '$mod.THelper.DoIt.call({',
  23228. ' p: $mod,',
  23229. ' get: function () {',
  23230. ' return this.p.a;',
  23231. ' },',
  23232. ' set: function (v) {',
  23233. ' this.p.a = v;',
  23234. ' }',
  23235. '}, 123);',
  23236. 'var $with = $mod.a;',
  23237. '$mod.THelper.DoIt.call({',
  23238. ' get: function () {',
  23239. ' return $with;',
  23240. ' },',
  23241. ' set: function (v) {',
  23242. ' $with = v;',
  23243. ' }',
  23244. '}, 123);',
  23245. '$mod.THelper.DoIt.call({',
  23246. ' p: $mod,',
  23247. ' get: function () {',
  23248. ' return this.p.c;',
  23249. ' },',
  23250. ' set: function (v) {',
  23251. ' this.p.c = v;',
  23252. ' }',
  23253. '}, 123);',
  23254. 'var $with1 = $mod.c;',
  23255. '$mod.THelper.DoIt.call({',
  23256. ' get: function () {',
  23257. ' return $with1;',
  23258. ' },',
  23259. ' set: function (v) {',
  23260. ' $with1 = v;',
  23261. ' }',
  23262. '}, 123);',
  23263. '$mod.THelper.DoIt.call({',
  23264. ' get: function () {',
  23265. ' return 3;',
  23266. ' },',
  23267. ' set: function (v) {',
  23268. ' rtl.raiseE("EPropReadOnly");',
  23269. ' }',
  23270. '}, 123);',
  23271. 'var $with2 = 3;',
  23272. ' $mod.THelper.DoIt.call({',
  23273. ' get: function () {',
  23274. ' return $with2;',
  23275. ' },',
  23276. ' set: function () {',
  23277. ' rtl.raiseE("EPropReadOnly");',
  23278. ' }',
  23279. ' }, 123);',
  23280. '']));
  23281. end;
  23282. procedure TTestModule.TestTypeHelper_PassFuncResult;
  23283. begin
  23284. StartProgram(false);
  23285. Add([
  23286. '{$modeswitch typehelpers}',
  23287. 'type',
  23288. ' THelper = type helper for word',
  23289. ' procedure DoIt(e: byte = 123);',
  23290. ' end;',
  23291. 'procedure THelper.DoIt(e: byte);',
  23292. 'begin',
  23293. 'end;',
  23294. 'function Foo(b: byte = 1): word;',
  23295. 'begin',
  23296. 'end;',
  23297. 'begin',
  23298. ' Foo.DoIt;',
  23299. ' Foo().DoIt;',
  23300. ' with Foo do DoIt;',
  23301. ' with Foo() do DoIt;',
  23302. '']);
  23303. ConvertProgram;
  23304. CheckSource('TestTypeHelper_PassFuncResult',
  23305. LinesToStr([ // statements
  23306. 'rtl.createHelper(this, "THelper", null, function () {',
  23307. ' this.DoIt = function (e) {',
  23308. ' };',
  23309. '});',
  23310. 'this.Foo = function (b) {',
  23311. ' var Result = 0;',
  23312. ' return Result;',
  23313. '};',
  23314. '']),
  23315. LinesToStr([ // $mod.$main
  23316. '$mod.THelper.DoIt.call({',
  23317. ' a: $mod.Foo(1),',
  23318. ' get: function () {',
  23319. ' return this.a;',
  23320. ' },',
  23321. ' set: function (v) {',
  23322. ' this.a = v;',
  23323. ' }',
  23324. '}, 123);',
  23325. '$mod.THelper.DoIt.call({',
  23326. ' a: $mod.Foo(1),',
  23327. ' get: function () {',
  23328. ' return this.a;',
  23329. ' },',
  23330. ' set: function (v) {',
  23331. ' this.a = v;',
  23332. ' }',
  23333. '}, 123);',
  23334. 'var $with = $mod.Foo(1);',
  23335. '$mod.THelper.DoIt.call({',
  23336. ' get: function () {',
  23337. ' return $with;',
  23338. ' },',
  23339. ' set: function (v) {',
  23340. ' $with = v;',
  23341. ' }',
  23342. '}, 123);',
  23343. 'var $with1 = $mod.Foo(1);',
  23344. '$mod.THelper.DoIt.call({',
  23345. ' get: function () {',
  23346. ' return $with1;',
  23347. ' },',
  23348. ' set: function (v) {',
  23349. ' $with1 = v;',
  23350. ' }',
  23351. '}, 123);',
  23352. '']));
  23353. end;
  23354. procedure TTestModule.TestTypeHelper_PassPropertyField;
  23355. begin
  23356. StartProgram(false);
  23357. Add([
  23358. '{$modeswitch typehelpers}',
  23359. 'type',
  23360. ' TObject = class',
  23361. ' FField: word;',
  23362. ' procedure SetField(Value: word);',
  23363. ' property Field: word read FField write SetField;',
  23364. ' end;',
  23365. ' THelper = type helper for word',
  23366. ' procedure Fly;',
  23367. ' class procedure Run; static;',
  23368. ' end;',
  23369. 'procedure TObject.SetField(Value: word);',
  23370. 'begin',
  23371. ' Field.Fly;',
  23372. ' Field.Run;',
  23373. ' Self.Field.Fly;',
  23374. ' Self.Field.Run;',
  23375. ' with Self do begin',
  23376. ' Field.Fly;',
  23377. ' Field.Run;',
  23378. ' end;',
  23379. ' with Self.Field do begin',
  23380. ' Fly;',
  23381. ' Run;',
  23382. ' end;',
  23383. 'end;',
  23384. 'procedure THelper.Fly;',
  23385. 'begin',
  23386. 'end;',
  23387. 'class procedure THelper.Run;',
  23388. 'begin',
  23389. 'end;',
  23390. 'var',
  23391. ' o: TObject;',
  23392. 'begin',
  23393. ' o.Field.Fly;',
  23394. ' o.Field.Run;',
  23395. ' with o do begin',
  23396. ' Field.Fly;',
  23397. ' Field.Run;',
  23398. ' end;',
  23399. ' with o.Field do begin',
  23400. ' Fly;',
  23401. ' Run;',
  23402. ' end;',
  23403. '']);
  23404. ConvertProgram;
  23405. CheckSource('TestTypeHelper_PassPropertyField',
  23406. LinesToStr([ // statements
  23407. 'rtl.createClass(this, "TObject", null, function () {',
  23408. ' this.$init = function () {',
  23409. ' this.FField = 0;',
  23410. ' };',
  23411. ' this.$final = function () {',
  23412. ' };',
  23413. ' this.SetField = function (Value) {',
  23414. ' $mod.THelper.Fly.call({',
  23415. ' p: this,',
  23416. ' get: function () {',
  23417. ' return this.p.FField;',
  23418. ' },',
  23419. ' set: function (v) {',
  23420. ' this.p.FField = v;',
  23421. ' }',
  23422. ' });',
  23423. ' $mod.THelper.Run();',
  23424. ' $mod.THelper.Fly.call({',
  23425. ' p: this,',
  23426. ' get: function () {',
  23427. ' return this.p.FField;',
  23428. ' },',
  23429. ' set: function (v) {',
  23430. ' this.p.FField = v;',
  23431. ' }',
  23432. ' });',
  23433. ' $mod.THelper.Run();',
  23434. ' $mod.THelper.Fly.call({',
  23435. ' p: this,',
  23436. ' get: function () {',
  23437. ' return this.p.FField;',
  23438. ' },',
  23439. ' set: function (v) {',
  23440. ' this.p.FField = v;',
  23441. ' }',
  23442. ' });',
  23443. ' $mod.THelper.Run();',
  23444. ' var $with = this.FField;',
  23445. ' $mod.THelper.Fly.call({',
  23446. ' get: function () {',
  23447. ' return $with;',
  23448. ' },',
  23449. ' set: function (v) {',
  23450. ' $with = v;',
  23451. ' }',
  23452. ' });',
  23453. ' $mod.THelper.Run();',
  23454. ' };',
  23455. '});',
  23456. 'rtl.createHelper(this, "THelper", null, function () {',
  23457. ' this.Fly = function () {',
  23458. ' };',
  23459. ' this.Run = function () {',
  23460. ' };',
  23461. '});',
  23462. 'this.o = null;',
  23463. '']),
  23464. LinesToStr([ // $mod.$main
  23465. '$mod.THelper.Fly.call({',
  23466. ' p: $mod.o,',
  23467. ' get: function () {',
  23468. ' return this.p.FField;',
  23469. ' },',
  23470. ' set: function (v) {',
  23471. ' this.p.FField = v;',
  23472. ' }',
  23473. '});',
  23474. '$mod.THelper.Run();',
  23475. 'var $with = $mod.o;',
  23476. '$mod.THelper.Fly.call({',
  23477. ' p: $with,',
  23478. ' get: function () {',
  23479. ' return this.p.FField;',
  23480. ' },',
  23481. ' set: function (v) {',
  23482. ' this.p.FField = v;',
  23483. ' }',
  23484. '});',
  23485. '$mod.THelper.Run();',
  23486. 'var $with1 = $mod.o.FField;',
  23487. '$mod.THelper.Fly.call({',
  23488. ' get: function () {',
  23489. ' return $with1;',
  23490. ' },',
  23491. ' set: function (v) {',
  23492. ' $with1 = v;',
  23493. ' }',
  23494. '});',
  23495. '$mod.THelper.Run();',
  23496. '']));
  23497. end;
  23498. procedure TTestModule.TestTypeHelper_PassPropertyGetter;
  23499. begin
  23500. StartProgram(false);
  23501. Add([
  23502. '{$modeswitch typehelpers}',
  23503. 'type',
  23504. ' TObject = class',
  23505. ' FField: word;',
  23506. ' function GetField: word;',
  23507. ' property Field: word read GetField write FField;',
  23508. ' end;',
  23509. ' THelper = type helper for word',
  23510. ' procedure Fly;',
  23511. ' class procedure Run; static;',
  23512. ' end;',
  23513. 'function TObject.GetField: word;',
  23514. 'begin',
  23515. ' Field.Fly;',
  23516. ' Field.Run;',
  23517. ' Self.Field.Fly;',
  23518. ' Self.Field.Run;',
  23519. ' with Self do begin',
  23520. ' Field.Fly;',
  23521. ' Field.Run;',
  23522. ' end;',
  23523. ' with Self.Field do begin',
  23524. ' Fly;',
  23525. ' Run;',
  23526. ' end;',
  23527. 'end;',
  23528. 'procedure THelper.Fly;',
  23529. 'begin',
  23530. 'end;',
  23531. 'class procedure THelper.Run;',
  23532. 'begin',
  23533. 'end;',
  23534. 'var',
  23535. ' o: TObject;',
  23536. 'begin',
  23537. ' o.Field.Fly;',
  23538. ' o.Field.Run;',
  23539. ' with o do begin',
  23540. ' Field.Fly;',
  23541. ' Field.Run;',
  23542. ' end;',
  23543. ' with o.Field do begin',
  23544. ' Fly;',
  23545. ' Run;',
  23546. ' end;',
  23547. '']);
  23548. ConvertProgram;
  23549. CheckSource('TestTypeHelper_PassPropertyGetter',
  23550. LinesToStr([ // statements
  23551. 'rtl.createClass(this, "TObject", null, function () {',
  23552. ' this.$init = function () {',
  23553. ' this.FField = 0;',
  23554. ' };',
  23555. ' this.$final = function () {',
  23556. ' };',
  23557. ' this.GetField = function () {',
  23558. ' var Result = 0;',
  23559. ' $mod.THelper.Fly.call({',
  23560. ' p: this.GetField(),',
  23561. ' get: function () {',
  23562. ' return this.p;',
  23563. ' },',
  23564. ' set: function (v) {',
  23565. ' this.p = v;',
  23566. ' }',
  23567. ' });',
  23568. ' $mod.THelper.Run();',
  23569. ' $mod.THelper.Fly.call({',
  23570. ' p: this.GetField(),',
  23571. ' get: function () {',
  23572. ' return this.p;',
  23573. ' },',
  23574. ' set: function (v) {',
  23575. ' this.p = v;',
  23576. ' }',
  23577. ' });',
  23578. ' $mod.THelper.Run();',
  23579. ' $mod.THelper.Fly.call({',
  23580. ' p: this.GetField(),',
  23581. ' get: function () {',
  23582. ' return this.p;',
  23583. ' },',
  23584. ' set: function (v) {',
  23585. ' this.p = v;',
  23586. ' }',
  23587. ' });',
  23588. ' $mod.THelper.Run();',
  23589. ' var $with = this.GetField();',
  23590. ' $mod.THelper.Fly.call({',
  23591. ' get: function () {',
  23592. ' return $with;',
  23593. ' },',
  23594. ' set: function (v) {',
  23595. ' $with = v;',
  23596. ' }',
  23597. ' });',
  23598. ' $mod.THelper.Run();',
  23599. ' return Result;',
  23600. ' };',
  23601. '});',
  23602. 'rtl.createHelper(this, "THelper", null, function () {',
  23603. ' this.Fly = function () {',
  23604. ' };',
  23605. ' this.Run = function () {',
  23606. ' };',
  23607. '});',
  23608. 'this.o = null;',
  23609. '']),
  23610. LinesToStr([ // $mod.$main
  23611. '$mod.THelper.Fly.call({',
  23612. ' p: $mod.o.GetField(),',
  23613. ' get: function () {',
  23614. ' return this.p;',
  23615. ' },',
  23616. ' set: function (v) {',
  23617. ' this.p = v;',
  23618. ' }',
  23619. '});',
  23620. '$mod.THelper.Run();',
  23621. 'var $with = $mod.o;',
  23622. '$mod.THelper.Fly.call({',
  23623. ' p: $with.GetField(),',
  23624. ' get: function () {',
  23625. ' return this.p;',
  23626. ' },',
  23627. ' set: function (v) {',
  23628. ' this.p = v;',
  23629. ' }',
  23630. '});',
  23631. '$mod.THelper.Run();',
  23632. 'var $with1 = $mod.o.GetField();',
  23633. '$mod.THelper.Fly.call({',
  23634. ' get: function () {',
  23635. ' return $with1;',
  23636. ' },',
  23637. ' set: function (v) {',
  23638. ' $with1 = v;',
  23639. ' }',
  23640. '});',
  23641. '$mod.THelper.Run();',
  23642. '']));
  23643. end;
  23644. procedure TTestModule.TestTypeHelper_PassClassPropertyField;
  23645. begin
  23646. StartProgram(false);
  23647. Add([
  23648. '{$modeswitch typehelpers}',
  23649. 'type',
  23650. ' TObject = class',
  23651. ' class var FField: word;',
  23652. ' class procedure SetField(Value: word);',
  23653. ' class property Field: word read FField write SetField;',
  23654. ' end;',
  23655. ' THelper = type helper for word',
  23656. ' procedure Fly(n: byte);',
  23657. ' end;',
  23658. 'class procedure TObject.SetField(Value: word);',
  23659. 'begin',
  23660. ' Field.Fly(1);',
  23661. ' Self.Field.Fly(2);',
  23662. ' with Self do Field.Fly(3);',
  23663. ' with Self.Field do Fly(4);',
  23664. ' TObject.Field.Fly(5);',
  23665. ' with TObject do Field.Fly(6);',
  23666. ' with TObject.Field do Fly(7);',
  23667. 'end;',
  23668. 'procedure THelper.Fly(n: byte);',
  23669. 'begin',
  23670. 'end;',
  23671. 'var',
  23672. ' o: TObject;',
  23673. 'begin',
  23674. ' o.Field.Fly(11);',
  23675. ' with o do Field.Fly(12);',
  23676. ' with o.Field do Fly(13);',
  23677. ' TObject.Field.Fly(14);',
  23678. ' with TObject do Field.Fly(15);',
  23679. ' with TObject.Field do Fly(16);',
  23680. '']);
  23681. ConvertProgram;
  23682. CheckSource('TestTypeHelper_PassClassPropertyField',
  23683. LinesToStr([ // statements
  23684. 'rtl.createClass(this, "TObject", null, function () {',
  23685. ' this.FField = 0;',
  23686. ' this.$init = function () {',
  23687. ' };',
  23688. ' this.$final = function () {',
  23689. ' };',
  23690. ' this.SetField = function (Value) {',
  23691. ' $mod.THelper.Fly.call({',
  23692. ' p: this,',
  23693. ' get: function () {',
  23694. ' return this.p.FField;',
  23695. ' },',
  23696. ' set: function (v) {',
  23697. ' $mod.TObject.FField = v;',
  23698. ' }',
  23699. ' }, 1);',
  23700. ' $mod.THelper.Fly.call({',
  23701. ' p: this,',
  23702. ' get: function () {',
  23703. ' return this.p.FField;',
  23704. ' },',
  23705. ' set: function (v) {',
  23706. ' $mod.TObject.FField = v;',
  23707. ' }',
  23708. ' }, 2);',
  23709. ' $mod.THelper.Fly.call({',
  23710. ' p: this,',
  23711. ' get: function () {',
  23712. ' return this.p.FField;',
  23713. ' },',
  23714. ' set: function (v) {',
  23715. ' $mod.TObject.FField = v;',
  23716. ' }',
  23717. ' }, 3);',
  23718. ' var $with = this.FField;',
  23719. ' $mod.THelper.Fly.call({',
  23720. ' get: function () {',
  23721. ' return $with;',
  23722. ' },',
  23723. ' set: function (v) {',
  23724. ' $with = v;',
  23725. ' }',
  23726. ' }, 4);',
  23727. ' $mod.THelper.Fly.call({',
  23728. ' p: $mod.TObject,',
  23729. ' get: function () {',
  23730. ' return this.p.FField;',
  23731. ' },',
  23732. ' set: function (v) {',
  23733. ' $mod.TObject.FField = v;',
  23734. ' }',
  23735. ' }, 5);',
  23736. ' var $with1 = $mod.TObject;',
  23737. ' $mod.THelper.Fly.call({',
  23738. ' p: $with1,',
  23739. ' get: function () {',
  23740. ' return this.p.FField;',
  23741. ' },',
  23742. ' set: function (v) {',
  23743. ' $mod.TObject.FField = v;',
  23744. ' }',
  23745. ' }, 6);',
  23746. ' var $with2 = $mod.TObject.FField;',
  23747. ' $mod.THelper.Fly.call({',
  23748. ' get: function () {',
  23749. ' return $with2;',
  23750. ' },',
  23751. ' set: function (v) {',
  23752. ' $with2 = v;',
  23753. ' }',
  23754. ' }, 7);',
  23755. ' };',
  23756. '});',
  23757. 'rtl.createHelper(this, "THelper", null, function () {',
  23758. ' this.Fly = function (n) {',
  23759. ' };',
  23760. '});',
  23761. 'this.o = null;',
  23762. '']),
  23763. LinesToStr([ // $mod.$main
  23764. '$mod.THelper.Fly.call({',
  23765. ' p: $mod.o,',
  23766. ' get: function () {',
  23767. ' return this.p.FField;',
  23768. ' },',
  23769. ' set: function (v) {',
  23770. ' $mod.TObject.FField = v;',
  23771. ' }',
  23772. '}, 11);',
  23773. 'var $with = $mod.o;',
  23774. '$mod.THelper.Fly.call({',
  23775. ' p: $with,',
  23776. ' get: function () {',
  23777. ' return this.p.FField;',
  23778. ' },',
  23779. ' set: function (v) {',
  23780. ' $mod.TObject.FField = v;',
  23781. ' }',
  23782. '}, 12);',
  23783. 'var $with1 = $mod.o.FField;',
  23784. '$mod.THelper.Fly.call({',
  23785. ' get: function () {',
  23786. ' return $with1;',
  23787. ' },',
  23788. ' set: function (v) {',
  23789. ' $with1 = v;',
  23790. ' }',
  23791. '}, 13);',
  23792. '$mod.THelper.Fly.call({',
  23793. ' p: $mod.TObject,',
  23794. ' get: function () {',
  23795. ' return this.p.FField;',
  23796. ' },',
  23797. ' set: function (v) {',
  23798. ' $mod.TObject.FField = v;',
  23799. ' }',
  23800. '}, 14);',
  23801. 'var $with2 = $mod.TObject;',
  23802. '$mod.THelper.Fly.call({',
  23803. ' p: $with2,',
  23804. ' get: function () {',
  23805. ' return this.p.FField;',
  23806. ' },',
  23807. ' set: function (v) {',
  23808. ' $mod.TObject.FField = v;',
  23809. ' }',
  23810. '}, 15);',
  23811. 'var $with3 = $mod.TObject.FField;',
  23812. '$mod.THelper.Fly.call({',
  23813. ' get: function () {',
  23814. ' return $with3;',
  23815. ' },',
  23816. ' set: function (v) {',
  23817. ' $with3 = v;',
  23818. ' }',
  23819. '}, 16);',
  23820. '']));
  23821. end;
  23822. procedure TTestModule.TestTypeHelper_PassClassPropertyGetterStatic;
  23823. begin
  23824. StartProgram(false);
  23825. Add([
  23826. '{$modeswitch typehelpers}',
  23827. 'type',
  23828. ' TObject = class',
  23829. ' class var FField: word;',
  23830. ' class function GetField: word; static;',
  23831. ' class property Field: word read GetField write FField;',
  23832. ' end;',
  23833. ' THelper = type helper for word',
  23834. ' procedure Fly(n: byte);',
  23835. ' end;',
  23836. 'class function TObject.GetField: word;',
  23837. 'begin',
  23838. ' Field.Fly(1);',
  23839. ' TObject.Field.Fly(5);',
  23840. ' with TObject do Field.Fly(6);',
  23841. ' with TObject.Field do Fly(7);',
  23842. 'end;',
  23843. 'procedure THelper.Fly(n: byte);',
  23844. 'begin',
  23845. 'end;',
  23846. 'var',
  23847. ' o: TObject;',
  23848. 'begin',
  23849. ' o.Field.Fly(11);',
  23850. ' with o do Field.Fly(12);',
  23851. ' with o.Field do Fly(13);',
  23852. '']);
  23853. ConvertProgram;
  23854. CheckSource('TestTypeHelper_PassClassPropertyGetterStatic',
  23855. LinesToStr([ // statements
  23856. 'rtl.createClass(this, "TObject", null, function () {',
  23857. ' this.FField = 0;',
  23858. ' this.$init = function () {',
  23859. ' };',
  23860. ' this.$final = function () {',
  23861. ' };',
  23862. ' this.GetField = function () {',
  23863. ' var Result = 0;',
  23864. ' $mod.THelper.Fly.call({',
  23865. ' p: $mod.TObject.GetField(),',
  23866. ' get: function () {',
  23867. ' return this.p;',
  23868. ' },',
  23869. ' set: function (v) {',
  23870. ' this.p = v;',
  23871. ' }',
  23872. ' }, 1);',
  23873. ' $mod.THelper.Fly.call({',
  23874. ' p: $mod.TObject.GetField(),',
  23875. ' get: function () {',
  23876. ' return this.p;',
  23877. ' },',
  23878. ' set: function (v) {',
  23879. ' this.p = v;',
  23880. ' }',
  23881. ' }, 5);',
  23882. ' var $with = $mod.TObject;',
  23883. ' $mod.THelper.Fly.call({',
  23884. ' p: $with.GetField(),',
  23885. ' get: function () {',
  23886. ' return this.p;',
  23887. ' },',
  23888. ' set: function (v) {',
  23889. ' this.p = v;',
  23890. ' }',
  23891. ' }, 6);',
  23892. ' var $with1 = $mod.TObject.GetField();',
  23893. ' $mod.THelper.Fly.call({',
  23894. ' get: function () {',
  23895. ' return $with1;',
  23896. ' },',
  23897. ' set: function (v) {',
  23898. ' $with1 = v;',
  23899. ' }',
  23900. ' }, 7);',
  23901. ' return Result;',
  23902. ' };',
  23903. '});',
  23904. 'rtl.createHelper(this, "THelper", null, function () {',
  23905. ' this.Fly = function (n) {',
  23906. ' };',
  23907. '});',
  23908. 'this.o = null;',
  23909. '']),
  23910. LinesToStr([ // $mod.$main
  23911. '$mod.THelper.Fly.call({',
  23912. ' p: $mod.TObject.GetField(),',
  23913. ' get: function () {',
  23914. ' return this.p;',
  23915. ' },',
  23916. ' set: function (v) {',
  23917. ' this.p = v;',
  23918. ' }',
  23919. '}, 11);',
  23920. 'var $with = $mod.o;',
  23921. '$mod.THelper.Fly.call({',
  23922. ' p: $with.GetField(),',
  23923. ' get: function () {',
  23924. ' return this.p;',
  23925. ' },',
  23926. ' set: function (v) {',
  23927. ' this.p = v;',
  23928. ' }',
  23929. '}, 12);',
  23930. 'var $with1 = $mod.TObject.GetField();',
  23931. '$mod.THelper.Fly.call({',
  23932. ' get: function () {',
  23933. ' return $with1;',
  23934. ' },',
  23935. ' set: function (v) {',
  23936. ' $with1 = v;',
  23937. ' }',
  23938. '}, 13);',
  23939. '']));
  23940. end;
  23941. procedure TTestModule.TestTypeHelper_PassClassPropertyGetterNonStatic;
  23942. begin
  23943. StartProgram(false);
  23944. Add([
  23945. '{$modeswitch typehelpers}',
  23946. 'type',
  23947. ' TObject = class',
  23948. ' class var FField: word;',
  23949. ' class function GetField: word;',
  23950. ' class property Field: word read GetField write FField;',
  23951. ' end;',
  23952. ' TClass = class of TObject;',
  23953. ' THelper = type helper for word',
  23954. ' procedure Fly(n: byte);',
  23955. ' end;',
  23956. 'class function TObject.GetField: word;',
  23957. 'begin',
  23958. ' Field.Fly(1);',
  23959. ' Self.Field.Fly(5);',
  23960. ' with Self do Field.Fly(6);',
  23961. ' with Self.Field do Fly(7);',
  23962. 'end;',
  23963. 'procedure THelper.Fly(n: byte);',
  23964. 'begin',
  23965. 'end;',
  23966. 'var',
  23967. ' o: TObject;',
  23968. ' c: TClass;',
  23969. 'begin',
  23970. ' o.Field.Fly(11);',
  23971. ' with o do Field.Fly(12);',
  23972. ' with o.Field do Fly(13);',
  23973. ' c.Field.Fly(14);',
  23974. ' with c do Field.Fly(15);',
  23975. ' with c.Field do Fly(16);',
  23976. '']);
  23977. ConvertProgram;
  23978. CheckSource('TestTypeHelper_PassClassPropertyGetterNonStatic',
  23979. LinesToStr([ // statements
  23980. 'rtl.createClass(this, "TObject", null, function () {',
  23981. ' this.FField = 0;',
  23982. ' this.$init = function () {',
  23983. ' };',
  23984. ' this.$final = function () {',
  23985. ' };',
  23986. ' this.GetField = function () {',
  23987. ' var Result = 0;',
  23988. ' $mod.THelper.Fly.call({',
  23989. ' p: this.GetField(),',
  23990. ' get: function () {',
  23991. ' return this.p;',
  23992. ' },',
  23993. ' set: function (v) {',
  23994. ' this.p = v;',
  23995. ' }',
  23996. ' }, 1);',
  23997. ' $mod.THelper.Fly.call({',
  23998. ' p: this.GetField(),',
  23999. ' get: function () {',
  24000. ' return this.p;',
  24001. ' },',
  24002. ' set: function (v) {',
  24003. ' this.p = v;',
  24004. ' }',
  24005. ' }, 5);',
  24006. ' $mod.THelper.Fly.call({',
  24007. ' p: this.GetField(),',
  24008. ' get: function () {',
  24009. ' return this.p;',
  24010. ' },',
  24011. ' set: function (v) {',
  24012. ' this.p = v;',
  24013. ' }',
  24014. ' }, 6);',
  24015. ' var $with = this.GetField();',
  24016. ' $mod.THelper.Fly.call({',
  24017. ' get: function () {',
  24018. ' return $with;',
  24019. ' },',
  24020. ' set: function (v) {',
  24021. ' $with = v;',
  24022. ' }',
  24023. ' }, 7);',
  24024. ' return Result;',
  24025. ' };',
  24026. '});',
  24027. 'rtl.createHelper(this, "THelper", null, function () {',
  24028. ' this.Fly = function (n) {',
  24029. ' };',
  24030. '});',
  24031. 'this.o = null;',
  24032. 'this.c = null;',
  24033. '']),
  24034. LinesToStr([ // $mod.$main
  24035. '$mod.THelper.Fly.call({',
  24036. ' p: $mod.o.$class.GetField(),',
  24037. ' get: function () {',
  24038. ' return this.p;',
  24039. ' },',
  24040. ' set: function (v) {',
  24041. ' this.p = v;',
  24042. ' }',
  24043. '}, 11);',
  24044. 'var $with = $mod.o;',
  24045. '$mod.THelper.Fly.call({',
  24046. ' p: $with.$class.GetField(),',
  24047. ' get: function () {',
  24048. ' return this.p;',
  24049. ' },',
  24050. ' set: function (v) {',
  24051. ' this.p = v;',
  24052. ' }',
  24053. '}, 12);',
  24054. 'var $with1 = $mod.o.$class.GetField();',
  24055. '$mod.THelper.Fly.call({',
  24056. ' get: function () {',
  24057. ' return $with1;',
  24058. ' },',
  24059. ' set: function (v) {',
  24060. ' $with1 = v;',
  24061. ' }',
  24062. '}, 13);',
  24063. '$mod.THelper.Fly.call({',
  24064. ' p: $mod.c.GetField(),',
  24065. ' get: function () {',
  24066. ' return this.p;',
  24067. ' },',
  24068. ' set: function (v) {',
  24069. ' this.p = v;',
  24070. ' }',
  24071. '}, 14);',
  24072. 'var $with2 = $mod.c;',
  24073. '$mod.THelper.Fly.call({',
  24074. ' p: $with2.GetField(),',
  24075. ' get: function () {',
  24076. ' return this.p;',
  24077. ' },',
  24078. ' set: function (v) {',
  24079. ' this.p = v;',
  24080. ' }',
  24081. '}, 15);',
  24082. 'var $with3 = $mod.c.GetField();',
  24083. '$mod.THelper.Fly.call({',
  24084. ' get: function () {',
  24085. ' return $with3;',
  24086. ' },',
  24087. ' set: function (v) {',
  24088. ' $with3 = v;',
  24089. ' }',
  24090. '}, 16);',
  24091. '']));
  24092. end;
  24093. procedure TTestModule.TestTypeHelper_Property;
  24094. begin
  24095. StartProgram(false);
  24096. Add([
  24097. '{$modeswitch typehelpers}',
  24098. 'type',
  24099. ' THelper = type helper for word',
  24100. ' function GetSize: longint;',
  24101. ' procedure SetSize(Value: longint);',
  24102. ' property Size: longint read GetSize write SetSize;',
  24103. ' end;',
  24104. 'function THelper.GetSize: longint;',
  24105. 'begin',
  24106. ' Result:=Size+1;',
  24107. ' Size:=2;',
  24108. ' Result:=Self.Size+3;',
  24109. ' Self.Size:=4;',
  24110. ' with Self do begin',
  24111. ' Result:=Size+5;',
  24112. ' Size:=6;',
  24113. ' end;',
  24114. 'end;',
  24115. 'procedure THelper.SetSize(Value: longint);',
  24116. 'begin',
  24117. 'end;',
  24118. 'var w: word;',
  24119. 'begin',
  24120. ' w:=w.Size+7;',
  24121. ' w.Size:=w+8;',
  24122. ' with w do begin',
  24123. ' w:=Size+9;',
  24124. ' Size:=w+10;',
  24125. ' end;',
  24126. '']);
  24127. ConvertProgram;
  24128. CheckSource('TestTypeHelper_Property',
  24129. LinesToStr([ // statements
  24130. 'rtl.createHelper(this, "THelper", null, function () {',
  24131. ' this.GetSize = function () {',
  24132. ' var Result = 0;',
  24133. ' Result = $mod.THelper.GetSize.call(this) + 1;',
  24134. ' $mod.THelper.SetSize.call(this, 2);',
  24135. ' Result = $mod.THelper.GetSize.call(this) + 3;',
  24136. ' $mod.THelper.SetSize.call(this, 4);',
  24137. ' var $with = this.get();',
  24138. ' Result = $mod.THelper.GetSize.call(this) + 5;',
  24139. ' $mod.THelper.SetSize.call(this, 6);',
  24140. ' return Result;',
  24141. ' };',
  24142. ' this.SetSize = function (Value) {',
  24143. ' };',
  24144. '});',
  24145. 'this.w = 0;',
  24146. '']),
  24147. LinesToStr([ // $mod.$main
  24148. '$mod.w = $mod.THelper.GetSize.call({',
  24149. ' p: $mod,',
  24150. ' get: function () {',
  24151. ' return this.p.w;',
  24152. ' },',
  24153. ' set: function (v) {',
  24154. ' this.p.w = v;',
  24155. ' }',
  24156. '}) + 7;',
  24157. '$mod.THelper.SetSize.call({',
  24158. ' p: $mod,',
  24159. ' get: function () {',
  24160. ' return this.p.w;',
  24161. ' },',
  24162. ' set: function (v) {',
  24163. ' this.p.w = v;',
  24164. ' }',
  24165. '}, $mod.w + 8);',
  24166. 'var $with = $mod.w;',
  24167. '$mod.w = $mod.THelper.GetSize.call({',
  24168. ' get: function () {',
  24169. ' return $with;',
  24170. ' },',
  24171. ' set: function (v) {',
  24172. ' $with = v;',
  24173. ' }',
  24174. '}) + 9;',
  24175. '$mod.THelper.SetSize.call({',
  24176. ' get: function () {',
  24177. ' return $with;',
  24178. ' },',
  24179. ' set: function (v) {',
  24180. ' $with = v;',
  24181. ' }',
  24182. '}, $mod.w + 10);',
  24183. '']));
  24184. end;
  24185. procedure TTestModule.TestTypeHelper_Property_Array;
  24186. begin
  24187. StartProgram(false);
  24188. Add([
  24189. '{$modeswitch typehelpers}',
  24190. 'type',
  24191. ' THelper = type helper for word',
  24192. ' function GetItems(Index: byte): boolean;',
  24193. ' procedure SetItems(Index: byte; Value: boolean);',
  24194. ' property Items[Index: byte]: boolean read GetItems write SetItems;',
  24195. ' end;',
  24196. 'function THelper.GetItems(Index: byte): boolean;',
  24197. 'begin',
  24198. ' Result:=Items[1];',
  24199. ' Items[2]:=false;',
  24200. ' Result:=Self.Items[3];',
  24201. ' Self.Items[4]:=true;',
  24202. ' with Self do begin',
  24203. ' Result:=Items[5];',
  24204. ' Items[6]:=false;',
  24205. ' end;',
  24206. 'end;',
  24207. 'procedure THelper.SetItems(Index: byte; Value: boolean);',
  24208. 'begin',
  24209. 'end;',
  24210. 'var',
  24211. ' w: word;',
  24212. ' b: boolean;',
  24213. 'begin',
  24214. ' b:=w.Items[1];',
  24215. ' w.Items[2]:=b;',
  24216. ' with w do begin',
  24217. ' b:=Items[3];',
  24218. ' Items[4]:=b;',
  24219. ' end;',
  24220. '']);
  24221. ConvertProgram;
  24222. CheckSource('TestTypeHelper_Property_Array',
  24223. LinesToStr([ // statements
  24224. 'rtl.createHelper(this, "THelper", null, function () {',
  24225. ' this.GetItems = function (Index) {',
  24226. ' var Result = false;',
  24227. ' Result = $mod.THelper.GetItems.call(this, 1);',
  24228. ' $mod.THelper.SetItems.call(this, 2, false);',
  24229. ' Result = $mod.THelper.GetItems.call(this, 3);',
  24230. ' $mod.THelper.SetItems.call(this, 4, true);',
  24231. ' var $with = this.get();',
  24232. ' Result = $mod.THelper.GetItems.call(this, 5);',
  24233. ' $mod.THelper.SetItems.call(this, 6, false);',
  24234. ' return Result;',
  24235. ' };',
  24236. ' this.SetItems = function (Index, Value) {',
  24237. ' };',
  24238. '});',
  24239. 'this.w = 0;',
  24240. 'this.b = false;',
  24241. '']),
  24242. LinesToStr([ // $mod.$main
  24243. '$mod.b = $mod.THelper.GetItems.call({',
  24244. ' p: $mod,',
  24245. ' get: function () {',
  24246. ' return this.p.w;',
  24247. ' },',
  24248. ' set: function (v) {',
  24249. ' this.p.w = v;',
  24250. ' }',
  24251. '}, 1);',
  24252. '$mod.THelper.SetItems.call({',
  24253. ' p: $mod,',
  24254. ' get: function () {',
  24255. ' return this.p.w;',
  24256. ' },',
  24257. ' set: function (v) {',
  24258. ' this.p.w = v;',
  24259. ' }',
  24260. '}, 2, $mod.b);',
  24261. 'var $with = $mod.w;',
  24262. '$mod.b = $mod.THelper.GetItems.call({',
  24263. ' get: function () {',
  24264. ' return $with;',
  24265. ' },',
  24266. ' set: function (v) {',
  24267. ' $with = v;',
  24268. ' }',
  24269. '}, 3);',
  24270. '$mod.THelper.SetItems.call({',
  24271. ' get: function () {',
  24272. ' return $with;',
  24273. ' },',
  24274. ' set: function (v) {',
  24275. ' $with = v;',
  24276. ' }',
  24277. '}, 4, $mod.b);',
  24278. '']));
  24279. end;
  24280. procedure TTestModule.TestTypeHelper_ClassProperty;
  24281. begin
  24282. StartProgram(false);
  24283. Add([
  24284. '{$modeswitch typehelpers}',
  24285. 'type',
  24286. ' THelper = type helper for word',
  24287. ' class function GetSize: longint; static;',
  24288. ' class procedure SetSize(Value: longint); static;',
  24289. ' class property Size: longint read GetSize write SetSize;',
  24290. ' end;',
  24291. 'class function THelper.GetSize: longint;',
  24292. 'begin',
  24293. ' Result:=Size+1;',
  24294. ' Size:=2;',
  24295. 'end;',
  24296. 'class procedure THelper.SetSize(Value: longint);',
  24297. 'begin',
  24298. 'end;',
  24299. 'begin',
  24300. '']);
  24301. ConvertProgram;
  24302. CheckSource('TestTypeHelper_ClassProperty',
  24303. LinesToStr([ // statements
  24304. 'rtl.createHelper(this, "THelper", null, function () {',
  24305. ' this.GetSize = function () {',
  24306. ' var Result = 0;',
  24307. ' Result = $mod.THelper.GetSize() + 1;',
  24308. ' $mod.THelper.SetSize(2);',
  24309. ' return Result;',
  24310. ' };',
  24311. ' this.SetSize = function (Value) {',
  24312. ' };',
  24313. '});',
  24314. '']),
  24315. LinesToStr([ // $mod.$main
  24316. '']));
  24317. end;
  24318. procedure TTestModule.TestTypeHelper_ClassProperty_Array;
  24319. begin
  24320. StartProgram(false);
  24321. Add([
  24322. '{$modeswitch typehelpers}',
  24323. 'type',
  24324. ' THelper = type helper for word',
  24325. ' class function GetItems(Index: byte): boolean; static;',
  24326. ' class procedure SetItems(Index: byte; Value: boolean); static;',
  24327. ' class property Items[Index: byte]: boolean read GetItems write SetItems;',
  24328. ' end;',
  24329. 'class function THelper.GetItems(Index: byte): boolean;',
  24330. 'begin',
  24331. ' Result:=Items[1];',
  24332. ' Items[2]:=false;',
  24333. 'end;',
  24334. 'class procedure THelper.SetItems(Index: byte; Value: boolean);',
  24335. 'begin',
  24336. 'end;',
  24337. 'var',
  24338. ' w: word;',
  24339. ' b: boolean;',
  24340. 'begin',
  24341. ' b:=w.Items[1];',
  24342. ' w.Items[2]:=b;',
  24343. ' with w do begin',
  24344. ' b:=Items[3];',
  24345. ' Items[4]:=b;',
  24346. ' end;',
  24347. '']);
  24348. ConvertProgram;
  24349. CheckSource('TestTypeHelper_ClassProperty_Array',
  24350. LinesToStr([ // statements
  24351. 'rtl.createHelper(this, "THelper", null, function () {',
  24352. ' this.GetItems = function (Index) {',
  24353. ' var Result = false;',
  24354. ' Result = $mod.THelper.GetItems(1);',
  24355. ' $mod.THelper.SetItems(2, false);',
  24356. ' return Result;',
  24357. ' };',
  24358. ' this.SetItems = function (Index, Value) {',
  24359. ' };',
  24360. '});',
  24361. 'this.w = 0;',
  24362. 'this.b = false;',
  24363. '']),
  24364. LinesToStr([ // $mod.$main
  24365. '$mod.b = $mod.THelper.GetItems(1);',
  24366. '$mod.THelper.SetItems(2, $mod.b);',
  24367. 'var $with = $mod.w;',
  24368. '$mod.b = $mod.THelper.GetItems(3);',
  24369. '$mod.THelper.SetItems(4, $mod.b);',
  24370. '']));
  24371. end;
  24372. procedure TTestModule.TestTypeHelper_ClassMethod;
  24373. begin
  24374. StartProgram(false);
  24375. Add([
  24376. '{$modeswitch typehelpers}',
  24377. 'type',
  24378. ' THelper = type helper for word',
  24379. ' class procedure DoStatic; static;',
  24380. ' end;',
  24381. 'class procedure THelper.DoStatic;',
  24382. 'begin',
  24383. ' DoStatic;',
  24384. ' DoStatic();',
  24385. 'end;',
  24386. 'var w: word;',
  24387. 'begin',
  24388. ' w.DoStatic;',
  24389. ' w.DoStatic();',
  24390. '']);
  24391. ConvertProgram;
  24392. CheckSource('TestTypeHelper_ClassMethod',
  24393. LinesToStr([ // statements
  24394. 'rtl.createHelper(this, "THelper", null, function () {',
  24395. ' this.DoStatic = function () {',
  24396. ' $mod.THelper.DoStatic();',
  24397. ' $mod.THelper.DoStatic();',
  24398. ' };',
  24399. '});',
  24400. 'this.w = 0;',
  24401. '']),
  24402. LinesToStr([ // $mod.$main
  24403. '$mod.THelper.DoStatic();',
  24404. '$mod.THelper.DoStatic();',
  24405. '']));
  24406. end;
  24407. procedure TTestModule.TestTypeHelper_ExtClassMethodFail;
  24408. begin
  24409. StartProgram(false);
  24410. Add([
  24411. '{$modeswitch typehelpers}',
  24412. 'type',
  24413. ' THelper = type helper for word',
  24414. ' procedure Run; external name ''Run'';',
  24415. ' end;',
  24416. 'var w: word;',
  24417. 'begin',
  24418. ' w.Run;',
  24419. '']);
  24420. SetExpectedPasResolverError('Not supported: external method in type helper',nNotSupportedX);
  24421. ConvertProgram;
  24422. end;
  24423. procedure TTestModule.TestTypeHelper_Constructor;
  24424. begin
  24425. StartProgram(false);
  24426. Add([
  24427. '{$modeswitch typehelpers}',
  24428. 'type',
  24429. ' THelper = type helper for word',
  24430. ' constructor Init(e: longint);',
  24431. ' end;',
  24432. 'constructor THelper.Init(e: longint);',
  24433. 'begin',
  24434. ' Self:=e;',
  24435. ' Init(e+1);',
  24436. 'end;',
  24437. 'var w: word;',
  24438. 'begin',
  24439. ' w:=word.Init(2);',
  24440. ' w:=w.Init(3);',
  24441. ' with word do w:=Init(4);',
  24442. ' with w do w:=Init(5);',
  24443. '']);
  24444. ConvertProgram;
  24445. CheckSource('TestTypeHelper_Constructor',
  24446. LinesToStr([ // statements
  24447. 'rtl.createHelper(this, "THelper", null, function () {',
  24448. ' this.Init = function (e) {',
  24449. ' this.set(e);',
  24450. ' $mod.THelper.Init.call(this, e + 1);',
  24451. ' return this.get();',
  24452. ' };',
  24453. ' this.$new = function (fn, args) {',
  24454. ' return this[fn].apply({',
  24455. ' p: 0,',
  24456. ' get: function () {',
  24457. ' return this.p;',
  24458. ' },',
  24459. ' set: function (v) {',
  24460. ' this.p = v;',
  24461. ' }',
  24462. ' }, args);',
  24463. ' };',
  24464. '});',
  24465. 'this.w = 0;',
  24466. '']),
  24467. LinesToStr([ // $mod.$main
  24468. '$mod.w = $mod.THelper.$new("Init", [2]);',
  24469. '$mod.w = $mod.THelper.Init.call({',
  24470. ' p: $mod,',
  24471. ' get: function () {',
  24472. ' return this.p.w;',
  24473. ' },',
  24474. ' set: function (v) {',
  24475. ' this.p.w = v;',
  24476. ' }',
  24477. '}, 3);',
  24478. '$mod.w = $mod.THelper.$new("Init", [4]);',
  24479. 'var $with = $mod.w;',
  24480. '$mod.w = $mod.THelper.Init.call({',
  24481. ' get: function () {',
  24482. ' return $with;',
  24483. ' },',
  24484. ' set: function (v) {',
  24485. ' $with = v;',
  24486. ' }',
  24487. '}, 5);',
  24488. '']));
  24489. end;
  24490. procedure TTestModule.TestTypeHelper_Word;
  24491. begin
  24492. StartProgram(false);
  24493. Add([
  24494. '{$modeswitch typehelpers}',
  24495. 'type',
  24496. ' THelper = type helper for word',
  24497. ' procedure DoIt(e: byte = 123);',
  24498. ' end;',
  24499. 'procedure THelper.DoIt(e: byte);',
  24500. 'begin',
  24501. ' Self:=e;',
  24502. ' Self:=Self+1;',
  24503. ' with Self do Doit;',
  24504. 'end;',
  24505. 'begin',
  24506. ' word(3).DoIt;',
  24507. '']);
  24508. ConvertProgram;
  24509. CheckSource('TestTypeHelper_Word',
  24510. LinesToStr([ // statements
  24511. 'rtl.createHelper(this, "THelper", null, function () {',
  24512. ' this.DoIt = function (e) {',
  24513. ' this.set(e);',
  24514. ' this.set(this.get() + 1);',
  24515. ' var $with = this.get();',
  24516. ' $mod.THelper.DoIt.call(this, 123);',
  24517. ' };',
  24518. '});',
  24519. '']),
  24520. LinesToStr([ // $mod.$main
  24521. '$mod.THelper.DoIt.call({',
  24522. ' get: function () {',
  24523. ' return 3;',
  24524. ' },',
  24525. ' set: function (v) {',
  24526. ' rtl.raiseE("EPropReadOnly");',
  24527. ' }',
  24528. '}, 123);',
  24529. '']));
  24530. end;
  24531. procedure TTestModule.TestTypeHelper_Boolean;
  24532. begin
  24533. StartProgram(false);
  24534. Add([
  24535. '{$modeswitch typehelpers}',
  24536. 'type',
  24537. ' Integer = longint;',
  24538. ' THelper = type helper for boolean',
  24539. ' procedure Run(e: wordbool = true);',
  24540. ' end;',
  24541. 'procedure THelper.Run(e: wordbool);',
  24542. 'begin',
  24543. ' Self:=e;',
  24544. ' Self:=not Self;',
  24545. ' with Self do Run;',
  24546. ' if Integer(Self)=0 then ;',
  24547. 'end;',
  24548. 'begin',
  24549. ' boolean(3).Run;',
  24550. '']);
  24551. ConvertProgram;
  24552. CheckSource('TestTypeHelper_Boolean',
  24553. LinesToStr([ // statements
  24554. 'rtl.createHelper(this, "THelper", null, function () {',
  24555. ' this.Run = function (e) {',
  24556. ' this.set(e);',
  24557. ' this.set(!this.get());',
  24558. ' var $with = this.get();',
  24559. ' $mod.THelper.Run.call(this, true);',
  24560. ' if ((this.get() ? 1 : 0) === 0) ;',
  24561. ' };',
  24562. '});',
  24563. '']),
  24564. LinesToStr([ // $mod.$main
  24565. '$mod.THelper.Run.call({',
  24566. ' a: 3 != 0,',
  24567. ' get: function () {',
  24568. ' return this.a;',
  24569. ' },',
  24570. ' set: function (v) {',
  24571. ' rtl.raiseE("EPropReadOnly");',
  24572. ' }',
  24573. '}, true);',
  24574. '']));
  24575. end;
  24576. procedure TTestModule.TestTypeHelper_WordBool;
  24577. begin
  24578. StartProgram(false);
  24579. Add([
  24580. '{$modeswitch typehelpers}',
  24581. 'type',
  24582. ' Integer = longint;',
  24583. ' THelper = type helper for WordBool',
  24584. ' procedure Run(e: wordbool = true);',
  24585. ' end;',
  24586. 'procedure THelper.Run(e: wordbool);',
  24587. 'var i: integer;',
  24588. 'begin',
  24589. ' i:=Integer(Self);',
  24590. 'end;',
  24591. 'var w: wordbool;',
  24592. 'begin',
  24593. ' w.Run;',
  24594. ' wordbool(3).Run;',
  24595. '']);
  24596. ConvertProgram;
  24597. CheckSource('TestTypeHelper_WordBool',
  24598. LinesToStr([ // statements
  24599. 'rtl.createHelper(this, "THelper", null, function () {',
  24600. ' this.Run = function (e) {',
  24601. ' var i = 0;',
  24602. ' i = (this.get() ? 1 : 0);',
  24603. ' };',
  24604. '});',
  24605. 'this.w = false;',
  24606. '']),
  24607. LinesToStr([ // $mod.$main
  24608. '$mod.THelper.Run.call({',
  24609. ' p: $mod,',
  24610. ' get: function () {',
  24611. ' return this.p.w;',
  24612. ' },',
  24613. ' set: function (v) {',
  24614. ' this.p.w = v;',
  24615. ' }',
  24616. '}, true);',
  24617. '$mod.THelper.Run.call({',
  24618. ' a: 3 != 0,',
  24619. ' get: function () {',
  24620. ' return this.a;',
  24621. ' },',
  24622. ' set: function (v) {',
  24623. ' rtl.raiseE("EPropReadOnly");',
  24624. ' }',
  24625. '}, true);',
  24626. '']));
  24627. end;
  24628. procedure TTestModule.TestTypeHelper_Double;
  24629. begin
  24630. StartProgram(false);
  24631. Add([
  24632. '{$modeswitch typehelpers}',
  24633. 'type',
  24634. ' Float = type double;',
  24635. ' THelper = type helper for Float',
  24636. ' const NPI = 3.141592;',
  24637. ' function ToStr: String;',
  24638. ' end;',
  24639. 'function THelper.ToStr: String;',
  24640. 'begin',
  24641. 'end;',
  24642. 'procedure DoIt(s: string);',
  24643. 'begin',
  24644. 'end;',
  24645. 'var f: Float;',
  24646. 'begin',
  24647. ' DoIt(f.toStr);',
  24648. ' DoIt(f.toStr());',
  24649. ' (f*f).toStr;',
  24650. ' DoIt((f*f).toStr);',
  24651. '']);
  24652. ConvertProgram;
  24653. CheckSource('TestTypeHelper_Double',
  24654. LinesToStr([ // statements
  24655. 'rtl.createHelper(this, "THelper", null, function () {',
  24656. ' this.NPI = 3.141592;',
  24657. ' this.ToStr = function () {',
  24658. ' var Result = "";',
  24659. ' return Result;',
  24660. ' };',
  24661. '});',
  24662. 'this.DoIt = function (s) {',
  24663. '};',
  24664. 'this.f = 0.0;',
  24665. '']),
  24666. LinesToStr([ // $mod.$main
  24667. '$mod.DoIt($mod.THelper.ToStr.call({',
  24668. ' p: $mod,',
  24669. ' get: function () {',
  24670. ' return this.p.f;',
  24671. ' },',
  24672. ' set: function (v) {',
  24673. ' this.p.f = v;',
  24674. ' }',
  24675. '}));',
  24676. '$mod.DoIt($mod.THelper.ToStr.call({',
  24677. ' p: $mod,',
  24678. ' get: function () {',
  24679. ' return this.p.f;',
  24680. ' },',
  24681. ' set: function (v) {',
  24682. ' this.p.f = v;',
  24683. ' }',
  24684. '}));',
  24685. '$mod.THelper.ToStr.call({',
  24686. ' a: $mod.f * $mod.f,',
  24687. ' get: function () {',
  24688. ' return this.a;',
  24689. ' },',
  24690. ' set: function (v) {',
  24691. ' rtl.raiseE("EPropReadOnly");',
  24692. ' }',
  24693. '});',
  24694. '$mod.DoIt($mod.THelper.ToStr.call({',
  24695. ' a: $mod.f * $mod.f,',
  24696. ' get: function () {',
  24697. ' return this.a;',
  24698. ' },',
  24699. ' set: function (v) {',
  24700. ' rtl.raiseE("EPropReadOnly");',
  24701. ' }',
  24702. '}));',
  24703. '']));
  24704. end;
  24705. procedure TTestModule.TestTypeHelper_NativeInt;
  24706. begin
  24707. StartProgram(false);
  24708. Add([
  24709. '{$modeswitch typehelpers}',
  24710. 'type',
  24711. ' MaxInt = type nativeint;',
  24712. ' THelperI = type helper for MaxInt',
  24713. ' function ToStr: String;',
  24714. ' end;',
  24715. ' MaxUInt = type nativeuint;',
  24716. ' THelperU = type helper for MaxUInt',
  24717. ' function ToStr: String;',
  24718. ' end;',
  24719. 'function THelperI.ToStr: String;',
  24720. 'begin',
  24721. ' Result:=str(Self);',
  24722. 'end;',
  24723. 'function THelperU.ToStr: String;',
  24724. 'begin',
  24725. ' Result:=str(Self);',
  24726. 'end;',
  24727. 'procedure DoIt(s: string);',
  24728. 'begin',
  24729. 'end;',
  24730. 'var i: MaxInt;',
  24731. 'begin',
  24732. ' DoIt(i.toStr);',
  24733. ' DoIt(i.toStr());',
  24734. ' (i*i).toStr;',
  24735. ' DoIt((i*i).toStr);',
  24736. '']);
  24737. ConvertProgram;
  24738. CheckSource('TestTypeHelper_NativeInt',
  24739. LinesToStr([ // statements
  24740. 'rtl.createHelper(this, "THelperI", null, function () {',
  24741. ' this.ToStr = function () {',
  24742. ' var Result = "";',
  24743. ' Result = "" + this.get();',
  24744. ' return Result;',
  24745. ' };',
  24746. '});',
  24747. 'rtl.createHelper(this, "THelperU", null, function () {',
  24748. ' this.ToStr = function () {',
  24749. ' var Result = "";',
  24750. ' Result = "" + this.get();',
  24751. ' return Result;',
  24752. ' };',
  24753. '});',
  24754. 'this.DoIt = function (s) {',
  24755. '};',
  24756. 'this.i = 0;',
  24757. '']),
  24758. LinesToStr([ // $mod.$main
  24759. '$mod.DoIt($mod.THelperI.ToStr.call({',
  24760. ' p: $mod,',
  24761. ' get: function () {',
  24762. ' return this.p.i;',
  24763. ' },',
  24764. ' set: function (v) {',
  24765. ' this.p.i = v;',
  24766. ' }',
  24767. '}));',
  24768. '$mod.DoIt($mod.THelperI.ToStr.call({',
  24769. ' p: $mod,',
  24770. ' get: function () {',
  24771. ' return this.p.i;',
  24772. ' },',
  24773. ' set: function (v) {',
  24774. ' this.p.i = v;',
  24775. ' }',
  24776. '}));',
  24777. '$mod.THelperI.ToStr.call({',
  24778. ' a: $mod.i * $mod.i,',
  24779. ' get: function () {',
  24780. ' return this.a;',
  24781. ' },',
  24782. ' set: function (v) {',
  24783. ' rtl.raiseE("EPropReadOnly");',
  24784. ' }',
  24785. '});',
  24786. '$mod.DoIt($mod.THelperI.ToStr.call({',
  24787. ' a: $mod.i * $mod.i,',
  24788. ' get: function () {',
  24789. ' return this.a;',
  24790. ' },',
  24791. ' set: function (v) {',
  24792. ' rtl.raiseE("EPropReadOnly");',
  24793. ' }',
  24794. '}));',
  24795. '']));
  24796. end;
  24797. procedure TTestModule.TestTypeHelper_StringChar;
  24798. begin
  24799. StartProgram(false);
  24800. Add([
  24801. '{$modeswitch typehelpers}',
  24802. 'type',
  24803. ' TStringHelper = type helper for string',
  24804. ' procedure DoIt(e: byte = 123);',
  24805. ' end;',
  24806. ' TCharHelper = type helper for char',
  24807. ' procedure Fly;',
  24808. ' end;',
  24809. 'procedure TStringHelper.DoIt(e: byte);',
  24810. 'begin',
  24811. ' Self[1]:=''c'';',
  24812. ' Self[2]:=Self[3];',
  24813. 'end;',
  24814. 'procedure TCharHelper.Fly;',
  24815. 'begin',
  24816. ' Self:=''c'';',
  24817. 'end;',
  24818. 'begin',
  24819. ' ''abc''.DoIt;',
  24820. ' ''xyz''.DoIt();',
  24821. ' ''c''.Fly();',
  24822. '']);
  24823. ConvertProgram;
  24824. CheckSource('TestTypeHelper_StringChar',
  24825. LinesToStr([ // statements
  24826. 'rtl.createHelper(this, "TStringHelper", null, function () {',
  24827. ' this.DoIt = function (e) {',
  24828. ' this.set(rtl.setCharAt(this.get(), 0, "c"));',
  24829. ' this.set(rtl.setCharAt(this.get(), 1, this.get().charAt(2)));',
  24830. ' };',
  24831. '});',
  24832. 'rtl.createHelper(this, "TCharHelper", null, function () {',
  24833. ' this.Fly = function () {',
  24834. ' this.set("c");',
  24835. ' };',
  24836. '});',
  24837. '']),
  24838. LinesToStr([ // $mod.$main
  24839. '$mod.TStringHelper.DoIt.call({',
  24840. ' get: function () {',
  24841. ' return "abc";',
  24842. ' },',
  24843. ' set: function (v) {',
  24844. ' rtl.raiseE("EPropReadOnly");',
  24845. ' }',
  24846. '}, 123);',
  24847. '$mod.TStringHelper.DoIt.call({',
  24848. ' get: function () {',
  24849. ' return "xyz";',
  24850. ' },',
  24851. ' set: function (v) {',
  24852. ' rtl.raiseE("EPropReadOnly");',
  24853. ' }',
  24854. '}, 123);',
  24855. '$mod.TCharHelper.Fly.call({',
  24856. ' get: function () {',
  24857. ' return "c";',
  24858. ' },',
  24859. ' set: function (v) {',
  24860. ' rtl.raiseE("EPropReadOnly");',
  24861. ' }',
  24862. '});',
  24863. '']));
  24864. end;
  24865. procedure TTestModule.TestTypeHelper_JSValue;
  24866. begin
  24867. StartProgram(false);
  24868. Add([
  24869. '{$modeswitch typehelpers}',
  24870. 'type',
  24871. ' TExtValue = type jsvalue;',
  24872. ' THelper = type helper for TExtValue',
  24873. ' function ToStr: String;',
  24874. ' end;',
  24875. 'function THelper.ToStr: String;',
  24876. 'begin',
  24877. 'end;',
  24878. 'var',
  24879. ' s: string;',
  24880. ' v: TExtValue;',
  24881. 'begin',
  24882. ' s:=v.toStr;',
  24883. ' s:=v.toStr();',
  24884. ' TExtValue(s).toStr;',
  24885. '']);
  24886. ConvertProgram;
  24887. CheckSource('TestTypeHelper_JSValue',
  24888. LinesToStr([ // statements
  24889. 'rtl.createHelper(this, "THelper", null, function () {',
  24890. ' this.ToStr = function () {',
  24891. ' var Result = "";',
  24892. ' return Result;',
  24893. ' };',
  24894. '});',
  24895. 'this.s = "";',
  24896. 'this.v = undefined;',
  24897. '']),
  24898. LinesToStr([ // $mod.$main
  24899. '$mod.s = $mod.THelper.ToStr.call({',
  24900. ' p: $mod,',
  24901. ' get: function () {',
  24902. ' return this.p.v;',
  24903. ' },',
  24904. ' set: function (v) {',
  24905. ' this.p.v = v;',
  24906. ' }',
  24907. '});',
  24908. '$mod.s = $mod.THelper.ToStr.call({',
  24909. ' p: $mod,',
  24910. ' get: function () {',
  24911. ' return this.p.v;',
  24912. ' },',
  24913. ' set: function (v) {',
  24914. ' this.p.v = v;',
  24915. ' }',
  24916. '});',
  24917. '$mod.THelper.ToStr.call({',
  24918. ' p: $mod,',
  24919. ' get: function () {',
  24920. ' return this.p.s;',
  24921. ' },',
  24922. ' set: function (v) {',
  24923. ' rtl.raiseE("EPropReadOnly");',
  24924. ' }',
  24925. '});',
  24926. '']));
  24927. end;
  24928. procedure TTestModule.TestTypeHelper_Array;
  24929. begin
  24930. StartProgram(false);
  24931. Add([
  24932. '{$modeswitch typehelpers}',
  24933. 'type',
  24934. ' TArrOfBool = array of boolean;',
  24935. ' TArrOfJS = array of jsvalue;',
  24936. ' THelper = type helper for TArrOfBool',
  24937. ' procedure DoIt(e: byte = 123);',
  24938. ' end;',
  24939. 'procedure THelper.DoIt(e: byte);',
  24940. 'begin',
  24941. ' Self[1]:=true;',
  24942. ' Self[2]:=not Self[3];',
  24943. ' SetLength(Self,4);',
  24944. 'end;',
  24945. 'var',
  24946. ' b: TArrOfBool;',
  24947. ' j: TArrOfJS;',
  24948. 'begin',
  24949. ' b.DoIt;',
  24950. ' TArrOfBool(j).DoIt();',
  24951. '']);
  24952. ConvertProgram;
  24953. CheckSource('TestTypeHelper_Array',
  24954. LinesToStr([ // statements
  24955. 'rtl.createHelper(this, "THelper", null, function () {',
  24956. ' this.DoIt = function (e) {',
  24957. ' this.get()[1] = true;',
  24958. ' this.get()[2] = !this.get()[3];',
  24959. ' this.set(rtl.arraySetLength(this.get(), false, 4));',
  24960. ' };',
  24961. '});',
  24962. 'this.b = [];',
  24963. 'this.j = [];',
  24964. '']),
  24965. LinesToStr([ // $mod.$main
  24966. '$mod.THelper.DoIt.call({',
  24967. ' p: $mod,',
  24968. ' get: function () {',
  24969. ' return this.p.b;',
  24970. ' },',
  24971. ' set: function (v) {',
  24972. ' this.p.b = v;',
  24973. ' }',
  24974. '}, 123);',
  24975. '$mod.THelper.DoIt.call({',
  24976. ' p: $mod,',
  24977. ' get: function () {',
  24978. ' return this.p.j;',
  24979. ' },',
  24980. ' set: function (v) {',
  24981. ' this.p.j = v;',
  24982. ' }',
  24983. '}, 123);',
  24984. '']));
  24985. end;
  24986. procedure TTestModule.TestTypeHelper_EnumType;
  24987. begin
  24988. StartProgram(false);
  24989. Add([
  24990. '{$modeswitch typehelpers}',
  24991. 'type',
  24992. ' TEnum = (red,blue);',
  24993. ' THelper = type helper for TEnum',
  24994. ' procedure DoIt(e: byte = 123);',
  24995. ' class procedure Swing(w: word); static;',
  24996. ' end;',
  24997. 'procedure THelper.DoIt(e: byte);',
  24998. 'begin',
  24999. ' Self:=red;',
  25000. ' Self:=succ(Self);',
  25001. ' with Self do Doit;',
  25002. 'end;',
  25003. 'class procedure THelper.Swing(w: word);',
  25004. 'begin',
  25005. 'end;',
  25006. 'var e: TEnum;',
  25007. 'begin',
  25008. ' e.DoIt;',
  25009. ' red.DoIt;',
  25010. ' TEnum.blue.DoIt;',
  25011. ' TEnum(1).DoIt;',
  25012. ' TEnum.Swing(3);',
  25013. '']);
  25014. ConvertProgram;
  25015. CheckSource('TestTypeHelper_EnumType',
  25016. LinesToStr([ // statements
  25017. 'this.TEnum = {',
  25018. ' "0": "red",',
  25019. ' red: 0,',
  25020. ' "1": "blue",',
  25021. ' blue: 1',
  25022. '};',
  25023. 'rtl.createHelper(this, "THelper", null, function () {',
  25024. ' this.DoIt = function (e) {',
  25025. ' this.set($mod.TEnum.red);',
  25026. ' this.set(this.get() + 1);',
  25027. ' var $with = this.get();',
  25028. ' $mod.THelper.DoIt.call(this, 123);',
  25029. ' };',
  25030. ' this.Swing = function (w) {',
  25031. ' };',
  25032. '});',
  25033. 'this.e = 0;',
  25034. '']),
  25035. LinesToStr([ // $mod.$main
  25036. '$mod.THelper.DoIt.call({',
  25037. ' p: $mod,',
  25038. ' get: function () {',
  25039. ' return this.p.e;',
  25040. ' },',
  25041. ' set: function (v) {',
  25042. ' this.p.e = v;',
  25043. ' }',
  25044. '}, 123);',
  25045. '$mod.THelper.DoIt.call({',
  25046. ' p: $mod.TEnum,',
  25047. ' get: function () {',
  25048. ' return this.p.red;',
  25049. ' },',
  25050. ' set: function (v) {',
  25051. ' rtl.raiseE("EPropReadOnly");',
  25052. ' }',
  25053. '}, 123);',
  25054. '$mod.THelper.DoIt.call({',
  25055. ' p: $mod.TEnum,',
  25056. ' get: function () {',
  25057. ' return this.p.blue;',
  25058. ' },',
  25059. ' set: function (v) {',
  25060. ' rtl.raiseE("EPropReadOnly");',
  25061. ' }',
  25062. '}, 123);',
  25063. '$mod.THelper.DoIt.call({',
  25064. ' get: function () {',
  25065. ' return 1;',
  25066. ' },',
  25067. ' set: function (v) {',
  25068. ' rtl.raiseE("EPropReadOnly");',
  25069. ' }',
  25070. '}, 123);',
  25071. '$mod.THelper.Swing(3);',
  25072. '']));
  25073. end;
  25074. procedure TTestModule.TestTypeHelper_SetType;
  25075. begin
  25076. StartProgram(false);
  25077. Add([
  25078. '{$modeswitch typehelpers}',
  25079. 'type',
  25080. ' TEnum = (red,blue);',
  25081. ' TSetOfEnum = set of TEnum;',
  25082. ' THelper = type helper for TSetOfEnum',
  25083. ' procedure DoIt(e: byte = 123);',
  25084. ' constructor Init(e: TEnum);',
  25085. ' constructor InitEmpty;',
  25086. ' end;',
  25087. 'procedure THelper.DoIt(e: byte);',
  25088. 'begin',
  25089. ' Self:=[];',
  25090. ' Self:=[red];',
  25091. ' Include(Self,blue);',
  25092. 'end;',
  25093. 'constructor THelper.Init(e: TEnum);',
  25094. 'begin',
  25095. ' Self:=[];',
  25096. ' Self:=[e];',
  25097. ' Include(Self,blue);',
  25098. 'end;',
  25099. 'constructor THelper.InitEmpty;',
  25100. 'begin',
  25101. 'end;',
  25102. 'var s: TSetOfEnum;',
  25103. 'begin',
  25104. ' s.DoIt;',
  25105. //' [red].DoIt;',
  25106. //' with s do DoIt;',
  25107. //' with [red,blue] do DoIt;',
  25108. ' s:=TSetOfEnum.Init(blue);',
  25109. ' s:=s.Init(blue);',
  25110. '']);
  25111. ConvertProgram;
  25112. CheckSource('TestTypeHelper_SetType',
  25113. LinesToStr([ // statements
  25114. 'this.TEnum = {',
  25115. ' "0": "red",',
  25116. ' red: 0,',
  25117. ' "1": "blue",',
  25118. ' blue: 1',
  25119. '};',
  25120. 'rtl.createHelper(this, "THelper", null, function () {',
  25121. ' this.DoIt = function (e) {',
  25122. ' this.set({});',
  25123. ' this.set(rtl.createSet($mod.TEnum.red));',
  25124. ' this.set(rtl.includeSet(this.get(), $mod.TEnum.blue));',
  25125. ' };',
  25126. ' this.Init = function (e) {',
  25127. ' this.set({});',
  25128. ' this.set(rtl.createSet(e));',
  25129. ' this.set(rtl.includeSet(this.get(), $mod.TEnum.blue));',
  25130. ' return this.get();',
  25131. ' };',
  25132. ' this.InitEmpty = function () {',
  25133. ' return this.get();',
  25134. ' };',
  25135. ' this.$new = function (fn, args) {',
  25136. ' return this[fn].apply({',
  25137. ' p: {},',
  25138. ' get: function () {',
  25139. ' return this.p;',
  25140. ' },',
  25141. ' set: function (v) {',
  25142. ' this.p = v;',
  25143. ' }',
  25144. ' }, args);',
  25145. ' };',
  25146. '});',
  25147. 'this.s = {};',
  25148. '']),
  25149. LinesToStr([ // $mod.$main
  25150. '$mod.THelper.DoIt.call({',
  25151. ' p: $mod,',
  25152. ' get: function () {',
  25153. ' return this.p.s;',
  25154. ' },',
  25155. ' set: function (v) {',
  25156. ' this.p.s = v;',
  25157. ' }',
  25158. '}, 123);',
  25159. '$mod.s = rtl.refSet($mod.THelper.$new("Init", [$mod.TEnum.blue]));',
  25160. '$mod.s = rtl.refSet($mod.THelper.Init.call({',
  25161. ' p: $mod,',
  25162. ' get: function () {',
  25163. ' return this.p.s;',
  25164. ' },',
  25165. ' set: function (v) {',
  25166. ' this.p.s = v;',
  25167. ' }',
  25168. '}, $mod.TEnum.blue));',
  25169. '']));
  25170. end;
  25171. procedure TTestModule.TestTypeHelper_InterfaceType;
  25172. begin
  25173. StartProgram(false);
  25174. Add([
  25175. '{$interfaces com}',
  25176. '{$modeswitch typehelpers}',
  25177. 'type',
  25178. ' IUnknown = interface',
  25179. ' function _AddRef: longint;',
  25180. ' function _Release: longint;',
  25181. ' end;',
  25182. ' TObject = class(IUnknown)',
  25183. ' function _AddRef: longint; virtual; abstract;',
  25184. ' function _Release: longint; virtual; abstract;',
  25185. ' end;',
  25186. ' THelper = type helper for IUnknown',
  25187. ' procedure Fly(e: byte = 123);',
  25188. ' class procedure Run; static;',
  25189. ' end;',
  25190. 'var',
  25191. ' i: IUnknown;',
  25192. ' o: TObject;',
  25193. 'procedure THelper.Fly(e: byte);',
  25194. 'begin',
  25195. ' i:=Self;',
  25196. ' o:=Self as TObject;',
  25197. ' Self:=nil;',
  25198. ' Self:=i;',
  25199. ' Self:=o;',
  25200. ' with Self do begin',
  25201. ' Fly;',
  25202. ' Fly();',
  25203. ' end;',
  25204. 'end;',
  25205. 'class procedure THelper.Run;',
  25206. 'var l: IUnknown;',
  25207. 'begin',
  25208. ' l.Fly;',
  25209. ' l.Fly();',
  25210. 'end;',
  25211. 'begin',
  25212. ' i.Fly;',
  25213. ' i.Fly();',
  25214. ' i.Run;',
  25215. ' i.Run();',
  25216. ' IUnknown.Run;',
  25217. ' IUnknown.Run();',
  25218. '']);
  25219. ConvertProgram;
  25220. CheckSource('TestTypeHelper_InterfaceType',
  25221. LinesToStr([ // statements
  25222. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  25223. 'rtl.createClass(this, "TObject", null, function () {',
  25224. ' this.$init = function () {',
  25225. ' };',
  25226. ' this.$final = function () {',
  25227. ' };',
  25228. ' rtl.addIntf(this, $mod.IUnknown);',
  25229. '});',
  25230. 'rtl.createHelper(this, "THelper", null, function () {',
  25231. ' this.Fly = function (e) {',
  25232. ' var $ir = rtl.createIntfRefs();',
  25233. ' try {',
  25234. ' rtl.setIntfP($mod, "i", this.get());',
  25235. ' $mod.o = rtl.intfAsClass(this.get(), $mod.TObject);',
  25236. ' this.set(null);',
  25237. ' this.set($mod.i);',
  25238. ' this.set($ir.ref(1, rtl.queryIntfT($mod.o, $mod.IUnknown)));',
  25239. ' var $with = this.get();',
  25240. ' $mod.THelper.Fly.call(this, 123);',
  25241. ' $mod.THelper.Fly.call(this, 123);',
  25242. ' } finally {',
  25243. ' $ir.free();',
  25244. ' };',
  25245. ' };',
  25246. ' this.Run = function () {',
  25247. ' var l = null;',
  25248. ' try {',
  25249. ' $mod.THelper.Fly.call({',
  25250. ' get: function () {',
  25251. ' return l;',
  25252. ' },',
  25253. ' set: function (v) {',
  25254. ' l = rtl.setIntfL(l, v);',
  25255. ' }',
  25256. ' }, 123);',
  25257. ' $mod.THelper.Fly.call({',
  25258. ' get: function () {',
  25259. ' return l;',
  25260. ' },',
  25261. ' set: function (v) {',
  25262. ' l = rtl.setIntfL(l, v);',
  25263. ' }',
  25264. ' }, 123);',
  25265. ' } finally {',
  25266. ' rtl._Release(l);',
  25267. ' };',
  25268. ' };',
  25269. '});',
  25270. 'this.i = null;',
  25271. 'this.o = null;',
  25272. '']),
  25273. LinesToStr([ // $mod.$main
  25274. '$mod.THelper.Fly.call({',
  25275. ' p: $mod,',
  25276. ' get: function () {',
  25277. ' return this.p.i;',
  25278. ' },',
  25279. ' set: function (v) {',
  25280. ' rtl.setIntfP(this.p, "i", v);',
  25281. ' }',
  25282. '}, 123);',
  25283. '$mod.THelper.Fly.call({',
  25284. ' p: $mod,',
  25285. ' get: function () {',
  25286. ' return this.p.i;',
  25287. ' },',
  25288. ' set: function (v) {',
  25289. ' rtl.setIntfP(this.p, "i", v);',
  25290. ' }',
  25291. '}, 123);',
  25292. '$mod.THelper.Run();',
  25293. '$mod.THelper.Run();',
  25294. '$mod.THelper.Run();',
  25295. '$mod.THelper.Run();',
  25296. '']));
  25297. end;
  25298. procedure TTestModule.TestTypeHelper_NestedSelf;
  25299. begin
  25300. StartProgram(false);
  25301. Add([
  25302. '{$modeswitch typehelpers}',
  25303. 'type',
  25304. ' THelper = type helper for string',
  25305. ' procedure Run(Value: string);',
  25306. ' end;',
  25307. 'procedure THelper.Run(Value: string);',
  25308. ' function Sub(i: nativeint): boolean;',
  25309. ' begin',
  25310. ' Result:=Self[i+1]=Value[i];',
  25311. ' end;',
  25312. 'begin',
  25313. ' if Self[3]=Value[4] then ;',
  25314. 'end;',
  25315. 'begin',
  25316. '']);
  25317. ConvertProgram;
  25318. CheckSource('TestTypeHelper_NestedSelf',
  25319. LinesToStr([ // statements
  25320. 'rtl.createHelper(this, "THelper", null, function () {',
  25321. ' this.Run = function (Value) {',
  25322. ' var $Self = this;',
  25323. ' function Sub(i) {',
  25324. ' var Result = false;',
  25325. ' Result = $Self.get().charAt((i + 1) - 1) === Value.charAt(i - 1);',
  25326. ' return Result;',
  25327. ' };',
  25328. ' if ($Self.get().charAt(2) === Value.charAt(3)) ;',
  25329. ' };',
  25330. '});',
  25331. '']),
  25332. LinesToStr([ // $mod.$main
  25333. '']));
  25334. end;
  25335. procedure TTestModule.TestProcType;
  25336. begin
  25337. StartProgram(false);
  25338. Add([
  25339. 'type',
  25340. ' TProcInt = procedure(vI: longint = 1);',
  25341. 'procedure DoIt(vJ: longint);',
  25342. 'begin end;',
  25343. 'var',
  25344. ' b: boolean;',
  25345. ' vP, vQ: tprocint;',
  25346. 'begin',
  25347. ' vp:=nil;',
  25348. ' vp:=vp;',
  25349. ' vp:=@doit;',
  25350. ' vp;',
  25351. ' vp();',
  25352. ' vp(2);',
  25353. ' b:=vp=nil;',
  25354. ' b:=nil=vp;',
  25355. ' b:=vp=vq;',
  25356. ' b:=vp=@doit;',
  25357. ' b:=@doit=vp;',
  25358. ' b:=vp<>nil;',
  25359. ' b:=nil<>vp;',
  25360. ' b:=vp<>vq;',
  25361. ' b:=vp<>@doit;',
  25362. ' b:=@doit<>vp;',
  25363. ' b:=Assigned(vp);',
  25364. ' if Assigned(vp) then ;']);
  25365. ConvertProgram;
  25366. CheckSource('TestProcType',
  25367. LinesToStr([ // statements
  25368. 'this.DoIt = function(vJ) {',
  25369. '};',
  25370. 'this.b = false;',
  25371. 'this.vP = null;',
  25372. 'this.vQ = null;'
  25373. ]),
  25374. LinesToStr([ // $mod.$main
  25375. '$mod.vP = null;',
  25376. '$mod.vP = $mod.vP;',
  25377. '$mod.vP = $mod.DoIt;',
  25378. '$mod.vP(1);',
  25379. '$mod.vP(1);',
  25380. '$mod.vP(2);',
  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. 'if ($mod.vP != null) ;',
  25393. '']));
  25394. end;
  25395. procedure TTestModule.TestProcType_Arg;
  25396. begin
  25397. StartProgram(false);
  25398. Add([
  25399. 'type',
  25400. ' TProcInt = procedure(vI: longint = 1);',
  25401. 'procedure DoIt(vJ: longint); begin end;',
  25402. 'procedure DoSome(vP, vQ: TProcInt);',
  25403. 'var',
  25404. ' b: boolean;',
  25405. 'begin',
  25406. ' vp:=nil;',
  25407. ' vp:=vp;',
  25408. ' vp:=@doit;',
  25409. ' vp;',
  25410. ' vp();',
  25411. ' vp(2);',
  25412. ' b:=vp=nil;',
  25413. ' b:=nil=vp;',
  25414. ' b:=vp=vq;',
  25415. ' b:=vp=@doit;',
  25416. ' b:=@doit=vp;',
  25417. ' b:=vp<>nil;',
  25418. ' b:=nil<>vp;',
  25419. ' b:=vp<>vq;',
  25420. ' b:=vp<>@doit;',
  25421. ' b:=@doit<>vp;',
  25422. ' b:=Assigned(vp);',
  25423. ' if Assigned(vp) then ;',
  25424. 'end;',
  25425. 'begin',
  25426. ' DoSome(@DoIt,nil);']);
  25427. ConvertProgram;
  25428. CheckSource('TestProcType_Arg',
  25429. LinesToStr([ // statements
  25430. 'this.DoIt = function(vJ) {',
  25431. '};',
  25432. 'this.DoSome = function(vP, vQ) {',
  25433. ' var b = false;',
  25434. ' vP = null;',
  25435. ' vP = vP;',
  25436. ' vP = $mod.DoIt;',
  25437. ' vP(1);',
  25438. ' vP(1);',
  25439. ' vP(2);',
  25440. ' b = vP === null;',
  25441. ' b = null === vP;',
  25442. ' b = rtl.eqCallback(vP,vQ);',
  25443. ' b = rtl.eqCallback(vP, $mod.DoIt);',
  25444. ' b = rtl.eqCallback($mod.DoIt, vP);',
  25445. ' b = vP !== null;',
  25446. ' b = null !== vP;',
  25447. ' b = !rtl.eqCallback(vP, vQ);',
  25448. ' b = !rtl.eqCallback(vP, $mod.DoIt);',
  25449. ' b = !rtl.eqCallback($mod.DoIt, vP);',
  25450. ' b = vP != null;',
  25451. ' if (vP != null) ;',
  25452. '};',
  25453. '']),
  25454. LinesToStr([ // $mod.$main
  25455. '$mod.DoSome($mod.DoIt,null);',
  25456. '']));
  25457. end;
  25458. procedure TTestModule.TestProcType_FunctionFPC;
  25459. begin
  25460. StartProgram(false);
  25461. Add('type');
  25462. Add(' TFuncInt = function(vA: longint = 1): longint;');
  25463. Add('function DoIt(vI: longint): longint;');
  25464. Add('begin end;');
  25465. Add('var');
  25466. Add(' b: boolean;');
  25467. Add(' vP, vQ: tfuncint;');
  25468. Add('begin');
  25469. Add(' vp:=nil;');
  25470. Add(' vp:=vp;');
  25471. Add(' vp:=@doit;'); // ok in fpc and delphi
  25472. //Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  25473. Add(' vp;'); // ok in fpc and delphi
  25474. Add(' vp();');
  25475. Add(' vp(2);');
  25476. Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  25477. Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  25478. Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  25479. Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  25480. Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  25481. //Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
  25482. Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
  25483. Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  25484. Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  25485. Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  25486. Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  25487. Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  25488. //Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
  25489. Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
  25490. Add(' b:=Assigned(vp);');
  25491. //Add(' doit(vp);'); // illegal in fpc, ok in delphi
  25492. Add(' doit(vp());'); // ok in fpc and delphi
  25493. Add(' doit(vp(2));'); // ok in fpc and delphi
  25494. ConvertProgram;
  25495. CheckSource('TestProcType_FunctionFPC',
  25496. LinesToStr([ // statements
  25497. 'this.DoIt = function(vI) {',
  25498. ' var Result = 0;',
  25499. ' return Result;',
  25500. '};',
  25501. 'this.b = false;',
  25502. 'this.vP = null;',
  25503. 'this.vQ = null;'
  25504. ]),
  25505. LinesToStr([ // $mod.$main
  25506. '$mod.vP = null;',
  25507. '$mod.vP = $mod.vP;',
  25508. '$mod.vP = $mod.DoIt;',
  25509. '$mod.vP(1);',
  25510. '$mod.vP(1);',
  25511. '$mod.vP(2);',
  25512. '$mod.b = $mod.vP === null;',
  25513. '$mod.b = null === $mod.vP;',
  25514. '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
  25515. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  25516. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  25517. '$mod.b = 4 === $mod.vP(1);',
  25518. '$mod.b = $mod.vP !== null;',
  25519. '$mod.b = null !== $mod.vP;',
  25520. '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
  25521. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  25522. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  25523. '$mod.b = 6 !== $mod.vP(1);',
  25524. '$mod.b = $mod.vP != null;',
  25525. '$mod.DoIt($mod.vP(1));',
  25526. '$mod.DoIt($mod.vP(2));',
  25527. '']));
  25528. end;
  25529. procedure TTestModule.TestProcType_FunctionDelphi;
  25530. begin
  25531. StartProgram(false);
  25532. Add('{$mode Delphi}');
  25533. Add('type');
  25534. Add(' TFuncInt = function(vA: longint = 1): longint;');
  25535. Add('function DoIt(vI: longint): longint;');
  25536. Add('begin end;');
  25537. Add('var');
  25538. Add(' b: boolean;');
  25539. Add(' vP, vQ: tfuncint;');
  25540. Add('begin');
  25541. Add(' vp:=nil;');
  25542. Add(' vp:=vp;');
  25543. Add(' vp:=@doit;'); // ok in fpc and delphi
  25544. Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  25545. Add(' vp;'); // ok in fpc and delphi
  25546. Add(' vp();');
  25547. Add(' vp(2);');
  25548. //Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  25549. //Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  25550. Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  25551. //Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  25552. //Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  25553. Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
  25554. Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
  25555. //Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  25556. //Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  25557. Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  25558. //Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  25559. //Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  25560. Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
  25561. Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
  25562. Add(' b:=Assigned(vp);');
  25563. Add(' doit(vp);'); // illegal in fpc, ok in delphi
  25564. Add(' doit(vp());'); // ok in fpc and delphi
  25565. Add(' doit(vp(2));'); // ok in fpc and delphi *)
  25566. ConvertProgram;
  25567. CheckSource('TestProcType_FunctionDelphi',
  25568. LinesToStr([ // statements
  25569. 'this.DoIt = function(vI) {',
  25570. ' var Result = 0;',
  25571. ' return Result;',
  25572. '};',
  25573. 'this.b = false;',
  25574. 'this.vP = null;',
  25575. 'this.vQ = null;'
  25576. ]),
  25577. LinesToStr([ // $mod.$main
  25578. '$mod.vP = null;',
  25579. '$mod.vP = $mod.vP;',
  25580. '$mod.vP = $mod.DoIt;',
  25581. '$mod.vP = $mod.DoIt;',
  25582. '$mod.vP(1);',
  25583. '$mod.vP(1);',
  25584. '$mod.vP(2);',
  25585. '$mod.b = $mod.vP(1) === $mod.vQ(1);',
  25586. '$mod.b = $mod.vP(1) === 3;',
  25587. '$mod.b = 4 === $mod.vP(1);',
  25588. '$mod.b = $mod.vP(1) !== $mod.vQ(1);',
  25589. '$mod.b = $mod.vP(1) !== 5;',
  25590. '$mod.b = 6 !== $mod.vP(1);',
  25591. '$mod.b = $mod.vP != null;',
  25592. '$mod.DoIt($mod.vP(1));',
  25593. '$mod.DoIt($mod.vP(1));',
  25594. '$mod.DoIt($mod.vP(2));',
  25595. '']));
  25596. end;
  25597. procedure TTestModule.TestProcType_ProcedureDelphi;
  25598. begin
  25599. StartProgram(false);
  25600. Add('{$mode Delphi}');
  25601. Add('type');
  25602. Add(' TProc = procedure;');
  25603. Add('procedure DoIt;');
  25604. Add('begin end;');
  25605. Add('var');
  25606. Add(' b: boolean;');
  25607. Add(' vP, vQ: tproc;');
  25608. Add('begin');
  25609. Add(' vp:=nil;');
  25610. Add(' vp:=vp;');
  25611. Add(' vp:=vq;');
  25612. 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
  25613. Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  25614. //Add(' vp:=@doit;'); // illegal in fpc, ok in delphi (because Delphi treats @F as Pointer), not supported by resolver
  25615. Add(' vp;'); // ok in fpc and delphi
  25616. Add(' vp();');
  25617. // equal
  25618. //Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  25619. Add(' b:=@@vp=nil;'); // ok in fpc delphi mode, ok in delphi
  25620. //Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  25621. Add(' b:=nil=@@vp;'); // ok in fpc delphi mode, ok in delphi
  25622. Add(' b:=@@vp=@@vq;'); // ok in fpc delphi mode, ok in Delphi
  25623. //Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  25624. //Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  25625. Add(' b:=@@vp=@doit;'); // ok in fpc delphi mode, ok in delphi
  25626. //Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  25627. Add(' b:=@doit=@@vp;'); // ok in fpc delphi mode, ok in delphi
  25628. // unequal
  25629. //Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  25630. Add(' b:=@@vp<>nil;'); // ok in fpc mode delphi, ok in delphi
  25631. //Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  25632. Add(' b:=nil<>@@vp;'); // ok in fpc mode delphi, ok in delphi
  25633. //Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  25634. Add(' b:=@@vp<>@@vq;'); // ok in fpc mode delphi, ok in delphi
  25635. //Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  25636. Add(' b:=@@vp<>@doit;'); // ok in fpc mode delphi, illegal in delphi
  25637. //Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  25638. Add(' b:=@doit<>@@vp;'); // ok in fpc mode delphi, illegal in delphi
  25639. Add(' b:=Assigned(vp);');
  25640. ConvertProgram;
  25641. CheckSource('TestProcType_ProcedureDelphi',
  25642. LinesToStr([ // statements
  25643. 'this.DoIt = function() {',
  25644. '};',
  25645. 'this.b = false;',
  25646. 'this.vP = null;',
  25647. 'this.vQ = null;'
  25648. ]),
  25649. LinesToStr([ // $mod.$main
  25650. '$mod.vP = null;',
  25651. '$mod.vP = $mod.vP;',
  25652. '$mod.vP = $mod.vQ;',
  25653. '$mod.vP = $mod.DoIt;',
  25654. '$mod.vP = $mod.DoIt;',
  25655. '$mod.vP();',
  25656. '$mod.vP();',
  25657. '$mod.b = $mod.vP === null;',
  25658. '$mod.b = null === $mod.vP;',
  25659. '$mod.b = rtl.eqCallback($mod.vP, $mod.vQ);',
  25660. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  25661. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  25662. '$mod.b = $mod.vP !== null;',
  25663. '$mod.b = null !== $mod.vP;',
  25664. '$mod.b = !rtl.eqCallback($mod.vP, $mod.vQ);',
  25665. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  25666. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  25667. '$mod.b = $mod.vP != null;',
  25668. '']));
  25669. end;
  25670. procedure TTestModule.TestProcType_AsParam;
  25671. begin
  25672. StartProgram(false);
  25673. Add('type');
  25674. Add(' TFuncInt = function(vA: longint = 1): longint;');
  25675. Add('procedure DoIt(vG: tfuncint; const vH: tfuncint; var vI: tfuncint);');
  25676. Add('var vJ: tfuncint;');
  25677. Add('begin');
  25678. Add(' vg:=vg;');
  25679. Add(' vj:=vh;');
  25680. Add(' vi:=vi;');
  25681. Add(' doit(vg,vg,vg);');
  25682. Add(' doit(vh,vh,vj);');
  25683. Add(' doit(vi,vi,vi);');
  25684. Add(' doit(vj,vj,vj);');
  25685. Add('end;');
  25686. Add('var i: tfuncint;');
  25687. Add('begin');
  25688. Add(' doit(i,i,i);');
  25689. ConvertProgram;
  25690. CheckSource('TestProcType_AsParam',
  25691. LinesToStr([ // statements
  25692. 'this.DoIt = function (vG,vH,vI) {',
  25693. ' var vJ = null;',
  25694. ' vG = vG;',
  25695. ' vJ = vH;',
  25696. ' vI.set(vI.get());',
  25697. ' $mod.DoIt(vG, vG, {',
  25698. ' get: function () {',
  25699. ' return vG;',
  25700. ' },',
  25701. ' set: function (v) {',
  25702. ' vG = v;',
  25703. ' }',
  25704. ' });',
  25705. ' $mod.DoIt(vH, vH, {',
  25706. ' get: function () {',
  25707. ' return vJ;',
  25708. ' },',
  25709. ' set: function (v) {',
  25710. ' vJ = v;',
  25711. ' }',
  25712. ' });',
  25713. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  25714. ' $mod.DoIt(vJ, vJ, {',
  25715. ' get: function () {',
  25716. ' return vJ;',
  25717. ' },',
  25718. ' set: function (v) {',
  25719. ' vJ = v;',
  25720. ' }',
  25721. ' });',
  25722. '};',
  25723. 'this.i = null;'
  25724. ]),
  25725. LinesToStr([
  25726. '$mod.DoIt($mod.i,$mod.i,{',
  25727. ' p: $mod,',
  25728. ' get: function () {',
  25729. ' return this.p.i;',
  25730. ' },',
  25731. ' set: function (v) {',
  25732. ' this.p.i = v;',
  25733. ' }',
  25734. '});'
  25735. ]));
  25736. end;
  25737. procedure TTestModule.TestProcType_MethodFPC;
  25738. begin
  25739. StartProgram(false);
  25740. Add('type');
  25741. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  25742. Add(' TObject = class');
  25743. Add(' function DoIt(vA: longint = 1): longint;');
  25744. Add(' end;');
  25745. Add('function TObject.DoIt(vA: longint = 1): longint;');
  25746. Add('begin');
  25747. Add('end;');
  25748. Add('var');
  25749. Add(' Obj: TObject;');
  25750. Add(' vP: tfuncint;');
  25751. Add(' b: boolean;');
  25752. Add('begin');
  25753. Add(' vp:[email protected];'); // ok in fpc and delphi
  25754. //Add(' vp:=obj.doit;'); // illegal in fpc, ok in delphi
  25755. Add(' vp;'); // ok in fpc and delphi
  25756. Add(' vp();');
  25757. Add(' vp(2);');
  25758. Add(' b:[email protected];'); // ok in fpc, illegal in delphi
  25759. Add(' b:[email protected]=vp;'); // ok in fpc, illegal in delphi
  25760. Add(' b:=vp<>@obj.doit;'); // ok in fpc, illegal in delphi
  25761. Add(' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
  25762. ConvertProgram;
  25763. CheckSource('TestProcType_MethodFPC',
  25764. LinesToStr([ // statements
  25765. 'rtl.createClass(this, "TObject", null, function () {',
  25766. ' this.$init = function () {',
  25767. ' };',
  25768. ' this.$final = function () {',
  25769. ' };',
  25770. ' this.DoIt = function (vA) {',
  25771. ' var Result = 0;',
  25772. ' return Result;',
  25773. ' };',
  25774. '});',
  25775. 'this.Obj = null;',
  25776. 'this.vP = null;',
  25777. 'this.b = false;'
  25778. ]),
  25779. LinesToStr([
  25780. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  25781. '$mod.vP(1);',
  25782. '$mod.vP(1);',
  25783. '$mod.vP(2);',
  25784. '$mod.b = rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
  25785. '$mod.b = rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
  25786. '$mod.b = !rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
  25787. '$mod.b = !rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
  25788. '']));
  25789. end;
  25790. procedure TTestModule.TestProcType_MethodDelphi;
  25791. begin
  25792. StartProgram(false);
  25793. Add([
  25794. '{$mode delphi}',
  25795. 'type',
  25796. ' TFuncInt = function(vA: longint = 1): longint of object;',
  25797. ' TObject = class',
  25798. ' function DoIt(vA: longint = 1): longint;',
  25799. ' end;',
  25800. 'function TObject.DoIt(vA: longint = 1): longint;',
  25801. 'begin',
  25802. 'end;',
  25803. 'var',
  25804. ' Obj: TObject;',
  25805. ' vP: tfuncint;',
  25806. ' b: boolean;',
  25807. 'begin',
  25808. ' vp:[email protected];', // ok in fpc and delphi
  25809. ' vp:=obj.doit;', // illegal in fpc, ok in delphi
  25810. ' vp;', // ok in fpc and delphi
  25811. ' vp();',
  25812. ' vp(2);',
  25813. //' b:[email protected];', // ok in fpc, illegal in delphi
  25814. //' b:[email protected]=vp;', // ok in fpc, illegal in delphi
  25815. //' b:=vp<>@obj.doit;', // ok in fpc, illegal in delphi
  25816. //' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
  25817. '']);
  25818. ConvertProgram;
  25819. CheckSource('TestProcType_MethodDelphi',
  25820. LinesToStr([ // statements
  25821. 'rtl.createClass(this, "TObject", null, function () {',
  25822. ' this.$init = function () {',
  25823. ' };',
  25824. ' this.$final = function () {',
  25825. ' };',
  25826. ' this.DoIt = function (vA) {',
  25827. ' var Result = 0;',
  25828. ' return Result;',
  25829. ' };',
  25830. '});',
  25831. 'this.Obj = null;',
  25832. 'this.vP = null;',
  25833. 'this.b = false;'
  25834. ]),
  25835. LinesToStr([
  25836. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  25837. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  25838. '$mod.vP(1);',
  25839. '$mod.vP(1);',
  25840. '$mod.vP(2);',
  25841. '']));
  25842. end;
  25843. procedure TTestModule.TestProcType_PropertyFPC;
  25844. begin
  25845. StartProgram(false);
  25846. Add('type');
  25847. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  25848. Add(' TObject = class');
  25849. Add(' FOnFoo: TFuncInt;');
  25850. Add(' function DoIt(vA: longint = 1): longint;');
  25851. Add(' function GetFoo: TFuncInt;');
  25852. Add(' procedure SetFoo(const Value: TFuncInt);');
  25853. Add(' function GetEvents(Index: longint): TFuncInt;');
  25854. Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
  25855. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  25856. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  25857. Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
  25858. Add(' end;');
  25859. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  25860. Add('function tobject.getfoo: tfuncint; begin end;');
  25861. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  25862. Add('function tobject.getevents(index: longint): tfuncint; begin end;');
  25863. Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
  25864. Add('var');
  25865. Add(' Obj: TObject;');
  25866. Add(' vP: tfuncint;');
  25867. Add(' b: boolean;');
  25868. Add('begin');
  25869. Add(' obj.onfoo:=nil;');
  25870. Add(' obj.onbar:=nil;');
  25871. Add(' obj.events[1]:=nil;');
  25872. Add(' obj.onfoo:=obj.onfoo;');
  25873. Add(' obj.onbar:=obj.onbar;');
  25874. Add(' obj.events[2]:=obj.events[3];');
  25875. Add(' obj.onfoo:[email protected];');
  25876. Add(' obj.onbar:[email protected];');
  25877. Add(' obj.events[4]:[email protected];');
  25878. //Add(' obj.onfoo:=obj.doit;'); // delphi
  25879. //Add(' obj.onbar:=obj.doit;'); // delphi
  25880. //Add(' obj.events[4]:=obj.doit;'); // delphi
  25881. Add(' obj.onfoo;');
  25882. Add(' obj.onbar;');
  25883. //Add(' obj.events[5];'); ToDo in pasresolver
  25884. Add(' obj.onfoo();');
  25885. Add(' obj.onbar();');
  25886. Add(' obj.events[6]();');
  25887. Add(' b:=obj.onfoo=nil;');
  25888. Add(' b:=obj.onbar=nil;');
  25889. Add(' b:=obj.events[7]=nil;');
  25890. Add(' b:=obj.onfoo<>nil;');
  25891. Add(' b:=obj.onbar<>nil;');
  25892. Add(' b:=obj.events[8]<>nil;');
  25893. Add(' b:=obj.onfoo=vp;');
  25894. Add(' b:=obj.onbar=vp;');
  25895. Add(' b:=obj.events[9]=vp;');
  25896. Add(' b:=obj.onfoo=obj.onfoo;');
  25897. Add(' b:=obj.onbar=obj.onfoo;');
  25898. Add(' b:=obj.events[10]=obj.onfoo;');
  25899. Add(' b:=obj.onfoo<>obj.onfoo;');
  25900. Add(' b:=obj.onbar<>obj.onfoo;');
  25901. Add(' b:=obj.events[11]<>obj.onfoo;');
  25902. Add(' b:[email protected];');
  25903. Add(' b:[email protected];');
  25904. Add(' b:=obj.events[12][email protected];');
  25905. Add(' b:=obj.onfoo<>@obj.doit;');
  25906. Add(' b:=obj.onbar<>@obj.doit;');
  25907. Add(' b:=obj.events[12]<>@obj.doit;');
  25908. Add(' b:=Assigned(obj.onfoo);');
  25909. Add(' b:=Assigned(obj.onbar);');
  25910. Add(' b:=Assigned(obj.events[13]);');
  25911. ConvertProgram;
  25912. CheckSource('TestProcType_PropertyFPC',
  25913. LinesToStr([ // statements
  25914. 'rtl.createClass(this, "TObject", null, function () {',
  25915. ' this.$init = function () {',
  25916. ' this.FOnFoo = null;',
  25917. ' };',
  25918. ' this.$final = function () {',
  25919. ' this.FOnFoo = undefined;',
  25920. ' };',
  25921. ' this.DoIt = function (vA) {',
  25922. ' var Result = 0;',
  25923. ' return Result;',
  25924. ' };',
  25925. 'this.GetFoo = function () {',
  25926. ' var Result = null;',
  25927. ' return Result;',
  25928. '};',
  25929. 'this.SetFoo = function (Value) {',
  25930. '};',
  25931. 'this.GetEvents = function (Index) {',
  25932. ' var Result = null;',
  25933. ' return Result;',
  25934. '};',
  25935. 'this.SetEvents = function (Index, Value) {',
  25936. '};',
  25937. '});',
  25938. 'this.Obj = null;',
  25939. 'this.vP = null;',
  25940. 'this.b = false;'
  25941. ]),
  25942. LinesToStr([
  25943. '$mod.Obj.FOnFoo = null;',
  25944. '$mod.Obj.SetFoo(null);',
  25945. '$mod.Obj.SetEvents(1, null);',
  25946. '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
  25947. '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
  25948. '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
  25949. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  25950. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  25951. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  25952. '$mod.Obj.FOnFoo(1);',
  25953. '$mod.Obj.GetFoo();',
  25954. '$mod.Obj.FOnFoo(1);',
  25955. '$mod.Obj.GetFoo()(1);',
  25956. '$mod.Obj.GetEvents(6)(1);',
  25957. '$mod.b = $mod.Obj.FOnFoo === null;',
  25958. '$mod.b = $mod.Obj.GetFoo() === null;',
  25959. '$mod.b = $mod.Obj.GetEvents(7) === null;',
  25960. '$mod.b = $mod.Obj.FOnFoo !== null;',
  25961. '$mod.b = $mod.Obj.GetFoo() !== null;',
  25962. '$mod.b = $mod.Obj.GetEvents(8) !== null;',
  25963. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.vP);',
  25964. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.vP);',
  25965. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(9), $mod.vP);',
  25966. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
  25967. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
  25968. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(10), $mod.Obj.FOnFoo);',
  25969. '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
  25970. '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
  25971. '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(11), $mod.Obj.FOnFoo);',
  25972. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
  25973. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
  25974. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
  25975. '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
  25976. '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
  25977. '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
  25978. '$mod.b = $mod.Obj.FOnFoo != null;',
  25979. '$mod.b = $mod.Obj.GetFoo() != null;',
  25980. '$mod.b = $mod.Obj.GetEvents(13) != null;',
  25981. '']));
  25982. end;
  25983. procedure TTestModule.TestProcType_PropertyDelphi;
  25984. begin
  25985. StartProgram(false);
  25986. Add('{$mode delphi}');
  25987. Add('type');
  25988. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  25989. Add(' TObject = class');
  25990. Add(' FOnFoo: TFuncInt;');
  25991. Add(' function DoIt(vA: longint = 1): longint;');
  25992. Add(' function GetFoo: TFuncInt;');
  25993. Add(' procedure SetFoo(const Value: TFuncInt);');
  25994. Add(' function GetEvents(Index: longint): TFuncInt;');
  25995. Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
  25996. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  25997. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  25998. Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
  25999. Add(' end;');
  26000. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  26001. Add('function tobject.getfoo: tfuncint; begin end;');
  26002. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  26003. Add('function tobject.getevents(index: longint): tfuncint; begin end;');
  26004. Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
  26005. Add('var');
  26006. Add(' Obj: TObject;');
  26007. Add(' vP: tfuncint;');
  26008. Add(' b: boolean;');
  26009. Add('begin');
  26010. Add(' obj.onfoo:=nil;');
  26011. Add(' obj.onbar:=nil;');
  26012. Add(' obj.events[1]:=nil;');
  26013. Add(' obj.onfoo:=obj.onfoo;');
  26014. Add(' obj.onbar:=obj.onbar;');
  26015. Add(' obj.events[2]:=obj.events[3];');
  26016. Add(' obj.onfoo:[email protected];');
  26017. Add(' obj.onbar:[email protected];');
  26018. Add(' obj.events[4]:[email protected];');
  26019. Add(' obj.onfoo:=obj.doit;'); // delphi
  26020. Add(' obj.onbar:=obj.doit;'); // delphi
  26021. Add(' obj.events[4]:=obj.doit;'); // delphi
  26022. Add(' obj.onfoo;');
  26023. Add(' obj.onbar;');
  26024. //Add(' obj.events[5];'); ToDo in pasresolver
  26025. Add(' obj.onfoo();');
  26026. Add(' obj.onbar();');
  26027. Add(' obj.events[6]();');
  26028. //Add(' b:=obj.onfoo=nil;'); // fpc
  26029. //Add(' b:=obj.onbar=nil;'); // fpc
  26030. //Add(' b:=obj.events[7]=nil;'); // fpc
  26031. //Add(' b:=obj.onfoo<>nil;'); // fpc
  26032. //Add(' b:=obj.onbar<>nil;'); // fpc
  26033. //Add(' b:=obj.events[8]<>nil;'); // fpc
  26034. Add(' b:=obj.onfoo=vp;');
  26035. Add(' b:=obj.onbar=vp;');
  26036. //Add(' b:=obj.events[9]=vp;'); ToDo in pasresolver
  26037. Add(' b:=obj.onfoo=obj.onfoo;');
  26038. Add(' b:=obj.onbar=obj.onfoo;');
  26039. //Add(' b:=obj.events[10]=obj.onfoo;'); // ToDo in pasresolver
  26040. Add(' b:=obj.onfoo<>obj.onfoo;');
  26041. Add(' b:=obj.onbar<>obj.onfoo;');
  26042. //Add(' b:=obj.events[11]<>obj.onfoo;'); // ToDo in pasresolver
  26043. //Add(' b:[email protected];'); // fpc
  26044. //Add(' b:[email protected];'); // fpc
  26045. //Add(' b:=obj.events[12][email protected];'); // fpc
  26046. //Add(' b:=obj.onfoo<>@obj.doit;'); // fpc
  26047. //Add(' b:=obj.onbar<>@obj.doit;'); // fpc
  26048. //Add(' b:=obj.events[12]<>@obj.doit;'); // fpc
  26049. Add(' b:=Assigned(obj.onfoo);');
  26050. Add(' b:=Assigned(obj.onbar);');
  26051. Add(' b:=Assigned(obj.events[13]);');
  26052. ConvertProgram;
  26053. CheckSource('TestProcType_PropertyDelphi',
  26054. LinesToStr([ // statements
  26055. 'rtl.createClass(this, "TObject", null, function () {',
  26056. ' this.$init = function () {',
  26057. ' this.FOnFoo = null;',
  26058. ' };',
  26059. ' this.$final = function () {',
  26060. ' this.FOnFoo = undefined;',
  26061. ' };',
  26062. ' this.DoIt = function (vA) {',
  26063. ' var Result = 0;',
  26064. ' return Result;',
  26065. ' };',
  26066. 'this.GetFoo = function () {',
  26067. ' var Result = null;',
  26068. ' return Result;',
  26069. '};',
  26070. 'this.SetFoo = function (Value) {',
  26071. '};',
  26072. 'this.GetEvents = function (Index) {',
  26073. ' var Result = null;',
  26074. ' return Result;',
  26075. '};',
  26076. 'this.SetEvents = function (Index, Value) {',
  26077. '};',
  26078. '});',
  26079. 'this.Obj = null;',
  26080. 'this.vP = null;',
  26081. 'this.b = false;'
  26082. ]),
  26083. LinesToStr([
  26084. '$mod.Obj.FOnFoo = null;',
  26085. '$mod.Obj.SetFoo(null);',
  26086. '$mod.Obj.SetEvents(1, null);',
  26087. '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
  26088. '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
  26089. '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
  26090. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  26091. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  26092. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  26093. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  26094. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  26095. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  26096. '$mod.Obj.FOnFoo(1);',
  26097. '$mod.Obj.GetFoo();',
  26098. '$mod.Obj.FOnFoo(1);',
  26099. '$mod.Obj.GetFoo()(1);',
  26100. '$mod.Obj.GetEvents(6)(1);',
  26101. '$mod.b = $mod.Obj.FOnFoo(1) === $mod.vP(1);',
  26102. '$mod.b = $mod.Obj.GetFoo() === $mod.vP(1);',
  26103. '$mod.b = $mod.Obj.FOnFoo(1) === $mod.Obj.FOnFoo(1);',
  26104. '$mod.b = $mod.Obj.GetFoo() === $mod.Obj.FOnFoo(1);',
  26105. '$mod.b = $mod.Obj.FOnFoo(1) !== $mod.Obj.FOnFoo(1);',
  26106. '$mod.b = $mod.Obj.GetFoo() !== $mod.Obj.FOnFoo(1);',
  26107. '$mod.b = $mod.Obj.FOnFoo != null;',
  26108. '$mod.b = $mod.Obj.GetFoo() != null;',
  26109. '$mod.b = $mod.Obj.GetEvents(13) != null;',
  26110. '']));
  26111. end;
  26112. procedure TTestModule.TestProcType_WithClassInstDoPropertyFPC;
  26113. begin
  26114. StartProgram(false);
  26115. Add('type');
  26116. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  26117. Add(' TObject = class');
  26118. Add(' FOnFoo: TFuncInt;');
  26119. Add(' function DoIt(vA: longint = 1): longint;');
  26120. Add(' function GetFoo: TFuncInt;');
  26121. Add(' procedure SetFoo(const Value: TFuncInt);');
  26122. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  26123. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  26124. Add(' end;');
  26125. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  26126. Add('function tobject.getfoo: tfuncint; begin end;');
  26127. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  26128. Add('var');
  26129. Add(' Obj: TObject;');
  26130. Add(' vP: tfuncint;');
  26131. Add(' b: boolean;');
  26132. Add('begin');
  26133. Add('with obj do begin');
  26134. Add(' fonfoo:=nil;');
  26135. Add(' onfoo:=nil;');
  26136. Add(' onbar:=nil;');
  26137. Add(' fonfoo:=fonfoo;');
  26138. Add(' onfoo:=onfoo;');
  26139. Add(' onbar:=onbar;');
  26140. Add(' fonfoo:=@doit;');
  26141. Add(' onfoo:=@doit;');
  26142. Add(' onbar:=@doit;');
  26143. //Add(' fonfoo:=doit;'); // delphi
  26144. //Add(' onfoo:=doit;'); // delphi
  26145. //Add(' onbar:=doit;'); // delphi
  26146. Add(' fonfoo;');
  26147. Add(' onfoo;');
  26148. Add(' onbar;');
  26149. Add(' fonfoo();');
  26150. Add(' onfoo();');
  26151. Add(' onbar();');
  26152. Add(' b:=fonfoo=nil;');
  26153. Add(' b:=onfoo=nil;');
  26154. Add(' b:=onbar=nil;');
  26155. Add(' b:=fonfoo<>nil;');
  26156. Add(' b:=onfoo<>nil;');
  26157. Add(' b:=onbar<>nil;');
  26158. Add(' b:=fonfoo=vp;');
  26159. Add(' b:=onfoo=vp;');
  26160. Add(' b:=onbar=vp;');
  26161. Add(' b:=fonfoo=fonfoo;');
  26162. Add(' b:=onfoo=onfoo;');
  26163. Add(' b:=onbar=onfoo;');
  26164. Add(' b:=fonfoo<>fonfoo;');
  26165. Add(' b:=onfoo<>onfoo;');
  26166. Add(' b:=onbar<>onfoo;');
  26167. Add(' b:=fonfoo=@doit;');
  26168. Add(' b:=onfoo=@doit;');
  26169. Add(' b:=onbar=@doit;');
  26170. Add(' b:=fonfoo<>@doit;');
  26171. Add(' b:=onfoo<>@doit;');
  26172. Add(' b:=onbar<>@doit;');
  26173. Add(' b:=Assigned(fonfoo);');
  26174. Add(' b:=Assigned(onfoo);');
  26175. Add(' b:=Assigned(onbar);');
  26176. Add('end;');
  26177. ConvertProgram;
  26178. CheckSource('TestProcType_WithClassInstDoPropertyFPC',
  26179. LinesToStr([ // statements
  26180. 'rtl.createClass(this, "TObject", null, function () {',
  26181. ' this.$init = function () {',
  26182. ' this.FOnFoo = null;',
  26183. ' };',
  26184. ' this.$final = function () {',
  26185. ' this.FOnFoo = undefined;',
  26186. ' };',
  26187. ' this.DoIt = function (vA) {',
  26188. ' var Result = 0;',
  26189. ' return Result;',
  26190. ' };',
  26191. ' this.GetFoo = function () {',
  26192. ' var Result = null;',
  26193. ' return Result;',
  26194. ' };',
  26195. ' this.SetFoo = function (Value) {',
  26196. ' };',
  26197. '});',
  26198. 'this.Obj = null;',
  26199. 'this.vP = null;',
  26200. 'this.b = false;'
  26201. ]),
  26202. LinesToStr([
  26203. 'var $with = $mod.Obj;',
  26204. '$with.FOnFoo = null;',
  26205. '$with.FOnFoo = null;',
  26206. '$with.SetFoo(null);',
  26207. '$with.FOnFoo = $with.FOnFoo;',
  26208. '$with.FOnFoo = $with.FOnFoo;',
  26209. '$with.SetFoo($with.GetFoo());',
  26210. '$with.FOnFoo = rtl.createCallback($with, "DoIt");',
  26211. '$with.FOnFoo = rtl.createCallback($with, "DoIt");',
  26212. '$with.SetFoo(rtl.createCallback($with, "DoIt"));',
  26213. '$with.FOnFoo(1);',
  26214. '$with.FOnFoo(1);',
  26215. '$with.GetFoo();',
  26216. '$with.FOnFoo(1);',
  26217. '$with.FOnFoo(1);',
  26218. '$with.GetFoo()(1);',
  26219. '$mod.b = $with.FOnFoo === null;',
  26220. '$mod.b = $with.FOnFoo === null;',
  26221. '$mod.b = $with.GetFoo() === null;',
  26222. '$mod.b = $with.FOnFoo !== null;',
  26223. '$mod.b = $with.FOnFoo !== null;',
  26224. '$mod.b = $with.GetFoo() !== null;',
  26225. '$mod.b = rtl.eqCallback($with.FOnFoo, $mod.vP);',
  26226. '$mod.b = rtl.eqCallback($with.FOnFoo, $mod.vP);',
  26227. '$mod.b = rtl.eqCallback($with.GetFoo(), $mod.vP);',
  26228. '$mod.b = rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  26229. '$mod.b = rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  26230. '$mod.b = rtl.eqCallback($with.GetFoo(), $with.FOnFoo);',
  26231. '$mod.b = !rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  26232. '$mod.b = !rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  26233. '$mod.b = !rtl.eqCallback($with.GetFoo(), $with.FOnFoo);',
  26234. '$mod.b = rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  26235. '$mod.b = rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  26236. '$mod.b = rtl.eqCallback($with.GetFoo(), rtl.createCallback($with, "DoIt"));',
  26237. '$mod.b = !rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  26238. '$mod.b = !rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  26239. '$mod.b = !rtl.eqCallback($with.GetFoo(), rtl.createCallback($with, "DoIt"));',
  26240. '$mod.b = $with.FOnFoo != null;',
  26241. '$mod.b = $with.FOnFoo != null;',
  26242. '$mod.b = $with.GetFoo() != null;',
  26243. '']));
  26244. end;
  26245. procedure TTestModule.TestProcType_Nested;
  26246. begin
  26247. StartProgram(false);
  26248. Add([
  26249. 'type',
  26250. ' TProcInt = procedure(vI: longint = 1);',
  26251. 'procedure DoIt(vJ: longint);',
  26252. 'var aProc: TProcInt;',
  26253. ' b: boolean;',
  26254. ' procedure Sub(vK: longint);',
  26255. ' var aSub: TProcInt;',
  26256. ' procedure SubSub(vK: longint);',
  26257. ' var aSubSub: TProcInt;',
  26258. ' begin;',
  26259. ' aProc:=@DoIt;',
  26260. ' aSub:=@DoIt;',
  26261. ' aSubSub:=@DoIt;',
  26262. ' aProc:=@Sub;',
  26263. ' aSub:=@Sub;',
  26264. ' aSubSub:=@Sub;',
  26265. ' aProc:=@SubSub;',
  26266. ' aSub:=@SubSub;',
  26267. ' aSubSub:=@SubSub;',
  26268. ' end;',
  26269. ' begin;',
  26270. ' end;',
  26271. 'begin;',
  26272. ' aProc:=@Sub;',
  26273. ' b:=aProc=@Sub;',
  26274. ' b:=@Sub=aProc;',
  26275. 'end;',
  26276. 'begin',
  26277. '']);
  26278. ConvertProgram;
  26279. CheckSource('TestProcType_Nested',
  26280. LinesToStr([ // statements
  26281. 'this.DoIt = function (vJ) {',
  26282. ' var aProc = null;',
  26283. ' var b = false;',
  26284. ' function Sub(vK) {',
  26285. ' var aSub = null;',
  26286. ' function SubSub(vK) {',
  26287. ' var aSubSub = null;',
  26288. ' aProc = $mod.DoIt;',
  26289. ' aSub = $mod.DoIt;',
  26290. ' aSubSub = $mod.DoIt;',
  26291. ' aProc = Sub;',
  26292. ' aSub = Sub;',
  26293. ' aSubSub = Sub;',
  26294. ' aProc = SubSub;',
  26295. ' aSub = SubSub;',
  26296. ' aSubSub = SubSub;',
  26297. ' };',
  26298. ' };',
  26299. ' aProc = Sub;',
  26300. ' b = rtl.eqCallback(aProc, Sub);',
  26301. ' b = rtl.eqCallback(Sub, aProc);',
  26302. '};',
  26303. '']),
  26304. LinesToStr([ // $mod.$main
  26305. '']));
  26306. end;
  26307. procedure TTestModule.TestProcType_NestedOfObject;
  26308. begin
  26309. StartProgram(false);
  26310. Add([
  26311. 'type',
  26312. ' TProcInt = procedure(vI: longint = 1) of object;',
  26313. ' TObject = class',
  26314. ' procedure DoIt(vJ: longint);',
  26315. ' end;',
  26316. 'procedure TObject.DoIt(vJ: longint);',
  26317. 'var aProc: TProcInt;',
  26318. ' b: boolean;',
  26319. ' procedure Sub(vK: longint);',
  26320. ' var aSub: TProcInt;',
  26321. ' procedure SubSub(vK: longint);',
  26322. ' var aSubSub: TProcInt;',
  26323. ' begin;',
  26324. ' aProc:=@DoIt;',
  26325. ' aSub:=@DoIt;',
  26326. ' aSubSub:=@DoIt;',
  26327. ' aProc:=@Sub;',
  26328. ' aSub:=@Sub;',
  26329. ' aSubSub:=@Sub;',
  26330. ' aProc:=@SubSub;',
  26331. ' aSub:=@SubSub;',
  26332. ' aSubSub:=@SubSub;',
  26333. ' end;',
  26334. ' begin;',
  26335. ' end;',
  26336. 'begin;',
  26337. ' aProc:=@Sub;',
  26338. ' b:=aProc=@Sub;',
  26339. ' b:=@Sub=aProc;',
  26340. 'end;',
  26341. 'begin',
  26342. '']);
  26343. ConvertProgram;
  26344. CheckSource('TestProcType_Nested',
  26345. LinesToStr([ // statements
  26346. 'rtl.createClass(this, "TObject", null, function () {',
  26347. ' this.$init = function () {',
  26348. ' };',
  26349. ' this.$final = function () {',
  26350. ' };',
  26351. ' this.DoIt = function (vJ) {',
  26352. ' var $Self = this;',
  26353. ' var aProc = null;',
  26354. ' var b = false;',
  26355. ' function Sub(vK) {',
  26356. ' var aSub = null;',
  26357. ' function SubSub(vK) {',
  26358. ' var aSubSub = null;',
  26359. ' aProc = rtl.createCallback($Self, "DoIt");',
  26360. ' aSub = rtl.createCallback($Self, "DoIt");',
  26361. ' aSubSub = rtl.createCallback($Self, "DoIt");',
  26362. ' aProc = Sub;',
  26363. ' aSub = Sub;',
  26364. ' aSubSub = Sub;',
  26365. ' aProc = SubSub;',
  26366. ' aSub = SubSub;',
  26367. ' aSubSub = SubSub;',
  26368. ' };',
  26369. ' };',
  26370. ' aProc = Sub;',
  26371. ' b = rtl.eqCallback(aProc, Sub);',
  26372. ' b = rtl.eqCallback(Sub, aProc);',
  26373. ' };',
  26374. '});',
  26375. '']),
  26376. LinesToStr([ // $mod.$main
  26377. '']));
  26378. end;
  26379. procedure TTestModule.TestProcType_ReferenceToProc;
  26380. begin
  26381. StartProgram(false);
  26382. Add([
  26383. 'type',
  26384. ' TProcRef = reference to procedure(i: longint = 0);',
  26385. ' TFuncRef = reference to function(i: longint = 0): longint;',
  26386. 'var',
  26387. ' p: TProcRef;',
  26388. ' f: TFuncRef;',
  26389. 'procedure DoIt(i: longint);',
  26390. 'begin',
  26391. 'end;',
  26392. 'function GetIt(i: longint): longint;',
  26393. 'begin',
  26394. ' p:=@DoIt;',
  26395. ' f:=@GetIt;',
  26396. ' f;',
  26397. ' f();',
  26398. ' f(1);',
  26399. 'end;',
  26400. 'begin',
  26401. ' p:=@DoIt;',
  26402. ' f:=@GetIt;',
  26403. ' f;',
  26404. ' f();',
  26405. ' f(1);',
  26406. ' p:=TProcRef(f);',
  26407. '']);
  26408. ConvertProgram;
  26409. CheckSource('TestProcType_ReferenceToProc',
  26410. LinesToStr([ // statements
  26411. 'this.p = null;',
  26412. 'this.f = null;',
  26413. 'this.DoIt = function (i) {',
  26414. '};',
  26415. 'this.GetIt = function (i) {',
  26416. ' var Result = 0;',
  26417. ' $mod.p = $mod.DoIt;',
  26418. ' $mod.f = $mod.GetIt;',
  26419. ' $mod.f(0);',
  26420. ' $mod.f(0);',
  26421. ' $mod.f(1);',
  26422. ' return Result;',
  26423. '};',
  26424. '']),
  26425. LinesToStr([ // $mod.$main
  26426. '$mod.p = $mod.DoIt;',
  26427. '$mod.f = $mod.GetIt;',
  26428. '$mod.f(0);',
  26429. '$mod.f(0);',
  26430. '$mod.f(1);',
  26431. '$mod.p = $mod.f;',
  26432. '']));
  26433. end;
  26434. procedure TTestModule.TestProcType_ReferenceToMethod;
  26435. begin
  26436. StartProgram(false);
  26437. Add([
  26438. 'type',
  26439. ' TFuncRef = reference to function(i: longint = 5): longint;',
  26440. ' TObject = class',
  26441. ' function Grow(s: longint): longint;',
  26442. ' end;',
  26443. 'var',
  26444. ' f: tfuncref;',
  26445. 'function tobject.grow(s: longint): longint;',
  26446. ' function GrowSub(i: longint): longint;',
  26447. ' begin',
  26448. ' f:=@grow;',
  26449. ' f:=@growsub;',
  26450. ' end;',
  26451. 'begin',
  26452. ' f:=@grow;',
  26453. ' f:=@growsub;',
  26454. 'end;',
  26455. 'begin',
  26456. '']);
  26457. ConvertProgram;
  26458. CheckSource('TestProcType_ReferenceToMethod',
  26459. LinesToStr([ // statements
  26460. 'rtl.createClass(this, "TObject", null, function () {',
  26461. ' this.$init = function () {',
  26462. ' };',
  26463. ' this.$final = function () {',
  26464. ' };',
  26465. ' this.Grow = function (s) {',
  26466. ' var $Self = this;',
  26467. ' var Result = 0;',
  26468. ' function GrowSub(i) {',
  26469. ' var Result = 0;',
  26470. ' $mod.f = rtl.createCallback($Self, "Grow");',
  26471. ' $mod.f = GrowSub;',
  26472. ' return Result;',
  26473. ' };',
  26474. ' $mod.f = rtl.createCallback($Self, "Grow");',
  26475. ' $mod.f = GrowSub;',
  26476. ' return Result;',
  26477. ' };',
  26478. '});',
  26479. 'this.f = null;',
  26480. '']),
  26481. LinesToStr([ // $mod.$main
  26482. '']));
  26483. end;
  26484. procedure TTestModule.TestProcType_Typecast;
  26485. begin
  26486. StartProgram(false);
  26487. Add([
  26488. 'type',
  26489. ' TNotifyEvent = procedure(Sender: Pointer) of object;',
  26490. ' TEvent = procedure of object;',
  26491. ' TGetter = function:longint of object;',
  26492. ' TProcA = procedure(i: longint);',
  26493. ' TFuncB = function(i, j: longint): longint;',
  26494. 'procedure DoIt(); varargs; begin end;',
  26495. 'var',
  26496. ' Notify: tnotifyevent;',
  26497. ' Event: tevent;',
  26498. ' Getter: tgetter;',
  26499. ' ProcA: tproca;',
  26500. ' FuncB: tfuncb;',
  26501. ' p: pointer;',
  26502. 'begin',
  26503. ' notify:=tnotifyevent(event);',
  26504. ' event:=tevent(event);',
  26505. ' event:=tevent(notify);',
  26506. ' event:=tevent(getter);',
  26507. ' event:=tevent(proca);',
  26508. ' proca:=tproca(funcb);',
  26509. ' funcb:=tfuncb(funcb);',
  26510. ' funcb:=tfuncb(proca);',
  26511. ' funcb:=tfuncb(getter);',
  26512. ' proca:=tproca(p);',
  26513. ' funcb:=tfuncb(p);',
  26514. ' getter:=tgetter(p);',
  26515. ' p:=pointer(notify);',
  26516. ' p:=notify;',
  26517. ' p:=pointer(proca);',
  26518. ' p:=proca;',
  26519. ' p:=pointer(funcb);',
  26520. ' p:=funcb;',
  26521. ' doit(Pointer(notify),pointer(event),pointer(proca));',
  26522. '']);
  26523. ConvertProgram;
  26524. CheckSource('TestProcType_Typecast',
  26525. LinesToStr([ // statements
  26526. 'this.DoIt = function () {',
  26527. '};',
  26528. 'this.Notify = null;',
  26529. 'this.Event = null;',
  26530. 'this.Getter = null;',
  26531. 'this.ProcA = null;',
  26532. 'this.FuncB = null;',
  26533. 'this.p = null;',
  26534. '']),
  26535. LinesToStr([ // $mod.$main
  26536. '$mod.Notify = $mod.Event;',
  26537. '$mod.Event = $mod.Event;',
  26538. '$mod.Event = $mod.Notify;',
  26539. '$mod.Event = $mod.Getter;',
  26540. '$mod.Event = $mod.ProcA;',
  26541. '$mod.ProcA = $mod.FuncB;',
  26542. '$mod.FuncB = $mod.FuncB;',
  26543. '$mod.FuncB = $mod.ProcA;',
  26544. '$mod.FuncB = $mod.Getter;',
  26545. '$mod.ProcA = $mod.p;',
  26546. '$mod.FuncB = $mod.p;',
  26547. '$mod.Getter = $mod.p;',
  26548. '$mod.p = $mod.Notify;',
  26549. '$mod.p = $mod.Notify;',
  26550. '$mod.p = $mod.ProcA;',
  26551. '$mod.p = $mod.ProcA;',
  26552. '$mod.p = $mod.FuncB;',
  26553. '$mod.p = $mod.FuncB;',
  26554. '$mod.DoIt($mod.Notify, $mod.Event, $mod.ProcA);',
  26555. '']));
  26556. end;
  26557. procedure TTestModule.TestProcType_PassProcToUntyped;
  26558. begin
  26559. StartProgram(false);
  26560. Add([
  26561. 'type',
  26562. ' TEvent = procedure of object;',
  26563. ' TFunc = function: longint;',
  26564. 'procedure DoIt(); varargs; begin end;',
  26565. 'procedure DoSome(const a; var b; p: pointer); begin end;',
  26566. 'var',
  26567. ' Event: tevent;',
  26568. ' Func: TFunc;',
  26569. 'begin',
  26570. ' doit(event,func);',
  26571. ' dosome(event,event,event);',
  26572. ' dosome(func,func,func);',
  26573. '']);
  26574. ConvertProgram;
  26575. CheckSource('TestProcType_PassProcToUntyped',
  26576. LinesToStr([ // statements
  26577. 'this.DoIt = function () {',
  26578. '};',
  26579. 'this.DoSome = function (a, b, p) {',
  26580. '};',
  26581. 'this.Event = null;',
  26582. 'this.Func = null;',
  26583. '']),
  26584. LinesToStr([ // $mod.$main
  26585. '$mod.DoIt($mod.Event, $mod.Func);',
  26586. '$mod.DoSome($mod.Event, {',
  26587. ' p: $mod,',
  26588. ' get: function () {',
  26589. ' return this.p.Event;',
  26590. ' },',
  26591. ' set: function (v) {',
  26592. ' this.p.Event = v;',
  26593. ' }',
  26594. '}, $mod.Event);',
  26595. '$mod.DoSome($mod.Func, {',
  26596. ' p: $mod,',
  26597. ' get: function () {',
  26598. ' return this.p.Func;',
  26599. ' },',
  26600. ' set: function (v) {',
  26601. ' this.p.Func = v;',
  26602. ' }',
  26603. '}, $mod.Func);',
  26604. '']));
  26605. end;
  26606. procedure TTestModule.TestProcType_PassProcToArray;
  26607. begin
  26608. StartProgram(false);
  26609. Add([
  26610. 'type',
  26611. ' TFunc = function: longint;',
  26612. ' TArrFunc = array of TFunc;',
  26613. 'procedure DoIt(Arr: TArrFunc); begin end;',
  26614. 'function GetIt: longint; begin end;',
  26615. 'var',
  26616. ' Func: tfunc;',
  26617. 'begin',
  26618. ' doit([]);',
  26619. ' doit([@GetIt]);',
  26620. ' doit([Func]);',
  26621. '']);
  26622. ConvertProgram;
  26623. CheckSource('TestProcType_PassProcToArray',
  26624. LinesToStr([ // statements
  26625. 'this.DoIt = function (Arr) {',
  26626. '};',
  26627. 'this.GetIt = function () {',
  26628. ' var Result = 0;',
  26629. ' return Result;',
  26630. '};',
  26631. 'this.Func = null;',
  26632. '']),
  26633. LinesToStr([ // $mod.$main
  26634. '$mod.DoIt([]);',
  26635. '$mod.DoIt([$mod.GetIt]);',
  26636. '$mod.DoIt([$mod.Func]);',
  26637. '']));
  26638. end;
  26639. procedure TTestModule.TestProcType_SafeCallObjFPC;
  26640. begin
  26641. StartProgram(false);
  26642. Add([
  26643. '{$modeswitch externalclass}',
  26644. 'type',
  26645. ' TProc = reference to procedure(i: longint); safecall;',
  26646. ' TEvent = procedure(i: longint) of object; safecall;',
  26647. ' TExtA = class external name ''ExtObj''',
  26648. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  26649. ' procedure DoSome(Id: longint = 1);',
  26650. ' procedure SetOnClick(const e: TEvent);',
  26651. ' property OnClick: TEvent write SetOnClick;',
  26652. ' class procedure Fly(Id: longint = 1); static;',
  26653. ' procedure SetOnShow(const p: TProc);',
  26654. ' property OnShow: TProc write SetOnShow;',
  26655. ' end;',
  26656. 'procedure Run(i: longint = 1);',
  26657. 'begin',
  26658. 'end;',
  26659. 'var',
  26660. ' Obj: texta;',
  26661. ' e: TEvent;',
  26662. ' p: TProc;',
  26663. 'begin',
  26664. ' e:=e;',
  26665. ' e:[email protected];',
  26666. ' e:[email protected];',
  26667. ' e:=TEvent(@obj.dosome);', // no safecall
  26668. ' obj.OnClick:[email protected];',
  26669. ' obj.OnClick:[email protected];',
  26670. ' obj.setonclick(@obj.doit);',
  26671. ' obj.setonclick(@obj.dosome);',
  26672. ' p:=@Run;',
  26673. ' p:[email protected];',
  26674. ' obj.OnShow:=@Run;',
  26675. ' obj.OnShow:[email protected];',
  26676. ' obj.setOnShow(@Run);',
  26677. ' obj.setOnShow(@TExtA.Fly);',
  26678. ' with obj do begin',
  26679. ' e:=@doit;',
  26680. ' e:=@dosome;',
  26681. ' OnClick:=@doit;',
  26682. ' OnClick:=@dosome;',
  26683. ' setonclick(@doit);',
  26684. ' setonclick(@dosome);',
  26685. ' OnShow:=@Run;',
  26686. ' setOnShow(@Run);',
  26687. ' end;']);
  26688. ConvertProgram;
  26689. CheckSource('TestProcType_SafeCallObjFPC',
  26690. LinesToStr([ // statements
  26691. 'this.Run = function (i) {',
  26692. '};',
  26693. 'this.Obj = null;',
  26694. 'this.e = null;',
  26695. 'this.p = null;',
  26696. '']),
  26697. LinesToStr([ // $mod.$main
  26698. '$mod.e = $mod.e;',
  26699. '$mod.e = rtl.createSafeCallback($mod.Obj, "$Execute");',
  26700. '$mod.e = rtl.createSafeCallback($mod.Obj, "DoSome");',
  26701. '$mod.e = rtl.createCallback($mod.Obj, "DoSome");',
  26702. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  26703. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  26704. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  26705. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  26706. '$mod.p = rtl.createSafeCallback($mod, "Run");',
  26707. '$mod.p = rtl.createSafeCallback(ExtObj, "Fly");',
  26708. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26709. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  26710. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26711. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  26712. 'var $with = $mod.Obj;',
  26713. '$mod.e = rtl.createSafeCallback($with, "$Execute");',
  26714. '$mod.e = rtl.createSafeCallback($with, "DoSome");',
  26715. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  26716. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  26717. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  26718. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  26719. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26720. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26721. '']));
  26722. end;
  26723. procedure TTestModule.TestProcType_SafeCallDelphi;
  26724. begin
  26725. StartProgram(false);
  26726. Add([
  26727. '{$mode delphi}',
  26728. '{$modeswitch externalclass}',
  26729. 'type',
  26730. ' TProc = reference to procedure(i: longint); safecall;',
  26731. ' TEvent = procedure(i: longint) of object; safecall;',
  26732. ' TExtA = class external name ''ExtObj''',
  26733. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  26734. ' procedure DoSome(Id: longint = 1);',
  26735. ' procedure SetOnClick(const e: TEvent);',
  26736. ' property OnClick: TEvent write SetOnClick;',
  26737. ' class procedure Fly(Id: longint = 1); static;',
  26738. ' procedure SetOnShow(const p: TProc);',
  26739. ' property OnShow: TProc write SetOnShow;',
  26740. ' end;',
  26741. 'procedure Run(i: longint = 1);',
  26742. 'begin',
  26743. 'end;',
  26744. 'var',
  26745. ' Obj: texta;',
  26746. ' e: TEvent;',
  26747. ' p: TProc;',
  26748. 'begin',
  26749. ' e:=e;',
  26750. ' e:=obj.doit;',
  26751. ' e:=obj.dosome;',
  26752. ' e:=TEvent(@obj.dosome);', // no safecall
  26753. ' obj.OnClick:=obj.doit;',
  26754. ' obj.OnClick:=obj.dosome;',
  26755. ' obj.setonclick(obj.doit);',
  26756. ' obj.setonclick(obj.dosome);',
  26757. ' p:=Run;',
  26758. ' p:=TExtA.Fly;',
  26759. ' obj.OnShow:=Run;',
  26760. ' obj.OnShow:=TExtA.Fly;',
  26761. ' obj.setOnShow(Run);',
  26762. ' obj.setOnShow(TExtA.Fly);',
  26763. ' with obj do begin',
  26764. ' e:=doit;',
  26765. ' e:=dosome;',
  26766. ' OnClick:=doit;',
  26767. ' OnClick:=dosome;',
  26768. ' setonclick(doit);',
  26769. ' setonclick(dosome);',
  26770. ' OnShow:=@Run;',
  26771. ' setOnShow(@Run);',
  26772. ' end;']);
  26773. ConvertProgram;
  26774. CheckSource('TestProcType_SafeCallDelphi',
  26775. LinesToStr([ // statements
  26776. 'this.Run = function (i) {',
  26777. '};',
  26778. 'this.Obj = null;',
  26779. 'this.e = null;',
  26780. 'this.p = null;',
  26781. '']),
  26782. LinesToStr([ // $mod.$main
  26783. '$mod.e = $mod.e;',
  26784. '$mod.e = rtl.createSafeCallback($mod.Obj, "$Execute");',
  26785. '$mod.e = rtl.createSafeCallback($mod.Obj, "DoSome");',
  26786. '$mod.e = rtl.createCallback($mod.Obj, "DoSome");',
  26787. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  26788. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  26789. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  26790. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  26791. '$mod.p = rtl.createSafeCallback($mod, "Run");',
  26792. '$mod.p = rtl.createSafeCallback(ExtObj, "Fly");',
  26793. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26794. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  26795. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26796. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  26797. 'var $with = $mod.Obj;',
  26798. '$mod.e = rtl.createSafeCallback($with, "$Execute");',
  26799. '$mod.e = rtl.createSafeCallback($with, "DoSome");',
  26800. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  26801. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  26802. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  26803. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  26804. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26805. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26806. '']));
  26807. end;
  26808. procedure TTestModule.TestPointer;
  26809. begin
  26810. StartProgram(false);
  26811. Add(['type',
  26812. ' TObject = class end;',
  26813. ' TClass = class of TObject;',
  26814. ' TArrInt = array of longint;',
  26815. 'const',
  26816. ' n = nil;',
  26817. 'var',
  26818. ' v: jsvalue;',
  26819. ' Obj: tobject;',
  26820. ' C: tclass;',
  26821. ' a: tarrint;',
  26822. ' p: Pointer = nil;',
  26823. ' s: string;',
  26824. 'begin',
  26825. ' p:=p;',
  26826. ' p:=nil;',
  26827. ' if p=nil then;',
  26828. ' if nil=p then;',
  26829. ' if Assigned(p) then;',
  26830. ' p:=Pointer(v);',
  26831. ' p:=obj;',
  26832. ' p:=c;',
  26833. ' p:=a;',
  26834. ' p:=tobject;',
  26835. ' obj:=TObject(p);',
  26836. ' c:=TClass(p);',
  26837. ' a:=TArrInt(p);',
  26838. ' p:=n;',
  26839. ' p:=Pointer(a);',
  26840. ' p:=pointer(s);',
  26841. ' s:=string(p);',
  26842. '']);
  26843. ConvertProgram;
  26844. CheckSource('TestPointer',
  26845. LinesToStr([ // statements
  26846. 'rtl.createClass(this, "TObject", null, function () {',
  26847. ' this.$init = function () {',
  26848. ' };',
  26849. ' this.$final = function () {',
  26850. ' };',
  26851. '});',
  26852. 'this.n = null;',
  26853. 'this.v = undefined;',
  26854. 'this.Obj = null;',
  26855. 'this.C = null;',
  26856. 'this.a = [];',
  26857. 'this.p = null;',
  26858. 'this.s = "";',
  26859. '']),
  26860. LinesToStr([ // $mod.$main
  26861. '$mod.p = $mod.p;',
  26862. '$mod.p = null;',
  26863. 'if ($mod.p === null) ;',
  26864. 'if (null === $mod.p) ;',
  26865. 'if ($mod.p != null) ;',
  26866. '$mod.p = $mod.v;',
  26867. '$mod.p = $mod.Obj;',
  26868. '$mod.p = $mod.C;',
  26869. '$mod.p = $mod.a;',
  26870. '$mod.p = $mod.TObject;',
  26871. '$mod.Obj = $mod.p;',
  26872. '$mod.C = $mod.p;',
  26873. '$mod.a = $mod.p;',
  26874. '$mod.p = null;',
  26875. '$mod.p = $mod.a;',
  26876. '$mod.p = $mod.s;',
  26877. '$mod.s = $mod.p;',
  26878. '']));
  26879. end;
  26880. procedure TTestModule.TestPointer_Proc;
  26881. begin
  26882. StartProgram(false);
  26883. Add('type');
  26884. Add(' TObject = class');
  26885. Add(' procedure DoIt; virtual; abstract;');
  26886. Add(' end;');
  26887. Add('procedure DoSome; begin end;');
  26888. Add('var');
  26889. Add(' o: TObject;');
  26890. Add(' p: Pointer;');
  26891. Add('begin');
  26892. Add(' p:=@DoSome;');
  26893. Add(' p:[email protected];');
  26894. ConvertProgram;
  26895. CheckSource('TestPointer_Proc',
  26896. LinesToStr([ // statements
  26897. 'rtl.createClass(this, "TObject", null, function () {',
  26898. ' this.$init = function () {',
  26899. ' };',
  26900. ' this.$final = function () {',
  26901. ' };',
  26902. '});',
  26903. 'this.DoSome = function () {',
  26904. '};',
  26905. 'this.o = null;',
  26906. 'this.p = null;',
  26907. '']),
  26908. LinesToStr([ // $mod.$main
  26909. '$mod.p = $mod.DoSome;',
  26910. '$mod.p = rtl.createCallback($mod.o, "DoIt");',
  26911. '']));
  26912. end;
  26913. procedure TTestModule.TestPointer_AssignRecordFail;
  26914. begin
  26915. StartProgram(false);
  26916. Add('type');
  26917. Add(' TRec = record end;');
  26918. Add('var');
  26919. Add(' p: Pointer;');
  26920. Add(' r: TRec;');
  26921. Add('begin');
  26922. Add(' p:=r;');
  26923. SetExpectedPasResolverError('Incompatible types: got "TRec" expected "Pointer"',
  26924. nIncompatibleTypesGotExpected);
  26925. ConvertProgram;
  26926. end;
  26927. procedure TTestModule.TestPointer_AssignStaticArrayFail;
  26928. begin
  26929. StartProgram(false);
  26930. Add('type');
  26931. Add(' TArr = array[boolean] of longint;');
  26932. Add('var');
  26933. Add(' p: Pointer;');
  26934. Add(' a: TArr;');
  26935. Add('begin');
  26936. Add(' p:=a;');
  26937. SetExpectedPasResolverError('Incompatible types: got "TArr" expected "Pointer"',
  26938. nIncompatibleTypesGotExpected);
  26939. ConvertProgram;
  26940. end;
  26941. procedure TTestModule.TestPointer_TypeCastJSValueToPointer;
  26942. begin
  26943. StartProgram(false);
  26944. Add([
  26945. 'procedure DoIt(args: array of jsvalue); begin end;',
  26946. 'procedure DoAll; varargs; begin end;',
  26947. 'var',
  26948. ' v: jsvalue;',
  26949. 'begin',
  26950. ' DoIt([pointer(v)]);',
  26951. ' DoAll(pointer(v));',
  26952. '']);
  26953. ConvertProgram;
  26954. CheckSource('TestPointer_TypeCastJSValueToPointer',
  26955. LinesToStr([ // statements
  26956. 'this.DoIt = function (args) {',
  26957. '};',
  26958. 'this.DoAll = function () {',
  26959. '};',
  26960. 'this.v = undefined;',
  26961. '']),
  26962. LinesToStr([ // $mod.$main
  26963. '$mod.DoIt([$mod.v]);',
  26964. '$mod.DoAll($mod.v);',
  26965. '']));
  26966. end;
  26967. procedure TTestModule.TestPointer_NonRecordFail;
  26968. begin
  26969. StartProgram(false);
  26970. Add([
  26971. 'type',
  26972. ' p = ^longint;',
  26973. 'begin',
  26974. '']);
  26975. SetExpectedPasResolverError('Not supported: pointer of Longint',nNotSupportedX);
  26976. ConvertProgram;
  26977. end;
  26978. procedure TTestModule.TestPointer_AnonymousArgTypeFail;
  26979. begin
  26980. StartProgram(false);
  26981. Add([
  26982. 'procedure DoIt(p: ^longint); begin end;',
  26983. 'begin',
  26984. '']);
  26985. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  26986. ConvertProgram;
  26987. end;
  26988. procedure TTestModule.TestPointer_AnonymousVarTypeFail;
  26989. begin
  26990. StartProgram(false);
  26991. Add([
  26992. 'var p: ^longint;',
  26993. 'begin',
  26994. '']);
  26995. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  26996. ConvertProgram;
  26997. end;
  26998. procedure TTestModule.TestPointer_AnonymousResultTypeFail;
  26999. begin
  27000. StartProgram(false);
  27001. Add([
  27002. 'function DoIt: ^longint; begin end;',
  27003. 'begin',
  27004. '']);
  27005. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  27006. ConvertProgram;
  27007. end;
  27008. procedure TTestModule.TestPointer_AddrOperatorFail;
  27009. begin
  27010. StartProgram(false);
  27011. Add([
  27012. 'var i: longint;',
  27013. 'begin',
  27014. ' if @i=nil then ;',
  27015. '']);
  27016. SetExpectedConverterError('illegal qualifier "@" in front of "i:Longint"',nIllegalQualifierInFrontOf);
  27017. ConvertProgram;
  27018. end;
  27019. procedure TTestModule.TestPointer_ArrayParamsFail;
  27020. begin
  27021. StartProgram(false);
  27022. Add([
  27023. 'var',
  27024. ' p: Pointer;',
  27025. 'begin',
  27026. ' p:=p[1];',
  27027. '']);
  27028. SetExpectedPasResolverError('illegal qualifier "[" after "Pointer"',nIllegalQualifierAfter);
  27029. ConvertProgram;
  27030. end;
  27031. procedure TTestModule.TestPointer_PointerAddFail;
  27032. begin
  27033. StartProgram(false);
  27034. Add([
  27035. 'var',
  27036. ' p: Pointer;',
  27037. 'begin',
  27038. ' p:=p+1;',
  27039. '']);
  27040. SetExpectedPasResolverError('Operator is not overloaded: "Pointer" + "Longint"',nOperatorIsNotOverloadedAOpB);
  27041. ConvertProgram;
  27042. end;
  27043. procedure TTestModule.TestPointer_IncPointerFail;
  27044. begin
  27045. StartProgram(false);
  27046. Add([
  27047. 'var',
  27048. ' p: Pointer;',
  27049. 'begin',
  27050. ' inc(p,1);',
  27051. '']);
  27052. SetExpectedPasResolverError('Incompatible type arg no. 1: Got "Pointer", expected "integer"',
  27053. nIncompatibleTypeArgNo);
  27054. ConvertProgram;
  27055. end;
  27056. procedure TTestModule.TestPointer_Record;
  27057. begin
  27058. StartProgram(false);
  27059. Add([
  27060. 'type',
  27061. ' TRec = record x: longint; end;',
  27062. ' PRec = ^TRec;',
  27063. 'var',
  27064. ' r: TRec;',
  27065. ' p: PRec;',
  27066. ' q: ^TRec;',
  27067. ' Ptr: pointer;',
  27068. 'begin',
  27069. ' new(p);',
  27070. ' p:=@r;',
  27071. ' r:=p^;',
  27072. ' r.x:=p^.x;',
  27073. ' p^.x:=r.x;',
  27074. ' if p^.x=3 then ;',
  27075. ' if 4=p^.x then ;',
  27076. ' dispose(p);',
  27077. ' new(q);',
  27078. ' dispose(q);',
  27079. ' Ptr:=p;',
  27080. ' p:=PRec(ptr);',
  27081. '']);
  27082. ConvertProgram;
  27083. CheckSource('TestPointer_Record',
  27084. LinesToStr([ // statements
  27085. 'rtl.recNewT(this, "TRec", function () {',
  27086. ' this.x = 0;',
  27087. ' this.$eq = function (b) {',
  27088. ' return this.x === b.x;',
  27089. ' };',
  27090. ' this.$assign = function (s) {',
  27091. ' this.x = s.x;',
  27092. ' return this;',
  27093. ' };',
  27094. '});',
  27095. 'this.r = this.TRec.$new();',
  27096. 'this.p = null;',
  27097. 'this.q = null;',
  27098. 'this.Ptr = null;',
  27099. '']),
  27100. LinesToStr([ // $mod.$main
  27101. '$mod.p = $mod.TRec.$new();',
  27102. '$mod.p = $mod.r;',
  27103. '$mod.r.$assign($mod.p);',
  27104. '$mod.r.x = $mod.p.x;',
  27105. '$mod.p.x = $mod.r.x;',
  27106. 'if ($mod.p.x === 3) ;',
  27107. 'if (4 === $mod.p.x) ;',
  27108. '$mod.p = null;',
  27109. '$mod.q = $mod.TRec.$new();',
  27110. '$mod.q = null;',
  27111. '$mod.Ptr = $mod.p;',
  27112. '$mod.p = $mod.Ptr;',
  27113. '']));
  27114. end;
  27115. procedure TTestModule.TestPointer_RecordArg;
  27116. begin
  27117. StartProgram(false);
  27118. Add([
  27119. '{$modeswitch autoderef}',
  27120. 'type',
  27121. ' TRec = record x: longint; end;',
  27122. ' PRec = ^TRec;',
  27123. 'function DoIt(const a: PRec; var b: PRec; out c: PRec): TRec;',
  27124. 'begin',
  27125. ' a.x:=a.x;',
  27126. ' a^.x:=a^.x;',
  27127. ' with a^ do',
  27128. ' x:=x;',
  27129. 'end;',
  27130. 'function GetIt(p: PRec): PRec;',
  27131. 'begin',
  27132. ' p.x:=p.x;',
  27133. ' p^.x:=p^.x;',
  27134. ' with p^ do',
  27135. ' x:=x;',
  27136. 'end;',
  27137. 'var',
  27138. ' r: TRec;',
  27139. ' p: PRec;',
  27140. 'begin',
  27141. ' p:=GetIt(p);',
  27142. ' p^:=GetIt(@r)^;',
  27143. ' DoIt(p,p,p);',
  27144. ' DoIt(@r,p,p);',
  27145. '']);
  27146. ConvertProgram;
  27147. CheckSource('TestPointer_RecordArg',
  27148. LinesToStr([ // statements
  27149. 'rtl.recNewT(this, "TRec", function () {',
  27150. ' this.x = 0;',
  27151. ' this.$eq = function (b) {',
  27152. ' return this.x === b.x;',
  27153. ' };',
  27154. ' this.$assign = function (s) {',
  27155. ' this.x = s.x;',
  27156. ' return this;',
  27157. ' };',
  27158. '});',
  27159. 'this.DoIt = function (a, b, c) {',
  27160. ' var Result = $mod.TRec.$new();',
  27161. ' a.x = a.x;',
  27162. ' a.x = a.x;',
  27163. ' a.x = a.x;',
  27164. ' return Result;',
  27165. '};',
  27166. 'this.GetIt = function (p) {',
  27167. ' var Result = null;',
  27168. ' p.x = p.x;',
  27169. ' p.x = p.x;',
  27170. ' p.x = p.x;',
  27171. ' return Result;',
  27172. '};',
  27173. 'this.r = this.TRec.$new();',
  27174. 'this.p = null;',
  27175. '']),
  27176. LinesToStr([ // $mod.$main
  27177. '$mod.p = $mod.GetIt($mod.p);',
  27178. '$mod.p.$assign($mod.GetIt($mod.r));',
  27179. '$mod.DoIt($mod.p, {',
  27180. ' p: $mod,',
  27181. ' get: function () {',
  27182. ' return this.p.p;',
  27183. ' },',
  27184. ' set: function (v) {',
  27185. ' this.p.p = v;',
  27186. ' }',
  27187. '}, {',
  27188. ' p: $mod,',
  27189. ' get: function () {',
  27190. ' return this.p.p;',
  27191. ' },',
  27192. ' set: function (v) {',
  27193. ' this.p.p = v;',
  27194. ' }',
  27195. '});',
  27196. '$mod.DoIt($mod.r, {',
  27197. ' p: $mod,',
  27198. ' get: function () {',
  27199. ' return this.p.p;',
  27200. ' },',
  27201. ' set: function (v) {',
  27202. ' this.p.p = v;',
  27203. ' }',
  27204. '}, {',
  27205. ' p: $mod,',
  27206. ' get: function () {',
  27207. ' return this.p.p;',
  27208. ' },',
  27209. ' set: function (v) {',
  27210. ' this.p.p = v;',
  27211. ' }',
  27212. '});',
  27213. '']));
  27214. end;
  27215. procedure TTestModule.TestJSValue_AssignToJSValue;
  27216. begin
  27217. StartProgram(false);
  27218. Add('var');
  27219. Add(' v: jsvalue;');
  27220. Add(' i: longint;');
  27221. Add(' s: string;');
  27222. Add(' b: boolean;');
  27223. Add(' d: double;');
  27224. Add(' p: pointer;');
  27225. Add('begin');
  27226. Add(' v:=v;');
  27227. Add(' v:=1;');
  27228. Add(' v:=i;');
  27229. Add(' v:='''';');
  27230. Add(' v:=''c'';');
  27231. Add(' v:=''foo'';');
  27232. Add(' v:=s;');
  27233. Add(' v:=false;');
  27234. Add(' v:=true;');
  27235. Add(' v:=b;');
  27236. Add(' v:=0.1;');
  27237. Add(' v:=d;');
  27238. Add(' v:=nil;');
  27239. Add(' v:=p;');
  27240. ConvertProgram;
  27241. CheckSource('TestJSValue_AssignToJSValue',
  27242. LinesToStr([ // statements
  27243. 'this.v = undefined;',
  27244. 'this.i = 0;',
  27245. 'this.s = "";',
  27246. 'this.b = false;',
  27247. 'this.d = 0.0;',
  27248. 'this.p = null;',
  27249. '']),
  27250. LinesToStr([ // $mod.$main
  27251. '$mod.v = $mod.v;',
  27252. '$mod.v = 1;',
  27253. '$mod.v = $mod.i;',
  27254. '$mod.v = "";',
  27255. '$mod.v = "c";',
  27256. '$mod.v = "foo";',
  27257. '$mod.v = $mod.s;',
  27258. '$mod.v = false;',
  27259. '$mod.v = true;',
  27260. '$mod.v = $mod.b;',
  27261. '$mod.v = 0.1;',
  27262. '$mod.v = $mod.d;',
  27263. '$mod.v = null;',
  27264. '$mod.v = $mod.p;',
  27265. '']));
  27266. end;
  27267. procedure TTestModule.TestJSValue_TypeCastToBaseType;
  27268. begin
  27269. StartProgram(false);
  27270. Add('type');
  27271. Add(' integer = longint;');
  27272. Add(' TYesNo = boolean;');
  27273. Add(' TFloat = double;');
  27274. Add(' TCaption = string;');
  27275. Add(' TChar = char;');
  27276. Add('var');
  27277. Add(' v: jsvalue;');
  27278. Add(' i: integer;');
  27279. Add(' s: TCaption;');
  27280. Add(' b: TYesNo;');
  27281. Add(' d: TFloat;');
  27282. Add(' c: char;');
  27283. Add('begin');
  27284. Add(' i:=longint(v);');
  27285. Add(' i:=integer(v);');
  27286. Add(' s:=string(v);');
  27287. Add(' s:=TCaption(v);');
  27288. Add(' b:=boolean(v);');
  27289. Add(' b:=TYesNo(v);');
  27290. Add(' d:=double(v);');
  27291. Add(' d:=TFloat(v);');
  27292. Add(' c:=char(v);');
  27293. Add(' c:=TChar(v);');
  27294. ConvertProgram;
  27295. CheckSource('TestJSValue_TypeCastToBaseType',
  27296. LinesToStr([ // statements
  27297. 'this.v = undefined;',
  27298. 'this.i = 0;',
  27299. 'this.s = "";',
  27300. 'this.b = false;',
  27301. 'this.d = 0.0;',
  27302. 'this.c = "";',
  27303. '']),
  27304. LinesToStr([ // $mod.$main
  27305. '$mod.i = rtl.trunc($mod.v);',
  27306. '$mod.i = rtl.trunc($mod.v);',
  27307. '$mod.s = "" + $mod.v;',
  27308. '$mod.s = "" + $mod.v;',
  27309. '$mod.b = !($mod.v == false);',
  27310. '$mod.b = !($mod.v == false);',
  27311. '$mod.d = rtl.getNumber($mod.v);',
  27312. '$mod.d = rtl.getNumber($mod.v);',
  27313. '$mod.c = rtl.getChar($mod.v);',
  27314. '$mod.c = rtl.getChar($mod.v);',
  27315. '']));
  27316. end;
  27317. procedure TTestModule.TestJSValue_TypecastToJSValue;
  27318. begin
  27319. StartProgram(false);
  27320. Add([
  27321. 'type',
  27322. ' TArr = array of word;',
  27323. ' TRec = record end;',
  27324. ' TSet = set of boolean;',
  27325. 'procedure Fly(v: jsvalue);',
  27326. 'begin',
  27327. 'end;',
  27328. 'var',
  27329. ' a: TArr;',
  27330. ' r: TRec;',
  27331. ' s: TSet;',
  27332. 'begin',
  27333. ' Fly(jsvalue(a));',
  27334. ' Fly(jsvalue(r));',
  27335. ' Fly(jsvalue(s));',
  27336. '']);
  27337. ConvertProgram;
  27338. CheckSource('TestJSValue_TypecastToJSValue',
  27339. LinesToStr([ // statements
  27340. 'rtl.recNewT(this, "TRec", function () {',
  27341. ' this.$eq = function (b) {',
  27342. ' return true;',
  27343. ' };',
  27344. ' this.$assign = function (s) {',
  27345. ' return this;',
  27346. ' };',
  27347. '});',
  27348. 'this.Fly = function (v) {',
  27349. '};',
  27350. 'this.a = [];',
  27351. 'this.r = this.TRec.$new();',
  27352. 'this.s = {};',
  27353. '']),
  27354. LinesToStr([ // $mod.$main
  27355. '$mod.Fly($mod.a);',
  27356. '$mod.Fly($mod.r);',
  27357. '$mod.Fly($mod.s);',
  27358. '']));
  27359. end;
  27360. procedure TTestModule.TestJSValue_Equal;
  27361. begin
  27362. StartProgram(false);
  27363. Add('type');
  27364. Add(' integer = longint;');
  27365. Add(' TYesNo = boolean;');
  27366. Add(' TFloat = double;');
  27367. Add(' TCaption = string;');
  27368. Add(' TChar = char;');
  27369. Add(' TMulti = JSValue;');
  27370. Add('var');
  27371. Add(' v: jsvalue;');
  27372. Add(' i: integer;');
  27373. Add(' s: TCaption;');
  27374. Add(' b: TYesNo;');
  27375. Add(' d: TFloat;');
  27376. Add(' c: char;');
  27377. Add(' m: TMulti;');
  27378. Add('begin');
  27379. Add(' b:=v=v;');
  27380. Add(' b:=v<>v;');
  27381. Add(' b:=v=1;');
  27382. Add(' b:=v<>1;');
  27383. Add(' b:=2=v;');
  27384. Add(' b:=2<>v;');
  27385. Add(' b:=v=i;');
  27386. Add(' b:=i=v;');
  27387. Add(' b:=v=nil;');
  27388. Add(' b:=nil=v;');
  27389. Add(' b:=v=false;');
  27390. Add(' b:=true=v;');
  27391. Add(' b:=v=b;');
  27392. Add(' b:=b=v;');
  27393. Add(' b:=v=s;');
  27394. Add(' b:=s=v;');
  27395. Add(' b:=v=''foo'';');
  27396. Add(' b:=''''=v;');
  27397. Add(' b:=v=d;');
  27398. Add(' b:=d=v;');
  27399. Add(' b:=v=3.4;');
  27400. Add(' b:=5.6=v;');
  27401. Add(' b:=v=c;');
  27402. Add(' b:=c=v;');
  27403. Add(' b:=m=m;');
  27404. Add(' b:=v=m;');
  27405. Add(' b:=m=v;');
  27406. ConvertProgram;
  27407. CheckSource('TestJSValue_Equal',
  27408. LinesToStr([ // statements
  27409. 'this.v = undefined;',
  27410. 'this.i = 0;',
  27411. 'this.s = "";',
  27412. 'this.b = false;',
  27413. 'this.d = 0.0;',
  27414. 'this.c = "";',
  27415. 'this.m = undefined;',
  27416. '']),
  27417. LinesToStr([ // $mod.$main
  27418. '$mod.b = $mod.v == $mod.v;',
  27419. '$mod.b = $mod.v != $mod.v;',
  27420. '$mod.b = $mod.v == 1;',
  27421. '$mod.b = $mod.v != 1;',
  27422. '$mod.b = 2 == $mod.v;',
  27423. '$mod.b = 2 != $mod.v;',
  27424. '$mod.b = $mod.v == $mod.i;',
  27425. '$mod.b = $mod.i == $mod.v;',
  27426. '$mod.b = $mod.v == null;',
  27427. '$mod.b = null == $mod.v;',
  27428. '$mod.b = $mod.v == false;',
  27429. '$mod.b = true == $mod.v;',
  27430. '$mod.b = $mod.v == $mod.b;',
  27431. '$mod.b = $mod.b == $mod.v;',
  27432. '$mod.b = $mod.v == $mod.s;',
  27433. '$mod.b = $mod.s == $mod.v;',
  27434. '$mod.b = $mod.v == "foo";',
  27435. '$mod.b = "" == $mod.v;',
  27436. '$mod.b = $mod.v == $mod.d;',
  27437. '$mod.b = $mod.d == $mod.v;',
  27438. '$mod.b = $mod.v == 3.4;',
  27439. '$mod.b = 5.6 == $mod.v;',
  27440. '$mod.b = $mod.v == $mod.c;',
  27441. '$mod.b = $mod.c == $mod.v;',
  27442. '$mod.b = $mod.m == $mod.m;',
  27443. '$mod.b = $mod.v == $mod.m;',
  27444. '$mod.b = $mod.m == $mod.v;',
  27445. '']));
  27446. end;
  27447. procedure TTestModule.TestJSValue_If;
  27448. begin
  27449. StartProgram(false);
  27450. Add([
  27451. 'procedure Fly(var u);',
  27452. 'begin',
  27453. ' if jsvalue(u) then ;',
  27454. 'end;',
  27455. 'var',
  27456. ' v: jsvalue;',
  27457. 'begin',
  27458. ' if v then ;',
  27459. ' while v do ;',
  27460. ' repeat until v;',
  27461. '']);
  27462. ConvertProgram;
  27463. CheckSource('TestJSValue_If',
  27464. LinesToStr([ // statements
  27465. 'this.Fly = function (u) {',
  27466. ' if (u.get()) ;',
  27467. '};',
  27468. 'this.v = undefined;',
  27469. '']),
  27470. LinesToStr([ // $mod.$main
  27471. 'if ($mod.v) ;',
  27472. 'while($mod.v){',
  27473. '};',
  27474. 'do{',
  27475. '} while(!$mod.v);',
  27476. '']));
  27477. end;
  27478. procedure TTestModule.TestJSValue_Not;
  27479. begin
  27480. StartProgram(false);
  27481. Add([
  27482. 'var',
  27483. ' v: jsvalue;',
  27484. ' b: boolean;',
  27485. 'begin',
  27486. ' b:=not v;',
  27487. ' if not v then ;',
  27488. ' while not v do ;',
  27489. ' repeat until not v;',
  27490. '']);
  27491. ConvertProgram;
  27492. CheckSource('TestJSValue_If',
  27493. LinesToStr([ // statements
  27494. 'this.v = undefined;',
  27495. 'this.b = false;',
  27496. '']),
  27497. LinesToStr([ // $mod.$main
  27498. '$mod.b=!$mod.v;',
  27499. 'if (!$mod.v) ;',
  27500. 'while(!$mod.v){',
  27501. '};',
  27502. 'do{',
  27503. '} while($mod.v);',
  27504. '']));
  27505. end;
  27506. procedure TTestModule.TestJSValue_Enum;
  27507. begin
  27508. StartProgram(false);
  27509. Add('type');
  27510. Add(' TColor = (red, blue);');
  27511. Add(' TRedBlue = TColor;');
  27512. Add('var');
  27513. Add(' v: jsvalue;');
  27514. Add(' e: TColor;');
  27515. Add('begin');
  27516. Add(' v:=e;');
  27517. Add(' v:=TColor(e);');
  27518. Add(' v:=TRedBlue(e);');
  27519. Add(' e:=TColor(v);');
  27520. Add(' e:=TRedBlue(v);');
  27521. ConvertProgram;
  27522. CheckSource('TestJSValue_Enum',
  27523. LinesToStr([ // statements
  27524. 'this.TColor = {',
  27525. ' "0": "red",',
  27526. ' red: 0,',
  27527. ' "1": "blue",',
  27528. ' blue: 1',
  27529. '};',
  27530. 'this.v = undefined;',
  27531. 'this.e = 0;',
  27532. '']),
  27533. LinesToStr([ // $mod.$main
  27534. '$mod.v = $mod.e;',
  27535. '$mod.v = $mod.e;',
  27536. '$mod.v = $mod.e;',
  27537. '$mod.e = $mod.v;',
  27538. '$mod.e = $mod.v;',
  27539. '']));
  27540. end;
  27541. procedure TTestModule.TestJSValue_ClassInstance;
  27542. begin
  27543. StartProgram(false);
  27544. Add([
  27545. 'type',
  27546. ' TObject = class',
  27547. ' end;',
  27548. ' TBirdObject = TObject;',
  27549. 'var',
  27550. ' v: jsvalue;',
  27551. ' o: TObject;',
  27552. 'begin',
  27553. ' v:=o;',
  27554. ' v:=TObject(o);',
  27555. ' v:=TBirdObject(o);',
  27556. ' o:=TObject(v);',
  27557. ' o:=TBirdObject(v);',
  27558. ' if v is TObject then ;',
  27559. '']);
  27560. ConvertProgram;
  27561. CheckSource('TestJSValue_ClassInstance',
  27562. LinesToStr([ // statements
  27563. 'rtl.createClass(this, "TObject", null, function () {',
  27564. ' this.$init = function () {',
  27565. ' };',
  27566. ' this.$final = function () {',
  27567. ' };',
  27568. '});',
  27569. 'this.v = undefined;',
  27570. 'this.o = null;',
  27571. '']),
  27572. LinesToStr([ // $mod.$main
  27573. '$mod.v = $mod.o;',
  27574. '$mod.v = $mod.o;',
  27575. '$mod.v = $mod.o;',
  27576. '$mod.o = rtl.getObject($mod.v);',
  27577. '$mod.o = rtl.getObject($mod.v);',
  27578. 'if (rtl.isExt($mod.v, $mod.TObject, 1)) ;',
  27579. '']));
  27580. end;
  27581. procedure TTestModule.TestJSValue_ClassOf;
  27582. begin
  27583. StartProgram(false);
  27584. Add([
  27585. 'type',
  27586. ' TClass = class of TObject;',
  27587. ' TObject = class',
  27588. ' end;',
  27589. ' TBirds = class of TBird;',
  27590. ' TBird = class(TObject) end;',
  27591. 'var',
  27592. ' v: jsvalue;',
  27593. ' c: TClass;',
  27594. 'begin',
  27595. ' v:=c;',
  27596. ' v:=TObject;',
  27597. ' v:=TClass(c);',
  27598. ' v:=TBirds(c);',
  27599. ' c:=TClass(v);',
  27600. ' c:=TBirds(v);',
  27601. ' if v is TClass then ;',
  27602. '']);
  27603. ConvertProgram;
  27604. CheckSource('TestJSValue_ClassOf',
  27605. LinesToStr([ // statements
  27606. 'rtl.createClass(this, "TObject", null, function () {',
  27607. ' this.$init = function () {',
  27608. ' };',
  27609. ' this.$final = function () {',
  27610. ' };',
  27611. '});',
  27612. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  27613. '});',
  27614. 'this.v = undefined;',
  27615. 'this.c = null;',
  27616. '']),
  27617. LinesToStr([ // $mod.$main
  27618. '$mod.v = $mod.c;',
  27619. '$mod.v = $mod.TObject;',
  27620. '$mod.v = $mod.c;',
  27621. '$mod.v = $mod.c;',
  27622. '$mod.c = rtl.getObject($mod.v);',
  27623. '$mod.c = rtl.getObject($mod.v);',
  27624. 'if (rtl.isExt($mod.v, $mod.TObject, 2)) ;',
  27625. '']));
  27626. end;
  27627. procedure TTestModule.TestJSValue_ArrayOfJSValue;
  27628. begin
  27629. StartProgram(false);
  27630. Add([
  27631. 'type',
  27632. ' integer = longint;',
  27633. ' TArray = array of JSValue;',
  27634. ' TArrgh = tarray;',
  27635. ' TArrInt = array of integer;',
  27636. 'var',
  27637. ' v: jsvalue;',
  27638. ' TheArray: tarray = (1,''2'');',
  27639. ' Arr: tarrgh;',
  27640. ' i: integer;',
  27641. ' ArrInt: tarrint;',
  27642. 'begin',
  27643. ' arr:=thearray;',
  27644. ' thearray:=arr;',
  27645. ' setlength(arr,2);',
  27646. ' setlength(thearray,3);',
  27647. ' arr[4]:=v;',
  27648. ' arr[5]:=length(thearray);',
  27649. ' arr[6]:=nil;',
  27650. ' arr[7]:=thearray[8];',
  27651. ' arr[low(arr)]:=high(thearray);',
  27652. ' arr:=arrint;',
  27653. ' arrInt:=tarrint(arr);',
  27654. ' if TheArray = nil then ;',
  27655. ' if nil = TheArray then ;',
  27656. ' if TheArray <> nil then ;',
  27657. ' if nil <> TheArray then ;',
  27658. '']);
  27659. ConvertProgram;
  27660. CheckSource('TestJSValue_ArrayOfJSValue',
  27661. LinesToStr([ // statements
  27662. 'this.v = undefined;',
  27663. 'this.TheArray = [1, "2"];',
  27664. 'this.Arr = [];',
  27665. 'this.i = 0;',
  27666. 'this.ArrInt = [];',
  27667. '']),
  27668. LinesToStr([ // $mod.$main
  27669. '$mod.Arr = rtl.arrayRef($mod.TheArray);',
  27670. '$mod.TheArray = rtl.arrayRef($mod.Arr);',
  27671. '$mod.Arr = rtl.arraySetLength($mod.Arr,undefined,2);',
  27672. '$mod.TheArray = rtl.arraySetLength($mod.TheArray,undefined,3);',
  27673. '$mod.Arr[4] = $mod.v;',
  27674. '$mod.Arr[5] = rtl.length($mod.TheArray);',
  27675. '$mod.Arr[6] = null;',
  27676. '$mod.Arr[7] = $mod.TheArray[8];',
  27677. '$mod.Arr[0] = rtl.length($mod.TheArray) - 1;',
  27678. '$mod.Arr = rtl.arrayRef($mod.ArrInt);',
  27679. '$mod.ArrInt = $mod.Arr;',
  27680. 'if (rtl.length($mod.TheArray) === 0) ;',
  27681. 'if (rtl.length($mod.TheArray) === 0) ;',
  27682. 'if (rtl.length($mod.TheArray) > 0) ;',
  27683. 'if (rtl.length($mod.TheArray) > 0) ;',
  27684. '']));
  27685. end;
  27686. procedure TTestModule.TestJSValue_ArrayLit;
  27687. begin
  27688. StartProgram(false);
  27689. Add([
  27690. 'type',
  27691. ' TFlag = (big,small);',
  27692. ' TArray = array of JSValue;',
  27693. ' TObject = class end;',
  27694. ' TClass = class of TObject;',
  27695. 'var',
  27696. ' v: jsvalue;',
  27697. ' a: TArray;',
  27698. ' o: TObject;',
  27699. 'begin',
  27700. ' a:=[];',
  27701. ' a:=[1];',
  27702. ' a:=[1,2];',
  27703. ' a:=[big];',
  27704. ' a:=[1,big];',
  27705. ' a:=[o,nil];',
  27706. '']);
  27707. ConvertProgram;
  27708. CheckSource('TestJSValue_ArrayLit',
  27709. LinesToStr([ // statements
  27710. 'this.TFlag = {',
  27711. ' "0": "big",',
  27712. ' big: 0,',
  27713. ' "1": "small",',
  27714. ' small: 1',
  27715. '};',
  27716. 'rtl.createClass(this, "TObject", null, function () {',
  27717. ' this.$init = function () {',
  27718. ' };',
  27719. ' this.$final = function () {',
  27720. ' };',
  27721. '});',
  27722. 'this.v = undefined;',
  27723. 'this.a = [];',
  27724. 'this.o = null;',
  27725. '']),
  27726. LinesToStr([ // $mod.$main
  27727. '$mod.a = [];',
  27728. '$mod.a = [1];',
  27729. '$mod.a = [1, 2];',
  27730. '$mod.a = [$mod.TFlag.big];',
  27731. '$mod.a = [1, $mod.TFlag.big];',
  27732. '$mod.a = [$mod.o, null];',
  27733. '']));
  27734. end;
  27735. procedure TTestModule.TestJSValue_Params;
  27736. begin
  27737. StartProgram(false);
  27738. Add('type');
  27739. Add(' integer = longint;');
  27740. Add(' TYesNo = boolean;');
  27741. Add(' TFloat = double;');
  27742. Add(' TCaption = string;');
  27743. Add(' TChar = char;');
  27744. Add('function DoIt(a: jsvalue; const b: jsvalue; var c: jsvalue; out d: jsvalue): jsvalue;');
  27745. Add('var');
  27746. Add(' l: jsvalue;');
  27747. Add('begin');
  27748. Add(' a:=a;');
  27749. Add(' l:=b;');
  27750. Add(' c:=c;');
  27751. Add(' d:=d;');
  27752. Add(' Result:=l;');
  27753. Add('end;');
  27754. Add('function DoSome(a: jsvalue; const b: jsvalue): jsvalue; begin end;');
  27755. Add('var');
  27756. Add(' v: jsvalue;');
  27757. Add(' i: integer;');
  27758. Add(' b: TYesNo;');
  27759. Add(' d: TFloat;');
  27760. Add(' s: TCaption;');
  27761. Add(' c: TChar;');
  27762. Add('begin');
  27763. Add(' v:=doit(v,v,v,v);');
  27764. Add(' i:=integer(dosome(i,i));');
  27765. Add(' b:=TYesNo(dosome(b,b));');
  27766. Add(' d:=TFloat(dosome(d,d));');
  27767. Add(' s:=TCaption(dosome(s,s));');
  27768. Add(' c:=TChar(dosome(c,c));');
  27769. ConvertProgram;
  27770. CheckSource('TestJSValue_Params',
  27771. LinesToStr([ // statements
  27772. 'this.DoIt = function (a, b, c, d) {',
  27773. ' var Result = undefined;',
  27774. ' var l = undefined;',
  27775. ' a = a;',
  27776. ' l = b;',
  27777. ' c.set(c.get());',
  27778. ' d.set(d.get());',
  27779. ' Result = l;',
  27780. ' return Result;',
  27781. '};',
  27782. 'this.DoSome = function (a, b) {',
  27783. ' var Result = undefined;',
  27784. ' return Result;',
  27785. '};',
  27786. 'this.v = undefined;',
  27787. 'this.i = 0;',
  27788. 'this.b = false;',
  27789. 'this.d = 0.0;',
  27790. 'this.s = "";',
  27791. 'this.c = "";',
  27792. '']),
  27793. LinesToStr([ // $mod.$main
  27794. '$mod.v = $mod.DoIt($mod.v, $mod.v, {',
  27795. ' p: $mod,',
  27796. ' get: function () {',
  27797. ' return this.p.v;',
  27798. ' },',
  27799. ' set: function (v) {',
  27800. ' this.p.v = v;',
  27801. ' }',
  27802. '}, {',
  27803. ' p: $mod,',
  27804. ' get: function () {',
  27805. ' return this.p.v;',
  27806. ' },',
  27807. ' set: function (v) {',
  27808. ' this.p.v = v;',
  27809. ' }',
  27810. '});',
  27811. '$mod.i = rtl.trunc($mod.DoSome($mod.i, $mod.i));',
  27812. '$mod.b = !($mod.DoSome($mod.b, $mod.b) == false);',
  27813. '$mod.d = rtl.getNumber($mod.DoSome($mod.d, $mod.d));',
  27814. '$mod.s = "" + $mod.DoSome($mod.s, $mod.s);',
  27815. '$mod.c = rtl.getChar($mod.DoSome($mod.c, $mod.c));',
  27816. '']));
  27817. end;
  27818. procedure TTestModule.TestJSValue_UntypedParam;
  27819. begin
  27820. StartProgram(false);
  27821. Add('function DoIt(const a; var b; out c): jsvalue;');
  27822. Add('begin');
  27823. Add(' Result:=a;');
  27824. Add(' Result:=b;');
  27825. Add(' Result:=c;');
  27826. Add(' b:=Result;');
  27827. Add(' c:=Result;');
  27828. Add('end;');
  27829. Add('var i: longint;');
  27830. Add('begin');
  27831. Add(' doit(i,i,i);');
  27832. ConvertProgram;
  27833. CheckSource('TestJSValue_UntypedParam',
  27834. LinesToStr([ // statements
  27835. 'this.DoIt = function (a, b, c) {',
  27836. ' var Result = undefined;',
  27837. ' Result = a;',
  27838. ' Result = b.get();',
  27839. ' Result = c.get();',
  27840. ' b.set(Result);',
  27841. ' c.set(Result);',
  27842. ' return Result;',
  27843. '};',
  27844. 'this.i = 0;',
  27845. '']),
  27846. LinesToStr([ // $mod.$main
  27847. '$mod.DoIt($mod.i, {',
  27848. ' p: $mod,',
  27849. ' get: function () {',
  27850. ' return this.p.i;',
  27851. ' },',
  27852. ' set: function (v) {',
  27853. ' this.p.i = v;',
  27854. ' }',
  27855. '}, {',
  27856. ' p: $mod,',
  27857. ' get: function () {',
  27858. ' return this.p.i;',
  27859. ' },',
  27860. ' set: function (v) {',
  27861. ' this.p.i = v;',
  27862. ' }',
  27863. '});',
  27864. '']));
  27865. end;
  27866. procedure TTestModule.TestJSValue_FuncResultType;
  27867. begin
  27868. StartProgram(false);
  27869. Add('type');
  27870. Add(' integer = longint;');
  27871. Add(' TJSValueArray = array of JSValue;');
  27872. Add(' TListSortCompare = function(Item1, Item2: JSValue): Integer;');
  27873. Add('procedure Sort(P: JSValue; aList: TJSValueArray; const Compare: TListSortCompare);');
  27874. Add('begin');
  27875. Add(' while Compare(P,aList[0])>0 do ;');
  27876. Add('end;');
  27877. Add('var');
  27878. Add(' Compare: TListSortCompare;');
  27879. Add(' V: JSValue;');
  27880. Add(' i: integer;');
  27881. Add('begin');
  27882. Add(' if Compare(V,V)>0 then ;');
  27883. Add(' if Compare(i,i)>1 then ;');
  27884. Add(' if Compare(nil,false)>2 then ;');
  27885. Add(' if Compare(1,true)>3 then ;');
  27886. ConvertProgram;
  27887. CheckSource('TestJSValue_UntypedParam',
  27888. LinesToStr([ // statements
  27889. 'this.Sort = function (P, aList, Compare) {',
  27890. ' while (Compare(P, aList[0]) > 0) {',
  27891. ' };',
  27892. '};',
  27893. 'this.Compare = null;',
  27894. 'this.V = undefined;',
  27895. 'this.i = 0;',
  27896. '']),
  27897. LinesToStr([ // $mod.$main
  27898. 'if ($mod.Compare($mod.V, $mod.V) > 0) ;',
  27899. 'if ($mod.Compare($mod.i, $mod.i) > 1) ;',
  27900. 'if ($mod.Compare(null, false) > 2) ;',
  27901. 'if ($mod.Compare(1, true) > 3) ;',
  27902. '']));
  27903. end;
  27904. procedure TTestModule.TestJSValue_ProcType_Assign;
  27905. begin
  27906. StartProgram(false);
  27907. Add('type');
  27908. Add(' integer = longint;');
  27909. Add(' TObject = class');
  27910. Add(' class function GetGlob: integer;');
  27911. Add(' function Getter: integer;');
  27912. Add(' end;');
  27913. Add('class function TObject.GetGlob: integer;');
  27914. Add('var v1: jsvalue;');
  27915. Add('begin');
  27916. Add(' v1:=@GetGlob;');
  27917. Add(' v1:[email protected];');
  27918. Add('end;');
  27919. Add('function TObject.Getter: integer;');
  27920. Add('var v2: jsvalue;');
  27921. Add('begin');
  27922. Add(' v2:=@Getter;');
  27923. Add(' v2:[email protected];');
  27924. Add(' v2:=@GetGlob;');
  27925. Add(' v2:[email protected];');
  27926. Add('end;');
  27927. Add('function GetIt(i: integer): integer;');
  27928. Add('var v3: jsvalue;');
  27929. Add('begin');
  27930. Add(' v3:=@GetIt;');
  27931. Add('end;');
  27932. Add('var');
  27933. Add(' V: JSValue;');
  27934. Add(' o: TObject;');
  27935. Add('begin');
  27936. Add(' v:=@GetIt;');
  27937. Add(' v:[email protected];');
  27938. Add(' v:[email protected];');
  27939. ConvertProgram;
  27940. CheckSource('TestJSValue_ProcType_Assign',
  27941. LinesToStr([ // statements
  27942. 'rtl.createClass(this, "TObject", null, function () {',
  27943. ' this.$init = function () {',
  27944. ' };',
  27945. ' this.$final = function () {',
  27946. ' };',
  27947. ' this.GetGlob = function () {',
  27948. ' var Result = 0;',
  27949. ' var v1 = undefined;',
  27950. ' v1 = rtl.createCallback(this, "GetGlob");',
  27951. ' v1 = rtl.createCallback(this, "GetGlob");',
  27952. ' return Result;',
  27953. ' };',
  27954. ' this.Getter = function () {',
  27955. ' var Result = 0;',
  27956. ' var v2 = undefined;',
  27957. ' v2 = rtl.createCallback(this, "Getter");',
  27958. ' v2 = rtl.createCallback(this, "Getter");',
  27959. ' v2 = rtl.createCallback(this.$class, "GetGlob");',
  27960. ' v2 = rtl.createCallback(this.$class, "GetGlob");',
  27961. ' return Result;',
  27962. ' };',
  27963. '});',
  27964. 'this.GetIt = function (i) {',
  27965. ' var Result = 0;',
  27966. ' var v3 = undefined;',
  27967. ' v3 = $mod.GetIt;',
  27968. ' return Result;',
  27969. '};',
  27970. 'this.V = undefined;',
  27971. 'this.o = null;',
  27972. '']),
  27973. LinesToStr([ // $mod.$main
  27974. '$mod.V = $mod.GetIt;',
  27975. '$mod.V = rtl.createCallback($mod.o, "Getter");',
  27976. '$mod.V = rtl.createCallback($mod.o.$class, "GetGlob");',
  27977. '']));
  27978. end;
  27979. procedure TTestModule.TestJSValue_ProcType_Equal;
  27980. begin
  27981. StartProgram(false);
  27982. Add('type');
  27983. Add(' integer = longint;');
  27984. Add(' TObject = class');
  27985. Add(' class function GetGlob: integer;');
  27986. Add(' function Getter: integer;');
  27987. Add(' end;');
  27988. Add('class function TObject.GetGlob: integer;');
  27989. Add('var v1: jsvalue;');
  27990. Add('begin');
  27991. Add(' if v1=@GetGlob then;');
  27992. Add(' if [email protected] then ;');
  27993. Add('end;');
  27994. Add('function TObject.Getter: integer;');
  27995. Add('var v2: jsvalue;');
  27996. Add('begin');
  27997. Add(' if v2=@Getter then;');
  27998. Add(' if [email protected] then ;');
  27999. Add(' if v2=@GetGlob then;');
  28000. Add(' if [email protected] then;');
  28001. Add('end;');
  28002. Add('function GetIt(i: integer): integer;');
  28003. Add('var v3: jsvalue;');
  28004. Add('begin');
  28005. Add(' if v3=@GetIt then;');
  28006. Add('end;');
  28007. Add('var');
  28008. Add(' V: JSValue;');
  28009. Add(' o: TObject;');
  28010. Add('begin');
  28011. Add(' if v=@GetIt then;');
  28012. Add(' if [email protected] then;');
  28013. Add(' if [email protected] then;');
  28014. Add(' if @GetIt=v then;');
  28015. Add(' if @o.Getter=v then;');
  28016. Add(' if @o.GetGlob=v then;');
  28017. ConvertProgram;
  28018. CheckSource('TestJSValue_ProcType_Equal',
  28019. LinesToStr([ // statements
  28020. 'rtl.createClass(this, "TObject", null, function () {',
  28021. ' this.$init = function () {',
  28022. ' };',
  28023. ' this.$final = function () {',
  28024. ' };',
  28025. ' this.GetGlob = function () {',
  28026. ' var Result = 0;',
  28027. ' var v1 = undefined;',
  28028. ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
  28029. ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
  28030. ' return Result;',
  28031. ' };',
  28032. ' this.Getter = function () {',
  28033. ' var Result = 0;',
  28034. ' var v2 = undefined;',
  28035. ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
  28036. ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
  28037. ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
  28038. ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
  28039. ' return Result;',
  28040. ' };',
  28041. '});',
  28042. 'this.GetIt = function (i) {',
  28043. ' var Result = 0;',
  28044. ' var v3 = undefined;',
  28045. ' if (rtl.eqCallback(v3, $mod.GetIt)) ;',
  28046. ' return Result;',
  28047. '};',
  28048. 'this.V = undefined;',
  28049. 'this.o = null;',
  28050. '']),
  28051. LinesToStr([ // $mod.$main
  28052. 'if (rtl.eqCallback($mod.V, $mod.GetIt)) ;',
  28053. 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o, "Getter"))) ;',
  28054. 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o.$class, "GetGlob"))) ;',
  28055. 'if (rtl.eqCallback($mod.GetIt, $mod.V)) ;',
  28056. 'if (rtl.eqCallback(rtl.createCallback($mod.o, "Getter"), $mod.V)) ;',
  28057. 'if (rtl.eqCallback(rtl.createCallback($mod.o.$class, "GetGlob"), $mod.V)) ;',
  28058. '']));
  28059. end;
  28060. procedure TTestModule.TestJSValue_ProcType_Param;
  28061. begin
  28062. StartProgram(false);
  28063. Add([
  28064. 'type',
  28065. ' variant = jsvalue;',
  28066. ' TArrVariant = array of variant;',
  28067. ' TArrVar2 = TArrVariant;',
  28068. ' TFuncInt = function: longint;',
  28069. 'function GetIt: longint;',
  28070. 'begin',
  28071. 'end;',
  28072. 'procedure DoIt(p: jsvalue; Arr: TArrVar2);',
  28073. 'var v: variant;',
  28074. 'begin',
  28075. ' v:=arr[1];',
  28076. 'end;',
  28077. 'var s: string;',
  28078. 'begin',
  28079. ' DoIt(GetIt,[]);',
  28080. ' DoIt(@GetIt,[]);',
  28081. ' DoIt(1,[s,GetIt]);',
  28082. ' DoIt(1,[s,@GetIt]);',
  28083. '']);
  28084. ConvertProgram;
  28085. CheckSource('TestJSValue_ProcType_Param',
  28086. LinesToStr([ // statements
  28087. 'this.GetIt = function () {',
  28088. ' var Result = 0;',
  28089. ' return Result;',
  28090. '};',
  28091. 'this.DoIt = function (p, Arr) {',
  28092. ' var v = undefined;',
  28093. ' v = Arr[1];',
  28094. '};',
  28095. 'this.s = "";',
  28096. '']),
  28097. LinesToStr([ // $mod.$main
  28098. '$mod.DoIt($mod.GetIt(), []);',
  28099. '$mod.DoIt($mod.GetIt, []);',
  28100. '$mod.DoIt(1, [$mod.s, $mod.GetIt()]);',
  28101. '$mod.DoIt(1, [$mod.s, $mod.GetIt]);',
  28102. '']));
  28103. end;
  28104. procedure TTestModule.TestJSValue_AssignToPointerFail;
  28105. begin
  28106. StartProgram(false);
  28107. Add([
  28108. 'var',
  28109. ' v: JSValue;',
  28110. ' p: Pointer;',
  28111. 'begin',
  28112. ' p:=v;',
  28113. '']);
  28114. SetExpectedPasResolverError('Incompatible types: got "JSValue" expected "Pointer"',
  28115. nIncompatibleTypesGotExpected);
  28116. ConvertProgram;
  28117. end;
  28118. procedure TTestModule.TestJSValue_OverloadDouble;
  28119. begin
  28120. StartProgram(false);
  28121. Add([
  28122. 'type',
  28123. ' integer = longint;',
  28124. ' tdatetime = double;',
  28125. 'procedure DoIt(d: double); begin end;',
  28126. 'procedure DoIt(v: jsvalue); begin end;',
  28127. 'var',
  28128. ' d: double;',
  28129. ' dt: tdatetime;',
  28130. ' i: integer;',
  28131. ' b: byte;',
  28132. ' shi: shortint;',
  28133. ' w: word;',
  28134. ' smi: smallint;',
  28135. ' lw: longword;',
  28136. ' li: longint;',
  28137. ' ni: nativeint;',
  28138. ' nu: nativeuint;',
  28139. 'begin',
  28140. ' DoIt(d);',
  28141. ' DoIt(dt);',
  28142. ' DoIt(i);',
  28143. ' DoIt(b);',
  28144. ' DoIt(shi);',
  28145. ' DoIt(w);',
  28146. ' DoIt(smi);',
  28147. ' DoIt(lw);',
  28148. ' DoIt(li);',
  28149. ' DoIt(ni);',
  28150. ' DoIt(nu);',
  28151. '']);
  28152. ConvertProgram;
  28153. CheckSource('TestJSValue_OverloadDouble',
  28154. LinesToStr([ // statements
  28155. 'this.DoIt = function (d) {',
  28156. '};',
  28157. 'this.DoIt$1 = function (v) {',
  28158. '};',
  28159. 'this.d = 0.0;',
  28160. 'this.dt = 0.0;',
  28161. 'this.i = 0;',
  28162. 'this.b = 0;',
  28163. 'this.shi = 0;',
  28164. 'this.w = 0;',
  28165. 'this.smi = 0;',
  28166. 'this.lw = 0;',
  28167. 'this.li = 0;',
  28168. 'this.ni = 0;',
  28169. 'this.nu = 0;',
  28170. '']),
  28171. LinesToStr([ // $mod.$main
  28172. '$mod.DoIt($mod.d);',
  28173. '$mod.DoIt($mod.dt);',
  28174. '$mod.DoIt$1($mod.i);',
  28175. '$mod.DoIt$1($mod.b);',
  28176. '$mod.DoIt$1($mod.shi);',
  28177. '$mod.DoIt$1($mod.w);',
  28178. '$mod.DoIt$1($mod.smi);',
  28179. '$mod.DoIt$1($mod.lw);',
  28180. '$mod.DoIt$1($mod.li);',
  28181. '$mod.DoIt$1($mod.ni);',
  28182. '$mod.DoIt$1($mod.nu);',
  28183. '']));
  28184. end;
  28185. procedure TTestModule.TestJSValue_OverloadNativeInt;
  28186. begin
  28187. StartProgram(false);
  28188. Add([
  28189. 'type',
  28190. ' integer = longint;',
  28191. ' int53 = nativeint;',
  28192. ' tdatetime = double;',
  28193. 'procedure DoIt(n: nativeint); begin end;',
  28194. 'procedure DoIt(v: jsvalue); begin end;',
  28195. 'var',
  28196. ' d: double;',
  28197. ' dt: tdatetime;',
  28198. ' i: integer;',
  28199. ' b: byte;',
  28200. ' shi: shortint;',
  28201. ' w: word;',
  28202. ' smi: smallint;',
  28203. ' lw: longword;',
  28204. ' li: longint;',
  28205. ' ni: nativeint;',
  28206. ' nu: nativeuint;',
  28207. 'begin',
  28208. ' DoIt(d);',
  28209. ' DoIt(dt);',
  28210. ' DoIt(i);',
  28211. ' DoIt(b);',
  28212. ' DoIt(shi);',
  28213. ' DoIt(w);',
  28214. ' DoIt(smi);',
  28215. ' DoIt(lw);',
  28216. ' DoIt(li);',
  28217. ' DoIt(ni);',
  28218. ' DoIt(nu);',
  28219. '']);
  28220. ConvertProgram;
  28221. CheckSource('TestJSValue_OverloadNativeInt',
  28222. LinesToStr([ // statements
  28223. 'this.DoIt = function (n) {',
  28224. '};',
  28225. 'this.DoIt$1 = function (v) {',
  28226. '};',
  28227. 'this.d = 0.0;',
  28228. 'this.dt = 0.0;',
  28229. 'this.i = 0;',
  28230. 'this.b = 0;',
  28231. 'this.shi = 0;',
  28232. 'this.w = 0;',
  28233. 'this.smi = 0;',
  28234. 'this.lw = 0;',
  28235. 'this.li = 0;',
  28236. 'this.ni = 0;',
  28237. 'this.nu = 0;',
  28238. '']),
  28239. LinesToStr([ // $mod.$main
  28240. '$mod.DoIt$1($mod.d);',
  28241. '$mod.DoIt$1($mod.dt);',
  28242. '$mod.DoIt($mod.i);',
  28243. '$mod.DoIt($mod.b);',
  28244. '$mod.DoIt($mod.shi);',
  28245. '$mod.DoIt($mod.w);',
  28246. '$mod.DoIt($mod.smi);',
  28247. '$mod.DoIt($mod.lw);',
  28248. '$mod.DoIt($mod.li);',
  28249. '$mod.DoIt($mod.ni);',
  28250. '$mod.DoIt($mod.nu);',
  28251. '']));
  28252. end;
  28253. procedure TTestModule.TestJSValue_OverloadWord;
  28254. begin
  28255. StartProgram(false);
  28256. Add([
  28257. 'type',
  28258. ' integer = longint;',
  28259. ' int53 = nativeint;',
  28260. ' tdatetime = double;',
  28261. 'procedure DoIt(w: word); begin end;',
  28262. 'procedure DoIt(v: jsvalue); begin end;',
  28263. 'var',
  28264. ' d: double;',
  28265. ' dt: tdatetime;',
  28266. ' i: integer;',
  28267. ' b: byte;',
  28268. ' shi: shortint;',
  28269. ' w: word;',
  28270. ' smi: smallint;',
  28271. ' lw: longword;',
  28272. ' li: longint;',
  28273. ' ni: nativeint;',
  28274. ' nu: nativeuint;',
  28275. 'begin',
  28276. ' DoIt(d);',
  28277. ' DoIt(dt);',
  28278. ' DoIt(i);',
  28279. ' DoIt(b);',
  28280. ' DoIt(shi);',
  28281. ' DoIt(w);',
  28282. ' DoIt(smi);',
  28283. ' DoIt(lw);',
  28284. ' DoIt(li);',
  28285. ' DoIt(ni);',
  28286. ' DoIt(nu);',
  28287. '']);
  28288. ConvertProgram;
  28289. CheckSource('TestJSValue_OverloadWord',
  28290. LinesToStr([ // statements
  28291. 'this.DoIt = function (w) {',
  28292. '};',
  28293. 'this.DoIt$1 = function (v) {',
  28294. '};',
  28295. 'this.d = 0.0;',
  28296. 'this.dt = 0.0;',
  28297. 'this.i = 0;',
  28298. 'this.b = 0;',
  28299. 'this.shi = 0;',
  28300. 'this.w = 0;',
  28301. 'this.smi = 0;',
  28302. 'this.lw = 0;',
  28303. 'this.li = 0;',
  28304. 'this.ni = 0;',
  28305. 'this.nu = 0;',
  28306. '']),
  28307. LinesToStr([ // $mod.$main
  28308. '$mod.DoIt$1($mod.d);',
  28309. '$mod.DoIt$1($mod.dt);',
  28310. '$mod.DoIt$1($mod.i);',
  28311. '$mod.DoIt($mod.b);',
  28312. '$mod.DoIt($mod.shi);',
  28313. '$mod.DoIt($mod.w);',
  28314. '$mod.DoIt$1($mod.smi);',
  28315. '$mod.DoIt$1($mod.lw);',
  28316. '$mod.DoIt$1($mod.li);',
  28317. '$mod.DoIt$1($mod.ni);',
  28318. '$mod.DoIt$1($mod.nu);',
  28319. '']));
  28320. end;
  28321. procedure TTestModule.TestJSValue_OverloadString;
  28322. begin
  28323. StartProgram(false);
  28324. Add([
  28325. 'type',
  28326. ' uni = string;',
  28327. ' WChar = char;',
  28328. 'procedure DoIt(s: string); begin end;',
  28329. 'procedure DoIt(v: jsvalue); begin end;',
  28330. 'var',
  28331. ' s: string;',
  28332. ' c: char;',
  28333. ' u: uni;',
  28334. 'begin',
  28335. ' DoIt(s);',
  28336. ' DoIt(c);',
  28337. ' DoIt(u);',
  28338. '']);
  28339. ConvertProgram;
  28340. CheckSource('TestJSValue_OverloadString',
  28341. LinesToStr([ // statements
  28342. 'this.DoIt = function (s) {',
  28343. '};',
  28344. 'this.DoIt$1 = function (v) {',
  28345. '};',
  28346. 'this.s = "";',
  28347. 'this.c = "";',
  28348. 'this.u = "";',
  28349. '']),
  28350. LinesToStr([ // $mod.$main
  28351. '$mod.DoIt($mod.s);',
  28352. '$mod.DoIt($mod.c);',
  28353. '$mod.DoIt($mod.u);',
  28354. '']));
  28355. end;
  28356. procedure TTestModule.TestJSValue_OverloadChar;
  28357. begin
  28358. StartProgram(false);
  28359. Add([
  28360. 'type',
  28361. ' uni = string;',
  28362. ' WChar = char;',
  28363. 'procedure DoIt(c: char); begin end;',
  28364. 'procedure DoIt(v: jsvalue); begin end;',
  28365. 'var',
  28366. ' s: string;',
  28367. ' c: char;',
  28368. ' u: uni;',
  28369. 'begin',
  28370. ' DoIt(s);',
  28371. ' DoIt(c);',
  28372. ' DoIt(u);',
  28373. '']);
  28374. ConvertProgram;
  28375. CheckSource('TestJSValue_OverloadChar',
  28376. LinesToStr([ // statements
  28377. 'this.DoIt = function (c) {',
  28378. '};',
  28379. 'this.DoIt$1 = function (v) {',
  28380. '};',
  28381. 'this.s = "";',
  28382. 'this.c = "";',
  28383. 'this.u = "";',
  28384. '']),
  28385. LinesToStr([ // $mod.$main
  28386. '$mod.DoIt$1($mod.s);',
  28387. '$mod.DoIt($mod.c);',
  28388. '$mod.DoIt$1($mod.u);',
  28389. '']));
  28390. end;
  28391. procedure TTestModule.TestJSValue_OverloadPointer;
  28392. begin
  28393. StartProgram(false);
  28394. Add([
  28395. 'type',
  28396. ' TObject = class end;',
  28397. 'procedure DoIt(p: pointer); begin end;',
  28398. 'procedure DoIt(v: jsvalue); begin end;',
  28399. 'var',
  28400. ' o: TObject;',
  28401. 'begin',
  28402. ' DoIt(o);',
  28403. '']);
  28404. ConvertProgram;
  28405. CheckSource('TestJSValue_OverloadPointer',
  28406. LinesToStr([ // statements
  28407. 'rtl.createClass(this, "TObject", null, function () {',
  28408. ' this.$init = function () {',
  28409. ' };',
  28410. ' this.$final = function () {',
  28411. ' };',
  28412. '});',
  28413. 'this.DoIt = function (p) {',
  28414. '};',
  28415. 'this.DoIt$1 = function (v) {',
  28416. '};',
  28417. 'this.o = null;',
  28418. '']),
  28419. LinesToStr([ // $mod.$main
  28420. '$mod.DoIt($mod.o);',
  28421. '']));
  28422. end;
  28423. procedure TTestModule.TestJSValue_ForIn;
  28424. begin
  28425. StartProgram(false);
  28426. Add([
  28427. 'var',
  28428. ' v: JSValue;',
  28429. ' key: string;',
  28430. 'begin',
  28431. ' for key in v do begin',
  28432. ' if key=''abc'' then ;',
  28433. ' end;',
  28434. '']);
  28435. ConvertProgram;
  28436. CheckSource('TestJSValue_ForIn',
  28437. LinesToStr([ // statements
  28438. 'this.v = undefined;',
  28439. 'this.key = "";',
  28440. '']),
  28441. LinesToStr([ // $mod.$main
  28442. 'for ($mod.key in $mod.v) {',
  28443. ' if ($mod.key === "abc") ;',
  28444. '};',
  28445. '']));
  28446. end;
  28447. procedure TTestModule.TestRTTI_IntRange;
  28448. begin
  28449. WithTypeInfo:=true;
  28450. StartProgram(true,[supTypeInfo]);
  28451. Add([
  28452. '{$modeswitch externalclass}',
  28453. 'type',
  28454. ' TGraphicsColor = -$7FFFFFFF-1..$7FFFFFFF;',
  28455. ' TColor = type TGraphicsColor;',
  28456. 'var',
  28457. ' p: TTypeInfo;',
  28458. ' k: TTypeKind;',
  28459. 'begin',
  28460. ' p:=typeinfo(TGraphicsColor);',
  28461. ' p:=typeinfo(TColor);',
  28462. ' k:=GetTypeKind(TGraphicsColor);',
  28463. ' k:=GetTypeKind(TColor);',
  28464. '']);
  28465. ConvertProgram;
  28466. CheckSource('TestRTTI_IntRange',
  28467. LinesToStr([ // statements
  28468. 'this.$rtti.$Int("TGraphicsColor", {',
  28469. ' minvalue: -2147483648,',
  28470. ' maxvalue: 2147483647,',
  28471. ' ordtype: 4',
  28472. '});',
  28473. 'this.$rtti.$inherited("TColor", this.$rtti["TGraphicsColor"], {});',
  28474. 'this.p = null;',
  28475. 'this.k = 0;',
  28476. '']),
  28477. LinesToStr([ // $mod.$main
  28478. '$mod.p = $mod.$rtti["TGraphicsColor"];',
  28479. '$mod.p = $mod.$rtti["TColor"];',
  28480. '$mod.k = 1;',
  28481. '$mod.k = 1;',
  28482. '']));
  28483. end;
  28484. procedure TTestModule.TestRTTI_Double;
  28485. begin
  28486. WithTypeInfo:=true;
  28487. StartProgram(true,[supTypeInfo]);
  28488. Add([
  28489. '{$modeswitch externalclass}',
  28490. 'type',
  28491. ' TFloat = type double;',
  28492. 'var',
  28493. ' p: TTypeInfo;',
  28494. 'begin',
  28495. ' p:=typeinfo(double);',
  28496. ' p:=typeinfo(TFloat);',
  28497. '']);
  28498. ConvertProgram;
  28499. CheckSource('TestRTTI_Double',
  28500. LinesToStr([ // statements
  28501. 'this.$rtti.$inherited("TFloat", rtl.double, {});',
  28502. 'this.p = null;',
  28503. '']),
  28504. LinesToStr([ // $mod.$main
  28505. '$mod.p = rtl.double;',
  28506. '$mod.p = $mod.$rtti["TFloat"];',
  28507. '']));
  28508. end;
  28509. procedure TTestModule.TestRTTI_ProcType;
  28510. begin
  28511. WithTypeInfo:=true;
  28512. StartProgram(false);
  28513. Add('type');
  28514. Add(' TProcA = procedure;');
  28515. Add(' TMethodB = procedure of object;');
  28516. Add(' TProcC = procedure; varargs;');
  28517. Add(' TProcD = procedure(i: longint; const j: string; var c: char; out d: double);');
  28518. Add(' TProcE = function: nativeint;');
  28519. Add(' TProcF = function(const p: TProcA): nativeuint;');
  28520. Add('var p: pointer;');
  28521. Add('begin');
  28522. Add(' p:=typeinfo(tproca);');
  28523. ConvertProgram;
  28524. CheckSource('TestRTTI_ProcType',
  28525. LinesToStr([ // statements
  28526. 'this.$rtti.$ProcVar("TProcA", {',
  28527. ' procsig: rtl.newTIProcSig(null)',
  28528. '});',
  28529. 'this.$rtti.$MethodVar("TMethodB", {',
  28530. ' procsig: rtl.newTIProcSig(null),',
  28531. ' methodkind: 0',
  28532. '});',
  28533. 'this.$rtti.$ProcVar("TProcC", {',
  28534. ' procsig: rtl.newTIProcSig(null, 2)',
  28535. '});',
  28536. 'this.$rtti.$ProcVar("TProcD", {',
  28537. ' procsig: rtl.newTIProcSig([["i", rtl.longint], ["j", rtl.string, 2], ["c", rtl.char, 1], ["d", rtl.double, 4]])',
  28538. '});',
  28539. 'this.$rtti.$ProcVar("TProcE", {',
  28540. ' procsig: rtl.newTIProcSig(null, rtl.nativeint)',
  28541. '});',
  28542. 'this.$rtti.$ProcVar("TProcF", {',
  28543. ' procsig: rtl.newTIProcSig([["p", this.$rtti["TProcA"], 2]], rtl.nativeuint)',
  28544. '});',
  28545. 'this.p = null;',
  28546. '']),
  28547. LinesToStr([ // $mod.$main
  28548. '$mod.p = $mod.$rtti["TProcA"];',
  28549. '']));
  28550. end;
  28551. procedure TTestModule.TestRTTI_ProcType_ArgFromOtherUnit;
  28552. begin
  28553. WithTypeInfo:=true;
  28554. AddModuleWithIntfImplSrc('unit2.pas',
  28555. LinesToStr([
  28556. 'type',
  28557. ' TObject = class end;'
  28558. ]),
  28559. '');
  28560. StartUnit(true);
  28561. Add('interface');
  28562. Add('uses unit2;');
  28563. Add('type');
  28564. Add(' TProcA = function(o: tobject): tobject;');
  28565. Add('implementation');
  28566. Add('type');
  28567. Add(' TProcB = function(o: tobject): tobject;');
  28568. Add('var p: Pointer;');
  28569. Add('initialization');
  28570. Add(' p:=typeinfo(tproca);');
  28571. Add(' p:=typeinfo(tprocb);');
  28572. ConvertUnit;
  28573. CheckSource('TestRTTI_ProcType_ArgFromOtherUnit',
  28574. LinesToStr([ // statements
  28575. 'var $impl = $mod.$impl;',
  28576. 'this.$rtti.$ProcVar("TProcA", {',
  28577. ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
  28578. '});',
  28579. '']),
  28580. LinesToStr([ // this.$init
  28581. '$impl.p = $mod.$rtti["TProcA"];',
  28582. '$impl.p = $mod.$rtti["TProcB"];',
  28583. '']),
  28584. LinesToStr([ // implementation
  28585. '$mod.$rtti.$ProcVar("TProcB", {',
  28586. ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
  28587. '});',
  28588. '$impl.p = null;',
  28589. '']) );
  28590. end;
  28591. procedure TTestModule.TestRTTI_EnumAndSetType;
  28592. begin
  28593. WithTypeInfo:=true;
  28594. StartProgram(false);
  28595. Add('type');
  28596. Add(' TFlag = (light,dark);');
  28597. Add(' TFlags = set of TFlag;');
  28598. Add(' TProc = function(f: TFlags): TFlag;');
  28599. Add('var p: pointer;');
  28600. Add('begin');
  28601. Add(' p:=typeinfo(tflag);');
  28602. Add(' p:=typeinfo(tflags);');
  28603. ConvertProgram;
  28604. CheckSource('TestRTTI_EnumAndType',
  28605. LinesToStr([ // statements
  28606. 'this.TFlag = {',
  28607. ' "0": "light",',
  28608. ' light: 0,',
  28609. ' "1": "dark",',
  28610. ' dark: 1',
  28611. '};',
  28612. 'this.$rtti.$Enum("TFlag", {',
  28613. ' minvalue: 0,',
  28614. ' maxvalue: 1,',
  28615. ' ordtype: 1,',
  28616. ' enumtype: this.TFlag',
  28617. '});',
  28618. 'this.$rtti.$Set("TFlags", {',
  28619. ' comptype: this.$rtti["TFlag"]',
  28620. '});',
  28621. 'this.$rtti.$ProcVar("TProc", {',
  28622. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TFlags"]]], this.$rtti["TFlag"])',
  28623. '});',
  28624. 'this.p = null;',
  28625. '']),
  28626. LinesToStr([ // $mod.$main
  28627. '$mod.p = $mod.$rtti["TFlag"];',
  28628. '$mod.p = $mod.$rtti["TFlags"];',
  28629. '']));
  28630. end;
  28631. procedure TTestModule.TestRTTI_EnumRange;
  28632. begin
  28633. WithTypeInfo:=true;
  28634. StartProgram(false);
  28635. Add([
  28636. 'type',
  28637. ' TCol = (red,green,blue);',
  28638. ' TColRg = green..blue;',
  28639. ' TSetOfColRg = set of TColRg;',
  28640. 'var p: pointer;',
  28641. 'begin',
  28642. ' p:=typeinfo(tcolrg);',
  28643. ' p:=typeinfo(tsetofcolrg);',
  28644. '']);
  28645. ConvertProgram;
  28646. end;
  28647. procedure TTestModule.TestRTTI_AnonymousEnumType;
  28648. begin
  28649. WithTypeInfo:=true;
  28650. StartProgram(false);
  28651. Add('type');
  28652. Add(' TFlags = set of (red, green);');
  28653. Add('var');
  28654. Add(' f: TFlags;');
  28655. Add('begin');
  28656. Add(' Include(f,red);');
  28657. ConvertProgram;
  28658. CheckSource('TestRTTI_AnonymousEnumType',
  28659. LinesToStr([ // statements
  28660. 'this.TFlags$a = {',
  28661. ' "0": "red",',
  28662. ' red: 0,',
  28663. ' "1": "green",',
  28664. ' green: 1',
  28665. '};',
  28666. 'this.$rtti.$Enum("TFlags$a", {',
  28667. ' minvalue: 0,',
  28668. ' maxvalue: 1,',
  28669. ' ordtype: 1,',
  28670. ' enumtype: this.TFlags$a',
  28671. '});',
  28672. 'this.$rtti.$Set("TFlags", {',
  28673. ' comptype: this.$rtti["TFlags$a"]',
  28674. '});',
  28675. 'this.f = {};',
  28676. '']),
  28677. LinesToStr([
  28678. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  28679. '']));
  28680. end;
  28681. procedure TTestModule.TestRTTI_StaticArray;
  28682. begin
  28683. WithTypeInfo:=true;
  28684. StartProgram(false);
  28685. Add('type');
  28686. Add(' TFlag = (light,dark);');
  28687. Add(' TFlagNames = array[TFlag] of string;');
  28688. Add(' TBoolNames = array[boolean] of string;');
  28689. Add(' TByteArray = array[1..32768] of byte;');
  28690. Add(' TProc = function(f: TBoolNames): TFlagNames;');
  28691. Add('var p: pointer;');
  28692. Add('begin');
  28693. Add(' p:=typeinfo(TFlagNames);');
  28694. Add(' p:=typeinfo(TBoolNames);');
  28695. ConvertProgram;
  28696. CheckSource('TestRTTI_StaticArray',
  28697. LinesToStr([ // statements
  28698. 'this.TFlag = {',
  28699. ' "0": "light",',
  28700. ' light: 0,',
  28701. ' "1": "dark",',
  28702. ' dark: 1',
  28703. '};',
  28704. 'this.$rtti.$Enum("TFlag", {',
  28705. ' minvalue: 0,',
  28706. ' maxvalue: 1,',
  28707. ' ordtype: 1,',
  28708. ' enumtype: this.TFlag',
  28709. '});',
  28710. 'this.$rtti.$StaticArray("TFlagNames", {',
  28711. ' dims: [2],',
  28712. ' eltype: rtl.string',
  28713. '});',
  28714. 'this.$rtti.$StaticArray("TBoolNames", {',
  28715. ' dims: [2],',
  28716. ' eltype: rtl.string',
  28717. '});',
  28718. 'this.$rtti.$StaticArray("TByteArray", {',
  28719. ' dims: [32768],',
  28720. ' eltype: rtl.byte',
  28721. '});',
  28722. 'this.$rtti.$ProcVar("TProc", {',
  28723. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TBoolNames"]]], this.$rtti["TFlagNames"])',
  28724. '});',
  28725. 'this.p = null;',
  28726. '']),
  28727. LinesToStr([ // $mod.$main
  28728. '$mod.p = $mod.$rtti["TFlagNames"];',
  28729. '$mod.p = $mod.$rtti["TBoolNames"];',
  28730. '']));
  28731. end;
  28732. procedure TTestModule.TestRTTI_DynArray;
  28733. begin
  28734. WithTypeInfo:=true;
  28735. StartProgram(false);
  28736. Add('type');
  28737. Add(' TArrStr = array of string;');
  28738. Add(' TArr2Dim = array of tarrstr;');
  28739. Add(' TProc = function(f: TArrStr): TArr2Dim;');
  28740. Add('var p: pointer;');
  28741. Add('begin');
  28742. Add(' p:=typeinfo(tarrstr);');
  28743. Add(' p:=typeinfo(tarr2dim);');
  28744. ConvertProgram;
  28745. CheckSource('TestRTTI_DynArray',
  28746. LinesToStr([ // statements
  28747. 'this.$rtti.$DynArray("TArrStr", {',
  28748. ' eltype: rtl.string',
  28749. '});',
  28750. 'this.$rtti.$DynArray("TArr2Dim", {',
  28751. ' eltype: this.$rtti["TArrStr"]',
  28752. '});',
  28753. 'this.$rtti.$ProcVar("TProc", {',
  28754. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TArrStr"]]], this.$rtti["TArr2Dim"])',
  28755. '});',
  28756. 'this.p = null;',
  28757. '']),
  28758. LinesToStr([ // $mod.$main
  28759. '$mod.p = $mod.$rtti["TArrStr"];',
  28760. '$mod.p = $mod.$rtti["TArr2Dim"];',
  28761. '']));
  28762. end;
  28763. procedure TTestModule.TestRTTI_ArrayNestedAnonymous;
  28764. begin
  28765. WithTypeInfo:=true;
  28766. StartProgram(false);
  28767. Add('type');
  28768. Add(' TArr = array of array of longint;');
  28769. Add('var a: TArr;');
  28770. Add('begin');
  28771. ConvertProgram;
  28772. CheckSource('TestRTTI_ArrayNestedAnonymous',
  28773. LinesToStr([ // statements
  28774. 'this.$rtti.$DynArray("TArr$a", {',
  28775. ' eltype: rtl.longint',
  28776. '});',
  28777. 'this.$rtti.$DynArray("TArr", {',
  28778. ' eltype: this.$rtti["TArr$a"]',
  28779. '});',
  28780. 'this.a = [];',
  28781. '']),
  28782. LinesToStr([ // $mod.$main
  28783. ]));
  28784. end;
  28785. procedure TTestModule.TestRTTI_PublishedMethodOverloadFail;
  28786. begin
  28787. WithTypeInfo:=true;
  28788. StartProgram(false);
  28789. Add('type');
  28790. Add(' TObject = class');
  28791. Add(' published');
  28792. Add(' procedure Proc; virtual; abstract;');
  28793. Add(' procedure Proc(Sender: tobject); virtual; abstract;');
  28794. Add(' end;');
  28795. Add('begin');
  28796. SetExpectedPasResolverError('Duplicate published method "Proc" at test1.pp(6,19)',
  28797. nDuplicatePublishedMethodXAtY);
  28798. ConvertProgram;
  28799. end;
  28800. procedure TTestModule.TestRTTI_PublishedMethodHideNoHint;
  28801. begin
  28802. WithTypeInfo:=true;
  28803. StartUnit(false);
  28804. Add([
  28805. 'interface',
  28806. 'type',
  28807. ' TObject = class',
  28808. ' end;',
  28809. ' {$M+}',
  28810. ' TBird = class',
  28811. ' procedure Fly;',
  28812. ' end;',
  28813. ' {$M-}',
  28814. 'type',
  28815. ' TEagle = class(TBird)',
  28816. ' procedure Fly;',
  28817. ' end;',
  28818. 'implementation',
  28819. 'procedure TBird.Fly;',
  28820. 'begin',
  28821. 'end;',
  28822. 'procedure TEagle.Fly;',
  28823. 'begin',
  28824. 'end;',
  28825. '']);
  28826. ConvertUnit;
  28827. CheckSource('TestRTTI_PublishedMethodHideNoHint',
  28828. LinesToStr([ // statements
  28829. 'rtl.createClass(this, "TObject", null, function () {',
  28830. ' this.$init = function () {',
  28831. ' };',
  28832. ' this.$final = function () {',
  28833. ' };',
  28834. '});',
  28835. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  28836. ' this.Fly = function () {',
  28837. ' };',
  28838. ' var $r = this.$rtti;',
  28839. ' $r.addMethod("Fly", 0, null);',
  28840. '});',
  28841. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  28842. ' this.Fly = function () {',
  28843. ' };',
  28844. ' var $r = this.$rtti;',
  28845. ' $r.addMethod("Fly", 0, null);',
  28846. '});',
  28847. '']),
  28848. LinesToStr([ // $mod.$main
  28849. ]));
  28850. CheckResolverUnexpectedHints(true);
  28851. end;
  28852. procedure TTestModule.TestRTTI_PublishedMethodExternalFail;
  28853. begin
  28854. WithTypeInfo:=true;
  28855. StartProgram(false);
  28856. Add('type');
  28857. Add(' TObject = class');
  28858. Add(' published');
  28859. Add(' procedure Proc; external name ''foo'';');
  28860. Add(' end;');
  28861. Add('begin');
  28862. SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
  28863. nPublishedNameMustMatchExternal);
  28864. ConvertProgram;
  28865. end;
  28866. procedure TTestModule.TestRTTI_PublishedClassPropertyFail;
  28867. begin
  28868. WithTypeInfo:=true;
  28869. StartProgram(false);
  28870. Add('type');
  28871. Add(' TObject = class');
  28872. Add(' class var FA: longint;');
  28873. Add(' published');
  28874. Add(' class property A: longint read FA;');
  28875. Add(' end;');
  28876. Add('begin');
  28877. SetExpectedPasResolverError('Invalid published property modifier "class"',
  28878. nInvalidXModifierY);
  28879. ConvertProgram;
  28880. end;
  28881. procedure TTestModule.TestRTTI_PublishedClassFieldFail;
  28882. begin
  28883. WithTypeInfo:=true;
  28884. StartProgram(false);
  28885. Add('type');
  28886. Add(' TObject = class');
  28887. Add(' published');
  28888. Add(' class var FA: longint;');
  28889. Add(' end;');
  28890. Add('begin');
  28891. SetExpectedPasResolverError(sSymbolCannotBePublished,
  28892. nSymbolCannotBePublished);
  28893. ConvertProgram;
  28894. end;
  28895. procedure TTestModule.TestRTTI_PublishedFieldExternalFail;
  28896. begin
  28897. WithTypeInfo:=true;
  28898. StartProgram(false);
  28899. Add('{$modeswitch externalclass}');
  28900. Add('type');
  28901. Add(' TObject = class');
  28902. Add(' published');
  28903. Add(' V: longint; external name ''foo'';');
  28904. Add(' end;');
  28905. Add('begin');
  28906. SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
  28907. nPublishedNameMustMatchExternal);
  28908. ConvertProgram;
  28909. end;
  28910. procedure TTestModule.TestRTTI_Class_Field;
  28911. begin
  28912. WithTypeInfo:=true;
  28913. StartProgram(false);
  28914. Add('{$modeswitch externalclass}');
  28915. Add('type');
  28916. Add(' TObject = class');
  28917. Add(' private');
  28918. Add(' FPropA: string;');
  28919. Add(' published');
  28920. Add(' VarLI: longint;');
  28921. Add(' VarC: char;');
  28922. Add(' VarS: string;');
  28923. Add(' VarD: double;');
  28924. Add(' VarB: boolean;');
  28925. Add(' VarLW: longword;');
  28926. Add(' VarSmI: smallint;');
  28927. Add(' VarW: word;');
  28928. Add(' VarShI: shortint;');
  28929. Add(' VarBy: byte;');
  28930. Add(' VarExt: longint external name ''VarExt'';');
  28931. Add(' ArrA, ArrB: array of byte;');
  28932. Add(' end;');
  28933. Add('var p: pointer;');
  28934. Add(' Obj: tobject;');
  28935. Add('begin');
  28936. Add(' p:=typeinfo(tobject);');
  28937. Add(' p:=typeinfo(p);');
  28938. Add(' p:=typeinfo(obj);');
  28939. ConvertProgram;
  28940. CheckSource('TestRTTI_Class_Field',
  28941. LinesToStr([ // statements
  28942. 'rtl.createClass(this, "TObject", null, function () {',
  28943. ' this.$init = function () {',
  28944. ' this.FPropA = "";',
  28945. ' this.VarLI = 0;',
  28946. ' this.VarC = "";',
  28947. ' this.VarS = "";',
  28948. ' this.VarD = 0.0;',
  28949. ' this.VarB = false;',
  28950. ' this.VarLW = 0;',
  28951. ' this.VarSmI = 0;',
  28952. ' this.VarW = 0;',
  28953. ' this.VarShI = 0;',
  28954. ' this.VarBy = 0;',
  28955. ' this.ArrA = [];',
  28956. ' this.ArrB = [];',
  28957. ' };',
  28958. ' this.$final = function () {',
  28959. ' this.ArrA = undefined;',
  28960. ' this.ArrB = undefined;',
  28961. ' };',
  28962. ' var $r = this.$rtti;',
  28963. ' $r.addField("VarLI", rtl.longint);',
  28964. ' $r.addField("VarC", rtl.char);',
  28965. ' $r.addField("VarS", rtl.string);',
  28966. ' $r.addField("VarD", rtl.double);',
  28967. ' $r.addField("VarB", rtl.boolean);',
  28968. ' $r.addField("VarLW", rtl.longword);',
  28969. ' $r.addField("VarSmI", rtl.smallint);',
  28970. ' $r.addField("VarW", rtl.word);',
  28971. ' $r.addField("VarShI", rtl.shortint);',
  28972. ' $r.addField("VarBy", rtl.byte);',
  28973. ' $r.addField("VarExt", rtl.longint);',
  28974. ' $mod.$rtti.$DynArray("TObject.ArrB$a", {',
  28975. ' eltype: rtl.byte',
  28976. ' });',
  28977. ' $r.addField("ArrA", $mod.$rtti["TObject.ArrB$a"]);',
  28978. ' $r.addField("ArrB", $mod.$rtti["TObject.ArrB$a"]);',
  28979. '});',
  28980. 'this.p = null;',
  28981. 'this.Obj = null;',
  28982. '']),
  28983. LinesToStr([ // $mod.$main
  28984. '$mod.p = $mod.$rtti["TObject"];',
  28985. '$mod.p = rtl.pointer;',
  28986. '$mod.p = $mod.Obj.$rtti;',
  28987. '']));
  28988. end;
  28989. procedure TTestModule.TestRTTI_Class_Method;
  28990. begin
  28991. WithTypeInfo:=true;
  28992. StartProgram(false);
  28993. Add('type');
  28994. Add(' TObject = class');
  28995. Add(' private');
  28996. Add(' procedure Internal; external name ''$intern'';');
  28997. Add(' published');
  28998. Add(' procedure Click; virtual; abstract;');
  28999. Add(' procedure Notify(Sender: TObject); virtual; abstract;');
  29000. Add(' function GetNotify: boolean; external name ''GetNotify'';');
  29001. Add(' procedure Println(a,b: longint); varargs; virtual; abstract;');
  29002. Add(' end;');
  29003. Add('begin');
  29004. ConvertProgram;
  29005. CheckSource('TestRTTI_Class_Method',
  29006. LinesToStr([ // statements
  29007. 'rtl.createClass(this, "TObject", null, function () {',
  29008. ' this.$init = function () {',
  29009. ' };',
  29010. ' this.$final = function () {',
  29011. ' };',
  29012. ' var $r = this.$rtti;',
  29013. ' $r.addMethod("Click", 0, null);',
  29014. ' $r.addMethod("Notify", 0, [["Sender", $r]]);',
  29015. ' $r.addMethod("GetNotify", 1, null, rtl.boolean,{flags: 4});',
  29016. ' $r.addMethod("Println", 0, [["a", rtl.longint], ["b", rtl.longint]], null, {',
  29017. ' flags: 2',
  29018. ' });',
  29019. '});',
  29020. '']),
  29021. LinesToStr([ // $mod.$main
  29022. '']));
  29023. end;
  29024. procedure TTestModule.TestRTTI_Class_MethodArgFlags;
  29025. begin
  29026. WithTypeInfo:=true;
  29027. StartProgram(false);
  29028. Add('type');
  29029. Add(' TObject = class');
  29030. Add(' published');
  29031. Add(' procedure OpenArray(const Args: array of string); virtual; abstract;');
  29032. Add(' procedure ByRef(var Value: longint; out Item: longint); virtual; abstract;');
  29033. Add(' procedure Untyped(var Value; out Item); virtual; abstract;');
  29034. Add(' end;');
  29035. Add('begin');
  29036. ConvertProgram;
  29037. CheckSource('TestRTTI_Class_MethodOpenArray',
  29038. LinesToStr([ // statements
  29039. 'rtl.createClass(this, "TObject", null, function () {',
  29040. ' this.$init = function () {',
  29041. ' };',
  29042. ' this.$final = function () {',
  29043. ' };',
  29044. ' var $r = this.$rtti;',
  29045. '$r.addMethod("OpenArray", 0, [["Args", rtl.string, 10]]);',
  29046. '$r.addMethod("ByRef", 0, [["Value", rtl.longint, 1], ["Item", rtl.longint, 4]]);',
  29047. '$r.addMethod("Untyped", 0, [["Value", null, 1], ["Item", null, 4]]);',
  29048. '});',
  29049. '']),
  29050. LinesToStr([ // $mod.$main
  29051. '']));
  29052. end;
  29053. procedure TTestModule.TestRTTI_Class_Property;
  29054. begin
  29055. WithTypeInfo:=true;
  29056. StartProgram(false);
  29057. Add('{$modeswitch externalclass}');
  29058. Add('type');
  29059. Add(' TObject = class');
  29060. Add(' private');
  29061. Add(' FColor: longint;');
  29062. Add(' FColorStored: boolean;');
  29063. Add(' procedure SetColor(Value: longint); virtual; abstract;');
  29064. Add(' function GetColor: longint; virtual; abstract;');
  29065. Add(' function GetColorStored: boolean; virtual; abstract;');
  29066. Add(' FExtSize: longint external name ''$extSize'';');
  29067. Add(' FExtSizeStored: boolean external name ''$extSizeStored'';');
  29068. Add(' procedure SetExtSize(Value: longint); external name ''$setSize'';');
  29069. Add(' function GetExtSize: longint; external name ''$getSize'';');
  29070. Add(' function GetExtSizeStored: boolean; external name ''$getExtSizeStored'';');
  29071. Add(' published');
  29072. Add(' property ColorA: longint read FColor;');
  29073. Add(' property ColorB: longint write FColor;');
  29074. Add(' property ColorC: longint read GetColor write SetColor;');
  29075. Add(' property ColorD: longint read FColor write FColor stored FColorStored;');
  29076. Add(' property ExtSizeA: longint read FExtSize write FExtSize;');
  29077. Add(' property ExtSizeB: longint read GetExtSize write SetExtSize stored FExtSizeStored;');
  29078. Add(' property ExtSizeC: longint read FExtSize write FExtSize stored GetExtSizeStored;');
  29079. Add(' end;');
  29080. Add('begin');
  29081. ConvertProgram;
  29082. CheckSource('TestRTTI_Class_Property',
  29083. LinesToStr([ // statements
  29084. 'rtl.createClass(this, "TObject", null, function () {',
  29085. ' this.$init = function () {',
  29086. ' this.FColor = 0;',
  29087. ' this.FColorStored = false;',
  29088. ' };',
  29089. ' this.$final = function () {',
  29090. ' };',
  29091. ' var $r = this.$rtti;',
  29092. ' $r.addProperty("ColorA", 0, rtl.longint, "FColor", "");',
  29093. ' $r.addProperty("ColorB", 0, rtl.longint, "", "FColor");',
  29094. ' $r.addProperty("ColorC", 3, rtl.longint, "GetColor", "SetColor");',
  29095. ' $r.addProperty(',
  29096. ' "ColorD",',
  29097. ' 8,',
  29098. ' rtl.longint,',
  29099. ' "FColor",',
  29100. ' "FColor",',
  29101. ' {',
  29102. ' stored: "FColorStored"',
  29103. ' }',
  29104. ' );',
  29105. ' $r.addProperty("ExtSizeA", 0, rtl.longint, "$extSize", "$extSize");',
  29106. ' $r.addProperty(',
  29107. ' "ExtSizeB",',
  29108. ' 11,',
  29109. ' rtl.longint,',
  29110. ' "$getSize",',
  29111. ' "$setSize",',
  29112. ' {',
  29113. ' stored: "$extSizeStored"',
  29114. ' }',
  29115. ' );',
  29116. ' $r.addProperty(',
  29117. ' "ExtSizeC",',
  29118. ' 12,',
  29119. ' rtl.longint,',
  29120. ' "$extSize",',
  29121. ' "$extSize",',
  29122. ' {',
  29123. ' stored: "$getExtSizeStored"',
  29124. ' }',
  29125. ' );',
  29126. '});',
  29127. '']),
  29128. LinesToStr([ // $mod.$main
  29129. '']));
  29130. end;
  29131. procedure TTestModule.TestRTTI_Class_PropertyParams;
  29132. begin
  29133. WithTypeInfo:=true;
  29134. StartProgram(false);
  29135. Add('{$modeswitch externalclass}');
  29136. Add('type');
  29137. Add(' integer = longint;');
  29138. Add(' TObject = class');
  29139. Add(' private');
  29140. Add(' function GetItems(i: integer): tobject; virtual; abstract;');
  29141. Add(' procedure SetItems(i: integer; value: tobject); virtual; abstract;');
  29142. Add(' function GetValues(const i: integer; var b: boolean): char; virtual; abstract;');
  29143. Add(' procedure SetValues(const i: integer; var b: boolean; value: char); virtual; abstract;');
  29144. Add(' published');
  29145. Add(' property Items[Index: integer]: tobject read getitems write setitems;');
  29146. Add(' property Values[const keya: integer; var keyb: boolean]: char read getvalues write setvalues;');
  29147. Add(' end;');
  29148. Add('begin');
  29149. ConvertProgram;
  29150. CheckSource('TestRTTI_Class_PropertyParams',
  29151. LinesToStr([ // statements
  29152. 'rtl.createClass(this, "TObject", null, function () {',
  29153. ' this.$init = function () {',
  29154. ' };',
  29155. ' this.$final = function () {',
  29156. ' };',
  29157. ' var $r = this.$rtti;',
  29158. ' $r.addProperty("Items", 3, $r, "GetItems", "SetItems");',
  29159. ' $r.addProperty("Values", 3, rtl.char, "GetValues", "SetValues");',
  29160. '});',
  29161. '']),
  29162. LinesToStr([ // $mod.$main
  29163. '']));
  29164. end;
  29165. procedure TTestModule.TestRTTI_Class_OtherUnit_TypeAlias;
  29166. begin
  29167. WithTypeInfo:=true;
  29168. AddModuleWithIntfImplSrc('unit1.pas',
  29169. 'type TColor = -5..5;',
  29170. '');
  29171. StartProgram(true);
  29172. Add([
  29173. 'uses unit1;',
  29174. 'type',
  29175. ' TColorAlias = TColor;',
  29176. ' TColorTypeAlias = type TColor;',
  29177. ' TObject = class',
  29178. ' private',
  29179. ' fColor: TColor;',
  29180. ' fAlias: TColorAlias;',
  29181. ' fTypeAlias: TColorTypeAlias;',
  29182. ' published',
  29183. ' property Color: TColor read fcolor;',
  29184. ' property Alias: TColorAlias read falias;',
  29185. ' property TypeAlias: TColorTypeAlias read ftypealias;',
  29186. ' end;',
  29187. 'begin',
  29188. '']);
  29189. ConvertProgram;
  29190. CheckSource('TestRTTI_Class_OtherUnit_TypeAlias',
  29191. LinesToStr([ // statements
  29192. 'this.$rtti.$inherited("TColorTypeAlias", pas.unit1.$rtti["TColor"], {});',
  29193. 'rtl.createClass(this, "TObject", null, function () {',
  29194. ' this.$init = function () {',
  29195. ' this.fColor = 0;',
  29196. ' this.fAlias = 0;',
  29197. ' this.fTypeAlias = 0;',
  29198. ' };',
  29199. ' this.$final = function () {',
  29200. ' };',
  29201. ' var $r = this.$rtti;',
  29202. ' $r.addProperty("Color", 0, pas.unit1.$rtti["TColor"], "fColor", "");',
  29203. ' $r.addProperty("Alias", 0, pas.unit1.$rtti["TColor"], "fAlias", "");',
  29204. ' $r.addProperty("TypeAlias", 0, $mod.$rtti["TColorTypeAlias"], "fTypeAlias", "");',
  29205. '});',
  29206. '']),
  29207. LinesToStr([ // $mod.$main
  29208. '']));
  29209. end;
  29210. procedure TTestModule.TestRTTI_Class_OmitRTTI;
  29211. begin
  29212. WithTypeInfo:=true;
  29213. StartProgram(false);
  29214. Add([
  29215. '{$modeswitch omitrtti}',
  29216. 'type',
  29217. ' TObject = class',
  29218. ' private',
  29219. ' FA: byte;',
  29220. ' published',
  29221. ' property A: byte read FA write FA;',
  29222. ' end;',
  29223. 'begin']);
  29224. ConvertProgram;
  29225. CheckSource('TestRTTI_Class_OmitRTTI',
  29226. LinesToStr([ // statements
  29227. 'rtl.createClass(this, "TObject", null, function () {',
  29228. ' this.$init = function () {',
  29229. ' this.FA = 0;',
  29230. ' };',
  29231. ' this.$final = function () {',
  29232. ' };',
  29233. '});',
  29234. '']),
  29235. LinesToStr([ // $mod.$main
  29236. '']));
  29237. end;
  29238. procedure TTestModule.TestRTTI_Class_Field_AnonymousArrayOfSelfClass;
  29239. begin
  29240. WithTypeInfo:=true;
  29241. StartUnit(true,[supTObject]);
  29242. Add([
  29243. 'interface',
  29244. 'type',
  29245. ' {$M+}',
  29246. ' TBird = class',
  29247. ' published',
  29248. ' Swarm: array of TBird;',
  29249. ' end;',
  29250. 'implementation',
  29251. '']);
  29252. ConvertUnit;
  29253. CheckSource('TestRTTI_Class_Field_AnonymousArrayOfSelfClass',
  29254. LinesToStr([ // statements
  29255. 'rtl.createClass(this, "TBird", pas.system.TObject, function () {',
  29256. ' this.$init = function () {',
  29257. ' pas.system.TObject.$init.call(this);',
  29258. ' this.Swarm = [];',
  29259. ' };',
  29260. ' this.$final = function () {',
  29261. ' this.Swarm = undefined;',
  29262. ' pas.system.TObject.$final.call(this);',
  29263. ' };',
  29264. ' var $r = this.$rtti;',
  29265. ' $mod.$rtti.$DynArray("TBird.Swarm$a", {',
  29266. ' eltype: $r',
  29267. ' });',
  29268. ' $r.addField("Swarm", $mod.$rtti["TBird.Swarm$a"]);',
  29269. '});',
  29270. '']),
  29271. LinesToStr([ // $mod.$main
  29272. '']));
  29273. end;
  29274. procedure TTestModule.TestRTTI_IndexModifier;
  29275. begin
  29276. WithTypeInfo:=true;
  29277. StartProgram(false);
  29278. Add([
  29279. 'type',
  29280. ' TEnum = (red, blue);',
  29281. ' TObject = class',
  29282. ' FB: boolean;',
  29283. ' procedure SetIntBool(Index: longint; b: boolean); virtual; abstract;',
  29284. ' function GetBoolBool(Index: boolean): boolean; virtual; abstract;',
  29285. ' procedure SetBoolBool(Index: boolean; b: boolean); virtual; abstract;',
  29286. ' function GetEnumBool(Index: TEnum): boolean; virtual; abstract;',
  29287. ' function GetStrIntBool(A: String; I: longint): boolean; virtual; abstract;',
  29288. ' procedure SetStrIntBool(A: String; I: longint; b: boolean); virtual; abstract;',
  29289. ' published',
  29290. ' property B1: boolean index 1 read FB write SetIntBool;',
  29291. ' property B2: boolean index TEnum.blue read GetEnumBool write FB;',
  29292. ' property I1[A: String]: boolean index 2 read GetStrIntBool write SetStrIntBool;',
  29293. ' end;',
  29294. 'begin']);
  29295. ConvertProgram;
  29296. CheckSource('TestRTTI_IndexModifier',
  29297. LinesToStr([ // statements
  29298. 'this.TEnum = {',
  29299. ' "0": "red",',
  29300. ' red: 0,',
  29301. ' "1": "blue",',
  29302. ' blue: 1',
  29303. '};',
  29304. 'this.$rtti.$Enum("TEnum", {',
  29305. ' minvalue: 0,',
  29306. ' maxvalue: 1,',
  29307. ' ordtype: 1,',
  29308. ' enumtype: this.TEnum',
  29309. '});',
  29310. 'rtl.createClass(this, "TObject", null, function () {',
  29311. ' this.$init = function () {',
  29312. ' this.FB = false;',
  29313. ' };',
  29314. ' this.$final = function () {',
  29315. ' };',
  29316. ' var $r = this.$rtti;',
  29317. ' $r.addProperty(',
  29318. ' "B1",',
  29319. ' 18,',
  29320. ' rtl.boolean,',
  29321. ' "FB",',
  29322. ' "SetIntBool",',
  29323. ' {',
  29324. ' index: 1',
  29325. ' }',
  29326. ' );',
  29327. ' $r.addProperty(',
  29328. ' "B2",',
  29329. ' 17,',
  29330. ' rtl.boolean,',
  29331. ' "GetEnumBool",',
  29332. ' "FB",',
  29333. ' {',
  29334. ' index: $mod.TEnum.blue',
  29335. ' }',
  29336. ' );',
  29337. ' $r.addProperty(',
  29338. ' "I1",',
  29339. ' 19,',
  29340. ' rtl.boolean,',
  29341. ' "GetStrIntBool",',
  29342. ' "SetStrIntBool",',
  29343. ' {',
  29344. ' index: 2',
  29345. ' }',
  29346. ' );',
  29347. '});',
  29348. '']),
  29349. LinesToStr([ // $mod.$main
  29350. '']));
  29351. end;
  29352. procedure TTestModule.TestRTTI_StoredModifier;
  29353. begin
  29354. WithTypeInfo:=true;
  29355. StartProgram(false);
  29356. Add([
  29357. 'const',
  29358. ' ConstB = true;',
  29359. 'type',
  29360. ' TObject = class',
  29361. ' private',
  29362. ' FB: boolean;',
  29363. ' function IsBStored: boolean; virtual; abstract;',
  29364. ' published',
  29365. ' property BoolA: boolean read FB stored true;',
  29366. ' property BoolB: boolean read FB stored false;',
  29367. ' property BoolC: boolean read FB stored FB;',
  29368. ' property BoolD: boolean read FB stored ConstB;',
  29369. ' property BoolE: boolean read FB stored IsBStored;',
  29370. ' end;',
  29371. 'begin']);
  29372. ConvertProgram;
  29373. CheckSource('TestRTTI_StoredModifier',
  29374. LinesToStr([ // statements
  29375. 'this.ConstB = true;',
  29376. 'rtl.createClass(this, "TObject", null, function () {',
  29377. ' this.$init = function () {',
  29378. ' this.FB = false;',
  29379. ' };',
  29380. ' this.$final = function () {',
  29381. ' };',
  29382. ' var $r = this.$rtti;',
  29383. ' $r.addProperty("BoolA", 0, rtl.boolean, "FB", "");',
  29384. ' $r.addProperty("BoolB", 4, rtl.boolean, "FB", "");',
  29385. ' $r.addProperty(',
  29386. ' "BoolC",',
  29387. ' 8,',
  29388. ' rtl.boolean,',
  29389. ' "FB",',
  29390. ' "",',
  29391. ' {',
  29392. ' stored: "FB"',
  29393. ' }',
  29394. ' );',
  29395. ' $r.addProperty("BoolD", 0, rtl.boolean, "FB", "");',
  29396. ' $r.addProperty(',
  29397. ' "BoolE",',
  29398. ' 12,',
  29399. ' rtl.boolean,',
  29400. ' "FB",',
  29401. ' "",',
  29402. ' {',
  29403. ' stored: "IsBStored"',
  29404. ' }',
  29405. ' );',
  29406. '});',
  29407. '']),
  29408. LinesToStr([ // $mod.$main
  29409. '']));
  29410. end;
  29411. procedure TTestModule.TestRTTI_DefaultValue;
  29412. begin
  29413. WithTypeInfo:=true;
  29414. StartProgram(false);
  29415. Add([
  29416. 'type',
  29417. ' TEnum = (red, blue);',
  29418. 'const',
  29419. ' CB = true or false;',
  29420. ' CI = 1+2;',
  29421. 'type',
  29422. ' TObject = class',
  29423. ' FB: boolean;',
  29424. ' FI: longint;',
  29425. ' FE: TEnum;',
  29426. ' published',
  29427. ' property B1: boolean read FB default true;',
  29428. ' property B2: boolean read FB default CB;',
  29429. ' property B3: boolean read FB default test1.cb;',
  29430. ' property I1: longint read FI default 2;',
  29431. ' property I2: longint read FI default CI;',
  29432. ' property E1: TEnum read FE default red;',
  29433. ' property E2: TEnum read FE default TEnum.blue;',
  29434. ' end;',
  29435. 'begin']);
  29436. ConvertProgram;
  29437. CheckSource('TestRTTI_DefaultValue',
  29438. LinesToStr([ // statements
  29439. 'this.TEnum = {',
  29440. ' "0": "red",',
  29441. ' red: 0,',
  29442. ' "1": "blue",',
  29443. ' blue: 1',
  29444. '};',
  29445. 'this.$rtti.$Enum("TEnum", {',
  29446. ' minvalue: 0,',
  29447. ' maxvalue: 1,',
  29448. ' ordtype: 1,',
  29449. ' enumtype: this.TEnum',
  29450. '});',
  29451. 'this.CB = true || false;',
  29452. 'this.CI = 1 + 2;',
  29453. 'rtl.createClass(this, "TObject", null, function () {',
  29454. ' this.$init = function () {',
  29455. ' this.FB = false;',
  29456. ' this.FI = 0;',
  29457. ' this.FE = 0;',
  29458. ' };',
  29459. ' this.$final = function () {',
  29460. ' };',
  29461. ' var $r = this.$rtti;',
  29462. ' $r.addProperty(',
  29463. ' "B1",',
  29464. ' 0,',
  29465. ' rtl.boolean,',
  29466. ' "FB",',
  29467. ' "",',
  29468. ' {',
  29469. ' Default: true',
  29470. ' }',
  29471. ' );',
  29472. ' $r.addProperty(',
  29473. ' "B2",',
  29474. ' 0,',
  29475. ' rtl.boolean,',
  29476. ' "FB",',
  29477. ' "",',
  29478. ' {',
  29479. ' Default: true',
  29480. ' }',
  29481. ' );',
  29482. ' $r.addProperty(',
  29483. ' "B3",',
  29484. ' 0,',
  29485. ' rtl.boolean,',
  29486. ' "FB",',
  29487. ' "",',
  29488. ' {',
  29489. ' Default: true',
  29490. ' }',
  29491. ' );',
  29492. ' $r.addProperty(',
  29493. ' "I1",',
  29494. ' 0,',
  29495. ' rtl.longint,',
  29496. ' "FI",',
  29497. ' "",',
  29498. ' {',
  29499. ' Default: 2',
  29500. ' }',
  29501. ' );',
  29502. ' $r.addProperty(',
  29503. ' "I2",',
  29504. ' 0,',
  29505. ' rtl.longint,',
  29506. ' "FI",',
  29507. ' "",',
  29508. ' {',
  29509. ' Default: 3',
  29510. ' }',
  29511. ' );',
  29512. ' $r.addProperty(',
  29513. ' "E1",',
  29514. ' 0,',
  29515. ' $mod.$rtti["TEnum"],',
  29516. ' "FE",',
  29517. ' "",',
  29518. ' {',
  29519. ' Default: $mod.TEnum.red',
  29520. ' }',
  29521. ' );',
  29522. ' $r.addProperty(',
  29523. ' "E2",',
  29524. ' 0,',
  29525. ' $mod.$rtti["TEnum"],',
  29526. ' "FE",',
  29527. ' "",',
  29528. ' {',
  29529. ' Default: $mod.TEnum.blue',
  29530. ' }',
  29531. ' );',
  29532. '});',
  29533. '']),
  29534. LinesToStr([ // $mod.$main
  29535. '']));
  29536. end;
  29537. procedure TTestModule.TestRTTI_DefaultValueSet;
  29538. begin
  29539. WithTypeInfo:=true;
  29540. StartProgram(false);
  29541. Add([
  29542. 'type',
  29543. ' TEnum = (red, blue);',
  29544. ' TSet = set of TEnum;',
  29545. 'const',
  29546. ' CSet = [red,blue];',
  29547. 'type',
  29548. ' TObject = class',
  29549. ' FSet: TSet;',
  29550. ' published',
  29551. ' property Set1: TSet read FSet default [];',
  29552. ' property Set2: TSet read FSet default [red];',
  29553. ' property Set3: TSet read FSet default [red,blue];',
  29554. ' property Set4: TSet read FSet default CSet;',
  29555. ' end;',
  29556. 'begin']);
  29557. ConvertProgram;
  29558. CheckSource('TestRTTI_DefaultValueSet',
  29559. LinesToStr([ // statements
  29560. 'this.TEnum = {',
  29561. ' "0": "red",',
  29562. ' red: 0,',
  29563. ' "1": "blue",',
  29564. ' blue: 1',
  29565. '};',
  29566. 'this.$rtti.$Enum("TEnum", {',
  29567. ' minvalue: 0,',
  29568. ' maxvalue: 1,',
  29569. ' ordtype: 1,',
  29570. ' enumtype: this.TEnum',
  29571. '});',
  29572. 'this.$rtti.$Set("TSet", {',
  29573. ' comptype: this.$rtti["TEnum"]',
  29574. '});',
  29575. 'this.CSet = rtl.createSet(this.TEnum.red, this.TEnum.blue);',
  29576. 'rtl.createClass(this, "TObject", null, function () {',
  29577. ' this.$init = function () {',
  29578. ' this.FSet = {};',
  29579. ' };',
  29580. ' this.$final = function () {',
  29581. ' this.FSet = undefined;',
  29582. ' };',
  29583. ' var $r = this.$rtti;',
  29584. ' $r.addProperty(',
  29585. ' "Set1",',
  29586. ' 0,',
  29587. ' $mod.$rtti["TSet"],',
  29588. ' "FSet",',
  29589. ' "",',
  29590. ' {',
  29591. ' Default: {}',
  29592. ' }',
  29593. ' );',
  29594. ' $r.addProperty(',
  29595. ' "Set2",',
  29596. ' 0,',
  29597. ' $mod.$rtti["TSet"],',
  29598. ' "FSet",',
  29599. ' "",',
  29600. ' {',
  29601. ' Default: rtl.createSet($mod.TEnum.red)',
  29602. ' }',
  29603. ' );',
  29604. ' $r.addProperty(',
  29605. ' "Set3",',
  29606. ' 0,',
  29607. ' $mod.$rtti["TSet"],',
  29608. ' "FSet",',
  29609. ' "",',
  29610. ' {',
  29611. ' Default: rtl.createSet($mod.TEnum.red, $mod.TEnum.blue)',
  29612. ' }',
  29613. ' );',
  29614. ' $r.addProperty(',
  29615. ' "Set4",',
  29616. ' 0,',
  29617. ' $mod.$rtti["TSet"],',
  29618. ' "FSet",',
  29619. ' "",',
  29620. ' {',
  29621. ' Default: $mod.CSet',
  29622. ' }',
  29623. ' );',
  29624. '});',
  29625. '']),
  29626. LinesToStr([ // $mod.$main
  29627. '']));
  29628. end;
  29629. procedure TTestModule.TestRTTI_DefaultValueRangeType;
  29630. begin
  29631. WithTypeInfo:=true;
  29632. StartProgram(false);
  29633. Add([
  29634. 'type',
  29635. ' TRg = -1..1;',
  29636. 'const',
  29637. ' l = low(TRg);',
  29638. ' h = high(TRg);',
  29639. 'type',
  29640. ' TObject = class',
  29641. ' FV: TRg;',
  29642. ' published',
  29643. ' property V1: TRg read FV default -1;',
  29644. ' end;',
  29645. 'begin']);
  29646. ConvertProgram;
  29647. CheckSource('TestRTTI_DefaultValueRangeType',
  29648. LinesToStr([ // statements
  29649. 'this.$rtti.$Int("TRg", {',
  29650. ' minvalue: -1,',
  29651. ' maxvalue: 1,',
  29652. ' ordtype: 0',
  29653. '});',
  29654. 'this.l = -1;',
  29655. 'this.h = 1;',
  29656. 'rtl.createClass(this, "TObject", null, function () {',
  29657. ' this.$init = function () {',
  29658. ' this.FV = 0;',
  29659. ' };',
  29660. ' this.$final = function () {',
  29661. ' };',
  29662. ' var $r = this.$rtti;',
  29663. ' $r.addProperty(',
  29664. ' "V1",',
  29665. ' 0,',
  29666. ' $mod.$rtti["TRg"],',
  29667. ' "FV",',
  29668. ' "",',
  29669. ' {',
  29670. ' Default: -1',
  29671. ' }',
  29672. ' );',
  29673. '});',
  29674. '']),
  29675. LinesToStr([ // $mod.$main
  29676. '']));
  29677. end;
  29678. procedure TTestModule.TestRTTI_DefaultValueInherit;
  29679. begin
  29680. WithTypeInfo:=true;
  29681. StartProgram(false);
  29682. Add([
  29683. 'type',
  29684. ' TObject = class',
  29685. ' FA, FB: byte;',
  29686. ' property A: byte read FA default 1;',
  29687. ' property B: byte read FB default 2;',
  29688. ' end;',
  29689. ' TBird = class',
  29690. ' published',
  29691. ' property A;',
  29692. ' property B nodefault;',
  29693. ' end;',
  29694. 'begin']);
  29695. ConvertProgram;
  29696. CheckSource('TestRTTI_DefaultValueInherit',
  29697. LinesToStr([ // statements
  29698. 'rtl.createClass(this, "TObject", null, function () {',
  29699. ' this.$init = function () {',
  29700. ' this.FA = 0;',
  29701. ' this.FB = 0;',
  29702. ' };',
  29703. ' this.$final = function () {',
  29704. ' };',
  29705. '});',
  29706. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  29707. ' var $r = this.$rtti;',
  29708. ' $r.addProperty(',
  29709. ' "A",',
  29710. ' 0,',
  29711. ' rtl.byte,',
  29712. ' "FA",',
  29713. ' "",',
  29714. ' {',
  29715. ' Default: 1',
  29716. ' }',
  29717. ' );',
  29718. ' $r.addProperty("B", 0, rtl.byte, "FB", "");',
  29719. '});',
  29720. '']),
  29721. LinesToStr([ // $mod.$main
  29722. '']));
  29723. end;
  29724. procedure TTestModule.TestRTTI_OverrideMethod;
  29725. begin
  29726. WithTypeInfo:=true;
  29727. StartProgram(false);
  29728. Add('type');
  29729. Add(' TObject = class');
  29730. Add(' published');
  29731. Add(' procedure DoIt; virtual; abstract;');
  29732. Add(' end;');
  29733. Add(' TSky = class');
  29734. Add(' published');
  29735. Add(' procedure DoIt; override;');
  29736. Add(' end;');
  29737. Add('procedure TSky.DoIt; begin end;');
  29738. Add('begin');
  29739. ConvertProgram;
  29740. CheckSource('TestRTTI_OverrideMethod',
  29741. LinesToStr([ // statements
  29742. 'rtl.createClass(this, "TObject", null, function () {',
  29743. ' this.$init = function () {',
  29744. ' };',
  29745. ' this.$final = function () {',
  29746. ' };',
  29747. ' var $r = this.$rtti;',
  29748. ' $r.addMethod("DoIt", 0, null);',
  29749. '});',
  29750. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  29751. ' this.DoIt = function () {',
  29752. ' };',
  29753. '});',
  29754. '']),
  29755. LinesToStr([ // $mod.$main
  29756. '']));
  29757. end;
  29758. procedure TTestModule.TestRTTI_ReintroduceMethod;
  29759. begin
  29760. WithTypeInfo:=true;
  29761. StartProgram(false);
  29762. Add([
  29763. 'type',
  29764. ' TObject = class',
  29765. ' published',
  29766. ' procedure DoIt;',
  29767. ' end;',
  29768. ' TSky = class',
  29769. ' published',
  29770. ' procedure DoIt; reintroduce;',
  29771. ' end;',
  29772. 'procedure TObject.DoIt; begin end;',
  29773. 'procedure TSky.DoIt;',
  29774. 'begin',
  29775. ' inherited DoIt;',
  29776. 'end;',
  29777. 'begin']);
  29778. ConvertProgram;
  29779. CheckSource('TestRTTI_ReintroduceMethod',
  29780. LinesToStr([ // statements
  29781. 'rtl.createClass(this, "TObject", null, function () {',
  29782. ' this.$init = function () {',
  29783. ' };',
  29784. ' this.$final = function () {',
  29785. ' };',
  29786. ' this.DoIt = function () {',
  29787. ' };',
  29788. ' var $r = this.$rtti;',
  29789. ' $r.addMethod("DoIt", 0, null);',
  29790. '});',
  29791. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  29792. ' this.DoIt = function () {',
  29793. ' $mod.TObject.DoIt.call(this);',
  29794. ' };',
  29795. ' var $r = this.$rtti;',
  29796. ' $r.addMethod("DoIt", 0, null);',
  29797. '});',
  29798. '']),
  29799. LinesToStr([ // $mod.$main
  29800. '']));
  29801. end;
  29802. procedure TTestModule.TestRTTI_OverloadProperty;
  29803. begin
  29804. WithTypeInfo:=true;
  29805. StartProgram(false);
  29806. Add('type');
  29807. Add(' TObject = class');
  29808. Add(' protected');
  29809. Add(' FFlag: longint;');
  29810. Add(' published');
  29811. Add(' property Flag: longint read fflag;');
  29812. Add(' end;');
  29813. Add(' TSky = class');
  29814. Add(' published');
  29815. Add(' property FLAG: longint write fflag;');
  29816. Add(' end;');
  29817. Add('begin');
  29818. ConvertProgram;
  29819. CheckSource('TestRTTI_OverrideMethod',
  29820. LinesToStr([ // statements
  29821. 'rtl.createClass(this, "TObject", null, function () {',
  29822. ' this.$init = function () {',
  29823. ' this.FFlag = 0;',
  29824. ' };',
  29825. ' this.$final = function () {',
  29826. ' };',
  29827. ' var $r = this.$rtti;',
  29828. ' $r.addProperty("Flag", 0, rtl.longint, "FFlag", "");',
  29829. '});',
  29830. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  29831. ' var $r = this.$rtti;',
  29832. ' $r.addProperty("Flag", 0, rtl.longint, "", "FFlag");',
  29833. '});',
  29834. '']),
  29835. LinesToStr([ // $mod.$main
  29836. '']));
  29837. end;
  29838. procedure TTestModule.TestRTTI_ClassForward;
  29839. begin
  29840. WithTypeInfo:=true;
  29841. StartProgram(false);
  29842. Add('type');
  29843. Add(' TObject = class end;');
  29844. Add(' tbridge = class;');
  29845. Add(' TProc = function: tbridge;');
  29846. Add(' TOger = class');
  29847. Add(' published');
  29848. Add(' FBridge: tbridge;');
  29849. Add(' procedure SetBridge(Value: tbridge); virtual; abstract;');
  29850. Add(' property Bridge: tbridge read fbridge write setbridge;');
  29851. Add(' end;');
  29852. Add(' TBridge = class');
  29853. Add(' FOger: toger;');
  29854. Add(' end;');
  29855. Add('var p: Pointer;');
  29856. Add(' b: tbridge;');
  29857. Add('begin');
  29858. Add(' p:=typeinfo(tbridge);');
  29859. Add(' p:=typeinfo(b);');
  29860. ConvertProgram;
  29861. CheckSource('TestRTTI_ClassForward',
  29862. LinesToStr([ // statements
  29863. 'rtl.createClass(this, "TObject", null, function () {',
  29864. ' this.$init = function () {',
  29865. ' };',
  29866. ' this.$final = function () {',
  29867. ' };',
  29868. '});',
  29869. 'this.$rtti.$Class("TBridge");',
  29870. 'this.$rtti.$ProcVar("TProc", {',
  29871. ' procsig: rtl.newTIProcSig(null, this.$rtti["TBridge"])',
  29872. '});',
  29873. 'rtl.createClass(this, "TOger", this.TObject, function () {',
  29874. ' this.$init = function () {',
  29875. ' $mod.TObject.$init.call(this);',
  29876. ' this.FBridge = null;',
  29877. ' };',
  29878. ' this.$final = function () {',
  29879. ' this.FBridge = undefined;',
  29880. ' $mod.TObject.$final.call(this);',
  29881. ' };',
  29882. ' var $r = this.$rtti;',
  29883. ' $r.addField("FBridge", $mod.$rtti["TBridge"]);',
  29884. ' $r.addMethod("SetBridge", 0, [["Value", $mod.$rtti["TBridge"]]]);',
  29885. ' $r.addProperty("Bridge", 2, $mod.$rtti["TBridge"], "FBridge", "SetBridge");',
  29886. '});',
  29887. 'rtl.createClass(this, "TBridge", this.TObject, function () {',
  29888. ' this.$init = function () {',
  29889. ' $mod.TObject.$init.call(this);',
  29890. ' this.FOger = null;',
  29891. ' };',
  29892. ' this.$final = function () {',
  29893. ' this.FOger = undefined;',
  29894. ' $mod.TObject.$final.call(this);',
  29895. ' };',
  29896. '});',
  29897. 'this.p = null;',
  29898. 'this.b = null;',
  29899. '']),
  29900. LinesToStr([ // $mod.$main
  29901. '$mod.p = $mod.$rtti["TBridge"];',
  29902. '$mod.p = $mod.b.$rtti;',
  29903. '']));
  29904. end;
  29905. procedure TTestModule.TestRTTI_ClassOf;
  29906. begin
  29907. WithTypeInfo:=true;
  29908. StartProgram(false);
  29909. Add('type');
  29910. Add(' TClass = class of tobject;');
  29911. Add(' TProcA = function: TClass;');
  29912. Add(' TObject = class');
  29913. Add(' published');
  29914. Add(' C: tclass;');
  29915. Add(' end;');
  29916. Add(' tfox = class;');
  29917. Add(' TBird = class end;');
  29918. Add(' TBirds = class of tbird;');
  29919. Add(' TFox = class end;');
  29920. Add(' TFoxes = class of tfox;');
  29921. Add(' TCows = class of TCow;');
  29922. Add(' TCow = class;');
  29923. Add(' TCow = class end;');
  29924. Add('begin');
  29925. ConvertProgram;
  29926. CheckSource('TestRTTI_ClassOf',
  29927. LinesToStr([ // statements
  29928. 'this.$rtti.$Class("TObject");',
  29929. 'this.$rtti.$ClassRef("TClass", {',
  29930. ' instancetype: this.$rtti["TObject"]',
  29931. '});',
  29932. 'this.$rtti.$ProcVar("TProcA", {',
  29933. ' procsig: rtl.newTIProcSig(null, this.$rtti["TClass"])',
  29934. '});',
  29935. 'rtl.createClass(this, "TObject", null, function () {',
  29936. ' this.$init = function () {',
  29937. ' this.C = null;',
  29938. ' };',
  29939. ' this.$final = function () {',
  29940. ' this.C = undefined;',
  29941. ' };',
  29942. ' var $r = this.$rtti;',
  29943. ' $r.addField("C", $mod.$rtti["TClass"]);',
  29944. '});',
  29945. 'this.$rtti.$Class("TFox");',
  29946. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  29947. '});',
  29948. 'this.$rtti.$ClassRef("TBirds", {',
  29949. ' instancetype: this.$rtti["TBird"]',
  29950. '});',
  29951. 'rtl.createClass(this, "TFox", this.TObject, function () {',
  29952. '});',
  29953. 'this.$rtti.$ClassRef("TFoxes", {',
  29954. ' instancetype: this.$rtti["TFox"]',
  29955. '});',
  29956. 'this.$rtti.$Class("TCow");',
  29957. 'this.$rtti.$ClassRef("TCows", {',
  29958. ' instancetype: this.$rtti["TCow"]',
  29959. '});',
  29960. 'rtl.createClass(this, "TCow", this.TObject, function () {',
  29961. '});',
  29962. '']),
  29963. LinesToStr([ // $mod.$main
  29964. '']));
  29965. end;
  29966. procedure TTestModule.TestRTTI_Record;
  29967. begin
  29968. WithTypeInfo:=true;
  29969. StartProgram(false);
  29970. Add('type');
  29971. Add(' integer = longint;');
  29972. Add(' TPoint = record');
  29973. Add(' x,y: integer;');
  29974. Add(' end;');
  29975. Add('var p: pointer;');
  29976. Add(' r: tpoint;');
  29977. Add('begin');
  29978. Add(' p:=typeinfo(tpoint);');
  29979. Add(' p:=typeinfo(r);');
  29980. Add(' p:=typeinfo(r.x);');
  29981. ConvertProgram;
  29982. CheckSource('TestRTTI_Record',
  29983. LinesToStr([ // statements
  29984. 'rtl.recNewT(this, "TPoint", function () {',
  29985. ' this.x = 0;',
  29986. ' this.y = 0;',
  29987. ' this.$eq = function (b) {',
  29988. ' return (this.x === b.x) && (this.y === b.y);',
  29989. ' };',
  29990. ' this.$assign = function (s) {',
  29991. ' this.x = s.x;',
  29992. ' this.y = s.y;',
  29993. ' return this;',
  29994. ' };',
  29995. ' var $r = $mod.$rtti.$Record("TPoint", {});',
  29996. ' $r.addField("x", rtl.longint);',
  29997. ' $r.addField("y", rtl.longint);',
  29998. '});',
  29999. 'this.p = null;',
  30000. 'this.r = this.TPoint.$new();',
  30001. '']),
  30002. LinesToStr([ // $mod.$main
  30003. '$mod.p = $mod.$rtti["TPoint"];',
  30004. '$mod.p = $mod.$rtti["TPoint"];',
  30005. '$mod.p = rtl.longint;',
  30006. '']));
  30007. end;
  30008. procedure TTestModule.TestRTTI_RecordAnonymousArray;
  30009. begin
  30010. WithTypeInfo:=true;
  30011. StartProgram(false);
  30012. Add('type');
  30013. Add(' TFloatRec = record');
  30014. Add(' c,d: array of char;');
  30015. // Add(' i: array of array of longint;');
  30016. Add(' end;');
  30017. Add('var p: pointer;');
  30018. Add(' r: tfloatrec;');
  30019. Add('begin');
  30020. Add(' p:=typeinfo(tfloatrec);');
  30021. Add(' p:=typeinfo(r);');
  30022. Add(' p:=typeinfo(r.d);');
  30023. ConvertProgram;
  30024. CheckSource('TestRTTI_Record',
  30025. LinesToStr([ // statements
  30026. 'rtl.recNewT(this, "TFloatRec", function () {',
  30027. ' this.$new = function () {',
  30028. ' var r = Object.create(this);',
  30029. ' r.c = [];',
  30030. ' r.d = [];',
  30031. ' return r;',
  30032. ' };',
  30033. ' this.$eq = function (b) {',
  30034. ' return (this.c === b.c) && (this.d === b.d);',
  30035. ' };',
  30036. ' this.$assign = function (s) {',
  30037. ' this.c = rtl.arrayRef(s.c);',
  30038. ' this.d = rtl.arrayRef(s.d);',
  30039. ' return this;',
  30040. ' };',
  30041. ' var $r = $mod.$rtti.$Record("TFloatRec", {});',
  30042. ' $mod.$rtti.$DynArray("TFloatRec.d$a", {',
  30043. ' eltype: rtl.char',
  30044. ' });',
  30045. ' $r.addField("c", $mod.$rtti["TFloatRec.d$a"]);',
  30046. ' $r.addField("d", $mod.$rtti["TFloatRec.d$a"]);',
  30047. '});',
  30048. 'this.p = null;',
  30049. 'this.r = this.TFloatRec.$new();',
  30050. '']),
  30051. LinesToStr([ // $mod.$main
  30052. '$mod.p = $mod.$rtti["TFloatRec"];',
  30053. '$mod.p = $mod.$rtti["TFloatRec"];',
  30054. '$mod.p = $mod.$rtti["TFloatRec.d$a"];',
  30055. '']));
  30056. end;
  30057. procedure TTestModule.TestRTTI_Record_ClassVarType;
  30058. begin
  30059. WithTypeInfo:=true;
  30060. StartProgram(false);
  30061. Add([
  30062. '{$modeswitch AdvancedRecords}',
  30063. 'type',
  30064. ' TPoint = record',
  30065. ' type TProc = procedure(w: word);',
  30066. ' class var p: TProc;',
  30067. ' end;',
  30068. 'begin',
  30069. '']);
  30070. ConvertProgram;
  30071. CheckSource('TestRTTI_Record_ClassVarType',
  30072. LinesToStr([ // statements
  30073. 'rtl.recNewT(this, "TPoint", function () {',
  30074. ' $mod.$rtti.$ProcVar("TPoint.TProc", {',
  30075. ' procsig: rtl.newTIProcSig([["w", rtl.word]])',
  30076. ' });',
  30077. ' this.p = null;',
  30078. ' this.$eq = function (b) {',
  30079. ' return true;',
  30080. ' };',
  30081. ' this.$assign = function (s) {',
  30082. ' return this;',
  30083. ' };',
  30084. ' var $r = $mod.$rtti.$Record("TPoint", {});',
  30085. ' $r.addField("p", $mod.$rtti["TPoint.TProc"]);',
  30086. '}, true);',
  30087. '']),
  30088. LinesToStr([ // $mod.$main
  30089. '']));
  30090. end;
  30091. procedure TTestModule.TestRTTI_LocalTypes;
  30092. begin
  30093. WithTypeInfo:=true;
  30094. StartProgram(false);
  30095. Add([
  30096. 'procedure DoIt;',
  30097. 'type',
  30098. ' integer = longint;',
  30099. ' TPoint = record',
  30100. ' x,y: integer;',
  30101. ' end;',
  30102. 'var p: TPoint;',
  30103. 'begin',
  30104. 'end;',
  30105. 'begin']);
  30106. ConvertProgram;
  30107. CheckSource('TestRTTI_LocalTypes',
  30108. LinesToStr([ // statements
  30109. 'var TPoint = rtl.recNewT(null, "", function () {',
  30110. ' this.x = 0;',
  30111. ' this.y = 0;',
  30112. ' this.$eq = function (b) {',
  30113. ' return (this.x === b.x) && (this.y === b.y);',
  30114. ' };',
  30115. ' this.$assign = function (s) {',
  30116. ' this.x = s.x;',
  30117. ' this.y = s.y;',
  30118. ' return this;',
  30119. ' };',
  30120. '});',
  30121. 'this.DoIt = function () {',
  30122. ' var p = TPoint.$new();',
  30123. '};',
  30124. '']),
  30125. LinesToStr([ // $mod.$main
  30126. '']));
  30127. end;
  30128. procedure TTestModule.TestRTTI_TypeInfo_BaseTypes;
  30129. begin
  30130. WithTypeInfo:=true;
  30131. StartProgram(false);
  30132. Add([
  30133. 'type',
  30134. ' TCaption = string;',
  30135. ' TYesNo = boolean;',
  30136. ' TLetter = char;',
  30137. ' TFloat = double;',
  30138. ' TPtr = pointer;',
  30139. ' TShortInt = shortint;',
  30140. ' TByte = byte;',
  30141. ' TSmallInt = smallint;',
  30142. ' TWord = word;',
  30143. ' TInt32 = longint;',
  30144. ' TDWord = longword;',
  30145. ' TValue = jsvalue;',
  30146. 'var p: TPtr;',
  30147. 'begin',
  30148. ' p:=typeinfo(string);',
  30149. ' p:=typeinfo(tcaption);',
  30150. ' p:=typeinfo(boolean);',
  30151. ' p:=typeinfo(tyesno);',
  30152. ' p:=typeinfo(char);',
  30153. ' p:=typeinfo(tletter);',
  30154. ' p:=typeinfo(double);',
  30155. ' p:=typeinfo(tfloat);',
  30156. ' p:=typeinfo(pointer);',
  30157. ' p:=typeinfo(tptr);',
  30158. ' p:=typeinfo(shortint);',
  30159. ' p:=typeinfo(tshortint);',
  30160. ' p:=typeinfo(byte);',
  30161. ' p:=typeinfo(tbyte);',
  30162. ' p:=typeinfo(smallint);',
  30163. ' p:=typeinfo(tsmallint);',
  30164. ' p:=typeinfo(word);',
  30165. ' p:=typeinfo(tword);',
  30166. ' p:=typeinfo(longword);',
  30167. ' p:=typeinfo(tdword);',
  30168. ' p:=typeinfo(jsvalue);',
  30169. ' p:=typeinfo(tvalue);',
  30170. '']);
  30171. ConvertProgram;
  30172. CheckSource('TestRTTI_TypeInfo_BaseTypes',
  30173. LinesToStr([ // statements
  30174. 'this.p = null;',
  30175. '']),
  30176. LinesToStr([ // $mod.$main
  30177. '$mod.p = rtl.string;',
  30178. '$mod.p = rtl.string;',
  30179. '$mod.p = rtl.boolean;',
  30180. '$mod.p = rtl.boolean;',
  30181. '$mod.p = rtl.char;',
  30182. '$mod.p = rtl.char;',
  30183. '$mod.p = rtl.double;',
  30184. '$mod.p = rtl.double;',
  30185. '$mod.p = rtl.pointer;',
  30186. '$mod.p = rtl.pointer;',
  30187. '$mod.p = rtl.shortint;',
  30188. '$mod.p = rtl.shortint;',
  30189. '$mod.p = rtl.byte;',
  30190. '$mod.p = rtl.byte;',
  30191. '$mod.p = rtl.smallint;',
  30192. '$mod.p = rtl.smallint;',
  30193. '$mod.p = rtl.word;',
  30194. '$mod.p = rtl.word;',
  30195. '$mod.p = rtl.longword;',
  30196. '$mod.p = rtl.longword;',
  30197. '$mod.p = rtl.jsvalue;',
  30198. '$mod.p = rtl.jsvalue;',
  30199. '']));
  30200. end;
  30201. procedure TTestModule.TestRTTI_TypeInfo_Type_BaseTypes;
  30202. begin
  30203. WithTypeInfo:=true;
  30204. StartProgram(false);
  30205. Add([
  30206. 'type',
  30207. ' TCaption = type string;',
  30208. ' TYesNo = type boolean;',
  30209. ' TLetter = type char;',
  30210. ' TFloat = type double;',
  30211. ' TPtr = type pointer;',
  30212. ' TShortInt = type shortint;',
  30213. ' TByte = type byte;',
  30214. ' TSmallInt = type smallint;',
  30215. ' TWord = type word;',
  30216. ' TInt32 = type longint;',
  30217. ' TDWord = type longword;',
  30218. ' TValue = type jsvalue;',
  30219. ' TAliasValue = type TValue;',
  30220. 'var',
  30221. ' p: TPtr;',
  30222. ' a: TAliasValue;',
  30223. 'begin',
  30224. ' p:=typeinfo(tcaption);',
  30225. ' p:=typeinfo(tyesno);',
  30226. ' p:=typeinfo(tletter);',
  30227. ' p:=typeinfo(tfloat);',
  30228. ' p:=typeinfo(tptr);',
  30229. ' p:=typeinfo(tshortint);',
  30230. ' p:=typeinfo(tbyte);',
  30231. ' p:=typeinfo(tsmallint);',
  30232. ' p:=typeinfo(tword);',
  30233. ' p:=typeinfo(tdword);',
  30234. ' p:=typeinfo(tvalue);',
  30235. ' p:=typeinfo(taliasvalue);',
  30236. ' p:=typeinfo(a);',
  30237. '']);
  30238. ConvertProgram;
  30239. CheckSource('TestRTTI_TypeInfo_Type_BaseTypes',
  30240. LinesToStr([ // statements
  30241. 'this.$rtti.$inherited("TCaption", rtl.string, {});',
  30242. 'this.$rtti.$inherited("TYesNo", rtl.boolean, {});',
  30243. 'this.$rtti.$inherited("TLetter", rtl.char, {});',
  30244. 'this.$rtti.$inherited("TFloat", rtl.double, {});',
  30245. 'this.$rtti.$inherited("TPtr", rtl.pointer, {});',
  30246. 'this.$rtti.$inherited("TShortInt", rtl.shortint, {});',
  30247. 'this.$rtti.$inherited("TByte", rtl.byte, {});',
  30248. 'this.$rtti.$inherited("TSmallInt", rtl.smallint, {});',
  30249. 'this.$rtti.$inherited("TWord", rtl.word, {});',
  30250. 'this.$rtti.$inherited("TInt32", rtl.longint, {});',
  30251. 'this.$rtti.$inherited("TDWord", rtl.longword, {});',
  30252. 'this.$rtti.$inherited("TValue", rtl.jsvalue, {});',
  30253. 'this.$rtti.$inherited("TAliasValue", this.$rtti["TValue"], {});',
  30254. 'this.p = null;',
  30255. 'this.a = undefined;',
  30256. '']),
  30257. LinesToStr([ // $mod.$main
  30258. '$mod.p = $mod.$rtti["TCaption"];',
  30259. '$mod.p = $mod.$rtti["TYesNo"];',
  30260. '$mod.p = $mod.$rtti["TLetter"];',
  30261. '$mod.p = $mod.$rtti["TFloat"];',
  30262. '$mod.p = $mod.$rtti["TPtr"];',
  30263. '$mod.p = $mod.$rtti["TShortInt"];',
  30264. '$mod.p = $mod.$rtti["TByte"];',
  30265. '$mod.p = $mod.$rtti["TSmallInt"];',
  30266. '$mod.p = $mod.$rtti["TWord"];',
  30267. '$mod.p = $mod.$rtti["TDWord"];',
  30268. '$mod.p = $mod.$rtti["TValue"];',
  30269. '$mod.p = $mod.$rtti["TAliasValue"];',
  30270. '$mod.p = $mod.$rtti["TAliasValue"];',
  30271. '']));
  30272. end;
  30273. procedure TTestModule.TestRTTI_TypeInfo_LocalFail;
  30274. begin
  30275. WithTypeInfo:=true;
  30276. StartProgram(false);
  30277. Add('procedure DoIt;');
  30278. Add('type');
  30279. Add(' integer = longint;');
  30280. Add(' TPoint = record');
  30281. Add(' x,y: integer;');
  30282. Add(' end;');
  30283. Add('var p: pointer;');
  30284. Add('begin');
  30285. Add(' p:=typeinfo(tpoint);');
  30286. Add('end;');
  30287. Add('begin');
  30288. SetExpectedPasResolverError(sSymbolCannotBePublished,nSymbolCannotBePublished);
  30289. ConvertProgram;
  30290. end;
  30291. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses1;
  30292. begin
  30293. WithTypeInfo:=true;
  30294. StartProgram(true,[supTypeInfo]);
  30295. Add([
  30296. '{$modeswitch externalclass}',
  30297. 'type',
  30298. ' TFlag = (up,down);',
  30299. ' TFlags = set of TFlag;',
  30300. 'var',
  30301. ' ti: TTypeInfo;',
  30302. ' tiInt: TTypeInfoInteger;',
  30303. ' tiEnum: TTypeInfoEnum;',
  30304. ' tiSet: TTypeInfoSet;',
  30305. 'begin',
  30306. ' ti:=typeinfo(string);',
  30307. ' ti:=typeinfo(boolean);',
  30308. ' ti:=typeinfo(char);',
  30309. ' ti:=typeinfo(double);',
  30310. ' tiInt:=typeinfo(shortint);',
  30311. ' tiInt:=typeinfo(byte);',
  30312. ' tiInt:=typeinfo(smallint);',
  30313. ' tiInt:=typeinfo(word);',
  30314. ' tiInt:=typeinfo(longint);',
  30315. ' tiInt:=typeinfo(longword);',
  30316. ' ti:=typeinfo(jsvalue);',
  30317. ' tiEnum:=typeinfo(tflag);',
  30318. ' tiSet:=typeinfo(tflags);']);
  30319. ConvertProgram;
  30320. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses1',
  30321. LinesToStr([ // statements
  30322. 'this.TFlag = {',
  30323. ' "0": "up",',
  30324. ' up: 0,',
  30325. ' "1": "down",',
  30326. ' down: 1',
  30327. '};',
  30328. 'this.$rtti.$Enum("TFlag", {',
  30329. ' minvalue: 0,',
  30330. ' maxvalue: 1,',
  30331. ' ordtype: 1,',
  30332. ' enumtype: this.TFlag',
  30333. '});',
  30334. 'this.$rtti.$Set("TFlags", {',
  30335. ' comptype: this.$rtti["TFlag"]',
  30336. '});',
  30337. 'this.ti = null;',
  30338. 'this.tiInt = null;',
  30339. 'this.tiEnum = null;',
  30340. 'this.tiSet = null;',
  30341. '']),
  30342. LinesToStr([ // $mod.$main
  30343. '$mod.ti = rtl.string;',
  30344. '$mod.ti = rtl.boolean;',
  30345. '$mod.ti = rtl.char;',
  30346. '$mod.ti = rtl.double;',
  30347. '$mod.tiInt = rtl.shortint;',
  30348. '$mod.tiInt = rtl.byte;',
  30349. '$mod.tiInt = rtl.smallint;',
  30350. '$mod.tiInt = rtl.word;',
  30351. '$mod.tiInt = rtl.longint;',
  30352. '$mod.tiInt = rtl.longword;',
  30353. '$mod.ti = rtl.jsvalue;',
  30354. '$mod.tiEnum = $mod.$rtti["TFlag"];',
  30355. '$mod.tiSet = $mod.$rtti["TFlags"];',
  30356. '']));
  30357. end;
  30358. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses2;
  30359. begin
  30360. WithTypeInfo:=true;
  30361. StartProgram(true,[supTypeInfo]);
  30362. Add('{$modeswitch externalclass}');
  30363. Add('type');
  30364. Add(' TStaticArr = array[boolean] of string;');
  30365. Add(' TDynArr = array of string;');
  30366. Add(' TProc = procedure;');
  30367. Add(' TMethod = procedure of object;');
  30368. Add('var');
  30369. Add(' StaticArray: TStaticArr;');
  30370. Add(' tiStaticArray: TTypeInfoStaticArray;');
  30371. Add(' DynArray: TDynArr;');
  30372. Add(' tiDynArray: TTypeInfoDynArray;');
  30373. Add(' ProcVar: TProc;');
  30374. Add(' tiProcVar: TTypeInfoProcVar;');
  30375. Add(' MethodVar: TMethod;');
  30376. Add(' tiMethodVar: TTypeInfoMethodVar;');
  30377. Add('begin');
  30378. Add(' tiStaticArray:=typeinfo(StaticArray);');
  30379. Add(' tiStaticArray:=typeinfo(TStaticArr);');
  30380. Add(' tiDynArray:=typeinfo(DynArray);');
  30381. Add(' tiDynArray:=typeinfo(TDynArr);');
  30382. Add(' tiProcVar:=typeinfo(ProcVar);');
  30383. Add(' tiProcVar:=typeinfo(TProc);');
  30384. Add(' tiMethodVar:=typeinfo(MethodVar);');
  30385. Add(' tiMethodVar:=typeinfo(TMethod);');
  30386. ConvertProgram;
  30387. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses2',
  30388. LinesToStr([ // statements
  30389. 'this.$rtti.$StaticArray("TStaticArr", {',
  30390. ' dims: [2],',
  30391. ' eltype: rtl.string',
  30392. '});',
  30393. 'this.$rtti.$DynArray("TDynArr", {',
  30394. ' eltype: rtl.string',
  30395. '});',
  30396. 'this.$rtti.$ProcVar("TProc", {',
  30397. ' procsig: rtl.newTIProcSig(null)',
  30398. '});',
  30399. 'this.$rtti.$MethodVar("TMethod", {',
  30400. ' procsig: rtl.newTIProcSig(null),',
  30401. ' methodkind: 0',
  30402. '});',
  30403. 'this.StaticArray = rtl.arraySetLength(null,"",2);',
  30404. 'this.tiStaticArray = null;',
  30405. 'this.DynArray = [];',
  30406. 'this.tiDynArray = null;',
  30407. 'this.ProcVar = null;',
  30408. 'this.tiProcVar = null;',
  30409. 'this.MethodVar = null;',
  30410. 'this.tiMethodVar = null;',
  30411. '']),
  30412. LinesToStr([ // $mod.$main
  30413. '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
  30414. '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
  30415. '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
  30416. '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
  30417. '$mod.tiProcVar = $mod.$rtti["TProc"];',
  30418. '$mod.tiProcVar = $mod.$rtti["TProc"];',
  30419. '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
  30420. '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
  30421. '']));
  30422. end;
  30423. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses3;
  30424. begin
  30425. WithTypeInfo:=true;
  30426. StartProgram(true,[supTypeInfo]);
  30427. Add('{$modeswitch externalclass}');
  30428. Add('type');
  30429. Add(' TRec = record end;');
  30430. // ToDo: ^TRec
  30431. Add(' TObject = class end;');
  30432. Add(' TClass = class of tobject;');
  30433. Add('var');
  30434. Add(' Rec: trec;');
  30435. Add(' tiRecord: ttypeinforecord;');
  30436. Add(' Obj: tobject;');
  30437. Add(' tiClass: ttypeinfoclass;');
  30438. Add(' aClass: tclass;');
  30439. Add(' tiClassRef: ttypeinfoclassref;');
  30440. // ToDo: ^TRec
  30441. Add(' tiPointer: ttypeinfopointer;');
  30442. Add('begin');
  30443. Add(' tirecord:=typeinfo(trec);');
  30444. Add(' tirecord:=typeinfo(trec);');
  30445. Add(' ticlass:=typeinfo(obj);');
  30446. Add(' ticlass:=typeinfo(tobject);');
  30447. Add(' ticlass:=typeinfo(aclass);');
  30448. Add(' ticlassref:=typeinfo(tclass);');
  30449. ConvertProgram;
  30450. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses3',
  30451. LinesToStr([ // statements
  30452. 'rtl.recNewT(this, "TRec", function () {',
  30453. ' this.$eq = function (b) {',
  30454. ' return true;',
  30455. ' };',
  30456. ' this.$assign = function (s) {',
  30457. ' return this;',
  30458. ' };',
  30459. ' $mod.$rtti.$Record("TRec", {});',
  30460. '});',
  30461. 'rtl.createClass(this, "TObject", null, function () {',
  30462. ' this.$init = function () {',
  30463. ' };',
  30464. ' this.$final = function () {',
  30465. ' };',
  30466. '});',
  30467. 'this.$rtti.$ClassRef("TClass", {',
  30468. ' instancetype: this.$rtti["TObject"]',
  30469. '});',
  30470. 'this.Rec = this.TRec.$new();',
  30471. 'this.tiRecord = null;',
  30472. 'this.Obj = null;',
  30473. 'this.tiClass = null;',
  30474. 'this.aClass = null;',
  30475. 'this.tiClassRef = null;',
  30476. 'this.tiPointer = null;',
  30477. '']),
  30478. LinesToStr([ // $mod.$main
  30479. '$mod.tiRecord = $mod.$rtti["TRec"];',
  30480. '$mod.tiRecord = $mod.$rtti["TRec"];',
  30481. '$mod.tiClass = $mod.Obj.$rtti;',
  30482. '$mod.tiClass = $mod.$rtti["TObject"];',
  30483. '$mod.tiClass = $mod.aClass.$rtti;',
  30484. '$mod.tiClassRef = $mod.$rtti["TClass"];',
  30485. '']));
  30486. end;
  30487. procedure TTestModule.TestRTTI_TypeInfo_FunctionClassType;
  30488. begin
  30489. WithTypeInfo:=true;
  30490. StartProgram(true,[supTypeInfo]);
  30491. Add([
  30492. '{$modeswitch externalclass}',
  30493. 'type',
  30494. ' TClass = class of tobject;',
  30495. ' TObject = class',
  30496. ' function MyClass: TClass;',
  30497. ' class function ClassType: TClass;',
  30498. ' end;',
  30499. 'function TObject.MyClass: TClass;',
  30500. 'var t: TTypeInfoClass;',
  30501. 'begin',
  30502. ' t:=TypeInfo(Self);',
  30503. ' t:=TypeInfo(Result);',
  30504. ' t:=TypeInfo(TObject);',
  30505. 'end;',
  30506. 'class function TObject.ClassType: TClass;',
  30507. 'var t: TTypeInfoClass;',
  30508. 'begin',
  30509. ' t:=TypeInfo(Self);',
  30510. ' t:=TypeInfo(Result);',
  30511. 'end;',
  30512. 'var',
  30513. ' Obj: TObject;',
  30514. ' t: TTypeInfoClass;',
  30515. 'begin',
  30516. ' t:=TypeInfo(TObject.ClassType);',
  30517. ' t:=TypeInfo(Obj.ClassType);',
  30518. ' t:=TypeInfo(Obj.MyClass);',
  30519. '']);
  30520. ConvertProgram;
  30521. CheckSource('TestRTTI_TypeInfo_FunctionClassType',
  30522. LinesToStr([ // statements
  30523. 'this.$rtti.$Class("TObject");',
  30524. 'this.$rtti.$ClassRef("TClass", {',
  30525. ' instancetype: this.$rtti["TObject"]',
  30526. '});',
  30527. 'rtl.createClass(this, "TObject", null, function () {',
  30528. ' this.$init = function () {',
  30529. ' };',
  30530. ' this.$final = function () {',
  30531. ' };',
  30532. ' this.MyClass = function () {',
  30533. ' var Result = null;',
  30534. ' var t = null;',
  30535. ' t = this.$rtti;',
  30536. ' t = Result.$rtti;',
  30537. ' t = $mod.$rtti["TObject"];',
  30538. ' return Result;',
  30539. ' };',
  30540. ' this.ClassType = function () {',
  30541. ' var Result = null;',
  30542. ' var t = null;',
  30543. ' t = this.$rtti;',
  30544. ' t = Result.$rtti;',
  30545. ' return Result;',
  30546. ' };',
  30547. '});',
  30548. 'this.Obj = null;',
  30549. 'this.t = null;',
  30550. '']),
  30551. LinesToStr([ // $mod.$main
  30552. '$mod.t = $mod.TObject.ClassType().$rtti;',
  30553. '$mod.t = $mod.Obj.$class.ClassType().$rtti;',
  30554. '$mod.t = $mod.Obj.MyClass().$rtti;',
  30555. '']));
  30556. end;
  30557. procedure TTestModule.TestRTTI_TypeInfo_MixedUnits_PointerAndClass;
  30558. begin
  30559. WithTypeInfo:=true;
  30560. AddModuleWithIntfImplSrc('typinfo.pas',
  30561. LinesToStr([
  30562. '{$modeswitch externalclass}',
  30563. 'type',
  30564. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  30565. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo) end;',
  30566. '']),
  30567. '');
  30568. AddModuleWithIntfImplSrc('unit2.pas',
  30569. LinesToStr([
  30570. 'uses typinfo;',
  30571. 'type PTypeInfo = TTypeInfo;', // delphi compatibility code
  30572. 'procedure DoPtr(p: PTypeInfo);',
  30573. 'procedure DoInfo(t: TTypeInfo);',
  30574. 'procedure DoInt(t: TTypeInfoInteger);',
  30575. '']),
  30576. LinesToStr([
  30577. 'procedure DoPtr(p: PTypeInfo);',
  30578. 'begin end;',
  30579. 'procedure DoInfo(t: TTypeInfo);',
  30580. 'begin end;',
  30581. 'procedure DoInt(t: TTypeInfoInteger);',
  30582. 'begin end;',
  30583. '']));
  30584. StartUnit(true);
  30585. Add([
  30586. 'interface',
  30587. 'uses unit2;', // does not use unit typinfo
  30588. 'implementation',
  30589. 'var',
  30590. ' i: byte;',
  30591. ' p: pointer;',
  30592. ' t: PTypeInfo;',
  30593. 'initialization',
  30594. ' p:=typeinfo(i);',
  30595. ' t:=typeinfo(i);',
  30596. ' if p=t then ;',
  30597. ' if p=typeinfo(i) then ;',
  30598. ' if typeinfo(i)=p then ;',
  30599. ' if t=typeinfo(i) then ;',
  30600. ' if typeinfo(i)=t then ;',
  30601. ' DoPtr(p);',
  30602. ' DoPtr(t);',
  30603. ' DoPtr(typeinfo(i));',
  30604. ' DoInfo(p);',
  30605. ' DoInfo(t);',
  30606. ' DoInfo(typeinfo(i));',
  30607. ' DoInt(typeinfo(i));',
  30608. '']);
  30609. ConvertUnit;
  30610. CheckSource('TestRTTI_TypeInfo_MixedUnits_PointerAndClass',
  30611. LinesToStr([ // statements
  30612. 'var $impl = $mod.$impl;',
  30613. '']),
  30614. LinesToStr([ // this.$init
  30615. '$impl.p = rtl.byte;',
  30616. '$impl.t = rtl.byte;',
  30617. 'if ($impl.p === $impl.t) ;',
  30618. 'if ($impl.p === rtl.byte) ;',
  30619. 'if (rtl.byte === $impl.p) ;',
  30620. 'if ($impl.t === rtl.byte) ;',
  30621. 'if (rtl.byte === $impl.t) ;',
  30622. 'pas.unit2.DoPtr($impl.p);',
  30623. 'pas.unit2.DoPtr($impl.t);',
  30624. 'pas.unit2.DoPtr(rtl.byte);',
  30625. 'pas.unit2.DoInfo($impl.p);',
  30626. 'pas.unit2.DoInfo($impl.t);',
  30627. 'pas.unit2.DoInfo(rtl.byte);',
  30628. 'pas.unit2.DoInt(rtl.byte);',
  30629. '']),
  30630. LinesToStr([ // implementation
  30631. '$impl.i = 0;',
  30632. '$impl.p = null;',
  30633. '$impl.t = null;',
  30634. '']) );
  30635. end;
  30636. procedure TTestModule.TestRTTI_Interface_Corba;
  30637. begin
  30638. WithTypeInfo:=true;
  30639. StartProgram(true,[supTypeInfo]);
  30640. Add([
  30641. '{$interfaces corba}',
  30642. '{$modeswitch externalclass}',
  30643. 'type',
  30644. ' IUnknown = interface',
  30645. ' end;',
  30646. ' IBird = interface',
  30647. ' function GetItem: longint;',
  30648. ' procedure SetItem(Value: longint);',
  30649. ' property Item: longint read GetItem write SetItem;',
  30650. ' end;',
  30651. 'procedure DoIt(t: TTypeInfoInterface); begin end;',
  30652. 'var',
  30653. ' i: IBird;',
  30654. ' t: TTypeInfoInterface;',
  30655. 'begin',
  30656. ' t:=TypeInfo(IBird);',
  30657. ' t:=TypeInfo(i);',
  30658. ' DoIt(t);',
  30659. ' DoIt(TypeInfo(IBird));',
  30660. '']);
  30661. ConvertProgram;
  30662. CheckSource('TestRTTI_Interface_Corba',
  30663. LinesToStr([ // statements
  30664. 'rtl.createInterface(',
  30665. ' this,',
  30666. ' "IUnknown",',
  30667. ' "{B92D5841-758A-322B-B800-000000000000}",',
  30668. ' [],',
  30669. ' null,',
  30670. ' function () {',
  30671. ' }',
  30672. ');',
  30673. 'rtl.createInterface(',
  30674. ' this,',
  30675. ' "IBird",',
  30676. ' "{D32D5841-6264-3AE3-A2C9-B91CE922C9B9}",',
  30677. ' ["GetItem", "SetItem"],',
  30678. ' null,',
  30679. ' function () {',
  30680. ' var $r = this.$rtti;',
  30681. ' $r.addMethod("GetItem", 1, null, rtl.longint);',
  30682. ' $r.addMethod("SetItem", 0, [["Value", rtl.longint]]);',
  30683. ' $r.addProperty("Item", 3, rtl.longint, "GetItem", "SetItem");',
  30684. ' }',
  30685. ');',
  30686. 'this.DoIt = function (t) {',
  30687. '}; ',
  30688. 'this.i = null;',
  30689. 'this.t = null;',
  30690. '']),
  30691. LinesToStr([ // $mod.$main
  30692. '$mod.t = $mod.$rtti["IBird"];',
  30693. '$mod.t = $mod.i.$rtti;',
  30694. '$mod.DoIt($mod.t);',
  30695. '$mod.DoIt($mod.$rtti["IBird"]);',
  30696. '']));
  30697. end;
  30698. procedure TTestModule.TestRTTI_Interface_COM;
  30699. begin
  30700. WithTypeInfo:=true;
  30701. StartProgram(true,[supTypeInfo]);
  30702. Add([
  30703. '{$interfaces com}',
  30704. '{$modeswitch externalclass}',
  30705. 'type',
  30706. ' TGuid = record end;',
  30707. ' integer = longint;',
  30708. ' IUnknown = interface',
  30709. ' function QueryInterface(const iid: TGuid; out obj): Integer;',
  30710. ' function _AddRef: Integer;',
  30711. ' function _Release: Integer;',
  30712. ' end;',
  30713. ' IBird = interface',
  30714. ' function GetItem: longint;',
  30715. ' procedure SetItem(Value: longint);',
  30716. ' property Item: longint read GetItem write SetItem;',
  30717. ' end;',
  30718. 'var',
  30719. ' i: IBird;',
  30720. ' t: TTypeInfoInterface;',
  30721. 'begin',
  30722. ' t:=TypeInfo(IBird);',
  30723. ' t:=TypeInfo(i);',
  30724. '']);
  30725. ConvertProgram;
  30726. CheckSource('TestRTTI_Interface_COM',
  30727. LinesToStr([ // statements
  30728. 'rtl.recNewT(this, "TGuid", function () {',
  30729. ' this.$eq = function (b) {',
  30730. ' return true;',
  30731. ' };',
  30732. ' this.$assign = function (s) {',
  30733. ' return this;',
  30734. ' };',
  30735. ' $mod.$rtti.$Record("TGuid", {});',
  30736. '});',
  30737. 'rtl.createInterface(',
  30738. ' this,',
  30739. ' "IUnknown",',
  30740. ' "{D7ADB00D-1A9B-3EDC-B123-730E661DDFA9}",',
  30741. ' ["QueryInterface", "_AddRef", "_Release"],',
  30742. ' null,',
  30743. ' function () {',
  30744. ' this.$kind = "com";',
  30745. ' var $r = this.$rtti;',
  30746. ' $r.addMethod("QueryInterface", 1, [["iid", $mod.$rtti["TGuid"], 2], ["obj", null, 4]], rtl.longint);',
  30747. ' $r.addMethod("_AddRef", 1, null, rtl.longint);',
  30748. ' $r.addMethod("_Release", 1, null, rtl.longint);',
  30749. ' }',
  30750. ');',
  30751. 'rtl.createInterface(',
  30752. ' this,',
  30753. ' "IBird",',
  30754. ' "{9CC77572-0E45-3594-9A88-9E8D865C9E0A}",',
  30755. ' ["GetItem", "SetItem"],',
  30756. ' this.IUnknown,',
  30757. ' function () {',
  30758. ' var $r = this.$rtti;',
  30759. ' $r.addMethod("GetItem", 1, null, rtl.longint);',
  30760. ' $r.addMethod("SetItem", 0, [["Value", rtl.longint]]);',
  30761. ' $r.addProperty("Item", 3, rtl.longint, "GetItem", "SetItem");',
  30762. ' }',
  30763. ');',
  30764. 'this.i = null;',
  30765. 'this.t = null;',
  30766. '']),
  30767. LinesToStr([ // $mod.$main
  30768. '$mod.t = $mod.$rtti["IBird"];',
  30769. '$mod.t = $mod.i.$rtti;',
  30770. '']));
  30771. end;
  30772. procedure TTestModule.TestRTTI_ClassHelper;
  30773. begin
  30774. WithTypeInfo:=true;
  30775. StartProgram(true,[supTypeInfo]);
  30776. Add([
  30777. '{$interfaces com}',
  30778. '{$modeswitch externalclass}',
  30779. 'type',
  30780. ' TObject = class',
  30781. ' end;',
  30782. ' THelper = class helper for TObject',
  30783. ' published',
  30784. ' function GetItem: longint;',
  30785. ' property Item: longint read GetItem;',
  30786. ' end;',
  30787. 'function THelper.GetItem: longint;',
  30788. 'begin',
  30789. 'end;',
  30790. 'var',
  30791. ' t: TTypeInfoHelper;',
  30792. 'begin',
  30793. ' t:=TypeInfo(THelper);',
  30794. '']);
  30795. ConvertProgram;
  30796. CheckSource('TestRTTI_ClassHelper',
  30797. LinesToStr([ // statements
  30798. 'rtl.createClass(this, "TObject", null, function () {',
  30799. ' this.$init = function () {',
  30800. ' };',
  30801. ' this.$final = function () {',
  30802. ' };',
  30803. '});',
  30804. 'rtl.createHelper(this, "THelper", null, function () {',
  30805. ' this.GetItem = function () {',
  30806. ' var Result = 0;',
  30807. ' return Result;',
  30808. ' };',
  30809. ' var $r = this.$rtti;',
  30810. ' $r.addMethod("GetItem", 1, null, rtl.longint);',
  30811. ' $r.addProperty("Item", 1, rtl.longint, "GetItem", "");',
  30812. '});',
  30813. 'this.t = null;',
  30814. '']),
  30815. LinesToStr([ // $mod.$main
  30816. '$mod.t = $mod.$rtti["THelper"];',
  30817. '']));
  30818. end;
  30819. procedure TTestModule.TestRTTI_ExternalClass;
  30820. begin
  30821. WithTypeInfo:=true;
  30822. StartProgram(true,[supTypeInfo]);
  30823. Add([
  30824. '{$modeswitch externalclass}',
  30825. 'type',
  30826. ' TJSObject = class external name ''Object''',
  30827. ' end;',
  30828. ' TJSArray = class external name ''Array'' (TJSObject)',
  30829. ' end;',
  30830. 'var',
  30831. ' p: Pointer;',
  30832. ' tc: TTypeInfoExtClass;',
  30833. 'begin',
  30834. ' p:=typeinfo(TJSArray);']);
  30835. ConvertProgram;
  30836. CheckSource('TestRTTI_ExternalClass',
  30837. LinesToStr([ // statements
  30838. 'this.$rtti.$ExtClass("TJSObject", {',
  30839. ' jsclass: "Object"',
  30840. '});',
  30841. 'this.$rtti.$ExtClass("TJSArray", {',
  30842. ' ancestor: this.$rtti["TJSObject"],',
  30843. ' jsclass: "Array"',
  30844. '});',
  30845. 'this.p = null;',
  30846. 'this.tc = null;',
  30847. '']),
  30848. LinesToStr([ // $mod.$main
  30849. '$mod.p = $mod.$rtti["TJSArray"];',
  30850. '']));
  30851. end;
  30852. procedure TTestModule.TestRTTI_Unit;
  30853. begin
  30854. WithTypeInfo:=true;
  30855. AddModuleWithIntfImplSrc('unit2.pas',
  30856. LinesToStr([
  30857. '{$mode delphi}',
  30858. 'type',
  30859. ' TWordArray = array of word;',
  30860. ' TArray<T> = array of T;',
  30861. '']),
  30862. '');
  30863. StartUnit(true,[supTypeInfo,supTInterfacedObject]);
  30864. Add([
  30865. '{$mode delphi}',
  30866. 'interface',
  30867. 'uses unit2;',
  30868. 'type',
  30869. ' IBird = interface',
  30870. ' function Swoop: TWordArray;',
  30871. ' function Glide: TArray<word>;',
  30872. ' end;',
  30873. 'procedure Fly;',
  30874. 'implementation',
  30875. 'procedure Fly;',
  30876. 'var',
  30877. ' ta: tTypeInfoDynArray;',
  30878. ' ti: tTypeInfoInterface;',
  30879. 'begin',
  30880. ' ta:=typeinfo(TWordArray);',
  30881. ' ta:=typeinfo(TArray<word>);',
  30882. ' ti:=typeinfo(IBird);',
  30883. 'end;',
  30884. '']);
  30885. ConvertUnit;
  30886. CheckSource('TestRTTI_ExternalClass',
  30887. LinesToStr([ // statements
  30888. 'rtl.createInterface(',
  30889. ' this,',
  30890. ' "IBird",',
  30891. ' "{3B98AAAC-6116-3E17-AA85-F16786D85B09}",',
  30892. ' ["Swoop", "Glide"],',
  30893. ' pas.system.IUnknown,',
  30894. ' function () {',
  30895. ' var $r = this.$rtti;',
  30896. ' $r.addMethod("Swoop", 1, null, pas.unit2.$rtti["TWordArray"]);',
  30897. ' $r.addMethod("Glide", 1, null, pas.unit2.$rtti["TArray<System.Word>"]);',
  30898. ' }',
  30899. ');',
  30900. 'this.Fly = function () {',
  30901. ' var ta = null;',
  30902. ' var ti = null;',
  30903. ' ta = pas.unit2.$rtti["TWordArray"];',
  30904. ' ta = pas.unit2.$rtti["TArray<System.Word>"];',
  30905. ' ti = $mod.$rtti["IBird"];',
  30906. '};',
  30907. '']),
  30908. LinesToStr([ // $mod.$main
  30909. '']));
  30910. end;
  30911. procedure TTestModule.TestResourcestringProgram;
  30912. begin
  30913. AddModuleWithIntfImplSrc('unit2.pas',
  30914. LinesToStr([
  30915. 'resourcestring Title = ''Nice'';',
  30916. '']),
  30917. '');
  30918. StartProgram(true);
  30919. Add([
  30920. 'uses unit2;',
  30921. 'const Bar = ''bar'';',
  30922. 'resourcestring',
  30923. ' Red = ''red'';',
  30924. ' Foobar = ''fOo''+bar;',
  30925. 'var s: string;',
  30926. ' c: char;',
  30927. 'begin',
  30928. ' s:=red;',
  30929. ' s:=test1.red;',
  30930. ' s:=Title;',
  30931. ' c:=red[1];',
  30932. ' c:=test1.red[2];',
  30933. ' if red=foobar then ;',
  30934. ' if red[3]=red[4] then ;']);
  30935. ConvertProgram;
  30936. CheckSource('TestResourcestringProgram',
  30937. LinesToStr([ // statements
  30938. 'this.Bar = "bar";',
  30939. 'this.s = "";',
  30940. 'this.c = "";',
  30941. '$mod.$resourcestrings = {',
  30942. ' Red: {',
  30943. ' org: "red"',
  30944. ' },',
  30945. ' Foobar: {',
  30946. ' org: "fOobar"',
  30947. ' }',
  30948. '};',
  30949. '']),
  30950. LinesToStr([ // $mod.$main
  30951. '$mod.s = rtl.getResStr($mod, "Red");',
  30952. '$mod.s = rtl.getResStr($mod, "Red");',
  30953. '$mod.s = rtl.getResStr(pas.unit2, "Title");',
  30954. '$mod.c = rtl.getResStr($mod, "Red").charAt(0);',
  30955. '$mod.c = rtl.getResStr($mod, "Red").charAt(1);',
  30956. 'if (rtl.getResStr($mod, "Red") === rtl.getResStr($mod, "Foobar")) ;',
  30957. 'if (rtl.getResStr($mod, "Red").charAt(2) === rtl.getResStr($mod, "Red").charAt(3)) ;',
  30958. '']));
  30959. end;
  30960. procedure TTestModule.TestResourcestringUnit;
  30961. begin
  30962. AddModuleWithIntfImplSrc('unit2.pas',
  30963. LinesToStr([
  30964. 'resourcestring Title = ''Nice'';',
  30965. '']),
  30966. '');
  30967. StartUnit(true);
  30968. Add([
  30969. 'interface',
  30970. 'uses unit2;',
  30971. 'const Red = ''rEd'';',
  30972. 'resourcestring',
  30973. ' Blue = ''blue'';',
  30974. ' NotRed = ''not''+Red;',
  30975. 'var s: string;',
  30976. 'implementation',
  30977. 'resourcestring',
  30978. ' ImplGreen = ''green'';',
  30979. 'initialization',
  30980. ' s:=blue+ImplGreen;',
  30981. ' s:=test1.blue+test1.implgreen;',
  30982. ' s:=blue[1]+implgreen[2];',
  30983. ' s:=Title;',
  30984. '']);
  30985. ConvertUnit;
  30986. CheckSource('TestResourcestringUnit',
  30987. LinesToStr([ // statements
  30988. 'this.Red = "rEd";',
  30989. 'this.s = "";',
  30990. '$mod.$resourcestrings = {',
  30991. ' Blue: {',
  30992. ' org: "blue"',
  30993. ' },',
  30994. ' NotRed: {',
  30995. ' org: "notrEd"',
  30996. ' },',
  30997. ' ImplGreen: {',
  30998. ' org: "green"',
  30999. ' }',
  31000. '};',
  31001. '']),
  31002. LinesToStr([ // $mod.$main
  31003. '$mod.s = rtl.getResStr($mod, "Blue") + rtl.getResStr($mod, "ImplGreen");',
  31004. '$mod.s = rtl.getResStr($mod, "Blue") + rtl.getResStr($mod, "ImplGreen");',
  31005. '$mod.s = rtl.getResStr($mod, "Blue").charAt(0) + rtl.getResStr($mod, "ImplGreen").charAt(1);',
  31006. '$mod.s = rtl.getResStr(pas.unit2, "Title");',
  31007. '']));
  31008. end;
  31009. procedure TTestModule.TestResourcestringImplementation;
  31010. begin
  31011. StartUnit(false);
  31012. Add([
  31013. 'interface',
  31014. 'implementation',
  31015. 'resourcestring',
  31016. ' ImplRed = ''red'';']);
  31017. ConvertUnit;
  31018. CheckSource('TestResourcestringImplementation',
  31019. LinesToStr([ // intf statements
  31020. 'var $impl = $mod.$impl;']),
  31021. LinesToStr([ // $mod.$init
  31022. '']),
  31023. LinesToStr([ // impl statements
  31024. '$mod.$resourcestrings = {',
  31025. ' ImplRed: {',
  31026. ' org: "red"',
  31027. ' }',
  31028. '};',
  31029. '']));
  31030. end;
  31031. procedure TTestModule.TestAttributes_Members;
  31032. begin
  31033. WithTypeInfo:=true;
  31034. StartProgram(false);
  31035. Add([
  31036. '{$modeswitch PrefixedAttributes}',
  31037. 'type',
  31038. ' TObject = class',
  31039. ' constructor Create;',
  31040. ' end;',
  31041. ' TCustomAttribute = class',
  31042. ' constructor Create(Id: word);',
  31043. ' end;',
  31044. ' [Missing]',
  31045. ' TBird = class',
  31046. ' published',
  31047. ' [Tcustom]',
  31048. ' FField: word;',
  31049. ' [tcustom(14)]',
  31050. ' property Size: word read FField;',
  31051. ' [Tcustom(15)]',
  31052. ' procedure Fly; virtual; abstract;',
  31053. ' end;',
  31054. ' TRec = record',
  31055. ' [Tcustom,tcustom(14)]',
  31056. ' Size: word;',
  31057. ' end;',
  31058. 'constructor TObject.Create; begin end;',
  31059. 'constructor TCustomAttribute.Create(Id: word); begin end;',
  31060. 'begin',
  31061. '']);
  31062. ConvertProgram;
  31063. CheckSource('TestAttributes_Members',
  31064. LinesToStr([ // statements
  31065. 'rtl.createClass(this, "TObject", null, function () {',
  31066. ' this.$init = function () {',
  31067. ' };',
  31068. ' this.$final = function () {',
  31069. ' };',
  31070. ' this.Create = function () {',
  31071. ' return this;',
  31072. ' };',
  31073. '});',
  31074. 'rtl.createClass(this, "TCustomAttribute", this.TObject, function () {',
  31075. ' this.Create$1 = function (Id) {',
  31076. ' return this;',
  31077. ' };',
  31078. '});',
  31079. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  31080. ' this.$init = function () {',
  31081. ' $mod.TObject.$init.call(this);',
  31082. ' this.FField = 0;',
  31083. ' };',
  31084. ' var $r = this.$rtti;',
  31085. ' $r.addField("FField", rtl.word, {',
  31086. ' attr: [$mod.TCustomAttribute, "Create"]',
  31087. ' });',
  31088. ' $r.addProperty(',
  31089. ' "Size",',
  31090. ' 0,',
  31091. ' rtl.word,',
  31092. ' "FField",',
  31093. ' "",',
  31094. ' {',
  31095. ' attr: [$mod.TCustomAttribute, "Create$1", [14]]',
  31096. ' }',
  31097. ' );',
  31098. ' $r.addMethod("Fly", 0, null, null, {',
  31099. ' attr: [$mod.TCustomAttribute, "Create$1", [15]]',
  31100. ' });',
  31101. '});',
  31102. 'rtl.recNewT(this, "TRec", function () {',
  31103. ' this.Size = 0;',
  31104. ' this.$eq = function (b) {',
  31105. ' return this.Size === b.Size;',
  31106. ' };',
  31107. ' this.$assign = function (s) {',
  31108. ' this.Size = s.Size;',
  31109. ' return this;',
  31110. ' };',
  31111. ' var $r = $mod.$rtti.$Record("TRec", {});',
  31112. ' $r.addField("Size", rtl.word, {',
  31113. ' attr: [',
  31114. ' $mod.TCustomAttribute,',
  31115. ' "Create",',
  31116. ' $mod.TCustomAttribute,',
  31117. ' "Create$1",',
  31118. ' [14]',
  31119. ' ]',
  31120. ' });',
  31121. '});',
  31122. '']),
  31123. LinesToStr([ // $mod.$main
  31124. '']));
  31125. end;
  31126. procedure TTestModule.TestAttributes_Types;
  31127. begin
  31128. WithTypeInfo:=true;
  31129. StartProgram(false);
  31130. Add([
  31131. '{$modeswitch PrefixedAttributes}',
  31132. 'type',
  31133. ' TObject = class',
  31134. ' constructor Create(Id: word);',
  31135. ' end;',
  31136. ' TCustomAttribute = class',
  31137. ' end;',
  31138. ' [TCustom(1)]',
  31139. ' TMyClass = class',
  31140. ' end;',
  31141. ' [TCustom(11)]',
  31142. ' TMyDescendant = class(TMyClass)',
  31143. ' end;',
  31144. ' [TCustom(2)]',
  31145. ' TRec = record',
  31146. ' end;',
  31147. ' [TCustom(3)]',
  31148. ' TInt = type word;',
  31149. 'constructor TObject.Create(Id: word);',
  31150. 'begin',
  31151. 'end;',
  31152. 'var p: pointer;',
  31153. 'begin',
  31154. ' p:=typeinfo(TMyClass);',
  31155. ' p:=typeinfo(TRec);',
  31156. ' p:=typeinfo(TInt);',
  31157. '']);
  31158. ConvertProgram;
  31159. CheckSource('TestAttributes_Types',
  31160. LinesToStr([ // statements
  31161. 'rtl.createClass(this, "TObject", null, function () {',
  31162. ' this.$init = function () {',
  31163. ' };',
  31164. ' this.$final = function () {',
  31165. ' };',
  31166. ' this.Create = function (Id) {',
  31167. ' return this;',
  31168. ' };',
  31169. '});',
  31170. 'rtl.createClass(this, "TCustomAttribute", this.TObject, function () {',
  31171. '});',
  31172. 'rtl.createClass(this, "TMyClass", this.TObject, function () {',
  31173. ' var $r = this.$rtti;',
  31174. ' $r.attr = [$mod.TCustomAttribute, "Create", [1]];',
  31175. '});',
  31176. 'rtl.createClass(this, "TMyDescendant", this.TMyClass, function () {',
  31177. ' var $r = this.$rtti;',
  31178. ' $r.attr = [$mod.TCustomAttribute, "Create", [11]];',
  31179. '});',
  31180. 'rtl.recNewT(this, "TRec", function () {',
  31181. ' this.$eq = function (b) {',
  31182. ' return true;',
  31183. ' };',
  31184. ' this.$assign = function (s) {',
  31185. ' return this;',
  31186. ' };',
  31187. ' $mod.$rtti.$Record("TRec", {',
  31188. ' attr: [$mod.TCustomAttribute, "Create", [2]]',
  31189. ' });',
  31190. '});',
  31191. 'this.$rtti.$inherited("TInt", rtl.word, {',
  31192. ' attr: [this.TCustomAttribute, "Create", [3]]',
  31193. '});',
  31194. 'this.p = null;',
  31195. '']),
  31196. LinesToStr([ // $mod.$main
  31197. '$mod.p = $mod.$rtti["TMyClass"];',
  31198. '$mod.p = $mod.$rtti["TRec"];',
  31199. '$mod.p = $mod.$rtti["TInt"];',
  31200. '']));
  31201. end;
  31202. procedure TTestModule.TestAttributes_HelperConstructor_Fail;
  31203. begin
  31204. WithTypeInfo:=true;
  31205. StartProgram(false);
  31206. Add([
  31207. '{$modeswitch PrefixedAttributes}',
  31208. 'type',
  31209. ' TObject = class',
  31210. ' constructor Create;',
  31211. ' end;',
  31212. ' TCustomAttribute = class',
  31213. ' end;',
  31214. ' THelper = class helper for TCustomAttribute',
  31215. ' constructor Create(Id: word);',
  31216. ' end;',
  31217. ' [TCustom(3)]',
  31218. ' TMyInt = word;',
  31219. 'constructor TObject.Create; begin end;',
  31220. 'constructor THelper.Create(Id: word); begin end;',
  31221. 'begin',
  31222. ' if typeinfo(TMyInt)=nil then ;']);
  31223. ConvertProgram;
  31224. end;
  31225. procedure TTestModule.TestAssert;
  31226. begin
  31227. StartProgram(false);
  31228. Add([
  31229. 'procedure DoIt;',
  31230. 'var',
  31231. ' b: boolean;',
  31232. ' s: string;',
  31233. 'begin',
  31234. ' {$Assertions on}',
  31235. ' Assert(b);',
  31236. 'end;',
  31237. 'begin',
  31238. ' DoIt;',
  31239. '']);
  31240. ConvertProgram;
  31241. CheckSource('TestAssert',
  31242. LinesToStr([ // statements
  31243. 'this.DoIt = function () {',
  31244. ' var b = false;',
  31245. ' var s = "";',
  31246. ' if (!b) throw "assert failed";',
  31247. '};',
  31248. '']),
  31249. LinesToStr([ // $mod.$main
  31250. '$mod.DoIt();',
  31251. '']));
  31252. end;
  31253. procedure TTestModule.TestAssert_SysUtils;
  31254. begin
  31255. AddModuleWithIntfImplSrc('SysUtils.pas',
  31256. LinesToStr([
  31257. 'type',
  31258. ' TObject = class',
  31259. ' constructor Create;',
  31260. ' end;',
  31261. ' EAssertionFailed = class',
  31262. ' constructor Create(s: string);',
  31263. ' end;',
  31264. '']),
  31265. LinesToStr([
  31266. 'constructor TObject.Create;',
  31267. 'begin end;',
  31268. 'constructor EAssertionFailed.Create(s: string);',
  31269. 'begin end;',
  31270. '']) );
  31271. StartProgram(true);
  31272. Add([
  31273. 'uses sysutils;',
  31274. 'procedure DoIt;',
  31275. 'var',
  31276. ' b: boolean;',
  31277. ' s: string;',
  31278. 'begin',
  31279. ' {$Assertions on}',
  31280. ' Assert(b);',
  31281. ' Assert(b,''msg'');',
  31282. 'end;',
  31283. 'begin',
  31284. ' DoIt;',
  31285. '']);
  31286. ConvertProgram;
  31287. CheckSource('TestAssert_SysUtils',
  31288. LinesToStr([ // statements
  31289. 'this.DoIt = function () {',
  31290. ' var b = false;',
  31291. ' var s = "";',
  31292. ' if (!b) throw pas.SysUtils.EAssertionFailed.$create("Create");',
  31293. ' if (!b) throw pas.SysUtils.EAssertionFailed.$create("Create$1", ["msg"]);',
  31294. '};',
  31295. '']),
  31296. LinesToStr([ // $mod.$main
  31297. '$mod.DoIt();',
  31298. '']));
  31299. end;
  31300. procedure TTestModule.TestObjectChecks;
  31301. begin
  31302. Scanner.CurrentBoolSwitches:=Scanner.CurrentBoolSwitches+[bsObjectChecks];
  31303. StartProgram(false);
  31304. Add([
  31305. 'type',
  31306. ' TObject = class',
  31307. ' procedure DoIt;',
  31308. ' end;',
  31309. ' TClass = class of tobject;',
  31310. ' TBird = class',
  31311. ' end;',
  31312. ' TBirdClass = class of TBird;',
  31313. 'var',
  31314. ' o : TObject;',
  31315. ' c: TClass;',
  31316. ' b: TBird;',
  31317. ' bc: TBirdClass;',
  31318. 'procedure TObject.DoIt;',
  31319. 'begin',
  31320. ' b:=TBird(o);',
  31321. 'end;',
  31322. 'begin',
  31323. ' o.DoIt;',
  31324. ' b:=TBird(o);',
  31325. ' bc:=TBirdClass(c);',
  31326. '']);
  31327. ConvertProgram;
  31328. CheckSource('TestCheckMethodCall',
  31329. LinesToStr([ // statements
  31330. 'rtl.createClass(this, "TObject", null, function () {',
  31331. ' this.$init = function () {',
  31332. ' };',
  31333. ' this.$final = function () {',
  31334. ' };',
  31335. ' this.DoIt = function () {',
  31336. ' rtl.checkMethodCall(this,$mod.TObject);',
  31337. ' $mod.b = rtl.asExt($mod.o, $mod.TBird, 1);',
  31338. ' };',
  31339. '});',
  31340. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  31341. '});',
  31342. 'this.o = null;',
  31343. 'this.c = null;',
  31344. 'this.b = null;',
  31345. 'this.bc = null;',
  31346. '']),
  31347. LinesToStr([ // $mod.$main
  31348. '$mod.o.DoIt();',
  31349. '$mod.b = rtl.asExt($mod.o,$mod.TBird, 1);',
  31350. '$mod.bc = rtl.asExt($mod.c, $mod.TBird, 2);',
  31351. '']));
  31352. end;
  31353. procedure TTestModule.TestOverflowChecks_Int;
  31354. begin
  31355. Scanner.CurrentBoolSwitches:=Scanner.CurrentBoolSwitches+[bsOverflowChecks];
  31356. StartProgram(false);
  31357. Add([
  31358. 'procedure DoIt;',
  31359. 'var',
  31360. ' b: byte;',
  31361. ' n: nativeint;',
  31362. ' u: nativeuint;',
  31363. ' c: currency;',
  31364. 'begin',
  31365. ' n:=n+n;',
  31366. ' n:=n-n;',
  31367. ' n:=n+b;',
  31368. ' n:=b-n;',
  31369. ' n:=n*n;',
  31370. ' n:=n*u;',
  31371. ' c:=c+b;',
  31372. ' c:=b+c;',
  31373. ' c:=c*b;',
  31374. ' c:=b*c;',
  31375. 'end;',
  31376. 'begin',
  31377. '']);
  31378. ConvertProgram;
  31379. CheckSource('TestOverflowChecks_Int',
  31380. LinesToStr([ // statements
  31381. 'this.DoIt = function () {',
  31382. ' var b = 0;',
  31383. ' var n = 0;',
  31384. ' var u = 0;',
  31385. ' var c = 0;',
  31386. ' n = rtl.oc(n + n);',
  31387. ' n = rtl.oc(n - n);',
  31388. ' n = rtl.oc(n + b);',
  31389. ' n = rtl.oc(b - n);',
  31390. ' n = rtl.oc(n * n);',
  31391. ' n = rtl.oc(n * u);',
  31392. ' c = rtl.oc(c + (b * 10000));',
  31393. ' c = rtl.oc((b * 10000) + c);',
  31394. ' c = rtl.oc(c * b);',
  31395. ' c = rtl.oc(b * c);',
  31396. '};',
  31397. '']),
  31398. LinesToStr([ // $mod.$main
  31399. '']));
  31400. end;
  31401. procedure TTestModule.TestRangeChecks_AssignInt;
  31402. begin
  31403. Scanner.Options:=Scanner.Options+[po_CAssignments];
  31404. StartProgram(false);
  31405. Add([
  31406. '{$R+}',
  31407. 'var',
  31408. ' b: byte = 2;',
  31409. ' w: word = 3;',
  31410. 'procedure DoIt(p: byte);',
  31411. 'begin',
  31412. ' b:=w;',
  31413. ' b+=w;',
  31414. ' b:=1;',
  31415. 'end;',
  31416. '{$R-}',
  31417. 'procedure DoSome;',
  31418. 'begin',
  31419. ' DoIt(w);',
  31420. ' b:=w;',
  31421. ' b:=2;',
  31422. 'end;',
  31423. 'begin',
  31424. '{$R+}',
  31425. '']);
  31426. ConvertProgram;
  31427. CheckSource('TestRangeChecks_AssignInt',
  31428. LinesToStr([ // statements
  31429. 'this.b = 2;',
  31430. 'this.w = 3;',
  31431. 'this.DoIt = function (p) {',
  31432. ' rtl.rc(p, 0, 255);',
  31433. ' $mod.b = rtl.rc($mod.w,0,255);',
  31434. ' rtl.rc($mod.b += $mod.w, 0, 255);',
  31435. ' $mod.b = 1;',
  31436. '};',
  31437. 'this.DoSome = function () {',
  31438. ' $mod.DoIt($mod.w);',
  31439. ' $mod.b = $mod.w;',
  31440. ' $mod.b = 2;',
  31441. '};',
  31442. '']),
  31443. LinesToStr([ // $mod.$main
  31444. '']));
  31445. end;
  31446. procedure TTestModule.TestRangeChecks_AssignIntRange;
  31447. begin
  31448. Scanner.Options:=Scanner.Options+[po_CAssignments];
  31449. StartProgram(false);
  31450. Add([
  31451. '{$R+}',
  31452. 'type Ten = 1..10;',
  31453. 'var',
  31454. ' b: Ten = 2;',
  31455. ' w: Ten = 3;',
  31456. 'procedure DoIt(p: Ten);',
  31457. 'begin',
  31458. ' b:=w;',
  31459. ' b+=w;',
  31460. ' b:=1;',
  31461. 'end;',
  31462. '{$R-}',
  31463. 'procedure DoSome;',
  31464. 'begin',
  31465. ' DoIt(w);',
  31466. ' b:=w;',
  31467. ' b:=2;',
  31468. 'end;',
  31469. 'begin',
  31470. '{$R+}',
  31471. '']);
  31472. ConvertProgram;
  31473. CheckSource('TestRangeChecks_AssignIntRange',
  31474. LinesToStr([ // statements
  31475. 'this.b = 2;',
  31476. 'this.w = 3;',
  31477. 'this.DoIt = function (p) {',
  31478. ' rtl.rc(p, 1, 10);',
  31479. ' $mod.b = rtl.rc($mod.w, 1, 10);',
  31480. ' rtl.rc($mod.b += $mod.w, 1, 10);',
  31481. ' $mod.b = 1;',
  31482. '};',
  31483. 'this.DoSome = function () {',
  31484. ' $mod.DoIt($mod.w);',
  31485. ' $mod.b = $mod.w;',
  31486. ' $mod.b = 2;',
  31487. '};',
  31488. '']),
  31489. LinesToStr([ // $mod.$main
  31490. '']));
  31491. end;
  31492. procedure TTestModule.TestRangeChecks_AssignEnum;
  31493. begin
  31494. StartProgram(false);
  31495. Add([
  31496. '{$R+}',
  31497. 'type TEnum = (red,green);',
  31498. 'var',
  31499. ' e: TEnum = red;',
  31500. 'procedure DoIt(p: TEnum);',
  31501. 'begin',
  31502. ' e:=p;',
  31503. ' p:=TEnum(0);',
  31504. ' p:=succ(e);',
  31505. 'end;',
  31506. '{$R-}',
  31507. 'procedure DoSome;',
  31508. 'begin',
  31509. ' DoIt(e);',
  31510. ' e:=TEnum(1);',
  31511. ' e:=pred(e);',
  31512. 'end;',
  31513. 'begin',
  31514. '{$R+}',
  31515. '']);
  31516. ConvertProgram;
  31517. CheckSource('TestRangeChecks_AssignEnum',
  31518. LinesToStr([ // statements
  31519. 'this.TEnum = {',
  31520. ' "0": "red",',
  31521. ' red: 0,',
  31522. ' "1": "green",',
  31523. ' green: 1',
  31524. '};',
  31525. 'this.e = this.TEnum.red;',
  31526. 'this.DoIt = function (p) {',
  31527. ' rtl.rc(p, 0, 1);',
  31528. ' $mod.e = rtl.rc(p, 0, 1);',
  31529. ' p = 0;',
  31530. ' p = rtl.rc($mod.e + 1, 0, 1);',
  31531. '};',
  31532. 'this.DoSome = function () {',
  31533. ' $mod.DoIt($mod.e);',
  31534. ' $mod.e = 1;',
  31535. ' $mod.e = $mod.e - 1;',
  31536. '};',
  31537. '']),
  31538. LinesToStr([ // $mod.$main
  31539. '']));
  31540. end;
  31541. procedure TTestModule.TestRangeChecks_AssignEnumRange;
  31542. begin
  31543. StartProgram(false);
  31544. Add([
  31545. '{$R+}',
  31546. 'type',
  31547. ' TEnum = (red,green);',
  31548. ' TEnumRg = red..green;',
  31549. 'var',
  31550. ' e: TEnumRg = red;',
  31551. 'procedure DoIt(p: TEnumRg);',
  31552. 'begin',
  31553. ' e:=p;',
  31554. ' p:=TEnumRg(0);',
  31555. ' p:=succ(e);',
  31556. 'end;',
  31557. '{$R-}',
  31558. 'procedure DoSome;',
  31559. 'begin',
  31560. ' DoIt(e);',
  31561. ' e:=TEnum(1);',
  31562. ' e:=pred(e);',
  31563. 'end;',
  31564. 'begin',
  31565. '{$R+}',
  31566. '']);
  31567. ConvertProgram;
  31568. CheckSource('TestRangeChecks_AssignEnumRange',
  31569. LinesToStr([ // statements
  31570. 'this.TEnum = {',
  31571. ' "0": "red",',
  31572. ' red: 0,',
  31573. ' "1": "green",',
  31574. ' green: 1',
  31575. '};',
  31576. 'this.e = this.TEnum.red;',
  31577. 'this.DoIt = function (p) {',
  31578. ' rtl.rc(p, 0, 1);',
  31579. ' $mod.e = rtl.rc(p, 0, 1);',
  31580. ' p = 0;',
  31581. ' p = rtl.rc($mod.e + 1, 0, 1);',
  31582. '};',
  31583. 'this.DoSome = function () {',
  31584. ' $mod.DoIt($mod.e);',
  31585. ' $mod.e = 1;',
  31586. ' $mod.e = $mod.e - 1;',
  31587. '};',
  31588. '']),
  31589. LinesToStr([ // $mod.$main
  31590. '']));
  31591. end;
  31592. procedure TTestModule.TestRangeChecks_AssignChar;
  31593. begin
  31594. StartProgram(false);
  31595. Add([
  31596. '{$R+}',
  31597. 'type',
  31598. ' TLetter = char;',
  31599. 'var',
  31600. ' b: TLetter = ''2'';',
  31601. ' w: TLetter = ''3'';',
  31602. 'procedure DoIt(p: TLetter);',
  31603. 'begin',
  31604. ' b:=w;',
  31605. ' b:=''1'';',
  31606. 'end;',
  31607. '{$R-}',
  31608. 'procedure DoSome;',
  31609. 'begin',
  31610. ' DoIt(w);',
  31611. ' b:=w;',
  31612. ' b:=''2'';',
  31613. 'end;',
  31614. 'begin',
  31615. '{$R+}',
  31616. '']);
  31617. ConvertProgram;
  31618. CheckSource('TestRangeChecks_AssignChar',
  31619. LinesToStr([ // statements
  31620. 'this.b = "2";',
  31621. 'this.w = "3";',
  31622. 'this.DoIt = function (p) {',
  31623. ' rtl.rcc(p, 0, 65535);',
  31624. ' $mod.b = rtl.rcc($mod.w, 0, 65535);',
  31625. ' $mod.b = "1";',
  31626. '};',
  31627. 'this.DoSome = function () {',
  31628. ' $mod.DoIt($mod.w);',
  31629. ' $mod.b = $mod.w;',
  31630. ' $mod.b = "2";',
  31631. '};',
  31632. '']),
  31633. LinesToStr([ // $mod.$main
  31634. '']));
  31635. end;
  31636. procedure TTestModule.TestRangeChecks_AssignCharRange;
  31637. begin
  31638. StartProgram(false);
  31639. Add([
  31640. '{$R+}',
  31641. 'type TDigit = ''0''..''9'';',
  31642. 'var',
  31643. ' b: TDigit = ''2'';',
  31644. ' w: TDigit = ''3'';',
  31645. 'procedure DoIt(p: TDigit);',
  31646. 'begin',
  31647. ' b:=w;',
  31648. ' b:=''1'';',
  31649. 'end;',
  31650. '{$R-}',
  31651. 'procedure DoSome;',
  31652. 'begin',
  31653. ' DoIt(w);',
  31654. ' b:=w;',
  31655. ' b:=''2'';',
  31656. 'end;',
  31657. 'begin',
  31658. '{$R+}',
  31659. '']);
  31660. ConvertProgram;
  31661. CheckSource('TestRangeChecks_AssignCharRange',
  31662. LinesToStr([ // statements
  31663. 'this.b = "2";',
  31664. 'this.w = "3";',
  31665. 'this.DoIt = function (p) {',
  31666. ' rtl.rcc(p, 48, 57);',
  31667. ' $mod.b = rtl.rcc($mod.w, 48, 57);',
  31668. ' $mod.b = "1";',
  31669. '};',
  31670. 'this.DoSome = function () {',
  31671. ' $mod.DoIt($mod.w);',
  31672. ' $mod.b = $mod.w;',
  31673. ' $mod.b = "2";',
  31674. '};',
  31675. '']),
  31676. LinesToStr([ // $mod.$main
  31677. '']));
  31678. end;
  31679. procedure TTestModule.TestRangeChecks_ArrayIndex;
  31680. begin
  31681. StartProgram(false);
  31682. Add([
  31683. '{$R+}',
  31684. 'type',
  31685. ' Ten = 1..10;',
  31686. ' TArr = array of Ten;',
  31687. ' TArrArr = array of TArr;',
  31688. ' TArrByte = array[byte] of Ten;',
  31689. ' TArrChar = array[''0''..''9''] of Ten;',
  31690. ' TArrByteChar = array[byte,''0''..''9''] of Ten;',
  31691. ' TObject = class',
  31692. ' A: TArr;',
  31693. ' end;',
  31694. 'procedure DoIt;',
  31695. 'var',
  31696. ' Arr: TArr;',
  31697. ' ArrArr: TArrArr;',
  31698. ' ArrByte: TArrByte;',
  31699. ' ArrChar: TArrChar;',
  31700. ' ArrByteChar: TArrByteChar;',
  31701. ' i: Ten;',
  31702. ' c: char;',
  31703. ' o: tobject;',
  31704. 'begin',
  31705. ' i:=Arr[1];',
  31706. ' i:=ArrByteChar[1,''2''];',
  31707. ' Arr[1]:=Arr[1];',
  31708. ' Arr[i]:=Arr[i];',
  31709. ' ArrByte[3]:=ArrByte[3];',
  31710. ' ArrByte[i]:=ArrByte[i];',
  31711. ' ArrChar[''5'']:=ArrChar[''5''];',
  31712. ' ArrChar[c]:=ArrChar[c];',
  31713. ' ArrByteChar[7,''7'']:=ArrByteChar[7,''7''];',
  31714. ' ArrByteChar[i,c]:=ArrByteChar[i,c];',
  31715. ' o.a[i]:=o.a[i];',
  31716. 'end;',
  31717. 'begin',
  31718. '']);
  31719. ConvertProgram;
  31720. CheckSource('TestRangeChecks_ArrayIndex',
  31721. LinesToStr([ // statements
  31722. 'rtl.createClass(this, "TObject", null, function () {',
  31723. ' this.$init = function () {',
  31724. ' this.A = [];',
  31725. ' };',
  31726. ' this.$final = function () {',
  31727. ' this.A = undefined;',
  31728. ' };',
  31729. '});',
  31730. 'this.DoIt = function () {',
  31731. ' var Arr = [];',
  31732. ' var ArrArr = [];',
  31733. ' var ArrByte = rtl.arraySetLength(null, 0, 256);',
  31734. ' var ArrChar = rtl.arraySetLength(null, 0, 10);',
  31735. ' var ArrByteChar = rtl.arraySetLength(null, 0, 256, 10);',
  31736. ' var i = 0;',
  31737. ' var c = "";',
  31738. ' var o = null;',
  31739. ' i = rtl.rc(Arr[1], 1, 10);',
  31740. ' i = rtl.rc(ArrByteChar[1][2], 1, 10);',
  31741. ' Arr[1] = rtl.rc(Arr[1], 1, 10);',
  31742. ' rtl.rcArrW(Arr, i, rtl.rcArrR(Arr, i));',
  31743. ' ArrByte[3] = rtl.rc(ArrByte[3], 1, 10);',
  31744. ' rtl.rcArrW(ArrByte, i, rtl.rcArrR(ArrByte, i));',
  31745. ' ArrChar[5] = rtl.rc(ArrChar[5], 1, 10);',
  31746. ' rtl.rcArrW(ArrChar, c.charCodeAt() - 48, rtl.rcArrR(ArrChar, c.charCodeAt() - 48));',
  31747. ' ArrByteChar[7][7] = rtl.rc(ArrByteChar[7][7], 1, 10);',
  31748. ' rtl.rcArrW(ArrByteChar, i, c.charCodeAt() - 48, rtl.rcArrR(ArrByteChar, i, c.charCodeAt() - 48));',
  31749. ' rtl.rcArrW(o.A, i, rtl.rcArrR(o.A, i));',
  31750. '};',
  31751. '']),
  31752. LinesToStr([ // $mod.$main
  31753. '']));
  31754. end;
  31755. procedure TTestModule.TestRangeChecks_ArrayOfRecIndex;
  31756. begin
  31757. StartProgram(false);
  31758. Add([
  31759. '{$R+}',
  31760. 'type',
  31761. ' Ten = 1..10;',
  31762. ' TRec = record x: Ten end;',
  31763. ' TArr = array of TRec;',
  31764. ' TArrArr = array of TArr;',
  31765. ' TObject = class',
  31766. ' A: TArr;',
  31767. ' end;',
  31768. 'procedure DoIt;',
  31769. 'var',
  31770. ' Arr: TArr;',
  31771. ' ArrArr: TArrArr;',
  31772. ' i: Ten;',
  31773. ' o: tobject;',
  31774. 'begin',
  31775. ' Arr[1]:=Arr[1];',
  31776. ' Arr[i]:=Arr[i+1];',
  31777. ' o.a[i]:=o.a[i+2];',
  31778. 'end;',
  31779. 'begin',
  31780. '']);
  31781. ConvertProgram;
  31782. CheckSource('TestRangeChecks_ArrayOfRecIndex',
  31783. LinesToStr([ // statements
  31784. 'rtl.recNewT(this, "TRec", function () {',
  31785. ' this.x = 0;',
  31786. ' this.$eq = function (b) {',
  31787. ' return this.x === b.x;',
  31788. ' };',
  31789. ' this.$assign = function (s) {',
  31790. ' this.x = s.x;',
  31791. ' return this;',
  31792. ' };',
  31793. '});',
  31794. 'rtl.createClass(this, "TObject", null, function () {',
  31795. ' this.$init = function () {',
  31796. ' this.A = [];',
  31797. ' };',
  31798. ' this.$final = function () {',
  31799. ' this.A = undefined;',
  31800. ' };',
  31801. '});',
  31802. 'this.DoIt = function () {',
  31803. ' var Arr = [];',
  31804. ' var ArrArr = [];',
  31805. ' var i = 0;',
  31806. ' var o = null;',
  31807. ' Arr[1].$assign(Arr[1]);',
  31808. ' rtl.rcArrR(Arr, i).$assign(rtl.rcArrR(Arr, i + 1));',
  31809. ' rtl.rcArrR(o.A, i).$assign(rtl.rcArrR(o.A, i + 2));',
  31810. '};',
  31811. '']),
  31812. LinesToStr([ // $mod.$main
  31813. '']));
  31814. end;
  31815. procedure TTestModule.TestRangeChecks_StringIndex;
  31816. begin
  31817. StartProgram(false);
  31818. Add([
  31819. 'type',
  31820. ' TObject = class',
  31821. ' S: string;',
  31822. ' end;',
  31823. '{$R+}',
  31824. 'procedure DoIt(var h: string);',
  31825. 'var',
  31826. ' s: string;',
  31827. ' i: longint;',
  31828. ' c: char;',
  31829. ' o: tobject;',
  31830. 'begin',
  31831. ' c:=s[1];',
  31832. ' s[i]:=s[i];',
  31833. ' h[i]:=h[i];',
  31834. ' c:=o.s[i];',
  31835. ' o.s[i]:=c;',
  31836. 'end;',
  31837. 'begin',
  31838. '']);
  31839. ConvertProgram;
  31840. CheckSource('TestRangeChecks_StringIndex',
  31841. LinesToStr([ // statements
  31842. 'rtl.createClass(this, "TObject", null, function () {',
  31843. ' this.$init = function () {',
  31844. ' this.S = "";',
  31845. ' };',
  31846. ' this.$final = function () {',
  31847. ' };',
  31848. '});',
  31849. 'this.DoIt = function (h) {',
  31850. ' var s = "";',
  31851. ' var i = 0;',
  31852. ' var c = "";',
  31853. ' var o = null;',
  31854. ' c = rtl.rcc(rtl.rcCharAt(s, 0), 0, 65535);',
  31855. ' s = rtl.rcSetCharAt(s, i - 1, rtl.rcCharAt(s, i - 1));',
  31856. ' h.set(rtl.rcSetCharAt(h.get(), i - 1, rtl.rcCharAt(h.get(), i - 1)));',
  31857. ' c = rtl.rcc(rtl.rcCharAt(o.S, i - 1), 0, 65535);',
  31858. ' o.S = rtl.rcSetCharAt(o.S, i - 1, c);',
  31859. '};',
  31860. '']),
  31861. LinesToStr([ // $mod.$main
  31862. '']));
  31863. end;
  31864. procedure TTestModule.TestRangeChecks_TypecastInt;
  31865. begin
  31866. StartProgram(false);
  31867. Add([
  31868. '{$R+}',
  31869. 'var',
  31870. ' i: nativeint;',
  31871. ' b: byte;',
  31872. ' sh: shortint;',
  31873. ' w: word;',
  31874. ' sm: smallint;',
  31875. ' lw: longword;',
  31876. ' li: longint;',
  31877. 'begin',
  31878. ' b:=12+byte(i);',
  31879. ' sh:=12+shortint(i);',
  31880. ' w:=12+word(i);',
  31881. ' sm:=12+smallint(i);',
  31882. ' lw:=12+longword(i);',
  31883. ' li:=12+longint(i);',
  31884. '']);
  31885. ConvertProgram;
  31886. CheckSource('TestRangeChecks_TypecastInt',
  31887. LinesToStr([
  31888. 'this.i = 0;',
  31889. 'this.b = 0;',
  31890. 'this.sh = 0;',
  31891. 'this.w = 0;',
  31892. 'this.sm = 0;',
  31893. 'this.lw = 0;',
  31894. 'this.li = 0;',
  31895. '']),
  31896. LinesToStr([
  31897. '$mod.b = rtl.rc(12 + rtl.rc($mod.i, 0, 255), 0, 255);',
  31898. '$mod.sh = rtl.rc(12 + rtl.rc($mod.i, -128, 127), -128, 127);',
  31899. '$mod.w = rtl.rc(12 + rtl.rc($mod.i, 0, 65535), 0, 65535);',
  31900. '$mod.sm = rtl.rc(12 + rtl.rc($mod.i, -32768, 32767), -32768, 32767);',
  31901. '$mod.lw = rtl.rc(12 + rtl.rc($mod.i, 0, 4294967295), 0, 4294967295);',
  31902. '$mod.li = rtl.rc(12 + rtl.rc($mod.i, -2147483648, 2147483647), -2147483648, 2147483647);',
  31903. '']));
  31904. end;
  31905. procedure TTestModule.TestRangeChecks_TypeHelperInt;
  31906. begin
  31907. Scanner.Options:=Scanner.Options+[po_CAssignments];
  31908. StartProgram(false);
  31909. Add([
  31910. '{$modeswitch typehelpers}',
  31911. '{$R+}',
  31912. 'type',
  31913. ' TObject = class',
  31914. ' FSize: byte;',
  31915. ' property Size: byte read FSize;',
  31916. ' end;',
  31917. ' THelper = type helper for byte',
  31918. ' procedure SetIt(w: word);',
  31919. ' end;',
  31920. 'procedure THelper.SetIt(w: word);',
  31921. 'begin',
  31922. ' Self:=w;',
  31923. 'end;',
  31924. 'function GetIt: byte;',
  31925. 'begin',
  31926. ' Result.SetIt(2);',
  31927. 'end;',
  31928. 'var',
  31929. ' b: byte = 3;',
  31930. ' o: TObject;',
  31931. 'begin',
  31932. ' b.SetIt(14);',
  31933. ' with b do SetIt(15);',
  31934. ' o.Size.SetIt(16);',
  31935. '']);
  31936. ConvertProgram;
  31937. CheckSource('TestRangeChecks_AssignInt',
  31938. LinesToStr([ // statements
  31939. 'rtl.createClass(this, "TObject", null, function () {',
  31940. ' this.$init = function () {',
  31941. ' this.FSize = 0;',
  31942. ' };',
  31943. ' this.$final = function () {',
  31944. ' };',
  31945. '});',
  31946. 'rtl.createHelper(this, "THelper", null, function () {',
  31947. ' this.SetIt = function (w) {',
  31948. ' rtl.rc(w, 0, 65535);',
  31949. ' this.set(w);',
  31950. ' };',
  31951. '});',
  31952. 'this.GetIt = function () {',
  31953. ' var Result = 0;',
  31954. ' $mod.THelper.SetIt.call({',
  31955. ' get: function () {',
  31956. ' return Result;',
  31957. ' },',
  31958. ' set: function (v) {',
  31959. ' rtl.rc(v, 0, 255);',
  31960. ' Result = v;',
  31961. ' }',
  31962. ' }, 2);',
  31963. ' return Result;',
  31964. '};',
  31965. 'this.b = 3;',
  31966. 'this.o = null;',
  31967. '']),
  31968. LinesToStr([ // $mod.$main
  31969. '$mod.THelper.SetIt.call({',
  31970. ' p: $mod,',
  31971. ' get: function () {',
  31972. ' return this.p.b;',
  31973. ' },',
  31974. ' set: function (v) {',
  31975. ' rtl.rc(v, 0, 255);',
  31976. ' this.p.b = v;',
  31977. ' }',
  31978. '}, 14);',
  31979. 'var $with = $mod.b;',
  31980. '$mod.THelper.SetIt.call({',
  31981. ' get: function () {',
  31982. ' return $with;',
  31983. ' },',
  31984. ' set: function (v) {',
  31985. ' rtl.rc(v, 0, 255);',
  31986. ' $with = v;',
  31987. ' }',
  31988. '}, 15);',
  31989. '$mod.THelper.SetIt.call({',
  31990. ' p: $mod.o,',
  31991. ' get: function () {',
  31992. ' return this.p.FSize;',
  31993. ' },',
  31994. ' set: function (v) {',
  31995. ' rtl.rc(v, 0, 255);',
  31996. ' this.p.FSize = v;',
  31997. ' }',
  31998. '}, 16);',
  31999. '']));
  32000. end;
  32001. procedure TTestModule.TestAsync_Proc;
  32002. begin
  32003. StartProgram(false);
  32004. Add([
  32005. 'procedure Fly(w: word = 1); async; forward;',
  32006. 'procedure Run(w: word = 2); async;',
  32007. 'begin',
  32008. ' Fly(w);',
  32009. ' Fly;',
  32010. ' await(Fly(w));',
  32011. ' await(Fly);',
  32012. 'end;',
  32013. 'procedure Fly(w: word); ',
  32014. 'begin',
  32015. 'end;',
  32016. 'begin',
  32017. ' Run;',
  32018. ' Run(3);',
  32019. '']);
  32020. CheckResolverUnexpectedHints();
  32021. ConvertProgram;
  32022. CheckSource('TestAsync_Proc',
  32023. LinesToStr([ // statements
  32024. 'this.Run = async function (w) {',
  32025. ' $mod.Fly(w);',
  32026. ' $mod.Fly(1);',
  32027. ' await $mod.Fly(w);',
  32028. ' await $mod.Fly(1);',
  32029. '};',
  32030. 'this.Fly = async function (w) {',
  32031. '};',
  32032. '']),
  32033. LinesToStr([
  32034. '$mod.Run(2);',
  32035. '$mod.Run(3);',
  32036. '']));
  32037. end;
  32038. procedure TTestModule.TestAsync_CallResultIsPromise;
  32039. begin
  32040. StartProgram(false);
  32041. Add([
  32042. '{$modeswitch externalclass}',
  32043. 'type',
  32044. ' TObject = class',
  32045. ' end;',
  32046. ' TJSPromise = class external name ''Promise''',
  32047. ' end;',
  32048. ' TBird = class',
  32049. ' function Fly: word; async; ',
  32050. ' end;',
  32051. 'function TBird.Fly: word; async; ',
  32052. 'begin',
  32053. ' Result:=3;',
  32054. ' Fly:=4+Result;',
  32055. ' if Result=5 then ;',
  32056. ' exit(6);',
  32057. 'end;',
  32058. 'function Run: word; async;',
  32059. 'begin',
  32060. ' Result:=11+Result;',
  32061. ' inc(Result);',
  32062. 'end;',
  32063. 'var',
  32064. ' p: TJSPromise;',
  32065. ' o: TBird;',
  32066. 'begin',
  32067. ' p:=Run;',
  32068. ' p:=Run();',
  32069. ' if Run=p then ;',
  32070. ' if p=Run then ;',
  32071. ' if Run()=p then ;',
  32072. ' if p=Run() then ;',
  32073. ' p:=o.Fly;',
  32074. ' p:=o.Fly();',
  32075. ' if o.Fly=p then ;',
  32076. ' if o.Fly()=p then ;',
  32077. ' with o do begin',
  32078. ' p:=Fly;',
  32079. ' p:=Fly();',
  32080. ' if Fly=p then ;',
  32081. ' if Fly()=p then ;',
  32082. ' end;',
  32083. '']);
  32084. CheckResolverUnexpectedHints();
  32085. ConvertProgram;
  32086. CheckSource('TestAsync_CallResultIsPromise',
  32087. LinesToStr([ // statements
  32088. 'rtl.createClass(this, "TObject", null, function () {',
  32089. ' this.$init = function () {',
  32090. ' };',
  32091. ' this.$final = function () {',
  32092. ' };',
  32093. '});',
  32094. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  32095. ' this.Fly = async function () {',
  32096. ' var Result = 0;',
  32097. ' Result = 3;',
  32098. ' Result = 4 + Result;',
  32099. ' if (Result === 5) ;',
  32100. ' return 6;',
  32101. ' return Result;',
  32102. ' };',
  32103. '});',
  32104. 'this.Run = async function () {',
  32105. ' var Result = 0;',
  32106. ' Result = 11 + Result;',
  32107. ' Result += 1;',
  32108. ' return Result;',
  32109. '};',
  32110. 'this.p = null;',
  32111. 'this.o = null;',
  32112. '']),
  32113. LinesToStr([
  32114. '$mod.p = $mod.Run();',
  32115. '$mod.p = $mod.Run();',
  32116. 'if ($mod.Run() === $mod.p) ;',
  32117. 'if ($mod.p === $mod.Run()) ;',
  32118. 'if ($mod.Run() === $mod.p) ;',
  32119. 'if ($mod.p === $mod.Run()) ;',
  32120. '$mod.p = $mod.o.Fly();',
  32121. '$mod.p = $mod.o.Fly();',
  32122. 'if ($mod.o.Fly() === $mod.p) ;',
  32123. 'if ($mod.o.Fly() === $mod.p) ;',
  32124. 'var $with = $mod.o;',
  32125. '$mod.p = $with.Fly();',
  32126. '$mod.p = $with.Fly();',
  32127. 'if ($with.Fly() === $mod.p) ;',
  32128. 'if ($with.Fly() === $mod.p) ;',
  32129. '']));
  32130. end;
  32131. procedure TTestModule.TestAsync_ConstructorFail;
  32132. begin
  32133. StartProgram(false);
  32134. Add([
  32135. 'type',
  32136. ' TObject = class',
  32137. ' end;',
  32138. ' TBird = class',
  32139. ' constructor Create; async;',
  32140. ' end;',
  32141. 'constructor TBird.Create; async;',
  32142. 'begin',
  32143. 'end;',
  32144. 'begin',
  32145. '']);
  32146. SetExpectedPasResolverError('Invalid constructor modifier async',nInvalidXModifierY);
  32147. ConvertProgram;
  32148. end;
  32149. procedure TTestModule.TestAsync_PropertyGetterFail;
  32150. begin
  32151. StartProgram(false);
  32152. Add([
  32153. 'type',
  32154. ' TObject = class',
  32155. ' end;',
  32156. ' TBird = class',
  32157. ' function GetSize: word; async;',
  32158. ' property Size: word read GetSize;',
  32159. ' end;',
  32160. 'function TBird.GetSize: word; async;',
  32161. 'begin',
  32162. 'end;',
  32163. 'begin',
  32164. '']);
  32165. SetExpectedPasResolverError('Invalid property getter modifier async',nInvalidXModifierY);
  32166. ConvertProgram;
  32167. end;
  32168. procedure TTestModule.TestAwait_NonPromiseWithTypeFail;
  32169. begin
  32170. StartProgram(false);
  32171. Add([
  32172. 'procedure Run; async;',
  32173. 'begin',
  32174. ' await(word,1);',
  32175. 'end;',
  32176. 'begin',
  32177. '']);
  32178. SetExpectedPasResolverError('Incompatible type arg no. 2: Got "Longint", expected "TJSPromise"',nIncompatibleTypeArgNo);
  32179. ConvertProgram;
  32180. end;
  32181. procedure TTestModule.TestAwait_AsyncCallTypeMismatch;
  32182. begin
  32183. StartProgram(false);
  32184. Add([
  32185. 'type',
  32186. ' TObject = class',
  32187. ' end;',
  32188. ' TBird = class',
  32189. ' end;',
  32190. 'function Fly: TObject; async;',
  32191. 'begin',
  32192. 'end;',
  32193. 'procedure Run; async;',
  32194. 'begin',
  32195. ' await(TBird,Fly);',
  32196. 'end;',
  32197. 'begin',
  32198. '']);
  32199. SetExpectedPasResolverError('Incompatible type arg no. 2: Got "TObject", expected "TBird"',nIncompatibleTypeArgNo);
  32200. ConvertProgram;
  32201. end;
  32202. procedure TTestModule.TestAWait_OutsideAsyncFail;
  32203. begin
  32204. StartProgram(false);
  32205. Add([
  32206. 'procedure Crawl(w: double); ',
  32207. 'begin',
  32208. 'end;',
  32209. 'procedure Run(w: double);',
  32210. 'begin',
  32211. ' await(Crawl(w));',
  32212. 'end;',
  32213. 'begin',
  32214. ' Run(1);']);
  32215. SetExpectedPasResolverError(sAWaitOnlyInAsyncProcedure,nAWaitOnlyInAsyncProcedure);
  32216. ConvertProgram;
  32217. end;
  32218. procedure TTestModule.TestAWait_IntegerFail;
  32219. begin
  32220. StartProgram(false);
  32221. Add([
  32222. 'function Run: word;',
  32223. 'begin',
  32224. 'end;',
  32225. 'procedure Fly(w: word); async;',
  32226. 'begin',
  32227. ' await(Run());',
  32228. 'end;',
  32229. 'begin',
  32230. ' Fly(1);']);
  32231. SetExpectedPasResolverError('async function expected, but Result:Word found',nXExpectedButYFound);
  32232. ConvertProgram;
  32233. end;
  32234. procedure TTestModule.TestAWait_ExternalClassPromise;
  32235. begin
  32236. StartProgram(false);
  32237. Add([
  32238. '{$modeswitch externalclass}',
  32239. 'type',
  32240. ' TJSPromise = class external name ''Promise''',
  32241. ' end;',
  32242. ' TJSThenable = class external name ''Thenable''',
  32243. ' end;',
  32244. 'function Fly(w: word): TJSPromise;',
  32245. 'begin',
  32246. 'end;',
  32247. 'function Jump(w: word): word; async;',
  32248. 'begin',
  32249. 'end;',
  32250. 'function Eat(w: word): TJSPromise; async;',
  32251. 'begin',
  32252. 'end;',
  32253. 'function Run(d: double): word; async;',
  32254. 'var',
  32255. ' p: TJSPromise;',
  32256. 'begin',
  32257. ' Result:=await(word,p);', // promise needs type
  32258. ' Result:=await(word,Fly(3));', // promise needs type
  32259. ' Result:=await(Jump(4));', // async non promise must omit the type
  32260. ' Result:=await(word,Jump(5));', // async call can provide fitting type
  32261. ' Result:=await(word,Eat(6));', // promise needs type
  32262. 'end;',
  32263. 'begin',
  32264. '']);
  32265. ConvertProgram;
  32266. CheckSource('TestAWait_ExternalClassPromise',
  32267. LinesToStr([ // statements
  32268. 'this.Fly = function (w) {',
  32269. ' var Result = null;',
  32270. ' return Result;',
  32271. '};',
  32272. 'this.Jump = async function (w) {',
  32273. ' var Result = 0;',
  32274. ' return Result;',
  32275. '};',
  32276. 'this.Eat = async function (w) {',
  32277. ' var Result = null;',
  32278. ' return Result;',
  32279. '};',
  32280. 'this.Run = async function (d) {',
  32281. ' var Result = 0;',
  32282. ' var p = null;',
  32283. ' Result = await p;',
  32284. ' Result = await $mod.Fly(3);',
  32285. ' Result = await $mod.Jump(4);',
  32286. ' Result = await $mod.Jump(5);',
  32287. ' Result = await $mod.Eat(6);',
  32288. ' return Result;',
  32289. '};',
  32290. '']),
  32291. LinesToStr([
  32292. ]));
  32293. CheckResolverUnexpectedHints();
  32294. end;
  32295. procedure TTestModule.TestAWait_JSValue;
  32296. begin
  32297. StartProgram(false);
  32298. Add([
  32299. '{$modeswitch externalclass}',
  32300. 'type',
  32301. ' TJSPromise = class external name ''Promise''',
  32302. ' end;',
  32303. 'function Fly(w: word): jsvalue; async;',
  32304. 'begin',
  32305. 'end;',
  32306. 'function Run(d: jsvalue; var e): word; async;',
  32307. 'begin',
  32308. ' Result:=await(word,d);', // promise needs type
  32309. ' d:=await(Fly(4));', // async non promise must omit the type
  32310. ' Result:=await(word,e);', // promise needs type
  32311. 'end;',
  32312. 'begin',
  32313. '']);
  32314. ConvertProgram;
  32315. CheckSource('TestAWait_JSValue',
  32316. LinesToStr([ // statements
  32317. 'this.Fly = async function (w) {',
  32318. ' var Result = undefined;',
  32319. ' return Result;',
  32320. '};',
  32321. 'this.Run = async function (d, e) {',
  32322. ' var Result = 0;',
  32323. ' Result = await d;',
  32324. ' d = await $mod.Fly(4);',
  32325. ' Result = await e.get();',
  32326. ' return Result;',
  32327. '};',
  32328. '']),
  32329. LinesToStr([
  32330. ]));
  32331. CheckResolverUnexpectedHints();
  32332. end;
  32333. procedure TTestModule.TestAWait_Result;
  32334. begin
  32335. StartProgram(false);
  32336. Add([
  32337. '{$modeswitch externalclass}',
  32338. 'type',
  32339. ' TJSPromise = class external name ''Promise''',
  32340. ' end;',
  32341. 'function Crawl(d: double = 1.3): TJSPromise; ',
  32342. 'begin',
  32343. 'end;',
  32344. 'function Run(d: double = 1.6): word; async;',
  32345. 'begin',
  32346. ' Result:=await(word,Crawl);',
  32347. ' Result:=await(word,Crawl(4.5));',
  32348. ' Result:=await(Run);',
  32349. ' Result:=await(Run(6.7));',
  32350. 'end;',
  32351. 'begin',
  32352. ' Run(1);']);
  32353. ConvertProgram;
  32354. CheckSource('TestAWait_Result',
  32355. LinesToStr([ // statements
  32356. 'this.Crawl = function (d) {',
  32357. ' var Result = null;',
  32358. ' return Result;',
  32359. '};',
  32360. 'this.Run = async function (d) {',
  32361. ' var Result = 0;',
  32362. ' Result = await $mod.Crawl(1.3);',
  32363. ' Result = await $mod.Crawl(4.5);',
  32364. ' Result = await $mod.Run(1.6);',
  32365. ' Result = await $mod.Run(6.7);',
  32366. ' return Result;',
  32367. '};',
  32368. '']),
  32369. LinesToStr([
  32370. '$mod.Run(1);'
  32371. ]));
  32372. CheckResolverUnexpectedHints();
  32373. end;
  32374. procedure TTestModule.TestAWait_ResultPromiseMissingTypeFail;
  32375. begin
  32376. StartProgram(false);
  32377. Add([
  32378. '{$mode objfpc}',
  32379. '{$modeswitch externalclass}',
  32380. 'type',
  32381. ' TJSPromise = class external name ''Promise''',
  32382. ' end;',
  32383. 'function Run: TJSPromise; async;',
  32384. 'begin',
  32385. 'end;',
  32386. 'procedure Fly(w: word); async;',
  32387. 'begin',
  32388. ' await(Run());',
  32389. 'end;',
  32390. 'begin',
  32391. ' Fly(1);']);
  32392. SetExpectedPasResolverError('Wrong number of parameters specified for call to "function await(aType,TJSPromise):aType"',
  32393. nWrongNumberOfParametersForCallTo);
  32394. ConvertProgram;
  32395. end;
  32396. procedure TTestModule.TestAsync_AnonymousProc;
  32397. begin
  32398. StartProgram(false);
  32399. Add([
  32400. '{$mode objfpc}',
  32401. '{$modeswitch externalclass}',
  32402. 'type',
  32403. ' TJSPromise = class external name ''Promise''',
  32404. ' end;',
  32405. 'type',
  32406. ' TFunc = reference to function(x: double): word; async;',
  32407. 'function Crawl(d: double = 1.3): word; async;',
  32408. 'begin',
  32409. 'end;',
  32410. 'var Func: TFunc;',
  32411. 'begin',
  32412. ' Func:=function(c:double):word async begin',
  32413. ' Result:=await(Crawl(c));',
  32414. ' end;',
  32415. ' Func:=function(c:double):word async assembler asm',
  32416. ' end;',
  32417. ' ']);
  32418. ConvertProgram;
  32419. CheckSource('TestAsync_AnonymousProc',
  32420. LinesToStr([ // statements
  32421. 'this.Crawl = async function (d) {',
  32422. ' var Result = 0;',
  32423. ' return Result;',
  32424. '};',
  32425. 'this.Func = null;',
  32426. '']),
  32427. LinesToStr([
  32428. '$mod.Func = async function (c) {',
  32429. ' var Result = 0;',
  32430. ' Result = await $mod.Crawl(c);',
  32431. ' return Result;',
  32432. '};',
  32433. '$mod.Func = async function (c) {',
  32434. '};',
  32435. '']));
  32436. CheckResolverUnexpectedHints();
  32437. end;
  32438. procedure TTestModule.TestAsync_ProcType;
  32439. begin
  32440. StartProgram(false);
  32441. Add([
  32442. '{$mode objfpc}',
  32443. 'type',
  32444. ' TRefFunc = reference to function(x: double = 1.3): word; async;',
  32445. ' TFunc = function(x: double = 1.1): word; async;',
  32446. ' TProc = procedure(x: longint = 7); async;',
  32447. 'function Crawl(d: double): word; async;',
  32448. 'begin',
  32449. 'end;',
  32450. 'procedure Run(e:longint); async;',
  32451. 'begin',
  32452. 'end;',
  32453. 'procedure Fly(p: TProc); async;',
  32454. 'begin',
  32455. ' await(p);',
  32456. ' await(p());',
  32457. 'end;',
  32458. 'var',
  32459. ' RefFunc: TRefFunc;',
  32460. ' Func: TFunc;',
  32461. ' Proc, ProcB: TProc;',
  32462. 'begin',
  32463. ' Func:=@Crawl;',
  32464. ' RefFunc:=@Crawl;',
  32465. ' RefFunc:=function(c:double):word async begin',
  32466. ' Result:=await(RefFunc);',
  32467. ' Result:=await(RefFunc());',
  32468. ' Result:=await(Func);',
  32469. ' Result:=await(Func());',
  32470. ' await(Proc);',
  32471. ' await(Proc());',
  32472. ' await(Proc(13));',
  32473. ' end;',
  32474. ' Proc:=@Run;',
  32475. ' if Proc=ProcB then ;',
  32476. ' ']);
  32477. ConvertProgram;
  32478. CheckResolverUnexpectedHints();
  32479. CheckSource('TestAsync_ProcType',
  32480. LinesToStr([ // statements
  32481. 'this.Crawl = async function (d) {',
  32482. ' var Result = 0;',
  32483. ' return Result;',
  32484. '};',
  32485. 'this.Run = async function (e) {',
  32486. '};',
  32487. 'this.Fly = async function (p) {',
  32488. ' await p(7);',
  32489. ' await p(7);',
  32490. '};',
  32491. 'this.RefFunc = null;',
  32492. 'this.Func = null;',
  32493. 'this.Proc = null;',
  32494. 'this.ProcB = null;',
  32495. '']),
  32496. LinesToStr([
  32497. '$mod.Func = $mod.Crawl;',
  32498. '$mod.RefFunc = $mod.Crawl;',
  32499. '$mod.RefFunc = async function (c) {',
  32500. ' var Result = 0;',
  32501. ' Result = await $mod.RefFunc(1.3);',
  32502. ' Result = await $mod.RefFunc(1.3);',
  32503. ' Result = await $mod.Func(1.1);',
  32504. ' Result = await $mod.Func(1.1);',
  32505. ' await $mod.Proc(7);',
  32506. ' await $mod.Proc(7);',
  32507. ' await $mod.Proc(13);',
  32508. ' return Result;',
  32509. '};',
  32510. '$mod.Proc = $mod.Run;',
  32511. 'if (rtl.eqCallback($mod.Proc, $mod.ProcB)) ;',
  32512. '']));
  32513. end;
  32514. procedure TTestModule.TestAsync_ProcTypeAsyncModMismatchFail;
  32515. begin
  32516. StartProgram(false);
  32517. Add([
  32518. '{$mode objfpc}',
  32519. 'type',
  32520. ' TRefFunc = reference to function(x: double = 1.3): word;',
  32521. 'function Crawl(d: double): word; async;',
  32522. 'begin',
  32523. 'end;',
  32524. 'var',
  32525. ' RefFunc: TRefFunc;',
  32526. 'begin',
  32527. ' RefFunc:=@Crawl;',
  32528. ' ']);
  32529. SetExpectedPasResolverError('procedure type modifier "async" mismatch',nXModifierMismatchY);
  32530. ConvertProgram;
  32531. end;
  32532. procedure TTestModule.TestAsync_Inherited;
  32533. begin
  32534. StartProgram(false);
  32535. Add([
  32536. '{$mode objfpc}',
  32537. '{$modeswitch externalclass}',
  32538. 'type',
  32539. ' TJSPromise = class external name ''Promise''',
  32540. ' end;',
  32541. ' TObject = class',
  32542. ' function Run(w: word = 3): word; async; virtual;',
  32543. ' end;',
  32544. ' TBird = class',
  32545. ' function Run(w: word = 3): word; async; override;',
  32546. ' end;',
  32547. 'function TObject.Run(w: word = 3): word; async;',
  32548. 'begin',
  32549. 'end;',
  32550. 'function TBird.Run(w: word = 3): word;', // async modifier not needed in impl
  32551. 'var p: TJSPromise;',
  32552. 'begin',
  32553. ' p:=inherited;',
  32554. ' p:=inherited Run;',
  32555. ' p:=inherited Run();',
  32556. ' p:=inherited Run(4);',
  32557. ' exit(p);',
  32558. ' exit(inherited);',
  32559. ' exit(inherited Run);',
  32560. ' exit(inherited Run(5));',
  32561. ' exit(6);',
  32562. 'end;',
  32563. 'begin',
  32564. ' ']);
  32565. ConvertProgram;
  32566. CheckSource('TestAsync_Inherited',
  32567. LinesToStr([ // statements
  32568. 'rtl.createClass(this, "TObject", null, function () {',
  32569. ' this.$init = function () {',
  32570. ' };',
  32571. ' this.$final = function () {',
  32572. ' };',
  32573. ' this.Run = async function (w) {',
  32574. ' var Result = 0;',
  32575. ' return Result;',
  32576. ' };',
  32577. '});',
  32578. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  32579. ' this.Run = async function (w) {',
  32580. ' var Result = 0;',
  32581. ' var p = null;',
  32582. ' p = $mod.TObject.Run.apply(this, arguments);',
  32583. ' p = $mod.TObject.Run.call(this, 3);',
  32584. ' p = $mod.TObject.Run.call(this, 3);',
  32585. ' p = $mod.TObject.Run.call(this, 4);',
  32586. ' return p;',
  32587. ' return $mod.TObject.Run.apply(this, arguments);',
  32588. ' return $mod.TObject.Run.call(this, 3);',
  32589. ' return $mod.TObject.Run.call(this, 5);',
  32590. ' return 6;',
  32591. ' return Result;',
  32592. ' };',
  32593. '});',
  32594. '']),
  32595. LinesToStr([
  32596. '']));
  32597. CheckResolverUnexpectedHints();
  32598. end;
  32599. procedure TTestModule.TestAsync_ClassInterface;
  32600. begin
  32601. StartProgram(false);
  32602. Add([
  32603. '{$mode objfpc}',
  32604. '{$modeswitch externalclass}',
  32605. 'type',
  32606. ' TJSPromise = class external name ''Promise''',
  32607. ' end;',
  32608. ' IUnknown = interface',
  32609. ' function _AddRef: longint;',
  32610. ' function _Release: longint;',
  32611. ' end;',
  32612. 'function Say(i: IUnknown): IUnknown; async;',
  32613. 'begin',
  32614. 'end;',
  32615. 'function Run: IUnknown; async;',
  32616. 'begin',
  32617. ' Result:=await(Run);',
  32618. ' Result:=await(Run());',
  32619. ' Result:=await(Run) as IUnknown;',
  32620. ' Result:=await(Say(nil));',
  32621. ' Result:=await(Say(await(Run())));',
  32622. ' Result:=await(Say(await(Run()) as IUnknown));',
  32623. ' Result:=await(Say(await(Run()) as IUnknown)) as IUnknown;',
  32624. 'end;',
  32625. 'procedure Fly;',
  32626. 'var p: TJSPromise;',
  32627. 'begin',
  32628. ' Run;',
  32629. ' Run();',
  32630. ' p:=Run;',
  32631. ' p:=Run();',
  32632. 'end;',
  32633. 'begin',
  32634. ' ']);
  32635. ConvertProgram;
  32636. CheckSource('TestAsync_ClassInterface',
  32637. LinesToStr([ // statements
  32638. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  32639. 'this.Say = async function (i) {',
  32640. ' var Result = null;',
  32641. ' return Result;',
  32642. '};',
  32643. 'this.Run = async function () {',
  32644. ' var Result = null;',
  32645. ' var $ok = false;',
  32646. ' try {',
  32647. ' Result = rtl.setIntfL(Result, await $mod.Run());',
  32648. ' Result = rtl.setIntfL(Result, await $mod.Run());',
  32649. ' Result = rtl.setIntfL(Result, rtl.intfAsIntfT(await $mod.Run(), $mod.IUnknown));',
  32650. ' Result = rtl.setIntfL(Result, await $mod.Say(null));',
  32651. ' Result = rtl.setIntfL(Result, await $mod.Say(await $mod.Run()));',
  32652. ' Result = rtl.setIntfL(Result, await $mod.Say(rtl.intfAsIntfT(await $mod.Run(), $mod.IUnknown)));',
  32653. ' Result = rtl.setIntfL(Result, rtl.intfAsIntfT(await $mod.Say(rtl.intfAsIntfT(await $mod.Run(), $mod.IUnknown)), $mod.IUnknown));',
  32654. ' $ok = true;',
  32655. ' } finally {',
  32656. ' if (!$ok) rtl._Release(Result);',
  32657. ' };',
  32658. ' return Result;',
  32659. '};',
  32660. 'this.Fly = function () {',
  32661. ' var p = null;',
  32662. ' $mod.Run();',
  32663. ' $mod.Run();',
  32664. ' p = $mod.Run();',
  32665. ' p = $mod.Run();',
  32666. '};',
  32667. '']),
  32668. LinesToStr([
  32669. '']));
  32670. CheckResolverUnexpectedHints();
  32671. end;
  32672. procedure TTestModule.TestAsync_ClassInterface_AsyncMissmatchFail;
  32673. begin
  32674. StartProgram(true,[supTInterfacedObject]);
  32675. Add([
  32676. '{$mode objfpc}',
  32677. '{$modeswitch externalclass}',
  32678. 'type',
  32679. ' TJSPromise = class external name ''Promise''',
  32680. ' end;',
  32681. ' IBird = interface',
  32682. ' procedure Run;',
  32683. ' end;',
  32684. ' TBird = class(TInterfacedObject,IBird)',
  32685. ' procedure Run; async;',
  32686. ' end;',
  32687. 'procedure TBird.Run;',
  32688. 'begin',
  32689. 'end;',
  32690. 'begin',
  32691. ' ']);
  32692. SetExpectedPasResolverError('procedure type modifier "async" mismatch',nXModifierMismatchY);
  32693. ConvertProgram;
  32694. end;
  32695. Initialization
  32696. RegisterTests([TTestModule]);
  32697. end.