tcmodules.pas 881 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664236652366623667236682366923670236712367223673236742367523676236772367823679236802368123682236832368423685236862368723688236892369023691236922369323694236952369623697236982369923700237012370223703237042370523706237072370823709237102371123712237132371423715237162371723718237192372023721237222372323724237252372623727237282372923730237312373223733237342373523736237372373823739237402374123742237432374423745237462374723748237492375023751237522375323754237552375623757237582375923760237612376223763237642376523766237672376823769237702377123772237732377423775237762377723778237792378023781237822378323784237852378623787237882378923790237912379223793237942379523796237972379823799238002380123802238032380423805238062380723808238092381023811238122381323814238152381623817238182381923820238212382223823238242382523826238272382823829238302383123832238332383423835238362383723838238392384023841238422384323844238452384623847238482384923850238512385223853238542385523856238572385823859238602386123862238632386423865238662386723868238692387023871238722387323874238752387623877238782387923880238812388223883238842388523886238872388823889238902389123892238932389423895238962389723898238992390023901239022390323904239052390623907239082390923910239112391223913239142391523916239172391823919239202392123922239232392423925239262392723928239292393023931239322393323934239352393623937239382393923940239412394223943239442394523946239472394823949239502395123952239532395423955239562395723958239592396023961239622396323964239652396623967239682396923970239712397223973239742397523976239772397823979239802398123982239832398423985239862398723988239892399023991239922399323994239952399623997239982399924000240012400224003240042400524006240072400824009240102401124012240132401424015240162401724018240192402024021240222402324024240252402624027240282402924030240312403224033240342403524036240372403824039240402404124042240432404424045240462404724048240492405024051240522405324054240552405624057240582405924060240612406224063240642406524066240672406824069240702407124072240732407424075240762407724078240792408024081240822408324084240852408624087240882408924090240912409224093240942409524096240972409824099241002410124102241032410424105241062410724108241092411024111241122411324114241152411624117241182411924120241212412224123241242412524126241272412824129241302413124132241332413424135241362413724138241392414024141241422414324144241452414624147241482414924150241512415224153241542415524156241572415824159241602416124162241632416424165241662416724168241692417024171241722417324174241752417624177241782417924180241812418224183241842418524186241872418824189241902419124192241932419424195241962419724198241992420024201242022420324204242052420624207242082420924210242112421224213242142421524216242172421824219242202422124222242232422424225242262422724228242292423024231242322423324234242352423624237242382423924240242412424224243242442424524246242472424824249242502425124252242532425424255242562425724258242592426024261242622426324264242652426624267242682426924270242712427224273242742427524276242772427824279242802428124282242832428424285242862428724288242892429024291242922429324294242952429624297242982429924300243012430224303243042430524306243072430824309243102431124312243132431424315243162431724318243192432024321243222432324324243252432624327243282432924330243312433224333243342433524336243372433824339243402434124342243432434424345243462434724348243492435024351243522435324354243552435624357243582435924360243612436224363243642436524366243672436824369243702437124372243732437424375243762437724378243792438024381243822438324384243852438624387243882438924390243912439224393243942439524396243972439824399244002440124402244032440424405244062440724408244092441024411244122441324414244152441624417244182441924420244212442224423244242442524426244272442824429244302443124432244332443424435244362443724438244392444024441244422444324444244452444624447244482444924450244512445224453244542445524456244572445824459244602446124462244632446424465244662446724468244692447024471244722447324474244752447624477244782447924480244812448224483244842448524486244872448824489244902449124492244932449424495244962449724498244992450024501245022450324504245052450624507245082450924510245112451224513245142451524516245172451824519245202452124522245232452424525245262452724528245292453024531245322453324534245352453624537245382453924540245412454224543245442454524546245472454824549245502455124552245532455424555245562455724558245592456024561245622456324564245652456624567245682456924570245712457224573245742457524576245772457824579245802458124582245832458424585245862458724588245892459024591245922459324594245952459624597245982459924600246012460224603246042460524606246072460824609246102461124612246132461424615246162461724618246192462024621246222462324624246252462624627246282462924630246312463224633246342463524636246372463824639246402464124642246432464424645246462464724648246492465024651246522465324654246552465624657246582465924660246612466224663246642466524666246672466824669246702467124672246732467424675246762467724678246792468024681246822468324684246852468624687246882468924690246912469224693246942469524696246972469824699247002470124702247032470424705247062470724708247092471024711247122471324714247152471624717247182471924720247212472224723247242472524726247272472824729247302473124732247332473424735247362473724738247392474024741247422474324744247452474624747247482474924750247512475224753247542475524756247572475824759247602476124762247632476424765247662476724768247692477024771247722477324774247752477624777247782477924780247812478224783247842478524786247872478824789247902479124792247932479424795247962479724798247992480024801248022480324804248052480624807248082480924810248112481224813248142481524816248172481824819248202482124822248232482424825248262482724828248292483024831248322483324834248352483624837248382483924840248412484224843248442484524846248472484824849248502485124852248532485424855248562485724858248592486024861248622486324864248652486624867248682486924870248712487224873248742487524876248772487824879248802488124882248832488424885248862488724888248892489024891248922489324894248952489624897248982489924900249012490224903249042490524906249072490824909249102491124912249132491424915249162491724918249192492024921249222492324924249252492624927249282492924930249312493224933249342493524936249372493824939249402494124942249432494424945249462494724948249492495024951249522495324954249552495624957249582495924960249612496224963249642496524966249672496824969249702497124972249732497424975249762497724978249792498024981249822498324984249852498624987249882498924990249912499224993249942499524996249972499824999250002500125002250032500425005250062500725008250092501025011250122501325014250152501625017250182501925020250212502225023250242502525026250272502825029250302503125032250332503425035250362503725038250392504025041250422504325044250452504625047250482504925050250512505225053250542505525056250572505825059250602506125062250632506425065250662506725068250692507025071250722507325074250752507625077250782507925080250812508225083250842508525086250872508825089250902509125092250932509425095250962509725098250992510025101251022510325104251052510625107251082510925110251112511225113251142511525116251172511825119251202512125122251232512425125251262512725128251292513025131251322513325134251352513625137251382513925140251412514225143251442514525146251472514825149251502515125152251532515425155251562515725158251592516025161251622516325164251652516625167251682516925170251712517225173251742517525176251772517825179251802518125182251832518425185251862518725188251892519025191251922519325194251952519625197251982519925200252012520225203252042520525206252072520825209252102521125212252132521425215252162521725218252192522025221252222522325224252252522625227252282522925230252312523225233252342523525236252372523825239252402524125242252432524425245252462524725248252492525025251252522525325254252552525625257252582525925260252612526225263252642526525266252672526825269252702527125272252732527425275252762527725278252792528025281252822528325284252852528625287252882528925290252912529225293252942529525296252972529825299253002530125302253032530425305253062530725308253092531025311253122531325314253152531625317253182531925320253212532225323253242532525326253272532825329253302533125332253332533425335253362533725338253392534025341253422534325344253452534625347253482534925350253512535225353253542535525356253572535825359253602536125362253632536425365253662536725368253692537025371253722537325374253752537625377253782537925380253812538225383253842538525386253872538825389253902539125392253932539425395253962539725398253992540025401254022540325404254052540625407254082540925410254112541225413254142541525416254172541825419254202542125422254232542425425254262542725428254292543025431254322543325434254352543625437254382543925440254412544225443254442544525446254472544825449254502545125452254532545425455254562545725458254592546025461254622546325464254652546625467254682546925470254712547225473254742547525476254772547825479254802548125482254832548425485254862548725488254892549025491254922549325494254952549625497254982549925500255012550225503255042550525506255072550825509255102551125512255132551425515255162551725518255192552025521255222552325524255252552625527255282552925530255312553225533255342553525536255372553825539255402554125542255432554425545255462554725548255492555025551255522555325554255552555625557255582555925560255612556225563255642556525566255672556825569255702557125572255732557425575255762557725578255792558025581255822558325584255852558625587255882558925590255912559225593255942559525596255972559825599256002560125602256032560425605256062560725608256092561025611256122561325614256152561625617256182561925620256212562225623256242562525626256272562825629256302563125632256332563425635256362563725638256392564025641256422564325644256452564625647256482564925650256512565225653256542565525656256572565825659256602566125662256632566425665256662566725668256692567025671256722567325674256752567625677256782567925680256812568225683256842568525686256872568825689256902569125692256932569425695256962569725698256992570025701257022570325704257052570625707257082570925710257112571225713257142571525716257172571825719257202572125722257232572425725257262572725728257292573025731257322573325734257352573625737257382573925740257412574225743257442574525746257472574825749257502575125752257532575425755257562575725758257592576025761257622576325764257652576625767257682576925770257712577225773257742577525776257772577825779257802578125782257832578425785257862578725788257892579025791257922579325794257952579625797257982579925800258012580225803258042580525806258072580825809258102581125812258132581425815258162581725818258192582025821258222582325824258252582625827258282582925830258312583225833258342583525836258372583825839258402584125842258432584425845258462584725848258492585025851258522585325854258552585625857258582585925860258612586225863258642586525866258672586825869258702587125872258732587425875258762587725878258792588025881258822588325884258852588625887258882588925890258912589225893258942589525896258972589825899259002590125902259032590425905259062590725908259092591025911259122591325914259152591625917259182591925920259212592225923259242592525926259272592825929259302593125932259332593425935259362593725938259392594025941259422594325944259452594625947259482594925950259512595225953259542595525956259572595825959259602596125962259632596425965259662596725968259692597025971259722597325974259752597625977259782597925980259812598225983259842598525986259872598825989259902599125992259932599425995259962599725998259992600026001260022600326004260052600626007260082600926010260112601226013260142601526016260172601826019260202602126022260232602426025260262602726028260292603026031260322603326034260352603626037260382603926040260412604226043260442604526046260472604826049260502605126052260532605426055260562605726058260592606026061260622606326064260652606626067260682606926070260712607226073260742607526076260772607826079260802608126082260832608426085260862608726088260892609026091260922609326094260952609626097260982609926100261012610226103261042610526106261072610826109261102611126112261132611426115261162611726118261192612026121261222612326124261252612626127261282612926130261312613226133261342613526136261372613826139261402614126142261432614426145261462614726148261492615026151261522615326154261552615626157261582615926160261612616226163261642616526166261672616826169261702617126172261732617426175261762617726178261792618026181261822618326184261852618626187261882618926190261912619226193261942619526196261972619826199262002620126202262032620426205262062620726208262092621026211262122621326214262152621626217262182621926220262212622226223262242622526226262272622826229262302623126232262332623426235262362623726238262392624026241262422624326244262452624626247262482624926250262512625226253262542625526256262572625826259262602626126262262632626426265262662626726268262692627026271262722627326274262752627626277262782627926280262812628226283262842628526286262872628826289262902629126292262932629426295262962629726298262992630026301263022630326304263052630626307263082630926310263112631226313263142631526316263172631826319263202632126322263232632426325263262632726328263292633026331263322633326334263352633626337263382633926340263412634226343263442634526346263472634826349263502635126352263532635426355263562635726358263592636026361263622636326364263652636626367263682636926370263712637226373263742637526376263772637826379263802638126382263832638426385263862638726388263892639026391263922639326394263952639626397263982639926400264012640226403264042640526406264072640826409264102641126412264132641426415264162641726418264192642026421264222642326424264252642626427264282642926430264312643226433264342643526436264372643826439264402644126442264432644426445264462644726448264492645026451264522645326454264552645626457264582645926460264612646226463264642646526466264672646826469264702647126472264732647426475264762647726478264792648026481264822648326484264852648626487264882648926490264912649226493264942649526496264972649826499265002650126502265032650426505265062650726508265092651026511265122651326514265152651626517265182651926520265212652226523265242652526526265272652826529265302653126532265332653426535265362653726538265392654026541265422654326544265452654626547265482654926550265512655226553265542655526556265572655826559265602656126562265632656426565265662656726568265692657026571265722657326574265752657626577265782657926580265812658226583265842658526586265872658826589265902659126592265932659426595265962659726598265992660026601266022660326604266052660626607266082660926610266112661226613266142661526616266172661826619266202662126622266232662426625266262662726628266292663026631266322663326634266352663626637266382663926640266412664226643266442664526646266472664826649266502665126652266532665426655266562665726658266592666026661266622666326664266652666626667266682666926670266712667226673266742667526676266772667826679266802668126682266832668426685266862668726688266892669026691266922669326694266952669626697266982669926700267012670226703267042670526706267072670826709267102671126712267132671426715267162671726718267192672026721267222672326724267252672626727267282672926730267312673226733267342673526736267372673826739267402674126742267432674426745267462674726748267492675026751267522675326754267552675626757267582675926760267612676226763267642676526766267672676826769267702677126772267732677426775267762677726778267792678026781267822678326784267852678626787267882678926790267912679226793267942679526796267972679826799268002680126802268032680426805268062680726808268092681026811268122681326814268152681626817268182681926820268212682226823268242682526826268272682826829268302683126832268332683426835268362683726838268392684026841268422684326844268452684626847268482684926850268512685226853268542685526856268572685826859268602686126862268632686426865268662686726868268692687026871268722687326874268752687626877268782687926880268812688226883268842688526886268872688826889268902689126892268932689426895268962689726898268992690026901269022690326904269052690626907269082690926910269112691226913269142691526916269172691826919269202692126922269232692426925269262692726928269292693026931269322693326934269352693626937269382693926940269412694226943269442694526946269472694826949269502695126952269532695426955269562695726958269592696026961269622696326964269652696626967269682696926970269712697226973269742697526976269772697826979269802698126982269832698426985269862698726988269892699026991269922699326994269952699626997269982699927000270012700227003270042700527006270072700827009270102701127012270132701427015270162701727018270192702027021270222702327024270252702627027270282702927030270312703227033270342703527036270372703827039270402704127042270432704427045270462704727048270492705027051270522705327054270552705627057270582705927060270612706227063270642706527066270672706827069270702707127072270732707427075270762707727078270792708027081270822708327084270852708627087270882708927090270912709227093270942709527096270972709827099271002710127102271032710427105271062710727108271092711027111271122711327114271152711627117271182711927120271212712227123271242712527126271272712827129271302713127132271332713427135271362713727138271392714027141271422714327144271452714627147271482714927150271512715227153271542715527156271572715827159271602716127162271632716427165271662716727168271692717027171271722717327174271752717627177271782717927180271812718227183271842718527186271872718827189271902719127192271932719427195271962719727198271992720027201272022720327204272052720627207272082720927210272112721227213272142721527216272172721827219272202722127222272232722427225272262722727228272292723027231272322723327234272352723627237272382723927240272412724227243272442724527246272472724827249272502725127252272532725427255272562725727258272592726027261272622726327264272652726627267272682726927270272712727227273272742727527276272772727827279272802728127282272832728427285272862728727288272892729027291272922729327294272952729627297272982729927300273012730227303273042730527306273072730827309273102731127312273132731427315273162731727318273192732027321273222732327324273252732627327273282732927330273312733227333273342733527336273372733827339273402734127342273432734427345273462734727348273492735027351273522735327354273552735627357273582735927360273612736227363273642736527366273672736827369273702737127372273732737427375273762737727378273792738027381273822738327384273852738627387273882738927390273912739227393273942739527396273972739827399274002740127402274032740427405274062740727408274092741027411274122741327414274152741627417274182741927420274212742227423274242742527426274272742827429274302743127432274332743427435274362743727438274392744027441274422744327444274452744627447274482744927450274512745227453274542745527456274572745827459274602746127462274632746427465274662746727468274692747027471274722747327474274752747627477274782747927480274812748227483274842748527486274872748827489274902749127492274932749427495274962749727498274992750027501275022750327504275052750627507275082750927510275112751227513275142751527516275172751827519275202752127522275232752427525275262752727528275292753027531275322753327534275352753627537275382753927540275412754227543275442754527546275472754827549275502755127552275532755427555275562755727558275592756027561275622756327564275652756627567275682756927570275712757227573275742757527576275772757827579275802758127582275832758427585275862758727588275892759027591275922759327594275952759627597275982759927600276012760227603276042760527606276072760827609276102761127612276132761427615276162761727618276192762027621276222762327624276252762627627276282762927630276312763227633276342763527636276372763827639276402764127642276432764427645276462764727648276492765027651276522765327654276552765627657276582765927660276612766227663276642766527666276672766827669276702767127672276732767427675276762767727678276792768027681276822768327684276852768627687276882768927690276912769227693276942769527696276972769827699277002770127702277032770427705277062770727708277092771027711277122771327714277152771627717277182771927720277212772227723277242772527726277272772827729277302773127732277332773427735277362773727738277392774027741277422774327744277452774627747277482774927750277512775227753277542775527756277572775827759277602776127762277632776427765277662776727768277692777027771277722777327774277752777627777277782777927780277812778227783277842778527786277872778827789277902779127792277932779427795277962779727798277992780027801278022780327804278052780627807278082780927810278112781227813278142781527816278172781827819278202782127822278232782427825278262782727828278292783027831278322783327834278352783627837278382783927840278412784227843278442784527846278472784827849278502785127852278532785427855278562785727858278592786027861278622786327864278652786627867278682786927870278712787227873278742787527876278772787827879278802788127882278832788427885278862788727888278892789027891278922789327894278952789627897278982789927900279012790227903279042790527906279072790827909279102791127912279132791427915279162791727918279192792027921279222792327924279252792627927279282792927930279312793227933279342793527936279372793827939279402794127942279432794427945279462794727948279492795027951279522795327954279552795627957279582795927960279612796227963279642796527966279672796827969279702797127972279732797427975279762797727978279792798027981279822798327984279852798627987279882798927990279912799227993279942799527996279972799827999280002800128002280032800428005280062800728008280092801028011280122801328014280152801628017280182801928020280212802228023280242802528026280272802828029280302803128032280332803428035280362803728038280392804028041280422804328044280452804628047280482804928050280512805228053280542805528056280572805828059280602806128062280632806428065280662806728068280692807028071280722807328074280752807628077280782807928080280812808228083280842808528086280872808828089280902809128092280932809428095280962809728098280992810028101281022810328104281052810628107281082810928110281112811228113281142811528116281172811828119281202812128122281232812428125281262812728128281292813028131281322813328134281352813628137281382813928140281412814228143281442814528146281472814828149281502815128152281532815428155281562815728158281592816028161281622816328164281652816628167281682816928170281712817228173281742817528176281772817828179281802818128182281832818428185281862818728188281892819028191281922819328194281952819628197281982819928200282012820228203282042820528206282072820828209282102821128212282132821428215282162821728218282192822028221282222822328224282252822628227282282822928230282312823228233282342823528236282372823828239282402824128242282432824428245282462824728248282492825028251282522825328254282552825628257282582825928260282612826228263282642826528266282672826828269282702827128272282732827428275282762827728278282792828028281282822828328284282852828628287282882828928290282912829228293282942829528296282972829828299283002830128302283032830428305283062830728308283092831028311283122831328314283152831628317283182831928320283212832228323283242832528326283272832828329283302833128332283332833428335283362833728338283392834028341283422834328344283452834628347283482834928350283512835228353283542835528356283572835828359283602836128362283632836428365283662836728368283692837028371283722837328374283752837628377283782837928380283812838228383283842838528386283872838828389283902839128392283932839428395283962839728398283992840028401284022840328404284052840628407284082840928410284112841228413284142841528416284172841828419284202842128422284232842428425284262842728428284292843028431284322843328434284352843628437284382843928440284412844228443284442844528446284472844828449284502845128452284532845428455284562845728458284592846028461284622846328464284652846628467284682846928470284712847228473284742847528476284772847828479284802848128482284832848428485284862848728488284892849028491284922849328494284952849628497284982849928500285012850228503285042850528506285072850828509285102851128512285132851428515285162851728518285192852028521285222852328524285252852628527285282852928530285312853228533285342853528536285372853828539285402854128542285432854428545285462854728548285492855028551285522855328554285552855628557285582855928560285612856228563285642856528566285672856828569285702857128572285732857428575285762857728578285792858028581285822858328584285852858628587285882858928590285912859228593285942859528596285972859828599286002860128602286032860428605286062860728608286092861028611286122861328614286152861628617286182861928620286212862228623286242862528626286272862828629286302863128632286332863428635286362863728638286392864028641286422864328644286452864628647286482864928650286512865228653286542865528656286572865828659286602866128662286632866428665286662866728668286692867028671286722867328674286752867628677286782867928680286812868228683286842868528686286872868828689286902869128692286932869428695286962869728698286992870028701287022870328704287052870628707287082870928710287112871228713287142871528716287172871828719287202872128722287232872428725287262872728728287292873028731287322873328734287352873628737287382873928740287412874228743287442874528746287472874828749287502875128752287532875428755287562875728758287592876028761287622876328764287652876628767287682876928770287712877228773287742877528776287772877828779287802878128782287832878428785287862878728788287892879028791287922879328794287952879628797287982879928800288012880228803288042880528806288072880828809288102881128812288132881428815288162881728818288192882028821288222882328824288252882628827288282882928830288312883228833288342883528836288372883828839288402884128842288432884428845288462884728848288492885028851288522885328854288552885628857288582885928860288612886228863288642886528866288672886828869288702887128872288732887428875288762887728878288792888028881288822888328884288852888628887288882888928890288912889228893288942889528896288972889828899289002890128902289032890428905289062890728908289092891028911289122891328914289152891628917289182891928920289212892228923289242892528926289272892828929289302893128932289332893428935289362893728938289392894028941289422894328944289452894628947289482894928950289512895228953289542895528956289572895828959289602896128962289632896428965289662896728968289692897028971289722897328974289752897628977289782897928980289812898228983289842898528986289872898828989289902899128992289932899428995289962899728998289992900029001290022900329004290052900629007290082900929010290112901229013290142901529016290172901829019290202902129022290232902429025290262902729028290292903029031290322903329034290352903629037290382903929040290412904229043290442904529046290472904829049290502905129052290532905429055290562905729058290592906029061290622906329064290652906629067290682906929070290712907229073290742907529076290772907829079290802908129082290832908429085290862908729088290892909029091290922909329094290952909629097290982909929100291012910229103291042910529106291072910829109291102911129112291132911429115291162911729118291192912029121291222912329124291252912629127291282912929130291312913229133291342913529136291372913829139291402914129142291432914429145291462914729148291492915029151291522915329154291552915629157291582915929160291612916229163291642916529166291672916829169291702917129172291732917429175291762917729178291792918029181291822918329184291852918629187291882918929190291912919229193291942919529196291972919829199292002920129202292032920429205292062920729208292092921029211292122921329214292152921629217292182921929220292212922229223292242922529226292272922829229292302923129232292332923429235292362923729238292392924029241292422924329244292452924629247292482924929250292512925229253292542925529256292572925829259292602926129262292632926429265292662926729268292692927029271292722927329274292752927629277292782927929280292812928229283292842928529286292872928829289292902929129292292932929429295292962929729298292992930029301293022930329304293052930629307293082930929310293112931229313293142931529316293172931829319293202932129322293232932429325293262932729328293292933029331293322933329334293352933629337293382933929340293412934229343293442934529346293472934829349293502935129352293532935429355293562935729358293592936029361293622936329364293652936629367293682936929370293712937229373293742937529376293772937829379293802938129382293832938429385293862938729388293892939029391293922939329394293952939629397293982939929400294012940229403294042940529406294072940829409294102941129412294132941429415294162941729418294192942029421294222942329424294252942629427294282942929430294312943229433294342943529436294372943829439294402944129442294432944429445294462944729448294492945029451294522945329454294552945629457294582945929460294612946229463294642946529466294672946829469294702947129472294732947429475294762947729478294792948029481294822948329484294852948629487294882948929490294912949229493294942949529496294972949829499295002950129502295032950429505295062950729508295092951029511295122951329514295152951629517295182951929520295212952229523295242952529526295272952829529295302953129532295332953429535295362953729538295392954029541295422954329544295452954629547295482954929550295512955229553295542955529556295572955829559295602956129562295632956429565295662956729568295692957029571295722957329574295752957629577295782957929580295812958229583295842958529586295872958829589295902959129592295932959429595295962959729598295992960029601296022960329604296052960629607296082960929610296112961229613296142961529616296172961829619296202962129622296232962429625296262962729628296292963029631296322963329634296352963629637296382963929640296412964229643296442964529646296472964829649296502965129652296532965429655296562965729658296592966029661296622966329664296652966629667296682966929670296712967229673296742967529676296772967829679296802968129682296832968429685296862968729688296892969029691296922969329694296952969629697296982969929700297012970229703297042970529706297072970829709297102971129712297132971429715297162971729718297192972029721297222972329724297252972629727297282972929730297312973229733297342973529736297372973829739297402974129742297432974429745297462974729748297492975029751297522975329754297552975629757297582975929760297612976229763297642976529766297672976829769297702977129772297732977429775297762977729778297792978029781297822978329784297852978629787297882978929790297912979229793297942979529796297972979829799298002980129802298032980429805298062980729808298092981029811298122981329814298152981629817298182981929820298212982229823298242982529826298272982829829298302983129832298332983429835298362983729838298392984029841298422984329844298452984629847298482984929850298512985229853298542985529856298572985829859298602986129862298632986429865298662986729868298692987029871298722987329874298752987629877298782987929880298812988229883298842988529886298872988829889298902989129892298932989429895298962989729898298992990029901299022990329904299052990629907299082990929910299112991229913299142991529916299172991829919299202992129922299232992429925299262992729928299292993029931299322993329934299352993629937299382993929940299412994229943299442994529946299472994829949299502995129952299532995429955299562995729958299592996029961299622996329964299652996629967299682996929970299712997229973299742997529976299772997829979299802998129982299832998429985299862998729988299892999029991299922999329994299952999629997299982999930000300013000230003300043000530006300073000830009300103001130012300133001430015300163001730018300193002030021300223002330024300253002630027300283002930030300313003230033300343003530036300373003830039300403004130042300433004430045300463004730048300493005030051300523005330054300553005630057300583005930060300613006230063300643006530066300673006830069300703007130072300733007430075300763007730078300793008030081300823008330084300853008630087300883008930090300913009230093300943009530096300973009830099301003010130102301033010430105301063010730108301093011030111301123011330114301153011630117301183011930120301213012230123301243012530126301273012830129301303013130132301333013430135301363013730138301393014030141301423014330144301453014630147301483014930150301513015230153301543015530156301573015830159301603016130162301633016430165301663016730168301693017030171301723017330174301753017630177301783017930180301813018230183301843018530186301873018830189301903019130192301933019430195301963019730198301993020030201302023020330204302053020630207302083020930210302113021230213302143021530216302173021830219302203022130222302233022430225302263022730228302293023030231302323023330234302353023630237302383023930240302413024230243302443024530246302473024830249302503025130252302533025430255302563025730258302593026030261302623026330264302653026630267302683026930270302713027230273302743027530276302773027830279302803028130282302833028430285302863028730288302893029030291302923029330294302953029630297302983029930300303013030230303303043030530306303073030830309303103031130312303133031430315303163031730318303193032030321303223032330324303253032630327303283032930330303313033230333303343033530336303373033830339303403034130342303433034430345303463034730348303493035030351303523035330354303553035630357303583035930360303613036230363303643036530366303673036830369303703037130372303733037430375303763037730378303793038030381303823038330384303853038630387303883038930390303913039230393303943039530396303973039830399304003040130402304033040430405304063040730408304093041030411304123041330414304153041630417304183041930420304213042230423304243042530426304273042830429304303043130432304333043430435304363043730438304393044030441304423044330444304453044630447304483044930450304513045230453304543045530456304573045830459304603046130462304633046430465304663046730468304693047030471304723047330474304753047630477304783047930480304813048230483304843048530486304873048830489304903049130492304933049430495304963049730498304993050030501305023050330504305053050630507305083050930510305113051230513305143051530516305173051830519305203052130522305233052430525305263052730528305293053030531305323053330534305353053630537305383053930540305413054230543305443054530546305473054830549305503055130552305533055430555305563055730558305593056030561305623056330564305653056630567305683056930570305713057230573305743057530576305773057830579305803058130582305833058430585305863058730588305893059030591305923059330594305953059630597305983059930600306013060230603306043060530606306073060830609306103061130612306133061430615306163061730618306193062030621306223062330624306253062630627306283062930630306313063230633306343063530636306373063830639306403064130642306433064430645306463064730648306493065030651306523065330654306553065630657306583065930660306613066230663306643066530666306673066830669306703067130672306733067430675306763067730678306793068030681306823068330684306853068630687306883068930690306913069230693306943069530696306973069830699307003070130702307033070430705307063070730708307093071030711307123071330714307153071630717307183071930720307213072230723307243072530726307273072830729307303073130732307333073430735307363073730738307393074030741307423074330744307453074630747307483074930750307513075230753307543075530756307573075830759307603076130762307633076430765307663076730768307693077030771307723077330774307753077630777307783077930780307813078230783307843078530786307873078830789307903079130792307933079430795307963079730798307993080030801308023080330804308053080630807308083080930810308113081230813308143081530816308173081830819308203082130822308233082430825308263082730828308293083030831308323083330834308353083630837308383083930840308413084230843308443084530846308473084830849308503085130852308533085430855308563085730858308593086030861308623086330864308653086630867308683086930870308713087230873308743087530876308773087830879308803088130882308833088430885308863088730888308893089030891308923089330894308953089630897308983089930900309013090230903309043090530906309073090830909309103091130912309133091430915309163091730918309193092030921309223092330924309253092630927309283092930930309313093230933309343093530936309373093830939309403094130942309433094430945309463094730948309493095030951309523095330954309553095630957309583095930960309613096230963309643096530966309673096830969309703097130972309733097430975309763097730978309793098030981309823098330984309853098630987309883098930990309913099230993309943099530996309973099830999310003100131002310033100431005310063100731008310093101031011310123101331014310153101631017310183101931020310213102231023310243102531026310273102831029310303103131032310333103431035310363103731038310393104031041310423104331044310453104631047310483104931050310513105231053310543105531056310573105831059310603106131062310633106431065310663106731068310693107031071310723107331074310753107631077310783107931080310813108231083310843108531086310873108831089310903109131092310933109431095310963109731098310993110031101311023110331104311053110631107311083110931110311113111231113311143111531116311173111831119311203112131122311233112431125311263112731128311293113031131311323113331134311353113631137311383113931140311413114231143311443114531146311473114831149311503115131152311533115431155311563115731158311593116031161311623116331164311653116631167311683116931170311713117231173311743117531176311773117831179311803118131182311833118431185311863118731188311893119031191311923119331194311953119631197311983119931200312013120231203312043120531206312073120831209312103121131212312133121431215312163121731218312193122031221312223122331224312253122631227312283122931230312313123231233312343123531236312373123831239312403124131242312433124431245312463124731248312493125031251312523125331254312553125631257312583125931260312613126231263312643126531266312673126831269312703127131272312733127431275312763127731278312793128031281312823128331284312853128631287312883128931290312913129231293312943129531296312973129831299313003130131302313033130431305313063130731308313093131031311313123131331314313153131631317313183131931320313213132231323313243132531326313273132831329313303133131332313333133431335313363133731338313393134031341313423134331344313453134631347313483134931350313513135231353313543135531356313573135831359313603136131362313633136431365313663136731368313693137031371313723137331374313753137631377313783137931380313813138231383313843138531386313873138831389313903139131392313933139431395313963139731398313993140031401314023140331404314053140631407314083140931410314113141231413314143141531416314173141831419314203142131422314233142431425314263142731428314293143031431314323143331434314353143631437314383143931440314413144231443314443144531446314473144831449314503145131452314533145431455314563145731458314593146031461314623146331464314653146631467314683146931470314713147231473314743147531476314773147831479314803148131482314833148431485314863148731488314893149031491314923149331494314953149631497314983149931500315013150231503315043150531506315073150831509315103151131512315133151431515315163151731518315193152031521315223152331524315253152631527315283152931530315313153231533315343153531536315373153831539315403154131542315433154431545315463154731548315493155031551315523155331554315553155631557315583155931560315613156231563315643156531566315673156831569315703157131572315733157431575315763157731578315793158031581315823158331584315853158631587315883158931590315913159231593315943159531596315973159831599316003160131602316033160431605316063160731608316093161031611316123161331614316153161631617316183161931620316213162231623316243162531626316273162831629316303163131632316333163431635316363163731638316393164031641316423164331644316453164631647316483164931650316513165231653316543165531656316573165831659316603166131662316633166431665316663166731668316693167031671316723167331674316753167631677316783167931680316813168231683316843168531686316873168831689316903169131692316933169431695316963169731698316993170031701317023170331704317053170631707317083170931710317113171231713317143171531716317173171831719317203172131722317233172431725317263172731728317293173031731317323173331734317353173631737317383173931740317413174231743317443174531746317473174831749317503175131752317533175431755317563175731758317593176031761317623176331764317653176631767317683176931770317713177231773317743177531776317773177831779317803178131782317833178431785317863178731788317893179031791317923179331794317953179631797317983179931800318013180231803318043180531806318073180831809318103181131812318133181431815318163181731818318193182031821318223182331824318253182631827318283182931830318313183231833318343183531836318373183831839318403184131842318433184431845318463184731848318493185031851318523185331854318553185631857318583185931860318613186231863318643186531866318673186831869318703187131872318733187431875318763187731878318793188031881318823188331884318853188631887318883188931890318913189231893318943189531896318973189831899319003190131902319033190431905319063190731908319093191031911319123191331914319153191631917319183191931920319213192231923319243192531926319273192831929319303193131932319333193431935319363193731938319393194031941319423194331944319453194631947319483194931950319513195231953319543195531956319573195831959319603196131962319633196431965319663196731968319693197031971319723197331974319753197631977319783197931980319813198231983319843198531986319873198831989319903199131992319933199431995319963199731998319993200032001320023200332004320053200632007320083200932010320113201232013320143201532016320173201832019320203202132022320233202432025320263202732028320293203032031320323203332034320353203632037320383203932040320413204232043320443204532046320473204832049320503205132052320533205432055320563205732058320593206032061320623206332064320653206632067320683206932070320713207232073320743207532076320773207832079320803208132082320833208432085320863208732088320893209032091320923209332094320953209632097320983209932100321013210232103321043210532106321073210832109321103211132112321133211432115321163211732118321193212032121321223212332124321253212632127321283212932130321313213232133321343213532136321373213832139321403214132142321433214432145321463214732148321493215032151321523215332154321553215632157321583215932160321613216232163321643216532166321673216832169321703217132172321733217432175321763217732178321793218032181321823218332184321853218632187321883218932190321913219232193321943219532196321973219832199322003220132202322033220432205322063220732208322093221032211322123221332214322153221632217322183221932220322213222232223322243222532226322273222832229322303223132232322333223432235322363223732238322393224032241322423224332244322453224632247322483224932250322513225232253322543225532256322573225832259322603226132262322633226432265322663226732268322693227032271322723227332274322753227632277322783227932280322813228232283322843228532286322873228832289322903229132292322933229432295322963229732298322993230032301323023230332304323053230632307323083230932310323113231232313323143231532316323173231832319323203232132322323233232432325323263232732328323293233032331323323233332334323353233632337323383233932340323413234232343323443234532346323473234832349323503235132352323533235432355323563235732358323593236032361323623236332364323653236632367323683236932370323713237232373323743237532376323773237832379323803238132382323833238432385323863238732388323893239032391323923239332394323953239632397323983239932400324013240232403324043240532406324073240832409324103241132412324133241432415324163241732418324193242032421324223242332424324253242632427324283242932430324313243232433324343243532436324373243832439324403244132442324433244432445324463244732448324493245032451324523245332454324553245632457324583245932460324613246232463324643246532466324673246832469324703247132472324733247432475324763247732478324793248032481324823248332484324853248632487324883248932490324913249232493324943249532496324973249832499325003250132502325033250432505325063250732508325093251032511325123251332514325153251632517325183251932520325213252232523325243252532526325273252832529325303253132532325333253432535325363253732538325393254032541325423254332544325453254632547325483254932550325513255232553325543255532556325573255832559325603256132562325633256432565325663256732568325693257032571325723257332574325753257632577325783257932580325813258232583325843258532586325873258832589325903259132592325933259432595325963259732598325993260032601326023260332604326053260632607326083260932610326113261232613326143261532616326173261832619326203262132622326233262432625326263262732628326293263032631326323263332634326353263632637326383263932640326413264232643326443264532646326473264832649326503265132652326533265432655326563265732658326593266032661326623266332664326653266632667326683266932670326713267232673326743267532676326773267832679326803268132682326833268432685326863268732688326893269032691326923269332694326953269632697326983269932700327013270232703327043270532706327073270832709327103271132712327133271432715327163271732718327193272032721327223272332724327253272632727327283272932730327313273232733327343273532736327373273832739327403274132742327433274432745327463274732748327493275032751327523275332754327553275632757327583275932760327613276232763327643276532766327673276832769327703277132772327733277432775327763277732778327793278032781327823278332784327853278632787327883278932790327913279232793327943279532796327973279832799328003280132802328033280432805328063280732808328093281032811328123281332814328153281632817328183281932820328213282232823328243282532826328273282832829328303283132832328333283432835328363283732838328393284032841328423284332844328453284632847328483284932850328513285232853328543285532856328573285832859328603286132862328633286432865328663286732868328693287032871328723287332874328753287632877328783287932880328813288232883328843288532886328873288832889328903289132892328933289432895328963289732898328993290032901329023290332904329053290632907329083290932910329113291232913329143291532916329173291832919329203292132922329233292432925329263292732928329293293032931329323293332934329353293632937329383293932940329413294232943329443294532946329473294832949329503295132952329533295432955329563295732958329593296032961329623296332964329653296632967329683296932970329713297232973329743297532976329773297832979329803298132982329833298432985329863298732988329893299032991329923299332994329953299632997329983299933000330013300233003330043300533006330073300833009330103301133012330133301433015330163301733018330193302033021330223302333024330253302633027330283302933030330313303233033330343303533036330373303833039330403304133042330433304433045330463304733048330493305033051330523305333054330553305633057330583305933060330613306233063330643306533066330673306833069330703307133072330733307433075330763307733078330793308033081330823308333084330853308633087330883308933090330913309233093330943309533096330973309833099331003310133102331033310433105331063310733108331093311033111331123311333114331153311633117331183311933120331213312233123331243312533126331273312833129331303313133132331333313433135331363313733138331393314033141331423314333144331453314633147331483314933150331513315233153331543315533156331573315833159331603316133162331633316433165331663316733168331693317033171331723317333174331753317633177331783317933180331813318233183331843318533186331873318833189331903319133192331933319433195331963319733198331993320033201332023320333204332053320633207332083320933210332113321233213332143321533216332173321833219332203322133222332233322433225332263322733228332293323033231332323323333234332353323633237332383323933240332413324233243332443324533246332473324833249332503325133252332533325433255332563325733258332593326033261332623326333264332653326633267332683326933270332713327233273332743327533276332773327833279332803328133282332833328433285332863328733288332893329033291332923329333294332953329633297332983329933300333013330233303333043330533306333073330833309333103331133312333133331433315333163331733318333193332033321333223332333324333253332633327333283332933330333313333233333333343333533336333373333833339333403334133342333433334433345333463334733348333493335033351333523335333354333553335633357333583335933360333613336233363333643336533366333673336833369333703337133372333733337433375333763337733378333793338033381333823338333384333853338633387333883338933390333913339233393333943339533396333973339833399334003340133402334033340433405334063340733408334093341033411334123341333414334153341633417334183341933420334213342233423334243342533426334273342833429334303343133432334333343433435334363343733438334393344033441334423344333444334453344633447334483344933450334513345233453334543345533456334573345833459334603346133462334633346433465334663346733468334693347033471334723347333474334753347633477334783347933480334813348233483334843348533486334873348833489334903349133492334933349433495334963349733498334993350033501335023350333504335053350633507335083350933510335113351233513335143351533516335173351833519335203352133522335233352433525335263352733528335293353033531335323353333534335353353633537335383353933540335413354233543335443354533546335473354833549335503355133552335533355433555335563355733558335593356033561335623356333564335653356633567335683356933570335713357233573335743357533576335773357833579335803358133582335833358433585335863358733588335893359033591335923359333594
  1. {
  2. This file is part of the Free Component Library (FCL)
  3. Copyright (c) 2018 by Michael Van Canneyt
  4. Unit tests for Pascal-to-Javascript converter class.
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************
  11. Examples:
  12. ./testpas2js --suite=TTestModule.TestEmptyProgram
  13. ./testpas2js --suite=TTestModule.TestEmptyUnit
  14. }
  15. unit TCModules;
  16. {$mode objfpc}{$H+}
  17. interface
  18. uses
  19. Classes, SysUtils, fpcunit, testregistry, contnrs,
  20. jstree, jswriter, jsbase,
  21. PasTree, PScanner, PasResolver, PParser, PasResolveEval,
  22. FPPas2Js;
  23. const
  24. // default parser+scanner options
  25. po_tcmodules = po_Pas2js+[po_KeepScannerError];
  26. co_tcmodules = [];
  27. type
  28. TSrcMarkerKind = (
  29. mkLabel,
  30. mkResolverReference,
  31. mkDirectReference
  32. );
  33. PSrcMarker = ^TSrcMarker;
  34. TSrcMarker = record
  35. Kind: TSrcMarkerKind;
  36. Filename: string;
  37. Row: integer;
  38. StartCol, EndCol: integer; // token start, end column
  39. Identifier: string;
  40. Next: PSrcMarker;
  41. end;
  42. TSystemUnitPart = (
  43. supTObject,
  44. supTVarRec,
  45. supTypeInfo,
  46. supTInterfacedObject,
  47. supWriteln
  48. );
  49. TSystemUnitParts = set of TSystemUnitPart;
  50. { TTestHintMessage }
  51. TTestHintMessage = class
  52. public
  53. Id: int64;
  54. MsgType: TMessageType;
  55. MsgNumber: integer;
  56. Msg: string;
  57. SourcePos: TPasSourcePos;
  58. end;
  59. { TTestPasParser }
  60. TTestPasParser = Class(TPasParser)
  61. end;
  62. TOnFindUnit = function(const aUnitName: String): TPasModule of object;
  63. { TTestEnginePasResolver }
  64. TTestEnginePasResolver = class(TPas2JsResolver)
  65. private
  66. FFilename: string;
  67. FModule: TPasModule;
  68. FOnFindUnit: TOnFindUnit;
  69. FParser: TTestPasParser;
  70. FStreamResolver: TStreamResolver;
  71. FScanner: TPas2jsPasScanner;
  72. FSource: string;
  73. public
  74. destructor Destroy; override;
  75. function FindUnit(const AName, InFilename: String; NameExpr,
  76. InFileExpr: TPasExpr): TPasModule; override;
  77. procedure UsedInterfacesFinished(Section: TPasSection); override;
  78. property OnFindUnit: TOnFindUnit read FOnFindUnit write FOnFindUnit;
  79. property Filename: string read FFilename write FFilename;
  80. property StreamResolver: TStreamResolver read FStreamResolver write FStreamResolver;
  81. property Scanner: TPas2jsPasScanner read FScanner write FScanner;
  82. property Parser: TTestPasParser read FParser write FParser;
  83. property Source: string read FSource write FSource;
  84. property Module: TPasModule read FModule;
  85. end;
  86. { TCustomTestModule }
  87. TCustomTestModule = Class(TTestCase)
  88. private
  89. FConverter: TPasToJSConverter;
  90. FEngine: TTestEnginePasResolver;
  91. FExpectedErrorClass: ExceptClass;
  92. FExpectedErrorMsg: string;
  93. FExpectedErrorNumber: integer;
  94. FFilename: string;
  95. FFileResolver: TStreamResolver;
  96. FHub: TPas2JSResolverHub;
  97. FJSImplementationUses: TJSArrayLiteral;
  98. FJSInitBody: TJSFunctionBody;
  99. FJSImplentationUses: TJSArrayLiteral;
  100. FJSInterfaceUses: TJSArrayLiteral;
  101. FJSModule: TJSSourceElements;
  102. FJSModuleSrc: TJSSourceElements;
  103. FJSSource: TStringList;
  104. FModule: TPasModule;
  105. FJSModuleCallArgs: TJSArguments;
  106. FModules: TObjectList;// list of TTestEnginePasResolver
  107. FParser: TTestPasParser;
  108. FPasProgram: TPasProgram;
  109. FPasLibrary: TPasLibrary;
  110. FHintMsgs: TObjectList; // list of TTestHintMessage
  111. FHintMsgsGood: TFPList; // list of TTestHintMessage marked as expected
  112. FJSRegModuleCall: TJSCallExpression;
  113. FScanner: TPas2jsPasScanner;
  114. FSkipTests: boolean;
  115. FSource: TStringList;
  116. FFirstPasStatement: TPasImplBlock;
  117. FWithTypeInfo: boolean;
  118. {$IFDEF EnablePasTreeGlobalRefCount}
  119. FElementRefCountAtSetup: int64;
  120. {$ENDIF}
  121. function GetMsgCount: integer;
  122. function GetMsgs(Index: integer): TTestHintMessage;
  123. function GetResolverCount: integer;
  124. function GetResolvers(Index: integer): TTestEnginePasResolver;
  125. function OnPasResolverFindUnit(const aUnitName: String): TPasModule;
  126. procedure OnParserLog(Sender: TObject; const Msg: String);
  127. procedure OnPasResolverLog(Sender: TObject; const Msg: String);
  128. procedure OnScannerLog(Sender: TObject; const Msg: String);
  129. procedure SetWithTypeInfo(const AValue: boolean);
  130. protected
  131. procedure SetUp; override;
  132. function CreateConverter: TPasToJSConverter; virtual;
  133. function LoadUnit(const aUnitName: String): TPasModule;
  134. procedure InitScanner(aScanner: TPas2jsPasScanner); virtual;
  135. procedure TearDown; override;
  136. Procedure Add(Line: string); virtual;
  137. Procedure Add(const Lines: array of string);
  138. Procedure StartParsing; virtual;
  139. procedure ParseModuleQueue; virtual;
  140. procedure ParseModule; virtual;
  141. procedure ParseProgram; virtual;
  142. procedure ParseLibrary; virtual;
  143. procedure ParseUnit; virtual;
  144. protected
  145. function FindModuleWithFilename(aFilename: string): TTestEnginePasResolver; virtual;
  146. function AddModule(aFilename: string): TTestEnginePasResolver; virtual;
  147. function AddModuleWithSrc(aFilename, Src: string): TTestEnginePasResolver; virtual;
  148. function AddModuleWithIntfImplSrc(aFilename, InterfaceSrc,
  149. ImplementationSrc: string): TTestEnginePasResolver; virtual;
  150. procedure AddSystemUnit(Parts: TSystemUnitParts = []); virtual;
  151. procedure StartProgram(NeedSystemUnit: boolean; SystemUnitParts: TSystemUnitParts = []); virtual;
  152. procedure StartLibrary(NeedSystemUnit: boolean; SystemUnitParts: TSystemUnitParts = []); virtual;
  153. procedure StartUnit(NeedSystemUnit: boolean; SystemUnitParts: TSystemUnitParts = []); virtual;
  154. procedure ConvertModule; virtual;
  155. procedure ConvertProgram; virtual;
  156. procedure ConvertLibrary; virtual;
  157. procedure ConvertUnit; virtual;
  158. function ConvertJSModuleToString(El: TJSElement): string; virtual;
  159. procedure CheckDottedIdentifier(Msg: string; El: TJSElement; DottedName: string);
  160. function GetDottedIdentifier(El: TJSElement): string;
  161. procedure CheckSource(Msg,Statements: String; InitStatements: string = '';
  162. ImplStatements: string = ''); virtual;
  163. procedure CheckDiff(Msg, Expected, Actual: string); virtual;
  164. procedure CheckUnit(Filename, ExpectedSrc: string); virtual;
  165. procedure CheckHint(MsgType: TMessageType; MsgNumber: integer;
  166. Msg: string; Marker: PSrcMarker = nil); virtual;
  167. procedure CheckResolverUnexpectedHints(WithSourcePos: boolean = false); virtual;
  168. procedure SetExpectedScannerError(Msg: string; MsgNumber: integer);
  169. procedure SetExpectedParserError(Msg: string; MsgNumber: integer);
  170. procedure SetExpectedPasResolverError(Msg: string; MsgNumber: integer);
  171. procedure SetExpectedConverterError(Msg: string; MsgNumber: integer);
  172. function IsErrorExpected(E: Exception): boolean;
  173. procedure HandleScannerError(E: EScannerError);
  174. procedure HandleParserError(E: EParserError);
  175. procedure HandlePasResolveError(E: EPasResolve);
  176. procedure HandlePas2JSError(E: EPas2JS);
  177. procedure HandleException(E: Exception);
  178. procedure FailException(E: Exception);
  179. procedure WriteSources(const aFilename: string; aRow, aCol: integer);
  180. function IndexOfResolver(const Filename: string): integer;
  181. function GetResolver(const Filename: string): TTestEnginePasResolver;
  182. function GetDefaultNamespace: string;
  183. property PasProgram: TPasProgram Read FPasProgram;
  184. property PasLibrary: TPasLibrary Read FPasLibrary;
  185. property Resolvers[Index: integer]: TTestEnginePasResolver read GetResolvers;
  186. property ResolverCount: integer read GetResolverCount;
  187. property Engine: TTestEnginePasResolver read FEngine;
  188. property Filename: string read FFilename;
  189. Property Module: TPasModule Read FModule;
  190. property FirstPasStatement: TPasImplBlock read FFirstPasStatement;
  191. property Converter: TPasToJSConverter read FConverter;
  192. property JSSource: TStringList read FJSSource;
  193. property JSModule: TJSSourceElements read FJSModule;
  194. property JSRegModuleCall: TJSCallExpression read FJSRegModuleCall;
  195. property JSModuleCallArgs: TJSArguments read FJSModuleCallArgs;
  196. property JSImplementationUses: TJSArrayLiteral read FJSImplementationUses;
  197. property JSInterfaceUses: TJSArrayLiteral read FJSInterfaceUses;
  198. property JSModuleSrc: TJSSourceElements read FJSModuleSrc;
  199. property JSInitBody: TJSFunctionBody read FJSInitBody;
  200. property ExpectedErrorClass: ExceptClass read FExpectedErrorClass write FExpectedErrorClass;
  201. property ExpectedErrorMsg: string read FExpectedErrorMsg write FExpectedErrorMsg;
  202. property ExpectedErrorNumber: integer read FExpectedErrorNumber write FExpectedErrorNumber;
  203. property SkipTests: boolean read FSkipTests write FSkipTests;
  204. public
  205. constructor Create; override;
  206. destructor Destroy; override;
  207. property Hub: TPas2JSResolverHub read FHub;
  208. property Source: TStringList read FSource;
  209. property FileResolver: TStreamResolver read FFileResolver;
  210. property Scanner: TPas2jsPasScanner read FScanner;
  211. property Parser: TTestPasParser read FParser;
  212. property MsgCount: integer read GetMsgCount;
  213. property Msgs[Index: integer]: TTestHintMessage read GetMsgs;
  214. property WithTypeInfo: boolean read FWithTypeInfo write SetWithTypeInfo;
  215. end;
  216. { TTestModule }
  217. TTestModule = class(TCustomTestModule)
  218. Published
  219. Procedure TestReservedWords;
  220. // program, units, includes
  221. Procedure TestEmptyProgram;
  222. Procedure TestEmptyProgramUseStrict;
  223. Procedure TestEmptyUnit;
  224. Procedure TestEmptyUnitUseStrict;
  225. Procedure TestDottedUnitNames;
  226. Procedure TestDottedUnitNameImpl;
  227. Procedure TestDottedUnitExpr;
  228. Procedure Test_ModeFPCFail;
  229. Procedure Test_ModeSwitchCBlocksFail;
  230. Procedure TestUnit_UseSystem;
  231. Procedure TestUnit_Intf1Impl2Intf1;
  232. Procedure TestIncludeVersion;
  233. // vars/const
  234. Procedure TestVarInt;
  235. Procedure TestVarBaseTypes;
  236. Procedure TestBaseTypeSingleFail;
  237. Procedure TestBaseTypeExtendedFail;
  238. Procedure TestConstBaseTypes;
  239. Procedure TestUnitImplVars;
  240. Procedure TestUnitImplConsts;
  241. Procedure TestUnitImplRecord;
  242. Procedure TestRenameJSNameConflict;
  243. Procedure TestLocalConst;
  244. Procedure TestVarExternal;
  245. Procedure TestVarExternalOtherUnit;
  246. Procedure TestVarAbsoluteFail;
  247. Procedure TestConstExternal;
  248. // numbers
  249. Procedure TestDouble;
  250. Procedure TestInteger;
  251. Procedure TestIntegerRange;
  252. Procedure TestIntegerTypecasts;
  253. Procedure TestInteger_BitwiseShrNativeInt;
  254. Procedure TestInteger_BitwiseShlNativeInt;
  255. Procedure TestInteger_SystemFunc;
  256. Procedure TestCurrency;
  257. Procedure TestForBoolDo;
  258. Procedure TestForIntDo;
  259. Procedure TestForIntInDo;
  260. // strings
  261. Procedure TestCharConst;
  262. Procedure TestChar_Compare;
  263. Procedure TestChar_BuiltInProcs;
  264. Procedure TestStringConst;
  265. Procedure TestStringConst_InvalidUTF16;
  266. Procedure TestStringConstSurrogate;
  267. Procedure TestString_Length;
  268. Procedure TestString_Compare;
  269. Procedure TestString_SetLength;
  270. Procedure TestString_CharAt;
  271. Procedure TestStringHMinusFail;
  272. Procedure TestStr;
  273. Procedure TestBaseType_AnsiStringFail;
  274. Procedure TestBaseType_WideStringFail;
  275. Procedure TestBaseType_ShortStringFail;
  276. Procedure TestBaseType_RawByteStringFail;
  277. Procedure TestTypeShortstring_Fail;
  278. Procedure TestCharSet_Custom;
  279. Procedure TestWideChar;
  280. Procedure TestForCharDo;
  281. Procedure TestForCharInDo;
  282. // alias types
  283. Procedure TestAliasTypeRef;
  284. Procedure TestTypeCast_BaseTypes;
  285. Procedure TestTypeCast_AliasBaseTypes;
  286. // functions
  287. Procedure TestEmptyProc;
  288. Procedure TestProcOneParam;
  289. Procedure TestFunctionWithoutParams;
  290. Procedure TestProcedureWithoutParams;
  291. Procedure TestPrgProcVar;
  292. Procedure TestProcTwoArgs;
  293. Procedure TestProc_DefaultValue;
  294. Procedure TestUnitProcVar;
  295. Procedure TestImplProc;
  296. Procedure TestFunctionResult;
  297. Procedure TestNestedProc;
  298. Procedure TestNestedProc_ResultString;
  299. Procedure TestForwardProc;
  300. Procedure TestNestedForwardProc;
  301. Procedure TestAssignFunctionResult;
  302. Procedure TestFunctionResultInCondition;
  303. Procedure TestFunctionResultInForLoop;
  304. Procedure TestFunctionResultInTypeCast;
  305. Procedure TestExit;
  306. Procedure TestExit_ResultInFinally;
  307. Procedure TestBreak;
  308. Procedure TestBreakAsVar;
  309. Procedure TestContinue;
  310. Procedure TestProc_External;
  311. Procedure TestProc_ExternalOtherUnit;
  312. Procedure TestProc_Asm;
  313. Procedure TestProc_AsmSubBlock;
  314. Procedure TestProc_Assembler;
  315. Procedure TestProc_VarParam;
  316. Procedure TestProc_VarParamString;
  317. Procedure TestProc_VarParamV;
  318. Procedure TestProc_Overload;
  319. Procedure TestProc_OverloadForward;
  320. Procedure TestProc_OverloadIntfImpl;
  321. Procedure TestProc_OverloadNested;
  322. Procedure TestProc_OverloadNestedForward;
  323. Procedure TestProc_OverloadUnitCycle;
  324. Procedure TestProc_Varargs;
  325. Procedure TestProc_ConstOrder;
  326. Procedure TestProc_DuplicateConst;
  327. Procedure TestProc_LocalVarAbsolute;
  328. Procedure TestProc_LocalVarInit;
  329. Procedure TestProc_ReservedWords;
  330. Procedure TestProc_ConstRefWord;
  331. // anonymous functions
  332. Procedure TestAnonymousProc_Assign_ObjFPC;
  333. Procedure TestAnonymousProc_Assign_Delphi;
  334. Procedure TestAnonymousProc_Arg;
  335. Procedure TestAnonymousProc_Typecast;
  336. Procedure TestAnonymousProc_With;
  337. Procedure TestAnonymousProc_ExceptOn;
  338. Procedure TestAnonymousProc_Nested;
  339. Procedure TestAnonymousProc_NestedAssignResult;
  340. Procedure TestAnonymousProc_Class;
  341. Procedure TestAnonymousProc_ForLoop;
  342. Procedure TestAnonymousProc_AsmDelphi;
  343. // enums, sets
  344. Procedure TestEnum_Name;
  345. Procedure TestEnum_Number;
  346. Procedure TestEnum_ConstFail;
  347. Procedure TestEnum_Functions;
  348. Procedure TestEnumRg_Functions;
  349. Procedure TestEnum_AsParams;
  350. Procedure TestEnumRange_Array;
  351. Procedure TestEnum_ForIn;
  352. Procedure TestEnum_ScopedNumber;
  353. Procedure TestEnum_InFunction;
  354. Procedure TestEnum_Name_Anonymous_Unit;
  355. Procedure TestSet_Enum;
  356. Procedure TestSet_Operators;
  357. Procedure TestSet_Operator_In;
  358. Procedure TestSet_Functions;
  359. Procedure TestSet_PassAsArgClone;
  360. Procedure TestSet_AsParams;
  361. Procedure TestSet_Property;
  362. Procedure TestSet_EnumConst;
  363. Procedure TestSet_IntConst;
  364. Procedure TestSet_IntRange;
  365. Procedure TestSet_AnonymousEnumType;
  366. Procedure TestSet_AnonymousEnumTypeChar; // ToDo
  367. Procedure TestSet_ConstEnum;
  368. Procedure TestSet_ConstChar;
  369. Procedure TestSet_ConstInt;
  370. Procedure TestSet_InFunction;
  371. Procedure TestSet_ForIn;
  372. // statements
  373. Procedure TestNestBegin;
  374. Procedure TestIncDec;
  375. Procedure TestLoHiFpcMode;
  376. Procedure TestLoHiDelphiMode;
  377. Procedure TestAssignments;
  378. Procedure TestArithmeticOperators1;
  379. Procedure TestMultiAdd;
  380. Procedure TestLogicalOperators;
  381. Procedure TestBitwiseOperators;
  382. Procedure TestBitwiseOperatorsLongword;
  383. Procedure TestFunctionInt;
  384. Procedure TestFunctionString;
  385. Procedure TestIfThen;
  386. Procedure TestForLoop;
  387. Procedure TestForLoopInsideFunction;
  388. Procedure TestForLoop_ReadVarAfter;
  389. Procedure TestForLoop_Nested;
  390. Procedure TestRepeatUntil;
  391. Procedure TestAsmBlock;
  392. Procedure TestAsmPas_Impl; // ToDo
  393. Procedure TestTryFinally;
  394. Procedure TestTryExcept;
  395. Procedure TestTryExcept_ReservedWords;
  396. Procedure TestIfThenRaiseElse;
  397. Procedure TestCaseOf;
  398. Procedure TestCaseOf_UseSwitch;
  399. Procedure TestCaseOfNoElse;
  400. Procedure TestCaseOfNoElse_UseSwitch;
  401. Procedure TestCaseOfRange;
  402. Procedure TestCaseOfString;
  403. Procedure TestCaseOfChar;
  404. Procedure TestCaseOfExternalClassConst;
  405. Procedure TestDebugger;
  406. // arrays
  407. Procedure TestArray_Dynamic;
  408. Procedure TestArray_Dynamic_Nil;
  409. Procedure TestArray_DynMultiDimensional;
  410. Procedure TestArray_DynamicAssign;
  411. Procedure TestArray_StaticInt;
  412. Procedure TestArray_StaticBool;
  413. Procedure TestArray_StaticChar;
  414. Procedure TestArray_StaticMultiDim;
  415. Procedure TestArray_StaticInFunction;
  416. Procedure TestArray_StaticMultiDimEqualNotImplemented;
  417. Procedure TestArrayOfRecord;
  418. Procedure TestArray_StaticRecord;
  419. Procedure TestArrayOfSet;
  420. Procedure TestArray_DynAsParam;
  421. Procedure TestArray_StaticAsParam;
  422. Procedure TestArrayElement_AsParams;
  423. Procedure TestArrayElementFromFuncResult_AsParams;
  424. Procedure TestArrayEnumTypeRange;
  425. Procedure TestArray_SetLengthOutArg;
  426. Procedure TestArray_SetLengthProperty;
  427. Procedure TestArray_SetLengthMultiDim;
  428. Procedure TestArray_SetLengthDynOfStatic;
  429. Procedure TestArray_OpenArrayOfString;
  430. Procedure TestArray_ArrayOfCharAssignString; // ToDo
  431. Procedure TestArray_ConstRef;
  432. Procedure TestArray_Concat;
  433. Procedure TestArray_Copy;
  434. Procedure TestArray_InsertDelete;
  435. Procedure TestArray_DynArrayConstObjFPC;
  436. Procedure TestArray_DynArrayConstDelphi;
  437. Procedure TestArray_ArrayLitAsParam;
  438. Procedure TestArray_ArrayLitMultiDimAsParam;
  439. Procedure TestArray_ArrayLitStaticAsParam;
  440. Procedure TestArray_ForInArrOfString;
  441. Procedure TestExternalClass_TypeCastArrayToExternalClass;
  442. Procedure TestExternalClass_TypeCastArrayFromExternalClass;
  443. Procedure TestArrayOfConst_TVarRec;
  444. Procedure TestArrayOfConst_PassBaseTypes;
  445. Procedure TestArrayOfConst_PassObj;
  446. // record
  447. Procedure TestRecord_Empty;
  448. Procedure TestRecord_Var;
  449. Procedure TestRecord_VarExternal;
  450. Procedure TestRecord_WithDo;
  451. Procedure TestRecord_Assign;
  452. Procedure TestRecord_AsParams;
  453. Procedure TestRecord_ConstRef;
  454. Procedure TestRecordElement_AsParams;
  455. Procedure TestRecordElementFromFuncResult_AsParams;
  456. Procedure TestRecordElementFromWith_AsParams;
  457. Procedure TestRecord_Equal;
  458. Procedure TestRecord_JSValue;
  459. Procedure TestRecord_VariantFail;
  460. Procedure TestRecord_FieldArray;
  461. Procedure TestRecord_Const;
  462. Procedure TestRecord_TypecastFail;
  463. Procedure TestRecord_InFunction;
  464. Procedure TestRecord_AnonymousFail;
  465. // advanced record
  466. Procedure TestAdvRecord_Function;
  467. Procedure TestAdvRecord_Property;
  468. Procedure TestAdvRecord_PropertyDefault;
  469. Procedure TestAdvRecord_Property_ClassMethod;
  470. Procedure TestAdvRecord_Const;
  471. Procedure TestAdvRecord_ExternalField;
  472. Procedure TestAdvRecord_SubRecord;
  473. Procedure TestAdvRecord_SubClass;
  474. Procedure TestAdvRecord_SubInterfaceFail;
  475. Procedure TestAdvRecord_Constructor;
  476. Procedure TestAdvRecord_ClassConstructor_Program;
  477. Procedure TestAdvRecord_ClassConstructor_Unit;
  478. // classes
  479. Procedure TestClass_TObjectDefaultConstructor;
  480. Procedure TestClass_TObjectConstructorWithParams;
  481. Procedure TestClass_TObjectConstructorWithDefaultParam;
  482. Procedure TestClass_Var;
  483. Procedure TestClass_Method;
  484. Procedure TestClass_Implementation;
  485. Procedure TestClass_Inheritance;
  486. Procedure TestClass_TypeAlias;
  487. Procedure TestClass_AbstractMethod;
  488. Procedure TestClass_CallInherited_ProcNoParams;
  489. Procedure TestClass_CallInherited_WithParams;
  490. Procedure TestClasS_CallInheritedConstructor;
  491. Procedure TestClass_ClassVar_Assign;
  492. Procedure TestClass_CallClassMethod;
  493. Procedure TestClass_CallClassMethodStatic; // ToDo
  494. Procedure TestClass_Property;
  495. Procedure TestClass_Property_ClassMethod;
  496. Procedure TestClass_Property_Indexed;
  497. Procedure TestClass_Property_IndexSpec;
  498. Procedure TestClass_PropertyOfTypeArray;
  499. Procedure TestClass_PropertyDefault;
  500. Procedure TestClass_PropertyDefault_TypecastToOtherDefault;
  501. //Procedure TestClass_PropertyDefault;
  502. Procedure TestClass_PropertyOverride;
  503. Procedure TestClass_PropertyIncVisibility;
  504. Procedure TestClass_Assigned;
  505. Procedure TestClass_WithClassDoCreate;
  506. Procedure TestClass_WithClassInstDoProperty;
  507. Procedure TestClass_WithClassInstDoPropertyWithParams;
  508. Procedure TestClass_WithClassInstDoFunc;
  509. Procedure TestClass_TypeCast;
  510. Procedure TestClass_TypeCastUntypedParam;
  511. Procedure TestClass_Overloads;
  512. Procedure TestClass_OverloadsAncestor;
  513. Procedure TestClass_OverloadConstructor;
  514. Procedure TestClass_OverloadDelphiOverride;
  515. Procedure TestClass_ReintroduceVarDelphi;
  516. Procedure TestClass_ReintroducedVar;
  517. Procedure TestClass_RaiseDescendant;
  518. Procedure TestClass_ExternalMethod;
  519. Procedure TestClass_ExternalVirtualNameMismatchFail;
  520. Procedure TestClass_ExternalOverrideFail;
  521. Procedure TestClass_ExternalVar;
  522. Procedure TestClass_Const;
  523. Procedure TestClass_ConstEnum;
  524. Procedure TestClass_LocalConstDuplicate_Prg;
  525. Procedure TestClass_LocalConstDuplicate_Unit;
  526. // ToDo: Procedure TestAdvRecord_LocalConstDuplicate;
  527. Procedure TestClass_LocalVarSelfFail;
  528. Procedure TestClass_ArgSelfFail;
  529. Procedure TestClass_NestedProcSelf;
  530. Procedure TestClass_NestedProcSelf2;
  531. Procedure TestClass_NestedProcClassSelf;
  532. Procedure TestClass_NestedProcCallInherited;
  533. Procedure TestClass_TObjectFree;
  534. Procedure TestClass_TObjectFree_VarArg;
  535. Procedure TestClass_TObjectFreeNewInstance;
  536. Procedure TestClass_TObjectFreeLowerCase;
  537. Procedure TestClass_TObjectFreeFunctionFail;
  538. Procedure TestClass_TObjectFreePropertyFail;
  539. Procedure TestClass_ForIn;
  540. Procedure TestClass_DispatchMessage;
  541. Procedure TestClass_Message_DuplicateIntFail;
  542. Procedure TestClass_DispatchMessage_WrongFieldNameFail;
  543. // class of
  544. Procedure TestClassOf_Create;
  545. Procedure TestClassOf_Call;
  546. Procedure TestClassOf_Assign;
  547. Procedure TestClassOf_Is;
  548. Procedure TestClassOf_Compare;
  549. Procedure TestClassOf_ClassVar;
  550. Procedure TestClassOf_ClassMethod;
  551. Procedure TestClassOf_ClassProperty;
  552. Procedure TestClassOf_ClassMethodSelf;
  553. Procedure TestClassOf_TypeCast;
  554. Procedure TestClassOf_ImplicitFunctionCall;
  555. Procedure TestClassOf_Const;
  556. // nested class
  557. Procedure TestNestedClass_Alias;
  558. Procedure TestNestedClass_Record;
  559. Procedure TestNestedClass_Class;
  560. // external class
  561. Procedure TestExternalClass_Var;
  562. Procedure TestExternalClass_Const;
  563. Procedure TestExternalClass_Dollar;
  564. Procedure TestExternalClass_DuplicateVarFail;
  565. Procedure TestExternalClass_Method;
  566. Procedure TestExternalClass_ClassMethod;
  567. Procedure TestExternalClass_ClassMethodStatic;
  568. Procedure TestExternalClass_FunctionResultInTypeCast;
  569. Procedure TestExternalClass_NonExternalOverride;
  570. Procedure TestExternalClass_OverloadHint;
  571. Procedure TestExternalClass_SameNamePublishedProperty;
  572. Procedure TestExternalClass_Property;
  573. Procedure TestExternalClass_PropertyDate;
  574. Procedure TestExternalClass_ClassProperty;
  575. Procedure TestExternalClass_ClassOf;
  576. Procedure TestExternalClass_ClassOtherUnit;
  577. Procedure TestExternalClass_Is;
  578. Procedure TestExternalClass_As;
  579. Procedure TestExternalClass_DestructorFail;
  580. Procedure TestExternalClass_New;
  581. Procedure TestExternalClass_ClassOf_New;
  582. Procedure TestExternalClass_FuncClassOf_New;
  583. Procedure TestExternalClass_New_PasClassFail;
  584. Procedure TestExternalClass_New_PasClassBracketsFail;
  585. Procedure TestExternalClass_NewExtName;
  586. Procedure TestExternalClass_Constructor;
  587. Procedure TestExternalClass_ConstructorBrackets;
  588. Procedure TestExternalClass_LocalConstSameName;
  589. Procedure TestExternalClass_ReintroduceOverload;
  590. Procedure TestExternalClass_Inherited;
  591. Procedure TestExternalClass_PascalAncestorFail;
  592. Procedure TestExternalClass_NewInstance;
  593. Procedure TestExternalClass_NewInstance_NonVirtualFail;
  594. Procedure TestExternalClass_NewInstance_FirstParamNotString_Fail;
  595. Procedure TestExternalClass_NewInstance_SecondParamTyped_Fail;
  596. Procedure TestExternalClass_JSFunctionPasDescendant;
  597. Procedure TestExternalClass_PascalProperty;
  598. Procedure TestExternalClass_TypeCastToRootClass;
  599. Procedure TestExternalClass_TypeCastToJSObject;
  600. Procedure TestExternalClass_TypeCastStringToExternalString;
  601. Procedure TestExternalClass_TypeCastToJSFunction;
  602. Procedure TestExternalClass_TypeCastDelphiUnrelated;
  603. Procedure TestExternalClass_CallClassFunctionOfInstanceFail;
  604. Procedure TestExternalClass_BracketAccessor;
  605. Procedure TestExternalClass_BracketAccessor_Call;
  606. Procedure TestExternalClass_BracketAccessor_2ParamsFail;
  607. Procedure TestExternalClass_BracketAccessor_ReadOnly;
  608. Procedure TestExternalClass_BracketAccessor_WriteOnly;
  609. Procedure TestExternalClass_BracketAccessor_MultiType;
  610. Procedure TestExternalClass_BracketAccessor_Index;
  611. Procedure TestExternalClass_ForInJSObject;
  612. Procedure TestExternalClass_ForInJSArray;
  613. Procedure TestExternalClass_IncompatibleArgDuplicateIdentifier;
  614. // class interfaces
  615. Procedure TestClassInterface_Corba;
  616. Procedure TestClassInterface_ProcExternalFail;
  617. Procedure TestClassInterface_Overloads;
  618. Procedure TestClassInterface_DuplicateGUIInIntfListFail;
  619. Procedure TestClassInterface_DuplicateGUIInAncestorFail;
  620. Procedure TestClassInterface_AncestorImpl;
  621. Procedure TestClassInterface_ImplReintroduce;
  622. Procedure TestClassInterface_MethodResolution;
  623. Procedure TestClassInterface_AncestorMoreInterfaces;
  624. Procedure TestClassInterface_MethodOverride;
  625. Procedure TestClassInterface_Corba_Delegation;
  626. Procedure TestClassInterface_Corba_DelegationStatic;
  627. Procedure TestClassInterface_Corba_Operators;
  628. Procedure TestClassInterface_Corba_Args;
  629. Procedure TestClassInterface_Corba_ForIn;
  630. Procedure TestClassInterface_COM_AssignVar;
  631. Procedure TestClassInterface_COM_AssignArg;
  632. Procedure TestClassInterface_COM_FunctionResult;
  633. Procedure TestClassInterface_COM_InheritedFuncResult;
  634. Procedure TestClassInterface_COM_IsAsTypeCasts;
  635. Procedure TestClassInterface_COM_PassAsArg;
  636. Procedure TestClassInterface_COM_PassToUntypedParam;
  637. Procedure TestClassInterface_COM_FunctionInExpr;
  638. Procedure TestClassInterface_COM_Property;
  639. Procedure TestClassInterface_COM_IntfProperty;
  640. Procedure TestClassInterface_COM_Delegation;
  641. Procedure TestClassInterface_COM_With;
  642. Procedure TestClassInterface_COM_ForIn;
  643. Procedure TestClassInterface_COM_ArrayOfIntfFail;
  644. Procedure TestClassInterface_COM_RecordIntfFail;
  645. Procedure TestClassInterface_COM_UnitInitialization;
  646. Procedure TestClassInterface_GUID;
  647. Procedure TestClassInterface_GUIDProperty;
  648. // helpers
  649. Procedure TestClassHelper_ClassVar;
  650. Procedure TestClassHelper_Method_AccessInstanceFields;
  651. Procedure TestClassHelper_Method_Call;
  652. Procedure TestClassHelper_Method_Nested_Call;
  653. Procedure TestClassHelper_ClassMethod_Call;
  654. Procedure TestClassHelper_ClassOf;
  655. Procedure TestClassHelper_MethodRefObjFPC;
  656. Procedure TestClassHelper_Constructor;
  657. Procedure TestClassHelper_InheritedObjFPC;
  658. Procedure TestClassHelper_Property;
  659. Procedure TestClassHelper_Property_Array;
  660. Procedure TestClassHelper_Property_Array_Default;
  661. Procedure TestClassHelper_Property_Array_DefaultDefault;
  662. Procedure TestClassHelper_ClassProperty;
  663. Procedure TestClassHelper_ClassPropertyStatic;
  664. Procedure TestClassHelper_ClassProperty_Array;
  665. Procedure TestClassHelper_ForIn;
  666. Procedure TestClassHelper_PassProperty;
  667. Procedure TestExtClassHelper_ClassVar;
  668. Procedure TestExtClassHelper_Method_Call;
  669. Procedure TestExtClassHelper_ClassMethod_MissingStatic;
  670. Procedure TestRecordHelper_ClassVar;
  671. Procedure TestRecordHelper_Method_Call;
  672. Procedure TestRecordHelper_Constructor;
  673. Procedure TestTypeHelper_ClassVar;
  674. Procedure TestTypeHelper_PassResultElement;
  675. Procedure TestTypeHelper_PassArgs;
  676. Procedure TestTypeHelper_PassVarConst;
  677. Procedure TestTypeHelper_PassFuncResult;
  678. Procedure TestTypeHelper_PassPropertyField;
  679. Procedure TestTypeHelper_PassPropertyGetter;
  680. Procedure TestTypeHelper_PassClassPropertyField;
  681. Procedure TestTypeHelper_PassClassPropertyGetterStatic;
  682. Procedure TestTypeHelper_PassClassPropertyGetterNonStatic;
  683. Procedure TestTypeHelper_Property;
  684. Procedure TestTypeHelper_Property_Array;
  685. Procedure TestTypeHelper_ClassProperty;
  686. Procedure TestTypeHelper_ClassProperty_Array;
  687. Procedure TestTypeHelper_ClassMethod;
  688. Procedure TestTypeHelper_ExtClassMethodFail;
  689. Procedure TestTypeHelper_Constructor;
  690. Procedure TestTypeHelper_Word;
  691. Procedure TestTypeHelper_Boolean;
  692. Procedure TestTypeHelper_WordBool;
  693. Procedure TestTypeHelper_Double;
  694. Procedure TestTypeHelper_NativeInt;
  695. Procedure TestTypeHelper_StringChar;
  696. Procedure TestTypeHelper_JSValue;
  697. Procedure TestTypeHelper_Array;
  698. Procedure TestTypeHelper_EnumType;
  699. Procedure TestTypeHelper_SetType;
  700. Procedure TestTypeHelper_InterfaceType;
  701. Procedure TestTypeHelper_NestedSelf;
  702. // proc types
  703. Procedure TestProcType;
  704. Procedure TestProcType_Arg;
  705. Procedure TestProcType_FunctionFPC;
  706. Procedure TestProcType_FunctionDelphi;
  707. Procedure TestProcType_ProcedureDelphi;
  708. Procedure TestProcType_AsParam;
  709. Procedure TestProcType_MethodFPC;
  710. Procedure TestProcType_MethodDelphi;
  711. Procedure TestProcType_PropertyFPC;
  712. Procedure TestProcType_PropertyDelphi;
  713. Procedure TestProcType_WithClassInstDoPropertyFPC;
  714. Procedure TestProcType_Nested;
  715. Procedure TestProcType_NestedOfObject;
  716. Procedure TestProcType_ReferenceToProc;
  717. Procedure TestProcType_ReferenceToMethod;
  718. Procedure TestProcType_Typecast;
  719. Procedure TestProcType_PassProcToUntyped;
  720. Procedure TestProcType_PassProcToArray;
  721. Procedure TestProcType_SafeCallObjFPC;
  722. Procedure TestProcType_SafeCallDelphi;
  723. // pointer
  724. Procedure TestPointer;
  725. Procedure TestPointer_Proc;
  726. Procedure TestPointer_AssignRecordFail;
  727. Procedure TestPointer_AssignStaticArrayFail;
  728. Procedure TestPointer_TypeCastJSValueToPointer;
  729. Procedure TestPointer_NonRecordFail;
  730. Procedure TestPointer_AnonymousArgTypeFail;
  731. Procedure TestPointer_AnonymousVarTypeFail;
  732. Procedure TestPointer_AnonymousResultTypeFail;
  733. Procedure TestPointer_AddrOperatorFail;
  734. Procedure TestPointer_ArrayParamsFail;
  735. Procedure TestPointer_PointerAddFail;
  736. Procedure TestPointer_IncPointerFail;
  737. Procedure TestPointer_Record;
  738. Procedure TestPointer_RecordArg;
  739. // jsvalue
  740. Procedure TestJSValue_AssignToJSValue;
  741. Procedure TestJSValue_TypeCastToBaseType;
  742. Procedure TestJSValue_TypecastToJSValue;
  743. Procedure TestJSValue_Equal;
  744. Procedure TestJSValue_If;
  745. Procedure TestJSValue_Not;
  746. Procedure TestJSValue_Enum;
  747. Procedure TestJSValue_ClassInstance;
  748. Procedure TestJSValue_ClassOf;
  749. Procedure TestJSValue_ArrayOfJSValue;
  750. Procedure TestJSValue_ArrayLit;
  751. Procedure TestJSValue_Params;
  752. Procedure TestJSValue_UntypedParam;
  753. Procedure TestJSValue_FuncResultType;
  754. Procedure TestJSValue_ProcType_Assign;
  755. Procedure TestJSValue_ProcType_Equal;
  756. Procedure TestJSValue_ProcType_Param;
  757. Procedure TestJSValue_AssignToPointerFail;
  758. Procedure TestJSValue_OverloadDouble;
  759. Procedure TestJSValue_OverloadNativeInt;
  760. Procedure TestJSValue_OverloadWord;
  761. Procedure TestJSValue_OverloadString;
  762. Procedure TestJSValue_OverloadChar;
  763. Procedure TestJSValue_OverloadPointer;
  764. Procedure TestJSValue_ForIn;
  765. // RTTI
  766. Procedure TestRTTI_IntRange;
  767. Procedure TestRTTI_Double;
  768. Procedure TestRTTI_ProcType;
  769. Procedure TestRTTI_ProcType_ArgFromOtherUnit;
  770. Procedure TestRTTI_EnumAndSetType;
  771. Procedure TestRTTI_EnumRange;
  772. Procedure TestRTTI_AnonymousEnumType;
  773. Procedure TestRTTI_StaticArray;
  774. Procedure TestRTTI_DynArray;
  775. Procedure TestRTTI_ArrayNestedAnonymous;
  776. Procedure TestRTTI_PublishedMethodOverloadFail;
  777. Procedure TestRTTI_PublishedMethodHideNoHint;
  778. Procedure TestRTTI_PublishedMethodExternalFail;
  779. Procedure TestRTTI_PublishedClassPropertyFail;
  780. Procedure TestRTTI_PublishedClassFieldFail;
  781. Procedure TestRTTI_PublishedFieldExternalFail;
  782. Procedure TestRTTI_Class_Field;
  783. Procedure TestRTTI_Class_Method;
  784. Procedure TestRTTI_Class_MethodArgFlags;
  785. Procedure TestRTTI_Class_Property;
  786. Procedure TestRTTI_Class_PropertyParams;
  787. Procedure TestRTTI_Class_OtherUnit_TypeAlias;
  788. Procedure TestRTTI_Class_OmitRTTI;
  789. Procedure TestRTTI_Class_Field_AnonymousArrayOfSelfClass;
  790. Procedure TestRTTI_IndexModifier;
  791. Procedure TestRTTI_StoredModifier;
  792. Procedure TestRTTI_DefaultValue;
  793. Procedure TestRTTI_DefaultValueSet;
  794. Procedure TestRTTI_DefaultValueRangeType;
  795. Procedure TestRTTI_DefaultValueInherit;
  796. Procedure TestRTTI_OverrideMethod;
  797. Procedure TestRTTI_ReintroduceMethod;
  798. Procedure TestRTTI_OverloadProperty;
  799. // ToDo: array argument
  800. Procedure TestRTTI_ClassForward;
  801. Procedure TestRTTI_ClassOf;
  802. Procedure TestRTTI_Record;
  803. Procedure TestRTTI_RecordAnonymousArray;
  804. Procedure TestRTTI_Record_ClassVarType;
  805. Procedure TestRTTI_LocalTypes;
  806. Procedure TestRTTI_TypeInfo_BaseTypes;
  807. Procedure TestRTTI_TypeInfo_Type_BaseTypes;
  808. Procedure TestRTTI_TypeInfo_LocalFail;
  809. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses1;
  810. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses2;
  811. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses3;
  812. Procedure TestRTTI_TypeInfo_FunctionClassType;
  813. Procedure TestRTTI_TypeInfo_MixedUnits_PointerAndClass;
  814. Procedure TestRTTI_Interface_Corba;
  815. Procedure TestRTTI_Interface_COM;
  816. Procedure TestRTTI_ClassHelper;
  817. Procedure TestRTTI_ExternalClass;
  818. Procedure TestRTTI_Unit;
  819. // Resourcestring
  820. Procedure TestResourcestringProgram;
  821. Procedure TestResourcestringUnit;
  822. Procedure TestResourcestringImplementation;
  823. // Attributes
  824. Procedure TestAttributes_Members;
  825. Procedure TestAttributes_Types;
  826. Procedure TestAttributes_HelperConstructor_Fail;
  827. // Assertions, checks
  828. procedure TestAssert;
  829. procedure TestAssert_SysUtils;
  830. procedure TestObjectChecks;
  831. procedure TestOverflowChecks_Int;
  832. procedure TestRangeChecks_AssignInt;
  833. procedure TestRangeChecks_AssignIntRange;
  834. procedure TestRangeChecks_AssignEnum;
  835. procedure TestRangeChecks_AssignEnumRange;
  836. procedure TestRangeChecks_AssignChar;
  837. procedure TestRangeChecks_AssignCharRange;
  838. procedure TestRangeChecks_ArrayIndex;
  839. procedure TestRangeChecks_ArrayOfRecIndex;
  840. procedure TestRangeChecks_StringIndex;
  841. procedure TestRangeChecks_TypecastInt;
  842. procedure TestRangeChecks_TypeHelperInt;
  843. // Async/AWait
  844. Procedure TestAsync_Proc;
  845. Procedure TestAsync_CallResultIsPromise;
  846. Procedure TestAsync_ConstructorFail;
  847. Procedure TestAsync_PropertyGetterFail;
  848. Procedure TestAwait_NonPromiseWithTypeFail;
  849. Procedure TestAwait_AsyncCallTypeMismatch;
  850. Procedure TestAWait_OutsideAsyncFail;
  851. Procedure TestAWait_IntegerFail;
  852. Procedure TestAWait_ExternalClassPromise;
  853. Procedure TestAWait_JSValue;
  854. Procedure TestAWait_Result;
  855. Procedure TestAWait_ResultPromiseMissingTypeFail; // await(AsyncCallResultPromise) needs T
  856. Procedure TestAsync_AnonymousProc;
  857. Procedure TestAsync_ProcType;
  858. Procedure TestAsync_ProcTypeAsyncModMismatchFail;
  859. Procedure TestAsync_Inherited;
  860. Procedure TestAsync_ClassInterface;
  861. Procedure TestAsync_ClassInterface_AsyncMissmatchFail;
  862. // Library
  863. Procedure TestLibrary_Empty;
  864. Procedure TestLibrary_ExportFunc; // ToDo
  865. // ToDo: test delayed specialization init
  866. // ToDO: analyzer
  867. end;
  868. function LinesToStr(Args: array of const): string;
  869. function ExtractFileUnitName(aFilename: string): string;
  870. function JSToStr(El: TJSElement): string;
  871. function CheckSrcDiff(Expected, Actual: string; out Msg: string): boolean;
  872. implementation
  873. function LinesToStr(Args: array of const): string;
  874. var
  875. s: String;
  876. i: Integer;
  877. begin
  878. s:='';
  879. for i:=Low(Args) to High(Args) do
  880. case Args[i].VType of
  881. vtChar: s += Args[i].VChar+LineEnding;
  882. vtString: s += Args[i].VString^+LineEnding;
  883. vtPChar: s += Args[i].VPChar+LineEnding;
  884. vtWideChar: s += AnsiString(Args[i].VWideChar)+LineEnding;
  885. vtPWideChar: s += AnsiString(Args[i].VPWideChar)+LineEnding;
  886. vtAnsiString: s += AnsiString(Args[i].VAnsiString)+LineEnding;
  887. vtWidestring: s += AnsiString(WideString(Args[i].VWideString))+LineEnding;
  888. vtUnicodeString:s += AnsiString(UnicodeString(Args[i].VUnicodeString))+LineEnding;
  889. end;
  890. Result:=s;
  891. end;
  892. function ExtractFileUnitName(aFilename: string): string;
  893. var
  894. p: Integer;
  895. begin
  896. Result:=ExtractFileName(aFilename);
  897. if Result='' then exit;
  898. for p:=length(Result) downto 1 do
  899. case Result[p] of
  900. '/','\': exit;
  901. '.':
  902. begin
  903. Delete(Result,p,length(Result));
  904. exit;
  905. end;
  906. end;
  907. end;
  908. function JSToStr(El: TJSElement): string;
  909. var
  910. aWriter: TBufferWriter;
  911. aJSWriter: TJSWriter;
  912. begin
  913. aJSWriter:=nil;
  914. aWriter:=TBufferWriter.Create(1000);
  915. try
  916. aJSWriter:=TJSWriter.Create(aWriter);
  917. aJSWriter.IndentSize:=2;
  918. aJSWriter.WriteJS(El);
  919. Result:=aWriter.AsString;
  920. finally
  921. aJSWriter.Free;
  922. aWriter.Free;
  923. end;
  924. end;
  925. function CheckSrcDiff(Expected, Actual: string; out Msg: string): boolean;
  926. // search diff, ignore changes in spaces
  927. const
  928. SpaceChars = [#9,#10,#13,' '];
  929. var
  930. ExpectedP, ActualP: PChar;
  931. function FindLineEnd(p: PChar): PChar;
  932. begin
  933. Result:=p;
  934. while not (Result^ in [#0,#10,#13]) do inc(Result);
  935. end;
  936. function FindLineStart(p, MinP: PChar): PChar;
  937. begin
  938. while (p>MinP) and not (p[-1] in [#10,#13]) do dec(p);
  939. Result:=p;
  940. end;
  941. procedure SkipLineEnd(var p: PChar);
  942. begin
  943. if p^ in [#10,#13] then
  944. begin
  945. if (p[1] in [#10,#13]) and (p^<>p[1]) then
  946. inc(p,2)
  947. else
  948. inc(p);
  949. end;
  950. end;
  951. function HasSpecialChar(s: string): boolean;
  952. var
  953. i: Integer;
  954. begin
  955. for i:=1 to length(s) do
  956. if s[i] in [#0..#31,#127..#255] then
  957. exit(true);
  958. Result:=false;
  959. end;
  960. function HashSpecialChars(s: string): string;
  961. var
  962. i: Integer;
  963. begin
  964. Result:='';
  965. for i:=1 to length(s) do
  966. if s[i] in [#0..#31,#127..#255] then
  967. Result:=Result+'#'+hexstr(ord(s[i]),2)
  968. else
  969. Result:=Result+s[i];
  970. end;
  971. procedure DiffFound;
  972. var
  973. ActLineStartP, ActLineEndP, p, StartPos: PChar;
  974. ExpLine, ActLine: String;
  975. i, LineNo, DiffLineNo: Integer;
  976. begin
  977. writeln('Diff found "',Msg,'". Lines:');
  978. // write correct lines
  979. p:=PChar(Expected);
  980. LineNo:=0;
  981. DiffLineNo:=0;
  982. repeat
  983. StartPos:=p;
  984. while not (p^ in [#0,#10,#13]) do inc(p);
  985. ExpLine:=copy(Expected,StartPos-PChar(Expected)+1,p-StartPos);
  986. SkipLineEnd(p);
  987. inc(LineNo);
  988. if (p<=ExpectedP) and (p^<>#0) then
  989. begin
  990. writeln('= ',ExpLine);
  991. end else begin
  992. // diff line
  993. if DiffLineNo=0 then DiffLineNo:=LineNo;
  994. // write actual line
  995. ActLineStartP:=FindLineStart(ActualP,PChar(Actual));
  996. ActLineEndP:=FindLineEnd(ActualP);
  997. ActLine:=copy(Actual,ActLineStartP-PChar(Actual)+1,ActLineEndP-ActLineStartP);
  998. writeln('- ',ActLine);
  999. if HasSpecialChar(ActLine) then
  1000. writeln('- ',HashSpecialChars(ActLine));
  1001. // write expected line
  1002. writeln('+ ',ExpLine);
  1003. if HasSpecialChar(ExpLine) then
  1004. writeln('- ',HashSpecialChars(ExpLine));
  1005. // write empty line with pointer ^
  1006. for i:=1 to 2+ExpectedP-StartPos do write(' ');
  1007. writeln('^');
  1008. Msg:='expected "'+ExpLine+'", but got "'+ActLine+'".';
  1009. CheckSrcDiff:=false;
  1010. // write up to three following actual lines to get some context
  1011. for i:=1 to 3 do begin
  1012. ActLineStartP:=ActLineEndP;
  1013. SkipLineEnd(ActLineStartP);
  1014. if ActLineStartP^=#0 then break;
  1015. ActLineEndP:=FindLineEnd(ActLineStartP);
  1016. ActLine:=copy(Actual,ActLineStartP-PChar(Actual)+1,ActLineEndP-ActLineStartP);
  1017. writeln('~ ',ActLine);
  1018. end;
  1019. exit;
  1020. end;
  1021. until p^=#0;
  1022. writeln('DiffFound Actual:-----------------------');
  1023. writeln(Actual);
  1024. writeln('DiffFound Expected:---------------------');
  1025. writeln(Expected);
  1026. writeln('DiffFound ------------------------------');
  1027. Msg:='diff found, but lines are the same, internal error';
  1028. CheckSrcDiff:=false;
  1029. end;
  1030. var
  1031. IsSpaceNeeded: Boolean;
  1032. LastChar, Quote: Char;
  1033. begin
  1034. Result:=true;
  1035. Msg:='';
  1036. if Expected='' then Expected:=' ';
  1037. if Actual='' then Actual:=' ';
  1038. ExpectedP:=PChar(Expected);
  1039. ActualP:=PChar(Actual);
  1040. repeat
  1041. //writeln('TTestModule.CheckDiff Exp="',ExpectedP^,'" Act="',ActualP^,'"');
  1042. case ExpectedP^ of
  1043. #0:
  1044. begin
  1045. // check that rest of Actual has only spaces
  1046. while ActualP^ in SpaceChars do inc(ActualP);
  1047. if ActualP^<>#0 then
  1048. begin
  1049. DiffFound;
  1050. exit;
  1051. end;
  1052. exit(true);
  1053. end;
  1054. ' ',#9,#10,#13:
  1055. begin
  1056. // skip space in Expected
  1057. IsSpaceNeeded:=false;
  1058. if ExpectedP>PChar(Expected) then
  1059. LastChar:=ExpectedP[-1]
  1060. else
  1061. LastChar:=#0;
  1062. while ExpectedP^ in SpaceChars do inc(ExpectedP);
  1063. if (LastChar in ['a'..'z','A'..'Z','0'..'9','_','$'])
  1064. and (ExpectedP^ in ['a'..'z','A'..'Z','0'..'9','_','$']) then
  1065. IsSpaceNeeded:=true;
  1066. if IsSpaceNeeded and (not (ActualP^ in SpaceChars)) then
  1067. begin
  1068. DiffFound;
  1069. exit;
  1070. end;
  1071. while ActualP^ in SpaceChars do inc(ActualP);
  1072. end;
  1073. '''','"':
  1074. begin
  1075. while ActualP^ in SpaceChars do inc(ActualP);
  1076. if ExpectedP^<>ActualP^ then
  1077. begin
  1078. DiffFound;
  1079. exit;
  1080. end;
  1081. Quote:=ExpectedP^;
  1082. repeat
  1083. inc(ExpectedP);
  1084. inc(ActualP);
  1085. if ExpectedP^<>ActualP^ then
  1086. begin
  1087. DiffFound;
  1088. exit;
  1089. end;
  1090. if (ExpectedP^ in [#0,#10,#13]) then
  1091. break
  1092. else if (ExpectedP^=Quote) then
  1093. begin
  1094. inc(ExpectedP);
  1095. inc(ActualP);
  1096. break;
  1097. end;
  1098. until false;
  1099. end;
  1100. else
  1101. while ActualP^ in SpaceChars do inc(ActualP);
  1102. if ExpectedP^<>ActualP^ then
  1103. begin
  1104. DiffFound;
  1105. exit;
  1106. end;
  1107. inc(ExpectedP);
  1108. inc(ActualP);
  1109. end;
  1110. until false;
  1111. end;
  1112. { TTestEnginePasResolver }
  1113. destructor TTestEnginePasResolver.Destroy;
  1114. begin
  1115. FreeAndNil(FStreamResolver);
  1116. FreeAndNil(FParser);
  1117. FreeAndNil(FScanner);
  1118. FreeAndNil(FStreamResolver);
  1119. if Module<>nil then
  1120. begin
  1121. Module.Release{$IFDEF CheckPasTreeRefCount}('CreateElement'){$ENDIF};
  1122. FModule:=nil;
  1123. end;
  1124. inherited Destroy;
  1125. end;
  1126. function TTestEnginePasResolver.FindUnit(const AName, InFilename: String;
  1127. NameExpr, InFileExpr: TPasExpr): TPasModule;
  1128. begin
  1129. Result:=nil;
  1130. if InFilename<>'' then
  1131. RaiseNotYetImplemented(20180224101926,InFileExpr,'Use testcase tcunitsearch instead');
  1132. if Assigned(OnFindUnit) then
  1133. Result:=OnFindUnit(AName);
  1134. if NameExpr=nil then ;
  1135. end;
  1136. procedure TTestEnginePasResolver.UsedInterfacesFinished(Section: TPasSection);
  1137. begin
  1138. // do not parse recursively
  1139. // parse via the queue
  1140. if Section=nil then ;
  1141. end;
  1142. { TCustomTestModule }
  1143. function TCustomTestModule.GetMsgCount: integer;
  1144. begin
  1145. Result:=FHintMsgs.Count;
  1146. end;
  1147. function TCustomTestModule.GetMsgs(Index: integer): TTestHintMessage;
  1148. begin
  1149. Result:=TTestHintMessage(FHintMsgs[Index]);
  1150. end;
  1151. function TCustomTestModule.GetResolverCount: integer;
  1152. begin
  1153. Result:=FModules.Count;
  1154. end;
  1155. function TCustomTestModule.GetResolvers(Index: integer
  1156. ): TTestEnginePasResolver;
  1157. begin
  1158. Result:=TTestEnginePasResolver(FModules[Index]);
  1159. end;
  1160. function TCustomTestModule.OnPasResolverFindUnit(const aUnitName: String
  1161. ): TPasModule;
  1162. var
  1163. DefNamespace: String;
  1164. begin
  1165. //writeln('TTestModule.OnPasResolverFindUnit START Unit="',aUnitName,'"');
  1166. if (Pos('.',aUnitName)<1) then
  1167. begin
  1168. DefNamespace:=GetDefaultNamespace;
  1169. if DefNamespace<>'' then
  1170. begin
  1171. Result:=LoadUnit(DefNamespace+'.'+aUnitName);
  1172. if Result<>nil then exit;
  1173. end;
  1174. end;
  1175. Result:=LoadUnit(aUnitName);
  1176. if Result<>nil then exit;
  1177. {$IFDEF VerbosePas2JS}
  1178. writeln('TTestModule.OnPasResolverFindUnit missing unit "',aUnitName,'"');
  1179. {$ENDIF}
  1180. Fail('can''t find unit "'+aUnitName+'"');
  1181. end;
  1182. procedure TCustomTestModule.OnParserLog(Sender: TObject; const Msg: String);
  1183. var
  1184. aParser: TPasParser;
  1185. Item: TTestHintMessage;
  1186. begin
  1187. aParser:=Sender as TPasParser;
  1188. Item:=TTestHintMessage.Create;
  1189. Item.Id:=aParser.LastMsgNumber;
  1190. Item.MsgType:=aParser.LastMsgType;
  1191. Item.MsgNumber:=aParser.LastMsgNumber;
  1192. Item.Msg:=Msg;
  1193. Item.SourcePos:=aParser.Scanner.CurSourcePos;
  1194. {$IFDEF VerbosePas2JS}
  1195. writeln('TCustomTestModule.OnParserLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1196. {$ENDIF}
  1197. FHintMsgs.Add(Item);
  1198. end;
  1199. procedure TCustomTestModule.OnPasResolverLog(Sender: TObject; const Msg: String
  1200. );
  1201. var
  1202. aResolver: TTestEnginePasResolver;
  1203. Item: TTestHintMessage;
  1204. begin
  1205. aResolver:=Sender as TTestEnginePasResolver;
  1206. Item:=TTestHintMessage.Create;
  1207. Item.Id:=aResolver.LastMsgId;
  1208. Item.MsgType:=aResolver.LastMsgType;
  1209. Item.MsgNumber:=aResolver.LastMsgNumber;
  1210. Item.Msg:=Msg;
  1211. Item.SourcePos:=aResolver.LastSourcePos;
  1212. {$IFDEF VerbosePas2JS}
  1213. writeln('TCustomTestModule.OnPasResolverLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1214. {$ENDIF}
  1215. FHintMsgs.Add(Item);
  1216. end;
  1217. procedure TCustomTestModule.OnScannerLog(Sender: TObject; const Msg: String);
  1218. var
  1219. Item: TTestHintMessage;
  1220. aScanner: TPas2jsPasScanner;
  1221. begin
  1222. aScanner:=Sender as TPas2jsPasScanner;
  1223. Item:=TTestHintMessage.Create;
  1224. Item.Id:=aScanner.LastMsgNumber;
  1225. Item.MsgType:=aScanner.LastMsgType;
  1226. Item.MsgNumber:=aScanner.LastMsgNumber;
  1227. Item.Msg:=Msg;
  1228. Item.SourcePos:=aScanner.CurSourcePos;
  1229. {$IFDEF VerbosePas2JS}
  1230. writeln('TCustomTestModule.OnScannerLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1231. {$ENDIF}
  1232. FHintMsgs.Add(Item);
  1233. end;
  1234. procedure TCustomTestModule.SetWithTypeInfo(const AValue: boolean);
  1235. begin
  1236. if FWithTypeInfo=AValue then Exit;
  1237. FWithTypeInfo:=AValue;
  1238. if AValue then
  1239. Converter.Options:=Converter.Options-[coNoTypeInfo]
  1240. else
  1241. Converter.Options:=Converter.Options+[coNoTypeInfo];
  1242. end;
  1243. function TCustomTestModule.LoadUnit(const aUnitName: String): TPasModule;
  1244. var
  1245. i: Integer;
  1246. CurEngine: TTestEnginePasResolver;
  1247. CurUnitName: String;
  1248. begin
  1249. //writeln('TTestModule.FindUnit START Unit="',aUnitName,'"');
  1250. Result:=nil;
  1251. if (Module.ClassType=TPasModule)
  1252. and (CompareText(Module.Name,aUnitName)=0) then
  1253. exit(Module);
  1254. for i:=0 to ResolverCount-1 do
  1255. begin
  1256. CurEngine:=Resolvers[i];
  1257. CurUnitName:=ExtractFileUnitName(CurEngine.Filename);
  1258. //writeln('TTestModule.FindUnit Checking ',i,'/',ResolverCount,' ',CurEngine.Filename,' ',CurUnitName);
  1259. if CompareText(aUnitName,CurUnitName)=0 then
  1260. begin
  1261. Result:=CurEngine.Module;
  1262. if Result<>nil then exit;
  1263. //writeln('TTestModule.FindUnit PARSING unit "',CurEngine.Filename,'"');
  1264. FileResolver.FindSourceFile(aUnitName);
  1265. CurEngine.StreamResolver:=TStreamResolver.Create;
  1266. CurEngine.StreamResolver.OwnsStreams:=True;
  1267. //writeln('TTestModule.FindUnit SOURCE=',CurEngine.Source);
  1268. CurEngine.StreamResolver.AddStream(CurEngine.FileName,TStringStream.Create(CurEngine.Source));
  1269. CurEngine.Scanner:=TPas2jsPasScanner.Create(CurEngine.StreamResolver);
  1270. InitScanner(CurEngine.Scanner);
  1271. CurEngine.Parser:=TTestPasParser.Create(CurEngine.Scanner,CurEngine.StreamResolver,CurEngine);
  1272. CurEngine.Parser.Options:=po_tcmodules;
  1273. if CompareText(CurUnitName,'System')=0 then
  1274. CurEngine.Parser.ImplicitUses.Clear;
  1275. CurEngine.Scanner.OpenFile(CurEngine.Filename);
  1276. try
  1277. CurEngine.Parser.NextToken;
  1278. CurEngine.Parser.ParseUnit(CurEngine.FModule);
  1279. except
  1280. on E: Exception do
  1281. HandleException(E);
  1282. end;
  1283. //writeln('TTestModule.FindUnit END ',CurUnitName);
  1284. Result:=CurEngine.Module;
  1285. exit;
  1286. end;
  1287. end;
  1288. end;
  1289. procedure TCustomTestModule.SetUp;
  1290. begin
  1291. {$IFDEF EnablePasTreeGlobalRefCount}
  1292. FElementRefCountAtSetup:=TPasElement.GlobalRefCount;
  1293. {$ENDIF}
  1294. if FModules<>nil then
  1295. begin
  1296. writeln('TCustomTestModule.SetUp FModules<>nil');
  1297. Halt;
  1298. end;
  1299. inherited SetUp;
  1300. FSkipTests:=false;
  1301. FWithTypeInfo:=false;
  1302. FSource:=TStringList.Create;
  1303. FHub:=TPas2JSResolverHub.Create(Self);
  1304. FModules:=TObjectList.Create(true);
  1305. FFilename:='test1.pp';
  1306. FFileResolver:=TStreamResolver.Create;
  1307. FFileResolver.OwnsStreams:=True;
  1308. FScanner:=TPas2jsPasScanner.Create(FFileResolver);
  1309. InitScanner(FScanner);
  1310. FEngine:=AddModule(Filename);
  1311. FEngine.Scanner:=FScanner;
  1312. FScanner.Resolver:=FEngine;
  1313. FParser:=TTestPasParser.Create(FScanner,FFileResolver,FEngine);
  1314. FParser.OnLog:=@OnParserLog;
  1315. FEngine.Parser:=FParser;
  1316. Parser.Options:=po_tcmodules;
  1317. FModule:=Nil;
  1318. FConverter:=CreateConverter;
  1319. FExpectedErrorClass:=nil;
  1320. end;
  1321. function TCustomTestModule.CreateConverter: TPasToJSConverter;
  1322. var
  1323. Options: TPasToJsConverterOptions;
  1324. begin
  1325. Result:=TPasToJSConverter.Create;
  1326. Options:=co_tcmodules;
  1327. if WithTypeInfo then
  1328. Exclude(Options,coNoTypeInfo)
  1329. else
  1330. Include(Options,coNoTypeInfo);
  1331. Result.Options:=Options;
  1332. Result.Globals:=TPasToJSConverterGlobals.Create(Result);
  1333. end;
  1334. procedure TCustomTestModule.InitScanner(aScanner: TPas2jsPasScanner);
  1335. begin
  1336. aScanner.AllowedModeSwitches:=msAllPas2jsModeSwitches;
  1337. aScanner.ReadOnlyModeSwitches:=msAllPas2jsModeSwitchesReadOnly;
  1338. aScanner.CurrentModeSwitches:=OBJFPCModeSwitches*msAllPas2jsModeSwitches+msAllPas2jsModeSwitchesReadOnly;
  1339. aScanner.AllowedBoolSwitches:=bsAllPas2jsBoolSwitches;
  1340. aScanner.ReadOnlyBoolSwitches:=bsAllPas2jsBoolSwitchesReadOnly;
  1341. aScanner.CurrentBoolSwitches:=bsAllPas2jsBoolSwitchesReadOnly+[bsHints,bsNotes,bsWarnings,bsWriteableConst];
  1342. aScanner.AllowedValueSwitches:=vsAllPas2jsValueSwitches;
  1343. aScanner.ReadOnlyValueSwitches:=vsAllPas2jsValueSwitchesReadOnly;
  1344. aScanner.OnLog:=@OnScannerLog;
  1345. aScanner.CompilerVersion:='Comp.Ver.tcmodules';
  1346. end;
  1347. procedure TCustomTestModule.TearDown;
  1348. {$IFDEF CheckPasTreeRefCount}
  1349. var
  1350. El: TPasElement;
  1351. {$ENDIF}
  1352. var
  1353. i: Integer;
  1354. CurModule: TPasModule;
  1355. begin
  1356. FHintMsgs.Clear;
  1357. FHintMsgsGood.Clear;
  1358. FSkipTests:=false;
  1359. FWithTypeInfo:=false;
  1360. FJSRegModuleCall:=nil;
  1361. FJSModuleCallArgs:=nil;
  1362. FJSImplentationUses:=nil;
  1363. FJSInterfaceUses:=nil;
  1364. FJSModuleSrc:=nil;
  1365. FJSInitBody:=nil;
  1366. FreeAndNil(FJSSource);
  1367. FreeAndNil(FJSModule);
  1368. FreeAndNil(FConverter);
  1369. Engine.Clear;
  1370. FreeAndNil(FSource);
  1371. FreeAndNil(FFileResolver);
  1372. if FModules<>nil then
  1373. begin
  1374. for i:=0 to FModules.Count-1 do
  1375. begin
  1376. CurModule:=TTestEnginePasResolver(FModules[i]).Module;
  1377. if CurModule=nil then continue;
  1378. //writeln('TCustomTestModule.TearDown ReleaseUsedUnits ',CurModule.Name,' ',CurModule.RefCount,' ',CurModule.RefIds.Text);
  1379. CurModule.ReleaseUsedUnits;
  1380. end;
  1381. if FModule<>nil then
  1382. FModule.ReleaseUsedUnits;
  1383. for i:=0 to FModules.Count-1 do
  1384. begin
  1385. CurModule:=TTestEnginePasResolver(FModules[i]).Module;
  1386. if CurModule=nil then continue;
  1387. //writeln('TCustomTestModule.TearDown UsesReleased ',CurModule.Name,' ',CurModule.RefCount,' ',CurModule.RefIds.Text);
  1388. end;
  1389. FreeAndNil(FModules);
  1390. ReleaseAndNil(TPasElement(FModule){$IFDEF CheckPasTreeRefCount},'CreateElement'{$ENDIF});
  1391. FEngine:=nil;
  1392. end;
  1393. FreeAndNil(FHub);
  1394. inherited TearDown;
  1395. {$IFDEF EnablePasTreeGlobalRefCount}
  1396. if FElementRefCountAtSetup<>TPasElement.GlobalRefCount then
  1397. begin
  1398. writeln('TCustomTestModule.TearDown GlobalRefCount Was='+IntToStr(FElementRefCountAtSetup)+' Now='+IntToStr(TPasElement.GlobalRefCount));
  1399. {$IFDEF CheckPasTreeRefCount}
  1400. El:=TPasElement.FirstRefEl;
  1401. while El<>nil do
  1402. begin
  1403. writeln(' ',GetObjName(El),' RefIds.Count=',El.RefIds.Count,':');
  1404. for i:=0 to El.RefIds.Count-1 do
  1405. writeln(' ',El.RefIds[i]);
  1406. El:=El.NextRefEl;
  1407. end;
  1408. {$ENDIF}
  1409. Halt;
  1410. Fail('TCustomTestModule.TearDown Was='+IntToStr(FElementRefCountAtSetup)+' Now='+IntToStr(TPasElement.GlobalRefCount));
  1411. end;
  1412. {$ENDIF}
  1413. end;
  1414. procedure TCustomTestModule.Add(Line: string);
  1415. begin
  1416. Source.Add(Line);
  1417. end;
  1418. procedure TCustomTestModule.Add(const Lines: array of string);
  1419. var
  1420. i: Integer;
  1421. begin
  1422. for i:=low(Lines) to high(Lines) do
  1423. Add(Lines[i]);
  1424. end;
  1425. procedure TCustomTestModule.StartParsing;
  1426. var
  1427. Src: String;
  1428. begin
  1429. Src:=Source.Text;
  1430. FEngine.Source:=Src;
  1431. FileResolver.AddStream(FileName,TStringStream.Create(Src));
  1432. Scanner.OpenFile(FileName);
  1433. Writeln('// Test : ',Self.TestName);
  1434. Writeln(Src);
  1435. end;
  1436. procedure TCustomTestModule.ParseModuleQueue;
  1437. var
  1438. i: Integer;
  1439. CurResolver: TTestEnginePasResolver;
  1440. Found: Boolean;
  1441. Section: TPasSection;
  1442. begin
  1443. // parse til exception or all modules finished
  1444. while not SkipTests do
  1445. begin
  1446. Found:=false;
  1447. for i:=0 to ResolverCount-1 do
  1448. begin
  1449. CurResolver:=Resolvers[i];
  1450. if CurResolver.CurrentParser=nil then continue;
  1451. if not CurResolver.CurrentParser.CanParseContinue(Section) then
  1452. continue;
  1453. CurResolver.Parser.ParseContinue;
  1454. Found:=true;
  1455. break;
  1456. end;
  1457. if not Found then break;
  1458. end;
  1459. for i:=0 to ResolverCount-1 do
  1460. begin
  1461. CurResolver:=Resolvers[i];
  1462. if CurResolver.Parser=nil then
  1463. begin
  1464. if CurResolver.CurrentParser<>nil then
  1465. Fail('TCustomTestModule.ParseModuleQueue '+CurResolver.Filename+' '+GetObjName(CurResolver.Parser)+'=Parser<>CurrentParser='+GetObjName(CurResolver.CurrentParser));
  1466. continue;
  1467. end;
  1468. if CurResolver.Parser.CurModule<>nil then
  1469. Fail('TCustomTestModule.ParseModuleQueue '+CurResolver.Filename+' NOT FINISHED CurModule='+GetObjName(CurResolver.Parser.CurModule));
  1470. end;
  1471. end;
  1472. procedure TCustomTestModule.ParseModule;
  1473. begin
  1474. if SkipTests then exit;
  1475. FFirstPasStatement:=nil;
  1476. try
  1477. StartParsing;
  1478. Parser.ParseMain(FModule);
  1479. ParseModuleQueue;
  1480. except
  1481. on E: Exception do
  1482. HandleException(E);
  1483. end;
  1484. if SkipTests then exit;
  1485. AssertNotNull('Module resulted in Module',Module);
  1486. AssertEquals('modulename',lowercase(ChangeFileExt(FFileName,'')),lowercase(Module.Name));
  1487. TAssert.AssertSame('Has resolver',Engine,Parser.Engine);
  1488. end;
  1489. procedure TCustomTestModule.ParseProgram;
  1490. begin
  1491. if SkipTests then exit;
  1492. ParseModule;
  1493. if SkipTests then exit;
  1494. AssertEquals('Has program',TPasProgram,Module.ClassType);
  1495. FPasProgram:=TPasProgram(Module);
  1496. AssertNotNull('Has program section',PasProgram.ProgramSection);
  1497. AssertNotNull('Has initialization section',PasProgram.InitializationSection);
  1498. if (PasProgram.InitializationSection.Elements.Count>0) then
  1499. if TObject(PasProgram.InitializationSection.Elements[0]) is TPasImplBlock then
  1500. FFirstPasStatement:=TPasImplBlock(PasProgram.InitializationSection.Elements[0]);
  1501. end;
  1502. procedure TCustomTestModule.ParseLibrary;
  1503. var
  1504. Init: TInitializationSection;
  1505. begin
  1506. if SkipTests then exit;
  1507. ParseModule;
  1508. if SkipTests then exit;
  1509. AssertEquals('Has library',TPasLibrary,Module.ClassType);
  1510. FPasLibrary:=TPasLibrary(Module);
  1511. AssertNotNull('Has library section',PasLibrary.LibrarySection);
  1512. Init:=PasLibrary.InitializationSection;
  1513. if (Init<>nil) and (Init.Elements.Count>0) then
  1514. if TObject(Init.Elements[0]) is TPasImplBlock then
  1515. FFirstPasStatement:=TPasImplBlock(PasLibrary.InitializationSection.Elements[0]);
  1516. end;
  1517. procedure TCustomTestModule.ParseUnit;
  1518. begin
  1519. if SkipTests then exit;
  1520. ParseModule;
  1521. if SkipTests then exit;
  1522. AssertEquals('Has unit (TPasModule)',TPasModule,Module.ClassType);
  1523. AssertNotNull('Has interface section',Module.InterfaceSection);
  1524. AssertNotNull('Has implementation section',Module.ImplementationSection);
  1525. if (Module.InitializationSection<>nil)
  1526. and (Module.InitializationSection.Elements.Count>0)
  1527. and (TObject(Module.InitializationSection.Elements[0]) is TPasImplBlock) then
  1528. FFirstPasStatement:=TPasImplBlock(Module.InitializationSection.Elements[0]);
  1529. end;
  1530. function TCustomTestModule.FindModuleWithFilename(aFilename: string
  1531. ): TTestEnginePasResolver;
  1532. var
  1533. i: Integer;
  1534. begin
  1535. for i:=0 to ResolverCount-1 do
  1536. if CompareText(Resolvers[i].Filename,aFilename)=0 then
  1537. exit(Resolvers[i]);
  1538. Result:=nil;
  1539. end;
  1540. function TCustomTestModule.AddModule(aFilename: string
  1541. ): TTestEnginePasResolver;
  1542. begin
  1543. //writeln('TTestModuleConverter.AddModule ',aFilename);
  1544. if FindModuleWithFilename(aFilename)<>nil then
  1545. Fail('TTestModuleConverter.AddModule: file "'+aFilename+'" already exists');
  1546. Result:=TTestEnginePasResolver.Create;
  1547. Result.Filename:=aFilename;
  1548. Result.AddObjFPCBuiltInIdentifiers(btAllJSBaseTypes,bfAllJSBaseProcs);
  1549. Result.OnFindUnit:=@OnPasResolverFindUnit;
  1550. Result.OnLog:=@OnPasResolverLog;
  1551. Result.Hub:=Hub;
  1552. FModules.Add(Result);
  1553. end;
  1554. function TCustomTestModule.AddModuleWithSrc(aFilename, Src: string
  1555. ): TTestEnginePasResolver;
  1556. begin
  1557. Result:=AddModule(aFilename);
  1558. Result.Source:=Src;
  1559. end;
  1560. function TCustomTestModule.AddModuleWithIntfImplSrc(aFilename, InterfaceSrc,
  1561. ImplementationSrc: string): TTestEnginePasResolver;
  1562. var
  1563. Src: String;
  1564. begin
  1565. Src:='unit '+ExtractFileUnitName(aFilename)+';'+LineEnding;
  1566. Src+=LineEnding;
  1567. Src+='interface'+LineEnding;
  1568. Src+=LineEnding;
  1569. Src+=InterfaceSrc;
  1570. Src+='implementation'+LineEnding;
  1571. Src+=LineEnding;
  1572. Src+=ImplementationSrc;
  1573. Src+='end.'+LineEnding;
  1574. Result:=AddModuleWithSrc(aFilename,Src);
  1575. end;
  1576. procedure TCustomTestModule.AddSystemUnit(Parts: TSystemUnitParts);
  1577. var
  1578. Intf, Impl: TStringList;
  1579. begin
  1580. Intf:=TStringList.Create;
  1581. if supTInterfacedObject in Parts then Include(Parts,supTObject);
  1582. // unit interface
  1583. if [supTVarRec,supTypeInfo]*Parts<>[] then
  1584. Intf.Add('{$modeswitch externalclass}');
  1585. Intf.Add('type');
  1586. Intf.Add(' integer=longint;');
  1587. Intf.Add(' sizeint=nativeint;');
  1588. //'const',
  1589. //' LineEnding = #10;',
  1590. //' DirectorySeparator = ''/'';',
  1591. //' DriveSeparator = '''';',
  1592. //' AllowDirectorySeparators : set of char = [''\'',''/''];',
  1593. //' AllowDriveSeparators : set of char = [];',
  1594. if supTObject in Parts then
  1595. Intf.AddStrings([
  1596. 'type',
  1597. ' TClass = class of TObject;',
  1598. ' TObject = class',
  1599. ' constructor Create;',
  1600. ' destructor Destroy; virtual;',
  1601. ' class function ClassType: TClass; assembler;',
  1602. ' class function ClassName: String; assembler;',
  1603. ' class function ClassNameIs(const Name: string): boolean;',
  1604. ' class function ClassParent: TClass; assembler;',
  1605. ' class function InheritsFrom(aClass: TClass): boolean; assembler;',
  1606. ' class function UnitName: String; assembler;',
  1607. ' procedure AfterConstruction; virtual;',
  1608. ' procedure BeforeDestruction;virtual;',
  1609. ' function Equals(Obj: TObject): boolean; virtual;',
  1610. ' function ToString: String; virtual;',
  1611. ' end;']);
  1612. if supTInterfacedObject in Parts then
  1613. Intf.AddStrings([
  1614. ' {$Interfaces COM}',
  1615. ' IUnknown = interface',
  1616. ' [''{00000000-0000-0000-C000-000000000046}'']',
  1617. //' function QueryInterface(const iid: TGuid; out obj): Integer;',
  1618. ' function _AddRef: Integer;',
  1619. ' function _Release: Integer;',
  1620. ' end;',
  1621. ' IInterface = IUnknown;',
  1622. ' TInterfacedObject = class(TObject,IUnknown)',
  1623. ' protected',
  1624. ' fRefCount: Integer;',
  1625. ' { implement methods of IUnknown }',
  1626. //' function QueryInterface(const iid: TGuid; out obj): Integer; virtual;',
  1627. ' function _AddRef: Integer; virtual;',
  1628. ' function _Release: Integer; virtual;',
  1629. ' end;',
  1630. ' TInterfacedClass = class of TInterfacedObject;',
  1631. '',
  1632. '']);
  1633. if supTVarRec in Parts then
  1634. Intf.AddStrings([
  1635. 'const',
  1636. ' vtInteger = 0;',
  1637. ' vtBoolean = 1;',
  1638. ' vtJSValue = 19;',
  1639. 'type',
  1640. ' PVarRec = ^TVarRec;',
  1641. ' TVarRec = record',
  1642. ' VType : byte;',
  1643. ' VJSValue: JSValue;',
  1644. ' vInteger: longint external name ''VJSValue'';',
  1645. ' vBoolean: boolean external name ''VJSValue'';',
  1646. ' end;',
  1647. ' TVarRecArray = array of TVarRec;',
  1648. 'function VarRecs: TVarRecArray; varargs;',
  1649. '']);
  1650. if supTypeInfo in Parts then
  1651. begin
  1652. Intf.AddStrings([
  1653. 'type',
  1654. ' TTypeKind = (',
  1655. ' tkUnknown, // 0',
  1656. ' tkInteger, // 1',
  1657. ' tkChar, // 2 in Delphi/FPC tkWChar, tkUChar',
  1658. ' tkString, // 3 in Delphi/FPC tkSString, tkWString or tkUString',
  1659. ' tkEnumeration, // 4',
  1660. ' tkSet, // 5',
  1661. ' tkDouble, // 6',
  1662. ' tkBool, // 7',
  1663. ' tkProcVar, // 8 function or procedure',
  1664. ' tkMethod, // 9 proc var of object',
  1665. ' tkArray, // 10 static array',
  1666. ' tkDynArray, // 11',
  1667. ' tkRecord, // 12',
  1668. ' tkClass, // 13',
  1669. ' tkClassRef, // 14',
  1670. ' tkPointer, // 15',
  1671. ' tkJSValue, // 16',
  1672. ' tkRefToProcVar, // 17 variable of procedure type',
  1673. ' tkInterface, // 18',
  1674. ' //tkObject,',
  1675. ' //tkSString,tkLString,tkAString,tkWString,',
  1676. ' //tkVariant,',
  1677. ' //tkWChar,',
  1678. ' //tkInt64,',
  1679. ' //tkQWord,',
  1680. ' //tkInterfaceRaw,',
  1681. ' //tkUString,tkUChar,',
  1682. ' tkHelper, // 19',
  1683. ' //tkFile,',
  1684. ' tkExtClass // 20',
  1685. ' );',
  1686. ' TTypeKinds = set of TTypeKind;',
  1687. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  1688. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo)',
  1689. ' end;',
  1690. ' TTypeInfoEnum = class external name ''rtl.tTypeInfoEnum''(TTypeInfoInteger) end;',
  1691. ' TTypeInfoSet = class external name ''rtl.tTypeInfoSet''(TTypeInfo) end;',
  1692. ' TTypeInfoStaticArray = class external name ''rtl.tTypeInfoStaticArray''(TTypeInfo) end;',
  1693. ' TTypeInfoDynArray = class external name ''rtl.tTypeInfoDynArray''(TTypeInfo) end;',
  1694. ' TTypeInfoProcVar = class external name ''rtl.tTypeInfoProcVar''(TTypeInfo) end;',
  1695. ' TTypeInfoMethodVar = class external name ''rtl.tTypeInfoMethodVar''(TTypeInfoProcVar) end;',
  1696. ' TTypeInfoClass = class external name ''rtl.tTypeInfoClass''(TTypeInfo) end;',
  1697. ' TTypeInfoClassRef = class external name ''rtl.tTypeInfoClassRef''(TTypeInfo) end;',
  1698. ' TTypeInfoExtClass = class external name ''rtl.tTypeInfoExtClass''(TTypeInfo) end;',
  1699. ' TTypeInfoRecord = class external name ''rtl.tTypeInfoRecord''(TTypeInfo) end;',
  1700. ' TTypeInfoPointer = class external name ''rtl.tTypeInfoPointer''(TTypeInfo) end;',
  1701. ' TTypeInfoHelper = class external name ''rtl.tTypeInfoHelper''(TTypeInfo) end;',
  1702. ' TTypeInfoInterface = class external name ''rtl.tTypeInfoInterface''(TTypeInfo) end;',
  1703. '']);
  1704. end;
  1705. if supWriteln in Parts then
  1706. Intf.Add('procedure writeln; varargs; external name ''console.log'';');
  1707. Intf.Add('var');
  1708. Intf.Add(' ExitCode: Longint = 0;');
  1709. // unit implementation
  1710. Impl:=TStringList.Create;
  1711. if supTObject in Parts then
  1712. Impl.AddStrings([
  1713. '// needed by ClassNameIs, the real SameText is in SysUtils',
  1714. 'function SameText(const s1, s2: String): Boolean; assembler;',
  1715. 'asm',
  1716. 'end;',
  1717. 'constructor TObject.Create; begin end;',
  1718. 'destructor TObject.Destroy; begin end;',
  1719. 'class function TObject.ClassType: TClass; assembler;',
  1720. 'asm',
  1721. 'end;',
  1722. 'class function TObject.ClassName: String; assembler;',
  1723. 'asm',
  1724. 'end;',
  1725. 'class function TObject.ClassNameIs(const Name: string): boolean;',
  1726. 'begin',
  1727. ' Result:=SameText(Name,ClassName);',
  1728. 'end;',
  1729. 'class function TObject.ClassParent: TClass; assembler;',
  1730. 'asm',
  1731. 'end;',
  1732. 'class function TObject.InheritsFrom(aClass: TClass): boolean; assembler;',
  1733. 'asm',
  1734. 'end;',
  1735. 'class function TObject.UnitName: String; assembler;',
  1736. 'asm',
  1737. 'end;',
  1738. 'procedure TObject.AfterConstruction; begin end;',
  1739. 'procedure TObject.BeforeDestruction; begin end;',
  1740. 'function TObject.Equals(Obj: TObject): boolean;',
  1741. 'begin',
  1742. ' Result:=Obj=Self;',
  1743. 'end;',
  1744. 'function TObject.ToString: String;',
  1745. 'begin',
  1746. ' Result:=ClassName;',
  1747. 'end;'
  1748. ]);
  1749. if supTInterfacedObject in Parts then
  1750. Impl.AddStrings([
  1751. //'function TInterfacedObject.QueryInterface(const iid: TGuid; out obj): Integer;',
  1752. //'begin',
  1753. //'end;',
  1754. 'function TInterfacedObject._AddRef: Integer;',
  1755. 'begin',
  1756. 'end;',
  1757. 'function TInterfacedObject._Release: Integer;',
  1758. 'begin',
  1759. 'end;',
  1760. '']);
  1761. if supTVarRec in Parts then
  1762. Impl.AddStrings([
  1763. 'function VarRecs: TVarRecArray; varargs;',
  1764. 'var',
  1765. ' v: PVarRec;',
  1766. 'begin',
  1767. ' v^.VType:=1;',
  1768. ' v^.VJSValue:=2;',
  1769. 'end;',
  1770. '']);
  1771. try
  1772. AddModuleWithIntfImplSrc('system.pp',Intf.Text,Impl.Text);
  1773. finally
  1774. Intf.Free;
  1775. Impl.Free;
  1776. end;
  1777. end;
  1778. procedure TCustomTestModule.StartProgram(NeedSystemUnit: boolean;
  1779. SystemUnitParts: TSystemUnitParts);
  1780. begin
  1781. if NeedSystemUnit then
  1782. AddSystemUnit(SystemUnitParts)
  1783. else
  1784. Parser.ImplicitUses.Clear;
  1785. Add('program '+ExtractFileUnitName(Filename)+';');
  1786. Add('');
  1787. end;
  1788. procedure TCustomTestModule.StartLibrary(NeedSystemUnit: boolean;
  1789. SystemUnitParts: TSystemUnitParts);
  1790. begin
  1791. if NeedSystemUnit then
  1792. AddSystemUnit(SystemUnitParts)
  1793. else
  1794. Parser.ImplicitUses.Clear;
  1795. Add('library '+ExtractFileUnitName(Filename)+';');
  1796. Add('');
  1797. end;
  1798. procedure TCustomTestModule.StartUnit(NeedSystemUnit: boolean;
  1799. SystemUnitParts: TSystemUnitParts);
  1800. begin
  1801. if NeedSystemUnit then
  1802. AddSystemUnit(SystemUnitParts)
  1803. else
  1804. Parser.ImplicitUses.Clear;
  1805. Add('unit Test1;');
  1806. Add('');
  1807. end;
  1808. procedure TCustomTestModule.ConvertModule;
  1809. procedure CheckUsesList(UsesName: String; Arg: TJSArrayLiteralElement;
  1810. out UsesLit: TJSArrayLiteral);
  1811. var
  1812. i: Integer;
  1813. Item: TJSElement;
  1814. Lit: TJSLiteral;
  1815. begin
  1816. UsesLit:=nil;
  1817. AssertNotNull(UsesName+' uses section',Arg.Expr);
  1818. if (Arg.Expr.ClassType=TJSLiteral) and TJSLiteral(Arg.Expr).Value.IsNull then
  1819. exit; // null is ok
  1820. AssertEquals(UsesName+' uses section param is array',TJSArrayLiteral,Arg.Expr.ClassType);
  1821. FJSInterfaceUses:=TJSArrayLiteral(Arg.Expr);
  1822. for i:=0 to FJSInterfaceUses.Elements.Count-1 do
  1823. begin
  1824. Item:=FJSInterfaceUses.Elements.Elements[i].Expr;
  1825. AssertNotNull(UsesName+' uses section item['+IntToStr(i)+'].Expr',Item);
  1826. AssertEquals(UsesName+' uses section item['+IntToStr(i)+'] is lit',TJSLiteral,Item.ClassType);
  1827. Lit:=TJSLiteral(Item);
  1828. AssertEquals(UsesName+' uses section item['+IntToStr(i)+'] is string lit',
  1829. ord(jsbase.jstString),ord(Lit.Value.ValueType));
  1830. end;
  1831. end;
  1832. procedure CheckFunctionParam(ParamName: string; Arg: TJSArrayLiteralElement;
  1833. out Src: TJSSourceElements);
  1834. var
  1835. FunDecl: TJSFunctionDeclarationStatement;
  1836. FunDef: TJSFuncDef;
  1837. FunBody: TJSFunctionBody;
  1838. begin
  1839. Src:=nil;
  1840. AssertNotNull(ParamName,Arg.Expr);
  1841. AssertEquals(ParamName+' Arg.Expr type',TJSFunctionDeclarationStatement,Arg.Expr.ClassType);
  1842. FunDecl:=Arg.Expr as TJSFunctionDeclarationStatement;
  1843. AssertNotNull(ParamName+' FunDecl.AFunction',FunDecl.AFunction);
  1844. AssertEquals(ParamName+' FunDecl.AFunction type',TJSFuncDef,FunDecl.AFunction.ClassType);
  1845. FunDef:=FunDecl.AFunction as TJSFuncDef;
  1846. AssertEquals(ParamName+' name empty','',String(FunDef.Name));
  1847. AssertNotNull(ParamName+' body',FunDef.Body);
  1848. AssertEquals(ParamName+' body type',TJSFunctionBody,FunDef.Body.ClassType);
  1849. FunBody:=FunDef.Body as TJSFunctionBody;
  1850. AssertNotNull(ParamName+' body.A',FunBody.A);
  1851. AssertEquals(ParamName+' body.A type',TJSSourceElements,FunBody.A.ClassType);
  1852. Src:=FunBody.A as TJSSourceElements;
  1853. end;
  1854. var
  1855. ModuleNameExpr: TJSLiteral;
  1856. InitFunction: TJSFunctionDeclarationStatement;
  1857. InitAssign: TJSSimpleAssignStatement;
  1858. InitName: String;
  1859. LastNode: TJSElement;
  1860. Arg: TJSArrayLiteralElement;
  1861. begin
  1862. if SkipTests then exit;
  1863. try
  1864. FJSModule:=FConverter.ConvertPasElement(Module,Engine) as TJSSourceElements;
  1865. except
  1866. on E: Exception do
  1867. HandleException(E);
  1868. end;
  1869. if SkipTests then exit;
  1870. if ExpectedErrorClass<>nil then
  1871. Fail('Missing '+ExpectedErrorClass.ClassName+' error {'+ExpectedErrorMsg+'} ('+IntToStr(ExpectedErrorNumber)+')');
  1872. FJSSource:=TStringList.Create;
  1873. FJSSource.Text:=ConvertJSModuleToString(JSModule);
  1874. {$IFDEF VerbosePas2JS}
  1875. writeln('TTestModule.ConvertModule JS:');
  1876. write(FJSSource.Text);
  1877. {$ENDIF}
  1878. // rtl.module(...
  1879. AssertEquals('jsmodule has one statement - the call',1,JSModule.Statements.Count);
  1880. AssertNotNull('register module call',JSModule.Statements.Nodes[0].Node);
  1881. AssertEquals('register module call',TJSCallExpression,JSModule.Statements.Nodes[0].Node.ClassType);
  1882. FJSRegModuleCall:=JSModule.Statements.Nodes[0].Node as TJSCallExpression;
  1883. AssertNotNull('register module rtl.module expr',JSRegModuleCall.Expr);
  1884. AssertNotNull('register module rtl.module args',JSRegModuleCall.Args);
  1885. AssertEquals('rtl.module args',TJSArguments,JSRegModuleCall.Args.ClassType);
  1886. FJSModuleCallArgs:=JSRegModuleCall.Args as TJSArguments;
  1887. // parameter 'unitname'
  1888. if JSModuleCallArgs.Elements.Count<1 then
  1889. Fail('rtl.module first param unit missing');
  1890. Arg:=JSModuleCallArgs.Elements.Elements[0];
  1891. AssertNotNull('module name param',Arg.Expr);
  1892. ModuleNameExpr:=Arg.Expr as TJSLiteral;
  1893. AssertEquals('module name param is string',ord(jstString),ord(ModuleNameExpr.Value.ValueType));
  1894. if Module is TPasProgram then
  1895. AssertEquals('module name','program',String(ModuleNameExpr.Value.AsString))
  1896. else if Module is TPasLibrary then
  1897. AssertEquals('module name','library',String(ModuleNameExpr.Value.AsString))
  1898. else
  1899. AssertEquals('module name',Module.Name,String(ModuleNameExpr.Value.AsString));
  1900. // main uses section
  1901. if JSModuleCallArgs.Elements.Count<2 then
  1902. Fail('rtl.module second param main uses missing');
  1903. Arg:=JSModuleCallArgs.Elements.Elements[1];
  1904. CheckUsesList('interface',Arg,FJSInterfaceUses);
  1905. // program/library/interface function()
  1906. if JSModuleCallArgs.Elements.Count<3 then
  1907. Fail('rtl.module third param intf-function missing');
  1908. Arg:=JSModuleCallArgs.Elements.Elements[2];
  1909. CheckFunctionParam('module intf-function',Arg,FJSModuleSrc);
  1910. // search for $mod.$init or $mod.$main - the last statement
  1911. if (Module is TPasProgram) or (Module is TPasLibrary) then
  1912. begin
  1913. InitName:='$main';
  1914. AssertEquals('$mod.'+InitName+' function 1',true,JSModuleSrc.Statements.Count>0);
  1915. end
  1916. else
  1917. InitName:='$init';
  1918. FJSInitBody:=nil;
  1919. if JSModuleSrc.Statements.Count>0 then
  1920. begin
  1921. LastNode:=JSModuleSrc.Statements.Nodes[JSModuleSrc.Statements.Count-1].Node;
  1922. if LastNode is TJSSimpleAssignStatement then
  1923. begin
  1924. InitAssign:=LastNode as TJSSimpleAssignStatement;
  1925. if GetDottedIdentifier(InitAssign.LHS)='$mod.'+InitName then
  1926. begin
  1927. InitFunction:=InitAssign.Expr as TJSFunctionDeclarationStatement;
  1928. FJSInitBody:=InitFunction.AFunction.Body as TJSFunctionBody;
  1929. end
  1930. else if (Module is TPasProgram) or (Module is TPasLibrary) then
  1931. CheckDottedIdentifier('init function',InitAssign.LHS,'$mod.'+InitName);
  1932. end;
  1933. end;
  1934. // optional: implementation uses section
  1935. if JSModuleCallArgs.Elements.Count<4 then
  1936. exit;
  1937. Arg:=JSModuleCallArgs.Elements.Elements[3];
  1938. CheckUsesList('implementation',Arg,FJSImplentationUses);
  1939. end;
  1940. procedure TCustomTestModule.ConvertProgram;
  1941. begin
  1942. Add('end.');
  1943. ParseProgram;
  1944. ConvertModule;
  1945. end;
  1946. procedure TCustomTestModule.ConvertLibrary;
  1947. begin
  1948. Add('end.');
  1949. ParseLibrary;
  1950. ConvertModule;
  1951. end;
  1952. procedure TCustomTestModule.ConvertUnit;
  1953. begin
  1954. Add('end.');
  1955. ParseUnit;
  1956. ConvertModule;
  1957. end;
  1958. function TCustomTestModule.ConvertJSModuleToString(El: TJSElement): string;
  1959. begin
  1960. Result:=tcmodules.JSToStr(El);
  1961. end;
  1962. procedure TCustomTestModule.CheckDottedIdentifier(Msg: string; El: TJSElement;
  1963. DottedName: string);
  1964. begin
  1965. if DottedName='' then
  1966. begin
  1967. AssertNull(Msg,El);
  1968. end
  1969. else
  1970. begin
  1971. AssertNotNull(Msg,El);
  1972. AssertEquals(Msg,DottedName,GetDottedIdentifier(El));
  1973. end;
  1974. end;
  1975. function TCustomTestModule.GetDottedIdentifier(El: TJSElement): string;
  1976. begin
  1977. if El=nil then
  1978. Result:=''
  1979. else if El is TJSPrimaryExpressionIdent then
  1980. Result:=String(TJSPrimaryExpressionIdent(El).Name)
  1981. else if El is TJSDotMemberExpression then
  1982. Result:=GetDottedIdentifier(TJSDotMemberExpression(El).MExpr)+'.'+String(TJSDotMemberExpression(El).Name)
  1983. else
  1984. AssertEquals('GetDottedIdentifier',TJSPrimaryExpressionIdent,El.ClassType);
  1985. end;
  1986. procedure TCustomTestModule.CheckSource(Msg, Statements: String;
  1987. InitStatements: string; ImplStatements: string);
  1988. var
  1989. ActualSrc, ExpectedSrc, InitName: String;
  1990. begin
  1991. ActualSrc:=JSToStr(JSModuleSrc);
  1992. if coUseStrict in Converter.Options then
  1993. ExpectedSrc:='"use strict";'+LineEnding
  1994. else
  1995. ExpectedSrc:='';
  1996. ExpectedSrc:=ExpectedSrc+'var $mod = this;'+LineEnding;
  1997. ExpectedSrc:=ExpectedSrc+Statements;
  1998. // unit implementation
  1999. if (Trim(ImplStatements)<>'') then
  2000. ExpectedSrc:=ExpectedSrc+LineEnding
  2001. +'$mod.$implcode = function () {'+LineEnding
  2002. +ImplStatements
  2003. +'};'+LineEnding;
  2004. // program main or unit initialization
  2005. if (Module is TPasProgram) or (Trim(InitStatements)<>'') then
  2006. begin
  2007. if (Module is TPasProgram) or (Module is TPasLibrary) then
  2008. InitName:='$main'
  2009. else
  2010. InitName:='$init';
  2011. ExpectedSrc:=ExpectedSrc+LineEnding
  2012. +'$mod.'+InitName+' = function () {'+LineEnding
  2013. +InitStatements
  2014. +'};'+LineEnding;
  2015. end;
  2016. //writeln('TCustomTestModule.CheckSource ExpectedIntf="',ExpectedSrc,'"');
  2017. //writeln('TTestModule.CheckSource InitStatements="',Trim(InitStatements),'"');
  2018. CheckDiff(Msg,ExpectedSrc,ActualSrc);
  2019. end;
  2020. procedure TCustomTestModule.CheckDiff(Msg, Expected, Actual: string);
  2021. // search diff, ignore changes in spaces
  2022. var
  2023. s: string;
  2024. begin
  2025. if CheckSrcDiff(Expected,Actual,s) then exit;
  2026. Fail(Msg+': '+s);
  2027. end;
  2028. procedure TCustomTestModule.CheckUnit(Filename, ExpectedSrc: string);
  2029. var
  2030. aResolver: TTestEnginePasResolver;
  2031. aConverter: TPasToJSConverter;
  2032. aJSModule: TJSSourceElements;
  2033. ActualSrc: String;
  2034. begin
  2035. aResolver:=GetResolver(Filename);
  2036. AssertNotNull('missing resolver of unit '+Filename,aResolver);
  2037. AssertNotNull('missing resolver.module of unit '+Filename,aResolver.Module);
  2038. {$IFDEF VerbosePas2JS}
  2039. writeln('CheckUnit '+Filename+' converting ...');
  2040. {$ENDIF}
  2041. aConverter:=CreateConverter;
  2042. aJSModule:=nil;
  2043. try
  2044. try
  2045. aJSModule:=aConverter.ConvertPasElement(aResolver.Module,aResolver) as TJSSourceElements;
  2046. except
  2047. on E: Exception do
  2048. HandleException(E);
  2049. end;
  2050. ActualSrc:=ConvertJSModuleToString(aJSModule);
  2051. {$IFDEF VerbosePas2JS}
  2052. writeln('TTestModule.CheckUnit ',Filename,' Pas:');
  2053. write(aResolver.Source);
  2054. writeln('TTestModule.CheckUnit ',Filename,' JS:');
  2055. write(ActualSrc);
  2056. {$ENDIF}
  2057. CheckDiff('Converted unit: "'+ChangeFileExt(Filename,'.js')+'"',ExpectedSrc,ActualSrc);
  2058. finally
  2059. aJSModule.Free;
  2060. aConverter.Free;
  2061. end;
  2062. end;
  2063. procedure TCustomTestModule.CheckHint(MsgType: TMessageType;
  2064. MsgNumber: integer; Msg: string; Marker: PSrcMarker);
  2065. var
  2066. i: Integer;
  2067. Item: TTestHintMessage;
  2068. Expected,Actual: string;
  2069. begin
  2070. //writeln('TCustomTestModule.CheckHint MsgCount=',MsgCount);
  2071. for i:=0 to MsgCount-1 do
  2072. begin
  2073. Item:=Msgs[i];
  2074. if (Item.MsgNumber<>MsgNumber) or (Item.Msg<>Msg) then continue;
  2075. if (Marker<>nil) then
  2076. begin
  2077. if Item.SourcePos.Row<>cardinal(Marker^.Row) then continue;
  2078. if (Item.SourcePos.Column<cardinal(Marker^.StartCol))
  2079. or (Item.SourcePos.Column>cardinal(Marker^.EndCol)) then continue;
  2080. end;
  2081. // found
  2082. FHintMsgsGood.Add(Item);
  2083. str(Item.MsgType,Actual);
  2084. str(MsgType,Expected);
  2085. AssertEquals('MsgType',Expected,Actual);
  2086. exit;
  2087. end;
  2088. // needed message missing -> show emitted messages
  2089. WriteSources('',0,0);
  2090. for i:=0 to MsgCount-1 do
  2091. begin
  2092. Item:=Msgs[i];
  2093. write('TCustomTestModule.CheckHint ',i,'/',MsgCount,' ',Item.MsgType,
  2094. ' ('+IntToStr(Item.MsgNumber),')');
  2095. if Marker<>nil then
  2096. write(' '+ExtractFileName(Item.SourcePos.FileName),'(',Item.SourcePos.Row,',',Item.SourcePos.Column,')');
  2097. writeln(' {',Item.Msg,'}');
  2098. end;
  2099. str(MsgType,Expected);
  2100. Actual:='Missing '+Expected+' ('+IntToStr(MsgNumber)+')';
  2101. if Marker<>nil then
  2102. Actual:=Actual+' '+ExtractFileName(Marker^.Filename)+'('+IntToStr(Marker^.Row)+','+IntToStr(Marker^.StartCol)+'..'+IntToStr(Marker^.EndCol)+')';
  2103. Actual:=Actual+' '+Msg;
  2104. Fail(Actual);
  2105. end;
  2106. procedure TCustomTestModule.CheckResolverUnexpectedHints(WithSourcePos: boolean
  2107. );
  2108. var
  2109. i: Integer;
  2110. s, Txt: String;
  2111. Msg: TTestHintMessage;
  2112. begin
  2113. for i:=0 to MsgCount-1 do
  2114. begin
  2115. Msg:=Msgs[i];
  2116. if FHintMsgsGood.IndexOf(Msg)>=0 then continue;
  2117. s:='';
  2118. str(Msg.MsgType,s);
  2119. Txt:='Unexpected resolver message found ['+IntToStr(Msg.Id)+'] '
  2120. +s+': ('+IntToStr(Msg.MsgNumber)+')';
  2121. if WithSourcePos then
  2122. Txt:=Txt+' '+ExtractFileName(Msg.SourcePos.FileName)+'('+IntToStr(Msg.SourcePos.Row)+','+IntToStr(Msg.SourcePos.Column)+')';
  2123. Txt:=Txt+' {'+Msg.Msg+'}';
  2124. Fail(Txt);
  2125. end;
  2126. end;
  2127. procedure TCustomTestModule.SetExpectedScannerError(Msg: string;
  2128. MsgNumber: integer);
  2129. begin
  2130. ExpectedErrorClass:=EScannerError;
  2131. ExpectedErrorMsg:=Msg;
  2132. ExpectedErrorNumber:=MsgNumber;
  2133. end;
  2134. procedure TCustomTestModule.SetExpectedParserError(Msg: string;
  2135. MsgNumber: integer);
  2136. begin
  2137. ExpectedErrorClass:=EParserError;
  2138. ExpectedErrorMsg:=Msg;
  2139. ExpectedErrorNumber:=MsgNumber;
  2140. end;
  2141. procedure TCustomTestModule.SetExpectedPasResolverError(Msg: string;
  2142. MsgNumber: integer);
  2143. begin
  2144. ExpectedErrorClass:=EPasResolve;
  2145. ExpectedErrorMsg:=Msg;
  2146. ExpectedErrorNumber:=MsgNumber;
  2147. end;
  2148. procedure TCustomTestModule.SetExpectedConverterError(Msg: string;
  2149. MsgNumber: integer);
  2150. begin
  2151. ExpectedErrorClass:=EPas2JS;
  2152. ExpectedErrorMsg:=Msg;
  2153. ExpectedErrorNumber:=MsgNumber;
  2154. end;
  2155. function TCustomTestModule.IsErrorExpected(E: Exception): boolean;
  2156. var
  2157. MsgNumber: Integer;
  2158. Msg: String;
  2159. begin
  2160. Result:=false;
  2161. if (ExpectedErrorClass=nil) or (ExpectedErrorClass<>E.ClassType) then exit;
  2162. Msg:=E.Message;
  2163. if E is EPas2JS then
  2164. MsgNumber:=EPas2JS(E).MsgNumber
  2165. else if E is EPasResolve then
  2166. MsgNumber:=EPasResolve(E).MsgNumber
  2167. else if E is EParserError then
  2168. MsgNumber:=Parser.LastMsgNumber
  2169. else if E is EScannerError then
  2170. begin
  2171. MsgNumber:=Scanner.LastMsgNumber;
  2172. Msg:=Scanner.LastMsg;
  2173. end
  2174. else
  2175. MsgNumber:=0;
  2176. Result:=(MsgNumber=ExpectedErrorNumber) and (Msg=ExpectedErrorMsg);
  2177. if Result then
  2178. SkipTests:=true;
  2179. end;
  2180. procedure TCustomTestModule.HandleScannerError(E: EScannerError);
  2181. begin
  2182. if IsErrorExpected(E) then exit;
  2183. WriteSources(Scanner.CurFilename,Scanner.CurRow,Scanner.CurColumn);
  2184. writeln('ERROR: TCustomTestModule.HandleScannerError '+E.ClassName+':'+E.Message
  2185. +' '+Scanner.CurFilename
  2186. +'('+IntToStr(Scanner.CurRow)+','+IntToStr(Scanner.CurColumn)+')');
  2187. FailException(E);
  2188. end;
  2189. procedure TCustomTestModule.HandleParserError(E: EParserError);
  2190. begin
  2191. if IsErrorExpected(E) then exit;
  2192. WriteSources(E.Filename,E.Row,E.Column);
  2193. writeln('ERROR: TCustomTestModule.HandleParserError '+E.ClassName+':'+E.Message
  2194. +' '+E.Filename+'('+IntToStr(E.Row)+','+IntToStr(E.Column)+')'
  2195. +' MainModuleScannerLine="'+Scanner.CurLine+'"'
  2196. );
  2197. FailException(E);
  2198. end;
  2199. procedure TCustomTestModule.HandlePasResolveError(E: EPasResolve);
  2200. var
  2201. P: TPasSourcePos;
  2202. begin
  2203. if IsErrorExpected(E) then exit;
  2204. P:=E.SourcePos;
  2205. WriteSources(P.FileName,P.Row,P.Column);
  2206. writeln('ERROR: TCustomTestModule.HandlePasResolveError '+E.ClassName+':'+E.Message
  2207. +' '+P.FileName+'('+IntToStr(P.Row)+','+IntToStr(P.Column)+')');
  2208. FailException(E);
  2209. end;
  2210. procedure TCustomTestModule.HandlePas2JSError(E: EPas2JS);
  2211. var
  2212. Row, Col: integer;
  2213. begin
  2214. if IsErrorExpected(E) then exit;
  2215. Engine.UnmangleSourceLineNumber(E.PasElement.SourceLinenumber,Row,Col);
  2216. WriteSources(E.PasElement.SourceFilename,Row,Col);
  2217. writeln('ERROR: TCustomTestModule.HandlePas2JSError '+E.ClassName+':'+E.Message
  2218. +' '+E.PasElement.SourceFilename
  2219. +'('+IntToStr(Row)+','+IntToStr(Col)+')');
  2220. FailException(E);
  2221. end;
  2222. procedure TCustomTestModule.HandleException(E: Exception);
  2223. begin
  2224. if E is EScannerError then
  2225. HandleScannerError(EScannerError(E))
  2226. else if E is EParserError then
  2227. HandleParserError(EParserError(E))
  2228. else if E is EPasResolve then
  2229. HandlePasResolveError(EPasResolve(E))
  2230. else if E is EPas2JS then
  2231. HandlePas2JSError(EPas2JS(E))
  2232. else
  2233. begin
  2234. if IsErrorExpected(E) then exit;
  2235. if not (E is EAssertionFailedError) then
  2236. begin
  2237. WriteSources('',0,0);
  2238. writeln('ERROR: TCustomTestModule.HandleException '+E.ClassName+':'+E.Message);
  2239. end;
  2240. FailException(E);
  2241. end;
  2242. end;
  2243. procedure TCustomTestModule.FailException(E: Exception);
  2244. var
  2245. MsgNumber: Integer;
  2246. begin
  2247. if ExpectedErrorClass<>nil then
  2248. begin
  2249. if FExpectedErrorClass=E.ClassType then
  2250. begin
  2251. if E is EPas2JS then
  2252. MsgNumber:=EPas2JS(E).MsgNumber
  2253. else if E is EPasResolve then
  2254. MsgNumber:=EPasResolve(E).MsgNumber
  2255. else if E is EParserError then
  2256. MsgNumber:=Parser.LastMsgNumber
  2257. else if E is EScannerError then
  2258. MsgNumber:=Scanner.LastMsgNumber
  2259. else
  2260. MsgNumber:=0;
  2261. AssertEquals('Expected error message ('+IntToStr(ExpectedErrorNumber)+')','{'+ExpectedErrorMsg+'}','{'+E.Message+'}');
  2262. AssertEquals('Expected {'+ExpectedErrorMsg+'}, but got msg {'+E.Message+'} number',
  2263. ExpectedErrorNumber,MsgNumber);
  2264. end else begin
  2265. AssertEquals('Wrong exception class',ExpectedErrorClass.ClassName,E.ClassName);
  2266. end;
  2267. end;
  2268. Fail(E.Message);
  2269. end;
  2270. procedure TCustomTestModule.WriteSources(const aFilename: string; aRow,
  2271. aCol: integer);
  2272. var
  2273. IsSrc: Boolean;
  2274. i, j: Integer;
  2275. SrcLines: TStringList;
  2276. Line: string;
  2277. aModule: TTestEnginePasResolver;
  2278. begin
  2279. writeln('TCustomTestModule.WriteSources File="',aFilename,'" Row=',aRow,' Col=',aCol);
  2280. for i:=0 to ResolverCount-1 do
  2281. begin
  2282. aModule:=Resolvers[i];
  2283. SrcLines:=TStringList.Create;
  2284. try
  2285. SrcLines.Text:=aModule.Source;
  2286. IsSrc:=ExtractFilename(aModule.Filename)=ExtractFileName(aFilename);
  2287. writeln('Testcode:-File="',aModule.Filename,'"----------------------------------:');
  2288. for j:=1 to SrcLines.Count do
  2289. begin
  2290. Line:=SrcLines[j-1];
  2291. if IsSrc and (j=aRow) then
  2292. begin
  2293. write('*');
  2294. Line:=LeftStr(Line,aCol-1)+'|'+copy(Line,aCol,length(Line));
  2295. end;
  2296. writeln(Format('%:4d: ',[j]),Line);
  2297. end;
  2298. finally
  2299. SrcLines.Free;
  2300. end;
  2301. end;
  2302. end;
  2303. function TCustomTestModule.IndexOfResolver(const Filename: string): integer;
  2304. var
  2305. i: Integer;
  2306. begin
  2307. for i:=0 to ResolverCount-1 do
  2308. if Filename=Resolvers[i].Filename then exit(i);
  2309. Result:=-1;
  2310. end;
  2311. function TCustomTestModule.GetResolver(const Filename: string
  2312. ): TTestEnginePasResolver;
  2313. var
  2314. i: Integer;
  2315. begin
  2316. i:=IndexOfResolver(Filename);
  2317. if i<0 then exit(nil);
  2318. Result:=Resolvers[i];
  2319. end;
  2320. function TCustomTestModule.GetDefaultNamespace: string;
  2321. var
  2322. C: TClass;
  2323. begin
  2324. Result:='';
  2325. if FModule=nil then exit;
  2326. C:=FModule.ClassType;
  2327. if (C=TPasProgram) or (C=TPasLibrary) or (C=TPasPackage) then
  2328. Result:=Engine.DefaultNameSpace;
  2329. end;
  2330. constructor TCustomTestModule.Create;
  2331. begin
  2332. inherited Create;
  2333. FHintMsgs:=TObjectList.Create(true);
  2334. FHintMsgsGood:=TFPList.Create;
  2335. end;
  2336. destructor TCustomTestModule.Destroy;
  2337. begin
  2338. FreeAndNil(FHintMsgs);
  2339. FreeAndNil(FHintMsgsGood);
  2340. inherited Destroy;
  2341. end;
  2342. { TTestModule }
  2343. procedure TTestModule.TestReservedWords;
  2344. var
  2345. i: integer;
  2346. begin
  2347. for i:=low(JSReservedWords) to High(JSReservedWords)-1 do
  2348. if CompareStr(JSReservedWords[i],JSReservedWords[i+1])>=0 then
  2349. Fail('20170203135442 '+JSReservedWords[i]+' >= '+JSReservedWords[i+1]);
  2350. for i:=low(JSReservedGlobalWords) to High(JSReservedGlobalWords)-1 do
  2351. if CompareStr(JSReservedGlobalWords[i],JSReservedGlobalWords[i+1])>=0 then
  2352. Fail('20170203135443 '+JSReservedGlobalWords[i]+' >= '+JSReservedGlobalWords[i+1]);
  2353. end;
  2354. procedure TTestModule.TestEmptyProgram;
  2355. begin
  2356. StartProgram(false);
  2357. Add('begin');
  2358. ConvertProgram;
  2359. CheckSource('TestEmptyProgram','','');
  2360. end;
  2361. procedure TTestModule.TestEmptyProgramUseStrict;
  2362. begin
  2363. Converter.Options:=Converter.Options+[coUseStrict];
  2364. StartProgram(false);
  2365. Add('begin');
  2366. ConvertProgram;
  2367. CheckSource('TestEmptyProgramUseStrict','','');
  2368. end;
  2369. procedure TTestModule.TestEmptyUnit;
  2370. begin
  2371. StartUnit(false);
  2372. Add('interface');
  2373. Add('implementation');
  2374. ConvertUnit;
  2375. CheckSource('TestEmptyUnit',
  2376. LinesToStr([
  2377. ]),
  2378. '');
  2379. end;
  2380. procedure TTestModule.TestEmptyUnitUseStrict;
  2381. begin
  2382. Converter.Options:=Converter.Options+[coUseStrict];
  2383. StartUnit(false);
  2384. Add('interface');
  2385. Add('implementation');
  2386. ConvertUnit;
  2387. CheckSource('TestEmptyUnitUseStrict',
  2388. LinesToStr([
  2389. ''
  2390. ]),
  2391. '');
  2392. end;
  2393. procedure TTestModule.TestDottedUnitNames;
  2394. begin
  2395. AddModuleWithIntfImplSrc('NS1.Unit2.pas',
  2396. LinesToStr([
  2397. 'var iV: longint;'
  2398. ]),
  2399. '');
  2400. FFilename:='ns1.test1.pp';
  2401. StartProgram(true);
  2402. Add('uses unIt2;');
  2403. Add('var');
  2404. Add(' i: longint;');
  2405. Add('begin');
  2406. Add(' i:=iv;');
  2407. Add(' i:=uNit2.iv;');
  2408. Add(' i:=Ns1.TEst1.i;');
  2409. ConvertProgram;
  2410. CheckSource('TestDottedUnitNames',
  2411. LinesToStr([
  2412. 'this.i = 0;',
  2413. '']),
  2414. LinesToStr([ // this.$init
  2415. '$mod.i = pas["NS1.Unit2"].iV;',
  2416. '$mod.i = pas["NS1.Unit2"].iV;',
  2417. '$mod.i = $mod.i;',
  2418. '']) );
  2419. end;
  2420. procedure TTestModule.TestDottedUnitNameImpl;
  2421. begin
  2422. AddModuleWithIntfImplSrc('TEST.UnitA.pas',
  2423. LinesToStr([
  2424. 'type',
  2425. ' TObject = class end;',
  2426. ' TTestA = class',
  2427. ' end;'
  2428. ]),
  2429. LinesToStr(['uses TEST.UnitB;'])
  2430. );
  2431. AddModuleWithIntfImplSrc('TEST.UnitB.pas',
  2432. LinesToStr([
  2433. 'uses TEST.UnitA;',
  2434. 'type TTestB = class(TTestA);'
  2435. ]),
  2436. ''
  2437. );
  2438. StartProgram(true);
  2439. Add('uses TEST.UnitA;');
  2440. Add('begin');
  2441. ConvertProgram;
  2442. CheckSource('TestDottedUnitNameImpl',
  2443. LinesToStr([
  2444. '']),
  2445. LinesToStr([ // this.$init
  2446. '']) );
  2447. CheckUnit('TEST.UnitA.pas',
  2448. LinesToStr([
  2449. 'rtl.module("TEST.UnitA", ["system"], function () {',
  2450. ' var $mod = this;',
  2451. ' rtl.createClass(this, "TObject", null, function () {',
  2452. ' this.$init = function () {',
  2453. ' };',
  2454. ' this.$final = function () {',
  2455. ' };',
  2456. ' });',
  2457. ' rtl.createClass(this, "TTestA", this.TObject, function () {',
  2458. ' });',
  2459. '}, ["TEST.UnitB"]);'
  2460. ]));
  2461. CheckUnit('TEST.UnitB.pas',
  2462. LinesToStr([
  2463. 'rtl.module("TEST.UnitB", ["system","TEST.UnitA"], function () {',
  2464. ' var $mod = this;',
  2465. ' rtl.createClass(this, "TTestB", pas["TEST.UnitA"].TTestA, function () {',
  2466. ' });',
  2467. '});'
  2468. ]));
  2469. end;
  2470. procedure TTestModule.TestDottedUnitExpr;
  2471. begin
  2472. AddModuleWithIntfImplSrc('NS2.SubNs2.Unit2.pas',
  2473. LinesToStr([
  2474. 'procedure DoIt;'
  2475. ]),
  2476. 'procedure DoIt; begin end;');
  2477. FFilename:='Ns1.SubNs1.Test1.pp';
  2478. StartProgram(true);
  2479. Add('uses Ns2.sUbnS2.unIt2;');
  2480. Add('var');
  2481. Add(' i: longint;');
  2482. Add('begin');
  2483. Add(' ns2.subns2.unit2.doit;');
  2484. Add(' i:=Ns1.SubNS1.TEst1.i;');
  2485. ConvertProgram;
  2486. CheckSource('TestDottedUnitExpr',
  2487. LinesToStr([
  2488. 'this.i = 0;',
  2489. '']),
  2490. LinesToStr([ // this.$init
  2491. 'pas["NS2.SubNs2.Unit2"].DoIt();',
  2492. '$mod.i = $mod.i;',
  2493. '']) );
  2494. end;
  2495. procedure TTestModule.Test_ModeFPCFail;
  2496. begin
  2497. StartProgram(false);
  2498. Add('{$mode FPC}');
  2499. Add('begin');
  2500. SetExpectedScannerError('Invalid mode: "FPC"',nErrInvalidMode);
  2501. ConvertProgram;
  2502. end;
  2503. procedure TTestModule.Test_ModeSwitchCBlocksFail;
  2504. begin
  2505. StartProgram(false);
  2506. Add('{$modeswitch cblocks-}');
  2507. Add('begin');
  2508. ConvertProgram;
  2509. CheckHint(mtWarning,nErrInvalidModeSwitch,'Warning: test1.pp(3,23) : Invalid mode switch: "cblocks"');
  2510. CheckResolverUnexpectedHints();
  2511. end;
  2512. procedure TTestModule.TestUnit_UseSystem;
  2513. begin
  2514. StartUnit(true);
  2515. Add([
  2516. 'interface',
  2517. 'var i: integer;',
  2518. 'implementation']);
  2519. ConvertUnit;
  2520. CheckSource('TestUnit_UseSystem',
  2521. LinesToStr([
  2522. 'this.i = 0;',
  2523. '']),
  2524. LinesToStr([
  2525. '']) );
  2526. end;
  2527. procedure TTestModule.TestUnit_Intf1Impl2Intf1;
  2528. begin
  2529. AddModuleWithIntfImplSrc('unit1.pp',
  2530. LinesToStr([
  2531. 'type number = longint;']),
  2532. LinesToStr([
  2533. 'uses test1;',
  2534. 'procedure DoIt;',
  2535. 'begin',
  2536. ' i:=3;',
  2537. 'end;']));
  2538. StartUnit(true);
  2539. Add([
  2540. 'interface',
  2541. 'uses unit1;',
  2542. 'var i: number;',
  2543. 'implementation']);
  2544. ConvertUnit;
  2545. CheckSource('TestUnit_Intf1Impl2Intf1',
  2546. LinesToStr([
  2547. 'this.i = 0;',
  2548. '']),
  2549. LinesToStr([
  2550. '']) );
  2551. end;
  2552. procedure TTestModule.TestIncludeVersion;
  2553. begin
  2554. StartProgram(false);
  2555. Add([
  2556. 'var',
  2557. ' s: string;',
  2558. ' i: word;',
  2559. 'begin',
  2560. ' s:={$I %line%};',
  2561. ' i:={$I %linenum%};',
  2562. ' s:={$I %currentroutine%};',
  2563. ' s:={$I %pas2jsversion%};',
  2564. ' s:={$I %pas2jstarget%};',
  2565. ' s:={$I %pas2jstargetos%};',
  2566. ' s:={$I %pas2jstargetcpu%};',
  2567. ' s:={$I %file%};',
  2568. '']);
  2569. ConvertProgram;
  2570. CheckSource('TestIncludeVersion',
  2571. LinesToStr([
  2572. 'this.s="";',
  2573. 'this.i = 0;']),
  2574. LinesToStr([
  2575. '$mod.s = "7";',
  2576. '$mod.i = 8;',
  2577. '$mod.s = "<anonymous>";',
  2578. '$mod.s = "Comp.Ver.tcmodules";',
  2579. '$mod.s = "Browser";',
  2580. '$mod.s = "Browser";',
  2581. '$mod.s = "ECMAScript5";',
  2582. '$mod.s = "test1.pp";',
  2583. '']));
  2584. end;
  2585. procedure TTestModule.TestVarInt;
  2586. begin
  2587. StartProgram(false);
  2588. Add('var MyI: longint;');
  2589. Add('begin');
  2590. ConvertProgram;
  2591. CheckSource('TestVarInt','this.MyI=0;','');
  2592. end;
  2593. procedure TTestModule.TestVarBaseTypes;
  2594. begin
  2595. StartProgram(false);
  2596. Add('var');
  2597. Add(' i: longint;');
  2598. Add(' s: string;');
  2599. Add(' c: char;');
  2600. Add(' b: boolean;');
  2601. Add(' d: double;');
  2602. Add(' i2: longint = 3;');
  2603. Add(' s2: string = ''foo'';');
  2604. Add(' c2: char = ''4'';');
  2605. Add(' b2: boolean = true;');
  2606. Add(' d2: double = 5.6;');
  2607. Add(' i3: longint = $707;');
  2608. Add(' i4: nativeint = 9007199254740991;');
  2609. Add(' i5: nativeint = -9007199254740991-1;');
  2610. Add(' i6: nativeint = $fffffffffffff;');
  2611. Add(' i7: nativeint = -$fffffffffffff-1;');
  2612. Add(' i8: byte = 00;');
  2613. Add(' u8: nativeuint = $fffffffffffff;');
  2614. Add(' u9: nativeuint = $0000000000000;');
  2615. Add(' u10: nativeuint = $00ff00;');
  2616. Add('begin');
  2617. ConvertProgram;
  2618. CheckSource('TestVarBaseTypes',
  2619. LinesToStr([
  2620. 'this.i = 0;',
  2621. 'this.s = "";',
  2622. 'this.c = "";',
  2623. 'this.b = false;',
  2624. 'this.d = 0.0;',
  2625. 'this.i2 = 3;',
  2626. 'this.s2 = "foo";',
  2627. 'this.c2 = "4";',
  2628. 'this.b2 = true;',
  2629. 'this.d2 = 5.6;',
  2630. 'this.i3 = 0x707;',
  2631. 'this.i4 = 9007199254740991;',
  2632. 'this.i5 = -9007199254740991-1;',
  2633. 'this.i6 = 0xfffffffffffff;',
  2634. 'this.i7 =-0xfffffffffffff-1;',
  2635. 'this.i8 = 0;',
  2636. 'this.u8 = 0xfffffffffffff;',
  2637. 'this.u9 = 0x0;',
  2638. 'this.u10 = 0xff00;'
  2639. ]),
  2640. '');
  2641. end;
  2642. procedure TTestModule.TestBaseTypeSingleFail;
  2643. begin
  2644. StartProgram(false);
  2645. Add('var s: single;');
  2646. SetExpectedPasResolverError('identifier not found "single"',PasResolveEval.nIdentifierNotFound);
  2647. ConvertProgram;
  2648. end;
  2649. procedure TTestModule.TestBaseTypeExtendedFail;
  2650. begin
  2651. StartProgram(false);
  2652. Add('var e: extended;');
  2653. SetExpectedPasResolverError('identifier not found "extended"',PasResolveEval.nIdentifierNotFound);
  2654. ConvertProgram;
  2655. end;
  2656. procedure TTestModule.TestConstBaseTypes;
  2657. begin
  2658. StartProgram(false);
  2659. Add('const');
  2660. Add(' i: longint = 3;');
  2661. Add(' s: string = ''foo'';');
  2662. Add(' c: char = ''4'';');
  2663. Add(' b: boolean = true;');
  2664. Add(' d: double = 5.6;');
  2665. Add(' e = low(word);');
  2666. Add(' f = high(word);');
  2667. Add('begin');
  2668. ConvertProgram;
  2669. CheckSource('TestVarBaseTypes',
  2670. LinesToStr([
  2671. 'this.i=3;',
  2672. 'this.s="foo";',
  2673. 'this.c="4";',
  2674. 'this.b=true;',
  2675. 'this.d=5.6;',
  2676. 'this.e = 0;',
  2677. 'this.f = 65535;'
  2678. ]),
  2679. '');
  2680. end;
  2681. procedure TTestModule.TestAliasTypeRef;
  2682. begin
  2683. StartProgram(false);
  2684. Add('type');
  2685. Add(' a=longint;');
  2686. Add(' b=a;');
  2687. Add('var');
  2688. Add(' c: A;');
  2689. Add(' d: B;');
  2690. Add('begin');
  2691. ConvertProgram;
  2692. CheckSource('TestAliasTypeRef',
  2693. LinesToStr([ // statements
  2694. 'this.c = 0;',
  2695. 'this.d = 0;'
  2696. ]),
  2697. LinesToStr([ // this.$main
  2698. ''
  2699. ]));
  2700. end;
  2701. procedure TTestModule.TestTypeCast_BaseTypes;
  2702. begin
  2703. StartProgram(false);
  2704. Add([
  2705. 'var',
  2706. ' i: longint;',
  2707. ' b: boolean;',
  2708. ' d: double;',
  2709. ' s: string;',
  2710. ' c: char;',
  2711. 'begin',
  2712. ' i:=longint(i);',
  2713. ' i:=longint(b);',
  2714. ' b:=boolean(b);',
  2715. ' b:=boolean(i);',
  2716. ' d:=double(d);',
  2717. ' d:=double(i);',
  2718. ' s:=string(s);',
  2719. ' s:=string(c);',
  2720. ' c:=char(c);',
  2721. ' c:=char(i);',
  2722. ' c:=char(65);',
  2723. ' c:=char(#10);',
  2724. ' c:=char(#$E000);',
  2725. '']);
  2726. ConvertProgram;
  2727. CheckSource('TestAliasTypeRef',
  2728. LinesToStr([ // statements
  2729. 'this.i = 0;',
  2730. 'this.b = false;',
  2731. 'this.d = 0.0;',
  2732. 'this.s = "";',
  2733. 'this.c = "";',
  2734. '']),
  2735. LinesToStr([ // this.$main
  2736. '$mod.i = $mod.i;',
  2737. '$mod.i = ($mod.b ? 1 : 0);',
  2738. '$mod.b = $mod.b;',
  2739. '$mod.b = $mod.i != 0;',
  2740. '$mod.d = $mod.d;',
  2741. '$mod.d = $mod.i;',
  2742. '$mod.s = $mod.s;',
  2743. '$mod.s = $mod.c;',
  2744. '$mod.c = $mod.c;',
  2745. '$mod.c = String.fromCharCode($mod.i);',
  2746. '$mod.c = "A";',
  2747. '$mod.c = "\n";',
  2748. '$mod.c = "";',
  2749. '']));
  2750. end;
  2751. procedure TTestModule.TestTypeCast_AliasBaseTypes;
  2752. begin
  2753. StartProgram(false);
  2754. Add('type');
  2755. Add(' integer = longint;');
  2756. Add(' TYesNo = boolean;');
  2757. Add(' TFloat = double;');
  2758. Add(' TCaption = string;');
  2759. Add(' TChar = char;');
  2760. Add('var');
  2761. Add(' i: integer;');
  2762. Add(' b: TYesNo;');
  2763. Add(' d: TFloat;');
  2764. Add(' s: TCaption;');
  2765. Add(' c: TChar;');
  2766. Add('begin');
  2767. Add(' i:=integer(i);');
  2768. Add(' i:=integer(b);');
  2769. Add(' b:=TYesNo(b);');
  2770. Add(' b:=TYesNo(i);');
  2771. Add(' d:=TFloat(d);');
  2772. Add(' d:=TFloat(i);');
  2773. Add(' s:=TCaption(s);');
  2774. Add(' s:=TCaption(c);');
  2775. Add(' c:=TChar(c);');
  2776. ConvertProgram;
  2777. CheckSource('TestAliasTypeRef',
  2778. LinesToStr([ // statements
  2779. 'this.i = 0;',
  2780. 'this.b = false;',
  2781. 'this.d = 0.0;',
  2782. 'this.s = "";',
  2783. 'this.c = "";',
  2784. '']),
  2785. LinesToStr([ // this.$main
  2786. '$mod.i = $mod.i;',
  2787. '$mod.i = ($mod.b ? 1 : 0);',
  2788. '$mod.b = $mod.b;',
  2789. '$mod.b = $mod.i != 0;',
  2790. '$mod.d = $mod.d;',
  2791. '$mod.d = $mod.i;',
  2792. '$mod.s = $mod.s;',
  2793. '$mod.s = $mod.c;',
  2794. '$mod.c = $mod.c;',
  2795. '']));
  2796. end;
  2797. procedure TTestModule.TestEmptyProc;
  2798. begin
  2799. StartProgram(false);
  2800. Add('procedure Test;');
  2801. Add('begin');
  2802. Add('end;');
  2803. Add('begin');
  2804. ConvertProgram;
  2805. CheckSource('TestEmptyProc',
  2806. LinesToStr([ // statements
  2807. 'this.Test = function () {',
  2808. '};'
  2809. ]),
  2810. LinesToStr([ // this.$main
  2811. ''
  2812. ]));
  2813. end;
  2814. procedure TTestModule.TestProcOneParam;
  2815. begin
  2816. StartProgram(false);
  2817. Add('procedure ProcA(i: longint);');
  2818. Add('begin');
  2819. Add('end;');
  2820. Add('begin');
  2821. Add(' PROCA(3);');
  2822. ConvertProgram;
  2823. CheckSource('TestProcOneParam',
  2824. LinesToStr([ // statements
  2825. 'this.ProcA = function (i) {',
  2826. '};'
  2827. ]),
  2828. LinesToStr([ // this.$main
  2829. '$mod.ProcA(3);'
  2830. ]));
  2831. end;
  2832. procedure TTestModule.TestFunctionWithoutParams;
  2833. begin
  2834. StartProgram(false);
  2835. Add('function FuncA: longint;');
  2836. Add('begin');
  2837. Add('end;');
  2838. Add('var i: longint;');
  2839. Add('begin');
  2840. Add(' I:=FUNCA();');
  2841. Add(' I:=FUNCA;');
  2842. Add(' FUNCA();');
  2843. Add(' FUNCA;');
  2844. ConvertProgram;
  2845. CheckSource('TestProcWithoutParams',
  2846. LinesToStr([ // statements
  2847. 'this.FuncA = function () {',
  2848. ' var Result = 0;',
  2849. ' return Result;',
  2850. '};',
  2851. 'this.i=0;'
  2852. ]),
  2853. LinesToStr([ // this.$main
  2854. '$mod.i=$mod.FuncA();',
  2855. '$mod.i=$mod.FuncA();',
  2856. '$mod.FuncA();',
  2857. '$mod.FuncA();'
  2858. ]));
  2859. end;
  2860. procedure TTestModule.TestProcedureWithoutParams;
  2861. begin
  2862. StartProgram(false);
  2863. Add('procedure ProcA;');
  2864. Add('begin');
  2865. Add('end;');
  2866. Add('begin');
  2867. Add(' PROCA();');
  2868. Add(' PROCA;');
  2869. ConvertProgram;
  2870. CheckSource('TestProcWithoutParams',
  2871. LinesToStr([ // statements
  2872. 'this.ProcA = function () {',
  2873. '};'
  2874. ]),
  2875. LinesToStr([ // this.$main
  2876. '$mod.ProcA();',
  2877. '$mod.ProcA();'
  2878. ]));
  2879. end;
  2880. procedure TTestModule.TestIncDec;
  2881. begin
  2882. StartProgram(false);
  2883. Add([
  2884. 'procedure DoIt(var i: longint);',
  2885. 'begin',
  2886. ' inc(i);',
  2887. ' inc(i,2);',
  2888. 'end;',
  2889. 'var',
  2890. ' Bar: longint;',
  2891. 'begin',
  2892. ' inc(bar);',
  2893. ' inc(bar,2);',
  2894. ' dec(bar);',
  2895. ' dec(bar,3);',
  2896. '']);
  2897. ConvertProgram;
  2898. CheckSource('TestIncDec',
  2899. LinesToStr([ // statements
  2900. 'this.DoIt = function (i) {',
  2901. ' i.set(i.get()+1);',
  2902. ' i.set(i.get()+2);',
  2903. '};',
  2904. 'this.Bar = 0;'
  2905. ]),
  2906. LinesToStr([ // this.$main
  2907. '$mod.Bar+=1;',
  2908. '$mod.Bar+=2;',
  2909. '$mod.Bar-=1;',
  2910. '$mod.Bar-=3;'
  2911. ]));
  2912. end;
  2913. procedure TTestModule.TestLoHiFpcMode;
  2914. begin
  2915. StartProgram(false);
  2916. Add([
  2917. '{$mode objfpc}',
  2918. 'const',
  2919. ' LoByte1 = Lo(Word($1234));',
  2920. ' HiByte1 = Hi(Word($1234));',
  2921. ' LoByte2 = Lo(SmallInt($1234));',
  2922. ' HiByte2 = Hi(SmallInt($1234));',
  2923. ' LoWord1 = Lo($1234CDEF);',
  2924. ' HiWord1 = Hi($1234CDEF);',
  2925. ' LoWord2 = Lo(-$1234CDEF);',
  2926. ' HiWord2 = Hi(-$1234CDEF);',
  2927. ' lo4:byte=lo(byte($34));',
  2928. ' hi4:byte=hi(byte($34));',
  2929. ' lo5:byte=lo(shortint(-$34));',
  2930. ' hi5:byte=hi(shortint(-$34));',
  2931. ' lo6:longword=lo($123456789ABCD);',
  2932. ' hi6:longword=hi($123456789ABCD);',
  2933. ' lo7:longword=lo(-$123456789ABCD);',
  2934. ' hi7:longword=hi(-$123456789ABCD);',
  2935. 'var',
  2936. ' b: Byte;',
  2937. ' ss: shortint;',
  2938. ' w: Word;',
  2939. ' si: SmallInt;',
  2940. ' lw: LongWord;',
  2941. ' li: LongInt;',
  2942. ' b2: Byte;',
  2943. ' ni: nativeint;',
  2944. 'begin',
  2945. ' w := $1234;',
  2946. ' ss := -$12;',
  2947. ' b := lo(ss);',
  2948. ' b := HI(ss);',
  2949. ' b := lo(w);',
  2950. ' b := HI(w);',
  2951. ' b2 := lo(b);',
  2952. ' b2 := hi(b);',
  2953. ' lw := $1234CDEF;',
  2954. ' w := lo(lw);',
  2955. ' w := hi(lw);',
  2956. ' ni := $123456789ABCD;',
  2957. ' lw := lo(ni);',
  2958. ' lw := hi(ni);',
  2959. '']);
  2960. ConvertProgram;
  2961. CheckSource('TestLoHiFpcMode',
  2962. LinesToStr([ // statements
  2963. 'this.LoByte1 = 0x1234 & 0xFF;',
  2964. 'this.HiByte1 = (0x1234 >> 8) & 0xFF;',
  2965. 'this.LoByte2 = 0x1234 & 0xFF;',
  2966. 'this.HiByte2 = (0x1234 >> 8) & 0xFF;',
  2967. 'this.LoWord1 = 0x1234CDEF & 0xFFFF;',
  2968. 'this.HiWord1 = (0x1234CDEF >> 16) & 0xFFFF;',
  2969. 'this.LoWord2 = -0x1234CDEF & 0xFFFF;',
  2970. 'this.HiWord2 = (-0x1234CDEF >> 16) & 0xFFFF;',
  2971. 'this.lo4 = 0x34 & 0xF;',
  2972. 'this.hi4 = (0x34 >> 4) & 0xF;',
  2973. 'this.lo5 = (((-0x34 & 255) << 24) >> 24) & 0xFF;',
  2974. 'this.hi5 = ((((-0x34 & 255) << 24) >> 24) >> 8) & 0xFF;',
  2975. 'this.lo6 = 0x123456789ABCD >>> 0;',
  2976. 'this.hi6 = 74565 >>> 0;',
  2977. 'this.lo7 = -0x123456789ABCD >>> 0;',
  2978. 'this.hi7 = Math.floor(-0x123456789ABCD / 4294967296) >>> 0;',
  2979. 'this.b = 0;',
  2980. 'this.ss = 0;',
  2981. 'this.w = 0;',
  2982. 'this.si = 0;',
  2983. 'this.lw = 0;',
  2984. 'this.li = 0;',
  2985. 'this.b2 = 0;',
  2986. 'this.ni = 0;',
  2987. '']),
  2988. LinesToStr([ // this.$main
  2989. '$mod.w = 0x1234;',
  2990. '$mod.ss = -0x12;',
  2991. '$mod.b = $mod.ss & 0xFF;',
  2992. '$mod.b = ($mod.ss >> 8) & 0xFF;',
  2993. '$mod.b = $mod.w & 0xFF;',
  2994. '$mod.b = ($mod.w >> 8) & 0xFF;',
  2995. '$mod.b2 = $mod.b & 0xF;',
  2996. '$mod.b2 = ($mod.b >> 4) & 0xF;',
  2997. '$mod.lw = 0x1234CDEF;',
  2998. '$mod.w = $mod.lw & 0xFFFF;',
  2999. '$mod.w = ($mod.lw >> 16) & 0xFFFF;',
  3000. '$mod.ni = 0x123456789ABCD;',
  3001. '$mod.lw = $mod.ni >>> 0;',
  3002. '$mod.lw = Math.floor($mod.ni / 4294967296) >>> 0;',
  3003. '']));
  3004. end;
  3005. procedure TTestModule.TestLoHiDelphiMode;
  3006. begin
  3007. StartProgram(false);
  3008. Add([
  3009. '{$mode delphi}',
  3010. 'const',
  3011. ' LoByte1 = Lo(Word($1234));',
  3012. ' HiByte1 = Hi(Word($1234));',
  3013. ' LoByte2 = Lo(SmallInt($1234));',
  3014. ' HiByte2 = Hi(SmallInt($1234));',
  3015. ' LoByte3 = Lo($1234CDEF);',
  3016. ' HiByte3 = Hi($1234CDEF);',
  3017. ' LoByte4 = Lo(-$1234CDEF);',
  3018. ' HiByte4 = Hi(-$1234CDEF);',
  3019. 'var',
  3020. ' b: Byte;',
  3021. ' w: Word;',
  3022. ' si: SmallInt;',
  3023. ' lw: LongWord;',
  3024. ' li: LongInt;',
  3025. 'begin',
  3026. ' w := $1234;',
  3027. ' b := lo(w);',
  3028. ' b := HI(w);',
  3029. ' lw := $1234CDEF;',
  3030. ' b := lo(lw);',
  3031. ' b := hi(lw);',
  3032. '']);
  3033. ConvertProgram;
  3034. CheckSource('TestLoHiDelphiMode',
  3035. LinesToStr([ // statements
  3036. 'this.LoByte1 = 0x1234 & 0xFF;',
  3037. 'this.HiByte1 = (0x1234 >> 8) & 0xFF;',
  3038. 'this.LoByte2 = 0x1234 & 0xFF;',
  3039. 'this.HiByte2 = (0x1234 >> 8) & 0xFF;',
  3040. 'this.LoByte3 = 0x1234CDEF & 0xFF;',
  3041. 'this.HiByte3 = (0x1234CDEF >> 8) & 0xFF;',
  3042. 'this.LoByte4 = -0x1234CDEF & 0xFF;',
  3043. 'this.HiByte4 = (-0x1234CDEF >> 8) & 0xFF;',
  3044. 'this.b = 0;',
  3045. 'this.w = 0;',
  3046. 'this.si = 0;',
  3047. 'this.lw = 0;',
  3048. 'this.li = 0;'
  3049. ]),
  3050. LinesToStr([ // this.$main
  3051. '$mod.w = 0x1234;',
  3052. '$mod.b = $mod.w & 0xFF;',
  3053. '$mod.b = ($mod.w >> 8) & 0xFF;',
  3054. '$mod.lw = 0x1234CDEF;',
  3055. '$mod.b = $mod.lw & 0xFF;',
  3056. '$mod.b = ($mod.lw >> 8) & 0xFF;'
  3057. ]));
  3058. end;
  3059. procedure TTestModule.TestAssignments;
  3060. begin
  3061. StartProgram(false);
  3062. Parser.Options:=Parser.Options+[po_cassignments];
  3063. Add('var');
  3064. Add(' Bar:longint;');
  3065. Add('begin');
  3066. Add(' bar:=3;');
  3067. Add(' bar+=4;');
  3068. Add(' bar-=5;');
  3069. Add(' bar*=6;');
  3070. ConvertProgram;
  3071. CheckSource('TestAssignments',
  3072. LinesToStr([ // statements
  3073. 'this.Bar = 0;'
  3074. ]),
  3075. LinesToStr([ // this.$main
  3076. '$mod.Bar=3;',
  3077. '$mod.Bar+=4;',
  3078. '$mod.Bar-=5;',
  3079. '$mod.Bar*=6;'
  3080. ]));
  3081. end;
  3082. procedure TTestModule.TestArithmeticOperators1;
  3083. begin
  3084. StartProgram(false);
  3085. Add('var');
  3086. Add(' vA,vB,vC:longint;');
  3087. Add('begin');
  3088. Add(' va:=1;');
  3089. Add(' vb:=va+va;');
  3090. Add(' vb:=va div vb;');
  3091. Add(' vb:=va mod vb;');
  3092. Add(' vb:=va+va*vb+va div vb;');
  3093. Add(' vc:=-va;');
  3094. Add(' va:=va-vb;');
  3095. Add(' vb:=va;');
  3096. Add(' if va<vb then vc:=va else vc:=vb;');
  3097. ConvertProgram;
  3098. CheckSource('TestArithmeticOperators1',
  3099. LinesToStr([ // statements
  3100. 'this.vA = 0;',
  3101. 'this.vB = 0;',
  3102. 'this.vC = 0;'
  3103. ]),
  3104. LinesToStr([ // this.$main
  3105. '$mod.vA = 1;',
  3106. '$mod.vB = $mod.vA + $mod.vA;',
  3107. '$mod.vB = rtl.trunc($mod.vA / $mod.vB);',
  3108. '$mod.vB = $mod.vA % $mod.vB;',
  3109. '$mod.vB = $mod.vA + ($mod.vA * $mod.vB) + rtl.trunc($mod.vA / $mod.vB);',
  3110. '$mod.vC = -$mod.vA;',
  3111. '$mod.vA = $mod.vA - $mod.vB;',
  3112. '$mod.vB = $mod.vA;',
  3113. 'if ($mod.vA < $mod.vB){ $mod.vC = $mod.vA } else $mod.vC = $mod.vB;'
  3114. ]));
  3115. end;
  3116. procedure TTestModule.TestMultiAdd;
  3117. begin
  3118. StartProgram(false);
  3119. Add([
  3120. 'function Fly: string; external name ''fly'';',
  3121. 'function TryEncodeDate(Year, Month, Day: Word): Boolean;',
  3122. 'var',
  3123. ' Date: double;',
  3124. 'begin',
  3125. ' Result:=(Year>0) and (Year<10000) and',
  3126. ' (Month >= 1) and (Month<=12) and',
  3127. ' (Day>0) and (Day<=31);',
  3128. ' Date := (146097*Year) SHR 2 + (1461*Year) SHR 2 + (153*LongWord(Month)+2) DIV 5 + LongWord(Day);',
  3129. 'end;',
  3130. 'var s: string;',
  3131. 'begin',
  3132. ' s:=''a''+''b''+''c''+''d'';',
  3133. ' s:=s+Fly+''e'';',
  3134. ' s:=Fly+Fly+Fly;',
  3135. '']);
  3136. ConvertProgram;
  3137. CheckSource('TestMultiAdd',
  3138. LinesToStr([ // statements
  3139. 'this.TryEncodeDate = function (Year, Month, Day) {',
  3140. ' var Result = false;',
  3141. ' var date = 0.0;',
  3142. ' Result = (Year > 0) && (Year < 10000) && (Month >= 1) && (Month <= 12) && (Day > 0) && (Day <= 31);',
  3143. ' date = ((146097 * Year) >>> 2) + ((1461 * Year) >>> 2) + rtl.trunc(((153 * Month) + 2) / 5) + Day;',
  3144. ' return Result;',
  3145. '};',
  3146. 'this.s = "";',
  3147. '']),
  3148. LinesToStr([ // this.$main
  3149. '$mod.s = "a" + "b" + "c" + "d";',
  3150. '$mod.s = $mod.s + fly() + "e";',
  3151. '$mod.s = fly() + fly() + fly();',
  3152. '']));
  3153. end;
  3154. procedure TTestModule.TestLogicalOperators;
  3155. begin
  3156. StartProgram(false);
  3157. Add('var');
  3158. Add(' vA,vB,vC:boolean;');
  3159. Add('begin');
  3160. Add(' va:=vb and vc;');
  3161. Add(' va:=vb or vc;');
  3162. Add(' va:=vb xor vc;');
  3163. Add(' va:=true and vc;');
  3164. Add(' va:=(vb and vc) or (va and vb);');
  3165. Add(' va:=not vb;');
  3166. ConvertProgram;
  3167. CheckSource('TestLogicalOperators',
  3168. LinesToStr([ // statements
  3169. 'this.vA = false;',
  3170. 'this.vB = false;',
  3171. 'this.vC = false;'
  3172. ]),
  3173. LinesToStr([ // this.$main
  3174. '$mod.vA = $mod.vB && $mod.vC;',
  3175. '$mod.vA = $mod.vB || $mod.vC;',
  3176. '$mod.vA = $mod.vB ^ $mod.vC;',
  3177. '$mod.vA = true && $mod.vC;',
  3178. '$mod.vA = ($mod.vB && $mod.vC) || ($mod.vA && $mod.vB);',
  3179. '$mod.vA = !$mod.vB;'
  3180. ]));
  3181. end;
  3182. procedure TTestModule.TestBitwiseOperators;
  3183. begin
  3184. StartProgram(false);
  3185. Add([
  3186. 'var',
  3187. ' vA,vB,vC:longint;',
  3188. ' X,Y,Z: nativeint;',
  3189. 'begin',
  3190. ' va:=vb and vc;',
  3191. ' va:=vb or vc;',
  3192. ' va:=vb xor vc;',
  3193. ' va:=vb shl vc;',
  3194. ' va:=vb shr vc;',
  3195. ' va:=3 and vc;',
  3196. ' va:=(vb and vc) or (va and vb);',
  3197. ' va:=not vb;',
  3198. ' X:=Y and Z;',
  3199. ' X:=Y and va;',
  3200. ' X:=Y or Z;',
  3201. ' X:=Y or va;',
  3202. ' X:=Y xor Z;',
  3203. ' X:=Y xor va;',
  3204. '']);
  3205. ConvertProgram;
  3206. CheckSource('TestBitwiseOperators',
  3207. LinesToStr([ // statements
  3208. 'this.vA = 0;',
  3209. 'this.vB = 0;',
  3210. 'this.vC = 0;',
  3211. 'this.X = 0;',
  3212. 'this.Y = 0;',
  3213. 'this.Z = 0;',
  3214. '']),
  3215. LinesToStr([ // this.$main
  3216. '$mod.vA = $mod.vB & $mod.vC;',
  3217. '$mod.vA = $mod.vB | $mod.vC;',
  3218. '$mod.vA = $mod.vB ^ $mod.vC;',
  3219. '$mod.vA = $mod.vB << $mod.vC;',
  3220. '$mod.vA = $mod.vB >>> $mod.vC;',
  3221. '$mod.vA = 3 & $mod.vC;',
  3222. '$mod.vA = ($mod.vB & $mod.vC) | ($mod.vA & $mod.vB);',
  3223. '$mod.vA = ~$mod.vB;',
  3224. '$mod.X = rtl.and($mod.Y, $mod.Z);',
  3225. '$mod.X = $mod.Y & $mod.vA;',
  3226. '$mod.X = rtl.or($mod.Y, $mod.Z);',
  3227. '$mod.X = rtl.or($mod.Y, $mod.vA);',
  3228. '$mod.X = rtl.xor($mod.Y, $mod.Z);',
  3229. '$mod.X = rtl.xor($mod.Y, $mod.vA);',
  3230. '']));
  3231. end;
  3232. procedure TTestModule.TestBitwiseOperatorsLongword;
  3233. begin
  3234. StartProgram(false);
  3235. Add([
  3236. 'var',
  3237. ' a,b,c:longword;',
  3238. ' i: longint;',
  3239. 'begin',
  3240. ' a:=$12345678;',
  3241. ' b:=$EDCBA987;',
  3242. ' c:=not a;',
  3243. ' c:=a and b;',
  3244. ' c:=a and $ffff0000;',
  3245. ' c:=a or b;',
  3246. ' c:=a or $ff00ff00;',
  3247. ' c:=a xor b;',
  3248. ' c:=a xor $f0f0f0f0;',
  3249. ' c:=a shl 1;',
  3250. ' c:=a shl 16;',
  3251. ' c:=a shl 24;',
  3252. ' c:=a shl b;',
  3253. ' c:=a shr 1;',
  3254. ' c:=a shr 16;',
  3255. ' c:=a shr 24;',
  3256. ' c:=a shr b;',
  3257. ' c:=(b and c) or (a and b);',
  3258. ' c:=i and a;',
  3259. ' c:=i or a;',
  3260. ' c:=i xor a;',
  3261. '']);
  3262. ConvertProgram;
  3263. CheckSource('TestBitwiseOperatorsLongword',
  3264. LinesToStr([ // statements
  3265. 'this.a = 0;',
  3266. 'this.b = 0;',
  3267. 'this.c = 0;',
  3268. 'this.i = 0;',
  3269. '']),
  3270. LinesToStr([ // this.$main
  3271. '$mod.a = 0x12345678;',
  3272. '$mod.b = 0xEDCBA987;',
  3273. '$mod.c = rtl.lw(~$mod.a);',
  3274. '$mod.c = rtl.lw($mod.a & $mod.b);',
  3275. '$mod.c = rtl.lw($mod.a & 0xffff0000);',
  3276. '$mod.c = rtl.lw($mod.a | $mod.b);',
  3277. '$mod.c = rtl.lw($mod.a | 0xff00ff00);',
  3278. '$mod.c = rtl.lw($mod.a ^ $mod.b);',
  3279. '$mod.c = rtl.lw($mod.a ^ 0xf0f0f0f0);',
  3280. '$mod.c = rtl.lw($mod.a << 1);',
  3281. '$mod.c = rtl.lw($mod.a << 16);',
  3282. '$mod.c = rtl.lw($mod.a << 24);',
  3283. '$mod.c = rtl.lw($mod.a << $mod.b);',
  3284. '$mod.c = rtl.lw($mod.a >>> 1);',
  3285. '$mod.c = rtl.lw($mod.a >>> 16);',
  3286. '$mod.c = rtl.lw($mod.a >>> 24);',
  3287. '$mod.c = rtl.lw($mod.a >>> $mod.b);',
  3288. '$mod.c = rtl.lw(rtl.lw($mod.b & $mod.c) | rtl.lw($mod.a & $mod.b));',
  3289. '$mod.c = $mod.i & $mod.a;',
  3290. '$mod.c = $mod.i | $mod.a;',
  3291. '$mod.c = $mod.i ^ $mod.a;',
  3292. '']));
  3293. end;
  3294. procedure TTestModule.TestPrgProcVar;
  3295. begin
  3296. StartProgram(false);
  3297. Add('procedure Proc1;');
  3298. Add('type');
  3299. Add(' t1=longint;');
  3300. Add('var');
  3301. Add(' vA:t1;');
  3302. Add('begin');
  3303. Add('end;');
  3304. Add('begin');
  3305. ConvertProgram;
  3306. CheckSource('TestPrgProcVar',
  3307. LinesToStr([ // statements
  3308. 'this.Proc1 = function () {',
  3309. ' var vA=0;',
  3310. '};'
  3311. ]),
  3312. LinesToStr([ // this.$main
  3313. ''
  3314. ]));
  3315. end;
  3316. procedure TTestModule.TestUnitProcVar;
  3317. begin
  3318. StartUnit(false);
  3319. Add('interface');
  3320. Add('');
  3321. Add('type tA=string; // unit scope');
  3322. Add('procedure Proc1;');
  3323. Add('');
  3324. Add('implementation');
  3325. Add('');
  3326. Add('procedure Proc1;');
  3327. Add('type tA=longint; // local proc scope');
  3328. Add('var v1:tA; // using local tA');
  3329. Add('begin');
  3330. Add('end;');
  3331. Add('var v2:tA; // using interface tA');
  3332. ConvertUnit;
  3333. CheckSource('TestUnitProcVar',
  3334. LinesToStr([ // statements
  3335. 'var $impl = $mod.$impl;',
  3336. 'this.Proc1 = function () {',
  3337. ' var v1 = 0;',
  3338. '};',
  3339. '']),
  3340. // this.$init
  3341. '',
  3342. // implementation
  3343. LinesToStr([
  3344. '$impl.v2 = "";',
  3345. '']));
  3346. end;
  3347. procedure TTestModule.TestImplProc;
  3348. begin
  3349. StartUnit(false);
  3350. Add('interface');
  3351. Add('');
  3352. Add('procedure Proc1;');
  3353. Add('');
  3354. Add('implementation');
  3355. Add('');
  3356. Add('procedure Proc1; begin end;');
  3357. Add('procedure Proc2; begin end;');
  3358. Add('initialization');
  3359. Add(' Proc1;');
  3360. Add(' Proc2;');
  3361. ConvertUnit;
  3362. CheckSource('TestImplProc',
  3363. LinesToStr([ // statements
  3364. 'var $impl = $mod.$impl;',
  3365. 'this.Proc1 = function () {',
  3366. '};',
  3367. '']),
  3368. LinesToStr([ // this.$init
  3369. '$mod.Proc1();',
  3370. '$impl.Proc2();',
  3371. '']),
  3372. LinesToStr([ // implementation
  3373. '$impl.Proc2 = function () {',
  3374. '};',
  3375. ''])
  3376. );
  3377. end;
  3378. procedure TTestModule.TestFunctionResult;
  3379. begin
  3380. StartProgram(false);
  3381. Add('function Func1: longint;');
  3382. Add('begin');
  3383. Add(' Result:=3;');
  3384. Add(' Func1:=4;');
  3385. Add('end;');
  3386. Add('begin');
  3387. ConvertProgram;
  3388. CheckSource('TestFunctionResult',
  3389. LinesToStr([ // statements
  3390. 'this.Func1 = function () {',
  3391. ' var Result = 0;',
  3392. ' Result = 3;',
  3393. ' Result = 4;',
  3394. ' return Result;',
  3395. '};'
  3396. ]),
  3397. '');
  3398. end;
  3399. procedure TTestModule.TestNestedProc;
  3400. begin
  3401. StartProgram(false);
  3402. Add([
  3403. 'var vInUnit: longint;',
  3404. 'function DoIt(pA,pD: longint): longint;',
  3405. 'var',
  3406. ' vB: longint;',
  3407. ' vC: longint;',
  3408. ' function Nesty(pA: longint): longint; ',
  3409. ' var vB: longint;',
  3410. ' begin',
  3411. ' Result:=pa+vb+vc+pd+vInUnit;',
  3412. ' nesty:=3;',
  3413. ' doit:=4;',
  3414. ' exit;',
  3415. ' end;',
  3416. 'begin',
  3417. ' Result:=pa+vb+vc;',
  3418. ' doit:=6;',
  3419. ' exit;',
  3420. 'end;',
  3421. 'begin']);
  3422. ConvertProgram;
  3423. CheckSource('TestNestedProc',
  3424. LinesToStr([ // statements
  3425. 'this.vInUnit = 0;',
  3426. 'this.DoIt = function (pA, pD) {',
  3427. ' var Result = 0;',
  3428. ' var vB = 0;',
  3429. ' var vC = 0;',
  3430. ' function Nesty(pA) {',
  3431. ' var Result$1 = 0;',
  3432. ' var vB = 0;',
  3433. ' Result$1 = pA + vB + vC + pD + $mod.vInUnit;',
  3434. ' Result$1 = 3;',
  3435. ' Result = 4;',
  3436. ' return Result$1;',
  3437. ' return Result$1;',
  3438. ' };',
  3439. ' Result = pA + vB + vC;',
  3440. ' Result = 6;',
  3441. ' return Result;',
  3442. ' return Result;',
  3443. '};'
  3444. ]),
  3445. '');
  3446. end;
  3447. procedure TTestModule.TestNestedProc_ResultString;
  3448. begin
  3449. StartProgram(false);
  3450. Add([
  3451. 'function DoIt: string;',
  3452. ' function Nesty: string; ',
  3453. ' begin',
  3454. ' nesty:=#65#66;',
  3455. ' nesty[1]:=#67;',
  3456. ' doit:=#68;',
  3457. ' doit[2]:=#69;',
  3458. ' end;',
  3459. 'begin',
  3460. ' doit:=#70;',
  3461. ' doit[3]:=#71;',
  3462. 'end;',
  3463. 'begin']);
  3464. ConvertProgram;
  3465. CheckSource('TestNestedProc_ResultString',
  3466. LinesToStr([ // statements
  3467. 'this.DoIt = function () {',
  3468. ' var Result = "";',
  3469. ' function Nesty() {',
  3470. ' var Result$1 = "";',
  3471. ' Result$1 = "AB";',
  3472. ' Result$1 = rtl.setCharAt(Result$1, 0, "C");',
  3473. ' Result = "D";',
  3474. ' Result = rtl.setCharAt(Result, 1, "E");',
  3475. ' return Result$1;',
  3476. ' };',
  3477. ' Result = "F";',
  3478. ' Result = rtl.setCharAt(Result, 2, "G");',
  3479. ' return Result;',
  3480. '};'
  3481. ]),
  3482. '');
  3483. end;
  3484. procedure TTestModule.TestForwardProc;
  3485. begin
  3486. StartProgram(false);
  3487. Add('procedure FuncA(Bar: longint); forward;');
  3488. Add('procedure FuncB(Bar: longint);');
  3489. Add('begin');
  3490. Add(' funca(bar);');
  3491. Add('end;');
  3492. Add('procedure funca(bar: longint);');
  3493. Add('begin');
  3494. Add(' if bar=3 then ;');
  3495. Add('end;');
  3496. Add('begin');
  3497. Add(' funca(4);');
  3498. Add(' funcb(5);');
  3499. ConvertProgram;
  3500. CheckSource('TestForwardProc',
  3501. LinesToStr([ // statements'
  3502. 'this.FuncB = function (Bar) {',
  3503. ' $mod.FuncA(Bar);',
  3504. '};',
  3505. 'this.FuncA = function (Bar) {',
  3506. ' if (Bar === 3);',
  3507. '};'
  3508. ]),
  3509. LinesToStr([
  3510. '$mod.FuncA(4);',
  3511. '$mod.FuncB(5);'
  3512. ])
  3513. );
  3514. end;
  3515. procedure TTestModule.TestNestedForwardProc;
  3516. begin
  3517. StartProgram(false);
  3518. Add('procedure FuncA;');
  3519. Add(' procedure FuncB(i: longint); forward;');
  3520. Add(' procedure FuncC(i: longint);');
  3521. Add(' begin');
  3522. Add(' funcb(i);');
  3523. Add(' end;');
  3524. Add(' procedure FuncB(i: longint);');
  3525. Add(' begin');
  3526. Add(' if i=3 then ;');
  3527. Add(' end;');
  3528. Add('begin');
  3529. Add(' funcc(4)');
  3530. Add('end;');
  3531. Add('begin');
  3532. Add(' funca;');
  3533. ConvertProgram;
  3534. CheckSource('TestNestedForwardProc',
  3535. LinesToStr([ // statements'
  3536. 'this.FuncA = function () {',
  3537. ' function FuncC(i) {',
  3538. ' FuncB(i);',
  3539. ' };',
  3540. ' function FuncB(i) {',
  3541. ' if (i === 3);',
  3542. ' };',
  3543. ' FuncC(4);',
  3544. '};'
  3545. ]),
  3546. LinesToStr([
  3547. '$mod.FuncA();'
  3548. ])
  3549. );
  3550. end;
  3551. procedure TTestModule.TestAssignFunctionResult;
  3552. begin
  3553. StartProgram(false);
  3554. Add('function Func1: longint;');
  3555. Add('begin');
  3556. Add('end;');
  3557. Add('var i: longint;');
  3558. Add('begin');
  3559. Add(' i:=func1();');
  3560. Add(' i:=func1()+func1();');
  3561. ConvertProgram;
  3562. CheckSource('TestAssignFunctionResult',
  3563. LinesToStr([ // statements
  3564. 'this.Func1 = function () {',
  3565. ' var Result = 0;',
  3566. ' return Result;',
  3567. '};',
  3568. 'this.i = 0;'
  3569. ]),
  3570. LinesToStr([
  3571. '$mod.i = $mod.Func1();',
  3572. '$mod.i = $mod.Func1() + $mod.Func1();'
  3573. ]));
  3574. end;
  3575. procedure TTestModule.TestFunctionResultInCondition;
  3576. begin
  3577. StartProgram(false);
  3578. Add('function Func1: longint;');
  3579. Add('begin');
  3580. Add('end;');
  3581. Add('function Func2: boolean;');
  3582. Add('begin');
  3583. Add('end;');
  3584. Add('var i: longint;');
  3585. Add('begin');
  3586. Add(' if func2 then ;');
  3587. Add(' if i=func1() then ;');
  3588. Add(' if i=func1 then ;');
  3589. ConvertProgram;
  3590. CheckSource('TestFunctionResultInCondition',
  3591. LinesToStr([ // statements
  3592. 'this.Func1 = function () {',
  3593. ' var Result = 0;',
  3594. ' return Result;',
  3595. '};',
  3596. 'this.Func2 = function () {',
  3597. ' var Result = false;',
  3598. ' return Result;',
  3599. '};',
  3600. 'this.i = 0;'
  3601. ]),
  3602. LinesToStr([
  3603. 'if ($mod.Func2());',
  3604. 'if ($mod.i === $mod.Func1());',
  3605. 'if ($mod.i === $mod.Func1());'
  3606. ]));
  3607. end;
  3608. procedure TTestModule.TestFunctionResultInForLoop;
  3609. begin
  3610. StartProgram(false);
  3611. Add([
  3612. 'function Func1(a: array of longint): longint;',
  3613. 'begin',
  3614. ' for Result:=High(a) downto Low(a) do if a[Result]=0 then exit;',
  3615. ' for Result in a do if a[Result]=0 then exit;',
  3616. 'end;',
  3617. 'begin',
  3618. ' Func1([1,2,3])']);
  3619. ConvertProgram;
  3620. CheckSource('TestFunctionResultInForLoop',
  3621. LinesToStr([ // statements
  3622. 'this.Func1 = function (a) {',
  3623. ' var Result = 0;',
  3624. ' for (var $l = rtl.length(a) - 1; $l >= 0; $l--) {',
  3625. ' Result = $l;',
  3626. ' if (a[Result] === 0) return Result;',
  3627. ' };',
  3628. ' for (var $in = a, $l1 = 0, $end = rtl.length($in) - 1; $l1 <= $end; $l1++) {',
  3629. ' Result = $in[$l1];',
  3630. ' if (a[Result] === 0) return Result;',
  3631. ' };',
  3632. ' return Result;',
  3633. '};',
  3634. '']),
  3635. LinesToStr([
  3636. '$mod.Func1([1, 2, 3]);'
  3637. ]));
  3638. end;
  3639. procedure TTestModule.TestFunctionResultInTypeCast;
  3640. begin
  3641. StartProgram(false);
  3642. Add([
  3643. 'function GetInt: longint;',
  3644. 'begin',
  3645. 'end;',
  3646. 'begin',
  3647. ' if Byte(GetInt)=0 then ;',
  3648. '']);
  3649. ConvertProgram;
  3650. CheckSource('TestFunctionResultInTypeCast',
  3651. LinesToStr([ // statements
  3652. 'this.GetInt = function () {',
  3653. ' var Result = 0;',
  3654. ' return Result;',
  3655. '};',
  3656. '']),
  3657. LinesToStr([
  3658. 'if (($mod.GetInt() & 255) === 0) ;'
  3659. ]));
  3660. end;
  3661. procedure TTestModule.TestExit;
  3662. begin
  3663. StartProgram(false);
  3664. Add('procedure ProcA;');
  3665. Add('begin');
  3666. Add(' exit;');
  3667. Add('end;');
  3668. Add('function FuncB: longint;');
  3669. Add('begin');
  3670. Add(' exit;');
  3671. Add(' exit(3);');
  3672. Add('end;');
  3673. Add('function FuncC: string;');
  3674. Add('begin');
  3675. Add(' exit;');
  3676. Add(' exit(''a'');');
  3677. Add(' exit(''abc'');');
  3678. Add('end;');
  3679. Add('begin');
  3680. Add(' exit;');
  3681. Add(' exit(1);');
  3682. ConvertProgram;
  3683. CheckSource('TestExit',
  3684. LinesToStr([ // statements
  3685. 'this.ProcA = function () {',
  3686. ' return;',
  3687. '};',
  3688. 'this.FuncB = function () {',
  3689. ' var Result = 0;',
  3690. ' return Result;',
  3691. ' return 3;',
  3692. ' return Result;',
  3693. '};',
  3694. 'this.FuncC = function () {',
  3695. ' var Result = "";',
  3696. ' return Result;',
  3697. ' return "a";',
  3698. ' return "abc";',
  3699. ' return Result;',
  3700. '};'
  3701. ]),
  3702. LinesToStr([
  3703. 'return;',
  3704. 'return 1;',
  3705. '']));
  3706. end;
  3707. procedure TTestModule.TestExit_ResultInFinally;
  3708. begin
  3709. StartProgram(false);
  3710. Add([
  3711. 'function Run: word;',
  3712. 'begin',
  3713. ' try',
  3714. ' exit(3);', // no Result in finally -> use return 3
  3715. ' finally',
  3716. ' end;',
  3717. 'end;',
  3718. 'function Fly: word;',
  3719. 'begin',
  3720. ' try',
  3721. ' exit(3);',
  3722. ' finally',
  3723. ' if Result>0 then ;',
  3724. ' end;',
  3725. 'end;',
  3726. 'function Jump: word;',
  3727. 'begin',
  3728. ' try',
  3729. ' try',
  3730. ' exit(4);',
  3731. ' finally',
  3732. ' end;',
  3733. ' finally',
  3734. ' if Result>0 then ;',
  3735. ' end;',
  3736. 'end;',
  3737. 'begin',
  3738. '']);
  3739. ConvertProgram;
  3740. CheckSource('TestExit_ResultInFinally',
  3741. LinesToStr([ // statements
  3742. 'this.Run = function () {',
  3743. ' var Result = 0;',
  3744. ' try {',
  3745. ' return 3;',
  3746. ' } finally {',
  3747. ' };',
  3748. ' return Result;',
  3749. '};',
  3750. 'this.Fly = function () {',
  3751. ' var Result = 0;',
  3752. ' try {',
  3753. ' Result = 3;',
  3754. ' return Result;',
  3755. ' } finally {',
  3756. ' if (Result > 0) ;',
  3757. ' };',
  3758. ' return Result;',
  3759. '};',
  3760. 'this.Jump = function () {',
  3761. ' var Result = 0;',
  3762. ' try {',
  3763. ' try {',
  3764. ' Result = 4;',
  3765. ' return Result;',
  3766. ' } finally {',
  3767. ' };',
  3768. ' } finally {',
  3769. ' if (Result > 0) ;',
  3770. ' };',
  3771. ' return Result;',
  3772. '};',
  3773. '']),
  3774. LinesToStr([
  3775. '']));
  3776. end;
  3777. procedure TTestModule.TestBreak;
  3778. begin
  3779. StartProgram(false);
  3780. Add([
  3781. 'var',
  3782. ' i: longint;',
  3783. 'begin',
  3784. ' repeat',
  3785. ' break;',
  3786. ' until true;',
  3787. ' while true do',
  3788. ' break;',
  3789. ' for i:=1 to 2 do',
  3790. ' break;']);
  3791. ConvertProgram;
  3792. CheckSource('TestBreak',
  3793. LinesToStr([ // statements
  3794. 'this.i = 0;'
  3795. ]),
  3796. LinesToStr([
  3797. 'do {',
  3798. ' break;',
  3799. '} while (!true);',
  3800. 'while (true) break;',
  3801. 'for ($mod.i = 1; $mod.i <= 2; $mod.i++) break;',
  3802. '']));
  3803. end;
  3804. procedure TTestModule.TestBreakAsVar;
  3805. begin
  3806. StartProgram(false);
  3807. Add([
  3808. 'procedure DoIt(break: boolean);',
  3809. 'begin',
  3810. ' if break then ;',
  3811. 'end;',
  3812. 'var',
  3813. ' break: boolean;',
  3814. 'begin',
  3815. ' if break then ;']);
  3816. ConvertProgram;
  3817. CheckSource('TestBreakAsVar',
  3818. LinesToStr([ // statements
  3819. 'this.DoIt = function (Break) {',
  3820. ' if (Break) ;',
  3821. '};',
  3822. 'this.Break = false;',
  3823. '']),
  3824. LinesToStr([
  3825. 'if($mod.Break) ;',
  3826. '']));
  3827. end;
  3828. procedure TTestModule.TestContinue;
  3829. begin
  3830. StartProgram(false);
  3831. Add('var i: longint;');
  3832. Add('begin');
  3833. Add(' repeat');
  3834. Add(' continue;');
  3835. Add(' until true;');
  3836. Add(' while true do');
  3837. Add(' continue;');
  3838. Add(' for i:=1 to 2 do');
  3839. Add(' continue;');
  3840. ConvertProgram;
  3841. CheckSource('TestContinue',
  3842. LinesToStr([ // statements
  3843. 'this.i = 0;'
  3844. ]),
  3845. LinesToStr([
  3846. 'do {',
  3847. ' continue;',
  3848. '} while (!true);',
  3849. 'while (true) continue;',
  3850. 'for ($mod.i = 1; $mod.i <= 2; $mod.i++) continue;',
  3851. '']));
  3852. end;
  3853. procedure TTestModule.TestProc_External;
  3854. begin
  3855. StartProgram(false);
  3856. Add('procedure Foo; external name ''console.log'';');
  3857. Add('function Bar: longint; external name ''get.item'';');
  3858. Add('function Bla(s: string): longint; external name ''apply.something'';');
  3859. Add('var');
  3860. Add(' i: longint;');
  3861. Add('begin');
  3862. Add(' Foo;');
  3863. Add(' i:=Bar;');
  3864. Add(' i:=Bla(''abc'');');
  3865. ConvertProgram;
  3866. CheckSource('TestProc_External',
  3867. LinesToStr([ // statements
  3868. 'this.i = 0;'
  3869. ]),
  3870. LinesToStr([
  3871. 'console.log();',
  3872. '$mod.i = get.item();',
  3873. '$mod.i = apply.something("abc");'
  3874. ]));
  3875. end;
  3876. procedure TTestModule.TestProc_ExternalOtherUnit;
  3877. begin
  3878. AddModuleWithIntfImplSrc('unit2.pas',
  3879. LinesToStr([
  3880. 'procedure Now; external name ''Date.now'';',
  3881. 'procedure DoIt;'
  3882. ]),
  3883. 'procedure doit; begin end;');
  3884. StartUnit(true);
  3885. Add('interface');
  3886. Add('uses unit2;');
  3887. Add('implementation');
  3888. Add('begin');
  3889. Add(' now;');
  3890. Add(' now();');
  3891. Add(' uNit2.now;');
  3892. Add(' uNit2.now();');
  3893. Add(' doit;');
  3894. Add(' uNit2.doit;');
  3895. ConvertUnit;
  3896. CheckSource('TestProc_ExternalOtherUnit',
  3897. LinesToStr([
  3898. '']),
  3899. LinesToStr([
  3900. 'Date.now();',
  3901. 'Date.now();',
  3902. 'Date.now();',
  3903. 'Date.now();',
  3904. 'pas.unit2.DoIt();',
  3905. 'pas.unit2.DoIt();',
  3906. '']));
  3907. end;
  3908. procedure TTestModule.TestProc_Asm;
  3909. begin
  3910. StartProgram(false);
  3911. Add([
  3912. '{$mode delphi}',
  3913. 'function DoIt: longint;',
  3914. 'begin;',
  3915. ' asm',
  3916. ' { a:{ b:{}, c:[]}, d:''1'' };',
  3917. ' end;',
  3918. ' asm console.log(); end;',
  3919. ' asm',
  3920. ' s = "'' ";',
  3921. ' s = ''" '';',
  3922. ' s = s + "world" + "''";',
  3923. ' // end',
  3924. ' s = ''end'';',
  3925. ' s = "end";',
  3926. ' s = "foo\"bar";',
  3927. ' s = ''a\''b'';',
  3928. ' s = `${expr}\`-"-''-`;',
  3929. ' s = `multi',
  3930. 'line`;',
  3931. ' end;',
  3932. 'end;',
  3933. 'procedure Fly;',
  3934. 'asm',
  3935. ' return;',
  3936. 'end;',
  3937. 'begin']);
  3938. ConvertProgram;
  3939. CheckSource('TestProc_Asm',
  3940. LinesToStr([ // statements
  3941. 'this.DoIt = function () {',
  3942. ' var Result = 0;',
  3943. ' { a:{ b:{}, c:[]}, d:''1'' };',
  3944. ' console.log();',
  3945. ' s = "'' ";',
  3946. ' s = ''" '';',
  3947. ' s = s + "world" + "''";',
  3948. ' // end',
  3949. ' s = ''end'';',
  3950. ' s = "end";',
  3951. ' s = "foo\"bar";',
  3952. ' s = ''a\''b'';',
  3953. ' s = `${expr}\`-"-''-`;',
  3954. ' s = `multi',
  3955. 'line`;',
  3956. ' return Result;',
  3957. '};',
  3958. 'this.Fly = function () {',
  3959. ' return;',
  3960. '};',
  3961. '']),
  3962. LinesToStr([
  3963. ''
  3964. ]));
  3965. end;
  3966. procedure TTestModule.TestProc_AsmSubBlock;
  3967. begin
  3968. StartProgram(true,[supTObject]);
  3969. Add([
  3970. '{$mode delphi}',
  3971. 'type',
  3972. ' TBird = class end;',
  3973. 'procedure Run(w: word);',
  3974. 'begin;',
  3975. ' if true then asm console.log(); end;',
  3976. ' if w>3 then asm',
  3977. ' var a = w+1;',
  3978. ' w = a+3;',
  3979. ' end;',
  3980. ' while (w>7) do asm',
  3981. ' w+=3; w*=2;',
  3982. ' end;',
  3983. ' try',
  3984. ' except',
  3985. ' on E: TBird do',
  3986. ' asm console.log(E); end;',
  3987. ' on E: TObject do',
  3988. ' asm var i=3; i--; end;',
  3989. ' else asm Fly; High; end;',
  3990. ' end;',
  3991. 'end;',
  3992. 'begin']);
  3993. ConvertProgram;
  3994. CheckSource('TestProc_AsmSubBlock',
  3995. LinesToStr([ // statements
  3996. 'rtl.createClass(this, "TBird", pas.system.TObject, function () {',
  3997. '});',
  3998. 'this.Run = function (w) {',
  3999. ' if (true) console.log();',
  4000. ' if (w > 3) {',
  4001. ' var a = w+1;',
  4002. ' w = a+3;',
  4003. ' };',
  4004. ' while (w > 7) {',
  4005. ' w+=3; w*=2;',
  4006. ' };',
  4007. ' try {} catch ($e) {',
  4008. ' if ($mod.TBird.isPrototypeOf($e)) {',
  4009. ' var E = $e;',
  4010. ' console.log(E);',
  4011. ' } else if (pas.system.TObject.isPrototypeOf($e)) {',
  4012. ' var E = $e;',
  4013. ' var i=3; i--;',
  4014. ' } else {',
  4015. ' Fly; High;',
  4016. ' }',
  4017. ' };',
  4018. '};',
  4019. '']),
  4020. LinesToStr([
  4021. ''
  4022. ]));
  4023. end;
  4024. procedure TTestModule.TestProc_Assembler;
  4025. begin
  4026. StartProgram(false);
  4027. Add('function DoIt: longint; assembler;');
  4028. Add('asm');
  4029. Add('{ a:{ b:{}, c:[]}, d:''1'' };');
  4030. Add('end;');
  4031. Add('begin');
  4032. ConvertProgram;
  4033. CheckSource('TestProc_Assembler',
  4034. LinesToStr([ // statements
  4035. 'this.DoIt = function () {',
  4036. ' { a:{ b:{}, c:[]}, d:''1'' };',
  4037. '};'
  4038. ]),
  4039. LinesToStr([
  4040. ''
  4041. ]));
  4042. end;
  4043. procedure TTestModule.TestProc_VarParam;
  4044. begin
  4045. StartProgram(false);
  4046. Add('type integer = longint;');
  4047. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  4048. Add('var vJ: integer;');
  4049. Add('begin');
  4050. Add(' vg:=vg+1;');
  4051. Add(' vj:=vh+2;');
  4052. Add(' vi:=vi+3;');
  4053. Add(' doit(vg,vg,vg);');
  4054. Add(' doit(vh,vh,vj);');
  4055. Add(' doit(vi,vi,vi);');
  4056. Add(' doit(vj,vj,vj);');
  4057. Add('end;');
  4058. Add('var i: integer;');
  4059. Add('begin');
  4060. Add(' doit(i,i,i);');
  4061. ConvertProgram;
  4062. CheckSource('TestProc_VarParam',
  4063. LinesToStr([ // statements
  4064. 'this.DoIt = function (vG,vH,vI) {',
  4065. ' var vJ = 0;',
  4066. ' vG = vG + 1;',
  4067. ' vJ = vH + 2;',
  4068. ' vI.set(vI.get()+3);',
  4069. ' $mod.DoIt(vG, vG, {',
  4070. ' get: function () {',
  4071. ' return vG;',
  4072. ' },',
  4073. ' set: function (v) {',
  4074. ' vG = v;',
  4075. ' }',
  4076. ' });',
  4077. ' $mod.DoIt(vH, vH, {',
  4078. ' get: function () {',
  4079. ' return vJ;',
  4080. ' },',
  4081. ' set: function (v) {',
  4082. ' vJ = v;',
  4083. ' }',
  4084. ' });',
  4085. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  4086. ' $mod.DoIt(vJ, vJ, {',
  4087. ' get: function () {',
  4088. ' return vJ;',
  4089. ' },',
  4090. ' set: function (v) {',
  4091. ' vJ = v;',
  4092. ' }',
  4093. ' });',
  4094. '};',
  4095. 'this.i = 0;'
  4096. ]),
  4097. LinesToStr([
  4098. '$mod.DoIt($mod.i,$mod.i,{',
  4099. ' p: $mod,',
  4100. ' get: function () {',
  4101. ' return this.p.i;',
  4102. ' },',
  4103. ' set: function (v) {',
  4104. ' this.p.i = v;',
  4105. ' }',
  4106. '});'
  4107. ]));
  4108. end;
  4109. procedure TTestModule.TestProc_VarParamString;
  4110. begin
  4111. StartProgram(false);
  4112. Add(['type TCaption = string;',
  4113. 'procedure DoIt(vA: TCaption; var vB: TCaption; out vC: TCaption);',
  4114. 'var c: char;',
  4115. 'begin',
  4116. ' va[1]:=c;',
  4117. ' vb[2]:=c;',
  4118. ' vc[3]:=c;',
  4119. 'end;',
  4120. 'begin']);
  4121. ConvertProgram;
  4122. CheckSource('TestProc_VarParamString',
  4123. LinesToStr([ // statements
  4124. 'this.DoIt = function (vA,vB,vC) {',
  4125. ' var c = "";',
  4126. ' vA = rtl.setCharAt(vA, 0, c);',
  4127. ' vB.set(rtl.setCharAt(vB.get(), 1, c));',
  4128. ' vC.set(rtl.setCharAt(vC.get(), 2, c));',
  4129. '};',
  4130. '']),
  4131. LinesToStr([
  4132. ]));
  4133. end;
  4134. procedure TTestModule.TestProc_VarParamV;
  4135. begin
  4136. StartProgram(false);
  4137. Add([
  4138. 'procedure Inc2(var i: longint);',
  4139. 'begin',
  4140. ' i:=i+2;',
  4141. 'end;',
  4142. 'procedure DoIt(v: longint);',
  4143. 'var p: array of longint;',
  4144. 'begin',
  4145. ' Inc2(v);',
  4146. ' Inc2(p[v]);',
  4147. 'end;',
  4148. 'begin']);
  4149. ConvertProgram;
  4150. CheckSource('TestProc_VarParamV',
  4151. LinesToStr([ // statements
  4152. 'this.Inc2 = function (i) {',
  4153. ' i.set(i.get()+2);',
  4154. '};',
  4155. 'this.DoIt = function (v) {',
  4156. ' var p = [];',
  4157. ' $mod.Inc2({get: function () {',
  4158. ' return v;',
  4159. ' }, set: function (w) {',
  4160. ' v = w;',
  4161. ' }});',
  4162. ' $mod.Inc2({',
  4163. ' a: v,',
  4164. ' p: p,',
  4165. ' get: function () {',
  4166. ' return this.p[this.a];',
  4167. ' },',
  4168. ' set: function (v) {',
  4169. ' this.p[this.a] = v;',
  4170. ' }',
  4171. ' });',
  4172. '};',
  4173. '']),
  4174. LinesToStr([
  4175. '']));
  4176. end;
  4177. procedure TTestModule.TestProc_Overload;
  4178. begin
  4179. StartProgram(false);
  4180. Add('procedure DoIt(vI: longint); begin end;');
  4181. Add('procedure DoIt(vI, vJ: longint); begin end;');
  4182. Add('procedure DoIt(vD: double); begin end;');
  4183. Add('begin');
  4184. Add(' DoIt(1);');
  4185. Add(' DoIt(2,3);');
  4186. Add(' DoIt(4.5);');
  4187. ConvertProgram;
  4188. CheckSource('TestProcedureOverload',
  4189. LinesToStr([ // statements
  4190. 'this.DoIt = function (vI) {',
  4191. '};',
  4192. 'this.DoIt$1 = function (vI, vJ) {',
  4193. '};',
  4194. 'this.DoIt$2 = function (vD) {',
  4195. '};',
  4196. '']),
  4197. LinesToStr([
  4198. '$mod.DoIt(1);',
  4199. '$mod.DoIt$1(2, 3);',
  4200. '$mod.DoIt$2(4.5);',
  4201. '']));
  4202. end;
  4203. procedure TTestModule.TestProc_OverloadForward;
  4204. begin
  4205. StartProgram(false);
  4206. Add('procedure DoIt(vI: longint); forward;');
  4207. Add('procedure DoIt(vI, vJ: longint); begin end;');
  4208. Add('procedure doit(vi: longint); begin end;');
  4209. Add('begin');
  4210. Add(' doit(1);');
  4211. Add(' doit(2,3);');
  4212. ConvertProgram;
  4213. CheckSource('TestProcedureOverloadForward',
  4214. LinesToStr([ // statements
  4215. 'this.DoIt$1 = function (vI, vJ) {',
  4216. '};',
  4217. 'this.DoIt = function (vI) {',
  4218. '};',
  4219. '']),
  4220. LinesToStr([
  4221. '$mod.DoIt(1);',
  4222. '$mod.DoIt$1(2, 3);',
  4223. '']));
  4224. end;
  4225. procedure TTestModule.TestProc_OverloadIntfImpl;
  4226. begin
  4227. StartUnit(false);
  4228. Add('interface');
  4229. Add('procedure DoIt(vI: longint);');
  4230. Add('procedure DoIt(vI, vJ: longint);');
  4231. Add('implementation');
  4232. Add('procedure DoIt(vI, vJ, vK, vL, vM: longint); forward;');
  4233. Add('procedure DoIt(vI, vJ, vK: longint); begin end;');
  4234. Add('procedure DoIt(vi: longint); begin end;');
  4235. Add('procedure DoIt(vI, vJ, vK, vL: longint); begin end;');
  4236. Add('procedure DoIt(vi, vj: longint); begin end;');
  4237. Add('procedure DoIt(vi, vj, vk, vl, vm: longint); begin end;');
  4238. Add('begin');
  4239. Add(' doit(1);');
  4240. Add(' doit(2,3);');
  4241. Add(' doit(4,5,6);');
  4242. Add(' doit(7,8,9,10);');
  4243. Add(' doit(11,12,13,14,15);');
  4244. ConvertUnit;
  4245. CheckSource('TestProcedureOverloadUnit',
  4246. LinesToStr([ // statements
  4247. 'var $impl = $mod.$impl;',
  4248. 'this.DoIt = function (vI) {',
  4249. '};',
  4250. 'this.DoIt$1 = function (vI, vJ) {',
  4251. '};',
  4252. '']),
  4253. LinesToStr([ // this.$init
  4254. '$mod.DoIt(1);',
  4255. '$mod.DoIt$1(2, 3);',
  4256. '$impl.DoIt$3(4,5,6);',
  4257. '$impl.DoIt$4(7,8,9,10);',
  4258. '$impl.DoIt$2(11,12,13,14,15);',
  4259. '']),
  4260. LinesToStr([ // implementation
  4261. '$impl.DoIt$3 = function (vI, vJ, vK) {',
  4262. '};',
  4263. '$impl.DoIt$4 = function (vI, vJ, vK, vL) {',
  4264. '};',
  4265. '$impl.DoIt$2 = function (vI, vJ, vK, vL, vM) {',
  4266. '};',
  4267. '']));
  4268. end;
  4269. procedure TTestModule.TestProc_OverloadNested;
  4270. begin
  4271. StartProgram(false);
  4272. Add([
  4273. 'procedure doit(vA: longint);',
  4274. ' procedure DoIt(vA, vB: longint); overload;',
  4275. ' begin',
  4276. ' doit(1);',
  4277. ' doit(1,2);',
  4278. ' end;',
  4279. ' procedure doit(vA, vB, vC: longint);',
  4280. ' begin',
  4281. ' doit(1);',
  4282. ' doit(1,2);',
  4283. ' doit(1,2,3);',
  4284. ' end;',
  4285. 'begin',
  4286. ' doit(1);',
  4287. ' doit(1,2);',
  4288. ' doit(1,2,3);',
  4289. 'end;',
  4290. 'begin // main',
  4291. ' doit(1);']);
  4292. ConvertProgram;
  4293. CheckSource('TestProcedureOverloadNested',
  4294. LinesToStr([ // statements
  4295. 'this.doit = function (vA) {',
  4296. ' function DoIt$1(vA, vB) {',
  4297. ' $mod.doit(1);',
  4298. ' DoIt$1(1, 2);',
  4299. ' };',
  4300. ' function doit$2(vA, vB, vC) {',
  4301. ' $mod.doit(1);',
  4302. ' DoIt$1(1, 2);',
  4303. ' doit$2(1, 2, 3);',
  4304. ' };',
  4305. ' $mod.doit(1);',
  4306. ' DoIt$1(1, 2);',
  4307. ' doit$2(1, 2, 3);',
  4308. '};',
  4309. '']),
  4310. LinesToStr([
  4311. '$mod.doit(1);',
  4312. '']));
  4313. end;
  4314. procedure TTestModule.TestProc_OverloadNestedForward;
  4315. begin
  4316. StartProgram(false);
  4317. Add([
  4318. 'procedure DoIt(vA: longint); overload; forward;',
  4319. 'procedure DoIt(vB, vC: longint); overload;',
  4320. 'begin // 2 param overload',
  4321. ' doit(1);',
  4322. ' doit(1,2);',
  4323. 'end;',
  4324. 'procedure doit(vA: longint);',
  4325. ' procedure DoIt(vA, vB, vC: longint); overload; forward;',
  4326. ' procedure DoIt(vA, vB, vC, vD: longint); overload;',
  4327. ' begin // 4 param overload',
  4328. ' doit(1);',
  4329. ' doit(1,2);',
  4330. ' doit(1,2,3);',
  4331. ' doit(1,2,3,4);',
  4332. ' end;',
  4333. ' procedure doit(vA, vB, vC: longint);',
  4334. ' procedure DoIt(vA, vB, vC, vD, vE: longint); overload; forward;',
  4335. ' procedure DoIt(vA, vB, vC, vD, vE, vF: longint); overload;',
  4336. ' begin // 6 param overload',
  4337. ' doit(1);',
  4338. ' doit(1,2);',
  4339. ' doit(1,2,3);',
  4340. ' doit(1,2,3,4);',
  4341. ' doit(1,2,3,4,5);',
  4342. ' doit(1,2,3,4,5,6);',
  4343. ' end;',
  4344. ' procedure doit(vA, vB, vC, vD, vE: longint);',
  4345. ' begin // 5 param overload',
  4346. ' doit(1);',
  4347. ' doit(1,2);',
  4348. ' doit(1,2,3);',
  4349. ' doit(1,2,3,4);',
  4350. ' doit(1,2,3,4,5);',
  4351. ' doit(1,2,3,4,5,6);',
  4352. ' end;',
  4353. ' begin // 3 param overload',
  4354. ' doit(1);',
  4355. ' doit(1,2);',
  4356. ' doit(1,2,3);',
  4357. ' doit(1,2,3,4);',
  4358. ' doit(1,2,3,4,5);',
  4359. ' doit(1,2,3,4,5,6);',
  4360. ' end;',
  4361. 'begin // 1 param overload',
  4362. ' doit(1);',
  4363. ' doit(1,2);',
  4364. ' doit(1,2,3);',
  4365. ' doit(1,2,3,4);',
  4366. 'end;',
  4367. 'begin // main',
  4368. ' doit(1);',
  4369. ' doit(1,2);']);
  4370. ConvertProgram;
  4371. CheckSource('TestProc_OverloadNestedForward',
  4372. LinesToStr([ // statements
  4373. 'this.DoIt$1 = function (vB, vC) {',
  4374. ' $mod.DoIt(1);',
  4375. ' $mod.DoIt$1(1, 2);',
  4376. '};',
  4377. 'this.DoIt = function (vA) {',
  4378. ' function DoIt$3(vA, vB, vC, vD) {',
  4379. ' $mod.DoIt(1);',
  4380. ' $mod.DoIt$1(1, 2);',
  4381. ' DoIt$2(1, 2, 3);',
  4382. ' DoIt$3(1, 2, 3, 4);',
  4383. ' };',
  4384. ' function DoIt$2(vA, vB, vC) {',
  4385. ' function DoIt$5(vA, vB, vC, vD, vE, vF) {',
  4386. ' $mod.DoIt(1);',
  4387. ' $mod.DoIt$1(1, 2);',
  4388. ' DoIt$2(1, 2, 3);',
  4389. ' DoIt$3(1, 2, 3, 4);',
  4390. ' DoIt$4(1, 2, 3, 4, 5);',
  4391. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  4392. ' };',
  4393. ' function DoIt$4(vA, vB, vC, vD, vE) {',
  4394. ' $mod.DoIt(1);',
  4395. ' $mod.DoIt$1(1, 2);',
  4396. ' DoIt$2(1, 2, 3);',
  4397. ' DoIt$3(1, 2, 3, 4);',
  4398. ' DoIt$4(1, 2, 3, 4, 5);',
  4399. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  4400. ' };',
  4401. ' $mod.DoIt(1);',
  4402. ' $mod.DoIt$1(1, 2);',
  4403. ' DoIt$2(1, 2, 3);',
  4404. ' DoIt$3(1, 2, 3, 4);',
  4405. ' DoIt$4(1, 2, 3, 4, 5);',
  4406. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  4407. ' };',
  4408. ' $mod.DoIt(1);',
  4409. ' $mod.DoIt$1(1, 2);',
  4410. ' DoIt$2(1, 2, 3);',
  4411. ' DoIt$3(1, 2, 3, 4);',
  4412. '};',
  4413. '']),
  4414. LinesToStr([
  4415. '$mod.DoIt(1);',
  4416. '$mod.DoIt$1(1, 2);',
  4417. '']));
  4418. end;
  4419. procedure TTestModule.TestProc_OverloadUnitCycle;
  4420. begin
  4421. AddModuleWithIntfImplSrc('Unit2.pas',
  4422. LinesToStr([
  4423. 'type',
  4424. ' TObject = class',
  4425. ' procedure DoIt(b: boolean); virtual; abstract;',
  4426. ' procedure DoIt(i: longint); virtual; abstract;',
  4427. ' end;',
  4428. '']),
  4429. 'uses test1;');
  4430. StartUnit(true);
  4431. Add([
  4432. 'interface',
  4433. 'uses unit2;',
  4434. 'type',
  4435. ' TEagle = class(TObject)',
  4436. ' procedure DoIt(b: boolean); override;',
  4437. ' procedure DoIt(i: longint); override;',
  4438. ' end;',
  4439. 'implementation',
  4440. 'procedure TEagle.DoIt(b: boolean); begin end;',
  4441. 'procedure TEagle.DoIt(i: longint); begin end;',
  4442. '']);
  4443. ConvertUnit;
  4444. CheckSource('TestProc_OverloadUnitCycle',
  4445. LinesToStr([ // statements
  4446. 'rtl.createClass(this, "TEagle", pas.Unit2.TObject, function () {',
  4447. ' this.DoIt = function (b) {',
  4448. ' };',
  4449. ' this.DoIt$1 = function (i) {',
  4450. ' };',
  4451. '});',
  4452. '']),
  4453. '',
  4454. LinesToStr([
  4455. '']));
  4456. end;
  4457. procedure TTestModule.TestProc_Varargs;
  4458. begin
  4459. StartProgram(false);
  4460. Add([
  4461. 'procedure ProcA(i:longint); varargs; external name ''ProcA'';',
  4462. 'procedure ProcB; varargs; external name ''ProcB'';',
  4463. 'procedure ProcC(i: longint = 17); varargs; external name ''ProcC'';',
  4464. 'function GetIt: longint; begin end;',
  4465. 'begin',
  4466. ' ProcA(1);',
  4467. ' ProcA(1,2);',
  4468. ' ProcA(1,2.0);',
  4469. ' ProcA(1,2,3);',
  4470. ' ProcA(1,''2'');',
  4471. ' ProcA(2,'''');',
  4472. ' ProcA(3,false);',
  4473. ' ProcB;',
  4474. ' ProcB();',
  4475. ' ProcB(4);',
  4476. ' ProcB(''foo'');',
  4477. ' ProcC;',
  4478. ' ProcC();',
  4479. ' ProcC(4);',
  4480. ' ProcC(5,''foo'');',
  4481. ' ProcB(GetIt);',
  4482. ' ProcB(GetIt());',
  4483. ' ProcB(GetIt,GetIt());']);
  4484. ConvertProgram;
  4485. CheckSource('TestProc_Varargs',
  4486. LinesToStr([ // statements
  4487. 'this.GetIt = function () {',
  4488. ' var Result = 0;',
  4489. ' return Result;',
  4490. '};',
  4491. '']),
  4492. LinesToStr([
  4493. 'ProcA(1);',
  4494. 'ProcA(1, 2);',
  4495. 'ProcA(1, 2.0);',
  4496. 'ProcA(1, 2, 3);',
  4497. 'ProcA(1, "2");',
  4498. 'ProcA(2, "");',
  4499. 'ProcA(3, false);',
  4500. 'ProcB();',
  4501. 'ProcB();',
  4502. 'ProcB(4);',
  4503. 'ProcB("foo");',
  4504. 'ProcC(17);',
  4505. 'ProcC(17);',
  4506. 'ProcC(4);',
  4507. 'ProcC(5, "foo");',
  4508. 'ProcB($mod.GetIt());',
  4509. 'ProcB($mod.GetIt());',
  4510. 'ProcB($mod.GetIt(), $mod.GetIt());',
  4511. '']));
  4512. end;
  4513. procedure TTestModule.TestProc_ConstOrder;
  4514. begin
  4515. StartProgram(false);
  4516. Add([
  4517. 'const A = 3;',
  4518. 'const B = A+1;',
  4519. 'procedure DoIt;',
  4520. 'const C = A+1;',
  4521. 'const D = B+1;',
  4522. 'const E = D+C+B+A;',
  4523. 'begin',
  4524. 'end;',
  4525. 'begin'
  4526. ]);
  4527. ConvertProgram;
  4528. CheckSource('TestProc_ConstOrder',
  4529. LinesToStr([ // statements
  4530. 'this.A = 3;',
  4531. 'this.B = 3 + 1;',
  4532. 'var C = 3 + 1;',
  4533. 'var D = 4 + 1;',
  4534. 'var E = 5 + 4 + 4 + 3;',
  4535. 'this.DoIt = function () {',
  4536. '};',
  4537. '']),
  4538. LinesToStr([
  4539. ''
  4540. ]));
  4541. end;
  4542. procedure TTestModule.TestProc_DuplicateConst;
  4543. begin
  4544. StartProgram(false);
  4545. Add([
  4546. 'const A = 1;',
  4547. 'procedure DoIt;',
  4548. 'const A = 2;',
  4549. ' procedure SubIt;',
  4550. ' const A = 21;',
  4551. ' begin',
  4552. ' end;',
  4553. 'begin',
  4554. 'end;',
  4555. 'procedure DoSome;',
  4556. 'const A = 3;',
  4557. 'begin',
  4558. 'end;',
  4559. 'begin'
  4560. ]);
  4561. ConvertProgram;
  4562. CheckSource('TestProc_DuplicateConst',
  4563. LinesToStr([ // statements
  4564. 'this.A = 1;',
  4565. 'var A$1 = 2;',
  4566. 'var A$2 = 21;',
  4567. 'this.DoIt = function () {',
  4568. ' function SubIt() {',
  4569. ' };',
  4570. '};',
  4571. 'var A$3 = 3;',
  4572. 'this.DoSome = function () {',
  4573. '};',
  4574. '']),
  4575. LinesToStr([
  4576. ''
  4577. ]));
  4578. end;
  4579. procedure TTestModule.TestProc_LocalVarAbsolute;
  4580. begin
  4581. StartProgram(false);
  4582. Add([
  4583. 'type',
  4584. ' TObject = class',
  4585. ' Index: longint;',
  4586. ' procedure DoAbs(Item: pointer);',
  4587. ' end;',
  4588. 'procedure TObject.DoAbs(Item: pointer);',
  4589. 'var',
  4590. ' o: TObject absolute Item;',
  4591. 'begin',
  4592. ' if o.Index<o.Index then o.Index:=o.Index;',
  4593. 'end;',
  4594. 'procedure DoIt(i: longint; p: pointer);',
  4595. 'var',
  4596. ' d: double absolute i;',
  4597. ' s: string absolute d;',
  4598. ' oi: TObject absolute i;',
  4599. ' op: TObject absolute p;',
  4600. 'begin',
  4601. ' if d=d then d:=d;',
  4602. ' if s=s then s:=s;',
  4603. ' if oi.Index<oi.Index then oi.Index:=oi.Index;',
  4604. ' if op.Index=op.Index then op.Index:=op.Index;',
  4605. 'end;',
  4606. 'begin']);
  4607. ConvertProgram;
  4608. CheckSource('TestProc_LocalVarAbsolute',
  4609. LinesToStr([ // statements
  4610. 'rtl.createClass(this, "TObject", null, function () {',
  4611. ' this.$init = function () {',
  4612. ' this.Index = 0;',
  4613. ' };',
  4614. ' this.$final = function () {',
  4615. ' };',
  4616. ' this.DoAbs = function (Item) {',
  4617. ' if (Item.Index < Item.Index) Item.Index = Item.Index;',
  4618. ' };',
  4619. '});',
  4620. 'this.DoIt = function (i, p) {',
  4621. ' if (i === i) i = i;',
  4622. ' if (i === i) i = i;',
  4623. ' if (i.Index < i.Index) i.Index = i.Index;',
  4624. ' if (p.Index === p.Index) p.Index = p.Index;',
  4625. '};'
  4626. ]),
  4627. LinesToStr([
  4628. ]));
  4629. end;
  4630. procedure TTestModule.TestProc_LocalVarInit;
  4631. begin
  4632. StartProgram(false);
  4633. Add([
  4634. 'type TBytes = array of byte;',
  4635. 'procedure DoIt;',
  4636. 'const c = 4;',
  4637. 'var',
  4638. ' b: byte = 1;',
  4639. ' w: word = 2+c;',
  4640. ' p: pointer = nil;',
  4641. ' Buffer: TBytes = nil;',
  4642. 'begin',
  4643. 'end;',
  4644. 'begin']);
  4645. ConvertProgram;
  4646. CheckSource('TestProc_LocalVarInit',
  4647. LinesToStr([ // statements
  4648. 'var c = 4;',
  4649. 'this.DoIt = function () {',
  4650. ' var b = 1;',
  4651. ' var w = 2 + 4;',
  4652. ' var p = null;',
  4653. ' var Buffer = [];',
  4654. '};',
  4655. '']),
  4656. LinesToStr([
  4657. ]));
  4658. end;
  4659. procedure TTestModule.TestProc_ReservedWords;
  4660. begin
  4661. StartProgram(false);
  4662. Add([
  4663. 'procedure Date(ArrayBuffer: longint);',
  4664. 'const',
  4665. ' NaN: longint = 3;',
  4666. 'var',
  4667. ' &Boolean: longint;',
  4668. ' procedure Error(ArrayBuffer: longint);',
  4669. ' begin',
  4670. ' end;',
  4671. 'begin',
  4672. ' Nan:=&bOolean;',
  4673. 'end;',
  4674. 'begin',
  4675. ' Date(1);']);
  4676. ConvertProgram;
  4677. CheckSource('TestProc_ReservedWords',
  4678. LinesToStr([ // statements
  4679. 'var naN = 3;',
  4680. 'this.Date = function (arrayBuffer) {',
  4681. ' var boolean = 0;',
  4682. ' function error(arrayBuffer) {',
  4683. ' };',
  4684. ' naN = boolean;',
  4685. '};',
  4686. '']),
  4687. LinesToStr([
  4688. ' $mod.Date(1);'
  4689. ]));
  4690. end;
  4691. procedure TTestModule.TestProc_ConstRefWord;
  4692. begin
  4693. StartProgram(false);
  4694. Add([
  4695. 'procedure Run(constref w: word);',
  4696. 'var l: word;',
  4697. 'begin',
  4698. ' l:=w;',
  4699. ' Run(w);',
  4700. ' Run(l);',
  4701. 'end;',
  4702. 'procedure Fly(a: word; var b: word; out c: word; const d: word; constref e: word);',
  4703. 'begin',
  4704. ' Run(a);',
  4705. ' Run(b);',
  4706. ' Run(c);',
  4707. ' Run(d);',
  4708. ' Run(e);',
  4709. 'end;',
  4710. 'begin',
  4711. ' Run(1);']);
  4712. ConvertProgram;
  4713. CheckHint(mtWarning,nConstRefNotForXAsConst,'ConstRef not yet implemented for Word. Treating as Const');
  4714. CheckSource('TestProc_ConstRefWord',
  4715. LinesToStr([ // statements
  4716. 'this.Run = function (w) {',
  4717. ' var l = 0;',
  4718. ' l = w;',
  4719. ' $mod.Run(w);',
  4720. ' $mod.Run(l);',
  4721. '};',
  4722. 'this.Fly = function (a, b, c, d, e) {',
  4723. ' $mod.Run(a);',
  4724. ' $mod.Run(b.get());',
  4725. ' $mod.Run(c.get());',
  4726. ' $mod.Run(d);',
  4727. ' $mod.Run(e);',
  4728. '};',
  4729. '']),
  4730. LinesToStr([
  4731. '$mod.Run(1);'
  4732. ]));
  4733. end;
  4734. procedure TTestModule.TestAnonymousProc_Assign_ObjFPC;
  4735. begin
  4736. StartProgram(false);
  4737. Add([
  4738. '{$mode objfpc}',
  4739. 'type',
  4740. ' TFunc = reference to function(x: word): word;',
  4741. 'var Func: TFunc;',
  4742. 'procedure DoIt(a: word);',
  4743. 'begin',
  4744. ' Func:=function(b:word): word',
  4745. ' begin',
  4746. ' Result:=a+b;',
  4747. ' exit(b);',
  4748. ' exit(Result);',
  4749. ' end;',// test semicolon
  4750. ' a:=3;',
  4751. 'end;',
  4752. 'begin',
  4753. ' Func:=function(c:word):word begin',
  4754. ' Result:=3+c;',
  4755. ' exit(c);',
  4756. ' exit(Result);',
  4757. ' end;']);
  4758. ConvertProgram;
  4759. CheckSource('TestAnonymousProc_Assign_ObjFPC',
  4760. LinesToStr([ // statements
  4761. 'this.Func = null;',
  4762. 'this.DoIt = function (a) {',
  4763. ' $mod.Func = function (b) {',
  4764. ' var Result = 0;',
  4765. ' Result = a + b;',
  4766. ' return b;',
  4767. ' return Result;',
  4768. ' return Result;',
  4769. ' };',
  4770. ' a = 3;',
  4771. '};',
  4772. '']),
  4773. LinesToStr([
  4774. '$mod.Func = function (c) {',
  4775. ' var Result = 0;',
  4776. ' Result = 3 + c;',
  4777. ' return c;',
  4778. ' return Result;',
  4779. ' return Result;',
  4780. '};',
  4781. '']));
  4782. end;
  4783. procedure TTestModule.TestAnonymousProc_Assign_Delphi;
  4784. begin
  4785. StartProgram(false);
  4786. Add([
  4787. '{$mode delphi}',
  4788. 'type',
  4789. ' TProc = reference to procedure(x: word);',
  4790. 'procedure DoIt(a: word);',
  4791. 'var Proc: TProc;',
  4792. 'begin',
  4793. ' Proc:=procedure(b:word) begin end;',
  4794. 'end;',
  4795. 'var Proc: TProc;',
  4796. 'begin',
  4797. ' Proc:=procedure(c:word) begin end;',
  4798. '']);
  4799. ConvertProgram;
  4800. CheckSource('TestAnonymousProc_Assign_Delphi',
  4801. LinesToStr([ // statements
  4802. 'this.DoIt = function (a) {',
  4803. ' var Proc = null;',
  4804. ' Proc = function (b) {',
  4805. ' };',
  4806. '};',
  4807. 'this.Proc = null;',
  4808. '']),
  4809. LinesToStr([
  4810. '$mod.Proc = function (c) {',
  4811. '};',
  4812. '']));
  4813. end;
  4814. procedure TTestModule.TestAnonymousProc_Arg;
  4815. begin
  4816. StartProgram(false);
  4817. Add([
  4818. 'type',
  4819. ' TProc = reference to procedure;',
  4820. ' TFunc = reference to function(x: word): word;',
  4821. 'procedure DoMore(f,g: TProc);',
  4822. 'begin',
  4823. 'end;',
  4824. 'procedure DoOdd(v: jsvalue);',
  4825. 'begin',
  4826. 'end;',
  4827. 'procedure DoIt(f: TFunc);',
  4828. 'begin',
  4829. ' DoIt(function(b:word): word',
  4830. ' begin',
  4831. ' Result:=1+b;',
  4832. ' end);',
  4833. ' DoMore(procedure begin end, procedure begin end);',
  4834. ' DoOdd(procedure begin end);',
  4835. 'end;',
  4836. 'begin',
  4837. ' DoMore(procedure begin end,',
  4838. ' procedure assembler asm',
  4839. ' console.log("c");',
  4840. ' end);',
  4841. '']);
  4842. ConvertProgram;
  4843. CheckSource('TestAnonymousProc_Arg',
  4844. LinesToStr([ // statements
  4845. 'this.DoMore = function (f, g) {',
  4846. '};',
  4847. 'this.DoOdd = function (v) {',
  4848. '};',
  4849. 'this.DoIt = function (f) {',
  4850. ' $mod.DoIt(function (b) {',
  4851. ' var Result = 0;',
  4852. ' Result = 1 + b;',
  4853. ' return Result;',
  4854. ' });',
  4855. ' $mod.DoMore(function () {',
  4856. ' }, function () {',
  4857. ' });',
  4858. ' $mod.DoOdd(function () {',
  4859. ' });',
  4860. '};',
  4861. '']),
  4862. LinesToStr([
  4863. '$mod.DoMore(function () {',
  4864. '}, function () {',
  4865. ' console.log("c");',
  4866. '});',
  4867. '']));
  4868. end;
  4869. procedure TTestModule.TestAnonymousProc_Typecast;
  4870. begin
  4871. StartProgram(false);
  4872. Add([
  4873. 'type',
  4874. ' TProc = reference to procedure(w: word);',
  4875. ' TArr = array of word;',
  4876. ' TFuncArr = reference to function: TArr;',
  4877. 'procedure DoIt(p: TProc);',
  4878. 'var',
  4879. ' w: word;',
  4880. ' a: TArr;',
  4881. 'begin',
  4882. ' p:=TProc(procedure(b: smallint) begin end);',
  4883. ' a:=TFuncArr(function: TArr begin end)();',
  4884. ' w:=TFuncArr(function: TArr begin end)()[3];',
  4885. 'end;',
  4886. 'begin']);
  4887. ConvertProgram;
  4888. CheckSource('TestAnonymousProc_Typecast',
  4889. LinesToStr([ // statements
  4890. 'this.DoIt = function (p) {',
  4891. ' var w = 0;',
  4892. ' var a = [];',
  4893. ' p = function (b) {',
  4894. ' };',
  4895. ' a = function () {',
  4896. ' var Result = [];',
  4897. ' return Result;',
  4898. ' }();',
  4899. ' w = function () {',
  4900. ' var Result = [];',
  4901. ' return Result;',
  4902. ' }()[3];',
  4903. '};',
  4904. '']),
  4905. LinesToStr([
  4906. '']));
  4907. end;
  4908. procedure TTestModule.TestAnonymousProc_With;
  4909. begin
  4910. StartProgram(false);
  4911. Add([
  4912. 'type',
  4913. ' TProc = reference to procedure(w: word);',
  4914. ' TObject = class',
  4915. ' b: boolean;',
  4916. ' end;',
  4917. 'var',
  4918. ' p: TProc;',
  4919. ' bird: TObject;',
  4920. 'begin',
  4921. ' with bird do',
  4922. ' p:=procedure(w: word)',
  4923. ' begin',
  4924. ' b:=w>2;',
  4925. ' end;',
  4926. '']);
  4927. ConvertProgram;
  4928. CheckSource('TestAnonymousProc_With',
  4929. LinesToStr([ // statements
  4930. 'rtl.createClass(this, "TObject", null, function () {',
  4931. ' this.$init = function () {',
  4932. ' this.b = false;',
  4933. ' };',
  4934. ' this.$final = function () {',
  4935. ' };',
  4936. '});',
  4937. 'this.p = null;',
  4938. 'this.bird = null;',
  4939. '']),
  4940. LinesToStr([
  4941. 'var $with = $mod.bird;',
  4942. '$mod.p = function (w) {',
  4943. ' $with.b = w > 2;',
  4944. '};',
  4945. '']));
  4946. end;
  4947. procedure TTestModule.TestAnonymousProc_ExceptOn;
  4948. begin
  4949. StartProgram(false);
  4950. Add([
  4951. 'type',
  4952. ' TProc = reference to procedure;',
  4953. ' TObject = class',
  4954. ' b: boolean;',
  4955. ' end;',
  4956. 'procedure DoIt;',
  4957. 'var',
  4958. ' p: TProc;',
  4959. 'begin',
  4960. ' try',
  4961. ' except',
  4962. ' on E: TObject do',
  4963. ' p:=procedure',
  4964. ' begin',
  4965. ' E.b:=true;',
  4966. ' end;',
  4967. ' end;',
  4968. 'end;',
  4969. 'begin']);
  4970. ConvertProgram;
  4971. CheckSource('TestAnonymousProc_ExceptOn',
  4972. LinesToStr([ // statements
  4973. 'rtl.createClass(this, "TObject", null, function () {',
  4974. ' this.$init = function () {',
  4975. ' this.b = false;',
  4976. ' };',
  4977. ' this.$final = function () {',
  4978. ' };',
  4979. '});',
  4980. 'this.DoIt = function () {',
  4981. ' var p = null;',
  4982. ' try {} catch ($e) {',
  4983. ' if ($mod.TObject.isPrototypeOf($e)) {',
  4984. ' var E = $e;',
  4985. ' p = function () {',
  4986. ' E.b = true;',
  4987. ' };',
  4988. ' } else throw $e',
  4989. ' };',
  4990. '};',
  4991. '']),
  4992. LinesToStr([
  4993. '']));
  4994. end;
  4995. procedure TTestModule.TestAnonymousProc_Nested;
  4996. begin
  4997. StartProgram(false);
  4998. Add([
  4999. 'type',
  5000. ' TProc = reference to procedure;',
  5001. ' TObject = class',
  5002. ' i: byte;',
  5003. ' procedure DoIt;',
  5004. ' end;',
  5005. 'procedure TObject.DoIt;',
  5006. 'var',
  5007. ' p: TProc;',
  5008. ' procedure Sub;',
  5009. ' begin',
  5010. ' p:=procedure',
  5011. ' begin',
  5012. ' i:=3;',
  5013. ' Self.i:=4;',
  5014. ' p:=procedure',
  5015. ' procedure SubSub;',
  5016. ' begin',
  5017. ' i:=13;',
  5018. ' Self.i:=14;',
  5019. ' end;',
  5020. ' begin',
  5021. ' i:=13;',
  5022. ' Self.i:=14;',
  5023. ' end;',
  5024. ' end;',
  5025. ' end;',
  5026. 'begin',
  5027. 'end;',
  5028. 'begin']);
  5029. ConvertProgram;
  5030. CheckSource('TestAnonymousProc_Nested',
  5031. LinesToStr([ // statements
  5032. 'rtl.createClass(this, "TObject", null, function () {',
  5033. ' this.$init = function () {',
  5034. ' this.i = 0;',
  5035. ' };',
  5036. ' this.$final = function () {',
  5037. ' };',
  5038. ' this.DoIt = function () {',
  5039. ' var $Self = this;',
  5040. ' var p = null;',
  5041. ' function Sub() {',
  5042. ' p = function () {',
  5043. ' $Self.i = 3;',
  5044. ' $Self.i = 4;',
  5045. ' p = function () {',
  5046. ' function SubSub() {',
  5047. ' $Self.i = 13;',
  5048. ' $Self.i = 14;',
  5049. ' };',
  5050. ' $Self.i = 13;',
  5051. ' $Self.i = 14;',
  5052. ' };',
  5053. ' };',
  5054. ' };',
  5055. ' };',
  5056. '});',
  5057. '']),
  5058. LinesToStr([
  5059. '']));
  5060. end;
  5061. procedure TTestModule.TestAnonymousProc_NestedAssignResult;
  5062. begin
  5063. StartProgram(false);
  5064. Add([
  5065. 'type',
  5066. ' TProc = reference to procedure;',
  5067. 'function DoIt: TProc;',
  5068. ' function Sub: TProc;',
  5069. ' begin',
  5070. ' Result:=procedure',
  5071. ' begin',
  5072. ' Sub:=procedure',
  5073. ' procedure SubSub;',
  5074. ' begin',
  5075. ' Result:=nil;',
  5076. ' Sub:=nil;',
  5077. ' DoIt:=nil;',
  5078. ' end;',
  5079. ' begin',
  5080. ' Result:=nil;',
  5081. ' Sub:=nil;',
  5082. ' DoIt:=nil;',
  5083. ' end;',
  5084. ' end;',
  5085. ' end;',
  5086. 'begin',
  5087. 'end;',
  5088. 'begin']);
  5089. ConvertProgram;
  5090. CheckSource('TestAnonymousProc_NestedAssignResult',
  5091. LinesToStr([ // statements
  5092. 'this.DoIt = function () {',
  5093. ' var Result = null;',
  5094. ' function Sub() {',
  5095. ' var Result$1 = null;',
  5096. ' Result$1 = function () {',
  5097. ' Result$1 = function () {',
  5098. ' function SubSub() {',
  5099. ' Result$1 = null;',
  5100. ' Result$1 = null;',
  5101. ' Result = null;',
  5102. ' };',
  5103. ' Result$1 = null;',
  5104. ' Result$1 = null;',
  5105. ' Result = null;',
  5106. ' };',
  5107. ' };',
  5108. ' return Result$1;',
  5109. ' };',
  5110. ' return Result;',
  5111. '};',
  5112. '']),
  5113. LinesToStr([
  5114. '']));
  5115. end;
  5116. procedure TTestModule.TestAnonymousProc_Class;
  5117. begin
  5118. StartProgram(false);
  5119. Add([
  5120. 'type',
  5121. ' TProc = reference to procedure;',
  5122. ' TEvent = procedure of object;',
  5123. ' TObject = class',
  5124. ' Size: word;',
  5125. ' function GetIt: TProc;',
  5126. ' procedure DoIt; virtual; abstract;',
  5127. ' end;',
  5128. 'function TObject.GetIt: TProc;',
  5129. 'begin',
  5130. ' Result:=procedure',
  5131. ' var p: TEvent;',
  5132. ' begin',
  5133. ' Size:=Size;',
  5134. ' Size:=Self.Size;',
  5135. ' p:=@DoIt;',
  5136. ' p:[email protected];',
  5137. ' end;',
  5138. 'end;',
  5139. 'begin']);
  5140. ConvertProgram;
  5141. CheckSource('TestAnonymousProc_Class',
  5142. LinesToStr([ // statements
  5143. 'rtl.createClass(this, "TObject", null, function () {',
  5144. ' this.$init = function () {',
  5145. ' this.Size = 0;',
  5146. ' };',
  5147. ' this.$final = function () {',
  5148. ' };',
  5149. ' this.GetIt = function () {',
  5150. ' var $Self = this;',
  5151. ' var Result = null;',
  5152. ' Result = function () {',
  5153. ' var p = null;',
  5154. ' $Self.Size = $Self.Size;',
  5155. ' $Self.Size = $Self.Size;',
  5156. ' p = rtl.createCallback($Self, "DoIt");',
  5157. ' p = rtl.createCallback($Self, "DoIt");',
  5158. ' };',
  5159. ' return Result;',
  5160. ' };',
  5161. '});',
  5162. '']),
  5163. LinesToStr([
  5164. '']));
  5165. end;
  5166. procedure TTestModule.TestAnonymousProc_ForLoop;
  5167. begin
  5168. StartProgram(false);
  5169. Add([
  5170. 'type TProc = reference to procedure;',
  5171. 'procedure Foo(p: TProc);',
  5172. 'begin',
  5173. 'end;',
  5174. 'procedure DoIt;',
  5175. 'var i: word;',
  5176. ' a: word;',
  5177. 'begin',
  5178. ' for i:=1 to 10 do begin',
  5179. ' Foo(procedure begin a:=3; end);',
  5180. ' end;',
  5181. 'end;',
  5182. 'begin',
  5183. ' DoIt;']);
  5184. ConvertProgram;
  5185. CheckSource('TestAnonymousProc_ForLoop',
  5186. LinesToStr([ // statements
  5187. 'this.Foo = function (p) {',
  5188. '};',
  5189. 'this.DoIt = function () {',
  5190. ' var i = 0;',
  5191. ' var a = 0;',
  5192. ' for (i = 1; i <= 10; i++) {',
  5193. ' $mod.Foo(function () {',
  5194. ' a = 3;',
  5195. ' });',
  5196. ' };',
  5197. '};',
  5198. '']),
  5199. LinesToStr([
  5200. '$mod.DoIt();'
  5201. ]));
  5202. end;
  5203. procedure TTestModule.TestAnonymousProc_AsmDelphi;
  5204. begin
  5205. StartProgram(false);
  5206. Add([
  5207. '{$mode delphi}',
  5208. 'type',
  5209. ' TProc = reference to procedure;',
  5210. ' TFunc = reference to function(x: word): word;',
  5211. 'procedure Run;',
  5212. 'asm',
  5213. 'end;',
  5214. 'procedure Walk(p: TProc; f: TFunc);',
  5215. 'begin',
  5216. ' Walk(procedure asm end, function(b:word): word asm return 1+b; end);',
  5217. 'end;',
  5218. 'begin',
  5219. ' Walk(procedure',
  5220. ' asm',
  5221. ' console.log("a");',
  5222. ' end,',
  5223. ' function(x: word): word asm',
  5224. ' console.log("c");',
  5225. ' end);',
  5226. '']);
  5227. ConvertProgram;
  5228. CheckSource('TestAnonymousProc_AsmDelphi',
  5229. LinesToStr([ // statements
  5230. 'this.Run = function () {',
  5231. '};',
  5232. 'this.Walk = function (p, f) {',
  5233. ' $mod.Walk(function () {',
  5234. ' }, function (b) {',
  5235. ' return 1+b;',
  5236. ' });',
  5237. '};',
  5238. '']),
  5239. LinesToStr([
  5240. '$mod.Walk(function () {',
  5241. ' console.log("a");',
  5242. '}, function (x) {',
  5243. ' console.log("c");',
  5244. '});',
  5245. '']));
  5246. end;
  5247. procedure TTestModule.TestEnum_Name;
  5248. begin
  5249. StartProgram(false);
  5250. Add('type TMyEnum = (Red, Green, Blue);');
  5251. Add('var e: TMyEnum;');
  5252. Add('var f: TMyEnum = Blue;');
  5253. Add('begin');
  5254. Add(' e:=green;');
  5255. Add(' e:=default(TMyEnum);');
  5256. ConvertProgram;
  5257. CheckSource('TestEnum_Name',
  5258. LinesToStr([ // statements
  5259. 'this.TMyEnum = {',
  5260. ' "0":"Red",',
  5261. ' Red:0,',
  5262. ' "1":"Green",',
  5263. ' Green:1,',
  5264. ' "2":"Blue",',
  5265. ' Blue:2',
  5266. ' };',
  5267. 'this.e = 0;',
  5268. 'this.f = this.TMyEnum.Blue;'
  5269. ]),
  5270. LinesToStr([
  5271. '$mod.e=$mod.TMyEnum.Green;',
  5272. '$mod.e=$mod.TMyEnum.Red;'
  5273. ]));
  5274. end;
  5275. procedure TTestModule.TestEnum_Number;
  5276. begin
  5277. Converter.Options:=Converter.Options+[coEnumNumbers];
  5278. StartProgram(false);
  5279. Add('type TMyEnum = (Red, Green);');
  5280. Add('var');
  5281. Add(' e: TMyEnum;');
  5282. Add(' f: TMyEnum = Green;');
  5283. Add(' i: longint;');
  5284. Add('begin');
  5285. Add(' e:=green;');
  5286. Add(' i:=longint(e);');
  5287. ConvertProgram;
  5288. CheckSource('TestEnumNumber',
  5289. LinesToStr([ // statements
  5290. 'this.TMyEnum = {',
  5291. ' "0":"Red",',
  5292. ' Red:0,',
  5293. ' "1":"Green",',
  5294. ' Green:1',
  5295. ' };',
  5296. 'this.e = 0;',
  5297. 'this.f = 1;',
  5298. 'this.i = 0;'
  5299. ]),
  5300. LinesToStr([
  5301. '$mod.e=1;',
  5302. '$mod.i=$mod.e;'
  5303. ]));
  5304. end;
  5305. procedure TTestModule.TestEnum_ConstFail;
  5306. begin
  5307. StartProgram(false);
  5308. Add([
  5309. 'type TMyEnum = (Red = 100, Green = 101);',
  5310. 'var',
  5311. ' e: TMyEnum;',
  5312. ' f: TMyEnum = Green;',
  5313. 'begin',
  5314. ' e:=green;']);
  5315. SetExpectedPasResolverError('not yet implemented: Red:TPasEnumValue [20180126202434] "enum const"',3002);
  5316. ConvertProgram;
  5317. end;
  5318. procedure TTestModule.TestEnum_Functions;
  5319. begin
  5320. StartProgram(false);
  5321. Add([
  5322. 'type TMyEnum = (Red, Green);',
  5323. 'procedure DoIt(var e: TMyEnum; var i: word);',
  5324. 'var',
  5325. ' v: longint;',
  5326. ' s: string;',
  5327. 'begin',
  5328. ' val(s,e,v);',
  5329. ' val(s,e,i);',
  5330. 'end;',
  5331. 'var',
  5332. ' e: TMyEnum;',
  5333. ' i: longint;',
  5334. ' s: string;',
  5335. ' b: boolean;',
  5336. 'begin',
  5337. ' i:=ord(red);',
  5338. ' i:=ord(green);',
  5339. ' i:=ord(e);',
  5340. ' i:=ord(b);',
  5341. ' e:=low(tmyenum);',
  5342. ' e:=low(e);',
  5343. ' b:=low(boolean);',
  5344. ' e:=high(tmyenum);',
  5345. ' e:=high(e);',
  5346. ' b:=high(boolean);',
  5347. ' e:=pred(green);',
  5348. ' e:=pred(e);',
  5349. ' b:=pred(b);',
  5350. ' e:=succ(red);',
  5351. ' e:=succ(e);',
  5352. ' b:=succ(b);',
  5353. ' e:=tmyenum(1);',
  5354. ' e:=tmyenum(i);',
  5355. ' s:=str(e);',
  5356. ' str(e,s);',
  5357. ' str(red,s);',
  5358. ' s:=str(e:3);',
  5359. ' writestr(s,e:3,red);',
  5360. ' val(s,e,i);',
  5361. ' i:=longint(e);']);
  5362. ConvertProgram;
  5363. CheckSource('TestEnum_Functions',
  5364. LinesToStr([ // statements
  5365. 'this.TMyEnum = {',
  5366. ' "0":"Red",',
  5367. ' Red:0,',
  5368. ' "1":"Green",',
  5369. ' Green:1',
  5370. ' };',
  5371. 'this.DoIt = function (e, i) {',
  5372. ' var v = 0;',
  5373. ' var s = "";',
  5374. ' e.set(rtl.valEnum(s, $mod.TMyEnum, function (w) {',
  5375. ' v = w;',
  5376. ' }));',
  5377. ' e.set(rtl.valEnum(s, $mod.TMyEnum, i.set));',
  5378. '};',
  5379. 'this.e = 0;',
  5380. 'this.i = 0;',
  5381. 'this.s = "";',
  5382. 'this.b = false;',
  5383. '']),
  5384. LinesToStr([
  5385. '$mod.i=$mod.TMyEnum.Red;',
  5386. '$mod.i=$mod.TMyEnum.Green;',
  5387. '$mod.i=$mod.e;',
  5388. '$mod.i=$mod.b+0;',
  5389. '$mod.e=$mod.TMyEnum.Red;',
  5390. '$mod.e=$mod.TMyEnum.Red;',
  5391. '$mod.b=false;',
  5392. '$mod.e=$mod.TMyEnum.Green;',
  5393. '$mod.e=$mod.TMyEnum.Green;',
  5394. '$mod.b=true;',
  5395. '$mod.e=$mod.TMyEnum.Green-1;',
  5396. '$mod.e=$mod.e-1;',
  5397. '$mod.b=false;',
  5398. '$mod.e=$mod.TMyEnum.Red+1;',
  5399. '$mod.e=$mod.e+1;',
  5400. '$mod.b=true;',
  5401. '$mod.e=1;',
  5402. '$mod.e=$mod.i;',
  5403. '$mod.s = $mod.TMyEnum[$mod.e];',
  5404. '$mod.s = $mod.TMyEnum[$mod.e];',
  5405. '$mod.s = $mod.TMyEnum[$mod.TMyEnum.Red];',
  5406. '$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3);',
  5407. '$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3)+$mod.TMyEnum[$mod.TMyEnum.Red];',
  5408. '$mod.e = rtl.valEnum($mod.s, $mod.TMyEnum, function (v) {',
  5409. ' $mod.i = v;',
  5410. '});',
  5411. '$mod.i=$mod.e;',
  5412. '']));
  5413. end;
  5414. procedure TTestModule.TestEnumRg_Functions;
  5415. begin
  5416. StartProgram(false);
  5417. Add([
  5418. 'type',
  5419. ' TEnum = (Red, Green, Blue);',
  5420. ' TEnumRg = Green..Blue;',
  5421. 'procedure DoIt(var e: TEnumRg; var i: word);',
  5422. 'var',
  5423. ' v: longint;',
  5424. ' s: string;',
  5425. 'begin',
  5426. ' val(s,e,v);',
  5427. ' val(s,e,i);',
  5428. 'end;',
  5429. 'var',
  5430. ' e: TEnumRg;',
  5431. ' i: longint;',
  5432. ' s: string;',
  5433. 'begin',
  5434. ' i:=ord(green);',
  5435. ' i:=ord(e);',
  5436. ' e:=low(tenumrg);',
  5437. ' e:=low(e);',
  5438. ' e:=high(tenumrg);',
  5439. ' e:=high(e);',
  5440. ' e:=pred(blue);',
  5441. ' e:=pred(e);',
  5442. ' e:=succ(green);',
  5443. ' e:=succ(e);',
  5444. ' e:=tenumrg(1);',
  5445. ' e:=tenumrg(i);',
  5446. ' s:=str(e);',
  5447. ' str(e,s);',
  5448. ' str(red,s);',
  5449. ' s:=str(e:3);',
  5450. ' writestr(s,e:3,blue);',
  5451. ' val(s,e,i);',
  5452. ' i:=longint(e);']);
  5453. ConvertProgram;
  5454. CheckSource('TestEnumRg_Functions',
  5455. LinesToStr([ // statements
  5456. 'this.TEnum = {',
  5457. ' "0":"Red",',
  5458. ' Red:0,',
  5459. ' "1":"Green",',
  5460. ' Green:1,',
  5461. ' "2":"Blue",',
  5462. ' Blue:2',
  5463. ' };',
  5464. 'this.DoIt = function (e, i) {',
  5465. ' var v = 0;',
  5466. ' var s = "";',
  5467. ' e.set(rtl.valEnum(s, $mod.TEnum, function (w) {',
  5468. ' v = w;',
  5469. ' }));',
  5470. ' e.set(rtl.valEnum(s, $mod.TEnum, i.set));',
  5471. '};',
  5472. 'this.e = this.TEnum.Green;',
  5473. 'this.i = 0;',
  5474. 'this.s = "";',
  5475. '']),
  5476. LinesToStr([
  5477. '$mod.i=$mod.TEnum.Green;',
  5478. '$mod.i=$mod.e;',
  5479. '$mod.e=$mod.TEnum.Green;',
  5480. '$mod.e=$mod.TEnum.Green;',
  5481. '$mod.e=$mod.TEnum.Blue;',
  5482. '$mod.e=$mod.TEnum.Blue;',
  5483. '$mod.e=$mod.TEnum.Blue-1;',
  5484. '$mod.e=$mod.e-1;',
  5485. '$mod.e=$mod.TEnum.Green+1;',
  5486. '$mod.e=$mod.e+1;',
  5487. '$mod.e=1;',
  5488. '$mod.e=$mod.i;',
  5489. '$mod.s = $mod.TEnum[$mod.e];',
  5490. '$mod.s = $mod.TEnum[$mod.e];',
  5491. '$mod.s = $mod.TEnum[$mod.TEnum.Red];',
  5492. '$mod.s = rtl.spaceLeft($mod.TEnum[$mod.e], 3);',
  5493. '$mod.s = rtl.spaceLeft($mod.TEnum[$mod.e], 3)+$mod.TEnum[$mod.TEnum.Blue];',
  5494. '$mod.e = rtl.valEnum($mod.s, $mod.TEnum, function (v) {',
  5495. ' $mod.i = v;',
  5496. '});',
  5497. '$mod.i=$mod.e;',
  5498. '']));
  5499. end;
  5500. procedure TTestModule.TestEnum_AsParams;
  5501. begin
  5502. StartProgram(false);
  5503. Add('type TEnum = (Red,Blue);');
  5504. Add('procedure DoIt(vG: TEnum; const vH: TEnum; var vI: TEnum);');
  5505. Add('var vJ: TEnum;');
  5506. Add('begin');
  5507. Add(' vg:=vg;');
  5508. Add(' vj:=vh;');
  5509. Add(' vi:=vi;');
  5510. Add(' doit(vg,vg,vg);');
  5511. Add(' doit(vh,vh,vj);');
  5512. Add(' doit(vi,vi,vi);');
  5513. Add(' doit(vj,vj,vj);');
  5514. Add('end;');
  5515. Add('var i: TEnum;');
  5516. Add('begin');
  5517. Add(' doit(i,i,i);');
  5518. ConvertProgram;
  5519. CheckSource('TestEnum_AsParams',
  5520. LinesToStr([ // statements
  5521. 'this.TEnum = {',
  5522. ' "0": "Red",',
  5523. ' Red: 0,',
  5524. ' "1": "Blue",',
  5525. ' Blue: 1',
  5526. '};',
  5527. 'this.DoIt = function (vG,vH,vI) {',
  5528. ' var vJ = 0;',
  5529. ' vG = vG;',
  5530. ' vJ = vH;',
  5531. ' vI.set(vI.get());',
  5532. ' $mod.DoIt(vG, vG, {',
  5533. ' get: function () {',
  5534. ' return vG;',
  5535. ' },',
  5536. ' set: function (v) {',
  5537. ' vG = v;',
  5538. ' }',
  5539. ' });',
  5540. ' $mod.DoIt(vH, vH, {',
  5541. ' get: function () {',
  5542. ' return vJ;',
  5543. ' },',
  5544. ' set: function (v) {',
  5545. ' vJ = v;',
  5546. ' }',
  5547. ' });',
  5548. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  5549. ' $mod.DoIt(vJ, vJ, {',
  5550. ' get: function () {',
  5551. ' return vJ;',
  5552. ' },',
  5553. ' set: function (v) {',
  5554. ' vJ = v;',
  5555. ' }',
  5556. ' });',
  5557. '};',
  5558. 'this.i = 0;'
  5559. ]),
  5560. LinesToStr([
  5561. '$mod.DoIt($mod.i,$mod.i,{',
  5562. ' p: $mod,',
  5563. ' get: function () {',
  5564. ' return this.p.i;',
  5565. ' },',
  5566. ' set: function (v) {',
  5567. ' this.p.i = v;',
  5568. ' }',
  5569. '});'
  5570. ]));
  5571. end;
  5572. procedure TTestModule.TestEnumRange_Array;
  5573. begin
  5574. StartProgram(false);
  5575. Add([
  5576. 'type',
  5577. ' TEnum = (Red, Green, Blue);',
  5578. ' TEnumRg = green..blue;',
  5579. ' TArr = array[TEnumRg] of byte;',
  5580. ' TArr2 = array[green..blue] of byte;',
  5581. 'var',
  5582. ' a: TArr;',
  5583. ' b: TArr = (3,4);',
  5584. ' c: TArr2 = (5,6);',
  5585. 'begin',
  5586. ' a[green] := b[blue];',
  5587. ' c[green] := c[blue];',
  5588. '']);
  5589. ConvertProgram;
  5590. CheckSource('TestEnumRange_Array',
  5591. LinesToStr([ // statements
  5592. 'this.TEnum = {',
  5593. ' "0": "Red",',
  5594. ' Red: 0,',
  5595. ' "1": "Green",',
  5596. ' Green: 1,',
  5597. ' "2": "Blue",',
  5598. ' Blue: 2',
  5599. '};',
  5600. 'this.a = rtl.arraySetLength(null, 0, 2);',
  5601. 'this.b = [3, 4];',
  5602. 'this.c = [5, 6];',
  5603. '']),
  5604. LinesToStr([
  5605. ' $mod.a[$mod.TEnum.Green - 1] = $mod.b[$mod.TEnum.Blue - 1];',
  5606. ' $mod.c[$mod.TEnum.Green - 1] = $mod.c[$mod.TEnum.Blue - 1];',
  5607. '']));
  5608. end;
  5609. procedure TTestModule.TestEnum_ForIn;
  5610. begin
  5611. StartProgram(false);
  5612. Add([
  5613. 'type',
  5614. ' TEnum = (Red, Green, Blue);',
  5615. ' TEnumRg = green..blue;',
  5616. ' TArr = array[TEnum] of byte;',
  5617. ' TArrRg = array[TEnumRg] of byte;',
  5618. 'var',
  5619. ' e: TEnum;',
  5620. ' a1: TArr = (3,4,5);',
  5621. ' a2: TArrRg = (11,12);',
  5622. ' b: byte;',
  5623. 'begin',
  5624. ' for e in TEnum do ;',
  5625. ' for e in TEnumRg do ;',
  5626. ' for e in TArr do ;',
  5627. ' for e in TArrRg do ;',
  5628. ' for b in a1 do ;',
  5629. ' for b in a2 do ;',
  5630. '']);
  5631. ConvertProgram;
  5632. CheckSource('TestEnum_ForIn',
  5633. LinesToStr([ // statements
  5634. 'this.TEnum = {',
  5635. ' "0": "Red",',
  5636. ' Red: 0,',
  5637. ' "1": "Green",',
  5638. ' Green: 1,',
  5639. ' "2": "Blue",',
  5640. ' Blue: 2',
  5641. '};',
  5642. 'this.e = 0;',
  5643. 'this.a1 = [3, 4, 5];',
  5644. 'this.a2 = [11, 12];',
  5645. 'this.b = 0;',
  5646. '']),
  5647. LinesToStr([
  5648. ' for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  5649. ' for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  5650. ' for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  5651. ' for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  5652. ' for (var $in = $mod.a1, $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) $mod.b = $in[$l];',
  5653. ' for (var $in1 = $mod.a2, $l1 = 0, $end1 = rtl.length($in1) - 1; $l1 <= $end1; $l1++) $mod.b = $in1[$l1];',
  5654. '']));
  5655. end;
  5656. procedure TTestModule.TestEnum_ScopedNumber;
  5657. begin
  5658. Converter.Options:=Converter.Options+[coEnumNumbers];
  5659. StartProgram(false);
  5660. Add([
  5661. 'type',
  5662. ' TEnum = (Red, Green);',
  5663. 'var',
  5664. ' e: TEnum;',
  5665. 'begin',
  5666. ' e:=TEnum.Green;',
  5667. '']);
  5668. ConvertProgram;
  5669. CheckSource('TestEnum_ScopedNumber',
  5670. LinesToStr([ // statements
  5671. 'this.TEnum = {',
  5672. ' "0": "Red",',
  5673. ' Red: 0,',
  5674. ' "1": "Green",',
  5675. ' Green: 1',
  5676. '};',
  5677. 'this.e = 0;',
  5678. '']),
  5679. LinesToStr([
  5680. '$mod.e = 1;']));
  5681. end;
  5682. procedure TTestModule.TestEnum_InFunction;
  5683. begin
  5684. StartProgram(false);
  5685. Add([
  5686. 'const TEnum = 3;',
  5687. 'procedure DoIt;',
  5688. 'type',
  5689. ' TEnum = (Red, Green, Blue);',
  5690. ' procedure Sub;',
  5691. ' type',
  5692. ' TEnumSub = (Left, Right);',
  5693. ' var',
  5694. ' es: TEnumSub;',
  5695. ' begin',
  5696. ' es:=Left;',
  5697. ' end;',
  5698. 'var',
  5699. ' e, e2: TEnum;',
  5700. 'begin',
  5701. ' if e in [red,blue] then e2:=e;',
  5702. 'end;',
  5703. 'begin']);
  5704. ConvertProgram;
  5705. CheckSource('TestEnum_InFunction',
  5706. LinesToStr([ // statements
  5707. 'this.TEnum = 3;',
  5708. 'var TEnum$1 = {',
  5709. ' "0":"Red",',
  5710. ' Red:0,',
  5711. ' "1":"Green",',
  5712. ' Green:1,',
  5713. ' "2":"Blue",',
  5714. ' Blue:2',
  5715. ' };',
  5716. 'var TEnumSub = {',
  5717. ' "0": "Left",',
  5718. ' Left: 0,',
  5719. ' "1": "Right",',
  5720. ' Right: 1',
  5721. '};',
  5722. 'this.DoIt = function () {',
  5723. ' function Sub() {',
  5724. ' var es = 0;',
  5725. ' es = TEnumSub.Left;',
  5726. ' };',
  5727. ' var e = 0;',
  5728. ' var e2 = 0;',
  5729. ' if (e in rtl.createSet(TEnum$1.Red, TEnum$1.Blue)) e2 = e;',
  5730. '};',
  5731. '']),
  5732. LinesToStr([
  5733. '']));
  5734. end;
  5735. procedure TTestModule.TestEnum_Name_Anonymous_Unit;
  5736. begin
  5737. StartUnit(true);
  5738. Add([
  5739. 'interface',
  5740. 'var color: (red, green);',
  5741. 'implementation',
  5742. 'initialization',
  5743. ' color:=green;',
  5744. '']);
  5745. ConvertUnit;
  5746. CheckSource('TestEnum_Name_Anonymous_Unit',
  5747. LinesToStr([
  5748. 'this.color$a = {',
  5749. ' "0": "red",',
  5750. ' red: 0,',
  5751. ' "1": "green",',
  5752. ' green: 1',
  5753. '};',
  5754. 'this.color = 0;',
  5755. '']),
  5756. LinesToStr([ // this.$init
  5757. '$mod.color = $mod.color$a.green;',
  5758. '']),
  5759. LinesToStr([ // implementation
  5760. '']) );
  5761. end;
  5762. procedure TTestModule.TestSet_Enum;
  5763. begin
  5764. StartProgram(false);
  5765. Add([
  5766. 'type',
  5767. ' TColor = (Red, Green, Blue);',
  5768. ' TColors = set of TColor;',
  5769. 'var',
  5770. ' c: TColor;',
  5771. ' s: TColors;',
  5772. ' t: TColors = [];',
  5773. ' u: TColors = [Red];',
  5774. 'begin',
  5775. ' s:=[];',
  5776. ' s:=[Green];',
  5777. ' s:=[Green,Blue];',
  5778. ' s:=[Red..Blue];',
  5779. ' s:=[Red,Green..Blue];',
  5780. ' s:=[Red,c];',
  5781. ' s:=t;',
  5782. ' s:=default(TColors);',
  5783. '']);
  5784. ConvertProgram;
  5785. CheckSource('TestSet',
  5786. LinesToStr([ // statements
  5787. 'this.TColor = {',
  5788. ' "0":"Red",',
  5789. ' Red:0,',
  5790. ' "1":"Green",',
  5791. ' Green:1,',
  5792. ' "2":"Blue",',
  5793. ' Blue:2',
  5794. ' };',
  5795. 'this.c = 0;',
  5796. 'this.s = {};',
  5797. 'this.t = {};',
  5798. 'this.u = rtl.createSet(this.TColor.Red);'
  5799. ]),
  5800. LinesToStr([
  5801. '$mod.s={};',
  5802. '$mod.s=rtl.createSet($mod.TColor.Green);',
  5803. '$mod.s=rtl.createSet($mod.TColor.Green,$mod.TColor.Blue);',
  5804. '$mod.s=rtl.createSet(null,$mod.TColor.Red,$mod.TColor.Blue);',
  5805. '$mod.s=rtl.createSet($mod.TColor.Red,null,$mod.TColor.Green,$mod.TColor.Blue);',
  5806. '$mod.s=rtl.createSet($mod.TColor.Red,$mod.c);',
  5807. '$mod.s=rtl.refSet($mod.t);',
  5808. '$mod.s={};',
  5809. '']));
  5810. end;
  5811. procedure TTestModule.TestSet_Operators;
  5812. begin
  5813. StartProgram(false);
  5814. Add('type');
  5815. Add(' TColor = (Red, Green, Blue);');
  5816. Add(' TColors = set of tcolor;');
  5817. Add('var');
  5818. Add(' vC: TColor;');
  5819. Add(' vS: TColors;');
  5820. Add(' vT: TColors;');
  5821. Add(' vU: TColors;');
  5822. Add(' B: boolean;');
  5823. Add('begin');
  5824. Add(' include(vs,green);');
  5825. Add(' exclude(vs,vc);');
  5826. Add(' vs:=vt+vu;');
  5827. Add(' vs:=vt+[red];');
  5828. Add(' vs:=[red]+vt;');
  5829. Add(' vs:=[red]+[green];');
  5830. Add(' vs:=vt-vu;');
  5831. Add(' vs:=vt-[red];');
  5832. Add(' vs:=[red]-vt;');
  5833. Add(' vs:=[red]-[green];');
  5834. Add(' vs:=vt*vu;');
  5835. Add(' vs:=vt*[red];');
  5836. Add(' vs:=[red]*vt;');
  5837. Add(' vs:=[red]*[green];');
  5838. Add(' vs:=vt><vu;');
  5839. Add(' vs:=vt><[red];');
  5840. Add(' vs:=[red]><vt;');
  5841. Add(' vs:=[red]><[green];');
  5842. Add(' b:=vt=vu;');
  5843. Add(' b:=vt=[red];');
  5844. Add(' b:=[red]=vt;');
  5845. Add(' b:=[red]=[green];');
  5846. Add(' b:=vt<>vu;');
  5847. Add(' b:=vt<>[red];');
  5848. Add(' b:=[red]<>vt;');
  5849. Add(' b:=[red]<>[green];');
  5850. Add(' b:=vt<=vu;');
  5851. Add(' b:=vt<=[red];');
  5852. Add(' b:=[red]<=vt;');
  5853. Add(' b:=[red]<=[green];');
  5854. Add(' b:=vt>=vu;');
  5855. Add(' b:=vt>=[red];');
  5856. Add(' b:=[red]>=vt;');
  5857. Add(' b:=[red]>=[green];');
  5858. ConvertProgram;
  5859. CheckSource('TestSet_Operators',
  5860. LinesToStr([ // statements
  5861. 'this.TColor = {',
  5862. ' "0":"Red",',
  5863. ' Red:0,',
  5864. ' "1":"Green",',
  5865. ' Green:1,',
  5866. ' "2":"Blue",',
  5867. ' Blue:2',
  5868. ' };',
  5869. 'this.vC = 0;',
  5870. 'this.vS = {};',
  5871. 'this.vT = {};',
  5872. 'this.vU = {};',
  5873. 'this.B = false;'
  5874. ]),
  5875. LinesToStr([
  5876. '$mod.vS = rtl.includeSet($mod.vS,$mod.TColor.Green);',
  5877. '$mod.vS = rtl.excludeSet($mod.vS,$mod.vC);',
  5878. '$mod.vS = rtl.unionSet($mod.vT, $mod.vU);',
  5879. '$mod.vS = rtl.unionSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5880. '$mod.vS = rtl.unionSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5881. '$mod.vS = rtl.unionSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5882. '$mod.vS = rtl.diffSet($mod.vT, $mod.vU);',
  5883. '$mod.vS = rtl.diffSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5884. '$mod.vS = rtl.diffSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5885. '$mod.vS = rtl.diffSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5886. '$mod.vS = rtl.intersectSet($mod.vT, $mod.vU);',
  5887. '$mod.vS = rtl.intersectSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5888. '$mod.vS = rtl.intersectSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5889. '$mod.vS = rtl.intersectSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5890. '$mod.vS = rtl.symDiffSet($mod.vT, $mod.vU);',
  5891. '$mod.vS = rtl.symDiffSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5892. '$mod.vS = rtl.symDiffSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5893. '$mod.vS = rtl.symDiffSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5894. '$mod.B = rtl.eqSet($mod.vT, $mod.vU);',
  5895. '$mod.B = rtl.eqSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5896. '$mod.B = rtl.eqSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5897. '$mod.B = rtl.eqSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5898. '$mod.B = rtl.neSet($mod.vT, $mod.vU);',
  5899. '$mod.B = rtl.neSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5900. '$mod.B = rtl.neSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5901. '$mod.B = rtl.neSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5902. '$mod.B = rtl.leSet($mod.vT, $mod.vU);',
  5903. '$mod.B = rtl.leSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5904. '$mod.B = rtl.leSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5905. '$mod.B = rtl.leSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5906. '$mod.B = rtl.geSet($mod.vT, $mod.vU);',
  5907. '$mod.B = rtl.geSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5908. '$mod.B = rtl.geSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5909. '$mod.B = rtl.geSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5910. '']));
  5911. end;
  5912. procedure TTestModule.TestSet_Operator_In;
  5913. begin
  5914. StartProgram(false);
  5915. Add([
  5916. 'type',
  5917. ' TColor = (Red, Green, Blue);',
  5918. ' TColors = set of tcolor;',
  5919. ' TColorRg = green..blue;',
  5920. 'var',
  5921. ' vC: tcolor;',
  5922. ' vT: tcolors;',
  5923. ' B: boolean;',
  5924. ' rg: TColorRg;',
  5925. 'begin',
  5926. ' b:=red in vt;',
  5927. ' b:=vc in vt;',
  5928. ' b:=green in [red..blue];',
  5929. ' b:=vc in [red..blue];',
  5930. ' ',
  5931. ' if red in vt then ;',
  5932. ' while vC in vt do ;',
  5933. ' repeat',
  5934. ' until vC in vt;',
  5935. ' if rg in [green..blue] then ;',
  5936. '']);
  5937. ConvertProgram;
  5938. CheckSource('TestSet_Operator_In',
  5939. LinesToStr([ // statements
  5940. 'this.TColor = {',
  5941. ' "0":"Red",',
  5942. ' Red:0,',
  5943. ' "1":"Green",',
  5944. ' Green:1,',
  5945. ' "2":"Blue",',
  5946. ' Blue:2',
  5947. ' };',
  5948. 'this.vC = 0;',
  5949. 'this.vT = {};',
  5950. 'this.B = false;',
  5951. 'this.rg = this.TColor.Green;',
  5952. '']),
  5953. LinesToStr([
  5954. '$mod.B = $mod.TColor.Red in $mod.vT;',
  5955. '$mod.B = $mod.vC in $mod.vT;',
  5956. '$mod.B = $mod.TColor.Green in rtl.createSet(null, $mod.TColor.Red, $mod.TColor.Blue);',
  5957. '$mod.B = $mod.vC in rtl.createSet(null, $mod.TColor.Red, $mod.TColor.Blue);',
  5958. 'if ($mod.TColor.Red in $mod.vT) ;',
  5959. 'while ($mod.vC in $mod.vT) {',
  5960. '};',
  5961. 'do {',
  5962. '} while (!($mod.vC in $mod.vT));',
  5963. 'if ($mod.rg in rtl.createSet(null, $mod.TColor.Green, $mod.TColor.Blue)) ;',
  5964. '']));
  5965. end;
  5966. procedure TTestModule.TestSet_Functions;
  5967. begin
  5968. StartProgram(false);
  5969. Add('type');
  5970. Add(' TMyEnum = (Red, Green);');
  5971. Add(' TMyEnums = set of TMyEnum;');
  5972. Add('var');
  5973. Add(' e: TMyEnum;');
  5974. Add(' s: TMyEnums;');
  5975. Add('begin');
  5976. Add(' e:=Low(TMyEnums);');
  5977. Add(' e:=Low(s);');
  5978. Add(' e:=High(TMyEnums);');
  5979. Add(' e:=High(s);');
  5980. ConvertProgram;
  5981. CheckSource('TestSetFunctions',
  5982. LinesToStr([ // statements
  5983. 'this.TMyEnum = {',
  5984. ' "0":"Red",',
  5985. ' Red:0,',
  5986. ' "1":"Green",',
  5987. ' Green:1',
  5988. ' };',
  5989. 'this.e = 0;',
  5990. 'this.s = {};'
  5991. ]),
  5992. LinesToStr([
  5993. '$mod.e=$mod.TMyEnum.Red;',
  5994. '$mod.e=$mod.TMyEnum.Red;',
  5995. '$mod.e=$mod.TMyEnum.Green;',
  5996. '$mod.e=$mod.TMyEnum.Green;',
  5997. '']));
  5998. end;
  5999. procedure TTestModule.TestSet_PassAsArgClone;
  6000. begin
  6001. StartProgram(false);
  6002. Add('type');
  6003. Add(' TMyEnum = (Red, Green);');
  6004. Add(' TMyEnums = set of TMyEnum;');
  6005. Add('procedure DoDefault(s: tmyenums); begin end;');
  6006. Add('procedure DoConst(const s: tmyenums); begin end;');
  6007. Add('var');
  6008. Add(' aSet: tmyenums;');
  6009. Add('begin');
  6010. Add(' dodefault(aset);');
  6011. Add(' doconst(aset);');
  6012. ConvertProgram;
  6013. CheckSource('TestSetFunctions',
  6014. LinesToStr([ // statements
  6015. 'this.TMyEnum = {',
  6016. ' "0":"Red",',
  6017. ' Red:0,',
  6018. ' "1":"Green",',
  6019. ' Green:1',
  6020. ' };',
  6021. 'this.DoDefault = function (s) {',
  6022. '};',
  6023. 'this.DoConst = function (s) {',
  6024. '};',
  6025. 'this.aSet = {};'
  6026. ]),
  6027. LinesToStr([
  6028. '$mod.DoDefault(rtl.refSet($mod.aSet));',
  6029. '$mod.DoConst($mod.aSet);',
  6030. '']));
  6031. end;
  6032. procedure TTestModule.TestSet_AsParams;
  6033. begin
  6034. StartProgram(false);
  6035. Add([
  6036. 'type TEnum = (Red,Blue);',
  6037. 'type TEnums = set of TEnum;',
  6038. 'function DoIt(vG: TEnums; const vH: TEnums; var vI: TEnums): TEnums;',
  6039. 'var vJ: TEnums;',
  6040. 'begin',
  6041. ' Include(vg,red);',
  6042. ' Include(result,blue);',
  6043. ' vg:=vg;',
  6044. ' vj:=vh;',
  6045. ' vi:=vi;',
  6046. ' doit(vg,vg,vg);',
  6047. ' doit(vh,vh,vj);',
  6048. ' doit(vi,vi,vi);',
  6049. ' doit(vj,vj,vj);',
  6050. 'end;',
  6051. 'var i: TEnums;',
  6052. 'begin',
  6053. ' doit(i,i,i);']);
  6054. ConvertProgram;
  6055. CheckSource('TestSet_AsParams',
  6056. LinesToStr([ // statements
  6057. 'this.TEnum = {',
  6058. ' "0": "Red",',
  6059. ' Red: 0,',
  6060. ' "1": "Blue",',
  6061. ' Blue: 1',
  6062. '};',
  6063. 'this.DoIt = function (vG,vH,vI) {',
  6064. ' var Result = {};',
  6065. ' var vJ = {};',
  6066. ' vG = rtl.includeSet(vG, $mod.TEnum.Red);',
  6067. ' Result = rtl.includeSet(Result, $mod.TEnum.Blue);',
  6068. ' vG = rtl.refSet(vG);',
  6069. ' vJ = rtl.refSet(vH);',
  6070. ' vI.set(rtl.refSet(vI.get()));',
  6071. ' $mod.DoIt(rtl.refSet(vG), vG, {',
  6072. ' get: function () {',
  6073. ' return vG;',
  6074. ' },',
  6075. ' set: function (v) {',
  6076. ' vG = v;',
  6077. ' }',
  6078. ' });',
  6079. ' $mod.DoIt(rtl.refSet(vH), vH, {',
  6080. ' get: function () {',
  6081. ' return vJ;',
  6082. ' },',
  6083. ' set: function (v) {',
  6084. ' vJ = v;',
  6085. ' }',
  6086. ' });',
  6087. ' $mod.DoIt(rtl.refSet(vI.get()), vI.get(), vI);',
  6088. ' $mod.DoIt(rtl.refSet(vJ), vJ, {',
  6089. ' get: function () {',
  6090. ' return vJ;',
  6091. ' },',
  6092. ' set: function (v) {',
  6093. ' vJ = v;',
  6094. ' }',
  6095. ' });',
  6096. ' return Result;',
  6097. '};',
  6098. 'this.i = {};'
  6099. ]),
  6100. LinesToStr([
  6101. '$mod.DoIt(rtl.refSet($mod.i),$mod.i,{',
  6102. ' p: $mod,',
  6103. ' get: function () {',
  6104. ' return this.p.i;',
  6105. ' },',
  6106. ' set: function (v) {',
  6107. ' this.p.i = v;',
  6108. ' }',
  6109. '});'
  6110. ]));
  6111. end;
  6112. procedure TTestModule.TestSet_Property;
  6113. begin
  6114. StartProgram(false);
  6115. Add('type');
  6116. Add(' TEnum = (Red,Blue);');
  6117. Add(' TEnums = set of TEnum;');
  6118. Add(' TObject = class');
  6119. Add(' function GetColors: TEnums; external name ''GetColors'';');
  6120. Add(' procedure SetColors(const Value: TEnums); external name ''SetColors'';');
  6121. Add(' property Colors: TEnums read GetColors write SetColors;');
  6122. Add(' end;');
  6123. Add('procedure DoIt(i: TEnums; const j: TEnums; var k: TEnums; out l: TEnums);');
  6124. Add('begin end;');
  6125. Add('var Obj: TObject;');
  6126. Add('begin');
  6127. Add(' Include(Obj.Colors,Red);');
  6128. Add(' Exclude(Obj.Colors,Red);');
  6129. //Add(' DoIt(Obj.Colors,Obj.Colors,Obj.Colors,Obj.Colors);');
  6130. ConvertProgram;
  6131. CheckSource('TestSet_Property',
  6132. LinesToStr([ // statements
  6133. 'this.TEnum = {',
  6134. ' "0": "Red",',
  6135. ' Red: 0,',
  6136. ' "1": "Blue",',
  6137. ' Blue: 1',
  6138. '};',
  6139. 'rtl.createClass(this, "TObject", null, function () {',
  6140. ' this.$init = function () {',
  6141. ' };',
  6142. ' this.$final = function () {',
  6143. ' };',
  6144. '});',
  6145. 'this.DoIt = function (i, j, k, l) {',
  6146. '};',
  6147. 'this.Obj = null;',
  6148. '']),
  6149. LinesToStr([
  6150. '$mod.Obj.SetColors(rtl.includeSet($mod.Obj.GetColors(), $mod.TEnum.Red));',
  6151. '$mod.Obj.SetColors(rtl.excludeSet($mod.Obj.GetColors(), $mod.TEnum.Red));',
  6152. '']));
  6153. end;
  6154. procedure TTestModule.TestSet_EnumConst;
  6155. begin
  6156. StartProgram(false);
  6157. Add([
  6158. 'type',
  6159. ' TEnum = (Red,Blue);',
  6160. ' TEnums = set of TEnum;',
  6161. 'const',
  6162. ' Orange = red;',
  6163. 'var',
  6164. ' Enum: tenum;',
  6165. ' Enums: tenums;',
  6166. 'begin',
  6167. ' Include(enums,orange);',
  6168. ' Exclude(enums,orange);',
  6169. ' if orange in enums then;',
  6170. ' if orange in [orange,red] then;']);
  6171. ConvertProgram;
  6172. CheckSource('TestSet_EnumConst',
  6173. LinesToStr([ // statements
  6174. 'this.TEnum = {',
  6175. ' "0": "Red",',
  6176. ' Red: 0,',
  6177. ' "1": "Blue",',
  6178. ' Blue: 1',
  6179. '};',
  6180. 'this.Orange = this.TEnum.Red;',
  6181. 'this.Enum = 0;',
  6182. 'this.Enums = {};',
  6183. '']),
  6184. LinesToStr([
  6185. '$mod.Enums = rtl.includeSet($mod.Enums, $mod.TEnum.Red);',
  6186. '$mod.Enums = rtl.excludeSet($mod.Enums, $mod.TEnum.Red);',
  6187. 'if ($mod.TEnum.Red in $mod.Enums) ;',
  6188. 'if ($mod.TEnum.Red in rtl.createSet($mod.TEnum.Red, $mod.TEnum.Red)) ;',
  6189. '']));
  6190. end;
  6191. procedure TTestModule.TestSet_IntConst;
  6192. begin
  6193. StartProgram(false);
  6194. Add([
  6195. 'type',
  6196. ' TEnums = set of Byte;',
  6197. 'const',
  6198. ' Orange = 0;',
  6199. 'var',
  6200. ' Enum: byte;',
  6201. ' Enums: tenums;',
  6202. 'begin',
  6203. ' Enums:=[];',
  6204. ' Enums:=[0];',
  6205. ' Enums:=[1..2];',
  6206. //' Include(enums,orange);',
  6207. //' Exclude(enums,orange);',
  6208. ' if orange in enums then;',
  6209. ' if orange in [orange,1] then;']);
  6210. ConvertProgram;
  6211. CheckSource('TestSet_IntConst',
  6212. LinesToStr([ // statements
  6213. 'this.Orange = 0;',
  6214. 'this.Enum = 0;',
  6215. 'this.Enums = {};',
  6216. '']),
  6217. LinesToStr([
  6218. '$mod.Enums = {};',
  6219. '$mod.Enums = rtl.createSet(0);',
  6220. '$mod.Enums = rtl.createSet(null, 1, 2);',
  6221. 'if (0 in $mod.Enums) ;',
  6222. 'if (0 in rtl.createSet(0, 1)) ;',
  6223. '']));
  6224. end;
  6225. procedure TTestModule.TestSet_IntRange;
  6226. begin
  6227. StartProgram(false);
  6228. Add([
  6229. 'type',
  6230. ' TRange = 1..3;',
  6231. ' TEnums = set of TRange;',
  6232. 'const',
  6233. ' Orange = 2;',
  6234. 'var',
  6235. ' Enum: byte;',
  6236. ' Enums: TEnums;',
  6237. 'begin',
  6238. ' Enums:=[];',
  6239. ' Enums:=[1];',
  6240. ' Enums:=[2..3];',
  6241. ' Include(enums,orange);',
  6242. ' Exclude(enums,orange);',
  6243. ' if orange in enums then;',
  6244. ' if orange in [orange,1] then;']);
  6245. ConvertProgram;
  6246. CheckSource('TestSet_IntRange',
  6247. LinesToStr([ // statements
  6248. 'this.Orange = 2;',
  6249. 'this.Enum = 0;',
  6250. 'this.Enums = {};',
  6251. '']),
  6252. LinesToStr([
  6253. '$mod.Enums = {};',
  6254. '$mod.Enums = rtl.createSet(1);',
  6255. '$mod.Enums = rtl.createSet(null, 2, 3);',
  6256. '$mod.Enums = rtl.includeSet($mod.Enums, 2);',
  6257. '$mod.Enums = rtl.excludeSet($mod.Enums, 2);',
  6258. 'if (2 in $mod.Enums) ;',
  6259. 'if (2 in rtl.createSet(2, 1)) ;',
  6260. '']));
  6261. end;
  6262. procedure TTestModule.TestSet_AnonymousEnumType;
  6263. begin
  6264. StartProgram(false);
  6265. Add('type');
  6266. Add(' TFlags = set of (red, green);');
  6267. Add('const');
  6268. Add(' favorite = red;');
  6269. Add('var');
  6270. Add(' f: TFlags;');
  6271. Add(' i: longint;');
  6272. Add('begin');
  6273. Add(' Include(f,red);');
  6274. Add(' Include(f,favorite);');
  6275. Add(' i:=ord(red);');
  6276. Add(' i:=ord(favorite);');
  6277. Add(' i:=ord(low(TFlags));');
  6278. Add(' i:=ord(low(f));');
  6279. Add(' i:=ord(low(favorite));');
  6280. Add(' i:=ord(high(TFlags));');
  6281. Add(' i:=ord(high(f));');
  6282. Add(' i:=ord(high(favorite));');
  6283. Add(' f:=[green,favorite];');
  6284. ConvertProgram;
  6285. CheckSource('TestSet_AnonymousEnumType',
  6286. LinesToStr([ // statements
  6287. 'this.TFlags$a = {',
  6288. ' "0": "red",',
  6289. ' red: 0,',
  6290. ' "1": "green",',
  6291. ' green: 1',
  6292. '};',
  6293. 'this.favorite = this.TFlags$a.red;',
  6294. 'this.f = {};',
  6295. 'this.i = 0;',
  6296. '']),
  6297. LinesToStr([
  6298. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  6299. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  6300. '$mod.i = $mod.TFlags$a.red;',
  6301. '$mod.i = $mod.TFlags$a.red;',
  6302. '$mod.i = $mod.TFlags$a.red;',
  6303. '$mod.i = $mod.TFlags$a.red;',
  6304. '$mod.i = $mod.TFlags$a.red;',
  6305. '$mod.i = $mod.TFlags$a.green;',
  6306. '$mod.i = $mod.TFlags$a.green;',
  6307. '$mod.i = $mod.TFlags$a.green;',
  6308. '$mod.f = rtl.createSet($mod.TFlags$a.green, $mod.TFlags$a.red);',
  6309. '']));
  6310. end;
  6311. procedure TTestModule.TestSet_AnonymousEnumTypeChar;
  6312. begin
  6313. exit;
  6314. StartProgram(false);
  6315. Add([
  6316. 'type',
  6317. ' TAtoZ = ''A''..''Z'';',
  6318. ' TSetOfAZ = set of TAtoZ;',
  6319. 'var',
  6320. ' c: char;',
  6321. ' a: TAtoZ;',
  6322. ' s: TSetOfAZ = [''P'',''A''];',
  6323. ' i: longint;',
  6324. 'begin',
  6325. ' Include(s,''S'');',
  6326. ' Include(s,c);',
  6327. ' Include(s,a);',
  6328. ' c:=low(TAtoZ);',
  6329. ' i:=ord(low(TAtoZ));',
  6330. ' a:=high(TAtoZ);',
  6331. ' a:=high(TSetOfAtoZ);',
  6332. ' s:=[a,c,''M''];',
  6333. '']);
  6334. ConvertProgram;
  6335. CheckSource('TestSet_AnonymousEnumTypeChar',
  6336. LinesToStr([ // statements
  6337. '']),
  6338. LinesToStr([
  6339. '']));
  6340. end;
  6341. procedure TTestModule.TestSet_ConstEnum;
  6342. begin
  6343. StartProgram(false);
  6344. Add([
  6345. 'type',
  6346. ' TEnum = (red,blue,green);',
  6347. ' TEnums = set of TEnum;',
  6348. 'const',
  6349. ' teAny = [low(TEnum)..high(TEnum)];',
  6350. ' teRedBlue = [low(TEnum)..pred(high(TEnum))];',
  6351. 'var',
  6352. ' e: TEnum;',
  6353. ' s: TEnums;',
  6354. 'begin',
  6355. ' if blue in teAny then;',
  6356. ' if blue in teAny+[e] then;',
  6357. ' if blue in teAny+teRedBlue then;',
  6358. ' if e in [red,blue] then;',
  6359. ' s:=teAny;',
  6360. ' s:=teAny+[e];',
  6361. ' s:=[e]+teAny;',
  6362. ' s:=teAny+teRedBlue;',
  6363. ' s:=teAny+teRedBlue+[e];',
  6364. '']);
  6365. ConvertProgram;
  6366. CheckSource('TestSet_ConstEnum',
  6367. LinesToStr([ // statements
  6368. 'this.TEnum = {',
  6369. ' "0": "red",',
  6370. ' red: 0,',
  6371. ' "1": "blue",',
  6372. ' blue: 1,',
  6373. ' "2": "green",',
  6374. ' green: 2',
  6375. '};',
  6376. 'this.teAny = rtl.createSet(null, this.TEnum.red, this.TEnum.green);',
  6377. 'this.teRedBlue = rtl.createSet(null, this.TEnum.red, this.TEnum.green - 1);',
  6378. 'this.e = 0;',
  6379. 'this.s = {};',
  6380. '']),
  6381. LinesToStr([
  6382. 'if ($mod.TEnum.blue in $mod.teAny) ;',
  6383. 'if ($mod.TEnum.blue in rtl.unionSet($mod.teAny, rtl.createSet($mod.e))) ;',
  6384. 'if ($mod.TEnum.blue in rtl.unionSet($mod.teAny, $mod.teRedBlue)) ;',
  6385. 'if ($mod.e in rtl.createSet($mod.TEnum.red, $mod.TEnum.blue)) ;',
  6386. '$mod.s = rtl.refSet($mod.teAny);',
  6387. '$mod.s = rtl.unionSet($mod.teAny, rtl.createSet($mod.e));',
  6388. '$mod.s = rtl.unionSet(rtl.createSet($mod.e), $mod.teAny);',
  6389. '$mod.s = rtl.unionSet($mod.teAny, $mod.teRedBlue);',
  6390. '$mod.s = rtl.unionSet(rtl.unionSet($mod.teAny, $mod.teRedBlue), rtl.createSet($mod.e));',
  6391. '']));
  6392. end;
  6393. procedure TTestModule.TestSet_ConstChar;
  6394. begin
  6395. StartProgram(false);
  6396. Add([
  6397. 'const',
  6398. ' LowChars = [''a''..''z''];',
  6399. ' Chars = LowChars+[''A''..''Z''];',
  6400. ' sc = [''А'', ''Я''];',
  6401. 'var',
  6402. ' c: char;',
  6403. ' s: string;',
  6404. 'begin',
  6405. ' if c in lowchars then ;',
  6406. ' if ''a'' in lowchars then ;',
  6407. ' if s[1] in lowchars then ;',
  6408. ' if c in chars then ;',
  6409. ' if c in [''a''..''z'',''_''] then ;',
  6410. ' if ''b'' in [''a''..''z'',''_''] then ;',
  6411. ' if ''Я'' in sc then ;',
  6412. ' if 3=ord('' '') then ;',
  6413. '']);
  6414. ConvertProgram;
  6415. CheckSource('TestSet_ConstChar',
  6416. LinesToStr([ // statements
  6417. 'this.LowChars = rtl.createSet(null, 97, 122);',
  6418. 'this.Chars = rtl.unionSet(this.LowChars, rtl.createSet(null, 65, 90));',
  6419. 'this.sc = rtl.createSet(1040, 1071);',
  6420. 'this.c = "";',
  6421. 'this.s = "";',
  6422. '']),
  6423. LinesToStr([
  6424. 'if ($mod.c.charCodeAt() in $mod.LowChars) ;',
  6425. 'if (97 in $mod.LowChars) ;',
  6426. 'if ($mod.s.charCodeAt(0) in $mod.LowChars) ;',
  6427. 'if ($mod.c.charCodeAt() in $mod.Chars) ;',
  6428. 'if ($mod.c.charCodeAt() in rtl.createSet(null, 97, 122, 95)) ;',
  6429. 'if (98 in rtl.createSet(null, 97, 122, 95)) ;',
  6430. 'if (1071 in $mod.sc) ;',
  6431. 'if (3 === 32) ;',
  6432. '']));
  6433. end;
  6434. procedure TTestModule.TestSet_ConstInt;
  6435. begin
  6436. StartProgram(false);
  6437. Add([
  6438. 'const',
  6439. ' Months = [1..12];',
  6440. ' Mirror = [-12..-1]+Months;',
  6441. 'var',
  6442. ' i: smallint;',
  6443. 'begin',
  6444. ' if 3 in Months then;',
  6445. ' if i in Months+[i] then;',
  6446. ' if i in Months+Mirror then;',
  6447. ' if i in [4..6,8] then;',
  6448. '']);
  6449. ConvertProgram;
  6450. CheckSource('TestSet_ConstInt',
  6451. LinesToStr([ // statements
  6452. 'this.Months = rtl.createSet(null, 1, 12);',
  6453. 'this.Mirror = rtl.unionSet(rtl.createSet(null, -12, -1), this.Months);',
  6454. 'this.i = 0;',
  6455. '']),
  6456. LinesToStr([
  6457. 'if (3 in $mod.Months) ;',
  6458. 'if ($mod.i in rtl.unionSet($mod.Months, rtl.createSet($mod.i))) ;',
  6459. 'if ($mod.i in rtl.unionSet($mod.Months, $mod.Mirror)) ;',
  6460. 'if ($mod.i in rtl.createSet(null, 4, 6, 8)) ;',
  6461. '']));
  6462. end;
  6463. procedure TTestModule.TestSet_InFunction;
  6464. begin
  6465. StartProgram(false);
  6466. Add([
  6467. 'const',
  6468. ' TEnum = 3;',
  6469. ' TSetOfEnum = 4;',
  6470. ' TSetOfAno = 5;',
  6471. 'procedure DoIt;',
  6472. 'type',
  6473. ' TEnum = (red, blue);',
  6474. ' TSetOfEnum = set of TEnum;',
  6475. ' TSetOfAno = set of (up,down);',
  6476. 'var',
  6477. ' e: TEnum;',
  6478. ' se: TSetOfEnum;',
  6479. ' sa: TSetOfAno;',
  6480. 'begin',
  6481. ' se:=[e];',
  6482. ' sa:=[up];',
  6483. 'end;',
  6484. 'begin',
  6485. '']);
  6486. ConvertProgram;
  6487. CheckSource('TestSet_InFunction',
  6488. LinesToStr([ // statements
  6489. 'this.TEnum = 3;',
  6490. 'this.TSetOfEnum = 4;',
  6491. 'this.TSetOfAno = 5;',
  6492. 'var TEnum$1 = {',
  6493. ' "0": "red",',
  6494. ' red: 0,',
  6495. ' "1": "blue",',
  6496. ' blue: 1',
  6497. '};',
  6498. 'var TSetOfAno$a = {',
  6499. ' "0": "up",',
  6500. ' up: 0,',
  6501. ' "1": "down",',
  6502. ' down: 1',
  6503. '};',
  6504. 'this.DoIt = function () {',
  6505. ' var e = 0;',
  6506. ' var se = {};',
  6507. ' var sa = {};',
  6508. ' se = rtl.createSet(e);',
  6509. ' sa = rtl.createSet(TSetOfAno$a.up);',
  6510. '};',
  6511. '']),
  6512. LinesToStr([
  6513. '']));
  6514. end;
  6515. procedure TTestModule.TestSet_ForIn;
  6516. begin
  6517. StartProgram(false);
  6518. Add([
  6519. 'type',
  6520. ' TEnum = (Red, Green, Blue);',
  6521. ' TEnumRg = green..blue;',
  6522. ' TSetOfEnum = set of TEnum;',
  6523. ' TSetOfEnumRg = set of TEnumRg;',
  6524. 'var',
  6525. ' e, e2: TEnum;',
  6526. ' er: TEnum;',
  6527. ' s: TSetOfEnum;',
  6528. 'begin',
  6529. ' for e in TSetOfEnum do ;',
  6530. ' for e in TSetOfEnumRg do ;',
  6531. ' for e in [] do e2:=e;',
  6532. ' for e in [red..green] do e2:=e;',
  6533. ' for e in [green,blue] do e2:=e;',
  6534. ' for e in [red,blue] do e2:=e;',
  6535. ' for e in s do e2:=e;',
  6536. ' for er in TSetOfEnumRg do ;',
  6537. '']);
  6538. ConvertProgram;
  6539. CheckSource('TestSet_ForIn',
  6540. LinesToStr([ // statements
  6541. 'this.TEnum = {',
  6542. ' "0":"Red",',
  6543. ' Red:0,',
  6544. ' "1":"Green",',
  6545. ' Green:1,',
  6546. ' "2":"Blue",',
  6547. ' Blue:2',
  6548. ' };',
  6549. 'this.e = 0;',
  6550. 'this.e2 = 0;',
  6551. 'this.er = 0;',
  6552. 'this.s = {};',
  6553. '']),
  6554. LinesToStr([
  6555. 'for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  6556. 'for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  6557. 'for ($mod.e = 0; $mod.e <= 1; $mod.e++) $mod.e2 = $mod.e;',
  6558. 'for ($mod.e = 1; $mod.e <= 2; $mod.e++) $mod.e2 = $mod.e;',
  6559. 'for ($mod.e in rtl.createSet($mod.TEnum.Red, $mod.TEnum.Blue)) $mod.e2 = $mod.e;',
  6560. 'for (var $l in $mod.s){',
  6561. ' $mod.e = +$l;',
  6562. ' $mod.e2 = $mod.e;',
  6563. '};',
  6564. 'for ($mod.er = 1; $mod.er <= 2; $mod.er++) ;',
  6565. '']));
  6566. end;
  6567. procedure TTestModule.TestNestBegin;
  6568. begin
  6569. StartProgram(false);
  6570. Add('begin');
  6571. Add(' begin');
  6572. Add(' begin');
  6573. Add(' end;');
  6574. Add(' begin');
  6575. Add(' if true then ;');
  6576. Add(' end;');
  6577. Add(' end;');
  6578. ConvertProgram;
  6579. CheckSource('TestNestBegin',
  6580. '',
  6581. 'if (true) ;');
  6582. end;
  6583. procedure TTestModule.TestUnitImplVars;
  6584. begin
  6585. StartUnit(false);
  6586. Add('interface');
  6587. Add('implementation');
  6588. Add('var');
  6589. Add(' V1:longint;');
  6590. Add(' V2:longint = 3;');
  6591. Add(' V3:string = ''abc'';');
  6592. ConvertUnit;
  6593. CheckSource('TestUnitImplVars',
  6594. LinesToStr([ // statements
  6595. 'var $impl = $mod.$impl;',
  6596. '']),
  6597. '', // this.$init
  6598. LinesToStr([ // implementation
  6599. '$impl.V1 = 0;',
  6600. '$impl.V2 = 3;',
  6601. '$impl.V3 = "abc";',
  6602. '']) );
  6603. end;
  6604. procedure TTestModule.TestUnitImplConsts;
  6605. begin
  6606. StartUnit(false);
  6607. Add('interface');
  6608. Add('implementation');
  6609. Add('const');
  6610. Add(' v1 = 3;');
  6611. Add(' v2:longint = 4;');
  6612. Add(' v3:string = ''abc'';');
  6613. ConvertUnit;
  6614. CheckSource('TestUnitImplConsts',
  6615. LinesToStr([ // statements
  6616. 'var $impl = $mod.$impl;',
  6617. '']),
  6618. '', // this.$init
  6619. LinesToStr([ // implementation
  6620. '$impl.v1 = 3;',
  6621. '$impl.v2 = 4;',
  6622. '$impl.v3 = "abc";',
  6623. '']) );
  6624. end;
  6625. procedure TTestModule.TestUnitImplRecord;
  6626. begin
  6627. StartUnit(false);
  6628. Add('interface');
  6629. Add('implementation');
  6630. Add('type');
  6631. Add(' TMyRecord = record');
  6632. Add(' i: longint;');
  6633. Add(' end;');
  6634. Add('var aRec: TMyRecord;');
  6635. Add('initialization');
  6636. Add(' arec.i:=3;');
  6637. ConvertUnit;
  6638. CheckSource('TestUnitImplRecord',
  6639. LinesToStr([ // statements
  6640. 'var $impl = $mod.$impl;',
  6641. '']),
  6642. // this.$init
  6643. '$impl.aRec.i = 3;',
  6644. LinesToStr([ // implementation
  6645. 'rtl.recNewT($impl, "TMyRecord", function () {',
  6646. ' this.i = 0;',
  6647. ' this.$eq = function (b) {',
  6648. ' return this.i === b.i;',
  6649. ' };',
  6650. ' this.$assign = function (s) {',
  6651. ' this.i = s.i;',
  6652. ' return this;',
  6653. ' };',
  6654. '});',
  6655. '$impl.aRec = $impl.TMyRecord.$new();',
  6656. '']) );
  6657. end;
  6658. procedure TTestModule.TestRenameJSNameConflict;
  6659. begin
  6660. StartProgram(false);
  6661. Add('var apply: longint;');
  6662. Add('var bind: longint;');
  6663. Add('var call: longint;');
  6664. Add('begin');
  6665. ConvertProgram;
  6666. CheckSource('TestRenameJSNameConflict',
  6667. LinesToStr([ // statements
  6668. 'this.Apply = 0;',
  6669. 'this.Bind = 0;',
  6670. 'this.Call = 0;'
  6671. ]),
  6672. LinesToStr([ // this.$main
  6673. ''
  6674. ]));
  6675. end;
  6676. procedure TTestModule.TestLocalConst;
  6677. begin
  6678. StartProgram(false);
  6679. Add('procedure DoIt;');
  6680. Add('const');
  6681. Add(' cA: longint = 1;');
  6682. Add(' cB = 2;');
  6683. Add(' procedure Sub;');
  6684. Add(' const');
  6685. Add(' csA = 3;');
  6686. Add(' cB: double = 4;');
  6687. Add(' begin');
  6688. Add(' cb:=cb+csa;');
  6689. Add(' ca:=ca+csa+5;');
  6690. Add(' end;');
  6691. Add('begin');
  6692. Add(' ca:=ca+cb+6;');
  6693. Add('end;');
  6694. Add('begin');
  6695. ConvertProgram;
  6696. CheckSource('TestLocalConst',
  6697. LinesToStr([
  6698. 'var cA = 1;',
  6699. 'var cB = 2;',
  6700. 'var csA = 3;',
  6701. 'var cB$1 = 4;',
  6702. 'this.DoIt = function () {',
  6703. ' function Sub() {',
  6704. ' cB$1 = cB$1 + 3;',
  6705. ' cA = cA + 3 + 5;',
  6706. ' };',
  6707. ' cA = cA + 2 + 6;',
  6708. '};'
  6709. ]),
  6710. LinesToStr([
  6711. ]));
  6712. end;
  6713. procedure TTestModule.TestVarExternal;
  6714. begin
  6715. StartProgram(false);
  6716. Add('var');
  6717. Add(' NaN: double; external name ''Global.NaN'';');
  6718. Add(' d: double;');
  6719. Add('begin');
  6720. Add(' d:=NaN;');
  6721. ConvertProgram;
  6722. CheckSource('TestVarExternal',
  6723. LinesToStr([
  6724. 'this.d = 0.0;'
  6725. ]),
  6726. LinesToStr([
  6727. '$mod.d = Global.NaN;'
  6728. ]));
  6729. end;
  6730. procedure TTestModule.TestVarExternalOtherUnit;
  6731. begin
  6732. AddModuleWithIntfImplSrc('unit2.pas',
  6733. LinesToStr([
  6734. 'var NaN: double; external name ''Global.NaN'';',
  6735. 'var iV: longint;'
  6736. ]),
  6737. '');
  6738. StartUnit(true);
  6739. Add('interface');
  6740. Add('uses unit2;');
  6741. Add('implementation');
  6742. Add('var');
  6743. Add(' d: double;');
  6744. Add(' i: longint; external name ''$i'';');
  6745. Add('begin');
  6746. Add(' d:=nan;');
  6747. Add(' d:=uNit2.nan;');
  6748. Add(' d:=test1.d;');
  6749. Add(' i:=iv;');
  6750. Add(' i:=uNit2.iv;');
  6751. Add(' i:=test1.i;');
  6752. ConvertUnit;
  6753. CheckSource('TestVarExternalOtherUnit',
  6754. LinesToStr([
  6755. 'var $impl = $mod.$impl;',
  6756. '']),
  6757. LinesToStr([ // this.$init
  6758. '$impl.d = Global.NaN;',
  6759. '$impl.d = Global.NaN;',
  6760. '$impl.d = $impl.d;',
  6761. '$i = pas.unit2.iV;',
  6762. '$i = pas.unit2.iV;',
  6763. '$i = $i;',
  6764. '']),
  6765. LinesToStr([ // implementation
  6766. '$impl.d = 0.0;',
  6767. '']) );
  6768. end;
  6769. procedure TTestModule.TestVarAbsoluteFail;
  6770. begin
  6771. StartProgram(false);
  6772. Add([
  6773. 'var',
  6774. ' a: longint;',
  6775. ' b: longword absolute a;',
  6776. 'begin']);
  6777. SetExpectedPasResolverError('Invalid variable modifier "absolute"',nInvalidVariableModifier);
  6778. ConvertProgram;
  6779. end;
  6780. procedure TTestModule.TestConstExternal;
  6781. begin
  6782. StartProgram(false);
  6783. Add([
  6784. 'const',
  6785. ' PI: double; external name ''Global.PI'';',
  6786. ' Tau = 2*pi;',
  6787. 'var d: double;',
  6788. 'begin',
  6789. ' d:=pi;',
  6790. ' d:=tau+pi;']);
  6791. ConvertProgram;
  6792. CheckSource('TestConstExternal',
  6793. LinesToStr([
  6794. 'this.Tau = 2*Global.PI;',
  6795. 'this.d = 0.0;'
  6796. ]),
  6797. LinesToStr([
  6798. '$mod.d = Global.PI;',
  6799. '$mod.d = $mod.Tau + Global.PI;'
  6800. ]));
  6801. end;
  6802. procedure TTestModule.TestDouble;
  6803. begin
  6804. StartProgram(false);
  6805. Add([
  6806. 'type',
  6807. ' TDateTime = double;',
  6808. 'const',
  6809. ' a = TDateTime(2.7);',
  6810. ' b = a + TDateTime(1.7);',
  6811. ' c = 0.9 + 0.1;',
  6812. ' f0_1 = 0.1;',
  6813. ' f0_3 = 0.3;',
  6814. ' fn0_1 = -0.1;',
  6815. ' fn0_3 = -0.3;',
  6816. ' fn0_003 = -0.003;',
  6817. ' fn0_123456789 = -0.123456789;',
  6818. ' fn300_0 = -300.0;',
  6819. ' fn123456_0 = -123456.0;',
  6820. ' fn1234567_8 = -1234567.8;',
  6821. ' fn12345678_9 = -12345678.9;',
  6822. ' f1_0En12 = 1E-12;',
  6823. ' fn1_0En12 = -1E-12;',
  6824. ' maxdouble = 1.7e+308;',
  6825. ' mindouble = -1.7e+308;',
  6826. ' MinSafeIntDouble = -$1fffffffffffff;',
  6827. ' MinSafeIntDouble2 = -$20000000000000-1;',
  6828. ' MaxSafeIntDouble = $1fffffffffffff;',
  6829. ' DZeroResolution = 1E-12;',
  6830. ' Minus1 = -1E-12;',
  6831. ' EPS = 1E-9;',
  6832. ' DELTA = 0.001;',
  6833. ' Big = 129.789E+100;',
  6834. ' Test0_15 = 0.15;',
  6835. ' Test999 = 2.9999999999999;',
  6836. ' Test111999 = 211199999999999000.0;',
  6837. ' TestMinus111999 = -211199999999999000.0;',
  6838. ' Inf = 1.0 / 0.0;',
  6839. ' NegInf = -1.0 / 0.0;',
  6840. 'procedure Run(d: double); external name ''Run'';',
  6841. 'var',
  6842. ' d: double = b;',
  6843. 'begin',
  6844. ' d:=1.0;',
  6845. ' d:=1.0/3.0;',
  6846. ' d:=1.0/(3-2-1);',
  6847. ' d:=1/3;',
  6848. ' d:=5.0E-324;',
  6849. ' d:=1.7E308;',
  6850. ' d:=001.00E00;',
  6851. ' d:=002.00E001;',
  6852. ' d:=003.000E000;',
  6853. ' d:=-004.00E-00;',
  6854. ' d:=-005.00E-001;',
  6855. ' d:=10**3;',
  6856. ' d:=10 mod 3;',
  6857. ' d:=10 div 3;',
  6858. ' d:=c;',
  6859. ' d:=f0_1;',
  6860. ' d:=f0_3;',
  6861. ' d:=fn0_1;',
  6862. ' d:=fn0_3;',
  6863. ' d:=fn0_003;',
  6864. ' d:=fn0_123456789;',
  6865. ' d:=fn300_0;',
  6866. ' d:=fn123456_0;',
  6867. ' d:=fn1234567_8;',
  6868. ' d:=fn12345678_9;',
  6869. ' d:=f1_0En12;',
  6870. ' d:=fn1_0En12;',
  6871. ' d:=maxdouble;',
  6872. ' d:=mindouble;',
  6873. ' d:=MinSafeIntDouble;',
  6874. ' d:=double(MinSafeIntDouble);',
  6875. ' d:=MinSafeIntDouble2;',
  6876. ' d:=double(MinSafeIntDouble2);',
  6877. ' d:=MaxSafeIntDouble;',
  6878. ' d:=default(double);',
  6879. ' Run(Inf);',
  6880. ' Run(NegInf);',
  6881. '']);
  6882. ConvertProgram;
  6883. CheckSource('TestDouble',
  6884. LinesToStr([
  6885. 'this.a = 2.7;',
  6886. 'this.b = 2.7 + 1.7;',
  6887. 'this.c = 0.9 + 0.1;',
  6888. 'this.f0_1 = 0.1;',
  6889. 'this.f0_3 = 0.3;',
  6890. 'this.fn0_1 = -0.1;',
  6891. 'this.fn0_3 = -0.3;',
  6892. 'this.fn0_003 = -0.003;',
  6893. 'this.fn0_123456789 = -0.123456789;',
  6894. 'this.fn300_0 = -300.0;',
  6895. 'this.fn123456_0 = -123456.0;',
  6896. 'this.fn1234567_8 = -1234567.8;',
  6897. 'this.fn12345678_9 = -12345678.9;',
  6898. 'this.f1_0En12 = 1E-12;',
  6899. 'this.fn1_0En12 = -1E-12;',
  6900. 'this.maxdouble = 1.7e+308;',
  6901. 'this.mindouble = -1.7e+308;',
  6902. 'this.MinSafeIntDouble = -0x1fffffffffffff;',
  6903. 'this.MinSafeIntDouble2 = -0x20000000000000 - 1;',
  6904. 'this.MaxSafeIntDouble = 0x1fffffffffffff;',
  6905. 'this.DZeroResolution = 1E-12;',
  6906. 'this.Minus1 = -1E-12;',
  6907. 'this.EPS = 1E-9;',
  6908. 'this.DELTA = 0.001;',
  6909. 'this.Big = 129.789E+100;',
  6910. 'this.Test0_15 = 0.15;',
  6911. 'this.Test999 = 2.9999999999999;',
  6912. 'this.Test111999 = 211199999999999000.0;',
  6913. 'this.TestMinus111999 = -211199999999999000.0;',
  6914. 'this.Inf = 1.0 / 0.0;',
  6915. 'this.NegInf = -1.0 / 0.0;',
  6916. 'this.d = 4.4;',
  6917. '']),
  6918. LinesToStr([
  6919. '$mod.d = 1.0;',
  6920. '$mod.d = 1.0 / 3.0;',
  6921. '$mod.d = 1.0 / (3 - 2 - 1);',
  6922. '$mod.d = 1 / 3;',
  6923. '$mod.d = 5.0E-324;',
  6924. '$mod.d = 1.7E308;',
  6925. '$mod.d = 1.00E0;',
  6926. '$mod.d = 2.00E1;',
  6927. '$mod.d = 3.000E0;',
  6928. '$mod.d = -4.00E-0;',
  6929. '$mod.d = -5.00E-1;',
  6930. '$mod.d = Math.pow(10, 3);',
  6931. '$mod.d = 10 % 3;',
  6932. '$mod.d = rtl.trunc(10 / 3);',
  6933. '$mod.d = 1;',
  6934. '$mod.d = 0.1;',
  6935. '$mod.d = 0.3;',
  6936. '$mod.d = -0.1;',
  6937. '$mod.d = -0.3;',
  6938. '$mod.d = -0.003;',
  6939. '$mod.d = -0.123456789;',
  6940. '$mod.d = -300;',
  6941. '$mod.d = -123456;',
  6942. '$mod.d = -1234567.8;',
  6943. '$mod.d = -1.23456789E7;',
  6944. '$mod.d = 1E-12;',
  6945. '$mod.d = -1E-12;',
  6946. '$mod.d = 1.7E308;',
  6947. '$mod.d = -1.7E308;',
  6948. '$mod.d = -9007199254740991;',
  6949. '$mod.d = -9007199254740991;',
  6950. '$mod.d = -9.007199254740992E15;',
  6951. '$mod.d = -9.007199254740992E15;',
  6952. '$mod.d = 9007199254740991;',
  6953. '$mod.d = 0.0;',
  6954. 'Run(1 / 0);',
  6955. 'Run(-1 / 0);',
  6956. '']));
  6957. end;
  6958. procedure TTestModule.TestInteger;
  6959. begin
  6960. StartProgram(false);
  6961. Add([
  6962. 'const',
  6963. ' MinInt = low(NativeInt);',
  6964. ' MaxInt = high(NativeInt);',
  6965. 'type',
  6966. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  6967. 'const',
  6968. ' a = low(TMyInt)+High(TMyInt);',
  6969. 'var',
  6970. ' i: TMyInt;',
  6971. 'begin',
  6972. ' i:=-MinInt;',
  6973. ' i:=default(TMyInt);',
  6974. ' i:=low(i)+high(i);',
  6975. '']);
  6976. ConvertProgram;
  6977. CheckSource('TestIntegerRange',
  6978. LinesToStr([
  6979. 'this.MinInt = -9007199254740991;',
  6980. 'this.MaxInt = 9007199254740991;',
  6981. 'this.a = -9007199254740991 + 9007199254740991;',
  6982. 'this.i = 0;',
  6983. '']),
  6984. LinesToStr([
  6985. '$mod.i = - -9007199254740991;',
  6986. '$mod.i = -9007199254740991;',
  6987. '$mod.i = -9007199254740991 + 9007199254740991;',
  6988. '']));
  6989. end;
  6990. procedure TTestModule.TestIntegerRange;
  6991. begin
  6992. StartProgram(false);
  6993. Add([
  6994. 'const',
  6995. ' MinInt = -1;',
  6996. ' MaxInt = +1;',
  6997. 'type',
  6998. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  6999. ' TInt2 = 1..3;',
  7000. 'const',
  7001. ' a = low(TMyInt)+High(TMyInt);',
  7002. ' b = low(TInt2)+High(TInt2);',
  7003. ' s1 = [1];',
  7004. ' s2 = [1,2];',
  7005. ' s3 = [1..3];',
  7006. ' s4 = [low(shortint)..high(shortint)];',
  7007. ' s5 = [succ(low(shortint))..pred(high(shortint))];',
  7008. ' s6 = 1 in s2;',
  7009. 'var',
  7010. ' i: TMyInt;',
  7011. ' i2: TInt2;',
  7012. 'begin',
  7013. ' i:=i2;',
  7014. ' i:=default(TMyInt);',
  7015. ' if i=i2 then ;',
  7016. ' i:=ord(i2);',
  7017. '']);
  7018. ConvertProgram;
  7019. CheckSource('TestIntegerRange',
  7020. LinesToStr([
  7021. 'this.MinInt = -1;',
  7022. 'this.MaxInt = +1;',
  7023. 'this.a = -1 + 1;',
  7024. 'this.b = 1 + 3;',
  7025. 'this.s1 = rtl.createSet(1);',
  7026. 'this.s2 = rtl.createSet(1, 2);',
  7027. 'this.s3 = rtl.createSet(null, 1, 3);',
  7028. 'this.s4 = rtl.createSet(null, -128, 127);',
  7029. 'this.s5 = rtl.createSet(null, -128 + 1, 127 - 1);',
  7030. 'this.s6 = 1 in this.s2;',
  7031. 'this.i = 0;',
  7032. 'this.i2 = 0;',
  7033. '']),
  7034. LinesToStr([
  7035. '$mod.i = $mod.i2;',
  7036. '$mod.i = -1;',
  7037. 'if ($mod.i === $mod.i2) ;',
  7038. '$mod.i = $mod.i2;',
  7039. '']));
  7040. end;
  7041. procedure TTestModule.TestIntegerTypecasts;
  7042. begin
  7043. StartProgram(false);
  7044. Add([
  7045. 'var',
  7046. ' i: nativeint;',
  7047. ' b: byte;',
  7048. ' sh: shortint;',
  7049. ' w: word;',
  7050. ' sm: smallint;',
  7051. ' lw: longword;',
  7052. ' li: longint;',
  7053. 'begin',
  7054. ' b:=byte(i);',
  7055. ' sh:=shortint(i);',
  7056. ' w:=word(i);',
  7057. ' sm:=smallint(i);',
  7058. ' lw:=longword(i);',
  7059. ' li:=longint(i);',
  7060. '']);
  7061. ConvertProgram;
  7062. CheckSource('TestIntegerTypecasts',
  7063. LinesToStr([
  7064. 'this.i = 0;',
  7065. 'this.b = 0;',
  7066. 'this.sh = 0;',
  7067. 'this.w = 0;',
  7068. 'this.sm = 0;',
  7069. 'this.lw = 0;',
  7070. 'this.li = 0;',
  7071. '']),
  7072. LinesToStr([
  7073. '$mod.b = $mod.i & 255;',
  7074. '$mod.sh = (($mod.i & 255) << 24) >> 24;',
  7075. '$mod.w = $mod.i & 65535;',
  7076. '$mod.sm = (($mod.i & 65535) << 16) >> 16;',
  7077. '$mod.lw = $mod.i >>> 0;',
  7078. '$mod.li = $mod.i & 0xFFFFFFFF;',
  7079. '']));
  7080. end;
  7081. procedure TTestModule.TestInteger_BitwiseShrNativeInt;
  7082. begin
  7083. StartProgram(false);
  7084. Add([
  7085. 'var',
  7086. ' i,j: nativeint;',
  7087. 'begin',
  7088. ' i:=i shr 0;',
  7089. ' i:=i shr 1;',
  7090. ' i:=i shr 3;',
  7091. ' i:=i shr 54;',
  7092. ' i:=j shr i;',
  7093. '']);
  7094. ConvertProgram;
  7095. CheckResolverUnexpectedHints;
  7096. CheckSource('TestInteger_BitwiseShrNativeInt',
  7097. LinesToStr([
  7098. 'this.i = 0;',
  7099. 'this.j = 0;',
  7100. '']),
  7101. LinesToStr([
  7102. '$mod.i = $mod.i;',
  7103. '$mod.i = Math.floor($mod.i / 2);',
  7104. '$mod.i = Math.floor($mod.i / 8);',
  7105. '$mod.i = 0;',
  7106. '$mod.i = rtl.shr($mod.j, $mod.i);',
  7107. '']));
  7108. end;
  7109. procedure TTestModule.TestInteger_BitwiseShlNativeInt;
  7110. begin
  7111. StartProgram(false);
  7112. Add([
  7113. 'var',
  7114. ' i: nativeint;',
  7115. 'begin',
  7116. ' i:=i shl 0;',
  7117. ' i:=i shl 54;',
  7118. ' i:=123456789012 shl 1;',
  7119. ' i:=i shl 1;',
  7120. '']);
  7121. ConvertProgram;
  7122. CheckResolverUnexpectedHints;
  7123. CheckSource('TestInteger_BitwiseShrNativeInt',
  7124. LinesToStr([
  7125. 'this.i = 0;',
  7126. '']),
  7127. LinesToStr([
  7128. '$mod.i = $mod.i;',
  7129. '$mod.i = 0;',
  7130. '$mod.i = 246913578024;',
  7131. '$mod.i = rtl.shl($mod.i, 1);',
  7132. '']));
  7133. end;
  7134. procedure TTestModule.TestInteger_SystemFunc;
  7135. begin
  7136. StartProgram(true);
  7137. Add([
  7138. 'var',
  7139. ' i: byte;',
  7140. ' s: string;',
  7141. 'begin',
  7142. ' system.inc(i);',
  7143. ' system.str(i,s);',
  7144. ' s:=system.str(i);',
  7145. ' i:=system.low(i);',
  7146. ' i:=system.high(i);',
  7147. ' i:=system.pred(i);',
  7148. ' i:=system.succ(i);',
  7149. ' i:=system.ord(i);',
  7150. '']);
  7151. ConvertProgram;
  7152. CheckResolverUnexpectedHints;
  7153. CheckSource('TestInteger_SystemFunc',
  7154. LinesToStr([
  7155. 'this.i = 0;',
  7156. 'this.s = "";',
  7157. '']),
  7158. LinesToStr([
  7159. '$mod.i += 1;',
  7160. '$mod.s = "" + $mod.i;',
  7161. '$mod.s = "" + $mod.i;',
  7162. '$mod.i = 0;',
  7163. '$mod.i = 255;',
  7164. '$mod.i = $mod.i - 1;',
  7165. '$mod.i = $mod.i + 1;',
  7166. '$mod.i = $mod.i;',
  7167. '']));
  7168. end;
  7169. procedure TTestModule.TestCurrency;
  7170. begin
  7171. StartProgram(false);
  7172. Add([
  7173. 'type',
  7174. ' TCoin = currency;',
  7175. 'const',
  7176. ' a = TCoin(2.7);',
  7177. ' b = a + TCoin(1.7);',
  7178. ' MinSafeIntCurrency: TCoin = -92233720368.5477;',
  7179. ' MaxSafeIntCurrency: TCoin = 92233720368.5477;',
  7180. 'var',
  7181. ' c: TCoin = b;',
  7182. ' i: nativeint;',
  7183. ' d: double;',
  7184. ' j: jsvalue;',
  7185. 'function DoIt(c: currency): currency; begin end;',
  7186. 'function GetIt(d: double): double; begin end;',
  7187. 'procedure Write(v: jsvalue); begin end;',
  7188. 'begin',
  7189. ' c:=1.0;',
  7190. ' c:=0.1;',
  7191. ' c:=1.0/3.0;',
  7192. ' c:=1/3;',
  7193. ' c:=a;',
  7194. ' d:=c;',
  7195. ' c:=d;',
  7196. ' c:=currency(c);',
  7197. ' c:=currency(d);',
  7198. ' d:=double(c);',
  7199. ' c:=i;',
  7200. ' c:=currency(i);',
  7201. //' i:=c;', not allowed
  7202. ' i:=nativeint(c);',
  7203. ' c:=c+a;',
  7204. ' c:=-c-a;',
  7205. ' c:=d+c;',
  7206. ' c:=c+d;',
  7207. ' c:=d-c;',
  7208. ' c:=c-d;',
  7209. ' c:=c*a;',
  7210. ' c:=a*c;',
  7211. ' c:=d*c;',
  7212. ' c:=c*d;',
  7213. ' c:=c/a;',
  7214. ' c:=a/c;',
  7215. ' c:=d/c;',
  7216. ' c:=c/d;',
  7217. ' c:=c**a;',
  7218. ' c:=a**c;',
  7219. ' c:=d**c;',
  7220. ' c:=c**d;',
  7221. ' if c=c then ;',
  7222. ' if c=a then ;',
  7223. ' if a=c then ;',
  7224. ' if d=c then ;',
  7225. ' if c=d then ;',
  7226. ' c:=DoIt(c);',
  7227. ' c:=DoIt(i);',
  7228. ' c:=DoIt(d);',
  7229. ' c:=GetIt(c);',
  7230. ' j:=c;',
  7231. ' Write(c);',
  7232. ' c:=default(currency);',
  7233. ' j:=str(c);',
  7234. ' j:=str(c:0:3);',
  7235. '']);
  7236. ConvertProgram;
  7237. CheckSource('TestCurrency',
  7238. LinesToStr([
  7239. 'this.a = 27000;',
  7240. 'this.b = this.a + 17000;',
  7241. 'this.MinSafeIntCurrency = -92233720368.5477;',
  7242. 'this.MaxSafeIntCurrency = 92233720368.5477;',
  7243. 'this.c = this.b;',
  7244. 'this.i = 0;',
  7245. 'this.d = 0.0;',
  7246. 'this.j = undefined;',
  7247. 'this.DoIt = function (c) {',
  7248. ' var Result = 0;',
  7249. ' return Result;',
  7250. '};',
  7251. 'this.GetIt = function (d) {',
  7252. ' var Result = 0.0;',
  7253. ' return Result;',
  7254. '};',
  7255. 'this.Write = function (v) {',
  7256. '};',
  7257. '']),
  7258. LinesToStr([
  7259. '$mod.c = 10000;',
  7260. '$mod.c = 1000;',
  7261. '$mod.c = rtl.trunc((1.0 / 3.0) * 10000);',
  7262. '$mod.c = rtl.trunc((1 / 3) * 10000);',
  7263. '$mod.c = $mod.a;',
  7264. '$mod.d = $mod.c / 10000;',
  7265. '$mod.c = rtl.trunc($mod.d * 10000);',
  7266. '$mod.c = $mod.c;',
  7267. '$mod.c = $mod.d * 10000;',
  7268. '$mod.d = $mod.c / 10000;',
  7269. '$mod.c = $mod.i * 10000;',
  7270. '$mod.c = $mod.i * 10000;',
  7271. '$mod.i = rtl.trunc($mod.c / 10000);',
  7272. '$mod.c = $mod.c + $mod.a;',
  7273. '$mod.c = -$mod.c - $mod.a;',
  7274. '$mod.c = ($mod.d * 10000) + $mod.c;',
  7275. '$mod.c = $mod.c + ($mod.d * 10000);',
  7276. '$mod.c = ($mod.d * 10000) - $mod.c;',
  7277. '$mod.c = $mod.c - ($mod.d * 10000);',
  7278. '$mod.c = ($mod.c * $mod.a) / 10000;',
  7279. '$mod.c = ($mod.a * $mod.c) / 10000;',
  7280. '$mod.c = $mod.d * $mod.c;',
  7281. '$mod.c = $mod.c * $mod.d;',
  7282. '$mod.c = rtl.trunc(($mod.c / $mod.a) * 10000);',
  7283. '$mod.c = rtl.trunc(($mod.a / $mod.c) * 10000);',
  7284. '$mod.c = rtl.trunc($mod.d / $mod.c);',
  7285. '$mod.c = rtl.trunc($mod.c / $mod.d);',
  7286. '$mod.c = rtl.trunc(Math.pow($mod.c / 10000, $mod.a / 10000) * 10000);',
  7287. '$mod.c = rtl.trunc(Math.pow($mod.a / 10000, $mod.c / 10000) * 10000);',
  7288. '$mod.c = rtl.trunc(Math.pow($mod.d, $mod.c / 10000) * 10000);',
  7289. '$mod.c = rtl.trunc(Math.pow($mod.c / 10000, $mod.d) * 10000);',
  7290. 'if ($mod.c === $mod.c) ;',
  7291. 'if ($mod.c === $mod.a) ;',
  7292. 'if ($mod.a === $mod.c) ;',
  7293. 'if (($mod.d * 10000) === $mod.c) ;',
  7294. 'if ($mod.c === ($mod.d * 10000)) ;',
  7295. '$mod.c = $mod.DoIt($mod.c);',
  7296. '$mod.c = $mod.DoIt($mod.i * 10000);',
  7297. '$mod.c = $mod.DoIt($mod.d * 10000);',
  7298. '$mod.c = rtl.trunc($mod.GetIt($mod.c / 10000) * 10000);',
  7299. '$mod.j = $mod.c / 10000;',
  7300. '$mod.Write($mod.c / 10000);',
  7301. '$mod.c = 0;',
  7302. '$mod.j = rtl.floatToStr($mod.c / 10000);',
  7303. '$mod.j = rtl.floatToStr($mod.c / 10000, 0, 3);',
  7304. '']));
  7305. end;
  7306. procedure TTestModule.TestForBoolDo;
  7307. begin
  7308. StartProgram(false);
  7309. Add([
  7310. 'var b: boolean;',
  7311. 'begin',
  7312. ' for b:=false to true do ;',
  7313. ' for b:=b downto false do ;',
  7314. ' for b in boolean do ;',
  7315. '']);
  7316. ConvertProgram;
  7317. CheckSource('TestForBoolDo',
  7318. LinesToStr([ // statements
  7319. 'this.b = false;']),
  7320. LinesToStr([ // this.$main
  7321. 'for (var $l = 0; $l <= 1; $l++) $mod.b = $l !== 0;',
  7322. 'for (var $l1 = +$mod.b; $l1 >= 0; $l1--) $mod.b = $l1 !== 0;',
  7323. 'for (var $l2 = 0; $l2 <= 1; $l2++) $mod.b = $l2 !== 0;',
  7324. '']));
  7325. end;
  7326. procedure TTestModule.TestForIntDo;
  7327. begin
  7328. StartProgram(false);
  7329. Add([
  7330. 'var i: longint;',
  7331. 'begin',
  7332. ' for i:=3 to 5 do ;',
  7333. ' for i:=i downto 2 do ;',
  7334. ' for i in byte do ;',
  7335. '']);
  7336. ConvertProgram;
  7337. CheckSource('TestForIntDo',
  7338. LinesToStr([ // statements
  7339. 'this.i = 0;']),
  7340. LinesToStr([ // this.$main
  7341. 'for ($mod.i = 3; $mod.i <= 5; $mod.i++) ;',
  7342. 'for (var $l = $mod.i; $l >= 2; $l--) $mod.i = $l;',
  7343. 'for (var $l1 = 0; $l1 <= 255; $l1++) $mod.i = $l1;',
  7344. '']));
  7345. end;
  7346. procedure TTestModule.TestForIntInDo;
  7347. begin
  7348. StartProgram(false);
  7349. Add([
  7350. 'type',
  7351. ' TSetOfInt = set of byte;',
  7352. ' TIntRg = 3..7;',
  7353. ' TSetOfIntRg = set of TIntRg;',
  7354. 'var',
  7355. ' i,i2: longint;',
  7356. ' a1: array of byte;',
  7357. ' a2: array[1..3] of byte;',
  7358. ' soi: TSetOfInt;',
  7359. ' soir: TSetOfIntRg;',
  7360. ' ir: TIntRg;',
  7361. 'begin',
  7362. ' for i in byte do ;',
  7363. ' for i in a1 do ;',
  7364. ' for i in a2 do ;',
  7365. ' for i in [11..13] do ;',
  7366. ' for i in TSetOfInt do ;',
  7367. ' for i in TIntRg do ;',
  7368. ' for i in soi do i2:=i;',
  7369. ' for i in TSetOfIntRg do ;',
  7370. ' for i in soir do ;',
  7371. ' for ir in TIntRg do ;',
  7372. ' for ir in TSetOfIntRg do ;',
  7373. ' for ir in soir do ;',
  7374. '']);
  7375. ConvertProgram;
  7376. CheckSource('TestForIntInDo',
  7377. LinesToStr([ // statements
  7378. 'this.i = 0;',
  7379. 'this.i2 = 0;',
  7380. 'this.a1 = [];',
  7381. 'this.a2 = rtl.arraySetLength(null, 0, 3);',
  7382. 'this.soi = {};',
  7383. 'this.soir = {};',
  7384. 'this.ir = 0;',
  7385. '']),
  7386. LinesToStr([ // this.$main
  7387. 'for (var $l = 0; $l <= 255; $l++) $mod.i = $l;',
  7388. 'for (var $in = $mod.a1, $l1 = 0, $end = rtl.length($in) - 1; $l1 <= $end; $l1++) $mod.i = $in[$l1];',
  7389. 'for (var $in1 = $mod.a2, $l2 = 0, $end1 = rtl.length($in1) - 1; $l2 <= $end1; $l2++) $mod.i = $in1[$l2];',
  7390. 'for (var $l3 = 11; $l3 <= 13; $l3++) $mod.i = $l3;',
  7391. 'for (var $l4 = 0; $l4 <= 255; $l4++) $mod.i = $l4;',
  7392. 'for (var $l5 = 3; $l5 <= 7; $l5++) $mod.i = $l5;',
  7393. 'for (var $l6 in $mod.soi) {',
  7394. ' $mod.i = +$l6;',
  7395. ' $mod.i2 = $mod.i;',
  7396. '};',
  7397. 'for (var $l7 = 3; $l7 <= 7; $l7++) $mod.i = $l7;',
  7398. 'for (var $l8 in $mod.soir) $mod.i = +$l8;',
  7399. 'for (var $l9 = 3; $l9 <= 7; $l9++) $mod.ir = $l9;',
  7400. 'for (var $l10 = 3; $l10 <= 7; $l10++) $mod.ir = $l10;',
  7401. 'for (var $l11 in $mod.soir) $mod.ir = +$l11;',
  7402. '']));
  7403. end;
  7404. procedure TTestModule.TestCharConst;
  7405. begin
  7406. StartProgram(false);
  7407. Add([
  7408. 'const',
  7409. ' a = #$00F3;',
  7410. ' c: char = ''1'';',
  7411. ' wc: widechar = ''ä'';',
  7412. 'begin',
  7413. ' c:=#0;',
  7414. ' c:=#1;',
  7415. ' c:=#9;',
  7416. ' c:=#10;',
  7417. ' c:=#13;',
  7418. ' c:=#31;',
  7419. ' c:=#32;',
  7420. ' c:=#$A;',
  7421. ' c:=#$0A;',
  7422. ' c:=#$b;',
  7423. ' c:=#$0b;',
  7424. ' c:=^A;',
  7425. ' c:=''"'';',
  7426. ' c:=default(char);',
  7427. ' c:=#$00E4;', // ä
  7428. ' c:=''ä'';',
  7429. ' c:=#$E4;', // ä
  7430. ' c:=#$D800;', // invalid UTF-16
  7431. ' c:=#$DFFF;', // invalid UTF-16
  7432. ' c:=#$FFFF;', // last UCS-2
  7433. ' c:=high(c);', // last UCS-2
  7434. ' c:=#269;',
  7435. '']);
  7436. ConvertProgram;
  7437. CheckSource('TestCharConst',
  7438. LinesToStr([
  7439. 'this.a="ó";',
  7440. 'this.c="1";',
  7441. 'this.wc="ä";'
  7442. ]),
  7443. LinesToStr([
  7444. '$mod.c="\x00";',
  7445. '$mod.c="\x01";',
  7446. '$mod.c="\t";',
  7447. '$mod.c="\n";',
  7448. '$mod.c="\r";',
  7449. '$mod.c="\x1F";',
  7450. '$mod.c=" ";',
  7451. '$mod.c="\n";',
  7452. '$mod.c="\n";',
  7453. '$mod.c="\x0B";',
  7454. '$mod.c="\x0B";',
  7455. '$mod.c="\x01";',
  7456. '$mod.c=''"'';',
  7457. '$mod.c="\x00";',
  7458. '$mod.c = "ä";',
  7459. '$mod.c = "ä";',
  7460. '$mod.c = "ä";',
  7461. '$mod.c="\uD800";',
  7462. '$mod.c="\uDFFF";',
  7463. '$mod.c="\uFFFF";',
  7464. '$mod.c="\uFFFF";',
  7465. '$mod.c = "č";',
  7466. '']));
  7467. end;
  7468. procedure TTestModule.TestChar_Compare;
  7469. begin
  7470. StartProgram(false);
  7471. Add('var');
  7472. Add(' c: char;');
  7473. Add(' b: boolean;');
  7474. Add('begin');
  7475. Add(' b:=c=''1'';');
  7476. Add(' b:=''2''=c;');
  7477. Add(' b:=''3''=''4'';');
  7478. Add(' b:=c<>''5'';');
  7479. Add(' b:=''6''<>c;');
  7480. Add(' b:=c>''7'';');
  7481. Add(' b:=''8''>c;');
  7482. Add(' b:=c>=''9'';');
  7483. Add(' b:=''A''>=c;');
  7484. Add(' b:=c<''B'';');
  7485. Add(' b:=''C''<c;');
  7486. Add(' b:=c<=''D'';');
  7487. Add(' b:=''E''<=c;');
  7488. ConvertProgram;
  7489. CheckSource('TestChar_Compare',
  7490. LinesToStr([
  7491. 'this.c="";',
  7492. 'this.b = false;'
  7493. ]),
  7494. LinesToStr([
  7495. '$mod.b = $mod.c === "1";',
  7496. '$mod.b = "2" === $mod.c;',
  7497. '$mod.b = "3" === "4";',
  7498. '$mod.b = $mod.c !== "5";',
  7499. '$mod.b = "6" !== $mod.c;',
  7500. '$mod.b = $mod.c > "7";',
  7501. '$mod.b = "8" > $mod.c;',
  7502. '$mod.b = $mod.c >= "9";',
  7503. '$mod.b = "A" >= $mod.c;',
  7504. '$mod.b = $mod.c < "B";',
  7505. '$mod.b = "C" < $mod.c;',
  7506. '$mod.b = $mod.c <= "D";',
  7507. '$mod.b = "E" <= $mod.c;',
  7508. '']));
  7509. end;
  7510. procedure TTestModule.TestChar_BuiltInProcs;
  7511. begin
  7512. StartProgram(false);
  7513. Add([
  7514. 'var',
  7515. ' c: char;',
  7516. ' i: longint;',
  7517. ' s: string;',
  7518. 'begin',
  7519. ' i:=ord(c);',
  7520. ' i:=ord(s[i]);',
  7521. ' c:=chr(i);',
  7522. ' c:=pred(c);',
  7523. ' c:=succ(c);',
  7524. ' c:=low(c);',
  7525. ' c:=high(c);',
  7526. ' i:=byte(c);',
  7527. ' i:=word(c);',
  7528. ' i:=longint(c);',
  7529. '']);
  7530. ConvertProgram;
  7531. CheckSource('TestChar_BuiltInProcs',
  7532. LinesToStr([
  7533. 'this.c = "";',
  7534. 'this.i = 0;',
  7535. 'this.s = "";'
  7536. ]),
  7537. LinesToStr([
  7538. '$mod.i = $mod.c.charCodeAt();',
  7539. '$mod.i = $mod.s.charCodeAt($mod.i-1);',
  7540. '$mod.c = String.fromCharCode($mod.i);',
  7541. '$mod.c = String.fromCharCode($mod.c.charCodeAt() - 1);',
  7542. '$mod.c = String.fromCharCode($mod.c.charCodeAt() + 1);',
  7543. '$mod.c = "\x00";',
  7544. '$mod.c = "\uFFFF";',
  7545. '$mod.i = $mod.c.charCodeAt() & 255;',
  7546. '$mod.i = $mod.c.charCodeAt();',
  7547. '$mod.i = $mod.c.charCodeAt() & 0xFFFFFFFF;',
  7548. '']));
  7549. end;
  7550. procedure TTestModule.TestStringConst;
  7551. begin
  7552. StartProgram(false);
  7553. Add([
  7554. '{$H+}',
  7555. 'const',
  7556. ' a = #$00F3#$017C;', // first <256, then >=256
  7557. ' b = string(''a'');',
  7558. ' c = string(''ä'');',
  7559. ' d = UnicodeString(''b'');',
  7560. ' e = UnicodeString(''ö'');',
  7561. ' f = low(a)+high(b);',
  7562. ' g: word = low(a);',
  7563. 'var',
  7564. ' s: string = ''abc'';',
  7565. ' i: longint;',
  7566. 'begin',
  7567. ' s:='''';',
  7568. ' s:=#13#10;',
  7569. ' s:=#9''foo'';',
  7570. ' s:=#$A9;',
  7571. ' s:=''foo''#13''bar'';',
  7572. ' s:=''"'';',
  7573. ' s:=''"''''"'';',
  7574. ' s:=#$20AC;', // euro
  7575. ' s:=#$10437;', // outside BMP
  7576. ' s:=''abc''#$20AC;', // ascii,#
  7577. ' s:=''ä''#$20AC;', // non ascii,#
  7578. ' s:=#$20AC''abc'';', // #, ascii
  7579. ' s:=#$20AC''ä'';', // #, non ascii
  7580. ' s:=default(string);',
  7581. ' s:=concat(s);',
  7582. ' s:=concat(s,''a'',s);',
  7583. ' s:=#250#269;',
  7584. ' i:=low(s)+high(a);',
  7585. //' s:=#$2F804;',
  7586. // ToDo: \uD87E\uDC04 -> \u{2F804}
  7587. '']);
  7588. ConvertProgram;
  7589. CheckSource('TestStringConst',
  7590. LinesToStr([
  7591. 'this.a = "óż";',
  7592. 'this.b = "a";',
  7593. 'this.c = "ä";',
  7594. 'this.d = "b";',
  7595. 'this.e = "ö";',
  7596. 'this.f = 1 + this.b.length;',
  7597. 'this.g = 1;',
  7598. 'this.s="abc";',
  7599. 'this.i = 0;',
  7600. '']),
  7601. LinesToStr([
  7602. '$mod.s="";',
  7603. '$mod.s="\r\n";',
  7604. '$mod.s="\tfoo";',
  7605. '$mod.s="©";',
  7606. '$mod.s="foo\rbar";',
  7607. '$mod.s=''"'';',
  7608. '$mod.s=''"\''"'';',
  7609. '$mod.s="€";',
  7610. '$mod.s="'#$F0#$90#$90#$B7'";',
  7611. '$mod.s = "abc€";',
  7612. '$mod.s = "ä€";',
  7613. '$mod.s = "€abc";',
  7614. '$mod.s = "ۊ";',
  7615. '$mod.s="";',
  7616. '$mod.s = $mod.s;',
  7617. '$mod.s = $mod.s.concat("a", $mod.s);',
  7618. '$mod.s = "úč";',
  7619. '$mod.i = 1 + $mod.a.length;',
  7620. '']));
  7621. end;
  7622. procedure TTestModule.TestStringConst_InvalidUTF16;
  7623. begin
  7624. StartProgram(false);
  7625. Add([
  7626. 'const',
  7627. ' a: char = #$D87E;',
  7628. ' b: string = #$D87E;',
  7629. ' c: string = #$D87E#43;',
  7630. 'begin',
  7631. ' c:=''abc''#$D87E;',
  7632. ' c:=#0#1#2;',
  7633. ' c:=#127;',
  7634. ' c:=#128;',
  7635. ' c:=#255;',
  7636. ' c:=#256;',
  7637. '']);
  7638. ConvertProgram;
  7639. CheckSource('TestStringConst',
  7640. LinesToStr([
  7641. 'this.a = "\uD87E";',
  7642. 'this.b = "\uD87E";',
  7643. 'this.c = "\uD87E+";',
  7644. '']),
  7645. LinesToStr([
  7646. '$mod.c = "abc\uD87E";',
  7647. '$mod.c = "\x00\x01\x02";',
  7648. '$mod.c = "'#127'";',
  7649. '$mod.c = "'#$c2#$80'";',
  7650. '$mod.c = "'#$c3#$BF'";',
  7651. '$mod.c = "'#$c4#$80'";',
  7652. '']));
  7653. end;
  7654. procedure TTestModule.TestStringConstSurrogate;
  7655. begin
  7656. StartProgram(false);
  7657. Add([
  7658. 'var',
  7659. ' s: string;',
  7660. 'begin',
  7661. ' s:=''😊'';', // 1F60A
  7662. '']);
  7663. ConvertProgram;
  7664. CheckSource('TestStringConstSurrogate',
  7665. LinesToStr([
  7666. 'this.s="";'
  7667. ]),
  7668. LinesToStr([
  7669. '$mod.s="😊";'
  7670. ]));
  7671. end;
  7672. procedure TTestModule.TestString_Length;
  7673. begin
  7674. StartProgram(false);
  7675. Add('const c = ''foo'';');
  7676. Add('var');
  7677. Add(' s: string;');
  7678. Add(' i: longint;');
  7679. Add('begin');
  7680. Add(' i:=length(s);');
  7681. Add(' i:=length(s+s);');
  7682. Add(' i:=length(''abc'');');
  7683. Add(' i:=length(c);');
  7684. ConvertProgram;
  7685. CheckSource('TestString_Length',
  7686. LinesToStr([
  7687. 'this.c = "foo";',
  7688. 'this.s = "";',
  7689. 'this.i = 0;',
  7690. '']),
  7691. LinesToStr([
  7692. '$mod.i = $mod.s.length;',
  7693. '$mod.i = ($mod.s+$mod.s).length;',
  7694. '$mod.i = "abc".length;',
  7695. '$mod.i = $mod.c.length;',
  7696. '']));
  7697. end;
  7698. procedure TTestModule.TestString_Compare;
  7699. begin
  7700. StartProgram(false);
  7701. Add('var');
  7702. Add(' s, t: string;');
  7703. Add(' b: boolean;');
  7704. Add('begin');
  7705. Add(' b:=s=t;');
  7706. Add(' b:=s<>t;');
  7707. Add(' b:=s>t;');
  7708. Add(' b:=s>=t;');
  7709. Add(' b:=s<t;');
  7710. Add(' b:=s<=t;');
  7711. ConvertProgram;
  7712. CheckSource('TestString_Compare',
  7713. LinesToStr([ // statements
  7714. 'this.s = "";',
  7715. 'this.t = "";',
  7716. 'this.b =false;'
  7717. ]),
  7718. LinesToStr([ // this.$main
  7719. '$mod.b = $mod.s === $mod.t;',
  7720. '$mod.b = $mod.s !== $mod.t;',
  7721. '$mod.b = $mod.s > $mod.t;',
  7722. '$mod.b = $mod.s >= $mod.t;',
  7723. '$mod.b = $mod.s < $mod.t;',
  7724. '$mod.b = $mod.s <= $mod.t;',
  7725. '']));
  7726. end;
  7727. procedure TTestModule.TestString_SetLength;
  7728. begin
  7729. StartProgram(false);
  7730. Add([
  7731. 'procedure DoIt(var s: string);',
  7732. 'begin',
  7733. ' SetLength(s,2);',
  7734. 'end;',
  7735. 'var s: string;',
  7736. 'begin',
  7737. ' SetLength(s,3);',
  7738. '']);
  7739. ConvertProgram;
  7740. CheckSource('TestString_SetLength',
  7741. LinesToStr([ // statements
  7742. 'this.DoIt = function (s) {',
  7743. ' s.set(rtl.strSetLength(s.get(), 2));',
  7744. '};',
  7745. 'this.s = "";',
  7746. '']),
  7747. LinesToStr([ // this.$main
  7748. '$mod.s = rtl.strSetLength($mod.s, 3);'
  7749. ]));
  7750. end;
  7751. procedure TTestModule.TestString_CharAt;
  7752. begin
  7753. StartProgram(false);
  7754. Add([
  7755. 'var',
  7756. ' s: string;',
  7757. ' c: char;',
  7758. ' b: boolean;',
  7759. 'begin',
  7760. ' b:= s[1] = c;',
  7761. ' b:= c = s[1];',
  7762. ' b:= c <> s[1];',
  7763. ' b:= c > s[1];',
  7764. ' b:= c >= s[1];',
  7765. ' b:= c < s[2];',
  7766. ' b:= c <= s[1];',
  7767. ' s[1] := c;',
  7768. ' s[2+3] := c;']);
  7769. ConvertProgram;
  7770. CheckSource('TestString_CharAt',
  7771. LinesToStr([ // statements
  7772. 'this.s = "";',
  7773. 'this.c = "";',
  7774. 'this.b = false;'
  7775. ]),
  7776. LinesToStr([ // this.$main
  7777. '$mod.b = $mod.s.charAt(0) === $mod.c;',
  7778. '$mod.b = $mod.c === $mod.s.charAt(0);',
  7779. '$mod.b = $mod.c !== $mod.s.charAt(0);',
  7780. '$mod.b = $mod.c > $mod.s.charAt(0);',
  7781. '$mod.b = $mod.c >= $mod.s.charAt(0);',
  7782. '$mod.b = $mod.c < $mod.s.charAt(1);',
  7783. '$mod.b = $mod.c <= $mod.s.charAt(0);',
  7784. '$mod.s = rtl.setCharAt($mod.s, 0, $mod.c);',
  7785. '$mod.s = rtl.setCharAt($mod.s, (2 + 3) - 1, $mod.c);',
  7786. '']));
  7787. end;
  7788. procedure TTestModule.TestStringHMinusFail;
  7789. begin
  7790. StartProgram(false);
  7791. Add([
  7792. '{$H-}',
  7793. 'var s: string;',
  7794. 'begin']);
  7795. ConvertProgram;
  7796. CheckHint(mtWarning,nWarnIllegalCompilerDirectiveX,'Warning: test1.pp(3,6) : Illegal compiler directive "H-"');
  7797. end;
  7798. procedure TTestModule.TestStr;
  7799. begin
  7800. StartProgram(false);
  7801. Add('var');
  7802. Add(' b: boolean;');
  7803. Add(' i: longint;');
  7804. Add(' d: double;');
  7805. Add(' s: string;');
  7806. Add('begin');
  7807. Add(' str(b,s);');
  7808. Add(' str(i,s);');
  7809. Add(' str(d,s);');
  7810. Add(' str(i:3,s);');
  7811. Add(' str(d:3:2,s);');
  7812. Add(' Str(12.456:12:1,s);');
  7813. Add(' Str(12.456:12,s);');
  7814. Add(' s:=str(b);');
  7815. Add(' s:=str(i);');
  7816. Add(' s:=str(d);');
  7817. Add(' s:=str(i,i);');
  7818. Add(' s:=str(i:3);');
  7819. Add(' s:=str(d:3:2);');
  7820. Add(' s:=str(i:4,i);');
  7821. Add(' s:=str(i,i:5);');
  7822. Add(' s:=str(i:4,i:5);');
  7823. Add(' s:=str(s,s);');
  7824. Add(' s:=str(s,''foo'');');
  7825. ConvertProgram;
  7826. CheckSource('TestStr',
  7827. LinesToStr([ // statements
  7828. 'this.b = false;',
  7829. 'this.i = 0;',
  7830. 'this.d = 0.0;',
  7831. 'this.s = "";',
  7832. '']),
  7833. LinesToStr([ // this.$main
  7834. '$mod.s = ""+$mod.b;',
  7835. '$mod.s = ""+$mod.i;',
  7836. '$mod.s = rtl.floatToStr($mod.d);',
  7837. '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
  7838. '$mod.s = rtl.floatToStr($mod.d,3,2);',
  7839. '$mod.s = rtl.floatToStr(12.456,12,1);',
  7840. '$mod.s = rtl.floatToStr(12.456,12);',
  7841. '$mod.s = ""+$mod.b;',
  7842. '$mod.s = ""+$mod.i;',
  7843. '$mod.s = rtl.floatToStr($mod.d);',
  7844. '$mod.s = ""+$mod.i+$mod.i;',
  7845. '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
  7846. '$mod.s = rtl.floatToStr($mod.d,3,2);',
  7847. '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + $mod.i;',
  7848. '$mod.s = "" + $mod.i + rtl.spaceLeft("" + $mod.i, 5);',
  7849. '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + rtl.spaceLeft("" + $mod.i, 5);',
  7850. '$mod.s = $mod.s + $mod.s;',
  7851. '$mod.s = $mod.s + "foo";',
  7852. '']));
  7853. end;
  7854. procedure TTestModule.TestBaseType_AnsiStringFail;
  7855. begin
  7856. StartProgram(false);
  7857. Add('var s: AnsiString');
  7858. SetExpectedPasResolverError('identifier not found "AnsiString"',PasResolveEval.nIdentifierNotFound);
  7859. ConvertProgram;
  7860. end;
  7861. procedure TTestModule.TestBaseType_WideStringFail;
  7862. begin
  7863. StartProgram(false);
  7864. Add('var s: WideString');
  7865. SetExpectedPasResolverError('identifier not found "WideString"',PasResolveEval.nIdentifierNotFound);
  7866. ConvertProgram;
  7867. end;
  7868. procedure TTestModule.TestBaseType_ShortStringFail;
  7869. begin
  7870. StartProgram(false);
  7871. Add('var s: ShortString');
  7872. SetExpectedPasResolverError('identifier not found "ShortString"',PasResolveEval.nIdentifierNotFound);
  7873. ConvertProgram;
  7874. end;
  7875. procedure TTestModule.TestBaseType_RawByteStringFail;
  7876. begin
  7877. StartProgram(false);
  7878. Add('var s: RawByteString');
  7879. SetExpectedPasResolverError('identifier not found "RawByteString"',PasResolveEval.nIdentifierNotFound);
  7880. ConvertProgram;
  7881. end;
  7882. procedure TTestModule.TestTypeShortstring_Fail;
  7883. begin
  7884. StartProgram(false);
  7885. Add('type t = string[12];');
  7886. Add('var s: t;');
  7887. Add('begin');
  7888. SetExpectedPasResolverError('illegal qualifier "["',nIllegalQualifier);
  7889. ConvertProgram;
  7890. end;
  7891. procedure TTestModule.TestCharSet_Custom;
  7892. begin
  7893. StartProgram(false);
  7894. Add([
  7895. 'type',
  7896. ' TCharRg = ''a''..''z'';',
  7897. ' TSetOfCharRg = set of TCharRg;',
  7898. ' TCharRg2 = ''m''..''p'';',
  7899. 'const',
  7900. ' crg: TCharRg = ''b'';',
  7901. 'var',
  7902. ' c: char;',
  7903. ' crg2: TCharRg2;',
  7904. ' s: TSetOfCharRg;',
  7905. 'begin',
  7906. ' c:=crg;',
  7907. ' crg:=c;',
  7908. ' crg2:=crg;',
  7909. ' if c=crg then ;',
  7910. ' if crg=c then ;',
  7911. ' if crg=crg2 then ;',
  7912. ' if c in s then ;',
  7913. ' if crg2 in s then ;',
  7914. ' c:=default(TCharRg);',
  7915. '']);
  7916. ConvertProgram;
  7917. CheckSource('TestCharSet_Custom',
  7918. LinesToStr([ // statements
  7919. 'this.crg = "b";',
  7920. 'this.c = "";',
  7921. 'this.crg2 = "m";',
  7922. 'this.s = {};',
  7923. '']),
  7924. LinesToStr([ // this.$main
  7925. '$mod.c = $mod.crg;',
  7926. '$mod.crg = $mod.c;',
  7927. '$mod.crg2 = $mod.crg;',
  7928. 'if ($mod.c === $mod.crg) ;',
  7929. 'if ($mod.crg === $mod.c) ;',
  7930. 'if ($mod.crg === $mod.crg2) ;',
  7931. 'if ($mod.c.charCodeAt() in $mod.s) ;',
  7932. 'if ($mod.crg2.charCodeAt() in $mod.s) ;',
  7933. '$mod.c = "a";',
  7934. '']));
  7935. end;
  7936. procedure TTestModule.TestWideChar;
  7937. begin
  7938. StartProgram(false);
  7939. Add([
  7940. 'procedure Fly(var c: char);',
  7941. 'begin',
  7942. 'end;',
  7943. 'procedure Run(var c: widechar);',
  7944. 'begin',
  7945. 'end;',
  7946. 'var',
  7947. ' c: char;',
  7948. ' wc: widechar;',
  7949. ' w: word;',
  7950. 'begin',
  7951. ' Fly(wc);',
  7952. ' Run(c);',
  7953. ' wc:=WideChar(w);',
  7954. ' w:=ord(wc);',
  7955. '']);
  7956. ConvertProgram;
  7957. CheckSource('TestWideChar_VarArg',
  7958. LinesToStr([ // statements
  7959. 'this.Fly = function (c) {',
  7960. '};',
  7961. 'this.Run = function (c) {',
  7962. '};',
  7963. 'this.c = "";',
  7964. 'this.wc = "";',
  7965. 'this.w = 0;',
  7966. '']),
  7967. LinesToStr([ // this.$main
  7968. '$mod.Fly({',
  7969. ' p: $mod,',
  7970. ' get: function () {',
  7971. ' return this.p.wc;',
  7972. ' },',
  7973. ' set: function (v) {',
  7974. ' this.p.wc = v;',
  7975. ' }',
  7976. '});',
  7977. '$mod.Run({',
  7978. ' p: $mod,',
  7979. ' get: function () {',
  7980. ' return this.p.c;',
  7981. ' },',
  7982. ' set: function (v) {',
  7983. ' this.p.c = v;',
  7984. ' }',
  7985. '});',
  7986. '$mod.wc = String.fromCharCode($mod.w);',
  7987. '$mod.w = $mod.wc.charCodeAt();',
  7988. '',
  7989. '']));
  7990. end;
  7991. procedure TTestModule.TestForCharDo;
  7992. begin
  7993. StartProgram(false);
  7994. Add([
  7995. 'var c: char;',
  7996. 'begin',
  7997. ' for c:=''a'' to ''c'' do ;',
  7998. ' for c:=c downto ''a'' do ;',
  7999. ' for c:=''Б'' to ''Я'' do ;',
  8000. '']);
  8001. ConvertProgram;
  8002. CheckSource('TestForCharDo',
  8003. LinesToStr([ // statements
  8004. 'this.c = "";']),
  8005. LinesToStr([ // this.$main
  8006. 'for (var $l = 97; $l <= 99; $l++) $mod.c = String.fromCharCode($l);',
  8007. 'for (var $l1 = $mod.c.charCodeAt(); $l1 >= 97; $l1--) $mod.c = String.fromCharCode($l1);',
  8008. 'for (var $l2 = 1041; $l2 <= 1071; $l2++) $mod.c = String.fromCharCode($l2);',
  8009. '']));
  8010. end;
  8011. procedure TTestModule.TestForCharInDo;
  8012. begin
  8013. StartProgram(false);
  8014. Add([
  8015. 'type',
  8016. ' TSetOfChar = set of char;',
  8017. ' TCharRg = ''a''..''z'';',
  8018. ' TSetOfCharRg = set of TCharRg;',
  8019. 'const Foo = ''foo'';',
  8020. 'var',
  8021. ' c,c2: char;',
  8022. ' s: string;',
  8023. ' a1: array of char;',
  8024. ' a2: array[1..3] of char;',
  8025. ' soc: TSetOfChar;',
  8026. ' socr: TSetOfCharRg;',
  8027. ' cr: TCharRg;',
  8028. 'begin',
  8029. ' for c in foo do ;',
  8030. ' for c in s do ;',
  8031. ' for c in char do ;',
  8032. ' for c in a1 do ;',
  8033. ' for c in a2 do ;',
  8034. ' for c in [''1''..''3''] do ;',
  8035. ' for c in TSetOfChar do ;',
  8036. ' for c in TCharRg do ;',
  8037. ' for c in soc do c2:=c;',
  8038. ' for c in TSetOfCharRg do ;',
  8039. ' for c in socr do ;',
  8040. ' for cr in TCharRg do ;',
  8041. ' for cr in TSetOfCharRg do ;',
  8042. ' for cr in socr do ;',
  8043. '']);
  8044. ConvertProgram;
  8045. CheckSource('TestForCharInDo',
  8046. LinesToStr([ // statements
  8047. 'this.Foo = "foo";',
  8048. 'this.c = "";',
  8049. 'this.c2 = "";',
  8050. 'this.s = "";',
  8051. 'this.a1 = [];',
  8052. 'this.a2 = rtl.arraySetLength(null, "", 3);',
  8053. 'this.soc = {};',
  8054. 'this.socr = {};',
  8055. 'this.cr = "a";',
  8056. '']),
  8057. LinesToStr([ // this.$main
  8058. 'for (var $in = $mod.Foo, $l = 0, $end = $in.length - 1; $l <= $end; $l++) $mod.c = $in.charAt($l);',
  8059. 'for (var $in1 = $mod.s, $l1 = 0, $end1 = $in1.length - 1; $l1 <= $end1; $l1++) $mod.c = $in1.charAt($l1);',
  8060. 'for (var $l2 = 0; $l2 <= 65535; $l2++) $mod.c = String.fromCharCode($l2);',
  8061. 'for (var $in2 = $mod.a1, $l3 = 0, $end2 = rtl.length($in2) - 1; $l3 <= $end2; $l3++) $mod.c = $in2[$l3];',
  8062. 'for (var $in3 = $mod.a2, $l4 = 0, $end3 = rtl.length($in3) - 1; $l4 <= $end3; $l4++) $mod.c = $in3[$l4];',
  8063. 'for (var $l5 = 49; $l5 <= 51; $l5++) $mod.c = String.fromCharCode($l5);',
  8064. 'for (var $l6 = 0; $l6 <= 65535; $l6++) $mod.c = String.fromCharCode($l6);',
  8065. 'for (var $l7 = 97; $l7 <= 122; $l7++) $mod.c = String.fromCharCode($l7);',
  8066. 'for (var $l8 in $mod.soc) {',
  8067. ' $mod.c = String.fromCharCode($l8);',
  8068. ' $mod.c2 = $mod.c;',
  8069. '};',
  8070. 'for (var $l9 = 97; $l9 <= 122; $l9++) $mod.c = String.fromCharCode($l9);',
  8071. 'for (var $l10 in $mod.socr) $mod.c = String.fromCharCode($l10);',
  8072. 'for (var $l11 = 97; $l11 <= 122; $l11++) $mod.cr = String.fromCharCode($l11);',
  8073. 'for (var $l12 = 97; $l12 <= 122; $l12++) $mod.cr = String.fromCharCode($l12);',
  8074. 'for (var $l13 in $mod.socr) $mod.cr = String.fromCharCode($l13);',
  8075. '']));
  8076. end;
  8077. procedure TTestModule.TestProcTwoArgs;
  8078. begin
  8079. StartProgram(false);
  8080. Add('procedure Test(a,b: longint);');
  8081. Add('begin');
  8082. Add('end;');
  8083. Add('begin');
  8084. ConvertProgram;
  8085. CheckSource('TestProcTwoArgs',
  8086. LinesToStr([ // statements
  8087. 'this.Test = function (a,b) {',
  8088. '};'
  8089. ]),
  8090. LinesToStr([ // this.$main
  8091. ''
  8092. ]));
  8093. end;
  8094. procedure TTestModule.TestProc_DefaultValue;
  8095. begin
  8096. StartProgram(false);
  8097. Add('procedure p1(i: longint = 1);');
  8098. Add('begin');
  8099. Add('end;');
  8100. Add('procedure p2(i: longint = 1; c: char = ''a'');');
  8101. Add('begin');
  8102. Add('end;');
  8103. Add('procedure p3(d: double = 1.0; b: boolean = false; s: string = ''abc'');');
  8104. Add('begin');
  8105. Add('end;');
  8106. Add('begin');
  8107. Add(' p1;');
  8108. Add(' p1();');
  8109. Add(' p1(11);');
  8110. Add(' p2;');
  8111. Add(' p2();');
  8112. Add(' p2(12);');
  8113. Add(' p2(13,''b'');');
  8114. Add(' p3();');
  8115. ConvertProgram;
  8116. CheckSource('TestProc_DefaultValue',
  8117. LinesToStr([ // statements
  8118. 'this.p1 = function (i) {',
  8119. '};',
  8120. 'this.p2 = function (i,c) {',
  8121. '};',
  8122. 'this.p3 = function (d,b,s) {',
  8123. '};'
  8124. ]),
  8125. LinesToStr([ // this.$main
  8126. ' $mod.p1(1);',
  8127. ' $mod.p1(1);',
  8128. ' $mod.p1(11);',
  8129. ' $mod.p2(1,"a");',
  8130. ' $mod.p2(1,"a");',
  8131. ' $mod.p2(12,"a");',
  8132. ' $mod.p2(13,"b");',
  8133. ' $mod.p3(1.0,false,"abc");'
  8134. ]));
  8135. end;
  8136. procedure TTestModule.TestFunctionInt;
  8137. begin
  8138. StartProgram(false);
  8139. Add('function MyTest(Bar: longint): longint;');
  8140. Add('begin');
  8141. Add(' Result:=2*bar');
  8142. Add('end;');
  8143. Add('begin');
  8144. ConvertProgram;
  8145. CheckSource('TestFunctionInt',
  8146. LinesToStr([ // statements
  8147. 'this.MyTest = function (Bar) {',
  8148. ' var Result = 0;',
  8149. ' Result = 2*Bar;',
  8150. ' return Result;',
  8151. '};'
  8152. ]),
  8153. LinesToStr([ // this.$main
  8154. ''
  8155. ]));
  8156. end;
  8157. procedure TTestModule.TestFunctionString;
  8158. begin
  8159. StartProgram(false);
  8160. Add('function Test(Bar: string): string;');
  8161. Add('begin');
  8162. Add(' Result:=bar+BAR');
  8163. Add('end;');
  8164. Add('begin');
  8165. ConvertProgram;
  8166. CheckSource('TestFunctionString',
  8167. LinesToStr([ // statements
  8168. 'this.Test = function (Bar) {',
  8169. ' var Result = "";',
  8170. ' Result = Bar+Bar;',
  8171. ' return Result;',
  8172. '};'
  8173. ]),
  8174. LinesToStr([ // this.$main
  8175. ''
  8176. ]));
  8177. end;
  8178. procedure TTestModule.TestIfThen;
  8179. begin
  8180. StartProgram(false);
  8181. Add([
  8182. 'var b: boolean;',
  8183. 'begin',
  8184. ' if b then ;',
  8185. ' if b then else ;']);
  8186. ConvertProgram;
  8187. CheckSource('TestIfThen',
  8188. LinesToStr([ // statements
  8189. 'this.b = false;',
  8190. '']),
  8191. LinesToStr([ // this.$main
  8192. 'if ($mod.b) ;',
  8193. 'if ($mod.b) ;',
  8194. '']));
  8195. end;
  8196. procedure TTestModule.TestForLoop;
  8197. begin
  8198. StartProgram(false);
  8199. Add('var');
  8200. Add(' vI, vJ, vN: longint;');
  8201. Add('begin');
  8202. Add(' VJ:=0;');
  8203. Add(' VN:=3;');
  8204. Add(' for VI:=1 to VN do');
  8205. Add(' begin');
  8206. Add(' VJ:=VJ+VI;');
  8207. Add(' end;');
  8208. ConvertProgram;
  8209. CheckSource('TestForLoop',
  8210. LinesToStr([ // statements
  8211. 'this.vI = 0;',
  8212. 'this.vJ = 0;',
  8213. 'this.vN = 0;'
  8214. ]),
  8215. LinesToStr([ // this.$main
  8216. ' $mod.vJ = 0;',
  8217. ' $mod.vN = 3;',
  8218. ' for (var $l = 1, $end = $mod.vN; $l <= $end; $l++) {',
  8219. ' $mod.vI = $l;',
  8220. ' $mod.vJ = $mod.vJ + $mod.vI;',
  8221. ' };',
  8222. '']));
  8223. end;
  8224. procedure TTestModule.TestForLoopInsideFunction;
  8225. begin
  8226. StartProgram(false);
  8227. Add('function SumNumbers(Count: longint): longint;');
  8228. Add('var');
  8229. Add(' vI, vJ: longint;');
  8230. Add('begin');
  8231. Add(' vj:=0;');
  8232. Add(' for vi:=1 to count do');
  8233. Add(' begin');
  8234. Add(' vj:=vj+vi;');
  8235. Add(' end;');
  8236. Add('end;');
  8237. Add('begin');
  8238. Add(' sumnumbers(3);');
  8239. ConvertProgram;
  8240. CheckSource('TestForLoopInsideFunction',
  8241. LinesToStr([ // statements
  8242. 'this.SumNumbers = function (Count) {',
  8243. ' var Result = 0;',
  8244. ' var vI = 0;',
  8245. ' var vJ = 0;',
  8246. ' vJ = 0;',
  8247. ' for (var $l = 1, $end = Count; $l <= $end; $l++) {',
  8248. ' vI = $l;',
  8249. ' vJ = vJ + vI;',
  8250. ' };',
  8251. ' return Result;',
  8252. '};'
  8253. ]),
  8254. LinesToStr([ // $mod.$main
  8255. ' $mod.SumNumbers(3);'
  8256. ]));
  8257. end;
  8258. procedure TTestModule.TestForLoop_ReadVarAfter;
  8259. begin
  8260. StartProgram(false);
  8261. Add('var');
  8262. Add(' vI: longint;');
  8263. Add('begin');
  8264. Add(' for vi:=1 to 2 do ;');
  8265. Add(' if vi=3 then ;');
  8266. ConvertProgram;
  8267. CheckSource('TestForLoop',
  8268. LinesToStr([ // statements
  8269. 'this.vI = 0;'
  8270. ]),
  8271. LinesToStr([ // this.$main
  8272. ' for ($mod.vI = 1; $mod.vI <= 2; $mod.vI++) ;',
  8273. ' if ($mod.vI===3) ;'
  8274. ]));
  8275. end;
  8276. procedure TTestModule.TestForLoop_Nested;
  8277. begin
  8278. StartProgram(false);
  8279. Add('function SumNumbers(Count: longint): longint;');
  8280. Add('var');
  8281. Add(' vI, vJ, vK: longint;');
  8282. Add('begin');
  8283. Add(' VK:=0;');
  8284. Add(' for VI:=1 to count do');
  8285. Add(' begin');
  8286. Add(' for vj:=1 to vi do');
  8287. Add(' begin');
  8288. Add(' vk:=VK+VI;');
  8289. Add(' end;');
  8290. Add(' end;');
  8291. Add('end;');
  8292. Add('begin');
  8293. Add(' sumnumbers(3);');
  8294. ConvertProgram;
  8295. CheckSource('TestForLoopInFunction',
  8296. LinesToStr([ // statements
  8297. 'this.SumNumbers = function (Count) {',
  8298. ' var Result = 0;',
  8299. ' var vI = 0;',
  8300. ' var vJ = 0;',
  8301. ' var vK = 0;',
  8302. ' vK = 0;',
  8303. ' for (var $l = 1, $end = Count; $l <= $end; $l++) {',
  8304. ' vI = $l;',
  8305. ' for (var $l1 = 1, $end1 = vI; $l1 <= $end1; $l1++) {',
  8306. ' vJ = $l1;',
  8307. ' vK = vK + vI;',
  8308. ' };',
  8309. ' };',
  8310. ' return Result;',
  8311. '};'
  8312. ]),
  8313. LinesToStr([ // $mod.$main
  8314. ' $mod.SumNumbers(3);'
  8315. ]));
  8316. end;
  8317. procedure TTestModule.TestRepeatUntil;
  8318. begin
  8319. StartProgram(false);
  8320. Add('var');
  8321. Add(' vI, vJ, vN: longint;');
  8322. Add('begin');
  8323. Add(' vn:=3;');
  8324. Add(' vj:=0;');
  8325. Add(' VI:=0;');
  8326. Add(' repeat');
  8327. Add(' VI:=vi+1;');
  8328. Add(' vj:=VJ+vI;');
  8329. Add(' until vi>=vn');
  8330. ConvertProgram;
  8331. CheckSource('TestRepeatUntil',
  8332. LinesToStr([ // statements
  8333. 'this.vI = 0;',
  8334. 'this.vJ = 0;',
  8335. 'this.vN = 0;'
  8336. ]),
  8337. LinesToStr([ // $mod.$main
  8338. ' $mod.vN = 3;',
  8339. ' $mod.vJ = 0;',
  8340. ' $mod.vI = 0;',
  8341. ' do{',
  8342. ' $mod.vI = $mod.vI + 1;',
  8343. ' $mod.vJ = $mod.vJ + $mod.vI;',
  8344. ' }while(!($mod.vI>=$mod.vN));'
  8345. ]));
  8346. end;
  8347. procedure TTestModule.TestAsmBlock;
  8348. begin
  8349. StartProgram(false);
  8350. Add([
  8351. 'var',
  8352. ' vI: longint;',
  8353. 'begin',
  8354. ' vi:=1;',
  8355. ' asm',
  8356. ' if (vI===1) {',
  8357. ' vI=2;',
  8358. //' console.log(''end;'');', ToDo
  8359. ' }',
  8360. ' if (vI===2){ vI=3; }',
  8361. ' end;',
  8362. ' VI:=4;']);
  8363. ConvertProgram;
  8364. CheckSource('TestAsmBlock',
  8365. LinesToStr([ // statements
  8366. 'this.vI = 0;'
  8367. ]),
  8368. LinesToStr([ // $mod.$main
  8369. '$mod.vI = 1;',
  8370. 'if (vI===1) {',
  8371. ' vI=2;',
  8372. '}',
  8373. 'if (vI===2){ vI=3; }',
  8374. ';',
  8375. '$mod.vI = 4;'
  8376. ]));
  8377. end;
  8378. procedure TTestModule.TestAsmPas_Impl;
  8379. begin
  8380. StartUnit(false);
  8381. Add('interface');
  8382. Add('const cIntf: longint = 1;');
  8383. Add('var vIntf: longint;');
  8384. Add('implementation');
  8385. Add('const cImpl: longint = 2;');
  8386. Add('var vImpl: longint;');
  8387. Add('procedure DoIt;');
  8388. Add('const cLoc: longint = 3;');
  8389. Add('var vLoc: longint;');
  8390. Add('begin;');
  8391. Add(' asm');
  8392. //Add(' pas(vIntf)=pas(cIntf);');
  8393. //Add(' pas(vImpl)=pas(cImpl);');
  8394. //Add(' pas(vLoc)=pas(cLoc);');
  8395. Add(' end;');
  8396. Add('end;');
  8397. ConvertUnit;
  8398. CheckSource('TestAsmPas_Impl',
  8399. LinesToStr([
  8400. 'var $impl = $mod.$impl;',
  8401. 'this.cIntf = 1;',
  8402. 'this.vIntf = 0;',
  8403. '']),
  8404. '', // this.$init
  8405. LinesToStr([ // implementation
  8406. '$impl.cImpl = 2;',
  8407. '$impl.vImpl = 0;',
  8408. 'var cLoc = 3;',
  8409. '$impl.DoIt = function () {',
  8410. ' var vLoc = 0;',
  8411. '};',
  8412. '']) );
  8413. end;
  8414. procedure TTestModule.TestTryFinally;
  8415. begin
  8416. StartProgram(false);
  8417. Add('var i: longint;');
  8418. Add('begin');
  8419. Add(' try');
  8420. Add(' i:=0; i:=2 div i;');
  8421. Add(' finally');
  8422. Add(' i:=3');
  8423. Add(' end;');
  8424. ConvertProgram;
  8425. CheckSource('TestTryFinally',
  8426. LinesToStr([ // statements
  8427. 'this.i = 0;'
  8428. ]),
  8429. LinesToStr([ // $mod.$main
  8430. 'try {',
  8431. ' $mod.i = 0;',
  8432. ' $mod.i = rtl.trunc(2 / $mod.i);',
  8433. '} finally {',
  8434. ' $mod.i = 3;',
  8435. '};'
  8436. ]));
  8437. end;
  8438. procedure TTestModule.TestTryExcept;
  8439. begin
  8440. StartProgram(false);
  8441. Add([
  8442. 'type',
  8443. ' TObject = class end;',
  8444. ' Exception = class Msg: string; end;',
  8445. ' EInvalidCast = class(Exception) end;',
  8446. 'var vI: longint;',
  8447. 'begin',
  8448. ' try',
  8449. ' vi:=1;',
  8450. ' except',
  8451. ' vi:=2',
  8452. ' end;',
  8453. ' try',
  8454. ' vi:=3;',
  8455. ' except',
  8456. ' raise;',
  8457. ' end;',
  8458. ' try',
  8459. ' VI:=4;',
  8460. ' except',
  8461. ' on einvalidcast do',
  8462. ' raise;',
  8463. ' on E: exception do',
  8464. ' if e.msg='''' then',
  8465. ' raise e;',
  8466. ' else',
  8467. ' vi:=5',
  8468. ' end;',
  8469. ' try',
  8470. ' VI:=6;',
  8471. ' except',
  8472. ' on einvalidcast do ;',
  8473. ' end;',
  8474. '']);
  8475. ConvertProgram;
  8476. CheckSource('TestTryExcept',
  8477. LinesToStr([ // statements
  8478. 'rtl.createClass(this, "TObject", null, function () {',
  8479. ' this.$init = function () {',
  8480. ' };',
  8481. ' this.$final = function () {',
  8482. ' };',
  8483. '});',
  8484. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  8485. ' this.$init = function () {',
  8486. ' $mod.TObject.$init.call(this);',
  8487. ' this.Msg = "";',
  8488. ' };',
  8489. '});',
  8490. 'rtl.createClass(this, "EInvalidCast", this.Exception, function () {',
  8491. '});',
  8492. 'this.vI = 0;'
  8493. ]),
  8494. LinesToStr([ // $mod.$main
  8495. 'try {',
  8496. ' $mod.vI = 1;',
  8497. '} catch ($e) {',
  8498. ' $mod.vI = 2;',
  8499. '};',
  8500. 'try {',
  8501. ' $mod.vI = 3;',
  8502. '} catch ($e) {',
  8503. ' throw $e;',
  8504. '};',
  8505. 'try {',
  8506. ' $mod.vI = 4;',
  8507. '} catch ($e) {',
  8508. ' if ($mod.EInvalidCast.isPrototypeOf($e)){',
  8509. ' throw $e',
  8510. ' } else if ($mod.Exception.isPrototypeOf($e)) {',
  8511. ' var E = $e;',
  8512. ' if (E.Msg === "") throw E;',
  8513. ' } else {',
  8514. ' $mod.vI = 5;',
  8515. ' }',
  8516. '};',
  8517. 'try {',
  8518. ' $mod.vI = 6;',
  8519. '} catch ($e) {',
  8520. ' if ($mod.EInvalidCast.isPrototypeOf($e)){' ,
  8521. ' } else throw $e',
  8522. '};',
  8523. '']));
  8524. end;
  8525. procedure TTestModule.TestTryExcept_ReservedWords;
  8526. begin
  8527. StartProgram(false);
  8528. Add([
  8529. 'type',
  8530. ' TObject = class end;',
  8531. ' Exception = class',
  8532. ' Symbol: string;',
  8533. ' end;',
  8534. 'var &try: longint;',
  8535. 'begin',
  8536. ' try',
  8537. ' &try:=4;',
  8538. ' except',
  8539. ' on Error: exception do',
  8540. ' if errOR.symBol='''' then',
  8541. ' raise ERRor;',
  8542. ' end;',
  8543. '']);
  8544. ConvertProgram;
  8545. CheckSource('TestTryExcept_ReservedWords',
  8546. LinesToStr([ // statements
  8547. 'rtl.createClass(this, "TObject", null, function () {',
  8548. ' this.$init = function () {',
  8549. ' };',
  8550. ' this.$final = function () {',
  8551. ' };',
  8552. '});',
  8553. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  8554. ' this.$init = function () {',
  8555. ' $mod.TObject.$init.call(this);',
  8556. ' this.Symbol = "";',
  8557. ' };',
  8558. '});',
  8559. 'this.Try = 0;',
  8560. '']),
  8561. LinesToStr([ // $mod.$main
  8562. 'try {',
  8563. ' $mod.Try = 4;',
  8564. '} catch ($e) {',
  8565. ' if ($mod.Exception.isPrototypeOf($e)) {',
  8566. ' var error = $e;',
  8567. ' if (error.Symbol === "") throw error;',
  8568. ' } else throw $e',
  8569. '};',
  8570. '']));
  8571. end;
  8572. procedure TTestModule.TestIfThenRaiseElse;
  8573. begin
  8574. StartProgram(false);
  8575. Add([
  8576. 'type',
  8577. ' TObject = class',
  8578. ' constructor Create;',
  8579. ' end;',
  8580. 'constructor TObject.Create;',
  8581. 'begin',
  8582. 'end;',
  8583. 'var b: boolean;',
  8584. 'begin',
  8585. ' if b then',
  8586. ' raise TObject.Create',
  8587. ' else',
  8588. ' b:=false;',
  8589. '']);
  8590. ConvertProgram;
  8591. CheckSource('TestIfThenRaiseElse',
  8592. LinesToStr([ // statements
  8593. 'rtl.createClass(this, "TObject", null, function () {',
  8594. ' this.$init = function () {',
  8595. ' };',
  8596. ' this.$final = function () {',
  8597. ' };',
  8598. ' this.Create = function () {',
  8599. ' return this;',
  8600. ' };',
  8601. '});',
  8602. 'this.b = false;',
  8603. '']),
  8604. LinesToStr([ // $mod.$main
  8605. 'if ($mod.b) {',
  8606. ' throw $mod.TObject.$create("Create")}',
  8607. ' else $mod.b = false;',
  8608. '']));
  8609. end;
  8610. procedure TTestModule.TestCaseOf;
  8611. begin
  8612. StartProgram(false);
  8613. Add([
  8614. 'const e: longint; external name ''$e'';',
  8615. 'var vI: longint;',
  8616. 'begin',
  8617. ' case vi of',
  8618. ' 1: ;',
  8619. ' 2: vi:=3;',
  8620. ' e: ;',
  8621. ' else',
  8622. ' VI:=4',
  8623. ' end;']);
  8624. ConvertProgram;
  8625. CheckSource('TestCaseOf',
  8626. LinesToStr([ // statements
  8627. 'this.vI = 0;'
  8628. ]),
  8629. LinesToStr([ // $mod.$main
  8630. 'var $tmp = $mod.vI;',
  8631. 'if ($tmp === 1) {}',
  8632. 'else if ($tmp === 2) {',
  8633. ' $mod.vI = 3}',
  8634. ' else if ($tmp === $e) {}',
  8635. 'else {',
  8636. ' $mod.vI = 4;',
  8637. '};'
  8638. ]));
  8639. end;
  8640. procedure TTestModule.TestCaseOf_UseSwitch;
  8641. begin
  8642. StartProgram(false);
  8643. Converter.UseSwitchStatement:=true;
  8644. Add('var Vi: longint;');
  8645. Add('begin');
  8646. Add(' case vi of');
  8647. Add(' 1: ;');
  8648. Add(' 2: VI:=3;');
  8649. Add(' else');
  8650. Add(' vi:=4');
  8651. Add(' end;');
  8652. ConvertProgram;
  8653. CheckSource('TestCaseOf_UseSwitch',
  8654. LinesToStr([ // statements
  8655. 'this.Vi = 0;'
  8656. ]),
  8657. LinesToStr([ // $mod.$main
  8658. 'switch ($mod.Vi) {',
  8659. 'case 1:',
  8660. ' break;',
  8661. 'case 2:',
  8662. ' $mod.Vi = 3;',
  8663. ' break;',
  8664. 'default:',
  8665. ' $mod.Vi = 4;',
  8666. '};'
  8667. ]));
  8668. end;
  8669. procedure TTestModule.TestCaseOfNoElse;
  8670. begin
  8671. StartProgram(false);
  8672. Add('var Vi: longint;');
  8673. Add('begin');
  8674. Add(' case vi of');
  8675. Add(' 1: begin vi:=2; VI:=3; end;');
  8676. Add(' end;');
  8677. ConvertProgram;
  8678. CheckSource('TestCaseOfNoElse',
  8679. LinesToStr([ // statements
  8680. 'this.Vi = 0;'
  8681. ]),
  8682. LinesToStr([ // $mod.$main
  8683. 'var $tmp = $mod.Vi;',
  8684. 'if ($tmp === 1) {',
  8685. ' $mod.Vi = 2;',
  8686. ' $mod.Vi = 3;',
  8687. '};'
  8688. ]));
  8689. end;
  8690. procedure TTestModule.TestCaseOfNoElse_UseSwitch;
  8691. begin
  8692. StartProgram(false);
  8693. Converter.UseSwitchStatement:=true;
  8694. Add('var vI: longint;');
  8695. Add('begin');
  8696. Add(' case vi of');
  8697. Add(' 1: begin VI:=2; vi:=3; end;');
  8698. Add(' end;');
  8699. ConvertProgram;
  8700. CheckSource('TestCaseOfNoElse_UseSwitch',
  8701. LinesToStr([ // statements
  8702. 'this.vI = 0;'
  8703. ]),
  8704. LinesToStr([ // $mod.$main
  8705. 'switch ($mod.vI) {',
  8706. 'case 1:',
  8707. ' $mod.vI = 2;',
  8708. ' $mod.vI = 3;',
  8709. ' break;',
  8710. '};'
  8711. ]));
  8712. end;
  8713. procedure TTestModule.TestCaseOfRange;
  8714. begin
  8715. StartProgram(false);
  8716. Add('var vI: longint;');
  8717. Add('begin');
  8718. Add(' case vi of');
  8719. Add(' 1..3: vi:=14;');
  8720. Add(' 4,5: vi:=16;');
  8721. Add(' 6..7,9..10: ;');
  8722. Add(' else ;');
  8723. Add(' end;');
  8724. ConvertProgram;
  8725. CheckSource('TestCaseOfRange',
  8726. LinesToStr([ // statements
  8727. 'this.vI = 0;'
  8728. ]),
  8729. LinesToStr([ // $mod.$main
  8730. 'var $tmp = $mod.vI;',
  8731. 'if (($tmp >= 1) && ($tmp <= 3)){',
  8732. ' $mod.vI = 14',
  8733. '} else if (($tmp === 4) || ($tmp === 5)){',
  8734. ' $mod.vI = 16',
  8735. '} else if ((($tmp >= 6) && ($tmp <= 7)) || (($tmp >= 9) && ($tmp <= 10))) ;'
  8736. ]));
  8737. end;
  8738. procedure TTestModule.TestCaseOfString;
  8739. begin
  8740. StartProgram(false);
  8741. Add([
  8742. 'var s,h: string;',
  8743. 'begin',
  8744. ' case s of',
  8745. ' ''foo'': s:=h;',
  8746. ' ''a''..''z'': h:=s;',
  8747. ' ''ў'', ''ё'': ;',
  8748. ' ''Б''..''Я'': ;',
  8749. ' end;',
  8750. '']);
  8751. ConvertProgram;
  8752. CheckSource('TestCaseOfString',
  8753. LinesToStr([ // statements
  8754. 'this.s = "";',
  8755. 'this.h = "";',
  8756. '']),
  8757. LinesToStr([ // $mod.$main
  8758. 'var $tmp = $mod.s;',
  8759. 'if ($tmp === "foo") {',
  8760. ' $mod.s = $mod.h}',
  8761. ' else if (($tmp.length === 1) && ($tmp >= "a") && ($tmp <= "z")) {',
  8762. ' $mod.h = $mod.s}',
  8763. ' else if (($tmp === "ў") || ($tmp === "ё")) {}',
  8764. ' else if (($tmp.length === 1) && ($tmp >= "Б") && ($tmp <= "Я")) ;',
  8765. '']));
  8766. end;
  8767. procedure TTestModule.TestCaseOfChar;
  8768. begin
  8769. StartProgram(false);
  8770. Add([
  8771. 'var s,h: char;',
  8772. 'begin',
  8773. ' case s of',
  8774. ' ''a''..''z'': h:=s;',
  8775. ' ''ä'': ;',
  8776. ' ''ў'', ''ё'': ;',
  8777. ' ''Б''..''Я'': ;',
  8778. ' end;',
  8779. '']);
  8780. ConvertProgram;
  8781. CheckSource('TestCaseOfString',
  8782. LinesToStr([ // statements
  8783. 'this.s = "";',
  8784. 'this.h = "";',
  8785. '']),
  8786. LinesToStr([ // $mod.$main
  8787. 'var $tmp = $mod.s;',
  8788. 'if (($tmp >= "a") && ($tmp <= "z")) {',
  8789. ' $mod.h = $mod.s}',
  8790. ' else if ($tmp === "ä") {}',
  8791. ' else if (($tmp === "ў") || ($tmp === "ё")) {}',
  8792. ' else if (($tmp >= "Б") && ($tmp <= "Я")) ;',
  8793. '']));
  8794. end;
  8795. procedure TTestModule.TestCaseOfExternalClassConst;
  8796. begin
  8797. StartProgram(false);
  8798. Add([
  8799. '{$modeswitch externalclass}',
  8800. 'type',
  8801. ' TBird = class external name ''Bird''',
  8802. ' const e: longint;',
  8803. ' end;',
  8804. 'var vI: longint;',
  8805. 'begin',
  8806. ' case vi of',
  8807. ' 1: vi:=3;',
  8808. ' TBird.e: ;',
  8809. ' end;']);
  8810. ConvertProgram;
  8811. CheckSource('TestCaseOfExternalClassConst',
  8812. LinesToStr([ // statements
  8813. 'this.vI = 0;'
  8814. ]),
  8815. LinesToStr([ // $mod.$main
  8816. 'var $tmp = $mod.vI;',
  8817. 'if ($tmp === 1) {',
  8818. ' $mod.vI = 3}',
  8819. ' else if ($tmp === Bird.e) ;'
  8820. ]));
  8821. end;
  8822. procedure TTestModule.TestDebugger;
  8823. begin
  8824. StartProgram(false);
  8825. Add([
  8826. 'procedure DoIt;',
  8827. 'begin',
  8828. ' deBugger;',
  8829. ' DeBugger();',
  8830. 'end;',
  8831. 'begin',
  8832. ' Debugger;']);
  8833. ConvertProgram;
  8834. CheckSource('TestDebugger',
  8835. LinesToStr([ // statements
  8836. 'this.DoIt = function () {',
  8837. ' debugger;',
  8838. ' debugger;',
  8839. '};',
  8840. '']),
  8841. LinesToStr([ // $mod.$main
  8842. 'debugger;',
  8843. '']));
  8844. end;
  8845. procedure TTestModule.TestArray_Dynamic;
  8846. begin
  8847. StartProgram(false);
  8848. Add([
  8849. 'type',
  8850. ' TArrayInt = array of longint;',
  8851. 'var',
  8852. ' Arr: TArrayInt;',
  8853. ' i: longint;',
  8854. ' b: boolean;',
  8855. 'begin',
  8856. ' SetLength(arr,3);',
  8857. ' arr[0]:=4;',
  8858. ' arr[1]:=length(arr)+arr[0];',
  8859. ' arr[i]:=5;',
  8860. ' arr[arr[i]]:=arr[6];',
  8861. ' i:=low(arr);',
  8862. ' i:=high(arr);',
  8863. ' b:=Assigned(arr);',
  8864. ' Arr:=default(TArrayInt);']);
  8865. ConvertProgram;
  8866. CheckSource('TestArray_Dynamic',
  8867. LinesToStr([ // statements
  8868. 'this.Arr = [];',
  8869. 'this.i = 0;',
  8870. 'this.b = false;'
  8871. ]),
  8872. LinesToStr([ // $mod.$main
  8873. '$mod.Arr = rtl.arraySetLength($mod.Arr,0,3);',
  8874. '$mod.Arr[0] = 4;',
  8875. '$mod.Arr[1] = rtl.length($mod.Arr) + $mod.Arr[0];',
  8876. '$mod.Arr[$mod.i] = 5;',
  8877. '$mod.Arr[$mod.Arr[$mod.i]] = $mod.Arr[6];',
  8878. '$mod.i = 0;',
  8879. '$mod.i = rtl.length($mod.Arr) - 1;',
  8880. '$mod.b = rtl.length($mod.Arr) > 0;',
  8881. '$mod.Arr = [];',
  8882. '']));
  8883. end;
  8884. procedure TTestModule.TestArray_Dynamic_Nil;
  8885. begin
  8886. StartProgram(false);
  8887. Add('type');
  8888. Add(' TArrayInt = array of longint;');
  8889. Add('var');
  8890. Add(' Arr: TArrayInt;');
  8891. Add('procedure DoIt(const i: TArrayInt; j: TArrayInt); begin end;');
  8892. Add('begin');
  8893. Add(' arr:=nil;');
  8894. Add(' if arr=nil then;');
  8895. Add(' if nil=arr then;');
  8896. Add(' if arr<>nil then;');
  8897. Add(' if nil<>arr then;');
  8898. Add(' DoIt(nil,nil);');
  8899. ConvertProgram;
  8900. CheckSource('TestArray_Dynamic',
  8901. LinesToStr([ // statements
  8902. 'this.Arr = [];',
  8903. 'this.DoIt = function(i,j){',
  8904. '};'
  8905. ]),
  8906. LinesToStr([ // $mod.$main
  8907. '$mod.Arr = [];',
  8908. 'if (rtl.length($mod.Arr) === 0) ;',
  8909. 'if (rtl.length($mod.Arr) === 0) ;',
  8910. 'if (rtl.length($mod.Arr) > 0) ;',
  8911. 'if (rtl.length($mod.Arr) > 0) ;',
  8912. '$mod.DoIt([],[]);',
  8913. '']));
  8914. end;
  8915. procedure TTestModule.TestArray_DynMultiDimensional;
  8916. begin
  8917. StartProgram(false);
  8918. Add([
  8919. 'type',
  8920. ' TArrayInt = array of longint;',
  8921. ' TArrayArrayInt = array of TArrayInt;',
  8922. 'var',
  8923. ' Arr: TArrayInt;',
  8924. ' Arr2: TArrayArrayInt;',
  8925. ' i: longint;',
  8926. 'begin',
  8927. ' arr2:=nil;',
  8928. ' if arr2=nil then;',
  8929. ' if nil=arr2 then;',
  8930. ' i:=low(arr2);',
  8931. ' i:=low(arr2[1]);',
  8932. ' i:=high(arr2);',
  8933. ' i:=high(arr2[2]);',
  8934. ' arr2[3]:=arr;',
  8935. ' arr2[4][5]:=i;',
  8936. ' i:=arr2[6][7];',
  8937. ' arr2[8,9]:=i;',
  8938. ' i:=arr2[10,11];',
  8939. ' SetLength(arr2,14);',
  8940. ' SetLength(arr2[15],16);']);
  8941. ConvertProgram;
  8942. CheckSource('TestArray_Dynamic',
  8943. LinesToStr([ // statements
  8944. 'this.Arr = [];',
  8945. 'this.Arr2 = [];',
  8946. 'this.i = 0;'
  8947. ]),
  8948. LinesToStr([ // $mod.$main
  8949. '$mod.Arr2 = [];',
  8950. 'if (rtl.length($mod.Arr2) === 0) ;',
  8951. 'if (rtl.length($mod.Arr2) === 0) ;',
  8952. '$mod.i = 0;',
  8953. '$mod.i = 0;',
  8954. '$mod.i = rtl.length($mod.Arr2) - 1;',
  8955. '$mod.i = rtl.length($mod.Arr2[2]) - 1;',
  8956. '$mod.Arr2[3] = rtl.arrayRef($mod.Arr);',
  8957. '$mod.Arr2[4][5] = $mod.i;',
  8958. '$mod.i = $mod.Arr2[6][7];',
  8959. '$mod.Arr2[8][9] = $mod.i;',
  8960. '$mod.i = $mod.Arr2[10][11];',
  8961. '$mod.Arr2 = rtl.arraySetLength($mod.Arr2, [], 14);',
  8962. '$mod.Arr2[15] = rtl.arraySetLength($mod.Arr2[15], 0, 16);',
  8963. '']));
  8964. end;
  8965. procedure TTestModule.TestArray_DynamicAssign;
  8966. begin
  8967. StartProgram(false);
  8968. Add([
  8969. 'type',
  8970. ' TArrayInt = array of longint;',
  8971. ' TArrayArrayInt = array of TArrayInt;',
  8972. 'procedure Run(a: TArrayInt; const b: TArrayInt; constref c: TArrayInt);',
  8973. 'begin',
  8974. 'end;',
  8975. 'procedure Fly(var a: TArrayInt);',
  8976. 'begin',
  8977. 'end;',
  8978. 'var',
  8979. ' Arr: TArrayInt;',
  8980. ' Arr2: TArrayArrayInt;',
  8981. 'begin',
  8982. ' arr:=nil;',
  8983. ' arr2:=nil;',
  8984. ' arr2[1]:=nil;',
  8985. ' arr2[2]:=arr;',
  8986. ' Run(arr,arr,arr);',
  8987. ' Fly(arr);',
  8988. ' Run(arr2[4],arr2[5],arr2[6]);',
  8989. ' Fly(arr2[7]);',
  8990. '']);
  8991. ConvertProgram;
  8992. CheckSource('TestArray_DynamicAssign',
  8993. LinesToStr([ // statements
  8994. 'this.Run = function (a, b, c) {',
  8995. '};',
  8996. 'this.Fly = function (a) {',
  8997. '};',
  8998. 'this.Arr = [];',
  8999. 'this.Arr2 = [];',
  9000. '']),
  9001. LinesToStr([ // $mod.$main
  9002. '$mod.Arr = [];',
  9003. '$mod.Arr2 = [];',
  9004. '$mod.Arr2[1] = [];',
  9005. '$mod.Arr2[2] = rtl.arrayRef($mod.Arr);',
  9006. '$mod.Run(rtl.arrayRef($mod.Arr), $mod.Arr, $mod.Arr);',
  9007. '$mod.Fly({',
  9008. ' p: $mod,',
  9009. ' get: function () {',
  9010. ' return this.p.Arr;',
  9011. ' },',
  9012. ' set: function (v) {',
  9013. ' this.p.Arr = v;',
  9014. ' }',
  9015. '});',
  9016. '$mod.Run(rtl.arrayRef($mod.Arr2[4]), $mod.Arr2[5], $mod.Arr2[6]);',
  9017. '$mod.Fly({',
  9018. ' a: 7,',
  9019. ' p: $mod.Arr2,',
  9020. ' get: function () {',
  9021. ' return this.p[this.a];',
  9022. ' },',
  9023. ' set: function (v) {',
  9024. ' this.p[this.a] = v;',
  9025. ' }',
  9026. '});',
  9027. '']));
  9028. end;
  9029. procedure TTestModule.TestArray_StaticInt;
  9030. begin
  9031. StartProgram(false);
  9032. Add('type');
  9033. Add(' TArrayInt = array[2..4] of longint;');
  9034. Add('var');
  9035. Add(' Arr: TArrayInt;');
  9036. Add(' Arr2: TArrayInt = (5,6,7);');
  9037. Add(' i: longint;');
  9038. Add(' b: boolean;');
  9039. Add('begin');
  9040. Add(' arr[2]:=4;');
  9041. Add(' arr[3]:=arr[2]+arr[3];');
  9042. Add(' arr[i]:=5;');
  9043. Add(' arr[arr[i]]:=arr[high(arr)];');
  9044. Add(' i:=low(arr);');
  9045. Add(' i:=high(arr);');
  9046. Add(' b:=arr[2]=arr[3];');
  9047. Add(' arr:=default(TArrayInt);');
  9048. ConvertProgram;
  9049. CheckSource('TestArray_StaticInt',
  9050. LinesToStr([ // statements
  9051. 'this.Arr = rtl.arraySetLength(null,0,3);',
  9052. 'this.Arr2 = [5, 6, 7];',
  9053. 'this.i = 0;',
  9054. 'this.b = false;'
  9055. ]),
  9056. LinesToStr([ // $mod.$main
  9057. '$mod.Arr[0] = 4;',
  9058. '$mod.Arr[1] = $mod.Arr[0] + $mod.Arr[1];',
  9059. '$mod.Arr[$mod.i-2] = 5;',
  9060. '$mod.Arr[$mod.Arr[$mod.i-2]-2] = $mod.Arr[2];',
  9061. '$mod.i = 2;',
  9062. '$mod.i = 4;',
  9063. '$mod.b = $mod.Arr[0] === $mod.Arr[1];',
  9064. '$mod.Arr = rtl.arraySetLength(null,0,3);',
  9065. '']));
  9066. end;
  9067. procedure TTestModule.TestArray_StaticBool;
  9068. begin
  9069. StartProgram(false);
  9070. Add('type');
  9071. Add(' TBools = array[boolean] of boolean;');
  9072. Add(' TBool2 = array[true..true] of boolean;');
  9073. Add('var');
  9074. Add(' Arr: TBools;');
  9075. Add(' Arr2: TBool2;');
  9076. Add(' Arr3: TBools = (true,false);');
  9077. Add(' b: boolean;');
  9078. Add('begin');
  9079. Add(' b:=low(arr);');
  9080. Add(' b:=high(arr);');
  9081. Add(' arr[true]:=false;');
  9082. Add(' arr[false]:=arr[b] or arr[true];');
  9083. Add(' arr[b]:=true;');
  9084. Add(' arr[arr[b]]:=arr[high(arr)];');
  9085. Add(' b:=arr[false]=arr[true];');
  9086. Add(' b:=low(arr2);');
  9087. Add(' b:=high(arr2);');
  9088. Add(' arr2[true]:=true;');
  9089. Add(' arr2[true]:=arr2[true] and arr2[b];');
  9090. Add(' arr2[b]:=false;');
  9091. ConvertProgram;
  9092. CheckSource('TestArray_StaticBool',
  9093. LinesToStr([ // statements
  9094. 'this.Arr = rtl.arraySetLength(null,false,2);',
  9095. 'this.Arr2 = rtl.arraySetLength(null,false,1);',
  9096. 'this.Arr3 = [true, false];',
  9097. 'this.b = false;'
  9098. ]),
  9099. LinesToStr([ // $mod.$main
  9100. '$mod.b = false;',
  9101. '$mod.b = true;',
  9102. '$mod.Arr[1] = false;',
  9103. '$mod.Arr[0] = $mod.Arr[+$mod.b] || $mod.Arr[1];',
  9104. '$mod.Arr[+$mod.b] = true;',
  9105. '$mod.Arr[+$mod.Arr[+$mod.b]] = $mod.Arr[1];',
  9106. '$mod.b = $mod.Arr[0] === $mod.Arr[1];',
  9107. '$mod.b = true;',
  9108. '$mod.b = true;',
  9109. '$mod.Arr2[0] = true;',
  9110. '$mod.Arr2[0] = $mod.Arr2[0] && $mod.Arr2[1-$mod.b];',
  9111. '$mod.Arr2[1-$mod.b] = false;',
  9112. '']));
  9113. end;
  9114. procedure TTestModule.TestArray_StaticChar;
  9115. begin
  9116. StartProgram(false);
  9117. Add([
  9118. 'type',
  9119. ' TChars = array[char] of char;',
  9120. ' TChars2 = array[''a''..''z''] of char;',
  9121. 'var',
  9122. ' Arr: TChars;',
  9123. ' Arr2: TChars2;',
  9124. ' Arr3: array[2..4] of char = (''p'',''a'',''s'');',
  9125. ' Arr4: array[11..13] of char = ''pas'';',
  9126. ' Arr5: array[21..22] of char = ''äö'';',
  9127. ' Arr6: array[31..32] of char = ''ä''+''ö'';',
  9128. ' c: char;',
  9129. ' b: boolean;',
  9130. 'begin',
  9131. ' c:=low(arr);',
  9132. ' c:=high(arr);',
  9133. ' arr[''B'']:=''a'';',
  9134. ' arr[''D'']:=arr[c];',
  9135. ' arr[c]:=arr[''d''];',
  9136. ' arr[arr[c]]:=arr[high(arr)];',
  9137. ' b:=arr[low(arr)]=arr[''e''];',
  9138. ' c:=low(arr2);',
  9139. ' c:=high(arr2);',
  9140. ' arr2[''b'']:=''f'';',
  9141. ' arr2[''a'']:=arr2[c];',
  9142. ' arr2[c]:=arr2[''g''];']);
  9143. ConvertProgram;
  9144. CheckSource('TestArray_StaticChar',
  9145. LinesToStr([ // statements
  9146. 'this.Arr = rtl.arraySetLength(null, "", 65536);',
  9147. 'this.Arr2 = rtl.arraySetLength(null, "", 26);',
  9148. 'this.Arr3 = ["p", "a", "s"];',
  9149. 'this.Arr4 = ["p", "a", "s"];',
  9150. 'this.Arr5 = ["ä", "ö"];',
  9151. 'this.Arr6 = ["ä", "ö"];',
  9152. 'this.c = "";',
  9153. 'this.b = false;',
  9154. '']),
  9155. LinesToStr([ // $mod.$main
  9156. '$mod.c = "\x00";',
  9157. '$mod.c = "\uFFFF";',
  9158. '$mod.Arr[66] = "a";',
  9159. '$mod.Arr[68] = $mod.Arr[$mod.c.charCodeAt()];',
  9160. '$mod.Arr[$mod.c.charCodeAt()] = $mod.Arr[100];',
  9161. '$mod.Arr[$mod.Arr[$mod.c.charCodeAt()].charCodeAt()] = $mod.Arr[65535];',
  9162. '$mod.b = $mod.Arr[0] === $mod.Arr[101];',
  9163. '$mod.c = "a";',
  9164. '$mod.c = "z";',
  9165. '$mod.Arr2[1] = "f";',
  9166. '$mod.Arr2[0] = $mod.Arr2[$mod.c.charCodeAt() - 97];',
  9167. '$mod.Arr2[$mod.c.charCodeAt() - 97] = $mod.Arr2[6];',
  9168. '']));
  9169. end;
  9170. procedure TTestModule.TestArray_StaticMultiDim;
  9171. begin
  9172. StartProgram(false);
  9173. Add([
  9174. 'type',
  9175. ' TArrayInt = array[1..3] of longint;',
  9176. ' TArrayArrayInt = array[5..6] of TArrayInt;',
  9177. 'var',
  9178. ' Arr: TArrayInt;',
  9179. ' Arr2: TArrayArrayInt;',
  9180. ' Arr3: array[boolean] of TArrayInt = ((11,12,13),(21,22,23));',
  9181. ' i: longint;',
  9182. 'begin',
  9183. ' i:=low(arr);',
  9184. ' i:=low(arr2);',
  9185. ' i:=low(arr2[5]);',
  9186. ' i:=high(arr);',
  9187. ' i:=high(arr2);',
  9188. ' i:=high(arr2[6]);',
  9189. ' arr2[5]:=arr;',
  9190. ' arr2[6][2]:=i;',
  9191. ' i:=arr2[6][3];',
  9192. ' arr2[6,3]:=i;',
  9193. ' i:=arr2[5,2];',
  9194. ' arr2:=arr2;',// clone multi dim static array
  9195. ' arr3:=arr3;',// clone anonymous multi dim static array
  9196. '']);
  9197. ConvertProgram;
  9198. CheckSource('TestArray_StaticMultiDim',
  9199. LinesToStr([ // statements
  9200. 'this.TArrayArrayInt$clone = function (a) {',
  9201. ' var r = [];',
  9202. ' for (var i = 0; i < 2; i++) r.push(a[i].slice(0));',
  9203. ' return r;',
  9204. '};',
  9205. 'this.Arr = rtl.arraySetLength(null, 0, 3);',
  9206. 'this.Arr2 = rtl.arraySetLength(null, 0, 2, 3);',
  9207. 'this.Arr3$a$clone = function (a) {',
  9208. ' var r = [];',
  9209. ' for (var i = 0; i < 2; i++) r.push(a[i].slice(0));',
  9210. ' return r;',
  9211. '};',
  9212. 'this.Arr3 = [[11, 12, 13], [21, 22, 23]];',
  9213. 'this.i = 0;'
  9214. ]),
  9215. LinesToStr([ // $mod.$main
  9216. '$mod.i = 1;',
  9217. '$mod.i = 5;',
  9218. '$mod.i = 1;',
  9219. '$mod.i = 3;',
  9220. '$mod.i = 6;',
  9221. '$mod.i = 3;',
  9222. '$mod.Arr2[0] = $mod.Arr.slice(0);',
  9223. '$mod.Arr2[1][1] = $mod.i;',
  9224. '$mod.i = $mod.Arr2[1][2];',
  9225. '$mod.Arr2[1][2] = $mod.i;',
  9226. '$mod.i = $mod.Arr2[0][1];',
  9227. '$mod.Arr2 = $mod.TArrayArrayInt$clone($mod.Arr2);',
  9228. '$mod.Arr3 = $mod.Arr3$a$clone($mod.Arr3);',
  9229. '']));
  9230. end;
  9231. procedure TTestModule.TestArray_StaticInFunction;
  9232. begin
  9233. StartProgram(false);
  9234. Add([
  9235. 'const TArrayInt = 3;',
  9236. 'const TArrayArrayInt = 4;',
  9237. 'procedure DoIt;',
  9238. 'type',
  9239. ' TArrayInt = array[1..3] of longint;',
  9240. ' TArrayArrayInt = array[5..6] of TArrayInt;',
  9241. 'var',
  9242. ' Arr: TArrayInt;',
  9243. ' Arr2: TArrayArrayInt;',
  9244. ' Arr3: array[boolean] of TArrayInt = ((11,12,13),(21,22,23));',
  9245. ' i: longint;',
  9246. 'begin',
  9247. ' arr2[5]:=arr;',
  9248. ' arr2:=arr2;',// clone multi dim static array
  9249. ' arr3:=arr3;',// clone multi dim anonymous static array
  9250. 'end;',
  9251. 'begin',
  9252. '']);
  9253. ConvertProgram;
  9254. CheckSource('TestArray_StaticInFunction',
  9255. LinesToStr([ // statements
  9256. 'this.TArrayInt = 3;',
  9257. 'this.TArrayArrayInt = 4;',
  9258. 'var TArrayArrayInt$1$clone = function (a) {',
  9259. ' var r = [];',
  9260. ' for (var i = 0; i < 2; i++) r.push(a[i].slice(0));',
  9261. ' return r;',
  9262. '};',
  9263. 'var Arr3$a$clone = function (a) {',
  9264. ' var r = [];',
  9265. ' for (var i = 0; i < 2; i++) r.push(a[i].slice(0));',
  9266. ' return r;',
  9267. '};',
  9268. 'this.DoIt = function () {',
  9269. ' var Arr = rtl.arraySetLength(null, 0, 3);',
  9270. ' var Arr2 = rtl.arraySetLength(null, 0, 2, 3);',
  9271. ' var Arr3 = [[11, 12, 13], [21, 22, 23]];',
  9272. ' var i = 0;',
  9273. ' Arr2[0] = Arr.slice(0);',
  9274. ' Arr2 = TArrayArrayInt$1$clone(Arr2);',
  9275. ' Arr3 = Arr3$a$clone(Arr3);',
  9276. '};',
  9277. '']),
  9278. LinesToStr([ // $mod.$main
  9279. '']));
  9280. end;
  9281. procedure TTestModule.TestArray_StaticMultiDimEqualNotImplemented;
  9282. begin
  9283. StartProgram(false);
  9284. Add([
  9285. 'type',
  9286. ' TArrayInt = array[1..3,1..2] of longint;',
  9287. 'var',
  9288. ' a,b: TArrayInt;',
  9289. 'begin',
  9290. ' if a=b then ;',
  9291. '']);
  9292. SetExpectedPasResolverError('compare static array is not supported',
  9293. nXIsNotSupported);
  9294. ConvertProgram;
  9295. end;
  9296. procedure TTestModule.TestArrayOfRecord;
  9297. begin
  9298. StartProgram(false);
  9299. Add([
  9300. 'type',
  9301. ' TRec = record',
  9302. ' Int: longint;',
  9303. ' end;',
  9304. ' TArrayRec = array of TRec;',
  9305. 'procedure DoIt(vd: TRec; const vc: TRec; var vv: TRec);',
  9306. 'begin',
  9307. 'end;',
  9308. 'var',
  9309. ' Arr: TArrayRec;',
  9310. ' r: TRec;',
  9311. ' i: longint;',
  9312. 'begin',
  9313. ' SetLength(arr,3);',
  9314. ' arr[0].int:=4;',
  9315. ' arr[1].int:=length(arr)+arr[2].int;',
  9316. ' arr[arr[i].int].int:=arr[5].int;',
  9317. ' arr[7]:=r;',
  9318. ' r:=arr[8];',
  9319. ' i:=low(arr);',
  9320. ' i:=high(arr);',
  9321. ' DoIt(Arr[9],Arr[10],Arr[11]);']);
  9322. ConvertProgram;
  9323. CheckSource('TestArrayOfRecord',
  9324. LinesToStr([ // statements
  9325. 'rtl.recNewT(this, "TRec", function () {',
  9326. ' this.Int = 0;',
  9327. ' this.$eq = function (b) {',
  9328. ' return this.Int === b.Int;',
  9329. ' };',
  9330. ' this.$assign = function (s) {',
  9331. ' this.Int = s.Int;',
  9332. ' return this;',
  9333. ' };',
  9334. '});',
  9335. 'this.DoIt = function (vd, vc, vv) {',
  9336. '};',
  9337. 'this.Arr = [];',
  9338. 'this.r = this.TRec.$new();',
  9339. 'this.i = 0;'
  9340. ]),
  9341. LinesToStr([ // $mod.$main
  9342. '$mod.Arr = rtl.arraySetLength($mod.Arr,$mod.TRec,3);',
  9343. '$mod.Arr[0].Int = 4;',
  9344. '$mod.Arr[1].Int = rtl.length($mod.Arr)+$mod.Arr[2].Int;',
  9345. '$mod.Arr[$mod.Arr[$mod.i].Int].Int = $mod.Arr[5].Int;',
  9346. '$mod.Arr[7].$assign($mod.r);',
  9347. '$mod.r.$assign($mod.Arr[8]);',
  9348. '$mod.i = 0;',
  9349. '$mod.i = rtl.length($mod.Arr)-1;',
  9350. '$mod.DoIt($mod.TRec.$clone($mod.Arr[9]), $mod.Arr[10], $mod.Arr[11]);',
  9351. '']));
  9352. end;
  9353. procedure TTestModule.TestArray_StaticRecord;
  9354. begin
  9355. StartProgram(false);
  9356. Add([
  9357. 'type',
  9358. ' TRec = record',
  9359. ' Int: longint;',
  9360. ' end;',
  9361. ' TArrayRec = array[1..2] of TRec;',
  9362. 'var',
  9363. ' Arr: TArrayRec;',
  9364. 'begin',
  9365. ' arr[1].int:=length(arr)+low(arr)+high(arr);',
  9366. '']);
  9367. ConvertProgram;
  9368. CheckSource('TestArray_StaticRecord',
  9369. LinesToStr([ // statements
  9370. 'rtl.recNewT(this, "TRec", function () {',
  9371. ' this.Int = 0;',
  9372. ' this.$eq = function (b) {',
  9373. ' return this.Int === b.Int;',
  9374. ' };',
  9375. ' this.$assign = function (s) {',
  9376. ' this.Int = s.Int;',
  9377. ' return this;',
  9378. ' };',
  9379. '});',
  9380. 'this.TArrayRec$clone = function (a) {',
  9381. ' var r = [];',
  9382. ' for (var i = 0; i < 2; i++) r.push($mod.TRec.$clone(a[i]));',
  9383. ' return r;',
  9384. '};',
  9385. 'this.Arr = rtl.arraySetLength(null, this.TRec, 2);',
  9386. '']),
  9387. LinesToStr([ // $mod.$main
  9388. '$mod.Arr[0].Int = 2 + 1 + 2;']));
  9389. end;
  9390. procedure TTestModule.TestArrayOfSet;
  9391. begin
  9392. StartProgram(false);
  9393. Add([
  9394. 'type',
  9395. ' TFlag = (big,small);',
  9396. ' TSetOfFlag = set of tflag;',
  9397. ' TArrayFlag = array of TSetOfFlag;',
  9398. 'procedure DoIt(const a: Tarrayflag);',
  9399. 'begin',
  9400. 'end;',
  9401. 'var',
  9402. ' f: TFlag;',
  9403. ' s: TSetOfFlag;',
  9404. ' Arr: TArrayFlag;',
  9405. ' i: longint;',
  9406. 'begin',
  9407. ' SetLength(arr,3);',
  9408. ' arr[0]:=s;',
  9409. ' arr[1]:=[big];',
  9410. ' arr[2]:=[big]+s;',
  9411. ' arr[3]:=s+[big];',
  9412. ' arr[4]:=arr[5];',
  9413. ' s:=arr[6];',
  9414. ' i:=low(arr);',
  9415. ' i:=high(arr);',
  9416. ' DoIt(arr);',
  9417. ' DoIt([s]);',
  9418. ' DoIt([[],s]);',
  9419. ' DoIt([s,[]]);',
  9420. '']);
  9421. ConvertProgram;
  9422. CheckSource('TestArrayOfSet',
  9423. LinesToStr([ // statements
  9424. 'this.TFlag = {',
  9425. ' "0": "big",',
  9426. ' big: 0,',
  9427. ' "1": "small",',
  9428. ' small: 1',
  9429. '};',
  9430. 'this.DoIt = function (a) {',
  9431. '};',
  9432. 'this.f = 0;',
  9433. 'this.s = {};',
  9434. 'this.Arr = [];',
  9435. 'this.i = 0;',
  9436. '']),
  9437. LinesToStr([ // $mod.$main
  9438. '$mod.Arr = rtl.arraySetLength($mod.Arr, {}, 3);',
  9439. '$mod.Arr[0] = rtl.refSet($mod.s);',
  9440. '$mod.Arr[1] = rtl.createSet($mod.TFlag.big);',
  9441. '$mod.Arr[2] = rtl.unionSet(rtl.createSet($mod.TFlag.big), $mod.s);',
  9442. '$mod.Arr[3] = rtl.unionSet($mod.s, rtl.createSet($mod.TFlag.big));',
  9443. '$mod.Arr[4] = rtl.refSet($mod.Arr[5]);',
  9444. '$mod.s = rtl.refSet($mod.Arr[6]);',
  9445. '$mod.i = 0;',
  9446. '$mod.i = rtl.length($mod.Arr) - 1;',
  9447. '$mod.DoIt($mod.Arr);',
  9448. '$mod.DoIt([rtl.refSet($mod.s)]);',
  9449. '$mod.DoIt([{}, rtl.refSet($mod.s)]);',
  9450. '$mod.DoIt([rtl.refSet($mod.s), {}]);',
  9451. '']));
  9452. end;
  9453. procedure TTestModule.TestArray_DynAsParam;
  9454. begin
  9455. StartProgram(false);
  9456. Add([
  9457. 'type integer = longint;',
  9458. 'type TArrInt = array of integer;',
  9459. 'procedure DoIt(vG: TArrInt; const vH: TArrInt; var vI: TArrInt);',
  9460. 'var vJ: TArrInt;',
  9461. 'begin',
  9462. ' vg:=vg;',
  9463. ' vj:=vh;',
  9464. ' vi:=vi;',
  9465. ' doit(vg,vg,vg);',
  9466. ' doit(vh,vh,vj);',
  9467. ' doit(vi,vi,vi);',
  9468. ' doit(vj,vj,vj);',
  9469. 'end;',
  9470. 'var i: TArrInt;',
  9471. 'begin',
  9472. ' doit(i,i,i);']);
  9473. ConvertProgram;
  9474. CheckSource('TestArray_DynAsParams',
  9475. LinesToStr([ // statements
  9476. 'this.DoIt = function (vG,vH,vI) {',
  9477. ' var vJ = [];',
  9478. ' vG = rtl.arrayRef(vG);',
  9479. ' vJ = rtl.arrayRef(vH);',
  9480. ' vI.set(rtl.arrayRef(vI.get()));',
  9481. ' $mod.DoIt(rtl.arrayRef(vG), vG, {',
  9482. ' get: function () {',
  9483. ' return vG;',
  9484. ' },',
  9485. ' set: function (v) {',
  9486. ' vG = v;',
  9487. ' }',
  9488. ' });',
  9489. ' $mod.DoIt(rtl.arrayRef(vH), vH, {',
  9490. ' get: function () {',
  9491. ' return vJ;',
  9492. ' },',
  9493. ' set: function (v) {',
  9494. ' vJ = v;',
  9495. ' }',
  9496. ' });',
  9497. ' $mod.DoIt(rtl.arrayRef(vI.get()), vI.get(), vI);',
  9498. ' $mod.DoIt(rtl.arrayRef(vJ), vJ, {',
  9499. ' get: function () {',
  9500. ' return vJ;',
  9501. ' },',
  9502. ' set: function (v) {',
  9503. ' vJ = v;',
  9504. ' }',
  9505. ' });',
  9506. '};',
  9507. 'this.i = [];'
  9508. ]),
  9509. LinesToStr([
  9510. '$mod.DoIt(rtl.arrayRef($mod.i),$mod.i,{',
  9511. ' p: $mod,',
  9512. ' get: function () {',
  9513. ' return this.p.i;',
  9514. ' },',
  9515. ' set: function (v) {',
  9516. ' this.p.i = v;',
  9517. ' }',
  9518. '});'
  9519. ]));
  9520. end;
  9521. procedure TTestModule.TestArray_StaticAsParam;
  9522. begin
  9523. StartProgram(false);
  9524. Add([
  9525. 'type integer = longint;',
  9526. 'type TArrInt = array[1..2] of integer;',
  9527. 'procedure DoIt(vG: TArrInt; const vH: TArrInt; var vI: TArrInt);',
  9528. 'var vJ: TArrInt;',
  9529. 'begin',
  9530. ' vg:=vg;',
  9531. ' vj:=vh;',
  9532. ' vi:=vi;',
  9533. ' doit(vg,vg,vg);',
  9534. ' doit(vh,vh,vj);',
  9535. ' doit(vi,vi,vi);',
  9536. ' doit(vj,vj,vj);',
  9537. 'end;',
  9538. 'var i: TArrInt;',
  9539. 'begin',
  9540. ' doit(i,i,i);']);
  9541. ConvertProgram;
  9542. CheckSource('TestArray_StaticAsParams',
  9543. LinesToStr([ // statements
  9544. 'this.DoIt = function (vG,vH,vI) {',
  9545. ' var vJ = rtl.arraySetLength(null, 0, 2);',
  9546. ' vG = vG.slice(0);',
  9547. ' vJ = vH.slice(0);',
  9548. ' vI.set(vI.get().slice(0));',
  9549. ' $mod.DoIt(vG.slice(0), vG, {',
  9550. ' get: function () {',
  9551. ' return vG;',
  9552. ' },',
  9553. ' set: function (v) {',
  9554. ' vG = v;',
  9555. ' }',
  9556. ' });',
  9557. ' $mod.DoIt(vH.slice(0), vH, {',
  9558. ' get: function () {',
  9559. ' return vJ;',
  9560. ' },',
  9561. ' set: function (v) {',
  9562. ' vJ = v;',
  9563. ' }',
  9564. ' });',
  9565. ' $mod.DoIt(vI.get().slice(0), vI.get(), vI);',
  9566. ' $mod.DoIt(vJ.slice(0), vJ, {',
  9567. ' get: function () {',
  9568. ' return vJ;',
  9569. ' },',
  9570. ' set: function (v) {',
  9571. ' vJ = v;',
  9572. ' }',
  9573. ' });',
  9574. '};',
  9575. 'this.i = rtl.arraySetLength(null, 0, 2);'
  9576. ]),
  9577. LinesToStr([
  9578. '$mod.DoIt($mod.i.slice(0),$mod.i,{',
  9579. ' p: $mod,',
  9580. ' get: function () {',
  9581. ' return this.p.i;',
  9582. ' },',
  9583. ' set: function (v) {',
  9584. ' this.p.i = v;',
  9585. ' }',
  9586. '});'
  9587. ]));
  9588. end;
  9589. procedure TTestModule.TestArrayElement_AsParams;
  9590. begin
  9591. StartProgram(false);
  9592. Add('type integer = longint;');
  9593. Add('type TArrayInt = array of integer;');
  9594. Add('procedure DoIt(vG: Integer; const vH: Integer; var vI: Integer);');
  9595. Add('var vJ: tarrayint;');
  9596. Add('begin');
  9597. Add(' vi:=vi;');
  9598. Add(' doit(vi,vi,vi);');
  9599. Add(' doit(vj[1+1],vj[1+2],vj[1+3]);');
  9600. Add('end;');
  9601. Add('var a: TArrayInt;');
  9602. Add('begin');
  9603. Add(' doit(a[1+4],a[1+5],a[1+6]);');
  9604. ConvertProgram;
  9605. CheckSource('TestArrayElement_AsParams',
  9606. LinesToStr([ // statements
  9607. 'this.DoIt = function (vG,vH,vI) {',
  9608. ' var vJ = [];',
  9609. ' vI.set(vI.get());',
  9610. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  9611. ' $mod.DoIt(vJ[1+1], vJ[1+2], {',
  9612. ' a:1+3,',
  9613. ' p:vJ,',
  9614. ' get: function () {',
  9615. ' return this.p[this.a];',
  9616. ' },',
  9617. ' set: function (v) {',
  9618. ' this.p[this.a] = v;',
  9619. ' }',
  9620. ' });',
  9621. '};',
  9622. 'this.a = [];'
  9623. ]),
  9624. LinesToStr([
  9625. '$mod.DoIt($mod.a[1+4],$mod.a[1+5],{',
  9626. ' a: 1+6,',
  9627. ' p: $mod.a,',
  9628. ' get: function () {',
  9629. ' return this.p[this.a];',
  9630. ' },',
  9631. ' set: function (v) {',
  9632. ' this.p[this.a] = v;',
  9633. ' }',
  9634. '});'
  9635. ]));
  9636. end;
  9637. procedure TTestModule.TestArrayElementFromFuncResult_AsParams;
  9638. begin
  9639. StartProgram(false);
  9640. Add('type Integer = longint;');
  9641. Add('type TArrayInt = array of integer;');
  9642. Add('function GetArr(vB: integer = 0): tarrayint;');
  9643. Add('begin');
  9644. Add('end;');
  9645. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  9646. Add('begin');
  9647. Add('end;');
  9648. Add('begin');
  9649. Add(' doit(getarr[1+1],getarr[1+2],getarr[1+3]);');
  9650. Add(' doit(getarr()[2+1],getarr()[2+2],getarr()[2+3]);');
  9651. Add(' doit(getarr(7)[3+1],getarr(8)[3+2],getarr(9)[3+3]);');
  9652. ConvertProgram;
  9653. CheckSource('TestArrayElementFromFuncResult_AsParams',
  9654. LinesToStr([ // statements
  9655. 'this.GetArr = function (vB) {',
  9656. ' var Result = [];',
  9657. ' return Result;',
  9658. '};',
  9659. 'this.DoIt = function (vG,vH,vI) {',
  9660. '};'
  9661. ]),
  9662. LinesToStr([
  9663. '$mod.DoIt($mod.GetArr(0)[1+1],$mod.GetArr(0)[1+2],{',
  9664. ' a: 1+3,',
  9665. ' p: $mod.GetArr(0),',
  9666. ' get: function () {',
  9667. ' return this.p[this.a];',
  9668. ' },',
  9669. ' set: function (v) {',
  9670. ' this.p[this.a] = v;',
  9671. ' }',
  9672. '});',
  9673. '$mod.DoIt($mod.GetArr(0)[2+1],$mod.GetArr(0)[2+2],{',
  9674. ' a: 2+3,',
  9675. ' p: $mod.GetArr(0),',
  9676. ' get: function () {',
  9677. ' return this.p[this.a];',
  9678. ' },',
  9679. ' set: function (v) {',
  9680. ' this.p[this.a] = v;',
  9681. ' }',
  9682. '});',
  9683. '$mod.DoIt($mod.GetArr(7)[3+1],$mod.GetArr(8)[3+2],{',
  9684. ' a: 3+3,',
  9685. ' p: $mod.GetArr(9),',
  9686. ' get: function () {',
  9687. ' return this.p[this.a];',
  9688. ' },',
  9689. ' set: function (v) {',
  9690. ' this.p[this.a] = v;',
  9691. ' }',
  9692. '});',
  9693. '']));
  9694. end;
  9695. procedure TTestModule.TestArrayEnumTypeRange;
  9696. begin
  9697. StartProgram(false);
  9698. Add([
  9699. 'type',
  9700. ' TEnum = (red,blue);',
  9701. ' TEnumArray = array[TEnum] of longint;',
  9702. 'var',
  9703. ' e: TEnum;',
  9704. ' i: longint;',
  9705. ' a: TEnumArray;',
  9706. ' numbers: TEnumArray = (1,2);',
  9707. ' names: array[TEnum] of string = (''red'',''blue'');',
  9708. 'begin',
  9709. ' e:=low(a);',
  9710. ' e:=high(a);',
  9711. ' i:=a[red];',
  9712. ' a[e]:=a[e];']);
  9713. ConvertProgram;
  9714. CheckSource('TestArrayEnumTypeRange',
  9715. LinesToStr([ // statements
  9716. ' this.TEnum = {',
  9717. ' "0": "red",',
  9718. ' red: 0,',
  9719. ' "1": "blue",',
  9720. ' blue: 1',
  9721. '};',
  9722. 'this.e = 0;',
  9723. 'this.i = 0;',
  9724. 'this.a = rtl.arraySetLength(null,0,2);',
  9725. 'this.numbers = [1, 2];',
  9726. 'this.names = ["red", "blue"];',
  9727. '']),
  9728. LinesToStr([ // $mod.$main
  9729. '$mod.e = $mod.TEnum.red;',
  9730. '$mod.e = $mod.TEnum.blue;',
  9731. '$mod.i = $mod.a[$mod.TEnum.red];',
  9732. '$mod.a[$mod.e] = $mod.a[$mod.e];',
  9733. '']));
  9734. end;
  9735. procedure TTestModule.TestArray_SetLengthOutArg;
  9736. begin
  9737. StartProgram(false);
  9738. Add([
  9739. 'type TArrInt = array of longint;',
  9740. 'procedure DoIt(out a: TArrInt);',
  9741. 'begin',
  9742. ' SetLength(a,2);',
  9743. 'end;',
  9744. 'begin',
  9745. '']);
  9746. ConvertProgram;
  9747. CheckSource('TestArray_SetLengthOutArg',
  9748. LinesToStr([ // statements
  9749. 'this.DoIt = function (a) {',
  9750. ' a.set(rtl.arraySetLength(a.get(), 0, 2));',
  9751. '};',
  9752. '']),
  9753. LinesToStr([
  9754. '']));
  9755. end;
  9756. procedure TTestModule.TestArray_SetLengthProperty;
  9757. begin
  9758. StartProgram(false);
  9759. Add('type');
  9760. Add(' TArrInt = array of longint;');
  9761. Add(' TObject = class');
  9762. Add(' function GetColors: TArrInt; external name ''GetColors'';');
  9763. Add(' procedure SetColors(const Value: TArrInt); external name ''SetColors'';');
  9764. Add(' property Colors: TArrInt read GetColors write SetColors;');
  9765. Add(' end;');
  9766. Add('var Obj: TObject;');
  9767. Add('begin');
  9768. Add(' SetLength(Obj.Colors,2);');
  9769. ConvertProgram;
  9770. CheckSource('TestArray_SetLengthProperty',
  9771. LinesToStr([ // statements
  9772. 'rtl.createClass(this, "TObject", null, function () {',
  9773. ' this.$init = function () {',
  9774. ' };',
  9775. ' this.$final = function () {',
  9776. ' };',
  9777. '});',
  9778. 'this.Obj = null;',
  9779. '']),
  9780. LinesToStr([
  9781. '$mod.Obj.SetColors(rtl.arraySetLength($mod.Obj.GetColors(), 0, 2));',
  9782. '']));
  9783. end;
  9784. procedure TTestModule.TestArray_SetLengthMultiDim;
  9785. begin
  9786. StartProgram(false);
  9787. Add([
  9788. 'type',
  9789. ' TArrArrInt = array of array of longint;',
  9790. ' TArrStaInt = array of array[1..2] of longint;',
  9791. 'var',
  9792. ' a: TArrArrInt;',
  9793. ' b: TArrStaInt;',
  9794. 'begin',
  9795. ' SetLength(a,2);',
  9796. ' SetLength(a,3,4);',
  9797. ' SetLength(b,5);',
  9798. '']);
  9799. ConvertProgram;
  9800. CheckSource('TestArray_SetLengthMultiDim',
  9801. LinesToStr([ // statements
  9802. 'this.a = [];',
  9803. 'this.b = [];',
  9804. '']),
  9805. LinesToStr([
  9806. '$mod.a = rtl.arraySetLength($mod.a, [], 2);',
  9807. '$mod.a = rtl.arraySetLength($mod.a, 0, 3, 4);',
  9808. '$mod.b = rtl.arraySetLength($mod.b, 0, 5, "s", 2);',
  9809. '']));
  9810. end;
  9811. procedure TTestModule.TestArray_SetLengthDynOfStatic;
  9812. begin
  9813. StartProgram(false);
  9814. Add([
  9815. 'type',
  9816. ' TStaArr1 = array[1..3] of boolean;',
  9817. //' TStaArr2 = array[5..6] of TStaArr1;',
  9818. ' TDynArr1StaArr1 = array of TStaArr1;',
  9819. //' TDynArr1StaArr2 = array of TStaArr2;',
  9820. ' TDynArr2StaArr1 = array of TDynArr1StaArr1;',
  9821. //' TDynArr2StaArr2 = array of TDynArr1StaArr2;',
  9822. 'var',
  9823. ' DynArr1StaArr1: TDynArr1StaArr1;',
  9824. //' DynArr1StaArr2: TDynArr1StaArr1;',
  9825. ' DynArr2StaArr1: TDynArr2StaArr1;',
  9826. //' DynArr2StaArr2: TDynArr2StaArr2;',
  9827. 'begin',
  9828. ' SetLength(DynArr1StaArr1,11);',
  9829. ' SetLength(DynArr2StaArr1,12);',
  9830. ' SetLength(DynArr2StaArr1[13],14);',
  9831. ' SetLength(DynArr2StaArr1,15,16);',
  9832. //' SetLength(DynArr1StaArr2,21);',
  9833. //' SetLength(DynArr2StaArr2,22);',
  9834. //' SetLength(DynArr2StaArr2[23],24);',
  9835. //' SetLength(DynArr2StaArr2,25,26);',
  9836. '']);
  9837. ConvertProgram;
  9838. CheckSource('TestArray_DynOfStatic',
  9839. LinesToStr([ // statements
  9840. 'this.DynArr1StaArr1 = [];',
  9841. 'this.DynArr2StaArr1 = [];',
  9842. '']),
  9843. LinesToStr([ // $mod.$main
  9844. '$mod.DynArr1StaArr1 = rtl.arraySetLength($mod.DynArr1StaArr1, false, 11, "s", 3);',
  9845. '$mod.DynArr2StaArr1 = rtl.arraySetLength($mod.DynArr2StaArr1, [], 12);',
  9846. '$mod.DynArr2StaArr1[13] = rtl.arraySetLength($mod.DynArr2StaArr1[13], false, 14, "s", 3);',
  9847. '$mod.DynArr2StaArr1 = rtl.arraySetLength(',
  9848. ' $mod.DynArr2StaArr1,',
  9849. ' false,',
  9850. ' 15,',
  9851. ' 16,',
  9852. ' "s",',
  9853. ' 3',
  9854. ');',
  9855. '']));
  9856. end;
  9857. procedure TTestModule.TestArray_OpenArrayOfString;
  9858. begin
  9859. StartProgram(false);
  9860. Add('procedure DoIt(const a: array of String);');
  9861. Add('var');
  9862. Add(' i: longint;');
  9863. Add(' s: string;');
  9864. Add('begin');
  9865. Add(' for i:=low(a) to high(a) do s:=a[length(a)-i-1];');
  9866. Add('end;');
  9867. Add('var s: string;');
  9868. Add('begin');
  9869. Add(' DoIt([]);');
  9870. Add(' DoIt([s,''foo'','''',s+s]);');
  9871. ConvertProgram;
  9872. CheckSource('TestArray_OpenArrayOfString',
  9873. LinesToStr([ // statements
  9874. 'this.DoIt = function (a) {',
  9875. ' var i = 0;',
  9876. ' var s = "";',
  9877. ' for (var $l = 0, $end = rtl.length(a) - 1; $l <= $end; $l++) {',
  9878. ' i = $l;',
  9879. ' s = a[rtl.length(a) - i - 1];',
  9880. ' };',
  9881. '};',
  9882. 'this.s = "";',
  9883. '']),
  9884. LinesToStr([
  9885. '$mod.DoIt([]);',
  9886. '$mod.DoIt([$mod.s, "foo", "", $mod.s + $mod.s]);',
  9887. '']));
  9888. end;
  9889. procedure TTestModule.TestArray_ArrayOfCharAssignString;
  9890. begin
  9891. StartProgram(false);
  9892. Add([
  9893. 'type TArr = array of char;',
  9894. 'var',
  9895. ' c: char;',
  9896. ' s: string;',
  9897. ' a: TArr;',
  9898. 'procedure Run(const a: array of char);',
  9899. 'begin',
  9900. ' Run(c);',
  9901. ' Run(s);',
  9902. 'end;',
  9903. 'begin',
  9904. ' a:=c;',
  9905. ' a:=s;',
  9906. ' a:=#13;',
  9907. ' a:=''Foo'';',
  9908. ' Run(c);',
  9909. ' Run(s);',
  9910. '']);
  9911. ConvertProgram;
  9912. CheckSource('TestArray_ArrayOfCharAssignString',
  9913. LinesToStr([ // statements
  9914. 'this.c = "";',
  9915. 'this.s = "";',
  9916. 'this.a = [];',
  9917. 'this.Run = function (a) {',
  9918. ' $mod.Run($mod.c.split(""));',
  9919. ' $mod.Run($mod.s.split(""));',
  9920. '};',
  9921. '']),
  9922. LinesToStr([
  9923. '$mod.a = $mod.c.split("");',
  9924. '$mod.a = $mod.s.split("");',
  9925. '$mod.a = "\r".split("");',
  9926. '$mod.a = "Foo".split("");',
  9927. '$mod.Run($mod.c.split(""));',
  9928. '$mod.Run($mod.s.split(""));',
  9929. '']));
  9930. end;
  9931. procedure TTestModule.TestArray_ConstRef;
  9932. begin
  9933. StartProgram(false);
  9934. Add([
  9935. 'type TArr = array of word;',
  9936. 'procedure Run(constref a: TArr);',
  9937. 'begin',
  9938. 'end;',
  9939. 'procedure Fly(a: TArr; var b: TArr; out c: TArr; const d: TArr; constref e: TArr);',
  9940. 'var l: TArr;',
  9941. 'begin',
  9942. ' Run(l);',
  9943. ' Run(a);',
  9944. ' Run(b);',
  9945. ' Run(c);',
  9946. ' Run(d);',
  9947. ' Run(e);',
  9948. 'end;',
  9949. 'begin',
  9950. '']);
  9951. ConvertProgram;
  9952. CheckResolverUnexpectedHints();
  9953. CheckSource('TestArray_ConstRef',
  9954. LinesToStr([ // statements
  9955. 'this.Run = function (a) {',
  9956. '};',
  9957. 'this.Fly = function (a, b, c, d, e) {',
  9958. ' var l = [];',
  9959. ' $mod.Run(l);',
  9960. ' $mod.Run(a);',
  9961. ' $mod.Run(b.get());',
  9962. ' $mod.Run(c.get());',
  9963. ' $mod.Run(d);',
  9964. ' $mod.Run(e);',
  9965. '};',
  9966. '']),
  9967. LinesToStr([
  9968. '']));
  9969. end;
  9970. procedure TTestModule.TestArray_Concat;
  9971. begin
  9972. StartProgram(false);
  9973. Add([
  9974. 'type',
  9975. ' integer = longint;',
  9976. ' TFlag = (big,small);',
  9977. ' TFlags = set of TFlag;',
  9978. ' TRec = record',
  9979. ' i: integer;',
  9980. ' end;',
  9981. ' TArrInt = array of integer;',
  9982. ' TArrRec = array of TRec;',
  9983. ' TArrFlag = array of TFlag;',
  9984. ' TArrSet = array of TFlags;',
  9985. ' TArrJSValue = array of jsvalue;',
  9986. 'var',
  9987. ' ArrInt: tarrint;',
  9988. ' ArrRec: tarrrec;',
  9989. ' ArrFlag: tarrflag;',
  9990. ' ArrSet: tarrset;',
  9991. ' ArrJSValue: tarrjsvalue;',
  9992. 'begin',
  9993. ' arrint:=concat(arrint);',
  9994. ' arrint:=concat(arrint,arrint);',
  9995. ' arrint:=concat(arrint,arrint,arrint);',
  9996. ' arrrec:=concat(arrrec);',
  9997. ' arrrec:=concat(arrrec,arrrec);',
  9998. ' arrrec:=concat(arrrec,arrrec,arrrec);',
  9999. ' arrset:=concat(arrset);',
  10000. ' arrset:=concat(arrset,arrset);',
  10001. ' arrset:=concat(arrset,arrset,arrset);',
  10002. ' arrjsvalue:=concat(arrjsvalue);',
  10003. ' arrjsvalue:=concat(arrjsvalue,arrjsvalue);',
  10004. ' arrjsvalue:=concat(arrjsvalue,arrjsvalue,arrjsvalue);',
  10005. ' arrint:=concat([1],arrint);',
  10006. ' arrflag:=concat([big]);',
  10007. ' arrflag:=concat([big],arrflag);',
  10008. ' arrflag:=concat(arrflag,[small]);',
  10009. '']);
  10010. ConvertProgram;
  10011. CheckSource('TestArray_Concat',
  10012. LinesToStr([ // statements
  10013. 'this.TFlag = {',
  10014. ' "0": "big",',
  10015. ' big: 0,',
  10016. ' "1": "small",',
  10017. ' small: 1',
  10018. '};',
  10019. 'rtl.recNewT(this, "TRec", function () {',
  10020. ' this.i = 0;',
  10021. ' this.$eq = function (b) {',
  10022. ' return this.i === b.i;',
  10023. ' };',
  10024. ' this.$assign = function (s) {',
  10025. ' this.i = s.i;',
  10026. ' return this;',
  10027. ' };',
  10028. '});',
  10029. 'this.ArrInt = [];',
  10030. 'this.ArrRec = [];',
  10031. 'this.ArrFlag = [];',
  10032. 'this.ArrSet = [];',
  10033. 'this.ArrJSValue = [];',
  10034. '']),
  10035. LinesToStr([ // $mod.$main
  10036. '$mod.ArrInt = rtl.arrayRef($mod.ArrInt);',
  10037. '$mod.ArrInt = rtl.arrayConcatN($mod.ArrInt, $mod.ArrInt);',
  10038. '$mod.ArrInt = rtl.arrayConcatN($mod.ArrInt, $mod.ArrInt, $mod.ArrInt);',
  10039. '$mod.ArrRec = rtl.arrayRef($mod.ArrRec);',
  10040. '$mod.ArrRec = rtl.arrayConcat($mod.TRec, $mod.ArrRec, $mod.ArrRec);',
  10041. '$mod.ArrRec = rtl.arrayConcat($mod.TRec, $mod.ArrRec, $mod.ArrRec, $mod.ArrRec);',
  10042. '$mod.ArrSet = rtl.arrayRef($mod.ArrSet);',
  10043. '$mod.ArrSet = rtl.arrayConcat("refSet", $mod.ArrSet, $mod.ArrSet);',
  10044. '$mod.ArrSet = rtl.arrayConcat("refSet", $mod.ArrSet, $mod.ArrSet, $mod.ArrSet);',
  10045. '$mod.ArrJSValue = rtl.arrayRef($mod.ArrJSValue);',
  10046. '$mod.ArrJSValue = rtl.arrayConcatN($mod.ArrJSValue, $mod.ArrJSValue);',
  10047. '$mod.ArrJSValue = rtl.arrayConcatN($mod.ArrJSValue, $mod.ArrJSValue, $mod.ArrJSValue);',
  10048. '$mod.ArrInt = rtl.arrayConcatN([1], $mod.ArrInt);',
  10049. '$mod.ArrFlag = [$mod.TFlag.big];',
  10050. '$mod.ArrFlag = rtl.arrayConcatN([$mod.TFlag.big], $mod.ArrFlag);',
  10051. '$mod.ArrFlag = rtl.arrayConcatN($mod.ArrFlag, [$mod.TFlag.small]);',
  10052. '']));
  10053. end;
  10054. procedure TTestModule.TestArray_Copy;
  10055. begin
  10056. StartProgram(false);
  10057. Add([
  10058. 'type',
  10059. ' integer = longint;',
  10060. ' TFlag = (big,small);',
  10061. ' TFlags = set of TFlag;',
  10062. ' TRec = record',
  10063. ' i: integer;',
  10064. ' end;',
  10065. ' TArrInt = array of integer;',
  10066. ' TArrRec = array of TRec;',
  10067. ' TArrSet = array of TFlags;',
  10068. ' TArrJSValue = array of jsvalue;',
  10069. 'var',
  10070. ' ArrInt: tarrint;',
  10071. ' ArrRec: tarrrec;',
  10072. ' ArrSet: tarrset;',
  10073. ' ArrJSValue: tarrjsvalue;',
  10074. 'begin',
  10075. ' arrint:=copy(arrint);',
  10076. ' arrint:=copy(arrint,2);',
  10077. ' arrint:=copy(arrint,3,4);',
  10078. ' arrint:=copy([1,1],1,2);',
  10079. ' arrrec:=copy(arrrec);',
  10080. ' arrrec:=copy(arrrec,5);',
  10081. ' arrrec:=copy(arrrec,6,7);',
  10082. ' arrset:=copy(arrset);',
  10083. ' arrset:=copy(arrset,8);',
  10084. ' arrset:=copy(arrset,9,10);',
  10085. ' arrjsvalue:=copy(arrjsvalue);',
  10086. ' arrjsvalue:=copy(arrjsvalue,11);',
  10087. ' arrjsvalue:=copy(arrjsvalue,12,13);',
  10088. ' ']);
  10089. ConvertProgram;
  10090. CheckSource('TestArray_Copy',
  10091. LinesToStr([ // statements
  10092. 'this.TFlag = {',
  10093. ' "0": "big",',
  10094. ' big: 0,',
  10095. ' "1": "small",',
  10096. ' small: 1',
  10097. '};',
  10098. 'rtl.recNewT(this, "TRec", function () {',
  10099. ' this.i = 0;',
  10100. ' this.$eq = function (b) {',
  10101. ' return this.i === b.i;',
  10102. ' };',
  10103. ' this.$assign = function (s) {',
  10104. ' this.i = s.i;',
  10105. ' return this;',
  10106. ' };',
  10107. '});',
  10108. 'this.ArrInt = [];',
  10109. 'this.ArrRec = [];',
  10110. 'this.ArrSet = [];',
  10111. 'this.ArrJSValue = [];',
  10112. '']),
  10113. LinesToStr([ // $mod.$main
  10114. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 0);',
  10115. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 2);',
  10116. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 3, 4);',
  10117. '$mod.ArrInt = rtl.arrayCopy(0, [1, 1], 1, 2);',
  10118. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 0);',
  10119. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 5);',
  10120. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 6, 7);',
  10121. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 0);',
  10122. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 8);',
  10123. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 9, 10);',
  10124. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 0);',
  10125. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 11);',
  10126. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 12, 13);',
  10127. '']));
  10128. end;
  10129. procedure TTestModule.TestArray_InsertDelete;
  10130. begin
  10131. StartProgram(false);
  10132. Add([
  10133. 'type',
  10134. ' integer = longint;',
  10135. ' TFlag = (big,small);',
  10136. ' TFlags = set of TFlag;',
  10137. ' TRec = record',
  10138. ' i: integer;',
  10139. ' end;',
  10140. ' TArrInt = array of integer;',
  10141. ' TArrRec = array of TRec;',
  10142. ' TArrSet = array of TFlags;',
  10143. ' TArrJSValue = array of jsvalue;',
  10144. ' TArrArrInt = array of TArrInt;',
  10145. 'var',
  10146. ' ArrInt: tarrint;',
  10147. ' ArrRec: tarrrec;',
  10148. ' ArrSet: tarrset;',
  10149. ' ArrJSValue: tarrjsvalue;',
  10150. ' ArrArrInt: TArrArrInt;',
  10151. 'begin',
  10152. ' Insert(1,arrint,2);',
  10153. ' Insert(arrint[3],arrint,4);',
  10154. ' Insert(arrrec[5],arrrec,6);',
  10155. ' Insert(arrset[7],arrset,7);',
  10156. ' Insert(arrjsvalue[8],arrjsvalue,9);',
  10157. ' Insert(10,arrjsvalue,11);',
  10158. ' Insert([23],arrarrint,22);',
  10159. ' Delete(arrint,12,13);',
  10160. ' Delete(arrrec,14,15);',
  10161. ' Delete(arrset,17,18);',
  10162. ' Delete(arrjsvalue,19,10);']);
  10163. ConvertProgram;
  10164. CheckSource('TestArray_InsertDelete',
  10165. LinesToStr([ // statements
  10166. 'this.TFlag = {',
  10167. ' "0": "big",',
  10168. ' big: 0,',
  10169. ' "1": "small",',
  10170. ' small: 1',
  10171. '};',
  10172. 'rtl.recNewT(this, "TRec", function () {',
  10173. ' this.i = 0;',
  10174. ' this.$eq = function (b) {',
  10175. ' return this.i === b.i;',
  10176. ' };',
  10177. ' this.$assign = function (s) {',
  10178. ' this.i = s.i;',
  10179. ' return this;',
  10180. ' };',
  10181. '});',
  10182. 'this.ArrInt = [];',
  10183. 'this.ArrRec = [];',
  10184. 'this.ArrSet = [];',
  10185. 'this.ArrJSValue = [];',
  10186. 'this.ArrArrInt = [];',
  10187. '']),
  10188. LinesToStr([ // $mod.$main
  10189. '$mod.ArrInt = rtl.arrayInsert(1, $mod.ArrInt, 2);',
  10190. '$mod.ArrInt = rtl.arrayInsert($mod.ArrInt[3], $mod.ArrInt, 4);',
  10191. '$mod.ArrRec = rtl.arrayInsert($mod.ArrRec[5], $mod.ArrRec, 6);',
  10192. '$mod.ArrSet = rtl.arrayInsert($mod.ArrSet[7], $mod.ArrSet, 7);',
  10193. '$mod.ArrJSValue = rtl.arrayInsert($mod.ArrJSValue[8], $mod.ArrJSValue, 9);',
  10194. '$mod.ArrJSValue = rtl.arrayInsert(10, $mod.ArrJSValue, 11);',
  10195. '$mod.ArrArrInt = rtl.arrayInsert([23], $mod.ArrArrInt, 22);',
  10196. '$mod.ArrInt.splice(12, 13);',
  10197. '$mod.ArrRec.splice(14, 15);',
  10198. '$mod.ArrSet.splice(17, 18);',
  10199. '$mod.ArrJSValue.splice(19, 10);',
  10200. '']));
  10201. end;
  10202. procedure TTestModule.TestArray_DynArrayConstObjFPC;
  10203. begin
  10204. Parser.Options:=Parser.Options+[po_cassignments];
  10205. StartProgram(false);
  10206. Add([
  10207. '{$modeswitch arrayoperators}',
  10208. 'type',
  10209. ' integer = longint;',
  10210. ' TArrInt = array of integer;',
  10211. ' TArrStr = array of string;',
  10212. 'const',
  10213. ' Ints: TArrInt = (1,2,3);',
  10214. ' Aliases: TarrStr = (''foo'',''b'');',
  10215. ' OneInt: TArrInt = (7);',
  10216. ' OneStr: array of integer = (7);',
  10217. ' Chars: array of char = ''aoc'';',
  10218. ' Names: array of string = (''a'',''foo'');',
  10219. ' NameCount = low(Names)+high(Names)+length(Names);',
  10220. 'var i: integer;',
  10221. 'begin',
  10222. ' Ints:=[];',
  10223. ' Ints:=[1,1];',
  10224. ' Ints:=[1]+[2];',
  10225. ' Ints:=[2];',
  10226. ' Ints:=[]+ints;',
  10227. ' Ints:=Ints+[];',
  10228. ' Ints:=Ints+OneInt;',
  10229. ' Ints:=Ints+[1,1];',
  10230. ' Ints:=[i,i]+Ints;',
  10231. ' Ints:=[1]+[i]+[3];',
  10232. '']);
  10233. ConvertProgram;
  10234. CheckSource('TestArray_DynArrayConstObjFPC',
  10235. LinesToStr([ // statements
  10236. 'this.Ints = [1, 2, 3];',
  10237. 'this.Aliases = ["foo", "b"];',
  10238. 'this.OneInt = [7];',
  10239. 'this.OneStr = [7];',
  10240. 'this.Chars = ["a", "o", "c"];',
  10241. 'this.Names = ["a", "foo"];',
  10242. 'this.NameCount = 0 + (rtl.length(this.Names) - 1) + rtl.length(this.Names);',
  10243. 'this.i = 0;',
  10244. '']),
  10245. LinesToStr([ // $mod.$main
  10246. '$mod.Ints = [];',
  10247. '$mod.Ints = [1, 1];',
  10248. '$mod.Ints = rtl.arrayConcatN([1], [2]);',
  10249. '$mod.Ints = [2];',
  10250. '$mod.Ints = rtl.arrayConcatN([], $mod.Ints);',
  10251. '$mod.Ints = rtl.arrayConcatN($mod.Ints, []);',
  10252. '$mod.Ints = rtl.arrayConcatN($mod.Ints, $mod.OneInt);',
  10253. '$mod.Ints = rtl.arrayConcatN($mod.Ints, [1, 1]);',
  10254. '$mod.Ints = rtl.arrayConcatN([$mod.i, $mod.i], $mod.Ints);',
  10255. '$mod.Ints = rtl.arrayConcatN(rtl.arrayConcatN([1], [$mod.i]), [3]);',
  10256. '']));
  10257. end;
  10258. procedure TTestModule.TestArray_DynArrayConstDelphi;
  10259. begin
  10260. StartProgram(false);
  10261. // Note: const c = [1,1]; defines a set!
  10262. Add([
  10263. '{$mode delphi}',
  10264. 'type',
  10265. ' integer = longint;',
  10266. ' TArrInt = array of integer;',
  10267. ' TArrStr = array of string;',
  10268. 'const',
  10269. ' Ints: TArrInt = [1,1,2];',
  10270. ' Aliases: TarrStr = [''foo'',''b''];',
  10271. ' OneInt: TArrInt = [7];',
  10272. ' OneStr: array of integer = [7]+[8];',
  10273. ' Chars: array of char = ''aoc'';',
  10274. ' Names: array of string = [''a'',''a''];',
  10275. ' NameCount = low(Names)+high(Names)+length(Names);',
  10276. 'begin',
  10277. '']);
  10278. ConvertProgram;
  10279. CheckSource('TestArray_DynArrayConstDelphi',
  10280. LinesToStr([ // statements
  10281. 'this.Ints = [1, 1, 2];',
  10282. 'this.Aliases = ["foo", "b"];',
  10283. 'this.OneInt = [7];',
  10284. 'this.OneStr = rtl.arrayConcatN([7],[8]);',
  10285. 'this.Chars = ["a", "o", "c"];',
  10286. 'this.Names = ["a", "a"];',
  10287. 'this.NameCount = 0 + (rtl.length(this.Names) - 1) + rtl.length(this.Names);',
  10288. '']),
  10289. LinesToStr([ // $mod.$main
  10290. '']));
  10291. end;
  10292. procedure TTestModule.TestArray_ArrayLitAsParam;
  10293. begin
  10294. StartProgram(false);
  10295. Add([
  10296. '{$modeswitch arrayoperators}',
  10297. 'type',
  10298. ' integer = longint;',
  10299. ' TArrInt = array of integer;',
  10300. ' TArrSet = array of (red,green,blue);',
  10301. 'procedure DoOpenInt(const a: array of integer); forward;',
  10302. 'procedure DoInt(const a: TArrInt);',
  10303. 'begin',
  10304. ' DoInt(a+[1]);',
  10305. ' DoInt([1]+a);',
  10306. ' DoOpenInt(a);',
  10307. ' DoOpenInt(a+[1]);',
  10308. ' DoOpenInt([1]+a);',
  10309. 'end;',
  10310. 'procedure DoOpenInt(const a: array of integer);',
  10311. 'begin',
  10312. ' DoOpenInt(a+[1]);',
  10313. ' DoOpenInt([1]+a);',
  10314. ' DoInt(a);',
  10315. ' DoInt(a+[1]);',
  10316. ' DoInt([1]+a);',
  10317. 'end;',
  10318. 'procedure DoSet(const a: TArrSet);',
  10319. 'begin',
  10320. ' DoSet(a+[red]);',
  10321. ' DoSet([blue]+a);',
  10322. 'end;',
  10323. 'var',
  10324. ' i: TArrInt;',
  10325. ' s: TArrSet;',
  10326. 'begin',
  10327. ' DoInt([1]);',
  10328. ' DoInt([1]+[2]);',
  10329. ' DoInt(i+[1]);',
  10330. ' DoInt([1]+i);',
  10331. ' DoOpenInt([1]);',
  10332. ' DoOpenInt([1]+[2]);',
  10333. ' DoOpenInt(i+[1]);',
  10334. ' DoOpenInt([1]+i);',
  10335. ' DoSet([red]);',
  10336. ' DoSet([blue]+[green]);',
  10337. ' DoSet(s+[blue]);',
  10338. ' DoSet([red]+s);',
  10339. '']);
  10340. ConvertProgram;
  10341. CheckSource('TestArray_ArrayLitAsParam',
  10342. LinesToStr([ // statements
  10343. 'this.TArrSet$a = {',
  10344. ' "0": "red",',
  10345. ' red: 0,',
  10346. ' "1": "green",',
  10347. ' green: 1,',
  10348. ' "2": "blue",',
  10349. ' blue: 2',
  10350. '};',
  10351. 'this.DoInt = function (a) {',
  10352. ' $mod.DoInt(rtl.arrayConcatN(a, [1]));',
  10353. ' $mod.DoInt(rtl.arrayConcatN([1], a));',
  10354. ' $mod.DoOpenInt(a);',
  10355. ' $mod.DoOpenInt(rtl.arrayConcatN(a, [1]));',
  10356. ' $mod.DoOpenInt(rtl.arrayConcatN([1], a));',
  10357. '};',
  10358. 'this.DoOpenInt = function (a) {',
  10359. ' $mod.DoOpenInt(rtl.arrayConcatN(a, [1]));',
  10360. ' $mod.DoOpenInt(rtl.arrayConcatN([1], a));',
  10361. ' $mod.DoInt(a);',
  10362. ' $mod.DoInt(rtl.arrayConcatN(a, [1]));',
  10363. ' $mod.DoInt(rtl.arrayConcatN([1], a));',
  10364. '};',
  10365. 'this.DoSet = function (a) {',
  10366. ' $mod.DoSet(rtl.arrayConcatN(a, [$mod.TArrSet$a.red]));',
  10367. ' $mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.blue], a));',
  10368. '};',
  10369. 'this.i = [];',
  10370. 'this.s = [];',
  10371. '']),
  10372. LinesToStr([ // $mod.$main
  10373. '$mod.DoInt([1]);',
  10374. '$mod.DoInt(rtl.arrayConcatN([1], [2]));',
  10375. '$mod.DoInt(rtl.arrayConcatN($mod.i, [1]));',
  10376. '$mod.DoInt(rtl.arrayConcatN([1], $mod.i));',
  10377. '$mod.DoOpenInt([1]);',
  10378. '$mod.DoOpenInt(rtl.arrayConcatN([1], [2]));',
  10379. '$mod.DoOpenInt(rtl.arrayConcatN($mod.i, [1]));',
  10380. '$mod.DoOpenInt(rtl.arrayConcatN([1], $mod.i));',
  10381. '$mod.DoSet([$mod.TArrSet$a.red]);',
  10382. '$mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.blue], [$mod.TArrSet$a.green]));',
  10383. '$mod.DoSet(rtl.arrayConcatN($mod.s, [$mod.TArrSet$a.blue]));',
  10384. '$mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.red], $mod.s));',
  10385. '']));
  10386. end;
  10387. procedure TTestModule.TestArray_ArrayLitMultiDimAsParam;
  10388. begin
  10389. StartProgram(false);
  10390. Add([
  10391. '{$modeswitch arrayoperators}',
  10392. 'type',
  10393. ' integer = longint;',
  10394. ' TArrInt = array of integer;',
  10395. ' TArrArrInt = array of TArrInt;',
  10396. 'procedure DoInt(const a: TArrArrInt);',
  10397. 'begin',
  10398. ' DoInt(a+[[1]]);',
  10399. ' DoInt([[1]]+a);',
  10400. ' DoInt(a);',
  10401. 'end;',
  10402. 'var',
  10403. ' i: TArrInt;',
  10404. ' a: TArrArrInt;',
  10405. 'begin',
  10406. ' a:=[[1]];',
  10407. ' a:=[i];',
  10408. ' a:=a+[i];',
  10409. ' a:=[i]+a;',
  10410. ' a:=[[1]+i];',
  10411. ' a:=[[1]+[2]];',
  10412. ' a:=[i+[2]];',
  10413. ' DoInt([[1]]);',
  10414. ' DoInt([[1]+[2],[3,4],[5]]);',
  10415. ' DoInt([i+[1]]+a);',
  10416. ' DoInt([i]+a);',
  10417. '']);
  10418. ConvertProgram;
  10419. CheckSource('TestArray_ArrayLitMultiDimAsParam',
  10420. LinesToStr([ // statements
  10421. 'this.DoInt = function (a) {',
  10422. ' $mod.DoInt(rtl.arrayConcatN(a, [[1]]));',
  10423. ' $mod.DoInt(rtl.arrayConcatN([[1]], a));',
  10424. ' $mod.DoInt(a);',
  10425. '};',
  10426. 'this.i = [];',
  10427. 'this.a = [];',
  10428. '']),
  10429. LinesToStr([ // $mod.$main
  10430. '$mod.a = [[1]];',
  10431. '$mod.a = [$mod.i];',
  10432. '$mod.a = rtl.arrayConcatN($mod.a, [$mod.i]);',
  10433. '$mod.a = rtl.arrayConcatN([$mod.i], $mod.a);',
  10434. '$mod.a = [rtl.arrayConcatN([1], $mod.i)];',
  10435. '$mod.a = [rtl.arrayConcatN([1], [2])];',
  10436. '$mod.a = [rtl.arrayConcatN($mod.i, [2])];',
  10437. '$mod.DoInt([[1]]);',
  10438. '$mod.DoInt([rtl.arrayConcatN([1], [2]), [3, 4], [5]]);',
  10439. '$mod.DoInt(rtl.arrayConcatN([rtl.arrayConcatN($mod.i, [1])], $mod.a));',
  10440. '$mod.DoInt(rtl.arrayConcatN([$mod.i], $mod.a));',
  10441. '']));
  10442. end;
  10443. procedure TTestModule.TestArray_ArrayLitStaticAsParam;
  10444. begin
  10445. StartProgram(false);
  10446. Add([
  10447. '{$modeswitch arrayoperators}',
  10448. 'type',
  10449. ' integer = longint;',
  10450. ' TArrInt = array[1..2] of integer;',
  10451. ' TArrArrInt = array of TArrInt;',
  10452. 'procedure DoInt(const a: TArrArrInt);',
  10453. 'begin',
  10454. ' DoInt(a+[[1,2]]);',
  10455. ' DoInt([[1,2]]+a);',
  10456. ' DoInt(a);',
  10457. 'end;',
  10458. 'var',
  10459. ' i: TArrInt;',
  10460. ' a: TArrArrInt;',
  10461. 'begin',
  10462. ' a:=[[1,1]];',
  10463. ' a:=[i];',
  10464. ' a:=a+[i];',
  10465. ' a:=[i]+a;',
  10466. ' DoInt([[1,1]]);',
  10467. ' DoInt([[1,2],[3,4]]);',
  10468. '']);
  10469. ConvertProgram;
  10470. CheckSource('TestArray_ArrayLitStaticAsParam',
  10471. LinesToStr([ // statements
  10472. 'this.DoInt = function (a) {',
  10473. ' $mod.DoInt(rtl.arrayConcatN(a, [[1, 2]]));',
  10474. ' $mod.DoInt(rtl.arrayConcatN([[1, 2]], a));',
  10475. ' $mod.DoInt(a);',
  10476. '};',
  10477. 'this.i = rtl.arraySetLength(null, 0, 2);',
  10478. 'this.a = [];',
  10479. '']),
  10480. LinesToStr([ // $mod.$main
  10481. '$mod.a = [[1, 1]];',
  10482. '$mod.a = [$mod.i.slice(0)];',
  10483. '$mod.a = rtl.arrayConcatN($mod.a, [$mod.i.slice(0)]);',
  10484. '$mod.a = rtl.arrayConcatN([$mod.i.slice(0)], $mod.a);',
  10485. '$mod.DoInt([[1, 1]]);',
  10486. '$mod.DoInt([[1, 2], [3, 4]]);',
  10487. '']));
  10488. end;
  10489. procedure TTestModule.TestArray_ForInArrOfString;
  10490. begin
  10491. StartProgram(false);
  10492. Add([
  10493. 'type',
  10494. 'type',
  10495. ' TMonthNameArray = array [1..12] of string;',
  10496. ' TMonthNames = TMonthNameArray;',
  10497. ' TObject = class',
  10498. ' private',
  10499. ' function GetLongMonthNames: TMonthNames; virtual; abstract;',
  10500. ' public',
  10501. ' Property LongMonthNames : TMonthNames Read GetLongMonthNames;',
  10502. ' end;',
  10503. 'var',
  10504. ' f: TObject;',
  10505. ' Month: string;',
  10506. ' Names: array of string = (''a'',''foo'',''bar'');',
  10507. ' i: longint;',
  10508. 'begin',
  10509. ' for Month in f.LongMonthNames do ;',
  10510. ' for Month in Names do ;',
  10511. ' for i:=low(Names) to high(Names) do ;',
  10512. '']);
  10513. ConvertProgram;
  10514. CheckSource('TestArray_ForInArrOfString',
  10515. LinesToStr([ // statements
  10516. 'rtl.createClass(this, "TObject", null, function () {',
  10517. ' this.$init = function () {',
  10518. ' };',
  10519. ' this.$final = function () {',
  10520. ' };',
  10521. '});',
  10522. 'this.f = null;',
  10523. 'this.Month = "";',
  10524. 'this.Names = ["a", "foo", "bar"];',
  10525. 'this.i = 0;',
  10526. '']),
  10527. LinesToStr([ // $mod.$main
  10528. 'for (var $in = $mod.f.GetLongMonthNames(), $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) $mod.Month = $in[$l];',
  10529. 'for (var $in1 = $mod.Names, $l1 = 0, $end1 = rtl.length($in1) - 1; $l1 <= $end1; $l1++) $mod.Month = $in1[$l1];',
  10530. 'for (var $l2 = 0, $end2 = rtl.length($mod.Names) - 1; $l2 <= $end2; $l2++) $mod.i = $l2;',
  10531. '']));
  10532. end;
  10533. procedure TTestModule.TestExternalClass_TypeCastArrayToExternalClass;
  10534. begin
  10535. StartProgram(false);
  10536. Add([
  10537. '{$modeswitch externalclass}',
  10538. 'type',
  10539. ' TJSObject = class external name ''Object''',
  10540. ' end;',
  10541. ' TJSArray = class external name ''Array''',
  10542. ' class function isArray(Value: JSValue) : boolean;',
  10543. ' function concat() : TJSArray; varargs;',
  10544. ' end;',
  10545. 'var',
  10546. ' aObj: TJSArray;',
  10547. ' a: array of longint;',
  10548. ' o: TJSObject;',
  10549. 'begin',
  10550. ' if TJSArray.isArray(65) then ;',
  10551. ' aObj:=TJSArray(a).concat(a);',
  10552. ' o:=TJSObject(a);']);
  10553. ConvertProgram;
  10554. CheckSource('TestExternalClass_TypeCastArrayToExternalClass',
  10555. LinesToStr([ // statements
  10556. 'this.aObj = null;',
  10557. 'this.a = [];',
  10558. 'this.o = null;',
  10559. '']),
  10560. LinesToStr([ // $mod.$main
  10561. 'if (Array.isArray(65)) ;',
  10562. '$mod.aObj = $mod.a.concat($mod.a);',
  10563. '$mod.o = $mod.a;',
  10564. '']));
  10565. end;
  10566. procedure TTestModule.TestExternalClass_TypeCastArrayFromExternalClass;
  10567. begin
  10568. StartProgram(false);
  10569. Add([
  10570. '{$modeswitch externalclass}',
  10571. 'type',
  10572. ' TArrStr = array of string;',
  10573. ' TJSArray = class external name ''Array''',
  10574. ' end;',
  10575. ' TJSObject = class external name ''Object''',
  10576. ' end;',
  10577. 'var',
  10578. ' aObj: TJSArray;',
  10579. ' a: TArrStr;',
  10580. ' jo: TJSObject;',
  10581. 'begin',
  10582. ' a:=TArrStr(aObj);',
  10583. ' TArrStr(aObj)[1]:=TArrStr(aObj)[2];',
  10584. ' a:=TarrStr(jo);',
  10585. '']);
  10586. ConvertProgram;
  10587. CheckSource('TestExternalClass_TypeCastArrayFromExternalClass',
  10588. LinesToStr([ // statements
  10589. 'this.aObj = null;',
  10590. 'this.a = [];',
  10591. 'this.jo = null;',
  10592. '']),
  10593. LinesToStr([ // $mod.$main
  10594. '$mod.a = $mod.aObj;',
  10595. '$mod.aObj[1] = $mod.aObj[2];',
  10596. '$mod.a = $mod.jo;',
  10597. '']));
  10598. end;
  10599. procedure TTestModule.TestArrayOfConst_TVarRec;
  10600. begin
  10601. StartProgram(true,[supTVarRec]);
  10602. Add([
  10603. 'procedure Say(args: array of const);',
  10604. 'var',
  10605. ' i: longint;',
  10606. ' v: TVarRec;',
  10607. 'begin',
  10608. ' for i:=low(args) to high(args) do begin',
  10609. ' v:=args[i];',
  10610. ' case v.vtype of',
  10611. ' vtInteger: if length(args)=args[i].vInteger then ;',
  10612. ' end;',
  10613. ' end;',
  10614. ' for v in args do ;',
  10615. ' args:=nil;',
  10616. ' SetLength(args,2);',
  10617. 'end;',
  10618. 'begin']);
  10619. ConvertProgram;
  10620. CheckSource('TestArrayOfConst_TVarRec',
  10621. LinesToStr([ // statements
  10622. 'this.Say = function (args) {',
  10623. ' var i = 0;',
  10624. ' var v = pas.system.TVarRec.$new();',
  10625. ' for (var $l = 0, $end = rtl.length(args) - 1; $l <= $end; $l++) {',
  10626. ' i = $l;',
  10627. ' v.$assign(args[i]);',
  10628. ' var $tmp = v.VType;',
  10629. ' if ($tmp === 0) if (rtl.length(args) === args[i].VJSValue) ;',
  10630. ' };',
  10631. ' for (var $in = args, $l1 = 0, $end1 = rtl.length($in) - 1; $l1 <= $end1; $l1++) v = $in[$l1];',
  10632. ' args = [];',
  10633. ' args = rtl.arraySetLength(args, pas.system.TVarRec, 2);',
  10634. '};',
  10635. '']),
  10636. LinesToStr([ // $mod.$main
  10637. ]));
  10638. end;
  10639. procedure TTestModule.TestArrayOfConst_PassBaseTypes;
  10640. begin
  10641. StartProgram(true,[supTVarRec]);
  10642. Add([
  10643. 'procedure Say(args: array of const);',
  10644. 'begin',
  10645. ' Say(args);',
  10646. 'end;',
  10647. 'var',
  10648. ' p: Pointer;',
  10649. ' j: jsvalue;',
  10650. ' c: currency;',
  10651. 'begin',
  10652. ' Say([]);',
  10653. ' Say([1]);',
  10654. ' Say([''c'',''foo'',nil,true,1.3,p,j,c]);',
  10655. '']);
  10656. ConvertProgram;
  10657. CheckSource('TestArrayOfConst_PassBaseTypes',
  10658. LinesToStr([ // statements
  10659. 'this.Say = function (args) {',
  10660. ' $mod.Say(args);',
  10661. '};',
  10662. 'this.p = null;',
  10663. 'this.j = undefined;',
  10664. 'this.c = 0;',
  10665. '']),
  10666. LinesToStr([ // $mod.$main
  10667. '$mod.Say([]);',
  10668. '$mod.Say(pas.system.VarRecs(0, 1));',
  10669. '$mod.Say(pas.system.VarRecs(',
  10670. ' 9,',
  10671. ' "c",',
  10672. ' 18,',
  10673. ' "foo",',
  10674. ' 5,',
  10675. ' null,',
  10676. ' 1,',
  10677. ' true,',
  10678. ' 3,',
  10679. ' 1.3,',
  10680. ' 5,',
  10681. ' $mod.p,',
  10682. ' 20,',
  10683. ' $mod.j,',
  10684. ' 12,',
  10685. ' $mod.c',
  10686. ' ));',
  10687. '']));
  10688. end;
  10689. procedure TTestModule.TestArrayOfConst_PassObj;
  10690. begin
  10691. StartProgram(true,[supTVarRec]);
  10692. Add([
  10693. '{$interfaces corba}',
  10694. 'type',
  10695. ' TObject = class',
  10696. ' end;',
  10697. ' TClass = class of TObject;',
  10698. ' IUnknown = interface',
  10699. ' end;',
  10700. 'procedure Say(args: array of const);',
  10701. 'begin',
  10702. 'end;',
  10703. 'var',
  10704. ' o: TObject;',
  10705. ' c: TClass;',
  10706. ' i: IUnknown;',
  10707. 'begin',
  10708. ' Say([o,c,TObject]);',
  10709. ' Say([nil,i]);',
  10710. '']);
  10711. ConvertProgram;
  10712. CheckSource('TestArrayOfConst_PassObj',
  10713. LinesToStr([ // statements
  10714. 'rtl.createClass(this, "TObject", null, function () {',
  10715. ' this.$init = function () {',
  10716. ' };',
  10717. ' this.$final = function () {',
  10718. ' };',
  10719. '});',
  10720. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  10721. 'this.Say = function (args) {',
  10722. '};',
  10723. 'this.o = null;',
  10724. 'this.c = null;',
  10725. 'this.i = null;',
  10726. '']),
  10727. LinesToStr([ // $mod.$main
  10728. '$mod.Say(pas.system.VarRecs(',
  10729. ' 7,',
  10730. ' $mod.o,',
  10731. ' 8,',
  10732. ' $mod.c,',
  10733. ' 8,',
  10734. ' $mod.TObject',
  10735. '));',
  10736. '$mod.Say(pas.system.VarRecs(5, null, 14, $mod.i));',
  10737. '']));
  10738. end;
  10739. procedure TTestModule.TestRecord_Empty;
  10740. begin
  10741. StartProgram(false);
  10742. Add([
  10743. 'type',
  10744. ' TRecA = record',
  10745. ' end;',
  10746. 'var a,b: TRecA;',
  10747. 'begin',
  10748. ' if a=b then ;']);
  10749. ConvertProgram;
  10750. CheckSource('TestRecord_Empty',
  10751. LinesToStr([ // statements
  10752. 'rtl.recNewT(this, "TRecA", function () {',
  10753. ' this.$eq = function (b) {',
  10754. ' return true;',
  10755. ' };',
  10756. ' this.$assign = function (s) {',
  10757. ' return this;',
  10758. ' };',
  10759. '});',
  10760. 'this.a = this.TRecA.$new();',
  10761. 'this.b = this.TRecA.$new();',
  10762. '']),
  10763. LinesToStr([ // $mod.$main
  10764. 'if ($mod.a.$eq($mod.b)) ;'
  10765. ]));
  10766. end;
  10767. procedure TTestModule.TestRecord_Var;
  10768. begin
  10769. StartProgram(false);
  10770. Add('type');
  10771. Add(' TRecA = record');
  10772. Add(' Bold: longint;');
  10773. Add(' end;');
  10774. Add('var Rec: TRecA;');
  10775. Add('begin');
  10776. Add(' rec.bold:=123');
  10777. ConvertProgram;
  10778. CheckSource('TestRecord_Var',
  10779. LinesToStr([ // statements
  10780. 'rtl.recNewT(this, "TRecA", function () {',
  10781. ' this.Bold = 0;',
  10782. ' this.$eq = function (b) {',
  10783. ' return this.Bold === b.Bold;',
  10784. ' };',
  10785. ' this.$assign = function (s) {',
  10786. ' this.Bold = s.Bold;',
  10787. ' return this;',
  10788. ' };',
  10789. '});',
  10790. 'this.Rec = this.TRecA.$new();',
  10791. '']),
  10792. LinesToStr([ // $mod.$main
  10793. '$mod.Rec.Bold = 123;'
  10794. ]));
  10795. end;
  10796. procedure TTestModule.TestRecord_VarExternal;
  10797. begin
  10798. StartProgram(false);
  10799. Add([
  10800. '{$modeswitch externalclass}',
  10801. 'type',
  10802. ' TRecA = record',
  10803. ' i: byte;',
  10804. ' length_: longint external name ''length'';',
  10805. ' end;',
  10806. 'var Rec: TRecA;',
  10807. 'begin',
  10808. ' rec.length_ := rec.length_',
  10809. '']);
  10810. ConvertProgram;
  10811. CheckSource('TestRecord_VarExternal',
  10812. LinesToStr([ // statements
  10813. 'rtl.recNewT(this, "TRecA", function () {',
  10814. ' this.i = 0;',
  10815. ' this.$eq = function (b) {',
  10816. ' return (this.i === b.i) && (this.length === b.length);',
  10817. ' };',
  10818. ' this.$assign = function (s) {',
  10819. ' this.i = s.i;',
  10820. ' this.length = s.length;',
  10821. ' return this;',
  10822. ' };',
  10823. '});',
  10824. 'this.Rec = this.TRecA.$new();',
  10825. '']),
  10826. LinesToStr([ // $mod.$main
  10827. '$mod.Rec.length = $mod.Rec.length;'
  10828. ]));
  10829. end;
  10830. procedure TTestModule.TestRecord_WithDo;
  10831. begin
  10832. StartProgram(false);
  10833. Add('type');
  10834. Add(' TRec = record');
  10835. Add(' vI: longint;');
  10836. Add(' end;');
  10837. Add('var');
  10838. Add(' Int: longint;');
  10839. Add(' r: TRec;');
  10840. Add('begin');
  10841. Add(' with r do');
  10842. Add(' int:=vi;');
  10843. Add(' with r do begin');
  10844. Add(' int:=vi;');
  10845. Add(' vi:=int;');
  10846. Add(' end;');
  10847. ConvertProgram;
  10848. CheckSource('TestWithRecordDo',
  10849. LinesToStr([ // statements
  10850. 'rtl.recNewT(this, "TRec", function () {',
  10851. ' this.vI = 0;',
  10852. ' this.$eq = function (b) {',
  10853. ' return this.vI === b.vI;',
  10854. ' };',
  10855. ' this.$assign = function (s) {',
  10856. ' this.vI = s.vI;',
  10857. ' return this;',
  10858. ' };',
  10859. '});',
  10860. 'this.Int = 0;',
  10861. 'this.r = this.TRec.$new();',
  10862. '']),
  10863. LinesToStr([ // $mod.$main
  10864. 'var $with = $mod.r;',
  10865. '$mod.Int = $with.vI;',
  10866. 'var $with1 = $mod.r;',
  10867. '$mod.Int = $with1.vI;',
  10868. '$with1.vI = $mod.Int;'
  10869. ]));
  10870. end;
  10871. procedure TTestModule.TestRecord_Assign;
  10872. begin
  10873. StartProgram(false);
  10874. Add([
  10875. 'type',
  10876. ' TEnum = (red,green);',
  10877. ' TEnums = set of TEnum;',
  10878. ' TSmallRec = record',
  10879. ' N: longint;',
  10880. ' end;',
  10881. ' TBigRec = record',
  10882. ' Int: longint;',
  10883. ' D: double;',
  10884. ' Arr: array of longint;',
  10885. ' Arr2: array[1..2] of longint;',
  10886. ' Small: TSmallRec;',
  10887. ' Enums: TEnums;',
  10888. ' end;',
  10889. 'var',
  10890. ' r, s: TBigRec;',
  10891. 'begin',
  10892. ' r:=s;',
  10893. ' r:=default(TBigRec);',
  10894. ' r:=default(s);',
  10895. '']);
  10896. ConvertProgram;
  10897. CheckSource('TestRecord_Assign',
  10898. LinesToStr([ // statements
  10899. 'this.TEnum = {',
  10900. ' "0": "red",',
  10901. ' red: 0,',
  10902. ' "1": "green",',
  10903. ' green: 1',
  10904. '};',
  10905. 'rtl.recNewT(this, "TSmallRec", function () {',
  10906. ' this.N = 0;',
  10907. ' this.$eq = function (b) {',
  10908. ' return this.N === b.N;',
  10909. ' };',
  10910. ' this.$assign = function (s) {',
  10911. ' this.N = s.N;',
  10912. ' return this;',
  10913. ' };',
  10914. '});',
  10915. 'rtl.recNewT(this, "TBigRec", function () {',
  10916. ' this.Int = 0;',
  10917. ' this.D = 0.0;',
  10918. ' this.$new = function () {',
  10919. ' var r = Object.create(this);',
  10920. ' r.Arr = [];',
  10921. ' r.Arr2 = rtl.arraySetLength(null, 0, 2);',
  10922. ' r.Small = $mod.TSmallRec.$new();',
  10923. ' r.Enums = {};',
  10924. ' return r;',
  10925. ' };',
  10926. ' this.$eq = function (b) {',
  10927. ' 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);',
  10928. ' };',
  10929. ' this.$assign = function (s) {',
  10930. ' this.Int = s.Int;',
  10931. ' this.D = s.D;',
  10932. ' this.Arr = rtl.arrayRef(s.Arr);',
  10933. ' this.Arr2 = s.Arr2.slice(0);',
  10934. ' this.Small.$assign(s.Small);',
  10935. ' this.Enums = rtl.refSet(s.Enums);',
  10936. ' return this;',
  10937. ' };',
  10938. '});',
  10939. 'this.r = this.TBigRec.$new();',
  10940. 'this.s = this.TBigRec.$new();',
  10941. '']),
  10942. LinesToStr([ // $mod.$main
  10943. '$mod.r.$assign($mod.s);',
  10944. '$mod.r.$assign($mod.TBigRec.$new());',
  10945. '$mod.r.$assign($mod.TBigRec.$new());',
  10946. '']));
  10947. end;
  10948. procedure TTestModule.TestRecord_AsParams;
  10949. begin
  10950. StartProgram(false);
  10951. Add([
  10952. 'type',
  10953. ' integer = longint;',
  10954. ' TRecord = record',
  10955. ' i: integer;',
  10956. ' end;',
  10957. 'procedure DoIt(vD: TRecord; const vC: TRecord; var vV: TRecord; var U);',
  10958. 'var vL: TRecord;',
  10959. 'begin',
  10960. ' vd:=vd;',
  10961. ' vd.i:=vd.i;',
  10962. ' vl:=vc;',
  10963. ' vv:=vv;',
  10964. ' vv.i:=vv.i;',
  10965. ' U:=vl;',
  10966. ' U:=vd;',
  10967. ' U:=vc;',
  10968. ' U:=vv;',
  10969. ' vl:=TRecord(U);',
  10970. ' vd:=TRecord(U);',
  10971. ' vv:=TRecord(U);',
  10972. ' doit(vd,vd,vd,vd);',
  10973. ' doit(vc,vc,vl,vl);',
  10974. ' doit(vv,vv,vv,vv);',
  10975. ' doit(vl,vl,vl,vl);',
  10976. ' TRecord(U).i:=3;',
  10977. 'end;',
  10978. 'var i: TRecord;',
  10979. 'begin',
  10980. ' doit(i,i,i,i);',
  10981. '']);
  10982. ConvertProgram;
  10983. CheckSource('TestRecord_AsParams',
  10984. LinesToStr([ // statements
  10985. 'rtl.recNewT(this, "TRecord", function () {',
  10986. ' this.i = 0;',
  10987. ' this.$eq = function (b) {',
  10988. ' return this.i === b.i;',
  10989. ' };',
  10990. ' this.$assign = function (s) {',
  10991. ' this.i = s.i;',
  10992. ' return this;',
  10993. ' };',
  10994. '});',
  10995. 'this.DoIt = function (vD, vC, vV, U) {',
  10996. ' var vL = $mod.TRecord.$new();',
  10997. ' vD.$assign(vD);',
  10998. ' vD.i = vD.i;',
  10999. ' vL.$assign(vC);',
  11000. ' vV.$assign(vV);',
  11001. ' vV.i = vV.i;',
  11002. ' U.$assign(vL);',
  11003. ' U.$assign(vD);',
  11004. ' U.$assign(vC);',
  11005. ' U.$assign(vV);',
  11006. ' vL.$assign(U);',
  11007. ' vD.$assign(U);',
  11008. ' vV.$assign(U);',
  11009. ' $mod.DoIt($mod.TRecord.$clone(vD), vD, vD, vD);',
  11010. ' $mod.DoIt($mod.TRecord.$clone(vC), vC, vL, vL);',
  11011. ' $mod.DoIt($mod.TRecord.$clone(vV), vV, vV, vV);',
  11012. ' $mod.DoIt($mod.TRecord.$clone(vL), vL, vL, vL);',
  11013. ' U.i = 3;',
  11014. '};',
  11015. 'this.i = this.TRecord.$new();'
  11016. ]),
  11017. LinesToStr([
  11018. '$mod.DoIt($mod.TRecord.$clone($mod.i), $mod.i, $mod.i, $mod.i);',
  11019. '']));
  11020. end;
  11021. procedure TTestModule.TestRecord_ConstRef;
  11022. begin
  11023. StartProgram(false);
  11024. Add([
  11025. 'type TRec = record i: word; end;',
  11026. 'procedure Run(constref a: TRec);',
  11027. 'begin',
  11028. 'end;',
  11029. 'procedure Fly(a: TRec; var b: TRec; out c: TRec; const d: TRec; constref e: TRec);',
  11030. 'var l: TRec;',
  11031. 'begin',
  11032. ' Run(l);',
  11033. ' Run(a);',
  11034. ' Run(b);',
  11035. ' Run(c);',
  11036. ' Run(d);',
  11037. ' Run(e);',
  11038. 'end;',
  11039. 'begin',
  11040. '']);
  11041. ConvertProgram;
  11042. CheckResolverUnexpectedHints();
  11043. CheckSource('TestRecord_ConstRef',
  11044. LinesToStr([ // statements
  11045. 'rtl.recNewT(this, "TRec", function () {',
  11046. ' this.i = 0;',
  11047. ' this.$eq = function (b) {',
  11048. ' return this.i === b.i;',
  11049. ' };',
  11050. ' this.$assign = function (s) {',
  11051. ' this.i = s.i;',
  11052. ' return this;',
  11053. ' };',
  11054. '});',
  11055. 'this.Run = function (a) {',
  11056. '};',
  11057. 'this.Fly = function (a, b, c, d, e) {',
  11058. ' var l = $mod.TRec.$new();',
  11059. ' $mod.Run(l);',
  11060. ' $mod.Run(a);',
  11061. ' $mod.Run(b);',
  11062. ' $mod.Run(c);',
  11063. ' $mod.Run(d);',
  11064. ' $mod.Run(e);',
  11065. '};',
  11066. '']),
  11067. LinesToStr([
  11068. '']));
  11069. end;
  11070. procedure TTestModule.TestRecordElement_AsParams;
  11071. begin
  11072. StartProgram(false);
  11073. Add('type');
  11074. Add(' integer = longint;');
  11075. Add(' TRecord = record');
  11076. Add(' i: integer;');
  11077. Add(' end;');
  11078. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  11079. Add('var vJ: TRecord;');
  11080. Add('begin');
  11081. Add(' doit(vj.i,vj.i,vj.i);');
  11082. Add('end;');
  11083. Add('var r: TRecord;');
  11084. Add('begin');
  11085. Add(' doit(r.i,r.i,r.i);');
  11086. ConvertProgram;
  11087. CheckSource('TestRecordElement_AsParams',
  11088. LinesToStr([ // statements
  11089. 'rtl.recNewT(this, "TRecord", function () {',
  11090. ' this.i = 0;',
  11091. ' this.$eq = function (b) {',
  11092. ' return this.i === b.i;',
  11093. ' };',
  11094. ' this.$assign = function (s) {',
  11095. ' this.i = s.i;',
  11096. ' return this;',
  11097. ' };',
  11098. '});',
  11099. 'this.DoIt = function (vG,vH,vI) {',
  11100. ' var vJ = $mod.TRecord.$new();',
  11101. ' $mod.DoIt(vJ.i, vJ.i, {',
  11102. ' p: vJ,',
  11103. ' get: function () {',
  11104. ' return this.p.i;',
  11105. ' },',
  11106. ' set: function (v) {',
  11107. ' this.p.i = v;',
  11108. ' }',
  11109. ' });',
  11110. '};',
  11111. 'this.r = this.TRecord.$new();'
  11112. ]),
  11113. LinesToStr([
  11114. '$mod.DoIt($mod.r.i,$mod.r.i,{',
  11115. ' p: $mod.r,',
  11116. ' get: function () {',
  11117. ' return this.p.i;',
  11118. ' },',
  11119. ' set: function (v) {',
  11120. ' this.p.i = v;',
  11121. ' }',
  11122. '});'
  11123. ]));
  11124. end;
  11125. procedure TTestModule.TestRecordElementFromFuncResult_AsParams;
  11126. begin
  11127. StartProgram(false);
  11128. Add('type');
  11129. Add(' integer = longint;');
  11130. Add(' TRecord = record');
  11131. Add(' i: integer;');
  11132. Add(' end;');
  11133. Add('function GetRec(vB: integer = 0): TRecord;');
  11134. Add('begin');
  11135. Add('end;');
  11136. Add('procedure DoIt(vG: integer; const vH: integer);');
  11137. Add('begin');
  11138. Add('end;');
  11139. Add('begin');
  11140. Add(' doit(getrec.i,getrec.i);');
  11141. Add(' doit(getrec().i,getrec().i);');
  11142. Add(' doit(getrec(1).i,getrec(2).i);');
  11143. ConvertProgram;
  11144. CheckSource('TestRecordElementFromFuncResult_AsParams',
  11145. LinesToStr([ // statements
  11146. 'rtl.recNewT(this, "TRecord", function () {',
  11147. ' this.i = 0;',
  11148. ' this.$eq = function (b) {',
  11149. ' return this.i === b.i;',
  11150. ' };',
  11151. ' this.$assign = function (s) {',
  11152. ' this.i = s.i;',
  11153. ' return this;',
  11154. ' };',
  11155. '});',
  11156. 'this.GetRec = function (vB) {',
  11157. ' var Result = $mod.TRecord.$new();',
  11158. ' return Result;',
  11159. '};',
  11160. 'this.DoIt = function (vG, vH) {',
  11161. '};',
  11162. '']),
  11163. LinesToStr([
  11164. '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
  11165. '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
  11166. '$mod.DoIt($mod.GetRec(1).i,$mod.GetRec(2).i);',
  11167. '']));
  11168. end;
  11169. procedure TTestModule.TestRecordElementFromWith_AsParams;
  11170. begin
  11171. StartProgram(false);
  11172. Add('type');
  11173. Add(' integer = longint;');
  11174. Add(' TRecord = record');
  11175. Add(' i: integer;');
  11176. Add(' end;');
  11177. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  11178. Add('begin');
  11179. Add('end;');
  11180. Add('var r: trecord;');
  11181. Add('begin');
  11182. Add(' with r do ');
  11183. Add(' doit(i,i,i);');
  11184. ConvertProgram;
  11185. CheckSource('TestRecordElementFromWith_AsParams',
  11186. LinesToStr([ // statements
  11187. 'rtl.recNewT(this, "TRecord", function () {',
  11188. ' this.i = 0;',
  11189. ' this.$eq = function (b) {',
  11190. ' return this.i === b.i;',
  11191. ' };',
  11192. ' this.$assign = function (s) {',
  11193. ' this.i = s.i;',
  11194. ' return this;',
  11195. ' };',
  11196. '});',
  11197. 'this.DoIt = function (vG,vH,vI) {',
  11198. '};',
  11199. 'this.r = this.TRecord.$new();'
  11200. ]),
  11201. LinesToStr([
  11202. 'var $with = $mod.r;',
  11203. '$mod.DoIt($with.i,$with.i,{',
  11204. ' p: $with,',
  11205. ' get: function () {',
  11206. ' return this.p.i;',
  11207. ' },',
  11208. ' set: function (v) {',
  11209. ' this.p.i = v;',
  11210. ' }',
  11211. '});',
  11212. '']));
  11213. end;
  11214. procedure TTestModule.TestRecord_Equal;
  11215. begin
  11216. StartProgram(false);
  11217. Add('type');
  11218. Add(' integer = longint;');
  11219. Add(' TFlag = (red,blue);');
  11220. Add(' TFlags = set of TFlag;');
  11221. Add(' TProc = procedure;');
  11222. Add(' TRecord = record');
  11223. Add(' i: integer;');
  11224. Add(' Event: TProc;');
  11225. Add(' f: TFlags;');
  11226. Add(' end;');
  11227. Add(' TNested = record');
  11228. Add(' r: TRecord;');
  11229. Add(' end;');
  11230. Add('var');
  11231. Add(' b: boolean;');
  11232. Add(' r,s: trecord;');
  11233. Add('begin');
  11234. Add(' b:=r=s;');
  11235. Add(' b:=r<>s;');
  11236. ConvertProgram;
  11237. CheckSource('TestRecord_Equal',
  11238. LinesToStr([ // statements
  11239. 'this.TFlag = {',
  11240. ' "0": "red",',
  11241. ' red: 0,',
  11242. ' "1": "blue",',
  11243. ' blue: 1',
  11244. '};',
  11245. 'rtl.recNewT(this, "TRecord", function () {',
  11246. ' this.i = 0;',
  11247. ' this.Event = null;',
  11248. ' this.$new = function () {',
  11249. ' var r = Object.create(this);',
  11250. ' r.f = {};',
  11251. ' return r;',
  11252. ' };',
  11253. ' this.$eq = function (b) {',
  11254. ' return (this.i === b.i) && rtl.eqCallback(this.Event, b.Event) && rtl.eqSet(this.f, b.f);',
  11255. ' };',
  11256. ' this.$assign = function (s) {',
  11257. ' this.i = s.i;',
  11258. ' this.Event = s.Event;',
  11259. ' this.f = rtl.refSet(s.f);',
  11260. ' return this;',
  11261. ' };',
  11262. '});',
  11263. 'rtl.recNewT(this, "TNested", function () {',
  11264. ' this.$new = function () {',
  11265. ' var r = Object.create(this);',
  11266. ' r.r = $mod.TRecord.$new();',
  11267. ' return r;',
  11268. ' };',
  11269. ' this.$eq = function (b) {',
  11270. ' return this.r.$eq(b.r);',
  11271. ' };',
  11272. ' this.$assign = function (s) {',
  11273. ' this.r.$assign(s.r);',
  11274. ' return this;',
  11275. ' };',
  11276. '});',
  11277. 'this.b = false;',
  11278. 'this.r = this.TRecord.$new();',
  11279. 'this.s = this.TRecord.$new();',
  11280. '']),
  11281. LinesToStr([
  11282. '$mod.b = $mod.r.$eq($mod.s);',
  11283. '$mod.b = !$mod.r.$eq($mod.s);',
  11284. '']));
  11285. end;
  11286. procedure TTestModule.TestRecord_JSValue;
  11287. begin
  11288. StartProgram(false);
  11289. Add([
  11290. 'type',
  11291. ' TRecord = record',
  11292. ' i: longint;',
  11293. ' end;',
  11294. 'procedure Fly(d: jsvalue; const c: jsvalue);',
  11295. 'begin',
  11296. 'end;',
  11297. 'procedure Run(d: TRecord; const c: TRecord; var v: TRecord);',
  11298. 'begin',
  11299. ' if jsvalue(d) then ;',
  11300. ' if jsvalue(c) then ;',
  11301. ' if jsvalue(v) then ;',
  11302. 'end;',
  11303. 'var',
  11304. ' Jv: jsvalue;',
  11305. ' Rec: trecord;',
  11306. 'begin',
  11307. ' rec:=trecord(jv);',
  11308. ' jv:=rec;',
  11309. ' Fly(rec,rec);',
  11310. ' Fly(@rec,@rec);',
  11311. ' if jsvalue(Rec) then ;',
  11312. ' Run(trecord(jv),trecord(jv),rec);',
  11313. '']);
  11314. ConvertProgram;
  11315. CheckSource('TestRecord_JSValue',
  11316. LinesToStr([ // statements
  11317. 'rtl.recNewT(this, "TRecord", function () {',
  11318. ' this.i = 0;',
  11319. ' this.$eq = function (b) {',
  11320. ' return this.i === b.i;',
  11321. ' };',
  11322. ' this.$assign = function (s) {',
  11323. ' this.i = s.i;',
  11324. ' return this;',
  11325. ' };',
  11326. '});',
  11327. 'this.Fly = function (d, c) {',
  11328. '};',
  11329. 'this.Run = function (d, c, v) {',
  11330. ' if (d) ;',
  11331. ' if (c) ;',
  11332. ' if (v) ;',
  11333. '};',
  11334. 'this.Jv = undefined;',
  11335. 'this.Rec = this.TRecord.$new();',
  11336. '']),
  11337. LinesToStr([
  11338. '$mod.Rec.$assign(rtl.getObject($mod.Jv));',
  11339. '$mod.Jv = $mod.Rec;',
  11340. '$mod.Fly($mod.TRecord.$clone($mod.Rec), $mod.Rec);',
  11341. '$mod.Fly($mod.Rec, $mod.Rec);',
  11342. 'if ($mod.Rec) ;',
  11343. '$mod.Run($mod.TRecord.$clone(rtl.getObject($mod.Jv)), rtl.getObject($mod.Jv), $mod.Rec);',
  11344. '']));
  11345. end;
  11346. procedure TTestModule.TestRecord_VariantFail;
  11347. begin
  11348. StartProgram(false);
  11349. Add([
  11350. 'type',
  11351. ' TRec = record',
  11352. ' case word of',
  11353. ' 0: (b0, b1: Byte);',
  11354. ' 1: (i: word);',
  11355. ' end;',
  11356. 'begin']);
  11357. SetExpectedPasResolverError('variant record is not supported',
  11358. nXIsNotSupported);
  11359. ConvertProgram;
  11360. end;
  11361. procedure TTestModule.TestRecord_FieldArray;
  11362. begin
  11363. StartProgram(false);
  11364. Add([
  11365. 'type',
  11366. ' TArrInt = array[3..4] of longint;',
  11367. ' TArrArrInt = array[3..4] of longint;',
  11368. ' TRec = record',
  11369. ' a: array of longint;',
  11370. ' s: array[1..2] of longint;',
  11371. ' m: array[1..2,3..4] of longint;',
  11372. ' o: TArrArrInt;',
  11373. ' end;',
  11374. 'begin']);
  11375. ConvertProgram;
  11376. CheckSource('TestRecord_FieldArray',
  11377. LinesToStr([ // statements
  11378. 'rtl.recNewT(this, "TRec", function () {',
  11379. ' this.$new = function () {',
  11380. ' var r = Object.create(this);',
  11381. ' r.a = [];',
  11382. ' r.s = rtl.arraySetLength(null, 0, 2);',
  11383. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  11384. ' r.o = rtl.arraySetLength(null, 0, 2);',
  11385. ' return r;',
  11386. ' };',
  11387. ' this.$eq = function (b) {',
  11388. ' return (this.a === b.a) && rtl.arrayEq(this.s, b.s) && rtl.arrayEq(this.m, b.m) && rtl.arrayEq(this.o, b.o);',
  11389. ' };',
  11390. ' this.$assign = function (s) {',
  11391. ' this.a = rtl.arrayRef(s.a);',
  11392. ' this.s = s.s.slice(0);',
  11393. ' this.m = s.m.slice(0);',
  11394. ' this.o = s.o.slice(0);',
  11395. ' return this;',
  11396. ' };',
  11397. '});',
  11398. '']),
  11399. LinesToStr([ // $mod.$main
  11400. '']));
  11401. end;
  11402. procedure TTestModule.TestRecord_Const;
  11403. begin
  11404. StartProgram(false);
  11405. Add([
  11406. 'type',
  11407. ' TArrInt = array[3..4] of longint;',
  11408. ' TPoint = record x,y: longint; end;',
  11409. ' TRec = record',
  11410. ' i: longint;',
  11411. ' a: array of longint;',
  11412. ' s: array[1..2] of longint;',
  11413. ' m: array[1..2,3..4] of longint;',
  11414. ' p: TPoint;',
  11415. ' end;',
  11416. ' TPoints = array of TPoint;',
  11417. 'const',
  11418. ' r: TRec = (',
  11419. ' i:1;',
  11420. ' a:(2,3);',
  11421. ' s:(4,5);',
  11422. ' m:( (11,12), (13,14) );',
  11423. ' p: (x:21; y:22)',
  11424. ' );',
  11425. ' p: TPoints = ( (x:1;y:2), (x:3;y:4) );',
  11426. 'begin']);
  11427. ConvertProgram;
  11428. CheckSource('TestRecord_Const',
  11429. LinesToStr([ // statements
  11430. 'rtl.recNewT(this, "TPoint", function () {',
  11431. ' this.x = 0;',
  11432. ' this.y = 0;',
  11433. ' this.$eq = function (b) {',
  11434. ' return (this.x === b.x) && (this.y === b.y);',
  11435. ' };',
  11436. ' this.$assign = function (s) {',
  11437. ' this.x = s.x;',
  11438. ' this.y = s.y;',
  11439. ' return this;',
  11440. ' };',
  11441. '});',
  11442. 'rtl.recNewT(this, "TRec", function () {',
  11443. ' this.i = 0;',
  11444. ' this.$new = function () {',
  11445. ' var r = Object.create(this);',
  11446. ' r.a = [];',
  11447. ' r.s = rtl.arraySetLength(null, 0, 2);',
  11448. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  11449. ' r.p = $mod.TPoint.$new();',
  11450. ' return r;',
  11451. ' };',
  11452. ' this.$eq = function (b) {',
  11453. ' 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);',
  11454. ' };',
  11455. ' this.$assign = function (s) {',
  11456. ' this.i = s.i;',
  11457. ' this.a = rtl.arrayRef(s.a);',
  11458. ' this.s = s.s.slice(0);',
  11459. ' this.m = s.m.slice(0);',
  11460. ' this.p.$assign(s.p);',
  11461. ' return this;',
  11462. ' };',
  11463. '});',
  11464. 'this.r = this.TRec.$clone({',
  11465. ' i: 1,',
  11466. ' a: [2, 3],',
  11467. ' s: [4, 5],',
  11468. ' m: [[11, 12], [13, 14]],',
  11469. ' p: this.TPoint.$clone({',
  11470. ' x: 21,',
  11471. ' y: 22',
  11472. ' })',
  11473. '});',
  11474. 'this.p = [this.TPoint.$clone({',
  11475. ' x: 1,',
  11476. ' y: 2',
  11477. '}), this.TPoint.$clone({',
  11478. ' x: 3,',
  11479. ' y: 4',
  11480. '})];',
  11481. '']),
  11482. LinesToStr([ // $mod.$main
  11483. '']));
  11484. end;
  11485. procedure TTestModule.TestRecord_TypecastFail;
  11486. begin
  11487. StartProgram(false);
  11488. Add([
  11489. 'type',
  11490. ' TPoint = record x,y: longint; end;',
  11491. ' TRec = record l: longint end;',
  11492. 'var p: TPoint;',
  11493. 'begin',
  11494. ' if TRec(p).l=2 then ;']);
  11495. SetExpectedPasResolverError('Illegal type conversion: "TPoint" to "record TRec"',
  11496. nIllegalTypeConversionTo);
  11497. ConvertProgram;
  11498. end;
  11499. procedure TTestModule.TestRecord_InFunction;
  11500. begin
  11501. StartProgram(false);
  11502. Add([
  11503. 'var TPoint: longint = 3;',
  11504. 'procedure DoIt;',
  11505. 'type',
  11506. ' TPoint = record x,y: longint; end;',
  11507. ' TPoints = array of TPoint;',
  11508. 'var',
  11509. ' r: TPoint;',
  11510. ' p: TPoints;',
  11511. 'begin',
  11512. ' SetLength(p,2);',
  11513. 'end;',
  11514. 'begin']);
  11515. ConvertProgram;
  11516. CheckSource('TestRecord_InFunction',
  11517. LinesToStr([ // statements
  11518. 'this.TPoint = 3;',
  11519. 'var TPoint$1 = rtl.recNewT(null, "", function () {',
  11520. ' this.x = 0;',
  11521. ' this.y = 0;',
  11522. ' this.$eq = function (b) {',
  11523. ' return (this.x === b.x) && (this.y === b.y);',
  11524. ' };',
  11525. ' this.$assign = function (s) {',
  11526. ' this.x = s.x;',
  11527. ' this.y = s.y;',
  11528. ' return this;',
  11529. ' };',
  11530. '});',
  11531. 'this.DoIt = function () {',
  11532. ' var r = TPoint$1.$new();',
  11533. ' var p = [];',
  11534. ' p = rtl.arraySetLength(p, TPoint$1, 2);',
  11535. '};',
  11536. '']),
  11537. LinesToStr([ // $mod.$main
  11538. '']));
  11539. end;
  11540. procedure TTestModule.TestRecord_AnonymousFail;
  11541. begin
  11542. StartProgram(false);
  11543. Add([
  11544. 'var',
  11545. ' r: record x: word end;',
  11546. 'begin']);
  11547. SetExpectedPasResolverError('not yet implemented: :TPasRecordType [20190408224556] "anonymous record type"',
  11548. nNotYetImplemented);
  11549. ConvertProgram;
  11550. end;
  11551. procedure TTestModule.TestAdvRecord_Function;
  11552. begin
  11553. StartProgram(false);
  11554. Parser.Options:=Parser.Options+[po_cassignments];
  11555. Add([
  11556. '{$modeswitch AdvancedRecords}',
  11557. 'type',
  11558. ' TPoint = record',
  11559. ' x,y: word;',
  11560. ' function Add(const apt: TPoint): TPoint;',
  11561. ' end;',
  11562. 'function TPoint.Add(const apt: TPoint): TPoint;',
  11563. 'begin',
  11564. ' Result:=Self;',
  11565. ' Result.x+=apt.x;',
  11566. ' Result.y:=Result.y+apt.y;',
  11567. ' Self:=apt;',
  11568. 'end;',
  11569. 'var p,q: TPoint;',
  11570. 'begin',
  11571. ' p.add(q);',
  11572. ' p:=default(TPoint);',
  11573. ' p:=q;',
  11574. '']);
  11575. ConvertProgram;
  11576. CheckSource('TestAdvRecord_Function',
  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.Add = function (apt) {',
  11590. ' var Result = $mod.TPoint.$new();',
  11591. ' Result.$assign(this);',
  11592. ' Result.x += apt.x;',
  11593. ' Result.y = Result.y + apt.y;',
  11594. ' this.$assign(apt);',
  11595. ' return Result;',
  11596. ' };',
  11597. '});',
  11598. 'this.p = this.TPoint.$new();',
  11599. 'this.q = this.TPoint.$new();',
  11600. '']),
  11601. LinesToStr([ // $mod.$main
  11602. '$mod.p.Add($mod.q);',
  11603. '$mod.p.$assign($mod.TPoint.$new());',
  11604. '$mod.p.$assign($mod.q);',
  11605. '']));
  11606. end;
  11607. procedure TTestModule.TestAdvRecord_Property;
  11608. begin
  11609. StartProgram(false);
  11610. Add([
  11611. '{$modeswitch AdvancedRecords}',
  11612. 'type',
  11613. ' TPoint = record',
  11614. ' x,y: word;',
  11615. ' strict private',
  11616. ' function GetSize: longword;',
  11617. ' procedure SetSize(Value: longword);',
  11618. ' public',
  11619. ' property Size: longword read GetSize write SetSize;',
  11620. ' property Left: word read x write y;',
  11621. ' end;',
  11622. 'procedure SetSize(Value: longword); begin end;',// check auto rename
  11623. 'function TPoint.GetSize: longword;',
  11624. 'begin',
  11625. ' x:=y;',
  11626. ' Size:=Size;',
  11627. ' Left:=Left;',
  11628. 'end;',
  11629. 'procedure TPoint.SetSize(Value: longword);',
  11630. 'begin',
  11631. 'end;',
  11632. 'var p,q: TPoint;',
  11633. 'begin',
  11634. ' p.Size:=q.Size;',
  11635. ' p.Left:=q.Left;',
  11636. '']);
  11637. ConvertProgram;
  11638. CheckSource('TestAdvRecord_Property',
  11639. LinesToStr([ // statements
  11640. 'rtl.recNewT(this, "TPoint", function () {',
  11641. ' this.x = 0;',
  11642. ' this.y = 0;',
  11643. ' this.$eq = function (b) {',
  11644. ' return (this.x === b.x) && (this.y === b.y);',
  11645. ' };',
  11646. ' this.$assign = function (s) {',
  11647. ' this.x = s.x;',
  11648. ' this.y = s.y;',
  11649. ' return this;',
  11650. ' };',
  11651. ' this.GetSize = function () {',
  11652. ' var Result = 0;',
  11653. ' this.x = this.y;',
  11654. ' this.SetSize(this.GetSize());',
  11655. ' this.y = this.x;',
  11656. ' return Result;',
  11657. ' };',
  11658. ' this.SetSize = function (Value) {',
  11659. ' };',
  11660. '});',
  11661. 'this.SetSize = function (Value) {',
  11662. '};',
  11663. 'this.p = this.TPoint.$new();',
  11664. 'this.q = this.TPoint.$new();',
  11665. '']),
  11666. LinesToStr([ // $mod.$main
  11667. '$mod.p.SetSize($mod.q.GetSize());',
  11668. '$mod.p.y = $mod.q.x;',
  11669. '']));
  11670. end;
  11671. procedure TTestModule.TestAdvRecord_PropertyDefault;
  11672. begin
  11673. StartProgram(false);
  11674. Add([
  11675. '{$modeswitch AdvancedRecords}',
  11676. 'type',
  11677. ' TPoint = record',
  11678. ' strict private',
  11679. ' function GetItems(Index: word): word;',
  11680. ' procedure SetItems(Index: word; Value: word);',
  11681. ' public',
  11682. ' property Items[Index: word]: word read GetItems write SetItems; default;',
  11683. ' end;',
  11684. 'function TPoint.GetItems(Index: word): word;',
  11685. 'begin',
  11686. ' Items[index]:=Items[index];',
  11687. ' self.Items[index]:=self.Items[index];',
  11688. 'end;',
  11689. 'procedure TPoint.SetItems(Index: word; Value: word);',
  11690. 'begin',
  11691. 'end;',
  11692. 'var p: TPoint;',
  11693. 'begin',
  11694. ' p[1]:=p[2];',
  11695. ' p.Items[3]:=p.Items[4];',
  11696. '']);
  11697. ConvertProgram;
  11698. CheckSource('TestAdvRecord_PropertyDefault',
  11699. LinesToStr([ // statements
  11700. 'rtl.recNewT(this, "TPoint", function () {',
  11701. ' this.$eq = function (b) {',
  11702. ' return true;',
  11703. ' };',
  11704. ' this.$assign = function (s) {',
  11705. ' return this;',
  11706. ' };',
  11707. ' this.GetItems = function (Index) {',
  11708. ' var Result = 0;',
  11709. ' this.SetItems(Index, this.GetItems(Index));',
  11710. ' this.SetItems(Index, this.GetItems(Index));',
  11711. ' return Result;',
  11712. ' };',
  11713. ' this.SetItems = function (Index, Value) {',
  11714. ' };',
  11715. '});',
  11716. 'this.p = this.TPoint.$new();',
  11717. '']),
  11718. LinesToStr([ // $mod.$main
  11719. '$mod.p.SetItems(1, $mod.p.GetItems(2));',
  11720. '$mod.p.SetItems(3, $mod.p.GetItems(4));',
  11721. '']));
  11722. end;
  11723. procedure TTestModule.TestAdvRecord_Property_ClassMethod;
  11724. begin
  11725. StartProgram(false);
  11726. Add([
  11727. '{$modeswitch AdvancedRecords}',
  11728. 'type',
  11729. ' TRec = record',
  11730. ' class var',
  11731. ' Fx: longint;',
  11732. ' Fy: longint;',
  11733. ' class function GetInt: longint; static;',
  11734. ' class procedure SetInt(Value: longint); static;',
  11735. ' class procedure DoIt; static;',
  11736. ' class property IntA: longint read Fx write Fy;',
  11737. ' class property IntB: longint read GetInt write SetInt;',
  11738. ' end;',
  11739. 'class function trec.getint: longint;',
  11740. 'begin',
  11741. ' result:=fx;',
  11742. 'end;',
  11743. 'class procedure trec.setint(value: longint);',
  11744. 'begin',
  11745. 'end;',
  11746. 'class procedure trec.doit;',
  11747. 'begin',
  11748. ' IntA:=IntA+1;',
  11749. ' IntB:=IntB+1;',
  11750. 'end;',
  11751. 'var r: trec;',
  11752. 'begin',
  11753. ' trec.inta:=trec.inta+1;',
  11754. ' if trec.intb=2 then;',
  11755. ' trec.intb:=trec.intb+2;',
  11756. ' trec.setint(trec.inta);',
  11757. ' r.inta:=r.inta+1;',
  11758. ' if r.intb=2 then;',
  11759. ' r.intb:=r.intb+2;',
  11760. ' r.setint(r.inta);']);
  11761. ConvertProgram;
  11762. CheckSource('TestAdvRecord_Property_ClassMethod',
  11763. LinesToStr([ // statements
  11764. 'rtl.recNewT(this, "TRec", function () {',
  11765. ' this.Fx = 0;',
  11766. ' this.Fy = 0;',
  11767. ' this.$eq = function (b) {',
  11768. ' return true;',
  11769. ' };',
  11770. ' this.$assign = function (s) {',
  11771. ' return this;',
  11772. ' };',
  11773. ' this.GetInt = function () {',
  11774. ' var Result = 0;',
  11775. ' Result = $mod.TRec.Fx;',
  11776. ' return Result;',
  11777. ' };',
  11778. ' this.SetInt = function (Value) {',
  11779. ' };',
  11780. ' this.DoIt = function () {',
  11781. ' $mod.TRec.Fy = $mod.TRec.Fx + 1;',
  11782. ' $mod.TRec.SetInt($mod.TRec.GetInt() + 1);',
  11783. ' };',
  11784. '}, true);',
  11785. 'this.r = this.TRec.$new();',
  11786. '']),
  11787. LinesToStr([ // $mod.$main
  11788. '$mod.TRec.Fy = $mod.TRec.Fx + 1;',
  11789. 'if ($mod.TRec.GetInt() === 2) ;',
  11790. '$mod.TRec.SetInt($mod.TRec.GetInt() + 2);',
  11791. '$mod.TRec.SetInt($mod.TRec.Fx);',
  11792. '$mod.TRec.Fy = $mod.r.Fx + 1;',
  11793. 'if ($mod.TRec.GetInt() === 2) ;',
  11794. '$mod.TRec.SetInt($mod.TRec.GetInt() + 2);',
  11795. '$mod.TRec.SetInt($mod.r.Fx);',
  11796. '']));
  11797. end;
  11798. procedure TTestModule.TestAdvRecord_Const;
  11799. begin
  11800. StartProgram(false);
  11801. Add([
  11802. '{$modeswitch AdvancedRecords}',
  11803. 'type',
  11804. ' TArrInt = array[3..4] of longint;',
  11805. ' TPoint = record',
  11806. ' x,y: longint;',
  11807. ' class var Count: nativeint;',
  11808. ' end;',
  11809. ' TRec = record',
  11810. ' i: longint;',
  11811. ' a: array of longint;',
  11812. ' s: array[1..2] of longint;',
  11813. ' m: array[1..2,3..4] of longint;',
  11814. ' p: TPoint;',
  11815. ' end;',
  11816. ' TPoints = array of TPoint;',
  11817. 'const',
  11818. ' r: TRec = (',
  11819. ' i:1;',
  11820. ' a:(2,3);',
  11821. ' s:(4,5);',
  11822. ' m:( (11,12), (13,14) );',
  11823. ' p: (x:21)',
  11824. ' );',
  11825. ' p: TPoints = ( (x:1;y:2), (x:3;y:4) );',
  11826. 'begin']);
  11827. ConvertProgram;
  11828. CheckSource('TestAdvRecord_Const',
  11829. LinesToStr([ // statements
  11830. 'rtl.recNewT(this, "TPoint", function () {',
  11831. ' this.x = 0;',
  11832. ' this.y = 0;',
  11833. ' this.Count = 0;',
  11834. ' this.$eq = function (b) {',
  11835. ' return (this.x === b.x) && (this.y === b.y);',
  11836. ' };',
  11837. ' this.$assign = function (s) {',
  11838. ' this.x = s.x;',
  11839. ' this.y = s.y;',
  11840. ' return this;',
  11841. ' };',
  11842. '}, true);',
  11843. 'rtl.recNewT(this, "TRec", function () {',
  11844. ' this.i = 0;',
  11845. ' this.$new = function () {',
  11846. ' var r = Object.create(this);',
  11847. ' r.a = [];',
  11848. ' r.s = rtl.arraySetLength(null, 0, 2);',
  11849. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  11850. ' r.p = $mod.TPoint.$new();',
  11851. ' return r;',
  11852. ' };',
  11853. ' this.$eq = function (b) {',
  11854. ' 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);',
  11855. ' };',
  11856. ' this.$assign = function (s) {',
  11857. ' this.i = s.i;',
  11858. ' this.a = rtl.arrayRef(s.a);',
  11859. ' this.s = s.s.slice(0);',
  11860. ' this.m = s.m.slice(0);',
  11861. ' this.p.$assign(s.p);',
  11862. ' return this;',
  11863. ' };',
  11864. '});',
  11865. 'this.r = this.TRec.$clone({',
  11866. ' i: 1,',
  11867. ' a: [2, 3],',
  11868. ' s: [4, 5],',
  11869. ' m: [[11, 12], [13, 14]],',
  11870. ' p: this.TPoint.$clone({',
  11871. ' x: 21,',
  11872. ' y: 0',
  11873. ' })',
  11874. '});',
  11875. 'this.p = [this.TPoint.$clone({',
  11876. ' x: 1,',
  11877. ' y: 2',
  11878. '}), this.TPoint.$clone({',
  11879. ' x: 3,',
  11880. ' y: 4',
  11881. '})];',
  11882. '']),
  11883. LinesToStr([ // $mod.$main
  11884. '']));
  11885. end;
  11886. procedure TTestModule.TestAdvRecord_ExternalField;
  11887. begin
  11888. StartProgram(false);
  11889. Add([
  11890. '{$modeswitch AdvancedRecords}',
  11891. '{$modeswitch externalclass}',
  11892. 'type',
  11893. ' TCar = record',
  11894. ' public',
  11895. ' Intern: longint external name ''$Intern'';',
  11896. ' Intern2: longint external name ''$Intern2'';',
  11897. ' Bracket: longint external name ''["A B"]'';',
  11898. ' procedure DoIt;',
  11899. ' end;',
  11900. 'procedure tcar.doit;',
  11901. 'begin',
  11902. ' Intern:=Intern+1;',
  11903. ' Intern2:=Intern2+2;',
  11904. ' Bracket:=Bracket+3;',
  11905. 'end;',
  11906. 'var Rec: TCar = (intern: 11; intern2: 12; bracket: 13);',
  11907. 'begin',
  11908. ' Rec.intern:=Rec.intern+1;',
  11909. ' Rec.intern2:=Rec.intern2+2;',
  11910. ' Rec.Bracket:=Rec.Bracket+3;',
  11911. ' with Rec do begin',
  11912. ' intern:=intern+1;',
  11913. ' intern2:=intern2+2;',
  11914. ' Bracket:=Bracket+3;',
  11915. ' end;']);
  11916. ConvertProgram;
  11917. CheckSource('TestAdvRecord_ExternalField',
  11918. LinesToStr([ // statements
  11919. 'rtl.recNewT(this, "TCar", function () {',
  11920. ' this.$eq = function (b) {',
  11921. ' return (this.$Intern === b.$Intern) && (this.$Intern2 === b.$Intern2) && (this["A B"] === b["A B"]);',
  11922. ' };',
  11923. ' this.$assign = function (s) {',
  11924. ' this.$Intern = s.$Intern;',
  11925. ' this.$Intern2 = s.$Intern2;',
  11926. ' this["A B"] = s["A B"];',
  11927. ' return this;',
  11928. ' };',
  11929. ' this.DoIt = function () {',
  11930. ' this.$Intern = this.$Intern + 1;',
  11931. ' this.$Intern2 = this.$Intern2 + 2;',
  11932. ' this["A B"] = this["A B"] + 3;',
  11933. ' };',
  11934. '});',
  11935. 'this.Rec = this.TCar.$clone({',
  11936. ' $Intern: 11,',
  11937. ' $Intern2: 12,',
  11938. ' "A B": 13',
  11939. '});',
  11940. '']),
  11941. LinesToStr([ // $mod.$main
  11942. '$mod.Rec.$Intern = $mod.Rec.$Intern + 1;',
  11943. '$mod.Rec.$Intern2 = $mod.Rec.$Intern2 + 2;',
  11944. '$mod.Rec["A B"] = $mod.Rec["A B"] + 3;',
  11945. 'var $with = $mod.Rec;',
  11946. '$with.$Intern = $with.$Intern + 1;',
  11947. '$with.$Intern2 = $with.$Intern2 + 2;',
  11948. '$with["A B"] = $with["A B"] + 3;',
  11949. '']));
  11950. end;
  11951. procedure TTestModule.TestAdvRecord_SubRecord;
  11952. begin
  11953. StartProgram(false);
  11954. Add([
  11955. '{$modeswitch AdvancedRecords}',
  11956. 'type',
  11957. ' TRec = record',
  11958. ' type',
  11959. ' TPoint = record',
  11960. ' x,y: longint;',
  11961. ' class var Count: nativeint;',
  11962. ' procedure DoIt;',
  11963. ' class procedure DoThat; static;',
  11964. ' end;',
  11965. ' var',
  11966. ' i: longint;',
  11967. ' p: TPoint;',
  11968. ' procedure DoSome;',
  11969. ' end;',
  11970. 'const',
  11971. ' r: TRec = (',
  11972. ' i:1;',
  11973. ' p: (x:21;y:22)',
  11974. ' );',
  11975. 'procedure TRec.DoSome;',
  11976. 'begin',
  11977. ' p.x:=p.y+1;',
  11978. ' p.Count:=p.Count+2;',
  11979. 'end;',
  11980. 'procedure TRec.TPoint.DoIt;',
  11981. 'begin',
  11982. ' Count:=Count+3;',
  11983. 'end;',
  11984. 'class procedure TRec.TPoint.DoThat;',
  11985. 'begin',
  11986. ' Count:=Count+4;',
  11987. 'end;',
  11988. 'begin']);
  11989. ConvertProgram;
  11990. CheckSource('TestAdvRecord_SubRecord',
  11991. LinesToStr([ // statements
  11992. 'rtl.recNewT(this, "TRec", function () {',
  11993. ' rtl.recNewT(this, "TPoint", function () {',
  11994. ' this.x = 0;',
  11995. ' this.y = 0;',
  11996. ' this.Count = 0;',
  11997. ' this.$eq = function (b) {',
  11998. ' return (this.x === b.x) && (this.y === b.y);',
  11999. ' };',
  12000. ' this.$assign = function (s) {',
  12001. ' this.x = s.x;',
  12002. ' this.y = s.y;',
  12003. ' return this;',
  12004. ' };',
  12005. ' this.DoIt = function () {',
  12006. ' $mod.TRec.TPoint.Count = this.Count + 3;',
  12007. ' };',
  12008. ' this.DoThat = function () {',
  12009. ' $mod.TRec.TPoint.Count = $mod.TRec.TPoint.Count + 4;',
  12010. ' };',
  12011. ' }, true);',
  12012. ' this.i = 0;',
  12013. ' this.$new = function () {',
  12014. ' var r = Object.create(this);',
  12015. ' r.p = this.TPoint.$new();',
  12016. ' return r;',
  12017. ' };',
  12018. ' this.$eq = function (b) {',
  12019. ' return (this.i === b.i) && this.p.$eq(b.p);',
  12020. ' };',
  12021. ' this.$assign = function (s) {',
  12022. ' this.i = s.i;',
  12023. ' this.p.$assign(s.p);',
  12024. ' return this;',
  12025. ' };',
  12026. ' this.DoSome = function () {',
  12027. ' this.p.x = this.p.y + 1;',
  12028. ' this.TPoint.Count = this.p.Count + 2;',
  12029. ' };',
  12030. '}, true);',
  12031. 'this.r = this.TRec.$clone({',
  12032. ' i: 1,',
  12033. ' p: this.TRec.TPoint.$clone({',
  12034. ' x: 21,',
  12035. ' y: 22',
  12036. ' })',
  12037. '});',
  12038. '']),
  12039. LinesToStr([ // $mod.$main
  12040. '']));
  12041. end;
  12042. procedure TTestModule.TestAdvRecord_SubClass;
  12043. begin
  12044. StartProgram(false);
  12045. Add([
  12046. '{$modeswitch AdvancedRecords}',
  12047. 'type',
  12048. ' TObject = class end;',
  12049. ' TPoint = record',
  12050. ' type',
  12051. ' TBird = class',
  12052. ' procedure DoIt;',
  12053. ' class procedure Glob;',
  12054. ' end;',
  12055. ' procedure DoIt(b: TBird);',
  12056. ' end;',
  12057. 'procedure TPoint.TBird.DoIt;',
  12058. 'begin',
  12059. ' doit;',
  12060. ' self.doit;',
  12061. ' glob;',
  12062. ' self.glob;',
  12063. 'end;',
  12064. 'class procedure TPoint.TBird.Glob;',
  12065. 'begin',
  12066. ' glob;',
  12067. ' self.glob;',
  12068. 'end;',
  12069. 'procedure TPoint.DoIt(b: TBird);',
  12070. 'begin',
  12071. ' b.doit;',
  12072. ' b.glob;',
  12073. ' TBird.glob;',
  12074. 'end;',
  12075. 'begin',
  12076. '']);
  12077. ConvertProgram;
  12078. CheckSource('TestAdvRecord_SubClass',
  12079. LinesToStr([ // statements
  12080. 'rtl.createClass(this, "TObject", null, function () {',
  12081. ' this.$init = function () {',
  12082. ' };',
  12083. ' this.$final = function () {',
  12084. ' };',
  12085. '});',
  12086. 'rtl.recNewT(this, "TPoint", function () {',
  12087. ' rtl.createClass(this, "TBird", $mod.TObject, function () {',
  12088. ' this.DoIt = function () {',
  12089. ' this.DoIt();',
  12090. ' this.DoIt();',
  12091. ' this.$class.Glob();',
  12092. ' this.$class.Glob();',
  12093. ' };',
  12094. ' this.Glob = function () {',
  12095. ' this.Glob();',
  12096. ' this.Glob();',
  12097. ' };',
  12098. ' }, "TPoint.TBird");',
  12099. ' this.$eq = function (b) {',
  12100. ' return true;',
  12101. ' };',
  12102. ' this.$assign = function (s) {',
  12103. ' return this;',
  12104. ' };',
  12105. ' this.DoIt = function (b) {',
  12106. ' b.DoIt();',
  12107. ' b.$class.Glob();',
  12108. ' this.TBird.Glob();',
  12109. ' };',
  12110. '}, true);',
  12111. '']),
  12112. LinesToStr([ // $mod.$main
  12113. '']));
  12114. end;
  12115. procedure TTestModule.TestAdvRecord_SubInterfaceFail;
  12116. begin
  12117. StartProgram(false);
  12118. Add([
  12119. '{$modeswitch AdvancedRecords}',
  12120. 'type',
  12121. ' IUnknown = interface end;',
  12122. ' TPoint = record',
  12123. ' type IBird = interface end;',
  12124. ' end;',
  12125. 'begin',
  12126. '']);
  12127. SetExpectedPasResolverError('not yet implemented: IBird:TPasClassType [20190105143752] "interface inside record"',
  12128. nNotYetImplemented);
  12129. ParseProgram;
  12130. end;
  12131. procedure TTestModule.TestAdvRecord_Constructor;
  12132. begin
  12133. StartProgram(false);
  12134. Add([
  12135. '{$modeswitch AdvancedRecords}',
  12136. 'type',
  12137. ' TPoint = record',
  12138. ' x,y: longint;',
  12139. ' class procedure Run(w: longint = 13); static;',
  12140. ' constructor Create(ax: longint; ay: longint = -1);',
  12141. ' end;',
  12142. 'class procedure tpoint.run(w: longint);',
  12143. 'begin',
  12144. ' run;',
  12145. ' run();',
  12146. 'end;',
  12147. 'constructor tpoint.create(ax,ay: longint);',
  12148. 'begin',
  12149. ' x:=ax;',
  12150. ' self.y:=ay;',
  12151. ' run;',
  12152. ' run(ax);',
  12153. 'end;',
  12154. 'var r: TPoint;',
  12155. 'begin',
  12156. ' r:=TPoint.Create(1,2);',
  12157. ' with TPoint do r:=Create(1,2);',
  12158. ' r.Create(3);',
  12159. ' r:=r.Create(4);',
  12160. '']);
  12161. ConvertProgram;
  12162. CheckSource('TestAdvRecord_Constructor',
  12163. LinesToStr([ // statements
  12164. 'rtl.recNewT(this, "TPoint", function () {',
  12165. ' this.x = 0;',
  12166. ' this.y = 0;',
  12167. ' this.$eq = function (b) {',
  12168. ' return (this.x === b.x) && (this.y === b.y);',
  12169. ' };',
  12170. ' this.$assign = function (s) {',
  12171. ' this.x = s.x;',
  12172. ' this.y = s.y;',
  12173. ' return this;',
  12174. ' };',
  12175. ' this.Run = function (w) {',
  12176. ' $mod.TPoint.Run(13);',
  12177. ' $mod.TPoint.Run(13);',
  12178. ' };',
  12179. ' this.Create = function (ax, ay) {',
  12180. ' this.x = ax;',
  12181. ' this.y = ay;',
  12182. ' this.Run(13);',
  12183. ' this.Run(ax);',
  12184. ' return this;',
  12185. ' };',
  12186. '});',
  12187. 'this.r = this.TPoint.$new();',
  12188. '']),
  12189. LinesToStr([ // $mod.$main
  12190. '$mod.r.$assign($mod.TPoint.$new().Create(1, 2));',
  12191. 'var $with = $mod.TPoint;',
  12192. '$mod.r.$assign($with.$new().Create(1, 2));',
  12193. '$mod.r.Create(3, -1);',
  12194. '$mod.r.$assign($mod.r.Create(4, -1));',
  12195. '']));
  12196. end;
  12197. procedure TTestModule.TestAdvRecord_ClassConstructor_Program;
  12198. begin
  12199. StartProgram(false);
  12200. Add([
  12201. '{$modeswitch AdvancedRecords}',
  12202. 'type',
  12203. ' TPoint = record',
  12204. ' class var x: longint;',
  12205. ' class procedure Fly; static;',
  12206. ' class constructor Init;',
  12207. ' end;',
  12208. 'var count: word;',
  12209. 'class procedure Tpoint.Fly;',
  12210. 'begin',
  12211. 'end;',
  12212. 'class constructor tpoint.init;',
  12213. 'begin',
  12214. ' count:=count+1;',
  12215. ' x:=x+3;',
  12216. ' tpoint.x:=tpoint.x+4;',
  12217. ' fly;',
  12218. ' tpoint.fly;',
  12219. 'end;',
  12220. 'var r: TPoint;',
  12221. 'begin',
  12222. ' r.x:=r.x+10;',
  12223. ' r.Fly;',
  12224. ' r.Fly();',
  12225. '']);
  12226. ConvertProgram;
  12227. CheckSource('TestAdvRecord_ClassConstructor_Program',
  12228. LinesToStr([ // statements
  12229. 'rtl.recNewT(this, "TPoint", function () {',
  12230. ' this.x = 0;',
  12231. ' this.$eq = function (b) {',
  12232. ' return true;',
  12233. ' };',
  12234. ' this.$assign = function (s) {',
  12235. ' return this;',
  12236. ' };',
  12237. ' this.Fly = function () {',
  12238. ' };',
  12239. '}, true);',
  12240. 'this.count = 0;',
  12241. 'this.r = this.TPoint.$new();',
  12242. '']),
  12243. LinesToStr([ // $mod.$main
  12244. '(function () {',
  12245. ' $mod.count = $mod.count + 1;',
  12246. ' $mod.TPoint.x = $mod.TPoint.x + 3;',
  12247. ' $mod.TPoint.x = $mod.TPoint.x + 4;',
  12248. ' $mod.TPoint.Fly();',
  12249. ' $mod.TPoint.Fly();',
  12250. '})();',
  12251. '$mod.TPoint.x = $mod.r.x + 10;',
  12252. '$mod.TPoint.Fly();',
  12253. '$mod.TPoint.Fly();',
  12254. '']));
  12255. end;
  12256. procedure TTestModule.TestAdvRecord_ClassConstructor_Unit;
  12257. begin
  12258. StartUnit(false);
  12259. Add([
  12260. 'interface',
  12261. '{$modeswitch AdvancedRecords}',
  12262. 'type',
  12263. ' TPoint = record',
  12264. ' class var x: longint;',
  12265. ' class procedure Fly; static;',
  12266. ' class constructor Init;',
  12267. ' end;',
  12268. 'implementation',
  12269. 'var count: word;',
  12270. 'class procedure Tpoint.Fly;',
  12271. 'begin',
  12272. 'end;',
  12273. 'class constructor tpoint.init;',
  12274. 'begin',
  12275. ' count:=count+1;',
  12276. ' x:=3;',
  12277. ' tpoint.x:=4;',
  12278. ' fly;',
  12279. ' tpoint.fly;',
  12280. 'end;',
  12281. '']);
  12282. ConvertUnit;
  12283. CheckSource('TestAdvRecord_ClassConstructor_Unit',
  12284. LinesToStr([ // statements
  12285. 'var $impl = $mod.$impl;',
  12286. 'rtl.recNewT(this, "TPoint", function () {',
  12287. ' this.x = 0;',
  12288. ' this.$eq = function (b) {',
  12289. ' return true;',
  12290. ' };',
  12291. ' this.$assign = function (s) {',
  12292. ' return this;',
  12293. ' };',
  12294. ' this.Fly = function () {',
  12295. ' };',
  12296. '}, true);',
  12297. '']),
  12298. LinesToStr([ // $mod.$init
  12299. '(function () {',
  12300. ' $impl.count = $impl.count + 1;',
  12301. ' $mod.TPoint.x = 3;',
  12302. ' $mod.TPoint.x = 4;',
  12303. ' $mod.TPoint.Fly();',
  12304. ' $mod.TPoint.Fly();',
  12305. '})();',
  12306. '']),
  12307. LinesToStr([ // $mod.$main
  12308. '$impl.count = 0;',
  12309. '']));
  12310. end;
  12311. procedure TTestModule.TestClass_TObjectDefaultConstructor;
  12312. begin
  12313. StartProgram(false);
  12314. Add(['type',
  12315. ' TObject = class',
  12316. ' public',
  12317. ' constructor Create;',
  12318. ' destructor Destroy;',
  12319. ' end;',
  12320. ' TBird = TObject;',
  12321. 'constructor tobject.create;',
  12322. 'begin end;',
  12323. 'destructor tobject.destroy;',
  12324. 'begin end;',
  12325. 'var Obj: tobject;',
  12326. 'begin',
  12327. ' obj:=tobject.create;',
  12328. ' obj:=tobject.create();',
  12329. ' obj:=tbird.create;',
  12330. ' obj:=tbird.create();',
  12331. ' obj:=obj.create();',
  12332. ' obj.destroy;',
  12333. '']);
  12334. ConvertProgram;
  12335. CheckSource('TestClass_TObjectDefaultConstructor',
  12336. LinesToStr([ // statements
  12337. 'rtl.createClass(this,"TObject",null,function(){',
  12338. ' this.$init = function () {',
  12339. ' };',
  12340. ' this.$final = function () {',
  12341. ' };',
  12342. ' this.Create = function(){',
  12343. ' return this;',
  12344. ' };',
  12345. ' this.Destroy = function(){',
  12346. ' };',
  12347. '});',
  12348. 'this.Obj = null;'
  12349. ]),
  12350. LinesToStr([ // $mod.$main
  12351. '$mod.Obj = $mod.TObject.$create("Create");',
  12352. '$mod.Obj = $mod.TObject.$create("Create");',
  12353. '$mod.Obj = $mod.TObject.$create("Create");',
  12354. '$mod.Obj = $mod.TObject.$create("Create");',
  12355. '$mod.Obj = $mod.Obj.Create();',
  12356. '$mod.Obj.$destroy("Destroy");',
  12357. '']));
  12358. end;
  12359. procedure TTestModule.TestClass_TObjectConstructorWithParams;
  12360. begin
  12361. StartProgram(false);
  12362. Add('type');
  12363. Add(' TObject = class');
  12364. Add(' public');
  12365. Add(' constructor Create(Par: longint);');
  12366. Add(' end;');
  12367. Add('constructor tobject.create(par: longint);');
  12368. Add('begin end;');
  12369. Add('var Obj: tobject;');
  12370. Add('begin');
  12371. Add(' obj:=tobject.create(3);');
  12372. ConvertProgram;
  12373. CheckSource('TestClass_TObjectConstructorWithParams',
  12374. LinesToStr([ // statements
  12375. 'rtl.createClass(this,"TObject",null,function(){',
  12376. ' this.$init = function () {',
  12377. ' };',
  12378. ' this.$final = function () {',
  12379. ' };',
  12380. ' this.Create = function(Par){',
  12381. ' return this;',
  12382. ' };',
  12383. '});',
  12384. 'this.Obj = null;'
  12385. ]),
  12386. LinesToStr([ // $mod.$main
  12387. '$mod.Obj = $mod.TObject.$create("Create",[3]);'
  12388. ]));
  12389. end;
  12390. procedure TTestModule.TestClass_TObjectConstructorWithDefaultParam;
  12391. begin
  12392. StartProgram(false);
  12393. Add('type');
  12394. Add(' TObject = class');
  12395. Add(' public');
  12396. Add(' constructor Create;');
  12397. Add(' end;');
  12398. Add(' TTest = class(TObject)');
  12399. Add(' public');
  12400. Add(' constructor Create(const Par: longint = 1);');
  12401. Add(' end;');
  12402. Add('constructor tobject.create;');
  12403. Add('begin end;');
  12404. Add('constructor ttest.create(const par: longint);');
  12405. Add('begin end;');
  12406. Add('var t: ttest;');
  12407. Add('begin');
  12408. Add(' t:=ttest.create;');
  12409. Add(' t:=ttest.create(2);');
  12410. ConvertProgram;
  12411. CheckSource('TestClass_TObjectConstructorWithDefaultParam',
  12412. LinesToStr([ // statements
  12413. 'rtl.createClass(this,"TObject",null,function(){',
  12414. ' this.$init = function () {',
  12415. ' };',
  12416. ' this.$final = function () {',
  12417. ' };',
  12418. ' this.Create = function(){',
  12419. ' return this;',
  12420. ' };',
  12421. '});',
  12422. 'rtl.createClass(this, "TTest", this.TObject, function () {',
  12423. ' this.Create$1 = function (Par) {',
  12424. ' return this;',
  12425. ' };',
  12426. '});',
  12427. 'this.t = null;'
  12428. ]),
  12429. LinesToStr([ // $mod.$main
  12430. '$mod.t = $mod.TTest.$create("Create$1", [1]);',
  12431. '$mod.t = $mod.TTest.$create("Create$1", [2]);'
  12432. ]));
  12433. end;
  12434. procedure TTestModule.TestClass_Var;
  12435. begin
  12436. StartProgram(false);
  12437. Add([
  12438. 'type',
  12439. ' TObject = class',
  12440. ' public',
  12441. ' vI: longint;',
  12442. ' constructor Create(Par: longint);',
  12443. ' end;',
  12444. 'constructor tobject.create(par: longint);',
  12445. 'begin',
  12446. ' vi:=par+3',
  12447. 'end;',
  12448. 'var Obj: tobject;',
  12449. 'begin',
  12450. ' obj:=tobject.create(4);',
  12451. ' obj.vi:=obj.VI+5;']);
  12452. ConvertProgram;
  12453. CheckSource('TestClass_Var',
  12454. LinesToStr([ // statements
  12455. 'rtl.createClass(this,"TObject",null,function(){',
  12456. ' this.$init = function () {',
  12457. ' this.vI = 0;',
  12458. ' };',
  12459. ' this.$final = function () {',
  12460. ' };',
  12461. ' this.Create = function(Par){',
  12462. ' this.vI = Par+3;',
  12463. ' return this;',
  12464. ' };',
  12465. '});',
  12466. 'this.Obj = null;'
  12467. ]),
  12468. LinesToStr([ // $mod.$main
  12469. '$mod.Obj = $mod.TObject.$create("Create",[4]);',
  12470. '$mod.Obj.vI = $mod.Obj.vI + 5;'
  12471. ]));
  12472. end;
  12473. procedure TTestModule.TestClass_Method;
  12474. begin
  12475. StartProgram(false);
  12476. Add('type');
  12477. Add(' TObject = class');
  12478. Add(' public');
  12479. Add(' vI: longint;');
  12480. Add(' Sub: TObject;');
  12481. Add(' constructor Create;');
  12482. Add(' function GetIt(Par: longint): tobject;');
  12483. Add(' end;');
  12484. Add('constructor tobject.create; begin end;');
  12485. Add('function tobject.getit(par: longint): tobject;');
  12486. Add('begin');
  12487. Add(' Self.vi:=par+3;');
  12488. Add(' Result:=self.sub;');
  12489. Add('end;');
  12490. Add('var Obj: tobject;');
  12491. Add('begin');
  12492. Add(' obj:=tobject.create;');
  12493. Add(' obj.getit(4);');
  12494. Add(' obj.sub.sub:=nil;');
  12495. Add(' obj.sub.getit(5);');
  12496. Add(' obj.sub.getit(6).SUB:=nil;');
  12497. Add(' obj.sub.getit(7).GETIT(8);');
  12498. Add(' obj.sub.getit(9).SuB.getit(10);');
  12499. ConvertProgram;
  12500. CheckSource('TestClass_Method',
  12501. LinesToStr([ // statements
  12502. 'rtl.createClass(this,"TObject",null,function(){',
  12503. ' this.$init = function () {',
  12504. ' this.vI = 0;',
  12505. ' this.Sub = null;',
  12506. ' };',
  12507. ' this.$final = function () {',
  12508. ' this.Sub = undefined;',
  12509. ' };',
  12510. ' this.Create = function(){',
  12511. ' return this;',
  12512. ' };',
  12513. ' this.GetIt = function(Par){',
  12514. ' var Result = null;',
  12515. ' this.vI = Par + 3;',
  12516. ' Result = this.Sub;',
  12517. ' return Result;',
  12518. ' };',
  12519. '});',
  12520. 'this.Obj = null;'
  12521. ]),
  12522. LinesToStr([ // $mod.$main
  12523. '$mod.Obj = $mod.TObject.$create("Create");',
  12524. '$mod.Obj.GetIt(4);',
  12525. '$mod.Obj.Sub.Sub=null;',
  12526. '$mod.Obj.Sub.GetIt(5);',
  12527. '$mod.Obj.Sub.GetIt(6).Sub=null;',
  12528. '$mod.Obj.Sub.GetIt(7).GetIt(8);',
  12529. '$mod.Obj.Sub.GetIt(9).Sub.GetIt(10);'
  12530. ]));
  12531. end;
  12532. procedure TTestModule.TestClass_Implementation;
  12533. begin
  12534. StartUnit(false);
  12535. Add([
  12536. 'interface',
  12537. 'type',
  12538. ' TObject = class',
  12539. ' constructor Create;',
  12540. ' end;',
  12541. 'implementation',
  12542. 'type',
  12543. ' TIntClass = class',
  12544. ' constructor Create; reintroduce;',
  12545. ' class procedure DoGlob;',
  12546. ' end;',
  12547. 'constructor tintclass.create;',
  12548. 'begin',
  12549. ' inherited;',
  12550. ' inherited create;',
  12551. ' doglob;',
  12552. 'end;',
  12553. 'class procedure tintclass.doglob;',
  12554. 'begin',
  12555. 'end;',
  12556. 'constructor tobject.create;',
  12557. 'var',
  12558. ' iC: tintclass;',
  12559. 'begin',
  12560. ' ic:=tintclass.create;',
  12561. ' tintclass.doglob;',
  12562. ' ic.doglob;',
  12563. 'end;',
  12564. 'initialization',
  12565. ' tintclass.doglob;',
  12566. '']);
  12567. ConvertUnit;
  12568. CheckSource('TestClass_Implementation',
  12569. LinesToStr([ // statements
  12570. 'var $impl = $mod.$impl;',
  12571. 'rtl.createClass(this, "TObject", null, function () {',
  12572. ' this.$init = function () {',
  12573. ' };',
  12574. ' this.$final = function () {',
  12575. ' };',
  12576. ' this.Create = function () {',
  12577. ' var iC = null;',
  12578. ' iC = $impl.TIntClass.$create("Create$1");',
  12579. ' $impl.TIntClass.DoGlob();',
  12580. ' iC.$class.DoGlob();',
  12581. ' return this;',
  12582. ' };',
  12583. '});',
  12584. '']),
  12585. LinesToStr([ // $mod.$main
  12586. '$impl.TIntClass.DoGlob();',
  12587. '']),
  12588. LinesToStr([
  12589. 'rtl.createClass($impl, "TIntClass", $mod.TObject, function () {',
  12590. ' this.Create$1 = function () {',
  12591. ' $mod.TObject.Create.call(this);',
  12592. ' $mod.TObject.Create.call(this);',
  12593. ' this.$class.DoGlob();',
  12594. ' return this;',
  12595. ' };',
  12596. ' this.DoGlob = function () {',
  12597. ' };',
  12598. '});',
  12599. '']));
  12600. end;
  12601. procedure TTestModule.TestClass_Inheritance;
  12602. begin
  12603. StartProgram(false);
  12604. Add('type');
  12605. Add(' TObject = class');
  12606. Add(' public');
  12607. Add(' constructor Create;');
  12608. Add(' end;');
  12609. Add(' TClassA = class');
  12610. Add(' end;');
  12611. Add(' TClassB = class(TObject)');
  12612. Add(' procedure ProcB;');
  12613. Add(' end;');
  12614. Add('constructor tobject.create; begin end;');
  12615. Add('procedure tclassb.procb; begin end;');
  12616. Add('var');
  12617. Add(' oO: TObject;');
  12618. Add(' oA: TClassA;');
  12619. Add(' oB: TClassB;');
  12620. Add('begin');
  12621. Add(' oO:=tobject.Create;');
  12622. Add(' oA:=tclassa.Create;');
  12623. Add(' ob:=tclassb.Create;');
  12624. Add(' if oo is tclassa then ;');
  12625. Add(' ob:=oo as tclassb;');
  12626. Add(' (oo as tclassb).procb;');
  12627. ConvertProgram;
  12628. CheckSource('TestClass_Inheritance',
  12629. LinesToStr([ // statements
  12630. 'rtl.createClass(this,"TObject",null,function(){',
  12631. ' this.$init = function () {',
  12632. ' };',
  12633. ' this.$final = function () {',
  12634. ' };',
  12635. ' this.Create = function () {',
  12636. ' return this;',
  12637. ' };',
  12638. '});',
  12639. 'rtl.createClass(this,"TClassA",this.TObject,function(){',
  12640. '});',
  12641. 'rtl.createClass(this,"TClassB",this.TObject,function(){',
  12642. ' this.ProcB = function () {',
  12643. ' };',
  12644. '});',
  12645. 'this.oO = null;',
  12646. 'this.oA = null;',
  12647. 'this.oB = null;'
  12648. ]),
  12649. LinesToStr([ // $mod.$main
  12650. '$mod.oO = $mod.TObject.$create("Create");',
  12651. '$mod.oA = $mod.TClassA.$create("Create");',
  12652. '$mod.oB = $mod.TClassB.$create("Create");',
  12653. 'if ($mod.TClassA.isPrototypeOf($mod.oO));',
  12654. '$mod.oB = rtl.as($mod.oO, $mod.TClassB);',
  12655. 'rtl.as($mod.oO, $mod.TClassB).ProcB();'
  12656. ]));
  12657. end;
  12658. procedure TTestModule.TestClass_TypeAlias;
  12659. begin
  12660. StartProgram(false);
  12661. Add([
  12662. '{$interfaces corba}',
  12663. 'type',
  12664. ' IObject = interface',
  12665. ' end;',
  12666. ' IBird = type IObject;',
  12667. ' TObject = class',
  12668. ' end;',
  12669. ' TBird = type TObject;',
  12670. 'var',
  12671. ' oObj: TObject;',
  12672. ' oBird: TBird;',
  12673. ' IntfObj: IObject;',
  12674. ' IntfBird: IBird;',
  12675. 'begin',
  12676. ' oObj:=oBird;',
  12677. '']);
  12678. ConvertProgram;
  12679. CheckSource('TestClass_TypeAlias',
  12680. LinesToStr([ // statements
  12681. 'rtl.createInterface(this, "IObject", "{B92D5841-6F2A-306A-8000-000000000000}", [], null);',
  12682. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-387B-AE88-F10981585074}", [], this.IObject);',
  12683. 'rtl.createClass(this, "TObject", null, function () {',
  12684. ' this.$init = function () {',
  12685. ' };',
  12686. ' this.$final = function () {',
  12687. ' };',
  12688. '});',
  12689. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  12690. '});',
  12691. 'this.oObj = null;',
  12692. 'this.oBird = null;',
  12693. 'this.IntfObj = null;',
  12694. 'this.IntfBird = null;',
  12695. '']),
  12696. LinesToStr([ // $mod.$main
  12697. '$mod.oObj = $mod.oBird;',
  12698. '']));
  12699. end;
  12700. procedure TTestModule.TestClass_AbstractMethod;
  12701. begin
  12702. StartProgram(false);
  12703. Add('type');
  12704. Add(' TObject = class');
  12705. Add(' public');
  12706. Add(' procedure DoIt; virtual; abstract;');
  12707. Add(' end;');
  12708. Add('begin');
  12709. ConvertProgram;
  12710. CheckSource('TestClass_AbstractMethod',
  12711. LinesToStr([ // statements
  12712. 'rtl.createClass(this,"TObject",null,function(){',
  12713. ' this.$init = function () {',
  12714. ' };',
  12715. ' this.$final = function () {',
  12716. ' };',
  12717. '});'
  12718. ]),
  12719. LinesToStr([ // this.$main
  12720. ''
  12721. ]));
  12722. end;
  12723. procedure TTestModule.TestClass_CallInherited_ProcNoParams;
  12724. begin
  12725. StartProgram(false);
  12726. Add([
  12727. 'type',
  12728. ' TObject = class',
  12729. ' procedure DoAbstract; virtual; abstract;',
  12730. ' procedure DoVirtual; virtual;',
  12731. ' procedure DoIt;',
  12732. ' end;',
  12733. ' TA = class',
  12734. ' procedure doabstract; override;',
  12735. ' procedure dovirtual; override;',
  12736. ' procedure DoSome;',
  12737. ' end;',
  12738. 'procedure tobject.dovirtual;',
  12739. 'begin',
  12740. ' inherited; // call non existing ancestor -> ignore silently',
  12741. 'end;',
  12742. 'procedure tobject.doit;',
  12743. 'begin',
  12744. 'end;',
  12745. 'procedure ta.doabstract;',
  12746. 'begin',
  12747. ' inherited dovirtual; // call TObject.DoVirtual',
  12748. 'end;',
  12749. 'procedure ta.dovirtual;',
  12750. 'begin',
  12751. ' inherited; // call TObject.DoVirtual',
  12752. ' inherited dovirtual; // call TObject.DoVirtual',
  12753. ' inherited dovirtual(); // call TObject.DoVirtual',
  12754. ' doit;',
  12755. ' doit();',
  12756. 'end;',
  12757. 'procedure ta.dosome;',
  12758. 'begin',
  12759. ' inherited; // call non existing ancestor method -> silently ignore',
  12760. 'end;',
  12761. 'begin']);
  12762. ConvertProgram;
  12763. CheckSource('TestClass_CallInherited_ProcNoParams',
  12764. LinesToStr([ // statements
  12765. 'rtl.createClass(this,"TObject",null,function(){',
  12766. ' this.$init = function () {',
  12767. ' };',
  12768. ' this.$final = function () {',
  12769. ' };',
  12770. ' this.DoVirtual = function () {',
  12771. ' };',
  12772. ' this.DoIt = function () {',
  12773. ' };',
  12774. '});',
  12775. 'rtl.createClass(this, "TA", this.TObject, function () {',
  12776. ' this.DoAbstract = function () {',
  12777. ' $mod.TObject.DoVirtual.call(this);',
  12778. ' };',
  12779. ' this.DoVirtual = function () {',
  12780. ' $mod.TObject.DoVirtual.call(this);',
  12781. ' $mod.TObject.DoVirtual.call(this);',
  12782. ' $mod.TObject.DoVirtual.call(this);',
  12783. ' this.DoIt();',
  12784. ' this.DoIt();',
  12785. ' };',
  12786. ' this.DoSome = function () {',
  12787. ' };',
  12788. '});'
  12789. ]),
  12790. LinesToStr([ // this.$main
  12791. ''
  12792. ]));
  12793. end;
  12794. procedure TTestModule.TestClass_CallInherited_WithParams;
  12795. begin
  12796. StartProgram(false);
  12797. Add([
  12798. 'type',
  12799. ' TObject = class',
  12800. ' procedure DoAbstract(pA: longint; pB: longint = 0); virtual; abstract;',
  12801. ' procedure DoVirtual(pA: longint; pB: longint = 0); virtual;',
  12802. ' procedure DoIt(pA: longint; pB: longint = 0);',
  12803. ' procedure DoIt2(pA: longint = 1; pB: longint = 2);',
  12804. ' function GetIt(pA: longint = 1; pB: longint = 2): longint;',
  12805. ' end;',
  12806. ' TClassA = class',
  12807. ' procedure DoAbstract(pA: longint; pB: longint = 0); override;',
  12808. ' procedure DoVirtual(pA: longint; pB: longint = 0); override;',
  12809. ' function GetIt(pA: longint = 1; pB: longint = 2): longint;',
  12810. ' end;',
  12811. 'procedure tobject.dovirtual(pa: longint; pb: longint = 0);',
  12812. 'begin',
  12813. 'end;',
  12814. 'procedure tobject.doit(pa: longint; pb: longint = 0);',
  12815. 'begin',
  12816. 'end;',
  12817. 'procedure tobject.doit2(pa: longint; pb: longint = 0);',
  12818. 'begin',
  12819. 'end;',
  12820. 'function tobject.getit(pa: longint; pb: longint = 0): longint;',
  12821. 'begin',
  12822. 'end;',
  12823. 'procedure tclassa.doabstract(pa: longint; pb: longint = 0);',
  12824. 'begin',
  12825. ' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)',
  12826. ' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)',
  12827. 'end;',
  12828. 'procedure tclassa.dovirtual(pa: longint; pb: longint = 0);',
  12829. 'begin',
  12830. ' inherited; // call TObject.DoVirtual(pA,pB)',
  12831. ' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)',
  12832. ' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)',
  12833. ' doit(pa,pb);',
  12834. ' doit(pa);',
  12835. ' doit2(pa);',
  12836. ' doit2;',
  12837. 'end;',
  12838. 'function tclassa.getit(pa: longint; pb: longint = 0): longint;',
  12839. 'begin',
  12840. ' pa:=inherited;',
  12841. 'end;',
  12842. 'begin']);
  12843. ConvertProgram;
  12844. CheckSource('TestClass_CallInherited_WithParams',
  12845. LinesToStr([ // statements
  12846. 'rtl.createClass(this,"TObject",null,function(){',
  12847. ' this.$init = function () {',
  12848. ' };',
  12849. ' this.$final = function () {',
  12850. ' };',
  12851. ' this.DoVirtual = function (pA,pB) {',
  12852. ' };',
  12853. ' this.DoIt = function (pA,pB) {',
  12854. ' };',
  12855. ' this.DoIt2 = function (pA,pB) {',
  12856. ' };',
  12857. ' this.GetIt = function (pA, pB) {',
  12858. ' var Result = 0;',
  12859. ' return Result;',
  12860. ' };',
  12861. '});',
  12862. 'rtl.createClass(this, "TClassA", this.TObject, function () {',
  12863. ' this.DoAbstract = function (pA,pB) {',
  12864. ' $mod.TObject.DoVirtual.call(this,pA,pB);',
  12865. ' $mod.TObject.DoVirtual.call(this,pA,0);',
  12866. ' };',
  12867. ' this.DoVirtual = function (pA,pB) {',
  12868. ' $mod.TObject.DoVirtual.apply(this, arguments);',
  12869. ' $mod.TObject.DoVirtual.call(this,pA,pB);',
  12870. ' $mod.TObject.DoVirtual.call(this,pA,0);',
  12871. ' this.DoIt(pA,pB);',
  12872. ' this.DoIt(pA,0);',
  12873. ' this.DoIt2(pA,2);',
  12874. ' this.DoIt2(1,2);',
  12875. ' };',
  12876. ' this.GetIt$1 = function (pA, pB) {',
  12877. ' var Result = 0;',
  12878. ' pA = $mod.TObject.GetIt.apply(this, arguments);',
  12879. ' return Result;',
  12880. ' };',
  12881. '});'
  12882. ]),
  12883. LinesToStr([ // this.$main
  12884. ''
  12885. ]));
  12886. end;
  12887. procedure TTestModule.TestClasS_CallInheritedConstructor;
  12888. begin
  12889. StartProgram(false);
  12890. Add('type');
  12891. Add(' TObject = class');
  12892. Add(' constructor Create; virtual;');
  12893. Add(' constructor CreateWithB(b: boolean);');
  12894. Add(' end;');
  12895. Add(' TA = class');
  12896. Add(' constructor Create; override;');
  12897. Add(' constructor CreateWithC(c: char);');
  12898. Add(' procedure DoIt;');
  12899. Add(' class function DoSome: TObject;');
  12900. Add(' end;');
  12901. Add('constructor tobject.create;');
  12902. Add('begin');
  12903. Add(' inherited; // call non existing ancestor -> ignore silently');
  12904. Add('end;');
  12905. Add('constructor tobject.createwithb(b: boolean);');
  12906. Add('begin');
  12907. Add(' inherited; // call non existing ancestor -> ignore silently');
  12908. Add(' create; // normal call');
  12909. Add('end;');
  12910. Add('constructor ta.create;');
  12911. Add('begin');
  12912. Add(' inherited; // normal call TObject.Create');
  12913. Add(' inherited create; // normal call TObject.Create');
  12914. Add(' inherited createwithb(false); // normal call TObject.CreateWithB');
  12915. Add('end;');
  12916. Add('constructor ta.createwithc(c: char);');
  12917. Add('begin');
  12918. Add(' inherited create; // call TObject.Create');
  12919. Add(' inherited createwithb(true); // call TObject.CreateWithB');
  12920. Add(' doit;');
  12921. Add(' doit();');
  12922. Add(' dosome;');
  12923. Add('end;');
  12924. Add('procedure ta.doit;');
  12925. Add('begin');
  12926. Add(' create; // normal call');
  12927. Add(' createwithb(false); // normal call');
  12928. Add(' createwithc(''c''); // normal call');
  12929. Add('end;');
  12930. Add('class function ta.dosome: TObject;');
  12931. Add('begin');
  12932. Add(' Result:=create; // constructor');
  12933. Add(' Result:=createwithb(true); // constructor');
  12934. Add(' Result:=createwithc(''c''); // constructor');
  12935. Add('end;');
  12936. Add('begin');
  12937. ConvertProgram;
  12938. CheckSource('TestClass_CallInheritedConstructor',
  12939. LinesToStr([ // statements
  12940. 'rtl.createClass(this,"TObject",null,function(){',
  12941. ' this.$init = function () {',
  12942. ' };',
  12943. ' this.$final = function () {',
  12944. ' };',
  12945. ' this.Create = function () {',
  12946. ' return this;',
  12947. ' };',
  12948. ' this.CreateWithB = function (b) {',
  12949. ' this.Create();',
  12950. ' return this;',
  12951. ' };',
  12952. '});',
  12953. 'rtl.createClass(this, "TA", this.TObject, function () {',
  12954. ' this.Create = function () {',
  12955. ' $mod.TObject.Create.call(this);',
  12956. ' $mod.TObject.Create.call(this);',
  12957. ' $mod.TObject.CreateWithB.call(this, false);',
  12958. ' return this;',
  12959. ' };',
  12960. ' this.CreateWithC = function (c) {',
  12961. ' $mod.TObject.Create.call(this);',
  12962. ' $mod.TObject.CreateWithB.call(this, true);',
  12963. ' this.DoIt();',
  12964. ' this.DoIt();',
  12965. ' this.$class.DoSome();',
  12966. ' return this;',
  12967. ' };',
  12968. ' this.DoIt = function () {',
  12969. ' this.Create();',
  12970. ' this.CreateWithB(false);',
  12971. ' this.CreateWithC("c");',
  12972. ' };',
  12973. ' this.DoSome = function () {',
  12974. ' var Result = null;',
  12975. ' Result = this.$create("Create");',
  12976. ' Result = this.$create("CreateWithB", [true]);',
  12977. ' Result = this.$create("CreateWithC", ["c"]);',
  12978. ' return Result;',
  12979. ' };',
  12980. '});'
  12981. ]),
  12982. LinesToStr([ // this.$main
  12983. ''
  12984. ]));
  12985. end;
  12986. procedure TTestModule.TestClass_ClassVar_Assign;
  12987. begin
  12988. StartProgram(false);
  12989. Add([
  12990. 'type',
  12991. ' TObject = class',
  12992. ' public',
  12993. ' class var vI: longint;',
  12994. ' class var Sub: TObject;',
  12995. ' constructor Create;',
  12996. ' class function GetIt(var Par: longint): tobject;',
  12997. ' end;',
  12998. 'constructor tobject.create;',
  12999. 'begin',
  13000. ' vi:=vi+1;',
  13001. ' Self.vi:=Self.vi+1;',
  13002. ' inc(vi);',
  13003. 'end;',
  13004. 'class function tobject.getit(var par: longint): tobject;',
  13005. 'begin',
  13006. ' vi:=vi+3;',
  13007. ' Self.vi:=Self.vi+4;',
  13008. ' inc(vi);',
  13009. ' Result:=self.sub;',
  13010. ' GetIt(vi);',
  13011. 'end;',
  13012. 'var Obj: tobject;',
  13013. 'begin',
  13014. ' obj:=tobject.create;',
  13015. ' tobject.vi:=3;',
  13016. ' if tobject.vi=4 then ;',
  13017. ' tobject.sub:=nil;',
  13018. ' obj.sub:=nil;',
  13019. ' obj.sub.sub:=nil;']);
  13020. ConvertProgram;
  13021. CheckSource('TestClass_ClassVar_Assign',
  13022. LinesToStr([ // statements
  13023. 'rtl.createClass(this,"TObject",null,function(){',
  13024. ' this.vI = 0;',
  13025. ' this.Sub = null;',
  13026. ' this.$init = function () {',
  13027. ' };',
  13028. ' this.$final = function () {',
  13029. ' };',
  13030. ' this.Create = function(){',
  13031. ' $mod.TObject.vI = this.vI+1;',
  13032. ' $mod.TObject.vI = this.vI+1;',
  13033. ' $mod.TObject.vI += 1;',
  13034. ' return this;',
  13035. ' };',
  13036. ' this.GetIt = function(Par){',
  13037. ' var Result = null;',
  13038. ' $mod.TObject.vI = this.vI + 3;',
  13039. ' $mod.TObject.vI = this.vI + 4;',
  13040. ' $mod.TObject.vI += 1;',
  13041. ' Result = this.Sub;',
  13042. ' this.GetIt({',
  13043. ' p: $mod.TObject,',
  13044. ' get: function () {',
  13045. ' return this.p.vI;',
  13046. ' },',
  13047. ' set: function (v) {',
  13048. ' this.p.vI = v;',
  13049. ' }',
  13050. ' });',
  13051. ' return Result;',
  13052. ' };',
  13053. '});',
  13054. 'this.Obj = null;'
  13055. ]),
  13056. LinesToStr([ // $mod.$main
  13057. '$mod.Obj = $mod.TObject.$create("Create");',
  13058. '$mod.TObject.vI = 3;',
  13059. 'if ($mod.TObject.vI === 4);',
  13060. '$mod.TObject.Sub=null;',
  13061. '$mod.TObject.Sub=null;',
  13062. '$mod.TObject.Sub=null;',
  13063. '']));
  13064. end;
  13065. procedure TTestModule.TestClass_CallClassMethod;
  13066. begin
  13067. StartProgram(false);
  13068. Add('type');
  13069. Add(' TObject = class');
  13070. Add(' public');
  13071. Add(' class var vI: longint;');
  13072. Add(' class var Sub: TObject;');
  13073. Add(' constructor Create;');
  13074. Add(' function GetMore(Par: longint): longint;');
  13075. Add(' class function GetIt(Par: longint): tobject;');
  13076. Add(' end;');
  13077. Add('constructor tobject.create;');
  13078. Add('begin');
  13079. Add(' sub:=getit(3);');
  13080. Add(' vi:=getmore(4);');
  13081. Add(' sub:=Self.getit(5);');
  13082. Add(' vi:=Self.getmore(6);');
  13083. Add('end;');
  13084. Add('function tobject.getmore(par: longint): longint;');
  13085. Add('begin');
  13086. Add(' sub:=getit(11);');
  13087. Add(' vi:=getmore(12);');
  13088. Add(' sub:=self.getit(13);');
  13089. Add(' vi:=self.getmore(14);');
  13090. Add('end;');
  13091. Add('class function tobject.getit(par: longint): tobject;');
  13092. Add('begin');
  13093. Add(' sub:=getit(21);');
  13094. Add(' vi:=sub.getmore(22);');
  13095. Add(' sub:=self.getit(23);');
  13096. Add(' vi:=self.sub.getmore(24);');
  13097. Add('end;');
  13098. Add('var Obj: tobject;');
  13099. Add('begin');
  13100. Add(' obj:=tobject.create;');
  13101. Add(' tobject.getit(5);');
  13102. Add(' obj.getit(6);');
  13103. Add(' obj.sub.getit(7);');
  13104. Add(' obj.sub.getit(8).SUB:=nil;');
  13105. Add(' obj.sub.getit(9).GETIT(10);');
  13106. Add(' obj.sub.getit(11).SuB.getit(12);');
  13107. ConvertProgram;
  13108. CheckSource('TestClass_CallClassMethod',
  13109. LinesToStr([ // statements
  13110. 'rtl.createClass(this,"TObject",null,function(){',
  13111. ' this.vI = 0;',
  13112. ' this.Sub = null;',
  13113. ' this.$init = function () {',
  13114. ' };',
  13115. ' this.$final = function () {',
  13116. ' };',
  13117. ' this.Create = function(){',
  13118. ' $mod.TObject.Sub = this.$class.GetIt(3);',
  13119. ' $mod.TObject.vI = this.GetMore(4);',
  13120. ' $mod.TObject.Sub = this.$class.GetIt(5);',
  13121. ' $mod.TObject.vI = this.GetMore(6);',
  13122. ' return this;',
  13123. ' };',
  13124. ' this.GetMore = function(Par){',
  13125. ' var Result = 0;',
  13126. ' $mod.TObject.Sub = this.$class.GetIt(11);',
  13127. ' $mod.TObject.vI = this.GetMore(12);',
  13128. ' $mod.TObject.Sub = this.$class.GetIt(13);',
  13129. ' $mod.TObject.vI = this.GetMore(14);',
  13130. ' return Result;',
  13131. ' };',
  13132. ' this.GetIt = function(Par){',
  13133. ' var Result = null;',
  13134. ' $mod.TObject.Sub = this.GetIt(21);',
  13135. ' $mod.TObject.vI = this.Sub.GetMore(22);',
  13136. ' $mod.TObject.Sub = this.GetIt(23);',
  13137. ' $mod.TObject.vI = this.Sub.GetMore(24);',
  13138. ' return Result;',
  13139. ' };',
  13140. '});',
  13141. 'this.Obj = null;'
  13142. ]),
  13143. LinesToStr([ // $mod.$main
  13144. '$mod.Obj = $mod.TObject.$create("Create");',
  13145. '$mod.TObject.GetIt(5);',
  13146. '$mod.Obj.$class.GetIt(6);',
  13147. '$mod.Obj.Sub.$class.GetIt(7);',
  13148. '$mod.TObject.Sub=null;',
  13149. '$mod.Obj.Sub.$class.GetIt(9).$class.GetIt(10);',
  13150. '$mod.Obj.Sub.$class.GetIt(11).Sub.$class.GetIt(12);',
  13151. '']));
  13152. end;
  13153. procedure TTestModule.TestClass_CallClassMethodStatic;
  13154. begin
  13155. StartProgram(false);
  13156. Add([
  13157. 'type',
  13158. ' TObject = class',
  13159. ' public',
  13160. ' class function Fly: tobject; static;',
  13161. ' end;',
  13162. 'class function tobject.Fly: tobject;',
  13163. 'begin',
  13164. ' Result.Fly;',
  13165. ' Result.Fly();',
  13166. ' Fly;',
  13167. ' Fly();',
  13168. ' Fly.Fly;',
  13169. ' Fly.Fly();',
  13170. 'end;',
  13171. 'var Obj: tobject;',
  13172. 'begin',
  13173. ' obj.Fly;',
  13174. ' obj.Fly();',
  13175. ' with obj do begin',
  13176. ' Fly;',
  13177. ' Fly();',
  13178. ' end;',
  13179. '']);
  13180. ConvertProgram;
  13181. CheckSource('TestClass_CallClassMethodStatic',
  13182. LinesToStr([ // statements
  13183. 'rtl.createClass(this, "TObject", null, function () {',
  13184. ' this.$init = function () {',
  13185. ' };',
  13186. ' this.$final = function () {',
  13187. ' };',
  13188. ' this.Fly = function () {',
  13189. ' var Result = null;',
  13190. ' $mod.TObject.Fly();',
  13191. ' $mod.TObject.Fly();',
  13192. ' $mod.TObject.Fly();',
  13193. ' $mod.TObject.Fly();',
  13194. ' $mod.TObject.Fly();',
  13195. ' $mod.TObject.Fly();',
  13196. ' return Result;',
  13197. ' };',
  13198. '});',
  13199. 'this.Obj = null;'
  13200. ]),
  13201. LinesToStr([ // $mod.$main
  13202. '$mod.TObject.Fly();',
  13203. '$mod.TObject.Fly();',
  13204. 'var $with = $mod.Obj;',
  13205. '$with.Fly();',
  13206. '$with.Fly();',
  13207. '']));
  13208. end;
  13209. procedure TTestModule.TestClass_Property;
  13210. begin
  13211. StartProgram(false);
  13212. Add('type');
  13213. Add(' TObject = class');
  13214. Add(' Fx: longint;');
  13215. Add(' Fy: longint;');
  13216. Add(' function GetInt: longint;');
  13217. Add(' procedure SetInt(Value: longint);');
  13218. Add(' procedure DoIt;');
  13219. Add(' property IntA: longint read Fx write Fy;');
  13220. Add(' property IntB: longint read GetInt write SetInt;');
  13221. Add(' end;');
  13222. Add('function tobject.getint: longint;');
  13223. Add('begin');
  13224. Add(' result:=fx;');
  13225. Add('end;');
  13226. Add('procedure tobject.setint(value: longint);');
  13227. Add('begin');
  13228. Add(' if value=fy then exit;');
  13229. Add(' fy:=value;');
  13230. Add('end;');
  13231. Add('procedure tobject.doit;');
  13232. Add('begin');
  13233. Add(' IntA:=IntA+1;');
  13234. Add(' Self.IntA:=Self.IntA+1;');
  13235. Add(' IntB:=IntB+1;');
  13236. Add(' Self.IntB:=Self.IntB+1;');
  13237. Add('end;');
  13238. Add('var Obj: tobject;');
  13239. Add('begin');
  13240. Add(' obj.inta:=obj.inta+1;');
  13241. Add(' if obj.intb=2 then;');
  13242. Add(' obj.intb:=obj.intb+2;');
  13243. Add(' obj.setint(obj.inta);');
  13244. ConvertProgram;
  13245. CheckSource('TestClass_Property',
  13246. LinesToStr([ // statements
  13247. 'rtl.createClass(this, "TObject", null, function () {',
  13248. ' this.$init = function () {',
  13249. ' this.Fx = 0;',
  13250. ' this.Fy = 0;',
  13251. ' };',
  13252. ' this.$final = function () {',
  13253. ' };',
  13254. ' this.GetInt = function () {',
  13255. ' var Result = 0;',
  13256. ' Result = this.Fx;',
  13257. ' return Result;',
  13258. ' };',
  13259. ' this.SetInt = function (Value) {',
  13260. ' if (Value === this.Fy) return;',
  13261. ' this.Fy = Value;',
  13262. ' };',
  13263. ' this.DoIt = function () {',
  13264. ' this.Fy = this.Fx + 1;',
  13265. ' this.Fy = this.Fx + 1;',
  13266. ' this.SetInt(this.GetInt() + 1);',
  13267. ' this.SetInt(this.GetInt() + 1);',
  13268. ' };',
  13269. '});',
  13270. 'this.Obj = null;'
  13271. ]),
  13272. LinesToStr([ // $mod.$main
  13273. '$mod.Obj.Fy = $mod.Obj.Fx + 1;',
  13274. 'if ($mod.Obj.GetInt() === 2);',
  13275. '$mod.Obj.SetInt($mod.Obj.GetInt() + 2);',
  13276. '$mod.Obj.SetInt($mod.Obj.Fx);'
  13277. ]));
  13278. end;
  13279. procedure TTestModule.TestClass_Property_ClassMethod;
  13280. begin
  13281. StartProgram(false);
  13282. Add([
  13283. 'type',
  13284. ' TObject = class',
  13285. ' class var Fx: longint;',
  13286. ' class var Fy: longint;',
  13287. ' class function GetInt: longint;',
  13288. ' class procedure SetInt(Value: longint);',
  13289. ' end;',
  13290. ' TBird = class',
  13291. ' class procedure DoIt;',
  13292. ' class property IntA: longint read Fx write Fy;',
  13293. ' class property IntB: longint read GetInt write SetInt;',
  13294. ' end;',
  13295. 'class function tobject.getint: longint;',
  13296. 'begin',
  13297. ' result:=fx;',
  13298. 'end;',
  13299. 'class procedure tobject.setint(value: longint);',
  13300. 'begin',
  13301. 'end;',
  13302. 'class procedure tbird.doit;',
  13303. 'begin',
  13304. ' FX:=3;',
  13305. ' IntA:=IntA+1;',
  13306. ' Self.IntA:=Self.IntA+1;',
  13307. ' IntB:=IntB+1;',
  13308. ' Self.IntB:=Self.IntB+1;',
  13309. ' with Self do begin',
  13310. ' FX:=11;',
  13311. ' IntA:=IntA+12;',
  13312. ' IntB:=IntB+13;',
  13313. ' end;',
  13314. 'end;',
  13315. 'var Obj: tbird;',
  13316. 'begin',
  13317. ' tbird.fx:=tbird.fx+1;',
  13318. ' tbird.inta:=tbird.inta+1;',
  13319. ' if tbird.intb=2 then;',
  13320. ' tbird.intb:=tbird.intb+2;',
  13321. ' tbird.setint(tbird.inta);',
  13322. ' obj.inta:=obj.inta+1;',
  13323. ' if obj.intb=2 then;',
  13324. ' obj.intb:=obj.intb+2;',
  13325. ' obj.setint(obj.inta);',
  13326. ' with Tbird do begin',
  13327. ' FX:=FY+1;',
  13328. ' inta:=inta+2;',
  13329. ' intb:=intb+3;',
  13330. ' end;',
  13331. ' with Obj do begin',
  13332. ' FX:=FY+1;',
  13333. ' inta:=inta+2;',
  13334. ' intb:=intb+3;',
  13335. ' end;',
  13336. '']);
  13337. ConvertProgram;
  13338. CheckSource('TestClass_Property_ClassMethod',
  13339. LinesToStr([ // statements
  13340. 'rtl.createClass(this, "TObject", null, function () {',
  13341. ' this.Fx = 0;',
  13342. ' this.Fy = 0;',
  13343. ' this.$init = function () {',
  13344. ' };',
  13345. ' this.$final = function () {',
  13346. ' };',
  13347. ' this.GetInt = function () {',
  13348. ' var Result = 0;',
  13349. ' Result = this.Fx;',
  13350. ' return Result;',
  13351. ' };',
  13352. ' this.SetInt = function (Value) {',
  13353. ' };',
  13354. '});',
  13355. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  13356. ' this.DoIt = function () {',
  13357. ' $mod.TObject.Fx = 3;',
  13358. ' $mod.TObject.Fy = this.Fx + 1;',
  13359. ' $mod.TObject.Fy = this.Fx + 1;',
  13360. ' this.SetInt(this.GetInt() + 1);',
  13361. ' this.SetInt(this.GetInt() + 1);',
  13362. ' $mod.TObject.Fx = 11;',
  13363. ' $mod.TObject.Fy = this.Fx + 12;',
  13364. ' this.SetInt(this.GetInt() + 13);',
  13365. ' };',
  13366. '});',
  13367. 'this.Obj = null;'
  13368. ]),
  13369. LinesToStr([ // $mod.$main
  13370. '$mod.TObject.Fx = $mod.TBird.Fx + 1;',
  13371. '$mod.TObject.Fy = $mod.TBird.Fx + 1;',
  13372. 'if ($mod.TBird.GetInt() === 2);',
  13373. '$mod.TBird.SetInt($mod.TBird.GetInt() + 2);',
  13374. '$mod.TBird.SetInt($mod.TBird.Fx);',
  13375. '$mod.TObject.Fy = $mod.Obj.Fx + 1;',
  13376. 'if ($mod.Obj.$class.GetInt() === 2);',
  13377. '$mod.Obj.$class.SetInt($mod.Obj.$class.GetInt() + 2);',
  13378. '$mod.Obj.$class.SetInt($mod.Obj.Fx);',
  13379. 'var $with = $mod.TBird;',
  13380. '$mod.TObject.Fx = $with.Fy + 1;',
  13381. '$mod.TObject.Fy = $with.Fx + 2;',
  13382. '$with.SetInt($with.GetInt() + 3);',
  13383. 'var $with1 = $mod.Obj;',
  13384. '$mod.TObject.Fx = $with1.Fy + 1;',
  13385. '$mod.TObject.Fy = $with1.Fx + 2;',
  13386. '$with1.$class.SetInt($with1.$class.GetInt() + 3);',
  13387. '']));
  13388. end;
  13389. procedure TTestModule.TestClass_Property_Indexed;
  13390. begin
  13391. StartProgram(false);
  13392. Add([
  13393. 'type',
  13394. ' TObject = class',
  13395. ' FItems: array of longint;',
  13396. ' function GetItems(Index: longint): longint;',
  13397. ' procedure SetItems(Index: longint; Value: longint);',
  13398. ' procedure DoIt;',
  13399. ' property Items[Index: longint]: longint read getitems write setitems;',
  13400. ' end;',
  13401. 'function tobject.getitems(index: longint): longint;',
  13402. 'begin',
  13403. ' Result:=fitems[index];',
  13404. 'end;',
  13405. 'procedure tobject.setitems(index: longint; value: longint);',
  13406. 'begin',
  13407. ' fitems[index]:=value;',
  13408. 'end;',
  13409. 'procedure tobject.doit;',
  13410. 'begin',
  13411. ' items[1]:=2;',
  13412. ' items[3]:=items[4];',
  13413. ' self.items[5]:=self.items[6];',
  13414. ' items[items[7]]:=items[items[8]];',
  13415. 'end;',
  13416. 'var Obj: tobject;',
  13417. 'begin',
  13418. ' obj.Items[11]:=obj.Items[12];',
  13419. '']);
  13420. ConvertProgram;
  13421. CheckSource('TestClass_Property_Indexed',
  13422. LinesToStr([ // statements
  13423. 'rtl.createClass(this, "TObject", null, function () {',
  13424. ' this.$init = function () {',
  13425. ' this.FItems = [];',
  13426. ' };',
  13427. ' this.$final = function () {',
  13428. ' this.FItems = undefined;',
  13429. ' };',
  13430. ' this.GetItems = function (Index) {',
  13431. ' var Result = 0;',
  13432. ' Result = this.FItems[Index];',
  13433. ' return Result;',
  13434. ' };',
  13435. ' this.SetItems = function (Index, Value) {',
  13436. ' this.FItems[Index] = Value;',
  13437. ' };',
  13438. ' this.DoIt = function () {',
  13439. ' this.SetItems(1, 2);',
  13440. ' this.SetItems(3,this.GetItems(4));',
  13441. ' this.SetItems(5,this.GetItems(6));',
  13442. ' this.SetItems(this.GetItems(7), this.GetItems(this.GetItems(8)));',
  13443. ' };',
  13444. '});',
  13445. 'this.Obj = null;'
  13446. ]),
  13447. LinesToStr([ // $mod.$main
  13448. '$mod.Obj.SetItems(11,$mod.Obj.GetItems(12));'
  13449. ]));
  13450. end;
  13451. procedure TTestModule.TestClass_Property_IndexSpec;
  13452. begin
  13453. StartProgram(false);
  13454. Add([
  13455. 'type',
  13456. ' TEnum = (red, blue);',
  13457. ' TObject = class',
  13458. ' function GetIntBool(Index: longint): boolean; virtual; abstract;',
  13459. ' procedure SetIntBool(Index: longint; b: boolean); virtual; abstract;',
  13460. ' function GetEnumBool(Index: TEnum): boolean; virtual; abstract;',
  13461. ' procedure SetEnumBool(Index: TEnum; b: boolean); virtual; abstract;',
  13462. ' function GetStrIntBool(A: String; I: longint): boolean; virtual; abstract;',
  13463. ' procedure SetStrIntBool(A: String; I: longint; b: boolean); virtual; abstract;',
  13464. ' property B1: boolean index 1 read GetIntBool write SetIntBool;',
  13465. ' property B2: boolean index TEnum.blue read GetEnumBool write SetEnumBool;',
  13466. ' property B3: boolean index ord(red) read GetIntBool write SetIntBool;',
  13467. ' property I1[A: String]: boolean index ord(blue) read GetStrIntBool write SetStrIntBool;',
  13468. ' end;',
  13469. 'procedure DoIt(b: boolean); begin end;',
  13470. 'var',
  13471. ' o: TObject;',
  13472. 'begin',
  13473. ' o.B1:=o.B1;',
  13474. ' o.B2:=o.B2;',
  13475. ' o.B3:=o.B3;',
  13476. ' o.I1[''a'']:=o.I1[''b''];',
  13477. ' doit(o.b1);',
  13478. ' doit(o.b2);',
  13479. ' doit(o.i1[''c'']);',
  13480. '']);
  13481. ConvertProgram;
  13482. CheckSource('TestClass_Property_IndexSpec',
  13483. LinesToStr([ // statements
  13484. 'this.TEnum = {',
  13485. ' "0": "red",',
  13486. ' red: 0,',
  13487. ' "1": "blue",',
  13488. ' blue: 1',
  13489. '};',
  13490. 'rtl.createClass(this, "TObject", null, function () {',
  13491. ' this.$init = function () {',
  13492. ' };',
  13493. ' this.$final = function () {',
  13494. ' };',
  13495. '});',
  13496. 'this.DoIt = function (b) {',
  13497. '};',
  13498. 'this.o = null;',
  13499. '']),
  13500. LinesToStr([ // $mod.$main
  13501. '$mod.o.SetIntBool(1, $mod.o.GetIntBool(1));',
  13502. '$mod.o.SetEnumBool($mod.TEnum.blue, $mod.o.GetEnumBool($mod.TEnum.blue));',
  13503. '$mod.o.SetIntBool(0, $mod.o.GetIntBool(0));',
  13504. '$mod.o.SetStrIntBool("a", 1, $mod.o.GetStrIntBool("b", 1));',
  13505. '$mod.DoIt($mod.o.GetIntBool(1));',
  13506. '$mod.DoIt($mod.o.GetEnumBool($mod.TEnum.blue));',
  13507. '$mod.DoIt($mod.o.GetStrIntBool("c", 1));',
  13508. '']));
  13509. end;
  13510. procedure TTestModule.TestClass_PropertyOfTypeArray;
  13511. begin
  13512. StartProgram(false);
  13513. Add('type');
  13514. Add(' TArray = array of longint;');
  13515. Add(' TObject = class');
  13516. Add(' FItems: TArray;');
  13517. Add(' function GetItems: tarray;');
  13518. Add(' procedure SetItems(Value: tarray);');
  13519. Add(' property Items: tarray read getitems write setitems;');
  13520. Add(' procedure SetNumbers(const Value: tarray);');
  13521. Add(' property Numbers: tarray write setnumbers;');
  13522. Add(' end;');
  13523. Add('function tobject.getitems: tarray;');
  13524. Add('begin');
  13525. Add(' Result:=fitems;');
  13526. Add('end;');
  13527. Add('procedure tobject.setitems(value: tarray);');
  13528. Add('begin');
  13529. Add(' fitems:=value;');
  13530. Add(' fitems:=nil;');
  13531. Add(' Items:=nil;');
  13532. Add(' Items:=Items;');
  13533. Add(' Items[1]:=2;');
  13534. Add(' fitems[3]:=Items[4];');
  13535. Add(' Items[5]:=Items[6];');
  13536. Add(' Self.Items[7]:=8;');
  13537. Add(' Self.Items[9]:=Self.Items[10];');
  13538. Add(' Items[Items[11]]:=Items[Items[12]];');
  13539. Add('end;');
  13540. Add('procedure tobject.SetNumbers(const Value: tarray);');
  13541. Add('begin;');
  13542. Add(' Numbers:=nil;');
  13543. Add(' Numbers:=Value;');
  13544. Add(' Self.Numbers:=Value;');
  13545. Add('end;');
  13546. Add('var Obj: tobject;');
  13547. Add('begin');
  13548. Add(' obj.items:=nil;');
  13549. Add(' obj.items:=obj.items;');
  13550. Add(' obj.items[11]:=obj.items[12];');
  13551. ConvertProgram;
  13552. CheckSource('TestClass_PropertyOfTypeArray',
  13553. LinesToStr([ // statements
  13554. 'rtl.createClass(this, "TObject", null, function () {',
  13555. ' this.$init = function () {',
  13556. ' this.FItems = [];',
  13557. ' };',
  13558. ' this.$final = function () {',
  13559. ' this.FItems = undefined;',
  13560. ' };',
  13561. ' this.GetItems = function () {',
  13562. ' var Result = [];',
  13563. ' Result = rtl.arrayRef(this.FItems);',
  13564. ' return Result;',
  13565. ' };',
  13566. ' this.SetItems = function (Value) {',
  13567. ' this.FItems = rtl.arrayRef(Value);',
  13568. ' this.FItems = [];',
  13569. ' this.SetItems([]);',
  13570. ' this.SetItems(rtl.arrayRef(this.GetItems()));',
  13571. ' this.GetItems()[1] = 2;',
  13572. ' this.FItems[3] = this.GetItems()[4];',
  13573. ' this.GetItems()[5] = this.GetItems()[6];',
  13574. ' this.GetItems()[7] = 8;',
  13575. ' this.GetItems()[9] = this.GetItems()[10];',
  13576. ' this.GetItems()[this.GetItems()[11]] = this.GetItems()[this.GetItems()[12]];',
  13577. ' };',
  13578. ' this.SetNumbers = function (Value) {',
  13579. ' this.SetNumbers([]);',
  13580. ' this.SetNumbers(Value);',
  13581. ' this.SetNumbers(Value);',
  13582. ' };',
  13583. '});',
  13584. 'this.Obj = null;'
  13585. ]),
  13586. LinesToStr([ // $mod.$main
  13587. '$mod.Obj.SetItems([]);',
  13588. '$mod.Obj.SetItems($mod.Obj.GetItems());',
  13589. '$mod.Obj.GetItems()[11] = $mod.Obj.GetItems()[12];'
  13590. ]));
  13591. end;
  13592. procedure TTestModule.TestClass_PropertyDefault;
  13593. begin
  13594. StartProgram(false);
  13595. Add([
  13596. 'type',
  13597. ' TArray = array of longint;',
  13598. ' TObject = class',
  13599. ' end;',
  13600. ' TBird = class',
  13601. ' FItems: TArray;',
  13602. ' function GetItems(Index: longint): longint;',
  13603. ' procedure SetItems(Index, Value: longint);',
  13604. ' property Items[Index: longint]: longint read getitems write setitems; default;',
  13605. ' end;',
  13606. 'function TBird.getitems(index: longint): longint;',
  13607. 'begin',
  13608. 'end;',
  13609. 'procedure TBird.setitems(index, value: longint);',
  13610. 'begin',
  13611. ' Self[1]:=2;',
  13612. ' Self[3]:=Self[index];',
  13613. ' Self[index]:=Self[Self[value]];',
  13614. ' Self[Self[4]]:=value;',
  13615. 'end;',
  13616. 'var',
  13617. ' Bird: TBird;',
  13618. ' Obj: TObject;',
  13619. 'begin',
  13620. ' bird[11]:=12;',
  13621. ' bird[13]:=bird[14];',
  13622. ' bird[Bird[15]]:=bird[Bird[15]];',
  13623. ' TBird(obj)[16]:=TBird(obj)[17];',
  13624. ' (obj as tbird)[18]:=19;',
  13625. '']);
  13626. ConvertProgram;
  13627. CheckSource('TestClass_PropertyDefault',
  13628. LinesToStr([ // statements
  13629. 'rtl.createClass(this, "TObject", null, function () {',
  13630. ' this.$init = function () {',
  13631. ' };',
  13632. ' this.$final = function () {',
  13633. ' };',
  13634. '});',
  13635. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  13636. ' this.$init = function () {',
  13637. ' $mod.TObject.$init.call(this);',
  13638. ' this.FItems = [];',
  13639. ' };',
  13640. ' this.$final = function () {',
  13641. ' this.FItems = undefined;',
  13642. ' $mod.TObject.$final.call(this);',
  13643. ' };',
  13644. ' this.GetItems = function (Index) {',
  13645. ' var Result = 0;',
  13646. ' return Result;',
  13647. ' };',
  13648. ' this.SetItems = function (Index, Value) {',
  13649. ' this.SetItems(1, 2);',
  13650. ' this.SetItems(3, this.GetItems(Index));',
  13651. ' this.SetItems(Index, this.GetItems(this.GetItems(Value)));',
  13652. ' this.SetItems(this.GetItems(4), Value);',
  13653. ' };',
  13654. '});',
  13655. 'this.Bird = null;',
  13656. 'this.Obj = null;',
  13657. '']),
  13658. LinesToStr([ // $mod.$main
  13659. '$mod.Bird.SetItems(11, 12);',
  13660. '$mod.Bird.SetItems(13, $mod.Bird.GetItems(14));',
  13661. '$mod.Bird.SetItems($mod.Bird.GetItems(15), $mod.Bird.GetItems($mod.Bird.GetItems(15)));',
  13662. '$mod.Obj.SetItems(16, $mod.Obj.GetItems(17));',
  13663. 'rtl.as($mod.Obj, $mod.TBird).SetItems(18, 19);',
  13664. '']));
  13665. end;
  13666. procedure TTestModule.TestClass_PropertyDefault_TypecastToOtherDefault;
  13667. begin
  13668. StartProgram(false);
  13669. Add([
  13670. 'type',
  13671. ' TObject = class end;',
  13672. ' TAlphaList = class',
  13673. ' function GetAlphas(Index: boolean): Pointer; virtual; abstract;',
  13674. ' procedure SetAlphas(Index: boolean; Value: Pointer); virtual; abstract;',
  13675. ' property Alphas[Index: boolean]: Pointer read getAlphas write setAlphas; default;',
  13676. ' end;',
  13677. ' TBetaList = class',
  13678. ' function GetBetas(Index: longint): Pointer; virtual; abstract;',
  13679. ' procedure SetBetas(Index: longint; Value: Pointer); virtual; abstract;',
  13680. ' property Betas[Index: longint]: Pointer read getBetas write setBetas; default;',
  13681. ' end;',
  13682. ' TBird = class',
  13683. ' procedure DoIt;',
  13684. ' end;',
  13685. 'procedure TBird.DoIt;',
  13686. 'var',
  13687. ' List: TAlphaList;',
  13688. 'begin',
  13689. ' if TBetaList(List[true])[3]=nil then ;',
  13690. ' TBetaList(List[false])[5]:=nil;',
  13691. 'end;',
  13692. 'var',
  13693. ' List: TAlphaList;',
  13694. 'begin',
  13695. ' if TBetaList(List[true])[3]=nil then ;',
  13696. ' TBetaList(List[false])[5]:=nil;',
  13697. '']);
  13698. ConvertProgram;
  13699. CheckSource('TestClass_PropertyDefault_TypecastToOtherDefault',
  13700. LinesToStr([ // statements
  13701. 'rtl.createClass(this, "TObject", null, function () {',
  13702. ' this.$init = function () {',
  13703. ' };',
  13704. ' this.$final = function () {',
  13705. ' };',
  13706. '});',
  13707. 'rtl.createClass(this, "TAlphaList", this.TObject, function () {',
  13708. '});',
  13709. 'rtl.createClass(this, "TBetaList", this.TObject, function () {',
  13710. '});',
  13711. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  13712. ' this.DoIt = function () {',
  13713. ' var List = null;',
  13714. ' if (List.GetAlphas(true).GetBetas(3) === null) ;',
  13715. ' List.GetAlphas(false).SetBetas(5, null);',
  13716. ' };',
  13717. '});',
  13718. 'this.List = null;',
  13719. '']),
  13720. LinesToStr([ // $mod.$main
  13721. 'if ($mod.List.GetAlphas(true).GetBetas(3) === null) ;',
  13722. '$mod.List.GetAlphas(false).SetBetas(5, null);',
  13723. '']));
  13724. end;
  13725. procedure TTestModule.TestClass_PropertyOverride;
  13726. begin
  13727. StartProgram(false);
  13728. Add('type');
  13729. Add(' integer = longint;');
  13730. Add(' TObject = class');
  13731. Add(' FItem: integer;');
  13732. Add(' function GetItem: integer; external name ''GetItem'';');
  13733. Add(' procedure SetItem(Value: integer); external name ''SetItem'';');
  13734. Add(' property Item: integer read getitem write setitem;');
  13735. Add(' end;');
  13736. Add(' TCar = class');
  13737. Add(' FBag: integer;');
  13738. Add(' function GetBag: integer; external name ''GetBag'';');
  13739. Add(' property Item read getbag;');
  13740. Add(' end;');
  13741. Add('var');
  13742. Add(' Obj: tobject;');
  13743. Add(' Car: tcar;');
  13744. Add('begin');
  13745. Add(' Obj.Item:=Obj.Item;');
  13746. Add(' Car.Item:=Car.Item;');
  13747. ConvertProgram;
  13748. CheckSource('TestClass_PropertyOverride',
  13749. LinesToStr([ // statements
  13750. 'rtl.createClass(this, "TObject", null, function () {',
  13751. ' this.$init = function () {',
  13752. ' this.FItem = 0;',
  13753. ' };',
  13754. ' this.$final = function () {',
  13755. ' };',
  13756. '});',
  13757. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  13758. ' this.$init = function () {',
  13759. ' $mod.TObject.$init.call(this);',
  13760. ' this.FBag = 0;',
  13761. ' };',
  13762. '});',
  13763. 'this.Obj = null;',
  13764. 'this.Car = null;',
  13765. '']),
  13766. LinesToStr([ // $mod.$main
  13767. '$mod.Obj.SetItem($mod.Obj.GetItem());',
  13768. '$mod.Car.SetItem($mod.Car.GetBag());',
  13769. '']));
  13770. end;
  13771. procedure TTestModule.TestClass_PropertyIncVisibility;
  13772. begin
  13773. AddModuleWithIntfImplSrc('unit1.pp',
  13774. LinesToStr([
  13775. 'type',
  13776. ' TNumber = longint;',
  13777. ' TInteger = longint;',
  13778. ' TObject = class',
  13779. ' private',
  13780. ' function GetItems(Index: TNumber): TInteger; virtual; abstract;',
  13781. ' procedure SetItems(Index: TInteger; Value: TNumber); virtual; abstract;',
  13782. ' protected',
  13783. ' property Items[Index: TNumber]: longint read GetItems write SetItems;',
  13784. ' end;']),
  13785. LinesToStr([
  13786. '']));
  13787. StartProgram(true);
  13788. Add([
  13789. 'uses unit1;',
  13790. 'type',
  13791. ' TBird = class',
  13792. ' public',
  13793. ' property Items;',
  13794. ' end;',
  13795. 'procedure DoIt(i: TInteger);',
  13796. 'begin',
  13797. 'end;',
  13798. 'var b: TBird;',
  13799. 'begin',
  13800. ' b.Items[1]:=2;',
  13801. ' b.Items[3]:=b.Items[4];',
  13802. ' DoIt(b.Items[5]);',
  13803. '']);
  13804. ConvertProgram;
  13805. CheckSource('TestClass_PropertyIncVisibility',
  13806. LinesToStr([ // statements
  13807. 'rtl.createClass(this, "TBird", pas.unit1.TObject, function () {',
  13808. '});',
  13809. 'this.DoIt = function (i) {',
  13810. '};',
  13811. 'this.b = null;'
  13812. ]),
  13813. LinesToStr([ // $mod.$main
  13814. '$mod.b.SetItems(1, 2);',
  13815. '$mod.b.SetItems(3, $mod.b.GetItems(4));',
  13816. '$mod.DoIt($mod.b.GetItems(5));'
  13817. ]));
  13818. end;
  13819. procedure TTestModule.TestClass_Assigned;
  13820. begin
  13821. StartProgram(false);
  13822. Add('type');
  13823. Add(' TObject = class');
  13824. Add(' end;');
  13825. Add('var');
  13826. Add(' Obj: tobject;');
  13827. Add(' b: boolean;');
  13828. Add('begin');
  13829. Add(' if Assigned(obj) then ;');
  13830. Add(' b:=Assigned(obj) or false;');
  13831. ConvertProgram;
  13832. CheckSource('TestClass_Assigned',
  13833. LinesToStr([ // statements
  13834. 'rtl.createClass(this, "TObject", null, function () {',
  13835. ' this.$init = function () {',
  13836. ' };',
  13837. ' this.$final = function () {',
  13838. ' };',
  13839. '});',
  13840. 'this.Obj = null;',
  13841. 'this.b = false;'
  13842. ]),
  13843. LinesToStr([ // $mod.$main
  13844. 'if ($mod.Obj != null);',
  13845. '$mod.b = ($mod.Obj != null) || false;'
  13846. ]));
  13847. end;
  13848. procedure TTestModule.TestClass_WithClassDoCreate;
  13849. begin
  13850. StartProgram(false);
  13851. Add('type');
  13852. Add(' TObject = class');
  13853. Add(' aBool: boolean;');
  13854. Add(' Arr: array of boolean;');
  13855. Add(' constructor Create;');
  13856. Add(' end;');
  13857. Add('constructor TObject.Create; begin end;');
  13858. Add('var');
  13859. Add(' Obj: tobject;');
  13860. Add(' b: boolean;');
  13861. Add('begin');
  13862. Add(' with tobject.create do begin');
  13863. Add(' b:=abool;');
  13864. Add(' abool:=b;');
  13865. Add(' b:=arr[1];');
  13866. Add(' arr[2]:=b;');
  13867. Add(' end;');
  13868. Add(' with tobject do');
  13869. Add(' obj:=create;');
  13870. Add(' with obj do begin');
  13871. Add(' create;');
  13872. Add(' b:=abool;');
  13873. Add(' abool:=b;');
  13874. Add(' b:=arr[3];');
  13875. Add(' arr[4]:=b;');
  13876. Add(' end;');
  13877. ConvertProgram;
  13878. CheckSource('TestClass_WithClassDoCreate',
  13879. LinesToStr([ // statements
  13880. 'rtl.createClass(this, "TObject", null, function () {',
  13881. ' this.$init = function () {',
  13882. ' this.aBool = false;',
  13883. ' this.Arr = [];',
  13884. ' };',
  13885. ' this.$final = function () {',
  13886. ' this.Arr = undefined;',
  13887. ' };',
  13888. ' this.Create = function () {',
  13889. ' return this;',
  13890. ' };',
  13891. '});',
  13892. 'this.Obj = null;',
  13893. 'this.b = false;'
  13894. ]),
  13895. LinesToStr([ // $mod.$main
  13896. 'var $with = $mod.TObject.$create("Create");',
  13897. '$mod.b = $with.aBool;',
  13898. '$with.aBool = $mod.b;',
  13899. '$mod.b = $with.Arr[1];',
  13900. '$with.Arr[2] = $mod.b;',
  13901. 'var $with1 = $mod.TObject;',
  13902. '$mod.Obj = $with1.$create("Create");',
  13903. 'var $with2 = $mod.Obj;',
  13904. '$with2.Create();',
  13905. '$mod.b = $with2.aBool;',
  13906. '$with2.aBool = $mod.b;',
  13907. '$mod.b = $with2.Arr[3];',
  13908. '$with2.Arr[4] = $mod.b;',
  13909. '']));
  13910. end;
  13911. procedure TTestModule.TestClass_WithClassInstDoProperty;
  13912. begin
  13913. StartProgram(false);
  13914. Add('type');
  13915. Add(' TObject = class');
  13916. Add(' FInt: longint;');
  13917. Add(' constructor Create;');
  13918. Add(' function GetSize: longint;');
  13919. Add(' procedure SetSize(Value: longint);');
  13920. Add(' property Int: longint read FInt write FInt;');
  13921. Add(' property Size: longint read GetSize write SetSize;');
  13922. Add(' end;');
  13923. Add('constructor TObject.Create; begin end;');
  13924. Add('function TObject.GetSize: longint; begin; end;');
  13925. Add('procedure TObject.SetSize(Value: longint); begin; end;');
  13926. Add('var');
  13927. Add(' Obj: tobject;');
  13928. Add(' i: longint;');
  13929. Add('begin');
  13930. Add(' with TObject.Create do begin');
  13931. Add(' i:=int;');
  13932. Add(' int:=i;');
  13933. Add(' i:=size;');
  13934. Add(' size:=i;');
  13935. Add(' end;');
  13936. Add(' with obj do begin');
  13937. Add(' i:=int;');
  13938. Add(' int:=i;');
  13939. Add(' i:=size;');
  13940. Add(' size:=i;');
  13941. Add(' end;');
  13942. ConvertProgram;
  13943. CheckSource('TestClass_WithClassInstDoProperty',
  13944. LinesToStr([ // statements
  13945. 'rtl.createClass(this, "TObject", null, function () {',
  13946. ' this.$init = function () {',
  13947. ' this.FInt = 0;',
  13948. ' };',
  13949. ' this.$final = function () {',
  13950. ' };',
  13951. ' this.Create = function () {',
  13952. ' return this;',
  13953. ' };',
  13954. ' this.GetSize = function () {',
  13955. ' var Result = 0;',
  13956. ' return Result;',
  13957. ' };',
  13958. ' this.SetSize = function (Value) {',
  13959. ' };',
  13960. '});',
  13961. 'this.Obj = null;',
  13962. 'this.i = 0;'
  13963. ]),
  13964. LinesToStr([ // $mod.$main
  13965. 'var $with = $mod.TObject.$create("Create");',
  13966. '$mod.i = $with.FInt;',
  13967. '$with.FInt = $mod.i;',
  13968. '$mod.i = $with.GetSize();',
  13969. '$with.SetSize($mod.i);',
  13970. 'var $with1 = $mod.Obj;',
  13971. '$mod.i = $with1.FInt;',
  13972. '$with1.FInt = $mod.i;',
  13973. '$mod.i = $with1.GetSize();',
  13974. '$with1.SetSize($mod.i);',
  13975. '']));
  13976. end;
  13977. procedure TTestModule.TestClass_WithClassInstDoPropertyWithParams;
  13978. begin
  13979. StartProgram(false);
  13980. Add('type');
  13981. Add(' TObject = class');
  13982. Add(' constructor Create;');
  13983. Add(' function GetItems(Index: longint): longint;');
  13984. Add(' procedure SetItems(Index, Value: longint);');
  13985. Add(' property Items[Index: longint]: longint read GetItems write SetItems;');
  13986. Add(' end;');
  13987. Add('constructor TObject.Create; begin end;');
  13988. Add('function tobject.getitems(index: longint): longint; begin; end;');
  13989. Add('procedure tobject.setitems(index, value: longint); begin; end;');
  13990. Add('var');
  13991. Add(' Obj: tobject;');
  13992. Add(' i: longint;');
  13993. Add('begin');
  13994. Add(' with TObject.Create do begin');
  13995. Add(' i:=Items[1];');
  13996. Add(' Items[2]:=i;');
  13997. Add(' end;');
  13998. Add(' with obj do begin');
  13999. Add(' i:=Items[3];');
  14000. Add(' Items[4]:=i;');
  14001. Add(' end;');
  14002. ConvertProgram;
  14003. CheckSource('TestClass_WithClassInstDoPropertyWithParams',
  14004. LinesToStr([ // statements
  14005. 'rtl.createClass(this, "TObject", null, function () {',
  14006. ' this.$init = function () {',
  14007. ' };',
  14008. ' this.$final = function () {',
  14009. ' };',
  14010. ' this.Create = function () {',
  14011. ' return this;',
  14012. ' };',
  14013. ' this.GetItems = function (Index) {',
  14014. ' var Result = 0;',
  14015. ' return Result;',
  14016. ' };',
  14017. ' this.SetItems = function (Index, Value) {',
  14018. ' };',
  14019. '});',
  14020. 'this.Obj = null;',
  14021. 'this.i = 0;'
  14022. ]),
  14023. LinesToStr([ // $mod.$main
  14024. 'var $with = $mod.TObject.$create("Create");',
  14025. '$mod.i = $with.GetItems(1);',
  14026. '$with.SetItems(2, $mod.i);',
  14027. 'var $with1 = $mod.Obj;',
  14028. '$mod.i = $with1.GetItems(3);',
  14029. '$with1.SetItems(4, $mod.i);',
  14030. '']));
  14031. end;
  14032. procedure TTestModule.TestClass_WithClassInstDoFunc;
  14033. begin
  14034. StartProgram(false);
  14035. Add('type');
  14036. Add(' TObject = class');
  14037. Add(' constructor Create;');
  14038. Add(' function GetSize: longint;');
  14039. Add(' procedure SetSize(Value: longint);');
  14040. Add(' end;');
  14041. Add('constructor TObject.Create; begin end;');
  14042. Add('function TObject.GetSize: longint; begin; end;');
  14043. Add('procedure TObject.SetSize(Value: longint); begin; end;');
  14044. Add('var');
  14045. Add(' Obj: tobject;');
  14046. Add(' i: longint;');
  14047. Add('begin');
  14048. Add(' with TObject.Create do begin');
  14049. Add(' i:=GetSize;');
  14050. Add(' i:=GetSize();');
  14051. Add(' SetSize(i);');
  14052. Add(' end;');
  14053. Add(' with obj do begin');
  14054. Add(' i:=GetSize;');
  14055. Add(' i:=GetSize();');
  14056. Add(' SetSize(i);');
  14057. Add(' end;');
  14058. ConvertProgram;
  14059. CheckSource('TestClass_WithClassInstDoFunc',
  14060. LinesToStr([ // statements
  14061. 'rtl.createClass(this, "TObject", null, function () {',
  14062. ' this.$init = function () {',
  14063. ' };',
  14064. ' this.$final = function () {',
  14065. ' };',
  14066. ' this.Create = function () {',
  14067. ' return this;',
  14068. ' };',
  14069. ' this.GetSize = function () {',
  14070. ' var Result = 0;',
  14071. ' return Result;',
  14072. ' };',
  14073. ' this.SetSize = function (Value) {',
  14074. ' };',
  14075. '});',
  14076. 'this.Obj = null;',
  14077. 'this.i = 0;'
  14078. ]),
  14079. LinesToStr([ // $mod.$main
  14080. 'var $with = $mod.TObject.$create("Create");',
  14081. '$mod.i = $with.GetSize();',
  14082. '$mod.i = $with.GetSize();',
  14083. '$with.SetSize($mod.i);',
  14084. 'var $with1 = $mod.Obj;',
  14085. '$mod.i = $with1.GetSize();',
  14086. '$mod.i = $with1.GetSize();',
  14087. '$with1.SetSize($mod.i);',
  14088. '']));
  14089. end;
  14090. procedure TTestModule.TestClass_TypeCast;
  14091. begin
  14092. StartProgram(false);
  14093. Add('type');
  14094. Add(' TObject = class');
  14095. Add(' Next: TObject;');
  14096. Add(' constructor Create;');
  14097. Add(' end;');
  14098. Add(' TControl = class(TObject)');
  14099. Add(' Arr: array of TObject;');
  14100. Add(' function GetIt(vI: longint = 0): TObject;');
  14101. Add(' end;');
  14102. Add('constructor tobject.create; begin end;');
  14103. Add('function tcontrol.getit(vi: longint = 0): tobject; begin end;');
  14104. Add('var');
  14105. Add(' Obj: tobject;');
  14106. Add('begin');
  14107. Add(' obj:=tcontrol(obj).next;');
  14108. Add(' tcontrol(obj):=nil;');
  14109. Add(' obj:=tcontrol(obj);');
  14110. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit);');
  14111. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit());');
  14112. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit(1));');
  14113. Add(' tcontrol(obj):=tcontrol(tcontrol(tcontrol(obj).getit).arr[2]);');
  14114. Add(' obj:=tcontrol(nil);');
  14115. ConvertProgram;
  14116. CheckSource('TestClass_TypeCast',
  14117. LinesToStr([ // statements
  14118. 'rtl.createClass(this, "TObject", null, function () {',
  14119. ' this.$init = function () {',
  14120. ' this.Next = null;',
  14121. ' };',
  14122. ' this.$final = function () {',
  14123. ' this.Next = undefined;',
  14124. ' };',
  14125. ' this.Create = function () {',
  14126. ' return this;',
  14127. ' };',
  14128. '});',
  14129. 'rtl.createClass(this, "TControl", this.TObject, function () {',
  14130. ' this.$init = function () {',
  14131. ' $mod.TObject.$init.call(this);',
  14132. ' this.Arr = [];',
  14133. ' };',
  14134. ' this.$final = function () {',
  14135. ' this.Arr = undefined;',
  14136. ' $mod.TObject.$final.call(this);',
  14137. ' };',
  14138. ' this.GetIt = function (vI) {',
  14139. ' var Result = null;',
  14140. ' return Result;',
  14141. ' };',
  14142. '});',
  14143. 'this.Obj = null;'
  14144. ]),
  14145. LinesToStr([ // $mod.$main
  14146. '$mod.Obj = $mod.Obj.Next;',
  14147. '$mod.Obj = null;',
  14148. '$mod.Obj = $mod.Obj;',
  14149. '$mod.Obj = $mod.Obj.GetIt(0);',
  14150. '$mod.Obj = $mod.Obj.GetIt(0);',
  14151. '$mod.Obj = $mod.Obj.GetIt(1);',
  14152. '$mod.Obj = $mod.Obj.GetIt(0).Arr[2];',
  14153. '$mod.Obj = null;',
  14154. '']));
  14155. end;
  14156. procedure TTestModule.TestClass_TypeCastUntypedParam;
  14157. begin
  14158. StartProgram(false);
  14159. Add('type');
  14160. Add(' TObject = class end;');
  14161. Add('procedure ProcA(var A);');
  14162. Add('begin');
  14163. Add(' TObject(A):=nil;');
  14164. Add(' TObject(A):=TObject(A);');
  14165. Add(' if TObject(A)=nil then ;');
  14166. Add(' if nil=TObject(A) then ;');
  14167. Add('end;');
  14168. Add('procedure ProcB(out A);');
  14169. Add('begin');
  14170. Add(' TObject(A):=nil;');
  14171. Add(' TObject(A):=TObject(A);');
  14172. Add(' if TObject(A)=nil then ;');
  14173. Add(' if nil=TObject(A) then ;');
  14174. Add('end;');
  14175. Add('procedure ProcC(const A);');
  14176. Add('begin');
  14177. Add(' if TObject(A)=nil then ;');
  14178. Add(' if nil=TObject(A) then ;');
  14179. Add('end;');
  14180. Add('var o: TObject;');
  14181. Add('begin');
  14182. Add(' ProcA(o);');
  14183. Add(' ProcB(o);');
  14184. Add(' ProcC(o);');
  14185. ConvertProgram;
  14186. CheckSource('TestClass_TypeCastUntypedParam',
  14187. LinesToStr([ // statements
  14188. 'rtl.createClass(this, "TObject", null, function () {',
  14189. ' this.$init = function () {',
  14190. ' };',
  14191. ' this.$final = function () {',
  14192. ' };',
  14193. '});',
  14194. 'this.ProcA = function (A) {',
  14195. ' A.set(null);',
  14196. ' A.set(A.get());',
  14197. ' if (A.get() === null);',
  14198. ' if (null === A.get());',
  14199. '};',
  14200. 'this.ProcB = function (A) {',
  14201. ' A.set(null);',
  14202. ' A.set(A.get());',
  14203. ' if (A.get() === null);',
  14204. ' if (null === A.get());',
  14205. '};',
  14206. 'this.ProcC = function (A) {',
  14207. ' if (A === null);',
  14208. ' if (null === A);',
  14209. '};',
  14210. 'this.o = null;',
  14211. '']),
  14212. LinesToStr([ // $mod.$main
  14213. '$mod.ProcA({',
  14214. ' p: $mod,',
  14215. ' get: function () {',
  14216. ' return this.p.o;',
  14217. ' },',
  14218. ' set: function (v) {',
  14219. ' this.p.o = v;',
  14220. ' }',
  14221. '});',
  14222. '$mod.ProcB({',
  14223. ' p: $mod,',
  14224. ' get: function () {',
  14225. ' return this.p.o;',
  14226. ' },',
  14227. ' set: function (v) {',
  14228. ' this.p.o = v;',
  14229. ' }',
  14230. '});',
  14231. '$mod.ProcC($mod.o);',
  14232. '']));
  14233. end;
  14234. procedure TTestModule.TestClass_Overloads;
  14235. begin
  14236. StartProgram(false);
  14237. Add('type');
  14238. Add(' TObject = class');
  14239. Add(' procedure DoIt;');
  14240. Add(' procedure DoIt(vI: longint);');
  14241. Add(' end;');
  14242. Add('procedure TObject.DoIt;');
  14243. Add('begin');
  14244. Add(' DoIt;');
  14245. Add(' DoIt(1);');
  14246. Add('end;');
  14247. Add('procedure TObject.DoIt(vI: longint); begin end;');
  14248. Add('begin');
  14249. ConvertProgram;
  14250. CheckSource('TestClass_Overloads',
  14251. LinesToStr([ // statements
  14252. 'rtl.createClass(this, "TObject", null, function () {',
  14253. ' this.$init = function () {',
  14254. ' };',
  14255. ' this.$final = function () {',
  14256. ' };',
  14257. ' this.DoIt = function () {',
  14258. ' this.DoIt();',
  14259. ' this.DoIt$1(1);',
  14260. ' };',
  14261. ' this.DoIt$1 = function (vI) {',
  14262. ' };',
  14263. '});',
  14264. '']),
  14265. LinesToStr([ // $mod.$main
  14266. '']));
  14267. end;
  14268. procedure TTestModule.TestClass_OverloadsAncestor;
  14269. begin
  14270. StartProgram(false);
  14271. Add('type');
  14272. Add(' TObject = class;');
  14273. Add(' TObject = class');
  14274. Add(' procedure DoIt(vA: longint);');
  14275. Add(' procedure DoIt(vA, vB: longint);');
  14276. Add(' end;');
  14277. Add(' TCar = class;');
  14278. Add(' TCar = class');
  14279. Add(' procedure DoIt(vA: longint);');
  14280. Add(' procedure DoIt(vA, vB: longint);');
  14281. Add(' end;');
  14282. Add('procedure tobject.doit(va: longint);');
  14283. Add('begin');
  14284. Add(' doit(1);');
  14285. Add(' doit(1,2);');
  14286. Add('end;');
  14287. Add('procedure tobject.doit(va, vb: longint); begin end;');
  14288. Add('procedure tcar.doit(va: longint);');
  14289. Add('begin');
  14290. Add(' doit(1);');
  14291. Add(' doit(1,2);');
  14292. Add(' inherited doit(1);');
  14293. Add(' inherited doit(1,2);');
  14294. Add('end;');
  14295. Add('procedure tcar.doit(va, vb: longint); begin end;');
  14296. Add('begin');
  14297. ConvertProgram;
  14298. CheckSource('TestClass_OverloadsAncestor',
  14299. LinesToStr([ // statements
  14300. 'rtl.createClass(this, "TObject", null, function () {',
  14301. ' this.$init = function () {',
  14302. ' };',
  14303. ' this.$final = function () {',
  14304. ' };',
  14305. ' this.DoIt = function (vA) {',
  14306. ' this.DoIt(1);',
  14307. ' this.DoIt$1(1,2);',
  14308. ' };',
  14309. ' this.DoIt$1 = function (vA, vB) {',
  14310. ' };',
  14311. '});',
  14312. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  14313. ' this.DoIt$2 = function (vA) {',
  14314. ' this.DoIt$2(1);',
  14315. ' this.DoIt$3(1, 2);',
  14316. ' $mod.TObject.DoIt.call(this, 1);',
  14317. ' $mod.TObject.DoIt$1.call(this, 1, 2);',
  14318. ' };',
  14319. ' this.DoIt$3 = function (vA, vB) {',
  14320. ' };',
  14321. '});',
  14322. '']),
  14323. LinesToStr([ // $mod.$main
  14324. '']));
  14325. end;
  14326. procedure TTestModule.TestClass_OverloadConstructor;
  14327. begin
  14328. StartProgram(false);
  14329. Add('type');
  14330. Add(' TObject = class');
  14331. Add(' constructor Create(vA: longint);');
  14332. Add(' constructor Create(vA, vB: longint);');
  14333. Add(' end;');
  14334. Add(' TCar = class');
  14335. Add(' constructor Create(vA: longint);');
  14336. Add(' constructor Create(vA, vB: longint);');
  14337. Add(' end;');
  14338. Add('constructor tobject.create(va: longint);');
  14339. Add('begin');
  14340. Add(' create(1);');
  14341. Add(' create(1,2);');
  14342. Add('end;');
  14343. Add('constructor tobject.create(va, vb: longint); begin end;');
  14344. Add('constructor tcar.create(va: longint);');
  14345. Add('begin');
  14346. Add(' create(1);');
  14347. Add(' create(1,2);');
  14348. Add(' inherited create(1);');
  14349. Add(' inherited create(1,2);');
  14350. Add('end;');
  14351. Add('constructor tcar.create(va, vb: longint); begin end;');
  14352. Add('begin');
  14353. Add(' tobject.create(1);');
  14354. Add(' tobject.create(1,2);');
  14355. Add(' tcar.create(1);');
  14356. Add(' tcar.create(1,2);');
  14357. ConvertProgram;
  14358. CheckSource('TestClass_OverloadConstructor',
  14359. LinesToStr([ // statements
  14360. 'rtl.createClass(this, "TObject", null, function () {',
  14361. ' this.$init = function () {',
  14362. ' };',
  14363. ' this.$final = function () {',
  14364. ' };',
  14365. ' this.Create = function (vA) {',
  14366. ' this.Create(1);',
  14367. ' this.Create$1(1,2);',
  14368. ' return this;',
  14369. ' };',
  14370. ' this.Create$1 = function (vA, vB) {',
  14371. ' return this;',
  14372. ' };',
  14373. '});',
  14374. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  14375. ' this.Create$2 = function (vA) {',
  14376. ' this.Create$2(1);',
  14377. ' this.Create$3(1, 2);',
  14378. ' $mod.TObject.Create.call(this, 1);',
  14379. ' $mod.TObject.Create$1.call(this, 1, 2);',
  14380. ' return this;',
  14381. ' };',
  14382. ' this.Create$3 = function (vA, vB) {',
  14383. ' return this;',
  14384. ' };',
  14385. '});',
  14386. '']),
  14387. LinesToStr([ // $mod.$main
  14388. '$mod.TObject.$create("Create", [1]);',
  14389. '$mod.TObject.$create("Create$1", [1, 2]);',
  14390. '$mod.TCar.$create("Create$2", [1]);',
  14391. '$mod.TCar.$create("Create$3", [1, 2]);',
  14392. '']));
  14393. end;
  14394. procedure TTestModule.TestClass_OverloadDelphiOverride;
  14395. begin
  14396. StartProgram(false);
  14397. Add([
  14398. '{$mode delphi}',
  14399. 'type',
  14400. ' TObject = class end;',
  14401. ' TBird = class',
  14402. ' function {#a}GetValue: longint; overload; virtual;',
  14403. ' function {#b}GetValue(AValue: longint): longint; overload; virtual;',
  14404. ' end;',
  14405. ' TEagle = class(TBird)',
  14406. ' function {#c}GetValue: longint; overload; override;',
  14407. ' function {#d}GetValue(AValue: longint): longint; overload; override;',
  14408. ' end;',
  14409. 'function TBird.GetValue: longint;',
  14410. 'begin',
  14411. ' if 3={@a}GetValue then ;',
  14412. ' if 4={@b}GetValue(5) then ;',
  14413. 'end;',
  14414. 'function TBird.GetValue(AValue: longint): longint;',
  14415. 'begin',
  14416. 'end;',
  14417. 'function TEagle.GetValue: longint;',
  14418. 'begin',
  14419. ' if 13={@c}GetValue then ;',
  14420. ' if 14={@d}GetValue(15) then ;',
  14421. ' if 15=inherited {@a}GetValue then ;',
  14422. ' if 16=inherited {@b}GetValue(17) then ;',
  14423. 'end;',
  14424. 'function TEagle.GetValue(AValue: longint): longint;',
  14425. 'begin',
  14426. 'end;',
  14427. 'var',
  14428. ' e: TEagle;',
  14429. 'begin',
  14430. ' if 23=e.{@c}GetValue then ;',
  14431. ' if 24=e.{@d}GetValue(25) then ;']);
  14432. ConvertProgram;
  14433. CheckSource('TestClass_OverloadDelphiOverride',
  14434. LinesToStr([ // statements
  14435. 'rtl.createClass(this, "TObject", null, function () {',
  14436. ' this.$init = function () {',
  14437. ' };',
  14438. ' this.$final = function () {',
  14439. ' };',
  14440. '});',
  14441. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  14442. ' this.GetValue = function () {',
  14443. ' var Result = 0;',
  14444. ' if (3 === this.GetValue()) ;',
  14445. ' if (4 === this.GetValue$1(5)) ;',
  14446. ' return Result;',
  14447. ' };',
  14448. ' this.GetValue$1 = function (AValue) {',
  14449. ' var Result = 0;',
  14450. ' return Result;',
  14451. ' };',
  14452. '});',
  14453. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  14454. ' this.GetValue = function () {',
  14455. ' var Result = 0;',
  14456. ' if (13 === this.GetValue()) ;',
  14457. ' if (14 === this.GetValue$1(15)) ;',
  14458. ' if (15 === $mod.TBird.GetValue.call(this)) ;',
  14459. ' if (16 === $mod.TBird.GetValue$1.call(this, 17)) ;',
  14460. ' return Result;',
  14461. ' };',
  14462. ' this.GetValue$1 = function (AValue) {',
  14463. ' var Result = 0;',
  14464. ' return Result;',
  14465. ' };',
  14466. '});',
  14467. 'this.e = null;',
  14468. '']),
  14469. LinesToStr([ // $mod.$main
  14470. 'if (23 === $mod.e.GetValue()) ;',
  14471. 'if (24 === $mod.e.GetValue$1(25)) ;',
  14472. '']));
  14473. end;
  14474. procedure TTestModule.TestClass_ReintroduceVarDelphi;
  14475. begin
  14476. StartProgram(false);
  14477. Add([
  14478. '{$mode delphi}',
  14479. 'type',
  14480. ' TObject = class end;',
  14481. ' TAnimal = class',
  14482. ' public',
  14483. ' {#animal_a}A: longint;',
  14484. ' function {#animal_b}B: longint;',
  14485. ' end;',
  14486. ' TBird = class(TAnimal)',
  14487. ' public',
  14488. ' {#bird_a}A: double;',
  14489. ' {#bird_b}B: boolean;',
  14490. ' end;',
  14491. ' TEagle = class(TBird)',
  14492. ' public',
  14493. ' function {#eagle_a}A: boolean;',
  14494. ' {#eagle_b}B: double;',
  14495. ' end;',
  14496. 'function TAnimal.B: longint;',
  14497. 'begin',
  14498. 'end;',
  14499. 'function TEagle.A: boolean;',
  14500. 'begin',
  14501. ' {@eagle_b}B:=3.3;',
  14502. ' {@eagle_a}A();',
  14503. ' TBird(Self).{@bird_b}B:=true;',
  14504. ' TAnimal(Self).{@animal_a}A:=17;',
  14505. ' inherited {@bird_b}B:=inherited {bird_a}A>1;', // Delphi allows only inherited <functionname>
  14506. 'end;',
  14507. 'var',
  14508. ' e: TEagle;',
  14509. 'begin',
  14510. ' e.{@eagle_b}B:=5.3;',
  14511. ' if e.{@eagle_a}A then ;',
  14512. '']);
  14513. ConvertProgram;
  14514. CheckSource('TestClass_ReintroduceVarDelphi',
  14515. LinesToStr([ // statements
  14516. 'rtl.createClass(this, "TObject", null, function () {',
  14517. ' this.$init = function () {',
  14518. ' };',
  14519. ' this.$final = function () {',
  14520. ' };',
  14521. '});',
  14522. 'rtl.createClass(this, "TAnimal", this.TObject, function () {',
  14523. ' this.$init = function () {',
  14524. ' $mod.TObject.$init.call(this);',
  14525. ' this.A = 0;',
  14526. ' };',
  14527. ' this.B = function () {',
  14528. ' var Result = 0;',
  14529. ' return Result;',
  14530. ' };',
  14531. '});',
  14532. 'rtl.createClass(this, "TBird", this.TAnimal, function () {',
  14533. ' this.$init = function () {',
  14534. ' $mod.TAnimal.$init.call(this);',
  14535. ' this.A$1 = 0.0;',
  14536. ' this.B$1 = false;',
  14537. ' };',
  14538. '});',
  14539. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  14540. ' this.$init = function () {',
  14541. ' $mod.TBird.$init.call(this);',
  14542. ' this.B$2 = 0.0;',
  14543. ' };',
  14544. ' this.A$2 = function () {',
  14545. ' var Result = false;',
  14546. ' this.B$2 = 3.3;',
  14547. ' this.A$2();',
  14548. ' this.B$1 = true;',
  14549. ' this.A = 17;',
  14550. ' this.B$1 = this.A$1 > 1;',
  14551. ' return Result;',
  14552. ' };',
  14553. '});',
  14554. 'this.e = null;',
  14555. '']),
  14556. LinesToStr([ // $mod.$main
  14557. '$mod.e.B$2 = 5.3;',
  14558. 'if ($mod.e.A$2()) ;',
  14559. '']));
  14560. end;
  14561. procedure TTestModule.TestClass_ReintroducedVar;
  14562. begin
  14563. StartProgram(false);
  14564. Add('type');
  14565. Add(' TObject = class');
  14566. Add(' strict private');
  14567. Add(' Some: longint;');
  14568. Add(' end;');
  14569. Add(' TMobile = class');
  14570. Add(' strict private');
  14571. Add(' Some: string;');
  14572. Add(' end;');
  14573. Add(' TCar = class(tmobile)');
  14574. Add(' procedure Some;');
  14575. Add(' procedure Some(vA: longint);');
  14576. Add(' end;');
  14577. Add('procedure tcar.some;');
  14578. Add('begin');
  14579. Add(' Some;');
  14580. Add(' Some(1);');
  14581. Add('end;');
  14582. Add('procedure tcar.some(va: longint); begin end;');
  14583. Add('begin');
  14584. ConvertProgram;
  14585. CheckSource('TestClass_ReintroducedVar',
  14586. LinesToStr([ // statements
  14587. 'rtl.createClass(this, "TObject", null, function () {',
  14588. ' this.$init = function () {',
  14589. ' this.Some = 0;',
  14590. ' };',
  14591. ' this.$final = function () {',
  14592. ' };',
  14593. '});',
  14594. 'rtl.createClass(this, "TMobile", this.TObject, function () {',
  14595. ' this.$init = function () {',
  14596. ' $mod.TObject.$init.call(this);',
  14597. ' this.Some$1 = "";',
  14598. ' };',
  14599. '});',
  14600. 'rtl.createClass(this, "TCar", this.TMobile, function () {',
  14601. ' this.Some$2 = function () {',
  14602. ' this.Some$2();',
  14603. ' this.Some$3(1);',
  14604. ' };',
  14605. ' this.Some$3 = function (vA) {',
  14606. ' };',
  14607. '});',
  14608. '']),
  14609. LinesToStr([ // $mod.$main
  14610. '']));
  14611. end;
  14612. procedure TTestModule.TestClass_RaiseDescendant;
  14613. begin
  14614. StartProgram(false);
  14615. Add([
  14616. 'type',
  14617. ' TObject = class',
  14618. ' constructor Create(Msg: string);',
  14619. ' end;',
  14620. ' Exception = class',
  14621. ' end;',
  14622. ' EConvertError = class(Exception)',
  14623. ' end;',
  14624. 'constructor TObject.Create(Msg: string); begin end;',
  14625. 'function AssertConv(Msg: string = ''def''): EConvertError; begin end;',
  14626. 'begin',
  14627. ' raise Exception.Create(''Bar1'');',
  14628. ' raise EConvertError.Create(''Bar2'');',
  14629. ' raise AssertConv(''Bar2'');',
  14630. ' raise AssertConv;',
  14631. '']);
  14632. ConvertProgram;
  14633. CheckSource('TestClass_RaiseDescendant',
  14634. LinesToStr([ // statements
  14635. 'rtl.createClass(this, "TObject", null, function () {',
  14636. ' this.$init = function () {',
  14637. ' };',
  14638. ' this.$final = function () {',
  14639. ' };',
  14640. ' this.Create = function (Msg) {',
  14641. ' return this;',
  14642. ' };',
  14643. '});',
  14644. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  14645. '});',
  14646. 'rtl.createClass(this, "EConvertError", this.Exception, function () {',
  14647. '});',
  14648. 'this.AssertConv = function (Msg) {',
  14649. ' var Result = null;',
  14650. ' return Result;',
  14651. '};',
  14652. '']),
  14653. LinesToStr([ // $mod.$main
  14654. 'throw $mod.Exception.$create("Create",["Bar1"]);',
  14655. 'throw $mod.EConvertError.$create("Create",["Bar2"]);',
  14656. 'throw $mod.AssertConv("Bar2");',
  14657. 'throw $mod.AssertConv("def");',
  14658. '']));
  14659. end;
  14660. procedure TTestModule.TestClass_ExternalMethod;
  14661. begin
  14662. AddModuleWithIntfImplSrc('unit2.pas',
  14663. LinesToStr([
  14664. 'type',
  14665. ' TObject = class',
  14666. ' public',
  14667. ' procedure Intern; external name ''$DoIntern'';',
  14668. ' end;',
  14669. '']),
  14670. LinesToStr([
  14671. '']));
  14672. StartUnit(true);
  14673. Add('interface');
  14674. Add('uses unit2;');
  14675. Add('type');
  14676. Add(' TCar = class(TObject)');
  14677. Add(' public');
  14678. Add(' procedure Intern2; external name ''$DoIntern2'';');
  14679. Add(' procedure DoIt;');
  14680. Add(' end;');
  14681. Add('implementation');
  14682. Add('procedure tcar.doit;');
  14683. Add('begin');
  14684. Add(' Intern;');
  14685. Add(' Intern();');
  14686. Add(' Intern2;');
  14687. Add(' Intern2();');
  14688. Add('end;');
  14689. Add('var Obj: TCar;');
  14690. Add('begin');
  14691. Add(' obj.intern;');
  14692. Add(' obj.intern();');
  14693. Add(' obj.intern2;');
  14694. Add(' obj.intern2();');
  14695. Add(' obj.doit;');
  14696. Add(' obj.doit();');
  14697. Add(' with obj do begin');
  14698. Add(' Intern;');
  14699. Add(' Intern();');
  14700. Add(' Intern2;');
  14701. Add(' Intern2();');
  14702. Add(' end;');
  14703. ConvertUnit;
  14704. CheckSource('TestClass_ExternalMethod',
  14705. LinesToStr([
  14706. 'var $impl = $mod.$impl;',
  14707. 'rtl.createClass(this, "TCar", pas.unit2.TObject, function () {',
  14708. ' this.DoIt = function () {',
  14709. ' this.$DoIntern();',
  14710. ' this.$DoIntern();',
  14711. ' this.$DoIntern2();',
  14712. ' this.$DoIntern2();',
  14713. ' };',
  14714. ' });',
  14715. '']),
  14716. LinesToStr([ // this.$init
  14717. '$impl.Obj.$DoIntern();',
  14718. '$impl.Obj.$DoIntern();',
  14719. '$impl.Obj.$DoIntern2();',
  14720. '$impl.Obj.$DoIntern2();',
  14721. '$impl.Obj.DoIt();',
  14722. '$impl.Obj.DoIt();',
  14723. 'var $with = $impl.Obj;',
  14724. '$with.$DoIntern();',
  14725. '$with.$DoIntern();',
  14726. '$with.$DoIntern2();',
  14727. '$with.$DoIntern2();',
  14728. '']),
  14729. LinesToStr([ // implementation
  14730. '$impl.Obj = null;',
  14731. '']) );
  14732. end;
  14733. procedure TTestModule.TestClass_ExternalVirtualNameMismatchFail;
  14734. begin
  14735. StartProgram(false);
  14736. Add('type');
  14737. Add(' TObject = class');
  14738. Add(' procedure DoIt; virtual; external name ''Foo'';');
  14739. Add(' end;');
  14740. Add('begin');
  14741. SetExpectedPasResolverError('Virtual method name must match external',
  14742. nVirtualMethodNameMustMatchExternal);
  14743. ConvertProgram;
  14744. end;
  14745. procedure TTestModule.TestClass_ExternalOverrideFail;
  14746. begin
  14747. StartProgram(false);
  14748. Add('type');
  14749. Add(' TObject = class');
  14750. Add(' procedure DoIt; virtual; external name ''DoIt'';');
  14751. Add(' end;');
  14752. Add(' TCar = class');
  14753. Add(' procedure DoIt; override; external name ''DoIt'';');
  14754. Add(' end;');
  14755. Add('begin');
  14756. SetExpectedPasResolverError('Invalid procedure modifier override,external',
  14757. nInvalidXModifierY);
  14758. ConvertProgram;
  14759. end;
  14760. procedure TTestModule.TestClass_ExternalVar;
  14761. begin
  14762. AddModuleWithIntfImplSrc('unit2.pas',
  14763. LinesToStr([
  14764. '{$modeswitch externalclass}',
  14765. 'type',
  14766. ' TObject = class',
  14767. ' public',
  14768. ' Intern: longint external name ''$Intern'';',
  14769. ' Bracket: longint external name ''["A B"]'';',
  14770. ' end;',
  14771. '']),
  14772. LinesToStr([
  14773. '']));
  14774. StartUnit(true);
  14775. Add([
  14776. 'interface',
  14777. 'uses unit2;',
  14778. '{$modeswitch externalclass}',
  14779. 'type',
  14780. ' TCar = class(tobject)',
  14781. ' public',
  14782. ' Intern2: longint external name ''$Intern2'';',
  14783. ' procedure DoIt;',
  14784. ' end;',
  14785. 'implementation',
  14786. 'procedure tcar.doit;',
  14787. 'begin',
  14788. ' Intern:=Intern+1;',
  14789. ' Intern2:=Intern2+2;',
  14790. ' Bracket:=Bracket+3;',
  14791. 'end;',
  14792. 'var Obj: TCar;',
  14793. 'begin',
  14794. ' obj.intern:=obj.intern+1;',
  14795. ' obj.intern2:=obj.intern2+2;',
  14796. ' obj.Bracket:=obj.Bracket+3;',
  14797. ' with obj do begin',
  14798. ' intern:=intern+1;',
  14799. ' intern2:=intern2+2;',
  14800. ' Bracket:=Bracket+3;',
  14801. ' end;']);
  14802. ConvertUnit;
  14803. CheckSource('TestClass_ExternalVar',
  14804. LinesToStr([
  14805. 'var $impl = $mod.$impl;',
  14806. 'rtl.createClass(this, "TCar", pas.unit2.TObject, function () {',
  14807. ' this.DoIt = function () {',
  14808. ' this.$Intern = this.$Intern + 1;',
  14809. ' this.$Intern2 = this.$Intern2 + 2;',
  14810. ' this["A B"] = this["A B"] + 3;',
  14811. ' };',
  14812. ' });',
  14813. '']),
  14814. LinesToStr([
  14815. '$impl.Obj.$Intern = $impl.Obj.$Intern + 1;',
  14816. '$impl.Obj.$Intern2 = $impl.Obj.$Intern2 + 2;',
  14817. '$impl.Obj["A B"] = $impl.Obj["A B"] + 3;',
  14818. 'var $with = $impl.Obj;',
  14819. '$with.$Intern = $with.$Intern + 1;',
  14820. '$with.$Intern2 = $with.$Intern2 + 2;',
  14821. '$with["A B"] = $with["A B"] + 3;',
  14822. '']),
  14823. LinesToStr([ // implementation
  14824. '$impl.Obj = null;',
  14825. '']));
  14826. end;
  14827. procedure TTestModule.TestClass_Const;
  14828. begin
  14829. StartProgram(false);
  14830. Add([
  14831. 'type',
  14832. ' integer = longint;',
  14833. ' TClass = class of TObject;',
  14834. ' TObject = class',
  14835. ' public',
  14836. ' const cI: integer = 3;',
  14837. ' procedure DoIt;',
  14838. ' class procedure DoMore;',
  14839. ' end;',
  14840. 'procedure tobject.doit;',
  14841. 'begin',
  14842. ' if cI=4 then;',
  14843. ' if 5=cI then;',
  14844. ' if Self.cI=6 then;',
  14845. ' if 7=Self.cI then;',
  14846. ' with Self do begin',
  14847. ' if cI=11 then;',
  14848. ' if 12=cI then;',
  14849. ' end;',
  14850. 'end;',
  14851. 'class procedure tobject.domore;',
  14852. 'begin',
  14853. ' if cI=8 then;',
  14854. ' if Self.cI=9 then;',
  14855. ' if 10=cI then;',
  14856. ' if 11=Self.cI then;',
  14857. ' with Self do begin',
  14858. ' if cI=13 then;',
  14859. ' if 14=cI then;',
  14860. ' end;',
  14861. 'end;',
  14862. 'var',
  14863. ' Obj: TObject;',
  14864. ' Cla: TClass;',
  14865. 'begin',
  14866. ' if TObject.cI=21 then ;',
  14867. ' if Obj.cI=22 then ;',
  14868. ' if Cla.cI=23 then ;',
  14869. ' with obj do if ci=24 then;',
  14870. ' with TObject do if ci=25 then;',
  14871. ' with Cla do if ci=26 then;']);
  14872. ConvertProgram;
  14873. CheckSource('TestClass_Const',
  14874. LinesToStr([
  14875. 'rtl.createClass(this, "TObject", null, function () {',
  14876. ' this.cI = 3;',
  14877. ' this.$init = function () {',
  14878. ' };',
  14879. ' this.$final = function () {',
  14880. ' };',
  14881. ' this.DoIt = function () {',
  14882. ' if (this.cI === 4) ;',
  14883. ' if (5 === this.cI) ;',
  14884. ' if (this.cI === 6) ;',
  14885. ' if (7 === this.cI) ;',
  14886. ' if (this.cI === 11) ;',
  14887. ' if (12 === this.cI) ;',
  14888. ' };',
  14889. ' this.DoMore = function () {',
  14890. ' if (this.cI === 8) ;',
  14891. ' if (this.cI === 9) ;',
  14892. ' if (10 === this.cI) ;',
  14893. ' if (11 === this.cI) ;',
  14894. ' if (this.cI === 13) ;',
  14895. ' if (14 === this.cI) ;',
  14896. ' };',
  14897. '});',
  14898. 'this.Obj = null;',
  14899. 'this.Cla = null;',
  14900. '']),
  14901. LinesToStr([
  14902. 'if ($mod.TObject.cI === 21) ;',
  14903. 'if ($mod.Obj.cI === 22) ;',
  14904. 'if ($mod.Cla.cI === 23) ;',
  14905. 'var $with = $mod.Obj;',
  14906. 'if ($with.cI === 24) ;',
  14907. 'var $with1 = $mod.TObject;',
  14908. 'if ($with1.cI === 25) ;',
  14909. 'var $with2 = $mod.Cla;',
  14910. 'if ($with2.cI === 26) ;',
  14911. '']));
  14912. end;
  14913. procedure TTestModule.TestClass_ConstEnum;
  14914. begin
  14915. StartProgram(false);
  14916. Add([
  14917. 'type',
  14918. ' TEnum = (red,blue);',
  14919. ' TObject = class',
  14920. ' end;',
  14921. ' TAnimal = class',
  14922. ' public',
  14923. ' type TSubEnum = (light,dark);',
  14924. ' const a = high(TEnum);',
  14925. ' const b = high(TSubEnum);',
  14926. ' end;',
  14927. ' TBird = class(TAnimal)',
  14928. ' public',
  14929. ' const c = high(TEnum);',
  14930. ' const d = high(TSubEnum);',
  14931. ' end;',
  14932. ' TAnt = class',
  14933. ' public',
  14934. ' const e = high(TEnum);',
  14935. ' const f = high(TBird.TSubEnum);',
  14936. ' end;',
  14937. 'begin',
  14938. '']);
  14939. ConvertProgram;
  14940. CheckSource('TestClass_ConstEnum',
  14941. LinesToStr([
  14942. 'this.TEnum = {',
  14943. ' "0": "red",',
  14944. ' red: 0,',
  14945. ' "1": "blue",',
  14946. ' blue: 1',
  14947. '};',
  14948. 'rtl.createClass(this, "TObject", null, function () {',
  14949. ' this.$init = function () {',
  14950. ' };',
  14951. ' this.$final = function () {',
  14952. ' };',
  14953. '});',
  14954. 'rtl.createClass(this, "TAnimal", this.TObject, function () {',
  14955. ' this.TSubEnum = {',
  14956. ' "0": "light",',
  14957. ' light: 0,',
  14958. ' "1": "dark",',
  14959. ' dark: 1',
  14960. ' };',
  14961. ' this.a = $mod.TEnum.blue;',
  14962. ' this.b = this.TSubEnum.dark;',
  14963. '});',
  14964. 'rtl.createClass(this, "TBird", this.TAnimal, function () {',
  14965. ' this.c = $mod.TEnum.blue;',
  14966. ' this.d = this.TSubEnum.dark;',
  14967. '});',
  14968. 'rtl.createClass(this, "TAnt", this.TObject, function () {',
  14969. ' this.e = $mod.TEnum.blue;',
  14970. ' this.f = $mod.TAnimal.TSubEnum.dark;',
  14971. '});',
  14972. '']),
  14973. LinesToStr([
  14974. '']));
  14975. end;
  14976. procedure TTestModule.TestClass_LocalConstDuplicate_Prg;
  14977. begin
  14978. StartProgram(false);
  14979. Add([
  14980. 'type',
  14981. ' TObject = class',
  14982. ' const cI: longint = 3;',
  14983. ' procedure Fly;',
  14984. ' procedure Run;',
  14985. ' end;',
  14986. ' TBird = class',
  14987. ' procedure Go;',
  14988. ' end;',
  14989. 'procedure tobject.fly;',
  14990. 'const cI: word = 4;',
  14991. 'begin',
  14992. ' if cI=Self.cI then ;',
  14993. 'end;',
  14994. 'procedure tobject.run;',
  14995. 'const cI: word = 5;',
  14996. 'begin',
  14997. ' if cI=Self.cI then ;',
  14998. 'end;',
  14999. 'procedure tbird.go;',
  15000. 'const cI: word = 6;',
  15001. 'begin',
  15002. ' if cI=Self.cI then ;',
  15003. 'end;',
  15004. 'begin',
  15005. '']);
  15006. ConvertProgram;
  15007. CheckSource('TestClass_LocalConstDuplicate_Prg',
  15008. LinesToStr([
  15009. 'rtl.createClass(this, "TObject", null, function () {',
  15010. ' this.cI = 3;',
  15011. ' this.$init = function () {',
  15012. ' };',
  15013. ' this.$final = function () {',
  15014. ' };',
  15015. ' var cI$1 = 4;',
  15016. ' this.Fly = function () {',
  15017. ' if (cI$1 === this.cI) ;',
  15018. ' };',
  15019. ' var cI$2 = 5;',
  15020. ' this.Run = function () {',
  15021. ' if (cI$2 === this.cI) ;',
  15022. ' };',
  15023. '});',
  15024. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15025. ' var cI$3 = 6;',
  15026. ' this.Go = function () {',
  15027. ' if (cI$3 === this.cI) ;',
  15028. ' };',
  15029. '});',
  15030. '']),
  15031. LinesToStr([
  15032. '']));
  15033. end;
  15034. procedure TTestModule.TestClass_LocalConstDuplicate_Unit;
  15035. begin
  15036. StartUnit(false);
  15037. Add([
  15038. 'interface',
  15039. 'type',
  15040. ' TObject = class',
  15041. ' const cI: longint = 3;',
  15042. ' procedure Fly;',
  15043. ' procedure Run;',
  15044. ' end;',
  15045. ' TBird = class',
  15046. ' procedure Go;',
  15047. ' end;',
  15048. 'implementation',
  15049. 'procedure tobject.fly;',
  15050. 'const cI: word = 4;',
  15051. 'begin',
  15052. ' if cI=Self.cI then ;',
  15053. 'end;',
  15054. 'procedure tobject.run;',
  15055. 'const cI: word = 5;',
  15056. 'begin',
  15057. ' if cI=Self.cI then ;',
  15058. 'end;',
  15059. 'procedure tbird.go;',
  15060. 'const cI: word = 6;',
  15061. 'begin',
  15062. ' if cI=Self.cI then ;',
  15063. 'end;',
  15064. '']);
  15065. ConvertUnit;
  15066. CheckSource('TestClass_LocalConstDuplicate_Unit',
  15067. LinesToStr([
  15068. 'rtl.createClass(this, "TObject", null, function () {',
  15069. ' this.cI = 3;',
  15070. ' this.$init = function () {',
  15071. ' };',
  15072. ' this.$final = function () {',
  15073. ' };',
  15074. ' var cI$1 = 4;',
  15075. ' this.Fly = function () {',
  15076. ' if (cI$1 === this.cI) ;',
  15077. ' };',
  15078. ' var cI$2 = 5;',
  15079. ' this.Run = function () {',
  15080. ' if (cI$2 === this.cI) ;',
  15081. ' };',
  15082. '});',
  15083. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15084. ' var cI$3 = 6;',
  15085. ' this.Go = function () {',
  15086. ' if (cI$3 === this.cI) ;',
  15087. ' };',
  15088. '});',
  15089. '']),
  15090. '',
  15091. '');
  15092. end;
  15093. procedure TTestModule.TestClass_LocalVarSelfFail;
  15094. begin
  15095. StartProgram(false);
  15096. Add([
  15097. 'type',
  15098. ' TObject = class',
  15099. ' constructor Create;',
  15100. ' end;',
  15101. 'constructor tobject.create;',
  15102. 'var self: longint;',
  15103. 'begin',
  15104. 'end',
  15105. 'begin',
  15106. '']);
  15107. SetExpectedPasResolverError('Duplicate identifier "self" at (0)',nDuplicateIdentifier);
  15108. ConvertProgram;
  15109. end;
  15110. procedure TTestModule.TestClass_ArgSelfFail;
  15111. begin
  15112. StartProgram(false);
  15113. Add([
  15114. 'type',
  15115. ' TObject = class',
  15116. ' procedure DoIt(Self: longint);',
  15117. ' end;',
  15118. 'procedure tobject.doit(self: longint);',
  15119. 'begin',
  15120. 'end',
  15121. 'begin',
  15122. '']);
  15123. SetExpectedPasResolverError('Duplicate identifier "Self" at test1.pp(5,24)',nDuplicateIdentifier);
  15124. ConvertProgram;
  15125. end;
  15126. procedure TTestModule.TestClass_NestedProcSelf;
  15127. begin
  15128. StartProgram(false);
  15129. Add([
  15130. 'type',
  15131. ' TObject = class',
  15132. ' Key: longint;',
  15133. ' class var State: longint;',
  15134. ' procedure DoIt;',
  15135. ' function GetSize: longint; virtual; abstract;',
  15136. ' procedure SetSize(Value: longint); virtual; abstract;',
  15137. ' property Size: longint read GetSize write SetSize;',
  15138. ' end;',
  15139. 'procedure tobject.doit;',
  15140. ' procedure Sub;',
  15141. ' begin',
  15142. ' key:=key+2;',
  15143. ' self.key:=self.key+3;',
  15144. ' state:=state+4;',
  15145. ' self.state:=self.state+5;',
  15146. ' tobject.state:=tobject.state+6;',
  15147. ' size:=size+7;',
  15148. ' self.size:=self.size+8;',
  15149. ' end;',
  15150. 'begin',
  15151. ' sub;',
  15152. ' key:=key+12;',
  15153. ' self.key:=self.key+13;',
  15154. ' state:=state+14;',
  15155. ' self.state:=self.state+15;',
  15156. ' tobject.state:=tobject.state+16;',
  15157. ' size:=size+17;',
  15158. ' self.size:=self.size+18;',
  15159. 'end;',
  15160. 'begin',
  15161. '']);
  15162. ConvertProgram;
  15163. CheckSource('TestClass_NestedProcSelf',
  15164. LinesToStr([ // statements
  15165. 'rtl.createClass(this, "TObject", null, function () {',
  15166. ' this.State = 0;',
  15167. ' this.$init = function () {',
  15168. ' this.Key = 0;',
  15169. ' };',
  15170. ' this.$final = function () {',
  15171. ' };',
  15172. ' this.DoIt = function () {',
  15173. ' var $Self = this;',
  15174. ' function Sub() {',
  15175. ' $Self.Key = $Self.Key + 2;',
  15176. ' $Self.Key = $Self.Key + 3;',
  15177. ' $mod.TObject.State = $Self.State + 4;',
  15178. ' $mod.TObject.State = $Self.State + 5;',
  15179. ' $mod.TObject.State = $mod.TObject.State + 6;',
  15180. ' $Self.SetSize($Self.GetSize() + 7);',
  15181. ' $Self.SetSize($Self.GetSize() + 8);',
  15182. ' };',
  15183. ' Sub();',
  15184. ' this.Key = this.Key + 12;',
  15185. ' $Self.Key = $Self.Key + 13;',
  15186. ' $mod.TObject.State = this.State + 14;',
  15187. ' $mod.TObject.State = $Self.State + 15;',
  15188. ' $mod.TObject.State = $mod.TObject.State + 16;',
  15189. ' this.SetSize(this.GetSize() + 17);',
  15190. ' $Self.SetSize($Self.GetSize() + 18);',
  15191. ' };',
  15192. '});',
  15193. '']),
  15194. LinesToStr([ // $mod.$main
  15195. '']));
  15196. end;
  15197. procedure TTestModule.TestClass_NestedProcSelf2;
  15198. begin
  15199. StartProgram(false);
  15200. Add([
  15201. 'type',
  15202. ' TObject = class',
  15203. ' Key: longint;',
  15204. ' class var State: longint;',
  15205. ' function GetSize: longint; virtual; abstract;',
  15206. ' procedure SetSize(Value: longint); virtual; abstract;',
  15207. ' property Size: longint read GetSize write SetSize;',
  15208. ' end;',
  15209. ' TBird = class',
  15210. ' procedure DoIt;',
  15211. ' end;',
  15212. 'procedure tbird.doit;',
  15213. ' procedure Sub;',
  15214. ' begin',
  15215. ' key:=key+2;',
  15216. ' self.key:=self.key+3;',
  15217. ' state:=state+4;',
  15218. ' self.state:=self.state+5;',
  15219. ' tobject.state:=tobject.state+6;',
  15220. ' size:=size+7;',
  15221. ' self.size:=self.size+8;',
  15222. ' end;',
  15223. 'begin',
  15224. ' sub;',
  15225. ' key:=key+12;',
  15226. ' self.key:=self.key+13;',
  15227. ' state:=state+14;',
  15228. ' self.state:=self.state+15;',
  15229. ' tobject.state:=tobject.state+16;',
  15230. ' size:=size+17;',
  15231. ' self.size:=self.size+18;',
  15232. 'end;',
  15233. 'begin',
  15234. '']);
  15235. ConvertProgram;
  15236. CheckSource('TestClass_NestedProcSelf2',
  15237. LinesToStr([ // statements
  15238. 'rtl.createClass(this, "TObject", null, function () {',
  15239. ' this.State = 0;',
  15240. ' this.$init = function () {',
  15241. ' this.Key = 0;',
  15242. ' };',
  15243. ' this.$final = function () {',
  15244. ' };',
  15245. '});',
  15246. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15247. ' this.DoIt = function () {',
  15248. ' var $Self = this;',
  15249. ' function Sub() {',
  15250. ' $Self.Key = $Self.Key + 2;',
  15251. ' $Self.Key = $Self.Key + 3;',
  15252. ' $mod.TObject.State = $Self.State + 4;',
  15253. ' $mod.TObject.State = $Self.State + 5;',
  15254. ' $mod.TObject.State = $mod.TObject.State + 6;',
  15255. ' $Self.SetSize($Self.GetSize() + 7);',
  15256. ' $Self.SetSize($Self.GetSize() + 8);',
  15257. ' };',
  15258. ' Sub();',
  15259. ' this.Key = this.Key + 12;',
  15260. ' $Self.Key = $Self.Key + 13;',
  15261. ' $mod.TObject.State = this.State + 14;',
  15262. ' $mod.TObject.State = $Self.State + 15;',
  15263. ' $mod.TObject.State = $mod.TObject.State + 16;',
  15264. ' this.SetSize(this.GetSize() + 17);',
  15265. ' $Self.SetSize($Self.GetSize() + 18);',
  15266. ' };',
  15267. '});',
  15268. '']),
  15269. LinesToStr([ // $mod.$main
  15270. '']));
  15271. end;
  15272. procedure TTestModule.TestClass_NestedProcClassSelf;
  15273. begin
  15274. StartProgram(false);
  15275. Add([
  15276. 'type',
  15277. ' TObject = class',
  15278. ' class var State: longint;',
  15279. ' class procedure DoIt;',
  15280. ' class function GetSize: longint; virtual; abstract;',
  15281. ' class procedure SetSize(Value: longint); virtual; abstract;',
  15282. ' class property Size: longint read GetSize write SetSize;',
  15283. ' end;',
  15284. 'class procedure tobject.doit;',
  15285. ' procedure Sub;',
  15286. ' begin',
  15287. ' state:=state+2;',
  15288. ' self.state:=self.state+3;',
  15289. ' tobject.state:=tobject.state+4;',
  15290. ' size:=size+5;',
  15291. ' self.size:=self.size+6;',
  15292. ' tobject.size:=tobject.size+7;',
  15293. ' end;',
  15294. 'begin',
  15295. ' sub;',
  15296. ' state:=state+12;',
  15297. ' self.state:=self.state+13;',
  15298. ' tobject.state:=tobject.state+14;',
  15299. ' size:=size+15;',
  15300. ' self.size:=self.size+16;',
  15301. ' tobject.size:=tobject.size+17;',
  15302. 'end;',
  15303. 'begin',
  15304. '']);
  15305. ConvertProgram;
  15306. CheckSource('TestClass_NestedProcClassSelf',
  15307. LinesToStr([ // statements
  15308. 'rtl.createClass(this, "TObject", null, function () {',
  15309. ' this.State = 0;',
  15310. ' this.$init = function () {',
  15311. ' };',
  15312. ' this.$final = function () {',
  15313. ' };',
  15314. ' this.DoIt = function () {',
  15315. ' var $Self = this;',
  15316. ' function Sub() {',
  15317. ' $mod.TObject.State = $Self.State + 2;',
  15318. ' $mod.TObject.State = $Self.State + 3;',
  15319. ' $mod.TObject.State = $mod.TObject.State + 4;',
  15320. ' $Self.SetSize($Self.GetSize() + 5);',
  15321. ' $Self.SetSize($Self.GetSize() + 6);',
  15322. ' $mod.TObject.SetSize($mod.TObject.GetSize() + 7);',
  15323. ' };',
  15324. ' Sub();',
  15325. ' $mod.TObject.State = this.State + 12;',
  15326. ' $mod.TObject.State = $Self.State + 13;',
  15327. ' $mod.TObject.State = $mod.TObject.State + 14;',
  15328. ' this.SetSize(this.GetSize() + 15);',
  15329. ' $Self.SetSize($Self.GetSize() + 16);',
  15330. ' $mod.TObject.SetSize($mod.TObject.GetSize() + 17);',
  15331. ' };',
  15332. '});',
  15333. '']),
  15334. LinesToStr([ // $mod.$main
  15335. '']));
  15336. end;
  15337. procedure TTestModule.TestClass_NestedProcCallInherited;
  15338. begin
  15339. StartProgram(false);
  15340. Add([
  15341. 'type',
  15342. ' TObject = class',
  15343. ' function DoIt(k: boolean): longint; virtual;',
  15344. ' end;',
  15345. ' TBird = class',
  15346. ' function DoIt(k: boolean): longint; override;',
  15347. ' end;',
  15348. 'function tobject.doit(k: boolean): longint;',
  15349. 'begin',
  15350. 'end;',
  15351. 'function tbird.doit(k: boolean): longint;',
  15352. ' procedure Sub;',
  15353. ' begin',
  15354. ' inherited DoIt(true);',
  15355. //' if inherited DoIt(false)=4 then ;',
  15356. ' end;',
  15357. 'begin',
  15358. ' Sub;',
  15359. ' inherited;',
  15360. ' inherited DoIt(true);',
  15361. //' if inherited DoIt(false)=14 then ;',
  15362. 'end;',
  15363. 'begin',
  15364. '']);
  15365. ConvertProgram;
  15366. CheckSource('TestClass_NestedProcCallInherited',
  15367. LinesToStr([ // statements
  15368. 'rtl.createClass(this, "TObject", null, function () {',
  15369. ' this.$init = function () {',
  15370. ' };',
  15371. ' this.$final = function () {',
  15372. ' };',
  15373. ' this.DoIt = function (k) {',
  15374. ' var Result = 0;',
  15375. ' return Result;',
  15376. ' };',
  15377. '});',
  15378. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15379. ' this.DoIt = function (k) {',
  15380. ' var $Self = this;',
  15381. ' var Result = 0;',
  15382. ' function Sub() {',
  15383. ' $mod.TObject.DoIt.call($Self, true);',
  15384. ' };',
  15385. ' Sub();',
  15386. ' $mod.TObject.DoIt.apply(this, arguments);',
  15387. ' $mod.TObject.DoIt.call(this, true);',
  15388. ' return Result;',
  15389. ' };',
  15390. '});',
  15391. '']),
  15392. LinesToStr([ // $mod.$main
  15393. '']));
  15394. end;
  15395. procedure TTestModule.TestClass_TObjectFree;
  15396. begin
  15397. StartProgram(false);
  15398. Add([
  15399. 'type',
  15400. ' TObject = class',
  15401. ' Obj: tobject;',
  15402. ' procedure Free;',
  15403. ' procedure Release;',
  15404. ' end;',
  15405. 'procedure tobject.free;',
  15406. 'begin',
  15407. 'end;',
  15408. 'procedure tobject.release;',
  15409. 'begin',
  15410. ' free;',
  15411. ' if true then free;',
  15412. 'end;',
  15413. 'function DoIt(o: tobject): tobject;',
  15414. 'var l: tobject;',
  15415. 'begin',
  15416. ' o.free;',
  15417. ' o.free();',
  15418. ' l.free;',
  15419. ' l.free();',
  15420. ' o.obj.free;',
  15421. ' o.obj.free();',
  15422. ' with o do obj.free;',
  15423. ' with o do obj.free();',
  15424. ' result.Free;',
  15425. ' result.Free();',
  15426. 'end;',
  15427. 'var o: tobject;',
  15428. ' a: array of tobject;',
  15429. 'begin',
  15430. ' o.free;',
  15431. ' o.obj.free;',
  15432. ' a[1+2].free;',
  15433. '']);
  15434. ConvertProgram;
  15435. CheckSource('TestClass_TObjectFree',
  15436. LinesToStr([ // statements
  15437. 'rtl.createClass(this, "TObject", null, function () {',
  15438. ' this.$init = function () {',
  15439. ' this.Obj = null;',
  15440. ' };',
  15441. ' this.$final = function () {',
  15442. ' this.Obj = undefined;',
  15443. ' };',
  15444. ' this.Free = function () {',
  15445. ' };',
  15446. ' this.Release = function () {',
  15447. ' this.Free();',
  15448. ' if (true) this.Free();',
  15449. ' };',
  15450. '});',
  15451. 'this.DoIt = function (o) {',
  15452. ' var Result = null;',
  15453. ' var l = null;',
  15454. ' o = rtl.freeLoc(o);',
  15455. ' o = rtl.freeLoc(o);',
  15456. ' l = rtl.freeLoc(l);',
  15457. ' l = rtl.freeLoc(l);',
  15458. ' rtl.free(o, "Obj");',
  15459. ' rtl.free(o, "Obj");',
  15460. ' rtl.free(o, "Obj");',
  15461. ' rtl.free(o, "Obj");',
  15462. ' Result = rtl.freeLoc(Result);',
  15463. ' Result = rtl.freeLoc(Result);',
  15464. ' return Result;',
  15465. '};',
  15466. 'this.o = null;',
  15467. 'this.a = [];',
  15468. '']),
  15469. LinesToStr([ // $mod.$main
  15470. 'rtl.free($mod, "o");',
  15471. 'rtl.free($mod.o, "Obj");',
  15472. 'rtl.free($mod.a, 1 + 2);',
  15473. '']));
  15474. end;
  15475. procedure TTestModule.TestClass_TObjectFree_VarArg;
  15476. begin
  15477. StartProgram(false);
  15478. Add([
  15479. 'type',
  15480. ' TObject = class',
  15481. ' Obj: tobject;',
  15482. ' procedure Free;',
  15483. ' end;',
  15484. 'procedure tobject.free;',
  15485. 'begin',
  15486. 'end;',
  15487. 'procedure DoIt(var o: tobject);',
  15488. 'begin',
  15489. ' o.free;',
  15490. ' o.free();',
  15491. 'end;',
  15492. 'begin',
  15493. '']);
  15494. ConvertProgram;
  15495. CheckSource('TestClass_TObjectFree_VarArg',
  15496. LinesToStr([ // statements
  15497. 'rtl.createClass(this, "TObject", null, function () {',
  15498. ' this.$init = function () {',
  15499. ' this.Obj = null;',
  15500. ' };',
  15501. ' this.$final = function () {',
  15502. ' this.Obj = undefined;',
  15503. ' };',
  15504. ' this.Free = function () {',
  15505. ' };',
  15506. '});',
  15507. 'this.DoIt = function (o) {',
  15508. ' o.set(rtl.freeLoc(o.get()));',
  15509. ' o.set(rtl.freeLoc(o.get()));',
  15510. '};',
  15511. '']),
  15512. LinesToStr([ // $mod.$main
  15513. '']));
  15514. end;
  15515. procedure TTestModule.TestClass_TObjectFreeNewInstance;
  15516. begin
  15517. StartProgram(false);
  15518. Add([
  15519. 'type',
  15520. ' TObject = class',
  15521. ' constructor Create;',
  15522. ' procedure Free;',
  15523. ' end;',
  15524. 'constructor TObject.Create; begin end;',
  15525. 'procedure tobject.free; begin end;',
  15526. 'begin',
  15527. ' with tobject.create do free;',
  15528. '']);
  15529. ConvertProgram;
  15530. CheckSource('TestClass_TObjectFreeNewInstance',
  15531. LinesToStr([ // statements
  15532. 'rtl.createClass(this, "TObject", null, function () {',
  15533. ' this.$init = function () {',
  15534. ' };',
  15535. ' this.$final = function () {',
  15536. ' };',
  15537. ' this.Create = function () {',
  15538. ' return this;',
  15539. ' };',
  15540. ' this.Free = function () {',
  15541. ' };',
  15542. '});',
  15543. '']),
  15544. LinesToStr([ // $mod.$main
  15545. 'var $with = $mod.TObject.$create("Create");',
  15546. '$with=rtl.freeLoc($with);',
  15547. '']));
  15548. end;
  15549. procedure TTestModule.TestClass_TObjectFreeLowerCase;
  15550. begin
  15551. StartProgram(false);
  15552. Add([
  15553. 'type',
  15554. ' TObject = class',
  15555. ' destructor Destroy;',
  15556. ' procedure Free;',
  15557. ' end;',
  15558. 'destructor TObject.Destroy; begin end;',
  15559. 'procedure tobject.free; begin end;',
  15560. 'var o: tobject;',
  15561. 'begin',
  15562. ' o.free;',
  15563. '']);
  15564. Converter.UseLowerCase:=true;
  15565. ConvertProgram;
  15566. CheckSource('TestClass_TObjectFreeLowerCase',
  15567. LinesToStr([ // statements
  15568. 'rtl.createClass(this, "tobject", null, function () {',
  15569. ' this.$init = function () {',
  15570. ' };',
  15571. ' this.$final = function () {',
  15572. ' };',
  15573. ' rtl.tObjectDestroy = "destroy";',
  15574. ' this.destroy = function () {',
  15575. ' };',
  15576. ' this.free = function () {',
  15577. ' };',
  15578. '});',
  15579. 'this.o = null;',
  15580. '']),
  15581. LinesToStr([ // $mod.$main
  15582. 'rtl.free($mod, "o");',
  15583. '']));
  15584. end;
  15585. procedure TTestModule.TestClass_TObjectFreeFunctionFail;
  15586. begin
  15587. StartProgram(false);
  15588. Add([
  15589. 'type',
  15590. ' TObject = class',
  15591. ' procedure Free;',
  15592. ' function GetObj: tobject; virtual; abstract;',
  15593. ' end;',
  15594. 'procedure tobject.free;',
  15595. 'begin',
  15596. 'end;',
  15597. 'var o: tobject;',
  15598. 'begin',
  15599. ' o.getobj.free;',
  15600. '']);
  15601. SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
  15602. ConvertProgram;
  15603. end;
  15604. procedure TTestModule.TestClass_TObjectFreePropertyFail;
  15605. begin
  15606. StartProgram(false);
  15607. Add([
  15608. 'type',
  15609. ' TObject = class',
  15610. ' procedure Free;',
  15611. ' FObj: TObject;',
  15612. ' property Obj: tobject read FObj write FObj;',
  15613. ' end;',
  15614. 'procedure tobject.free;',
  15615. 'begin',
  15616. 'end;',
  15617. 'var o: tobject;',
  15618. 'begin',
  15619. ' o.obj.free;',
  15620. '']);
  15621. SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
  15622. ConvertProgram;
  15623. end;
  15624. procedure TTestModule.TestClass_ForIn;
  15625. begin
  15626. StartProgram(false);
  15627. Add([
  15628. 'type',
  15629. ' TObject = class end;',
  15630. ' TItem = TObject;',
  15631. ' TEnumerator = class',
  15632. ' FCurrent: TItem;',
  15633. ' property Current: TItem read FCurrent;',
  15634. ' function MoveNext: boolean;',
  15635. ' end;',
  15636. ' TBird = class',
  15637. ' function GetEnumerator: TEnumerator;',
  15638. ' end;',
  15639. 'function TEnumerator.MoveNext: boolean;',
  15640. 'begin',
  15641. 'end;',
  15642. 'function TBird.GetEnumerator: TEnumerator;',
  15643. 'begin',
  15644. 'end;',
  15645. 'var',
  15646. ' b: TBird;',
  15647. ' i, i2: TItem;',
  15648. 'begin',
  15649. ' for i in b do i2:=i;']);
  15650. ConvertProgram;
  15651. CheckSource('TestClass_ForIn',
  15652. LinesToStr([ // statements
  15653. 'rtl.createClass(this, "TObject", null, function () {',
  15654. ' this.$init = function () {',
  15655. ' };',
  15656. ' this.$final = function () {',
  15657. ' };',
  15658. '});',
  15659. 'rtl.createClass(this, "TEnumerator", this.TObject, function () {',
  15660. ' this.$init = function () {',
  15661. ' $mod.TObject.$init.call(this);',
  15662. ' this.FCurrent = null;',
  15663. ' };',
  15664. ' this.$final = function () {',
  15665. ' this.FCurrent = undefined;',
  15666. ' $mod.TObject.$final.call(this);',
  15667. ' };',
  15668. ' this.MoveNext = function () {',
  15669. ' var Result = false;',
  15670. ' return Result;',
  15671. ' };',
  15672. '});',
  15673. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15674. ' this.GetEnumerator = function () {',
  15675. ' var Result = null;',
  15676. ' return Result;',
  15677. ' };',
  15678. '});',
  15679. 'this.b = null;',
  15680. 'this.i = null;',
  15681. 'this.i2 = null;'
  15682. ]),
  15683. LinesToStr([ // $mod.$main
  15684. 'var $in = $mod.b.GetEnumerator();',
  15685. 'try {',
  15686. ' while ($in.MoveNext()){',
  15687. ' $mod.i = $in.FCurrent;',
  15688. ' $mod.i2 = $mod.i;',
  15689. ' }',
  15690. '} finally {',
  15691. ' $in = rtl.freeLoc($in)',
  15692. '};',
  15693. '']));
  15694. end;
  15695. procedure TTestModule.TestClass_DispatchMessage;
  15696. begin
  15697. StartProgram(false);
  15698. Add([
  15699. 'type',
  15700. ' TObject = class',
  15701. ' {$DispatchField DispInt}',
  15702. ' procedure Dispatch(var Msg); virtual; abstract;',
  15703. ' {$DispatchStrField DispStr}',
  15704. ' procedure DispatchStr(var Msg); virtual; abstract;',
  15705. ' end;',
  15706. ' THopMsg = record',
  15707. ' DispInt: longint;',
  15708. ' end;',
  15709. ' TPutMsg = record',
  15710. ' DispStr: string;',
  15711. ' end;',
  15712. ' TBird = class',
  15713. ' procedure Fly(var Msg); virtual; abstract; message 2;',
  15714. ' procedure Run; overload; virtual; abstract;',
  15715. ' procedure Run(var Msg); overload; message ''Fast'';',
  15716. ' procedure Hop(var Msg: THopMsg); virtual; abstract; message 3;',
  15717. ' procedure Put(var Msg: TPutMsg); virtual; abstract; message ''foo'';',
  15718. ' end;',
  15719. 'procedure TBird.Run(var Msg);',
  15720. 'begin',
  15721. 'end;',
  15722. 'begin',
  15723. '']);
  15724. ConvertProgram;
  15725. CheckSource('TestClass_Message',
  15726. LinesToStr([ // statements
  15727. 'rtl.createClass(this, "TObject", null, function () {',
  15728. ' this.$init = function () {',
  15729. ' };',
  15730. ' this.$final = function () {',
  15731. ' };',
  15732. '});',
  15733. 'rtl.recNewT(this, "THopMsg", function () {',
  15734. ' this.DispInt = 0;',
  15735. ' this.$eq = function (b) {',
  15736. ' return this.DispInt === b.DispInt;',
  15737. ' };',
  15738. ' this.$assign = function (s) {',
  15739. ' this.DispInt = s.DispInt;',
  15740. ' return this;',
  15741. ' };',
  15742. '});',
  15743. 'rtl.recNewT(this, "TPutMsg", function () {',
  15744. ' this.DispStr = "";',
  15745. ' this.$eq = function (b) {',
  15746. ' return this.DispStr === b.DispStr;',
  15747. ' };',
  15748. ' this.$assign = function (s) {',
  15749. ' this.DispStr = s.DispStr;',
  15750. ' return this;',
  15751. ' };',
  15752. '});',
  15753. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15754. ' this.Run$1 = function (Msg) {',
  15755. ' };',
  15756. ' this.$msgint = {',
  15757. ' "2": "Fly",',
  15758. ' "3": "Hop"',
  15759. ' };',
  15760. ' this.$msgstr = {',
  15761. ' Fast: "Run$1",',
  15762. ' foo: "Put"',
  15763. ' };',
  15764. '});',
  15765. '']),
  15766. LinesToStr([ // $mod.$main
  15767. '']));
  15768. end;
  15769. procedure TTestModule.TestClass_Message_DuplicateIntFail;
  15770. begin
  15771. StartProgram(false);
  15772. Add([
  15773. 'type',
  15774. ' TObject = class',
  15775. ' procedure Fly(var Msg); virtual; abstract; message 3;',
  15776. ' procedure Run(var Msg); virtual; abstract; message 1+2;',
  15777. ' end;',
  15778. 'begin',
  15779. '']);
  15780. SetExpectedPasResolverError('Duplicate message id "3" at test1.pp(5,56)',nDuplicateMessageIdXAtY);
  15781. ConvertProgram;
  15782. end;
  15783. procedure TTestModule.TestClass_DispatchMessage_WrongFieldNameFail;
  15784. begin
  15785. StartProgram(false);
  15786. Add([
  15787. 'type',
  15788. ' TObject = class',
  15789. ' {$dispatchfield Msg}',
  15790. ' procedure Dispatch(var Msg); virtual; abstract;',
  15791. ' end;',
  15792. ' TFlyMsg = record',
  15793. ' FlyId: longint;',
  15794. ' end;',
  15795. ' TBird = class',
  15796. ' procedure Fly(var Msg: TFlyMsg); virtual; abstract; message 3;',
  15797. ' end;',
  15798. 'begin',
  15799. '']);
  15800. ConvertProgram;
  15801. CheckHint(mtWarning,nDispatchRequiresX,'Dispatch requires record field "Msg"');
  15802. end;
  15803. procedure TTestModule.TestClassOf_Create;
  15804. begin
  15805. StartProgram(false);
  15806. Add('type');
  15807. Add(' TObject = class');
  15808. Add(' constructor Create;');
  15809. Add(' end;');
  15810. Add(' TClass = class of TObject;');
  15811. Add('constructor tobject.create; begin end;');
  15812. Add('var');
  15813. Add(' Obj: tobject;');
  15814. Add(' C: tclass;');
  15815. Add('begin');
  15816. Add(' obj:=C.create;');
  15817. Add(' with c do obj:=create;');
  15818. ConvertProgram;
  15819. CheckSource('TestClassOf_Create',
  15820. LinesToStr([ // statements
  15821. 'rtl.createClass(this, "TObject", null, function () {',
  15822. ' this.$init = function () {',
  15823. ' };',
  15824. ' this.$final = function () {',
  15825. ' };',
  15826. ' this.Create = function () {',
  15827. ' return this;',
  15828. ' };',
  15829. '});',
  15830. 'this.Obj = null;',
  15831. 'this.C = null;'
  15832. ]),
  15833. LinesToStr([ // $mod.$main
  15834. '$mod.Obj = $mod.C.$create("Create");',
  15835. 'var $with = $mod.C;',
  15836. '$mod.Obj = $with.$create("Create");',
  15837. '']));
  15838. end;
  15839. procedure TTestModule.TestClassOf_Call;
  15840. begin
  15841. StartProgram(false);
  15842. Add('type');
  15843. Add(' TObject = class');
  15844. Add(' class procedure DoIt;');
  15845. Add(' end;');
  15846. Add(' TClass = class of TObject;');
  15847. Add('class procedure tobject.doit; begin end;');
  15848. Add('var');
  15849. Add(' C: tclass;');
  15850. Add('begin');
  15851. Add(' c.doit;');
  15852. Add(' with c do doit;');
  15853. ConvertProgram;
  15854. CheckSource('TestClassOf_Call',
  15855. LinesToStr([ // statements
  15856. 'rtl.createClass(this, "TObject", null, function () {',
  15857. ' this.$init = function () {',
  15858. ' };',
  15859. ' this.$final = function () {',
  15860. ' };',
  15861. ' this.DoIt = function () {',
  15862. ' };',
  15863. '});',
  15864. 'this.C = null;'
  15865. ]),
  15866. LinesToStr([ // $mod.$main
  15867. '$mod.C.DoIt();',
  15868. 'var $with = $mod.C;',
  15869. '$with.DoIt();',
  15870. '']));
  15871. end;
  15872. procedure TTestModule.TestClassOf_Assign;
  15873. begin
  15874. StartProgram(false);
  15875. Add('type');
  15876. Add(' TClass = class of TObject;');
  15877. Add(' TObject = class');
  15878. Add(' ClassType: TClass; ');
  15879. Add(' end;');
  15880. Add('var');
  15881. Add(' Obj: tobject;');
  15882. Add(' C: tclass;');
  15883. Add('begin');
  15884. Add(' c:=nil;');
  15885. Add(' c:=obj.classtype;');
  15886. ConvertProgram;
  15887. CheckSource('TestClassOf_Assign',
  15888. LinesToStr([ // statements
  15889. 'rtl.createClass(this, "TObject", null, function () {',
  15890. ' this.$init = function () {',
  15891. ' this.ClassType = null;',
  15892. ' };',
  15893. ' this.$final = function () {',
  15894. ' this.ClassType = undefined;',
  15895. ' };',
  15896. '});',
  15897. 'this.Obj = null;',
  15898. 'this.C = null;'
  15899. ]),
  15900. LinesToStr([ // $mod.$main
  15901. '$mod.C = null;',
  15902. '$mod.C = $mod.Obj.ClassType;',
  15903. '']));
  15904. end;
  15905. procedure TTestModule.TestClassOf_Is;
  15906. begin
  15907. StartProgram(false);
  15908. Add('type');
  15909. Add(' TClass = class of TObject;');
  15910. Add(' TObject = class');
  15911. Add(' end;');
  15912. Add(' TCar = class');
  15913. Add(' end;');
  15914. Add(' TCars = class of TCar;');
  15915. Add('var');
  15916. Add(' Obj: tobject;');
  15917. Add(' C: tclass;');
  15918. Add(' Cars: tcars;');
  15919. Add('begin');
  15920. Add(' if c is tcar then ;');
  15921. Add(' if c is tcars then ;');
  15922. ConvertProgram;
  15923. CheckSource('TestClassOf_Is',
  15924. LinesToStr([ // statements
  15925. 'rtl.createClass(this, "TObject", null, function () {',
  15926. ' this.$init = function () {',
  15927. ' };',
  15928. ' this.$final = function () {',
  15929. ' };',
  15930. '});',
  15931. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  15932. '});',
  15933. 'this.Obj = null;',
  15934. 'this.C = null;',
  15935. 'this.Cars = null;'
  15936. ]),
  15937. LinesToStr([ // $mod.$main
  15938. 'if(rtl.is($mod.C,$mod.TCar));',
  15939. 'if(rtl.is($mod.C,$mod.TCar));',
  15940. '']));
  15941. end;
  15942. procedure TTestModule.TestClassOf_Compare;
  15943. begin
  15944. StartProgram(false);
  15945. Add('type');
  15946. Add(' TClass = class of TObject;');
  15947. Add(' TObject = class');
  15948. Add(' ClassType: TClass; ');
  15949. Add(' end;');
  15950. Add('var');
  15951. Add(' b: boolean;');
  15952. Add(' Obj: tobject;');
  15953. Add(' C: tclass;');
  15954. Add('begin');
  15955. Add(' b:=c=nil;');
  15956. Add(' b:=nil=c;');
  15957. Add(' b:=c=obj.classtype;');
  15958. Add(' b:=obj.classtype=c;');
  15959. Add(' b:=c=TObject;');
  15960. Add(' b:=TObject=c;');
  15961. Add(' b:=c<>nil;');
  15962. Add(' b:=nil<>c;');
  15963. Add(' b:=c<>obj.classtype;');
  15964. Add(' b:=obj.classtype<>c;');
  15965. Add(' b:=c<>TObject;');
  15966. Add(' b:=TObject<>c;');
  15967. ConvertProgram;
  15968. CheckSource('TestClassOf_Compare',
  15969. LinesToStr([ // statements
  15970. 'rtl.createClass(this, "TObject", null, function () {',
  15971. ' this.$init = function () {',
  15972. ' this.ClassType = null;',
  15973. ' };',
  15974. ' this.$final = function () {',
  15975. ' this.ClassType = undefined;',
  15976. ' };',
  15977. '});',
  15978. 'this.b = false;',
  15979. 'this.Obj = null;',
  15980. 'this.C = null;'
  15981. ]),
  15982. LinesToStr([ // $mod.$main
  15983. '$mod.b = $mod.C === null;',
  15984. '$mod.b = null === $mod.C;',
  15985. '$mod.b = $mod.C === $mod.Obj.ClassType;',
  15986. '$mod.b = $mod.Obj.ClassType === $mod.C;',
  15987. '$mod.b = $mod.C === $mod.TObject;',
  15988. '$mod.b = $mod.TObject === $mod.C;',
  15989. '$mod.b = $mod.C !== null;',
  15990. '$mod.b = null !== $mod.C;',
  15991. '$mod.b = $mod.C !== $mod.Obj.ClassType;',
  15992. '$mod.b = $mod.Obj.ClassType !== $mod.C;',
  15993. '$mod.b = $mod.C !== $mod.TObject;',
  15994. '$mod.b = $mod.TObject !== $mod.C;',
  15995. '']));
  15996. end;
  15997. procedure TTestModule.TestClassOf_ClassVar;
  15998. begin
  15999. StartProgram(false);
  16000. Add('type');
  16001. Add(' TObject = class');
  16002. Add(' class var id: longint;');
  16003. Add(' end;');
  16004. Add(' TClass = class of TObject;');
  16005. Add('var');
  16006. Add(' C: tclass;');
  16007. Add('begin');
  16008. Add(' C.id:=C.id;');
  16009. ConvertProgram;
  16010. CheckSource('TestClassOf_ClassVar',
  16011. LinesToStr([ // statements
  16012. 'rtl.createClass(this, "TObject", null, function () {',
  16013. ' this.id = 0;',
  16014. ' this.$init = function () {',
  16015. ' };',
  16016. ' this.$final = function () {',
  16017. ' };',
  16018. '});',
  16019. 'this.C = null;'
  16020. ]),
  16021. LinesToStr([ // $mod.$main
  16022. '$mod.TObject.id = $mod.C.id;',
  16023. '']));
  16024. end;
  16025. procedure TTestModule.TestClassOf_ClassMethod;
  16026. begin
  16027. StartProgram(false);
  16028. Add('type');
  16029. Add(' TObject = class');
  16030. Add(' class function DoIt(i: longint = 0): longint;');
  16031. Add(' end;');
  16032. Add(' TClass = class of TObject;');
  16033. Add('class function tobject.doit(i: longint = 0): longint; begin end;');
  16034. Add('var');
  16035. Add(' i: longint;');
  16036. Add(' C: tclass;');
  16037. Add('begin');
  16038. Add(' C.DoIt;');
  16039. Add(' C.DoIt();');
  16040. Add(' i:=C.DoIt;');
  16041. Add(' i:=C.DoIt();');
  16042. ConvertProgram;
  16043. CheckSource('TestClassOf_ClassMethod',
  16044. LinesToStr([ // statements
  16045. 'rtl.createClass(this, "TObject", null, function () {',
  16046. ' this.$init = function () {',
  16047. ' };',
  16048. ' this.$final = function () {',
  16049. ' };',
  16050. ' this.DoIt = function (i) {',
  16051. ' var Result = 0;',
  16052. ' return Result;',
  16053. ' };',
  16054. '});',
  16055. 'this.i = 0;',
  16056. 'this.C = null;'
  16057. ]),
  16058. LinesToStr([ // $mod.$main
  16059. '$mod.C.DoIt(0);',
  16060. '$mod.C.DoIt(0);',
  16061. '$mod.i = $mod.C.DoIt(0);',
  16062. '$mod.i = $mod.C.DoIt(0);',
  16063. '']));
  16064. end;
  16065. procedure TTestModule.TestClassOf_ClassProperty;
  16066. begin
  16067. StartProgram(false);
  16068. Add([
  16069. 'type',
  16070. ' TObject = class',
  16071. ' class var FA: longint;',
  16072. ' class function GetA: longint;',
  16073. ' class procedure SetA(Value: longint);',
  16074. ' class property pA: longint read fa write fa;',
  16075. ' class property pB: longint read geta write seta;',
  16076. ' end;',
  16077. ' TObjectClass = class of tobject;',
  16078. 'class function tobject.geta: longint; begin end;',
  16079. 'class procedure tobject.seta(value: longint); begin end;',
  16080. 'var',
  16081. ' b: boolean;',
  16082. ' Obj: tobject;',
  16083. ' Cla: tobjectclass;',
  16084. 'begin',
  16085. ' obj.pa:=obj.pa;',
  16086. ' obj.pb:=obj.pb;',
  16087. ' b:=obj.pa=4;',
  16088. ' b:=obj.pb=obj.pb;',
  16089. ' b:=5=obj.pa;',
  16090. ' cla.pa:=6;',
  16091. ' cla.pa:=cla.pa;',
  16092. ' cla.pb:=cla.pb;',
  16093. ' b:=cla.pa=7;',
  16094. ' b:=cla.pb=cla.pb;',
  16095. ' b:=8=cla.pa;',
  16096. ' tobject.pa:=9;',
  16097. ' tobject.pb:=tobject.pb;',
  16098. ' b:=tobject.pa=10;',
  16099. ' b:=11=tobject.pa;',
  16100. '']);
  16101. ConvertProgram;
  16102. CheckSource('TestClassOf_ClassProperty',
  16103. LinesToStr([ // statements
  16104. 'rtl.createClass(this, "TObject", null, function () {',
  16105. ' this.FA = 0;',
  16106. ' this.$init = function () {',
  16107. ' };',
  16108. ' this.$final = function () {',
  16109. ' };',
  16110. ' this.GetA = function () {',
  16111. ' var Result = 0;',
  16112. ' return Result;',
  16113. ' };',
  16114. ' this.SetA = function (Value) {',
  16115. ' };',
  16116. '});',
  16117. 'this.b = false;',
  16118. 'this.Obj = null;',
  16119. 'this.Cla = null;'
  16120. ]),
  16121. LinesToStr([ // $mod.$main
  16122. '$mod.TObject.FA = $mod.Obj.FA;',
  16123. '$mod.Obj.$class.SetA($mod.Obj.$class.GetA());',
  16124. '$mod.b = $mod.Obj.FA === 4;',
  16125. '$mod.b = $mod.Obj.$class.GetA() === $mod.Obj.$class.GetA();',
  16126. '$mod.b = 5 === $mod.Obj.FA;',
  16127. '$mod.TObject.FA = 6;',
  16128. '$mod.TObject.FA = $mod.Cla.FA;',
  16129. '$mod.Cla.SetA($mod.Cla.GetA());',
  16130. '$mod.b = $mod.Cla.FA === 7;',
  16131. '$mod.b = $mod.Cla.GetA() === $mod.Cla.GetA();',
  16132. '$mod.b = 8 === $mod.Cla.FA;',
  16133. '$mod.TObject.FA = 9;',
  16134. '$mod.TObject.SetA($mod.TObject.GetA());',
  16135. '$mod.b = $mod.TObject.FA === 10;',
  16136. '$mod.b = 11 === $mod.TObject.FA;',
  16137. '']));
  16138. end;
  16139. procedure TTestModule.TestClassOf_ClassMethodSelf;
  16140. begin
  16141. StartProgram(false);
  16142. Add('type');
  16143. Add(' TObject = class');
  16144. Add(' class var GlobalId: longint;');
  16145. Add(' class procedure ProcA;');
  16146. Add(' end;');
  16147. Add('class procedure tobject.proca;');
  16148. Add('var b: boolean;');
  16149. Add('begin');
  16150. Add(' b:=self=nil;');
  16151. Add(' b:=self.globalid=3;');
  16152. Add(' b:=4=self.globalid;');
  16153. Add(' self.globalid:=5;');
  16154. Add(' self.proca;');
  16155. Add('end;');
  16156. Add('begin');
  16157. ConvertProgram;
  16158. CheckSource('TestClassOf_ClassMethodSelf',
  16159. LinesToStr([ // statements
  16160. 'rtl.createClass(this, "TObject", null, function () {',
  16161. ' this.GlobalId = 0;',
  16162. ' this.$init = function () {',
  16163. ' };',
  16164. ' this.$final = function () {',
  16165. ' };',
  16166. ' this.ProcA = function () {',
  16167. ' var b = false;',
  16168. ' b = this === null;',
  16169. ' b = this.GlobalId === 3;',
  16170. ' b = 4 === this.GlobalId;',
  16171. ' $mod.TObject.GlobalId = 5;',
  16172. ' this.ProcA();',
  16173. ' };',
  16174. '});'
  16175. ]),
  16176. LinesToStr([ // $mod.$main
  16177. '']));
  16178. end;
  16179. procedure TTestModule.TestClassOf_TypeCast;
  16180. begin
  16181. StartProgram(false);
  16182. Add('type');
  16183. Add(' TObject = class');
  16184. Add(' class procedure {#TObject_DoIt}DoIt;');
  16185. Add(' end;');
  16186. Add(' TClass = class of TObject;');
  16187. Add(' TMobile = class');
  16188. Add(' class procedure {#TMobile_DoIt}DoIt;');
  16189. Add(' end;');
  16190. Add(' TMobileClass = class of TMobile;');
  16191. Add(' TCar = class(TMobile)');
  16192. Add(' class procedure {#TCar_DoIt}DoIt;');
  16193. Add(' end;');
  16194. Add(' TCarClass = class of TCar;');
  16195. Add('class procedure TObject.DoIt;');
  16196. Add('begin');
  16197. Add(' TClass(Self).{@TObject_DoIt}DoIt;');
  16198. Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
  16199. Add('end;');
  16200. Add('class procedure TMobile.DoIt;');
  16201. Add('begin');
  16202. Add(' TClass(Self).{@TObject_DoIt}DoIt;');
  16203. Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
  16204. Add(' TCarClass(Self).{@TCar_DoIt}DoIt;');
  16205. Add('end;');
  16206. Add('class procedure TCar.DoIt; begin end;');
  16207. Add('var');
  16208. Add(' ObjC: TClass;');
  16209. Add(' MobileC: TMobileClass;');
  16210. Add(' CarC: TCarClass;');
  16211. Add('begin');
  16212. Add(' ObjC.{@TObject_DoIt}DoIt;');
  16213. Add(' MobileC.{@TMobile_DoIt}DoIt;');
  16214. Add(' CarC.{@TCar_DoIt}DoIt;');
  16215. Add(' TClass(ObjC).{@TObject_DoIt}DoIt;');
  16216. Add(' TMobileClass(ObjC).{@TMobile_DoIt}DoIt;');
  16217. Add(' TCarClass(ObjC).{@TCar_DoIt}DoIt;');
  16218. Add(' TClass(MobileC).{@TObject_DoIt}DoIt;');
  16219. Add(' TMobileClass(MobileC).{@TMobile_DoIt}DoIt;');
  16220. Add(' TCarClass(MobileC).{@TCar_DoIt}DoIt;');
  16221. Add(' TClass(CarC).{@TObject_DoIt}DoIt;');
  16222. Add(' TMobileClass(CarC).{@TMobile_DoIt}DoIt;');
  16223. Add(' TCarClass(CarC).{@TCar_DoIt}DoIt;');
  16224. ConvertProgram;
  16225. CheckSource('TestClassOf_TypeCast',
  16226. LinesToStr([ // statements
  16227. 'rtl.createClass(this, "TObject", null, function () {',
  16228. ' this.$init = function () {',
  16229. ' };',
  16230. ' this.$final = function () {',
  16231. ' };',
  16232. ' this.DoIt = function () {',
  16233. ' this.DoIt();',
  16234. ' this.DoIt$1();',
  16235. ' };',
  16236. '});',
  16237. 'rtl.createClass(this, "TMobile", this.TObject, function () {',
  16238. ' this.DoIt$1 = function () {',
  16239. ' this.DoIt();',
  16240. ' this.DoIt$1();',
  16241. ' this.DoIt$2();',
  16242. ' };',
  16243. '});',
  16244. 'rtl.createClass(this, "TCar", this.TMobile, function () {',
  16245. ' this.DoIt$2 = function () {',
  16246. ' };',
  16247. '});',
  16248. 'this.ObjC = null;',
  16249. 'this.MobileC = null;',
  16250. 'this.CarC = null;',
  16251. '']),
  16252. LinesToStr([ // $mod.$main
  16253. '$mod.ObjC.DoIt();',
  16254. '$mod.MobileC.DoIt$1();',
  16255. '$mod.CarC.DoIt$2();',
  16256. '$mod.ObjC.DoIt();',
  16257. '$mod.ObjC.DoIt$1();',
  16258. '$mod.ObjC.DoIt$2();',
  16259. '$mod.MobileC.DoIt();',
  16260. '$mod.MobileC.DoIt$1();',
  16261. '$mod.MobileC.DoIt$2();',
  16262. '$mod.CarC.DoIt();',
  16263. '$mod.CarC.DoIt$1();',
  16264. '$mod.CarC.DoIt$2();',
  16265. '']));
  16266. end;
  16267. procedure TTestModule.TestClassOf_ImplicitFunctionCall;
  16268. begin
  16269. StartProgram(false);
  16270. Add('type');
  16271. Add(' TObject = class');
  16272. Add(' function CurNow: longint; ');
  16273. Add(' class function Now: longint; ');
  16274. Add(' end;');
  16275. Add('function TObject.CurNow: longint; begin end;');
  16276. Add('class function TObject.Now: longint; begin end;');
  16277. Add('var');
  16278. Add(' Obj: tobject;');
  16279. Add(' vI: longint;');
  16280. Add('begin');
  16281. Add(' obj.curnow;');
  16282. Add(' vi:=obj.curnow;');
  16283. Add(' tobject.now;');
  16284. Add(' vi:=tobject.now;');
  16285. ConvertProgram;
  16286. CheckSource('TestClassOf_ImplicitFunctionCall',
  16287. LinesToStr([ // statements
  16288. 'rtl.createClass(this, "TObject", null, function () {',
  16289. ' this.$init = function () {',
  16290. ' };',
  16291. ' this.$final = function () {',
  16292. ' };',
  16293. ' this.CurNow = function () {',
  16294. ' var Result = 0;',
  16295. ' return Result;',
  16296. ' };',
  16297. ' this.Now = function () {',
  16298. ' var Result = 0;',
  16299. ' return Result;',
  16300. ' };',
  16301. '});',
  16302. 'this.Obj = null;',
  16303. 'this.vI = 0;',
  16304. '']),
  16305. LinesToStr([ // $mod.$main
  16306. '$mod.Obj.CurNow();',
  16307. '$mod.vI = $mod.Obj.CurNow();',
  16308. '$mod.TObject.Now();',
  16309. '$mod.vI = $mod.TObject.Now();',
  16310. '']));
  16311. end;
  16312. procedure TTestModule.TestClassOf_Const;
  16313. begin
  16314. StartProgram(false);
  16315. Add([
  16316. 'type',
  16317. ' TObject = class',
  16318. ' end;',
  16319. ' TBird = TObject;',
  16320. ' TBirds = class of TBird;',
  16321. ' TEagles = TBirds;',
  16322. ' THawk = class(TBird);',
  16323. 'const',
  16324. ' Hawk: TEagles = THawk;',
  16325. ' DefaultBirdClasses : Array [1..2] of TEagles = (',
  16326. ' TBird,',
  16327. ' THawk',
  16328. ' );',
  16329. 'begin']);
  16330. ConvertProgram;
  16331. CheckSource('TestClassOf_Const',
  16332. LinesToStr([ // statements
  16333. 'rtl.createClass(this, "TObject", null, function () {',
  16334. ' this.$init = function () {',
  16335. ' };',
  16336. ' this.$final = function () {',
  16337. ' };',
  16338. '});',
  16339. 'rtl.createClass(this, "THawk", this.TObject, function () {',
  16340. '});',
  16341. 'this.Hawk = this.THawk;',
  16342. 'this.DefaultBirdClasses = [this.TObject, this.THawk];',
  16343. '']),
  16344. LinesToStr([ // $mod.$main
  16345. '']));
  16346. end;
  16347. procedure TTestModule.TestNestedClass_Alias;
  16348. begin
  16349. WithTypeInfo:=true;
  16350. StartProgram(false);
  16351. Add([
  16352. 'type',
  16353. ' TObject = class',
  16354. ' type TNested = type longint;',
  16355. ' end;',
  16356. 'type TAlias = type tobject.tnested;',
  16357. 'var i: tobject.tnested = 3;',
  16358. 'var j: TAlias = 4;',
  16359. 'begin',
  16360. ' if typeinfo(TAlias)=nil then ;',
  16361. ' if typeinfo(tobject.tnested)=nil then ;',
  16362. '']);
  16363. ConvertProgram;
  16364. CheckSource('TestNestedClass_Alias',
  16365. LinesToStr([ // statements
  16366. 'rtl.createClass(this, "TObject", null, function () {',
  16367. ' $mod.$rtti.$inherited("TObject.TNested", rtl.longint, {});',
  16368. ' this.$init = function () {',
  16369. ' };',
  16370. ' this.$final = function () {',
  16371. ' };',
  16372. '});',
  16373. 'this.$rtti.$inherited("TAlias", this.$rtti["TObject.TNested"], {});',
  16374. 'this.i = 3;',
  16375. 'this.j = 4;',
  16376. '']),
  16377. LinesToStr([ // $mod.$main
  16378. 'if ($mod.$rtti["TAlias"] === null) ;',
  16379. 'if ($mod.$rtti["TObject.TNested"] === null) ;',
  16380. '']));
  16381. end;
  16382. procedure TTestModule.TestNestedClass_Record;
  16383. begin
  16384. WithTypeInfo:=true;
  16385. StartProgram(false);
  16386. Add([
  16387. 'type',
  16388. ' TObject = class',
  16389. ' type TPoint = record',
  16390. ' x,y: byte;',
  16391. ' end;',
  16392. ' procedure DoIt(t: TPoint);',
  16393. ' end;',
  16394. 'procedure tobject.DoIt(t: TPoint);',
  16395. 'var p: TPoint;',
  16396. 'begin',
  16397. ' t.x:=t.y;',
  16398. ' p:=t;',
  16399. 'end;',
  16400. 'var',
  16401. ' p: tobject.tpoint = (x:2; y:4);',
  16402. ' o: TObject;',
  16403. 'begin',
  16404. ' p:=p;',
  16405. ' o.doit(p);',
  16406. '']);
  16407. ConvertProgram;
  16408. CheckSource('TestNestedClass_Record',
  16409. LinesToStr([ // statements
  16410. 'rtl.createClass(this, "TObject", null, function () {',
  16411. ' rtl.recNewT(this, "TPoint", function () {',
  16412. ' this.x = 0;',
  16413. ' this.y = 0;',
  16414. ' this.$eq = function (b) {',
  16415. ' return (this.x === b.x) && (this.y === b.y);',
  16416. ' };',
  16417. ' this.$assign = function (s) {',
  16418. ' this.x = s.x;',
  16419. ' this.y = s.y;',
  16420. ' return this;',
  16421. ' };',
  16422. ' var $r = $mod.$rtti.$Record("TObject.TPoint", {});',
  16423. ' $r.addField("x", rtl.byte);',
  16424. ' $r.addField("y", rtl.byte);',
  16425. ' });',
  16426. ' this.$init = function () {',
  16427. ' };',
  16428. ' this.$final = function () {',
  16429. ' };',
  16430. ' this.DoIt = function (t) {',
  16431. ' var p = this.TPoint.$new();',
  16432. ' t.x = t.y;',
  16433. ' p.$assign(t);',
  16434. ' };',
  16435. '});',
  16436. 'this.p = this.TObject.TPoint.$clone({',
  16437. ' x: 2,',
  16438. ' y: 4',
  16439. '});',
  16440. 'this.o = null;',
  16441. '']),
  16442. LinesToStr([ // $mod.$main
  16443. '$mod.p.$assign($mod.p);',
  16444. '$mod.o.DoIt($mod.TObject.TPoint.$clone($mod.p));',
  16445. '']));
  16446. end;
  16447. procedure TTestModule.TestNestedClass_Class;
  16448. begin
  16449. WithTypeInfo:=true;
  16450. StartProgram(false);
  16451. Add([
  16452. 'type',
  16453. ' TObject = class end;',
  16454. ' TBird = class',
  16455. ' type TLeg = class',
  16456. ' FId: longint;',
  16457. ' constructor Create;',
  16458. ' function Create(i: longint): TLeg;',
  16459. ' end;',
  16460. ' function DoIt(b: TBird): Tleg;',
  16461. ' end;',
  16462. 'constructor tbird.tleg.create;',
  16463. 'begin',
  16464. ' FId:=3;',
  16465. 'end;',
  16466. 'function tbird.tleg.Create(i: longint): TLeg;',
  16467. 'begin',
  16468. ' Create;',
  16469. ' Result:=TLeg.Create;',
  16470. ' Result:=TBird.TLeg.Create;',
  16471. ' Result:=Create(3);',
  16472. ' FId:=i;',
  16473. 'end;',
  16474. 'function tbird.DoIt(b: tbird): tleg;',
  16475. 'begin',
  16476. ' Result.Create;',
  16477. ' Result:=TLeg.Create;',
  16478. ' Result:=TBird.TLeg.Create;',
  16479. ' Result:=Result.Create(3);',
  16480. 'end;',
  16481. 'var',
  16482. ' b: Tbird.tleg;',
  16483. 'begin',
  16484. ' b.Create;',
  16485. ' b:=TBird.TLeg.Create;',
  16486. ' b:=b.Create(3);',
  16487. '']);
  16488. ConvertProgram;
  16489. CheckSource('TestNestedClass_Class',
  16490. LinesToStr([ // statements
  16491. 'rtl.createClass(this, "TObject", null, function () {',
  16492. ' this.$init = function () {',
  16493. ' };',
  16494. ' this.$final = function () {',
  16495. ' };',
  16496. '});',
  16497. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16498. ' rtl.createClass(this, "TLeg", $mod.TObject, function () {',
  16499. ' this.$init = function () {',
  16500. ' $mod.TObject.$init.call(this);',
  16501. ' this.FId = 0;',
  16502. ' };',
  16503. ' this.Create = function () {',
  16504. ' this.FId = 3;',
  16505. ' return this;',
  16506. ' };',
  16507. ' this.Create$1 = function (i) {',
  16508. ' var Result = null;',
  16509. ' this.Create();',
  16510. ' Result = $mod.TBird.TLeg.$create("Create");',
  16511. ' Result = $mod.TBird.TLeg.$create("Create");',
  16512. ' Result = this.Create$1(3);',
  16513. ' this.FId = i;',
  16514. ' return Result;',
  16515. ' };',
  16516. ' }, "TBird.TLeg");',
  16517. ' this.DoIt = function (b) {',
  16518. ' var Result = null;',
  16519. ' Result.Create();',
  16520. ' Result = this.TLeg.$create("Create");',
  16521. ' Result = $mod.TBird.TLeg.$create("Create");',
  16522. ' Result = Result.Create$1(3);',
  16523. ' return Result;',
  16524. ' };',
  16525. '});',
  16526. 'this.b = null;',
  16527. '']),
  16528. LinesToStr([ // $mod.$main
  16529. '$mod.b.Create();',
  16530. '$mod.b = $mod.TBird.TLeg.$create("Create");',
  16531. '$mod.b = $mod.b.Create$1(3);',
  16532. '']));
  16533. end;
  16534. procedure TTestModule.TestExternalClass_Var;
  16535. begin
  16536. StartProgram(false);
  16537. Add([
  16538. '{$modeswitch externalclass}',
  16539. 'type',
  16540. ' TExtA = class external name ''ExtObj''',
  16541. ' Id: longint external name ''$Id'';',
  16542. ' B: longint;',
  16543. ' end;',
  16544. 'var Obj: TExtA;',
  16545. 'begin',
  16546. ' obj.id:=obj.id+1;',
  16547. ' obj.B:=obj.B+1;']);
  16548. ConvertProgram;
  16549. CheckSource('TestExternalClass_Var',
  16550. LinesToStr([ // statements
  16551. 'this.Obj = null;',
  16552. '']),
  16553. LinesToStr([ // $mod.$main
  16554. '$mod.Obj.$Id = $mod.Obj.$Id + 1;',
  16555. '$mod.Obj.B = $mod.Obj.B + 1;',
  16556. '']));
  16557. end;
  16558. procedure TTestModule.TestExternalClass_Const;
  16559. begin
  16560. StartProgram(false);
  16561. Add([
  16562. '{$modeswitch externalclass}',
  16563. 'type',
  16564. ' TExtA = class external name ''ExtObj''',
  16565. ' const Two: longint = 2;',
  16566. ' const Three = 3;',
  16567. ' const Id: longint;',
  16568. ' end;',
  16569. ' TExtB = class external name ''ExtB''',
  16570. ' A: TExtA;',
  16571. ' end;',
  16572. 'var',
  16573. ' A: texta;',
  16574. ' B: textb;',
  16575. ' i: longint;',
  16576. 'begin',
  16577. ' i:=a.two;',
  16578. ' i:=texta.two;',
  16579. ' i:=a.three;',
  16580. ' i:=texta.three;',
  16581. ' i:=a.id;',
  16582. ' i:=texta.id;',
  16583. '']);
  16584. ConvertProgram;
  16585. CheckSource('TestExternalClass_Const',
  16586. LinesToStr([ // statements
  16587. 'this.A = null;',
  16588. 'this.B = null;',
  16589. 'this.i = 0;',
  16590. '']),
  16591. LinesToStr([ // $mod.$main
  16592. '$mod.i = 2;',
  16593. '$mod.i = 2;',
  16594. '$mod.i = 3;',
  16595. '$mod.i = 3;',
  16596. '$mod.i = $mod.A.Id;',
  16597. '$mod.i = ExtObj.Id;',
  16598. '']));
  16599. end;
  16600. procedure TTestModule.TestExternalClass_Dollar;
  16601. begin
  16602. StartProgram(false);
  16603. Add([
  16604. '{$modeswitch externalclass}',
  16605. 'type',
  16606. ' TExtA = class external name ''$''',
  16607. ' Id: longint external name ''$'';',
  16608. ' function Bla(i: longint): longint; external name ''$'';',
  16609. ' end;',
  16610. 'function dollar(k: longint): longint; external name ''$'';',
  16611. 'var Obj: TExtA;',
  16612. 'begin',
  16613. ' dollar(1);',
  16614. ' obj.id:=obj.id+2;',
  16615. ' obj.Bla(3);',
  16616. '']);
  16617. ConvertProgram;
  16618. CheckSource('TestExternalClass_Dollar',
  16619. LinesToStr([ // statements
  16620. 'this.Obj = null;',
  16621. '']),
  16622. LinesToStr([ // $mod.$main
  16623. '$(1);',
  16624. '$mod.Obj.$ = $mod.Obj.$ + 2;',
  16625. '$mod.Obj.$(3);',
  16626. '']));
  16627. end;
  16628. procedure TTestModule.TestExternalClass_DuplicateVarFail;
  16629. begin
  16630. StartProgram(false);
  16631. Add('{$modeswitch externalclass}');
  16632. Add('type');
  16633. Add(' TExtA = class external name ''ExtA''');
  16634. Add(' Id: longint external name ''$Id'';');
  16635. Add(' end;');
  16636. Add(' TExtB = class external ''lib'' name ''ExtB''(TExtA)');
  16637. Add(' Id: longint;');
  16638. Add(' end;');
  16639. Add('begin');
  16640. SetExpectedPasResolverError('Duplicate identifier "Id" at test1.pp(6,5)',nDuplicateIdentifier);
  16641. ConvertProgram;
  16642. end;
  16643. procedure TTestModule.TestExternalClass_Method;
  16644. begin
  16645. StartProgram(false);
  16646. Add(['{$modeswitch externalclass}',
  16647. 'type',
  16648. ' TExtA = class external name ''ExtObj''',
  16649. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  16650. ' procedure DoSome(Id: longint = 1);',
  16651. ' end;',
  16652. 'var Obj: texta;',
  16653. 'begin',
  16654. ' obj.doit;',
  16655. ' obj.doit();',
  16656. ' obj.doit(2);',
  16657. ' with obj do begin',
  16658. ' doit;',
  16659. ' doit();',
  16660. ' doit(3);',
  16661. ' end;']);
  16662. ConvertProgram;
  16663. CheckSource('TestExternalClass_Method',
  16664. LinesToStr([ // statements
  16665. 'this.Obj = null;',
  16666. '']),
  16667. LinesToStr([ // $mod.$main
  16668. '$mod.Obj.$Execute(1);',
  16669. '$mod.Obj.$Execute(1);',
  16670. '$mod.Obj.$Execute(2);',
  16671. 'var $with = $mod.Obj;',
  16672. '$with.$Execute(1);',
  16673. '$with.$Execute(1);',
  16674. '$with.$Execute(3);',
  16675. '']));
  16676. end;
  16677. procedure TTestModule.TestExternalClass_ClassMethod;
  16678. begin
  16679. StartProgram(false);
  16680. Add([
  16681. '{$modeswitch externalclass}',
  16682. 'type',
  16683. ' TExtA = class external name ''ExtObj''',
  16684. ' class procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  16685. ' end;',
  16686. ' TExtB = TExtA;',
  16687. 'var p: Pointer;',
  16688. 'begin',
  16689. ' texta.doit;',
  16690. ' texta.doit();',
  16691. ' texta.doit(2);',
  16692. ' p:[email protected];',
  16693. ' with texta do begin',
  16694. ' doit;',
  16695. ' doit();',
  16696. ' doit(3);',
  16697. ' p:=@DoIt;',
  16698. ' end;',
  16699. ' textb.doit;',
  16700. ' textb.doit();',
  16701. ' textb.doit(4);',
  16702. ' with textb do begin',
  16703. ' doit;',
  16704. ' doit();',
  16705. ' doit(5);',
  16706. ' end;',
  16707. '']);
  16708. ConvertProgram;
  16709. CheckSource('TestExternalClass_ClassMethod',
  16710. LinesToStr([ // statements
  16711. 'this.p = null;',
  16712. '']),
  16713. LinesToStr([ // $mod.$main
  16714. 'ExtObj.$Execute(1);',
  16715. 'ExtObj.$Execute(1);',
  16716. 'ExtObj.$Execute(2);',
  16717. '$mod.p = rtl.createCallback(ExtObj, "$Execute");',
  16718. 'ExtObj.$Execute(1);',
  16719. 'ExtObj.$Execute(1);',
  16720. 'ExtObj.$Execute(3);',
  16721. '$mod.p = rtl.createCallback(ExtObj, "$Execute");',
  16722. 'ExtObj.$Execute(1);',
  16723. 'ExtObj.$Execute(1);',
  16724. 'ExtObj.$Execute(4);',
  16725. 'ExtObj.$Execute(1);',
  16726. 'ExtObj.$Execute(1);',
  16727. 'ExtObj.$Execute(5);',
  16728. '']));
  16729. end;
  16730. procedure TTestModule.TestExternalClass_ClassMethodStatic;
  16731. begin
  16732. StartProgram(false);
  16733. Add([
  16734. '{$modeswitch externalclass}',
  16735. 'type',
  16736. ' TExtA = class external name ''ExtObj''',
  16737. ' class procedure DoIt(Id: longint = 1); static;',
  16738. ' end;',
  16739. 'var p: Pointer;',
  16740. 'begin',
  16741. ' texta.doit;',
  16742. ' texta.doit();',
  16743. ' texta.doit(2);',
  16744. ' p:[email protected];',
  16745. ' with texta do begin',
  16746. ' doit;',
  16747. ' doit();',
  16748. ' doit(3);',
  16749. ' p:=@DoIt;',
  16750. ' end;',
  16751. '']);
  16752. ConvertProgram;
  16753. CheckSource('TestExternalClass_ClassMethodStatic',
  16754. LinesToStr([ // statements
  16755. 'this.p = null;',
  16756. '']),
  16757. LinesToStr([ // $mod.$main
  16758. 'ExtObj.DoIt(1);',
  16759. 'ExtObj.DoIt(1);',
  16760. 'ExtObj.DoIt(2);',
  16761. '$mod.p = ExtObj.DoIt;',
  16762. 'ExtObj.DoIt(1);',
  16763. 'ExtObj.DoIt(1);',
  16764. 'ExtObj.DoIt(3);',
  16765. '$mod.p = ExtObj.DoIt;',
  16766. '']));
  16767. end;
  16768. procedure TTestModule.TestExternalClass_FunctionResultInTypeCast;
  16769. begin
  16770. StartProgram(false);
  16771. Add([
  16772. '{$modeswitch externalclass}',
  16773. 'type',
  16774. ' TBird = class external name ''Array''',
  16775. ' end;',
  16776. 'function GetPtr: Pointer;',
  16777. 'begin',
  16778. 'end;',
  16779. 'procedure Write(const p);',
  16780. 'begin',
  16781. 'end;',
  16782. 'procedure WriteLn; varargs;',
  16783. 'begin',
  16784. 'end;',
  16785. 'begin',
  16786. ' if TBird(GetPtr)=nil then ;',
  16787. ' Write(GetPtr);',
  16788. ' WriteLn(GetPtr);',
  16789. ' Write(TBird(GetPtr));',
  16790. ' WriteLn(TBird(GetPtr));',
  16791. '']);
  16792. ConvertProgram;
  16793. CheckSource('TestFunctionResultInTypeCast',
  16794. LinesToStr([ // statements
  16795. 'this.GetPtr = function () {',
  16796. ' var Result = null;',
  16797. ' return Result;',
  16798. '};',
  16799. 'this.Write = function (p) {',
  16800. '};',
  16801. 'this.WriteLn = function () {',
  16802. '};',
  16803. '']),
  16804. LinesToStr([
  16805. 'if ($mod.GetPtr() === null) ;',
  16806. '$mod.Write($mod.GetPtr());',
  16807. '$mod.WriteLn($mod.GetPtr());',
  16808. '$mod.Write($mod.GetPtr());',
  16809. '$mod.WriteLn($mod.GetPtr());',
  16810. '']));
  16811. end;
  16812. procedure TTestModule.TestExternalClass_NonExternalOverride;
  16813. begin
  16814. StartProgram(false);
  16815. Add([
  16816. '{$modeswitch externalclass}',
  16817. 'type',
  16818. ' TExtA = class external name ''ExtObjA''',
  16819. ' procedure ProcA; virtual;',
  16820. ' procedure ProcB; virtual;',
  16821. ' end;',
  16822. ' TExtB = class external name ''ExtObjB'' (TExtA)',
  16823. ' end;',
  16824. ' TExtC = class (TExtB)',
  16825. ' procedure ProcA; override;',
  16826. ' end;',
  16827. 'procedure TExtC.ProcA;',
  16828. 'begin',
  16829. ' ProcA;',
  16830. ' Self.ProcA;',
  16831. ' ProcB;',
  16832. ' Self.ProcB;',
  16833. 'end;',
  16834. 'var',
  16835. ' A: texta;',
  16836. ' B: textb;',
  16837. ' C: textc;',
  16838. 'begin',
  16839. ' a.proca;',
  16840. ' b.proca;',
  16841. ' c.proca;']);
  16842. ConvertProgram;
  16843. CheckSource('TestExternalClass_NonExternalOverride',
  16844. LinesToStr([ // statements
  16845. 'rtl.createClassExt(this, "TExtC", ExtObjB, "", function () {',
  16846. ' this.$init = function () {',
  16847. ' };',
  16848. ' this.$final = function () {',
  16849. ' };',
  16850. ' this.ProcA = function () {',
  16851. ' this.ProcA();',
  16852. ' this.ProcA();',
  16853. ' this.ProcB();',
  16854. ' this.ProcB();',
  16855. ' };',
  16856. '});',
  16857. 'this.A = null;',
  16858. 'this.B = null;',
  16859. 'this.C = null;',
  16860. '']),
  16861. LinesToStr([ // $mod.$main
  16862. '$mod.A.ProcA();',
  16863. '$mod.B.ProcA();',
  16864. '$mod.C.ProcA();',
  16865. '']));
  16866. end;
  16867. procedure TTestModule.TestExternalClass_OverloadHint;
  16868. begin
  16869. StartProgram(false);
  16870. Add([
  16871. '{$modeswitch externalclass}',
  16872. 'type',
  16873. ' TExtA = class external name ''ExtObjA''',
  16874. ' procedure DoIt;',
  16875. ' procedure DoIt(i: longint);',
  16876. ' end;',
  16877. 'begin',
  16878. '']);
  16879. ConvertProgram;
  16880. CheckResolverUnexpectedHints(true);
  16881. CheckSource('TestExternalClass_OverloadHint',
  16882. LinesToStr([ // statements
  16883. '']),
  16884. LinesToStr([ // $mod.$main
  16885. '']));
  16886. end;
  16887. procedure TTestModule.TestExternalClass_SameNamePublishedProperty;
  16888. begin
  16889. WithTypeInfo:=true;
  16890. StartProgram(false);
  16891. Add([
  16892. '{$modeswitch externalclass}',
  16893. 'type',
  16894. ' JSwiper = class external name ''Swiper''',
  16895. ' constructor New;',
  16896. ' end;',
  16897. ' TObject = class',
  16898. ' private',
  16899. ' FSwiper: JSwiper;',
  16900. ' published',
  16901. ' property Swiper: JSwiper read FSwiper write FSwiper;',
  16902. ' end;',
  16903. 'begin',
  16904. ' JSwiper.new;',
  16905. '']);
  16906. ConvertProgram;
  16907. CheckSource('TestExternalClass_SameNamePublishedProperty',
  16908. LinesToStr([ // statements
  16909. 'this.$rtti.$ExtClass("JSwiper", {',
  16910. ' jsclass: "Swiper"',
  16911. '});',
  16912. 'rtl.createClass(this, "TObject", null, function () {',
  16913. ' this.$init = function () {',
  16914. ' this.FSwiper = null;',
  16915. ' };',
  16916. ' this.$final = function () {',
  16917. ' this.FSwiper = undefined;',
  16918. ' };',
  16919. ' var $r = this.$rtti;',
  16920. ' $r.addProperty("Swiper", 0, $mod.$rtti["JSwiper"], "FSwiper", "FSwiper");',
  16921. '});',
  16922. '']),
  16923. LinesToStr([ // $mod.$main
  16924. 'new Swiper();',
  16925. '']));
  16926. end;
  16927. procedure TTestModule.TestExternalClass_Property;
  16928. begin
  16929. StartProgram(false);
  16930. Add([
  16931. '{$modeswitch externalclass}',
  16932. 'type',
  16933. ' TExtA = class external name ''ExtA''',
  16934. ' function getYear: longint;',
  16935. ' procedure setYear(Value: longint);',
  16936. ' property Year: longint read getyear write setyear;',
  16937. ' end;',
  16938. ' TExtB = class (TExtA)',
  16939. ' procedure OtherSetYear(Value: longint);',
  16940. ' property year write othersetyear;',
  16941. ' end;',
  16942. 'procedure textb.othersetyear(value: longint);',
  16943. 'begin',
  16944. ' setYear(Value+4);',
  16945. 'end;',
  16946. 'var',
  16947. ' A: texta;',
  16948. ' B: textb;',
  16949. 'begin',
  16950. ' a.year:=a.year+1;',
  16951. ' b.year:=b.year+2;']);
  16952. ConvertProgram;
  16953. CheckSource('TestExternalClass_NonExternalOverride',
  16954. LinesToStr([ // statements
  16955. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  16956. ' this.$init = function () {',
  16957. ' };',
  16958. ' this.$final = function () {',
  16959. ' };',
  16960. ' this.OtherSetYear = function (Value) {',
  16961. ' this.setYear(Value+4);',
  16962. ' };',
  16963. '});',
  16964. 'this.A = null;',
  16965. 'this.B = null;',
  16966. '']),
  16967. LinesToStr([ // $mod.$main
  16968. '$mod.A.setYear($mod.A.getYear()+1);',
  16969. '$mod.B.OtherSetYear($mod.B.getYear()+2);',
  16970. '']));
  16971. end;
  16972. procedure TTestModule.TestExternalClass_PropertyDate;
  16973. begin
  16974. StartProgram(false);
  16975. Add([
  16976. '{$modeswitch externalclass}',
  16977. 'type',
  16978. ' TExtA = class external name ''ExtA''',
  16979. ' end;',
  16980. ' TExtB = class (TExtA)',
  16981. ' FDate: string;',
  16982. ' property Date: string read FDate write FDate;',
  16983. ' property ExtA: string read FDate write FDate;',
  16984. ' end;',
  16985. ' {$M+}',
  16986. ' TObject = class',
  16987. ' FDate: string;',
  16988. ' published',
  16989. ' property Date: string read FDate write FDate;',
  16990. ' property ExtA: string read FDate write FDate;',
  16991. ' end;',
  16992. 'var',
  16993. ' B: textb;',
  16994. ' o: TObject;',
  16995. 'begin',
  16996. ' b.date:=b.exta;',
  16997. ' o.date:=o.exta;']);
  16998. ConvertProgram;
  16999. CheckSource('TestExternalClass_PropertyDate',
  17000. LinesToStr([ // statements
  17001. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  17002. ' this.$init = function () {',
  17003. ' this.FDate = "";',
  17004. ' };',
  17005. ' this.$final = function () {',
  17006. ' };',
  17007. '});',
  17008. 'rtl.createClass(this, "TObject", null, function () {',
  17009. ' this.$init = function () {',
  17010. ' this.FDate = "";',
  17011. ' };',
  17012. ' this.$final = function () {',
  17013. ' };',
  17014. ' var $r = this.$rtti;',
  17015. ' $r.addField("FDate", rtl.string);',
  17016. ' $r.addProperty("Date", 0, rtl.string, "FDate", "FDate");',
  17017. ' $r.addProperty("ExtA", 0, rtl.string, "FDate", "FDate");',
  17018. '});',
  17019. 'this.B = null;',
  17020. 'this.o = null;',
  17021. '']),
  17022. LinesToStr([ // $mod.$main
  17023. '$mod.B.FDate = $mod.B.FDate;',
  17024. '$mod.o.FDate = $mod.o.FDate;',
  17025. '']));
  17026. end;
  17027. procedure TTestModule.TestExternalClass_ClassProperty;
  17028. begin
  17029. StartProgram(false);
  17030. Add('{$modeswitch externalclass}');
  17031. Add('type');
  17032. Add(' TExtA = class external name ''ExtA''');
  17033. Add(' class function getYear: longint;');
  17034. Add(' class procedure setYear(Value: longint);');
  17035. Add(' class property Year: longint read getyear write setyear;');
  17036. Add(' end;');
  17037. Add(' TExtB = class (TExtA)');
  17038. Add(' class function GetCentury: longint;');
  17039. Add(' class procedure SetCentury(Value: longint);');
  17040. Add(' class property Century: longint read getcentury write setcentury;');
  17041. Add(' end;');
  17042. Add('class function textb.getcentury: longint;');
  17043. Add('begin');
  17044. Add('end;');
  17045. Add('class procedure textb.setcentury(value: longint);');
  17046. Add('begin');
  17047. Add(' setyear(value+11);');
  17048. Add(' texta.year:=texta.year+12;');
  17049. Add(' year:=year+13;');
  17050. Add(' textb.century:=textb.century+14;');
  17051. Add(' century:=century+15;');
  17052. Add('end;');
  17053. Add('var');
  17054. Add(' A: texta;');
  17055. Add(' B: textb;');
  17056. Add('begin');
  17057. Add(' texta.year:=texta.year+1;');
  17058. Add(' textb.year:=textb.year+2;');
  17059. Add(' TextA.year:=TextA.year+3;');
  17060. Add(' b.year:=b.year+4;');
  17061. Add(' textb.century:=textb.century+5;');
  17062. Add(' b.century:=b.century+6;');
  17063. ConvertProgram;
  17064. CheckSource('TestExternalClass_ClassProperty',
  17065. LinesToStr([ // statements
  17066. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  17067. ' this.$init = function () {',
  17068. ' };',
  17069. ' this.$final = function () {',
  17070. ' };',
  17071. ' this.GetCentury = function () {',
  17072. ' var Result = 0;',
  17073. ' return Result;',
  17074. ' };',
  17075. ' this.SetCentury = function (Value) {',
  17076. ' this.setYear(Value + 11);',
  17077. ' ExtA.setYear(ExtA.getYear() + 12);',
  17078. ' this.setYear(this.getYear() + 13);',
  17079. ' $mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 14);',
  17080. ' this.SetCentury(this.GetCentury() + 15);',
  17081. ' };',
  17082. '});',
  17083. 'this.A = null;',
  17084. 'this.B = null;',
  17085. '']),
  17086. LinesToStr([ // $mod.$main
  17087. 'ExtA.setYear(ExtA.getYear() + 1);',
  17088. '$mod.TExtB.setYear($mod.TExtB.getYear() + 2);',
  17089. 'ExtA.setYear(ExtA.getYear() + 3);',
  17090. '$mod.B.setYear($mod.B.getYear() + 4);',
  17091. '$mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 5);',
  17092. '$mod.B.$class.SetCentury($mod.B.$class.GetCentury() + 6);',
  17093. '']));
  17094. end;
  17095. procedure TTestModule.TestExternalClass_ClassOf;
  17096. begin
  17097. StartProgram(false);
  17098. Add('{$modeswitch externalclass}');
  17099. Add('type');
  17100. Add(' TExtA = class external name ''ExtA''');
  17101. Add(' procedure ProcA; virtual;');
  17102. Add(' procedure ProcB; virtual;');
  17103. Add(' end;');
  17104. Add(' TExtAClass = class of TExtA;');
  17105. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  17106. Add(' end;');
  17107. Add(' TExtBClass = class of TExtB;');
  17108. Add(' TExtC = class (TExtB)');
  17109. Add(' procedure ProcA; override;');
  17110. Add(' end;');
  17111. Add(' TExtCClass = class of TExtC;');
  17112. Add('procedure TExtC.ProcA; begin end;');
  17113. Add('var');
  17114. Add(' A: texta; ClA: TExtAClass;');
  17115. Add(' B: textb; ClB: TExtBClass;');
  17116. Add(' C: textc; ClC: TExtCClass;');
  17117. Add('begin');
  17118. Add(' ClA:=texta;');
  17119. Add(' ClA:=textb;');
  17120. Add(' ClA:=textc;');
  17121. Add(' ClB:=textb;');
  17122. Add(' ClB:=textc;');
  17123. Add(' ClC:=textc;');
  17124. ConvertProgram;
  17125. CheckSource('TestExternalClass_ClassOf',
  17126. LinesToStr([ // statements
  17127. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  17128. ' this.$init = function () {',
  17129. ' };',
  17130. ' this.$final = function () {',
  17131. ' };',
  17132. ' this.ProcA = function () {',
  17133. ' };',
  17134. '});',
  17135. 'this.A = null;',
  17136. 'this.ClA = null;',
  17137. 'this.B = null;',
  17138. 'this.ClB = null;',
  17139. 'this.C = null;',
  17140. 'this.ClC = null;',
  17141. '']),
  17142. LinesToStr([ // $mod.$main
  17143. '$mod.ClA = ExtA;',
  17144. '$mod.ClA = ExtB;',
  17145. '$mod.ClA = $mod.TExtC;',
  17146. '$mod.ClB = ExtB;',
  17147. '$mod.ClB = $mod.TExtC;',
  17148. '$mod.ClC = $mod.TExtC;',
  17149. '']));
  17150. end;
  17151. procedure TTestModule.TestExternalClass_ClassOtherUnit;
  17152. begin
  17153. AddModuleWithIntfImplSrc('unit2.pas',
  17154. LinesToStr([
  17155. '{$modeswitch externalclass}',
  17156. 'type',
  17157. ' TExtA = class external name ''ExtA''',
  17158. ' class var Id: longint;',
  17159. ' end;',
  17160. '']),
  17161. '');
  17162. StartUnit(true);
  17163. Add('interface');
  17164. Add('uses unit2;');
  17165. Add('implementation');
  17166. Add('begin');
  17167. Add(' unit2.texta.id:=unit2.texta.id+1;');
  17168. ConvertUnit;
  17169. CheckSource('TestExternalClass_ClassOtherUnit',
  17170. LinesToStr([
  17171. '']),
  17172. LinesToStr([
  17173. 'ExtA.Id = ExtA.Id + 1;',
  17174. '']));
  17175. end;
  17176. procedure TTestModule.TestExternalClass_Is;
  17177. begin
  17178. StartProgram(false);
  17179. Add([
  17180. '{$modeswitch externalclass}',
  17181. 'type',
  17182. ' TExtA = class external name ''ExtA''',
  17183. ' end;',
  17184. ' TExtAClass = class of TExtA;',
  17185. ' TExtB = class external name ''ExtB'' (TExtA)',
  17186. ' end;',
  17187. ' TExtBClass = class of TExtB;',
  17188. ' TExtC = class (TExtB)',
  17189. ' end;',
  17190. ' TExtCClass = class of TExtC;',
  17191. 'var',
  17192. ' A: texta; ClA: TExtAClass;',
  17193. ' B: textb; ClB: TExtBClass;',
  17194. ' C: textc; ClC: TExtCClass;',
  17195. 'begin',
  17196. ' if a is textb then ;',
  17197. ' if a is textc then ;',
  17198. ' if b is textc then ;',
  17199. ' if cla is textb then ;',
  17200. ' if cla is textc then ;',
  17201. ' if clb is textc then ;',
  17202. ' try',
  17203. ' except',
  17204. ' on TExtA do ;',
  17205. ' on e: TExtB do ;',
  17206. ' end;',
  17207. '']);
  17208. ConvertProgram;
  17209. CheckSource('TestExternalClass_Is',
  17210. LinesToStr([ // statements
  17211. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  17212. ' this.$init = function () {',
  17213. ' };',
  17214. ' this.$final = function () {',
  17215. ' };',
  17216. '});',
  17217. 'this.A = null;',
  17218. 'this.ClA = null;',
  17219. 'this.B = null;',
  17220. 'this.ClB = null;',
  17221. 'this.C = null;',
  17222. 'this.ClC = null;',
  17223. '']),
  17224. LinesToStr([ // $mod.$main
  17225. 'if (rtl.isExt($mod.A, ExtB)) ;',
  17226. 'if ($mod.TExtC.isPrototypeOf($mod.A)) ;',
  17227. 'if ($mod.TExtC.isPrototypeOf($mod.B)) ;',
  17228. 'if (rtl.isExt($mod.ClA, ExtB)) ;',
  17229. 'if (rtl.is($mod.ClA, $mod.TExtC)) ;',
  17230. 'if (rtl.is($mod.ClB, $mod.TExtC)) ;',
  17231. 'try {} catch ($e) {',
  17232. ' if (rtl.isExt($e,ExtA)) {}',
  17233. ' else if (rtl.isExt($e,ExtB)) {',
  17234. ' var e = $e;',
  17235. ' } else throw $e',
  17236. '};',
  17237. '']));
  17238. end;
  17239. procedure TTestModule.TestExternalClass_As;
  17240. begin
  17241. StartProgram(false);
  17242. Add('{$modeswitch externalclass}');
  17243. Add('type');
  17244. Add(' TExtA = class external name ''ExtA''');
  17245. Add(' end;');
  17246. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  17247. Add(' end;');
  17248. Add(' TExtC = class (TExtB)');
  17249. Add(' end;');
  17250. Add('var');
  17251. Add(' A: texta;');
  17252. Add(' B: textb;');
  17253. Add(' C: textc;');
  17254. Add('begin');
  17255. Add(' b:=a as textb;');
  17256. Add(' c:=a as textc;');
  17257. Add(' c:=b as textc;');
  17258. ConvertProgram;
  17259. CheckSource('TestExternalClass_Is',
  17260. LinesToStr([ // statements
  17261. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  17262. ' this.$init = function () {',
  17263. ' };',
  17264. ' this.$final = function () {',
  17265. ' };',
  17266. '});',
  17267. 'this.A = null;',
  17268. 'this.B = null;',
  17269. 'this.C = null;',
  17270. '']),
  17271. LinesToStr([ // $mod.$main
  17272. '$mod.B = rtl.asExt($mod.A, ExtB);',
  17273. '$mod.C = rtl.as($mod.A, $mod.TExtC);',
  17274. '$mod.C = rtl.as($mod.B, $mod.TExtC);',
  17275. '']));
  17276. end;
  17277. procedure TTestModule.TestExternalClass_DestructorFail;
  17278. begin
  17279. StartProgram(false);
  17280. Add('{$modeswitch externalclass}');
  17281. Add('type');
  17282. Add(' TExtA = class external name ''ExtA''');
  17283. Add(' destructor Free;');
  17284. Add(' end;');
  17285. SetExpectedPasResolverError('Pascal element not supported: destructor',
  17286. nPasElementNotSupported);
  17287. ConvertProgram;
  17288. end;
  17289. procedure TTestModule.TestExternalClass_New;
  17290. begin
  17291. StartProgram(false);
  17292. Add([
  17293. '{$modeswitch externalclass}',
  17294. 'type',
  17295. ' TExtA = class external name ''ExtA''',
  17296. ' constructor New;',
  17297. ' constructor New(i: longint; j: longint = 2);',
  17298. ' end;',
  17299. 'var',
  17300. ' A: texta;',
  17301. 'begin',
  17302. ' a:=texta.new;',
  17303. ' a:=texta(texta.new);',
  17304. ' a:=texta.new();',
  17305. ' a:=texta.new(1);',
  17306. ' with texta do begin',
  17307. ' a:=new;',
  17308. ' a:=new();',
  17309. ' a:=new(2);',
  17310. ' end;',
  17311. ' a:=test1.texta.new;',
  17312. ' a:=test1.texta.new();',
  17313. ' a:=test1.texta.new(3);',
  17314. '']);
  17315. ConvertProgram;
  17316. CheckSource('TestExternalClass_New',
  17317. LinesToStr([ // statements
  17318. 'this.A = null;',
  17319. '']),
  17320. LinesToStr([ // $mod.$main
  17321. '$mod.A = new ExtA();',
  17322. '$mod.A = new ExtA();',
  17323. '$mod.A = new ExtA();',
  17324. '$mod.A = new ExtA(1,2);',
  17325. '$mod.A = new ExtA();',
  17326. '$mod.A = new ExtA();',
  17327. '$mod.A = new ExtA(2,2);',
  17328. '$mod.A = new ExtA();',
  17329. '$mod.A = new ExtA();',
  17330. '$mod.A = new ExtA(3,2);',
  17331. '']));
  17332. end;
  17333. procedure TTestModule.TestExternalClass_ClassOf_New;
  17334. begin
  17335. StartProgram(false);
  17336. Add('{$modeswitch externalclass}');
  17337. Add('type');
  17338. Add(' TExtAClass = class of TExtA;');
  17339. Add(' TExtA = class external name ''ExtA''');
  17340. Add(' C: TExtAClass;');
  17341. Add(' constructor New;');
  17342. Add(' end;');
  17343. Add('var');
  17344. Add(' A: texta;');
  17345. Add(' C: textaclass;');
  17346. Add('begin');
  17347. Add(' a:=c.new;');
  17348. Add(' a:=c.new();');
  17349. Add(' with C do begin');
  17350. Add(' a:=new;');
  17351. Add(' a:=new();');
  17352. Add(' end;');
  17353. Add(' a:=test1.c.new;');
  17354. Add(' a:=test1.c.new();');
  17355. Add(' a:=A.c.new();');
  17356. ConvertProgram;
  17357. CheckSource('TestExternalClass_ClassOf_New',
  17358. LinesToStr([ // statements
  17359. 'this.A = null;',
  17360. 'this.C = null;',
  17361. '']),
  17362. LinesToStr([ // $mod.$main
  17363. '$mod.A = new $mod.C();',
  17364. '$mod.A = new $mod.C();',
  17365. 'var $with = $mod.C;',
  17366. '$mod.A = new $with();',
  17367. '$mod.A = new $with();',
  17368. '$mod.A = new $mod.C();',
  17369. '$mod.A = new $mod.C();',
  17370. '$mod.A = new $mod.A.C();',
  17371. '']));
  17372. end;
  17373. procedure TTestModule.TestExternalClass_FuncClassOf_New;
  17374. begin
  17375. StartProgram(false);
  17376. Add([
  17377. '{$modeswitch externalclass}',
  17378. 'type',
  17379. ' TExtAClass = class of TExtA;',
  17380. ' TExtA = class external name ''ExtA''',
  17381. ' constructor New;',
  17382. ' end;',
  17383. 'function GetCreator: TExtAClass;',
  17384. 'begin',
  17385. ' Result:=TExtA;',
  17386. 'end;',
  17387. 'var',
  17388. ' A: texta;',
  17389. 'begin',
  17390. ' a:=getcreator.new;',
  17391. ' a:=getcreator().new;',
  17392. ' a:=getcreator().new();',
  17393. ' a:=getcreator.new();',
  17394. ' with getcreator do begin',
  17395. ' a:=new;',
  17396. ' a:=new();',
  17397. ' end;']);
  17398. ConvertProgram;
  17399. CheckSource('TestExternalClass_FuncClassOf_New',
  17400. LinesToStr([ // statements
  17401. 'this.GetCreator = function () {',
  17402. ' var Result = null;',
  17403. ' Result = ExtA;',
  17404. ' return Result;',
  17405. '};',
  17406. 'this.A = null;',
  17407. '']),
  17408. LinesToStr([ // $mod.$main
  17409. '$mod.A = new ($mod.GetCreator())();',
  17410. '$mod.A = new ($mod.GetCreator())();',
  17411. '$mod.A = new ($mod.GetCreator())();',
  17412. '$mod.A = new ($mod.GetCreator())();',
  17413. 'var $with = $mod.GetCreator();',
  17414. '$mod.A = new $with();',
  17415. '$mod.A = new $with();',
  17416. '']));
  17417. end;
  17418. procedure TTestModule.TestExternalClass_New_PasClassFail;
  17419. begin
  17420. StartProgram(false);
  17421. Add([
  17422. '{$modeswitch externalclass}',
  17423. 'type',
  17424. ' TExtA = class external name ''ExtA''',
  17425. ' constructor New;',
  17426. ' end;',
  17427. ' TBird = class(TExtA)',
  17428. ' end;',
  17429. 'begin',
  17430. ' TBird.new;',
  17431. '']);
  17432. SetExpectedPasResolverError(sJSNewNotSupported,nJSNewNotSupported);
  17433. ConvertProgram;
  17434. end;
  17435. procedure TTestModule.TestExternalClass_New_PasClassBracketsFail;
  17436. begin
  17437. StartProgram(false);
  17438. Add([
  17439. '{$modeswitch externalclass}',
  17440. 'type',
  17441. ' TExtA = class external name ''ExtA''',
  17442. ' constructor New;',
  17443. ' end;',
  17444. ' TBird = class(TExtA)',
  17445. ' end;',
  17446. 'begin',
  17447. ' TBird.new();',
  17448. '']);
  17449. SetExpectedPasResolverError(sJSNewNotSupported,nJSNewNotSupported);
  17450. ConvertProgram;
  17451. end;
  17452. procedure TTestModule.TestExternalClass_NewExtName;
  17453. begin
  17454. StartProgram(false);
  17455. Add([
  17456. '{$modeswitch externalclass}',
  17457. 'type',
  17458. ' TExtA = class external name ''ExtA''',
  17459. ' constructor New; external name ''Other'';',
  17460. ' constructor New(i: longint; j: longint = 2); external name ''A.B'';',
  17461. ' end;',
  17462. 'var',
  17463. ' A: texta;',
  17464. 'begin',
  17465. ' a:=texta.new;',
  17466. ' a:=texta(texta.new);',
  17467. ' a:=texta.new();',
  17468. ' a:=texta.new(1);',
  17469. ' with texta do begin',
  17470. ' a:=new;',
  17471. ' a:=new();',
  17472. ' a:=new(2);',
  17473. ' end;',
  17474. ' a:=test1.texta.new;',
  17475. ' a:=test1.texta.new();',
  17476. ' a:=test1.texta.new(3);',
  17477. '']);
  17478. ConvertProgram;
  17479. CheckSource('TestExternalClass_NewExtName',
  17480. LinesToStr([ // statements
  17481. 'this.A = null;',
  17482. '']),
  17483. LinesToStr([ // $mod.$main
  17484. '$mod.A = new Other();',
  17485. '$mod.A = new Other();',
  17486. '$mod.A = new Other();',
  17487. '$mod.A = new A.B(1,2);',
  17488. '$mod.A = new Other();',
  17489. '$mod.A = new Other();',
  17490. '$mod.A = new A.B(2,2);',
  17491. '$mod.A = new Other();',
  17492. '$mod.A = new Other();',
  17493. '$mod.A = new A.B(3,2);',
  17494. '']));
  17495. end;
  17496. procedure TTestModule.TestExternalClass_Constructor;
  17497. begin
  17498. StartProgram(false);
  17499. Add([
  17500. '{$modeswitch externalclass}',
  17501. 'type',
  17502. ' TExtA = class external name ''ExtA''',
  17503. ' public type',
  17504. ' TExtB = class external name ''ExtB''',
  17505. ' public type',
  17506. ' TExtC = class external name ''ExtC''',
  17507. ' constructor New;',
  17508. ' constructor New(i: word);',
  17509. ' end;',
  17510. ' end;',
  17511. ' constructor Create;',
  17512. ' constructor Create(i: longint; j: longint = 2);',
  17513. ' end;',
  17514. 'var',
  17515. ' A: texta;',
  17516. ' C: texta.textb.textc;',
  17517. 'begin',
  17518. ' a:=texta.create;',
  17519. ' a:=texta(texta.create);',
  17520. ' a:=texta.create();',
  17521. ' a:=texta.create(1);',
  17522. ' with texta do begin',
  17523. ' a:=create;',
  17524. ' a:=create();',
  17525. ' a:=create(2);',
  17526. ' end;',
  17527. ' a:=test1.texta.create;',
  17528. ' a:=test1.texta.create();',
  17529. ' a:=test1.texta.create(3);',
  17530. ' c:=texta.textb.textc.new;',
  17531. ' c:=texta.textb.textc.new();',
  17532. ' c:=texta.textb.textc.new(4);',
  17533. '']);
  17534. ConvertProgram;
  17535. CheckSource('TestExternalClass_Constructor',
  17536. LinesToStr([ // statements
  17537. 'this.A = null;',
  17538. 'this.C = null;',
  17539. '']),
  17540. LinesToStr([ // $mod.$main
  17541. '$mod.A = new ExtA.Create();',
  17542. '$mod.A = new ExtA.Create();',
  17543. '$mod.A = new ExtA.Create();',
  17544. '$mod.A = new ExtA.Create(1,2);',
  17545. '$mod.A = new ExtA.Create();',
  17546. '$mod.A = new ExtA.Create();',
  17547. '$mod.A = new ExtA.Create(2,2);',
  17548. '$mod.A = new ExtA.Create();',
  17549. '$mod.A = new ExtA.Create();',
  17550. '$mod.A = new ExtA.Create(3,2);',
  17551. '$mod.C = new ExtA.ExtB.ExtC();',
  17552. '$mod.C = new ExtA.ExtB.ExtC();',
  17553. '$mod.C = new ExtA.ExtB.ExtC(4);',
  17554. '']));
  17555. end;
  17556. procedure TTestModule.TestExternalClass_ConstructorBrackets;
  17557. begin
  17558. StartProgram(false);
  17559. Add([
  17560. '{$modeswitch externalclass}',
  17561. 'type',
  17562. ' TExtA = class external name ''ExtA''',
  17563. ' constructor Create; external name ''{}'';',
  17564. ' end;',
  17565. 'var',
  17566. ' A: texta;',
  17567. 'begin',
  17568. ' a:=texta.create;',
  17569. ' a:=texta(texta.create);',
  17570. ' a:=texta.create();',
  17571. ' with texta do begin',
  17572. ' a:=create;',
  17573. ' a:=create();',
  17574. ' end;',
  17575. ' a:=test1.texta.create;',
  17576. ' a:=test1.texta.create();',
  17577. '']);
  17578. ConvertProgram;
  17579. CheckSource('TestExternalClass_ConstructorBrackets',
  17580. LinesToStr([ // statements
  17581. 'this.A = null;',
  17582. '']),
  17583. LinesToStr([ // $mod.$main
  17584. '$mod.A = {};',
  17585. '$mod.A = {};',
  17586. '$mod.A = {};',
  17587. '$mod.A = {};',
  17588. '$mod.A = {};',
  17589. '$mod.A = {};',
  17590. '$mod.A = {};',
  17591. '']));
  17592. end;
  17593. procedure TTestModule.TestExternalClass_LocalConstSameName;
  17594. begin
  17595. StartProgram(false);
  17596. Add('{$modeswitch externalclass}');
  17597. Add('type');
  17598. Add(' TExtA = class external name ''ExtA''');
  17599. Add(' constructor New;');
  17600. Add(' end;');
  17601. Add('function DoIt: longint;');
  17602. Add('const ExtA: longint = 3;');
  17603. Add('begin');
  17604. Add(' Result:=ExtA;');
  17605. Add('end;');
  17606. Add('var');
  17607. Add(' A: texta;');
  17608. Add('begin');
  17609. Add(' a:=texta.new;');
  17610. ConvertProgram;
  17611. CheckSource('TestExternalClass_LocalConstSameName',
  17612. LinesToStr([ // statements
  17613. 'var ExtA$1 = 3;',
  17614. 'this.DoIt = function () {',
  17615. ' var Result = 0;',
  17616. ' Result = ExtA$1;',
  17617. ' return Result;',
  17618. '};',
  17619. 'this.A = null;',
  17620. '']),
  17621. LinesToStr([ // $mod.$main
  17622. '$mod.A = new ExtA();',
  17623. '']));
  17624. end;
  17625. procedure TTestModule.TestExternalClass_ReintroduceOverload;
  17626. begin
  17627. StartProgram(false);
  17628. Add('{$modeswitch externalclass}');
  17629. Add('type');
  17630. Add(' TExtA = class external name ''ExtA''');
  17631. Add(' procedure DoIt;');
  17632. Add(' end;');
  17633. Add(' TMyA = class(TExtA)');
  17634. Add(' procedure DoIt;');
  17635. Add(' end;');
  17636. Add('procedure TMyA.DoIt; begin end;');
  17637. Add('begin');
  17638. ConvertProgram;
  17639. CheckSource('TestExternalClass_ReintroduceOverload',
  17640. LinesToStr([ // statements
  17641. 'rtl.createClassExt(this, "TMyA", ExtA, "", function () {',
  17642. ' this.$init = function () {',
  17643. ' };',
  17644. ' this.$final = function () {',
  17645. ' };',
  17646. ' this.DoIt$1 = function () {',
  17647. ' };',
  17648. '});',
  17649. '']),
  17650. LinesToStr([ // $mod.$main
  17651. '']));
  17652. end;
  17653. procedure TTestModule.TestExternalClass_Inherited;
  17654. begin
  17655. StartProgram(false);
  17656. Add('{$modeswitch externalclass}');
  17657. Add('type');
  17658. Add(' TExtA = class external name ''ExtA''');
  17659. Add(' procedure DoIt(i: longint = 1); virtual;');
  17660. Add(' procedure DoSome(j: longint = 2);');
  17661. Add(' end;');
  17662. Add(' TExtB = class external name ''ExtB''(TExtA)');
  17663. Add(' end;');
  17664. Add(' TMyC = class(TExtB)');
  17665. Add(' procedure DoIt(i: longint = 1); override;');
  17666. Add(' procedure DoSome(j: longint = 2); reintroduce;');
  17667. Add(' end;');
  17668. Add('procedure TMyC.DoIt(i: longint);');
  17669. Add('begin');
  17670. Add(' inherited;');
  17671. Add(' inherited DoIt;');
  17672. Add(' inherited DoIt();');
  17673. Add(' inherited DoIt(3);');
  17674. Add(' inherited DoSome;');
  17675. Add(' inherited DoSome();');
  17676. Add(' inherited DoSome(4);');
  17677. Add('end;');
  17678. Add('procedure TMyC.DoSome(j: longint);');
  17679. Add('begin');
  17680. Add(' inherited;');
  17681. Add('end;');
  17682. Add('begin');
  17683. ConvertProgram;
  17684. CheckSource('TestExternalClass_ReintroduceOverload',
  17685. LinesToStr([ // statements
  17686. 'rtl.createClassExt(this, "TMyC", ExtB, "", function () {',
  17687. ' this.$init = function () {',
  17688. ' };',
  17689. ' this.$final = function () {',
  17690. ' };',
  17691. ' this.DoIt = function (i) {',
  17692. ' ExtB.DoIt.apply(this, arguments);',
  17693. ' ExtB.DoIt.call(this, 1);',
  17694. ' ExtB.DoIt.call(this, 1);',
  17695. ' ExtB.DoIt.call(this, 3);',
  17696. ' ExtB.DoSome.call(this, 2);',
  17697. ' ExtB.DoSome.call(this, 2);',
  17698. ' ExtB.DoSome.call(this, 4);',
  17699. ' };',
  17700. ' this.DoSome$1 = function (j) {',
  17701. ' ExtB.DoSome.apply(this, arguments);',
  17702. ' };',
  17703. '});',
  17704. '']),
  17705. LinesToStr([ // $mod.$main
  17706. '']));
  17707. end;
  17708. procedure TTestModule.TestExternalClass_PascalAncestorFail;
  17709. begin
  17710. StartProgram(false);
  17711. Add('{$modeswitch externalclass}');
  17712. Add('type');
  17713. Add(' TObject = class');
  17714. Add(' end;');
  17715. Add(' TExtA = class external name ''ExtA''(TObject)');
  17716. Add(' end;');
  17717. Add('begin');
  17718. SetExpectedPasResolverError('Ancestor "TObject" is not external',nAncestorIsNotExternal);
  17719. ConvertProgram;
  17720. end;
  17721. procedure TTestModule.TestExternalClass_NewInstance;
  17722. begin
  17723. StartProgram(false);
  17724. Add('{$modeswitch externalclass}');
  17725. Add('type');
  17726. Add(' TExtA = class external name ''ExtA''');
  17727. Add(' end;');
  17728. Add(' TMyB = class(TExtA)');
  17729. Add(' protected');
  17730. Add(' class function NewInstance(fnname: string; const paramarray): TMyB; virtual;');
  17731. Add(' end;');
  17732. Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
  17733. Add('begin end;');
  17734. Add('begin');
  17735. ConvertProgram;
  17736. CheckSource('TestExternalClass_NewInstance',
  17737. LinesToStr([ // statements
  17738. 'rtl.createClassExt(this, "TMyB", ExtA, "NewInstance", function () {',
  17739. ' this.$init = function () {',
  17740. ' };',
  17741. ' this.$final = function () {',
  17742. ' };',
  17743. ' this.NewInstance = function (fnname, paramarray) {',
  17744. ' var Result = null;',
  17745. ' return Result;',
  17746. ' };',
  17747. '});',
  17748. '']),
  17749. LinesToStr([ // $mod.$main
  17750. '']));
  17751. end;
  17752. procedure TTestModule.TestExternalClass_NewInstance_NonVirtualFail;
  17753. begin
  17754. StartProgram(false);
  17755. Add('{$modeswitch externalclass}');
  17756. Add('type');
  17757. Add(' TExtA = class external name ''ExtA''');
  17758. Add(' end;');
  17759. Add(' TMyB = class(TExtA)');
  17760. Add(' protected');
  17761. Add(' class function NewInstance(fnname: string; const paramarray): TMyB;');
  17762. Add(' end;');
  17763. Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
  17764. Add('begin end;');
  17765. Add('begin');
  17766. SetExpectedPasResolverError(sNewInstanceFunctionMustBeVirtual,nNewInstanceFunctionMustBeVirtual);
  17767. ConvertProgram;
  17768. end;
  17769. procedure TTestModule.TestExternalClass_NewInstance_FirstParamNotString_Fail;
  17770. begin
  17771. StartProgram(false);
  17772. Add('{$modeswitch externalclass}');
  17773. Add('type');
  17774. Add(' TExtA = class external name ''ExtA''');
  17775. Add(' end;');
  17776. Add(' TMyB = class(TExtA)');
  17777. Add(' protected');
  17778. Add(' class function NewInstance(fnname: longint; const paramarray): TMyB; virtual;');
  17779. Add(' end;');
  17780. Add('class function TMyB.NewInstance(fnname: longint; const paramarray): TMyB;');
  17781. Add('begin end;');
  17782. Add('begin');
  17783. SetExpectedPasResolverError('Incompatible type arg no. 1: Got "Longint", expected "String"',
  17784. nIncompatibleTypeArgNo);
  17785. ConvertProgram;
  17786. end;
  17787. procedure TTestModule.TestExternalClass_NewInstance_SecondParamTyped_Fail;
  17788. begin
  17789. StartProgram(false);
  17790. Add('{$modeswitch externalclass}');
  17791. Add('type');
  17792. Add(' TExtA = class external name ''ExtA''');
  17793. Add(' end;');
  17794. Add(' TMyB = class(TExtA)');
  17795. Add(' protected');
  17796. Add(' class function NewInstance(fnname: string; const paramarray: string): TMyB; virtual;');
  17797. Add(' end;');
  17798. Add('class function TMyB.NewInstance(fnname: string; const paramarray: string): TMyB;');
  17799. Add('begin end;');
  17800. Add('begin');
  17801. SetExpectedPasResolverError('Incompatible type arg no. 2: Got "type", expected "untyped"',
  17802. nIncompatibleTypeArgNo);
  17803. ConvertProgram;
  17804. end;
  17805. procedure TTestModule.TestExternalClass_JSFunctionPasDescendant;
  17806. begin
  17807. StartProgram(false);
  17808. Add([
  17809. '{$modeswitch externalclass}',
  17810. 'type',
  17811. ' TJSFunction = class external name ''Function''',
  17812. ' end;',
  17813. ' TExtA = class external name ''ExtA''(TJSFunction)',
  17814. ' constructor New(w: word);',
  17815. ' end;',
  17816. ' TBird = class (TExtA)',
  17817. ' public',
  17818. ' Size: word;',
  17819. ' class var Legs: word;',
  17820. ' constructor Create(a: word);',
  17821. ' end;',
  17822. ' TEagle = class (TBird)',
  17823. ' public',
  17824. ' constructor Create(b: word); reintroduce;',
  17825. ' end;',
  17826. 'constructor TBird.Create(a: word);',
  17827. 'begin',
  17828. ' inherited;', // silently ignored
  17829. ' inherited New(a);', // this.$func(a)
  17830. 'end;',
  17831. 'constructor TEagle.Create(b: word);',
  17832. 'begin',
  17833. ' inherited Create(b);',
  17834. 'end;',
  17835. 'var',
  17836. ' Bird: TBird;',
  17837. ' Eagle: TEagle;',
  17838. 'begin',
  17839. ' Bird:=TBird.Create(3);',
  17840. ' Eagle:=TEagle.Create(4);',
  17841. ' Bird.Size:=Bird.Size+5;',
  17842. ' Bird.Legs:=Bird.Legs+6;',
  17843. ' Eagle.Size:=Eagle.Size+5;',
  17844. ' Eagle.Legs:=Eagle.Legs+6;',
  17845. '']);
  17846. ConvertProgram;
  17847. CheckSource('TestExternalClass_JSFunctionPasDescendant',
  17848. LinesToStr([ // statements
  17849. 'rtl.createClassExt(this, "TBird", ExtA, "", function () {',
  17850. ' this.Legs = 0;',
  17851. ' this.$init = function () {',
  17852. ' this.Size = 0;',
  17853. ' };',
  17854. ' this.$final = function () {',
  17855. ' };',
  17856. ' this.Create = function (a) {',
  17857. ' this.$ancestorfunc(a);',
  17858. ' return this;',
  17859. ' };',
  17860. '});',
  17861. 'rtl.createClassExt(this, "TEagle", this.TBird, "", function () {',
  17862. ' this.Create$1 = function (b) {',
  17863. ' $mod.TBird.Create.call(this, b);',
  17864. ' return this;',
  17865. ' };',
  17866. '});',
  17867. 'this.Bird = null;',
  17868. 'this.Eagle = null;',
  17869. '']),
  17870. LinesToStr([ // $mod.$main
  17871. '$mod.Bird = $mod.TBird.$create("Create", [3]);',
  17872. '$mod.Eagle = $mod.TEagle.$create("Create$1", [4]);',
  17873. '$mod.Bird.Size = $mod.Bird.Size + 5;',
  17874. '$mod.TBird.Legs = $mod.Bird.Legs + 6;',
  17875. '$mod.Eagle.Size = $mod.Eagle.Size + 5;',
  17876. '$mod.TBird.Legs = $mod.Eagle.Legs + 6;',
  17877. '']));
  17878. end;
  17879. procedure TTestModule.TestExternalClass_PascalProperty;
  17880. begin
  17881. StartProgram(false);
  17882. Add('{$modeswitch externalclass}');
  17883. Add('type');
  17884. Add(' TJSElement = class;');
  17885. Add(' TJSNotifyEvent = procedure(Sender: TJSElement) of object;');
  17886. Add(' TJSElement = class external name ''ExtA''');
  17887. Add(' end;');
  17888. Add(' TControl = class(TJSElement)');
  17889. Add(' private');
  17890. Add(' FOnClick: TJSNotifyEvent;');
  17891. Add(' property OnClick: TJSNotifyEvent read FOnClick write FOnClick;');
  17892. Add(' procedure Click(Sender: TJSElement);');
  17893. Add(' end;');
  17894. Add('procedure TControl.Click(Sender: TJSElement);');
  17895. Add('begin');
  17896. Add(' OnClick(Self);');
  17897. Add('end;');
  17898. Add('var');
  17899. Add(' Ctrl: TControl;');
  17900. Add('begin');
  17901. Add(' Ctrl.OnClick:[email protected];');
  17902. Add(' Ctrl.OnClick(Ctrl);');
  17903. ConvertProgram;
  17904. CheckSource('TestExternalClass_PascalProperty',
  17905. LinesToStr([ // statements
  17906. 'rtl.createClassExt(this, "TControl", ExtA, "", function () {',
  17907. ' this.$init = function () {',
  17908. ' this.FOnClick = null;',
  17909. ' };',
  17910. ' this.$final = function () {',
  17911. ' this.FOnClick = undefined;',
  17912. ' };',
  17913. ' this.Click = function (Sender) {',
  17914. ' this.FOnClick(this);',
  17915. ' };',
  17916. '});',
  17917. 'this.Ctrl = null;',
  17918. '']),
  17919. LinesToStr([ // $mod.$main
  17920. '$mod.Ctrl.FOnClick = rtl.createCallback($mod.Ctrl, "Click");',
  17921. '$mod.Ctrl.FOnClick($mod.Ctrl);',
  17922. '']));
  17923. end;
  17924. procedure TTestModule.TestExternalClass_TypeCastToRootClass;
  17925. begin
  17926. StartProgram(false);
  17927. Add([
  17928. '{$modeswitch externalclass}',
  17929. 'type',
  17930. ' IUnknown = interface end;',
  17931. ' TObject = class',
  17932. ' end;',
  17933. ' TChild = class',
  17934. ' end;',
  17935. ' TExtRootA = class external name ''ExtRootA''',
  17936. ' end;',
  17937. ' TExtChildA = class external name ''ExtChildA''(TExtRootA)',
  17938. ' end;',
  17939. ' TExtRootB = class external name ''ExtRootB''',
  17940. ' end;',
  17941. ' TExtChildB = class external name ''ExtChildB''(TExtRootB)',
  17942. ' end;',
  17943. 'var',
  17944. ' Obj: TObject;',
  17945. ' Child: TChild;',
  17946. ' RootA: TExtRootA;',
  17947. ' ChildA: TExtChildA;',
  17948. ' RootB: TExtRootB;',
  17949. ' ChildB: TExtChildB;',
  17950. ' i: IUnknown;',
  17951. 'begin',
  17952. ' obj:=tobject(roota);',
  17953. ' obj:=tobject(childa);',
  17954. ' child:=tchild(tobject(roota));',
  17955. ' roota:=textroota(obj);',
  17956. ' roota:=textroota(child);',
  17957. ' roota:=textroota(rootb);',
  17958. ' roota:=textroota(childb);',
  17959. ' childa:=textchilda(textroota(obj));',
  17960. ' roota:=TExtRootA(i)',
  17961. '']);
  17962. ConvertProgram;
  17963. CheckSource('TestExternalClass_TypeCastToRootClass',
  17964. LinesToStr([ // statements
  17965. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  17966. 'rtl.createClass(this, "TObject", null, function () {',
  17967. ' this.$init = function () {',
  17968. ' };',
  17969. ' this.$final = function () {',
  17970. ' };',
  17971. '});',
  17972. 'rtl.createClass(this, "TChild", this.TObject, function () {',
  17973. '});',
  17974. 'this.Obj = null;',
  17975. 'this.Child = null;',
  17976. 'this.RootA = null;',
  17977. 'this.ChildA = null;',
  17978. 'this.RootB = null;',
  17979. 'this.ChildB = null;',
  17980. 'this.i = null;',
  17981. '']),
  17982. LinesToStr([ // $mod.$main
  17983. '$mod.Obj = $mod.RootA;',
  17984. '$mod.Obj = $mod.ChildA;',
  17985. '$mod.Child = $mod.RootA;',
  17986. '$mod.RootA = $mod.Obj;',
  17987. '$mod.RootA = $mod.Child;',
  17988. '$mod.RootA = $mod.RootB;',
  17989. '$mod.RootA = $mod.ChildB;',
  17990. '$mod.ChildA = $mod.Obj;',
  17991. '$mod.RootA = $mod.i;',
  17992. '']));
  17993. end;
  17994. procedure TTestModule.TestExternalClass_TypeCastToJSObject;
  17995. begin
  17996. StartProgram(false);
  17997. Add([
  17998. '{$modeswitch externalclass}',
  17999. 'type',
  18000. ' IUnknown = interface end;',
  18001. ' IBird = interface(IUnknown) end;',
  18002. ' TClass = class of TObject;',
  18003. ' TObject = class',
  18004. ' end;',
  18005. ' TChild = class',
  18006. ' end;',
  18007. ' TJSObject = class external name ''Object''',
  18008. ' end;',
  18009. ' TRec = record end;',
  18010. 'var',
  18011. ' Obj: TObject;',
  18012. ' Child: TChild;',
  18013. ' i: IUnknown;',
  18014. ' Bird: IBird;',
  18015. ' j: TJSObject;',
  18016. ' r: TRec;',
  18017. ' c: TClass;',
  18018. 'begin',
  18019. ' j:=tjsobject(IUnknown);',
  18020. ' j:=tjsobject(IBird);',
  18021. ' j:=tjsobject(TObject);',
  18022. ' j:=tjsobject(TChild);',
  18023. ' j:=tjsobject(TRec);',
  18024. ' j:=tjsobject(Obj);',
  18025. ' j:=tjsobject(Child);',
  18026. ' j:=tjsobject(i);',
  18027. ' j:=tjsobject(Bird);',
  18028. ' j:=tjsobject(r);',
  18029. ' j:=tjsobject(c);',
  18030. '']);
  18031. ConvertProgram;
  18032. CheckSource('TestExternalClass_TypeCastToJSObject',
  18033. LinesToStr([ // statements
  18034. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  18035. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], this.IUnknown);',
  18036. 'rtl.createClass(this, "TObject", null, function () {',
  18037. ' this.$init = function () {',
  18038. ' };',
  18039. ' this.$final = function () {',
  18040. ' };',
  18041. '});',
  18042. 'rtl.createClass(this, "TChild", this.TObject, function () {',
  18043. '});',
  18044. 'rtl.recNewT(this, "TRec", function () {',
  18045. ' this.$eq = function (b) {',
  18046. ' return true;',
  18047. ' };',
  18048. ' this.$assign = function (s) {',
  18049. ' return this;',
  18050. ' };',
  18051. '});',
  18052. 'this.Obj = null;',
  18053. 'this.Child = null;',
  18054. 'this.i = null;',
  18055. 'this.Bird = null;',
  18056. 'this.j = null;',
  18057. 'this.r = this.TRec.$new();',
  18058. 'this.c = null;',
  18059. '']),
  18060. LinesToStr([ // $mod.$main
  18061. '$mod.j = $mod.IUnknown;',
  18062. '$mod.j = $mod.IBird;',
  18063. '$mod.j = $mod.TObject;',
  18064. '$mod.j = $mod.TChild;',
  18065. '$mod.j = $mod.TRec;',
  18066. '$mod.j = $mod.Obj;',
  18067. '$mod.j = $mod.Child;',
  18068. '$mod.j = $mod.i;',
  18069. '$mod.j = $mod.Bird;',
  18070. '$mod.j = $mod.r;',
  18071. '$mod.j = $mod.c;',
  18072. '']));
  18073. end;
  18074. procedure TTestModule.TestExternalClass_TypeCastStringToExternalString;
  18075. begin
  18076. StartProgram(false);
  18077. Add('{$modeswitch externalclass}');
  18078. Add('type');
  18079. Add(' TJSString = class external name ''String''');
  18080. Add(' class function fromCharCode() : string; varargs;');
  18081. Add(' function anchor(const aName : string) : string;');
  18082. Add(' end;');
  18083. Add('var');
  18084. Add(' s: string;');
  18085. Add('begin');
  18086. Add(' s:=TJSString.fromCharCode(65,66);');
  18087. Add(' s:=TJSString(s).anchor(s);');
  18088. Add(' s:=TJSString(''foo'').anchor(s);');
  18089. ConvertProgram;
  18090. CheckSource('TestExternalClass_TypeCastStringToExternalString',
  18091. LinesToStr([ // statements
  18092. 'this.s = "";',
  18093. '']),
  18094. LinesToStr([ // $mod.$main
  18095. '$mod.s = String.fromCharCode(65, 66);',
  18096. '$mod.s = $mod.s.anchor($mod.s);',
  18097. '$mod.s = "foo".anchor($mod.s);',
  18098. '']));
  18099. end;
  18100. procedure TTestModule.TestExternalClass_TypeCastToJSFunction;
  18101. begin
  18102. StartProgram(false);
  18103. Add([
  18104. '{$modeswitch externalclass}',
  18105. 'type',
  18106. ' TJSObject = class external name ''Object'' end;',
  18107. ' TJSFunction = class external name ''Function''',
  18108. ' function bind(thisArg: TJSObject): TJSFunction; varargs;',
  18109. ' function call(thisArg: TJSObject): JSValue; varargs;',
  18110. ' end;',
  18111. ' TObject = class',
  18112. ' procedure DoIt(i: longint);',
  18113. ' end;',
  18114. ' TFuncInt = function(o: TObject): longint;',
  18115. 'function GetIt(o: TObject): longint;',
  18116. ' procedure Sub; begin end;',
  18117. 'var',
  18118. ' f: TJSFunction;',
  18119. ' fi: TFuncInt;',
  18120. 'begin',
  18121. ' fi:=TFuncInt(f);',
  18122. ' f:=TJSFunction(fi);',
  18123. ' f:=TJSFunction(@GetIt);',
  18124. ' f:=TJSFunction(@GetIt).bind(nil,3);',
  18125. ' f:=TJSFunction(@Sub);',
  18126. ' f:=TJSFunction(@o.doit);',
  18127. ' f:=TJSFunction(fi).bind(nil,4)',
  18128. 'end;',
  18129. 'procedure TObject.DoIt(i: longint);',
  18130. ' procedure Sub; begin end;',
  18131. 'var f: TJSFunction;',
  18132. 'begin',
  18133. ' f:=TJSFunction(@DoIt);',
  18134. ' f:=TJSFunction(@DoIt).bind(nil,13);',
  18135. ' f:=TJSFunction(@Sub);',
  18136. ' f:=TJSFunction(@GetIt);',
  18137. 'end;',
  18138. 'begin']);
  18139. ConvertProgram;
  18140. CheckSource('TestExternalClass_TypeCastToJSFunction',
  18141. LinesToStr([ // statements
  18142. 'rtl.createClass(this, "TObject", null, function () {',
  18143. ' this.$init = function () {',
  18144. ' };',
  18145. ' this.$final = function () {',
  18146. ' };',
  18147. ' this.DoIt = function (i) {',
  18148. ' var $Self = this;',
  18149. ' function Sub() {',
  18150. ' };',
  18151. ' var f = null;',
  18152. ' f = this.DoIt;',
  18153. ' f = this.DoIt.bind(null, 13);',
  18154. ' f = Sub;',
  18155. ' f = $mod.GetIt;',
  18156. ' };',
  18157. '});',
  18158. 'this.GetIt = function (o) {',
  18159. ' var Result = 0;',
  18160. ' function Sub() {',
  18161. ' };',
  18162. ' var f = null;',
  18163. ' var fi = null;',
  18164. ' fi = f;',
  18165. ' f = fi;',
  18166. ' f = $mod.GetIt;',
  18167. ' f = $mod.GetIt.bind(null, 3);',
  18168. ' f = Sub;',
  18169. ' f = $mod.TObject.DoIt;',
  18170. ' f = fi.bind(null, 4);',
  18171. ' return Result;',
  18172. '};',
  18173. '']),
  18174. LinesToStr([ // $mod.$main
  18175. '']));
  18176. end;
  18177. procedure TTestModule.TestExternalClass_TypeCastDelphiUnrelated;
  18178. begin
  18179. StartProgram(false);
  18180. Add([
  18181. '{$mode delphi}',
  18182. '{$modeswitch externalclass}',
  18183. 'type',
  18184. ' TJSObject = class external name ''Object'' end;',
  18185. ' TJSWindow = class external name ''Window''(TJSObject)',
  18186. ' procedure Open;',
  18187. ' end;',
  18188. ' TJSEventTarget = class external name ''Event''(TJSObject)',
  18189. ' procedure Execute;',
  18190. ' end;',
  18191. 'procedure Fly;',
  18192. 'var',
  18193. ' w: TJSWindow;',
  18194. ' e: TJSEventTarget;',
  18195. 'begin',
  18196. ' w:=TJSWindow(e);',
  18197. ' e:=TJSEventTarget(w);',
  18198. 'end;',
  18199. 'begin']);
  18200. ConvertProgram;
  18201. CheckSource('TestExternalClass_TypeCastDelphiUnrelated',
  18202. LinesToStr([ // statements
  18203. 'this.Fly = function () {',
  18204. ' var w = null;',
  18205. ' var e = null;',
  18206. ' w = e;',
  18207. ' e = w;',
  18208. '};',
  18209. '']),
  18210. LinesToStr([ // $mod.$main
  18211. '']));
  18212. end;
  18213. procedure TTestModule.TestExternalClass_CallClassFunctionOfInstanceFail;
  18214. begin
  18215. StartProgram(false);
  18216. Add('{$modeswitch externalclass}');
  18217. Add('type');
  18218. Add(' TJSString = class external name ''String''');
  18219. Add(' class function fromCharCode() : string; varargs;');
  18220. Add(' end;');
  18221. Add('var');
  18222. Add(' s: string;');
  18223. Add(' sObj: TJSString;');
  18224. Add('begin');
  18225. Add(' s:=sObj.fromCharCode(65,66);');
  18226. SetExpectedPasResolverError('External class instance cannot access static class function fromCharCode',
  18227. nExternalClassInstanceCannotAccessStaticX);
  18228. ConvertProgram;
  18229. end;
  18230. procedure TTestModule.TestExternalClass_BracketAccessor;
  18231. begin
  18232. StartProgram(false);
  18233. Add([
  18234. '{$modeswitch externalclass}',
  18235. 'type',
  18236. ' TJSArray = class external name ''Array2''',
  18237. ' function GetItems(Index: longint): jsvalue; external name ''[]'';',
  18238. ' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';',
  18239. ' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;',
  18240. ' end;',
  18241. 'procedure DoIt(vI: JSValue; const vJ: jsvalue; var vK: jsvalue; out vL: jsvalue);',
  18242. 'begin end;',
  18243. 'var',
  18244. ' Arr: tjsarray;',
  18245. ' s: string;',
  18246. ' i: longint;',
  18247. ' v: jsvalue;',
  18248. 'begin',
  18249. ' v:=arr[0];',
  18250. ' v:=arr.items[1];',
  18251. ' arr[2]:=s;',
  18252. ' arr.items[3]:=s;',
  18253. ' arr[4]:=i;',
  18254. ' arr[5]:=arr[6];',
  18255. ' arr.items[7]:=arr.items[8];',
  18256. ' with arr do items[9]:=items[10];',
  18257. ' doit(arr[7],arr[8],arr[9],arr[10]);',
  18258. ' with arr do begin',
  18259. ' v:=GetItems(14);',
  18260. ' setitems(15,16);',
  18261. ' end;',
  18262. ' v:=test1.arr.items[17];',
  18263. ' test1.arr.items[18]:=v;',
  18264. '']);
  18265. ConvertProgram;
  18266. CheckSource('TestExternalClass_BracketAccessor',
  18267. LinesToStr([ // statements
  18268. 'this.DoIt = function (vI, vJ, vK, vL) {',
  18269. '};',
  18270. 'this.Arr = null;',
  18271. 'this.s = "";',
  18272. 'this.i = 0;',
  18273. 'this.v = undefined;',
  18274. '']),
  18275. LinesToStr([ // $mod.$main
  18276. '$mod.v = $mod.Arr[0];',
  18277. '$mod.v = $mod.Arr[1];',
  18278. '$mod.Arr[2] = $mod.s;',
  18279. '$mod.Arr[3] = $mod.s;',
  18280. '$mod.Arr[4] = $mod.i;',
  18281. '$mod.Arr[5] = $mod.Arr[6];',
  18282. '$mod.Arr[7] = $mod.Arr[8];',
  18283. 'var $with = $mod.Arr;',
  18284. '$with[9] = $with[10];',
  18285. '$mod.DoIt($mod.Arr[7], $mod.Arr[8], {',
  18286. ' a: 9,',
  18287. ' p: $mod.Arr,',
  18288. ' get: function () {',
  18289. ' return this.p[this.a];',
  18290. ' },',
  18291. ' set: function (v) {',
  18292. ' this.p[this.a] = v;',
  18293. ' }',
  18294. '}, {',
  18295. ' a: 10,',
  18296. ' p: $mod.Arr,',
  18297. ' get: function () {',
  18298. ' return this.p[this.a];',
  18299. ' },',
  18300. ' set: function (v) {',
  18301. ' this.p[this.a] = v;',
  18302. ' }',
  18303. '});',
  18304. 'var $with1 = $mod.Arr;',
  18305. '$mod.v = $with1[14];',
  18306. '$with1[15] = 16;',
  18307. '$mod.v = $mod.Arr[17];',
  18308. '$mod.Arr[18] = $mod.v;',
  18309. '']));
  18310. end;
  18311. procedure TTestModule.TestExternalClass_BracketAccessor_Call;
  18312. begin
  18313. StartProgram(false);
  18314. Add([
  18315. '{$modeswitch externalclass}',
  18316. 'type',
  18317. ' TJSArray = class external name ''Array2''',
  18318. ' function GetItems(Index: longint): jsvalue; external name ''[]'';',
  18319. ' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';',
  18320. ' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;',
  18321. ' end;',
  18322. ' TMyArr = class(TJSArray)',
  18323. ' procedure DoIt;',
  18324. ' end;',
  18325. 'procedure tmyarr.DoIt;',
  18326. 'begin',
  18327. ' Items[1]:=Items[2];',
  18328. ' SetItems(3,getItems(4));',
  18329. 'end;',
  18330. 'var',
  18331. ' Arr: tmyarr;',
  18332. ' s: string;',
  18333. ' i: longint;',
  18334. ' v: jsvalue;',
  18335. 'begin',
  18336. ' v:=arr[0];',
  18337. ' v:=arr.items[1];',
  18338. ' arr[2]:=s;',
  18339. ' arr.items[3]:=s;',
  18340. ' arr[4]:=i;',
  18341. ' arr[5]:=arr[6];',
  18342. ' arr.items[7]:=arr.items[8];',
  18343. ' with arr do items[9]:=items[10];',
  18344. ' with arr do begin',
  18345. ' v:=GetItems(14);',
  18346. ' setitems(15,16);',
  18347. ' end;',
  18348. '']);
  18349. ConvertProgram;
  18350. CheckSource('TestExternalClass_BracketAccessor_Call',
  18351. LinesToStr([ // statements
  18352. 'rtl.createClassExt(this, "TMyArr", Array2, "", function () {',
  18353. ' this.$init = function () {',
  18354. ' };',
  18355. ' this.$final = function () {',
  18356. ' };',
  18357. ' this.DoIt = function () {',
  18358. ' this[1] = this[2];',
  18359. ' this[3] = this[4];',
  18360. ' };',
  18361. '});',
  18362. 'this.Arr = null;',
  18363. 'this.s = "";',
  18364. 'this.i = 0;',
  18365. 'this.v = undefined;',
  18366. '']),
  18367. LinesToStr([ // $mod.$main
  18368. '$mod.v = $mod.Arr[0];',
  18369. '$mod.v = $mod.Arr[1];',
  18370. '$mod.Arr[2] = $mod.s;',
  18371. '$mod.Arr[3] = $mod.s;',
  18372. '$mod.Arr[4] = $mod.i;',
  18373. '$mod.Arr[5] = $mod.Arr[6];',
  18374. '$mod.Arr[7] = $mod.Arr[8];',
  18375. 'var $with = $mod.Arr;',
  18376. '$with[9] = $with[10];',
  18377. 'var $with1 = $mod.Arr;',
  18378. '$mod.v = $with1[14];',
  18379. '$with1[15] = 16;',
  18380. '']));
  18381. end;
  18382. procedure TTestModule.TestExternalClass_BracketAccessor_2ParamsFail;
  18383. begin
  18384. StartProgram(false);
  18385. Add('{$modeswitch externalclass}');
  18386. Add('type');
  18387. Add(' TJSArray = class external name ''Array2''');
  18388. Add(' function GetItems(Index1, Index2: longint): jsvalue; external name ''[]'';');
  18389. Add(' procedure SetItems(Index1, Index2: longint; Value: jsvalue); external name ''[]'';');
  18390. Add(' property Items[Index1, Index2: longint]: jsvalue read GetItems write SetItems; default;');
  18391. Add(' end;');
  18392. Add('begin');
  18393. SetExpectedPasResolverError(sBracketAccessorOfExternalClassMustHaveOneParameter,
  18394. nBracketAccessorOfExternalClassMustHaveOneParameter);
  18395. ConvertProgram;
  18396. end;
  18397. procedure TTestModule.TestExternalClass_BracketAccessor_ReadOnly;
  18398. begin
  18399. StartProgram(false);
  18400. Add('{$modeswitch externalclass}');
  18401. Add('type');
  18402. Add(' TJSArray = class external name ''Array2''');
  18403. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  18404. Add(' property Items[Index: longint]: jsvalue read GetItems; default;');
  18405. Add(' end;');
  18406. Add('procedure DoIt(vI: JSValue; const vJ: jsvalue);');
  18407. Add('begin end;');
  18408. Add('var');
  18409. Add(' Arr: tjsarray;');
  18410. Add(' v: jsvalue;');
  18411. Add('begin');
  18412. Add(' v:=arr[0];');
  18413. Add(' v:=arr.items[1];');
  18414. Add(' with arr do v:=items[2];');
  18415. Add(' doit(arr[3],arr[4]);');
  18416. ConvertProgram;
  18417. CheckSource('TestExternalClass_BracketAccessor_ReadOnly',
  18418. LinesToStr([ // statements
  18419. 'this.DoIt = function (vI, vJ) {',
  18420. '};',
  18421. 'this.Arr = null;',
  18422. 'this.v = undefined;',
  18423. '']),
  18424. LinesToStr([ // $mod.$main
  18425. '$mod.v = $mod.Arr[0];',
  18426. '$mod.v = $mod.Arr[1];',
  18427. 'var $with = $mod.Arr;',
  18428. '$mod.v = $with[2];',
  18429. '$mod.DoIt($mod.Arr[3], $mod.Arr[4]);',
  18430. '']));
  18431. end;
  18432. procedure TTestModule.TestExternalClass_BracketAccessor_WriteOnly;
  18433. begin
  18434. StartProgram(false);
  18435. Add('{$modeswitch externalclass}');
  18436. Add('type');
  18437. Add(' TJSArray = class external name ''Array2''');
  18438. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  18439. Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
  18440. Add(' end;');
  18441. Add('var');
  18442. Add(' Arr: tjsarray;');
  18443. Add(' s: string;');
  18444. Add(' i: longint;');
  18445. Add(' v: jsvalue;');
  18446. Add('begin');
  18447. Add(' arr[2]:=s;');
  18448. Add(' arr.items[3]:=s;');
  18449. Add(' arr[4]:=i;');
  18450. Add(' with arr do items[5]:=i;');
  18451. ConvertProgram;
  18452. CheckSource('TestExternalClass_BracketAccessor_WriteOnly',
  18453. LinesToStr([ // statements
  18454. 'this.Arr = null;',
  18455. 'this.s = "";',
  18456. 'this.i = 0;',
  18457. 'this.v = undefined;',
  18458. '']),
  18459. LinesToStr([ // $mod.$main
  18460. '$mod.Arr[2] = $mod.s;',
  18461. '$mod.Arr[3] = $mod.s;',
  18462. '$mod.Arr[4] = $mod.i;',
  18463. 'var $with = $mod.Arr;',
  18464. '$with[5] = $mod.i;',
  18465. '']));
  18466. end;
  18467. procedure TTestModule.TestExternalClass_BracketAccessor_MultiType;
  18468. begin
  18469. StartProgram(false);
  18470. Add('{$modeswitch externalclass}');
  18471. Add('type');
  18472. Add(' TJSArray = class external name ''Array2''');
  18473. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  18474. Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
  18475. Add(' procedure SetNumbers(Index: longint; Value: longint); external name ''[]'';');
  18476. Add(' property Numbers[Index: longint]: longint write SetNumbers;');
  18477. Add(' end;');
  18478. Add('var');
  18479. Add(' Arr: tjsarray;');
  18480. Add(' s: string;');
  18481. Add(' i: longint;');
  18482. Add(' v: jsvalue;');
  18483. Add('begin');
  18484. Add(' arr[2]:=s;');
  18485. Add(' arr.items[3]:=s;');
  18486. Add(' arr.numbers[4]:=i;');
  18487. Add(' with arr do items[5]:=i;');
  18488. Add(' with arr do numbers[6]:=i;');
  18489. ConvertProgram;
  18490. CheckSource('TestExternalClass_BracketAccessor_MultiType',
  18491. LinesToStr([ // statements
  18492. 'this.Arr = null;',
  18493. 'this.s = "";',
  18494. 'this.i = 0;',
  18495. 'this.v = undefined;',
  18496. '']),
  18497. LinesToStr([ // $mod.$main
  18498. '$mod.Arr[2] = $mod.s;',
  18499. '$mod.Arr[3] = $mod.s;',
  18500. '$mod.Arr[4] = $mod.i;',
  18501. 'var $with = $mod.Arr;',
  18502. '$with[5] = $mod.i;',
  18503. 'var $with1 = $mod.Arr;',
  18504. '$with1[6] = $mod.i;',
  18505. '']));
  18506. end;
  18507. procedure TTestModule.TestExternalClass_BracketAccessor_Index;
  18508. begin
  18509. StartProgram(false);
  18510. Add('{$modeswitch externalclass}');
  18511. Add('type');
  18512. Add(' TJSArray = class external name ''Array2''');
  18513. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  18514. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  18515. Add(' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;');
  18516. Add(' end;');
  18517. Add('var');
  18518. Add(' Arr: tjsarray;');
  18519. Add(' i: longint;');
  18520. Add(' IntArr: array of longint;');
  18521. Add(' v: jsvalue;');
  18522. Add('begin');
  18523. Add(' v:=arr.items[i];');
  18524. Add(' arr[longint(v)]:=arr.items[intarr[0]];');
  18525. Add(' arr.items[intarr[1]]:=arr[IntArr[2]];');
  18526. ConvertProgram;
  18527. CheckSource('TestExternalClass_BracketAccessor_Index',
  18528. LinesToStr([ // statements
  18529. 'this.Arr = null;',
  18530. 'this.i = 0;',
  18531. 'this.IntArr = [];',
  18532. 'this.v = undefined;',
  18533. '']),
  18534. LinesToStr([ // $mod.$main
  18535. '$mod.v = $mod.Arr[$mod.i];',
  18536. '$mod.Arr[rtl.trunc($mod.v)] = $mod.Arr[$mod.IntArr[0]];',
  18537. '$mod.Arr[$mod.IntArr[1]] = $mod.Arr[$mod.IntArr[2]];',
  18538. '']));
  18539. end;
  18540. procedure TTestModule.TestExternalClass_ForInJSObject;
  18541. begin
  18542. StartProgram(false);
  18543. Add([
  18544. '{$modeswitch externalclass}',
  18545. 'type',
  18546. ' TJSObject = class external name ''Object''',
  18547. ' end;',
  18548. 'var',
  18549. ' o: TJSObject;',
  18550. ' key: string;',
  18551. 'begin',
  18552. ' for key in o do',
  18553. ' if key=''abc'' then ;',
  18554. '']);
  18555. ConvertProgram;
  18556. CheckSource('TestExternalClass_ForInJSObject',
  18557. LinesToStr([ // statements
  18558. 'this.o = null;',
  18559. 'this.key = "";',
  18560. '']),
  18561. LinesToStr([ // $mod.$main
  18562. 'for ($mod.key in $mod.o) if ($mod.key === "abc") ;',
  18563. '']));
  18564. end;
  18565. procedure TTestModule.TestExternalClass_ForInJSArray;
  18566. begin
  18567. StartProgram(false);
  18568. Add([
  18569. '{$modeswitch externalclass}',
  18570. 'type',
  18571. ' TJSInt8Array = class external name ''Int8Array''',
  18572. ' private',
  18573. ' flength: NativeInt external name ''length'';',
  18574. ' function getValue(Index: NativeInt): shortint; external name ''[]'';',
  18575. ' public',
  18576. ' property values[Index: NativeInt]: Shortint Read getValue; default;',
  18577. ' property Length: NativeInt read flength;',
  18578. ' end;',
  18579. 'var',
  18580. ' a: TJSInt8Array;',
  18581. ' value: shortint;',
  18582. 'begin',
  18583. ' for value in a do',
  18584. ' if value=3 then ;',
  18585. '']);
  18586. ConvertProgram;
  18587. CheckSource('TestExternalClass_ForInJSArray',
  18588. LinesToStr([ // statements
  18589. 'this.a = null;',
  18590. 'this.value = 0;',
  18591. '']),
  18592. LinesToStr([ // $mod.$main
  18593. 'for (var $in = $mod.a, $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) {',
  18594. ' $mod.value = $in[$l];',
  18595. ' if ($mod.value === 3) ;',
  18596. '};',
  18597. '']));
  18598. end;
  18599. procedure TTestModule.TestExternalClass_IncompatibleArgDuplicateIdentifier;
  18600. begin
  18601. AddModuleWithIntfImplSrc('unit2.pas',
  18602. LinesToStr([
  18603. '{$modeswitch externalclass}',
  18604. 'type',
  18605. ' TJSBufferSource = class external name ''BufferSource''',
  18606. ' end;',
  18607. 'procedure DoIt(s: TJSBufferSource); external name ''DoIt'';',
  18608. '']),
  18609. '');
  18610. AddModuleWithIntfImplSrc('unit3.pas',
  18611. LinesToStr([
  18612. '{$modeswitch externalclass}',
  18613. 'type',
  18614. ' TJSBufferSource = class external name ''BufferSource''',
  18615. ' end;',
  18616. '']),
  18617. '');
  18618. StartUnit(true);
  18619. Add([
  18620. 'interface',
  18621. 'uses unit2, unit3;',
  18622. 'procedure DoSome(s: TJSBufferSource);',
  18623. 'implementation',
  18624. 'procedure DoSome(s: TJSBufferSource);',
  18625. 'begin',
  18626. ' DoIt(s);',
  18627. 'end;',
  18628. '']);
  18629. SetExpectedPasResolverError('Incompatible type arg no. 1: Got "unit3.TJSBufferSource", expected "unit2.TJSBufferSource"',
  18630. nIncompatibleTypeArgNo);
  18631. ConvertUnit;
  18632. end;
  18633. procedure TTestModule.TestClassInterface_Corba;
  18634. begin
  18635. StartProgram(false);
  18636. Add([
  18637. '{$interfaces corba}',
  18638. 'type',
  18639. ' IUnknown = interface;',
  18640. ' IUnknown = interface',
  18641. ' [''{00000000-0000-0000-C000-000000000046}'']',
  18642. ' end;',
  18643. ' IInterface = IUnknown;',
  18644. ' IBird = interface(IInterface)',
  18645. ' function GetSize: longint;',
  18646. ' procedure SetSize(i: longint);',
  18647. ' property Size: longint read GetSize write SetSize;',
  18648. ' procedure DoIt(i: longint);',
  18649. ' end;',
  18650. ' TObject = class',
  18651. ' end;',
  18652. ' TBird = class(TObject,IBird)',
  18653. ' function GetSize: longint; virtual; abstract;',
  18654. ' procedure SetSize(i: longint); virtual; abstract;',
  18655. ' procedure DoIt(i: longint); virtual; abstract;',
  18656. ' end;',
  18657. 'var',
  18658. ' BirdIntf: IBird;',
  18659. 'begin',
  18660. ' BirdIntf.Size:=BirdIntf.Size;',
  18661. '']);
  18662. ConvertProgram;
  18663. CheckSource('TestClassInterface_Corba',
  18664. LinesToStr([ // statements
  18665. 'rtl.createInterface(this, "IUnknown", "{00000000-0000-0000-C000-000000000046}", [], null);',
  18666. 'rtl.createInterface(this, "IBird", "{5BD1A53B-69BB-37EE-AF32-BEFB86D85B03}", ["GetSize", "SetSize", "DoIt"], this.IUnknown);',
  18667. 'rtl.createClass(this, "TObject", null, function () {',
  18668. ' this.$init = function () {',
  18669. ' };',
  18670. ' this.$final = function () {',
  18671. ' };',
  18672. '});',
  18673. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18674. ' rtl.addIntf(this, $mod.IBird);',
  18675. '});',
  18676. 'this.BirdIntf = null;',
  18677. '']),
  18678. LinesToStr([ // $mod.$main
  18679. ' $mod.BirdIntf.SetSize($mod.BirdIntf.GetSize());',
  18680. '']));
  18681. end;
  18682. procedure TTestModule.TestClassInterface_ProcExternalFail;
  18683. begin
  18684. StartProgram(false);
  18685. Add([
  18686. '{$interfaces corba}',
  18687. 'type',
  18688. ' IUnknown = interface',
  18689. ' procedure DoIt; external name ''foo'';',
  18690. ' end;',
  18691. 'begin']);
  18692. SetExpectedParserError(
  18693. 'Fields are not allowed in interface at token "Identifier external" in file test1.pp at line 6 column 21',
  18694. nParserNoFieldsAllowed);
  18695. ConvertProgram;
  18696. end;
  18697. procedure TTestModule.TestClassInterface_Overloads;
  18698. begin
  18699. StartProgram(false);
  18700. Add([
  18701. '{$interfaces corba}',
  18702. 'type',
  18703. ' integer = longint;',
  18704. ' IUnknown = interface',
  18705. ' procedure DoIt(i: integer);',
  18706. ' procedure DoIt(s: string);',
  18707. ' end;',
  18708. ' IBird = interface(IUnknown)',
  18709. ' procedure DoIt(b: boolean); overload;',
  18710. ' end;',
  18711. ' TObject = class',
  18712. ' end;',
  18713. ' TBird = class(TObject,IBird)',
  18714. ' procedure DoIt(o: TObject);',
  18715. ' procedure DoIt(s: string);',
  18716. ' procedure DoIt(i: integer);',
  18717. ' procedure DoIt(b: boolean);',
  18718. ' end;',
  18719. 'procedure TBird.DoIt(o: TObject); begin end;',
  18720. 'procedure TBird.DoIt(s: string); begin end;',
  18721. 'procedure TBird.DoIt(i: integer); begin end;',
  18722. 'procedure TBird.DoIt(b: boolean); begin end;',
  18723. 'var',
  18724. ' BirdIntf: IBird;',
  18725. 'begin',
  18726. ' BirdIntf.DoIt(3);',
  18727. ' BirdIntf.DoIt(''abc'');',
  18728. ' BirdIntf.DoIt(true);',
  18729. '']);
  18730. ConvertProgram;
  18731. CheckSource('TestClassInterface_Overloads',
  18732. LinesToStr([ // statements
  18733. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDC4-8A2AE2C59400}", ["DoIt", "DoIt$1"], null);',
  18734. 'rtl.createInterface(this, "IBird", "{8285DD5E-EA3E-396E-AE88-000B86AABF05}", ["DoIt$2"], this.IUnknown);',
  18735. 'rtl.createClass(this, "TObject", null, function () {',
  18736. ' this.$init = function () {',
  18737. ' };',
  18738. ' this.$final = function () {',
  18739. ' };',
  18740. '});',
  18741. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18742. ' this.DoIt = function (o) {',
  18743. ' };',
  18744. ' this.DoIt$1 = function (s) {',
  18745. ' };',
  18746. ' this.DoIt$2 = function (i) {',
  18747. ' };',
  18748. ' this.DoIt$3 = function (b) {',
  18749. ' };',
  18750. ' rtl.addIntf(this, $mod.IBird, {',
  18751. ' DoIt$2: "DoIt$3",',
  18752. ' DoIt: "DoIt$2"',
  18753. ' });',
  18754. '});',
  18755. 'this.BirdIntf = null;',
  18756. '']),
  18757. LinesToStr([ // $mod.$main
  18758. '$mod.BirdIntf.DoIt(3);',
  18759. '$mod.BirdIntf.DoIt$1("abc");',
  18760. '$mod.BirdIntf.DoIt$2(true);',
  18761. '']));
  18762. end;
  18763. procedure TTestModule.TestClassInterface_DuplicateGUIInIntfListFail;
  18764. begin
  18765. StartProgram(false);
  18766. Add([
  18767. '{$interfaces corba}',
  18768. 'type',
  18769. ' IBird = interface',
  18770. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  18771. ' end;',
  18772. ' IDog = interface',
  18773. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  18774. ' end;',
  18775. ' TObject = class(IBird,IDog)',
  18776. ' end;',
  18777. 'begin']);
  18778. SetExpectedPasResolverError('Duplicate GUID {4B3BA825-E0EC-4799-A19C-55F714A07959} in IDog and IBird',
  18779. nDuplicateGUIDXInYZ);
  18780. ConvertProgram;
  18781. end;
  18782. procedure TTestModule.TestClassInterface_DuplicateGUIInAncestorFail;
  18783. begin
  18784. StartProgram(false);
  18785. Add([
  18786. '{$interfaces corba}',
  18787. 'type',
  18788. ' IAnimal = interface',
  18789. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  18790. ' end;',
  18791. ' IBird = interface(IAnimal)',
  18792. ' end;',
  18793. ' IHawk = interface(IBird)',
  18794. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  18795. ' end;',
  18796. 'begin']);
  18797. SetExpectedPasResolverError('Duplicate GUID {4B3BA825-E0EC-4799-A19C-55F714A07959} in IHawk and IAnimal',
  18798. nDuplicateGUIDXInYZ);
  18799. ConvertProgram;
  18800. end;
  18801. procedure TTestModule.TestClassInterface_AncestorImpl;
  18802. begin
  18803. StartProgram(false);
  18804. Add([
  18805. '{$interfaces corba}',
  18806. 'type',
  18807. ' integer = longint;',
  18808. ' IUnknown = interface',
  18809. ' procedure DoIt(i: integer);',
  18810. ' end;',
  18811. ' IBird = interface',
  18812. ' procedure Fly(i: integer);',
  18813. ' end;',
  18814. ' TObject = class(IUnknown)',
  18815. ' procedure DoIt(i: integer);',
  18816. ' end;',
  18817. ' TBird = class(IBird)',
  18818. ' procedure Fly(i: integer);',
  18819. ' end;',
  18820. 'procedure TObject.DoIt(i: integer); begin end;',
  18821. 'procedure TBird.Fly(i: integer); begin end;',
  18822. 'begin',
  18823. '']);
  18824. ConvertProgram;
  18825. CheckSource('TestClassInterface_AncestorIntf',
  18826. LinesToStr([ // statements
  18827. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDC4-8A2800000000}", ["DoIt"], null);',
  18828. 'rtl.createInterface(this, "IBird", "{B92D5841-6264-3AE3-BF20-000000000000}", ["Fly"], null);',
  18829. 'rtl.createClass(this, "TObject", null, function () {',
  18830. ' this.$init = function () {',
  18831. ' };',
  18832. ' this.$final = function () {',
  18833. ' };',
  18834. ' this.DoIt = function (i) {',
  18835. ' };',
  18836. ' rtl.addIntf(this, $mod.IUnknown);',
  18837. '});',
  18838. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18839. ' this.Fly = function (i) {',
  18840. ' };',
  18841. ' rtl.addIntf(this, $mod.IBird);',
  18842. ' rtl.addIntf(this, $mod.IUnknown);',
  18843. '});',
  18844. '']),
  18845. LinesToStr([ // $mod.$main
  18846. '']));
  18847. end;
  18848. procedure TTestModule.TestClassInterface_ImplReintroduce;
  18849. begin
  18850. StartProgram(false);
  18851. Add([
  18852. '{$interfaces corba}',
  18853. 'type',
  18854. ' integer = longint;',
  18855. ' IBird = interface',
  18856. ' procedure DoIt(i: integer);',
  18857. ' end;',
  18858. ' TObject = class',
  18859. ' procedure DoIt(i: integer);',
  18860. ' end;',
  18861. ' TBird = class(IBird)',
  18862. ' procedure DoIt(i: integer); virtual; reintroduce;',
  18863. ' end;',
  18864. 'procedure TObject.DoIt(i: integer); begin end;',
  18865. 'procedure TBird.DoIt(i: integer); begin end;',
  18866. 'begin',
  18867. '']);
  18868. ConvertProgram;
  18869. CheckSource('TestClassInterface_ImplReintroduce',
  18870. LinesToStr([ // statements
  18871. 'rtl.createInterface(this, "IBird", "{B92D5841-6264-3AE2-8594-000000000000}", ["DoIt"], null);',
  18872. 'rtl.createClass(this, "TObject", null, function () {',
  18873. ' this.$init = function () {',
  18874. ' };',
  18875. ' this.$final = function () {',
  18876. ' };',
  18877. ' this.DoIt = function (i) {',
  18878. ' };',
  18879. '});',
  18880. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18881. ' this.DoIt$1 = function (i) {',
  18882. ' };',
  18883. ' rtl.addIntf(this, $mod.IBird, {',
  18884. ' DoIt: "DoIt$1"',
  18885. ' });',
  18886. '});',
  18887. '']),
  18888. LinesToStr([ // $mod.$main
  18889. '']));
  18890. end;
  18891. procedure TTestModule.TestClassInterface_MethodResolution;
  18892. begin
  18893. StartProgram(false);
  18894. Add([
  18895. '{$interfaces corba}',
  18896. 'type',
  18897. ' IUnknown = interface',
  18898. ' procedure Walk(i: longint);',
  18899. ' end;',
  18900. ' IBird = interface(IUnknown)',
  18901. ' procedure Walk(b: boolean); overload;',
  18902. ' procedure Fly(s: string);',
  18903. ' end;',
  18904. ' TObject = class',
  18905. ' end;',
  18906. ' TBird = class(TObject,IBird)',
  18907. ' procedure IBird.Fly = Move;',
  18908. ' procedure IBird.Walk = Hop;',
  18909. ' procedure Hop(i: longint);',
  18910. ' procedure Move(s: string);',
  18911. ' procedure Hop(b: boolean);',
  18912. ' end;',
  18913. 'procedure TBird.Move(s: string); begin end;',
  18914. 'procedure TBird.Hop(i: longint); begin end;',
  18915. 'procedure TBird.Hop(b: boolean); begin end;',
  18916. 'var',
  18917. ' BirdIntf: IBird;',
  18918. 'begin',
  18919. ' BirdIntf.Walk(3);',
  18920. ' BirdIntf.Walk(true);',
  18921. ' BirdIntf.Fly(''abc'');',
  18922. '']);
  18923. ConvertProgram;
  18924. CheckSource('TestClassInterface_MethodResolution',
  18925. LinesToStr([ // statements
  18926. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDD7-23D600000000}", ["Walk"], null);',
  18927. 'rtl.createInterface(this, "IBird", "{CF8A4986-80F6-396E-AE88-000B86AAE208}", ["Walk$1", "Fly"], this.IUnknown);',
  18928. 'rtl.createClass(this, "TObject", null, function () {',
  18929. ' this.$init = function () {',
  18930. ' };',
  18931. ' this.$final = function () {',
  18932. ' };',
  18933. '});',
  18934. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18935. ' this.Hop = function (i) {',
  18936. ' };',
  18937. ' this.Move = function (s) {',
  18938. ' };',
  18939. ' this.Hop$1 = function (b) {',
  18940. ' };',
  18941. ' rtl.addIntf(this, $mod.IBird, {',
  18942. ' Walk$1: "Hop$1",',
  18943. ' Fly: "Move",',
  18944. ' Walk: "Hop"',
  18945. ' });',
  18946. '});',
  18947. 'this.BirdIntf = null;',
  18948. '']),
  18949. LinesToStr([ // $mod.$main
  18950. '$mod.BirdIntf.Walk(3);',
  18951. '$mod.BirdIntf.Walk$1(true);',
  18952. '$mod.BirdIntf.Fly("abc");',
  18953. '']));
  18954. end;
  18955. procedure TTestModule.TestClassInterface_AncestorMoreInterfaces;
  18956. begin
  18957. StartProgram(false);
  18958. Add([
  18959. '{$interfaces com}',
  18960. 'type',
  18961. ' IUnknown = interface',
  18962. ' function _AddRef: longint;',
  18963. ' procedure Walk;',
  18964. ' end;',
  18965. ' IBird = interface end;',
  18966. ' IDog = interface end;',
  18967. ' TObject = class(IBird,IDog)',
  18968. ' function _AddRef: longint; virtual; abstract;',
  18969. ' procedure Walk; virtual; abstract;',
  18970. ' end;',
  18971. ' TBird = class(IUnknown)',
  18972. ' end;',
  18973. 'begin',
  18974. '']);
  18975. ConvertProgram;
  18976. CheckSource('TestClassInterface_COM_AncestorLess',
  18977. LinesToStr([ // statements
  18978. 'rtl.createInterface(this, "IUnknown", "{8F2D5841-758A-322B-BDDF-21CD521DD723}", ["_AddRef", "Walk"], null);',
  18979. 'rtl.createInterface(this, "IBird", "{CCE11D4C-6504-3AEE-AE88-000B86AAE675}", [], this.IUnknown);',
  18980. 'rtl.createInterface(this, "IDog", "{CCE11D4C-6504-3AEE-AE88-000B8E5FC675}", [], this.IUnknown);',
  18981. 'rtl.createClass(this, "TObject", null, function () {',
  18982. ' this.$init = function () {',
  18983. ' };',
  18984. ' this.$final = function () {',
  18985. ' };',
  18986. ' rtl.addIntf(this, $mod.IBird);',
  18987. ' rtl.addIntf(this, $mod.IDog);',
  18988. '});',
  18989. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18990. ' rtl.addIntf(this, $mod.IUnknown);',
  18991. ' rtl.addIntf(this, $mod.IBird);',
  18992. ' rtl.addIntf(this, $mod.IDog);',
  18993. '});',
  18994. '']),
  18995. LinesToStr([ // $mod.$main
  18996. '']));
  18997. end;
  18998. procedure TTestModule.TestClassInterface_MethodOverride;
  18999. begin
  19000. StartProgram(false);
  19001. Add([
  19002. '{$interfaces corba}',
  19003. 'type',
  19004. ' IUnknown = interface',
  19005. ' [''{D6D98E5B-8A10-4FEC-856A-7BFC847FE74B}'']',
  19006. ' procedure Go;',
  19007. ' end;',
  19008. ' TObject = class(IUnknown)',
  19009. ' procedure Go; virtual; abstract;',
  19010. ' end;',
  19011. ' TBird = class',
  19012. ' procedure Go; override;',
  19013. ' end;',
  19014. ' TCat = class(TObject)',
  19015. ' procedure Go; override;',
  19016. ' end;',
  19017. ' TDog = class(TObject, IUnknown)',
  19018. ' procedure Go; override;',
  19019. ' end;',
  19020. 'procedure TBird.Go; begin end;',
  19021. 'procedure TCat.Go; begin end;',
  19022. 'procedure TDog.Go; begin end;',
  19023. 'begin',
  19024. '']);
  19025. ConvertProgram;
  19026. CheckSource('TestClassInterface_MethodOverride',
  19027. LinesToStr([ // statements
  19028. 'rtl.createInterface(this, "IUnknown", "{D6D98E5B-8A10-4FEC-856A-7BFC847FE74B}", ["Go"], null);',
  19029. 'rtl.createClass(this, "TObject", null, function () {',
  19030. ' this.$init = function () {',
  19031. ' };',
  19032. ' this.$final = function () {',
  19033. ' };',
  19034. ' rtl.addIntf(this, $mod.IUnknown);',
  19035. '});',
  19036. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  19037. ' this.Go = function () {',
  19038. ' };',
  19039. ' rtl.addIntf(this, $mod.IUnknown);',
  19040. '});',
  19041. 'rtl.createClass(this, "TCat", this.TObject, function () {',
  19042. ' this.Go = function () {',
  19043. ' };',
  19044. ' rtl.addIntf(this, $mod.IUnknown);',
  19045. '});',
  19046. 'rtl.createClass(this, "TDog", this.TObject, function () {',
  19047. ' this.Go = function () {',
  19048. ' };',
  19049. ' rtl.addIntf(this, $mod.IUnknown);',
  19050. '});',
  19051. '']),
  19052. LinesToStr([ // $mod.$main
  19053. '']));
  19054. end;
  19055. procedure TTestModule.TestClassInterface_Corba_Delegation;
  19056. begin
  19057. StartProgram(false);
  19058. Add([
  19059. '{$interfaces corba}',
  19060. 'type',
  19061. ' IUnknown = interface',
  19062. ' end;',
  19063. ' IBird = interface(IUnknown)',
  19064. ' procedure Fly(s: string);',
  19065. ' end;',
  19066. ' IEagle = interface(IBird)',
  19067. ' end;',
  19068. ' IDove = interface(IBird)',
  19069. ' end;',
  19070. ' ISwallow = interface(IBird)',
  19071. ' end;',
  19072. ' TObject = class',
  19073. ' end;',
  19074. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  19075. ' procedure Fly(s: string); virtual; abstract;',
  19076. ' end;',
  19077. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  19078. ' FBirdIntf: IBird;',
  19079. ' property BirdIntf: IBird read FBirdIntf implements IBird;',
  19080. ' function GetEagleIntf: IEagle; virtual; abstract;',
  19081. ' property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  19082. ' FDoveObj: TBird;',
  19083. ' property DoveObj: TBird read FDoveObj implements IDove;',
  19084. ' function GetSwallowObj: TBird; virtual; abstract;',
  19085. ' property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  19086. ' end;',
  19087. 'begin',
  19088. '']);
  19089. ConvertProgram;
  19090. CheckSource('TestClassInterface_Corba_Delegation',
  19091. LinesToStr([ // statements
  19092. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19093. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  19094. 'rtl.createInterface(this, "IEagle", "{489289DE-FDE2-34A6-8288-39119022B1B4}", [], this.IBird);',
  19095. 'rtl.createInterface(this, "IDove", "{489289DE-FDE2-34A6-8288-39118EF16074}", [], this.IBird);',
  19096. 'rtl.createInterface(this, "ISwallow", "{B89289DE-FDE2-34A6-8288-3911CBDCB359}", [], this.IBird);',
  19097. 'rtl.createClass(this, "TObject", null, function () {',
  19098. ' this.$init = function () {',
  19099. ' };',
  19100. ' this.$final = function () {',
  19101. ' };',
  19102. '});',
  19103. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  19104. ' rtl.addIntf(this, $mod.IBird);',
  19105. ' rtl.addIntf(this, $mod.IEagle);',
  19106. ' rtl.addIntf(this, $mod.IDove);',
  19107. ' rtl.addIntf(this, $mod.ISwallow);',
  19108. '});',
  19109. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  19110. ' this.$init = function () {',
  19111. ' $mod.TObject.$init.call(this);',
  19112. ' this.FBirdIntf = null;',
  19113. ' this.FDoveObj = null;',
  19114. ' };',
  19115. ' this.$final = function () {',
  19116. ' this.FBirdIntf = undefined;',
  19117. ' this.FDoveObj = undefined;',
  19118. ' $mod.TObject.$final.call(this);',
  19119. ' };',
  19120. ' this.$intfmaps = {',
  19121. ' "{478D080B-C0F6-396E-AE88-000B87785B07}": function () {',
  19122. ' return this.FBirdIntf;',
  19123. ' },',
  19124. ' "{489289DE-FDE2-34A6-8288-39119022B1B4}": function () {',
  19125. ' return this.GetEagleIntf();',
  19126. ' },',
  19127. ' "{489289DE-FDE2-34A6-8288-39118EF16074}": function () {',
  19128. ' return rtl.getIntfT(this.FDoveObj, $mod.IDove);',
  19129. ' },',
  19130. ' "{B89289DE-FDE2-34A6-8288-3911CBDCB359}": function () {',
  19131. ' return rtl.getIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  19132. ' }',
  19133. ' };',
  19134. '});',
  19135. '']),
  19136. LinesToStr([ // $mod.$main
  19137. '']));
  19138. end;
  19139. procedure TTestModule.TestClassInterface_Corba_DelegationStatic;
  19140. begin
  19141. StartProgram(false);
  19142. Add([
  19143. '{$interfaces corba}',
  19144. 'type',
  19145. ' IUnknown = interface',
  19146. ' end;',
  19147. ' IBird = interface(IUnknown)',
  19148. ' procedure Fly(s: string);',
  19149. ' end;',
  19150. ' IEagle = interface(IBird)',
  19151. ' end;',
  19152. ' IDove = interface(IBird)',
  19153. ' end;',
  19154. ' ISwallow = interface(IBird)',
  19155. ' end;',
  19156. ' TObject = class',
  19157. ' end;',
  19158. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  19159. ' procedure Fly(s: string); virtual; abstract;',
  19160. ' end;',
  19161. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  19162. ' private',
  19163. ' class var FBirdIntf: IBird;',
  19164. ' class var FDoveObj: TBird;',
  19165. ' class function GetEagleIntf: IEagle; virtual; abstract;',
  19166. ' class function GetSwallowObj: TBird; virtual; abstract;',
  19167. ' protected',
  19168. ' class property BirdIntf: IBird read FBirdIntf implements IBird;',
  19169. ' class property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  19170. ' class property DoveObj: TBird read FDoveObj implements IDove;',
  19171. ' class property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  19172. ' end;',
  19173. 'begin',
  19174. '']);
  19175. ConvertProgram;
  19176. CheckSource('TestClassInterface_Corba_DelegationStatic',
  19177. LinesToStr([ // statements
  19178. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19179. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  19180. 'rtl.createInterface(this, "IEagle", "{489289DE-FDE2-34A6-8288-39119022B1B4}", [], this.IBird);',
  19181. 'rtl.createInterface(this, "IDove", "{489289DE-FDE2-34A6-8288-39118EF16074}", [], this.IBird);',
  19182. 'rtl.createInterface(this, "ISwallow", "{B89289DE-FDE2-34A6-8288-3911CBDCB359}", [], this.IBird);',
  19183. 'rtl.createClass(this, "TObject", null, function () {',
  19184. ' this.$init = function () {',
  19185. ' };',
  19186. ' this.$final = function () {',
  19187. ' };',
  19188. '});',
  19189. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  19190. ' rtl.addIntf(this, $mod.IBird);',
  19191. ' rtl.addIntf(this, $mod.IEagle);',
  19192. ' rtl.addIntf(this, $mod.IDove);',
  19193. ' rtl.addIntf(this, $mod.ISwallow);',
  19194. '});',
  19195. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  19196. ' this.FBirdIntf = null;',
  19197. ' this.FDoveObj = null;',
  19198. ' this.$intfmaps = {',
  19199. ' "{478D080B-C0F6-396E-AE88-000B87785B07}": function () {',
  19200. ' return this.FBirdIntf;',
  19201. ' },',
  19202. ' "{489289DE-FDE2-34A6-8288-39119022B1B4}": function () {',
  19203. ' return this.GetEagleIntf();',
  19204. ' },',
  19205. ' "{489289DE-FDE2-34A6-8288-39118EF16074}": function () {',
  19206. ' return rtl.getIntfT(this.FDoveObj, $mod.IDove);',
  19207. ' },',
  19208. ' "{B89289DE-FDE2-34A6-8288-3911CBDCB359}": function () {',
  19209. ' return rtl.getIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  19210. ' }',
  19211. ' };',
  19212. '});',
  19213. '']),
  19214. LinesToStr([ // $mod.$main
  19215. '']));
  19216. end;
  19217. procedure TTestModule.TestClassInterface_Corba_Operators;
  19218. begin
  19219. StartProgram(false);
  19220. Add([
  19221. '{$interfaces corba}',
  19222. 'type',
  19223. ' IUnknown = interface',
  19224. ' end;',
  19225. ' IBird = interface(IUnknown)',
  19226. ' function GetItems(Index: longint): longint;',
  19227. ' procedure SetItems(Index: longint; Value: longint);',
  19228. ' property Items[Index: longint]: longint read GetItems write SetItems; default;',
  19229. ' end;',
  19230. ' TObject = class',
  19231. ' end;',
  19232. ' TBird = class(TObject,IBird)',
  19233. ' function GetItems(Index: longint): longint; virtual; abstract;',
  19234. ' procedure SetItems(Index: longint; Value: longint); virtual; abstract;',
  19235. ' end;',
  19236. 'var',
  19237. ' IntfVar: IBird = nil;',
  19238. ' IntfVar2: IBird;',
  19239. ' ObjVar: TBird;',
  19240. ' v: JSValue;',
  19241. 'begin',
  19242. ' IntfVar:=nil;',
  19243. ' IntfVar[3]:=IntfVar[4];',
  19244. ' if Assigned(IntfVar) then ;',
  19245. ' IntfVar:=IntfVar2;',
  19246. ' IntfVar:=ObjVar;',
  19247. ' if IntfVar=IntfVar2 then ;',
  19248. ' if IntfVar<>IntfVar2 then ;',
  19249. ' if IntfVar is IBird then ;',
  19250. ' if IntfVar is TBird then ;',
  19251. ' if ObjVar is IBird then ;',
  19252. ' IntfVar:=IntfVar2 as IBird;',
  19253. ' ObjVar:=IntfVar2 as TBird;',
  19254. ' IntfVar:=ObjVar as IBird;',
  19255. ' IntfVar:=IBird(IntfVar2);',
  19256. ' ObjVar:=TBird(IntfVar);',
  19257. ' IntfVar:=IBird(ObjVar);',
  19258. ' v:=IntfVar;',
  19259. ' IntfVar:=IBird(v);',
  19260. ' if v is IBird then ;',
  19261. ' v:=JSValue(IntfVar);',
  19262. ' v:=IBird;',
  19263. '']);
  19264. ConvertProgram;
  19265. CheckSource('TestClassInterface_Corba_Operators',
  19266. LinesToStr([ // statements
  19267. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19268. 'rtl.createInterface(this, "IBird", "{D53FED90-DE59-3202-B1AE-000B87785B08}", ["GetItems", "SetItems"], this.IUnknown);',
  19269. 'rtl.createClass(this, "TObject", null, function () {',
  19270. ' this.$init = function () {',
  19271. ' };',
  19272. ' this.$final = function () {',
  19273. ' };',
  19274. '});',
  19275. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  19276. ' rtl.addIntf(this, $mod.IBird);',
  19277. '});',
  19278. 'this.IntfVar = null;',
  19279. 'this.IntfVar2 = null;',
  19280. 'this.ObjVar = null;',
  19281. 'this.v = undefined;',
  19282. '']),
  19283. LinesToStr([ // $mod.$main
  19284. '$mod.IntfVar = null;',
  19285. '$mod.IntfVar.SetItems(3, $mod.IntfVar.GetItems(4));',
  19286. 'if ($mod.IntfVar != null) ;',
  19287. '$mod.IntfVar = $mod.IntfVar2;',
  19288. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar,$mod.IBird);',
  19289. 'if ($mod.IntfVar === $mod.IntfVar2) ;',
  19290. 'if ($mod.IntfVar !== $mod.IntfVar2) ;',
  19291. 'if ($mod.IBird.isPrototypeOf($mod.IntfVar)) ;',
  19292. 'if (rtl.intfIsClass($mod.IntfVar, $mod.TBird)) ;',
  19293. 'if (rtl.getIntfT($mod.ObjVar, $mod.IBird) !== null) ;',
  19294. '$mod.IntfVar = rtl.as($mod.IntfVar2, $mod.IBird);',
  19295. '$mod.ObjVar = rtl.intfAsClass($mod.IntfVar2, $mod.TBird);',
  19296. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar, $mod.IBird);',
  19297. '$mod.IntfVar = $mod.IntfVar2;',
  19298. '$mod.ObjVar = rtl.intfToClass($mod.IntfVar, $mod.TBird);',
  19299. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar, $mod.IBird);',
  19300. '$mod.v = $mod.IntfVar;',
  19301. '$mod.IntfVar = rtl.getObject($mod.v);',
  19302. 'if (rtl.isExt($mod.v, $mod.IBird, 1)) ;',
  19303. '$mod.v = $mod.IntfVar;',
  19304. '$mod.v = $mod.IBird;',
  19305. '']));
  19306. end;
  19307. procedure TTestModule.TestClassInterface_Corba_Args;
  19308. begin
  19309. StartProgram(false);
  19310. Add([
  19311. '{$interfaces corba}',
  19312. 'type',
  19313. ' IUnknown = interface',
  19314. ' end;',
  19315. ' IBird = interface(IUnknown)',
  19316. ' end;',
  19317. ' TObject = class',
  19318. ' end;',
  19319. ' TBird = class(TObject,IBird)',
  19320. ' end;',
  19321. 'procedure DoIt(var u; i: IBird; const j: IBird);',
  19322. 'begin',
  19323. ' DoIt(i,i,i);',
  19324. 'end;',
  19325. 'procedure Change(var i: IBird; out j: IBird);',
  19326. 'begin',
  19327. ' DoIt(i,i,i);',
  19328. ' Change(i,i);',
  19329. 'end;',
  19330. 'var',
  19331. ' i: IBird;',
  19332. ' o: TBird;',
  19333. 'begin',
  19334. ' DoIt(i,i,i);',
  19335. ' Change(i,i);',
  19336. ' DoIt(o,o,o);',
  19337. '']);
  19338. ConvertProgram;
  19339. CheckSource('TestClassInterface_Corba_Args',
  19340. LinesToStr([ // statements
  19341. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19342. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], this.IUnknown);',
  19343. 'rtl.createClass(this, "TObject", null, function () {',
  19344. ' this.$init = function () {',
  19345. ' };',
  19346. ' this.$final = function () {',
  19347. ' };',
  19348. '});',
  19349. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  19350. ' rtl.addIntf(this, $mod.IBird);',
  19351. '});',
  19352. 'this.DoIt = function (u, i, j) {',
  19353. ' $mod.DoIt({',
  19354. ' get: function () {',
  19355. ' return i;',
  19356. ' },',
  19357. ' set: function (v) {',
  19358. ' i = v;',
  19359. ' }',
  19360. ' }, i, i);',
  19361. '};',
  19362. 'this.Change = function (i, j) {',
  19363. ' $mod.DoIt(i, i.get(), i.get());',
  19364. ' $mod.Change(i, i);',
  19365. '};',
  19366. 'this.i = null;',
  19367. 'this.o = null;',
  19368. '']),
  19369. LinesToStr([ // $mod.$main
  19370. '$mod.DoIt({',
  19371. ' p: $mod,',
  19372. ' get: function () {',
  19373. ' return this.p.i;',
  19374. ' },',
  19375. ' set: function (v) {',
  19376. ' this.p.i = v;',
  19377. ' }',
  19378. '}, $mod.i, $mod.i);',
  19379. '$mod.Change({',
  19380. ' p: $mod,',
  19381. ' get: function () {',
  19382. ' return this.p.i;',
  19383. ' },',
  19384. ' set: function (v) {',
  19385. ' this.p.i = v;',
  19386. ' }',
  19387. '}, {',
  19388. ' p: $mod,',
  19389. ' get: function () {',
  19390. ' return this.p.i;',
  19391. ' },',
  19392. ' set: function (v) {',
  19393. ' this.p.i = v;',
  19394. ' }',
  19395. '});',
  19396. '$mod.DoIt({',
  19397. ' p: $mod,',
  19398. ' get: function () {',
  19399. ' return this.p.o;',
  19400. ' },',
  19401. ' set: function (v) {',
  19402. ' this.p.o = v;',
  19403. ' }',
  19404. '}, rtl.getIntfT($mod.o, $mod.IBird), rtl.getIntfT($mod.o, $mod.IBird));',
  19405. '']));
  19406. end;
  19407. procedure TTestModule.TestClassInterface_Corba_ForIn;
  19408. begin
  19409. StartProgram(false);
  19410. Add([
  19411. '{$interfaces corba}',
  19412. 'type',
  19413. ' IUnknown = interface end;',
  19414. ' TObject = class',
  19415. ' Id: longint;',
  19416. ' end;',
  19417. ' IEnumerator = interface(IUnknown)',
  19418. ' function GetCurrent: TObject;',
  19419. ' function MoveNext: Boolean;',
  19420. ' property Current: TObject read GetCurrent;',
  19421. ' end;',
  19422. ' IEnumerable = interface(IUnknown)',
  19423. ' function GetEnumerator: IEnumerator;',
  19424. ' end;',
  19425. 'var',
  19426. ' o: TObject;',
  19427. ' i: IEnumerable;',
  19428. 'begin',
  19429. ' for o in i do o.Id:=3;',
  19430. '']);
  19431. ConvertProgram;
  19432. CheckSource('TestClassInterface_Corba_ForIn',
  19433. LinesToStr([ // statements
  19434. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19435. 'rtl.createClass(this, "TObject", null, function () {',
  19436. ' this.$init = function () {',
  19437. ' this.Id = 0;',
  19438. ' };',
  19439. ' this.$final = function () {',
  19440. ' };',
  19441. '});',
  19442. 'rtl.createInterface(this, "IEnumerator", "{95D7745D-ED61-3F13-BBE4-07708161999E}", ["GetCurrent", "MoveNext"], this.IUnknown);',
  19443. 'rtl.createInterface(this, "IEnumerable", "{8CC9D45D-ED7D-3B73-96B6-290B931BB19E}", ["GetEnumerator"], this.IUnknown);',
  19444. 'this.o = null;',
  19445. 'this.i = null;',
  19446. '']),
  19447. LinesToStr([ // $mod.$main
  19448. 'var $in = $mod.i.GetEnumerator();',
  19449. 'while ($in.MoveNext()) {',
  19450. ' $mod.o = $in.GetCurrent();',
  19451. ' $mod.o.Id = 3;',
  19452. '};',
  19453. '']));
  19454. end;
  19455. procedure TTestModule.TestClassInterface_COM_AssignVar;
  19456. begin
  19457. StartProgram(false);
  19458. Add([
  19459. '{$interfaces com}',
  19460. 'type',
  19461. ' IUnknown = interface',
  19462. ' function _AddRef: longint;',
  19463. ' function _Release: longint;',
  19464. ' end;',
  19465. ' TObject = class(IUnknown)',
  19466. ' function _AddRef: longint; virtual; abstract;',
  19467. ' function _Release: longint; virtual; abstract;',
  19468. ' end;',
  19469. 'var',
  19470. ' i: IUnknown;',
  19471. 'procedure DoGlobal(o: TObject);',
  19472. 'begin',
  19473. ' i:=nil;',
  19474. ' i:=o;',
  19475. ' i:=i;',
  19476. 'end;',
  19477. 'procedure DoLocal(o: TObject);',
  19478. 'const k: IUnknown = nil;',
  19479. 'var j: IUnknown;',
  19480. 'begin',
  19481. ' k:=o;',
  19482. ' k:=i;',
  19483. ' j:=o;',
  19484. ' j:=i;',
  19485. 'end;',
  19486. 'var o: TObject;',
  19487. 'begin',
  19488. ' i:=nil;',
  19489. ' i:=o;',
  19490. '']);
  19491. ConvertProgram;
  19492. CheckSource('TestClassInterface_COM_AssignVar',
  19493. LinesToStr([ // statements
  19494. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19495. 'rtl.createClass(this, "TObject", null, function () {',
  19496. ' this.$init = function () {',
  19497. ' };',
  19498. ' this.$final = function () {',
  19499. ' };',
  19500. ' rtl.addIntf(this, $mod.IUnknown);',
  19501. '});',
  19502. 'this.i = null;',
  19503. 'this.DoGlobal = function (o) {',
  19504. ' rtl.setIntfP($mod, "i", null);',
  19505. ' rtl.setIntfP($mod, "i", rtl.queryIntfT(o, $mod.IUnknown), true);',
  19506. ' rtl.setIntfP($mod, "i", $mod.i);',
  19507. '};',
  19508. 'var k = null;',
  19509. 'this.DoLocal = function (o) {',
  19510. ' var j = null;',
  19511. ' try{',
  19512. ' k = rtl.setIntfL(k, rtl.queryIntfT(o, $mod.IUnknown), true);',
  19513. ' k = rtl.setIntfL(k, $mod.i);',
  19514. ' j = rtl.setIntfL(j, rtl.queryIntfT(o, $mod.IUnknown), true);',
  19515. ' j = rtl.setIntfL(j, $mod.i);',
  19516. ' }finally{',
  19517. ' rtl._Release(j);',
  19518. ' };',
  19519. '};',
  19520. 'this.o = null;',
  19521. '']),
  19522. LinesToStr([ // $mod.$main
  19523. 'rtl.setIntfP($mod, "i", null);',
  19524. 'rtl.setIntfP($mod, "i", rtl.queryIntfT($mod.o, $mod.IUnknown), true);',
  19525. '']));
  19526. end;
  19527. procedure TTestModule.TestClassInterface_COM_AssignArg;
  19528. begin
  19529. StartProgram(false);
  19530. Add([
  19531. '{$interfaces com}',
  19532. 'type',
  19533. ' IUnknown = interface',
  19534. ' function _AddRef: longint;',
  19535. ' function _Release: longint;',
  19536. ' end;',
  19537. ' TObject = class(IUnknown)',
  19538. ' function _AddRef: longint; virtual; abstract;',
  19539. ' function _Release: longint; virtual; abstract;',
  19540. ' end;',
  19541. 'procedure DoDefault(i, j: IUnknown);',
  19542. 'begin',
  19543. ' i:=nil;',
  19544. ' i:=j;',
  19545. 'end;',
  19546. 'begin',
  19547. '']);
  19548. ConvertProgram;
  19549. CheckSource('TestClassInterface_COM_AssignArg',
  19550. LinesToStr([ // statements
  19551. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19552. 'rtl.createClass(this, "TObject", null, function () {',
  19553. ' this.$init = function () {',
  19554. ' };',
  19555. ' this.$final = function () {',
  19556. ' };',
  19557. ' rtl.addIntf(this, $mod.IUnknown);',
  19558. '});',
  19559. 'this.DoDefault = function (i, j) {',
  19560. ' rtl._AddRef(i);',
  19561. ' try {',
  19562. ' i = rtl.setIntfL(i, null);',
  19563. ' i = rtl.setIntfL(i, j);',
  19564. ' } finally {',
  19565. ' rtl._Release(i);',
  19566. ' };',
  19567. '};',
  19568. '']),
  19569. LinesToStr([ // $mod.$main
  19570. '']));
  19571. end;
  19572. procedure TTestModule.TestClassInterface_COM_FunctionResult;
  19573. begin
  19574. StartProgram(false);
  19575. Add([
  19576. '{$interfaces com}',
  19577. 'type',
  19578. ' IUnknown = interface',
  19579. ' function _AddRef: longint;',
  19580. ' function _Release: longint;',
  19581. ' end;',
  19582. ' TObject = class(IUnknown)',
  19583. ' function _AddRef: longint; virtual; abstract;',
  19584. ' function _Release: longint; virtual; abstract;',
  19585. ' end;',
  19586. 'function DoDefault(i: IUnknown): IUnknown;',
  19587. 'begin',
  19588. ' Result:=i;',
  19589. ' if Result<>nil then exit;',
  19590. 'end;',
  19591. 'begin',
  19592. '']);
  19593. ConvertProgram;
  19594. CheckSource('TestClassInterface_COM_FunctionResult',
  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. ' rtl.addIntf(this, $mod.IUnknown);',
  19603. '});',
  19604. 'this.DoDefault = function (i) {',
  19605. ' var Result = null;',
  19606. ' var $ok = false;',
  19607. ' try {',
  19608. ' Result = rtl.setIntfL(Result, i);',
  19609. ' if(Result !== null){',
  19610. ' $ok = true;',
  19611. ' return Result;',
  19612. ' };',
  19613. ' $ok = true;',
  19614. ' } finally {',
  19615. ' if(!$ok) rtl._Release(Result);',
  19616. ' };',
  19617. ' return Result;',
  19618. '};',
  19619. '']),
  19620. LinesToStr([ // $mod.$main
  19621. '']));
  19622. end;
  19623. procedure TTestModule.TestClassInterface_COM_InheritedFuncResult;
  19624. begin
  19625. StartProgram(false);
  19626. Add([
  19627. '{$interfaces com}',
  19628. 'type',
  19629. ' IUnknown = interface',
  19630. ' function _AddRef: longint;',
  19631. ' function _Release: longint;',
  19632. ' end;',
  19633. ' TObject = class(IUnknown)',
  19634. ' function _AddRef: longint; virtual; abstract;',
  19635. ' function _Release: longint; virtual; abstract;',
  19636. ' function GetIntf: IUnknown; virtual;',
  19637. ' end;',
  19638. ' TMouse = class',
  19639. ' function GetIntf: IUnknown; override;',
  19640. ' end;',
  19641. 'function TObject.GetIntf: IUnknown; begin end;',
  19642. 'function TMouse.GetIntf: IUnknown;',
  19643. 'var i: IUnknown;',
  19644. 'begin',
  19645. ' inherited;',
  19646. ' inherited GetIntf;',
  19647. ' inherited GetIntf();',
  19648. ' Result:=inherited GetIntf;',
  19649. ' Result:=inherited GetIntf();',
  19650. ' i:=inherited GetIntf;',
  19651. ' i:=inherited GetIntf();',
  19652. 'end;',
  19653. 'begin',
  19654. '']);
  19655. ConvertProgram;
  19656. CheckSource('TestClassInterface_COM_InheritedFuncResult',
  19657. LinesToStr([ // statements
  19658. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19659. 'rtl.createClass(this, "TObject", null, function () {',
  19660. ' this.$init = function () {',
  19661. ' };',
  19662. ' this.$final = function () {',
  19663. ' };',
  19664. ' this.GetIntf = function () {',
  19665. ' var Result = null;',
  19666. ' return Result;',
  19667. ' };',
  19668. ' rtl.addIntf(this, $mod.IUnknown);',
  19669. '});',
  19670. 'rtl.createClass(this, "TMouse", this.TObject, function () {',
  19671. ' this.GetIntf = function () {',
  19672. ' var Result = null;',
  19673. ' var i = null;',
  19674. ' var $ir = rtl.createIntfRefs();',
  19675. ' var $ok = false;',
  19676. ' try {',
  19677. ' $ir.ref(1, $mod.TObject.GetIntf.call(this));',
  19678. ' $ir.ref(2, $mod.TObject.GetIntf.call(this));',
  19679. ' $ir.ref(3, $mod.TObject.GetIntf.call(this));',
  19680. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  19681. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  19682. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  19683. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  19684. ' $ok = true;',
  19685. ' } finally {',
  19686. ' $ir.free();',
  19687. ' rtl._Release(i);',
  19688. ' if (!$ok) rtl._Release(Result);',
  19689. ' };',
  19690. ' return Result;',
  19691. ' };',
  19692. ' rtl.addIntf(this, $mod.IUnknown);',
  19693. '});',
  19694. '']),
  19695. LinesToStr([ // $mod.$main
  19696. '']));
  19697. end;
  19698. procedure TTestModule.TestClassInterface_COM_IsAsTypeCasts;
  19699. begin
  19700. StartProgram(false);
  19701. Add([
  19702. '{$interfaces com}',
  19703. 'type',
  19704. ' IUnknown = interface',
  19705. ' function _AddRef: longint;',
  19706. ' function _Release: longint;',
  19707. ' end;',
  19708. ' TObject = class(IUnknown)',
  19709. ' function _AddRef: longint; virtual; abstract;',
  19710. ' function _Release: longint; virtual; abstract;',
  19711. ' end;',
  19712. 'procedure DoDefault(i, j: IUnknown; o: TObject);',
  19713. 'begin',
  19714. ' if i is IUnknown then ;',
  19715. ' if o is IUnknown then ;',
  19716. ' if i is TObject then ;',
  19717. ' i:=j as IUnknown;',
  19718. ' i:=o as IUnknown;',
  19719. ' o:=j as TObject;',
  19720. ' i:=IUnknown(j);',
  19721. ' i:=IUnknown(o);',
  19722. ' o:=TObject(i);',
  19723. 'end;',
  19724. 'begin',
  19725. '']);
  19726. ConvertProgram;
  19727. CheckSource('TestClassInterface_COM_IsAsTypeCasts',
  19728. LinesToStr([ // statements
  19729. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19730. 'rtl.createClass(this, "TObject", null, function () {',
  19731. ' this.$init = function () {',
  19732. ' };',
  19733. ' this.$final = function () {',
  19734. ' };',
  19735. ' rtl.addIntf(this, $mod.IUnknown);',
  19736. '});',
  19737. 'this.DoDefault = function (i, j, o) {',
  19738. ' rtl._AddRef(i);',
  19739. ' try {',
  19740. ' if (rtl.intfIsIntfT(i, $mod.IUnknown)) ;',
  19741. ' if (rtl.queryIntfIsT(o, $mod.IUnknown)) ;',
  19742. ' if (rtl.intfIsClass(i, $mod.TObject)) ;',
  19743. ' i = rtl.setIntfL(i, rtl.intfAsIntfT(j, $mod.IUnknown));',
  19744. ' i = rtl.setIntfL(i, rtl.queryIntfT(o, $mod.IUnknown), true);',
  19745. ' o = rtl.intfAsClass(j, $mod.TObject);',
  19746. ' i = rtl.setIntfL(i, j);',
  19747. ' i = rtl.setIntfL(i, rtl.queryIntfT(o, $mod.IUnknown), true);',
  19748. ' o = rtl.intfToClass(i, $mod.TObject);',
  19749. ' } finally {',
  19750. ' rtl._Release(i);',
  19751. ' };',
  19752. '};',
  19753. '']),
  19754. LinesToStr([ // $mod.$main
  19755. '']));
  19756. end;
  19757. procedure TTestModule.TestClassInterface_COM_PassAsArg;
  19758. begin
  19759. StartProgram(false);
  19760. Add([
  19761. '{$interfaces com}',
  19762. 'type',
  19763. ' IUnknown = interface',
  19764. ' function _AddRef: longint;',
  19765. ' function _Release: longint;',
  19766. ' end;',
  19767. ' TObject = class(IUnknown)',
  19768. ' function _AddRef: longint; virtual; abstract;',
  19769. ' function _Release: longint; virtual; abstract;',
  19770. ' end;',
  19771. 'procedure DoIt(v: IUnknown; const j: IUnknown; var k: IUnknown; out l: IUnknown);',
  19772. 'var o: TObject;',
  19773. 'begin',
  19774. ' DoIt(v,v,v,v);',
  19775. ' DoIt(o,o,k,k);',
  19776. 'end;',
  19777. 'procedure DoSome;',
  19778. 'var v: IUnknown;',
  19779. 'begin',
  19780. ' DoIt(v,v,v,v);',
  19781. 'end;',
  19782. 'var i: IUnknown;',
  19783. 'begin',
  19784. ' DoIt(i,i,i,i);',
  19785. '']);
  19786. ConvertProgram;
  19787. CheckSource('TestClassInterface_COM_PassAsArg',
  19788. LinesToStr([ // statements
  19789. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19790. 'rtl.createClass(this, "TObject", null, function () {',
  19791. ' this.$init = function () {',
  19792. ' };',
  19793. ' this.$final = function () {',
  19794. ' };',
  19795. ' rtl.addIntf(this, $mod.IUnknown);',
  19796. '});',
  19797. 'this.DoIt = function (v, j, k, l) {',
  19798. ' var o = null;',
  19799. ' var $ir = rtl.createIntfRefs();',
  19800. ' rtl._AddRef(v);',
  19801. ' try {',
  19802. ' $mod.DoIt(v, v, {',
  19803. ' get: function () {',
  19804. ' return v;',
  19805. ' },',
  19806. ' set: function (w) {',
  19807. ' v = rtl.setIntfL(v, w);',
  19808. ' }',
  19809. ' }, {',
  19810. ' get: function () {',
  19811. ' return v;',
  19812. ' },',
  19813. ' set: function (w) {',
  19814. ' v = rtl.setIntfL(v, w);',
  19815. ' }',
  19816. ' });',
  19817. ' $mod.DoIt($ir.ref(1, rtl.queryIntfT(o, $mod.IUnknown)), $ir.ref(2, rtl.queryIntfT(o, $mod.IUnknown)), k, k);',
  19818. ' } finally {',
  19819. ' $ir.free();',
  19820. ' rtl._Release(v);',
  19821. ' };',
  19822. '};',
  19823. 'this.DoSome = function () {',
  19824. ' var v = null;',
  19825. ' try {',
  19826. ' $mod.DoIt(v, v, {',
  19827. ' get: function () {',
  19828. ' return v;',
  19829. ' },',
  19830. ' set: function (w) {',
  19831. ' v = rtl.setIntfL(v, w);',
  19832. ' }',
  19833. ' }, {',
  19834. ' get: function () {',
  19835. ' return v;',
  19836. ' },',
  19837. ' set: function (w) {',
  19838. ' v = rtl.setIntfL(v, w);',
  19839. ' }',
  19840. ' });',
  19841. ' } finally {',
  19842. ' rtl._Release(v);',
  19843. ' };',
  19844. '};',
  19845. 'this.i = null;',
  19846. '']),
  19847. LinesToStr([ // $mod.$main
  19848. '$mod.DoIt($mod.i, $mod.i, {',
  19849. ' p: $mod,',
  19850. ' get: function () {',
  19851. ' return this.p.i;',
  19852. ' },',
  19853. ' set: function (v) {',
  19854. ' rtl.setIntfP(this.p, "i", v);',
  19855. ' }',
  19856. '}, {',
  19857. ' p: $mod,',
  19858. ' get: function () {',
  19859. ' return this.p.i;',
  19860. ' },',
  19861. ' set: function (v) {',
  19862. ' rtl.setIntfP(this.p, "i", v);',
  19863. ' }',
  19864. '});',
  19865. '']));
  19866. end;
  19867. procedure TTestModule.TestClassInterface_COM_PassToUntypedParam;
  19868. begin
  19869. StartProgram(false);
  19870. Add([
  19871. '{$interfaces com}',
  19872. 'type',
  19873. ' IUnknown = interface',
  19874. ' function _AddRef: longint;',
  19875. ' function _Release: longint;',
  19876. ' end;',
  19877. ' TObject = class(IUnknown)',
  19878. ' function _AddRef: longint; virtual; abstract;',
  19879. ' function _Release: longint; virtual; abstract;',
  19880. ' end;',
  19881. 'procedure DoIt(out i);',
  19882. 'begin end;',
  19883. 'procedure DoSome;',
  19884. 'var v: IUnknown;',
  19885. 'begin',
  19886. ' DoIt(v);',
  19887. 'end;',
  19888. 'function GetIt: IUnknown;',
  19889. 'begin',
  19890. ' DoIt(Result);',
  19891. 'end;',
  19892. 'var i: IUnknown;',
  19893. 'begin',
  19894. ' DoIt(i);',
  19895. '']);
  19896. ConvertProgram;
  19897. CheckSource('TestClassInterface_COM_PassToUntypedParam',
  19898. LinesToStr([ // statements
  19899. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19900. 'rtl.createClass(this, "TObject", null, function () {',
  19901. ' this.$init = function () {',
  19902. ' };',
  19903. ' this.$final = function () {',
  19904. ' };',
  19905. ' rtl.addIntf(this, $mod.IUnknown);',
  19906. '});',
  19907. 'this.DoIt = function (i) {',
  19908. '};',
  19909. 'this.DoSome = function () {',
  19910. ' var v = null;',
  19911. ' try {',
  19912. ' $mod.DoIt({',
  19913. ' get: function () {',
  19914. ' return v;',
  19915. ' },',
  19916. ' set: function (w) {',
  19917. ' v = w;',
  19918. ' }',
  19919. ' });',
  19920. ' } finally {',
  19921. ' rtl._Release(v);',
  19922. ' };',
  19923. '};',
  19924. 'this.GetIt = function () {',
  19925. ' var Result = null;',
  19926. ' var $ok = false;',
  19927. ' try {',
  19928. ' $mod.DoIt({',
  19929. ' get: function () {',
  19930. ' return Result;',
  19931. ' },',
  19932. ' set: function (v) {',
  19933. ' Result = v;',
  19934. ' }',
  19935. ' });',
  19936. ' $ok = true;',
  19937. ' } finally {',
  19938. ' if (!$ok) rtl._Release(Result);',
  19939. ' };',
  19940. ' return Result;',
  19941. '};',
  19942. 'this.i = null;',
  19943. '']),
  19944. LinesToStr([ // $mod.$main
  19945. 'try {',
  19946. ' $mod.DoIt({',
  19947. ' p: $mod,',
  19948. ' get: function () {',
  19949. ' return this.p.i;',
  19950. ' },',
  19951. ' set: function (v) {',
  19952. ' this.p.i = v;',
  19953. ' }',
  19954. ' });',
  19955. '} finally {',
  19956. ' rtl._Release($mod.i);',
  19957. '};',
  19958. '']));
  19959. end;
  19960. procedure TTestModule.TestClassInterface_COM_FunctionInExpr;
  19961. begin
  19962. StartProgram(false);
  19963. Add([
  19964. '{$interfaces com}',
  19965. 'type',
  19966. ' IUnknown = interface',
  19967. ' function _AddRef: longint;',
  19968. ' function _Release: longint;',
  19969. ' end;',
  19970. ' TObject = class(IUnknown)',
  19971. ' function _AddRef: longint; virtual; abstract;',
  19972. ' function _Release: longint; virtual; abstract;',
  19973. ' end;',
  19974. 'function GetIt: IUnknown;',
  19975. 'begin',
  19976. 'end;',
  19977. 'procedure DoSome;',
  19978. 'var v: IUnknown;',
  19979. ' i: longint;',
  19980. 'begin',
  19981. ' v:=GetIt;',
  19982. ' v:=GetIt();',
  19983. ' GetIt()._AddRef;',
  19984. ' i:=GetIt()._AddRef;',
  19985. 'end;',
  19986. 'var v: IUnknown;',
  19987. ' i: longint;',
  19988. 'begin',
  19989. ' v:=GetIt;',
  19990. ' v:=GetIt();',
  19991. ' GetIt()._AddRef;',
  19992. ' i:=GetIt()._AddRef;',
  19993. '']);
  19994. ConvertProgram;
  19995. CheckSource('TestClassInterface_COM_FunctionInExpr',
  19996. LinesToStr([ // statements
  19997. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19998. 'rtl.createClass(this, "TObject", null, function () {',
  19999. ' this.$init = function () {',
  20000. ' };',
  20001. ' this.$final = function () {',
  20002. ' };',
  20003. ' rtl.addIntf(this, $mod.IUnknown);',
  20004. '});',
  20005. 'this.GetIt = function () {',
  20006. ' var Result = null;',
  20007. ' return Result;',
  20008. '};',
  20009. 'this.DoSome = function () {',
  20010. ' var v = null;',
  20011. ' var i = 0;',
  20012. ' var $ir = rtl.createIntfRefs();',
  20013. ' try {',
  20014. ' v = rtl.setIntfL(v, $mod.GetIt(), true);',
  20015. ' v = rtl.setIntfL(v, $mod.GetIt(), true);',
  20016. ' $ir.ref(1, $mod.GetIt())._AddRef();',
  20017. ' i = $ir.ref(2, $mod.GetIt())._AddRef();',
  20018. ' } finally {',
  20019. ' $ir.free();',
  20020. ' rtl._Release(v);',
  20021. ' };',
  20022. '};',
  20023. 'this.v = null;',
  20024. 'this.i = 0;',
  20025. '']),
  20026. LinesToStr([ // $mod.$main
  20027. 'var $ir = rtl.createIntfRefs();',
  20028. 'try {',
  20029. ' rtl.setIntfP($mod, "v", $mod.GetIt(), true);',
  20030. ' rtl.setIntfP($mod, "v", $mod.GetIt(), true);',
  20031. ' $ir.ref(1, $mod.GetIt())._AddRef();',
  20032. ' $mod.i = $ir.ref(2, $mod.GetIt())._AddRef();',
  20033. '} finally {',
  20034. ' $ir.free();',
  20035. '};',
  20036. '']));
  20037. end;
  20038. procedure TTestModule.TestClassInterface_COM_Property;
  20039. begin
  20040. StartProgram(false);
  20041. Add([
  20042. '{$interfaces com}',
  20043. 'type',
  20044. ' IUnknown = interface',
  20045. ' function _AddRef: longint;',
  20046. ' function _Release: longint;',
  20047. ' end;',
  20048. ' TObject = class(IUnknown)',
  20049. ' FAnt: IUnknown;',
  20050. ' function _AddRef: longint; virtual; abstract;',
  20051. ' function _Release: longint; virtual; abstract;',
  20052. ' function GetBird: IUnknown; virtual; abstract;',
  20053. ' procedure SetBird(Value: IUnknown); virtual; abstract;',
  20054. ' function GetItems(Index: longint): IUnknown; virtual; abstract;',
  20055. ' procedure SetItems(Index: longint; Value: IUnknown); virtual; abstract;',
  20056. ' property Ant: IUnknown read FAnt write FAnt;',
  20057. ' property Bird: IUnknown read GetBird write SetBird;',
  20058. ' property Items[Index: longint]: IUnknown read GetItems write SetItems; default;',
  20059. ' end;',
  20060. 'procedure DoIt;',
  20061. 'var',
  20062. ' o: TObject;',
  20063. ' v: IUnknown;',
  20064. 'begin',
  20065. ' v:=o.Ant;',
  20066. ' o.Ant:=v;',
  20067. ' o.Ant:=o.Ant;',
  20068. ' v:=o.Bird;',
  20069. ' o.Bird:=v;',
  20070. ' o.Bird:=o.Bird;',
  20071. ' v:=o.Items[1];',
  20072. ' o.Items[2]:=v;',
  20073. ' o.Items[3]:=o.Items[4];',
  20074. ' v:=o[5];',
  20075. ' o[6]:=v;',
  20076. ' o[7]:=o[8];',
  20077. 'end;',
  20078. 'begin',
  20079. '']);
  20080. ConvertProgram;
  20081. CheckSource('TestClassInterface_COM_Property',
  20082. LinesToStr([ // statements
  20083. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  20084. 'rtl.createClass(this, "TObject", null, function () {',
  20085. ' this.$init = function () {',
  20086. ' this.FAnt = null;',
  20087. ' };',
  20088. ' this.$final = function () {',
  20089. ' this.FAnt = undefined;',
  20090. ' };',
  20091. ' rtl.addIntf(this, $mod.IUnknown);',
  20092. '});',
  20093. 'this.DoIt = function () {',
  20094. ' var o = null;',
  20095. ' var v = null;',
  20096. ' var $ir = rtl.createIntfRefs();',
  20097. ' try {',
  20098. ' v = rtl.setIntfL(v, o.FAnt);',
  20099. ' rtl.setIntfP(o, "FAnt", v);',
  20100. ' rtl.setIntfP(o, "FAnt", o.FAnt);',
  20101. ' v = rtl.setIntfL(v, o.GetBird(), true);',
  20102. ' o.SetBird(v);',
  20103. ' o.SetBird($ir.ref(1, o.GetBird()));',
  20104. ' v = rtl.setIntfL(v, o.GetItems(1), true);',
  20105. ' o.SetItems(2, v);',
  20106. ' o.SetItems(3, $ir.ref(2, o.GetItems(4)));',
  20107. ' v = rtl.setIntfL(v, o.GetItems(5), true);',
  20108. ' o.SetItems(6, v);',
  20109. ' o.SetItems(7, $ir.ref(3, o.GetItems(8)));',
  20110. ' } finally {',
  20111. ' $ir.free();',
  20112. ' rtl._Release(v);',
  20113. ' };',
  20114. '};',
  20115. '']),
  20116. LinesToStr([ // $mod.$main
  20117. '']));
  20118. end;
  20119. procedure TTestModule.TestClassInterface_COM_IntfProperty;
  20120. begin
  20121. StartProgram(false);
  20122. Add([
  20123. '{$interfaces com}',
  20124. 'type',
  20125. ' IUnknown = interface',
  20126. ' function _AddRef: longint;',
  20127. ' function _Release: longint;',
  20128. ' function GetBird: IUnknown;',
  20129. ' procedure SetBird(Value: IUnknown);',
  20130. ' function GetItems(Index: longint): IUnknown;',
  20131. ' procedure SetItems(Index: longint; Value: IUnknown);',
  20132. ' property Bird: IUnknown read GetBird write SetBird;',
  20133. ' property Items[Index: longint]: IUnknown read GetItems write SetItems; default;',
  20134. ' end;',
  20135. ' TObject = class(IUnknown)',
  20136. ' function _AddRef: longint; virtual; abstract;',
  20137. ' function _Release: longint; virtual; abstract;',
  20138. ' function GetBird: IUnknown; virtual; abstract;',
  20139. ' procedure SetBird(Value: IUnknown); virtual; abstract;',
  20140. ' function GetItems(Index: longint): IUnknown; virtual; abstract;',
  20141. ' procedure SetItems(Index: longint; Value: IUnknown); virtual; abstract;',
  20142. ' end;',
  20143. 'procedure DoIt;',
  20144. 'var',
  20145. ' o: TObject;',
  20146. ' v: IUnknown;',
  20147. 'begin',
  20148. ' v:=v.Items[1];',
  20149. ' v.Items[2]:=v;',
  20150. ' v.Items[3]:=v.Items[4];',
  20151. ' v:=v[5];',
  20152. ' v[6]:=v;',
  20153. ' v[7]:=v[8];',
  20154. ' v[9].Bird.Bird:=v;',
  20155. ' v:=v.Bird[10].Bird',
  20156. 'end;',
  20157. 'begin',
  20158. '']);
  20159. ConvertProgram;
  20160. CheckSource('TestClassInterface_COM_IntfProperty',
  20161. LinesToStr([ // statements
  20162. 'rtl.createInterface(this, "IUnknown", "{385F5482-571B-338C-8130-4E97F330543B}", [',
  20163. ' "_AddRef",',
  20164. ' "_Release",',
  20165. ' "GetBird",',
  20166. ' "SetBird",',
  20167. ' "GetItems",',
  20168. ' "SetItems"',
  20169. '], null);',
  20170. 'rtl.createClass(this, "TObject", null, function () {',
  20171. ' this.$init = function () {',
  20172. ' };',
  20173. ' this.$final = function () {',
  20174. ' };',
  20175. ' rtl.addIntf(this, $mod.IUnknown);',
  20176. '});',
  20177. 'this.DoIt = function () {',
  20178. ' var o = null;',
  20179. ' var v = null;',
  20180. ' var $ir = rtl.createIntfRefs();',
  20181. ' try {',
  20182. ' v = rtl.setIntfL(v, v.GetItems(1), true);',
  20183. ' v.SetItems(2, v);',
  20184. ' v.SetItems(3, $ir.ref(1, v.GetItems(4)));',
  20185. ' v = rtl.setIntfL(v, v.GetItems(5), true);',
  20186. ' v.SetItems(6, v);',
  20187. ' v.SetItems(7, $ir.ref(2, v.GetItems(8)));',
  20188. ' $ir.ref(4, $ir.ref(3, v.GetItems(9)).GetBird()).SetBird(v);',
  20189. ' v = rtl.setIntfL(v, $ir.ref(6, $ir.ref(5, v.GetBird()).GetItems(10)).GetBird(), true);',
  20190. ' } finally {',
  20191. ' $ir.free();',
  20192. ' rtl._Release(v);',
  20193. ' };',
  20194. '};',
  20195. '']),
  20196. LinesToStr([ // $mod.$main
  20197. '']));
  20198. end;
  20199. procedure TTestModule.TestClassInterface_COM_Delegation;
  20200. begin
  20201. StartProgram(false);
  20202. Add([
  20203. '{$interfaces com}',
  20204. 'type',
  20205. ' IUnknown = interface',
  20206. ' function _AddRef: longint;',
  20207. ' function _Release: longint;',
  20208. ' end;',
  20209. ' IBird = interface(IUnknown)',
  20210. ' procedure Fly(s: string);',
  20211. ' end;',
  20212. ' IEagle = interface(IBird) end;',
  20213. ' IDove = interface(IBird) end;',
  20214. ' ISwallow = interface(IBird) end;',
  20215. ' TObject = class',
  20216. ' end;',
  20217. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  20218. ' function _AddRef: longint; virtual; abstract;',
  20219. ' function _Release: longint; virtual; abstract;',
  20220. ' procedure Fly(s: string); virtual; abstract;',
  20221. ' end;',
  20222. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  20223. ' function _AddRef: longint; virtual; abstract;',
  20224. ' function _Release: longint; virtual; abstract;',
  20225. ' FBirdIntf: IBird;',
  20226. ' property BirdIntf: IBird read FBirdIntf implements IBird;',
  20227. ' function GetEagleIntf: IEagle; virtual; abstract;',
  20228. ' property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  20229. ' FDoveObj: TBird;',
  20230. ' property DoveObj: TBird read FDoveObj implements IDove;',
  20231. ' function GetSwallowObj: TBird; virtual; abstract;',
  20232. ' property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  20233. ' end;',
  20234. 'begin',
  20235. '']);
  20236. ConvertProgram;
  20237. CheckSource('TestClassInterface_COM_Delegation',
  20238. LinesToStr([ // statements
  20239. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  20240. 'rtl.createInterface(this, "IBird", "{CC440C7F-7623-3DEE-AE88-000B86AAF108}", ["Fly"], this.IUnknown);',
  20241. 'rtl.createInterface(this, "IEagle", "{4B6A41C9-B020-3D7C-B688-96D19022B1B4}", [], this.IBird);',
  20242. 'rtl.createInterface(this, "IDove", "{4B6A41C9-B020-3D7C-B688-96D18EF16074}", [], this.IBird);',
  20243. 'rtl.createInterface(this, "ISwallow", "{BB6A41C9-B020-3D7C-B688-96D1CBDCB359}", [], this.IBird);',
  20244. 'rtl.createClass(this, "TObject", null, function () {',
  20245. ' this.$init = function () {',
  20246. ' };',
  20247. ' this.$final = function () {',
  20248. ' };',
  20249. '});',
  20250. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20251. ' rtl.addIntf(this, $mod.IBird);',
  20252. ' rtl.addIntf(this, $mod.IEagle);',
  20253. ' rtl.addIntf(this, $mod.IDove);',
  20254. ' rtl.addIntf(this, $mod.ISwallow);',
  20255. '});',
  20256. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  20257. ' this.$init = function () {',
  20258. ' $mod.TObject.$init.call(this);',
  20259. ' this.FBirdIntf = null;',
  20260. ' this.FDoveObj = null;',
  20261. ' };',
  20262. ' this.$final = function () {',
  20263. ' this.FBirdIntf = undefined;',
  20264. ' this.FDoveObj = undefined;',
  20265. ' $mod.TObject.$final.call(this);',
  20266. ' };',
  20267. ' this.$intfmaps = {',
  20268. ' "{CC440C7F-7623-3DEE-AE88-000B86AAF108}": function () {',
  20269. ' return rtl._AddRef(this.FBirdIntf);',
  20270. ' },',
  20271. ' "{4B6A41C9-B020-3D7C-B688-96D19022B1B4}": function () {',
  20272. ' return this.GetEagleIntf();',
  20273. ' },',
  20274. ' "{4B6A41C9-B020-3D7C-B688-96D18EF16074}": function () {',
  20275. ' return rtl.queryIntfT(this.FDoveObj, $mod.IDove);',
  20276. ' },',
  20277. ' "{BB6A41C9-B020-3D7C-B688-96D1CBDCB359}": function () {',
  20278. ' return rtl.queryIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  20279. ' }',
  20280. ' };',
  20281. '});',
  20282. '']),
  20283. LinesToStr([ // $mod.$main
  20284. '']));
  20285. end;
  20286. procedure TTestModule.TestClassInterface_COM_With;
  20287. begin
  20288. StartProgram(false);
  20289. Add([
  20290. '{$interfaces com}',
  20291. 'type',
  20292. ' IUnknown = interface',
  20293. ' function _AddRef: longint;',
  20294. ' function _Release: longint;',
  20295. ' function GetAnt: IUnknown;',
  20296. ' property Ant: IUnknown read GetAnt;',
  20297. ' end;',
  20298. ' TObject = class(IUnknown)',
  20299. ' function _AddRef: longint; virtual; abstract;',
  20300. ' function _Release: longint; virtual; abstract;',
  20301. ' function GetAnt: IUnknown; virtual; abstract;',
  20302. ' property Ant: IUnknown read GetAnt;',
  20303. ' end;',
  20304. 'procedure DoIt;',
  20305. 'var',
  20306. ' i: IUnknown;',
  20307. 'begin',
  20308. ' with i do ',
  20309. ' GetAnt;',
  20310. ' with i.Ant, Ant do ',
  20311. ' GetAnt;',
  20312. 'end;',
  20313. 'begin',
  20314. '']);
  20315. ConvertProgram;
  20316. CheckSource('TestClassInterface_COM_With',
  20317. LinesToStr([ // statements
  20318. 'rtl.createInterface(this, "IUnknown", "{D7ADB00D-C6B6-39FB-BDDF-21CD521DDFA9}", ["_AddRef", "_Release", "GetAnt"], null);',
  20319. 'rtl.createClass(this, "TObject", null, function () {',
  20320. ' this.$init = function () {',
  20321. ' };',
  20322. ' this.$final = function () {',
  20323. ' };',
  20324. ' rtl.addIntf(this, $mod.IUnknown);',
  20325. '});',
  20326. 'this.DoIt = function () {',
  20327. ' var i = null;',
  20328. ' var $ir = rtl.createIntfRefs();',
  20329. ' try {',
  20330. ' $ir.ref(1, i.GetAnt());',
  20331. ' var $with = $ir.ref(2, i.GetAnt());',
  20332. ' var $with1 = $ir.ref(3, $with.GetAnt());',
  20333. ' $ir.ref(4, $with1.GetAnt());',
  20334. ' } finally {',
  20335. ' $ir.free();',
  20336. ' };',
  20337. '};',
  20338. '']),
  20339. LinesToStr([ // $mod.$main
  20340. '']));
  20341. end;
  20342. procedure TTestModule.TestClassInterface_COM_ForIn;
  20343. begin
  20344. StartProgram(false);
  20345. Add([
  20346. '{$interfaces com}',
  20347. 'type',
  20348. ' IUnknown = interface end;',
  20349. ' TObject = class',
  20350. ' Id: longint;',
  20351. ' end;',
  20352. ' IEnumerator = interface(IUnknown)',
  20353. ' function GetCurrent: TObject;',
  20354. ' function MoveNext: Boolean;',
  20355. ' property Current: TObject read GetCurrent;',
  20356. ' end;',
  20357. ' IEnumerable = interface(IUnknown)',
  20358. ' function GetEnumerator: IEnumerator;',
  20359. ' end;',
  20360. 'var',
  20361. ' o: TObject;',
  20362. ' i: IEnumerable;',
  20363. 'begin',
  20364. ' for o in i do o.Id:=3;',
  20365. '']);
  20366. ConvertProgram;
  20367. CheckSource('TestClassInterface_COM_ForIn',
  20368. LinesToStr([ // statements
  20369. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  20370. 'rtl.createClass(this, "TObject", null, function () {',
  20371. ' this.$init = function () {',
  20372. ' this.Id = 0;',
  20373. ' };',
  20374. ' this.$final = function () {',
  20375. ' };',
  20376. '});',
  20377. 'rtl.createInterface(this, "IEnumerator", "{95D7745D-ED61-3F13-BBE4-07708161999E}", ["GetCurrent", "MoveNext"], this.IUnknown);',
  20378. 'rtl.createInterface(this, "IEnumerable", "{8CC9D45D-ED7D-3B73-96B6-290B931BB19E}", ["GetEnumerator"], this.IUnknown);',
  20379. 'this.o = null;',
  20380. 'this.i = null;',
  20381. '']),
  20382. LinesToStr([ // $mod.$main
  20383. 'var $in = $mod.i.GetEnumerator();',
  20384. 'try {',
  20385. ' while ($in.MoveNext()) {',
  20386. ' $mod.o = $in.GetCurrent();',
  20387. ' $mod.o.Id = 3;',
  20388. ' }',
  20389. '} finally {',
  20390. ' rtl._Release($in)',
  20391. '};',
  20392. '']));
  20393. end;
  20394. procedure TTestModule.TestClassInterface_COM_ArrayOfIntfFail;
  20395. begin
  20396. StartProgram(false);
  20397. Add([
  20398. '{$interfaces com}',
  20399. 'type',
  20400. ' IUnknown = interface',
  20401. ' function _AddRef: longint;',
  20402. ' function _Release: longint;',
  20403. ' end;',
  20404. ' TObject = class',
  20405. ' end;',
  20406. ' TArrOfIntf = array of IUnknown;',
  20407. 'begin',
  20408. '']);
  20409. SetExpectedPasResolverError('Not supported: array of COM-interface',nNotSupportedX);
  20410. ConvertProgram;
  20411. end;
  20412. procedure TTestModule.TestClassInterface_COM_RecordIntfFail;
  20413. begin
  20414. StartProgram(false);
  20415. Add([
  20416. '{$interfaces com}',
  20417. 'type',
  20418. ' IUnknown = interface',
  20419. ' function _AddRef: longint;',
  20420. ' function _Release: longint;',
  20421. ' end;',
  20422. ' TRec = record',
  20423. ' i: IUnknown;',
  20424. ' end;',
  20425. 'begin',
  20426. '']);
  20427. SetExpectedPasResolverError('Not supported: COM-interface as record member',nNotSupportedX);
  20428. ConvertProgram;
  20429. end;
  20430. procedure TTestModule.TestClassInterface_COM_UnitInitialization;
  20431. begin
  20432. StartUnit(false);
  20433. Add([
  20434. '{$interfaces com}',
  20435. 'interface',
  20436. 'implementation',
  20437. 'type',
  20438. ' IUnknown = interface',
  20439. ' function _AddRef: longint;',
  20440. ' end;',
  20441. ' TObject = class(IUnknown)',
  20442. ' function _AddRef: longint;',
  20443. ' end;',
  20444. 'function TObject._AddRef: longint; begin end;',
  20445. 'var i: IUnknown;',
  20446. ' o: TObject;',
  20447. 'initialization',
  20448. ' i:=nil;',
  20449. ' i:=i;',
  20450. ' i:=o;',
  20451. ' if (o as IUnknown)=nil then ;',
  20452. '']);
  20453. ConvertUnit;
  20454. CheckSource('TestClassInterface_COM_UnitInitialization',
  20455. LinesToStr([ // statements
  20456. 'var $impl = $mod.$impl;',
  20457. '']),
  20458. LinesToStr([ // this.$init
  20459. 'var $ir = rtl.createIntfRefs();',
  20460. 'try {',
  20461. ' rtl.setIntfP($impl, "i", null);',
  20462. ' rtl.setIntfP($impl, "i", $impl.i);',
  20463. ' rtl.setIntfP($impl, "i", rtl.queryIntfT($impl.o, $impl.IUnknown), true);',
  20464. ' if ($ir.ref(1, rtl.queryIntfT($impl.o, $impl.IUnknown)) === null) ;',
  20465. '} finally {',
  20466. ' $ir.free();',
  20467. '};',
  20468. '']),
  20469. LinesToStr([ // implementation
  20470. 'rtl.createInterface($impl, "IUnknown", "{B92D5841-758A-322B-BDDF-21CD52180000}", ["_AddRef"], null);',
  20471. 'rtl.createClass($impl, "TObject", null, function () {',
  20472. ' this.$init = function () {',
  20473. ' };',
  20474. ' this.$final = function () {',
  20475. ' };',
  20476. ' this._AddRef = function () {',
  20477. ' var Result = 0;',
  20478. ' return Result;',
  20479. ' };',
  20480. ' rtl.addIntf(this, $impl.IUnknown);',
  20481. '});',
  20482. '$impl.i = null;',
  20483. '$impl.o = null;',
  20484. ''])
  20485. );
  20486. end;
  20487. procedure TTestModule.TestClassInterface_GUID;
  20488. begin
  20489. StartProgram(false);
  20490. Add([
  20491. '{$interfaces corba}',
  20492. 'type',
  20493. ' IUnknown = interface',
  20494. ' [''{f31db68f-3010-D355-4EBA-CDD4EF4A737C}'']',
  20495. ' end;',
  20496. ' TObject = class end;',
  20497. ' TGUID = record D1, D2, D3, D4: word; end;',
  20498. ' TAliasGUID = TGUID;',
  20499. ' TGUIDString = type string;',
  20500. ' TAliasGUIDString = TGUIDString;',
  20501. 'procedure DoConstGUIDIt(const g: TAliasGUID); overload;',
  20502. 'begin end;',
  20503. 'procedure DoDefGUID(g: TAliasGUID); overload;',
  20504. 'begin end;',
  20505. 'procedure DoStr(const s: TAliasGUIDString); overload;',
  20506. 'begin end;',
  20507. 'var',
  20508. ' i: IUnknown;',
  20509. ' g: TAliasGUID = ''{d91c9af4-3C93-420F-A303-BF5BA82BFD23}'';',
  20510. ' s: TAliasGUIDString;',
  20511. 'begin',
  20512. ' DoConstGUIDIt(IUnknown);',
  20513. ' DoDefGUID(IUnknown);',
  20514. ' DoStr(IUnknown);',
  20515. ' DoConstGUIDIt(i);',
  20516. ' DoDefGUID(i);',
  20517. ' DoStr(i);',
  20518. ' DoConstGUIDIt(''{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}'');',
  20519. ' DoDefGUID(''{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}'');',
  20520. ' DoStr(g);',
  20521. ' g:=i;',
  20522. ' g:=IUnknown;',
  20523. ' g:=''{D91C9AF4-3C93-420F-A303-bf5ba82bfd23}'';',
  20524. ' s:=i;',
  20525. ' s:=IUnknown;',
  20526. ' s:=g;',
  20527. ' if g=i then ;',
  20528. ' if i=g then ;',
  20529. ' if g=IUnknown then ;',
  20530. ' if IUnknown=g then ;',
  20531. ' if s=i then ;',
  20532. ' if i=s then ;',
  20533. ' if s=IUnknown then ;',
  20534. ' if IUnknown=s then ;',
  20535. ' if s=g then ;',
  20536. ' if g=s then ;',
  20537. '']);
  20538. ConvertProgram;
  20539. CheckSource('TestClassInterface_GUID',
  20540. LinesToStr([ // statements
  20541. 'rtl.createInterface(this, "IUnknown", "{F31DB68F-3010-D355-4EBA-CDD4EF4A737C}", [], null);',
  20542. 'rtl.createClass(this, "TObject", null, function () {',
  20543. ' this.$init = function () {',
  20544. ' };',
  20545. ' this.$final = function () {',
  20546. ' };',
  20547. '});',
  20548. 'rtl.recNewT(this, "TGUID", function () {',
  20549. ' this.D1 = 0;',
  20550. ' this.D2 = 0;',
  20551. ' this.D3 = 0;',
  20552. ' this.D4 = 0;',
  20553. ' this.$eq = function (b) {',
  20554. ' return (this.D1 === b.D1) && (this.D2 === b.D2) && (this.D3 === b.D3) && (this.D4 === b.D4);',
  20555. ' };',
  20556. ' this.$assign = function (s) {',
  20557. ' this.D1 = s.D1;',
  20558. ' this.D2 = s.D2;',
  20559. ' this.D3 = s.D3;',
  20560. ' this.D4 = s.D4;',
  20561. ' return this;',
  20562. ' };',
  20563. '});',
  20564. 'this.DoConstGUIDIt = function (g) {',
  20565. '};',
  20566. 'this.DoDefGUID = function (g) {',
  20567. '};',
  20568. 'this.DoStr = function (s) {',
  20569. '};',
  20570. 'this.i = null;',
  20571. 'this.g = this.TGUID.$clone({',
  20572. ' D1: 0xD91C9AF4,',
  20573. ' D2: 0x3C93,',
  20574. ' D3: 0x420F,',
  20575. ' D4: [',
  20576. ' 0xA3,',
  20577. ' 0x03,',
  20578. ' 0xBF,',
  20579. ' 0x5B,',
  20580. ' 0xA8,',
  20581. ' 0x2B,',
  20582. ' 0xFD,',
  20583. ' 0x23',
  20584. ' ]',
  20585. '});',
  20586. 'this.s = "";',
  20587. '']),
  20588. LinesToStr([ // $mod.$main
  20589. '$mod.DoConstGUIDIt(rtl.getIntfGUIDR($mod.IUnknown));',
  20590. '$mod.DoDefGUID($mod.TGUID.$clone(rtl.getIntfGUIDR($mod.IUnknown)));',
  20591. '$mod.DoStr($mod.IUnknown.$guid);',
  20592. '$mod.DoConstGUIDIt(rtl.getIntfGUIDR($mod.i));',
  20593. '$mod.DoDefGUID($mod.TGUID.$clone(rtl.getIntfGUIDR($mod.i)));',
  20594. '$mod.DoStr($mod.i.$guid);',
  20595. '$mod.DoConstGUIDIt(rtl.strToGUIDR("{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}"));',
  20596. '$mod.DoDefGUID(rtl.strToGUIDR("{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}"));',
  20597. '$mod.DoStr(rtl.guidrToStr($mod.g));',
  20598. '$mod.g.$assign(rtl.getIntfGUIDR($mod.i));',
  20599. '$mod.g.$assign(rtl.getIntfGUIDR($mod.IUnknown));',
  20600. '$mod.g.$assign({',
  20601. ' D1: 0xD91C9AF4,',
  20602. ' D2: 0x3C93,',
  20603. ' D3: 0x420F,',
  20604. ' D4: [',
  20605. ' 0xA3,',
  20606. ' 0x03,',
  20607. ' 0xBF,',
  20608. ' 0x5B,',
  20609. ' 0xA8,',
  20610. ' 0x2B,',
  20611. ' 0xFD,',
  20612. ' 0x23',
  20613. ' ]',
  20614. '});',
  20615. '$mod.s = $mod.i.$guid;',
  20616. '$mod.s = $mod.IUnknown.$guid;',
  20617. '$mod.s = rtl.guidrToStr($mod.g);',
  20618. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.i))) ;',
  20619. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.i))) ;',
  20620. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.IUnknown))) ;',
  20621. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.IUnknown))) ;',
  20622. 'if ($mod.s === $mod.i.$guid) ;',
  20623. 'if ($mod.i.$guid === $mod.s) ;',
  20624. 'if ($mod.s === $mod.IUnknown.$guid) ;',
  20625. 'if ($mod.IUnknown.$guid === $mod.s) ;',
  20626. 'if ($mod.g.$eq(rtl.createTGUID($mod.s))) ;',
  20627. 'if ($mod.g.$eq(rtl.createTGUID($mod.s))) ;',
  20628. '']));
  20629. end;
  20630. procedure TTestModule.TestClassInterface_GUIDProperty;
  20631. begin
  20632. StartProgram(false);
  20633. Add([
  20634. '{$interfaces corba}',
  20635. 'type',
  20636. ' IUnknown = interface',
  20637. ' [''{f31db68f-3010-D355-4EBA-CDD4EF4A737C}'']',
  20638. ' end;',
  20639. ' TGUID = record D1, D2, D3, D4: word; end;',
  20640. ' TAliasGUID = TGUID;',
  20641. ' TGUIDString = type string;',
  20642. ' TAliasGUIDString = TGUIDString;',
  20643. ' TObject = class',
  20644. ' function GetG: TAliasGUID; virtual; abstract;',
  20645. ' procedure SetG(const Value: TAliasGUID); virtual; abstract;',
  20646. ' function GetS: TAliasGUIDString; virtual; abstract;',
  20647. ' procedure SetS(const Value: TAliasGUIDString); virtual; abstract;',
  20648. ' property g: TAliasGUID read GetG write SetG;',
  20649. ' property s: TAliasGUIDString read GetS write SetS;',
  20650. ' end;',
  20651. 'var o: TObject;',
  20652. 'begin',
  20653. ' o.g:=IUnknown;',
  20654. ' o.g:=''{D91C9AF4-3C93-420F-A303-bf5ba82bfd23}'';',
  20655. ' o.s:=IUnknown;',
  20656. ' o.s:=o.g;',
  20657. '']);
  20658. ConvertProgram;
  20659. CheckSource('TestClassInterface_GUIDProperty',
  20660. LinesToStr([ // statements
  20661. 'rtl.createInterface(this, "IUnknown", "{F31DB68F-3010-D355-4EBA-CDD4EF4A737C}", [], null);',
  20662. 'rtl.recNewT(this, "TGUID", function () {',
  20663. ' this.D1 = 0;',
  20664. ' this.D2 = 0;',
  20665. ' this.D3 = 0;',
  20666. ' this.D4 = 0;',
  20667. ' this.$eq = function (b) {',
  20668. ' return (this.D1 === b.D1) && (this.D2 === b.D2) && (this.D3 === b.D3) && (this.D4 === b.D4);',
  20669. ' };',
  20670. ' this.$assign = function (s) {',
  20671. ' this.D1 = s.D1;',
  20672. ' this.D2 = s.D2;',
  20673. ' this.D3 = s.D3;',
  20674. ' this.D4 = s.D4;',
  20675. ' return this;',
  20676. ' };',
  20677. '});',
  20678. 'rtl.createClass(this, "TObject", null, function () {',
  20679. ' this.$init = function () {',
  20680. ' };',
  20681. ' this.$final = function () {',
  20682. ' };',
  20683. '});',
  20684. 'this.o = null;',
  20685. '']),
  20686. LinesToStr([ // $mod.$main
  20687. '$mod.o.SetG(rtl.getIntfGUIDR($mod.IUnknown));',
  20688. '$mod.o.SetG({',
  20689. ' D1: 0xD91C9AF4,',
  20690. ' D2: 0x3C93,',
  20691. ' D3: 0x420F,',
  20692. ' D4: [',
  20693. ' 0xA3,',
  20694. ' 0x03,',
  20695. ' 0xBF,',
  20696. ' 0x5B,',
  20697. ' 0xA8,',
  20698. ' 0x2B,',
  20699. ' 0xFD,',
  20700. ' 0x23',
  20701. ' ]',
  20702. '});',
  20703. '$mod.o.SetS($mod.IUnknown.$guid);',
  20704. '$mod.o.SetS(rtl.guidrToStr($mod.o.GetG()));',
  20705. '']));
  20706. end;
  20707. procedure TTestModule.TestClassHelper_ClassVar;
  20708. begin
  20709. StartProgram(false);
  20710. Add([
  20711. 'type',
  20712. ' TObject = class',
  20713. ' end;',
  20714. ' THelper = class helper for TObject',
  20715. ' const',
  20716. ' One = 1;',
  20717. ' Two: word = 2;',
  20718. ' class var',
  20719. ' Glob: word;',
  20720. ' function Foo(w: word): word;',
  20721. ' class function Bar(w: word): word;',
  20722. ' end;',
  20723. 'function THelper.foo(w: word): word;',
  20724. 'begin',
  20725. ' Result:=w;',
  20726. ' Two:=One+w;',
  20727. ' Glob:=Glob;',
  20728. ' Result:=Self.Glob;',
  20729. ' Self.Glob:=Self.Glob;',
  20730. ' with Self do Glob:=Glob;',
  20731. 'end;',
  20732. 'class function THelper.bar(w: word): word;',
  20733. 'begin',
  20734. ' Result:=w;',
  20735. ' Two:=One;',
  20736. ' Glob:=Glob;',
  20737. ' Self.Glob:=Self.Glob;',
  20738. ' with Self do Glob:=Glob;',
  20739. 'end;',
  20740. 'var o: TObject;',
  20741. 'begin',
  20742. ' tobject.two:=tobject.one;',
  20743. ' tobject.Glob:=tobject.Glob;',
  20744. ' with tobject do begin',
  20745. ' two:=one;',
  20746. ' Glob:=Glob;',
  20747. ' end;',
  20748. ' o.two:=o.one;',
  20749. ' o.Glob:=o.Glob;',
  20750. ' with o do begin',
  20751. ' two:=one;',
  20752. ' Glob:=Glob;',
  20753. ' end;',
  20754. '']);
  20755. ConvertProgram;
  20756. CheckSource('TestClassHelper_ClassVar',
  20757. LinesToStr([ // statements
  20758. 'rtl.createClass(this, "TObject", null, function () {',
  20759. ' this.$init = function () {',
  20760. ' };',
  20761. ' this.$final = function () {',
  20762. ' };',
  20763. '});',
  20764. 'rtl.createHelper(this, "THelper", null, function () {',
  20765. ' this.One = 1;',
  20766. ' this.Two = 2;',
  20767. ' this.Glob = 0;',
  20768. ' this.Foo = function (w) {',
  20769. ' var Result = 0;',
  20770. ' Result = w;',
  20771. ' $mod.THelper.Two = 1 + w;',
  20772. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20773. ' Result = $mod.THelper.Glob;',
  20774. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20775. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20776. ' return Result;',
  20777. ' };',
  20778. ' this.Bar = function (w) {',
  20779. ' var Result = 0;',
  20780. ' Result = w;',
  20781. ' $mod.THelper.Two = 1;',
  20782. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20783. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20784. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20785. ' return Result;',
  20786. ' };',
  20787. '});',
  20788. 'this.o = null;',
  20789. '']),
  20790. LinesToStr([ // $mod.$main
  20791. '$mod.THelper.Two = 1;',
  20792. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20793. 'var $with = $mod.TObject;',
  20794. '$mod.THelper.Two = 1;',
  20795. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20796. '$mod.THelper.Two = 1;',
  20797. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20798. 'var $with1 = $mod.o;',
  20799. '$mod.THelper.Two = 1;',
  20800. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20801. '']));
  20802. end;
  20803. procedure TTestModule.TestClassHelper_Method_AccessInstanceFields;
  20804. begin
  20805. StartProgram(false);
  20806. Add([
  20807. 'type',
  20808. ' TObject = class',
  20809. ' FSize: word;',
  20810. ' property Size: word read FSize write FSize;',
  20811. ' end;',
  20812. ' THelper = class helper for TObject',
  20813. ' function Foo(w: word = 1): word;',
  20814. ' end;',
  20815. 'function THelper.foo(w: word): word;',
  20816. 'begin',
  20817. ' Result:=Size;',
  20818. ' Size:=Size+2;',
  20819. ' Self.Size:=Self.Size+3;',
  20820. ' FSize:=FSize+4;',
  20821. ' Self.FSize:=Self.FSize+5;',
  20822. ' with Self do begin',
  20823. ' Size:=Size+6;',
  20824. ' FSize:=FSize+7;',
  20825. ' FSize:=FSize+8;',
  20826. ' end;',
  20827. 'end;',
  20828. 'begin',
  20829. '']);
  20830. ConvertProgram;
  20831. CheckSource('TestClassHelper_Method_AccessInstanceFields',
  20832. LinesToStr([ // statements
  20833. 'rtl.createClass(this, "TObject", null, function () {',
  20834. ' this.$init = function () {',
  20835. ' this.FSize = 0;',
  20836. ' };',
  20837. ' this.$final = function () {',
  20838. ' };',
  20839. '});',
  20840. 'rtl.createHelper(this, "THelper", null, function () {',
  20841. ' this.Foo = function (w) {',
  20842. ' var Result = 0;',
  20843. ' Result = this.FSize;',
  20844. ' this.FSize = this.FSize + 2;',
  20845. ' this.FSize = this.FSize + 3;',
  20846. ' this.FSize = this.FSize + 4;',
  20847. ' this.FSize = this.FSize + 5;',
  20848. ' this.FSize = this.FSize + 6;',
  20849. ' this.FSize = this.FSize + 7;',
  20850. ' this.FSize = this.FSize + 8;',
  20851. ' return Result;',
  20852. ' };',
  20853. '});',
  20854. '']),
  20855. LinesToStr([ // $mod.$main
  20856. '']));
  20857. end;
  20858. procedure TTestModule.TestClassHelper_Method_Call;
  20859. begin
  20860. StartProgram(false);
  20861. Add([
  20862. 'type',
  20863. ' TObject = class',
  20864. ' procedure Run(w: word = 10);',
  20865. ' end;',
  20866. ' THelper = class helper for TObject',
  20867. ' function Foo(w: word = 1): word;',
  20868. ' end;',
  20869. 'procedure TObject.Run(w: word);',
  20870. 'var o: TObject;',
  20871. 'begin',
  20872. ' Foo;',
  20873. ' Foo();',
  20874. ' Foo(2);',
  20875. ' Self.Foo;',
  20876. ' Self.Foo();',
  20877. ' Self.Foo(3);',
  20878. ' with Self do begin',
  20879. ' Foo;',
  20880. ' Foo();',
  20881. ' Foo(4);',
  20882. ' end;',
  20883. ' with o do Foo(5);',
  20884. 'end;',
  20885. 'function THelper.foo(w: word): word;',
  20886. 'begin',
  20887. ' Run;',
  20888. ' Run();',
  20889. ' Run(11);',
  20890. ' Foo;',
  20891. ' Foo();',
  20892. ' Foo(12);',
  20893. ' Self.Foo;',
  20894. ' Self.Foo();',
  20895. ' Self.Foo(13);',
  20896. ' with Self do begin',
  20897. ' Foo;',
  20898. ' Foo();',
  20899. ' Foo(14);',
  20900. ' end;',
  20901. 'end;',
  20902. 'var Obj: TObject;',
  20903. 'begin',
  20904. ' obj.Foo;',
  20905. ' obj.Foo();',
  20906. ' obj.Foo(21);',
  20907. ' with obj do begin',
  20908. ' Foo;',
  20909. ' Foo();',
  20910. ' Foo(22);',
  20911. ' end;',
  20912. '']);
  20913. ConvertProgram;
  20914. CheckSource('TestClassHelper_Method_Call',
  20915. LinesToStr([ // statements
  20916. 'rtl.createClass(this, "TObject", null, function () {',
  20917. ' this.$init = function () {',
  20918. ' };',
  20919. ' this.$final = function () {',
  20920. ' };',
  20921. ' this.Run = function (w) {',
  20922. ' var o = null;',
  20923. ' $mod.THelper.Foo.call(this, 1);',
  20924. ' $mod.THelper.Foo.call(this, 1);',
  20925. ' $mod.THelper.Foo.call(this, 2);',
  20926. ' $mod.THelper.Foo.call(this, 1);',
  20927. ' $mod.THelper.Foo.call(this, 1);',
  20928. ' $mod.THelper.Foo.call(this, 3);',
  20929. ' $mod.THelper.Foo.call(this, 1);',
  20930. ' $mod.THelper.Foo.call(this, 1);',
  20931. ' $mod.THelper.Foo.call(this, 4);',
  20932. ' $mod.THelper.Foo.call(o, 5);',
  20933. ' };',
  20934. '});',
  20935. 'rtl.createHelper(this, "THelper", null, function () {',
  20936. ' this.Foo = function (w) {',
  20937. ' var Result = 0;',
  20938. ' this.Run(10);',
  20939. ' this.Run(10);',
  20940. ' this.Run(11);',
  20941. ' $mod.THelper.Foo.call(this, 1);',
  20942. ' $mod.THelper.Foo.call(this, 1);',
  20943. ' $mod.THelper.Foo.call(this, 12);',
  20944. ' $mod.THelper.Foo.call(this, 1);',
  20945. ' $mod.THelper.Foo.call(this, 1);',
  20946. ' $mod.THelper.Foo.call(this, 13);',
  20947. ' $mod.THelper.Foo.call(this, 1);',
  20948. ' $mod.THelper.Foo.call(this, 1);',
  20949. ' $mod.THelper.Foo.call(this, 14);',
  20950. ' return Result;',
  20951. ' };',
  20952. '});',
  20953. 'this.Obj = null;',
  20954. '']),
  20955. LinesToStr([ // $mod.$main
  20956. '$mod.THelper.Foo.call($mod.Obj, 1);',
  20957. '$mod.THelper.Foo.call($mod.Obj, 1);',
  20958. '$mod.THelper.Foo.call($mod.Obj, 21);',
  20959. 'var $with = $mod.Obj;',
  20960. '$mod.THelper.Foo.call($with, 1);',
  20961. '$mod.THelper.Foo.call($with, 1);',
  20962. '$mod.THelper.Foo.call($with, 22);',
  20963. '']));
  20964. end;
  20965. procedure TTestModule.TestClassHelper_Method_Nested_Call;
  20966. begin
  20967. StartProgram(false);
  20968. Add([
  20969. 'type',
  20970. ' TObject = class',
  20971. ' procedure Run(w: word = 10);',
  20972. ' end;',
  20973. ' THelper = class helper for TObject',
  20974. ' function Foo(w: word = 1): word;',
  20975. ' end;',
  20976. 'procedure TObject.Run(w: word);',
  20977. ' procedure Sub(Self: TObject);',
  20978. ' begin',
  20979. ' Foo;',
  20980. ' Foo();',
  20981. ' Self.Foo;',
  20982. ' Self.Foo();',
  20983. ' with Self do begin',
  20984. ' Foo;',
  20985. ' Foo();',
  20986. ' end;',
  20987. ' end;',
  20988. 'begin',
  20989. 'end;',
  20990. 'function THelper.foo(w: word): word;',
  20991. ' procedure Sub(Self: TObject);',
  20992. ' begin',
  20993. ' Run;',
  20994. ' Run();',
  20995. ' Foo;',
  20996. ' Foo();',
  20997. ' Self.Foo;',
  20998. ' Self.Foo();',
  20999. ' with Self do begin',
  21000. ' Foo;',
  21001. ' Foo();',
  21002. ' end;',
  21003. ' end;',
  21004. 'begin',
  21005. 'end;',
  21006. 'begin',
  21007. '']);
  21008. ConvertProgram;
  21009. CheckSource('TestClassHelper_Method_Nested_Call',
  21010. LinesToStr([ // statements
  21011. 'rtl.createClass(this, "TObject", null, function () {',
  21012. ' this.$init = function () {',
  21013. ' };',
  21014. ' this.$final = function () {',
  21015. ' };',
  21016. ' this.Run = function (w) {',
  21017. ' var $Self = this;',
  21018. ' function Sub(Self) {',
  21019. ' $mod.THelper.Foo.call($Self, 1);',
  21020. ' $mod.THelper.Foo.call($Self, 1);',
  21021. ' $mod.THelper.Foo.call(Self, 1);',
  21022. ' $mod.THelper.Foo.call(Self, 1);',
  21023. ' $mod.THelper.Foo.call(Self, 1);',
  21024. ' $mod.THelper.Foo.call(Self, 1);',
  21025. ' };',
  21026. ' };',
  21027. '});',
  21028. 'rtl.createHelper(this, "THelper", null, function () {',
  21029. ' this.Foo = function (w) {',
  21030. ' var $Self = this;',
  21031. ' var Result = 0;',
  21032. ' function Sub(Self) {',
  21033. ' $Self.Run(10);',
  21034. ' $Self.Run(10);',
  21035. ' $mod.THelper.Foo.call($Self, 1);',
  21036. ' $mod.THelper.Foo.call($Self, 1);',
  21037. ' $mod.THelper.Foo.call(Self, 1);',
  21038. ' $mod.THelper.Foo.call(Self, 1);',
  21039. ' $mod.THelper.Foo.call(Self, 1);',
  21040. ' $mod.THelper.Foo.call(Self, 1);',
  21041. ' };',
  21042. ' return Result;',
  21043. ' };',
  21044. '});',
  21045. '']),
  21046. LinesToStr([ // $mod.$main
  21047. '']));
  21048. end;
  21049. procedure TTestModule.TestClassHelper_ClassMethod_Call;
  21050. begin
  21051. StartProgram(false);
  21052. Add([
  21053. 'type',
  21054. ' TObject = class',
  21055. ' class procedure Run(w: word = 10);',
  21056. ' end;',
  21057. ' THelper = class helper for TObject',
  21058. ' class function Foo(w: word = 1): word;',
  21059. ' end;',
  21060. 'class procedure TObject.Run(w: word);',
  21061. 'begin',
  21062. ' Foo;',
  21063. ' Foo();',
  21064. ' Self.Foo;',
  21065. ' Self.Foo();',
  21066. ' with Self do begin',
  21067. ' Foo;',
  21068. ' Foo();',
  21069. ' end;',
  21070. 'end;',
  21071. 'class function THelper.foo(w: word): word;',
  21072. 'begin',
  21073. ' Run;',
  21074. ' Run();',
  21075. ' Foo;',
  21076. ' Foo();',
  21077. ' Self.Foo;',
  21078. ' Self.Foo();',
  21079. ' with Self do begin',
  21080. ' Foo;',
  21081. ' Foo();',
  21082. ' end;',
  21083. 'end;',
  21084. 'var',
  21085. ' Obj: TObject;',
  21086. 'begin',
  21087. ' obj.Foo;',
  21088. ' obj.Foo();',
  21089. ' with obj do begin',
  21090. ' Foo;',
  21091. ' Foo();',
  21092. ' end;',
  21093. ' tobject.Foo;',
  21094. ' tobject.Foo();',
  21095. ' with tobject do begin',
  21096. ' Foo;',
  21097. ' Foo();',
  21098. ' end;',
  21099. '']);
  21100. ConvertProgram;
  21101. CheckSource('TestClassHelper_ClassMethod_Call',
  21102. LinesToStr([ // statements
  21103. 'rtl.createClass(this, "TObject", null, function () {',
  21104. ' this.$init = function () {',
  21105. ' };',
  21106. ' this.$final = function () {',
  21107. ' };',
  21108. ' this.Run = function (w) {',
  21109. ' $mod.THelper.Foo.call(this, 1);',
  21110. ' $mod.THelper.Foo.call(this, 1);',
  21111. ' $mod.THelper.Foo.call(this, 1);',
  21112. ' $mod.THelper.Foo.call(this, 1);',
  21113. ' $mod.THelper.Foo.call(this, 1);',
  21114. ' $mod.THelper.Foo.call(this, 1);',
  21115. ' };',
  21116. '});',
  21117. 'rtl.createHelper(this, "THelper", null, function () {',
  21118. ' this.Foo = function (w) {',
  21119. ' var Result = 0;',
  21120. ' this.Run(10);',
  21121. ' this.Run(10);',
  21122. ' $mod.THelper.Foo.call(this, 1);',
  21123. ' $mod.THelper.Foo.call(this, 1);',
  21124. ' $mod.THelper.Foo.call(this, 1);',
  21125. ' $mod.THelper.Foo.call(this, 1);',
  21126. ' $mod.THelper.Foo.call(this, 1);',
  21127. ' $mod.THelper.Foo.call(this, 1);',
  21128. ' return Result;',
  21129. ' };',
  21130. '});',
  21131. 'this.Obj = null;',
  21132. '']),
  21133. LinesToStr([ // $mod.$main
  21134. '$mod.THelper.Foo.call($mod.Obj.$class, 1);',
  21135. '$mod.THelper.Foo.call($mod.Obj.$class, 1);',
  21136. 'var $with = $mod.Obj;',
  21137. '$mod.THelper.Foo.call($with.$class, 1);',
  21138. '$mod.THelper.Foo.call($with.$class, 1);',
  21139. '$mod.THelper.Foo.call($mod.TObject, 1);',
  21140. '$mod.THelper.Foo.call($mod.TObject, 1);',
  21141. 'var $with1 = $mod.TObject;',
  21142. '$mod.THelper.Foo.call($mod.TObject, 1);',
  21143. '$mod.THelper.Foo.call($mod.TObject, 1);',
  21144. '']));
  21145. end;
  21146. procedure TTestModule.TestClassHelper_ClassOf;
  21147. begin
  21148. StartProgram(false);
  21149. Add([
  21150. 'type',
  21151. ' TObject = class',
  21152. ' end;',
  21153. ' TClass = class of TObject;',
  21154. ' THelper = class helper for TObject',
  21155. ' class function Foo(w: word = 1): word;',
  21156. ' end;',
  21157. 'class function THelper.foo(w: word): word;',
  21158. 'begin',
  21159. 'end;',
  21160. 'var',
  21161. ' c: TClass;',
  21162. 'begin',
  21163. ' c.Foo;',
  21164. ' c.Foo();',
  21165. ' with c do begin',
  21166. ' Foo;',
  21167. ' Foo();',
  21168. ' end;',
  21169. '']);
  21170. ConvertProgram;
  21171. CheckSource('TestClassHelper_ClassOf',
  21172. LinesToStr([ // statements
  21173. 'rtl.createClass(this, "TObject", null, function () {',
  21174. ' this.$init = function () {',
  21175. ' };',
  21176. ' this.$final = function () {',
  21177. ' };',
  21178. '});',
  21179. 'rtl.createHelper(this, "THelper", null, function () {',
  21180. ' this.Foo = function (w) {',
  21181. ' var Result = 0;',
  21182. ' return Result;',
  21183. ' };',
  21184. '});',
  21185. 'this.c = null;',
  21186. '']),
  21187. LinesToStr([ // $mod.$main
  21188. '$mod.THelper.Foo.call($mod.c, 1);',
  21189. '$mod.THelper.Foo.call($mod.c, 1);',
  21190. 'var $with = $mod.c;',
  21191. '$mod.THelper.Foo.call($with, 1);',
  21192. '$mod.THelper.Foo.call($with, 1);',
  21193. '']));
  21194. end;
  21195. procedure TTestModule.TestClassHelper_MethodRefObjFPC;
  21196. begin
  21197. StartProgram(false);
  21198. Add([
  21199. '{$mode objfpc}',
  21200. 'type',
  21201. ' TObject = class',
  21202. ' procedure DoIt;',
  21203. ' end;',
  21204. ' THelper = class helper for TObject',
  21205. ' procedure Fly(w: word = 1);',
  21206. ' class procedure Glide(w: word = 1);',
  21207. ' class procedure Run(w: word = 1); static;',
  21208. ' end;',
  21209. ' TFly = procedure(w: word) of object;',
  21210. ' TGlide = TFly;',
  21211. ' TRun = procedure(w: word);',
  21212. 'var',
  21213. ' f: TFly;',
  21214. ' g: TGlide;',
  21215. ' r: TRun;',
  21216. 'procedure TObject.DoIt;',
  21217. 'begin',
  21218. ' f:=@fly;',
  21219. ' g:=@glide;',
  21220. ' r:=@run;',
  21221. ' f:[email protected];',
  21222. ' g:[email protected];',
  21223. ' r:[email protected];',
  21224. ' with self do begin',
  21225. ' f:=@fly;',
  21226. ' g:=@glide;',
  21227. ' r:=@run;',
  21228. ' end;',
  21229. 'end;',
  21230. 'procedure THelper.fly(w: word);',
  21231. 'begin',
  21232. ' f:=@fly;',
  21233. ' g:=@glide;',
  21234. ' r:=@run;',
  21235. 'end;',
  21236. 'class procedure THelper.glide(w: word);',
  21237. 'begin',
  21238. ' g:=@glide;',
  21239. ' r:=@run;',
  21240. 'end;',
  21241. 'class procedure THelper.run(w: word);',
  21242. 'begin',
  21243. ' g:=@glide;',
  21244. ' r:=@run;',
  21245. 'end;',
  21246. 'var',
  21247. ' Obj: TObject;',
  21248. 'begin',
  21249. ' f:[email protected];',
  21250. ' g:[email protected];',
  21251. ' r:[email protected];',
  21252. ' with obj do begin',
  21253. ' f:=@fly;',
  21254. ' g:=@glide;',
  21255. ' r:=@run;',
  21256. ' end;',
  21257. ' g:[email protected];',
  21258. ' r:[email protected];',
  21259. ' with tobject do begin',
  21260. ' g:=@glide;',
  21261. ' r:=@run;',
  21262. ' end;',
  21263. '']);
  21264. ConvertProgram;
  21265. CheckSource('TestClassHelper_MethodRefObjFPC',
  21266. LinesToStr([ // statements
  21267. 'rtl.createClass(this, "TObject", null, function () {',
  21268. ' this.$init = function () {',
  21269. ' };',
  21270. ' this.$final = function () {',
  21271. ' };',
  21272. ' this.DoIt = function () {',
  21273. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  21274. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  21275. ' $mod.r = $mod.THelper.Run;',
  21276. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  21277. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  21278. ' $mod.r = $mod.THelper.Run;',
  21279. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  21280. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  21281. ' $mod.r = $mod.THelper.Run;',
  21282. ' };',
  21283. '});',
  21284. 'rtl.createHelper(this, "THelper", null, function () {',
  21285. ' this.Fly = function (w) {',
  21286. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  21287. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  21288. ' $mod.r = $mod.THelper.Run;',
  21289. ' };',
  21290. ' this.Glide = function (w) {',
  21291. ' $mod.g = rtl.createCallback(this, $mod.THelper.Glide);',
  21292. ' $mod.r = $mod.THelper.Run;',
  21293. ' };',
  21294. ' this.Run = function (w) {',
  21295. ' $mod.g = rtl.createCallback($mod.THelper, $mod.THelper.Glide);',
  21296. ' $mod.r = $mod.THelper.Run;',
  21297. ' };',
  21298. '});',
  21299. 'this.f = null;',
  21300. 'this.g = null;',
  21301. 'this.r = null;',
  21302. 'this.Obj = null;',
  21303. '']),
  21304. LinesToStr([ // $mod.$main
  21305. '$mod.f = rtl.createCallback($mod.Obj, $mod.THelper.Fly);',
  21306. '$mod.g = rtl.createCallback($mod.Obj.$class, $mod.THelper.Glide);',
  21307. '$mod.r = $mod.THelper.Run;',
  21308. 'var $with = $mod.Obj;',
  21309. '$mod.f = rtl.createCallback($with, $mod.THelper.Fly);',
  21310. '$mod.g = rtl.createCallback($with.$class, $mod.THelper.Glide);',
  21311. '$mod.r = $mod.THelper.Run;',
  21312. '$mod.g = rtl.createCallback($mod.TObject, $mod.THelper.Glide);',
  21313. '$mod.r = $mod.THelper.Run;',
  21314. 'var $with1 = $mod.TObject;',
  21315. '$mod.g = rtl.createCallback($with1, $mod.THelper.Glide);',
  21316. '$mod.r = $mod.THelper.Run;',
  21317. '']));
  21318. end;
  21319. procedure TTestModule.TestClassHelper_Constructor;
  21320. begin
  21321. StartProgram(false);
  21322. Add([
  21323. 'type',
  21324. ' TObject = class',
  21325. ' constructor Create;',
  21326. ' end;',
  21327. ' TClass = class of TObject;',
  21328. ' THelper = class helper for TObject',
  21329. ' constructor NewHlp(w: word);',
  21330. ' end;',
  21331. 'var',
  21332. ' obj: TObject;',
  21333. ' c: TClass;',
  21334. 'constructor TObject.Create;',
  21335. 'begin',
  21336. ' NewHlp(2);', // normal call
  21337. ' tobject.NewHlp(3);', // new instance
  21338. ' c.newhlp(4);', // new instance
  21339. 'end;',
  21340. 'constructor THelper.NewHlp(w: word);',
  21341. 'begin',
  21342. ' create;', // normal call
  21343. ' tobject.create;', // new instance
  21344. ' NewHlp(2);', // normal call
  21345. ' tobject.NewHlp(3);', // new instance
  21346. ' c.newhlp(4);', // new instance
  21347. 'end;',
  21348. 'begin',
  21349. ' obj.newhlp(2);', // normal call
  21350. ' with Obj do newhlp(12);', // normal call
  21351. ' tobject.newhlp(3);', // new instance
  21352. ' with tobject do newhlp(13);', // new instance
  21353. ' c.newhlp(4);', // new instance
  21354. ' with c do newhlp(14);', // new instance
  21355. '']);
  21356. ConvertProgram;
  21357. CheckSource('TestClassHelper_Constructor',
  21358. LinesToStr([ // statements
  21359. 'rtl.createClass(this, "TObject", null, function () {',
  21360. ' this.$init = function () {',
  21361. ' };',
  21362. ' this.$final = function () {',
  21363. ' };',
  21364. ' this.Create = function () {',
  21365. ' $mod.THelper.NewHlp.call(this, 2);',
  21366. ' $mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  21367. ' $mod.c.$create($mod.THelper.NewHlp, [4]);',
  21368. ' return this;',
  21369. ' };',
  21370. '});',
  21371. 'rtl.createHelper(this, "THelper", null, function () {',
  21372. ' this.NewHlp = function (w) {',
  21373. ' this.Create();',
  21374. ' $mod.TObject.$create("Create");',
  21375. ' $mod.THelper.NewHlp.call(this, 2);',
  21376. ' $mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  21377. ' $mod.c.$create($mod.THelper.NewHlp, [4]);',
  21378. ' return this;',
  21379. ' };',
  21380. '});',
  21381. 'this.obj = null;',
  21382. 'this.c = null;',
  21383. '']),
  21384. LinesToStr([ // $mod.$main
  21385. '$mod.THelper.NewHlp.call($mod.obj, 2);',
  21386. 'var $with = $mod.obj;',
  21387. '$mod.THelper.NewHlp.call($with, 12);',
  21388. '$mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  21389. 'var $with1 = $mod.TObject;',
  21390. '$with1.$create($mod.THelper.NewHlp, [13]);',
  21391. '$mod.c.$create($mod.THelper.NewHlp, [4]);',
  21392. 'var $with2 = $mod.c;',
  21393. '$with2.$create($mod.THelper.NewHlp, [14]);',
  21394. '']));
  21395. end;
  21396. procedure TTestModule.TestClassHelper_InheritedObjFPC;
  21397. begin
  21398. StartProgram(false);
  21399. Add([
  21400. 'type',
  21401. ' TObject = class',
  21402. ' procedure Fly;',
  21403. ' end;',
  21404. ' TObjHelper = class helper for TObject',
  21405. ' procedure Fly;',
  21406. ' end;',
  21407. ' TBird = class',
  21408. ' procedure Fly;',
  21409. ' end;',
  21410. ' TBirdHelper = class helper for TBird',
  21411. ' procedure Fly;',
  21412. ' procedure Walk(w: word);',
  21413. ' end;',
  21414. ' TEagleHelper = class helper(TBirdHelper) for TBird',
  21415. ' procedure Fly;',
  21416. ' procedure Walk(w: word);',
  21417. ' end;',
  21418. 'procedure Tobject.fly;',
  21419. 'begin',
  21420. ' inherited;', // ignore
  21421. 'end;',
  21422. 'procedure Tobjhelper.fly;',
  21423. 'begin',
  21424. ' {@TObject_Fly}inherited;',
  21425. ' inherited {@TObject_Fly}Fly;',
  21426. 'end;',
  21427. 'procedure Tbird.fly;',
  21428. 'begin',
  21429. ' {@TObjHelper_Fly}inherited;',
  21430. ' inherited {@TObjHelper_Fly}Fly;',
  21431. 'end;',
  21432. 'procedure Tbirdhelper.fly;',
  21433. 'begin',
  21434. ' {@TBird_Fly}inherited;',
  21435. ' inherited {@TBird_Fly}Fly;',
  21436. 'end;',
  21437. 'procedure Tbirdhelper.walk(w: word);',
  21438. 'begin',
  21439. 'end;',
  21440. 'procedure teagleHelper.fly;',
  21441. 'begin',
  21442. ' {@TBird_Fly}inherited;',
  21443. ' inherited {@TBird_Fly}Fly;',
  21444. 'end;',
  21445. 'procedure teagleHelper.walk(w: word);',
  21446. 'begin',
  21447. ' {@TBirdHelper_Walk}inherited;',
  21448. ' inherited {@TBirdHelper_Walk}Walk(3);',
  21449. 'end;',
  21450. 'begin',
  21451. '']);
  21452. ConvertProgram;
  21453. CheckSource('TestClassHelper_InheritedObjFPC',
  21454. LinesToStr([ // statements
  21455. 'rtl.createClass(this, "TObject", null, function () {',
  21456. ' this.$init = function () {',
  21457. ' };',
  21458. ' this.$final = function () {',
  21459. ' };',
  21460. ' this.Fly = function () {',
  21461. ' };',
  21462. '});',
  21463. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21464. ' this.Fly = function () {',
  21465. ' $mod.TObject.Fly.call(this);',
  21466. ' $mod.TObject.Fly.call(this);',
  21467. ' };',
  21468. '});',
  21469. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21470. ' this.Fly$1 = function () {',
  21471. ' $mod.TObjHelper.Fly.call(this);',
  21472. ' $mod.TObjHelper.Fly.call(this);',
  21473. ' };',
  21474. '});',
  21475. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  21476. ' this.Fly = function () {',
  21477. ' $mod.TBird.Fly$1.call(this);',
  21478. ' $mod.TBird.Fly$1.call(this);',
  21479. ' };',
  21480. ' this.Walk = function (w) {',
  21481. ' };',
  21482. '});',
  21483. 'rtl.createHelper(this, "TEagleHelper", this.TBirdHelper, function () {',
  21484. ' this.Fly$1 = function () {',
  21485. ' $mod.TBird.Fly$1.call(this);',
  21486. ' $mod.TBird.Fly$1.call(this);',
  21487. ' };',
  21488. ' this.Walk$1 = function (w) {',
  21489. ' $mod.TBirdHelper.Walk.apply(this, arguments);',
  21490. ' $mod.TBirdHelper.Walk.call(this, 3);',
  21491. ' };',
  21492. '});',
  21493. '']),
  21494. LinesToStr([ // $mod.$main
  21495. '']));
  21496. end;
  21497. procedure TTestModule.TestClassHelper_Property;
  21498. begin
  21499. StartProgram(false);
  21500. Add([
  21501. 'type',
  21502. ' TObject = class',
  21503. ' FSize: word;',
  21504. ' function GetSpeed: word;',
  21505. ' procedure SetSpeed(Value: word);',
  21506. ' end;',
  21507. ' TObjHelper = class helper for TObject',
  21508. ' function GetLeft: word;',
  21509. ' procedure SetLeft(Value: word);',
  21510. ' property Size: word read FSize write FSize;',
  21511. ' property Speed: word read GetSpeed write SetSpeed;',
  21512. ' property Left: word read GetLeft write SetLeft;',
  21513. ' end;',
  21514. ' TBird = class',
  21515. ' property NotRight: word read GetLeft write SetLeft;',
  21516. ' procedure DoIt;',
  21517. ' end;',
  21518. 'var',
  21519. ' b: TBird;',
  21520. 'function Tobject.GetSpeed: word;',
  21521. 'begin',
  21522. ' Size:=Size+11;',
  21523. ' Speed:=Speed+12;',
  21524. ' Result:=Left+13;',
  21525. ' Left:=13;',
  21526. ' Left:=Left+13;',
  21527. ' Self.Size:=Self.Size+21;',
  21528. ' Self.Speed:=Self.Speed+22;',
  21529. ' Self.Left:=Self.Left+23;',
  21530. ' with Self do begin',
  21531. ' Size:=Size+31;',
  21532. ' Speed:=Speed+32;',
  21533. ' Left:=Left+33;',
  21534. ' end;',
  21535. 'end;',
  21536. 'procedure Tobject.SetSpeed(Value: word);',
  21537. 'begin',
  21538. 'end;',
  21539. 'function TObjHelper.GetLeft: word;',
  21540. 'begin',
  21541. ' Size:=Size+11;',
  21542. ' Speed:=Speed+12;',
  21543. ' Left:=Left+13;',
  21544. ' Self.Size:=Self.Size+21;',
  21545. ' Self.Speed:=Self.Speed+22;',
  21546. ' Self.Left:=Self.Left+23;',
  21547. ' with Self do begin',
  21548. ' Size:=Size+31;',
  21549. ' Speed:=Speed+32;',
  21550. ' Left:=Left+33;',
  21551. ' end;',
  21552. 'end;',
  21553. 'procedure TObjHelper.SetLeft(Value: word);',
  21554. 'begin',
  21555. 'end;',
  21556. 'procedure TBird.DoIt;',
  21557. 'begin',
  21558. ' NotRight:=NotRight+11;',
  21559. ' Self.NotRight:=Self.NotRight+21;',
  21560. ' with Self do begin',
  21561. ' NotRight:=NotRight+31;',
  21562. ' end;',
  21563. 'end;',
  21564. 'begin',
  21565. ' b.Size:=b.Size+11;',
  21566. ' b.Speed:=b.Speed+12;',
  21567. ' b.Left:=b.Left+13;',
  21568. ' b.NotRight:=b.NotRight+14;',
  21569. ' with b do begin',
  21570. ' Size:=Size+31;',
  21571. ' Speed:=Speed+32;',
  21572. ' Left:=Left+33;',
  21573. ' NotRight:=NotRight+34;',
  21574. ' end;',
  21575. '']);
  21576. ConvertProgram;
  21577. CheckSource('TestClassHelper_Property',
  21578. LinesToStr([ // statements
  21579. 'rtl.createClass(this, "TObject", null, function () {',
  21580. ' this.$init = function () {',
  21581. ' this.FSize = 0;',
  21582. ' };',
  21583. ' this.$final = function () {',
  21584. ' };',
  21585. ' this.GetSpeed = function () {',
  21586. ' var Result = 0;',
  21587. ' this.FSize = this.FSize + 11;',
  21588. ' this.SetSpeed(this.GetSpeed() + 12);',
  21589. ' Result = $mod.TObjHelper.GetLeft.call(this) + 13;',
  21590. ' $mod.TObjHelper.SetLeft.call(this, 13);',
  21591. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  21592. ' this.FSize = this.FSize + 21;',
  21593. ' this.SetSpeed(this.GetSpeed() + 22);',
  21594. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  21595. ' this.FSize = this.FSize + 31;',
  21596. ' this.SetSpeed(this.GetSpeed() + 32);',
  21597. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  21598. ' return Result;',
  21599. ' };',
  21600. ' this.SetSpeed = function (Value) {',
  21601. ' };',
  21602. '});',
  21603. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21604. ' this.GetLeft = function () {',
  21605. ' var Result = 0;',
  21606. ' this.FSize = this.FSize + 11;',
  21607. ' this.SetSpeed(this.GetSpeed() + 12);',
  21608. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  21609. ' this.FSize = this.FSize + 21;',
  21610. ' this.SetSpeed(this.GetSpeed() + 22);',
  21611. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  21612. ' this.FSize = this.FSize + 31;',
  21613. ' this.SetSpeed(this.GetSpeed() + 32);',
  21614. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  21615. ' return Result;',
  21616. ' };',
  21617. ' this.SetLeft = function (Value) {',
  21618. ' };',
  21619. '});',
  21620. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21621. ' this.DoIt = function () {',
  21622. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 11);',
  21623. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 21);',
  21624. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 31);',
  21625. ' };',
  21626. '});',
  21627. 'this.b = null;',
  21628. '']),
  21629. LinesToStr([ // $mod.$main
  21630. '$mod.b.FSize = $mod.b.FSize + 11;',
  21631. '$mod.b.SetSpeed($mod.b.GetSpeed() + 12);',
  21632. '$mod.TObjHelper.SetLeft.call($mod.b, $mod.TObjHelper.GetLeft.call($mod.b) + 13);',
  21633. '$mod.TObjHelper.SetLeft.call($mod.b, $mod.TObjHelper.GetLeft.call($mod.b) + 14);',
  21634. 'var $with = $mod.b;',
  21635. '$with.FSize = $with.FSize + 31;',
  21636. '$with.SetSpeed($with.GetSpeed() + 32);',
  21637. '$mod.TObjHelper.SetLeft.call($with, $mod.TObjHelper.GetLeft.call($with) + 33);',
  21638. '$mod.TObjHelper.SetLeft.call($with, $mod.TObjHelper.GetLeft.call($with) + 34);',
  21639. '']));
  21640. end;
  21641. procedure TTestModule.TestClassHelper_Property_Array;
  21642. begin
  21643. StartProgram(false);
  21644. Add([
  21645. 'type',
  21646. ' TObject = class',
  21647. ' function GetSpeed(Index: boolean): word;',
  21648. ' procedure SetSpeed(Index: boolean; Value: word);',
  21649. ' end;',
  21650. ' TObjHelper = class helper for TObject',
  21651. ' function GetSize(Index: boolean): word;',
  21652. ' procedure SetSize(Index: boolean; Value: word);',
  21653. ' property Size[Index: boolean]: word read GetSize write SetSize;',
  21654. ' property Speed[Index: boolean]: word read GetSpeed write SetSpeed;',
  21655. ' end;',
  21656. ' TBird = class',
  21657. ' property Items[Index: boolean]: word read GetSize write SetSize;',
  21658. ' procedure DoIt;',
  21659. ' end;',
  21660. 'var',
  21661. ' b: TBird;',
  21662. 'function Tobject.GetSpeed(Index: boolean): word;',
  21663. 'begin',
  21664. ' Result:=Size[false];',
  21665. ' Size[true]:=Size[false]+11;',
  21666. ' Speed[true]:=Speed[false]+12;',
  21667. ' Self.Size[true]:=Self.Size[false]+21;',
  21668. ' Self.Speed[true]:=Self.Speed[false]+22;',
  21669. ' with Self do begin',
  21670. ' Size[true]:=Size[false]+31;',
  21671. ' Speed[true]:=Speed[false]+32;',
  21672. ' end;',
  21673. 'end;',
  21674. 'procedure Tobject.SetSpeed(Index: boolean; Value: word);',
  21675. 'begin',
  21676. 'end;',
  21677. 'function TObjHelper.GetSize(Index: boolean): word;',
  21678. 'begin',
  21679. ' Size[true]:=Size[false]+11;',
  21680. ' Speed[true]:=Speed[false]+12;',
  21681. ' Self.Size[true]:=Self.Size[false]+21;',
  21682. ' Self.Speed[true]:=Self.Speed[false]+22;',
  21683. ' with Self do begin',
  21684. ' Size[true]:=Size[false]+31;',
  21685. ' Speed[true]:=Speed[false]+32;',
  21686. ' end;',
  21687. 'end;',
  21688. 'procedure TObjHelper.SetSize(Index: boolean; Value: word);',
  21689. 'begin',
  21690. 'end;',
  21691. 'procedure TBird.DoIt;',
  21692. 'begin',
  21693. ' Items[true]:=Items[false]+11;',
  21694. ' Self.Items[true]:=Self.Items[false]+21;',
  21695. ' with Self do Items[true]:=Items[false]+31;',
  21696. 'end;',
  21697. 'begin',
  21698. ' b.Size[true]:=b.Size[false]+11;',
  21699. ' b.Speed[true]:=b.Speed[false]+12;',
  21700. ' b.Items[true]:=b.Items[false]+13;',
  21701. ' with b do begin',
  21702. ' Size[true]:=Size[false]+21;',
  21703. ' Speed[true]:=Speed[false]+22;',
  21704. ' Items[true]:=Items[false]+23;',
  21705. ' end;',
  21706. '']);
  21707. ConvertProgram;
  21708. CheckSource('TestClassHelper_Property_Array',
  21709. LinesToStr([ // statements
  21710. 'rtl.createClass(this, "TObject", null, function () {',
  21711. ' this.$init = function () {',
  21712. ' };',
  21713. ' this.$final = function () {',
  21714. ' };',
  21715. ' this.GetSpeed = function (Index) {',
  21716. ' var Result = 0;',
  21717. ' Result = $mod.TObjHelper.GetSize.call(this, false);',
  21718. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  21719. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  21720. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  21721. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  21722. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  21723. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  21724. ' return Result;',
  21725. ' };',
  21726. ' this.SetSpeed = function (Index, Value) {',
  21727. ' };',
  21728. '});',
  21729. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21730. ' this.GetSize = function (Index) {',
  21731. ' var Result = 0;',
  21732. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  21733. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  21734. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  21735. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  21736. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  21737. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  21738. ' return Result;',
  21739. ' };',
  21740. ' this.SetSize = function (Index, Value) {',
  21741. ' };',
  21742. '});',
  21743. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21744. ' this.DoIt = function () {',
  21745. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  21746. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  21747. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  21748. ' };',
  21749. '});',
  21750. 'this.b = null;',
  21751. '']),
  21752. LinesToStr([ // $mod.$main
  21753. '$mod.TObjHelper.SetSize.call($mod.b, true, $mod.TObjHelper.GetSize.call($mod.b, false) + 11);',
  21754. '$mod.b.SetSpeed(true, $mod.b.GetSpeed(false) + 12);',
  21755. '$mod.TObjHelper.SetSize.call($mod.b, true, $mod.TObjHelper.GetSize.call($mod.b, false) + 13);',
  21756. 'var $with = $mod.b;',
  21757. '$mod.TObjHelper.SetSize.call($with, true, $mod.TObjHelper.GetSize.call($with, false) + 21);',
  21758. '$with.SetSpeed(true, $with.GetSpeed(false) + 22);',
  21759. '$mod.TObjHelper.SetSize.call($with, true, $mod.TObjHelper.GetSize.call($with, false) + 23);',
  21760. '']));
  21761. end;
  21762. procedure TTestModule.TestClassHelper_Property_Array_Default;
  21763. begin
  21764. StartProgram(false);
  21765. Add([
  21766. 'type',
  21767. ' TObject = class',
  21768. ' function GetSpeed(Index: boolean): word;',
  21769. ' procedure SetSpeed(Index: boolean; Value: word);',
  21770. ' end;',
  21771. ' TObjHelper = class helper for TObject',
  21772. ' property Speed[Index: boolean]: word read GetSpeed write SetSpeed; default;',
  21773. ' end;',
  21774. ' TBird = class',
  21775. ' end;',
  21776. ' TBirdHelper = class helper for TBird',
  21777. ' function GetSize(Index: word): boolean;',
  21778. ' procedure SetSize(Index: word; Value: boolean);',
  21779. ' property Size[Index: word]: boolean read GetSize write SetSize; default;',
  21780. ' end;',
  21781. 'function Tobject.GetSpeed(Index: boolean): word;',
  21782. 'begin',
  21783. ' Self[true]:=Self[false]+1;',
  21784. 'end;',
  21785. 'procedure Tobject.SetSpeed(Index: boolean; Value: word);',
  21786. 'begin',
  21787. 'end;',
  21788. 'function TBirdHelper.GetSize(Index: word): boolean;',
  21789. 'begin',
  21790. ' Self[1]:=not Self[2];',
  21791. 'end;',
  21792. 'procedure TBirdHelper.SetSize(Index: word; Value: boolean);',
  21793. 'begin',
  21794. 'end;',
  21795. 'var',
  21796. ' o: TObject;',
  21797. ' b: TBird;',
  21798. 'begin',
  21799. ' o[true]:=o[false]+1;',
  21800. ' b[3]:=not b[4];',
  21801. '']);
  21802. ConvertProgram;
  21803. CheckSource('TestClassHelper_Property_Array_Default',
  21804. LinesToStr([ // statements
  21805. 'rtl.createClass(this, "TObject", null, function () {',
  21806. ' this.$init = function () {',
  21807. ' };',
  21808. ' this.$final = function () {',
  21809. ' };',
  21810. ' this.GetSpeed = function (Index) {',
  21811. ' var Result = 0;',
  21812. ' this.SetSpeed(true, this.GetSpeed(false) + 1);',
  21813. ' return Result;',
  21814. ' };',
  21815. ' this.SetSpeed = function (Index, Value) {',
  21816. ' };',
  21817. '});',
  21818. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21819. '});',
  21820. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21821. '});',
  21822. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  21823. ' this.GetSize = function (Index) {',
  21824. ' var Result = false;',
  21825. ' $mod.TBirdHelper.SetSize.call(this, 1, !$mod.TBirdHelper.GetSize.call(this, 2));',
  21826. ' return Result;',
  21827. ' };',
  21828. ' this.SetSize = function (Index, Value) {',
  21829. ' };',
  21830. '});',
  21831. 'this.o = null;',
  21832. 'this.b = null;',
  21833. '']),
  21834. LinesToStr([ // $mod.$main
  21835. '$mod.o.SetSpeed(true, $mod.o.GetSpeed(false) + 1);',
  21836. '$mod.TBirdHelper.SetSize.call($mod.b, 3, !$mod.TBirdHelper.GetSize.call($mod.b, 4));',
  21837. '']));
  21838. end;
  21839. procedure TTestModule.TestClassHelper_Property_Array_DefaultDefault;
  21840. begin
  21841. StartProgram(false);
  21842. Add([
  21843. 'type',
  21844. ' TObject = class',
  21845. ' end;',
  21846. ' TObjHelper = class helper for TObject',
  21847. ' function GetItems(Index: word): TObject;',
  21848. ' procedure SetItems(Index: word; Value: TObject);',
  21849. ' property Items[Index: word]: TObject read GetItems write SetItems; default;',
  21850. ' end;',
  21851. 'function Tobjhelper.GetItems(Index: word): TObject;',
  21852. 'begin',
  21853. ' Self[1][2]:=Self[3][4];',
  21854. 'end;',
  21855. 'procedure Tobjhelper.SetItems(Index: word; Value: TObject);',
  21856. 'begin',
  21857. 'end;',
  21858. 'var',
  21859. ' o: TObject;',
  21860. 'begin',
  21861. ' o[1][2]:=o[3][4];',
  21862. '']);
  21863. ConvertProgram;
  21864. CheckSource('TestClassHelper_Property_Array_DefaultDefault',
  21865. LinesToStr([ // statements
  21866. 'rtl.createClass(this, "TObject", null, function () {',
  21867. ' this.$init = function () {',
  21868. ' };',
  21869. ' this.$final = function () {',
  21870. ' };',
  21871. '});',
  21872. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21873. ' this.GetItems = function (Index) {',
  21874. ' var Result = null;',
  21875. ' $mod.TObjHelper.SetItems.call($mod.TObjHelper.GetItems.call(this, 1), 2, $mod.TObjHelper.GetItems.call($mod.TObjHelper.GetItems.call(this, 3), 4));',
  21876. ' return Result;',
  21877. ' };',
  21878. ' this.SetItems = function (Index, Value) {',
  21879. ' };',
  21880. '});',
  21881. 'this.o = null;',
  21882. '']),
  21883. LinesToStr([ // $mod.$main
  21884. '$mod.TObjHelper.SetItems.call($mod.TObjHelper.GetItems.call($mod.o, 1), 2, $mod.TObjHelper.GetItems.call($mod.TObjHelper.GetItems.call($mod.o, 3), 4));',
  21885. '']));
  21886. end;
  21887. procedure TTestModule.TestClassHelper_ClassProperty;
  21888. begin
  21889. StartProgram(false);
  21890. Add([
  21891. 'type',
  21892. ' TObject = class',
  21893. ' class var FSize: word;',
  21894. ' class function GetSpeed: word;',
  21895. ' class procedure SetSpeed(Value: word); virtual; abstract;',
  21896. ' end;',
  21897. ' TObjHelper = class helper for TObject',
  21898. ' class function GetLeft: word;',
  21899. ' class procedure SetLeft(Value: word);',
  21900. ' class property Size: word read FSize write FSize;',
  21901. ' class property Speed: word read GetSpeed write SetSpeed;',
  21902. ' class property Left: word read GetLeft write SetLeft;',
  21903. ' end;',
  21904. ' TBird = class',
  21905. ' class property NotRight: word read GetLeft write SetLeft;',
  21906. ' class procedure DoIt;',
  21907. ' end;',
  21908. ' TBirdClass = class of TBird;',
  21909. 'class function Tobject.GetSpeed: word;',
  21910. 'begin',
  21911. ' Size:=Size+11;',
  21912. ' Speed:=Speed+12;',
  21913. ' Left:=Left+13;',
  21914. ' Self.Size:=Self.Size+21;',
  21915. ' Self.Speed:=Self.Speed+22;',
  21916. ' Self.Left:=Self.Left+23;',
  21917. ' with Self do begin',
  21918. ' Size:=Size+31;',
  21919. ' Speed:=Speed+32;',
  21920. ' Left:=Left+33;',
  21921. ' end;',
  21922. 'end;',
  21923. 'class function TObjHelper.GetLeft: word;',
  21924. 'begin',
  21925. ' Size:=Size+11;',
  21926. ' Speed:=Speed+12;',
  21927. ' Left:=Left+13;',
  21928. ' Self.Size:=Self.Size+21;',
  21929. ' Self.Speed:=Self.Speed+22;',
  21930. ' Self.Left:=Self.Left+23;',
  21931. ' with Self do begin',
  21932. ' Size:=Size+31;',
  21933. ' Speed:=Speed+32;',
  21934. ' Left:=Left+33;',
  21935. ' end;',
  21936. 'end;',
  21937. 'class procedure TObjHelper.SetLeft(Value: word);',
  21938. 'begin',
  21939. 'end;',
  21940. 'class procedure TBird.DoIt;',
  21941. 'begin',
  21942. ' NotRight:=NotRight+11;',
  21943. ' Self.NotRight:=Self.NotRight+21;',
  21944. ' with Self do NotRight:=NotRight+31;',
  21945. 'end;',
  21946. 'var',
  21947. ' b: TBird;',
  21948. ' c: TBirdClass;',
  21949. 'begin',
  21950. ' b.Size:=b.Size+11;',
  21951. ' b.Speed:=b.Speed+12;',
  21952. ' b.Left:=b.Left+13;',
  21953. ' b.NotRight:=b.NotRight+14;',
  21954. ' with b do begin',
  21955. ' Size:=Size+31;',
  21956. ' Speed:=Speed+32;',
  21957. ' Left:=Left+33;',
  21958. ' NotRight:=NotRight+34;',
  21959. ' end;',
  21960. ' c.Size:=c.Size+11;',
  21961. ' c.Speed:=c.Speed+12;',
  21962. ' c.Left:=c.Left+13;',
  21963. ' c.NotRight:=c.NotRight+14;',
  21964. ' with c do begin',
  21965. ' Size:=Size+31;',
  21966. ' Speed:=Speed+32;',
  21967. ' Left:=Left+33;',
  21968. ' NotRight:=NotRight+34;',
  21969. ' end;',
  21970. ' tbird.Size:=tbird.Size+11;',
  21971. ' tbird.Speed:=tbird.Speed+12;',
  21972. ' tbird.Left:=tbird.Left+13;',
  21973. ' tbird.NotRight:=tbird.NotRight+14;',
  21974. ' with tbird do begin',
  21975. ' Size:=Size+31;',
  21976. ' Speed:=Speed+32;',
  21977. ' Left:=Left+33;',
  21978. ' NotRight:=NotRight+34;',
  21979. ' end;',
  21980. '']);
  21981. ConvertProgram;
  21982. CheckSource('TestClassHelper_ClassProperty',
  21983. LinesToStr([ // statements
  21984. 'rtl.createClass(this, "TObject", null, function () {',
  21985. ' this.FSize = 0;',
  21986. ' this.$init = function () {',
  21987. ' };',
  21988. ' this.$final = function () {',
  21989. ' };',
  21990. ' this.GetSpeed = function () {',
  21991. ' var Result = 0;',
  21992. ' $mod.TObject.FSize = this.FSize + 11;',
  21993. ' this.SetSpeed(this.GetSpeed() + 12);',
  21994. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  21995. ' $mod.TObject.FSize = this.FSize + 21;',
  21996. ' this.SetSpeed(this.GetSpeed() + 22);',
  21997. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  21998. ' $mod.TObject.FSize = this.FSize + 31;',
  21999. ' this.SetSpeed(this.GetSpeed() + 32);',
  22000. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  22001. ' return Result;',
  22002. ' };',
  22003. '});',
  22004. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  22005. ' this.GetLeft = function () {',
  22006. ' var Result = 0;',
  22007. ' $mod.TObject.FSize = this.FSize + 11;',
  22008. ' this.SetSpeed(this.GetSpeed() + 12);',
  22009. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  22010. ' $mod.TObject.FSize = this.FSize + 21;',
  22011. ' this.SetSpeed(this.GetSpeed() + 22);',
  22012. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  22013. ' $mod.TObject.FSize = this.FSize + 31;',
  22014. ' this.SetSpeed(this.GetSpeed() + 32);',
  22015. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  22016. ' return Result;',
  22017. ' };',
  22018. ' this.SetLeft = function (Value) {',
  22019. ' };',
  22020. '});',
  22021. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  22022. ' this.DoIt = function () {',
  22023. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 11);',
  22024. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 21);',
  22025. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 31);',
  22026. ' };',
  22027. '});',
  22028. 'this.b = null;',
  22029. 'this.c = null;',
  22030. '']),
  22031. LinesToStr([ // $mod.$main
  22032. '$mod.TObject.FSize = $mod.b.FSize + 11;',
  22033. '$mod.b.$class.SetSpeed($mod.b.$class.GetSpeed() + 12);',
  22034. '$mod.TObjHelper.SetLeft.call($mod.b.$class, $mod.TObjHelper.GetLeft.call($mod.b.$class) + 13);',
  22035. '$mod.TObjHelper.SetLeft.call($mod.b.$class, $mod.TObjHelper.GetLeft.call($mod.b.$class) + 14);',
  22036. 'var $with = $mod.b;',
  22037. '$mod.TObject.FSize = $with.FSize + 31;',
  22038. '$with.$class.SetSpeed($with.$class.GetSpeed() + 32);',
  22039. '$mod.TObjHelper.SetLeft.call($with.$class, $mod.TObjHelper.GetLeft.call($with.$class) + 33);',
  22040. '$mod.TObjHelper.SetLeft.call($with.$class, $mod.TObjHelper.GetLeft.call($with.$class) + 34);',
  22041. '$mod.TObject.FSize = $mod.c.FSize + 11;',
  22042. '$mod.c.SetSpeed($mod.c.GetSpeed() + 12);',
  22043. '$mod.TObjHelper.SetLeft.call($mod.c, $mod.TObjHelper.GetLeft.call($mod.c) + 13);',
  22044. '$mod.TObjHelper.SetLeft.call($mod.c, $mod.TObjHelper.GetLeft.call($mod.c) + 14);',
  22045. 'var $with1 = $mod.c;',
  22046. '$mod.TObject.FSize = $with1.FSize + 31;',
  22047. '$with1.SetSpeed($with1.GetSpeed() + 32);',
  22048. '$mod.TObjHelper.SetLeft.call($with1, $mod.TObjHelper.GetLeft.call($with1) + 33);',
  22049. '$mod.TObjHelper.SetLeft.call($with1, $mod.TObjHelper.GetLeft.call($with1) + 34);',
  22050. '$mod.TObject.FSize = $mod.TBird.FSize + 11;',
  22051. '$mod.TBird.SetSpeed($mod.TBird.GetSpeed() + 12);',
  22052. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 13);',
  22053. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 14);',
  22054. 'var $with2 = $mod.TBird;',
  22055. '$mod.TObject.FSize = $with2.FSize + 31;',
  22056. '$with2.SetSpeed($with2.GetSpeed() + 32);',
  22057. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 33);',
  22058. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 34);',
  22059. '']));
  22060. end;
  22061. procedure TTestModule.TestClassHelper_ClassPropertyStatic;
  22062. begin
  22063. StartProgram(false);
  22064. Add([
  22065. 'type',
  22066. ' TObject = class',
  22067. ' class function GetSpeed: word; static;',
  22068. ' class procedure SetSpeed(Value: word); static;',
  22069. ' end;',
  22070. ' TObjHelper = class helper for TObject',
  22071. ' class function GetLeft: word; static;',
  22072. ' class procedure SetLeft(Value: word); static;',
  22073. ' class property Speed: word read GetSpeed write SetSpeed;',
  22074. ' class property Left: word read GetLeft write SetLeft;',
  22075. ' end;',
  22076. ' TBird = class',
  22077. ' class property NotRight: word read GetLeft write SetLeft;',
  22078. ' class procedure DoIt; static;',
  22079. ' class procedure DoSome;',
  22080. ' end;',
  22081. ' TBirdClass = class of TBird;',
  22082. 'class function Tobject.GetSpeed: word;',
  22083. 'begin',
  22084. ' Speed:=Speed+12;',
  22085. ' Left:=Left+13;',
  22086. 'end;',
  22087. 'class procedure TObject.SetSpeed(Value: word);',
  22088. 'begin',
  22089. 'end;',
  22090. 'class function TObjHelper.GetLeft: word;',
  22091. 'begin',
  22092. ' Speed:=Speed+12;',
  22093. ' Left:=Left+13;',
  22094. 'end;',
  22095. 'class procedure TObjHelper.SetLeft(Value: word);',
  22096. 'begin',
  22097. 'end;',
  22098. 'class procedure TBird.DoIt;',
  22099. 'begin',
  22100. ' NotRight:=NotRight+11;',
  22101. 'end;',
  22102. 'class procedure TBird.DoSome;',
  22103. 'begin',
  22104. ' Speed:=Speed+12;',
  22105. ' Left:=Left+13;',
  22106. ' Self.Speed:=Self.Speed+22;',
  22107. ' Self.Left:=Self.Left+23;',
  22108. ' with Self do begin',
  22109. ' Speed:=Speed+32;',
  22110. ' Left:=Left+33;',
  22111. ' end;',
  22112. ' NotRight:=NotRight+11;',
  22113. ' Self.NotRight:=Self.NotRight+21;',
  22114. ' with Self do NotRight:=NotRight+31;',
  22115. 'end;',
  22116. 'var',
  22117. ' b: TBird;',
  22118. ' c: TBirdClass;',
  22119. 'begin',
  22120. ' b.Speed:=b.Speed+12;',
  22121. ' b.Left:=b.Left+13;',
  22122. ' b.NotRight:=b.NotRight+14;',
  22123. ' with b do begin',
  22124. ' Speed:=Speed+32;',
  22125. ' Left:=Left+33;',
  22126. ' NotRight:=NotRight+34;',
  22127. ' end;',
  22128. ' c.Speed:=c.Speed+12;',
  22129. ' c.Left:=c.Left+13;',
  22130. ' c.NotRight:=c.NotRight+14;',
  22131. ' with c do begin',
  22132. ' Speed:=Speed+32;',
  22133. ' Left:=Left+33;',
  22134. ' NotRight:=NotRight+34;',
  22135. ' end;',
  22136. ' tbird.Speed:=tbird.Speed+12;',
  22137. ' tbird.Left:=tbird.Left+13;',
  22138. ' tbird.NotRight:=tbird.NotRight+14;',
  22139. ' with tbird do begin',
  22140. ' Speed:=Speed+32;',
  22141. ' Left:=Left+33;',
  22142. ' NotRight:=NotRight+34;',
  22143. ' end;',
  22144. '']);
  22145. ConvertProgram;
  22146. CheckSource('TestClassHelper_ClassPropertyStatic',
  22147. LinesToStr([ // statements
  22148. 'rtl.createClass(this, "TObject", null, function () {',
  22149. ' this.$init = function () {',
  22150. ' };',
  22151. ' this.$final = function () {',
  22152. ' };',
  22153. ' this.GetSpeed = function () {',
  22154. ' var Result = 0;',
  22155. ' $mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  22156. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  22157. ' return Result;',
  22158. ' };',
  22159. ' this.SetSpeed = function (Value) {',
  22160. ' };',
  22161. '});',
  22162. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  22163. ' this.GetLeft = function () {',
  22164. ' var Result = 0;',
  22165. ' $mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  22166. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  22167. ' return Result;',
  22168. ' };',
  22169. ' this.SetLeft = function (Value) {',
  22170. ' };',
  22171. '});',
  22172. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  22173. ' this.DoIt = function () {',
  22174. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 11);',
  22175. ' };',
  22176. ' this.DoSome = function () {',
  22177. ' this.SetSpeed(this.GetSpeed() + 12);',
  22178. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  22179. ' this.SetSpeed(this.GetSpeed() + 22);',
  22180. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 23);',
  22181. ' this.SetSpeed(this.GetSpeed() + 32);',
  22182. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  22183. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 11);',
  22184. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 21);',
  22185. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 31);',
  22186. ' };',
  22187. '});',
  22188. 'this.b = null;',
  22189. 'this.c = null;',
  22190. '']),
  22191. LinesToStr([ // $mod.$main
  22192. '$mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  22193. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  22194. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  22195. 'var $with = $mod.b;',
  22196. '$with.SetSpeed($with.GetSpeed() + 32);',
  22197. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  22198. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  22199. '$mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  22200. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  22201. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  22202. 'var $with1 = $mod.c;',
  22203. '$with1.SetSpeed($with1.GetSpeed() + 32);',
  22204. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  22205. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  22206. '$mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  22207. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  22208. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  22209. 'var $with2 = $mod.TBird;',
  22210. '$with2.SetSpeed($with2.GetSpeed() + 32);',
  22211. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  22212. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  22213. '']));
  22214. end;
  22215. procedure TTestModule.TestClassHelper_ClassProperty_Array;
  22216. begin
  22217. StartProgram(false);
  22218. Add([
  22219. 'type',
  22220. ' TObject = class',
  22221. ' class function GetSpeed(Index: boolean): word;',
  22222. ' class procedure SetSpeed(Index: boolean; Value: word); virtual; abstract;',
  22223. ' end;',
  22224. ' TObjHelper = class helper for TObject',
  22225. ' class function GetSize(Index: boolean): word;',
  22226. ' class procedure SetSize(Index: boolean; Value: word);',
  22227. ' class property Size[Index: boolean]: word read GetSize write SetSize;',
  22228. ' class property Speed[Index: boolean]: word read GetSpeed write SetSpeed;',
  22229. ' end;',
  22230. ' TBird = class',
  22231. ' class property Items[Index: boolean]: word read GetSize write SetSize;',
  22232. ' class procedure DoIt;',
  22233. ' end;',
  22234. ' TBirdClass = class of TBird;',
  22235. 'class function Tobject.GetSpeed(Index: boolean): word;',
  22236. 'begin',
  22237. ' Size[true]:=Size[false]+11;',
  22238. ' Speed[true]:=Speed[false]+12;',
  22239. ' Self.Size[true]:=Self.Size[false]+21;',
  22240. ' Self.Speed[true]:=Self.Speed[false]+22;',
  22241. ' with Self do begin',
  22242. ' Size[true]:=Size[false]+31;',
  22243. ' Speed[true]:=Speed[false]+32;',
  22244. ' end;',
  22245. 'end;',
  22246. 'class function TObjHelper.GetSize(Index: boolean): word;',
  22247. 'begin',
  22248. ' Size[true]:=Size[false]+11;',
  22249. ' Speed[true]:=Speed[false]+12;',
  22250. ' Self.Size[true]:=Self.Size[false]+21;',
  22251. ' Self.Speed[true]:=Self.Speed[false]+22;',
  22252. ' with Self do begin',
  22253. ' Size[true]:=Size[false]+31;',
  22254. ' Speed[true]:=Speed[false]+32;',
  22255. ' end;',
  22256. 'end;',
  22257. 'class procedure TObjHelper.SetSize(Index: boolean; Value: word);',
  22258. 'begin',
  22259. 'end;',
  22260. 'class procedure TBird.DoIt;',
  22261. 'begin',
  22262. ' Items[true]:=Items[false]+11;',
  22263. ' Self.Items[true]:=Self.Items[false]+21;',
  22264. ' with Self do Items[true]:=Items[false]+31;',
  22265. 'end;',
  22266. 'var',
  22267. ' b: TBird;',
  22268. ' c: TBirdClass;',
  22269. 'begin',
  22270. ' b.Size[true]:=b.Size[false]+11;',
  22271. ' b.Speed[true]:=b.Speed[false]+12;',
  22272. ' b.Items[true]:=b.Items[false]+13;',
  22273. ' with b do begin',
  22274. ' Size[true]:=Size[false]+21;',
  22275. ' Speed[true]:=Speed[false]+22;',
  22276. ' Items[true]:=Items[false]+23;',
  22277. ' end;',
  22278. ' c.Size[true]:=c.Size[false]+11;',
  22279. ' c.Speed[true]:=c.Speed[false]+12;',
  22280. ' c.Items[true]:=c.Items[false]+13;',
  22281. ' with c do begin',
  22282. ' Size[true]:=Size[false]+21;',
  22283. ' Speed[true]:=Speed[false]+22;',
  22284. ' Items[true]:=Items[false]+23;',
  22285. ' end;',
  22286. ' TBird.Size[true]:=TBird.Size[false]+11;',
  22287. ' TBird.Speed[true]:=TBird.Speed[false]+12;',
  22288. ' TBird.Items[true]:=TBird.Items[false]+13;',
  22289. ' with TBird do begin',
  22290. ' Size[true]:=Size[false]+21;',
  22291. ' Speed[true]:=Speed[false]+22;',
  22292. ' Items[true]:=Items[false]+23;',
  22293. ' end;',
  22294. '']);
  22295. ConvertProgram;
  22296. CheckSource('TestClassHelper_ClassProperty_Array',
  22297. LinesToStr([ // statements
  22298. 'rtl.createClass(this, "TObject", null, function () {',
  22299. ' this.$init = function () {',
  22300. ' };',
  22301. ' this.$final = function () {',
  22302. ' };',
  22303. ' this.GetSpeed = function (Index) {',
  22304. ' var Result = 0;',
  22305. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  22306. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  22307. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  22308. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  22309. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  22310. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  22311. ' return Result;',
  22312. ' };',
  22313. '});',
  22314. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  22315. ' this.GetSize = function (Index) {',
  22316. ' var Result = 0;',
  22317. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  22318. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  22319. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  22320. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  22321. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  22322. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  22323. ' return Result;',
  22324. ' };',
  22325. ' this.SetSize = function (Index, Value) {',
  22326. ' };',
  22327. '});',
  22328. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  22329. ' this.DoIt = function () {',
  22330. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  22331. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  22332. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  22333. ' };',
  22334. '});',
  22335. 'this.b = null;',
  22336. 'this.c = null;',
  22337. '']),
  22338. LinesToStr([ // $mod.$main
  22339. '$mod.TObjHelper.SetSize.call($mod.b.$class, true, $mod.TObjHelper.GetSize.call($mod.b.$class, false) + 11);',
  22340. '$mod.b.$class.SetSpeed(true, $mod.b.$class.GetSpeed(false) + 12);',
  22341. '$mod.TObjHelper.SetSize.call($mod.b.$class, true, $mod.TObjHelper.GetSize.call($mod.b.$class, false) + 13);',
  22342. 'var $with = $mod.b;',
  22343. '$mod.TObjHelper.SetSize.call($with.$class, true, $mod.TObjHelper.GetSize.call($with.$class, false) + 21);',
  22344. '$with.$class.SetSpeed(true, $with.$class.GetSpeed(false) + 22);',
  22345. '$mod.TObjHelper.SetSize.call($with.$class, true, $mod.TObjHelper.GetSize.call($with.$class, false) + 23);',
  22346. '$mod.TObjHelper.SetSize.call($mod.c, true, $mod.TObjHelper.GetSize.call($mod.c, false) + 11);',
  22347. '$mod.c.SetSpeed(true, $mod.c.GetSpeed(false) + 12);',
  22348. '$mod.TObjHelper.SetSize.call($mod.c, true, $mod.TObjHelper.GetSize.call($mod.c, false) + 13);',
  22349. 'var $with1 = $mod.c;',
  22350. '$mod.TObjHelper.SetSize.call($with1, true, $mod.TObjHelper.GetSize.call($with1, false) + 21);',
  22351. '$with1.SetSpeed(true, $with1.GetSpeed(false) + 22);',
  22352. '$mod.TObjHelper.SetSize.call($with1, true, $mod.TObjHelper.GetSize.call($with1, false) + 23);',
  22353. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 11);',
  22354. '$mod.TBird.SetSpeed(true, $mod.TBird.GetSpeed(false) + 12);',
  22355. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 13);',
  22356. 'var $with2 = $mod.TBird;',
  22357. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 21);',
  22358. '$with2.SetSpeed(true, $with2.GetSpeed(false) + 22);',
  22359. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 23);',
  22360. '']));
  22361. end;
  22362. procedure TTestModule.TestClassHelper_ForIn;
  22363. begin
  22364. StartProgram(false);
  22365. Add([
  22366. 'type',
  22367. ' TObject = class end;',
  22368. ' TItem = TObject;',
  22369. ' TEnumerator = class',
  22370. ' FCurrent: TItem;',
  22371. ' property Current: TItem read FCurrent;',
  22372. ' function MoveNext: boolean;',
  22373. ' end;',
  22374. ' TBird = class',
  22375. ' end;',
  22376. ' TBirdHelper = class helper for TBird',
  22377. ' function GetEnumerator: TEnumerator;',
  22378. ' end;',
  22379. 'function TEnumerator.MoveNext: boolean;',
  22380. 'begin',
  22381. 'end;',
  22382. 'function TBirdHelper.GetEnumerator: TEnumerator;',
  22383. 'begin',
  22384. 'end;',
  22385. 'var',
  22386. ' b: TBird;',
  22387. ' i, i2: TItem;',
  22388. 'begin',
  22389. ' for i in b do i2:=i;']);
  22390. ConvertProgram;
  22391. CheckSource('TestClassHelper_ForIn',
  22392. LinesToStr([ // statements
  22393. 'rtl.createClass(this, "TObject", null, function () {',
  22394. ' this.$init = function () {',
  22395. ' };',
  22396. ' this.$final = function () {',
  22397. ' };',
  22398. '});',
  22399. 'rtl.createClass(this, "TEnumerator", this.TObject, function () {',
  22400. ' this.$init = function () {',
  22401. ' $mod.TObject.$init.call(this);',
  22402. ' this.FCurrent = null;',
  22403. ' };',
  22404. ' this.$final = function () {',
  22405. ' this.FCurrent = undefined;',
  22406. ' $mod.TObject.$final.call(this);',
  22407. ' };',
  22408. ' this.MoveNext = function () {',
  22409. ' var Result = false;',
  22410. ' return Result;',
  22411. ' };',
  22412. '});',
  22413. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  22414. '});',
  22415. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  22416. ' this.GetEnumerator = function () {',
  22417. ' var Result = null;',
  22418. ' return Result;',
  22419. ' };',
  22420. '});',
  22421. 'this.b = null;',
  22422. 'this.i = null;',
  22423. 'this.i2 = null;'
  22424. ]),
  22425. LinesToStr([ // $mod.$main
  22426. 'var $in = $mod.TBirdHelper.GetEnumerator.call($mod.b);',
  22427. 'try {',
  22428. ' while ($in.MoveNext()){',
  22429. ' $mod.i = $in.FCurrent;',
  22430. ' $mod.i2 = $mod.i;',
  22431. ' }',
  22432. '} finally {',
  22433. ' $in = rtl.freeLoc($in)',
  22434. '};',
  22435. '']));
  22436. end;
  22437. procedure TTestModule.TestClassHelper_PassProperty;
  22438. begin
  22439. StartProgram(false);
  22440. Add([
  22441. 'type',
  22442. ' TObject = class',
  22443. ' FField: TObject;',
  22444. ' property Field: TObject read FField write FField;',
  22445. ' end;',
  22446. ' THelper = class helper for TObject',
  22447. ' procedure Fly;',
  22448. ' class procedure Run;',
  22449. ' class procedure Jump; static;',
  22450. ' end;',
  22451. 'procedure THelper.Fly;',
  22452. 'begin',
  22453. ' Field.Fly;',
  22454. ' Field.Run;',
  22455. ' Field.Jump;',
  22456. ' with Field do begin',
  22457. ' Fly;',
  22458. ' Run;',
  22459. ' Jump;',
  22460. ' end;',
  22461. 'end;',
  22462. 'class procedure THelper.Run;',
  22463. 'begin',
  22464. 'end;',
  22465. 'class procedure THelper.Jump;',
  22466. 'begin',
  22467. 'end;',
  22468. 'var',
  22469. ' b: TObject;',
  22470. 'begin',
  22471. ' b.Field.Fly;',
  22472. ' b.Field.Run;',
  22473. ' b.Field.Jump;',
  22474. ' with b do begin',
  22475. ' Field.Run;',
  22476. ' Field.Fly;',
  22477. ' Field.Jump;',
  22478. ' end;',
  22479. ' with b.Field do begin',
  22480. ' Run;',
  22481. ' Fly;',
  22482. ' Jump;',
  22483. ' end;',
  22484. '']);
  22485. ConvertProgram;
  22486. CheckSource('TestClassHelper_PassProperty',
  22487. LinesToStr([ // statements
  22488. 'rtl.createClass(this, "TObject", null, function () {',
  22489. ' this.$init = function () {',
  22490. ' this.FField = null;',
  22491. ' };',
  22492. ' this.$final = function () {',
  22493. ' this.FField = undefined;',
  22494. ' };',
  22495. '});',
  22496. 'rtl.createHelper(this, "THelper", null, function () {',
  22497. ' this.Fly = function () {',
  22498. ' $mod.THelper.Fly.call(this.FField);',
  22499. ' $mod.THelper.Run.call(this.FField.$class);',
  22500. ' $mod.THelper.Jump();',
  22501. ' var $with = this.FField;',
  22502. ' $mod.THelper.Fly.call($with);',
  22503. ' $mod.THelper.Run.call($with.$class);',
  22504. ' $mod.THelper.Jump();',
  22505. ' };',
  22506. ' this.Run = function () {',
  22507. ' };',
  22508. ' this.Jump = function () {',
  22509. ' };',
  22510. '});',
  22511. 'this.b = null;',
  22512. '']),
  22513. LinesToStr([ // $mod.$main
  22514. '$mod.THelper.Fly.call($mod.b.FField);',
  22515. '$mod.THelper.Run.call($mod.b.FField.$class);',
  22516. '$mod.THelper.Jump();',
  22517. 'var $with = $mod.b;',
  22518. '$mod.THelper.Run.call($with.FField.$class);',
  22519. '$mod.THelper.Fly.call($with.FField);',
  22520. '$mod.THelper.Jump();',
  22521. 'var $with1 = $mod.b.FField;',
  22522. '$mod.THelper.Run.call($with1.$class);',
  22523. '$mod.THelper.Fly.call($with1);',
  22524. '$mod.THelper.Jump();',
  22525. '']));
  22526. end;
  22527. procedure TTestModule.TestExtClassHelper_ClassVar;
  22528. begin
  22529. StartProgram(false);
  22530. Add([
  22531. '{$modeswitch externalclass}',
  22532. 'type',
  22533. ' TExtA = class external name ''ExtObj''',
  22534. ' end;',
  22535. ' THelper = class helper for TExtA',
  22536. ' const',
  22537. ' One = 1;',
  22538. ' Two: word = 2;',
  22539. ' class var',
  22540. ' Glob: word;',
  22541. ' function Foo(w: word): word;',
  22542. ' class function Bar(w: word): word; static;',
  22543. ' end;',
  22544. 'function THelper.foo(w: word): word;',
  22545. 'begin',
  22546. ' Result:=w;',
  22547. ' Two:=One+w;',
  22548. ' Glob:=Glob;',
  22549. ' Result:=Self.Glob;',
  22550. ' Self.Glob:=Self.Glob;',
  22551. ' with Self do Glob:=Glob;',
  22552. 'end;',
  22553. 'class function THelper.bar(w: word): word;',
  22554. 'begin',
  22555. ' Result:=w;',
  22556. ' Two:=One;',
  22557. ' Glob:=Glob;',
  22558. 'end;',
  22559. 'var o: TExtA;',
  22560. 'begin',
  22561. ' texta.two:=texta.one;',
  22562. ' texta.Glob:=texta.Glob;',
  22563. ' with texta do begin',
  22564. ' two:=one;',
  22565. ' Glob:=Glob;',
  22566. ' end;',
  22567. ' o.two:=o.one;',
  22568. ' o.Glob:=o.Glob;',
  22569. ' with o do begin',
  22570. ' two:=one;',
  22571. ' Glob:=Glob;',
  22572. ' end;',
  22573. '']);
  22574. ConvertProgram;
  22575. CheckSource('TestExtClassHelper_ClassVar',
  22576. LinesToStr([ // statements
  22577. 'rtl.createHelper(this, "THelper", null, function () {',
  22578. ' this.One = 1;',
  22579. ' this.Two = 2;',
  22580. ' this.Glob = 0;',
  22581. ' this.Foo = function (w) {',
  22582. ' var Result = 0;',
  22583. ' Result = w;',
  22584. ' $mod.THelper.Two = 1 + w;',
  22585. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22586. ' Result = $mod.THelper.Glob;',
  22587. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22588. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22589. ' return Result;',
  22590. ' };',
  22591. ' this.Bar = function (w) {',
  22592. ' var Result = 0;',
  22593. ' Result = w;',
  22594. ' $mod.THelper.Two = 1;',
  22595. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22596. ' return Result;',
  22597. ' };',
  22598. '});',
  22599. 'this.o = null;',
  22600. '']),
  22601. LinesToStr([ // $mod.$main
  22602. '$mod.THelper.Two = 1;',
  22603. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22604. '$mod.THelper.Two = 1;',
  22605. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22606. '$mod.THelper.Two = 1;',
  22607. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22608. 'var $with = $mod.o;',
  22609. '$mod.THelper.Two = 1;',
  22610. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22611. '']));
  22612. end;
  22613. procedure TTestModule.TestExtClassHelper_Method_Call;
  22614. begin
  22615. StartProgram(false);
  22616. Add([
  22617. '{$modeswitch externalclass}',
  22618. 'type',
  22619. ' TFly = function(w: word): word of object;',
  22620. ' TExtA = class external name ''ExtObj''',
  22621. ' procedure Run(w: word = 10);',
  22622. ' end;',
  22623. ' THelper = class helper for TExtA',
  22624. ' function Foo(w: word = 1): word;',
  22625. ' function Fly(w: word = 2): word; external name ''Fly'';',
  22626. ' end;',
  22627. 'var p: TFly;',
  22628. 'function THelper.foo(w: word): word;',
  22629. 'begin',
  22630. ' Run;',
  22631. ' Run();',
  22632. ' Run(11);',
  22633. ' Foo;',
  22634. ' Foo();',
  22635. ' Foo(12);',
  22636. ' Self.Foo;',
  22637. ' Self.Foo();',
  22638. ' Self.Foo(13);',
  22639. ' Fly;',
  22640. ' Fly();',
  22641. ' with Self do begin',
  22642. ' Foo;',
  22643. ' Foo();',
  22644. ' Foo(14);',
  22645. ' Fly;',
  22646. ' Fly();',
  22647. ' end;',
  22648. ' p:=@Fly;',
  22649. 'end;',
  22650. 'var Obj: TExtA;',
  22651. 'begin',
  22652. ' obj.Foo;',
  22653. ' obj.Foo();',
  22654. ' obj.Foo(21);',
  22655. ' obj.Fly;',
  22656. ' obj.Fly();',
  22657. ' with obj do begin',
  22658. ' Foo;',
  22659. ' Foo();',
  22660. ' Foo(22);',
  22661. ' Fly;',
  22662. ' Fly();',
  22663. ' end;',
  22664. ' p:[email protected];',
  22665. '']);
  22666. ConvertProgram;
  22667. CheckSource('TestExtClassHelper_Method_Call',
  22668. LinesToStr([ // statements
  22669. 'rtl.createHelper(this, "THelper", null, function () {',
  22670. ' this.Foo = function (w) {',
  22671. ' var Result = 0;',
  22672. ' this.Run(10);',
  22673. ' this.Run(10);',
  22674. ' this.Run(11);',
  22675. ' $mod.THelper.Foo.call(this, 1);',
  22676. ' $mod.THelper.Foo.call(this, 1);',
  22677. ' $mod.THelper.Foo.call(this, 12);',
  22678. ' $mod.THelper.Foo.call(this, 1);',
  22679. ' $mod.THelper.Foo.call(this, 1);',
  22680. ' $mod.THelper.Foo.call(this, 13);',
  22681. ' this.Fly(2);',
  22682. ' this.Fly(2);',
  22683. ' $mod.THelper.Foo.call(this, 1);',
  22684. ' $mod.THelper.Foo.call(this, 1);',
  22685. ' $mod.THelper.Foo.call(this, 14);',
  22686. ' this.Fly(2);',
  22687. ' this.Fly(2);',
  22688. ' $mod.p = rtl.createCallback(this, "Fly");',
  22689. ' return Result;',
  22690. ' };',
  22691. '});',
  22692. 'this.p = null;',
  22693. 'this.Obj = null;',
  22694. '']),
  22695. LinesToStr([ // $mod.$main
  22696. '$mod.THelper.Foo.call($mod.Obj, 1);',
  22697. '$mod.THelper.Foo.call($mod.Obj, 1);',
  22698. '$mod.THelper.Foo.call($mod.Obj, 21);',
  22699. '$mod.Obj.Fly(2);',
  22700. '$mod.Obj.Fly(2);',
  22701. 'var $with = $mod.Obj;',
  22702. '$mod.THelper.Foo.call($with, 1);',
  22703. '$mod.THelper.Foo.call($with, 1);',
  22704. '$mod.THelper.Foo.call($with, 22);',
  22705. '$with.Fly(2);',
  22706. '$with.Fly(2);',
  22707. '$mod.p = rtl.createCallback($mod.Obj, "Fly");',
  22708. '']));
  22709. end;
  22710. procedure TTestModule.TestExtClassHelper_ClassMethod_MissingStatic;
  22711. begin
  22712. StartProgram(false);
  22713. Add([
  22714. '{$modeswitch externalclass}',
  22715. 'type',
  22716. ' TExtA = class external name ''ExtObj''',
  22717. ' procedure Run(w: word = 10);',
  22718. ' end;',
  22719. ' THelper = class helper for TExtA',
  22720. ' class procedure Fly;',
  22721. ' end;',
  22722. 'class procedure THelper.Fly;',
  22723. 'begin end;',
  22724. 'begin',
  22725. '']);
  22726. SetExpectedPasResolverError(sHelperClassMethodForExtClassMustBeStatic,
  22727. nHelperClassMethodForExtClassMustBeStatic);
  22728. ConvertProgram;
  22729. end;
  22730. procedure TTestModule.TestRecordHelper_ClassVar;
  22731. begin
  22732. StartProgram(false);
  22733. Add([
  22734. 'type',
  22735. ' TRec = record',
  22736. ' end;',
  22737. ' THelper = record helper for TRec',
  22738. ' const',
  22739. ' One = 1;',
  22740. ' Two: word = 2;',
  22741. ' class var',
  22742. ' Glob: word;',
  22743. ' function Foo(w: word): word;',
  22744. ' class function Bar(w: word): word; static;',
  22745. ' end;',
  22746. 'function THelper.foo(w: word): word;',
  22747. 'begin',
  22748. ' Result:=w;',
  22749. ' Two:=One+w;',
  22750. ' Glob:=Glob;',
  22751. ' Result:=Self.Glob;',
  22752. ' Self.Glob:=Self.Glob;',
  22753. ' with Self do Glob:=Glob;',
  22754. ' Self:=Self;',
  22755. 'end;',
  22756. 'class function THelper.bar(w: word): word;',
  22757. 'begin',
  22758. ' Result:=w;',
  22759. ' Two:=One;',
  22760. ' Glob:=Glob;',
  22761. 'end;',
  22762. 'var r: TRec;',
  22763. 'begin',
  22764. ' trec.two:=trec.one;',
  22765. ' trec.Glob:=trec.Glob;',
  22766. ' with trec do begin',
  22767. ' two:=one;',
  22768. ' Glob:=Glob;',
  22769. ' end;',
  22770. ' r.two:=r.one;',
  22771. ' r.Glob:=r.Glob;',
  22772. ' with r do begin',
  22773. ' two:=one;',
  22774. ' Glob:=Glob;',
  22775. ' end;',
  22776. '']);
  22777. ConvertProgram;
  22778. CheckSource('TestRecordHelper_ClassVar',
  22779. LinesToStr([ // statements
  22780. 'rtl.recNewT(this, "TRec", function () {',
  22781. ' this.$eq = function (b) {',
  22782. ' return true;',
  22783. ' };',
  22784. ' this.$assign = function (s) {',
  22785. ' return this;',
  22786. ' };',
  22787. '});',
  22788. 'rtl.createHelper(this, "THelper", null, function () {',
  22789. ' this.One = 1;',
  22790. ' this.Two = 2;',
  22791. ' this.Glob = 0;',
  22792. ' this.Foo = function (w) {',
  22793. ' var Result = 0;',
  22794. ' Result = w;',
  22795. ' $mod.THelper.Two = 1 + w;',
  22796. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22797. ' Result = $mod.THelper.Glob;',
  22798. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22799. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22800. ' this.$assign(this);',
  22801. ' return Result;',
  22802. ' };',
  22803. ' this.Bar = function (w) {',
  22804. ' var Result = 0;',
  22805. ' Result = w;',
  22806. ' $mod.THelper.Two = 1;',
  22807. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22808. ' return Result;',
  22809. ' };',
  22810. '});',
  22811. 'this.r = this.TRec.$new();',
  22812. '']),
  22813. LinesToStr([ // $mod.$main
  22814. '$mod.THelper.Two = 1;',
  22815. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22816. 'var $with = $mod.TRec;',
  22817. '$mod.THelper.Two = 1;',
  22818. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22819. '$mod.THelper.Two = 1;',
  22820. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22821. 'var $with1 = $mod.r;',
  22822. '$mod.THelper.Two = 1;',
  22823. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22824. '']));
  22825. end;
  22826. procedure TTestModule.TestRecordHelper_Method_Call;
  22827. begin
  22828. StartProgram(false);
  22829. Add([
  22830. '{$modeswitch AdvancedRecords}',
  22831. 'type',
  22832. ' TRec = record',
  22833. ' procedure Run(w: word = 10);',
  22834. ' end;',
  22835. ' THelper = record helper for TRec',
  22836. ' function Foo(w: word = 1): word;',
  22837. ' end;',
  22838. 'procedure TRec.Run(w: word);',
  22839. 'begin',
  22840. ' Foo;',
  22841. ' Foo();',
  22842. ' Foo(2);',
  22843. ' Self.Foo;',
  22844. ' Self.Foo();',
  22845. ' Self.Foo(3);',
  22846. ' with Self do begin',
  22847. ' Foo;',
  22848. ' Foo();',
  22849. ' Foo(4);',
  22850. ' end;',
  22851. 'end;',
  22852. 'function THelper.foo(w: word): word;',
  22853. 'begin',
  22854. ' Run;',
  22855. ' Run();',
  22856. ' Run(11);',
  22857. ' Foo;',
  22858. ' Foo();',
  22859. ' Foo(12);',
  22860. ' Self.Foo;',
  22861. ' Self.Foo();',
  22862. ' Self.Foo(13);',
  22863. ' with Self do begin',
  22864. ' Foo;',
  22865. ' Foo();',
  22866. ' Foo(14);',
  22867. ' end;',
  22868. 'end;',
  22869. 'var Rec: TRec;',
  22870. 'begin',
  22871. ' Rec.Foo;',
  22872. ' Rec.Foo();',
  22873. ' Rec.Foo(21);',
  22874. ' with Rec do begin',
  22875. ' Foo;',
  22876. ' Foo();',
  22877. ' Foo(22);',
  22878. ' end;',
  22879. '']);
  22880. ConvertProgram;
  22881. CheckSource('TestRecordHelper_Method_Call',
  22882. LinesToStr([ // statements
  22883. 'rtl.recNewT(this, "TRec", function () {',
  22884. ' this.$eq = function (b) {',
  22885. ' return true;',
  22886. ' };',
  22887. ' this.$assign = function (s) {',
  22888. ' return this;',
  22889. ' };',
  22890. ' this.Run = function (w) {',
  22891. ' $mod.THelper.Foo.call(this, 1);',
  22892. ' $mod.THelper.Foo.call(this, 1);',
  22893. ' $mod.THelper.Foo.call(this, 2);',
  22894. ' $mod.THelper.Foo.call(this, 1);',
  22895. ' $mod.THelper.Foo.call(this, 1);',
  22896. ' $mod.THelper.Foo.call(this, 3);',
  22897. ' $mod.THelper.Foo.call(this, 1);',
  22898. ' $mod.THelper.Foo.call(this, 1);',
  22899. ' $mod.THelper.Foo.call(this, 4);',
  22900. ' };',
  22901. '});',
  22902. 'rtl.createHelper(this, "THelper", null, function () {',
  22903. ' this.Foo = function (w) {',
  22904. ' var Result = 0;',
  22905. ' this.Run(10);',
  22906. ' this.Run(10);',
  22907. ' this.Run(11);',
  22908. ' $mod.THelper.Foo.call(this, 1);',
  22909. ' $mod.THelper.Foo.call(this, 1);',
  22910. ' $mod.THelper.Foo.call(this, 12);',
  22911. ' $mod.THelper.Foo.call(this, 1);',
  22912. ' $mod.THelper.Foo.call(this, 1);',
  22913. ' $mod.THelper.Foo.call(this, 13);',
  22914. ' $mod.THelper.Foo.call(this, 1);',
  22915. ' $mod.THelper.Foo.call(this, 1);',
  22916. ' $mod.THelper.Foo.call(this, 14);',
  22917. ' return Result;',
  22918. ' };',
  22919. '});',
  22920. 'this.Rec = this.TRec.$new();',
  22921. '']),
  22922. LinesToStr([ // $mod.$main
  22923. '$mod.THelper.Foo.call($mod.Rec, 1);',
  22924. '$mod.THelper.Foo.call($mod.Rec, 1);',
  22925. '$mod.THelper.Foo.call($mod.Rec, 21);',
  22926. 'var $with = $mod.Rec;',
  22927. '$mod.THelper.Foo.call($with, 1);',
  22928. '$mod.THelper.Foo.call($with, 1);',
  22929. '$mod.THelper.Foo.call($with, 22);',
  22930. '']));
  22931. end;
  22932. procedure TTestModule.TestRecordHelper_Constructor;
  22933. begin
  22934. StartProgram(false);
  22935. Add([
  22936. '{$modeswitch AdvancedRecords}',
  22937. 'type',
  22938. ' TRec = record',
  22939. ' constructor Create(w: word);',
  22940. ' end;',
  22941. ' THelper = record helper for TRec',
  22942. ' constructor NewHlp(w: word);',
  22943. ' end;',
  22944. 'var',
  22945. ' Rec: TRec;',
  22946. 'constructor TRec.Create(w: word);',
  22947. 'begin',
  22948. ' NewHlp(2);', // normal call
  22949. ' trec.NewHlp(3);', // new instance
  22950. 'end;',
  22951. 'constructor THelper.NewHlp(w: word);',
  22952. 'begin',
  22953. ' create(2);', // normal call
  22954. ' trec.create(3);', // new instance
  22955. ' NewHlp(4);', // normal call
  22956. ' trec.NewHlp(5);', // new instance
  22957. 'end;',
  22958. 'begin',
  22959. ' rec.newhlp(2);', // normal call
  22960. ' with rec do newhlp(12);', // normal call
  22961. ' trec.newhlp(3);', // new instance
  22962. ' with trec do newhlp(13);', // new instance
  22963. '']);
  22964. ConvertProgram;
  22965. CheckSource('TestRecordHelper_Constructor',
  22966. LinesToStr([ // statements
  22967. 'rtl.recNewT(this, "TRec", function () {',
  22968. ' this.$eq = function (b) {',
  22969. ' return true;',
  22970. ' };',
  22971. ' this.$assign = function (s) {',
  22972. ' return this;',
  22973. ' };',
  22974. ' this.Create = function (w) {',
  22975. ' $mod.THelper.NewHlp.call(this, 2);',
  22976. ' $mod.THelper.$new("NewHlp", [3]);',
  22977. ' return this;',
  22978. ' };',
  22979. '});',
  22980. 'rtl.createHelper(this, "THelper", null, function () {',
  22981. ' this.NewHlp = function (w) {',
  22982. ' this.Create(2);',
  22983. ' $mod.TRec.$new().Create(3);',
  22984. ' $mod.THelper.NewHlp.call(this, 4);',
  22985. ' $mod.THelper.$new("NewHlp", [5]);',
  22986. ' return this;',
  22987. ' };',
  22988. ' this.$new = function (fn, args) {',
  22989. ' return this[fn].apply($mod.TRec.$new(), args);',
  22990. ' };',
  22991. '});',
  22992. 'this.Rec = this.TRec.$new();',
  22993. '']),
  22994. LinesToStr([ // $mod.$main
  22995. '$mod.THelper.NewHlp.call($mod.Rec, 2);',
  22996. 'var $with = $mod.Rec;',
  22997. '$mod.THelper.NewHlp.call($with, 12);',
  22998. '$mod.THelper.$new("NewHlp", [3]);',
  22999. 'var $with1 = $mod.TRec;',
  23000. '$mod.THelper.$new("NewHlp", [13]);',
  23001. '']));
  23002. end;
  23003. procedure TTestModule.TestTypeHelper_ClassVar;
  23004. begin
  23005. StartProgram(false);
  23006. Add([
  23007. '{$modeswitch typehelpers}',
  23008. 'type',
  23009. ' THelper = type helper for byte',
  23010. ' const',
  23011. ' One = 1;',
  23012. ' Two: word = 2;',
  23013. ' class var',
  23014. ' Glob: word;',
  23015. ' function Foo(w: word): word;',
  23016. ' class function Bar(w: word): word; static;',
  23017. ' end;',
  23018. 'function THelper.foo(w: word): word;',
  23019. 'begin',
  23020. ' Result:=w;',
  23021. ' Two:=One+w;',
  23022. ' Glob:=Glob;',
  23023. ' Result:=Self.Glob;',
  23024. ' Self.Glob:=Self.Glob;',
  23025. ' with Self do Glob:=Glob;',
  23026. 'end;',
  23027. 'class function THelper.bar(w: word): word;',
  23028. 'begin',
  23029. ' Result:=w;',
  23030. ' Two:=One;',
  23031. ' Glob:=Glob;',
  23032. 'end;',
  23033. 'var b: byte;',
  23034. 'begin',
  23035. ' byte.two:=byte.one;',
  23036. ' byte.Glob:=byte.Glob;',
  23037. ' with byte do begin',
  23038. ' two:=one;',
  23039. ' Glob:=Glob;',
  23040. ' end;',
  23041. ' b.two:=b.one;',
  23042. ' b.Glob:=b.Glob;',
  23043. ' with b do begin',
  23044. ' two:=one;',
  23045. ' Glob:=Glob;',
  23046. ' end;',
  23047. '']);
  23048. ConvertProgram;
  23049. CheckSource('TestTypeHelper_ClassVar',
  23050. LinesToStr([ // statements
  23051. 'rtl.createHelper(this, "THelper", null, function () {',
  23052. ' this.One = 1;',
  23053. ' this.Two = 2;',
  23054. ' this.Glob = 0;',
  23055. ' this.Foo = function (w) {',
  23056. ' var Result = 0;',
  23057. ' Result = w;',
  23058. ' $mod.THelper.Two = 1 + w;',
  23059. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  23060. ' Result = $mod.THelper.Glob;',
  23061. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  23062. ' var $with = this.get();',
  23063. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  23064. ' return Result;',
  23065. ' };',
  23066. ' this.Bar = function (w) {',
  23067. ' var Result = 0;',
  23068. ' Result = w;',
  23069. ' $mod.THelper.Two = 1;',
  23070. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  23071. ' return Result;',
  23072. ' };',
  23073. '});',
  23074. 'this.b = 0;',
  23075. '']),
  23076. LinesToStr([ // $mod.$main
  23077. '$mod.THelper.Two = 1;',
  23078. '$mod.THelper.Glob = $mod.THelper.Glob;',
  23079. '$mod.THelper.Two = 1;',
  23080. '$mod.THelper.Glob = $mod.THelper.Glob;',
  23081. '$mod.THelper.Two = 1;',
  23082. '$mod.THelper.Glob = $mod.THelper.Glob;',
  23083. 'var $with = $mod.b;',
  23084. '$mod.THelper.Two = 1;',
  23085. '$mod.THelper.Glob = $mod.THelper.Glob;',
  23086. '']));
  23087. end;
  23088. procedure TTestModule.TestTypeHelper_PassResultElement;
  23089. begin
  23090. StartProgram(false);
  23091. Add([
  23092. '{$modeswitch typehelpers}',
  23093. 'type',
  23094. ' THelper = type helper for word',
  23095. ' procedure DoIt(e: byte = 123);',
  23096. ' class procedure DoSome(e: byte = 456); static;',
  23097. ' end;',
  23098. 'procedure THelper.DoIt(e: byte);',
  23099. 'begin',
  23100. 'end;',
  23101. 'class procedure THelper.DoSome(e: byte);',
  23102. 'begin',
  23103. 'end;',
  23104. 'function Foo(w: word): word;',
  23105. 'begin',
  23106. ' Result.DoIt;',
  23107. ' Result.DoIt();',
  23108. ' Result.DoSome;',
  23109. ' Result.DoSome();',
  23110. ' with Result do begin',
  23111. ' DoIt;',
  23112. ' DoIt();',
  23113. ' DoSome;',
  23114. ' DoSome();',
  23115. ' end;',
  23116. 'end;',
  23117. 'begin',
  23118. '']);
  23119. ConvertProgram;
  23120. CheckSource('TestTypeHelper_PassResultElement',
  23121. LinesToStr([ // statements
  23122. 'rtl.createHelper(this, "THelper", null, function () {',
  23123. ' this.DoIt = function (e) {',
  23124. ' };',
  23125. ' this.DoSome = function (e) {',
  23126. ' };',
  23127. '});',
  23128. 'this.Foo = function (w) {',
  23129. ' var Result = 0;',
  23130. ' $mod.THelper.DoIt.call({',
  23131. ' get: function () {',
  23132. ' return Result;',
  23133. ' },',
  23134. ' set: function (v) {',
  23135. ' Result = v;',
  23136. ' }',
  23137. ' }, 123);',
  23138. ' $mod.THelper.DoIt.call({',
  23139. ' get: function () {',
  23140. ' return Result;',
  23141. ' },',
  23142. ' set: function (v) {',
  23143. ' Result = v;',
  23144. ' }',
  23145. ' }, 123);',
  23146. ' $mod.THelper.DoSome(456);',
  23147. ' $mod.THelper.DoSome(456);',
  23148. ' $mod.THelper.DoIt.call({',
  23149. ' get: function () {',
  23150. ' return Result;',
  23151. ' },',
  23152. ' set: function (v) {',
  23153. ' Result = v;',
  23154. ' }',
  23155. ' }, 123);',
  23156. ' $mod.THelper.DoIt.call({',
  23157. ' get: function () {',
  23158. ' return Result;',
  23159. ' },',
  23160. ' set: function (v) {',
  23161. ' Result = v;',
  23162. ' }',
  23163. ' }, 123);',
  23164. ' $mod.THelper.DoSome(456);',
  23165. ' $mod.THelper.DoSome(456);',
  23166. ' return Result;',
  23167. '};',
  23168. '']),
  23169. LinesToStr([ // $mod.$main
  23170. '']));
  23171. end;
  23172. procedure TTestModule.TestTypeHelper_PassArgs;
  23173. begin
  23174. StartProgram(false);
  23175. Add([
  23176. '{$modeswitch typehelpers}',
  23177. 'type',
  23178. ' THelper = type helper for word',
  23179. ' procedure DoIt(e: byte = 123);',
  23180. ' end;',
  23181. 'procedure THelper.DoIt(e: byte);',
  23182. 'begin',
  23183. 'end;',
  23184. 'procedure FooDefault(a: word);',
  23185. 'begin',
  23186. ' a.DoIt;',
  23187. ' with a do DoIt;',
  23188. 'end;',
  23189. 'procedure FooConst(const a: word);',
  23190. 'begin',
  23191. ' a.DoIt;',
  23192. ' with a do DoIt;',
  23193. 'end;',
  23194. 'procedure FooVar(var a: word);',
  23195. 'begin',
  23196. ' a.DoIt;',
  23197. ' with a do DoIt;',
  23198. 'end;',
  23199. 'begin',
  23200. '']);
  23201. ConvertProgram;
  23202. CheckSource('TestTypeHelper_PassArgs',
  23203. LinesToStr([ // statements
  23204. 'rtl.createHelper(this, "THelper", null, function () {',
  23205. ' this.DoIt = function (e) {',
  23206. ' };',
  23207. '});',
  23208. 'this.FooDefault = function (a) {',
  23209. ' $mod.THelper.DoIt.call({',
  23210. ' get: function () {',
  23211. ' return a;',
  23212. ' },',
  23213. ' set: function (v) {',
  23214. ' a = v;',
  23215. ' }',
  23216. ' }, 123);',
  23217. ' $mod.THelper.DoIt.call({',
  23218. ' get: function () {',
  23219. ' return a;',
  23220. ' },',
  23221. ' set: function (v) {',
  23222. ' a = v;',
  23223. ' }',
  23224. ' }, 123);',
  23225. '};',
  23226. 'this.FooConst = function (a) {',
  23227. ' $mod.THelper.DoIt.call({',
  23228. ' get: function () {',
  23229. ' return a;',
  23230. ' },',
  23231. ' set: function (v) {',
  23232. ' rtl.raiseE("EPropReadOnly");',
  23233. ' }',
  23234. ' }, 123);',
  23235. ' $mod.THelper.DoIt.call({',
  23236. ' get: function () {',
  23237. ' return a;',
  23238. ' },',
  23239. ' set: function () {',
  23240. ' rtl.raiseE("EPropReadOnly");',
  23241. ' }',
  23242. ' }, 123);',
  23243. '};',
  23244. 'this.FooVar = function (a) {',
  23245. ' $mod.THelper.DoIt.call(a, 123);',
  23246. ' var $with = a.get();',
  23247. ' $mod.THelper.DoIt.call(a, 123);',
  23248. '};',
  23249. '']),
  23250. LinesToStr([ // $mod.$main
  23251. '']));
  23252. end;
  23253. procedure TTestModule.TestTypeHelper_PassVarConst;
  23254. begin
  23255. StartProgram(false);
  23256. Add([
  23257. '{$modeswitch typehelpers}',
  23258. 'type',
  23259. ' THelper = type helper for word',
  23260. ' procedure DoIt(e: byte = 123);',
  23261. ' end;',
  23262. 'procedure THelper.DoIt(e: byte);',
  23263. 'begin',
  23264. 'end;',
  23265. 'var a: word;',
  23266. 'const c: word = 2;',
  23267. '{$writeableconst off}',
  23268. 'const r: word = 3;',
  23269. 'begin',
  23270. ' a.DoIt;',
  23271. ' with a do DoIt;',
  23272. ' c.DoIt;',
  23273. ' with c do DoIt;',
  23274. ' r.DoIt;',
  23275. ' with r do DoIt;',
  23276. '']);
  23277. ConvertProgram;
  23278. CheckSource('TestTypeHelper_PassVarConst',
  23279. LinesToStr([ // statements
  23280. 'rtl.createHelper(this, "THelper", null, function () {',
  23281. ' this.DoIt = function (e) {',
  23282. ' };',
  23283. '});',
  23284. 'this.a = 0;',
  23285. 'this.c = 2;',
  23286. 'this.r = 3;',
  23287. '']),
  23288. LinesToStr([ // $mod.$main
  23289. '$mod.THelper.DoIt.call({',
  23290. ' p: $mod,',
  23291. ' get: function () {',
  23292. ' return this.p.a;',
  23293. ' },',
  23294. ' set: function (v) {',
  23295. ' this.p.a = v;',
  23296. ' }',
  23297. '}, 123);',
  23298. 'var $with = $mod.a;',
  23299. '$mod.THelper.DoIt.call({',
  23300. ' get: function () {',
  23301. ' return $with;',
  23302. ' },',
  23303. ' set: function (v) {',
  23304. ' $with = v;',
  23305. ' }',
  23306. '}, 123);',
  23307. '$mod.THelper.DoIt.call({',
  23308. ' p: $mod,',
  23309. ' get: function () {',
  23310. ' return this.p.c;',
  23311. ' },',
  23312. ' set: function (v) {',
  23313. ' this.p.c = v;',
  23314. ' }',
  23315. '}, 123);',
  23316. 'var $with1 = $mod.c;',
  23317. '$mod.THelper.DoIt.call({',
  23318. ' get: function () {',
  23319. ' return $with1;',
  23320. ' },',
  23321. ' set: function (v) {',
  23322. ' $with1 = v;',
  23323. ' }',
  23324. '}, 123);',
  23325. '$mod.THelper.DoIt.call({',
  23326. ' get: function () {',
  23327. ' return 3;',
  23328. ' },',
  23329. ' set: function (v) {',
  23330. ' rtl.raiseE("EPropReadOnly");',
  23331. ' }',
  23332. '}, 123);',
  23333. 'var $with2 = 3;',
  23334. ' $mod.THelper.DoIt.call({',
  23335. ' get: function () {',
  23336. ' return $with2;',
  23337. ' },',
  23338. ' set: function () {',
  23339. ' rtl.raiseE("EPropReadOnly");',
  23340. ' }',
  23341. ' }, 123);',
  23342. '']));
  23343. end;
  23344. procedure TTestModule.TestTypeHelper_PassFuncResult;
  23345. begin
  23346. StartProgram(false);
  23347. Add([
  23348. '{$modeswitch typehelpers}',
  23349. 'type',
  23350. ' THelper = type helper for word',
  23351. ' procedure DoIt(e: byte = 123);',
  23352. ' end;',
  23353. 'procedure THelper.DoIt(e: byte);',
  23354. 'begin',
  23355. 'end;',
  23356. 'function Foo(b: byte = 1): word;',
  23357. 'begin',
  23358. 'end;',
  23359. 'begin',
  23360. ' Foo.DoIt;',
  23361. ' Foo().DoIt;',
  23362. ' with Foo do DoIt;',
  23363. ' with Foo() do DoIt;',
  23364. '']);
  23365. ConvertProgram;
  23366. CheckSource('TestTypeHelper_PassFuncResult',
  23367. LinesToStr([ // statements
  23368. 'rtl.createHelper(this, "THelper", null, function () {',
  23369. ' this.DoIt = function (e) {',
  23370. ' };',
  23371. '});',
  23372. 'this.Foo = function (b) {',
  23373. ' var Result = 0;',
  23374. ' return Result;',
  23375. '};',
  23376. '']),
  23377. LinesToStr([ // $mod.$main
  23378. '$mod.THelper.DoIt.call({',
  23379. ' a: $mod.Foo(1),',
  23380. ' get: function () {',
  23381. ' return this.a;',
  23382. ' },',
  23383. ' set: function (v) {',
  23384. ' this.a = v;',
  23385. ' }',
  23386. '}, 123);',
  23387. '$mod.THelper.DoIt.call({',
  23388. ' a: $mod.Foo(1),',
  23389. ' get: function () {',
  23390. ' return this.a;',
  23391. ' },',
  23392. ' set: function (v) {',
  23393. ' this.a = v;',
  23394. ' }',
  23395. '}, 123);',
  23396. 'var $with = $mod.Foo(1);',
  23397. '$mod.THelper.DoIt.call({',
  23398. ' get: function () {',
  23399. ' return $with;',
  23400. ' },',
  23401. ' set: function (v) {',
  23402. ' $with = v;',
  23403. ' }',
  23404. '}, 123);',
  23405. 'var $with1 = $mod.Foo(1);',
  23406. '$mod.THelper.DoIt.call({',
  23407. ' get: function () {',
  23408. ' return $with1;',
  23409. ' },',
  23410. ' set: function (v) {',
  23411. ' $with1 = v;',
  23412. ' }',
  23413. '}, 123);',
  23414. '']));
  23415. end;
  23416. procedure TTestModule.TestTypeHelper_PassPropertyField;
  23417. begin
  23418. StartProgram(false);
  23419. Add([
  23420. '{$modeswitch typehelpers}',
  23421. 'type',
  23422. ' TObject = class',
  23423. ' FField: word;',
  23424. ' procedure SetField(Value: word);',
  23425. ' property Field: word read FField write SetField;',
  23426. ' end;',
  23427. ' THelper = type helper for word',
  23428. ' procedure Fly;',
  23429. ' class procedure Run; static;',
  23430. ' end;',
  23431. 'procedure TObject.SetField(Value: word);',
  23432. 'begin',
  23433. ' Field.Fly;',
  23434. ' Field.Run;',
  23435. ' Self.Field.Fly;',
  23436. ' Self.Field.Run;',
  23437. ' with Self do begin',
  23438. ' Field.Fly;',
  23439. ' Field.Run;',
  23440. ' end;',
  23441. ' with Self.Field do begin',
  23442. ' Fly;',
  23443. ' Run;',
  23444. ' end;',
  23445. 'end;',
  23446. 'procedure THelper.Fly;',
  23447. 'begin',
  23448. 'end;',
  23449. 'class procedure THelper.Run;',
  23450. 'begin',
  23451. 'end;',
  23452. 'var',
  23453. ' o: TObject;',
  23454. 'begin',
  23455. ' o.Field.Fly;',
  23456. ' o.Field.Run;',
  23457. ' with o do begin',
  23458. ' Field.Fly;',
  23459. ' Field.Run;',
  23460. ' end;',
  23461. ' with o.Field do begin',
  23462. ' Fly;',
  23463. ' Run;',
  23464. ' end;',
  23465. '']);
  23466. ConvertProgram;
  23467. CheckSource('TestTypeHelper_PassPropertyField',
  23468. LinesToStr([ // statements
  23469. 'rtl.createClass(this, "TObject", null, function () {',
  23470. ' this.$init = function () {',
  23471. ' this.FField = 0;',
  23472. ' };',
  23473. ' this.$final = function () {',
  23474. ' };',
  23475. ' this.SetField = function (Value) {',
  23476. ' $mod.THelper.Fly.call({',
  23477. ' p: this,',
  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. ' $mod.THelper.Fly.call({',
  23487. ' p: this,',
  23488. ' get: function () {',
  23489. ' return this.p.FField;',
  23490. ' },',
  23491. ' set: function (v) {',
  23492. ' this.p.FField = v;',
  23493. ' }',
  23494. ' });',
  23495. ' $mod.THelper.Run();',
  23496. ' $mod.THelper.Fly.call({',
  23497. ' p: this,',
  23498. ' get: function () {',
  23499. ' return this.p.FField;',
  23500. ' },',
  23501. ' set: function (v) {',
  23502. ' this.p.FField = v;',
  23503. ' }',
  23504. ' });',
  23505. ' $mod.THelper.Run();',
  23506. ' var $with = this.FField;',
  23507. ' $mod.THelper.Fly.call({',
  23508. ' get: function () {',
  23509. ' return $with;',
  23510. ' },',
  23511. ' set: function (v) {',
  23512. ' $with = v;',
  23513. ' }',
  23514. ' });',
  23515. ' $mod.THelper.Run();',
  23516. ' };',
  23517. '});',
  23518. 'rtl.createHelper(this, "THelper", null, function () {',
  23519. ' this.Fly = function () {',
  23520. ' };',
  23521. ' this.Run = function () {',
  23522. ' };',
  23523. '});',
  23524. 'this.o = null;',
  23525. '']),
  23526. LinesToStr([ // $mod.$main
  23527. '$mod.THelper.Fly.call({',
  23528. ' p: $mod.o,',
  23529. ' get: function () {',
  23530. ' return this.p.FField;',
  23531. ' },',
  23532. ' set: function (v) {',
  23533. ' this.p.FField = v;',
  23534. ' }',
  23535. '});',
  23536. '$mod.THelper.Run();',
  23537. 'var $with = $mod.o;',
  23538. '$mod.THelper.Fly.call({',
  23539. ' p: $with,',
  23540. ' get: function () {',
  23541. ' return this.p.FField;',
  23542. ' },',
  23543. ' set: function (v) {',
  23544. ' this.p.FField = v;',
  23545. ' }',
  23546. '});',
  23547. '$mod.THelper.Run();',
  23548. 'var $with1 = $mod.o.FField;',
  23549. '$mod.THelper.Fly.call({',
  23550. ' get: function () {',
  23551. ' return $with1;',
  23552. ' },',
  23553. ' set: function (v) {',
  23554. ' $with1 = v;',
  23555. ' }',
  23556. '});',
  23557. '$mod.THelper.Run();',
  23558. '']));
  23559. end;
  23560. procedure TTestModule.TestTypeHelper_PassPropertyGetter;
  23561. begin
  23562. StartProgram(false);
  23563. Add([
  23564. '{$modeswitch typehelpers}',
  23565. 'type',
  23566. ' TObject = class',
  23567. ' FField: word;',
  23568. ' function GetField: word;',
  23569. ' property Field: word read GetField write FField;',
  23570. ' end;',
  23571. ' THelper = type helper for word',
  23572. ' procedure Fly;',
  23573. ' class procedure Run; static;',
  23574. ' end;',
  23575. 'function TObject.GetField: word;',
  23576. 'begin',
  23577. ' Field.Fly;',
  23578. ' Field.Run;',
  23579. ' Self.Field.Fly;',
  23580. ' Self.Field.Run;',
  23581. ' with Self do begin',
  23582. ' Field.Fly;',
  23583. ' Field.Run;',
  23584. ' end;',
  23585. ' with Self.Field do begin',
  23586. ' Fly;',
  23587. ' Run;',
  23588. ' end;',
  23589. 'end;',
  23590. 'procedure THelper.Fly;',
  23591. 'begin',
  23592. 'end;',
  23593. 'class procedure THelper.Run;',
  23594. 'begin',
  23595. 'end;',
  23596. 'var',
  23597. ' o: TObject;',
  23598. 'begin',
  23599. ' o.Field.Fly;',
  23600. ' o.Field.Run;',
  23601. ' with o do begin',
  23602. ' Field.Fly;',
  23603. ' Field.Run;',
  23604. ' end;',
  23605. ' with o.Field do begin',
  23606. ' Fly;',
  23607. ' Run;',
  23608. ' end;',
  23609. '']);
  23610. ConvertProgram;
  23611. CheckSource('TestTypeHelper_PassPropertyGetter',
  23612. LinesToStr([ // statements
  23613. 'rtl.createClass(this, "TObject", null, function () {',
  23614. ' this.$init = function () {',
  23615. ' this.FField = 0;',
  23616. ' };',
  23617. ' this.$final = function () {',
  23618. ' };',
  23619. ' this.GetField = function () {',
  23620. ' var Result = 0;',
  23621. ' $mod.THelper.Fly.call({',
  23622. ' p: this.GetField(),',
  23623. ' get: function () {',
  23624. ' return this.p;',
  23625. ' },',
  23626. ' set: function (v) {',
  23627. ' this.p = v;',
  23628. ' }',
  23629. ' });',
  23630. ' $mod.THelper.Run();',
  23631. ' $mod.THelper.Fly.call({',
  23632. ' p: this.GetField(),',
  23633. ' get: function () {',
  23634. ' return this.p;',
  23635. ' },',
  23636. ' set: function (v) {',
  23637. ' this.p = v;',
  23638. ' }',
  23639. ' });',
  23640. ' $mod.THelper.Run();',
  23641. ' $mod.THelper.Fly.call({',
  23642. ' p: this.GetField(),',
  23643. ' get: function () {',
  23644. ' return this.p;',
  23645. ' },',
  23646. ' set: function (v) {',
  23647. ' this.p = v;',
  23648. ' }',
  23649. ' });',
  23650. ' $mod.THelper.Run();',
  23651. ' var $with = this.GetField();',
  23652. ' $mod.THelper.Fly.call({',
  23653. ' get: function () {',
  23654. ' return $with;',
  23655. ' },',
  23656. ' set: function (v) {',
  23657. ' $with = v;',
  23658. ' }',
  23659. ' });',
  23660. ' $mod.THelper.Run();',
  23661. ' return Result;',
  23662. ' };',
  23663. '});',
  23664. 'rtl.createHelper(this, "THelper", null, function () {',
  23665. ' this.Fly = function () {',
  23666. ' };',
  23667. ' this.Run = function () {',
  23668. ' };',
  23669. '});',
  23670. 'this.o = null;',
  23671. '']),
  23672. LinesToStr([ // $mod.$main
  23673. '$mod.THelper.Fly.call({',
  23674. ' p: $mod.o.GetField(),',
  23675. ' get: function () {',
  23676. ' return this.p;',
  23677. ' },',
  23678. ' set: function (v) {',
  23679. ' this.p = v;',
  23680. ' }',
  23681. '});',
  23682. '$mod.THelper.Run();',
  23683. 'var $with = $mod.o;',
  23684. '$mod.THelper.Fly.call({',
  23685. ' p: $with.GetField(),',
  23686. ' get: function () {',
  23687. ' return this.p;',
  23688. ' },',
  23689. ' set: function (v) {',
  23690. ' this.p = v;',
  23691. ' }',
  23692. '});',
  23693. '$mod.THelper.Run();',
  23694. 'var $with1 = $mod.o.GetField();',
  23695. '$mod.THelper.Fly.call({',
  23696. ' get: function () {',
  23697. ' return $with1;',
  23698. ' },',
  23699. ' set: function (v) {',
  23700. ' $with1 = v;',
  23701. ' }',
  23702. '});',
  23703. '$mod.THelper.Run();',
  23704. '']));
  23705. end;
  23706. procedure TTestModule.TestTypeHelper_PassClassPropertyField;
  23707. begin
  23708. StartProgram(false);
  23709. Add([
  23710. '{$modeswitch typehelpers}',
  23711. 'type',
  23712. ' TObject = class',
  23713. ' class var FField: word;',
  23714. ' class procedure SetField(Value: word);',
  23715. ' class property Field: word read FField write SetField;',
  23716. ' end;',
  23717. ' THelper = type helper for word',
  23718. ' procedure Fly(n: byte);',
  23719. ' end;',
  23720. 'class procedure TObject.SetField(Value: word);',
  23721. 'begin',
  23722. ' Field.Fly(1);',
  23723. ' Self.Field.Fly(2);',
  23724. ' with Self do Field.Fly(3);',
  23725. ' with Self.Field do Fly(4);',
  23726. ' TObject.Field.Fly(5);',
  23727. ' with TObject do Field.Fly(6);',
  23728. ' with TObject.Field do Fly(7);',
  23729. 'end;',
  23730. 'procedure THelper.Fly(n: byte);',
  23731. 'begin',
  23732. 'end;',
  23733. 'var',
  23734. ' o: TObject;',
  23735. 'begin',
  23736. ' o.Field.Fly(11);',
  23737. ' with o do Field.Fly(12);',
  23738. ' with o.Field do Fly(13);',
  23739. ' TObject.Field.Fly(14);',
  23740. ' with TObject do Field.Fly(15);',
  23741. ' with TObject.Field do Fly(16);',
  23742. '']);
  23743. ConvertProgram;
  23744. CheckSource('TestTypeHelper_PassClassPropertyField',
  23745. LinesToStr([ // statements
  23746. 'rtl.createClass(this, "TObject", null, function () {',
  23747. ' this.FField = 0;',
  23748. ' this.$init = function () {',
  23749. ' };',
  23750. ' this.$final = function () {',
  23751. ' };',
  23752. ' this.SetField = function (Value) {',
  23753. ' $mod.THelper.Fly.call({',
  23754. ' p: this,',
  23755. ' get: function () {',
  23756. ' return this.p.FField;',
  23757. ' },',
  23758. ' set: function (v) {',
  23759. ' $mod.TObject.FField = v;',
  23760. ' }',
  23761. ' }, 1);',
  23762. ' $mod.THelper.Fly.call({',
  23763. ' p: this,',
  23764. ' get: function () {',
  23765. ' return this.p.FField;',
  23766. ' },',
  23767. ' set: function (v) {',
  23768. ' $mod.TObject.FField = v;',
  23769. ' }',
  23770. ' }, 2);',
  23771. ' $mod.THelper.Fly.call({',
  23772. ' p: this,',
  23773. ' get: function () {',
  23774. ' return this.p.FField;',
  23775. ' },',
  23776. ' set: function (v) {',
  23777. ' $mod.TObject.FField = v;',
  23778. ' }',
  23779. ' }, 3);',
  23780. ' var $with = this.FField;',
  23781. ' $mod.THelper.Fly.call({',
  23782. ' get: function () {',
  23783. ' return $with;',
  23784. ' },',
  23785. ' set: function (v) {',
  23786. ' $with = v;',
  23787. ' }',
  23788. ' }, 4);',
  23789. ' $mod.THelper.Fly.call({',
  23790. ' p: $mod.TObject,',
  23791. ' get: function () {',
  23792. ' return this.p.FField;',
  23793. ' },',
  23794. ' set: function (v) {',
  23795. ' $mod.TObject.FField = v;',
  23796. ' }',
  23797. ' }, 5);',
  23798. ' var $with1 = $mod.TObject;',
  23799. ' $mod.THelper.Fly.call({',
  23800. ' p: $with1,',
  23801. ' get: function () {',
  23802. ' return this.p.FField;',
  23803. ' },',
  23804. ' set: function (v) {',
  23805. ' $mod.TObject.FField = v;',
  23806. ' }',
  23807. ' }, 6);',
  23808. ' var $with2 = $mod.TObject.FField;',
  23809. ' $mod.THelper.Fly.call({',
  23810. ' get: function () {',
  23811. ' return $with2;',
  23812. ' },',
  23813. ' set: function (v) {',
  23814. ' $with2 = v;',
  23815. ' }',
  23816. ' }, 7);',
  23817. ' };',
  23818. '});',
  23819. 'rtl.createHelper(this, "THelper", null, function () {',
  23820. ' this.Fly = function (n) {',
  23821. ' };',
  23822. '});',
  23823. 'this.o = null;',
  23824. '']),
  23825. LinesToStr([ // $mod.$main
  23826. '$mod.THelper.Fly.call({',
  23827. ' p: $mod.o,',
  23828. ' get: function () {',
  23829. ' return this.p.FField;',
  23830. ' },',
  23831. ' set: function (v) {',
  23832. ' $mod.TObject.FField = v;',
  23833. ' }',
  23834. '}, 11);',
  23835. 'var $with = $mod.o;',
  23836. '$mod.THelper.Fly.call({',
  23837. ' p: $with,',
  23838. ' get: function () {',
  23839. ' return this.p.FField;',
  23840. ' },',
  23841. ' set: function (v) {',
  23842. ' $mod.TObject.FField = v;',
  23843. ' }',
  23844. '}, 12);',
  23845. 'var $with1 = $mod.o.FField;',
  23846. '$mod.THelper.Fly.call({',
  23847. ' get: function () {',
  23848. ' return $with1;',
  23849. ' },',
  23850. ' set: function (v) {',
  23851. ' $with1 = v;',
  23852. ' }',
  23853. '}, 13);',
  23854. '$mod.THelper.Fly.call({',
  23855. ' p: $mod.TObject,',
  23856. ' get: function () {',
  23857. ' return this.p.FField;',
  23858. ' },',
  23859. ' set: function (v) {',
  23860. ' $mod.TObject.FField = v;',
  23861. ' }',
  23862. '}, 14);',
  23863. 'var $with2 = $mod.TObject;',
  23864. '$mod.THelper.Fly.call({',
  23865. ' p: $with2,',
  23866. ' get: function () {',
  23867. ' return this.p.FField;',
  23868. ' },',
  23869. ' set: function (v) {',
  23870. ' $mod.TObject.FField = v;',
  23871. ' }',
  23872. '}, 15);',
  23873. 'var $with3 = $mod.TObject.FField;',
  23874. '$mod.THelper.Fly.call({',
  23875. ' get: function () {',
  23876. ' return $with3;',
  23877. ' },',
  23878. ' set: function (v) {',
  23879. ' $with3 = v;',
  23880. ' }',
  23881. '}, 16);',
  23882. '']));
  23883. end;
  23884. procedure TTestModule.TestTypeHelper_PassClassPropertyGetterStatic;
  23885. begin
  23886. StartProgram(false);
  23887. Add([
  23888. '{$modeswitch typehelpers}',
  23889. 'type',
  23890. ' TObject = class',
  23891. ' class var FField: word;',
  23892. ' class function GetField: word; static;',
  23893. ' class property Field: word read GetField write FField;',
  23894. ' end;',
  23895. ' THelper = type helper for word',
  23896. ' procedure Fly(n: byte);',
  23897. ' end;',
  23898. 'class function TObject.GetField: word;',
  23899. 'begin',
  23900. ' Field.Fly(1);',
  23901. ' TObject.Field.Fly(5);',
  23902. ' with TObject do Field.Fly(6);',
  23903. ' with TObject.Field do Fly(7);',
  23904. 'end;',
  23905. 'procedure THelper.Fly(n: byte);',
  23906. 'begin',
  23907. 'end;',
  23908. 'var',
  23909. ' o: TObject;',
  23910. 'begin',
  23911. ' o.Field.Fly(11);',
  23912. ' with o do Field.Fly(12);',
  23913. ' with o.Field do Fly(13);',
  23914. '']);
  23915. ConvertProgram;
  23916. CheckSource('TestTypeHelper_PassClassPropertyGetterStatic',
  23917. LinesToStr([ // statements
  23918. 'rtl.createClass(this, "TObject", null, function () {',
  23919. ' this.FField = 0;',
  23920. ' this.$init = function () {',
  23921. ' };',
  23922. ' this.$final = function () {',
  23923. ' };',
  23924. ' this.GetField = function () {',
  23925. ' var Result = 0;',
  23926. ' $mod.THelper.Fly.call({',
  23927. ' p: $mod.TObject.GetField(),',
  23928. ' get: function () {',
  23929. ' return this.p;',
  23930. ' },',
  23931. ' set: function (v) {',
  23932. ' this.p = v;',
  23933. ' }',
  23934. ' }, 1);',
  23935. ' $mod.THelper.Fly.call({',
  23936. ' p: $mod.TObject.GetField(),',
  23937. ' get: function () {',
  23938. ' return this.p;',
  23939. ' },',
  23940. ' set: function (v) {',
  23941. ' this.p = v;',
  23942. ' }',
  23943. ' }, 5);',
  23944. ' var $with = $mod.TObject;',
  23945. ' $mod.THelper.Fly.call({',
  23946. ' p: $with.GetField(),',
  23947. ' get: function () {',
  23948. ' return this.p;',
  23949. ' },',
  23950. ' set: function (v) {',
  23951. ' this.p = v;',
  23952. ' }',
  23953. ' }, 6);',
  23954. ' var $with1 = $mod.TObject.GetField();',
  23955. ' $mod.THelper.Fly.call({',
  23956. ' get: function () {',
  23957. ' return $with1;',
  23958. ' },',
  23959. ' set: function (v) {',
  23960. ' $with1 = v;',
  23961. ' }',
  23962. ' }, 7);',
  23963. ' return Result;',
  23964. ' };',
  23965. '});',
  23966. 'rtl.createHelper(this, "THelper", null, function () {',
  23967. ' this.Fly = function (n) {',
  23968. ' };',
  23969. '});',
  23970. 'this.o = null;',
  23971. '']),
  23972. LinesToStr([ // $mod.$main
  23973. '$mod.THelper.Fly.call({',
  23974. ' p: $mod.TObject.GetField(),',
  23975. ' get: function () {',
  23976. ' return this.p;',
  23977. ' },',
  23978. ' set: function (v) {',
  23979. ' this.p = v;',
  23980. ' }',
  23981. '}, 11);',
  23982. 'var $with = $mod.o;',
  23983. '$mod.THelper.Fly.call({',
  23984. ' p: $with.GetField(),',
  23985. ' get: function () {',
  23986. ' return this.p;',
  23987. ' },',
  23988. ' set: function (v) {',
  23989. ' this.p = v;',
  23990. ' }',
  23991. '}, 12);',
  23992. 'var $with1 = $mod.TObject.GetField();',
  23993. '$mod.THelper.Fly.call({',
  23994. ' get: function () {',
  23995. ' return $with1;',
  23996. ' },',
  23997. ' set: function (v) {',
  23998. ' $with1 = v;',
  23999. ' }',
  24000. '}, 13);',
  24001. '']));
  24002. end;
  24003. procedure TTestModule.TestTypeHelper_PassClassPropertyGetterNonStatic;
  24004. begin
  24005. StartProgram(false);
  24006. Add([
  24007. '{$modeswitch typehelpers}',
  24008. 'type',
  24009. ' TObject = class',
  24010. ' class var FField: word;',
  24011. ' class function GetField: word;',
  24012. ' class property Field: word read GetField write FField;',
  24013. ' end;',
  24014. ' TClass = class of TObject;',
  24015. ' THelper = type helper for word',
  24016. ' procedure Fly(n: byte);',
  24017. ' end;',
  24018. 'class function TObject.GetField: word;',
  24019. 'begin',
  24020. ' Field.Fly(1);',
  24021. ' Self.Field.Fly(5);',
  24022. ' with Self do Field.Fly(6);',
  24023. ' with Self.Field do Fly(7);',
  24024. 'end;',
  24025. 'procedure THelper.Fly(n: byte);',
  24026. 'begin',
  24027. 'end;',
  24028. 'var',
  24029. ' o: TObject;',
  24030. ' c: TClass;',
  24031. 'begin',
  24032. ' o.Field.Fly(11);',
  24033. ' with o do Field.Fly(12);',
  24034. ' with o.Field do Fly(13);',
  24035. ' c.Field.Fly(14);',
  24036. ' with c do Field.Fly(15);',
  24037. ' with c.Field do Fly(16);',
  24038. '']);
  24039. ConvertProgram;
  24040. CheckSource('TestTypeHelper_PassClassPropertyGetterNonStatic',
  24041. LinesToStr([ // statements
  24042. 'rtl.createClass(this, "TObject", null, function () {',
  24043. ' this.FField = 0;',
  24044. ' this.$init = function () {',
  24045. ' };',
  24046. ' this.$final = function () {',
  24047. ' };',
  24048. ' this.GetField = function () {',
  24049. ' var Result = 0;',
  24050. ' $mod.THelper.Fly.call({',
  24051. ' p: this.GetField(),',
  24052. ' get: function () {',
  24053. ' return this.p;',
  24054. ' },',
  24055. ' set: function (v) {',
  24056. ' this.p = v;',
  24057. ' }',
  24058. ' }, 1);',
  24059. ' $mod.THelper.Fly.call({',
  24060. ' p: this.GetField(),',
  24061. ' get: function () {',
  24062. ' return this.p;',
  24063. ' },',
  24064. ' set: function (v) {',
  24065. ' this.p = v;',
  24066. ' }',
  24067. ' }, 5);',
  24068. ' $mod.THelper.Fly.call({',
  24069. ' p: this.GetField(),',
  24070. ' get: function () {',
  24071. ' return this.p;',
  24072. ' },',
  24073. ' set: function (v) {',
  24074. ' this.p = v;',
  24075. ' }',
  24076. ' }, 6);',
  24077. ' var $with = this.GetField();',
  24078. ' $mod.THelper.Fly.call({',
  24079. ' get: function () {',
  24080. ' return $with;',
  24081. ' },',
  24082. ' set: function (v) {',
  24083. ' $with = v;',
  24084. ' }',
  24085. ' }, 7);',
  24086. ' return Result;',
  24087. ' };',
  24088. '});',
  24089. 'rtl.createHelper(this, "THelper", null, function () {',
  24090. ' this.Fly = function (n) {',
  24091. ' };',
  24092. '});',
  24093. 'this.o = null;',
  24094. 'this.c = null;',
  24095. '']),
  24096. LinesToStr([ // $mod.$main
  24097. '$mod.THelper.Fly.call({',
  24098. ' p: $mod.o.$class.GetField(),',
  24099. ' get: function () {',
  24100. ' return this.p;',
  24101. ' },',
  24102. ' set: function (v) {',
  24103. ' this.p = v;',
  24104. ' }',
  24105. '}, 11);',
  24106. 'var $with = $mod.o;',
  24107. '$mod.THelper.Fly.call({',
  24108. ' p: $with.$class.GetField(),',
  24109. ' get: function () {',
  24110. ' return this.p;',
  24111. ' },',
  24112. ' set: function (v) {',
  24113. ' this.p = v;',
  24114. ' }',
  24115. '}, 12);',
  24116. 'var $with1 = $mod.o.$class.GetField();',
  24117. '$mod.THelper.Fly.call({',
  24118. ' get: function () {',
  24119. ' return $with1;',
  24120. ' },',
  24121. ' set: function (v) {',
  24122. ' $with1 = v;',
  24123. ' }',
  24124. '}, 13);',
  24125. '$mod.THelper.Fly.call({',
  24126. ' p: $mod.c.GetField(),',
  24127. ' get: function () {',
  24128. ' return this.p;',
  24129. ' },',
  24130. ' set: function (v) {',
  24131. ' this.p = v;',
  24132. ' }',
  24133. '}, 14);',
  24134. 'var $with2 = $mod.c;',
  24135. '$mod.THelper.Fly.call({',
  24136. ' p: $with2.GetField(),',
  24137. ' get: function () {',
  24138. ' return this.p;',
  24139. ' },',
  24140. ' set: function (v) {',
  24141. ' this.p = v;',
  24142. ' }',
  24143. '}, 15);',
  24144. 'var $with3 = $mod.c.GetField();',
  24145. '$mod.THelper.Fly.call({',
  24146. ' get: function () {',
  24147. ' return $with3;',
  24148. ' },',
  24149. ' set: function (v) {',
  24150. ' $with3 = v;',
  24151. ' }',
  24152. '}, 16);',
  24153. '']));
  24154. end;
  24155. procedure TTestModule.TestTypeHelper_Property;
  24156. begin
  24157. StartProgram(false);
  24158. Add([
  24159. '{$modeswitch typehelpers}',
  24160. 'type',
  24161. ' THelper = type helper for word',
  24162. ' function GetSize: longint;',
  24163. ' procedure SetSize(Value: longint);',
  24164. ' property Size: longint read GetSize write SetSize;',
  24165. ' end;',
  24166. 'function THelper.GetSize: longint;',
  24167. 'begin',
  24168. ' Result:=Size+1;',
  24169. ' Size:=2;',
  24170. ' Result:=Self.Size+3;',
  24171. ' Self.Size:=4;',
  24172. ' with Self do begin',
  24173. ' Result:=Size+5;',
  24174. ' Size:=6;',
  24175. ' end;',
  24176. 'end;',
  24177. 'procedure THelper.SetSize(Value: longint);',
  24178. 'begin',
  24179. 'end;',
  24180. 'var w: word;',
  24181. 'begin',
  24182. ' w:=w.Size+7;',
  24183. ' w.Size:=w+8;',
  24184. ' with w do begin',
  24185. ' w:=Size+9;',
  24186. ' Size:=w+10;',
  24187. ' end;',
  24188. '']);
  24189. ConvertProgram;
  24190. CheckSource('TestTypeHelper_Property',
  24191. LinesToStr([ // statements
  24192. 'rtl.createHelper(this, "THelper", null, function () {',
  24193. ' this.GetSize = function () {',
  24194. ' var Result = 0;',
  24195. ' Result = $mod.THelper.GetSize.call(this) + 1;',
  24196. ' $mod.THelper.SetSize.call(this, 2);',
  24197. ' Result = $mod.THelper.GetSize.call(this) + 3;',
  24198. ' $mod.THelper.SetSize.call(this, 4);',
  24199. ' var $with = this.get();',
  24200. ' Result = $mod.THelper.GetSize.call(this) + 5;',
  24201. ' $mod.THelper.SetSize.call(this, 6);',
  24202. ' return Result;',
  24203. ' };',
  24204. ' this.SetSize = function (Value) {',
  24205. ' };',
  24206. '});',
  24207. 'this.w = 0;',
  24208. '']),
  24209. LinesToStr([ // $mod.$main
  24210. '$mod.w = $mod.THelper.GetSize.call({',
  24211. ' p: $mod,',
  24212. ' get: function () {',
  24213. ' return this.p.w;',
  24214. ' },',
  24215. ' set: function (v) {',
  24216. ' this.p.w = v;',
  24217. ' }',
  24218. '}) + 7;',
  24219. '$mod.THelper.SetSize.call({',
  24220. ' p: $mod,',
  24221. ' get: function () {',
  24222. ' return this.p.w;',
  24223. ' },',
  24224. ' set: function (v) {',
  24225. ' this.p.w = v;',
  24226. ' }',
  24227. '}, $mod.w + 8);',
  24228. 'var $with = $mod.w;',
  24229. '$mod.w = $mod.THelper.GetSize.call({',
  24230. ' get: function () {',
  24231. ' return $with;',
  24232. ' },',
  24233. ' set: function (v) {',
  24234. ' $with = v;',
  24235. ' }',
  24236. '}) + 9;',
  24237. '$mod.THelper.SetSize.call({',
  24238. ' get: function () {',
  24239. ' return $with;',
  24240. ' },',
  24241. ' set: function (v) {',
  24242. ' $with = v;',
  24243. ' }',
  24244. '}, $mod.w + 10);',
  24245. '']));
  24246. end;
  24247. procedure TTestModule.TestTypeHelper_Property_Array;
  24248. begin
  24249. StartProgram(false);
  24250. Add([
  24251. '{$modeswitch typehelpers}',
  24252. 'type',
  24253. ' THelper = type helper for word',
  24254. ' function GetItems(Index: byte): boolean;',
  24255. ' procedure SetItems(Index: byte; Value: boolean);',
  24256. ' property Items[Index: byte]: boolean read GetItems write SetItems;',
  24257. ' end;',
  24258. 'function THelper.GetItems(Index: byte): boolean;',
  24259. 'begin',
  24260. ' Result:=Items[1];',
  24261. ' Items[2]:=false;',
  24262. ' Result:=Self.Items[3];',
  24263. ' Self.Items[4]:=true;',
  24264. ' with Self do begin',
  24265. ' Result:=Items[5];',
  24266. ' Items[6]:=false;',
  24267. ' end;',
  24268. 'end;',
  24269. 'procedure THelper.SetItems(Index: byte; Value: boolean);',
  24270. 'begin',
  24271. 'end;',
  24272. 'var',
  24273. ' w: word;',
  24274. ' b: boolean;',
  24275. 'begin',
  24276. ' b:=w.Items[1];',
  24277. ' w.Items[2]:=b;',
  24278. ' with w do begin',
  24279. ' b:=Items[3];',
  24280. ' Items[4]:=b;',
  24281. ' end;',
  24282. '']);
  24283. ConvertProgram;
  24284. CheckSource('TestTypeHelper_Property_Array',
  24285. LinesToStr([ // statements
  24286. 'rtl.createHelper(this, "THelper", null, function () {',
  24287. ' this.GetItems = function (Index) {',
  24288. ' var Result = false;',
  24289. ' Result = $mod.THelper.GetItems.call(this, 1);',
  24290. ' $mod.THelper.SetItems.call(this, 2, false);',
  24291. ' Result = $mod.THelper.GetItems.call(this, 3);',
  24292. ' $mod.THelper.SetItems.call(this, 4, true);',
  24293. ' var $with = this.get();',
  24294. ' Result = $mod.THelper.GetItems.call(this, 5);',
  24295. ' $mod.THelper.SetItems.call(this, 6, false);',
  24296. ' return Result;',
  24297. ' };',
  24298. ' this.SetItems = function (Index, Value) {',
  24299. ' };',
  24300. '});',
  24301. 'this.w = 0;',
  24302. 'this.b = false;',
  24303. '']),
  24304. LinesToStr([ // $mod.$main
  24305. '$mod.b = $mod.THelper.GetItems.call({',
  24306. ' p: $mod,',
  24307. ' get: function () {',
  24308. ' return this.p.w;',
  24309. ' },',
  24310. ' set: function (v) {',
  24311. ' this.p.w = v;',
  24312. ' }',
  24313. '}, 1);',
  24314. '$mod.THelper.SetItems.call({',
  24315. ' p: $mod,',
  24316. ' get: function () {',
  24317. ' return this.p.w;',
  24318. ' },',
  24319. ' set: function (v) {',
  24320. ' this.p.w = v;',
  24321. ' }',
  24322. '}, 2, $mod.b);',
  24323. 'var $with = $mod.w;',
  24324. '$mod.b = $mod.THelper.GetItems.call({',
  24325. ' get: function () {',
  24326. ' return $with;',
  24327. ' },',
  24328. ' set: function (v) {',
  24329. ' $with = v;',
  24330. ' }',
  24331. '}, 3);',
  24332. '$mod.THelper.SetItems.call({',
  24333. ' get: function () {',
  24334. ' return $with;',
  24335. ' },',
  24336. ' set: function (v) {',
  24337. ' $with = v;',
  24338. ' }',
  24339. '}, 4, $mod.b);',
  24340. '']));
  24341. end;
  24342. procedure TTestModule.TestTypeHelper_ClassProperty;
  24343. begin
  24344. StartProgram(false);
  24345. Add([
  24346. '{$modeswitch typehelpers}',
  24347. 'type',
  24348. ' THelper = type helper for word',
  24349. ' class function GetSize: longint; static;',
  24350. ' class procedure SetSize(Value: longint); static;',
  24351. ' class property Size: longint read GetSize write SetSize;',
  24352. ' end;',
  24353. 'class function THelper.GetSize: longint;',
  24354. 'begin',
  24355. ' Result:=Size+1;',
  24356. ' Size:=2;',
  24357. 'end;',
  24358. 'class procedure THelper.SetSize(Value: longint);',
  24359. 'begin',
  24360. 'end;',
  24361. 'begin',
  24362. '']);
  24363. ConvertProgram;
  24364. CheckSource('TestTypeHelper_ClassProperty',
  24365. LinesToStr([ // statements
  24366. 'rtl.createHelper(this, "THelper", null, function () {',
  24367. ' this.GetSize = function () {',
  24368. ' var Result = 0;',
  24369. ' Result = $mod.THelper.GetSize() + 1;',
  24370. ' $mod.THelper.SetSize(2);',
  24371. ' return Result;',
  24372. ' };',
  24373. ' this.SetSize = function (Value) {',
  24374. ' };',
  24375. '});',
  24376. '']),
  24377. LinesToStr([ // $mod.$main
  24378. '']));
  24379. end;
  24380. procedure TTestModule.TestTypeHelper_ClassProperty_Array;
  24381. begin
  24382. StartProgram(false);
  24383. Add([
  24384. '{$modeswitch typehelpers}',
  24385. 'type',
  24386. ' THelper = type helper for word',
  24387. ' class function GetItems(Index: byte): boolean; static;',
  24388. ' class procedure SetItems(Index: byte; Value: boolean); static;',
  24389. ' class property Items[Index: byte]: boolean read GetItems write SetItems;',
  24390. ' end;',
  24391. 'class function THelper.GetItems(Index: byte): boolean;',
  24392. 'begin',
  24393. ' Result:=Items[1];',
  24394. ' Items[2]:=false;',
  24395. 'end;',
  24396. 'class procedure THelper.SetItems(Index: byte; Value: boolean);',
  24397. 'begin',
  24398. 'end;',
  24399. 'var',
  24400. ' w: word;',
  24401. ' b: boolean;',
  24402. 'begin',
  24403. ' b:=w.Items[1];',
  24404. ' w.Items[2]:=b;',
  24405. ' with w do begin',
  24406. ' b:=Items[3];',
  24407. ' Items[4]:=b;',
  24408. ' end;',
  24409. '']);
  24410. ConvertProgram;
  24411. CheckSource('TestTypeHelper_ClassProperty_Array',
  24412. LinesToStr([ // statements
  24413. 'rtl.createHelper(this, "THelper", null, function () {',
  24414. ' this.GetItems = function (Index) {',
  24415. ' var Result = false;',
  24416. ' Result = $mod.THelper.GetItems(1);',
  24417. ' $mod.THelper.SetItems(2, false);',
  24418. ' return Result;',
  24419. ' };',
  24420. ' this.SetItems = function (Index, Value) {',
  24421. ' };',
  24422. '});',
  24423. 'this.w = 0;',
  24424. 'this.b = false;',
  24425. '']),
  24426. LinesToStr([ // $mod.$main
  24427. '$mod.b = $mod.THelper.GetItems(1);',
  24428. '$mod.THelper.SetItems(2, $mod.b);',
  24429. 'var $with = $mod.w;',
  24430. '$mod.b = $mod.THelper.GetItems(3);',
  24431. '$mod.THelper.SetItems(4, $mod.b);',
  24432. '']));
  24433. end;
  24434. procedure TTestModule.TestTypeHelper_ClassMethod;
  24435. begin
  24436. StartProgram(false);
  24437. Add([
  24438. '{$modeswitch typehelpers}',
  24439. 'type',
  24440. ' THelper = type helper for word',
  24441. ' class procedure DoStatic; static;',
  24442. ' end;',
  24443. 'class procedure THelper.DoStatic;',
  24444. 'begin',
  24445. ' DoStatic;',
  24446. ' DoStatic();',
  24447. 'end;',
  24448. 'var w: word;',
  24449. 'begin',
  24450. ' w.DoStatic;',
  24451. ' w.DoStatic();',
  24452. '']);
  24453. ConvertProgram;
  24454. CheckSource('TestTypeHelper_ClassMethod',
  24455. LinesToStr([ // statements
  24456. 'rtl.createHelper(this, "THelper", null, function () {',
  24457. ' this.DoStatic = function () {',
  24458. ' $mod.THelper.DoStatic();',
  24459. ' $mod.THelper.DoStatic();',
  24460. ' };',
  24461. '});',
  24462. 'this.w = 0;',
  24463. '']),
  24464. LinesToStr([ // $mod.$main
  24465. '$mod.THelper.DoStatic();',
  24466. '$mod.THelper.DoStatic();',
  24467. '']));
  24468. end;
  24469. procedure TTestModule.TestTypeHelper_ExtClassMethodFail;
  24470. begin
  24471. StartProgram(false);
  24472. Add([
  24473. '{$modeswitch typehelpers}',
  24474. 'type',
  24475. ' THelper = type helper for word',
  24476. ' procedure Run; external name ''Run'';',
  24477. ' end;',
  24478. 'var w: word;',
  24479. 'begin',
  24480. ' w.Run;',
  24481. '']);
  24482. SetExpectedPasResolverError('Not supported: external method in type helper',nNotSupportedX);
  24483. ConvertProgram;
  24484. end;
  24485. procedure TTestModule.TestTypeHelper_Constructor;
  24486. begin
  24487. StartProgram(false);
  24488. Add([
  24489. '{$modeswitch typehelpers}',
  24490. 'type',
  24491. ' THelper = type helper for word',
  24492. ' constructor Init(e: longint);',
  24493. ' end;',
  24494. 'constructor THelper.Init(e: longint);',
  24495. 'begin',
  24496. ' Self:=e;',
  24497. ' Init(e+1);',
  24498. 'end;',
  24499. 'var w: word;',
  24500. 'begin',
  24501. ' w:=word.Init(2);',
  24502. ' w:=w.Init(3);',
  24503. ' with word do w:=Init(4);',
  24504. ' with w do w:=Init(5);',
  24505. '']);
  24506. ConvertProgram;
  24507. CheckSource('TestTypeHelper_Constructor',
  24508. LinesToStr([ // statements
  24509. 'rtl.createHelper(this, "THelper", null, function () {',
  24510. ' this.Init = function (e) {',
  24511. ' this.set(e);',
  24512. ' $mod.THelper.Init.call(this, e + 1);',
  24513. ' return this.get();',
  24514. ' };',
  24515. ' this.$new = function (fn, args) {',
  24516. ' return this[fn].apply({',
  24517. ' p: 0,',
  24518. ' get: function () {',
  24519. ' return this.p;',
  24520. ' },',
  24521. ' set: function (v) {',
  24522. ' this.p = v;',
  24523. ' }',
  24524. ' }, args);',
  24525. ' };',
  24526. '});',
  24527. 'this.w = 0;',
  24528. '']),
  24529. LinesToStr([ // $mod.$main
  24530. '$mod.w = $mod.THelper.$new("Init", [2]);',
  24531. '$mod.w = $mod.THelper.Init.call({',
  24532. ' p: $mod,',
  24533. ' get: function () {',
  24534. ' return this.p.w;',
  24535. ' },',
  24536. ' set: function (v) {',
  24537. ' this.p.w = v;',
  24538. ' }',
  24539. '}, 3);',
  24540. '$mod.w = $mod.THelper.$new("Init", [4]);',
  24541. 'var $with = $mod.w;',
  24542. '$mod.w = $mod.THelper.Init.call({',
  24543. ' get: function () {',
  24544. ' return $with;',
  24545. ' },',
  24546. ' set: function (v) {',
  24547. ' $with = v;',
  24548. ' }',
  24549. '}, 5);',
  24550. '']));
  24551. end;
  24552. procedure TTestModule.TestTypeHelper_Word;
  24553. begin
  24554. StartProgram(false);
  24555. Add([
  24556. '{$modeswitch typehelpers}',
  24557. 'type',
  24558. ' THelper = type helper for word',
  24559. ' procedure DoIt(e: byte = 123);',
  24560. ' end;',
  24561. 'procedure THelper.DoIt(e: byte);',
  24562. 'begin',
  24563. ' Self:=e;',
  24564. ' Self:=Self+1;',
  24565. ' with Self do Doit;',
  24566. 'end;',
  24567. 'begin',
  24568. ' word(3).DoIt;',
  24569. '']);
  24570. ConvertProgram;
  24571. CheckSource('TestTypeHelper_Word',
  24572. LinesToStr([ // statements
  24573. 'rtl.createHelper(this, "THelper", null, function () {',
  24574. ' this.DoIt = function (e) {',
  24575. ' this.set(e);',
  24576. ' this.set(this.get() + 1);',
  24577. ' var $with = this.get();',
  24578. ' $mod.THelper.DoIt.call(this, 123);',
  24579. ' };',
  24580. '});',
  24581. '']),
  24582. LinesToStr([ // $mod.$main
  24583. '$mod.THelper.DoIt.call({',
  24584. ' get: function () {',
  24585. ' return 3;',
  24586. ' },',
  24587. ' set: function (v) {',
  24588. ' rtl.raiseE("EPropReadOnly");',
  24589. ' }',
  24590. '}, 123);',
  24591. '']));
  24592. end;
  24593. procedure TTestModule.TestTypeHelper_Boolean;
  24594. begin
  24595. StartProgram(false);
  24596. Add([
  24597. '{$modeswitch typehelpers}',
  24598. 'type',
  24599. ' Integer = longint;',
  24600. ' THelper = type helper for boolean',
  24601. ' procedure Run(e: wordbool = true);',
  24602. ' end;',
  24603. 'procedure THelper.Run(e: wordbool);',
  24604. 'begin',
  24605. ' Self:=e;',
  24606. ' Self:=not Self;',
  24607. ' with Self do Run;',
  24608. ' if Integer(Self)=0 then ;',
  24609. 'end;',
  24610. 'begin',
  24611. ' boolean(3).Run;',
  24612. '']);
  24613. ConvertProgram;
  24614. CheckSource('TestTypeHelper_Boolean',
  24615. LinesToStr([ // statements
  24616. 'rtl.createHelper(this, "THelper", null, function () {',
  24617. ' this.Run = function (e) {',
  24618. ' this.set(e);',
  24619. ' this.set(!this.get());',
  24620. ' var $with = this.get();',
  24621. ' $mod.THelper.Run.call(this, true);',
  24622. ' if ((this.get() ? 1 : 0) === 0) ;',
  24623. ' };',
  24624. '});',
  24625. '']),
  24626. LinesToStr([ // $mod.$main
  24627. '$mod.THelper.Run.call({',
  24628. ' a: 3 != 0,',
  24629. ' get: function () {',
  24630. ' return this.a;',
  24631. ' },',
  24632. ' set: function (v) {',
  24633. ' rtl.raiseE("EPropReadOnly");',
  24634. ' }',
  24635. '}, true);',
  24636. '']));
  24637. end;
  24638. procedure TTestModule.TestTypeHelper_WordBool;
  24639. begin
  24640. StartProgram(false);
  24641. Add([
  24642. '{$modeswitch typehelpers}',
  24643. 'type',
  24644. ' Integer = longint;',
  24645. ' THelper = type helper for WordBool',
  24646. ' procedure Run(e: wordbool = true);',
  24647. ' end;',
  24648. 'procedure THelper.Run(e: wordbool);',
  24649. 'var i: integer;',
  24650. 'begin',
  24651. ' i:=Integer(Self);',
  24652. 'end;',
  24653. 'var w: wordbool;',
  24654. 'begin',
  24655. ' w.Run;',
  24656. ' wordbool(3).Run;',
  24657. '']);
  24658. ConvertProgram;
  24659. CheckSource('TestTypeHelper_WordBool',
  24660. LinesToStr([ // statements
  24661. 'rtl.createHelper(this, "THelper", null, function () {',
  24662. ' this.Run = function (e) {',
  24663. ' var i = 0;',
  24664. ' i = (this.get() ? 1 : 0);',
  24665. ' };',
  24666. '});',
  24667. 'this.w = false;',
  24668. '']),
  24669. LinesToStr([ // $mod.$main
  24670. '$mod.THelper.Run.call({',
  24671. ' p: $mod,',
  24672. ' get: function () {',
  24673. ' return this.p.w;',
  24674. ' },',
  24675. ' set: function (v) {',
  24676. ' this.p.w = v;',
  24677. ' }',
  24678. '}, true);',
  24679. '$mod.THelper.Run.call({',
  24680. ' a: 3 != 0,',
  24681. ' get: function () {',
  24682. ' return this.a;',
  24683. ' },',
  24684. ' set: function (v) {',
  24685. ' rtl.raiseE("EPropReadOnly");',
  24686. ' }',
  24687. '}, true);',
  24688. '']));
  24689. end;
  24690. procedure TTestModule.TestTypeHelper_Double;
  24691. begin
  24692. StartProgram(false);
  24693. Add([
  24694. '{$modeswitch typehelpers}',
  24695. 'type',
  24696. ' Float = type double;',
  24697. ' THelper = type helper for Float',
  24698. ' const NPI = 3.141592;',
  24699. ' function ToStr: String;',
  24700. ' end;',
  24701. 'function THelper.ToStr: String;',
  24702. 'begin',
  24703. 'end;',
  24704. 'procedure DoIt(s: string);',
  24705. 'begin',
  24706. 'end;',
  24707. 'var f: Float;',
  24708. 'begin',
  24709. ' DoIt(f.toStr);',
  24710. ' DoIt(f.toStr());',
  24711. ' (f*f).toStr;',
  24712. ' DoIt((f*f).toStr);',
  24713. '']);
  24714. ConvertProgram;
  24715. CheckSource('TestTypeHelper_Double',
  24716. LinesToStr([ // statements
  24717. 'rtl.createHelper(this, "THelper", null, function () {',
  24718. ' this.NPI = 3.141592;',
  24719. ' this.ToStr = function () {',
  24720. ' var Result = "";',
  24721. ' return Result;',
  24722. ' };',
  24723. '});',
  24724. 'this.DoIt = function (s) {',
  24725. '};',
  24726. 'this.f = 0.0;',
  24727. '']),
  24728. LinesToStr([ // $mod.$main
  24729. '$mod.DoIt($mod.THelper.ToStr.call({',
  24730. ' p: $mod,',
  24731. ' get: function () {',
  24732. ' return this.p.f;',
  24733. ' },',
  24734. ' set: function (v) {',
  24735. ' this.p.f = v;',
  24736. ' }',
  24737. '}));',
  24738. '$mod.DoIt($mod.THelper.ToStr.call({',
  24739. ' p: $mod,',
  24740. ' get: function () {',
  24741. ' return this.p.f;',
  24742. ' },',
  24743. ' set: function (v) {',
  24744. ' this.p.f = v;',
  24745. ' }',
  24746. '}));',
  24747. '$mod.THelper.ToStr.call({',
  24748. ' a: $mod.f * $mod.f,',
  24749. ' get: function () {',
  24750. ' return this.a;',
  24751. ' },',
  24752. ' set: function (v) {',
  24753. ' rtl.raiseE("EPropReadOnly");',
  24754. ' }',
  24755. '});',
  24756. '$mod.DoIt($mod.THelper.ToStr.call({',
  24757. ' a: $mod.f * $mod.f,',
  24758. ' get: function () {',
  24759. ' return this.a;',
  24760. ' },',
  24761. ' set: function (v) {',
  24762. ' rtl.raiseE("EPropReadOnly");',
  24763. ' }',
  24764. '}));',
  24765. '']));
  24766. end;
  24767. procedure TTestModule.TestTypeHelper_NativeInt;
  24768. begin
  24769. StartProgram(false);
  24770. Add([
  24771. '{$modeswitch typehelpers}',
  24772. 'type',
  24773. ' MaxInt = type nativeint;',
  24774. ' THelperI = type helper for MaxInt',
  24775. ' function ToStr: String;',
  24776. ' end;',
  24777. ' MaxUInt = type nativeuint;',
  24778. ' THelperU = type helper for MaxUInt',
  24779. ' function ToStr: String;',
  24780. ' end;',
  24781. 'function THelperI.ToStr: String;',
  24782. 'begin',
  24783. ' Result:=str(Self);',
  24784. 'end;',
  24785. 'function THelperU.ToStr: String;',
  24786. 'begin',
  24787. ' Result:=str(Self);',
  24788. 'end;',
  24789. 'procedure DoIt(s: string);',
  24790. 'begin',
  24791. 'end;',
  24792. 'var i: MaxInt;',
  24793. 'begin',
  24794. ' DoIt(i.toStr);',
  24795. ' DoIt(i.toStr());',
  24796. ' (i*i).toStr;',
  24797. ' DoIt((i*i).toStr);',
  24798. '']);
  24799. ConvertProgram;
  24800. CheckSource('TestTypeHelper_NativeInt',
  24801. LinesToStr([ // statements
  24802. 'rtl.createHelper(this, "THelperI", null, function () {',
  24803. ' this.ToStr = function () {',
  24804. ' var Result = "";',
  24805. ' Result = "" + this.get();',
  24806. ' return Result;',
  24807. ' };',
  24808. '});',
  24809. 'rtl.createHelper(this, "THelperU", null, function () {',
  24810. ' this.ToStr = function () {',
  24811. ' var Result = "";',
  24812. ' Result = "" + this.get();',
  24813. ' return Result;',
  24814. ' };',
  24815. '});',
  24816. 'this.DoIt = function (s) {',
  24817. '};',
  24818. 'this.i = 0;',
  24819. '']),
  24820. LinesToStr([ // $mod.$main
  24821. '$mod.DoIt($mod.THelperI.ToStr.call({',
  24822. ' p: $mod,',
  24823. ' get: function () {',
  24824. ' return this.p.i;',
  24825. ' },',
  24826. ' set: function (v) {',
  24827. ' this.p.i = v;',
  24828. ' }',
  24829. '}));',
  24830. '$mod.DoIt($mod.THelperI.ToStr.call({',
  24831. ' p: $mod,',
  24832. ' get: function () {',
  24833. ' return this.p.i;',
  24834. ' },',
  24835. ' set: function (v) {',
  24836. ' this.p.i = v;',
  24837. ' }',
  24838. '}));',
  24839. '$mod.THelperI.ToStr.call({',
  24840. ' a: $mod.i * $mod.i,',
  24841. ' get: function () {',
  24842. ' return this.a;',
  24843. ' },',
  24844. ' set: function (v) {',
  24845. ' rtl.raiseE("EPropReadOnly");',
  24846. ' }',
  24847. '});',
  24848. '$mod.DoIt($mod.THelperI.ToStr.call({',
  24849. ' a: $mod.i * $mod.i,',
  24850. ' get: function () {',
  24851. ' return this.a;',
  24852. ' },',
  24853. ' set: function (v) {',
  24854. ' rtl.raiseE("EPropReadOnly");',
  24855. ' }',
  24856. '}));',
  24857. '']));
  24858. end;
  24859. procedure TTestModule.TestTypeHelper_StringChar;
  24860. begin
  24861. StartProgram(false);
  24862. Add([
  24863. '{$modeswitch typehelpers}',
  24864. 'type',
  24865. ' TStringHelper = type helper for string',
  24866. ' procedure DoIt(e: byte = 123);',
  24867. ' end;',
  24868. ' TCharHelper = type helper for char',
  24869. ' procedure Fly;',
  24870. ' end;',
  24871. 'procedure TStringHelper.DoIt(e: byte);',
  24872. 'begin',
  24873. ' Self[1]:=''c'';',
  24874. ' Self[2]:=Self[3];',
  24875. 'end;',
  24876. 'procedure TCharHelper.Fly;',
  24877. 'begin',
  24878. ' Self:=''c'';',
  24879. 'end;',
  24880. 'begin',
  24881. ' ''abc''.DoIt;',
  24882. ' ''xyz''.DoIt();',
  24883. ' ''c''.Fly();',
  24884. '']);
  24885. ConvertProgram;
  24886. CheckSource('TestTypeHelper_StringChar',
  24887. LinesToStr([ // statements
  24888. 'rtl.createHelper(this, "TStringHelper", null, function () {',
  24889. ' this.DoIt = function (e) {',
  24890. ' this.set(rtl.setCharAt(this.get(), 0, "c"));',
  24891. ' this.set(rtl.setCharAt(this.get(), 1, this.get().charAt(2)));',
  24892. ' };',
  24893. '});',
  24894. 'rtl.createHelper(this, "TCharHelper", null, function () {',
  24895. ' this.Fly = function () {',
  24896. ' this.set("c");',
  24897. ' };',
  24898. '});',
  24899. '']),
  24900. LinesToStr([ // $mod.$main
  24901. '$mod.TStringHelper.DoIt.call({',
  24902. ' get: function () {',
  24903. ' return "abc";',
  24904. ' },',
  24905. ' set: function (v) {',
  24906. ' rtl.raiseE("EPropReadOnly");',
  24907. ' }',
  24908. '}, 123);',
  24909. '$mod.TStringHelper.DoIt.call({',
  24910. ' get: function () {',
  24911. ' return "xyz";',
  24912. ' },',
  24913. ' set: function (v) {',
  24914. ' rtl.raiseE("EPropReadOnly");',
  24915. ' }',
  24916. '}, 123);',
  24917. '$mod.TCharHelper.Fly.call({',
  24918. ' get: function () {',
  24919. ' return "c";',
  24920. ' },',
  24921. ' set: function (v) {',
  24922. ' rtl.raiseE("EPropReadOnly");',
  24923. ' }',
  24924. '});',
  24925. '']));
  24926. end;
  24927. procedure TTestModule.TestTypeHelper_JSValue;
  24928. begin
  24929. StartProgram(false);
  24930. Add([
  24931. '{$modeswitch typehelpers}',
  24932. 'type',
  24933. ' TExtValue = type jsvalue;',
  24934. ' THelper = type helper for TExtValue',
  24935. ' function ToStr: String;',
  24936. ' end;',
  24937. 'function THelper.ToStr: String;',
  24938. 'begin',
  24939. 'end;',
  24940. 'var',
  24941. ' s: string;',
  24942. ' v: TExtValue;',
  24943. 'begin',
  24944. ' s:=v.toStr;',
  24945. ' s:=v.toStr();',
  24946. ' TExtValue(s).toStr;',
  24947. '']);
  24948. ConvertProgram;
  24949. CheckSource('TestTypeHelper_JSValue',
  24950. LinesToStr([ // statements
  24951. 'rtl.createHelper(this, "THelper", null, function () {',
  24952. ' this.ToStr = function () {',
  24953. ' var Result = "";',
  24954. ' return Result;',
  24955. ' };',
  24956. '});',
  24957. 'this.s = "";',
  24958. 'this.v = undefined;',
  24959. '']),
  24960. LinesToStr([ // $mod.$main
  24961. '$mod.s = $mod.THelper.ToStr.call({',
  24962. ' p: $mod,',
  24963. ' get: function () {',
  24964. ' return this.p.v;',
  24965. ' },',
  24966. ' set: function (v) {',
  24967. ' this.p.v = v;',
  24968. ' }',
  24969. '});',
  24970. '$mod.s = $mod.THelper.ToStr.call({',
  24971. ' p: $mod,',
  24972. ' get: function () {',
  24973. ' return this.p.v;',
  24974. ' },',
  24975. ' set: function (v) {',
  24976. ' this.p.v = v;',
  24977. ' }',
  24978. '});',
  24979. '$mod.THelper.ToStr.call({',
  24980. ' p: $mod,',
  24981. ' get: function () {',
  24982. ' return this.p.s;',
  24983. ' },',
  24984. ' set: function (v) {',
  24985. ' rtl.raiseE("EPropReadOnly");',
  24986. ' }',
  24987. '});',
  24988. '']));
  24989. end;
  24990. procedure TTestModule.TestTypeHelper_Array;
  24991. begin
  24992. StartProgram(false);
  24993. Add([
  24994. '{$modeswitch typehelpers}',
  24995. 'type',
  24996. ' TArrOfBool = array of boolean;',
  24997. ' TArrOfJS = array of jsvalue;',
  24998. ' THelper = type helper for TArrOfBool',
  24999. ' procedure DoIt(e: byte = 123);',
  25000. ' end;',
  25001. 'procedure THelper.DoIt(e: byte);',
  25002. 'begin',
  25003. ' Self[1]:=true;',
  25004. ' Self[2]:=not Self[3];',
  25005. ' SetLength(Self,4);',
  25006. 'end;',
  25007. 'var',
  25008. ' b: TArrOfBool;',
  25009. ' j: TArrOfJS;',
  25010. 'begin',
  25011. ' b.DoIt;',
  25012. ' TArrOfBool(j).DoIt();',
  25013. '']);
  25014. ConvertProgram;
  25015. CheckSource('TestTypeHelper_Array',
  25016. LinesToStr([ // statements
  25017. 'rtl.createHelper(this, "THelper", null, function () {',
  25018. ' this.DoIt = function (e) {',
  25019. ' this.get()[1] = true;',
  25020. ' this.get()[2] = !this.get()[3];',
  25021. ' this.set(rtl.arraySetLength(this.get(), false, 4));',
  25022. ' };',
  25023. '});',
  25024. 'this.b = [];',
  25025. 'this.j = [];',
  25026. '']),
  25027. LinesToStr([ // $mod.$main
  25028. '$mod.THelper.DoIt.call({',
  25029. ' p: $mod,',
  25030. ' get: function () {',
  25031. ' return this.p.b;',
  25032. ' },',
  25033. ' set: function (v) {',
  25034. ' this.p.b = v;',
  25035. ' }',
  25036. '}, 123);',
  25037. '$mod.THelper.DoIt.call({',
  25038. ' p: $mod,',
  25039. ' get: function () {',
  25040. ' return this.p.j;',
  25041. ' },',
  25042. ' set: function (v) {',
  25043. ' this.p.j = v;',
  25044. ' }',
  25045. '}, 123);',
  25046. '']));
  25047. end;
  25048. procedure TTestModule.TestTypeHelper_EnumType;
  25049. begin
  25050. StartProgram(false);
  25051. Add([
  25052. '{$modeswitch typehelpers}',
  25053. 'type',
  25054. ' TEnum = (red,blue);',
  25055. ' THelper = type helper for TEnum',
  25056. ' procedure DoIt(e: byte = 123);',
  25057. ' class procedure Swing(w: word); static;',
  25058. ' end;',
  25059. 'procedure THelper.DoIt(e: byte);',
  25060. 'begin',
  25061. ' Self:=red;',
  25062. ' Self:=succ(Self);',
  25063. ' with Self do Doit;',
  25064. 'end;',
  25065. 'class procedure THelper.Swing(w: word);',
  25066. 'begin',
  25067. 'end;',
  25068. 'var e: TEnum;',
  25069. 'begin',
  25070. ' e.DoIt;',
  25071. ' red.DoIt;',
  25072. ' TEnum.blue.DoIt;',
  25073. ' TEnum(1).DoIt;',
  25074. ' TEnum.Swing(3);',
  25075. '']);
  25076. ConvertProgram;
  25077. CheckSource('TestTypeHelper_EnumType',
  25078. LinesToStr([ // statements
  25079. 'this.TEnum = {',
  25080. ' "0": "red",',
  25081. ' red: 0,',
  25082. ' "1": "blue",',
  25083. ' blue: 1',
  25084. '};',
  25085. 'rtl.createHelper(this, "THelper", null, function () {',
  25086. ' this.DoIt = function (e) {',
  25087. ' this.set($mod.TEnum.red);',
  25088. ' this.set(this.get() + 1);',
  25089. ' var $with = this.get();',
  25090. ' $mod.THelper.DoIt.call(this, 123);',
  25091. ' };',
  25092. ' this.Swing = function (w) {',
  25093. ' };',
  25094. '});',
  25095. 'this.e = 0;',
  25096. '']),
  25097. LinesToStr([ // $mod.$main
  25098. '$mod.THelper.DoIt.call({',
  25099. ' p: $mod,',
  25100. ' get: function () {',
  25101. ' return this.p.e;',
  25102. ' },',
  25103. ' set: function (v) {',
  25104. ' this.p.e = v;',
  25105. ' }',
  25106. '}, 123);',
  25107. '$mod.THelper.DoIt.call({',
  25108. ' p: $mod.TEnum,',
  25109. ' get: function () {',
  25110. ' return this.p.red;',
  25111. ' },',
  25112. ' set: function (v) {',
  25113. ' rtl.raiseE("EPropReadOnly");',
  25114. ' }',
  25115. '}, 123);',
  25116. '$mod.THelper.DoIt.call({',
  25117. ' p: $mod.TEnum,',
  25118. ' get: function () {',
  25119. ' return this.p.blue;',
  25120. ' },',
  25121. ' set: function (v) {',
  25122. ' rtl.raiseE("EPropReadOnly");',
  25123. ' }',
  25124. '}, 123);',
  25125. '$mod.THelper.DoIt.call({',
  25126. ' get: function () {',
  25127. ' return 1;',
  25128. ' },',
  25129. ' set: function (v) {',
  25130. ' rtl.raiseE("EPropReadOnly");',
  25131. ' }',
  25132. '}, 123);',
  25133. '$mod.THelper.Swing(3);',
  25134. '']));
  25135. end;
  25136. procedure TTestModule.TestTypeHelper_SetType;
  25137. begin
  25138. StartProgram(false);
  25139. Add([
  25140. '{$modeswitch typehelpers}',
  25141. 'type',
  25142. ' TEnum = (red,blue);',
  25143. ' TSetOfEnum = set of TEnum;',
  25144. ' THelper = type helper for TSetOfEnum',
  25145. ' procedure DoIt(e: byte = 123);',
  25146. ' constructor Init(e: TEnum);',
  25147. ' constructor InitEmpty;',
  25148. ' end;',
  25149. 'procedure THelper.DoIt(e: byte);',
  25150. 'begin',
  25151. ' Self:=[];',
  25152. ' Self:=[red];',
  25153. ' Include(Self,blue);',
  25154. 'end;',
  25155. 'constructor THelper.Init(e: TEnum);',
  25156. 'begin',
  25157. ' Self:=[];',
  25158. ' Self:=[e];',
  25159. ' Include(Self,blue);',
  25160. 'end;',
  25161. 'constructor THelper.InitEmpty;',
  25162. 'begin',
  25163. 'end;',
  25164. 'var s: TSetOfEnum;',
  25165. 'begin',
  25166. ' s.DoIt;',
  25167. //' [red].DoIt;',
  25168. //' with s do DoIt;',
  25169. //' with [red,blue] do DoIt;',
  25170. ' s:=TSetOfEnum.Init(blue);',
  25171. ' s:=s.Init(blue);',
  25172. '']);
  25173. ConvertProgram;
  25174. CheckSource('TestTypeHelper_SetType',
  25175. LinesToStr([ // statements
  25176. 'this.TEnum = {',
  25177. ' "0": "red",',
  25178. ' red: 0,',
  25179. ' "1": "blue",',
  25180. ' blue: 1',
  25181. '};',
  25182. 'rtl.createHelper(this, "THelper", null, function () {',
  25183. ' this.DoIt = function (e) {',
  25184. ' this.set({});',
  25185. ' this.set(rtl.createSet($mod.TEnum.red));',
  25186. ' this.set(rtl.includeSet(this.get(), $mod.TEnum.blue));',
  25187. ' };',
  25188. ' this.Init = function (e) {',
  25189. ' this.set({});',
  25190. ' this.set(rtl.createSet(e));',
  25191. ' this.set(rtl.includeSet(this.get(), $mod.TEnum.blue));',
  25192. ' return this.get();',
  25193. ' };',
  25194. ' this.InitEmpty = function () {',
  25195. ' return this.get();',
  25196. ' };',
  25197. ' this.$new = function (fn, args) {',
  25198. ' return this[fn].apply({',
  25199. ' p: {},',
  25200. ' get: function () {',
  25201. ' return this.p;',
  25202. ' },',
  25203. ' set: function (v) {',
  25204. ' this.p = v;',
  25205. ' }',
  25206. ' }, args);',
  25207. ' };',
  25208. '});',
  25209. 'this.s = {};',
  25210. '']),
  25211. LinesToStr([ // $mod.$main
  25212. '$mod.THelper.DoIt.call({',
  25213. ' p: $mod,',
  25214. ' get: function () {',
  25215. ' return this.p.s;',
  25216. ' },',
  25217. ' set: function (v) {',
  25218. ' this.p.s = v;',
  25219. ' }',
  25220. '}, 123);',
  25221. '$mod.s = rtl.refSet($mod.THelper.$new("Init", [$mod.TEnum.blue]));',
  25222. '$mod.s = rtl.refSet($mod.THelper.Init.call({',
  25223. ' p: $mod,',
  25224. ' get: function () {',
  25225. ' return this.p.s;',
  25226. ' },',
  25227. ' set: function (v) {',
  25228. ' this.p.s = v;',
  25229. ' }',
  25230. '}, $mod.TEnum.blue));',
  25231. '']));
  25232. end;
  25233. procedure TTestModule.TestTypeHelper_InterfaceType;
  25234. begin
  25235. StartProgram(false);
  25236. Add([
  25237. '{$interfaces com}',
  25238. '{$modeswitch typehelpers}',
  25239. 'type',
  25240. ' IUnknown = interface',
  25241. ' function _AddRef: longint;',
  25242. ' function _Release: longint;',
  25243. ' end;',
  25244. ' TObject = class(IUnknown)',
  25245. ' function _AddRef: longint; virtual; abstract;',
  25246. ' function _Release: longint; virtual; abstract;',
  25247. ' end;',
  25248. ' THelper = type helper for IUnknown',
  25249. ' procedure Fly(e: byte = 123);',
  25250. ' class procedure Run; static;',
  25251. ' end;',
  25252. 'var',
  25253. ' i: IUnknown;',
  25254. ' o: TObject;',
  25255. 'procedure THelper.Fly(e: byte);',
  25256. 'begin',
  25257. ' i:=Self;',
  25258. ' o:=Self as TObject;',
  25259. ' Self:=nil;',
  25260. ' Self:=i;',
  25261. ' Self:=o;',
  25262. ' with Self do begin',
  25263. ' Fly;',
  25264. ' Fly();',
  25265. ' end;',
  25266. 'end;',
  25267. 'class procedure THelper.Run;',
  25268. 'var l: IUnknown;',
  25269. 'begin',
  25270. ' l.Fly;',
  25271. ' l.Fly();',
  25272. 'end;',
  25273. 'begin',
  25274. ' i.Fly;',
  25275. ' i.Fly();',
  25276. ' i.Run;',
  25277. ' i.Run();',
  25278. ' IUnknown.Run;',
  25279. ' IUnknown.Run();',
  25280. '']);
  25281. ConvertProgram;
  25282. CheckSource('TestTypeHelper_InterfaceType',
  25283. LinesToStr([ // statements
  25284. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  25285. 'rtl.createClass(this, "TObject", null, function () {',
  25286. ' this.$init = function () {',
  25287. ' };',
  25288. ' this.$final = function () {',
  25289. ' };',
  25290. ' rtl.addIntf(this, $mod.IUnknown);',
  25291. '});',
  25292. 'rtl.createHelper(this, "THelper", null, function () {',
  25293. ' this.Fly = function (e) {',
  25294. ' var $ir = rtl.createIntfRefs();',
  25295. ' try {',
  25296. ' rtl.setIntfP($mod, "i", this.get());',
  25297. ' $mod.o = rtl.intfAsClass(this.get(), $mod.TObject);',
  25298. ' this.set(null);',
  25299. ' this.set($mod.i);',
  25300. ' this.set($ir.ref(1, rtl.queryIntfT($mod.o, $mod.IUnknown)));',
  25301. ' var $with = this.get();',
  25302. ' $mod.THelper.Fly.call(this, 123);',
  25303. ' $mod.THelper.Fly.call(this, 123);',
  25304. ' } finally {',
  25305. ' $ir.free();',
  25306. ' };',
  25307. ' };',
  25308. ' this.Run = function () {',
  25309. ' var l = null;',
  25310. ' try {',
  25311. ' $mod.THelper.Fly.call({',
  25312. ' get: function () {',
  25313. ' return l;',
  25314. ' },',
  25315. ' set: function (v) {',
  25316. ' l = rtl.setIntfL(l, v);',
  25317. ' }',
  25318. ' }, 123);',
  25319. ' $mod.THelper.Fly.call({',
  25320. ' get: function () {',
  25321. ' return l;',
  25322. ' },',
  25323. ' set: function (v) {',
  25324. ' l = rtl.setIntfL(l, v);',
  25325. ' }',
  25326. ' }, 123);',
  25327. ' } finally {',
  25328. ' rtl._Release(l);',
  25329. ' };',
  25330. ' };',
  25331. '});',
  25332. 'this.i = null;',
  25333. 'this.o = null;',
  25334. '']),
  25335. LinesToStr([ // $mod.$main
  25336. '$mod.THelper.Fly.call({',
  25337. ' p: $mod,',
  25338. ' get: function () {',
  25339. ' return this.p.i;',
  25340. ' },',
  25341. ' set: function (v) {',
  25342. ' rtl.setIntfP(this.p, "i", v);',
  25343. ' }',
  25344. '}, 123);',
  25345. '$mod.THelper.Fly.call({',
  25346. ' p: $mod,',
  25347. ' get: function () {',
  25348. ' return this.p.i;',
  25349. ' },',
  25350. ' set: function (v) {',
  25351. ' rtl.setIntfP(this.p, "i", v);',
  25352. ' }',
  25353. '}, 123);',
  25354. '$mod.THelper.Run();',
  25355. '$mod.THelper.Run();',
  25356. '$mod.THelper.Run();',
  25357. '$mod.THelper.Run();',
  25358. '']));
  25359. end;
  25360. procedure TTestModule.TestTypeHelper_NestedSelf;
  25361. begin
  25362. StartProgram(false);
  25363. Add([
  25364. '{$modeswitch typehelpers}',
  25365. 'type',
  25366. ' THelper = type helper for string',
  25367. ' procedure Run(Value: string);',
  25368. ' end;',
  25369. 'procedure THelper.Run(Value: string);',
  25370. ' function Sub(i: nativeint): boolean;',
  25371. ' begin',
  25372. ' Result:=Self[i+1]=Value[i];',
  25373. ' end;',
  25374. 'begin',
  25375. ' if Self[3]=Value[4] then ;',
  25376. 'end;',
  25377. 'begin',
  25378. '']);
  25379. ConvertProgram;
  25380. CheckSource('TestTypeHelper_NestedSelf',
  25381. LinesToStr([ // statements
  25382. 'rtl.createHelper(this, "THelper", null, function () {',
  25383. ' this.Run = function (Value) {',
  25384. ' var $Self = this;',
  25385. ' function Sub(i) {',
  25386. ' var Result = false;',
  25387. ' Result = $Self.get().charAt((i + 1) - 1) === Value.charAt(i - 1);',
  25388. ' return Result;',
  25389. ' };',
  25390. ' if ($Self.get().charAt(2) === Value.charAt(3)) ;',
  25391. ' };',
  25392. '});',
  25393. '']),
  25394. LinesToStr([ // $mod.$main
  25395. '']));
  25396. end;
  25397. procedure TTestModule.TestProcType;
  25398. begin
  25399. StartProgram(false);
  25400. Add([
  25401. 'type',
  25402. ' TProcInt = procedure(vI: longint = 1);',
  25403. 'procedure DoIt(vJ: longint);',
  25404. 'begin end;',
  25405. 'var',
  25406. ' b: boolean;',
  25407. ' vP, vQ: tprocint;',
  25408. 'begin',
  25409. ' vp:=nil;',
  25410. ' vp:=vp;',
  25411. ' vp:=@doit;',
  25412. ' vp;',
  25413. ' vp();',
  25414. ' vp(2);',
  25415. ' b:=vp=nil;',
  25416. ' b:=nil=vp;',
  25417. ' b:=vp=vq;',
  25418. ' b:=vp=@doit;',
  25419. ' b:=@doit=vp;',
  25420. ' b:=vp<>nil;',
  25421. ' b:=nil<>vp;',
  25422. ' b:=vp<>vq;',
  25423. ' b:=vp<>@doit;',
  25424. ' b:=@doit<>vp;',
  25425. ' b:=Assigned(vp);',
  25426. ' if Assigned(vp) then ;']);
  25427. ConvertProgram;
  25428. CheckSource('TestProcType',
  25429. LinesToStr([ // statements
  25430. 'this.DoIt = function(vJ) {',
  25431. '};',
  25432. 'this.b = false;',
  25433. 'this.vP = null;',
  25434. 'this.vQ = null;'
  25435. ]),
  25436. LinesToStr([ // $mod.$main
  25437. '$mod.vP = null;',
  25438. '$mod.vP = $mod.vP;',
  25439. '$mod.vP = $mod.DoIt;',
  25440. '$mod.vP(1);',
  25441. '$mod.vP(1);',
  25442. '$mod.vP(2);',
  25443. '$mod.b = $mod.vP === null;',
  25444. '$mod.b = null === $mod.vP;',
  25445. '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
  25446. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  25447. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  25448. '$mod.b = $mod.vP !== null;',
  25449. '$mod.b = null !== $mod.vP;',
  25450. '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
  25451. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  25452. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  25453. '$mod.b = $mod.vP != null;',
  25454. 'if ($mod.vP != null) ;',
  25455. '']));
  25456. end;
  25457. procedure TTestModule.TestProcType_Arg;
  25458. begin
  25459. StartProgram(false);
  25460. Add([
  25461. 'type',
  25462. ' TProcInt = procedure(vI: longint = 1);',
  25463. 'procedure DoIt(vJ: longint); begin end;',
  25464. 'procedure DoSome(vP, vQ: TProcInt);',
  25465. 'var',
  25466. ' b: boolean;',
  25467. 'begin',
  25468. ' vp:=nil;',
  25469. ' vp:=vp;',
  25470. ' vp:=@doit;',
  25471. ' vp;',
  25472. ' vp();',
  25473. ' vp(2);',
  25474. ' b:=vp=nil;',
  25475. ' b:=nil=vp;',
  25476. ' b:=vp=vq;',
  25477. ' b:=vp=@doit;',
  25478. ' b:=@doit=vp;',
  25479. ' b:=vp<>nil;',
  25480. ' b:=nil<>vp;',
  25481. ' b:=vp<>vq;',
  25482. ' b:=vp<>@doit;',
  25483. ' b:=@doit<>vp;',
  25484. ' b:=Assigned(vp);',
  25485. ' if Assigned(vp) then ;',
  25486. 'end;',
  25487. 'begin',
  25488. ' DoSome(@DoIt,nil);']);
  25489. ConvertProgram;
  25490. CheckSource('TestProcType_Arg',
  25491. LinesToStr([ // statements
  25492. 'this.DoIt = function(vJ) {',
  25493. '};',
  25494. 'this.DoSome = function(vP, vQ) {',
  25495. ' var b = false;',
  25496. ' vP = null;',
  25497. ' vP = vP;',
  25498. ' vP = $mod.DoIt;',
  25499. ' vP(1);',
  25500. ' vP(1);',
  25501. ' vP(2);',
  25502. ' b = vP === null;',
  25503. ' b = null === vP;',
  25504. ' b = rtl.eqCallback(vP,vQ);',
  25505. ' b = rtl.eqCallback(vP, $mod.DoIt);',
  25506. ' b = rtl.eqCallback($mod.DoIt, vP);',
  25507. ' b = vP !== null;',
  25508. ' b = null !== vP;',
  25509. ' b = !rtl.eqCallback(vP, vQ);',
  25510. ' b = !rtl.eqCallback(vP, $mod.DoIt);',
  25511. ' b = !rtl.eqCallback($mod.DoIt, vP);',
  25512. ' b = vP != null;',
  25513. ' if (vP != null) ;',
  25514. '};',
  25515. '']),
  25516. LinesToStr([ // $mod.$main
  25517. '$mod.DoSome($mod.DoIt,null);',
  25518. '']));
  25519. end;
  25520. procedure TTestModule.TestProcType_FunctionFPC;
  25521. begin
  25522. StartProgram(false);
  25523. Add('type');
  25524. Add(' TFuncInt = function(vA: longint = 1): longint;');
  25525. Add('function DoIt(vI: longint): longint;');
  25526. Add('begin end;');
  25527. Add('var');
  25528. Add(' b: boolean;');
  25529. Add(' vP, vQ: tfuncint;');
  25530. Add('begin');
  25531. Add(' vp:=nil;');
  25532. Add(' vp:=vp;');
  25533. Add(' vp:=@doit;'); // ok in fpc and delphi
  25534. //Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  25535. Add(' vp;'); // ok in fpc and delphi
  25536. Add(' vp();');
  25537. Add(' vp(2);');
  25538. Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  25539. Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  25540. Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  25541. Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  25542. Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  25543. //Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
  25544. Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
  25545. Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  25546. Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  25547. Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  25548. Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  25549. Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  25550. //Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
  25551. Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
  25552. Add(' b:=Assigned(vp);');
  25553. //Add(' doit(vp);'); // illegal in fpc, ok in delphi
  25554. Add(' doit(vp());'); // ok in fpc and delphi
  25555. Add(' doit(vp(2));'); // ok in fpc and delphi
  25556. ConvertProgram;
  25557. CheckSource('TestProcType_FunctionFPC',
  25558. LinesToStr([ // statements
  25559. 'this.DoIt = function(vI) {',
  25560. ' var Result = 0;',
  25561. ' return Result;',
  25562. '};',
  25563. 'this.b = false;',
  25564. 'this.vP = null;',
  25565. 'this.vQ = null;'
  25566. ]),
  25567. LinesToStr([ // $mod.$main
  25568. '$mod.vP = null;',
  25569. '$mod.vP = $mod.vP;',
  25570. '$mod.vP = $mod.DoIt;',
  25571. '$mod.vP(1);',
  25572. '$mod.vP(1);',
  25573. '$mod.vP(2);',
  25574. '$mod.b = $mod.vP === null;',
  25575. '$mod.b = null === $mod.vP;',
  25576. '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
  25577. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  25578. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  25579. '$mod.b = 4 === $mod.vP(1);',
  25580. '$mod.b = $mod.vP !== null;',
  25581. '$mod.b = null !== $mod.vP;',
  25582. '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
  25583. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  25584. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  25585. '$mod.b = 6 !== $mod.vP(1);',
  25586. '$mod.b = $mod.vP != null;',
  25587. '$mod.DoIt($mod.vP(1));',
  25588. '$mod.DoIt($mod.vP(2));',
  25589. '']));
  25590. end;
  25591. procedure TTestModule.TestProcType_FunctionDelphi;
  25592. begin
  25593. StartProgram(false);
  25594. Add('{$mode Delphi}');
  25595. Add('type');
  25596. Add(' TFuncInt = function(vA: longint = 1): longint;');
  25597. Add('function DoIt(vI: longint): longint;');
  25598. Add('begin end;');
  25599. Add('var');
  25600. Add(' b: boolean;');
  25601. Add(' vP, vQ: tfuncint;');
  25602. Add('begin');
  25603. Add(' vp:=nil;');
  25604. Add(' vp:=vp;');
  25605. Add(' vp:=@doit;'); // ok in fpc and delphi
  25606. Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  25607. Add(' vp;'); // ok in fpc and delphi
  25608. Add(' vp();');
  25609. Add(' vp(2);');
  25610. //Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  25611. //Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  25612. Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  25613. //Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  25614. //Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  25615. Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
  25616. Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
  25617. //Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  25618. //Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  25619. Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  25620. //Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  25621. //Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  25622. Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
  25623. Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
  25624. Add(' b:=Assigned(vp);');
  25625. Add(' doit(vp);'); // illegal in fpc, ok in delphi
  25626. Add(' doit(vp());'); // ok in fpc and delphi
  25627. Add(' doit(vp(2));'); // ok in fpc and delphi *)
  25628. ConvertProgram;
  25629. CheckSource('TestProcType_FunctionDelphi',
  25630. LinesToStr([ // statements
  25631. 'this.DoIt = function(vI) {',
  25632. ' var Result = 0;',
  25633. ' return Result;',
  25634. '};',
  25635. 'this.b = false;',
  25636. 'this.vP = null;',
  25637. 'this.vQ = null;'
  25638. ]),
  25639. LinesToStr([ // $mod.$main
  25640. '$mod.vP = null;',
  25641. '$mod.vP = $mod.vP;',
  25642. '$mod.vP = $mod.DoIt;',
  25643. '$mod.vP = $mod.DoIt;',
  25644. '$mod.vP(1);',
  25645. '$mod.vP(1);',
  25646. '$mod.vP(2);',
  25647. '$mod.b = $mod.vP(1) === $mod.vQ(1);',
  25648. '$mod.b = $mod.vP(1) === 3;',
  25649. '$mod.b = 4 === $mod.vP(1);',
  25650. '$mod.b = $mod.vP(1) !== $mod.vQ(1);',
  25651. '$mod.b = $mod.vP(1) !== 5;',
  25652. '$mod.b = 6 !== $mod.vP(1);',
  25653. '$mod.b = $mod.vP != null;',
  25654. '$mod.DoIt($mod.vP(1));',
  25655. '$mod.DoIt($mod.vP(1));',
  25656. '$mod.DoIt($mod.vP(2));',
  25657. '']));
  25658. end;
  25659. procedure TTestModule.TestProcType_ProcedureDelphi;
  25660. begin
  25661. StartProgram(false);
  25662. Add('{$mode Delphi}');
  25663. Add('type');
  25664. Add(' TProc = procedure;');
  25665. Add('procedure DoIt;');
  25666. Add('begin end;');
  25667. Add('var');
  25668. Add(' b: boolean;');
  25669. Add(' vP, vQ: tproc;');
  25670. Add('begin');
  25671. Add(' vp:=nil;');
  25672. Add(' vp:=vp;');
  25673. Add(' vp:=vq;');
  25674. 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
  25675. Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  25676. //Add(' vp:=@doit;'); // illegal in fpc, ok in delphi (because Delphi treats @F as Pointer), not supported by resolver
  25677. Add(' vp;'); // ok in fpc and delphi
  25678. Add(' vp();');
  25679. // equal
  25680. //Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  25681. Add(' b:=@@vp=nil;'); // ok in fpc delphi mode, ok in delphi
  25682. //Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  25683. Add(' b:=nil=@@vp;'); // ok in fpc delphi mode, ok in delphi
  25684. Add(' b:=@@vp=@@vq;'); // ok in fpc delphi mode, ok in Delphi
  25685. //Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  25686. //Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  25687. Add(' b:=@@vp=@doit;'); // ok in fpc delphi mode, ok in delphi
  25688. //Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  25689. Add(' b:=@doit=@@vp;'); // ok in fpc delphi mode, ok in delphi
  25690. // unequal
  25691. //Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  25692. Add(' b:=@@vp<>nil;'); // ok in fpc mode delphi, ok in delphi
  25693. //Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  25694. Add(' b:=nil<>@@vp;'); // ok in fpc mode delphi, ok in delphi
  25695. //Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  25696. Add(' b:=@@vp<>@@vq;'); // ok in fpc mode delphi, ok in delphi
  25697. //Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  25698. Add(' b:=@@vp<>@doit;'); // ok in fpc mode delphi, illegal in delphi
  25699. //Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  25700. Add(' b:=@doit<>@@vp;'); // ok in fpc mode delphi, illegal in delphi
  25701. Add(' b:=Assigned(vp);');
  25702. ConvertProgram;
  25703. CheckSource('TestProcType_ProcedureDelphi',
  25704. LinesToStr([ // statements
  25705. 'this.DoIt = function() {',
  25706. '};',
  25707. 'this.b = false;',
  25708. 'this.vP = null;',
  25709. 'this.vQ = null;'
  25710. ]),
  25711. LinesToStr([ // $mod.$main
  25712. '$mod.vP = null;',
  25713. '$mod.vP = $mod.vP;',
  25714. '$mod.vP = $mod.vQ;',
  25715. '$mod.vP = $mod.DoIt;',
  25716. '$mod.vP = $mod.DoIt;',
  25717. '$mod.vP();',
  25718. '$mod.vP();',
  25719. '$mod.b = $mod.vP === null;',
  25720. '$mod.b = null === $mod.vP;',
  25721. '$mod.b = rtl.eqCallback($mod.vP, $mod.vQ);',
  25722. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  25723. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  25724. '$mod.b = $mod.vP !== null;',
  25725. '$mod.b = null !== $mod.vP;',
  25726. '$mod.b = !rtl.eqCallback($mod.vP, $mod.vQ);',
  25727. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  25728. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  25729. '$mod.b = $mod.vP != null;',
  25730. '']));
  25731. end;
  25732. procedure TTestModule.TestProcType_AsParam;
  25733. begin
  25734. StartProgram(false);
  25735. Add('type');
  25736. Add(' TFuncInt = function(vA: longint = 1): longint;');
  25737. Add('procedure DoIt(vG: tfuncint; const vH: tfuncint; var vI: tfuncint);');
  25738. Add('var vJ: tfuncint;');
  25739. Add('begin');
  25740. Add(' vg:=vg;');
  25741. Add(' vj:=vh;');
  25742. Add(' vi:=vi;');
  25743. Add(' doit(vg,vg,vg);');
  25744. Add(' doit(vh,vh,vj);');
  25745. Add(' doit(vi,vi,vi);');
  25746. Add(' doit(vj,vj,vj);');
  25747. Add('end;');
  25748. Add('var i: tfuncint;');
  25749. Add('begin');
  25750. Add(' doit(i,i,i);');
  25751. ConvertProgram;
  25752. CheckSource('TestProcType_AsParam',
  25753. LinesToStr([ // statements
  25754. 'this.DoIt = function (vG,vH,vI) {',
  25755. ' var vJ = null;',
  25756. ' vG = vG;',
  25757. ' vJ = vH;',
  25758. ' vI.set(vI.get());',
  25759. ' $mod.DoIt(vG, vG, {',
  25760. ' get: function () {',
  25761. ' return vG;',
  25762. ' },',
  25763. ' set: function (v) {',
  25764. ' vG = v;',
  25765. ' }',
  25766. ' });',
  25767. ' $mod.DoIt(vH, vH, {',
  25768. ' get: function () {',
  25769. ' return vJ;',
  25770. ' },',
  25771. ' set: function (v) {',
  25772. ' vJ = v;',
  25773. ' }',
  25774. ' });',
  25775. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  25776. ' $mod.DoIt(vJ, vJ, {',
  25777. ' get: function () {',
  25778. ' return vJ;',
  25779. ' },',
  25780. ' set: function (v) {',
  25781. ' vJ = v;',
  25782. ' }',
  25783. ' });',
  25784. '};',
  25785. 'this.i = null;'
  25786. ]),
  25787. LinesToStr([
  25788. '$mod.DoIt($mod.i,$mod.i,{',
  25789. ' p: $mod,',
  25790. ' get: function () {',
  25791. ' return this.p.i;',
  25792. ' },',
  25793. ' set: function (v) {',
  25794. ' this.p.i = v;',
  25795. ' }',
  25796. '});'
  25797. ]));
  25798. end;
  25799. procedure TTestModule.TestProcType_MethodFPC;
  25800. begin
  25801. StartProgram(false);
  25802. Add('type');
  25803. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  25804. Add(' TObject = class');
  25805. Add(' function DoIt(vA: longint = 1): longint;');
  25806. Add(' end;');
  25807. Add('function TObject.DoIt(vA: longint = 1): longint;');
  25808. Add('begin');
  25809. Add('end;');
  25810. Add('var');
  25811. Add(' Obj: TObject;');
  25812. Add(' vP: tfuncint;');
  25813. Add(' b: boolean;');
  25814. Add('begin');
  25815. Add(' vp:[email protected];'); // ok in fpc and delphi
  25816. //Add(' vp:=obj.doit;'); // illegal in fpc, ok in delphi
  25817. Add(' vp;'); // ok in fpc and delphi
  25818. Add(' vp();');
  25819. Add(' vp(2);');
  25820. Add(' b:[email protected];'); // ok in fpc, illegal in delphi
  25821. Add(' b:[email protected]=vp;'); // ok in fpc, illegal in delphi
  25822. Add(' b:=vp<>@obj.doit;'); // ok in fpc, illegal in delphi
  25823. Add(' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
  25824. ConvertProgram;
  25825. CheckSource('TestProcType_MethodFPC',
  25826. LinesToStr([ // statements
  25827. 'rtl.createClass(this, "TObject", null, function () {',
  25828. ' this.$init = function () {',
  25829. ' };',
  25830. ' this.$final = function () {',
  25831. ' };',
  25832. ' this.DoIt = function (vA) {',
  25833. ' var Result = 0;',
  25834. ' return Result;',
  25835. ' };',
  25836. '});',
  25837. 'this.Obj = null;',
  25838. 'this.vP = null;',
  25839. 'this.b = false;'
  25840. ]),
  25841. LinesToStr([
  25842. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  25843. '$mod.vP(1);',
  25844. '$mod.vP(1);',
  25845. '$mod.vP(2);',
  25846. '$mod.b = rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
  25847. '$mod.b = rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
  25848. '$mod.b = !rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
  25849. '$mod.b = !rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
  25850. '']));
  25851. end;
  25852. procedure TTestModule.TestProcType_MethodDelphi;
  25853. begin
  25854. StartProgram(false);
  25855. Add([
  25856. '{$mode delphi}',
  25857. 'type',
  25858. ' TFuncInt = function(vA: longint = 1): longint of object;',
  25859. ' TObject = class',
  25860. ' function DoIt(vA: longint = 1): longint;',
  25861. ' end;',
  25862. 'function TObject.DoIt(vA: longint = 1): longint;',
  25863. 'begin',
  25864. 'end;',
  25865. 'var',
  25866. ' Obj: TObject;',
  25867. ' vP: tfuncint;',
  25868. ' b: boolean;',
  25869. 'begin',
  25870. ' vp:[email protected];', // ok in fpc and delphi
  25871. ' vp:=obj.doit;', // illegal in fpc, ok in delphi
  25872. ' vp;', // ok in fpc and delphi
  25873. ' vp();',
  25874. ' vp(2);',
  25875. //' b:[email protected];', // ok in fpc, illegal in delphi
  25876. //' b:[email protected]=vp;', // ok in fpc, illegal in delphi
  25877. //' b:=vp<>@obj.doit;', // ok in fpc, illegal in delphi
  25878. //' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
  25879. '']);
  25880. ConvertProgram;
  25881. CheckSource('TestProcType_MethodDelphi',
  25882. LinesToStr([ // statements
  25883. 'rtl.createClass(this, "TObject", null, function () {',
  25884. ' this.$init = function () {',
  25885. ' };',
  25886. ' this.$final = function () {',
  25887. ' };',
  25888. ' this.DoIt = function (vA) {',
  25889. ' var Result = 0;',
  25890. ' return Result;',
  25891. ' };',
  25892. '});',
  25893. 'this.Obj = null;',
  25894. 'this.vP = null;',
  25895. 'this.b = false;'
  25896. ]),
  25897. LinesToStr([
  25898. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  25899. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  25900. '$mod.vP(1);',
  25901. '$mod.vP(1);',
  25902. '$mod.vP(2);',
  25903. '']));
  25904. end;
  25905. procedure TTestModule.TestProcType_PropertyFPC;
  25906. begin
  25907. StartProgram(false);
  25908. Add('type');
  25909. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  25910. Add(' TObject = class');
  25911. Add(' FOnFoo: TFuncInt;');
  25912. Add(' function DoIt(vA: longint = 1): longint;');
  25913. Add(' function GetFoo: TFuncInt;');
  25914. Add(' procedure SetFoo(const Value: TFuncInt);');
  25915. Add(' function GetEvents(Index: longint): TFuncInt;');
  25916. Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
  25917. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  25918. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  25919. Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
  25920. Add(' end;');
  25921. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  25922. Add('function tobject.getfoo: tfuncint; begin end;');
  25923. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  25924. Add('function tobject.getevents(index: longint): tfuncint; begin end;');
  25925. Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
  25926. Add('var');
  25927. Add(' Obj: TObject;');
  25928. Add(' vP: tfuncint;');
  25929. Add(' b: boolean;');
  25930. Add('begin');
  25931. Add(' obj.onfoo:=nil;');
  25932. Add(' obj.onbar:=nil;');
  25933. Add(' obj.events[1]:=nil;');
  25934. Add(' obj.onfoo:=obj.onfoo;');
  25935. Add(' obj.onbar:=obj.onbar;');
  25936. Add(' obj.events[2]:=obj.events[3];');
  25937. Add(' obj.onfoo:[email protected];');
  25938. Add(' obj.onbar:[email protected];');
  25939. Add(' obj.events[4]:[email protected];');
  25940. //Add(' obj.onfoo:=obj.doit;'); // delphi
  25941. //Add(' obj.onbar:=obj.doit;'); // delphi
  25942. //Add(' obj.events[4]:=obj.doit;'); // delphi
  25943. Add(' obj.onfoo;');
  25944. Add(' obj.onbar;');
  25945. //Add(' obj.events[5];'); ToDo in pasresolver
  25946. Add(' obj.onfoo();');
  25947. Add(' obj.onbar();');
  25948. Add(' obj.events[6]();');
  25949. Add(' b:=obj.onfoo=nil;');
  25950. Add(' b:=obj.onbar=nil;');
  25951. Add(' b:=obj.events[7]=nil;');
  25952. Add(' b:=obj.onfoo<>nil;');
  25953. Add(' b:=obj.onbar<>nil;');
  25954. Add(' b:=obj.events[8]<>nil;');
  25955. Add(' b:=obj.onfoo=vp;');
  25956. Add(' b:=obj.onbar=vp;');
  25957. Add(' b:=obj.events[9]=vp;');
  25958. Add(' b:=obj.onfoo=obj.onfoo;');
  25959. Add(' b:=obj.onbar=obj.onfoo;');
  25960. Add(' b:=obj.events[10]=obj.onfoo;');
  25961. Add(' b:=obj.onfoo<>obj.onfoo;');
  25962. Add(' b:=obj.onbar<>obj.onfoo;');
  25963. Add(' b:=obj.events[11]<>obj.onfoo;');
  25964. Add(' b:[email protected];');
  25965. Add(' b:[email protected];');
  25966. Add(' b:=obj.events[12][email protected];');
  25967. Add(' b:=obj.onfoo<>@obj.doit;');
  25968. Add(' b:=obj.onbar<>@obj.doit;');
  25969. Add(' b:=obj.events[12]<>@obj.doit;');
  25970. Add(' b:=Assigned(obj.onfoo);');
  25971. Add(' b:=Assigned(obj.onbar);');
  25972. Add(' b:=Assigned(obj.events[13]);');
  25973. ConvertProgram;
  25974. CheckSource('TestProcType_PropertyFPC',
  25975. LinesToStr([ // statements
  25976. 'rtl.createClass(this, "TObject", null, function () {',
  25977. ' this.$init = function () {',
  25978. ' this.FOnFoo = null;',
  25979. ' };',
  25980. ' this.$final = function () {',
  25981. ' this.FOnFoo = undefined;',
  25982. ' };',
  25983. ' this.DoIt = function (vA) {',
  25984. ' var Result = 0;',
  25985. ' return Result;',
  25986. ' };',
  25987. 'this.GetFoo = function () {',
  25988. ' var Result = null;',
  25989. ' return Result;',
  25990. '};',
  25991. 'this.SetFoo = function (Value) {',
  25992. '};',
  25993. 'this.GetEvents = function (Index) {',
  25994. ' var Result = null;',
  25995. ' return Result;',
  25996. '};',
  25997. 'this.SetEvents = function (Index, Value) {',
  25998. '};',
  25999. '});',
  26000. 'this.Obj = null;',
  26001. 'this.vP = null;',
  26002. 'this.b = false;'
  26003. ]),
  26004. LinesToStr([
  26005. '$mod.Obj.FOnFoo = null;',
  26006. '$mod.Obj.SetFoo(null);',
  26007. '$mod.Obj.SetEvents(1, null);',
  26008. '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
  26009. '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
  26010. '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
  26011. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  26012. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  26013. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  26014. '$mod.Obj.FOnFoo(1);',
  26015. '$mod.Obj.GetFoo();',
  26016. '$mod.Obj.FOnFoo(1);',
  26017. '$mod.Obj.GetFoo()(1);',
  26018. '$mod.Obj.GetEvents(6)(1);',
  26019. '$mod.b = $mod.Obj.FOnFoo === null;',
  26020. '$mod.b = $mod.Obj.GetFoo() === null;',
  26021. '$mod.b = $mod.Obj.GetEvents(7) === null;',
  26022. '$mod.b = $mod.Obj.FOnFoo !== null;',
  26023. '$mod.b = $mod.Obj.GetFoo() !== null;',
  26024. '$mod.b = $mod.Obj.GetEvents(8) !== null;',
  26025. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.vP);',
  26026. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.vP);',
  26027. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(9), $mod.vP);',
  26028. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
  26029. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
  26030. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(10), $mod.Obj.FOnFoo);',
  26031. '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
  26032. '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
  26033. '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(11), $mod.Obj.FOnFoo);',
  26034. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
  26035. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
  26036. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
  26037. '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
  26038. '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
  26039. '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
  26040. '$mod.b = $mod.Obj.FOnFoo != null;',
  26041. '$mod.b = $mod.Obj.GetFoo() != null;',
  26042. '$mod.b = $mod.Obj.GetEvents(13) != null;',
  26043. '']));
  26044. end;
  26045. procedure TTestModule.TestProcType_PropertyDelphi;
  26046. begin
  26047. StartProgram(false);
  26048. Add('{$mode delphi}');
  26049. Add('type');
  26050. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  26051. Add(' TObject = class');
  26052. Add(' FOnFoo: TFuncInt;');
  26053. Add(' function DoIt(vA: longint = 1): longint;');
  26054. Add(' function GetFoo: TFuncInt;');
  26055. Add(' procedure SetFoo(const Value: TFuncInt);');
  26056. Add(' function GetEvents(Index: longint): TFuncInt;');
  26057. Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
  26058. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  26059. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  26060. Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
  26061. Add(' end;');
  26062. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  26063. Add('function tobject.getfoo: tfuncint; begin end;');
  26064. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  26065. Add('function tobject.getevents(index: longint): tfuncint; begin end;');
  26066. Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
  26067. Add('var');
  26068. Add(' Obj: TObject;');
  26069. Add(' vP: tfuncint;');
  26070. Add(' b: boolean;');
  26071. Add('begin');
  26072. Add(' obj.onfoo:=nil;');
  26073. Add(' obj.onbar:=nil;');
  26074. Add(' obj.events[1]:=nil;');
  26075. Add(' obj.onfoo:=obj.onfoo;');
  26076. Add(' obj.onbar:=obj.onbar;');
  26077. Add(' obj.events[2]:=obj.events[3];');
  26078. Add(' obj.onfoo:[email protected];');
  26079. Add(' obj.onbar:[email protected];');
  26080. Add(' obj.events[4]:[email protected];');
  26081. Add(' obj.onfoo:=obj.doit;'); // delphi
  26082. Add(' obj.onbar:=obj.doit;'); // delphi
  26083. Add(' obj.events[4]:=obj.doit;'); // delphi
  26084. Add(' obj.onfoo;');
  26085. Add(' obj.onbar;');
  26086. //Add(' obj.events[5];'); ToDo in pasresolver
  26087. Add(' obj.onfoo();');
  26088. Add(' obj.onbar();');
  26089. Add(' obj.events[6]();');
  26090. //Add(' b:=obj.onfoo=nil;'); // fpc
  26091. //Add(' b:=obj.onbar=nil;'); // fpc
  26092. //Add(' b:=obj.events[7]=nil;'); // fpc
  26093. //Add(' b:=obj.onfoo<>nil;'); // fpc
  26094. //Add(' b:=obj.onbar<>nil;'); // fpc
  26095. //Add(' b:=obj.events[8]<>nil;'); // fpc
  26096. Add(' b:=obj.onfoo=vp;');
  26097. Add(' b:=obj.onbar=vp;');
  26098. //Add(' b:=obj.events[9]=vp;'); ToDo in pasresolver
  26099. Add(' b:=obj.onfoo=obj.onfoo;');
  26100. Add(' b:=obj.onbar=obj.onfoo;');
  26101. //Add(' b:=obj.events[10]=obj.onfoo;'); // ToDo in pasresolver
  26102. Add(' b:=obj.onfoo<>obj.onfoo;');
  26103. Add(' b:=obj.onbar<>obj.onfoo;');
  26104. //Add(' b:=obj.events[11]<>obj.onfoo;'); // ToDo in pasresolver
  26105. //Add(' b:[email protected];'); // fpc
  26106. //Add(' b:[email protected];'); // fpc
  26107. //Add(' b:=obj.events[12][email protected];'); // fpc
  26108. //Add(' b:=obj.onfoo<>@obj.doit;'); // fpc
  26109. //Add(' b:=obj.onbar<>@obj.doit;'); // fpc
  26110. //Add(' b:=obj.events[12]<>@obj.doit;'); // fpc
  26111. Add(' b:=Assigned(obj.onfoo);');
  26112. Add(' b:=Assigned(obj.onbar);');
  26113. Add(' b:=Assigned(obj.events[13]);');
  26114. ConvertProgram;
  26115. CheckSource('TestProcType_PropertyDelphi',
  26116. LinesToStr([ // statements
  26117. 'rtl.createClass(this, "TObject", null, function () {',
  26118. ' this.$init = function () {',
  26119. ' this.FOnFoo = null;',
  26120. ' };',
  26121. ' this.$final = function () {',
  26122. ' this.FOnFoo = undefined;',
  26123. ' };',
  26124. ' this.DoIt = function (vA) {',
  26125. ' var Result = 0;',
  26126. ' return Result;',
  26127. ' };',
  26128. 'this.GetFoo = function () {',
  26129. ' var Result = null;',
  26130. ' return Result;',
  26131. '};',
  26132. 'this.SetFoo = function (Value) {',
  26133. '};',
  26134. 'this.GetEvents = function (Index) {',
  26135. ' var Result = null;',
  26136. ' return Result;',
  26137. '};',
  26138. 'this.SetEvents = function (Index, Value) {',
  26139. '};',
  26140. '});',
  26141. 'this.Obj = null;',
  26142. 'this.vP = null;',
  26143. 'this.b = false;'
  26144. ]),
  26145. LinesToStr([
  26146. '$mod.Obj.FOnFoo = null;',
  26147. '$mod.Obj.SetFoo(null);',
  26148. '$mod.Obj.SetEvents(1, null);',
  26149. '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
  26150. '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
  26151. '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
  26152. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  26153. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  26154. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  26155. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  26156. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  26157. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  26158. '$mod.Obj.FOnFoo(1);',
  26159. '$mod.Obj.GetFoo();',
  26160. '$mod.Obj.FOnFoo(1);',
  26161. '$mod.Obj.GetFoo()(1);',
  26162. '$mod.Obj.GetEvents(6)(1);',
  26163. '$mod.b = $mod.Obj.FOnFoo(1) === $mod.vP(1);',
  26164. '$mod.b = $mod.Obj.GetFoo() === $mod.vP(1);',
  26165. '$mod.b = $mod.Obj.FOnFoo(1) === $mod.Obj.FOnFoo(1);',
  26166. '$mod.b = $mod.Obj.GetFoo() === $mod.Obj.FOnFoo(1);',
  26167. '$mod.b = $mod.Obj.FOnFoo(1) !== $mod.Obj.FOnFoo(1);',
  26168. '$mod.b = $mod.Obj.GetFoo() !== $mod.Obj.FOnFoo(1);',
  26169. '$mod.b = $mod.Obj.FOnFoo != null;',
  26170. '$mod.b = $mod.Obj.GetFoo() != null;',
  26171. '$mod.b = $mod.Obj.GetEvents(13) != null;',
  26172. '']));
  26173. end;
  26174. procedure TTestModule.TestProcType_WithClassInstDoPropertyFPC;
  26175. begin
  26176. StartProgram(false);
  26177. Add('type');
  26178. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  26179. Add(' TObject = class');
  26180. Add(' FOnFoo: TFuncInt;');
  26181. Add(' function DoIt(vA: longint = 1): longint;');
  26182. Add(' function GetFoo: TFuncInt;');
  26183. Add(' procedure SetFoo(const Value: TFuncInt);');
  26184. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  26185. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  26186. Add(' end;');
  26187. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  26188. Add('function tobject.getfoo: tfuncint; begin end;');
  26189. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  26190. Add('var');
  26191. Add(' Obj: TObject;');
  26192. Add(' vP: tfuncint;');
  26193. Add(' b: boolean;');
  26194. Add('begin');
  26195. Add('with obj do begin');
  26196. Add(' fonfoo:=nil;');
  26197. Add(' onfoo:=nil;');
  26198. Add(' onbar:=nil;');
  26199. Add(' fonfoo:=fonfoo;');
  26200. Add(' onfoo:=onfoo;');
  26201. Add(' onbar:=onbar;');
  26202. Add(' fonfoo:=@doit;');
  26203. Add(' onfoo:=@doit;');
  26204. Add(' onbar:=@doit;');
  26205. //Add(' fonfoo:=doit;'); // delphi
  26206. //Add(' onfoo:=doit;'); // delphi
  26207. //Add(' onbar:=doit;'); // delphi
  26208. Add(' fonfoo;');
  26209. Add(' onfoo;');
  26210. Add(' onbar;');
  26211. Add(' fonfoo();');
  26212. Add(' onfoo();');
  26213. Add(' onbar();');
  26214. Add(' b:=fonfoo=nil;');
  26215. Add(' b:=onfoo=nil;');
  26216. Add(' b:=onbar=nil;');
  26217. Add(' b:=fonfoo<>nil;');
  26218. Add(' b:=onfoo<>nil;');
  26219. Add(' b:=onbar<>nil;');
  26220. Add(' b:=fonfoo=vp;');
  26221. Add(' b:=onfoo=vp;');
  26222. Add(' b:=onbar=vp;');
  26223. Add(' b:=fonfoo=fonfoo;');
  26224. Add(' b:=onfoo=onfoo;');
  26225. Add(' b:=onbar=onfoo;');
  26226. Add(' b:=fonfoo<>fonfoo;');
  26227. Add(' b:=onfoo<>onfoo;');
  26228. Add(' b:=onbar<>onfoo;');
  26229. Add(' b:=fonfoo=@doit;');
  26230. Add(' b:=onfoo=@doit;');
  26231. Add(' b:=onbar=@doit;');
  26232. Add(' b:=fonfoo<>@doit;');
  26233. Add(' b:=onfoo<>@doit;');
  26234. Add(' b:=onbar<>@doit;');
  26235. Add(' b:=Assigned(fonfoo);');
  26236. Add(' b:=Assigned(onfoo);');
  26237. Add(' b:=Assigned(onbar);');
  26238. Add('end;');
  26239. ConvertProgram;
  26240. CheckSource('TestProcType_WithClassInstDoPropertyFPC',
  26241. LinesToStr([ // statements
  26242. 'rtl.createClass(this, "TObject", null, function () {',
  26243. ' this.$init = function () {',
  26244. ' this.FOnFoo = null;',
  26245. ' };',
  26246. ' this.$final = function () {',
  26247. ' this.FOnFoo = undefined;',
  26248. ' };',
  26249. ' this.DoIt = function (vA) {',
  26250. ' var Result = 0;',
  26251. ' return Result;',
  26252. ' };',
  26253. ' this.GetFoo = function () {',
  26254. ' var Result = null;',
  26255. ' return Result;',
  26256. ' };',
  26257. ' this.SetFoo = function (Value) {',
  26258. ' };',
  26259. '});',
  26260. 'this.Obj = null;',
  26261. 'this.vP = null;',
  26262. 'this.b = false;'
  26263. ]),
  26264. LinesToStr([
  26265. 'var $with = $mod.Obj;',
  26266. '$with.FOnFoo = null;',
  26267. '$with.FOnFoo = null;',
  26268. '$with.SetFoo(null);',
  26269. '$with.FOnFoo = $with.FOnFoo;',
  26270. '$with.FOnFoo = $with.FOnFoo;',
  26271. '$with.SetFoo($with.GetFoo());',
  26272. '$with.FOnFoo = rtl.createCallback($with, "DoIt");',
  26273. '$with.FOnFoo = rtl.createCallback($with, "DoIt");',
  26274. '$with.SetFoo(rtl.createCallback($with, "DoIt"));',
  26275. '$with.FOnFoo(1);',
  26276. '$with.FOnFoo(1);',
  26277. '$with.GetFoo();',
  26278. '$with.FOnFoo(1);',
  26279. '$with.FOnFoo(1);',
  26280. '$with.GetFoo()(1);',
  26281. '$mod.b = $with.FOnFoo === null;',
  26282. '$mod.b = $with.FOnFoo === null;',
  26283. '$mod.b = $with.GetFoo() === null;',
  26284. '$mod.b = $with.FOnFoo !== null;',
  26285. '$mod.b = $with.FOnFoo !== null;',
  26286. '$mod.b = $with.GetFoo() !== null;',
  26287. '$mod.b = rtl.eqCallback($with.FOnFoo, $mod.vP);',
  26288. '$mod.b = rtl.eqCallback($with.FOnFoo, $mod.vP);',
  26289. '$mod.b = rtl.eqCallback($with.GetFoo(), $mod.vP);',
  26290. '$mod.b = rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  26291. '$mod.b = rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  26292. '$mod.b = rtl.eqCallback($with.GetFoo(), $with.FOnFoo);',
  26293. '$mod.b = !rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  26294. '$mod.b = !rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  26295. '$mod.b = !rtl.eqCallback($with.GetFoo(), $with.FOnFoo);',
  26296. '$mod.b = rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  26297. '$mod.b = rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  26298. '$mod.b = rtl.eqCallback($with.GetFoo(), rtl.createCallback($with, "DoIt"));',
  26299. '$mod.b = !rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  26300. '$mod.b = !rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  26301. '$mod.b = !rtl.eqCallback($with.GetFoo(), rtl.createCallback($with, "DoIt"));',
  26302. '$mod.b = $with.FOnFoo != null;',
  26303. '$mod.b = $with.FOnFoo != null;',
  26304. '$mod.b = $with.GetFoo() != null;',
  26305. '']));
  26306. end;
  26307. procedure TTestModule.TestProcType_Nested;
  26308. begin
  26309. StartProgram(false);
  26310. Add([
  26311. 'type',
  26312. ' TProcInt = procedure(vI: longint = 1);',
  26313. 'procedure DoIt(vJ: longint);',
  26314. 'var aProc: TProcInt;',
  26315. ' b: boolean;',
  26316. ' procedure Sub(vK: longint);',
  26317. ' var aSub: TProcInt;',
  26318. ' procedure SubSub(vK: longint);',
  26319. ' var aSubSub: TProcInt;',
  26320. ' begin;',
  26321. ' aProc:=@DoIt;',
  26322. ' aSub:=@DoIt;',
  26323. ' aSubSub:=@DoIt;',
  26324. ' aProc:=@Sub;',
  26325. ' aSub:=@Sub;',
  26326. ' aSubSub:=@Sub;',
  26327. ' aProc:=@SubSub;',
  26328. ' aSub:=@SubSub;',
  26329. ' aSubSub:=@SubSub;',
  26330. ' end;',
  26331. ' begin;',
  26332. ' end;',
  26333. 'begin;',
  26334. ' aProc:=@Sub;',
  26335. ' b:=aProc=@Sub;',
  26336. ' b:=@Sub=aProc;',
  26337. 'end;',
  26338. 'begin',
  26339. '']);
  26340. ConvertProgram;
  26341. CheckSource('TestProcType_Nested',
  26342. LinesToStr([ // statements
  26343. 'this.DoIt = function (vJ) {',
  26344. ' var aProc = null;',
  26345. ' var b = false;',
  26346. ' function Sub(vK) {',
  26347. ' var aSub = null;',
  26348. ' function SubSub(vK) {',
  26349. ' var aSubSub = null;',
  26350. ' aProc = $mod.DoIt;',
  26351. ' aSub = $mod.DoIt;',
  26352. ' aSubSub = $mod.DoIt;',
  26353. ' aProc = Sub;',
  26354. ' aSub = Sub;',
  26355. ' aSubSub = Sub;',
  26356. ' aProc = SubSub;',
  26357. ' aSub = SubSub;',
  26358. ' aSubSub = SubSub;',
  26359. ' };',
  26360. ' };',
  26361. ' aProc = Sub;',
  26362. ' b = rtl.eqCallback(aProc, Sub);',
  26363. ' b = rtl.eqCallback(Sub, aProc);',
  26364. '};',
  26365. '']),
  26366. LinesToStr([ // $mod.$main
  26367. '']));
  26368. end;
  26369. procedure TTestModule.TestProcType_NestedOfObject;
  26370. begin
  26371. StartProgram(false);
  26372. Add([
  26373. 'type',
  26374. ' TProcInt = procedure(vI: longint = 1) of object;',
  26375. ' TObject = class',
  26376. ' procedure DoIt(vJ: longint);',
  26377. ' end;',
  26378. 'procedure TObject.DoIt(vJ: longint);',
  26379. 'var aProc: TProcInt;',
  26380. ' b: boolean;',
  26381. ' procedure Sub(vK: longint);',
  26382. ' var aSub: TProcInt;',
  26383. ' procedure SubSub(vK: longint);',
  26384. ' var aSubSub: TProcInt;',
  26385. ' begin;',
  26386. ' aProc:=@DoIt;',
  26387. ' aSub:=@DoIt;',
  26388. ' aSubSub:=@DoIt;',
  26389. ' aProc:=@Sub;',
  26390. ' aSub:=@Sub;',
  26391. ' aSubSub:=@Sub;',
  26392. ' aProc:=@SubSub;',
  26393. ' aSub:=@SubSub;',
  26394. ' aSubSub:=@SubSub;',
  26395. ' end;',
  26396. ' begin;',
  26397. ' end;',
  26398. 'begin;',
  26399. ' aProc:=@Sub;',
  26400. ' b:=aProc=@Sub;',
  26401. ' b:=@Sub=aProc;',
  26402. 'end;',
  26403. 'begin',
  26404. '']);
  26405. ConvertProgram;
  26406. CheckSource('TestProcType_Nested',
  26407. LinesToStr([ // statements
  26408. 'rtl.createClass(this, "TObject", null, function () {',
  26409. ' this.$init = function () {',
  26410. ' };',
  26411. ' this.$final = function () {',
  26412. ' };',
  26413. ' this.DoIt = function (vJ) {',
  26414. ' var $Self = this;',
  26415. ' var aProc = null;',
  26416. ' var b = false;',
  26417. ' function Sub(vK) {',
  26418. ' var aSub = null;',
  26419. ' function SubSub(vK) {',
  26420. ' var aSubSub = null;',
  26421. ' aProc = rtl.createCallback($Self, "DoIt");',
  26422. ' aSub = rtl.createCallback($Self, "DoIt");',
  26423. ' aSubSub = rtl.createCallback($Self, "DoIt");',
  26424. ' aProc = Sub;',
  26425. ' aSub = Sub;',
  26426. ' aSubSub = Sub;',
  26427. ' aProc = SubSub;',
  26428. ' aSub = SubSub;',
  26429. ' aSubSub = SubSub;',
  26430. ' };',
  26431. ' };',
  26432. ' aProc = Sub;',
  26433. ' b = rtl.eqCallback(aProc, Sub);',
  26434. ' b = rtl.eqCallback(Sub, aProc);',
  26435. ' };',
  26436. '});',
  26437. '']),
  26438. LinesToStr([ // $mod.$main
  26439. '']));
  26440. end;
  26441. procedure TTestModule.TestProcType_ReferenceToProc;
  26442. begin
  26443. StartProgram(false);
  26444. Add([
  26445. 'type',
  26446. ' TProcRef = reference to procedure(i: longint = 0);',
  26447. ' TFuncRef = reference to function(i: longint = 0): longint;',
  26448. 'var',
  26449. ' p: TProcRef;',
  26450. ' f: TFuncRef;',
  26451. 'procedure DoIt(i: longint);',
  26452. 'begin',
  26453. 'end;',
  26454. 'function GetIt(i: longint): longint;',
  26455. 'begin',
  26456. ' p:=@DoIt;',
  26457. ' f:=@GetIt;',
  26458. ' f;',
  26459. ' f();',
  26460. ' f(1);',
  26461. 'end;',
  26462. 'begin',
  26463. ' p:=@DoIt;',
  26464. ' f:=@GetIt;',
  26465. ' f;',
  26466. ' f();',
  26467. ' f(1);',
  26468. ' p:=TProcRef(f);',
  26469. '']);
  26470. ConvertProgram;
  26471. CheckSource('TestProcType_ReferenceToProc',
  26472. LinesToStr([ // statements
  26473. 'this.p = null;',
  26474. 'this.f = null;',
  26475. 'this.DoIt = function (i) {',
  26476. '};',
  26477. 'this.GetIt = function (i) {',
  26478. ' var Result = 0;',
  26479. ' $mod.p = $mod.DoIt;',
  26480. ' $mod.f = $mod.GetIt;',
  26481. ' $mod.f(0);',
  26482. ' $mod.f(0);',
  26483. ' $mod.f(1);',
  26484. ' return Result;',
  26485. '};',
  26486. '']),
  26487. LinesToStr([ // $mod.$main
  26488. '$mod.p = $mod.DoIt;',
  26489. '$mod.f = $mod.GetIt;',
  26490. '$mod.f(0);',
  26491. '$mod.f(0);',
  26492. '$mod.f(1);',
  26493. '$mod.p = $mod.f;',
  26494. '']));
  26495. end;
  26496. procedure TTestModule.TestProcType_ReferenceToMethod;
  26497. begin
  26498. StartProgram(false);
  26499. Add([
  26500. 'type',
  26501. ' TFuncRef = reference to function(i: longint = 5): longint;',
  26502. ' TObject = class',
  26503. ' function Grow(s: longint): longint;',
  26504. ' end;',
  26505. 'var',
  26506. ' f: tfuncref;',
  26507. 'function tobject.grow(s: longint): longint;',
  26508. ' function GrowSub(i: longint): longint;',
  26509. ' begin',
  26510. ' f:=@grow;',
  26511. ' f:=@growsub;',
  26512. ' end;',
  26513. 'begin',
  26514. ' f:=@grow;',
  26515. ' f:=@growsub;',
  26516. 'end;',
  26517. 'begin',
  26518. '']);
  26519. ConvertProgram;
  26520. CheckSource('TestProcType_ReferenceToMethod',
  26521. LinesToStr([ // statements
  26522. 'rtl.createClass(this, "TObject", null, function () {',
  26523. ' this.$init = function () {',
  26524. ' };',
  26525. ' this.$final = function () {',
  26526. ' };',
  26527. ' this.Grow = function (s) {',
  26528. ' var $Self = this;',
  26529. ' var Result = 0;',
  26530. ' function GrowSub(i) {',
  26531. ' var Result = 0;',
  26532. ' $mod.f = rtl.createCallback($Self, "Grow");',
  26533. ' $mod.f = GrowSub;',
  26534. ' return Result;',
  26535. ' };',
  26536. ' $mod.f = rtl.createCallback($Self, "Grow");',
  26537. ' $mod.f = GrowSub;',
  26538. ' return Result;',
  26539. ' };',
  26540. '});',
  26541. 'this.f = null;',
  26542. '']),
  26543. LinesToStr([ // $mod.$main
  26544. '']));
  26545. end;
  26546. procedure TTestModule.TestProcType_Typecast;
  26547. begin
  26548. StartProgram(false);
  26549. Add([
  26550. 'type',
  26551. ' TNotifyEvent = procedure(Sender: Pointer) of object;',
  26552. ' TEvent = procedure of object;',
  26553. ' TGetter = function:longint of object;',
  26554. ' TProcA = procedure(i: longint);',
  26555. ' TFuncB = function(i, j: longint): longint;',
  26556. 'procedure DoIt(); varargs; begin end;',
  26557. 'var',
  26558. ' Notify: tnotifyevent;',
  26559. ' Event: tevent;',
  26560. ' Getter: tgetter;',
  26561. ' ProcA: tproca;',
  26562. ' FuncB: tfuncb;',
  26563. ' p: pointer;',
  26564. 'begin',
  26565. ' notify:=tnotifyevent(event);',
  26566. ' event:=tevent(event);',
  26567. ' event:=tevent(notify);',
  26568. ' event:=tevent(getter);',
  26569. ' event:=tevent(proca);',
  26570. ' proca:=tproca(funcb);',
  26571. ' funcb:=tfuncb(funcb);',
  26572. ' funcb:=tfuncb(proca);',
  26573. ' funcb:=tfuncb(getter);',
  26574. ' proca:=tproca(p);',
  26575. ' funcb:=tfuncb(p);',
  26576. ' getter:=tgetter(p);',
  26577. ' p:=pointer(notify);',
  26578. ' p:=notify;',
  26579. ' p:=pointer(proca);',
  26580. ' p:=proca;',
  26581. ' p:=pointer(funcb);',
  26582. ' p:=funcb;',
  26583. ' doit(Pointer(notify),pointer(event),pointer(proca));',
  26584. '']);
  26585. ConvertProgram;
  26586. CheckSource('TestProcType_Typecast',
  26587. LinesToStr([ // statements
  26588. 'this.DoIt = function () {',
  26589. '};',
  26590. 'this.Notify = null;',
  26591. 'this.Event = null;',
  26592. 'this.Getter = null;',
  26593. 'this.ProcA = null;',
  26594. 'this.FuncB = null;',
  26595. 'this.p = null;',
  26596. '']),
  26597. LinesToStr([ // $mod.$main
  26598. '$mod.Notify = $mod.Event;',
  26599. '$mod.Event = $mod.Event;',
  26600. '$mod.Event = $mod.Notify;',
  26601. '$mod.Event = $mod.Getter;',
  26602. '$mod.Event = $mod.ProcA;',
  26603. '$mod.ProcA = $mod.FuncB;',
  26604. '$mod.FuncB = $mod.FuncB;',
  26605. '$mod.FuncB = $mod.ProcA;',
  26606. '$mod.FuncB = $mod.Getter;',
  26607. '$mod.ProcA = $mod.p;',
  26608. '$mod.FuncB = $mod.p;',
  26609. '$mod.Getter = $mod.p;',
  26610. '$mod.p = $mod.Notify;',
  26611. '$mod.p = $mod.Notify;',
  26612. '$mod.p = $mod.ProcA;',
  26613. '$mod.p = $mod.ProcA;',
  26614. '$mod.p = $mod.FuncB;',
  26615. '$mod.p = $mod.FuncB;',
  26616. '$mod.DoIt($mod.Notify, $mod.Event, $mod.ProcA);',
  26617. '']));
  26618. end;
  26619. procedure TTestModule.TestProcType_PassProcToUntyped;
  26620. begin
  26621. StartProgram(false);
  26622. Add([
  26623. 'type',
  26624. ' TEvent = procedure of object;',
  26625. ' TFunc = function: longint;',
  26626. 'procedure DoIt(); varargs; begin end;',
  26627. 'procedure DoSome(const a; var b; p: pointer); begin end;',
  26628. 'var',
  26629. ' Event: tevent;',
  26630. ' Func: TFunc;',
  26631. 'begin',
  26632. ' doit(event,func);',
  26633. ' dosome(event,event,event);',
  26634. ' dosome(func,func,func);',
  26635. '']);
  26636. ConvertProgram;
  26637. CheckSource('TestProcType_PassProcToUntyped',
  26638. LinesToStr([ // statements
  26639. 'this.DoIt = function () {',
  26640. '};',
  26641. 'this.DoSome = function (a, b, p) {',
  26642. '};',
  26643. 'this.Event = null;',
  26644. 'this.Func = null;',
  26645. '']),
  26646. LinesToStr([ // $mod.$main
  26647. '$mod.DoIt($mod.Event, $mod.Func);',
  26648. '$mod.DoSome($mod.Event, {',
  26649. ' p: $mod,',
  26650. ' get: function () {',
  26651. ' return this.p.Event;',
  26652. ' },',
  26653. ' set: function (v) {',
  26654. ' this.p.Event = v;',
  26655. ' }',
  26656. '}, $mod.Event);',
  26657. '$mod.DoSome($mod.Func, {',
  26658. ' p: $mod,',
  26659. ' get: function () {',
  26660. ' return this.p.Func;',
  26661. ' },',
  26662. ' set: function (v) {',
  26663. ' this.p.Func = v;',
  26664. ' }',
  26665. '}, $mod.Func);',
  26666. '']));
  26667. end;
  26668. procedure TTestModule.TestProcType_PassProcToArray;
  26669. begin
  26670. StartProgram(false);
  26671. Add([
  26672. 'type',
  26673. ' TFunc = function: longint;',
  26674. ' TArrFunc = array of TFunc;',
  26675. 'procedure DoIt(Arr: TArrFunc); begin end;',
  26676. 'function GetIt: longint; begin end;',
  26677. 'var',
  26678. ' Func: tfunc;',
  26679. 'begin',
  26680. ' doit([]);',
  26681. ' doit([@GetIt]);',
  26682. ' doit([Func]);',
  26683. '']);
  26684. ConvertProgram;
  26685. CheckSource('TestProcType_PassProcToArray',
  26686. LinesToStr([ // statements
  26687. 'this.DoIt = function (Arr) {',
  26688. '};',
  26689. 'this.GetIt = function () {',
  26690. ' var Result = 0;',
  26691. ' return Result;',
  26692. '};',
  26693. 'this.Func = null;',
  26694. '']),
  26695. LinesToStr([ // $mod.$main
  26696. '$mod.DoIt([]);',
  26697. '$mod.DoIt([$mod.GetIt]);',
  26698. '$mod.DoIt([$mod.Func]);',
  26699. '']));
  26700. end;
  26701. procedure TTestModule.TestProcType_SafeCallObjFPC;
  26702. begin
  26703. StartProgram(false);
  26704. Add([
  26705. '{$modeswitch externalclass}',
  26706. 'type',
  26707. ' TProc = reference to procedure(i: longint); safecall;',
  26708. ' TEvent = procedure(i: longint) of object; safecall;',
  26709. ' TExtA = class external name ''ExtObj''',
  26710. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  26711. ' procedure DoSome(Id: longint = 1);',
  26712. ' procedure SetOnClick(const e: TEvent);',
  26713. ' property OnClick: TEvent write SetOnClick;',
  26714. ' class procedure Fly(Id: longint = 1); static;',
  26715. ' procedure SetOnShow(const p: TProc);',
  26716. ' property OnShow: TProc write SetOnShow;',
  26717. ' end;',
  26718. 'procedure Run(i: longint = 1);',
  26719. 'begin',
  26720. 'end;',
  26721. 'var',
  26722. ' Obj: texta;',
  26723. ' e: TEvent;',
  26724. ' p: TProc;',
  26725. 'begin',
  26726. ' e:=e;',
  26727. ' e:[email protected];',
  26728. ' e:[email protected];',
  26729. ' e:=TEvent(@obj.dosome);', // no safecall
  26730. ' obj.OnClick:[email protected];',
  26731. ' obj.OnClick:[email protected];',
  26732. ' obj.setonclick(@obj.doit);',
  26733. ' obj.setonclick(@obj.dosome);',
  26734. ' p:=@Run;',
  26735. ' p:[email protected];',
  26736. ' obj.OnShow:=@Run;',
  26737. ' obj.OnShow:[email protected];',
  26738. ' obj.setOnShow(@Run);',
  26739. ' obj.setOnShow(@TExtA.Fly);',
  26740. ' with obj do begin',
  26741. ' e:=@doit;',
  26742. ' e:=@dosome;',
  26743. ' OnClick:=@doit;',
  26744. ' OnClick:=@dosome;',
  26745. ' setonclick(@doit);',
  26746. ' setonclick(@dosome);',
  26747. ' OnShow:=@Run;',
  26748. ' setOnShow(@Run);',
  26749. ' end;']);
  26750. ConvertProgram;
  26751. CheckSource('TestProcType_SafeCallObjFPC',
  26752. LinesToStr([ // statements
  26753. 'this.Run = function (i) {',
  26754. '};',
  26755. 'this.Obj = null;',
  26756. 'this.e = null;',
  26757. 'this.p = null;',
  26758. '']),
  26759. LinesToStr([ // $mod.$main
  26760. '$mod.e = $mod.e;',
  26761. '$mod.e = rtl.createSafeCallback($mod.Obj, "$Execute");',
  26762. '$mod.e = rtl.createSafeCallback($mod.Obj, "DoSome");',
  26763. '$mod.e = rtl.createCallback($mod.Obj, "DoSome");',
  26764. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  26765. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  26766. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  26767. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  26768. '$mod.p = rtl.createSafeCallback($mod, "Run");',
  26769. '$mod.p = rtl.createSafeCallback(ExtObj, "Fly");',
  26770. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26771. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  26772. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26773. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  26774. 'var $with = $mod.Obj;',
  26775. '$mod.e = rtl.createSafeCallback($with, "$Execute");',
  26776. '$mod.e = rtl.createSafeCallback($with, "DoSome");',
  26777. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  26778. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  26779. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  26780. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  26781. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26782. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26783. '']));
  26784. end;
  26785. procedure TTestModule.TestProcType_SafeCallDelphi;
  26786. begin
  26787. StartProgram(false);
  26788. Add([
  26789. '{$mode delphi}',
  26790. '{$modeswitch externalclass}',
  26791. 'type',
  26792. ' TProc = reference to procedure(i: longint); safecall;',
  26793. ' TEvent = procedure(i: longint) of object; safecall;',
  26794. ' TExtA = class external name ''ExtObj''',
  26795. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  26796. ' procedure DoSome(Id: longint = 1);',
  26797. ' procedure SetOnClick(const e: TEvent);',
  26798. ' property OnClick: TEvent write SetOnClick;',
  26799. ' class procedure Fly(Id: longint = 1); static;',
  26800. ' procedure SetOnShow(const p: TProc);',
  26801. ' property OnShow: TProc write SetOnShow;',
  26802. ' end;',
  26803. 'procedure Run(i: longint = 1);',
  26804. 'begin',
  26805. 'end;',
  26806. 'var',
  26807. ' Obj: texta;',
  26808. ' e: TEvent;',
  26809. ' p: TProc;',
  26810. 'begin',
  26811. ' e:=e;',
  26812. ' e:=obj.doit;',
  26813. ' e:=obj.dosome;',
  26814. ' e:=TEvent(@obj.dosome);', // no safecall
  26815. ' obj.OnClick:=obj.doit;',
  26816. ' obj.OnClick:=obj.dosome;',
  26817. ' obj.setonclick(obj.doit);',
  26818. ' obj.setonclick(obj.dosome);',
  26819. ' p:=Run;',
  26820. ' p:=TExtA.Fly;',
  26821. ' obj.OnShow:=Run;',
  26822. ' obj.OnShow:=TExtA.Fly;',
  26823. ' obj.setOnShow(Run);',
  26824. ' obj.setOnShow(TExtA.Fly);',
  26825. ' with obj do begin',
  26826. ' e:=doit;',
  26827. ' e:=dosome;',
  26828. ' OnClick:=doit;',
  26829. ' OnClick:=dosome;',
  26830. ' setonclick(doit);',
  26831. ' setonclick(dosome);',
  26832. ' OnShow:=@Run;',
  26833. ' setOnShow(@Run);',
  26834. ' end;']);
  26835. ConvertProgram;
  26836. CheckSource('TestProcType_SafeCallDelphi',
  26837. LinesToStr([ // statements
  26838. 'this.Run = function (i) {',
  26839. '};',
  26840. 'this.Obj = null;',
  26841. 'this.e = null;',
  26842. 'this.p = null;',
  26843. '']),
  26844. LinesToStr([ // $mod.$main
  26845. '$mod.e = $mod.e;',
  26846. '$mod.e = rtl.createSafeCallback($mod.Obj, "$Execute");',
  26847. '$mod.e = rtl.createSafeCallback($mod.Obj, "DoSome");',
  26848. '$mod.e = rtl.createCallback($mod.Obj, "DoSome");',
  26849. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  26850. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  26851. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  26852. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  26853. '$mod.p = rtl.createSafeCallback($mod, "Run");',
  26854. '$mod.p = rtl.createSafeCallback(ExtObj, "Fly");',
  26855. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26856. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  26857. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26858. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  26859. 'var $with = $mod.Obj;',
  26860. '$mod.e = rtl.createSafeCallback($with, "$Execute");',
  26861. '$mod.e = rtl.createSafeCallback($with, "DoSome");',
  26862. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  26863. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  26864. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  26865. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  26866. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26867. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26868. '']));
  26869. end;
  26870. procedure TTestModule.TestPointer;
  26871. begin
  26872. StartProgram(false);
  26873. Add(['type',
  26874. ' TObject = class end;',
  26875. ' TClass = class of TObject;',
  26876. ' TArrInt = array of longint;',
  26877. 'const',
  26878. ' n = nil;',
  26879. 'var',
  26880. ' v: jsvalue;',
  26881. ' Obj: tobject;',
  26882. ' C: tclass;',
  26883. ' a: tarrint;',
  26884. ' p: Pointer = nil;',
  26885. ' s: string;',
  26886. 'begin',
  26887. ' p:=p;',
  26888. ' p:=nil;',
  26889. ' if p=nil then;',
  26890. ' if nil=p then;',
  26891. ' if Assigned(p) then;',
  26892. ' p:=Pointer(v);',
  26893. ' p:=obj;',
  26894. ' p:=c;',
  26895. ' p:=a;',
  26896. ' p:=tobject;',
  26897. ' obj:=TObject(p);',
  26898. ' c:=TClass(p);',
  26899. ' a:=TArrInt(p);',
  26900. ' p:=n;',
  26901. ' p:=Pointer(a);',
  26902. ' p:=pointer(s);',
  26903. ' s:=string(p);',
  26904. '']);
  26905. ConvertProgram;
  26906. CheckSource('TestPointer',
  26907. LinesToStr([ // statements
  26908. 'rtl.createClass(this, "TObject", null, function () {',
  26909. ' this.$init = function () {',
  26910. ' };',
  26911. ' this.$final = function () {',
  26912. ' };',
  26913. '});',
  26914. 'this.n = null;',
  26915. 'this.v = undefined;',
  26916. 'this.Obj = null;',
  26917. 'this.C = null;',
  26918. 'this.a = [];',
  26919. 'this.p = null;',
  26920. 'this.s = "";',
  26921. '']),
  26922. LinesToStr([ // $mod.$main
  26923. '$mod.p = $mod.p;',
  26924. '$mod.p = null;',
  26925. 'if ($mod.p === null) ;',
  26926. 'if (null === $mod.p) ;',
  26927. 'if ($mod.p != null) ;',
  26928. '$mod.p = $mod.v;',
  26929. '$mod.p = $mod.Obj;',
  26930. '$mod.p = $mod.C;',
  26931. '$mod.p = $mod.a;',
  26932. '$mod.p = $mod.TObject;',
  26933. '$mod.Obj = $mod.p;',
  26934. '$mod.C = $mod.p;',
  26935. '$mod.a = $mod.p;',
  26936. '$mod.p = null;',
  26937. '$mod.p = $mod.a;',
  26938. '$mod.p = $mod.s;',
  26939. '$mod.s = $mod.p;',
  26940. '']));
  26941. end;
  26942. procedure TTestModule.TestPointer_Proc;
  26943. begin
  26944. StartProgram(false);
  26945. Add('type');
  26946. Add(' TObject = class');
  26947. Add(' procedure DoIt; virtual; abstract;');
  26948. Add(' end;');
  26949. Add('procedure DoSome; begin end;');
  26950. Add('var');
  26951. Add(' o: TObject;');
  26952. Add(' p: Pointer;');
  26953. Add('begin');
  26954. Add(' p:=@DoSome;');
  26955. Add(' p:[email protected];');
  26956. ConvertProgram;
  26957. CheckSource('TestPointer_Proc',
  26958. LinesToStr([ // statements
  26959. 'rtl.createClass(this, "TObject", null, function () {',
  26960. ' this.$init = function () {',
  26961. ' };',
  26962. ' this.$final = function () {',
  26963. ' };',
  26964. '});',
  26965. 'this.DoSome = function () {',
  26966. '};',
  26967. 'this.o = null;',
  26968. 'this.p = null;',
  26969. '']),
  26970. LinesToStr([ // $mod.$main
  26971. '$mod.p = $mod.DoSome;',
  26972. '$mod.p = rtl.createCallback($mod.o, "DoIt");',
  26973. '']));
  26974. end;
  26975. procedure TTestModule.TestPointer_AssignRecordFail;
  26976. begin
  26977. StartProgram(false);
  26978. Add('type');
  26979. Add(' TRec = record end;');
  26980. Add('var');
  26981. Add(' p: Pointer;');
  26982. Add(' r: TRec;');
  26983. Add('begin');
  26984. Add(' p:=r;');
  26985. SetExpectedPasResolverError('Incompatible types: got "TRec" expected "Pointer"',
  26986. nIncompatibleTypesGotExpected);
  26987. ConvertProgram;
  26988. end;
  26989. procedure TTestModule.TestPointer_AssignStaticArrayFail;
  26990. begin
  26991. StartProgram(false);
  26992. Add('type');
  26993. Add(' TArr = array[boolean] of longint;');
  26994. Add('var');
  26995. Add(' p: Pointer;');
  26996. Add(' a: TArr;');
  26997. Add('begin');
  26998. Add(' p:=a;');
  26999. SetExpectedPasResolverError('Incompatible types: got "TArr" expected "Pointer"',
  27000. nIncompatibleTypesGotExpected);
  27001. ConvertProgram;
  27002. end;
  27003. procedure TTestModule.TestPointer_TypeCastJSValueToPointer;
  27004. begin
  27005. StartProgram(false);
  27006. Add([
  27007. 'procedure DoIt(args: array of jsvalue); begin end;',
  27008. 'procedure DoAll; varargs; begin end;',
  27009. 'var',
  27010. ' v: jsvalue;',
  27011. 'begin',
  27012. ' DoIt([pointer(v)]);',
  27013. ' DoAll(pointer(v));',
  27014. '']);
  27015. ConvertProgram;
  27016. CheckSource('TestPointer_TypeCastJSValueToPointer',
  27017. LinesToStr([ // statements
  27018. 'this.DoIt = function (args) {',
  27019. '};',
  27020. 'this.DoAll = function () {',
  27021. '};',
  27022. 'this.v = undefined;',
  27023. '']),
  27024. LinesToStr([ // $mod.$main
  27025. '$mod.DoIt([$mod.v]);',
  27026. '$mod.DoAll($mod.v);',
  27027. '']));
  27028. end;
  27029. procedure TTestModule.TestPointer_NonRecordFail;
  27030. begin
  27031. StartProgram(false);
  27032. Add([
  27033. 'type',
  27034. ' p = ^longint;',
  27035. 'begin',
  27036. '']);
  27037. SetExpectedPasResolverError('Not supported: pointer of Longint',nNotSupportedX);
  27038. ConvertProgram;
  27039. end;
  27040. procedure TTestModule.TestPointer_AnonymousArgTypeFail;
  27041. begin
  27042. StartProgram(false);
  27043. Add([
  27044. 'procedure DoIt(p: ^longint); begin end;',
  27045. 'begin',
  27046. '']);
  27047. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  27048. ConvertProgram;
  27049. end;
  27050. procedure TTestModule.TestPointer_AnonymousVarTypeFail;
  27051. begin
  27052. StartProgram(false);
  27053. Add([
  27054. 'var p: ^longint;',
  27055. 'begin',
  27056. '']);
  27057. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  27058. ConvertProgram;
  27059. end;
  27060. procedure TTestModule.TestPointer_AnonymousResultTypeFail;
  27061. begin
  27062. StartProgram(false);
  27063. Add([
  27064. 'function DoIt: ^longint; begin end;',
  27065. 'begin',
  27066. '']);
  27067. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  27068. ConvertProgram;
  27069. end;
  27070. procedure TTestModule.TestPointer_AddrOperatorFail;
  27071. begin
  27072. StartProgram(false);
  27073. Add([
  27074. 'var i: longint;',
  27075. 'begin',
  27076. ' if @i=nil then ;',
  27077. '']);
  27078. SetExpectedConverterError('illegal qualifier "@" in front of "i:Longint"',nIllegalQualifierInFrontOf);
  27079. ConvertProgram;
  27080. end;
  27081. procedure TTestModule.TestPointer_ArrayParamsFail;
  27082. begin
  27083. StartProgram(false);
  27084. Add([
  27085. 'var',
  27086. ' p: Pointer;',
  27087. 'begin',
  27088. ' p:=p[1];',
  27089. '']);
  27090. SetExpectedPasResolverError('illegal qualifier "[" after "Pointer"',nIllegalQualifierAfter);
  27091. ConvertProgram;
  27092. end;
  27093. procedure TTestModule.TestPointer_PointerAddFail;
  27094. begin
  27095. StartProgram(false);
  27096. Add([
  27097. 'var',
  27098. ' p: Pointer;',
  27099. 'begin',
  27100. ' p:=p+1;',
  27101. '']);
  27102. SetExpectedPasResolverError('Operator is not overloaded: "Pointer" + "Longint"',nOperatorIsNotOverloadedAOpB);
  27103. ConvertProgram;
  27104. end;
  27105. procedure TTestModule.TestPointer_IncPointerFail;
  27106. begin
  27107. StartProgram(false);
  27108. Add([
  27109. 'var',
  27110. ' p: Pointer;',
  27111. 'begin',
  27112. ' inc(p,1);',
  27113. '']);
  27114. SetExpectedPasResolverError('Incompatible type arg no. 1: Got "Pointer", expected "integer"',
  27115. nIncompatibleTypeArgNo);
  27116. ConvertProgram;
  27117. end;
  27118. procedure TTestModule.TestPointer_Record;
  27119. begin
  27120. StartProgram(false);
  27121. Add([
  27122. 'type',
  27123. ' TRec = record x: longint; end;',
  27124. ' PRec = ^TRec;',
  27125. 'var',
  27126. ' r: TRec;',
  27127. ' p: PRec;',
  27128. ' q: ^TRec;',
  27129. ' Ptr: pointer;',
  27130. 'begin',
  27131. ' new(p);',
  27132. ' p:=@r;',
  27133. ' r:=p^;',
  27134. ' r.x:=p^.x;',
  27135. ' p^.x:=r.x;',
  27136. ' if p^.x=3 then ;',
  27137. ' if 4=p^.x then ;',
  27138. ' dispose(p);',
  27139. ' new(q);',
  27140. ' dispose(q);',
  27141. ' Ptr:=p;',
  27142. ' p:=PRec(ptr);',
  27143. '']);
  27144. ConvertProgram;
  27145. CheckSource('TestPointer_Record',
  27146. LinesToStr([ // statements
  27147. 'rtl.recNewT(this, "TRec", function () {',
  27148. ' this.x = 0;',
  27149. ' this.$eq = function (b) {',
  27150. ' return this.x === b.x;',
  27151. ' };',
  27152. ' this.$assign = function (s) {',
  27153. ' this.x = s.x;',
  27154. ' return this;',
  27155. ' };',
  27156. '});',
  27157. 'this.r = this.TRec.$new();',
  27158. 'this.p = null;',
  27159. 'this.q = null;',
  27160. 'this.Ptr = null;',
  27161. '']),
  27162. LinesToStr([ // $mod.$main
  27163. '$mod.p = $mod.TRec.$new();',
  27164. '$mod.p = $mod.r;',
  27165. '$mod.r.$assign($mod.p);',
  27166. '$mod.r.x = $mod.p.x;',
  27167. '$mod.p.x = $mod.r.x;',
  27168. 'if ($mod.p.x === 3) ;',
  27169. 'if (4 === $mod.p.x) ;',
  27170. '$mod.p = null;',
  27171. '$mod.q = $mod.TRec.$new();',
  27172. '$mod.q = null;',
  27173. '$mod.Ptr = $mod.p;',
  27174. '$mod.p = $mod.Ptr;',
  27175. '']));
  27176. end;
  27177. procedure TTestModule.TestPointer_RecordArg;
  27178. begin
  27179. StartProgram(false);
  27180. Add([
  27181. '{$modeswitch autoderef}',
  27182. 'type',
  27183. ' TRec = record x: longint; end;',
  27184. ' PRec = ^TRec;',
  27185. 'function DoIt(const a: PRec; var b: PRec; out c: PRec): TRec;',
  27186. 'begin',
  27187. ' a.x:=a.x;',
  27188. ' a^.x:=a^.x;',
  27189. ' with a^ do',
  27190. ' x:=x;',
  27191. 'end;',
  27192. 'function GetIt(p: PRec): PRec;',
  27193. 'begin',
  27194. ' p.x:=p.x;',
  27195. ' p^.x:=p^.x;',
  27196. ' with p^ do',
  27197. ' x:=x;',
  27198. 'end;',
  27199. 'var',
  27200. ' r: TRec;',
  27201. ' p: PRec;',
  27202. 'begin',
  27203. ' p:=GetIt(p);',
  27204. ' p^:=GetIt(@r)^;',
  27205. ' DoIt(p,p,p);',
  27206. ' DoIt(@r,p,p);',
  27207. '']);
  27208. ConvertProgram;
  27209. CheckSource('TestPointer_RecordArg',
  27210. LinesToStr([ // statements
  27211. 'rtl.recNewT(this, "TRec", function () {',
  27212. ' this.x = 0;',
  27213. ' this.$eq = function (b) {',
  27214. ' return this.x === b.x;',
  27215. ' };',
  27216. ' this.$assign = function (s) {',
  27217. ' this.x = s.x;',
  27218. ' return this;',
  27219. ' };',
  27220. '});',
  27221. 'this.DoIt = function (a, b, c) {',
  27222. ' var Result = $mod.TRec.$new();',
  27223. ' a.x = a.x;',
  27224. ' a.x = a.x;',
  27225. ' a.x = a.x;',
  27226. ' return Result;',
  27227. '};',
  27228. 'this.GetIt = function (p) {',
  27229. ' var Result = null;',
  27230. ' p.x = p.x;',
  27231. ' p.x = p.x;',
  27232. ' p.x = p.x;',
  27233. ' return Result;',
  27234. '};',
  27235. 'this.r = this.TRec.$new();',
  27236. 'this.p = null;',
  27237. '']),
  27238. LinesToStr([ // $mod.$main
  27239. '$mod.p = $mod.GetIt($mod.p);',
  27240. '$mod.p.$assign($mod.GetIt($mod.r));',
  27241. '$mod.DoIt($mod.p, {',
  27242. ' p: $mod,',
  27243. ' get: function () {',
  27244. ' return this.p.p;',
  27245. ' },',
  27246. ' set: function (v) {',
  27247. ' this.p.p = v;',
  27248. ' }',
  27249. '}, {',
  27250. ' p: $mod,',
  27251. ' get: function () {',
  27252. ' return this.p.p;',
  27253. ' },',
  27254. ' set: function (v) {',
  27255. ' this.p.p = v;',
  27256. ' }',
  27257. '});',
  27258. '$mod.DoIt($mod.r, {',
  27259. ' p: $mod,',
  27260. ' get: function () {',
  27261. ' return this.p.p;',
  27262. ' },',
  27263. ' set: function (v) {',
  27264. ' this.p.p = v;',
  27265. ' }',
  27266. '}, {',
  27267. ' p: $mod,',
  27268. ' get: function () {',
  27269. ' return this.p.p;',
  27270. ' },',
  27271. ' set: function (v) {',
  27272. ' this.p.p = v;',
  27273. ' }',
  27274. '});',
  27275. '']));
  27276. end;
  27277. procedure TTestModule.TestJSValue_AssignToJSValue;
  27278. begin
  27279. StartProgram(false);
  27280. Add('var');
  27281. Add(' v: jsvalue;');
  27282. Add(' i: longint;');
  27283. Add(' s: string;');
  27284. Add(' b: boolean;');
  27285. Add(' d: double;');
  27286. Add(' p: pointer;');
  27287. Add('begin');
  27288. Add(' v:=v;');
  27289. Add(' v:=1;');
  27290. Add(' v:=i;');
  27291. Add(' v:='''';');
  27292. Add(' v:=''c'';');
  27293. Add(' v:=''foo'';');
  27294. Add(' v:=s;');
  27295. Add(' v:=false;');
  27296. Add(' v:=true;');
  27297. Add(' v:=b;');
  27298. Add(' v:=0.1;');
  27299. Add(' v:=d;');
  27300. Add(' v:=nil;');
  27301. Add(' v:=p;');
  27302. ConvertProgram;
  27303. CheckSource('TestJSValue_AssignToJSValue',
  27304. LinesToStr([ // statements
  27305. 'this.v = undefined;',
  27306. 'this.i = 0;',
  27307. 'this.s = "";',
  27308. 'this.b = false;',
  27309. 'this.d = 0.0;',
  27310. 'this.p = null;',
  27311. '']),
  27312. LinesToStr([ // $mod.$main
  27313. '$mod.v = $mod.v;',
  27314. '$mod.v = 1;',
  27315. '$mod.v = $mod.i;',
  27316. '$mod.v = "";',
  27317. '$mod.v = "c";',
  27318. '$mod.v = "foo";',
  27319. '$mod.v = $mod.s;',
  27320. '$mod.v = false;',
  27321. '$mod.v = true;',
  27322. '$mod.v = $mod.b;',
  27323. '$mod.v = 0.1;',
  27324. '$mod.v = $mod.d;',
  27325. '$mod.v = null;',
  27326. '$mod.v = $mod.p;',
  27327. '']));
  27328. end;
  27329. procedure TTestModule.TestJSValue_TypeCastToBaseType;
  27330. begin
  27331. StartProgram(false);
  27332. Add('type');
  27333. Add(' integer = longint;');
  27334. Add(' TYesNo = boolean;');
  27335. Add(' TFloat = double;');
  27336. Add(' TCaption = string;');
  27337. Add(' TChar = char;');
  27338. Add('var');
  27339. Add(' v: jsvalue;');
  27340. Add(' i: integer;');
  27341. Add(' s: TCaption;');
  27342. Add(' b: TYesNo;');
  27343. Add(' d: TFloat;');
  27344. Add(' c: char;');
  27345. Add('begin');
  27346. Add(' i:=longint(v);');
  27347. Add(' i:=integer(v);');
  27348. Add(' s:=string(v);');
  27349. Add(' s:=TCaption(v);');
  27350. Add(' b:=boolean(v);');
  27351. Add(' b:=TYesNo(v);');
  27352. Add(' d:=double(v);');
  27353. Add(' d:=TFloat(v);');
  27354. Add(' c:=char(v);');
  27355. Add(' c:=TChar(v);');
  27356. ConvertProgram;
  27357. CheckSource('TestJSValue_TypeCastToBaseType',
  27358. LinesToStr([ // statements
  27359. 'this.v = undefined;',
  27360. 'this.i = 0;',
  27361. 'this.s = "";',
  27362. 'this.b = false;',
  27363. 'this.d = 0.0;',
  27364. 'this.c = "";',
  27365. '']),
  27366. LinesToStr([ // $mod.$main
  27367. '$mod.i = rtl.trunc($mod.v);',
  27368. '$mod.i = rtl.trunc($mod.v);',
  27369. '$mod.s = "" + $mod.v;',
  27370. '$mod.s = "" + $mod.v;',
  27371. '$mod.b = !($mod.v == false);',
  27372. '$mod.b = !($mod.v == false);',
  27373. '$mod.d = rtl.getNumber($mod.v);',
  27374. '$mod.d = rtl.getNumber($mod.v);',
  27375. '$mod.c = rtl.getChar($mod.v);',
  27376. '$mod.c = rtl.getChar($mod.v);',
  27377. '']));
  27378. end;
  27379. procedure TTestModule.TestJSValue_TypecastToJSValue;
  27380. begin
  27381. StartProgram(false);
  27382. Add([
  27383. 'type',
  27384. ' TArr = array of word;',
  27385. ' TRec = record end;',
  27386. ' TSet = set of boolean;',
  27387. 'procedure Fly(v: jsvalue);',
  27388. 'begin',
  27389. 'end;',
  27390. 'var',
  27391. ' a: TArr;',
  27392. ' r: TRec;',
  27393. ' s: TSet;',
  27394. 'begin',
  27395. ' Fly(jsvalue(a));',
  27396. ' Fly(jsvalue(r));',
  27397. ' Fly(jsvalue(s));',
  27398. '']);
  27399. ConvertProgram;
  27400. CheckSource('TestJSValue_TypecastToJSValue',
  27401. LinesToStr([ // statements
  27402. 'rtl.recNewT(this, "TRec", function () {',
  27403. ' this.$eq = function (b) {',
  27404. ' return true;',
  27405. ' };',
  27406. ' this.$assign = function (s) {',
  27407. ' return this;',
  27408. ' };',
  27409. '});',
  27410. 'this.Fly = function (v) {',
  27411. '};',
  27412. 'this.a = [];',
  27413. 'this.r = this.TRec.$new();',
  27414. 'this.s = {};',
  27415. '']),
  27416. LinesToStr([ // $mod.$main
  27417. '$mod.Fly($mod.a);',
  27418. '$mod.Fly($mod.r);',
  27419. '$mod.Fly($mod.s);',
  27420. '']));
  27421. end;
  27422. procedure TTestModule.TestJSValue_Equal;
  27423. begin
  27424. StartProgram(false);
  27425. Add('type');
  27426. Add(' integer = longint;');
  27427. Add(' TYesNo = boolean;');
  27428. Add(' TFloat = double;');
  27429. Add(' TCaption = string;');
  27430. Add(' TChar = char;');
  27431. Add(' TMulti = JSValue;');
  27432. Add('var');
  27433. Add(' v: jsvalue;');
  27434. Add(' i: integer;');
  27435. Add(' s: TCaption;');
  27436. Add(' b: TYesNo;');
  27437. Add(' d: TFloat;');
  27438. Add(' c: char;');
  27439. Add(' m: TMulti;');
  27440. Add('begin');
  27441. Add(' b:=v=v;');
  27442. Add(' b:=v<>v;');
  27443. Add(' b:=v=1;');
  27444. Add(' b:=v<>1;');
  27445. Add(' b:=2=v;');
  27446. Add(' b:=2<>v;');
  27447. Add(' b:=v=i;');
  27448. Add(' b:=i=v;');
  27449. Add(' b:=v=nil;');
  27450. Add(' b:=nil=v;');
  27451. Add(' b:=v=false;');
  27452. Add(' b:=true=v;');
  27453. Add(' b:=v=b;');
  27454. Add(' b:=b=v;');
  27455. Add(' b:=v=s;');
  27456. Add(' b:=s=v;');
  27457. Add(' b:=v=''foo'';');
  27458. Add(' b:=''''=v;');
  27459. Add(' b:=v=d;');
  27460. Add(' b:=d=v;');
  27461. Add(' b:=v=3.4;');
  27462. Add(' b:=5.6=v;');
  27463. Add(' b:=v=c;');
  27464. Add(' b:=c=v;');
  27465. Add(' b:=m=m;');
  27466. Add(' b:=v=m;');
  27467. Add(' b:=m=v;');
  27468. ConvertProgram;
  27469. CheckSource('TestJSValue_Equal',
  27470. LinesToStr([ // statements
  27471. 'this.v = undefined;',
  27472. 'this.i = 0;',
  27473. 'this.s = "";',
  27474. 'this.b = false;',
  27475. 'this.d = 0.0;',
  27476. 'this.c = "";',
  27477. 'this.m = undefined;',
  27478. '']),
  27479. LinesToStr([ // $mod.$main
  27480. '$mod.b = $mod.v == $mod.v;',
  27481. '$mod.b = $mod.v != $mod.v;',
  27482. '$mod.b = $mod.v == 1;',
  27483. '$mod.b = $mod.v != 1;',
  27484. '$mod.b = 2 == $mod.v;',
  27485. '$mod.b = 2 != $mod.v;',
  27486. '$mod.b = $mod.v == $mod.i;',
  27487. '$mod.b = $mod.i == $mod.v;',
  27488. '$mod.b = $mod.v == null;',
  27489. '$mod.b = null == $mod.v;',
  27490. '$mod.b = $mod.v == false;',
  27491. '$mod.b = true == $mod.v;',
  27492. '$mod.b = $mod.v == $mod.b;',
  27493. '$mod.b = $mod.b == $mod.v;',
  27494. '$mod.b = $mod.v == $mod.s;',
  27495. '$mod.b = $mod.s == $mod.v;',
  27496. '$mod.b = $mod.v == "foo";',
  27497. '$mod.b = "" == $mod.v;',
  27498. '$mod.b = $mod.v == $mod.d;',
  27499. '$mod.b = $mod.d == $mod.v;',
  27500. '$mod.b = $mod.v == 3.4;',
  27501. '$mod.b = 5.6 == $mod.v;',
  27502. '$mod.b = $mod.v == $mod.c;',
  27503. '$mod.b = $mod.c == $mod.v;',
  27504. '$mod.b = $mod.m == $mod.m;',
  27505. '$mod.b = $mod.v == $mod.m;',
  27506. '$mod.b = $mod.m == $mod.v;',
  27507. '']));
  27508. end;
  27509. procedure TTestModule.TestJSValue_If;
  27510. begin
  27511. StartProgram(false);
  27512. Add([
  27513. 'procedure Fly(var u);',
  27514. 'begin',
  27515. ' if jsvalue(u) then ;',
  27516. 'end;',
  27517. 'var',
  27518. ' v: jsvalue;',
  27519. 'begin',
  27520. ' if v then ;',
  27521. ' while v do ;',
  27522. ' repeat until v;',
  27523. '']);
  27524. ConvertProgram;
  27525. CheckSource('TestJSValue_If',
  27526. LinesToStr([ // statements
  27527. 'this.Fly = function (u) {',
  27528. ' if (u.get()) ;',
  27529. '};',
  27530. 'this.v = undefined;',
  27531. '']),
  27532. LinesToStr([ // $mod.$main
  27533. 'if ($mod.v) ;',
  27534. 'while($mod.v){',
  27535. '};',
  27536. 'do{',
  27537. '} while(!$mod.v);',
  27538. '']));
  27539. end;
  27540. procedure TTestModule.TestJSValue_Not;
  27541. begin
  27542. StartProgram(false);
  27543. Add([
  27544. 'var',
  27545. ' v: jsvalue;',
  27546. ' b: boolean;',
  27547. 'begin',
  27548. ' b:=not v;',
  27549. ' if not v then ;',
  27550. ' while not v do ;',
  27551. ' repeat until not v;',
  27552. '']);
  27553. ConvertProgram;
  27554. CheckSource('TestJSValue_If',
  27555. LinesToStr([ // statements
  27556. 'this.v = undefined;',
  27557. 'this.b = false;',
  27558. '']),
  27559. LinesToStr([ // $mod.$main
  27560. '$mod.b=!$mod.v;',
  27561. 'if (!$mod.v) ;',
  27562. 'while(!$mod.v){',
  27563. '};',
  27564. 'do{',
  27565. '} while($mod.v);',
  27566. '']));
  27567. end;
  27568. procedure TTestModule.TestJSValue_Enum;
  27569. begin
  27570. StartProgram(false);
  27571. Add('type');
  27572. Add(' TColor = (red, blue);');
  27573. Add(' TRedBlue = TColor;');
  27574. Add('var');
  27575. Add(' v: jsvalue;');
  27576. Add(' e: TColor;');
  27577. Add('begin');
  27578. Add(' v:=e;');
  27579. Add(' v:=TColor(e);');
  27580. Add(' v:=TRedBlue(e);');
  27581. Add(' e:=TColor(v);');
  27582. Add(' e:=TRedBlue(v);');
  27583. ConvertProgram;
  27584. CheckSource('TestJSValue_Enum',
  27585. LinesToStr([ // statements
  27586. 'this.TColor = {',
  27587. ' "0": "red",',
  27588. ' red: 0,',
  27589. ' "1": "blue",',
  27590. ' blue: 1',
  27591. '};',
  27592. 'this.v = undefined;',
  27593. 'this.e = 0;',
  27594. '']),
  27595. LinesToStr([ // $mod.$main
  27596. '$mod.v = $mod.e;',
  27597. '$mod.v = $mod.e;',
  27598. '$mod.v = $mod.e;',
  27599. '$mod.e = $mod.v;',
  27600. '$mod.e = $mod.v;',
  27601. '']));
  27602. end;
  27603. procedure TTestModule.TestJSValue_ClassInstance;
  27604. begin
  27605. StartProgram(false);
  27606. Add([
  27607. 'type',
  27608. ' TObject = class',
  27609. ' end;',
  27610. ' TBirdObject = TObject;',
  27611. 'var',
  27612. ' v: jsvalue;',
  27613. ' o: TObject;',
  27614. 'begin',
  27615. ' v:=o;',
  27616. ' v:=TObject(o);',
  27617. ' v:=TBirdObject(o);',
  27618. ' o:=TObject(v);',
  27619. ' o:=TBirdObject(v);',
  27620. ' if v is TObject then ;',
  27621. '']);
  27622. ConvertProgram;
  27623. CheckSource('TestJSValue_ClassInstance',
  27624. LinesToStr([ // statements
  27625. 'rtl.createClass(this, "TObject", null, function () {',
  27626. ' this.$init = function () {',
  27627. ' };',
  27628. ' this.$final = function () {',
  27629. ' };',
  27630. '});',
  27631. 'this.v = undefined;',
  27632. 'this.o = null;',
  27633. '']),
  27634. LinesToStr([ // $mod.$main
  27635. '$mod.v = $mod.o;',
  27636. '$mod.v = $mod.o;',
  27637. '$mod.v = $mod.o;',
  27638. '$mod.o = rtl.getObject($mod.v);',
  27639. '$mod.o = rtl.getObject($mod.v);',
  27640. 'if (rtl.isExt($mod.v, $mod.TObject, 1)) ;',
  27641. '']));
  27642. end;
  27643. procedure TTestModule.TestJSValue_ClassOf;
  27644. begin
  27645. StartProgram(false);
  27646. Add([
  27647. 'type',
  27648. ' TClass = class of TObject;',
  27649. ' TObject = class',
  27650. ' end;',
  27651. ' TBirds = class of TBird;',
  27652. ' TBird = class(TObject) end;',
  27653. 'var',
  27654. ' v: jsvalue;',
  27655. ' c: TClass;',
  27656. 'begin',
  27657. ' v:=c;',
  27658. ' v:=TObject;',
  27659. ' v:=TClass(c);',
  27660. ' v:=TBirds(c);',
  27661. ' c:=TClass(v);',
  27662. ' c:=TBirds(v);',
  27663. ' if v is TClass then ;',
  27664. '']);
  27665. ConvertProgram;
  27666. CheckSource('TestJSValue_ClassOf',
  27667. LinesToStr([ // statements
  27668. 'rtl.createClass(this, "TObject", null, function () {',
  27669. ' this.$init = function () {',
  27670. ' };',
  27671. ' this.$final = function () {',
  27672. ' };',
  27673. '});',
  27674. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  27675. '});',
  27676. 'this.v = undefined;',
  27677. 'this.c = null;',
  27678. '']),
  27679. LinesToStr([ // $mod.$main
  27680. '$mod.v = $mod.c;',
  27681. '$mod.v = $mod.TObject;',
  27682. '$mod.v = $mod.c;',
  27683. '$mod.v = $mod.c;',
  27684. '$mod.c = rtl.getObject($mod.v);',
  27685. '$mod.c = rtl.getObject($mod.v);',
  27686. 'if (rtl.isExt($mod.v, $mod.TObject, 2)) ;',
  27687. '']));
  27688. end;
  27689. procedure TTestModule.TestJSValue_ArrayOfJSValue;
  27690. begin
  27691. StartProgram(false);
  27692. Add([
  27693. 'type',
  27694. ' integer = longint;',
  27695. ' TArray = array of JSValue;',
  27696. ' TArrgh = tarray;',
  27697. ' TArrInt = array of integer;',
  27698. 'var',
  27699. ' v: jsvalue;',
  27700. ' TheArray: tarray = (1,''2'');',
  27701. ' Arr: tarrgh;',
  27702. ' i: integer;',
  27703. ' ArrInt: tarrint;',
  27704. 'begin',
  27705. ' arr:=thearray;',
  27706. ' thearray:=arr;',
  27707. ' setlength(arr,2);',
  27708. ' setlength(thearray,3);',
  27709. ' arr[4]:=v;',
  27710. ' arr[5]:=length(thearray);',
  27711. ' arr[6]:=nil;',
  27712. ' arr[7]:=thearray[8];',
  27713. ' arr[low(arr)]:=high(thearray);',
  27714. ' arr:=arrint;',
  27715. ' arrInt:=tarrint(arr);',
  27716. ' if TheArray = nil then ;',
  27717. ' if nil = TheArray then ;',
  27718. ' if TheArray <> nil then ;',
  27719. ' if nil <> TheArray then ;',
  27720. '']);
  27721. ConvertProgram;
  27722. CheckSource('TestJSValue_ArrayOfJSValue',
  27723. LinesToStr([ // statements
  27724. 'this.v = undefined;',
  27725. 'this.TheArray = [1, "2"];',
  27726. 'this.Arr = [];',
  27727. 'this.i = 0;',
  27728. 'this.ArrInt = [];',
  27729. '']),
  27730. LinesToStr([ // $mod.$main
  27731. '$mod.Arr = rtl.arrayRef($mod.TheArray);',
  27732. '$mod.TheArray = rtl.arrayRef($mod.Arr);',
  27733. '$mod.Arr = rtl.arraySetLength($mod.Arr,undefined,2);',
  27734. '$mod.TheArray = rtl.arraySetLength($mod.TheArray,undefined,3);',
  27735. '$mod.Arr[4] = $mod.v;',
  27736. '$mod.Arr[5] = rtl.length($mod.TheArray);',
  27737. '$mod.Arr[6] = null;',
  27738. '$mod.Arr[7] = $mod.TheArray[8];',
  27739. '$mod.Arr[0] = rtl.length($mod.TheArray) - 1;',
  27740. '$mod.Arr = rtl.arrayRef($mod.ArrInt);',
  27741. '$mod.ArrInt = $mod.Arr;',
  27742. 'if (rtl.length($mod.TheArray) === 0) ;',
  27743. 'if (rtl.length($mod.TheArray) === 0) ;',
  27744. 'if (rtl.length($mod.TheArray) > 0) ;',
  27745. 'if (rtl.length($mod.TheArray) > 0) ;',
  27746. '']));
  27747. end;
  27748. procedure TTestModule.TestJSValue_ArrayLit;
  27749. begin
  27750. StartProgram(false);
  27751. Add([
  27752. 'type',
  27753. ' TFlag = (big,small);',
  27754. ' TArray = array of JSValue;',
  27755. ' TObject = class end;',
  27756. ' TClass = class of TObject;',
  27757. 'var',
  27758. ' v: jsvalue;',
  27759. ' a: TArray;',
  27760. ' o: TObject;',
  27761. 'begin',
  27762. ' a:=[];',
  27763. ' a:=[1];',
  27764. ' a:=[1,2];',
  27765. ' a:=[big];',
  27766. ' a:=[1,big];',
  27767. ' a:=[o,nil];',
  27768. '']);
  27769. ConvertProgram;
  27770. CheckSource('TestJSValue_ArrayLit',
  27771. LinesToStr([ // statements
  27772. 'this.TFlag = {',
  27773. ' "0": "big",',
  27774. ' big: 0,',
  27775. ' "1": "small",',
  27776. ' small: 1',
  27777. '};',
  27778. 'rtl.createClass(this, "TObject", null, function () {',
  27779. ' this.$init = function () {',
  27780. ' };',
  27781. ' this.$final = function () {',
  27782. ' };',
  27783. '});',
  27784. 'this.v = undefined;',
  27785. 'this.a = [];',
  27786. 'this.o = null;',
  27787. '']),
  27788. LinesToStr([ // $mod.$main
  27789. '$mod.a = [];',
  27790. '$mod.a = [1];',
  27791. '$mod.a = [1, 2];',
  27792. '$mod.a = [$mod.TFlag.big];',
  27793. '$mod.a = [1, $mod.TFlag.big];',
  27794. '$mod.a = [$mod.o, null];',
  27795. '']));
  27796. end;
  27797. procedure TTestModule.TestJSValue_Params;
  27798. begin
  27799. StartProgram(false);
  27800. Add('type');
  27801. Add(' integer = longint;');
  27802. Add(' TYesNo = boolean;');
  27803. Add(' TFloat = double;');
  27804. Add(' TCaption = string;');
  27805. Add(' TChar = char;');
  27806. Add('function DoIt(a: jsvalue; const b: jsvalue; var c: jsvalue; out d: jsvalue): jsvalue;');
  27807. Add('var');
  27808. Add(' l: jsvalue;');
  27809. Add('begin');
  27810. Add(' a:=a;');
  27811. Add(' l:=b;');
  27812. Add(' c:=c;');
  27813. Add(' d:=d;');
  27814. Add(' Result:=l;');
  27815. Add('end;');
  27816. Add('function DoSome(a: jsvalue; const b: jsvalue): jsvalue; begin end;');
  27817. Add('var');
  27818. Add(' v: jsvalue;');
  27819. Add(' i: integer;');
  27820. Add(' b: TYesNo;');
  27821. Add(' d: TFloat;');
  27822. Add(' s: TCaption;');
  27823. Add(' c: TChar;');
  27824. Add('begin');
  27825. Add(' v:=doit(v,v,v,v);');
  27826. Add(' i:=integer(dosome(i,i));');
  27827. Add(' b:=TYesNo(dosome(b,b));');
  27828. Add(' d:=TFloat(dosome(d,d));');
  27829. Add(' s:=TCaption(dosome(s,s));');
  27830. Add(' c:=TChar(dosome(c,c));');
  27831. ConvertProgram;
  27832. CheckSource('TestJSValue_Params',
  27833. LinesToStr([ // statements
  27834. 'this.DoIt = function (a, b, c, d) {',
  27835. ' var Result = undefined;',
  27836. ' var l = undefined;',
  27837. ' a = a;',
  27838. ' l = b;',
  27839. ' c.set(c.get());',
  27840. ' d.set(d.get());',
  27841. ' Result = l;',
  27842. ' return Result;',
  27843. '};',
  27844. 'this.DoSome = function (a, b) {',
  27845. ' var Result = undefined;',
  27846. ' return Result;',
  27847. '};',
  27848. 'this.v = undefined;',
  27849. 'this.i = 0;',
  27850. 'this.b = false;',
  27851. 'this.d = 0.0;',
  27852. 'this.s = "";',
  27853. 'this.c = "";',
  27854. '']),
  27855. LinesToStr([ // $mod.$main
  27856. '$mod.v = $mod.DoIt($mod.v, $mod.v, {',
  27857. ' p: $mod,',
  27858. ' get: function () {',
  27859. ' return this.p.v;',
  27860. ' },',
  27861. ' set: function (v) {',
  27862. ' this.p.v = v;',
  27863. ' }',
  27864. '}, {',
  27865. ' p: $mod,',
  27866. ' get: function () {',
  27867. ' return this.p.v;',
  27868. ' },',
  27869. ' set: function (v) {',
  27870. ' this.p.v = v;',
  27871. ' }',
  27872. '});',
  27873. '$mod.i = rtl.trunc($mod.DoSome($mod.i, $mod.i));',
  27874. '$mod.b = !($mod.DoSome($mod.b, $mod.b) == false);',
  27875. '$mod.d = rtl.getNumber($mod.DoSome($mod.d, $mod.d));',
  27876. '$mod.s = "" + $mod.DoSome($mod.s, $mod.s);',
  27877. '$mod.c = rtl.getChar($mod.DoSome($mod.c, $mod.c));',
  27878. '']));
  27879. end;
  27880. procedure TTestModule.TestJSValue_UntypedParam;
  27881. begin
  27882. StartProgram(false);
  27883. Add('function DoIt(const a; var b; out c): jsvalue;');
  27884. Add('begin');
  27885. Add(' Result:=a;');
  27886. Add(' Result:=b;');
  27887. Add(' Result:=c;');
  27888. Add(' b:=Result;');
  27889. Add(' c:=Result;');
  27890. Add('end;');
  27891. Add('var i: longint;');
  27892. Add('begin');
  27893. Add(' doit(i,i,i);');
  27894. ConvertProgram;
  27895. CheckSource('TestJSValue_UntypedParam',
  27896. LinesToStr([ // statements
  27897. 'this.DoIt = function (a, b, c) {',
  27898. ' var Result = undefined;',
  27899. ' Result = a;',
  27900. ' Result = b.get();',
  27901. ' Result = c.get();',
  27902. ' b.set(Result);',
  27903. ' c.set(Result);',
  27904. ' return Result;',
  27905. '};',
  27906. 'this.i = 0;',
  27907. '']),
  27908. LinesToStr([ // $mod.$main
  27909. '$mod.DoIt($mod.i, {',
  27910. ' p: $mod,',
  27911. ' get: function () {',
  27912. ' return this.p.i;',
  27913. ' },',
  27914. ' set: function (v) {',
  27915. ' this.p.i = v;',
  27916. ' }',
  27917. '}, {',
  27918. ' p: $mod,',
  27919. ' get: function () {',
  27920. ' return this.p.i;',
  27921. ' },',
  27922. ' set: function (v) {',
  27923. ' this.p.i = v;',
  27924. ' }',
  27925. '});',
  27926. '']));
  27927. end;
  27928. procedure TTestModule.TestJSValue_FuncResultType;
  27929. begin
  27930. StartProgram(false);
  27931. Add('type');
  27932. Add(' integer = longint;');
  27933. Add(' TJSValueArray = array of JSValue;');
  27934. Add(' TListSortCompare = function(Item1, Item2: JSValue): Integer;');
  27935. Add('procedure Sort(P: JSValue; aList: TJSValueArray; const Compare: TListSortCompare);');
  27936. Add('begin');
  27937. Add(' while Compare(P,aList[0])>0 do ;');
  27938. Add('end;');
  27939. Add('var');
  27940. Add(' Compare: TListSortCompare;');
  27941. Add(' V: JSValue;');
  27942. Add(' i: integer;');
  27943. Add('begin');
  27944. Add(' if Compare(V,V)>0 then ;');
  27945. Add(' if Compare(i,i)>1 then ;');
  27946. Add(' if Compare(nil,false)>2 then ;');
  27947. Add(' if Compare(1,true)>3 then ;');
  27948. ConvertProgram;
  27949. CheckSource('TestJSValue_UntypedParam',
  27950. LinesToStr([ // statements
  27951. 'this.Sort = function (P, aList, Compare) {',
  27952. ' while (Compare(P, aList[0]) > 0) {',
  27953. ' };',
  27954. '};',
  27955. 'this.Compare = null;',
  27956. 'this.V = undefined;',
  27957. 'this.i = 0;',
  27958. '']),
  27959. LinesToStr([ // $mod.$main
  27960. 'if ($mod.Compare($mod.V, $mod.V) > 0) ;',
  27961. 'if ($mod.Compare($mod.i, $mod.i) > 1) ;',
  27962. 'if ($mod.Compare(null, false) > 2) ;',
  27963. 'if ($mod.Compare(1, true) > 3) ;',
  27964. '']));
  27965. end;
  27966. procedure TTestModule.TestJSValue_ProcType_Assign;
  27967. begin
  27968. StartProgram(false);
  27969. Add('type');
  27970. Add(' integer = longint;');
  27971. Add(' TObject = class');
  27972. Add(' class function GetGlob: integer;');
  27973. Add(' function Getter: integer;');
  27974. Add(' end;');
  27975. Add('class function TObject.GetGlob: integer;');
  27976. Add('var v1: jsvalue;');
  27977. Add('begin');
  27978. Add(' v1:=@GetGlob;');
  27979. Add(' v1:[email protected];');
  27980. Add('end;');
  27981. Add('function TObject.Getter: integer;');
  27982. Add('var v2: jsvalue;');
  27983. Add('begin');
  27984. Add(' v2:=@Getter;');
  27985. Add(' v2:[email protected];');
  27986. Add(' v2:=@GetGlob;');
  27987. Add(' v2:[email protected];');
  27988. Add('end;');
  27989. Add('function GetIt(i: integer): integer;');
  27990. Add('var v3: jsvalue;');
  27991. Add('begin');
  27992. Add(' v3:=@GetIt;');
  27993. Add('end;');
  27994. Add('var');
  27995. Add(' V: JSValue;');
  27996. Add(' o: TObject;');
  27997. Add('begin');
  27998. Add(' v:=@GetIt;');
  27999. Add(' v:[email protected];');
  28000. Add(' v:[email protected];');
  28001. ConvertProgram;
  28002. CheckSource('TestJSValue_ProcType_Assign',
  28003. LinesToStr([ // statements
  28004. 'rtl.createClass(this, "TObject", null, function () {',
  28005. ' this.$init = function () {',
  28006. ' };',
  28007. ' this.$final = function () {',
  28008. ' };',
  28009. ' this.GetGlob = function () {',
  28010. ' var Result = 0;',
  28011. ' var v1 = undefined;',
  28012. ' v1 = rtl.createCallback(this, "GetGlob");',
  28013. ' v1 = rtl.createCallback(this, "GetGlob");',
  28014. ' return Result;',
  28015. ' };',
  28016. ' this.Getter = function () {',
  28017. ' var Result = 0;',
  28018. ' var v2 = undefined;',
  28019. ' v2 = rtl.createCallback(this, "Getter");',
  28020. ' v2 = rtl.createCallback(this, "Getter");',
  28021. ' v2 = rtl.createCallback(this.$class, "GetGlob");',
  28022. ' v2 = rtl.createCallback(this.$class, "GetGlob");',
  28023. ' return Result;',
  28024. ' };',
  28025. '});',
  28026. 'this.GetIt = function (i) {',
  28027. ' var Result = 0;',
  28028. ' var v3 = undefined;',
  28029. ' v3 = $mod.GetIt;',
  28030. ' return Result;',
  28031. '};',
  28032. 'this.V = undefined;',
  28033. 'this.o = null;',
  28034. '']),
  28035. LinesToStr([ // $mod.$main
  28036. '$mod.V = $mod.GetIt;',
  28037. '$mod.V = rtl.createCallback($mod.o, "Getter");',
  28038. '$mod.V = rtl.createCallback($mod.o.$class, "GetGlob");',
  28039. '']));
  28040. end;
  28041. procedure TTestModule.TestJSValue_ProcType_Equal;
  28042. begin
  28043. StartProgram(false);
  28044. Add('type');
  28045. Add(' integer = longint;');
  28046. Add(' TObject = class');
  28047. Add(' class function GetGlob: integer;');
  28048. Add(' function Getter: integer;');
  28049. Add(' end;');
  28050. Add('class function TObject.GetGlob: integer;');
  28051. Add('var v1: jsvalue;');
  28052. Add('begin');
  28053. Add(' if v1=@GetGlob then;');
  28054. Add(' if [email protected] then ;');
  28055. Add('end;');
  28056. Add('function TObject.Getter: integer;');
  28057. Add('var v2: jsvalue;');
  28058. Add('begin');
  28059. Add(' if v2=@Getter then;');
  28060. Add(' if [email protected] then ;');
  28061. Add(' if v2=@GetGlob then;');
  28062. Add(' if [email protected] then;');
  28063. Add('end;');
  28064. Add('function GetIt(i: integer): integer;');
  28065. Add('var v3: jsvalue;');
  28066. Add('begin');
  28067. Add(' if v3=@GetIt then;');
  28068. Add('end;');
  28069. Add('var');
  28070. Add(' V: JSValue;');
  28071. Add(' o: TObject;');
  28072. Add('begin');
  28073. Add(' if v=@GetIt then;');
  28074. Add(' if [email protected] then;');
  28075. Add(' if [email protected] then;');
  28076. Add(' if @GetIt=v then;');
  28077. Add(' if @o.Getter=v then;');
  28078. Add(' if @o.GetGlob=v then;');
  28079. ConvertProgram;
  28080. CheckSource('TestJSValue_ProcType_Equal',
  28081. LinesToStr([ // statements
  28082. 'rtl.createClass(this, "TObject", null, function () {',
  28083. ' this.$init = function () {',
  28084. ' };',
  28085. ' this.$final = function () {',
  28086. ' };',
  28087. ' this.GetGlob = function () {',
  28088. ' var Result = 0;',
  28089. ' var v1 = undefined;',
  28090. ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
  28091. ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
  28092. ' return Result;',
  28093. ' };',
  28094. ' this.Getter = function () {',
  28095. ' var Result = 0;',
  28096. ' var v2 = undefined;',
  28097. ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
  28098. ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
  28099. ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
  28100. ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
  28101. ' return Result;',
  28102. ' };',
  28103. '});',
  28104. 'this.GetIt = function (i) {',
  28105. ' var Result = 0;',
  28106. ' var v3 = undefined;',
  28107. ' if (rtl.eqCallback(v3, $mod.GetIt)) ;',
  28108. ' return Result;',
  28109. '};',
  28110. 'this.V = undefined;',
  28111. 'this.o = null;',
  28112. '']),
  28113. LinesToStr([ // $mod.$main
  28114. 'if (rtl.eqCallback($mod.V, $mod.GetIt)) ;',
  28115. 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o, "Getter"))) ;',
  28116. 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o.$class, "GetGlob"))) ;',
  28117. 'if (rtl.eqCallback($mod.GetIt, $mod.V)) ;',
  28118. 'if (rtl.eqCallback(rtl.createCallback($mod.o, "Getter"), $mod.V)) ;',
  28119. 'if (rtl.eqCallback(rtl.createCallback($mod.o.$class, "GetGlob"), $mod.V)) ;',
  28120. '']));
  28121. end;
  28122. procedure TTestModule.TestJSValue_ProcType_Param;
  28123. begin
  28124. StartProgram(false);
  28125. Add([
  28126. 'type',
  28127. ' variant = jsvalue;',
  28128. ' TArrVariant = array of variant;',
  28129. ' TArrVar2 = TArrVariant;',
  28130. ' TFuncInt = function: longint;',
  28131. 'function GetIt: longint;',
  28132. 'begin',
  28133. 'end;',
  28134. 'procedure DoIt(p: jsvalue; Arr: TArrVar2);',
  28135. 'var v: variant;',
  28136. 'begin',
  28137. ' v:=arr[1];',
  28138. 'end;',
  28139. 'var s: string;',
  28140. 'begin',
  28141. ' DoIt(GetIt,[]);',
  28142. ' DoIt(@GetIt,[]);',
  28143. ' DoIt(1,[s,GetIt]);',
  28144. ' DoIt(1,[s,@GetIt]);',
  28145. '']);
  28146. ConvertProgram;
  28147. CheckSource('TestJSValue_ProcType_Param',
  28148. LinesToStr([ // statements
  28149. 'this.GetIt = function () {',
  28150. ' var Result = 0;',
  28151. ' return Result;',
  28152. '};',
  28153. 'this.DoIt = function (p, Arr) {',
  28154. ' var v = undefined;',
  28155. ' v = Arr[1];',
  28156. '};',
  28157. 'this.s = "";',
  28158. '']),
  28159. LinesToStr([ // $mod.$main
  28160. '$mod.DoIt($mod.GetIt(), []);',
  28161. '$mod.DoIt($mod.GetIt, []);',
  28162. '$mod.DoIt(1, [$mod.s, $mod.GetIt()]);',
  28163. '$mod.DoIt(1, [$mod.s, $mod.GetIt]);',
  28164. '']));
  28165. end;
  28166. procedure TTestModule.TestJSValue_AssignToPointerFail;
  28167. begin
  28168. StartProgram(false);
  28169. Add([
  28170. 'var',
  28171. ' v: JSValue;',
  28172. ' p: Pointer;',
  28173. 'begin',
  28174. ' p:=v;',
  28175. '']);
  28176. SetExpectedPasResolverError('Incompatible types: got "JSValue" expected "Pointer"',
  28177. nIncompatibleTypesGotExpected);
  28178. ConvertProgram;
  28179. end;
  28180. procedure TTestModule.TestJSValue_OverloadDouble;
  28181. begin
  28182. StartProgram(false);
  28183. Add([
  28184. 'type',
  28185. ' integer = longint;',
  28186. ' tdatetime = double;',
  28187. 'procedure DoIt(d: double); begin end;',
  28188. 'procedure DoIt(v: jsvalue); begin end;',
  28189. 'var',
  28190. ' d: double;',
  28191. ' dt: tdatetime;',
  28192. ' i: integer;',
  28193. ' b: byte;',
  28194. ' shi: shortint;',
  28195. ' w: word;',
  28196. ' smi: smallint;',
  28197. ' lw: longword;',
  28198. ' li: longint;',
  28199. ' ni: nativeint;',
  28200. ' nu: nativeuint;',
  28201. 'begin',
  28202. ' DoIt(d);',
  28203. ' DoIt(dt);',
  28204. ' DoIt(i);',
  28205. ' DoIt(b);',
  28206. ' DoIt(shi);',
  28207. ' DoIt(w);',
  28208. ' DoIt(smi);',
  28209. ' DoIt(lw);',
  28210. ' DoIt(li);',
  28211. ' DoIt(ni);',
  28212. ' DoIt(nu);',
  28213. '']);
  28214. ConvertProgram;
  28215. CheckSource('TestJSValue_OverloadDouble',
  28216. LinesToStr([ // statements
  28217. 'this.DoIt = function (d) {',
  28218. '};',
  28219. 'this.DoIt$1 = function (v) {',
  28220. '};',
  28221. 'this.d = 0.0;',
  28222. 'this.dt = 0.0;',
  28223. 'this.i = 0;',
  28224. 'this.b = 0;',
  28225. 'this.shi = 0;',
  28226. 'this.w = 0;',
  28227. 'this.smi = 0;',
  28228. 'this.lw = 0;',
  28229. 'this.li = 0;',
  28230. 'this.ni = 0;',
  28231. 'this.nu = 0;',
  28232. '']),
  28233. LinesToStr([ // $mod.$main
  28234. '$mod.DoIt($mod.d);',
  28235. '$mod.DoIt($mod.dt);',
  28236. '$mod.DoIt$1($mod.i);',
  28237. '$mod.DoIt$1($mod.b);',
  28238. '$mod.DoIt$1($mod.shi);',
  28239. '$mod.DoIt$1($mod.w);',
  28240. '$mod.DoIt$1($mod.smi);',
  28241. '$mod.DoIt$1($mod.lw);',
  28242. '$mod.DoIt$1($mod.li);',
  28243. '$mod.DoIt$1($mod.ni);',
  28244. '$mod.DoIt$1($mod.nu);',
  28245. '']));
  28246. end;
  28247. procedure TTestModule.TestJSValue_OverloadNativeInt;
  28248. begin
  28249. StartProgram(false);
  28250. Add([
  28251. 'type',
  28252. ' integer = longint;',
  28253. ' int53 = nativeint;',
  28254. ' tdatetime = double;',
  28255. 'procedure DoIt(n: nativeint); begin end;',
  28256. 'procedure DoIt(v: jsvalue); begin end;',
  28257. 'var',
  28258. ' d: double;',
  28259. ' dt: tdatetime;',
  28260. ' i: integer;',
  28261. ' b: byte;',
  28262. ' shi: shortint;',
  28263. ' w: word;',
  28264. ' smi: smallint;',
  28265. ' lw: longword;',
  28266. ' li: longint;',
  28267. ' ni: nativeint;',
  28268. ' nu: nativeuint;',
  28269. 'begin',
  28270. ' DoIt(d);',
  28271. ' DoIt(dt);',
  28272. ' DoIt(i);',
  28273. ' DoIt(b);',
  28274. ' DoIt(shi);',
  28275. ' DoIt(w);',
  28276. ' DoIt(smi);',
  28277. ' DoIt(lw);',
  28278. ' DoIt(li);',
  28279. ' DoIt(ni);',
  28280. ' DoIt(nu);',
  28281. '']);
  28282. ConvertProgram;
  28283. CheckSource('TestJSValue_OverloadNativeInt',
  28284. LinesToStr([ // statements
  28285. 'this.DoIt = function (n) {',
  28286. '};',
  28287. 'this.DoIt$1 = function (v) {',
  28288. '};',
  28289. 'this.d = 0.0;',
  28290. 'this.dt = 0.0;',
  28291. 'this.i = 0;',
  28292. 'this.b = 0;',
  28293. 'this.shi = 0;',
  28294. 'this.w = 0;',
  28295. 'this.smi = 0;',
  28296. 'this.lw = 0;',
  28297. 'this.li = 0;',
  28298. 'this.ni = 0;',
  28299. 'this.nu = 0;',
  28300. '']),
  28301. LinesToStr([ // $mod.$main
  28302. '$mod.DoIt$1($mod.d);',
  28303. '$mod.DoIt$1($mod.dt);',
  28304. '$mod.DoIt($mod.i);',
  28305. '$mod.DoIt($mod.b);',
  28306. '$mod.DoIt($mod.shi);',
  28307. '$mod.DoIt($mod.w);',
  28308. '$mod.DoIt($mod.smi);',
  28309. '$mod.DoIt($mod.lw);',
  28310. '$mod.DoIt($mod.li);',
  28311. '$mod.DoIt($mod.ni);',
  28312. '$mod.DoIt($mod.nu);',
  28313. '']));
  28314. end;
  28315. procedure TTestModule.TestJSValue_OverloadWord;
  28316. begin
  28317. StartProgram(false);
  28318. Add([
  28319. 'type',
  28320. ' integer = longint;',
  28321. ' int53 = nativeint;',
  28322. ' tdatetime = double;',
  28323. 'procedure DoIt(w: word); begin end;',
  28324. 'procedure DoIt(v: jsvalue); begin end;',
  28325. 'var',
  28326. ' d: double;',
  28327. ' dt: tdatetime;',
  28328. ' i: integer;',
  28329. ' b: byte;',
  28330. ' shi: shortint;',
  28331. ' w: word;',
  28332. ' smi: smallint;',
  28333. ' lw: longword;',
  28334. ' li: longint;',
  28335. ' ni: nativeint;',
  28336. ' nu: nativeuint;',
  28337. 'begin',
  28338. ' DoIt(d);',
  28339. ' DoIt(dt);',
  28340. ' DoIt(i);',
  28341. ' DoIt(b);',
  28342. ' DoIt(shi);',
  28343. ' DoIt(w);',
  28344. ' DoIt(smi);',
  28345. ' DoIt(lw);',
  28346. ' DoIt(li);',
  28347. ' DoIt(ni);',
  28348. ' DoIt(nu);',
  28349. '']);
  28350. ConvertProgram;
  28351. CheckSource('TestJSValue_OverloadWord',
  28352. LinesToStr([ // statements
  28353. 'this.DoIt = function (w) {',
  28354. '};',
  28355. 'this.DoIt$1 = function (v) {',
  28356. '};',
  28357. 'this.d = 0.0;',
  28358. 'this.dt = 0.0;',
  28359. 'this.i = 0;',
  28360. 'this.b = 0;',
  28361. 'this.shi = 0;',
  28362. 'this.w = 0;',
  28363. 'this.smi = 0;',
  28364. 'this.lw = 0;',
  28365. 'this.li = 0;',
  28366. 'this.ni = 0;',
  28367. 'this.nu = 0;',
  28368. '']),
  28369. LinesToStr([ // $mod.$main
  28370. '$mod.DoIt$1($mod.d);',
  28371. '$mod.DoIt$1($mod.dt);',
  28372. '$mod.DoIt$1($mod.i);',
  28373. '$mod.DoIt($mod.b);',
  28374. '$mod.DoIt($mod.shi);',
  28375. '$mod.DoIt($mod.w);',
  28376. '$mod.DoIt$1($mod.smi);',
  28377. '$mod.DoIt$1($mod.lw);',
  28378. '$mod.DoIt$1($mod.li);',
  28379. '$mod.DoIt$1($mod.ni);',
  28380. '$mod.DoIt$1($mod.nu);',
  28381. '']));
  28382. end;
  28383. procedure TTestModule.TestJSValue_OverloadString;
  28384. begin
  28385. StartProgram(false);
  28386. Add([
  28387. 'type',
  28388. ' uni = string;',
  28389. ' WChar = char;',
  28390. 'procedure DoIt(s: string); begin end;',
  28391. 'procedure DoIt(v: jsvalue); begin end;',
  28392. 'var',
  28393. ' s: string;',
  28394. ' c: char;',
  28395. ' u: uni;',
  28396. 'begin',
  28397. ' DoIt(s);',
  28398. ' DoIt(c);',
  28399. ' DoIt(u);',
  28400. '']);
  28401. ConvertProgram;
  28402. CheckSource('TestJSValue_OverloadString',
  28403. LinesToStr([ // statements
  28404. 'this.DoIt = function (s) {',
  28405. '};',
  28406. 'this.DoIt$1 = function (v) {',
  28407. '};',
  28408. 'this.s = "";',
  28409. 'this.c = "";',
  28410. 'this.u = "";',
  28411. '']),
  28412. LinesToStr([ // $mod.$main
  28413. '$mod.DoIt($mod.s);',
  28414. '$mod.DoIt($mod.c);',
  28415. '$mod.DoIt($mod.u);',
  28416. '']));
  28417. end;
  28418. procedure TTestModule.TestJSValue_OverloadChar;
  28419. begin
  28420. StartProgram(false);
  28421. Add([
  28422. 'type',
  28423. ' uni = string;',
  28424. ' WChar = char;',
  28425. 'procedure DoIt(c: char); begin end;',
  28426. 'procedure DoIt(v: jsvalue); begin end;',
  28427. 'var',
  28428. ' s: string;',
  28429. ' c: char;',
  28430. ' u: uni;',
  28431. 'begin',
  28432. ' DoIt(s);',
  28433. ' DoIt(c);',
  28434. ' DoIt(u);',
  28435. '']);
  28436. ConvertProgram;
  28437. CheckSource('TestJSValue_OverloadChar',
  28438. LinesToStr([ // statements
  28439. 'this.DoIt = function (c) {',
  28440. '};',
  28441. 'this.DoIt$1 = function (v) {',
  28442. '};',
  28443. 'this.s = "";',
  28444. 'this.c = "";',
  28445. 'this.u = "";',
  28446. '']),
  28447. LinesToStr([ // $mod.$main
  28448. '$mod.DoIt$1($mod.s);',
  28449. '$mod.DoIt($mod.c);',
  28450. '$mod.DoIt$1($mod.u);',
  28451. '']));
  28452. end;
  28453. procedure TTestModule.TestJSValue_OverloadPointer;
  28454. begin
  28455. StartProgram(false);
  28456. Add([
  28457. 'type',
  28458. ' TObject = class end;',
  28459. 'procedure DoIt(p: pointer); begin end;',
  28460. 'procedure DoIt(v: jsvalue); begin end;',
  28461. 'var',
  28462. ' o: TObject;',
  28463. 'begin',
  28464. ' DoIt(o);',
  28465. '']);
  28466. ConvertProgram;
  28467. CheckSource('TestJSValue_OverloadPointer',
  28468. LinesToStr([ // statements
  28469. 'rtl.createClass(this, "TObject", null, function () {',
  28470. ' this.$init = function () {',
  28471. ' };',
  28472. ' this.$final = function () {',
  28473. ' };',
  28474. '});',
  28475. 'this.DoIt = function (p) {',
  28476. '};',
  28477. 'this.DoIt$1 = function (v) {',
  28478. '};',
  28479. 'this.o = null;',
  28480. '']),
  28481. LinesToStr([ // $mod.$main
  28482. '$mod.DoIt($mod.o);',
  28483. '']));
  28484. end;
  28485. procedure TTestModule.TestJSValue_ForIn;
  28486. begin
  28487. StartProgram(false);
  28488. Add([
  28489. 'var',
  28490. ' v: JSValue;',
  28491. ' key: string;',
  28492. 'begin',
  28493. ' for key in v do begin',
  28494. ' if key=''abc'' then ;',
  28495. ' end;',
  28496. '']);
  28497. ConvertProgram;
  28498. CheckSource('TestJSValue_ForIn',
  28499. LinesToStr([ // statements
  28500. 'this.v = undefined;',
  28501. 'this.key = "";',
  28502. '']),
  28503. LinesToStr([ // $mod.$main
  28504. 'for ($mod.key in $mod.v) {',
  28505. ' if ($mod.key === "abc") ;',
  28506. '};',
  28507. '']));
  28508. end;
  28509. procedure TTestModule.TestRTTI_IntRange;
  28510. begin
  28511. WithTypeInfo:=true;
  28512. StartProgram(true,[supTypeInfo]);
  28513. Add([
  28514. '{$modeswitch externalclass}',
  28515. 'type',
  28516. ' TGraphicsColor = -$7FFFFFFF-1..$7FFFFFFF;',
  28517. ' TColor = type TGraphicsColor;',
  28518. 'var',
  28519. ' p: TTypeInfo;',
  28520. ' k: TTypeKind;',
  28521. 'begin',
  28522. ' p:=typeinfo(TGraphicsColor);',
  28523. ' p:=typeinfo(TColor);',
  28524. ' k:=GetTypeKind(TGraphicsColor);',
  28525. ' k:=GetTypeKind(TColor);',
  28526. '']);
  28527. ConvertProgram;
  28528. CheckSource('TestRTTI_IntRange',
  28529. LinesToStr([ // statements
  28530. 'this.$rtti.$Int("TGraphicsColor", {',
  28531. ' minvalue: -2147483648,',
  28532. ' maxvalue: 2147483647,',
  28533. ' ordtype: 4',
  28534. '});',
  28535. 'this.$rtti.$inherited("TColor", this.$rtti["TGraphicsColor"], {});',
  28536. 'this.p = null;',
  28537. 'this.k = 0;',
  28538. '']),
  28539. LinesToStr([ // $mod.$main
  28540. '$mod.p = $mod.$rtti["TGraphicsColor"];',
  28541. '$mod.p = $mod.$rtti["TColor"];',
  28542. '$mod.k = 1;',
  28543. '$mod.k = 1;',
  28544. '']));
  28545. end;
  28546. procedure TTestModule.TestRTTI_Double;
  28547. begin
  28548. WithTypeInfo:=true;
  28549. StartProgram(true,[supTypeInfo]);
  28550. Add([
  28551. '{$modeswitch externalclass}',
  28552. 'type',
  28553. ' TFloat = type double;',
  28554. 'var',
  28555. ' p: TTypeInfo;',
  28556. 'begin',
  28557. ' p:=typeinfo(double);',
  28558. ' p:=typeinfo(TFloat);',
  28559. '']);
  28560. ConvertProgram;
  28561. CheckSource('TestRTTI_Double',
  28562. LinesToStr([ // statements
  28563. 'this.$rtti.$inherited("TFloat", rtl.double, {});',
  28564. 'this.p = null;',
  28565. '']),
  28566. LinesToStr([ // $mod.$main
  28567. '$mod.p = rtl.double;',
  28568. '$mod.p = $mod.$rtti["TFloat"];',
  28569. '']));
  28570. end;
  28571. procedure TTestModule.TestRTTI_ProcType;
  28572. begin
  28573. WithTypeInfo:=true;
  28574. StartProgram(false);
  28575. Add('type');
  28576. Add(' TProcA = procedure;');
  28577. Add(' TMethodB = procedure of object;');
  28578. Add(' TProcC = procedure; varargs;');
  28579. Add(' TProcD = procedure(i: longint; const j: string; var c: char; out d: double);');
  28580. Add(' TProcE = function: nativeint;');
  28581. Add(' TProcF = function(const p: TProcA): nativeuint;');
  28582. Add('var p: pointer;');
  28583. Add('begin');
  28584. Add(' p:=typeinfo(tproca);');
  28585. ConvertProgram;
  28586. CheckSource('TestRTTI_ProcType',
  28587. LinesToStr([ // statements
  28588. 'this.$rtti.$ProcVar("TProcA", {',
  28589. ' procsig: rtl.newTIProcSig([])',
  28590. '});',
  28591. 'this.$rtti.$MethodVar("TMethodB", {',
  28592. ' procsig: rtl.newTIProcSig([]),',
  28593. ' methodkind: 0',
  28594. '});',
  28595. 'this.$rtti.$ProcVar("TProcC", {',
  28596. ' procsig: rtl.newTIProcSig([], null, 2)',
  28597. '});',
  28598. 'this.$rtti.$ProcVar("TProcD", {',
  28599. ' procsig: rtl.newTIProcSig([["i", rtl.longint], ["j", rtl.string, 2], ["c", rtl.char, 1], ["d", rtl.double, 4]])',
  28600. '});',
  28601. 'this.$rtti.$ProcVar("TProcE", {',
  28602. ' procsig: rtl.newTIProcSig([], rtl.nativeint)',
  28603. '});',
  28604. 'this.$rtti.$ProcVar("TProcF", {',
  28605. ' procsig: rtl.newTIProcSig([["p", this.$rtti["TProcA"], 2]], rtl.nativeuint)',
  28606. '});',
  28607. 'this.p = null;',
  28608. '']),
  28609. LinesToStr([ // $mod.$main
  28610. '$mod.p = $mod.$rtti["TProcA"];',
  28611. '']));
  28612. end;
  28613. procedure TTestModule.TestRTTI_ProcType_ArgFromOtherUnit;
  28614. begin
  28615. WithTypeInfo:=true;
  28616. AddModuleWithIntfImplSrc('unit2.pas',
  28617. LinesToStr([
  28618. 'type',
  28619. ' TObject = class end;'
  28620. ]),
  28621. '');
  28622. StartUnit(true);
  28623. Add('interface');
  28624. Add('uses unit2;');
  28625. Add('type');
  28626. Add(' TProcA = function(o: tobject): tobject;');
  28627. Add('implementation');
  28628. Add('type');
  28629. Add(' TProcB = function(o: tobject): tobject;');
  28630. Add('var p: Pointer;');
  28631. Add('initialization');
  28632. Add(' p:=typeinfo(tproca);');
  28633. Add(' p:=typeinfo(tprocb);');
  28634. ConvertUnit;
  28635. CheckSource('TestRTTI_ProcType_ArgFromOtherUnit',
  28636. LinesToStr([ // statements
  28637. 'var $impl = $mod.$impl;',
  28638. 'this.$rtti.$ProcVar("TProcA", {',
  28639. ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
  28640. '});',
  28641. '']),
  28642. LinesToStr([ // this.$init
  28643. '$impl.p = $mod.$rtti["TProcA"];',
  28644. '$impl.p = $mod.$rtti["TProcB"];',
  28645. '']),
  28646. LinesToStr([ // implementation
  28647. '$mod.$rtti.$ProcVar("TProcB", {',
  28648. ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
  28649. '});',
  28650. '$impl.p = null;',
  28651. '']) );
  28652. end;
  28653. procedure TTestModule.TestRTTI_EnumAndSetType;
  28654. begin
  28655. WithTypeInfo:=true;
  28656. StartProgram(false);
  28657. Add('type');
  28658. Add(' TFlag = (light,dark);');
  28659. Add(' TFlags = set of TFlag;');
  28660. Add(' TProc = function(f: TFlags): TFlag;');
  28661. Add('var p: pointer;');
  28662. Add('begin');
  28663. Add(' p:=typeinfo(tflag);');
  28664. Add(' p:=typeinfo(tflags);');
  28665. ConvertProgram;
  28666. CheckSource('TestRTTI_EnumAndType',
  28667. LinesToStr([ // statements
  28668. 'this.TFlag = {',
  28669. ' "0": "light",',
  28670. ' light: 0,',
  28671. ' "1": "dark",',
  28672. ' dark: 1',
  28673. '};',
  28674. 'this.$rtti.$Enum("TFlag", {',
  28675. ' minvalue: 0,',
  28676. ' maxvalue: 1,',
  28677. ' ordtype: 1,',
  28678. ' enumtype: this.TFlag',
  28679. '});',
  28680. 'this.$rtti.$Set("TFlags", {',
  28681. ' comptype: this.$rtti["TFlag"]',
  28682. '});',
  28683. 'this.$rtti.$ProcVar("TProc", {',
  28684. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TFlags"]]], this.$rtti["TFlag"])',
  28685. '});',
  28686. 'this.p = null;',
  28687. '']),
  28688. LinesToStr([ // $mod.$main
  28689. '$mod.p = $mod.$rtti["TFlag"];',
  28690. '$mod.p = $mod.$rtti["TFlags"];',
  28691. '']));
  28692. end;
  28693. procedure TTestModule.TestRTTI_EnumRange;
  28694. begin
  28695. WithTypeInfo:=true;
  28696. StartProgram(false);
  28697. Add([
  28698. 'type',
  28699. ' TCol = (red,green,blue);',
  28700. ' TColRg = green..blue;',
  28701. ' TSetOfColRg = set of TColRg;',
  28702. 'var p: pointer;',
  28703. 'begin',
  28704. ' p:=typeinfo(tcolrg);',
  28705. ' p:=typeinfo(tsetofcolrg);',
  28706. '']);
  28707. ConvertProgram;
  28708. end;
  28709. procedure TTestModule.TestRTTI_AnonymousEnumType;
  28710. begin
  28711. WithTypeInfo:=true;
  28712. StartProgram(false);
  28713. Add('type');
  28714. Add(' TFlags = set of (red, green);');
  28715. Add('var');
  28716. Add(' f: TFlags;');
  28717. Add('begin');
  28718. Add(' Include(f,red);');
  28719. ConvertProgram;
  28720. CheckSource('TestRTTI_AnonymousEnumType',
  28721. LinesToStr([ // statements
  28722. 'this.TFlags$a = {',
  28723. ' "0": "red",',
  28724. ' red: 0,',
  28725. ' "1": "green",',
  28726. ' green: 1',
  28727. '};',
  28728. 'this.$rtti.$Enum("TFlags$a", {',
  28729. ' minvalue: 0,',
  28730. ' maxvalue: 1,',
  28731. ' ordtype: 1,',
  28732. ' enumtype: this.TFlags$a',
  28733. '});',
  28734. 'this.$rtti.$Set("TFlags", {',
  28735. ' comptype: this.$rtti["TFlags$a"]',
  28736. '});',
  28737. 'this.f = {};',
  28738. '']),
  28739. LinesToStr([
  28740. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  28741. '']));
  28742. end;
  28743. procedure TTestModule.TestRTTI_StaticArray;
  28744. begin
  28745. WithTypeInfo:=true;
  28746. StartProgram(false);
  28747. Add('type');
  28748. Add(' TFlag = (light,dark);');
  28749. Add(' TFlagNames = array[TFlag] of string;');
  28750. Add(' TBoolNames = array[boolean] of string;');
  28751. Add(' TByteArray = array[1..32768] of byte;');
  28752. Add(' TProc = function(f: TBoolNames): TFlagNames;');
  28753. Add('var p: pointer;');
  28754. Add('begin');
  28755. Add(' p:=typeinfo(TFlagNames);');
  28756. Add(' p:=typeinfo(TBoolNames);');
  28757. ConvertProgram;
  28758. CheckSource('TestRTTI_StaticArray',
  28759. LinesToStr([ // statements
  28760. 'this.TFlag = {',
  28761. ' "0": "light",',
  28762. ' light: 0,',
  28763. ' "1": "dark",',
  28764. ' dark: 1',
  28765. '};',
  28766. 'this.$rtti.$Enum("TFlag", {',
  28767. ' minvalue: 0,',
  28768. ' maxvalue: 1,',
  28769. ' ordtype: 1,',
  28770. ' enumtype: this.TFlag',
  28771. '});',
  28772. 'this.$rtti.$StaticArray("TFlagNames", {',
  28773. ' dims: [2],',
  28774. ' eltype: rtl.string',
  28775. '});',
  28776. 'this.$rtti.$StaticArray("TBoolNames", {',
  28777. ' dims: [2],',
  28778. ' eltype: rtl.string',
  28779. '});',
  28780. 'this.$rtti.$StaticArray("TByteArray", {',
  28781. ' dims: [32768],',
  28782. ' eltype: rtl.byte',
  28783. '});',
  28784. 'this.$rtti.$ProcVar("TProc", {',
  28785. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TBoolNames"]]], this.$rtti["TFlagNames"])',
  28786. '});',
  28787. 'this.p = null;',
  28788. '']),
  28789. LinesToStr([ // $mod.$main
  28790. '$mod.p = $mod.$rtti["TFlagNames"];',
  28791. '$mod.p = $mod.$rtti["TBoolNames"];',
  28792. '']));
  28793. end;
  28794. procedure TTestModule.TestRTTI_DynArray;
  28795. begin
  28796. WithTypeInfo:=true;
  28797. StartProgram(false);
  28798. Add('type');
  28799. Add(' TArrStr = array of string;');
  28800. Add(' TArr2Dim = array of tarrstr;');
  28801. Add(' TProc = function(f: TArrStr): TArr2Dim;');
  28802. Add('var p: pointer;');
  28803. Add('begin');
  28804. Add(' p:=typeinfo(tarrstr);');
  28805. Add(' p:=typeinfo(tarr2dim);');
  28806. ConvertProgram;
  28807. CheckSource('TestRTTI_DynArray',
  28808. LinesToStr([ // statements
  28809. 'this.$rtti.$DynArray("TArrStr", {',
  28810. ' eltype: rtl.string',
  28811. '});',
  28812. 'this.$rtti.$DynArray("TArr2Dim", {',
  28813. ' eltype: this.$rtti["TArrStr"]',
  28814. '});',
  28815. 'this.$rtti.$ProcVar("TProc", {',
  28816. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TArrStr"]]], this.$rtti["TArr2Dim"])',
  28817. '});',
  28818. 'this.p = null;',
  28819. '']),
  28820. LinesToStr([ // $mod.$main
  28821. '$mod.p = $mod.$rtti["TArrStr"];',
  28822. '$mod.p = $mod.$rtti["TArr2Dim"];',
  28823. '']));
  28824. end;
  28825. procedure TTestModule.TestRTTI_ArrayNestedAnonymous;
  28826. begin
  28827. WithTypeInfo:=true;
  28828. StartProgram(false);
  28829. Add('type');
  28830. Add(' TArr = array of array of longint;');
  28831. Add('var a: TArr;');
  28832. Add('begin');
  28833. ConvertProgram;
  28834. CheckSource('TestRTTI_ArrayNestedAnonymous',
  28835. LinesToStr([ // statements
  28836. 'this.$rtti.$DynArray("TArr$a", {',
  28837. ' eltype: rtl.longint',
  28838. '});',
  28839. 'this.$rtti.$DynArray("TArr", {',
  28840. ' eltype: this.$rtti["TArr$a"]',
  28841. '});',
  28842. 'this.a = [];',
  28843. '']),
  28844. LinesToStr([ // $mod.$main
  28845. ]));
  28846. end;
  28847. procedure TTestModule.TestRTTI_PublishedMethodOverloadFail;
  28848. begin
  28849. WithTypeInfo:=true;
  28850. StartProgram(false);
  28851. Add('type');
  28852. Add(' TObject = class');
  28853. Add(' published');
  28854. Add(' procedure Proc; virtual; abstract;');
  28855. Add(' procedure Proc(Sender: tobject); virtual; abstract;');
  28856. Add(' end;');
  28857. Add('begin');
  28858. SetExpectedPasResolverError('Duplicate published method "Proc" at test1.pp(6,19)',
  28859. nDuplicatePublishedMethodXAtY);
  28860. ConvertProgram;
  28861. end;
  28862. procedure TTestModule.TestRTTI_PublishedMethodHideNoHint;
  28863. begin
  28864. WithTypeInfo:=true;
  28865. StartUnit(false);
  28866. Add([
  28867. 'interface',
  28868. 'type',
  28869. ' TObject = class',
  28870. ' end;',
  28871. ' {$M+}',
  28872. ' TBird = class',
  28873. ' procedure Fly;',
  28874. ' end;',
  28875. ' {$M-}',
  28876. 'type',
  28877. ' TEagle = class(TBird)',
  28878. ' procedure Fly;',
  28879. ' end;',
  28880. 'implementation',
  28881. 'procedure TBird.Fly;',
  28882. 'begin',
  28883. 'end;',
  28884. 'procedure TEagle.Fly;',
  28885. 'begin',
  28886. 'end;',
  28887. '']);
  28888. ConvertUnit;
  28889. CheckSource('TestRTTI_PublishedMethodHideNoHint',
  28890. LinesToStr([ // statements
  28891. 'rtl.createClass(this, "TObject", null, function () {',
  28892. ' this.$init = function () {',
  28893. ' };',
  28894. ' this.$final = function () {',
  28895. ' };',
  28896. '});',
  28897. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  28898. ' this.Fly = function () {',
  28899. ' };',
  28900. ' var $r = this.$rtti;',
  28901. ' $r.addMethod("Fly", 0, []);',
  28902. '});',
  28903. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  28904. ' this.Fly = function () {',
  28905. ' };',
  28906. ' var $r = this.$rtti;',
  28907. ' $r.addMethod("Fly", 0, []);',
  28908. '});',
  28909. '']),
  28910. LinesToStr([ // $mod.$main
  28911. ]));
  28912. CheckResolverUnexpectedHints(true);
  28913. end;
  28914. procedure TTestModule.TestRTTI_PublishedMethodExternalFail;
  28915. begin
  28916. WithTypeInfo:=true;
  28917. StartProgram(false);
  28918. Add('type');
  28919. Add(' TObject = class');
  28920. Add(' published');
  28921. Add(' procedure Proc; external name ''foo'';');
  28922. Add(' end;');
  28923. Add('begin');
  28924. SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
  28925. nPublishedNameMustMatchExternal);
  28926. ConvertProgram;
  28927. end;
  28928. procedure TTestModule.TestRTTI_PublishedClassPropertyFail;
  28929. begin
  28930. WithTypeInfo:=true;
  28931. StartProgram(false);
  28932. Add('type');
  28933. Add(' TObject = class');
  28934. Add(' class var FA: longint;');
  28935. Add(' published');
  28936. Add(' class property A: longint read FA;');
  28937. Add(' end;');
  28938. Add('begin');
  28939. SetExpectedPasResolverError('Invalid published property modifier "class"',
  28940. nInvalidXModifierY);
  28941. ConvertProgram;
  28942. end;
  28943. procedure TTestModule.TestRTTI_PublishedClassFieldFail;
  28944. begin
  28945. WithTypeInfo:=true;
  28946. StartProgram(false);
  28947. Add('type');
  28948. Add(' TObject = class');
  28949. Add(' published');
  28950. Add(' class var FA: longint;');
  28951. Add(' end;');
  28952. Add('begin');
  28953. SetExpectedPasResolverError(sSymbolCannotBePublished,
  28954. nSymbolCannotBePublished);
  28955. ConvertProgram;
  28956. end;
  28957. procedure TTestModule.TestRTTI_PublishedFieldExternalFail;
  28958. begin
  28959. WithTypeInfo:=true;
  28960. StartProgram(false);
  28961. Add('{$modeswitch externalclass}');
  28962. Add('type');
  28963. Add(' TObject = class');
  28964. Add(' published');
  28965. Add(' V: longint; external name ''foo'';');
  28966. Add(' end;');
  28967. Add('begin');
  28968. SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
  28969. nPublishedNameMustMatchExternal);
  28970. ConvertProgram;
  28971. end;
  28972. procedure TTestModule.TestRTTI_Class_Field;
  28973. begin
  28974. WithTypeInfo:=true;
  28975. StartProgram(false);
  28976. Add('{$modeswitch externalclass}');
  28977. Add('type');
  28978. Add(' TObject = class');
  28979. Add(' private');
  28980. Add(' FPropA: string;');
  28981. Add(' published');
  28982. Add(' VarLI: longint;');
  28983. Add(' VarC: char;');
  28984. Add(' VarS: string;');
  28985. Add(' VarD: double;');
  28986. Add(' VarB: boolean;');
  28987. Add(' VarLW: longword;');
  28988. Add(' VarSmI: smallint;');
  28989. Add(' VarW: word;');
  28990. Add(' VarShI: shortint;');
  28991. Add(' VarBy: byte;');
  28992. Add(' VarExt: longint external name ''VarExt'';');
  28993. Add(' ArrA, ArrB: array of byte;');
  28994. Add(' end;');
  28995. Add('var p: pointer;');
  28996. Add(' Obj: tobject;');
  28997. Add('begin');
  28998. Add(' p:=typeinfo(tobject);');
  28999. Add(' p:=typeinfo(p);');
  29000. Add(' p:=typeinfo(obj);');
  29001. ConvertProgram;
  29002. CheckSource('TestRTTI_Class_Field',
  29003. LinesToStr([ // statements
  29004. 'rtl.createClass(this, "TObject", null, function () {',
  29005. ' this.$init = function () {',
  29006. ' this.FPropA = "";',
  29007. ' this.VarLI = 0;',
  29008. ' this.VarC = "";',
  29009. ' this.VarS = "";',
  29010. ' this.VarD = 0.0;',
  29011. ' this.VarB = false;',
  29012. ' this.VarLW = 0;',
  29013. ' this.VarSmI = 0;',
  29014. ' this.VarW = 0;',
  29015. ' this.VarShI = 0;',
  29016. ' this.VarBy = 0;',
  29017. ' this.ArrA = [];',
  29018. ' this.ArrB = [];',
  29019. ' };',
  29020. ' this.$final = function () {',
  29021. ' this.ArrA = undefined;',
  29022. ' this.ArrB = undefined;',
  29023. ' };',
  29024. ' var $r = this.$rtti;',
  29025. ' $r.addField("VarLI", rtl.longint);',
  29026. ' $r.addField("VarC", rtl.char);',
  29027. ' $r.addField("VarS", rtl.string);',
  29028. ' $r.addField("VarD", rtl.double);',
  29029. ' $r.addField("VarB", rtl.boolean);',
  29030. ' $r.addField("VarLW", rtl.longword);',
  29031. ' $r.addField("VarSmI", rtl.smallint);',
  29032. ' $r.addField("VarW", rtl.word);',
  29033. ' $r.addField("VarShI", rtl.shortint);',
  29034. ' $r.addField("VarBy", rtl.byte);',
  29035. ' $r.addField("VarExt", rtl.longint);',
  29036. ' $mod.$rtti.$DynArray("TObject.ArrB$a", {',
  29037. ' eltype: rtl.byte',
  29038. ' });',
  29039. ' $r.addField("ArrA", $mod.$rtti["TObject.ArrB$a"]);',
  29040. ' $r.addField("ArrB", $mod.$rtti["TObject.ArrB$a"]);',
  29041. '});',
  29042. 'this.p = null;',
  29043. 'this.Obj = null;',
  29044. '']),
  29045. LinesToStr([ // $mod.$main
  29046. '$mod.p = $mod.$rtti["TObject"];',
  29047. '$mod.p = rtl.pointer;',
  29048. '$mod.p = $mod.Obj.$rtti;',
  29049. '']));
  29050. end;
  29051. procedure TTestModule.TestRTTI_Class_Method;
  29052. begin
  29053. WithTypeInfo:=true;
  29054. StartProgram(false);
  29055. Add([
  29056. 'type',
  29057. ' TObject = class',
  29058. ' private',
  29059. ' procedure Internal; external name ''$intern'';',
  29060. ' published',
  29061. ' procedure Click; virtual; abstract;',
  29062. ' procedure Notify(Sender: TObject); virtual; abstract;',
  29063. ' function GetNotify: boolean; external name ''GetNotify'';',
  29064. ' procedure Println(a,b: longint); varargs; virtual; abstract;',
  29065. ' function Fetch(URL: string): word; async; external name ''Fetch'';',
  29066. ' end;',
  29067. 'begin']);
  29068. ConvertProgram;
  29069. CheckSource('TestRTTI_Class_Method',
  29070. LinesToStr([ // statements
  29071. 'rtl.createClass(this, "TObject", null, function () {',
  29072. ' this.$init = function () {',
  29073. ' };',
  29074. ' this.$final = function () {',
  29075. ' };',
  29076. ' var $r = this.$rtti;',
  29077. ' $r.addMethod("Click", 0, []);',
  29078. ' $r.addMethod("Notify", 0, [["Sender", $r]]);',
  29079. ' $r.addMethod("GetNotify", 1, [], rtl.boolean, 4);',
  29080. ' $r.addMethod("Println", 0, [["a", rtl.longint], ["b", rtl.longint]], null, 2);',
  29081. ' $r.addMethod("Fetch", 1, [["URL", rtl.string]], rtl.word, 20);',
  29082. '});',
  29083. '']),
  29084. LinesToStr([ // $mod.$main
  29085. '']));
  29086. end;
  29087. procedure TTestModule.TestRTTI_Class_MethodArgFlags;
  29088. begin
  29089. WithTypeInfo:=true;
  29090. StartProgram(false);
  29091. Add('type');
  29092. Add(' TObject = class');
  29093. Add(' published');
  29094. Add(' procedure OpenArray(const Args: array of string); virtual; abstract;');
  29095. Add(' procedure ByRef(var Value: longint; out Item: longint); virtual; abstract;');
  29096. Add(' procedure Untyped(var Value; out Item); virtual; abstract;');
  29097. Add(' end;');
  29098. Add('begin');
  29099. ConvertProgram;
  29100. CheckSource('TestRTTI_Class_MethodOpenArray',
  29101. LinesToStr([ // statements
  29102. 'rtl.createClass(this, "TObject", null, function () {',
  29103. ' this.$init = function () {',
  29104. ' };',
  29105. ' this.$final = function () {',
  29106. ' };',
  29107. ' var $r = this.$rtti;',
  29108. '$r.addMethod("OpenArray", 0, [["Args", rtl.string, 10]]);',
  29109. '$r.addMethod("ByRef", 0, [["Value", rtl.longint, 1], ["Item", rtl.longint, 4]]);',
  29110. '$r.addMethod("Untyped", 0, [["Value", null, 1], ["Item", null, 4]]);',
  29111. '});',
  29112. '']),
  29113. LinesToStr([ // $mod.$main
  29114. '']));
  29115. end;
  29116. procedure TTestModule.TestRTTI_Class_Property;
  29117. begin
  29118. WithTypeInfo:=true;
  29119. StartProgram(false);
  29120. Add('{$modeswitch externalclass}');
  29121. Add('type');
  29122. Add(' TObject = class');
  29123. Add(' private');
  29124. Add(' FColor: longint;');
  29125. Add(' FColorStored: boolean;');
  29126. Add(' procedure SetColor(Value: longint); virtual; abstract;');
  29127. Add(' function GetColor: longint; virtual; abstract;');
  29128. Add(' function GetColorStored: boolean; virtual; abstract;');
  29129. Add(' FExtSize: longint external name ''$extSize'';');
  29130. Add(' FExtSizeStored: boolean external name ''$extSizeStored'';');
  29131. Add(' procedure SetExtSize(Value: longint); external name ''$setSize'';');
  29132. Add(' function GetExtSize: longint; external name ''$getSize'';');
  29133. Add(' function GetExtSizeStored: boolean; external name ''$getExtSizeStored'';');
  29134. Add(' published');
  29135. Add(' property ColorA: longint read FColor;');
  29136. Add(' property ColorB: longint write FColor;');
  29137. Add(' property ColorC: longint read GetColor write SetColor;');
  29138. Add(' property ColorD: longint read FColor write FColor stored FColorStored;');
  29139. Add(' property ExtSizeA: longint read FExtSize write FExtSize;');
  29140. Add(' property ExtSizeB: longint read GetExtSize write SetExtSize stored FExtSizeStored;');
  29141. Add(' property ExtSizeC: longint read FExtSize write FExtSize stored GetExtSizeStored;');
  29142. Add(' end;');
  29143. Add('begin');
  29144. ConvertProgram;
  29145. CheckSource('TestRTTI_Class_Property',
  29146. LinesToStr([ // statements
  29147. 'rtl.createClass(this, "TObject", null, function () {',
  29148. ' this.$init = function () {',
  29149. ' this.FColor = 0;',
  29150. ' this.FColorStored = false;',
  29151. ' };',
  29152. ' this.$final = function () {',
  29153. ' };',
  29154. ' var $r = this.$rtti;',
  29155. ' $r.addProperty("ColorA", 0, rtl.longint, "FColor", "");',
  29156. ' $r.addProperty("ColorB", 0, rtl.longint, "", "FColor");',
  29157. ' $r.addProperty("ColorC", 3, rtl.longint, "GetColor", "SetColor");',
  29158. ' $r.addProperty(',
  29159. ' "ColorD",',
  29160. ' 8,',
  29161. ' rtl.longint,',
  29162. ' "FColor",',
  29163. ' "FColor",',
  29164. ' {',
  29165. ' stored: "FColorStored"',
  29166. ' }',
  29167. ' );',
  29168. ' $r.addProperty("ExtSizeA", 0, rtl.longint, "$extSize", "$extSize");',
  29169. ' $r.addProperty(',
  29170. ' "ExtSizeB",',
  29171. ' 11,',
  29172. ' rtl.longint,',
  29173. ' "$getSize",',
  29174. ' "$setSize",',
  29175. ' {',
  29176. ' stored: "$extSizeStored"',
  29177. ' }',
  29178. ' );',
  29179. ' $r.addProperty(',
  29180. ' "ExtSizeC",',
  29181. ' 12,',
  29182. ' rtl.longint,',
  29183. ' "$extSize",',
  29184. ' "$extSize",',
  29185. ' {',
  29186. ' stored: "$getExtSizeStored"',
  29187. ' }',
  29188. ' );',
  29189. '});',
  29190. '']),
  29191. LinesToStr([ // $mod.$main
  29192. '']));
  29193. end;
  29194. procedure TTestModule.TestRTTI_Class_PropertyParams;
  29195. begin
  29196. WithTypeInfo:=true;
  29197. StartProgram(false);
  29198. Add('{$modeswitch externalclass}');
  29199. Add('type');
  29200. Add(' integer = longint;');
  29201. Add(' TObject = class');
  29202. Add(' private');
  29203. Add(' function GetItems(i: integer): tobject; virtual; abstract;');
  29204. Add(' procedure SetItems(i: integer; value: tobject); virtual; abstract;');
  29205. Add(' function GetValues(const i: integer; var b: boolean): char; virtual; abstract;');
  29206. Add(' procedure SetValues(const i: integer; var b: boolean; value: char); virtual; abstract;');
  29207. Add(' published');
  29208. Add(' property Items[Index: integer]: tobject read getitems write setitems;');
  29209. Add(' property Values[const keya: integer; var keyb: boolean]: char read getvalues write setvalues;');
  29210. Add(' end;');
  29211. Add('begin');
  29212. ConvertProgram;
  29213. CheckSource('TestRTTI_Class_PropertyParams',
  29214. LinesToStr([ // statements
  29215. 'rtl.createClass(this, "TObject", null, function () {',
  29216. ' this.$init = function () {',
  29217. ' };',
  29218. ' this.$final = function () {',
  29219. ' };',
  29220. ' var $r = this.$rtti;',
  29221. ' $r.addProperty("Items", 3, $r, "GetItems", "SetItems");',
  29222. ' $r.addProperty("Values", 3, rtl.char, "GetValues", "SetValues");',
  29223. '});',
  29224. '']),
  29225. LinesToStr([ // $mod.$main
  29226. '']));
  29227. end;
  29228. procedure TTestModule.TestRTTI_Class_OtherUnit_TypeAlias;
  29229. begin
  29230. WithTypeInfo:=true;
  29231. AddModuleWithIntfImplSrc('unit1.pas',
  29232. 'type TColor = -5..5;',
  29233. '');
  29234. StartProgram(true);
  29235. Add([
  29236. 'uses unit1;',
  29237. 'type',
  29238. ' TColorAlias = TColor;',
  29239. ' TColorTypeAlias = type TColor;',
  29240. ' TObject = class',
  29241. ' private',
  29242. ' fColor: TColor;',
  29243. ' fAlias: TColorAlias;',
  29244. ' fTypeAlias: TColorTypeAlias;',
  29245. ' published',
  29246. ' property Color: TColor read fcolor;',
  29247. ' property Alias: TColorAlias read falias;',
  29248. ' property TypeAlias: TColorTypeAlias read ftypealias;',
  29249. ' end;',
  29250. 'begin',
  29251. '']);
  29252. ConvertProgram;
  29253. CheckSource('TestRTTI_Class_OtherUnit_TypeAlias',
  29254. LinesToStr([ // statements
  29255. 'this.$rtti.$inherited("TColorTypeAlias", pas.unit1.$rtti["TColor"], {});',
  29256. 'rtl.createClass(this, "TObject", null, function () {',
  29257. ' this.$init = function () {',
  29258. ' this.fColor = 0;',
  29259. ' this.fAlias = 0;',
  29260. ' this.fTypeAlias = 0;',
  29261. ' };',
  29262. ' this.$final = function () {',
  29263. ' };',
  29264. ' var $r = this.$rtti;',
  29265. ' $r.addProperty("Color", 0, pas.unit1.$rtti["TColor"], "fColor", "");',
  29266. ' $r.addProperty("Alias", 0, pas.unit1.$rtti["TColor"], "fAlias", "");',
  29267. ' $r.addProperty("TypeAlias", 0, $mod.$rtti["TColorTypeAlias"], "fTypeAlias", "");',
  29268. '});',
  29269. '']),
  29270. LinesToStr([ // $mod.$main
  29271. '']));
  29272. end;
  29273. procedure TTestModule.TestRTTI_Class_OmitRTTI;
  29274. begin
  29275. WithTypeInfo:=true;
  29276. StartProgram(false);
  29277. Add([
  29278. '{$modeswitch omitrtti}',
  29279. 'type',
  29280. ' TObject = class',
  29281. ' private',
  29282. ' FA: byte;',
  29283. ' published',
  29284. ' property A: byte read FA write FA;',
  29285. ' end;',
  29286. 'begin']);
  29287. ConvertProgram;
  29288. CheckSource('TestRTTI_Class_OmitRTTI',
  29289. LinesToStr([ // statements
  29290. 'rtl.createClass(this, "TObject", null, function () {',
  29291. ' this.$init = function () {',
  29292. ' this.FA = 0;',
  29293. ' };',
  29294. ' this.$final = function () {',
  29295. ' };',
  29296. '});',
  29297. '']),
  29298. LinesToStr([ // $mod.$main
  29299. '']));
  29300. end;
  29301. procedure TTestModule.TestRTTI_Class_Field_AnonymousArrayOfSelfClass;
  29302. begin
  29303. WithTypeInfo:=true;
  29304. StartUnit(true,[supTObject]);
  29305. Add([
  29306. 'interface',
  29307. 'type',
  29308. ' {$M+}',
  29309. ' TBird = class',
  29310. ' published',
  29311. ' Swarm: array of TBird;',
  29312. ' end;',
  29313. 'implementation',
  29314. '']);
  29315. ConvertUnit;
  29316. CheckSource('TestRTTI_Class_Field_AnonymousArrayOfSelfClass',
  29317. LinesToStr([ // statements
  29318. 'rtl.createClass(this, "TBird", pas.system.TObject, function () {',
  29319. ' this.$init = function () {',
  29320. ' pas.system.TObject.$init.call(this);',
  29321. ' this.Swarm = [];',
  29322. ' };',
  29323. ' this.$final = function () {',
  29324. ' this.Swarm = undefined;',
  29325. ' pas.system.TObject.$final.call(this);',
  29326. ' };',
  29327. ' var $r = this.$rtti;',
  29328. ' $mod.$rtti.$DynArray("TBird.Swarm$a", {',
  29329. ' eltype: $r',
  29330. ' });',
  29331. ' $r.addField("Swarm", $mod.$rtti["TBird.Swarm$a"]);',
  29332. '});',
  29333. '']),
  29334. LinesToStr([ // $mod.$main
  29335. '']));
  29336. end;
  29337. procedure TTestModule.TestRTTI_IndexModifier;
  29338. begin
  29339. WithTypeInfo:=true;
  29340. StartProgram(false);
  29341. Add([
  29342. 'type',
  29343. ' TEnum = (red, blue);',
  29344. ' TObject = class',
  29345. ' FB: boolean;',
  29346. ' procedure SetIntBool(Index: longint; b: boolean); virtual; abstract;',
  29347. ' function GetBoolBool(Index: boolean): boolean; virtual; abstract;',
  29348. ' procedure SetBoolBool(Index: boolean; b: boolean); virtual; abstract;',
  29349. ' function GetEnumBool(Index: TEnum): boolean; virtual; abstract;',
  29350. ' function GetStrIntBool(A: String; I: longint): boolean; virtual; abstract;',
  29351. ' procedure SetStrIntBool(A: String; I: longint; b: boolean); virtual; abstract;',
  29352. ' published',
  29353. ' property B1: boolean index 1 read FB write SetIntBool;',
  29354. ' property B2: boolean index TEnum.blue read GetEnumBool write FB;',
  29355. ' property I1[A: String]: boolean index 2 read GetStrIntBool write SetStrIntBool;',
  29356. ' end;',
  29357. 'begin']);
  29358. ConvertProgram;
  29359. CheckSource('TestRTTI_IndexModifier',
  29360. LinesToStr([ // statements
  29361. 'this.TEnum = {',
  29362. ' "0": "red",',
  29363. ' red: 0,',
  29364. ' "1": "blue",',
  29365. ' blue: 1',
  29366. '};',
  29367. 'this.$rtti.$Enum("TEnum", {',
  29368. ' minvalue: 0,',
  29369. ' maxvalue: 1,',
  29370. ' ordtype: 1,',
  29371. ' enumtype: this.TEnum',
  29372. '});',
  29373. 'rtl.createClass(this, "TObject", null, function () {',
  29374. ' this.$init = function () {',
  29375. ' this.FB = false;',
  29376. ' };',
  29377. ' this.$final = function () {',
  29378. ' };',
  29379. ' var $r = this.$rtti;',
  29380. ' $r.addProperty(',
  29381. ' "B1",',
  29382. ' 18,',
  29383. ' rtl.boolean,',
  29384. ' "FB",',
  29385. ' "SetIntBool",',
  29386. ' {',
  29387. ' index: 1',
  29388. ' }',
  29389. ' );',
  29390. ' $r.addProperty(',
  29391. ' "B2",',
  29392. ' 17,',
  29393. ' rtl.boolean,',
  29394. ' "GetEnumBool",',
  29395. ' "FB",',
  29396. ' {',
  29397. ' index: $mod.TEnum.blue',
  29398. ' }',
  29399. ' );',
  29400. ' $r.addProperty(',
  29401. ' "I1",',
  29402. ' 19,',
  29403. ' rtl.boolean,',
  29404. ' "GetStrIntBool",',
  29405. ' "SetStrIntBool",',
  29406. ' {',
  29407. ' index: 2',
  29408. ' }',
  29409. ' );',
  29410. '});',
  29411. '']),
  29412. LinesToStr([ // $mod.$main
  29413. '']));
  29414. end;
  29415. procedure TTestModule.TestRTTI_StoredModifier;
  29416. begin
  29417. WithTypeInfo:=true;
  29418. StartProgram(false);
  29419. Add([
  29420. 'const',
  29421. ' ConstB = true;',
  29422. 'type',
  29423. ' TObject = class',
  29424. ' private',
  29425. ' FB: boolean;',
  29426. ' function IsBStored: boolean; virtual; abstract;',
  29427. ' published',
  29428. ' property BoolA: boolean read FB stored true;',
  29429. ' property BoolB: boolean read FB stored false;',
  29430. ' property BoolC: boolean read FB stored FB;',
  29431. ' property BoolD: boolean read FB stored ConstB;',
  29432. ' property BoolE: boolean read FB stored IsBStored;',
  29433. ' end;',
  29434. 'begin']);
  29435. ConvertProgram;
  29436. CheckSource('TestRTTI_StoredModifier',
  29437. LinesToStr([ // statements
  29438. 'this.ConstB = true;',
  29439. 'rtl.createClass(this, "TObject", null, function () {',
  29440. ' this.$init = function () {',
  29441. ' this.FB = false;',
  29442. ' };',
  29443. ' this.$final = function () {',
  29444. ' };',
  29445. ' var $r = this.$rtti;',
  29446. ' $r.addProperty("BoolA", 0, rtl.boolean, "FB", "");',
  29447. ' $r.addProperty("BoolB", 4, rtl.boolean, "FB", "");',
  29448. ' $r.addProperty(',
  29449. ' "BoolC",',
  29450. ' 8,',
  29451. ' rtl.boolean,',
  29452. ' "FB",',
  29453. ' "",',
  29454. ' {',
  29455. ' stored: "FB"',
  29456. ' }',
  29457. ' );',
  29458. ' $r.addProperty("BoolD", 0, rtl.boolean, "FB", "");',
  29459. ' $r.addProperty(',
  29460. ' "BoolE",',
  29461. ' 12,',
  29462. ' rtl.boolean,',
  29463. ' "FB",',
  29464. ' "",',
  29465. ' {',
  29466. ' stored: "IsBStored"',
  29467. ' }',
  29468. ' );',
  29469. '});',
  29470. '']),
  29471. LinesToStr([ // $mod.$main
  29472. '']));
  29473. end;
  29474. procedure TTestModule.TestRTTI_DefaultValue;
  29475. begin
  29476. WithTypeInfo:=true;
  29477. StartProgram(false);
  29478. Add([
  29479. 'type',
  29480. ' TEnum = (red, blue);',
  29481. 'const',
  29482. ' CB = true or false;',
  29483. ' CI = 1+2;',
  29484. 'type',
  29485. ' TObject = class',
  29486. ' FB: boolean;',
  29487. ' FI: longint;',
  29488. ' FE: TEnum;',
  29489. ' published',
  29490. ' property B1: boolean read FB default true;',
  29491. ' property B2: boolean read FB default CB;',
  29492. ' property B3: boolean read FB default test1.cb;',
  29493. ' property I1: longint read FI default 2;',
  29494. ' property I2: longint read FI default CI;',
  29495. ' property E1: TEnum read FE default red;',
  29496. ' property E2: TEnum read FE default TEnum.blue;',
  29497. ' end;',
  29498. 'begin']);
  29499. ConvertProgram;
  29500. CheckSource('TestRTTI_DefaultValue',
  29501. LinesToStr([ // statements
  29502. 'this.TEnum = {',
  29503. ' "0": "red",',
  29504. ' red: 0,',
  29505. ' "1": "blue",',
  29506. ' blue: 1',
  29507. '};',
  29508. 'this.$rtti.$Enum("TEnum", {',
  29509. ' minvalue: 0,',
  29510. ' maxvalue: 1,',
  29511. ' ordtype: 1,',
  29512. ' enumtype: this.TEnum',
  29513. '});',
  29514. 'this.CB = true || false;',
  29515. 'this.CI = 1 + 2;',
  29516. 'rtl.createClass(this, "TObject", null, function () {',
  29517. ' this.$init = function () {',
  29518. ' this.FB = false;',
  29519. ' this.FI = 0;',
  29520. ' this.FE = 0;',
  29521. ' };',
  29522. ' this.$final = function () {',
  29523. ' };',
  29524. ' var $r = this.$rtti;',
  29525. ' $r.addProperty(',
  29526. ' "B1",',
  29527. ' 0,',
  29528. ' rtl.boolean,',
  29529. ' "FB",',
  29530. ' "",',
  29531. ' {',
  29532. ' Default: true',
  29533. ' }',
  29534. ' );',
  29535. ' $r.addProperty(',
  29536. ' "B2",',
  29537. ' 0,',
  29538. ' rtl.boolean,',
  29539. ' "FB",',
  29540. ' "",',
  29541. ' {',
  29542. ' Default: true',
  29543. ' }',
  29544. ' );',
  29545. ' $r.addProperty(',
  29546. ' "B3",',
  29547. ' 0,',
  29548. ' rtl.boolean,',
  29549. ' "FB",',
  29550. ' "",',
  29551. ' {',
  29552. ' Default: true',
  29553. ' }',
  29554. ' );',
  29555. ' $r.addProperty(',
  29556. ' "I1",',
  29557. ' 0,',
  29558. ' rtl.longint,',
  29559. ' "FI",',
  29560. ' "",',
  29561. ' {',
  29562. ' Default: 2',
  29563. ' }',
  29564. ' );',
  29565. ' $r.addProperty(',
  29566. ' "I2",',
  29567. ' 0,',
  29568. ' rtl.longint,',
  29569. ' "FI",',
  29570. ' "",',
  29571. ' {',
  29572. ' Default: 3',
  29573. ' }',
  29574. ' );',
  29575. ' $r.addProperty(',
  29576. ' "E1",',
  29577. ' 0,',
  29578. ' $mod.$rtti["TEnum"],',
  29579. ' "FE",',
  29580. ' "",',
  29581. ' {',
  29582. ' Default: $mod.TEnum.red',
  29583. ' }',
  29584. ' );',
  29585. ' $r.addProperty(',
  29586. ' "E2",',
  29587. ' 0,',
  29588. ' $mod.$rtti["TEnum"],',
  29589. ' "FE",',
  29590. ' "",',
  29591. ' {',
  29592. ' Default: $mod.TEnum.blue',
  29593. ' }',
  29594. ' );',
  29595. '});',
  29596. '']),
  29597. LinesToStr([ // $mod.$main
  29598. '']));
  29599. end;
  29600. procedure TTestModule.TestRTTI_DefaultValueSet;
  29601. begin
  29602. WithTypeInfo:=true;
  29603. StartProgram(false);
  29604. Add([
  29605. 'type',
  29606. ' TEnum = (red, blue);',
  29607. ' TSet = set of TEnum;',
  29608. 'const',
  29609. ' CSet = [red,blue];',
  29610. 'type',
  29611. ' TObject = class',
  29612. ' FSet: TSet;',
  29613. ' published',
  29614. ' property Set1: TSet read FSet default [];',
  29615. ' property Set2: TSet read FSet default [red];',
  29616. ' property Set3: TSet read FSet default [red,blue];',
  29617. ' property Set4: TSet read FSet default CSet;',
  29618. ' end;',
  29619. 'begin']);
  29620. ConvertProgram;
  29621. CheckSource('TestRTTI_DefaultValueSet',
  29622. LinesToStr([ // statements
  29623. 'this.TEnum = {',
  29624. ' "0": "red",',
  29625. ' red: 0,',
  29626. ' "1": "blue",',
  29627. ' blue: 1',
  29628. '};',
  29629. 'this.$rtti.$Enum("TEnum", {',
  29630. ' minvalue: 0,',
  29631. ' maxvalue: 1,',
  29632. ' ordtype: 1,',
  29633. ' enumtype: this.TEnum',
  29634. '});',
  29635. 'this.$rtti.$Set("TSet", {',
  29636. ' comptype: this.$rtti["TEnum"]',
  29637. '});',
  29638. 'this.CSet = rtl.createSet(this.TEnum.red, this.TEnum.blue);',
  29639. 'rtl.createClass(this, "TObject", null, function () {',
  29640. ' this.$init = function () {',
  29641. ' this.FSet = {};',
  29642. ' };',
  29643. ' this.$final = function () {',
  29644. ' this.FSet = undefined;',
  29645. ' };',
  29646. ' var $r = this.$rtti;',
  29647. ' $r.addProperty(',
  29648. ' "Set1",',
  29649. ' 0,',
  29650. ' $mod.$rtti["TSet"],',
  29651. ' "FSet",',
  29652. ' "",',
  29653. ' {',
  29654. ' Default: {}',
  29655. ' }',
  29656. ' );',
  29657. ' $r.addProperty(',
  29658. ' "Set2",',
  29659. ' 0,',
  29660. ' $mod.$rtti["TSet"],',
  29661. ' "FSet",',
  29662. ' "",',
  29663. ' {',
  29664. ' Default: rtl.createSet($mod.TEnum.red)',
  29665. ' }',
  29666. ' );',
  29667. ' $r.addProperty(',
  29668. ' "Set3",',
  29669. ' 0,',
  29670. ' $mod.$rtti["TSet"],',
  29671. ' "FSet",',
  29672. ' "",',
  29673. ' {',
  29674. ' Default: rtl.createSet($mod.TEnum.red, $mod.TEnum.blue)',
  29675. ' }',
  29676. ' );',
  29677. ' $r.addProperty(',
  29678. ' "Set4",',
  29679. ' 0,',
  29680. ' $mod.$rtti["TSet"],',
  29681. ' "FSet",',
  29682. ' "",',
  29683. ' {',
  29684. ' Default: $mod.CSet',
  29685. ' }',
  29686. ' );',
  29687. '});',
  29688. '']),
  29689. LinesToStr([ // $mod.$main
  29690. '']));
  29691. end;
  29692. procedure TTestModule.TestRTTI_DefaultValueRangeType;
  29693. begin
  29694. WithTypeInfo:=true;
  29695. StartProgram(false);
  29696. Add([
  29697. 'type',
  29698. ' TRg = -1..1;',
  29699. 'const',
  29700. ' l = low(TRg);',
  29701. ' h = high(TRg);',
  29702. 'type',
  29703. ' TObject = class',
  29704. ' FV: TRg;',
  29705. ' published',
  29706. ' property V1: TRg read FV default -1;',
  29707. ' end;',
  29708. 'begin']);
  29709. ConvertProgram;
  29710. CheckSource('TestRTTI_DefaultValueRangeType',
  29711. LinesToStr([ // statements
  29712. 'this.$rtti.$Int("TRg", {',
  29713. ' minvalue: -1,',
  29714. ' maxvalue: 1,',
  29715. ' ordtype: 0',
  29716. '});',
  29717. 'this.l = -1;',
  29718. 'this.h = 1;',
  29719. 'rtl.createClass(this, "TObject", null, function () {',
  29720. ' this.$init = function () {',
  29721. ' this.FV = 0;',
  29722. ' };',
  29723. ' this.$final = function () {',
  29724. ' };',
  29725. ' var $r = this.$rtti;',
  29726. ' $r.addProperty(',
  29727. ' "V1",',
  29728. ' 0,',
  29729. ' $mod.$rtti["TRg"],',
  29730. ' "FV",',
  29731. ' "",',
  29732. ' {',
  29733. ' Default: -1',
  29734. ' }',
  29735. ' );',
  29736. '});',
  29737. '']),
  29738. LinesToStr([ // $mod.$main
  29739. '']));
  29740. end;
  29741. procedure TTestModule.TestRTTI_DefaultValueInherit;
  29742. begin
  29743. WithTypeInfo:=true;
  29744. StartProgram(false);
  29745. Add([
  29746. 'type',
  29747. ' TObject = class',
  29748. ' FA, FB: byte;',
  29749. ' property A: byte read FA default 1;',
  29750. ' property B: byte read FB default 2;',
  29751. ' end;',
  29752. ' TBird = class',
  29753. ' published',
  29754. ' property A;',
  29755. ' property B nodefault;',
  29756. ' end;',
  29757. 'begin']);
  29758. ConvertProgram;
  29759. CheckSource('TestRTTI_DefaultValueInherit',
  29760. LinesToStr([ // statements
  29761. 'rtl.createClass(this, "TObject", null, function () {',
  29762. ' this.$init = function () {',
  29763. ' this.FA = 0;',
  29764. ' this.FB = 0;',
  29765. ' };',
  29766. ' this.$final = function () {',
  29767. ' };',
  29768. '});',
  29769. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  29770. ' var $r = this.$rtti;',
  29771. ' $r.addProperty(',
  29772. ' "A",',
  29773. ' 0,',
  29774. ' rtl.byte,',
  29775. ' "FA",',
  29776. ' "",',
  29777. ' {',
  29778. ' Default: 1',
  29779. ' }',
  29780. ' );',
  29781. ' $r.addProperty("B", 0, rtl.byte, "FB", "");',
  29782. '});',
  29783. '']),
  29784. LinesToStr([ // $mod.$main
  29785. '']));
  29786. end;
  29787. procedure TTestModule.TestRTTI_OverrideMethod;
  29788. begin
  29789. WithTypeInfo:=true;
  29790. StartProgram(false);
  29791. Add('type');
  29792. Add(' TObject = class');
  29793. Add(' published');
  29794. Add(' procedure DoIt; virtual; abstract;');
  29795. Add(' end;');
  29796. Add(' TSky = class');
  29797. Add(' published');
  29798. Add(' procedure DoIt; override;');
  29799. Add(' end;');
  29800. Add('procedure TSky.DoIt; begin end;');
  29801. Add('begin');
  29802. ConvertProgram;
  29803. CheckSource('TestRTTI_OverrideMethod',
  29804. LinesToStr([ // statements
  29805. 'rtl.createClass(this, "TObject", null, function () {',
  29806. ' this.$init = function () {',
  29807. ' };',
  29808. ' this.$final = function () {',
  29809. ' };',
  29810. ' var $r = this.$rtti;',
  29811. ' $r.addMethod("DoIt", 0, []);',
  29812. '});',
  29813. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  29814. ' this.DoIt = function () {',
  29815. ' };',
  29816. '});',
  29817. '']),
  29818. LinesToStr([ // $mod.$main
  29819. '']));
  29820. end;
  29821. procedure TTestModule.TestRTTI_ReintroduceMethod;
  29822. begin
  29823. WithTypeInfo:=true;
  29824. StartProgram(false);
  29825. Add([
  29826. 'type',
  29827. ' TObject = class',
  29828. ' published',
  29829. ' procedure DoIt;',
  29830. ' end;',
  29831. ' TSky = class',
  29832. ' published',
  29833. ' procedure DoIt; reintroduce;',
  29834. ' end;',
  29835. 'procedure TObject.DoIt; begin end;',
  29836. 'procedure TSky.DoIt;',
  29837. 'begin',
  29838. ' inherited DoIt;',
  29839. 'end;',
  29840. 'begin']);
  29841. ConvertProgram;
  29842. CheckSource('TestRTTI_ReintroduceMethod',
  29843. LinesToStr([ // statements
  29844. 'rtl.createClass(this, "TObject", null, function () {',
  29845. ' this.$init = function () {',
  29846. ' };',
  29847. ' this.$final = function () {',
  29848. ' };',
  29849. ' this.DoIt = function () {',
  29850. ' };',
  29851. ' var $r = this.$rtti;',
  29852. ' $r.addMethod("DoIt", 0, []);',
  29853. '});',
  29854. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  29855. ' this.DoIt = function () {',
  29856. ' $mod.TObject.DoIt.call(this);',
  29857. ' };',
  29858. ' var $r = this.$rtti;',
  29859. ' $r.addMethod("DoIt", 0, []);',
  29860. '});',
  29861. '']),
  29862. LinesToStr([ // $mod.$main
  29863. '']));
  29864. end;
  29865. procedure TTestModule.TestRTTI_OverloadProperty;
  29866. begin
  29867. WithTypeInfo:=true;
  29868. StartProgram(false);
  29869. Add('type');
  29870. Add(' TObject = class');
  29871. Add(' protected');
  29872. Add(' FFlag: longint;');
  29873. Add(' published');
  29874. Add(' property Flag: longint read fflag;');
  29875. Add(' end;');
  29876. Add(' TSky = class');
  29877. Add(' published');
  29878. Add(' property FLAG: longint write fflag;');
  29879. Add(' end;');
  29880. Add('begin');
  29881. ConvertProgram;
  29882. CheckSource('TestRTTI_OverrideMethod',
  29883. LinesToStr([ // statements
  29884. 'rtl.createClass(this, "TObject", null, function () {',
  29885. ' this.$init = function () {',
  29886. ' this.FFlag = 0;',
  29887. ' };',
  29888. ' this.$final = function () {',
  29889. ' };',
  29890. ' var $r = this.$rtti;',
  29891. ' $r.addProperty("Flag", 0, rtl.longint, "FFlag", "");',
  29892. '});',
  29893. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  29894. ' var $r = this.$rtti;',
  29895. ' $r.addProperty("Flag", 0, rtl.longint, "", "FFlag");',
  29896. '});',
  29897. '']),
  29898. LinesToStr([ // $mod.$main
  29899. '']));
  29900. end;
  29901. procedure TTestModule.TestRTTI_ClassForward;
  29902. begin
  29903. WithTypeInfo:=true;
  29904. StartProgram(false);
  29905. Add('type');
  29906. Add(' TObject = class end;');
  29907. Add(' tbridge = class;');
  29908. Add(' TProc = function: tbridge;');
  29909. Add(' TOger = class');
  29910. Add(' published');
  29911. Add(' FBridge: tbridge;');
  29912. Add(' procedure SetBridge(Value: tbridge); virtual; abstract;');
  29913. Add(' property Bridge: tbridge read fbridge write setbridge;');
  29914. Add(' end;');
  29915. Add(' TBridge = class');
  29916. Add(' FOger: toger;');
  29917. Add(' end;');
  29918. Add('var p: Pointer;');
  29919. Add(' b: tbridge;');
  29920. Add('begin');
  29921. Add(' p:=typeinfo(tbridge);');
  29922. Add(' p:=typeinfo(b);');
  29923. ConvertProgram;
  29924. CheckSource('TestRTTI_ClassForward',
  29925. LinesToStr([ // statements
  29926. 'rtl.createClass(this, "TObject", null, function () {',
  29927. ' this.$init = function () {',
  29928. ' };',
  29929. ' this.$final = function () {',
  29930. ' };',
  29931. '});',
  29932. 'this.$rtti.$Class("TBridge");',
  29933. 'this.$rtti.$ProcVar("TProc", {',
  29934. ' procsig: rtl.newTIProcSig([], this.$rtti["TBridge"])',
  29935. '});',
  29936. 'rtl.createClass(this, "TOger", this.TObject, function () {',
  29937. ' this.$init = function () {',
  29938. ' $mod.TObject.$init.call(this);',
  29939. ' this.FBridge = null;',
  29940. ' };',
  29941. ' this.$final = function () {',
  29942. ' this.FBridge = undefined;',
  29943. ' $mod.TObject.$final.call(this);',
  29944. ' };',
  29945. ' var $r = this.$rtti;',
  29946. ' $r.addField("FBridge", $mod.$rtti["TBridge"]);',
  29947. ' $r.addMethod("SetBridge", 0, [["Value", $mod.$rtti["TBridge"]]]);',
  29948. ' $r.addProperty("Bridge", 2, $mod.$rtti["TBridge"], "FBridge", "SetBridge");',
  29949. '});',
  29950. 'rtl.createClass(this, "TBridge", this.TObject, function () {',
  29951. ' this.$init = function () {',
  29952. ' $mod.TObject.$init.call(this);',
  29953. ' this.FOger = null;',
  29954. ' };',
  29955. ' this.$final = function () {',
  29956. ' this.FOger = undefined;',
  29957. ' $mod.TObject.$final.call(this);',
  29958. ' };',
  29959. '});',
  29960. 'this.p = null;',
  29961. 'this.b = null;',
  29962. '']),
  29963. LinesToStr([ // $mod.$main
  29964. '$mod.p = $mod.$rtti["TBridge"];',
  29965. '$mod.p = $mod.b.$rtti;',
  29966. '']));
  29967. end;
  29968. procedure TTestModule.TestRTTI_ClassOf;
  29969. begin
  29970. WithTypeInfo:=true;
  29971. StartProgram(false);
  29972. Add('type');
  29973. Add(' TClass = class of tobject;');
  29974. Add(' TProcA = function: TClass;');
  29975. Add(' TObject = class');
  29976. Add(' published');
  29977. Add(' C: tclass;');
  29978. Add(' end;');
  29979. Add(' tfox = class;');
  29980. Add(' TBird = class end;');
  29981. Add(' TBirds = class of tbird;');
  29982. Add(' TFox = class end;');
  29983. Add(' TFoxes = class of tfox;');
  29984. Add(' TCows = class of TCow;');
  29985. Add(' TCow = class;');
  29986. Add(' TCow = class end;');
  29987. Add('begin');
  29988. ConvertProgram;
  29989. CheckSource('TestRTTI_ClassOf',
  29990. LinesToStr([ // statements
  29991. 'this.$rtti.$Class("TObject");',
  29992. 'this.$rtti.$ClassRef("TClass", {',
  29993. ' instancetype: this.$rtti["TObject"]',
  29994. '});',
  29995. 'this.$rtti.$ProcVar("TProcA", {',
  29996. ' procsig: rtl.newTIProcSig([], this.$rtti["TClass"])',
  29997. '});',
  29998. 'rtl.createClass(this, "TObject", null, function () {',
  29999. ' this.$init = function () {',
  30000. ' this.C = null;',
  30001. ' };',
  30002. ' this.$final = function () {',
  30003. ' this.C = undefined;',
  30004. ' };',
  30005. ' var $r = this.$rtti;',
  30006. ' $r.addField("C", $mod.$rtti["TClass"]);',
  30007. '});',
  30008. 'this.$rtti.$Class("TFox");',
  30009. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  30010. '});',
  30011. 'this.$rtti.$ClassRef("TBirds", {',
  30012. ' instancetype: this.$rtti["TBird"]',
  30013. '});',
  30014. 'rtl.createClass(this, "TFox", this.TObject, function () {',
  30015. '});',
  30016. 'this.$rtti.$ClassRef("TFoxes", {',
  30017. ' instancetype: this.$rtti["TFox"]',
  30018. '});',
  30019. 'this.$rtti.$Class("TCow");',
  30020. 'this.$rtti.$ClassRef("TCows", {',
  30021. ' instancetype: this.$rtti["TCow"]',
  30022. '});',
  30023. 'rtl.createClass(this, "TCow", this.TObject, function () {',
  30024. '});',
  30025. '']),
  30026. LinesToStr([ // $mod.$main
  30027. '']));
  30028. end;
  30029. procedure TTestModule.TestRTTI_Record;
  30030. begin
  30031. WithTypeInfo:=true;
  30032. StartProgram(false);
  30033. Add('type');
  30034. Add(' integer = longint;');
  30035. Add(' TPoint = record');
  30036. Add(' x,y: integer;');
  30037. Add(' end;');
  30038. Add('var p: pointer;');
  30039. Add(' r: tpoint;');
  30040. Add('begin');
  30041. Add(' p:=typeinfo(tpoint);');
  30042. Add(' p:=typeinfo(r);');
  30043. Add(' p:=typeinfo(r.x);');
  30044. ConvertProgram;
  30045. CheckSource('TestRTTI_Record',
  30046. LinesToStr([ // statements
  30047. 'rtl.recNewT(this, "TPoint", function () {',
  30048. ' this.x = 0;',
  30049. ' this.y = 0;',
  30050. ' this.$eq = function (b) {',
  30051. ' return (this.x === b.x) && (this.y === b.y);',
  30052. ' };',
  30053. ' this.$assign = function (s) {',
  30054. ' this.x = s.x;',
  30055. ' this.y = s.y;',
  30056. ' return this;',
  30057. ' };',
  30058. ' var $r = $mod.$rtti.$Record("TPoint", {});',
  30059. ' $r.addField("x", rtl.longint);',
  30060. ' $r.addField("y", rtl.longint);',
  30061. '});',
  30062. 'this.p = null;',
  30063. 'this.r = this.TPoint.$new();',
  30064. '']),
  30065. LinesToStr([ // $mod.$main
  30066. '$mod.p = $mod.$rtti["TPoint"];',
  30067. '$mod.p = $mod.$rtti["TPoint"];',
  30068. '$mod.p = rtl.longint;',
  30069. '']));
  30070. end;
  30071. procedure TTestModule.TestRTTI_RecordAnonymousArray;
  30072. begin
  30073. WithTypeInfo:=true;
  30074. StartProgram(false);
  30075. Add('type');
  30076. Add(' TFloatRec = record');
  30077. Add(' c,d: array of char;');
  30078. // Add(' i: array of array of longint;');
  30079. Add(' end;');
  30080. Add('var p: pointer;');
  30081. Add(' r: tfloatrec;');
  30082. Add('begin');
  30083. Add(' p:=typeinfo(tfloatrec);');
  30084. Add(' p:=typeinfo(r);');
  30085. Add(' p:=typeinfo(r.d);');
  30086. ConvertProgram;
  30087. CheckSource('TestRTTI_Record',
  30088. LinesToStr([ // statements
  30089. 'rtl.recNewT(this, "TFloatRec", function () {',
  30090. ' this.$new = function () {',
  30091. ' var r = Object.create(this);',
  30092. ' r.c = [];',
  30093. ' r.d = [];',
  30094. ' return r;',
  30095. ' };',
  30096. ' this.$eq = function (b) {',
  30097. ' return (this.c === b.c) && (this.d === b.d);',
  30098. ' };',
  30099. ' this.$assign = function (s) {',
  30100. ' this.c = rtl.arrayRef(s.c);',
  30101. ' this.d = rtl.arrayRef(s.d);',
  30102. ' return this;',
  30103. ' };',
  30104. ' var $r = $mod.$rtti.$Record("TFloatRec", {});',
  30105. ' $mod.$rtti.$DynArray("TFloatRec.d$a", {',
  30106. ' eltype: rtl.char',
  30107. ' });',
  30108. ' $r.addField("c", $mod.$rtti["TFloatRec.d$a"]);',
  30109. ' $r.addField("d", $mod.$rtti["TFloatRec.d$a"]);',
  30110. '});',
  30111. 'this.p = null;',
  30112. 'this.r = this.TFloatRec.$new();',
  30113. '']),
  30114. LinesToStr([ // $mod.$main
  30115. '$mod.p = $mod.$rtti["TFloatRec"];',
  30116. '$mod.p = $mod.$rtti["TFloatRec"];',
  30117. '$mod.p = $mod.$rtti["TFloatRec.d$a"];',
  30118. '']));
  30119. end;
  30120. procedure TTestModule.TestRTTI_Record_ClassVarType;
  30121. begin
  30122. WithTypeInfo:=true;
  30123. StartProgram(false);
  30124. Add([
  30125. '{$modeswitch AdvancedRecords}',
  30126. 'type',
  30127. ' TPoint = record',
  30128. ' type TProc = procedure(w: word);',
  30129. ' class var p: TProc;',
  30130. ' end;',
  30131. 'begin',
  30132. '']);
  30133. ConvertProgram;
  30134. CheckSource('TestRTTI_Record_ClassVarType',
  30135. LinesToStr([ // statements
  30136. 'rtl.recNewT(this, "TPoint", function () {',
  30137. ' $mod.$rtti.$ProcVar("TPoint.TProc", {',
  30138. ' procsig: rtl.newTIProcSig([["w", rtl.word]])',
  30139. ' });',
  30140. ' this.p = null;',
  30141. ' this.$eq = function (b) {',
  30142. ' return true;',
  30143. ' };',
  30144. ' this.$assign = function (s) {',
  30145. ' return this;',
  30146. ' };',
  30147. ' var $r = $mod.$rtti.$Record("TPoint", {});',
  30148. ' $r.addField("p", $mod.$rtti["TPoint.TProc"]);',
  30149. '}, true);',
  30150. '']),
  30151. LinesToStr([ // $mod.$main
  30152. '']));
  30153. end;
  30154. procedure TTestModule.TestRTTI_LocalTypes;
  30155. begin
  30156. WithTypeInfo:=true;
  30157. StartProgram(false);
  30158. Add([
  30159. 'procedure DoIt;',
  30160. 'type',
  30161. ' integer = longint;',
  30162. ' TPoint = record',
  30163. ' x,y: integer;',
  30164. ' end;',
  30165. 'var p: TPoint;',
  30166. 'begin',
  30167. 'end;',
  30168. 'begin']);
  30169. ConvertProgram;
  30170. CheckSource('TestRTTI_LocalTypes',
  30171. LinesToStr([ // statements
  30172. 'var TPoint = rtl.recNewT(null, "", function () {',
  30173. ' this.x = 0;',
  30174. ' this.y = 0;',
  30175. ' this.$eq = function (b) {',
  30176. ' return (this.x === b.x) && (this.y === b.y);',
  30177. ' };',
  30178. ' this.$assign = function (s) {',
  30179. ' this.x = s.x;',
  30180. ' this.y = s.y;',
  30181. ' return this;',
  30182. ' };',
  30183. '});',
  30184. 'this.DoIt = function () {',
  30185. ' var p = TPoint.$new();',
  30186. '};',
  30187. '']),
  30188. LinesToStr([ // $mod.$main
  30189. '']));
  30190. end;
  30191. procedure TTestModule.TestRTTI_TypeInfo_BaseTypes;
  30192. begin
  30193. WithTypeInfo:=true;
  30194. StartProgram(false);
  30195. Add([
  30196. 'type',
  30197. ' TCaption = string;',
  30198. ' TYesNo = boolean;',
  30199. ' TLetter = char;',
  30200. ' TFloat = double;',
  30201. ' TPtr = pointer;',
  30202. ' TShortInt = shortint;',
  30203. ' TByte = byte;',
  30204. ' TSmallInt = smallint;',
  30205. ' TWord = word;',
  30206. ' TInt32 = longint;',
  30207. ' TDWord = longword;',
  30208. ' TValue = jsvalue;',
  30209. 'var p: TPtr;',
  30210. 'begin',
  30211. ' p:=typeinfo(string);',
  30212. ' p:=typeinfo(tcaption);',
  30213. ' p:=typeinfo(boolean);',
  30214. ' p:=typeinfo(tyesno);',
  30215. ' p:=typeinfo(char);',
  30216. ' p:=typeinfo(tletter);',
  30217. ' p:=typeinfo(double);',
  30218. ' p:=typeinfo(tfloat);',
  30219. ' p:=typeinfo(pointer);',
  30220. ' p:=typeinfo(tptr);',
  30221. ' p:=typeinfo(shortint);',
  30222. ' p:=typeinfo(tshortint);',
  30223. ' p:=typeinfo(byte);',
  30224. ' p:=typeinfo(tbyte);',
  30225. ' p:=typeinfo(smallint);',
  30226. ' p:=typeinfo(tsmallint);',
  30227. ' p:=typeinfo(word);',
  30228. ' p:=typeinfo(tword);',
  30229. ' p:=typeinfo(longword);',
  30230. ' p:=typeinfo(tdword);',
  30231. ' p:=typeinfo(jsvalue);',
  30232. ' p:=typeinfo(tvalue);',
  30233. '']);
  30234. ConvertProgram;
  30235. CheckSource('TestRTTI_TypeInfo_BaseTypes',
  30236. LinesToStr([ // statements
  30237. 'this.p = null;',
  30238. '']),
  30239. LinesToStr([ // $mod.$main
  30240. '$mod.p = rtl.string;',
  30241. '$mod.p = rtl.string;',
  30242. '$mod.p = rtl.boolean;',
  30243. '$mod.p = rtl.boolean;',
  30244. '$mod.p = rtl.char;',
  30245. '$mod.p = rtl.char;',
  30246. '$mod.p = rtl.double;',
  30247. '$mod.p = rtl.double;',
  30248. '$mod.p = rtl.pointer;',
  30249. '$mod.p = rtl.pointer;',
  30250. '$mod.p = rtl.shortint;',
  30251. '$mod.p = rtl.shortint;',
  30252. '$mod.p = rtl.byte;',
  30253. '$mod.p = rtl.byte;',
  30254. '$mod.p = rtl.smallint;',
  30255. '$mod.p = rtl.smallint;',
  30256. '$mod.p = rtl.word;',
  30257. '$mod.p = rtl.word;',
  30258. '$mod.p = rtl.longword;',
  30259. '$mod.p = rtl.longword;',
  30260. '$mod.p = rtl.jsvalue;',
  30261. '$mod.p = rtl.jsvalue;',
  30262. '']));
  30263. end;
  30264. procedure TTestModule.TestRTTI_TypeInfo_Type_BaseTypes;
  30265. begin
  30266. WithTypeInfo:=true;
  30267. StartProgram(false);
  30268. Add([
  30269. 'type',
  30270. ' TCaption = type string;',
  30271. ' TYesNo = type boolean;',
  30272. ' TLetter = type char;',
  30273. ' TFloat = type double;',
  30274. ' TPtr = type pointer;',
  30275. ' TShortInt = type shortint;',
  30276. ' TByte = type byte;',
  30277. ' TSmallInt = type smallint;',
  30278. ' TWord = type word;',
  30279. ' TInt32 = type longint;',
  30280. ' TDWord = type longword;',
  30281. ' TValue = type jsvalue;',
  30282. ' TAliasValue = type TValue;',
  30283. 'var',
  30284. ' p: TPtr;',
  30285. ' a: TAliasValue;',
  30286. 'begin',
  30287. ' p:=typeinfo(tcaption);',
  30288. ' p:=typeinfo(tyesno);',
  30289. ' p:=typeinfo(tletter);',
  30290. ' p:=typeinfo(tfloat);',
  30291. ' p:=typeinfo(tptr);',
  30292. ' p:=typeinfo(tshortint);',
  30293. ' p:=typeinfo(tbyte);',
  30294. ' p:=typeinfo(tsmallint);',
  30295. ' p:=typeinfo(tword);',
  30296. ' p:=typeinfo(tdword);',
  30297. ' p:=typeinfo(tvalue);',
  30298. ' p:=typeinfo(taliasvalue);',
  30299. ' p:=typeinfo(a);',
  30300. '']);
  30301. ConvertProgram;
  30302. CheckSource('TestRTTI_TypeInfo_Type_BaseTypes',
  30303. LinesToStr([ // statements
  30304. 'this.$rtti.$inherited("TCaption", rtl.string, {});',
  30305. 'this.$rtti.$inherited("TYesNo", rtl.boolean, {});',
  30306. 'this.$rtti.$inherited("TLetter", rtl.char, {});',
  30307. 'this.$rtti.$inherited("TFloat", rtl.double, {});',
  30308. 'this.$rtti.$inherited("TPtr", rtl.pointer, {});',
  30309. 'this.$rtti.$inherited("TShortInt", rtl.shortint, {});',
  30310. 'this.$rtti.$inherited("TByte", rtl.byte, {});',
  30311. 'this.$rtti.$inherited("TSmallInt", rtl.smallint, {});',
  30312. 'this.$rtti.$inherited("TWord", rtl.word, {});',
  30313. 'this.$rtti.$inherited("TInt32", rtl.longint, {});',
  30314. 'this.$rtti.$inherited("TDWord", rtl.longword, {});',
  30315. 'this.$rtti.$inherited("TValue", rtl.jsvalue, {});',
  30316. 'this.$rtti.$inherited("TAliasValue", this.$rtti["TValue"], {});',
  30317. 'this.p = null;',
  30318. 'this.a = undefined;',
  30319. '']),
  30320. LinesToStr([ // $mod.$main
  30321. '$mod.p = $mod.$rtti["TCaption"];',
  30322. '$mod.p = $mod.$rtti["TYesNo"];',
  30323. '$mod.p = $mod.$rtti["TLetter"];',
  30324. '$mod.p = $mod.$rtti["TFloat"];',
  30325. '$mod.p = $mod.$rtti["TPtr"];',
  30326. '$mod.p = $mod.$rtti["TShortInt"];',
  30327. '$mod.p = $mod.$rtti["TByte"];',
  30328. '$mod.p = $mod.$rtti["TSmallInt"];',
  30329. '$mod.p = $mod.$rtti["TWord"];',
  30330. '$mod.p = $mod.$rtti["TDWord"];',
  30331. '$mod.p = $mod.$rtti["TValue"];',
  30332. '$mod.p = $mod.$rtti["TAliasValue"];',
  30333. '$mod.p = $mod.$rtti["TAliasValue"];',
  30334. '']));
  30335. end;
  30336. procedure TTestModule.TestRTTI_TypeInfo_LocalFail;
  30337. begin
  30338. WithTypeInfo:=true;
  30339. StartProgram(false);
  30340. Add('procedure DoIt;');
  30341. Add('type');
  30342. Add(' integer = longint;');
  30343. Add(' TPoint = record');
  30344. Add(' x,y: integer;');
  30345. Add(' end;');
  30346. Add('var p: pointer;');
  30347. Add('begin');
  30348. Add(' p:=typeinfo(tpoint);');
  30349. Add('end;');
  30350. Add('begin');
  30351. SetExpectedPasResolverError(sSymbolCannotBePublished,nSymbolCannotBePublished);
  30352. ConvertProgram;
  30353. end;
  30354. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses1;
  30355. begin
  30356. WithTypeInfo:=true;
  30357. StartProgram(true,[supTypeInfo]);
  30358. Add([
  30359. '{$modeswitch externalclass}',
  30360. 'type',
  30361. ' TFlag = (up,down);',
  30362. ' TFlags = set of TFlag;',
  30363. 'var',
  30364. ' ti: TTypeInfo;',
  30365. ' tiInt: TTypeInfoInteger;',
  30366. ' tiEnum: TTypeInfoEnum;',
  30367. ' tiSet: TTypeInfoSet;',
  30368. 'begin',
  30369. ' ti:=typeinfo(string);',
  30370. ' ti:=typeinfo(boolean);',
  30371. ' ti:=typeinfo(char);',
  30372. ' ti:=typeinfo(double);',
  30373. ' tiInt:=typeinfo(shortint);',
  30374. ' tiInt:=typeinfo(byte);',
  30375. ' tiInt:=typeinfo(smallint);',
  30376. ' tiInt:=typeinfo(word);',
  30377. ' tiInt:=typeinfo(longint);',
  30378. ' tiInt:=typeinfo(longword);',
  30379. ' ti:=typeinfo(jsvalue);',
  30380. ' tiEnum:=typeinfo(tflag);',
  30381. ' tiSet:=typeinfo(tflags);']);
  30382. ConvertProgram;
  30383. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses1',
  30384. LinesToStr([ // statements
  30385. 'this.TFlag = {',
  30386. ' "0": "up",',
  30387. ' up: 0,',
  30388. ' "1": "down",',
  30389. ' down: 1',
  30390. '};',
  30391. 'this.$rtti.$Enum("TFlag", {',
  30392. ' minvalue: 0,',
  30393. ' maxvalue: 1,',
  30394. ' ordtype: 1,',
  30395. ' enumtype: this.TFlag',
  30396. '});',
  30397. 'this.$rtti.$Set("TFlags", {',
  30398. ' comptype: this.$rtti["TFlag"]',
  30399. '});',
  30400. 'this.ti = null;',
  30401. 'this.tiInt = null;',
  30402. 'this.tiEnum = null;',
  30403. 'this.tiSet = null;',
  30404. '']),
  30405. LinesToStr([ // $mod.$main
  30406. '$mod.ti = rtl.string;',
  30407. '$mod.ti = rtl.boolean;',
  30408. '$mod.ti = rtl.char;',
  30409. '$mod.ti = rtl.double;',
  30410. '$mod.tiInt = rtl.shortint;',
  30411. '$mod.tiInt = rtl.byte;',
  30412. '$mod.tiInt = rtl.smallint;',
  30413. '$mod.tiInt = rtl.word;',
  30414. '$mod.tiInt = rtl.longint;',
  30415. '$mod.tiInt = rtl.longword;',
  30416. '$mod.ti = rtl.jsvalue;',
  30417. '$mod.tiEnum = $mod.$rtti["TFlag"];',
  30418. '$mod.tiSet = $mod.$rtti["TFlags"];',
  30419. '']));
  30420. end;
  30421. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses2;
  30422. begin
  30423. WithTypeInfo:=true;
  30424. StartProgram(true,[supTypeInfo]);
  30425. Add('{$modeswitch externalclass}');
  30426. Add('type');
  30427. Add(' TStaticArr = array[boolean] of string;');
  30428. Add(' TDynArr = array of string;');
  30429. Add(' TProc = procedure;');
  30430. Add(' TMethod = procedure of object;');
  30431. Add('var');
  30432. Add(' StaticArray: TStaticArr;');
  30433. Add(' tiStaticArray: TTypeInfoStaticArray;');
  30434. Add(' DynArray: TDynArr;');
  30435. Add(' tiDynArray: TTypeInfoDynArray;');
  30436. Add(' ProcVar: TProc;');
  30437. Add(' tiProcVar: TTypeInfoProcVar;');
  30438. Add(' MethodVar: TMethod;');
  30439. Add(' tiMethodVar: TTypeInfoMethodVar;');
  30440. Add('begin');
  30441. Add(' tiStaticArray:=typeinfo(StaticArray);');
  30442. Add(' tiStaticArray:=typeinfo(TStaticArr);');
  30443. Add(' tiDynArray:=typeinfo(DynArray);');
  30444. Add(' tiDynArray:=typeinfo(TDynArr);');
  30445. Add(' tiProcVar:=typeinfo(ProcVar);');
  30446. Add(' tiProcVar:=typeinfo(TProc);');
  30447. Add(' tiMethodVar:=typeinfo(MethodVar);');
  30448. Add(' tiMethodVar:=typeinfo(TMethod);');
  30449. ConvertProgram;
  30450. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses2',
  30451. LinesToStr([ // statements
  30452. 'this.$rtti.$StaticArray("TStaticArr", {',
  30453. ' dims: [2],',
  30454. ' eltype: rtl.string',
  30455. '});',
  30456. 'this.$rtti.$DynArray("TDynArr", {',
  30457. ' eltype: rtl.string',
  30458. '});',
  30459. 'this.$rtti.$ProcVar("TProc", {',
  30460. ' procsig: rtl.newTIProcSig([])',
  30461. '});',
  30462. 'this.$rtti.$MethodVar("TMethod", {',
  30463. ' procsig: rtl.newTIProcSig([]),',
  30464. ' methodkind: 0',
  30465. '});',
  30466. 'this.StaticArray = rtl.arraySetLength(null,"",2);',
  30467. 'this.tiStaticArray = null;',
  30468. 'this.DynArray = [];',
  30469. 'this.tiDynArray = null;',
  30470. 'this.ProcVar = null;',
  30471. 'this.tiProcVar = null;',
  30472. 'this.MethodVar = null;',
  30473. 'this.tiMethodVar = null;',
  30474. '']),
  30475. LinesToStr([ // $mod.$main
  30476. '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
  30477. '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
  30478. '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
  30479. '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
  30480. '$mod.tiProcVar = $mod.$rtti["TProc"];',
  30481. '$mod.tiProcVar = $mod.$rtti["TProc"];',
  30482. '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
  30483. '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
  30484. '']));
  30485. end;
  30486. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses3;
  30487. begin
  30488. WithTypeInfo:=true;
  30489. StartProgram(true,[supTypeInfo]);
  30490. Add('{$modeswitch externalclass}');
  30491. Add('type');
  30492. Add(' TRec = record end;');
  30493. // ToDo: ^TRec
  30494. Add(' TObject = class end;');
  30495. Add(' TClass = class of tobject;');
  30496. Add('var');
  30497. Add(' Rec: trec;');
  30498. Add(' tiRecord: ttypeinforecord;');
  30499. Add(' Obj: tobject;');
  30500. Add(' tiClass: ttypeinfoclass;');
  30501. Add(' aClass: tclass;');
  30502. Add(' tiClassRef: ttypeinfoclassref;');
  30503. // ToDo: ^TRec
  30504. Add(' tiPointer: ttypeinfopointer;');
  30505. Add('begin');
  30506. Add(' tirecord:=typeinfo(trec);');
  30507. Add(' tirecord:=typeinfo(trec);');
  30508. Add(' ticlass:=typeinfo(obj);');
  30509. Add(' ticlass:=typeinfo(tobject);');
  30510. Add(' ticlass:=typeinfo(aclass);');
  30511. Add(' ticlassref:=typeinfo(tclass);');
  30512. ConvertProgram;
  30513. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses3',
  30514. LinesToStr([ // statements
  30515. 'rtl.recNewT(this, "TRec", function () {',
  30516. ' this.$eq = function (b) {',
  30517. ' return true;',
  30518. ' };',
  30519. ' this.$assign = function (s) {',
  30520. ' return this;',
  30521. ' };',
  30522. ' $mod.$rtti.$Record("TRec", {});',
  30523. '});',
  30524. 'rtl.createClass(this, "TObject", null, function () {',
  30525. ' this.$init = function () {',
  30526. ' };',
  30527. ' this.$final = function () {',
  30528. ' };',
  30529. '});',
  30530. 'this.$rtti.$ClassRef("TClass", {',
  30531. ' instancetype: this.$rtti["TObject"]',
  30532. '});',
  30533. 'this.Rec = this.TRec.$new();',
  30534. 'this.tiRecord = null;',
  30535. 'this.Obj = null;',
  30536. 'this.tiClass = null;',
  30537. 'this.aClass = null;',
  30538. 'this.tiClassRef = null;',
  30539. 'this.tiPointer = null;',
  30540. '']),
  30541. LinesToStr([ // $mod.$main
  30542. '$mod.tiRecord = $mod.$rtti["TRec"];',
  30543. '$mod.tiRecord = $mod.$rtti["TRec"];',
  30544. '$mod.tiClass = $mod.Obj.$rtti;',
  30545. '$mod.tiClass = $mod.$rtti["TObject"];',
  30546. '$mod.tiClass = $mod.aClass.$rtti;',
  30547. '$mod.tiClassRef = $mod.$rtti["TClass"];',
  30548. '']));
  30549. end;
  30550. procedure TTestModule.TestRTTI_TypeInfo_FunctionClassType;
  30551. begin
  30552. WithTypeInfo:=true;
  30553. StartProgram(true,[supTypeInfo]);
  30554. Add([
  30555. '{$modeswitch externalclass}',
  30556. 'type',
  30557. ' TClass = class of tobject;',
  30558. ' TObject = class',
  30559. ' function MyClass: TClass;',
  30560. ' class function ClassType: TClass;',
  30561. ' end;',
  30562. 'function TObject.MyClass: TClass;',
  30563. 'var t: TTypeInfoClass;',
  30564. 'begin',
  30565. ' t:=TypeInfo(Self);',
  30566. ' t:=TypeInfo(Result);',
  30567. ' t:=TypeInfo(TObject);',
  30568. 'end;',
  30569. 'class function TObject.ClassType: TClass;',
  30570. 'var t: TTypeInfoClass;',
  30571. 'begin',
  30572. ' t:=TypeInfo(Self);',
  30573. ' t:=TypeInfo(Result);',
  30574. 'end;',
  30575. 'var',
  30576. ' Obj: TObject;',
  30577. ' t: TTypeInfoClass;',
  30578. 'begin',
  30579. ' t:=TypeInfo(TObject.ClassType);',
  30580. ' t:=TypeInfo(Obj.ClassType);',
  30581. ' t:=TypeInfo(Obj.MyClass);',
  30582. '']);
  30583. ConvertProgram;
  30584. CheckSource('TestRTTI_TypeInfo_FunctionClassType',
  30585. LinesToStr([ // statements
  30586. 'this.$rtti.$Class("TObject");',
  30587. 'this.$rtti.$ClassRef("TClass", {',
  30588. ' instancetype: this.$rtti["TObject"]',
  30589. '});',
  30590. 'rtl.createClass(this, "TObject", null, function () {',
  30591. ' this.$init = function () {',
  30592. ' };',
  30593. ' this.$final = function () {',
  30594. ' };',
  30595. ' this.MyClass = function () {',
  30596. ' var Result = null;',
  30597. ' var t = null;',
  30598. ' t = this.$rtti;',
  30599. ' t = Result.$rtti;',
  30600. ' t = $mod.$rtti["TObject"];',
  30601. ' return Result;',
  30602. ' };',
  30603. ' this.ClassType = function () {',
  30604. ' var Result = null;',
  30605. ' var t = null;',
  30606. ' t = this.$rtti;',
  30607. ' t = Result.$rtti;',
  30608. ' return Result;',
  30609. ' };',
  30610. '});',
  30611. 'this.Obj = null;',
  30612. 'this.t = null;',
  30613. '']),
  30614. LinesToStr([ // $mod.$main
  30615. '$mod.t = $mod.TObject.ClassType().$rtti;',
  30616. '$mod.t = $mod.Obj.$class.ClassType().$rtti;',
  30617. '$mod.t = $mod.Obj.MyClass().$rtti;',
  30618. '']));
  30619. end;
  30620. procedure TTestModule.TestRTTI_TypeInfo_MixedUnits_PointerAndClass;
  30621. begin
  30622. WithTypeInfo:=true;
  30623. AddModuleWithIntfImplSrc('typinfo.pas',
  30624. LinesToStr([
  30625. '{$modeswitch externalclass}',
  30626. 'type',
  30627. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  30628. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo) end;',
  30629. '']),
  30630. '');
  30631. AddModuleWithIntfImplSrc('unit2.pas',
  30632. LinesToStr([
  30633. 'uses typinfo;',
  30634. 'type PTypeInfo = TTypeInfo;', // delphi compatibility code
  30635. 'procedure DoPtr(p: PTypeInfo);',
  30636. 'procedure DoInfo(t: TTypeInfo);',
  30637. 'procedure DoInt(t: TTypeInfoInteger);',
  30638. '']),
  30639. LinesToStr([
  30640. 'procedure DoPtr(p: PTypeInfo);',
  30641. 'begin end;',
  30642. 'procedure DoInfo(t: TTypeInfo);',
  30643. 'begin end;',
  30644. 'procedure DoInt(t: TTypeInfoInteger);',
  30645. 'begin end;',
  30646. '']));
  30647. StartUnit(true);
  30648. Add([
  30649. 'interface',
  30650. 'uses unit2;', // does not use unit typinfo
  30651. 'implementation',
  30652. 'var',
  30653. ' i: byte;',
  30654. ' p: pointer;',
  30655. ' t: PTypeInfo;',
  30656. 'initialization',
  30657. ' p:=typeinfo(i);',
  30658. ' t:=typeinfo(i);',
  30659. ' if p=t then ;',
  30660. ' if p=typeinfo(i) then ;',
  30661. ' if typeinfo(i)=p then ;',
  30662. ' if t=typeinfo(i) then ;',
  30663. ' if typeinfo(i)=t then ;',
  30664. ' DoPtr(p);',
  30665. ' DoPtr(t);',
  30666. ' DoPtr(typeinfo(i));',
  30667. ' DoInfo(p);',
  30668. ' DoInfo(t);',
  30669. ' DoInfo(typeinfo(i));',
  30670. ' DoInt(typeinfo(i));',
  30671. '']);
  30672. ConvertUnit;
  30673. CheckSource('TestRTTI_TypeInfo_MixedUnits_PointerAndClass',
  30674. LinesToStr([ // statements
  30675. 'var $impl = $mod.$impl;',
  30676. '']),
  30677. LinesToStr([ // this.$init
  30678. '$impl.p = rtl.byte;',
  30679. '$impl.t = rtl.byte;',
  30680. 'if ($impl.p === $impl.t) ;',
  30681. 'if ($impl.p === rtl.byte) ;',
  30682. 'if (rtl.byte === $impl.p) ;',
  30683. 'if ($impl.t === rtl.byte) ;',
  30684. 'if (rtl.byte === $impl.t) ;',
  30685. 'pas.unit2.DoPtr($impl.p);',
  30686. 'pas.unit2.DoPtr($impl.t);',
  30687. 'pas.unit2.DoPtr(rtl.byte);',
  30688. 'pas.unit2.DoInfo($impl.p);',
  30689. 'pas.unit2.DoInfo($impl.t);',
  30690. 'pas.unit2.DoInfo(rtl.byte);',
  30691. 'pas.unit2.DoInt(rtl.byte);',
  30692. '']),
  30693. LinesToStr([ // implementation
  30694. '$impl.i = 0;',
  30695. '$impl.p = null;',
  30696. '$impl.t = null;',
  30697. '']) );
  30698. end;
  30699. procedure TTestModule.TestRTTI_Interface_Corba;
  30700. begin
  30701. WithTypeInfo:=true;
  30702. StartProgram(true,[supTypeInfo]);
  30703. Add([
  30704. '{$interfaces corba}',
  30705. '{$modeswitch externalclass}',
  30706. 'type',
  30707. ' IUnknown = interface',
  30708. ' end;',
  30709. ' IBird = interface',
  30710. ' function GetItem: longint;',
  30711. ' procedure SetItem(Value: longint);',
  30712. ' property Item: longint read GetItem write SetItem;',
  30713. ' end;',
  30714. 'procedure DoIt(t: TTypeInfoInterface); begin end;',
  30715. 'var',
  30716. ' i: IBird;',
  30717. ' t: TTypeInfoInterface;',
  30718. 'begin',
  30719. ' t:=TypeInfo(IBird);',
  30720. ' t:=TypeInfo(i);',
  30721. ' DoIt(t);',
  30722. ' DoIt(TypeInfo(IBird));',
  30723. '']);
  30724. ConvertProgram;
  30725. CheckSource('TestRTTI_Interface_Corba',
  30726. LinesToStr([ // statements
  30727. 'rtl.createInterface(',
  30728. ' this,',
  30729. ' "IUnknown",',
  30730. ' "{B92D5841-758A-322B-B800-000000000000}",',
  30731. ' [],',
  30732. ' null,',
  30733. ' function () {',
  30734. ' }',
  30735. ');',
  30736. 'rtl.createInterface(',
  30737. ' this,',
  30738. ' "IBird",',
  30739. ' "{D32D5841-6264-3AE3-A2C9-B91CE922C9B9}",',
  30740. ' ["GetItem", "SetItem"],',
  30741. ' null,',
  30742. ' function () {',
  30743. ' var $r = this.$rtti;',
  30744. ' $r.addMethod("GetItem", 1, [], rtl.longint);',
  30745. ' $r.addMethod("SetItem", 0, [["Value", rtl.longint]]);',
  30746. ' $r.addProperty("Item", 3, rtl.longint, "GetItem", "SetItem");',
  30747. ' }',
  30748. ');',
  30749. 'this.DoIt = function (t) {',
  30750. '}; ',
  30751. 'this.i = null;',
  30752. 'this.t = null;',
  30753. '']),
  30754. LinesToStr([ // $mod.$main
  30755. '$mod.t = $mod.$rtti["IBird"];',
  30756. '$mod.t = $mod.i.$rtti;',
  30757. '$mod.DoIt($mod.t);',
  30758. '$mod.DoIt($mod.$rtti["IBird"]);',
  30759. '']));
  30760. end;
  30761. procedure TTestModule.TestRTTI_Interface_COM;
  30762. begin
  30763. WithTypeInfo:=true;
  30764. StartProgram(true,[supTypeInfo]);
  30765. Add([
  30766. '{$interfaces com}',
  30767. '{$modeswitch externalclass}',
  30768. 'type',
  30769. ' TGuid = record end;',
  30770. ' integer = longint;',
  30771. ' IUnknown = interface',
  30772. ' function QueryInterface(const iid: TGuid; out obj): Integer;',
  30773. ' function _AddRef: Integer;',
  30774. ' function _Release: Integer;',
  30775. ' end;',
  30776. ' IBird = interface',
  30777. ' function GetItem: longint;',
  30778. ' procedure SetItem(Value: longint);',
  30779. ' property Item: longint read GetItem write SetItem;',
  30780. ' end;',
  30781. 'var',
  30782. ' i: IBird;',
  30783. ' t: TTypeInfoInterface;',
  30784. 'begin',
  30785. ' t:=TypeInfo(IBird);',
  30786. ' t:=TypeInfo(i);',
  30787. '']);
  30788. ConvertProgram;
  30789. CheckSource('TestRTTI_Interface_COM',
  30790. LinesToStr([ // statements
  30791. 'rtl.recNewT(this, "TGuid", function () {',
  30792. ' this.$eq = function (b) {',
  30793. ' return true;',
  30794. ' };',
  30795. ' this.$assign = function (s) {',
  30796. ' return this;',
  30797. ' };',
  30798. ' $mod.$rtti.$Record("TGuid", {});',
  30799. '});',
  30800. 'rtl.createInterface(',
  30801. ' this,',
  30802. ' "IUnknown",',
  30803. ' "{D7ADB00D-1A9B-3EDC-B123-730E661DDFA9}",',
  30804. ' ["QueryInterface", "_AddRef", "_Release"],',
  30805. ' null,',
  30806. ' function () {',
  30807. ' this.$kind = "com";',
  30808. ' var $r = this.$rtti;',
  30809. ' $r.addMethod("QueryInterface", 1, [["iid", $mod.$rtti["TGuid"], 2], ["obj", null, 4]], rtl.longint);',
  30810. ' $r.addMethod("_AddRef", 1, [], rtl.longint);',
  30811. ' $r.addMethod("_Release", 1, [], rtl.longint);',
  30812. ' }',
  30813. ');',
  30814. 'rtl.createInterface(',
  30815. ' this,',
  30816. ' "IBird",',
  30817. ' "{9CC77572-0E45-3594-9A88-9E8D865C9E0A}",',
  30818. ' ["GetItem", "SetItem"],',
  30819. ' this.IUnknown,',
  30820. ' function () {',
  30821. ' var $r = this.$rtti;',
  30822. ' $r.addMethod("GetItem", 1, [], rtl.longint);',
  30823. ' $r.addMethod("SetItem", 0, [["Value", rtl.longint]]);',
  30824. ' $r.addProperty("Item", 3, rtl.longint, "GetItem", "SetItem");',
  30825. ' }',
  30826. ');',
  30827. 'this.i = null;',
  30828. 'this.t = null;',
  30829. '']),
  30830. LinesToStr([ // $mod.$main
  30831. '$mod.t = $mod.$rtti["IBird"];',
  30832. '$mod.t = $mod.i.$rtti;',
  30833. '']));
  30834. end;
  30835. procedure TTestModule.TestRTTI_ClassHelper;
  30836. begin
  30837. WithTypeInfo:=true;
  30838. StartProgram(true,[supTypeInfo]);
  30839. Add([
  30840. '{$interfaces com}',
  30841. '{$modeswitch externalclass}',
  30842. 'type',
  30843. ' TObject = class',
  30844. ' end;',
  30845. ' THelper = class helper for TObject',
  30846. ' published',
  30847. ' function GetItem: longint;',
  30848. ' property Item: longint read GetItem;',
  30849. ' end;',
  30850. 'function THelper.GetItem: longint;',
  30851. 'begin',
  30852. 'end;',
  30853. 'var',
  30854. ' t: TTypeInfoHelper;',
  30855. 'begin',
  30856. ' t:=TypeInfo(THelper);',
  30857. '']);
  30858. ConvertProgram;
  30859. CheckSource('TestRTTI_ClassHelper',
  30860. LinesToStr([ // statements
  30861. 'rtl.createClass(this, "TObject", null, function () {',
  30862. ' this.$init = function () {',
  30863. ' };',
  30864. ' this.$final = function () {',
  30865. ' };',
  30866. '});',
  30867. 'rtl.createHelper(this, "THelper", null, function () {',
  30868. ' this.GetItem = function () {',
  30869. ' var Result = 0;',
  30870. ' return Result;',
  30871. ' };',
  30872. ' var $r = this.$rtti;',
  30873. ' $r.addMethod("GetItem", 1, [], rtl.longint);',
  30874. ' $r.addProperty("Item", 1, rtl.longint, "GetItem", "");',
  30875. '});',
  30876. 'this.t = null;',
  30877. '']),
  30878. LinesToStr([ // $mod.$main
  30879. '$mod.t = $mod.$rtti["THelper"];',
  30880. '']));
  30881. end;
  30882. procedure TTestModule.TestRTTI_ExternalClass;
  30883. begin
  30884. WithTypeInfo:=true;
  30885. StartProgram(true,[supTypeInfo]);
  30886. Add([
  30887. '{$modeswitch externalclass}',
  30888. 'type',
  30889. ' TJSObject = class external name ''Object''',
  30890. ' end;',
  30891. ' TJSArray = class external name ''Array'' (TJSObject)',
  30892. ' end;',
  30893. 'var',
  30894. ' p: Pointer;',
  30895. ' tc: TTypeInfoExtClass;',
  30896. 'begin',
  30897. ' p:=typeinfo(TJSArray);']);
  30898. ConvertProgram;
  30899. CheckSource('TestRTTI_ExternalClass',
  30900. LinesToStr([ // statements
  30901. 'this.$rtti.$ExtClass("TJSObject", {',
  30902. ' jsclass: "Object"',
  30903. '});',
  30904. 'this.$rtti.$ExtClass("TJSArray", {',
  30905. ' ancestor: this.$rtti["TJSObject"],',
  30906. ' jsclass: "Array"',
  30907. '});',
  30908. 'this.p = null;',
  30909. 'this.tc = null;',
  30910. '']),
  30911. LinesToStr([ // $mod.$main
  30912. '$mod.p = $mod.$rtti["TJSArray"];',
  30913. '']));
  30914. end;
  30915. procedure TTestModule.TestRTTI_Unit;
  30916. begin
  30917. WithTypeInfo:=true;
  30918. AddModuleWithIntfImplSrc('unit2.pas',
  30919. LinesToStr([
  30920. '{$mode delphi}',
  30921. 'type',
  30922. ' TWordArray = array of word;',
  30923. ' TArray<T> = array of T;',
  30924. '']),
  30925. '');
  30926. StartUnit(true,[supTypeInfo,supTInterfacedObject]);
  30927. Add([
  30928. '{$mode delphi}',
  30929. 'interface',
  30930. 'uses unit2;',
  30931. 'type',
  30932. ' IBird = interface',
  30933. ' function Swoop: TWordArray;',
  30934. ' function Glide: TArray<word>;',
  30935. ' end;',
  30936. 'procedure Fly;',
  30937. 'implementation',
  30938. 'procedure Fly;',
  30939. 'var',
  30940. ' ta: tTypeInfoDynArray;',
  30941. ' ti: tTypeInfoInterface;',
  30942. 'begin',
  30943. ' ta:=typeinfo(TWordArray);',
  30944. ' ta:=typeinfo(TArray<word>);',
  30945. ' ti:=typeinfo(IBird);',
  30946. 'end;',
  30947. '']);
  30948. ConvertUnit;
  30949. CheckSource('TestRTTI_ExternalClass',
  30950. LinesToStr([ // statements
  30951. 'rtl.createInterface(',
  30952. ' this,',
  30953. ' "IBird",',
  30954. ' "{3B98AAAC-6116-3E17-AA85-F16786D85B09}",',
  30955. ' ["Swoop", "Glide"],',
  30956. ' pas.system.IUnknown,',
  30957. ' function () {',
  30958. ' var $r = this.$rtti;',
  30959. ' $r.addMethod("Swoop", 1, [], pas.unit2.$rtti["TWordArray"]);',
  30960. ' $r.addMethod("Glide", 1, [], pas.unit2.$rtti["TArray<System.Word>"]);',
  30961. ' }',
  30962. ');',
  30963. 'this.Fly = function () {',
  30964. ' var ta = null;',
  30965. ' var ti = null;',
  30966. ' ta = pas.unit2.$rtti["TWordArray"];',
  30967. ' ta = pas.unit2.$rtti["TArray<System.Word>"];',
  30968. ' ti = $mod.$rtti["IBird"];',
  30969. '};',
  30970. '']),
  30971. LinesToStr([ // $mod.$main
  30972. '']));
  30973. end;
  30974. procedure TTestModule.TestResourcestringProgram;
  30975. begin
  30976. AddModuleWithIntfImplSrc('unit2.pas',
  30977. LinesToStr([
  30978. 'resourcestring Title = ''Nice'';',
  30979. '']),
  30980. '');
  30981. StartProgram(true);
  30982. Add([
  30983. 'uses unit2;',
  30984. 'const Bar = ''bar'';',
  30985. 'resourcestring',
  30986. ' Red = ''red'';',
  30987. ' Foobar = ''fOo''+bar;',
  30988. 'var s: string;',
  30989. ' c: char;',
  30990. 'begin',
  30991. ' s:=red;',
  30992. ' s:=test1.red;',
  30993. ' s:=Title;',
  30994. ' c:=red[1];',
  30995. ' c:=test1.red[2];',
  30996. ' if red=foobar then ;',
  30997. ' if red[3]=red[4] then ;']);
  30998. ConvertProgram;
  30999. CheckSource('TestResourcestringProgram',
  31000. LinesToStr([ // statements
  31001. 'this.Bar = "bar";',
  31002. 'this.s = "";',
  31003. 'this.c = "";',
  31004. '$mod.$resourcestrings = {',
  31005. ' Red: {',
  31006. ' org: "red"',
  31007. ' },',
  31008. ' Foobar: {',
  31009. ' org: "fOobar"',
  31010. ' }',
  31011. '};',
  31012. '']),
  31013. LinesToStr([ // $mod.$main
  31014. '$mod.s = rtl.getResStr($mod, "Red");',
  31015. '$mod.s = rtl.getResStr($mod, "Red");',
  31016. '$mod.s = rtl.getResStr(pas.unit2, "Title");',
  31017. '$mod.c = rtl.getResStr($mod, "Red").charAt(0);',
  31018. '$mod.c = rtl.getResStr($mod, "Red").charAt(1);',
  31019. 'if (rtl.getResStr($mod, "Red") === rtl.getResStr($mod, "Foobar")) ;',
  31020. 'if (rtl.getResStr($mod, "Red").charAt(2) === rtl.getResStr($mod, "Red").charAt(3)) ;',
  31021. '']));
  31022. end;
  31023. procedure TTestModule.TestResourcestringUnit;
  31024. begin
  31025. AddModuleWithIntfImplSrc('unit2.pas',
  31026. LinesToStr([
  31027. 'resourcestring Title = ''Nice'';',
  31028. '']),
  31029. '');
  31030. StartUnit(true);
  31031. Add([
  31032. 'interface',
  31033. 'uses unit2;',
  31034. 'const Red = ''rEd'';',
  31035. 'resourcestring',
  31036. ' Blue = ''blue'';',
  31037. ' NotRed = ''not''+Red;',
  31038. 'var s: string;',
  31039. 'implementation',
  31040. 'resourcestring',
  31041. ' ImplGreen = ''green'';',
  31042. 'initialization',
  31043. ' s:=blue+ImplGreen;',
  31044. ' s:=test1.blue+test1.implgreen;',
  31045. ' s:=blue[1]+implgreen[2];',
  31046. ' s:=Title;',
  31047. '']);
  31048. ConvertUnit;
  31049. CheckSource('TestResourcestringUnit',
  31050. LinesToStr([ // statements
  31051. 'this.Red = "rEd";',
  31052. 'this.s = "";',
  31053. '$mod.$resourcestrings = {',
  31054. ' Blue: {',
  31055. ' org: "blue"',
  31056. ' },',
  31057. ' NotRed: {',
  31058. ' org: "notrEd"',
  31059. ' },',
  31060. ' ImplGreen: {',
  31061. ' org: "green"',
  31062. ' }',
  31063. '};',
  31064. '']),
  31065. LinesToStr([ // $mod.$main
  31066. '$mod.s = rtl.getResStr($mod, "Blue") + rtl.getResStr($mod, "ImplGreen");',
  31067. '$mod.s = rtl.getResStr($mod, "Blue") + rtl.getResStr($mod, "ImplGreen");',
  31068. '$mod.s = rtl.getResStr($mod, "Blue").charAt(0) + rtl.getResStr($mod, "ImplGreen").charAt(1);',
  31069. '$mod.s = rtl.getResStr(pas.unit2, "Title");',
  31070. '']));
  31071. end;
  31072. procedure TTestModule.TestResourcestringImplementation;
  31073. begin
  31074. StartUnit(false);
  31075. Add([
  31076. 'interface',
  31077. 'implementation',
  31078. 'resourcestring',
  31079. ' ImplRed = ''red'';']);
  31080. ConvertUnit;
  31081. CheckSource('TestResourcestringImplementation',
  31082. LinesToStr([ // intf statements
  31083. 'var $impl = $mod.$impl;']),
  31084. LinesToStr([ // $mod.$init
  31085. '']),
  31086. LinesToStr([ // impl statements
  31087. '$mod.$resourcestrings = {',
  31088. ' ImplRed: {',
  31089. ' org: "red"',
  31090. ' }',
  31091. '};',
  31092. '']));
  31093. end;
  31094. procedure TTestModule.TestAttributes_Members;
  31095. begin
  31096. WithTypeInfo:=true;
  31097. StartProgram(false);
  31098. Add([
  31099. '{$modeswitch PrefixedAttributes}',
  31100. 'type',
  31101. ' TObject = class',
  31102. ' constructor Create;',
  31103. ' end;',
  31104. ' TCustomAttribute = class',
  31105. ' constructor Create(Id: word);',
  31106. ' end;',
  31107. ' [Missing]',
  31108. ' TBird = class',
  31109. ' published',
  31110. ' [Tcustom]',
  31111. ' FField: word;',
  31112. ' [tcustom(14)]',
  31113. ' property Size: word read FField;',
  31114. ' [Tcustom(15)]',
  31115. ' procedure Fly; virtual; abstract;',
  31116. ' end;',
  31117. ' TRec = record',
  31118. ' [Tcustom,tcustom(14)]',
  31119. ' Size: word;',
  31120. ' end;',
  31121. 'constructor TObject.Create; begin end;',
  31122. 'constructor TCustomAttribute.Create(Id: word); begin end;',
  31123. 'begin',
  31124. '']);
  31125. ConvertProgram;
  31126. CheckSource('TestAttributes_Members',
  31127. LinesToStr([ // statements
  31128. 'rtl.createClass(this, "TObject", null, function () {',
  31129. ' this.$init = function () {',
  31130. ' };',
  31131. ' this.$final = function () {',
  31132. ' };',
  31133. ' this.Create = function () {',
  31134. ' return this;',
  31135. ' };',
  31136. '});',
  31137. 'rtl.createClass(this, "TCustomAttribute", this.TObject, function () {',
  31138. ' this.Create$1 = function (Id) {',
  31139. ' return this;',
  31140. ' };',
  31141. '});',
  31142. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  31143. ' this.$init = function () {',
  31144. ' $mod.TObject.$init.call(this);',
  31145. ' this.FField = 0;',
  31146. ' };',
  31147. ' var $r = this.$rtti;',
  31148. ' $r.addField("FField", rtl.word, {',
  31149. ' attr: [$mod.TCustomAttribute, "Create"]',
  31150. ' });',
  31151. ' $r.addProperty(',
  31152. ' "Size",',
  31153. ' 0,',
  31154. ' rtl.word,',
  31155. ' "FField",',
  31156. ' "",',
  31157. ' {',
  31158. ' attr: [$mod.TCustomAttribute, "Create$1", [14]]',
  31159. ' }',
  31160. ' );',
  31161. ' $r.addMethod("Fly", 0, [], null, 0, {',
  31162. ' attr: [$mod.TCustomAttribute, "Create$1", [15]]',
  31163. ' });',
  31164. '});',
  31165. 'rtl.recNewT(this, "TRec", function () {',
  31166. ' this.Size = 0;',
  31167. ' this.$eq = function (b) {',
  31168. ' return this.Size === b.Size;',
  31169. ' };',
  31170. ' this.$assign = function (s) {',
  31171. ' this.Size = s.Size;',
  31172. ' return this;',
  31173. ' };',
  31174. ' var $r = $mod.$rtti.$Record("TRec", {});',
  31175. ' $r.addField("Size", rtl.word, {',
  31176. ' attr: [',
  31177. ' $mod.TCustomAttribute,',
  31178. ' "Create",',
  31179. ' $mod.TCustomAttribute,',
  31180. ' "Create$1",',
  31181. ' [14]',
  31182. ' ]',
  31183. ' });',
  31184. '});',
  31185. '']),
  31186. LinesToStr([ // $mod.$main
  31187. '']));
  31188. end;
  31189. procedure TTestModule.TestAttributes_Types;
  31190. begin
  31191. WithTypeInfo:=true;
  31192. StartProgram(false);
  31193. Add([
  31194. '{$modeswitch PrefixedAttributes}',
  31195. 'type',
  31196. ' TObject = class',
  31197. ' constructor Create(Id: word);',
  31198. ' end;',
  31199. ' TCustomAttribute = class',
  31200. ' end;',
  31201. ' [TCustom(1)]',
  31202. ' TMyClass = class',
  31203. ' end;',
  31204. ' [TCustom(11)]',
  31205. ' TMyDescendant = class(TMyClass)',
  31206. ' end;',
  31207. ' [TCustom(2)]',
  31208. ' TRec = record',
  31209. ' end;',
  31210. ' [TCustom(3)]',
  31211. ' TInt = type word;',
  31212. 'constructor TObject.Create(Id: word);',
  31213. 'begin',
  31214. 'end;',
  31215. 'var p: pointer;',
  31216. 'begin',
  31217. ' p:=typeinfo(TMyClass);',
  31218. ' p:=typeinfo(TRec);',
  31219. ' p:=typeinfo(TInt);',
  31220. '']);
  31221. ConvertProgram;
  31222. CheckSource('TestAttributes_Types',
  31223. LinesToStr([ // statements
  31224. 'rtl.createClass(this, "TObject", null, function () {',
  31225. ' this.$init = function () {',
  31226. ' };',
  31227. ' this.$final = function () {',
  31228. ' };',
  31229. ' this.Create = function (Id) {',
  31230. ' return this;',
  31231. ' };',
  31232. '});',
  31233. 'rtl.createClass(this, "TCustomAttribute", this.TObject, function () {',
  31234. '});',
  31235. 'rtl.createClass(this, "TMyClass", this.TObject, function () {',
  31236. ' var $r = this.$rtti;',
  31237. ' $r.attr = [$mod.TCustomAttribute, "Create", [1]];',
  31238. '});',
  31239. 'rtl.createClass(this, "TMyDescendant", this.TMyClass, function () {',
  31240. ' var $r = this.$rtti;',
  31241. ' $r.attr = [$mod.TCustomAttribute, "Create", [11]];',
  31242. '});',
  31243. 'rtl.recNewT(this, "TRec", function () {',
  31244. ' this.$eq = function (b) {',
  31245. ' return true;',
  31246. ' };',
  31247. ' this.$assign = function (s) {',
  31248. ' return this;',
  31249. ' };',
  31250. ' $mod.$rtti.$Record("TRec", {',
  31251. ' attr: [$mod.TCustomAttribute, "Create", [2]]',
  31252. ' });',
  31253. '});',
  31254. 'this.$rtti.$inherited("TInt", rtl.word, {',
  31255. ' attr: [this.TCustomAttribute, "Create", [3]]',
  31256. '});',
  31257. 'this.p = null;',
  31258. '']),
  31259. LinesToStr([ // $mod.$main
  31260. '$mod.p = $mod.$rtti["TMyClass"];',
  31261. '$mod.p = $mod.$rtti["TRec"];',
  31262. '$mod.p = $mod.$rtti["TInt"];',
  31263. '']));
  31264. end;
  31265. procedure TTestModule.TestAttributes_HelperConstructor_Fail;
  31266. begin
  31267. WithTypeInfo:=true;
  31268. StartProgram(false);
  31269. Add([
  31270. '{$modeswitch PrefixedAttributes}',
  31271. 'type',
  31272. ' TObject = class',
  31273. ' constructor Create;',
  31274. ' end;',
  31275. ' TCustomAttribute = class',
  31276. ' end;',
  31277. ' THelper = class helper for TCustomAttribute',
  31278. ' constructor Create(Id: word);',
  31279. ' end;',
  31280. ' [TCustom(3)]',
  31281. ' TMyInt = word;',
  31282. 'constructor TObject.Create; begin end;',
  31283. 'constructor THelper.Create(Id: word); begin end;',
  31284. 'begin',
  31285. ' if typeinfo(TMyInt)=nil then ;']);
  31286. ConvertProgram;
  31287. end;
  31288. procedure TTestModule.TestAssert;
  31289. begin
  31290. StartProgram(false);
  31291. Add([
  31292. 'procedure DoIt;',
  31293. 'var',
  31294. ' b: boolean;',
  31295. ' s: string;',
  31296. 'begin',
  31297. ' {$Assertions on}',
  31298. ' Assert(b);',
  31299. 'end;',
  31300. 'begin',
  31301. ' DoIt;',
  31302. '']);
  31303. ConvertProgram;
  31304. CheckSource('TestAssert',
  31305. LinesToStr([ // statements
  31306. 'this.DoIt = function () {',
  31307. ' var b = false;',
  31308. ' var s = "";',
  31309. ' if (!b) throw "assert failed";',
  31310. '};',
  31311. '']),
  31312. LinesToStr([ // $mod.$main
  31313. '$mod.DoIt();',
  31314. '']));
  31315. end;
  31316. procedure TTestModule.TestAssert_SysUtils;
  31317. begin
  31318. AddModuleWithIntfImplSrc('SysUtils.pas',
  31319. LinesToStr([
  31320. 'type',
  31321. ' TObject = class',
  31322. ' constructor Create;',
  31323. ' end;',
  31324. ' EAssertionFailed = class',
  31325. ' constructor Create(s: string);',
  31326. ' end;',
  31327. '']),
  31328. LinesToStr([
  31329. 'constructor TObject.Create;',
  31330. 'begin end;',
  31331. 'constructor EAssertionFailed.Create(s: string);',
  31332. 'begin end;',
  31333. '']) );
  31334. StartProgram(true);
  31335. Add([
  31336. 'uses sysutils;',
  31337. 'procedure DoIt;',
  31338. 'var',
  31339. ' b: boolean;',
  31340. ' s: string;',
  31341. 'begin',
  31342. ' {$Assertions on}',
  31343. ' Assert(b);',
  31344. ' Assert(b,''msg'');',
  31345. 'end;',
  31346. 'begin',
  31347. ' DoIt;',
  31348. '']);
  31349. ConvertProgram;
  31350. CheckSource('TestAssert_SysUtils',
  31351. LinesToStr([ // statements
  31352. 'this.DoIt = function () {',
  31353. ' var b = false;',
  31354. ' var s = "";',
  31355. ' if (!b) throw pas.SysUtils.EAssertionFailed.$create("Create");',
  31356. ' if (!b) throw pas.SysUtils.EAssertionFailed.$create("Create$1", ["msg"]);',
  31357. '};',
  31358. '']),
  31359. LinesToStr([ // $mod.$main
  31360. '$mod.DoIt();',
  31361. '']));
  31362. end;
  31363. procedure TTestModule.TestObjectChecks;
  31364. begin
  31365. Scanner.CurrentBoolSwitches:=Scanner.CurrentBoolSwitches+[bsObjectChecks];
  31366. StartProgram(false);
  31367. Add([
  31368. 'type',
  31369. ' TObject = class',
  31370. ' procedure DoIt;',
  31371. ' end;',
  31372. ' TClass = class of tobject;',
  31373. ' TBird = class',
  31374. ' end;',
  31375. ' TBirdClass = class of TBird;',
  31376. 'var',
  31377. ' o : TObject;',
  31378. ' c: TClass;',
  31379. ' b: TBird;',
  31380. ' bc: TBirdClass;',
  31381. 'procedure TObject.DoIt;',
  31382. 'begin',
  31383. ' b:=TBird(o);',
  31384. 'end;',
  31385. 'begin',
  31386. ' o.DoIt;',
  31387. ' b:=TBird(o);',
  31388. ' bc:=TBirdClass(c);',
  31389. '']);
  31390. ConvertProgram;
  31391. CheckSource('TestCheckMethodCall',
  31392. LinesToStr([ // statements
  31393. 'rtl.createClass(this, "TObject", null, function () {',
  31394. ' this.$init = function () {',
  31395. ' };',
  31396. ' this.$final = function () {',
  31397. ' };',
  31398. ' this.DoIt = function () {',
  31399. ' rtl.checkMethodCall(this,$mod.TObject);',
  31400. ' $mod.b = rtl.asExt($mod.o, $mod.TBird, 1);',
  31401. ' };',
  31402. '});',
  31403. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  31404. '});',
  31405. 'this.o = null;',
  31406. 'this.c = null;',
  31407. 'this.b = null;',
  31408. 'this.bc = null;',
  31409. '']),
  31410. LinesToStr([ // $mod.$main
  31411. '$mod.o.DoIt();',
  31412. '$mod.b = rtl.asExt($mod.o,$mod.TBird, 1);',
  31413. '$mod.bc = rtl.asExt($mod.c, $mod.TBird, 2);',
  31414. '']));
  31415. end;
  31416. procedure TTestModule.TestOverflowChecks_Int;
  31417. begin
  31418. Scanner.CurrentBoolSwitches:=Scanner.CurrentBoolSwitches+[bsOverflowChecks];
  31419. StartProgram(false);
  31420. Add([
  31421. 'procedure DoIt;',
  31422. 'var',
  31423. ' b: byte;',
  31424. ' n: nativeint;',
  31425. ' u: nativeuint;',
  31426. ' c: currency;',
  31427. 'begin',
  31428. ' n:=n+n;',
  31429. ' n:=n-n;',
  31430. ' n:=n+b;',
  31431. ' n:=b-n;',
  31432. ' n:=n*n;',
  31433. ' n:=n*u;',
  31434. ' c:=c+b;',
  31435. ' c:=b+c;',
  31436. ' c:=c*b;',
  31437. ' c:=b*c;',
  31438. 'end;',
  31439. 'begin',
  31440. '']);
  31441. ConvertProgram;
  31442. CheckSource('TestOverflowChecks_Int',
  31443. LinesToStr([ // statements
  31444. 'this.DoIt = function () {',
  31445. ' var b = 0;',
  31446. ' var n = 0;',
  31447. ' var u = 0;',
  31448. ' var c = 0;',
  31449. ' n = rtl.oc(n + n);',
  31450. ' n = rtl.oc(n - n);',
  31451. ' n = rtl.oc(n + b);',
  31452. ' n = rtl.oc(b - n);',
  31453. ' n = rtl.oc(n * n);',
  31454. ' n = rtl.oc(n * u);',
  31455. ' c = rtl.oc(c + (b * 10000));',
  31456. ' c = rtl.oc((b * 10000) + c);',
  31457. ' c = rtl.oc(c * b);',
  31458. ' c = rtl.oc(b * c);',
  31459. '};',
  31460. '']),
  31461. LinesToStr([ // $mod.$main
  31462. '']));
  31463. end;
  31464. procedure TTestModule.TestRangeChecks_AssignInt;
  31465. begin
  31466. Scanner.Options:=Scanner.Options+[po_CAssignments];
  31467. StartProgram(false);
  31468. Add([
  31469. '{$R+}',
  31470. 'var',
  31471. ' b: byte = 2;',
  31472. ' w: word = 3;',
  31473. 'procedure DoIt(p: byte);',
  31474. 'begin',
  31475. ' b:=w;',
  31476. ' b+=w;',
  31477. ' b:=1;',
  31478. 'end;',
  31479. '{$R-}',
  31480. 'procedure DoSome;',
  31481. 'begin',
  31482. ' DoIt(w);',
  31483. ' b:=w;',
  31484. ' b:=2;',
  31485. 'end;',
  31486. 'begin',
  31487. '{$R+}',
  31488. '']);
  31489. ConvertProgram;
  31490. CheckSource('TestRangeChecks_AssignInt',
  31491. LinesToStr([ // statements
  31492. 'this.b = 2;',
  31493. 'this.w = 3;',
  31494. 'this.DoIt = function (p) {',
  31495. ' rtl.rc(p, 0, 255);',
  31496. ' $mod.b = rtl.rc($mod.w,0,255);',
  31497. ' rtl.rc($mod.b += $mod.w, 0, 255);',
  31498. ' $mod.b = 1;',
  31499. '};',
  31500. 'this.DoSome = function () {',
  31501. ' $mod.DoIt($mod.w);',
  31502. ' $mod.b = $mod.w;',
  31503. ' $mod.b = 2;',
  31504. '};',
  31505. '']),
  31506. LinesToStr([ // $mod.$main
  31507. '']));
  31508. end;
  31509. procedure TTestModule.TestRangeChecks_AssignIntRange;
  31510. begin
  31511. Scanner.Options:=Scanner.Options+[po_CAssignments];
  31512. StartProgram(false);
  31513. Add([
  31514. '{$R+}',
  31515. 'type Ten = 1..10;',
  31516. 'var',
  31517. ' b: Ten = 2;',
  31518. ' w: Ten = 3;',
  31519. 'procedure DoIt(p: Ten);',
  31520. 'begin',
  31521. ' b:=w;',
  31522. ' b+=w;',
  31523. ' b:=1;',
  31524. 'end;',
  31525. '{$R-}',
  31526. 'procedure DoSome;',
  31527. 'begin',
  31528. ' DoIt(w);',
  31529. ' b:=w;',
  31530. ' b:=2;',
  31531. 'end;',
  31532. 'begin',
  31533. '{$R+}',
  31534. '']);
  31535. ConvertProgram;
  31536. CheckSource('TestRangeChecks_AssignIntRange',
  31537. LinesToStr([ // statements
  31538. 'this.b = 2;',
  31539. 'this.w = 3;',
  31540. 'this.DoIt = function (p) {',
  31541. ' rtl.rc(p, 1, 10);',
  31542. ' $mod.b = rtl.rc($mod.w, 1, 10);',
  31543. ' rtl.rc($mod.b += $mod.w, 1, 10);',
  31544. ' $mod.b = 1;',
  31545. '};',
  31546. 'this.DoSome = function () {',
  31547. ' $mod.DoIt($mod.w);',
  31548. ' $mod.b = $mod.w;',
  31549. ' $mod.b = 2;',
  31550. '};',
  31551. '']),
  31552. LinesToStr([ // $mod.$main
  31553. '']));
  31554. end;
  31555. procedure TTestModule.TestRangeChecks_AssignEnum;
  31556. begin
  31557. StartProgram(false);
  31558. Add([
  31559. '{$R+}',
  31560. 'type TEnum = (red,green);',
  31561. 'var',
  31562. ' e: TEnum = red;',
  31563. 'procedure DoIt(p: TEnum);',
  31564. 'begin',
  31565. ' e:=p;',
  31566. ' p:=TEnum(0);',
  31567. ' p:=succ(e);',
  31568. 'end;',
  31569. '{$R-}',
  31570. 'procedure DoSome;',
  31571. 'begin',
  31572. ' DoIt(e);',
  31573. ' e:=TEnum(1);',
  31574. ' e:=pred(e);',
  31575. 'end;',
  31576. 'begin',
  31577. '{$R+}',
  31578. '']);
  31579. ConvertProgram;
  31580. CheckSource('TestRangeChecks_AssignEnum',
  31581. LinesToStr([ // statements
  31582. 'this.TEnum = {',
  31583. ' "0": "red",',
  31584. ' red: 0,',
  31585. ' "1": "green",',
  31586. ' green: 1',
  31587. '};',
  31588. 'this.e = this.TEnum.red;',
  31589. 'this.DoIt = function (p) {',
  31590. ' rtl.rc(p, 0, 1);',
  31591. ' $mod.e = rtl.rc(p, 0, 1);',
  31592. ' p = 0;',
  31593. ' p = rtl.rc($mod.e + 1, 0, 1);',
  31594. '};',
  31595. 'this.DoSome = function () {',
  31596. ' $mod.DoIt($mod.e);',
  31597. ' $mod.e = 1;',
  31598. ' $mod.e = $mod.e - 1;',
  31599. '};',
  31600. '']),
  31601. LinesToStr([ // $mod.$main
  31602. '']));
  31603. end;
  31604. procedure TTestModule.TestRangeChecks_AssignEnumRange;
  31605. begin
  31606. StartProgram(false);
  31607. Add([
  31608. '{$R+}',
  31609. 'type',
  31610. ' TEnum = (red,green);',
  31611. ' TEnumRg = red..green;',
  31612. 'var',
  31613. ' e: TEnumRg = red;',
  31614. 'procedure DoIt(p: TEnumRg);',
  31615. 'begin',
  31616. ' e:=p;',
  31617. ' p:=TEnumRg(0);',
  31618. ' p:=succ(e);',
  31619. 'end;',
  31620. '{$R-}',
  31621. 'procedure DoSome;',
  31622. 'begin',
  31623. ' DoIt(e);',
  31624. ' e:=TEnum(1);',
  31625. ' e:=pred(e);',
  31626. 'end;',
  31627. 'begin',
  31628. '{$R+}',
  31629. '']);
  31630. ConvertProgram;
  31631. CheckSource('TestRangeChecks_AssignEnumRange',
  31632. LinesToStr([ // statements
  31633. 'this.TEnum = {',
  31634. ' "0": "red",',
  31635. ' red: 0,',
  31636. ' "1": "green",',
  31637. ' green: 1',
  31638. '};',
  31639. 'this.e = this.TEnum.red;',
  31640. 'this.DoIt = function (p) {',
  31641. ' rtl.rc(p, 0, 1);',
  31642. ' $mod.e = rtl.rc(p, 0, 1);',
  31643. ' p = 0;',
  31644. ' p = rtl.rc($mod.e + 1, 0, 1);',
  31645. '};',
  31646. 'this.DoSome = function () {',
  31647. ' $mod.DoIt($mod.e);',
  31648. ' $mod.e = 1;',
  31649. ' $mod.e = $mod.e - 1;',
  31650. '};',
  31651. '']),
  31652. LinesToStr([ // $mod.$main
  31653. '']));
  31654. end;
  31655. procedure TTestModule.TestRangeChecks_AssignChar;
  31656. begin
  31657. StartProgram(false);
  31658. Add([
  31659. '{$R+}',
  31660. 'type',
  31661. ' TLetter = char;',
  31662. 'var',
  31663. ' b: TLetter = ''2'';',
  31664. ' w: TLetter = ''3'';',
  31665. 'procedure DoIt(p: TLetter);',
  31666. 'begin',
  31667. ' b:=w;',
  31668. ' b:=''1'';',
  31669. 'end;',
  31670. '{$R-}',
  31671. 'procedure DoSome;',
  31672. 'begin',
  31673. ' DoIt(w);',
  31674. ' b:=w;',
  31675. ' b:=''2'';',
  31676. 'end;',
  31677. 'begin',
  31678. '{$R+}',
  31679. '']);
  31680. ConvertProgram;
  31681. CheckSource('TestRangeChecks_AssignChar',
  31682. LinesToStr([ // statements
  31683. 'this.b = "2";',
  31684. 'this.w = "3";',
  31685. 'this.DoIt = function (p) {',
  31686. ' rtl.rcc(p, 0, 65535);',
  31687. ' $mod.b = rtl.rcc($mod.w, 0, 65535);',
  31688. ' $mod.b = "1";',
  31689. '};',
  31690. 'this.DoSome = function () {',
  31691. ' $mod.DoIt($mod.w);',
  31692. ' $mod.b = $mod.w;',
  31693. ' $mod.b = "2";',
  31694. '};',
  31695. '']),
  31696. LinesToStr([ // $mod.$main
  31697. '']));
  31698. end;
  31699. procedure TTestModule.TestRangeChecks_AssignCharRange;
  31700. begin
  31701. StartProgram(false);
  31702. Add([
  31703. '{$R+}',
  31704. 'type TDigit = ''0''..''9'';',
  31705. 'var',
  31706. ' b: TDigit = ''2'';',
  31707. ' w: TDigit = ''3'';',
  31708. 'procedure DoIt(p: TDigit);',
  31709. 'begin',
  31710. ' b:=w;',
  31711. ' b:=''1'';',
  31712. 'end;',
  31713. '{$R-}',
  31714. 'procedure DoSome;',
  31715. 'begin',
  31716. ' DoIt(w);',
  31717. ' b:=w;',
  31718. ' b:=''2'';',
  31719. 'end;',
  31720. 'begin',
  31721. '{$R+}',
  31722. '']);
  31723. ConvertProgram;
  31724. CheckSource('TestRangeChecks_AssignCharRange',
  31725. LinesToStr([ // statements
  31726. 'this.b = "2";',
  31727. 'this.w = "3";',
  31728. 'this.DoIt = function (p) {',
  31729. ' rtl.rcc(p, 48, 57);',
  31730. ' $mod.b = rtl.rcc($mod.w, 48, 57);',
  31731. ' $mod.b = "1";',
  31732. '};',
  31733. 'this.DoSome = function () {',
  31734. ' $mod.DoIt($mod.w);',
  31735. ' $mod.b = $mod.w;',
  31736. ' $mod.b = "2";',
  31737. '};',
  31738. '']),
  31739. LinesToStr([ // $mod.$main
  31740. '']));
  31741. end;
  31742. procedure TTestModule.TestRangeChecks_ArrayIndex;
  31743. begin
  31744. StartProgram(false);
  31745. Add([
  31746. '{$R+}',
  31747. 'type',
  31748. ' Ten = 1..10;',
  31749. ' TArr = array of Ten;',
  31750. ' TArrArr = array of TArr;',
  31751. ' TArrByte = array[byte] of Ten;',
  31752. ' TArrChar = array[''0''..''9''] of Ten;',
  31753. ' TArrByteChar = array[byte,''0''..''9''] of Ten;',
  31754. ' TObject = class',
  31755. ' A: TArr;',
  31756. ' end;',
  31757. 'procedure DoIt;',
  31758. 'var',
  31759. ' Arr: TArr;',
  31760. ' ArrArr: TArrArr;',
  31761. ' ArrByte: TArrByte;',
  31762. ' ArrChar: TArrChar;',
  31763. ' ArrByteChar: TArrByteChar;',
  31764. ' i: Ten;',
  31765. ' c: char;',
  31766. ' o: tobject;',
  31767. 'begin',
  31768. ' i:=Arr[1];',
  31769. ' i:=ArrByteChar[1,''2''];',
  31770. ' Arr[1]:=Arr[1];',
  31771. ' Arr[i]:=Arr[i];',
  31772. ' ArrByte[3]:=ArrByte[3];',
  31773. ' ArrByte[i]:=ArrByte[i];',
  31774. ' ArrChar[''5'']:=ArrChar[''5''];',
  31775. ' ArrChar[c]:=ArrChar[c];',
  31776. ' ArrByteChar[7,''7'']:=ArrByteChar[7,''7''];',
  31777. ' ArrByteChar[i,c]:=ArrByteChar[i,c];',
  31778. ' o.a[i]:=o.a[i];',
  31779. 'end;',
  31780. 'begin',
  31781. '']);
  31782. ConvertProgram;
  31783. CheckSource('TestRangeChecks_ArrayIndex',
  31784. LinesToStr([ // statements
  31785. 'rtl.createClass(this, "TObject", null, function () {',
  31786. ' this.$init = function () {',
  31787. ' this.A = [];',
  31788. ' };',
  31789. ' this.$final = function () {',
  31790. ' this.A = undefined;',
  31791. ' };',
  31792. '});',
  31793. 'this.DoIt = function () {',
  31794. ' var Arr = [];',
  31795. ' var ArrArr = [];',
  31796. ' var ArrByte = rtl.arraySetLength(null, 0, 256);',
  31797. ' var ArrChar = rtl.arraySetLength(null, 0, 10);',
  31798. ' var ArrByteChar = rtl.arraySetLength(null, 0, 256, 10);',
  31799. ' var i = 0;',
  31800. ' var c = "";',
  31801. ' var o = null;',
  31802. ' i = rtl.rc(Arr[1], 1, 10);',
  31803. ' i = rtl.rc(ArrByteChar[1][2], 1, 10);',
  31804. ' Arr[1] = rtl.rc(Arr[1], 1, 10);',
  31805. ' rtl.rcArrW(Arr, i, rtl.rcArrR(Arr, i));',
  31806. ' ArrByte[3] = rtl.rc(ArrByte[3], 1, 10);',
  31807. ' rtl.rcArrW(ArrByte, i, rtl.rcArrR(ArrByte, i));',
  31808. ' ArrChar[5] = rtl.rc(ArrChar[5], 1, 10);',
  31809. ' rtl.rcArrW(ArrChar, c.charCodeAt() - 48, rtl.rcArrR(ArrChar, c.charCodeAt() - 48));',
  31810. ' ArrByteChar[7][7] = rtl.rc(ArrByteChar[7][7], 1, 10);',
  31811. ' rtl.rcArrW(ArrByteChar, i, c.charCodeAt() - 48, rtl.rcArrR(ArrByteChar, i, c.charCodeAt() - 48));',
  31812. ' rtl.rcArrW(o.A, i, rtl.rcArrR(o.A, i));',
  31813. '};',
  31814. '']),
  31815. LinesToStr([ // $mod.$main
  31816. '']));
  31817. end;
  31818. procedure TTestModule.TestRangeChecks_ArrayOfRecIndex;
  31819. begin
  31820. StartProgram(false);
  31821. Add([
  31822. '{$R+}',
  31823. 'type',
  31824. ' Ten = 1..10;',
  31825. ' TRec = record x: Ten end;',
  31826. ' TArr = array of TRec;',
  31827. ' TArrArr = array of TArr;',
  31828. ' TObject = class',
  31829. ' A: TArr;',
  31830. ' end;',
  31831. 'procedure DoIt;',
  31832. 'var',
  31833. ' Arr: TArr;',
  31834. ' ArrArr: TArrArr;',
  31835. ' i: Ten;',
  31836. ' o: tobject;',
  31837. 'begin',
  31838. ' Arr[1]:=Arr[1];',
  31839. ' Arr[i]:=Arr[i+1];',
  31840. ' o.a[i]:=o.a[i+2];',
  31841. 'end;',
  31842. 'begin',
  31843. '']);
  31844. ConvertProgram;
  31845. CheckSource('TestRangeChecks_ArrayOfRecIndex',
  31846. LinesToStr([ // statements
  31847. 'rtl.recNewT(this, "TRec", function () {',
  31848. ' this.x = 0;',
  31849. ' this.$eq = function (b) {',
  31850. ' return this.x === b.x;',
  31851. ' };',
  31852. ' this.$assign = function (s) {',
  31853. ' this.x = s.x;',
  31854. ' return this;',
  31855. ' };',
  31856. '});',
  31857. 'rtl.createClass(this, "TObject", null, function () {',
  31858. ' this.$init = function () {',
  31859. ' this.A = [];',
  31860. ' };',
  31861. ' this.$final = function () {',
  31862. ' this.A = undefined;',
  31863. ' };',
  31864. '});',
  31865. 'this.DoIt = function () {',
  31866. ' var Arr = [];',
  31867. ' var ArrArr = [];',
  31868. ' var i = 0;',
  31869. ' var o = null;',
  31870. ' Arr[1].$assign(Arr[1]);',
  31871. ' rtl.rcArrR(Arr, i).$assign(rtl.rcArrR(Arr, i + 1));',
  31872. ' rtl.rcArrR(o.A, i).$assign(rtl.rcArrR(o.A, i + 2));',
  31873. '};',
  31874. '']),
  31875. LinesToStr([ // $mod.$main
  31876. '']));
  31877. end;
  31878. procedure TTestModule.TestRangeChecks_StringIndex;
  31879. begin
  31880. StartProgram(false);
  31881. Add([
  31882. 'type',
  31883. ' TObject = class',
  31884. ' S: string;',
  31885. ' end;',
  31886. '{$R+}',
  31887. 'procedure DoIt(var h: string);',
  31888. 'var',
  31889. ' s: string;',
  31890. ' i: longint;',
  31891. ' c: char;',
  31892. ' o: tobject;',
  31893. 'begin',
  31894. ' c:=s[1];',
  31895. ' s[i]:=s[i];',
  31896. ' h[i]:=h[i];',
  31897. ' c:=o.s[i];',
  31898. ' o.s[i]:=c;',
  31899. 'end;',
  31900. 'begin',
  31901. '']);
  31902. ConvertProgram;
  31903. CheckSource('TestRangeChecks_StringIndex',
  31904. LinesToStr([ // statements
  31905. 'rtl.createClass(this, "TObject", null, function () {',
  31906. ' this.$init = function () {',
  31907. ' this.S = "";',
  31908. ' };',
  31909. ' this.$final = function () {',
  31910. ' };',
  31911. '});',
  31912. 'this.DoIt = function (h) {',
  31913. ' var s = "";',
  31914. ' var i = 0;',
  31915. ' var c = "";',
  31916. ' var o = null;',
  31917. ' c = rtl.rcc(rtl.rcCharAt(s, 0), 0, 65535);',
  31918. ' s = rtl.rcSetCharAt(s, i - 1, rtl.rcCharAt(s, i - 1));',
  31919. ' h.set(rtl.rcSetCharAt(h.get(), i - 1, rtl.rcCharAt(h.get(), i - 1)));',
  31920. ' c = rtl.rcc(rtl.rcCharAt(o.S, i - 1), 0, 65535);',
  31921. ' o.S = rtl.rcSetCharAt(o.S, i - 1, c);',
  31922. '};',
  31923. '']),
  31924. LinesToStr([ // $mod.$main
  31925. '']));
  31926. end;
  31927. procedure TTestModule.TestRangeChecks_TypecastInt;
  31928. begin
  31929. StartProgram(false);
  31930. Add([
  31931. '{$R+}',
  31932. 'var',
  31933. ' i: nativeint;',
  31934. ' b: byte;',
  31935. ' sh: shortint;',
  31936. ' w: word;',
  31937. ' sm: smallint;',
  31938. ' lw: longword;',
  31939. ' li: longint;',
  31940. 'begin',
  31941. ' b:=12+byte(i);',
  31942. ' sh:=12+shortint(i);',
  31943. ' w:=12+word(i);',
  31944. ' sm:=12+smallint(i);',
  31945. ' lw:=12+longword(i);',
  31946. ' li:=12+longint(i);',
  31947. '']);
  31948. ConvertProgram;
  31949. CheckSource('TestRangeChecks_TypecastInt',
  31950. LinesToStr([
  31951. 'this.i = 0;',
  31952. 'this.b = 0;',
  31953. 'this.sh = 0;',
  31954. 'this.w = 0;',
  31955. 'this.sm = 0;',
  31956. 'this.lw = 0;',
  31957. 'this.li = 0;',
  31958. '']),
  31959. LinesToStr([
  31960. '$mod.b = rtl.rc(12 + rtl.rc($mod.i, 0, 255), 0, 255);',
  31961. '$mod.sh = rtl.rc(12 + rtl.rc($mod.i, -128, 127), -128, 127);',
  31962. '$mod.w = rtl.rc(12 + rtl.rc($mod.i, 0, 65535), 0, 65535);',
  31963. '$mod.sm = rtl.rc(12 + rtl.rc($mod.i, -32768, 32767), -32768, 32767);',
  31964. '$mod.lw = rtl.rc(12 + rtl.rc($mod.i, 0, 4294967295), 0, 4294967295);',
  31965. '$mod.li = rtl.rc(12 + rtl.rc($mod.i, -2147483648, 2147483647), -2147483648, 2147483647);',
  31966. '']));
  31967. end;
  31968. procedure TTestModule.TestRangeChecks_TypeHelperInt;
  31969. begin
  31970. Scanner.Options:=Scanner.Options+[po_CAssignments];
  31971. StartProgram(false);
  31972. Add([
  31973. '{$modeswitch typehelpers}',
  31974. '{$R+}',
  31975. 'type',
  31976. ' TObject = class',
  31977. ' FSize: byte;',
  31978. ' property Size: byte read FSize;',
  31979. ' end;',
  31980. ' THelper = type helper for byte',
  31981. ' procedure SetIt(w: word);',
  31982. ' end;',
  31983. 'procedure THelper.SetIt(w: word);',
  31984. 'begin',
  31985. ' Self:=w;',
  31986. 'end;',
  31987. 'function GetIt: byte;',
  31988. 'begin',
  31989. ' Result.SetIt(2);',
  31990. 'end;',
  31991. 'var',
  31992. ' b: byte = 3;',
  31993. ' o: TObject;',
  31994. 'begin',
  31995. ' b.SetIt(14);',
  31996. ' with b do SetIt(15);',
  31997. ' o.Size.SetIt(16);',
  31998. '']);
  31999. ConvertProgram;
  32000. CheckSource('TestRangeChecks_AssignInt',
  32001. LinesToStr([ // statements
  32002. 'rtl.createClass(this, "TObject", null, function () {',
  32003. ' this.$init = function () {',
  32004. ' this.FSize = 0;',
  32005. ' };',
  32006. ' this.$final = function () {',
  32007. ' };',
  32008. '});',
  32009. 'rtl.createHelper(this, "THelper", null, function () {',
  32010. ' this.SetIt = function (w) {',
  32011. ' rtl.rc(w, 0, 65535);',
  32012. ' this.set(w);',
  32013. ' };',
  32014. '});',
  32015. 'this.GetIt = function () {',
  32016. ' var Result = 0;',
  32017. ' $mod.THelper.SetIt.call({',
  32018. ' get: function () {',
  32019. ' return Result;',
  32020. ' },',
  32021. ' set: function (v) {',
  32022. ' rtl.rc(v, 0, 255);',
  32023. ' Result = v;',
  32024. ' }',
  32025. ' }, 2);',
  32026. ' return Result;',
  32027. '};',
  32028. 'this.b = 3;',
  32029. 'this.o = null;',
  32030. '']),
  32031. LinesToStr([ // $mod.$main
  32032. '$mod.THelper.SetIt.call({',
  32033. ' p: $mod,',
  32034. ' get: function () {',
  32035. ' return this.p.b;',
  32036. ' },',
  32037. ' set: function (v) {',
  32038. ' rtl.rc(v, 0, 255);',
  32039. ' this.p.b = v;',
  32040. ' }',
  32041. '}, 14);',
  32042. 'var $with = $mod.b;',
  32043. '$mod.THelper.SetIt.call({',
  32044. ' get: function () {',
  32045. ' return $with;',
  32046. ' },',
  32047. ' set: function (v) {',
  32048. ' rtl.rc(v, 0, 255);',
  32049. ' $with = v;',
  32050. ' }',
  32051. '}, 15);',
  32052. '$mod.THelper.SetIt.call({',
  32053. ' p: $mod.o,',
  32054. ' get: function () {',
  32055. ' return this.p.FSize;',
  32056. ' },',
  32057. ' set: function (v) {',
  32058. ' rtl.rc(v, 0, 255);',
  32059. ' this.p.FSize = v;',
  32060. ' }',
  32061. '}, 16);',
  32062. '']));
  32063. end;
  32064. procedure TTestModule.TestAsync_Proc;
  32065. begin
  32066. StartProgram(false);
  32067. Add([
  32068. 'procedure Fly(w: word = 1); async; forward;',
  32069. 'procedure Run(w: word = 2); async;',
  32070. 'begin',
  32071. ' Fly(w);',
  32072. ' Fly;',
  32073. ' await(Fly(w));',
  32074. ' await(Fly);',
  32075. 'end;',
  32076. 'procedure Fly(w: word); ',
  32077. 'begin',
  32078. 'end;',
  32079. 'begin',
  32080. ' Run;',
  32081. ' Run(3);',
  32082. '']);
  32083. CheckResolverUnexpectedHints();
  32084. ConvertProgram;
  32085. CheckSource('TestAsync_Proc',
  32086. LinesToStr([ // statements
  32087. 'this.Run = async function (w) {',
  32088. ' $mod.Fly(w);',
  32089. ' $mod.Fly(1);',
  32090. ' await $mod.Fly(w);',
  32091. ' await $mod.Fly(1);',
  32092. '};',
  32093. 'this.Fly = async function (w) {',
  32094. '};',
  32095. '']),
  32096. LinesToStr([
  32097. '$mod.Run(2);',
  32098. '$mod.Run(3);',
  32099. '']));
  32100. end;
  32101. procedure TTestModule.TestAsync_CallResultIsPromise;
  32102. begin
  32103. StartProgram(false);
  32104. Add([
  32105. '{$modeswitch externalclass}',
  32106. 'type',
  32107. ' TObject = class',
  32108. ' end;',
  32109. ' TJSPromise = class external name ''Promise''',
  32110. ' end;',
  32111. ' TBird = class',
  32112. ' function Fly: word; async; ',
  32113. ' end;',
  32114. 'function TBird.Fly: word; async; ',
  32115. 'begin',
  32116. ' Result:=3;',
  32117. ' Fly:=4+Result;',
  32118. ' if Result=5 then ;',
  32119. ' exit(6);',
  32120. 'end;',
  32121. 'function Run: word; async;',
  32122. 'begin',
  32123. ' Result:=11+Result;',
  32124. ' inc(Result);',
  32125. 'end;',
  32126. 'var',
  32127. ' p: TJSPromise;',
  32128. ' o: TBird;',
  32129. 'begin',
  32130. ' p:=Run;',
  32131. ' p:=Run();',
  32132. ' if Run=p then ;',
  32133. ' if p=Run then ;',
  32134. ' if Run()=p then ;',
  32135. ' if p=Run() then ;',
  32136. ' p:=o.Fly;',
  32137. ' p:=o.Fly();',
  32138. ' if o.Fly=p then ;',
  32139. ' if o.Fly()=p then ;',
  32140. ' with o do begin',
  32141. ' p:=Fly;',
  32142. ' p:=Fly();',
  32143. ' if Fly=p then ;',
  32144. ' if Fly()=p then ;',
  32145. ' end;',
  32146. '']);
  32147. CheckResolverUnexpectedHints();
  32148. ConvertProgram;
  32149. CheckSource('TestAsync_CallResultIsPromise',
  32150. LinesToStr([ // statements
  32151. 'rtl.createClass(this, "TObject", null, function () {',
  32152. ' this.$init = function () {',
  32153. ' };',
  32154. ' this.$final = function () {',
  32155. ' };',
  32156. '});',
  32157. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  32158. ' this.Fly = async function () {',
  32159. ' var Result = 0;',
  32160. ' Result = 3;',
  32161. ' Result = 4 + Result;',
  32162. ' if (Result === 5) ;',
  32163. ' return 6;',
  32164. ' return Result;',
  32165. ' };',
  32166. '});',
  32167. 'this.Run = async function () {',
  32168. ' var Result = 0;',
  32169. ' Result = 11 + Result;',
  32170. ' Result += 1;',
  32171. ' return Result;',
  32172. '};',
  32173. 'this.p = null;',
  32174. 'this.o = null;',
  32175. '']),
  32176. LinesToStr([
  32177. '$mod.p = $mod.Run();',
  32178. '$mod.p = $mod.Run();',
  32179. 'if ($mod.Run() === $mod.p) ;',
  32180. 'if ($mod.p === $mod.Run()) ;',
  32181. 'if ($mod.Run() === $mod.p) ;',
  32182. 'if ($mod.p === $mod.Run()) ;',
  32183. '$mod.p = $mod.o.Fly();',
  32184. '$mod.p = $mod.o.Fly();',
  32185. 'if ($mod.o.Fly() === $mod.p) ;',
  32186. 'if ($mod.o.Fly() === $mod.p) ;',
  32187. 'var $with = $mod.o;',
  32188. '$mod.p = $with.Fly();',
  32189. '$mod.p = $with.Fly();',
  32190. 'if ($with.Fly() === $mod.p) ;',
  32191. 'if ($with.Fly() === $mod.p) ;',
  32192. '']));
  32193. end;
  32194. procedure TTestModule.TestAsync_ConstructorFail;
  32195. begin
  32196. StartProgram(false);
  32197. Add([
  32198. 'type',
  32199. ' TObject = class',
  32200. ' end;',
  32201. ' TBird = class',
  32202. ' constructor Create; async;',
  32203. ' end;',
  32204. 'constructor TBird.Create; async;',
  32205. 'begin',
  32206. 'end;',
  32207. 'begin',
  32208. '']);
  32209. SetExpectedPasResolverError('Invalid constructor modifier async',nInvalidXModifierY);
  32210. ConvertProgram;
  32211. end;
  32212. procedure TTestModule.TestAsync_PropertyGetterFail;
  32213. begin
  32214. StartProgram(false);
  32215. Add([
  32216. 'type',
  32217. ' TObject = class',
  32218. ' end;',
  32219. ' TBird = class',
  32220. ' function GetSize: word; async;',
  32221. ' property Size: word read GetSize;',
  32222. ' end;',
  32223. 'function TBird.GetSize: word; async;',
  32224. 'begin',
  32225. 'end;',
  32226. 'begin',
  32227. '']);
  32228. SetExpectedPasResolverError('Invalid property getter modifier async',nInvalidXModifierY);
  32229. ConvertProgram;
  32230. end;
  32231. procedure TTestModule.TestAwait_NonPromiseWithTypeFail;
  32232. begin
  32233. StartProgram(false);
  32234. Add([
  32235. 'procedure Run; async;',
  32236. 'begin',
  32237. ' await(word,1);',
  32238. 'end;',
  32239. 'begin',
  32240. '']);
  32241. SetExpectedPasResolverError('Incompatible type arg no. 2: Got "Longint", expected "TJSPromise"',nIncompatibleTypeArgNo);
  32242. ConvertProgram;
  32243. end;
  32244. procedure TTestModule.TestAwait_AsyncCallTypeMismatch;
  32245. begin
  32246. StartProgram(false);
  32247. Add([
  32248. 'type',
  32249. ' TObject = class',
  32250. ' end;',
  32251. ' TBird = class',
  32252. ' end;',
  32253. 'function Fly: TObject; async;',
  32254. 'begin',
  32255. 'end;',
  32256. 'procedure Run; async;',
  32257. 'begin',
  32258. ' await(TBird,Fly);',
  32259. 'end;',
  32260. 'begin',
  32261. '']);
  32262. SetExpectedPasResolverError('Incompatible type arg no. 2: Got "TObject", expected "TBird"',nIncompatibleTypeArgNo);
  32263. ConvertProgram;
  32264. end;
  32265. procedure TTestModule.TestAWait_OutsideAsyncFail;
  32266. begin
  32267. StartProgram(false);
  32268. Add([
  32269. 'procedure Crawl(w: double); ',
  32270. 'begin',
  32271. 'end;',
  32272. 'procedure Run(w: double);',
  32273. 'begin',
  32274. ' await(Crawl(w));',
  32275. 'end;',
  32276. 'begin',
  32277. ' Run(1);']);
  32278. SetExpectedPasResolverError(sAWaitOnlyInAsyncProcedure,nAWaitOnlyInAsyncProcedure);
  32279. ConvertProgram;
  32280. end;
  32281. procedure TTestModule.TestAWait_IntegerFail;
  32282. begin
  32283. StartProgram(false);
  32284. Add([
  32285. 'function Run: word;',
  32286. 'begin',
  32287. 'end;',
  32288. 'procedure Fly(w: word); async;',
  32289. 'begin',
  32290. ' await(Run());',
  32291. 'end;',
  32292. 'begin',
  32293. ' Fly(1);']);
  32294. SetExpectedPasResolverError('async function expected, but Result:Word found',nXExpectedButYFound);
  32295. ConvertProgram;
  32296. end;
  32297. procedure TTestModule.TestAWait_ExternalClassPromise;
  32298. begin
  32299. StartProgram(false);
  32300. Add([
  32301. '{$modeswitch externalclass}',
  32302. 'type',
  32303. ' TJSPromise = class external name ''Promise''',
  32304. ' end;',
  32305. ' TJSThenable = class external name ''Thenable''',
  32306. ' end;',
  32307. 'function Fly(w: word): TJSPromise;',
  32308. 'begin',
  32309. 'end;',
  32310. 'function Jump(w: word): word; async;',
  32311. 'begin',
  32312. 'end;',
  32313. 'function Eat(w: word): TJSPromise; async;',
  32314. 'begin',
  32315. 'end;',
  32316. 'function Run(d: double): word; async;',
  32317. 'var',
  32318. ' p: TJSPromise;',
  32319. 'begin',
  32320. ' Result:=await(word,p);', // promise needs type
  32321. ' Result:=await(word,Fly(3));', // promise needs type
  32322. ' Result:=await(Jump(4));', // async non promise must omit the type
  32323. ' Result:=await(word,Jump(5));', // async call can provide fitting type
  32324. ' Result:=await(word,Eat(6));', // promise needs type
  32325. 'end;',
  32326. 'begin',
  32327. '']);
  32328. ConvertProgram;
  32329. CheckSource('TestAWait_ExternalClassPromise',
  32330. LinesToStr([ // statements
  32331. 'this.Fly = function (w) {',
  32332. ' var Result = null;',
  32333. ' return Result;',
  32334. '};',
  32335. 'this.Jump = async function (w) {',
  32336. ' var Result = 0;',
  32337. ' return Result;',
  32338. '};',
  32339. 'this.Eat = async function (w) {',
  32340. ' var Result = null;',
  32341. ' return Result;',
  32342. '};',
  32343. 'this.Run = async function (d) {',
  32344. ' var Result = 0;',
  32345. ' var p = null;',
  32346. ' Result = await p;',
  32347. ' Result = await $mod.Fly(3);',
  32348. ' Result = await $mod.Jump(4);',
  32349. ' Result = await $mod.Jump(5);',
  32350. ' Result = await $mod.Eat(6);',
  32351. ' return Result;',
  32352. '};',
  32353. '']),
  32354. LinesToStr([
  32355. ]));
  32356. CheckResolverUnexpectedHints();
  32357. end;
  32358. procedure TTestModule.TestAWait_JSValue;
  32359. begin
  32360. StartProgram(false);
  32361. Add([
  32362. '{$modeswitch externalclass}',
  32363. 'type',
  32364. ' TJSPromise = class external name ''Promise''',
  32365. ' end;',
  32366. 'function Fly(w: word): jsvalue; async;',
  32367. 'begin',
  32368. 'end;',
  32369. 'function Run(d: jsvalue; var e): word; async;',
  32370. 'begin',
  32371. ' Result:=await(word,d);', // promise needs type
  32372. ' d:=await(Fly(4));', // async non promise must omit the type
  32373. ' Result:=await(word,e);', // promise needs type
  32374. 'end;',
  32375. 'begin',
  32376. '']);
  32377. ConvertProgram;
  32378. CheckSource('TestAWait_JSValue',
  32379. LinesToStr([ // statements
  32380. 'this.Fly = async function (w) {',
  32381. ' var Result = undefined;',
  32382. ' return Result;',
  32383. '};',
  32384. 'this.Run = async function (d, e) {',
  32385. ' var Result = 0;',
  32386. ' Result = await d;',
  32387. ' d = await $mod.Fly(4);',
  32388. ' Result = await e.get();',
  32389. ' return Result;',
  32390. '};',
  32391. '']),
  32392. LinesToStr([
  32393. ]));
  32394. CheckResolverUnexpectedHints();
  32395. end;
  32396. procedure TTestModule.TestAWait_Result;
  32397. begin
  32398. StartProgram(false);
  32399. Add([
  32400. '{$modeswitch externalclass}',
  32401. 'type',
  32402. ' TJSPromise = class external name ''Promise''',
  32403. ' end;',
  32404. 'function Crawl(d: double = 1.3): TJSPromise; ',
  32405. 'begin',
  32406. 'end;',
  32407. 'function Run(d: double = 1.6): word; async;',
  32408. 'begin',
  32409. ' Result:=await(word,Crawl);',
  32410. ' Result:=await(word,Crawl(4.5));',
  32411. ' Result:=await(Run);',
  32412. ' Result:=await(Run(6.7));',
  32413. 'end;',
  32414. 'begin',
  32415. ' Run(1);']);
  32416. ConvertProgram;
  32417. CheckSource('TestAWait_Result',
  32418. LinesToStr([ // statements
  32419. 'this.Crawl = function (d) {',
  32420. ' var Result = null;',
  32421. ' return Result;',
  32422. '};',
  32423. 'this.Run = async function (d) {',
  32424. ' var Result = 0;',
  32425. ' Result = await $mod.Crawl(1.3);',
  32426. ' Result = await $mod.Crawl(4.5);',
  32427. ' Result = await $mod.Run(1.6);',
  32428. ' Result = await $mod.Run(6.7);',
  32429. ' return Result;',
  32430. '};',
  32431. '']),
  32432. LinesToStr([
  32433. '$mod.Run(1);'
  32434. ]));
  32435. CheckResolverUnexpectedHints();
  32436. end;
  32437. procedure TTestModule.TestAWait_ResultPromiseMissingTypeFail;
  32438. begin
  32439. StartProgram(false);
  32440. Add([
  32441. '{$mode objfpc}',
  32442. '{$modeswitch externalclass}',
  32443. 'type',
  32444. ' TJSPromise = class external name ''Promise''',
  32445. ' end;',
  32446. 'function Run: TJSPromise; async;',
  32447. 'begin',
  32448. 'end;',
  32449. 'procedure Fly(w: word); async;',
  32450. 'begin',
  32451. ' await(Run());',
  32452. 'end;',
  32453. 'begin',
  32454. ' Fly(1);']);
  32455. SetExpectedPasResolverError('Wrong number of parameters specified for call to "function await(aType,TJSPromise):aType"',
  32456. nWrongNumberOfParametersForCallTo);
  32457. ConvertProgram;
  32458. end;
  32459. procedure TTestModule.TestAsync_AnonymousProc;
  32460. begin
  32461. StartProgram(false);
  32462. Add([
  32463. '{$mode objfpc}',
  32464. '{$modeswitch externalclass}',
  32465. 'type',
  32466. ' TJSPromise = class external name ''Promise''',
  32467. ' end;',
  32468. 'type',
  32469. ' TFunc = reference to function(x: double): word; async;',
  32470. 'function Crawl(d: double = 1.3): word; async;',
  32471. 'begin',
  32472. 'end;',
  32473. 'var Func: TFunc;',
  32474. 'begin',
  32475. ' Func:=function(c:double):word async begin',
  32476. ' Result:=await(Crawl(c));',
  32477. ' end;',
  32478. ' Func:=function(c:double):word async assembler asm',
  32479. ' end;',
  32480. ' ']);
  32481. ConvertProgram;
  32482. CheckSource('TestAsync_AnonymousProc',
  32483. LinesToStr([ // statements
  32484. 'this.Crawl = async function (d) {',
  32485. ' var Result = 0;',
  32486. ' return Result;',
  32487. '};',
  32488. 'this.Func = null;',
  32489. '']),
  32490. LinesToStr([
  32491. '$mod.Func = async function (c) {',
  32492. ' var Result = 0;',
  32493. ' Result = await $mod.Crawl(c);',
  32494. ' return Result;',
  32495. '};',
  32496. '$mod.Func = async function (c) {',
  32497. '};',
  32498. '']));
  32499. CheckResolverUnexpectedHints();
  32500. end;
  32501. procedure TTestModule.TestAsync_ProcType;
  32502. begin
  32503. StartProgram(false);
  32504. Add([
  32505. '{$mode objfpc}',
  32506. 'type',
  32507. ' TRefFunc = reference to function(x: double = 1.3): word; async;',
  32508. ' TFunc = function(x: double = 1.1): word; async;',
  32509. ' TProc = procedure(x: longint = 7); async;',
  32510. 'function Crawl(d: double): word; async;',
  32511. 'begin',
  32512. 'end;',
  32513. 'procedure Run(e:longint); async;',
  32514. 'begin',
  32515. 'end;',
  32516. 'procedure Fly(p: TProc); async;',
  32517. 'begin',
  32518. ' await(p);',
  32519. ' await(p());',
  32520. 'end;',
  32521. 'var',
  32522. ' RefFunc: TRefFunc;',
  32523. ' Func: TFunc;',
  32524. ' Proc, ProcB: TProc;',
  32525. 'begin',
  32526. ' Func:=@Crawl;',
  32527. ' RefFunc:=@Crawl;',
  32528. ' RefFunc:=function(c:double):word async begin',
  32529. ' Result:=await(RefFunc);',
  32530. ' Result:=await(RefFunc());',
  32531. ' Result:=await(Func);',
  32532. ' Result:=await(Func());',
  32533. ' await(Proc);',
  32534. ' await(Proc());',
  32535. ' await(Proc(13));',
  32536. ' end;',
  32537. ' Proc:=@Run;',
  32538. ' if Proc=ProcB then ;',
  32539. ' ']);
  32540. ConvertProgram;
  32541. CheckResolverUnexpectedHints();
  32542. CheckSource('TestAsync_ProcType',
  32543. LinesToStr([ // statements
  32544. 'this.Crawl = async function (d) {',
  32545. ' var Result = 0;',
  32546. ' return Result;',
  32547. '};',
  32548. 'this.Run = async function (e) {',
  32549. '};',
  32550. 'this.Fly = async function (p) {',
  32551. ' await p(7);',
  32552. ' await p(7);',
  32553. '};',
  32554. 'this.RefFunc = null;',
  32555. 'this.Func = null;',
  32556. 'this.Proc = null;',
  32557. 'this.ProcB = null;',
  32558. '']),
  32559. LinesToStr([
  32560. '$mod.Func = $mod.Crawl;',
  32561. '$mod.RefFunc = $mod.Crawl;',
  32562. '$mod.RefFunc = async function (c) {',
  32563. ' var Result = 0;',
  32564. ' Result = await $mod.RefFunc(1.3);',
  32565. ' Result = await $mod.RefFunc(1.3);',
  32566. ' Result = await $mod.Func(1.1);',
  32567. ' Result = await $mod.Func(1.1);',
  32568. ' await $mod.Proc(7);',
  32569. ' await $mod.Proc(7);',
  32570. ' await $mod.Proc(13);',
  32571. ' return Result;',
  32572. '};',
  32573. '$mod.Proc = $mod.Run;',
  32574. 'if (rtl.eqCallback($mod.Proc, $mod.ProcB)) ;',
  32575. '']));
  32576. end;
  32577. procedure TTestModule.TestAsync_ProcTypeAsyncModMismatchFail;
  32578. begin
  32579. StartProgram(false);
  32580. Add([
  32581. '{$mode objfpc}',
  32582. 'type',
  32583. ' TRefFunc = reference to function(x: double = 1.3): word;',
  32584. 'function Crawl(d: double): word; async;',
  32585. 'begin',
  32586. 'end;',
  32587. 'var',
  32588. ' RefFunc: TRefFunc;',
  32589. 'begin',
  32590. ' RefFunc:=@Crawl;',
  32591. ' ']);
  32592. SetExpectedPasResolverError('procedure type modifier "async" mismatch',nXModifierMismatchY);
  32593. ConvertProgram;
  32594. end;
  32595. procedure TTestModule.TestAsync_Inherited;
  32596. begin
  32597. StartProgram(false);
  32598. Add([
  32599. '{$mode objfpc}',
  32600. '{$modeswitch externalclass}',
  32601. 'type',
  32602. ' TJSPromise = class external name ''Promise''',
  32603. ' end;',
  32604. ' TObject = class',
  32605. ' function Run(w: word = 3): word; async; virtual;',
  32606. ' end;',
  32607. ' TBird = class',
  32608. ' function Run(w: word = 3): word; async; override;',
  32609. ' end;',
  32610. 'function TObject.Run(w: word = 3): word; async;',
  32611. 'begin',
  32612. 'end;',
  32613. 'function TBird.Run(w: word = 3): word;', // async modifier not needed in impl
  32614. 'var p: TJSPromise;',
  32615. 'begin',
  32616. ' p:=inherited;',
  32617. ' p:=inherited Run;',
  32618. ' p:=inherited Run();',
  32619. ' p:=inherited Run(4);',
  32620. ' exit(p);',
  32621. ' exit(inherited);',
  32622. ' exit(inherited Run);',
  32623. ' exit(inherited Run(5));',
  32624. ' exit(6);',
  32625. 'end;',
  32626. 'begin',
  32627. ' ']);
  32628. ConvertProgram;
  32629. CheckSource('TestAsync_Inherited',
  32630. LinesToStr([ // statements
  32631. 'rtl.createClass(this, "TObject", null, function () {',
  32632. ' this.$init = function () {',
  32633. ' };',
  32634. ' this.$final = function () {',
  32635. ' };',
  32636. ' this.Run = async function (w) {',
  32637. ' var Result = 0;',
  32638. ' return Result;',
  32639. ' };',
  32640. '});',
  32641. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  32642. ' this.Run = async function (w) {',
  32643. ' var Result = 0;',
  32644. ' var p = null;',
  32645. ' p = $mod.TObject.Run.apply(this, arguments);',
  32646. ' p = $mod.TObject.Run.call(this, 3);',
  32647. ' p = $mod.TObject.Run.call(this, 3);',
  32648. ' p = $mod.TObject.Run.call(this, 4);',
  32649. ' return p;',
  32650. ' return $mod.TObject.Run.apply(this, arguments);',
  32651. ' return $mod.TObject.Run.call(this, 3);',
  32652. ' return $mod.TObject.Run.call(this, 5);',
  32653. ' return 6;',
  32654. ' return Result;',
  32655. ' };',
  32656. '});',
  32657. '']),
  32658. LinesToStr([
  32659. '']));
  32660. CheckResolverUnexpectedHints();
  32661. end;
  32662. procedure TTestModule.TestAsync_ClassInterface;
  32663. begin
  32664. StartProgram(false);
  32665. Add([
  32666. '{$mode objfpc}',
  32667. '{$modeswitch externalclass}',
  32668. 'type',
  32669. ' TJSPromise = class external name ''Promise''',
  32670. ' end;',
  32671. ' IUnknown = interface',
  32672. ' function _AddRef: longint;',
  32673. ' function _Release: longint;',
  32674. ' end;',
  32675. 'function Say(i: IUnknown): IUnknown; async;',
  32676. 'begin',
  32677. 'end;',
  32678. 'function Run: IUnknown; async;',
  32679. 'begin',
  32680. ' Result:=await(Run);',
  32681. ' Result:=await(Run());',
  32682. ' Result:=await(Run) as IUnknown;',
  32683. ' Result:=await(Say(nil));',
  32684. ' Result:=await(Say(await(Run())));',
  32685. ' Result:=await(Say(await(Run()) as IUnknown));',
  32686. ' Result:=await(Say(await(Run()) as IUnknown)) as IUnknown;',
  32687. 'end;',
  32688. 'procedure Fly;',
  32689. 'var p: TJSPromise;',
  32690. 'begin',
  32691. ' Run;',
  32692. ' Run();',
  32693. ' p:=Run;',
  32694. ' p:=Run();',
  32695. 'end;',
  32696. 'begin',
  32697. ' ']);
  32698. ConvertProgram;
  32699. CheckSource('TestAsync_ClassInterface',
  32700. LinesToStr([ // statements
  32701. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  32702. 'this.Say = async function (i) {',
  32703. ' var Result = null;',
  32704. ' return Result;',
  32705. '};',
  32706. 'this.Run = async function () {',
  32707. ' var Result = null;',
  32708. ' var $ok = false;',
  32709. ' try {',
  32710. ' Result = rtl.setIntfL(Result, await $mod.Run());',
  32711. ' Result = rtl.setIntfL(Result, await $mod.Run());',
  32712. ' Result = rtl.setIntfL(Result, rtl.intfAsIntfT(await $mod.Run(), $mod.IUnknown));',
  32713. ' Result = rtl.setIntfL(Result, await $mod.Say(null));',
  32714. ' Result = rtl.setIntfL(Result, await $mod.Say(await $mod.Run()));',
  32715. ' Result = rtl.setIntfL(Result, await $mod.Say(rtl.intfAsIntfT(await $mod.Run(), $mod.IUnknown)));',
  32716. ' Result = rtl.setIntfL(Result, rtl.intfAsIntfT(await $mod.Say(rtl.intfAsIntfT(await $mod.Run(), $mod.IUnknown)), $mod.IUnknown));',
  32717. ' $ok = true;',
  32718. ' } finally {',
  32719. ' if (!$ok) rtl._Release(Result);',
  32720. ' };',
  32721. ' return Result;',
  32722. '};',
  32723. 'this.Fly = function () {',
  32724. ' var p = null;',
  32725. ' $mod.Run();',
  32726. ' $mod.Run();',
  32727. ' p = $mod.Run();',
  32728. ' p = $mod.Run();',
  32729. '};',
  32730. '']),
  32731. LinesToStr([
  32732. '']));
  32733. CheckResolverUnexpectedHints();
  32734. end;
  32735. procedure TTestModule.TestAsync_ClassInterface_AsyncMissmatchFail;
  32736. begin
  32737. StartProgram(true,[supTInterfacedObject]);
  32738. Add([
  32739. '{$mode objfpc}',
  32740. '{$modeswitch externalclass}',
  32741. 'type',
  32742. ' TJSPromise = class external name ''Promise''',
  32743. ' end;',
  32744. ' IBird = interface',
  32745. ' procedure Run;',
  32746. ' end;',
  32747. ' TBird = class(TInterfacedObject,IBird)',
  32748. ' procedure Run; async;',
  32749. ' end;',
  32750. 'procedure TBird.Run;',
  32751. 'begin',
  32752. 'end;',
  32753. 'begin',
  32754. ' ']);
  32755. SetExpectedPasResolverError('procedure type modifier "async" mismatch',nXModifierMismatchY);
  32756. ConvertProgram;
  32757. end;
  32758. procedure TTestModule.TestLibrary_Empty;
  32759. begin
  32760. StartLibrary(false);
  32761. Add([
  32762. '']);
  32763. ConvertLibrary;
  32764. CheckSource('TestLibrary_Empty',
  32765. LinesToStr([ // statements
  32766. '']),
  32767. LinesToStr([
  32768. '']));
  32769. CheckResolverUnexpectedHints();
  32770. end;
  32771. procedure TTestModule.TestLibrary_ExportFunc;
  32772. begin
  32773. exit;
  32774. StartLibrary(false);
  32775. Add([
  32776. 'procedure Run(w: word);',
  32777. 'begin',
  32778. 'end;',
  32779. 'exports',
  32780. ' Run,',
  32781. ' run name ''Foo'';',
  32782. '']);
  32783. ConvertLibrary;
  32784. CheckSource('TestLibrary_ExportFunc',
  32785. LinesToStr([ // statements
  32786. '']),
  32787. LinesToStr([
  32788. '']));
  32789. CheckResolverUnexpectedHints();
  32790. end;
  32791. Initialization
  32792. RegisterTests([TTestModule]);
  32793. end.