tcmodules.pas 880 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702237032370423705237062370723708237092371023711237122371323714237152371623717237182371923720237212372223723237242372523726237272372823729237302373123732237332373423735237362373723738237392374023741237422374323744237452374623747237482374923750237512375223753237542375523756237572375823759237602376123762237632376423765237662376723768237692377023771237722377323774237752377623777237782377923780237812378223783237842378523786237872378823789237902379123792237932379423795237962379723798237992380023801238022380323804238052380623807238082380923810238112381223813238142381523816238172381823819238202382123822238232382423825238262382723828238292383023831238322383323834238352383623837238382383923840238412384223843238442384523846238472384823849238502385123852238532385423855238562385723858238592386023861238622386323864238652386623867238682386923870238712387223873238742387523876238772387823879238802388123882238832388423885238862388723888238892389023891238922389323894238952389623897238982389923900239012390223903239042390523906239072390823909239102391123912239132391423915239162391723918239192392023921239222392323924239252392623927239282392923930239312393223933239342393523936239372393823939239402394123942239432394423945239462394723948239492395023951239522395323954239552395623957239582395923960239612396223963239642396523966239672396823969239702397123972239732397423975239762397723978239792398023981239822398323984239852398623987239882398923990239912399223993239942399523996239972399823999240002400124002240032400424005240062400724008240092401024011240122401324014240152401624017240182401924020240212402224023240242402524026240272402824029240302403124032240332403424035240362403724038240392404024041240422404324044240452404624047240482404924050240512405224053240542405524056240572405824059240602406124062240632406424065240662406724068240692407024071240722407324074240752407624077240782407924080240812408224083240842408524086240872408824089240902409124092240932409424095240962409724098240992410024101241022410324104241052410624107241082410924110241112411224113241142411524116241172411824119241202412124122241232412424125241262412724128241292413024131241322413324134241352413624137241382413924140241412414224143241442414524146241472414824149241502415124152241532415424155241562415724158241592416024161241622416324164241652416624167241682416924170241712417224173241742417524176241772417824179241802418124182241832418424185241862418724188241892419024191241922419324194241952419624197241982419924200242012420224203242042420524206242072420824209242102421124212242132421424215242162421724218242192422024221242222422324224242252422624227242282422924230242312423224233242342423524236242372423824239242402424124242242432424424245242462424724248242492425024251242522425324254242552425624257242582425924260242612426224263242642426524266242672426824269242702427124272242732427424275242762427724278242792428024281242822428324284242852428624287242882428924290242912429224293242942429524296242972429824299243002430124302243032430424305243062430724308243092431024311243122431324314243152431624317243182431924320243212432224323243242432524326243272432824329243302433124332243332433424335243362433724338243392434024341243422434324344243452434624347243482434924350243512435224353243542435524356243572435824359243602436124362243632436424365243662436724368243692437024371243722437324374243752437624377243782437924380243812438224383243842438524386243872438824389243902439124392243932439424395243962439724398243992440024401244022440324404244052440624407244082440924410244112441224413244142441524416244172441824419244202442124422244232442424425244262442724428244292443024431244322443324434244352443624437244382443924440244412444224443244442444524446244472444824449244502445124452244532445424455244562445724458244592446024461244622446324464244652446624467244682446924470244712447224473244742447524476244772447824479244802448124482244832448424485244862448724488244892449024491244922449324494244952449624497244982449924500245012450224503245042450524506245072450824509245102451124512245132451424515245162451724518245192452024521245222452324524245252452624527245282452924530245312453224533245342453524536245372453824539245402454124542245432454424545245462454724548245492455024551245522455324554245552455624557245582455924560245612456224563245642456524566245672456824569245702457124572245732457424575245762457724578245792458024581245822458324584245852458624587245882458924590245912459224593245942459524596245972459824599246002460124602246032460424605246062460724608246092461024611246122461324614246152461624617246182461924620246212462224623246242462524626246272462824629246302463124632246332463424635246362463724638246392464024641246422464324644246452464624647246482464924650246512465224653246542465524656246572465824659246602466124662246632466424665246662466724668246692467024671246722467324674246752467624677246782467924680246812468224683246842468524686246872468824689246902469124692246932469424695246962469724698246992470024701247022470324704247052470624707247082470924710247112471224713247142471524716247172471824719247202472124722247232472424725247262472724728247292473024731247322473324734247352473624737247382473924740247412474224743247442474524746247472474824749247502475124752247532475424755247562475724758247592476024761247622476324764247652476624767247682476924770247712477224773247742477524776247772477824779247802478124782247832478424785247862478724788247892479024791247922479324794247952479624797247982479924800248012480224803248042480524806248072480824809248102481124812248132481424815248162481724818248192482024821248222482324824248252482624827248282482924830248312483224833248342483524836248372483824839248402484124842248432484424845248462484724848248492485024851248522485324854248552485624857248582485924860248612486224863248642486524866248672486824869248702487124872248732487424875248762487724878248792488024881248822488324884248852488624887248882488924890248912489224893248942489524896248972489824899249002490124902249032490424905249062490724908249092491024911249122491324914249152491624917249182491924920249212492224923249242492524926249272492824929249302493124932249332493424935249362493724938249392494024941249422494324944249452494624947249482494924950249512495224953249542495524956249572495824959249602496124962249632496424965249662496724968249692497024971249722497324974249752497624977249782497924980249812498224983249842498524986249872498824989249902499124992249932499424995249962499724998249992500025001250022500325004250052500625007250082500925010250112501225013250142501525016250172501825019250202502125022250232502425025250262502725028250292503025031250322503325034250352503625037250382503925040250412504225043250442504525046250472504825049250502505125052250532505425055250562505725058250592506025061250622506325064250652506625067250682506925070250712507225073250742507525076250772507825079250802508125082250832508425085250862508725088250892509025091250922509325094250952509625097250982509925100251012510225103251042510525106251072510825109251102511125112251132511425115251162511725118251192512025121251222512325124251252512625127251282512925130251312513225133251342513525136251372513825139251402514125142251432514425145251462514725148251492515025151251522515325154251552515625157251582515925160251612516225163251642516525166251672516825169251702517125172251732517425175251762517725178251792518025181251822518325184251852518625187251882518925190251912519225193251942519525196251972519825199252002520125202252032520425205252062520725208252092521025211252122521325214252152521625217252182521925220252212522225223252242522525226252272522825229252302523125232252332523425235252362523725238252392524025241252422524325244252452524625247252482524925250252512525225253252542525525256252572525825259252602526125262252632526425265252662526725268252692527025271252722527325274252752527625277252782527925280252812528225283252842528525286252872528825289252902529125292252932529425295252962529725298252992530025301253022530325304253052530625307253082530925310253112531225313253142531525316253172531825319253202532125322253232532425325253262532725328253292533025331253322533325334253352533625337253382533925340253412534225343253442534525346253472534825349253502535125352253532535425355253562535725358253592536025361253622536325364253652536625367253682536925370253712537225373253742537525376253772537825379253802538125382253832538425385253862538725388253892539025391253922539325394253952539625397253982539925400254012540225403254042540525406254072540825409254102541125412254132541425415254162541725418254192542025421254222542325424254252542625427254282542925430254312543225433254342543525436254372543825439254402544125442254432544425445254462544725448254492545025451254522545325454254552545625457254582545925460254612546225463254642546525466254672546825469254702547125472254732547425475254762547725478254792548025481254822548325484254852548625487254882548925490254912549225493254942549525496254972549825499255002550125502255032550425505255062550725508255092551025511255122551325514255152551625517255182551925520255212552225523255242552525526255272552825529255302553125532255332553425535255362553725538255392554025541255422554325544255452554625547255482554925550255512555225553255542555525556255572555825559255602556125562255632556425565255662556725568255692557025571255722557325574255752557625577255782557925580255812558225583255842558525586255872558825589255902559125592255932559425595255962559725598255992560025601256022560325604256052560625607256082560925610256112561225613256142561525616256172561825619256202562125622256232562425625256262562725628256292563025631256322563325634256352563625637256382563925640256412564225643256442564525646256472564825649256502565125652256532565425655256562565725658256592566025661256622566325664256652566625667256682566925670256712567225673256742567525676256772567825679256802568125682256832568425685256862568725688256892569025691256922569325694256952569625697256982569925700257012570225703257042570525706257072570825709257102571125712257132571425715257162571725718257192572025721257222572325724257252572625727257282572925730257312573225733257342573525736257372573825739257402574125742257432574425745257462574725748257492575025751257522575325754257552575625757257582575925760257612576225763257642576525766257672576825769257702577125772257732577425775257762577725778257792578025781257822578325784257852578625787257882578925790257912579225793257942579525796257972579825799258002580125802258032580425805258062580725808258092581025811258122581325814258152581625817258182581925820258212582225823258242582525826258272582825829258302583125832258332583425835258362583725838258392584025841258422584325844258452584625847258482584925850258512585225853258542585525856258572585825859258602586125862258632586425865258662586725868258692587025871258722587325874258752587625877258782587925880258812588225883258842588525886258872588825889258902589125892258932589425895258962589725898258992590025901259022590325904259052590625907259082590925910259112591225913259142591525916259172591825919259202592125922259232592425925259262592725928259292593025931259322593325934259352593625937259382593925940259412594225943259442594525946259472594825949259502595125952259532595425955259562595725958259592596025961259622596325964259652596625967259682596925970259712597225973259742597525976259772597825979259802598125982259832598425985259862598725988259892599025991259922599325994259952599625997259982599926000260012600226003260042600526006260072600826009260102601126012260132601426015260162601726018260192602026021260222602326024260252602626027260282602926030260312603226033260342603526036260372603826039260402604126042260432604426045260462604726048260492605026051260522605326054260552605626057260582605926060260612606226063260642606526066260672606826069260702607126072260732607426075260762607726078260792608026081260822608326084260852608626087260882608926090260912609226093260942609526096260972609826099261002610126102261032610426105261062610726108261092611026111261122611326114261152611626117261182611926120261212612226123261242612526126261272612826129261302613126132261332613426135261362613726138261392614026141261422614326144261452614626147261482614926150261512615226153261542615526156261572615826159261602616126162261632616426165261662616726168261692617026171261722617326174261752617626177261782617926180261812618226183261842618526186261872618826189261902619126192261932619426195261962619726198261992620026201262022620326204262052620626207262082620926210262112621226213262142621526216262172621826219262202622126222262232622426225262262622726228262292623026231262322623326234262352623626237262382623926240262412624226243262442624526246262472624826249262502625126252262532625426255262562625726258262592626026261262622626326264262652626626267262682626926270262712627226273262742627526276262772627826279262802628126282262832628426285262862628726288262892629026291262922629326294262952629626297262982629926300263012630226303263042630526306263072630826309263102631126312263132631426315263162631726318263192632026321263222632326324263252632626327263282632926330263312633226333263342633526336263372633826339263402634126342263432634426345263462634726348263492635026351263522635326354263552635626357263582635926360263612636226363263642636526366263672636826369263702637126372263732637426375263762637726378263792638026381263822638326384263852638626387263882638926390263912639226393263942639526396263972639826399264002640126402264032640426405264062640726408264092641026411264122641326414264152641626417264182641926420264212642226423264242642526426264272642826429264302643126432264332643426435264362643726438264392644026441264422644326444264452644626447264482644926450264512645226453264542645526456264572645826459264602646126462264632646426465264662646726468264692647026471264722647326474264752647626477264782647926480264812648226483264842648526486264872648826489264902649126492264932649426495264962649726498264992650026501265022650326504265052650626507265082650926510265112651226513265142651526516265172651826519265202652126522265232652426525265262652726528265292653026531265322653326534265352653626537265382653926540265412654226543265442654526546265472654826549265502655126552265532655426555265562655726558265592656026561265622656326564265652656626567265682656926570265712657226573265742657526576265772657826579265802658126582265832658426585265862658726588265892659026591265922659326594265952659626597265982659926600266012660226603266042660526606266072660826609266102661126612266132661426615266162661726618266192662026621266222662326624266252662626627266282662926630266312663226633266342663526636266372663826639266402664126642266432664426645266462664726648266492665026651266522665326654266552665626657266582665926660266612666226663266642666526666266672666826669266702667126672266732667426675266762667726678266792668026681266822668326684266852668626687266882668926690266912669226693266942669526696266972669826699267002670126702267032670426705267062670726708267092671026711267122671326714267152671626717267182671926720267212672226723267242672526726267272672826729267302673126732267332673426735267362673726738267392674026741267422674326744267452674626747267482674926750267512675226753267542675526756267572675826759267602676126762267632676426765267662676726768267692677026771267722677326774267752677626777267782677926780267812678226783267842678526786267872678826789267902679126792267932679426795267962679726798267992680026801268022680326804268052680626807268082680926810268112681226813268142681526816268172681826819268202682126822268232682426825268262682726828268292683026831268322683326834268352683626837268382683926840268412684226843268442684526846268472684826849268502685126852268532685426855268562685726858268592686026861268622686326864268652686626867268682686926870268712687226873268742687526876268772687826879268802688126882268832688426885268862688726888268892689026891268922689326894268952689626897268982689926900269012690226903269042690526906269072690826909269102691126912269132691426915269162691726918269192692026921269222692326924269252692626927269282692926930269312693226933269342693526936269372693826939269402694126942269432694426945269462694726948269492695026951269522695326954269552695626957269582695926960269612696226963269642696526966269672696826969269702697126972269732697426975269762697726978269792698026981269822698326984269852698626987269882698926990269912699226993269942699526996269972699826999270002700127002270032700427005270062700727008270092701027011270122701327014270152701627017270182701927020270212702227023270242702527026270272702827029270302703127032270332703427035270362703727038270392704027041270422704327044270452704627047270482704927050270512705227053270542705527056270572705827059270602706127062270632706427065270662706727068270692707027071270722707327074270752707627077270782707927080270812708227083270842708527086270872708827089270902709127092270932709427095270962709727098270992710027101271022710327104271052710627107271082710927110271112711227113271142711527116271172711827119271202712127122271232712427125271262712727128271292713027131271322713327134271352713627137271382713927140271412714227143271442714527146271472714827149271502715127152271532715427155271562715727158271592716027161271622716327164271652716627167271682716927170271712717227173271742717527176271772717827179271802718127182271832718427185271862718727188271892719027191271922719327194271952719627197271982719927200272012720227203272042720527206272072720827209272102721127212272132721427215272162721727218272192722027221272222722327224272252722627227272282722927230272312723227233272342723527236272372723827239272402724127242272432724427245272462724727248272492725027251272522725327254272552725627257272582725927260272612726227263272642726527266272672726827269272702727127272272732727427275272762727727278272792728027281272822728327284272852728627287272882728927290272912729227293272942729527296272972729827299273002730127302273032730427305273062730727308273092731027311273122731327314273152731627317273182731927320273212732227323273242732527326273272732827329273302733127332273332733427335273362733727338273392734027341273422734327344273452734627347273482734927350273512735227353273542735527356273572735827359273602736127362273632736427365273662736727368273692737027371273722737327374273752737627377273782737927380273812738227383273842738527386273872738827389273902739127392273932739427395273962739727398273992740027401274022740327404274052740627407274082740927410274112741227413274142741527416274172741827419274202742127422274232742427425274262742727428274292743027431274322743327434274352743627437274382743927440274412744227443274442744527446274472744827449274502745127452274532745427455274562745727458274592746027461274622746327464274652746627467274682746927470274712747227473274742747527476274772747827479274802748127482274832748427485274862748727488274892749027491274922749327494274952749627497274982749927500275012750227503275042750527506275072750827509275102751127512275132751427515275162751727518275192752027521275222752327524275252752627527275282752927530275312753227533275342753527536275372753827539275402754127542275432754427545275462754727548275492755027551275522755327554275552755627557275582755927560275612756227563275642756527566275672756827569275702757127572275732757427575275762757727578275792758027581275822758327584275852758627587275882758927590275912759227593275942759527596275972759827599276002760127602276032760427605276062760727608276092761027611276122761327614276152761627617276182761927620276212762227623276242762527626276272762827629276302763127632276332763427635276362763727638276392764027641276422764327644276452764627647276482764927650276512765227653276542765527656276572765827659276602766127662276632766427665276662766727668276692767027671276722767327674276752767627677276782767927680276812768227683276842768527686276872768827689276902769127692276932769427695276962769727698276992770027701277022770327704277052770627707277082770927710277112771227713277142771527716277172771827719277202772127722277232772427725277262772727728277292773027731277322773327734277352773627737277382773927740277412774227743277442774527746277472774827749277502775127752277532775427755277562775727758277592776027761277622776327764277652776627767277682776927770277712777227773277742777527776277772777827779277802778127782277832778427785277862778727788277892779027791277922779327794277952779627797277982779927800278012780227803278042780527806278072780827809278102781127812278132781427815278162781727818278192782027821278222782327824278252782627827278282782927830278312783227833278342783527836278372783827839278402784127842278432784427845278462784727848278492785027851278522785327854278552785627857278582785927860278612786227863278642786527866278672786827869278702787127872278732787427875278762787727878278792788027881278822788327884278852788627887278882788927890278912789227893278942789527896278972789827899279002790127902279032790427905279062790727908279092791027911279122791327914279152791627917279182791927920279212792227923279242792527926279272792827929279302793127932279332793427935279362793727938279392794027941279422794327944279452794627947279482794927950279512795227953279542795527956279572795827959279602796127962279632796427965279662796727968279692797027971279722797327974279752797627977279782797927980279812798227983279842798527986279872798827989279902799127992279932799427995279962799727998279992800028001280022800328004280052800628007280082800928010280112801228013280142801528016280172801828019280202802128022280232802428025280262802728028280292803028031280322803328034280352803628037280382803928040280412804228043280442804528046280472804828049280502805128052280532805428055280562805728058280592806028061280622806328064280652806628067280682806928070280712807228073280742807528076280772807828079280802808128082280832808428085280862808728088280892809028091280922809328094280952809628097280982809928100281012810228103281042810528106281072810828109281102811128112281132811428115281162811728118281192812028121281222812328124281252812628127281282812928130281312813228133281342813528136281372813828139281402814128142281432814428145281462814728148281492815028151281522815328154281552815628157281582815928160281612816228163281642816528166281672816828169281702817128172281732817428175281762817728178281792818028181281822818328184281852818628187281882818928190281912819228193281942819528196281972819828199282002820128202282032820428205282062820728208282092821028211282122821328214282152821628217282182821928220282212822228223282242822528226282272822828229282302823128232282332823428235282362823728238282392824028241282422824328244282452824628247282482824928250282512825228253282542825528256282572825828259282602826128262282632826428265282662826728268282692827028271282722827328274282752827628277282782827928280282812828228283282842828528286282872828828289282902829128292282932829428295282962829728298282992830028301283022830328304283052830628307283082830928310283112831228313283142831528316283172831828319283202832128322283232832428325283262832728328283292833028331283322833328334283352833628337283382833928340283412834228343283442834528346283472834828349283502835128352283532835428355283562835728358283592836028361283622836328364283652836628367283682836928370283712837228373283742837528376283772837828379283802838128382283832838428385283862838728388283892839028391283922839328394283952839628397283982839928400284012840228403284042840528406284072840828409284102841128412284132841428415284162841728418284192842028421284222842328424284252842628427284282842928430284312843228433284342843528436284372843828439284402844128442284432844428445284462844728448284492845028451284522845328454284552845628457284582845928460284612846228463284642846528466284672846828469284702847128472284732847428475284762847728478284792848028481284822848328484284852848628487284882848928490284912849228493284942849528496284972849828499285002850128502285032850428505285062850728508285092851028511285122851328514285152851628517285182851928520285212852228523285242852528526285272852828529285302853128532285332853428535285362853728538285392854028541285422854328544285452854628547285482854928550285512855228553285542855528556285572855828559285602856128562285632856428565285662856728568285692857028571285722857328574285752857628577285782857928580285812858228583285842858528586285872858828589285902859128592285932859428595285962859728598285992860028601286022860328604286052860628607286082860928610286112861228613286142861528616286172861828619286202862128622286232862428625286262862728628286292863028631286322863328634286352863628637286382863928640286412864228643286442864528646286472864828649286502865128652286532865428655286562865728658286592866028661286622866328664286652866628667286682866928670286712867228673286742867528676286772867828679286802868128682286832868428685286862868728688286892869028691286922869328694286952869628697286982869928700287012870228703287042870528706287072870828709287102871128712287132871428715287162871728718287192872028721287222872328724287252872628727287282872928730287312873228733287342873528736287372873828739287402874128742287432874428745287462874728748287492875028751287522875328754287552875628757287582875928760287612876228763287642876528766287672876828769287702877128772287732877428775287762877728778287792878028781287822878328784287852878628787287882878928790287912879228793287942879528796287972879828799288002880128802288032880428805288062880728808288092881028811288122881328814288152881628817288182881928820288212882228823288242882528826288272882828829288302883128832288332883428835288362883728838288392884028841288422884328844288452884628847288482884928850288512885228853288542885528856288572885828859288602886128862288632886428865288662886728868288692887028871288722887328874288752887628877288782887928880288812888228883288842888528886288872888828889288902889128892288932889428895288962889728898288992890028901289022890328904289052890628907289082890928910289112891228913289142891528916289172891828919289202892128922289232892428925289262892728928289292893028931289322893328934289352893628937289382893928940289412894228943289442894528946289472894828949289502895128952289532895428955289562895728958289592896028961289622896328964289652896628967289682896928970289712897228973289742897528976289772897828979289802898128982289832898428985289862898728988289892899028991289922899328994289952899628997289982899929000290012900229003290042900529006290072900829009290102901129012290132901429015290162901729018290192902029021290222902329024290252902629027290282902929030290312903229033290342903529036290372903829039290402904129042290432904429045290462904729048290492905029051290522905329054290552905629057290582905929060290612906229063290642906529066290672906829069290702907129072290732907429075290762907729078290792908029081290822908329084290852908629087290882908929090290912909229093290942909529096290972909829099291002910129102291032910429105291062910729108291092911029111291122911329114291152911629117291182911929120291212912229123291242912529126291272912829129291302913129132291332913429135291362913729138291392914029141291422914329144291452914629147291482914929150291512915229153291542915529156291572915829159291602916129162291632916429165291662916729168291692917029171291722917329174291752917629177291782917929180291812918229183291842918529186291872918829189291902919129192291932919429195291962919729198291992920029201292022920329204292052920629207292082920929210292112921229213292142921529216292172921829219292202922129222292232922429225292262922729228292292923029231292322923329234292352923629237292382923929240292412924229243292442924529246292472924829249292502925129252292532925429255292562925729258292592926029261292622926329264292652926629267292682926929270292712927229273292742927529276292772927829279292802928129282292832928429285292862928729288292892929029291292922929329294292952929629297292982929929300293012930229303293042930529306293072930829309293102931129312293132931429315293162931729318293192932029321293222932329324293252932629327293282932929330293312933229333293342933529336293372933829339293402934129342293432934429345293462934729348293492935029351293522935329354293552935629357293582935929360293612936229363293642936529366293672936829369293702937129372293732937429375293762937729378293792938029381293822938329384293852938629387293882938929390293912939229393293942939529396293972939829399294002940129402294032940429405294062940729408294092941029411294122941329414294152941629417294182941929420294212942229423294242942529426294272942829429294302943129432294332943429435294362943729438294392944029441294422944329444294452944629447294482944929450294512945229453294542945529456294572945829459294602946129462294632946429465294662946729468294692947029471294722947329474294752947629477294782947929480294812948229483294842948529486294872948829489294902949129492294932949429495294962949729498294992950029501295022950329504295052950629507295082950929510295112951229513295142951529516295172951829519295202952129522295232952429525295262952729528295292953029531295322953329534295352953629537295382953929540295412954229543295442954529546295472954829549295502955129552295532955429555295562955729558295592956029561295622956329564295652956629567295682956929570295712957229573295742957529576295772957829579295802958129582295832958429585295862958729588295892959029591295922959329594295952959629597295982959929600296012960229603296042960529606296072960829609296102961129612296132961429615296162961729618296192962029621296222962329624296252962629627296282962929630296312963229633296342963529636296372963829639296402964129642296432964429645296462964729648296492965029651296522965329654296552965629657296582965929660296612966229663296642966529666296672966829669296702967129672296732967429675296762967729678296792968029681296822968329684296852968629687296882968929690296912969229693296942969529696296972969829699297002970129702297032970429705297062970729708297092971029711297122971329714297152971629717297182971929720297212972229723297242972529726297272972829729297302973129732297332973429735297362973729738297392974029741297422974329744297452974629747297482974929750297512975229753297542975529756297572975829759297602976129762297632976429765297662976729768297692977029771297722977329774297752977629777297782977929780297812978229783297842978529786297872978829789297902979129792297932979429795297962979729798297992980029801298022980329804298052980629807298082980929810298112981229813298142981529816298172981829819298202982129822298232982429825298262982729828298292983029831298322983329834298352983629837298382983929840298412984229843298442984529846298472984829849298502985129852298532985429855298562985729858298592986029861298622986329864298652986629867298682986929870298712987229873298742987529876298772987829879298802988129882298832988429885298862988729888298892989029891298922989329894298952989629897298982989929900299012990229903299042990529906299072990829909299102991129912299132991429915299162991729918299192992029921299222992329924299252992629927299282992929930299312993229933299342993529936299372993829939299402994129942299432994429945299462994729948299492995029951299522995329954299552995629957299582995929960299612996229963299642996529966299672996829969299702997129972299732997429975299762997729978299792998029981299822998329984299852998629987299882998929990299912999229993299942999529996299972999829999300003000130002300033000430005300063000730008300093001030011300123001330014300153001630017300183001930020300213002230023300243002530026300273002830029300303003130032300333003430035300363003730038300393004030041300423004330044300453004630047300483004930050300513005230053300543005530056300573005830059300603006130062300633006430065300663006730068300693007030071300723007330074300753007630077300783007930080300813008230083300843008530086300873008830089300903009130092300933009430095300963009730098300993010030101301023010330104301053010630107301083010930110301113011230113301143011530116301173011830119301203012130122301233012430125301263012730128301293013030131301323013330134301353013630137301383013930140301413014230143301443014530146301473014830149301503015130152301533015430155301563015730158301593016030161301623016330164301653016630167301683016930170301713017230173301743017530176301773017830179301803018130182301833018430185301863018730188301893019030191301923019330194301953019630197301983019930200302013020230203302043020530206302073020830209302103021130212302133021430215302163021730218302193022030221302223022330224302253022630227302283022930230302313023230233302343023530236302373023830239302403024130242302433024430245302463024730248302493025030251302523025330254302553025630257302583025930260302613026230263302643026530266302673026830269302703027130272302733027430275302763027730278302793028030281302823028330284302853028630287302883028930290302913029230293302943029530296302973029830299303003030130302303033030430305303063030730308303093031030311303123031330314303153031630317303183031930320303213032230323303243032530326303273032830329303303033130332303333033430335303363033730338303393034030341303423034330344303453034630347303483034930350303513035230353303543035530356303573035830359303603036130362303633036430365303663036730368303693037030371303723037330374303753037630377303783037930380303813038230383303843038530386303873038830389303903039130392303933039430395303963039730398303993040030401304023040330404304053040630407304083040930410304113041230413304143041530416304173041830419304203042130422304233042430425304263042730428304293043030431304323043330434304353043630437304383043930440304413044230443304443044530446304473044830449304503045130452304533045430455304563045730458304593046030461304623046330464304653046630467304683046930470304713047230473304743047530476304773047830479304803048130482304833048430485304863048730488304893049030491304923049330494304953049630497304983049930500305013050230503305043050530506305073050830509305103051130512305133051430515305163051730518305193052030521305223052330524305253052630527305283052930530305313053230533305343053530536305373053830539305403054130542305433054430545305463054730548305493055030551305523055330554305553055630557305583055930560305613056230563305643056530566305673056830569305703057130572305733057430575305763057730578305793058030581305823058330584305853058630587305883058930590305913059230593305943059530596305973059830599306003060130602306033060430605306063060730608306093061030611306123061330614306153061630617306183061930620306213062230623306243062530626306273062830629306303063130632306333063430635306363063730638306393064030641306423064330644306453064630647306483064930650306513065230653306543065530656306573065830659306603066130662306633066430665306663066730668306693067030671306723067330674306753067630677306783067930680306813068230683306843068530686306873068830689306903069130692306933069430695306963069730698306993070030701307023070330704307053070630707307083070930710307113071230713307143071530716307173071830719307203072130722307233072430725307263072730728307293073030731307323073330734307353073630737307383073930740307413074230743307443074530746307473074830749307503075130752307533075430755307563075730758307593076030761307623076330764307653076630767307683076930770307713077230773307743077530776307773077830779307803078130782307833078430785307863078730788307893079030791307923079330794307953079630797307983079930800308013080230803308043080530806308073080830809308103081130812308133081430815308163081730818308193082030821308223082330824308253082630827308283082930830308313083230833308343083530836308373083830839308403084130842308433084430845308463084730848308493085030851308523085330854308553085630857308583085930860308613086230863308643086530866308673086830869308703087130872308733087430875308763087730878308793088030881308823088330884308853088630887308883088930890308913089230893308943089530896308973089830899309003090130902309033090430905309063090730908309093091030911309123091330914309153091630917309183091930920309213092230923309243092530926309273092830929309303093130932309333093430935309363093730938309393094030941309423094330944309453094630947309483094930950309513095230953309543095530956309573095830959309603096130962309633096430965309663096730968309693097030971309723097330974309753097630977309783097930980309813098230983309843098530986309873098830989309903099130992309933099430995309963099730998309993100031001310023100331004310053100631007310083100931010310113101231013310143101531016310173101831019310203102131022310233102431025310263102731028310293103031031310323103331034310353103631037310383103931040310413104231043310443104531046310473104831049310503105131052310533105431055310563105731058310593106031061310623106331064310653106631067310683106931070310713107231073310743107531076310773107831079310803108131082310833108431085310863108731088310893109031091310923109331094310953109631097310983109931100311013110231103311043110531106311073110831109311103111131112311133111431115311163111731118311193112031121311223112331124311253112631127311283112931130311313113231133311343113531136311373113831139311403114131142311433114431145311463114731148311493115031151311523115331154311553115631157311583115931160311613116231163311643116531166311673116831169311703117131172311733117431175311763117731178311793118031181311823118331184311853118631187311883118931190311913119231193311943119531196311973119831199312003120131202312033120431205312063120731208312093121031211312123121331214312153121631217312183121931220312213122231223312243122531226312273122831229312303123131232312333123431235312363123731238312393124031241312423124331244312453124631247312483124931250312513125231253312543125531256312573125831259312603126131262312633126431265312663126731268312693127031271312723127331274312753127631277312783127931280312813128231283312843128531286312873128831289312903129131292312933129431295312963129731298312993130031301313023130331304313053130631307313083130931310313113131231313313143131531316313173131831319313203132131322313233132431325313263132731328313293133031331313323133331334313353133631337313383133931340313413134231343313443134531346313473134831349313503135131352313533135431355313563135731358313593136031361313623136331364313653136631367313683136931370313713137231373313743137531376313773137831379313803138131382313833138431385313863138731388313893139031391313923139331394313953139631397313983139931400314013140231403314043140531406314073140831409314103141131412314133141431415314163141731418314193142031421314223142331424314253142631427314283142931430314313143231433314343143531436314373143831439314403144131442314433144431445314463144731448314493145031451314523145331454314553145631457314583145931460314613146231463314643146531466314673146831469314703147131472314733147431475314763147731478314793148031481314823148331484314853148631487314883148931490314913149231493314943149531496314973149831499315003150131502315033150431505315063150731508315093151031511315123151331514315153151631517315183151931520315213152231523315243152531526315273152831529315303153131532315333153431535315363153731538315393154031541315423154331544315453154631547315483154931550315513155231553315543155531556315573155831559315603156131562315633156431565315663156731568315693157031571315723157331574315753157631577315783157931580315813158231583315843158531586315873158831589315903159131592315933159431595315963159731598315993160031601316023160331604316053160631607316083160931610316113161231613316143161531616316173161831619316203162131622316233162431625316263162731628316293163031631316323163331634316353163631637316383163931640316413164231643316443164531646316473164831649316503165131652316533165431655316563165731658316593166031661316623166331664316653166631667316683166931670316713167231673316743167531676316773167831679316803168131682316833168431685316863168731688316893169031691316923169331694316953169631697316983169931700317013170231703317043170531706317073170831709317103171131712317133171431715317163171731718317193172031721317223172331724317253172631727317283172931730317313173231733317343173531736317373173831739317403174131742317433174431745317463174731748317493175031751317523175331754317553175631757317583175931760317613176231763317643176531766317673176831769317703177131772317733177431775317763177731778317793178031781317823178331784317853178631787317883178931790317913179231793317943179531796317973179831799318003180131802318033180431805318063180731808318093181031811318123181331814318153181631817318183181931820318213182231823318243182531826318273182831829318303183131832318333183431835318363183731838318393184031841318423184331844318453184631847318483184931850318513185231853318543185531856318573185831859318603186131862318633186431865318663186731868318693187031871318723187331874318753187631877318783187931880318813188231883318843188531886318873188831889318903189131892318933189431895318963189731898318993190031901319023190331904319053190631907319083190931910319113191231913319143191531916319173191831919319203192131922319233192431925319263192731928319293193031931319323193331934319353193631937319383193931940319413194231943319443194531946319473194831949319503195131952319533195431955319563195731958319593196031961319623196331964319653196631967319683196931970319713197231973319743197531976319773197831979319803198131982319833198431985319863198731988319893199031991319923199331994319953199631997319983199932000320013200232003320043200532006320073200832009320103201132012320133201432015320163201732018320193202032021320223202332024320253202632027320283202932030320313203232033320343203532036320373203832039320403204132042320433204432045320463204732048320493205032051320523205332054320553205632057320583205932060320613206232063320643206532066320673206832069320703207132072320733207432075320763207732078320793208032081320823208332084320853208632087320883208932090320913209232093320943209532096320973209832099321003210132102321033210432105321063210732108321093211032111321123211332114321153211632117321183211932120321213212232123321243212532126321273212832129321303213132132321333213432135321363213732138321393214032141321423214332144321453214632147321483214932150321513215232153321543215532156321573215832159321603216132162321633216432165321663216732168321693217032171321723217332174321753217632177321783217932180321813218232183321843218532186321873218832189321903219132192321933219432195321963219732198321993220032201322023220332204322053220632207322083220932210322113221232213322143221532216322173221832219322203222132222322233222432225322263222732228322293223032231322323223332234322353223632237322383223932240322413224232243322443224532246322473224832249322503225132252322533225432255322563225732258322593226032261322623226332264322653226632267322683226932270322713227232273322743227532276322773227832279322803228132282322833228432285322863228732288322893229032291322923229332294322953229632297322983229932300323013230232303323043230532306323073230832309323103231132312323133231432315323163231732318323193232032321323223232332324323253232632327323283232932330323313233232333323343233532336323373233832339323403234132342323433234432345323463234732348323493235032351323523235332354323553235632357323583235932360323613236232363323643236532366323673236832369323703237132372323733237432375323763237732378323793238032381323823238332384323853238632387323883238932390323913239232393323943239532396323973239832399324003240132402324033240432405324063240732408324093241032411324123241332414324153241632417324183241932420324213242232423324243242532426324273242832429324303243132432324333243432435324363243732438324393244032441324423244332444324453244632447324483244932450324513245232453324543245532456324573245832459324603246132462324633246432465324663246732468324693247032471324723247332474324753247632477324783247932480324813248232483324843248532486324873248832489324903249132492324933249432495324963249732498324993250032501325023250332504325053250632507325083250932510325113251232513325143251532516325173251832519325203252132522325233252432525325263252732528325293253032531325323253332534325353253632537325383253932540325413254232543325443254532546325473254832549325503255132552325533255432555325563255732558325593256032561325623256332564325653256632567325683256932570325713257232573325743257532576325773257832579325803258132582325833258432585325863258732588325893259032591325923259332594325953259632597325983259932600326013260232603326043260532606326073260832609326103261132612326133261432615326163261732618326193262032621326223262332624326253262632627326283262932630326313263232633326343263532636326373263832639326403264132642326433264432645326463264732648326493265032651326523265332654326553265632657326583265932660326613266232663326643266532666326673266832669326703267132672326733267432675326763267732678326793268032681326823268332684326853268632687326883268932690326913269232693326943269532696326973269832699327003270132702327033270432705327063270732708327093271032711327123271332714327153271632717327183271932720327213272232723327243272532726327273272832729327303273132732327333273432735327363273732738327393274032741327423274332744327453274632747327483274932750327513275232753327543275532756327573275832759327603276132762327633276432765327663276732768327693277032771327723277332774327753277632777327783277932780327813278232783327843278532786327873278832789327903279132792327933279432795327963279732798327993280032801328023280332804328053280632807328083280932810328113281232813328143281532816328173281832819328203282132822328233282432825328263282732828328293283032831328323283332834328353283632837328383283932840328413284232843328443284532846328473284832849328503285132852328533285432855328563285732858328593286032861328623286332864328653286632867328683286932870328713287232873328743287532876328773287832879328803288132882328833288432885328863288732888328893289032891328923289332894328953289632897328983289932900329013290232903329043290532906329073290832909329103291132912329133291432915329163291732918329193292032921329223292332924329253292632927329283292932930329313293232933329343293532936329373293832939329403294132942329433294432945329463294732948329493295032951329523295332954329553295632957329583295932960329613296232963329643296532966329673296832969329703297132972329733297432975329763297732978329793298032981329823298332984329853298632987329883298932990329913299232993329943299532996329973299832999330003300133002330033300433005330063300733008330093301033011330123301333014330153301633017330183301933020330213302233023330243302533026330273302833029330303303133032330333303433035330363303733038330393304033041330423304333044330453304633047330483304933050330513305233053330543305533056330573305833059330603306133062330633306433065330663306733068330693307033071330723307333074330753307633077330783307933080330813308233083330843308533086330873308833089330903309133092330933309433095330963309733098330993310033101331023310333104331053310633107331083310933110331113311233113331143311533116331173311833119331203312133122331233312433125331263312733128331293313033131331323313333134331353313633137331383313933140331413314233143331443314533146331473314833149331503315133152331533315433155331563315733158331593316033161331623316333164331653316633167331683316933170331713317233173331743317533176331773317833179331803318133182331833318433185331863318733188331893319033191331923319333194331953319633197331983319933200332013320233203332043320533206332073320833209332103321133212332133321433215332163321733218332193322033221332223322333224332253322633227332283322933230332313323233233332343323533236332373323833239332403324133242332433324433245332463324733248332493325033251332523325333254332553325633257332583325933260332613326233263332643326533266332673326833269332703327133272332733327433275332763327733278332793328033281332823328333284332853328633287332883328933290332913329233293332943329533296332973329833299333003330133302333033330433305333063330733308333093331033311333123331333314333153331633317333183331933320333213332233323333243332533326333273332833329333303333133332333333333433335333363333733338333393334033341333423334333344333453334633347333483334933350333513335233353333543335533356333573335833359333603336133362333633336433365333663336733368333693337033371333723337333374333753337633377333783337933380333813338233383333843338533386333873338833389333903339133392333933339433395333963339733398333993340033401334023340333404334053340633407334083340933410334113341233413334143341533416334173341833419334203342133422334233342433425334263342733428334293343033431334323343333434334353343633437334383343933440334413344233443334443344533446334473344833449334503345133452334533345433455334563345733458334593346033461334623346333464334653346633467334683346933470334713347233473334743347533476334773347833479334803348133482334833348433485334863348733488334893349033491334923349333494334953349633497334983349933500335013350233503335043350533506335073350833509335103351133512335133351433515335163351733518335193352033521335223352333524335253352633527335283352933530335313353233533335343353533536335373353833539335403354133542335433354433545335463354733548335493355033551335523355333554335553355633557335583355933560335613356233563335643356533566335673356833569335703357133572335733357433575
  1. {
  2. This file is part of the Free Component Library (FCL)
  3. Copyright (c) 2018 by Michael Van Canneyt
  4. Unit tests for Pascal-to-Javascript converter class.
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************
  11. Examples:
  12. ./testpas2js --suite=TTestModule.TestEmptyProgram
  13. ./testpas2js --suite=TTestModule.TestEmptyUnit
  14. }
  15. unit TCModules;
  16. {$mode objfpc}{$H+}
  17. interface
  18. uses
  19. Classes, SysUtils, fpcunit, testregistry, contnrs,
  20. jstree, jswriter, jsbase,
  21. PasTree, PScanner, PasResolver, PParser, PasResolveEval,
  22. FPPas2Js;
  23. const
  24. // default parser+scanner options
  25. po_tcmodules = po_Pas2js+[po_KeepScannerError];
  26. co_tcmodules = [];
  27. type
  28. TSrcMarkerKind = (
  29. mkLabel,
  30. mkResolverReference,
  31. mkDirectReference
  32. );
  33. PSrcMarker = ^TSrcMarker;
  34. TSrcMarker = record
  35. Kind: TSrcMarkerKind;
  36. Filename: string;
  37. Row: integer;
  38. StartCol, EndCol: integer; // token start, end column
  39. Identifier: string;
  40. Next: PSrcMarker;
  41. end;
  42. TSystemUnitPart = (
  43. supTObject,
  44. supTVarRec,
  45. supTypeInfo,
  46. supTInterfacedObject,
  47. supWriteln
  48. );
  49. TSystemUnitParts = set of TSystemUnitPart;
  50. { TTestHintMessage }
  51. TTestHintMessage = class
  52. public
  53. Id: int64;
  54. MsgType: TMessageType;
  55. MsgNumber: integer;
  56. Msg: string;
  57. SourcePos: TPasSourcePos;
  58. end;
  59. { TTestPasParser }
  60. TTestPasParser = Class(TPasParser)
  61. end;
  62. TOnFindUnit = function(const aUnitName: String): TPasModule of object;
  63. { TTestEnginePasResolver }
  64. TTestEnginePasResolver = class(TPas2JsResolver)
  65. private
  66. FFilename: string;
  67. FModule: TPasModule;
  68. FOnFindUnit: TOnFindUnit;
  69. FParser: TTestPasParser;
  70. FStreamResolver: TStreamResolver;
  71. FScanner: TPas2jsPasScanner;
  72. FSource: string;
  73. public
  74. destructor Destroy; override;
  75. function FindUnit(const AName, InFilename: String; NameExpr,
  76. InFileExpr: TPasExpr): TPasModule; override;
  77. procedure UsedInterfacesFinished(Section: TPasSection); override;
  78. property OnFindUnit: TOnFindUnit read FOnFindUnit write FOnFindUnit;
  79. property Filename: string read FFilename write FFilename;
  80. property StreamResolver: TStreamResolver read FStreamResolver write FStreamResolver;
  81. property Scanner: TPas2jsPasScanner read FScanner write FScanner;
  82. property Parser: TTestPasParser read FParser write FParser;
  83. property Source: string read FSource write FSource;
  84. property Module: TPasModule read FModule;
  85. end;
  86. { TCustomTestModule }
  87. TCustomTestModule = Class(TTestCase)
  88. private
  89. FConverter: TPasToJSConverter;
  90. FEngine: TTestEnginePasResolver;
  91. FExpectedErrorClass: ExceptClass;
  92. FExpectedErrorMsg: string;
  93. FExpectedErrorNumber: integer;
  94. FFilename: string;
  95. FFileResolver: TStreamResolver;
  96. FHub: TPas2JSResolverHub;
  97. FJSImplementationUses: TJSArrayLiteral;
  98. FJSInitBody: TJSFunctionBody;
  99. FJSImplentationUses: TJSArrayLiteral;
  100. FJSInterfaceUses: TJSArrayLiteral;
  101. FJSModule: TJSSourceElements;
  102. FJSModuleSrc: TJSSourceElements;
  103. FJSSource: TStringList;
  104. FModule: TPasModule;
  105. FJSModuleCallArgs: TJSArguments;
  106. FModules: TObjectList;// list of TTestEnginePasResolver
  107. FParser: TTestPasParser;
  108. FPasProgram: TPasProgram;
  109. FPasLibrary: TPasLibrary;
  110. FHintMsgs: TObjectList; // list of TTestHintMessage
  111. FHintMsgsGood: TFPList; // list of TTestHintMessage marked as expected
  112. FJSRegModuleCall: TJSCallExpression;
  113. FScanner: TPas2jsPasScanner;
  114. FSkipTests: boolean;
  115. FSource: TStringList;
  116. FFirstPasStatement: TPasImplBlock;
  117. FWithTypeInfo: boolean;
  118. {$IFDEF EnablePasTreeGlobalRefCount}
  119. FElementRefCountAtSetup: int64;
  120. {$ENDIF}
  121. function GetMsgCount: integer;
  122. function GetMsgs(Index: integer): TTestHintMessage;
  123. function GetResolverCount: integer;
  124. function GetResolvers(Index: integer): TTestEnginePasResolver;
  125. function OnPasResolverFindUnit(const aUnitName: String): TPasModule;
  126. procedure OnParserLog(Sender: TObject; const Msg: String);
  127. procedure OnPasResolverLog(Sender: TObject; const Msg: String);
  128. procedure OnScannerLog(Sender: TObject; const Msg: String);
  129. procedure SetWithTypeInfo(const AValue: boolean);
  130. protected
  131. procedure SetUp; override;
  132. function CreateConverter: TPasToJSConverter; virtual;
  133. function LoadUnit(const aUnitName: String): TPasModule;
  134. procedure InitScanner(aScanner: TPas2jsPasScanner); virtual;
  135. procedure TearDown; override;
  136. Procedure Add(Line: string); virtual;
  137. Procedure Add(const Lines: array of string);
  138. Procedure StartParsing; virtual;
  139. procedure ParseModuleQueue; virtual;
  140. procedure ParseModule; virtual;
  141. procedure ParseProgram; virtual;
  142. procedure ParseLibrary; virtual;
  143. procedure ParseUnit; virtual;
  144. protected
  145. function FindModuleWithFilename(aFilename: string): TTestEnginePasResolver; virtual;
  146. function AddModule(aFilename: string): TTestEnginePasResolver; virtual;
  147. function AddModuleWithSrc(aFilename, Src: string): TTestEnginePasResolver; virtual;
  148. function AddModuleWithIntfImplSrc(aFilename, InterfaceSrc,
  149. ImplementationSrc: string): TTestEnginePasResolver; virtual;
  150. procedure AddSystemUnit(Parts: TSystemUnitParts = []); virtual;
  151. procedure StartProgram(NeedSystemUnit: boolean; SystemUnitParts: TSystemUnitParts = []); virtual;
  152. procedure StartLibrary(NeedSystemUnit: boolean; SystemUnitParts: TSystemUnitParts = []); virtual;
  153. procedure StartUnit(NeedSystemUnit: boolean; SystemUnitParts: TSystemUnitParts = []); virtual;
  154. procedure ConvertModule; virtual;
  155. procedure ConvertProgram; virtual;
  156. procedure ConvertLibrary; virtual;
  157. procedure ConvertUnit; virtual;
  158. function ConvertJSModuleToString(El: TJSElement): string; virtual;
  159. procedure CheckDottedIdentifier(Msg: string; El: TJSElement; DottedName: string);
  160. function GetDottedIdentifier(El: TJSElement): string;
  161. procedure CheckSource(Msg,Statements: String; InitStatements: string = '';
  162. ImplStatements: string = ''); virtual;
  163. procedure CheckDiff(Msg, Expected, Actual: string); virtual;
  164. procedure CheckUnit(Filename, ExpectedSrc: string); virtual;
  165. procedure CheckHint(MsgType: TMessageType; MsgNumber: integer;
  166. Msg: string; Marker: PSrcMarker = nil); virtual;
  167. procedure CheckResolverUnexpectedHints(WithSourcePos: boolean = false); virtual;
  168. procedure SetExpectedScannerError(Msg: string; MsgNumber: integer);
  169. procedure SetExpectedParserError(Msg: string; MsgNumber: integer);
  170. procedure SetExpectedPasResolverError(Msg: string; MsgNumber: integer);
  171. procedure SetExpectedConverterError(Msg: string; MsgNumber: integer);
  172. function IsErrorExpected(E: Exception): boolean;
  173. procedure HandleScannerError(E: EScannerError);
  174. procedure HandleParserError(E: EParserError);
  175. procedure HandlePasResolveError(E: EPasResolve);
  176. procedure HandlePas2JSError(E: EPas2JS);
  177. procedure HandleException(E: Exception);
  178. procedure FailException(E: Exception);
  179. procedure WriteSources(const aFilename: string; aRow, aCol: integer);
  180. function IndexOfResolver(const Filename: string): integer;
  181. function GetResolver(const Filename: string): TTestEnginePasResolver;
  182. function GetDefaultNamespace: string;
  183. property PasProgram: TPasProgram Read FPasProgram;
  184. property PasLibrary: TPasLibrary Read FPasLibrary;
  185. property Resolvers[Index: integer]: TTestEnginePasResolver read GetResolvers;
  186. property ResolverCount: integer read GetResolverCount;
  187. property Engine: TTestEnginePasResolver read FEngine;
  188. property Filename: string read FFilename;
  189. Property Module: TPasModule Read FModule;
  190. property FirstPasStatement: TPasImplBlock read FFirstPasStatement;
  191. property Converter: TPasToJSConverter read FConverter;
  192. property JSSource: TStringList read FJSSource;
  193. property JSModule: TJSSourceElements read FJSModule;
  194. property JSRegModuleCall: TJSCallExpression read FJSRegModuleCall;
  195. property JSModuleCallArgs: TJSArguments read FJSModuleCallArgs;
  196. property JSImplementationUses: TJSArrayLiteral read FJSImplementationUses;
  197. property JSInterfaceUses: TJSArrayLiteral read FJSInterfaceUses;
  198. property JSModuleSrc: TJSSourceElements read FJSModuleSrc;
  199. property JSInitBody: TJSFunctionBody read FJSInitBody;
  200. property ExpectedErrorClass: ExceptClass read FExpectedErrorClass write FExpectedErrorClass;
  201. property ExpectedErrorMsg: string read FExpectedErrorMsg write FExpectedErrorMsg;
  202. property ExpectedErrorNumber: integer read FExpectedErrorNumber write FExpectedErrorNumber;
  203. property SkipTests: boolean read FSkipTests write FSkipTests;
  204. public
  205. constructor Create; override;
  206. destructor Destroy; override;
  207. property Hub: TPas2JSResolverHub read FHub;
  208. property Source: TStringList read FSource;
  209. property FileResolver: TStreamResolver read FFileResolver;
  210. property Scanner: TPas2jsPasScanner read FScanner;
  211. property Parser: TTestPasParser read FParser;
  212. property MsgCount: integer read GetMsgCount;
  213. property Msgs[Index: integer]: TTestHintMessage read GetMsgs;
  214. property WithTypeInfo: boolean read FWithTypeInfo write SetWithTypeInfo;
  215. end;
  216. { TTestModule }
  217. TTestModule = class(TCustomTestModule)
  218. Published
  219. Procedure TestReservedWords;
  220. // program, units, includes
  221. Procedure TestEmptyProgram;
  222. Procedure TestEmptyProgramUseStrict;
  223. Procedure TestEmptyUnit;
  224. Procedure TestEmptyUnitUseStrict;
  225. Procedure TestDottedUnitNames;
  226. Procedure TestDottedUnitNameImpl;
  227. Procedure TestDottedUnitExpr;
  228. Procedure Test_ModeFPCFail;
  229. Procedure Test_ModeSwitchCBlocksFail;
  230. Procedure TestUnit_UseSystem;
  231. Procedure TestUnit_Intf1Impl2Intf1;
  232. Procedure TestIncludeVersion;
  233. // vars/const
  234. Procedure TestVarInt;
  235. Procedure TestVarBaseTypes;
  236. Procedure TestBaseTypeSingleFail;
  237. Procedure TestBaseTypeExtendedFail;
  238. Procedure TestConstBaseTypes;
  239. Procedure TestUnitImplVars;
  240. Procedure TestUnitImplConsts;
  241. Procedure TestUnitImplRecord;
  242. Procedure TestRenameJSNameConflict;
  243. Procedure TestLocalConst;
  244. Procedure TestVarExternal;
  245. Procedure TestVarExternalOtherUnit;
  246. Procedure TestVarAbsoluteFail;
  247. Procedure TestConstExternal;
  248. // numbers
  249. Procedure TestDouble;
  250. Procedure TestInteger;
  251. Procedure TestIntegerRange;
  252. Procedure TestIntegerTypecasts;
  253. Procedure TestInteger_BitwiseShrNativeInt;
  254. Procedure TestInteger_BitwiseShlNativeInt;
  255. Procedure TestInteger_SystemFunc;
  256. Procedure TestCurrency;
  257. Procedure TestForBoolDo;
  258. Procedure TestForIntDo;
  259. Procedure TestForIntInDo;
  260. // strings
  261. Procedure TestCharConst;
  262. Procedure TestChar_Compare;
  263. Procedure TestChar_BuiltInProcs;
  264. Procedure TestStringConst;
  265. Procedure TestStringConst_InvalidUTF16;
  266. Procedure TestStringConstSurrogate;
  267. Procedure TestString_Length;
  268. Procedure TestString_Compare;
  269. Procedure TestString_SetLength;
  270. Procedure TestString_CharAt;
  271. Procedure TestStringHMinusFail;
  272. Procedure TestStr;
  273. Procedure TestBaseType_AnsiStringFail;
  274. Procedure TestBaseType_WideStringFail;
  275. Procedure TestBaseType_ShortStringFail;
  276. Procedure TestBaseType_RawByteStringFail;
  277. Procedure TestTypeShortstring_Fail;
  278. Procedure TestCharSet_Custom;
  279. Procedure TestWideChar;
  280. Procedure TestForCharDo;
  281. Procedure TestForCharInDo;
  282. // alias types
  283. Procedure TestAliasTypeRef;
  284. Procedure TestTypeCast_BaseTypes;
  285. Procedure TestTypeCast_AliasBaseTypes;
  286. // functions
  287. Procedure TestEmptyProc;
  288. Procedure TestProcOneParam;
  289. Procedure TestFunctionWithoutParams;
  290. Procedure TestProcedureWithoutParams;
  291. Procedure TestPrgProcVar;
  292. Procedure TestProcTwoArgs;
  293. Procedure TestProc_DefaultValue;
  294. Procedure TestUnitProcVar;
  295. Procedure TestImplProc;
  296. Procedure TestFunctionResult;
  297. Procedure TestNestedProc;
  298. Procedure TestNestedProc_ResultString;
  299. Procedure TestForwardProc;
  300. Procedure TestNestedForwardProc;
  301. Procedure TestAssignFunctionResult;
  302. Procedure TestFunctionResultInCondition;
  303. Procedure TestFunctionResultInForLoop;
  304. Procedure TestFunctionResultInTypeCast;
  305. Procedure TestExit;
  306. Procedure TestExit_ResultInFinally;
  307. Procedure TestBreak;
  308. Procedure TestBreakAsVar;
  309. Procedure TestContinue;
  310. Procedure TestProc_External;
  311. Procedure TestProc_ExternalOtherUnit;
  312. Procedure TestProc_Asm;
  313. Procedure TestProc_AsmSubBlock;
  314. Procedure TestProc_Assembler;
  315. Procedure TestProc_VarParam;
  316. Procedure TestProc_VarParamString;
  317. Procedure TestProc_VarParamV;
  318. Procedure TestProc_Overload;
  319. Procedure TestProc_OverloadForward;
  320. Procedure TestProc_OverloadIntfImpl;
  321. Procedure TestProc_OverloadNested;
  322. Procedure TestProc_OverloadNestedForward;
  323. Procedure TestProc_OverloadUnitCycle;
  324. Procedure TestProc_Varargs;
  325. Procedure TestProc_ConstOrder;
  326. Procedure TestProc_DuplicateConst;
  327. Procedure TestProc_LocalVarAbsolute;
  328. Procedure TestProc_LocalVarInit;
  329. Procedure TestProc_ReservedWords;
  330. Procedure TestProc_ConstRefWord;
  331. // anonymous functions
  332. Procedure TestAnonymousProc_Assign_ObjFPC;
  333. Procedure TestAnonymousProc_Assign_Delphi;
  334. Procedure TestAnonymousProc_Arg;
  335. Procedure TestAnonymousProc_Typecast;
  336. Procedure TestAnonymousProc_With;
  337. Procedure TestAnonymousProc_ExceptOn;
  338. Procedure TestAnonymousProc_Nested;
  339. Procedure TestAnonymousProc_NestedAssignResult;
  340. Procedure TestAnonymousProc_Class;
  341. Procedure TestAnonymousProc_ForLoop;
  342. Procedure TestAnonymousProc_AsmDelphi;
  343. // enums, sets
  344. Procedure TestEnum_Name;
  345. Procedure TestEnum_Number;
  346. Procedure TestEnum_ConstFail;
  347. Procedure TestEnum_Functions;
  348. Procedure TestEnumRg_Functions;
  349. Procedure TestEnum_AsParams;
  350. Procedure TestEnumRange_Array;
  351. Procedure TestEnum_ForIn;
  352. Procedure TestEnum_ScopedNumber;
  353. Procedure TestEnum_InFunction;
  354. Procedure TestEnum_Name_Anonymous_Unit;
  355. Procedure TestSet_Enum;
  356. Procedure TestSet_Operators;
  357. Procedure TestSet_Operator_In;
  358. Procedure TestSet_Functions;
  359. Procedure TestSet_PassAsArgClone;
  360. Procedure TestSet_AsParams;
  361. Procedure TestSet_Property;
  362. Procedure TestSet_EnumConst;
  363. Procedure TestSet_IntConst;
  364. Procedure TestSet_IntRange;
  365. Procedure TestSet_AnonymousEnumType;
  366. Procedure TestSet_AnonymousEnumTypeChar; // ToDo
  367. Procedure TestSet_ConstEnum;
  368. Procedure TestSet_ConstChar;
  369. Procedure TestSet_ConstInt;
  370. Procedure TestSet_InFunction;
  371. Procedure TestSet_ForIn;
  372. // statements
  373. Procedure TestNestBegin;
  374. Procedure TestIncDec;
  375. Procedure TestLoHiFpcMode;
  376. Procedure TestLoHiDelphiMode;
  377. Procedure TestAssignments;
  378. Procedure TestArithmeticOperators1;
  379. Procedure TestMultiAdd;
  380. Procedure TestLogicalOperators;
  381. Procedure TestBitwiseOperators;
  382. Procedure TestBitwiseOperatorsLongword;
  383. Procedure TestFunctionInt;
  384. Procedure TestFunctionString;
  385. Procedure TestIfThen;
  386. Procedure TestForLoop;
  387. Procedure TestForLoopInsideFunction;
  388. Procedure TestForLoop_ReadVarAfter;
  389. Procedure TestForLoop_Nested;
  390. Procedure TestRepeatUntil;
  391. Procedure TestAsmBlock;
  392. Procedure TestAsmPas_Impl; // ToDo
  393. Procedure TestTryFinally;
  394. Procedure TestTryExcept;
  395. Procedure TestTryExcept_ReservedWords;
  396. Procedure TestIfThenRaiseElse;
  397. Procedure TestCaseOf;
  398. Procedure TestCaseOf_UseSwitch;
  399. Procedure TestCaseOfNoElse;
  400. Procedure TestCaseOfNoElse_UseSwitch;
  401. Procedure TestCaseOfRange;
  402. Procedure TestCaseOfString;
  403. Procedure TestCaseOfChar;
  404. Procedure TestCaseOfExternalClassConst;
  405. Procedure TestDebugger;
  406. // arrays
  407. Procedure TestArray_Dynamic;
  408. Procedure TestArray_Dynamic_Nil;
  409. Procedure TestArray_DynMultiDimensional;
  410. Procedure TestArray_DynamicAssign;
  411. Procedure TestArray_StaticInt;
  412. Procedure TestArray_StaticBool;
  413. Procedure TestArray_StaticChar;
  414. Procedure TestArray_StaticMultiDim;
  415. Procedure TestArray_StaticInFunction;
  416. Procedure TestArray_StaticMultiDimEqualNotImplemented;
  417. Procedure TestArrayOfRecord;
  418. Procedure TestArray_StaticRecord;
  419. Procedure TestArrayOfSet;
  420. Procedure TestArray_DynAsParam;
  421. Procedure TestArray_StaticAsParam;
  422. Procedure TestArrayElement_AsParams;
  423. Procedure TestArrayElementFromFuncResult_AsParams;
  424. Procedure TestArrayEnumTypeRange;
  425. Procedure TestArray_SetLengthOutArg;
  426. Procedure TestArray_SetLengthProperty;
  427. Procedure TestArray_SetLengthMultiDim;
  428. Procedure TestArray_SetLengthDynOfStatic;
  429. Procedure TestArray_OpenArrayOfString;
  430. Procedure TestArray_ArrayOfCharAssignString; // ToDo
  431. Procedure TestArray_ConstRef;
  432. Procedure TestArray_Concat;
  433. Procedure TestArray_Copy;
  434. Procedure TestArray_InsertDelete;
  435. Procedure TestArray_DynArrayConstObjFPC;
  436. Procedure TestArray_DynArrayConstDelphi;
  437. Procedure TestArray_ArrayLitAsParam;
  438. Procedure TestArray_ArrayLitMultiDimAsParam;
  439. Procedure TestArray_ArrayLitStaticAsParam;
  440. Procedure TestArray_ForInArrOfString;
  441. Procedure TestExternalClass_TypeCastArrayToExternalClass;
  442. Procedure TestExternalClass_TypeCastArrayFromExternalClass;
  443. Procedure TestArrayOfConst_TVarRec;
  444. Procedure TestArrayOfConst_PassBaseTypes;
  445. Procedure TestArrayOfConst_PassObj;
  446. // record
  447. Procedure TestRecord_Empty;
  448. Procedure TestRecord_Var;
  449. Procedure TestRecord_VarExternal;
  450. Procedure TestRecord_WithDo;
  451. Procedure TestRecord_Assign;
  452. Procedure TestRecord_AsParams;
  453. Procedure TestRecord_ConstRef;
  454. Procedure TestRecordElement_AsParams;
  455. Procedure TestRecordElementFromFuncResult_AsParams;
  456. Procedure TestRecordElementFromWith_AsParams;
  457. Procedure TestRecord_Equal;
  458. Procedure TestRecord_JSValue;
  459. Procedure TestRecord_VariantFail;
  460. Procedure TestRecord_FieldArray;
  461. Procedure TestRecord_Const;
  462. Procedure TestRecord_TypecastFail;
  463. Procedure TestRecord_InFunction;
  464. Procedure TestRecord_AnonymousFail;
  465. // advanced record
  466. Procedure TestAdvRecord_Function;
  467. Procedure TestAdvRecord_Property;
  468. Procedure TestAdvRecord_PropertyDefault;
  469. Procedure TestAdvRecord_Property_ClassMethod;
  470. Procedure TestAdvRecord_Const;
  471. Procedure TestAdvRecord_ExternalField;
  472. Procedure TestAdvRecord_SubRecord;
  473. Procedure TestAdvRecord_SubClass;
  474. Procedure TestAdvRecord_SubInterfaceFail;
  475. Procedure TestAdvRecord_Constructor;
  476. Procedure TestAdvRecord_ClassConstructor_Program;
  477. Procedure TestAdvRecord_ClassConstructor_Unit;
  478. // classes
  479. Procedure TestClass_TObjectDefaultConstructor;
  480. Procedure TestClass_TObjectConstructorWithParams;
  481. Procedure TestClass_TObjectConstructorWithDefaultParam;
  482. Procedure TestClass_Var;
  483. Procedure TestClass_Method;
  484. Procedure TestClass_Implementation;
  485. Procedure TestClass_Inheritance;
  486. Procedure TestClass_TypeAlias;
  487. Procedure TestClass_AbstractMethod;
  488. Procedure TestClass_CallInherited_ProcNoParams;
  489. Procedure TestClass_CallInherited_WithParams;
  490. Procedure TestClasS_CallInheritedConstructor;
  491. Procedure TestClass_ClassVar_Assign;
  492. Procedure TestClass_CallClassMethod;
  493. Procedure TestClass_CallClassMethodStatic; // ToDo
  494. Procedure TestClass_Property;
  495. Procedure TestClass_Property_ClassMethod;
  496. Procedure TestClass_Property_Indexed;
  497. Procedure TestClass_Property_IndexSpec;
  498. Procedure TestClass_PropertyOfTypeArray;
  499. Procedure TestClass_PropertyDefault;
  500. Procedure TestClass_PropertyDefault_TypecastToOtherDefault;
  501. //Procedure TestClass_PropertyDefault;
  502. Procedure TestClass_PropertyOverride;
  503. Procedure TestClass_PropertyIncVisibility;
  504. Procedure TestClass_Assigned;
  505. Procedure TestClass_WithClassDoCreate;
  506. Procedure TestClass_WithClassInstDoProperty;
  507. Procedure TestClass_WithClassInstDoPropertyWithParams;
  508. Procedure TestClass_WithClassInstDoFunc;
  509. Procedure TestClass_TypeCast;
  510. Procedure TestClass_TypeCastUntypedParam;
  511. Procedure TestClass_Overloads;
  512. Procedure TestClass_OverloadsAncestor;
  513. Procedure TestClass_OverloadConstructor;
  514. Procedure TestClass_OverloadDelphiOverride;
  515. Procedure TestClass_ReintroduceVarDelphi;
  516. Procedure TestClass_ReintroducedVar;
  517. Procedure TestClass_RaiseDescendant;
  518. Procedure TestClass_ExternalMethod;
  519. Procedure TestClass_ExternalVirtualNameMismatchFail;
  520. Procedure TestClass_ExternalOverrideFail;
  521. Procedure TestClass_ExternalVar;
  522. Procedure TestClass_Const;
  523. Procedure TestClass_ConstEnum;
  524. Procedure TestClass_LocalConstDuplicate_Prg;
  525. Procedure TestClass_LocalConstDuplicate_Unit;
  526. // ToDo: Procedure TestAdvRecord_LocalConstDuplicate;
  527. Procedure TestClass_LocalVarSelfFail;
  528. Procedure TestClass_ArgSelfFail;
  529. Procedure TestClass_NestedProcSelf;
  530. Procedure TestClass_NestedProcSelf2;
  531. Procedure TestClass_NestedProcClassSelf;
  532. Procedure TestClass_NestedProcCallInherited;
  533. Procedure TestClass_TObjectFree;
  534. Procedure TestClass_TObjectFree_VarArg;
  535. Procedure TestClass_TObjectFreeNewInstance;
  536. Procedure TestClass_TObjectFreeLowerCase;
  537. Procedure TestClass_TObjectFreeFunctionFail;
  538. Procedure TestClass_TObjectFreePropertyFail;
  539. Procedure TestClass_ForIn;
  540. Procedure TestClass_DispatchMessage;
  541. Procedure TestClass_Message_DuplicateIntFail;
  542. Procedure TestClass_DispatchMessage_WrongFieldNameFail;
  543. // class of
  544. Procedure TestClassOf_Create;
  545. Procedure TestClassOf_Call;
  546. Procedure TestClassOf_Assign;
  547. Procedure TestClassOf_Is;
  548. Procedure TestClassOf_Compare;
  549. Procedure TestClassOf_ClassVar;
  550. Procedure TestClassOf_ClassMethod;
  551. Procedure TestClassOf_ClassProperty;
  552. Procedure TestClassOf_ClassMethodSelf;
  553. Procedure TestClassOf_TypeCast;
  554. Procedure TestClassOf_ImplicitFunctionCall;
  555. Procedure TestClassOf_Const;
  556. // nested class
  557. Procedure TestNestedClass_Alias;
  558. Procedure TestNestedClass_Record;
  559. Procedure TestNestedClass_Class;
  560. // external class
  561. Procedure TestExternalClass_Var;
  562. Procedure TestExternalClass_Const;
  563. Procedure TestExternalClass_Dollar;
  564. Procedure TestExternalClass_DuplicateVarFail;
  565. Procedure TestExternalClass_Method;
  566. Procedure TestExternalClass_ClassMethod;
  567. Procedure TestExternalClass_ClassMethodStatic;
  568. Procedure TestExternalClass_FunctionResultInTypeCast;
  569. Procedure TestExternalClass_NonExternalOverride;
  570. Procedure TestExternalClass_OverloadHint;
  571. Procedure TestExternalClass_SameNamePublishedProperty;
  572. Procedure TestExternalClass_Property;
  573. Procedure TestExternalClass_PropertyDate;
  574. Procedure TestExternalClass_ClassProperty;
  575. Procedure TestExternalClass_ClassOf;
  576. Procedure TestExternalClass_ClassOtherUnit;
  577. Procedure TestExternalClass_Is;
  578. Procedure TestExternalClass_As;
  579. Procedure TestExternalClass_DestructorFail;
  580. Procedure TestExternalClass_New;
  581. Procedure TestExternalClass_ClassOf_New;
  582. Procedure TestExternalClass_FuncClassOf_New;
  583. Procedure TestExternalClass_New_PasClassFail;
  584. Procedure TestExternalClass_New_PasClassBracketsFail;
  585. Procedure TestExternalClass_NewExtName;
  586. Procedure TestExternalClass_Constructor;
  587. Procedure TestExternalClass_ConstructorBrackets;
  588. Procedure TestExternalClass_LocalConstSameName;
  589. Procedure TestExternalClass_ReintroduceOverload;
  590. Procedure TestExternalClass_Inherited;
  591. Procedure TestExternalClass_PascalAncestorFail;
  592. Procedure TestExternalClass_NewInstance;
  593. Procedure TestExternalClass_NewInstance_NonVirtualFail;
  594. Procedure TestExternalClass_NewInstance_FirstParamNotString_Fail;
  595. Procedure TestExternalClass_NewInstance_SecondParamTyped_Fail;
  596. Procedure TestExternalClass_JSFunctionPasDescendant;
  597. Procedure TestExternalClass_PascalProperty;
  598. Procedure TestExternalClass_TypeCastToRootClass;
  599. Procedure TestExternalClass_TypeCastToJSObject;
  600. Procedure TestExternalClass_TypeCastStringToExternalString;
  601. Procedure TestExternalClass_TypeCastToJSFunction;
  602. Procedure TestExternalClass_TypeCastDelphiUnrelated;
  603. Procedure TestExternalClass_CallClassFunctionOfInstanceFail;
  604. Procedure TestExternalClass_BracketAccessor;
  605. Procedure TestExternalClass_BracketAccessor_Call;
  606. Procedure TestExternalClass_BracketAccessor_2ParamsFail;
  607. Procedure TestExternalClass_BracketAccessor_ReadOnly;
  608. Procedure TestExternalClass_BracketAccessor_WriteOnly;
  609. Procedure TestExternalClass_BracketAccessor_MultiType;
  610. Procedure TestExternalClass_BracketAccessor_Index;
  611. Procedure TestExternalClass_ForInJSObject;
  612. Procedure TestExternalClass_ForInJSArray;
  613. Procedure TestExternalClass_IncompatibleArgDuplicateIdentifier;
  614. // class interfaces
  615. Procedure TestClassInterface_Corba;
  616. Procedure TestClassInterface_ProcExternalFail;
  617. Procedure TestClassInterface_Overloads;
  618. Procedure TestClassInterface_DuplicateGUIInIntfListFail;
  619. Procedure TestClassInterface_DuplicateGUIInAncestorFail;
  620. Procedure TestClassInterface_AncestorImpl;
  621. Procedure TestClassInterface_ImplReintroduce;
  622. Procedure TestClassInterface_MethodResolution;
  623. Procedure TestClassInterface_AncestorMoreInterfaces;
  624. Procedure TestClassInterface_MethodOverride;
  625. Procedure TestClassInterface_Corba_Delegation;
  626. Procedure TestClassInterface_Corba_DelegationStatic;
  627. Procedure TestClassInterface_Corba_Operators;
  628. Procedure TestClassInterface_Corba_Args;
  629. Procedure TestClassInterface_Corba_ForIn;
  630. Procedure TestClassInterface_COM_AssignVar;
  631. Procedure TestClassInterface_COM_AssignArg;
  632. Procedure TestClassInterface_COM_FunctionResult;
  633. Procedure TestClassInterface_COM_InheritedFuncResult;
  634. Procedure TestClassInterface_COM_IsAsTypeCasts;
  635. Procedure TestClassInterface_COM_PassAsArg;
  636. Procedure TestClassInterface_COM_PassToUntypedParam;
  637. Procedure TestClassInterface_COM_FunctionInExpr;
  638. Procedure TestClassInterface_COM_Property;
  639. Procedure TestClassInterface_COM_IntfProperty;
  640. Procedure TestClassInterface_COM_Delegation;
  641. Procedure TestClassInterface_COM_With;
  642. Procedure TestClassInterface_COM_ForIn;
  643. Procedure TestClassInterface_COM_ArrayOfIntfFail;
  644. Procedure TestClassInterface_COM_RecordIntfFail;
  645. Procedure TestClassInterface_COM_UnitInitialization;
  646. Procedure TestClassInterface_GUID;
  647. Procedure TestClassInterface_GUIDProperty;
  648. // helpers
  649. Procedure TestClassHelper_ClassVar;
  650. Procedure TestClassHelper_Method_AccessInstanceFields;
  651. Procedure TestClassHelper_Method_Call;
  652. Procedure TestClassHelper_Method_Nested_Call;
  653. Procedure TestClassHelper_ClassMethod_Call;
  654. Procedure TestClassHelper_ClassOf;
  655. Procedure TestClassHelper_MethodRefObjFPC;
  656. Procedure TestClassHelper_Constructor;
  657. Procedure TestClassHelper_InheritedObjFPC;
  658. Procedure TestClassHelper_Property;
  659. Procedure TestClassHelper_Property_Array;
  660. Procedure TestClassHelper_Property_Array_Default;
  661. Procedure TestClassHelper_Property_Array_DefaultDefault;
  662. Procedure TestClassHelper_ClassProperty;
  663. Procedure TestClassHelper_ClassPropertyStatic;
  664. Procedure TestClassHelper_ClassProperty_Array;
  665. Procedure TestClassHelper_ForIn;
  666. Procedure TestClassHelper_PassProperty;
  667. Procedure TestExtClassHelper_ClassVar;
  668. Procedure TestExtClassHelper_Method_Call;
  669. Procedure TestExtClassHelper_ClassMethod_MissingStatic;
  670. Procedure TestRecordHelper_ClassVar;
  671. Procedure TestRecordHelper_Method_Call;
  672. Procedure TestRecordHelper_Constructor;
  673. Procedure TestTypeHelper_ClassVar;
  674. Procedure TestTypeHelper_PassResultElement;
  675. Procedure TestTypeHelper_PassArgs;
  676. Procedure TestTypeHelper_PassVarConst;
  677. Procedure TestTypeHelper_PassFuncResult;
  678. Procedure TestTypeHelper_PassPropertyField;
  679. Procedure TestTypeHelper_PassPropertyGetter;
  680. Procedure TestTypeHelper_PassClassPropertyField;
  681. Procedure TestTypeHelper_PassClassPropertyGetterStatic;
  682. Procedure TestTypeHelper_PassClassPropertyGetterNonStatic;
  683. Procedure TestTypeHelper_Property;
  684. Procedure TestTypeHelper_Property_Array;
  685. Procedure TestTypeHelper_ClassProperty;
  686. Procedure TestTypeHelper_ClassProperty_Array;
  687. Procedure TestTypeHelper_ClassMethod;
  688. Procedure TestTypeHelper_ExtClassMethodFail;
  689. Procedure TestTypeHelper_Constructor;
  690. Procedure TestTypeHelper_Word;
  691. Procedure TestTypeHelper_Boolean;
  692. Procedure TestTypeHelper_WordBool;
  693. Procedure TestTypeHelper_Double;
  694. Procedure TestTypeHelper_NativeInt;
  695. Procedure TestTypeHelper_StringChar;
  696. Procedure TestTypeHelper_JSValue;
  697. Procedure TestTypeHelper_Array;
  698. Procedure TestTypeHelper_EnumType;
  699. Procedure TestTypeHelper_SetType;
  700. Procedure TestTypeHelper_InterfaceType;
  701. Procedure TestTypeHelper_NestedSelf;
  702. // proc types
  703. Procedure TestProcType;
  704. Procedure TestProcType_Arg;
  705. Procedure TestProcType_FunctionFPC;
  706. Procedure TestProcType_FunctionDelphi;
  707. Procedure TestProcType_ProcedureDelphi;
  708. Procedure TestProcType_AsParam;
  709. Procedure TestProcType_MethodFPC;
  710. Procedure TestProcType_MethodDelphi;
  711. Procedure TestProcType_PropertyFPC;
  712. Procedure TestProcType_PropertyDelphi;
  713. Procedure TestProcType_WithClassInstDoPropertyFPC;
  714. Procedure TestProcType_Nested;
  715. Procedure TestProcType_NestedOfObject;
  716. Procedure TestProcType_ReferenceToProc;
  717. Procedure TestProcType_ReferenceToMethod;
  718. Procedure TestProcType_Typecast;
  719. Procedure TestProcType_PassProcToUntyped;
  720. Procedure TestProcType_PassProcToArray;
  721. Procedure TestProcType_SafeCallObjFPC;
  722. Procedure TestProcType_SafeCallDelphi;
  723. // pointer
  724. Procedure TestPointer;
  725. Procedure TestPointer_Proc;
  726. Procedure TestPointer_AssignRecordFail;
  727. Procedure TestPointer_AssignStaticArrayFail;
  728. Procedure TestPointer_TypeCastJSValueToPointer;
  729. Procedure TestPointer_NonRecordFail;
  730. Procedure TestPointer_AnonymousArgTypeFail;
  731. Procedure TestPointer_AnonymousVarTypeFail;
  732. Procedure TestPointer_AnonymousResultTypeFail;
  733. Procedure TestPointer_AddrOperatorFail;
  734. Procedure TestPointer_ArrayParamsFail;
  735. Procedure TestPointer_PointerAddFail;
  736. Procedure TestPointer_IncPointerFail;
  737. Procedure TestPointer_Record;
  738. Procedure TestPointer_RecordArg;
  739. // jsvalue
  740. Procedure TestJSValue_AssignToJSValue;
  741. Procedure TestJSValue_TypeCastToBaseType;
  742. Procedure TestJSValue_TypecastToJSValue;
  743. Procedure TestJSValue_Equal;
  744. Procedure TestJSValue_If;
  745. Procedure TestJSValue_Not;
  746. Procedure TestJSValue_Enum;
  747. Procedure TestJSValue_ClassInstance;
  748. Procedure TestJSValue_ClassOf;
  749. Procedure TestJSValue_ArrayOfJSValue;
  750. Procedure TestJSValue_ArrayLit;
  751. Procedure TestJSValue_Params;
  752. Procedure TestJSValue_UntypedParam;
  753. Procedure TestJSValue_FuncResultType;
  754. Procedure TestJSValue_ProcType_Assign;
  755. Procedure TestJSValue_ProcType_Equal;
  756. Procedure TestJSValue_ProcType_Param;
  757. Procedure TestJSValue_AssignToPointerFail;
  758. Procedure TestJSValue_OverloadDouble;
  759. Procedure TestJSValue_OverloadNativeInt;
  760. Procedure TestJSValue_OverloadWord;
  761. Procedure TestJSValue_OverloadString;
  762. Procedure TestJSValue_OverloadChar;
  763. Procedure TestJSValue_OverloadPointer;
  764. Procedure TestJSValue_ForIn;
  765. // RTTI
  766. Procedure TestRTTI_IntRange;
  767. Procedure TestRTTI_Double;
  768. Procedure TestRTTI_ProcType;
  769. Procedure TestRTTI_ProcType_ArgFromOtherUnit;
  770. Procedure TestRTTI_EnumAndSetType;
  771. Procedure TestRTTI_EnumRange;
  772. Procedure TestRTTI_AnonymousEnumType;
  773. Procedure TestRTTI_StaticArray;
  774. Procedure TestRTTI_DynArray;
  775. Procedure TestRTTI_ArrayNestedAnonymous;
  776. Procedure TestRTTI_PublishedMethodOverloadFail;
  777. Procedure TestRTTI_PublishedMethodHideNoHint;
  778. Procedure TestRTTI_PublishedMethodExternalFail;
  779. Procedure TestRTTI_PublishedClassPropertyFail;
  780. Procedure TestRTTI_PublishedClassFieldFail;
  781. Procedure TestRTTI_PublishedFieldExternalFail;
  782. Procedure TestRTTI_Class_Field;
  783. Procedure TestRTTI_Class_Method;
  784. Procedure TestRTTI_Class_MethodArgFlags;
  785. Procedure TestRTTI_Class_Property;
  786. Procedure TestRTTI_Class_PropertyParams;
  787. Procedure TestRTTI_Class_OtherUnit_TypeAlias;
  788. Procedure TestRTTI_Class_OmitRTTI;
  789. Procedure TestRTTI_Class_Field_AnonymousArrayOfSelfClass;
  790. Procedure TestRTTI_IndexModifier;
  791. Procedure TestRTTI_StoredModifier;
  792. Procedure TestRTTI_DefaultValue;
  793. Procedure TestRTTI_DefaultValueSet;
  794. Procedure TestRTTI_DefaultValueRangeType;
  795. Procedure TestRTTI_DefaultValueInherit;
  796. Procedure TestRTTI_OverrideMethod;
  797. Procedure TestRTTI_ReintroduceMethod;
  798. Procedure TestRTTI_OverloadProperty;
  799. // ToDo: array argument
  800. Procedure TestRTTI_ClassForward;
  801. Procedure TestRTTI_ClassOf;
  802. Procedure TestRTTI_Record;
  803. Procedure TestRTTI_RecordAnonymousArray;
  804. Procedure TestRTTI_Record_ClassVarType;
  805. Procedure TestRTTI_LocalTypes;
  806. Procedure TestRTTI_TypeInfo_BaseTypes;
  807. Procedure TestRTTI_TypeInfo_Type_BaseTypes;
  808. Procedure TestRTTI_TypeInfo_LocalFail;
  809. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses1;
  810. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses2;
  811. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses3;
  812. Procedure TestRTTI_TypeInfo_FunctionClassType;
  813. Procedure TestRTTI_TypeInfo_MixedUnits_PointerAndClass;
  814. Procedure TestRTTI_Interface_Corba;
  815. Procedure TestRTTI_Interface_COM;
  816. Procedure TestRTTI_ClassHelper;
  817. Procedure TestRTTI_ExternalClass;
  818. Procedure TestRTTI_Unit;
  819. // Resourcestring
  820. Procedure TestResourcestringProgram;
  821. Procedure TestResourcestringUnit;
  822. Procedure TestResourcestringImplementation;
  823. // Attributes
  824. Procedure TestAttributes_Members;
  825. Procedure TestAttributes_Types;
  826. Procedure TestAttributes_HelperConstructor_Fail;
  827. // Assertions, checks
  828. procedure TestAssert;
  829. procedure TestAssert_SysUtils;
  830. procedure TestObjectChecks;
  831. procedure TestOverflowChecks_Int;
  832. procedure TestRangeChecks_AssignInt;
  833. procedure TestRangeChecks_AssignIntRange;
  834. procedure TestRangeChecks_AssignEnum;
  835. procedure TestRangeChecks_AssignEnumRange;
  836. procedure TestRangeChecks_AssignChar;
  837. procedure TestRangeChecks_AssignCharRange;
  838. procedure TestRangeChecks_ArrayIndex;
  839. procedure TestRangeChecks_ArrayOfRecIndex;
  840. procedure TestRangeChecks_StringIndex;
  841. procedure TestRangeChecks_TypecastInt;
  842. procedure TestRangeChecks_TypeHelperInt;
  843. // Async/AWait
  844. Procedure TestAsync_Proc;
  845. Procedure TestAsync_CallResultIsPromise;
  846. Procedure TestAsync_ConstructorFail;
  847. Procedure TestAsync_PropertyGetterFail;
  848. Procedure TestAwait_NonPromiseWithTypeFail;
  849. Procedure TestAwait_AsyncCallTypeMismatch;
  850. Procedure TestAWait_OutsideAsyncFail;
  851. Procedure TestAWait_IntegerFail;
  852. Procedure TestAWait_ExternalClassPromise;
  853. Procedure TestAWait_JSValue;
  854. Procedure TestAWait_Result;
  855. Procedure TestAWait_ResultPromiseMissingTypeFail; // await(AsyncCallResultPromise) needs T
  856. Procedure TestAsync_AnonymousProc;
  857. Procedure TestAsync_ProcType;
  858. Procedure TestAsync_ProcTypeAsyncModMismatchFail;
  859. Procedure TestAsync_Inherited;
  860. Procedure TestAsync_ClassInterface;
  861. Procedure TestAsync_ClassInterface_AsyncMissmatchFail;
  862. // Library
  863. Procedure TestLibrary_Empty;
  864. Procedure TestLibrary_ExportFunc; // ToDo
  865. // ToDo: test delayed specialization init
  866. // ToDO: analyzer
  867. end;
  868. function LinesToStr(Args: array of const): string;
  869. function ExtractFileUnitName(aFilename: string): string;
  870. function JSToStr(El: TJSElement): string;
  871. function CheckSrcDiff(Expected, Actual: string; out Msg: string): boolean;
  872. implementation
  873. function LinesToStr(Args: array of const): string;
  874. var
  875. s: String;
  876. i: Integer;
  877. begin
  878. s:='';
  879. for i:=Low(Args) to High(Args) do
  880. case Args[i].VType of
  881. vtChar: s += Args[i].VChar+LineEnding;
  882. vtString: s += Args[i].VString^+LineEnding;
  883. vtPChar: s += Args[i].VPChar+LineEnding;
  884. vtWideChar: s += AnsiString(Args[i].VWideChar)+LineEnding;
  885. vtPWideChar: s += AnsiString(Args[i].VPWideChar)+LineEnding;
  886. vtAnsiString: s += AnsiString(Args[i].VAnsiString)+LineEnding;
  887. vtWidestring: s += AnsiString(WideString(Args[i].VWideString))+LineEnding;
  888. vtUnicodeString:s += AnsiString(UnicodeString(Args[i].VUnicodeString))+LineEnding;
  889. end;
  890. Result:=s;
  891. end;
  892. function ExtractFileUnitName(aFilename: string): string;
  893. var
  894. p: Integer;
  895. begin
  896. Result:=ExtractFileName(aFilename);
  897. if Result='' then exit;
  898. for p:=length(Result) downto 1 do
  899. case Result[p] of
  900. '/','\': exit;
  901. '.':
  902. begin
  903. Delete(Result,p,length(Result));
  904. exit;
  905. end;
  906. end;
  907. end;
  908. function JSToStr(El: TJSElement): string;
  909. var
  910. aWriter: TBufferWriter;
  911. aJSWriter: TJSWriter;
  912. begin
  913. aJSWriter:=nil;
  914. aWriter:=TBufferWriter.Create(1000);
  915. try
  916. aJSWriter:=TJSWriter.Create(aWriter);
  917. aJSWriter.IndentSize:=2;
  918. aJSWriter.WriteJS(El);
  919. Result:=aWriter.AsString;
  920. finally
  921. aJSWriter.Free;
  922. aWriter.Free;
  923. end;
  924. end;
  925. function CheckSrcDiff(Expected, Actual: string; out Msg: string): boolean;
  926. // search diff, ignore changes in spaces
  927. const
  928. SpaceChars = [#9,#10,#13,' '];
  929. var
  930. ExpectedP, ActualP: PChar;
  931. function FindLineEnd(p: PChar): PChar;
  932. begin
  933. Result:=p;
  934. while not (Result^ in [#0,#10,#13]) do inc(Result);
  935. end;
  936. function FindLineStart(p, MinP: PChar): PChar;
  937. begin
  938. while (p>MinP) and not (p[-1] in [#10,#13]) do dec(p);
  939. Result:=p;
  940. end;
  941. procedure SkipLineEnd(var p: PChar);
  942. begin
  943. if p^ in [#10,#13] then
  944. begin
  945. if (p[1] in [#10,#13]) and (p^<>p[1]) then
  946. inc(p,2)
  947. else
  948. inc(p);
  949. end;
  950. end;
  951. function HasSpecialChar(s: string): boolean;
  952. var
  953. i: Integer;
  954. begin
  955. for i:=1 to length(s) do
  956. if s[i] in [#0..#31,#127..#255] then
  957. exit(true);
  958. Result:=false;
  959. end;
  960. function HashSpecialChars(s: string): string;
  961. var
  962. i: Integer;
  963. begin
  964. Result:='';
  965. for i:=1 to length(s) do
  966. if s[i] in [#0..#31,#127..#255] then
  967. Result:=Result+'#'+hexstr(ord(s[i]),2)
  968. else
  969. Result:=Result+s[i];
  970. end;
  971. procedure DiffFound;
  972. var
  973. ActLineStartP, ActLineEndP, p, StartPos: PChar;
  974. ExpLine, ActLine: String;
  975. i, LineNo, DiffLineNo: Integer;
  976. begin
  977. writeln('Diff found "',Msg,'". Lines:');
  978. // write correct lines
  979. p:=PChar(Expected);
  980. LineNo:=0;
  981. DiffLineNo:=0;
  982. repeat
  983. StartPos:=p;
  984. while not (p^ in [#0,#10,#13]) do inc(p);
  985. ExpLine:=copy(Expected,StartPos-PChar(Expected)+1,p-StartPos);
  986. SkipLineEnd(p);
  987. inc(LineNo);
  988. if (p<=ExpectedP) and (p^<>#0) then
  989. begin
  990. writeln('= ',ExpLine);
  991. end else begin
  992. // diff line
  993. if DiffLineNo=0 then DiffLineNo:=LineNo;
  994. // write actual line
  995. ActLineStartP:=FindLineStart(ActualP,PChar(Actual));
  996. ActLineEndP:=FindLineEnd(ActualP);
  997. ActLine:=copy(Actual,ActLineStartP-PChar(Actual)+1,ActLineEndP-ActLineStartP);
  998. writeln('- ',ActLine);
  999. if HasSpecialChar(ActLine) then
  1000. writeln('- ',HashSpecialChars(ActLine));
  1001. // write expected line
  1002. writeln('+ ',ExpLine);
  1003. if HasSpecialChar(ExpLine) then
  1004. writeln('- ',HashSpecialChars(ExpLine));
  1005. // write empty line with pointer ^
  1006. for i:=1 to 2+ExpectedP-StartPos do write(' ');
  1007. writeln('^');
  1008. Msg:='expected "'+ExpLine+'", but got "'+ActLine+'".';
  1009. CheckSrcDiff:=false;
  1010. // write up to three following actual lines to get some context
  1011. for i:=1 to 3 do begin
  1012. ActLineStartP:=ActLineEndP;
  1013. SkipLineEnd(ActLineStartP);
  1014. if ActLineStartP^=#0 then break;
  1015. ActLineEndP:=FindLineEnd(ActLineStartP);
  1016. ActLine:=copy(Actual,ActLineStartP-PChar(Actual)+1,ActLineEndP-ActLineStartP);
  1017. writeln('~ ',ActLine);
  1018. end;
  1019. exit;
  1020. end;
  1021. until p^=#0;
  1022. writeln('DiffFound Actual:-----------------------');
  1023. writeln(Actual);
  1024. writeln('DiffFound Expected:---------------------');
  1025. writeln(Expected);
  1026. writeln('DiffFound ------------------------------');
  1027. Msg:='diff found, but lines are the same, internal error';
  1028. CheckSrcDiff:=false;
  1029. end;
  1030. var
  1031. IsSpaceNeeded: Boolean;
  1032. LastChar, Quote: Char;
  1033. begin
  1034. Result:=true;
  1035. Msg:='';
  1036. if Expected='' then Expected:=' ';
  1037. if Actual='' then Actual:=' ';
  1038. ExpectedP:=PChar(Expected);
  1039. ActualP:=PChar(Actual);
  1040. repeat
  1041. //writeln('TTestModule.CheckDiff Exp="',ExpectedP^,'" Act="',ActualP^,'"');
  1042. case ExpectedP^ of
  1043. #0:
  1044. begin
  1045. // check that rest of Actual has only spaces
  1046. while ActualP^ in SpaceChars do inc(ActualP);
  1047. if ActualP^<>#0 then
  1048. begin
  1049. DiffFound;
  1050. exit;
  1051. end;
  1052. exit(true);
  1053. end;
  1054. ' ',#9,#10,#13:
  1055. begin
  1056. // skip space in Expected
  1057. IsSpaceNeeded:=false;
  1058. if ExpectedP>PChar(Expected) then
  1059. LastChar:=ExpectedP[-1]
  1060. else
  1061. LastChar:=#0;
  1062. while ExpectedP^ in SpaceChars do inc(ExpectedP);
  1063. if (LastChar in ['a'..'z','A'..'Z','0'..'9','_','$'])
  1064. and (ExpectedP^ in ['a'..'z','A'..'Z','0'..'9','_','$']) then
  1065. IsSpaceNeeded:=true;
  1066. if IsSpaceNeeded and (not (ActualP^ in SpaceChars)) then
  1067. begin
  1068. DiffFound;
  1069. exit;
  1070. end;
  1071. while ActualP^ in SpaceChars do inc(ActualP);
  1072. end;
  1073. '''','"':
  1074. begin
  1075. while ActualP^ in SpaceChars do inc(ActualP);
  1076. if ExpectedP^<>ActualP^ then
  1077. begin
  1078. DiffFound;
  1079. exit;
  1080. end;
  1081. Quote:=ExpectedP^;
  1082. repeat
  1083. inc(ExpectedP);
  1084. inc(ActualP);
  1085. if ExpectedP^<>ActualP^ then
  1086. begin
  1087. DiffFound;
  1088. exit;
  1089. end;
  1090. if (ExpectedP^ in [#0,#10,#13]) then
  1091. break
  1092. else if (ExpectedP^=Quote) then
  1093. begin
  1094. inc(ExpectedP);
  1095. inc(ActualP);
  1096. break;
  1097. end;
  1098. until false;
  1099. end;
  1100. else
  1101. while ActualP^ in SpaceChars do inc(ActualP);
  1102. if ExpectedP^<>ActualP^ then
  1103. begin
  1104. DiffFound;
  1105. exit;
  1106. end;
  1107. inc(ExpectedP);
  1108. inc(ActualP);
  1109. end;
  1110. until false;
  1111. end;
  1112. { TTestEnginePasResolver }
  1113. destructor TTestEnginePasResolver.Destroy;
  1114. begin
  1115. FreeAndNil(FStreamResolver);
  1116. FreeAndNil(FParser);
  1117. FreeAndNil(FScanner);
  1118. FreeAndNil(FStreamResolver);
  1119. if Module<>nil then
  1120. begin
  1121. Module.Release{$IFDEF CheckPasTreeRefCount}('CreateElement'){$ENDIF};
  1122. FModule:=nil;
  1123. end;
  1124. inherited Destroy;
  1125. end;
  1126. function TTestEnginePasResolver.FindUnit(const AName, InFilename: String;
  1127. NameExpr, InFileExpr: TPasExpr): TPasModule;
  1128. begin
  1129. Result:=nil;
  1130. if InFilename<>'' then
  1131. RaiseNotYetImplemented(20180224101926,InFileExpr,'Use testcase tcunitsearch instead');
  1132. if Assigned(OnFindUnit) then
  1133. Result:=OnFindUnit(AName);
  1134. if NameExpr=nil then ;
  1135. end;
  1136. procedure TTestEnginePasResolver.UsedInterfacesFinished(Section: TPasSection);
  1137. begin
  1138. // do not parse recursively
  1139. // parse via the queue
  1140. if Section=nil then ;
  1141. end;
  1142. { TCustomTestModule }
  1143. function TCustomTestModule.GetMsgCount: integer;
  1144. begin
  1145. Result:=FHintMsgs.Count;
  1146. end;
  1147. function TCustomTestModule.GetMsgs(Index: integer): TTestHintMessage;
  1148. begin
  1149. Result:=TTestHintMessage(FHintMsgs[Index]);
  1150. end;
  1151. function TCustomTestModule.GetResolverCount: integer;
  1152. begin
  1153. Result:=FModules.Count;
  1154. end;
  1155. function TCustomTestModule.GetResolvers(Index: integer
  1156. ): TTestEnginePasResolver;
  1157. begin
  1158. Result:=TTestEnginePasResolver(FModules[Index]);
  1159. end;
  1160. function TCustomTestModule.OnPasResolverFindUnit(const aUnitName: String
  1161. ): TPasModule;
  1162. var
  1163. DefNamespace: String;
  1164. begin
  1165. //writeln('TTestModule.OnPasResolverFindUnit START Unit="',aUnitName,'"');
  1166. if (Pos('.',aUnitName)<1) then
  1167. begin
  1168. DefNamespace:=GetDefaultNamespace;
  1169. if DefNamespace<>'' then
  1170. begin
  1171. Result:=LoadUnit(DefNamespace+'.'+aUnitName);
  1172. if Result<>nil then exit;
  1173. end;
  1174. end;
  1175. Result:=LoadUnit(aUnitName);
  1176. if Result<>nil then exit;
  1177. {$IFDEF VerbosePas2JS}
  1178. writeln('TTestModule.OnPasResolverFindUnit missing unit "',aUnitName,'"');
  1179. {$ENDIF}
  1180. Fail('can''t find unit "'+aUnitName+'"');
  1181. end;
  1182. procedure TCustomTestModule.OnParserLog(Sender: TObject; const Msg: String);
  1183. var
  1184. aParser: TPasParser;
  1185. Item: TTestHintMessage;
  1186. begin
  1187. aParser:=Sender as TPasParser;
  1188. Item:=TTestHintMessage.Create;
  1189. Item.Id:=aParser.LastMsgNumber;
  1190. Item.MsgType:=aParser.LastMsgType;
  1191. Item.MsgNumber:=aParser.LastMsgNumber;
  1192. Item.Msg:=Msg;
  1193. Item.SourcePos:=aParser.Scanner.CurSourcePos;
  1194. {$IFDEF VerbosePas2JS}
  1195. writeln('TCustomTestModule.OnParserLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1196. {$ENDIF}
  1197. FHintMsgs.Add(Item);
  1198. end;
  1199. procedure TCustomTestModule.OnPasResolverLog(Sender: TObject; const Msg: String
  1200. );
  1201. var
  1202. aResolver: TTestEnginePasResolver;
  1203. Item: TTestHintMessage;
  1204. begin
  1205. aResolver:=Sender as TTestEnginePasResolver;
  1206. Item:=TTestHintMessage.Create;
  1207. Item.Id:=aResolver.LastMsgId;
  1208. Item.MsgType:=aResolver.LastMsgType;
  1209. Item.MsgNumber:=aResolver.LastMsgNumber;
  1210. Item.Msg:=Msg;
  1211. Item.SourcePos:=aResolver.LastSourcePos;
  1212. {$IFDEF VerbosePas2JS}
  1213. writeln('TCustomTestModule.OnPasResolverLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1214. {$ENDIF}
  1215. FHintMsgs.Add(Item);
  1216. end;
  1217. procedure TCustomTestModule.OnScannerLog(Sender: TObject; const Msg: String);
  1218. var
  1219. Item: TTestHintMessage;
  1220. aScanner: TPas2jsPasScanner;
  1221. begin
  1222. aScanner:=Sender as TPas2jsPasScanner;
  1223. Item:=TTestHintMessage.Create;
  1224. Item.Id:=aScanner.LastMsgNumber;
  1225. Item.MsgType:=aScanner.LastMsgType;
  1226. Item.MsgNumber:=aScanner.LastMsgNumber;
  1227. Item.Msg:=Msg;
  1228. Item.SourcePos:=aScanner.CurSourcePos;
  1229. {$IFDEF VerbosePas2JS}
  1230. writeln('TCustomTestModule.OnScannerLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1231. {$ENDIF}
  1232. FHintMsgs.Add(Item);
  1233. end;
  1234. procedure TCustomTestModule.SetWithTypeInfo(const AValue: boolean);
  1235. begin
  1236. if FWithTypeInfo=AValue then Exit;
  1237. FWithTypeInfo:=AValue;
  1238. if AValue then
  1239. Converter.Options:=Converter.Options-[coNoTypeInfo]
  1240. else
  1241. Converter.Options:=Converter.Options+[coNoTypeInfo];
  1242. end;
  1243. function TCustomTestModule.LoadUnit(const aUnitName: String): TPasModule;
  1244. var
  1245. i: Integer;
  1246. CurEngine: TTestEnginePasResolver;
  1247. CurUnitName: String;
  1248. begin
  1249. //writeln('TTestModule.FindUnit START Unit="',aUnitName,'"');
  1250. Result:=nil;
  1251. if (Module.ClassType=TPasModule)
  1252. and (CompareText(Module.Name,aUnitName)=0) then
  1253. exit(Module);
  1254. for i:=0 to ResolverCount-1 do
  1255. begin
  1256. CurEngine:=Resolvers[i];
  1257. CurUnitName:=ExtractFileUnitName(CurEngine.Filename);
  1258. //writeln('TTestModule.FindUnit Checking ',i,'/',ResolverCount,' ',CurEngine.Filename,' ',CurUnitName);
  1259. if CompareText(aUnitName,CurUnitName)=0 then
  1260. begin
  1261. Result:=CurEngine.Module;
  1262. if Result<>nil then exit;
  1263. //writeln('TTestModule.FindUnit PARSING unit "',CurEngine.Filename,'"');
  1264. FileResolver.FindSourceFile(aUnitName);
  1265. CurEngine.StreamResolver:=TStreamResolver.Create;
  1266. CurEngine.StreamResolver.OwnsStreams:=True;
  1267. //writeln('TTestModule.FindUnit SOURCE=',CurEngine.Source);
  1268. CurEngine.StreamResolver.AddStream(CurEngine.FileName,TStringStream.Create(CurEngine.Source));
  1269. CurEngine.Scanner:=TPas2jsPasScanner.Create(CurEngine.StreamResolver);
  1270. InitScanner(CurEngine.Scanner);
  1271. CurEngine.Parser:=TTestPasParser.Create(CurEngine.Scanner,CurEngine.StreamResolver,CurEngine);
  1272. CurEngine.Parser.Options:=po_tcmodules;
  1273. if CompareText(CurUnitName,'System')=0 then
  1274. CurEngine.Parser.ImplicitUses.Clear;
  1275. CurEngine.Scanner.OpenFile(CurEngine.Filename);
  1276. try
  1277. CurEngine.Parser.NextToken;
  1278. CurEngine.Parser.ParseUnit(CurEngine.FModule);
  1279. except
  1280. on E: Exception do
  1281. HandleException(E);
  1282. end;
  1283. //writeln('TTestModule.FindUnit END ',CurUnitName);
  1284. Result:=CurEngine.Module;
  1285. exit;
  1286. end;
  1287. end;
  1288. end;
  1289. procedure TCustomTestModule.SetUp;
  1290. begin
  1291. {$IFDEF EnablePasTreeGlobalRefCount}
  1292. FElementRefCountAtSetup:=TPasElement.GlobalRefCount;
  1293. {$ENDIF}
  1294. if FModules<>nil then
  1295. begin
  1296. writeln('TCustomTestModule.SetUp FModules<>nil');
  1297. Halt;
  1298. end;
  1299. inherited SetUp;
  1300. FSkipTests:=false;
  1301. FWithTypeInfo:=false;
  1302. FSource:=TStringList.Create;
  1303. FHub:=TPas2JSResolverHub.Create(Self);
  1304. FModules:=TObjectList.Create(true);
  1305. FFilename:='test1.pp';
  1306. FFileResolver:=TStreamResolver.Create;
  1307. FFileResolver.OwnsStreams:=True;
  1308. FScanner:=TPas2jsPasScanner.Create(FFileResolver);
  1309. InitScanner(FScanner);
  1310. FEngine:=AddModule(Filename);
  1311. FEngine.Scanner:=FScanner;
  1312. FScanner.Resolver:=FEngine;
  1313. FParser:=TTestPasParser.Create(FScanner,FFileResolver,FEngine);
  1314. FParser.OnLog:=@OnParserLog;
  1315. FEngine.Parser:=FParser;
  1316. Parser.Options:=po_tcmodules;
  1317. FModule:=Nil;
  1318. FConverter:=CreateConverter;
  1319. FExpectedErrorClass:=nil;
  1320. end;
  1321. function TCustomTestModule.CreateConverter: TPasToJSConverter;
  1322. var
  1323. Options: TPasToJsConverterOptions;
  1324. begin
  1325. Result:=TPasToJSConverter.Create;
  1326. Options:=co_tcmodules;
  1327. if WithTypeInfo then
  1328. Exclude(Options,coNoTypeInfo)
  1329. else
  1330. Include(Options,coNoTypeInfo);
  1331. Result.Options:=Options;
  1332. Result.Globals:=TPasToJSConverterGlobals.Create(Result);
  1333. end;
  1334. procedure TCustomTestModule.InitScanner(aScanner: TPas2jsPasScanner);
  1335. begin
  1336. aScanner.AllowedModeSwitches:=msAllPas2jsModeSwitches;
  1337. aScanner.ReadOnlyModeSwitches:=msAllPas2jsModeSwitchesReadOnly;
  1338. aScanner.CurrentModeSwitches:=OBJFPCModeSwitches*msAllPas2jsModeSwitches+msAllPas2jsModeSwitchesReadOnly;
  1339. aScanner.AllowedBoolSwitches:=bsAllPas2jsBoolSwitches;
  1340. aScanner.ReadOnlyBoolSwitches:=bsAllPas2jsBoolSwitchesReadOnly;
  1341. aScanner.CurrentBoolSwitches:=bsAllPas2jsBoolSwitchesReadOnly+[bsHints,bsNotes,bsWarnings,bsWriteableConst];
  1342. aScanner.AllowedValueSwitches:=vsAllPas2jsValueSwitches;
  1343. aScanner.ReadOnlyValueSwitches:=vsAllPas2jsValueSwitchesReadOnly;
  1344. aScanner.OnLog:=@OnScannerLog;
  1345. aScanner.CompilerVersion:='Comp.Ver.tcmodules';
  1346. end;
  1347. procedure TCustomTestModule.TearDown;
  1348. {$IFDEF CheckPasTreeRefCount}
  1349. var
  1350. El: TPasElement;
  1351. {$ENDIF}
  1352. var
  1353. i: Integer;
  1354. CurModule: TPasModule;
  1355. begin
  1356. FHintMsgs.Clear;
  1357. FHintMsgsGood.Clear;
  1358. FSkipTests:=false;
  1359. FWithTypeInfo:=false;
  1360. FJSRegModuleCall:=nil;
  1361. FJSModuleCallArgs:=nil;
  1362. FJSImplentationUses:=nil;
  1363. FJSInterfaceUses:=nil;
  1364. FJSModuleSrc:=nil;
  1365. FJSInitBody:=nil;
  1366. FreeAndNil(FJSSource);
  1367. FreeAndNil(FJSModule);
  1368. FreeAndNil(FConverter);
  1369. Engine.Clear;
  1370. FreeAndNil(FSource);
  1371. FreeAndNil(FFileResolver);
  1372. if FModules<>nil then
  1373. begin
  1374. for i:=0 to FModules.Count-1 do
  1375. begin
  1376. CurModule:=TTestEnginePasResolver(FModules[i]).Module;
  1377. if CurModule=nil then continue;
  1378. //writeln('TCustomTestModule.TearDown ReleaseUsedUnits ',CurModule.Name,' ',CurModule.RefCount,' ',CurModule.RefIds.Text);
  1379. CurModule.ReleaseUsedUnits;
  1380. end;
  1381. if FModule<>nil then
  1382. FModule.ReleaseUsedUnits;
  1383. for i:=0 to FModules.Count-1 do
  1384. begin
  1385. CurModule:=TTestEnginePasResolver(FModules[i]).Module;
  1386. if CurModule=nil then continue;
  1387. //writeln('TCustomTestModule.TearDown UsesReleased ',CurModule.Name,' ',CurModule.RefCount,' ',CurModule.RefIds.Text);
  1388. end;
  1389. FreeAndNil(FModules);
  1390. ReleaseAndNil(TPasElement(FModule){$IFDEF CheckPasTreeRefCount},'CreateElement'{$ENDIF});
  1391. FEngine:=nil;
  1392. end;
  1393. FreeAndNil(FHub);
  1394. inherited TearDown;
  1395. {$IFDEF EnablePasTreeGlobalRefCount}
  1396. if FElementRefCountAtSetup<>TPasElement.GlobalRefCount then
  1397. begin
  1398. writeln('TCustomTestModule.TearDown GlobalRefCount Was='+IntToStr(FElementRefCountAtSetup)+' Now='+IntToStr(TPasElement.GlobalRefCount));
  1399. {$IFDEF CheckPasTreeRefCount}
  1400. El:=TPasElement.FirstRefEl;
  1401. while El<>nil do
  1402. begin
  1403. writeln(' ',GetObjName(El),' RefIds.Count=',El.RefIds.Count,':');
  1404. for i:=0 to El.RefIds.Count-1 do
  1405. writeln(' ',El.RefIds[i]);
  1406. El:=El.NextRefEl;
  1407. end;
  1408. {$ENDIF}
  1409. Halt;
  1410. Fail('TCustomTestModule.TearDown Was='+IntToStr(FElementRefCountAtSetup)+' Now='+IntToStr(TPasElement.GlobalRefCount));
  1411. end;
  1412. {$ENDIF}
  1413. end;
  1414. procedure TCustomTestModule.Add(Line: string);
  1415. begin
  1416. Source.Add(Line);
  1417. end;
  1418. procedure TCustomTestModule.Add(const Lines: array of string);
  1419. var
  1420. i: Integer;
  1421. begin
  1422. for i:=low(Lines) to high(Lines) do
  1423. Add(Lines[i]);
  1424. end;
  1425. procedure TCustomTestModule.StartParsing;
  1426. var
  1427. Src: String;
  1428. begin
  1429. Src:=Source.Text;
  1430. FEngine.Source:=Src;
  1431. FileResolver.AddStream(FileName,TStringStream.Create(Src));
  1432. Scanner.OpenFile(FileName);
  1433. Writeln('// Test : ',Self.TestName);
  1434. Writeln(Src);
  1435. end;
  1436. procedure TCustomTestModule.ParseModuleQueue;
  1437. var
  1438. i: Integer;
  1439. CurResolver: TTestEnginePasResolver;
  1440. Found: Boolean;
  1441. Section: TPasSection;
  1442. begin
  1443. // parse til exception or all modules finished
  1444. while not SkipTests do
  1445. begin
  1446. Found:=false;
  1447. for i:=0 to ResolverCount-1 do
  1448. begin
  1449. CurResolver:=Resolvers[i];
  1450. if CurResolver.CurrentParser=nil then continue;
  1451. if not CurResolver.CurrentParser.CanParseContinue(Section) then
  1452. continue;
  1453. CurResolver.Parser.ParseContinue;
  1454. Found:=true;
  1455. break;
  1456. end;
  1457. if not Found then break;
  1458. end;
  1459. for i:=0 to ResolverCount-1 do
  1460. begin
  1461. CurResolver:=Resolvers[i];
  1462. if CurResolver.Parser=nil then
  1463. begin
  1464. if CurResolver.CurrentParser<>nil then
  1465. Fail('TCustomTestModule.ParseModuleQueue '+CurResolver.Filename+' '+GetObjName(CurResolver.Parser)+'=Parser<>CurrentParser='+GetObjName(CurResolver.CurrentParser));
  1466. continue;
  1467. end;
  1468. if CurResolver.Parser.CurModule<>nil then
  1469. Fail('TCustomTestModule.ParseModuleQueue '+CurResolver.Filename+' NOT FINISHED CurModule='+GetObjName(CurResolver.Parser.CurModule));
  1470. end;
  1471. end;
  1472. procedure TCustomTestModule.ParseModule;
  1473. begin
  1474. if SkipTests then exit;
  1475. FFirstPasStatement:=nil;
  1476. try
  1477. StartParsing;
  1478. Parser.ParseMain(FModule);
  1479. ParseModuleQueue;
  1480. except
  1481. on E: Exception do
  1482. HandleException(E);
  1483. end;
  1484. if SkipTests then exit;
  1485. AssertNotNull('Module resulted in Module',Module);
  1486. AssertEquals('modulename',lowercase(ChangeFileExt(FFileName,'')),lowercase(Module.Name));
  1487. TAssert.AssertSame('Has resolver',Engine,Parser.Engine);
  1488. end;
  1489. procedure TCustomTestModule.ParseProgram;
  1490. begin
  1491. if SkipTests then exit;
  1492. ParseModule;
  1493. if SkipTests then exit;
  1494. AssertEquals('Has program',TPasProgram,Module.ClassType);
  1495. FPasProgram:=TPasProgram(Module);
  1496. AssertNotNull('Has program section',PasProgram.ProgramSection);
  1497. AssertNotNull('Has initialization section',PasProgram.InitializationSection);
  1498. if (PasProgram.InitializationSection.Elements.Count>0) then
  1499. if TObject(PasProgram.InitializationSection.Elements[0]) is TPasImplBlock then
  1500. FFirstPasStatement:=TPasImplBlock(PasProgram.InitializationSection.Elements[0]);
  1501. end;
  1502. procedure TCustomTestModule.ParseLibrary;
  1503. var
  1504. Init: TInitializationSection;
  1505. begin
  1506. if SkipTests then exit;
  1507. ParseModule;
  1508. if SkipTests then exit;
  1509. AssertEquals('Has library',TPasLibrary,Module.ClassType);
  1510. FPasLibrary:=TPasLibrary(Module);
  1511. AssertNotNull('Has library section',PasLibrary.LibrarySection);
  1512. Init:=PasLibrary.InitializationSection;
  1513. if (Init<>nil) and (Init.Elements.Count>0) then
  1514. if TObject(Init.Elements[0]) is TPasImplBlock then
  1515. FFirstPasStatement:=TPasImplBlock(PasLibrary.InitializationSection.Elements[0]);
  1516. end;
  1517. procedure TCustomTestModule.ParseUnit;
  1518. begin
  1519. if SkipTests then exit;
  1520. ParseModule;
  1521. if SkipTests then exit;
  1522. AssertEquals('Has unit (TPasModule)',TPasModule,Module.ClassType);
  1523. AssertNotNull('Has interface section',Module.InterfaceSection);
  1524. AssertNotNull('Has implementation section',Module.ImplementationSection);
  1525. if (Module.InitializationSection<>nil)
  1526. and (Module.InitializationSection.Elements.Count>0)
  1527. and (TObject(Module.InitializationSection.Elements[0]) is TPasImplBlock) then
  1528. FFirstPasStatement:=TPasImplBlock(Module.InitializationSection.Elements[0]);
  1529. end;
  1530. function TCustomTestModule.FindModuleWithFilename(aFilename: string
  1531. ): TTestEnginePasResolver;
  1532. var
  1533. i: Integer;
  1534. begin
  1535. for i:=0 to ResolverCount-1 do
  1536. if CompareText(Resolvers[i].Filename,aFilename)=0 then
  1537. exit(Resolvers[i]);
  1538. Result:=nil;
  1539. end;
  1540. function TCustomTestModule.AddModule(aFilename: string
  1541. ): TTestEnginePasResolver;
  1542. begin
  1543. //writeln('TTestModuleConverter.AddModule ',aFilename);
  1544. if FindModuleWithFilename(aFilename)<>nil then
  1545. Fail('TTestModuleConverter.AddModule: file "'+aFilename+'" already exists');
  1546. Result:=TTestEnginePasResolver.Create;
  1547. Result.Filename:=aFilename;
  1548. Result.AddObjFPCBuiltInIdentifiers(btAllJSBaseTypes,bfAllJSBaseProcs);
  1549. Result.OnFindUnit:=@OnPasResolverFindUnit;
  1550. Result.OnLog:=@OnPasResolverLog;
  1551. Result.Hub:=Hub;
  1552. FModules.Add(Result);
  1553. end;
  1554. function TCustomTestModule.AddModuleWithSrc(aFilename, Src: string
  1555. ): TTestEnginePasResolver;
  1556. begin
  1557. Result:=AddModule(aFilename);
  1558. Result.Source:=Src;
  1559. end;
  1560. function TCustomTestModule.AddModuleWithIntfImplSrc(aFilename, InterfaceSrc,
  1561. ImplementationSrc: string): TTestEnginePasResolver;
  1562. var
  1563. Src: String;
  1564. begin
  1565. Src:='unit '+ExtractFileUnitName(aFilename)+';'+LineEnding;
  1566. Src+=LineEnding;
  1567. Src+='interface'+LineEnding;
  1568. Src+=LineEnding;
  1569. Src+=InterfaceSrc;
  1570. Src+='implementation'+LineEnding;
  1571. Src+=LineEnding;
  1572. Src+=ImplementationSrc;
  1573. Src+='end.'+LineEnding;
  1574. Result:=AddModuleWithSrc(aFilename,Src);
  1575. end;
  1576. procedure TCustomTestModule.AddSystemUnit(Parts: TSystemUnitParts);
  1577. var
  1578. Intf, Impl: TStringList;
  1579. begin
  1580. Intf:=TStringList.Create;
  1581. if supTInterfacedObject in Parts then Include(Parts,supTObject);
  1582. // unit interface
  1583. if [supTVarRec,supTypeInfo]*Parts<>[] then
  1584. Intf.Add('{$modeswitch externalclass}');
  1585. Intf.Add('type');
  1586. Intf.Add(' integer=longint;');
  1587. Intf.Add(' sizeint=nativeint;');
  1588. //'const',
  1589. //' LineEnding = #10;',
  1590. //' DirectorySeparator = ''/'';',
  1591. //' DriveSeparator = '''';',
  1592. //' AllowDirectorySeparators : set of char = [''\'',''/''];',
  1593. //' AllowDriveSeparators : set of char = [];',
  1594. if supTObject in Parts then
  1595. Intf.AddStrings([
  1596. 'type',
  1597. ' TClass = class of TObject;',
  1598. ' TObject = class',
  1599. ' constructor Create;',
  1600. ' destructor Destroy; virtual;',
  1601. ' class function ClassType: TClass; assembler;',
  1602. ' class function ClassName: String; assembler;',
  1603. ' class function ClassNameIs(const Name: string): boolean;',
  1604. ' class function ClassParent: TClass; assembler;',
  1605. ' class function InheritsFrom(aClass: TClass): boolean; assembler;',
  1606. ' class function UnitName: String; assembler;',
  1607. ' procedure AfterConstruction; virtual;',
  1608. ' procedure BeforeDestruction;virtual;',
  1609. ' function Equals(Obj: TObject): boolean; virtual;',
  1610. ' function ToString: String; virtual;',
  1611. ' end;']);
  1612. if supTInterfacedObject in Parts then
  1613. Intf.AddStrings([
  1614. ' {$Interfaces COM}',
  1615. ' IUnknown = interface',
  1616. ' [''{00000000-0000-0000-C000-000000000046}'']',
  1617. //' function QueryInterface(const iid: TGuid; out obj): Integer;',
  1618. ' function _AddRef: Integer;',
  1619. ' function _Release: Integer;',
  1620. ' end;',
  1621. ' IInterface = IUnknown;',
  1622. ' TInterfacedObject = class(TObject,IUnknown)',
  1623. ' protected',
  1624. ' fRefCount: Integer;',
  1625. ' { implement methods of IUnknown }',
  1626. //' function QueryInterface(const iid: TGuid; out obj): Integer; virtual;',
  1627. ' function _AddRef: Integer; virtual;',
  1628. ' function _Release: Integer; virtual;',
  1629. ' end;',
  1630. ' TInterfacedClass = class of TInterfacedObject;',
  1631. '',
  1632. '']);
  1633. if supTVarRec in Parts then
  1634. Intf.AddStrings([
  1635. 'const',
  1636. ' vtInteger = 0;',
  1637. ' vtBoolean = 1;',
  1638. ' vtJSValue = 19;',
  1639. 'type',
  1640. ' PVarRec = ^TVarRec;',
  1641. ' TVarRec = record',
  1642. ' VType : byte;',
  1643. ' VJSValue: JSValue;',
  1644. ' vInteger: longint external name ''VJSValue'';',
  1645. ' vBoolean: boolean external name ''VJSValue'';',
  1646. ' end;',
  1647. ' TVarRecArray = array of TVarRec;',
  1648. 'function VarRecs: TVarRecArray; varargs;',
  1649. '']);
  1650. if supTypeInfo in Parts then
  1651. begin
  1652. Intf.AddStrings([
  1653. 'type',
  1654. ' TTypeKind = (',
  1655. ' tkUnknown, // 0',
  1656. ' tkInteger, // 1',
  1657. ' tkChar, // 2 in Delphi/FPC tkWChar, tkUChar',
  1658. ' tkString, // 3 in Delphi/FPC tkSString, tkWString or tkUString',
  1659. ' tkEnumeration, // 4',
  1660. ' tkSet, // 5',
  1661. ' tkDouble, // 6',
  1662. ' tkBool, // 7',
  1663. ' tkProcVar, // 8 function or procedure',
  1664. ' tkMethod, // 9 proc var of object',
  1665. ' tkArray, // 10 static array',
  1666. ' tkDynArray, // 11',
  1667. ' tkRecord, // 12',
  1668. ' tkClass, // 13',
  1669. ' tkClassRef, // 14',
  1670. ' tkPointer, // 15',
  1671. ' tkJSValue, // 16',
  1672. ' tkRefToProcVar, // 17 variable of procedure type',
  1673. ' tkInterface, // 18',
  1674. ' //tkObject,',
  1675. ' //tkSString,tkLString,tkAString,tkWString,',
  1676. ' //tkVariant,',
  1677. ' //tkWChar,',
  1678. ' //tkInt64,',
  1679. ' //tkQWord,',
  1680. ' //tkInterfaceRaw,',
  1681. ' //tkUString,tkUChar,',
  1682. ' tkHelper, // 19',
  1683. ' //tkFile,',
  1684. ' tkExtClass // 20',
  1685. ' );',
  1686. ' TTypeKinds = set of TTypeKind;',
  1687. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  1688. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo)',
  1689. ' end;',
  1690. ' TTypeInfoEnum = class external name ''rtl.tTypeInfoEnum''(TTypeInfoInteger) end;',
  1691. ' TTypeInfoSet = class external name ''rtl.tTypeInfoSet''(TTypeInfo) end;',
  1692. ' TTypeInfoStaticArray = class external name ''rtl.tTypeInfoStaticArray''(TTypeInfo) end;',
  1693. ' TTypeInfoDynArray = class external name ''rtl.tTypeInfoDynArray''(TTypeInfo) end;',
  1694. ' TTypeInfoProcVar = class external name ''rtl.tTypeInfoProcVar''(TTypeInfo) end;',
  1695. ' TTypeInfoMethodVar = class external name ''rtl.tTypeInfoMethodVar''(TTypeInfoProcVar) end;',
  1696. ' TTypeInfoClass = class external name ''rtl.tTypeInfoClass''(TTypeInfo) end;',
  1697. ' TTypeInfoClassRef = class external name ''rtl.tTypeInfoClassRef''(TTypeInfo) end;',
  1698. ' TTypeInfoExtClass = class external name ''rtl.tTypeInfoExtClass''(TTypeInfo) end;',
  1699. ' TTypeInfoRecord = class external name ''rtl.tTypeInfoRecord''(TTypeInfo) end;',
  1700. ' TTypeInfoPointer = class external name ''rtl.tTypeInfoPointer''(TTypeInfo) end;',
  1701. ' TTypeInfoHelper = class external name ''rtl.tTypeInfoHelper''(TTypeInfo) end;',
  1702. ' TTypeInfoInterface = class external name ''rtl.tTypeInfoInterface''(TTypeInfo) end;',
  1703. '']);
  1704. end;
  1705. if supWriteln in Parts then
  1706. Intf.Add('procedure writeln; varargs; external name ''console.log'';');
  1707. Intf.Add('var');
  1708. Intf.Add(' ExitCode: Longint = 0;');
  1709. // unit implementation
  1710. Impl:=TStringList.Create;
  1711. if supTObject in Parts then
  1712. Impl.AddStrings([
  1713. '// needed by ClassNameIs, the real SameText is in SysUtils',
  1714. 'function SameText(const s1, s2: String): Boolean; assembler;',
  1715. 'asm',
  1716. 'end;',
  1717. 'constructor TObject.Create; begin end;',
  1718. 'destructor TObject.Destroy; begin end;',
  1719. 'class function TObject.ClassType: TClass; assembler;',
  1720. 'asm',
  1721. 'end;',
  1722. 'class function TObject.ClassName: String; assembler;',
  1723. 'asm',
  1724. 'end;',
  1725. 'class function TObject.ClassNameIs(const Name: string): boolean;',
  1726. 'begin',
  1727. ' Result:=SameText(Name,ClassName);',
  1728. 'end;',
  1729. 'class function TObject.ClassParent: TClass; assembler;',
  1730. 'asm',
  1731. 'end;',
  1732. 'class function TObject.InheritsFrom(aClass: TClass): boolean; assembler;',
  1733. 'asm',
  1734. 'end;',
  1735. 'class function TObject.UnitName: String; assembler;',
  1736. 'asm',
  1737. 'end;',
  1738. 'procedure TObject.AfterConstruction; begin end;',
  1739. 'procedure TObject.BeforeDestruction; begin end;',
  1740. 'function TObject.Equals(Obj: TObject): boolean;',
  1741. 'begin',
  1742. ' Result:=Obj=Self;',
  1743. 'end;',
  1744. 'function TObject.ToString: String;',
  1745. 'begin',
  1746. ' Result:=ClassName;',
  1747. 'end;'
  1748. ]);
  1749. if supTInterfacedObject in Parts then
  1750. Impl.AddStrings([
  1751. //'function TInterfacedObject.QueryInterface(const iid: TGuid; out obj): Integer;',
  1752. //'begin',
  1753. //'end;',
  1754. 'function TInterfacedObject._AddRef: Integer;',
  1755. 'begin',
  1756. 'end;',
  1757. 'function TInterfacedObject._Release: Integer;',
  1758. 'begin',
  1759. 'end;',
  1760. '']);
  1761. if supTVarRec in Parts then
  1762. Impl.AddStrings([
  1763. 'function VarRecs: TVarRecArray; varargs;',
  1764. 'var',
  1765. ' v: PVarRec;',
  1766. 'begin',
  1767. ' v^.VType:=1;',
  1768. ' v^.VJSValue:=2;',
  1769. 'end;',
  1770. '']);
  1771. try
  1772. AddModuleWithIntfImplSrc('system.pp',Intf.Text,Impl.Text);
  1773. finally
  1774. Intf.Free;
  1775. Impl.Free;
  1776. end;
  1777. end;
  1778. procedure TCustomTestModule.StartProgram(NeedSystemUnit: boolean;
  1779. SystemUnitParts: TSystemUnitParts);
  1780. begin
  1781. if NeedSystemUnit then
  1782. AddSystemUnit(SystemUnitParts)
  1783. else
  1784. Parser.ImplicitUses.Clear;
  1785. Add('program '+ExtractFileUnitName(Filename)+';');
  1786. Add('');
  1787. end;
  1788. procedure TCustomTestModule.StartLibrary(NeedSystemUnit: boolean;
  1789. SystemUnitParts: TSystemUnitParts);
  1790. begin
  1791. if NeedSystemUnit then
  1792. AddSystemUnit(SystemUnitParts)
  1793. else
  1794. Parser.ImplicitUses.Clear;
  1795. Add('library '+ExtractFileUnitName(Filename)+';');
  1796. Add('');
  1797. end;
  1798. procedure TCustomTestModule.StartUnit(NeedSystemUnit: boolean;
  1799. SystemUnitParts: TSystemUnitParts);
  1800. begin
  1801. if NeedSystemUnit then
  1802. AddSystemUnit(SystemUnitParts)
  1803. else
  1804. Parser.ImplicitUses.Clear;
  1805. Add('unit Test1;');
  1806. Add('');
  1807. end;
  1808. procedure TCustomTestModule.ConvertModule;
  1809. procedure CheckUsesList(UsesName: String; Arg: TJSArrayLiteralElement;
  1810. out UsesLit: TJSArrayLiteral);
  1811. var
  1812. i: Integer;
  1813. Item: TJSElement;
  1814. Lit: TJSLiteral;
  1815. begin
  1816. UsesLit:=nil;
  1817. AssertNotNull(UsesName+' uses section',Arg.Expr);
  1818. if (Arg.Expr.ClassType=TJSLiteral) and TJSLiteral(Arg.Expr).Value.IsNull then
  1819. exit; // null is ok
  1820. AssertEquals(UsesName+' uses section param is array',TJSArrayLiteral,Arg.Expr.ClassType);
  1821. FJSInterfaceUses:=TJSArrayLiteral(Arg.Expr);
  1822. for i:=0 to FJSInterfaceUses.Elements.Count-1 do
  1823. begin
  1824. Item:=FJSInterfaceUses.Elements.Elements[i].Expr;
  1825. AssertNotNull(UsesName+' uses section item['+IntToStr(i)+'].Expr',Item);
  1826. AssertEquals(UsesName+' uses section item['+IntToStr(i)+'] is lit',TJSLiteral,Item.ClassType);
  1827. Lit:=TJSLiteral(Item);
  1828. AssertEquals(UsesName+' uses section item['+IntToStr(i)+'] is string lit',
  1829. ord(jsbase.jstString),ord(Lit.Value.ValueType));
  1830. end;
  1831. end;
  1832. procedure CheckFunctionParam(ParamName: string; Arg: TJSArrayLiteralElement;
  1833. out Src: TJSSourceElements);
  1834. var
  1835. FunDecl: TJSFunctionDeclarationStatement;
  1836. FunDef: TJSFuncDef;
  1837. FunBody: TJSFunctionBody;
  1838. begin
  1839. Src:=nil;
  1840. AssertNotNull(ParamName,Arg.Expr);
  1841. AssertEquals(ParamName+' Arg.Expr type',TJSFunctionDeclarationStatement,Arg.Expr.ClassType);
  1842. FunDecl:=Arg.Expr as TJSFunctionDeclarationStatement;
  1843. AssertNotNull(ParamName+' FunDecl.AFunction',FunDecl.AFunction);
  1844. AssertEquals(ParamName+' FunDecl.AFunction type',TJSFuncDef,FunDecl.AFunction.ClassType);
  1845. FunDef:=FunDecl.AFunction as TJSFuncDef;
  1846. AssertEquals(ParamName+' name empty','',String(FunDef.Name));
  1847. AssertNotNull(ParamName+' body',FunDef.Body);
  1848. AssertEquals(ParamName+' body type',TJSFunctionBody,FunDef.Body.ClassType);
  1849. FunBody:=FunDef.Body as TJSFunctionBody;
  1850. AssertNotNull(ParamName+' body.A',FunBody.A);
  1851. AssertEquals(ParamName+' body.A type',TJSSourceElements,FunBody.A.ClassType);
  1852. Src:=FunBody.A as TJSSourceElements;
  1853. end;
  1854. var
  1855. ModuleNameExpr: TJSLiteral;
  1856. InitFunction: TJSFunctionDeclarationStatement;
  1857. InitAssign: TJSSimpleAssignStatement;
  1858. InitName: String;
  1859. LastNode: TJSElement;
  1860. Arg: TJSArrayLiteralElement;
  1861. begin
  1862. if SkipTests then exit;
  1863. try
  1864. FJSModule:=FConverter.ConvertPasElement(Module,Engine) as TJSSourceElements;
  1865. except
  1866. on E: Exception do
  1867. HandleException(E);
  1868. end;
  1869. if SkipTests then exit;
  1870. if ExpectedErrorClass<>nil then
  1871. Fail('Missing '+ExpectedErrorClass.ClassName+' error {'+ExpectedErrorMsg+'} ('+IntToStr(ExpectedErrorNumber)+')');
  1872. FJSSource:=TStringList.Create;
  1873. FJSSource.Text:=ConvertJSModuleToString(JSModule);
  1874. {$IFDEF VerbosePas2JS}
  1875. writeln('TTestModule.ConvertModule JS:');
  1876. write(FJSSource.Text);
  1877. {$ENDIF}
  1878. // rtl.module(...
  1879. AssertEquals('jsmodule has one statement - the call',1,JSModule.Statements.Count);
  1880. AssertNotNull('register module call',JSModule.Statements.Nodes[0].Node);
  1881. AssertEquals('register module call',TJSCallExpression,JSModule.Statements.Nodes[0].Node.ClassType);
  1882. FJSRegModuleCall:=JSModule.Statements.Nodes[0].Node as TJSCallExpression;
  1883. AssertNotNull('register module rtl.module expr',JSRegModuleCall.Expr);
  1884. AssertNotNull('register module rtl.module args',JSRegModuleCall.Args);
  1885. AssertEquals('rtl.module args',TJSArguments,JSRegModuleCall.Args.ClassType);
  1886. FJSModuleCallArgs:=JSRegModuleCall.Args as TJSArguments;
  1887. // parameter 'unitname'
  1888. if JSModuleCallArgs.Elements.Count<1 then
  1889. Fail('rtl.module first param unit missing');
  1890. Arg:=JSModuleCallArgs.Elements.Elements[0];
  1891. AssertNotNull('module name param',Arg.Expr);
  1892. ModuleNameExpr:=Arg.Expr as TJSLiteral;
  1893. AssertEquals('module name param is string',ord(jstString),ord(ModuleNameExpr.Value.ValueType));
  1894. if Module is TPasProgram then
  1895. AssertEquals('module name','program',String(ModuleNameExpr.Value.AsString))
  1896. else if Module is TPasLibrary then
  1897. AssertEquals('module name','library',String(ModuleNameExpr.Value.AsString))
  1898. else
  1899. AssertEquals('module name',Module.Name,String(ModuleNameExpr.Value.AsString));
  1900. // main uses section
  1901. if JSModuleCallArgs.Elements.Count<2 then
  1902. Fail('rtl.module second param main uses missing');
  1903. Arg:=JSModuleCallArgs.Elements.Elements[1];
  1904. CheckUsesList('interface',Arg,FJSInterfaceUses);
  1905. // program/library/interface function()
  1906. if JSModuleCallArgs.Elements.Count<3 then
  1907. Fail('rtl.module third param intf-function missing');
  1908. Arg:=JSModuleCallArgs.Elements.Elements[2];
  1909. CheckFunctionParam('module intf-function',Arg,FJSModuleSrc);
  1910. // search for $mod.$init or $mod.$main - the last statement
  1911. if (Module is TPasProgram) or (Module is TPasLibrary) then
  1912. begin
  1913. InitName:='$main';
  1914. AssertEquals('$mod.'+InitName+' function 1',true,JSModuleSrc.Statements.Count>0);
  1915. end
  1916. else
  1917. InitName:='$init';
  1918. FJSInitBody:=nil;
  1919. if JSModuleSrc.Statements.Count>0 then
  1920. begin
  1921. LastNode:=JSModuleSrc.Statements.Nodes[JSModuleSrc.Statements.Count-1].Node;
  1922. if LastNode is TJSSimpleAssignStatement then
  1923. begin
  1924. InitAssign:=LastNode as TJSSimpleAssignStatement;
  1925. if GetDottedIdentifier(InitAssign.LHS)='$mod.'+InitName then
  1926. begin
  1927. InitFunction:=InitAssign.Expr as TJSFunctionDeclarationStatement;
  1928. FJSInitBody:=InitFunction.AFunction.Body as TJSFunctionBody;
  1929. end
  1930. else if (Module is TPasProgram) or (Module is TPasLibrary) then
  1931. CheckDottedIdentifier('init function',InitAssign.LHS,'$mod.'+InitName);
  1932. end;
  1933. end;
  1934. // optional: implementation uses section
  1935. if JSModuleCallArgs.Elements.Count<4 then
  1936. exit;
  1937. Arg:=JSModuleCallArgs.Elements.Elements[3];
  1938. CheckUsesList('implementation',Arg,FJSImplentationUses);
  1939. end;
  1940. procedure TCustomTestModule.ConvertProgram;
  1941. begin
  1942. Add('end.');
  1943. ParseProgram;
  1944. ConvertModule;
  1945. end;
  1946. procedure TCustomTestModule.ConvertLibrary;
  1947. begin
  1948. Add('end.');
  1949. ParseLibrary;
  1950. ConvertModule;
  1951. end;
  1952. procedure TCustomTestModule.ConvertUnit;
  1953. begin
  1954. Add('end.');
  1955. ParseUnit;
  1956. ConvertModule;
  1957. end;
  1958. function TCustomTestModule.ConvertJSModuleToString(El: TJSElement): string;
  1959. begin
  1960. Result:=tcmodules.JSToStr(El);
  1961. end;
  1962. procedure TCustomTestModule.CheckDottedIdentifier(Msg: string; El: TJSElement;
  1963. DottedName: string);
  1964. begin
  1965. if DottedName='' then
  1966. begin
  1967. AssertNull(Msg,El);
  1968. end
  1969. else
  1970. begin
  1971. AssertNotNull(Msg,El);
  1972. AssertEquals(Msg,DottedName,GetDottedIdentifier(El));
  1973. end;
  1974. end;
  1975. function TCustomTestModule.GetDottedIdentifier(El: TJSElement): string;
  1976. begin
  1977. if El=nil then
  1978. Result:=''
  1979. else if El is TJSPrimaryExpressionIdent then
  1980. Result:=String(TJSPrimaryExpressionIdent(El).Name)
  1981. else if El is TJSDotMemberExpression then
  1982. Result:=GetDottedIdentifier(TJSDotMemberExpression(El).MExpr)+'.'+String(TJSDotMemberExpression(El).Name)
  1983. else
  1984. AssertEquals('GetDottedIdentifier',TJSPrimaryExpressionIdent,El.ClassType);
  1985. end;
  1986. procedure TCustomTestModule.CheckSource(Msg, Statements: String;
  1987. InitStatements: string; ImplStatements: string);
  1988. var
  1989. ActualSrc, ExpectedSrc, InitName: String;
  1990. begin
  1991. ActualSrc:=JSToStr(JSModuleSrc);
  1992. if coUseStrict in Converter.Options then
  1993. ExpectedSrc:='"use strict";'+LineEnding
  1994. else
  1995. ExpectedSrc:='';
  1996. ExpectedSrc:=ExpectedSrc+'var $mod = this;'+LineEnding;
  1997. ExpectedSrc:=ExpectedSrc+Statements;
  1998. // unit implementation
  1999. if (Trim(ImplStatements)<>'') then
  2000. ExpectedSrc:=ExpectedSrc+LineEnding
  2001. +'$mod.$implcode = function () {'+LineEnding
  2002. +ImplStatements
  2003. +'};'+LineEnding;
  2004. // program main or unit initialization
  2005. if (Module is TPasProgram) or (Trim(InitStatements)<>'') then
  2006. begin
  2007. if (Module is TPasProgram) or (Module is TPasLibrary) then
  2008. InitName:='$main'
  2009. else
  2010. InitName:='$init';
  2011. ExpectedSrc:=ExpectedSrc+LineEnding
  2012. +'$mod.'+InitName+' = function () {'+LineEnding
  2013. +InitStatements
  2014. +'};'+LineEnding;
  2015. end;
  2016. //writeln('TCustomTestModule.CheckSource ExpectedIntf="',ExpectedSrc,'"');
  2017. //writeln('TTestModule.CheckSource InitStatements="',Trim(InitStatements),'"');
  2018. CheckDiff(Msg,ExpectedSrc,ActualSrc);
  2019. end;
  2020. procedure TCustomTestModule.CheckDiff(Msg, Expected, Actual: string);
  2021. // search diff, ignore changes in spaces
  2022. var
  2023. s: string;
  2024. begin
  2025. if CheckSrcDiff(Expected,Actual,s) then exit;
  2026. Fail(Msg+': '+s);
  2027. end;
  2028. procedure TCustomTestModule.CheckUnit(Filename, ExpectedSrc: string);
  2029. var
  2030. aResolver: TTestEnginePasResolver;
  2031. aConverter: TPasToJSConverter;
  2032. aJSModule: TJSSourceElements;
  2033. ActualSrc: String;
  2034. begin
  2035. aResolver:=GetResolver(Filename);
  2036. AssertNotNull('missing resolver of unit '+Filename,aResolver);
  2037. AssertNotNull('missing resolver.module of unit '+Filename,aResolver.Module);
  2038. {$IFDEF VerbosePas2JS}
  2039. writeln('CheckUnit '+Filename+' converting ...');
  2040. {$ENDIF}
  2041. aConverter:=CreateConverter;
  2042. aJSModule:=nil;
  2043. try
  2044. try
  2045. aJSModule:=aConverter.ConvertPasElement(aResolver.Module,aResolver) as TJSSourceElements;
  2046. except
  2047. on E: Exception do
  2048. HandleException(E);
  2049. end;
  2050. ActualSrc:=ConvertJSModuleToString(aJSModule);
  2051. {$IFDEF VerbosePas2JS}
  2052. writeln('TTestModule.CheckUnit ',Filename,' Pas:');
  2053. write(aResolver.Source);
  2054. writeln('TTestModule.CheckUnit ',Filename,' JS:');
  2055. write(ActualSrc);
  2056. {$ENDIF}
  2057. CheckDiff('Converted unit: "'+ChangeFileExt(Filename,'.js')+'"',ExpectedSrc,ActualSrc);
  2058. finally
  2059. aJSModule.Free;
  2060. aConverter.Free;
  2061. end;
  2062. end;
  2063. procedure TCustomTestModule.CheckHint(MsgType: TMessageType;
  2064. MsgNumber: integer; Msg: string; Marker: PSrcMarker);
  2065. var
  2066. i: Integer;
  2067. Item: TTestHintMessage;
  2068. Expected,Actual: string;
  2069. begin
  2070. //writeln('TCustomTestModule.CheckHint MsgCount=',MsgCount);
  2071. for i:=0 to MsgCount-1 do
  2072. begin
  2073. Item:=Msgs[i];
  2074. if (Item.MsgNumber<>MsgNumber) or (Item.Msg<>Msg) then continue;
  2075. if (Marker<>nil) then
  2076. begin
  2077. if Item.SourcePos.Row<>cardinal(Marker^.Row) then continue;
  2078. if (Item.SourcePos.Column<cardinal(Marker^.StartCol))
  2079. or (Item.SourcePos.Column>cardinal(Marker^.EndCol)) then continue;
  2080. end;
  2081. // found
  2082. FHintMsgsGood.Add(Item);
  2083. str(Item.MsgType,Actual);
  2084. str(MsgType,Expected);
  2085. AssertEquals('MsgType',Expected,Actual);
  2086. exit;
  2087. end;
  2088. // needed message missing -> show emitted messages
  2089. WriteSources('',0,0);
  2090. for i:=0 to MsgCount-1 do
  2091. begin
  2092. Item:=Msgs[i];
  2093. write('TCustomTestModule.CheckHint ',i,'/',MsgCount,' ',Item.MsgType,
  2094. ' ('+IntToStr(Item.MsgNumber),')');
  2095. if Marker<>nil then
  2096. write(' '+ExtractFileName(Item.SourcePos.FileName),'(',Item.SourcePos.Row,',',Item.SourcePos.Column,')');
  2097. writeln(' {',Item.Msg,'}');
  2098. end;
  2099. str(MsgType,Expected);
  2100. Actual:='Missing '+Expected+' ('+IntToStr(MsgNumber)+')';
  2101. if Marker<>nil then
  2102. Actual:=Actual+' '+ExtractFileName(Marker^.Filename)+'('+IntToStr(Marker^.Row)+','+IntToStr(Marker^.StartCol)+'..'+IntToStr(Marker^.EndCol)+')';
  2103. Actual:=Actual+' '+Msg;
  2104. Fail(Actual);
  2105. end;
  2106. procedure TCustomTestModule.CheckResolverUnexpectedHints(WithSourcePos: boolean
  2107. );
  2108. var
  2109. i: Integer;
  2110. s, Txt: String;
  2111. Msg: TTestHintMessage;
  2112. begin
  2113. for i:=0 to MsgCount-1 do
  2114. begin
  2115. Msg:=Msgs[i];
  2116. if FHintMsgsGood.IndexOf(Msg)>=0 then continue;
  2117. s:='';
  2118. str(Msg.MsgType,s);
  2119. Txt:='Unexpected resolver message found ['+IntToStr(Msg.Id)+'] '
  2120. +s+': ('+IntToStr(Msg.MsgNumber)+')';
  2121. if WithSourcePos then
  2122. Txt:=Txt+' '+ExtractFileName(Msg.SourcePos.FileName)+'('+IntToStr(Msg.SourcePos.Row)+','+IntToStr(Msg.SourcePos.Column)+')';
  2123. Txt:=Txt+' {'+Msg.Msg+'}';
  2124. Fail(Txt);
  2125. end;
  2126. end;
  2127. procedure TCustomTestModule.SetExpectedScannerError(Msg: string;
  2128. MsgNumber: integer);
  2129. begin
  2130. ExpectedErrorClass:=EScannerError;
  2131. ExpectedErrorMsg:=Msg;
  2132. ExpectedErrorNumber:=MsgNumber;
  2133. end;
  2134. procedure TCustomTestModule.SetExpectedParserError(Msg: string;
  2135. MsgNumber: integer);
  2136. begin
  2137. ExpectedErrorClass:=EParserError;
  2138. ExpectedErrorMsg:=Msg;
  2139. ExpectedErrorNumber:=MsgNumber;
  2140. end;
  2141. procedure TCustomTestModule.SetExpectedPasResolverError(Msg: string;
  2142. MsgNumber: integer);
  2143. begin
  2144. ExpectedErrorClass:=EPasResolve;
  2145. ExpectedErrorMsg:=Msg;
  2146. ExpectedErrorNumber:=MsgNumber;
  2147. end;
  2148. procedure TCustomTestModule.SetExpectedConverterError(Msg: string;
  2149. MsgNumber: integer);
  2150. begin
  2151. ExpectedErrorClass:=EPas2JS;
  2152. ExpectedErrorMsg:=Msg;
  2153. ExpectedErrorNumber:=MsgNumber;
  2154. end;
  2155. function TCustomTestModule.IsErrorExpected(E: Exception): boolean;
  2156. var
  2157. MsgNumber: Integer;
  2158. Msg: String;
  2159. begin
  2160. Result:=false;
  2161. if (ExpectedErrorClass=nil) or (ExpectedErrorClass<>E.ClassType) then exit;
  2162. Msg:=E.Message;
  2163. if E is EPas2JS then
  2164. MsgNumber:=EPas2JS(E).MsgNumber
  2165. else if E is EPasResolve then
  2166. MsgNumber:=EPasResolve(E).MsgNumber
  2167. else if E is EParserError then
  2168. MsgNumber:=Parser.LastMsgNumber
  2169. else if E is EScannerError then
  2170. begin
  2171. MsgNumber:=Scanner.LastMsgNumber;
  2172. Msg:=Scanner.LastMsg;
  2173. end
  2174. else
  2175. MsgNumber:=0;
  2176. Result:=(MsgNumber=ExpectedErrorNumber) and (Msg=ExpectedErrorMsg);
  2177. if Result then
  2178. SkipTests:=true;
  2179. end;
  2180. procedure TCustomTestModule.HandleScannerError(E: EScannerError);
  2181. begin
  2182. if IsErrorExpected(E) then exit;
  2183. WriteSources(Scanner.CurFilename,Scanner.CurRow,Scanner.CurColumn);
  2184. writeln('ERROR: TCustomTestModule.HandleScannerError '+E.ClassName+':'+E.Message
  2185. +' '+Scanner.CurFilename
  2186. +'('+IntToStr(Scanner.CurRow)+','+IntToStr(Scanner.CurColumn)+')');
  2187. FailException(E);
  2188. end;
  2189. procedure TCustomTestModule.HandleParserError(E: EParserError);
  2190. begin
  2191. if IsErrorExpected(E) then exit;
  2192. WriteSources(E.Filename,E.Row,E.Column);
  2193. writeln('ERROR: TCustomTestModule.HandleParserError '+E.ClassName+':'+E.Message
  2194. +' '+E.Filename+'('+IntToStr(E.Row)+','+IntToStr(E.Column)+')'
  2195. +' MainModuleScannerLine="'+Scanner.CurLine+'"'
  2196. );
  2197. FailException(E);
  2198. end;
  2199. procedure TCustomTestModule.HandlePasResolveError(E: EPasResolve);
  2200. var
  2201. P: TPasSourcePos;
  2202. begin
  2203. if IsErrorExpected(E) then exit;
  2204. P:=E.SourcePos;
  2205. WriteSources(P.FileName,P.Row,P.Column);
  2206. writeln('ERROR: TCustomTestModule.HandlePasResolveError '+E.ClassName+':'+E.Message
  2207. +' '+P.FileName+'('+IntToStr(P.Row)+','+IntToStr(P.Column)+')');
  2208. FailException(E);
  2209. end;
  2210. procedure TCustomTestModule.HandlePas2JSError(E: EPas2JS);
  2211. var
  2212. Row, Col: integer;
  2213. begin
  2214. if IsErrorExpected(E) then exit;
  2215. Engine.UnmangleSourceLineNumber(E.PasElement.SourceLinenumber,Row,Col);
  2216. WriteSources(E.PasElement.SourceFilename,Row,Col);
  2217. writeln('ERROR: TCustomTestModule.HandlePas2JSError '+E.ClassName+':'+E.Message
  2218. +' '+E.PasElement.SourceFilename
  2219. +'('+IntToStr(Row)+','+IntToStr(Col)+')');
  2220. FailException(E);
  2221. end;
  2222. procedure TCustomTestModule.HandleException(E: Exception);
  2223. begin
  2224. if E is EScannerError then
  2225. HandleScannerError(EScannerError(E))
  2226. else if E is EParserError then
  2227. HandleParserError(EParserError(E))
  2228. else if E is EPasResolve then
  2229. HandlePasResolveError(EPasResolve(E))
  2230. else if E is EPas2JS then
  2231. HandlePas2JSError(EPas2JS(E))
  2232. else
  2233. begin
  2234. if IsErrorExpected(E) then exit;
  2235. if not (E is EAssertionFailedError) then
  2236. begin
  2237. WriteSources('',0,0);
  2238. writeln('ERROR: TCustomTestModule.HandleException '+E.ClassName+':'+E.Message);
  2239. end;
  2240. FailException(E);
  2241. end;
  2242. end;
  2243. procedure TCustomTestModule.FailException(E: Exception);
  2244. var
  2245. MsgNumber: Integer;
  2246. begin
  2247. if ExpectedErrorClass<>nil then
  2248. begin
  2249. if FExpectedErrorClass=E.ClassType then
  2250. begin
  2251. if E is EPas2JS then
  2252. MsgNumber:=EPas2JS(E).MsgNumber
  2253. else if E is EPasResolve then
  2254. MsgNumber:=EPasResolve(E).MsgNumber
  2255. else if E is EParserError then
  2256. MsgNumber:=Parser.LastMsgNumber
  2257. else if E is EScannerError then
  2258. MsgNumber:=Scanner.LastMsgNumber
  2259. else
  2260. MsgNumber:=0;
  2261. AssertEquals('Expected error message ('+IntToStr(ExpectedErrorNumber)+')','{'+ExpectedErrorMsg+'}','{'+E.Message+'}');
  2262. AssertEquals('Expected {'+ExpectedErrorMsg+'}, but got msg {'+E.Message+'} number',
  2263. ExpectedErrorNumber,MsgNumber);
  2264. end else begin
  2265. AssertEquals('Wrong exception class',ExpectedErrorClass.ClassName,E.ClassName);
  2266. end;
  2267. end;
  2268. Fail(E.Message);
  2269. end;
  2270. procedure TCustomTestModule.WriteSources(const aFilename: string; aRow,
  2271. aCol: integer);
  2272. var
  2273. IsSrc: Boolean;
  2274. i, j: Integer;
  2275. SrcLines: TStringList;
  2276. Line: string;
  2277. aModule: TTestEnginePasResolver;
  2278. begin
  2279. writeln('TCustomTestModule.WriteSources File="',aFilename,'" Row=',aRow,' Col=',aCol);
  2280. for i:=0 to ResolverCount-1 do
  2281. begin
  2282. aModule:=Resolvers[i];
  2283. SrcLines:=TStringList.Create;
  2284. try
  2285. SrcLines.Text:=aModule.Source;
  2286. IsSrc:=ExtractFilename(aModule.Filename)=ExtractFileName(aFilename);
  2287. writeln('Testcode:-File="',aModule.Filename,'"----------------------------------:');
  2288. for j:=1 to SrcLines.Count do
  2289. begin
  2290. Line:=SrcLines[j-1];
  2291. if IsSrc and (j=aRow) then
  2292. begin
  2293. write('*');
  2294. Line:=LeftStr(Line,aCol-1)+'|'+copy(Line,aCol,length(Line));
  2295. end;
  2296. writeln(Format('%:4d: ',[j]),Line);
  2297. end;
  2298. finally
  2299. SrcLines.Free;
  2300. end;
  2301. end;
  2302. end;
  2303. function TCustomTestModule.IndexOfResolver(const Filename: string): integer;
  2304. var
  2305. i: Integer;
  2306. begin
  2307. for i:=0 to ResolverCount-1 do
  2308. if Filename=Resolvers[i].Filename then exit(i);
  2309. Result:=-1;
  2310. end;
  2311. function TCustomTestModule.GetResolver(const Filename: string
  2312. ): TTestEnginePasResolver;
  2313. var
  2314. i: Integer;
  2315. begin
  2316. i:=IndexOfResolver(Filename);
  2317. if i<0 then exit(nil);
  2318. Result:=Resolvers[i];
  2319. end;
  2320. function TCustomTestModule.GetDefaultNamespace: string;
  2321. var
  2322. C: TClass;
  2323. begin
  2324. Result:='';
  2325. if FModule=nil then exit;
  2326. C:=FModule.ClassType;
  2327. if (C=TPasProgram) or (C=TPasLibrary) or (C=TPasPackage) then
  2328. Result:=Engine.DefaultNameSpace;
  2329. end;
  2330. constructor TCustomTestModule.Create;
  2331. begin
  2332. inherited Create;
  2333. FHintMsgs:=TObjectList.Create(true);
  2334. FHintMsgsGood:=TFPList.Create;
  2335. end;
  2336. destructor TCustomTestModule.Destroy;
  2337. begin
  2338. FreeAndNil(FHintMsgs);
  2339. FreeAndNil(FHintMsgsGood);
  2340. inherited Destroy;
  2341. end;
  2342. { TTestModule }
  2343. procedure TTestModule.TestReservedWords;
  2344. var
  2345. i: integer;
  2346. begin
  2347. for i:=low(JSReservedWords) to High(JSReservedWords)-1 do
  2348. if CompareStr(JSReservedWords[i],JSReservedWords[i+1])>=0 then
  2349. Fail('20170203135442 '+JSReservedWords[i]+' >= '+JSReservedWords[i+1]);
  2350. for i:=low(JSReservedGlobalWords) to High(JSReservedGlobalWords)-1 do
  2351. if CompareStr(JSReservedGlobalWords[i],JSReservedGlobalWords[i+1])>=0 then
  2352. Fail('20170203135443 '+JSReservedGlobalWords[i]+' >= '+JSReservedGlobalWords[i+1]);
  2353. end;
  2354. procedure TTestModule.TestEmptyProgram;
  2355. begin
  2356. StartProgram(false);
  2357. Add('begin');
  2358. ConvertProgram;
  2359. CheckSource('TestEmptyProgram','','');
  2360. end;
  2361. procedure TTestModule.TestEmptyProgramUseStrict;
  2362. begin
  2363. Converter.Options:=Converter.Options+[coUseStrict];
  2364. StartProgram(false);
  2365. Add('begin');
  2366. ConvertProgram;
  2367. CheckSource('TestEmptyProgramUseStrict','','');
  2368. end;
  2369. procedure TTestModule.TestEmptyUnit;
  2370. begin
  2371. StartUnit(false);
  2372. Add('interface');
  2373. Add('implementation');
  2374. ConvertUnit;
  2375. CheckSource('TestEmptyUnit',
  2376. LinesToStr([
  2377. ]),
  2378. '');
  2379. end;
  2380. procedure TTestModule.TestEmptyUnitUseStrict;
  2381. begin
  2382. Converter.Options:=Converter.Options+[coUseStrict];
  2383. StartUnit(false);
  2384. Add('interface');
  2385. Add('implementation');
  2386. ConvertUnit;
  2387. CheckSource('TestEmptyUnitUseStrict',
  2388. LinesToStr([
  2389. ''
  2390. ]),
  2391. '');
  2392. end;
  2393. procedure TTestModule.TestDottedUnitNames;
  2394. begin
  2395. AddModuleWithIntfImplSrc('NS1.Unit2.pas',
  2396. LinesToStr([
  2397. 'var iV: longint;'
  2398. ]),
  2399. '');
  2400. FFilename:='ns1.test1.pp';
  2401. StartProgram(true);
  2402. Add('uses unIt2;');
  2403. Add('var');
  2404. Add(' i: longint;');
  2405. Add('begin');
  2406. Add(' i:=iv;');
  2407. Add(' i:=uNit2.iv;');
  2408. Add(' i:=Ns1.TEst1.i;');
  2409. ConvertProgram;
  2410. CheckSource('TestDottedUnitNames',
  2411. LinesToStr([
  2412. 'this.i = 0;',
  2413. '']),
  2414. LinesToStr([ // this.$init
  2415. '$mod.i = pas["NS1.Unit2"].iV;',
  2416. '$mod.i = pas["NS1.Unit2"].iV;',
  2417. '$mod.i = $mod.i;',
  2418. '']) );
  2419. end;
  2420. procedure TTestModule.TestDottedUnitNameImpl;
  2421. begin
  2422. AddModuleWithIntfImplSrc('TEST.UnitA.pas',
  2423. LinesToStr([
  2424. 'type',
  2425. ' TObject = class end;',
  2426. ' TTestA = class',
  2427. ' end;'
  2428. ]),
  2429. LinesToStr(['uses TEST.UnitB;'])
  2430. );
  2431. AddModuleWithIntfImplSrc('TEST.UnitB.pas',
  2432. LinesToStr([
  2433. 'uses TEST.UnitA;',
  2434. 'type TTestB = class(TTestA);'
  2435. ]),
  2436. ''
  2437. );
  2438. StartProgram(true);
  2439. Add('uses TEST.UnitA;');
  2440. Add('begin');
  2441. ConvertProgram;
  2442. CheckSource('TestDottedUnitNameImpl',
  2443. LinesToStr([
  2444. '']),
  2445. LinesToStr([ // this.$init
  2446. '']) );
  2447. CheckUnit('TEST.UnitA.pas',
  2448. LinesToStr([
  2449. 'rtl.module("TEST.UnitA", ["system"], function () {',
  2450. ' var $mod = this;',
  2451. ' rtl.createClass(this, "TObject", null, function () {',
  2452. ' this.$init = function () {',
  2453. ' };',
  2454. ' this.$final = function () {',
  2455. ' };',
  2456. ' });',
  2457. ' rtl.createClass(this, "TTestA", this.TObject, function () {',
  2458. ' });',
  2459. '}, ["TEST.UnitB"]);'
  2460. ]));
  2461. CheckUnit('TEST.UnitB.pas',
  2462. LinesToStr([
  2463. 'rtl.module("TEST.UnitB", ["system","TEST.UnitA"], function () {',
  2464. ' var $mod = this;',
  2465. ' rtl.createClass(this, "TTestB", pas["TEST.UnitA"].TTestA, function () {',
  2466. ' });',
  2467. '});'
  2468. ]));
  2469. end;
  2470. procedure TTestModule.TestDottedUnitExpr;
  2471. begin
  2472. AddModuleWithIntfImplSrc('NS2.SubNs2.Unit2.pas',
  2473. LinesToStr([
  2474. 'procedure DoIt;'
  2475. ]),
  2476. 'procedure DoIt; begin end;');
  2477. FFilename:='Ns1.SubNs1.Test1.pp';
  2478. StartProgram(true);
  2479. Add('uses Ns2.sUbnS2.unIt2;');
  2480. Add('var');
  2481. Add(' i: longint;');
  2482. Add('begin');
  2483. Add(' ns2.subns2.unit2.doit;');
  2484. Add(' i:=Ns1.SubNS1.TEst1.i;');
  2485. ConvertProgram;
  2486. CheckSource('TestDottedUnitExpr',
  2487. LinesToStr([
  2488. 'this.i = 0;',
  2489. '']),
  2490. LinesToStr([ // this.$init
  2491. 'pas["NS2.SubNs2.Unit2"].DoIt();',
  2492. '$mod.i = $mod.i;',
  2493. '']) );
  2494. end;
  2495. procedure TTestModule.Test_ModeFPCFail;
  2496. begin
  2497. StartProgram(false);
  2498. Add('{$mode FPC}');
  2499. Add('begin');
  2500. SetExpectedScannerError('Invalid mode: "FPC"',nErrInvalidMode);
  2501. ConvertProgram;
  2502. end;
  2503. procedure TTestModule.Test_ModeSwitchCBlocksFail;
  2504. begin
  2505. StartProgram(false);
  2506. Add('{$modeswitch cblocks-}');
  2507. Add('begin');
  2508. ConvertProgram;
  2509. CheckHint(mtWarning,nErrInvalidModeSwitch,'Warning: test1.pp(3,23) : Invalid mode switch: "cblocks"');
  2510. CheckResolverUnexpectedHints();
  2511. end;
  2512. procedure TTestModule.TestUnit_UseSystem;
  2513. begin
  2514. StartUnit(true);
  2515. Add([
  2516. 'interface',
  2517. 'var i: integer;',
  2518. 'implementation']);
  2519. ConvertUnit;
  2520. CheckSource('TestUnit_UseSystem',
  2521. LinesToStr([
  2522. 'this.i = 0;',
  2523. '']),
  2524. LinesToStr([
  2525. '']) );
  2526. end;
  2527. procedure TTestModule.TestUnit_Intf1Impl2Intf1;
  2528. begin
  2529. AddModuleWithIntfImplSrc('unit1.pp',
  2530. LinesToStr([
  2531. 'type number = longint;']),
  2532. LinesToStr([
  2533. 'uses test1;',
  2534. 'procedure DoIt;',
  2535. 'begin',
  2536. ' i:=3;',
  2537. 'end;']));
  2538. StartUnit(true);
  2539. Add([
  2540. 'interface',
  2541. 'uses unit1;',
  2542. 'var i: number;',
  2543. 'implementation']);
  2544. ConvertUnit;
  2545. CheckSource('TestUnit_Intf1Impl2Intf1',
  2546. LinesToStr([
  2547. 'this.i = 0;',
  2548. '']),
  2549. LinesToStr([
  2550. '']) );
  2551. end;
  2552. procedure TTestModule.TestIncludeVersion;
  2553. begin
  2554. StartProgram(false);
  2555. Add([
  2556. 'var',
  2557. ' s: string;',
  2558. ' i: word;',
  2559. 'begin',
  2560. ' s:={$I %line%};',
  2561. ' i:={$I %linenum%};',
  2562. ' s:={$I %currentroutine%};',
  2563. ' s:={$I %pas2jsversion%};',
  2564. ' s:={$I %pas2jstarget%};',
  2565. ' s:={$I %pas2jstargetos%};',
  2566. ' s:={$I %pas2jstargetcpu%};',
  2567. ' s:={$I %file%};',
  2568. '']);
  2569. ConvertProgram;
  2570. CheckSource('TestIncludeVersion',
  2571. LinesToStr([
  2572. 'this.s="";',
  2573. 'this.i = 0;']),
  2574. LinesToStr([
  2575. '$mod.s = "7";',
  2576. '$mod.i = 8;',
  2577. '$mod.s = "<anonymous>";',
  2578. '$mod.s = "Comp.Ver.tcmodules";',
  2579. '$mod.s = "Browser";',
  2580. '$mod.s = "Browser";',
  2581. '$mod.s = "ECMAScript5";',
  2582. '$mod.s = "test1.pp";',
  2583. '']));
  2584. end;
  2585. procedure TTestModule.TestVarInt;
  2586. begin
  2587. StartProgram(false);
  2588. Add('var MyI: longint;');
  2589. Add('begin');
  2590. ConvertProgram;
  2591. CheckSource('TestVarInt','this.MyI=0;','');
  2592. end;
  2593. procedure TTestModule.TestVarBaseTypes;
  2594. begin
  2595. StartProgram(false);
  2596. Add('var');
  2597. Add(' i: longint;');
  2598. Add(' s: string;');
  2599. Add(' c: char;');
  2600. Add(' b: boolean;');
  2601. Add(' d: double;');
  2602. Add(' i2: longint = 3;');
  2603. Add(' s2: string = ''foo'';');
  2604. Add(' c2: char = ''4'';');
  2605. Add(' b2: boolean = true;');
  2606. Add(' d2: double = 5.6;');
  2607. Add(' i3: longint = $707;');
  2608. Add(' i4: nativeint = 9007199254740991;');
  2609. Add(' i5: nativeint = -9007199254740991-1;');
  2610. Add(' i6: nativeint = $fffffffffffff;');
  2611. Add(' i7: nativeint = -$fffffffffffff-1;');
  2612. Add(' i8: byte = 00;');
  2613. Add(' u8: nativeuint = $fffffffffffff;');
  2614. Add(' u9: nativeuint = $0000000000000;');
  2615. Add(' u10: nativeuint = $00ff00;');
  2616. Add('begin');
  2617. ConvertProgram;
  2618. CheckSource('TestVarBaseTypes',
  2619. LinesToStr([
  2620. 'this.i = 0;',
  2621. 'this.s = "";',
  2622. 'this.c = "";',
  2623. 'this.b = false;',
  2624. 'this.d = 0.0;',
  2625. 'this.i2 = 3;',
  2626. 'this.s2 = "foo";',
  2627. 'this.c2 = "4";',
  2628. 'this.b2 = true;',
  2629. 'this.d2 = 5.6;',
  2630. 'this.i3 = 0x707;',
  2631. 'this.i4 = 9007199254740991;',
  2632. 'this.i5 = -9007199254740991-1;',
  2633. 'this.i6 = 0xfffffffffffff;',
  2634. 'this.i7 =-0xfffffffffffff-1;',
  2635. 'this.i8 = 0;',
  2636. 'this.u8 = 0xfffffffffffff;',
  2637. 'this.u9 = 0x0;',
  2638. 'this.u10 = 0xff00;'
  2639. ]),
  2640. '');
  2641. end;
  2642. procedure TTestModule.TestBaseTypeSingleFail;
  2643. begin
  2644. StartProgram(false);
  2645. Add('var s: single;');
  2646. SetExpectedPasResolverError('identifier not found "single"',PasResolveEval.nIdentifierNotFound);
  2647. ConvertProgram;
  2648. end;
  2649. procedure TTestModule.TestBaseTypeExtendedFail;
  2650. begin
  2651. StartProgram(false);
  2652. Add('var e: extended;');
  2653. SetExpectedPasResolverError('identifier not found "extended"',PasResolveEval.nIdentifierNotFound);
  2654. ConvertProgram;
  2655. end;
  2656. procedure TTestModule.TestConstBaseTypes;
  2657. begin
  2658. StartProgram(false);
  2659. Add('const');
  2660. Add(' i: longint = 3;');
  2661. Add(' s: string = ''foo'';');
  2662. Add(' c: char = ''4'';');
  2663. Add(' b: boolean = true;');
  2664. Add(' d: double = 5.6;');
  2665. Add(' e = low(word);');
  2666. Add(' f = high(word);');
  2667. Add('begin');
  2668. ConvertProgram;
  2669. CheckSource('TestVarBaseTypes',
  2670. LinesToStr([
  2671. 'this.i=3;',
  2672. 'this.s="foo";',
  2673. 'this.c="4";',
  2674. 'this.b=true;',
  2675. 'this.d=5.6;',
  2676. 'this.e = 0;',
  2677. 'this.f = 65535;'
  2678. ]),
  2679. '');
  2680. end;
  2681. procedure TTestModule.TestAliasTypeRef;
  2682. begin
  2683. StartProgram(false);
  2684. Add('type');
  2685. Add(' a=longint;');
  2686. Add(' b=a;');
  2687. Add('var');
  2688. Add(' c: A;');
  2689. Add(' d: B;');
  2690. Add('begin');
  2691. ConvertProgram;
  2692. CheckSource('TestAliasTypeRef',
  2693. LinesToStr([ // statements
  2694. 'this.c = 0;',
  2695. 'this.d = 0;'
  2696. ]),
  2697. LinesToStr([ // this.$main
  2698. ''
  2699. ]));
  2700. end;
  2701. procedure TTestModule.TestTypeCast_BaseTypes;
  2702. begin
  2703. StartProgram(false);
  2704. Add([
  2705. 'var',
  2706. ' i: longint;',
  2707. ' b: boolean;',
  2708. ' d: double;',
  2709. ' s: string;',
  2710. ' c: char;',
  2711. 'begin',
  2712. ' i:=longint(i);',
  2713. ' i:=longint(b);',
  2714. ' b:=boolean(b);',
  2715. ' b:=boolean(i);',
  2716. ' d:=double(d);',
  2717. ' d:=double(i);',
  2718. ' s:=string(s);',
  2719. ' s:=string(c);',
  2720. ' c:=char(c);',
  2721. ' c:=char(i);',
  2722. ' c:=char(65);',
  2723. ' c:=char(#10);',
  2724. ' c:=char(#$E000);',
  2725. '']);
  2726. ConvertProgram;
  2727. CheckSource('TestAliasTypeRef',
  2728. LinesToStr([ // statements
  2729. 'this.i = 0;',
  2730. 'this.b = false;',
  2731. 'this.d = 0.0;',
  2732. 'this.s = "";',
  2733. 'this.c = "";',
  2734. '']),
  2735. LinesToStr([ // this.$main
  2736. '$mod.i = $mod.i;',
  2737. '$mod.i = ($mod.b ? 1 : 0);',
  2738. '$mod.b = $mod.b;',
  2739. '$mod.b = $mod.i != 0;',
  2740. '$mod.d = $mod.d;',
  2741. '$mod.d = $mod.i;',
  2742. '$mod.s = $mod.s;',
  2743. '$mod.s = $mod.c;',
  2744. '$mod.c = $mod.c;',
  2745. '$mod.c = String.fromCharCode($mod.i);',
  2746. '$mod.c = "A";',
  2747. '$mod.c = "\n";',
  2748. '$mod.c = "";',
  2749. '']));
  2750. end;
  2751. procedure TTestModule.TestTypeCast_AliasBaseTypes;
  2752. begin
  2753. StartProgram(false);
  2754. Add('type');
  2755. Add(' integer = longint;');
  2756. Add(' TYesNo = boolean;');
  2757. Add(' TFloat = double;');
  2758. Add(' TCaption = string;');
  2759. Add(' TChar = char;');
  2760. Add('var');
  2761. Add(' i: integer;');
  2762. Add(' b: TYesNo;');
  2763. Add(' d: TFloat;');
  2764. Add(' s: TCaption;');
  2765. Add(' c: TChar;');
  2766. Add('begin');
  2767. Add(' i:=integer(i);');
  2768. Add(' i:=integer(b);');
  2769. Add(' b:=TYesNo(b);');
  2770. Add(' b:=TYesNo(i);');
  2771. Add(' d:=TFloat(d);');
  2772. Add(' d:=TFloat(i);');
  2773. Add(' s:=TCaption(s);');
  2774. Add(' s:=TCaption(c);');
  2775. Add(' c:=TChar(c);');
  2776. ConvertProgram;
  2777. CheckSource('TestAliasTypeRef',
  2778. LinesToStr([ // statements
  2779. 'this.i = 0;',
  2780. 'this.b = false;',
  2781. 'this.d = 0.0;',
  2782. 'this.s = "";',
  2783. 'this.c = "";',
  2784. '']),
  2785. LinesToStr([ // this.$main
  2786. '$mod.i = $mod.i;',
  2787. '$mod.i = ($mod.b ? 1 : 0);',
  2788. '$mod.b = $mod.b;',
  2789. '$mod.b = $mod.i != 0;',
  2790. '$mod.d = $mod.d;',
  2791. '$mod.d = $mod.i;',
  2792. '$mod.s = $mod.s;',
  2793. '$mod.s = $mod.c;',
  2794. '$mod.c = $mod.c;',
  2795. '']));
  2796. end;
  2797. procedure TTestModule.TestEmptyProc;
  2798. begin
  2799. StartProgram(false);
  2800. Add('procedure Test;');
  2801. Add('begin');
  2802. Add('end;');
  2803. Add('begin');
  2804. ConvertProgram;
  2805. CheckSource('TestEmptyProc',
  2806. LinesToStr([ // statements
  2807. 'this.Test = function () {',
  2808. '};'
  2809. ]),
  2810. LinesToStr([ // this.$main
  2811. ''
  2812. ]));
  2813. end;
  2814. procedure TTestModule.TestProcOneParam;
  2815. begin
  2816. StartProgram(false);
  2817. Add('procedure ProcA(i: longint);');
  2818. Add('begin');
  2819. Add('end;');
  2820. Add('begin');
  2821. Add(' PROCA(3);');
  2822. ConvertProgram;
  2823. CheckSource('TestProcOneParam',
  2824. LinesToStr([ // statements
  2825. 'this.ProcA = function (i) {',
  2826. '};'
  2827. ]),
  2828. LinesToStr([ // this.$main
  2829. '$mod.ProcA(3);'
  2830. ]));
  2831. end;
  2832. procedure TTestModule.TestFunctionWithoutParams;
  2833. begin
  2834. StartProgram(false);
  2835. Add('function FuncA: longint;');
  2836. Add('begin');
  2837. Add('end;');
  2838. Add('var i: longint;');
  2839. Add('begin');
  2840. Add(' I:=FUNCA();');
  2841. Add(' I:=FUNCA;');
  2842. Add(' FUNCA();');
  2843. Add(' FUNCA;');
  2844. ConvertProgram;
  2845. CheckSource('TestProcWithoutParams',
  2846. LinesToStr([ // statements
  2847. 'this.FuncA = function () {',
  2848. ' var Result = 0;',
  2849. ' return Result;',
  2850. '};',
  2851. 'this.i=0;'
  2852. ]),
  2853. LinesToStr([ // this.$main
  2854. '$mod.i=$mod.FuncA();',
  2855. '$mod.i=$mod.FuncA();',
  2856. '$mod.FuncA();',
  2857. '$mod.FuncA();'
  2858. ]));
  2859. end;
  2860. procedure TTestModule.TestProcedureWithoutParams;
  2861. begin
  2862. StartProgram(false);
  2863. Add('procedure ProcA;');
  2864. Add('begin');
  2865. Add('end;');
  2866. Add('begin');
  2867. Add(' PROCA();');
  2868. Add(' PROCA;');
  2869. ConvertProgram;
  2870. CheckSource('TestProcWithoutParams',
  2871. LinesToStr([ // statements
  2872. 'this.ProcA = function () {',
  2873. '};'
  2874. ]),
  2875. LinesToStr([ // this.$main
  2876. '$mod.ProcA();',
  2877. '$mod.ProcA();'
  2878. ]));
  2879. end;
  2880. procedure TTestModule.TestIncDec;
  2881. begin
  2882. StartProgram(false);
  2883. Add([
  2884. 'procedure DoIt(var i: longint);',
  2885. 'begin',
  2886. ' inc(i);',
  2887. ' inc(i,2);',
  2888. 'end;',
  2889. 'var',
  2890. ' Bar: longint;',
  2891. 'begin',
  2892. ' inc(bar);',
  2893. ' inc(bar,2);',
  2894. ' dec(bar);',
  2895. ' dec(bar,3);',
  2896. '']);
  2897. ConvertProgram;
  2898. CheckSource('TestIncDec',
  2899. LinesToStr([ // statements
  2900. 'this.DoIt = function (i) {',
  2901. ' i.set(i.get()+1);',
  2902. ' i.set(i.get()+2);',
  2903. '};',
  2904. 'this.Bar = 0;'
  2905. ]),
  2906. LinesToStr([ // this.$main
  2907. '$mod.Bar+=1;',
  2908. '$mod.Bar+=2;',
  2909. '$mod.Bar-=1;',
  2910. '$mod.Bar-=3;'
  2911. ]));
  2912. end;
  2913. procedure TTestModule.TestLoHiFpcMode;
  2914. begin
  2915. StartProgram(false);
  2916. Add([
  2917. '{$mode objfpc}',
  2918. 'const',
  2919. ' LoByte1 = Lo(Word($1234));',
  2920. ' HiByte1 = Hi(Word($1234));',
  2921. ' LoByte2 = Lo(SmallInt($1234));',
  2922. ' HiByte2 = Hi(SmallInt($1234));',
  2923. ' LoWord1 = Lo($1234CDEF);',
  2924. ' HiWord1 = Hi($1234CDEF);',
  2925. ' LoWord2 = Lo(-$1234CDEF);',
  2926. ' HiWord2 = Hi(-$1234CDEF);',
  2927. ' lo4:byte=lo(byte($34));',
  2928. ' hi4:byte=hi(byte($34));',
  2929. ' lo5:byte=lo(shortint(-$34));',
  2930. ' hi5:byte=hi(shortint(-$34));',
  2931. ' lo6:longword=lo($123456789ABCD);',
  2932. ' hi6:longword=hi($123456789ABCD);',
  2933. ' lo7:longword=lo(-$123456789ABCD);',
  2934. ' hi7:longword=hi(-$123456789ABCD);',
  2935. 'var',
  2936. ' b: Byte;',
  2937. ' ss: shortint;',
  2938. ' w: Word;',
  2939. ' si: SmallInt;',
  2940. ' lw: LongWord;',
  2941. ' li: LongInt;',
  2942. ' b2: Byte;',
  2943. ' ni: nativeint;',
  2944. 'begin',
  2945. ' w := $1234;',
  2946. ' ss := -$12;',
  2947. ' b := lo(ss);',
  2948. ' b := HI(ss);',
  2949. ' b := lo(w);',
  2950. ' b := HI(w);',
  2951. ' b2 := lo(b);',
  2952. ' b2 := hi(b);',
  2953. ' lw := $1234CDEF;',
  2954. ' w := lo(lw);',
  2955. ' w := hi(lw);',
  2956. ' ni := $123456789ABCD;',
  2957. ' lw := lo(ni);',
  2958. ' lw := hi(ni);',
  2959. '']);
  2960. ConvertProgram;
  2961. CheckSource('TestLoHiFpcMode',
  2962. LinesToStr([ // statements
  2963. 'this.LoByte1 = 0x1234 & 0xFF;',
  2964. 'this.HiByte1 = (0x1234 >> 8) & 0xFF;',
  2965. 'this.LoByte2 = 0x1234 & 0xFF;',
  2966. 'this.HiByte2 = (0x1234 >> 8) & 0xFF;',
  2967. 'this.LoWord1 = 0x1234CDEF & 0xFFFF;',
  2968. 'this.HiWord1 = (0x1234CDEF >> 16) & 0xFFFF;',
  2969. 'this.LoWord2 = -0x1234CDEF & 0xFFFF;',
  2970. 'this.HiWord2 = (-0x1234CDEF >> 16) & 0xFFFF;',
  2971. 'this.lo4 = 0x34 & 0xF;',
  2972. 'this.hi4 = (0x34 >> 4) & 0xF;',
  2973. 'this.lo5 = (((-0x34 & 255) << 24) >> 24) & 0xFF;',
  2974. 'this.hi5 = ((((-0x34 & 255) << 24) >> 24) >> 8) & 0xFF;',
  2975. 'this.lo6 = 0x123456789ABCD >>> 0;',
  2976. 'this.hi6 = 74565 >>> 0;',
  2977. 'this.lo7 = -0x123456789ABCD >>> 0;',
  2978. 'this.hi7 = Math.floor(-0x123456789ABCD / 4294967296) >>> 0;',
  2979. 'this.b = 0;',
  2980. 'this.ss = 0;',
  2981. 'this.w = 0;',
  2982. 'this.si = 0;',
  2983. 'this.lw = 0;',
  2984. 'this.li = 0;',
  2985. 'this.b2 = 0;',
  2986. 'this.ni = 0;',
  2987. '']),
  2988. LinesToStr([ // this.$main
  2989. '$mod.w = 0x1234;',
  2990. '$mod.ss = -0x12;',
  2991. '$mod.b = $mod.ss & 0xFF;',
  2992. '$mod.b = ($mod.ss >> 8) & 0xFF;',
  2993. '$mod.b = $mod.w & 0xFF;',
  2994. '$mod.b = ($mod.w >> 8) & 0xFF;',
  2995. '$mod.b2 = $mod.b & 0xF;',
  2996. '$mod.b2 = ($mod.b >> 4) & 0xF;',
  2997. '$mod.lw = 0x1234CDEF;',
  2998. '$mod.w = $mod.lw & 0xFFFF;',
  2999. '$mod.w = ($mod.lw >> 16) & 0xFFFF;',
  3000. '$mod.ni = 0x123456789ABCD;',
  3001. '$mod.lw = $mod.ni >>> 0;',
  3002. '$mod.lw = Math.floor($mod.ni / 4294967296) >>> 0;',
  3003. '']));
  3004. end;
  3005. procedure TTestModule.TestLoHiDelphiMode;
  3006. begin
  3007. StartProgram(false);
  3008. Add([
  3009. '{$mode delphi}',
  3010. 'const',
  3011. ' LoByte1 = Lo(Word($1234));',
  3012. ' HiByte1 = Hi(Word($1234));',
  3013. ' LoByte2 = Lo(SmallInt($1234));',
  3014. ' HiByte2 = Hi(SmallInt($1234));',
  3015. ' LoByte3 = Lo($1234CDEF);',
  3016. ' HiByte3 = Hi($1234CDEF);',
  3017. ' LoByte4 = Lo(-$1234CDEF);',
  3018. ' HiByte4 = Hi(-$1234CDEF);',
  3019. 'var',
  3020. ' b: Byte;',
  3021. ' w: Word;',
  3022. ' si: SmallInt;',
  3023. ' lw: LongWord;',
  3024. ' li: LongInt;',
  3025. 'begin',
  3026. ' w := $1234;',
  3027. ' b := lo(w);',
  3028. ' b := HI(w);',
  3029. ' lw := $1234CDEF;',
  3030. ' b := lo(lw);',
  3031. ' b := hi(lw);',
  3032. '']);
  3033. ConvertProgram;
  3034. CheckSource('TestLoHiDelphiMode',
  3035. LinesToStr([ // statements
  3036. 'this.LoByte1 = 0x1234 & 0xFF;',
  3037. 'this.HiByte1 = (0x1234 >> 8) & 0xFF;',
  3038. 'this.LoByte2 = 0x1234 & 0xFF;',
  3039. 'this.HiByte2 = (0x1234 >> 8) & 0xFF;',
  3040. 'this.LoByte3 = 0x1234CDEF & 0xFF;',
  3041. 'this.HiByte3 = (0x1234CDEF >> 8) & 0xFF;',
  3042. 'this.LoByte4 = -0x1234CDEF & 0xFF;',
  3043. 'this.HiByte4 = (-0x1234CDEF >> 8) & 0xFF;',
  3044. 'this.b = 0;',
  3045. 'this.w = 0;',
  3046. 'this.si = 0;',
  3047. 'this.lw = 0;',
  3048. 'this.li = 0;'
  3049. ]),
  3050. LinesToStr([ // this.$main
  3051. '$mod.w = 0x1234;',
  3052. '$mod.b = $mod.w & 0xFF;',
  3053. '$mod.b = ($mod.w >> 8) & 0xFF;',
  3054. '$mod.lw = 0x1234CDEF;',
  3055. '$mod.b = $mod.lw & 0xFF;',
  3056. '$mod.b = ($mod.lw >> 8) & 0xFF;'
  3057. ]));
  3058. end;
  3059. procedure TTestModule.TestAssignments;
  3060. begin
  3061. StartProgram(false);
  3062. Parser.Options:=Parser.Options+[po_cassignments];
  3063. Add('var');
  3064. Add(' Bar:longint;');
  3065. Add('begin');
  3066. Add(' bar:=3;');
  3067. Add(' bar+=4;');
  3068. Add(' bar-=5;');
  3069. Add(' bar*=6;');
  3070. ConvertProgram;
  3071. CheckSource('TestAssignments',
  3072. LinesToStr([ // statements
  3073. 'this.Bar = 0;'
  3074. ]),
  3075. LinesToStr([ // this.$main
  3076. '$mod.Bar=3;',
  3077. '$mod.Bar+=4;',
  3078. '$mod.Bar-=5;',
  3079. '$mod.Bar*=6;'
  3080. ]));
  3081. end;
  3082. procedure TTestModule.TestArithmeticOperators1;
  3083. begin
  3084. StartProgram(false);
  3085. Add('var');
  3086. Add(' vA,vB,vC:longint;');
  3087. Add('begin');
  3088. Add(' va:=1;');
  3089. Add(' vb:=va+va;');
  3090. Add(' vb:=va div vb;');
  3091. Add(' vb:=va mod vb;');
  3092. Add(' vb:=va+va*vb+va div vb;');
  3093. Add(' vc:=-va;');
  3094. Add(' va:=va-vb;');
  3095. Add(' vb:=va;');
  3096. Add(' if va<vb then vc:=va else vc:=vb;');
  3097. ConvertProgram;
  3098. CheckSource('TestArithmeticOperators1',
  3099. LinesToStr([ // statements
  3100. 'this.vA = 0;',
  3101. 'this.vB = 0;',
  3102. 'this.vC = 0;'
  3103. ]),
  3104. LinesToStr([ // this.$main
  3105. '$mod.vA = 1;',
  3106. '$mod.vB = $mod.vA + $mod.vA;',
  3107. '$mod.vB = rtl.trunc($mod.vA / $mod.vB);',
  3108. '$mod.vB = $mod.vA % $mod.vB;',
  3109. '$mod.vB = $mod.vA + ($mod.vA * $mod.vB) + rtl.trunc($mod.vA / $mod.vB);',
  3110. '$mod.vC = -$mod.vA;',
  3111. '$mod.vA = $mod.vA - $mod.vB;',
  3112. '$mod.vB = $mod.vA;',
  3113. 'if ($mod.vA < $mod.vB){ $mod.vC = $mod.vA } else $mod.vC = $mod.vB;'
  3114. ]));
  3115. end;
  3116. procedure TTestModule.TestMultiAdd;
  3117. begin
  3118. StartProgram(false);
  3119. Add([
  3120. 'function Fly: string; external name ''fly'';',
  3121. 'function TryEncodeDate(Year, Month, Day: Word): Boolean;',
  3122. 'var',
  3123. ' Date: double;',
  3124. 'begin',
  3125. ' Result:=(Year>0) and (Year<10000) and',
  3126. ' (Month >= 1) and (Month<=12) and',
  3127. ' (Day>0) and (Day<=31);',
  3128. ' Date := (146097*Year) SHR 2 + (1461*Year) SHR 2 + (153*LongWord(Month)+2) DIV 5 + LongWord(Day);',
  3129. 'end;',
  3130. 'var s: string;',
  3131. 'begin',
  3132. ' s:=''a''+''b''+''c''+''d'';',
  3133. ' s:=s+Fly+''e'';',
  3134. ' s:=Fly+Fly+Fly;',
  3135. '']);
  3136. ConvertProgram;
  3137. CheckSource('TestMultiAdd',
  3138. LinesToStr([ // statements
  3139. 'this.TryEncodeDate = function (Year, Month, Day) {',
  3140. ' var Result = false;',
  3141. ' var date = 0.0;',
  3142. ' Result = (Year > 0) && (Year < 10000) && (Month >= 1) && (Month <= 12) && (Day > 0) && (Day <= 31);',
  3143. ' date = ((146097 * Year) >>> 2) + ((1461 * Year) >>> 2) + rtl.trunc(((153 * Month) + 2) / 5) + Day;',
  3144. ' return Result;',
  3145. '};',
  3146. 'this.s = "";',
  3147. '']),
  3148. LinesToStr([ // this.$main
  3149. '$mod.s = "a" + "b" + "c" + "d";',
  3150. '$mod.s = $mod.s + fly() + "e";',
  3151. '$mod.s = fly() + fly() + fly();',
  3152. '']));
  3153. end;
  3154. procedure TTestModule.TestLogicalOperators;
  3155. begin
  3156. StartProgram(false);
  3157. Add('var');
  3158. Add(' vA,vB,vC:boolean;');
  3159. Add('begin');
  3160. Add(' va:=vb and vc;');
  3161. Add(' va:=vb or vc;');
  3162. Add(' va:=vb xor vc;');
  3163. Add(' va:=true and vc;');
  3164. Add(' va:=(vb and vc) or (va and vb);');
  3165. Add(' va:=not vb;');
  3166. ConvertProgram;
  3167. CheckSource('TestLogicalOperators',
  3168. LinesToStr([ // statements
  3169. 'this.vA = false;',
  3170. 'this.vB = false;',
  3171. 'this.vC = false;'
  3172. ]),
  3173. LinesToStr([ // this.$main
  3174. '$mod.vA = $mod.vB && $mod.vC;',
  3175. '$mod.vA = $mod.vB || $mod.vC;',
  3176. '$mod.vA = $mod.vB ^ $mod.vC;',
  3177. '$mod.vA = true && $mod.vC;',
  3178. '$mod.vA = ($mod.vB && $mod.vC) || ($mod.vA && $mod.vB);',
  3179. '$mod.vA = !$mod.vB;'
  3180. ]));
  3181. end;
  3182. procedure TTestModule.TestBitwiseOperators;
  3183. begin
  3184. StartProgram(false);
  3185. Add([
  3186. 'var',
  3187. ' vA,vB,vC:longint;',
  3188. ' X,Y,Z: nativeint;',
  3189. 'begin',
  3190. ' va:=vb and vc;',
  3191. ' va:=vb or vc;',
  3192. ' va:=vb xor vc;',
  3193. ' va:=vb shl vc;',
  3194. ' va:=vb shr vc;',
  3195. ' va:=3 and vc;',
  3196. ' va:=(vb and vc) or (va and vb);',
  3197. ' va:=not vb;',
  3198. ' X:=Y and Z;',
  3199. ' X:=Y and va;',
  3200. ' X:=Y or Z;',
  3201. ' X:=Y or va;',
  3202. ' X:=Y xor Z;',
  3203. ' X:=Y xor va;',
  3204. '']);
  3205. ConvertProgram;
  3206. CheckSource('TestBitwiseOperators',
  3207. LinesToStr([ // statements
  3208. 'this.vA = 0;',
  3209. 'this.vB = 0;',
  3210. 'this.vC = 0;',
  3211. 'this.X = 0;',
  3212. 'this.Y = 0;',
  3213. 'this.Z = 0;',
  3214. '']),
  3215. LinesToStr([ // this.$main
  3216. '$mod.vA = $mod.vB & $mod.vC;',
  3217. '$mod.vA = $mod.vB | $mod.vC;',
  3218. '$mod.vA = $mod.vB ^ $mod.vC;',
  3219. '$mod.vA = $mod.vB << $mod.vC;',
  3220. '$mod.vA = $mod.vB >>> $mod.vC;',
  3221. '$mod.vA = 3 & $mod.vC;',
  3222. '$mod.vA = ($mod.vB & $mod.vC) | ($mod.vA & $mod.vB);',
  3223. '$mod.vA = ~$mod.vB;',
  3224. '$mod.X = rtl.and($mod.Y, $mod.Z);',
  3225. '$mod.X = $mod.Y & $mod.vA;',
  3226. '$mod.X = rtl.or($mod.Y, $mod.Z);',
  3227. '$mod.X = rtl.or($mod.Y, $mod.vA);',
  3228. '$mod.X = rtl.xor($mod.Y, $mod.Z);',
  3229. '$mod.X = rtl.xor($mod.Y, $mod.vA);',
  3230. '']));
  3231. end;
  3232. procedure TTestModule.TestBitwiseOperatorsLongword;
  3233. begin
  3234. StartProgram(false);
  3235. Add([
  3236. 'var',
  3237. ' a,b,c:longword;',
  3238. ' i: longint;',
  3239. 'begin',
  3240. ' a:=$12345678;',
  3241. ' b:=$EDCBA987;',
  3242. ' c:=not a;',
  3243. ' c:=a and b;',
  3244. ' c:=a and $ffff0000;',
  3245. ' c:=a or b;',
  3246. ' c:=a or $ff00ff00;',
  3247. ' c:=a xor b;',
  3248. ' c:=a xor $f0f0f0f0;',
  3249. ' c:=a shl 1;',
  3250. ' c:=a shl 16;',
  3251. ' c:=a shl 24;',
  3252. ' c:=a shl b;',
  3253. ' c:=a shr 1;',
  3254. ' c:=a shr 16;',
  3255. ' c:=a shr 24;',
  3256. ' c:=a shr b;',
  3257. ' c:=(b and c) or (a and b);',
  3258. ' c:=i and a;',
  3259. ' c:=i or a;',
  3260. ' c:=i xor a;',
  3261. '']);
  3262. ConvertProgram;
  3263. CheckSource('TestBitwiseOperatorsLongword',
  3264. LinesToStr([ // statements
  3265. 'this.a = 0;',
  3266. 'this.b = 0;',
  3267. 'this.c = 0;',
  3268. 'this.i = 0;',
  3269. '']),
  3270. LinesToStr([ // this.$main
  3271. '$mod.a = 0x12345678;',
  3272. '$mod.b = 0xEDCBA987;',
  3273. '$mod.c = rtl.lw(~$mod.a);',
  3274. '$mod.c = rtl.lw($mod.a & $mod.b);',
  3275. '$mod.c = rtl.lw($mod.a & 0xffff0000);',
  3276. '$mod.c = rtl.lw($mod.a | $mod.b);',
  3277. '$mod.c = rtl.lw($mod.a | 0xff00ff00);',
  3278. '$mod.c = rtl.lw($mod.a ^ $mod.b);',
  3279. '$mod.c = rtl.lw($mod.a ^ 0xf0f0f0f0);',
  3280. '$mod.c = rtl.lw($mod.a << 1);',
  3281. '$mod.c = rtl.lw($mod.a << 16);',
  3282. '$mod.c = rtl.lw($mod.a << 24);',
  3283. '$mod.c = rtl.lw($mod.a << $mod.b);',
  3284. '$mod.c = rtl.lw($mod.a >>> 1);',
  3285. '$mod.c = rtl.lw($mod.a >>> 16);',
  3286. '$mod.c = rtl.lw($mod.a >>> 24);',
  3287. '$mod.c = rtl.lw($mod.a >>> $mod.b);',
  3288. '$mod.c = rtl.lw(rtl.lw($mod.b & $mod.c) | rtl.lw($mod.a & $mod.b));',
  3289. '$mod.c = $mod.i & $mod.a;',
  3290. '$mod.c = $mod.i | $mod.a;',
  3291. '$mod.c = $mod.i ^ $mod.a;',
  3292. '']));
  3293. end;
  3294. procedure TTestModule.TestPrgProcVar;
  3295. begin
  3296. StartProgram(false);
  3297. Add('procedure Proc1;');
  3298. Add('type');
  3299. Add(' t1=longint;');
  3300. Add('var');
  3301. Add(' vA:t1;');
  3302. Add('begin');
  3303. Add('end;');
  3304. Add('begin');
  3305. ConvertProgram;
  3306. CheckSource('TestPrgProcVar',
  3307. LinesToStr([ // statements
  3308. 'this.Proc1 = function () {',
  3309. ' var vA=0;',
  3310. '};'
  3311. ]),
  3312. LinesToStr([ // this.$main
  3313. ''
  3314. ]));
  3315. end;
  3316. procedure TTestModule.TestUnitProcVar;
  3317. begin
  3318. StartUnit(false);
  3319. Add('interface');
  3320. Add('');
  3321. Add('type tA=string; // unit scope');
  3322. Add('procedure Proc1;');
  3323. Add('');
  3324. Add('implementation');
  3325. Add('');
  3326. Add('procedure Proc1;');
  3327. Add('type tA=longint; // local proc scope');
  3328. Add('var v1:tA; // using local tA');
  3329. Add('begin');
  3330. Add('end;');
  3331. Add('var v2:tA; // using interface tA');
  3332. ConvertUnit;
  3333. CheckSource('TestUnitProcVar',
  3334. LinesToStr([ // statements
  3335. 'var $impl = $mod.$impl;',
  3336. 'this.Proc1 = function () {',
  3337. ' var v1 = 0;',
  3338. '};',
  3339. '']),
  3340. // this.$init
  3341. '',
  3342. // implementation
  3343. LinesToStr([
  3344. '$impl.v2 = "";',
  3345. '']));
  3346. end;
  3347. procedure TTestModule.TestImplProc;
  3348. begin
  3349. StartUnit(false);
  3350. Add('interface');
  3351. Add('');
  3352. Add('procedure Proc1;');
  3353. Add('');
  3354. Add('implementation');
  3355. Add('');
  3356. Add('procedure Proc1; begin end;');
  3357. Add('procedure Proc2; begin end;');
  3358. Add('initialization');
  3359. Add(' Proc1;');
  3360. Add(' Proc2;');
  3361. ConvertUnit;
  3362. CheckSource('TestImplProc',
  3363. LinesToStr([ // statements
  3364. 'var $impl = $mod.$impl;',
  3365. 'this.Proc1 = function () {',
  3366. '};',
  3367. '']),
  3368. LinesToStr([ // this.$init
  3369. '$mod.Proc1();',
  3370. '$impl.Proc2();',
  3371. '']),
  3372. LinesToStr([ // implementation
  3373. '$impl.Proc2 = function () {',
  3374. '};',
  3375. ''])
  3376. );
  3377. end;
  3378. procedure TTestModule.TestFunctionResult;
  3379. begin
  3380. StartProgram(false);
  3381. Add('function Func1: longint;');
  3382. Add('begin');
  3383. Add(' Result:=3;');
  3384. Add(' Func1:=4;');
  3385. Add('end;');
  3386. Add('begin');
  3387. ConvertProgram;
  3388. CheckSource('TestFunctionResult',
  3389. LinesToStr([ // statements
  3390. 'this.Func1 = function () {',
  3391. ' var Result = 0;',
  3392. ' Result = 3;',
  3393. ' Result = 4;',
  3394. ' return Result;',
  3395. '};'
  3396. ]),
  3397. '');
  3398. end;
  3399. procedure TTestModule.TestNestedProc;
  3400. begin
  3401. StartProgram(false);
  3402. Add([
  3403. 'var vInUnit: longint;',
  3404. 'function DoIt(pA,pD: longint): longint;',
  3405. 'var',
  3406. ' vB: longint;',
  3407. ' vC: longint;',
  3408. ' function Nesty(pA: longint): longint; ',
  3409. ' var vB: longint;',
  3410. ' begin',
  3411. ' Result:=pa+vb+vc+pd+vInUnit;',
  3412. ' nesty:=3;',
  3413. ' doit:=4;',
  3414. ' exit;',
  3415. ' end;',
  3416. 'begin',
  3417. ' Result:=pa+vb+vc;',
  3418. ' doit:=6;',
  3419. ' exit;',
  3420. 'end;',
  3421. 'begin']);
  3422. ConvertProgram;
  3423. CheckSource('TestNestedProc',
  3424. LinesToStr([ // statements
  3425. 'this.vInUnit = 0;',
  3426. 'this.DoIt = function (pA, pD) {',
  3427. ' var Result = 0;',
  3428. ' var vB = 0;',
  3429. ' var vC = 0;',
  3430. ' function Nesty(pA) {',
  3431. ' var Result$1 = 0;',
  3432. ' var vB = 0;',
  3433. ' Result$1 = pA + vB + vC + pD + $mod.vInUnit;',
  3434. ' Result$1 = 3;',
  3435. ' Result = 4;',
  3436. ' return Result$1;',
  3437. ' return Result$1;',
  3438. ' };',
  3439. ' Result = pA + vB + vC;',
  3440. ' Result = 6;',
  3441. ' return Result;',
  3442. ' return Result;',
  3443. '};'
  3444. ]),
  3445. '');
  3446. end;
  3447. procedure TTestModule.TestNestedProc_ResultString;
  3448. begin
  3449. StartProgram(false);
  3450. Add([
  3451. 'function DoIt: string;',
  3452. ' function Nesty: string; ',
  3453. ' begin',
  3454. ' nesty:=#65#66;',
  3455. ' nesty[1]:=#67;',
  3456. ' doit:=#68;',
  3457. ' doit[2]:=#69;',
  3458. ' end;',
  3459. 'begin',
  3460. ' doit:=#70;',
  3461. ' doit[3]:=#71;',
  3462. 'end;',
  3463. 'begin']);
  3464. ConvertProgram;
  3465. CheckSource('TestNestedProc_ResultString',
  3466. LinesToStr([ // statements
  3467. 'this.DoIt = function () {',
  3468. ' var Result = "";',
  3469. ' function Nesty() {',
  3470. ' var Result$1 = "";',
  3471. ' Result$1 = "AB";',
  3472. ' Result$1 = rtl.setCharAt(Result$1, 0, "C");',
  3473. ' Result = "D";',
  3474. ' Result = rtl.setCharAt(Result, 1, "E");',
  3475. ' return Result$1;',
  3476. ' };',
  3477. ' Result = "F";',
  3478. ' Result = rtl.setCharAt(Result, 2, "G");',
  3479. ' return Result;',
  3480. '};'
  3481. ]),
  3482. '');
  3483. end;
  3484. procedure TTestModule.TestForwardProc;
  3485. begin
  3486. StartProgram(false);
  3487. Add('procedure FuncA(Bar: longint); forward;');
  3488. Add('procedure FuncB(Bar: longint);');
  3489. Add('begin');
  3490. Add(' funca(bar);');
  3491. Add('end;');
  3492. Add('procedure funca(bar: longint);');
  3493. Add('begin');
  3494. Add(' if bar=3 then ;');
  3495. Add('end;');
  3496. Add('begin');
  3497. Add(' funca(4);');
  3498. Add(' funcb(5);');
  3499. ConvertProgram;
  3500. CheckSource('TestForwardProc',
  3501. LinesToStr([ // statements'
  3502. 'this.FuncB = function (Bar) {',
  3503. ' $mod.FuncA(Bar);',
  3504. '};',
  3505. 'this.FuncA = function (Bar) {',
  3506. ' if (Bar === 3);',
  3507. '};'
  3508. ]),
  3509. LinesToStr([
  3510. '$mod.FuncA(4);',
  3511. '$mod.FuncB(5);'
  3512. ])
  3513. );
  3514. end;
  3515. procedure TTestModule.TestNestedForwardProc;
  3516. begin
  3517. StartProgram(false);
  3518. Add('procedure FuncA;');
  3519. Add(' procedure FuncB(i: longint); forward;');
  3520. Add(' procedure FuncC(i: longint);');
  3521. Add(' begin');
  3522. Add(' funcb(i);');
  3523. Add(' end;');
  3524. Add(' procedure FuncB(i: longint);');
  3525. Add(' begin');
  3526. Add(' if i=3 then ;');
  3527. Add(' end;');
  3528. Add('begin');
  3529. Add(' funcc(4)');
  3530. Add('end;');
  3531. Add('begin');
  3532. Add(' funca;');
  3533. ConvertProgram;
  3534. CheckSource('TestNestedForwardProc',
  3535. LinesToStr([ // statements'
  3536. 'this.FuncA = function () {',
  3537. ' function FuncC(i) {',
  3538. ' FuncB(i);',
  3539. ' };',
  3540. ' function FuncB(i) {',
  3541. ' if (i === 3);',
  3542. ' };',
  3543. ' FuncC(4);',
  3544. '};'
  3545. ]),
  3546. LinesToStr([
  3547. '$mod.FuncA();'
  3548. ])
  3549. );
  3550. end;
  3551. procedure TTestModule.TestAssignFunctionResult;
  3552. begin
  3553. StartProgram(false);
  3554. Add('function Func1: longint;');
  3555. Add('begin');
  3556. Add('end;');
  3557. Add('var i: longint;');
  3558. Add('begin');
  3559. Add(' i:=func1();');
  3560. Add(' i:=func1()+func1();');
  3561. ConvertProgram;
  3562. CheckSource('TestAssignFunctionResult',
  3563. LinesToStr([ // statements
  3564. 'this.Func1 = function () {',
  3565. ' var Result = 0;',
  3566. ' return Result;',
  3567. '};',
  3568. 'this.i = 0;'
  3569. ]),
  3570. LinesToStr([
  3571. '$mod.i = $mod.Func1();',
  3572. '$mod.i = $mod.Func1() + $mod.Func1();'
  3573. ]));
  3574. end;
  3575. procedure TTestModule.TestFunctionResultInCondition;
  3576. begin
  3577. StartProgram(false);
  3578. Add('function Func1: longint;');
  3579. Add('begin');
  3580. Add('end;');
  3581. Add('function Func2: boolean;');
  3582. Add('begin');
  3583. Add('end;');
  3584. Add('var i: longint;');
  3585. Add('begin');
  3586. Add(' if func2 then ;');
  3587. Add(' if i=func1() then ;');
  3588. Add(' if i=func1 then ;');
  3589. ConvertProgram;
  3590. CheckSource('TestFunctionResultInCondition',
  3591. LinesToStr([ // statements
  3592. 'this.Func1 = function () {',
  3593. ' var Result = 0;',
  3594. ' return Result;',
  3595. '};',
  3596. 'this.Func2 = function () {',
  3597. ' var Result = false;',
  3598. ' return Result;',
  3599. '};',
  3600. 'this.i = 0;'
  3601. ]),
  3602. LinesToStr([
  3603. 'if ($mod.Func2());',
  3604. 'if ($mod.i === $mod.Func1());',
  3605. 'if ($mod.i === $mod.Func1());'
  3606. ]));
  3607. end;
  3608. procedure TTestModule.TestFunctionResultInForLoop;
  3609. begin
  3610. StartProgram(false);
  3611. Add([
  3612. 'function Func1(a: array of longint): longint;',
  3613. 'begin',
  3614. ' for Result:=High(a) downto Low(a) do if a[Result]=0 then exit;',
  3615. ' for Result in a do if a[Result]=0 then exit;',
  3616. 'end;',
  3617. 'begin',
  3618. ' Func1([1,2,3])']);
  3619. ConvertProgram;
  3620. CheckSource('TestFunctionResultInForLoop',
  3621. LinesToStr([ // statements
  3622. 'this.Func1 = function (a) {',
  3623. ' var Result = 0;',
  3624. ' for (var $l = rtl.length(a) - 1; $l >= 0; $l--) {',
  3625. ' Result = $l;',
  3626. ' if (a[Result] === 0) return Result;',
  3627. ' };',
  3628. ' for (var $in = a, $l1 = 0, $end = rtl.length($in) - 1; $l1 <= $end; $l1++) {',
  3629. ' Result = $in[$l1];',
  3630. ' if (a[Result] === 0) return Result;',
  3631. ' };',
  3632. ' return Result;',
  3633. '};',
  3634. '']),
  3635. LinesToStr([
  3636. '$mod.Func1([1, 2, 3]);'
  3637. ]));
  3638. end;
  3639. procedure TTestModule.TestFunctionResultInTypeCast;
  3640. begin
  3641. StartProgram(false);
  3642. Add([
  3643. 'function GetInt: longint;',
  3644. 'begin',
  3645. 'end;',
  3646. 'begin',
  3647. ' if Byte(GetInt)=0 then ;',
  3648. '']);
  3649. ConvertProgram;
  3650. CheckSource('TestFunctionResultInTypeCast',
  3651. LinesToStr([ // statements
  3652. 'this.GetInt = function () {',
  3653. ' var Result = 0;',
  3654. ' return Result;',
  3655. '};',
  3656. '']),
  3657. LinesToStr([
  3658. 'if (($mod.GetInt() & 255) === 0) ;'
  3659. ]));
  3660. end;
  3661. procedure TTestModule.TestExit;
  3662. begin
  3663. StartProgram(false);
  3664. Add('procedure ProcA;');
  3665. Add('begin');
  3666. Add(' exit;');
  3667. Add('end;');
  3668. Add('function FuncB: longint;');
  3669. Add('begin');
  3670. Add(' exit;');
  3671. Add(' exit(3);');
  3672. Add('end;');
  3673. Add('function FuncC: string;');
  3674. Add('begin');
  3675. Add(' exit;');
  3676. Add(' exit(''a'');');
  3677. Add(' exit(''abc'');');
  3678. Add('end;');
  3679. Add('begin');
  3680. Add(' exit;');
  3681. Add(' exit(1);');
  3682. ConvertProgram;
  3683. CheckSource('TestExit',
  3684. LinesToStr([ // statements
  3685. 'this.ProcA = function () {',
  3686. ' return;',
  3687. '};',
  3688. 'this.FuncB = function () {',
  3689. ' var Result = 0;',
  3690. ' return Result;',
  3691. ' return 3;',
  3692. ' return Result;',
  3693. '};',
  3694. 'this.FuncC = function () {',
  3695. ' var Result = "";',
  3696. ' return Result;',
  3697. ' return "a";',
  3698. ' return "abc";',
  3699. ' return Result;',
  3700. '};'
  3701. ]),
  3702. LinesToStr([
  3703. 'return;',
  3704. 'return 1;',
  3705. '']));
  3706. end;
  3707. procedure TTestModule.TestExit_ResultInFinally;
  3708. begin
  3709. StartProgram(false);
  3710. Add([
  3711. 'function Run: word;',
  3712. 'begin',
  3713. ' try',
  3714. ' exit(3);', // no Result in finally -> use return 3
  3715. ' finally',
  3716. ' end;',
  3717. 'end;',
  3718. 'function Fly: word;',
  3719. 'begin',
  3720. ' try',
  3721. ' exit(3);',
  3722. ' finally',
  3723. ' if Result>0 then ;',
  3724. ' end;',
  3725. 'end;',
  3726. 'function Jump: word;',
  3727. 'begin',
  3728. ' try',
  3729. ' try',
  3730. ' exit(4);',
  3731. ' finally',
  3732. ' end;',
  3733. ' finally',
  3734. ' if Result>0 then ;',
  3735. ' end;',
  3736. 'end;',
  3737. 'begin',
  3738. '']);
  3739. ConvertProgram;
  3740. CheckSource('TestExit_ResultInFinally',
  3741. LinesToStr([ // statements
  3742. 'this.Run = function () {',
  3743. ' var Result = 0;',
  3744. ' try {',
  3745. ' return 3;',
  3746. ' } finally {',
  3747. ' };',
  3748. ' return Result;',
  3749. '};',
  3750. 'this.Fly = function () {',
  3751. ' var Result = 0;',
  3752. ' try {',
  3753. ' Result = 3;',
  3754. ' return Result;',
  3755. ' } finally {',
  3756. ' if (Result > 0) ;',
  3757. ' };',
  3758. ' return Result;',
  3759. '};',
  3760. 'this.Jump = function () {',
  3761. ' var Result = 0;',
  3762. ' try {',
  3763. ' try {',
  3764. ' Result = 4;',
  3765. ' return Result;',
  3766. ' } finally {',
  3767. ' };',
  3768. ' } finally {',
  3769. ' if (Result > 0) ;',
  3770. ' };',
  3771. ' return Result;',
  3772. '};',
  3773. '']),
  3774. LinesToStr([
  3775. '']));
  3776. end;
  3777. procedure TTestModule.TestBreak;
  3778. begin
  3779. StartProgram(false);
  3780. Add([
  3781. 'var',
  3782. ' i: longint;',
  3783. 'begin',
  3784. ' repeat',
  3785. ' break;',
  3786. ' until true;',
  3787. ' while true do',
  3788. ' break;',
  3789. ' for i:=1 to 2 do',
  3790. ' break;']);
  3791. ConvertProgram;
  3792. CheckSource('TestBreak',
  3793. LinesToStr([ // statements
  3794. 'this.i = 0;'
  3795. ]),
  3796. LinesToStr([
  3797. 'do {',
  3798. ' break;',
  3799. '} while (!true);',
  3800. 'while (true) break;',
  3801. 'for ($mod.i = 1; $mod.i <= 2; $mod.i++) break;',
  3802. '']));
  3803. end;
  3804. procedure TTestModule.TestBreakAsVar;
  3805. begin
  3806. StartProgram(false);
  3807. Add([
  3808. 'procedure DoIt(break: boolean);',
  3809. 'begin',
  3810. ' if break then ;',
  3811. 'end;',
  3812. 'var',
  3813. ' break: boolean;',
  3814. 'begin',
  3815. ' if break then ;']);
  3816. ConvertProgram;
  3817. CheckSource('TestBreakAsVar',
  3818. LinesToStr([ // statements
  3819. 'this.DoIt = function (Break) {',
  3820. ' if (Break) ;',
  3821. '};',
  3822. 'this.Break = false;',
  3823. '']),
  3824. LinesToStr([
  3825. 'if($mod.Break) ;',
  3826. '']));
  3827. end;
  3828. procedure TTestModule.TestContinue;
  3829. begin
  3830. StartProgram(false);
  3831. Add('var i: longint;');
  3832. Add('begin');
  3833. Add(' repeat');
  3834. Add(' continue;');
  3835. Add(' until true;');
  3836. Add(' while true do');
  3837. Add(' continue;');
  3838. Add(' for i:=1 to 2 do');
  3839. Add(' continue;');
  3840. ConvertProgram;
  3841. CheckSource('TestContinue',
  3842. LinesToStr([ // statements
  3843. 'this.i = 0;'
  3844. ]),
  3845. LinesToStr([
  3846. 'do {',
  3847. ' continue;',
  3848. '} while (!true);',
  3849. 'while (true) continue;',
  3850. 'for ($mod.i = 1; $mod.i <= 2; $mod.i++) continue;',
  3851. '']));
  3852. end;
  3853. procedure TTestModule.TestProc_External;
  3854. begin
  3855. StartProgram(false);
  3856. Add('procedure Foo; external name ''console.log'';');
  3857. Add('function Bar: longint; external name ''get.item'';');
  3858. Add('function Bla(s: string): longint; external name ''apply.something'';');
  3859. Add('var');
  3860. Add(' i: longint;');
  3861. Add('begin');
  3862. Add(' Foo;');
  3863. Add(' i:=Bar;');
  3864. Add(' i:=Bla(''abc'');');
  3865. ConvertProgram;
  3866. CheckSource('TestProc_External',
  3867. LinesToStr([ // statements
  3868. 'this.i = 0;'
  3869. ]),
  3870. LinesToStr([
  3871. 'console.log();',
  3872. '$mod.i = get.item();',
  3873. '$mod.i = apply.something("abc");'
  3874. ]));
  3875. end;
  3876. procedure TTestModule.TestProc_ExternalOtherUnit;
  3877. begin
  3878. AddModuleWithIntfImplSrc('unit2.pas',
  3879. LinesToStr([
  3880. 'procedure Now; external name ''Date.now'';',
  3881. 'procedure DoIt;'
  3882. ]),
  3883. 'procedure doit; begin end;');
  3884. StartUnit(true);
  3885. Add('interface');
  3886. Add('uses unit2;');
  3887. Add('implementation');
  3888. Add('begin');
  3889. Add(' now;');
  3890. Add(' now();');
  3891. Add(' uNit2.now;');
  3892. Add(' uNit2.now();');
  3893. Add(' doit;');
  3894. Add(' uNit2.doit;');
  3895. ConvertUnit;
  3896. CheckSource('TestProc_ExternalOtherUnit',
  3897. LinesToStr([
  3898. '']),
  3899. LinesToStr([
  3900. 'Date.now();',
  3901. 'Date.now();',
  3902. 'Date.now();',
  3903. 'Date.now();',
  3904. 'pas.unit2.DoIt();',
  3905. 'pas.unit2.DoIt();',
  3906. '']));
  3907. end;
  3908. procedure TTestModule.TestProc_Asm;
  3909. begin
  3910. StartProgram(false);
  3911. Add([
  3912. '{$mode delphi}',
  3913. 'function DoIt: longint;',
  3914. 'begin;',
  3915. ' asm',
  3916. ' { a:{ b:{}, c:[]}, d:''1'' };',
  3917. ' end;',
  3918. ' asm console.log(); end;',
  3919. ' asm',
  3920. ' s = "'' ";',
  3921. ' s = ''" '';',
  3922. ' s = s + "world" + "''";',
  3923. ' // end',
  3924. ' s = ''end'';',
  3925. ' s = "end";',
  3926. ' s = "foo\"bar";',
  3927. ' s = ''a\''b'';',
  3928. ' s = `${expr}\`-"-''-`;',
  3929. ' s = `multi',
  3930. 'line`;',
  3931. ' end;',
  3932. 'end;',
  3933. 'procedure Fly;',
  3934. 'asm',
  3935. ' return;',
  3936. 'end;',
  3937. 'begin']);
  3938. ConvertProgram;
  3939. CheckSource('TestProc_Asm',
  3940. LinesToStr([ // statements
  3941. 'this.DoIt = function () {',
  3942. ' var Result = 0;',
  3943. ' { a:{ b:{}, c:[]}, d:''1'' };',
  3944. ' console.log();',
  3945. ' s = "'' ";',
  3946. ' s = ''" '';',
  3947. ' s = s + "world" + "''";',
  3948. ' // end',
  3949. ' s = ''end'';',
  3950. ' s = "end";',
  3951. ' s = "foo\"bar";',
  3952. ' s = ''a\''b'';',
  3953. ' s = `${expr}\`-"-''-`;',
  3954. ' s = `multi',
  3955. 'line`;',
  3956. ' return Result;',
  3957. '};',
  3958. 'this.Fly = function () {',
  3959. ' return;',
  3960. '};',
  3961. '']),
  3962. LinesToStr([
  3963. ''
  3964. ]));
  3965. end;
  3966. procedure TTestModule.TestProc_AsmSubBlock;
  3967. begin
  3968. StartProgram(true,[supTObject]);
  3969. Add([
  3970. '{$mode delphi}',
  3971. 'type',
  3972. ' TBird = class end;',
  3973. 'procedure Run(w: word);',
  3974. 'begin;',
  3975. ' if true then asm console.log(); end;',
  3976. ' if w>3 then asm',
  3977. ' var a = w+1;',
  3978. ' w = a+3;',
  3979. ' end;',
  3980. ' while (w>7) do asm',
  3981. ' w+=3; w*=2;',
  3982. ' end;',
  3983. ' try',
  3984. ' except',
  3985. ' on E: TBird do',
  3986. ' asm console.log(E); end;',
  3987. ' on E: TObject do',
  3988. ' asm var i=3; i--; end;',
  3989. ' else asm Fly; High; end;',
  3990. ' end;',
  3991. 'end;',
  3992. 'begin']);
  3993. ConvertProgram;
  3994. CheckSource('TestProc_AsmSubBlock',
  3995. LinesToStr([ // statements
  3996. 'rtl.createClass(this, "TBird", pas.system.TObject, function () {',
  3997. '});',
  3998. 'this.Run = function (w) {',
  3999. ' if (true) console.log();',
  4000. ' if (w > 3) {',
  4001. ' var a = w+1;',
  4002. ' w = a+3;',
  4003. ' };',
  4004. ' while (w > 7) {',
  4005. ' w+=3; w*=2;',
  4006. ' };',
  4007. ' try {} catch ($e) {',
  4008. ' if ($mod.TBird.isPrototypeOf($e)) {',
  4009. ' var E = $e;',
  4010. ' console.log(E);',
  4011. ' } else if (pas.system.TObject.isPrototypeOf($e)) {',
  4012. ' var E = $e;',
  4013. ' var i=3; i--;',
  4014. ' } else {',
  4015. ' Fly; High;',
  4016. ' }',
  4017. ' };',
  4018. '};',
  4019. '']),
  4020. LinesToStr([
  4021. ''
  4022. ]));
  4023. end;
  4024. procedure TTestModule.TestProc_Assembler;
  4025. begin
  4026. StartProgram(false);
  4027. Add('function DoIt: longint; assembler;');
  4028. Add('asm');
  4029. Add('{ a:{ b:{}, c:[]}, d:''1'' };');
  4030. Add('end;');
  4031. Add('begin');
  4032. ConvertProgram;
  4033. CheckSource('TestProc_Assembler',
  4034. LinesToStr([ // statements
  4035. 'this.DoIt = function () {',
  4036. ' { a:{ b:{}, c:[]}, d:''1'' };',
  4037. '};'
  4038. ]),
  4039. LinesToStr([
  4040. ''
  4041. ]));
  4042. end;
  4043. procedure TTestModule.TestProc_VarParam;
  4044. begin
  4045. StartProgram(false);
  4046. Add('type integer = longint;');
  4047. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  4048. Add('var vJ: integer;');
  4049. Add('begin');
  4050. Add(' vg:=vg+1;');
  4051. Add(' vj:=vh+2;');
  4052. Add(' vi:=vi+3;');
  4053. Add(' doit(vg,vg,vg);');
  4054. Add(' doit(vh,vh,vj);');
  4055. Add(' doit(vi,vi,vi);');
  4056. Add(' doit(vj,vj,vj);');
  4057. Add('end;');
  4058. Add('var i: integer;');
  4059. Add('begin');
  4060. Add(' doit(i,i,i);');
  4061. ConvertProgram;
  4062. CheckSource('TestProc_VarParam',
  4063. LinesToStr([ // statements
  4064. 'this.DoIt = function (vG,vH,vI) {',
  4065. ' var vJ = 0;',
  4066. ' vG = vG + 1;',
  4067. ' vJ = vH + 2;',
  4068. ' vI.set(vI.get()+3);',
  4069. ' $mod.DoIt(vG, vG, {',
  4070. ' get: function () {',
  4071. ' return vG;',
  4072. ' },',
  4073. ' set: function (v) {',
  4074. ' vG = v;',
  4075. ' }',
  4076. ' });',
  4077. ' $mod.DoIt(vH, vH, {',
  4078. ' get: function () {',
  4079. ' return vJ;',
  4080. ' },',
  4081. ' set: function (v) {',
  4082. ' vJ = v;',
  4083. ' }',
  4084. ' });',
  4085. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  4086. ' $mod.DoIt(vJ, vJ, {',
  4087. ' get: function () {',
  4088. ' return vJ;',
  4089. ' },',
  4090. ' set: function (v) {',
  4091. ' vJ = v;',
  4092. ' }',
  4093. ' });',
  4094. '};',
  4095. 'this.i = 0;'
  4096. ]),
  4097. LinesToStr([
  4098. '$mod.DoIt($mod.i,$mod.i,{',
  4099. ' p: $mod,',
  4100. ' get: function () {',
  4101. ' return this.p.i;',
  4102. ' },',
  4103. ' set: function (v) {',
  4104. ' this.p.i = v;',
  4105. ' }',
  4106. '});'
  4107. ]));
  4108. end;
  4109. procedure TTestModule.TestProc_VarParamString;
  4110. begin
  4111. StartProgram(false);
  4112. Add(['type TCaption = string;',
  4113. 'procedure DoIt(vA: TCaption; var vB: TCaption; out vC: TCaption);',
  4114. 'var c: char;',
  4115. 'begin',
  4116. ' va[1]:=c;',
  4117. ' vb[2]:=c;',
  4118. ' vc[3]:=c;',
  4119. 'end;',
  4120. 'begin']);
  4121. ConvertProgram;
  4122. CheckSource('TestProc_VarParamString',
  4123. LinesToStr([ // statements
  4124. 'this.DoIt = function (vA,vB,vC) {',
  4125. ' var c = "";',
  4126. ' vA = rtl.setCharAt(vA, 0, c);',
  4127. ' vB.set(rtl.setCharAt(vB.get(), 1, c));',
  4128. ' vC.set(rtl.setCharAt(vC.get(), 2, c));',
  4129. '};',
  4130. '']),
  4131. LinesToStr([
  4132. ]));
  4133. end;
  4134. procedure TTestModule.TestProc_VarParamV;
  4135. begin
  4136. StartProgram(false);
  4137. Add([
  4138. 'procedure Inc2(var i: longint);',
  4139. 'begin',
  4140. ' i:=i+2;',
  4141. 'end;',
  4142. 'procedure DoIt(v: longint);',
  4143. 'var p: array of longint;',
  4144. 'begin',
  4145. ' Inc2(v);',
  4146. ' Inc2(p[v]);',
  4147. 'end;',
  4148. 'begin']);
  4149. ConvertProgram;
  4150. CheckSource('TestProc_VarParamV',
  4151. LinesToStr([ // statements
  4152. 'this.Inc2 = function (i) {',
  4153. ' i.set(i.get()+2);',
  4154. '};',
  4155. 'this.DoIt = function (v) {',
  4156. ' var p = [];',
  4157. ' $mod.Inc2({get: function () {',
  4158. ' return v;',
  4159. ' }, set: function (w) {',
  4160. ' v = w;',
  4161. ' }});',
  4162. ' $mod.Inc2({',
  4163. ' a: v,',
  4164. ' p: p,',
  4165. ' get: function () {',
  4166. ' return this.p[this.a];',
  4167. ' },',
  4168. ' set: function (v) {',
  4169. ' this.p[this.a] = v;',
  4170. ' }',
  4171. ' });',
  4172. '};',
  4173. '']),
  4174. LinesToStr([
  4175. '']));
  4176. end;
  4177. procedure TTestModule.TestProc_Overload;
  4178. begin
  4179. StartProgram(false);
  4180. Add('procedure DoIt(vI: longint); begin end;');
  4181. Add('procedure DoIt(vI, vJ: longint); begin end;');
  4182. Add('procedure DoIt(vD: double); begin end;');
  4183. Add('begin');
  4184. Add(' DoIt(1);');
  4185. Add(' DoIt(2,3);');
  4186. Add(' DoIt(4.5);');
  4187. ConvertProgram;
  4188. CheckSource('TestProcedureOverload',
  4189. LinesToStr([ // statements
  4190. 'this.DoIt = function (vI) {',
  4191. '};',
  4192. 'this.DoIt$1 = function (vI, vJ) {',
  4193. '};',
  4194. 'this.DoIt$2 = function (vD) {',
  4195. '};',
  4196. '']),
  4197. LinesToStr([
  4198. '$mod.DoIt(1);',
  4199. '$mod.DoIt$1(2, 3);',
  4200. '$mod.DoIt$2(4.5);',
  4201. '']));
  4202. end;
  4203. procedure TTestModule.TestProc_OverloadForward;
  4204. begin
  4205. StartProgram(false);
  4206. Add('procedure DoIt(vI: longint); forward;');
  4207. Add('procedure DoIt(vI, vJ: longint); begin end;');
  4208. Add('procedure doit(vi: longint); begin end;');
  4209. Add('begin');
  4210. Add(' doit(1);');
  4211. Add(' doit(2,3);');
  4212. ConvertProgram;
  4213. CheckSource('TestProcedureOverloadForward',
  4214. LinesToStr([ // statements
  4215. 'this.DoIt$1 = function (vI, vJ) {',
  4216. '};',
  4217. 'this.DoIt = function (vI) {',
  4218. '};',
  4219. '']),
  4220. LinesToStr([
  4221. '$mod.DoIt(1);',
  4222. '$mod.DoIt$1(2, 3);',
  4223. '']));
  4224. end;
  4225. procedure TTestModule.TestProc_OverloadIntfImpl;
  4226. begin
  4227. StartUnit(false);
  4228. Add('interface');
  4229. Add('procedure DoIt(vI: longint);');
  4230. Add('procedure DoIt(vI, vJ: longint);');
  4231. Add('implementation');
  4232. Add('procedure DoIt(vI, vJ, vK, vL, vM: longint); forward;');
  4233. Add('procedure DoIt(vI, vJ, vK: longint); begin end;');
  4234. Add('procedure DoIt(vi: longint); begin end;');
  4235. Add('procedure DoIt(vI, vJ, vK, vL: longint); begin end;');
  4236. Add('procedure DoIt(vi, vj: longint); begin end;');
  4237. Add('procedure DoIt(vi, vj, vk, vl, vm: longint); begin end;');
  4238. Add('begin');
  4239. Add(' doit(1);');
  4240. Add(' doit(2,3);');
  4241. Add(' doit(4,5,6);');
  4242. Add(' doit(7,8,9,10);');
  4243. Add(' doit(11,12,13,14,15);');
  4244. ConvertUnit;
  4245. CheckSource('TestProcedureOverloadUnit',
  4246. LinesToStr([ // statements
  4247. 'var $impl = $mod.$impl;',
  4248. 'this.DoIt = function (vI) {',
  4249. '};',
  4250. 'this.DoIt$1 = function (vI, vJ) {',
  4251. '};',
  4252. '']),
  4253. LinesToStr([ // this.$init
  4254. '$mod.DoIt(1);',
  4255. '$mod.DoIt$1(2, 3);',
  4256. '$impl.DoIt$3(4,5,6);',
  4257. '$impl.DoIt$4(7,8,9,10);',
  4258. '$impl.DoIt$2(11,12,13,14,15);',
  4259. '']),
  4260. LinesToStr([ // implementation
  4261. '$impl.DoIt$3 = function (vI, vJ, vK) {',
  4262. '};',
  4263. '$impl.DoIt$4 = function (vI, vJ, vK, vL) {',
  4264. '};',
  4265. '$impl.DoIt$2 = function (vI, vJ, vK, vL, vM) {',
  4266. '};',
  4267. '']));
  4268. end;
  4269. procedure TTestModule.TestProc_OverloadNested;
  4270. begin
  4271. StartProgram(false);
  4272. Add([
  4273. 'procedure doit(vA: longint);',
  4274. ' procedure DoIt(vA, vB: longint); overload;',
  4275. ' begin',
  4276. ' doit(1);',
  4277. ' doit(1,2);',
  4278. ' end;',
  4279. ' procedure doit(vA, vB, vC: longint);',
  4280. ' begin',
  4281. ' doit(1);',
  4282. ' doit(1,2);',
  4283. ' doit(1,2,3);',
  4284. ' end;',
  4285. 'begin',
  4286. ' doit(1);',
  4287. ' doit(1,2);',
  4288. ' doit(1,2,3);',
  4289. 'end;',
  4290. 'begin // main',
  4291. ' doit(1);']);
  4292. ConvertProgram;
  4293. CheckSource('TestProcedureOverloadNested',
  4294. LinesToStr([ // statements
  4295. 'this.doit = function (vA) {',
  4296. ' function DoIt$1(vA, vB) {',
  4297. ' $mod.doit(1);',
  4298. ' DoIt$1(1, 2);',
  4299. ' };',
  4300. ' function doit$2(vA, vB, vC) {',
  4301. ' $mod.doit(1);',
  4302. ' DoIt$1(1, 2);',
  4303. ' doit$2(1, 2, 3);',
  4304. ' };',
  4305. ' $mod.doit(1);',
  4306. ' DoIt$1(1, 2);',
  4307. ' doit$2(1, 2, 3);',
  4308. '};',
  4309. '']),
  4310. LinesToStr([
  4311. '$mod.doit(1);',
  4312. '']));
  4313. end;
  4314. procedure TTestModule.TestProc_OverloadNestedForward;
  4315. begin
  4316. StartProgram(false);
  4317. Add([
  4318. 'procedure DoIt(vA: longint); overload; forward;',
  4319. 'procedure DoIt(vB, vC: longint); overload;',
  4320. 'begin // 2 param overload',
  4321. ' doit(1);',
  4322. ' doit(1,2);',
  4323. 'end;',
  4324. 'procedure doit(vA: longint);',
  4325. ' procedure DoIt(vA, vB, vC: longint); overload; forward;',
  4326. ' procedure DoIt(vA, vB, vC, vD: longint); overload;',
  4327. ' begin // 4 param overload',
  4328. ' doit(1);',
  4329. ' doit(1,2);',
  4330. ' doit(1,2,3);',
  4331. ' doit(1,2,3,4);',
  4332. ' end;',
  4333. ' procedure doit(vA, vB, vC: longint);',
  4334. ' procedure DoIt(vA, vB, vC, vD, vE: longint); overload; forward;',
  4335. ' procedure DoIt(vA, vB, vC, vD, vE, vF: longint); overload;',
  4336. ' begin // 6 param overload',
  4337. ' doit(1);',
  4338. ' doit(1,2);',
  4339. ' doit(1,2,3);',
  4340. ' doit(1,2,3,4);',
  4341. ' doit(1,2,3,4,5);',
  4342. ' doit(1,2,3,4,5,6);',
  4343. ' end;',
  4344. ' procedure doit(vA, vB, vC, vD, vE: longint);',
  4345. ' begin // 5 param overload',
  4346. ' doit(1);',
  4347. ' doit(1,2);',
  4348. ' doit(1,2,3);',
  4349. ' doit(1,2,3,4);',
  4350. ' doit(1,2,3,4,5);',
  4351. ' doit(1,2,3,4,5,6);',
  4352. ' end;',
  4353. ' begin // 3 param overload',
  4354. ' doit(1);',
  4355. ' doit(1,2);',
  4356. ' doit(1,2,3);',
  4357. ' doit(1,2,3,4);',
  4358. ' doit(1,2,3,4,5);',
  4359. ' doit(1,2,3,4,5,6);',
  4360. ' end;',
  4361. 'begin // 1 param overload',
  4362. ' doit(1);',
  4363. ' doit(1,2);',
  4364. ' doit(1,2,3);',
  4365. ' doit(1,2,3,4);',
  4366. 'end;',
  4367. 'begin // main',
  4368. ' doit(1);',
  4369. ' doit(1,2);']);
  4370. ConvertProgram;
  4371. CheckSource('TestProc_OverloadNestedForward',
  4372. LinesToStr([ // statements
  4373. 'this.DoIt$1 = function (vB, vC) {',
  4374. ' $mod.DoIt(1);',
  4375. ' $mod.DoIt$1(1, 2);',
  4376. '};',
  4377. 'this.DoIt = function (vA) {',
  4378. ' function DoIt$3(vA, vB, vC, vD) {',
  4379. ' $mod.DoIt(1);',
  4380. ' $mod.DoIt$1(1, 2);',
  4381. ' DoIt$2(1, 2, 3);',
  4382. ' DoIt$3(1, 2, 3, 4);',
  4383. ' };',
  4384. ' function DoIt$2(vA, vB, vC) {',
  4385. ' function DoIt$5(vA, vB, vC, vD, vE, vF) {',
  4386. ' $mod.DoIt(1);',
  4387. ' $mod.DoIt$1(1, 2);',
  4388. ' DoIt$2(1, 2, 3);',
  4389. ' DoIt$3(1, 2, 3, 4);',
  4390. ' DoIt$4(1, 2, 3, 4, 5);',
  4391. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  4392. ' };',
  4393. ' function DoIt$4(vA, vB, vC, vD, vE) {',
  4394. ' $mod.DoIt(1);',
  4395. ' $mod.DoIt$1(1, 2);',
  4396. ' DoIt$2(1, 2, 3);',
  4397. ' DoIt$3(1, 2, 3, 4);',
  4398. ' DoIt$4(1, 2, 3, 4, 5);',
  4399. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  4400. ' };',
  4401. ' $mod.DoIt(1);',
  4402. ' $mod.DoIt$1(1, 2);',
  4403. ' DoIt$2(1, 2, 3);',
  4404. ' DoIt$3(1, 2, 3, 4);',
  4405. ' DoIt$4(1, 2, 3, 4, 5);',
  4406. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  4407. ' };',
  4408. ' $mod.DoIt(1);',
  4409. ' $mod.DoIt$1(1, 2);',
  4410. ' DoIt$2(1, 2, 3);',
  4411. ' DoIt$3(1, 2, 3, 4);',
  4412. '};',
  4413. '']),
  4414. LinesToStr([
  4415. '$mod.DoIt(1);',
  4416. '$mod.DoIt$1(1, 2);',
  4417. '']));
  4418. end;
  4419. procedure TTestModule.TestProc_OverloadUnitCycle;
  4420. begin
  4421. AddModuleWithIntfImplSrc('Unit2.pas',
  4422. LinesToStr([
  4423. 'type',
  4424. ' TObject = class',
  4425. ' procedure DoIt(b: boolean); virtual; abstract;',
  4426. ' procedure DoIt(i: longint); virtual; abstract;',
  4427. ' end;',
  4428. '']),
  4429. 'uses test1;');
  4430. StartUnit(true);
  4431. Add([
  4432. 'interface',
  4433. 'uses unit2;',
  4434. 'type',
  4435. ' TEagle = class(TObject)',
  4436. ' procedure DoIt(b: boolean); override;',
  4437. ' procedure DoIt(i: longint); override;',
  4438. ' end;',
  4439. 'implementation',
  4440. 'procedure TEagle.DoIt(b: boolean); begin end;',
  4441. 'procedure TEagle.DoIt(i: longint); begin end;',
  4442. '']);
  4443. ConvertUnit;
  4444. CheckSource('TestProc_OverloadUnitCycle',
  4445. LinesToStr([ // statements
  4446. 'rtl.createClass(this, "TEagle", pas.Unit2.TObject, function () {',
  4447. ' this.DoIt = function (b) {',
  4448. ' };',
  4449. ' this.DoIt$1 = function (i) {',
  4450. ' };',
  4451. '});',
  4452. '']),
  4453. '',
  4454. LinesToStr([
  4455. '']));
  4456. end;
  4457. procedure TTestModule.TestProc_Varargs;
  4458. begin
  4459. StartProgram(false);
  4460. Add([
  4461. 'procedure ProcA(i:longint); varargs; external name ''ProcA'';',
  4462. 'procedure ProcB; varargs; external name ''ProcB'';',
  4463. 'procedure ProcC(i: longint = 17); varargs; external name ''ProcC'';',
  4464. 'function GetIt: longint; begin end;',
  4465. 'begin',
  4466. ' ProcA(1);',
  4467. ' ProcA(1,2);',
  4468. ' ProcA(1,2.0);',
  4469. ' ProcA(1,2,3);',
  4470. ' ProcA(1,''2'');',
  4471. ' ProcA(2,'''');',
  4472. ' ProcA(3,false);',
  4473. ' ProcB;',
  4474. ' ProcB();',
  4475. ' ProcB(4);',
  4476. ' ProcB(''foo'');',
  4477. ' ProcC;',
  4478. ' ProcC();',
  4479. ' ProcC(4);',
  4480. ' ProcC(5,''foo'');',
  4481. ' ProcB(GetIt);',
  4482. ' ProcB(GetIt());',
  4483. ' ProcB(GetIt,GetIt());']);
  4484. ConvertProgram;
  4485. CheckSource('TestProc_Varargs',
  4486. LinesToStr([ // statements
  4487. 'this.GetIt = function () {',
  4488. ' var Result = 0;',
  4489. ' return Result;',
  4490. '};',
  4491. '']),
  4492. LinesToStr([
  4493. 'ProcA(1);',
  4494. 'ProcA(1, 2);',
  4495. 'ProcA(1, 2.0);',
  4496. 'ProcA(1, 2, 3);',
  4497. 'ProcA(1, "2");',
  4498. 'ProcA(2, "");',
  4499. 'ProcA(3, false);',
  4500. 'ProcB();',
  4501. 'ProcB();',
  4502. 'ProcB(4);',
  4503. 'ProcB("foo");',
  4504. 'ProcC(17);',
  4505. 'ProcC(17);',
  4506. 'ProcC(4);',
  4507. 'ProcC(5, "foo");',
  4508. 'ProcB($mod.GetIt());',
  4509. 'ProcB($mod.GetIt());',
  4510. 'ProcB($mod.GetIt(), $mod.GetIt());',
  4511. '']));
  4512. end;
  4513. procedure TTestModule.TestProc_ConstOrder;
  4514. begin
  4515. StartProgram(false);
  4516. Add([
  4517. 'const A = 3;',
  4518. 'const B = A+1;',
  4519. 'procedure DoIt;',
  4520. 'const C = A+1;',
  4521. 'const D = B+1;',
  4522. 'const E = D+C+B+A;',
  4523. 'begin',
  4524. 'end;',
  4525. 'begin'
  4526. ]);
  4527. ConvertProgram;
  4528. CheckSource('TestProc_ConstOrder',
  4529. LinesToStr([ // statements
  4530. 'this.A = 3;',
  4531. 'this.B = 3 + 1;',
  4532. 'var C = 3 + 1;',
  4533. 'var D = 4 + 1;',
  4534. 'var E = 5 + 4 + 4 + 3;',
  4535. 'this.DoIt = function () {',
  4536. '};',
  4537. '']),
  4538. LinesToStr([
  4539. ''
  4540. ]));
  4541. end;
  4542. procedure TTestModule.TestProc_DuplicateConst;
  4543. begin
  4544. StartProgram(false);
  4545. Add([
  4546. 'const A = 1;',
  4547. 'procedure DoIt;',
  4548. 'const A = 2;',
  4549. ' procedure SubIt;',
  4550. ' const A = 21;',
  4551. ' begin',
  4552. ' end;',
  4553. 'begin',
  4554. 'end;',
  4555. 'procedure DoSome;',
  4556. 'const A = 3;',
  4557. 'begin',
  4558. 'end;',
  4559. 'begin'
  4560. ]);
  4561. ConvertProgram;
  4562. CheckSource('TestProc_DuplicateConst',
  4563. LinesToStr([ // statements
  4564. 'this.A = 1;',
  4565. 'var A$1 = 2;',
  4566. 'var A$2 = 21;',
  4567. 'this.DoIt = function () {',
  4568. ' function SubIt() {',
  4569. ' };',
  4570. '};',
  4571. 'var A$3 = 3;',
  4572. 'this.DoSome = function () {',
  4573. '};',
  4574. '']),
  4575. LinesToStr([
  4576. ''
  4577. ]));
  4578. end;
  4579. procedure TTestModule.TestProc_LocalVarAbsolute;
  4580. begin
  4581. StartProgram(false);
  4582. Add([
  4583. 'type',
  4584. ' TObject = class',
  4585. ' Index: longint;',
  4586. ' procedure DoAbs(Item: pointer);',
  4587. ' end;',
  4588. 'procedure TObject.DoAbs(Item: pointer);',
  4589. 'var',
  4590. ' o: TObject absolute Item;',
  4591. 'begin',
  4592. ' if o.Index<o.Index then o.Index:=o.Index;',
  4593. 'end;',
  4594. 'procedure DoIt(i: longint; p: pointer);',
  4595. 'var',
  4596. ' d: double absolute i;',
  4597. ' s: string absolute d;',
  4598. ' oi: TObject absolute i;',
  4599. ' op: TObject absolute p;',
  4600. 'begin',
  4601. ' if d=d then d:=d;',
  4602. ' if s=s then s:=s;',
  4603. ' if oi.Index<oi.Index then oi.Index:=oi.Index;',
  4604. ' if op.Index=op.Index then op.Index:=op.Index;',
  4605. 'end;',
  4606. 'begin']);
  4607. ConvertProgram;
  4608. CheckSource('TestProc_LocalVarAbsolute',
  4609. LinesToStr([ // statements
  4610. 'rtl.createClass(this, "TObject", null, function () {',
  4611. ' this.$init = function () {',
  4612. ' this.Index = 0;',
  4613. ' };',
  4614. ' this.$final = function () {',
  4615. ' };',
  4616. ' this.DoAbs = function (Item) {',
  4617. ' if (Item.Index < Item.Index) Item.Index = Item.Index;',
  4618. ' };',
  4619. '});',
  4620. 'this.DoIt = function (i, p) {',
  4621. ' if (i === i) i = i;',
  4622. ' if (i === i) i = i;',
  4623. ' if (i.Index < i.Index) i.Index = i.Index;',
  4624. ' if (p.Index === p.Index) p.Index = p.Index;',
  4625. '};'
  4626. ]),
  4627. LinesToStr([
  4628. ]));
  4629. end;
  4630. procedure TTestModule.TestProc_LocalVarInit;
  4631. begin
  4632. StartProgram(false);
  4633. Add([
  4634. 'type TBytes = array of byte;',
  4635. 'procedure DoIt;',
  4636. 'const c = 4;',
  4637. 'var',
  4638. ' b: byte = 1;',
  4639. ' w: word = 2+c;',
  4640. ' p: pointer = nil;',
  4641. ' Buffer: TBytes = nil;',
  4642. 'begin',
  4643. 'end;',
  4644. 'begin']);
  4645. ConvertProgram;
  4646. CheckSource('TestProc_LocalVarInit',
  4647. LinesToStr([ // statements
  4648. 'var c = 4;',
  4649. 'this.DoIt = function () {',
  4650. ' var b = 1;',
  4651. ' var w = 2 + 4;',
  4652. ' var p = null;',
  4653. ' var Buffer = [];',
  4654. '};',
  4655. '']),
  4656. LinesToStr([
  4657. ]));
  4658. end;
  4659. procedure TTestModule.TestProc_ReservedWords;
  4660. begin
  4661. StartProgram(false);
  4662. Add([
  4663. 'procedure Date(ArrayBuffer: longint);',
  4664. 'const',
  4665. ' NaN: longint = 3;',
  4666. 'var',
  4667. ' &Boolean: longint;',
  4668. ' procedure Error(ArrayBuffer: longint);',
  4669. ' begin',
  4670. ' end;',
  4671. 'begin',
  4672. ' Nan:=&bOolean;',
  4673. 'end;',
  4674. 'begin',
  4675. ' Date(1);']);
  4676. ConvertProgram;
  4677. CheckSource('TestProc_ReservedWords',
  4678. LinesToStr([ // statements
  4679. 'var naN = 3;',
  4680. 'this.Date = function (arrayBuffer) {',
  4681. ' var boolean = 0;',
  4682. ' function error(arrayBuffer) {',
  4683. ' };',
  4684. ' naN = boolean;',
  4685. '};',
  4686. '']),
  4687. LinesToStr([
  4688. ' $mod.Date(1);'
  4689. ]));
  4690. end;
  4691. procedure TTestModule.TestProc_ConstRefWord;
  4692. begin
  4693. StartProgram(false);
  4694. Add([
  4695. 'procedure Run(constref w: word);',
  4696. 'var l: word;',
  4697. 'begin',
  4698. ' l:=w;',
  4699. ' Run(w);',
  4700. ' Run(l);',
  4701. 'end;',
  4702. 'procedure Fly(a: word; var b: word; out c: word; const d: word; constref e: word);',
  4703. 'begin',
  4704. ' Run(a);',
  4705. ' Run(b);',
  4706. ' Run(c);',
  4707. ' Run(d);',
  4708. ' Run(e);',
  4709. 'end;',
  4710. 'begin',
  4711. ' Run(1);']);
  4712. ConvertProgram;
  4713. CheckHint(mtWarning,nConstRefNotForXAsConst,'ConstRef not yet implemented for Word. Treating as Const');
  4714. CheckSource('TestProc_ConstRefWord',
  4715. LinesToStr([ // statements
  4716. 'this.Run = function (w) {',
  4717. ' var l = 0;',
  4718. ' l = w;',
  4719. ' $mod.Run(w);',
  4720. ' $mod.Run(l);',
  4721. '};',
  4722. 'this.Fly = function (a, b, c, d, e) {',
  4723. ' $mod.Run(a);',
  4724. ' $mod.Run(b.get());',
  4725. ' $mod.Run(c.get());',
  4726. ' $mod.Run(d);',
  4727. ' $mod.Run(e);',
  4728. '};',
  4729. '']),
  4730. LinesToStr([
  4731. '$mod.Run(1);'
  4732. ]));
  4733. end;
  4734. procedure TTestModule.TestAnonymousProc_Assign_ObjFPC;
  4735. begin
  4736. StartProgram(false);
  4737. Add([
  4738. '{$mode objfpc}',
  4739. 'type',
  4740. ' TFunc = reference to function(x: word): word;',
  4741. 'var Func: TFunc;',
  4742. 'procedure DoIt(a: word);',
  4743. 'begin',
  4744. ' Func:=function(b:word): word',
  4745. ' begin',
  4746. ' Result:=a+b;',
  4747. ' exit(b);',
  4748. ' exit(Result);',
  4749. ' end;',// test semicolon
  4750. ' a:=3;',
  4751. 'end;',
  4752. 'begin',
  4753. ' Func:=function(c:word):word begin',
  4754. ' Result:=3+c;',
  4755. ' exit(c);',
  4756. ' exit(Result);',
  4757. ' end;']);
  4758. ConvertProgram;
  4759. CheckSource('TestAnonymousProc_Assign_ObjFPC',
  4760. LinesToStr([ // statements
  4761. 'this.Func = null;',
  4762. 'this.DoIt = function (a) {',
  4763. ' $mod.Func = function (b) {',
  4764. ' var Result = 0;',
  4765. ' Result = a + b;',
  4766. ' return b;',
  4767. ' return Result;',
  4768. ' return Result;',
  4769. ' };',
  4770. ' a = 3;',
  4771. '};',
  4772. '']),
  4773. LinesToStr([
  4774. '$mod.Func = function (c) {',
  4775. ' var Result = 0;',
  4776. ' Result = 3 + c;',
  4777. ' return c;',
  4778. ' return Result;',
  4779. ' return Result;',
  4780. '};',
  4781. '']));
  4782. end;
  4783. procedure TTestModule.TestAnonymousProc_Assign_Delphi;
  4784. begin
  4785. StartProgram(false);
  4786. Add([
  4787. '{$mode delphi}',
  4788. 'type',
  4789. ' TProc = reference to procedure(x: word);',
  4790. 'procedure DoIt(a: word);',
  4791. 'var Proc: TProc;',
  4792. 'begin',
  4793. ' Proc:=procedure(b:word) begin end;',
  4794. 'end;',
  4795. 'var Proc: TProc;',
  4796. 'begin',
  4797. ' Proc:=procedure(c:word) begin end;',
  4798. '']);
  4799. ConvertProgram;
  4800. CheckSource('TestAnonymousProc_Assign_Delphi',
  4801. LinesToStr([ // statements
  4802. 'this.DoIt = function (a) {',
  4803. ' var Proc = null;',
  4804. ' Proc = function (b) {',
  4805. ' };',
  4806. '};',
  4807. 'this.Proc = null;',
  4808. '']),
  4809. LinesToStr([
  4810. '$mod.Proc = function (c) {',
  4811. '};',
  4812. '']));
  4813. end;
  4814. procedure TTestModule.TestAnonymousProc_Arg;
  4815. begin
  4816. StartProgram(false);
  4817. Add([
  4818. 'type',
  4819. ' TProc = reference to procedure;',
  4820. ' TFunc = reference to function(x: word): word;',
  4821. 'procedure DoMore(f,g: TProc);',
  4822. 'begin',
  4823. 'end;',
  4824. 'procedure DoOdd(v: jsvalue);',
  4825. 'begin',
  4826. 'end;',
  4827. 'procedure DoIt(f: TFunc);',
  4828. 'begin',
  4829. ' DoIt(function(b:word): word',
  4830. ' begin',
  4831. ' Result:=1+b;',
  4832. ' end);',
  4833. ' DoMore(procedure begin end, procedure begin end);',
  4834. ' DoOdd(procedure begin end);',
  4835. 'end;',
  4836. 'begin',
  4837. ' DoMore(procedure begin end,',
  4838. ' procedure assembler asm',
  4839. ' console.log("c");',
  4840. ' end);',
  4841. '']);
  4842. ConvertProgram;
  4843. CheckSource('TestAnonymousProc_Arg',
  4844. LinesToStr([ // statements
  4845. 'this.DoMore = function (f, g) {',
  4846. '};',
  4847. 'this.DoOdd = function (v) {',
  4848. '};',
  4849. 'this.DoIt = function (f) {',
  4850. ' $mod.DoIt(function (b) {',
  4851. ' var Result = 0;',
  4852. ' Result = 1 + b;',
  4853. ' return Result;',
  4854. ' });',
  4855. ' $mod.DoMore(function () {',
  4856. ' }, function () {',
  4857. ' });',
  4858. ' $mod.DoOdd(function () {',
  4859. ' });',
  4860. '};',
  4861. '']),
  4862. LinesToStr([
  4863. '$mod.DoMore(function () {',
  4864. '}, function () {',
  4865. ' console.log("c");',
  4866. '});',
  4867. '']));
  4868. end;
  4869. procedure TTestModule.TestAnonymousProc_Typecast;
  4870. begin
  4871. StartProgram(false);
  4872. Add([
  4873. 'type',
  4874. ' TProc = reference to procedure(w: word);',
  4875. ' TArr = array of word;',
  4876. ' TFuncArr = reference to function: TArr;',
  4877. 'procedure DoIt(p: TProc);',
  4878. 'var',
  4879. ' w: word;',
  4880. ' a: TArr;',
  4881. 'begin',
  4882. ' p:=TProc(procedure(b: smallint) begin end);',
  4883. ' a:=TFuncArr(function: TArr begin end)();',
  4884. ' w:=TFuncArr(function: TArr begin end)()[3];',
  4885. 'end;',
  4886. 'begin']);
  4887. ConvertProgram;
  4888. CheckSource('TestAnonymousProc_Typecast',
  4889. LinesToStr([ // statements
  4890. 'this.DoIt = function (p) {',
  4891. ' var w = 0;',
  4892. ' var a = [];',
  4893. ' p = function (b) {',
  4894. ' };',
  4895. ' a = function () {',
  4896. ' var Result = [];',
  4897. ' return Result;',
  4898. ' }();',
  4899. ' w = function () {',
  4900. ' var Result = [];',
  4901. ' return Result;',
  4902. ' }()[3];',
  4903. '};',
  4904. '']),
  4905. LinesToStr([
  4906. '']));
  4907. end;
  4908. procedure TTestModule.TestAnonymousProc_With;
  4909. begin
  4910. StartProgram(false);
  4911. Add([
  4912. 'type',
  4913. ' TProc = reference to procedure(w: word);',
  4914. ' TObject = class',
  4915. ' b: boolean;',
  4916. ' end;',
  4917. 'var',
  4918. ' p: TProc;',
  4919. ' bird: TObject;',
  4920. 'begin',
  4921. ' with bird do',
  4922. ' p:=procedure(w: word)',
  4923. ' begin',
  4924. ' b:=w>2;',
  4925. ' end;',
  4926. '']);
  4927. ConvertProgram;
  4928. CheckSource('TestAnonymousProc_With',
  4929. LinesToStr([ // statements
  4930. 'rtl.createClass(this, "TObject", null, function () {',
  4931. ' this.$init = function () {',
  4932. ' this.b = false;',
  4933. ' };',
  4934. ' this.$final = function () {',
  4935. ' };',
  4936. '});',
  4937. 'this.p = null;',
  4938. 'this.bird = null;',
  4939. '']),
  4940. LinesToStr([
  4941. 'var $with = $mod.bird;',
  4942. '$mod.p = function (w) {',
  4943. ' $with.b = w > 2;',
  4944. '};',
  4945. '']));
  4946. end;
  4947. procedure TTestModule.TestAnonymousProc_ExceptOn;
  4948. begin
  4949. StartProgram(false);
  4950. Add([
  4951. 'type',
  4952. ' TProc = reference to procedure;',
  4953. ' TObject = class',
  4954. ' b: boolean;',
  4955. ' end;',
  4956. 'procedure DoIt;',
  4957. 'var',
  4958. ' p: TProc;',
  4959. 'begin',
  4960. ' try',
  4961. ' except',
  4962. ' on E: TObject do',
  4963. ' p:=procedure',
  4964. ' begin',
  4965. ' E.b:=true;',
  4966. ' end;',
  4967. ' end;',
  4968. 'end;',
  4969. 'begin']);
  4970. ConvertProgram;
  4971. CheckSource('TestAnonymousProc_ExceptOn',
  4972. LinesToStr([ // statements
  4973. 'rtl.createClass(this, "TObject", null, function () {',
  4974. ' this.$init = function () {',
  4975. ' this.b = false;',
  4976. ' };',
  4977. ' this.$final = function () {',
  4978. ' };',
  4979. '});',
  4980. 'this.DoIt = function () {',
  4981. ' var p = null;',
  4982. ' try {} catch ($e) {',
  4983. ' if ($mod.TObject.isPrototypeOf($e)) {',
  4984. ' var E = $e;',
  4985. ' p = function () {',
  4986. ' E.b = true;',
  4987. ' };',
  4988. ' } else throw $e',
  4989. ' };',
  4990. '};',
  4991. '']),
  4992. LinesToStr([
  4993. '']));
  4994. end;
  4995. procedure TTestModule.TestAnonymousProc_Nested;
  4996. begin
  4997. StartProgram(false);
  4998. Add([
  4999. 'type',
  5000. ' TProc = reference to procedure;',
  5001. ' TObject = class',
  5002. ' i: byte;',
  5003. ' procedure DoIt;',
  5004. ' end;',
  5005. 'procedure TObject.DoIt;',
  5006. 'var',
  5007. ' p: TProc;',
  5008. ' procedure Sub;',
  5009. ' begin',
  5010. ' p:=procedure',
  5011. ' begin',
  5012. ' i:=3;',
  5013. ' Self.i:=4;',
  5014. ' p:=procedure',
  5015. ' procedure SubSub;',
  5016. ' begin',
  5017. ' i:=13;',
  5018. ' Self.i:=14;',
  5019. ' end;',
  5020. ' begin',
  5021. ' i:=13;',
  5022. ' Self.i:=14;',
  5023. ' end;',
  5024. ' end;',
  5025. ' end;',
  5026. 'begin',
  5027. 'end;',
  5028. 'begin']);
  5029. ConvertProgram;
  5030. CheckSource('TestAnonymousProc_Nested',
  5031. LinesToStr([ // statements
  5032. 'rtl.createClass(this, "TObject", null, function () {',
  5033. ' this.$init = function () {',
  5034. ' this.i = 0;',
  5035. ' };',
  5036. ' this.$final = function () {',
  5037. ' };',
  5038. ' this.DoIt = function () {',
  5039. ' var $Self = this;',
  5040. ' var p = null;',
  5041. ' function Sub() {',
  5042. ' p = function () {',
  5043. ' $Self.i = 3;',
  5044. ' $Self.i = 4;',
  5045. ' p = function () {',
  5046. ' function SubSub() {',
  5047. ' $Self.i = 13;',
  5048. ' $Self.i = 14;',
  5049. ' };',
  5050. ' $Self.i = 13;',
  5051. ' $Self.i = 14;',
  5052. ' };',
  5053. ' };',
  5054. ' };',
  5055. ' };',
  5056. '});',
  5057. '']),
  5058. LinesToStr([
  5059. '']));
  5060. end;
  5061. procedure TTestModule.TestAnonymousProc_NestedAssignResult;
  5062. begin
  5063. StartProgram(false);
  5064. Add([
  5065. 'type',
  5066. ' TProc = reference to procedure;',
  5067. 'function DoIt: TProc;',
  5068. ' function Sub: TProc;',
  5069. ' begin',
  5070. ' Result:=procedure',
  5071. ' begin',
  5072. ' Sub:=procedure',
  5073. ' procedure SubSub;',
  5074. ' begin',
  5075. ' Result:=nil;',
  5076. ' Sub:=nil;',
  5077. ' DoIt:=nil;',
  5078. ' end;',
  5079. ' begin',
  5080. ' Result:=nil;',
  5081. ' Sub:=nil;',
  5082. ' DoIt:=nil;',
  5083. ' end;',
  5084. ' end;',
  5085. ' end;',
  5086. 'begin',
  5087. 'end;',
  5088. 'begin']);
  5089. ConvertProgram;
  5090. CheckSource('TestAnonymousProc_NestedAssignResult',
  5091. LinesToStr([ // statements
  5092. 'this.DoIt = function () {',
  5093. ' var Result = null;',
  5094. ' function Sub() {',
  5095. ' var Result$1 = null;',
  5096. ' Result$1 = function () {',
  5097. ' Result$1 = function () {',
  5098. ' function SubSub() {',
  5099. ' Result$1 = null;',
  5100. ' Result$1 = null;',
  5101. ' Result = null;',
  5102. ' };',
  5103. ' Result$1 = null;',
  5104. ' Result$1 = null;',
  5105. ' Result = null;',
  5106. ' };',
  5107. ' };',
  5108. ' return Result$1;',
  5109. ' };',
  5110. ' return Result;',
  5111. '};',
  5112. '']),
  5113. LinesToStr([
  5114. '']));
  5115. end;
  5116. procedure TTestModule.TestAnonymousProc_Class;
  5117. begin
  5118. StartProgram(false);
  5119. Add([
  5120. 'type',
  5121. ' TProc = reference to procedure;',
  5122. ' TEvent = procedure of object;',
  5123. ' TObject = class',
  5124. ' Size: word;',
  5125. ' function GetIt: TProc;',
  5126. ' procedure DoIt; virtual; abstract;',
  5127. ' end;',
  5128. 'function TObject.GetIt: TProc;',
  5129. 'begin',
  5130. ' Result:=procedure',
  5131. ' var p: TEvent;',
  5132. ' begin',
  5133. ' Size:=Size;',
  5134. ' Size:=Self.Size;',
  5135. ' p:=@DoIt;',
  5136. ' p:[email protected];',
  5137. ' end;',
  5138. 'end;',
  5139. 'begin']);
  5140. ConvertProgram;
  5141. CheckSource('TestAnonymousProc_Class',
  5142. LinesToStr([ // statements
  5143. 'rtl.createClass(this, "TObject", null, function () {',
  5144. ' this.$init = function () {',
  5145. ' this.Size = 0;',
  5146. ' };',
  5147. ' this.$final = function () {',
  5148. ' };',
  5149. ' this.GetIt = function () {',
  5150. ' var $Self = this;',
  5151. ' var Result = null;',
  5152. ' Result = function () {',
  5153. ' var p = null;',
  5154. ' $Self.Size = $Self.Size;',
  5155. ' $Self.Size = $Self.Size;',
  5156. ' p = rtl.createCallback($Self, "DoIt");',
  5157. ' p = rtl.createCallback($Self, "DoIt");',
  5158. ' };',
  5159. ' return Result;',
  5160. ' };',
  5161. '});',
  5162. '']),
  5163. LinesToStr([
  5164. '']));
  5165. end;
  5166. procedure TTestModule.TestAnonymousProc_ForLoop;
  5167. begin
  5168. StartProgram(false);
  5169. Add([
  5170. 'type TProc = reference to procedure;',
  5171. 'procedure Foo(p: TProc);',
  5172. 'begin',
  5173. 'end;',
  5174. 'procedure DoIt;',
  5175. 'var i: word;',
  5176. ' a: word;',
  5177. 'begin',
  5178. ' for i:=1 to 10 do begin',
  5179. ' Foo(procedure begin a:=3; end);',
  5180. ' end;',
  5181. 'end;',
  5182. 'begin',
  5183. ' DoIt;']);
  5184. ConvertProgram;
  5185. CheckSource('TestAnonymousProc_ForLoop',
  5186. LinesToStr([ // statements
  5187. 'this.Foo = function (p) {',
  5188. '};',
  5189. 'this.DoIt = function () {',
  5190. ' var i = 0;',
  5191. ' var a = 0;',
  5192. ' for (i = 1; i <= 10; i++) {',
  5193. ' $mod.Foo(function () {',
  5194. ' a = 3;',
  5195. ' });',
  5196. ' };',
  5197. '};',
  5198. '']),
  5199. LinesToStr([
  5200. '$mod.DoIt();'
  5201. ]));
  5202. end;
  5203. procedure TTestModule.TestAnonymousProc_AsmDelphi;
  5204. begin
  5205. StartProgram(false);
  5206. Add([
  5207. '{$mode delphi}',
  5208. 'type',
  5209. ' TProc = reference to procedure;',
  5210. ' TFunc = reference to function(x: word): word;',
  5211. 'procedure Run;',
  5212. 'asm',
  5213. 'end;',
  5214. 'procedure Walk(p: TProc; f: TFunc);',
  5215. 'begin',
  5216. ' Walk(procedure asm end, function(b:word): word asm return 1+b; end);',
  5217. 'end;',
  5218. 'begin',
  5219. ' Walk(procedure',
  5220. ' asm',
  5221. ' console.log("a");',
  5222. ' end,',
  5223. ' function(x: word): word asm',
  5224. ' console.log("c");',
  5225. ' end);',
  5226. '']);
  5227. ConvertProgram;
  5228. CheckSource('TestAnonymousProc_AsmDelphi',
  5229. LinesToStr([ // statements
  5230. 'this.Run = function () {',
  5231. '};',
  5232. 'this.Walk = function (p, f) {',
  5233. ' $mod.Walk(function () {',
  5234. ' }, function (b) {',
  5235. ' return 1+b;',
  5236. ' });',
  5237. '};',
  5238. '']),
  5239. LinesToStr([
  5240. '$mod.Walk(function () {',
  5241. ' console.log("a");',
  5242. '}, function (x) {',
  5243. ' console.log("c");',
  5244. '});',
  5245. '']));
  5246. end;
  5247. procedure TTestModule.TestEnum_Name;
  5248. begin
  5249. StartProgram(false);
  5250. Add('type TMyEnum = (Red, Green, Blue);');
  5251. Add('var e: TMyEnum;');
  5252. Add('var f: TMyEnum = Blue;');
  5253. Add('begin');
  5254. Add(' e:=green;');
  5255. Add(' e:=default(TMyEnum);');
  5256. ConvertProgram;
  5257. CheckSource('TestEnum_Name',
  5258. LinesToStr([ // statements
  5259. 'this.TMyEnum = {',
  5260. ' "0":"Red",',
  5261. ' Red:0,',
  5262. ' "1":"Green",',
  5263. ' Green:1,',
  5264. ' "2":"Blue",',
  5265. ' Blue:2',
  5266. ' };',
  5267. 'this.e = 0;',
  5268. 'this.f = this.TMyEnum.Blue;'
  5269. ]),
  5270. LinesToStr([
  5271. '$mod.e=$mod.TMyEnum.Green;',
  5272. '$mod.e=$mod.TMyEnum.Red;'
  5273. ]));
  5274. end;
  5275. procedure TTestModule.TestEnum_Number;
  5276. begin
  5277. Converter.Options:=Converter.Options+[coEnumNumbers];
  5278. StartProgram(false);
  5279. Add('type TMyEnum = (Red, Green);');
  5280. Add('var');
  5281. Add(' e: TMyEnum;');
  5282. Add(' f: TMyEnum = Green;');
  5283. Add(' i: longint;');
  5284. Add('begin');
  5285. Add(' e:=green;');
  5286. Add(' i:=longint(e);');
  5287. ConvertProgram;
  5288. CheckSource('TestEnumNumber',
  5289. LinesToStr([ // statements
  5290. 'this.TMyEnum = {',
  5291. ' "0":"Red",',
  5292. ' Red:0,',
  5293. ' "1":"Green",',
  5294. ' Green:1',
  5295. ' };',
  5296. 'this.e = 0;',
  5297. 'this.f = 1;',
  5298. 'this.i = 0;'
  5299. ]),
  5300. LinesToStr([
  5301. '$mod.e=1;',
  5302. '$mod.i=$mod.e;'
  5303. ]));
  5304. end;
  5305. procedure TTestModule.TestEnum_ConstFail;
  5306. begin
  5307. StartProgram(false);
  5308. Add([
  5309. 'type TMyEnum = (Red = 100, Green = 101);',
  5310. 'var',
  5311. ' e: TMyEnum;',
  5312. ' f: TMyEnum = Green;',
  5313. 'begin',
  5314. ' e:=green;']);
  5315. SetExpectedPasResolverError('not yet implemented: Red:TPasEnumValue [20180126202434] "enum const"',3002);
  5316. ConvertProgram;
  5317. end;
  5318. procedure TTestModule.TestEnum_Functions;
  5319. begin
  5320. StartProgram(false);
  5321. Add([
  5322. 'type TMyEnum = (Red, Green);',
  5323. 'procedure DoIt(var e: TMyEnum; var i: word);',
  5324. 'var',
  5325. ' v: longint;',
  5326. ' s: string;',
  5327. 'begin',
  5328. ' val(s,e,v);',
  5329. ' val(s,e,i);',
  5330. 'end;',
  5331. 'var',
  5332. ' e: TMyEnum;',
  5333. ' i: longint;',
  5334. ' s: string;',
  5335. ' b: boolean;',
  5336. 'begin',
  5337. ' i:=ord(red);',
  5338. ' i:=ord(green);',
  5339. ' i:=ord(e);',
  5340. ' i:=ord(b);',
  5341. ' e:=low(tmyenum);',
  5342. ' e:=low(e);',
  5343. ' b:=low(boolean);',
  5344. ' e:=high(tmyenum);',
  5345. ' e:=high(e);',
  5346. ' b:=high(boolean);',
  5347. ' e:=pred(green);',
  5348. ' e:=pred(e);',
  5349. ' b:=pred(b);',
  5350. ' e:=succ(red);',
  5351. ' e:=succ(e);',
  5352. ' b:=succ(b);',
  5353. ' e:=tmyenum(1);',
  5354. ' e:=tmyenum(i);',
  5355. ' s:=str(e);',
  5356. ' str(e,s);',
  5357. ' str(red,s);',
  5358. ' s:=str(e:3);',
  5359. ' writestr(s,e:3,red);',
  5360. ' val(s,e,i);',
  5361. ' i:=longint(e);']);
  5362. ConvertProgram;
  5363. CheckSource('TestEnum_Functions',
  5364. LinesToStr([ // statements
  5365. 'this.TMyEnum = {',
  5366. ' "0":"Red",',
  5367. ' Red:0,',
  5368. ' "1":"Green",',
  5369. ' Green:1',
  5370. ' };',
  5371. 'this.DoIt = function (e, i) {',
  5372. ' var v = 0;',
  5373. ' var s = "";',
  5374. ' e.set(rtl.valEnum(s, $mod.TMyEnum, function (w) {',
  5375. ' v = w;',
  5376. ' }));',
  5377. ' e.set(rtl.valEnum(s, $mod.TMyEnum, i.set));',
  5378. '};',
  5379. 'this.e = 0;',
  5380. 'this.i = 0;',
  5381. 'this.s = "";',
  5382. 'this.b = false;',
  5383. '']),
  5384. LinesToStr([
  5385. '$mod.i=$mod.TMyEnum.Red;',
  5386. '$mod.i=$mod.TMyEnum.Green;',
  5387. '$mod.i=$mod.e;',
  5388. '$mod.i=$mod.b+0;',
  5389. '$mod.e=$mod.TMyEnum.Red;',
  5390. '$mod.e=$mod.TMyEnum.Red;',
  5391. '$mod.b=false;',
  5392. '$mod.e=$mod.TMyEnum.Green;',
  5393. '$mod.e=$mod.TMyEnum.Green;',
  5394. '$mod.b=true;',
  5395. '$mod.e=$mod.TMyEnum.Green-1;',
  5396. '$mod.e=$mod.e-1;',
  5397. '$mod.b=false;',
  5398. '$mod.e=$mod.TMyEnum.Red+1;',
  5399. '$mod.e=$mod.e+1;',
  5400. '$mod.b=true;',
  5401. '$mod.e=1;',
  5402. '$mod.e=$mod.i;',
  5403. '$mod.s = $mod.TMyEnum[$mod.e];',
  5404. '$mod.s = $mod.TMyEnum[$mod.e];',
  5405. '$mod.s = $mod.TMyEnum[$mod.TMyEnum.Red];',
  5406. '$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3);',
  5407. '$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3)+$mod.TMyEnum[$mod.TMyEnum.Red];',
  5408. '$mod.e = rtl.valEnum($mod.s, $mod.TMyEnum, function (v) {',
  5409. ' $mod.i = v;',
  5410. '});',
  5411. '$mod.i=$mod.e;',
  5412. '']));
  5413. end;
  5414. procedure TTestModule.TestEnumRg_Functions;
  5415. begin
  5416. StartProgram(false);
  5417. Add([
  5418. 'type',
  5419. ' TEnum = (Red, Green, Blue);',
  5420. ' TEnumRg = Green..Blue;',
  5421. 'procedure DoIt(var e: TEnumRg; var i: word);',
  5422. 'var',
  5423. ' v: longint;',
  5424. ' s: string;',
  5425. 'begin',
  5426. ' val(s,e,v);',
  5427. ' val(s,e,i);',
  5428. 'end;',
  5429. 'var',
  5430. ' e: TEnumRg;',
  5431. ' i: longint;',
  5432. ' s: string;',
  5433. 'begin',
  5434. ' i:=ord(green);',
  5435. ' i:=ord(e);',
  5436. ' e:=low(tenumrg);',
  5437. ' e:=low(e);',
  5438. ' e:=high(tenumrg);',
  5439. ' e:=high(e);',
  5440. ' e:=pred(blue);',
  5441. ' e:=pred(e);',
  5442. ' e:=succ(green);',
  5443. ' e:=succ(e);',
  5444. ' e:=tenumrg(1);',
  5445. ' e:=tenumrg(i);',
  5446. ' s:=str(e);',
  5447. ' str(e,s);',
  5448. ' str(red,s);',
  5449. ' s:=str(e:3);',
  5450. ' writestr(s,e:3,blue);',
  5451. ' val(s,e,i);',
  5452. ' i:=longint(e);']);
  5453. ConvertProgram;
  5454. CheckSource('TestEnumRg_Functions',
  5455. LinesToStr([ // statements
  5456. 'this.TEnum = {',
  5457. ' "0":"Red",',
  5458. ' Red:0,',
  5459. ' "1":"Green",',
  5460. ' Green:1,',
  5461. ' "2":"Blue",',
  5462. ' Blue:2',
  5463. ' };',
  5464. 'this.DoIt = function (e, i) {',
  5465. ' var v = 0;',
  5466. ' var s = "";',
  5467. ' e.set(rtl.valEnum(s, $mod.TEnum, function (w) {',
  5468. ' v = w;',
  5469. ' }));',
  5470. ' e.set(rtl.valEnum(s, $mod.TEnum, i.set));',
  5471. '};',
  5472. 'this.e = this.TEnum.Green;',
  5473. 'this.i = 0;',
  5474. 'this.s = "";',
  5475. '']),
  5476. LinesToStr([
  5477. '$mod.i=$mod.TEnum.Green;',
  5478. '$mod.i=$mod.e;',
  5479. '$mod.e=$mod.TEnum.Green;',
  5480. '$mod.e=$mod.TEnum.Green;',
  5481. '$mod.e=$mod.TEnum.Blue;',
  5482. '$mod.e=$mod.TEnum.Blue;',
  5483. '$mod.e=$mod.TEnum.Blue-1;',
  5484. '$mod.e=$mod.e-1;',
  5485. '$mod.e=$mod.TEnum.Green+1;',
  5486. '$mod.e=$mod.e+1;',
  5487. '$mod.e=1;',
  5488. '$mod.e=$mod.i;',
  5489. '$mod.s = $mod.TEnum[$mod.e];',
  5490. '$mod.s = $mod.TEnum[$mod.e];',
  5491. '$mod.s = $mod.TEnum[$mod.TEnum.Red];',
  5492. '$mod.s = rtl.spaceLeft($mod.TEnum[$mod.e], 3);',
  5493. '$mod.s = rtl.spaceLeft($mod.TEnum[$mod.e], 3)+$mod.TEnum[$mod.TEnum.Blue];',
  5494. '$mod.e = rtl.valEnum($mod.s, $mod.TEnum, function (v) {',
  5495. ' $mod.i = v;',
  5496. '});',
  5497. '$mod.i=$mod.e;',
  5498. '']));
  5499. end;
  5500. procedure TTestModule.TestEnum_AsParams;
  5501. begin
  5502. StartProgram(false);
  5503. Add('type TEnum = (Red,Blue);');
  5504. Add('procedure DoIt(vG: TEnum; const vH: TEnum; var vI: TEnum);');
  5505. Add('var vJ: TEnum;');
  5506. Add('begin');
  5507. Add(' vg:=vg;');
  5508. Add(' vj:=vh;');
  5509. Add(' vi:=vi;');
  5510. Add(' doit(vg,vg,vg);');
  5511. Add(' doit(vh,vh,vj);');
  5512. Add(' doit(vi,vi,vi);');
  5513. Add(' doit(vj,vj,vj);');
  5514. Add('end;');
  5515. Add('var i: TEnum;');
  5516. Add('begin');
  5517. Add(' doit(i,i,i);');
  5518. ConvertProgram;
  5519. CheckSource('TestEnum_AsParams',
  5520. LinesToStr([ // statements
  5521. 'this.TEnum = {',
  5522. ' "0": "Red",',
  5523. ' Red: 0,',
  5524. ' "1": "Blue",',
  5525. ' Blue: 1',
  5526. '};',
  5527. 'this.DoIt = function (vG,vH,vI) {',
  5528. ' var vJ = 0;',
  5529. ' vG = vG;',
  5530. ' vJ = vH;',
  5531. ' vI.set(vI.get());',
  5532. ' $mod.DoIt(vG, vG, {',
  5533. ' get: function () {',
  5534. ' return vG;',
  5535. ' },',
  5536. ' set: function (v) {',
  5537. ' vG = v;',
  5538. ' }',
  5539. ' });',
  5540. ' $mod.DoIt(vH, vH, {',
  5541. ' get: function () {',
  5542. ' return vJ;',
  5543. ' },',
  5544. ' set: function (v) {',
  5545. ' vJ = v;',
  5546. ' }',
  5547. ' });',
  5548. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  5549. ' $mod.DoIt(vJ, vJ, {',
  5550. ' get: function () {',
  5551. ' return vJ;',
  5552. ' },',
  5553. ' set: function (v) {',
  5554. ' vJ = v;',
  5555. ' }',
  5556. ' });',
  5557. '};',
  5558. 'this.i = 0;'
  5559. ]),
  5560. LinesToStr([
  5561. '$mod.DoIt($mod.i,$mod.i,{',
  5562. ' p: $mod,',
  5563. ' get: function () {',
  5564. ' return this.p.i;',
  5565. ' },',
  5566. ' set: function (v) {',
  5567. ' this.p.i = v;',
  5568. ' }',
  5569. '});'
  5570. ]));
  5571. end;
  5572. procedure TTestModule.TestEnumRange_Array;
  5573. begin
  5574. StartProgram(false);
  5575. Add([
  5576. 'type',
  5577. ' TEnum = (Red, Green, Blue);',
  5578. ' TEnumRg = green..blue;',
  5579. ' TArr = array[TEnumRg] of byte;',
  5580. ' TArr2 = array[green..blue] of byte;',
  5581. 'var',
  5582. ' a: TArr;',
  5583. ' b: TArr = (3,4);',
  5584. ' c: TArr2 = (5,6);',
  5585. 'begin',
  5586. ' a[green] := b[blue];',
  5587. ' c[green] := c[blue];',
  5588. '']);
  5589. ConvertProgram;
  5590. CheckSource('TestEnumRange_Array',
  5591. LinesToStr([ // statements
  5592. 'this.TEnum = {',
  5593. ' "0": "Red",',
  5594. ' Red: 0,',
  5595. ' "1": "Green",',
  5596. ' Green: 1,',
  5597. ' "2": "Blue",',
  5598. ' Blue: 2',
  5599. '};',
  5600. 'this.a = rtl.arraySetLength(null, 0, 2);',
  5601. 'this.b = [3, 4];',
  5602. 'this.c = [5, 6];',
  5603. '']),
  5604. LinesToStr([
  5605. ' $mod.a[$mod.TEnum.Green - 1] = $mod.b[$mod.TEnum.Blue - 1];',
  5606. ' $mod.c[$mod.TEnum.Green - 1] = $mod.c[$mod.TEnum.Blue - 1];',
  5607. '']));
  5608. end;
  5609. procedure TTestModule.TestEnum_ForIn;
  5610. begin
  5611. StartProgram(false);
  5612. Add([
  5613. 'type',
  5614. ' TEnum = (Red, Green, Blue);',
  5615. ' TEnumRg = green..blue;',
  5616. ' TArr = array[TEnum] of byte;',
  5617. ' TArrRg = array[TEnumRg] of byte;',
  5618. 'var',
  5619. ' e: TEnum;',
  5620. ' a1: TArr = (3,4,5);',
  5621. ' a2: TArrRg = (11,12);',
  5622. ' b: byte;',
  5623. 'begin',
  5624. ' for e in TEnum do ;',
  5625. ' for e in TEnumRg do ;',
  5626. ' for e in TArr do ;',
  5627. ' for e in TArrRg do ;',
  5628. ' for b in a1 do ;',
  5629. ' for b in a2 do ;',
  5630. '']);
  5631. ConvertProgram;
  5632. CheckSource('TestEnum_ForIn',
  5633. LinesToStr([ // statements
  5634. 'this.TEnum = {',
  5635. ' "0": "Red",',
  5636. ' Red: 0,',
  5637. ' "1": "Green",',
  5638. ' Green: 1,',
  5639. ' "2": "Blue",',
  5640. ' Blue: 2',
  5641. '};',
  5642. 'this.e = 0;',
  5643. 'this.a1 = [3, 4, 5];',
  5644. 'this.a2 = [11, 12];',
  5645. 'this.b = 0;',
  5646. '']),
  5647. LinesToStr([
  5648. ' for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  5649. ' for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  5650. ' for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  5651. ' for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  5652. ' for (var $in = $mod.a1, $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) $mod.b = $in[$l];',
  5653. ' for (var $in1 = $mod.a2, $l1 = 0, $end1 = rtl.length($in1) - 1; $l1 <= $end1; $l1++) $mod.b = $in1[$l1];',
  5654. '']));
  5655. end;
  5656. procedure TTestModule.TestEnum_ScopedNumber;
  5657. begin
  5658. Converter.Options:=Converter.Options+[coEnumNumbers];
  5659. StartProgram(false);
  5660. Add([
  5661. 'type',
  5662. ' TEnum = (Red, Green);',
  5663. 'var',
  5664. ' e: TEnum;',
  5665. 'begin',
  5666. ' e:=TEnum.Green;',
  5667. '']);
  5668. ConvertProgram;
  5669. CheckSource('TestEnum_ScopedNumber',
  5670. LinesToStr([ // statements
  5671. 'this.TEnum = {',
  5672. ' "0": "Red",',
  5673. ' Red: 0,',
  5674. ' "1": "Green",',
  5675. ' Green: 1',
  5676. '};',
  5677. 'this.e = 0;',
  5678. '']),
  5679. LinesToStr([
  5680. '$mod.e = 1;']));
  5681. end;
  5682. procedure TTestModule.TestEnum_InFunction;
  5683. begin
  5684. StartProgram(false);
  5685. Add([
  5686. 'const TEnum = 3;',
  5687. 'procedure DoIt;',
  5688. 'type',
  5689. ' TEnum = (Red, Green, Blue);',
  5690. ' procedure Sub;',
  5691. ' type',
  5692. ' TEnumSub = (Left, Right);',
  5693. ' var',
  5694. ' es: TEnumSub;',
  5695. ' begin',
  5696. ' es:=Left;',
  5697. ' end;',
  5698. 'var',
  5699. ' e, e2: TEnum;',
  5700. 'begin',
  5701. ' if e in [red,blue] then e2:=e;',
  5702. 'end;',
  5703. 'begin']);
  5704. ConvertProgram;
  5705. CheckSource('TestEnum_InFunction',
  5706. LinesToStr([ // statements
  5707. 'this.TEnum = 3;',
  5708. 'var TEnum$1 = {',
  5709. ' "0":"Red",',
  5710. ' Red:0,',
  5711. ' "1":"Green",',
  5712. ' Green:1,',
  5713. ' "2":"Blue",',
  5714. ' Blue:2',
  5715. ' };',
  5716. 'var TEnumSub = {',
  5717. ' "0": "Left",',
  5718. ' Left: 0,',
  5719. ' "1": "Right",',
  5720. ' Right: 1',
  5721. '};',
  5722. 'this.DoIt = function () {',
  5723. ' function Sub() {',
  5724. ' var es = 0;',
  5725. ' es = TEnumSub.Left;',
  5726. ' };',
  5727. ' var e = 0;',
  5728. ' var e2 = 0;',
  5729. ' if (e in rtl.createSet(TEnum$1.Red, TEnum$1.Blue)) e2 = e;',
  5730. '};',
  5731. '']),
  5732. LinesToStr([
  5733. '']));
  5734. end;
  5735. procedure TTestModule.TestEnum_Name_Anonymous_Unit;
  5736. begin
  5737. StartUnit(true);
  5738. Add([
  5739. 'interface',
  5740. 'var color: (red, green);',
  5741. 'implementation',
  5742. 'initialization',
  5743. ' color:=green;',
  5744. '']);
  5745. ConvertUnit;
  5746. CheckSource('TestEnum_Name_Anonymous_Unit',
  5747. LinesToStr([
  5748. 'this.color$a = {',
  5749. ' "0": "red",',
  5750. ' red: 0,',
  5751. ' "1": "green",',
  5752. ' green: 1',
  5753. '};',
  5754. 'this.color = 0;',
  5755. '']),
  5756. LinesToStr([ // this.$init
  5757. '$mod.color = $mod.color$a.green;',
  5758. '']),
  5759. LinesToStr([ // implementation
  5760. '']) );
  5761. end;
  5762. procedure TTestModule.TestSet_Enum;
  5763. begin
  5764. StartProgram(false);
  5765. Add([
  5766. 'type',
  5767. ' TColor = (Red, Green, Blue);',
  5768. ' TColors = set of TColor;',
  5769. 'var',
  5770. ' c: TColor;',
  5771. ' s: TColors;',
  5772. ' t: TColors = [];',
  5773. ' u: TColors = [Red];',
  5774. 'begin',
  5775. ' s:=[];',
  5776. ' s:=[Green];',
  5777. ' s:=[Green,Blue];',
  5778. ' s:=[Red..Blue];',
  5779. ' s:=[Red,Green..Blue];',
  5780. ' s:=[Red,c];',
  5781. ' s:=t;',
  5782. ' s:=default(TColors);',
  5783. '']);
  5784. ConvertProgram;
  5785. CheckSource('TestSet',
  5786. LinesToStr([ // statements
  5787. 'this.TColor = {',
  5788. ' "0":"Red",',
  5789. ' Red:0,',
  5790. ' "1":"Green",',
  5791. ' Green:1,',
  5792. ' "2":"Blue",',
  5793. ' Blue:2',
  5794. ' };',
  5795. 'this.c = 0;',
  5796. 'this.s = {};',
  5797. 'this.t = {};',
  5798. 'this.u = rtl.createSet(this.TColor.Red);'
  5799. ]),
  5800. LinesToStr([
  5801. '$mod.s={};',
  5802. '$mod.s=rtl.createSet($mod.TColor.Green);',
  5803. '$mod.s=rtl.createSet($mod.TColor.Green,$mod.TColor.Blue);',
  5804. '$mod.s=rtl.createSet(null,$mod.TColor.Red,$mod.TColor.Blue);',
  5805. '$mod.s=rtl.createSet($mod.TColor.Red,null,$mod.TColor.Green,$mod.TColor.Blue);',
  5806. '$mod.s=rtl.createSet($mod.TColor.Red,$mod.c);',
  5807. '$mod.s=rtl.refSet($mod.t);',
  5808. '$mod.s={};',
  5809. '']));
  5810. end;
  5811. procedure TTestModule.TestSet_Operators;
  5812. begin
  5813. StartProgram(false);
  5814. Add('type');
  5815. Add(' TColor = (Red, Green, Blue);');
  5816. Add(' TColors = set of tcolor;');
  5817. Add('var');
  5818. Add(' vC: TColor;');
  5819. Add(' vS: TColors;');
  5820. Add(' vT: TColors;');
  5821. Add(' vU: TColors;');
  5822. Add(' B: boolean;');
  5823. Add('begin');
  5824. Add(' include(vs,green);');
  5825. Add(' exclude(vs,vc);');
  5826. Add(' vs:=vt+vu;');
  5827. Add(' vs:=vt+[red];');
  5828. Add(' vs:=[red]+vt;');
  5829. Add(' vs:=[red]+[green];');
  5830. Add(' vs:=vt-vu;');
  5831. Add(' vs:=vt-[red];');
  5832. Add(' vs:=[red]-vt;');
  5833. Add(' vs:=[red]-[green];');
  5834. Add(' vs:=vt*vu;');
  5835. Add(' vs:=vt*[red];');
  5836. Add(' vs:=[red]*vt;');
  5837. Add(' vs:=[red]*[green];');
  5838. Add(' vs:=vt><vu;');
  5839. Add(' vs:=vt><[red];');
  5840. Add(' vs:=[red]><vt;');
  5841. Add(' vs:=[red]><[green];');
  5842. Add(' b:=vt=vu;');
  5843. Add(' b:=vt=[red];');
  5844. Add(' b:=[red]=vt;');
  5845. Add(' b:=[red]=[green];');
  5846. Add(' b:=vt<>vu;');
  5847. Add(' b:=vt<>[red];');
  5848. Add(' b:=[red]<>vt;');
  5849. Add(' b:=[red]<>[green];');
  5850. Add(' b:=vt<=vu;');
  5851. Add(' b:=vt<=[red];');
  5852. Add(' b:=[red]<=vt;');
  5853. Add(' b:=[red]<=[green];');
  5854. Add(' b:=vt>=vu;');
  5855. Add(' b:=vt>=[red];');
  5856. Add(' b:=[red]>=vt;');
  5857. Add(' b:=[red]>=[green];');
  5858. ConvertProgram;
  5859. CheckSource('TestSet_Operators',
  5860. LinesToStr([ // statements
  5861. 'this.TColor = {',
  5862. ' "0":"Red",',
  5863. ' Red:0,',
  5864. ' "1":"Green",',
  5865. ' Green:1,',
  5866. ' "2":"Blue",',
  5867. ' Blue:2',
  5868. ' };',
  5869. 'this.vC = 0;',
  5870. 'this.vS = {};',
  5871. 'this.vT = {};',
  5872. 'this.vU = {};',
  5873. 'this.B = false;'
  5874. ]),
  5875. LinesToStr([
  5876. '$mod.vS = rtl.includeSet($mod.vS,$mod.TColor.Green);',
  5877. '$mod.vS = rtl.excludeSet($mod.vS,$mod.vC);',
  5878. '$mod.vS = rtl.unionSet($mod.vT, $mod.vU);',
  5879. '$mod.vS = rtl.unionSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5880. '$mod.vS = rtl.unionSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5881. '$mod.vS = rtl.unionSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5882. '$mod.vS = rtl.diffSet($mod.vT, $mod.vU);',
  5883. '$mod.vS = rtl.diffSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5884. '$mod.vS = rtl.diffSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5885. '$mod.vS = rtl.diffSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5886. '$mod.vS = rtl.intersectSet($mod.vT, $mod.vU);',
  5887. '$mod.vS = rtl.intersectSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5888. '$mod.vS = rtl.intersectSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5889. '$mod.vS = rtl.intersectSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5890. '$mod.vS = rtl.symDiffSet($mod.vT, $mod.vU);',
  5891. '$mod.vS = rtl.symDiffSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5892. '$mod.vS = rtl.symDiffSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5893. '$mod.vS = rtl.symDiffSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5894. '$mod.B = rtl.eqSet($mod.vT, $mod.vU);',
  5895. '$mod.B = rtl.eqSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5896. '$mod.B = rtl.eqSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5897. '$mod.B = rtl.eqSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5898. '$mod.B = rtl.neSet($mod.vT, $mod.vU);',
  5899. '$mod.B = rtl.neSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5900. '$mod.B = rtl.neSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5901. '$mod.B = rtl.neSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5902. '$mod.B = rtl.leSet($mod.vT, $mod.vU);',
  5903. '$mod.B = rtl.leSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5904. '$mod.B = rtl.leSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5905. '$mod.B = rtl.leSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5906. '$mod.B = rtl.geSet($mod.vT, $mod.vU);',
  5907. '$mod.B = rtl.geSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5908. '$mod.B = rtl.geSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5909. '$mod.B = rtl.geSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5910. '']));
  5911. end;
  5912. procedure TTestModule.TestSet_Operator_In;
  5913. begin
  5914. StartProgram(false);
  5915. Add([
  5916. 'type',
  5917. ' TColor = (Red, Green, Blue);',
  5918. ' TColors = set of tcolor;',
  5919. ' TColorRg = green..blue;',
  5920. 'var',
  5921. ' vC: tcolor;',
  5922. ' vT: tcolors;',
  5923. ' B: boolean;',
  5924. ' rg: TColorRg;',
  5925. 'begin',
  5926. ' b:=red in vt;',
  5927. ' b:=vc in vt;',
  5928. ' b:=green in [red..blue];',
  5929. ' b:=vc in [red..blue];',
  5930. ' ',
  5931. ' if red in vt then ;',
  5932. ' while vC in vt do ;',
  5933. ' repeat',
  5934. ' until vC in vt;',
  5935. ' if rg in [green..blue] then ;',
  5936. '']);
  5937. ConvertProgram;
  5938. CheckSource('TestSet_Operator_In',
  5939. LinesToStr([ // statements
  5940. 'this.TColor = {',
  5941. ' "0":"Red",',
  5942. ' Red:0,',
  5943. ' "1":"Green",',
  5944. ' Green:1,',
  5945. ' "2":"Blue",',
  5946. ' Blue:2',
  5947. ' };',
  5948. 'this.vC = 0;',
  5949. 'this.vT = {};',
  5950. 'this.B = false;',
  5951. 'this.rg = this.TColor.Green;',
  5952. '']),
  5953. LinesToStr([
  5954. '$mod.B = $mod.TColor.Red in $mod.vT;',
  5955. '$mod.B = $mod.vC in $mod.vT;',
  5956. '$mod.B = $mod.TColor.Green in rtl.createSet(null, $mod.TColor.Red, $mod.TColor.Blue);',
  5957. '$mod.B = $mod.vC in rtl.createSet(null, $mod.TColor.Red, $mod.TColor.Blue);',
  5958. 'if ($mod.TColor.Red in $mod.vT) ;',
  5959. 'while ($mod.vC in $mod.vT) {',
  5960. '};',
  5961. 'do {',
  5962. '} while (!($mod.vC in $mod.vT));',
  5963. 'if ($mod.rg in rtl.createSet(null, $mod.TColor.Green, $mod.TColor.Blue)) ;',
  5964. '']));
  5965. end;
  5966. procedure TTestModule.TestSet_Functions;
  5967. begin
  5968. StartProgram(false);
  5969. Add('type');
  5970. Add(' TMyEnum = (Red, Green);');
  5971. Add(' TMyEnums = set of TMyEnum;');
  5972. Add('var');
  5973. Add(' e: TMyEnum;');
  5974. Add(' s: TMyEnums;');
  5975. Add('begin');
  5976. Add(' e:=Low(TMyEnums);');
  5977. Add(' e:=Low(s);');
  5978. Add(' e:=High(TMyEnums);');
  5979. Add(' e:=High(s);');
  5980. ConvertProgram;
  5981. CheckSource('TestSetFunctions',
  5982. LinesToStr([ // statements
  5983. 'this.TMyEnum = {',
  5984. ' "0":"Red",',
  5985. ' Red:0,',
  5986. ' "1":"Green",',
  5987. ' Green:1',
  5988. ' };',
  5989. 'this.e = 0;',
  5990. 'this.s = {};'
  5991. ]),
  5992. LinesToStr([
  5993. '$mod.e=$mod.TMyEnum.Red;',
  5994. '$mod.e=$mod.TMyEnum.Red;',
  5995. '$mod.e=$mod.TMyEnum.Green;',
  5996. '$mod.e=$mod.TMyEnum.Green;',
  5997. '']));
  5998. end;
  5999. procedure TTestModule.TestSet_PassAsArgClone;
  6000. begin
  6001. StartProgram(false);
  6002. Add('type');
  6003. Add(' TMyEnum = (Red, Green);');
  6004. Add(' TMyEnums = set of TMyEnum;');
  6005. Add('procedure DoDefault(s: tmyenums); begin end;');
  6006. Add('procedure DoConst(const s: tmyenums); begin end;');
  6007. Add('var');
  6008. Add(' aSet: tmyenums;');
  6009. Add('begin');
  6010. Add(' dodefault(aset);');
  6011. Add(' doconst(aset);');
  6012. ConvertProgram;
  6013. CheckSource('TestSetFunctions',
  6014. LinesToStr([ // statements
  6015. 'this.TMyEnum = {',
  6016. ' "0":"Red",',
  6017. ' Red:0,',
  6018. ' "1":"Green",',
  6019. ' Green:1',
  6020. ' };',
  6021. 'this.DoDefault = function (s) {',
  6022. '};',
  6023. 'this.DoConst = function (s) {',
  6024. '};',
  6025. 'this.aSet = {};'
  6026. ]),
  6027. LinesToStr([
  6028. '$mod.DoDefault(rtl.refSet($mod.aSet));',
  6029. '$mod.DoConst($mod.aSet);',
  6030. '']));
  6031. end;
  6032. procedure TTestModule.TestSet_AsParams;
  6033. begin
  6034. StartProgram(false);
  6035. Add([
  6036. 'type TEnum = (Red,Blue);',
  6037. 'type TEnums = set of TEnum;',
  6038. 'function DoIt(vG: TEnums; const vH: TEnums; var vI: TEnums): TEnums;',
  6039. 'var vJ: TEnums;',
  6040. 'begin',
  6041. ' Include(vg,red);',
  6042. ' Include(result,blue);',
  6043. ' vg:=vg;',
  6044. ' vj:=vh;',
  6045. ' vi:=vi;',
  6046. ' doit(vg,vg,vg);',
  6047. ' doit(vh,vh,vj);',
  6048. ' doit(vi,vi,vi);',
  6049. ' doit(vj,vj,vj);',
  6050. 'end;',
  6051. 'var i: TEnums;',
  6052. 'begin',
  6053. ' doit(i,i,i);']);
  6054. ConvertProgram;
  6055. CheckSource('TestSet_AsParams',
  6056. LinesToStr([ // statements
  6057. 'this.TEnum = {',
  6058. ' "0": "Red",',
  6059. ' Red: 0,',
  6060. ' "1": "Blue",',
  6061. ' Blue: 1',
  6062. '};',
  6063. 'this.DoIt = function (vG,vH,vI) {',
  6064. ' var Result = {};',
  6065. ' var vJ = {};',
  6066. ' vG = rtl.includeSet(vG, $mod.TEnum.Red);',
  6067. ' Result = rtl.includeSet(Result, $mod.TEnum.Blue);',
  6068. ' vG = rtl.refSet(vG);',
  6069. ' vJ = rtl.refSet(vH);',
  6070. ' vI.set(rtl.refSet(vI.get()));',
  6071. ' $mod.DoIt(rtl.refSet(vG), vG, {',
  6072. ' get: function () {',
  6073. ' return vG;',
  6074. ' },',
  6075. ' set: function (v) {',
  6076. ' vG = v;',
  6077. ' }',
  6078. ' });',
  6079. ' $mod.DoIt(rtl.refSet(vH), vH, {',
  6080. ' get: function () {',
  6081. ' return vJ;',
  6082. ' },',
  6083. ' set: function (v) {',
  6084. ' vJ = v;',
  6085. ' }',
  6086. ' });',
  6087. ' $mod.DoIt(rtl.refSet(vI.get()), vI.get(), vI);',
  6088. ' $mod.DoIt(rtl.refSet(vJ), vJ, {',
  6089. ' get: function () {',
  6090. ' return vJ;',
  6091. ' },',
  6092. ' set: function (v) {',
  6093. ' vJ = v;',
  6094. ' }',
  6095. ' });',
  6096. ' return Result;',
  6097. '};',
  6098. 'this.i = {};'
  6099. ]),
  6100. LinesToStr([
  6101. '$mod.DoIt(rtl.refSet($mod.i),$mod.i,{',
  6102. ' p: $mod,',
  6103. ' get: function () {',
  6104. ' return this.p.i;',
  6105. ' },',
  6106. ' set: function (v) {',
  6107. ' this.p.i = v;',
  6108. ' }',
  6109. '});'
  6110. ]));
  6111. end;
  6112. procedure TTestModule.TestSet_Property;
  6113. begin
  6114. StartProgram(false);
  6115. Add('type');
  6116. Add(' TEnum = (Red,Blue);');
  6117. Add(' TEnums = set of TEnum;');
  6118. Add(' TObject = class');
  6119. Add(' function GetColors: TEnums; external name ''GetColors'';');
  6120. Add(' procedure SetColors(const Value: TEnums); external name ''SetColors'';');
  6121. Add(' property Colors: TEnums read GetColors write SetColors;');
  6122. Add(' end;');
  6123. Add('procedure DoIt(i: TEnums; const j: TEnums; var k: TEnums; out l: TEnums);');
  6124. Add('begin end;');
  6125. Add('var Obj: TObject;');
  6126. Add('begin');
  6127. Add(' Include(Obj.Colors,Red);');
  6128. Add(' Exclude(Obj.Colors,Red);');
  6129. //Add(' DoIt(Obj.Colors,Obj.Colors,Obj.Colors,Obj.Colors);');
  6130. ConvertProgram;
  6131. CheckSource('TestSet_Property',
  6132. LinesToStr([ // statements
  6133. 'this.TEnum = {',
  6134. ' "0": "Red",',
  6135. ' Red: 0,',
  6136. ' "1": "Blue",',
  6137. ' Blue: 1',
  6138. '};',
  6139. 'rtl.createClass(this, "TObject", null, function () {',
  6140. ' this.$init = function () {',
  6141. ' };',
  6142. ' this.$final = function () {',
  6143. ' };',
  6144. '});',
  6145. 'this.DoIt = function (i, j, k, l) {',
  6146. '};',
  6147. 'this.Obj = null;',
  6148. '']),
  6149. LinesToStr([
  6150. '$mod.Obj.SetColors(rtl.includeSet($mod.Obj.GetColors(), $mod.TEnum.Red));',
  6151. '$mod.Obj.SetColors(rtl.excludeSet($mod.Obj.GetColors(), $mod.TEnum.Red));',
  6152. '']));
  6153. end;
  6154. procedure TTestModule.TestSet_EnumConst;
  6155. begin
  6156. StartProgram(false);
  6157. Add([
  6158. 'type',
  6159. ' TEnum = (Red,Blue);',
  6160. ' TEnums = set of TEnum;',
  6161. 'const',
  6162. ' Orange = red;',
  6163. 'var',
  6164. ' Enum: tenum;',
  6165. ' Enums: tenums;',
  6166. 'begin',
  6167. ' Include(enums,orange);',
  6168. ' Exclude(enums,orange);',
  6169. ' if orange in enums then;',
  6170. ' if orange in [orange,red] then;']);
  6171. ConvertProgram;
  6172. CheckSource('TestSet_EnumConst',
  6173. LinesToStr([ // statements
  6174. 'this.TEnum = {',
  6175. ' "0": "Red",',
  6176. ' Red: 0,',
  6177. ' "1": "Blue",',
  6178. ' Blue: 1',
  6179. '};',
  6180. 'this.Orange = this.TEnum.Red;',
  6181. 'this.Enum = 0;',
  6182. 'this.Enums = {};',
  6183. '']),
  6184. LinesToStr([
  6185. '$mod.Enums = rtl.includeSet($mod.Enums, $mod.TEnum.Red);',
  6186. '$mod.Enums = rtl.excludeSet($mod.Enums, $mod.TEnum.Red);',
  6187. 'if ($mod.TEnum.Red in $mod.Enums) ;',
  6188. 'if ($mod.TEnum.Red in rtl.createSet($mod.TEnum.Red, $mod.TEnum.Red)) ;',
  6189. '']));
  6190. end;
  6191. procedure TTestModule.TestSet_IntConst;
  6192. begin
  6193. StartProgram(false);
  6194. Add([
  6195. 'type',
  6196. ' TEnums = set of Byte;',
  6197. 'const',
  6198. ' Orange = 0;',
  6199. 'var',
  6200. ' Enum: byte;',
  6201. ' Enums: tenums;',
  6202. 'begin',
  6203. ' Enums:=[];',
  6204. ' Enums:=[0];',
  6205. ' Enums:=[1..2];',
  6206. //' Include(enums,orange);',
  6207. //' Exclude(enums,orange);',
  6208. ' if orange in enums then;',
  6209. ' if orange in [orange,1] then;']);
  6210. ConvertProgram;
  6211. CheckSource('TestSet_IntConst',
  6212. LinesToStr([ // statements
  6213. 'this.Orange = 0;',
  6214. 'this.Enum = 0;',
  6215. 'this.Enums = {};',
  6216. '']),
  6217. LinesToStr([
  6218. '$mod.Enums = {};',
  6219. '$mod.Enums = rtl.createSet(0);',
  6220. '$mod.Enums = rtl.createSet(null, 1, 2);',
  6221. 'if (0 in $mod.Enums) ;',
  6222. 'if (0 in rtl.createSet(0, 1)) ;',
  6223. '']));
  6224. end;
  6225. procedure TTestModule.TestSet_IntRange;
  6226. begin
  6227. StartProgram(false);
  6228. Add([
  6229. 'type',
  6230. ' TRange = 1..3;',
  6231. ' TEnums = set of TRange;',
  6232. 'const',
  6233. ' Orange = 2;',
  6234. 'var',
  6235. ' Enum: byte;',
  6236. ' Enums: TEnums;',
  6237. 'begin',
  6238. ' Enums:=[];',
  6239. ' Enums:=[1];',
  6240. ' Enums:=[2..3];',
  6241. ' Include(enums,orange);',
  6242. ' Exclude(enums,orange);',
  6243. ' if orange in enums then;',
  6244. ' if orange in [orange,1] then;']);
  6245. ConvertProgram;
  6246. CheckSource('TestSet_IntRange',
  6247. LinesToStr([ // statements
  6248. 'this.Orange = 2;',
  6249. 'this.Enum = 0;',
  6250. 'this.Enums = {};',
  6251. '']),
  6252. LinesToStr([
  6253. '$mod.Enums = {};',
  6254. '$mod.Enums = rtl.createSet(1);',
  6255. '$mod.Enums = rtl.createSet(null, 2, 3);',
  6256. '$mod.Enums = rtl.includeSet($mod.Enums, 2);',
  6257. '$mod.Enums = rtl.excludeSet($mod.Enums, 2);',
  6258. 'if (2 in $mod.Enums) ;',
  6259. 'if (2 in rtl.createSet(2, 1)) ;',
  6260. '']));
  6261. end;
  6262. procedure TTestModule.TestSet_AnonymousEnumType;
  6263. begin
  6264. StartProgram(false);
  6265. Add('type');
  6266. Add(' TFlags = set of (red, green);');
  6267. Add('const');
  6268. Add(' favorite = red;');
  6269. Add('var');
  6270. Add(' f: TFlags;');
  6271. Add(' i: longint;');
  6272. Add('begin');
  6273. Add(' Include(f,red);');
  6274. Add(' Include(f,favorite);');
  6275. Add(' i:=ord(red);');
  6276. Add(' i:=ord(favorite);');
  6277. Add(' i:=ord(low(TFlags));');
  6278. Add(' i:=ord(low(f));');
  6279. Add(' i:=ord(low(favorite));');
  6280. Add(' i:=ord(high(TFlags));');
  6281. Add(' i:=ord(high(f));');
  6282. Add(' i:=ord(high(favorite));');
  6283. Add(' f:=[green,favorite];');
  6284. ConvertProgram;
  6285. CheckSource('TestSet_AnonymousEnumType',
  6286. LinesToStr([ // statements
  6287. 'this.TFlags$a = {',
  6288. ' "0": "red",',
  6289. ' red: 0,',
  6290. ' "1": "green",',
  6291. ' green: 1',
  6292. '};',
  6293. 'this.favorite = this.TFlags$a.red;',
  6294. 'this.f = {};',
  6295. 'this.i = 0;',
  6296. '']),
  6297. LinesToStr([
  6298. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  6299. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  6300. '$mod.i = $mod.TFlags$a.red;',
  6301. '$mod.i = $mod.TFlags$a.red;',
  6302. '$mod.i = $mod.TFlags$a.red;',
  6303. '$mod.i = $mod.TFlags$a.red;',
  6304. '$mod.i = $mod.TFlags$a.red;',
  6305. '$mod.i = $mod.TFlags$a.green;',
  6306. '$mod.i = $mod.TFlags$a.green;',
  6307. '$mod.i = $mod.TFlags$a.green;',
  6308. '$mod.f = rtl.createSet($mod.TFlags$a.green, $mod.TFlags$a.red);',
  6309. '']));
  6310. end;
  6311. procedure TTestModule.TestSet_AnonymousEnumTypeChar;
  6312. begin
  6313. exit;
  6314. StartProgram(false);
  6315. Add([
  6316. 'type',
  6317. ' TAtoZ = ''A''..''Z'';',
  6318. ' TSetOfAZ = set of TAtoZ;',
  6319. 'var',
  6320. ' c: char;',
  6321. ' a: TAtoZ;',
  6322. ' s: TSetOfAZ = [''P'',''A''];',
  6323. ' i: longint;',
  6324. 'begin',
  6325. ' Include(s,''S'');',
  6326. ' Include(s,c);',
  6327. ' Include(s,a);',
  6328. ' c:=low(TAtoZ);',
  6329. ' i:=ord(low(TAtoZ));',
  6330. ' a:=high(TAtoZ);',
  6331. ' a:=high(TSetOfAtoZ);',
  6332. ' s:=[a,c,''M''];',
  6333. '']);
  6334. ConvertProgram;
  6335. CheckSource('TestSet_AnonymousEnumTypeChar',
  6336. LinesToStr([ // statements
  6337. '']),
  6338. LinesToStr([
  6339. '']));
  6340. end;
  6341. procedure TTestModule.TestSet_ConstEnum;
  6342. begin
  6343. StartProgram(false);
  6344. Add([
  6345. 'type',
  6346. ' TEnum = (red,blue,green);',
  6347. ' TEnums = set of TEnum;',
  6348. 'const',
  6349. ' teAny = [low(TEnum)..high(TEnum)];',
  6350. ' teRedBlue = [low(TEnum)..pred(high(TEnum))];',
  6351. 'var',
  6352. ' e: TEnum;',
  6353. ' s: TEnums;',
  6354. 'begin',
  6355. ' if blue in teAny then;',
  6356. ' if blue in teAny+[e] then;',
  6357. ' if blue in teAny+teRedBlue then;',
  6358. ' if e in [red,blue] then;',
  6359. ' s:=teAny;',
  6360. ' s:=teAny+[e];',
  6361. ' s:=[e]+teAny;',
  6362. ' s:=teAny+teRedBlue;',
  6363. ' s:=teAny+teRedBlue+[e];',
  6364. '']);
  6365. ConvertProgram;
  6366. CheckSource('TestSet_ConstEnum',
  6367. LinesToStr([ // statements
  6368. 'this.TEnum = {',
  6369. ' "0": "red",',
  6370. ' red: 0,',
  6371. ' "1": "blue",',
  6372. ' blue: 1,',
  6373. ' "2": "green",',
  6374. ' green: 2',
  6375. '};',
  6376. 'this.teAny = rtl.createSet(null, this.TEnum.red, this.TEnum.green);',
  6377. 'this.teRedBlue = rtl.createSet(null, this.TEnum.red, this.TEnum.green - 1);',
  6378. 'this.e = 0;',
  6379. 'this.s = {};',
  6380. '']),
  6381. LinesToStr([
  6382. 'if ($mod.TEnum.blue in $mod.teAny) ;',
  6383. 'if ($mod.TEnum.blue in rtl.unionSet($mod.teAny, rtl.createSet($mod.e))) ;',
  6384. 'if ($mod.TEnum.blue in rtl.unionSet($mod.teAny, $mod.teRedBlue)) ;',
  6385. 'if ($mod.e in rtl.createSet($mod.TEnum.red, $mod.TEnum.blue)) ;',
  6386. '$mod.s = rtl.refSet($mod.teAny);',
  6387. '$mod.s = rtl.unionSet($mod.teAny, rtl.createSet($mod.e));',
  6388. '$mod.s = rtl.unionSet(rtl.createSet($mod.e), $mod.teAny);',
  6389. '$mod.s = rtl.unionSet($mod.teAny, $mod.teRedBlue);',
  6390. '$mod.s = rtl.unionSet(rtl.unionSet($mod.teAny, $mod.teRedBlue), rtl.createSet($mod.e));',
  6391. '']));
  6392. end;
  6393. procedure TTestModule.TestSet_ConstChar;
  6394. begin
  6395. StartProgram(false);
  6396. Add([
  6397. 'const',
  6398. ' LowChars = [''a''..''z''];',
  6399. ' Chars = LowChars+[''A''..''Z''];',
  6400. ' sc = [''А'', ''Я''];',
  6401. 'var',
  6402. ' c: char;',
  6403. ' s: string;',
  6404. 'begin',
  6405. ' if c in lowchars then ;',
  6406. ' if ''a'' in lowchars then ;',
  6407. ' if s[1] in lowchars then ;',
  6408. ' if c in chars then ;',
  6409. ' if c in [''a''..''z'',''_''] then ;',
  6410. ' if ''b'' in [''a''..''z'',''_''] then ;',
  6411. ' if ''Я'' in sc then ;',
  6412. ' if 3=ord('' '') then ;',
  6413. '']);
  6414. ConvertProgram;
  6415. CheckSource('TestSet_ConstChar',
  6416. LinesToStr([ // statements
  6417. 'this.LowChars = rtl.createSet(null, 97, 122);',
  6418. 'this.Chars = rtl.unionSet(this.LowChars, rtl.createSet(null, 65, 90));',
  6419. 'this.sc = rtl.createSet(1040, 1071);',
  6420. 'this.c = "";',
  6421. 'this.s = "";',
  6422. '']),
  6423. LinesToStr([
  6424. 'if ($mod.c.charCodeAt() in $mod.LowChars) ;',
  6425. 'if (97 in $mod.LowChars) ;',
  6426. 'if ($mod.s.charCodeAt(0) in $mod.LowChars) ;',
  6427. 'if ($mod.c.charCodeAt() in $mod.Chars) ;',
  6428. 'if ($mod.c.charCodeAt() in rtl.createSet(null, 97, 122, 95)) ;',
  6429. 'if (98 in rtl.createSet(null, 97, 122, 95)) ;',
  6430. 'if (1071 in $mod.sc) ;',
  6431. 'if (3 === 32) ;',
  6432. '']));
  6433. end;
  6434. procedure TTestModule.TestSet_ConstInt;
  6435. begin
  6436. StartProgram(false);
  6437. Add([
  6438. 'const',
  6439. ' Months = [1..12];',
  6440. ' Mirror = [-12..-1]+Months;',
  6441. 'var',
  6442. ' i: smallint;',
  6443. 'begin',
  6444. ' if 3 in Months then;',
  6445. ' if i in Months+[i] then;',
  6446. ' if i in Months+Mirror then;',
  6447. ' if i in [4..6,8] then;',
  6448. '']);
  6449. ConvertProgram;
  6450. CheckSource('TestSet_ConstInt',
  6451. LinesToStr([ // statements
  6452. 'this.Months = rtl.createSet(null, 1, 12);',
  6453. 'this.Mirror = rtl.unionSet(rtl.createSet(null, -12, -1), this.Months);',
  6454. 'this.i = 0;',
  6455. '']),
  6456. LinesToStr([
  6457. 'if (3 in $mod.Months) ;',
  6458. 'if ($mod.i in rtl.unionSet($mod.Months, rtl.createSet($mod.i))) ;',
  6459. 'if ($mod.i in rtl.unionSet($mod.Months, $mod.Mirror)) ;',
  6460. 'if ($mod.i in rtl.createSet(null, 4, 6, 8)) ;',
  6461. '']));
  6462. end;
  6463. procedure TTestModule.TestSet_InFunction;
  6464. begin
  6465. StartProgram(false);
  6466. Add([
  6467. 'const',
  6468. ' TEnum = 3;',
  6469. ' TSetOfEnum = 4;',
  6470. ' TSetOfAno = 5;',
  6471. 'procedure DoIt;',
  6472. 'type',
  6473. ' TEnum = (red, blue);',
  6474. ' TSetOfEnum = set of TEnum;',
  6475. ' TSetOfAno = set of (up,down);',
  6476. 'var',
  6477. ' e: TEnum;',
  6478. ' se: TSetOfEnum;',
  6479. ' sa: TSetOfAno;',
  6480. 'begin',
  6481. ' se:=[e];',
  6482. ' sa:=[up];',
  6483. 'end;',
  6484. 'begin',
  6485. '']);
  6486. ConvertProgram;
  6487. CheckSource('TestSet_InFunction',
  6488. LinesToStr([ // statements
  6489. 'this.TEnum = 3;',
  6490. 'this.TSetOfEnum = 4;',
  6491. 'this.TSetOfAno = 5;',
  6492. 'var TEnum$1 = {',
  6493. ' "0": "red",',
  6494. ' red: 0,',
  6495. ' "1": "blue",',
  6496. ' blue: 1',
  6497. '};',
  6498. 'var TSetOfAno$a = {',
  6499. ' "0": "up",',
  6500. ' up: 0,',
  6501. ' "1": "down",',
  6502. ' down: 1',
  6503. '};',
  6504. 'this.DoIt = function () {',
  6505. ' var e = 0;',
  6506. ' var se = {};',
  6507. ' var sa = {};',
  6508. ' se = rtl.createSet(e);',
  6509. ' sa = rtl.createSet(TSetOfAno$a.up);',
  6510. '};',
  6511. '']),
  6512. LinesToStr([
  6513. '']));
  6514. end;
  6515. procedure TTestModule.TestSet_ForIn;
  6516. begin
  6517. StartProgram(false);
  6518. Add([
  6519. 'type',
  6520. ' TEnum = (Red, Green, Blue);',
  6521. ' TEnumRg = green..blue;',
  6522. ' TSetOfEnum = set of TEnum;',
  6523. ' TSetOfEnumRg = set of TEnumRg;',
  6524. 'var',
  6525. ' e, e2: TEnum;',
  6526. ' er: TEnum;',
  6527. ' s: TSetOfEnum;',
  6528. 'begin',
  6529. ' for e in TSetOfEnum do ;',
  6530. ' for e in TSetOfEnumRg do ;',
  6531. ' for e in [] do e2:=e;',
  6532. ' for e in [red..green] do e2:=e;',
  6533. ' for e in [green,blue] do e2:=e;',
  6534. ' for e in [red,blue] do e2:=e;',
  6535. ' for e in s do e2:=e;',
  6536. ' for er in TSetOfEnumRg do ;',
  6537. '']);
  6538. ConvertProgram;
  6539. CheckSource('TestSet_ForIn',
  6540. LinesToStr([ // statements
  6541. 'this.TEnum = {',
  6542. ' "0":"Red",',
  6543. ' Red:0,',
  6544. ' "1":"Green",',
  6545. ' Green:1,',
  6546. ' "2":"Blue",',
  6547. ' Blue:2',
  6548. ' };',
  6549. 'this.e = 0;',
  6550. 'this.e2 = 0;',
  6551. 'this.er = 0;',
  6552. 'this.s = {};',
  6553. '']),
  6554. LinesToStr([
  6555. 'for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  6556. 'for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  6557. 'for ($mod.e = 0; $mod.e <= 1; $mod.e++) $mod.e2 = $mod.e;',
  6558. 'for ($mod.e = 1; $mod.e <= 2; $mod.e++) $mod.e2 = $mod.e;',
  6559. 'for ($mod.e in rtl.createSet($mod.TEnum.Red, $mod.TEnum.Blue)) $mod.e2 = $mod.e;',
  6560. 'for (var $l in $mod.s){',
  6561. ' $mod.e = +$l;',
  6562. ' $mod.e2 = $mod.e;',
  6563. '};',
  6564. 'for ($mod.er = 1; $mod.er <= 2; $mod.er++) ;',
  6565. '']));
  6566. end;
  6567. procedure TTestModule.TestNestBegin;
  6568. begin
  6569. StartProgram(false);
  6570. Add('begin');
  6571. Add(' begin');
  6572. Add(' begin');
  6573. Add(' end;');
  6574. Add(' begin');
  6575. Add(' if true then ;');
  6576. Add(' end;');
  6577. Add(' end;');
  6578. ConvertProgram;
  6579. CheckSource('TestNestBegin',
  6580. '',
  6581. 'if (true) ;');
  6582. end;
  6583. procedure TTestModule.TestUnitImplVars;
  6584. begin
  6585. StartUnit(false);
  6586. Add('interface');
  6587. Add('implementation');
  6588. Add('var');
  6589. Add(' V1:longint;');
  6590. Add(' V2:longint = 3;');
  6591. Add(' V3:string = ''abc'';');
  6592. ConvertUnit;
  6593. CheckSource('TestUnitImplVars',
  6594. LinesToStr([ // statements
  6595. 'var $impl = $mod.$impl;',
  6596. '']),
  6597. '', // this.$init
  6598. LinesToStr([ // implementation
  6599. '$impl.V1 = 0;',
  6600. '$impl.V2 = 3;',
  6601. '$impl.V3 = "abc";',
  6602. '']) );
  6603. end;
  6604. procedure TTestModule.TestUnitImplConsts;
  6605. begin
  6606. StartUnit(false);
  6607. Add('interface');
  6608. Add('implementation');
  6609. Add('const');
  6610. Add(' v1 = 3;');
  6611. Add(' v2:longint = 4;');
  6612. Add(' v3:string = ''abc'';');
  6613. ConvertUnit;
  6614. CheckSource('TestUnitImplConsts',
  6615. LinesToStr([ // statements
  6616. 'var $impl = $mod.$impl;',
  6617. '']),
  6618. '', // this.$init
  6619. LinesToStr([ // implementation
  6620. '$impl.v1 = 3;',
  6621. '$impl.v2 = 4;',
  6622. '$impl.v3 = "abc";',
  6623. '']) );
  6624. end;
  6625. procedure TTestModule.TestUnitImplRecord;
  6626. begin
  6627. StartUnit(false);
  6628. Add('interface');
  6629. Add('implementation');
  6630. Add('type');
  6631. Add(' TMyRecord = record');
  6632. Add(' i: longint;');
  6633. Add(' end;');
  6634. Add('var aRec: TMyRecord;');
  6635. Add('initialization');
  6636. Add(' arec.i:=3;');
  6637. ConvertUnit;
  6638. CheckSource('TestUnitImplRecord',
  6639. LinesToStr([ // statements
  6640. 'var $impl = $mod.$impl;',
  6641. '']),
  6642. // this.$init
  6643. '$impl.aRec.i = 3;',
  6644. LinesToStr([ // implementation
  6645. 'rtl.recNewT($impl, "TMyRecord", function () {',
  6646. ' this.i = 0;',
  6647. ' this.$eq = function (b) {',
  6648. ' return this.i === b.i;',
  6649. ' };',
  6650. ' this.$assign = function (s) {',
  6651. ' this.i = s.i;',
  6652. ' return this;',
  6653. ' };',
  6654. '});',
  6655. '$impl.aRec = $impl.TMyRecord.$new();',
  6656. '']) );
  6657. end;
  6658. procedure TTestModule.TestRenameJSNameConflict;
  6659. begin
  6660. StartProgram(false);
  6661. Add('var apply: longint;');
  6662. Add('var bind: longint;');
  6663. Add('var call: longint;');
  6664. Add('begin');
  6665. ConvertProgram;
  6666. CheckSource('TestRenameJSNameConflict',
  6667. LinesToStr([ // statements
  6668. 'this.Apply = 0;',
  6669. 'this.Bind = 0;',
  6670. 'this.Call = 0;'
  6671. ]),
  6672. LinesToStr([ // this.$main
  6673. ''
  6674. ]));
  6675. end;
  6676. procedure TTestModule.TestLocalConst;
  6677. begin
  6678. StartProgram(false);
  6679. Add('procedure DoIt;');
  6680. Add('const');
  6681. Add(' cA: longint = 1;');
  6682. Add(' cB = 2;');
  6683. Add(' procedure Sub;');
  6684. Add(' const');
  6685. Add(' csA = 3;');
  6686. Add(' cB: double = 4;');
  6687. Add(' begin');
  6688. Add(' cb:=cb+csa;');
  6689. Add(' ca:=ca+csa+5;');
  6690. Add(' end;');
  6691. Add('begin');
  6692. Add(' ca:=ca+cb+6;');
  6693. Add('end;');
  6694. Add('begin');
  6695. ConvertProgram;
  6696. CheckSource('TestLocalConst',
  6697. LinesToStr([
  6698. 'var cA = 1;',
  6699. 'var cB = 2;',
  6700. 'var csA = 3;',
  6701. 'var cB$1 = 4;',
  6702. 'this.DoIt = function () {',
  6703. ' function Sub() {',
  6704. ' cB$1 = cB$1 + 3;',
  6705. ' cA = cA + 3 + 5;',
  6706. ' };',
  6707. ' cA = cA + 2 + 6;',
  6708. '};'
  6709. ]),
  6710. LinesToStr([
  6711. ]));
  6712. end;
  6713. procedure TTestModule.TestVarExternal;
  6714. begin
  6715. StartProgram(false);
  6716. Add('var');
  6717. Add(' NaN: double; external name ''Global.NaN'';');
  6718. Add(' d: double;');
  6719. Add('begin');
  6720. Add(' d:=NaN;');
  6721. ConvertProgram;
  6722. CheckSource('TestVarExternal',
  6723. LinesToStr([
  6724. 'this.d = 0.0;'
  6725. ]),
  6726. LinesToStr([
  6727. '$mod.d = Global.NaN;'
  6728. ]));
  6729. end;
  6730. procedure TTestModule.TestVarExternalOtherUnit;
  6731. begin
  6732. AddModuleWithIntfImplSrc('unit2.pas',
  6733. LinesToStr([
  6734. 'var NaN: double; external name ''Global.NaN'';',
  6735. 'var iV: longint;'
  6736. ]),
  6737. '');
  6738. StartUnit(true);
  6739. Add('interface');
  6740. Add('uses unit2;');
  6741. Add('implementation');
  6742. Add('var');
  6743. Add(' d: double;');
  6744. Add(' i: longint; external name ''$i'';');
  6745. Add('begin');
  6746. Add(' d:=nan;');
  6747. Add(' d:=uNit2.nan;');
  6748. Add(' d:=test1.d;');
  6749. Add(' i:=iv;');
  6750. Add(' i:=uNit2.iv;');
  6751. Add(' i:=test1.i;');
  6752. ConvertUnit;
  6753. CheckSource('TestVarExternalOtherUnit',
  6754. LinesToStr([
  6755. 'var $impl = $mod.$impl;',
  6756. '']),
  6757. LinesToStr([ // this.$init
  6758. '$impl.d = Global.NaN;',
  6759. '$impl.d = Global.NaN;',
  6760. '$impl.d = $impl.d;',
  6761. '$i = pas.unit2.iV;',
  6762. '$i = pas.unit2.iV;',
  6763. '$i = $i;',
  6764. '']),
  6765. LinesToStr([ // implementation
  6766. '$impl.d = 0.0;',
  6767. '']) );
  6768. end;
  6769. procedure TTestModule.TestVarAbsoluteFail;
  6770. begin
  6771. StartProgram(false);
  6772. Add([
  6773. 'var',
  6774. ' a: longint;',
  6775. ' b: longword absolute a;',
  6776. 'begin']);
  6777. SetExpectedPasResolverError('Invalid variable modifier "absolute"',nInvalidVariableModifier);
  6778. ConvertProgram;
  6779. end;
  6780. procedure TTestModule.TestConstExternal;
  6781. begin
  6782. StartProgram(false);
  6783. Add([
  6784. 'const',
  6785. ' PI: double; external name ''Global.PI'';',
  6786. ' Tau = 2*pi;',
  6787. 'var d: double;',
  6788. 'begin',
  6789. ' d:=pi;',
  6790. ' d:=tau+pi;']);
  6791. ConvertProgram;
  6792. CheckSource('TestConstExternal',
  6793. LinesToStr([
  6794. 'this.Tau = 2*Global.PI;',
  6795. 'this.d = 0.0;'
  6796. ]),
  6797. LinesToStr([
  6798. '$mod.d = Global.PI;',
  6799. '$mod.d = $mod.Tau + Global.PI;'
  6800. ]));
  6801. end;
  6802. procedure TTestModule.TestDouble;
  6803. begin
  6804. StartProgram(false);
  6805. Add([
  6806. 'type',
  6807. ' TDateTime = double;',
  6808. 'const',
  6809. ' a = TDateTime(2.7);',
  6810. ' b = a + TDateTime(1.7);',
  6811. ' c = 0.9 + 0.1;',
  6812. ' f0_1 = 0.1;',
  6813. ' f0_3 = 0.3;',
  6814. ' fn0_1 = -0.1;',
  6815. ' fn0_3 = -0.3;',
  6816. ' fn0_003 = -0.003;',
  6817. ' fn0_123456789 = -0.123456789;',
  6818. ' fn300_0 = -300.0;',
  6819. ' fn123456_0 = -123456.0;',
  6820. ' fn1234567_8 = -1234567.8;',
  6821. ' fn12345678_9 = -12345678.9;',
  6822. ' f1_0En12 = 1E-12;',
  6823. ' fn1_0En12 = -1E-12;',
  6824. ' maxdouble = 1.7e+308;',
  6825. ' mindouble = -1.7e+308;',
  6826. ' MinSafeIntDouble = -$1fffffffffffff;',
  6827. ' MinSafeIntDouble2 = -$20000000000000-1;',
  6828. ' MaxSafeIntDouble = $1fffffffffffff;',
  6829. ' DZeroResolution = 1E-12;',
  6830. ' Minus1 = -1E-12;',
  6831. ' EPS = 1E-9;',
  6832. ' DELTA = 0.001;',
  6833. ' Big = 129.789E+100;',
  6834. ' Test0_15 = 0.15;',
  6835. ' Test999 = 2.9999999999999;',
  6836. ' Test111999 = 211199999999999000.0;',
  6837. ' TestMinus111999 = -211199999999999000.0;',
  6838. 'var',
  6839. ' d: double = b;',
  6840. 'begin',
  6841. ' d:=1.0;',
  6842. ' d:=1.0/3.0;',
  6843. ' d:=1/3;',
  6844. ' d:=5.0E-324;',
  6845. ' d:=1.7E308;',
  6846. ' d:=001.00E00;',
  6847. ' d:=002.00E001;',
  6848. ' d:=003.000E000;',
  6849. ' d:=-004.00E-00;',
  6850. ' d:=-005.00E-001;',
  6851. ' d:=10**3;',
  6852. ' d:=10 mod 3;',
  6853. ' d:=10 div 3;',
  6854. ' d:=c;',
  6855. ' d:=f0_1;',
  6856. ' d:=f0_3;',
  6857. ' d:=fn0_1;',
  6858. ' d:=fn0_3;',
  6859. ' d:=fn0_003;',
  6860. ' d:=fn0_123456789;',
  6861. ' d:=fn300_0;',
  6862. ' d:=fn123456_0;',
  6863. ' d:=fn1234567_8;',
  6864. ' d:=fn12345678_9;',
  6865. ' d:=f1_0En12;',
  6866. ' d:=fn1_0En12;',
  6867. ' d:=maxdouble;',
  6868. ' d:=mindouble;',
  6869. ' d:=MinSafeIntDouble;',
  6870. ' d:=double(MinSafeIntDouble);',
  6871. ' d:=MinSafeIntDouble2;',
  6872. ' d:=double(MinSafeIntDouble2);',
  6873. ' d:=MaxSafeIntDouble;',
  6874. ' d:=default(double);',
  6875. '']);
  6876. ConvertProgram;
  6877. CheckSource('TestDouble',
  6878. LinesToStr([
  6879. 'this.a = 2.7;',
  6880. 'this.b = 2.7 + 1.7;',
  6881. 'this.c = 0.9 + 0.1;',
  6882. 'this.f0_1 = 0.1;',
  6883. 'this.f0_3 = 0.3;',
  6884. 'this.fn0_1 = -0.1;',
  6885. 'this.fn0_3 = -0.3;',
  6886. 'this.fn0_003 = -0.003;',
  6887. 'this.fn0_123456789 = -0.123456789;',
  6888. 'this.fn300_0 = -300.0;',
  6889. 'this.fn123456_0 = -123456.0;',
  6890. 'this.fn1234567_8 = -1234567.8;',
  6891. 'this.fn12345678_9 = -12345678.9;',
  6892. 'this.f1_0En12 = 1E-12;',
  6893. 'this.fn1_0En12 = -1E-12;',
  6894. 'this.maxdouble = 1.7e+308;',
  6895. 'this.mindouble = -1.7e+308;',
  6896. 'this.MinSafeIntDouble = -0x1fffffffffffff;',
  6897. 'this.MinSafeIntDouble2 = -0x20000000000000 - 1;',
  6898. 'this.MaxSafeIntDouble = 0x1fffffffffffff;',
  6899. 'this.DZeroResolution = 1E-12;',
  6900. 'this.Minus1 = -1E-12;',
  6901. 'this.EPS = 1E-9;',
  6902. 'this.DELTA = 0.001;',
  6903. 'this.Big = 129.789E+100;',
  6904. 'this.Test0_15 = 0.15;',
  6905. 'this.Test999 = 2.9999999999999;',
  6906. 'this.Test111999 = 211199999999999000.0;',
  6907. 'this.TestMinus111999 = -211199999999999000.0;',
  6908. 'this.d = 4.4;'
  6909. ]),
  6910. LinesToStr([
  6911. '$mod.d = 1.0;',
  6912. '$mod.d = 1.0 / 3.0;',
  6913. '$mod.d = 1 / 3;',
  6914. '$mod.d = 5.0E-324;',
  6915. '$mod.d = 1.7E308;',
  6916. '$mod.d = 1.00E0;',
  6917. '$mod.d = 2.00E1;',
  6918. '$mod.d = 3.000E0;',
  6919. '$mod.d = -4.00E-0;',
  6920. '$mod.d = -5.00E-1;',
  6921. '$mod.d = Math.pow(10, 3);',
  6922. '$mod.d = 10 % 3;',
  6923. '$mod.d = rtl.trunc(10 / 3);',
  6924. '$mod.d = 1;',
  6925. '$mod.d = 0.1;',
  6926. '$mod.d = 0.3;',
  6927. '$mod.d = -0.1;',
  6928. '$mod.d = -0.3;',
  6929. '$mod.d = -0.003;',
  6930. '$mod.d = -0.123456789;',
  6931. '$mod.d = -300;',
  6932. '$mod.d = -123456;',
  6933. '$mod.d = -1234567.8;',
  6934. '$mod.d = -1.23456789E7;',
  6935. '$mod.d = 1E-12;',
  6936. '$mod.d = -1E-12;',
  6937. '$mod.d = 1.7E308;',
  6938. '$mod.d = -1.7E308;',
  6939. '$mod.d = -9007199254740991;',
  6940. '$mod.d = -9007199254740991;',
  6941. '$mod.d = -9.007199254740992E15;',
  6942. '$mod.d = -9.007199254740992E15;',
  6943. '$mod.d = 9007199254740991;',
  6944. '$mod.d = 0.0;',
  6945. '']));
  6946. end;
  6947. procedure TTestModule.TestInteger;
  6948. begin
  6949. StartProgram(false);
  6950. Add([
  6951. 'const',
  6952. ' MinInt = low(NativeInt);',
  6953. ' MaxInt = high(NativeInt);',
  6954. 'type',
  6955. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  6956. 'const',
  6957. ' a = low(TMyInt)+High(TMyInt);',
  6958. 'var',
  6959. ' i: TMyInt;',
  6960. 'begin',
  6961. ' i:=-MinInt;',
  6962. ' i:=default(TMyInt);',
  6963. ' i:=low(i)+high(i);',
  6964. '']);
  6965. ConvertProgram;
  6966. CheckSource('TestIntegerRange',
  6967. LinesToStr([
  6968. 'this.MinInt = -9007199254740991;',
  6969. 'this.MaxInt = 9007199254740991;',
  6970. 'this.a = -9007199254740991 + 9007199254740991;',
  6971. 'this.i = 0;',
  6972. '']),
  6973. LinesToStr([
  6974. '$mod.i = - -9007199254740991;',
  6975. '$mod.i = -9007199254740991;',
  6976. '$mod.i = -9007199254740991 + 9007199254740991;',
  6977. '']));
  6978. end;
  6979. procedure TTestModule.TestIntegerRange;
  6980. begin
  6981. StartProgram(false);
  6982. Add([
  6983. 'const',
  6984. ' MinInt = -1;',
  6985. ' MaxInt = +1;',
  6986. 'type',
  6987. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  6988. ' TInt2 = 1..3;',
  6989. 'const',
  6990. ' a = low(TMyInt)+High(TMyInt);',
  6991. ' b = low(TInt2)+High(TInt2);',
  6992. ' s1 = [1];',
  6993. ' s2 = [1,2];',
  6994. ' s3 = [1..3];',
  6995. ' s4 = [low(shortint)..high(shortint)];',
  6996. ' s5 = [succ(low(shortint))..pred(high(shortint))];',
  6997. ' s6 = 1 in s2;',
  6998. 'var',
  6999. ' i: TMyInt;',
  7000. ' i2: TInt2;',
  7001. 'begin',
  7002. ' i:=i2;',
  7003. ' i:=default(TMyInt);',
  7004. ' if i=i2 then ;',
  7005. ' i:=ord(i2);',
  7006. '']);
  7007. ConvertProgram;
  7008. CheckSource('TestIntegerRange',
  7009. LinesToStr([
  7010. 'this.MinInt = -1;',
  7011. 'this.MaxInt = +1;',
  7012. 'this.a = -1 + 1;',
  7013. 'this.b = 1 + 3;',
  7014. 'this.s1 = rtl.createSet(1);',
  7015. 'this.s2 = rtl.createSet(1, 2);',
  7016. 'this.s3 = rtl.createSet(null, 1, 3);',
  7017. 'this.s4 = rtl.createSet(null, -128, 127);',
  7018. 'this.s5 = rtl.createSet(null, -128 + 1, 127 - 1);',
  7019. 'this.s6 = 1 in this.s2;',
  7020. 'this.i = 0;',
  7021. 'this.i2 = 0;',
  7022. '']),
  7023. LinesToStr([
  7024. '$mod.i = $mod.i2;',
  7025. '$mod.i = -1;',
  7026. 'if ($mod.i === $mod.i2) ;',
  7027. '$mod.i = $mod.i2;',
  7028. '']));
  7029. end;
  7030. procedure TTestModule.TestIntegerTypecasts;
  7031. begin
  7032. StartProgram(false);
  7033. Add([
  7034. 'var',
  7035. ' i: nativeint;',
  7036. ' b: byte;',
  7037. ' sh: shortint;',
  7038. ' w: word;',
  7039. ' sm: smallint;',
  7040. ' lw: longword;',
  7041. ' li: longint;',
  7042. 'begin',
  7043. ' b:=byte(i);',
  7044. ' sh:=shortint(i);',
  7045. ' w:=word(i);',
  7046. ' sm:=smallint(i);',
  7047. ' lw:=longword(i);',
  7048. ' li:=longint(i);',
  7049. '']);
  7050. ConvertProgram;
  7051. CheckSource('TestIntegerTypecasts',
  7052. LinesToStr([
  7053. 'this.i = 0;',
  7054. 'this.b = 0;',
  7055. 'this.sh = 0;',
  7056. 'this.w = 0;',
  7057. 'this.sm = 0;',
  7058. 'this.lw = 0;',
  7059. 'this.li = 0;',
  7060. '']),
  7061. LinesToStr([
  7062. '$mod.b = $mod.i & 255;',
  7063. '$mod.sh = (($mod.i & 255) << 24) >> 24;',
  7064. '$mod.w = $mod.i & 65535;',
  7065. '$mod.sm = (($mod.i & 65535) << 16) >> 16;',
  7066. '$mod.lw = $mod.i >>> 0;',
  7067. '$mod.li = $mod.i & 0xFFFFFFFF;',
  7068. '']));
  7069. end;
  7070. procedure TTestModule.TestInteger_BitwiseShrNativeInt;
  7071. begin
  7072. StartProgram(false);
  7073. Add([
  7074. 'var',
  7075. ' i,j: nativeint;',
  7076. 'begin',
  7077. ' i:=i shr 0;',
  7078. ' i:=i shr 1;',
  7079. ' i:=i shr 3;',
  7080. ' i:=i shr 54;',
  7081. ' i:=j shr i;',
  7082. '']);
  7083. ConvertProgram;
  7084. CheckResolverUnexpectedHints;
  7085. CheckSource('TestInteger_BitwiseShrNativeInt',
  7086. LinesToStr([
  7087. 'this.i = 0;',
  7088. 'this.j = 0;',
  7089. '']),
  7090. LinesToStr([
  7091. '$mod.i = $mod.i;',
  7092. '$mod.i = Math.floor($mod.i / 2);',
  7093. '$mod.i = Math.floor($mod.i / 8);',
  7094. '$mod.i = 0;',
  7095. '$mod.i = rtl.shr($mod.j, $mod.i);',
  7096. '']));
  7097. end;
  7098. procedure TTestModule.TestInteger_BitwiseShlNativeInt;
  7099. begin
  7100. StartProgram(false);
  7101. Add([
  7102. 'var',
  7103. ' i: nativeint;',
  7104. 'begin',
  7105. ' i:=i shl 0;',
  7106. ' i:=i shl 54;',
  7107. ' i:=123456789012 shl 1;',
  7108. ' i:=i shl 1;',
  7109. '']);
  7110. ConvertProgram;
  7111. CheckResolverUnexpectedHints;
  7112. CheckSource('TestInteger_BitwiseShrNativeInt',
  7113. LinesToStr([
  7114. 'this.i = 0;',
  7115. '']),
  7116. LinesToStr([
  7117. '$mod.i = $mod.i;',
  7118. '$mod.i = 0;',
  7119. '$mod.i = 246913578024;',
  7120. '$mod.i = rtl.shl($mod.i, 1);',
  7121. '']));
  7122. end;
  7123. procedure TTestModule.TestInteger_SystemFunc;
  7124. begin
  7125. StartProgram(true);
  7126. Add([
  7127. 'var',
  7128. ' i: byte;',
  7129. ' s: string;',
  7130. 'begin',
  7131. ' system.inc(i);',
  7132. ' system.str(i,s);',
  7133. ' s:=system.str(i);',
  7134. ' i:=system.low(i);',
  7135. ' i:=system.high(i);',
  7136. ' i:=system.pred(i);',
  7137. ' i:=system.succ(i);',
  7138. ' i:=system.ord(i);',
  7139. '']);
  7140. ConvertProgram;
  7141. CheckResolverUnexpectedHints;
  7142. CheckSource('TestInteger_SystemFunc',
  7143. LinesToStr([
  7144. 'this.i = 0;',
  7145. 'this.s = "";',
  7146. '']),
  7147. LinesToStr([
  7148. '$mod.i += 1;',
  7149. '$mod.s = "" + $mod.i;',
  7150. '$mod.s = "" + $mod.i;',
  7151. '$mod.i = 0;',
  7152. '$mod.i = 255;',
  7153. '$mod.i = $mod.i - 1;',
  7154. '$mod.i = $mod.i + 1;',
  7155. '$mod.i = $mod.i;',
  7156. '']));
  7157. end;
  7158. procedure TTestModule.TestCurrency;
  7159. begin
  7160. StartProgram(false);
  7161. Add([
  7162. 'type',
  7163. ' TCoin = currency;',
  7164. 'const',
  7165. ' a = TCoin(2.7);',
  7166. ' b = a + TCoin(1.7);',
  7167. ' MinSafeIntCurrency: TCoin = -92233720368.5477;',
  7168. ' MaxSafeIntCurrency: TCoin = 92233720368.5477;',
  7169. 'var',
  7170. ' c: TCoin = b;',
  7171. ' i: nativeint;',
  7172. ' d: double;',
  7173. ' j: jsvalue;',
  7174. 'function DoIt(c: currency): currency; begin end;',
  7175. 'function GetIt(d: double): double; begin end;',
  7176. 'procedure Write(v: jsvalue); begin end;',
  7177. 'begin',
  7178. ' c:=1.0;',
  7179. ' c:=0.1;',
  7180. ' c:=1.0/3.0;',
  7181. ' c:=1/3;',
  7182. ' c:=a;',
  7183. ' d:=c;',
  7184. ' c:=d;',
  7185. ' c:=currency(c);',
  7186. ' c:=currency(d);',
  7187. ' d:=double(c);',
  7188. ' c:=i;',
  7189. ' c:=currency(i);',
  7190. //' i:=c;', not allowed
  7191. ' i:=nativeint(c);',
  7192. ' c:=c+a;',
  7193. ' c:=-c-a;',
  7194. ' c:=d+c;',
  7195. ' c:=c+d;',
  7196. ' c:=d-c;',
  7197. ' c:=c-d;',
  7198. ' c:=c*a;',
  7199. ' c:=a*c;',
  7200. ' c:=d*c;',
  7201. ' c:=c*d;',
  7202. ' c:=c/a;',
  7203. ' c:=a/c;',
  7204. ' c:=d/c;',
  7205. ' c:=c/d;',
  7206. ' c:=c**a;',
  7207. ' c:=a**c;',
  7208. ' c:=d**c;',
  7209. ' c:=c**d;',
  7210. ' if c=c then ;',
  7211. ' if c=a then ;',
  7212. ' if a=c then ;',
  7213. ' if d=c then ;',
  7214. ' if c=d then ;',
  7215. ' c:=DoIt(c);',
  7216. ' c:=DoIt(i);',
  7217. ' c:=DoIt(d);',
  7218. ' c:=GetIt(c);',
  7219. ' j:=c;',
  7220. ' Write(c);',
  7221. ' c:=default(currency);',
  7222. ' j:=str(c);',
  7223. ' j:=str(c:0:3);',
  7224. '']);
  7225. ConvertProgram;
  7226. CheckSource('TestCurrency',
  7227. LinesToStr([
  7228. 'this.a = 27000;',
  7229. 'this.b = this.a + 17000;',
  7230. 'this.MinSafeIntCurrency = -92233720368.5477;',
  7231. 'this.MaxSafeIntCurrency = 92233720368.5477;',
  7232. 'this.c = this.b;',
  7233. 'this.i = 0;',
  7234. 'this.d = 0.0;',
  7235. 'this.j = undefined;',
  7236. 'this.DoIt = function (c) {',
  7237. ' var Result = 0;',
  7238. ' return Result;',
  7239. '};',
  7240. 'this.GetIt = function (d) {',
  7241. ' var Result = 0.0;',
  7242. ' return Result;',
  7243. '};',
  7244. 'this.Write = function (v) {',
  7245. '};',
  7246. '']),
  7247. LinesToStr([
  7248. '$mod.c = 10000;',
  7249. '$mod.c = 1000;',
  7250. '$mod.c = rtl.trunc((1.0 / 3.0) * 10000);',
  7251. '$mod.c = rtl.trunc((1 / 3) * 10000);',
  7252. '$mod.c = $mod.a;',
  7253. '$mod.d = $mod.c / 10000;',
  7254. '$mod.c = rtl.trunc($mod.d * 10000);',
  7255. '$mod.c = $mod.c;',
  7256. '$mod.c = $mod.d * 10000;',
  7257. '$mod.d = $mod.c / 10000;',
  7258. '$mod.c = $mod.i * 10000;',
  7259. '$mod.c = $mod.i * 10000;',
  7260. '$mod.i = rtl.trunc($mod.c / 10000);',
  7261. '$mod.c = $mod.c + $mod.a;',
  7262. '$mod.c = -$mod.c - $mod.a;',
  7263. '$mod.c = ($mod.d * 10000) + $mod.c;',
  7264. '$mod.c = $mod.c + ($mod.d * 10000);',
  7265. '$mod.c = ($mod.d * 10000) - $mod.c;',
  7266. '$mod.c = $mod.c - ($mod.d * 10000);',
  7267. '$mod.c = ($mod.c * $mod.a) / 10000;',
  7268. '$mod.c = ($mod.a * $mod.c) / 10000;',
  7269. '$mod.c = $mod.d * $mod.c;',
  7270. '$mod.c = $mod.c * $mod.d;',
  7271. '$mod.c = rtl.trunc(($mod.c / $mod.a) * 10000);',
  7272. '$mod.c = rtl.trunc(($mod.a / $mod.c) * 10000);',
  7273. '$mod.c = rtl.trunc($mod.d / $mod.c);',
  7274. '$mod.c = rtl.trunc($mod.c / $mod.d);',
  7275. '$mod.c = rtl.trunc(Math.pow($mod.c / 10000, $mod.a / 10000) * 10000);',
  7276. '$mod.c = rtl.trunc(Math.pow($mod.a / 10000, $mod.c / 10000) * 10000);',
  7277. '$mod.c = rtl.trunc(Math.pow($mod.d, $mod.c / 10000) * 10000);',
  7278. '$mod.c = rtl.trunc(Math.pow($mod.c / 10000, $mod.d) * 10000);',
  7279. 'if ($mod.c === $mod.c) ;',
  7280. 'if ($mod.c === $mod.a) ;',
  7281. 'if ($mod.a === $mod.c) ;',
  7282. 'if (($mod.d * 10000) === $mod.c) ;',
  7283. 'if ($mod.c === ($mod.d * 10000)) ;',
  7284. '$mod.c = $mod.DoIt($mod.c);',
  7285. '$mod.c = $mod.DoIt($mod.i * 10000);',
  7286. '$mod.c = $mod.DoIt($mod.d * 10000);',
  7287. '$mod.c = rtl.trunc($mod.GetIt($mod.c / 10000) * 10000);',
  7288. '$mod.j = $mod.c / 10000;',
  7289. '$mod.Write($mod.c / 10000);',
  7290. '$mod.c = 0;',
  7291. '$mod.j = rtl.floatToStr($mod.c / 10000);',
  7292. '$mod.j = rtl.floatToStr($mod.c / 10000, 0, 3);',
  7293. '']));
  7294. end;
  7295. procedure TTestModule.TestForBoolDo;
  7296. begin
  7297. StartProgram(false);
  7298. Add([
  7299. 'var b: boolean;',
  7300. 'begin',
  7301. ' for b:=false to true do ;',
  7302. ' for b:=b downto false do ;',
  7303. ' for b in boolean do ;',
  7304. '']);
  7305. ConvertProgram;
  7306. CheckSource('TestForBoolDo',
  7307. LinesToStr([ // statements
  7308. 'this.b = false;']),
  7309. LinesToStr([ // this.$main
  7310. 'for (var $l = 0; $l <= 1; $l++) $mod.b = $l !== 0;',
  7311. 'for (var $l1 = +$mod.b; $l1 >= 0; $l1--) $mod.b = $l1 !== 0;',
  7312. 'for (var $l2 = 0; $l2 <= 1; $l2++) $mod.b = $l2 !== 0;',
  7313. '']));
  7314. end;
  7315. procedure TTestModule.TestForIntDo;
  7316. begin
  7317. StartProgram(false);
  7318. Add([
  7319. 'var i: longint;',
  7320. 'begin',
  7321. ' for i:=3 to 5 do ;',
  7322. ' for i:=i downto 2 do ;',
  7323. ' for i in byte do ;',
  7324. '']);
  7325. ConvertProgram;
  7326. CheckSource('TestForIntDo',
  7327. LinesToStr([ // statements
  7328. 'this.i = 0;']),
  7329. LinesToStr([ // this.$main
  7330. 'for ($mod.i = 3; $mod.i <= 5; $mod.i++) ;',
  7331. 'for (var $l = $mod.i; $l >= 2; $l--) $mod.i = $l;',
  7332. 'for (var $l1 = 0; $l1 <= 255; $l1++) $mod.i = $l1;',
  7333. '']));
  7334. end;
  7335. procedure TTestModule.TestForIntInDo;
  7336. begin
  7337. StartProgram(false);
  7338. Add([
  7339. 'type',
  7340. ' TSetOfInt = set of byte;',
  7341. ' TIntRg = 3..7;',
  7342. ' TSetOfIntRg = set of TIntRg;',
  7343. 'var',
  7344. ' i,i2: longint;',
  7345. ' a1: array of byte;',
  7346. ' a2: array[1..3] of byte;',
  7347. ' soi: TSetOfInt;',
  7348. ' soir: TSetOfIntRg;',
  7349. ' ir: TIntRg;',
  7350. 'begin',
  7351. ' for i in byte do ;',
  7352. ' for i in a1 do ;',
  7353. ' for i in a2 do ;',
  7354. ' for i in [11..13] do ;',
  7355. ' for i in TSetOfInt do ;',
  7356. ' for i in TIntRg do ;',
  7357. ' for i in soi do i2:=i;',
  7358. ' for i in TSetOfIntRg do ;',
  7359. ' for i in soir do ;',
  7360. ' for ir in TIntRg do ;',
  7361. ' for ir in TSetOfIntRg do ;',
  7362. ' for ir in soir do ;',
  7363. '']);
  7364. ConvertProgram;
  7365. CheckSource('TestForIntInDo',
  7366. LinesToStr([ // statements
  7367. 'this.i = 0;',
  7368. 'this.i2 = 0;',
  7369. 'this.a1 = [];',
  7370. 'this.a2 = rtl.arraySetLength(null, 0, 3);',
  7371. 'this.soi = {};',
  7372. 'this.soir = {};',
  7373. 'this.ir = 0;',
  7374. '']),
  7375. LinesToStr([ // this.$main
  7376. 'for (var $l = 0; $l <= 255; $l++) $mod.i = $l;',
  7377. 'for (var $in = $mod.a1, $l1 = 0, $end = rtl.length($in) - 1; $l1 <= $end; $l1++) $mod.i = $in[$l1];',
  7378. 'for (var $in1 = $mod.a2, $l2 = 0, $end1 = rtl.length($in1) - 1; $l2 <= $end1; $l2++) $mod.i = $in1[$l2];',
  7379. 'for (var $l3 = 11; $l3 <= 13; $l3++) $mod.i = $l3;',
  7380. 'for (var $l4 = 0; $l4 <= 255; $l4++) $mod.i = $l4;',
  7381. 'for (var $l5 = 3; $l5 <= 7; $l5++) $mod.i = $l5;',
  7382. 'for (var $l6 in $mod.soi) {',
  7383. ' $mod.i = +$l6;',
  7384. ' $mod.i2 = $mod.i;',
  7385. '};',
  7386. 'for (var $l7 = 3; $l7 <= 7; $l7++) $mod.i = $l7;',
  7387. 'for (var $l8 in $mod.soir) $mod.i = +$l8;',
  7388. 'for (var $l9 = 3; $l9 <= 7; $l9++) $mod.ir = $l9;',
  7389. 'for (var $l10 = 3; $l10 <= 7; $l10++) $mod.ir = $l10;',
  7390. 'for (var $l11 in $mod.soir) $mod.ir = +$l11;',
  7391. '']));
  7392. end;
  7393. procedure TTestModule.TestCharConst;
  7394. begin
  7395. StartProgram(false);
  7396. Add([
  7397. 'const',
  7398. ' a = #$00F3;',
  7399. ' c: char = ''1'';',
  7400. ' wc: widechar = ''ä'';',
  7401. 'begin',
  7402. ' c:=#0;',
  7403. ' c:=#1;',
  7404. ' c:=#9;',
  7405. ' c:=#10;',
  7406. ' c:=#13;',
  7407. ' c:=#31;',
  7408. ' c:=#32;',
  7409. ' c:=#$A;',
  7410. ' c:=#$0A;',
  7411. ' c:=#$b;',
  7412. ' c:=#$0b;',
  7413. ' c:=^A;',
  7414. ' c:=''"'';',
  7415. ' c:=default(char);',
  7416. ' c:=#$00E4;', // ä
  7417. ' c:=''ä'';',
  7418. ' c:=#$E4;', // ä
  7419. ' c:=#$D800;', // invalid UTF-16
  7420. ' c:=#$DFFF;', // invalid UTF-16
  7421. ' c:=#$FFFF;', // last UCS-2
  7422. ' c:=high(c);', // last UCS-2
  7423. ' c:=#269;',
  7424. '']);
  7425. ConvertProgram;
  7426. CheckSource('TestCharConst',
  7427. LinesToStr([
  7428. 'this.a="ó";',
  7429. 'this.c="1";',
  7430. 'this.wc="ä";'
  7431. ]),
  7432. LinesToStr([
  7433. '$mod.c="\x00";',
  7434. '$mod.c="\x01";',
  7435. '$mod.c="\t";',
  7436. '$mod.c="\n";',
  7437. '$mod.c="\r";',
  7438. '$mod.c="\x1F";',
  7439. '$mod.c=" ";',
  7440. '$mod.c="\n";',
  7441. '$mod.c="\n";',
  7442. '$mod.c="\x0B";',
  7443. '$mod.c="\x0B";',
  7444. '$mod.c="\x01";',
  7445. '$mod.c=''"'';',
  7446. '$mod.c="\x00";',
  7447. '$mod.c = "ä";',
  7448. '$mod.c = "ä";',
  7449. '$mod.c = "ä";',
  7450. '$mod.c="\uD800";',
  7451. '$mod.c="\uDFFF";',
  7452. '$mod.c="\uFFFF";',
  7453. '$mod.c="\uFFFF";',
  7454. '$mod.c = "č";',
  7455. '']));
  7456. end;
  7457. procedure TTestModule.TestChar_Compare;
  7458. begin
  7459. StartProgram(false);
  7460. Add('var');
  7461. Add(' c: char;');
  7462. Add(' b: boolean;');
  7463. Add('begin');
  7464. Add(' b:=c=''1'';');
  7465. Add(' b:=''2''=c;');
  7466. Add(' b:=''3''=''4'';');
  7467. Add(' b:=c<>''5'';');
  7468. Add(' b:=''6''<>c;');
  7469. Add(' b:=c>''7'';');
  7470. Add(' b:=''8''>c;');
  7471. Add(' b:=c>=''9'';');
  7472. Add(' b:=''A''>=c;');
  7473. Add(' b:=c<''B'';');
  7474. Add(' b:=''C''<c;');
  7475. Add(' b:=c<=''D'';');
  7476. Add(' b:=''E''<=c;');
  7477. ConvertProgram;
  7478. CheckSource('TestChar_Compare',
  7479. LinesToStr([
  7480. 'this.c="";',
  7481. 'this.b = false;'
  7482. ]),
  7483. LinesToStr([
  7484. '$mod.b = $mod.c === "1";',
  7485. '$mod.b = "2" === $mod.c;',
  7486. '$mod.b = "3" === "4";',
  7487. '$mod.b = $mod.c !== "5";',
  7488. '$mod.b = "6" !== $mod.c;',
  7489. '$mod.b = $mod.c > "7";',
  7490. '$mod.b = "8" > $mod.c;',
  7491. '$mod.b = $mod.c >= "9";',
  7492. '$mod.b = "A" >= $mod.c;',
  7493. '$mod.b = $mod.c < "B";',
  7494. '$mod.b = "C" < $mod.c;',
  7495. '$mod.b = $mod.c <= "D";',
  7496. '$mod.b = "E" <= $mod.c;',
  7497. '']));
  7498. end;
  7499. procedure TTestModule.TestChar_BuiltInProcs;
  7500. begin
  7501. StartProgram(false);
  7502. Add([
  7503. 'var',
  7504. ' c: char;',
  7505. ' i: longint;',
  7506. ' s: string;',
  7507. 'begin',
  7508. ' i:=ord(c);',
  7509. ' i:=ord(s[i]);',
  7510. ' c:=chr(i);',
  7511. ' c:=pred(c);',
  7512. ' c:=succ(c);',
  7513. ' c:=low(c);',
  7514. ' c:=high(c);',
  7515. ' i:=byte(c);',
  7516. ' i:=word(c);',
  7517. ' i:=longint(c);',
  7518. '']);
  7519. ConvertProgram;
  7520. CheckSource('TestChar_BuiltInProcs',
  7521. LinesToStr([
  7522. 'this.c = "";',
  7523. 'this.i = 0;',
  7524. 'this.s = "";'
  7525. ]),
  7526. LinesToStr([
  7527. '$mod.i = $mod.c.charCodeAt();',
  7528. '$mod.i = $mod.s.charCodeAt($mod.i-1);',
  7529. '$mod.c = String.fromCharCode($mod.i);',
  7530. '$mod.c = String.fromCharCode($mod.c.charCodeAt() - 1);',
  7531. '$mod.c = String.fromCharCode($mod.c.charCodeAt() + 1);',
  7532. '$mod.c = "\x00";',
  7533. '$mod.c = "\uFFFF";',
  7534. '$mod.i = $mod.c.charCodeAt() & 255;',
  7535. '$mod.i = $mod.c.charCodeAt();',
  7536. '$mod.i = $mod.c.charCodeAt() & 0xFFFFFFFF;',
  7537. '']));
  7538. end;
  7539. procedure TTestModule.TestStringConst;
  7540. begin
  7541. StartProgram(false);
  7542. Add([
  7543. '{$H+}',
  7544. 'const',
  7545. ' a = #$00F3#$017C;', // first <256, then >=256
  7546. ' b = string(''a'');',
  7547. ' c = string(''ä'');',
  7548. ' d = UnicodeString(''b'');',
  7549. ' e = UnicodeString(''ö'');',
  7550. 'var',
  7551. ' s: string = ''abc'';',
  7552. 'begin',
  7553. ' s:='''';',
  7554. ' s:=#13#10;',
  7555. ' s:=#9''foo'';',
  7556. ' s:=#$A9;',
  7557. ' s:=''foo''#13''bar'';',
  7558. ' s:=''"'';',
  7559. ' s:=''"''''"'';',
  7560. ' s:=#$20AC;', // euro
  7561. ' s:=#$10437;', // outside BMP
  7562. ' s:=''abc''#$20AC;', // ascii,#
  7563. ' s:=''ä''#$20AC;', // non ascii,#
  7564. ' s:=#$20AC''abc'';', // #, ascii
  7565. ' s:=#$20AC''ä'';', // #, non ascii
  7566. ' s:=default(string);',
  7567. ' s:=concat(s);',
  7568. ' s:=concat(s,''a'',s);',
  7569. ' s:=#250#269;',
  7570. //' s:=#$2F804;',
  7571. // ToDo: \uD87E\uDC04 -> \u{2F804}
  7572. '']);
  7573. ConvertProgram;
  7574. CheckSource('TestStringConst',
  7575. LinesToStr([
  7576. 'this.a = "óż";',
  7577. 'this.b = "a";',
  7578. 'this.c = "ä";',
  7579. 'this.d = "b";',
  7580. 'this.e = "ö";',
  7581. 'this.s="abc";',
  7582. '']),
  7583. LinesToStr([
  7584. '$mod.s="";',
  7585. '$mod.s="\r\n";',
  7586. '$mod.s="\tfoo";',
  7587. '$mod.s="©";',
  7588. '$mod.s="foo\rbar";',
  7589. '$mod.s=''"'';',
  7590. '$mod.s=''"\''"'';',
  7591. '$mod.s="€";',
  7592. '$mod.s="'#$F0#$90#$90#$B7'";',
  7593. '$mod.s = "abc€";',
  7594. '$mod.s = "ä€";',
  7595. '$mod.s = "€abc";',
  7596. '$mod.s = "ۊ";',
  7597. '$mod.s="";',
  7598. '$mod.s = $mod.s;',
  7599. '$mod.s = $mod.s.concat("a", $mod.s);',
  7600. '$mod.s = "úč";',
  7601. '']));
  7602. end;
  7603. procedure TTestModule.TestStringConst_InvalidUTF16;
  7604. begin
  7605. StartProgram(false);
  7606. Add([
  7607. 'const',
  7608. ' a: char = #$D87E;',
  7609. ' b: string = #$D87E;',
  7610. ' c: string = #$D87E#43;',
  7611. 'begin',
  7612. ' c:=''abc''#$D87E;',
  7613. ' c:=#0#1#2;',
  7614. ' c:=#127;',
  7615. ' c:=#128;',
  7616. ' c:=#255;',
  7617. ' c:=#256;',
  7618. '']);
  7619. ConvertProgram;
  7620. CheckSource('TestStringConst',
  7621. LinesToStr([
  7622. 'this.a = "\uD87E";',
  7623. 'this.b = "\uD87E";',
  7624. 'this.c = "\uD87E+";',
  7625. '']),
  7626. LinesToStr([
  7627. '$mod.c = "abc\uD87E";',
  7628. '$mod.c = "\x00\x01\x02";',
  7629. '$mod.c = "'#127'";',
  7630. '$mod.c = "'#$c2#$80'";',
  7631. '$mod.c = "'#$c3#$BF'";',
  7632. '$mod.c = "'#$c4#$80'";',
  7633. '']));
  7634. end;
  7635. procedure TTestModule.TestStringConstSurrogate;
  7636. begin
  7637. StartProgram(false);
  7638. Add([
  7639. 'var',
  7640. ' s: string;',
  7641. 'begin',
  7642. ' s:=''😊'';', // 1F60A
  7643. '']);
  7644. ConvertProgram;
  7645. CheckSource('TestStringConstSurrogate',
  7646. LinesToStr([
  7647. 'this.s="";'
  7648. ]),
  7649. LinesToStr([
  7650. '$mod.s="😊";'
  7651. ]));
  7652. end;
  7653. procedure TTestModule.TestString_Length;
  7654. begin
  7655. StartProgram(false);
  7656. Add('const c = ''foo'';');
  7657. Add('var');
  7658. Add(' s: string;');
  7659. Add(' i: longint;');
  7660. Add('begin');
  7661. Add(' i:=length(s);');
  7662. Add(' i:=length(s+s);');
  7663. Add(' i:=length(''abc'');');
  7664. Add(' i:=length(c);');
  7665. ConvertProgram;
  7666. CheckSource('TestString_Length',
  7667. LinesToStr([
  7668. 'this.c = "foo";',
  7669. 'this.s = "";',
  7670. 'this.i = 0;',
  7671. '']),
  7672. LinesToStr([
  7673. '$mod.i = $mod.s.length;',
  7674. '$mod.i = ($mod.s+$mod.s).length;',
  7675. '$mod.i = "abc".length;',
  7676. '$mod.i = $mod.c.length;',
  7677. '']));
  7678. end;
  7679. procedure TTestModule.TestString_Compare;
  7680. begin
  7681. StartProgram(false);
  7682. Add('var');
  7683. Add(' s, t: string;');
  7684. Add(' b: boolean;');
  7685. Add('begin');
  7686. Add(' b:=s=t;');
  7687. Add(' b:=s<>t;');
  7688. Add(' b:=s>t;');
  7689. Add(' b:=s>=t;');
  7690. Add(' b:=s<t;');
  7691. Add(' b:=s<=t;');
  7692. ConvertProgram;
  7693. CheckSource('TestString_Compare',
  7694. LinesToStr([ // statements
  7695. 'this.s = "";',
  7696. 'this.t = "";',
  7697. 'this.b =false;'
  7698. ]),
  7699. LinesToStr([ // this.$main
  7700. '$mod.b = $mod.s === $mod.t;',
  7701. '$mod.b = $mod.s !== $mod.t;',
  7702. '$mod.b = $mod.s > $mod.t;',
  7703. '$mod.b = $mod.s >= $mod.t;',
  7704. '$mod.b = $mod.s < $mod.t;',
  7705. '$mod.b = $mod.s <= $mod.t;',
  7706. '']));
  7707. end;
  7708. procedure TTestModule.TestString_SetLength;
  7709. begin
  7710. StartProgram(false);
  7711. Add([
  7712. 'procedure DoIt(var s: string);',
  7713. 'begin',
  7714. ' SetLength(s,2);',
  7715. 'end;',
  7716. 'var s: string;',
  7717. 'begin',
  7718. ' SetLength(s,3);',
  7719. '']);
  7720. ConvertProgram;
  7721. CheckSource('TestString_SetLength',
  7722. LinesToStr([ // statements
  7723. 'this.DoIt = function (s) {',
  7724. ' s.set(rtl.strSetLength(s.get(), 2));',
  7725. '};',
  7726. 'this.s = "";',
  7727. '']),
  7728. LinesToStr([ // this.$main
  7729. '$mod.s = rtl.strSetLength($mod.s, 3);'
  7730. ]));
  7731. end;
  7732. procedure TTestModule.TestString_CharAt;
  7733. begin
  7734. StartProgram(false);
  7735. Add([
  7736. 'var',
  7737. ' s: string;',
  7738. ' c: char;',
  7739. ' b: boolean;',
  7740. 'begin',
  7741. ' b:= s[1] = c;',
  7742. ' b:= c = s[1];',
  7743. ' b:= c <> s[1];',
  7744. ' b:= c > s[1];',
  7745. ' b:= c >= s[1];',
  7746. ' b:= c < s[2];',
  7747. ' b:= c <= s[1];',
  7748. ' s[1] := c;',
  7749. ' s[2+3] := c;']);
  7750. ConvertProgram;
  7751. CheckSource('TestString_CharAt',
  7752. LinesToStr([ // statements
  7753. 'this.s = "";',
  7754. 'this.c = "";',
  7755. 'this.b = false;'
  7756. ]),
  7757. LinesToStr([ // this.$main
  7758. '$mod.b = $mod.s.charAt(0) === $mod.c;',
  7759. '$mod.b = $mod.c === $mod.s.charAt(0);',
  7760. '$mod.b = $mod.c !== $mod.s.charAt(0);',
  7761. '$mod.b = $mod.c > $mod.s.charAt(0);',
  7762. '$mod.b = $mod.c >= $mod.s.charAt(0);',
  7763. '$mod.b = $mod.c < $mod.s.charAt(1);',
  7764. '$mod.b = $mod.c <= $mod.s.charAt(0);',
  7765. '$mod.s = rtl.setCharAt($mod.s, 0, $mod.c);',
  7766. '$mod.s = rtl.setCharAt($mod.s, (2 + 3) - 1, $mod.c);',
  7767. '']));
  7768. end;
  7769. procedure TTestModule.TestStringHMinusFail;
  7770. begin
  7771. StartProgram(false);
  7772. Add([
  7773. '{$H-}',
  7774. 'var s: string;',
  7775. 'begin']);
  7776. ConvertProgram;
  7777. CheckHint(mtWarning,nWarnIllegalCompilerDirectiveX,'Warning: test1.pp(3,6) : Illegal compiler directive "H-"');
  7778. end;
  7779. procedure TTestModule.TestStr;
  7780. begin
  7781. StartProgram(false);
  7782. Add('var');
  7783. Add(' b: boolean;');
  7784. Add(' i: longint;');
  7785. Add(' d: double;');
  7786. Add(' s: string;');
  7787. Add('begin');
  7788. Add(' str(b,s);');
  7789. Add(' str(i,s);');
  7790. Add(' str(d,s);');
  7791. Add(' str(i:3,s);');
  7792. Add(' str(d:3:2,s);');
  7793. Add(' Str(12.456:12:1,s);');
  7794. Add(' Str(12.456:12,s);');
  7795. Add(' s:=str(b);');
  7796. Add(' s:=str(i);');
  7797. Add(' s:=str(d);');
  7798. Add(' s:=str(i,i);');
  7799. Add(' s:=str(i:3);');
  7800. Add(' s:=str(d:3:2);');
  7801. Add(' s:=str(i:4,i);');
  7802. Add(' s:=str(i,i:5);');
  7803. Add(' s:=str(i:4,i:5);');
  7804. Add(' s:=str(s,s);');
  7805. Add(' s:=str(s,''foo'');');
  7806. ConvertProgram;
  7807. CheckSource('TestStr',
  7808. LinesToStr([ // statements
  7809. 'this.b = false;',
  7810. 'this.i = 0;',
  7811. 'this.d = 0.0;',
  7812. 'this.s = "";',
  7813. '']),
  7814. LinesToStr([ // this.$main
  7815. '$mod.s = ""+$mod.b;',
  7816. '$mod.s = ""+$mod.i;',
  7817. '$mod.s = rtl.floatToStr($mod.d);',
  7818. '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
  7819. '$mod.s = rtl.floatToStr($mod.d,3,2);',
  7820. '$mod.s = rtl.floatToStr(12.456,12,1);',
  7821. '$mod.s = rtl.floatToStr(12.456,12);',
  7822. '$mod.s = ""+$mod.b;',
  7823. '$mod.s = ""+$mod.i;',
  7824. '$mod.s = rtl.floatToStr($mod.d);',
  7825. '$mod.s = ""+$mod.i+$mod.i;',
  7826. '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
  7827. '$mod.s = rtl.floatToStr($mod.d,3,2);',
  7828. '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + $mod.i;',
  7829. '$mod.s = "" + $mod.i + rtl.spaceLeft("" + $mod.i, 5);',
  7830. '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + rtl.spaceLeft("" + $mod.i, 5);',
  7831. '$mod.s = $mod.s + $mod.s;',
  7832. '$mod.s = $mod.s + "foo";',
  7833. '']));
  7834. end;
  7835. procedure TTestModule.TestBaseType_AnsiStringFail;
  7836. begin
  7837. StartProgram(false);
  7838. Add('var s: AnsiString');
  7839. SetExpectedPasResolverError('identifier not found "AnsiString"',PasResolveEval.nIdentifierNotFound);
  7840. ConvertProgram;
  7841. end;
  7842. procedure TTestModule.TestBaseType_WideStringFail;
  7843. begin
  7844. StartProgram(false);
  7845. Add('var s: WideString');
  7846. SetExpectedPasResolverError('identifier not found "WideString"',PasResolveEval.nIdentifierNotFound);
  7847. ConvertProgram;
  7848. end;
  7849. procedure TTestModule.TestBaseType_ShortStringFail;
  7850. begin
  7851. StartProgram(false);
  7852. Add('var s: ShortString');
  7853. SetExpectedPasResolverError('identifier not found "ShortString"',PasResolveEval.nIdentifierNotFound);
  7854. ConvertProgram;
  7855. end;
  7856. procedure TTestModule.TestBaseType_RawByteStringFail;
  7857. begin
  7858. StartProgram(false);
  7859. Add('var s: RawByteString');
  7860. SetExpectedPasResolverError('identifier not found "RawByteString"',PasResolveEval.nIdentifierNotFound);
  7861. ConvertProgram;
  7862. end;
  7863. procedure TTestModule.TestTypeShortstring_Fail;
  7864. begin
  7865. StartProgram(false);
  7866. Add('type t = string[12];');
  7867. Add('var s: t;');
  7868. Add('begin');
  7869. SetExpectedPasResolverError('illegal qualifier "["',nIllegalQualifier);
  7870. ConvertProgram;
  7871. end;
  7872. procedure TTestModule.TestCharSet_Custom;
  7873. begin
  7874. StartProgram(false);
  7875. Add([
  7876. 'type',
  7877. ' TCharRg = ''a''..''z'';',
  7878. ' TSetOfCharRg = set of TCharRg;',
  7879. ' TCharRg2 = ''m''..''p'';',
  7880. 'const',
  7881. ' crg: TCharRg = ''b'';',
  7882. 'var',
  7883. ' c: char;',
  7884. ' crg2: TCharRg2;',
  7885. ' s: TSetOfCharRg;',
  7886. 'begin',
  7887. ' c:=crg;',
  7888. ' crg:=c;',
  7889. ' crg2:=crg;',
  7890. ' if c=crg then ;',
  7891. ' if crg=c then ;',
  7892. ' if crg=crg2 then ;',
  7893. ' if c in s then ;',
  7894. ' if crg2 in s then ;',
  7895. ' c:=default(TCharRg);',
  7896. '']);
  7897. ConvertProgram;
  7898. CheckSource('TestCharSet_Custom',
  7899. LinesToStr([ // statements
  7900. 'this.crg = "b";',
  7901. 'this.c = "";',
  7902. 'this.crg2 = "m";',
  7903. 'this.s = {};',
  7904. '']),
  7905. LinesToStr([ // this.$main
  7906. '$mod.c = $mod.crg;',
  7907. '$mod.crg = $mod.c;',
  7908. '$mod.crg2 = $mod.crg;',
  7909. 'if ($mod.c === $mod.crg) ;',
  7910. 'if ($mod.crg === $mod.c) ;',
  7911. 'if ($mod.crg === $mod.crg2) ;',
  7912. 'if ($mod.c.charCodeAt() in $mod.s) ;',
  7913. 'if ($mod.crg2.charCodeAt() in $mod.s) ;',
  7914. '$mod.c = "a";',
  7915. '']));
  7916. end;
  7917. procedure TTestModule.TestWideChar;
  7918. begin
  7919. StartProgram(false);
  7920. Add([
  7921. 'procedure Fly(var c: char);',
  7922. 'begin',
  7923. 'end;',
  7924. 'procedure Run(var c: widechar);',
  7925. 'begin',
  7926. 'end;',
  7927. 'var',
  7928. ' c: char;',
  7929. ' wc: widechar;',
  7930. ' w: word;',
  7931. 'begin',
  7932. ' Fly(wc);',
  7933. ' Run(c);',
  7934. ' wc:=WideChar(w);',
  7935. ' w:=ord(wc);',
  7936. '']);
  7937. ConvertProgram;
  7938. CheckSource('TestWideChar_VarArg',
  7939. LinesToStr([ // statements
  7940. 'this.Fly = function (c) {',
  7941. '};',
  7942. 'this.Run = function (c) {',
  7943. '};',
  7944. 'this.c = "";',
  7945. 'this.wc = "";',
  7946. 'this.w = 0;',
  7947. '']),
  7948. LinesToStr([ // this.$main
  7949. '$mod.Fly({',
  7950. ' p: $mod,',
  7951. ' get: function () {',
  7952. ' return this.p.wc;',
  7953. ' },',
  7954. ' set: function (v) {',
  7955. ' this.p.wc = v;',
  7956. ' }',
  7957. '});',
  7958. '$mod.Run({',
  7959. ' p: $mod,',
  7960. ' get: function () {',
  7961. ' return this.p.c;',
  7962. ' },',
  7963. ' set: function (v) {',
  7964. ' this.p.c = v;',
  7965. ' }',
  7966. '});',
  7967. '$mod.wc = String.fromCharCode($mod.w);',
  7968. '$mod.w = $mod.wc.charCodeAt();',
  7969. '',
  7970. '']));
  7971. end;
  7972. procedure TTestModule.TestForCharDo;
  7973. begin
  7974. StartProgram(false);
  7975. Add([
  7976. 'var c: char;',
  7977. 'begin',
  7978. ' for c:=''a'' to ''c'' do ;',
  7979. ' for c:=c downto ''a'' do ;',
  7980. ' for c:=''Б'' to ''Я'' do ;',
  7981. '']);
  7982. ConvertProgram;
  7983. CheckSource('TestForCharDo',
  7984. LinesToStr([ // statements
  7985. 'this.c = "";']),
  7986. LinesToStr([ // this.$main
  7987. 'for (var $l = 97; $l <= 99; $l++) $mod.c = String.fromCharCode($l);',
  7988. 'for (var $l1 = $mod.c.charCodeAt(); $l1 >= 97; $l1--) $mod.c = String.fromCharCode($l1);',
  7989. 'for (var $l2 = 1041; $l2 <= 1071; $l2++) $mod.c = String.fromCharCode($l2);',
  7990. '']));
  7991. end;
  7992. procedure TTestModule.TestForCharInDo;
  7993. begin
  7994. StartProgram(false);
  7995. Add([
  7996. 'type',
  7997. ' TSetOfChar = set of char;',
  7998. ' TCharRg = ''a''..''z'';',
  7999. ' TSetOfCharRg = set of TCharRg;',
  8000. 'const Foo = ''foo'';',
  8001. 'var',
  8002. ' c,c2: char;',
  8003. ' s: string;',
  8004. ' a1: array of char;',
  8005. ' a2: array[1..3] of char;',
  8006. ' soc: TSetOfChar;',
  8007. ' socr: TSetOfCharRg;',
  8008. ' cr: TCharRg;',
  8009. 'begin',
  8010. ' for c in foo do ;',
  8011. ' for c in s do ;',
  8012. ' for c in char do ;',
  8013. ' for c in a1 do ;',
  8014. ' for c in a2 do ;',
  8015. ' for c in [''1''..''3''] do ;',
  8016. ' for c in TSetOfChar do ;',
  8017. ' for c in TCharRg do ;',
  8018. ' for c in soc do c2:=c;',
  8019. ' for c in TSetOfCharRg do ;',
  8020. ' for c in socr do ;',
  8021. ' for cr in TCharRg do ;',
  8022. ' for cr in TSetOfCharRg do ;',
  8023. ' for cr in socr do ;',
  8024. '']);
  8025. ConvertProgram;
  8026. CheckSource('TestForCharInDo',
  8027. LinesToStr([ // statements
  8028. 'this.Foo = "foo";',
  8029. 'this.c = "";',
  8030. 'this.c2 = "";',
  8031. 'this.s = "";',
  8032. 'this.a1 = [];',
  8033. 'this.a2 = rtl.arraySetLength(null, "", 3);',
  8034. 'this.soc = {};',
  8035. 'this.socr = {};',
  8036. 'this.cr = "a";',
  8037. '']),
  8038. LinesToStr([ // this.$main
  8039. 'for (var $in = $mod.Foo, $l = 0, $end = $in.length - 1; $l <= $end; $l++) $mod.c = $in.charAt($l);',
  8040. 'for (var $in1 = $mod.s, $l1 = 0, $end1 = $in1.length - 1; $l1 <= $end1; $l1++) $mod.c = $in1.charAt($l1);',
  8041. 'for (var $l2 = 0; $l2 <= 65535; $l2++) $mod.c = String.fromCharCode($l2);',
  8042. 'for (var $in2 = $mod.a1, $l3 = 0, $end2 = rtl.length($in2) - 1; $l3 <= $end2; $l3++) $mod.c = $in2[$l3];',
  8043. 'for (var $in3 = $mod.a2, $l4 = 0, $end3 = rtl.length($in3) - 1; $l4 <= $end3; $l4++) $mod.c = $in3[$l4];',
  8044. 'for (var $l5 = 49; $l5 <= 51; $l5++) $mod.c = String.fromCharCode($l5);',
  8045. 'for (var $l6 = 0; $l6 <= 65535; $l6++) $mod.c = String.fromCharCode($l6);',
  8046. 'for (var $l7 = 97; $l7 <= 122; $l7++) $mod.c = String.fromCharCode($l7);',
  8047. 'for (var $l8 in $mod.soc) {',
  8048. ' $mod.c = String.fromCharCode($l8);',
  8049. ' $mod.c2 = $mod.c;',
  8050. '};',
  8051. 'for (var $l9 = 97; $l9 <= 122; $l9++) $mod.c = String.fromCharCode($l9);',
  8052. 'for (var $l10 in $mod.socr) $mod.c = String.fromCharCode($l10);',
  8053. 'for (var $l11 = 97; $l11 <= 122; $l11++) $mod.cr = String.fromCharCode($l11);',
  8054. 'for (var $l12 = 97; $l12 <= 122; $l12++) $mod.cr = String.fromCharCode($l12);',
  8055. 'for (var $l13 in $mod.socr) $mod.cr = String.fromCharCode($l13);',
  8056. '']));
  8057. end;
  8058. procedure TTestModule.TestProcTwoArgs;
  8059. begin
  8060. StartProgram(false);
  8061. Add('procedure Test(a,b: longint);');
  8062. Add('begin');
  8063. Add('end;');
  8064. Add('begin');
  8065. ConvertProgram;
  8066. CheckSource('TestProcTwoArgs',
  8067. LinesToStr([ // statements
  8068. 'this.Test = function (a,b) {',
  8069. '};'
  8070. ]),
  8071. LinesToStr([ // this.$main
  8072. ''
  8073. ]));
  8074. end;
  8075. procedure TTestModule.TestProc_DefaultValue;
  8076. begin
  8077. StartProgram(false);
  8078. Add('procedure p1(i: longint = 1);');
  8079. Add('begin');
  8080. Add('end;');
  8081. Add('procedure p2(i: longint = 1; c: char = ''a'');');
  8082. Add('begin');
  8083. Add('end;');
  8084. Add('procedure p3(d: double = 1.0; b: boolean = false; s: string = ''abc'');');
  8085. Add('begin');
  8086. Add('end;');
  8087. Add('begin');
  8088. Add(' p1;');
  8089. Add(' p1();');
  8090. Add(' p1(11);');
  8091. Add(' p2;');
  8092. Add(' p2();');
  8093. Add(' p2(12);');
  8094. Add(' p2(13,''b'');');
  8095. Add(' p3();');
  8096. ConvertProgram;
  8097. CheckSource('TestProc_DefaultValue',
  8098. LinesToStr([ // statements
  8099. 'this.p1 = function (i) {',
  8100. '};',
  8101. 'this.p2 = function (i,c) {',
  8102. '};',
  8103. 'this.p3 = function (d,b,s) {',
  8104. '};'
  8105. ]),
  8106. LinesToStr([ // this.$main
  8107. ' $mod.p1(1);',
  8108. ' $mod.p1(1);',
  8109. ' $mod.p1(11);',
  8110. ' $mod.p2(1,"a");',
  8111. ' $mod.p2(1,"a");',
  8112. ' $mod.p2(12,"a");',
  8113. ' $mod.p2(13,"b");',
  8114. ' $mod.p3(1.0,false,"abc");'
  8115. ]));
  8116. end;
  8117. procedure TTestModule.TestFunctionInt;
  8118. begin
  8119. StartProgram(false);
  8120. Add('function MyTest(Bar: longint): longint;');
  8121. Add('begin');
  8122. Add(' Result:=2*bar');
  8123. Add('end;');
  8124. Add('begin');
  8125. ConvertProgram;
  8126. CheckSource('TestFunctionInt',
  8127. LinesToStr([ // statements
  8128. 'this.MyTest = function (Bar) {',
  8129. ' var Result = 0;',
  8130. ' Result = 2*Bar;',
  8131. ' return Result;',
  8132. '};'
  8133. ]),
  8134. LinesToStr([ // this.$main
  8135. ''
  8136. ]));
  8137. end;
  8138. procedure TTestModule.TestFunctionString;
  8139. begin
  8140. StartProgram(false);
  8141. Add('function Test(Bar: string): string;');
  8142. Add('begin');
  8143. Add(' Result:=bar+BAR');
  8144. Add('end;');
  8145. Add('begin');
  8146. ConvertProgram;
  8147. CheckSource('TestFunctionString',
  8148. LinesToStr([ // statements
  8149. 'this.Test = function (Bar) {',
  8150. ' var Result = "";',
  8151. ' Result = Bar+Bar;',
  8152. ' return Result;',
  8153. '};'
  8154. ]),
  8155. LinesToStr([ // this.$main
  8156. ''
  8157. ]));
  8158. end;
  8159. procedure TTestModule.TestIfThen;
  8160. begin
  8161. StartProgram(false);
  8162. Add([
  8163. 'var b: boolean;',
  8164. 'begin',
  8165. ' if b then ;',
  8166. ' if b then else ;']);
  8167. ConvertProgram;
  8168. CheckSource('TestIfThen',
  8169. LinesToStr([ // statements
  8170. 'this.b = false;',
  8171. '']),
  8172. LinesToStr([ // this.$main
  8173. 'if ($mod.b) ;',
  8174. 'if ($mod.b) ;',
  8175. '']));
  8176. end;
  8177. procedure TTestModule.TestForLoop;
  8178. begin
  8179. StartProgram(false);
  8180. Add('var');
  8181. Add(' vI, vJ, vN: longint;');
  8182. Add('begin');
  8183. Add(' VJ:=0;');
  8184. Add(' VN:=3;');
  8185. Add(' for VI:=1 to VN do');
  8186. Add(' begin');
  8187. Add(' VJ:=VJ+VI;');
  8188. Add(' end;');
  8189. ConvertProgram;
  8190. CheckSource('TestForLoop',
  8191. LinesToStr([ // statements
  8192. 'this.vI = 0;',
  8193. 'this.vJ = 0;',
  8194. 'this.vN = 0;'
  8195. ]),
  8196. LinesToStr([ // this.$main
  8197. ' $mod.vJ = 0;',
  8198. ' $mod.vN = 3;',
  8199. ' for (var $l = 1, $end = $mod.vN; $l <= $end; $l++) {',
  8200. ' $mod.vI = $l;',
  8201. ' $mod.vJ = $mod.vJ + $mod.vI;',
  8202. ' };',
  8203. '']));
  8204. end;
  8205. procedure TTestModule.TestForLoopInsideFunction;
  8206. begin
  8207. StartProgram(false);
  8208. Add('function SumNumbers(Count: longint): longint;');
  8209. Add('var');
  8210. Add(' vI, vJ: longint;');
  8211. Add('begin');
  8212. Add(' vj:=0;');
  8213. Add(' for vi:=1 to count do');
  8214. Add(' begin');
  8215. Add(' vj:=vj+vi;');
  8216. Add(' end;');
  8217. Add('end;');
  8218. Add('begin');
  8219. Add(' sumnumbers(3);');
  8220. ConvertProgram;
  8221. CheckSource('TestForLoopInsideFunction',
  8222. LinesToStr([ // statements
  8223. 'this.SumNumbers = function (Count) {',
  8224. ' var Result = 0;',
  8225. ' var vI = 0;',
  8226. ' var vJ = 0;',
  8227. ' vJ = 0;',
  8228. ' for (var $l = 1, $end = Count; $l <= $end; $l++) {',
  8229. ' vI = $l;',
  8230. ' vJ = vJ + vI;',
  8231. ' };',
  8232. ' return Result;',
  8233. '};'
  8234. ]),
  8235. LinesToStr([ // $mod.$main
  8236. ' $mod.SumNumbers(3);'
  8237. ]));
  8238. end;
  8239. procedure TTestModule.TestForLoop_ReadVarAfter;
  8240. begin
  8241. StartProgram(false);
  8242. Add('var');
  8243. Add(' vI: longint;');
  8244. Add('begin');
  8245. Add(' for vi:=1 to 2 do ;');
  8246. Add(' if vi=3 then ;');
  8247. ConvertProgram;
  8248. CheckSource('TestForLoop',
  8249. LinesToStr([ // statements
  8250. 'this.vI = 0;'
  8251. ]),
  8252. LinesToStr([ // this.$main
  8253. ' for ($mod.vI = 1; $mod.vI <= 2; $mod.vI++) ;',
  8254. ' if ($mod.vI===3) ;'
  8255. ]));
  8256. end;
  8257. procedure TTestModule.TestForLoop_Nested;
  8258. begin
  8259. StartProgram(false);
  8260. Add('function SumNumbers(Count: longint): longint;');
  8261. Add('var');
  8262. Add(' vI, vJ, vK: longint;');
  8263. Add('begin');
  8264. Add(' VK:=0;');
  8265. Add(' for VI:=1 to count do');
  8266. Add(' begin');
  8267. Add(' for vj:=1 to vi do');
  8268. Add(' begin');
  8269. Add(' vk:=VK+VI;');
  8270. Add(' end;');
  8271. Add(' end;');
  8272. Add('end;');
  8273. Add('begin');
  8274. Add(' sumnumbers(3);');
  8275. ConvertProgram;
  8276. CheckSource('TestForLoopInFunction',
  8277. LinesToStr([ // statements
  8278. 'this.SumNumbers = function (Count) {',
  8279. ' var Result = 0;',
  8280. ' var vI = 0;',
  8281. ' var vJ = 0;',
  8282. ' var vK = 0;',
  8283. ' vK = 0;',
  8284. ' for (var $l = 1, $end = Count; $l <= $end; $l++) {',
  8285. ' vI = $l;',
  8286. ' for (var $l1 = 1, $end1 = vI; $l1 <= $end1; $l1++) {',
  8287. ' vJ = $l1;',
  8288. ' vK = vK + vI;',
  8289. ' };',
  8290. ' };',
  8291. ' return Result;',
  8292. '};'
  8293. ]),
  8294. LinesToStr([ // $mod.$main
  8295. ' $mod.SumNumbers(3);'
  8296. ]));
  8297. end;
  8298. procedure TTestModule.TestRepeatUntil;
  8299. begin
  8300. StartProgram(false);
  8301. Add('var');
  8302. Add(' vI, vJ, vN: longint;');
  8303. Add('begin');
  8304. Add(' vn:=3;');
  8305. Add(' vj:=0;');
  8306. Add(' VI:=0;');
  8307. Add(' repeat');
  8308. Add(' VI:=vi+1;');
  8309. Add(' vj:=VJ+vI;');
  8310. Add(' until vi>=vn');
  8311. ConvertProgram;
  8312. CheckSource('TestRepeatUntil',
  8313. LinesToStr([ // statements
  8314. 'this.vI = 0;',
  8315. 'this.vJ = 0;',
  8316. 'this.vN = 0;'
  8317. ]),
  8318. LinesToStr([ // $mod.$main
  8319. ' $mod.vN = 3;',
  8320. ' $mod.vJ = 0;',
  8321. ' $mod.vI = 0;',
  8322. ' do{',
  8323. ' $mod.vI = $mod.vI + 1;',
  8324. ' $mod.vJ = $mod.vJ + $mod.vI;',
  8325. ' }while(!($mod.vI>=$mod.vN));'
  8326. ]));
  8327. end;
  8328. procedure TTestModule.TestAsmBlock;
  8329. begin
  8330. StartProgram(false);
  8331. Add([
  8332. 'var',
  8333. ' vI: longint;',
  8334. 'begin',
  8335. ' vi:=1;',
  8336. ' asm',
  8337. ' if (vI===1) {',
  8338. ' vI=2;',
  8339. //' console.log(''end;'');', ToDo
  8340. ' }',
  8341. ' if (vI===2){ vI=3; }',
  8342. ' end;',
  8343. ' VI:=4;']);
  8344. ConvertProgram;
  8345. CheckSource('TestAsmBlock',
  8346. LinesToStr([ // statements
  8347. 'this.vI = 0;'
  8348. ]),
  8349. LinesToStr([ // $mod.$main
  8350. '$mod.vI = 1;',
  8351. 'if (vI===1) {',
  8352. ' vI=2;',
  8353. '}',
  8354. 'if (vI===2){ vI=3; }',
  8355. ';',
  8356. '$mod.vI = 4;'
  8357. ]));
  8358. end;
  8359. procedure TTestModule.TestAsmPas_Impl;
  8360. begin
  8361. StartUnit(false);
  8362. Add('interface');
  8363. Add('const cIntf: longint = 1;');
  8364. Add('var vIntf: longint;');
  8365. Add('implementation');
  8366. Add('const cImpl: longint = 2;');
  8367. Add('var vImpl: longint;');
  8368. Add('procedure DoIt;');
  8369. Add('const cLoc: longint = 3;');
  8370. Add('var vLoc: longint;');
  8371. Add('begin;');
  8372. Add(' asm');
  8373. //Add(' pas(vIntf)=pas(cIntf);');
  8374. //Add(' pas(vImpl)=pas(cImpl);');
  8375. //Add(' pas(vLoc)=pas(cLoc);');
  8376. Add(' end;');
  8377. Add('end;');
  8378. ConvertUnit;
  8379. CheckSource('TestAsmPas_Impl',
  8380. LinesToStr([
  8381. 'var $impl = $mod.$impl;',
  8382. 'this.cIntf = 1;',
  8383. 'this.vIntf = 0;',
  8384. '']),
  8385. '', // this.$init
  8386. LinesToStr([ // implementation
  8387. '$impl.cImpl = 2;',
  8388. '$impl.vImpl = 0;',
  8389. 'var cLoc = 3;',
  8390. '$impl.DoIt = function () {',
  8391. ' var vLoc = 0;',
  8392. '};',
  8393. '']) );
  8394. end;
  8395. procedure TTestModule.TestTryFinally;
  8396. begin
  8397. StartProgram(false);
  8398. Add('var i: longint;');
  8399. Add('begin');
  8400. Add(' try');
  8401. Add(' i:=0; i:=2 div i;');
  8402. Add(' finally');
  8403. Add(' i:=3');
  8404. Add(' end;');
  8405. ConvertProgram;
  8406. CheckSource('TestTryFinally',
  8407. LinesToStr([ // statements
  8408. 'this.i = 0;'
  8409. ]),
  8410. LinesToStr([ // $mod.$main
  8411. 'try {',
  8412. ' $mod.i = 0;',
  8413. ' $mod.i = rtl.trunc(2 / $mod.i);',
  8414. '} finally {',
  8415. ' $mod.i = 3;',
  8416. '};'
  8417. ]));
  8418. end;
  8419. procedure TTestModule.TestTryExcept;
  8420. begin
  8421. StartProgram(false);
  8422. Add([
  8423. 'type',
  8424. ' TObject = class end;',
  8425. ' Exception = class Msg: string; end;',
  8426. ' EInvalidCast = class(Exception) end;',
  8427. 'var vI: longint;',
  8428. 'begin',
  8429. ' try',
  8430. ' vi:=1;',
  8431. ' except',
  8432. ' vi:=2',
  8433. ' end;',
  8434. ' try',
  8435. ' vi:=3;',
  8436. ' except',
  8437. ' raise;',
  8438. ' end;',
  8439. ' try',
  8440. ' VI:=4;',
  8441. ' except',
  8442. ' on einvalidcast do',
  8443. ' raise;',
  8444. ' on E: exception do',
  8445. ' if e.msg='''' then',
  8446. ' raise e;',
  8447. ' else',
  8448. ' vi:=5',
  8449. ' end;',
  8450. ' try',
  8451. ' VI:=6;',
  8452. ' except',
  8453. ' on einvalidcast do ;',
  8454. ' end;',
  8455. '']);
  8456. ConvertProgram;
  8457. CheckSource('TestTryExcept',
  8458. LinesToStr([ // statements
  8459. 'rtl.createClass(this, "TObject", null, function () {',
  8460. ' this.$init = function () {',
  8461. ' };',
  8462. ' this.$final = function () {',
  8463. ' };',
  8464. '});',
  8465. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  8466. ' this.$init = function () {',
  8467. ' $mod.TObject.$init.call(this);',
  8468. ' this.Msg = "";',
  8469. ' };',
  8470. '});',
  8471. 'rtl.createClass(this, "EInvalidCast", this.Exception, function () {',
  8472. '});',
  8473. 'this.vI = 0;'
  8474. ]),
  8475. LinesToStr([ // $mod.$main
  8476. 'try {',
  8477. ' $mod.vI = 1;',
  8478. '} catch ($e) {',
  8479. ' $mod.vI = 2;',
  8480. '};',
  8481. 'try {',
  8482. ' $mod.vI = 3;',
  8483. '} catch ($e) {',
  8484. ' throw $e;',
  8485. '};',
  8486. 'try {',
  8487. ' $mod.vI = 4;',
  8488. '} catch ($e) {',
  8489. ' if ($mod.EInvalidCast.isPrototypeOf($e)){',
  8490. ' throw $e',
  8491. ' } else if ($mod.Exception.isPrototypeOf($e)) {',
  8492. ' var E = $e;',
  8493. ' if (E.Msg === "") throw E;',
  8494. ' } else {',
  8495. ' $mod.vI = 5;',
  8496. ' }',
  8497. '};',
  8498. 'try {',
  8499. ' $mod.vI = 6;',
  8500. '} catch ($e) {',
  8501. ' if ($mod.EInvalidCast.isPrototypeOf($e)){' ,
  8502. ' } else throw $e',
  8503. '};',
  8504. '']));
  8505. end;
  8506. procedure TTestModule.TestTryExcept_ReservedWords;
  8507. begin
  8508. StartProgram(false);
  8509. Add([
  8510. 'type',
  8511. ' TObject = class end;',
  8512. ' Exception = class',
  8513. ' Symbol: string;',
  8514. ' end;',
  8515. 'var &try: longint;',
  8516. 'begin',
  8517. ' try',
  8518. ' &try:=4;',
  8519. ' except',
  8520. ' on Error: exception do',
  8521. ' if errOR.symBol='''' then',
  8522. ' raise ERRor;',
  8523. ' end;',
  8524. '']);
  8525. ConvertProgram;
  8526. CheckSource('TestTryExcept_ReservedWords',
  8527. LinesToStr([ // statements
  8528. 'rtl.createClass(this, "TObject", null, function () {',
  8529. ' this.$init = function () {',
  8530. ' };',
  8531. ' this.$final = function () {',
  8532. ' };',
  8533. '});',
  8534. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  8535. ' this.$init = function () {',
  8536. ' $mod.TObject.$init.call(this);',
  8537. ' this.Symbol = "";',
  8538. ' };',
  8539. '});',
  8540. 'this.Try = 0;',
  8541. '']),
  8542. LinesToStr([ // $mod.$main
  8543. 'try {',
  8544. ' $mod.Try = 4;',
  8545. '} catch ($e) {',
  8546. ' if ($mod.Exception.isPrototypeOf($e)) {',
  8547. ' var error = $e;',
  8548. ' if (error.Symbol === "") throw error;',
  8549. ' } else throw $e',
  8550. '};',
  8551. '']));
  8552. end;
  8553. procedure TTestModule.TestIfThenRaiseElse;
  8554. begin
  8555. StartProgram(false);
  8556. Add([
  8557. 'type',
  8558. ' TObject = class',
  8559. ' constructor Create;',
  8560. ' end;',
  8561. 'constructor TObject.Create;',
  8562. 'begin',
  8563. 'end;',
  8564. 'var b: boolean;',
  8565. 'begin',
  8566. ' if b then',
  8567. ' raise TObject.Create',
  8568. ' else',
  8569. ' b:=false;',
  8570. '']);
  8571. ConvertProgram;
  8572. CheckSource('TestIfThenRaiseElse',
  8573. LinesToStr([ // statements
  8574. 'rtl.createClass(this, "TObject", null, function () {',
  8575. ' this.$init = function () {',
  8576. ' };',
  8577. ' this.$final = function () {',
  8578. ' };',
  8579. ' this.Create = function () {',
  8580. ' return this;',
  8581. ' };',
  8582. '});',
  8583. 'this.b = false;',
  8584. '']),
  8585. LinesToStr([ // $mod.$main
  8586. 'if ($mod.b) {',
  8587. ' throw $mod.TObject.$create("Create")}',
  8588. ' else $mod.b = false;',
  8589. '']));
  8590. end;
  8591. procedure TTestModule.TestCaseOf;
  8592. begin
  8593. StartProgram(false);
  8594. Add([
  8595. 'const e: longint; external name ''$e'';',
  8596. 'var vI: longint;',
  8597. 'begin',
  8598. ' case vi of',
  8599. ' 1: ;',
  8600. ' 2: vi:=3;',
  8601. ' e: ;',
  8602. ' else',
  8603. ' VI:=4',
  8604. ' end;']);
  8605. ConvertProgram;
  8606. CheckSource('TestCaseOf',
  8607. LinesToStr([ // statements
  8608. 'this.vI = 0;'
  8609. ]),
  8610. LinesToStr([ // $mod.$main
  8611. 'var $tmp = $mod.vI;',
  8612. 'if ($tmp === 1) {}',
  8613. 'else if ($tmp === 2) {',
  8614. ' $mod.vI = 3}',
  8615. ' else if ($tmp === $e) {}',
  8616. 'else {',
  8617. ' $mod.vI = 4;',
  8618. '};'
  8619. ]));
  8620. end;
  8621. procedure TTestModule.TestCaseOf_UseSwitch;
  8622. begin
  8623. StartProgram(false);
  8624. Converter.UseSwitchStatement:=true;
  8625. Add('var Vi: longint;');
  8626. Add('begin');
  8627. Add(' case vi of');
  8628. Add(' 1: ;');
  8629. Add(' 2: VI:=3;');
  8630. Add(' else');
  8631. Add(' vi:=4');
  8632. Add(' end;');
  8633. ConvertProgram;
  8634. CheckSource('TestCaseOf_UseSwitch',
  8635. LinesToStr([ // statements
  8636. 'this.Vi = 0;'
  8637. ]),
  8638. LinesToStr([ // $mod.$main
  8639. 'switch ($mod.Vi) {',
  8640. 'case 1:',
  8641. ' break;',
  8642. 'case 2:',
  8643. ' $mod.Vi = 3;',
  8644. ' break;',
  8645. 'default:',
  8646. ' $mod.Vi = 4;',
  8647. '};'
  8648. ]));
  8649. end;
  8650. procedure TTestModule.TestCaseOfNoElse;
  8651. begin
  8652. StartProgram(false);
  8653. Add('var Vi: longint;');
  8654. Add('begin');
  8655. Add(' case vi of');
  8656. Add(' 1: begin vi:=2; VI:=3; end;');
  8657. Add(' end;');
  8658. ConvertProgram;
  8659. CheckSource('TestCaseOfNoElse',
  8660. LinesToStr([ // statements
  8661. 'this.Vi = 0;'
  8662. ]),
  8663. LinesToStr([ // $mod.$main
  8664. 'var $tmp = $mod.Vi;',
  8665. 'if ($tmp === 1) {',
  8666. ' $mod.Vi = 2;',
  8667. ' $mod.Vi = 3;',
  8668. '};'
  8669. ]));
  8670. end;
  8671. procedure TTestModule.TestCaseOfNoElse_UseSwitch;
  8672. begin
  8673. StartProgram(false);
  8674. Converter.UseSwitchStatement:=true;
  8675. Add('var vI: longint;');
  8676. Add('begin');
  8677. Add(' case vi of');
  8678. Add(' 1: begin VI:=2; vi:=3; end;');
  8679. Add(' end;');
  8680. ConvertProgram;
  8681. CheckSource('TestCaseOfNoElse_UseSwitch',
  8682. LinesToStr([ // statements
  8683. 'this.vI = 0;'
  8684. ]),
  8685. LinesToStr([ // $mod.$main
  8686. 'switch ($mod.vI) {',
  8687. 'case 1:',
  8688. ' $mod.vI = 2;',
  8689. ' $mod.vI = 3;',
  8690. ' break;',
  8691. '};'
  8692. ]));
  8693. end;
  8694. procedure TTestModule.TestCaseOfRange;
  8695. begin
  8696. StartProgram(false);
  8697. Add('var vI: longint;');
  8698. Add('begin');
  8699. Add(' case vi of');
  8700. Add(' 1..3: vi:=14;');
  8701. Add(' 4,5: vi:=16;');
  8702. Add(' 6..7,9..10: ;');
  8703. Add(' else ;');
  8704. Add(' end;');
  8705. ConvertProgram;
  8706. CheckSource('TestCaseOfRange',
  8707. LinesToStr([ // statements
  8708. 'this.vI = 0;'
  8709. ]),
  8710. LinesToStr([ // $mod.$main
  8711. 'var $tmp = $mod.vI;',
  8712. 'if (($tmp >= 1) && ($tmp <= 3)){',
  8713. ' $mod.vI = 14',
  8714. '} else if (($tmp === 4) || ($tmp === 5)){',
  8715. ' $mod.vI = 16',
  8716. '} else if ((($tmp >= 6) && ($tmp <= 7)) || (($tmp >= 9) && ($tmp <= 10))) ;'
  8717. ]));
  8718. end;
  8719. procedure TTestModule.TestCaseOfString;
  8720. begin
  8721. StartProgram(false);
  8722. Add([
  8723. 'var s,h: string;',
  8724. 'begin',
  8725. ' case s of',
  8726. ' ''foo'': s:=h;',
  8727. ' ''a''..''z'': h:=s;',
  8728. ' ''ў'', ''ё'': ;',
  8729. ' ''Б''..''Я'': ;',
  8730. ' end;',
  8731. '']);
  8732. ConvertProgram;
  8733. CheckSource('TestCaseOfString',
  8734. LinesToStr([ // statements
  8735. 'this.s = "";',
  8736. 'this.h = "";',
  8737. '']),
  8738. LinesToStr([ // $mod.$main
  8739. 'var $tmp = $mod.s;',
  8740. 'if ($tmp === "foo") {',
  8741. ' $mod.s = $mod.h}',
  8742. ' else if (($tmp.length === 1) && ($tmp >= "a") && ($tmp <= "z")) {',
  8743. ' $mod.h = $mod.s}',
  8744. ' else if (($tmp === "ў") || ($tmp === "ё")) {}',
  8745. ' else if (($tmp.length === 1) && ($tmp >= "Б") && ($tmp <= "Я")) ;',
  8746. '']));
  8747. end;
  8748. procedure TTestModule.TestCaseOfChar;
  8749. begin
  8750. StartProgram(false);
  8751. Add([
  8752. 'var s,h: char;',
  8753. 'begin',
  8754. ' case s of',
  8755. ' ''a''..''z'': h:=s;',
  8756. ' ''ä'': ;',
  8757. ' ''ў'', ''ё'': ;',
  8758. ' ''Б''..''Я'': ;',
  8759. ' end;',
  8760. '']);
  8761. ConvertProgram;
  8762. CheckSource('TestCaseOfString',
  8763. LinesToStr([ // statements
  8764. 'this.s = "";',
  8765. 'this.h = "";',
  8766. '']),
  8767. LinesToStr([ // $mod.$main
  8768. 'var $tmp = $mod.s;',
  8769. 'if (($tmp >= "a") && ($tmp <= "z")) {',
  8770. ' $mod.h = $mod.s}',
  8771. ' else if ($tmp === "ä") {}',
  8772. ' else if (($tmp === "ў") || ($tmp === "ё")) {}',
  8773. ' else if (($tmp >= "Б") && ($tmp <= "Я")) ;',
  8774. '']));
  8775. end;
  8776. procedure TTestModule.TestCaseOfExternalClassConst;
  8777. begin
  8778. StartProgram(false);
  8779. Add([
  8780. '{$modeswitch externalclass}',
  8781. 'type',
  8782. ' TBird = class external name ''Bird''',
  8783. ' const e: longint;',
  8784. ' end;',
  8785. 'var vI: longint;',
  8786. 'begin',
  8787. ' case vi of',
  8788. ' 1: vi:=3;',
  8789. ' TBird.e: ;',
  8790. ' end;']);
  8791. ConvertProgram;
  8792. CheckSource('TestCaseOfExternalClassConst',
  8793. LinesToStr([ // statements
  8794. 'this.vI = 0;'
  8795. ]),
  8796. LinesToStr([ // $mod.$main
  8797. 'var $tmp = $mod.vI;',
  8798. 'if ($tmp === 1) {',
  8799. ' $mod.vI = 3}',
  8800. ' else if ($tmp === Bird.e) ;'
  8801. ]));
  8802. end;
  8803. procedure TTestModule.TestDebugger;
  8804. begin
  8805. StartProgram(false);
  8806. Add([
  8807. 'procedure DoIt;',
  8808. 'begin',
  8809. ' deBugger;',
  8810. ' DeBugger();',
  8811. 'end;',
  8812. 'begin',
  8813. ' Debugger;']);
  8814. ConvertProgram;
  8815. CheckSource('TestDebugger',
  8816. LinesToStr([ // statements
  8817. 'this.DoIt = function () {',
  8818. ' debugger;',
  8819. ' debugger;',
  8820. '};',
  8821. '']),
  8822. LinesToStr([ // $mod.$main
  8823. 'debugger;',
  8824. '']));
  8825. end;
  8826. procedure TTestModule.TestArray_Dynamic;
  8827. begin
  8828. StartProgram(false);
  8829. Add([
  8830. 'type',
  8831. ' TArrayInt = array of longint;',
  8832. 'var',
  8833. ' Arr: TArrayInt;',
  8834. ' i: longint;',
  8835. ' b: boolean;',
  8836. 'begin',
  8837. ' SetLength(arr,3);',
  8838. ' arr[0]:=4;',
  8839. ' arr[1]:=length(arr)+arr[0];',
  8840. ' arr[i]:=5;',
  8841. ' arr[arr[i]]:=arr[6];',
  8842. ' i:=low(arr);',
  8843. ' i:=high(arr);',
  8844. ' b:=Assigned(arr);',
  8845. ' Arr:=default(TArrayInt);']);
  8846. ConvertProgram;
  8847. CheckSource('TestArray_Dynamic',
  8848. LinesToStr([ // statements
  8849. 'this.Arr = [];',
  8850. 'this.i = 0;',
  8851. 'this.b = false;'
  8852. ]),
  8853. LinesToStr([ // $mod.$main
  8854. '$mod.Arr = rtl.arraySetLength($mod.Arr,0,3);',
  8855. '$mod.Arr[0] = 4;',
  8856. '$mod.Arr[1] = rtl.length($mod.Arr) + $mod.Arr[0];',
  8857. '$mod.Arr[$mod.i] = 5;',
  8858. '$mod.Arr[$mod.Arr[$mod.i]] = $mod.Arr[6];',
  8859. '$mod.i = 0;',
  8860. '$mod.i = rtl.length($mod.Arr) - 1;',
  8861. '$mod.b = rtl.length($mod.Arr) > 0;',
  8862. '$mod.Arr = [];',
  8863. '']));
  8864. end;
  8865. procedure TTestModule.TestArray_Dynamic_Nil;
  8866. begin
  8867. StartProgram(false);
  8868. Add('type');
  8869. Add(' TArrayInt = array of longint;');
  8870. Add('var');
  8871. Add(' Arr: TArrayInt;');
  8872. Add('procedure DoIt(const i: TArrayInt; j: TArrayInt); begin end;');
  8873. Add('begin');
  8874. Add(' arr:=nil;');
  8875. Add(' if arr=nil then;');
  8876. Add(' if nil=arr then;');
  8877. Add(' if arr<>nil then;');
  8878. Add(' if nil<>arr then;');
  8879. Add(' DoIt(nil,nil);');
  8880. ConvertProgram;
  8881. CheckSource('TestArray_Dynamic',
  8882. LinesToStr([ // statements
  8883. 'this.Arr = [];',
  8884. 'this.DoIt = function(i,j){',
  8885. '};'
  8886. ]),
  8887. LinesToStr([ // $mod.$main
  8888. '$mod.Arr = [];',
  8889. 'if (rtl.length($mod.Arr) === 0) ;',
  8890. 'if (rtl.length($mod.Arr) === 0) ;',
  8891. 'if (rtl.length($mod.Arr) > 0) ;',
  8892. 'if (rtl.length($mod.Arr) > 0) ;',
  8893. '$mod.DoIt([],[]);',
  8894. '']));
  8895. end;
  8896. procedure TTestModule.TestArray_DynMultiDimensional;
  8897. begin
  8898. StartProgram(false);
  8899. Add([
  8900. 'type',
  8901. ' TArrayInt = array of longint;',
  8902. ' TArrayArrayInt = array of TArrayInt;',
  8903. 'var',
  8904. ' Arr: TArrayInt;',
  8905. ' Arr2: TArrayArrayInt;',
  8906. ' i: longint;',
  8907. 'begin',
  8908. ' arr2:=nil;',
  8909. ' if arr2=nil then;',
  8910. ' if nil=arr2 then;',
  8911. ' i:=low(arr2);',
  8912. ' i:=low(arr2[1]);',
  8913. ' i:=high(arr2);',
  8914. ' i:=high(arr2[2]);',
  8915. ' arr2[3]:=arr;',
  8916. ' arr2[4][5]:=i;',
  8917. ' i:=arr2[6][7];',
  8918. ' arr2[8,9]:=i;',
  8919. ' i:=arr2[10,11];',
  8920. ' SetLength(arr2,14);',
  8921. ' SetLength(arr2[15],16);']);
  8922. ConvertProgram;
  8923. CheckSource('TestArray_Dynamic',
  8924. LinesToStr([ // statements
  8925. 'this.Arr = [];',
  8926. 'this.Arr2 = [];',
  8927. 'this.i = 0;'
  8928. ]),
  8929. LinesToStr([ // $mod.$main
  8930. '$mod.Arr2 = [];',
  8931. 'if (rtl.length($mod.Arr2) === 0) ;',
  8932. 'if (rtl.length($mod.Arr2) === 0) ;',
  8933. '$mod.i = 0;',
  8934. '$mod.i = 0;',
  8935. '$mod.i = rtl.length($mod.Arr2) - 1;',
  8936. '$mod.i = rtl.length($mod.Arr2[2]) - 1;',
  8937. '$mod.Arr2[3] = rtl.arrayRef($mod.Arr);',
  8938. '$mod.Arr2[4][5] = $mod.i;',
  8939. '$mod.i = $mod.Arr2[6][7];',
  8940. '$mod.Arr2[8][9] = $mod.i;',
  8941. '$mod.i = $mod.Arr2[10][11];',
  8942. '$mod.Arr2 = rtl.arraySetLength($mod.Arr2, [], 14);',
  8943. '$mod.Arr2[15] = rtl.arraySetLength($mod.Arr2[15], 0, 16);',
  8944. '']));
  8945. end;
  8946. procedure TTestModule.TestArray_DynamicAssign;
  8947. begin
  8948. StartProgram(false);
  8949. Add([
  8950. 'type',
  8951. ' TArrayInt = array of longint;',
  8952. ' TArrayArrayInt = array of TArrayInt;',
  8953. 'procedure Run(a: TArrayInt; const b: TArrayInt; constref c: TArrayInt);',
  8954. 'begin',
  8955. 'end;',
  8956. 'procedure Fly(var a: TArrayInt);',
  8957. 'begin',
  8958. 'end;',
  8959. 'var',
  8960. ' Arr: TArrayInt;',
  8961. ' Arr2: TArrayArrayInt;',
  8962. 'begin',
  8963. ' arr:=nil;',
  8964. ' arr2:=nil;',
  8965. ' arr2[1]:=nil;',
  8966. ' arr2[2]:=arr;',
  8967. ' Run(arr,arr,arr);',
  8968. ' Fly(arr);',
  8969. ' Run(arr2[4],arr2[5],arr2[6]);',
  8970. ' Fly(arr2[7]);',
  8971. '']);
  8972. ConvertProgram;
  8973. CheckSource('TestArray_DynamicAssign',
  8974. LinesToStr([ // statements
  8975. 'this.Run = function (a, b, c) {',
  8976. '};',
  8977. 'this.Fly = function (a) {',
  8978. '};',
  8979. 'this.Arr = [];',
  8980. 'this.Arr2 = [];',
  8981. '']),
  8982. LinesToStr([ // $mod.$main
  8983. '$mod.Arr = [];',
  8984. '$mod.Arr2 = [];',
  8985. '$mod.Arr2[1] = [];',
  8986. '$mod.Arr2[2] = rtl.arrayRef($mod.Arr);',
  8987. '$mod.Run(rtl.arrayRef($mod.Arr), $mod.Arr, $mod.Arr);',
  8988. '$mod.Fly({',
  8989. ' p: $mod,',
  8990. ' get: function () {',
  8991. ' return this.p.Arr;',
  8992. ' },',
  8993. ' set: function (v) {',
  8994. ' this.p.Arr = v;',
  8995. ' }',
  8996. '});',
  8997. '$mod.Run(rtl.arrayRef($mod.Arr2[4]), $mod.Arr2[5], $mod.Arr2[6]);',
  8998. '$mod.Fly({',
  8999. ' a: 7,',
  9000. ' p: $mod.Arr2,',
  9001. ' get: function () {',
  9002. ' return this.p[this.a];',
  9003. ' },',
  9004. ' set: function (v) {',
  9005. ' this.p[this.a] = v;',
  9006. ' }',
  9007. '});',
  9008. '']));
  9009. end;
  9010. procedure TTestModule.TestArray_StaticInt;
  9011. begin
  9012. StartProgram(false);
  9013. Add('type');
  9014. Add(' TArrayInt = array[2..4] of longint;');
  9015. Add('var');
  9016. Add(' Arr: TArrayInt;');
  9017. Add(' Arr2: TArrayInt = (5,6,7);');
  9018. Add(' i: longint;');
  9019. Add(' b: boolean;');
  9020. Add('begin');
  9021. Add(' arr[2]:=4;');
  9022. Add(' arr[3]:=arr[2]+arr[3];');
  9023. Add(' arr[i]:=5;');
  9024. Add(' arr[arr[i]]:=arr[high(arr)];');
  9025. Add(' i:=low(arr);');
  9026. Add(' i:=high(arr);');
  9027. Add(' b:=arr[2]=arr[3];');
  9028. Add(' arr:=default(TArrayInt);');
  9029. ConvertProgram;
  9030. CheckSource('TestArray_StaticInt',
  9031. LinesToStr([ // statements
  9032. 'this.Arr = rtl.arraySetLength(null,0,3);',
  9033. 'this.Arr2 = [5, 6, 7];',
  9034. 'this.i = 0;',
  9035. 'this.b = false;'
  9036. ]),
  9037. LinesToStr([ // $mod.$main
  9038. '$mod.Arr[0] = 4;',
  9039. '$mod.Arr[1] = $mod.Arr[0] + $mod.Arr[1];',
  9040. '$mod.Arr[$mod.i-2] = 5;',
  9041. '$mod.Arr[$mod.Arr[$mod.i-2]-2] = $mod.Arr[2];',
  9042. '$mod.i = 2;',
  9043. '$mod.i = 4;',
  9044. '$mod.b = $mod.Arr[0] === $mod.Arr[1];',
  9045. '$mod.Arr = rtl.arraySetLength(null,0,3);',
  9046. '']));
  9047. end;
  9048. procedure TTestModule.TestArray_StaticBool;
  9049. begin
  9050. StartProgram(false);
  9051. Add('type');
  9052. Add(' TBools = array[boolean] of boolean;');
  9053. Add(' TBool2 = array[true..true] of boolean;');
  9054. Add('var');
  9055. Add(' Arr: TBools;');
  9056. Add(' Arr2: TBool2;');
  9057. Add(' Arr3: TBools = (true,false);');
  9058. Add(' b: boolean;');
  9059. Add('begin');
  9060. Add(' b:=low(arr);');
  9061. Add(' b:=high(arr);');
  9062. Add(' arr[true]:=false;');
  9063. Add(' arr[false]:=arr[b] or arr[true];');
  9064. Add(' arr[b]:=true;');
  9065. Add(' arr[arr[b]]:=arr[high(arr)];');
  9066. Add(' b:=arr[false]=arr[true];');
  9067. Add(' b:=low(arr2);');
  9068. Add(' b:=high(arr2);');
  9069. Add(' arr2[true]:=true;');
  9070. Add(' arr2[true]:=arr2[true] and arr2[b];');
  9071. Add(' arr2[b]:=false;');
  9072. ConvertProgram;
  9073. CheckSource('TestArray_StaticBool',
  9074. LinesToStr([ // statements
  9075. 'this.Arr = rtl.arraySetLength(null,false,2);',
  9076. 'this.Arr2 = rtl.arraySetLength(null,false,1);',
  9077. 'this.Arr3 = [true, false];',
  9078. 'this.b = false;'
  9079. ]),
  9080. LinesToStr([ // $mod.$main
  9081. '$mod.b = false;',
  9082. '$mod.b = true;',
  9083. '$mod.Arr[1] = false;',
  9084. '$mod.Arr[0] = $mod.Arr[+$mod.b] || $mod.Arr[1];',
  9085. '$mod.Arr[+$mod.b] = true;',
  9086. '$mod.Arr[+$mod.Arr[+$mod.b]] = $mod.Arr[1];',
  9087. '$mod.b = $mod.Arr[0] === $mod.Arr[1];',
  9088. '$mod.b = true;',
  9089. '$mod.b = true;',
  9090. '$mod.Arr2[0] = true;',
  9091. '$mod.Arr2[0] = $mod.Arr2[0] && $mod.Arr2[1-$mod.b];',
  9092. '$mod.Arr2[1-$mod.b] = false;',
  9093. '']));
  9094. end;
  9095. procedure TTestModule.TestArray_StaticChar;
  9096. begin
  9097. StartProgram(false);
  9098. Add([
  9099. 'type',
  9100. ' TChars = array[char] of char;',
  9101. ' TChars2 = array[''a''..''z''] of char;',
  9102. 'var',
  9103. ' Arr: TChars;',
  9104. ' Arr2: TChars2;',
  9105. ' Arr3: array[2..4] of char = (''p'',''a'',''s'');',
  9106. ' Arr4: array[11..13] of char = ''pas'';',
  9107. ' Arr5: array[21..22] of char = ''äö'';',
  9108. ' Arr6: array[31..32] of char = ''ä''+''ö'';',
  9109. ' c: char;',
  9110. ' b: boolean;',
  9111. 'begin',
  9112. ' c:=low(arr);',
  9113. ' c:=high(arr);',
  9114. ' arr[''B'']:=''a'';',
  9115. ' arr[''D'']:=arr[c];',
  9116. ' arr[c]:=arr[''d''];',
  9117. ' arr[arr[c]]:=arr[high(arr)];',
  9118. ' b:=arr[low(arr)]=arr[''e''];',
  9119. ' c:=low(arr2);',
  9120. ' c:=high(arr2);',
  9121. ' arr2[''b'']:=''f'';',
  9122. ' arr2[''a'']:=arr2[c];',
  9123. ' arr2[c]:=arr2[''g''];']);
  9124. ConvertProgram;
  9125. CheckSource('TestArray_StaticChar',
  9126. LinesToStr([ // statements
  9127. 'this.Arr = rtl.arraySetLength(null, "", 65536);',
  9128. 'this.Arr2 = rtl.arraySetLength(null, "", 26);',
  9129. 'this.Arr3 = ["p", "a", "s"];',
  9130. 'this.Arr4 = ["p", "a", "s"];',
  9131. 'this.Arr5 = ["ä", "ö"];',
  9132. 'this.Arr6 = ["ä", "ö"];',
  9133. 'this.c = "";',
  9134. 'this.b = false;',
  9135. '']),
  9136. LinesToStr([ // $mod.$main
  9137. '$mod.c = "\x00";',
  9138. '$mod.c = "\uFFFF";',
  9139. '$mod.Arr[66] = "a";',
  9140. '$mod.Arr[68] = $mod.Arr[$mod.c.charCodeAt()];',
  9141. '$mod.Arr[$mod.c.charCodeAt()] = $mod.Arr[100];',
  9142. '$mod.Arr[$mod.Arr[$mod.c.charCodeAt()].charCodeAt()] = $mod.Arr[65535];',
  9143. '$mod.b = $mod.Arr[0] === $mod.Arr[101];',
  9144. '$mod.c = "a";',
  9145. '$mod.c = "z";',
  9146. '$mod.Arr2[1] = "f";',
  9147. '$mod.Arr2[0] = $mod.Arr2[$mod.c.charCodeAt() - 97];',
  9148. '$mod.Arr2[$mod.c.charCodeAt() - 97] = $mod.Arr2[6];',
  9149. '']));
  9150. end;
  9151. procedure TTestModule.TestArray_StaticMultiDim;
  9152. begin
  9153. StartProgram(false);
  9154. Add([
  9155. 'type',
  9156. ' TArrayInt = array[1..3] of longint;',
  9157. ' TArrayArrayInt = array[5..6] of TArrayInt;',
  9158. 'var',
  9159. ' Arr: TArrayInt;',
  9160. ' Arr2: TArrayArrayInt;',
  9161. ' Arr3: array[boolean] of TArrayInt = ((11,12,13),(21,22,23));',
  9162. ' i: longint;',
  9163. 'begin',
  9164. ' i:=low(arr);',
  9165. ' i:=low(arr2);',
  9166. ' i:=low(arr2[5]);',
  9167. ' i:=high(arr);',
  9168. ' i:=high(arr2);',
  9169. ' i:=high(arr2[6]);',
  9170. ' arr2[5]:=arr;',
  9171. ' arr2[6][2]:=i;',
  9172. ' i:=arr2[6][3];',
  9173. ' arr2[6,3]:=i;',
  9174. ' i:=arr2[5,2];',
  9175. ' arr2:=arr2;',// clone multi dim static array
  9176. ' arr3:=arr3;',// clone anonymous multi dim static array
  9177. '']);
  9178. ConvertProgram;
  9179. CheckSource('TestArray_StaticMultiDim',
  9180. LinesToStr([ // statements
  9181. 'this.TArrayArrayInt$clone = function (a) {',
  9182. ' var r = [];',
  9183. ' for (var i = 0; i < 2; i++) r.push(a[i].slice(0));',
  9184. ' return r;',
  9185. '};',
  9186. 'this.Arr = rtl.arraySetLength(null, 0, 3);',
  9187. 'this.Arr2 = rtl.arraySetLength(null, 0, 2, 3);',
  9188. 'this.Arr3$a$clone = function (a) {',
  9189. ' var r = [];',
  9190. ' for (var i = 0; i < 2; i++) r.push(a[i].slice(0));',
  9191. ' return r;',
  9192. '};',
  9193. 'this.Arr3 = [[11, 12, 13], [21, 22, 23]];',
  9194. 'this.i = 0;'
  9195. ]),
  9196. LinesToStr([ // $mod.$main
  9197. '$mod.i = 1;',
  9198. '$mod.i = 5;',
  9199. '$mod.i = 1;',
  9200. '$mod.i = 3;',
  9201. '$mod.i = 6;',
  9202. '$mod.i = 3;',
  9203. '$mod.Arr2[0] = $mod.Arr.slice(0);',
  9204. '$mod.Arr2[1][1] = $mod.i;',
  9205. '$mod.i = $mod.Arr2[1][2];',
  9206. '$mod.Arr2[1][2] = $mod.i;',
  9207. '$mod.i = $mod.Arr2[0][1];',
  9208. '$mod.Arr2 = $mod.TArrayArrayInt$clone($mod.Arr2);',
  9209. '$mod.Arr3 = $mod.Arr3$a$clone($mod.Arr3);',
  9210. '']));
  9211. end;
  9212. procedure TTestModule.TestArray_StaticInFunction;
  9213. begin
  9214. StartProgram(false);
  9215. Add([
  9216. 'const TArrayInt = 3;',
  9217. 'const TArrayArrayInt = 4;',
  9218. 'procedure DoIt;',
  9219. 'type',
  9220. ' TArrayInt = array[1..3] of longint;',
  9221. ' TArrayArrayInt = array[5..6] of TArrayInt;',
  9222. 'var',
  9223. ' Arr: TArrayInt;',
  9224. ' Arr2: TArrayArrayInt;',
  9225. ' Arr3: array[boolean] of TArrayInt = ((11,12,13),(21,22,23));',
  9226. ' i: longint;',
  9227. 'begin',
  9228. ' arr2[5]:=arr;',
  9229. ' arr2:=arr2;',// clone multi dim static array
  9230. ' arr3:=arr3;',// clone multi dim anonymous static array
  9231. 'end;',
  9232. 'begin',
  9233. '']);
  9234. ConvertProgram;
  9235. CheckSource('TestArray_StaticInFunction',
  9236. LinesToStr([ // statements
  9237. 'this.TArrayInt = 3;',
  9238. 'this.TArrayArrayInt = 4;',
  9239. 'var TArrayArrayInt$1$clone = function (a) {',
  9240. ' var r = [];',
  9241. ' for (var i = 0; i < 2; i++) r.push(a[i].slice(0));',
  9242. ' return r;',
  9243. '};',
  9244. 'var Arr3$a$clone = function (a) {',
  9245. ' var r = [];',
  9246. ' for (var i = 0; i < 2; i++) r.push(a[i].slice(0));',
  9247. ' return r;',
  9248. '};',
  9249. 'this.DoIt = function () {',
  9250. ' var Arr = rtl.arraySetLength(null, 0, 3);',
  9251. ' var Arr2 = rtl.arraySetLength(null, 0, 2, 3);',
  9252. ' var Arr3 = [[11, 12, 13], [21, 22, 23]];',
  9253. ' var i = 0;',
  9254. ' Arr2[0] = Arr.slice(0);',
  9255. ' Arr2 = TArrayArrayInt$1$clone(Arr2);',
  9256. ' Arr3 = Arr3$a$clone(Arr3);',
  9257. '};',
  9258. '']),
  9259. LinesToStr([ // $mod.$main
  9260. '']));
  9261. end;
  9262. procedure TTestModule.TestArray_StaticMultiDimEqualNotImplemented;
  9263. begin
  9264. StartProgram(false);
  9265. Add([
  9266. 'type',
  9267. ' TArrayInt = array[1..3,1..2] of longint;',
  9268. 'var',
  9269. ' a,b: TArrayInt;',
  9270. 'begin',
  9271. ' if a=b then ;',
  9272. '']);
  9273. SetExpectedPasResolverError('compare static array is not supported',
  9274. nXIsNotSupported);
  9275. ConvertProgram;
  9276. end;
  9277. procedure TTestModule.TestArrayOfRecord;
  9278. begin
  9279. StartProgram(false);
  9280. Add([
  9281. 'type',
  9282. ' TRec = record',
  9283. ' Int: longint;',
  9284. ' end;',
  9285. ' TArrayRec = array of TRec;',
  9286. 'procedure DoIt(vd: TRec; const vc: TRec; var vv: TRec);',
  9287. 'begin',
  9288. 'end;',
  9289. 'var',
  9290. ' Arr: TArrayRec;',
  9291. ' r: TRec;',
  9292. ' i: longint;',
  9293. 'begin',
  9294. ' SetLength(arr,3);',
  9295. ' arr[0].int:=4;',
  9296. ' arr[1].int:=length(arr)+arr[2].int;',
  9297. ' arr[arr[i].int].int:=arr[5].int;',
  9298. ' arr[7]:=r;',
  9299. ' r:=arr[8];',
  9300. ' i:=low(arr);',
  9301. ' i:=high(arr);',
  9302. ' DoIt(Arr[9],Arr[10],Arr[11]);']);
  9303. ConvertProgram;
  9304. CheckSource('TestArrayOfRecord',
  9305. LinesToStr([ // statements
  9306. 'rtl.recNewT(this, "TRec", function () {',
  9307. ' this.Int = 0;',
  9308. ' this.$eq = function (b) {',
  9309. ' return this.Int === b.Int;',
  9310. ' };',
  9311. ' this.$assign = function (s) {',
  9312. ' this.Int = s.Int;',
  9313. ' return this;',
  9314. ' };',
  9315. '});',
  9316. 'this.DoIt = function (vd, vc, vv) {',
  9317. '};',
  9318. 'this.Arr = [];',
  9319. 'this.r = this.TRec.$new();',
  9320. 'this.i = 0;'
  9321. ]),
  9322. LinesToStr([ // $mod.$main
  9323. '$mod.Arr = rtl.arraySetLength($mod.Arr,$mod.TRec,3);',
  9324. '$mod.Arr[0].Int = 4;',
  9325. '$mod.Arr[1].Int = rtl.length($mod.Arr)+$mod.Arr[2].Int;',
  9326. '$mod.Arr[$mod.Arr[$mod.i].Int].Int = $mod.Arr[5].Int;',
  9327. '$mod.Arr[7].$assign($mod.r);',
  9328. '$mod.r.$assign($mod.Arr[8]);',
  9329. '$mod.i = 0;',
  9330. '$mod.i = rtl.length($mod.Arr)-1;',
  9331. '$mod.DoIt($mod.TRec.$clone($mod.Arr[9]), $mod.Arr[10], $mod.Arr[11]);',
  9332. '']));
  9333. end;
  9334. procedure TTestModule.TestArray_StaticRecord;
  9335. begin
  9336. StartProgram(false);
  9337. Add([
  9338. 'type',
  9339. ' TRec = record',
  9340. ' Int: longint;',
  9341. ' end;',
  9342. ' TArrayRec = array[1..2] of TRec;',
  9343. 'var',
  9344. ' Arr: TArrayRec;',
  9345. 'begin',
  9346. ' arr[1].int:=length(arr)+low(arr)+high(arr);',
  9347. '']);
  9348. ConvertProgram;
  9349. CheckSource('TestArray_StaticRecord',
  9350. LinesToStr([ // statements
  9351. 'rtl.recNewT(this, "TRec", function () {',
  9352. ' this.Int = 0;',
  9353. ' this.$eq = function (b) {',
  9354. ' return this.Int === b.Int;',
  9355. ' };',
  9356. ' this.$assign = function (s) {',
  9357. ' this.Int = s.Int;',
  9358. ' return this;',
  9359. ' };',
  9360. '});',
  9361. 'this.TArrayRec$clone = function (a) {',
  9362. ' var r = [];',
  9363. ' for (var i = 0; i < 2; i++) r.push($mod.TRec.$clone(a[i]));',
  9364. ' return r;',
  9365. '};',
  9366. 'this.Arr = rtl.arraySetLength(null, this.TRec, 2);',
  9367. '']),
  9368. LinesToStr([ // $mod.$main
  9369. '$mod.Arr[0].Int = 2 + 1 + 2;']));
  9370. end;
  9371. procedure TTestModule.TestArrayOfSet;
  9372. begin
  9373. StartProgram(false);
  9374. Add([
  9375. 'type',
  9376. ' TFlag = (big,small);',
  9377. ' TSetOfFlag = set of tflag;',
  9378. ' TArrayFlag = array of TSetOfFlag;',
  9379. 'procedure DoIt(const a: Tarrayflag);',
  9380. 'begin',
  9381. 'end;',
  9382. 'var',
  9383. ' f: TFlag;',
  9384. ' s: TSetOfFlag;',
  9385. ' Arr: TArrayFlag;',
  9386. ' i: longint;',
  9387. 'begin',
  9388. ' SetLength(arr,3);',
  9389. ' arr[0]:=s;',
  9390. ' arr[1]:=[big];',
  9391. ' arr[2]:=[big]+s;',
  9392. ' arr[3]:=s+[big];',
  9393. ' arr[4]:=arr[5];',
  9394. ' s:=arr[6];',
  9395. ' i:=low(arr);',
  9396. ' i:=high(arr);',
  9397. ' DoIt(arr);',
  9398. ' DoIt([s]);',
  9399. ' DoIt([[],s]);',
  9400. ' DoIt([s,[]]);',
  9401. '']);
  9402. ConvertProgram;
  9403. CheckSource('TestArrayOfSet',
  9404. LinesToStr([ // statements
  9405. 'this.TFlag = {',
  9406. ' "0": "big",',
  9407. ' big: 0,',
  9408. ' "1": "small",',
  9409. ' small: 1',
  9410. '};',
  9411. 'this.DoIt = function (a) {',
  9412. '};',
  9413. 'this.f = 0;',
  9414. 'this.s = {};',
  9415. 'this.Arr = [];',
  9416. 'this.i = 0;',
  9417. '']),
  9418. LinesToStr([ // $mod.$main
  9419. '$mod.Arr = rtl.arraySetLength($mod.Arr, {}, 3);',
  9420. '$mod.Arr[0] = rtl.refSet($mod.s);',
  9421. '$mod.Arr[1] = rtl.createSet($mod.TFlag.big);',
  9422. '$mod.Arr[2] = rtl.unionSet(rtl.createSet($mod.TFlag.big), $mod.s);',
  9423. '$mod.Arr[3] = rtl.unionSet($mod.s, rtl.createSet($mod.TFlag.big));',
  9424. '$mod.Arr[4] = rtl.refSet($mod.Arr[5]);',
  9425. '$mod.s = rtl.refSet($mod.Arr[6]);',
  9426. '$mod.i = 0;',
  9427. '$mod.i = rtl.length($mod.Arr) - 1;',
  9428. '$mod.DoIt($mod.Arr);',
  9429. '$mod.DoIt([rtl.refSet($mod.s)]);',
  9430. '$mod.DoIt([{}, rtl.refSet($mod.s)]);',
  9431. '$mod.DoIt([rtl.refSet($mod.s), {}]);',
  9432. '']));
  9433. end;
  9434. procedure TTestModule.TestArray_DynAsParam;
  9435. begin
  9436. StartProgram(false);
  9437. Add([
  9438. 'type integer = longint;',
  9439. 'type TArrInt = array of integer;',
  9440. 'procedure DoIt(vG: TArrInt; const vH: TArrInt; var vI: TArrInt);',
  9441. 'var vJ: TArrInt;',
  9442. 'begin',
  9443. ' vg:=vg;',
  9444. ' vj:=vh;',
  9445. ' vi:=vi;',
  9446. ' doit(vg,vg,vg);',
  9447. ' doit(vh,vh,vj);',
  9448. ' doit(vi,vi,vi);',
  9449. ' doit(vj,vj,vj);',
  9450. 'end;',
  9451. 'var i: TArrInt;',
  9452. 'begin',
  9453. ' doit(i,i,i);']);
  9454. ConvertProgram;
  9455. CheckSource('TestArray_DynAsParams',
  9456. LinesToStr([ // statements
  9457. 'this.DoIt = function (vG,vH,vI) {',
  9458. ' var vJ = [];',
  9459. ' vG = rtl.arrayRef(vG);',
  9460. ' vJ = rtl.arrayRef(vH);',
  9461. ' vI.set(rtl.arrayRef(vI.get()));',
  9462. ' $mod.DoIt(rtl.arrayRef(vG), vG, {',
  9463. ' get: function () {',
  9464. ' return vG;',
  9465. ' },',
  9466. ' set: function (v) {',
  9467. ' vG = v;',
  9468. ' }',
  9469. ' });',
  9470. ' $mod.DoIt(rtl.arrayRef(vH), vH, {',
  9471. ' get: function () {',
  9472. ' return vJ;',
  9473. ' },',
  9474. ' set: function (v) {',
  9475. ' vJ = v;',
  9476. ' }',
  9477. ' });',
  9478. ' $mod.DoIt(rtl.arrayRef(vI.get()), vI.get(), vI);',
  9479. ' $mod.DoIt(rtl.arrayRef(vJ), vJ, {',
  9480. ' get: function () {',
  9481. ' return vJ;',
  9482. ' },',
  9483. ' set: function (v) {',
  9484. ' vJ = v;',
  9485. ' }',
  9486. ' });',
  9487. '};',
  9488. 'this.i = [];'
  9489. ]),
  9490. LinesToStr([
  9491. '$mod.DoIt(rtl.arrayRef($mod.i),$mod.i,{',
  9492. ' p: $mod,',
  9493. ' get: function () {',
  9494. ' return this.p.i;',
  9495. ' },',
  9496. ' set: function (v) {',
  9497. ' this.p.i = v;',
  9498. ' }',
  9499. '});'
  9500. ]));
  9501. end;
  9502. procedure TTestModule.TestArray_StaticAsParam;
  9503. begin
  9504. StartProgram(false);
  9505. Add([
  9506. 'type integer = longint;',
  9507. 'type TArrInt = array[1..2] of integer;',
  9508. 'procedure DoIt(vG: TArrInt; const vH: TArrInt; var vI: TArrInt);',
  9509. 'var vJ: TArrInt;',
  9510. 'begin',
  9511. ' vg:=vg;',
  9512. ' vj:=vh;',
  9513. ' vi:=vi;',
  9514. ' doit(vg,vg,vg);',
  9515. ' doit(vh,vh,vj);',
  9516. ' doit(vi,vi,vi);',
  9517. ' doit(vj,vj,vj);',
  9518. 'end;',
  9519. 'var i: TArrInt;',
  9520. 'begin',
  9521. ' doit(i,i,i);']);
  9522. ConvertProgram;
  9523. CheckSource('TestArray_StaticAsParams',
  9524. LinesToStr([ // statements
  9525. 'this.DoIt = function (vG,vH,vI) {',
  9526. ' var vJ = rtl.arraySetLength(null, 0, 2);',
  9527. ' vG = vG.slice(0);',
  9528. ' vJ = vH.slice(0);',
  9529. ' vI.set(vI.get().slice(0));',
  9530. ' $mod.DoIt(vG.slice(0), vG, {',
  9531. ' get: function () {',
  9532. ' return vG;',
  9533. ' },',
  9534. ' set: function (v) {',
  9535. ' vG = v;',
  9536. ' }',
  9537. ' });',
  9538. ' $mod.DoIt(vH.slice(0), vH, {',
  9539. ' get: function () {',
  9540. ' return vJ;',
  9541. ' },',
  9542. ' set: function (v) {',
  9543. ' vJ = v;',
  9544. ' }',
  9545. ' });',
  9546. ' $mod.DoIt(vI.get().slice(0), vI.get(), vI);',
  9547. ' $mod.DoIt(vJ.slice(0), vJ, {',
  9548. ' get: function () {',
  9549. ' return vJ;',
  9550. ' },',
  9551. ' set: function (v) {',
  9552. ' vJ = v;',
  9553. ' }',
  9554. ' });',
  9555. '};',
  9556. 'this.i = rtl.arraySetLength(null, 0, 2);'
  9557. ]),
  9558. LinesToStr([
  9559. '$mod.DoIt($mod.i.slice(0),$mod.i,{',
  9560. ' p: $mod,',
  9561. ' get: function () {',
  9562. ' return this.p.i;',
  9563. ' },',
  9564. ' set: function (v) {',
  9565. ' this.p.i = v;',
  9566. ' }',
  9567. '});'
  9568. ]));
  9569. end;
  9570. procedure TTestModule.TestArrayElement_AsParams;
  9571. begin
  9572. StartProgram(false);
  9573. Add('type integer = longint;');
  9574. Add('type TArrayInt = array of integer;');
  9575. Add('procedure DoIt(vG: Integer; const vH: Integer; var vI: Integer);');
  9576. Add('var vJ: tarrayint;');
  9577. Add('begin');
  9578. Add(' vi:=vi;');
  9579. Add(' doit(vi,vi,vi);');
  9580. Add(' doit(vj[1+1],vj[1+2],vj[1+3]);');
  9581. Add('end;');
  9582. Add('var a: TArrayInt;');
  9583. Add('begin');
  9584. Add(' doit(a[1+4],a[1+5],a[1+6]);');
  9585. ConvertProgram;
  9586. CheckSource('TestArrayElement_AsParams',
  9587. LinesToStr([ // statements
  9588. 'this.DoIt = function (vG,vH,vI) {',
  9589. ' var vJ = [];',
  9590. ' vI.set(vI.get());',
  9591. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  9592. ' $mod.DoIt(vJ[1+1], vJ[1+2], {',
  9593. ' a:1+3,',
  9594. ' p:vJ,',
  9595. ' get: function () {',
  9596. ' return this.p[this.a];',
  9597. ' },',
  9598. ' set: function (v) {',
  9599. ' this.p[this.a] = v;',
  9600. ' }',
  9601. ' });',
  9602. '};',
  9603. 'this.a = [];'
  9604. ]),
  9605. LinesToStr([
  9606. '$mod.DoIt($mod.a[1+4],$mod.a[1+5],{',
  9607. ' a: 1+6,',
  9608. ' p: $mod.a,',
  9609. ' get: function () {',
  9610. ' return this.p[this.a];',
  9611. ' },',
  9612. ' set: function (v) {',
  9613. ' this.p[this.a] = v;',
  9614. ' }',
  9615. '});'
  9616. ]));
  9617. end;
  9618. procedure TTestModule.TestArrayElementFromFuncResult_AsParams;
  9619. begin
  9620. StartProgram(false);
  9621. Add('type Integer = longint;');
  9622. Add('type TArrayInt = array of integer;');
  9623. Add('function GetArr(vB: integer = 0): tarrayint;');
  9624. Add('begin');
  9625. Add('end;');
  9626. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  9627. Add('begin');
  9628. Add('end;');
  9629. Add('begin');
  9630. Add(' doit(getarr[1+1],getarr[1+2],getarr[1+3]);');
  9631. Add(' doit(getarr()[2+1],getarr()[2+2],getarr()[2+3]);');
  9632. Add(' doit(getarr(7)[3+1],getarr(8)[3+2],getarr(9)[3+3]);');
  9633. ConvertProgram;
  9634. CheckSource('TestArrayElementFromFuncResult_AsParams',
  9635. LinesToStr([ // statements
  9636. 'this.GetArr = function (vB) {',
  9637. ' var Result = [];',
  9638. ' return Result;',
  9639. '};',
  9640. 'this.DoIt = function (vG,vH,vI) {',
  9641. '};'
  9642. ]),
  9643. LinesToStr([
  9644. '$mod.DoIt($mod.GetArr(0)[1+1],$mod.GetArr(0)[1+2],{',
  9645. ' a: 1+3,',
  9646. ' p: $mod.GetArr(0),',
  9647. ' get: function () {',
  9648. ' return this.p[this.a];',
  9649. ' },',
  9650. ' set: function (v) {',
  9651. ' this.p[this.a] = v;',
  9652. ' }',
  9653. '});',
  9654. '$mod.DoIt($mod.GetArr(0)[2+1],$mod.GetArr(0)[2+2],{',
  9655. ' a: 2+3,',
  9656. ' p: $mod.GetArr(0),',
  9657. ' get: function () {',
  9658. ' return this.p[this.a];',
  9659. ' },',
  9660. ' set: function (v) {',
  9661. ' this.p[this.a] = v;',
  9662. ' }',
  9663. '});',
  9664. '$mod.DoIt($mod.GetArr(7)[3+1],$mod.GetArr(8)[3+2],{',
  9665. ' a: 3+3,',
  9666. ' p: $mod.GetArr(9),',
  9667. ' get: function () {',
  9668. ' return this.p[this.a];',
  9669. ' },',
  9670. ' set: function (v) {',
  9671. ' this.p[this.a] = v;',
  9672. ' }',
  9673. '});',
  9674. '']));
  9675. end;
  9676. procedure TTestModule.TestArrayEnumTypeRange;
  9677. begin
  9678. StartProgram(false);
  9679. Add([
  9680. 'type',
  9681. ' TEnum = (red,blue);',
  9682. ' TEnumArray = array[TEnum] of longint;',
  9683. 'var',
  9684. ' e: TEnum;',
  9685. ' i: longint;',
  9686. ' a: TEnumArray;',
  9687. ' numbers: TEnumArray = (1,2);',
  9688. ' names: array[TEnum] of string = (''red'',''blue'');',
  9689. 'begin',
  9690. ' e:=low(a);',
  9691. ' e:=high(a);',
  9692. ' i:=a[red];',
  9693. ' a[e]:=a[e];']);
  9694. ConvertProgram;
  9695. CheckSource('TestArrayEnumTypeRange',
  9696. LinesToStr([ // statements
  9697. ' this.TEnum = {',
  9698. ' "0": "red",',
  9699. ' red: 0,',
  9700. ' "1": "blue",',
  9701. ' blue: 1',
  9702. '};',
  9703. 'this.e = 0;',
  9704. 'this.i = 0;',
  9705. 'this.a = rtl.arraySetLength(null,0,2);',
  9706. 'this.numbers = [1, 2];',
  9707. 'this.names = ["red", "blue"];',
  9708. '']),
  9709. LinesToStr([ // $mod.$main
  9710. '$mod.e = $mod.TEnum.red;',
  9711. '$mod.e = $mod.TEnum.blue;',
  9712. '$mod.i = $mod.a[$mod.TEnum.red];',
  9713. '$mod.a[$mod.e] = $mod.a[$mod.e];',
  9714. '']));
  9715. end;
  9716. procedure TTestModule.TestArray_SetLengthOutArg;
  9717. begin
  9718. StartProgram(false);
  9719. Add([
  9720. 'type TArrInt = array of longint;',
  9721. 'procedure DoIt(out a: TArrInt);',
  9722. 'begin',
  9723. ' SetLength(a,2);',
  9724. 'end;',
  9725. 'begin',
  9726. '']);
  9727. ConvertProgram;
  9728. CheckSource('TestArray_SetLengthOutArg',
  9729. LinesToStr([ // statements
  9730. 'this.DoIt = function (a) {',
  9731. ' a.set(rtl.arraySetLength(a.get(), 0, 2));',
  9732. '};',
  9733. '']),
  9734. LinesToStr([
  9735. '']));
  9736. end;
  9737. procedure TTestModule.TestArray_SetLengthProperty;
  9738. begin
  9739. StartProgram(false);
  9740. Add('type');
  9741. Add(' TArrInt = array of longint;');
  9742. Add(' TObject = class');
  9743. Add(' function GetColors: TArrInt; external name ''GetColors'';');
  9744. Add(' procedure SetColors(const Value: TArrInt); external name ''SetColors'';');
  9745. Add(' property Colors: TArrInt read GetColors write SetColors;');
  9746. Add(' end;');
  9747. Add('var Obj: TObject;');
  9748. Add('begin');
  9749. Add(' SetLength(Obj.Colors,2);');
  9750. ConvertProgram;
  9751. CheckSource('TestArray_SetLengthProperty',
  9752. LinesToStr([ // statements
  9753. 'rtl.createClass(this, "TObject", null, function () {',
  9754. ' this.$init = function () {',
  9755. ' };',
  9756. ' this.$final = function () {',
  9757. ' };',
  9758. '});',
  9759. 'this.Obj = null;',
  9760. '']),
  9761. LinesToStr([
  9762. '$mod.Obj.SetColors(rtl.arraySetLength($mod.Obj.GetColors(), 0, 2));',
  9763. '']));
  9764. end;
  9765. procedure TTestModule.TestArray_SetLengthMultiDim;
  9766. begin
  9767. StartProgram(false);
  9768. Add([
  9769. 'type',
  9770. ' TArrArrInt = array of array of longint;',
  9771. ' TArrStaInt = array of array[1..2] of longint;',
  9772. 'var',
  9773. ' a: TArrArrInt;',
  9774. ' b: TArrStaInt;',
  9775. 'begin',
  9776. ' SetLength(a,2);',
  9777. ' SetLength(a,3,4);',
  9778. ' SetLength(b,5);',
  9779. '']);
  9780. ConvertProgram;
  9781. CheckSource('TestArray_SetLengthMultiDim',
  9782. LinesToStr([ // statements
  9783. 'this.a = [];',
  9784. 'this.b = [];',
  9785. '']),
  9786. LinesToStr([
  9787. '$mod.a = rtl.arraySetLength($mod.a, [], 2);',
  9788. '$mod.a = rtl.arraySetLength($mod.a, 0, 3, 4);',
  9789. '$mod.b = rtl.arraySetLength($mod.b, 0, 5, "s", 2);',
  9790. '']));
  9791. end;
  9792. procedure TTestModule.TestArray_SetLengthDynOfStatic;
  9793. begin
  9794. StartProgram(false);
  9795. Add([
  9796. 'type',
  9797. ' TStaArr1 = array[1..3] of boolean;',
  9798. //' TStaArr2 = array[5..6] of TStaArr1;',
  9799. ' TDynArr1StaArr1 = array of TStaArr1;',
  9800. //' TDynArr1StaArr2 = array of TStaArr2;',
  9801. ' TDynArr2StaArr1 = array of TDynArr1StaArr1;',
  9802. //' TDynArr2StaArr2 = array of TDynArr1StaArr2;',
  9803. 'var',
  9804. ' DynArr1StaArr1: TDynArr1StaArr1;',
  9805. //' DynArr1StaArr2: TDynArr1StaArr1;',
  9806. ' DynArr2StaArr1: TDynArr2StaArr1;',
  9807. //' DynArr2StaArr2: TDynArr2StaArr2;',
  9808. 'begin',
  9809. ' SetLength(DynArr1StaArr1,11);',
  9810. ' SetLength(DynArr2StaArr1,12);',
  9811. ' SetLength(DynArr2StaArr1[13],14);',
  9812. ' SetLength(DynArr2StaArr1,15,16);',
  9813. //' SetLength(DynArr1StaArr2,21);',
  9814. //' SetLength(DynArr2StaArr2,22);',
  9815. //' SetLength(DynArr2StaArr2[23],24);',
  9816. //' SetLength(DynArr2StaArr2,25,26);',
  9817. '']);
  9818. ConvertProgram;
  9819. CheckSource('TestArray_DynOfStatic',
  9820. LinesToStr([ // statements
  9821. 'this.DynArr1StaArr1 = [];',
  9822. 'this.DynArr2StaArr1 = [];',
  9823. '']),
  9824. LinesToStr([ // $mod.$main
  9825. '$mod.DynArr1StaArr1 = rtl.arraySetLength($mod.DynArr1StaArr1, false, 11, "s", 3);',
  9826. '$mod.DynArr2StaArr1 = rtl.arraySetLength($mod.DynArr2StaArr1, [], 12);',
  9827. '$mod.DynArr2StaArr1[13] = rtl.arraySetLength($mod.DynArr2StaArr1[13], false, 14, "s", 3);',
  9828. '$mod.DynArr2StaArr1 = rtl.arraySetLength(',
  9829. ' $mod.DynArr2StaArr1,',
  9830. ' false,',
  9831. ' 15,',
  9832. ' 16,',
  9833. ' "s",',
  9834. ' 3',
  9835. ');',
  9836. '']));
  9837. end;
  9838. procedure TTestModule.TestArray_OpenArrayOfString;
  9839. begin
  9840. StartProgram(false);
  9841. Add('procedure DoIt(const a: array of String);');
  9842. Add('var');
  9843. Add(' i: longint;');
  9844. Add(' s: string;');
  9845. Add('begin');
  9846. Add(' for i:=low(a) to high(a) do s:=a[length(a)-i-1];');
  9847. Add('end;');
  9848. Add('var s: string;');
  9849. Add('begin');
  9850. Add(' DoIt([]);');
  9851. Add(' DoIt([s,''foo'','''',s+s]);');
  9852. ConvertProgram;
  9853. CheckSource('TestArray_OpenArrayOfString',
  9854. LinesToStr([ // statements
  9855. 'this.DoIt = function (a) {',
  9856. ' var i = 0;',
  9857. ' var s = "";',
  9858. ' for (var $l = 0, $end = rtl.length(a) - 1; $l <= $end; $l++) {',
  9859. ' i = $l;',
  9860. ' s = a[rtl.length(a) - i - 1];',
  9861. ' };',
  9862. '};',
  9863. 'this.s = "";',
  9864. '']),
  9865. LinesToStr([
  9866. '$mod.DoIt([]);',
  9867. '$mod.DoIt([$mod.s, "foo", "", $mod.s + $mod.s]);',
  9868. '']));
  9869. end;
  9870. procedure TTestModule.TestArray_ArrayOfCharAssignString;
  9871. begin
  9872. StartProgram(false);
  9873. Add([
  9874. 'type TArr = array of char;',
  9875. 'var',
  9876. ' c: char;',
  9877. ' s: string;',
  9878. ' a: TArr;',
  9879. 'procedure Run(const a: array of char);',
  9880. 'begin',
  9881. ' Run(c);',
  9882. ' Run(s);',
  9883. 'end;',
  9884. 'begin',
  9885. ' a:=c;',
  9886. ' a:=s;',
  9887. ' a:=#13;',
  9888. ' a:=''Foo'';',
  9889. ' Run(c);',
  9890. ' Run(s);',
  9891. '']);
  9892. ConvertProgram;
  9893. CheckSource('TestArray_ArrayOfCharAssignString',
  9894. LinesToStr([ // statements
  9895. 'this.c = "";',
  9896. 'this.s = "";',
  9897. 'this.a = [];',
  9898. 'this.Run = function (a) {',
  9899. ' $mod.Run($mod.c.split(""));',
  9900. ' $mod.Run($mod.s.split(""));',
  9901. '};',
  9902. '']),
  9903. LinesToStr([
  9904. '$mod.a = $mod.c.split("");',
  9905. '$mod.a = $mod.s.split("");',
  9906. '$mod.a = "\r".split("");',
  9907. '$mod.a = "Foo".split("");',
  9908. '$mod.Run($mod.c.split(""));',
  9909. '$mod.Run($mod.s.split(""));',
  9910. '']));
  9911. end;
  9912. procedure TTestModule.TestArray_ConstRef;
  9913. begin
  9914. StartProgram(false);
  9915. Add([
  9916. 'type TArr = array of word;',
  9917. 'procedure Run(constref a: TArr);',
  9918. 'begin',
  9919. 'end;',
  9920. 'procedure Fly(a: TArr; var b: TArr; out c: TArr; const d: TArr; constref e: TArr);',
  9921. 'var l: TArr;',
  9922. 'begin',
  9923. ' Run(l);',
  9924. ' Run(a);',
  9925. ' Run(b);',
  9926. ' Run(c);',
  9927. ' Run(d);',
  9928. ' Run(e);',
  9929. 'end;',
  9930. 'begin',
  9931. '']);
  9932. ConvertProgram;
  9933. CheckResolverUnexpectedHints();
  9934. CheckSource('TestArray_ConstRef',
  9935. LinesToStr([ // statements
  9936. 'this.Run = function (a) {',
  9937. '};',
  9938. 'this.Fly = function (a, b, c, d, e) {',
  9939. ' var l = [];',
  9940. ' $mod.Run(l);',
  9941. ' $mod.Run(a);',
  9942. ' $mod.Run(b.get());',
  9943. ' $mod.Run(c.get());',
  9944. ' $mod.Run(d);',
  9945. ' $mod.Run(e);',
  9946. '};',
  9947. '']),
  9948. LinesToStr([
  9949. '']));
  9950. end;
  9951. procedure TTestModule.TestArray_Concat;
  9952. begin
  9953. StartProgram(false);
  9954. Add([
  9955. 'type',
  9956. ' integer = longint;',
  9957. ' TFlag = (big,small);',
  9958. ' TFlags = set of TFlag;',
  9959. ' TRec = record',
  9960. ' i: integer;',
  9961. ' end;',
  9962. ' TArrInt = array of integer;',
  9963. ' TArrRec = array of TRec;',
  9964. ' TArrFlag = array of TFlag;',
  9965. ' TArrSet = array of TFlags;',
  9966. ' TArrJSValue = array of jsvalue;',
  9967. 'var',
  9968. ' ArrInt: tarrint;',
  9969. ' ArrRec: tarrrec;',
  9970. ' ArrFlag: tarrflag;',
  9971. ' ArrSet: tarrset;',
  9972. ' ArrJSValue: tarrjsvalue;',
  9973. 'begin',
  9974. ' arrint:=concat(arrint);',
  9975. ' arrint:=concat(arrint,arrint);',
  9976. ' arrint:=concat(arrint,arrint,arrint);',
  9977. ' arrrec:=concat(arrrec);',
  9978. ' arrrec:=concat(arrrec,arrrec);',
  9979. ' arrrec:=concat(arrrec,arrrec,arrrec);',
  9980. ' arrset:=concat(arrset);',
  9981. ' arrset:=concat(arrset,arrset);',
  9982. ' arrset:=concat(arrset,arrset,arrset);',
  9983. ' arrjsvalue:=concat(arrjsvalue);',
  9984. ' arrjsvalue:=concat(arrjsvalue,arrjsvalue);',
  9985. ' arrjsvalue:=concat(arrjsvalue,arrjsvalue,arrjsvalue);',
  9986. ' arrint:=concat([1],arrint);',
  9987. ' arrflag:=concat([big]);',
  9988. ' arrflag:=concat([big],arrflag);',
  9989. ' arrflag:=concat(arrflag,[small]);',
  9990. '']);
  9991. ConvertProgram;
  9992. CheckSource('TestArray_Concat',
  9993. LinesToStr([ // statements
  9994. 'this.TFlag = {',
  9995. ' "0": "big",',
  9996. ' big: 0,',
  9997. ' "1": "small",',
  9998. ' small: 1',
  9999. '};',
  10000. 'rtl.recNewT(this, "TRec", function () {',
  10001. ' this.i = 0;',
  10002. ' this.$eq = function (b) {',
  10003. ' return this.i === b.i;',
  10004. ' };',
  10005. ' this.$assign = function (s) {',
  10006. ' this.i = s.i;',
  10007. ' return this;',
  10008. ' };',
  10009. '});',
  10010. 'this.ArrInt = [];',
  10011. 'this.ArrRec = [];',
  10012. 'this.ArrFlag = [];',
  10013. 'this.ArrSet = [];',
  10014. 'this.ArrJSValue = [];',
  10015. '']),
  10016. LinesToStr([ // $mod.$main
  10017. '$mod.ArrInt = rtl.arrayRef($mod.ArrInt);',
  10018. '$mod.ArrInt = rtl.arrayConcatN($mod.ArrInt, $mod.ArrInt);',
  10019. '$mod.ArrInt = rtl.arrayConcatN($mod.ArrInt, $mod.ArrInt, $mod.ArrInt);',
  10020. '$mod.ArrRec = rtl.arrayRef($mod.ArrRec);',
  10021. '$mod.ArrRec = rtl.arrayConcat($mod.TRec, $mod.ArrRec, $mod.ArrRec);',
  10022. '$mod.ArrRec = rtl.arrayConcat($mod.TRec, $mod.ArrRec, $mod.ArrRec, $mod.ArrRec);',
  10023. '$mod.ArrSet = rtl.arrayRef($mod.ArrSet);',
  10024. '$mod.ArrSet = rtl.arrayConcat("refSet", $mod.ArrSet, $mod.ArrSet);',
  10025. '$mod.ArrSet = rtl.arrayConcat("refSet", $mod.ArrSet, $mod.ArrSet, $mod.ArrSet);',
  10026. '$mod.ArrJSValue = rtl.arrayRef($mod.ArrJSValue);',
  10027. '$mod.ArrJSValue = rtl.arrayConcatN($mod.ArrJSValue, $mod.ArrJSValue);',
  10028. '$mod.ArrJSValue = rtl.arrayConcatN($mod.ArrJSValue, $mod.ArrJSValue, $mod.ArrJSValue);',
  10029. '$mod.ArrInt = rtl.arrayConcatN([1], $mod.ArrInt);',
  10030. '$mod.ArrFlag = [$mod.TFlag.big];',
  10031. '$mod.ArrFlag = rtl.arrayConcatN([$mod.TFlag.big], $mod.ArrFlag);',
  10032. '$mod.ArrFlag = rtl.arrayConcatN($mod.ArrFlag, [$mod.TFlag.small]);',
  10033. '']));
  10034. end;
  10035. procedure TTestModule.TestArray_Copy;
  10036. begin
  10037. StartProgram(false);
  10038. Add([
  10039. 'type',
  10040. ' integer = longint;',
  10041. ' TFlag = (big,small);',
  10042. ' TFlags = set of TFlag;',
  10043. ' TRec = record',
  10044. ' i: integer;',
  10045. ' end;',
  10046. ' TArrInt = array of integer;',
  10047. ' TArrRec = array of TRec;',
  10048. ' TArrSet = array of TFlags;',
  10049. ' TArrJSValue = array of jsvalue;',
  10050. 'var',
  10051. ' ArrInt: tarrint;',
  10052. ' ArrRec: tarrrec;',
  10053. ' ArrSet: tarrset;',
  10054. ' ArrJSValue: tarrjsvalue;',
  10055. 'begin',
  10056. ' arrint:=copy(arrint);',
  10057. ' arrint:=copy(arrint,2);',
  10058. ' arrint:=copy(arrint,3,4);',
  10059. ' arrint:=copy([1,1],1,2);',
  10060. ' arrrec:=copy(arrrec);',
  10061. ' arrrec:=copy(arrrec,5);',
  10062. ' arrrec:=copy(arrrec,6,7);',
  10063. ' arrset:=copy(arrset);',
  10064. ' arrset:=copy(arrset,8);',
  10065. ' arrset:=copy(arrset,9,10);',
  10066. ' arrjsvalue:=copy(arrjsvalue);',
  10067. ' arrjsvalue:=copy(arrjsvalue,11);',
  10068. ' arrjsvalue:=copy(arrjsvalue,12,13);',
  10069. ' ']);
  10070. ConvertProgram;
  10071. CheckSource('TestArray_Copy',
  10072. LinesToStr([ // statements
  10073. 'this.TFlag = {',
  10074. ' "0": "big",',
  10075. ' big: 0,',
  10076. ' "1": "small",',
  10077. ' small: 1',
  10078. '};',
  10079. 'rtl.recNewT(this, "TRec", function () {',
  10080. ' this.i = 0;',
  10081. ' this.$eq = function (b) {',
  10082. ' return this.i === b.i;',
  10083. ' };',
  10084. ' this.$assign = function (s) {',
  10085. ' this.i = s.i;',
  10086. ' return this;',
  10087. ' };',
  10088. '});',
  10089. 'this.ArrInt = [];',
  10090. 'this.ArrRec = [];',
  10091. 'this.ArrSet = [];',
  10092. 'this.ArrJSValue = [];',
  10093. '']),
  10094. LinesToStr([ // $mod.$main
  10095. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 0);',
  10096. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 2);',
  10097. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 3, 4);',
  10098. '$mod.ArrInt = rtl.arrayCopy(0, [1, 1], 1, 2);',
  10099. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 0);',
  10100. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 5);',
  10101. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 6, 7);',
  10102. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 0);',
  10103. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 8);',
  10104. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 9, 10);',
  10105. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 0);',
  10106. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 11);',
  10107. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 12, 13);',
  10108. '']));
  10109. end;
  10110. procedure TTestModule.TestArray_InsertDelete;
  10111. begin
  10112. StartProgram(false);
  10113. Add([
  10114. 'type',
  10115. ' integer = longint;',
  10116. ' TFlag = (big,small);',
  10117. ' TFlags = set of TFlag;',
  10118. ' TRec = record',
  10119. ' i: integer;',
  10120. ' end;',
  10121. ' TArrInt = array of integer;',
  10122. ' TArrRec = array of TRec;',
  10123. ' TArrSet = array of TFlags;',
  10124. ' TArrJSValue = array of jsvalue;',
  10125. ' TArrArrInt = array of TArrInt;',
  10126. 'var',
  10127. ' ArrInt: tarrint;',
  10128. ' ArrRec: tarrrec;',
  10129. ' ArrSet: tarrset;',
  10130. ' ArrJSValue: tarrjsvalue;',
  10131. ' ArrArrInt: TArrArrInt;',
  10132. 'begin',
  10133. ' Insert(1,arrint,2);',
  10134. ' Insert(arrint[3],arrint,4);',
  10135. ' Insert(arrrec[5],arrrec,6);',
  10136. ' Insert(arrset[7],arrset,7);',
  10137. ' Insert(arrjsvalue[8],arrjsvalue,9);',
  10138. ' Insert(10,arrjsvalue,11);',
  10139. ' Insert([23],arrarrint,22);',
  10140. ' Delete(arrint,12,13);',
  10141. ' Delete(arrrec,14,15);',
  10142. ' Delete(arrset,17,18);',
  10143. ' Delete(arrjsvalue,19,10);']);
  10144. ConvertProgram;
  10145. CheckSource('TestArray_InsertDelete',
  10146. LinesToStr([ // statements
  10147. 'this.TFlag = {',
  10148. ' "0": "big",',
  10149. ' big: 0,',
  10150. ' "1": "small",',
  10151. ' small: 1',
  10152. '};',
  10153. 'rtl.recNewT(this, "TRec", function () {',
  10154. ' this.i = 0;',
  10155. ' this.$eq = function (b) {',
  10156. ' return this.i === b.i;',
  10157. ' };',
  10158. ' this.$assign = function (s) {',
  10159. ' this.i = s.i;',
  10160. ' return this;',
  10161. ' };',
  10162. '});',
  10163. 'this.ArrInt = [];',
  10164. 'this.ArrRec = [];',
  10165. 'this.ArrSet = [];',
  10166. 'this.ArrJSValue = [];',
  10167. 'this.ArrArrInt = [];',
  10168. '']),
  10169. LinesToStr([ // $mod.$main
  10170. '$mod.ArrInt = rtl.arrayInsert(1, $mod.ArrInt, 2);',
  10171. '$mod.ArrInt = rtl.arrayInsert($mod.ArrInt[3], $mod.ArrInt, 4);',
  10172. '$mod.ArrRec = rtl.arrayInsert($mod.ArrRec[5], $mod.ArrRec, 6);',
  10173. '$mod.ArrSet = rtl.arrayInsert($mod.ArrSet[7], $mod.ArrSet, 7);',
  10174. '$mod.ArrJSValue = rtl.arrayInsert($mod.ArrJSValue[8], $mod.ArrJSValue, 9);',
  10175. '$mod.ArrJSValue = rtl.arrayInsert(10, $mod.ArrJSValue, 11);',
  10176. '$mod.ArrArrInt = rtl.arrayInsert([23], $mod.ArrArrInt, 22);',
  10177. '$mod.ArrInt.splice(12, 13);',
  10178. '$mod.ArrRec.splice(14, 15);',
  10179. '$mod.ArrSet.splice(17, 18);',
  10180. '$mod.ArrJSValue.splice(19, 10);',
  10181. '']));
  10182. end;
  10183. procedure TTestModule.TestArray_DynArrayConstObjFPC;
  10184. begin
  10185. Parser.Options:=Parser.Options+[po_cassignments];
  10186. StartProgram(false);
  10187. Add([
  10188. '{$modeswitch arrayoperators}',
  10189. 'type',
  10190. ' integer = longint;',
  10191. ' TArrInt = array of integer;',
  10192. ' TArrStr = array of string;',
  10193. 'const',
  10194. ' Ints: TArrInt = (1,2,3);',
  10195. ' Aliases: TarrStr = (''foo'',''b'');',
  10196. ' OneInt: TArrInt = (7);',
  10197. ' OneStr: array of integer = (7);',
  10198. ' Chars: array of char = ''aoc'';',
  10199. ' Names: array of string = (''a'',''foo'');',
  10200. ' NameCount = low(Names)+high(Names)+length(Names);',
  10201. 'var i: integer;',
  10202. 'begin',
  10203. ' Ints:=[];',
  10204. ' Ints:=[1,1];',
  10205. ' Ints:=[1]+[2];',
  10206. ' Ints:=[2];',
  10207. ' Ints:=[]+ints;',
  10208. ' Ints:=Ints+[];',
  10209. ' Ints:=Ints+OneInt;',
  10210. ' Ints:=Ints+[1,1];',
  10211. ' Ints:=[i,i]+Ints;',
  10212. ' Ints:=[1]+[i]+[3];',
  10213. '']);
  10214. ConvertProgram;
  10215. CheckSource('TestArray_DynArrayConstObjFPC',
  10216. LinesToStr([ // statements
  10217. 'this.Ints = [1, 2, 3];',
  10218. 'this.Aliases = ["foo", "b"];',
  10219. 'this.OneInt = [7];',
  10220. 'this.OneStr = [7];',
  10221. 'this.Chars = ["a", "o", "c"];',
  10222. 'this.Names = ["a", "foo"];',
  10223. 'this.NameCount = 0 + (rtl.length(this.Names) - 1) + rtl.length(this.Names);',
  10224. 'this.i = 0;',
  10225. '']),
  10226. LinesToStr([ // $mod.$main
  10227. '$mod.Ints = [];',
  10228. '$mod.Ints = [1, 1];',
  10229. '$mod.Ints = rtl.arrayConcatN([1], [2]);',
  10230. '$mod.Ints = [2];',
  10231. '$mod.Ints = rtl.arrayConcatN([], $mod.Ints);',
  10232. '$mod.Ints = rtl.arrayConcatN($mod.Ints, []);',
  10233. '$mod.Ints = rtl.arrayConcatN($mod.Ints, $mod.OneInt);',
  10234. '$mod.Ints = rtl.arrayConcatN($mod.Ints, [1, 1]);',
  10235. '$mod.Ints = rtl.arrayConcatN([$mod.i, $mod.i], $mod.Ints);',
  10236. '$mod.Ints = rtl.arrayConcatN(rtl.arrayConcatN([1], [$mod.i]), [3]);',
  10237. '']));
  10238. end;
  10239. procedure TTestModule.TestArray_DynArrayConstDelphi;
  10240. begin
  10241. StartProgram(false);
  10242. // Note: const c = [1,1]; defines a set!
  10243. Add([
  10244. '{$mode delphi}',
  10245. 'type',
  10246. ' integer = longint;',
  10247. ' TArrInt = array of integer;',
  10248. ' TArrStr = array of string;',
  10249. 'const',
  10250. ' Ints: TArrInt = [1,1,2];',
  10251. ' Aliases: TarrStr = [''foo'',''b''];',
  10252. ' OneInt: TArrInt = [7];',
  10253. ' OneStr: array of integer = [7]+[8];',
  10254. ' Chars: array of char = ''aoc'';',
  10255. ' Names: array of string = [''a'',''a''];',
  10256. ' NameCount = low(Names)+high(Names)+length(Names);',
  10257. 'begin',
  10258. '']);
  10259. ConvertProgram;
  10260. CheckSource('TestArray_DynArrayConstDelphi',
  10261. LinesToStr([ // statements
  10262. 'this.Ints = [1, 1, 2];',
  10263. 'this.Aliases = ["foo", "b"];',
  10264. 'this.OneInt = [7];',
  10265. 'this.OneStr = rtl.arrayConcatN([7],[8]);',
  10266. 'this.Chars = ["a", "o", "c"];',
  10267. 'this.Names = ["a", "a"];',
  10268. 'this.NameCount = 0 + (rtl.length(this.Names) - 1) + rtl.length(this.Names);',
  10269. '']),
  10270. LinesToStr([ // $mod.$main
  10271. '']));
  10272. end;
  10273. procedure TTestModule.TestArray_ArrayLitAsParam;
  10274. begin
  10275. StartProgram(false);
  10276. Add([
  10277. '{$modeswitch arrayoperators}',
  10278. 'type',
  10279. ' integer = longint;',
  10280. ' TArrInt = array of integer;',
  10281. ' TArrSet = array of (red,green,blue);',
  10282. 'procedure DoOpenInt(const a: array of integer); forward;',
  10283. 'procedure DoInt(const a: TArrInt);',
  10284. 'begin',
  10285. ' DoInt(a+[1]);',
  10286. ' DoInt([1]+a);',
  10287. ' DoOpenInt(a);',
  10288. ' DoOpenInt(a+[1]);',
  10289. ' DoOpenInt([1]+a);',
  10290. 'end;',
  10291. 'procedure DoOpenInt(const a: array of integer);',
  10292. 'begin',
  10293. ' DoOpenInt(a+[1]);',
  10294. ' DoOpenInt([1]+a);',
  10295. ' DoInt(a);',
  10296. ' DoInt(a+[1]);',
  10297. ' DoInt([1]+a);',
  10298. 'end;',
  10299. 'procedure DoSet(const a: TArrSet);',
  10300. 'begin',
  10301. ' DoSet(a+[red]);',
  10302. ' DoSet([blue]+a);',
  10303. 'end;',
  10304. 'var',
  10305. ' i: TArrInt;',
  10306. ' s: TArrSet;',
  10307. 'begin',
  10308. ' DoInt([1]);',
  10309. ' DoInt([1]+[2]);',
  10310. ' DoInt(i+[1]);',
  10311. ' DoInt([1]+i);',
  10312. ' DoOpenInt([1]);',
  10313. ' DoOpenInt([1]+[2]);',
  10314. ' DoOpenInt(i+[1]);',
  10315. ' DoOpenInt([1]+i);',
  10316. ' DoSet([red]);',
  10317. ' DoSet([blue]+[green]);',
  10318. ' DoSet(s+[blue]);',
  10319. ' DoSet([red]+s);',
  10320. '']);
  10321. ConvertProgram;
  10322. CheckSource('TestArray_ArrayLitAsParam',
  10323. LinesToStr([ // statements
  10324. 'this.TArrSet$a = {',
  10325. ' "0": "red",',
  10326. ' red: 0,',
  10327. ' "1": "green",',
  10328. ' green: 1,',
  10329. ' "2": "blue",',
  10330. ' blue: 2',
  10331. '};',
  10332. 'this.DoInt = function (a) {',
  10333. ' $mod.DoInt(rtl.arrayConcatN(a, [1]));',
  10334. ' $mod.DoInt(rtl.arrayConcatN([1], a));',
  10335. ' $mod.DoOpenInt(a);',
  10336. ' $mod.DoOpenInt(rtl.arrayConcatN(a, [1]));',
  10337. ' $mod.DoOpenInt(rtl.arrayConcatN([1], a));',
  10338. '};',
  10339. 'this.DoOpenInt = function (a) {',
  10340. ' $mod.DoOpenInt(rtl.arrayConcatN(a, [1]));',
  10341. ' $mod.DoOpenInt(rtl.arrayConcatN([1], a));',
  10342. ' $mod.DoInt(a);',
  10343. ' $mod.DoInt(rtl.arrayConcatN(a, [1]));',
  10344. ' $mod.DoInt(rtl.arrayConcatN([1], a));',
  10345. '};',
  10346. 'this.DoSet = function (a) {',
  10347. ' $mod.DoSet(rtl.arrayConcatN(a, [$mod.TArrSet$a.red]));',
  10348. ' $mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.blue], a));',
  10349. '};',
  10350. 'this.i = [];',
  10351. 'this.s = [];',
  10352. '']),
  10353. LinesToStr([ // $mod.$main
  10354. '$mod.DoInt([1]);',
  10355. '$mod.DoInt(rtl.arrayConcatN([1], [2]));',
  10356. '$mod.DoInt(rtl.arrayConcatN($mod.i, [1]));',
  10357. '$mod.DoInt(rtl.arrayConcatN([1], $mod.i));',
  10358. '$mod.DoOpenInt([1]);',
  10359. '$mod.DoOpenInt(rtl.arrayConcatN([1], [2]));',
  10360. '$mod.DoOpenInt(rtl.arrayConcatN($mod.i, [1]));',
  10361. '$mod.DoOpenInt(rtl.arrayConcatN([1], $mod.i));',
  10362. '$mod.DoSet([$mod.TArrSet$a.red]);',
  10363. '$mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.blue], [$mod.TArrSet$a.green]));',
  10364. '$mod.DoSet(rtl.arrayConcatN($mod.s, [$mod.TArrSet$a.blue]));',
  10365. '$mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.red], $mod.s));',
  10366. '']));
  10367. end;
  10368. procedure TTestModule.TestArray_ArrayLitMultiDimAsParam;
  10369. begin
  10370. StartProgram(false);
  10371. Add([
  10372. '{$modeswitch arrayoperators}',
  10373. 'type',
  10374. ' integer = longint;',
  10375. ' TArrInt = array of integer;',
  10376. ' TArrArrInt = array of TArrInt;',
  10377. 'procedure DoInt(const a: TArrArrInt);',
  10378. 'begin',
  10379. ' DoInt(a+[[1]]);',
  10380. ' DoInt([[1]]+a);',
  10381. ' DoInt(a);',
  10382. 'end;',
  10383. 'var',
  10384. ' i: TArrInt;',
  10385. ' a: TArrArrInt;',
  10386. 'begin',
  10387. ' a:=[[1]];',
  10388. ' a:=[i];',
  10389. ' a:=a+[i];',
  10390. ' a:=[i]+a;',
  10391. ' a:=[[1]+i];',
  10392. ' a:=[[1]+[2]];',
  10393. ' a:=[i+[2]];',
  10394. ' DoInt([[1]]);',
  10395. ' DoInt([[1]+[2],[3,4],[5]]);',
  10396. ' DoInt([i+[1]]+a);',
  10397. ' DoInt([i]+a);',
  10398. '']);
  10399. ConvertProgram;
  10400. CheckSource('TestArray_ArrayLitMultiDimAsParam',
  10401. LinesToStr([ // statements
  10402. 'this.DoInt = function (a) {',
  10403. ' $mod.DoInt(rtl.arrayConcatN(a, [[1]]));',
  10404. ' $mod.DoInt(rtl.arrayConcatN([[1]], a));',
  10405. ' $mod.DoInt(a);',
  10406. '};',
  10407. 'this.i = [];',
  10408. 'this.a = [];',
  10409. '']),
  10410. LinesToStr([ // $mod.$main
  10411. '$mod.a = [[1]];',
  10412. '$mod.a = [$mod.i];',
  10413. '$mod.a = rtl.arrayConcatN($mod.a, [$mod.i]);',
  10414. '$mod.a = rtl.arrayConcatN([$mod.i], $mod.a);',
  10415. '$mod.a = [rtl.arrayConcatN([1], $mod.i)];',
  10416. '$mod.a = [rtl.arrayConcatN([1], [2])];',
  10417. '$mod.a = [rtl.arrayConcatN($mod.i, [2])];',
  10418. '$mod.DoInt([[1]]);',
  10419. '$mod.DoInt([rtl.arrayConcatN([1], [2]), [3, 4], [5]]);',
  10420. '$mod.DoInt(rtl.arrayConcatN([rtl.arrayConcatN($mod.i, [1])], $mod.a));',
  10421. '$mod.DoInt(rtl.arrayConcatN([$mod.i], $mod.a));',
  10422. '']));
  10423. end;
  10424. procedure TTestModule.TestArray_ArrayLitStaticAsParam;
  10425. begin
  10426. StartProgram(false);
  10427. Add([
  10428. '{$modeswitch arrayoperators}',
  10429. 'type',
  10430. ' integer = longint;',
  10431. ' TArrInt = array[1..2] of integer;',
  10432. ' TArrArrInt = array of TArrInt;',
  10433. 'procedure DoInt(const a: TArrArrInt);',
  10434. 'begin',
  10435. ' DoInt(a+[[1,2]]);',
  10436. ' DoInt([[1,2]]+a);',
  10437. ' DoInt(a);',
  10438. 'end;',
  10439. 'var',
  10440. ' i: TArrInt;',
  10441. ' a: TArrArrInt;',
  10442. 'begin',
  10443. ' a:=[[1,1]];',
  10444. ' a:=[i];',
  10445. ' a:=a+[i];',
  10446. ' a:=[i]+a;',
  10447. ' DoInt([[1,1]]);',
  10448. ' DoInt([[1,2],[3,4]]);',
  10449. '']);
  10450. ConvertProgram;
  10451. CheckSource('TestArray_ArrayLitStaticAsParam',
  10452. LinesToStr([ // statements
  10453. 'this.DoInt = function (a) {',
  10454. ' $mod.DoInt(rtl.arrayConcatN(a, [[1, 2]]));',
  10455. ' $mod.DoInt(rtl.arrayConcatN([[1, 2]], a));',
  10456. ' $mod.DoInt(a);',
  10457. '};',
  10458. 'this.i = rtl.arraySetLength(null, 0, 2);',
  10459. 'this.a = [];',
  10460. '']),
  10461. LinesToStr([ // $mod.$main
  10462. '$mod.a = [[1, 1]];',
  10463. '$mod.a = [$mod.i.slice(0)];',
  10464. '$mod.a = rtl.arrayConcatN($mod.a, [$mod.i.slice(0)]);',
  10465. '$mod.a = rtl.arrayConcatN([$mod.i.slice(0)], $mod.a);',
  10466. '$mod.DoInt([[1, 1]]);',
  10467. '$mod.DoInt([[1, 2], [3, 4]]);',
  10468. '']));
  10469. end;
  10470. procedure TTestModule.TestArray_ForInArrOfString;
  10471. begin
  10472. StartProgram(false);
  10473. Add([
  10474. 'type',
  10475. 'type',
  10476. ' TMonthNameArray = array [1..12] of string;',
  10477. ' TMonthNames = TMonthNameArray;',
  10478. ' TObject = class',
  10479. ' private',
  10480. ' function GetLongMonthNames: TMonthNames; virtual; abstract;',
  10481. ' public',
  10482. ' Property LongMonthNames : TMonthNames Read GetLongMonthNames;',
  10483. ' end;',
  10484. 'var',
  10485. ' f: TObject;',
  10486. ' Month: string;',
  10487. ' Names: array of string = (''a'',''foo'',''bar'');',
  10488. ' i: longint;',
  10489. 'begin',
  10490. ' for Month in f.LongMonthNames do ;',
  10491. ' for Month in Names do ;',
  10492. ' for i:=low(Names) to high(Names) do ;',
  10493. '']);
  10494. ConvertProgram;
  10495. CheckSource('TestArray_ForInArrOfString',
  10496. LinesToStr([ // statements
  10497. 'rtl.createClass(this, "TObject", null, function () {',
  10498. ' this.$init = function () {',
  10499. ' };',
  10500. ' this.$final = function () {',
  10501. ' };',
  10502. '});',
  10503. 'this.f = null;',
  10504. 'this.Month = "";',
  10505. 'this.Names = ["a", "foo", "bar"];',
  10506. 'this.i = 0;',
  10507. '']),
  10508. LinesToStr([ // $mod.$main
  10509. 'for (var $in = $mod.f.GetLongMonthNames(), $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) $mod.Month = $in[$l];',
  10510. 'for (var $in1 = $mod.Names, $l1 = 0, $end1 = rtl.length($in1) - 1; $l1 <= $end1; $l1++) $mod.Month = $in1[$l1];',
  10511. 'for (var $l2 = 0, $end2 = rtl.length($mod.Names) - 1; $l2 <= $end2; $l2++) $mod.i = $l2;',
  10512. '']));
  10513. end;
  10514. procedure TTestModule.TestExternalClass_TypeCastArrayToExternalClass;
  10515. begin
  10516. StartProgram(false);
  10517. Add([
  10518. '{$modeswitch externalclass}',
  10519. 'type',
  10520. ' TJSObject = class external name ''Object''',
  10521. ' end;',
  10522. ' TJSArray = class external name ''Array''',
  10523. ' class function isArray(Value: JSValue) : boolean;',
  10524. ' function concat() : TJSArray; varargs;',
  10525. ' end;',
  10526. 'var',
  10527. ' aObj: TJSArray;',
  10528. ' a: array of longint;',
  10529. ' o: TJSObject;',
  10530. 'begin',
  10531. ' if TJSArray.isArray(65) then ;',
  10532. ' aObj:=TJSArray(a).concat(a);',
  10533. ' o:=TJSObject(a);']);
  10534. ConvertProgram;
  10535. CheckSource('TestExternalClass_TypeCastArrayToExternalClass',
  10536. LinesToStr([ // statements
  10537. 'this.aObj = null;',
  10538. 'this.a = [];',
  10539. 'this.o = null;',
  10540. '']),
  10541. LinesToStr([ // $mod.$main
  10542. 'if (Array.isArray(65)) ;',
  10543. '$mod.aObj = $mod.a.concat($mod.a);',
  10544. '$mod.o = $mod.a;',
  10545. '']));
  10546. end;
  10547. procedure TTestModule.TestExternalClass_TypeCastArrayFromExternalClass;
  10548. begin
  10549. StartProgram(false);
  10550. Add([
  10551. '{$modeswitch externalclass}',
  10552. 'type',
  10553. ' TArrStr = array of string;',
  10554. ' TJSArray = class external name ''Array''',
  10555. ' end;',
  10556. ' TJSObject = class external name ''Object''',
  10557. ' end;',
  10558. 'var',
  10559. ' aObj: TJSArray;',
  10560. ' a: TArrStr;',
  10561. ' jo: TJSObject;',
  10562. 'begin',
  10563. ' a:=TArrStr(aObj);',
  10564. ' TArrStr(aObj)[1]:=TArrStr(aObj)[2];',
  10565. ' a:=TarrStr(jo);',
  10566. '']);
  10567. ConvertProgram;
  10568. CheckSource('TestExternalClass_TypeCastArrayFromExternalClass',
  10569. LinesToStr([ // statements
  10570. 'this.aObj = null;',
  10571. 'this.a = [];',
  10572. 'this.jo = null;',
  10573. '']),
  10574. LinesToStr([ // $mod.$main
  10575. '$mod.a = $mod.aObj;',
  10576. '$mod.aObj[1] = $mod.aObj[2];',
  10577. '$mod.a = $mod.jo;',
  10578. '']));
  10579. end;
  10580. procedure TTestModule.TestArrayOfConst_TVarRec;
  10581. begin
  10582. StartProgram(true,[supTVarRec]);
  10583. Add([
  10584. 'procedure Say(args: array of const);',
  10585. 'var',
  10586. ' i: longint;',
  10587. ' v: TVarRec;',
  10588. 'begin',
  10589. ' for i:=low(args) to high(args) do begin',
  10590. ' v:=args[i];',
  10591. ' case v.vtype of',
  10592. ' vtInteger: if length(args)=args[i].vInteger then ;',
  10593. ' end;',
  10594. ' end;',
  10595. ' for v in args do ;',
  10596. ' args:=nil;',
  10597. ' SetLength(args,2);',
  10598. 'end;',
  10599. 'begin']);
  10600. ConvertProgram;
  10601. CheckSource('TestArrayOfConst_TVarRec',
  10602. LinesToStr([ // statements
  10603. 'this.Say = function (args) {',
  10604. ' var i = 0;',
  10605. ' var v = pas.system.TVarRec.$new();',
  10606. ' for (var $l = 0, $end = rtl.length(args) - 1; $l <= $end; $l++) {',
  10607. ' i = $l;',
  10608. ' v.$assign(args[i]);',
  10609. ' var $tmp = v.VType;',
  10610. ' if ($tmp === 0) if (rtl.length(args) === args[i].VJSValue) ;',
  10611. ' };',
  10612. ' for (var $in = args, $l1 = 0, $end1 = rtl.length($in) - 1; $l1 <= $end1; $l1++) v = $in[$l1];',
  10613. ' args = [];',
  10614. ' args = rtl.arraySetLength(args, pas.system.TVarRec, 2);',
  10615. '};',
  10616. '']),
  10617. LinesToStr([ // $mod.$main
  10618. ]));
  10619. end;
  10620. procedure TTestModule.TestArrayOfConst_PassBaseTypes;
  10621. begin
  10622. StartProgram(true,[supTVarRec]);
  10623. Add([
  10624. 'procedure Say(args: array of const);',
  10625. 'begin',
  10626. ' Say(args);',
  10627. 'end;',
  10628. 'var',
  10629. ' p: Pointer;',
  10630. ' j: jsvalue;',
  10631. ' c: currency;',
  10632. 'begin',
  10633. ' Say([]);',
  10634. ' Say([1]);',
  10635. ' Say([''c'',''foo'',nil,true,1.3,p,j,c]);',
  10636. '']);
  10637. ConvertProgram;
  10638. CheckSource('TestArrayOfConst_PassBaseTypes',
  10639. LinesToStr([ // statements
  10640. 'this.Say = function (args) {',
  10641. ' $mod.Say(args);',
  10642. '};',
  10643. 'this.p = null;',
  10644. 'this.j = undefined;',
  10645. 'this.c = 0;',
  10646. '']),
  10647. LinesToStr([ // $mod.$main
  10648. '$mod.Say([]);',
  10649. '$mod.Say(pas.system.VarRecs(0, 1));',
  10650. '$mod.Say(pas.system.VarRecs(',
  10651. ' 9,',
  10652. ' "c",',
  10653. ' 18,',
  10654. ' "foo",',
  10655. ' 5,',
  10656. ' null,',
  10657. ' 1,',
  10658. ' true,',
  10659. ' 3,',
  10660. ' 1.3,',
  10661. ' 5,',
  10662. ' $mod.p,',
  10663. ' 20,',
  10664. ' $mod.j,',
  10665. ' 12,',
  10666. ' $mod.c',
  10667. ' ));',
  10668. '']));
  10669. end;
  10670. procedure TTestModule.TestArrayOfConst_PassObj;
  10671. begin
  10672. StartProgram(true,[supTVarRec]);
  10673. Add([
  10674. '{$interfaces corba}',
  10675. 'type',
  10676. ' TObject = class',
  10677. ' end;',
  10678. ' TClass = class of TObject;',
  10679. ' IUnknown = interface',
  10680. ' end;',
  10681. 'procedure Say(args: array of const);',
  10682. 'begin',
  10683. 'end;',
  10684. 'var',
  10685. ' o: TObject;',
  10686. ' c: TClass;',
  10687. ' i: IUnknown;',
  10688. 'begin',
  10689. ' Say([o,c,TObject]);',
  10690. ' Say([nil,i]);',
  10691. '']);
  10692. ConvertProgram;
  10693. CheckSource('TestArrayOfConst_PassObj',
  10694. LinesToStr([ // statements
  10695. 'rtl.createClass(this, "TObject", null, function () {',
  10696. ' this.$init = function () {',
  10697. ' };',
  10698. ' this.$final = function () {',
  10699. ' };',
  10700. '});',
  10701. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  10702. 'this.Say = function (args) {',
  10703. '};',
  10704. 'this.o = null;',
  10705. 'this.c = null;',
  10706. 'this.i = null;',
  10707. '']),
  10708. LinesToStr([ // $mod.$main
  10709. '$mod.Say(pas.system.VarRecs(',
  10710. ' 7,',
  10711. ' $mod.o,',
  10712. ' 8,',
  10713. ' $mod.c,',
  10714. ' 8,',
  10715. ' $mod.TObject',
  10716. '));',
  10717. '$mod.Say(pas.system.VarRecs(5, null, 14, $mod.i));',
  10718. '']));
  10719. end;
  10720. procedure TTestModule.TestRecord_Empty;
  10721. begin
  10722. StartProgram(false);
  10723. Add([
  10724. 'type',
  10725. ' TRecA = record',
  10726. ' end;',
  10727. 'var a,b: TRecA;',
  10728. 'begin',
  10729. ' if a=b then ;']);
  10730. ConvertProgram;
  10731. CheckSource('TestRecord_Empty',
  10732. LinesToStr([ // statements
  10733. 'rtl.recNewT(this, "TRecA", function () {',
  10734. ' this.$eq = function (b) {',
  10735. ' return true;',
  10736. ' };',
  10737. ' this.$assign = function (s) {',
  10738. ' return this;',
  10739. ' };',
  10740. '});',
  10741. 'this.a = this.TRecA.$new();',
  10742. 'this.b = this.TRecA.$new();',
  10743. '']),
  10744. LinesToStr([ // $mod.$main
  10745. 'if ($mod.a.$eq($mod.b)) ;'
  10746. ]));
  10747. end;
  10748. procedure TTestModule.TestRecord_Var;
  10749. begin
  10750. StartProgram(false);
  10751. Add('type');
  10752. Add(' TRecA = record');
  10753. Add(' Bold: longint;');
  10754. Add(' end;');
  10755. Add('var Rec: TRecA;');
  10756. Add('begin');
  10757. Add(' rec.bold:=123');
  10758. ConvertProgram;
  10759. CheckSource('TestRecord_Var',
  10760. LinesToStr([ // statements
  10761. 'rtl.recNewT(this, "TRecA", function () {',
  10762. ' this.Bold = 0;',
  10763. ' this.$eq = function (b) {',
  10764. ' return this.Bold === b.Bold;',
  10765. ' };',
  10766. ' this.$assign = function (s) {',
  10767. ' this.Bold = s.Bold;',
  10768. ' return this;',
  10769. ' };',
  10770. '});',
  10771. 'this.Rec = this.TRecA.$new();',
  10772. '']),
  10773. LinesToStr([ // $mod.$main
  10774. '$mod.Rec.Bold = 123;'
  10775. ]));
  10776. end;
  10777. procedure TTestModule.TestRecord_VarExternal;
  10778. begin
  10779. StartProgram(false);
  10780. Add([
  10781. '{$modeswitch externalclass}',
  10782. 'type',
  10783. ' TRecA = record',
  10784. ' i: byte;',
  10785. ' length_: longint external name ''length'';',
  10786. ' end;',
  10787. 'var Rec: TRecA;',
  10788. 'begin',
  10789. ' rec.length_ := rec.length_',
  10790. '']);
  10791. ConvertProgram;
  10792. CheckSource('TestRecord_VarExternal',
  10793. LinesToStr([ // statements
  10794. 'rtl.recNewT(this, "TRecA", function () {',
  10795. ' this.i = 0;',
  10796. ' this.$eq = function (b) {',
  10797. ' return (this.i === b.i) && (this.length === b.length);',
  10798. ' };',
  10799. ' this.$assign = function (s) {',
  10800. ' this.i = s.i;',
  10801. ' this.length = s.length;',
  10802. ' return this;',
  10803. ' };',
  10804. '});',
  10805. 'this.Rec = this.TRecA.$new();',
  10806. '']),
  10807. LinesToStr([ // $mod.$main
  10808. '$mod.Rec.length = $mod.Rec.length;'
  10809. ]));
  10810. end;
  10811. procedure TTestModule.TestRecord_WithDo;
  10812. begin
  10813. StartProgram(false);
  10814. Add('type');
  10815. Add(' TRec = record');
  10816. Add(' vI: longint;');
  10817. Add(' end;');
  10818. Add('var');
  10819. Add(' Int: longint;');
  10820. Add(' r: TRec;');
  10821. Add('begin');
  10822. Add(' with r do');
  10823. Add(' int:=vi;');
  10824. Add(' with r do begin');
  10825. Add(' int:=vi;');
  10826. Add(' vi:=int;');
  10827. Add(' end;');
  10828. ConvertProgram;
  10829. CheckSource('TestWithRecordDo',
  10830. LinesToStr([ // statements
  10831. 'rtl.recNewT(this, "TRec", function () {',
  10832. ' this.vI = 0;',
  10833. ' this.$eq = function (b) {',
  10834. ' return this.vI === b.vI;',
  10835. ' };',
  10836. ' this.$assign = function (s) {',
  10837. ' this.vI = s.vI;',
  10838. ' return this;',
  10839. ' };',
  10840. '});',
  10841. 'this.Int = 0;',
  10842. 'this.r = this.TRec.$new();',
  10843. '']),
  10844. LinesToStr([ // $mod.$main
  10845. 'var $with = $mod.r;',
  10846. '$mod.Int = $with.vI;',
  10847. 'var $with1 = $mod.r;',
  10848. '$mod.Int = $with1.vI;',
  10849. '$with1.vI = $mod.Int;'
  10850. ]));
  10851. end;
  10852. procedure TTestModule.TestRecord_Assign;
  10853. begin
  10854. StartProgram(false);
  10855. Add([
  10856. 'type',
  10857. ' TEnum = (red,green);',
  10858. ' TEnums = set of TEnum;',
  10859. ' TSmallRec = record',
  10860. ' N: longint;',
  10861. ' end;',
  10862. ' TBigRec = record',
  10863. ' Int: longint;',
  10864. ' D: double;',
  10865. ' Arr: array of longint;',
  10866. ' Arr2: array[1..2] of longint;',
  10867. ' Small: TSmallRec;',
  10868. ' Enums: TEnums;',
  10869. ' end;',
  10870. 'var',
  10871. ' r, s: TBigRec;',
  10872. 'begin',
  10873. ' r:=s;',
  10874. ' r:=default(TBigRec);',
  10875. ' r:=default(s);',
  10876. '']);
  10877. ConvertProgram;
  10878. CheckSource('TestRecord_Assign',
  10879. LinesToStr([ // statements
  10880. 'this.TEnum = {',
  10881. ' "0": "red",',
  10882. ' red: 0,',
  10883. ' "1": "green",',
  10884. ' green: 1',
  10885. '};',
  10886. 'rtl.recNewT(this, "TSmallRec", function () {',
  10887. ' this.N = 0;',
  10888. ' this.$eq = function (b) {',
  10889. ' return this.N === b.N;',
  10890. ' };',
  10891. ' this.$assign = function (s) {',
  10892. ' this.N = s.N;',
  10893. ' return this;',
  10894. ' };',
  10895. '});',
  10896. 'rtl.recNewT(this, "TBigRec", function () {',
  10897. ' this.Int = 0;',
  10898. ' this.D = 0.0;',
  10899. ' this.$new = function () {',
  10900. ' var r = Object.create(this);',
  10901. ' r.Arr = [];',
  10902. ' r.Arr2 = rtl.arraySetLength(null, 0, 2);',
  10903. ' r.Small = $mod.TSmallRec.$new();',
  10904. ' r.Enums = {};',
  10905. ' return r;',
  10906. ' };',
  10907. ' this.$eq = function (b) {',
  10908. ' return (this.Int === b.Int) && (this.D === b.D) && (this.Arr === b.Arr) && rtl.arrayEq(this.Arr2, b.Arr2) && this.Small.$eq(b.Small) && rtl.eqSet(this.Enums, b.Enums);',
  10909. ' };',
  10910. ' this.$assign = function (s) {',
  10911. ' this.Int = s.Int;',
  10912. ' this.D = s.D;',
  10913. ' this.Arr = rtl.arrayRef(s.Arr);',
  10914. ' this.Arr2 = s.Arr2.slice(0);',
  10915. ' this.Small.$assign(s.Small);',
  10916. ' this.Enums = rtl.refSet(s.Enums);',
  10917. ' return this;',
  10918. ' };',
  10919. '});',
  10920. 'this.r = this.TBigRec.$new();',
  10921. 'this.s = this.TBigRec.$new();',
  10922. '']),
  10923. LinesToStr([ // $mod.$main
  10924. '$mod.r.$assign($mod.s);',
  10925. '$mod.r.$assign($mod.TBigRec.$new());',
  10926. '$mod.r.$assign($mod.TBigRec.$new());',
  10927. '']));
  10928. end;
  10929. procedure TTestModule.TestRecord_AsParams;
  10930. begin
  10931. StartProgram(false);
  10932. Add([
  10933. 'type',
  10934. ' integer = longint;',
  10935. ' TRecord = record',
  10936. ' i: integer;',
  10937. ' end;',
  10938. 'procedure DoIt(vD: TRecord; const vC: TRecord; var vV: TRecord; var U);',
  10939. 'var vL: TRecord;',
  10940. 'begin',
  10941. ' vd:=vd;',
  10942. ' vd.i:=vd.i;',
  10943. ' vl:=vc;',
  10944. ' vv:=vv;',
  10945. ' vv.i:=vv.i;',
  10946. ' U:=vl;',
  10947. ' U:=vd;',
  10948. ' U:=vc;',
  10949. ' U:=vv;',
  10950. ' vl:=TRecord(U);',
  10951. ' vd:=TRecord(U);',
  10952. ' vv:=TRecord(U);',
  10953. ' doit(vd,vd,vd,vd);',
  10954. ' doit(vc,vc,vl,vl);',
  10955. ' doit(vv,vv,vv,vv);',
  10956. ' doit(vl,vl,vl,vl);',
  10957. ' TRecord(U).i:=3;',
  10958. 'end;',
  10959. 'var i: TRecord;',
  10960. 'begin',
  10961. ' doit(i,i,i,i);',
  10962. '']);
  10963. ConvertProgram;
  10964. CheckSource('TestRecord_AsParams',
  10965. LinesToStr([ // statements
  10966. 'rtl.recNewT(this, "TRecord", function () {',
  10967. ' this.i = 0;',
  10968. ' this.$eq = function (b) {',
  10969. ' return this.i === b.i;',
  10970. ' };',
  10971. ' this.$assign = function (s) {',
  10972. ' this.i = s.i;',
  10973. ' return this;',
  10974. ' };',
  10975. '});',
  10976. 'this.DoIt = function (vD, vC, vV, U) {',
  10977. ' var vL = $mod.TRecord.$new();',
  10978. ' vD.$assign(vD);',
  10979. ' vD.i = vD.i;',
  10980. ' vL.$assign(vC);',
  10981. ' vV.$assign(vV);',
  10982. ' vV.i = vV.i;',
  10983. ' U.$assign(vL);',
  10984. ' U.$assign(vD);',
  10985. ' U.$assign(vC);',
  10986. ' U.$assign(vV);',
  10987. ' vL.$assign(U);',
  10988. ' vD.$assign(U);',
  10989. ' vV.$assign(U);',
  10990. ' $mod.DoIt($mod.TRecord.$clone(vD), vD, vD, vD);',
  10991. ' $mod.DoIt($mod.TRecord.$clone(vC), vC, vL, vL);',
  10992. ' $mod.DoIt($mod.TRecord.$clone(vV), vV, vV, vV);',
  10993. ' $mod.DoIt($mod.TRecord.$clone(vL), vL, vL, vL);',
  10994. ' U.i = 3;',
  10995. '};',
  10996. 'this.i = this.TRecord.$new();'
  10997. ]),
  10998. LinesToStr([
  10999. '$mod.DoIt($mod.TRecord.$clone($mod.i), $mod.i, $mod.i, $mod.i);',
  11000. '']));
  11001. end;
  11002. procedure TTestModule.TestRecord_ConstRef;
  11003. begin
  11004. StartProgram(false);
  11005. Add([
  11006. 'type TRec = record i: word; end;',
  11007. 'procedure Run(constref a: TRec);',
  11008. 'begin',
  11009. 'end;',
  11010. 'procedure Fly(a: TRec; var b: TRec; out c: TRec; const d: TRec; constref e: TRec);',
  11011. 'var l: TRec;',
  11012. 'begin',
  11013. ' Run(l);',
  11014. ' Run(a);',
  11015. ' Run(b);',
  11016. ' Run(c);',
  11017. ' Run(d);',
  11018. ' Run(e);',
  11019. 'end;',
  11020. 'begin',
  11021. '']);
  11022. ConvertProgram;
  11023. CheckResolverUnexpectedHints();
  11024. CheckSource('TestRecord_ConstRef',
  11025. LinesToStr([ // statements
  11026. 'rtl.recNewT(this, "TRec", function () {',
  11027. ' this.i = 0;',
  11028. ' this.$eq = function (b) {',
  11029. ' return this.i === b.i;',
  11030. ' };',
  11031. ' this.$assign = function (s) {',
  11032. ' this.i = s.i;',
  11033. ' return this;',
  11034. ' };',
  11035. '});',
  11036. 'this.Run = function (a) {',
  11037. '};',
  11038. 'this.Fly = function (a, b, c, d, e) {',
  11039. ' var l = $mod.TRec.$new();',
  11040. ' $mod.Run(l);',
  11041. ' $mod.Run(a);',
  11042. ' $mod.Run(b);',
  11043. ' $mod.Run(c);',
  11044. ' $mod.Run(d);',
  11045. ' $mod.Run(e);',
  11046. '};',
  11047. '']),
  11048. LinesToStr([
  11049. '']));
  11050. end;
  11051. procedure TTestModule.TestRecordElement_AsParams;
  11052. begin
  11053. StartProgram(false);
  11054. Add('type');
  11055. Add(' integer = longint;');
  11056. Add(' TRecord = record');
  11057. Add(' i: integer;');
  11058. Add(' end;');
  11059. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  11060. Add('var vJ: TRecord;');
  11061. Add('begin');
  11062. Add(' doit(vj.i,vj.i,vj.i);');
  11063. Add('end;');
  11064. Add('var r: TRecord;');
  11065. Add('begin');
  11066. Add(' doit(r.i,r.i,r.i);');
  11067. ConvertProgram;
  11068. CheckSource('TestRecordElement_AsParams',
  11069. LinesToStr([ // statements
  11070. 'rtl.recNewT(this, "TRecord", function () {',
  11071. ' this.i = 0;',
  11072. ' this.$eq = function (b) {',
  11073. ' return this.i === b.i;',
  11074. ' };',
  11075. ' this.$assign = function (s) {',
  11076. ' this.i = s.i;',
  11077. ' return this;',
  11078. ' };',
  11079. '});',
  11080. 'this.DoIt = function (vG,vH,vI) {',
  11081. ' var vJ = $mod.TRecord.$new();',
  11082. ' $mod.DoIt(vJ.i, vJ.i, {',
  11083. ' p: vJ,',
  11084. ' get: function () {',
  11085. ' return this.p.i;',
  11086. ' },',
  11087. ' set: function (v) {',
  11088. ' this.p.i = v;',
  11089. ' }',
  11090. ' });',
  11091. '};',
  11092. 'this.r = this.TRecord.$new();'
  11093. ]),
  11094. LinesToStr([
  11095. '$mod.DoIt($mod.r.i,$mod.r.i,{',
  11096. ' p: $mod.r,',
  11097. ' get: function () {',
  11098. ' return this.p.i;',
  11099. ' },',
  11100. ' set: function (v) {',
  11101. ' this.p.i = v;',
  11102. ' }',
  11103. '});'
  11104. ]));
  11105. end;
  11106. procedure TTestModule.TestRecordElementFromFuncResult_AsParams;
  11107. begin
  11108. StartProgram(false);
  11109. Add('type');
  11110. Add(' integer = longint;');
  11111. Add(' TRecord = record');
  11112. Add(' i: integer;');
  11113. Add(' end;');
  11114. Add('function GetRec(vB: integer = 0): TRecord;');
  11115. Add('begin');
  11116. Add('end;');
  11117. Add('procedure DoIt(vG: integer; const vH: integer);');
  11118. Add('begin');
  11119. Add('end;');
  11120. Add('begin');
  11121. Add(' doit(getrec.i,getrec.i);');
  11122. Add(' doit(getrec().i,getrec().i);');
  11123. Add(' doit(getrec(1).i,getrec(2).i);');
  11124. ConvertProgram;
  11125. CheckSource('TestRecordElementFromFuncResult_AsParams',
  11126. LinesToStr([ // statements
  11127. 'rtl.recNewT(this, "TRecord", function () {',
  11128. ' this.i = 0;',
  11129. ' this.$eq = function (b) {',
  11130. ' return this.i === b.i;',
  11131. ' };',
  11132. ' this.$assign = function (s) {',
  11133. ' this.i = s.i;',
  11134. ' return this;',
  11135. ' };',
  11136. '});',
  11137. 'this.GetRec = function (vB) {',
  11138. ' var Result = $mod.TRecord.$new();',
  11139. ' return Result;',
  11140. '};',
  11141. 'this.DoIt = function (vG, vH) {',
  11142. '};',
  11143. '']),
  11144. LinesToStr([
  11145. '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
  11146. '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
  11147. '$mod.DoIt($mod.GetRec(1).i,$mod.GetRec(2).i);',
  11148. '']));
  11149. end;
  11150. procedure TTestModule.TestRecordElementFromWith_AsParams;
  11151. begin
  11152. StartProgram(false);
  11153. Add('type');
  11154. Add(' integer = longint;');
  11155. Add(' TRecord = record');
  11156. Add(' i: integer;');
  11157. Add(' end;');
  11158. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  11159. Add('begin');
  11160. Add('end;');
  11161. Add('var r: trecord;');
  11162. Add('begin');
  11163. Add(' with r do ');
  11164. Add(' doit(i,i,i);');
  11165. ConvertProgram;
  11166. CheckSource('TestRecordElementFromWith_AsParams',
  11167. LinesToStr([ // statements
  11168. 'rtl.recNewT(this, "TRecord", function () {',
  11169. ' this.i = 0;',
  11170. ' this.$eq = function (b) {',
  11171. ' return this.i === b.i;',
  11172. ' };',
  11173. ' this.$assign = function (s) {',
  11174. ' this.i = s.i;',
  11175. ' return this;',
  11176. ' };',
  11177. '});',
  11178. 'this.DoIt = function (vG,vH,vI) {',
  11179. '};',
  11180. 'this.r = this.TRecord.$new();'
  11181. ]),
  11182. LinesToStr([
  11183. 'var $with = $mod.r;',
  11184. '$mod.DoIt($with.i,$with.i,{',
  11185. ' p: $with,',
  11186. ' get: function () {',
  11187. ' return this.p.i;',
  11188. ' },',
  11189. ' set: function (v) {',
  11190. ' this.p.i = v;',
  11191. ' }',
  11192. '});',
  11193. '']));
  11194. end;
  11195. procedure TTestModule.TestRecord_Equal;
  11196. begin
  11197. StartProgram(false);
  11198. Add('type');
  11199. Add(' integer = longint;');
  11200. Add(' TFlag = (red,blue);');
  11201. Add(' TFlags = set of TFlag;');
  11202. Add(' TProc = procedure;');
  11203. Add(' TRecord = record');
  11204. Add(' i: integer;');
  11205. Add(' Event: TProc;');
  11206. Add(' f: TFlags;');
  11207. Add(' end;');
  11208. Add(' TNested = record');
  11209. Add(' r: TRecord;');
  11210. Add(' end;');
  11211. Add('var');
  11212. Add(' b: boolean;');
  11213. Add(' r,s: trecord;');
  11214. Add('begin');
  11215. Add(' b:=r=s;');
  11216. Add(' b:=r<>s;');
  11217. ConvertProgram;
  11218. CheckSource('TestRecord_Equal',
  11219. LinesToStr([ // statements
  11220. 'this.TFlag = {',
  11221. ' "0": "red",',
  11222. ' red: 0,',
  11223. ' "1": "blue",',
  11224. ' blue: 1',
  11225. '};',
  11226. 'rtl.recNewT(this, "TRecord", function () {',
  11227. ' this.i = 0;',
  11228. ' this.Event = null;',
  11229. ' this.$new = function () {',
  11230. ' var r = Object.create(this);',
  11231. ' r.f = {};',
  11232. ' return r;',
  11233. ' };',
  11234. ' this.$eq = function (b) {',
  11235. ' return (this.i === b.i) && rtl.eqCallback(this.Event, b.Event) && rtl.eqSet(this.f, b.f);',
  11236. ' };',
  11237. ' this.$assign = function (s) {',
  11238. ' this.i = s.i;',
  11239. ' this.Event = s.Event;',
  11240. ' this.f = rtl.refSet(s.f);',
  11241. ' return this;',
  11242. ' };',
  11243. '});',
  11244. 'rtl.recNewT(this, "TNested", function () {',
  11245. ' this.$new = function () {',
  11246. ' var r = Object.create(this);',
  11247. ' r.r = $mod.TRecord.$new();',
  11248. ' return r;',
  11249. ' };',
  11250. ' this.$eq = function (b) {',
  11251. ' return this.r.$eq(b.r);',
  11252. ' };',
  11253. ' this.$assign = function (s) {',
  11254. ' this.r.$assign(s.r);',
  11255. ' return this;',
  11256. ' };',
  11257. '});',
  11258. 'this.b = false;',
  11259. 'this.r = this.TRecord.$new();',
  11260. 'this.s = this.TRecord.$new();',
  11261. '']),
  11262. LinesToStr([
  11263. '$mod.b = $mod.r.$eq($mod.s);',
  11264. '$mod.b = !$mod.r.$eq($mod.s);',
  11265. '']));
  11266. end;
  11267. procedure TTestModule.TestRecord_JSValue;
  11268. begin
  11269. StartProgram(false);
  11270. Add([
  11271. 'type',
  11272. ' TRecord = record',
  11273. ' i: longint;',
  11274. ' end;',
  11275. 'procedure Fly(d: jsvalue; const c: jsvalue);',
  11276. 'begin',
  11277. 'end;',
  11278. 'procedure Run(d: TRecord; const c: TRecord; var v: TRecord);',
  11279. 'begin',
  11280. ' if jsvalue(d) then ;',
  11281. ' if jsvalue(c) then ;',
  11282. ' if jsvalue(v) then ;',
  11283. 'end;',
  11284. 'var',
  11285. ' Jv: jsvalue;',
  11286. ' Rec: trecord;',
  11287. 'begin',
  11288. ' rec:=trecord(jv);',
  11289. ' jv:=rec;',
  11290. ' Fly(rec,rec);',
  11291. ' Fly(@rec,@rec);',
  11292. ' if jsvalue(Rec) then ;',
  11293. ' Run(trecord(jv),trecord(jv),rec);',
  11294. '']);
  11295. ConvertProgram;
  11296. CheckSource('TestRecord_JSValue',
  11297. LinesToStr([ // statements
  11298. 'rtl.recNewT(this, "TRecord", function () {',
  11299. ' this.i = 0;',
  11300. ' this.$eq = function (b) {',
  11301. ' return this.i === b.i;',
  11302. ' };',
  11303. ' this.$assign = function (s) {',
  11304. ' this.i = s.i;',
  11305. ' return this;',
  11306. ' };',
  11307. '});',
  11308. 'this.Fly = function (d, c) {',
  11309. '};',
  11310. 'this.Run = function (d, c, v) {',
  11311. ' if (d) ;',
  11312. ' if (c) ;',
  11313. ' if (v) ;',
  11314. '};',
  11315. 'this.Jv = undefined;',
  11316. 'this.Rec = this.TRecord.$new();',
  11317. '']),
  11318. LinesToStr([
  11319. '$mod.Rec.$assign(rtl.getObject($mod.Jv));',
  11320. '$mod.Jv = $mod.Rec;',
  11321. '$mod.Fly($mod.TRecord.$clone($mod.Rec), $mod.Rec);',
  11322. '$mod.Fly($mod.Rec, $mod.Rec);',
  11323. 'if ($mod.Rec) ;',
  11324. '$mod.Run($mod.TRecord.$clone(rtl.getObject($mod.Jv)), rtl.getObject($mod.Jv), $mod.Rec);',
  11325. '']));
  11326. end;
  11327. procedure TTestModule.TestRecord_VariantFail;
  11328. begin
  11329. StartProgram(false);
  11330. Add([
  11331. 'type',
  11332. ' TRec = record',
  11333. ' case word of',
  11334. ' 0: (b0, b1: Byte);',
  11335. ' 1: (i: word);',
  11336. ' end;',
  11337. 'begin']);
  11338. SetExpectedPasResolverError('variant record is not supported',
  11339. nXIsNotSupported);
  11340. ConvertProgram;
  11341. end;
  11342. procedure TTestModule.TestRecord_FieldArray;
  11343. begin
  11344. StartProgram(false);
  11345. Add([
  11346. 'type',
  11347. ' TArrInt = array[3..4] of longint;',
  11348. ' TArrArrInt = array[3..4] of longint;',
  11349. ' TRec = record',
  11350. ' a: array of longint;',
  11351. ' s: array[1..2] of longint;',
  11352. ' m: array[1..2,3..4] of longint;',
  11353. ' o: TArrArrInt;',
  11354. ' end;',
  11355. 'begin']);
  11356. ConvertProgram;
  11357. CheckSource('TestRecord_FieldArray',
  11358. LinesToStr([ // statements
  11359. 'rtl.recNewT(this, "TRec", function () {',
  11360. ' this.$new = function () {',
  11361. ' var r = Object.create(this);',
  11362. ' r.a = [];',
  11363. ' r.s = rtl.arraySetLength(null, 0, 2);',
  11364. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  11365. ' r.o = rtl.arraySetLength(null, 0, 2);',
  11366. ' return r;',
  11367. ' };',
  11368. ' this.$eq = function (b) {',
  11369. ' return (this.a === b.a) && rtl.arrayEq(this.s, b.s) && rtl.arrayEq(this.m, b.m) && rtl.arrayEq(this.o, b.o);',
  11370. ' };',
  11371. ' this.$assign = function (s) {',
  11372. ' this.a = rtl.arrayRef(s.a);',
  11373. ' this.s = s.s.slice(0);',
  11374. ' this.m = s.m.slice(0);',
  11375. ' this.o = s.o.slice(0);',
  11376. ' return this;',
  11377. ' };',
  11378. '});',
  11379. '']),
  11380. LinesToStr([ // $mod.$main
  11381. '']));
  11382. end;
  11383. procedure TTestModule.TestRecord_Const;
  11384. begin
  11385. StartProgram(false);
  11386. Add([
  11387. 'type',
  11388. ' TArrInt = array[3..4] of longint;',
  11389. ' TPoint = record x,y: longint; end;',
  11390. ' TRec = record',
  11391. ' i: longint;',
  11392. ' a: array of longint;',
  11393. ' s: array[1..2] of longint;',
  11394. ' m: array[1..2,3..4] of longint;',
  11395. ' p: TPoint;',
  11396. ' end;',
  11397. ' TPoints = array of TPoint;',
  11398. 'const',
  11399. ' r: TRec = (',
  11400. ' i:1;',
  11401. ' a:(2,3);',
  11402. ' s:(4,5);',
  11403. ' m:( (11,12), (13,14) );',
  11404. ' p: (x:21; y:22)',
  11405. ' );',
  11406. ' p: TPoints = ( (x:1;y:2), (x:3;y:4) );',
  11407. 'begin']);
  11408. ConvertProgram;
  11409. CheckSource('TestRecord_Const',
  11410. LinesToStr([ // statements
  11411. 'rtl.recNewT(this, "TPoint", function () {',
  11412. ' this.x = 0;',
  11413. ' this.y = 0;',
  11414. ' this.$eq = function (b) {',
  11415. ' return (this.x === b.x) && (this.y === b.y);',
  11416. ' };',
  11417. ' this.$assign = function (s) {',
  11418. ' this.x = s.x;',
  11419. ' this.y = s.y;',
  11420. ' return this;',
  11421. ' };',
  11422. '});',
  11423. 'rtl.recNewT(this, "TRec", function () {',
  11424. ' this.i = 0;',
  11425. ' this.$new = function () {',
  11426. ' var r = Object.create(this);',
  11427. ' r.a = [];',
  11428. ' r.s = rtl.arraySetLength(null, 0, 2);',
  11429. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  11430. ' r.p = $mod.TPoint.$new();',
  11431. ' return r;',
  11432. ' };',
  11433. ' this.$eq = function (b) {',
  11434. ' return (this.i === b.i) && (this.a === b.a) && rtl.arrayEq(this.s, b.s) && rtl.arrayEq(this.m, b.m) && this.p.$eq(b.p);',
  11435. ' };',
  11436. ' this.$assign = function (s) {',
  11437. ' this.i = s.i;',
  11438. ' this.a = rtl.arrayRef(s.a);',
  11439. ' this.s = s.s.slice(0);',
  11440. ' this.m = s.m.slice(0);',
  11441. ' this.p.$assign(s.p);',
  11442. ' return this;',
  11443. ' };',
  11444. '});',
  11445. 'this.r = this.TRec.$clone({',
  11446. ' i: 1,',
  11447. ' a: [2, 3],',
  11448. ' s: [4, 5],',
  11449. ' m: [[11, 12], [13, 14]],',
  11450. ' p: this.TPoint.$clone({',
  11451. ' x: 21,',
  11452. ' y: 22',
  11453. ' })',
  11454. '});',
  11455. 'this.p = [this.TPoint.$clone({',
  11456. ' x: 1,',
  11457. ' y: 2',
  11458. '}), this.TPoint.$clone({',
  11459. ' x: 3,',
  11460. ' y: 4',
  11461. '})];',
  11462. '']),
  11463. LinesToStr([ // $mod.$main
  11464. '']));
  11465. end;
  11466. procedure TTestModule.TestRecord_TypecastFail;
  11467. begin
  11468. StartProgram(false);
  11469. Add([
  11470. 'type',
  11471. ' TPoint = record x,y: longint; end;',
  11472. ' TRec = record l: longint end;',
  11473. 'var p: TPoint;',
  11474. 'begin',
  11475. ' if TRec(p).l=2 then ;']);
  11476. SetExpectedPasResolverError('Illegal type conversion: "TPoint" to "record TRec"',
  11477. nIllegalTypeConversionTo);
  11478. ConvertProgram;
  11479. end;
  11480. procedure TTestModule.TestRecord_InFunction;
  11481. begin
  11482. StartProgram(false);
  11483. Add([
  11484. 'var TPoint: longint = 3;',
  11485. 'procedure DoIt;',
  11486. 'type',
  11487. ' TPoint = record x,y: longint; end;',
  11488. ' TPoints = array of TPoint;',
  11489. 'var',
  11490. ' r: TPoint;',
  11491. ' p: TPoints;',
  11492. 'begin',
  11493. ' SetLength(p,2);',
  11494. 'end;',
  11495. 'begin']);
  11496. ConvertProgram;
  11497. CheckSource('TestRecord_InFunction',
  11498. LinesToStr([ // statements
  11499. 'this.TPoint = 3;',
  11500. 'var TPoint$1 = rtl.recNewT(null, "", function () {',
  11501. ' this.x = 0;',
  11502. ' this.y = 0;',
  11503. ' this.$eq = function (b) {',
  11504. ' return (this.x === b.x) && (this.y === b.y);',
  11505. ' };',
  11506. ' this.$assign = function (s) {',
  11507. ' this.x = s.x;',
  11508. ' this.y = s.y;',
  11509. ' return this;',
  11510. ' };',
  11511. '});',
  11512. 'this.DoIt = function () {',
  11513. ' var r = TPoint$1.$new();',
  11514. ' var p = [];',
  11515. ' p = rtl.arraySetLength(p, TPoint$1, 2);',
  11516. '};',
  11517. '']),
  11518. LinesToStr([ // $mod.$main
  11519. '']));
  11520. end;
  11521. procedure TTestModule.TestRecord_AnonymousFail;
  11522. begin
  11523. StartProgram(false);
  11524. Add([
  11525. 'var',
  11526. ' r: record x: word end;',
  11527. 'begin']);
  11528. SetExpectedPasResolverError('not yet implemented: :TPasRecordType [20190408224556] "anonymous record type"',
  11529. nNotYetImplemented);
  11530. ConvertProgram;
  11531. end;
  11532. procedure TTestModule.TestAdvRecord_Function;
  11533. begin
  11534. StartProgram(false);
  11535. Parser.Options:=Parser.Options+[po_cassignments];
  11536. Add([
  11537. '{$modeswitch AdvancedRecords}',
  11538. 'type',
  11539. ' TPoint = record',
  11540. ' x,y: word;',
  11541. ' function Add(const apt: TPoint): TPoint;',
  11542. ' end;',
  11543. 'function TPoint.Add(const apt: TPoint): TPoint;',
  11544. 'begin',
  11545. ' Result:=Self;',
  11546. ' Result.x+=apt.x;',
  11547. ' Result.y:=Result.y+apt.y;',
  11548. ' Self:=apt;',
  11549. 'end;',
  11550. 'var p,q: TPoint;',
  11551. 'begin',
  11552. ' p.add(q);',
  11553. ' p:=default(TPoint);',
  11554. ' p:=q;',
  11555. '']);
  11556. ConvertProgram;
  11557. CheckSource('TestAdvRecord_Function',
  11558. LinesToStr([ // statements
  11559. 'rtl.recNewT(this, "TPoint", function () {',
  11560. ' this.x = 0;',
  11561. ' this.y = 0;',
  11562. ' this.$eq = function (b) {',
  11563. ' return (this.x === b.x) && (this.y === b.y);',
  11564. ' };',
  11565. ' this.$assign = function (s) {',
  11566. ' this.x = s.x;',
  11567. ' this.y = s.y;',
  11568. ' return this;',
  11569. ' };',
  11570. ' this.Add = function (apt) {',
  11571. ' var Result = $mod.TPoint.$new();',
  11572. ' Result.$assign(this);',
  11573. ' Result.x += apt.x;',
  11574. ' Result.y = Result.y + apt.y;',
  11575. ' this.$assign(apt);',
  11576. ' return Result;',
  11577. ' };',
  11578. '});',
  11579. 'this.p = this.TPoint.$new();',
  11580. 'this.q = this.TPoint.$new();',
  11581. '']),
  11582. LinesToStr([ // $mod.$main
  11583. '$mod.p.Add($mod.q);',
  11584. '$mod.p.$assign($mod.TPoint.$new());',
  11585. '$mod.p.$assign($mod.q);',
  11586. '']));
  11587. end;
  11588. procedure TTestModule.TestAdvRecord_Property;
  11589. begin
  11590. StartProgram(false);
  11591. Add([
  11592. '{$modeswitch AdvancedRecords}',
  11593. 'type',
  11594. ' TPoint = record',
  11595. ' x,y: word;',
  11596. ' strict private',
  11597. ' function GetSize: longword;',
  11598. ' procedure SetSize(Value: longword);',
  11599. ' public',
  11600. ' property Size: longword read GetSize write SetSize;',
  11601. ' property Left: word read x write y;',
  11602. ' end;',
  11603. 'procedure SetSize(Value: longword); begin end;',// check auto rename
  11604. 'function TPoint.GetSize: longword;',
  11605. 'begin',
  11606. ' x:=y;',
  11607. ' Size:=Size;',
  11608. ' Left:=Left;',
  11609. 'end;',
  11610. 'procedure TPoint.SetSize(Value: longword);',
  11611. 'begin',
  11612. 'end;',
  11613. 'var p,q: TPoint;',
  11614. 'begin',
  11615. ' p.Size:=q.Size;',
  11616. ' p.Left:=q.Left;',
  11617. '']);
  11618. ConvertProgram;
  11619. CheckSource('TestAdvRecord_Property',
  11620. LinesToStr([ // statements
  11621. 'rtl.recNewT(this, "TPoint", function () {',
  11622. ' this.x = 0;',
  11623. ' this.y = 0;',
  11624. ' this.$eq = function (b) {',
  11625. ' return (this.x === b.x) && (this.y === b.y);',
  11626. ' };',
  11627. ' this.$assign = function (s) {',
  11628. ' this.x = s.x;',
  11629. ' this.y = s.y;',
  11630. ' return this;',
  11631. ' };',
  11632. ' this.GetSize = function () {',
  11633. ' var Result = 0;',
  11634. ' this.x = this.y;',
  11635. ' this.SetSize(this.GetSize());',
  11636. ' this.y = this.x;',
  11637. ' return Result;',
  11638. ' };',
  11639. ' this.SetSize = function (Value) {',
  11640. ' };',
  11641. '});',
  11642. 'this.SetSize = function (Value) {',
  11643. '};',
  11644. 'this.p = this.TPoint.$new();',
  11645. 'this.q = this.TPoint.$new();',
  11646. '']),
  11647. LinesToStr([ // $mod.$main
  11648. '$mod.p.SetSize($mod.q.GetSize());',
  11649. '$mod.p.y = $mod.q.x;',
  11650. '']));
  11651. end;
  11652. procedure TTestModule.TestAdvRecord_PropertyDefault;
  11653. begin
  11654. StartProgram(false);
  11655. Add([
  11656. '{$modeswitch AdvancedRecords}',
  11657. 'type',
  11658. ' TPoint = record',
  11659. ' strict private',
  11660. ' function GetItems(Index: word): word;',
  11661. ' procedure SetItems(Index: word; Value: word);',
  11662. ' public',
  11663. ' property Items[Index: word]: word read GetItems write SetItems; default;',
  11664. ' end;',
  11665. 'function TPoint.GetItems(Index: word): word;',
  11666. 'begin',
  11667. ' Items[index]:=Items[index];',
  11668. ' self.Items[index]:=self.Items[index];',
  11669. 'end;',
  11670. 'procedure TPoint.SetItems(Index: word; Value: word);',
  11671. 'begin',
  11672. 'end;',
  11673. 'var p: TPoint;',
  11674. 'begin',
  11675. ' p[1]:=p[2];',
  11676. ' p.Items[3]:=p.Items[4];',
  11677. '']);
  11678. ConvertProgram;
  11679. CheckSource('TestAdvRecord_PropertyDefault',
  11680. LinesToStr([ // statements
  11681. 'rtl.recNewT(this, "TPoint", function () {',
  11682. ' this.$eq = function (b) {',
  11683. ' return true;',
  11684. ' };',
  11685. ' this.$assign = function (s) {',
  11686. ' return this;',
  11687. ' };',
  11688. ' this.GetItems = function (Index) {',
  11689. ' var Result = 0;',
  11690. ' this.SetItems(Index, this.GetItems(Index));',
  11691. ' this.SetItems(Index, this.GetItems(Index));',
  11692. ' return Result;',
  11693. ' };',
  11694. ' this.SetItems = function (Index, Value) {',
  11695. ' };',
  11696. '});',
  11697. 'this.p = this.TPoint.$new();',
  11698. '']),
  11699. LinesToStr([ // $mod.$main
  11700. '$mod.p.SetItems(1, $mod.p.GetItems(2));',
  11701. '$mod.p.SetItems(3, $mod.p.GetItems(4));',
  11702. '']));
  11703. end;
  11704. procedure TTestModule.TestAdvRecord_Property_ClassMethod;
  11705. begin
  11706. StartProgram(false);
  11707. Add([
  11708. '{$modeswitch AdvancedRecords}',
  11709. 'type',
  11710. ' TRec = record',
  11711. ' class var',
  11712. ' Fx: longint;',
  11713. ' Fy: longint;',
  11714. ' class function GetInt: longint; static;',
  11715. ' class procedure SetInt(Value: longint); static;',
  11716. ' class procedure DoIt; static;',
  11717. ' class property IntA: longint read Fx write Fy;',
  11718. ' class property IntB: longint read GetInt write SetInt;',
  11719. ' end;',
  11720. 'class function trec.getint: longint;',
  11721. 'begin',
  11722. ' result:=fx;',
  11723. 'end;',
  11724. 'class procedure trec.setint(value: longint);',
  11725. 'begin',
  11726. 'end;',
  11727. 'class procedure trec.doit;',
  11728. 'begin',
  11729. ' IntA:=IntA+1;',
  11730. ' IntB:=IntB+1;',
  11731. 'end;',
  11732. 'var r: trec;',
  11733. 'begin',
  11734. ' trec.inta:=trec.inta+1;',
  11735. ' if trec.intb=2 then;',
  11736. ' trec.intb:=trec.intb+2;',
  11737. ' trec.setint(trec.inta);',
  11738. ' r.inta:=r.inta+1;',
  11739. ' if r.intb=2 then;',
  11740. ' r.intb:=r.intb+2;',
  11741. ' r.setint(r.inta);']);
  11742. ConvertProgram;
  11743. CheckSource('TestAdvRecord_Property_ClassMethod',
  11744. LinesToStr([ // statements
  11745. 'rtl.recNewT(this, "TRec", function () {',
  11746. ' this.Fx = 0;',
  11747. ' this.Fy = 0;',
  11748. ' this.$eq = function (b) {',
  11749. ' return true;',
  11750. ' };',
  11751. ' this.$assign = function (s) {',
  11752. ' return this;',
  11753. ' };',
  11754. ' this.GetInt = function () {',
  11755. ' var Result = 0;',
  11756. ' Result = $mod.TRec.Fx;',
  11757. ' return Result;',
  11758. ' };',
  11759. ' this.SetInt = function (Value) {',
  11760. ' };',
  11761. ' this.DoIt = function () {',
  11762. ' $mod.TRec.Fy = $mod.TRec.Fx + 1;',
  11763. ' $mod.TRec.SetInt($mod.TRec.GetInt() + 1);',
  11764. ' };',
  11765. '}, true);',
  11766. 'this.r = this.TRec.$new();',
  11767. '']),
  11768. LinesToStr([ // $mod.$main
  11769. '$mod.TRec.Fy = $mod.TRec.Fx + 1;',
  11770. 'if ($mod.TRec.GetInt() === 2) ;',
  11771. '$mod.TRec.SetInt($mod.TRec.GetInt() + 2);',
  11772. '$mod.TRec.SetInt($mod.TRec.Fx);',
  11773. '$mod.TRec.Fy = $mod.r.Fx + 1;',
  11774. 'if ($mod.TRec.GetInt() === 2) ;',
  11775. '$mod.TRec.SetInt($mod.TRec.GetInt() + 2);',
  11776. '$mod.TRec.SetInt($mod.r.Fx);',
  11777. '']));
  11778. end;
  11779. procedure TTestModule.TestAdvRecord_Const;
  11780. begin
  11781. StartProgram(false);
  11782. Add([
  11783. '{$modeswitch AdvancedRecords}',
  11784. 'type',
  11785. ' TArrInt = array[3..4] of longint;',
  11786. ' TPoint = record',
  11787. ' x,y: longint;',
  11788. ' class var Count: nativeint;',
  11789. ' end;',
  11790. ' TRec = record',
  11791. ' i: longint;',
  11792. ' a: array of longint;',
  11793. ' s: array[1..2] of longint;',
  11794. ' m: array[1..2,3..4] of longint;',
  11795. ' p: TPoint;',
  11796. ' end;',
  11797. ' TPoints = array of TPoint;',
  11798. 'const',
  11799. ' r: TRec = (',
  11800. ' i:1;',
  11801. ' a:(2,3);',
  11802. ' s:(4,5);',
  11803. ' m:( (11,12), (13,14) );',
  11804. ' p: (x:21)',
  11805. ' );',
  11806. ' p: TPoints = ( (x:1;y:2), (x:3;y:4) );',
  11807. 'begin']);
  11808. ConvertProgram;
  11809. CheckSource('TestAdvRecord_Const',
  11810. LinesToStr([ // statements
  11811. 'rtl.recNewT(this, "TPoint", function () {',
  11812. ' this.x = 0;',
  11813. ' this.y = 0;',
  11814. ' this.Count = 0;',
  11815. ' this.$eq = function (b) {',
  11816. ' return (this.x === b.x) && (this.y === b.y);',
  11817. ' };',
  11818. ' this.$assign = function (s) {',
  11819. ' this.x = s.x;',
  11820. ' this.y = s.y;',
  11821. ' return this;',
  11822. ' };',
  11823. '}, true);',
  11824. 'rtl.recNewT(this, "TRec", function () {',
  11825. ' this.i = 0;',
  11826. ' this.$new = function () {',
  11827. ' var r = Object.create(this);',
  11828. ' r.a = [];',
  11829. ' r.s = rtl.arraySetLength(null, 0, 2);',
  11830. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  11831. ' r.p = $mod.TPoint.$new();',
  11832. ' return r;',
  11833. ' };',
  11834. ' this.$eq = function (b) {',
  11835. ' return (this.i === b.i) && (this.a === b.a) && rtl.arrayEq(this.s, b.s) && rtl.arrayEq(this.m, b.m) && this.p.$eq(b.p);',
  11836. ' };',
  11837. ' this.$assign = function (s) {',
  11838. ' this.i = s.i;',
  11839. ' this.a = rtl.arrayRef(s.a);',
  11840. ' this.s = s.s.slice(0);',
  11841. ' this.m = s.m.slice(0);',
  11842. ' this.p.$assign(s.p);',
  11843. ' return this;',
  11844. ' };',
  11845. '});',
  11846. 'this.r = this.TRec.$clone({',
  11847. ' i: 1,',
  11848. ' a: [2, 3],',
  11849. ' s: [4, 5],',
  11850. ' m: [[11, 12], [13, 14]],',
  11851. ' p: this.TPoint.$clone({',
  11852. ' x: 21,',
  11853. ' y: 0',
  11854. ' })',
  11855. '});',
  11856. 'this.p = [this.TPoint.$clone({',
  11857. ' x: 1,',
  11858. ' y: 2',
  11859. '}), this.TPoint.$clone({',
  11860. ' x: 3,',
  11861. ' y: 4',
  11862. '})];',
  11863. '']),
  11864. LinesToStr([ // $mod.$main
  11865. '']));
  11866. end;
  11867. procedure TTestModule.TestAdvRecord_ExternalField;
  11868. begin
  11869. StartProgram(false);
  11870. Add([
  11871. '{$modeswitch AdvancedRecords}',
  11872. '{$modeswitch externalclass}',
  11873. 'type',
  11874. ' TCar = record',
  11875. ' public',
  11876. ' Intern: longint external name ''$Intern'';',
  11877. ' Intern2: longint external name ''$Intern2'';',
  11878. ' Bracket: longint external name ''["A B"]'';',
  11879. ' procedure DoIt;',
  11880. ' end;',
  11881. 'procedure tcar.doit;',
  11882. 'begin',
  11883. ' Intern:=Intern+1;',
  11884. ' Intern2:=Intern2+2;',
  11885. ' Bracket:=Bracket+3;',
  11886. 'end;',
  11887. 'var Rec: TCar = (intern: 11; intern2: 12; bracket: 13);',
  11888. 'begin',
  11889. ' Rec.intern:=Rec.intern+1;',
  11890. ' Rec.intern2:=Rec.intern2+2;',
  11891. ' Rec.Bracket:=Rec.Bracket+3;',
  11892. ' with Rec do begin',
  11893. ' intern:=intern+1;',
  11894. ' intern2:=intern2+2;',
  11895. ' Bracket:=Bracket+3;',
  11896. ' end;']);
  11897. ConvertProgram;
  11898. CheckSource('TestAdvRecord_ExternalField',
  11899. LinesToStr([ // statements
  11900. 'rtl.recNewT(this, "TCar", function () {',
  11901. ' this.$eq = function (b) {',
  11902. ' return (this.$Intern === b.$Intern) && (this.$Intern2 === b.$Intern2) && (this["A B"] === b["A B"]);',
  11903. ' };',
  11904. ' this.$assign = function (s) {',
  11905. ' this.$Intern = s.$Intern;',
  11906. ' this.$Intern2 = s.$Intern2;',
  11907. ' this["A B"] = s["A B"];',
  11908. ' return this;',
  11909. ' };',
  11910. ' this.DoIt = function () {',
  11911. ' this.$Intern = this.$Intern + 1;',
  11912. ' this.$Intern2 = this.$Intern2 + 2;',
  11913. ' this["A B"] = this["A B"] + 3;',
  11914. ' };',
  11915. '});',
  11916. 'this.Rec = this.TCar.$clone({',
  11917. ' $Intern: 11,',
  11918. ' $Intern2: 12,',
  11919. ' "A B": 13',
  11920. '});',
  11921. '']),
  11922. LinesToStr([ // $mod.$main
  11923. '$mod.Rec.$Intern = $mod.Rec.$Intern + 1;',
  11924. '$mod.Rec.$Intern2 = $mod.Rec.$Intern2 + 2;',
  11925. '$mod.Rec["A B"] = $mod.Rec["A B"] + 3;',
  11926. 'var $with = $mod.Rec;',
  11927. '$with.$Intern = $with.$Intern + 1;',
  11928. '$with.$Intern2 = $with.$Intern2 + 2;',
  11929. '$with["A B"] = $with["A B"] + 3;',
  11930. '']));
  11931. end;
  11932. procedure TTestModule.TestAdvRecord_SubRecord;
  11933. begin
  11934. StartProgram(false);
  11935. Add([
  11936. '{$modeswitch AdvancedRecords}',
  11937. 'type',
  11938. ' TRec = record',
  11939. ' type',
  11940. ' TPoint = record',
  11941. ' x,y: longint;',
  11942. ' class var Count: nativeint;',
  11943. ' procedure DoIt;',
  11944. ' class procedure DoThat; static;',
  11945. ' end;',
  11946. ' var',
  11947. ' i: longint;',
  11948. ' p: TPoint;',
  11949. ' procedure DoSome;',
  11950. ' end;',
  11951. 'const',
  11952. ' r: TRec = (',
  11953. ' i:1;',
  11954. ' p: (x:21;y:22)',
  11955. ' );',
  11956. 'procedure TRec.DoSome;',
  11957. 'begin',
  11958. ' p.x:=p.y+1;',
  11959. ' p.Count:=p.Count+2;',
  11960. 'end;',
  11961. 'procedure TRec.TPoint.DoIt;',
  11962. 'begin',
  11963. ' Count:=Count+3;',
  11964. 'end;',
  11965. 'class procedure TRec.TPoint.DoThat;',
  11966. 'begin',
  11967. ' Count:=Count+4;',
  11968. 'end;',
  11969. 'begin']);
  11970. ConvertProgram;
  11971. CheckSource('TestAdvRecord_SubRecord',
  11972. LinesToStr([ // statements
  11973. 'rtl.recNewT(this, "TRec", function () {',
  11974. ' rtl.recNewT(this, "TPoint", function () {',
  11975. ' this.x = 0;',
  11976. ' this.y = 0;',
  11977. ' this.Count = 0;',
  11978. ' this.$eq = function (b) {',
  11979. ' return (this.x === b.x) && (this.y === b.y);',
  11980. ' };',
  11981. ' this.$assign = function (s) {',
  11982. ' this.x = s.x;',
  11983. ' this.y = s.y;',
  11984. ' return this;',
  11985. ' };',
  11986. ' this.DoIt = function () {',
  11987. ' $mod.TRec.TPoint.Count = this.Count + 3;',
  11988. ' };',
  11989. ' this.DoThat = function () {',
  11990. ' $mod.TRec.TPoint.Count = $mod.TRec.TPoint.Count + 4;',
  11991. ' };',
  11992. ' }, true);',
  11993. ' this.i = 0;',
  11994. ' this.$new = function () {',
  11995. ' var r = Object.create(this);',
  11996. ' r.p = this.TPoint.$new();',
  11997. ' return r;',
  11998. ' };',
  11999. ' this.$eq = function (b) {',
  12000. ' return (this.i === b.i) && this.p.$eq(b.p);',
  12001. ' };',
  12002. ' this.$assign = function (s) {',
  12003. ' this.i = s.i;',
  12004. ' this.p.$assign(s.p);',
  12005. ' return this;',
  12006. ' };',
  12007. ' this.DoSome = function () {',
  12008. ' this.p.x = this.p.y + 1;',
  12009. ' this.TPoint.Count = this.p.Count + 2;',
  12010. ' };',
  12011. '}, true);',
  12012. 'this.r = this.TRec.$clone({',
  12013. ' i: 1,',
  12014. ' p: this.TRec.TPoint.$clone({',
  12015. ' x: 21,',
  12016. ' y: 22',
  12017. ' })',
  12018. '});',
  12019. '']),
  12020. LinesToStr([ // $mod.$main
  12021. '']));
  12022. end;
  12023. procedure TTestModule.TestAdvRecord_SubClass;
  12024. begin
  12025. StartProgram(false);
  12026. Add([
  12027. '{$modeswitch AdvancedRecords}',
  12028. 'type',
  12029. ' TObject = class end;',
  12030. ' TPoint = record',
  12031. ' type',
  12032. ' TBird = class',
  12033. ' procedure DoIt;',
  12034. ' class procedure Glob;',
  12035. ' end;',
  12036. ' procedure DoIt(b: TBird);',
  12037. ' end;',
  12038. 'procedure TPoint.TBird.DoIt;',
  12039. 'begin',
  12040. ' doit;',
  12041. ' self.doit;',
  12042. ' glob;',
  12043. ' self.glob;',
  12044. 'end;',
  12045. 'class procedure TPoint.TBird.Glob;',
  12046. 'begin',
  12047. ' glob;',
  12048. ' self.glob;',
  12049. 'end;',
  12050. 'procedure TPoint.DoIt(b: TBird);',
  12051. 'begin',
  12052. ' b.doit;',
  12053. ' b.glob;',
  12054. ' TBird.glob;',
  12055. 'end;',
  12056. 'begin',
  12057. '']);
  12058. ConvertProgram;
  12059. CheckSource('TestAdvRecord_SubClass',
  12060. LinesToStr([ // statements
  12061. 'rtl.createClass(this, "TObject", null, function () {',
  12062. ' this.$init = function () {',
  12063. ' };',
  12064. ' this.$final = function () {',
  12065. ' };',
  12066. '});',
  12067. 'rtl.recNewT(this, "TPoint", function () {',
  12068. ' rtl.createClass(this, "TBird", $mod.TObject, function () {',
  12069. ' this.DoIt = function () {',
  12070. ' this.DoIt();',
  12071. ' this.DoIt();',
  12072. ' this.$class.Glob();',
  12073. ' this.$class.Glob();',
  12074. ' };',
  12075. ' this.Glob = function () {',
  12076. ' this.Glob();',
  12077. ' this.Glob();',
  12078. ' };',
  12079. ' }, "TPoint.TBird");',
  12080. ' this.$eq = function (b) {',
  12081. ' return true;',
  12082. ' };',
  12083. ' this.$assign = function (s) {',
  12084. ' return this;',
  12085. ' };',
  12086. ' this.DoIt = function (b) {',
  12087. ' b.DoIt();',
  12088. ' b.$class.Glob();',
  12089. ' this.TBird.Glob();',
  12090. ' };',
  12091. '}, true);',
  12092. '']),
  12093. LinesToStr([ // $mod.$main
  12094. '']));
  12095. end;
  12096. procedure TTestModule.TestAdvRecord_SubInterfaceFail;
  12097. begin
  12098. StartProgram(false);
  12099. Add([
  12100. '{$modeswitch AdvancedRecords}',
  12101. 'type',
  12102. ' IUnknown = interface end;',
  12103. ' TPoint = record',
  12104. ' type IBird = interface end;',
  12105. ' end;',
  12106. 'begin',
  12107. '']);
  12108. SetExpectedPasResolverError('not yet implemented: IBird:TPasClassType [20190105143752] "interface inside record"',
  12109. nNotYetImplemented);
  12110. ParseProgram;
  12111. end;
  12112. procedure TTestModule.TestAdvRecord_Constructor;
  12113. begin
  12114. StartProgram(false);
  12115. Add([
  12116. '{$modeswitch AdvancedRecords}',
  12117. 'type',
  12118. ' TPoint = record',
  12119. ' x,y: longint;',
  12120. ' class procedure Run(w: longint = 13); static;',
  12121. ' constructor Create(ax: longint; ay: longint = -1);',
  12122. ' end;',
  12123. 'class procedure tpoint.run(w: longint);',
  12124. 'begin',
  12125. ' run;',
  12126. ' run();',
  12127. 'end;',
  12128. 'constructor tpoint.create(ax,ay: longint);',
  12129. 'begin',
  12130. ' x:=ax;',
  12131. ' self.y:=ay;',
  12132. ' run;',
  12133. ' run(ax);',
  12134. 'end;',
  12135. 'var r: TPoint;',
  12136. 'begin',
  12137. ' r:=TPoint.Create(1,2);',
  12138. ' with TPoint do r:=Create(1,2);',
  12139. ' r.Create(3);',
  12140. ' r:=r.Create(4);',
  12141. '']);
  12142. ConvertProgram;
  12143. CheckSource('TestAdvRecord_Constructor',
  12144. LinesToStr([ // statements
  12145. 'rtl.recNewT(this, "TPoint", function () {',
  12146. ' this.x = 0;',
  12147. ' this.y = 0;',
  12148. ' this.$eq = function (b) {',
  12149. ' return (this.x === b.x) && (this.y === b.y);',
  12150. ' };',
  12151. ' this.$assign = function (s) {',
  12152. ' this.x = s.x;',
  12153. ' this.y = s.y;',
  12154. ' return this;',
  12155. ' };',
  12156. ' this.Run = function (w) {',
  12157. ' $mod.TPoint.Run(13);',
  12158. ' $mod.TPoint.Run(13);',
  12159. ' };',
  12160. ' this.Create = function (ax, ay) {',
  12161. ' this.x = ax;',
  12162. ' this.y = ay;',
  12163. ' this.Run(13);',
  12164. ' this.Run(ax);',
  12165. ' return this;',
  12166. ' };',
  12167. '});',
  12168. 'this.r = this.TPoint.$new();',
  12169. '']),
  12170. LinesToStr([ // $mod.$main
  12171. '$mod.r.$assign($mod.TPoint.$new().Create(1, 2));',
  12172. 'var $with = $mod.TPoint;',
  12173. '$mod.r.$assign($with.$new().Create(1, 2));',
  12174. '$mod.r.Create(3, -1);',
  12175. '$mod.r.$assign($mod.r.Create(4, -1));',
  12176. '']));
  12177. end;
  12178. procedure TTestModule.TestAdvRecord_ClassConstructor_Program;
  12179. begin
  12180. StartProgram(false);
  12181. Add([
  12182. '{$modeswitch AdvancedRecords}',
  12183. 'type',
  12184. ' TPoint = record',
  12185. ' class var x: longint;',
  12186. ' class procedure Fly; static;',
  12187. ' class constructor Init;',
  12188. ' end;',
  12189. 'var count: word;',
  12190. 'class procedure Tpoint.Fly;',
  12191. 'begin',
  12192. 'end;',
  12193. 'class constructor tpoint.init;',
  12194. 'begin',
  12195. ' count:=count+1;',
  12196. ' x:=x+3;',
  12197. ' tpoint.x:=tpoint.x+4;',
  12198. ' fly;',
  12199. ' tpoint.fly;',
  12200. 'end;',
  12201. 'var r: TPoint;',
  12202. 'begin',
  12203. ' r.x:=r.x+10;',
  12204. ' r.Fly;',
  12205. ' r.Fly();',
  12206. '']);
  12207. ConvertProgram;
  12208. CheckSource('TestAdvRecord_ClassConstructor_Program',
  12209. LinesToStr([ // statements
  12210. 'rtl.recNewT(this, "TPoint", function () {',
  12211. ' this.x = 0;',
  12212. ' this.$eq = function (b) {',
  12213. ' return true;',
  12214. ' };',
  12215. ' this.$assign = function (s) {',
  12216. ' return this;',
  12217. ' };',
  12218. ' this.Fly = function () {',
  12219. ' };',
  12220. '}, true);',
  12221. 'this.count = 0;',
  12222. 'this.r = this.TPoint.$new();',
  12223. '']),
  12224. LinesToStr([ // $mod.$main
  12225. '(function () {',
  12226. ' $mod.count = $mod.count + 1;',
  12227. ' $mod.TPoint.x = $mod.TPoint.x + 3;',
  12228. ' $mod.TPoint.x = $mod.TPoint.x + 4;',
  12229. ' $mod.TPoint.Fly();',
  12230. ' $mod.TPoint.Fly();',
  12231. '})();',
  12232. '$mod.TPoint.x = $mod.r.x + 10;',
  12233. '$mod.TPoint.Fly();',
  12234. '$mod.TPoint.Fly();',
  12235. '']));
  12236. end;
  12237. procedure TTestModule.TestAdvRecord_ClassConstructor_Unit;
  12238. begin
  12239. StartUnit(false);
  12240. Add([
  12241. 'interface',
  12242. '{$modeswitch AdvancedRecords}',
  12243. 'type',
  12244. ' TPoint = record',
  12245. ' class var x: longint;',
  12246. ' class procedure Fly; static;',
  12247. ' class constructor Init;',
  12248. ' end;',
  12249. 'implementation',
  12250. 'var count: word;',
  12251. 'class procedure Tpoint.Fly;',
  12252. 'begin',
  12253. 'end;',
  12254. 'class constructor tpoint.init;',
  12255. 'begin',
  12256. ' count:=count+1;',
  12257. ' x:=3;',
  12258. ' tpoint.x:=4;',
  12259. ' fly;',
  12260. ' tpoint.fly;',
  12261. 'end;',
  12262. '']);
  12263. ConvertUnit;
  12264. CheckSource('TestAdvRecord_ClassConstructor_Unit',
  12265. LinesToStr([ // statements
  12266. 'var $impl = $mod.$impl;',
  12267. 'rtl.recNewT(this, "TPoint", function () {',
  12268. ' this.x = 0;',
  12269. ' this.$eq = function (b) {',
  12270. ' return true;',
  12271. ' };',
  12272. ' this.$assign = function (s) {',
  12273. ' return this;',
  12274. ' };',
  12275. ' this.Fly = function () {',
  12276. ' };',
  12277. '}, true);',
  12278. '']),
  12279. LinesToStr([ // $mod.$init
  12280. '(function () {',
  12281. ' $impl.count = $impl.count + 1;',
  12282. ' $mod.TPoint.x = 3;',
  12283. ' $mod.TPoint.x = 4;',
  12284. ' $mod.TPoint.Fly();',
  12285. ' $mod.TPoint.Fly();',
  12286. '})();',
  12287. '']),
  12288. LinesToStr([ // $mod.$main
  12289. '$impl.count = 0;',
  12290. '']));
  12291. end;
  12292. procedure TTestModule.TestClass_TObjectDefaultConstructor;
  12293. begin
  12294. StartProgram(false);
  12295. Add(['type',
  12296. ' TObject = class',
  12297. ' public',
  12298. ' constructor Create;',
  12299. ' destructor Destroy;',
  12300. ' end;',
  12301. ' TBird = TObject;',
  12302. 'constructor tobject.create;',
  12303. 'begin end;',
  12304. 'destructor tobject.destroy;',
  12305. 'begin end;',
  12306. 'var Obj: tobject;',
  12307. 'begin',
  12308. ' obj:=tobject.create;',
  12309. ' obj:=tobject.create();',
  12310. ' obj:=tbird.create;',
  12311. ' obj:=tbird.create();',
  12312. ' obj:=obj.create();',
  12313. ' obj.destroy;',
  12314. '']);
  12315. ConvertProgram;
  12316. CheckSource('TestClass_TObjectDefaultConstructor',
  12317. LinesToStr([ // statements
  12318. 'rtl.createClass(this,"TObject",null,function(){',
  12319. ' this.$init = function () {',
  12320. ' };',
  12321. ' this.$final = function () {',
  12322. ' };',
  12323. ' this.Create = function(){',
  12324. ' return this;',
  12325. ' };',
  12326. ' this.Destroy = function(){',
  12327. ' };',
  12328. '});',
  12329. 'this.Obj = null;'
  12330. ]),
  12331. LinesToStr([ // $mod.$main
  12332. '$mod.Obj = $mod.TObject.$create("Create");',
  12333. '$mod.Obj = $mod.TObject.$create("Create");',
  12334. '$mod.Obj = $mod.TObject.$create("Create");',
  12335. '$mod.Obj = $mod.TObject.$create("Create");',
  12336. '$mod.Obj = $mod.Obj.Create();',
  12337. '$mod.Obj.$destroy("Destroy");',
  12338. '']));
  12339. end;
  12340. procedure TTestModule.TestClass_TObjectConstructorWithParams;
  12341. begin
  12342. StartProgram(false);
  12343. Add('type');
  12344. Add(' TObject = class');
  12345. Add(' public');
  12346. Add(' constructor Create(Par: longint);');
  12347. Add(' end;');
  12348. Add('constructor tobject.create(par: longint);');
  12349. Add('begin end;');
  12350. Add('var Obj: tobject;');
  12351. Add('begin');
  12352. Add(' obj:=tobject.create(3);');
  12353. ConvertProgram;
  12354. CheckSource('TestClass_TObjectConstructorWithParams',
  12355. LinesToStr([ // statements
  12356. 'rtl.createClass(this,"TObject",null,function(){',
  12357. ' this.$init = function () {',
  12358. ' };',
  12359. ' this.$final = function () {',
  12360. ' };',
  12361. ' this.Create = function(Par){',
  12362. ' return this;',
  12363. ' };',
  12364. '});',
  12365. 'this.Obj = null;'
  12366. ]),
  12367. LinesToStr([ // $mod.$main
  12368. '$mod.Obj = $mod.TObject.$create("Create",[3]);'
  12369. ]));
  12370. end;
  12371. procedure TTestModule.TestClass_TObjectConstructorWithDefaultParam;
  12372. begin
  12373. StartProgram(false);
  12374. Add('type');
  12375. Add(' TObject = class');
  12376. Add(' public');
  12377. Add(' constructor Create;');
  12378. Add(' end;');
  12379. Add(' TTest = class(TObject)');
  12380. Add(' public');
  12381. Add(' constructor Create(const Par: longint = 1);');
  12382. Add(' end;');
  12383. Add('constructor tobject.create;');
  12384. Add('begin end;');
  12385. Add('constructor ttest.create(const par: longint);');
  12386. Add('begin end;');
  12387. Add('var t: ttest;');
  12388. Add('begin');
  12389. Add(' t:=ttest.create;');
  12390. Add(' t:=ttest.create(2);');
  12391. ConvertProgram;
  12392. CheckSource('TestClass_TObjectConstructorWithDefaultParam',
  12393. LinesToStr([ // statements
  12394. 'rtl.createClass(this,"TObject",null,function(){',
  12395. ' this.$init = function () {',
  12396. ' };',
  12397. ' this.$final = function () {',
  12398. ' };',
  12399. ' this.Create = function(){',
  12400. ' return this;',
  12401. ' };',
  12402. '});',
  12403. 'rtl.createClass(this, "TTest", this.TObject, function () {',
  12404. ' this.Create$1 = function (Par) {',
  12405. ' return this;',
  12406. ' };',
  12407. '});',
  12408. 'this.t = null;'
  12409. ]),
  12410. LinesToStr([ // $mod.$main
  12411. '$mod.t = $mod.TTest.$create("Create$1", [1]);',
  12412. '$mod.t = $mod.TTest.$create("Create$1", [2]);'
  12413. ]));
  12414. end;
  12415. procedure TTestModule.TestClass_Var;
  12416. begin
  12417. StartProgram(false);
  12418. Add([
  12419. 'type',
  12420. ' TObject = class',
  12421. ' public',
  12422. ' vI: longint;',
  12423. ' constructor Create(Par: longint);',
  12424. ' end;',
  12425. 'constructor tobject.create(par: longint);',
  12426. 'begin',
  12427. ' vi:=par+3',
  12428. 'end;',
  12429. 'var Obj: tobject;',
  12430. 'begin',
  12431. ' obj:=tobject.create(4);',
  12432. ' obj.vi:=obj.VI+5;']);
  12433. ConvertProgram;
  12434. CheckSource('TestClass_Var',
  12435. LinesToStr([ // statements
  12436. 'rtl.createClass(this,"TObject",null,function(){',
  12437. ' this.$init = function () {',
  12438. ' this.vI = 0;',
  12439. ' };',
  12440. ' this.$final = function () {',
  12441. ' };',
  12442. ' this.Create = function(Par){',
  12443. ' this.vI = Par+3;',
  12444. ' return this;',
  12445. ' };',
  12446. '});',
  12447. 'this.Obj = null;'
  12448. ]),
  12449. LinesToStr([ // $mod.$main
  12450. '$mod.Obj = $mod.TObject.$create("Create",[4]);',
  12451. '$mod.Obj.vI = $mod.Obj.vI + 5;'
  12452. ]));
  12453. end;
  12454. procedure TTestModule.TestClass_Method;
  12455. begin
  12456. StartProgram(false);
  12457. Add('type');
  12458. Add(' TObject = class');
  12459. Add(' public');
  12460. Add(' vI: longint;');
  12461. Add(' Sub: TObject;');
  12462. Add(' constructor Create;');
  12463. Add(' function GetIt(Par: longint): tobject;');
  12464. Add(' end;');
  12465. Add('constructor tobject.create; begin end;');
  12466. Add('function tobject.getit(par: longint): tobject;');
  12467. Add('begin');
  12468. Add(' Self.vi:=par+3;');
  12469. Add(' Result:=self.sub;');
  12470. Add('end;');
  12471. Add('var Obj: tobject;');
  12472. Add('begin');
  12473. Add(' obj:=tobject.create;');
  12474. Add(' obj.getit(4);');
  12475. Add(' obj.sub.sub:=nil;');
  12476. Add(' obj.sub.getit(5);');
  12477. Add(' obj.sub.getit(6).SUB:=nil;');
  12478. Add(' obj.sub.getit(7).GETIT(8);');
  12479. Add(' obj.sub.getit(9).SuB.getit(10);');
  12480. ConvertProgram;
  12481. CheckSource('TestClass_Method',
  12482. LinesToStr([ // statements
  12483. 'rtl.createClass(this,"TObject",null,function(){',
  12484. ' this.$init = function () {',
  12485. ' this.vI = 0;',
  12486. ' this.Sub = null;',
  12487. ' };',
  12488. ' this.$final = function () {',
  12489. ' this.Sub = undefined;',
  12490. ' };',
  12491. ' this.Create = function(){',
  12492. ' return this;',
  12493. ' };',
  12494. ' this.GetIt = function(Par){',
  12495. ' var Result = null;',
  12496. ' this.vI = Par + 3;',
  12497. ' Result = this.Sub;',
  12498. ' return Result;',
  12499. ' };',
  12500. '});',
  12501. 'this.Obj = null;'
  12502. ]),
  12503. LinesToStr([ // $mod.$main
  12504. '$mod.Obj = $mod.TObject.$create("Create");',
  12505. '$mod.Obj.GetIt(4);',
  12506. '$mod.Obj.Sub.Sub=null;',
  12507. '$mod.Obj.Sub.GetIt(5);',
  12508. '$mod.Obj.Sub.GetIt(6).Sub=null;',
  12509. '$mod.Obj.Sub.GetIt(7).GetIt(8);',
  12510. '$mod.Obj.Sub.GetIt(9).Sub.GetIt(10);'
  12511. ]));
  12512. end;
  12513. procedure TTestModule.TestClass_Implementation;
  12514. begin
  12515. StartUnit(false);
  12516. Add([
  12517. 'interface',
  12518. 'type',
  12519. ' TObject = class',
  12520. ' constructor Create;',
  12521. ' end;',
  12522. 'implementation',
  12523. 'type',
  12524. ' TIntClass = class',
  12525. ' constructor Create; reintroduce;',
  12526. ' class procedure DoGlob;',
  12527. ' end;',
  12528. 'constructor tintclass.create;',
  12529. 'begin',
  12530. ' inherited;',
  12531. ' inherited create;',
  12532. ' doglob;',
  12533. 'end;',
  12534. 'class procedure tintclass.doglob;',
  12535. 'begin',
  12536. 'end;',
  12537. 'constructor tobject.create;',
  12538. 'var',
  12539. ' iC: tintclass;',
  12540. 'begin',
  12541. ' ic:=tintclass.create;',
  12542. ' tintclass.doglob;',
  12543. ' ic.doglob;',
  12544. 'end;',
  12545. 'initialization',
  12546. ' tintclass.doglob;',
  12547. '']);
  12548. ConvertUnit;
  12549. CheckSource('TestClass_Implementation',
  12550. LinesToStr([ // statements
  12551. 'var $impl = $mod.$impl;',
  12552. 'rtl.createClass(this, "TObject", null, function () {',
  12553. ' this.$init = function () {',
  12554. ' };',
  12555. ' this.$final = function () {',
  12556. ' };',
  12557. ' this.Create = function () {',
  12558. ' var iC = null;',
  12559. ' iC = $impl.TIntClass.$create("Create$1");',
  12560. ' $impl.TIntClass.DoGlob();',
  12561. ' iC.$class.DoGlob();',
  12562. ' return this;',
  12563. ' };',
  12564. '});',
  12565. '']),
  12566. LinesToStr([ // $mod.$main
  12567. '$impl.TIntClass.DoGlob();',
  12568. '']),
  12569. LinesToStr([
  12570. 'rtl.createClass($impl, "TIntClass", $mod.TObject, function () {',
  12571. ' this.Create$1 = function () {',
  12572. ' $mod.TObject.Create.call(this);',
  12573. ' $mod.TObject.Create.call(this);',
  12574. ' this.$class.DoGlob();',
  12575. ' return this;',
  12576. ' };',
  12577. ' this.DoGlob = function () {',
  12578. ' };',
  12579. '});',
  12580. '']));
  12581. end;
  12582. procedure TTestModule.TestClass_Inheritance;
  12583. begin
  12584. StartProgram(false);
  12585. Add('type');
  12586. Add(' TObject = class');
  12587. Add(' public');
  12588. Add(' constructor Create;');
  12589. Add(' end;');
  12590. Add(' TClassA = class');
  12591. Add(' end;');
  12592. Add(' TClassB = class(TObject)');
  12593. Add(' procedure ProcB;');
  12594. Add(' end;');
  12595. Add('constructor tobject.create; begin end;');
  12596. Add('procedure tclassb.procb; begin end;');
  12597. Add('var');
  12598. Add(' oO: TObject;');
  12599. Add(' oA: TClassA;');
  12600. Add(' oB: TClassB;');
  12601. Add('begin');
  12602. Add(' oO:=tobject.Create;');
  12603. Add(' oA:=tclassa.Create;');
  12604. Add(' ob:=tclassb.Create;');
  12605. Add(' if oo is tclassa then ;');
  12606. Add(' ob:=oo as tclassb;');
  12607. Add(' (oo as tclassb).procb;');
  12608. ConvertProgram;
  12609. CheckSource('TestClass_Inheritance',
  12610. LinesToStr([ // statements
  12611. 'rtl.createClass(this,"TObject",null,function(){',
  12612. ' this.$init = function () {',
  12613. ' };',
  12614. ' this.$final = function () {',
  12615. ' };',
  12616. ' this.Create = function () {',
  12617. ' return this;',
  12618. ' };',
  12619. '});',
  12620. 'rtl.createClass(this,"TClassA",this.TObject,function(){',
  12621. '});',
  12622. 'rtl.createClass(this,"TClassB",this.TObject,function(){',
  12623. ' this.ProcB = function () {',
  12624. ' };',
  12625. '});',
  12626. 'this.oO = null;',
  12627. 'this.oA = null;',
  12628. 'this.oB = null;'
  12629. ]),
  12630. LinesToStr([ // $mod.$main
  12631. '$mod.oO = $mod.TObject.$create("Create");',
  12632. '$mod.oA = $mod.TClassA.$create("Create");',
  12633. '$mod.oB = $mod.TClassB.$create("Create");',
  12634. 'if ($mod.TClassA.isPrototypeOf($mod.oO));',
  12635. '$mod.oB = rtl.as($mod.oO, $mod.TClassB);',
  12636. 'rtl.as($mod.oO, $mod.TClassB).ProcB();'
  12637. ]));
  12638. end;
  12639. procedure TTestModule.TestClass_TypeAlias;
  12640. begin
  12641. StartProgram(false);
  12642. Add([
  12643. '{$interfaces corba}',
  12644. 'type',
  12645. ' IObject = interface',
  12646. ' end;',
  12647. ' IBird = type IObject;',
  12648. ' TObject = class',
  12649. ' end;',
  12650. ' TBird = type TObject;',
  12651. 'var',
  12652. ' oObj: TObject;',
  12653. ' oBird: TBird;',
  12654. ' IntfObj: IObject;',
  12655. ' IntfBird: IBird;',
  12656. 'begin',
  12657. ' oObj:=oBird;',
  12658. '']);
  12659. ConvertProgram;
  12660. CheckSource('TestClass_TypeAlias',
  12661. LinesToStr([ // statements
  12662. 'rtl.createInterface(this, "IObject", "{B92D5841-6F2A-306A-8000-000000000000}", [], null);',
  12663. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-387B-AE88-F10981585074}", [], this.IObject);',
  12664. 'rtl.createClass(this, "TObject", null, function () {',
  12665. ' this.$init = function () {',
  12666. ' };',
  12667. ' this.$final = function () {',
  12668. ' };',
  12669. '});',
  12670. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  12671. '});',
  12672. 'this.oObj = null;',
  12673. 'this.oBird = null;',
  12674. 'this.IntfObj = null;',
  12675. 'this.IntfBird = null;',
  12676. '']),
  12677. LinesToStr([ // $mod.$main
  12678. '$mod.oObj = $mod.oBird;',
  12679. '']));
  12680. end;
  12681. procedure TTestModule.TestClass_AbstractMethod;
  12682. begin
  12683. StartProgram(false);
  12684. Add('type');
  12685. Add(' TObject = class');
  12686. Add(' public');
  12687. Add(' procedure DoIt; virtual; abstract;');
  12688. Add(' end;');
  12689. Add('begin');
  12690. ConvertProgram;
  12691. CheckSource('TestClass_AbstractMethod',
  12692. LinesToStr([ // statements
  12693. 'rtl.createClass(this,"TObject",null,function(){',
  12694. ' this.$init = function () {',
  12695. ' };',
  12696. ' this.$final = function () {',
  12697. ' };',
  12698. '});'
  12699. ]),
  12700. LinesToStr([ // this.$main
  12701. ''
  12702. ]));
  12703. end;
  12704. procedure TTestModule.TestClass_CallInherited_ProcNoParams;
  12705. begin
  12706. StartProgram(false);
  12707. Add([
  12708. 'type',
  12709. ' TObject = class',
  12710. ' procedure DoAbstract; virtual; abstract;',
  12711. ' procedure DoVirtual; virtual;',
  12712. ' procedure DoIt;',
  12713. ' end;',
  12714. ' TA = class',
  12715. ' procedure doabstract; override;',
  12716. ' procedure dovirtual; override;',
  12717. ' procedure DoSome;',
  12718. ' end;',
  12719. 'procedure tobject.dovirtual;',
  12720. 'begin',
  12721. ' inherited; // call non existing ancestor -> ignore silently',
  12722. 'end;',
  12723. 'procedure tobject.doit;',
  12724. 'begin',
  12725. 'end;',
  12726. 'procedure ta.doabstract;',
  12727. 'begin',
  12728. ' inherited dovirtual; // call TObject.DoVirtual',
  12729. 'end;',
  12730. 'procedure ta.dovirtual;',
  12731. 'begin',
  12732. ' inherited; // call TObject.DoVirtual',
  12733. ' inherited dovirtual; // call TObject.DoVirtual',
  12734. ' inherited dovirtual(); // call TObject.DoVirtual',
  12735. ' doit;',
  12736. ' doit();',
  12737. 'end;',
  12738. 'procedure ta.dosome;',
  12739. 'begin',
  12740. ' inherited; // call non existing ancestor method -> silently ignore',
  12741. 'end;',
  12742. 'begin']);
  12743. ConvertProgram;
  12744. CheckSource('TestClass_CallInherited_ProcNoParams',
  12745. LinesToStr([ // statements
  12746. 'rtl.createClass(this,"TObject",null,function(){',
  12747. ' this.$init = function () {',
  12748. ' };',
  12749. ' this.$final = function () {',
  12750. ' };',
  12751. ' this.DoVirtual = function () {',
  12752. ' };',
  12753. ' this.DoIt = function () {',
  12754. ' };',
  12755. '});',
  12756. 'rtl.createClass(this, "TA", this.TObject, function () {',
  12757. ' this.DoAbstract = function () {',
  12758. ' $mod.TObject.DoVirtual.call(this);',
  12759. ' };',
  12760. ' this.DoVirtual = function () {',
  12761. ' $mod.TObject.DoVirtual.call(this);',
  12762. ' $mod.TObject.DoVirtual.call(this);',
  12763. ' $mod.TObject.DoVirtual.call(this);',
  12764. ' this.DoIt();',
  12765. ' this.DoIt();',
  12766. ' };',
  12767. ' this.DoSome = function () {',
  12768. ' };',
  12769. '});'
  12770. ]),
  12771. LinesToStr([ // this.$main
  12772. ''
  12773. ]));
  12774. end;
  12775. procedure TTestModule.TestClass_CallInherited_WithParams;
  12776. begin
  12777. StartProgram(false);
  12778. Add([
  12779. 'type',
  12780. ' TObject = class',
  12781. ' procedure DoAbstract(pA: longint; pB: longint = 0); virtual; abstract;',
  12782. ' procedure DoVirtual(pA: longint; pB: longint = 0); virtual;',
  12783. ' procedure DoIt(pA: longint; pB: longint = 0);',
  12784. ' procedure DoIt2(pA: longint = 1; pB: longint = 2);',
  12785. ' function GetIt(pA: longint = 1; pB: longint = 2): longint;',
  12786. ' end;',
  12787. ' TClassA = class',
  12788. ' procedure DoAbstract(pA: longint; pB: longint = 0); override;',
  12789. ' procedure DoVirtual(pA: longint; pB: longint = 0); override;',
  12790. ' function GetIt(pA: longint = 1; pB: longint = 2): longint;',
  12791. ' end;',
  12792. 'procedure tobject.dovirtual(pa: longint; pb: longint = 0);',
  12793. 'begin',
  12794. 'end;',
  12795. 'procedure tobject.doit(pa: longint; pb: longint = 0);',
  12796. 'begin',
  12797. 'end;',
  12798. 'procedure tobject.doit2(pa: longint; pb: longint = 0);',
  12799. 'begin',
  12800. 'end;',
  12801. 'function tobject.getit(pa: longint; pb: longint = 0): longint;',
  12802. 'begin',
  12803. 'end;',
  12804. 'procedure tclassa.doabstract(pa: longint; pb: longint = 0);',
  12805. 'begin',
  12806. ' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)',
  12807. ' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)',
  12808. 'end;',
  12809. 'procedure tclassa.dovirtual(pa: longint; pb: longint = 0);',
  12810. 'begin',
  12811. ' inherited; // call TObject.DoVirtual(pA,pB)',
  12812. ' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)',
  12813. ' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)',
  12814. ' doit(pa,pb);',
  12815. ' doit(pa);',
  12816. ' doit2(pa);',
  12817. ' doit2;',
  12818. 'end;',
  12819. 'function tclassa.getit(pa: longint; pb: longint = 0): longint;',
  12820. 'begin',
  12821. ' pa:=inherited;',
  12822. 'end;',
  12823. 'begin']);
  12824. ConvertProgram;
  12825. CheckSource('TestClass_CallInherited_WithParams',
  12826. LinesToStr([ // statements
  12827. 'rtl.createClass(this,"TObject",null,function(){',
  12828. ' this.$init = function () {',
  12829. ' };',
  12830. ' this.$final = function () {',
  12831. ' };',
  12832. ' this.DoVirtual = function (pA,pB) {',
  12833. ' };',
  12834. ' this.DoIt = function (pA,pB) {',
  12835. ' };',
  12836. ' this.DoIt2 = function (pA,pB) {',
  12837. ' };',
  12838. ' this.GetIt = function (pA, pB) {',
  12839. ' var Result = 0;',
  12840. ' return Result;',
  12841. ' };',
  12842. '});',
  12843. 'rtl.createClass(this, "TClassA", this.TObject, function () {',
  12844. ' this.DoAbstract = function (pA,pB) {',
  12845. ' $mod.TObject.DoVirtual.call(this,pA,pB);',
  12846. ' $mod.TObject.DoVirtual.call(this,pA,0);',
  12847. ' };',
  12848. ' this.DoVirtual = function (pA,pB) {',
  12849. ' $mod.TObject.DoVirtual.apply(this, arguments);',
  12850. ' $mod.TObject.DoVirtual.call(this,pA,pB);',
  12851. ' $mod.TObject.DoVirtual.call(this,pA,0);',
  12852. ' this.DoIt(pA,pB);',
  12853. ' this.DoIt(pA,0);',
  12854. ' this.DoIt2(pA,2);',
  12855. ' this.DoIt2(1,2);',
  12856. ' };',
  12857. ' this.GetIt$1 = function (pA, pB) {',
  12858. ' var Result = 0;',
  12859. ' pA = $mod.TObject.GetIt.apply(this, arguments);',
  12860. ' return Result;',
  12861. ' };',
  12862. '});'
  12863. ]),
  12864. LinesToStr([ // this.$main
  12865. ''
  12866. ]));
  12867. end;
  12868. procedure TTestModule.TestClasS_CallInheritedConstructor;
  12869. begin
  12870. StartProgram(false);
  12871. Add('type');
  12872. Add(' TObject = class');
  12873. Add(' constructor Create; virtual;');
  12874. Add(' constructor CreateWithB(b: boolean);');
  12875. Add(' end;');
  12876. Add(' TA = class');
  12877. Add(' constructor Create; override;');
  12878. Add(' constructor CreateWithC(c: char);');
  12879. Add(' procedure DoIt;');
  12880. Add(' class function DoSome: TObject;');
  12881. Add(' end;');
  12882. Add('constructor tobject.create;');
  12883. Add('begin');
  12884. Add(' inherited; // call non existing ancestor -> ignore silently');
  12885. Add('end;');
  12886. Add('constructor tobject.createwithb(b: boolean);');
  12887. Add('begin');
  12888. Add(' inherited; // call non existing ancestor -> ignore silently');
  12889. Add(' create; // normal call');
  12890. Add('end;');
  12891. Add('constructor ta.create;');
  12892. Add('begin');
  12893. Add(' inherited; // normal call TObject.Create');
  12894. Add(' inherited create; // normal call TObject.Create');
  12895. Add(' inherited createwithb(false); // normal call TObject.CreateWithB');
  12896. Add('end;');
  12897. Add('constructor ta.createwithc(c: char);');
  12898. Add('begin');
  12899. Add(' inherited create; // call TObject.Create');
  12900. Add(' inherited createwithb(true); // call TObject.CreateWithB');
  12901. Add(' doit;');
  12902. Add(' doit();');
  12903. Add(' dosome;');
  12904. Add('end;');
  12905. Add('procedure ta.doit;');
  12906. Add('begin');
  12907. Add(' create; // normal call');
  12908. Add(' createwithb(false); // normal call');
  12909. Add(' createwithc(''c''); // normal call');
  12910. Add('end;');
  12911. Add('class function ta.dosome: TObject;');
  12912. Add('begin');
  12913. Add(' Result:=create; // constructor');
  12914. Add(' Result:=createwithb(true); // constructor');
  12915. Add(' Result:=createwithc(''c''); // constructor');
  12916. Add('end;');
  12917. Add('begin');
  12918. ConvertProgram;
  12919. CheckSource('TestClass_CallInheritedConstructor',
  12920. LinesToStr([ // statements
  12921. 'rtl.createClass(this,"TObject",null,function(){',
  12922. ' this.$init = function () {',
  12923. ' };',
  12924. ' this.$final = function () {',
  12925. ' };',
  12926. ' this.Create = function () {',
  12927. ' return this;',
  12928. ' };',
  12929. ' this.CreateWithB = function (b) {',
  12930. ' this.Create();',
  12931. ' return this;',
  12932. ' };',
  12933. '});',
  12934. 'rtl.createClass(this, "TA", this.TObject, function () {',
  12935. ' this.Create = function () {',
  12936. ' $mod.TObject.Create.call(this);',
  12937. ' $mod.TObject.Create.call(this);',
  12938. ' $mod.TObject.CreateWithB.call(this, false);',
  12939. ' return this;',
  12940. ' };',
  12941. ' this.CreateWithC = function (c) {',
  12942. ' $mod.TObject.Create.call(this);',
  12943. ' $mod.TObject.CreateWithB.call(this, true);',
  12944. ' this.DoIt();',
  12945. ' this.DoIt();',
  12946. ' this.$class.DoSome();',
  12947. ' return this;',
  12948. ' };',
  12949. ' this.DoIt = function () {',
  12950. ' this.Create();',
  12951. ' this.CreateWithB(false);',
  12952. ' this.CreateWithC("c");',
  12953. ' };',
  12954. ' this.DoSome = function () {',
  12955. ' var Result = null;',
  12956. ' Result = this.$create("Create");',
  12957. ' Result = this.$create("CreateWithB", [true]);',
  12958. ' Result = this.$create("CreateWithC", ["c"]);',
  12959. ' return Result;',
  12960. ' };',
  12961. '});'
  12962. ]),
  12963. LinesToStr([ // this.$main
  12964. ''
  12965. ]));
  12966. end;
  12967. procedure TTestModule.TestClass_ClassVar_Assign;
  12968. begin
  12969. StartProgram(false);
  12970. Add([
  12971. 'type',
  12972. ' TObject = class',
  12973. ' public',
  12974. ' class var vI: longint;',
  12975. ' class var Sub: TObject;',
  12976. ' constructor Create;',
  12977. ' class function GetIt(var Par: longint): tobject;',
  12978. ' end;',
  12979. 'constructor tobject.create;',
  12980. 'begin',
  12981. ' vi:=vi+1;',
  12982. ' Self.vi:=Self.vi+1;',
  12983. ' inc(vi);',
  12984. 'end;',
  12985. 'class function tobject.getit(var par: longint): tobject;',
  12986. 'begin',
  12987. ' vi:=vi+3;',
  12988. ' Self.vi:=Self.vi+4;',
  12989. ' inc(vi);',
  12990. ' Result:=self.sub;',
  12991. ' GetIt(vi);',
  12992. 'end;',
  12993. 'var Obj: tobject;',
  12994. 'begin',
  12995. ' obj:=tobject.create;',
  12996. ' tobject.vi:=3;',
  12997. ' if tobject.vi=4 then ;',
  12998. ' tobject.sub:=nil;',
  12999. ' obj.sub:=nil;',
  13000. ' obj.sub.sub:=nil;']);
  13001. ConvertProgram;
  13002. CheckSource('TestClass_ClassVar_Assign',
  13003. LinesToStr([ // statements
  13004. 'rtl.createClass(this,"TObject",null,function(){',
  13005. ' this.vI = 0;',
  13006. ' this.Sub = null;',
  13007. ' this.$init = function () {',
  13008. ' };',
  13009. ' this.$final = function () {',
  13010. ' };',
  13011. ' this.Create = function(){',
  13012. ' $mod.TObject.vI = this.vI+1;',
  13013. ' $mod.TObject.vI = this.vI+1;',
  13014. ' $mod.TObject.vI += 1;',
  13015. ' return this;',
  13016. ' };',
  13017. ' this.GetIt = function(Par){',
  13018. ' var Result = null;',
  13019. ' $mod.TObject.vI = this.vI + 3;',
  13020. ' $mod.TObject.vI = this.vI + 4;',
  13021. ' $mod.TObject.vI += 1;',
  13022. ' Result = this.Sub;',
  13023. ' this.GetIt({',
  13024. ' p: $mod.TObject,',
  13025. ' get: function () {',
  13026. ' return this.p.vI;',
  13027. ' },',
  13028. ' set: function (v) {',
  13029. ' this.p.vI = v;',
  13030. ' }',
  13031. ' });',
  13032. ' return Result;',
  13033. ' };',
  13034. '});',
  13035. 'this.Obj = null;'
  13036. ]),
  13037. LinesToStr([ // $mod.$main
  13038. '$mod.Obj = $mod.TObject.$create("Create");',
  13039. '$mod.TObject.vI = 3;',
  13040. 'if ($mod.TObject.vI === 4);',
  13041. '$mod.TObject.Sub=null;',
  13042. '$mod.TObject.Sub=null;',
  13043. '$mod.TObject.Sub=null;',
  13044. '']));
  13045. end;
  13046. procedure TTestModule.TestClass_CallClassMethod;
  13047. begin
  13048. StartProgram(false);
  13049. Add('type');
  13050. Add(' TObject = class');
  13051. Add(' public');
  13052. Add(' class var vI: longint;');
  13053. Add(' class var Sub: TObject;');
  13054. Add(' constructor Create;');
  13055. Add(' function GetMore(Par: longint): longint;');
  13056. Add(' class function GetIt(Par: longint): tobject;');
  13057. Add(' end;');
  13058. Add('constructor tobject.create;');
  13059. Add('begin');
  13060. Add(' sub:=getit(3);');
  13061. Add(' vi:=getmore(4);');
  13062. Add(' sub:=Self.getit(5);');
  13063. Add(' vi:=Self.getmore(6);');
  13064. Add('end;');
  13065. Add('function tobject.getmore(par: longint): longint;');
  13066. Add('begin');
  13067. Add(' sub:=getit(11);');
  13068. Add(' vi:=getmore(12);');
  13069. Add(' sub:=self.getit(13);');
  13070. Add(' vi:=self.getmore(14);');
  13071. Add('end;');
  13072. Add('class function tobject.getit(par: longint): tobject;');
  13073. Add('begin');
  13074. Add(' sub:=getit(21);');
  13075. Add(' vi:=sub.getmore(22);');
  13076. Add(' sub:=self.getit(23);');
  13077. Add(' vi:=self.sub.getmore(24);');
  13078. Add('end;');
  13079. Add('var Obj: tobject;');
  13080. Add('begin');
  13081. Add(' obj:=tobject.create;');
  13082. Add(' tobject.getit(5);');
  13083. Add(' obj.getit(6);');
  13084. Add(' obj.sub.getit(7);');
  13085. Add(' obj.sub.getit(8).SUB:=nil;');
  13086. Add(' obj.sub.getit(9).GETIT(10);');
  13087. Add(' obj.sub.getit(11).SuB.getit(12);');
  13088. ConvertProgram;
  13089. CheckSource('TestClass_CallClassMethod',
  13090. LinesToStr([ // statements
  13091. 'rtl.createClass(this,"TObject",null,function(){',
  13092. ' this.vI = 0;',
  13093. ' this.Sub = null;',
  13094. ' this.$init = function () {',
  13095. ' };',
  13096. ' this.$final = function () {',
  13097. ' };',
  13098. ' this.Create = function(){',
  13099. ' $mod.TObject.Sub = this.$class.GetIt(3);',
  13100. ' $mod.TObject.vI = this.GetMore(4);',
  13101. ' $mod.TObject.Sub = this.$class.GetIt(5);',
  13102. ' $mod.TObject.vI = this.GetMore(6);',
  13103. ' return this;',
  13104. ' };',
  13105. ' this.GetMore = function(Par){',
  13106. ' var Result = 0;',
  13107. ' $mod.TObject.Sub = this.$class.GetIt(11);',
  13108. ' $mod.TObject.vI = this.GetMore(12);',
  13109. ' $mod.TObject.Sub = this.$class.GetIt(13);',
  13110. ' $mod.TObject.vI = this.GetMore(14);',
  13111. ' return Result;',
  13112. ' };',
  13113. ' this.GetIt = function(Par){',
  13114. ' var Result = null;',
  13115. ' $mod.TObject.Sub = this.GetIt(21);',
  13116. ' $mod.TObject.vI = this.Sub.GetMore(22);',
  13117. ' $mod.TObject.Sub = this.GetIt(23);',
  13118. ' $mod.TObject.vI = this.Sub.GetMore(24);',
  13119. ' return Result;',
  13120. ' };',
  13121. '});',
  13122. 'this.Obj = null;'
  13123. ]),
  13124. LinesToStr([ // $mod.$main
  13125. '$mod.Obj = $mod.TObject.$create("Create");',
  13126. '$mod.TObject.GetIt(5);',
  13127. '$mod.Obj.$class.GetIt(6);',
  13128. '$mod.Obj.Sub.$class.GetIt(7);',
  13129. '$mod.TObject.Sub=null;',
  13130. '$mod.Obj.Sub.$class.GetIt(9).$class.GetIt(10);',
  13131. '$mod.Obj.Sub.$class.GetIt(11).Sub.$class.GetIt(12);',
  13132. '']));
  13133. end;
  13134. procedure TTestModule.TestClass_CallClassMethodStatic;
  13135. begin
  13136. StartProgram(false);
  13137. Add([
  13138. 'type',
  13139. ' TObject = class',
  13140. ' public',
  13141. ' class function Fly: tobject; static;',
  13142. ' end;',
  13143. 'class function tobject.Fly: tobject;',
  13144. 'begin',
  13145. ' Result.Fly;',
  13146. ' Result.Fly();',
  13147. ' Fly;',
  13148. ' Fly();',
  13149. ' Fly.Fly;',
  13150. ' Fly.Fly();',
  13151. 'end;',
  13152. 'var Obj: tobject;',
  13153. 'begin',
  13154. ' obj.Fly;',
  13155. ' obj.Fly();',
  13156. ' with obj do begin',
  13157. ' Fly;',
  13158. ' Fly();',
  13159. ' end;',
  13160. '']);
  13161. ConvertProgram;
  13162. CheckSource('TestClass_CallClassMethodStatic',
  13163. LinesToStr([ // statements
  13164. 'rtl.createClass(this, "TObject", null, function () {',
  13165. ' this.$init = function () {',
  13166. ' };',
  13167. ' this.$final = function () {',
  13168. ' };',
  13169. ' this.Fly = function () {',
  13170. ' var Result = null;',
  13171. ' $mod.TObject.Fly();',
  13172. ' $mod.TObject.Fly();',
  13173. ' $mod.TObject.Fly();',
  13174. ' $mod.TObject.Fly();',
  13175. ' $mod.TObject.Fly();',
  13176. ' $mod.TObject.Fly();',
  13177. ' return Result;',
  13178. ' };',
  13179. '});',
  13180. 'this.Obj = null;'
  13181. ]),
  13182. LinesToStr([ // $mod.$main
  13183. '$mod.TObject.Fly();',
  13184. '$mod.TObject.Fly();',
  13185. 'var $with = $mod.Obj;',
  13186. '$with.Fly();',
  13187. '$with.Fly();',
  13188. '']));
  13189. end;
  13190. procedure TTestModule.TestClass_Property;
  13191. begin
  13192. StartProgram(false);
  13193. Add('type');
  13194. Add(' TObject = class');
  13195. Add(' Fx: longint;');
  13196. Add(' Fy: longint;');
  13197. Add(' function GetInt: longint;');
  13198. Add(' procedure SetInt(Value: longint);');
  13199. Add(' procedure DoIt;');
  13200. Add(' property IntA: longint read Fx write Fy;');
  13201. Add(' property IntB: longint read GetInt write SetInt;');
  13202. Add(' end;');
  13203. Add('function tobject.getint: longint;');
  13204. Add('begin');
  13205. Add(' result:=fx;');
  13206. Add('end;');
  13207. Add('procedure tobject.setint(value: longint);');
  13208. Add('begin');
  13209. Add(' if value=fy then exit;');
  13210. Add(' fy:=value;');
  13211. Add('end;');
  13212. Add('procedure tobject.doit;');
  13213. Add('begin');
  13214. Add(' IntA:=IntA+1;');
  13215. Add(' Self.IntA:=Self.IntA+1;');
  13216. Add(' IntB:=IntB+1;');
  13217. Add(' Self.IntB:=Self.IntB+1;');
  13218. Add('end;');
  13219. Add('var Obj: tobject;');
  13220. Add('begin');
  13221. Add(' obj.inta:=obj.inta+1;');
  13222. Add(' if obj.intb=2 then;');
  13223. Add(' obj.intb:=obj.intb+2;');
  13224. Add(' obj.setint(obj.inta);');
  13225. ConvertProgram;
  13226. CheckSource('TestClass_Property',
  13227. LinesToStr([ // statements
  13228. 'rtl.createClass(this, "TObject", null, function () {',
  13229. ' this.$init = function () {',
  13230. ' this.Fx = 0;',
  13231. ' this.Fy = 0;',
  13232. ' };',
  13233. ' this.$final = function () {',
  13234. ' };',
  13235. ' this.GetInt = function () {',
  13236. ' var Result = 0;',
  13237. ' Result = this.Fx;',
  13238. ' return Result;',
  13239. ' };',
  13240. ' this.SetInt = function (Value) {',
  13241. ' if (Value === this.Fy) return;',
  13242. ' this.Fy = Value;',
  13243. ' };',
  13244. ' this.DoIt = function () {',
  13245. ' this.Fy = this.Fx + 1;',
  13246. ' this.Fy = this.Fx + 1;',
  13247. ' this.SetInt(this.GetInt() + 1);',
  13248. ' this.SetInt(this.GetInt() + 1);',
  13249. ' };',
  13250. '});',
  13251. 'this.Obj = null;'
  13252. ]),
  13253. LinesToStr([ // $mod.$main
  13254. '$mod.Obj.Fy = $mod.Obj.Fx + 1;',
  13255. 'if ($mod.Obj.GetInt() === 2);',
  13256. '$mod.Obj.SetInt($mod.Obj.GetInt() + 2);',
  13257. '$mod.Obj.SetInt($mod.Obj.Fx);'
  13258. ]));
  13259. end;
  13260. procedure TTestModule.TestClass_Property_ClassMethod;
  13261. begin
  13262. StartProgram(false);
  13263. Add([
  13264. 'type',
  13265. ' TObject = class',
  13266. ' class var Fx: longint;',
  13267. ' class var Fy: longint;',
  13268. ' class function GetInt: longint;',
  13269. ' class procedure SetInt(Value: longint);',
  13270. ' end;',
  13271. ' TBird = class',
  13272. ' class procedure DoIt;',
  13273. ' class property IntA: longint read Fx write Fy;',
  13274. ' class property IntB: longint read GetInt write SetInt;',
  13275. ' end;',
  13276. 'class function tobject.getint: longint;',
  13277. 'begin',
  13278. ' result:=fx;',
  13279. 'end;',
  13280. 'class procedure tobject.setint(value: longint);',
  13281. 'begin',
  13282. 'end;',
  13283. 'class procedure tbird.doit;',
  13284. 'begin',
  13285. ' FX:=3;',
  13286. ' IntA:=IntA+1;',
  13287. ' Self.IntA:=Self.IntA+1;',
  13288. ' IntB:=IntB+1;',
  13289. ' Self.IntB:=Self.IntB+1;',
  13290. ' with Self do begin',
  13291. ' FX:=11;',
  13292. ' IntA:=IntA+12;',
  13293. ' IntB:=IntB+13;',
  13294. ' end;',
  13295. 'end;',
  13296. 'var Obj: tbird;',
  13297. 'begin',
  13298. ' tbird.fx:=tbird.fx+1;',
  13299. ' tbird.inta:=tbird.inta+1;',
  13300. ' if tbird.intb=2 then;',
  13301. ' tbird.intb:=tbird.intb+2;',
  13302. ' tbird.setint(tbird.inta);',
  13303. ' obj.inta:=obj.inta+1;',
  13304. ' if obj.intb=2 then;',
  13305. ' obj.intb:=obj.intb+2;',
  13306. ' obj.setint(obj.inta);',
  13307. ' with Tbird do begin',
  13308. ' FX:=FY+1;',
  13309. ' inta:=inta+2;',
  13310. ' intb:=intb+3;',
  13311. ' end;',
  13312. ' with Obj do begin',
  13313. ' FX:=FY+1;',
  13314. ' inta:=inta+2;',
  13315. ' intb:=intb+3;',
  13316. ' end;',
  13317. '']);
  13318. ConvertProgram;
  13319. CheckSource('TestClass_Property_ClassMethod',
  13320. LinesToStr([ // statements
  13321. 'rtl.createClass(this, "TObject", null, function () {',
  13322. ' this.Fx = 0;',
  13323. ' this.Fy = 0;',
  13324. ' this.$init = function () {',
  13325. ' };',
  13326. ' this.$final = function () {',
  13327. ' };',
  13328. ' this.GetInt = function () {',
  13329. ' var Result = 0;',
  13330. ' Result = this.Fx;',
  13331. ' return Result;',
  13332. ' };',
  13333. ' this.SetInt = function (Value) {',
  13334. ' };',
  13335. '});',
  13336. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  13337. ' this.DoIt = function () {',
  13338. ' $mod.TObject.Fx = 3;',
  13339. ' $mod.TObject.Fy = this.Fx + 1;',
  13340. ' $mod.TObject.Fy = this.Fx + 1;',
  13341. ' this.SetInt(this.GetInt() + 1);',
  13342. ' this.SetInt(this.GetInt() + 1);',
  13343. ' $mod.TObject.Fx = 11;',
  13344. ' $mod.TObject.Fy = this.Fx + 12;',
  13345. ' this.SetInt(this.GetInt() + 13);',
  13346. ' };',
  13347. '});',
  13348. 'this.Obj = null;'
  13349. ]),
  13350. LinesToStr([ // $mod.$main
  13351. '$mod.TObject.Fx = $mod.TBird.Fx + 1;',
  13352. '$mod.TObject.Fy = $mod.TBird.Fx + 1;',
  13353. 'if ($mod.TBird.GetInt() === 2);',
  13354. '$mod.TBird.SetInt($mod.TBird.GetInt() + 2);',
  13355. '$mod.TBird.SetInt($mod.TBird.Fx);',
  13356. '$mod.TObject.Fy = $mod.Obj.Fx + 1;',
  13357. 'if ($mod.Obj.$class.GetInt() === 2);',
  13358. '$mod.Obj.$class.SetInt($mod.Obj.$class.GetInt() + 2);',
  13359. '$mod.Obj.$class.SetInt($mod.Obj.Fx);',
  13360. 'var $with = $mod.TBird;',
  13361. '$mod.TObject.Fx = $with.Fy + 1;',
  13362. '$mod.TObject.Fy = $with.Fx + 2;',
  13363. '$with.SetInt($with.GetInt() + 3);',
  13364. 'var $with1 = $mod.Obj;',
  13365. '$mod.TObject.Fx = $with1.Fy + 1;',
  13366. '$mod.TObject.Fy = $with1.Fx + 2;',
  13367. '$with1.$class.SetInt($with1.$class.GetInt() + 3);',
  13368. '']));
  13369. end;
  13370. procedure TTestModule.TestClass_Property_Indexed;
  13371. begin
  13372. StartProgram(false);
  13373. Add([
  13374. 'type',
  13375. ' TObject = class',
  13376. ' FItems: array of longint;',
  13377. ' function GetItems(Index: longint): longint;',
  13378. ' procedure SetItems(Index: longint; Value: longint);',
  13379. ' procedure DoIt;',
  13380. ' property Items[Index: longint]: longint read getitems write setitems;',
  13381. ' end;',
  13382. 'function tobject.getitems(index: longint): longint;',
  13383. 'begin',
  13384. ' Result:=fitems[index];',
  13385. 'end;',
  13386. 'procedure tobject.setitems(index: longint; value: longint);',
  13387. 'begin',
  13388. ' fitems[index]:=value;',
  13389. 'end;',
  13390. 'procedure tobject.doit;',
  13391. 'begin',
  13392. ' items[1]:=2;',
  13393. ' items[3]:=items[4];',
  13394. ' self.items[5]:=self.items[6];',
  13395. ' items[items[7]]:=items[items[8]];',
  13396. 'end;',
  13397. 'var Obj: tobject;',
  13398. 'begin',
  13399. ' obj.Items[11]:=obj.Items[12];',
  13400. '']);
  13401. ConvertProgram;
  13402. CheckSource('TestClass_Property_Indexed',
  13403. LinesToStr([ // statements
  13404. 'rtl.createClass(this, "TObject", null, function () {',
  13405. ' this.$init = function () {',
  13406. ' this.FItems = [];',
  13407. ' };',
  13408. ' this.$final = function () {',
  13409. ' this.FItems = undefined;',
  13410. ' };',
  13411. ' this.GetItems = function (Index) {',
  13412. ' var Result = 0;',
  13413. ' Result = this.FItems[Index];',
  13414. ' return Result;',
  13415. ' };',
  13416. ' this.SetItems = function (Index, Value) {',
  13417. ' this.FItems[Index] = Value;',
  13418. ' };',
  13419. ' this.DoIt = function () {',
  13420. ' this.SetItems(1, 2);',
  13421. ' this.SetItems(3,this.GetItems(4));',
  13422. ' this.SetItems(5,this.GetItems(6));',
  13423. ' this.SetItems(this.GetItems(7), this.GetItems(this.GetItems(8)));',
  13424. ' };',
  13425. '});',
  13426. 'this.Obj = null;'
  13427. ]),
  13428. LinesToStr([ // $mod.$main
  13429. '$mod.Obj.SetItems(11,$mod.Obj.GetItems(12));'
  13430. ]));
  13431. end;
  13432. procedure TTestModule.TestClass_Property_IndexSpec;
  13433. begin
  13434. StartProgram(false);
  13435. Add([
  13436. 'type',
  13437. ' TEnum = (red, blue);',
  13438. ' TObject = class',
  13439. ' function GetIntBool(Index: longint): boolean; virtual; abstract;',
  13440. ' procedure SetIntBool(Index: longint; b: boolean); virtual; abstract;',
  13441. ' function GetEnumBool(Index: TEnum): boolean; virtual; abstract;',
  13442. ' procedure SetEnumBool(Index: TEnum; b: boolean); virtual; abstract;',
  13443. ' function GetStrIntBool(A: String; I: longint): boolean; virtual; abstract;',
  13444. ' procedure SetStrIntBool(A: String; I: longint; b: boolean); virtual; abstract;',
  13445. ' property B1: boolean index 1 read GetIntBool write SetIntBool;',
  13446. ' property B2: boolean index TEnum.blue read GetEnumBool write SetEnumBool;',
  13447. ' property B3: boolean index ord(red) read GetIntBool write SetIntBool;',
  13448. ' property I1[A: String]: boolean index ord(blue) read GetStrIntBool write SetStrIntBool;',
  13449. ' end;',
  13450. 'procedure DoIt(b: boolean); begin end;',
  13451. 'var',
  13452. ' o: TObject;',
  13453. 'begin',
  13454. ' o.B1:=o.B1;',
  13455. ' o.B2:=o.B2;',
  13456. ' o.B3:=o.B3;',
  13457. ' o.I1[''a'']:=o.I1[''b''];',
  13458. ' doit(o.b1);',
  13459. ' doit(o.b2);',
  13460. ' doit(o.i1[''c'']);',
  13461. '']);
  13462. ConvertProgram;
  13463. CheckSource('TestClass_Property_IndexSpec',
  13464. LinesToStr([ // statements
  13465. 'this.TEnum = {',
  13466. ' "0": "red",',
  13467. ' red: 0,',
  13468. ' "1": "blue",',
  13469. ' blue: 1',
  13470. '};',
  13471. 'rtl.createClass(this, "TObject", null, function () {',
  13472. ' this.$init = function () {',
  13473. ' };',
  13474. ' this.$final = function () {',
  13475. ' };',
  13476. '});',
  13477. 'this.DoIt = function (b) {',
  13478. '};',
  13479. 'this.o = null;',
  13480. '']),
  13481. LinesToStr([ // $mod.$main
  13482. '$mod.o.SetIntBool(1, $mod.o.GetIntBool(1));',
  13483. '$mod.o.SetEnumBool($mod.TEnum.blue, $mod.o.GetEnumBool($mod.TEnum.blue));',
  13484. '$mod.o.SetIntBool(0, $mod.o.GetIntBool(0));',
  13485. '$mod.o.SetStrIntBool("a", 1, $mod.o.GetStrIntBool("b", 1));',
  13486. '$mod.DoIt($mod.o.GetIntBool(1));',
  13487. '$mod.DoIt($mod.o.GetEnumBool($mod.TEnum.blue));',
  13488. '$mod.DoIt($mod.o.GetStrIntBool("c", 1));',
  13489. '']));
  13490. end;
  13491. procedure TTestModule.TestClass_PropertyOfTypeArray;
  13492. begin
  13493. StartProgram(false);
  13494. Add('type');
  13495. Add(' TArray = array of longint;');
  13496. Add(' TObject = class');
  13497. Add(' FItems: TArray;');
  13498. Add(' function GetItems: tarray;');
  13499. Add(' procedure SetItems(Value: tarray);');
  13500. Add(' property Items: tarray read getitems write setitems;');
  13501. Add(' procedure SetNumbers(const Value: tarray);');
  13502. Add(' property Numbers: tarray write setnumbers;');
  13503. Add(' end;');
  13504. Add('function tobject.getitems: tarray;');
  13505. Add('begin');
  13506. Add(' Result:=fitems;');
  13507. Add('end;');
  13508. Add('procedure tobject.setitems(value: tarray);');
  13509. Add('begin');
  13510. Add(' fitems:=value;');
  13511. Add(' fitems:=nil;');
  13512. Add(' Items:=nil;');
  13513. Add(' Items:=Items;');
  13514. Add(' Items[1]:=2;');
  13515. Add(' fitems[3]:=Items[4];');
  13516. Add(' Items[5]:=Items[6];');
  13517. Add(' Self.Items[7]:=8;');
  13518. Add(' Self.Items[9]:=Self.Items[10];');
  13519. Add(' Items[Items[11]]:=Items[Items[12]];');
  13520. Add('end;');
  13521. Add('procedure tobject.SetNumbers(const Value: tarray);');
  13522. Add('begin;');
  13523. Add(' Numbers:=nil;');
  13524. Add(' Numbers:=Value;');
  13525. Add(' Self.Numbers:=Value;');
  13526. Add('end;');
  13527. Add('var Obj: tobject;');
  13528. Add('begin');
  13529. Add(' obj.items:=nil;');
  13530. Add(' obj.items:=obj.items;');
  13531. Add(' obj.items[11]:=obj.items[12];');
  13532. ConvertProgram;
  13533. CheckSource('TestClass_PropertyOfTypeArray',
  13534. LinesToStr([ // statements
  13535. 'rtl.createClass(this, "TObject", null, function () {',
  13536. ' this.$init = function () {',
  13537. ' this.FItems = [];',
  13538. ' };',
  13539. ' this.$final = function () {',
  13540. ' this.FItems = undefined;',
  13541. ' };',
  13542. ' this.GetItems = function () {',
  13543. ' var Result = [];',
  13544. ' Result = rtl.arrayRef(this.FItems);',
  13545. ' return Result;',
  13546. ' };',
  13547. ' this.SetItems = function (Value) {',
  13548. ' this.FItems = rtl.arrayRef(Value);',
  13549. ' this.FItems = [];',
  13550. ' this.SetItems([]);',
  13551. ' this.SetItems(rtl.arrayRef(this.GetItems()));',
  13552. ' this.GetItems()[1] = 2;',
  13553. ' this.FItems[3] = this.GetItems()[4];',
  13554. ' this.GetItems()[5] = this.GetItems()[6];',
  13555. ' this.GetItems()[7] = 8;',
  13556. ' this.GetItems()[9] = this.GetItems()[10];',
  13557. ' this.GetItems()[this.GetItems()[11]] = this.GetItems()[this.GetItems()[12]];',
  13558. ' };',
  13559. ' this.SetNumbers = function (Value) {',
  13560. ' this.SetNumbers([]);',
  13561. ' this.SetNumbers(Value);',
  13562. ' this.SetNumbers(Value);',
  13563. ' };',
  13564. '});',
  13565. 'this.Obj = null;'
  13566. ]),
  13567. LinesToStr([ // $mod.$main
  13568. '$mod.Obj.SetItems([]);',
  13569. '$mod.Obj.SetItems($mod.Obj.GetItems());',
  13570. '$mod.Obj.GetItems()[11] = $mod.Obj.GetItems()[12];'
  13571. ]));
  13572. end;
  13573. procedure TTestModule.TestClass_PropertyDefault;
  13574. begin
  13575. StartProgram(false);
  13576. Add([
  13577. 'type',
  13578. ' TArray = array of longint;',
  13579. ' TObject = class',
  13580. ' end;',
  13581. ' TBird = class',
  13582. ' FItems: TArray;',
  13583. ' function GetItems(Index: longint): longint;',
  13584. ' procedure SetItems(Index, Value: longint);',
  13585. ' property Items[Index: longint]: longint read getitems write setitems; default;',
  13586. ' end;',
  13587. 'function TBird.getitems(index: longint): longint;',
  13588. 'begin',
  13589. 'end;',
  13590. 'procedure TBird.setitems(index, value: longint);',
  13591. 'begin',
  13592. ' Self[1]:=2;',
  13593. ' Self[3]:=Self[index];',
  13594. ' Self[index]:=Self[Self[value]];',
  13595. ' Self[Self[4]]:=value;',
  13596. 'end;',
  13597. 'var',
  13598. ' Bird: TBird;',
  13599. ' Obj: TObject;',
  13600. 'begin',
  13601. ' bird[11]:=12;',
  13602. ' bird[13]:=bird[14];',
  13603. ' bird[Bird[15]]:=bird[Bird[15]];',
  13604. ' TBird(obj)[16]:=TBird(obj)[17];',
  13605. ' (obj as tbird)[18]:=19;',
  13606. '']);
  13607. ConvertProgram;
  13608. CheckSource('TestClass_PropertyDefault',
  13609. LinesToStr([ // statements
  13610. 'rtl.createClass(this, "TObject", null, function () {',
  13611. ' this.$init = function () {',
  13612. ' };',
  13613. ' this.$final = function () {',
  13614. ' };',
  13615. '});',
  13616. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  13617. ' this.$init = function () {',
  13618. ' $mod.TObject.$init.call(this);',
  13619. ' this.FItems = [];',
  13620. ' };',
  13621. ' this.$final = function () {',
  13622. ' this.FItems = undefined;',
  13623. ' $mod.TObject.$final.call(this);',
  13624. ' };',
  13625. ' this.GetItems = function (Index) {',
  13626. ' var Result = 0;',
  13627. ' return Result;',
  13628. ' };',
  13629. ' this.SetItems = function (Index, Value) {',
  13630. ' this.SetItems(1, 2);',
  13631. ' this.SetItems(3, this.GetItems(Index));',
  13632. ' this.SetItems(Index, this.GetItems(this.GetItems(Value)));',
  13633. ' this.SetItems(this.GetItems(4), Value);',
  13634. ' };',
  13635. '});',
  13636. 'this.Bird = null;',
  13637. 'this.Obj = null;',
  13638. '']),
  13639. LinesToStr([ // $mod.$main
  13640. '$mod.Bird.SetItems(11, 12);',
  13641. '$mod.Bird.SetItems(13, $mod.Bird.GetItems(14));',
  13642. '$mod.Bird.SetItems($mod.Bird.GetItems(15), $mod.Bird.GetItems($mod.Bird.GetItems(15)));',
  13643. '$mod.Obj.SetItems(16, $mod.Obj.GetItems(17));',
  13644. 'rtl.as($mod.Obj, $mod.TBird).SetItems(18, 19);',
  13645. '']));
  13646. end;
  13647. procedure TTestModule.TestClass_PropertyDefault_TypecastToOtherDefault;
  13648. begin
  13649. StartProgram(false);
  13650. Add([
  13651. 'type',
  13652. ' TObject = class end;',
  13653. ' TAlphaList = class',
  13654. ' function GetAlphas(Index: boolean): Pointer; virtual; abstract;',
  13655. ' procedure SetAlphas(Index: boolean; Value: Pointer); virtual; abstract;',
  13656. ' property Alphas[Index: boolean]: Pointer read getAlphas write setAlphas; default;',
  13657. ' end;',
  13658. ' TBetaList = class',
  13659. ' function GetBetas(Index: longint): Pointer; virtual; abstract;',
  13660. ' procedure SetBetas(Index: longint; Value: Pointer); virtual; abstract;',
  13661. ' property Betas[Index: longint]: Pointer read getBetas write setBetas; default;',
  13662. ' end;',
  13663. ' TBird = class',
  13664. ' procedure DoIt;',
  13665. ' end;',
  13666. 'procedure TBird.DoIt;',
  13667. 'var',
  13668. ' List: TAlphaList;',
  13669. 'begin',
  13670. ' if TBetaList(List[true])[3]=nil then ;',
  13671. ' TBetaList(List[false])[5]:=nil;',
  13672. 'end;',
  13673. 'var',
  13674. ' List: TAlphaList;',
  13675. 'begin',
  13676. ' if TBetaList(List[true])[3]=nil then ;',
  13677. ' TBetaList(List[false])[5]:=nil;',
  13678. '']);
  13679. ConvertProgram;
  13680. CheckSource('TestClass_PropertyDefault_TypecastToOtherDefault',
  13681. LinesToStr([ // statements
  13682. 'rtl.createClass(this, "TObject", null, function () {',
  13683. ' this.$init = function () {',
  13684. ' };',
  13685. ' this.$final = function () {',
  13686. ' };',
  13687. '});',
  13688. 'rtl.createClass(this, "TAlphaList", this.TObject, function () {',
  13689. '});',
  13690. 'rtl.createClass(this, "TBetaList", this.TObject, function () {',
  13691. '});',
  13692. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  13693. ' this.DoIt = function () {',
  13694. ' var List = null;',
  13695. ' if (List.GetAlphas(true).GetBetas(3) === null) ;',
  13696. ' List.GetAlphas(false).SetBetas(5, null);',
  13697. ' };',
  13698. '});',
  13699. 'this.List = null;',
  13700. '']),
  13701. LinesToStr([ // $mod.$main
  13702. 'if ($mod.List.GetAlphas(true).GetBetas(3) === null) ;',
  13703. '$mod.List.GetAlphas(false).SetBetas(5, null);',
  13704. '']));
  13705. end;
  13706. procedure TTestModule.TestClass_PropertyOverride;
  13707. begin
  13708. StartProgram(false);
  13709. Add('type');
  13710. Add(' integer = longint;');
  13711. Add(' TObject = class');
  13712. Add(' FItem: integer;');
  13713. Add(' function GetItem: integer; external name ''GetItem'';');
  13714. Add(' procedure SetItem(Value: integer); external name ''SetItem'';');
  13715. Add(' property Item: integer read getitem write setitem;');
  13716. Add(' end;');
  13717. Add(' TCar = class');
  13718. Add(' FBag: integer;');
  13719. Add(' function GetBag: integer; external name ''GetBag'';');
  13720. Add(' property Item read getbag;');
  13721. Add(' end;');
  13722. Add('var');
  13723. Add(' Obj: tobject;');
  13724. Add(' Car: tcar;');
  13725. Add('begin');
  13726. Add(' Obj.Item:=Obj.Item;');
  13727. Add(' Car.Item:=Car.Item;');
  13728. ConvertProgram;
  13729. CheckSource('TestClass_PropertyOverride',
  13730. LinesToStr([ // statements
  13731. 'rtl.createClass(this, "TObject", null, function () {',
  13732. ' this.$init = function () {',
  13733. ' this.FItem = 0;',
  13734. ' };',
  13735. ' this.$final = function () {',
  13736. ' };',
  13737. '});',
  13738. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  13739. ' this.$init = function () {',
  13740. ' $mod.TObject.$init.call(this);',
  13741. ' this.FBag = 0;',
  13742. ' };',
  13743. '});',
  13744. 'this.Obj = null;',
  13745. 'this.Car = null;',
  13746. '']),
  13747. LinesToStr([ // $mod.$main
  13748. '$mod.Obj.SetItem($mod.Obj.GetItem());',
  13749. '$mod.Car.SetItem($mod.Car.GetBag());',
  13750. '']));
  13751. end;
  13752. procedure TTestModule.TestClass_PropertyIncVisibility;
  13753. begin
  13754. AddModuleWithIntfImplSrc('unit1.pp',
  13755. LinesToStr([
  13756. 'type',
  13757. ' TNumber = longint;',
  13758. ' TInteger = longint;',
  13759. ' TObject = class',
  13760. ' private',
  13761. ' function GetItems(Index: TNumber): TInteger; virtual; abstract;',
  13762. ' procedure SetItems(Index: TInteger; Value: TNumber); virtual; abstract;',
  13763. ' protected',
  13764. ' property Items[Index: TNumber]: longint read GetItems write SetItems;',
  13765. ' end;']),
  13766. LinesToStr([
  13767. '']));
  13768. StartProgram(true);
  13769. Add([
  13770. 'uses unit1;',
  13771. 'type',
  13772. ' TBird = class',
  13773. ' public',
  13774. ' property Items;',
  13775. ' end;',
  13776. 'procedure DoIt(i: TInteger);',
  13777. 'begin',
  13778. 'end;',
  13779. 'var b: TBird;',
  13780. 'begin',
  13781. ' b.Items[1]:=2;',
  13782. ' b.Items[3]:=b.Items[4];',
  13783. ' DoIt(b.Items[5]);',
  13784. '']);
  13785. ConvertProgram;
  13786. CheckSource('TestClass_PropertyIncVisibility',
  13787. LinesToStr([ // statements
  13788. 'rtl.createClass(this, "TBird", pas.unit1.TObject, function () {',
  13789. '});',
  13790. 'this.DoIt = function (i) {',
  13791. '};',
  13792. 'this.b = null;'
  13793. ]),
  13794. LinesToStr([ // $mod.$main
  13795. '$mod.b.SetItems(1, 2);',
  13796. '$mod.b.SetItems(3, $mod.b.GetItems(4));',
  13797. '$mod.DoIt($mod.b.GetItems(5));'
  13798. ]));
  13799. end;
  13800. procedure TTestModule.TestClass_Assigned;
  13801. begin
  13802. StartProgram(false);
  13803. Add('type');
  13804. Add(' TObject = class');
  13805. Add(' end;');
  13806. Add('var');
  13807. Add(' Obj: tobject;');
  13808. Add(' b: boolean;');
  13809. Add('begin');
  13810. Add(' if Assigned(obj) then ;');
  13811. Add(' b:=Assigned(obj) or false;');
  13812. ConvertProgram;
  13813. CheckSource('TestClass_Assigned',
  13814. LinesToStr([ // statements
  13815. 'rtl.createClass(this, "TObject", null, function () {',
  13816. ' this.$init = function () {',
  13817. ' };',
  13818. ' this.$final = function () {',
  13819. ' };',
  13820. '});',
  13821. 'this.Obj = null;',
  13822. 'this.b = false;'
  13823. ]),
  13824. LinesToStr([ // $mod.$main
  13825. 'if ($mod.Obj != null);',
  13826. '$mod.b = ($mod.Obj != null) || false;'
  13827. ]));
  13828. end;
  13829. procedure TTestModule.TestClass_WithClassDoCreate;
  13830. begin
  13831. StartProgram(false);
  13832. Add('type');
  13833. Add(' TObject = class');
  13834. Add(' aBool: boolean;');
  13835. Add(' Arr: array of boolean;');
  13836. Add(' constructor Create;');
  13837. Add(' end;');
  13838. Add('constructor TObject.Create; begin end;');
  13839. Add('var');
  13840. Add(' Obj: tobject;');
  13841. Add(' b: boolean;');
  13842. Add('begin');
  13843. Add(' with tobject.create do begin');
  13844. Add(' b:=abool;');
  13845. Add(' abool:=b;');
  13846. Add(' b:=arr[1];');
  13847. Add(' arr[2]:=b;');
  13848. Add(' end;');
  13849. Add(' with tobject do');
  13850. Add(' obj:=create;');
  13851. Add(' with obj do begin');
  13852. Add(' create;');
  13853. Add(' b:=abool;');
  13854. Add(' abool:=b;');
  13855. Add(' b:=arr[3];');
  13856. Add(' arr[4]:=b;');
  13857. Add(' end;');
  13858. ConvertProgram;
  13859. CheckSource('TestClass_WithClassDoCreate',
  13860. LinesToStr([ // statements
  13861. 'rtl.createClass(this, "TObject", null, function () {',
  13862. ' this.$init = function () {',
  13863. ' this.aBool = false;',
  13864. ' this.Arr = [];',
  13865. ' };',
  13866. ' this.$final = function () {',
  13867. ' this.Arr = undefined;',
  13868. ' };',
  13869. ' this.Create = function () {',
  13870. ' return this;',
  13871. ' };',
  13872. '});',
  13873. 'this.Obj = null;',
  13874. 'this.b = false;'
  13875. ]),
  13876. LinesToStr([ // $mod.$main
  13877. 'var $with = $mod.TObject.$create("Create");',
  13878. '$mod.b = $with.aBool;',
  13879. '$with.aBool = $mod.b;',
  13880. '$mod.b = $with.Arr[1];',
  13881. '$with.Arr[2] = $mod.b;',
  13882. 'var $with1 = $mod.TObject;',
  13883. '$mod.Obj = $with1.$create("Create");',
  13884. 'var $with2 = $mod.Obj;',
  13885. '$with2.Create();',
  13886. '$mod.b = $with2.aBool;',
  13887. '$with2.aBool = $mod.b;',
  13888. '$mod.b = $with2.Arr[3];',
  13889. '$with2.Arr[4] = $mod.b;',
  13890. '']));
  13891. end;
  13892. procedure TTestModule.TestClass_WithClassInstDoProperty;
  13893. begin
  13894. StartProgram(false);
  13895. Add('type');
  13896. Add(' TObject = class');
  13897. Add(' FInt: longint;');
  13898. Add(' constructor Create;');
  13899. Add(' function GetSize: longint;');
  13900. Add(' procedure SetSize(Value: longint);');
  13901. Add(' property Int: longint read FInt write FInt;');
  13902. Add(' property Size: longint read GetSize write SetSize;');
  13903. Add(' end;');
  13904. Add('constructor TObject.Create; begin end;');
  13905. Add('function TObject.GetSize: longint; begin; end;');
  13906. Add('procedure TObject.SetSize(Value: longint); begin; end;');
  13907. Add('var');
  13908. Add(' Obj: tobject;');
  13909. Add(' i: longint;');
  13910. Add('begin');
  13911. Add(' with TObject.Create do begin');
  13912. Add(' i:=int;');
  13913. Add(' int:=i;');
  13914. Add(' i:=size;');
  13915. Add(' size:=i;');
  13916. Add(' end;');
  13917. Add(' with obj do begin');
  13918. Add(' i:=int;');
  13919. Add(' int:=i;');
  13920. Add(' i:=size;');
  13921. Add(' size:=i;');
  13922. Add(' end;');
  13923. ConvertProgram;
  13924. CheckSource('TestClass_WithClassInstDoProperty',
  13925. LinesToStr([ // statements
  13926. 'rtl.createClass(this, "TObject", null, function () {',
  13927. ' this.$init = function () {',
  13928. ' this.FInt = 0;',
  13929. ' };',
  13930. ' this.$final = function () {',
  13931. ' };',
  13932. ' this.Create = function () {',
  13933. ' return this;',
  13934. ' };',
  13935. ' this.GetSize = function () {',
  13936. ' var Result = 0;',
  13937. ' return Result;',
  13938. ' };',
  13939. ' this.SetSize = function (Value) {',
  13940. ' };',
  13941. '});',
  13942. 'this.Obj = null;',
  13943. 'this.i = 0;'
  13944. ]),
  13945. LinesToStr([ // $mod.$main
  13946. 'var $with = $mod.TObject.$create("Create");',
  13947. '$mod.i = $with.FInt;',
  13948. '$with.FInt = $mod.i;',
  13949. '$mod.i = $with.GetSize();',
  13950. '$with.SetSize($mod.i);',
  13951. 'var $with1 = $mod.Obj;',
  13952. '$mod.i = $with1.FInt;',
  13953. '$with1.FInt = $mod.i;',
  13954. '$mod.i = $with1.GetSize();',
  13955. '$with1.SetSize($mod.i);',
  13956. '']));
  13957. end;
  13958. procedure TTestModule.TestClass_WithClassInstDoPropertyWithParams;
  13959. begin
  13960. StartProgram(false);
  13961. Add('type');
  13962. Add(' TObject = class');
  13963. Add(' constructor Create;');
  13964. Add(' function GetItems(Index: longint): longint;');
  13965. Add(' procedure SetItems(Index, Value: longint);');
  13966. Add(' property Items[Index: longint]: longint read GetItems write SetItems;');
  13967. Add(' end;');
  13968. Add('constructor TObject.Create; begin end;');
  13969. Add('function tobject.getitems(index: longint): longint; begin; end;');
  13970. Add('procedure tobject.setitems(index, value: longint); begin; end;');
  13971. Add('var');
  13972. Add(' Obj: tobject;');
  13973. Add(' i: longint;');
  13974. Add('begin');
  13975. Add(' with TObject.Create do begin');
  13976. Add(' i:=Items[1];');
  13977. Add(' Items[2]:=i;');
  13978. Add(' end;');
  13979. Add(' with obj do begin');
  13980. Add(' i:=Items[3];');
  13981. Add(' Items[4]:=i;');
  13982. Add(' end;');
  13983. ConvertProgram;
  13984. CheckSource('TestClass_WithClassInstDoPropertyWithParams',
  13985. LinesToStr([ // statements
  13986. 'rtl.createClass(this, "TObject", null, function () {',
  13987. ' this.$init = function () {',
  13988. ' };',
  13989. ' this.$final = function () {',
  13990. ' };',
  13991. ' this.Create = function () {',
  13992. ' return this;',
  13993. ' };',
  13994. ' this.GetItems = function (Index) {',
  13995. ' var Result = 0;',
  13996. ' return Result;',
  13997. ' };',
  13998. ' this.SetItems = function (Index, Value) {',
  13999. ' };',
  14000. '});',
  14001. 'this.Obj = null;',
  14002. 'this.i = 0;'
  14003. ]),
  14004. LinesToStr([ // $mod.$main
  14005. 'var $with = $mod.TObject.$create("Create");',
  14006. '$mod.i = $with.GetItems(1);',
  14007. '$with.SetItems(2, $mod.i);',
  14008. 'var $with1 = $mod.Obj;',
  14009. '$mod.i = $with1.GetItems(3);',
  14010. '$with1.SetItems(4, $mod.i);',
  14011. '']));
  14012. end;
  14013. procedure TTestModule.TestClass_WithClassInstDoFunc;
  14014. begin
  14015. StartProgram(false);
  14016. Add('type');
  14017. Add(' TObject = class');
  14018. Add(' constructor Create;');
  14019. Add(' function GetSize: longint;');
  14020. Add(' procedure SetSize(Value: longint);');
  14021. Add(' end;');
  14022. Add('constructor TObject.Create; begin end;');
  14023. Add('function TObject.GetSize: longint; begin; end;');
  14024. Add('procedure TObject.SetSize(Value: longint); begin; end;');
  14025. Add('var');
  14026. Add(' Obj: tobject;');
  14027. Add(' i: longint;');
  14028. Add('begin');
  14029. Add(' with TObject.Create do begin');
  14030. Add(' i:=GetSize;');
  14031. Add(' i:=GetSize();');
  14032. Add(' SetSize(i);');
  14033. Add(' end;');
  14034. Add(' with obj do begin');
  14035. Add(' i:=GetSize;');
  14036. Add(' i:=GetSize();');
  14037. Add(' SetSize(i);');
  14038. Add(' end;');
  14039. ConvertProgram;
  14040. CheckSource('TestClass_WithClassInstDoFunc',
  14041. LinesToStr([ // statements
  14042. 'rtl.createClass(this, "TObject", null, function () {',
  14043. ' this.$init = function () {',
  14044. ' };',
  14045. ' this.$final = function () {',
  14046. ' };',
  14047. ' this.Create = function () {',
  14048. ' return this;',
  14049. ' };',
  14050. ' this.GetSize = function () {',
  14051. ' var Result = 0;',
  14052. ' return Result;',
  14053. ' };',
  14054. ' this.SetSize = function (Value) {',
  14055. ' };',
  14056. '});',
  14057. 'this.Obj = null;',
  14058. 'this.i = 0;'
  14059. ]),
  14060. LinesToStr([ // $mod.$main
  14061. 'var $with = $mod.TObject.$create("Create");',
  14062. '$mod.i = $with.GetSize();',
  14063. '$mod.i = $with.GetSize();',
  14064. '$with.SetSize($mod.i);',
  14065. 'var $with1 = $mod.Obj;',
  14066. '$mod.i = $with1.GetSize();',
  14067. '$mod.i = $with1.GetSize();',
  14068. '$with1.SetSize($mod.i);',
  14069. '']));
  14070. end;
  14071. procedure TTestModule.TestClass_TypeCast;
  14072. begin
  14073. StartProgram(false);
  14074. Add('type');
  14075. Add(' TObject = class');
  14076. Add(' Next: TObject;');
  14077. Add(' constructor Create;');
  14078. Add(' end;');
  14079. Add(' TControl = class(TObject)');
  14080. Add(' Arr: array of TObject;');
  14081. Add(' function GetIt(vI: longint = 0): TObject;');
  14082. Add(' end;');
  14083. Add('constructor tobject.create; begin end;');
  14084. Add('function tcontrol.getit(vi: longint = 0): tobject; begin end;');
  14085. Add('var');
  14086. Add(' Obj: tobject;');
  14087. Add('begin');
  14088. Add(' obj:=tcontrol(obj).next;');
  14089. Add(' tcontrol(obj):=nil;');
  14090. Add(' obj:=tcontrol(obj);');
  14091. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit);');
  14092. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit());');
  14093. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit(1));');
  14094. Add(' tcontrol(obj):=tcontrol(tcontrol(tcontrol(obj).getit).arr[2]);');
  14095. Add(' obj:=tcontrol(nil);');
  14096. ConvertProgram;
  14097. CheckSource('TestClass_TypeCast',
  14098. LinesToStr([ // statements
  14099. 'rtl.createClass(this, "TObject", null, function () {',
  14100. ' this.$init = function () {',
  14101. ' this.Next = null;',
  14102. ' };',
  14103. ' this.$final = function () {',
  14104. ' this.Next = undefined;',
  14105. ' };',
  14106. ' this.Create = function () {',
  14107. ' return this;',
  14108. ' };',
  14109. '});',
  14110. 'rtl.createClass(this, "TControl", this.TObject, function () {',
  14111. ' this.$init = function () {',
  14112. ' $mod.TObject.$init.call(this);',
  14113. ' this.Arr = [];',
  14114. ' };',
  14115. ' this.$final = function () {',
  14116. ' this.Arr = undefined;',
  14117. ' $mod.TObject.$final.call(this);',
  14118. ' };',
  14119. ' this.GetIt = function (vI) {',
  14120. ' var Result = null;',
  14121. ' return Result;',
  14122. ' };',
  14123. '});',
  14124. 'this.Obj = null;'
  14125. ]),
  14126. LinesToStr([ // $mod.$main
  14127. '$mod.Obj = $mod.Obj.Next;',
  14128. '$mod.Obj = null;',
  14129. '$mod.Obj = $mod.Obj;',
  14130. '$mod.Obj = $mod.Obj.GetIt(0);',
  14131. '$mod.Obj = $mod.Obj.GetIt(0);',
  14132. '$mod.Obj = $mod.Obj.GetIt(1);',
  14133. '$mod.Obj = $mod.Obj.GetIt(0).Arr[2];',
  14134. '$mod.Obj = null;',
  14135. '']));
  14136. end;
  14137. procedure TTestModule.TestClass_TypeCastUntypedParam;
  14138. begin
  14139. StartProgram(false);
  14140. Add('type');
  14141. Add(' TObject = class end;');
  14142. Add('procedure ProcA(var A);');
  14143. Add('begin');
  14144. Add(' TObject(A):=nil;');
  14145. Add(' TObject(A):=TObject(A);');
  14146. Add(' if TObject(A)=nil then ;');
  14147. Add(' if nil=TObject(A) then ;');
  14148. Add('end;');
  14149. Add('procedure ProcB(out A);');
  14150. Add('begin');
  14151. Add(' TObject(A):=nil;');
  14152. Add(' TObject(A):=TObject(A);');
  14153. Add(' if TObject(A)=nil then ;');
  14154. Add(' if nil=TObject(A) then ;');
  14155. Add('end;');
  14156. Add('procedure ProcC(const A);');
  14157. Add('begin');
  14158. Add(' if TObject(A)=nil then ;');
  14159. Add(' if nil=TObject(A) then ;');
  14160. Add('end;');
  14161. Add('var o: TObject;');
  14162. Add('begin');
  14163. Add(' ProcA(o);');
  14164. Add(' ProcB(o);');
  14165. Add(' ProcC(o);');
  14166. ConvertProgram;
  14167. CheckSource('TestClass_TypeCastUntypedParam',
  14168. LinesToStr([ // statements
  14169. 'rtl.createClass(this, "TObject", null, function () {',
  14170. ' this.$init = function () {',
  14171. ' };',
  14172. ' this.$final = function () {',
  14173. ' };',
  14174. '});',
  14175. 'this.ProcA = function (A) {',
  14176. ' A.set(null);',
  14177. ' A.set(A.get());',
  14178. ' if (A.get() === null);',
  14179. ' if (null === A.get());',
  14180. '};',
  14181. 'this.ProcB = function (A) {',
  14182. ' A.set(null);',
  14183. ' A.set(A.get());',
  14184. ' if (A.get() === null);',
  14185. ' if (null === A.get());',
  14186. '};',
  14187. 'this.ProcC = function (A) {',
  14188. ' if (A === null);',
  14189. ' if (null === A);',
  14190. '};',
  14191. 'this.o = null;',
  14192. '']),
  14193. LinesToStr([ // $mod.$main
  14194. '$mod.ProcA({',
  14195. ' p: $mod,',
  14196. ' get: function () {',
  14197. ' return this.p.o;',
  14198. ' },',
  14199. ' set: function (v) {',
  14200. ' this.p.o = v;',
  14201. ' }',
  14202. '});',
  14203. '$mod.ProcB({',
  14204. ' p: $mod,',
  14205. ' get: function () {',
  14206. ' return this.p.o;',
  14207. ' },',
  14208. ' set: function (v) {',
  14209. ' this.p.o = v;',
  14210. ' }',
  14211. '});',
  14212. '$mod.ProcC($mod.o);',
  14213. '']));
  14214. end;
  14215. procedure TTestModule.TestClass_Overloads;
  14216. begin
  14217. StartProgram(false);
  14218. Add('type');
  14219. Add(' TObject = class');
  14220. Add(' procedure DoIt;');
  14221. Add(' procedure DoIt(vI: longint);');
  14222. Add(' end;');
  14223. Add('procedure TObject.DoIt;');
  14224. Add('begin');
  14225. Add(' DoIt;');
  14226. Add(' DoIt(1);');
  14227. Add('end;');
  14228. Add('procedure TObject.DoIt(vI: longint); begin end;');
  14229. Add('begin');
  14230. ConvertProgram;
  14231. CheckSource('TestClass_Overloads',
  14232. LinesToStr([ // statements
  14233. 'rtl.createClass(this, "TObject", null, function () {',
  14234. ' this.$init = function () {',
  14235. ' };',
  14236. ' this.$final = function () {',
  14237. ' };',
  14238. ' this.DoIt = function () {',
  14239. ' this.DoIt();',
  14240. ' this.DoIt$1(1);',
  14241. ' };',
  14242. ' this.DoIt$1 = function (vI) {',
  14243. ' };',
  14244. '});',
  14245. '']),
  14246. LinesToStr([ // $mod.$main
  14247. '']));
  14248. end;
  14249. procedure TTestModule.TestClass_OverloadsAncestor;
  14250. begin
  14251. StartProgram(false);
  14252. Add('type');
  14253. Add(' TObject = class;');
  14254. Add(' TObject = class');
  14255. Add(' procedure DoIt(vA: longint);');
  14256. Add(' procedure DoIt(vA, vB: longint);');
  14257. Add(' end;');
  14258. Add(' TCar = class;');
  14259. Add(' TCar = class');
  14260. Add(' procedure DoIt(vA: longint);');
  14261. Add(' procedure DoIt(vA, vB: longint);');
  14262. Add(' end;');
  14263. Add('procedure tobject.doit(va: longint);');
  14264. Add('begin');
  14265. Add(' doit(1);');
  14266. Add(' doit(1,2);');
  14267. Add('end;');
  14268. Add('procedure tobject.doit(va, vb: longint); begin end;');
  14269. Add('procedure tcar.doit(va: longint);');
  14270. Add('begin');
  14271. Add(' doit(1);');
  14272. Add(' doit(1,2);');
  14273. Add(' inherited doit(1);');
  14274. Add(' inherited doit(1,2);');
  14275. Add('end;');
  14276. Add('procedure tcar.doit(va, vb: longint); begin end;');
  14277. Add('begin');
  14278. ConvertProgram;
  14279. CheckSource('TestClass_OverloadsAncestor',
  14280. LinesToStr([ // statements
  14281. 'rtl.createClass(this, "TObject", null, function () {',
  14282. ' this.$init = function () {',
  14283. ' };',
  14284. ' this.$final = function () {',
  14285. ' };',
  14286. ' this.DoIt = function (vA) {',
  14287. ' this.DoIt(1);',
  14288. ' this.DoIt$1(1,2);',
  14289. ' };',
  14290. ' this.DoIt$1 = function (vA, vB) {',
  14291. ' };',
  14292. '});',
  14293. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  14294. ' this.DoIt$2 = function (vA) {',
  14295. ' this.DoIt$2(1);',
  14296. ' this.DoIt$3(1, 2);',
  14297. ' $mod.TObject.DoIt.call(this, 1);',
  14298. ' $mod.TObject.DoIt$1.call(this, 1, 2);',
  14299. ' };',
  14300. ' this.DoIt$3 = function (vA, vB) {',
  14301. ' };',
  14302. '});',
  14303. '']),
  14304. LinesToStr([ // $mod.$main
  14305. '']));
  14306. end;
  14307. procedure TTestModule.TestClass_OverloadConstructor;
  14308. begin
  14309. StartProgram(false);
  14310. Add('type');
  14311. Add(' TObject = class');
  14312. Add(' constructor Create(vA: longint);');
  14313. Add(' constructor Create(vA, vB: longint);');
  14314. Add(' end;');
  14315. Add(' TCar = class');
  14316. Add(' constructor Create(vA: longint);');
  14317. Add(' constructor Create(vA, vB: longint);');
  14318. Add(' end;');
  14319. Add('constructor tobject.create(va: longint);');
  14320. Add('begin');
  14321. Add(' create(1);');
  14322. Add(' create(1,2);');
  14323. Add('end;');
  14324. Add('constructor tobject.create(va, vb: longint); begin end;');
  14325. Add('constructor tcar.create(va: longint);');
  14326. Add('begin');
  14327. Add(' create(1);');
  14328. Add(' create(1,2);');
  14329. Add(' inherited create(1);');
  14330. Add(' inherited create(1,2);');
  14331. Add('end;');
  14332. Add('constructor tcar.create(va, vb: longint); begin end;');
  14333. Add('begin');
  14334. Add(' tobject.create(1);');
  14335. Add(' tobject.create(1,2);');
  14336. Add(' tcar.create(1);');
  14337. Add(' tcar.create(1,2);');
  14338. ConvertProgram;
  14339. CheckSource('TestClass_OverloadConstructor',
  14340. LinesToStr([ // statements
  14341. 'rtl.createClass(this, "TObject", null, function () {',
  14342. ' this.$init = function () {',
  14343. ' };',
  14344. ' this.$final = function () {',
  14345. ' };',
  14346. ' this.Create = function (vA) {',
  14347. ' this.Create(1);',
  14348. ' this.Create$1(1,2);',
  14349. ' return this;',
  14350. ' };',
  14351. ' this.Create$1 = function (vA, vB) {',
  14352. ' return this;',
  14353. ' };',
  14354. '});',
  14355. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  14356. ' this.Create$2 = function (vA) {',
  14357. ' this.Create$2(1);',
  14358. ' this.Create$3(1, 2);',
  14359. ' $mod.TObject.Create.call(this, 1);',
  14360. ' $mod.TObject.Create$1.call(this, 1, 2);',
  14361. ' return this;',
  14362. ' };',
  14363. ' this.Create$3 = function (vA, vB) {',
  14364. ' return this;',
  14365. ' };',
  14366. '});',
  14367. '']),
  14368. LinesToStr([ // $mod.$main
  14369. '$mod.TObject.$create("Create", [1]);',
  14370. '$mod.TObject.$create("Create$1", [1, 2]);',
  14371. '$mod.TCar.$create("Create$2", [1]);',
  14372. '$mod.TCar.$create("Create$3", [1, 2]);',
  14373. '']));
  14374. end;
  14375. procedure TTestModule.TestClass_OverloadDelphiOverride;
  14376. begin
  14377. StartProgram(false);
  14378. Add([
  14379. '{$mode delphi}',
  14380. 'type',
  14381. ' TObject = class end;',
  14382. ' TBird = class',
  14383. ' function {#a}GetValue: longint; overload; virtual;',
  14384. ' function {#b}GetValue(AValue: longint): longint; overload; virtual;',
  14385. ' end;',
  14386. ' TEagle = class(TBird)',
  14387. ' function {#c}GetValue: longint; overload; override;',
  14388. ' function {#d}GetValue(AValue: longint): longint; overload; override;',
  14389. ' end;',
  14390. 'function TBird.GetValue: longint;',
  14391. 'begin',
  14392. ' if 3={@a}GetValue then ;',
  14393. ' if 4={@b}GetValue(5) then ;',
  14394. 'end;',
  14395. 'function TBird.GetValue(AValue: longint): longint;',
  14396. 'begin',
  14397. 'end;',
  14398. 'function TEagle.GetValue: longint;',
  14399. 'begin',
  14400. ' if 13={@c}GetValue then ;',
  14401. ' if 14={@d}GetValue(15) then ;',
  14402. ' if 15=inherited {@a}GetValue then ;',
  14403. ' if 16=inherited {@b}GetValue(17) then ;',
  14404. 'end;',
  14405. 'function TEagle.GetValue(AValue: longint): longint;',
  14406. 'begin',
  14407. 'end;',
  14408. 'var',
  14409. ' e: TEagle;',
  14410. 'begin',
  14411. ' if 23=e.{@c}GetValue then ;',
  14412. ' if 24=e.{@d}GetValue(25) then ;']);
  14413. ConvertProgram;
  14414. CheckSource('TestClass_OverloadDelphiOverride',
  14415. LinesToStr([ // statements
  14416. 'rtl.createClass(this, "TObject", null, function () {',
  14417. ' this.$init = function () {',
  14418. ' };',
  14419. ' this.$final = function () {',
  14420. ' };',
  14421. '});',
  14422. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  14423. ' this.GetValue = function () {',
  14424. ' var Result = 0;',
  14425. ' if (3 === this.GetValue()) ;',
  14426. ' if (4 === this.GetValue$1(5)) ;',
  14427. ' return Result;',
  14428. ' };',
  14429. ' this.GetValue$1 = function (AValue) {',
  14430. ' var Result = 0;',
  14431. ' return Result;',
  14432. ' };',
  14433. '});',
  14434. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  14435. ' this.GetValue = function () {',
  14436. ' var Result = 0;',
  14437. ' if (13 === this.GetValue()) ;',
  14438. ' if (14 === this.GetValue$1(15)) ;',
  14439. ' if (15 === $mod.TBird.GetValue.call(this)) ;',
  14440. ' if (16 === $mod.TBird.GetValue$1.call(this, 17)) ;',
  14441. ' return Result;',
  14442. ' };',
  14443. ' this.GetValue$1 = function (AValue) {',
  14444. ' var Result = 0;',
  14445. ' return Result;',
  14446. ' };',
  14447. '});',
  14448. 'this.e = null;',
  14449. '']),
  14450. LinesToStr([ // $mod.$main
  14451. 'if (23 === $mod.e.GetValue()) ;',
  14452. 'if (24 === $mod.e.GetValue$1(25)) ;',
  14453. '']));
  14454. end;
  14455. procedure TTestModule.TestClass_ReintroduceVarDelphi;
  14456. begin
  14457. StartProgram(false);
  14458. Add([
  14459. '{$mode delphi}',
  14460. 'type',
  14461. ' TObject = class end;',
  14462. ' TAnimal = class',
  14463. ' public',
  14464. ' {#animal_a}A: longint;',
  14465. ' function {#animal_b}B: longint;',
  14466. ' end;',
  14467. ' TBird = class(TAnimal)',
  14468. ' public',
  14469. ' {#bird_a}A: double;',
  14470. ' {#bird_b}B: boolean;',
  14471. ' end;',
  14472. ' TEagle = class(TBird)',
  14473. ' public',
  14474. ' function {#eagle_a}A: boolean;',
  14475. ' {#eagle_b}B: double;',
  14476. ' end;',
  14477. 'function TAnimal.B: longint;',
  14478. 'begin',
  14479. 'end;',
  14480. 'function TEagle.A: boolean;',
  14481. 'begin',
  14482. ' {@eagle_b}B:=3.3;',
  14483. ' {@eagle_a}A();',
  14484. ' TBird(Self).{@bird_b}B:=true;',
  14485. ' TAnimal(Self).{@animal_a}A:=17;',
  14486. ' inherited {@bird_b}B:=inherited {bird_a}A>1;', // Delphi allows only inherited <functionname>
  14487. 'end;',
  14488. 'var',
  14489. ' e: TEagle;',
  14490. 'begin',
  14491. ' e.{@eagle_b}B:=5.3;',
  14492. ' if e.{@eagle_a}A then ;',
  14493. '']);
  14494. ConvertProgram;
  14495. CheckSource('TestClass_ReintroduceVarDelphi',
  14496. LinesToStr([ // statements
  14497. 'rtl.createClass(this, "TObject", null, function () {',
  14498. ' this.$init = function () {',
  14499. ' };',
  14500. ' this.$final = function () {',
  14501. ' };',
  14502. '});',
  14503. 'rtl.createClass(this, "TAnimal", this.TObject, function () {',
  14504. ' this.$init = function () {',
  14505. ' $mod.TObject.$init.call(this);',
  14506. ' this.A = 0;',
  14507. ' };',
  14508. ' this.B = function () {',
  14509. ' var Result = 0;',
  14510. ' return Result;',
  14511. ' };',
  14512. '});',
  14513. 'rtl.createClass(this, "TBird", this.TAnimal, function () {',
  14514. ' this.$init = function () {',
  14515. ' $mod.TAnimal.$init.call(this);',
  14516. ' this.A$1 = 0.0;',
  14517. ' this.B$1 = false;',
  14518. ' };',
  14519. '});',
  14520. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  14521. ' this.$init = function () {',
  14522. ' $mod.TBird.$init.call(this);',
  14523. ' this.B$2 = 0.0;',
  14524. ' };',
  14525. ' this.A$2 = function () {',
  14526. ' var Result = false;',
  14527. ' this.B$2 = 3.3;',
  14528. ' this.A$2();',
  14529. ' this.B$1 = true;',
  14530. ' this.A = 17;',
  14531. ' this.B$1 = this.A$1 > 1;',
  14532. ' return Result;',
  14533. ' };',
  14534. '});',
  14535. 'this.e = null;',
  14536. '']),
  14537. LinesToStr([ // $mod.$main
  14538. '$mod.e.B$2 = 5.3;',
  14539. 'if ($mod.e.A$2()) ;',
  14540. '']));
  14541. end;
  14542. procedure TTestModule.TestClass_ReintroducedVar;
  14543. begin
  14544. StartProgram(false);
  14545. Add('type');
  14546. Add(' TObject = class');
  14547. Add(' strict private');
  14548. Add(' Some: longint;');
  14549. Add(' end;');
  14550. Add(' TMobile = class');
  14551. Add(' strict private');
  14552. Add(' Some: string;');
  14553. Add(' end;');
  14554. Add(' TCar = class(tmobile)');
  14555. Add(' procedure Some;');
  14556. Add(' procedure Some(vA: longint);');
  14557. Add(' end;');
  14558. Add('procedure tcar.some;');
  14559. Add('begin');
  14560. Add(' Some;');
  14561. Add(' Some(1);');
  14562. Add('end;');
  14563. Add('procedure tcar.some(va: longint); begin end;');
  14564. Add('begin');
  14565. ConvertProgram;
  14566. CheckSource('TestClass_ReintroducedVar',
  14567. LinesToStr([ // statements
  14568. 'rtl.createClass(this, "TObject", null, function () {',
  14569. ' this.$init = function () {',
  14570. ' this.Some = 0;',
  14571. ' };',
  14572. ' this.$final = function () {',
  14573. ' };',
  14574. '});',
  14575. 'rtl.createClass(this, "TMobile", this.TObject, function () {',
  14576. ' this.$init = function () {',
  14577. ' $mod.TObject.$init.call(this);',
  14578. ' this.Some$1 = "";',
  14579. ' };',
  14580. '});',
  14581. 'rtl.createClass(this, "TCar", this.TMobile, function () {',
  14582. ' this.Some$2 = function () {',
  14583. ' this.Some$2();',
  14584. ' this.Some$3(1);',
  14585. ' };',
  14586. ' this.Some$3 = function (vA) {',
  14587. ' };',
  14588. '});',
  14589. '']),
  14590. LinesToStr([ // $mod.$main
  14591. '']));
  14592. end;
  14593. procedure TTestModule.TestClass_RaiseDescendant;
  14594. begin
  14595. StartProgram(false);
  14596. Add([
  14597. 'type',
  14598. ' TObject = class',
  14599. ' constructor Create(Msg: string);',
  14600. ' end;',
  14601. ' Exception = class',
  14602. ' end;',
  14603. ' EConvertError = class(Exception)',
  14604. ' end;',
  14605. 'constructor TObject.Create(Msg: string); begin end;',
  14606. 'function AssertConv(Msg: string = ''def''): EConvertError; begin end;',
  14607. 'begin',
  14608. ' raise Exception.Create(''Bar1'');',
  14609. ' raise EConvertError.Create(''Bar2'');',
  14610. ' raise AssertConv(''Bar2'');',
  14611. ' raise AssertConv;',
  14612. '']);
  14613. ConvertProgram;
  14614. CheckSource('TestClass_RaiseDescendant',
  14615. LinesToStr([ // statements
  14616. 'rtl.createClass(this, "TObject", null, function () {',
  14617. ' this.$init = function () {',
  14618. ' };',
  14619. ' this.$final = function () {',
  14620. ' };',
  14621. ' this.Create = function (Msg) {',
  14622. ' return this;',
  14623. ' };',
  14624. '});',
  14625. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  14626. '});',
  14627. 'rtl.createClass(this, "EConvertError", this.Exception, function () {',
  14628. '});',
  14629. 'this.AssertConv = function (Msg) {',
  14630. ' var Result = null;',
  14631. ' return Result;',
  14632. '};',
  14633. '']),
  14634. LinesToStr([ // $mod.$main
  14635. 'throw $mod.Exception.$create("Create",["Bar1"]);',
  14636. 'throw $mod.EConvertError.$create("Create",["Bar2"]);',
  14637. 'throw $mod.AssertConv("Bar2");',
  14638. 'throw $mod.AssertConv("def");',
  14639. '']));
  14640. end;
  14641. procedure TTestModule.TestClass_ExternalMethod;
  14642. begin
  14643. AddModuleWithIntfImplSrc('unit2.pas',
  14644. LinesToStr([
  14645. 'type',
  14646. ' TObject = class',
  14647. ' public',
  14648. ' procedure Intern; external name ''$DoIntern'';',
  14649. ' end;',
  14650. '']),
  14651. LinesToStr([
  14652. '']));
  14653. StartUnit(true);
  14654. Add('interface');
  14655. Add('uses unit2;');
  14656. Add('type');
  14657. Add(' TCar = class(TObject)');
  14658. Add(' public');
  14659. Add(' procedure Intern2; external name ''$DoIntern2'';');
  14660. Add(' procedure DoIt;');
  14661. Add(' end;');
  14662. Add('implementation');
  14663. Add('procedure tcar.doit;');
  14664. Add('begin');
  14665. Add(' Intern;');
  14666. Add(' Intern();');
  14667. Add(' Intern2;');
  14668. Add(' Intern2();');
  14669. Add('end;');
  14670. Add('var Obj: TCar;');
  14671. Add('begin');
  14672. Add(' obj.intern;');
  14673. Add(' obj.intern();');
  14674. Add(' obj.intern2;');
  14675. Add(' obj.intern2();');
  14676. Add(' obj.doit;');
  14677. Add(' obj.doit();');
  14678. Add(' with obj do begin');
  14679. Add(' Intern;');
  14680. Add(' Intern();');
  14681. Add(' Intern2;');
  14682. Add(' Intern2();');
  14683. Add(' end;');
  14684. ConvertUnit;
  14685. CheckSource('TestClass_ExternalMethod',
  14686. LinesToStr([
  14687. 'var $impl = $mod.$impl;',
  14688. 'rtl.createClass(this, "TCar", pas.unit2.TObject, function () {',
  14689. ' this.DoIt = function () {',
  14690. ' this.$DoIntern();',
  14691. ' this.$DoIntern();',
  14692. ' this.$DoIntern2();',
  14693. ' this.$DoIntern2();',
  14694. ' };',
  14695. ' });',
  14696. '']),
  14697. LinesToStr([ // this.$init
  14698. '$impl.Obj.$DoIntern();',
  14699. '$impl.Obj.$DoIntern();',
  14700. '$impl.Obj.$DoIntern2();',
  14701. '$impl.Obj.$DoIntern2();',
  14702. '$impl.Obj.DoIt();',
  14703. '$impl.Obj.DoIt();',
  14704. 'var $with = $impl.Obj;',
  14705. '$with.$DoIntern();',
  14706. '$with.$DoIntern();',
  14707. '$with.$DoIntern2();',
  14708. '$with.$DoIntern2();',
  14709. '']),
  14710. LinesToStr([ // implementation
  14711. '$impl.Obj = null;',
  14712. '']) );
  14713. end;
  14714. procedure TTestModule.TestClass_ExternalVirtualNameMismatchFail;
  14715. begin
  14716. StartProgram(false);
  14717. Add('type');
  14718. Add(' TObject = class');
  14719. Add(' procedure DoIt; virtual; external name ''Foo'';');
  14720. Add(' end;');
  14721. Add('begin');
  14722. SetExpectedPasResolverError('Virtual method name must match external',
  14723. nVirtualMethodNameMustMatchExternal);
  14724. ConvertProgram;
  14725. end;
  14726. procedure TTestModule.TestClass_ExternalOverrideFail;
  14727. begin
  14728. StartProgram(false);
  14729. Add('type');
  14730. Add(' TObject = class');
  14731. Add(' procedure DoIt; virtual; external name ''DoIt'';');
  14732. Add(' end;');
  14733. Add(' TCar = class');
  14734. Add(' procedure DoIt; override; external name ''DoIt'';');
  14735. Add(' end;');
  14736. Add('begin');
  14737. SetExpectedPasResolverError('Invalid procedure modifier override,external',
  14738. nInvalidXModifierY);
  14739. ConvertProgram;
  14740. end;
  14741. procedure TTestModule.TestClass_ExternalVar;
  14742. begin
  14743. AddModuleWithIntfImplSrc('unit2.pas',
  14744. LinesToStr([
  14745. '{$modeswitch externalclass}',
  14746. 'type',
  14747. ' TObject = class',
  14748. ' public',
  14749. ' Intern: longint external name ''$Intern'';',
  14750. ' Bracket: longint external name ''["A B"]'';',
  14751. ' end;',
  14752. '']),
  14753. LinesToStr([
  14754. '']));
  14755. StartUnit(true);
  14756. Add([
  14757. 'interface',
  14758. 'uses unit2;',
  14759. '{$modeswitch externalclass}',
  14760. 'type',
  14761. ' TCar = class(tobject)',
  14762. ' public',
  14763. ' Intern2: longint external name ''$Intern2'';',
  14764. ' procedure DoIt;',
  14765. ' end;',
  14766. 'implementation',
  14767. 'procedure tcar.doit;',
  14768. 'begin',
  14769. ' Intern:=Intern+1;',
  14770. ' Intern2:=Intern2+2;',
  14771. ' Bracket:=Bracket+3;',
  14772. 'end;',
  14773. 'var Obj: TCar;',
  14774. 'begin',
  14775. ' obj.intern:=obj.intern+1;',
  14776. ' obj.intern2:=obj.intern2+2;',
  14777. ' obj.Bracket:=obj.Bracket+3;',
  14778. ' with obj do begin',
  14779. ' intern:=intern+1;',
  14780. ' intern2:=intern2+2;',
  14781. ' Bracket:=Bracket+3;',
  14782. ' end;']);
  14783. ConvertUnit;
  14784. CheckSource('TestClass_ExternalVar',
  14785. LinesToStr([
  14786. 'var $impl = $mod.$impl;',
  14787. 'rtl.createClass(this, "TCar", pas.unit2.TObject, function () {',
  14788. ' this.DoIt = function () {',
  14789. ' this.$Intern = this.$Intern + 1;',
  14790. ' this.$Intern2 = this.$Intern2 + 2;',
  14791. ' this["A B"] = this["A B"] + 3;',
  14792. ' };',
  14793. ' });',
  14794. '']),
  14795. LinesToStr([
  14796. '$impl.Obj.$Intern = $impl.Obj.$Intern + 1;',
  14797. '$impl.Obj.$Intern2 = $impl.Obj.$Intern2 + 2;',
  14798. '$impl.Obj["A B"] = $impl.Obj["A B"] + 3;',
  14799. 'var $with = $impl.Obj;',
  14800. '$with.$Intern = $with.$Intern + 1;',
  14801. '$with.$Intern2 = $with.$Intern2 + 2;',
  14802. '$with["A B"] = $with["A B"] + 3;',
  14803. '']),
  14804. LinesToStr([ // implementation
  14805. '$impl.Obj = null;',
  14806. '']));
  14807. end;
  14808. procedure TTestModule.TestClass_Const;
  14809. begin
  14810. StartProgram(false);
  14811. Add([
  14812. 'type',
  14813. ' integer = longint;',
  14814. ' TClass = class of TObject;',
  14815. ' TObject = class',
  14816. ' public',
  14817. ' const cI: integer = 3;',
  14818. ' procedure DoIt;',
  14819. ' class procedure DoMore;',
  14820. ' end;',
  14821. 'procedure tobject.doit;',
  14822. 'begin',
  14823. ' if cI=4 then;',
  14824. ' if 5=cI then;',
  14825. ' if Self.cI=6 then;',
  14826. ' if 7=Self.cI then;',
  14827. ' with Self do begin',
  14828. ' if cI=11 then;',
  14829. ' if 12=cI then;',
  14830. ' end;',
  14831. 'end;',
  14832. 'class procedure tobject.domore;',
  14833. 'begin',
  14834. ' if cI=8 then;',
  14835. ' if Self.cI=9 then;',
  14836. ' if 10=cI then;',
  14837. ' if 11=Self.cI then;',
  14838. ' with Self do begin',
  14839. ' if cI=13 then;',
  14840. ' if 14=cI then;',
  14841. ' end;',
  14842. 'end;',
  14843. 'var',
  14844. ' Obj: TObject;',
  14845. ' Cla: TClass;',
  14846. 'begin',
  14847. ' if TObject.cI=21 then ;',
  14848. ' if Obj.cI=22 then ;',
  14849. ' if Cla.cI=23 then ;',
  14850. ' with obj do if ci=24 then;',
  14851. ' with TObject do if ci=25 then;',
  14852. ' with Cla do if ci=26 then;']);
  14853. ConvertProgram;
  14854. CheckSource('TestClass_Const',
  14855. LinesToStr([
  14856. 'rtl.createClass(this, "TObject", null, function () {',
  14857. ' this.cI = 3;',
  14858. ' this.$init = function () {',
  14859. ' };',
  14860. ' this.$final = function () {',
  14861. ' };',
  14862. ' this.DoIt = function () {',
  14863. ' if (this.cI === 4) ;',
  14864. ' if (5 === this.cI) ;',
  14865. ' if (this.cI === 6) ;',
  14866. ' if (7 === this.cI) ;',
  14867. ' if (this.cI === 11) ;',
  14868. ' if (12 === this.cI) ;',
  14869. ' };',
  14870. ' this.DoMore = function () {',
  14871. ' if (this.cI === 8) ;',
  14872. ' if (this.cI === 9) ;',
  14873. ' if (10 === this.cI) ;',
  14874. ' if (11 === this.cI) ;',
  14875. ' if (this.cI === 13) ;',
  14876. ' if (14 === this.cI) ;',
  14877. ' };',
  14878. '});',
  14879. 'this.Obj = null;',
  14880. 'this.Cla = null;',
  14881. '']),
  14882. LinesToStr([
  14883. 'if ($mod.TObject.cI === 21) ;',
  14884. 'if ($mod.Obj.cI === 22) ;',
  14885. 'if ($mod.Cla.cI === 23) ;',
  14886. 'var $with = $mod.Obj;',
  14887. 'if ($with.cI === 24) ;',
  14888. 'var $with1 = $mod.TObject;',
  14889. 'if ($with1.cI === 25) ;',
  14890. 'var $with2 = $mod.Cla;',
  14891. 'if ($with2.cI === 26) ;',
  14892. '']));
  14893. end;
  14894. procedure TTestModule.TestClass_ConstEnum;
  14895. begin
  14896. StartProgram(false);
  14897. Add([
  14898. 'type',
  14899. ' TEnum = (red,blue);',
  14900. ' TObject = class',
  14901. ' end;',
  14902. ' TAnimal = class',
  14903. ' public',
  14904. ' type TSubEnum = (light,dark);',
  14905. ' const a = high(TEnum);',
  14906. ' const b = high(TSubEnum);',
  14907. ' end;',
  14908. ' TBird = class(TAnimal)',
  14909. ' public',
  14910. ' const c = high(TEnum);',
  14911. ' const d = high(TSubEnum);',
  14912. ' end;',
  14913. ' TAnt = class',
  14914. ' public',
  14915. ' const e = high(TEnum);',
  14916. ' const f = high(TBird.TSubEnum);',
  14917. ' end;',
  14918. 'begin',
  14919. '']);
  14920. ConvertProgram;
  14921. CheckSource('TestClass_ConstEnum',
  14922. LinesToStr([
  14923. 'this.TEnum = {',
  14924. ' "0": "red",',
  14925. ' red: 0,',
  14926. ' "1": "blue",',
  14927. ' blue: 1',
  14928. '};',
  14929. 'rtl.createClass(this, "TObject", null, function () {',
  14930. ' this.$init = function () {',
  14931. ' };',
  14932. ' this.$final = function () {',
  14933. ' };',
  14934. '});',
  14935. 'rtl.createClass(this, "TAnimal", this.TObject, function () {',
  14936. ' this.TSubEnum = {',
  14937. ' "0": "light",',
  14938. ' light: 0,',
  14939. ' "1": "dark",',
  14940. ' dark: 1',
  14941. ' };',
  14942. ' this.a = $mod.TEnum.blue;',
  14943. ' this.b = this.TSubEnum.dark;',
  14944. '});',
  14945. 'rtl.createClass(this, "TBird", this.TAnimal, function () {',
  14946. ' this.c = $mod.TEnum.blue;',
  14947. ' this.d = this.TSubEnum.dark;',
  14948. '});',
  14949. 'rtl.createClass(this, "TAnt", this.TObject, function () {',
  14950. ' this.e = $mod.TEnum.blue;',
  14951. ' this.f = $mod.TAnimal.TSubEnum.dark;',
  14952. '});',
  14953. '']),
  14954. LinesToStr([
  14955. '']));
  14956. end;
  14957. procedure TTestModule.TestClass_LocalConstDuplicate_Prg;
  14958. begin
  14959. StartProgram(false);
  14960. Add([
  14961. 'type',
  14962. ' TObject = class',
  14963. ' const cI: longint = 3;',
  14964. ' procedure Fly;',
  14965. ' procedure Run;',
  14966. ' end;',
  14967. ' TBird = class',
  14968. ' procedure Go;',
  14969. ' end;',
  14970. 'procedure tobject.fly;',
  14971. 'const cI: word = 4;',
  14972. 'begin',
  14973. ' if cI=Self.cI then ;',
  14974. 'end;',
  14975. 'procedure tobject.run;',
  14976. 'const cI: word = 5;',
  14977. 'begin',
  14978. ' if cI=Self.cI then ;',
  14979. 'end;',
  14980. 'procedure tbird.go;',
  14981. 'const cI: word = 6;',
  14982. 'begin',
  14983. ' if cI=Self.cI then ;',
  14984. 'end;',
  14985. 'begin',
  14986. '']);
  14987. ConvertProgram;
  14988. CheckSource('TestClass_LocalConstDuplicate_Prg',
  14989. LinesToStr([
  14990. 'rtl.createClass(this, "TObject", null, function () {',
  14991. ' this.cI = 3;',
  14992. ' this.$init = function () {',
  14993. ' };',
  14994. ' this.$final = function () {',
  14995. ' };',
  14996. ' var cI$1 = 4;',
  14997. ' this.Fly = function () {',
  14998. ' if (cI$1 === this.cI) ;',
  14999. ' };',
  15000. ' var cI$2 = 5;',
  15001. ' this.Run = function () {',
  15002. ' if (cI$2 === this.cI) ;',
  15003. ' };',
  15004. '});',
  15005. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15006. ' var cI$3 = 6;',
  15007. ' this.Go = function () {',
  15008. ' if (cI$3 === this.cI) ;',
  15009. ' };',
  15010. '});',
  15011. '']),
  15012. LinesToStr([
  15013. '']));
  15014. end;
  15015. procedure TTestModule.TestClass_LocalConstDuplicate_Unit;
  15016. begin
  15017. StartUnit(false);
  15018. Add([
  15019. 'interface',
  15020. 'type',
  15021. ' TObject = class',
  15022. ' const cI: longint = 3;',
  15023. ' procedure Fly;',
  15024. ' procedure Run;',
  15025. ' end;',
  15026. ' TBird = class',
  15027. ' procedure Go;',
  15028. ' end;',
  15029. 'implementation',
  15030. 'procedure tobject.fly;',
  15031. 'const cI: word = 4;',
  15032. 'begin',
  15033. ' if cI=Self.cI then ;',
  15034. 'end;',
  15035. 'procedure tobject.run;',
  15036. 'const cI: word = 5;',
  15037. 'begin',
  15038. ' if cI=Self.cI then ;',
  15039. 'end;',
  15040. 'procedure tbird.go;',
  15041. 'const cI: word = 6;',
  15042. 'begin',
  15043. ' if cI=Self.cI then ;',
  15044. 'end;',
  15045. '']);
  15046. ConvertUnit;
  15047. CheckSource('TestClass_LocalConstDuplicate_Unit',
  15048. LinesToStr([
  15049. 'rtl.createClass(this, "TObject", null, function () {',
  15050. ' this.cI = 3;',
  15051. ' this.$init = function () {',
  15052. ' };',
  15053. ' this.$final = function () {',
  15054. ' };',
  15055. ' var cI$1 = 4;',
  15056. ' this.Fly = function () {',
  15057. ' if (cI$1 === this.cI) ;',
  15058. ' };',
  15059. ' var cI$2 = 5;',
  15060. ' this.Run = function () {',
  15061. ' if (cI$2 === this.cI) ;',
  15062. ' };',
  15063. '});',
  15064. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15065. ' var cI$3 = 6;',
  15066. ' this.Go = function () {',
  15067. ' if (cI$3 === this.cI) ;',
  15068. ' };',
  15069. '});',
  15070. '']),
  15071. '',
  15072. '');
  15073. end;
  15074. procedure TTestModule.TestClass_LocalVarSelfFail;
  15075. begin
  15076. StartProgram(false);
  15077. Add([
  15078. 'type',
  15079. ' TObject = class',
  15080. ' constructor Create;',
  15081. ' end;',
  15082. 'constructor tobject.create;',
  15083. 'var self: longint;',
  15084. 'begin',
  15085. 'end',
  15086. 'begin',
  15087. '']);
  15088. SetExpectedPasResolverError('Duplicate identifier "self" at (0)',nDuplicateIdentifier);
  15089. ConvertProgram;
  15090. end;
  15091. procedure TTestModule.TestClass_ArgSelfFail;
  15092. begin
  15093. StartProgram(false);
  15094. Add([
  15095. 'type',
  15096. ' TObject = class',
  15097. ' procedure DoIt(Self: longint);',
  15098. ' end;',
  15099. 'procedure tobject.doit(self: longint);',
  15100. 'begin',
  15101. 'end',
  15102. 'begin',
  15103. '']);
  15104. SetExpectedPasResolverError('Duplicate identifier "Self" at test1.pp(5,24)',nDuplicateIdentifier);
  15105. ConvertProgram;
  15106. end;
  15107. procedure TTestModule.TestClass_NestedProcSelf;
  15108. begin
  15109. StartProgram(false);
  15110. Add([
  15111. 'type',
  15112. ' TObject = class',
  15113. ' Key: longint;',
  15114. ' class var State: longint;',
  15115. ' procedure DoIt;',
  15116. ' function GetSize: longint; virtual; abstract;',
  15117. ' procedure SetSize(Value: longint); virtual; abstract;',
  15118. ' property Size: longint read GetSize write SetSize;',
  15119. ' end;',
  15120. 'procedure tobject.doit;',
  15121. ' procedure Sub;',
  15122. ' begin',
  15123. ' key:=key+2;',
  15124. ' self.key:=self.key+3;',
  15125. ' state:=state+4;',
  15126. ' self.state:=self.state+5;',
  15127. ' tobject.state:=tobject.state+6;',
  15128. ' size:=size+7;',
  15129. ' self.size:=self.size+8;',
  15130. ' end;',
  15131. 'begin',
  15132. ' sub;',
  15133. ' key:=key+12;',
  15134. ' self.key:=self.key+13;',
  15135. ' state:=state+14;',
  15136. ' self.state:=self.state+15;',
  15137. ' tobject.state:=tobject.state+16;',
  15138. ' size:=size+17;',
  15139. ' self.size:=self.size+18;',
  15140. 'end;',
  15141. 'begin',
  15142. '']);
  15143. ConvertProgram;
  15144. CheckSource('TestClass_NestedProcSelf',
  15145. LinesToStr([ // statements
  15146. 'rtl.createClass(this, "TObject", null, function () {',
  15147. ' this.State = 0;',
  15148. ' this.$init = function () {',
  15149. ' this.Key = 0;',
  15150. ' };',
  15151. ' this.$final = function () {',
  15152. ' };',
  15153. ' this.DoIt = function () {',
  15154. ' var $Self = this;',
  15155. ' function Sub() {',
  15156. ' $Self.Key = $Self.Key + 2;',
  15157. ' $Self.Key = $Self.Key + 3;',
  15158. ' $mod.TObject.State = $Self.State + 4;',
  15159. ' $mod.TObject.State = $Self.State + 5;',
  15160. ' $mod.TObject.State = $mod.TObject.State + 6;',
  15161. ' $Self.SetSize($Self.GetSize() + 7);',
  15162. ' $Self.SetSize($Self.GetSize() + 8);',
  15163. ' };',
  15164. ' Sub();',
  15165. ' this.Key = this.Key + 12;',
  15166. ' $Self.Key = $Self.Key + 13;',
  15167. ' $mod.TObject.State = this.State + 14;',
  15168. ' $mod.TObject.State = $Self.State + 15;',
  15169. ' $mod.TObject.State = $mod.TObject.State + 16;',
  15170. ' this.SetSize(this.GetSize() + 17);',
  15171. ' $Self.SetSize($Self.GetSize() + 18);',
  15172. ' };',
  15173. '});',
  15174. '']),
  15175. LinesToStr([ // $mod.$main
  15176. '']));
  15177. end;
  15178. procedure TTestModule.TestClass_NestedProcSelf2;
  15179. begin
  15180. StartProgram(false);
  15181. Add([
  15182. 'type',
  15183. ' TObject = class',
  15184. ' Key: longint;',
  15185. ' class var State: longint;',
  15186. ' function GetSize: longint; virtual; abstract;',
  15187. ' procedure SetSize(Value: longint); virtual; abstract;',
  15188. ' property Size: longint read GetSize write SetSize;',
  15189. ' end;',
  15190. ' TBird = class',
  15191. ' procedure DoIt;',
  15192. ' end;',
  15193. 'procedure tbird.doit;',
  15194. ' procedure Sub;',
  15195. ' begin',
  15196. ' key:=key+2;',
  15197. ' self.key:=self.key+3;',
  15198. ' state:=state+4;',
  15199. ' self.state:=self.state+5;',
  15200. ' tobject.state:=tobject.state+6;',
  15201. ' size:=size+7;',
  15202. ' self.size:=self.size+8;',
  15203. ' end;',
  15204. 'begin',
  15205. ' sub;',
  15206. ' key:=key+12;',
  15207. ' self.key:=self.key+13;',
  15208. ' state:=state+14;',
  15209. ' self.state:=self.state+15;',
  15210. ' tobject.state:=tobject.state+16;',
  15211. ' size:=size+17;',
  15212. ' self.size:=self.size+18;',
  15213. 'end;',
  15214. 'begin',
  15215. '']);
  15216. ConvertProgram;
  15217. CheckSource('TestClass_NestedProcSelf2',
  15218. LinesToStr([ // statements
  15219. 'rtl.createClass(this, "TObject", null, function () {',
  15220. ' this.State = 0;',
  15221. ' this.$init = function () {',
  15222. ' this.Key = 0;',
  15223. ' };',
  15224. ' this.$final = function () {',
  15225. ' };',
  15226. '});',
  15227. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15228. ' this.DoIt = function () {',
  15229. ' var $Self = this;',
  15230. ' function Sub() {',
  15231. ' $Self.Key = $Self.Key + 2;',
  15232. ' $Self.Key = $Self.Key + 3;',
  15233. ' $mod.TObject.State = $Self.State + 4;',
  15234. ' $mod.TObject.State = $Self.State + 5;',
  15235. ' $mod.TObject.State = $mod.TObject.State + 6;',
  15236. ' $Self.SetSize($Self.GetSize() + 7);',
  15237. ' $Self.SetSize($Self.GetSize() + 8);',
  15238. ' };',
  15239. ' Sub();',
  15240. ' this.Key = this.Key + 12;',
  15241. ' $Self.Key = $Self.Key + 13;',
  15242. ' $mod.TObject.State = this.State + 14;',
  15243. ' $mod.TObject.State = $Self.State + 15;',
  15244. ' $mod.TObject.State = $mod.TObject.State + 16;',
  15245. ' this.SetSize(this.GetSize() + 17);',
  15246. ' $Self.SetSize($Self.GetSize() + 18);',
  15247. ' };',
  15248. '});',
  15249. '']),
  15250. LinesToStr([ // $mod.$main
  15251. '']));
  15252. end;
  15253. procedure TTestModule.TestClass_NestedProcClassSelf;
  15254. begin
  15255. StartProgram(false);
  15256. Add([
  15257. 'type',
  15258. ' TObject = class',
  15259. ' class var State: longint;',
  15260. ' class procedure DoIt;',
  15261. ' class function GetSize: longint; virtual; abstract;',
  15262. ' class procedure SetSize(Value: longint); virtual; abstract;',
  15263. ' class property Size: longint read GetSize write SetSize;',
  15264. ' end;',
  15265. 'class procedure tobject.doit;',
  15266. ' procedure Sub;',
  15267. ' begin',
  15268. ' state:=state+2;',
  15269. ' self.state:=self.state+3;',
  15270. ' tobject.state:=tobject.state+4;',
  15271. ' size:=size+5;',
  15272. ' self.size:=self.size+6;',
  15273. ' tobject.size:=tobject.size+7;',
  15274. ' end;',
  15275. 'begin',
  15276. ' sub;',
  15277. ' state:=state+12;',
  15278. ' self.state:=self.state+13;',
  15279. ' tobject.state:=tobject.state+14;',
  15280. ' size:=size+15;',
  15281. ' self.size:=self.size+16;',
  15282. ' tobject.size:=tobject.size+17;',
  15283. 'end;',
  15284. 'begin',
  15285. '']);
  15286. ConvertProgram;
  15287. CheckSource('TestClass_NestedProcClassSelf',
  15288. LinesToStr([ // statements
  15289. 'rtl.createClass(this, "TObject", null, function () {',
  15290. ' this.State = 0;',
  15291. ' this.$init = function () {',
  15292. ' };',
  15293. ' this.$final = function () {',
  15294. ' };',
  15295. ' this.DoIt = function () {',
  15296. ' var $Self = this;',
  15297. ' function Sub() {',
  15298. ' $mod.TObject.State = $Self.State + 2;',
  15299. ' $mod.TObject.State = $Self.State + 3;',
  15300. ' $mod.TObject.State = $mod.TObject.State + 4;',
  15301. ' $Self.SetSize($Self.GetSize() + 5);',
  15302. ' $Self.SetSize($Self.GetSize() + 6);',
  15303. ' $mod.TObject.SetSize($mod.TObject.GetSize() + 7);',
  15304. ' };',
  15305. ' Sub();',
  15306. ' $mod.TObject.State = this.State + 12;',
  15307. ' $mod.TObject.State = $Self.State + 13;',
  15308. ' $mod.TObject.State = $mod.TObject.State + 14;',
  15309. ' this.SetSize(this.GetSize() + 15);',
  15310. ' $Self.SetSize($Self.GetSize() + 16);',
  15311. ' $mod.TObject.SetSize($mod.TObject.GetSize() + 17);',
  15312. ' };',
  15313. '});',
  15314. '']),
  15315. LinesToStr([ // $mod.$main
  15316. '']));
  15317. end;
  15318. procedure TTestModule.TestClass_NestedProcCallInherited;
  15319. begin
  15320. StartProgram(false);
  15321. Add([
  15322. 'type',
  15323. ' TObject = class',
  15324. ' function DoIt(k: boolean): longint; virtual;',
  15325. ' end;',
  15326. ' TBird = class',
  15327. ' function DoIt(k: boolean): longint; override;',
  15328. ' end;',
  15329. 'function tobject.doit(k: boolean): longint;',
  15330. 'begin',
  15331. 'end;',
  15332. 'function tbird.doit(k: boolean): longint;',
  15333. ' procedure Sub;',
  15334. ' begin',
  15335. ' inherited DoIt(true);',
  15336. //' if inherited DoIt(false)=4 then ;',
  15337. ' end;',
  15338. 'begin',
  15339. ' Sub;',
  15340. ' inherited;',
  15341. ' inherited DoIt(true);',
  15342. //' if inherited DoIt(false)=14 then ;',
  15343. 'end;',
  15344. 'begin',
  15345. '']);
  15346. ConvertProgram;
  15347. CheckSource('TestClass_NestedProcCallInherited',
  15348. LinesToStr([ // statements
  15349. 'rtl.createClass(this, "TObject", null, function () {',
  15350. ' this.$init = function () {',
  15351. ' };',
  15352. ' this.$final = function () {',
  15353. ' };',
  15354. ' this.DoIt = function (k) {',
  15355. ' var Result = 0;',
  15356. ' return Result;',
  15357. ' };',
  15358. '});',
  15359. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15360. ' this.DoIt = function (k) {',
  15361. ' var $Self = this;',
  15362. ' var Result = 0;',
  15363. ' function Sub() {',
  15364. ' $mod.TObject.DoIt.call($Self, true);',
  15365. ' };',
  15366. ' Sub();',
  15367. ' $mod.TObject.DoIt.apply(this, arguments);',
  15368. ' $mod.TObject.DoIt.call(this, true);',
  15369. ' return Result;',
  15370. ' };',
  15371. '});',
  15372. '']),
  15373. LinesToStr([ // $mod.$main
  15374. '']));
  15375. end;
  15376. procedure TTestModule.TestClass_TObjectFree;
  15377. begin
  15378. StartProgram(false);
  15379. Add([
  15380. 'type',
  15381. ' TObject = class',
  15382. ' Obj: tobject;',
  15383. ' procedure Free;',
  15384. ' procedure Release;',
  15385. ' end;',
  15386. 'procedure tobject.free;',
  15387. 'begin',
  15388. 'end;',
  15389. 'procedure tobject.release;',
  15390. 'begin',
  15391. ' free;',
  15392. ' if true then free;',
  15393. 'end;',
  15394. 'function DoIt(o: tobject): tobject;',
  15395. 'var l: tobject;',
  15396. 'begin',
  15397. ' o.free;',
  15398. ' o.free();',
  15399. ' l.free;',
  15400. ' l.free();',
  15401. ' o.obj.free;',
  15402. ' o.obj.free();',
  15403. ' with o do obj.free;',
  15404. ' with o do obj.free();',
  15405. ' result.Free;',
  15406. ' result.Free();',
  15407. 'end;',
  15408. 'var o: tobject;',
  15409. ' a: array of tobject;',
  15410. 'begin',
  15411. ' o.free;',
  15412. ' o.obj.free;',
  15413. ' a[1+2].free;',
  15414. '']);
  15415. ConvertProgram;
  15416. CheckSource('TestClass_TObjectFree',
  15417. LinesToStr([ // statements
  15418. 'rtl.createClass(this, "TObject", null, function () {',
  15419. ' this.$init = function () {',
  15420. ' this.Obj = null;',
  15421. ' };',
  15422. ' this.$final = function () {',
  15423. ' this.Obj = undefined;',
  15424. ' };',
  15425. ' this.Free = function () {',
  15426. ' };',
  15427. ' this.Release = function () {',
  15428. ' this.Free();',
  15429. ' if (true) this.Free();',
  15430. ' };',
  15431. '});',
  15432. 'this.DoIt = function (o) {',
  15433. ' var Result = null;',
  15434. ' var l = null;',
  15435. ' o = rtl.freeLoc(o);',
  15436. ' o = rtl.freeLoc(o);',
  15437. ' l = rtl.freeLoc(l);',
  15438. ' l = rtl.freeLoc(l);',
  15439. ' rtl.free(o, "Obj");',
  15440. ' rtl.free(o, "Obj");',
  15441. ' rtl.free(o, "Obj");',
  15442. ' rtl.free(o, "Obj");',
  15443. ' Result = rtl.freeLoc(Result);',
  15444. ' Result = rtl.freeLoc(Result);',
  15445. ' return Result;',
  15446. '};',
  15447. 'this.o = null;',
  15448. 'this.a = [];',
  15449. '']),
  15450. LinesToStr([ // $mod.$main
  15451. 'rtl.free($mod, "o");',
  15452. 'rtl.free($mod.o, "Obj");',
  15453. 'rtl.free($mod.a, 1 + 2);',
  15454. '']));
  15455. end;
  15456. procedure TTestModule.TestClass_TObjectFree_VarArg;
  15457. begin
  15458. StartProgram(false);
  15459. Add([
  15460. 'type',
  15461. ' TObject = class',
  15462. ' Obj: tobject;',
  15463. ' procedure Free;',
  15464. ' end;',
  15465. 'procedure tobject.free;',
  15466. 'begin',
  15467. 'end;',
  15468. 'procedure DoIt(var o: tobject);',
  15469. 'begin',
  15470. ' o.free;',
  15471. ' o.free();',
  15472. 'end;',
  15473. 'begin',
  15474. '']);
  15475. ConvertProgram;
  15476. CheckSource('TestClass_TObjectFree_VarArg',
  15477. LinesToStr([ // statements
  15478. 'rtl.createClass(this, "TObject", null, function () {',
  15479. ' this.$init = function () {',
  15480. ' this.Obj = null;',
  15481. ' };',
  15482. ' this.$final = function () {',
  15483. ' this.Obj = undefined;',
  15484. ' };',
  15485. ' this.Free = function () {',
  15486. ' };',
  15487. '});',
  15488. 'this.DoIt = function (o) {',
  15489. ' o.set(rtl.freeLoc(o.get()));',
  15490. ' o.set(rtl.freeLoc(o.get()));',
  15491. '};',
  15492. '']),
  15493. LinesToStr([ // $mod.$main
  15494. '']));
  15495. end;
  15496. procedure TTestModule.TestClass_TObjectFreeNewInstance;
  15497. begin
  15498. StartProgram(false);
  15499. Add([
  15500. 'type',
  15501. ' TObject = class',
  15502. ' constructor Create;',
  15503. ' procedure Free;',
  15504. ' end;',
  15505. 'constructor TObject.Create; begin end;',
  15506. 'procedure tobject.free; begin end;',
  15507. 'begin',
  15508. ' with tobject.create do free;',
  15509. '']);
  15510. ConvertProgram;
  15511. CheckSource('TestClass_TObjectFreeNewInstance',
  15512. LinesToStr([ // statements
  15513. 'rtl.createClass(this, "TObject", null, function () {',
  15514. ' this.$init = function () {',
  15515. ' };',
  15516. ' this.$final = function () {',
  15517. ' };',
  15518. ' this.Create = function () {',
  15519. ' return this;',
  15520. ' };',
  15521. ' this.Free = function () {',
  15522. ' };',
  15523. '});',
  15524. '']),
  15525. LinesToStr([ // $mod.$main
  15526. 'var $with = $mod.TObject.$create("Create");',
  15527. '$with=rtl.freeLoc($with);',
  15528. '']));
  15529. end;
  15530. procedure TTestModule.TestClass_TObjectFreeLowerCase;
  15531. begin
  15532. StartProgram(false);
  15533. Add([
  15534. 'type',
  15535. ' TObject = class',
  15536. ' destructor Destroy;',
  15537. ' procedure Free;',
  15538. ' end;',
  15539. 'destructor TObject.Destroy; begin end;',
  15540. 'procedure tobject.free; begin end;',
  15541. 'var o: tobject;',
  15542. 'begin',
  15543. ' o.free;',
  15544. '']);
  15545. Converter.UseLowerCase:=true;
  15546. ConvertProgram;
  15547. CheckSource('TestClass_TObjectFreeLowerCase',
  15548. LinesToStr([ // statements
  15549. 'rtl.createClass(this, "tobject", null, function () {',
  15550. ' this.$init = function () {',
  15551. ' };',
  15552. ' this.$final = function () {',
  15553. ' };',
  15554. ' rtl.tObjectDestroy = "destroy";',
  15555. ' this.destroy = function () {',
  15556. ' };',
  15557. ' this.free = function () {',
  15558. ' };',
  15559. '});',
  15560. 'this.o = null;',
  15561. '']),
  15562. LinesToStr([ // $mod.$main
  15563. 'rtl.free($mod, "o");',
  15564. '']));
  15565. end;
  15566. procedure TTestModule.TestClass_TObjectFreeFunctionFail;
  15567. begin
  15568. StartProgram(false);
  15569. Add([
  15570. 'type',
  15571. ' TObject = class',
  15572. ' procedure Free;',
  15573. ' function GetObj: tobject; virtual; abstract;',
  15574. ' end;',
  15575. 'procedure tobject.free;',
  15576. 'begin',
  15577. 'end;',
  15578. 'var o: tobject;',
  15579. 'begin',
  15580. ' o.getobj.free;',
  15581. '']);
  15582. SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
  15583. ConvertProgram;
  15584. end;
  15585. procedure TTestModule.TestClass_TObjectFreePropertyFail;
  15586. begin
  15587. StartProgram(false);
  15588. Add([
  15589. 'type',
  15590. ' TObject = class',
  15591. ' procedure Free;',
  15592. ' FObj: TObject;',
  15593. ' property Obj: tobject read FObj write FObj;',
  15594. ' end;',
  15595. 'procedure tobject.free;',
  15596. 'begin',
  15597. 'end;',
  15598. 'var o: tobject;',
  15599. 'begin',
  15600. ' o.obj.free;',
  15601. '']);
  15602. SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
  15603. ConvertProgram;
  15604. end;
  15605. procedure TTestModule.TestClass_ForIn;
  15606. begin
  15607. StartProgram(false);
  15608. Add([
  15609. 'type',
  15610. ' TObject = class end;',
  15611. ' TItem = TObject;',
  15612. ' TEnumerator = class',
  15613. ' FCurrent: TItem;',
  15614. ' property Current: TItem read FCurrent;',
  15615. ' function MoveNext: boolean;',
  15616. ' end;',
  15617. ' TBird = class',
  15618. ' function GetEnumerator: TEnumerator;',
  15619. ' end;',
  15620. 'function TEnumerator.MoveNext: boolean;',
  15621. 'begin',
  15622. 'end;',
  15623. 'function TBird.GetEnumerator: TEnumerator;',
  15624. 'begin',
  15625. 'end;',
  15626. 'var',
  15627. ' b: TBird;',
  15628. ' i, i2: TItem;',
  15629. 'begin',
  15630. ' for i in b do i2:=i;']);
  15631. ConvertProgram;
  15632. CheckSource('TestClass_ForIn',
  15633. LinesToStr([ // statements
  15634. 'rtl.createClass(this, "TObject", null, function () {',
  15635. ' this.$init = function () {',
  15636. ' };',
  15637. ' this.$final = function () {',
  15638. ' };',
  15639. '});',
  15640. 'rtl.createClass(this, "TEnumerator", this.TObject, function () {',
  15641. ' this.$init = function () {',
  15642. ' $mod.TObject.$init.call(this);',
  15643. ' this.FCurrent = null;',
  15644. ' };',
  15645. ' this.$final = function () {',
  15646. ' this.FCurrent = undefined;',
  15647. ' $mod.TObject.$final.call(this);',
  15648. ' };',
  15649. ' this.MoveNext = function () {',
  15650. ' var Result = false;',
  15651. ' return Result;',
  15652. ' };',
  15653. '});',
  15654. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15655. ' this.GetEnumerator = function () {',
  15656. ' var Result = null;',
  15657. ' return Result;',
  15658. ' };',
  15659. '});',
  15660. 'this.b = null;',
  15661. 'this.i = null;',
  15662. 'this.i2 = null;'
  15663. ]),
  15664. LinesToStr([ // $mod.$main
  15665. 'var $in = $mod.b.GetEnumerator();',
  15666. 'try {',
  15667. ' while ($in.MoveNext()){',
  15668. ' $mod.i = $in.FCurrent;',
  15669. ' $mod.i2 = $mod.i;',
  15670. ' }',
  15671. '} finally {',
  15672. ' $in = rtl.freeLoc($in)',
  15673. '};',
  15674. '']));
  15675. end;
  15676. procedure TTestModule.TestClass_DispatchMessage;
  15677. begin
  15678. StartProgram(false);
  15679. Add([
  15680. 'type',
  15681. ' TObject = class',
  15682. ' {$DispatchField DispInt}',
  15683. ' procedure Dispatch(var Msg); virtual; abstract;',
  15684. ' {$DispatchStrField DispStr}',
  15685. ' procedure DispatchStr(var Msg); virtual; abstract;',
  15686. ' end;',
  15687. ' THopMsg = record',
  15688. ' DispInt: longint;',
  15689. ' end;',
  15690. ' TPutMsg = record',
  15691. ' DispStr: string;',
  15692. ' end;',
  15693. ' TBird = class',
  15694. ' procedure Fly(var Msg); virtual; abstract; message 2;',
  15695. ' procedure Run; overload; virtual; abstract;',
  15696. ' procedure Run(var Msg); overload; message ''Fast'';',
  15697. ' procedure Hop(var Msg: THopMsg); virtual; abstract; message 3;',
  15698. ' procedure Put(var Msg: TPutMsg); virtual; abstract; message ''foo'';',
  15699. ' end;',
  15700. 'procedure TBird.Run(var Msg);',
  15701. 'begin',
  15702. 'end;',
  15703. 'begin',
  15704. '']);
  15705. ConvertProgram;
  15706. CheckSource('TestClass_Message',
  15707. LinesToStr([ // statements
  15708. 'rtl.createClass(this, "TObject", null, function () {',
  15709. ' this.$init = function () {',
  15710. ' };',
  15711. ' this.$final = function () {',
  15712. ' };',
  15713. '});',
  15714. 'rtl.recNewT(this, "THopMsg", function () {',
  15715. ' this.DispInt = 0;',
  15716. ' this.$eq = function (b) {',
  15717. ' return this.DispInt === b.DispInt;',
  15718. ' };',
  15719. ' this.$assign = function (s) {',
  15720. ' this.DispInt = s.DispInt;',
  15721. ' return this;',
  15722. ' };',
  15723. '});',
  15724. 'rtl.recNewT(this, "TPutMsg", function () {',
  15725. ' this.DispStr = "";',
  15726. ' this.$eq = function (b) {',
  15727. ' return this.DispStr === b.DispStr;',
  15728. ' };',
  15729. ' this.$assign = function (s) {',
  15730. ' this.DispStr = s.DispStr;',
  15731. ' return this;',
  15732. ' };',
  15733. '});',
  15734. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15735. ' this.Run$1 = function (Msg) {',
  15736. ' };',
  15737. ' this.$msgint = {',
  15738. ' "2": "Fly",',
  15739. ' "3": "Hop"',
  15740. ' };',
  15741. ' this.$msgstr = {',
  15742. ' Fast: "Run$1",',
  15743. ' foo: "Put"',
  15744. ' };',
  15745. '});',
  15746. '']),
  15747. LinesToStr([ // $mod.$main
  15748. '']));
  15749. end;
  15750. procedure TTestModule.TestClass_Message_DuplicateIntFail;
  15751. begin
  15752. StartProgram(false);
  15753. Add([
  15754. 'type',
  15755. ' TObject = class',
  15756. ' procedure Fly(var Msg); virtual; abstract; message 3;',
  15757. ' procedure Run(var Msg); virtual; abstract; message 1+2;',
  15758. ' end;',
  15759. 'begin',
  15760. '']);
  15761. SetExpectedPasResolverError('Duplicate message id "3" at test1.pp(5,56)',nDuplicateMessageIdXAtY);
  15762. ConvertProgram;
  15763. end;
  15764. procedure TTestModule.TestClass_DispatchMessage_WrongFieldNameFail;
  15765. begin
  15766. StartProgram(false);
  15767. Add([
  15768. 'type',
  15769. ' TObject = class',
  15770. ' {$dispatchfield Msg}',
  15771. ' procedure Dispatch(var Msg); virtual; abstract;',
  15772. ' end;',
  15773. ' TFlyMsg = record',
  15774. ' FlyId: longint;',
  15775. ' end;',
  15776. ' TBird = class',
  15777. ' procedure Fly(var Msg: TFlyMsg); virtual; abstract; message 3;',
  15778. ' end;',
  15779. 'begin',
  15780. '']);
  15781. ConvertProgram;
  15782. CheckHint(mtWarning,nDispatchRequiresX,'Dispatch requires record field "Msg"');
  15783. end;
  15784. procedure TTestModule.TestClassOf_Create;
  15785. begin
  15786. StartProgram(false);
  15787. Add('type');
  15788. Add(' TObject = class');
  15789. Add(' constructor Create;');
  15790. Add(' end;');
  15791. Add(' TClass = class of TObject;');
  15792. Add('constructor tobject.create; begin end;');
  15793. Add('var');
  15794. Add(' Obj: tobject;');
  15795. Add(' C: tclass;');
  15796. Add('begin');
  15797. Add(' obj:=C.create;');
  15798. Add(' with c do obj:=create;');
  15799. ConvertProgram;
  15800. CheckSource('TestClassOf_Create',
  15801. LinesToStr([ // statements
  15802. 'rtl.createClass(this, "TObject", null, function () {',
  15803. ' this.$init = function () {',
  15804. ' };',
  15805. ' this.$final = function () {',
  15806. ' };',
  15807. ' this.Create = function () {',
  15808. ' return this;',
  15809. ' };',
  15810. '});',
  15811. 'this.Obj = null;',
  15812. 'this.C = null;'
  15813. ]),
  15814. LinesToStr([ // $mod.$main
  15815. '$mod.Obj = $mod.C.$create("Create");',
  15816. 'var $with = $mod.C;',
  15817. '$mod.Obj = $with.$create("Create");',
  15818. '']));
  15819. end;
  15820. procedure TTestModule.TestClassOf_Call;
  15821. begin
  15822. StartProgram(false);
  15823. Add('type');
  15824. Add(' TObject = class');
  15825. Add(' class procedure DoIt;');
  15826. Add(' end;');
  15827. Add(' TClass = class of TObject;');
  15828. Add('class procedure tobject.doit; begin end;');
  15829. Add('var');
  15830. Add(' C: tclass;');
  15831. Add('begin');
  15832. Add(' c.doit;');
  15833. Add(' with c do doit;');
  15834. ConvertProgram;
  15835. CheckSource('TestClassOf_Call',
  15836. LinesToStr([ // statements
  15837. 'rtl.createClass(this, "TObject", null, function () {',
  15838. ' this.$init = function () {',
  15839. ' };',
  15840. ' this.$final = function () {',
  15841. ' };',
  15842. ' this.DoIt = function () {',
  15843. ' };',
  15844. '});',
  15845. 'this.C = null;'
  15846. ]),
  15847. LinesToStr([ // $mod.$main
  15848. '$mod.C.DoIt();',
  15849. 'var $with = $mod.C;',
  15850. '$with.DoIt();',
  15851. '']));
  15852. end;
  15853. procedure TTestModule.TestClassOf_Assign;
  15854. begin
  15855. StartProgram(false);
  15856. Add('type');
  15857. Add(' TClass = class of TObject;');
  15858. Add(' TObject = class');
  15859. Add(' ClassType: TClass; ');
  15860. Add(' end;');
  15861. Add('var');
  15862. Add(' Obj: tobject;');
  15863. Add(' C: tclass;');
  15864. Add('begin');
  15865. Add(' c:=nil;');
  15866. Add(' c:=obj.classtype;');
  15867. ConvertProgram;
  15868. CheckSource('TestClassOf_Assign',
  15869. LinesToStr([ // statements
  15870. 'rtl.createClass(this, "TObject", null, function () {',
  15871. ' this.$init = function () {',
  15872. ' this.ClassType = null;',
  15873. ' };',
  15874. ' this.$final = function () {',
  15875. ' this.ClassType = undefined;',
  15876. ' };',
  15877. '});',
  15878. 'this.Obj = null;',
  15879. 'this.C = null;'
  15880. ]),
  15881. LinesToStr([ // $mod.$main
  15882. '$mod.C = null;',
  15883. '$mod.C = $mod.Obj.ClassType;',
  15884. '']));
  15885. end;
  15886. procedure TTestModule.TestClassOf_Is;
  15887. begin
  15888. StartProgram(false);
  15889. Add('type');
  15890. Add(' TClass = class of TObject;');
  15891. Add(' TObject = class');
  15892. Add(' end;');
  15893. Add(' TCar = class');
  15894. Add(' end;');
  15895. Add(' TCars = class of TCar;');
  15896. Add('var');
  15897. Add(' Obj: tobject;');
  15898. Add(' C: tclass;');
  15899. Add(' Cars: tcars;');
  15900. Add('begin');
  15901. Add(' if c is tcar then ;');
  15902. Add(' if c is tcars then ;');
  15903. ConvertProgram;
  15904. CheckSource('TestClassOf_Is',
  15905. LinesToStr([ // statements
  15906. 'rtl.createClass(this, "TObject", null, function () {',
  15907. ' this.$init = function () {',
  15908. ' };',
  15909. ' this.$final = function () {',
  15910. ' };',
  15911. '});',
  15912. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  15913. '});',
  15914. 'this.Obj = null;',
  15915. 'this.C = null;',
  15916. 'this.Cars = null;'
  15917. ]),
  15918. LinesToStr([ // $mod.$main
  15919. 'if(rtl.is($mod.C,$mod.TCar));',
  15920. 'if(rtl.is($mod.C,$mod.TCar));',
  15921. '']));
  15922. end;
  15923. procedure TTestModule.TestClassOf_Compare;
  15924. begin
  15925. StartProgram(false);
  15926. Add('type');
  15927. Add(' TClass = class of TObject;');
  15928. Add(' TObject = class');
  15929. Add(' ClassType: TClass; ');
  15930. Add(' end;');
  15931. Add('var');
  15932. Add(' b: boolean;');
  15933. Add(' Obj: tobject;');
  15934. Add(' C: tclass;');
  15935. Add('begin');
  15936. Add(' b:=c=nil;');
  15937. Add(' b:=nil=c;');
  15938. Add(' b:=c=obj.classtype;');
  15939. Add(' b:=obj.classtype=c;');
  15940. Add(' b:=c=TObject;');
  15941. Add(' b:=TObject=c;');
  15942. Add(' b:=c<>nil;');
  15943. Add(' b:=nil<>c;');
  15944. Add(' b:=c<>obj.classtype;');
  15945. Add(' b:=obj.classtype<>c;');
  15946. Add(' b:=c<>TObject;');
  15947. Add(' b:=TObject<>c;');
  15948. ConvertProgram;
  15949. CheckSource('TestClassOf_Compare',
  15950. LinesToStr([ // statements
  15951. 'rtl.createClass(this, "TObject", null, function () {',
  15952. ' this.$init = function () {',
  15953. ' this.ClassType = null;',
  15954. ' };',
  15955. ' this.$final = function () {',
  15956. ' this.ClassType = undefined;',
  15957. ' };',
  15958. '});',
  15959. 'this.b = false;',
  15960. 'this.Obj = null;',
  15961. 'this.C = null;'
  15962. ]),
  15963. LinesToStr([ // $mod.$main
  15964. '$mod.b = $mod.C === null;',
  15965. '$mod.b = null === $mod.C;',
  15966. '$mod.b = $mod.C === $mod.Obj.ClassType;',
  15967. '$mod.b = $mod.Obj.ClassType === $mod.C;',
  15968. '$mod.b = $mod.C === $mod.TObject;',
  15969. '$mod.b = $mod.TObject === $mod.C;',
  15970. '$mod.b = $mod.C !== null;',
  15971. '$mod.b = null !== $mod.C;',
  15972. '$mod.b = $mod.C !== $mod.Obj.ClassType;',
  15973. '$mod.b = $mod.Obj.ClassType !== $mod.C;',
  15974. '$mod.b = $mod.C !== $mod.TObject;',
  15975. '$mod.b = $mod.TObject !== $mod.C;',
  15976. '']));
  15977. end;
  15978. procedure TTestModule.TestClassOf_ClassVar;
  15979. begin
  15980. StartProgram(false);
  15981. Add('type');
  15982. Add(' TObject = class');
  15983. Add(' class var id: longint;');
  15984. Add(' end;');
  15985. Add(' TClass = class of TObject;');
  15986. Add('var');
  15987. Add(' C: tclass;');
  15988. Add('begin');
  15989. Add(' C.id:=C.id;');
  15990. ConvertProgram;
  15991. CheckSource('TestClassOf_ClassVar',
  15992. LinesToStr([ // statements
  15993. 'rtl.createClass(this, "TObject", null, function () {',
  15994. ' this.id = 0;',
  15995. ' this.$init = function () {',
  15996. ' };',
  15997. ' this.$final = function () {',
  15998. ' };',
  15999. '});',
  16000. 'this.C = null;'
  16001. ]),
  16002. LinesToStr([ // $mod.$main
  16003. '$mod.TObject.id = $mod.C.id;',
  16004. '']));
  16005. end;
  16006. procedure TTestModule.TestClassOf_ClassMethod;
  16007. begin
  16008. StartProgram(false);
  16009. Add('type');
  16010. Add(' TObject = class');
  16011. Add(' class function DoIt(i: longint = 0): longint;');
  16012. Add(' end;');
  16013. Add(' TClass = class of TObject;');
  16014. Add('class function tobject.doit(i: longint = 0): longint; begin end;');
  16015. Add('var');
  16016. Add(' i: longint;');
  16017. Add(' C: tclass;');
  16018. Add('begin');
  16019. Add(' C.DoIt;');
  16020. Add(' C.DoIt();');
  16021. Add(' i:=C.DoIt;');
  16022. Add(' i:=C.DoIt();');
  16023. ConvertProgram;
  16024. CheckSource('TestClassOf_ClassMethod',
  16025. LinesToStr([ // statements
  16026. 'rtl.createClass(this, "TObject", null, function () {',
  16027. ' this.$init = function () {',
  16028. ' };',
  16029. ' this.$final = function () {',
  16030. ' };',
  16031. ' this.DoIt = function (i) {',
  16032. ' var Result = 0;',
  16033. ' return Result;',
  16034. ' };',
  16035. '});',
  16036. 'this.i = 0;',
  16037. 'this.C = null;'
  16038. ]),
  16039. LinesToStr([ // $mod.$main
  16040. '$mod.C.DoIt(0);',
  16041. '$mod.C.DoIt(0);',
  16042. '$mod.i = $mod.C.DoIt(0);',
  16043. '$mod.i = $mod.C.DoIt(0);',
  16044. '']));
  16045. end;
  16046. procedure TTestModule.TestClassOf_ClassProperty;
  16047. begin
  16048. StartProgram(false);
  16049. Add([
  16050. 'type',
  16051. ' TObject = class',
  16052. ' class var FA: longint;',
  16053. ' class function GetA: longint;',
  16054. ' class procedure SetA(Value: longint);',
  16055. ' class property pA: longint read fa write fa;',
  16056. ' class property pB: longint read geta write seta;',
  16057. ' end;',
  16058. ' TObjectClass = class of tobject;',
  16059. 'class function tobject.geta: longint; begin end;',
  16060. 'class procedure tobject.seta(value: longint); begin end;',
  16061. 'var',
  16062. ' b: boolean;',
  16063. ' Obj: tobject;',
  16064. ' Cla: tobjectclass;',
  16065. 'begin',
  16066. ' obj.pa:=obj.pa;',
  16067. ' obj.pb:=obj.pb;',
  16068. ' b:=obj.pa=4;',
  16069. ' b:=obj.pb=obj.pb;',
  16070. ' b:=5=obj.pa;',
  16071. ' cla.pa:=6;',
  16072. ' cla.pa:=cla.pa;',
  16073. ' cla.pb:=cla.pb;',
  16074. ' b:=cla.pa=7;',
  16075. ' b:=cla.pb=cla.pb;',
  16076. ' b:=8=cla.pa;',
  16077. ' tobject.pa:=9;',
  16078. ' tobject.pb:=tobject.pb;',
  16079. ' b:=tobject.pa=10;',
  16080. ' b:=11=tobject.pa;',
  16081. '']);
  16082. ConvertProgram;
  16083. CheckSource('TestClassOf_ClassProperty',
  16084. LinesToStr([ // statements
  16085. 'rtl.createClass(this, "TObject", null, function () {',
  16086. ' this.FA = 0;',
  16087. ' this.$init = function () {',
  16088. ' };',
  16089. ' this.$final = function () {',
  16090. ' };',
  16091. ' this.GetA = function () {',
  16092. ' var Result = 0;',
  16093. ' return Result;',
  16094. ' };',
  16095. ' this.SetA = function (Value) {',
  16096. ' };',
  16097. '});',
  16098. 'this.b = false;',
  16099. 'this.Obj = null;',
  16100. 'this.Cla = null;'
  16101. ]),
  16102. LinesToStr([ // $mod.$main
  16103. '$mod.TObject.FA = $mod.Obj.FA;',
  16104. '$mod.Obj.$class.SetA($mod.Obj.$class.GetA());',
  16105. '$mod.b = $mod.Obj.FA === 4;',
  16106. '$mod.b = $mod.Obj.$class.GetA() === $mod.Obj.$class.GetA();',
  16107. '$mod.b = 5 === $mod.Obj.FA;',
  16108. '$mod.TObject.FA = 6;',
  16109. '$mod.TObject.FA = $mod.Cla.FA;',
  16110. '$mod.Cla.SetA($mod.Cla.GetA());',
  16111. '$mod.b = $mod.Cla.FA === 7;',
  16112. '$mod.b = $mod.Cla.GetA() === $mod.Cla.GetA();',
  16113. '$mod.b = 8 === $mod.Cla.FA;',
  16114. '$mod.TObject.FA = 9;',
  16115. '$mod.TObject.SetA($mod.TObject.GetA());',
  16116. '$mod.b = $mod.TObject.FA === 10;',
  16117. '$mod.b = 11 === $mod.TObject.FA;',
  16118. '']));
  16119. end;
  16120. procedure TTestModule.TestClassOf_ClassMethodSelf;
  16121. begin
  16122. StartProgram(false);
  16123. Add('type');
  16124. Add(' TObject = class');
  16125. Add(' class var GlobalId: longint;');
  16126. Add(' class procedure ProcA;');
  16127. Add(' end;');
  16128. Add('class procedure tobject.proca;');
  16129. Add('var b: boolean;');
  16130. Add('begin');
  16131. Add(' b:=self=nil;');
  16132. Add(' b:=self.globalid=3;');
  16133. Add(' b:=4=self.globalid;');
  16134. Add(' self.globalid:=5;');
  16135. Add(' self.proca;');
  16136. Add('end;');
  16137. Add('begin');
  16138. ConvertProgram;
  16139. CheckSource('TestClassOf_ClassMethodSelf',
  16140. LinesToStr([ // statements
  16141. 'rtl.createClass(this, "TObject", null, function () {',
  16142. ' this.GlobalId = 0;',
  16143. ' this.$init = function () {',
  16144. ' };',
  16145. ' this.$final = function () {',
  16146. ' };',
  16147. ' this.ProcA = function () {',
  16148. ' var b = false;',
  16149. ' b = this === null;',
  16150. ' b = this.GlobalId === 3;',
  16151. ' b = 4 === this.GlobalId;',
  16152. ' $mod.TObject.GlobalId = 5;',
  16153. ' this.ProcA();',
  16154. ' };',
  16155. '});'
  16156. ]),
  16157. LinesToStr([ // $mod.$main
  16158. '']));
  16159. end;
  16160. procedure TTestModule.TestClassOf_TypeCast;
  16161. begin
  16162. StartProgram(false);
  16163. Add('type');
  16164. Add(' TObject = class');
  16165. Add(' class procedure {#TObject_DoIt}DoIt;');
  16166. Add(' end;');
  16167. Add(' TClass = class of TObject;');
  16168. Add(' TMobile = class');
  16169. Add(' class procedure {#TMobile_DoIt}DoIt;');
  16170. Add(' end;');
  16171. Add(' TMobileClass = class of TMobile;');
  16172. Add(' TCar = class(TMobile)');
  16173. Add(' class procedure {#TCar_DoIt}DoIt;');
  16174. Add(' end;');
  16175. Add(' TCarClass = class of TCar;');
  16176. Add('class procedure TObject.DoIt;');
  16177. Add('begin');
  16178. Add(' TClass(Self).{@TObject_DoIt}DoIt;');
  16179. Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
  16180. Add('end;');
  16181. Add('class procedure TMobile.DoIt;');
  16182. Add('begin');
  16183. Add(' TClass(Self).{@TObject_DoIt}DoIt;');
  16184. Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
  16185. Add(' TCarClass(Self).{@TCar_DoIt}DoIt;');
  16186. Add('end;');
  16187. Add('class procedure TCar.DoIt; begin end;');
  16188. Add('var');
  16189. Add(' ObjC: TClass;');
  16190. Add(' MobileC: TMobileClass;');
  16191. Add(' CarC: TCarClass;');
  16192. Add('begin');
  16193. Add(' ObjC.{@TObject_DoIt}DoIt;');
  16194. Add(' MobileC.{@TMobile_DoIt}DoIt;');
  16195. Add(' CarC.{@TCar_DoIt}DoIt;');
  16196. Add(' TClass(ObjC).{@TObject_DoIt}DoIt;');
  16197. Add(' TMobileClass(ObjC).{@TMobile_DoIt}DoIt;');
  16198. Add(' TCarClass(ObjC).{@TCar_DoIt}DoIt;');
  16199. Add(' TClass(MobileC).{@TObject_DoIt}DoIt;');
  16200. Add(' TMobileClass(MobileC).{@TMobile_DoIt}DoIt;');
  16201. Add(' TCarClass(MobileC).{@TCar_DoIt}DoIt;');
  16202. Add(' TClass(CarC).{@TObject_DoIt}DoIt;');
  16203. Add(' TMobileClass(CarC).{@TMobile_DoIt}DoIt;');
  16204. Add(' TCarClass(CarC).{@TCar_DoIt}DoIt;');
  16205. ConvertProgram;
  16206. CheckSource('TestClassOf_TypeCast',
  16207. LinesToStr([ // statements
  16208. 'rtl.createClass(this, "TObject", null, function () {',
  16209. ' this.$init = function () {',
  16210. ' };',
  16211. ' this.$final = function () {',
  16212. ' };',
  16213. ' this.DoIt = function () {',
  16214. ' this.DoIt();',
  16215. ' this.DoIt$1();',
  16216. ' };',
  16217. '});',
  16218. 'rtl.createClass(this, "TMobile", this.TObject, function () {',
  16219. ' this.DoIt$1 = function () {',
  16220. ' this.DoIt();',
  16221. ' this.DoIt$1();',
  16222. ' this.DoIt$2();',
  16223. ' };',
  16224. '});',
  16225. 'rtl.createClass(this, "TCar", this.TMobile, function () {',
  16226. ' this.DoIt$2 = function () {',
  16227. ' };',
  16228. '});',
  16229. 'this.ObjC = null;',
  16230. 'this.MobileC = null;',
  16231. 'this.CarC = null;',
  16232. '']),
  16233. LinesToStr([ // $mod.$main
  16234. '$mod.ObjC.DoIt();',
  16235. '$mod.MobileC.DoIt$1();',
  16236. '$mod.CarC.DoIt$2();',
  16237. '$mod.ObjC.DoIt();',
  16238. '$mod.ObjC.DoIt$1();',
  16239. '$mod.ObjC.DoIt$2();',
  16240. '$mod.MobileC.DoIt();',
  16241. '$mod.MobileC.DoIt$1();',
  16242. '$mod.MobileC.DoIt$2();',
  16243. '$mod.CarC.DoIt();',
  16244. '$mod.CarC.DoIt$1();',
  16245. '$mod.CarC.DoIt$2();',
  16246. '']));
  16247. end;
  16248. procedure TTestModule.TestClassOf_ImplicitFunctionCall;
  16249. begin
  16250. StartProgram(false);
  16251. Add('type');
  16252. Add(' TObject = class');
  16253. Add(' function CurNow: longint; ');
  16254. Add(' class function Now: longint; ');
  16255. Add(' end;');
  16256. Add('function TObject.CurNow: longint; begin end;');
  16257. Add('class function TObject.Now: longint; begin end;');
  16258. Add('var');
  16259. Add(' Obj: tobject;');
  16260. Add(' vI: longint;');
  16261. Add('begin');
  16262. Add(' obj.curnow;');
  16263. Add(' vi:=obj.curnow;');
  16264. Add(' tobject.now;');
  16265. Add(' vi:=tobject.now;');
  16266. ConvertProgram;
  16267. CheckSource('TestClassOf_ImplicitFunctionCall',
  16268. LinesToStr([ // statements
  16269. 'rtl.createClass(this, "TObject", null, function () {',
  16270. ' this.$init = function () {',
  16271. ' };',
  16272. ' this.$final = function () {',
  16273. ' };',
  16274. ' this.CurNow = function () {',
  16275. ' var Result = 0;',
  16276. ' return Result;',
  16277. ' };',
  16278. ' this.Now = function () {',
  16279. ' var Result = 0;',
  16280. ' return Result;',
  16281. ' };',
  16282. '});',
  16283. 'this.Obj = null;',
  16284. 'this.vI = 0;',
  16285. '']),
  16286. LinesToStr([ // $mod.$main
  16287. '$mod.Obj.CurNow();',
  16288. '$mod.vI = $mod.Obj.CurNow();',
  16289. '$mod.TObject.Now();',
  16290. '$mod.vI = $mod.TObject.Now();',
  16291. '']));
  16292. end;
  16293. procedure TTestModule.TestClassOf_Const;
  16294. begin
  16295. StartProgram(false);
  16296. Add([
  16297. 'type',
  16298. ' TObject = class',
  16299. ' end;',
  16300. ' TBird = TObject;',
  16301. ' TBirds = class of TBird;',
  16302. ' TEagles = TBirds;',
  16303. ' THawk = class(TBird);',
  16304. 'const',
  16305. ' Hawk: TEagles = THawk;',
  16306. ' DefaultBirdClasses : Array [1..2] of TEagles = (',
  16307. ' TBird,',
  16308. ' THawk',
  16309. ' );',
  16310. 'begin']);
  16311. ConvertProgram;
  16312. CheckSource('TestClassOf_Const',
  16313. LinesToStr([ // statements
  16314. 'rtl.createClass(this, "TObject", null, function () {',
  16315. ' this.$init = function () {',
  16316. ' };',
  16317. ' this.$final = function () {',
  16318. ' };',
  16319. '});',
  16320. 'rtl.createClass(this, "THawk", this.TObject, function () {',
  16321. '});',
  16322. 'this.Hawk = this.THawk;',
  16323. 'this.DefaultBirdClasses = [this.TObject, this.THawk];',
  16324. '']),
  16325. LinesToStr([ // $mod.$main
  16326. '']));
  16327. end;
  16328. procedure TTestModule.TestNestedClass_Alias;
  16329. begin
  16330. WithTypeInfo:=true;
  16331. StartProgram(false);
  16332. Add([
  16333. 'type',
  16334. ' TObject = class',
  16335. ' type TNested = type longint;',
  16336. ' end;',
  16337. 'type TAlias = type tobject.tnested;',
  16338. 'var i: tobject.tnested = 3;',
  16339. 'var j: TAlias = 4;',
  16340. 'begin',
  16341. ' if typeinfo(TAlias)=nil then ;',
  16342. ' if typeinfo(tobject.tnested)=nil then ;',
  16343. '']);
  16344. ConvertProgram;
  16345. CheckSource('TestNestedClass_Alias',
  16346. LinesToStr([ // statements
  16347. 'rtl.createClass(this, "TObject", null, function () {',
  16348. ' $mod.$rtti.$inherited("TObject.TNested", rtl.longint, {});',
  16349. ' this.$init = function () {',
  16350. ' };',
  16351. ' this.$final = function () {',
  16352. ' };',
  16353. '});',
  16354. 'this.$rtti.$inherited("TAlias", this.$rtti["TObject.TNested"], {});',
  16355. 'this.i = 3;',
  16356. 'this.j = 4;',
  16357. '']),
  16358. LinesToStr([ // $mod.$main
  16359. 'if ($mod.$rtti["TAlias"] === null) ;',
  16360. 'if ($mod.$rtti["TObject.TNested"] === null) ;',
  16361. '']));
  16362. end;
  16363. procedure TTestModule.TestNestedClass_Record;
  16364. begin
  16365. WithTypeInfo:=true;
  16366. StartProgram(false);
  16367. Add([
  16368. 'type',
  16369. ' TObject = class',
  16370. ' type TPoint = record',
  16371. ' x,y: byte;',
  16372. ' end;',
  16373. ' procedure DoIt(t: TPoint);',
  16374. ' end;',
  16375. 'procedure tobject.DoIt(t: TPoint);',
  16376. 'var p: TPoint;',
  16377. 'begin',
  16378. ' t.x:=t.y;',
  16379. ' p:=t;',
  16380. 'end;',
  16381. 'var',
  16382. ' p: tobject.tpoint = (x:2; y:4);',
  16383. ' o: TObject;',
  16384. 'begin',
  16385. ' p:=p;',
  16386. ' o.doit(p);',
  16387. '']);
  16388. ConvertProgram;
  16389. CheckSource('TestNestedClass_Record',
  16390. LinesToStr([ // statements
  16391. 'rtl.createClass(this, "TObject", null, function () {',
  16392. ' rtl.recNewT(this, "TPoint", function () {',
  16393. ' this.x = 0;',
  16394. ' this.y = 0;',
  16395. ' this.$eq = function (b) {',
  16396. ' return (this.x === b.x) && (this.y === b.y);',
  16397. ' };',
  16398. ' this.$assign = function (s) {',
  16399. ' this.x = s.x;',
  16400. ' this.y = s.y;',
  16401. ' return this;',
  16402. ' };',
  16403. ' var $r = $mod.$rtti.$Record("TObject.TPoint", {});',
  16404. ' $r.addField("x", rtl.byte);',
  16405. ' $r.addField("y", rtl.byte);',
  16406. ' });',
  16407. ' this.$init = function () {',
  16408. ' };',
  16409. ' this.$final = function () {',
  16410. ' };',
  16411. ' this.DoIt = function (t) {',
  16412. ' var p = this.TPoint.$new();',
  16413. ' t.x = t.y;',
  16414. ' p.$assign(t);',
  16415. ' };',
  16416. '});',
  16417. 'this.p = this.TObject.TPoint.$clone({',
  16418. ' x: 2,',
  16419. ' y: 4',
  16420. '});',
  16421. 'this.o = null;',
  16422. '']),
  16423. LinesToStr([ // $mod.$main
  16424. '$mod.p.$assign($mod.p);',
  16425. '$mod.o.DoIt($mod.TObject.TPoint.$clone($mod.p));',
  16426. '']));
  16427. end;
  16428. procedure TTestModule.TestNestedClass_Class;
  16429. begin
  16430. WithTypeInfo:=true;
  16431. StartProgram(false);
  16432. Add([
  16433. 'type',
  16434. ' TObject = class end;',
  16435. ' TBird = class',
  16436. ' type TLeg = class',
  16437. ' FId: longint;',
  16438. ' constructor Create;',
  16439. ' function Create(i: longint): TLeg;',
  16440. ' end;',
  16441. ' function DoIt(b: TBird): Tleg;',
  16442. ' end;',
  16443. 'constructor tbird.tleg.create;',
  16444. 'begin',
  16445. ' FId:=3;',
  16446. 'end;',
  16447. 'function tbird.tleg.Create(i: longint): TLeg;',
  16448. 'begin',
  16449. ' Create;',
  16450. ' Result:=TLeg.Create;',
  16451. ' Result:=TBird.TLeg.Create;',
  16452. ' Result:=Create(3);',
  16453. ' FId:=i;',
  16454. 'end;',
  16455. 'function tbird.DoIt(b: tbird): tleg;',
  16456. 'begin',
  16457. ' Result.Create;',
  16458. ' Result:=TLeg.Create;',
  16459. ' Result:=TBird.TLeg.Create;',
  16460. ' Result:=Result.Create(3);',
  16461. 'end;',
  16462. 'var',
  16463. ' b: Tbird.tleg;',
  16464. 'begin',
  16465. ' b.Create;',
  16466. ' b:=TBird.TLeg.Create;',
  16467. ' b:=b.Create(3);',
  16468. '']);
  16469. ConvertProgram;
  16470. CheckSource('TestNestedClass_Class',
  16471. LinesToStr([ // statements
  16472. 'rtl.createClass(this, "TObject", null, function () {',
  16473. ' this.$init = function () {',
  16474. ' };',
  16475. ' this.$final = function () {',
  16476. ' };',
  16477. '});',
  16478. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16479. ' rtl.createClass(this, "TLeg", $mod.TObject, function () {',
  16480. ' this.$init = function () {',
  16481. ' $mod.TObject.$init.call(this);',
  16482. ' this.FId = 0;',
  16483. ' };',
  16484. ' this.Create = function () {',
  16485. ' this.FId = 3;',
  16486. ' return this;',
  16487. ' };',
  16488. ' this.Create$1 = function (i) {',
  16489. ' var Result = null;',
  16490. ' this.Create();',
  16491. ' Result = $mod.TBird.TLeg.$create("Create");',
  16492. ' Result = $mod.TBird.TLeg.$create("Create");',
  16493. ' Result = this.Create$1(3);',
  16494. ' this.FId = i;',
  16495. ' return Result;',
  16496. ' };',
  16497. ' }, "TBird.TLeg");',
  16498. ' this.DoIt = function (b) {',
  16499. ' var Result = null;',
  16500. ' Result.Create();',
  16501. ' Result = this.TLeg.$create("Create");',
  16502. ' Result = $mod.TBird.TLeg.$create("Create");',
  16503. ' Result = Result.Create$1(3);',
  16504. ' return Result;',
  16505. ' };',
  16506. '});',
  16507. 'this.b = null;',
  16508. '']),
  16509. LinesToStr([ // $mod.$main
  16510. '$mod.b.Create();',
  16511. '$mod.b = $mod.TBird.TLeg.$create("Create");',
  16512. '$mod.b = $mod.b.Create$1(3);',
  16513. '']));
  16514. end;
  16515. procedure TTestModule.TestExternalClass_Var;
  16516. begin
  16517. StartProgram(false);
  16518. Add([
  16519. '{$modeswitch externalclass}',
  16520. 'type',
  16521. ' TExtA = class external name ''ExtObj''',
  16522. ' Id: longint external name ''$Id'';',
  16523. ' B: longint;',
  16524. ' end;',
  16525. 'var Obj: TExtA;',
  16526. 'begin',
  16527. ' obj.id:=obj.id+1;',
  16528. ' obj.B:=obj.B+1;']);
  16529. ConvertProgram;
  16530. CheckSource('TestExternalClass_Var',
  16531. LinesToStr([ // statements
  16532. 'this.Obj = null;',
  16533. '']),
  16534. LinesToStr([ // $mod.$main
  16535. '$mod.Obj.$Id = $mod.Obj.$Id + 1;',
  16536. '$mod.Obj.B = $mod.Obj.B + 1;',
  16537. '']));
  16538. end;
  16539. procedure TTestModule.TestExternalClass_Const;
  16540. begin
  16541. StartProgram(false);
  16542. Add([
  16543. '{$modeswitch externalclass}',
  16544. 'type',
  16545. ' TExtA = class external name ''ExtObj''',
  16546. ' const Two: longint = 2;',
  16547. ' const Three = 3;',
  16548. ' const Id: longint;',
  16549. ' end;',
  16550. ' TExtB = class external name ''ExtB''',
  16551. ' A: TExtA;',
  16552. ' end;',
  16553. 'var',
  16554. ' A: texta;',
  16555. ' B: textb;',
  16556. ' i: longint;',
  16557. 'begin',
  16558. ' i:=a.two;',
  16559. ' i:=texta.two;',
  16560. ' i:=a.three;',
  16561. ' i:=texta.three;',
  16562. ' i:=a.id;',
  16563. ' i:=texta.id;',
  16564. '']);
  16565. ConvertProgram;
  16566. CheckSource('TestExternalClass_Const',
  16567. LinesToStr([ // statements
  16568. 'this.A = null;',
  16569. 'this.B = null;',
  16570. 'this.i = 0;',
  16571. '']),
  16572. LinesToStr([ // $mod.$main
  16573. '$mod.i = 2;',
  16574. '$mod.i = 2;',
  16575. '$mod.i = 3;',
  16576. '$mod.i = 3;',
  16577. '$mod.i = $mod.A.Id;',
  16578. '$mod.i = ExtObj.Id;',
  16579. '']));
  16580. end;
  16581. procedure TTestModule.TestExternalClass_Dollar;
  16582. begin
  16583. StartProgram(false);
  16584. Add([
  16585. '{$modeswitch externalclass}',
  16586. 'type',
  16587. ' TExtA = class external name ''$''',
  16588. ' Id: longint external name ''$'';',
  16589. ' function Bla(i: longint): longint; external name ''$'';',
  16590. ' end;',
  16591. 'function dollar(k: longint): longint; external name ''$'';',
  16592. 'var Obj: TExtA;',
  16593. 'begin',
  16594. ' dollar(1);',
  16595. ' obj.id:=obj.id+2;',
  16596. ' obj.Bla(3);',
  16597. '']);
  16598. ConvertProgram;
  16599. CheckSource('TestExternalClass_Dollar',
  16600. LinesToStr([ // statements
  16601. 'this.Obj = null;',
  16602. '']),
  16603. LinesToStr([ // $mod.$main
  16604. '$(1);',
  16605. '$mod.Obj.$ = $mod.Obj.$ + 2;',
  16606. '$mod.Obj.$(3);',
  16607. '']));
  16608. end;
  16609. procedure TTestModule.TestExternalClass_DuplicateVarFail;
  16610. begin
  16611. StartProgram(false);
  16612. Add('{$modeswitch externalclass}');
  16613. Add('type');
  16614. Add(' TExtA = class external name ''ExtA''');
  16615. Add(' Id: longint external name ''$Id'';');
  16616. Add(' end;');
  16617. Add(' TExtB = class external ''lib'' name ''ExtB''(TExtA)');
  16618. Add(' Id: longint;');
  16619. Add(' end;');
  16620. Add('begin');
  16621. SetExpectedPasResolverError('Duplicate identifier "Id" at test1.pp(6,5)',nDuplicateIdentifier);
  16622. ConvertProgram;
  16623. end;
  16624. procedure TTestModule.TestExternalClass_Method;
  16625. begin
  16626. StartProgram(false);
  16627. Add(['{$modeswitch externalclass}',
  16628. 'type',
  16629. ' TExtA = class external name ''ExtObj''',
  16630. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  16631. ' procedure DoSome(Id: longint = 1);',
  16632. ' end;',
  16633. 'var Obj: texta;',
  16634. 'begin',
  16635. ' obj.doit;',
  16636. ' obj.doit();',
  16637. ' obj.doit(2);',
  16638. ' with obj do begin',
  16639. ' doit;',
  16640. ' doit();',
  16641. ' doit(3);',
  16642. ' end;']);
  16643. ConvertProgram;
  16644. CheckSource('TestExternalClass_Method',
  16645. LinesToStr([ // statements
  16646. 'this.Obj = null;',
  16647. '']),
  16648. LinesToStr([ // $mod.$main
  16649. '$mod.Obj.$Execute(1);',
  16650. '$mod.Obj.$Execute(1);',
  16651. '$mod.Obj.$Execute(2);',
  16652. 'var $with = $mod.Obj;',
  16653. '$with.$Execute(1);',
  16654. '$with.$Execute(1);',
  16655. '$with.$Execute(3);',
  16656. '']));
  16657. end;
  16658. procedure TTestModule.TestExternalClass_ClassMethod;
  16659. begin
  16660. StartProgram(false);
  16661. Add([
  16662. '{$modeswitch externalclass}',
  16663. 'type',
  16664. ' TExtA = class external name ''ExtObj''',
  16665. ' class procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  16666. ' end;',
  16667. ' TExtB = TExtA;',
  16668. 'var p: Pointer;',
  16669. 'begin',
  16670. ' texta.doit;',
  16671. ' texta.doit();',
  16672. ' texta.doit(2);',
  16673. ' p:[email protected];',
  16674. ' with texta do begin',
  16675. ' doit;',
  16676. ' doit();',
  16677. ' doit(3);',
  16678. ' p:=@DoIt;',
  16679. ' end;',
  16680. ' textb.doit;',
  16681. ' textb.doit();',
  16682. ' textb.doit(4);',
  16683. ' with textb do begin',
  16684. ' doit;',
  16685. ' doit();',
  16686. ' doit(5);',
  16687. ' end;',
  16688. '']);
  16689. ConvertProgram;
  16690. CheckSource('TestExternalClass_ClassMethod',
  16691. LinesToStr([ // statements
  16692. 'this.p = null;',
  16693. '']),
  16694. LinesToStr([ // $mod.$main
  16695. 'ExtObj.$Execute(1);',
  16696. 'ExtObj.$Execute(1);',
  16697. 'ExtObj.$Execute(2);',
  16698. '$mod.p = rtl.createCallback(ExtObj, "$Execute");',
  16699. 'ExtObj.$Execute(1);',
  16700. 'ExtObj.$Execute(1);',
  16701. 'ExtObj.$Execute(3);',
  16702. '$mod.p = rtl.createCallback(ExtObj, "$Execute");',
  16703. 'ExtObj.$Execute(1);',
  16704. 'ExtObj.$Execute(1);',
  16705. 'ExtObj.$Execute(4);',
  16706. 'ExtObj.$Execute(1);',
  16707. 'ExtObj.$Execute(1);',
  16708. 'ExtObj.$Execute(5);',
  16709. '']));
  16710. end;
  16711. procedure TTestModule.TestExternalClass_ClassMethodStatic;
  16712. begin
  16713. StartProgram(false);
  16714. Add([
  16715. '{$modeswitch externalclass}',
  16716. 'type',
  16717. ' TExtA = class external name ''ExtObj''',
  16718. ' class procedure DoIt(Id: longint = 1); static;',
  16719. ' end;',
  16720. 'var p: Pointer;',
  16721. 'begin',
  16722. ' texta.doit;',
  16723. ' texta.doit();',
  16724. ' texta.doit(2);',
  16725. ' p:[email protected];',
  16726. ' with texta do begin',
  16727. ' doit;',
  16728. ' doit();',
  16729. ' doit(3);',
  16730. ' p:=@DoIt;',
  16731. ' end;',
  16732. '']);
  16733. ConvertProgram;
  16734. CheckSource('TestExternalClass_ClassMethodStatic',
  16735. LinesToStr([ // statements
  16736. 'this.p = null;',
  16737. '']),
  16738. LinesToStr([ // $mod.$main
  16739. 'ExtObj.DoIt(1);',
  16740. 'ExtObj.DoIt(1);',
  16741. 'ExtObj.DoIt(2);',
  16742. '$mod.p = ExtObj.DoIt;',
  16743. 'ExtObj.DoIt(1);',
  16744. 'ExtObj.DoIt(1);',
  16745. 'ExtObj.DoIt(3);',
  16746. '$mod.p = ExtObj.DoIt;',
  16747. '']));
  16748. end;
  16749. procedure TTestModule.TestExternalClass_FunctionResultInTypeCast;
  16750. begin
  16751. StartProgram(false);
  16752. Add([
  16753. '{$modeswitch externalclass}',
  16754. 'type',
  16755. ' TBird = class external name ''Array''',
  16756. ' end;',
  16757. 'function GetPtr: Pointer;',
  16758. 'begin',
  16759. 'end;',
  16760. 'procedure Write(const p);',
  16761. 'begin',
  16762. 'end;',
  16763. 'procedure WriteLn; varargs;',
  16764. 'begin',
  16765. 'end;',
  16766. 'begin',
  16767. ' if TBird(GetPtr)=nil then ;',
  16768. ' Write(GetPtr);',
  16769. ' WriteLn(GetPtr);',
  16770. ' Write(TBird(GetPtr));',
  16771. ' WriteLn(TBird(GetPtr));',
  16772. '']);
  16773. ConvertProgram;
  16774. CheckSource('TestFunctionResultInTypeCast',
  16775. LinesToStr([ // statements
  16776. 'this.GetPtr = function () {',
  16777. ' var Result = null;',
  16778. ' return Result;',
  16779. '};',
  16780. 'this.Write = function (p) {',
  16781. '};',
  16782. 'this.WriteLn = function () {',
  16783. '};',
  16784. '']),
  16785. LinesToStr([
  16786. 'if ($mod.GetPtr() === null) ;',
  16787. '$mod.Write($mod.GetPtr());',
  16788. '$mod.WriteLn($mod.GetPtr());',
  16789. '$mod.Write($mod.GetPtr());',
  16790. '$mod.WriteLn($mod.GetPtr());',
  16791. '']));
  16792. end;
  16793. procedure TTestModule.TestExternalClass_NonExternalOverride;
  16794. begin
  16795. StartProgram(false);
  16796. Add([
  16797. '{$modeswitch externalclass}',
  16798. 'type',
  16799. ' TExtA = class external name ''ExtObjA''',
  16800. ' procedure ProcA; virtual;',
  16801. ' procedure ProcB; virtual;',
  16802. ' end;',
  16803. ' TExtB = class external name ''ExtObjB'' (TExtA)',
  16804. ' end;',
  16805. ' TExtC = class (TExtB)',
  16806. ' procedure ProcA; override;',
  16807. ' end;',
  16808. 'procedure TExtC.ProcA;',
  16809. 'begin',
  16810. ' ProcA;',
  16811. ' Self.ProcA;',
  16812. ' ProcB;',
  16813. ' Self.ProcB;',
  16814. 'end;',
  16815. 'var',
  16816. ' A: texta;',
  16817. ' B: textb;',
  16818. ' C: textc;',
  16819. 'begin',
  16820. ' a.proca;',
  16821. ' b.proca;',
  16822. ' c.proca;']);
  16823. ConvertProgram;
  16824. CheckSource('TestExternalClass_NonExternalOverride',
  16825. LinesToStr([ // statements
  16826. 'rtl.createClassExt(this, "TExtC", ExtObjB, "", function () {',
  16827. ' this.$init = function () {',
  16828. ' };',
  16829. ' this.$final = function () {',
  16830. ' };',
  16831. ' this.ProcA = function () {',
  16832. ' this.ProcA();',
  16833. ' this.ProcA();',
  16834. ' this.ProcB();',
  16835. ' this.ProcB();',
  16836. ' };',
  16837. '});',
  16838. 'this.A = null;',
  16839. 'this.B = null;',
  16840. 'this.C = null;',
  16841. '']),
  16842. LinesToStr([ // $mod.$main
  16843. '$mod.A.ProcA();',
  16844. '$mod.B.ProcA();',
  16845. '$mod.C.ProcA();',
  16846. '']));
  16847. end;
  16848. procedure TTestModule.TestExternalClass_OverloadHint;
  16849. begin
  16850. StartProgram(false);
  16851. Add([
  16852. '{$modeswitch externalclass}',
  16853. 'type',
  16854. ' TExtA = class external name ''ExtObjA''',
  16855. ' procedure DoIt;',
  16856. ' procedure DoIt(i: longint);',
  16857. ' end;',
  16858. 'begin',
  16859. '']);
  16860. ConvertProgram;
  16861. CheckResolverUnexpectedHints(true);
  16862. CheckSource('TestExternalClass_OverloadHint',
  16863. LinesToStr([ // statements
  16864. '']),
  16865. LinesToStr([ // $mod.$main
  16866. '']));
  16867. end;
  16868. procedure TTestModule.TestExternalClass_SameNamePublishedProperty;
  16869. begin
  16870. WithTypeInfo:=true;
  16871. StartProgram(false);
  16872. Add([
  16873. '{$modeswitch externalclass}',
  16874. 'type',
  16875. ' JSwiper = class external name ''Swiper''',
  16876. ' constructor New;',
  16877. ' end;',
  16878. ' TObject = class',
  16879. ' private',
  16880. ' FSwiper: JSwiper;',
  16881. ' published',
  16882. ' property Swiper: JSwiper read FSwiper write FSwiper;',
  16883. ' end;',
  16884. 'begin',
  16885. ' JSwiper.new;',
  16886. '']);
  16887. ConvertProgram;
  16888. CheckSource('TestExternalClass_SameNamePublishedProperty',
  16889. LinesToStr([ // statements
  16890. 'this.$rtti.$ExtClass("JSwiper", {',
  16891. ' jsclass: "Swiper"',
  16892. '});',
  16893. 'rtl.createClass(this, "TObject", null, function () {',
  16894. ' this.$init = function () {',
  16895. ' this.FSwiper = null;',
  16896. ' };',
  16897. ' this.$final = function () {',
  16898. ' this.FSwiper = undefined;',
  16899. ' };',
  16900. ' var $r = this.$rtti;',
  16901. ' $r.addProperty("Swiper", 0, $mod.$rtti["JSwiper"], "FSwiper", "FSwiper");',
  16902. '});',
  16903. '']),
  16904. LinesToStr([ // $mod.$main
  16905. 'new Swiper();',
  16906. '']));
  16907. end;
  16908. procedure TTestModule.TestExternalClass_Property;
  16909. begin
  16910. StartProgram(false);
  16911. Add([
  16912. '{$modeswitch externalclass}',
  16913. 'type',
  16914. ' TExtA = class external name ''ExtA''',
  16915. ' function getYear: longint;',
  16916. ' procedure setYear(Value: longint);',
  16917. ' property Year: longint read getyear write setyear;',
  16918. ' end;',
  16919. ' TExtB = class (TExtA)',
  16920. ' procedure OtherSetYear(Value: longint);',
  16921. ' property year write othersetyear;',
  16922. ' end;',
  16923. 'procedure textb.othersetyear(value: longint);',
  16924. 'begin',
  16925. ' setYear(Value+4);',
  16926. 'end;',
  16927. 'var',
  16928. ' A: texta;',
  16929. ' B: textb;',
  16930. 'begin',
  16931. ' a.year:=a.year+1;',
  16932. ' b.year:=b.year+2;']);
  16933. ConvertProgram;
  16934. CheckSource('TestExternalClass_NonExternalOverride',
  16935. LinesToStr([ // statements
  16936. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  16937. ' this.$init = function () {',
  16938. ' };',
  16939. ' this.$final = function () {',
  16940. ' };',
  16941. ' this.OtherSetYear = function (Value) {',
  16942. ' this.setYear(Value+4);',
  16943. ' };',
  16944. '});',
  16945. 'this.A = null;',
  16946. 'this.B = null;',
  16947. '']),
  16948. LinesToStr([ // $mod.$main
  16949. '$mod.A.setYear($mod.A.getYear()+1);',
  16950. '$mod.B.OtherSetYear($mod.B.getYear()+2);',
  16951. '']));
  16952. end;
  16953. procedure TTestModule.TestExternalClass_PropertyDate;
  16954. begin
  16955. StartProgram(false);
  16956. Add([
  16957. '{$modeswitch externalclass}',
  16958. 'type',
  16959. ' TExtA = class external name ''ExtA''',
  16960. ' end;',
  16961. ' TExtB = class (TExtA)',
  16962. ' FDate: string;',
  16963. ' property Date: string read FDate write FDate;',
  16964. ' property ExtA: string read FDate write FDate;',
  16965. ' end;',
  16966. ' {$M+}',
  16967. ' TObject = class',
  16968. ' FDate: string;',
  16969. ' published',
  16970. ' property Date: string read FDate write FDate;',
  16971. ' property ExtA: string read FDate write FDate;',
  16972. ' end;',
  16973. 'var',
  16974. ' B: textb;',
  16975. ' o: TObject;',
  16976. 'begin',
  16977. ' b.date:=b.exta;',
  16978. ' o.date:=o.exta;']);
  16979. ConvertProgram;
  16980. CheckSource('TestExternalClass_PropertyDate',
  16981. LinesToStr([ // statements
  16982. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  16983. ' this.$init = function () {',
  16984. ' this.FDate = "";',
  16985. ' };',
  16986. ' this.$final = function () {',
  16987. ' };',
  16988. '});',
  16989. 'rtl.createClass(this, "TObject", null, function () {',
  16990. ' this.$init = function () {',
  16991. ' this.FDate = "";',
  16992. ' };',
  16993. ' this.$final = function () {',
  16994. ' };',
  16995. ' var $r = this.$rtti;',
  16996. ' $r.addField("FDate", rtl.string);',
  16997. ' $r.addProperty("Date", 0, rtl.string, "FDate", "FDate");',
  16998. ' $r.addProperty("ExtA", 0, rtl.string, "FDate", "FDate");',
  16999. '});',
  17000. 'this.B = null;',
  17001. 'this.o = null;',
  17002. '']),
  17003. LinesToStr([ // $mod.$main
  17004. '$mod.B.FDate = $mod.B.FDate;',
  17005. '$mod.o.FDate = $mod.o.FDate;',
  17006. '']));
  17007. end;
  17008. procedure TTestModule.TestExternalClass_ClassProperty;
  17009. begin
  17010. StartProgram(false);
  17011. Add('{$modeswitch externalclass}');
  17012. Add('type');
  17013. Add(' TExtA = class external name ''ExtA''');
  17014. Add(' class function getYear: longint;');
  17015. Add(' class procedure setYear(Value: longint);');
  17016. Add(' class property Year: longint read getyear write setyear;');
  17017. Add(' end;');
  17018. Add(' TExtB = class (TExtA)');
  17019. Add(' class function GetCentury: longint;');
  17020. Add(' class procedure SetCentury(Value: longint);');
  17021. Add(' class property Century: longint read getcentury write setcentury;');
  17022. Add(' end;');
  17023. Add('class function textb.getcentury: longint;');
  17024. Add('begin');
  17025. Add('end;');
  17026. Add('class procedure textb.setcentury(value: longint);');
  17027. Add('begin');
  17028. Add(' setyear(value+11);');
  17029. Add(' texta.year:=texta.year+12;');
  17030. Add(' year:=year+13;');
  17031. Add(' textb.century:=textb.century+14;');
  17032. Add(' century:=century+15;');
  17033. Add('end;');
  17034. Add('var');
  17035. Add(' A: texta;');
  17036. Add(' B: textb;');
  17037. Add('begin');
  17038. Add(' texta.year:=texta.year+1;');
  17039. Add(' textb.year:=textb.year+2;');
  17040. Add(' TextA.year:=TextA.year+3;');
  17041. Add(' b.year:=b.year+4;');
  17042. Add(' textb.century:=textb.century+5;');
  17043. Add(' b.century:=b.century+6;');
  17044. ConvertProgram;
  17045. CheckSource('TestExternalClass_ClassProperty',
  17046. LinesToStr([ // statements
  17047. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  17048. ' this.$init = function () {',
  17049. ' };',
  17050. ' this.$final = function () {',
  17051. ' };',
  17052. ' this.GetCentury = function () {',
  17053. ' var Result = 0;',
  17054. ' return Result;',
  17055. ' };',
  17056. ' this.SetCentury = function (Value) {',
  17057. ' this.setYear(Value + 11);',
  17058. ' ExtA.setYear(ExtA.getYear() + 12);',
  17059. ' this.setYear(this.getYear() + 13);',
  17060. ' $mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 14);',
  17061. ' this.SetCentury(this.GetCentury() + 15);',
  17062. ' };',
  17063. '});',
  17064. 'this.A = null;',
  17065. 'this.B = null;',
  17066. '']),
  17067. LinesToStr([ // $mod.$main
  17068. 'ExtA.setYear(ExtA.getYear() + 1);',
  17069. '$mod.TExtB.setYear($mod.TExtB.getYear() + 2);',
  17070. 'ExtA.setYear(ExtA.getYear() + 3);',
  17071. '$mod.B.setYear($mod.B.getYear() + 4);',
  17072. '$mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 5);',
  17073. '$mod.B.$class.SetCentury($mod.B.$class.GetCentury() + 6);',
  17074. '']));
  17075. end;
  17076. procedure TTestModule.TestExternalClass_ClassOf;
  17077. begin
  17078. StartProgram(false);
  17079. Add('{$modeswitch externalclass}');
  17080. Add('type');
  17081. Add(' TExtA = class external name ''ExtA''');
  17082. Add(' procedure ProcA; virtual;');
  17083. Add(' procedure ProcB; virtual;');
  17084. Add(' end;');
  17085. Add(' TExtAClass = class of TExtA;');
  17086. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  17087. Add(' end;');
  17088. Add(' TExtBClass = class of TExtB;');
  17089. Add(' TExtC = class (TExtB)');
  17090. Add(' procedure ProcA; override;');
  17091. Add(' end;');
  17092. Add(' TExtCClass = class of TExtC;');
  17093. Add('procedure TExtC.ProcA; begin end;');
  17094. Add('var');
  17095. Add(' A: texta; ClA: TExtAClass;');
  17096. Add(' B: textb; ClB: TExtBClass;');
  17097. Add(' C: textc; ClC: TExtCClass;');
  17098. Add('begin');
  17099. Add(' ClA:=texta;');
  17100. Add(' ClA:=textb;');
  17101. Add(' ClA:=textc;');
  17102. Add(' ClB:=textb;');
  17103. Add(' ClB:=textc;');
  17104. Add(' ClC:=textc;');
  17105. ConvertProgram;
  17106. CheckSource('TestExternalClass_ClassOf',
  17107. LinesToStr([ // statements
  17108. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  17109. ' this.$init = function () {',
  17110. ' };',
  17111. ' this.$final = function () {',
  17112. ' };',
  17113. ' this.ProcA = function () {',
  17114. ' };',
  17115. '});',
  17116. 'this.A = null;',
  17117. 'this.ClA = null;',
  17118. 'this.B = null;',
  17119. 'this.ClB = null;',
  17120. 'this.C = null;',
  17121. 'this.ClC = null;',
  17122. '']),
  17123. LinesToStr([ // $mod.$main
  17124. '$mod.ClA = ExtA;',
  17125. '$mod.ClA = ExtB;',
  17126. '$mod.ClA = $mod.TExtC;',
  17127. '$mod.ClB = ExtB;',
  17128. '$mod.ClB = $mod.TExtC;',
  17129. '$mod.ClC = $mod.TExtC;',
  17130. '']));
  17131. end;
  17132. procedure TTestModule.TestExternalClass_ClassOtherUnit;
  17133. begin
  17134. AddModuleWithIntfImplSrc('unit2.pas',
  17135. LinesToStr([
  17136. '{$modeswitch externalclass}',
  17137. 'type',
  17138. ' TExtA = class external name ''ExtA''',
  17139. ' class var Id: longint;',
  17140. ' end;',
  17141. '']),
  17142. '');
  17143. StartUnit(true);
  17144. Add('interface');
  17145. Add('uses unit2;');
  17146. Add('implementation');
  17147. Add('begin');
  17148. Add(' unit2.texta.id:=unit2.texta.id+1;');
  17149. ConvertUnit;
  17150. CheckSource('TestExternalClass_ClassOtherUnit',
  17151. LinesToStr([
  17152. '']),
  17153. LinesToStr([
  17154. 'ExtA.Id = ExtA.Id + 1;',
  17155. '']));
  17156. end;
  17157. procedure TTestModule.TestExternalClass_Is;
  17158. begin
  17159. StartProgram(false);
  17160. Add([
  17161. '{$modeswitch externalclass}',
  17162. 'type',
  17163. ' TExtA = class external name ''ExtA''',
  17164. ' end;',
  17165. ' TExtAClass = class of TExtA;',
  17166. ' TExtB = class external name ''ExtB'' (TExtA)',
  17167. ' end;',
  17168. ' TExtBClass = class of TExtB;',
  17169. ' TExtC = class (TExtB)',
  17170. ' end;',
  17171. ' TExtCClass = class of TExtC;',
  17172. 'var',
  17173. ' A: texta; ClA: TExtAClass;',
  17174. ' B: textb; ClB: TExtBClass;',
  17175. ' C: textc; ClC: TExtCClass;',
  17176. 'begin',
  17177. ' if a is textb then ;',
  17178. ' if a is textc then ;',
  17179. ' if b is textc then ;',
  17180. ' if cla is textb then ;',
  17181. ' if cla is textc then ;',
  17182. ' if clb is textc then ;',
  17183. ' try',
  17184. ' except',
  17185. ' on TExtA do ;',
  17186. ' on e: TExtB do ;',
  17187. ' end;',
  17188. '']);
  17189. ConvertProgram;
  17190. CheckSource('TestExternalClass_Is',
  17191. LinesToStr([ // statements
  17192. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  17193. ' this.$init = function () {',
  17194. ' };',
  17195. ' this.$final = function () {',
  17196. ' };',
  17197. '});',
  17198. 'this.A = null;',
  17199. 'this.ClA = null;',
  17200. 'this.B = null;',
  17201. 'this.ClB = null;',
  17202. 'this.C = null;',
  17203. 'this.ClC = null;',
  17204. '']),
  17205. LinesToStr([ // $mod.$main
  17206. 'if (rtl.isExt($mod.A, ExtB)) ;',
  17207. 'if ($mod.TExtC.isPrototypeOf($mod.A)) ;',
  17208. 'if ($mod.TExtC.isPrototypeOf($mod.B)) ;',
  17209. 'if (rtl.isExt($mod.ClA, ExtB)) ;',
  17210. 'if (rtl.is($mod.ClA, $mod.TExtC)) ;',
  17211. 'if (rtl.is($mod.ClB, $mod.TExtC)) ;',
  17212. 'try {} catch ($e) {',
  17213. ' if (rtl.isExt($e,ExtA)) {}',
  17214. ' else if (rtl.isExt($e,ExtB)) {',
  17215. ' var e = $e;',
  17216. ' } else throw $e',
  17217. '};',
  17218. '']));
  17219. end;
  17220. procedure TTestModule.TestExternalClass_As;
  17221. begin
  17222. StartProgram(false);
  17223. Add('{$modeswitch externalclass}');
  17224. Add('type');
  17225. Add(' TExtA = class external name ''ExtA''');
  17226. Add(' end;');
  17227. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  17228. Add(' end;');
  17229. Add(' TExtC = class (TExtB)');
  17230. Add(' end;');
  17231. Add('var');
  17232. Add(' A: texta;');
  17233. Add(' B: textb;');
  17234. Add(' C: textc;');
  17235. Add('begin');
  17236. Add(' b:=a as textb;');
  17237. Add(' c:=a as textc;');
  17238. Add(' c:=b as textc;');
  17239. ConvertProgram;
  17240. CheckSource('TestExternalClass_Is',
  17241. LinesToStr([ // statements
  17242. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  17243. ' this.$init = function () {',
  17244. ' };',
  17245. ' this.$final = function () {',
  17246. ' };',
  17247. '});',
  17248. 'this.A = null;',
  17249. 'this.B = null;',
  17250. 'this.C = null;',
  17251. '']),
  17252. LinesToStr([ // $mod.$main
  17253. '$mod.B = rtl.asExt($mod.A, ExtB);',
  17254. '$mod.C = rtl.as($mod.A, $mod.TExtC);',
  17255. '$mod.C = rtl.as($mod.B, $mod.TExtC);',
  17256. '']));
  17257. end;
  17258. procedure TTestModule.TestExternalClass_DestructorFail;
  17259. begin
  17260. StartProgram(false);
  17261. Add('{$modeswitch externalclass}');
  17262. Add('type');
  17263. Add(' TExtA = class external name ''ExtA''');
  17264. Add(' destructor Free;');
  17265. Add(' end;');
  17266. SetExpectedPasResolverError('Pascal element not supported: destructor',
  17267. nPasElementNotSupported);
  17268. ConvertProgram;
  17269. end;
  17270. procedure TTestModule.TestExternalClass_New;
  17271. begin
  17272. StartProgram(false);
  17273. Add([
  17274. '{$modeswitch externalclass}',
  17275. 'type',
  17276. ' TExtA = class external name ''ExtA''',
  17277. ' constructor New;',
  17278. ' constructor New(i: longint; j: longint = 2);',
  17279. ' end;',
  17280. 'var',
  17281. ' A: texta;',
  17282. 'begin',
  17283. ' a:=texta.new;',
  17284. ' a:=texta(texta.new);',
  17285. ' a:=texta.new();',
  17286. ' a:=texta.new(1);',
  17287. ' with texta do begin',
  17288. ' a:=new;',
  17289. ' a:=new();',
  17290. ' a:=new(2);',
  17291. ' end;',
  17292. ' a:=test1.texta.new;',
  17293. ' a:=test1.texta.new();',
  17294. ' a:=test1.texta.new(3);',
  17295. '']);
  17296. ConvertProgram;
  17297. CheckSource('TestExternalClass_New',
  17298. LinesToStr([ // statements
  17299. 'this.A = null;',
  17300. '']),
  17301. LinesToStr([ // $mod.$main
  17302. '$mod.A = new ExtA();',
  17303. '$mod.A = new ExtA();',
  17304. '$mod.A = new ExtA();',
  17305. '$mod.A = new ExtA(1,2);',
  17306. '$mod.A = new ExtA();',
  17307. '$mod.A = new ExtA();',
  17308. '$mod.A = new ExtA(2,2);',
  17309. '$mod.A = new ExtA();',
  17310. '$mod.A = new ExtA();',
  17311. '$mod.A = new ExtA(3,2);',
  17312. '']));
  17313. end;
  17314. procedure TTestModule.TestExternalClass_ClassOf_New;
  17315. begin
  17316. StartProgram(false);
  17317. Add('{$modeswitch externalclass}');
  17318. Add('type');
  17319. Add(' TExtAClass = class of TExtA;');
  17320. Add(' TExtA = class external name ''ExtA''');
  17321. Add(' C: TExtAClass;');
  17322. Add(' constructor New;');
  17323. Add(' end;');
  17324. Add('var');
  17325. Add(' A: texta;');
  17326. Add(' C: textaclass;');
  17327. Add('begin');
  17328. Add(' a:=c.new;');
  17329. Add(' a:=c.new();');
  17330. Add(' with C do begin');
  17331. Add(' a:=new;');
  17332. Add(' a:=new();');
  17333. Add(' end;');
  17334. Add(' a:=test1.c.new;');
  17335. Add(' a:=test1.c.new();');
  17336. Add(' a:=A.c.new();');
  17337. ConvertProgram;
  17338. CheckSource('TestExternalClass_ClassOf_New',
  17339. LinesToStr([ // statements
  17340. 'this.A = null;',
  17341. 'this.C = null;',
  17342. '']),
  17343. LinesToStr([ // $mod.$main
  17344. '$mod.A = new $mod.C();',
  17345. '$mod.A = new $mod.C();',
  17346. 'var $with = $mod.C;',
  17347. '$mod.A = new $with();',
  17348. '$mod.A = new $with();',
  17349. '$mod.A = new $mod.C();',
  17350. '$mod.A = new $mod.C();',
  17351. '$mod.A = new $mod.A.C();',
  17352. '']));
  17353. end;
  17354. procedure TTestModule.TestExternalClass_FuncClassOf_New;
  17355. begin
  17356. StartProgram(false);
  17357. Add([
  17358. '{$modeswitch externalclass}',
  17359. 'type',
  17360. ' TExtAClass = class of TExtA;',
  17361. ' TExtA = class external name ''ExtA''',
  17362. ' constructor New;',
  17363. ' end;',
  17364. 'function GetCreator: TExtAClass;',
  17365. 'begin',
  17366. ' Result:=TExtA;',
  17367. 'end;',
  17368. 'var',
  17369. ' A: texta;',
  17370. 'begin',
  17371. ' a:=getcreator.new;',
  17372. ' a:=getcreator().new;',
  17373. ' a:=getcreator().new();',
  17374. ' a:=getcreator.new();',
  17375. ' with getcreator do begin',
  17376. ' a:=new;',
  17377. ' a:=new();',
  17378. ' end;']);
  17379. ConvertProgram;
  17380. CheckSource('TestExternalClass_FuncClassOf_New',
  17381. LinesToStr([ // statements
  17382. 'this.GetCreator = function () {',
  17383. ' var Result = null;',
  17384. ' Result = ExtA;',
  17385. ' return Result;',
  17386. '};',
  17387. 'this.A = null;',
  17388. '']),
  17389. LinesToStr([ // $mod.$main
  17390. '$mod.A = new ($mod.GetCreator())();',
  17391. '$mod.A = new ($mod.GetCreator())();',
  17392. '$mod.A = new ($mod.GetCreator())();',
  17393. '$mod.A = new ($mod.GetCreator())();',
  17394. 'var $with = $mod.GetCreator();',
  17395. '$mod.A = new $with();',
  17396. '$mod.A = new $with();',
  17397. '']));
  17398. end;
  17399. procedure TTestModule.TestExternalClass_New_PasClassFail;
  17400. begin
  17401. StartProgram(false);
  17402. Add([
  17403. '{$modeswitch externalclass}',
  17404. 'type',
  17405. ' TExtA = class external name ''ExtA''',
  17406. ' constructor New;',
  17407. ' end;',
  17408. ' TBird = class(TExtA)',
  17409. ' end;',
  17410. 'begin',
  17411. ' TBird.new;',
  17412. '']);
  17413. SetExpectedPasResolverError(sJSNewNotSupported,nJSNewNotSupported);
  17414. ConvertProgram;
  17415. end;
  17416. procedure TTestModule.TestExternalClass_New_PasClassBracketsFail;
  17417. begin
  17418. StartProgram(false);
  17419. Add([
  17420. '{$modeswitch externalclass}',
  17421. 'type',
  17422. ' TExtA = class external name ''ExtA''',
  17423. ' constructor New;',
  17424. ' end;',
  17425. ' TBird = class(TExtA)',
  17426. ' end;',
  17427. 'begin',
  17428. ' TBird.new();',
  17429. '']);
  17430. SetExpectedPasResolverError(sJSNewNotSupported,nJSNewNotSupported);
  17431. ConvertProgram;
  17432. end;
  17433. procedure TTestModule.TestExternalClass_NewExtName;
  17434. begin
  17435. StartProgram(false);
  17436. Add([
  17437. '{$modeswitch externalclass}',
  17438. 'type',
  17439. ' TExtA = class external name ''ExtA''',
  17440. ' constructor New; external name ''Other'';',
  17441. ' constructor New(i: longint; j: longint = 2); external name ''A.B'';',
  17442. ' end;',
  17443. 'var',
  17444. ' A: texta;',
  17445. 'begin',
  17446. ' a:=texta.new;',
  17447. ' a:=texta(texta.new);',
  17448. ' a:=texta.new();',
  17449. ' a:=texta.new(1);',
  17450. ' with texta do begin',
  17451. ' a:=new;',
  17452. ' a:=new();',
  17453. ' a:=new(2);',
  17454. ' end;',
  17455. ' a:=test1.texta.new;',
  17456. ' a:=test1.texta.new();',
  17457. ' a:=test1.texta.new(3);',
  17458. '']);
  17459. ConvertProgram;
  17460. CheckSource('TestExternalClass_NewExtName',
  17461. LinesToStr([ // statements
  17462. 'this.A = null;',
  17463. '']),
  17464. LinesToStr([ // $mod.$main
  17465. '$mod.A = new Other();',
  17466. '$mod.A = new Other();',
  17467. '$mod.A = new Other();',
  17468. '$mod.A = new A.B(1,2);',
  17469. '$mod.A = new Other();',
  17470. '$mod.A = new Other();',
  17471. '$mod.A = new A.B(2,2);',
  17472. '$mod.A = new Other();',
  17473. '$mod.A = new Other();',
  17474. '$mod.A = new A.B(3,2);',
  17475. '']));
  17476. end;
  17477. procedure TTestModule.TestExternalClass_Constructor;
  17478. begin
  17479. StartProgram(false);
  17480. Add([
  17481. '{$modeswitch externalclass}',
  17482. 'type',
  17483. ' TExtA = class external name ''ExtA''',
  17484. ' public type',
  17485. ' TExtB = class external name ''ExtB''',
  17486. ' public type',
  17487. ' TExtC = class external name ''ExtC''',
  17488. ' constructor New;',
  17489. ' constructor New(i: word);',
  17490. ' end;',
  17491. ' end;',
  17492. ' constructor Create;',
  17493. ' constructor Create(i: longint; j: longint = 2);',
  17494. ' end;',
  17495. 'var',
  17496. ' A: texta;',
  17497. ' C: texta.textb.textc;',
  17498. 'begin',
  17499. ' a:=texta.create;',
  17500. ' a:=texta(texta.create);',
  17501. ' a:=texta.create();',
  17502. ' a:=texta.create(1);',
  17503. ' with texta do begin',
  17504. ' a:=create;',
  17505. ' a:=create();',
  17506. ' a:=create(2);',
  17507. ' end;',
  17508. ' a:=test1.texta.create;',
  17509. ' a:=test1.texta.create();',
  17510. ' a:=test1.texta.create(3);',
  17511. ' c:=texta.textb.textc.new;',
  17512. ' c:=texta.textb.textc.new();',
  17513. ' c:=texta.textb.textc.new(4);',
  17514. '']);
  17515. ConvertProgram;
  17516. CheckSource('TestExternalClass_Constructor',
  17517. LinesToStr([ // statements
  17518. 'this.A = null;',
  17519. 'this.C = null;',
  17520. '']),
  17521. LinesToStr([ // $mod.$main
  17522. '$mod.A = new ExtA.Create();',
  17523. '$mod.A = new ExtA.Create();',
  17524. '$mod.A = new ExtA.Create();',
  17525. '$mod.A = new ExtA.Create(1,2);',
  17526. '$mod.A = new ExtA.Create();',
  17527. '$mod.A = new ExtA.Create();',
  17528. '$mod.A = new ExtA.Create(2,2);',
  17529. '$mod.A = new ExtA.Create();',
  17530. '$mod.A = new ExtA.Create();',
  17531. '$mod.A = new ExtA.Create(3,2);',
  17532. '$mod.C = new ExtA.ExtB.ExtC();',
  17533. '$mod.C = new ExtA.ExtB.ExtC();',
  17534. '$mod.C = new ExtA.ExtB.ExtC(4);',
  17535. '']));
  17536. end;
  17537. procedure TTestModule.TestExternalClass_ConstructorBrackets;
  17538. begin
  17539. StartProgram(false);
  17540. Add([
  17541. '{$modeswitch externalclass}',
  17542. 'type',
  17543. ' TExtA = class external name ''ExtA''',
  17544. ' constructor Create; external name ''{}'';',
  17545. ' end;',
  17546. 'var',
  17547. ' A: texta;',
  17548. 'begin',
  17549. ' a:=texta.create;',
  17550. ' a:=texta(texta.create);',
  17551. ' a:=texta.create();',
  17552. ' with texta do begin',
  17553. ' a:=create;',
  17554. ' a:=create();',
  17555. ' end;',
  17556. ' a:=test1.texta.create;',
  17557. ' a:=test1.texta.create();',
  17558. '']);
  17559. ConvertProgram;
  17560. CheckSource('TestExternalClass_ConstructorBrackets',
  17561. LinesToStr([ // statements
  17562. 'this.A = null;',
  17563. '']),
  17564. LinesToStr([ // $mod.$main
  17565. '$mod.A = {};',
  17566. '$mod.A = {};',
  17567. '$mod.A = {};',
  17568. '$mod.A = {};',
  17569. '$mod.A = {};',
  17570. '$mod.A = {};',
  17571. '$mod.A = {};',
  17572. '']));
  17573. end;
  17574. procedure TTestModule.TestExternalClass_LocalConstSameName;
  17575. begin
  17576. StartProgram(false);
  17577. Add('{$modeswitch externalclass}');
  17578. Add('type');
  17579. Add(' TExtA = class external name ''ExtA''');
  17580. Add(' constructor New;');
  17581. Add(' end;');
  17582. Add('function DoIt: longint;');
  17583. Add('const ExtA: longint = 3;');
  17584. Add('begin');
  17585. Add(' Result:=ExtA;');
  17586. Add('end;');
  17587. Add('var');
  17588. Add(' A: texta;');
  17589. Add('begin');
  17590. Add(' a:=texta.new;');
  17591. ConvertProgram;
  17592. CheckSource('TestExternalClass_LocalConstSameName',
  17593. LinesToStr([ // statements
  17594. 'var ExtA$1 = 3;',
  17595. 'this.DoIt = function () {',
  17596. ' var Result = 0;',
  17597. ' Result = ExtA$1;',
  17598. ' return Result;',
  17599. '};',
  17600. 'this.A = null;',
  17601. '']),
  17602. LinesToStr([ // $mod.$main
  17603. '$mod.A = new ExtA();',
  17604. '']));
  17605. end;
  17606. procedure TTestModule.TestExternalClass_ReintroduceOverload;
  17607. begin
  17608. StartProgram(false);
  17609. Add('{$modeswitch externalclass}');
  17610. Add('type');
  17611. Add(' TExtA = class external name ''ExtA''');
  17612. Add(' procedure DoIt;');
  17613. Add(' end;');
  17614. Add(' TMyA = class(TExtA)');
  17615. Add(' procedure DoIt;');
  17616. Add(' end;');
  17617. Add('procedure TMyA.DoIt; begin end;');
  17618. Add('begin');
  17619. ConvertProgram;
  17620. CheckSource('TestExternalClass_ReintroduceOverload',
  17621. LinesToStr([ // statements
  17622. 'rtl.createClassExt(this, "TMyA", ExtA, "", function () {',
  17623. ' this.$init = function () {',
  17624. ' };',
  17625. ' this.$final = function () {',
  17626. ' };',
  17627. ' this.DoIt$1 = function () {',
  17628. ' };',
  17629. '});',
  17630. '']),
  17631. LinesToStr([ // $mod.$main
  17632. '']));
  17633. end;
  17634. procedure TTestModule.TestExternalClass_Inherited;
  17635. begin
  17636. StartProgram(false);
  17637. Add('{$modeswitch externalclass}');
  17638. Add('type');
  17639. Add(' TExtA = class external name ''ExtA''');
  17640. Add(' procedure DoIt(i: longint = 1); virtual;');
  17641. Add(' procedure DoSome(j: longint = 2);');
  17642. Add(' end;');
  17643. Add(' TExtB = class external name ''ExtB''(TExtA)');
  17644. Add(' end;');
  17645. Add(' TMyC = class(TExtB)');
  17646. Add(' procedure DoIt(i: longint = 1); override;');
  17647. Add(' procedure DoSome(j: longint = 2); reintroduce;');
  17648. Add(' end;');
  17649. Add('procedure TMyC.DoIt(i: longint);');
  17650. Add('begin');
  17651. Add(' inherited;');
  17652. Add(' inherited DoIt;');
  17653. Add(' inherited DoIt();');
  17654. Add(' inherited DoIt(3);');
  17655. Add(' inherited DoSome;');
  17656. Add(' inherited DoSome();');
  17657. Add(' inherited DoSome(4);');
  17658. Add('end;');
  17659. Add('procedure TMyC.DoSome(j: longint);');
  17660. Add('begin');
  17661. Add(' inherited;');
  17662. Add('end;');
  17663. Add('begin');
  17664. ConvertProgram;
  17665. CheckSource('TestExternalClass_ReintroduceOverload',
  17666. LinesToStr([ // statements
  17667. 'rtl.createClassExt(this, "TMyC", ExtB, "", function () {',
  17668. ' this.$init = function () {',
  17669. ' };',
  17670. ' this.$final = function () {',
  17671. ' };',
  17672. ' this.DoIt = function (i) {',
  17673. ' ExtB.DoIt.apply(this, arguments);',
  17674. ' ExtB.DoIt.call(this, 1);',
  17675. ' ExtB.DoIt.call(this, 1);',
  17676. ' ExtB.DoIt.call(this, 3);',
  17677. ' ExtB.DoSome.call(this, 2);',
  17678. ' ExtB.DoSome.call(this, 2);',
  17679. ' ExtB.DoSome.call(this, 4);',
  17680. ' };',
  17681. ' this.DoSome$1 = function (j) {',
  17682. ' ExtB.DoSome.apply(this, arguments);',
  17683. ' };',
  17684. '});',
  17685. '']),
  17686. LinesToStr([ // $mod.$main
  17687. '']));
  17688. end;
  17689. procedure TTestModule.TestExternalClass_PascalAncestorFail;
  17690. begin
  17691. StartProgram(false);
  17692. Add('{$modeswitch externalclass}');
  17693. Add('type');
  17694. Add(' TObject = class');
  17695. Add(' end;');
  17696. Add(' TExtA = class external name ''ExtA''(TObject)');
  17697. Add(' end;');
  17698. Add('begin');
  17699. SetExpectedPasResolverError('Ancestor "TObject" is not external',nAncestorIsNotExternal);
  17700. ConvertProgram;
  17701. end;
  17702. procedure TTestModule.TestExternalClass_NewInstance;
  17703. begin
  17704. StartProgram(false);
  17705. Add('{$modeswitch externalclass}');
  17706. Add('type');
  17707. Add(' TExtA = class external name ''ExtA''');
  17708. Add(' end;');
  17709. Add(' TMyB = class(TExtA)');
  17710. Add(' protected');
  17711. Add(' class function NewInstance(fnname: string; const paramarray): TMyB; virtual;');
  17712. Add(' end;');
  17713. Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
  17714. Add('begin end;');
  17715. Add('begin');
  17716. ConvertProgram;
  17717. CheckSource('TestExternalClass_NewInstance',
  17718. LinesToStr([ // statements
  17719. 'rtl.createClassExt(this, "TMyB", ExtA, "NewInstance", function () {',
  17720. ' this.$init = function () {',
  17721. ' };',
  17722. ' this.$final = function () {',
  17723. ' };',
  17724. ' this.NewInstance = function (fnname, paramarray) {',
  17725. ' var Result = null;',
  17726. ' return Result;',
  17727. ' };',
  17728. '});',
  17729. '']),
  17730. LinesToStr([ // $mod.$main
  17731. '']));
  17732. end;
  17733. procedure TTestModule.TestExternalClass_NewInstance_NonVirtualFail;
  17734. begin
  17735. StartProgram(false);
  17736. Add('{$modeswitch externalclass}');
  17737. Add('type');
  17738. Add(' TExtA = class external name ''ExtA''');
  17739. Add(' end;');
  17740. Add(' TMyB = class(TExtA)');
  17741. Add(' protected');
  17742. Add(' class function NewInstance(fnname: string; const paramarray): TMyB;');
  17743. Add(' end;');
  17744. Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
  17745. Add('begin end;');
  17746. Add('begin');
  17747. SetExpectedPasResolverError(sNewInstanceFunctionMustBeVirtual,nNewInstanceFunctionMustBeVirtual);
  17748. ConvertProgram;
  17749. end;
  17750. procedure TTestModule.TestExternalClass_NewInstance_FirstParamNotString_Fail;
  17751. begin
  17752. StartProgram(false);
  17753. Add('{$modeswitch externalclass}');
  17754. Add('type');
  17755. Add(' TExtA = class external name ''ExtA''');
  17756. Add(' end;');
  17757. Add(' TMyB = class(TExtA)');
  17758. Add(' protected');
  17759. Add(' class function NewInstance(fnname: longint; const paramarray): TMyB; virtual;');
  17760. Add(' end;');
  17761. Add('class function TMyB.NewInstance(fnname: longint; const paramarray): TMyB;');
  17762. Add('begin end;');
  17763. Add('begin');
  17764. SetExpectedPasResolverError('Incompatible type arg no. 1: Got "Longint", expected "String"',
  17765. nIncompatibleTypeArgNo);
  17766. ConvertProgram;
  17767. end;
  17768. procedure TTestModule.TestExternalClass_NewInstance_SecondParamTyped_Fail;
  17769. begin
  17770. StartProgram(false);
  17771. Add('{$modeswitch externalclass}');
  17772. Add('type');
  17773. Add(' TExtA = class external name ''ExtA''');
  17774. Add(' end;');
  17775. Add(' TMyB = class(TExtA)');
  17776. Add(' protected');
  17777. Add(' class function NewInstance(fnname: string; const paramarray: string): TMyB; virtual;');
  17778. Add(' end;');
  17779. Add('class function TMyB.NewInstance(fnname: string; const paramarray: string): TMyB;');
  17780. Add('begin end;');
  17781. Add('begin');
  17782. SetExpectedPasResolverError('Incompatible type arg no. 2: Got "type", expected "untyped"',
  17783. nIncompatibleTypeArgNo);
  17784. ConvertProgram;
  17785. end;
  17786. procedure TTestModule.TestExternalClass_JSFunctionPasDescendant;
  17787. begin
  17788. StartProgram(false);
  17789. Add([
  17790. '{$modeswitch externalclass}',
  17791. 'type',
  17792. ' TJSFunction = class external name ''Function''',
  17793. ' end;',
  17794. ' TExtA = class external name ''ExtA''(TJSFunction)',
  17795. ' constructor New(w: word);',
  17796. ' end;',
  17797. ' TBird = class (TExtA)',
  17798. ' public',
  17799. ' Size: word;',
  17800. ' class var Legs: word;',
  17801. ' constructor Create(a: word);',
  17802. ' end;',
  17803. ' TEagle = class (TBird)',
  17804. ' public',
  17805. ' constructor Create(b: word); reintroduce;',
  17806. ' end;',
  17807. 'constructor TBird.Create(a: word);',
  17808. 'begin',
  17809. ' inherited;', // silently ignored
  17810. ' inherited New(a);', // this.$func(a)
  17811. 'end;',
  17812. 'constructor TEagle.Create(b: word);',
  17813. 'begin',
  17814. ' inherited Create(b);',
  17815. 'end;',
  17816. 'var',
  17817. ' Bird: TBird;',
  17818. ' Eagle: TEagle;',
  17819. 'begin',
  17820. ' Bird:=TBird.Create(3);',
  17821. ' Eagle:=TEagle.Create(4);',
  17822. ' Bird.Size:=Bird.Size+5;',
  17823. ' Bird.Legs:=Bird.Legs+6;',
  17824. ' Eagle.Size:=Eagle.Size+5;',
  17825. ' Eagle.Legs:=Eagle.Legs+6;',
  17826. '']);
  17827. ConvertProgram;
  17828. CheckSource('TestExternalClass_JSFunctionPasDescendant',
  17829. LinesToStr([ // statements
  17830. 'rtl.createClassExt(this, "TBird", ExtA, "", function () {',
  17831. ' this.Legs = 0;',
  17832. ' this.$init = function () {',
  17833. ' this.Size = 0;',
  17834. ' };',
  17835. ' this.$final = function () {',
  17836. ' };',
  17837. ' this.Create = function (a) {',
  17838. ' this.$ancestorfunc(a);',
  17839. ' return this;',
  17840. ' };',
  17841. '});',
  17842. 'rtl.createClassExt(this, "TEagle", this.TBird, "", function () {',
  17843. ' this.Create$1 = function (b) {',
  17844. ' $mod.TBird.Create.call(this, b);',
  17845. ' return this;',
  17846. ' };',
  17847. '});',
  17848. 'this.Bird = null;',
  17849. 'this.Eagle = null;',
  17850. '']),
  17851. LinesToStr([ // $mod.$main
  17852. '$mod.Bird = $mod.TBird.$create("Create", [3]);',
  17853. '$mod.Eagle = $mod.TEagle.$create("Create$1", [4]);',
  17854. '$mod.Bird.Size = $mod.Bird.Size + 5;',
  17855. '$mod.TBird.Legs = $mod.Bird.Legs + 6;',
  17856. '$mod.Eagle.Size = $mod.Eagle.Size + 5;',
  17857. '$mod.TBird.Legs = $mod.Eagle.Legs + 6;',
  17858. '']));
  17859. end;
  17860. procedure TTestModule.TestExternalClass_PascalProperty;
  17861. begin
  17862. StartProgram(false);
  17863. Add('{$modeswitch externalclass}');
  17864. Add('type');
  17865. Add(' TJSElement = class;');
  17866. Add(' TJSNotifyEvent = procedure(Sender: TJSElement) of object;');
  17867. Add(' TJSElement = class external name ''ExtA''');
  17868. Add(' end;');
  17869. Add(' TControl = class(TJSElement)');
  17870. Add(' private');
  17871. Add(' FOnClick: TJSNotifyEvent;');
  17872. Add(' property OnClick: TJSNotifyEvent read FOnClick write FOnClick;');
  17873. Add(' procedure Click(Sender: TJSElement);');
  17874. Add(' end;');
  17875. Add('procedure TControl.Click(Sender: TJSElement);');
  17876. Add('begin');
  17877. Add(' OnClick(Self);');
  17878. Add('end;');
  17879. Add('var');
  17880. Add(' Ctrl: TControl;');
  17881. Add('begin');
  17882. Add(' Ctrl.OnClick:[email protected];');
  17883. Add(' Ctrl.OnClick(Ctrl);');
  17884. ConvertProgram;
  17885. CheckSource('TestExternalClass_PascalProperty',
  17886. LinesToStr([ // statements
  17887. 'rtl.createClassExt(this, "TControl", ExtA, "", function () {',
  17888. ' this.$init = function () {',
  17889. ' this.FOnClick = null;',
  17890. ' };',
  17891. ' this.$final = function () {',
  17892. ' this.FOnClick = undefined;',
  17893. ' };',
  17894. ' this.Click = function (Sender) {',
  17895. ' this.FOnClick(this);',
  17896. ' };',
  17897. '});',
  17898. 'this.Ctrl = null;',
  17899. '']),
  17900. LinesToStr([ // $mod.$main
  17901. '$mod.Ctrl.FOnClick = rtl.createCallback($mod.Ctrl, "Click");',
  17902. '$mod.Ctrl.FOnClick($mod.Ctrl);',
  17903. '']));
  17904. end;
  17905. procedure TTestModule.TestExternalClass_TypeCastToRootClass;
  17906. begin
  17907. StartProgram(false);
  17908. Add([
  17909. '{$modeswitch externalclass}',
  17910. 'type',
  17911. ' IUnknown = interface end;',
  17912. ' TObject = class',
  17913. ' end;',
  17914. ' TChild = class',
  17915. ' end;',
  17916. ' TExtRootA = class external name ''ExtRootA''',
  17917. ' end;',
  17918. ' TExtChildA = class external name ''ExtChildA''(TExtRootA)',
  17919. ' end;',
  17920. ' TExtRootB = class external name ''ExtRootB''',
  17921. ' end;',
  17922. ' TExtChildB = class external name ''ExtChildB''(TExtRootB)',
  17923. ' end;',
  17924. 'var',
  17925. ' Obj: TObject;',
  17926. ' Child: TChild;',
  17927. ' RootA: TExtRootA;',
  17928. ' ChildA: TExtChildA;',
  17929. ' RootB: TExtRootB;',
  17930. ' ChildB: TExtChildB;',
  17931. ' i: IUnknown;',
  17932. 'begin',
  17933. ' obj:=tobject(roota);',
  17934. ' obj:=tobject(childa);',
  17935. ' child:=tchild(tobject(roota));',
  17936. ' roota:=textroota(obj);',
  17937. ' roota:=textroota(child);',
  17938. ' roota:=textroota(rootb);',
  17939. ' roota:=textroota(childb);',
  17940. ' childa:=textchilda(textroota(obj));',
  17941. ' roota:=TExtRootA(i)',
  17942. '']);
  17943. ConvertProgram;
  17944. CheckSource('TestExternalClass_TypeCastToRootClass',
  17945. LinesToStr([ // statements
  17946. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  17947. 'rtl.createClass(this, "TObject", null, function () {',
  17948. ' this.$init = function () {',
  17949. ' };',
  17950. ' this.$final = function () {',
  17951. ' };',
  17952. '});',
  17953. 'rtl.createClass(this, "TChild", this.TObject, function () {',
  17954. '});',
  17955. 'this.Obj = null;',
  17956. 'this.Child = null;',
  17957. 'this.RootA = null;',
  17958. 'this.ChildA = null;',
  17959. 'this.RootB = null;',
  17960. 'this.ChildB = null;',
  17961. 'this.i = null;',
  17962. '']),
  17963. LinesToStr([ // $mod.$main
  17964. '$mod.Obj = $mod.RootA;',
  17965. '$mod.Obj = $mod.ChildA;',
  17966. '$mod.Child = $mod.RootA;',
  17967. '$mod.RootA = $mod.Obj;',
  17968. '$mod.RootA = $mod.Child;',
  17969. '$mod.RootA = $mod.RootB;',
  17970. '$mod.RootA = $mod.ChildB;',
  17971. '$mod.ChildA = $mod.Obj;',
  17972. '$mod.RootA = $mod.i;',
  17973. '']));
  17974. end;
  17975. procedure TTestModule.TestExternalClass_TypeCastToJSObject;
  17976. begin
  17977. StartProgram(false);
  17978. Add([
  17979. '{$modeswitch externalclass}',
  17980. 'type',
  17981. ' IUnknown = interface end;',
  17982. ' IBird = interface(IUnknown) end;',
  17983. ' TClass = class of TObject;',
  17984. ' TObject = class',
  17985. ' end;',
  17986. ' TChild = class',
  17987. ' end;',
  17988. ' TJSObject = class external name ''Object''',
  17989. ' end;',
  17990. ' TRec = record end;',
  17991. 'var',
  17992. ' Obj: TObject;',
  17993. ' Child: TChild;',
  17994. ' i: IUnknown;',
  17995. ' Bird: IBird;',
  17996. ' j: TJSObject;',
  17997. ' r: TRec;',
  17998. ' c: TClass;',
  17999. 'begin',
  18000. ' j:=tjsobject(IUnknown);',
  18001. ' j:=tjsobject(IBird);',
  18002. ' j:=tjsobject(TObject);',
  18003. ' j:=tjsobject(TChild);',
  18004. ' j:=tjsobject(TRec);',
  18005. ' j:=tjsobject(Obj);',
  18006. ' j:=tjsobject(Child);',
  18007. ' j:=tjsobject(i);',
  18008. ' j:=tjsobject(Bird);',
  18009. ' j:=tjsobject(r);',
  18010. ' j:=tjsobject(c);',
  18011. '']);
  18012. ConvertProgram;
  18013. CheckSource('TestExternalClass_TypeCastToJSObject',
  18014. LinesToStr([ // statements
  18015. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  18016. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], this.IUnknown);',
  18017. 'rtl.createClass(this, "TObject", null, function () {',
  18018. ' this.$init = function () {',
  18019. ' };',
  18020. ' this.$final = function () {',
  18021. ' };',
  18022. '});',
  18023. 'rtl.createClass(this, "TChild", this.TObject, function () {',
  18024. '});',
  18025. 'rtl.recNewT(this, "TRec", function () {',
  18026. ' this.$eq = function (b) {',
  18027. ' return true;',
  18028. ' };',
  18029. ' this.$assign = function (s) {',
  18030. ' return this;',
  18031. ' };',
  18032. '});',
  18033. 'this.Obj = null;',
  18034. 'this.Child = null;',
  18035. 'this.i = null;',
  18036. 'this.Bird = null;',
  18037. 'this.j = null;',
  18038. 'this.r = this.TRec.$new();',
  18039. 'this.c = null;',
  18040. '']),
  18041. LinesToStr([ // $mod.$main
  18042. '$mod.j = $mod.IUnknown;',
  18043. '$mod.j = $mod.IBird;',
  18044. '$mod.j = $mod.TObject;',
  18045. '$mod.j = $mod.TChild;',
  18046. '$mod.j = $mod.TRec;',
  18047. '$mod.j = $mod.Obj;',
  18048. '$mod.j = $mod.Child;',
  18049. '$mod.j = $mod.i;',
  18050. '$mod.j = $mod.Bird;',
  18051. '$mod.j = $mod.r;',
  18052. '$mod.j = $mod.c;',
  18053. '']));
  18054. end;
  18055. procedure TTestModule.TestExternalClass_TypeCastStringToExternalString;
  18056. begin
  18057. StartProgram(false);
  18058. Add('{$modeswitch externalclass}');
  18059. Add('type');
  18060. Add(' TJSString = class external name ''String''');
  18061. Add(' class function fromCharCode() : string; varargs;');
  18062. Add(' function anchor(const aName : string) : string;');
  18063. Add(' end;');
  18064. Add('var');
  18065. Add(' s: string;');
  18066. Add('begin');
  18067. Add(' s:=TJSString.fromCharCode(65,66);');
  18068. Add(' s:=TJSString(s).anchor(s);');
  18069. Add(' s:=TJSString(''foo'').anchor(s);');
  18070. ConvertProgram;
  18071. CheckSource('TestExternalClass_TypeCastStringToExternalString',
  18072. LinesToStr([ // statements
  18073. 'this.s = "";',
  18074. '']),
  18075. LinesToStr([ // $mod.$main
  18076. '$mod.s = String.fromCharCode(65, 66);',
  18077. '$mod.s = $mod.s.anchor($mod.s);',
  18078. '$mod.s = "foo".anchor($mod.s);',
  18079. '']));
  18080. end;
  18081. procedure TTestModule.TestExternalClass_TypeCastToJSFunction;
  18082. begin
  18083. StartProgram(false);
  18084. Add([
  18085. '{$modeswitch externalclass}',
  18086. 'type',
  18087. ' TJSObject = class external name ''Object'' end;',
  18088. ' TJSFunction = class external name ''Function''',
  18089. ' function bind(thisArg: TJSObject): TJSFunction; varargs;',
  18090. ' function call(thisArg: TJSObject): JSValue; varargs;',
  18091. ' end;',
  18092. ' TObject = class',
  18093. ' procedure DoIt(i: longint);',
  18094. ' end;',
  18095. ' TFuncInt = function(o: TObject): longint;',
  18096. 'function GetIt(o: TObject): longint;',
  18097. ' procedure Sub; begin end;',
  18098. 'var',
  18099. ' f: TJSFunction;',
  18100. ' fi: TFuncInt;',
  18101. 'begin',
  18102. ' fi:=TFuncInt(f);',
  18103. ' f:=TJSFunction(fi);',
  18104. ' f:=TJSFunction(@GetIt);',
  18105. ' f:=TJSFunction(@GetIt).bind(nil,3);',
  18106. ' f:=TJSFunction(@Sub);',
  18107. ' f:=TJSFunction(@o.doit);',
  18108. ' f:=TJSFunction(fi).bind(nil,4)',
  18109. 'end;',
  18110. 'procedure TObject.DoIt(i: longint);',
  18111. ' procedure Sub; begin end;',
  18112. 'var f: TJSFunction;',
  18113. 'begin',
  18114. ' f:=TJSFunction(@DoIt);',
  18115. ' f:=TJSFunction(@DoIt).bind(nil,13);',
  18116. ' f:=TJSFunction(@Sub);',
  18117. ' f:=TJSFunction(@GetIt);',
  18118. 'end;',
  18119. 'begin']);
  18120. ConvertProgram;
  18121. CheckSource('TestExternalClass_TypeCastToJSFunction',
  18122. LinesToStr([ // statements
  18123. 'rtl.createClass(this, "TObject", null, function () {',
  18124. ' this.$init = function () {',
  18125. ' };',
  18126. ' this.$final = function () {',
  18127. ' };',
  18128. ' this.DoIt = function (i) {',
  18129. ' var $Self = this;',
  18130. ' function Sub() {',
  18131. ' };',
  18132. ' var f = null;',
  18133. ' f = this.DoIt;',
  18134. ' f = this.DoIt.bind(null, 13);',
  18135. ' f = Sub;',
  18136. ' f = $mod.GetIt;',
  18137. ' };',
  18138. '});',
  18139. 'this.GetIt = function (o) {',
  18140. ' var Result = 0;',
  18141. ' function Sub() {',
  18142. ' };',
  18143. ' var f = null;',
  18144. ' var fi = null;',
  18145. ' fi = f;',
  18146. ' f = fi;',
  18147. ' f = $mod.GetIt;',
  18148. ' f = $mod.GetIt.bind(null, 3);',
  18149. ' f = Sub;',
  18150. ' f = $mod.TObject.DoIt;',
  18151. ' f = fi.bind(null, 4);',
  18152. ' return Result;',
  18153. '};',
  18154. '']),
  18155. LinesToStr([ // $mod.$main
  18156. '']));
  18157. end;
  18158. procedure TTestModule.TestExternalClass_TypeCastDelphiUnrelated;
  18159. begin
  18160. StartProgram(false);
  18161. Add([
  18162. '{$mode delphi}',
  18163. '{$modeswitch externalclass}',
  18164. 'type',
  18165. ' TJSObject = class external name ''Object'' end;',
  18166. ' TJSWindow = class external name ''Window''(TJSObject)',
  18167. ' procedure Open;',
  18168. ' end;',
  18169. ' TJSEventTarget = class external name ''Event''(TJSObject)',
  18170. ' procedure Execute;',
  18171. ' end;',
  18172. 'procedure Fly;',
  18173. 'var',
  18174. ' w: TJSWindow;',
  18175. ' e: TJSEventTarget;',
  18176. 'begin',
  18177. ' w:=TJSWindow(e);',
  18178. ' e:=TJSEventTarget(w);',
  18179. 'end;',
  18180. 'begin']);
  18181. ConvertProgram;
  18182. CheckSource('TestExternalClass_TypeCastDelphiUnrelated',
  18183. LinesToStr([ // statements
  18184. 'this.Fly = function () {',
  18185. ' var w = null;',
  18186. ' var e = null;',
  18187. ' w = e;',
  18188. ' e = w;',
  18189. '};',
  18190. '']),
  18191. LinesToStr([ // $mod.$main
  18192. '']));
  18193. end;
  18194. procedure TTestModule.TestExternalClass_CallClassFunctionOfInstanceFail;
  18195. begin
  18196. StartProgram(false);
  18197. Add('{$modeswitch externalclass}');
  18198. Add('type');
  18199. Add(' TJSString = class external name ''String''');
  18200. Add(' class function fromCharCode() : string; varargs;');
  18201. Add(' end;');
  18202. Add('var');
  18203. Add(' s: string;');
  18204. Add(' sObj: TJSString;');
  18205. Add('begin');
  18206. Add(' s:=sObj.fromCharCode(65,66);');
  18207. SetExpectedPasResolverError('External class instance cannot access static class function fromCharCode',
  18208. nExternalClassInstanceCannotAccessStaticX);
  18209. ConvertProgram;
  18210. end;
  18211. procedure TTestModule.TestExternalClass_BracketAccessor;
  18212. begin
  18213. StartProgram(false);
  18214. Add([
  18215. '{$modeswitch externalclass}',
  18216. 'type',
  18217. ' TJSArray = class external name ''Array2''',
  18218. ' function GetItems(Index: longint): jsvalue; external name ''[]'';',
  18219. ' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';',
  18220. ' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;',
  18221. ' end;',
  18222. 'procedure DoIt(vI: JSValue; const vJ: jsvalue; var vK: jsvalue; out vL: jsvalue);',
  18223. 'begin end;',
  18224. 'var',
  18225. ' Arr: tjsarray;',
  18226. ' s: string;',
  18227. ' i: longint;',
  18228. ' v: jsvalue;',
  18229. 'begin',
  18230. ' v:=arr[0];',
  18231. ' v:=arr.items[1];',
  18232. ' arr[2]:=s;',
  18233. ' arr.items[3]:=s;',
  18234. ' arr[4]:=i;',
  18235. ' arr[5]:=arr[6];',
  18236. ' arr.items[7]:=arr.items[8];',
  18237. ' with arr do items[9]:=items[10];',
  18238. ' doit(arr[7],arr[8],arr[9],arr[10]);',
  18239. ' with arr do begin',
  18240. ' v:=GetItems(14);',
  18241. ' setitems(15,16);',
  18242. ' end;',
  18243. ' v:=test1.arr.items[17];',
  18244. ' test1.arr.items[18]:=v;',
  18245. '']);
  18246. ConvertProgram;
  18247. CheckSource('TestExternalClass_BracketAccessor',
  18248. LinesToStr([ // statements
  18249. 'this.DoIt = function (vI, vJ, vK, vL) {',
  18250. '};',
  18251. 'this.Arr = null;',
  18252. 'this.s = "";',
  18253. 'this.i = 0;',
  18254. 'this.v = undefined;',
  18255. '']),
  18256. LinesToStr([ // $mod.$main
  18257. '$mod.v = $mod.Arr[0];',
  18258. '$mod.v = $mod.Arr[1];',
  18259. '$mod.Arr[2] = $mod.s;',
  18260. '$mod.Arr[3] = $mod.s;',
  18261. '$mod.Arr[4] = $mod.i;',
  18262. '$mod.Arr[5] = $mod.Arr[6];',
  18263. '$mod.Arr[7] = $mod.Arr[8];',
  18264. 'var $with = $mod.Arr;',
  18265. '$with[9] = $with[10];',
  18266. '$mod.DoIt($mod.Arr[7], $mod.Arr[8], {',
  18267. ' a: 9,',
  18268. ' p: $mod.Arr,',
  18269. ' get: function () {',
  18270. ' return this.p[this.a];',
  18271. ' },',
  18272. ' set: function (v) {',
  18273. ' this.p[this.a] = v;',
  18274. ' }',
  18275. '}, {',
  18276. ' a: 10,',
  18277. ' p: $mod.Arr,',
  18278. ' get: function () {',
  18279. ' return this.p[this.a];',
  18280. ' },',
  18281. ' set: function (v) {',
  18282. ' this.p[this.a] = v;',
  18283. ' }',
  18284. '});',
  18285. 'var $with1 = $mod.Arr;',
  18286. '$mod.v = $with1[14];',
  18287. '$with1[15] = 16;',
  18288. '$mod.v = $mod.Arr[17];',
  18289. '$mod.Arr[18] = $mod.v;',
  18290. '']));
  18291. end;
  18292. procedure TTestModule.TestExternalClass_BracketAccessor_Call;
  18293. begin
  18294. StartProgram(false);
  18295. Add([
  18296. '{$modeswitch externalclass}',
  18297. 'type',
  18298. ' TJSArray = class external name ''Array2''',
  18299. ' function GetItems(Index: longint): jsvalue; external name ''[]'';',
  18300. ' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';',
  18301. ' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;',
  18302. ' end;',
  18303. ' TMyArr = class(TJSArray)',
  18304. ' procedure DoIt;',
  18305. ' end;',
  18306. 'procedure tmyarr.DoIt;',
  18307. 'begin',
  18308. ' Items[1]:=Items[2];',
  18309. ' SetItems(3,getItems(4));',
  18310. 'end;',
  18311. 'var',
  18312. ' Arr: tmyarr;',
  18313. ' s: string;',
  18314. ' i: longint;',
  18315. ' v: jsvalue;',
  18316. 'begin',
  18317. ' v:=arr[0];',
  18318. ' v:=arr.items[1];',
  18319. ' arr[2]:=s;',
  18320. ' arr.items[3]:=s;',
  18321. ' arr[4]:=i;',
  18322. ' arr[5]:=arr[6];',
  18323. ' arr.items[7]:=arr.items[8];',
  18324. ' with arr do items[9]:=items[10];',
  18325. ' with arr do begin',
  18326. ' v:=GetItems(14);',
  18327. ' setitems(15,16);',
  18328. ' end;',
  18329. '']);
  18330. ConvertProgram;
  18331. CheckSource('TestExternalClass_BracketAccessor_Call',
  18332. LinesToStr([ // statements
  18333. 'rtl.createClassExt(this, "TMyArr", Array2, "", function () {',
  18334. ' this.$init = function () {',
  18335. ' };',
  18336. ' this.$final = function () {',
  18337. ' };',
  18338. ' this.DoIt = function () {',
  18339. ' this[1] = this[2];',
  18340. ' this[3] = this[4];',
  18341. ' };',
  18342. '});',
  18343. 'this.Arr = null;',
  18344. 'this.s = "";',
  18345. 'this.i = 0;',
  18346. 'this.v = undefined;',
  18347. '']),
  18348. LinesToStr([ // $mod.$main
  18349. '$mod.v = $mod.Arr[0];',
  18350. '$mod.v = $mod.Arr[1];',
  18351. '$mod.Arr[2] = $mod.s;',
  18352. '$mod.Arr[3] = $mod.s;',
  18353. '$mod.Arr[4] = $mod.i;',
  18354. '$mod.Arr[5] = $mod.Arr[6];',
  18355. '$mod.Arr[7] = $mod.Arr[8];',
  18356. 'var $with = $mod.Arr;',
  18357. '$with[9] = $with[10];',
  18358. 'var $with1 = $mod.Arr;',
  18359. '$mod.v = $with1[14];',
  18360. '$with1[15] = 16;',
  18361. '']));
  18362. end;
  18363. procedure TTestModule.TestExternalClass_BracketAccessor_2ParamsFail;
  18364. begin
  18365. StartProgram(false);
  18366. Add('{$modeswitch externalclass}');
  18367. Add('type');
  18368. Add(' TJSArray = class external name ''Array2''');
  18369. Add(' function GetItems(Index1, Index2: longint): jsvalue; external name ''[]'';');
  18370. Add(' procedure SetItems(Index1, Index2: longint; Value: jsvalue); external name ''[]'';');
  18371. Add(' property Items[Index1, Index2: longint]: jsvalue read GetItems write SetItems; default;');
  18372. Add(' end;');
  18373. Add('begin');
  18374. SetExpectedPasResolverError(sBracketAccessorOfExternalClassMustHaveOneParameter,
  18375. nBracketAccessorOfExternalClassMustHaveOneParameter);
  18376. ConvertProgram;
  18377. end;
  18378. procedure TTestModule.TestExternalClass_BracketAccessor_ReadOnly;
  18379. begin
  18380. StartProgram(false);
  18381. Add('{$modeswitch externalclass}');
  18382. Add('type');
  18383. Add(' TJSArray = class external name ''Array2''');
  18384. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  18385. Add(' property Items[Index: longint]: jsvalue read GetItems; default;');
  18386. Add(' end;');
  18387. Add('procedure DoIt(vI: JSValue; const vJ: jsvalue);');
  18388. Add('begin end;');
  18389. Add('var');
  18390. Add(' Arr: tjsarray;');
  18391. Add(' v: jsvalue;');
  18392. Add('begin');
  18393. Add(' v:=arr[0];');
  18394. Add(' v:=arr.items[1];');
  18395. Add(' with arr do v:=items[2];');
  18396. Add(' doit(arr[3],arr[4]);');
  18397. ConvertProgram;
  18398. CheckSource('TestExternalClass_BracketAccessor_ReadOnly',
  18399. LinesToStr([ // statements
  18400. 'this.DoIt = function (vI, vJ) {',
  18401. '};',
  18402. 'this.Arr = null;',
  18403. 'this.v = undefined;',
  18404. '']),
  18405. LinesToStr([ // $mod.$main
  18406. '$mod.v = $mod.Arr[0];',
  18407. '$mod.v = $mod.Arr[1];',
  18408. 'var $with = $mod.Arr;',
  18409. '$mod.v = $with[2];',
  18410. '$mod.DoIt($mod.Arr[3], $mod.Arr[4]);',
  18411. '']));
  18412. end;
  18413. procedure TTestModule.TestExternalClass_BracketAccessor_WriteOnly;
  18414. begin
  18415. StartProgram(false);
  18416. Add('{$modeswitch externalclass}');
  18417. Add('type');
  18418. Add(' TJSArray = class external name ''Array2''');
  18419. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  18420. Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
  18421. Add(' end;');
  18422. Add('var');
  18423. Add(' Arr: tjsarray;');
  18424. Add(' s: string;');
  18425. Add(' i: longint;');
  18426. Add(' v: jsvalue;');
  18427. Add('begin');
  18428. Add(' arr[2]:=s;');
  18429. Add(' arr.items[3]:=s;');
  18430. Add(' arr[4]:=i;');
  18431. Add(' with arr do items[5]:=i;');
  18432. ConvertProgram;
  18433. CheckSource('TestExternalClass_BracketAccessor_WriteOnly',
  18434. LinesToStr([ // statements
  18435. 'this.Arr = null;',
  18436. 'this.s = "";',
  18437. 'this.i = 0;',
  18438. 'this.v = undefined;',
  18439. '']),
  18440. LinesToStr([ // $mod.$main
  18441. '$mod.Arr[2] = $mod.s;',
  18442. '$mod.Arr[3] = $mod.s;',
  18443. '$mod.Arr[4] = $mod.i;',
  18444. 'var $with = $mod.Arr;',
  18445. '$with[5] = $mod.i;',
  18446. '']));
  18447. end;
  18448. procedure TTestModule.TestExternalClass_BracketAccessor_MultiType;
  18449. begin
  18450. StartProgram(false);
  18451. Add('{$modeswitch externalclass}');
  18452. Add('type');
  18453. Add(' TJSArray = class external name ''Array2''');
  18454. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  18455. Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
  18456. Add(' procedure SetNumbers(Index: longint; Value: longint); external name ''[]'';');
  18457. Add(' property Numbers[Index: longint]: longint write SetNumbers;');
  18458. Add(' end;');
  18459. Add('var');
  18460. Add(' Arr: tjsarray;');
  18461. Add(' s: string;');
  18462. Add(' i: longint;');
  18463. Add(' v: jsvalue;');
  18464. Add('begin');
  18465. Add(' arr[2]:=s;');
  18466. Add(' arr.items[3]:=s;');
  18467. Add(' arr.numbers[4]:=i;');
  18468. Add(' with arr do items[5]:=i;');
  18469. Add(' with arr do numbers[6]:=i;');
  18470. ConvertProgram;
  18471. CheckSource('TestExternalClass_BracketAccessor_MultiType',
  18472. LinesToStr([ // statements
  18473. 'this.Arr = null;',
  18474. 'this.s = "";',
  18475. 'this.i = 0;',
  18476. 'this.v = undefined;',
  18477. '']),
  18478. LinesToStr([ // $mod.$main
  18479. '$mod.Arr[2] = $mod.s;',
  18480. '$mod.Arr[3] = $mod.s;',
  18481. '$mod.Arr[4] = $mod.i;',
  18482. 'var $with = $mod.Arr;',
  18483. '$with[5] = $mod.i;',
  18484. 'var $with1 = $mod.Arr;',
  18485. '$with1[6] = $mod.i;',
  18486. '']));
  18487. end;
  18488. procedure TTestModule.TestExternalClass_BracketAccessor_Index;
  18489. begin
  18490. StartProgram(false);
  18491. Add('{$modeswitch externalclass}');
  18492. Add('type');
  18493. Add(' TJSArray = class external name ''Array2''');
  18494. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  18495. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  18496. Add(' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;');
  18497. Add(' end;');
  18498. Add('var');
  18499. Add(' Arr: tjsarray;');
  18500. Add(' i: longint;');
  18501. Add(' IntArr: array of longint;');
  18502. Add(' v: jsvalue;');
  18503. Add('begin');
  18504. Add(' v:=arr.items[i];');
  18505. Add(' arr[longint(v)]:=arr.items[intarr[0]];');
  18506. Add(' arr.items[intarr[1]]:=arr[IntArr[2]];');
  18507. ConvertProgram;
  18508. CheckSource('TestExternalClass_BracketAccessor_Index',
  18509. LinesToStr([ // statements
  18510. 'this.Arr = null;',
  18511. 'this.i = 0;',
  18512. 'this.IntArr = [];',
  18513. 'this.v = undefined;',
  18514. '']),
  18515. LinesToStr([ // $mod.$main
  18516. '$mod.v = $mod.Arr[$mod.i];',
  18517. '$mod.Arr[rtl.trunc($mod.v)] = $mod.Arr[$mod.IntArr[0]];',
  18518. '$mod.Arr[$mod.IntArr[1]] = $mod.Arr[$mod.IntArr[2]];',
  18519. '']));
  18520. end;
  18521. procedure TTestModule.TestExternalClass_ForInJSObject;
  18522. begin
  18523. StartProgram(false);
  18524. Add([
  18525. '{$modeswitch externalclass}',
  18526. 'type',
  18527. ' TJSObject = class external name ''Object''',
  18528. ' end;',
  18529. 'var',
  18530. ' o: TJSObject;',
  18531. ' key: string;',
  18532. 'begin',
  18533. ' for key in o do',
  18534. ' if key=''abc'' then ;',
  18535. '']);
  18536. ConvertProgram;
  18537. CheckSource('TestExternalClass_ForInJSObject',
  18538. LinesToStr([ // statements
  18539. 'this.o = null;',
  18540. 'this.key = "";',
  18541. '']),
  18542. LinesToStr([ // $mod.$main
  18543. 'for ($mod.key in $mod.o) if ($mod.key === "abc") ;',
  18544. '']));
  18545. end;
  18546. procedure TTestModule.TestExternalClass_ForInJSArray;
  18547. begin
  18548. StartProgram(false);
  18549. Add([
  18550. '{$modeswitch externalclass}',
  18551. 'type',
  18552. ' TJSInt8Array = class external name ''Int8Array''',
  18553. ' private',
  18554. ' flength: NativeInt external name ''length'';',
  18555. ' function getValue(Index: NativeInt): shortint; external name ''[]'';',
  18556. ' public',
  18557. ' property values[Index: NativeInt]: Shortint Read getValue; default;',
  18558. ' property Length: NativeInt read flength;',
  18559. ' end;',
  18560. 'var',
  18561. ' a: TJSInt8Array;',
  18562. ' value: shortint;',
  18563. 'begin',
  18564. ' for value in a do',
  18565. ' if value=3 then ;',
  18566. '']);
  18567. ConvertProgram;
  18568. CheckSource('TestExternalClass_ForInJSArray',
  18569. LinesToStr([ // statements
  18570. 'this.a = null;',
  18571. 'this.value = 0;',
  18572. '']),
  18573. LinesToStr([ // $mod.$main
  18574. 'for (var $in = $mod.a, $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) {',
  18575. ' $mod.value = $in[$l];',
  18576. ' if ($mod.value === 3) ;',
  18577. '};',
  18578. '']));
  18579. end;
  18580. procedure TTestModule.TestExternalClass_IncompatibleArgDuplicateIdentifier;
  18581. begin
  18582. AddModuleWithIntfImplSrc('unit2.pas',
  18583. LinesToStr([
  18584. '{$modeswitch externalclass}',
  18585. 'type',
  18586. ' TJSBufferSource = class external name ''BufferSource''',
  18587. ' end;',
  18588. 'procedure DoIt(s: TJSBufferSource); external name ''DoIt'';',
  18589. '']),
  18590. '');
  18591. AddModuleWithIntfImplSrc('unit3.pas',
  18592. LinesToStr([
  18593. '{$modeswitch externalclass}',
  18594. 'type',
  18595. ' TJSBufferSource = class external name ''BufferSource''',
  18596. ' end;',
  18597. '']),
  18598. '');
  18599. StartUnit(true);
  18600. Add([
  18601. 'interface',
  18602. 'uses unit2, unit3;',
  18603. 'procedure DoSome(s: TJSBufferSource);',
  18604. 'implementation',
  18605. 'procedure DoSome(s: TJSBufferSource);',
  18606. 'begin',
  18607. ' DoIt(s);',
  18608. 'end;',
  18609. '']);
  18610. SetExpectedPasResolverError('Incompatible type arg no. 1: Got "unit3.TJSBufferSource", expected "unit2.TJSBufferSource"',
  18611. nIncompatibleTypeArgNo);
  18612. ConvertUnit;
  18613. end;
  18614. procedure TTestModule.TestClassInterface_Corba;
  18615. begin
  18616. StartProgram(false);
  18617. Add([
  18618. '{$interfaces corba}',
  18619. 'type',
  18620. ' IUnknown = interface;',
  18621. ' IUnknown = interface',
  18622. ' [''{00000000-0000-0000-C000-000000000046}'']',
  18623. ' end;',
  18624. ' IInterface = IUnknown;',
  18625. ' IBird = interface(IInterface)',
  18626. ' function GetSize: longint;',
  18627. ' procedure SetSize(i: longint);',
  18628. ' property Size: longint read GetSize write SetSize;',
  18629. ' procedure DoIt(i: longint);',
  18630. ' end;',
  18631. ' TObject = class',
  18632. ' end;',
  18633. ' TBird = class(TObject,IBird)',
  18634. ' function GetSize: longint; virtual; abstract;',
  18635. ' procedure SetSize(i: longint); virtual; abstract;',
  18636. ' procedure DoIt(i: longint); virtual; abstract;',
  18637. ' end;',
  18638. 'var',
  18639. ' BirdIntf: IBird;',
  18640. 'begin',
  18641. ' BirdIntf.Size:=BirdIntf.Size;',
  18642. '']);
  18643. ConvertProgram;
  18644. CheckSource('TestClassInterface_Corba',
  18645. LinesToStr([ // statements
  18646. 'rtl.createInterface(this, "IUnknown", "{00000000-0000-0000-C000-000000000046}", [], null);',
  18647. 'rtl.createInterface(this, "IBird", "{5BD1A53B-69BB-37EE-AF32-BEFB86D85B03}", ["GetSize", "SetSize", "DoIt"], this.IUnknown);',
  18648. 'rtl.createClass(this, "TObject", null, function () {',
  18649. ' this.$init = function () {',
  18650. ' };',
  18651. ' this.$final = function () {',
  18652. ' };',
  18653. '});',
  18654. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18655. ' rtl.addIntf(this, $mod.IBird);',
  18656. '});',
  18657. 'this.BirdIntf = null;',
  18658. '']),
  18659. LinesToStr([ // $mod.$main
  18660. ' $mod.BirdIntf.SetSize($mod.BirdIntf.GetSize());',
  18661. '']));
  18662. end;
  18663. procedure TTestModule.TestClassInterface_ProcExternalFail;
  18664. begin
  18665. StartProgram(false);
  18666. Add([
  18667. '{$interfaces corba}',
  18668. 'type',
  18669. ' IUnknown = interface',
  18670. ' procedure DoIt; external name ''foo'';',
  18671. ' end;',
  18672. 'begin']);
  18673. SetExpectedParserError(
  18674. 'Fields are not allowed in interface at token "Identifier external" in file test1.pp at line 6 column 21',
  18675. nParserNoFieldsAllowed);
  18676. ConvertProgram;
  18677. end;
  18678. procedure TTestModule.TestClassInterface_Overloads;
  18679. begin
  18680. StartProgram(false);
  18681. Add([
  18682. '{$interfaces corba}',
  18683. 'type',
  18684. ' integer = longint;',
  18685. ' IUnknown = interface',
  18686. ' procedure DoIt(i: integer);',
  18687. ' procedure DoIt(s: string);',
  18688. ' end;',
  18689. ' IBird = interface(IUnknown)',
  18690. ' procedure DoIt(b: boolean); overload;',
  18691. ' end;',
  18692. ' TObject = class',
  18693. ' end;',
  18694. ' TBird = class(TObject,IBird)',
  18695. ' procedure DoIt(o: TObject);',
  18696. ' procedure DoIt(s: string);',
  18697. ' procedure DoIt(i: integer);',
  18698. ' procedure DoIt(b: boolean);',
  18699. ' end;',
  18700. 'procedure TBird.DoIt(o: TObject); begin end;',
  18701. 'procedure TBird.DoIt(s: string); begin end;',
  18702. 'procedure TBird.DoIt(i: integer); begin end;',
  18703. 'procedure TBird.DoIt(b: boolean); begin end;',
  18704. 'var',
  18705. ' BirdIntf: IBird;',
  18706. 'begin',
  18707. ' BirdIntf.DoIt(3);',
  18708. ' BirdIntf.DoIt(''abc'');',
  18709. ' BirdIntf.DoIt(true);',
  18710. '']);
  18711. ConvertProgram;
  18712. CheckSource('TestClassInterface_Overloads',
  18713. LinesToStr([ // statements
  18714. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDC4-8A2AE2C59400}", ["DoIt", "DoIt$1"], null);',
  18715. 'rtl.createInterface(this, "IBird", "{8285DD5E-EA3E-396E-AE88-000B86AABF05}", ["DoIt$2"], this.IUnknown);',
  18716. 'rtl.createClass(this, "TObject", null, function () {',
  18717. ' this.$init = function () {',
  18718. ' };',
  18719. ' this.$final = function () {',
  18720. ' };',
  18721. '});',
  18722. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18723. ' this.DoIt = function (o) {',
  18724. ' };',
  18725. ' this.DoIt$1 = function (s) {',
  18726. ' };',
  18727. ' this.DoIt$2 = function (i) {',
  18728. ' };',
  18729. ' this.DoIt$3 = function (b) {',
  18730. ' };',
  18731. ' rtl.addIntf(this, $mod.IBird, {',
  18732. ' DoIt$2: "DoIt$3",',
  18733. ' DoIt: "DoIt$2"',
  18734. ' });',
  18735. '});',
  18736. 'this.BirdIntf = null;',
  18737. '']),
  18738. LinesToStr([ // $mod.$main
  18739. '$mod.BirdIntf.DoIt(3);',
  18740. '$mod.BirdIntf.DoIt$1("abc");',
  18741. '$mod.BirdIntf.DoIt$2(true);',
  18742. '']));
  18743. end;
  18744. procedure TTestModule.TestClassInterface_DuplicateGUIInIntfListFail;
  18745. begin
  18746. StartProgram(false);
  18747. Add([
  18748. '{$interfaces corba}',
  18749. 'type',
  18750. ' IBird = interface',
  18751. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  18752. ' end;',
  18753. ' IDog = interface',
  18754. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  18755. ' end;',
  18756. ' TObject = class(IBird,IDog)',
  18757. ' end;',
  18758. 'begin']);
  18759. SetExpectedPasResolverError('Duplicate GUID {4B3BA825-E0EC-4799-A19C-55F714A07959} in IDog and IBird',
  18760. nDuplicateGUIDXInYZ);
  18761. ConvertProgram;
  18762. end;
  18763. procedure TTestModule.TestClassInterface_DuplicateGUIInAncestorFail;
  18764. begin
  18765. StartProgram(false);
  18766. Add([
  18767. '{$interfaces corba}',
  18768. 'type',
  18769. ' IAnimal = interface',
  18770. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  18771. ' end;',
  18772. ' IBird = interface(IAnimal)',
  18773. ' end;',
  18774. ' IHawk = interface(IBird)',
  18775. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  18776. ' end;',
  18777. 'begin']);
  18778. SetExpectedPasResolverError('Duplicate GUID {4B3BA825-E0EC-4799-A19C-55F714A07959} in IHawk and IAnimal',
  18779. nDuplicateGUIDXInYZ);
  18780. ConvertProgram;
  18781. end;
  18782. procedure TTestModule.TestClassInterface_AncestorImpl;
  18783. begin
  18784. StartProgram(false);
  18785. Add([
  18786. '{$interfaces corba}',
  18787. 'type',
  18788. ' integer = longint;',
  18789. ' IUnknown = interface',
  18790. ' procedure DoIt(i: integer);',
  18791. ' end;',
  18792. ' IBird = interface',
  18793. ' procedure Fly(i: integer);',
  18794. ' end;',
  18795. ' TObject = class(IUnknown)',
  18796. ' procedure DoIt(i: integer);',
  18797. ' end;',
  18798. ' TBird = class(IBird)',
  18799. ' procedure Fly(i: integer);',
  18800. ' end;',
  18801. 'procedure TObject.DoIt(i: integer); begin end;',
  18802. 'procedure TBird.Fly(i: integer); begin end;',
  18803. 'begin',
  18804. '']);
  18805. ConvertProgram;
  18806. CheckSource('TestClassInterface_AncestorIntf',
  18807. LinesToStr([ // statements
  18808. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDC4-8A2800000000}", ["DoIt"], null);',
  18809. 'rtl.createInterface(this, "IBird", "{B92D5841-6264-3AE3-BF20-000000000000}", ["Fly"], null);',
  18810. 'rtl.createClass(this, "TObject", null, function () {',
  18811. ' this.$init = function () {',
  18812. ' };',
  18813. ' this.$final = function () {',
  18814. ' };',
  18815. ' this.DoIt = function (i) {',
  18816. ' };',
  18817. ' rtl.addIntf(this, $mod.IUnknown);',
  18818. '});',
  18819. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18820. ' this.Fly = function (i) {',
  18821. ' };',
  18822. ' rtl.addIntf(this, $mod.IBird);',
  18823. ' rtl.addIntf(this, $mod.IUnknown);',
  18824. '});',
  18825. '']),
  18826. LinesToStr([ // $mod.$main
  18827. '']));
  18828. end;
  18829. procedure TTestModule.TestClassInterface_ImplReintroduce;
  18830. begin
  18831. StartProgram(false);
  18832. Add([
  18833. '{$interfaces corba}',
  18834. 'type',
  18835. ' integer = longint;',
  18836. ' IBird = interface',
  18837. ' procedure DoIt(i: integer);',
  18838. ' end;',
  18839. ' TObject = class',
  18840. ' procedure DoIt(i: integer);',
  18841. ' end;',
  18842. ' TBird = class(IBird)',
  18843. ' procedure DoIt(i: integer); virtual; reintroduce;',
  18844. ' end;',
  18845. 'procedure TObject.DoIt(i: integer); begin end;',
  18846. 'procedure TBird.DoIt(i: integer); begin end;',
  18847. 'begin',
  18848. '']);
  18849. ConvertProgram;
  18850. CheckSource('TestClassInterface_ImplReintroduce',
  18851. LinesToStr([ // statements
  18852. 'rtl.createInterface(this, "IBird", "{B92D5841-6264-3AE2-8594-000000000000}", ["DoIt"], null);',
  18853. 'rtl.createClass(this, "TObject", null, function () {',
  18854. ' this.$init = function () {',
  18855. ' };',
  18856. ' this.$final = function () {',
  18857. ' };',
  18858. ' this.DoIt = function (i) {',
  18859. ' };',
  18860. '});',
  18861. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18862. ' this.DoIt$1 = function (i) {',
  18863. ' };',
  18864. ' rtl.addIntf(this, $mod.IBird, {',
  18865. ' DoIt: "DoIt$1"',
  18866. ' });',
  18867. '});',
  18868. '']),
  18869. LinesToStr([ // $mod.$main
  18870. '']));
  18871. end;
  18872. procedure TTestModule.TestClassInterface_MethodResolution;
  18873. begin
  18874. StartProgram(false);
  18875. Add([
  18876. '{$interfaces corba}',
  18877. 'type',
  18878. ' IUnknown = interface',
  18879. ' procedure Walk(i: longint);',
  18880. ' end;',
  18881. ' IBird = interface(IUnknown)',
  18882. ' procedure Walk(b: boolean); overload;',
  18883. ' procedure Fly(s: string);',
  18884. ' end;',
  18885. ' TObject = class',
  18886. ' end;',
  18887. ' TBird = class(TObject,IBird)',
  18888. ' procedure IBird.Fly = Move;',
  18889. ' procedure IBird.Walk = Hop;',
  18890. ' procedure Hop(i: longint);',
  18891. ' procedure Move(s: string);',
  18892. ' procedure Hop(b: boolean);',
  18893. ' end;',
  18894. 'procedure TBird.Move(s: string); begin end;',
  18895. 'procedure TBird.Hop(i: longint); begin end;',
  18896. 'procedure TBird.Hop(b: boolean); begin end;',
  18897. 'var',
  18898. ' BirdIntf: IBird;',
  18899. 'begin',
  18900. ' BirdIntf.Walk(3);',
  18901. ' BirdIntf.Walk(true);',
  18902. ' BirdIntf.Fly(''abc'');',
  18903. '']);
  18904. ConvertProgram;
  18905. CheckSource('TestClassInterface_MethodResolution',
  18906. LinesToStr([ // statements
  18907. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDD7-23D600000000}", ["Walk"], null);',
  18908. 'rtl.createInterface(this, "IBird", "{CF8A4986-80F6-396E-AE88-000B86AAE208}", ["Walk$1", "Fly"], this.IUnknown);',
  18909. 'rtl.createClass(this, "TObject", null, function () {',
  18910. ' this.$init = function () {',
  18911. ' };',
  18912. ' this.$final = function () {',
  18913. ' };',
  18914. '});',
  18915. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18916. ' this.Hop = function (i) {',
  18917. ' };',
  18918. ' this.Move = function (s) {',
  18919. ' };',
  18920. ' this.Hop$1 = function (b) {',
  18921. ' };',
  18922. ' rtl.addIntf(this, $mod.IBird, {',
  18923. ' Walk$1: "Hop$1",',
  18924. ' Fly: "Move",',
  18925. ' Walk: "Hop"',
  18926. ' });',
  18927. '});',
  18928. 'this.BirdIntf = null;',
  18929. '']),
  18930. LinesToStr([ // $mod.$main
  18931. '$mod.BirdIntf.Walk(3);',
  18932. '$mod.BirdIntf.Walk$1(true);',
  18933. '$mod.BirdIntf.Fly("abc");',
  18934. '']));
  18935. end;
  18936. procedure TTestModule.TestClassInterface_AncestorMoreInterfaces;
  18937. begin
  18938. StartProgram(false);
  18939. Add([
  18940. '{$interfaces com}',
  18941. 'type',
  18942. ' IUnknown = interface',
  18943. ' function _AddRef: longint;',
  18944. ' procedure Walk;',
  18945. ' end;',
  18946. ' IBird = interface end;',
  18947. ' IDog = interface end;',
  18948. ' TObject = class(IBird,IDog)',
  18949. ' function _AddRef: longint; virtual; abstract;',
  18950. ' procedure Walk; virtual; abstract;',
  18951. ' end;',
  18952. ' TBird = class(IUnknown)',
  18953. ' end;',
  18954. 'begin',
  18955. '']);
  18956. ConvertProgram;
  18957. CheckSource('TestClassInterface_COM_AncestorLess',
  18958. LinesToStr([ // statements
  18959. 'rtl.createInterface(this, "IUnknown", "{8F2D5841-758A-322B-BDDF-21CD521DD723}", ["_AddRef", "Walk"], null);',
  18960. 'rtl.createInterface(this, "IBird", "{CCE11D4C-6504-3AEE-AE88-000B86AAE675}", [], this.IUnknown);',
  18961. 'rtl.createInterface(this, "IDog", "{CCE11D4C-6504-3AEE-AE88-000B8E5FC675}", [], this.IUnknown);',
  18962. 'rtl.createClass(this, "TObject", null, function () {',
  18963. ' this.$init = function () {',
  18964. ' };',
  18965. ' this.$final = function () {',
  18966. ' };',
  18967. ' rtl.addIntf(this, $mod.IBird);',
  18968. ' rtl.addIntf(this, $mod.IDog);',
  18969. '});',
  18970. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18971. ' rtl.addIntf(this, $mod.IUnknown);',
  18972. ' rtl.addIntf(this, $mod.IBird);',
  18973. ' rtl.addIntf(this, $mod.IDog);',
  18974. '});',
  18975. '']),
  18976. LinesToStr([ // $mod.$main
  18977. '']));
  18978. end;
  18979. procedure TTestModule.TestClassInterface_MethodOverride;
  18980. begin
  18981. StartProgram(false);
  18982. Add([
  18983. '{$interfaces corba}',
  18984. 'type',
  18985. ' IUnknown = interface',
  18986. ' [''{D6D98E5B-8A10-4FEC-856A-7BFC847FE74B}'']',
  18987. ' procedure Go;',
  18988. ' end;',
  18989. ' TObject = class(IUnknown)',
  18990. ' procedure Go; virtual; abstract;',
  18991. ' end;',
  18992. ' TBird = class',
  18993. ' procedure Go; override;',
  18994. ' end;',
  18995. ' TCat = class(TObject)',
  18996. ' procedure Go; override;',
  18997. ' end;',
  18998. ' TDog = class(TObject, IUnknown)',
  18999. ' procedure Go; override;',
  19000. ' end;',
  19001. 'procedure TBird.Go; begin end;',
  19002. 'procedure TCat.Go; begin end;',
  19003. 'procedure TDog.Go; begin end;',
  19004. 'begin',
  19005. '']);
  19006. ConvertProgram;
  19007. CheckSource('TestClassInterface_MethodOverride',
  19008. LinesToStr([ // statements
  19009. 'rtl.createInterface(this, "IUnknown", "{D6D98E5B-8A10-4FEC-856A-7BFC847FE74B}", ["Go"], null);',
  19010. 'rtl.createClass(this, "TObject", null, function () {',
  19011. ' this.$init = function () {',
  19012. ' };',
  19013. ' this.$final = function () {',
  19014. ' };',
  19015. ' rtl.addIntf(this, $mod.IUnknown);',
  19016. '});',
  19017. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  19018. ' this.Go = function () {',
  19019. ' };',
  19020. ' rtl.addIntf(this, $mod.IUnknown);',
  19021. '});',
  19022. 'rtl.createClass(this, "TCat", this.TObject, function () {',
  19023. ' this.Go = function () {',
  19024. ' };',
  19025. ' rtl.addIntf(this, $mod.IUnknown);',
  19026. '});',
  19027. 'rtl.createClass(this, "TDog", this.TObject, function () {',
  19028. ' this.Go = function () {',
  19029. ' };',
  19030. ' rtl.addIntf(this, $mod.IUnknown);',
  19031. '});',
  19032. '']),
  19033. LinesToStr([ // $mod.$main
  19034. '']));
  19035. end;
  19036. procedure TTestModule.TestClassInterface_Corba_Delegation;
  19037. begin
  19038. StartProgram(false);
  19039. Add([
  19040. '{$interfaces corba}',
  19041. 'type',
  19042. ' IUnknown = interface',
  19043. ' end;',
  19044. ' IBird = interface(IUnknown)',
  19045. ' procedure Fly(s: string);',
  19046. ' end;',
  19047. ' IEagle = interface(IBird)',
  19048. ' end;',
  19049. ' IDove = interface(IBird)',
  19050. ' end;',
  19051. ' ISwallow = interface(IBird)',
  19052. ' end;',
  19053. ' TObject = class',
  19054. ' end;',
  19055. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  19056. ' procedure Fly(s: string); virtual; abstract;',
  19057. ' end;',
  19058. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  19059. ' FBirdIntf: IBird;',
  19060. ' property BirdIntf: IBird read FBirdIntf implements IBird;',
  19061. ' function GetEagleIntf: IEagle; virtual; abstract;',
  19062. ' property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  19063. ' FDoveObj: TBird;',
  19064. ' property DoveObj: TBird read FDoveObj implements IDove;',
  19065. ' function GetSwallowObj: TBird; virtual; abstract;',
  19066. ' property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  19067. ' end;',
  19068. 'begin',
  19069. '']);
  19070. ConvertProgram;
  19071. CheckSource('TestClassInterface_Corba_Delegation',
  19072. LinesToStr([ // statements
  19073. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19074. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  19075. 'rtl.createInterface(this, "IEagle", "{489289DE-FDE2-34A6-8288-39119022B1B4}", [], this.IBird);',
  19076. 'rtl.createInterface(this, "IDove", "{489289DE-FDE2-34A6-8288-39118EF16074}", [], this.IBird);',
  19077. 'rtl.createInterface(this, "ISwallow", "{B89289DE-FDE2-34A6-8288-3911CBDCB359}", [], this.IBird);',
  19078. 'rtl.createClass(this, "TObject", null, function () {',
  19079. ' this.$init = function () {',
  19080. ' };',
  19081. ' this.$final = function () {',
  19082. ' };',
  19083. '});',
  19084. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  19085. ' rtl.addIntf(this, $mod.IBird);',
  19086. ' rtl.addIntf(this, $mod.IEagle);',
  19087. ' rtl.addIntf(this, $mod.IDove);',
  19088. ' rtl.addIntf(this, $mod.ISwallow);',
  19089. '});',
  19090. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  19091. ' this.$init = function () {',
  19092. ' $mod.TObject.$init.call(this);',
  19093. ' this.FBirdIntf = null;',
  19094. ' this.FDoveObj = null;',
  19095. ' };',
  19096. ' this.$final = function () {',
  19097. ' this.FBirdIntf = undefined;',
  19098. ' this.FDoveObj = undefined;',
  19099. ' $mod.TObject.$final.call(this);',
  19100. ' };',
  19101. ' this.$intfmaps = {',
  19102. ' "{478D080B-C0F6-396E-AE88-000B87785B07}": function () {',
  19103. ' return this.FBirdIntf;',
  19104. ' },',
  19105. ' "{489289DE-FDE2-34A6-8288-39119022B1B4}": function () {',
  19106. ' return this.GetEagleIntf();',
  19107. ' },',
  19108. ' "{489289DE-FDE2-34A6-8288-39118EF16074}": function () {',
  19109. ' return rtl.getIntfT(this.FDoveObj, $mod.IDove);',
  19110. ' },',
  19111. ' "{B89289DE-FDE2-34A6-8288-3911CBDCB359}": function () {',
  19112. ' return rtl.getIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  19113. ' }',
  19114. ' };',
  19115. '});',
  19116. '']),
  19117. LinesToStr([ // $mod.$main
  19118. '']));
  19119. end;
  19120. procedure TTestModule.TestClassInterface_Corba_DelegationStatic;
  19121. begin
  19122. StartProgram(false);
  19123. Add([
  19124. '{$interfaces corba}',
  19125. 'type',
  19126. ' IUnknown = interface',
  19127. ' end;',
  19128. ' IBird = interface(IUnknown)',
  19129. ' procedure Fly(s: string);',
  19130. ' end;',
  19131. ' IEagle = interface(IBird)',
  19132. ' end;',
  19133. ' IDove = interface(IBird)',
  19134. ' end;',
  19135. ' ISwallow = interface(IBird)',
  19136. ' end;',
  19137. ' TObject = class',
  19138. ' end;',
  19139. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  19140. ' procedure Fly(s: string); virtual; abstract;',
  19141. ' end;',
  19142. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  19143. ' private',
  19144. ' class var FBirdIntf: IBird;',
  19145. ' class var FDoveObj: TBird;',
  19146. ' class function GetEagleIntf: IEagle; virtual; abstract;',
  19147. ' class function GetSwallowObj: TBird; virtual; abstract;',
  19148. ' protected',
  19149. ' class property BirdIntf: IBird read FBirdIntf implements IBird;',
  19150. ' class property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  19151. ' class property DoveObj: TBird read FDoveObj implements IDove;',
  19152. ' class property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  19153. ' end;',
  19154. 'begin',
  19155. '']);
  19156. ConvertProgram;
  19157. CheckSource('TestClassInterface_Corba_DelegationStatic',
  19158. LinesToStr([ // statements
  19159. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19160. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  19161. 'rtl.createInterface(this, "IEagle", "{489289DE-FDE2-34A6-8288-39119022B1B4}", [], this.IBird);',
  19162. 'rtl.createInterface(this, "IDove", "{489289DE-FDE2-34A6-8288-39118EF16074}", [], this.IBird);',
  19163. 'rtl.createInterface(this, "ISwallow", "{B89289DE-FDE2-34A6-8288-3911CBDCB359}", [], this.IBird);',
  19164. 'rtl.createClass(this, "TObject", null, function () {',
  19165. ' this.$init = function () {',
  19166. ' };',
  19167. ' this.$final = function () {',
  19168. ' };',
  19169. '});',
  19170. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  19171. ' rtl.addIntf(this, $mod.IBird);',
  19172. ' rtl.addIntf(this, $mod.IEagle);',
  19173. ' rtl.addIntf(this, $mod.IDove);',
  19174. ' rtl.addIntf(this, $mod.ISwallow);',
  19175. '});',
  19176. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  19177. ' this.FBirdIntf = null;',
  19178. ' this.FDoveObj = null;',
  19179. ' this.$intfmaps = {',
  19180. ' "{478D080B-C0F6-396E-AE88-000B87785B07}": function () {',
  19181. ' return this.FBirdIntf;',
  19182. ' },',
  19183. ' "{489289DE-FDE2-34A6-8288-39119022B1B4}": function () {',
  19184. ' return this.GetEagleIntf();',
  19185. ' },',
  19186. ' "{489289DE-FDE2-34A6-8288-39118EF16074}": function () {',
  19187. ' return rtl.getIntfT(this.FDoveObj, $mod.IDove);',
  19188. ' },',
  19189. ' "{B89289DE-FDE2-34A6-8288-3911CBDCB359}": function () {',
  19190. ' return rtl.getIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  19191. ' }',
  19192. ' };',
  19193. '});',
  19194. '']),
  19195. LinesToStr([ // $mod.$main
  19196. '']));
  19197. end;
  19198. procedure TTestModule.TestClassInterface_Corba_Operators;
  19199. begin
  19200. StartProgram(false);
  19201. Add([
  19202. '{$interfaces corba}',
  19203. 'type',
  19204. ' IUnknown = interface',
  19205. ' end;',
  19206. ' IBird = interface(IUnknown)',
  19207. ' function GetItems(Index: longint): longint;',
  19208. ' procedure SetItems(Index: longint; Value: longint);',
  19209. ' property Items[Index: longint]: longint read GetItems write SetItems; default;',
  19210. ' end;',
  19211. ' TObject = class',
  19212. ' end;',
  19213. ' TBird = class(TObject,IBird)',
  19214. ' function GetItems(Index: longint): longint; virtual; abstract;',
  19215. ' procedure SetItems(Index: longint; Value: longint); virtual; abstract;',
  19216. ' end;',
  19217. 'var',
  19218. ' IntfVar: IBird = nil;',
  19219. ' IntfVar2: IBird;',
  19220. ' ObjVar: TBird;',
  19221. ' v: JSValue;',
  19222. 'begin',
  19223. ' IntfVar:=nil;',
  19224. ' IntfVar[3]:=IntfVar[4];',
  19225. ' if Assigned(IntfVar) then ;',
  19226. ' IntfVar:=IntfVar2;',
  19227. ' IntfVar:=ObjVar;',
  19228. ' if IntfVar=IntfVar2 then ;',
  19229. ' if IntfVar<>IntfVar2 then ;',
  19230. ' if IntfVar is IBird then ;',
  19231. ' if IntfVar is TBird then ;',
  19232. ' if ObjVar is IBird then ;',
  19233. ' IntfVar:=IntfVar2 as IBird;',
  19234. ' ObjVar:=IntfVar2 as TBird;',
  19235. ' IntfVar:=ObjVar as IBird;',
  19236. ' IntfVar:=IBird(IntfVar2);',
  19237. ' ObjVar:=TBird(IntfVar);',
  19238. ' IntfVar:=IBird(ObjVar);',
  19239. ' v:=IntfVar;',
  19240. ' IntfVar:=IBird(v);',
  19241. ' if v is IBird then ;',
  19242. ' v:=JSValue(IntfVar);',
  19243. ' v:=IBird;',
  19244. '']);
  19245. ConvertProgram;
  19246. CheckSource('TestClassInterface_Corba_Operators',
  19247. LinesToStr([ // statements
  19248. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19249. 'rtl.createInterface(this, "IBird", "{D53FED90-DE59-3202-B1AE-000B87785B08}", ["GetItems", "SetItems"], this.IUnknown);',
  19250. 'rtl.createClass(this, "TObject", null, function () {',
  19251. ' this.$init = function () {',
  19252. ' };',
  19253. ' this.$final = function () {',
  19254. ' };',
  19255. '});',
  19256. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  19257. ' rtl.addIntf(this, $mod.IBird);',
  19258. '});',
  19259. 'this.IntfVar = null;',
  19260. 'this.IntfVar2 = null;',
  19261. 'this.ObjVar = null;',
  19262. 'this.v = undefined;',
  19263. '']),
  19264. LinesToStr([ // $mod.$main
  19265. '$mod.IntfVar = null;',
  19266. '$mod.IntfVar.SetItems(3, $mod.IntfVar.GetItems(4));',
  19267. 'if ($mod.IntfVar != null) ;',
  19268. '$mod.IntfVar = $mod.IntfVar2;',
  19269. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar,$mod.IBird);',
  19270. 'if ($mod.IntfVar === $mod.IntfVar2) ;',
  19271. 'if ($mod.IntfVar !== $mod.IntfVar2) ;',
  19272. 'if ($mod.IBird.isPrototypeOf($mod.IntfVar)) ;',
  19273. 'if (rtl.intfIsClass($mod.IntfVar, $mod.TBird)) ;',
  19274. 'if (rtl.getIntfT($mod.ObjVar, $mod.IBird) !== null) ;',
  19275. '$mod.IntfVar = rtl.as($mod.IntfVar2, $mod.IBird);',
  19276. '$mod.ObjVar = rtl.intfAsClass($mod.IntfVar2, $mod.TBird);',
  19277. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar, $mod.IBird);',
  19278. '$mod.IntfVar = $mod.IntfVar2;',
  19279. '$mod.ObjVar = rtl.intfToClass($mod.IntfVar, $mod.TBird);',
  19280. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar, $mod.IBird);',
  19281. '$mod.v = $mod.IntfVar;',
  19282. '$mod.IntfVar = rtl.getObject($mod.v);',
  19283. 'if (rtl.isExt($mod.v, $mod.IBird, 1)) ;',
  19284. '$mod.v = $mod.IntfVar;',
  19285. '$mod.v = $mod.IBird;',
  19286. '']));
  19287. end;
  19288. procedure TTestModule.TestClassInterface_Corba_Args;
  19289. begin
  19290. StartProgram(false);
  19291. Add([
  19292. '{$interfaces corba}',
  19293. 'type',
  19294. ' IUnknown = interface',
  19295. ' end;',
  19296. ' IBird = interface(IUnknown)',
  19297. ' end;',
  19298. ' TObject = class',
  19299. ' end;',
  19300. ' TBird = class(TObject,IBird)',
  19301. ' end;',
  19302. 'procedure DoIt(var u; i: IBird; const j: IBird);',
  19303. 'begin',
  19304. ' DoIt(i,i,i);',
  19305. 'end;',
  19306. 'procedure Change(var i: IBird; out j: IBird);',
  19307. 'begin',
  19308. ' DoIt(i,i,i);',
  19309. ' Change(i,i);',
  19310. 'end;',
  19311. 'var',
  19312. ' i: IBird;',
  19313. ' o: TBird;',
  19314. 'begin',
  19315. ' DoIt(i,i,i);',
  19316. ' Change(i,i);',
  19317. ' DoIt(o,o,o);',
  19318. '']);
  19319. ConvertProgram;
  19320. CheckSource('TestClassInterface_Corba_Args',
  19321. LinesToStr([ // statements
  19322. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19323. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], this.IUnknown);',
  19324. 'rtl.createClass(this, "TObject", null, function () {',
  19325. ' this.$init = function () {',
  19326. ' };',
  19327. ' this.$final = function () {',
  19328. ' };',
  19329. '});',
  19330. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  19331. ' rtl.addIntf(this, $mod.IBird);',
  19332. '});',
  19333. 'this.DoIt = function (u, i, j) {',
  19334. ' $mod.DoIt({',
  19335. ' get: function () {',
  19336. ' return i;',
  19337. ' },',
  19338. ' set: function (v) {',
  19339. ' i = v;',
  19340. ' }',
  19341. ' }, i, i);',
  19342. '};',
  19343. 'this.Change = function (i, j) {',
  19344. ' $mod.DoIt(i, i.get(), i.get());',
  19345. ' $mod.Change(i, i);',
  19346. '};',
  19347. 'this.i = null;',
  19348. 'this.o = null;',
  19349. '']),
  19350. LinesToStr([ // $mod.$main
  19351. '$mod.DoIt({',
  19352. ' p: $mod,',
  19353. ' get: function () {',
  19354. ' return this.p.i;',
  19355. ' },',
  19356. ' set: function (v) {',
  19357. ' this.p.i = v;',
  19358. ' }',
  19359. '}, $mod.i, $mod.i);',
  19360. '$mod.Change({',
  19361. ' p: $mod,',
  19362. ' get: function () {',
  19363. ' return this.p.i;',
  19364. ' },',
  19365. ' set: function (v) {',
  19366. ' this.p.i = v;',
  19367. ' }',
  19368. '}, {',
  19369. ' p: $mod,',
  19370. ' get: function () {',
  19371. ' return this.p.i;',
  19372. ' },',
  19373. ' set: function (v) {',
  19374. ' this.p.i = v;',
  19375. ' }',
  19376. '});',
  19377. '$mod.DoIt({',
  19378. ' p: $mod,',
  19379. ' get: function () {',
  19380. ' return this.p.o;',
  19381. ' },',
  19382. ' set: function (v) {',
  19383. ' this.p.o = v;',
  19384. ' }',
  19385. '}, rtl.getIntfT($mod.o, $mod.IBird), rtl.getIntfT($mod.o, $mod.IBird));',
  19386. '']));
  19387. end;
  19388. procedure TTestModule.TestClassInterface_Corba_ForIn;
  19389. begin
  19390. StartProgram(false);
  19391. Add([
  19392. '{$interfaces corba}',
  19393. 'type',
  19394. ' IUnknown = interface end;',
  19395. ' TObject = class',
  19396. ' Id: longint;',
  19397. ' end;',
  19398. ' IEnumerator = interface(IUnknown)',
  19399. ' function GetCurrent: TObject;',
  19400. ' function MoveNext: Boolean;',
  19401. ' property Current: TObject read GetCurrent;',
  19402. ' end;',
  19403. ' IEnumerable = interface(IUnknown)',
  19404. ' function GetEnumerator: IEnumerator;',
  19405. ' end;',
  19406. 'var',
  19407. ' o: TObject;',
  19408. ' i: IEnumerable;',
  19409. 'begin',
  19410. ' for o in i do o.Id:=3;',
  19411. '']);
  19412. ConvertProgram;
  19413. CheckSource('TestClassInterface_Corba_ForIn',
  19414. LinesToStr([ // statements
  19415. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19416. 'rtl.createClass(this, "TObject", null, function () {',
  19417. ' this.$init = function () {',
  19418. ' this.Id = 0;',
  19419. ' };',
  19420. ' this.$final = function () {',
  19421. ' };',
  19422. '});',
  19423. 'rtl.createInterface(this, "IEnumerator", "{95D7745D-ED61-3F13-BBE4-07708161999E}", ["GetCurrent", "MoveNext"], this.IUnknown);',
  19424. 'rtl.createInterface(this, "IEnumerable", "{8CC9D45D-ED7D-3B73-96B6-290B931BB19E}", ["GetEnumerator"], this.IUnknown);',
  19425. 'this.o = null;',
  19426. 'this.i = null;',
  19427. '']),
  19428. LinesToStr([ // $mod.$main
  19429. 'var $in = $mod.i.GetEnumerator();',
  19430. 'while ($in.MoveNext()) {',
  19431. ' $mod.o = $in.GetCurrent();',
  19432. ' $mod.o.Id = 3;',
  19433. '};',
  19434. '']));
  19435. end;
  19436. procedure TTestModule.TestClassInterface_COM_AssignVar;
  19437. begin
  19438. StartProgram(false);
  19439. Add([
  19440. '{$interfaces com}',
  19441. 'type',
  19442. ' IUnknown = interface',
  19443. ' function _AddRef: longint;',
  19444. ' function _Release: longint;',
  19445. ' end;',
  19446. ' TObject = class(IUnknown)',
  19447. ' function _AddRef: longint; virtual; abstract;',
  19448. ' function _Release: longint; virtual; abstract;',
  19449. ' end;',
  19450. 'var',
  19451. ' i: IUnknown;',
  19452. 'procedure DoGlobal(o: TObject);',
  19453. 'begin',
  19454. ' i:=nil;',
  19455. ' i:=o;',
  19456. ' i:=i;',
  19457. 'end;',
  19458. 'procedure DoLocal(o: TObject);',
  19459. 'const k: IUnknown = nil;',
  19460. 'var j: IUnknown;',
  19461. 'begin',
  19462. ' k:=o;',
  19463. ' k:=i;',
  19464. ' j:=o;',
  19465. ' j:=i;',
  19466. 'end;',
  19467. 'var o: TObject;',
  19468. 'begin',
  19469. ' i:=nil;',
  19470. ' i:=o;',
  19471. '']);
  19472. ConvertProgram;
  19473. CheckSource('TestClassInterface_COM_AssignVar',
  19474. LinesToStr([ // statements
  19475. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19476. 'rtl.createClass(this, "TObject", null, function () {',
  19477. ' this.$init = function () {',
  19478. ' };',
  19479. ' this.$final = function () {',
  19480. ' };',
  19481. ' rtl.addIntf(this, $mod.IUnknown);',
  19482. '});',
  19483. 'this.i = null;',
  19484. 'this.DoGlobal = function (o) {',
  19485. ' rtl.setIntfP($mod, "i", null);',
  19486. ' rtl.setIntfP($mod, "i", rtl.queryIntfT(o, $mod.IUnknown), true);',
  19487. ' rtl.setIntfP($mod, "i", $mod.i);',
  19488. '};',
  19489. 'var k = null;',
  19490. 'this.DoLocal = function (o) {',
  19491. ' var j = null;',
  19492. ' try{',
  19493. ' k = rtl.setIntfL(k, rtl.queryIntfT(o, $mod.IUnknown), true);',
  19494. ' k = rtl.setIntfL(k, $mod.i);',
  19495. ' j = rtl.setIntfL(j, rtl.queryIntfT(o, $mod.IUnknown), true);',
  19496. ' j = rtl.setIntfL(j, $mod.i);',
  19497. ' }finally{',
  19498. ' rtl._Release(j);',
  19499. ' };',
  19500. '};',
  19501. 'this.o = null;',
  19502. '']),
  19503. LinesToStr([ // $mod.$main
  19504. 'rtl.setIntfP($mod, "i", null);',
  19505. 'rtl.setIntfP($mod, "i", rtl.queryIntfT($mod.o, $mod.IUnknown), true);',
  19506. '']));
  19507. end;
  19508. procedure TTestModule.TestClassInterface_COM_AssignArg;
  19509. begin
  19510. StartProgram(false);
  19511. Add([
  19512. '{$interfaces com}',
  19513. 'type',
  19514. ' IUnknown = interface',
  19515. ' function _AddRef: longint;',
  19516. ' function _Release: longint;',
  19517. ' end;',
  19518. ' TObject = class(IUnknown)',
  19519. ' function _AddRef: longint; virtual; abstract;',
  19520. ' function _Release: longint; virtual; abstract;',
  19521. ' end;',
  19522. 'procedure DoDefault(i, j: IUnknown);',
  19523. 'begin',
  19524. ' i:=nil;',
  19525. ' i:=j;',
  19526. 'end;',
  19527. 'begin',
  19528. '']);
  19529. ConvertProgram;
  19530. CheckSource('TestClassInterface_COM_AssignArg',
  19531. LinesToStr([ // statements
  19532. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19533. 'rtl.createClass(this, "TObject", null, function () {',
  19534. ' this.$init = function () {',
  19535. ' };',
  19536. ' this.$final = function () {',
  19537. ' };',
  19538. ' rtl.addIntf(this, $mod.IUnknown);',
  19539. '});',
  19540. 'this.DoDefault = function (i, j) {',
  19541. ' rtl._AddRef(i);',
  19542. ' try {',
  19543. ' i = rtl.setIntfL(i, null);',
  19544. ' i = rtl.setIntfL(i, j);',
  19545. ' } finally {',
  19546. ' rtl._Release(i);',
  19547. ' };',
  19548. '};',
  19549. '']),
  19550. LinesToStr([ // $mod.$main
  19551. '']));
  19552. end;
  19553. procedure TTestModule.TestClassInterface_COM_FunctionResult;
  19554. begin
  19555. StartProgram(false);
  19556. Add([
  19557. '{$interfaces com}',
  19558. 'type',
  19559. ' IUnknown = interface',
  19560. ' function _AddRef: longint;',
  19561. ' function _Release: longint;',
  19562. ' end;',
  19563. ' TObject = class(IUnknown)',
  19564. ' function _AddRef: longint; virtual; abstract;',
  19565. ' function _Release: longint; virtual; abstract;',
  19566. ' end;',
  19567. 'function DoDefault(i: IUnknown): IUnknown;',
  19568. 'begin',
  19569. ' Result:=i;',
  19570. ' if Result<>nil then exit;',
  19571. 'end;',
  19572. 'begin',
  19573. '']);
  19574. ConvertProgram;
  19575. CheckSource('TestClassInterface_COM_FunctionResult',
  19576. LinesToStr([ // statements
  19577. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19578. 'rtl.createClass(this, "TObject", null, function () {',
  19579. ' this.$init = function () {',
  19580. ' };',
  19581. ' this.$final = function () {',
  19582. ' };',
  19583. ' rtl.addIntf(this, $mod.IUnknown);',
  19584. '});',
  19585. 'this.DoDefault = function (i) {',
  19586. ' var Result = null;',
  19587. ' var $ok = false;',
  19588. ' try {',
  19589. ' Result = rtl.setIntfL(Result, i);',
  19590. ' if(Result !== null){',
  19591. ' $ok = true;',
  19592. ' return Result;',
  19593. ' };',
  19594. ' $ok = true;',
  19595. ' } finally {',
  19596. ' if(!$ok) rtl._Release(Result);',
  19597. ' };',
  19598. ' return Result;',
  19599. '};',
  19600. '']),
  19601. LinesToStr([ // $mod.$main
  19602. '']));
  19603. end;
  19604. procedure TTestModule.TestClassInterface_COM_InheritedFuncResult;
  19605. begin
  19606. StartProgram(false);
  19607. Add([
  19608. '{$interfaces com}',
  19609. 'type',
  19610. ' IUnknown = interface',
  19611. ' function _AddRef: longint;',
  19612. ' function _Release: longint;',
  19613. ' end;',
  19614. ' TObject = class(IUnknown)',
  19615. ' function _AddRef: longint; virtual; abstract;',
  19616. ' function _Release: longint; virtual; abstract;',
  19617. ' function GetIntf: IUnknown; virtual;',
  19618. ' end;',
  19619. ' TMouse = class',
  19620. ' function GetIntf: IUnknown; override;',
  19621. ' end;',
  19622. 'function TObject.GetIntf: IUnknown; begin end;',
  19623. 'function TMouse.GetIntf: IUnknown;',
  19624. 'var i: IUnknown;',
  19625. 'begin',
  19626. ' inherited;',
  19627. ' inherited GetIntf;',
  19628. ' inherited GetIntf();',
  19629. ' Result:=inherited GetIntf;',
  19630. ' Result:=inherited GetIntf();',
  19631. ' i:=inherited GetIntf;',
  19632. ' i:=inherited GetIntf();',
  19633. 'end;',
  19634. 'begin',
  19635. '']);
  19636. ConvertProgram;
  19637. CheckSource('TestClassInterface_COM_InheritedFuncResult',
  19638. LinesToStr([ // statements
  19639. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19640. 'rtl.createClass(this, "TObject", null, function () {',
  19641. ' this.$init = function () {',
  19642. ' };',
  19643. ' this.$final = function () {',
  19644. ' };',
  19645. ' this.GetIntf = function () {',
  19646. ' var Result = null;',
  19647. ' return Result;',
  19648. ' };',
  19649. ' rtl.addIntf(this, $mod.IUnknown);',
  19650. '});',
  19651. 'rtl.createClass(this, "TMouse", this.TObject, function () {',
  19652. ' this.GetIntf = function () {',
  19653. ' var Result = null;',
  19654. ' var i = null;',
  19655. ' var $ir = rtl.createIntfRefs();',
  19656. ' var $ok = false;',
  19657. ' try {',
  19658. ' $ir.ref(1, $mod.TObject.GetIntf.call(this));',
  19659. ' $ir.ref(2, $mod.TObject.GetIntf.call(this));',
  19660. ' $ir.ref(3, $mod.TObject.GetIntf.call(this));',
  19661. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  19662. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  19663. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  19664. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  19665. ' $ok = true;',
  19666. ' } finally {',
  19667. ' $ir.free();',
  19668. ' rtl._Release(i);',
  19669. ' if (!$ok) rtl._Release(Result);',
  19670. ' };',
  19671. ' return Result;',
  19672. ' };',
  19673. ' rtl.addIntf(this, $mod.IUnknown);',
  19674. '});',
  19675. '']),
  19676. LinesToStr([ // $mod.$main
  19677. '']));
  19678. end;
  19679. procedure TTestModule.TestClassInterface_COM_IsAsTypeCasts;
  19680. begin
  19681. StartProgram(false);
  19682. Add([
  19683. '{$interfaces com}',
  19684. 'type',
  19685. ' IUnknown = interface',
  19686. ' function _AddRef: longint;',
  19687. ' function _Release: longint;',
  19688. ' end;',
  19689. ' TObject = class(IUnknown)',
  19690. ' function _AddRef: longint; virtual; abstract;',
  19691. ' function _Release: longint; virtual; abstract;',
  19692. ' end;',
  19693. 'procedure DoDefault(i, j: IUnknown; o: TObject);',
  19694. 'begin',
  19695. ' if i is IUnknown then ;',
  19696. ' if o is IUnknown then ;',
  19697. ' if i is TObject then ;',
  19698. ' i:=j as IUnknown;',
  19699. ' i:=o as IUnknown;',
  19700. ' o:=j as TObject;',
  19701. ' i:=IUnknown(j);',
  19702. ' i:=IUnknown(o);',
  19703. ' o:=TObject(i);',
  19704. 'end;',
  19705. 'begin',
  19706. '']);
  19707. ConvertProgram;
  19708. CheckSource('TestClassInterface_COM_IsAsTypeCasts',
  19709. LinesToStr([ // statements
  19710. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19711. 'rtl.createClass(this, "TObject", null, function () {',
  19712. ' this.$init = function () {',
  19713. ' };',
  19714. ' this.$final = function () {',
  19715. ' };',
  19716. ' rtl.addIntf(this, $mod.IUnknown);',
  19717. '});',
  19718. 'this.DoDefault = function (i, j, o) {',
  19719. ' rtl._AddRef(i);',
  19720. ' try {',
  19721. ' if (rtl.intfIsIntfT(i, $mod.IUnknown)) ;',
  19722. ' if (rtl.queryIntfIsT(o, $mod.IUnknown)) ;',
  19723. ' if (rtl.intfIsClass(i, $mod.TObject)) ;',
  19724. ' i = rtl.setIntfL(i, rtl.intfAsIntfT(j, $mod.IUnknown));',
  19725. ' i = rtl.setIntfL(i, rtl.queryIntfT(o, $mod.IUnknown), true);',
  19726. ' o = rtl.intfAsClass(j, $mod.TObject);',
  19727. ' i = rtl.setIntfL(i, j);',
  19728. ' i = rtl.setIntfL(i, rtl.queryIntfT(o, $mod.IUnknown), true);',
  19729. ' o = rtl.intfToClass(i, $mod.TObject);',
  19730. ' } finally {',
  19731. ' rtl._Release(i);',
  19732. ' };',
  19733. '};',
  19734. '']),
  19735. LinesToStr([ // $mod.$main
  19736. '']));
  19737. end;
  19738. procedure TTestModule.TestClassInterface_COM_PassAsArg;
  19739. begin
  19740. StartProgram(false);
  19741. Add([
  19742. '{$interfaces com}',
  19743. 'type',
  19744. ' IUnknown = interface',
  19745. ' function _AddRef: longint;',
  19746. ' function _Release: longint;',
  19747. ' end;',
  19748. ' TObject = class(IUnknown)',
  19749. ' function _AddRef: longint; virtual; abstract;',
  19750. ' function _Release: longint; virtual; abstract;',
  19751. ' end;',
  19752. 'procedure DoIt(v: IUnknown; const j: IUnknown; var k: IUnknown; out l: IUnknown);',
  19753. 'var o: TObject;',
  19754. 'begin',
  19755. ' DoIt(v,v,v,v);',
  19756. ' DoIt(o,o,k,k);',
  19757. 'end;',
  19758. 'procedure DoSome;',
  19759. 'var v: IUnknown;',
  19760. 'begin',
  19761. ' DoIt(v,v,v,v);',
  19762. 'end;',
  19763. 'var i: IUnknown;',
  19764. 'begin',
  19765. ' DoIt(i,i,i,i);',
  19766. '']);
  19767. ConvertProgram;
  19768. CheckSource('TestClassInterface_COM_PassAsArg',
  19769. LinesToStr([ // statements
  19770. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19771. 'rtl.createClass(this, "TObject", null, function () {',
  19772. ' this.$init = function () {',
  19773. ' };',
  19774. ' this.$final = function () {',
  19775. ' };',
  19776. ' rtl.addIntf(this, $mod.IUnknown);',
  19777. '});',
  19778. 'this.DoIt = function (v, j, k, l) {',
  19779. ' var o = null;',
  19780. ' var $ir = rtl.createIntfRefs();',
  19781. ' rtl._AddRef(v);',
  19782. ' try {',
  19783. ' $mod.DoIt(v, v, {',
  19784. ' get: function () {',
  19785. ' return v;',
  19786. ' },',
  19787. ' set: function (w) {',
  19788. ' v = rtl.setIntfL(v, w);',
  19789. ' }',
  19790. ' }, {',
  19791. ' get: function () {',
  19792. ' return v;',
  19793. ' },',
  19794. ' set: function (w) {',
  19795. ' v = rtl.setIntfL(v, w);',
  19796. ' }',
  19797. ' });',
  19798. ' $mod.DoIt($ir.ref(1, rtl.queryIntfT(o, $mod.IUnknown)), $ir.ref(2, rtl.queryIntfT(o, $mod.IUnknown)), k, k);',
  19799. ' } finally {',
  19800. ' $ir.free();',
  19801. ' rtl._Release(v);',
  19802. ' };',
  19803. '};',
  19804. 'this.DoSome = function () {',
  19805. ' var v = null;',
  19806. ' try {',
  19807. ' $mod.DoIt(v, v, {',
  19808. ' get: function () {',
  19809. ' return v;',
  19810. ' },',
  19811. ' set: function (w) {',
  19812. ' v = rtl.setIntfL(v, w);',
  19813. ' }',
  19814. ' }, {',
  19815. ' get: function () {',
  19816. ' return v;',
  19817. ' },',
  19818. ' set: function (w) {',
  19819. ' v = rtl.setIntfL(v, w);',
  19820. ' }',
  19821. ' });',
  19822. ' } finally {',
  19823. ' rtl._Release(v);',
  19824. ' };',
  19825. '};',
  19826. 'this.i = null;',
  19827. '']),
  19828. LinesToStr([ // $mod.$main
  19829. '$mod.DoIt($mod.i, $mod.i, {',
  19830. ' p: $mod,',
  19831. ' get: function () {',
  19832. ' return this.p.i;',
  19833. ' },',
  19834. ' set: function (v) {',
  19835. ' rtl.setIntfP(this.p, "i", v);',
  19836. ' }',
  19837. '}, {',
  19838. ' p: $mod,',
  19839. ' get: function () {',
  19840. ' return this.p.i;',
  19841. ' },',
  19842. ' set: function (v) {',
  19843. ' rtl.setIntfP(this.p, "i", v);',
  19844. ' }',
  19845. '});',
  19846. '']));
  19847. end;
  19848. procedure TTestModule.TestClassInterface_COM_PassToUntypedParam;
  19849. begin
  19850. StartProgram(false);
  19851. Add([
  19852. '{$interfaces com}',
  19853. 'type',
  19854. ' IUnknown = interface',
  19855. ' function _AddRef: longint;',
  19856. ' function _Release: longint;',
  19857. ' end;',
  19858. ' TObject = class(IUnknown)',
  19859. ' function _AddRef: longint; virtual; abstract;',
  19860. ' function _Release: longint; virtual; abstract;',
  19861. ' end;',
  19862. 'procedure DoIt(out i);',
  19863. 'begin end;',
  19864. 'procedure DoSome;',
  19865. 'var v: IUnknown;',
  19866. 'begin',
  19867. ' DoIt(v);',
  19868. 'end;',
  19869. 'function GetIt: IUnknown;',
  19870. 'begin',
  19871. ' DoIt(Result);',
  19872. 'end;',
  19873. 'var i: IUnknown;',
  19874. 'begin',
  19875. ' DoIt(i);',
  19876. '']);
  19877. ConvertProgram;
  19878. CheckSource('TestClassInterface_COM_PassToUntypedParam',
  19879. LinesToStr([ // statements
  19880. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19881. 'rtl.createClass(this, "TObject", null, function () {',
  19882. ' this.$init = function () {',
  19883. ' };',
  19884. ' this.$final = function () {',
  19885. ' };',
  19886. ' rtl.addIntf(this, $mod.IUnknown);',
  19887. '});',
  19888. 'this.DoIt = function (i) {',
  19889. '};',
  19890. 'this.DoSome = function () {',
  19891. ' var v = null;',
  19892. ' try {',
  19893. ' $mod.DoIt({',
  19894. ' get: function () {',
  19895. ' return v;',
  19896. ' },',
  19897. ' set: function (w) {',
  19898. ' v = w;',
  19899. ' }',
  19900. ' });',
  19901. ' } finally {',
  19902. ' rtl._Release(v);',
  19903. ' };',
  19904. '};',
  19905. 'this.GetIt = function () {',
  19906. ' var Result = null;',
  19907. ' var $ok = false;',
  19908. ' try {',
  19909. ' $mod.DoIt({',
  19910. ' get: function () {',
  19911. ' return Result;',
  19912. ' },',
  19913. ' set: function (v) {',
  19914. ' Result = v;',
  19915. ' }',
  19916. ' });',
  19917. ' $ok = true;',
  19918. ' } finally {',
  19919. ' if (!$ok) rtl._Release(Result);',
  19920. ' };',
  19921. ' return Result;',
  19922. '};',
  19923. 'this.i = null;',
  19924. '']),
  19925. LinesToStr([ // $mod.$main
  19926. 'try {',
  19927. ' $mod.DoIt({',
  19928. ' p: $mod,',
  19929. ' get: function () {',
  19930. ' return this.p.i;',
  19931. ' },',
  19932. ' set: function (v) {',
  19933. ' this.p.i = v;',
  19934. ' }',
  19935. ' });',
  19936. '} finally {',
  19937. ' rtl._Release($mod.i);',
  19938. '};',
  19939. '']));
  19940. end;
  19941. procedure TTestModule.TestClassInterface_COM_FunctionInExpr;
  19942. begin
  19943. StartProgram(false);
  19944. Add([
  19945. '{$interfaces com}',
  19946. 'type',
  19947. ' IUnknown = interface',
  19948. ' function _AddRef: longint;',
  19949. ' function _Release: longint;',
  19950. ' end;',
  19951. ' TObject = class(IUnknown)',
  19952. ' function _AddRef: longint; virtual; abstract;',
  19953. ' function _Release: longint; virtual; abstract;',
  19954. ' end;',
  19955. 'function GetIt: IUnknown;',
  19956. 'begin',
  19957. 'end;',
  19958. 'procedure DoSome;',
  19959. 'var v: IUnknown;',
  19960. ' i: longint;',
  19961. 'begin',
  19962. ' v:=GetIt;',
  19963. ' v:=GetIt();',
  19964. ' GetIt()._AddRef;',
  19965. ' i:=GetIt()._AddRef;',
  19966. 'end;',
  19967. 'var v: IUnknown;',
  19968. ' i: longint;',
  19969. 'begin',
  19970. ' v:=GetIt;',
  19971. ' v:=GetIt();',
  19972. ' GetIt()._AddRef;',
  19973. ' i:=GetIt()._AddRef;',
  19974. '']);
  19975. ConvertProgram;
  19976. CheckSource('TestClassInterface_COM_FunctionInExpr',
  19977. LinesToStr([ // statements
  19978. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19979. 'rtl.createClass(this, "TObject", null, function () {',
  19980. ' this.$init = function () {',
  19981. ' };',
  19982. ' this.$final = function () {',
  19983. ' };',
  19984. ' rtl.addIntf(this, $mod.IUnknown);',
  19985. '});',
  19986. 'this.GetIt = function () {',
  19987. ' var Result = null;',
  19988. ' return Result;',
  19989. '};',
  19990. 'this.DoSome = function () {',
  19991. ' var v = null;',
  19992. ' var i = 0;',
  19993. ' var $ir = rtl.createIntfRefs();',
  19994. ' try {',
  19995. ' v = rtl.setIntfL(v, $mod.GetIt(), true);',
  19996. ' v = rtl.setIntfL(v, $mod.GetIt(), true);',
  19997. ' $ir.ref(1, $mod.GetIt())._AddRef();',
  19998. ' i = $ir.ref(2, $mod.GetIt())._AddRef();',
  19999. ' } finally {',
  20000. ' $ir.free();',
  20001. ' rtl._Release(v);',
  20002. ' };',
  20003. '};',
  20004. 'this.v = null;',
  20005. 'this.i = 0;',
  20006. '']),
  20007. LinesToStr([ // $mod.$main
  20008. 'var $ir = rtl.createIntfRefs();',
  20009. 'try {',
  20010. ' rtl.setIntfP($mod, "v", $mod.GetIt(), true);',
  20011. ' rtl.setIntfP($mod, "v", $mod.GetIt(), true);',
  20012. ' $ir.ref(1, $mod.GetIt())._AddRef();',
  20013. ' $mod.i = $ir.ref(2, $mod.GetIt())._AddRef();',
  20014. '} finally {',
  20015. ' $ir.free();',
  20016. '};',
  20017. '']));
  20018. end;
  20019. procedure TTestModule.TestClassInterface_COM_Property;
  20020. begin
  20021. StartProgram(false);
  20022. Add([
  20023. '{$interfaces com}',
  20024. 'type',
  20025. ' IUnknown = interface',
  20026. ' function _AddRef: longint;',
  20027. ' function _Release: longint;',
  20028. ' end;',
  20029. ' TObject = class(IUnknown)',
  20030. ' FAnt: IUnknown;',
  20031. ' function _AddRef: longint; virtual; abstract;',
  20032. ' function _Release: longint; virtual; abstract;',
  20033. ' function GetBird: IUnknown; virtual; abstract;',
  20034. ' procedure SetBird(Value: IUnknown); virtual; abstract;',
  20035. ' function GetItems(Index: longint): IUnknown; virtual; abstract;',
  20036. ' procedure SetItems(Index: longint; Value: IUnknown); virtual; abstract;',
  20037. ' property Ant: IUnknown read FAnt write FAnt;',
  20038. ' property Bird: IUnknown read GetBird write SetBird;',
  20039. ' property Items[Index: longint]: IUnknown read GetItems write SetItems; default;',
  20040. ' end;',
  20041. 'procedure DoIt;',
  20042. 'var',
  20043. ' o: TObject;',
  20044. ' v: IUnknown;',
  20045. 'begin',
  20046. ' v:=o.Ant;',
  20047. ' o.Ant:=v;',
  20048. ' o.Ant:=o.Ant;',
  20049. ' v:=o.Bird;',
  20050. ' o.Bird:=v;',
  20051. ' o.Bird:=o.Bird;',
  20052. ' v:=o.Items[1];',
  20053. ' o.Items[2]:=v;',
  20054. ' o.Items[3]:=o.Items[4];',
  20055. ' v:=o[5];',
  20056. ' o[6]:=v;',
  20057. ' o[7]:=o[8];',
  20058. 'end;',
  20059. 'begin',
  20060. '']);
  20061. ConvertProgram;
  20062. CheckSource('TestClassInterface_COM_Property',
  20063. LinesToStr([ // statements
  20064. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  20065. 'rtl.createClass(this, "TObject", null, function () {',
  20066. ' this.$init = function () {',
  20067. ' this.FAnt = null;',
  20068. ' };',
  20069. ' this.$final = function () {',
  20070. ' this.FAnt = undefined;',
  20071. ' };',
  20072. ' rtl.addIntf(this, $mod.IUnknown);',
  20073. '});',
  20074. 'this.DoIt = function () {',
  20075. ' var o = null;',
  20076. ' var v = null;',
  20077. ' var $ir = rtl.createIntfRefs();',
  20078. ' try {',
  20079. ' v = rtl.setIntfL(v, o.FAnt);',
  20080. ' rtl.setIntfP(o, "FAnt", v);',
  20081. ' rtl.setIntfP(o, "FAnt", o.FAnt);',
  20082. ' v = rtl.setIntfL(v, o.GetBird(), true);',
  20083. ' o.SetBird(v);',
  20084. ' o.SetBird($ir.ref(1, o.GetBird()));',
  20085. ' v = rtl.setIntfL(v, o.GetItems(1), true);',
  20086. ' o.SetItems(2, v);',
  20087. ' o.SetItems(3, $ir.ref(2, o.GetItems(4)));',
  20088. ' v = rtl.setIntfL(v, o.GetItems(5), true);',
  20089. ' o.SetItems(6, v);',
  20090. ' o.SetItems(7, $ir.ref(3, o.GetItems(8)));',
  20091. ' } finally {',
  20092. ' $ir.free();',
  20093. ' rtl._Release(v);',
  20094. ' };',
  20095. '};',
  20096. '']),
  20097. LinesToStr([ // $mod.$main
  20098. '']));
  20099. end;
  20100. procedure TTestModule.TestClassInterface_COM_IntfProperty;
  20101. begin
  20102. StartProgram(false);
  20103. Add([
  20104. '{$interfaces com}',
  20105. 'type',
  20106. ' IUnknown = interface',
  20107. ' function _AddRef: longint;',
  20108. ' function _Release: longint;',
  20109. ' function GetBird: IUnknown;',
  20110. ' procedure SetBird(Value: IUnknown);',
  20111. ' function GetItems(Index: longint): IUnknown;',
  20112. ' procedure SetItems(Index: longint; Value: IUnknown);',
  20113. ' property Bird: IUnknown read GetBird write SetBird;',
  20114. ' property Items[Index: longint]: IUnknown read GetItems write SetItems; default;',
  20115. ' end;',
  20116. ' TObject = class(IUnknown)',
  20117. ' function _AddRef: longint; virtual; abstract;',
  20118. ' function _Release: longint; virtual; abstract;',
  20119. ' function GetBird: IUnknown; virtual; abstract;',
  20120. ' procedure SetBird(Value: IUnknown); virtual; abstract;',
  20121. ' function GetItems(Index: longint): IUnknown; virtual; abstract;',
  20122. ' procedure SetItems(Index: longint; Value: IUnknown); virtual; abstract;',
  20123. ' end;',
  20124. 'procedure DoIt;',
  20125. 'var',
  20126. ' o: TObject;',
  20127. ' v: IUnknown;',
  20128. 'begin',
  20129. ' v:=v.Items[1];',
  20130. ' v.Items[2]:=v;',
  20131. ' v.Items[3]:=v.Items[4];',
  20132. ' v:=v[5];',
  20133. ' v[6]:=v;',
  20134. ' v[7]:=v[8];',
  20135. ' v[9].Bird.Bird:=v;',
  20136. ' v:=v.Bird[10].Bird',
  20137. 'end;',
  20138. 'begin',
  20139. '']);
  20140. ConvertProgram;
  20141. CheckSource('TestClassInterface_COM_IntfProperty',
  20142. LinesToStr([ // statements
  20143. 'rtl.createInterface(this, "IUnknown", "{385F5482-571B-338C-8130-4E97F330543B}", [',
  20144. ' "_AddRef",',
  20145. ' "_Release",',
  20146. ' "GetBird",',
  20147. ' "SetBird",',
  20148. ' "GetItems",',
  20149. ' "SetItems"',
  20150. '], null);',
  20151. 'rtl.createClass(this, "TObject", null, function () {',
  20152. ' this.$init = function () {',
  20153. ' };',
  20154. ' this.$final = function () {',
  20155. ' };',
  20156. ' rtl.addIntf(this, $mod.IUnknown);',
  20157. '});',
  20158. 'this.DoIt = function () {',
  20159. ' var o = null;',
  20160. ' var v = null;',
  20161. ' var $ir = rtl.createIntfRefs();',
  20162. ' try {',
  20163. ' v = rtl.setIntfL(v, v.GetItems(1), true);',
  20164. ' v.SetItems(2, v);',
  20165. ' v.SetItems(3, $ir.ref(1, v.GetItems(4)));',
  20166. ' v = rtl.setIntfL(v, v.GetItems(5), true);',
  20167. ' v.SetItems(6, v);',
  20168. ' v.SetItems(7, $ir.ref(2, v.GetItems(8)));',
  20169. ' $ir.ref(4, $ir.ref(3, v.GetItems(9)).GetBird()).SetBird(v);',
  20170. ' v = rtl.setIntfL(v, $ir.ref(6, $ir.ref(5, v.GetBird()).GetItems(10)).GetBird(), true);',
  20171. ' } finally {',
  20172. ' $ir.free();',
  20173. ' rtl._Release(v);',
  20174. ' };',
  20175. '};',
  20176. '']),
  20177. LinesToStr([ // $mod.$main
  20178. '']));
  20179. end;
  20180. procedure TTestModule.TestClassInterface_COM_Delegation;
  20181. begin
  20182. StartProgram(false);
  20183. Add([
  20184. '{$interfaces com}',
  20185. 'type',
  20186. ' IUnknown = interface',
  20187. ' function _AddRef: longint;',
  20188. ' function _Release: longint;',
  20189. ' end;',
  20190. ' IBird = interface(IUnknown)',
  20191. ' procedure Fly(s: string);',
  20192. ' end;',
  20193. ' IEagle = interface(IBird) end;',
  20194. ' IDove = interface(IBird) end;',
  20195. ' ISwallow = interface(IBird) end;',
  20196. ' TObject = class',
  20197. ' end;',
  20198. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  20199. ' function _AddRef: longint; virtual; abstract;',
  20200. ' function _Release: longint; virtual; abstract;',
  20201. ' procedure Fly(s: string); virtual; abstract;',
  20202. ' end;',
  20203. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  20204. ' function _AddRef: longint; virtual; abstract;',
  20205. ' function _Release: longint; virtual; abstract;',
  20206. ' FBirdIntf: IBird;',
  20207. ' property BirdIntf: IBird read FBirdIntf implements IBird;',
  20208. ' function GetEagleIntf: IEagle; virtual; abstract;',
  20209. ' property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  20210. ' FDoveObj: TBird;',
  20211. ' property DoveObj: TBird read FDoveObj implements IDove;',
  20212. ' function GetSwallowObj: TBird; virtual; abstract;',
  20213. ' property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  20214. ' end;',
  20215. 'begin',
  20216. '']);
  20217. ConvertProgram;
  20218. CheckSource('TestClassInterface_COM_Delegation',
  20219. LinesToStr([ // statements
  20220. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  20221. 'rtl.createInterface(this, "IBird", "{CC440C7F-7623-3DEE-AE88-000B86AAF108}", ["Fly"], this.IUnknown);',
  20222. 'rtl.createInterface(this, "IEagle", "{4B6A41C9-B020-3D7C-B688-96D19022B1B4}", [], this.IBird);',
  20223. 'rtl.createInterface(this, "IDove", "{4B6A41C9-B020-3D7C-B688-96D18EF16074}", [], this.IBird);',
  20224. 'rtl.createInterface(this, "ISwallow", "{BB6A41C9-B020-3D7C-B688-96D1CBDCB359}", [], this.IBird);',
  20225. 'rtl.createClass(this, "TObject", null, function () {',
  20226. ' this.$init = function () {',
  20227. ' };',
  20228. ' this.$final = function () {',
  20229. ' };',
  20230. '});',
  20231. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20232. ' rtl.addIntf(this, $mod.IBird);',
  20233. ' rtl.addIntf(this, $mod.IEagle);',
  20234. ' rtl.addIntf(this, $mod.IDove);',
  20235. ' rtl.addIntf(this, $mod.ISwallow);',
  20236. '});',
  20237. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  20238. ' this.$init = function () {',
  20239. ' $mod.TObject.$init.call(this);',
  20240. ' this.FBirdIntf = null;',
  20241. ' this.FDoveObj = null;',
  20242. ' };',
  20243. ' this.$final = function () {',
  20244. ' this.FBirdIntf = undefined;',
  20245. ' this.FDoveObj = undefined;',
  20246. ' $mod.TObject.$final.call(this);',
  20247. ' };',
  20248. ' this.$intfmaps = {',
  20249. ' "{CC440C7F-7623-3DEE-AE88-000B86AAF108}": function () {',
  20250. ' return rtl._AddRef(this.FBirdIntf);',
  20251. ' },',
  20252. ' "{4B6A41C9-B020-3D7C-B688-96D19022B1B4}": function () {',
  20253. ' return this.GetEagleIntf();',
  20254. ' },',
  20255. ' "{4B6A41C9-B020-3D7C-B688-96D18EF16074}": function () {',
  20256. ' return rtl.queryIntfT(this.FDoveObj, $mod.IDove);',
  20257. ' },',
  20258. ' "{BB6A41C9-B020-3D7C-B688-96D1CBDCB359}": function () {',
  20259. ' return rtl.queryIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  20260. ' }',
  20261. ' };',
  20262. '});',
  20263. '']),
  20264. LinesToStr([ // $mod.$main
  20265. '']));
  20266. end;
  20267. procedure TTestModule.TestClassInterface_COM_With;
  20268. begin
  20269. StartProgram(false);
  20270. Add([
  20271. '{$interfaces com}',
  20272. 'type',
  20273. ' IUnknown = interface',
  20274. ' function _AddRef: longint;',
  20275. ' function _Release: longint;',
  20276. ' function GetAnt: IUnknown;',
  20277. ' property Ant: IUnknown read GetAnt;',
  20278. ' end;',
  20279. ' TObject = class(IUnknown)',
  20280. ' function _AddRef: longint; virtual; abstract;',
  20281. ' function _Release: longint; virtual; abstract;',
  20282. ' function GetAnt: IUnknown; virtual; abstract;',
  20283. ' property Ant: IUnknown read GetAnt;',
  20284. ' end;',
  20285. 'procedure DoIt;',
  20286. 'var',
  20287. ' i: IUnknown;',
  20288. 'begin',
  20289. ' with i do ',
  20290. ' GetAnt;',
  20291. ' with i.Ant, Ant do ',
  20292. ' GetAnt;',
  20293. 'end;',
  20294. 'begin',
  20295. '']);
  20296. ConvertProgram;
  20297. CheckSource('TestClassInterface_COM_With',
  20298. LinesToStr([ // statements
  20299. 'rtl.createInterface(this, "IUnknown", "{D7ADB00D-C6B6-39FB-BDDF-21CD521DDFA9}", ["_AddRef", "_Release", "GetAnt"], null);',
  20300. 'rtl.createClass(this, "TObject", null, function () {',
  20301. ' this.$init = function () {',
  20302. ' };',
  20303. ' this.$final = function () {',
  20304. ' };',
  20305. ' rtl.addIntf(this, $mod.IUnknown);',
  20306. '});',
  20307. 'this.DoIt = function () {',
  20308. ' var i = null;',
  20309. ' var $ir = rtl.createIntfRefs();',
  20310. ' try {',
  20311. ' $ir.ref(1, i.GetAnt());',
  20312. ' var $with = $ir.ref(2, i.GetAnt());',
  20313. ' var $with1 = $ir.ref(3, $with.GetAnt());',
  20314. ' $ir.ref(4, $with1.GetAnt());',
  20315. ' } finally {',
  20316. ' $ir.free();',
  20317. ' };',
  20318. '};',
  20319. '']),
  20320. LinesToStr([ // $mod.$main
  20321. '']));
  20322. end;
  20323. procedure TTestModule.TestClassInterface_COM_ForIn;
  20324. begin
  20325. StartProgram(false);
  20326. Add([
  20327. '{$interfaces com}',
  20328. 'type',
  20329. ' IUnknown = interface end;',
  20330. ' TObject = class',
  20331. ' Id: longint;',
  20332. ' end;',
  20333. ' IEnumerator = interface(IUnknown)',
  20334. ' function GetCurrent: TObject;',
  20335. ' function MoveNext: Boolean;',
  20336. ' property Current: TObject read GetCurrent;',
  20337. ' end;',
  20338. ' IEnumerable = interface(IUnknown)',
  20339. ' function GetEnumerator: IEnumerator;',
  20340. ' end;',
  20341. 'var',
  20342. ' o: TObject;',
  20343. ' i: IEnumerable;',
  20344. 'begin',
  20345. ' for o in i do o.Id:=3;',
  20346. '']);
  20347. ConvertProgram;
  20348. CheckSource('TestClassInterface_COM_ForIn',
  20349. LinesToStr([ // statements
  20350. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  20351. 'rtl.createClass(this, "TObject", null, function () {',
  20352. ' this.$init = function () {',
  20353. ' this.Id = 0;',
  20354. ' };',
  20355. ' this.$final = function () {',
  20356. ' };',
  20357. '});',
  20358. 'rtl.createInterface(this, "IEnumerator", "{95D7745D-ED61-3F13-BBE4-07708161999E}", ["GetCurrent", "MoveNext"], this.IUnknown);',
  20359. 'rtl.createInterface(this, "IEnumerable", "{8CC9D45D-ED7D-3B73-96B6-290B931BB19E}", ["GetEnumerator"], this.IUnknown);',
  20360. 'this.o = null;',
  20361. 'this.i = null;',
  20362. '']),
  20363. LinesToStr([ // $mod.$main
  20364. 'var $in = $mod.i.GetEnumerator();',
  20365. 'try {',
  20366. ' while ($in.MoveNext()) {',
  20367. ' $mod.o = $in.GetCurrent();',
  20368. ' $mod.o.Id = 3;',
  20369. ' }',
  20370. '} finally {',
  20371. ' rtl._Release($in)',
  20372. '};',
  20373. '']));
  20374. end;
  20375. procedure TTestModule.TestClassInterface_COM_ArrayOfIntfFail;
  20376. begin
  20377. StartProgram(false);
  20378. Add([
  20379. '{$interfaces com}',
  20380. 'type',
  20381. ' IUnknown = interface',
  20382. ' function _AddRef: longint;',
  20383. ' function _Release: longint;',
  20384. ' end;',
  20385. ' TObject = class',
  20386. ' end;',
  20387. ' TArrOfIntf = array of IUnknown;',
  20388. 'begin',
  20389. '']);
  20390. SetExpectedPasResolverError('Not supported: array of COM-interface',nNotSupportedX);
  20391. ConvertProgram;
  20392. end;
  20393. procedure TTestModule.TestClassInterface_COM_RecordIntfFail;
  20394. begin
  20395. StartProgram(false);
  20396. Add([
  20397. '{$interfaces com}',
  20398. 'type',
  20399. ' IUnknown = interface',
  20400. ' function _AddRef: longint;',
  20401. ' function _Release: longint;',
  20402. ' end;',
  20403. ' TRec = record',
  20404. ' i: IUnknown;',
  20405. ' end;',
  20406. 'begin',
  20407. '']);
  20408. SetExpectedPasResolverError('Not supported: COM-interface as record member',nNotSupportedX);
  20409. ConvertProgram;
  20410. end;
  20411. procedure TTestModule.TestClassInterface_COM_UnitInitialization;
  20412. begin
  20413. StartUnit(false);
  20414. Add([
  20415. '{$interfaces com}',
  20416. 'interface',
  20417. 'implementation',
  20418. 'type',
  20419. ' IUnknown = interface',
  20420. ' function _AddRef: longint;',
  20421. ' end;',
  20422. ' TObject = class(IUnknown)',
  20423. ' function _AddRef: longint;',
  20424. ' end;',
  20425. 'function TObject._AddRef: longint; begin end;',
  20426. 'var i: IUnknown;',
  20427. ' o: TObject;',
  20428. 'initialization',
  20429. ' i:=nil;',
  20430. ' i:=i;',
  20431. ' i:=o;',
  20432. ' if (o as IUnknown)=nil then ;',
  20433. '']);
  20434. ConvertUnit;
  20435. CheckSource('TestClassInterface_COM_UnitInitialization',
  20436. LinesToStr([ // statements
  20437. 'var $impl = $mod.$impl;',
  20438. '']),
  20439. LinesToStr([ // this.$init
  20440. 'var $ir = rtl.createIntfRefs();',
  20441. 'try {',
  20442. ' rtl.setIntfP($impl, "i", null);',
  20443. ' rtl.setIntfP($impl, "i", $impl.i);',
  20444. ' rtl.setIntfP($impl, "i", rtl.queryIntfT($impl.o, $impl.IUnknown), true);',
  20445. ' if ($ir.ref(1, rtl.queryIntfT($impl.o, $impl.IUnknown)) === null) ;',
  20446. '} finally {',
  20447. ' $ir.free();',
  20448. '};',
  20449. '']),
  20450. LinesToStr([ // implementation
  20451. 'rtl.createInterface($impl, "IUnknown", "{B92D5841-758A-322B-BDDF-21CD52180000}", ["_AddRef"], null);',
  20452. 'rtl.createClass($impl, "TObject", null, function () {',
  20453. ' this.$init = function () {',
  20454. ' };',
  20455. ' this.$final = function () {',
  20456. ' };',
  20457. ' this._AddRef = function () {',
  20458. ' var Result = 0;',
  20459. ' return Result;',
  20460. ' };',
  20461. ' rtl.addIntf(this, $impl.IUnknown);',
  20462. '});',
  20463. '$impl.i = null;',
  20464. '$impl.o = null;',
  20465. ''])
  20466. );
  20467. end;
  20468. procedure TTestModule.TestClassInterface_GUID;
  20469. begin
  20470. StartProgram(false);
  20471. Add([
  20472. '{$interfaces corba}',
  20473. 'type',
  20474. ' IUnknown = interface',
  20475. ' [''{f31db68f-3010-D355-4EBA-CDD4EF4A737C}'']',
  20476. ' end;',
  20477. ' TObject = class end;',
  20478. ' TGUID = record D1, D2, D3, D4: word; end;',
  20479. ' TAliasGUID = TGUID;',
  20480. ' TGUIDString = type string;',
  20481. ' TAliasGUIDString = TGUIDString;',
  20482. 'procedure DoConstGUIDIt(const g: TAliasGUID); overload;',
  20483. 'begin end;',
  20484. 'procedure DoDefGUID(g: TAliasGUID); overload;',
  20485. 'begin end;',
  20486. 'procedure DoStr(const s: TAliasGUIDString); overload;',
  20487. 'begin end;',
  20488. 'var',
  20489. ' i: IUnknown;',
  20490. ' g: TAliasGUID = ''{d91c9af4-3C93-420F-A303-BF5BA82BFD23}'';',
  20491. ' s: TAliasGUIDString;',
  20492. 'begin',
  20493. ' DoConstGUIDIt(IUnknown);',
  20494. ' DoDefGUID(IUnknown);',
  20495. ' DoStr(IUnknown);',
  20496. ' DoConstGUIDIt(i);',
  20497. ' DoDefGUID(i);',
  20498. ' DoStr(i);',
  20499. ' DoConstGUIDIt(''{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}'');',
  20500. ' DoDefGUID(''{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}'');',
  20501. ' DoStr(g);',
  20502. ' g:=i;',
  20503. ' g:=IUnknown;',
  20504. ' g:=''{D91C9AF4-3C93-420F-A303-bf5ba82bfd23}'';',
  20505. ' s:=i;',
  20506. ' s:=IUnknown;',
  20507. ' s:=g;',
  20508. ' if g=i then ;',
  20509. ' if i=g then ;',
  20510. ' if g=IUnknown then ;',
  20511. ' if IUnknown=g then ;',
  20512. ' if s=i then ;',
  20513. ' if i=s then ;',
  20514. ' if s=IUnknown then ;',
  20515. ' if IUnknown=s then ;',
  20516. ' if s=g then ;',
  20517. ' if g=s then ;',
  20518. '']);
  20519. ConvertProgram;
  20520. CheckSource('TestClassInterface_GUID',
  20521. LinesToStr([ // statements
  20522. 'rtl.createInterface(this, "IUnknown", "{F31DB68F-3010-D355-4EBA-CDD4EF4A737C}", [], null);',
  20523. 'rtl.createClass(this, "TObject", null, function () {',
  20524. ' this.$init = function () {',
  20525. ' };',
  20526. ' this.$final = function () {',
  20527. ' };',
  20528. '});',
  20529. 'rtl.recNewT(this, "TGUID", function () {',
  20530. ' this.D1 = 0;',
  20531. ' this.D2 = 0;',
  20532. ' this.D3 = 0;',
  20533. ' this.D4 = 0;',
  20534. ' this.$eq = function (b) {',
  20535. ' return (this.D1 === b.D1) && (this.D2 === b.D2) && (this.D3 === b.D3) && (this.D4 === b.D4);',
  20536. ' };',
  20537. ' this.$assign = function (s) {',
  20538. ' this.D1 = s.D1;',
  20539. ' this.D2 = s.D2;',
  20540. ' this.D3 = s.D3;',
  20541. ' this.D4 = s.D4;',
  20542. ' return this;',
  20543. ' };',
  20544. '});',
  20545. 'this.DoConstGUIDIt = function (g) {',
  20546. '};',
  20547. 'this.DoDefGUID = function (g) {',
  20548. '};',
  20549. 'this.DoStr = function (s) {',
  20550. '};',
  20551. 'this.i = null;',
  20552. 'this.g = this.TGUID.$clone({',
  20553. ' D1: 0xD91C9AF4,',
  20554. ' D2: 0x3C93,',
  20555. ' D3: 0x420F,',
  20556. ' D4: [',
  20557. ' 0xA3,',
  20558. ' 0x03,',
  20559. ' 0xBF,',
  20560. ' 0x5B,',
  20561. ' 0xA8,',
  20562. ' 0x2B,',
  20563. ' 0xFD,',
  20564. ' 0x23',
  20565. ' ]',
  20566. '});',
  20567. 'this.s = "";',
  20568. '']),
  20569. LinesToStr([ // $mod.$main
  20570. '$mod.DoConstGUIDIt(rtl.getIntfGUIDR($mod.IUnknown));',
  20571. '$mod.DoDefGUID($mod.TGUID.$clone(rtl.getIntfGUIDR($mod.IUnknown)));',
  20572. '$mod.DoStr($mod.IUnknown.$guid);',
  20573. '$mod.DoConstGUIDIt(rtl.getIntfGUIDR($mod.i));',
  20574. '$mod.DoDefGUID($mod.TGUID.$clone(rtl.getIntfGUIDR($mod.i)));',
  20575. '$mod.DoStr($mod.i.$guid);',
  20576. '$mod.DoConstGUIDIt(rtl.strToGUIDR("{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}"));',
  20577. '$mod.DoDefGUID(rtl.strToGUIDR("{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}"));',
  20578. '$mod.DoStr(rtl.guidrToStr($mod.g));',
  20579. '$mod.g.$assign(rtl.getIntfGUIDR($mod.i));',
  20580. '$mod.g.$assign(rtl.getIntfGUIDR($mod.IUnknown));',
  20581. '$mod.g.$assign({',
  20582. ' D1: 0xD91C9AF4,',
  20583. ' D2: 0x3C93,',
  20584. ' D3: 0x420F,',
  20585. ' D4: [',
  20586. ' 0xA3,',
  20587. ' 0x03,',
  20588. ' 0xBF,',
  20589. ' 0x5B,',
  20590. ' 0xA8,',
  20591. ' 0x2B,',
  20592. ' 0xFD,',
  20593. ' 0x23',
  20594. ' ]',
  20595. '});',
  20596. '$mod.s = $mod.i.$guid;',
  20597. '$mod.s = $mod.IUnknown.$guid;',
  20598. '$mod.s = rtl.guidrToStr($mod.g);',
  20599. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.i))) ;',
  20600. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.i))) ;',
  20601. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.IUnknown))) ;',
  20602. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.IUnknown))) ;',
  20603. 'if ($mod.s === $mod.i.$guid) ;',
  20604. 'if ($mod.i.$guid === $mod.s) ;',
  20605. 'if ($mod.s === $mod.IUnknown.$guid) ;',
  20606. 'if ($mod.IUnknown.$guid === $mod.s) ;',
  20607. 'if ($mod.g.$eq(rtl.createTGUID($mod.s))) ;',
  20608. 'if ($mod.g.$eq(rtl.createTGUID($mod.s))) ;',
  20609. '']));
  20610. end;
  20611. procedure TTestModule.TestClassInterface_GUIDProperty;
  20612. begin
  20613. StartProgram(false);
  20614. Add([
  20615. '{$interfaces corba}',
  20616. 'type',
  20617. ' IUnknown = interface',
  20618. ' [''{f31db68f-3010-D355-4EBA-CDD4EF4A737C}'']',
  20619. ' end;',
  20620. ' TGUID = record D1, D2, D3, D4: word; end;',
  20621. ' TAliasGUID = TGUID;',
  20622. ' TGUIDString = type string;',
  20623. ' TAliasGUIDString = TGUIDString;',
  20624. ' TObject = class',
  20625. ' function GetG: TAliasGUID; virtual; abstract;',
  20626. ' procedure SetG(const Value: TAliasGUID); virtual; abstract;',
  20627. ' function GetS: TAliasGUIDString; virtual; abstract;',
  20628. ' procedure SetS(const Value: TAliasGUIDString); virtual; abstract;',
  20629. ' property g: TAliasGUID read GetG write SetG;',
  20630. ' property s: TAliasGUIDString read GetS write SetS;',
  20631. ' end;',
  20632. 'var o: TObject;',
  20633. 'begin',
  20634. ' o.g:=IUnknown;',
  20635. ' o.g:=''{D91C9AF4-3C93-420F-A303-bf5ba82bfd23}'';',
  20636. ' o.s:=IUnknown;',
  20637. ' o.s:=o.g;',
  20638. '']);
  20639. ConvertProgram;
  20640. CheckSource('TestClassInterface_GUIDProperty',
  20641. LinesToStr([ // statements
  20642. 'rtl.createInterface(this, "IUnknown", "{F31DB68F-3010-D355-4EBA-CDD4EF4A737C}", [], null);',
  20643. 'rtl.recNewT(this, "TGUID", function () {',
  20644. ' this.D1 = 0;',
  20645. ' this.D2 = 0;',
  20646. ' this.D3 = 0;',
  20647. ' this.D4 = 0;',
  20648. ' this.$eq = function (b) {',
  20649. ' return (this.D1 === b.D1) && (this.D2 === b.D2) && (this.D3 === b.D3) && (this.D4 === b.D4);',
  20650. ' };',
  20651. ' this.$assign = function (s) {',
  20652. ' this.D1 = s.D1;',
  20653. ' this.D2 = s.D2;',
  20654. ' this.D3 = s.D3;',
  20655. ' this.D4 = s.D4;',
  20656. ' return this;',
  20657. ' };',
  20658. '});',
  20659. 'rtl.createClass(this, "TObject", null, function () {',
  20660. ' this.$init = function () {',
  20661. ' };',
  20662. ' this.$final = function () {',
  20663. ' };',
  20664. '});',
  20665. 'this.o = null;',
  20666. '']),
  20667. LinesToStr([ // $mod.$main
  20668. '$mod.o.SetG(rtl.getIntfGUIDR($mod.IUnknown));',
  20669. '$mod.o.SetG({',
  20670. ' D1: 0xD91C9AF4,',
  20671. ' D2: 0x3C93,',
  20672. ' D3: 0x420F,',
  20673. ' D4: [',
  20674. ' 0xA3,',
  20675. ' 0x03,',
  20676. ' 0xBF,',
  20677. ' 0x5B,',
  20678. ' 0xA8,',
  20679. ' 0x2B,',
  20680. ' 0xFD,',
  20681. ' 0x23',
  20682. ' ]',
  20683. '});',
  20684. '$mod.o.SetS($mod.IUnknown.$guid);',
  20685. '$mod.o.SetS(rtl.guidrToStr($mod.o.GetG()));',
  20686. '']));
  20687. end;
  20688. procedure TTestModule.TestClassHelper_ClassVar;
  20689. begin
  20690. StartProgram(false);
  20691. Add([
  20692. 'type',
  20693. ' TObject = class',
  20694. ' end;',
  20695. ' THelper = class helper for TObject',
  20696. ' const',
  20697. ' One = 1;',
  20698. ' Two: word = 2;',
  20699. ' class var',
  20700. ' Glob: word;',
  20701. ' function Foo(w: word): word;',
  20702. ' class function Bar(w: word): word;',
  20703. ' end;',
  20704. 'function THelper.foo(w: word): word;',
  20705. 'begin',
  20706. ' Result:=w;',
  20707. ' Two:=One+w;',
  20708. ' Glob:=Glob;',
  20709. ' Result:=Self.Glob;',
  20710. ' Self.Glob:=Self.Glob;',
  20711. ' with Self do Glob:=Glob;',
  20712. 'end;',
  20713. 'class function THelper.bar(w: word): word;',
  20714. 'begin',
  20715. ' Result:=w;',
  20716. ' Two:=One;',
  20717. ' Glob:=Glob;',
  20718. ' Self.Glob:=Self.Glob;',
  20719. ' with Self do Glob:=Glob;',
  20720. 'end;',
  20721. 'var o: TObject;',
  20722. 'begin',
  20723. ' tobject.two:=tobject.one;',
  20724. ' tobject.Glob:=tobject.Glob;',
  20725. ' with tobject do begin',
  20726. ' two:=one;',
  20727. ' Glob:=Glob;',
  20728. ' end;',
  20729. ' o.two:=o.one;',
  20730. ' o.Glob:=o.Glob;',
  20731. ' with o do begin',
  20732. ' two:=one;',
  20733. ' Glob:=Glob;',
  20734. ' end;',
  20735. '']);
  20736. ConvertProgram;
  20737. CheckSource('TestClassHelper_ClassVar',
  20738. LinesToStr([ // statements
  20739. 'rtl.createClass(this, "TObject", null, function () {',
  20740. ' this.$init = function () {',
  20741. ' };',
  20742. ' this.$final = function () {',
  20743. ' };',
  20744. '});',
  20745. 'rtl.createHelper(this, "THelper", null, function () {',
  20746. ' this.One = 1;',
  20747. ' this.Two = 2;',
  20748. ' this.Glob = 0;',
  20749. ' this.Foo = function (w) {',
  20750. ' var Result = 0;',
  20751. ' Result = w;',
  20752. ' $mod.THelper.Two = 1 + w;',
  20753. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20754. ' Result = $mod.THelper.Glob;',
  20755. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20756. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20757. ' return Result;',
  20758. ' };',
  20759. ' this.Bar = function (w) {',
  20760. ' var Result = 0;',
  20761. ' Result = w;',
  20762. ' $mod.THelper.Two = 1;',
  20763. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20764. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20765. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20766. ' return Result;',
  20767. ' };',
  20768. '});',
  20769. 'this.o = null;',
  20770. '']),
  20771. LinesToStr([ // $mod.$main
  20772. '$mod.THelper.Two = 1;',
  20773. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20774. 'var $with = $mod.TObject;',
  20775. '$mod.THelper.Two = 1;',
  20776. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20777. '$mod.THelper.Two = 1;',
  20778. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20779. 'var $with1 = $mod.o;',
  20780. '$mod.THelper.Two = 1;',
  20781. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20782. '']));
  20783. end;
  20784. procedure TTestModule.TestClassHelper_Method_AccessInstanceFields;
  20785. begin
  20786. StartProgram(false);
  20787. Add([
  20788. 'type',
  20789. ' TObject = class',
  20790. ' FSize: word;',
  20791. ' property Size: word read FSize write FSize;',
  20792. ' end;',
  20793. ' THelper = class helper for TObject',
  20794. ' function Foo(w: word = 1): word;',
  20795. ' end;',
  20796. 'function THelper.foo(w: word): word;',
  20797. 'begin',
  20798. ' Result:=Size;',
  20799. ' Size:=Size+2;',
  20800. ' Self.Size:=Self.Size+3;',
  20801. ' FSize:=FSize+4;',
  20802. ' Self.FSize:=Self.FSize+5;',
  20803. ' with Self do begin',
  20804. ' Size:=Size+6;',
  20805. ' FSize:=FSize+7;',
  20806. ' FSize:=FSize+8;',
  20807. ' end;',
  20808. 'end;',
  20809. 'begin',
  20810. '']);
  20811. ConvertProgram;
  20812. CheckSource('TestClassHelper_Method_AccessInstanceFields',
  20813. LinesToStr([ // statements
  20814. 'rtl.createClass(this, "TObject", null, function () {',
  20815. ' this.$init = function () {',
  20816. ' this.FSize = 0;',
  20817. ' };',
  20818. ' this.$final = function () {',
  20819. ' };',
  20820. '});',
  20821. 'rtl.createHelper(this, "THelper", null, function () {',
  20822. ' this.Foo = function (w) {',
  20823. ' var Result = 0;',
  20824. ' Result = this.FSize;',
  20825. ' this.FSize = this.FSize + 2;',
  20826. ' this.FSize = this.FSize + 3;',
  20827. ' this.FSize = this.FSize + 4;',
  20828. ' this.FSize = this.FSize + 5;',
  20829. ' this.FSize = this.FSize + 6;',
  20830. ' this.FSize = this.FSize + 7;',
  20831. ' this.FSize = this.FSize + 8;',
  20832. ' return Result;',
  20833. ' };',
  20834. '});',
  20835. '']),
  20836. LinesToStr([ // $mod.$main
  20837. '']));
  20838. end;
  20839. procedure TTestModule.TestClassHelper_Method_Call;
  20840. begin
  20841. StartProgram(false);
  20842. Add([
  20843. 'type',
  20844. ' TObject = class',
  20845. ' procedure Run(w: word = 10);',
  20846. ' end;',
  20847. ' THelper = class helper for TObject',
  20848. ' function Foo(w: word = 1): word;',
  20849. ' end;',
  20850. 'procedure TObject.Run(w: word);',
  20851. 'var o: TObject;',
  20852. 'begin',
  20853. ' Foo;',
  20854. ' Foo();',
  20855. ' Foo(2);',
  20856. ' Self.Foo;',
  20857. ' Self.Foo();',
  20858. ' Self.Foo(3);',
  20859. ' with Self do begin',
  20860. ' Foo;',
  20861. ' Foo();',
  20862. ' Foo(4);',
  20863. ' end;',
  20864. ' with o do Foo(5);',
  20865. 'end;',
  20866. 'function THelper.foo(w: word): word;',
  20867. 'begin',
  20868. ' Run;',
  20869. ' Run();',
  20870. ' Run(11);',
  20871. ' Foo;',
  20872. ' Foo();',
  20873. ' Foo(12);',
  20874. ' Self.Foo;',
  20875. ' Self.Foo();',
  20876. ' Self.Foo(13);',
  20877. ' with Self do begin',
  20878. ' Foo;',
  20879. ' Foo();',
  20880. ' Foo(14);',
  20881. ' end;',
  20882. 'end;',
  20883. 'var Obj: TObject;',
  20884. 'begin',
  20885. ' obj.Foo;',
  20886. ' obj.Foo();',
  20887. ' obj.Foo(21);',
  20888. ' with obj do begin',
  20889. ' Foo;',
  20890. ' Foo();',
  20891. ' Foo(22);',
  20892. ' end;',
  20893. '']);
  20894. ConvertProgram;
  20895. CheckSource('TestClassHelper_Method_Call',
  20896. LinesToStr([ // statements
  20897. 'rtl.createClass(this, "TObject", null, function () {',
  20898. ' this.$init = function () {',
  20899. ' };',
  20900. ' this.$final = function () {',
  20901. ' };',
  20902. ' this.Run = function (w) {',
  20903. ' var o = null;',
  20904. ' $mod.THelper.Foo.call(this, 1);',
  20905. ' $mod.THelper.Foo.call(this, 1);',
  20906. ' $mod.THelper.Foo.call(this, 2);',
  20907. ' $mod.THelper.Foo.call(this, 1);',
  20908. ' $mod.THelper.Foo.call(this, 1);',
  20909. ' $mod.THelper.Foo.call(this, 3);',
  20910. ' $mod.THelper.Foo.call(this, 1);',
  20911. ' $mod.THelper.Foo.call(this, 1);',
  20912. ' $mod.THelper.Foo.call(this, 4);',
  20913. ' $mod.THelper.Foo.call(o, 5);',
  20914. ' };',
  20915. '});',
  20916. 'rtl.createHelper(this, "THelper", null, function () {',
  20917. ' this.Foo = function (w) {',
  20918. ' var Result = 0;',
  20919. ' this.Run(10);',
  20920. ' this.Run(10);',
  20921. ' this.Run(11);',
  20922. ' $mod.THelper.Foo.call(this, 1);',
  20923. ' $mod.THelper.Foo.call(this, 1);',
  20924. ' $mod.THelper.Foo.call(this, 12);',
  20925. ' $mod.THelper.Foo.call(this, 1);',
  20926. ' $mod.THelper.Foo.call(this, 1);',
  20927. ' $mod.THelper.Foo.call(this, 13);',
  20928. ' $mod.THelper.Foo.call(this, 1);',
  20929. ' $mod.THelper.Foo.call(this, 1);',
  20930. ' $mod.THelper.Foo.call(this, 14);',
  20931. ' return Result;',
  20932. ' };',
  20933. '});',
  20934. 'this.Obj = null;',
  20935. '']),
  20936. LinesToStr([ // $mod.$main
  20937. '$mod.THelper.Foo.call($mod.Obj, 1);',
  20938. '$mod.THelper.Foo.call($mod.Obj, 1);',
  20939. '$mod.THelper.Foo.call($mod.Obj, 21);',
  20940. 'var $with = $mod.Obj;',
  20941. '$mod.THelper.Foo.call($with, 1);',
  20942. '$mod.THelper.Foo.call($with, 1);',
  20943. '$mod.THelper.Foo.call($with, 22);',
  20944. '']));
  20945. end;
  20946. procedure TTestModule.TestClassHelper_Method_Nested_Call;
  20947. begin
  20948. StartProgram(false);
  20949. Add([
  20950. 'type',
  20951. ' TObject = class',
  20952. ' procedure Run(w: word = 10);',
  20953. ' end;',
  20954. ' THelper = class helper for TObject',
  20955. ' function Foo(w: word = 1): word;',
  20956. ' end;',
  20957. 'procedure TObject.Run(w: word);',
  20958. ' procedure Sub(Self: TObject);',
  20959. ' begin',
  20960. ' Foo;',
  20961. ' Foo();',
  20962. ' Self.Foo;',
  20963. ' Self.Foo();',
  20964. ' with Self do begin',
  20965. ' Foo;',
  20966. ' Foo();',
  20967. ' end;',
  20968. ' end;',
  20969. 'begin',
  20970. 'end;',
  20971. 'function THelper.foo(w: word): word;',
  20972. ' procedure Sub(Self: TObject);',
  20973. ' begin',
  20974. ' Run;',
  20975. ' Run();',
  20976. ' Foo;',
  20977. ' Foo();',
  20978. ' Self.Foo;',
  20979. ' Self.Foo();',
  20980. ' with Self do begin',
  20981. ' Foo;',
  20982. ' Foo();',
  20983. ' end;',
  20984. ' end;',
  20985. 'begin',
  20986. 'end;',
  20987. 'begin',
  20988. '']);
  20989. ConvertProgram;
  20990. CheckSource('TestClassHelper_Method_Nested_Call',
  20991. LinesToStr([ // statements
  20992. 'rtl.createClass(this, "TObject", null, function () {',
  20993. ' this.$init = function () {',
  20994. ' };',
  20995. ' this.$final = function () {',
  20996. ' };',
  20997. ' this.Run = function (w) {',
  20998. ' var $Self = this;',
  20999. ' function Sub(Self) {',
  21000. ' $mod.THelper.Foo.call($Self, 1);',
  21001. ' $mod.THelper.Foo.call($Self, 1);',
  21002. ' $mod.THelper.Foo.call(Self, 1);',
  21003. ' $mod.THelper.Foo.call(Self, 1);',
  21004. ' $mod.THelper.Foo.call(Self, 1);',
  21005. ' $mod.THelper.Foo.call(Self, 1);',
  21006. ' };',
  21007. ' };',
  21008. '});',
  21009. 'rtl.createHelper(this, "THelper", null, function () {',
  21010. ' this.Foo = function (w) {',
  21011. ' var $Self = this;',
  21012. ' var Result = 0;',
  21013. ' function Sub(Self) {',
  21014. ' $Self.Run(10);',
  21015. ' $Self.Run(10);',
  21016. ' $mod.THelper.Foo.call($Self, 1);',
  21017. ' $mod.THelper.Foo.call($Self, 1);',
  21018. ' $mod.THelper.Foo.call(Self, 1);',
  21019. ' $mod.THelper.Foo.call(Self, 1);',
  21020. ' $mod.THelper.Foo.call(Self, 1);',
  21021. ' $mod.THelper.Foo.call(Self, 1);',
  21022. ' };',
  21023. ' return Result;',
  21024. ' };',
  21025. '});',
  21026. '']),
  21027. LinesToStr([ // $mod.$main
  21028. '']));
  21029. end;
  21030. procedure TTestModule.TestClassHelper_ClassMethod_Call;
  21031. begin
  21032. StartProgram(false);
  21033. Add([
  21034. 'type',
  21035. ' TObject = class',
  21036. ' class procedure Run(w: word = 10);',
  21037. ' end;',
  21038. ' THelper = class helper for TObject',
  21039. ' class function Foo(w: word = 1): word;',
  21040. ' end;',
  21041. 'class procedure TObject.Run(w: word);',
  21042. 'begin',
  21043. ' Foo;',
  21044. ' Foo();',
  21045. ' Self.Foo;',
  21046. ' Self.Foo();',
  21047. ' with Self do begin',
  21048. ' Foo;',
  21049. ' Foo();',
  21050. ' end;',
  21051. 'end;',
  21052. 'class function THelper.foo(w: word): word;',
  21053. 'begin',
  21054. ' Run;',
  21055. ' Run();',
  21056. ' Foo;',
  21057. ' Foo();',
  21058. ' Self.Foo;',
  21059. ' Self.Foo();',
  21060. ' with Self do begin',
  21061. ' Foo;',
  21062. ' Foo();',
  21063. ' end;',
  21064. 'end;',
  21065. 'var',
  21066. ' Obj: TObject;',
  21067. 'begin',
  21068. ' obj.Foo;',
  21069. ' obj.Foo();',
  21070. ' with obj do begin',
  21071. ' Foo;',
  21072. ' Foo();',
  21073. ' end;',
  21074. ' tobject.Foo;',
  21075. ' tobject.Foo();',
  21076. ' with tobject do begin',
  21077. ' Foo;',
  21078. ' Foo();',
  21079. ' end;',
  21080. '']);
  21081. ConvertProgram;
  21082. CheckSource('TestClassHelper_ClassMethod_Call',
  21083. LinesToStr([ // statements
  21084. 'rtl.createClass(this, "TObject", null, function () {',
  21085. ' this.$init = function () {',
  21086. ' };',
  21087. ' this.$final = function () {',
  21088. ' };',
  21089. ' this.Run = function (w) {',
  21090. ' $mod.THelper.Foo.call(this, 1);',
  21091. ' $mod.THelper.Foo.call(this, 1);',
  21092. ' $mod.THelper.Foo.call(this, 1);',
  21093. ' $mod.THelper.Foo.call(this, 1);',
  21094. ' $mod.THelper.Foo.call(this, 1);',
  21095. ' $mod.THelper.Foo.call(this, 1);',
  21096. ' };',
  21097. '});',
  21098. 'rtl.createHelper(this, "THelper", null, function () {',
  21099. ' this.Foo = function (w) {',
  21100. ' var Result = 0;',
  21101. ' this.Run(10);',
  21102. ' this.Run(10);',
  21103. ' $mod.THelper.Foo.call(this, 1);',
  21104. ' $mod.THelper.Foo.call(this, 1);',
  21105. ' $mod.THelper.Foo.call(this, 1);',
  21106. ' $mod.THelper.Foo.call(this, 1);',
  21107. ' $mod.THelper.Foo.call(this, 1);',
  21108. ' $mod.THelper.Foo.call(this, 1);',
  21109. ' return Result;',
  21110. ' };',
  21111. '});',
  21112. 'this.Obj = null;',
  21113. '']),
  21114. LinesToStr([ // $mod.$main
  21115. '$mod.THelper.Foo.call($mod.Obj.$class, 1);',
  21116. '$mod.THelper.Foo.call($mod.Obj.$class, 1);',
  21117. 'var $with = $mod.Obj;',
  21118. '$mod.THelper.Foo.call($with.$class, 1);',
  21119. '$mod.THelper.Foo.call($with.$class, 1);',
  21120. '$mod.THelper.Foo.call($mod.TObject, 1);',
  21121. '$mod.THelper.Foo.call($mod.TObject, 1);',
  21122. 'var $with1 = $mod.TObject;',
  21123. '$mod.THelper.Foo.call($mod.TObject, 1);',
  21124. '$mod.THelper.Foo.call($mod.TObject, 1);',
  21125. '']));
  21126. end;
  21127. procedure TTestModule.TestClassHelper_ClassOf;
  21128. begin
  21129. StartProgram(false);
  21130. Add([
  21131. 'type',
  21132. ' TObject = class',
  21133. ' end;',
  21134. ' TClass = class of TObject;',
  21135. ' THelper = class helper for TObject',
  21136. ' class function Foo(w: word = 1): word;',
  21137. ' end;',
  21138. 'class function THelper.foo(w: word): word;',
  21139. 'begin',
  21140. 'end;',
  21141. 'var',
  21142. ' c: TClass;',
  21143. 'begin',
  21144. ' c.Foo;',
  21145. ' c.Foo();',
  21146. ' with c do begin',
  21147. ' Foo;',
  21148. ' Foo();',
  21149. ' end;',
  21150. '']);
  21151. ConvertProgram;
  21152. CheckSource('TestClassHelper_ClassOf',
  21153. LinesToStr([ // statements
  21154. 'rtl.createClass(this, "TObject", null, function () {',
  21155. ' this.$init = function () {',
  21156. ' };',
  21157. ' this.$final = function () {',
  21158. ' };',
  21159. '});',
  21160. 'rtl.createHelper(this, "THelper", null, function () {',
  21161. ' this.Foo = function (w) {',
  21162. ' var Result = 0;',
  21163. ' return Result;',
  21164. ' };',
  21165. '});',
  21166. 'this.c = null;',
  21167. '']),
  21168. LinesToStr([ // $mod.$main
  21169. '$mod.THelper.Foo.call($mod.c, 1);',
  21170. '$mod.THelper.Foo.call($mod.c, 1);',
  21171. 'var $with = $mod.c;',
  21172. '$mod.THelper.Foo.call($with, 1);',
  21173. '$mod.THelper.Foo.call($with, 1);',
  21174. '']));
  21175. end;
  21176. procedure TTestModule.TestClassHelper_MethodRefObjFPC;
  21177. begin
  21178. StartProgram(false);
  21179. Add([
  21180. '{$mode objfpc}',
  21181. 'type',
  21182. ' TObject = class',
  21183. ' procedure DoIt;',
  21184. ' end;',
  21185. ' THelper = class helper for TObject',
  21186. ' procedure Fly(w: word = 1);',
  21187. ' class procedure Glide(w: word = 1);',
  21188. ' class procedure Run(w: word = 1); static;',
  21189. ' end;',
  21190. ' TFly = procedure(w: word) of object;',
  21191. ' TGlide = TFly;',
  21192. ' TRun = procedure(w: word);',
  21193. 'var',
  21194. ' f: TFly;',
  21195. ' g: TGlide;',
  21196. ' r: TRun;',
  21197. 'procedure TObject.DoIt;',
  21198. 'begin',
  21199. ' f:=@fly;',
  21200. ' g:=@glide;',
  21201. ' r:=@run;',
  21202. ' f:[email protected];',
  21203. ' g:[email protected];',
  21204. ' r:[email protected];',
  21205. ' with self do begin',
  21206. ' f:=@fly;',
  21207. ' g:=@glide;',
  21208. ' r:=@run;',
  21209. ' end;',
  21210. 'end;',
  21211. 'procedure THelper.fly(w: word);',
  21212. 'begin',
  21213. ' f:=@fly;',
  21214. ' g:=@glide;',
  21215. ' r:=@run;',
  21216. 'end;',
  21217. 'class procedure THelper.glide(w: word);',
  21218. 'begin',
  21219. ' g:=@glide;',
  21220. ' r:=@run;',
  21221. 'end;',
  21222. 'class procedure THelper.run(w: word);',
  21223. 'begin',
  21224. ' g:=@glide;',
  21225. ' r:=@run;',
  21226. 'end;',
  21227. 'var',
  21228. ' Obj: TObject;',
  21229. 'begin',
  21230. ' f:[email protected];',
  21231. ' g:[email protected];',
  21232. ' r:[email protected];',
  21233. ' with obj do begin',
  21234. ' f:=@fly;',
  21235. ' g:=@glide;',
  21236. ' r:=@run;',
  21237. ' end;',
  21238. ' g:[email protected];',
  21239. ' r:[email protected];',
  21240. ' with tobject do begin',
  21241. ' g:=@glide;',
  21242. ' r:=@run;',
  21243. ' end;',
  21244. '']);
  21245. ConvertProgram;
  21246. CheckSource('TestClassHelper_MethodRefObjFPC',
  21247. LinesToStr([ // statements
  21248. 'rtl.createClass(this, "TObject", null, function () {',
  21249. ' this.$init = function () {',
  21250. ' };',
  21251. ' this.$final = function () {',
  21252. ' };',
  21253. ' this.DoIt = function () {',
  21254. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  21255. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  21256. ' $mod.r = $mod.THelper.Run;',
  21257. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  21258. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  21259. ' $mod.r = $mod.THelper.Run;',
  21260. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  21261. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  21262. ' $mod.r = $mod.THelper.Run;',
  21263. ' };',
  21264. '});',
  21265. 'rtl.createHelper(this, "THelper", null, function () {',
  21266. ' this.Fly = function (w) {',
  21267. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  21268. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  21269. ' $mod.r = $mod.THelper.Run;',
  21270. ' };',
  21271. ' this.Glide = function (w) {',
  21272. ' $mod.g = rtl.createCallback(this, $mod.THelper.Glide);',
  21273. ' $mod.r = $mod.THelper.Run;',
  21274. ' };',
  21275. ' this.Run = function (w) {',
  21276. ' $mod.g = rtl.createCallback($mod.THelper, $mod.THelper.Glide);',
  21277. ' $mod.r = $mod.THelper.Run;',
  21278. ' };',
  21279. '});',
  21280. 'this.f = null;',
  21281. 'this.g = null;',
  21282. 'this.r = null;',
  21283. 'this.Obj = null;',
  21284. '']),
  21285. LinesToStr([ // $mod.$main
  21286. '$mod.f = rtl.createCallback($mod.Obj, $mod.THelper.Fly);',
  21287. '$mod.g = rtl.createCallback($mod.Obj.$class, $mod.THelper.Glide);',
  21288. '$mod.r = $mod.THelper.Run;',
  21289. 'var $with = $mod.Obj;',
  21290. '$mod.f = rtl.createCallback($with, $mod.THelper.Fly);',
  21291. '$mod.g = rtl.createCallback($with.$class, $mod.THelper.Glide);',
  21292. '$mod.r = $mod.THelper.Run;',
  21293. '$mod.g = rtl.createCallback($mod.TObject, $mod.THelper.Glide);',
  21294. '$mod.r = $mod.THelper.Run;',
  21295. 'var $with1 = $mod.TObject;',
  21296. '$mod.g = rtl.createCallback($with1, $mod.THelper.Glide);',
  21297. '$mod.r = $mod.THelper.Run;',
  21298. '']));
  21299. end;
  21300. procedure TTestModule.TestClassHelper_Constructor;
  21301. begin
  21302. StartProgram(false);
  21303. Add([
  21304. 'type',
  21305. ' TObject = class',
  21306. ' constructor Create;',
  21307. ' end;',
  21308. ' TClass = class of TObject;',
  21309. ' THelper = class helper for TObject',
  21310. ' constructor NewHlp(w: word);',
  21311. ' end;',
  21312. 'var',
  21313. ' obj: TObject;',
  21314. ' c: TClass;',
  21315. 'constructor TObject.Create;',
  21316. 'begin',
  21317. ' NewHlp(2);', // normal call
  21318. ' tobject.NewHlp(3);', // new instance
  21319. ' c.newhlp(4);', // new instance
  21320. 'end;',
  21321. 'constructor THelper.NewHlp(w: word);',
  21322. 'begin',
  21323. ' create;', // normal call
  21324. ' tobject.create;', // new instance
  21325. ' NewHlp(2);', // normal call
  21326. ' tobject.NewHlp(3);', // new instance
  21327. ' c.newhlp(4);', // new instance
  21328. 'end;',
  21329. 'begin',
  21330. ' obj.newhlp(2);', // normal call
  21331. ' with Obj do newhlp(12);', // normal call
  21332. ' tobject.newhlp(3);', // new instance
  21333. ' with tobject do newhlp(13);', // new instance
  21334. ' c.newhlp(4);', // new instance
  21335. ' with c do newhlp(14);', // new instance
  21336. '']);
  21337. ConvertProgram;
  21338. CheckSource('TestClassHelper_Constructor',
  21339. LinesToStr([ // statements
  21340. 'rtl.createClass(this, "TObject", null, function () {',
  21341. ' this.$init = function () {',
  21342. ' };',
  21343. ' this.$final = function () {',
  21344. ' };',
  21345. ' this.Create = function () {',
  21346. ' $mod.THelper.NewHlp.call(this, 2);',
  21347. ' $mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  21348. ' $mod.c.$create($mod.THelper.NewHlp, [4]);',
  21349. ' return this;',
  21350. ' };',
  21351. '});',
  21352. 'rtl.createHelper(this, "THelper", null, function () {',
  21353. ' this.NewHlp = function (w) {',
  21354. ' this.Create();',
  21355. ' $mod.TObject.$create("Create");',
  21356. ' $mod.THelper.NewHlp.call(this, 2);',
  21357. ' $mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  21358. ' $mod.c.$create($mod.THelper.NewHlp, [4]);',
  21359. ' return this;',
  21360. ' };',
  21361. '});',
  21362. 'this.obj = null;',
  21363. 'this.c = null;',
  21364. '']),
  21365. LinesToStr([ // $mod.$main
  21366. '$mod.THelper.NewHlp.call($mod.obj, 2);',
  21367. 'var $with = $mod.obj;',
  21368. '$mod.THelper.NewHlp.call($with, 12);',
  21369. '$mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  21370. 'var $with1 = $mod.TObject;',
  21371. '$with1.$create($mod.THelper.NewHlp, [13]);',
  21372. '$mod.c.$create($mod.THelper.NewHlp, [4]);',
  21373. 'var $with2 = $mod.c;',
  21374. '$with2.$create($mod.THelper.NewHlp, [14]);',
  21375. '']));
  21376. end;
  21377. procedure TTestModule.TestClassHelper_InheritedObjFPC;
  21378. begin
  21379. StartProgram(false);
  21380. Add([
  21381. 'type',
  21382. ' TObject = class',
  21383. ' procedure Fly;',
  21384. ' end;',
  21385. ' TObjHelper = class helper for TObject',
  21386. ' procedure Fly;',
  21387. ' end;',
  21388. ' TBird = class',
  21389. ' procedure Fly;',
  21390. ' end;',
  21391. ' TBirdHelper = class helper for TBird',
  21392. ' procedure Fly;',
  21393. ' procedure Walk(w: word);',
  21394. ' end;',
  21395. ' TEagleHelper = class helper(TBirdHelper) for TBird',
  21396. ' procedure Fly;',
  21397. ' procedure Walk(w: word);',
  21398. ' end;',
  21399. 'procedure Tobject.fly;',
  21400. 'begin',
  21401. ' inherited;', // ignore
  21402. 'end;',
  21403. 'procedure Tobjhelper.fly;',
  21404. 'begin',
  21405. ' {@TObject_Fly}inherited;',
  21406. ' inherited {@TObject_Fly}Fly;',
  21407. 'end;',
  21408. 'procedure Tbird.fly;',
  21409. 'begin',
  21410. ' {@TObjHelper_Fly}inherited;',
  21411. ' inherited {@TObjHelper_Fly}Fly;',
  21412. 'end;',
  21413. 'procedure Tbirdhelper.fly;',
  21414. 'begin',
  21415. ' {@TBird_Fly}inherited;',
  21416. ' inherited {@TBird_Fly}Fly;',
  21417. 'end;',
  21418. 'procedure Tbirdhelper.walk(w: word);',
  21419. 'begin',
  21420. 'end;',
  21421. 'procedure teagleHelper.fly;',
  21422. 'begin',
  21423. ' {@TBird_Fly}inherited;',
  21424. ' inherited {@TBird_Fly}Fly;',
  21425. 'end;',
  21426. 'procedure teagleHelper.walk(w: word);',
  21427. 'begin',
  21428. ' {@TBirdHelper_Walk}inherited;',
  21429. ' inherited {@TBirdHelper_Walk}Walk(3);',
  21430. 'end;',
  21431. 'begin',
  21432. '']);
  21433. ConvertProgram;
  21434. CheckSource('TestClassHelper_InheritedObjFPC',
  21435. LinesToStr([ // statements
  21436. 'rtl.createClass(this, "TObject", null, function () {',
  21437. ' this.$init = function () {',
  21438. ' };',
  21439. ' this.$final = function () {',
  21440. ' };',
  21441. ' this.Fly = function () {',
  21442. ' };',
  21443. '});',
  21444. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21445. ' this.Fly = function () {',
  21446. ' $mod.TObject.Fly.call(this);',
  21447. ' $mod.TObject.Fly.call(this);',
  21448. ' };',
  21449. '});',
  21450. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21451. ' this.Fly$1 = function () {',
  21452. ' $mod.TObjHelper.Fly.call(this);',
  21453. ' $mod.TObjHelper.Fly.call(this);',
  21454. ' };',
  21455. '});',
  21456. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  21457. ' this.Fly = function () {',
  21458. ' $mod.TBird.Fly$1.call(this);',
  21459. ' $mod.TBird.Fly$1.call(this);',
  21460. ' };',
  21461. ' this.Walk = function (w) {',
  21462. ' };',
  21463. '});',
  21464. 'rtl.createHelper(this, "TEagleHelper", this.TBirdHelper, function () {',
  21465. ' this.Fly$1 = function () {',
  21466. ' $mod.TBird.Fly$1.call(this);',
  21467. ' $mod.TBird.Fly$1.call(this);',
  21468. ' };',
  21469. ' this.Walk$1 = function (w) {',
  21470. ' $mod.TBirdHelper.Walk.apply(this, arguments);',
  21471. ' $mod.TBirdHelper.Walk.call(this, 3);',
  21472. ' };',
  21473. '});',
  21474. '']),
  21475. LinesToStr([ // $mod.$main
  21476. '']));
  21477. end;
  21478. procedure TTestModule.TestClassHelper_Property;
  21479. begin
  21480. StartProgram(false);
  21481. Add([
  21482. 'type',
  21483. ' TObject = class',
  21484. ' FSize: word;',
  21485. ' function GetSpeed: word;',
  21486. ' procedure SetSpeed(Value: word);',
  21487. ' end;',
  21488. ' TObjHelper = class helper for TObject',
  21489. ' function GetLeft: word;',
  21490. ' procedure SetLeft(Value: word);',
  21491. ' property Size: word read FSize write FSize;',
  21492. ' property Speed: word read GetSpeed write SetSpeed;',
  21493. ' property Left: word read GetLeft write SetLeft;',
  21494. ' end;',
  21495. ' TBird = class',
  21496. ' property NotRight: word read GetLeft write SetLeft;',
  21497. ' procedure DoIt;',
  21498. ' end;',
  21499. 'var',
  21500. ' b: TBird;',
  21501. 'function Tobject.GetSpeed: word;',
  21502. 'begin',
  21503. ' Size:=Size+11;',
  21504. ' Speed:=Speed+12;',
  21505. ' Result:=Left+13;',
  21506. ' Left:=13;',
  21507. ' Left:=Left+13;',
  21508. ' Self.Size:=Self.Size+21;',
  21509. ' Self.Speed:=Self.Speed+22;',
  21510. ' Self.Left:=Self.Left+23;',
  21511. ' with Self do begin',
  21512. ' Size:=Size+31;',
  21513. ' Speed:=Speed+32;',
  21514. ' Left:=Left+33;',
  21515. ' end;',
  21516. 'end;',
  21517. 'procedure Tobject.SetSpeed(Value: word);',
  21518. 'begin',
  21519. 'end;',
  21520. 'function TObjHelper.GetLeft: word;',
  21521. 'begin',
  21522. ' Size:=Size+11;',
  21523. ' Speed:=Speed+12;',
  21524. ' Left:=Left+13;',
  21525. ' Self.Size:=Self.Size+21;',
  21526. ' Self.Speed:=Self.Speed+22;',
  21527. ' Self.Left:=Self.Left+23;',
  21528. ' with Self do begin',
  21529. ' Size:=Size+31;',
  21530. ' Speed:=Speed+32;',
  21531. ' Left:=Left+33;',
  21532. ' end;',
  21533. 'end;',
  21534. 'procedure TObjHelper.SetLeft(Value: word);',
  21535. 'begin',
  21536. 'end;',
  21537. 'procedure TBird.DoIt;',
  21538. 'begin',
  21539. ' NotRight:=NotRight+11;',
  21540. ' Self.NotRight:=Self.NotRight+21;',
  21541. ' with Self do begin',
  21542. ' NotRight:=NotRight+31;',
  21543. ' end;',
  21544. 'end;',
  21545. 'begin',
  21546. ' b.Size:=b.Size+11;',
  21547. ' b.Speed:=b.Speed+12;',
  21548. ' b.Left:=b.Left+13;',
  21549. ' b.NotRight:=b.NotRight+14;',
  21550. ' with b do begin',
  21551. ' Size:=Size+31;',
  21552. ' Speed:=Speed+32;',
  21553. ' Left:=Left+33;',
  21554. ' NotRight:=NotRight+34;',
  21555. ' end;',
  21556. '']);
  21557. ConvertProgram;
  21558. CheckSource('TestClassHelper_Property',
  21559. LinesToStr([ // statements
  21560. 'rtl.createClass(this, "TObject", null, function () {',
  21561. ' this.$init = function () {',
  21562. ' this.FSize = 0;',
  21563. ' };',
  21564. ' this.$final = function () {',
  21565. ' };',
  21566. ' this.GetSpeed = function () {',
  21567. ' var Result = 0;',
  21568. ' this.FSize = this.FSize + 11;',
  21569. ' this.SetSpeed(this.GetSpeed() + 12);',
  21570. ' Result = $mod.TObjHelper.GetLeft.call(this) + 13;',
  21571. ' $mod.TObjHelper.SetLeft.call(this, 13);',
  21572. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  21573. ' this.FSize = this.FSize + 21;',
  21574. ' this.SetSpeed(this.GetSpeed() + 22);',
  21575. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  21576. ' this.FSize = this.FSize + 31;',
  21577. ' this.SetSpeed(this.GetSpeed() + 32);',
  21578. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  21579. ' return Result;',
  21580. ' };',
  21581. ' this.SetSpeed = function (Value) {',
  21582. ' };',
  21583. '});',
  21584. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21585. ' this.GetLeft = function () {',
  21586. ' var Result = 0;',
  21587. ' this.FSize = this.FSize + 11;',
  21588. ' this.SetSpeed(this.GetSpeed() + 12);',
  21589. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  21590. ' this.FSize = this.FSize + 21;',
  21591. ' this.SetSpeed(this.GetSpeed() + 22);',
  21592. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  21593. ' this.FSize = this.FSize + 31;',
  21594. ' this.SetSpeed(this.GetSpeed() + 32);',
  21595. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  21596. ' return Result;',
  21597. ' };',
  21598. ' this.SetLeft = function (Value) {',
  21599. ' };',
  21600. '});',
  21601. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21602. ' this.DoIt = function () {',
  21603. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 11);',
  21604. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 21);',
  21605. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 31);',
  21606. ' };',
  21607. '});',
  21608. 'this.b = null;',
  21609. '']),
  21610. LinesToStr([ // $mod.$main
  21611. '$mod.b.FSize = $mod.b.FSize + 11;',
  21612. '$mod.b.SetSpeed($mod.b.GetSpeed() + 12);',
  21613. '$mod.TObjHelper.SetLeft.call($mod.b, $mod.TObjHelper.GetLeft.call($mod.b) + 13);',
  21614. '$mod.TObjHelper.SetLeft.call($mod.b, $mod.TObjHelper.GetLeft.call($mod.b) + 14);',
  21615. 'var $with = $mod.b;',
  21616. '$with.FSize = $with.FSize + 31;',
  21617. '$with.SetSpeed($with.GetSpeed() + 32);',
  21618. '$mod.TObjHelper.SetLeft.call($with, $mod.TObjHelper.GetLeft.call($with) + 33);',
  21619. '$mod.TObjHelper.SetLeft.call($with, $mod.TObjHelper.GetLeft.call($with) + 34);',
  21620. '']));
  21621. end;
  21622. procedure TTestModule.TestClassHelper_Property_Array;
  21623. begin
  21624. StartProgram(false);
  21625. Add([
  21626. 'type',
  21627. ' TObject = class',
  21628. ' function GetSpeed(Index: boolean): word;',
  21629. ' procedure SetSpeed(Index: boolean; Value: word);',
  21630. ' end;',
  21631. ' TObjHelper = class helper for TObject',
  21632. ' function GetSize(Index: boolean): word;',
  21633. ' procedure SetSize(Index: boolean; Value: word);',
  21634. ' property Size[Index: boolean]: word read GetSize write SetSize;',
  21635. ' property Speed[Index: boolean]: word read GetSpeed write SetSpeed;',
  21636. ' end;',
  21637. ' TBird = class',
  21638. ' property Items[Index: boolean]: word read GetSize write SetSize;',
  21639. ' procedure DoIt;',
  21640. ' end;',
  21641. 'var',
  21642. ' b: TBird;',
  21643. 'function Tobject.GetSpeed(Index: boolean): word;',
  21644. 'begin',
  21645. ' Result:=Size[false];',
  21646. ' Size[true]:=Size[false]+11;',
  21647. ' Speed[true]:=Speed[false]+12;',
  21648. ' Self.Size[true]:=Self.Size[false]+21;',
  21649. ' Self.Speed[true]:=Self.Speed[false]+22;',
  21650. ' with Self do begin',
  21651. ' Size[true]:=Size[false]+31;',
  21652. ' Speed[true]:=Speed[false]+32;',
  21653. ' end;',
  21654. 'end;',
  21655. 'procedure Tobject.SetSpeed(Index: boolean; Value: word);',
  21656. 'begin',
  21657. 'end;',
  21658. 'function TObjHelper.GetSize(Index: boolean): word;',
  21659. 'begin',
  21660. ' Size[true]:=Size[false]+11;',
  21661. ' Speed[true]:=Speed[false]+12;',
  21662. ' Self.Size[true]:=Self.Size[false]+21;',
  21663. ' Self.Speed[true]:=Self.Speed[false]+22;',
  21664. ' with Self do begin',
  21665. ' Size[true]:=Size[false]+31;',
  21666. ' Speed[true]:=Speed[false]+32;',
  21667. ' end;',
  21668. 'end;',
  21669. 'procedure TObjHelper.SetSize(Index: boolean; Value: word);',
  21670. 'begin',
  21671. 'end;',
  21672. 'procedure TBird.DoIt;',
  21673. 'begin',
  21674. ' Items[true]:=Items[false]+11;',
  21675. ' Self.Items[true]:=Self.Items[false]+21;',
  21676. ' with Self do Items[true]:=Items[false]+31;',
  21677. 'end;',
  21678. 'begin',
  21679. ' b.Size[true]:=b.Size[false]+11;',
  21680. ' b.Speed[true]:=b.Speed[false]+12;',
  21681. ' b.Items[true]:=b.Items[false]+13;',
  21682. ' with b do begin',
  21683. ' Size[true]:=Size[false]+21;',
  21684. ' Speed[true]:=Speed[false]+22;',
  21685. ' Items[true]:=Items[false]+23;',
  21686. ' end;',
  21687. '']);
  21688. ConvertProgram;
  21689. CheckSource('TestClassHelper_Property_Array',
  21690. LinesToStr([ // statements
  21691. 'rtl.createClass(this, "TObject", null, function () {',
  21692. ' this.$init = function () {',
  21693. ' };',
  21694. ' this.$final = function () {',
  21695. ' };',
  21696. ' this.GetSpeed = function (Index) {',
  21697. ' var Result = 0;',
  21698. ' Result = $mod.TObjHelper.GetSize.call(this, false);',
  21699. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  21700. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  21701. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  21702. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  21703. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  21704. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  21705. ' return Result;',
  21706. ' };',
  21707. ' this.SetSpeed = function (Index, Value) {',
  21708. ' };',
  21709. '});',
  21710. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21711. ' this.GetSize = function (Index) {',
  21712. ' var Result = 0;',
  21713. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  21714. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  21715. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  21716. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  21717. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  21718. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  21719. ' return Result;',
  21720. ' };',
  21721. ' this.SetSize = function (Index, Value) {',
  21722. ' };',
  21723. '});',
  21724. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21725. ' this.DoIt = function () {',
  21726. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  21727. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  21728. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  21729. ' };',
  21730. '});',
  21731. 'this.b = null;',
  21732. '']),
  21733. LinesToStr([ // $mod.$main
  21734. '$mod.TObjHelper.SetSize.call($mod.b, true, $mod.TObjHelper.GetSize.call($mod.b, false) + 11);',
  21735. '$mod.b.SetSpeed(true, $mod.b.GetSpeed(false) + 12);',
  21736. '$mod.TObjHelper.SetSize.call($mod.b, true, $mod.TObjHelper.GetSize.call($mod.b, false) + 13);',
  21737. 'var $with = $mod.b;',
  21738. '$mod.TObjHelper.SetSize.call($with, true, $mod.TObjHelper.GetSize.call($with, false) + 21);',
  21739. '$with.SetSpeed(true, $with.GetSpeed(false) + 22);',
  21740. '$mod.TObjHelper.SetSize.call($with, true, $mod.TObjHelper.GetSize.call($with, false) + 23);',
  21741. '']));
  21742. end;
  21743. procedure TTestModule.TestClassHelper_Property_Array_Default;
  21744. begin
  21745. StartProgram(false);
  21746. Add([
  21747. 'type',
  21748. ' TObject = class',
  21749. ' function GetSpeed(Index: boolean): word;',
  21750. ' procedure SetSpeed(Index: boolean; Value: word);',
  21751. ' end;',
  21752. ' TObjHelper = class helper for TObject',
  21753. ' property Speed[Index: boolean]: word read GetSpeed write SetSpeed; default;',
  21754. ' end;',
  21755. ' TBird = class',
  21756. ' end;',
  21757. ' TBirdHelper = class helper for TBird',
  21758. ' function GetSize(Index: word): boolean;',
  21759. ' procedure SetSize(Index: word; Value: boolean);',
  21760. ' property Size[Index: word]: boolean read GetSize write SetSize; default;',
  21761. ' end;',
  21762. 'function Tobject.GetSpeed(Index: boolean): word;',
  21763. 'begin',
  21764. ' Self[true]:=Self[false]+1;',
  21765. 'end;',
  21766. 'procedure Tobject.SetSpeed(Index: boolean; Value: word);',
  21767. 'begin',
  21768. 'end;',
  21769. 'function TBirdHelper.GetSize(Index: word): boolean;',
  21770. 'begin',
  21771. ' Self[1]:=not Self[2];',
  21772. 'end;',
  21773. 'procedure TBirdHelper.SetSize(Index: word; Value: boolean);',
  21774. 'begin',
  21775. 'end;',
  21776. 'var',
  21777. ' o: TObject;',
  21778. ' b: TBird;',
  21779. 'begin',
  21780. ' o[true]:=o[false]+1;',
  21781. ' b[3]:=not b[4];',
  21782. '']);
  21783. ConvertProgram;
  21784. CheckSource('TestClassHelper_Property_Array_Default',
  21785. LinesToStr([ // statements
  21786. 'rtl.createClass(this, "TObject", null, function () {',
  21787. ' this.$init = function () {',
  21788. ' };',
  21789. ' this.$final = function () {',
  21790. ' };',
  21791. ' this.GetSpeed = function (Index) {',
  21792. ' var Result = 0;',
  21793. ' this.SetSpeed(true, this.GetSpeed(false) + 1);',
  21794. ' return Result;',
  21795. ' };',
  21796. ' this.SetSpeed = function (Index, Value) {',
  21797. ' };',
  21798. '});',
  21799. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21800. '});',
  21801. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21802. '});',
  21803. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  21804. ' this.GetSize = function (Index) {',
  21805. ' var Result = false;',
  21806. ' $mod.TBirdHelper.SetSize.call(this, 1, !$mod.TBirdHelper.GetSize.call(this, 2));',
  21807. ' return Result;',
  21808. ' };',
  21809. ' this.SetSize = function (Index, Value) {',
  21810. ' };',
  21811. '});',
  21812. 'this.o = null;',
  21813. 'this.b = null;',
  21814. '']),
  21815. LinesToStr([ // $mod.$main
  21816. '$mod.o.SetSpeed(true, $mod.o.GetSpeed(false) + 1);',
  21817. '$mod.TBirdHelper.SetSize.call($mod.b, 3, !$mod.TBirdHelper.GetSize.call($mod.b, 4));',
  21818. '']));
  21819. end;
  21820. procedure TTestModule.TestClassHelper_Property_Array_DefaultDefault;
  21821. begin
  21822. StartProgram(false);
  21823. Add([
  21824. 'type',
  21825. ' TObject = class',
  21826. ' end;',
  21827. ' TObjHelper = class helper for TObject',
  21828. ' function GetItems(Index: word): TObject;',
  21829. ' procedure SetItems(Index: word; Value: TObject);',
  21830. ' property Items[Index: word]: TObject read GetItems write SetItems; default;',
  21831. ' end;',
  21832. 'function Tobjhelper.GetItems(Index: word): TObject;',
  21833. 'begin',
  21834. ' Self[1][2]:=Self[3][4];',
  21835. 'end;',
  21836. 'procedure Tobjhelper.SetItems(Index: word; Value: TObject);',
  21837. 'begin',
  21838. 'end;',
  21839. 'var',
  21840. ' o: TObject;',
  21841. 'begin',
  21842. ' o[1][2]:=o[3][4];',
  21843. '']);
  21844. ConvertProgram;
  21845. CheckSource('TestClassHelper_Property_Array_DefaultDefault',
  21846. LinesToStr([ // statements
  21847. 'rtl.createClass(this, "TObject", null, function () {',
  21848. ' this.$init = function () {',
  21849. ' };',
  21850. ' this.$final = function () {',
  21851. ' };',
  21852. '});',
  21853. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21854. ' this.GetItems = function (Index) {',
  21855. ' var Result = null;',
  21856. ' $mod.TObjHelper.SetItems.call($mod.TObjHelper.GetItems.call(this, 1), 2, $mod.TObjHelper.GetItems.call($mod.TObjHelper.GetItems.call(this, 3), 4));',
  21857. ' return Result;',
  21858. ' };',
  21859. ' this.SetItems = function (Index, Value) {',
  21860. ' };',
  21861. '});',
  21862. 'this.o = null;',
  21863. '']),
  21864. LinesToStr([ // $mod.$main
  21865. '$mod.TObjHelper.SetItems.call($mod.TObjHelper.GetItems.call($mod.o, 1), 2, $mod.TObjHelper.GetItems.call($mod.TObjHelper.GetItems.call($mod.o, 3), 4));',
  21866. '']));
  21867. end;
  21868. procedure TTestModule.TestClassHelper_ClassProperty;
  21869. begin
  21870. StartProgram(false);
  21871. Add([
  21872. 'type',
  21873. ' TObject = class',
  21874. ' class var FSize: word;',
  21875. ' class function GetSpeed: word;',
  21876. ' class procedure SetSpeed(Value: word); virtual; abstract;',
  21877. ' end;',
  21878. ' TObjHelper = class helper for TObject',
  21879. ' class function GetLeft: word;',
  21880. ' class procedure SetLeft(Value: word);',
  21881. ' class property Size: word read FSize write FSize;',
  21882. ' class property Speed: word read GetSpeed write SetSpeed;',
  21883. ' class property Left: word read GetLeft write SetLeft;',
  21884. ' end;',
  21885. ' TBird = class',
  21886. ' class property NotRight: word read GetLeft write SetLeft;',
  21887. ' class procedure DoIt;',
  21888. ' end;',
  21889. ' TBirdClass = class of TBird;',
  21890. 'class function Tobject.GetSpeed: word;',
  21891. 'begin',
  21892. ' Size:=Size+11;',
  21893. ' Speed:=Speed+12;',
  21894. ' Left:=Left+13;',
  21895. ' Self.Size:=Self.Size+21;',
  21896. ' Self.Speed:=Self.Speed+22;',
  21897. ' Self.Left:=Self.Left+23;',
  21898. ' with Self do begin',
  21899. ' Size:=Size+31;',
  21900. ' Speed:=Speed+32;',
  21901. ' Left:=Left+33;',
  21902. ' end;',
  21903. 'end;',
  21904. 'class function TObjHelper.GetLeft: word;',
  21905. 'begin',
  21906. ' Size:=Size+11;',
  21907. ' Speed:=Speed+12;',
  21908. ' Left:=Left+13;',
  21909. ' Self.Size:=Self.Size+21;',
  21910. ' Self.Speed:=Self.Speed+22;',
  21911. ' Self.Left:=Self.Left+23;',
  21912. ' with Self do begin',
  21913. ' Size:=Size+31;',
  21914. ' Speed:=Speed+32;',
  21915. ' Left:=Left+33;',
  21916. ' end;',
  21917. 'end;',
  21918. 'class procedure TObjHelper.SetLeft(Value: word);',
  21919. 'begin',
  21920. 'end;',
  21921. 'class procedure TBird.DoIt;',
  21922. 'begin',
  21923. ' NotRight:=NotRight+11;',
  21924. ' Self.NotRight:=Self.NotRight+21;',
  21925. ' with Self do NotRight:=NotRight+31;',
  21926. 'end;',
  21927. 'var',
  21928. ' b: TBird;',
  21929. ' c: TBirdClass;',
  21930. 'begin',
  21931. ' b.Size:=b.Size+11;',
  21932. ' b.Speed:=b.Speed+12;',
  21933. ' b.Left:=b.Left+13;',
  21934. ' b.NotRight:=b.NotRight+14;',
  21935. ' with b do begin',
  21936. ' Size:=Size+31;',
  21937. ' Speed:=Speed+32;',
  21938. ' Left:=Left+33;',
  21939. ' NotRight:=NotRight+34;',
  21940. ' end;',
  21941. ' c.Size:=c.Size+11;',
  21942. ' c.Speed:=c.Speed+12;',
  21943. ' c.Left:=c.Left+13;',
  21944. ' c.NotRight:=c.NotRight+14;',
  21945. ' with c do begin',
  21946. ' Size:=Size+31;',
  21947. ' Speed:=Speed+32;',
  21948. ' Left:=Left+33;',
  21949. ' NotRight:=NotRight+34;',
  21950. ' end;',
  21951. ' tbird.Size:=tbird.Size+11;',
  21952. ' tbird.Speed:=tbird.Speed+12;',
  21953. ' tbird.Left:=tbird.Left+13;',
  21954. ' tbird.NotRight:=tbird.NotRight+14;',
  21955. ' with tbird do begin',
  21956. ' Size:=Size+31;',
  21957. ' Speed:=Speed+32;',
  21958. ' Left:=Left+33;',
  21959. ' NotRight:=NotRight+34;',
  21960. ' end;',
  21961. '']);
  21962. ConvertProgram;
  21963. CheckSource('TestClassHelper_ClassProperty',
  21964. LinesToStr([ // statements
  21965. 'rtl.createClass(this, "TObject", null, function () {',
  21966. ' this.FSize = 0;',
  21967. ' this.$init = function () {',
  21968. ' };',
  21969. ' this.$final = function () {',
  21970. ' };',
  21971. ' this.GetSpeed = function () {',
  21972. ' var Result = 0;',
  21973. ' $mod.TObject.FSize = this.FSize + 11;',
  21974. ' this.SetSpeed(this.GetSpeed() + 12);',
  21975. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  21976. ' $mod.TObject.FSize = this.FSize + 21;',
  21977. ' this.SetSpeed(this.GetSpeed() + 22);',
  21978. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  21979. ' $mod.TObject.FSize = this.FSize + 31;',
  21980. ' this.SetSpeed(this.GetSpeed() + 32);',
  21981. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  21982. ' return Result;',
  21983. ' };',
  21984. '});',
  21985. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21986. ' this.GetLeft = function () {',
  21987. ' var Result = 0;',
  21988. ' $mod.TObject.FSize = this.FSize + 11;',
  21989. ' this.SetSpeed(this.GetSpeed() + 12);',
  21990. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  21991. ' $mod.TObject.FSize = this.FSize + 21;',
  21992. ' this.SetSpeed(this.GetSpeed() + 22);',
  21993. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  21994. ' $mod.TObject.FSize = this.FSize + 31;',
  21995. ' this.SetSpeed(this.GetSpeed() + 32);',
  21996. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  21997. ' return Result;',
  21998. ' };',
  21999. ' this.SetLeft = function (Value) {',
  22000. ' };',
  22001. '});',
  22002. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  22003. ' this.DoIt = function () {',
  22004. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 11);',
  22005. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 21);',
  22006. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 31);',
  22007. ' };',
  22008. '});',
  22009. 'this.b = null;',
  22010. 'this.c = null;',
  22011. '']),
  22012. LinesToStr([ // $mod.$main
  22013. '$mod.TObject.FSize = $mod.b.FSize + 11;',
  22014. '$mod.b.$class.SetSpeed($mod.b.$class.GetSpeed() + 12);',
  22015. '$mod.TObjHelper.SetLeft.call($mod.b.$class, $mod.TObjHelper.GetLeft.call($mod.b.$class) + 13);',
  22016. '$mod.TObjHelper.SetLeft.call($mod.b.$class, $mod.TObjHelper.GetLeft.call($mod.b.$class) + 14);',
  22017. 'var $with = $mod.b;',
  22018. '$mod.TObject.FSize = $with.FSize + 31;',
  22019. '$with.$class.SetSpeed($with.$class.GetSpeed() + 32);',
  22020. '$mod.TObjHelper.SetLeft.call($with.$class, $mod.TObjHelper.GetLeft.call($with.$class) + 33);',
  22021. '$mod.TObjHelper.SetLeft.call($with.$class, $mod.TObjHelper.GetLeft.call($with.$class) + 34);',
  22022. '$mod.TObject.FSize = $mod.c.FSize + 11;',
  22023. '$mod.c.SetSpeed($mod.c.GetSpeed() + 12);',
  22024. '$mod.TObjHelper.SetLeft.call($mod.c, $mod.TObjHelper.GetLeft.call($mod.c) + 13);',
  22025. '$mod.TObjHelper.SetLeft.call($mod.c, $mod.TObjHelper.GetLeft.call($mod.c) + 14);',
  22026. 'var $with1 = $mod.c;',
  22027. '$mod.TObject.FSize = $with1.FSize + 31;',
  22028. '$with1.SetSpeed($with1.GetSpeed() + 32);',
  22029. '$mod.TObjHelper.SetLeft.call($with1, $mod.TObjHelper.GetLeft.call($with1) + 33);',
  22030. '$mod.TObjHelper.SetLeft.call($with1, $mod.TObjHelper.GetLeft.call($with1) + 34);',
  22031. '$mod.TObject.FSize = $mod.TBird.FSize + 11;',
  22032. '$mod.TBird.SetSpeed($mod.TBird.GetSpeed() + 12);',
  22033. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 13);',
  22034. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 14);',
  22035. 'var $with2 = $mod.TBird;',
  22036. '$mod.TObject.FSize = $with2.FSize + 31;',
  22037. '$with2.SetSpeed($with2.GetSpeed() + 32);',
  22038. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 33);',
  22039. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 34);',
  22040. '']));
  22041. end;
  22042. procedure TTestModule.TestClassHelper_ClassPropertyStatic;
  22043. begin
  22044. StartProgram(false);
  22045. Add([
  22046. 'type',
  22047. ' TObject = class',
  22048. ' class function GetSpeed: word; static;',
  22049. ' class procedure SetSpeed(Value: word); static;',
  22050. ' end;',
  22051. ' TObjHelper = class helper for TObject',
  22052. ' class function GetLeft: word; static;',
  22053. ' class procedure SetLeft(Value: word); static;',
  22054. ' class property Speed: word read GetSpeed write SetSpeed;',
  22055. ' class property Left: word read GetLeft write SetLeft;',
  22056. ' end;',
  22057. ' TBird = class',
  22058. ' class property NotRight: word read GetLeft write SetLeft;',
  22059. ' class procedure DoIt; static;',
  22060. ' class procedure DoSome;',
  22061. ' end;',
  22062. ' TBirdClass = class of TBird;',
  22063. 'class function Tobject.GetSpeed: word;',
  22064. 'begin',
  22065. ' Speed:=Speed+12;',
  22066. ' Left:=Left+13;',
  22067. 'end;',
  22068. 'class procedure TObject.SetSpeed(Value: word);',
  22069. 'begin',
  22070. 'end;',
  22071. 'class function TObjHelper.GetLeft: word;',
  22072. 'begin',
  22073. ' Speed:=Speed+12;',
  22074. ' Left:=Left+13;',
  22075. 'end;',
  22076. 'class procedure TObjHelper.SetLeft(Value: word);',
  22077. 'begin',
  22078. 'end;',
  22079. 'class procedure TBird.DoIt;',
  22080. 'begin',
  22081. ' NotRight:=NotRight+11;',
  22082. 'end;',
  22083. 'class procedure TBird.DoSome;',
  22084. 'begin',
  22085. ' Speed:=Speed+12;',
  22086. ' Left:=Left+13;',
  22087. ' Self.Speed:=Self.Speed+22;',
  22088. ' Self.Left:=Self.Left+23;',
  22089. ' with Self do begin',
  22090. ' Speed:=Speed+32;',
  22091. ' Left:=Left+33;',
  22092. ' end;',
  22093. ' NotRight:=NotRight+11;',
  22094. ' Self.NotRight:=Self.NotRight+21;',
  22095. ' with Self do NotRight:=NotRight+31;',
  22096. 'end;',
  22097. 'var',
  22098. ' b: TBird;',
  22099. ' c: TBirdClass;',
  22100. 'begin',
  22101. ' b.Speed:=b.Speed+12;',
  22102. ' b.Left:=b.Left+13;',
  22103. ' b.NotRight:=b.NotRight+14;',
  22104. ' with b do begin',
  22105. ' Speed:=Speed+32;',
  22106. ' Left:=Left+33;',
  22107. ' NotRight:=NotRight+34;',
  22108. ' end;',
  22109. ' c.Speed:=c.Speed+12;',
  22110. ' c.Left:=c.Left+13;',
  22111. ' c.NotRight:=c.NotRight+14;',
  22112. ' with c do begin',
  22113. ' Speed:=Speed+32;',
  22114. ' Left:=Left+33;',
  22115. ' NotRight:=NotRight+34;',
  22116. ' end;',
  22117. ' tbird.Speed:=tbird.Speed+12;',
  22118. ' tbird.Left:=tbird.Left+13;',
  22119. ' tbird.NotRight:=tbird.NotRight+14;',
  22120. ' with tbird do begin',
  22121. ' Speed:=Speed+32;',
  22122. ' Left:=Left+33;',
  22123. ' NotRight:=NotRight+34;',
  22124. ' end;',
  22125. '']);
  22126. ConvertProgram;
  22127. CheckSource('TestClassHelper_ClassPropertyStatic',
  22128. LinesToStr([ // statements
  22129. 'rtl.createClass(this, "TObject", null, function () {',
  22130. ' this.$init = function () {',
  22131. ' };',
  22132. ' this.$final = function () {',
  22133. ' };',
  22134. ' this.GetSpeed = function () {',
  22135. ' var Result = 0;',
  22136. ' $mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  22137. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  22138. ' return Result;',
  22139. ' };',
  22140. ' this.SetSpeed = function (Value) {',
  22141. ' };',
  22142. '});',
  22143. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  22144. ' this.GetLeft = function () {',
  22145. ' var Result = 0;',
  22146. ' $mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  22147. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  22148. ' return Result;',
  22149. ' };',
  22150. ' this.SetLeft = function (Value) {',
  22151. ' };',
  22152. '});',
  22153. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  22154. ' this.DoIt = function () {',
  22155. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 11);',
  22156. ' };',
  22157. ' this.DoSome = function () {',
  22158. ' this.SetSpeed(this.GetSpeed() + 12);',
  22159. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  22160. ' this.SetSpeed(this.GetSpeed() + 22);',
  22161. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 23);',
  22162. ' this.SetSpeed(this.GetSpeed() + 32);',
  22163. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  22164. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 11);',
  22165. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 21);',
  22166. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 31);',
  22167. ' };',
  22168. '});',
  22169. 'this.b = null;',
  22170. 'this.c = null;',
  22171. '']),
  22172. LinesToStr([ // $mod.$main
  22173. '$mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  22174. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  22175. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  22176. 'var $with = $mod.b;',
  22177. '$with.SetSpeed($with.GetSpeed() + 32);',
  22178. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  22179. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  22180. '$mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  22181. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  22182. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  22183. 'var $with1 = $mod.c;',
  22184. '$with1.SetSpeed($with1.GetSpeed() + 32);',
  22185. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  22186. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  22187. '$mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  22188. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  22189. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  22190. 'var $with2 = $mod.TBird;',
  22191. '$with2.SetSpeed($with2.GetSpeed() + 32);',
  22192. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  22193. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  22194. '']));
  22195. end;
  22196. procedure TTestModule.TestClassHelper_ClassProperty_Array;
  22197. begin
  22198. StartProgram(false);
  22199. Add([
  22200. 'type',
  22201. ' TObject = class',
  22202. ' class function GetSpeed(Index: boolean): word;',
  22203. ' class procedure SetSpeed(Index: boolean; Value: word); virtual; abstract;',
  22204. ' end;',
  22205. ' TObjHelper = class helper for TObject',
  22206. ' class function GetSize(Index: boolean): word;',
  22207. ' class procedure SetSize(Index: boolean; Value: word);',
  22208. ' class property Size[Index: boolean]: word read GetSize write SetSize;',
  22209. ' class property Speed[Index: boolean]: word read GetSpeed write SetSpeed;',
  22210. ' end;',
  22211. ' TBird = class',
  22212. ' class property Items[Index: boolean]: word read GetSize write SetSize;',
  22213. ' class procedure DoIt;',
  22214. ' end;',
  22215. ' TBirdClass = class of TBird;',
  22216. 'class function Tobject.GetSpeed(Index: boolean): word;',
  22217. 'begin',
  22218. ' Size[true]:=Size[false]+11;',
  22219. ' Speed[true]:=Speed[false]+12;',
  22220. ' Self.Size[true]:=Self.Size[false]+21;',
  22221. ' Self.Speed[true]:=Self.Speed[false]+22;',
  22222. ' with Self do begin',
  22223. ' Size[true]:=Size[false]+31;',
  22224. ' Speed[true]:=Speed[false]+32;',
  22225. ' end;',
  22226. 'end;',
  22227. 'class function TObjHelper.GetSize(Index: boolean): word;',
  22228. 'begin',
  22229. ' Size[true]:=Size[false]+11;',
  22230. ' Speed[true]:=Speed[false]+12;',
  22231. ' Self.Size[true]:=Self.Size[false]+21;',
  22232. ' Self.Speed[true]:=Self.Speed[false]+22;',
  22233. ' with Self do begin',
  22234. ' Size[true]:=Size[false]+31;',
  22235. ' Speed[true]:=Speed[false]+32;',
  22236. ' end;',
  22237. 'end;',
  22238. 'class procedure TObjHelper.SetSize(Index: boolean; Value: word);',
  22239. 'begin',
  22240. 'end;',
  22241. 'class procedure TBird.DoIt;',
  22242. 'begin',
  22243. ' Items[true]:=Items[false]+11;',
  22244. ' Self.Items[true]:=Self.Items[false]+21;',
  22245. ' with Self do Items[true]:=Items[false]+31;',
  22246. 'end;',
  22247. 'var',
  22248. ' b: TBird;',
  22249. ' c: TBirdClass;',
  22250. 'begin',
  22251. ' b.Size[true]:=b.Size[false]+11;',
  22252. ' b.Speed[true]:=b.Speed[false]+12;',
  22253. ' b.Items[true]:=b.Items[false]+13;',
  22254. ' with b do begin',
  22255. ' Size[true]:=Size[false]+21;',
  22256. ' Speed[true]:=Speed[false]+22;',
  22257. ' Items[true]:=Items[false]+23;',
  22258. ' end;',
  22259. ' c.Size[true]:=c.Size[false]+11;',
  22260. ' c.Speed[true]:=c.Speed[false]+12;',
  22261. ' c.Items[true]:=c.Items[false]+13;',
  22262. ' with c do begin',
  22263. ' Size[true]:=Size[false]+21;',
  22264. ' Speed[true]:=Speed[false]+22;',
  22265. ' Items[true]:=Items[false]+23;',
  22266. ' end;',
  22267. ' TBird.Size[true]:=TBird.Size[false]+11;',
  22268. ' TBird.Speed[true]:=TBird.Speed[false]+12;',
  22269. ' TBird.Items[true]:=TBird.Items[false]+13;',
  22270. ' with TBird do begin',
  22271. ' Size[true]:=Size[false]+21;',
  22272. ' Speed[true]:=Speed[false]+22;',
  22273. ' Items[true]:=Items[false]+23;',
  22274. ' end;',
  22275. '']);
  22276. ConvertProgram;
  22277. CheckSource('TestClassHelper_ClassProperty_Array',
  22278. LinesToStr([ // statements
  22279. 'rtl.createClass(this, "TObject", null, function () {',
  22280. ' this.$init = function () {',
  22281. ' };',
  22282. ' this.$final = function () {',
  22283. ' };',
  22284. ' this.GetSpeed = function (Index) {',
  22285. ' var Result = 0;',
  22286. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  22287. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  22288. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  22289. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  22290. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  22291. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  22292. ' return Result;',
  22293. ' };',
  22294. '});',
  22295. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  22296. ' this.GetSize = function (Index) {',
  22297. ' var Result = 0;',
  22298. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  22299. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  22300. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  22301. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  22302. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  22303. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  22304. ' return Result;',
  22305. ' };',
  22306. ' this.SetSize = function (Index, Value) {',
  22307. ' };',
  22308. '});',
  22309. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  22310. ' this.DoIt = function () {',
  22311. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  22312. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  22313. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  22314. ' };',
  22315. '});',
  22316. 'this.b = null;',
  22317. 'this.c = null;',
  22318. '']),
  22319. LinesToStr([ // $mod.$main
  22320. '$mod.TObjHelper.SetSize.call($mod.b.$class, true, $mod.TObjHelper.GetSize.call($mod.b.$class, false) + 11);',
  22321. '$mod.b.$class.SetSpeed(true, $mod.b.$class.GetSpeed(false) + 12);',
  22322. '$mod.TObjHelper.SetSize.call($mod.b.$class, true, $mod.TObjHelper.GetSize.call($mod.b.$class, false) + 13);',
  22323. 'var $with = $mod.b;',
  22324. '$mod.TObjHelper.SetSize.call($with.$class, true, $mod.TObjHelper.GetSize.call($with.$class, false) + 21);',
  22325. '$with.$class.SetSpeed(true, $with.$class.GetSpeed(false) + 22);',
  22326. '$mod.TObjHelper.SetSize.call($with.$class, true, $mod.TObjHelper.GetSize.call($with.$class, false) + 23);',
  22327. '$mod.TObjHelper.SetSize.call($mod.c, true, $mod.TObjHelper.GetSize.call($mod.c, false) + 11);',
  22328. '$mod.c.SetSpeed(true, $mod.c.GetSpeed(false) + 12);',
  22329. '$mod.TObjHelper.SetSize.call($mod.c, true, $mod.TObjHelper.GetSize.call($mod.c, false) + 13);',
  22330. 'var $with1 = $mod.c;',
  22331. '$mod.TObjHelper.SetSize.call($with1, true, $mod.TObjHelper.GetSize.call($with1, false) + 21);',
  22332. '$with1.SetSpeed(true, $with1.GetSpeed(false) + 22);',
  22333. '$mod.TObjHelper.SetSize.call($with1, true, $mod.TObjHelper.GetSize.call($with1, false) + 23);',
  22334. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 11);',
  22335. '$mod.TBird.SetSpeed(true, $mod.TBird.GetSpeed(false) + 12);',
  22336. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 13);',
  22337. 'var $with2 = $mod.TBird;',
  22338. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 21);',
  22339. '$with2.SetSpeed(true, $with2.GetSpeed(false) + 22);',
  22340. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 23);',
  22341. '']));
  22342. end;
  22343. procedure TTestModule.TestClassHelper_ForIn;
  22344. begin
  22345. StartProgram(false);
  22346. Add([
  22347. 'type',
  22348. ' TObject = class end;',
  22349. ' TItem = TObject;',
  22350. ' TEnumerator = class',
  22351. ' FCurrent: TItem;',
  22352. ' property Current: TItem read FCurrent;',
  22353. ' function MoveNext: boolean;',
  22354. ' end;',
  22355. ' TBird = class',
  22356. ' end;',
  22357. ' TBirdHelper = class helper for TBird',
  22358. ' function GetEnumerator: TEnumerator;',
  22359. ' end;',
  22360. 'function TEnumerator.MoveNext: boolean;',
  22361. 'begin',
  22362. 'end;',
  22363. 'function TBirdHelper.GetEnumerator: TEnumerator;',
  22364. 'begin',
  22365. 'end;',
  22366. 'var',
  22367. ' b: TBird;',
  22368. ' i, i2: TItem;',
  22369. 'begin',
  22370. ' for i in b do i2:=i;']);
  22371. ConvertProgram;
  22372. CheckSource('TestClassHelper_ForIn',
  22373. LinesToStr([ // statements
  22374. 'rtl.createClass(this, "TObject", null, function () {',
  22375. ' this.$init = function () {',
  22376. ' };',
  22377. ' this.$final = function () {',
  22378. ' };',
  22379. '});',
  22380. 'rtl.createClass(this, "TEnumerator", this.TObject, function () {',
  22381. ' this.$init = function () {',
  22382. ' $mod.TObject.$init.call(this);',
  22383. ' this.FCurrent = null;',
  22384. ' };',
  22385. ' this.$final = function () {',
  22386. ' this.FCurrent = undefined;',
  22387. ' $mod.TObject.$final.call(this);',
  22388. ' };',
  22389. ' this.MoveNext = function () {',
  22390. ' var Result = false;',
  22391. ' return Result;',
  22392. ' };',
  22393. '});',
  22394. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  22395. '});',
  22396. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  22397. ' this.GetEnumerator = function () {',
  22398. ' var Result = null;',
  22399. ' return Result;',
  22400. ' };',
  22401. '});',
  22402. 'this.b = null;',
  22403. 'this.i = null;',
  22404. 'this.i2 = null;'
  22405. ]),
  22406. LinesToStr([ // $mod.$main
  22407. 'var $in = $mod.TBirdHelper.GetEnumerator.call($mod.b);',
  22408. 'try {',
  22409. ' while ($in.MoveNext()){',
  22410. ' $mod.i = $in.FCurrent;',
  22411. ' $mod.i2 = $mod.i;',
  22412. ' }',
  22413. '} finally {',
  22414. ' $in = rtl.freeLoc($in)',
  22415. '};',
  22416. '']));
  22417. end;
  22418. procedure TTestModule.TestClassHelper_PassProperty;
  22419. begin
  22420. StartProgram(false);
  22421. Add([
  22422. 'type',
  22423. ' TObject = class',
  22424. ' FField: TObject;',
  22425. ' property Field: TObject read FField write FField;',
  22426. ' end;',
  22427. ' THelper = class helper for TObject',
  22428. ' procedure Fly;',
  22429. ' class procedure Run;',
  22430. ' class procedure Jump; static;',
  22431. ' end;',
  22432. 'procedure THelper.Fly;',
  22433. 'begin',
  22434. ' Field.Fly;',
  22435. ' Field.Run;',
  22436. ' Field.Jump;',
  22437. ' with Field do begin',
  22438. ' Fly;',
  22439. ' Run;',
  22440. ' Jump;',
  22441. ' end;',
  22442. 'end;',
  22443. 'class procedure THelper.Run;',
  22444. 'begin',
  22445. 'end;',
  22446. 'class procedure THelper.Jump;',
  22447. 'begin',
  22448. 'end;',
  22449. 'var',
  22450. ' b: TObject;',
  22451. 'begin',
  22452. ' b.Field.Fly;',
  22453. ' b.Field.Run;',
  22454. ' b.Field.Jump;',
  22455. ' with b do begin',
  22456. ' Field.Run;',
  22457. ' Field.Fly;',
  22458. ' Field.Jump;',
  22459. ' end;',
  22460. ' with b.Field do begin',
  22461. ' Run;',
  22462. ' Fly;',
  22463. ' Jump;',
  22464. ' end;',
  22465. '']);
  22466. ConvertProgram;
  22467. CheckSource('TestClassHelper_PassProperty',
  22468. LinesToStr([ // statements
  22469. 'rtl.createClass(this, "TObject", null, function () {',
  22470. ' this.$init = function () {',
  22471. ' this.FField = null;',
  22472. ' };',
  22473. ' this.$final = function () {',
  22474. ' this.FField = undefined;',
  22475. ' };',
  22476. '});',
  22477. 'rtl.createHelper(this, "THelper", null, function () {',
  22478. ' this.Fly = function () {',
  22479. ' $mod.THelper.Fly.call(this.FField);',
  22480. ' $mod.THelper.Run.call(this.FField.$class);',
  22481. ' $mod.THelper.Jump();',
  22482. ' var $with = this.FField;',
  22483. ' $mod.THelper.Fly.call($with);',
  22484. ' $mod.THelper.Run.call($with.$class);',
  22485. ' $mod.THelper.Jump();',
  22486. ' };',
  22487. ' this.Run = function () {',
  22488. ' };',
  22489. ' this.Jump = function () {',
  22490. ' };',
  22491. '});',
  22492. 'this.b = null;',
  22493. '']),
  22494. LinesToStr([ // $mod.$main
  22495. '$mod.THelper.Fly.call($mod.b.FField);',
  22496. '$mod.THelper.Run.call($mod.b.FField.$class);',
  22497. '$mod.THelper.Jump();',
  22498. 'var $with = $mod.b;',
  22499. '$mod.THelper.Run.call($with.FField.$class);',
  22500. '$mod.THelper.Fly.call($with.FField);',
  22501. '$mod.THelper.Jump();',
  22502. 'var $with1 = $mod.b.FField;',
  22503. '$mod.THelper.Run.call($with1.$class);',
  22504. '$mod.THelper.Fly.call($with1);',
  22505. '$mod.THelper.Jump();',
  22506. '']));
  22507. end;
  22508. procedure TTestModule.TestExtClassHelper_ClassVar;
  22509. begin
  22510. StartProgram(false);
  22511. Add([
  22512. '{$modeswitch externalclass}',
  22513. 'type',
  22514. ' TExtA = class external name ''ExtObj''',
  22515. ' end;',
  22516. ' THelper = class helper for TExtA',
  22517. ' const',
  22518. ' One = 1;',
  22519. ' Two: word = 2;',
  22520. ' class var',
  22521. ' Glob: word;',
  22522. ' function Foo(w: word): word;',
  22523. ' class function Bar(w: word): word; static;',
  22524. ' end;',
  22525. 'function THelper.foo(w: word): word;',
  22526. 'begin',
  22527. ' Result:=w;',
  22528. ' Two:=One+w;',
  22529. ' Glob:=Glob;',
  22530. ' Result:=Self.Glob;',
  22531. ' Self.Glob:=Self.Glob;',
  22532. ' with Self do Glob:=Glob;',
  22533. 'end;',
  22534. 'class function THelper.bar(w: word): word;',
  22535. 'begin',
  22536. ' Result:=w;',
  22537. ' Two:=One;',
  22538. ' Glob:=Glob;',
  22539. 'end;',
  22540. 'var o: TExtA;',
  22541. 'begin',
  22542. ' texta.two:=texta.one;',
  22543. ' texta.Glob:=texta.Glob;',
  22544. ' with texta do begin',
  22545. ' two:=one;',
  22546. ' Glob:=Glob;',
  22547. ' end;',
  22548. ' o.two:=o.one;',
  22549. ' o.Glob:=o.Glob;',
  22550. ' with o do begin',
  22551. ' two:=one;',
  22552. ' Glob:=Glob;',
  22553. ' end;',
  22554. '']);
  22555. ConvertProgram;
  22556. CheckSource('TestExtClassHelper_ClassVar',
  22557. LinesToStr([ // statements
  22558. 'rtl.createHelper(this, "THelper", null, function () {',
  22559. ' this.One = 1;',
  22560. ' this.Two = 2;',
  22561. ' this.Glob = 0;',
  22562. ' this.Foo = function (w) {',
  22563. ' var Result = 0;',
  22564. ' Result = w;',
  22565. ' $mod.THelper.Two = 1 + w;',
  22566. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22567. ' Result = $mod.THelper.Glob;',
  22568. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22569. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22570. ' return Result;',
  22571. ' };',
  22572. ' this.Bar = function (w) {',
  22573. ' var Result = 0;',
  22574. ' Result = w;',
  22575. ' $mod.THelper.Two = 1;',
  22576. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22577. ' return Result;',
  22578. ' };',
  22579. '});',
  22580. 'this.o = null;',
  22581. '']),
  22582. LinesToStr([ // $mod.$main
  22583. '$mod.THelper.Two = 1;',
  22584. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22585. '$mod.THelper.Two = 1;',
  22586. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22587. '$mod.THelper.Two = 1;',
  22588. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22589. 'var $with = $mod.o;',
  22590. '$mod.THelper.Two = 1;',
  22591. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22592. '']));
  22593. end;
  22594. procedure TTestModule.TestExtClassHelper_Method_Call;
  22595. begin
  22596. StartProgram(false);
  22597. Add([
  22598. '{$modeswitch externalclass}',
  22599. 'type',
  22600. ' TFly = function(w: word): word of object;',
  22601. ' TExtA = class external name ''ExtObj''',
  22602. ' procedure Run(w: word = 10);',
  22603. ' end;',
  22604. ' THelper = class helper for TExtA',
  22605. ' function Foo(w: word = 1): word;',
  22606. ' function Fly(w: word = 2): word; external name ''Fly'';',
  22607. ' end;',
  22608. 'var p: TFly;',
  22609. 'function THelper.foo(w: word): word;',
  22610. 'begin',
  22611. ' Run;',
  22612. ' Run();',
  22613. ' Run(11);',
  22614. ' Foo;',
  22615. ' Foo();',
  22616. ' Foo(12);',
  22617. ' Self.Foo;',
  22618. ' Self.Foo();',
  22619. ' Self.Foo(13);',
  22620. ' Fly;',
  22621. ' Fly();',
  22622. ' with Self do begin',
  22623. ' Foo;',
  22624. ' Foo();',
  22625. ' Foo(14);',
  22626. ' Fly;',
  22627. ' Fly();',
  22628. ' end;',
  22629. ' p:=@Fly;',
  22630. 'end;',
  22631. 'var Obj: TExtA;',
  22632. 'begin',
  22633. ' obj.Foo;',
  22634. ' obj.Foo();',
  22635. ' obj.Foo(21);',
  22636. ' obj.Fly;',
  22637. ' obj.Fly();',
  22638. ' with obj do begin',
  22639. ' Foo;',
  22640. ' Foo();',
  22641. ' Foo(22);',
  22642. ' Fly;',
  22643. ' Fly();',
  22644. ' end;',
  22645. ' p:[email protected];',
  22646. '']);
  22647. ConvertProgram;
  22648. CheckSource('TestExtClassHelper_Method_Call',
  22649. LinesToStr([ // statements
  22650. 'rtl.createHelper(this, "THelper", null, function () {',
  22651. ' this.Foo = function (w) {',
  22652. ' var Result = 0;',
  22653. ' this.Run(10);',
  22654. ' this.Run(10);',
  22655. ' this.Run(11);',
  22656. ' $mod.THelper.Foo.call(this, 1);',
  22657. ' $mod.THelper.Foo.call(this, 1);',
  22658. ' $mod.THelper.Foo.call(this, 12);',
  22659. ' $mod.THelper.Foo.call(this, 1);',
  22660. ' $mod.THelper.Foo.call(this, 1);',
  22661. ' $mod.THelper.Foo.call(this, 13);',
  22662. ' this.Fly(2);',
  22663. ' this.Fly(2);',
  22664. ' $mod.THelper.Foo.call(this, 1);',
  22665. ' $mod.THelper.Foo.call(this, 1);',
  22666. ' $mod.THelper.Foo.call(this, 14);',
  22667. ' this.Fly(2);',
  22668. ' this.Fly(2);',
  22669. ' $mod.p = rtl.createCallback(this, "Fly");',
  22670. ' return Result;',
  22671. ' };',
  22672. '});',
  22673. 'this.p = null;',
  22674. 'this.Obj = null;',
  22675. '']),
  22676. LinesToStr([ // $mod.$main
  22677. '$mod.THelper.Foo.call($mod.Obj, 1);',
  22678. '$mod.THelper.Foo.call($mod.Obj, 1);',
  22679. '$mod.THelper.Foo.call($mod.Obj, 21);',
  22680. '$mod.Obj.Fly(2);',
  22681. '$mod.Obj.Fly(2);',
  22682. 'var $with = $mod.Obj;',
  22683. '$mod.THelper.Foo.call($with, 1);',
  22684. '$mod.THelper.Foo.call($with, 1);',
  22685. '$mod.THelper.Foo.call($with, 22);',
  22686. '$with.Fly(2);',
  22687. '$with.Fly(2);',
  22688. '$mod.p = rtl.createCallback($mod.Obj, "Fly");',
  22689. '']));
  22690. end;
  22691. procedure TTestModule.TestExtClassHelper_ClassMethod_MissingStatic;
  22692. begin
  22693. StartProgram(false);
  22694. Add([
  22695. '{$modeswitch externalclass}',
  22696. 'type',
  22697. ' TExtA = class external name ''ExtObj''',
  22698. ' procedure Run(w: word = 10);',
  22699. ' end;',
  22700. ' THelper = class helper for TExtA',
  22701. ' class procedure Fly;',
  22702. ' end;',
  22703. 'class procedure THelper.Fly;',
  22704. 'begin end;',
  22705. 'begin',
  22706. '']);
  22707. SetExpectedPasResolverError(sHelperClassMethodForExtClassMustBeStatic,
  22708. nHelperClassMethodForExtClassMustBeStatic);
  22709. ConvertProgram;
  22710. end;
  22711. procedure TTestModule.TestRecordHelper_ClassVar;
  22712. begin
  22713. StartProgram(false);
  22714. Add([
  22715. 'type',
  22716. ' TRec = record',
  22717. ' end;',
  22718. ' THelper = record helper for TRec',
  22719. ' const',
  22720. ' One = 1;',
  22721. ' Two: word = 2;',
  22722. ' class var',
  22723. ' Glob: word;',
  22724. ' function Foo(w: word): word;',
  22725. ' class function Bar(w: word): word; static;',
  22726. ' end;',
  22727. 'function THelper.foo(w: word): word;',
  22728. 'begin',
  22729. ' Result:=w;',
  22730. ' Two:=One+w;',
  22731. ' Glob:=Glob;',
  22732. ' Result:=Self.Glob;',
  22733. ' Self.Glob:=Self.Glob;',
  22734. ' with Self do Glob:=Glob;',
  22735. ' Self:=Self;',
  22736. 'end;',
  22737. 'class function THelper.bar(w: word): word;',
  22738. 'begin',
  22739. ' Result:=w;',
  22740. ' Two:=One;',
  22741. ' Glob:=Glob;',
  22742. 'end;',
  22743. 'var r: TRec;',
  22744. 'begin',
  22745. ' trec.two:=trec.one;',
  22746. ' trec.Glob:=trec.Glob;',
  22747. ' with trec do begin',
  22748. ' two:=one;',
  22749. ' Glob:=Glob;',
  22750. ' end;',
  22751. ' r.two:=r.one;',
  22752. ' r.Glob:=r.Glob;',
  22753. ' with r do begin',
  22754. ' two:=one;',
  22755. ' Glob:=Glob;',
  22756. ' end;',
  22757. '']);
  22758. ConvertProgram;
  22759. CheckSource('TestRecordHelper_ClassVar',
  22760. LinesToStr([ // statements
  22761. 'rtl.recNewT(this, "TRec", function () {',
  22762. ' this.$eq = function (b) {',
  22763. ' return true;',
  22764. ' };',
  22765. ' this.$assign = function (s) {',
  22766. ' return this;',
  22767. ' };',
  22768. '});',
  22769. 'rtl.createHelper(this, "THelper", null, function () {',
  22770. ' this.One = 1;',
  22771. ' this.Two = 2;',
  22772. ' this.Glob = 0;',
  22773. ' this.Foo = function (w) {',
  22774. ' var Result = 0;',
  22775. ' Result = w;',
  22776. ' $mod.THelper.Two = 1 + w;',
  22777. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22778. ' Result = $mod.THelper.Glob;',
  22779. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22780. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22781. ' this.$assign(this);',
  22782. ' return Result;',
  22783. ' };',
  22784. ' this.Bar = function (w) {',
  22785. ' var Result = 0;',
  22786. ' Result = w;',
  22787. ' $mod.THelper.Two = 1;',
  22788. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22789. ' return Result;',
  22790. ' };',
  22791. '});',
  22792. 'this.r = this.TRec.$new();',
  22793. '']),
  22794. LinesToStr([ // $mod.$main
  22795. '$mod.THelper.Two = 1;',
  22796. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22797. 'var $with = $mod.TRec;',
  22798. '$mod.THelper.Two = 1;',
  22799. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22800. '$mod.THelper.Two = 1;',
  22801. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22802. 'var $with1 = $mod.r;',
  22803. '$mod.THelper.Two = 1;',
  22804. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22805. '']));
  22806. end;
  22807. procedure TTestModule.TestRecordHelper_Method_Call;
  22808. begin
  22809. StartProgram(false);
  22810. Add([
  22811. '{$modeswitch AdvancedRecords}',
  22812. 'type',
  22813. ' TRec = record',
  22814. ' procedure Run(w: word = 10);',
  22815. ' end;',
  22816. ' THelper = record helper for TRec',
  22817. ' function Foo(w: word = 1): word;',
  22818. ' end;',
  22819. 'procedure TRec.Run(w: word);',
  22820. 'begin',
  22821. ' Foo;',
  22822. ' Foo();',
  22823. ' Foo(2);',
  22824. ' Self.Foo;',
  22825. ' Self.Foo();',
  22826. ' Self.Foo(3);',
  22827. ' with Self do begin',
  22828. ' Foo;',
  22829. ' Foo();',
  22830. ' Foo(4);',
  22831. ' end;',
  22832. 'end;',
  22833. 'function THelper.foo(w: word): word;',
  22834. 'begin',
  22835. ' Run;',
  22836. ' Run();',
  22837. ' Run(11);',
  22838. ' Foo;',
  22839. ' Foo();',
  22840. ' Foo(12);',
  22841. ' Self.Foo;',
  22842. ' Self.Foo();',
  22843. ' Self.Foo(13);',
  22844. ' with Self do begin',
  22845. ' Foo;',
  22846. ' Foo();',
  22847. ' Foo(14);',
  22848. ' end;',
  22849. 'end;',
  22850. 'var Rec: TRec;',
  22851. 'begin',
  22852. ' Rec.Foo;',
  22853. ' Rec.Foo();',
  22854. ' Rec.Foo(21);',
  22855. ' with Rec do begin',
  22856. ' Foo;',
  22857. ' Foo();',
  22858. ' Foo(22);',
  22859. ' end;',
  22860. '']);
  22861. ConvertProgram;
  22862. CheckSource('TestRecordHelper_Method_Call',
  22863. LinesToStr([ // statements
  22864. 'rtl.recNewT(this, "TRec", function () {',
  22865. ' this.$eq = function (b) {',
  22866. ' return true;',
  22867. ' };',
  22868. ' this.$assign = function (s) {',
  22869. ' return this;',
  22870. ' };',
  22871. ' this.Run = function (w) {',
  22872. ' $mod.THelper.Foo.call(this, 1);',
  22873. ' $mod.THelper.Foo.call(this, 1);',
  22874. ' $mod.THelper.Foo.call(this, 2);',
  22875. ' $mod.THelper.Foo.call(this, 1);',
  22876. ' $mod.THelper.Foo.call(this, 1);',
  22877. ' $mod.THelper.Foo.call(this, 3);',
  22878. ' $mod.THelper.Foo.call(this, 1);',
  22879. ' $mod.THelper.Foo.call(this, 1);',
  22880. ' $mod.THelper.Foo.call(this, 4);',
  22881. ' };',
  22882. '});',
  22883. 'rtl.createHelper(this, "THelper", null, function () {',
  22884. ' this.Foo = function (w) {',
  22885. ' var Result = 0;',
  22886. ' this.Run(10);',
  22887. ' this.Run(10);',
  22888. ' this.Run(11);',
  22889. ' $mod.THelper.Foo.call(this, 1);',
  22890. ' $mod.THelper.Foo.call(this, 1);',
  22891. ' $mod.THelper.Foo.call(this, 12);',
  22892. ' $mod.THelper.Foo.call(this, 1);',
  22893. ' $mod.THelper.Foo.call(this, 1);',
  22894. ' $mod.THelper.Foo.call(this, 13);',
  22895. ' $mod.THelper.Foo.call(this, 1);',
  22896. ' $mod.THelper.Foo.call(this, 1);',
  22897. ' $mod.THelper.Foo.call(this, 14);',
  22898. ' return Result;',
  22899. ' };',
  22900. '});',
  22901. 'this.Rec = this.TRec.$new();',
  22902. '']),
  22903. LinesToStr([ // $mod.$main
  22904. '$mod.THelper.Foo.call($mod.Rec, 1);',
  22905. '$mod.THelper.Foo.call($mod.Rec, 1);',
  22906. '$mod.THelper.Foo.call($mod.Rec, 21);',
  22907. 'var $with = $mod.Rec;',
  22908. '$mod.THelper.Foo.call($with, 1);',
  22909. '$mod.THelper.Foo.call($with, 1);',
  22910. '$mod.THelper.Foo.call($with, 22);',
  22911. '']));
  22912. end;
  22913. procedure TTestModule.TestRecordHelper_Constructor;
  22914. begin
  22915. StartProgram(false);
  22916. Add([
  22917. '{$modeswitch AdvancedRecords}',
  22918. 'type',
  22919. ' TRec = record',
  22920. ' constructor Create(w: word);',
  22921. ' end;',
  22922. ' THelper = record helper for TRec',
  22923. ' constructor NewHlp(w: word);',
  22924. ' end;',
  22925. 'var',
  22926. ' Rec: TRec;',
  22927. 'constructor TRec.Create(w: word);',
  22928. 'begin',
  22929. ' NewHlp(2);', // normal call
  22930. ' trec.NewHlp(3);', // new instance
  22931. 'end;',
  22932. 'constructor THelper.NewHlp(w: word);',
  22933. 'begin',
  22934. ' create(2);', // normal call
  22935. ' trec.create(3);', // new instance
  22936. ' NewHlp(4);', // normal call
  22937. ' trec.NewHlp(5);', // new instance
  22938. 'end;',
  22939. 'begin',
  22940. ' rec.newhlp(2);', // normal call
  22941. ' with rec do newhlp(12);', // normal call
  22942. ' trec.newhlp(3);', // new instance
  22943. ' with trec do newhlp(13);', // new instance
  22944. '']);
  22945. ConvertProgram;
  22946. CheckSource('TestRecordHelper_Constructor',
  22947. LinesToStr([ // statements
  22948. 'rtl.recNewT(this, "TRec", function () {',
  22949. ' this.$eq = function (b) {',
  22950. ' return true;',
  22951. ' };',
  22952. ' this.$assign = function (s) {',
  22953. ' return this;',
  22954. ' };',
  22955. ' this.Create = function (w) {',
  22956. ' $mod.THelper.NewHlp.call(this, 2);',
  22957. ' $mod.THelper.$new("NewHlp", [3]);',
  22958. ' return this;',
  22959. ' };',
  22960. '});',
  22961. 'rtl.createHelper(this, "THelper", null, function () {',
  22962. ' this.NewHlp = function (w) {',
  22963. ' this.Create(2);',
  22964. ' $mod.TRec.$new().Create(3);',
  22965. ' $mod.THelper.NewHlp.call(this, 4);',
  22966. ' $mod.THelper.$new("NewHlp", [5]);',
  22967. ' return this;',
  22968. ' };',
  22969. ' this.$new = function (fn, args) {',
  22970. ' return this[fn].apply($mod.TRec.$new(), args);',
  22971. ' };',
  22972. '});',
  22973. 'this.Rec = this.TRec.$new();',
  22974. '']),
  22975. LinesToStr([ // $mod.$main
  22976. '$mod.THelper.NewHlp.call($mod.Rec, 2);',
  22977. 'var $with = $mod.Rec;',
  22978. '$mod.THelper.NewHlp.call($with, 12);',
  22979. '$mod.THelper.$new("NewHlp", [3]);',
  22980. 'var $with1 = $mod.TRec;',
  22981. '$mod.THelper.$new("NewHlp", [13]);',
  22982. '']));
  22983. end;
  22984. procedure TTestModule.TestTypeHelper_ClassVar;
  22985. begin
  22986. StartProgram(false);
  22987. Add([
  22988. '{$modeswitch typehelpers}',
  22989. 'type',
  22990. ' THelper = type helper for byte',
  22991. ' const',
  22992. ' One = 1;',
  22993. ' Two: word = 2;',
  22994. ' class var',
  22995. ' Glob: word;',
  22996. ' function Foo(w: word): word;',
  22997. ' class function Bar(w: word): word; static;',
  22998. ' end;',
  22999. 'function THelper.foo(w: word): word;',
  23000. 'begin',
  23001. ' Result:=w;',
  23002. ' Two:=One+w;',
  23003. ' Glob:=Glob;',
  23004. ' Result:=Self.Glob;',
  23005. ' Self.Glob:=Self.Glob;',
  23006. ' with Self do Glob:=Glob;',
  23007. 'end;',
  23008. 'class function THelper.bar(w: word): word;',
  23009. 'begin',
  23010. ' Result:=w;',
  23011. ' Two:=One;',
  23012. ' Glob:=Glob;',
  23013. 'end;',
  23014. 'var b: byte;',
  23015. 'begin',
  23016. ' byte.two:=byte.one;',
  23017. ' byte.Glob:=byte.Glob;',
  23018. ' with byte do begin',
  23019. ' two:=one;',
  23020. ' Glob:=Glob;',
  23021. ' end;',
  23022. ' b.two:=b.one;',
  23023. ' b.Glob:=b.Glob;',
  23024. ' with b do begin',
  23025. ' two:=one;',
  23026. ' Glob:=Glob;',
  23027. ' end;',
  23028. '']);
  23029. ConvertProgram;
  23030. CheckSource('TestTypeHelper_ClassVar',
  23031. LinesToStr([ // statements
  23032. 'rtl.createHelper(this, "THelper", null, function () {',
  23033. ' this.One = 1;',
  23034. ' this.Two = 2;',
  23035. ' this.Glob = 0;',
  23036. ' this.Foo = function (w) {',
  23037. ' var Result = 0;',
  23038. ' Result = w;',
  23039. ' $mod.THelper.Two = 1 + w;',
  23040. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  23041. ' Result = $mod.THelper.Glob;',
  23042. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  23043. ' var $with = this.get();',
  23044. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  23045. ' return Result;',
  23046. ' };',
  23047. ' this.Bar = function (w) {',
  23048. ' var Result = 0;',
  23049. ' Result = w;',
  23050. ' $mod.THelper.Two = 1;',
  23051. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  23052. ' return Result;',
  23053. ' };',
  23054. '});',
  23055. 'this.b = 0;',
  23056. '']),
  23057. LinesToStr([ // $mod.$main
  23058. '$mod.THelper.Two = 1;',
  23059. '$mod.THelper.Glob = $mod.THelper.Glob;',
  23060. '$mod.THelper.Two = 1;',
  23061. '$mod.THelper.Glob = $mod.THelper.Glob;',
  23062. '$mod.THelper.Two = 1;',
  23063. '$mod.THelper.Glob = $mod.THelper.Glob;',
  23064. 'var $with = $mod.b;',
  23065. '$mod.THelper.Two = 1;',
  23066. '$mod.THelper.Glob = $mod.THelper.Glob;',
  23067. '']));
  23068. end;
  23069. procedure TTestModule.TestTypeHelper_PassResultElement;
  23070. begin
  23071. StartProgram(false);
  23072. Add([
  23073. '{$modeswitch typehelpers}',
  23074. 'type',
  23075. ' THelper = type helper for word',
  23076. ' procedure DoIt(e: byte = 123);',
  23077. ' class procedure DoSome(e: byte = 456); static;',
  23078. ' end;',
  23079. 'procedure THelper.DoIt(e: byte);',
  23080. 'begin',
  23081. 'end;',
  23082. 'class procedure THelper.DoSome(e: byte);',
  23083. 'begin',
  23084. 'end;',
  23085. 'function Foo(w: word): word;',
  23086. 'begin',
  23087. ' Result.DoIt;',
  23088. ' Result.DoIt();',
  23089. ' Result.DoSome;',
  23090. ' Result.DoSome();',
  23091. ' with Result do begin',
  23092. ' DoIt;',
  23093. ' DoIt();',
  23094. ' DoSome;',
  23095. ' DoSome();',
  23096. ' end;',
  23097. 'end;',
  23098. 'begin',
  23099. '']);
  23100. ConvertProgram;
  23101. CheckSource('TestTypeHelper_PassResultElement',
  23102. LinesToStr([ // statements
  23103. 'rtl.createHelper(this, "THelper", null, function () {',
  23104. ' this.DoIt = function (e) {',
  23105. ' };',
  23106. ' this.DoSome = function (e) {',
  23107. ' };',
  23108. '});',
  23109. 'this.Foo = function (w) {',
  23110. ' var Result = 0;',
  23111. ' $mod.THelper.DoIt.call({',
  23112. ' get: function () {',
  23113. ' return Result;',
  23114. ' },',
  23115. ' set: function (v) {',
  23116. ' Result = v;',
  23117. ' }',
  23118. ' }, 123);',
  23119. ' $mod.THelper.DoIt.call({',
  23120. ' get: function () {',
  23121. ' return Result;',
  23122. ' },',
  23123. ' set: function (v) {',
  23124. ' Result = v;',
  23125. ' }',
  23126. ' }, 123);',
  23127. ' $mod.THelper.DoSome(456);',
  23128. ' $mod.THelper.DoSome(456);',
  23129. ' $mod.THelper.DoIt.call({',
  23130. ' get: function () {',
  23131. ' return Result;',
  23132. ' },',
  23133. ' set: function (v) {',
  23134. ' Result = v;',
  23135. ' }',
  23136. ' }, 123);',
  23137. ' $mod.THelper.DoIt.call({',
  23138. ' get: function () {',
  23139. ' return Result;',
  23140. ' },',
  23141. ' set: function (v) {',
  23142. ' Result = v;',
  23143. ' }',
  23144. ' }, 123);',
  23145. ' $mod.THelper.DoSome(456);',
  23146. ' $mod.THelper.DoSome(456);',
  23147. ' return Result;',
  23148. '};',
  23149. '']),
  23150. LinesToStr([ // $mod.$main
  23151. '']));
  23152. end;
  23153. procedure TTestModule.TestTypeHelper_PassArgs;
  23154. begin
  23155. StartProgram(false);
  23156. Add([
  23157. '{$modeswitch typehelpers}',
  23158. 'type',
  23159. ' THelper = type helper for word',
  23160. ' procedure DoIt(e: byte = 123);',
  23161. ' end;',
  23162. 'procedure THelper.DoIt(e: byte);',
  23163. 'begin',
  23164. 'end;',
  23165. 'procedure FooDefault(a: word);',
  23166. 'begin',
  23167. ' a.DoIt;',
  23168. ' with a do DoIt;',
  23169. 'end;',
  23170. 'procedure FooConst(const a: word);',
  23171. 'begin',
  23172. ' a.DoIt;',
  23173. ' with a do DoIt;',
  23174. 'end;',
  23175. 'procedure FooVar(var a: word);',
  23176. 'begin',
  23177. ' a.DoIt;',
  23178. ' with a do DoIt;',
  23179. 'end;',
  23180. 'begin',
  23181. '']);
  23182. ConvertProgram;
  23183. CheckSource('TestTypeHelper_PassArgs',
  23184. LinesToStr([ // statements
  23185. 'rtl.createHelper(this, "THelper", null, function () {',
  23186. ' this.DoIt = function (e) {',
  23187. ' };',
  23188. '});',
  23189. 'this.FooDefault = function (a) {',
  23190. ' $mod.THelper.DoIt.call({',
  23191. ' get: function () {',
  23192. ' return a;',
  23193. ' },',
  23194. ' set: function (v) {',
  23195. ' a = v;',
  23196. ' }',
  23197. ' }, 123);',
  23198. ' $mod.THelper.DoIt.call({',
  23199. ' get: function () {',
  23200. ' return a;',
  23201. ' },',
  23202. ' set: function (v) {',
  23203. ' a = v;',
  23204. ' }',
  23205. ' }, 123);',
  23206. '};',
  23207. 'this.FooConst = function (a) {',
  23208. ' $mod.THelper.DoIt.call({',
  23209. ' get: function () {',
  23210. ' return a;',
  23211. ' },',
  23212. ' set: function (v) {',
  23213. ' rtl.raiseE("EPropReadOnly");',
  23214. ' }',
  23215. ' }, 123);',
  23216. ' $mod.THelper.DoIt.call({',
  23217. ' get: function () {',
  23218. ' return a;',
  23219. ' },',
  23220. ' set: function () {',
  23221. ' rtl.raiseE("EPropReadOnly");',
  23222. ' }',
  23223. ' }, 123);',
  23224. '};',
  23225. 'this.FooVar = function (a) {',
  23226. ' $mod.THelper.DoIt.call(a, 123);',
  23227. ' var $with = a.get();',
  23228. ' $mod.THelper.DoIt.call(a, 123);',
  23229. '};',
  23230. '']),
  23231. LinesToStr([ // $mod.$main
  23232. '']));
  23233. end;
  23234. procedure TTestModule.TestTypeHelper_PassVarConst;
  23235. begin
  23236. StartProgram(false);
  23237. Add([
  23238. '{$modeswitch typehelpers}',
  23239. 'type',
  23240. ' THelper = type helper for word',
  23241. ' procedure DoIt(e: byte = 123);',
  23242. ' end;',
  23243. 'procedure THelper.DoIt(e: byte);',
  23244. 'begin',
  23245. 'end;',
  23246. 'var a: word;',
  23247. 'const c: word = 2;',
  23248. '{$writeableconst off}',
  23249. 'const r: word = 3;',
  23250. 'begin',
  23251. ' a.DoIt;',
  23252. ' with a do DoIt;',
  23253. ' c.DoIt;',
  23254. ' with c do DoIt;',
  23255. ' r.DoIt;',
  23256. ' with r do DoIt;',
  23257. '']);
  23258. ConvertProgram;
  23259. CheckSource('TestTypeHelper_PassVarConst',
  23260. LinesToStr([ // statements
  23261. 'rtl.createHelper(this, "THelper", null, function () {',
  23262. ' this.DoIt = function (e) {',
  23263. ' };',
  23264. '});',
  23265. 'this.a = 0;',
  23266. 'this.c = 2;',
  23267. 'this.r = 3;',
  23268. '']),
  23269. LinesToStr([ // $mod.$main
  23270. '$mod.THelper.DoIt.call({',
  23271. ' p: $mod,',
  23272. ' get: function () {',
  23273. ' return this.p.a;',
  23274. ' },',
  23275. ' set: function (v) {',
  23276. ' this.p.a = v;',
  23277. ' }',
  23278. '}, 123);',
  23279. 'var $with = $mod.a;',
  23280. '$mod.THelper.DoIt.call({',
  23281. ' get: function () {',
  23282. ' return $with;',
  23283. ' },',
  23284. ' set: function (v) {',
  23285. ' $with = v;',
  23286. ' }',
  23287. '}, 123);',
  23288. '$mod.THelper.DoIt.call({',
  23289. ' p: $mod,',
  23290. ' get: function () {',
  23291. ' return this.p.c;',
  23292. ' },',
  23293. ' set: function (v) {',
  23294. ' this.p.c = v;',
  23295. ' }',
  23296. '}, 123);',
  23297. 'var $with1 = $mod.c;',
  23298. '$mod.THelper.DoIt.call({',
  23299. ' get: function () {',
  23300. ' return $with1;',
  23301. ' },',
  23302. ' set: function (v) {',
  23303. ' $with1 = v;',
  23304. ' }',
  23305. '}, 123);',
  23306. '$mod.THelper.DoIt.call({',
  23307. ' get: function () {',
  23308. ' return 3;',
  23309. ' },',
  23310. ' set: function (v) {',
  23311. ' rtl.raiseE("EPropReadOnly");',
  23312. ' }',
  23313. '}, 123);',
  23314. 'var $with2 = 3;',
  23315. ' $mod.THelper.DoIt.call({',
  23316. ' get: function () {',
  23317. ' return $with2;',
  23318. ' },',
  23319. ' set: function () {',
  23320. ' rtl.raiseE("EPropReadOnly");',
  23321. ' }',
  23322. ' }, 123);',
  23323. '']));
  23324. end;
  23325. procedure TTestModule.TestTypeHelper_PassFuncResult;
  23326. begin
  23327. StartProgram(false);
  23328. Add([
  23329. '{$modeswitch typehelpers}',
  23330. 'type',
  23331. ' THelper = type helper for word',
  23332. ' procedure DoIt(e: byte = 123);',
  23333. ' end;',
  23334. 'procedure THelper.DoIt(e: byte);',
  23335. 'begin',
  23336. 'end;',
  23337. 'function Foo(b: byte = 1): word;',
  23338. 'begin',
  23339. 'end;',
  23340. 'begin',
  23341. ' Foo.DoIt;',
  23342. ' Foo().DoIt;',
  23343. ' with Foo do DoIt;',
  23344. ' with Foo() do DoIt;',
  23345. '']);
  23346. ConvertProgram;
  23347. CheckSource('TestTypeHelper_PassFuncResult',
  23348. LinesToStr([ // statements
  23349. 'rtl.createHelper(this, "THelper", null, function () {',
  23350. ' this.DoIt = function (e) {',
  23351. ' };',
  23352. '});',
  23353. 'this.Foo = function (b) {',
  23354. ' var Result = 0;',
  23355. ' return Result;',
  23356. '};',
  23357. '']),
  23358. LinesToStr([ // $mod.$main
  23359. '$mod.THelper.DoIt.call({',
  23360. ' a: $mod.Foo(1),',
  23361. ' get: function () {',
  23362. ' return this.a;',
  23363. ' },',
  23364. ' set: function (v) {',
  23365. ' this.a = v;',
  23366. ' }',
  23367. '}, 123);',
  23368. '$mod.THelper.DoIt.call({',
  23369. ' a: $mod.Foo(1),',
  23370. ' get: function () {',
  23371. ' return this.a;',
  23372. ' },',
  23373. ' set: function (v) {',
  23374. ' this.a = v;',
  23375. ' }',
  23376. '}, 123);',
  23377. 'var $with = $mod.Foo(1);',
  23378. '$mod.THelper.DoIt.call({',
  23379. ' get: function () {',
  23380. ' return $with;',
  23381. ' },',
  23382. ' set: function (v) {',
  23383. ' $with = v;',
  23384. ' }',
  23385. '}, 123);',
  23386. 'var $with1 = $mod.Foo(1);',
  23387. '$mod.THelper.DoIt.call({',
  23388. ' get: function () {',
  23389. ' return $with1;',
  23390. ' },',
  23391. ' set: function (v) {',
  23392. ' $with1 = v;',
  23393. ' }',
  23394. '}, 123);',
  23395. '']));
  23396. end;
  23397. procedure TTestModule.TestTypeHelper_PassPropertyField;
  23398. begin
  23399. StartProgram(false);
  23400. Add([
  23401. '{$modeswitch typehelpers}',
  23402. 'type',
  23403. ' TObject = class',
  23404. ' FField: word;',
  23405. ' procedure SetField(Value: word);',
  23406. ' property Field: word read FField write SetField;',
  23407. ' end;',
  23408. ' THelper = type helper for word',
  23409. ' procedure Fly;',
  23410. ' class procedure Run; static;',
  23411. ' end;',
  23412. 'procedure TObject.SetField(Value: word);',
  23413. 'begin',
  23414. ' Field.Fly;',
  23415. ' Field.Run;',
  23416. ' Self.Field.Fly;',
  23417. ' Self.Field.Run;',
  23418. ' with Self do begin',
  23419. ' Field.Fly;',
  23420. ' Field.Run;',
  23421. ' end;',
  23422. ' with Self.Field do begin',
  23423. ' Fly;',
  23424. ' Run;',
  23425. ' end;',
  23426. 'end;',
  23427. 'procedure THelper.Fly;',
  23428. 'begin',
  23429. 'end;',
  23430. 'class procedure THelper.Run;',
  23431. 'begin',
  23432. 'end;',
  23433. 'var',
  23434. ' o: TObject;',
  23435. 'begin',
  23436. ' o.Field.Fly;',
  23437. ' o.Field.Run;',
  23438. ' with o do begin',
  23439. ' Field.Fly;',
  23440. ' Field.Run;',
  23441. ' end;',
  23442. ' with o.Field do begin',
  23443. ' Fly;',
  23444. ' Run;',
  23445. ' end;',
  23446. '']);
  23447. ConvertProgram;
  23448. CheckSource('TestTypeHelper_PassPropertyField',
  23449. LinesToStr([ // statements
  23450. 'rtl.createClass(this, "TObject", null, function () {',
  23451. ' this.$init = function () {',
  23452. ' this.FField = 0;',
  23453. ' };',
  23454. ' this.$final = function () {',
  23455. ' };',
  23456. ' this.SetField = function (Value) {',
  23457. ' $mod.THelper.Fly.call({',
  23458. ' p: this,',
  23459. ' get: function () {',
  23460. ' return this.p.FField;',
  23461. ' },',
  23462. ' set: function (v) {',
  23463. ' this.p.FField = v;',
  23464. ' }',
  23465. ' });',
  23466. ' $mod.THelper.Run();',
  23467. ' $mod.THelper.Fly.call({',
  23468. ' p: this,',
  23469. ' get: function () {',
  23470. ' return this.p.FField;',
  23471. ' },',
  23472. ' set: function (v) {',
  23473. ' this.p.FField = v;',
  23474. ' }',
  23475. ' });',
  23476. ' $mod.THelper.Run();',
  23477. ' $mod.THelper.Fly.call({',
  23478. ' p: this,',
  23479. ' get: function () {',
  23480. ' return this.p.FField;',
  23481. ' },',
  23482. ' set: function (v) {',
  23483. ' this.p.FField = v;',
  23484. ' }',
  23485. ' });',
  23486. ' $mod.THelper.Run();',
  23487. ' var $with = this.FField;',
  23488. ' $mod.THelper.Fly.call({',
  23489. ' get: function () {',
  23490. ' return $with;',
  23491. ' },',
  23492. ' set: function (v) {',
  23493. ' $with = v;',
  23494. ' }',
  23495. ' });',
  23496. ' $mod.THelper.Run();',
  23497. ' };',
  23498. '});',
  23499. 'rtl.createHelper(this, "THelper", null, function () {',
  23500. ' this.Fly = function () {',
  23501. ' };',
  23502. ' this.Run = function () {',
  23503. ' };',
  23504. '});',
  23505. 'this.o = null;',
  23506. '']),
  23507. LinesToStr([ // $mod.$main
  23508. '$mod.THelper.Fly.call({',
  23509. ' p: $mod.o,',
  23510. ' get: function () {',
  23511. ' return this.p.FField;',
  23512. ' },',
  23513. ' set: function (v) {',
  23514. ' this.p.FField = v;',
  23515. ' }',
  23516. '});',
  23517. '$mod.THelper.Run();',
  23518. 'var $with = $mod.o;',
  23519. '$mod.THelper.Fly.call({',
  23520. ' p: $with,',
  23521. ' get: function () {',
  23522. ' return this.p.FField;',
  23523. ' },',
  23524. ' set: function (v) {',
  23525. ' this.p.FField = v;',
  23526. ' }',
  23527. '});',
  23528. '$mod.THelper.Run();',
  23529. 'var $with1 = $mod.o.FField;',
  23530. '$mod.THelper.Fly.call({',
  23531. ' get: function () {',
  23532. ' return $with1;',
  23533. ' },',
  23534. ' set: function (v) {',
  23535. ' $with1 = v;',
  23536. ' }',
  23537. '});',
  23538. '$mod.THelper.Run();',
  23539. '']));
  23540. end;
  23541. procedure TTestModule.TestTypeHelper_PassPropertyGetter;
  23542. begin
  23543. StartProgram(false);
  23544. Add([
  23545. '{$modeswitch typehelpers}',
  23546. 'type',
  23547. ' TObject = class',
  23548. ' FField: word;',
  23549. ' function GetField: word;',
  23550. ' property Field: word read GetField write FField;',
  23551. ' end;',
  23552. ' THelper = type helper for word',
  23553. ' procedure Fly;',
  23554. ' class procedure Run; static;',
  23555. ' end;',
  23556. 'function TObject.GetField: word;',
  23557. 'begin',
  23558. ' Field.Fly;',
  23559. ' Field.Run;',
  23560. ' Self.Field.Fly;',
  23561. ' Self.Field.Run;',
  23562. ' with Self do begin',
  23563. ' Field.Fly;',
  23564. ' Field.Run;',
  23565. ' end;',
  23566. ' with Self.Field do begin',
  23567. ' Fly;',
  23568. ' Run;',
  23569. ' end;',
  23570. 'end;',
  23571. 'procedure THelper.Fly;',
  23572. 'begin',
  23573. 'end;',
  23574. 'class procedure THelper.Run;',
  23575. 'begin',
  23576. 'end;',
  23577. 'var',
  23578. ' o: TObject;',
  23579. 'begin',
  23580. ' o.Field.Fly;',
  23581. ' o.Field.Run;',
  23582. ' with o do begin',
  23583. ' Field.Fly;',
  23584. ' Field.Run;',
  23585. ' end;',
  23586. ' with o.Field do begin',
  23587. ' Fly;',
  23588. ' Run;',
  23589. ' end;',
  23590. '']);
  23591. ConvertProgram;
  23592. CheckSource('TestTypeHelper_PassPropertyGetter',
  23593. LinesToStr([ // statements
  23594. 'rtl.createClass(this, "TObject", null, function () {',
  23595. ' this.$init = function () {',
  23596. ' this.FField = 0;',
  23597. ' };',
  23598. ' this.$final = function () {',
  23599. ' };',
  23600. ' this.GetField = function () {',
  23601. ' var Result = 0;',
  23602. ' $mod.THelper.Fly.call({',
  23603. ' p: this.GetField(),',
  23604. ' get: function () {',
  23605. ' return this.p;',
  23606. ' },',
  23607. ' set: function (v) {',
  23608. ' this.p = v;',
  23609. ' }',
  23610. ' });',
  23611. ' $mod.THelper.Run();',
  23612. ' $mod.THelper.Fly.call({',
  23613. ' p: this.GetField(),',
  23614. ' get: function () {',
  23615. ' return this.p;',
  23616. ' },',
  23617. ' set: function (v) {',
  23618. ' this.p = v;',
  23619. ' }',
  23620. ' });',
  23621. ' $mod.THelper.Run();',
  23622. ' $mod.THelper.Fly.call({',
  23623. ' p: this.GetField(),',
  23624. ' get: function () {',
  23625. ' return this.p;',
  23626. ' },',
  23627. ' set: function (v) {',
  23628. ' this.p = v;',
  23629. ' }',
  23630. ' });',
  23631. ' $mod.THelper.Run();',
  23632. ' var $with = this.GetField();',
  23633. ' $mod.THelper.Fly.call({',
  23634. ' get: function () {',
  23635. ' return $with;',
  23636. ' },',
  23637. ' set: function (v) {',
  23638. ' $with = v;',
  23639. ' }',
  23640. ' });',
  23641. ' $mod.THelper.Run();',
  23642. ' return Result;',
  23643. ' };',
  23644. '});',
  23645. 'rtl.createHelper(this, "THelper", null, function () {',
  23646. ' this.Fly = function () {',
  23647. ' };',
  23648. ' this.Run = function () {',
  23649. ' };',
  23650. '});',
  23651. 'this.o = null;',
  23652. '']),
  23653. LinesToStr([ // $mod.$main
  23654. '$mod.THelper.Fly.call({',
  23655. ' p: $mod.o.GetField(),',
  23656. ' get: function () {',
  23657. ' return this.p;',
  23658. ' },',
  23659. ' set: function (v) {',
  23660. ' this.p = v;',
  23661. ' }',
  23662. '});',
  23663. '$mod.THelper.Run();',
  23664. 'var $with = $mod.o;',
  23665. '$mod.THelper.Fly.call({',
  23666. ' p: $with.GetField(),',
  23667. ' get: function () {',
  23668. ' return this.p;',
  23669. ' },',
  23670. ' set: function (v) {',
  23671. ' this.p = v;',
  23672. ' }',
  23673. '});',
  23674. '$mod.THelper.Run();',
  23675. 'var $with1 = $mod.o.GetField();',
  23676. '$mod.THelper.Fly.call({',
  23677. ' get: function () {',
  23678. ' return $with1;',
  23679. ' },',
  23680. ' set: function (v) {',
  23681. ' $with1 = v;',
  23682. ' }',
  23683. '});',
  23684. '$mod.THelper.Run();',
  23685. '']));
  23686. end;
  23687. procedure TTestModule.TestTypeHelper_PassClassPropertyField;
  23688. begin
  23689. StartProgram(false);
  23690. Add([
  23691. '{$modeswitch typehelpers}',
  23692. 'type',
  23693. ' TObject = class',
  23694. ' class var FField: word;',
  23695. ' class procedure SetField(Value: word);',
  23696. ' class property Field: word read FField write SetField;',
  23697. ' end;',
  23698. ' THelper = type helper for word',
  23699. ' procedure Fly(n: byte);',
  23700. ' end;',
  23701. 'class procedure TObject.SetField(Value: word);',
  23702. 'begin',
  23703. ' Field.Fly(1);',
  23704. ' Self.Field.Fly(2);',
  23705. ' with Self do Field.Fly(3);',
  23706. ' with Self.Field do Fly(4);',
  23707. ' TObject.Field.Fly(5);',
  23708. ' with TObject do Field.Fly(6);',
  23709. ' with TObject.Field do Fly(7);',
  23710. 'end;',
  23711. 'procedure THelper.Fly(n: byte);',
  23712. 'begin',
  23713. 'end;',
  23714. 'var',
  23715. ' o: TObject;',
  23716. 'begin',
  23717. ' o.Field.Fly(11);',
  23718. ' with o do Field.Fly(12);',
  23719. ' with o.Field do Fly(13);',
  23720. ' TObject.Field.Fly(14);',
  23721. ' with TObject do Field.Fly(15);',
  23722. ' with TObject.Field do Fly(16);',
  23723. '']);
  23724. ConvertProgram;
  23725. CheckSource('TestTypeHelper_PassClassPropertyField',
  23726. LinesToStr([ // statements
  23727. 'rtl.createClass(this, "TObject", null, function () {',
  23728. ' this.FField = 0;',
  23729. ' this.$init = function () {',
  23730. ' };',
  23731. ' this.$final = function () {',
  23732. ' };',
  23733. ' this.SetField = function (Value) {',
  23734. ' $mod.THelper.Fly.call({',
  23735. ' p: this,',
  23736. ' get: function () {',
  23737. ' return this.p.FField;',
  23738. ' },',
  23739. ' set: function (v) {',
  23740. ' $mod.TObject.FField = v;',
  23741. ' }',
  23742. ' }, 1);',
  23743. ' $mod.THelper.Fly.call({',
  23744. ' p: this,',
  23745. ' get: function () {',
  23746. ' return this.p.FField;',
  23747. ' },',
  23748. ' set: function (v) {',
  23749. ' $mod.TObject.FField = v;',
  23750. ' }',
  23751. ' }, 2);',
  23752. ' $mod.THelper.Fly.call({',
  23753. ' p: this,',
  23754. ' get: function () {',
  23755. ' return this.p.FField;',
  23756. ' },',
  23757. ' set: function (v) {',
  23758. ' $mod.TObject.FField = v;',
  23759. ' }',
  23760. ' }, 3);',
  23761. ' var $with = this.FField;',
  23762. ' $mod.THelper.Fly.call({',
  23763. ' get: function () {',
  23764. ' return $with;',
  23765. ' },',
  23766. ' set: function (v) {',
  23767. ' $with = v;',
  23768. ' }',
  23769. ' }, 4);',
  23770. ' $mod.THelper.Fly.call({',
  23771. ' p: $mod.TObject,',
  23772. ' get: function () {',
  23773. ' return this.p.FField;',
  23774. ' },',
  23775. ' set: function (v) {',
  23776. ' $mod.TObject.FField = v;',
  23777. ' }',
  23778. ' }, 5);',
  23779. ' var $with1 = $mod.TObject;',
  23780. ' $mod.THelper.Fly.call({',
  23781. ' p: $with1,',
  23782. ' get: function () {',
  23783. ' return this.p.FField;',
  23784. ' },',
  23785. ' set: function (v) {',
  23786. ' $mod.TObject.FField = v;',
  23787. ' }',
  23788. ' }, 6);',
  23789. ' var $with2 = $mod.TObject.FField;',
  23790. ' $mod.THelper.Fly.call({',
  23791. ' get: function () {',
  23792. ' return $with2;',
  23793. ' },',
  23794. ' set: function (v) {',
  23795. ' $with2 = v;',
  23796. ' }',
  23797. ' }, 7);',
  23798. ' };',
  23799. '});',
  23800. 'rtl.createHelper(this, "THelper", null, function () {',
  23801. ' this.Fly = function (n) {',
  23802. ' };',
  23803. '});',
  23804. 'this.o = null;',
  23805. '']),
  23806. LinesToStr([ // $mod.$main
  23807. '$mod.THelper.Fly.call({',
  23808. ' p: $mod.o,',
  23809. ' get: function () {',
  23810. ' return this.p.FField;',
  23811. ' },',
  23812. ' set: function (v) {',
  23813. ' $mod.TObject.FField = v;',
  23814. ' }',
  23815. '}, 11);',
  23816. 'var $with = $mod.o;',
  23817. '$mod.THelper.Fly.call({',
  23818. ' p: $with,',
  23819. ' get: function () {',
  23820. ' return this.p.FField;',
  23821. ' },',
  23822. ' set: function (v) {',
  23823. ' $mod.TObject.FField = v;',
  23824. ' }',
  23825. '}, 12);',
  23826. 'var $with1 = $mod.o.FField;',
  23827. '$mod.THelper.Fly.call({',
  23828. ' get: function () {',
  23829. ' return $with1;',
  23830. ' },',
  23831. ' set: function (v) {',
  23832. ' $with1 = v;',
  23833. ' }',
  23834. '}, 13);',
  23835. '$mod.THelper.Fly.call({',
  23836. ' p: $mod.TObject,',
  23837. ' get: function () {',
  23838. ' return this.p.FField;',
  23839. ' },',
  23840. ' set: function (v) {',
  23841. ' $mod.TObject.FField = v;',
  23842. ' }',
  23843. '}, 14);',
  23844. 'var $with2 = $mod.TObject;',
  23845. '$mod.THelper.Fly.call({',
  23846. ' p: $with2,',
  23847. ' get: function () {',
  23848. ' return this.p.FField;',
  23849. ' },',
  23850. ' set: function (v) {',
  23851. ' $mod.TObject.FField = v;',
  23852. ' }',
  23853. '}, 15);',
  23854. 'var $with3 = $mod.TObject.FField;',
  23855. '$mod.THelper.Fly.call({',
  23856. ' get: function () {',
  23857. ' return $with3;',
  23858. ' },',
  23859. ' set: function (v) {',
  23860. ' $with3 = v;',
  23861. ' }',
  23862. '}, 16);',
  23863. '']));
  23864. end;
  23865. procedure TTestModule.TestTypeHelper_PassClassPropertyGetterStatic;
  23866. begin
  23867. StartProgram(false);
  23868. Add([
  23869. '{$modeswitch typehelpers}',
  23870. 'type',
  23871. ' TObject = class',
  23872. ' class var FField: word;',
  23873. ' class function GetField: word; static;',
  23874. ' class property Field: word read GetField write FField;',
  23875. ' end;',
  23876. ' THelper = type helper for word',
  23877. ' procedure Fly(n: byte);',
  23878. ' end;',
  23879. 'class function TObject.GetField: word;',
  23880. 'begin',
  23881. ' Field.Fly(1);',
  23882. ' TObject.Field.Fly(5);',
  23883. ' with TObject do Field.Fly(6);',
  23884. ' with TObject.Field do Fly(7);',
  23885. 'end;',
  23886. 'procedure THelper.Fly(n: byte);',
  23887. 'begin',
  23888. 'end;',
  23889. 'var',
  23890. ' o: TObject;',
  23891. 'begin',
  23892. ' o.Field.Fly(11);',
  23893. ' with o do Field.Fly(12);',
  23894. ' with o.Field do Fly(13);',
  23895. '']);
  23896. ConvertProgram;
  23897. CheckSource('TestTypeHelper_PassClassPropertyGetterStatic',
  23898. LinesToStr([ // statements
  23899. 'rtl.createClass(this, "TObject", null, function () {',
  23900. ' this.FField = 0;',
  23901. ' this.$init = function () {',
  23902. ' };',
  23903. ' this.$final = function () {',
  23904. ' };',
  23905. ' this.GetField = function () {',
  23906. ' var Result = 0;',
  23907. ' $mod.THelper.Fly.call({',
  23908. ' p: $mod.TObject.GetField(),',
  23909. ' get: function () {',
  23910. ' return this.p;',
  23911. ' },',
  23912. ' set: function (v) {',
  23913. ' this.p = v;',
  23914. ' }',
  23915. ' }, 1);',
  23916. ' $mod.THelper.Fly.call({',
  23917. ' p: $mod.TObject.GetField(),',
  23918. ' get: function () {',
  23919. ' return this.p;',
  23920. ' },',
  23921. ' set: function (v) {',
  23922. ' this.p = v;',
  23923. ' }',
  23924. ' }, 5);',
  23925. ' var $with = $mod.TObject;',
  23926. ' $mod.THelper.Fly.call({',
  23927. ' p: $with.GetField(),',
  23928. ' get: function () {',
  23929. ' return this.p;',
  23930. ' },',
  23931. ' set: function (v) {',
  23932. ' this.p = v;',
  23933. ' }',
  23934. ' }, 6);',
  23935. ' var $with1 = $mod.TObject.GetField();',
  23936. ' $mod.THelper.Fly.call({',
  23937. ' get: function () {',
  23938. ' return $with1;',
  23939. ' },',
  23940. ' set: function (v) {',
  23941. ' $with1 = v;',
  23942. ' }',
  23943. ' }, 7);',
  23944. ' return Result;',
  23945. ' };',
  23946. '});',
  23947. 'rtl.createHelper(this, "THelper", null, function () {',
  23948. ' this.Fly = function (n) {',
  23949. ' };',
  23950. '});',
  23951. 'this.o = null;',
  23952. '']),
  23953. LinesToStr([ // $mod.$main
  23954. '$mod.THelper.Fly.call({',
  23955. ' p: $mod.TObject.GetField(),',
  23956. ' get: function () {',
  23957. ' return this.p;',
  23958. ' },',
  23959. ' set: function (v) {',
  23960. ' this.p = v;',
  23961. ' }',
  23962. '}, 11);',
  23963. 'var $with = $mod.o;',
  23964. '$mod.THelper.Fly.call({',
  23965. ' p: $with.GetField(),',
  23966. ' get: function () {',
  23967. ' return this.p;',
  23968. ' },',
  23969. ' set: function (v) {',
  23970. ' this.p = v;',
  23971. ' }',
  23972. '}, 12);',
  23973. 'var $with1 = $mod.TObject.GetField();',
  23974. '$mod.THelper.Fly.call({',
  23975. ' get: function () {',
  23976. ' return $with1;',
  23977. ' },',
  23978. ' set: function (v) {',
  23979. ' $with1 = v;',
  23980. ' }',
  23981. '}, 13);',
  23982. '']));
  23983. end;
  23984. procedure TTestModule.TestTypeHelper_PassClassPropertyGetterNonStatic;
  23985. begin
  23986. StartProgram(false);
  23987. Add([
  23988. '{$modeswitch typehelpers}',
  23989. 'type',
  23990. ' TObject = class',
  23991. ' class var FField: word;',
  23992. ' class function GetField: word;',
  23993. ' class property Field: word read GetField write FField;',
  23994. ' end;',
  23995. ' TClass = class of TObject;',
  23996. ' THelper = type helper for word',
  23997. ' procedure Fly(n: byte);',
  23998. ' end;',
  23999. 'class function TObject.GetField: word;',
  24000. 'begin',
  24001. ' Field.Fly(1);',
  24002. ' Self.Field.Fly(5);',
  24003. ' with Self do Field.Fly(6);',
  24004. ' with Self.Field do Fly(7);',
  24005. 'end;',
  24006. 'procedure THelper.Fly(n: byte);',
  24007. 'begin',
  24008. 'end;',
  24009. 'var',
  24010. ' o: TObject;',
  24011. ' c: TClass;',
  24012. 'begin',
  24013. ' o.Field.Fly(11);',
  24014. ' with o do Field.Fly(12);',
  24015. ' with o.Field do Fly(13);',
  24016. ' c.Field.Fly(14);',
  24017. ' with c do Field.Fly(15);',
  24018. ' with c.Field do Fly(16);',
  24019. '']);
  24020. ConvertProgram;
  24021. CheckSource('TestTypeHelper_PassClassPropertyGetterNonStatic',
  24022. LinesToStr([ // statements
  24023. 'rtl.createClass(this, "TObject", null, function () {',
  24024. ' this.FField = 0;',
  24025. ' this.$init = function () {',
  24026. ' };',
  24027. ' this.$final = function () {',
  24028. ' };',
  24029. ' this.GetField = function () {',
  24030. ' var Result = 0;',
  24031. ' $mod.THelper.Fly.call({',
  24032. ' p: this.GetField(),',
  24033. ' get: function () {',
  24034. ' return this.p;',
  24035. ' },',
  24036. ' set: function (v) {',
  24037. ' this.p = v;',
  24038. ' }',
  24039. ' }, 1);',
  24040. ' $mod.THelper.Fly.call({',
  24041. ' p: this.GetField(),',
  24042. ' get: function () {',
  24043. ' return this.p;',
  24044. ' },',
  24045. ' set: function (v) {',
  24046. ' this.p = v;',
  24047. ' }',
  24048. ' }, 5);',
  24049. ' $mod.THelper.Fly.call({',
  24050. ' p: this.GetField(),',
  24051. ' get: function () {',
  24052. ' return this.p;',
  24053. ' },',
  24054. ' set: function (v) {',
  24055. ' this.p = v;',
  24056. ' }',
  24057. ' }, 6);',
  24058. ' var $with = this.GetField();',
  24059. ' $mod.THelper.Fly.call({',
  24060. ' get: function () {',
  24061. ' return $with;',
  24062. ' },',
  24063. ' set: function (v) {',
  24064. ' $with = v;',
  24065. ' }',
  24066. ' }, 7);',
  24067. ' return Result;',
  24068. ' };',
  24069. '});',
  24070. 'rtl.createHelper(this, "THelper", null, function () {',
  24071. ' this.Fly = function (n) {',
  24072. ' };',
  24073. '});',
  24074. 'this.o = null;',
  24075. 'this.c = null;',
  24076. '']),
  24077. LinesToStr([ // $mod.$main
  24078. '$mod.THelper.Fly.call({',
  24079. ' p: $mod.o.$class.GetField(),',
  24080. ' get: function () {',
  24081. ' return this.p;',
  24082. ' },',
  24083. ' set: function (v) {',
  24084. ' this.p = v;',
  24085. ' }',
  24086. '}, 11);',
  24087. 'var $with = $mod.o;',
  24088. '$mod.THelper.Fly.call({',
  24089. ' p: $with.$class.GetField(),',
  24090. ' get: function () {',
  24091. ' return this.p;',
  24092. ' },',
  24093. ' set: function (v) {',
  24094. ' this.p = v;',
  24095. ' }',
  24096. '}, 12);',
  24097. 'var $with1 = $mod.o.$class.GetField();',
  24098. '$mod.THelper.Fly.call({',
  24099. ' get: function () {',
  24100. ' return $with1;',
  24101. ' },',
  24102. ' set: function (v) {',
  24103. ' $with1 = v;',
  24104. ' }',
  24105. '}, 13);',
  24106. '$mod.THelper.Fly.call({',
  24107. ' p: $mod.c.GetField(),',
  24108. ' get: function () {',
  24109. ' return this.p;',
  24110. ' },',
  24111. ' set: function (v) {',
  24112. ' this.p = v;',
  24113. ' }',
  24114. '}, 14);',
  24115. 'var $with2 = $mod.c;',
  24116. '$mod.THelper.Fly.call({',
  24117. ' p: $with2.GetField(),',
  24118. ' get: function () {',
  24119. ' return this.p;',
  24120. ' },',
  24121. ' set: function (v) {',
  24122. ' this.p = v;',
  24123. ' }',
  24124. '}, 15);',
  24125. 'var $with3 = $mod.c.GetField();',
  24126. '$mod.THelper.Fly.call({',
  24127. ' get: function () {',
  24128. ' return $with3;',
  24129. ' },',
  24130. ' set: function (v) {',
  24131. ' $with3 = v;',
  24132. ' }',
  24133. '}, 16);',
  24134. '']));
  24135. end;
  24136. procedure TTestModule.TestTypeHelper_Property;
  24137. begin
  24138. StartProgram(false);
  24139. Add([
  24140. '{$modeswitch typehelpers}',
  24141. 'type',
  24142. ' THelper = type helper for word',
  24143. ' function GetSize: longint;',
  24144. ' procedure SetSize(Value: longint);',
  24145. ' property Size: longint read GetSize write SetSize;',
  24146. ' end;',
  24147. 'function THelper.GetSize: longint;',
  24148. 'begin',
  24149. ' Result:=Size+1;',
  24150. ' Size:=2;',
  24151. ' Result:=Self.Size+3;',
  24152. ' Self.Size:=4;',
  24153. ' with Self do begin',
  24154. ' Result:=Size+5;',
  24155. ' Size:=6;',
  24156. ' end;',
  24157. 'end;',
  24158. 'procedure THelper.SetSize(Value: longint);',
  24159. 'begin',
  24160. 'end;',
  24161. 'var w: word;',
  24162. 'begin',
  24163. ' w:=w.Size+7;',
  24164. ' w.Size:=w+8;',
  24165. ' with w do begin',
  24166. ' w:=Size+9;',
  24167. ' Size:=w+10;',
  24168. ' end;',
  24169. '']);
  24170. ConvertProgram;
  24171. CheckSource('TestTypeHelper_Property',
  24172. LinesToStr([ // statements
  24173. 'rtl.createHelper(this, "THelper", null, function () {',
  24174. ' this.GetSize = function () {',
  24175. ' var Result = 0;',
  24176. ' Result = $mod.THelper.GetSize.call(this) + 1;',
  24177. ' $mod.THelper.SetSize.call(this, 2);',
  24178. ' Result = $mod.THelper.GetSize.call(this) + 3;',
  24179. ' $mod.THelper.SetSize.call(this, 4);',
  24180. ' var $with = this.get();',
  24181. ' Result = $mod.THelper.GetSize.call(this) + 5;',
  24182. ' $mod.THelper.SetSize.call(this, 6);',
  24183. ' return Result;',
  24184. ' };',
  24185. ' this.SetSize = function (Value) {',
  24186. ' };',
  24187. '});',
  24188. 'this.w = 0;',
  24189. '']),
  24190. LinesToStr([ // $mod.$main
  24191. '$mod.w = $mod.THelper.GetSize.call({',
  24192. ' p: $mod,',
  24193. ' get: function () {',
  24194. ' return this.p.w;',
  24195. ' },',
  24196. ' set: function (v) {',
  24197. ' this.p.w = v;',
  24198. ' }',
  24199. '}) + 7;',
  24200. '$mod.THelper.SetSize.call({',
  24201. ' p: $mod,',
  24202. ' get: function () {',
  24203. ' return this.p.w;',
  24204. ' },',
  24205. ' set: function (v) {',
  24206. ' this.p.w = v;',
  24207. ' }',
  24208. '}, $mod.w + 8);',
  24209. 'var $with = $mod.w;',
  24210. '$mod.w = $mod.THelper.GetSize.call({',
  24211. ' get: function () {',
  24212. ' return $with;',
  24213. ' },',
  24214. ' set: function (v) {',
  24215. ' $with = v;',
  24216. ' }',
  24217. '}) + 9;',
  24218. '$mod.THelper.SetSize.call({',
  24219. ' get: function () {',
  24220. ' return $with;',
  24221. ' },',
  24222. ' set: function (v) {',
  24223. ' $with = v;',
  24224. ' }',
  24225. '}, $mod.w + 10);',
  24226. '']));
  24227. end;
  24228. procedure TTestModule.TestTypeHelper_Property_Array;
  24229. begin
  24230. StartProgram(false);
  24231. Add([
  24232. '{$modeswitch typehelpers}',
  24233. 'type',
  24234. ' THelper = type helper for word',
  24235. ' function GetItems(Index: byte): boolean;',
  24236. ' procedure SetItems(Index: byte; Value: boolean);',
  24237. ' property Items[Index: byte]: boolean read GetItems write SetItems;',
  24238. ' end;',
  24239. 'function THelper.GetItems(Index: byte): boolean;',
  24240. 'begin',
  24241. ' Result:=Items[1];',
  24242. ' Items[2]:=false;',
  24243. ' Result:=Self.Items[3];',
  24244. ' Self.Items[4]:=true;',
  24245. ' with Self do begin',
  24246. ' Result:=Items[5];',
  24247. ' Items[6]:=false;',
  24248. ' end;',
  24249. 'end;',
  24250. 'procedure THelper.SetItems(Index: byte; Value: boolean);',
  24251. 'begin',
  24252. 'end;',
  24253. 'var',
  24254. ' w: word;',
  24255. ' b: boolean;',
  24256. 'begin',
  24257. ' b:=w.Items[1];',
  24258. ' w.Items[2]:=b;',
  24259. ' with w do begin',
  24260. ' b:=Items[3];',
  24261. ' Items[4]:=b;',
  24262. ' end;',
  24263. '']);
  24264. ConvertProgram;
  24265. CheckSource('TestTypeHelper_Property_Array',
  24266. LinesToStr([ // statements
  24267. 'rtl.createHelper(this, "THelper", null, function () {',
  24268. ' this.GetItems = function (Index) {',
  24269. ' var Result = false;',
  24270. ' Result = $mod.THelper.GetItems.call(this, 1);',
  24271. ' $mod.THelper.SetItems.call(this, 2, false);',
  24272. ' Result = $mod.THelper.GetItems.call(this, 3);',
  24273. ' $mod.THelper.SetItems.call(this, 4, true);',
  24274. ' var $with = this.get();',
  24275. ' Result = $mod.THelper.GetItems.call(this, 5);',
  24276. ' $mod.THelper.SetItems.call(this, 6, false);',
  24277. ' return Result;',
  24278. ' };',
  24279. ' this.SetItems = function (Index, Value) {',
  24280. ' };',
  24281. '});',
  24282. 'this.w = 0;',
  24283. 'this.b = false;',
  24284. '']),
  24285. LinesToStr([ // $mod.$main
  24286. '$mod.b = $mod.THelper.GetItems.call({',
  24287. ' p: $mod,',
  24288. ' get: function () {',
  24289. ' return this.p.w;',
  24290. ' },',
  24291. ' set: function (v) {',
  24292. ' this.p.w = v;',
  24293. ' }',
  24294. '}, 1);',
  24295. '$mod.THelper.SetItems.call({',
  24296. ' p: $mod,',
  24297. ' get: function () {',
  24298. ' return this.p.w;',
  24299. ' },',
  24300. ' set: function (v) {',
  24301. ' this.p.w = v;',
  24302. ' }',
  24303. '}, 2, $mod.b);',
  24304. 'var $with = $mod.w;',
  24305. '$mod.b = $mod.THelper.GetItems.call({',
  24306. ' get: function () {',
  24307. ' return $with;',
  24308. ' },',
  24309. ' set: function (v) {',
  24310. ' $with = v;',
  24311. ' }',
  24312. '}, 3);',
  24313. '$mod.THelper.SetItems.call({',
  24314. ' get: function () {',
  24315. ' return $with;',
  24316. ' },',
  24317. ' set: function (v) {',
  24318. ' $with = v;',
  24319. ' }',
  24320. '}, 4, $mod.b);',
  24321. '']));
  24322. end;
  24323. procedure TTestModule.TestTypeHelper_ClassProperty;
  24324. begin
  24325. StartProgram(false);
  24326. Add([
  24327. '{$modeswitch typehelpers}',
  24328. 'type',
  24329. ' THelper = type helper for word',
  24330. ' class function GetSize: longint; static;',
  24331. ' class procedure SetSize(Value: longint); static;',
  24332. ' class property Size: longint read GetSize write SetSize;',
  24333. ' end;',
  24334. 'class function THelper.GetSize: longint;',
  24335. 'begin',
  24336. ' Result:=Size+1;',
  24337. ' Size:=2;',
  24338. 'end;',
  24339. 'class procedure THelper.SetSize(Value: longint);',
  24340. 'begin',
  24341. 'end;',
  24342. 'begin',
  24343. '']);
  24344. ConvertProgram;
  24345. CheckSource('TestTypeHelper_ClassProperty',
  24346. LinesToStr([ // statements
  24347. 'rtl.createHelper(this, "THelper", null, function () {',
  24348. ' this.GetSize = function () {',
  24349. ' var Result = 0;',
  24350. ' Result = $mod.THelper.GetSize() + 1;',
  24351. ' $mod.THelper.SetSize(2);',
  24352. ' return Result;',
  24353. ' };',
  24354. ' this.SetSize = function (Value) {',
  24355. ' };',
  24356. '});',
  24357. '']),
  24358. LinesToStr([ // $mod.$main
  24359. '']));
  24360. end;
  24361. procedure TTestModule.TestTypeHelper_ClassProperty_Array;
  24362. begin
  24363. StartProgram(false);
  24364. Add([
  24365. '{$modeswitch typehelpers}',
  24366. 'type',
  24367. ' THelper = type helper for word',
  24368. ' class function GetItems(Index: byte): boolean; static;',
  24369. ' class procedure SetItems(Index: byte; Value: boolean); static;',
  24370. ' class property Items[Index: byte]: boolean read GetItems write SetItems;',
  24371. ' end;',
  24372. 'class function THelper.GetItems(Index: byte): boolean;',
  24373. 'begin',
  24374. ' Result:=Items[1];',
  24375. ' Items[2]:=false;',
  24376. 'end;',
  24377. 'class procedure THelper.SetItems(Index: byte; Value: boolean);',
  24378. 'begin',
  24379. 'end;',
  24380. 'var',
  24381. ' w: word;',
  24382. ' b: boolean;',
  24383. 'begin',
  24384. ' b:=w.Items[1];',
  24385. ' w.Items[2]:=b;',
  24386. ' with w do begin',
  24387. ' b:=Items[3];',
  24388. ' Items[4]:=b;',
  24389. ' end;',
  24390. '']);
  24391. ConvertProgram;
  24392. CheckSource('TestTypeHelper_ClassProperty_Array',
  24393. LinesToStr([ // statements
  24394. 'rtl.createHelper(this, "THelper", null, function () {',
  24395. ' this.GetItems = function (Index) {',
  24396. ' var Result = false;',
  24397. ' Result = $mod.THelper.GetItems(1);',
  24398. ' $mod.THelper.SetItems(2, false);',
  24399. ' return Result;',
  24400. ' };',
  24401. ' this.SetItems = function (Index, Value) {',
  24402. ' };',
  24403. '});',
  24404. 'this.w = 0;',
  24405. 'this.b = false;',
  24406. '']),
  24407. LinesToStr([ // $mod.$main
  24408. '$mod.b = $mod.THelper.GetItems(1);',
  24409. '$mod.THelper.SetItems(2, $mod.b);',
  24410. 'var $with = $mod.w;',
  24411. '$mod.b = $mod.THelper.GetItems(3);',
  24412. '$mod.THelper.SetItems(4, $mod.b);',
  24413. '']));
  24414. end;
  24415. procedure TTestModule.TestTypeHelper_ClassMethod;
  24416. begin
  24417. StartProgram(false);
  24418. Add([
  24419. '{$modeswitch typehelpers}',
  24420. 'type',
  24421. ' THelper = type helper for word',
  24422. ' class procedure DoStatic; static;',
  24423. ' end;',
  24424. 'class procedure THelper.DoStatic;',
  24425. 'begin',
  24426. ' DoStatic;',
  24427. ' DoStatic();',
  24428. 'end;',
  24429. 'var w: word;',
  24430. 'begin',
  24431. ' w.DoStatic;',
  24432. ' w.DoStatic();',
  24433. '']);
  24434. ConvertProgram;
  24435. CheckSource('TestTypeHelper_ClassMethod',
  24436. LinesToStr([ // statements
  24437. 'rtl.createHelper(this, "THelper", null, function () {',
  24438. ' this.DoStatic = function () {',
  24439. ' $mod.THelper.DoStatic();',
  24440. ' $mod.THelper.DoStatic();',
  24441. ' };',
  24442. '});',
  24443. 'this.w = 0;',
  24444. '']),
  24445. LinesToStr([ // $mod.$main
  24446. '$mod.THelper.DoStatic();',
  24447. '$mod.THelper.DoStatic();',
  24448. '']));
  24449. end;
  24450. procedure TTestModule.TestTypeHelper_ExtClassMethodFail;
  24451. begin
  24452. StartProgram(false);
  24453. Add([
  24454. '{$modeswitch typehelpers}',
  24455. 'type',
  24456. ' THelper = type helper for word',
  24457. ' procedure Run; external name ''Run'';',
  24458. ' end;',
  24459. 'var w: word;',
  24460. 'begin',
  24461. ' w.Run;',
  24462. '']);
  24463. SetExpectedPasResolverError('Not supported: external method in type helper',nNotSupportedX);
  24464. ConvertProgram;
  24465. end;
  24466. procedure TTestModule.TestTypeHelper_Constructor;
  24467. begin
  24468. StartProgram(false);
  24469. Add([
  24470. '{$modeswitch typehelpers}',
  24471. 'type',
  24472. ' THelper = type helper for word',
  24473. ' constructor Init(e: longint);',
  24474. ' end;',
  24475. 'constructor THelper.Init(e: longint);',
  24476. 'begin',
  24477. ' Self:=e;',
  24478. ' Init(e+1);',
  24479. 'end;',
  24480. 'var w: word;',
  24481. 'begin',
  24482. ' w:=word.Init(2);',
  24483. ' w:=w.Init(3);',
  24484. ' with word do w:=Init(4);',
  24485. ' with w do w:=Init(5);',
  24486. '']);
  24487. ConvertProgram;
  24488. CheckSource('TestTypeHelper_Constructor',
  24489. LinesToStr([ // statements
  24490. 'rtl.createHelper(this, "THelper", null, function () {',
  24491. ' this.Init = function (e) {',
  24492. ' this.set(e);',
  24493. ' $mod.THelper.Init.call(this, e + 1);',
  24494. ' return this.get();',
  24495. ' };',
  24496. ' this.$new = function (fn, args) {',
  24497. ' return this[fn].apply({',
  24498. ' p: 0,',
  24499. ' get: function () {',
  24500. ' return this.p;',
  24501. ' },',
  24502. ' set: function (v) {',
  24503. ' this.p = v;',
  24504. ' }',
  24505. ' }, args);',
  24506. ' };',
  24507. '});',
  24508. 'this.w = 0;',
  24509. '']),
  24510. LinesToStr([ // $mod.$main
  24511. '$mod.w = $mod.THelper.$new("Init", [2]);',
  24512. '$mod.w = $mod.THelper.Init.call({',
  24513. ' p: $mod,',
  24514. ' get: function () {',
  24515. ' return this.p.w;',
  24516. ' },',
  24517. ' set: function (v) {',
  24518. ' this.p.w = v;',
  24519. ' }',
  24520. '}, 3);',
  24521. '$mod.w = $mod.THelper.$new("Init", [4]);',
  24522. 'var $with = $mod.w;',
  24523. '$mod.w = $mod.THelper.Init.call({',
  24524. ' get: function () {',
  24525. ' return $with;',
  24526. ' },',
  24527. ' set: function (v) {',
  24528. ' $with = v;',
  24529. ' }',
  24530. '}, 5);',
  24531. '']));
  24532. end;
  24533. procedure TTestModule.TestTypeHelper_Word;
  24534. begin
  24535. StartProgram(false);
  24536. Add([
  24537. '{$modeswitch typehelpers}',
  24538. 'type',
  24539. ' THelper = type helper for word',
  24540. ' procedure DoIt(e: byte = 123);',
  24541. ' end;',
  24542. 'procedure THelper.DoIt(e: byte);',
  24543. 'begin',
  24544. ' Self:=e;',
  24545. ' Self:=Self+1;',
  24546. ' with Self do Doit;',
  24547. 'end;',
  24548. 'begin',
  24549. ' word(3).DoIt;',
  24550. '']);
  24551. ConvertProgram;
  24552. CheckSource('TestTypeHelper_Word',
  24553. LinesToStr([ // statements
  24554. 'rtl.createHelper(this, "THelper", null, function () {',
  24555. ' this.DoIt = function (e) {',
  24556. ' this.set(e);',
  24557. ' this.set(this.get() + 1);',
  24558. ' var $with = this.get();',
  24559. ' $mod.THelper.DoIt.call(this, 123);',
  24560. ' };',
  24561. '});',
  24562. '']),
  24563. LinesToStr([ // $mod.$main
  24564. '$mod.THelper.DoIt.call({',
  24565. ' get: function () {',
  24566. ' return 3;',
  24567. ' },',
  24568. ' set: function (v) {',
  24569. ' rtl.raiseE("EPropReadOnly");',
  24570. ' }',
  24571. '}, 123);',
  24572. '']));
  24573. end;
  24574. procedure TTestModule.TestTypeHelper_Boolean;
  24575. begin
  24576. StartProgram(false);
  24577. Add([
  24578. '{$modeswitch typehelpers}',
  24579. 'type',
  24580. ' Integer = longint;',
  24581. ' THelper = type helper for boolean',
  24582. ' procedure Run(e: wordbool = true);',
  24583. ' end;',
  24584. 'procedure THelper.Run(e: wordbool);',
  24585. 'begin',
  24586. ' Self:=e;',
  24587. ' Self:=not Self;',
  24588. ' with Self do Run;',
  24589. ' if Integer(Self)=0 then ;',
  24590. 'end;',
  24591. 'begin',
  24592. ' boolean(3).Run;',
  24593. '']);
  24594. ConvertProgram;
  24595. CheckSource('TestTypeHelper_Boolean',
  24596. LinesToStr([ // statements
  24597. 'rtl.createHelper(this, "THelper", null, function () {',
  24598. ' this.Run = function (e) {',
  24599. ' this.set(e);',
  24600. ' this.set(!this.get());',
  24601. ' var $with = this.get();',
  24602. ' $mod.THelper.Run.call(this, true);',
  24603. ' if ((this.get() ? 1 : 0) === 0) ;',
  24604. ' };',
  24605. '});',
  24606. '']),
  24607. LinesToStr([ // $mod.$main
  24608. '$mod.THelper.Run.call({',
  24609. ' a: 3 != 0,',
  24610. ' get: function () {',
  24611. ' return this.a;',
  24612. ' },',
  24613. ' set: function (v) {',
  24614. ' rtl.raiseE("EPropReadOnly");',
  24615. ' }',
  24616. '}, true);',
  24617. '']));
  24618. end;
  24619. procedure TTestModule.TestTypeHelper_WordBool;
  24620. begin
  24621. StartProgram(false);
  24622. Add([
  24623. '{$modeswitch typehelpers}',
  24624. 'type',
  24625. ' Integer = longint;',
  24626. ' THelper = type helper for WordBool',
  24627. ' procedure Run(e: wordbool = true);',
  24628. ' end;',
  24629. 'procedure THelper.Run(e: wordbool);',
  24630. 'var i: integer;',
  24631. 'begin',
  24632. ' i:=Integer(Self);',
  24633. 'end;',
  24634. 'var w: wordbool;',
  24635. 'begin',
  24636. ' w.Run;',
  24637. ' wordbool(3).Run;',
  24638. '']);
  24639. ConvertProgram;
  24640. CheckSource('TestTypeHelper_WordBool',
  24641. LinesToStr([ // statements
  24642. 'rtl.createHelper(this, "THelper", null, function () {',
  24643. ' this.Run = function (e) {',
  24644. ' var i = 0;',
  24645. ' i = (this.get() ? 1 : 0);',
  24646. ' };',
  24647. '});',
  24648. 'this.w = false;',
  24649. '']),
  24650. LinesToStr([ // $mod.$main
  24651. '$mod.THelper.Run.call({',
  24652. ' p: $mod,',
  24653. ' get: function () {',
  24654. ' return this.p.w;',
  24655. ' },',
  24656. ' set: function (v) {',
  24657. ' this.p.w = v;',
  24658. ' }',
  24659. '}, true);',
  24660. '$mod.THelper.Run.call({',
  24661. ' a: 3 != 0,',
  24662. ' get: function () {',
  24663. ' return this.a;',
  24664. ' },',
  24665. ' set: function (v) {',
  24666. ' rtl.raiseE("EPropReadOnly");',
  24667. ' }',
  24668. '}, true);',
  24669. '']));
  24670. end;
  24671. procedure TTestModule.TestTypeHelper_Double;
  24672. begin
  24673. StartProgram(false);
  24674. Add([
  24675. '{$modeswitch typehelpers}',
  24676. 'type',
  24677. ' Float = type double;',
  24678. ' THelper = type helper for Float',
  24679. ' const NPI = 3.141592;',
  24680. ' function ToStr: String;',
  24681. ' end;',
  24682. 'function THelper.ToStr: String;',
  24683. 'begin',
  24684. 'end;',
  24685. 'procedure DoIt(s: string);',
  24686. 'begin',
  24687. 'end;',
  24688. 'var f: Float;',
  24689. 'begin',
  24690. ' DoIt(f.toStr);',
  24691. ' DoIt(f.toStr());',
  24692. ' (f*f).toStr;',
  24693. ' DoIt((f*f).toStr);',
  24694. '']);
  24695. ConvertProgram;
  24696. CheckSource('TestTypeHelper_Double',
  24697. LinesToStr([ // statements
  24698. 'rtl.createHelper(this, "THelper", null, function () {',
  24699. ' this.NPI = 3.141592;',
  24700. ' this.ToStr = function () {',
  24701. ' var Result = "";',
  24702. ' return Result;',
  24703. ' };',
  24704. '});',
  24705. 'this.DoIt = function (s) {',
  24706. '};',
  24707. 'this.f = 0.0;',
  24708. '']),
  24709. LinesToStr([ // $mod.$main
  24710. '$mod.DoIt($mod.THelper.ToStr.call({',
  24711. ' p: $mod,',
  24712. ' get: function () {',
  24713. ' return this.p.f;',
  24714. ' },',
  24715. ' set: function (v) {',
  24716. ' this.p.f = v;',
  24717. ' }',
  24718. '}));',
  24719. '$mod.DoIt($mod.THelper.ToStr.call({',
  24720. ' p: $mod,',
  24721. ' get: function () {',
  24722. ' return this.p.f;',
  24723. ' },',
  24724. ' set: function (v) {',
  24725. ' this.p.f = v;',
  24726. ' }',
  24727. '}));',
  24728. '$mod.THelper.ToStr.call({',
  24729. ' a: $mod.f * $mod.f,',
  24730. ' get: function () {',
  24731. ' return this.a;',
  24732. ' },',
  24733. ' set: function (v) {',
  24734. ' rtl.raiseE("EPropReadOnly");',
  24735. ' }',
  24736. '});',
  24737. '$mod.DoIt($mod.THelper.ToStr.call({',
  24738. ' a: $mod.f * $mod.f,',
  24739. ' get: function () {',
  24740. ' return this.a;',
  24741. ' },',
  24742. ' set: function (v) {',
  24743. ' rtl.raiseE("EPropReadOnly");',
  24744. ' }',
  24745. '}));',
  24746. '']));
  24747. end;
  24748. procedure TTestModule.TestTypeHelper_NativeInt;
  24749. begin
  24750. StartProgram(false);
  24751. Add([
  24752. '{$modeswitch typehelpers}',
  24753. 'type',
  24754. ' MaxInt = type nativeint;',
  24755. ' THelperI = type helper for MaxInt',
  24756. ' function ToStr: String;',
  24757. ' end;',
  24758. ' MaxUInt = type nativeuint;',
  24759. ' THelperU = type helper for MaxUInt',
  24760. ' function ToStr: String;',
  24761. ' end;',
  24762. 'function THelperI.ToStr: String;',
  24763. 'begin',
  24764. ' Result:=str(Self);',
  24765. 'end;',
  24766. 'function THelperU.ToStr: String;',
  24767. 'begin',
  24768. ' Result:=str(Self);',
  24769. 'end;',
  24770. 'procedure DoIt(s: string);',
  24771. 'begin',
  24772. 'end;',
  24773. 'var i: MaxInt;',
  24774. 'begin',
  24775. ' DoIt(i.toStr);',
  24776. ' DoIt(i.toStr());',
  24777. ' (i*i).toStr;',
  24778. ' DoIt((i*i).toStr);',
  24779. '']);
  24780. ConvertProgram;
  24781. CheckSource('TestTypeHelper_NativeInt',
  24782. LinesToStr([ // statements
  24783. 'rtl.createHelper(this, "THelperI", null, function () {',
  24784. ' this.ToStr = function () {',
  24785. ' var Result = "";',
  24786. ' Result = "" + this.get();',
  24787. ' return Result;',
  24788. ' };',
  24789. '});',
  24790. 'rtl.createHelper(this, "THelperU", null, function () {',
  24791. ' this.ToStr = function () {',
  24792. ' var Result = "";',
  24793. ' Result = "" + this.get();',
  24794. ' return Result;',
  24795. ' };',
  24796. '});',
  24797. 'this.DoIt = function (s) {',
  24798. '};',
  24799. 'this.i = 0;',
  24800. '']),
  24801. LinesToStr([ // $mod.$main
  24802. '$mod.DoIt($mod.THelperI.ToStr.call({',
  24803. ' p: $mod,',
  24804. ' get: function () {',
  24805. ' return this.p.i;',
  24806. ' },',
  24807. ' set: function (v) {',
  24808. ' this.p.i = v;',
  24809. ' }',
  24810. '}));',
  24811. '$mod.DoIt($mod.THelperI.ToStr.call({',
  24812. ' p: $mod,',
  24813. ' get: function () {',
  24814. ' return this.p.i;',
  24815. ' },',
  24816. ' set: function (v) {',
  24817. ' this.p.i = v;',
  24818. ' }',
  24819. '}));',
  24820. '$mod.THelperI.ToStr.call({',
  24821. ' a: $mod.i * $mod.i,',
  24822. ' get: function () {',
  24823. ' return this.a;',
  24824. ' },',
  24825. ' set: function (v) {',
  24826. ' rtl.raiseE("EPropReadOnly");',
  24827. ' }',
  24828. '});',
  24829. '$mod.DoIt($mod.THelperI.ToStr.call({',
  24830. ' a: $mod.i * $mod.i,',
  24831. ' get: function () {',
  24832. ' return this.a;',
  24833. ' },',
  24834. ' set: function (v) {',
  24835. ' rtl.raiseE("EPropReadOnly");',
  24836. ' }',
  24837. '}));',
  24838. '']));
  24839. end;
  24840. procedure TTestModule.TestTypeHelper_StringChar;
  24841. begin
  24842. StartProgram(false);
  24843. Add([
  24844. '{$modeswitch typehelpers}',
  24845. 'type',
  24846. ' TStringHelper = type helper for string',
  24847. ' procedure DoIt(e: byte = 123);',
  24848. ' end;',
  24849. ' TCharHelper = type helper for char',
  24850. ' procedure Fly;',
  24851. ' end;',
  24852. 'procedure TStringHelper.DoIt(e: byte);',
  24853. 'begin',
  24854. ' Self[1]:=''c'';',
  24855. ' Self[2]:=Self[3];',
  24856. 'end;',
  24857. 'procedure TCharHelper.Fly;',
  24858. 'begin',
  24859. ' Self:=''c'';',
  24860. 'end;',
  24861. 'begin',
  24862. ' ''abc''.DoIt;',
  24863. ' ''xyz''.DoIt();',
  24864. ' ''c''.Fly();',
  24865. '']);
  24866. ConvertProgram;
  24867. CheckSource('TestTypeHelper_StringChar',
  24868. LinesToStr([ // statements
  24869. 'rtl.createHelper(this, "TStringHelper", null, function () {',
  24870. ' this.DoIt = function (e) {',
  24871. ' this.set(rtl.setCharAt(this.get(), 0, "c"));',
  24872. ' this.set(rtl.setCharAt(this.get(), 1, this.get().charAt(2)));',
  24873. ' };',
  24874. '});',
  24875. 'rtl.createHelper(this, "TCharHelper", null, function () {',
  24876. ' this.Fly = function () {',
  24877. ' this.set("c");',
  24878. ' };',
  24879. '});',
  24880. '']),
  24881. LinesToStr([ // $mod.$main
  24882. '$mod.TStringHelper.DoIt.call({',
  24883. ' get: function () {',
  24884. ' return "abc";',
  24885. ' },',
  24886. ' set: function (v) {',
  24887. ' rtl.raiseE("EPropReadOnly");',
  24888. ' }',
  24889. '}, 123);',
  24890. '$mod.TStringHelper.DoIt.call({',
  24891. ' get: function () {',
  24892. ' return "xyz";',
  24893. ' },',
  24894. ' set: function (v) {',
  24895. ' rtl.raiseE("EPropReadOnly");',
  24896. ' }',
  24897. '}, 123);',
  24898. '$mod.TCharHelper.Fly.call({',
  24899. ' get: function () {',
  24900. ' return "c";',
  24901. ' },',
  24902. ' set: function (v) {',
  24903. ' rtl.raiseE("EPropReadOnly");',
  24904. ' }',
  24905. '});',
  24906. '']));
  24907. end;
  24908. procedure TTestModule.TestTypeHelper_JSValue;
  24909. begin
  24910. StartProgram(false);
  24911. Add([
  24912. '{$modeswitch typehelpers}',
  24913. 'type',
  24914. ' TExtValue = type jsvalue;',
  24915. ' THelper = type helper for TExtValue',
  24916. ' function ToStr: String;',
  24917. ' end;',
  24918. 'function THelper.ToStr: String;',
  24919. 'begin',
  24920. 'end;',
  24921. 'var',
  24922. ' s: string;',
  24923. ' v: TExtValue;',
  24924. 'begin',
  24925. ' s:=v.toStr;',
  24926. ' s:=v.toStr();',
  24927. ' TExtValue(s).toStr;',
  24928. '']);
  24929. ConvertProgram;
  24930. CheckSource('TestTypeHelper_JSValue',
  24931. LinesToStr([ // statements
  24932. 'rtl.createHelper(this, "THelper", null, function () {',
  24933. ' this.ToStr = function () {',
  24934. ' var Result = "";',
  24935. ' return Result;',
  24936. ' };',
  24937. '});',
  24938. 'this.s = "";',
  24939. 'this.v = undefined;',
  24940. '']),
  24941. LinesToStr([ // $mod.$main
  24942. '$mod.s = $mod.THelper.ToStr.call({',
  24943. ' p: $mod,',
  24944. ' get: function () {',
  24945. ' return this.p.v;',
  24946. ' },',
  24947. ' set: function (v) {',
  24948. ' this.p.v = v;',
  24949. ' }',
  24950. '});',
  24951. '$mod.s = $mod.THelper.ToStr.call({',
  24952. ' p: $mod,',
  24953. ' get: function () {',
  24954. ' return this.p.v;',
  24955. ' },',
  24956. ' set: function (v) {',
  24957. ' this.p.v = v;',
  24958. ' }',
  24959. '});',
  24960. '$mod.THelper.ToStr.call({',
  24961. ' p: $mod,',
  24962. ' get: function () {',
  24963. ' return this.p.s;',
  24964. ' },',
  24965. ' set: function (v) {',
  24966. ' rtl.raiseE("EPropReadOnly");',
  24967. ' }',
  24968. '});',
  24969. '']));
  24970. end;
  24971. procedure TTestModule.TestTypeHelper_Array;
  24972. begin
  24973. StartProgram(false);
  24974. Add([
  24975. '{$modeswitch typehelpers}',
  24976. 'type',
  24977. ' TArrOfBool = array of boolean;',
  24978. ' TArrOfJS = array of jsvalue;',
  24979. ' THelper = type helper for TArrOfBool',
  24980. ' procedure DoIt(e: byte = 123);',
  24981. ' end;',
  24982. 'procedure THelper.DoIt(e: byte);',
  24983. 'begin',
  24984. ' Self[1]:=true;',
  24985. ' Self[2]:=not Self[3];',
  24986. ' SetLength(Self,4);',
  24987. 'end;',
  24988. 'var',
  24989. ' b: TArrOfBool;',
  24990. ' j: TArrOfJS;',
  24991. 'begin',
  24992. ' b.DoIt;',
  24993. ' TArrOfBool(j).DoIt();',
  24994. '']);
  24995. ConvertProgram;
  24996. CheckSource('TestTypeHelper_Array',
  24997. LinesToStr([ // statements
  24998. 'rtl.createHelper(this, "THelper", null, function () {',
  24999. ' this.DoIt = function (e) {',
  25000. ' this.get()[1] = true;',
  25001. ' this.get()[2] = !this.get()[3];',
  25002. ' this.set(rtl.arraySetLength(this.get(), false, 4));',
  25003. ' };',
  25004. '});',
  25005. 'this.b = [];',
  25006. 'this.j = [];',
  25007. '']),
  25008. LinesToStr([ // $mod.$main
  25009. '$mod.THelper.DoIt.call({',
  25010. ' p: $mod,',
  25011. ' get: function () {',
  25012. ' return this.p.b;',
  25013. ' },',
  25014. ' set: function (v) {',
  25015. ' this.p.b = v;',
  25016. ' }',
  25017. '}, 123);',
  25018. '$mod.THelper.DoIt.call({',
  25019. ' p: $mod,',
  25020. ' get: function () {',
  25021. ' return this.p.j;',
  25022. ' },',
  25023. ' set: function (v) {',
  25024. ' this.p.j = v;',
  25025. ' }',
  25026. '}, 123);',
  25027. '']));
  25028. end;
  25029. procedure TTestModule.TestTypeHelper_EnumType;
  25030. begin
  25031. StartProgram(false);
  25032. Add([
  25033. '{$modeswitch typehelpers}',
  25034. 'type',
  25035. ' TEnum = (red,blue);',
  25036. ' THelper = type helper for TEnum',
  25037. ' procedure DoIt(e: byte = 123);',
  25038. ' class procedure Swing(w: word); static;',
  25039. ' end;',
  25040. 'procedure THelper.DoIt(e: byte);',
  25041. 'begin',
  25042. ' Self:=red;',
  25043. ' Self:=succ(Self);',
  25044. ' with Self do Doit;',
  25045. 'end;',
  25046. 'class procedure THelper.Swing(w: word);',
  25047. 'begin',
  25048. 'end;',
  25049. 'var e: TEnum;',
  25050. 'begin',
  25051. ' e.DoIt;',
  25052. ' red.DoIt;',
  25053. ' TEnum.blue.DoIt;',
  25054. ' TEnum(1).DoIt;',
  25055. ' TEnum.Swing(3);',
  25056. '']);
  25057. ConvertProgram;
  25058. CheckSource('TestTypeHelper_EnumType',
  25059. LinesToStr([ // statements
  25060. 'this.TEnum = {',
  25061. ' "0": "red",',
  25062. ' red: 0,',
  25063. ' "1": "blue",',
  25064. ' blue: 1',
  25065. '};',
  25066. 'rtl.createHelper(this, "THelper", null, function () {',
  25067. ' this.DoIt = function (e) {',
  25068. ' this.set($mod.TEnum.red);',
  25069. ' this.set(this.get() + 1);',
  25070. ' var $with = this.get();',
  25071. ' $mod.THelper.DoIt.call(this, 123);',
  25072. ' };',
  25073. ' this.Swing = function (w) {',
  25074. ' };',
  25075. '});',
  25076. 'this.e = 0;',
  25077. '']),
  25078. LinesToStr([ // $mod.$main
  25079. '$mod.THelper.DoIt.call({',
  25080. ' p: $mod,',
  25081. ' get: function () {',
  25082. ' return this.p.e;',
  25083. ' },',
  25084. ' set: function (v) {',
  25085. ' this.p.e = v;',
  25086. ' }',
  25087. '}, 123);',
  25088. '$mod.THelper.DoIt.call({',
  25089. ' p: $mod.TEnum,',
  25090. ' get: function () {',
  25091. ' return this.p.red;',
  25092. ' },',
  25093. ' set: function (v) {',
  25094. ' rtl.raiseE("EPropReadOnly");',
  25095. ' }',
  25096. '}, 123);',
  25097. '$mod.THelper.DoIt.call({',
  25098. ' p: $mod.TEnum,',
  25099. ' get: function () {',
  25100. ' return this.p.blue;',
  25101. ' },',
  25102. ' set: function (v) {',
  25103. ' rtl.raiseE("EPropReadOnly");',
  25104. ' }',
  25105. '}, 123);',
  25106. '$mod.THelper.DoIt.call({',
  25107. ' get: function () {',
  25108. ' return 1;',
  25109. ' },',
  25110. ' set: function (v) {',
  25111. ' rtl.raiseE("EPropReadOnly");',
  25112. ' }',
  25113. '}, 123);',
  25114. '$mod.THelper.Swing(3);',
  25115. '']));
  25116. end;
  25117. procedure TTestModule.TestTypeHelper_SetType;
  25118. begin
  25119. StartProgram(false);
  25120. Add([
  25121. '{$modeswitch typehelpers}',
  25122. 'type',
  25123. ' TEnum = (red,blue);',
  25124. ' TSetOfEnum = set of TEnum;',
  25125. ' THelper = type helper for TSetOfEnum',
  25126. ' procedure DoIt(e: byte = 123);',
  25127. ' constructor Init(e: TEnum);',
  25128. ' constructor InitEmpty;',
  25129. ' end;',
  25130. 'procedure THelper.DoIt(e: byte);',
  25131. 'begin',
  25132. ' Self:=[];',
  25133. ' Self:=[red];',
  25134. ' Include(Self,blue);',
  25135. 'end;',
  25136. 'constructor THelper.Init(e: TEnum);',
  25137. 'begin',
  25138. ' Self:=[];',
  25139. ' Self:=[e];',
  25140. ' Include(Self,blue);',
  25141. 'end;',
  25142. 'constructor THelper.InitEmpty;',
  25143. 'begin',
  25144. 'end;',
  25145. 'var s: TSetOfEnum;',
  25146. 'begin',
  25147. ' s.DoIt;',
  25148. //' [red].DoIt;',
  25149. //' with s do DoIt;',
  25150. //' with [red,blue] do DoIt;',
  25151. ' s:=TSetOfEnum.Init(blue);',
  25152. ' s:=s.Init(blue);',
  25153. '']);
  25154. ConvertProgram;
  25155. CheckSource('TestTypeHelper_SetType',
  25156. LinesToStr([ // statements
  25157. 'this.TEnum = {',
  25158. ' "0": "red",',
  25159. ' red: 0,',
  25160. ' "1": "blue",',
  25161. ' blue: 1',
  25162. '};',
  25163. 'rtl.createHelper(this, "THelper", null, function () {',
  25164. ' this.DoIt = function (e) {',
  25165. ' this.set({});',
  25166. ' this.set(rtl.createSet($mod.TEnum.red));',
  25167. ' this.set(rtl.includeSet(this.get(), $mod.TEnum.blue));',
  25168. ' };',
  25169. ' this.Init = function (e) {',
  25170. ' this.set({});',
  25171. ' this.set(rtl.createSet(e));',
  25172. ' this.set(rtl.includeSet(this.get(), $mod.TEnum.blue));',
  25173. ' return this.get();',
  25174. ' };',
  25175. ' this.InitEmpty = function () {',
  25176. ' return this.get();',
  25177. ' };',
  25178. ' this.$new = function (fn, args) {',
  25179. ' return this[fn].apply({',
  25180. ' p: {},',
  25181. ' get: function () {',
  25182. ' return this.p;',
  25183. ' },',
  25184. ' set: function (v) {',
  25185. ' this.p = v;',
  25186. ' }',
  25187. ' }, args);',
  25188. ' };',
  25189. '});',
  25190. 'this.s = {};',
  25191. '']),
  25192. LinesToStr([ // $mod.$main
  25193. '$mod.THelper.DoIt.call({',
  25194. ' p: $mod,',
  25195. ' get: function () {',
  25196. ' return this.p.s;',
  25197. ' },',
  25198. ' set: function (v) {',
  25199. ' this.p.s = v;',
  25200. ' }',
  25201. '}, 123);',
  25202. '$mod.s = rtl.refSet($mod.THelper.$new("Init", [$mod.TEnum.blue]));',
  25203. '$mod.s = rtl.refSet($mod.THelper.Init.call({',
  25204. ' p: $mod,',
  25205. ' get: function () {',
  25206. ' return this.p.s;',
  25207. ' },',
  25208. ' set: function (v) {',
  25209. ' this.p.s = v;',
  25210. ' }',
  25211. '}, $mod.TEnum.blue));',
  25212. '']));
  25213. end;
  25214. procedure TTestModule.TestTypeHelper_InterfaceType;
  25215. begin
  25216. StartProgram(false);
  25217. Add([
  25218. '{$interfaces com}',
  25219. '{$modeswitch typehelpers}',
  25220. 'type',
  25221. ' IUnknown = interface',
  25222. ' function _AddRef: longint;',
  25223. ' function _Release: longint;',
  25224. ' end;',
  25225. ' TObject = class(IUnknown)',
  25226. ' function _AddRef: longint; virtual; abstract;',
  25227. ' function _Release: longint; virtual; abstract;',
  25228. ' end;',
  25229. ' THelper = type helper for IUnknown',
  25230. ' procedure Fly(e: byte = 123);',
  25231. ' class procedure Run; static;',
  25232. ' end;',
  25233. 'var',
  25234. ' i: IUnknown;',
  25235. ' o: TObject;',
  25236. 'procedure THelper.Fly(e: byte);',
  25237. 'begin',
  25238. ' i:=Self;',
  25239. ' o:=Self as TObject;',
  25240. ' Self:=nil;',
  25241. ' Self:=i;',
  25242. ' Self:=o;',
  25243. ' with Self do begin',
  25244. ' Fly;',
  25245. ' Fly();',
  25246. ' end;',
  25247. 'end;',
  25248. 'class procedure THelper.Run;',
  25249. 'var l: IUnknown;',
  25250. 'begin',
  25251. ' l.Fly;',
  25252. ' l.Fly();',
  25253. 'end;',
  25254. 'begin',
  25255. ' i.Fly;',
  25256. ' i.Fly();',
  25257. ' i.Run;',
  25258. ' i.Run();',
  25259. ' IUnknown.Run;',
  25260. ' IUnknown.Run();',
  25261. '']);
  25262. ConvertProgram;
  25263. CheckSource('TestTypeHelper_InterfaceType',
  25264. LinesToStr([ // statements
  25265. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  25266. 'rtl.createClass(this, "TObject", null, function () {',
  25267. ' this.$init = function () {',
  25268. ' };',
  25269. ' this.$final = function () {',
  25270. ' };',
  25271. ' rtl.addIntf(this, $mod.IUnknown);',
  25272. '});',
  25273. 'rtl.createHelper(this, "THelper", null, function () {',
  25274. ' this.Fly = function (e) {',
  25275. ' var $ir = rtl.createIntfRefs();',
  25276. ' try {',
  25277. ' rtl.setIntfP($mod, "i", this.get());',
  25278. ' $mod.o = rtl.intfAsClass(this.get(), $mod.TObject);',
  25279. ' this.set(null);',
  25280. ' this.set($mod.i);',
  25281. ' this.set($ir.ref(1, rtl.queryIntfT($mod.o, $mod.IUnknown)));',
  25282. ' var $with = this.get();',
  25283. ' $mod.THelper.Fly.call(this, 123);',
  25284. ' $mod.THelper.Fly.call(this, 123);',
  25285. ' } finally {',
  25286. ' $ir.free();',
  25287. ' };',
  25288. ' };',
  25289. ' this.Run = function () {',
  25290. ' var l = null;',
  25291. ' try {',
  25292. ' $mod.THelper.Fly.call({',
  25293. ' get: function () {',
  25294. ' return l;',
  25295. ' },',
  25296. ' set: function (v) {',
  25297. ' l = rtl.setIntfL(l, v);',
  25298. ' }',
  25299. ' }, 123);',
  25300. ' $mod.THelper.Fly.call({',
  25301. ' get: function () {',
  25302. ' return l;',
  25303. ' },',
  25304. ' set: function (v) {',
  25305. ' l = rtl.setIntfL(l, v);',
  25306. ' }',
  25307. ' }, 123);',
  25308. ' } finally {',
  25309. ' rtl._Release(l);',
  25310. ' };',
  25311. ' };',
  25312. '});',
  25313. 'this.i = null;',
  25314. 'this.o = null;',
  25315. '']),
  25316. LinesToStr([ // $mod.$main
  25317. '$mod.THelper.Fly.call({',
  25318. ' p: $mod,',
  25319. ' get: function () {',
  25320. ' return this.p.i;',
  25321. ' },',
  25322. ' set: function (v) {',
  25323. ' rtl.setIntfP(this.p, "i", v);',
  25324. ' }',
  25325. '}, 123);',
  25326. '$mod.THelper.Fly.call({',
  25327. ' p: $mod,',
  25328. ' get: function () {',
  25329. ' return this.p.i;',
  25330. ' },',
  25331. ' set: function (v) {',
  25332. ' rtl.setIntfP(this.p, "i", v);',
  25333. ' }',
  25334. '}, 123);',
  25335. '$mod.THelper.Run();',
  25336. '$mod.THelper.Run();',
  25337. '$mod.THelper.Run();',
  25338. '$mod.THelper.Run();',
  25339. '']));
  25340. end;
  25341. procedure TTestModule.TestTypeHelper_NestedSelf;
  25342. begin
  25343. StartProgram(false);
  25344. Add([
  25345. '{$modeswitch typehelpers}',
  25346. 'type',
  25347. ' THelper = type helper for string',
  25348. ' procedure Run(Value: string);',
  25349. ' end;',
  25350. 'procedure THelper.Run(Value: string);',
  25351. ' function Sub(i: nativeint): boolean;',
  25352. ' begin',
  25353. ' Result:=Self[i+1]=Value[i];',
  25354. ' end;',
  25355. 'begin',
  25356. ' if Self[3]=Value[4] then ;',
  25357. 'end;',
  25358. 'begin',
  25359. '']);
  25360. ConvertProgram;
  25361. CheckSource('TestTypeHelper_NestedSelf',
  25362. LinesToStr([ // statements
  25363. 'rtl.createHelper(this, "THelper", null, function () {',
  25364. ' this.Run = function (Value) {',
  25365. ' var $Self = this;',
  25366. ' function Sub(i) {',
  25367. ' var Result = false;',
  25368. ' Result = $Self.get().charAt((i + 1) - 1) === Value.charAt(i - 1);',
  25369. ' return Result;',
  25370. ' };',
  25371. ' if ($Self.get().charAt(2) === Value.charAt(3)) ;',
  25372. ' };',
  25373. '});',
  25374. '']),
  25375. LinesToStr([ // $mod.$main
  25376. '']));
  25377. end;
  25378. procedure TTestModule.TestProcType;
  25379. begin
  25380. StartProgram(false);
  25381. Add([
  25382. 'type',
  25383. ' TProcInt = procedure(vI: longint = 1);',
  25384. 'procedure DoIt(vJ: longint);',
  25385. 'begin end;',
  25386. 'var',
  25387. ' b: boolean;',
  25388. ' vP, vQ: tprocint;',
  25389. 'begin',
  25390. ' vp:=nil;',
  25391. ' vp:=vp;',
  25392. ' vp:=@doit;',
  25393. ' vp;',
  25394. ' vp();',
  25395. ' vp(2);',
  25396. ' b:=vp=nil;',
  25397. ' b:=nil=vp;',
  25398. ' b:=vp=vq;',
  25399. ' b:=vp=@doit;',
  25400. ' b:=@doit=vp;',
  25401. ' b:=vp<>nil;',
  25402. ' b:=nil<>vp;',
  25403. ' b:=vp<>vq;',
  25404. ' b:=vp<>@doit;',
  25405. ' b:=@doit<>vp;',
  25406. ' b:=Assigned(vp);',
  25407. ' if Assigned(vp) then ;']);
  25408. ConvertProgram;
  25409. CheckSource('TestProcType',
  25410. LinesToStr([ // statements
  25411. 'this.DoIt = function(vJ) {',
  25412. '};',
  25413. 'this.b = false;',
  25414. 'this.vP = null;',
  25415. 'this.vQ = null;'
  25416. ]),
  25417. LinesToStr([ // $mod.$main
  25418. '$mod.vP = null;',
  25419. '$mod.vP = $mod.vP;',
  25420. '$mod.vP = $mod.DoIt;',
  25421. '$mod.vP(1);',
  25422. '$mod.vP(1);',
  25423. '$mod.vP(2);',
  25424. '$mod.b = $mod.vP === null;',
  25425. '$mod.b = null === $mod.vP;',
  25426. '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
  25427. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  25428. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  25429. '$mod.b = $mod.vP !== null;',
  25430. '$mod.b = null !== $mod.vP;',
  25431. '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
  25432. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  25433. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  25434. '$mod.b = $mod.vP != null;',
  25435. 'if ($mod.vP != null) ;',
  25436. '']));
  25437. end;
  25438. procedure TTestModule.TestProcType_Arg;
  25439. begin
  25440. StartProgram(false);
  25441. Add([
  25442. 'type',
  25443. ' TProcInt = procedure(vI: longint = 1);',
  25444. 'procedure DoIt(vJ: longint); begin end;',
  25445. 'procedure DoSome(vP, vQ: TProcInt);',
  25446. 'var',
  25447. ' b: boolean;',
  25448. 'begin',
  25449. ' vp:=nil;',
  25450. ' vp:=vp;',
  25451. ' vp:=@doit;',
  25452. ' vp;',
  25453. ' vp();',
  25454. ' vp(2);',
  25455. ' b:=vp=nil;',
  25456. ' b:=nil=vp;',
  25457. ' b:=vp=vq;',
  25458. ' b:=vp=@doit;',
  25459. ' b:=@doit=vp;',
  25460. ' b:=vp<>nil;',
  25461. ' b:=nil<>vp;',
  25462. ' b:=vp<>vq;',
  25463. ' b:=vp<>@doit;',
  25464. ' b:=@doit<>vp;',
  25465. ' b:=Assigned(vp);',
  25466. ' if Assigned(vp) then ;',
  25467. 'end;',
  25468. 'begin',
  25469. ' DoSome(@DoIt,nil);']);
  25470. ConvertProgram;
  25471. CheckSource('TestProcType_Arg',
  25472. LinesToStr([ // statements
  25473. 'this.DoIt = function(vJ) {',
  25474. '};',
  25475. 'this.DoSome = function(vP, vQ) {',
  25476. ' var b = false;',
  25477. ' vP = null;',
  25478. ' vP = vP;',
  25479. ' vP = $mod.DoIt;',
  25480. ' vP(1);',
  25481. ' vP(1);',
  25482. ' vP(2);',
  25483. ' b = vP === null;',
  25484. ' b = null === vP;',
  25485. ' b = rtl.eqCallback(vP,vQ);',
  25486. ' b = rtl.eqCallback(vP, $mod.DoIt);',
  25487. ' b = rtl.eqCallback($mod.DoIt, vP);',
  25488. ' b = vP !== null;',
  25489. ' b = null !== vP;',
  25490. ' b = !rtl.eqCallback(vP, vQ);',
  25491. ' b = !rtl.eqCallback(vP, $mod.DoIt);',
  25492. ' b = !rtl.eqCallback($mod.DoIt, vP);',
  25493. ' b = vP != null;',
  25494. ' if (vP != null) ;',
  25495. '};',
  25496. '']),
  25497. LinesToStr([ // $mod.$main
  25498. '$mod.DoSome($mod.DoIt,null);',
  25499. '']));
  25500. end;
  25501. procedure TTestModule.TestProcType_FunctionFPC;
  25502. begin
  25503. StartProgram(false);
  25504. Add('type');
  25505. Add(' TFuncInt = function(vA: longint = 1): longint;');
  25506. Add('function DoIt(vI: longint): longint;');
  25507. Add('begin end;');
  25508. Add('var');
  25509. Add(' b: boolean;');
  25510. Add(' vP, vQ: tfuncint;');
  25511. Add('begin');
  25512. Add(' vp:=nil;');
  25513. Add(' vp:=vp;');
  25514. Add(' vp:=@doit;'); // ok in fpc and delphi
  25515. //Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  25516. Add(' vp;'); // ok in fpc and delphi
  25517. Add(' vp();');
  25518. Add(' vp(2);');
  25519. Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  25520. Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  25521. Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  25522. Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  25523. Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  25524. //Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
  25525. Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
  25526. Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  25527. Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  25528. Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  25529. Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  25530. Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  25531. //Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
  25532. Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
  25533. Add(' b:=Assigned(vp);');
  25534. //Add(' doit(vp);'); // illegal in fpc, ok in delphi
  25535. Add(' doit(vp());'); // ok in fpc and delphi
  25536. Add(' doit(vp(2));'); // ok in fpc and delphi
  25537. ConvertProgram;
  25538. CheckSource('TestProcType_FunctionFPC',
  25539. LinesToStr([ // statements
  25540. 'this.DoIt = function(vI) {',
  25541. ' var Result = 0;',
  25542. ' return Result;',
  25543. '};',
  25544. 'this.b = false;',
  25545. 'this.vP = null;',
  25546. 'this.vQ = null;'
  25547. ]),
  25548. LinesToStr([ // $mod.$main
  25549. '$mod.vP = null;',
  25550. '$mod.vP = $mod.vP;',
  25551. '$mod.vP = $mod.DoIt;',
  25552. '$mod.vP(1);',
  25553. '$mod.vP(1);',
  25554. '$mod.vP(2);',
  25555. '$mod.b = $mod.vP === null;',
  25556. '$mod.b = null === $mod.vP;',
  25557. '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
  25558. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  25559. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  25560. '$mod.b = 4 === $mod.vP(1);',
  25561. '$mod.b = $mod.vP !== null;',
  25562. '$mod.b = null !== $mod.vP;',
  25563. '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
  25564. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  25565. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  25566. '$mod.b = 6 !== $mod.vP(1);',
  25567. '$mod.b = $mod.vP != null;',
  25568. '$mod.DoIt($mod.vP(1));',
  25569. '$mod.DoIt($mod.vP(2));',
  25570. '']));
  25571. end;
  25572. procedure TTestModule.TestProcType_FunctionDelphi;
  25573. begin
  25574. StartProgram(false);
  25575. Add('{$mode Delphi}');
  25576. Add('type');
  25577. Add(' TFuncInt = function(vA: longint = 1): longint;');
  25578. Add('function DoIt(vI: longint): longint;');
  25579. Add('begin end;');
  25580. Add('var');
  25581. Add(' b: boolean;');
  25582. Add(' vP, vQ: tfuncint;');
  25583. Add('begin');
  25584. Add(' vp:=nil;');
  25585. Add(' vp:=vp;');
  25586. Add(' vp:=@doit;'); // ok in fpc and delphi
  25587. Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  25588. Add(' vp;'); // ok in fpc and delphi
  25589. Add(' vp();');
  25590. Add(' vp(2);');
  25591. //Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  25592. //Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  25593. Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  25594. //Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  25595. //Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  25596. Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
  25597. Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
  25598. //Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  25599. //Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  25600. Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  25601. //Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  25602. //Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  25603. Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
  25604. Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
  25605. Add(' b:=Assigned(vp);');
  25606. Add(' doit(vp);'); // illegal in fpc, ok in delphi
  25607. Add(' doit(vp());'); // ok in fpc and delphi
  25608. Add(' doit(vp(2));'); // ok in fpc and delphi *)
  25609. ConvertProgram;
  25610. CheckSource('TestProcType_FunctionDelphi',
  25611. LinesToStr([ // statements
  25612. 'this.DoIt = function(vI) {',
  25613. ' var Result = 0;',
  25614. ' return Result;',
  25615. '};',
  25616. 'this.b = false;',
  25617. 'this.vP = null;',
  25618. 'this.vQ = null;'
  25619. ]),
  25620. LinesToStr([ // $mod.$main
  25621. '$mod.vP = null;',
  25622. '$mod.vP = $mod.vP;',
  25623. '$mod.vP = $mod.DoIt;',
  25624. '$mod.vP = $mod.DoIt;',
  25625. '$mod.vP(1);',
  25626. '$mod.vP(1);',
  25627. '$mod.vP(2);',
  25628. '$mod.b = $mod.vP(1) === $mod.vQ(1);',
  25629. '$mod.b = $mod.vP(1) === 3;',
  25630. '$mod.b = 4 === $mod.vP(1);',
  25631. '$mod.b = $mod.vP(1) !== $mod.vQ(1);',
  25632. '$mod.b = $mod.vP(1) !== 5;',
  25633. '$mod.b = 6 !== $mod.vP(1);',
  25634. '$mod.b = $mod.vP != null;',
  25635. '$mod.DoIt($mod.vP(1));',
  25636. '$mod.DoIt($mod.vP(1));',
  25637. '$mod.DoIt($mod.vP(2));',
  25638. '']));
  25639. end;
  25640. procedure TTestModule.TestProcType_ProcedureDelphi;
  25641. begin
  25642. StartProgram(false);
  25643. Add('{$mode Delphi}');
  25644. Add('type');
  25645. Add(' TProc = procedure;');
  25646. Add('procedure DoIt;');
  25647. Add('begin end;');
  25648. Add('var');
  25649. Add(' b: boolean;');
  25650. Add(' vP, vQ: tproc;');
  25651. Add('begin');
  25652. Add(' vp:=nil;');
  25653. Add(' vp:=vp;');
  25654. Add(' vp:=vq;');
  25655. 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
  25656. Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  25657. //Add(' vp:=@doit;'); // illegal in fpc, ok in delphi (because Delphi treats @F as Pointer), not supported by resolver
  25658. Add(' vp;'); // ok in fpc and delphi
  25659. Add(' vp();');
  25660. // equal
  25661. //Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  25662. Add(' b:=@@vp=nil;'); // ok in fpc delphi mode, ok in delphi
  25663. //Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  25664. Add(' b:=nil=@@vp;'); // ok in fpc delphi mode, ok in delphi
  25665. Add(' b:=@@vp=@@vq;'); // ok in fpc delphi mode, ok in Delphi
  25666. //Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  25667. //Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  25668. Add(' b:=@@vp=@doit;'); // ok in fpc delphi mode, ok in delphi
  25669. //Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  25670. Add(' b:=@doit=@@vp;'); // ok in fpc delphi mode, ok in delphi
  25671. // unequal
  25672. //Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  25673. Add(' b:=@@vp<>nil;'); // ok in fpc mode delphi, ok in delphi
  25674. //Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  25675. Add(' b:=nil<>@@vp;'); // ok in fpc mode delphi, ok in delphi
  25676. //Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  25677. Add(' b:=@@vp<>@@vq;'); // ok in fpc mode delphi, ok in delphi
  25678. //Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  25679. Add(' b:=@@vp<>@doit;'); // ok in fpc mode delphi, illegal in delphi
  25680. //Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  25681. Add(' b:=@doit<>@@vp;'); // ok in fpc mode delphi, illegal in delphi
  25682. Add(' b:=Assigned(vp);');
  25683. ConvertProgram;
  25684. CheckSource('TestProcType_ProcedureDelphi',
  25685. LinesToStr([ // statements
  25686. 'this.DoIt = function() {',
  25687. '};',
  25688. 'this.b = false;',
  25689. 'this.vP = null;',
  25690. 'this.vQ = null;'
  25691. ]),
  25692. LinesToStr([ // $mod.$main
  25693. '$mod.vP = null;',
  25694. '$mod.vP = $mod.vP;',
  25695. '$mod.vP = $mod.vQ;',
  25696. '$mod.vP = $mod.DoIt;',
  25697. '$mod.vP = $mod.DoIt;',
  25698. '$mod.vP();',
  25699. '$mod.vP();',
  25700. '$mod.b = $mod.vP === null;',
  25701. '$mod.b = null === $mod.vP;',
  25702. '$mod.b = rtl.eqCallback($mod.vP, $mod.vQ);',
  25703. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  25704. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  25705. '$mod.b = $mod.vP !== null;',
  25706. '$mod.b = null !== $mod.vP;',
  25707. '$mod.b = !rtl.eqCallback($mod.vP, $mod.vQ);',
  25708. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  25709. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  25710. '$mod.b = $mod.vP != null;',
  25711. '']));
  25712. end;
  25713. procedure TTestModule.TestProcType_AsParam;
  25714. begin
  25715. StartProgram(false);
  25716. Add('type');
  25717. Add(' TFuncInt = function(vA: longint = 1): longint;');
  25718. Add('procedure DoIt(vG: tfuncint; const vH: tfuncint; var vI: tfuncint);');
  25719. Add('var vJ: tfuncint;');
  25720. Add('begin');
  25721. Add(' vg:=vg;');
  25722. Add(' vj:=vh;');
  25723. Add(' vi:=vi;');
  25724. Add(' doit(vg,vg,vg);');
  25725. Add(' doit(vh,vh,vj);');
  25726. Add(' doit(vi,vi,vi);');
  25727. Add(' doit(vj,vj,vj);');
  25728. Add('end;');
  25729. Add('var i: tfuncint;');
  25730. Add('begin');
  25731. Add(' doit(i,i,i);');
  25732. ConvertProgram;
  25733. CheckSource('TestProcType_AsParam',
  25734. LinesToStr([ // statements
  25735. 'this.DoIt = function (vG,vH,vI) {',
  25736. ' var vJ = null;',
  25737. ' vG = vG;',
  25738. ' vJ = vH;',
  25739. ' vI.set(vI.get());',
  25740. ' $mod.DoIt(vG, vG, {',
  25741. ' get: function () {',
  25742. ' return vG;',
  25743. ' },',
  25744. ' set: function (v) {',
  25745. ' vG = v;',
  25746. ' }',
  25747. ' });',
  25748. ' $mod.DoIt(vH, vH, {',
  25749. ' get: function () {',
  25750. ' return vJ;',
  25751. ' },',
  25752. ' set: function (v) {',
  25753. ' vJ = v;',
  25754. ' }',
  25755. ' });',
  25756. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  25757. ' $mod.DoIt(vJ, vJ, {',
  25758. ' get: function () {',
  25759. ' return vJ;',
  25760. ' },',
  25761. ' set: function (v) {',
  25762. ' vJ = v;',
  25763. ' }',
  25764. ' });',
  25765. '};',
  25766. 'this.i = null;'
  25767. ]),
  25768. LinesToStr([
  25769. '$mod.DoIt($mod.i,$mod.i,{',
  25770. ' p: $mod,',
  25771. ' get: function () {',
  25772. ' return this.p.i;',
  25773. ' },',
  25774. ' set: function (v) {',
  25775. ' this.p.i = v;',
  25776. ' }',
  25777. '});'
  25778. ]));
  25779. end;
  25780. procedure TTestModule.TestProcType_MethodFPC;
  25781. begin
  25782. StartProgram(false);
  25783. Add('type');
  25784. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  25785. Add(' TObject = class');
  25786. Add(' function DoIt(vA: longint = 1): longint;');
  25787. Add(' end;');
  25788. Add('function TObject.DoIt(vA: longint = 1): longint;');
  25789. Add('begin');
  25790. Add('end;');
  25791. Add('var');
  25792. Add(' Obj: TObject;');
  25793. Add(' vP: tfuncint;');
  25794. Add(' b: boolean;');
  25795. Add('begin');
  25796. Add(' vp:[email protected];'); // ok in fpc and delphi
  25797. //Add(' vp:=obj.doit;'); // illegal in fpc, ok in delphi
  25798. Add(' vp;'); // ok in fpc and delphi
  25799. Add(' vp();');
  25800. Add(' vp(2);');
  25801. Add(' b:[email protected];'); // ok in fpc, illegal in delphi
  25802. Add(' b:[email protected]=vp;'); // ok in fpc, illegal in delphi
  25803. Add(' b:=vp<>@obj.doit;'); // ok in fpc, illegal in delphi
  25804. Add(' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
  25805. ConvertProgram;
  25806. CheckSource('TestProcType_MethodFPC',
  25807. LinesToStr([ // statements
  25808. 'rtl.createClass(this, "TObject", null, function () {',
  25809. ' this.$init = function () {',
  25810. ' };',
  25811. ' this.$final = function () {',
  25812. ' };',
  25813. ' this.DoIt = function (vA) {',
  25814. ' var Result = 0;',
  25815. ' return Result;',
  25816. ' };',
  25817. '});',
  25818. 'this.Obj = null;',
  25819. 'this.vP = null;',
  25820. 'this.b = false;'
  25821. ]),
  25822. LinesToStr([
  25823. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  25824. '$mod.vP(1);',
  25825. '$mod.vP(1);',
  25826. '$mod.vP(2);',
  25827. '$mod.b = rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
  25828. '$mod.b = rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
  25829. '$mod.b = !rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
  25830. '$mod.b = !rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
  25831. '']));
  25832. end;
  25833. procedure TTestModule.TestProcType_MethodDelphi;
  25834. begin
  25835. StartProgram(false);
  25836. Add([
  25837. '{$mode delphi}',
  25838. 'type',
  25839. ' TFuncInt = function(vA: longint = 1): longint of object;',
  25840. ' TObject = class',
  25841. ' function DoIt(vA: longint = 1): longint;',
  25842. ' end;',
  25843. 'function TObject.DoIt(vA: longint = 1): longint;',
  25844. 'begin',
  25845. 'end;',
  25846. 'var',
  25847. ' Obj: TObject;',
  25848. ' vP: tfuncint;',
  25849. ' b: boolean;',
  25850. 'begin',
  25851. ' vp:[email protected];', // ok in fpc and delphi
  25852. ' vp:=obj.doit;', // illegal in fpc, ok in delphi
  25853. ' vp;', // ok in fpc and delphi
  25854. ' vp();',
  25855. ' vp(2);',
  25856. //' b:[email protected];', // ok in fpc, illegal in delphi
  25857. //' b:[email protected]=vp;', // ok in fpc, illegal in delphi
  25858. //' b:=vp<>@obj.doit;', // ok in fpc, illegal in delphi
  25859. //' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
  25860. '']);
  25861. ConvertProgram;
  25862. CheckSource('TestProcType_MethodDelphi',
  25863. LinesToStr([ // statements
  25864. 'rtl.createClass(this, "TObject", null, function () {',
  25865. ' this.$init = function () {',
  25866. ' };',
  25867. ' this.$final = function () {',
  25868. ' };',
  25869. ' this.DoIt = function (vA) {',
  25870. ' var Result = 0;',
  25871. ' return Result;',
  25872. ' };',
  25873. '});',
  25874. 'this.Obj = null;',
  25875. 'this.vP = null;',
  25876. 'this.b = false;'
  25877. ]),
  25878. LinesToStr([
  25879. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  25880. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  25881. '$mod.vP(1);',
  25882. '$mod.vP(1);',
  25883. '$mod.vP(2);',
  25884. '']));
  25885. end;
  25886. procedure TTestModule.TestProcType_PropertyFPC;
  25887. begin
  25888. StartProgram(false);
  25889. Add('type');
  25890. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  25891. Add(' TObject = class');
  25892. Add(' FOnFoo: TFuncInt;');
  25893. Add(' function DoIt(vA: longint = 1): longint;');
  25894. Add(' function GetFoo: TFuncInt;');
  25895. Add(' procedure SetFoo(const Value: TFuncInt);');
  25896. Add(' function GetEvents(Index: longint): TFuncInt;');
  25897. Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
  25898. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  25899. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  25900. Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
  25901. Add(' end;');
  25902. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  25903. Add('function tobject.getfoo: tfuncint; begin end;');
  25904. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  25905. Add('function tobject.getevents(index: longint): tfuncint; begin end;');
  25906. Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
  25907. Add('var');
  25908. Add(' Obj: TObject;');
  25909. Add(' vP: tfuncint;');
  25910. Add(' b: boolean;');
  25911. Add('begin');
  25912. Add(' obj.onfoo:=nil;');
  25913. Add(' obj.onbar:=nil;');
  25914. Add(' obj.events[1]:=nil;');
  25915. Add(' obj.onfoo:=obj.onfoo;');
  25916. Add(' obj.onbar:=obj.onbar;');
  25917. Add(' obj.events[2]:=obj.events[3];');
  25918. Add(' obj.onfoo:[email protected];');
  25919. Add(' obj.onbar:[email protected];');
  25920. Add(' obj.events[4]:[email protected];');
  25921. //Add(' obj.onfoo:=obj.doit;'); // delphi
  25922. //Add(' obj.onbar:=obj.doit;'); // delphi
  25923. //Add(' obj.events[4]:=obj.doit;'); // delphi
  25924. Add(' obj.onfoo;');
  25925. Add(' obj.onbar;');
  25926. //Add(' obj.events[5];'); ToDo in pasresolver
  25927. Add(' obj.onfoo();');
  25928. Add(' obj.onbar();');
  25929. Add(' obj.events[6]();');
  25930. Add(' b:=obj.onfoo=nil;');
  25931. Add(' b:=obj.onbar=nil;');
  25932. Add(' b:=obj.events[7]=nil;');
  25933. Add(' b:=obj.onfoo<>nil;');
  25934. Add(' b:=obj.onbar<>nil;');
  25935. Add(' b:=obj.events[8]<>nil;');
  25936. Add(' b:=obj.onfoo=vp;');
  25937. Add(' b:=obj.onbar=vp;');
  25938. Add(' b:=obj.events[9]=vp;');
  25939. Add(' b:=obj.onfoo=obj.onfoo;');
  25940. Add(' b:=obj.onbar=obj.onfoo;');
  25941. Add(' b:=obj.events[10]=obj.onfoo;');
  25942. Add(' b:=obj.onfoo<>obj.onfoo;');
  25943. Add(' b:=obj.onbar<>obj.onfoo;');
  25944. Add(' b:=obj.events[11]<>obj.onfoo;');
  25945. Add(' b:[email protected];');
  25946. Add(' b:[email protected];');
  25947. Add(' b:=obj.events[12][email protected];');
  25948. Add(' b:=obj.onfoo<>@obj.doit;');
  25949. Add(' b:=obj.onbar<>@obj.doit;');
  25950. Add(' b:=obj.events[12]<>@obj.doit;');
  25951. Add(' b:=Assigned(obj.onfoo);');
  25952. Add(' b:=Assigned(obj.onbar);');
  25953. Add(' b:=Assigned(obj.events[13]);');
  25954. ConvertProgram;
  25955. CheckSource('TestProcType_PropertyFPC',
  25956. LinesToStr([ // statements
  25957. 'rtl.createClass(this, "TObject", null, function () {',
  25958. ' this.$init = function () {',
  25959. ' this.FOnFoo = null;',
  25960. ' };',
  25961. ' this.$final = function () {',
  25962. ' this.FOnFoo = undefined;',
  25963. ' };',
  25964. ' this.DoIt = function (vA) {',
  25965. ' var Result = 0;',
  25966. ' return Result;',
  25967. ' };',
  25968. 'this.GetFoo = function () {',
  25969. ' var Result = null;',
  25970. ' return Result;',
  25971. '};',
  25972. 'this.SetFoo = function (Value) {',
  25973. '};',
  25974. 'this.GetEvents = function (Index) {',
  25975. ' var Result = null;',
  25976. ' return Result;',
  25977. '};',
  25978. 'this.SetEvents = function (Index, Value) {',
  25979. '};',
  25980. '});',
  25981. 'this.Obj = null;',
  25982. 'this.vP = null;',
  25983. 'this.b = false;'
  25984. ]),
  25985. LinesToStr([
  25986. '$mod.Obj.FOnFoo = null;',
  25987. '$mod.Obj.SetFoo(null);',
  25988. '$mod.Obj.SetEvents(1, null);',
  25989. '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
  25990. '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
  25991. '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
  25992. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  25993. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  25994. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  25995. '$mod.Obj.FOnFoo(1);',
  25996. '$mod.Obj.GetFoo();',
  25997. '$mod.Obj.FOnFoo(1);',
  25998. '$mod.Obj.GetFoo()(1);',
  25999. '$mod.Obj.GetEvents(6)(1);',
  26000. '$mod.b = $mod.Obj.FOnFoo === null;',
  26001. '$mod.b = $mod.Obj.GetFoo() === null;',
  26002. '$mod.b = $mod.Obj.GetEvents(7) === null;',
  26003. '$mod.b = $mod.Obj.FOnFoo !== null;',
  26004. '$mod.b = $mod.Obj.GetFoo() !== null;',
  26005. '$mod.b = $mod.Obj.GetEvents(8) !== null;',
  26006. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.vP);',
  26007. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.vP);',
  26008. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(9), $mod.vP);',
  26009. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
  26010. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
  26011. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(10), $mod.Obj.FOnFoo);',
  26012. '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
  26013. '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
  26014. '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(11), $mod.Obj.FOnFoo);',
  26015. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
  26016. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
  26017. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
  26018. '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
  26019. '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
  26020. '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
  26021. '$mod.b = $mod.Obj.FOnFoo != null;',
  26022. '$mod.b = $mod.Obj.GetFoo() != null;',
  26023. '$mod.b = $mod.Obj.GetEvents(13) != null;',
  26024. '']));
  26025. end;
  26026. procedure TTestModule.TestProcType_PropertyDelphi;
  26027. begin
  26028. StartProgram(false);
  26029. Add('{$mode delphi}');
  26030. Add('type');
  26031. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  26032. Add(' TObject = class');
  26033. Add(' FOnFoo: TFuncInt;');
  26034. Add(' function DoIt(vA: longint = 1): longint;');
  26035. Add(' function GetFoo: TFuncInt;');
  26036. Add(' procedure SetFoo(const Value: TFuncInt);');
  26037. Add(' function GetEvents(Index: longint): TFuncInt;');
  26038. Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
  26039. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  26040. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  26041. Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
  26042. Add(' end;');
  26043. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  26044. Add('function tobject.getfoo: tfuncint; begin end;');
  26045. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  26046. Add('function tobject.getevents(index: longint): tfuncint; begin end;');
  26047. Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
  26048. Add('var');
  26049. Add(' Obj: TObject;');
  26050. Add(' vP: tfuncint;');
  26051. Add(' b: boolean;');
  26052. Add('begin');
  26053. Add(' obj.onfoo:=nil;');
  26054. Add(' obj.onbar:=nil;');
  26055. Add(' obj.events[1]:=nil;');
  26056. Add(' obj.onfoo:=obj.onfoo;');
  26057. Add(' obj.onbar:=obj.onbar;');
  26058. Add(' obj.events[2]:=obj.events[3];');
  26059. Add(' obj.onfoo:[email protected];');
  26060. Add(' obj.onbar:[email protected];');
  26061. Add(' obj.events[4]:[email protected];');
  26062. Add(' obj.onfoo:=obj.doit;'); // delphi
  26063. Add(' obj.onbar:=obj.doit;'); // delphi
  26064. Add(' obj.events[4]:=obj.doit;'); // delphi
  26065. Add(' obj.onfoo;');
  26066. Add(' obj.onbar;');
  26067. //Add(' obj.events[5];'); ToDo in pasresolver
  26068. Add(' obj.onfoo();');
  26069. Add(' obj.onbar();');
  26070. Add(' obj.events[6]();');
  26071. //Add(' b:=obj.onfoo=nil;'); // fpc
  26072. //Add(' b:=obj.onbar=nil;'); // fpc
  26073. //Add(' b:=obj.events[7]=nil;'); // fpc
  26074. //Add(' b:=obj.onfoo<>nil;'); // fpc
  26075. //Add(' b:=obj.onbar<>nil;'); // fpc
  26076. //Add(' b:=obj.events[8]<>nil;'); // fpc
  26077. Add(' b:=obj.onfoo=vp;');
  26078. Add(' b:=obj.onbar=vp;');
  26079. //Add(' b:=obj.events[9]=vp;'); ToDo in pasresolver
  26080. Add(' b:=obj.onfoo=obj.onfoo;');
  26081. Add(' b:=obj.onbar=obj.onfoo;');
  26082. //Add(' b:=obj.events[10]=obj.onfoo;'); // ToDo in pasresolver
  26083. Add(' b:=obj.onfoo<>obj.onfoo;');
  26084. Add(' b:=obj.onbar<>obj.onfoo;');
  26085. //Add(' b:=obj.events[11]<>obj.onfoo;'); // ToDo in pasresolver
  26086. //Add(' b:[email protected];'); // fpc
  26087. //Add(' b:[email protected];'); // fpc
  26088. //Add(' b:=obj.events[12][email protected];'); // fpc
  26089. //Add(' b:=obj.onfoo<>@obj.doit;'); // fpc
  26090. //Add(' b:=obj.onbar<>@obj.doit;'); // fpc
  26091. //Add(' b:=obj.events[12]<>@obj.doit;'); // fpc
  26092. Add(' b:=Assigned(obj.onfoo);');
  26093. Add(' b:=Assigned(obj.onbar);');
  26094. Add(' b:=Assigned(obj.events[13]);');
  26095. ConvertProgram;
  26096. CheckSource('TestProcType_PropertyDelphi',
  26097. LinesToStr([ // statements
  26098. 'rtl.createClass(this, "TObject", null, function () {',
  26099. ' this.$init = function () {',
  26100. ' this.FOnFoo = null;',
  26101. ' };',
  26102. ' this.$final = function () {',
  26103. ' this.FOnFoo = undefined;',
  26104. ' };',
  26105. ' this.DoIt = function (vA) {',
  26106. ' var Result = 0;',
  26107. ' return Result;',
  26108. ' };',
  26109. 'this.GetFoo = function () {',
  26110. ' var Result = null;',
  26111. ' return Result;',
  26112. '};',
  26113. 'this.SetFoo = function (Value) {',
  26114. '};',
  26115. 'this.GetEvents = function (Index) {',
  26116. ' var Result = null;',
  26117. ' return Result;',
  26118. '};',
  26119. 'this.SetEvents = function (Index, Value) {',
  26120. '};',
  26121. '});',
  26122. 'this.Obj = null;',
  26123. 'this.vP = null;',
  26124. 'this.b = false;'
  26125. ]),
  26126. LinesToStr([
  26127. '$mod.Obj.FOnFoo = null;',
  26128. '$mod.Obj.SetFoo(null);',
  26129. '$mod.Obj.SetEvents(1, null);',
  26130. '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
  26131. '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
  26132. '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
  26133. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  26134. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  26135. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  26136. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  26137. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  26138. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  26139. '$mod.Obj.FOnFoo(1);',
  26140. '$mod.Obj.GetFoo();',
  26141. '$mod.Obj.FOnFoo(1);',
  26142. '$mod.Obj.GetFoo()(1);',
  26143. '$mod.Obj.GetEvents(6)(1);',
  26144. '$mod.b = $mod.Obj.FOnFoo(1) === $mod.vP(1);',
  26145. '$mod.b = $mod.Obj.GetFoo() === $mod.vP(1);',
  26146. '$mod.b = $mod.Obj.FOnFoo(1) === $mod.Obj.FOnFoo(1);',
  26147. '$mod.b = $mod.Obj.GetFoo() === $mod.Obj.FOnFoo(1);',
  26148. '$mod.b = $mod.Obj.FOnFoo(1) !== $mod.Obj.FOnFoo(1);',
  26149. '$mod.b = $mod.Obj.GetFoo() !== $mod.Obj.FOnFoo(1);',
  26150. '$mod.b = $mod.Obj.FOnFoo != null;',
  26151. '$mod.b = $mod.Obj.GetFoo() != null;',
  26152. '$mod.b = $mod.Obj.GetEvents(13) != null;',
  26153. '']));
  26154. end;
  26155. procedure TTestModule.TestProcType_WithClassInstDoPropertyFPC;
  26156. begin
  26157. StartProgram(false);
  26158. Add('type');
  26159. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  26160. Add(' TObject = class');
  26161. Add(' FOnFoo: TFuncInt;');
  26162. Add(' function DoIt(vA: longint = 1): longint;');
  26163. Add(' function GetFoo: TFuncInt;');
  26164. Add(' procedure SetFoo(const Value: TFuncInt);');
  26165. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  26166. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  26167. Add(' end;');
  26168. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  26169. Add('function tobject.getfoo: tfuncint; begin end;');
  26170. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  26171. Add('var');
  26172. Add(' Obj: TObject;');
  26173. Add(' vP: tfuncint;');
  26174. Add(' b: boolean;');
  26175. Add('begin');
  26176. Add('with obj do begin');
  26177. Add(' fonfoo:=nil;');
  26178. Add(' onfoo:=nil;');
  26179. Add(' onbar:=nil;');
  26180. Add(' fonfoo:=fonfoo;');
  26181. Add(' onfoo:=onfoo;');
  26182. Add(' onbar:=onbar;');
  26183. Add(' fonfoo:=@doit;');
  26184. Add(' onfoo:=@doit;');
  26185. Add(' onbar:=@doit;');
  26186. //Add(' fonfoo:=doit;'); // delphi
  26187. //Add(' onfoo:=doit;'); // delphi
  26188. //Add(' onbar:=doit;'); // delphi
  26189. Add(' fonfoo;');
  26190. Add(' onfoo;');
  26191. Add(' onbar;');
  26192. Add(' fonfoo();');
  26193. Add(' onfoo();');
  26194. Add(' onbar();');
  26195. Add(' b:=fonfoo=nil;');
  26196. Add(' b:=onfoo=nil;');
  26197. Add(' b:=onbar=nil;');
  26198. Add(' b:=fonfoo<>nil;');
  26199. Add(' b:=onfoo<>nil;');
  26200. Add(' b:=onbar<>nil;');
  26201. Add(' b:=fonfoo=vp;');
  26202. Add(' b:=onfoo=vp;');
  26203. Add(' b:=onbar=vp;');
  26204. Add(' b:=fonfoo=fonfoo;');
  26205. Add(' b:=onfoo=onfoo;');
  26206. Add(' b:=onbar=onfoo;');
  26207. Add(' b:=fonfoo<>fonfoo;');
  26208. Add(' b:=onfoo<>onfoo;');
  26209. Add(' b:=onbar<>onfoo;');
  26210. Add(' b:=fonfoo=@doit;');
  26211. Add(' b:=onfoo=@doit;');
  26212. Add(' b:=onbar=@doit;');
  26213. Add(' b:=fonfoo<>@doit;');
  26214. Add(' b:=onfoo<>@doit;');
  26215. Add(' b:=onbar<>@doit;');
  26216. Add(' b:=Assigned(fonfoo);');
  26217. Add(' b:=Assigned(onfoo);');
  26218. Add(' b:=Assigned(onbar);');
  26219. Add('end;');
  26220. ConvertProgram;
  26221. CheckSource('TestProcType_WithClassInstDoPropertyFPC',
  26222. LinesToStr([ // statements
  26223. 'rtl.createClass(this, "TObject", null, function () {',
  26224. ' this.$init = function () {',
  26225. ' this.FOnFoo = null;',
  26226. ' };',
  26227. ' this.$final = function () {',
  26228. ' this.FOnFoo = undefined;',
  26229. ' };',
  26230. ' this.DoIt = function (vA) {',
  26231. ' var Result = 0;',
  26232. ' return Result;',
  26233. ' };',
  26234. ' this.GetFoo = function () {',
  26235. ' var Result = null;',
  26236. ' return Result;',
  26237. ' };',
  26238. ' this.SetFoo = function (Value) {',
  26239. ' };',
  26240. '});',
  26241. 'this.Obj = null;',
  26242. 'this.vP = null;',
  26243. 'this.b = false;'
  26244. ]),
  26245. LinesToStr([
  26246. 'var $with = $mod.Obj;',
  26247. '$with.FOnFoo = null;',
  26248. '$with.FOnFoo = null;',
  26249. '$with.SetFoo(null);',
  26250. '$with.FOnFoo = $with.FOnFoo;',
  26251. '$with.FOnFoo = $with.FOnFoo;',
  26252. '$with.SetFoo($with.GetFoo());',
  26253. '$with.FOnFoo = rtl.createCallback($with, "DoIt");',
  26254. '$with.FOnFoo = rtl.createCallback($with, "DoIt");',
  26255. '$with.SetFoo(rtl.createCallback($with, "DoIt"));',
  26256. '$with.FOnFoo(1);',
  26257. '$with.FOnFoo(1);',
  26258. '$with.GetFoo();',
  26259. '$with.FOnFoo(1);',
  26260. '$with.FOnFoo(1);',
  26261. '$with.GetFoo()(1);',
  26262. '$mod.b = $with.FOnFoo === null;',
  26263. '$mod.b = $with.FOnFoo === null;',
  26264. '$mod.b = $with.GetFoo() === null;',
  26265. '$mod.b = $with.FOnFoo !== null;',
  26266. '$mod.b = $with.FOnFoo !== null;',
  26267. '$mod.b = $with.GetFoo() !== null;',
  26268. '$mod.b = rtl.eqCallback($with.FOnFoo, $mod.vP);',
  26269. '$mod.b = rtl.eqCallback($with.FOnFoo, $mod.vP);',
  26270. '$mod.b = rtl.eqCallback($with.GetFoo(), $mod.vP);',
  26271. '$mod.b = rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  26272. '$mod.b = rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  26273. '$mod.b = rtl.eqCallback($with.GetFoo(), $with.FOnFoo);',
  26274. '$mod.b = !rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  26275. '$mod.b = !rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  26276. '$mod.b = !rtl.eqCallback($with.GetFoo(), $with.FOnFoo);',
  26277. '$mod.b = rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  26278. '$mod.b = rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  26279. '$mod.b = rtl.eqCallback($with.GetFoo(), rtl.createCallback($with, "DoIt"));',
  26280. '$mod.b = !rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  26281. '$mod.b = !rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  26282. '$mod.b = !rtl.eqCallback($with.GetFoo(), rtl.createCallback($with, "DoIt"));',
  26283. '$mod.b = $with.FOnFoo != null;',
  26284. '$mod.b = $with.FOnFoo != null;',
  26285. '$mod.b = $with.GetFoo() != null;',
  26286. '']));
  26287. end;
  26288. procedure TTestModule.TestProcType_Nested;
  26289. begin
  26290. StartProgram(false);
  26291. Add([
  26292. 'type',
  26293. ' TProcInt = procedure(vI: longint = 1);',
  26294. 'procedure DoIt(vJ: longint);',
  26295. 'var aProc: TProcInt;',
  26296. ' b: boolean;',
  26297. ' procedure Sub(vK: longint);',
  26298. ' var aSub: TProcInt;',
  26299. ' procedure SubSub(vK: longint);',
  26300. ' var aSubSub: TProcInt;',
  26301. ' begin;',
  26302. ' aProc:=@DoIt;',
  26303. ' aSub:=@DoIt;',
  26304. ' aSubSub:=@DoIt;',
  26305. ' aProc:=@Sub;',
  26306. ' aSub:=@Sub;',
  26307. ' aSubSub:=@Sub;',
  26308. ' aProc:=@SubSub;',
  26309. ' aSub:=@SubSub;',
  26310. ' aSubSub:=@SubSub;',
  26311. ' end;',
  26312. ' begin;',
  26313. ' end;',
  26314. 'begin;',
  26315. ' aProc:=@Sub;',
  26316. ' b:=aProc=@Sub;',
  26317. ' b:=@Sub=aProc;',
  26318. 'end;',
  26319. 'begin',
  26320. '']);
  26321. ConvertProgram;
  26322. CheckSource('TestProcType_Nested',
  26323. LinesToStr([ // statements
  26324. 'this.DoIt = function (vJ) {',
  26325. ' var aProc = null;',
  26326. ' var b = false;',
  26327. ' function Sub(vK) {',
  26328. ' var aSub = null;',
  26329. ' function SubSub(vK) {',
  26330. ' var aSubSub = null;',
  26331. ' aProc = $mod.DoIt;',
  26332. ' aSub = $mod.DoIt;',
  26333. ' aSubSub = $mod.DoIt;',
  26334. ' aProc = Sub;',
  26335. ' aSub = Sub;',
  26336. ' aSubSub = Sub;',
  26337. ' aProc = SubSub;',
  26338. ' aSub = SubSub;',
  26339. ' aSubSub = SubSub;',
  26340. ' };',
  26341. ' };',
  26342. ' aProc = Sub;',
  26343. ' b = rtl.eqCallback(aProc, Sub);',
  26344. ' b = rtl.eqCallback(Sub, aProc);',
  26345. '};',
  26346. '']),
  26347. LinesToStr([ // $mod.$main
  26348. '']));
  26349. end;
  26350. procedure TTestModule.TestProcType_NestedOfObject;
  26351. begin
  26352. StartProgram(false);
  26353. Add([
  26354. 'type',
  26355. ' TProcInt = procedure(vI: longint = 1) of object;',
  26356. ' TObject = class',
  26357. ' procedure DoIt(vJ: longint);',
  26358. ' end;',
  26359. 'procedure TObject.DoIt(vJ: longint);',
  26360. 'var aProc: TProcInt;',
  26361. ' b: boolean;',
  26362. ' procedure Sub(vK: longint);',
  26363. ' var aSub: TProcInt;',
  26364. ' procedure SubSub(vK: longint);',
  26365. ' var aSubSub: TProcInt;',
  26366. ' begin;',
  26367. ' aProc:=@DoIt;',
  26368. ' aSub:=@DoIt;',
  26369. ' aSubSub:=@DoIt;',
  26370. ' aProc:=@Sub;',
  26371. ' aSub:=@Sub;',
  26372. ' aSubSub:=@Sub;',
  26373. ' aProc:=@SubSub;',
  26374. ' aSub:=@SubSub;',
  26375. ' aSubSub:=@SubSub;',
  26376. ' end;',
  26377. ' begin;',
  26378. ' end;',
  26379. 'begin;',
  26380. ' aProc:=@Sub;',
  26381. ' b:=aProc=@Sub;',
  26382. ' b:=@Sub=aProc;',
  26383. 'end;',
  26384. 'begin',
  26385. '']);
  26386. ConvertProgram;
  26387. CheckSource('TestProcType_Nested',
  26388. LinesToStr([ // statements
  26389. 'rtl.createClass(this, "TObject", null, function () {',
  26390. ' this.$init = function () {',
  26391. ' };',
  26392. ' this.$final = function () {',
  26393. ' };',
  26394. ' this.DoIt = function (vJ) {',
  26395. ' var $Self = this;',
  26396. ' var aProc = null;',
  26397. ' var b = false;',
  26398. ' function Sub(vK) {',
  26399. ' var aSub = null;',
  26400. ' function SubSub(vK) {',
  26401. ' var aSubSub = null;',
  26402. ' aProc = rtl.createCallback($Self, "DoIt");',
  26403. ' aSub = rtl.createCallback($Self, "DoIt");',
  26404. ' aSubSub = rtl.createCallback($Self, "DoIt");',
  26405. ' aProc = Sub;',
  26406. ' aSub = Sub;',
  26407. ' aSubSub = Sub;',
  26408. ' aProc = SubSub;',
  26409. ' aSub = SubSub;',
  26410. ' aSubSub = SubSub;',
  26411. ' };',
  26412. ' };',
  26413. ' aProc = Sub;',
  26414. ' b = rtl.eqCallback(aProc, Sub);',
  26415. ' b = rtl.eqCallback(Sub, aProc);',
  26416. ' };',
  26417. '});',
  26418. '']),
  26419. LinesToStr([ // $mod.$main
  26420. '']));
  26421. end;
  26422. procedure TTestModule.TestProcType_ReferenceToProc;
  26423. begin
  26424. StartProgram(false);
  26425. Add([
  26426. 'type',
  26427. ' TProcRef = reference to procedure(i: longint = 0);',
  26428. ' TFuncRef = reference to function(i: longint = 0): longint;',
  26429. 'var',
  26430. ' p: TProcRef;',
  26431. ' f: TFuncRef;',
  26432. 'procedure DoIt(i: longint);',
  26433. 'begin',
  26434. 'end;',
  26435. 'function GetIt(i: longint): longint;',
  26436. 'begin',
  26437. ' p:=@DoIt;',
  26438. ' f:=@GetIt;',
  26439. ' f;',
  26440. ' f();',
  26441. ' f(1);',
  26442. 'end;',
  26443. 'begin',
  26444. ' p:=@DoIt;',
  26445. ' f:=@GetIt;',
  26446. ' f;',
  26447. ' f();',
  26448. ' f(1);',
  26449. ' p:=TProcRef(f);',
  26450. '']);
  26451. ConvertProgram;
  26452. CheckSource('TestProcType_ReferenceToProc',
  26453. LinesToStr([ // statements
  26454. 'this.p = null;',
  26455. 'this.f = null;',
  26456. 'this.DoIt = function (i) {',
  26457. '};',
  26458. 'this.GetIt = function (i) {',
  26459. ' var Result = 0;',
  26460. ' $mod.p = $mod.DoIt;',
  26461. ' $mod.f = $mod.GetIt;',
  26462. ' $mod.f(0);',
  26463. ' $mod.f(0);',
  26464. ' $mod.f(1);',
  26465. ' return Result;',
  26466. '};',
  26467. '']),
  26468. LinesToStr([ // $mod.$main
  26469. '$mod.p = $mod.DoIt;',
  26470. '$mod.f = $mod.GetIt;',
  26471. '$mod.f(0);',
  26472. '$mod.f(0);',
  26473. '$mod.f(1);',
  26474. '$mod.p = $mod.f;',
  26475. '']));
  26476. end;
  26477. procedure TTestModule.TestProcType_ReferenceToMethod;
  26478. begin
  26479. StartProgram(false);
  26480. Add([
  26481. 'type',
  26482. ' TFuncRef = reference to function(i: longint = 5): longint;',
  26483. ' TObject = class',
  26484. ' function Grow(s: longint): longint;',
  26485. ' end;',
  26486. 'var',
  26487. ' f: tfuncref;',
  26488. 'function tobject.grow(s: longint): longint;',
  26489. ' function GrowSub(i: longint): longint;',
  26490. ' begin',
  26491. ' f:=@grow;',
  26492. ' f:=@growsub;',
  26493. ' end;',
  26494. 'begin',
  26495. ' f:=@grow;',
  26496. ' f:=@growsub;',
  26497. 'end;',
  26498. 'begin',
  26499. '']);
  26500. ConvertProgram;
  26501. CheckSource('TestProcType_ReferenceToMethod',
  26502. LinesToStr([ // statements
  26503. 'rtl.createClass(this, "TObject", null, function () {',
  26504. ' this.$init = function () {',
  26505. ' };',
  26506. ' this.$final = function () {',
  26507. ' };',
  26508. ' this.Grow = function (s) {',
  26509. ' var $Self = this;',
  26510. ' var Result = 0;',
  26511. ' function GrowSub(i) {',
  26512. ' var Result = 0;',
  26513. ' $mod.f = rtl.createCallback($Self, "Grow");',
  26514. ' $mod.f = GrowSub;',
  26515. ' return Result;',
  26516. ' };',
  26517. ' $mod.f = rtl.createCallback($Self, "Grow");',
  26518. ' $mod.f = GrowSub;',
  26519. ' return Result;',
  26520. ' };',
  26521. '});',
  26522. 'this.f = null;',
  26523. '']),
  26524. LinesToStr([ // $mod.$main
  26525. '']));
  26526. end;
  26527. procedure TTestModule.TestProcType_Typecast;
  26528. begin
  26529. StartProgram(false);
  26530. Add([
  26531. 'type',
  26532. ' TNotifyEvent = procedure(Sender: Pointer) of object;',
  26533. ' TEvent = procedure of object;',
  26534. ' TGetter = function:longint of object;',
  26535. ' TProcA = procedure(i: longint);',
  26536. ' TFuncB = function(i, j: longint): longint;',
  26537. 'procedure DoIt(); varargs; begin end;',
  26538. 'var',
  26539. ' Notify: tnotifyevent;',
  26540. ' Event: tevent;',
  26541. ' Getter: tgetter;',
  26542. ' ProcA: tproca;',
  26543. ' FuncB: tfuncb;',
  26544. ' p: pointer;',
  26545. 'begin',
  26546. ' notify:=tnotifyevent(event);',
  26547. ' event:=tevent(event);',
  26548. ' event:=tevent(notify);',
  26549. ' event:=tevent(getter);',
  26550. ' event:=tevent(proca);',
  26551. ' proca:=tproca(funcb);',
  26552. ' funcb:=tfuncb(funcb);',
  26553. ' funcb:=tfuncb(proca);',
  26554. ' funcb:=tfuncb(getter);',
  26555. ' proca:=tproca(p);',
  26556. ' funcb:=tfuncb(p);',
  26557. ' getter:=tgetter(p);',
  26558. ' p:=pointer(notify);',
  26559. ' p:=notify;',
  26560. ' p:=pointer(proca);',
  26561. ' p:=proca;',
  26562. ' p:=pointer(funcb);',
  26563. ' p:=funcb;',
  26564. ' doit(Pointer(notify),pointer(event),pointer(proca));',
  26565. '']);
  26566. ConvertProgram;
  26567. CheckSource('TestProcType_Typecast',
  26568. LinesToStr([ // statements
  26569. 'this.DoIt = function () {',
  26570. '};',
  26571. 'this.Notify = null;',
  26572. 'this.Event = null;',
  26573. 'this.Getter = null;',
  26574. 'this.ProcA = null;',
  26575. 'this.FuncB = null;',
  26576. 'this.p = null;',
  26577. '']),
  26578. LinesToStr([ // $mod.$main
  26579. '$mod.Notify = $mod.Event;',
  26580. '$mod.Event = $mod.Event;',
  26581. '$mod.Event = $mod.Notify;',
  26582. '$mod.Event = $mod.Getter;',
  26583. '$mod.Event = $mod.ProcA;',
  26584. '$mod.ProcA = $mod.FuncB;',
  26585. '$mod.FuncB = $mod.FuncB;',
  26586. '$mod.FuncB = $mod.ProcA;',
  26587. '$mod.FuncB = $mod.Getter;',
  26588. '$mod.ProcA = $mod.p;',
  26589. '$mod.FuncB = $mod.p;',
  26590. '$mod.Getter = $mod.p;',
  26591. '$mod.p = $mod.Notify;',
  26592. '$mod.p = $mod.Notify;',
  26593. '$mod.p = $mod.ProcA;',
  26594. '$mod.p = $mod.ProcA;',
  26595. '$mod.p = $mod.FuncB;',
  26596. '$mod.p = $mod.FuncB;',
  26597. '$mod.DoIt($mod.Notify, $mod.Event, $mod.ProcA);',
  26598. '']));
  26599. end;
  26600. procedure TTestModule.TestProcType_PassProcToUntyped;
  26601. begin
  26602. StartProgram(false);
  26603. Add([
  26604. 'type',
  26605. ' TEvent = procedure of object;',
  26606. ' TFunc = function: longint;',
  26607. 'procedure DoIt(); varargs; begin end;',
  26608. 'procedure DoSome(const a; var b; p: pointer); begin end;',
  26609. 'var',
  26610. ' Event: tevent;',
  26611. ' Func: TFunc;',
  26612. 'begin',
  26613. ' doit(event,func);',
  26614. ' dosome(event,event,event);',
  26615. ' dosome(func,func,func);',
  26616. '']);
  26617. ConvertProgram;
  26618. CheckSource('TestProcType_PassProcToUntyped',
  26619. LinesToStr([ // statements
  26620. 'this.DoIt = function () {',
  26621. '};',
  26622. 'this.DoSome = function (a, b, p) {',
  26623. '};',
  26624. 'this.Event = null;',
  26625. 'this.Func = null;',
  26626. '']),
  26627. LinesToStr([ // $mod.$main
  26628. '$mod.DoIt($mod.Event, $mod.Func);',
  26629. '$mod.DoSome($mod.Event, {',
  26630. ' p: $mod,',
  26631. ' get: function () {',
  26632. ' return this.p.Event;',
  26633. ' },',
  26634. ' set: function (v) {',
  26635. ' this.p.Event = v;',
  26636. ' }',
  26637. '}, $mod.Event);',
  26638. '$mod.DoSome($mod.Func, {',
  26639. ' p: $mod,',
  26640. ' get: function () {',
  26641. ' return this.p.Func;',
  26642. ' },',
  26643. ' set: function (v) {',
  26644. ' this.p.Func = v;',
  26645. ' }',
  26646. '}, $mod.Func);',
  26647. '']));
  26648. end;
  26649. procedure TTestModule.TestProcType_PassProcToArray;
  26650. begin
  26651. StartProgram(false);
  26652. Add([
  26653. 'type',
  26654. ' TFunc = function: longint;',
  26655. ' TArrFunc = array of TFunc;',
  26656. 'procedure DoIt(Arr: TArrFunc); begin end;',
  26657. 'function GetIt: longint; begin end;',
  26658. 'var',
  26659. ' Func: tfunc;',
  26660. 'begin',
  26661. ' doit([]);',
  26662. ' doit([@GetIt]);',
  26663. ' doit([Func]);',
  26664. '']);
  26665. ConvertProgram;
  26666. CheckSource('TestProcType_PassProcToArray',
  26667. LinesToStr([ // statements
  26668. 'this.DoIt = function (Arr) {',
  26669. '};',
  26670. 'this.GetIt = function () {',
  26671. ' var Result = 0;',
  26672. ' return Result;',
  26673. '};',
  26674. 'this.Func = null;',
  26675. '']),
  26676. LinesToStr([ // $mod.$main
  26677. '$mod.DoIt([]);',
  26678. '$mod.DoIt([$mod.GetIt]);',
  26679. '$mod.DoIt([$mod.Func]);',
  26680. '']));
  26681. end;
  26682. procedure TTestModule.TestProcType_SafeCallObjFPC;
  26683. begin
  26684. StartProgram(false);
  26685. Add([
  26686. '{$modeswitch externalclass}',
  26687. 'type',
  26688. ' TProc = reference to procedure(i: longint); safecall;',
  26689. ' TEvent = procedure(i: longint) of object; safecall;',
  26690. ' TExtA = class external name ''ExtObj''',
  26691. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  26692. ' procedure DoSome(Id: longint = 1);',
  26693. ' procedure SetOnClick(const e: TEvent);',
  26694. ' property OnClick: TEvent write SetOnClick;',
  26695. ' class procedure Fly(Id: longint = 1); static;',
  26696. ' procedure SetOnShow(const p: TProc);',
  26697. ' property OnShow: TProc write SetOnShow;',
  26698. ' end;',
  26699. 'procedure Run(i: longint = 1);',
  26700. 'begin',
  26701. 'end;',
  26702. 'var',
  26703. ' Obj: texta;',
  26704. ' e: TEvent;',
  26705. ' p: TProc;',
  26706. 'begin',
  26707. ' e:=e;',
  26708. ' e:[email protected];',
  26709. ' e:[email protected];',
  26710. ' e:=TEvent(@obj.dosome);', // no safecall
  26711. ' obj.OnClick:[email protected];',
  26712. ' obj.OnClick:[email protected];',
  26713. ' obj.setonclick(@obj.doit);',
  26714. ' obj.setonclick(@obj.dosome);',
  26715. ' p:=@Run;',
  26716. ' p:[email protected];',
  26717. ' obj.OnShow:=@Run;',
  26718. ' obj.OnShow:[email protected];',
  26719. ' obj.setOnShow(@Run);',
  26720. ' obj.setOnShow(@TExtA.Fly);',
  26721. ' with obj do begin',
  26722. ' e:=@doit;',
  26723. ' e:=@dosome;',
  26724. ' OnClick:=@doit;',
  26725. ' OnClick:=@dosome;',
  26726. ' setonclick(@doit);',
  26727. ' setonclick(@dosome);',
  26728. ' OnShow:=@Run;',
  26729. ' setOnShow(@Run);',
  26730. ' end;']);
  26731. ConvertProgram;
  26732. CheckSource('TestProcType_SafeCallObjFPC',
  26733. LinesToStr([ // statements
  26734. 'this.Run = function (i) {',
  26735. '};',
  26736. 'this.Obj = null;',
  26737. 'this.e = null;',
  26738. 'this.p = null;',
  26739. '']),
  26740. LinesToStr([ // $mod.$main
  26741. '$mod.e = $mod.e;',
  26742. '$mod.e = rtl.createSafeCallback($mod.Obj, "$Execute");',
  26743. '$mod.e = rtl.createSafeCallback($mod.Obj, "DoSome");',
  26744. '$mod.e = rtl.createCallback($mod.Obj, "DoSome");',
  26745. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  26746. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  26747. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  26748. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  26749. '$mod.p = rtl.createSafeCallback($mod, "Run");',
  26750. '$mod.p = rtl.createSafeCallback(ExtObj, "Fly");',
  26751. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26752. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  26753. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26754. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  26755. 'var $with = $mod.Obj;',
  26756. '$mod.e = rtl.createSafeCallback($with, "$Execute");',
  26757. '$mod.e = rtl.createSafeCallback($with, "DoSome");',
  26758. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  26759. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  26760. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  26761. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  26762. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26763. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26764. '']));
  26765. end;
  26766. procedure TTestModule.TestProcType_SafeCallDelphi;
  26767. begin
  26768. StartProgram(false);
  26769. Add([
  26770. '{$mode delphi}',
  26771. '{$modeswitch externalclass}',
  26772. 'type',
  26773. ' TProc = reference to procedure(i: longint); safecall;',
  26774. ' TEvent = procedure(i: longint) of object; safecall;',
  26775. ' TExtA = class external name ''ExtObj''',
  26776. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  26777. ' procedure DoSome(Id: longint = 1);',
  26778. ' procedure SetOnClick(const e: TEvent);',
  26779. ' property OnClick: TEvent write SetOnClick;',
  26780. ' class procedure Fly(Id: longint = 1); static;',
  26781. ' procedure SetOnShow(const p: TProc);',
  26782. ' property OnShow: TProc write SetOnShow;',
  26783. ' end;',
  26784. 'procedure Run(i: longint = 1);',
  26785. 'begin',
  26786. 'end;',
  26787. 'var',
  26788. ' Obj: texta;',
  26789. ' e: TEvent;',
  26790. ' p: TProc;',
  26791. 'begin',
  26792. ' e:=e;',
  26793. ' e:=obj.doit;',
  26794. ' e:=obj.dosome;',
  26795. ' e:=TEvent(@obj.dosome);', // no safecall
  26796. ' obj.OnClick:=obj.doit;',
  26797. ' obj.OnClick:=obj.dosome;',
  26798. ' obj.setonclick(obj.doit);',
  26799. ' obj.setonclick(obj.dosome);',
  26800. ' p:=Run;',
  26801. ' p:=TExtA.Fly;',
  26802. ' obj.OnShow:=Run;',
  26803. ' obj.OnShow:=TExtA.Fly;',
  26804. ' obj.setOnShow(Run);',
  26805. ' obj.setOnShow(TExtA.Fly);',
  26806. ' with obj do begin',
  26807. ' e:=doit;',
  26808. ' e:=dosome;',
  26809. ' OnClick:=doit;',
  26810. ' OnClick:=dosome;',
  26811. ' setonclick(doit);',
  26812. ' setonclick(dosome);',
  26813. ' OnShow:=@Run;',
  26814. ' setOnShow(@Run);',
  26815. ' end;']);
  26816. ConvertProgram;
  26817. CheckSource('TestProcType_SafeCallDelphi',
  26818. LinesToStr([ // statements
  26819. 'this.Run = function (i) {',
  26820. '};',
  26821. 'this.Obj = null;',
  26822. 'this.e = null;',
  26823. 'this.p = null;',
  26824. '']),
  26825. LinesToStr([ // $mod.$main
  26826. '$mod.e = $mod.e;',
  26827. '$mod.e = rtl.createSafeCallback($mod.Obj, "$Execute");',
  26828. '$mod.e = rtl.createSafeCallback($mod.Obj, "DoSome");',
  26829. '$mod.e = rtl.createCallback($mod.Obj, "DoSome");',
  26830. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  26831. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  26832. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  26833. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  26834. '$mod.p = rtl.createSafeCallback($mod, "Run");',
  26835. '$mod.p = rtl.createSafeCallback(ExtObj, "Fly");',
  26836. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26837. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  26838. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26839. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  26840. 'var $with = $mod.Obj;',
  26841. '$mod.e = rtl.createSafeCallback($with, "$Execute");',
  26842. '$mod.e = rtl.createSafeCallback($with, "DoSome");',
  26843. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  26844. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  26845. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  26846. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  26847. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26848. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26849. '']));
  26850. end;
  26851. procedure TTestModule.TestPointer;
  26852. begin
  26853. StartProgram(false);
  26854. Add(['type',
  26855. ' TObject = class end;',
  26856. ' TClass = class of TObject;',
  26857. ' TArrInt = array of longint;',
  26858. 'const',
  26859. ' n = nil;',
  26860. 'var',
  26861. ' v: jsvalue;',
  26862. ' Obj: tobject;',
  26863. ' C: tclass;',
  26864. ' a: tarrint;',
  26865. ' p: Pointer = nil;',
  26866. ' s: string;',
  26867. 'begin',
  26868. ' p:=p;',
  26869. ' p:=nil;',
  26870. ' if p=nil then;',
  26871. ' if nil=p then;',
  26872. ' if Assigned(p) then;',
  26873. ' p:=Pointer(v);',
  26874. ' p:=obj;',
  26875. ' p:=c;',
  26876. ' p:=a;',
  26877. ' p:=tobject;',
  26878. ' obj:=TObject(p);',
  26879. ' c:=TClass(p);',
  26880. ' a:=TArrInt(p);',
  26881. ' p:=n;',
  26882. ' p:=Pointer(a);',
  26883. ' p:=pointer(s);',
  26884. ' s:=string(p);',
  26885. '']);
  26886. ConvertProgram;
  26887. CheckSource('TestPointer',
  26888. LinesToStr([ // statements
  26889. 'rtl.createClass(this, "TObject", null, function () {',
  26890. ' this.$init = function () {',
  26891. ' };',
  26892. ' this.$final = function () {',
  26893. ' };',
  26894. '});',
  26895. 'this.n = null;',
  26896. 'this.v = undefined;',
  26897. 'this.Obj = null;',
  26898. 'this.C = null;',
  26899. 'this.a = [];',
  26900. 'this.p = null;',
  26901. 'this.s = "";',
  26902. '']),
  26903. LinesToStr([ // $mod.$main
  26904. '$mod.p = $mod.p;',
  26905. '$mod.p = null;',
  26906. 'if ($mod.p === null) ;',
  26907. 'if (null === $mod.p) ;',
  26908. 'if ($mod.p != null) ;',
  26909. '$mod.p = $mod.v;',
  26910. '$mod.p = $mod.Obj;',
  26911. '$mod.p = $mod.C;',
  26912. '$mod.p = $mod.a;',
  26913. '$mod.p = $mod.TObject;',
  26914. '$mod.Obj = $mod.p;',
  26915. '$mod.C = $mod.p;',
  26916. '$mod.a = $mod.p;',
  26917. '$mod.p = null;',
  26918. '$mod.p = $mod.a;',
  26919. '$mod.p = $mod.s;',
  26920. '$mod.s = $mod.p;',
  26921. '']));
  26922. end;
  26923. procedure TTestModule.TestPointer_Proc;
  26924. begin
  26925. StartProgram(false);
  26926. Add('type');
  26927. Add(' TObject = class');
  26928. Add(' procedure DoIt; virtual; abstract;');
  26929. Add(' end;');
  26930. Add('procedure DoSome; begin end;');
  26931. Add('var');
  26932. Add(' o: TObject;');
  26933. Add(' p: Pointer;');
  26934. Add('begin');
  26935. Add(' p:=@DoSome;');
  26936. Add(' p:[email protected];');
  26937. ConvertProgram;
  26938. CheckSource('TestPointer_Proc',
  26939. LinesToStr([ // statements
  26940. 'rtl.createClass(this, "TObject", null, function () {',
  26941. ' this.$init = function () {',
  26942. ' };',
  26943. ' this.$final = function () {',
  26944. ' };',
  26945. '});',
  26946. 'this.DoSome = function () {',
  26947. '};',
  26948. 'this.o = null;',
  26949. 'this.p = null;',
  26950. '']),
  26951. LinesToStr([ // $mod.$main
  26952. '$mod.p = $mod.DoSome;',
  26953. '$mod.p = rtl.createCallback($mod.o, "DoIt");',
  26954. '']));
  26955. end;
  26956. procedure TTestModule.TestPointer_AssignRecordFail;
  26957. begin
  26958. StartProgram(false);
  26959. Add('type');
  26960. Add(' TRec = record end;');
  26961. Add('var');
  26962. Add(' p: Pointer;');
  26963. Add(' r: TRec;');
  26964. Add('begin');
  26965. Add(' p:=r;');
  26966. SetExpectedPasResolverError('Incompatible types: got "TRec" expected "Pointer"',
  26967. nIncompatibleTypesGotExpected);
  26968. ConvertProgram;
  26969. end;
  26970. procedure TTestModule.TestPointer_AssignStaticArrayFail;
  26971. begin
  26972. StartProgram(false);
  26973. Add('type');
  26974. Add(' TArr = array[boolean] of longint;');
  26975. Add('var');
  26976. Add(' p: Pointer;');
  26977. Add(' a: TArr;');
  26978. Add('begin');
  26979. Add(' p:=a;');
  26980. SetExpectedPasResolverError('Incompatible types: got "TArr" expected "Pointer"',
  26981. nIncompatibleTypesGotExpected);
  26982. ConvertProgram;
  26983. end;
  26984. procedure TTestModule.TestPointer_TypeCastJSValueToPointer;
  26985. begin
  26986. StartProgram(false);
  26987. Add([
  26988. 'procedure DoIt(args: array of jsvalue); begin end;',
  26989. 'procedure DoAll; varargs; begin end;',
  26990. 'var',
  26991. ' v: jsvalue;',
  26992. 'begin',
  26993. ' DoIt([pointer(v)]);',
  26994. ' DoAll(pointer(v));',
  26995. '']);
  26996. ConvertProgram;
  26997. CheckSource('TestPointer_TypeCastJSValueToPointer',
  26998. LinesToStr([ // statements
  26999. 'this.DoIt = function (args) {',
  27000. '};',
  27001. 'this.DoAll = function () {',
  27002. '};',
  27003. 'this.v = undefined;',
  27004. '']),
  27005. LinesToStr([ // $mod.$main
  27006. '$mod.DoIt([$mod.v]);',
  27007. '$mod.DoAll($mod.v);',
  27008. '']));
  27009. end;
  27010. procedure TTestModule.TestPointer_NonRecordFail;
  27011. begin
  27012. StartProgram(false);
  27013. Add([
  27014. 'type',
  27015. ' p = ^longint;',
  27016. 'begin',
  27017. '']);
  27018. SetExpectedPasResolverError('Not supported: pointer of Longint',nNotSupportedX);
  27019. ConvertProgram;
  27020. end;
  27021. procedure TTestModule.TestPointer_AnonymousArgTypeFail;
  27022. begin
  27023. StartProgram(false);
  27024. Add([
  27025. 'procedure DoIt(p: ^longint); begin end;',
  27026. 'begin',
  27027. '']);
  27028. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  27029. ConvertProgram;
  27030. end;
  27031. procedure TTestModule.TestPointer_AnonymousVarTypeFail;
  27032. begin
  27033. StartProgram(false);
  27034. Add([
  27035. 'var p: ^longint;',
  27036. 'begin',
  27037. '']);
  27038. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  27039. ConvertProgram;
  27040. end;
  27041. procedure TTestModule.TestPointer_AnonymousResultTypeFail;
  27042. begin
  27043. StartProgram(false);
  27044. Add([
  27045. 'function DoIt: ^longint; begin end;',
  27046. 'begin',
  27047. '']);
  27048. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  27049. ConvertProgram;
  27050. end;
  27051. procedure TTestModule.TestPointer_AddrOperatorFail;
  27052. begin
  27053. StartProgram(false);
  27054. Add([
  27055. 'var i: longint;',
  27056. 'begin',
  27057. ' if @i=nil then ;',
  27058. '']);
  27059. SetExpectedConverterError('illegal qualifier "@" in front of "i:Longint"',nIllegalQualifierInFrontOf);
  27060. ConvertProgram;
  27061. end;
  27062. procedure TTestModule.TestPointer_ArrayParamsFail;
  27063. begin
  27064. StartProgram(false);
  27065. Add([
  27066. 'var',
  27067. ' p: Pointer;',
  27068. 'begin',
  27069. ' p:=p[1];',
  27070. '']);
  27071. SetExpectedPasResolverError('illegal qualifier "[" after "Pointer"',nIllegalQualifierAfter);
  27072. ConvertProgram;
  27073. end;
  27074. procedure TTestModule.TestPointer_PointerAddFail;
  27075. begin
  27076. StartProgram(false);
  27077. Add([
  27078. 'var',
  27079. ' p: Pointer;',
  27080. 'begin',
  27081. ' p:=p+1;',
  27082. '']);
  27083. SetExpectedPasResolverError('Operator is not overloaded: "Pointer" + "Longint"',nOperatorIsNotOverloadedAOpB);
  27084. ConvertProgram;
  27085. end;
  27086. procedure TTestModule.TestPointer_IncPointerFail;
  27087. begin
  27088. StartProgram(false);
  27089. Add([
  27090. 'var',
  27091. ' p: Pointer;',
  27092. 'begin',
  27093. ' inc(p,1);',
  27094. '']);
  27095. SetExpectedPasResolverError('Incompatible type arg no. 1: Got "Pointer", expected "integer"',
  27096. nIncompatibleTypeArgNo);
  27097. ConvertProgram;
  27098. end;
  27099. procedure TTestModule.TestPointer_Record;
  27100. begin
  27101. StartProgram(false);
  27102. Add([
  27103. 'type',
  27104. ' TRec = record x: longint; end;',
  27105. ' PRec = ^TRec;',
  27106. 'var',
  27107. ' r: TRec;',
  27108. ' p: PRec;',
  27109. ' q: ^TRec;',
  27110. ' Ptr: pointer;',
  27111. 'begin',
  27112. ' new(p);',
  27113. ' p:=@r;',
  27114. ' r:=p^;',
  27115. ' r.x:=p^.x;',
  27116. ' p^.x:=r.x;',
  27117. ' if p^.x=3 then ;',
  27118. ' if 4=p^.x then ;',
  27119. ' dispose(p);',
  27120. ' new(q);',
  27121. ' dispose(q);',
  27122. ' Ptr:=p;',
  27123. ' p:=PRec(ptr);',
  27124. '']);
  27125. ConvertProgram;
  27126. CheckSource('TestPointer_Record',
  27127. LinesToStr([ // statements
  27128. 'rtl.recNewT(this, "TRec", function () {',
  27129. ' this.x = 0;',
  27130. ' this.$eq = function (b) {',
  27131. ' return this.x === b.x;',
  27132. ' };',
  27133. ' this.$assign = function (s) {',
  27134. ' this.x = s.x;',
  27135. ' return this;',
  27136. ' };',
  27137. '});',
  27138. 'this.r = this.TRec.$new();',
  27139. 'this.p = null;',
  27140. 'this.q = null;',
  27141. 'this.Ptr = null;',
  27142. '']),
  27143. LinesToStr([ // $mod.$main
  27144. '$mod.p = $mod.TRec.$new();',
  27145. '$mod.p = $mod.r;',
  27146. '$mod.r.$assign($mod.p);',
  27147. '$mod.r.x = $mod.p.x;',
  27148. '$mod.p.x = $mod.r.x;',
  27149. 'if ($mod.p.x === 3) ;',
  27150. 'if (4 === $mod.p.x) ;',
  27151. '$mod.p = null;',
  27152. '$mod.q = $mod.TRec.$new();',
  27153. '$mod.q = null;',
  27154. '$mod.Ptr = $mod.p;',
  27155. '$mod.p = $mod.Ptr;',
  27156. '']));
  27157. end;
  27158. procedure TTestModule.TestPointer_RecordArg;
  27159. begin
  27160. StartProgram(false);
  27161. Add([
  27162. '{$modeswitch autoderef}',
  27163. 'type',
  27164. ' TRec = record x: longint; end;',
  27165. ' PRec = ^TRec;',
  27166. 'function DoIt(const a: PRec; var b: PRec; out c: PRec): TRec;',
  27167. 'begin',
  27168. ' a.x:=a.x;',
  27169. ' a^.x:=a^.x;',
  27170. ' with a^ do',
  27171. ' x:=x;',
  27172. 'end;',
  27173. 'function GetIt(p: PRec): PRec;',
  27174. 'begin',
  27175. ' p.x:=p.x;',
  27176. ' p^.x:=p^.x;',
  27177. ' with p^ do',
  27178. ' x:=x;',
  27179. 'end;',
  27180. 'var',
  27181. ' r: TRec;',
  27182. ' p: PRec;',
  27183. 'begin',
  27184. ' p:=GetIt(p);',
  27185. ' p^:=GetIt(@r)^;',
  27186. ' DoIt(p,p,p);',
  27187. ' DoIt(@r,p,p);',
  27188. '']);
  27189. ConvertProgram;
  27190. CheckSource('TestPointer_RecordArg',
  27191. LinesToStr([ // statements
  27192. 'rtl.recNewT(this, "TRec", function () {',
  27193. ' this.x = 0;',
  27194. ' this.$eq = function (b) {',
  27195. ' return this.x === b.x;',
  27196. ' };',
  27197. ' this.$assign = function (s) {',
  27198. ' this.x = s.x;',
  27199. ' return this;',
  27200. ' };',
  27201. '});',
  27202. 'this.DoIt = function (a, b, c) {',
  27203. ' var Result = $mod.TRec.$new();',
  27204. ' a.x = a.x;',
  27205. ' a.x = a.x;',
  27206. ' a.x = a.x;',
  27207. ' return Result;',
  27208. '};',
  27209. 'this.GetIt = function (p) {',
  27210. ' var Result = null;',
  27211. ' p.x = p.x;',
  27212. ' p.x = p.x;',
  27213. ' p.x = p.x;',
  27214. ' return Result;',
  27215. '};',
  27216. 'this.r = this.TRec.$new();',
  27217. 'this.p = null;',
  27218. '']),
  27219. LinesToStr([ // $mod.$main
  27220. '$mod.p = $mod.GetIt($mod.p);',
  27221. '$mod.p.$assign($mod.GetIt($mod.r));',
  27222. '$mod.DoIt($mod.p, {',
  27223. ' p: $mod,',
  27224. ' get: function () {',
  27225. ' return this.p.p;',
  27226. ' },',
  27227. ' set: function (v) {',
  27228. ' this.p.p = v;',
  27229. ' }',
  27230. '}, {',
  27231. ' p: $mod,',
  27232. ' get: function () {',
  27233. ' return this.p.p;',
  27234. ' },',
  27235. ' set: function (v) {',
  27236. ' this.p.p = v;',
  27237. ' }',
  27238. '});',
  27239. '$mod.DoIt($mod.r, {',
  27240. ' p: $mod,',
  27241. ' get: function () {',
  27242. ' return this.p.p;',
  27243. ' },',
  27244. ' set: function (v) {',
  27245. ' this.p.p = v;',
  27246. ' }',
  27247. '}, {',
  27248. ' p: $mod,',
  27249. ' get: function () {',
  27250. ' return this.p.p;',
  27251. ' },',
  27252. ' set: function (v) {',
  27253. ' this.p.p = v;',
  27254. ' }',
  27255. '});',
  27256. '']));
  27257. end;
  27258. procedure TTestModule.TestJSValue_AssignToJSValue;
  27259. begin
  27260. StartProgram(false);
  27261. Add('var');
  27262. Add(' v: jsvalue;');
  27263. Add(' i: longint;');
  27264. Add(' s: string;');
  27265. Add(' b: boolean;');
  27266. Add(' d: double;');
  27267. Add(' p: pointer;');
  27268. Add('begin');
  27269. Add(' v:=v;');
  27270. Add(' v:=1;');
  27271. Add(' v:=i;');
  27272. Add(' v:='''';');
  27273. Add(' v:=''c'';');
  27274. Add(' v:=''foo'';');
  27275. Add(' v:=s;');
  27276. Add(' v:=false;');
  27277. Add(' v:=true;');
  27278. Add(' v:=b;');
  27279. Add(' v:=0.1;');
  27280. Add(' v:=d;');
  27281. Add(' v:=nil;');
  27282. Add(' v:=p;');
  27283. ConvertProgram;
  27284. CheckSource('TestJSValue_AssignToJSValue',
  27285. LinesToStr([ // statements
  27286. 'this.v = undefined;',
  27287. 'this.i = 0;',
  27288. 'this.s = "";',
  27289. 'this.b = false;',
  27290. 'this.d = 0.0;',
  27291. 'this.p = null;',
  27292. '']),
  27293. LinesToStr([ // $mod.$main
  27294. '$mod.v = $mod.v;',
  27295. '$mod.v = 1;',
  27296. '$mod.v = $mod.i;',
  27297. '$mod.v = "";',
  27298. '$mod.v = "c";',
  27299. '$mod.v = "foo";',
  27300. '$mod.v = $mod.s;',
  27301. '$mod.v = false;',
  27302. '$mod.v = true;',
  27303. '$mod.v = $mod.b;',
  27304. '$mod.v = 0.1;',
  27305. '$mod.v = $mod.d;',
  27306. '$mod.v = null;',
  27307. '$mod.v = $mod.p;',
  27308. '']));
  27309. end;
  27310. procedure TTestModule.TestJSValue_TypeCastToBaseType;
  27311. begin
  27312. StartProgram(false);
  27313. Add('type');
  27314. Add(' integer = longint;');
  27315. Add(' TYesNo = boolean;');
  27316. Add(' TFloat = double;');
  27317. Add(' TCaption = string;');
  27318. Add(' TChar = char;');
  27319. Add('var');
  27320. Add(' v: jsvalue;');
  27321. Add(' i: integer;');
  27322. Add(' s: TCaption;');
  27323. Add(' b: TYesNo;');
  27324. Add(' d: TFloat;');
  27325. Add(' c: char;');
  27326. Add('begin');
  27327. Add(' i:=longint(v);');
  27328. Add(' i:=integer(v);');
  27329. Add(' s:=string(v);');
  27330. Add(' s:=TCaption(v);');
  27331. Add(' b:=boolean(v);');
  27332. Add(' b:=TYesNo(v);');
  27333. Add(' d:=double(v);');
  27334. Add(' d:=TFloat(v);');
  27335. Add(' c:=char(v);');
  27336. Add(' c:=TChar(v);');
  27337. ConvertProgram;
  27338. CheckSource('TestJSValue_TypeCastToBaseType',
  27339. LinesToStr([ // statements
  27340. 'this.v = undefined;',
  27341. 'this.i = 0;',
  27342. 'this.s = "";',
  27343. 'this.b = false;',
  27344. 'this.d = 0.0;',
  27345. 'this.c = "";',
  27346. '']),
  27347. LinesToStr([ // $mod.$main
  27348. '$mod.i = rtl.trunc($mod.v);',
  27349. '$mod.i = rtl.trunc($mod.v);',
  27350. '$mod.s = "" + $mod.v;',
  27351. '$mod.s = "" + $mod.v;',
  27352. '$mod.b = !($mod.v == false);',
  27353. '$mod.b = !($mod.v == false);',
  27354. '$mod.d = rtl.getNumber($mod.v);',
  27355. '$mod.d = rtl.getNumber($mod.v);',
  27356. '$mod.c = rtl.getChar($mod.v);',
  27357. '$mod.c = rtl.getChar($mod.v);',
  27358. '']));
  27359. end;
  27360. procedure TTestModule.TestJSValue_TypecastToJSValue;
  27361. begin
  27362. StartProgram(false);
  27363. Add([
  27364. 'type',
  27365. ' TArr = array of word;',
  27366. ' TRec = record end;',
  27367. ' TSet = set of boolean;',
  27368. 'procedure Fly(v: jsvalue);',
  27369. 'begin',
  27370. 'end;',
  27371. 'var',
  27372. ' a: TArr;',
  27373. ' r: TRec;',
  27374. ' s: TSet;',
  27375. 'begin',
  27376. ' Fly(jsvalue(a));',
  27377. ' Fly(jsvalue(r));',
  27378. ' Fly(jsvalue(s));',
  27379. '']);
  27380. ConvertProgram;
  27381. CheckSource('TestJSValue_TypecastToJSValue',
  27382. LinesToStr([ // statements
  27383. 'rtl.recNewT(this, "TRec", function () {',
  27384. ' this.$eq = function (b) {',
  27385. ' return true;',
  27386. ' };',
  27387. ' this.$assign = function (s) {',
  27388. ' return this;',
  27389. ' };',
  27390. '});',
  27391. 'this.Fly = function (v) {',
  27392. '};',
  27393. 'this.a = [];',
  27394. 'this.r = this.TRec.$new();',
  27395. 'this.s = {};',
  27396. '']),
  27397. LinesToStr([ // $mod.$main
  27398. '$mod.Fly($mod.a);',
  27399. '$mod.Fly($mod.r);',
  27400. '$mod.Fly($mod.s);',
  27401. '']));
  27402. end;
  27403. procedure TTestModule.TestJSValue_Equal;
  27404. begin
  27405. StartProgram(false);
  27406. Add('type');
  27407. Add(' integer = longint;');
  27408. Add(' TYesNo = boolean;');
  27409. Add(' TFloat = double;');
  27410. Add(' TCaption = string;');
  27411. Add(' TChar = char;');
  27412. Add(' TMulti = JSValue;');
  27413. Add('var');
  27414. Add(' v: jsvalue;');
  27415. Add(' i: integer;');
  27416. Add(' s: TCaption;');
  27417. Add(' b: TYesNo;');
  27418. Add(' d: TFloat;');
  27419. Add(' c: char;');
  27420. Add(' m: TMulti;');
  27421. Add('begin');
  27422. Add(' b:=v=v;');
  27423. Add(' b:=v<>v;');
  27424. Add(' b:=v=1;');
  27425. Add(' b:=v<>1;');
  27426. Add(' b:=2=v;');
  27427. Add(' b:=2<>v;');
  27428. Add(' b:=v=i;');
  27429. Add(' b:=i=v;');
  27430. Add(' b:=v=nil;');
  27431. Add(' b:=nil=v;');
  27432. Add(' b:=v=false;');
  27433. Add(' b:=true=v;');
  27434. Add(' b:=v=b;');
  27435. Add(' b:=b=v;');
  27436. Add(' b:=v=s;');
  27437. Add(' b:=s=v;');
  27438. Add(' b:=v=''foo'';');
  27439. Add(' b:=''''=v;');
  27440. Add(' b:=v=d;');
  27441. Add(' b:=d=v;');
  27442. Add(' b:=v=3.4;');
  27443. Add(' b:=5.6=v;');
  27444. Add(' b:=v=c;');
  27445. Add(' b:=c=v;');
  27446. Add(' b:=m=m;');
  27447. Add(' b:=v=m;');
  27448. Add(' b:=m=v;');
  27449. ConvertProgram;
  27450. CheckSource('TestJSValue_Equal',
  27451. LinesToStr([ // statements
  27452. 'this.v = undefined;',
  27453. 'this.i = 0;',
  27454. 'this.s = "";',
  27455. 'this.b = false;',
  27456. 'this.d = 0.0;',
  27457. 'this.c = "";',
  27458. 'this.m = undefined;',
  27459. '']),
  27460. LinesToStr([ // $mod.$main
  27461. '$mod.b = $mod.v == $mod.v;',
  27462. '$mod.b = $mod.v != $mod.v;',
  27463. '$mod.b = $mod.v == 1;',
  27464. '$mod.b = $mod.v != 1;',
  27465. '$mod.b = 2 == $mod.v;',
  27466. '$mod.b = 2 != $mod.v;',
  27467. '$mod.b = $mod.v == $mod.i;',
  27468. '$mod.b = $mod.i == $mod.v;',
  27469. '$mod.b = $mod.v == null;',
  27470. '$mod.b = null == $mod.v;',
  27471. '$mod.b = $mod.v == false;',
  27472. '$mod.b = true == $mod.v;',
  27473. '$mod.b = $mod.v == $mod.b;',
  27474. '$mod.b = $mod.b == $mod.v;',
  27475. '$mod.b = $mod.v == $mod.s;',
  27476. '$mod.b = $mod.s == $mod.v;',
  27477. '$mod.b = $mod.v == "foo";',
  27478. '$mod.b = "" == $mod.v;',
  27479. '$mod.b = $mod.v == $mod.d;',
  27480. '$mod.b = $mod.d == $mod.v;',
  27481. '$mod.b = $mod.v == 3.4;',
  27482. '$mod.b = 5.6 == $mod.v;',
  27483. '$mod.b = $mod.v == $mod.c;',
  27484. '$mod.b = $mod.c == $mod.v;',
  27485. '$mod.b = $mod.m == $mod.m;',
  27486. '$mod.b = $mod.v == $mod.m;',
  27487. '$mod.b = $mod.m == $mod.v;',
  27488. '']));
  27489. end;
  27490. procedure TTestModule.TestJSValue_If;
  27491. begin
  27492. StartProgram(false);
  27493. Add([
  27494. 'procedure Fly(var u);',
  27495. 'begin',
  27496. ' if jsvalue(u) then ;',
  27497. 'end;',
  27498. 'var',
  27499. ' v: jsvalue;',
  27500. 'begin',
  27501. ' if v then ;',
  27502. ' while v do ;',
  27503. ' repeat until v;',
  27504. '']);
  27505. ConvertProgram;
  27506. CheckSource('TestJSValue_If',
  27507. LinesToStr([ // statements
  27508. 'this.Fly = function (u) {',
  27509. ' if (u.get()) ;',
  27510. '};',
  27511. 'this.v = undefined;',
  27512. '']),
  27513. LinesToStr([ // $mod.$main
  27514. 'if ($mod.v) ;',
  27515. 'while($mod.v){',
  27516. '};',
  27517. 'do{',
  27518. '} while(!$mod.v);',
  27519. '']));
  27520. end;
  27521. procedure TTestModule.TestJSValue_Not;
  27522. begin
  27523. StartProgram(false);
  27524. Add([
  27525. 'var',
  27526. ' v: jsvalue;',
  27527. ' b: boolean;',
  27528. 'begin',
  27529. ' b:=not v;',
  27530. ' if not v then ;',
  27531. ' while not v do ;',
  27532. ' repeat until not v;',
  27533. '']);
  27534. ConvertProgram;
  27535. CheckSource('TestJSValue_If',
  27536. LinesToStr([ // statements
  27537. 'this.v = undefined;',
  27538. 'this.b = false;',
  27539. '']),
  27540. LinesToStr([ // $mod.$main
  27541. '$mod.b=!$mod.v;',
  27542. 'if (!$mod.v) ;',
  27543. 'while(!$mod.v){',
  27544. '};',
  27545. 'do{',
  27546. '} while($mod.v);',
  27547. '']));
  27548. end;
  27549. procedure TTestModule.TestJSValue_Enum;
  27550. begin
  27551. StartProgram(false);
  27552. Add('type');
  27553. Add(' TColor = (red, blue);');
  27554. Add(' TRedBlue = TColor;');
  27555. Add('var');
  27556. Add(' v: jsvalue;');
  27557. Add(' e: TColor;');
  27558. Add('begin');
  27559. Add(' v:=e;');
  27560. Add(' v:=TColor(e);');
  27561. Add(' v:=TRedBlue(e);');
  27562. Add(' e:=TColor(v);');
  27563. Add(' e:=TRedBlue(v);');
  27564. ConvertProgram;
  27565. CheckSource('TestJSValue_Enum',
  27566. LinesToStr([ // statements
  27567. 'this.TColor = {',
  27568. ' "0": "red",',
  27569. ' red: 0,',
  27570. ' "1": "blue",',
  27571. ' blue: 1',
  27572. '};',
  27573. 'this.v = undefined;',
  27574. 'this.e = 0;',
  27575. '']),
  27576. LinesToStr([ // $mod.$main
  27577. '$mod.v = $mod.e;',
  27578. '$mod.v = $mod.e;',
  27579. '$mod.v = $mod.e;',
  27580. '$mod.e = $mod.v;',
  27581. '$mod.e = $mod.v;',
  27582. '']));
  27583. end;
  27584. procedure TTestModule.TestJSValue_ClassInstance;
  27585. begin
  27586. StartProgram(false);
  27587. Add([
  27588. 'type',
  27589. ' TObject = class',
  27590. ' end;',
  27591. ' TBirdObject = TObject;',
  27592. 'var',
  27593. ' v: jsvalue;',
  27594. ' o: TObject;',
  27595. 'begin',
  27596. ' v:=o;',
  27597. ' v:=TObject(o);',
  27598. ' v:=TBirdObject(o);',
  27599. ' o:=TObject(v);',
  27600. ' o:=TBirdObject(v);',
  27601. ' if v is TObject then ;',
  27602. '']);
  27603. ConvertProgram;
  27604. CheckSource('TestJSValue_ClassInstance',
  27605. LinesToStr([ // statements
  27606. 'rtl.createClass(this, "TObject", null, function () {',
  27607. ' this.$init = function () {',
  27608. ' };',
  27609. ' this.$final = function () {',
  27610. ' };',
  27611. '});',
  27612. 'this.v = undefined;',
  27613. 'this.o = null;',
  27614. '']),
  27615. LinesToStr([ // $mod.$main
  27616. '$mod.v = $mod.o;',
  27617. '$mod.v = $mod.o;',
  27618. '$mod.v = $mod.o;',
  27619. '$mod.o = rtl.getObject($mod.v);',
  27620. '$mod.o = rtl.getObject($mod.v);',
  27621. 'if (rtl.isExt($mod.v, $mod.TObject, 1)) ;',
  27622. '']));
  27623. end;
  27624. procedure TTestModule.TestJSValue_ClassOf;
  27625. begin
  27626. StartProgram(false);
  27627. Add([
  27628. 'type',
  27629. ' TClass = class of TObject;',
  27630. ' TObject = class',
  27631. ' end;',
  27632. ' TBirds = class of TBird;',
  27633. ' TBird = class(TObject) end;',
  27634. 'var',
  27635. ' v: jsvalue;',
  27636. ' c: TClass;',
  27637. 'begin',
  27638. ' v:=c;',
  27639. ' v:=TObject;',
  27640. ' v:=TClass(c);',
  27641. ' v:=TBirds(c);',
  27642. ' c:=TClass(v);',
  27643. ' c:=TBirds(v);',
  27644. ' if v is TClass then ;',
  27645. '']);
  27646. ConvertProgram;
  27647. CheckSource('TestJSValue_ClassOf',
  27648. LinesToStr([ // statements
  27649. 'rtl.createClass(this, "TObject", null, function () {',
  27650. ' this.$init = function () {',
  27651. ' };',
  27652. ' this.$final = function () {',
  27653. ' };',
  27654. '});',
  27655. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  27656. '});',
  27657. 'this.v = undefined;',
  27658. 'this.c = null;',
  27659. '']),
  27660. LinesToStr([ // $mod.$main
  27661. '$mod.v = $mod.c;',
  27662. '$mod.v = $mod.TObject;',
  27663. '$mod.v = $mod.c;',
  27664. '$mod.v = $mod.c;',
  27665. '$mod.c = rtl.getObject($mod.v);',
  27666. '$mod.c = rtl.getObject($mod.v);',
  27667. 'if (rtl.isExt($mod.v, $mod.TObject, 2)) ;',
  27668. '']));
  27669. end;
  27670. procedure TTestModule.TestJSValue_ArrayOfJSValue;
  27671. begin
  27672. StartProgram(false);
  27673. Add([
  27674. 'type',
  27675. ' integer = longint;',
  27676. ' TArray = array of JSValue;',
  27677. ' TArrgh = tarray;',
  27678. ' TArrInt = array of integer;',
  27679. 'var',
  27680. ' v: jsvalue;',
  27681. ' TheArray: tarray = (1,''2'');',
  27682. ' Arr: tarrgh;',
  27683. ' i: integer;',
  27684. ' ArrInt: tarrint;',
  27685. 'begin',
  27686. ' arr:=thearray;',
  27687. ' thearray:=arr;',
  27688. ' setlength(arr,2);',
  27689. ' setlength(thearray,3);',
  27690. ' arr[4]:=v;',
  27691. ' arr[5]:=length(thearray);',
  27692. ' arr[6]:=nil;',
  27693. ' arr[7]:=thearray[8];',
  27694. ' arr[low(arr)]:=high(thearray);',
  27695. ' arr:=arrint;',
  27696. ' arrInt:=tarrint(arr);',
  27697. ' if TheArray = nil then ;',
  27698. ' if nil = TheArray then ;',
  27699. ' if TheArray <> nil then ;',
  27700. ' if nil <> TheArray then ;',
  27701. '']);
  27702. ConvertProgram;
  27703. CheckSource('TestJSValue_ArrayOfJSValue',
  27704. LinesToStr([ // statements
  27705. 'this.v = undefined;',
  27706. 'this.TheArray = [1, "2"];',
  27707. 'this.Arr = [];',
  27708. 'this.i = 0;',
  27709. 'this.ArrInt = [];',
  27710. '']),
  27711. LinesToStr([ // $mod.$main
  27712. '$mod.Arr = rtl.arrayRef($mod.TheArray);',
  27713. '$mod.TheArray = rtl.arrayRef($mod.Arr);',
  27714. '$mod.Arr = rtl.arraySetLength($mod.Arr,undefined,2);',
  27715. '$mod.TheArray = rtl.arraySetLength($mod.TheArray,undefined,3);',
  27716. '$mod.Arr[4] = $mod.v;',
  27717. '$mod.Arr[5] = rtl.length($mod.TheArray);',
  27718. '$mod.Arr[6] = null;',
  27719. '$mod.Arr[7] = $mod.TheArray[8];',
  27720. '$mod.Arr[0] = rtl.length($mod.TheArray) - 1;',
  27721. '$mod.Arr = rtl.arrayRef($mod.ArrInt);',
  27722. '$mod.ArrInt = $mod.Arr;',
  27723. 'if (rtl.length($mod.TheArray) === 0) ;',
  27724. 'if (rtl.length($mod.TheArray) === 0) ;',
  27725. 'if (rtl.length($mod.TheArray) > 0) ;',
  27726. 'if (rtl.length($mod.TheArray) > 0) ;',
  27727. '']));
  27728. end;
  27729. procedure TTestModule.TestJSValue_ArrayLit;
  27730. begin
  27731. StartProgram(false);
  27732. Add([
  27733. 'type',
  27734. ' TFlag = (big,small);',
  27735. ' TArray = array of JSValue;',
  27736. ' TObject = class end;',
  27737. ' TClass = class of TObject;',
  27738. 'var',
  27739. ' v: jsvalue;',
  27740. ' a: TArray;',
  27741. ' o: TObject;',
  27742. 'begin',
  27743. ' a:=[];',
  27744. ' a:=[1];',
  27745. ' a:=[1,2];',
  27746. ' a:=[big];',
  27747. ' a:=[1,big];',
  27748. ' a:=[o,nil];',
  27749. '']);
  27750. ConvertProgram;
  27751. CheckSource('TestJSValue_ArrayLit',
  27752. LinesToStr([ // statements
  27753. 'this.TFlag = {',
  27754. ' "0": "big",',
  27755. ' big: 0,',
  27756. ' "1": "small",',
  27757. ' small: 1',
  27758. '};',
  27759. 'rtl.createClass(this, "TObject", null, function () {',
  27760. ' this.$init = function () {',
  27761. ' };',
  27762. ' this.$final = function () {',
  27763. ' };',
  27764. '});',
  27765. 'this.v = undefined;',
  27766. 'this.a = [];',
  27767. 'this.o = null;',
  27768. '']),
  27769. LinesToStr([ // $mod.$main
  27770. '$mod.a = [];',
  27771. '$mod.a = [1];',
  27772. '$mod.a = [1, 2];',
  27773. '$mod.a = [$mod.TFlag.big];',
  27774. '$mod.a = [1, $mod.TFlag.big];',
  27775. '$mod.a = [$mod.o, null];',
  27776. '']));
  27777. end;
  27778. procedure TTestModule.TestJSValue_Params;
  27779. begin
  27780. StartProgram(false);
  27781. Add('type');
  27782. Add(' integer = longint;');
  27783. Add(' TYesNo = boolean;');
  27784. Add(' TFloat = double;');
  27785. Add(' TCaption = string;');
  27786. Add(' TChar = char;');
  27787. Add('function DoIt(a: jsvalue; const b: jsvalue; var c: jsvalue; out d: jsvalue): jsvalue;');
  27788. Add('var');
  27789. Add(' l: jsvalue;');
  27790. Add('begin');
  27791. Add(' a:=a;');
  27792. Add(' l:=b;');
  27793. Add(' c:=c;');
  27794. Add(' d:=d;');
  27795. Add(' Result:=l;');
  27796. Add('end;');
  27797. Add('function DoSome(a: jsvalue; const b: jsvalue): jsvalue; begin end;');
  27798. Add('var');
  27799. Add(' v: jsvalue;');
  27800. Add(' i: integer;');
  27801. Add(' b: TYesNo;');
  27802. Add(' d: TFloat;');
  27803. Add(' s: TCaption;');
  27804. Add(' c: TChar;');
  27805. Add('begin');
  27806. Add(' v:=doit(v,v,v,v);');
  27807. Add(' i:=integer(dosome(i,i));');
  27808. Add(' b:=TYesNo(dosome(b,b));');
  27809. Add(' d:=TFloat(dosome(d,d));');
  27810. Add(' s:=TCaption(dosome(s,s));');
  27811. Add(' c:=TChar(dosome(c,c));');
  27812. ConvertProgram;
  27813. CheckSource('TestJSValue_Params',
  27814. LinesToStr([ // statements
  27815. 'this.DoIt = function (a, b, c, d) {',
  27816. ' var Result = undefined;',
  27817. ' var l = undefined;',
  27818. ' a = a;',
  27819. ' l = b;',
  27820. ' c.set(c.get());',
  27821. ' d.set(d.get());',
  27822. ' Result = l;',
  27823. ' return Result;',
  27824. '};',
  27825. 'this.DoSome = function (a, b) {',
  27826. ' var Result = undefined;',
  27827. ' return Result;',
  27828. '};',
  27829. 'this.v = undefined;',
  27830. 'this.i = 0;',
  27831. 'this.b = false;',
  27832. 'this.d = 0.0;',
  27833. 'this.s = "";',
  27834. 'this.c = "";',
  27835. '']),
  27836. LinesToStr([ // $mod.$main
  27837. '$mod.v = $mod.DoIt($mod.v, $mod.v, {',
  27838. ' p: $mod,',
  27839. ' get: function () {',
  27840. ' return this.p.v;',
  27841. ' },',
  27842. ' set: function (v) {',
  27843. ' this.p.v = v;',
  27844. ' }',
  27845. '}, {',
  27846. ' p: $mod,',
  27847. ' get: function () {',
  27848. ' return this.p.v;',
  27849. ' },',
  27850. ' set: function (v) {',
  27851. ' this.p.v = v;',
  27852. ' }',
  27853. '});',
  27854. '$mod.i = rtl.trunc($mod.DoSome($mod.i, $mod.i));',
  27855. '$mod.b = !($mod.DoSome($mod.b, $mod.b) == false);',
  27856. '$mod.d = rtl.getNumber($mod.DoSome($mod.d, $mod.d));',
  27857. '$mod.s = "" + $mod.DoSome($mod.s, $mod.s);',
  27858. '$mod.c = rtl.getChar($mod.DoSome($mod.c, $mod.c));',
  27859. '']));
  27860. end;
  27861. procedure TTestModule.TestJSValue_UntypedParam;
  27862. begin
  27863. StartProgram(false);
  27864. Add('function DoIt(const a; var b; out c): jsvalue;');
  27865. Add('begin');
  27866. Add(' Result:=a;');
  27867. Add(' Result:=b;');
  27868. Add(' Result:=c;');
  27869. Add(' b:=Result;');
  27870. Add(' c:=Result;');
  27871. Add('end;');
  27872. Add('var i: longint;');
  27873. Add('begin');
  27874. Add(' doit(i,i,i);');
  27875. ConvertProgram;
  27876. CheckSource('TestJSValue_UntypedParam',
  27877. LinesToStr([ // statements
  27878. 'this.DoIt = function (a, b, c) {',
  27879. ' var Result = undefined;',
  27880. ' Result = a;',
  27881. ' Result = b.get();',
  27882. ' Result = c.get();',
  27883. ' b.set(Result);',
  27884. ' c.set(Result);',
  27885. ' return Result;',
  27886. '};',
  27887. 'this.i = 0;',
  27888. '']),
  27889. LinesToStr([ // $mod.$main
  27890. '$mod.DoIt($mod.i, {',
  27891. ' p: $mod,',
  27892. ' get: function () {',
  27893. ' return this.p.i;',
  27894. ' },',
  27895. ' set: function (v) {',
  27896. ' this.p.i = v;',
  27897. ' }',
  27898. '}, {',
  27899. ' p: $mod,',
  27900. ' get: function () {',
  27901. ' return this.p.i;',
  27902. ' },',
  27903. ' set: function (v) {',
  27904. ' this.p.i = v;',
  27905. ' }',
  27906. '});',
  27907. '']));
  27908. end;
  27909. procedure TTestModule.TestJSValue_FuncResultType;
  27910. begin
  27911. StartProgram(false);
  27912. Add('type');
  27913. Add(' integer = longint;');
  27914. Add(' TJSValueArray = array of JSValue;');
  27915. Add(' TListSortCompare = function(Item1, Item2: JSValue): Integer;');
  27916. Add('procedure Sort(P: JSValue; aList: TJSValueArray; const Compare: TListSortCompare);');
  27917. Add('begin');
  27918. Add(' while Compare(P,aList[0])>0 do ;');
  27919. Add('end;');
  27920. Add('var');
  27921. Add(' Compare: TListSortCompare;');
  27922. Add(' V: JSValue;');
  27923. Add(' i: integer;');
  27924. Add('begin');
  27925. Add(' if Compare(V,V)>0 then ;');
  27926. Add(' if Compare(i,i)>1 then ;');
  27927. Add(' if Compare(nil,false)>2 then ;');
  27928. Add(' if Compare(1,true)>3 then ;');
  27929. ConvertProgram;
  27930. CheckSource('TestJSValue_UntypedParam',
  27931. LinesToStr([ // statements
  27932. 'this.Sort = function (P, aList, Compare) {',
  27933. ' while (Compare(P, aList[0]) > 0) {',
  27934. ' };',
  27935. '};',
  27936. 'this.Compare = null;',
  27937. 'this.V = undefined;',
  27938. 'this.i = 0;',
  27939. '']),
  27940. LinesToStr([ // $mod.$main
  27941. 'if ($mod.Compare($mod.V, $mod.V) > 0) ;',
  27942. 'if ($mod.Compare($mod.i, $mod.i) > 1) ;',
  27943. 'if ($mod.Compare(null, false) > 2) ;',
  27944. 'if ($mod.Compare(1, true) > 3) ;',
  27945. '']));
  27946. end;
  27947. procedure TTestModule.TestJSValue_ProcType_Assign;
  27948. begin
  27949. StartProgram(false);
  27950. Add('type');
  27951. Add(' integer = longint;');
  27952. Add(' TObject = class');
  27953. Add(' class function GetGlob: integer;');
  27954. Add(' function Getter: integer;');
  27955. Add(' end;');
  27956. Add('class function TObject.GetGlob: integer;');
  27957. Add('var v1: jsvalue;');
  27958. Add('begin');
  27959. Add(' v1:=@GetGlob;');
  27960. Add(' v1:[email protected];');
  27961. Add('end;');
  27962. Add('function TObject.Getter: integer;');
  27963. Add('var v2: jsvalue;');
  27964. Add('begin');
  27965. Add(' v2:=@Getter;');
  27966. Add(' v2:[email protected];');
  27967. Add(' v2:=@GetGlob;');
  27968. Add(' v2:[email protected];');
  27969. Add('end;');
  27970. Add('function GetIt(i: integer): integer;');
  27971. Add('var v3: jsvalue;');
  27972. Add('begin');
  27973. Add(' v3:=@GetIt;');
  27974. Add('end;');
  27975. Add('var');
  27976. Add(' V: JSValue;');
  27977. Add(' o: TObject;');
  27978. Add('begin');
  27979. Add(' v:=@GetIt;');
  27980. Add(' v:[email protected];');
  27981. Add(' v:[email protected];');
  27982. ConvertProgram;
  27983. CheckSource('TestJSValue_ProcType_Assign',
  27984. LinesToStr([ // statements
  27985. 'rtl.createClass(this, "TObject", null, function () {',
  27986. ' this.$init = function () {',
  27987. ' };',
  27988. ' this.$final = function () {',
  27989. ' };',
  27990. ' this.GetGlob = function () {',
  27991. ' var Result = 0;',
  27992. ' var v1 = undefined;',
  27993. ' v1 = rtl.createCallback(this, "GetGlob");',
  27994. ' v1 = rtl.createCallback(this, "GetGlob");',
  27995. ' return Result;',
  27996. ' };',
  27997. ' this.Getter = function () {',
  27998. ' var Result = 0;',
  27999. ' var v2 = undefined;',
  28000. ' v2 = rtl.createCallback(this, "Getter");',
  28001. ' v2 = rtl.createCallback(this, "Getter");',
  28002. ' v2 = rtl.createCallback(this.$class, "GetGlob");',
  28003. ' v2 = rtl.createCallback(this.$class, "GetGlob");',
  28004. ' return Result;',
  28005. ' };',
  28006. '});',
  28007. 'this.GetIt = function (i) {',
  28008. ' var Result = 0;',
  28009. ' var v3 = undefined;',
  28010. ' v3 = $mod.GetIt;',
  28011. ' return Result;',
  28012. '};',
  28013. 'this.V = undefined;',
  28014. 'this.o = null;',
  28015. '']),
  28016. LinesToStr([ // $mod.$main
  28017. '$mod.V = $mod.GetIt;',
  28018. '$mod.V = rtl.createCallback($mod.o, "Getter");',
  28019. '$mod.V = rtl.createCallback($mod.o.$class, "GetGlob");',
  28020. '']));
  28021. end;
  28022. procedure TTestModule.TestJSValue_ProcType_Equal;
  28023. begin
  28024. StartProgram(false);
  28025. Add('type');
  28026. Add(' integer = longint;');
  28027. Add(' TObject = class');
  28028. Add(' class function GetGlob: integer;');
  28029. Add(' function Getter: integer;');
  28030. Add(' end;');
  28031. Add('class function TObject.GetGlob: integer;');
  28032. Add('var v1: jsvalue;');
  28033. Add('begin');
  28034. Add(' if v1=@GetGlob then;');
  28035. Add(' if [email protected] then ;');
  28036. Add('end;');
  28037. Add('function TObject.Getter: integer;');
  28038. Add('var v2: jsvalue;');
  28039. Add('begin');
  28040. Add(' if v2=@Getter then;');
  28041. Add(' if [email protected] then ;');
  28042. Add(' if v2=@GetGlob then;');
  28043. Add(' if [email protected] then;');
  28044. Add('end;');
  28045. Add('function GetIt(i: integer): integer;');
  28046. Add('var v3: jsvalue;');
  28047. Add('begin');
  28048. Add(' if v3=@GetIt then;');
  28049. Add('end;');
  28050. Add('var');
  28051. Add(' V: JSValue;');
  28052. Add(' o: TObject;');
  28053. Add('begin');
  28054. Add(' if v=@GetIt then;');
  28055. Add(' if [email protected] then;');
  28056. Add(' if [email protected] then;');
  28057. Add(' if @GetIt=v then;');
  28058. Add(' if @o.Getter=v then;');
  28059. Add(' if @o.GetGlob=v then;');
  28060. ConvertProgram;
  28061. CheckSource('TestJSValue_ProcType_Equal',
  28062. LinesToStr([ // statements
  28063. 'rtl.createClass(this, "TObject", null, function () {',
  28064. ' this.$init = function () {',
  28065. ' };',
  28066. ' this.$final = function () {',
  28067. ' };',
  28068. ' this.GetGlob = function () {',
  28069. ' var Result = 0;',
  28070. ' var v1 = undefined;',
  28071. ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
  28072. ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
  28073. ' return Result;',
  28074. ' };',
  28075. ' this.Getter = function () {',
  28076. ' var Result = 0;',
  28077. ' var v2 = undefined;',
  28078. ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
  28079. ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
  28080. ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
  28081. ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
  28082. ' return Result;',
  28083. ' };',
  28084. '});',
  28085. 'this.GetIt = function (i) {',
  28086. ' var Result = 0;',
  28087. ' var v3 = undefined;',
  28088. ' if (rtl.eqCallback(v3, $mod.GetIt)) ;',
  28089. ' return Result;',
  28090. '};',
  28091. 'this.V = undefined;',
  28092. 'this.o = null;',
  28093. '']),
  28094. LinesToStr([ // $mod.$main
  28095. 'if (rtl.eqCallback($mod.V, $mod.GetIt)) ;',
  28096. 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o, "Getter"))) ;',
  28097. 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o.$class, "GetGlob"))) ;',
  28098. 'if (rtl.eqCallback($mod.GetIt, $mod.V)) ;',
  28099. 'if (rtl.eqCallback(rtl.createCallback($mod.o, "Getter"), $mod.V)) ;',
  28100. 'if (rtl.eqCallback(rtl.createCallback($mod.o.$class, "GetGlob"), $mod.V)) ;',
  28101. '']));
  28102. end;
  28103. procedure TTestModule.TestJSValue_ProcType_Param;
  28104. begin
  28105. StartProgram(false);
  28106. Add([
  28107. 'type',
  28108. ' variant = jsvalue;',
  28109. ' TArrVariant = array of variant;',
  28110. ' TArrVar2 = TArrVariant;',
  28111. ' TFuncInt = function: longint;',
  28112. 'function GetIt: longint;',
  28113. 'begin',
  28114. 'end;',
  28115. 'procedure DoIt(p: jsvalue; Arr: TArrVar2);',
  28116. 'var v: variant;',
  28117. 'begin',
  28118. ' v:=arr[1];',
  28119. 'end;',
  28120. 'var s: string;',
  28121. 'begin',
  28122. ' DoIt(GetIt,[]);',
  28123. ' DoIt(@GetIt,[]);',
  28124. ' DoIt(1,[s,GetIt]);',
  28125. ' DoIt(1,[s,@GetIt]);',
  28126. '']);
  28127. ConvertProgram;
  28128. CheckSource('TestJSValue_ProcType_Param',
  28129. LinesToStr([ // statements
  28130. 'this.GetIt = function () {',
  28131. ' var Result = 0;',
  28132. ' return Result;',
  28133. '};',
  28134. 'this.DoIt = function (p, Arr) {',
  28135. ' var v = undefined;',
  28136. ' v = Arr[1];',
  28137. '};',
  28138. 'this.s = "";',
  28139. '']),
  28140. LinesToStr([ // $mod.$main
  28141. '$mod.DoIt($mod.GetIt(), []);',
  28142. '$mod.DoIt($mod.GetIt, []);',
  28143. '$mod.DoIt(1, [$mod.s, $mod.GetIt()]);',
  28144. '$mod.DoIt(1, [$mod.s, $mod.GetIt]);',
  28145. '']));
  28146. end;
  28147. procedure TTestModule.TestJSValue_AssignToPointerFail;
  28148. begin
  28149. StartProgram(false);
  28150. Add([
  28151. 'var',
  28152. ' v: JSValue;',
  28153. ' p: Pointer;',
  28154. 'begin',
  28155. ' p:=v;',
  28156. '']);
  28157. SetExpectedPasResolverError('Incompatible types: got "JSValue" expected "Pointer"',
  28158. nIncompatibleTypesGotExpected);
  28159. ConvertProgram;
  28160. end;
  28161. procedure TTestModule.TestJSValue_OverloadDouble;
  28162. begin
  28163. StartProgram(false);
  28164. Add([
  28165. 'type',
  28166. ' integer = longint;',
  28167. ' tdatetime = double;',
  28168. 'procedure DoIt(d: double); begin end;',
  28169. 'procedure DoIt(v: jsvalue); begin end;',
  28170. 'var',
  28171. ' d: double;',
  28172. ' dt: tdatetime;',
  28173. ' i: integer;',
  28174. ' b: byte;',
  28175. ' shi: shortint;',
  28176. ' w: word;',
  28177. ' smi: smallint;',
  28178. ' lw: longword;',
  28179. ' li: longint;',
  28180. ' ni: nativeint;',
  28181. ' nu: nativeuint;',
  28182. 'begin',
  28183. ' DoIt(d);',
  28184. ' DoIt(dt);',
  28185. ' DoIt(i);',
  28186. ' DoIt(b);',
  28187. ' DoIt(shi);',
  28188. ' DoIt(w);',
  28189. ' DoIt(smi);',
  28190. ' DoIt(lw);',
  28191. ' DoIt(li);',
  28192. ' DoIt(ni);',
  28193. ' DoIt(nu);',
  28194. '']);
  28195. ConvertProgram;
  28196. CheckSource('TestJSValue_OverloadDouble',
  28197. LinesToStr([ // statements
  28198. 'this.DoIt = function (d) {',
  28199. '};',
  28200. 'this.DoIt$1 = function (v) {',
  28201. '};',
  28202. 'this.d = 0.0;',
  28203. 'this.dt = 0.0;',
  28204. 'this.i = 0;',
  28205. 'this.b = 0;',
  28206. 'this.shi = 0;',
  28207. 'this.w = 0;',
  28208. 'this.smi = 0;',
  28209. 'this.lw = 0;',
  28210. 'this.li = 0;',
  28211. 'this.ni = 0;',
  28212. 'this.nu = 0;',
  28213. '']),
  28214. LinesToStr([ // $mod.$main
  28215. '$mod.DoIt($mod.d);',
  28216. '$mod.DoIt($mod.dt);',
  28217. '$mod.DoIt$1($mod.i);',
  28218. '$mod.DoIt$1($mod.b);',
  28219. '$mod.DoIt$1($mod.shi);',
  28220. '$mod.DoIt$1($mod.w);',
  28221. '$mod.DoIt$1($mod.smi);',
  28222. '$mod.DoIt$1($mod.lw);',
  28223. '$mod.DoIt$1($mod.li);',
  28224. '$mod.DoIt$1($mod.ni);',
  28225. '$mod.DoIt$1($mod.nu);',
  28226. '']));
  28227. end;
  28228. procedure TTestModule.TestJSValue_OverloadNativeInt;
  28229. begin
  28230. StartProgram(false);
  28231. Add([
  28232. 'type',
  28233. ' integer = longint;',
  28234. ' int53 = nativeint;',
  28235. ' tdatetime = double;',
  28236. 'procedure DoIt(n: nativeint); begin end;',
  28237. 'procedure DoIt(v: jsvalue); begin end;',
  28238. 'var',
  28239. ' d: double;',
  28240. ' dt: tdatetime;',
  28241. ' i: integer;',
  28242. ' b: byte;',
  28243. ' shi: shortint;',
  28244. ' w: word;',
  28245. ' smi: smallint;',
  28246. ' lw: longword;',
  28247. ' li: longint;',
  28248. ' ni: nativeint;',
  28249. ' nu: nativeuint;',
  28250. 'begin',
  28251. ' DoIt(d);',
  28252. ' DoIt(dt);',
  28253. ' DoIt(i);',
  28254. ' DoIt(b);',
  28255. ' DoIt(shi);',
  28256. ' DoIt(w);',
  28257. ' DoIt(smi);',
  28258. ' DoIt(lw);',
  28259. ' DoIt(li);',
  28260. ' DoIt(ni);',
  28261. ' DoIt(nu);',
  28262. '']);
  28263. ConvertProgram;
  28264. CheckSource('TestJSValue_OverloadNativeInt',
  28265. LinesToStr([ // statements
  28266. 'this.DoIt = function (n) {',
  28267. '};',
  28268. 'this.DoIt$1 = function (v) {',
  28269. '};',
  28270. 'this.d = 0.0;',
  28271. 'this.dt = 0.0;',
  28272. 'this.i = 0;',
  28273. 'this.b = 0;',
  28274. 'this.shi = 0;',
  28275. 'this.w = 0;',
  28276. 'this.smi = 0;',
  28277. 'this.lw = 0;',
  28278. 'this.li = 0;',
  28279. 'this.ni = 0;',
  28280. 'this.nu = 0;',
  28281. '']),
  28282. LinesToStr([ // $mod.$main
  28283. '$mod.DoIt$1($mod.d);',
  28284. '$mod.DoIt$1($mod.dt);',
  28285. '$mod.DoIt($mod.i);',
  28286. '$mod.DoIt($mod.b);',
  28287. '$mod.DoIt($mod.shi);',
  28288. '$mod.DoIt($mod.w);',
  28289. '$mod.DoIt($mod.smi);',
  28290. '$mod.DoIt($mod.lw);',
  28291. '$mod.DoIt($mod.li);',
  28292. '$mod.DoIt($mod.ni);',
  28293. '$mod.DoIt($mod.nu);',
  28294. '']));
  28295. end;
  28296. procedure TTestModule.TestJSValue_OverloadWord;
  28297. begin
  28298. StartProgram(false);
  28299. Add([
  28300. 'type',
  28301. ' integer = longint;',
  28302. ' int53 = nativeint;',
  28303. ' tdatetime = double;',
  28304. 'procedure DoIt(w: word); begin end;',
  28305. 'procedure DoIt(v: jsvalue); begin end;',
  28306. 'var',
  28307. ' d: double;',
  28308. ' dt: tdatetime;',
  28309. ' i: integer;',
  28310. ' b: byte;',
  28311. ' shi: shortint;',
  28312. ' w: word;',
  28313. ' smi: smallint;',
  28314. ' lw: longword;',
  28315. ' li: longint;',
  28316. ' ni: nativeint;',
  28317. ' nu: nativeuint;',
  28318. 'begin',
  28319. ' DoIt(d);',
  28320. ' DoIt(dt);',
  28321. ' DoIt(i);',
  28322. ' DoIt(b);',
  28323. ' DoIt(shi);',
  28324. ' DoIt(w);',
  28325. ' DoIt(smi);',
  28326. ' DoIt(lw);',
  28327. ' DoIt(li);',
  28328. ' DoIt(ni);',
  28329. ' DoIt(nu);',
  28330. '']);
  28331. ConvertProgram;
  28332. CheckSource('TestJSValue_OverloadWord',
  28333. LinesToStr([ // statements
  28334. 'this.DoIt = function (w) {',
  28335. '};',
  28336. 'this.DoIt$1 = function (v) {',
  28337. '};',
  28338. 'this.d = 0.0;',
  28339. 'this.dt = 0.0;',
  28340. 'this.i = 0;',
  28341. 'this.b = 0;',
  28342. 'this.shi = 0;',
  28343. 'this.w = 0;',
  28344. 'this.smi = 0;',
  28345. 'this.lw = 0;',
  28346. 'this.li = 0;',
  28347. 'this.ni = 0;',
  28348. 'this.nu = 0;',
  28349. '']),
  28350. LinesToStr([ // $mod.$main
  28351. '$mod.DoIt$1($mod.d);',
  28352. '$mod.DoIt$1($mod.dt);',
  28353. '$mod.DoIt$1($mod.i);',
  28354. '$mod.DoIt($mod.b);',
  28355. '$mod.DoIt($mod.shi);',
  28356. '$mod.DoIt($mod.w);',
  28357. '$mod.DoIt$1($mod.smi);',
  28358. '$mod.DoIt$1($mod.lw);',
  28359. '$mod.DoIt$1($mod.li);',
  28360. '$mod.DoIt$1($mod.ni);',
  28361. '$mod.DoIt$1($mod.nu);',
  28362. '']));
  28363. end;
  28364. procedure TTestModule.TestJSValue_OverloadString;
  28365. begin
  28366. StartProgram(false);
  28367. Add([
  28368. 'type',
  28369. ' uni = string;',
  28370. ' WChar = char;',
  28371. 'procedure DoIt(s: string); begin end;',
  28372. 'procedure DoIt(v: jsvalue); begin end;',
  28373. 'var',
  28374. ' s: string;',
  28375. ' c: char;',
  28376. ' u: uni;',
  28377. 'begin',
  28378. ' DoIt(s);',
  28379. ' DoIt(c);',
  28380. ' DoIt(u);',
  28381. '']);
  28382. ConvertProgram;
  28383. CheckSource('TestJSValue_OverloadString',
  28384. LinesToStr([ // statements
  28385. 'this.DoIt = function (s) {',
  28386. '};',
  28387. 'this.DoIt$1 = function (v) {',
  28388. '};',
  28389. 'this.s = "";',
  28390. 'this.c = "";',
  28391. 'this.u = "";',
  28392. '']),
  28393. LinesToStr([ // $mod.$main
  28394. '$mod.DoIt($mod.s);',
  28395. '$mod.DoIt($mod.c);',
  28396. '$mod.DoIt($mod.u);',
  28397. '']));
  28398. end;
  28399. procedure TTestModule.TestJSValue_OverloadChar;
  28400. begin
  28401. StartProgram(false);
  28402. Add([
  28403. 'type',
  28404. ' uni = string;',
  28405. ' WChar = char;',
  28406. 'procedure DoIt(c: char); begin end;',
  28407. 'procedure DoIt(v: jsvalue); begin end;',
  28408. 'var',
  28409. ' s: string;',
  28410. ' c: char;',
  28411. ' u: uni;',
  28412. 'begin',
  28413. ' DoIt(s);',
  28414. ' DoIt(c);',
  28415. ' DoIt(u);',
  28416. '']);
  28417. ConvertProgram;
  28418. CheckSource('TestJSValue_OverloadChar',
  28419. LinesToStr([ // statements
  28420. 'this.DoIt = function (c) {',
  28421. '};',
  28422. 'this.DoIt$1 = function (v) {',
  28423. '};',
  28424. 'this.s = "";',
  28425. 'this.c = "";',
  28426. 'this.u = "";',
  28427. '']),
  28428. LinesToStr([ // $mod.$main
  28429. '$mod.DoIt$1($mod.s);',
  28430. '$mod.DoIt($mod.c);',
  28431. '$mod.DoIt$1($mod.u);',
  28432. '']));
  28433. end;
  28434. procedure TTestModule.TestJSValue_OverloadPointer;
  28435. begin
  28436. StartProgram(false);
  28437. Add([
  28438. 'type',
  28439. ' TObject = class end;',
  28440. 'procedure DoIt(p: pointer); begin end;',
  28441. 'procedure DoIt(v: jsvalue); begin end;',
  28442. 'var',
  28443. ' o: TObject;',
  28444. 'begin',
  28445. ' DoIt(o);',
  28446. '']);
  28447. ConvertProgram;
  28448. CheckSource('TestJSValue_OverloadPointer',
  28449. LinesToStr([ // statements
  28450. 'rtl.createClass(this, "TObject", null, function () {',
  28451. ' this.$init = function () {',
  28452. ' };',
  28453. ' this.$final = function () {',
  28454. ' };',
  28455. '});',
  28456. 'this.DoIt = function (p) {',
  28457. '};',
  28458. 'this.DoIt$1 = function (v) {',
  28459. '};',
  28460. 'this.o = null;',
  28461. '']),
  28462. LinesToStr([ // $mod.$main
  28463. '$mod.DoIt($mod.o);',
  28464. '']));
  28465. end;
  28466. procedure TTestModule.TestJSValue_ForIn;
  28467. begin
  28468. StartProgram(false);
  28469. Add([
  28470. 'var',
  28471. ' v: JSValue;',
  28472. ' key: string;',
  28473. 'begin',
  28474. ' for key in v do begin',
  28475. ' if key=''abc'' then ;',
  28476. ' end;',
  28477. '']);
  28478. ConvertProgram;
  28479. CheckSource('TestJSValue_ForIn',
  28480. LinesToStr([ // statements
  28481. 'this.v = undefined;',
  28482. 'this.key = "";',
  28483. '']),
  28484. LinesToStr([ // $mod.$main
  28485. 'for ($mod.key in $mod.v) {',
  28486. ' if ($mod.key === "abc") ;',
  28487. '};',
  28488. '']));
  28489. end;
  28490. procedure TTestModule.TestRTTI_IntRange;
  28491. begin
  28492. WithTypeInfo:=true;
  28493. StartProgram(true,[supTypeInfo]);
  28494. Add([
  28495. '{$modeswitch externalclass}',
  28496. 'type',
  28497. ' TGraphicsColor = -$7FFFFFFF-1..$7FFFFFFF;',
  28498. ' TColor = type TGraphicsColor;',
  28499. 'var',
  28500. ' p: TTypeInfo;',
  28501. ' k: TTypeKind;',
  28502. 'begin',
  28503. ' p:=typeinfo(TGraphicsColor);',
  28504. ' p:=typeinfo(TColor);',
  28505. ' k:=GetTypeKind(TGraphicsColor);',
  28506. ' k:=GetTypeKind(TColor);',
  28507. '']);
  28508. ConvertProgram;
  28509. CheckSource('TestRTTI_IntRange',
  28510. LinesToStr([ // statements
  28511. 'this.$rtti.$Int("TGraphicsColor", {',
  28512. ' minvalue: -2147483648,',
  28513. ' maxvalue: 2147483647,',
  28514. ' ordtype: 4',
  28515. '});',
  28516. 'this.$rtti.$inherited("TColor", this.$rtti["TGraphicsColor"], {});',
  28517. 'this.p = null;',
  28518. 'this.k = 0;',
  28519. '']),
  28520. LinesToStr([ // $mod.$main
  28521. '$mod.p = $mod.$rtti["TGraphicsColor"];',
  28522. '$mod.p = $mod.$rtti["TColor"];',
  28523. '$mod.k = 1;',
  28524. '$mod.k = 1;',
  28525. '']));
  28526. end;
  28527. procedure TTestModule.TestRTTI_Double;
  28528. begin
  28529. WithTypeInfo:=true;
  28530. StartProgram(true,[supTypeInfo]);
  28531. Add([
  28532. '{$modeswitch externalclass}',
  28533. 'type',
  28534. ' TFloat = type double;',
  28535. 'var',
  28536. ' p: TTypeInfo;',
  28537. 'begin',
  28538. ' p:=typeinfo(double);',
  28539. ' p:=typeinfo(TFloat);',
  28540. '']);
  28541. ConvertProgram;
  28542. CheckSource('TestRTTI_Double',
  28543. LinesToStr([ // statements
  28544. 'this.$rtti.$inherited("TFloat", rtl.double, {});',
  28545. 'this.p = null;',
  28546. '']),
  28547. LinesToStr([ // $mod.$main
  28548. '$mod.p = rtl.double;',
  28549. '$mod.p = $mod.$rtti["TFloat"];',
  28550. '']));
  28551. end;
  28552. procedure TTestModule.TestRTTI_ProcType;
  28553. begin
  28554. WithTypeInfo:=true;
  28555. StartProgram(false);
  28556. Add('type');
  28557. Add(' TProcA = procedure;');
  28558. Add(' TMethodB = procedure of object;');
  28559. Add(' TProcC = procedure; varargs;');
  28560. Add(' TProcD = procedure(i: longint; const j: string; var c: char; out d: double);');
  28561. Add(' TProcE = function: nativeint;');
  28562. Add(' TProcF = function(const p: TProcA): nativeuint;');
  28563. Add('var p: pointer;');
  28564. Add('begin');
  28565. Add(' p:=typeinfo(tproca);');
  28566. ConvertProgram;
  28567. CheckSource('TestRTTI_ProcType',
  28568. LinesToStr([ // statements
  28569. 'this.$rtti.$ProcVar("TProcA", {',
  28570. ' procsig: rtl.newTIProcSig([])',
  28571. '});',
  28572. 'this.$rtti.$MethodVar("TMethodB", {',
  28573. ' procsig: rtl.newTIProcSig([]),',
  28574. ' methodkind: 0',
  28575. '});',
  28576. 'this.$rtti.$ProcVar("TProcC", {',
  28577. ' procsig: rtl.newTIProcSig([], null, 2)',
  28578. '});',
  28579. 'this.$rtti.$ProcVar("TProcD", {',
  28580. ' procsig: rtl.newTIProcSig([["i", rtl.longint], ["j", rtl.string, 2], ["c", rtl.char, 1], ["d", rtl.double, 4]])',
  28581. '});',
  28582. 'this.$rtti.$ProcVar("TProcE", {',
  28583. ' procsig: rtl.newTIProcSig([], rtl.nativeint)',
  28584. '});',
  28585. 'this.$rtti.$ProcVar("TProcF", {',
  28586. ' procsig: rtl.newTIProcSig([["p", this.$rtti["TProcA"], 2]], rtl.nativeuint)',
  28587. '});',
  28588. 'this.p = null;',
  28589. '']),
  28590. LinesToStr([ // $mod.$main
  28591. '$mod.p = $mod.$rtti["TProcA"];',
  28592. '']));
  28593. end;
  28594. procedure TTestModule.TestRTTI_ProcType_ArgFromOtherUnit;
  28595. begin
  28596. WithTypeInfo:=true;
  28597. AddModuleWithIntfImplSrc('unit2.pas',
  28598. LinesToStr([
  28599. 'type',
  28600. ' TObject = class end;'
  28601. ]),
  28602. '');
  28603. StartUnit(true);
  28604. Add('interface');
  28605. Add('uses unit2;');
  28606. Add('type');
  28607. Add(' TProcA = function(o: tobject): tobject;');
  28608. Add('implementation');
  28609. Add('type');
  28610. Add(' TProcB = function(o: tobject): tobject;');
  28611. Add('var p: Pointer;');
  28612. Add('initialization');
  28613. Add(' p:=typeinfo(tproca);');
  28614. Add(' p:=typeinfo(tprocb);');
  28615. ConvertUnit;
  28616. CheckSource('TestRTTI_ProcType_ArgFromOtherUnit',
  28617. LinesToStr([ // statements
  28618. 'var $impl = $mod.$impl;',
  28619. 'this.$rtti.$ProcVar("TProcA", {',
  28620. ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
  28621. '});',
  28622. '']),
  28623. LinesToStr([ // this.$init
  28624. '$impl.p = $mod.$rtti["TProcA"];',
  28625. '$impl.p = $mod.$rtti["TProcB"];',
  28626. '']),
  28627. LinesToStr([ // implementation
  28628. '$mod.$rtti.$ProcVar("TProcB", {',
  28629. ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
  28630. '});',
  28631. '$impl.p = null;',
  28632. '']) );
  28633. end;
  28634. procedure TTestModule.TestRTTI_EnumAndSetType;
  28635. begin
  28636. WithTypeInfo:=true;
  28637. StartProgram(false);
  28638. Add('type');
  28639. Add(' TFlag = (light,dark);');
  28640. Add(' TFlags = set of TFlag;');
  28641. Add(' TProc = function(f: TFlags): TFlag;');
  28642. Add('var p: pointer;');
  28643. Add('begin');
  28644. Add(' p:=typeinfo(tflag);');
  28645. Add(' p:=typeinfo(tflags);');
  28646. ConvertProgram;
  28647. CheckSource('TestRTTI_EnumAndType',
  28648. LinesToStr([ // statements
  28649. 'this.TFlag = {',
  28650. ' "0": "light",',
  28651. ' light: 0,',
  28652. ' "1": "dark",',
  28653. ' dark: 1',
  28654. '};',
  28655. 'this.$rtti.$Enum("TFlag", {',
  28656. ' minvalue: 0,',
  28657. ' maxvalue: 1,',
  28658. ' ordtype: 1,',
  28659. ' enumtype: this.TFlag',
  28660. '});',
  28661. 'this.$rtti.$Set("TFlags", {',
  28662. ' comptype: this.$rtti["TFlag"]',
  28663. '});',
  28664. 'this.$rtti.$ProcVar("TProc", {',
  28665. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TFlags"]]], this.$rtti["TFlag"])',
  28666. '});',
  28667. 'this.p = null;',
  28668. '']),
  28669. LinesToStr([ // $mod.$main
  28670. '$mod.p = $mod.$rtti["TFlag"];',
  28671. '$mod.p = $mod.$rtti["TFlags"];',
  28672. '']));
  28673. end;
  28674. procedure TTestModule.TestRTTI_EnumRange;
  28675. begin
  28676. WithTypeInfo:=true;
  28677. StartProgram(false);
  28678. Add([
  28679. 'type',
  28680. ' TCol = (red,green,blue);',
  28681. ' TColRg = green..blue;',
  28682. ' TSetOfColRg = set of TColRg;',
  28683. 'var p: pointer;',
  28684. 'begin',
  28685. ' p:=typeinfo(tcolrg);',
  28686. ' p:=typeinfo(tsetofcolrg);',
  28687. '']);
  28688. ConvertProgram;
  28689. end;
  28690. procedure TTestModule.TestRTTI_AnonymousEnumType;
  28691. begin
  28692. WithTypeInfo:=true;
  28693. StartProgram(false);
  28694. Add('type');
  28695. Add(' TFlags = set of (red, green);');
  28696. Add('var');
  28697. Add(' f: TFlags;');
  28698. Add('begin');
  28699. Add(' Include(f,red);');
  28700. ConvertProgram;
  28701. CheckSource('TestRTTI_AnonymousEnumType',
  28702. LinesToStr([ // statements
  28703. 'this.TFlags$a = {',
  28704. ' "0": "red",',
  28705. ' red: 0,',
  28706. ' "1": "green",',
  28707. ' green: 1',
  28708. '};',
  28709. 'this.$rtti.$Enum("TFlags$a", {',
  28710. ' minvalue: 0,',
  28711. ' maxvalue: 1,',
  28712. ' ordtype: 1,',
  28713. ' enumtype: this.TFlags$a',
  28714. '});',
  28715. 'this.$rtti.$Set("TFlags", {',
  28716. ' comptype: this.$rtti["TFlags$a"]',
  28717. '});',
  28718. 'this.f = {};',
  28719. '']),
  28720. LinesToStr([
  28721. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  28722. '']));
  28723. end;
  28724. procedure TTestModule.TestRTTI_StaticArray;
  28725. begin
  28726. WithTypeInfo:=true;
  28727. StartProgram(false);
  28728. Add('type');
  28729. Add(' TFlag = (light,dark);');
  28730. Add(' TFlagNames = array[TFlag] of string;');
  28731. Add(' TBoolNames = array[boolean] of string;');
  28732. Add(' TByteArray = array[1..32768] of byte;');
  28733. Add(' TProc = function(f: TBoolNames): TFlagNames;');
  28734. Add('var p: pointer;');
  28735. Add('begin');
  28736. Add(' p:=typeinfo(TFlagNames);');
  28737. Add(' p:=typeinfo(TBoolNames);');
  28738. ConvertProgram;
  28739. CheckSource('TestRTTI_StaticArray',
  28740. LinesToStr([ // statements
  28741. 'this.TFlag = {',
  28742. ' "0": "light",',
  28743. ' light: 0,',
  28744. ' "1": "dark",',
  28745. ' dark: 1',
  28746. '};',
  28747. 'this.$rtti.$Enum("TFlag", {',
  28748. ' minvalue: 0,',
  28749. ' maxvalue: 1,',
  28750. ' ordtype: 1,',
  28751. ' enumtype: this.TFlag',
  28752. '});',
  28753. 'this.$rtti.$StaticArray("TFlagNames", {',
  28754. ' dims: [2],',
  28755. ' eltype: rtl.string',
  28756. '});',
  28757. 'this.$rtti.$StaticArray("TBoolNames", {',
  28758. ' dims: [2],',
  28759. ' eltype: rtl.string',
  28760. '});',
  28761. 'this.$rtti.$StaticArray("TByteArray", {',
  28762. ' dims: [32768],',
  28763. ' eltype: rtl.byte',
  28764. '});',
  28765. 'this.$rtti.$ProcVar("TProc", {',
  28766. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TBoolNames"]]], this.$rtti["TFlagNames"])',
  28767. '});',
  28768. 'this.p = null;',
  28769. '']),
  28770. LinesToStr([ // $mod.$main
  28771. '$mod.p = $mod.$rtti["TFlagNames"];',
  28772. '$mod.p = $mod.$rtti["TBoolNames"];',
  28773. '']));
  28774. end;
  28775. procedure TTestModule.TestRTTI_DynArray;
  28776. begin
  28777. WithTypeInfo:=true;
  28778. StartProgram(false);
  28779. Add('type');
  28780. Add(' TArrStr = array of string;');
  28781. Add(' TArr2Dim = array of tarrstr;');
  28782. Add(' TProc = function(f: TArrStr): TArr2Dim;');
  28783. Add('var p: pointer;');
  28784. Add('begin');
  28785. Add(' p:=typeinfo(tarrstr);');
  28786. Add(' p:=typeinfo(tarr2dim);');
  28787. ConvertProgram;
  28788. CheckSource('TestRTTI_DynArray',
  28789. LinesToStr([ // statements
  28790. 'this.$rtti.$DynArray("TArrStr", {',
  28791. ' eltype: rtl.string',
  28792. '});',
  28793. 'this.$rtti.$DynArray("TArr2Dim", {',
  28794. ' eltype: this.$rtti["TArrStr"]',
  28795. '});',
  28796. 'this.$rtti.$ProcVar("TProc", {',
  28797. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TArrStr"]]], this.$rtti["TArr2Dim"])',
  28798. '});',
  28799. 'this.p = null;',
  28800. '']),
  28801. LinesToStr([ // $mod.$main
  28802. '$mod.p = $mod.$rtti["TArrStr"];',
  28803. '$mod.p = $mod.$rtti["TArr2Dim"];',
  28804. '']));
  28805. end;
  28806. procedure TTestModule.TestRTTI_ArrayNestedAnonymous;
  28807. begin
  28808. WithTypeInfo:=true;
  28809. StartProgram(false);
  28810. Add('type');
  28811. Add(' TArr = array of array of longint;');
  28812. Add('var a: TArr;');
  28813. Add('begin');
  28814. ConvertProgram;
  28815. CheckSource('TestRTTI_ArrayNestedAnonymous',
  28816. LinesToStr([ // statements
  28817. 'this.$rtti.$DynArray("TArr$a", {',
  28818. ' eltype: rtl.longint',
  28819. '});',
  28820. 'this.$rtti.$DynArray("TArr", {',
  28821. ' eltype: this.$rtti["TArr$a"]',
  28822. '});',
  28823. 'this.a = [];',
  28824. '']),
  28825. LinesToStr([ // $mod.$main
  28826. ]));
  28827. end;
  28828. procedure TTestModule.TestRTTI_PublishedMethodOverloadFail;
  28829. begin
  28830. WithTypeInfo:=true;
  28831. StartProgram(false);
  28832. Add('type');
  28833. Add(' TObject = class');
  28834. Add(' published');
  28835. Add(' procedure Proc; virtual; abstract;');
  28836. Add(' procedure Proc(Sender: tobject); virtual; abstract;');
  28837. Add(' end;');
  28838. Add('begin');
  28839. SetExpectedPasResolverError('Duplicate published method "Proc" at test1.pp(6,19)',
  28840. nDuplicatePublishedMethodXAtY);
  28841. ConvertProgram;
  28842. end;
  28843. procedure TTestModule.TestRTTI_PublishedMethodHideNoHint;
  28844. begin
  28845. WithTypeInfo:=true;
  28846. StartUnit(false);
  28847. Add([
  28848. 'interface',
  28849. 'type',
  28850. ' TObject = class',
  28851. ' end;',
  28852. ' {$M+}',
  28853. ' TBird = class',
  28854. ' procedure Fly;',
  28855. ' end;',
  28856. ' {$M-}',
  28857. 'type',
  28858. ' TEagle = class(TBird)',
  28859. ' procedure Fly;',
  28860. ' end;',
  28861. 'implementation',
  28862. 'procedure TBird.Fly;',
  28863. 'begin',
  28864. 'end;',
  28865. 'procedure TEagle.Fly;',
  28866. 'begin',
  28867. 'end;',
  28868. '']);
  28869. ConvertUnit;
  28870. CheckSource('TestRTTI_PublishedMethodHideNoHint',
  28871. LinesToStr([ // statements
  28872. 'rtl.createClass(this, "TObject", null, function () {',
  28873. ' this.$init = function () {',
  28874. ' };',
  28875. ' this.$final = function () {',
  28876. ' };',
  28877. '});',
  28878. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  28879. ' this.Fly = function () {',
  28880. ' };',
  28881. ' var $r = this.$rtti;',
  28882. ' $r.addMethod("Fly", 0, []);',
  28883. '});',
  28884. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  28885. ' this.Fly = function () {',
  28886. ' };',
  28887. ' var $r = this.$rtti;',
  28888. ' $r.addMethod("Fly", 0, []);',
  28889. '});',
  28890. '']),
  28891. LinesToStr([ // $mod.$main
  28892. ]));
  28893. CheckResolverUnexpectedHints(true);
  28894. end;
  28895. procedure TTestModule.TestRTTI_PublishedMethodExternalFail;
  28896. begin
  28897. WithTypeInfo:=true;
  28898. StartProgram(false);
  28899. Add('type');
  28900. Add(' TObject = class');
  28901. Add(' published');
  28902. Add(' procedure Proc; external name ''foo'';');
  28903. Add(' end;');
  28904. Add('begin');
  28905. SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
  28906. nPublishedNameMustMatchExternal);
  28907. ConvertProgram;
  28908. end;
  28909. procedure TTestModule.TestRTTI_PublishedClassPropertyFail;
  28910. begin
  28911. WithTypeInfo:=true;
  28912. StartProgram(false);
  28913. Add('type');
  28914. Add(' TObject = class');
  28915. Add(' class var FA: longint;');
  28916. Add(' published');
  28917. Add(' class property A: longint read FA;');
  28918. Add(' end;');
  28919. Add('begin');
  28920. SetExpectedPasResolverError('Invalid published property modifier "class"',
  28921. nInvalidXModifierY);
  28922. ConvertProgram;
  28923. end;
  28924. procedure TTestModule.TestRTTI_PublishedClassFieldFail;
  28925. begin
  28926. WithTypeInfo:=true;
  28927. StartProgram(false);
  28928. Add('type');
  28929. Add(' TObject = class');
  28930. Add(' published');
  28931. Add(' class var FA: longint;');
  28932. Add(' end;');
  28933. Add('begin');
  28934. SetExpectedPasResolverError(sSymbolCannotBePublished,
  28935. nSymbolCannotBePublished);
  28936. ConvertProgram;
  28937. end;
  28938. procedure TTestModule.TestRTTI_PublishedFieldExternalFail;
  28939. begin
  28940. WithTypeInfo:=true;
  28941. StartProgram(false);
  28942. Add('{$modeswitch externalclass}');
  28943. Add('type');
  28944. Add(' TObject = class');
  28945. Add(' published');
  28946. Add(' V: longint; external name ''foo'';');
  28947. Add(' end;');
  28948. Add('begin');
  28949. SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
  28950. nPublishedNameMustMatchExternal);
  28951. ConvertProgram;
  28952. end;
  28953. procedure TTestModule.TestRTTI_Class_Field;
  28954. begin
  28955. WithTypeInfo:=true;
  28956. StartProgram(false);
  28957. Add('{$modeswitch externalclass}');
  28958. Add('type');
  28959. Add(' TObject = class');
  28960. Add(' private');
  28961. Add(' FPropA: string;');
  28962. Add(' published');
  28963. Add(' VarLI: longint;');
  28964. Add(' VarC: char;');
  28965. Add(' VarS: string;');
  28966. Add(' VarD: double;');
  28967. Add(' VarB: boolean;');
  28968. Add(' VarLW: longword;');
  28969. Add(' VarSmI: smallint;');
  28970. Add(' VarW: word;');
  28971. Add(' VarShI: shortint;');
  28972. Add(' VarBy: byte;');
  28973. Add(' VarExt: longint external name ''VarExt'';');
  28974. Add(' ArrA, ArrB: array of byte;');
  28975. Add(' end;');
  28976. Add('var p: pointer;');
  28977. Add(' Obj: tobject;');
  28978. Add('begin');
  28979. Add(' p:=typeinfo(tobject);');
  28980. Add(' p:=typeinfo(p);');
  28981. Add(' p:=typeinfo(obj);');
  28982. ConvertProgram;
  28983. CheckSource('TestRTTI_Class_Field',
  28984. LinesToStr([ // statements
  28985. 'rtl.createClass(this, "TObject", null, function () {',
  28986. ' this.$init = function () {',
  28987. ' this.FPropA = "";',
  28988. ' this.VarLI = 0;',
  28989. ' this.VarC = "";',
  28990. ' this.VarS = "";',
  28991. ' this.VarD = 0.0;',
  28992. ' this.VarB = false;',
  28993. ' this.VarLW = 0;',
  28994. ' this.VarSmI = 0;',
  28995. ' this.VarW = 0;',
  28996. ' this.VarShI = 0;',
  28997. ' this.VarBy = 0;',
  28998. ' this.ArrA = [];',
  28999. ' this.ArrB = [];',
  29000. ' };',
  29001. ' this.$final = function () {',
  29002. ' this.ArrA = undefined;',
  29003. ' this.ArrB = undefined;',
  29004. ' };',
  29005. ' var $r = this.$rtti;',
  29006. ' $r.addField("VarLI", rtl.longint);',
  29007. ' $r.addField("VarC", rtl.char);',
  29008. ' $r.addField("VarS", rtl.string);',
  29009. ' $r.addField("VarD", rtl.double);',
  29010. ' $r.addField("VarB", rtl.boolean);',
  29011. ' $r.addField("VarLW", rtl.longword);',
  29012. ' $r.addField("VarSmI", rtl.smallint);',
  29013. ' $r.addField("VarW", rtl.word);',
  29014. ' $r.addField("VarShI", rtl.shortint);',
  29015. ' $r.addField("VarBy", rtl.byte);',
  29016. ' $r.addField("VarExt", rtl.longint);',
  29017. ' $mod.$rtti.$DynArray("TObject.ArrB$a", {',
  29018. ' eltype: rtl.byte',
  29019. ' });',
  29020. ' $r.addField("ArrA", $mod.$rtti["TObject.ArrB$a"]);',
  29021. ' $r.addField("ArrB", $mod.$rtti["TObject.ArrB$a"]);',
  29022. '});',
  29023. 'this.p = null;',
  29024. 'this.Obj = null;',
  29025. '']),
  29026. LinesToStr([ // $mod.$main
  29027. '$mod.p = $mod.$rtti["TObject"];',
  29028. '$mod.p = rtl.pointer;',
  29029. '$mod.p = $mod.Obj.$rtti;',
  29030. '']));
  29031. end;
  29032. procedure TTestModule.TestRTTI_Class_Method;
  29033. begin
  29034. WithTypeInfo:=true;
  29035. StartProgram(false);
  29036. Add([
  29037. 'type',
  29038. ' TObject = class',
  29039. ' private',
  29040. ' procedure Internal; external name ''$intern'';',
  29041. ' published',
  29042. ' procedure Click; virtual; abstract;',
  29043. ' procedure Notify(Sender: TObject); virtual; abstract;',
  29044. ' function GetNotify: boolean; external name ''GetNotify'';',
  29045. ' procedure Println(a,b: longint); varargs; virtual; abstract;',
  29046. ' function Fetch(URL: string): word; async; external name ''Fetch'';',
  29047. ' end;',
  29048. 'begin']);
  29049. ConvertProgram;
  29050. CheckSource('TestRTTI_Class_Method',
  29051. LinesToStr([ // statements
  29052. 'rtl.createClass(this, "TObject", null, function () {',
  29053. ' this.$init = function () {',
  29054. ' };',
  29055. ' this.$final = function () {',
  29056. ' };',
  29057. ' var $r = this.$rtti;',
  29058. ' $r.addMethod("Click", 0, []);',
  29059. ' $r.addMethod("Notify", 0, [["Sender", $r]]);',
  29060. ' $r.addMethod("GetNotify", 1, [], rtl.boolean, 4);',
  29061. ' $r.addMethod("Println", 0, [["a", rtl.longint], ["b", rtl.longint]], null, 2);',
  29062. ' $r.addMethod("Fetch", 1, [["URL", rtl.string]], rtl.word, 20);',
  29063. '});',
  29064. '']),
  29065. LinesToStr([ // $mod.$main
  29066. '']));
  29067. end;
  29068. procedure TTestModule.TestRTTI_Class_MethodArgFlags;
  29069. begin
  29070. WithTypeInfo:=true;
  29071. StartProgram(false);
  29072. Add('type');
  29073. Add(' TObject = class');
  29074. Add(' published');
  29075. Add(' procedure OpenArray(const Args: array of string); virtual; abstract;');
  29076. Add(' procedure ByRef(var Value: longint; out Item: longint); virtual; abstract;');
  29077. Add(' procedure Untyped(var Value; out Item); virtual; abstract;');
  29078. Add(' end;');
  29079. Add('begin');
  29080. ConvertProgram;
  29081. CheckSource('TestRTTI_Class_MethodOpenArray',
  29082. LinesToStr([ // statements
  29083. 'rtl.createClass(this, "TObject", null, function () {',
  29084. ' this.$init = function () {',
  29085. ' };',
  29086. ' this.$final = function () {',
  29087. ' };',
  29088. ' var $r = this.$rtti;',
  29089. '$r.addMethod("OpenArray", 0, [["Args", rtl.string, 10]]);',
  29090. '$r.addMethod("ByRef", 0, [["Value", rtl.longint, 1], ["Item", rtl.longint, 4]]);',
  29091. '$r.addMethod("Untyped", 0, [["Value", null, 1], ["Item", null, 4]]);',
  29092. '});',
  29093. '']),
  29094. LinesToStr([ // $mod.$main
  29095. '']));
  29096. end;
  29097. procedure TTestModule.TestRTTI_Class_Property;
  29098. begin
  29099. WithTypeInfo:=true;
  29100. StartProgram(false);
  29101. Add('{$modeswitch externalclass}');
  29102. Add('type');
  29103. Add(' TObject = class');
  29104. Add(' private');
  29105. Add(' FColor: longint;');
  29106. Add(' FColorStored: boolean;');
  29107. Add(' procedure SetColor(Value: longint); virtual; abstract;');
  29108. Add(' function GetColor: longint; virtual; abstract;');
  29109. Add(' function GetColorStored: boolean; virtual; abstract;');
  29110. Add(' FExtSize: longint external name ''$extSize'';');
  29111. Add(' FExtSizeStored: boolean external name ''$extSizeStored'';');
  29112. Add(' procedure SetExtSize(Value: longint); external name ''$setSize'';');
  29113. Add(' function GetExtSize: longint; external name ''$getSize'';');
  29114. Add(' function GetExtSizeStored: boolean; external name ''$getExtSizeStored'';');
  29115. Add(' published');
  29116. Add(' property ColorA: longint read FColor;');
  29117. Add(' property ColorB: longint write FColor;');
  29118. Add(' property ColorC: longint read GetColor write SetColor;');
  29119. Add(' property ColorD: longint read FColor write FColor stored FColorStored;');
  29120. Add(' property ExtSizeA: longint read FExtSize write FExtSize;');
  29121. Add(' property ExtSizeB: longint read GetExtSize write SetExtSize stored FExtSizeStored;');
  29122. Add(' property ExtSizeC: longint read FExtSize write FExtSize stored GetExtSizeStored;');
  29123. Add(' end;');
  29124. Add('begin');
  29125. ConvertProgram;
  29126. CheckSource('TestRTTI_Class_Property',
  29127. LinesToStr([ // statements
  29128. 'rtl.createClass(this, "TObject", null, function () {',
  29129. ' this.$init = function () {',
  29130. ' this.FColor = 0;',
  29131. ' this.FColorStored = false;',
  29132. ' };',
  29133. ' this.$final = function () {',
  29134. ' };',
  29135. ' var $r = this.$rtti;',
  29136. ' $r.addProperty("ColorA", 0, rtl.longint, "FColor", "");',
  29137. ' $r.addProperty("ColorB", 0, rtl.longint, "", "FColor");',
  29138. ' $r.addProperty("ColorC", 3, rtl.longint, "GetColor", "SetColor");',
  29139. ' $r.addProperty(',
  29140. ' "ColorD",',
  29141. ' 8,',
  29142. ' rtl.longint,',
  29143. ' "FColor",',
  29144. ' "FColor",',
  29145. ' {',
  29146. ' stored: "FColorStored"',
  29147. ' }',
  29148. ' );',
  29149. ' $r.addProperty("ExtSizeA", 0, rtl.longint, "$extSize", "$extSize");',
  29150. ' $r.addProperty(',
  29151. ' "ExtSizeB",',
  29152. ' 11,',
  29153. ' rtl.longint,',
  29154. ' "$getSize",',
  29155. ' "$setSize",',
  29156. ' {',
  29157. ' stored: "$extSizeStored"',
  29158. ' }',
  29159. ' );',
  29160. ' $r.addProperty(',
  29161. ' "ExtSizeC",',
  29162. ' 12,',
  29163. ' rtl.longint,',
  29164. ' "$extSize",',
  29165. ' "$extSize",',
  29166. ' {',
  29167. ' stored: "$getExtSizeStored"',
  29168. ' }',
  29169. ' );',
  29170. '});',
  29171. '']),
  29172. LinesToStr([ // $mod.$main
  29173. '']));
  29174. end;
  29175. procedure TTestModule.TestRTTI_Class_PropertyParams;
  29176. begin
  29177. WithTypeInfo:=true;
  29178. StartProgram(false);
  29179. Add('{$modeswitch externalclass}');
  29180. Add('type');
  29181. Add(' integer = longint;');
  29182. Add(' TObject = class');
  29183. Add(' private');
  29184. Add(' function GetItems(i: integer): tobject; virtual; abstract;');
  29185. Add(' procedure SetItems(i: integer; value: tobject); virtual; abstract;');
  29186. Add(' function GetValues(const i: integer; var b: boolean): char; virtual; abstract;');
  29187. Add(' procedure SetValues(const i: integer; var b: boolean; value: char); virtual; abstract;');
  29188. Add(' published');
  29189. Add(' property Items[Index: integer]: tobject read getitems write setitems;');
  29190. Add(' property Values[const keya: integer; var keyb: boolean]: char read getvalues write setvalues;');
  29191. Add(' end;');
  29192. Add('begin');
  29193. ConvertProgram;
  29194. CheckSource('TestRTTI_Class_PropertyParams',
  29195. LinesToStr([ // statements
  29196. 'rtl.createClass(this, "TObject", null, function () {',
  29197. ' this.$init = function () {',
  29198. ' };',
  29199. ' this.$final = function () {',
  29200. ' };',
  29201. ' var $r = this.$rtti;',
  29202. ' $r.addProperty("Items", 3, $r, "GetItems", "SetItems");',
  29203. ' $r.addProperty("Values", 3, rtl.char, "GetValues", "SetValues");',
  29204. '});',
  29205. '']),
  29206. LinesToStr([ // $mod.$main
  29207. '']));
  29208. end;
  29209. procedure TTestModule.TestRTTI_Class_OtherUnit_TypeAlias;
  29210. begin
  29211. WithTypeInfo:=true;
  29212. AddModuleWithIntfImplSrc('unit1.pas',
  29213. 'type TColor = -5..5;',
  29214. '');
  29215. StartProgram(true);
  29216. Add([
  29217. 'uses unit1;',
  29218. 'type',
  29219. ' TColorAlias = TColor;',
  29220. ' TColorTypeAlias = type TColor;',
  29221. ' TObject = class',
  29222. ' private',
  29223. ' fColor: TColor;',
  29224. ' fAlias: TColorAlias;',
  29225. ' fTypeAlias: TColorTypeAlias;',
  29226. ' published',
  29227. ' property Color: TColor read fcolor;',
  29228. ' property Alias: TColorAlias read falias;',
  29229. ' property TypeAlias: TColorTypeAlias read ftypealias;',
  29230. ' end;',
  29231. 'begin',
  29232. '']);
  29233. ConvertProgram;
  29234. CheckSource('TestRTTI_Class_OtherUnit_TypeAlias',
  29235. LinesToStr([ // statements
  29236. 'this.$rtti.$inherited("TColorTypeAlias", pas.unit1.$rtti["TColor"], {});',
  29237. 'rtl.createClass(this, "TObject", null, function () {',
  29238. ' this.$init = function () {',
  29239. ' this.fColor = 0;',
  29240. ' this.fAlias = 0;',
  29241. ' this.fTypeAlias = 0;',
  29242. ' };',
  29243. ' this.$final = function () {',
  29244. ' };',
  29245. ' var $r = this.$rtti;',
  29246. ' $r.addProperty("Color", 0, pas.unit1.$rtti["TColor"], "fColor", "");',
  29247. ' $r.addProperty("Alias", 0, pas.unit1.$rtti["TColor"], "fAlias", "");',
  29248. ' $r.addProperty("TypeAlias", 0, $mod.$rtti["TColorTypeAlias"], "fTypeAlias", "");',
  29249. '});',
  29250. '']),
  29251. LinesToStr([ // $mod.$main
  29252. '']));
  29253. end;
  29254. procedure TTestModule.TestRTTI_Class_OmitRTTI;
  29255. begin
  29256. WithTypeInfo:=true;
  29257. StartProgram(false);
  29258. Add([
  29259. '{$modeswitch omitrtti}',
  29260. 'type',
  29261. ' TObject = class',
  29262. ' private',
  29263. ' FA: byte;',
  29264. ' published',
  29265. ' property A: byte read FA write FA;',
  29266. ' end;',
  29267. 'begin']);
  29268. ConvertProgram;
  29269. CheckSource('TestRTTI_Class_OmitRTTI',
  29270. LinesToStr([ // statements
  29271. 'rtl.createClass(this, "TObject", null, function () {',
  29272. ' this.$init = function () {',
  29273. ' this.FA = 0;',
  29274. ' };',
  29275. ' this.$final = function () {',
  29276. ' };',
  29277. '});',
  29278. '']),
  29279. LinesToStr([ // $mod.$main
  29280. '']));
  29281. end;
  29282. procedure TTestModule.TestRTTI_Class_Field_AnonymousArrayOfSelfClass;
  29283. begin
  29284. WithTypeInfo:=true;
  29285. StartUnit(true,[supTObject]);
  29286. Add([
  29287. 'interface',
  29288. 'type',
  29289. ' {$M+}',
  29290. ' TBird = class',
  29291. ' published',
  29292. ' Swarm: array of TBird;',
  29293. ' end;',
  29294. 'implementation',
  29295. '']);
  29296. ConvertUnit;
  29297. CheckSource('TestRTTI_Class_Field_AnonymousArrayOfSelfClass',
  29298. LinesToStr([ // statements
  29299. 'rtl.createClass(this, "TBird", pas.system.TObject, function () {',
  29300. ' this.$init = function () {',
  29301. ' pas.system.TObject.$init.call(this);',
  29302. ' this.Swarm = [];',
  29303. ' };',
  29304. ' this.$final = function () {',
  29305. ' this.Swarm = undefined;',
  29306. ' pas.system.TObject.$final.call(this);',
  29307. ' };',
  29308. ' var $r = this.$rtti;',
  29309. ' $mod.$rtti.$DynArray("TBird.Swarm$a", {',
  29310. ' eltype: $r',
  29311. ' });',
  29312. ' $r.addField("Swarm", $mod.$rtti["TBird.Swarm$a"]);',
  29313. '});',
  29314. '']),
  29315. LinesToStr([ // $mod.$main
  29316. '']));
  29317. end;
  29318. procedure TTestModule.TestRTTI_IndexModifier;
  29319. begin
  29320. WithTypeInfo:=true;
  29321. StartProgram(false);
  29322. Add([
  29323. 'type',
  29324. ' TEnum = (red, blue);',
  29325. ' TObject = class',
  29326. ' FB: boolean;',
  29327. ' procedure SetIntBool(Index: longint; b: boolean); virtual; abstract;',
  29328. ' function GetBoolBool(Index: boolean): boolean; virtual; abstract;',
  29329. ' procedure SetBoolBool(Index: boolean; b: boolean); virtual; abstract;',
  29330. ' function GetEnumBool(Index: TEnum): boolean; virtual; abstract;',
  29331. ' function GetStrIntBool(A: String; I: longint): boolean; virtual; abstract;',
  29332. ' procedure SetStrIntBool(A: String; I: longint; b: boolean); virtual; abstract;',
  29333. ' published',
  29334. ' property B1: boolean index 1 read FB write SetIntBool;',
  29335. ' property B2: boolean index TEnum.blue read GetEnumBool write FB;',
  29336. ' property I1[A: String]: boolean index 2 read GetStrIntBool write SetStrIntBool;',
  29337. ' end;',
  29338. 'begin']);
  29339. ConvertProgram;
  29340. CheckSource('TestRTTI_IndexModifier',
  29341. LinesToStr([ // statements
  29342. 'this.TEnum = {',
  29343. ' "0": "red",',
  29344. ' red: 0,',
  29345. ' "1": "blue",',
  29346. ' blue: 1',
  29347. '};',
  29348. 'this.$rtti.$Enum("TEnum", {',
  29349. ' minvalue: 0,',
  29350. ' maxvalue: 1,',
  29351. ' ordtype: 1,',
  29352. ' enumtype: this.TEnum',
  29353. '});',
  29354. 'rtl.createClass(this, "TObject", null, function () {',
  29355. ' this.$init = function () {',
  29356. ' this.FB = false;',
  29357. ' };',
  29358. ' this.$final = function () {',
  29359. ' };',
  29360. ' var $r = this.$rtti;',
  29361. ' $r.addProperty(',
  29362. ' "B1",',
  29363. ' 18,',
  29364. ' rtl.boolean,',
  29365. ' "FB",',
  29366. ' "SetIntBool",',
  29367. ' {',
  29368. ' index: 1',
  29369. ' }',
  29370. ' );',
  29371. ' $r.addProperty(',
  29372. ' "B2",',
  29373. ' 17,',
  29374. ' rtl.boolean,',
  29375. ' "GetEnumBool",',
  29376. ' "FB",',
  29377. ' {',
  29378. ' index: $mod.TEnum.blue',
  29379. ' }',
  29380. ' );',
  29381. ' $r.addProperty(',
  29382. ' "I1",',
  29383. ' 19,',
  29384. ' rtl.boolean,',
  29385. ' "GetStrIntBool",',
  29386. ' "SetStrIntBool",',
  29387. ' {',
  29388. ' index: 2',
  29389. ' }',
  29390. ' );',
  29391. '});',
  29392. '']),
  29393. LinesToStr([ // $mod.$main
  29394. '']));
  29395. end;
  29396. procedure TTestModule.TestRTTI_StoredModifier;
  29397. begin
  29398. WithTypeInfo:=true;
  29399. StartProgram(false);
  29400. Add([
  29401. 'const',
  29402. ' ConstB = true;',
  29403. 'type',
  29404. ' TObject = class',
  29405. ' private',
  29406. ' FB: boolean;',
  29407. ' function IsBStored: boolean; virtual; abstract;',
  29408. ' published',
  29409. ' property BoolA: boolean read FB stored true;',
  29410. ' property BoolB: boolean read FB stored false;',
  29411. ' property BoolC: boolean read FB stored FB;',
  29412. ' property BoolD: boolean read FB stored ConstB;',
  29413. ' property BoolE: boolean read FB stored IsBStored;',
  29414. ' end;',
  29415. 'begin']);
  29416. ConvertProgram;
  29417. CheckSource('TestRTTI_StoredModifier',
  29418. LinesToStr([ // statements
  29419. 'this.ConstB = true;',
  29420. 'rtl.createClass(this, "TObject", null, function () {',
  29421. ' this.$init = function () {',
  29422. ' this.FB = false;',
  29423. ' };',
  29424. ' this.$final = function () {',
  29425. ' };',
  29426. ' var $r = this.$rtti;',
  29427. ' $r.addProperty("BoolA", 0, rtl.boolean, "FB", "");',
  29428. ' $r.addProperty("BoolB", 4, rtl.boolean, "FB", "");',
  29429. ' $r.addProperty(',
  29430. ' "BoolC",',
  29431. ' 8,',
  29432. ' rtl.boolean,',
  29433. ' "FB",',
  29434. ' "",',
  29435. ' {',
  29436. ' stored: "FB"',
  29437. ' }',
  29438. ' );',
  29439. ' $r.addProperty("BoolD", 0, rtl.boolean, "FB", "");',
  29440. ' $r.addProperty(',
  29441. ' "BoolE",',
  29442. ' 12,',
  29443. ' rtl.boolean,',
  29444. ' "FB",',
  29445. ' "",',
  29446. ' {',
  29447. ' stored: "IsBStored"',
  29448. ' }',
  29449. ' );',
  29450. '});',
  29451. '']),
  29452. LinesToStr([ // $mod.$main
  29453. '']));
  29454. end;
  29455. procedure TTestModule.TestRTTI_DefaultValue;
  29456. begin
  29457. WithTypeInfo:=true;
  29458. StartProgram(false);
  29459. Add([
  29460. 'type',
  29461. ' TEnum = (red, blue);',
  29462. 'const',
  29463. ' CB = true or false;',
  29464. ' CI = 1+2;',
  29465. 'type',
  29466. ' TObject = class',
  29467. ' FB: boolean;',
  29468. ' FI: longint;',
  29469. ' FE: TEnum;',
  29470. ' published',
  29471. ' property B1: boolean read FB default true;',
  29472. ' property B2: boolean read FB default CB;',
  29473. ' property B3: boolean read FB default test1.cb;',
  29474. ' property I1: longint read FI default 2;',
  29475. ' property I2: longint read FI default CI;',
  29476. ' property E1: TEnum read FE default red;',
  29477. ' property E2: TEnum read FE default TEnum.blue;',
  29478. ' end;',
  29479. 'begin']);
  29480. ConvertProgram;
  29481. CheckSource('TestRTTI_DefaultValue',
  29482. LinesToStr([ // statements
  29483. 'this.TEnum = {',
  29484. ' "0": "red",',
  29485. ' red: 0,',
  29486. ' "1": "blue",',
  29487. ' blue: 1',
  29488. '};',
  29489. 'this.$rtti.$Enum("TEnum", {',
  29490. ' minvalue: 0,',
  29491. ' maxvalue: 1,',
  29492. ' ordtype: 1,',
  29493. ' enumtype: this.TEnum',
  29494. '});',
  29495. 'this.CB = true || false;',
  29496. 'this.CI = 1 + 2;',
  29497. 'rtl.createClass(this, "TObject", null, function () {',
  29498. ' this.$init = function () {',
  29499. ' this.FB = false;',
  29500. ' this.FI = 0;',
  29501. ' this.FE = 0;',
  29502. ' };',
  29503. ' this.$final = function () {',
  29504. ' };',
  29505. ' var $r = this.$rtti;',
  29506. ' $r.addProperty(',
  29507. ' "B1",',
  29508. ' 0,',
  29509. ' rtl.boolean,',
  29510. ' "FB",',
  29511. ' "",',
  29512. ' {',
  29513. ' Default: true',
  29514. ' }',
  29515. ' );',
  29516. ' $r.addProperty(',
  29517. ' "B2",',
  29518. ' 0,',
  29519. ' rtl.boolean,',
  29520. ' "FB",',
  29521. ' "",',
  29522. ' {',
  29523. ' Default: true',
  29524. ' }',
  29525. ' );',
  29526. ' $r.addProperty(',
  29527. ' "B3",',
  29528. ' 0,',
  29529. ' rtl.boolean,',
  29530. ' "FB",',
  29531. ' "",',
  29532. ' {',
  29533. ' Default: true',
  29534. ' }',
  29535. ' );',
  29536. ' $r.addProperty(',
  29537. ' "I1",',
  29538. ' 0,',
  29539. ' rtl.longint,',
  29540. ' "FI",',
  29541. ' "",',
  29542. ' {',
  29543. ' Default: 2',
  29544. ' }',
  29545. ' );',
  29546. ' $r.addProperty(',
  29547. ' "I2",',
  29548. ' 0,',
  29549. ' rtl.longint,',
  29550. ' "FI",',
  29551. ' "",',
  29552. ' {',
  29553. ' Default: 3',
  29554. ' }',
  29555. ' );',
  29556. ' $r.addProperty(',
  29557. ' "E1",',
  29558. ' 0,',
  29559. ' $mod.$rtti["TEnum"],',
  29560. ' "FE",',
  29561. ' "",',
  29562. ' {',
  29563. ' Default: $mod.TEnum.red',
  29564. ' }',
  29565. ' );',
  29566. ' $r.addProperty(',
  29567. ' "E2",',
  29568. ' 0,',
  29569. ' $mod.$rtti["TEnum"],',
  29570. ' "FE",',
  29571. ' "",',
  29572. ' {',
  29573. ' Default: $mod.TEnum.blue',
  29574. ' }',
  29575. ' );',
  29576. '});',
  29577. '']),
  29578. LinesToStr([ // $mod.$main
  29579. '']));
  29580. end;
  29581. procedure TTestModule.TestRTTI_DefaultValueSet;
  29582. begin
  29583. WithTypeInfo:=true;
  29584. StartProgram(false);
  29585. Add([
  29586. 'type',
  29587. ' TEnum = (red, blue);',
  29588. ' TSet = set of TEnum;',
  29589. 'const',
  29590. ' CSet = [red,blue];',
  29591. 'type',
  29592. ' TObject = class',
  29593. ' FSet: TSet;',
  29594. ' published',
  29595. ' property Set1: TSet read FSet default [];',
  29596. ' property Set2: TSet read FSet default [red];',
  29597. ' property Set3: TSet read FSet default [red,blue];',
  29598. ' property Set4: TSet read FSet default CSet;',
  29599. ' end;',
  29600. 'begin']);
  29601. ConvertProgram;
  29602. CheckSource('TestRTTI_DefaultValueSet',
  29603. LinesToStr([ // statements
  29604. 'this.TEnum = {',
  29605. ' "0": "red",',
  29606. ' red: 0,',
  29607. ' "1": "blue",',
  29608. ' blue: 1',
  29609. '};',
  29610. 'this.$rtti.$Enum("TEnum", {',
  29611. ' minvalue: 0,',
  29612. ' maxvalue: 1,',
  29613. ' ordtype: 1,',
  29614. ' enumtype: this.TEnum',
  29615. '});',
  29616. 'this.$rtti.$Set("TSet", {',
  29617. ' comptype: this.$rtti["TEnum"]',
  29618. '});',
  29619. 'this.CSet = rtl.createSet(this.TEnum.red, this.TEnum.blue);',
  29620. 'rtl.createClass(this, "TObject", null, function () {',
  29621. ' this.$init = function () {',
  29622. ' this.FSet = {};',
  29623. ' };',
  29624. ' this.$final = function () {',
  29625. ' this.FSet = undefined;',
  29626. ' };',
  29627. ' var $r = this.$rtti;',
  29628. ' $r.addProperty(',
  29629. ' "Set1",',
  29630. ' 0,',
  29631. ' $mod.$rtti["TSet"],',
  29632. ' "FSet",',
  29633. ' "",',
  29634. ' {',
  29635. ' Default: {}',
  29636. ' }',
  29637. ' );',
  29638. ' $r.addProperty(',
  29639. ' "Set2",',
  29640. ' 0,',
  29641. ' $mod.$rtti["TSet"],',
  29642. ' "FSet",',
  29643. ' "",',
  29644. ' {',
  29645. ' Default: rtl.createSet($mod.TEnum.red)',
  29646. ' }',
  29647. ' );',
  29648. ' $r.addProperty(',
  29649. ' "Set3",',
  29650. ' 0,',
  29651. ' $mod.$rtti["TSet"],',
  29652. ' "FSet",',
  29653. ' "",',
  29654. ' {',
  29655. ' Default: rtl.createSet($mod.TEnum.red, $mod.TEnum.blue)',
  29656. ' }',
  29657. ' );',
  29658. ' $r.addProperty(',
  29659. ' "Set4",',
  29660. ' 0,',
  29661. ' $mod.$rtti["TSet"],',
  29662. ' "FSet",',
  29663. ' "",',
  29664. ' {',
  29665. ' Default: $mod.CSet',
  29666. ' }',
  29667. ' );',
  29668. '});',
  29669. '']),
  29670. LinesToStr([ // $mod.$main
  29671. '']));
  29672. end;
  29673. procedure TTestModule.TestRTTI_DefaultValueRangeType;
  29674. begin
  29675. WithTypeInfo:=true;
  29676. StartProgram(false);
  29677. Add([
  29678. 'type',
  29679. ' TRg = -1..1;',
  29680. 'const',
  29681. ' l = low(TRg);',
  29682. ' h = high(TRg);',
  29683. 'type',
  29684. ' TObject = class',
  29685. ' FV: TRg;',
  29686. ' published',
  29687. ' property V1: TRg read FV default -1;',
  29688. ' end;',
  29689. 'begin']);
  29690. ConvertProgram;
  29691. CheckSource('TestRTTI_DefaultValueRangeType',
  29692. LinesToStr([ // statements
  29693. 'this.$rtti.$Int("TRg", {',
  29694. ' minvalue: -1,',
  29695. ' maxvalue: 1,',
  29696. ' ordtype: 0',
  29697. '});',
  29698. 'this.l = -1;',
  29699. 'this.h = 1;',
  29700. 'rtl.createClass(this, "TObject", null, function () {',
  29701. ' this.$init = function () {',
  29702. ' this.FV = 0;',
  29703. ' };',
  29704. ' this.$final = function () {',
  29705. ' };',
  29706. ' var $r = this.$rtti;',
  29707. ' $r.addProperty(',
  29708. ' "V1",',
  29709. ' 0,',
  29710. ' $mod.$rtti["TRg"],',
  29711. ' "FV",',
  29712. ' "",',
  29713. ' {',
  29714. ' Default: -1',
  29715. ' }',
  29716. ' );',
  29717. '});',
  29718. '']),
  29719. LinesToStr([ // $mod.$main
  29720. '']));
  29721. end;
  29722. procedure TTestModule.TestRTTI_DefaultValueInherit;
  29723. begin
  29724. WithTypeInfo:=true;
  29725. StartProgram(false);
  29726. Add([
  29727. 'type',
  29728. ' TObject = class',
  29729. ' FA, FB: byte;',
  29730. ' property A: byte read FA default 1;',
  29731. ' property B: byte read FB default 2;',
  29732. ' end;',
  29733. ' TBird = class',
  29734. ' published',
  29735. ' property A;',
  29736. ' property B nodefault;',
  29737. ' end;',
  29738. 'begin']);
  29739. ConvertProgram;
  29740. CheckSource('TestRTTI_DefaultValueInherit',
  29741. LinesToStr([ // statements
  29742. 'rtl.createClass(this, "TObject", null, function () {',
  29743. ' this.$init = function () {',
  29744. ' this.FA = 0;',
  29745. ' this.FB = 0;',
  29746. ' };',
  29747. ' this.$final = function () {',
  29748. ' };',
  29749. '});',
  29750. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  29751. ' var $r = this.$rtti;',
  29752. ' $r.addProperty(',
  29753. ' "A",',
  29754. ' 0,',
  29755. ' rtl.byte,',
  29756. ' "FA",',
  29757. ' "",',
  29758. ' {',
  29759. ' Default: 1',
  29760. ' }',
  29761. ' );',
  29762. ' $r.addProperty("B", 0, rtl.byte, "FB", "");',
  29763. '});',
  29764. '']),
  29765. LinesToStr([ // $mod.$main
  29766. '']));
  29767. end;
  29768. procedure TTestModule.TestRTTI_OverrideMethod;
  29769. begin
  29770. WithTypeInfo:=true;
  29771. StartProgram(false);
  29772. Add('type');
  29773. Add(' TObject = class');
  29774. Add(' published');
  29775. Add(' procedure DoIt; virtual; abstract;');
  29776. Add(' end;');
  29777. Add(' TSky = class');
  29778. Add(' published');
  29779. Add(' procedure DoIt; override;');
  29780. Add(' end;');
  29781. Add('procedure TSky.DoIt; begin end;');
  29782. Add('begin');
  29783. ConvertProgram;
  29784. CheckSource('TestRTTI_OverrideMethod',
  29785. LinesToStr([ // statements
  29786. 'rtl.createClass(this, "TObject", null, function () {',
  29787. ' this.$init = function () {',
  29788. ' };',
  29789. ' this.$final = function () {',
  29790. ' };',
  29791. ' var $r = this.$rtti;',
  29792. ' $r.addMethod("DoIt", 0, []);',
  29793. '});',
  29794. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  29795. ' this.DoIt = function () {',
  29796. ' };',
  29797. '});',
  29798. '']),
  29799. LinesToStr([ // $mod.$main
  29800. '']));
  29801. end;
  29802. procedure TTestModule.TestRTTI_ReintroduceMethod;
  29803. begin
  29804. WithTypeInfo:=true;
  29805. StartProgram(false);
  29806. Add([
  29807. 'type',
  29808. ' TObject = class',
  29809. ' published',
  29810. ' procedure DoIt;',
  29811. ' end;',
  29812. ' TSky = class',
  29813. ' published',
  29814. ' procedure DoIt; reintroduce;',
  29815. ' end;',
  29816. 'procedure TObject.DoIt; begin end;',
  29817. 'procedure TSky.DoIt;',
  29818. 'begin',
  29819. ' inherited DoIt;',
  29820. 'end;',
  29821. 'begin']);
  29822. ConvertProgram;
  29823. CheckSource('TestRTTI_ReintroduceMethod',
  29824. LinesToStr([ // statements
  29825. 'rtl.createClass(this, "TObject", null, function () {',
  29826. ' this.$init = function () {',
  29827. ' };',
  29828. ' this.$final = function () {',
  29829. ' };',
  29830. ' this.DoIt = function () {',
  29831. ' };',
  29832. ' var $r = this.$rtti;',
  29833. ' $r.addMethod("DoIt", 0, []);',
  29834. '});',
  29835. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  29836. ' this.DoIt = function () {',
  29837. ' $mod.TObject.DoIt.call(this);',
  29838. ' };',
  29839. ' var $r = this.$rtti;',
  29840. ' $r.addMethod("DoIt", 0, []);',
  29841. '});',
  29842. '']),
  29843. LinesToStr([ // $mod.$main
  29844. '']));
  29845. end;
  29846. procedure TTestModule.TestRTTI_OverloadProperty;
  29847. begin
  29848. WithTypeInfo:=true;
  29849. StartProgram(false);
  29850. Add('type');
  29851. Add(' TObject = class');
  29852. Add(' protected');
  29853. Add(' FFlag: longint;');
  29854. Add(' published');
  29855. Add(' property Flag: longint read fflag;');
  29856. Add(' end;');
  29857. Add(' TSky = class');
  29858. Add(' published');
  29859. Add(' property FLAG: longint write fflag;');
  29860. Add(' end;');
  29861. Add('begin');
  29862. ConvertProgram;
  29863. CheckSource('TestRTTI_OverrideMethod',
  29864. LinesToStr([ // statements
  29865. 'rtl.createClass(this, "TObject", null, function () {',
  29866. ' this.$init = function () {',
  29867. ' this.FFlag = 0;',
  29868. ' };',
  29869. ' this.$final = function () {',
  29870. ' };',
  29871. ' var $r = this.$rtti;',
  29872. ' $r.addProperty("Flag", 0, rtl.longint, "FFlag", "");',
  29873. '});',
  29874. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  29875. ' var $r = this.$rtti;',
  29876. ' $r.addProperty("Flag", 0, rtl.longint, "", "FFlag");',
  29877. '});',
  29878. '']),
  29879. LinesToStr([ // $mod.$main
  29880. '']));
  29881. end;
  29882. procedure TTestModule.TestRTTI_ClassForward;
  29883. begin
  29884. WithTypeInfo:=true;
  29885. StartProgram(false);
  29886. Add('type');
  29887. Add(' TObject = class end;');
  29888. Add(' tbridge = class;');
  29889. Add(' TProc = function: tbridge;');
  29890. Add(' TOger = class');
  29891. Add(' published');
  29892. Add(' FBridge: tbridge;');
  29893. Add(' procedure SetBridge(Value: tbridge); virtual; abstract;');
  29894. Add(' property Bridge: tbridge read fbridge write setbridge;');
  29895. Add(' end;');
  29896. Add(' TBridge = class');
  29897. Add(' FOger: toger;');
  29898. Add(' end;');
  29899. Add('var p: Pointer;');
  29900. Add(' b: tbridge;');
  29901. Add('begin');
  29902. Add(' p:=typeinfo(tbridge);');
  29903. Add(' p:=typeinfo(b);');
  29904. ConvertProgram;
  29905. CheckSource('TestRTTI_ClassForward',
  29906. LinesToStr([ // statements
  29907. 'rtl.createClass(this, "TObject", null, function () {',
  29908. ' this.$init = function () {',
  29909. ' };',
  29910. ' this.$final = function () {',
  29911. ' };',
  29912. '});',
  29913. 'this.$rtti.$Class("TBridge");',
  29914. 'this.$rtti.$ProcVar("TProc", {',
  29915. ' procsig: rtl.newTIProcSig([], this.$rtti["TBridge"])',
  29916. '});',
  29917. 'rtl.createClass(this, "TOger", this.TObject, function () {',
  29918. ' this.$init = function () {',
  29919. ' $mod.TObject.$init.call(this);',
  29920. ' this.FBridge = null;',
  29921. ' };',
  29922. ' this.$final = function () {',
  29923. ' this.FBridge = undefined;',
  29924. ' $mod.TObject.$final.call(this);',
  29925. ' };',
  29926. ' var $r = this.$rtti;',
  29927. ' $r.addField("FBridge", $mod.$rtti["TBridge"]);',
  29928. ' $r.addMethod("SetBridge", 0, [["Value", $mod.$rtti["TBridge"]]]);',
  29929. ' $r.addProperty("Bridge", 2, $mod.$rtti["TBridge"], "FBridge", "SetBridge");',
  29930. '});',
  29931. 'rtl.createClass(this, "TBridge", this.TObject, function () {',
  29932. ' this.$init = function () {',
  29933. ' $mod.TObject.$init.call(this);',
  29934. ' this.FOger = null;',
  29935. ' };',
  29936. ' this.$final = function () {',
  29937. ' this.FOger = undefined;',
  29938. ' $mod.TObject.$final.call(this);',
  29939. ' };',
  29940. '});',
  29941. 'this.p = null;',
  29942. 'this.b = null;',
  29943. '']),
  29944. LinesToStr([ // $mod.$main
  29945. '$mod.p = $mod.$rtti["TBridge"];',
  29946. '$mod.p = $mod.b.$rtti;',
  29947. '']));
  29948. end;
  29949. procedure TTestModule.TestRTTI_ClassOf;
  29950. begin
  29951. WithTypeInfo:=true;
  29952. StartProgram(false);
  29953. Add('type');
  29954. Add(' TClass = class of tobject;');
  29955. Add(' TProcA = function: TClass;');
  29956. Add(' TObject = class');
  29957. Add(' published');
  29958. Add(' C: tclass;');
  29959. Add(' end;');
  29960. Add(' tfox = class;');
  29961. Add(' TBird = class end;');
  29962. Add(' TBirds = class of tbird;');
  29963. Add(' TFox = class end;');
  29964. Add(' TFoxes = class of tfox;');
  29965. Add(' TCows = class of TCow;');
  29966. Add(' TCow = class;');
  29967. Add(' TCow = class end;');
  29968. Add('begin');
  29969. ConvertProgram;
  29970. CheckSource('TestRTTI_ClassOf',
  29971. LinesToStr([ // statements
  29972. 'this.$rtti.$Class("TObject");',
  29973. 'this.$rtti.$ClassRef("TClass", {',
  29974. ' instancetype: this.$rtti["TObject"]',
  29975. '});',
  29976. 'this.$rtti.$ProcVar("TProcA", {',
  29977. ' procsig: rtl.newTIProcSig([], this.$rtti["TClass"])',
  29978. '});',
  29979. 'rtl.createClass(this, "TObject", null, function () {',
  29980. ' this.$init = function () {',
  29981. ' this.C = null;',
  29982. ' };',
  29983. ' this.$final = function () {',
  29984. ' this.C = undefined;',
  29985. ' };',
  29986. ' var $r = this.$rtti;',
  29987. ' $r.addField("C", $mod.$rtti["TClass"]);',
  29988. '});',
  29989. 'this.$rtti.$Class("TFox");',
  29990. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  29991. '});',
  29992. 'this.$rtti.$ClassRef("TBirds", {',
  29993. ' instancetype: this.$rtti["TBird"]',
  29994. '});',
  29995. 'rtl.createClass(this, "TFox", this.TObject, function () {',
  29996. '});',
  29997. 'this.$rtti.$ClassRef("TFoxes", {',
  29998. ' instancetype: this.$rtti["TFox"]',
  29999. '});',
  30000. 'this.$rtti.$Class("TCow");',
  30001. 'this.$rtti.$ClassRef("TCows", {',
  30002. ' instancetype: this.$rtti["TCow"]',
  30003. '});',
  30004. 'rtl.createClass(this, "TCow", this.TObject, function () {',
  30005. '});',
  30006. '']),
  30007. LinesToStr([ // $mod.$main
  30008. '']));
  30009. end;
  30010. procedure TTestModule.TestRTTI_Record;
  30011. begin
  30012. WithTypeInfo:=true;
  30013. StartProgram(false);
  30014. Add('type');
  30015. Add(' integer = longint;');
  30016. Add(' TPoint = record');
  30017. Add(' x,y: integer;');
  30018. Add(' end;');
  30019. Add('var p: pointer;');
  30020. Add(' r: tpoint;');
  30021. Add('begin');
  30022. Add(' p:=typeinfo(tpoint);');
  30023. Add(' p:=typeinfo(r);');
  30024. Add(' p:=typeinfo(r.x);');
  30025. ConvertProgram;
  30026. CheckSource('TestRTTI_Record',
  30027. LinesToStr([ // statements
  30028. 'rtl.recNewT(this, "TPoint", function () {',
  30029. ' this.x = 0;',
  30030. ' this.y = 0;',
  30031. ' this.$eq = function (b) {',
  30032. ' return (this.x === b.x) && (this.y === b.y);',
  30033. ' };',
  30034. ' this.$assign = function (s) {',
  30035. ' this.x = s.x;',
  30036. ' this.y = s.y;',
  30037. ' return this;',
  30038. ' };',
  30039. ' var $r = $mod.$rtti.$Record("TPoint", {});',
  30040. ' $r.addField("x", rtl.longint);',
  30041. ' $r.addField("y", rtl.longint);',
  30042. '});',
  30043. 'this.p = null;',
  30044. 'this.r = this.TPoint.$new();',
  30045. '']),
  30046. LinesToStr([ // $mod.$main
  30047. '$mod.p = $mod.$rtti["TPoint"];',
  30048. '$mod.p = $mod.$rtti["TPoint"];',
  30049. '$mod.p = rtl.longint;',
  30050. '']));
  30051. end;
  30052. procedure TTestModule.TestRTTI_RecordAnonymousArray;
  30053. begin
  30054. WithTypeInfo:=true;
  30055. StartProgram(false);
  30056. Add('type');
  30057. Add(' TFloatRec = record');
  30058. Add(' c,d: array of char;');
  30059. // Add(' i: array of array of longint;');
  30060. Add(' end;');
  30061. Add('var p: pointer;');
  30062. Add(' r: tfloatrec;');
  30063. Add('begin');
  30064. Add(' p:=typeinfo(tfloatrec);');
  30065. Add(' p:=typeinfo(r);');
  30066. Add(' p:=typeinfo(r.d);');
  30067. ConvertProgram;
  30068. CheckSource('TestRTTI_Record',
  30069. LinesToStr([ // statements
  30070. 'rtl.recNewT(this, "TFloatRec", function () {',
  30071. ' this.$new = function () {',
  30072. ' var r = Object.create(this);',
  30073. ' r.c = [];',
  30074. ' r.d = [];',
  30075. ' return r;',
  30076. ' };',
  30077. ' this.$eq = function (b) {',
  30078. ' return (this.c === b.c) && (this.d === b.d);',
  30079. ' };',
  30080. ' this.$assign = function (s) {',
  30081. ' this.c = rtl.arrayRef(s.c);',
  30082. ' this.d = rtl.arrayRef(s.d);',
  30083. ' return this;',
  30084. ' };',
  30085. ' var $r = $mod.$rtti.$Record("TFloatRec", {});',
  30086. ' $mod.$rtti.$DynArray("TFloatRec.d$a", {',
  30087. ' eltype: rtl.char',
  30088. ' });',
  30089. ' $r.addField("c", $mod.$rtti["TFloatRec.d$a"]);',
  30090. ' $r.addField("d", $mod.$rtti["TFloatRec.d$a"]);',
  30091. '});',
  30092. 'this.p = null;',
  30093. 'this.r = this.TFloatRec.$new();',
  30094. '']),
  30095. LinesToStr([ // $mod.$main
  30096. '$mod.p = $mod.$rtti["TFloatRec"];',
  30097. '$mod.p = $mod.$rtti["TFloatRec"];',
  30098. '$mod.p = $mod.$rtti["TFloatRec.d$a"];',
  30099. '']));
  30100. end;
  30101. procedure TTestModule.TestRTTI_Record_ClassVarType;
  30102. begin
  30103. WithTypeInfo:=true;
  30104. StartProgram(false);
  30105. Add([
  30106. '{$modeswitch AdvancedRecords}',
  30107. 'type',
  30108. ' TPoint = record',
  30109. ' type TProc = procedure(w: word);',
  30110. ' class var p: TProc;',
  30111. ' end;',
  30112. 'begin',
  30113. '']);
  30114. ConvertProgram;
  30115. CheckSource('TestRTTI_Record_ClassVarType',
  30116. LinesToStr([ // statements
  30117. 'rtl.recNewT(this, "TPoint", function () {',
  30118. ' $mod.$rtti.$ProcVar("TPoint.TProc", {',
  30119. ' procsig: rtl.newTIProcSig([["w", rtl.word]])',
  30120. ' });',
  30121. ' this.p = null;',
  30122. ' this.$eq = function (b) {',
  30123. ' return true;',
  30124. ' };',
  30125. ' this.$assign = function (s) {',
  30126. ' return this;',
  30127. ' };',
  30128. ' var $r = $mod.$rtti.$Record("TPoint", {});',
  30129. ' $r.addField("p", $mod.$rtti["TPoint.TProc"]);',
  30130. '}, true);',
  30131. '']),
  30132. LinesToStr([ // $mod.$main
  30133. '']));
  30134. end;
  30135. procedure TTestModule.TestRTTI_LocalTypes;
  30136. begin
  30137. WithTypeInfo:=true;
  30138. StartProgram(false);
  30139. Add([
  30140. 'procedure DoIt;',
  30141. 'type',
  30142. ' integer = longint;',
  30143. ' TPoint = record',
  30144. ' x,y: integer;',
  30145. ' end;',
  30146. 'var p: TPoint;',
  30147. 'begin',
  30148. 'end;',
  30149. 'begin']);
  30150. ConvertProgram;
  30151. CheckSource('TestRTTI_LocalTypes',
  30152. LinesToStr([ // statements
  30153. 'var TPoint = rtl.recNewT(null, "", function () {',
  30154. ' this.x = 0;',
  30155. ' this.y = 0;',
  30156. ' this.$eq = function (b) {',
  30157. ' return (this.x === b.x) && (this.y === b.y);',
  30158. ' };',
  30159. ' this.$assign = function (s) {',
  30160. ' this.x = s.x;',
  30161. ' this.y = s.y;',
  30162. ' return this;',
  30163. ' };',
  30164. '});',
  30165. 'this.DoIt = function () {',
  30166. ' var p = TPoint.$new();',
  30167. '};',
  30168. '']),
  30169. LinesToStr([ // $mod.$main
  30170. '']));
  30171. end;
  30172. procedure TTestModule.TestRTTI_TypeInfo_BaseTypes;
  30173. begin
  30174. WithTypeInfo:=true;
  30175. StartProgram(false);
  30176. Add([
  30177. 'type',
  30178. ' TCaption = string;',
  30179. ' TYesNo = boolean;',
  30180. ' TLetter = char;',
  30181. ' TFloat = double;',
  30182. ' TPtr = pointer;',
  30183. ' TShortInt = shortint;',
  30184. ' TByte = byte;',
  30185. ' TSmallInt = smallint;',
  30186. ' TWord = word;',
  30187. ' TInt32 = longint;',
  30188. ' TDWord = longword;',
  30189. ' TValue = jsvalue;',
  30190. 'var p: TPtr;',
  30191. 'begin',
  30192. ' p:=typeinfo(string);',
  30193. ' p:=typeinfo(tcaption);',
  30194. ' p:=typeinfo(boolean);',
  30195. ' p:=typeinfo(tyesno);',
  30196. ' p:=typeinfo(char);',
  30197. ' p:=typeinfo(tletter);',
  30198. ' p:=typeinfo(double);',
  30199. ' p:=typeinfo(tfloat);',
  30200. ' p:=typeinfo(pointer);',
  30201. ' p:=typeinfo(tptr);',
  30202. ' p:=typeinfo(shortint);',
  30203. ' p:=typeinfo(tshortint);',
  30204. ' p:=typeinfo(byte);',
  30205. ' p:=typeinfo(tbyte);',
  30206. ' p:=typeinfo(smallint);',
  30207. ' p:=typeinfo(tsmallint);',
  30208. ' p:=typeinfo(word);',
  30209. ' p:=typeinfo(tword);',
  30210. ' p:=typeinfo(longword);',
  30211. ' p:=typeinfo(tdword);',
  30212. ' p:=typeinfo(jsvalue);',
  30213. ' p:=typeinfo(tvalue);',
  30214. '']);
  30215. ConvertProgram;
  30216. CheckSource('TestRTTI_TypeInfo_BaseTypes',
  30217. LinesToStr([ // statements
  30218. 'this.p = null;',
  30219. '']),
  30220. LinesToStr([ // $mod.$main
  30221. '$mod.p = rtl.string;',
  30222. '$mod.p = rtl.string;',
  30223. '$mod.p = rtl.boolean;',
  30224. '$mod.p = rtl.boolean;',
  30225. '$mod.p = rtl.char;',
  30226. '$mod.p = rtl.char;',
  30227. '$mod.p = rtl.double;',
  30228. '$mod.p = rtl.double;',
  30229. '$mod.p = rtl.pointer;',
  30230. '$mod.p = rtl.pointer;',
  30231. '$mod.p = rtl.shortint;',
  30232. '$mod.p = rtl.shortint;',
  30233. '$mod.p = rtl.byte;',
  30234. '$mod.p = rtl.byte;',
  30235. '$mod.p = rtl.smallint;',
  30236. '$mod.p = rtl.smallint;',
  30237. '$mod.p = rtl.word;',
  30238. '$mod.p = rtl.word;',
  30239. '$mod.p = rtl.longword;',
  30240. '$mod.p = rtl.longword;',
  30241. '$mod.p = rtl.jsvalue;',
  30242. '$mod.p = rtl.jsvalue;',
  30243. '']));
  30244. end;
  30245. procedure TTestModule.TestRTTI_TypeInfo_Type_BaseTypes;
  30246. begin
  30247. WithTypeInfo:=true;
  30248. StartProgram(false);
  30249. Add([
  30250. 'type',
  30251. ' TCaption = type string;',
  30252. ' TYesNo = type boolean;',
  30253. ' TLetter = type char;',
  30254. ' TFloat = type double;',
  30255. ' TPtr = type pointer;',
  30256. ' TShortInt = type shortint;',
  30257. ' TByte = type byte;',
  30258. ' TSmallInt = type smallint;',
  30259. ' TWord = type word;',
  30260. ' TInt32 = type longint;',
  30261. ' TDWord = type longword;',
  30262. ' TValue = type jsvalue;',
  30263. ' TAliasValue = type TValue;',
  30264. 'var',
  30265. ' p: TPtr;',
  30266. ' a: TAliasValue;',
  30267. 'begin',
  30268. ' p:=typeinfo(tcaption);',
  30269. ' p:=typeinfo(tyesno);',
  30270. ' p:=typeinfo(tletter);',
  30271. ' p:=typeinfo(tfloat);',
  30272. ' p:=typeinfo(tptr);',
  30273. ' p:=typeinfo(tshortint);',
  30274. ' p:=typeinfo(tbyte);',
  30275. ' p:=typeinfo(tsmallint);',
  30276. ' p:=typeinfo(tword);',
  30277. ' p:=typeinfo(tdword);',
  30278. ' p:=typeinfo(tvalue);',
  30279. ' p:=typeinfo(taliasvalue);',
  30280. ' p:=typeinfo(a);',
  30281. '']);
  30282. ConvertProgram;
  30283. CheckSource('TestRTTI_TypeInfo_Type_BaseTypes',
  30284. LinesToStr([ // statements
  30285. 'this.$rtti.$inherited("TCaption", rtl.string, {});',
  30286. 'this.$rtti.$inherited("TYesNo", rtl.boolean, {});',
  30287. 'this.$rtti.$inherited("TLetter", rtl.char, {});',
  30288. 'this.$rtti.$inherited("TFloat", rtl.double, {});',
  30289. 'this.$rtti.$inherited("TPtr", rtl.pointer, {});',
  30290. 'this.$rtti.$inherited("TShortInt", rtl.shortint, {});',
  30291. 'this.$rtti.$inherited("TByte", rtl.byte, {});',
  30292. 'this.$rtti.$inherited("TSmallInt", rtl.smallint, {});',
  30293. 'this.$rtti.$inherited("TWord", rtl.word, {});',
  30294. 'this.$rtti.$inherited("TInt32", rtl.longint, {});',
  30295. 'this.$rtti.$inherited("TDWord", rtl.longword, {});',
  30296. 'this.$rtti.$inherited("TValue", rtl.jsvalue, {});',
  30297. 'this.$rtti.$inherited("TAliasValue", this.$rtti["TValue"], {});',
  30298. 'this.p = null;',
  30299. 'this.a = undefined;',
  30300. '']),
  30301. LinesToStr([ // $mod.$main
  30302. '$mod.p = $mod.$rtti["TCaption"];',
  30303. '$mod.p = $mod.$rtti["TYesNo"];',
  30304. '$mod.p = $mod.$rtti["TLetter"];',
  30305. '$mod.p = $mod.$rtti["TFloat"];',
  30306. '$mod.p = $mod.$rtti["TPtr"];',
  30307. '$mod.p = $mod.$rtti["TShortInt"];',
  30308. '$mod.p = $mod.$rtti["TByte"];',
  30309. '$mod.p = $mod.$rtti["TSmallInt"];',
  30310. '$mod.p = $mod.$rtti["TWord"];',
  30311. '$mod.p = $mod.$rtti["TDWord"];',
  30312. '$mod.p = $mod.$rtti["TValue"];',
  30313. '$mod.p = $mod.$rtti["TAliasValue"];',
  30314. '$mod.p = $mod.$rtti["TAliasValue"];',
  30315. '']));
  30316. end;
  30317. procedure TTestModule.TestRTTI_TypeInfo_LocalFail;
  30318. begin
  30319. WithTypeInfo:=true;
  30320. StartProgram(false);
  30321. Add('procedure DoIt;');
  30322. Add('type');
  30323. Add(' integer = longint;');
  30324. Add(' TPoint = record');
  30325. Add(' x,y: integer;');
  30326. Add(' end;');
  30327. Add('var p: pointer;');
  30328. Add('begin');
  30329. Add(' p:=typeinfo(tpoint);');
  30330. Add('end;');
  30331. Add('begin');
  30332. SetExpectedPasResolverError(sSymbolCannotBePublished,nSymbolCannotBePublished);
  30333. ConvertProgram;
  30334. end;
  30335. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses1;
  30336. begin
  30337. WithTypeInfo:=true;
  30338. StartProgram(true,[supTypeInfo]);
  30339. Add([
  30340. '{$modeswitch externalclass}',
  30341. 'type',
  30342. ' TFlag = (up,down);',
  30343. ' TFlags = set of TFlag;',
  30344. 'var',
  30345. ' ti: TTypeInfo;',
  30346. ' tiInt: TTypeInfoInteger;',
  30347. ' tiEnum: TTypeInfoEnum;',
  30348. ' tiSet: TTypeInfoSet;',
  30349. 'begin',
  30350. ' ti:=typeinfo(string);',
  30351. ' ti:=typeinfo(boolean);',
  30352. ' ti:=typeinfo(char);',
  30353. ' ti:=typeinfo(double);',
  30354. ' tiInt:=typeinfo(shortint);',
  30355. ' tiInt:=typeinfo(byte);',
  30356. ' tiInt:=typeinfo(smallint);',
  30357. ' tiInt:=typeinfo(word);',
  30358. ' tiInt:=typeinfo(longint);',
  30359. ' tiInt:=typeinfo(longword);',
  30360. ' ti:=typeinfo(jsvalue);',
  30361. ' tiEnum:=typeinfo(tflag);',
  30362. ' tiSet:=typeinfo(tflags);']);
  30363. ConvertProgram;
  30364. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses1',
  30365. LinesToStr([ // statements
  30366. 'this.TFlag = {',
  30367. ' "0": "up",',
  30368. ' up: 0,',
  30369. ' "1": "down",',
  30370. ' down: 1',
  30371. '};',
  30372. 'this.$rtti.$Enum("TFlag", {',
  30373. ' minvalue: 0,',
  30374. ' maxvalue: 1,',
  30375. ' ordtype: 1,',
  30376. ' enumtype: this.TFlag',
  30377. '});',
  30378. 'this.$rtti.$Set("TFlags", {',
  30379. ' comptype: this.$rtti["TFlag"]',
  30380. '});',
  30381. 'this.ti = null;',
  30382. 'this.tiInt = null;',
  30383. 'this.tiEnum = null;',
  30384. 'this.tiSet = null;',
  30385. '']),
  30386. LinesToStr([ // $mod.$main
  30387. '$mod.ti = rtl.string;',
  30388. '$mod.ti = rtl.boolean;',
  30389. '$mod.ti = rtl.char;',
  30390. '$mod.ti = rtl.double;',
  30391. '$mod.tiInt = rtl.shortint;',
  30392. '$mod.tiInt = rtl.byte;',
  30393. '$mod.tiInt = rtl.smallint;',
  30394. '$mod.tiInt = rtl.word;',
  30395. '$mod.tiInt = rtl.longint;',
  30396. '$mod.tiInt = rtl.longword;',
  30397. '$mod.ti = rtl.jsvalue;',
  30398. '$mod.tiEnum = $mod.$rtti["TFlag"];',
  30399. '$mod.tiSet = $mod.$rtti["TFlags"];',
  30400. '']));
  30401. end;
  30402. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses2;
  30403. begin
  30404. WithTypeInfo:=true;
  30405. StartProgram(true,[supTypeInfo]);
  30406. Add('{$modeswitch externalclass}');
  30407. Add('type');
  30408. Add(' TStaticArr = array[boolean] of string;');
  30409. Add(' TDynArr = array of string;');
  30410. Add(' TProc = procedure;');
  30411. Add(' TMethod = procedure of object;');
  30412. Add('var');
  30413. Add(' StaticArray: TStaticArr;');
  30414. Add(' tiStaticArray: TTypeInfoStaticArray;');
  30415. Add(' DynArray: TDynArr;');
  30416. Add(' tiDynArray: TTypeInfoDynArray;');
  30417. Add(' ProcVar: TProc;');
  30418. Add(' tiProcVar: TTypeInfoProcVar;');
  30419. Add(' MethodVar: TMethod;');
  30420. Add(' tiMethodVar: TTypeInfoMethodVar;');
  30421. Add('begin');
  30422. Add(' tiStaticArray:=typeinfo(StaticArray);');
  30423. Add(' tiStaticArray:=typeinfo(TStaticArr);');
  30424. Add(' tiDynArray:=typeinfo(DynArray);');
  30425. Add(' tiDynArray:=typeinfo(TDynArr);');
  30426. Add(' tiProcVar:=typeinfo(ProcVar);');
  30427. Add(' tiProcVar:=typeinfo(TProc);');
  30428. Add(' tiMethodVar:=typeinfo(MethodVar);');
  30429. Add(' tiMethodVar:=typeinfo(TMethod);');
  30430. ConvertProgram;
  30431. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses2',
  30432. LinesToStr([ // statements
  30433. 'this.$rtti.$StaticArray("TStaticArr", {',
  30434. ' dims: [2],',
  30435. ' eltype: rtl.string',
  30436. '});',
  30437. 'this.$rtti.$DynArray("TDynArr", {',
  30438. ' eltype: rtl.string',
  30439. '});',
  30440. 'this.$rtti.$ProcVar("TProc", {',
  30441. ' procsig: rtl.newTIProcSig([])',
  30442. '});',
  30443. 'this.$rtti.$MethodVar("TMethod", {',
  30444. ' procsig: rtl.newTIProcSig([]),',
  30445. ' methodkind: 0',
  30446. '});',
  30447. 'this.StaticArray = rtl.arraySetLength(null,"",2);',
  30448. 'this.tiStaticArray = null;',
  30449. 'this.DynArray = [];',
  30450. 'this.tiDynArray = null;',
  30451. 'this.ProcVar = null;',
  30452. 'this.tiProcVar = null;',
  30453. 'this.MethodVar = null;',
  30454. 'this.tiMethodVar = null;',
  30455. '']),
  30456. LinesToStr([ // $mod.$main
  30457. '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
  30458. '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
  30459. '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
  30460. '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
  30461. '$mod.tiProcVar = $mod.$rtti["TProc"];',
  30462. '$mod.tiProcVar = $mod.$rtti["TProc"];',
  30463. '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
  30464. '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
  30465. '']));
  30466. end;
  30467. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses3;
  30468. begin
  30469. WithTypeInfo:=true;
  30470. StartProgram(true,[supTypeInfo]);
  30471. Add('{$modeswitch externalclass}');
  30472. Add('type');
  30473. Add(' TRec = record end;');
  30474. // ToDo: ^TRec
  30475. Add(' TObject = class end;');
  30476. Add(' TClass = class of tobject;');
  30477. Add('var');
  30478. Add(' Rec: trec;');
  30479. Add(' tiRecord: ttypeinforecord;');
  30480. Add(' Obj: tobject;');
  30481. Add(' tiClass: ttypeinfoclass;');
  30482. Add(' aClass: tclass;');
  30483. Add(' tiClassRef: ttypeinfoclassref;');
  30484. // ToDo: ^TRec
  30485. Add(' tiPointer: ttypeinfopointer;');
  30486. Add('begin');
  30487. Add(' tirecord:=typeinfo(trec);');
  30488. Add(' tirecord:=typeinfo(trec);');
  30489. Add(' ticlass:=typeinfo(obj);');
  30490. Add(' ticlass:=typeinfo(tobject);');
  30491. Add(' ticlass:=typeinfo(aclass);');
  30492. Add(' ticlassref:=typeinfo(tclass);');
  30493. ConvertProgram;
  30494. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses3',
  30495. LinesToStr([ // statements
  30496. 'rtl.recNewT(this, "TRec", function () {',
  30497. ' this.$eq = function (b) {',
  30498. ' return true;',
  30499. ' };',
  30500. ' this.$assign = function (s) {',
  30501. ' return this;',
  30502. ' };',
  30503. ' $mod.$rtti.$Record("TRec", {});',
  30504. '});',
  30505. 'rtl.createClass(this, "TObject", null, function () {',
  30506. ' this.$init = function () {',
  30507. ' };',
  30508. ' this.$final = function () {',
  30509. ' };',
  30510. '});',
  30511. 'this.$rtti.$ClassRef("TClass", {',
  30512. ' instancetype: this.$rtti["TObject"]',
  30513. '});',
  30514. 'this.Rec = this.TRec.$new();',
  30515. 'this.tiRecord = null;',
  30516. 'this.Obj = null;',
  30517. 'this.tiClass = null;',
  30518. 'this.aClass = null;',
  30519. 'this.tiClassRef = null;',
  30520. 'this.tiPointer = null;',
  30521. '']),
  30522. LinesToStr([ // $mod.$main
  30523. '$mod.tiRecord = $mod.$rtti["TRec"];',
  30524. '$mod.tiRecord = $mod.$rtti["TRec"];',
  30525. '$mod.tiClass = $mod.Obj.$rtti;',
  30526. '$mod.tiClass = $mod.$rtti["TObject"];',
  30527. '$mod.tiClass = $mod.aClass.$rtti;',
  30528. '$mod.tiClassRef = $mod.$rtti["TClass"];',
  30529. '']));
  30530. end;
  30531. procedure TTestModule.TestRTTI_TypeInfo_FunctionClassType;
  30532. begin
  30533. WithTypeInfo:=true;
  30534. StartProgram(true,[supTypeInfo]);
  30535. Add([
  30536. '{$modeswitch externalclass}',
  30537. 'type',
  30538. ' TClass = class of tobject;',
  30539. ' TObject = class',
  30540. ' function MyClass: TClass;',
  30541. ' class function ClassType: TClass;',
  30542. ' end;',
  30543. 'function TObject.MyClass: TClass;',
  30544. 'var t: TTypeInfoClass;',
  30545. 'begin',
  30546. ' t:=TypeInfo(Self);',
  30547. ' t:=TypeInfo(Result);',
  30548. ' t:=TypeInfo(TObject);',
  30549. 'end;',
  30550. 'class function TObject.ClassType: TClass;',
  30551. 'var t: TTypeInfoClass;',
  30552. 'begin',
  30553. ' t:=TypeInfo(Self);',
  30554. ' t:=TypeInfo(Result);',
  30555. 'end;',
  30556. 'var',
  30557. ' Obj: TObject;',
  30558. ' t: TTypeInfoClass;',
  30559. 'begin',
  30560. ' t:=TypeInfo(TObject.ClassType);',
  30561. ' t:=TypeInfo(Obj.ClassType);',
  30562. ' t:=TypeInfo(Obj.MyClass);',
  30563. '']);
  30564. ConvertProgram;
  30565. CheckSource('TestRTTI_TypeInfo_FunctionClassType',
  30566. LinesToStr([ // statements
  30567. 'this.$rtti.$Class("TObject");',
  30568. 'this.$rtti.$ClassRef("TClass", {',
  30569. ' instancetype: this.$rtti["TObject"]',
  30570. '});',
  30571. 'rtl.createClass(this, "TObject", null, function () {',
  30572. ' this.$init = function () {',
  30573. ' };',
  30574. ' this.$final = function () {',
  30575. ' };',
  30576. ' this.MyClass = function () {',
  30577. ' var Result = null;',
  30578. ' var t = null;',
  30579. ' t = this.$rtti;',
  30580. ' t = Result.$rtti;',
  30581. ' t = $mod.$rtti["TObject"];',
  30582. ' return Result;',
  30583. ' };',
  30584. ' this.ClassType = function () {',
  30585. ' var Result = null;',
  30586. ' var t = null;',
  30587. ' t = this.$rtti;',
  30588. ' t = Result.$rtti;',
  30589. ' return Result;',
  30590. ' };',
  30591. '});',
  30592. 'this.Obj = null;',
  30593. 'this.t = null;',
  30594. '']),
  30595. LinesToStr([ // $mod.$main
  30596. '$mod.t = $mod.TObject.ClassType().$rtti;',
  30597. '$mod.t = $mod.Obj.$class.ClassType().$rtti;',
  30598. '$mod.t = $mod.Obj.MyClass().$rtti;',
  30599. '']));
  30600. end;
  30601. procedure TTestModule.TestRTTI_TypeInfo_MixedUnits_PointerAndClass;
  30602. begin
  30603. WithTypeInfo:=true;
  30604. AddModuleWithIntfImplSrc('typinfo.pas',
  30605. LinesToStr([
  30606. '{$modeswitch externalclass}',
  30607. 'type',
  30608. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  30609. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo) end;',
  30610. '']),
  30611. '');
  30612. AddModuleWithIntfImplSrc('unit2.pas',
  30613. LinesToStr([
  30614. 'uses typinfo;',
  30615. 'type PTypeInfo = TTypeInfo;', // delphi compatibility code
  30616. 'procedure DoPtr(p: PTypeInfo);',
  30617. 'procedure DoInfo(t: TTypeInfo);',
  30618. 'procedure DoInt(t: TTypeInfoInteger);',
  30619. '']),
  30620. LinesToStr([
  30621. 'procedure DoPtr(p: PTypeInfo);',
  30622. 'begin end;',
  30623. 'procedure DoInfo(t: TTypeInfo);',
  30624. 'begin end;',
  30625. 'procedure DoInt(t: TTypeInfoInteger);',
  30626. 'begin end;',
  30627. '']));
  30628. StartUnit(true);
  30629. Add([
  30630. 'interface',
  30631. 'uses unit2;', // does not use unit typinfo
  30632. 'implementation',
  30633. 'var',
  30634. ' i: byte;',
  30635. ' p: pointer;',
  30636. ' t: PTypeInfo;',
  30637. 'initialization',
  30638. ' p:=typeinfo(i);',
  30639. ' t:=typeinfo(i);',
  30640. ' if p=t then ;',
  30641. ' if p=typeinfo(i) then ;',
  30642. ' if typeinfo(i)=p then ;',
  30643. ' if t=typeinfo(i) then ;',
  30644. ' if typeinfo(i)=t then ;',
  30645. ' DoPtr(p);',
  30646. ' DoPtr(t);',
  30647. ' DoPtr(typeinfo(i));',
  30648. ' DoInfo(p);',
  30649. ' DoInfo(t);',
  30650. ' DoInfo(typeinfo(i));',
  30651. ' DoInt(typeinfo(i));',
  30652. '']);
  30653. ConvertUnit;
  30654. CheckSource('TestRTTI_TypeInfo_MixedUnits_PointerAndClass',
  30655. LinesToStr([ // statements
  30656. 'var $impl = $mod.$impl;',
  30657. '']),
  30658. LinesToStr([ // this.$init
  30659. '$impl.p = rtl.byte;',
  30660. '$impl.t = rtl.byte;',
  30661. 'if ($impl.p === $impl.t) ;',
  30662. 'if ($impl.p === rtl.byte) ;',
  30663. 'if (rtl.byte === $impl.p) ;',
  30664. 'if ($impl.t === rtl.byte) ;',
  30665. 'if (rtl.byte === $impl.t) ;',
  30666. 'pas.unit2.DoPtr($impl.p);',
  30667. 'pas.unit2.DoPtr($impl.t);',
  30668. 'pas.unit2.DoPtr(rtl.byte);',
  30669. 'pas.unit2.DoInfo($impl.p);',
  30670. 'pas.unit2.DoInfo($impl.t);',
  30671. 'pas.unit2.DoInfo(rtl.byte);',
  30672. 'pas.unit2.DoInt(rtl.byte);',
  30673. '']),
  30674. LinesToStr([ // implementation
  30675. '$impl.i = 0;',
  30676. '$impl.p = null;',
  30677. '$impl.t = null;',
  30678. '']) );
  30679. end;
  30680. procedure TTestModule.TestRTTI_Interface_Corba;
  30681. begin
  30682. WithTypeInfo:=true;
  30683. StartProgram(true,[supTypeInfo]);
  30684. Add([
  30685. '{$interfaces corba}',
  30686. '{$modeswitch externalclass}',
  30687. 'type',
  30688. ' IUnknown = interface',
  30689. ' end;',
  30690. ' IBird = interface',
  30691. ' function GetItem: longint;',
  30692. ' procedure SetItem(Value: longint);',
  30693. ' property Item: longint read GetItem write SetItem;',
  30694. ' end;',
  30695. 'procedure DoIt(t: TTypeInfoInterface); begin end;',
  30696. 'var',
  30697. ' i: IBird;',
  30698. ' t: TTypeInfoInterface;',
  30699. 'begin',
  30700. ' t:=TypeInfo(IBird);',
  30701. ' t:=TypeInfo(i);',
  30702. ' DoIt(t);',
  30703. ' DoIt(TypeInfo(IBird));',
  30704. '']);
  30705. ConvertProgram;
  30706. CheckSource('TestRTTI_Interface_Corba',
  30707. LinesToStr([ // statements
  30708. 'rtl.createInterface(',
  30709. ' this,',
  30710. ' "IUnknown",',
  30711. ' "{B92D5841-758A-322B-B800-000000000000}",',
  30712. ' [],',
  30713. ' null,',
  30714. ' function () {',
  30715. ' }',
  30716. ');',
  30717. 'rtl.createInterface(',
  30718. ' this,',
  30719. ' "IBird",',
  30720. ' "{D32D5841-6264-3AE3-A2C9-B91CE922C9B9}",',
  30721. ' ["GetItem", "SetItem"],',
  30722. ' null,',
  30723. ' function () {',
  30724. ' var $r = this.$rtti;',
  30725. ' $r.addMethod("GetItem", 1, [], rtl.longint);',
  30726. ' $r.addMethod("SetItem", 0, [["Value", rtl.longint]]);',
  30727. ' $r.addProperty("Item", 3, rtl.longint, "GetItem", "SetItem");',
  30728. ' }',
  30729. ');',
  30730. 'this.DoIt = function (t) {',
  30731. '}; ',
  30732. 'this.i = null;',
  30733. 'this.t = null;',
  30734. '']),
  30735. LinesToStr([ // $mod.$main
  30736. '$mod.t = $mod.$rtti["IBird"];',
  30737. '$mod.t = $mod.i.$rtti;',
  30738. '$mod.DoIt($mod.t);',
  30739. '$mod.DoIt($mod.$rtti["IBird"]);',
  30740. '']));
  30741. end;
  30742. procedure TTestModule.TestRTTI_Interface_COM;
  30743. begin
  30744. WithTypeInfo:=true;
  30745. StartProgram(true,[supTypeInfo]);
  30746. Add([
  30747. '{$interfaces com}',
  30748. '{$modeswitch externalclass}',
  30749. 'type',
  30750. ' TGuid = record end;',
  30751. ' integer = longint;',
  30752. ' IUnknown = interface',
  30753. ' function QueryInterface(const iid: TGuid; out obj): Integer;',
  30754. ' function _AddRef: Integer;',
  30755. ' function _Release: Integer;',
  30756. ' end;',
  30757. ' IBird = interface',
  30758. ' function GetItem: longint;',
  30759. ' procedure SetItem(Value: longint);',
  30760. ' property Item: longint read GetItem write SetItem;',
  30761. ' end;',
  30762. 'var',
  30763. ' i: IBird;',
  30764. ' t: TTypeInfoInterface;',
  30765. 'begin',
  30766. ' t:=TypeInfo(IBird);',
  30767. ' t:=TypeInfo(i);',
  30768. '']);
  30769. ConvertProgram;
  30770. CheckSource('TestRTTI_Interface_COM',
  30771. LinesToStr([ // statements
  30772. 'rtl.recNewT(this, "TGuid", function () {',
  30773. ' this.$eq = function (b) {',
  30774. ' return true;',
  30775. ' };',
  30776. ' this.$assign = function (s) {',
  30777. ' return this;',
  30778. ' };',
  30779. ' $mod.$rtti.$Record("TGuid", {});',
  30780. '});',
  30781. 'rtl.createInterface(',
  30782. ' this,',
  30783. ' "IUnknown",',
  30784. ' "{D7ADB00D-1A9B-3EDC-B123-730E661DDFA9}",',
  30785. ' ["QueryInterface", "_AddRef", "_Release"],',
  30786. ' null,',
  30787. ' function () {',
  30788. ' this.$kind = "com";',
  30789. ' var $r = this.$rtti;',
  30790. ' $r.addMethod("QueryInterface", 1, [["iid", $mod.$rtti["TGuid"], 2], ["obj", null, 4]], rtl.longint);',
  30791. ' $r.addMethod("_AddRef", 1, [], rtl.longint);',
  30792. ' $r.addMethod("_Release", 1, [], rtl.longint);',
  30793. ' }',
  30794. ');',
  30795. 'rtl.createInterface(',
  30796. ' this,',
  30797. ' "IBird",',
  30798. ' "{9CC77572-0E45-3594-9A88-9E8D865C9E0A}",',
  30799. ' ["GetItem", "SetItem"],',
  30800. ' this.IUnknown,',
  30801. ' function () {',
  30802. ' var $r = this.$rtti;',
  30803. ' $r.addMethod("GetItem", 1, [], rtl.longint);',
  30804. ' $r.addMethod("SetItem", 0, [["Value", rtl.longint]]);',
  30805. ' $r.addProperty("Item", 3, rtl.longint, "GetItem", "SetItem");',
  30806. ' }',
  30807. ');',
  30808. 'this.i = null;',
  30809. 'this.t = null;',
  30810. '']),
  30811. LinesToStr([ // $mod.$main
  30812. '$mod.t = $mod.$rtti["IBird"];',
  30813. '$mod.t = $mod.i.$rtti;',
  30814. '']));
  30815. end;
  30816. procedure TTestModule.TestRTTI_ClassHelper;
  30817. begin
  30818. WithTypeInfo:=true;
  30819. StartProgram(true,[supTypeInfo]);
  30820. Add([
  30821. '{$interfaces com}',
  30822. '{$modeswitch externalclass}',
  30823. 'type',
  30824. ' TObject = class',
  30825. ' end;',
  30826. ' THelper = class helper for TObject',
  30827. ' published',
  30828. ' function GetItem: longint;',
  30829. ' property Item: longint read GetItem;',
  30830. ' end;',
  30831. 'function THelper.GetItem: longint;',
  30832. 'begin',
  30833. 'end;',
  30834. 'var',
  30835. ' t: TTypeInfoHelper;',
  30836. 'begin',
  30837. ' t:=TypeInfo(THelper);',
  30838. '']);
  30839. ConvertProgram;
  30840. CheckSource('TestRTTI_ClassHelper',
  30841. LinesToStr([ // statements
  30842. 'rtl.createClass(this, "TObject", null, function () {',
  30843. ' this.$init = function () {',
  30844. ' };',
  30845. ' this.$final = function () {',
  30846. ' };',
  30847. '});',
  30848. 'rtl.createHelper(this, "THelper", null, function () {',
  30849. ' this.GetItem = function () {',
  30850. ' var Result = 0;',
  30851. ' return Result;',
  30852. ' };',
  30853. ' var $r = this.$rtti;',
  30854. ' $r.addMethod("GetItem", 1, [], rtl.longint);',
  30855. ' $r.addProperty("Item", 1, rtl.longint, "GetItem", "");',
  30856. '});',
  30857. 'this.t = null;',
  30858. '']),
  30859. LinesToStr([ // $mod.$main
  30860. '$mod.t = $mod.$rtti["THelper"];',
  30861. '']));
  30862. end;
  30863. procedure TTestModule.TestRTTI_ExternalClass;
  30864. begin
  30865. WithTypeInfo:=true;
  30866. StartProgram(true,[supTypeInfo]);
  30867. Add([
  30868. '{$modeswitch externalclass}',
  30869. 'type',
  30870. ' TJSObject = class external name ''Object''',
  30871. ' end;',
  30872. ' TJSArray = class external name ''Array'' (TJSObject)',
  30873. ' end;',
  30874. 'var',
  30875. ' p: Pointer;',
  30876. ' tc: TTypeInfoExtClass;',
  30877. 'begin',
  30878. ' p:=typeinfo(TJSArray);']);
  30879. ConvertProgram;
  30880. CheckSource('TestRTTI_ExternalClass',
  30881. LinesToStr([ // statements
  30882. 'this.$rtti.$ExtClass("TJSObject", {',
  30883. ' jsclass: "Object"',
  30884. '});',
  30885. 'this.$rtti.$ExtClass("TJSArray", {',
  30886. ' ancestor: this.$rtti["TJSObject"],',
  30887. ' jsclass: "Array"',
  30888. '});',
  30889. 'this.p = null;',
  30890. 'this.tc = null;',
  30891. '']),
  30892. LinesToStr([ // $mod.$main
  30893. '$mod.p = $mod.$rtti["TJSArray"];',
  30894. '']));
  30895. end;
  30896. procedure TTestModule.TestRTTI_Unit;
  30897. begin
  30898. WithTypeInfo:=true;
  30899. AddModuleWithIntfImplSrc('unit2.pas',
  30900. LinesToStr([
  30901. '{$mode delphi}',
  30902. 'type',
  30903. ' TWordArray = array of word;',
  30904. ' TArray<T> = array of T;',
  30905. '']),
  30906. '');
  30907. StartUnit(true,[supTypeInfo,supTInterfacedObject]);
  30908. Add([
  30909. '{$mode delphi}',
  30910. 'interface',
  30911. 'uses unit2;',
  30912. 'type',
  30913. ' IBird = interface',
  30914. ' function Swoop: TWordArray;',
  30915. ' function Glide: TArray<word>;',
  30916. ' end;',
  30917. 'procedure Fly;',
  30918. 'implementation',
  30919. 'procedure Fly;',
  30920. 'var',
  30921. ' ta: tTypeInfoDynArray;',
  30922. ' ti: tTypeInfoInterface;',
  30923. 'begin',
  30924. ' ta:=typeinfo(TWordArray);',
  30925. ' ta:=typeinfo(TArray<word>);',
  30926. ' ti:=typeinfo(IBird);',
  30927. 'end;',
  30928. '']);
  30929. ConvertUnit;
  30930. CheckSource('TestRTTI_ExternalClass',
  30931. LinesToStr([ // statements
  30932. 'rtl.createInterface(',
  30933. ' this,',
  30934. ' "IBird",',
  30935. ' "{3B98AAAC-6116-3E17-AA85-F16786D85B09}",',
  30936. ' ["Swoop", "Glide"],',
  30937. ' pas.system.IUnknown,',
  30938. ' function () {',
  30939. ' var $r = this.$rtti;',
  30940. ' $r.addMethod("Swoop", 1, [], pas.unit2.$rtti["TWordArray"]);',
  30941. ' $r.addMethod("Glide", 1, [], pas.unit2.$rtti["TArray<System.Word>"]);',
  30942. ' }',
  30943. ');',
  30944. 'this.Fly = function () {',
  30945. ' var ta = null;',
  30946. ' var ti = null;',
  30947. ' ta = pas.unit2.$rtti["TWordArray"];',
  30948. ' ta = pas.unit2.$rtti["TArray<System.Word>"];',
  30949. ' ti = $mod.$rtti["IBird"];',
  30950. '};',
  30951. '']),
  30952. LinesToStr([ // $mod.$main
  30953. '']));
  30954. end;
  30955. procedure TTestModule.TestResourcestringProgram;
  30956. begin
  30957. AddModuleWithIntfImplSrc('unit2.pas',
  30958. LinesToStr([
  30959. 'resourcestring Title = ''Nice'';',
  30960. '']),
  30961. '');
  30962. StartProgram(true);
  30963. Add([
  30964. 'uses unit2;',
  30965. 'const Bar = ''bar'';',
  30966. 'resourcestring',
  30967. ' Red = ''red'';',
  30968. ' Foobar = ''fOo''+bar;',
  30969. 'var s: string;',
  30970. ' c: char;',
  30971. 'begin',
  30972. ' s:=red;',
  30973. ' s:=test1.red;',
  30974. ' s:=Title;',
  30975. ' c:=red[1];',
  30976. ' c:=test1.red[2];',
  30977. ' if red=foobar then ;',
  30978. ' if red[3]=red[4] then ;']);
  30979. ConvertProgram;
  30980. CheckSource('TestResourcestringProgram',
  30981. LinesToStr([ // statements
  30982. 'this.Bar = "bar";',
  30983. 'this.s = "";',
  30984. 'this.c = "";',
  30985. '$mod.$resourcestrings = {',
  30986. ' Red: {',
  30987. ' org: "red"',
  30988. ' },',
  30989. ' Foobar: {',
  30990. ' org: "fOobar"',
  30991. ' }',
  30992. '};',
  30993. '']),
  30994. LinesToStr([ // $mod.$main
  30995. '$mod.s = rtl.getResStr($mod, "Red");',
  30996. '$mod.s = rtl.getResStr($mod, "Red");',
  30997. '$mod.s = rtl.getResStr(pas.unit2, "Title");',
  30998. '$mod.c = rtl.getResStr($mod, "Red").charAt(0);',
  30999. '$mod.c = rtl.getResStr($mod, "Red").charAt(1);',
  31000. 'if (rtl.getResStr($mod, "Red") === rtl.getResStr($mod, "Foobar")) ;',
  31001. 'if (rtl.getResStr($mod, "Red").charAt(2) === rtl.getResStr($mod, "Red").charAt(3)) ;',
  31002. '']));
  31003. end;
  31004. procedure TTestModule.TestResourcestringUnit;
  31005. begin
  31006. AddModuleWithIntfImplSrc('unit2.pas',
  31007. LinesToStr([
  31008. 'resourcestring Title = ''Nice'';',
  31009. '']),
  31010. '');
  31011. StartUnit(true);
  31012. Add([
  31013. 'interface',
  31014. 'uses unit2;',
  31015. 'const Red = ''rEd'';',
  31016. 'resourcestring',
  31017. ' Blue = ''blue'';',
  31018. ' NotRed = ''not''+Red;',
  31019. 'var s: string;',
  31020. 'implementation',
  31021. 'resourcestring',
  31022. ' ImplGreen = ''green'';',
  31023. 'initialization',
  31024. ' s:=blue+ImplGreen;',
  31025. ' s:=test1.blue+test1.implgreen;',
  31026. ' s:=blue[1]+implgreen[2];',
  31027. ' s:=Title;',
  31028. '']);
  31029. ConvertUnit;
  31030. CheckSource('TestResourcestringUnit',
  31031. LinesToStr([ // statements
  31032. 'this.Red = "rEd";',
  31033. 'this.s = "";',
  31034. '$mod.$resourcestrings = {',
  31035. ' Blue: {',
  31036. ' org: "blue"',
  31037. ' },',
  31038. ' NotRed: {',
  31039. ' org: "notrEd"',
  31040. ' },',
  31041. ' ImplGreen: {',
  31042. ' org: "green"',
  31043. ' }',
  31044. '};',
  31045. '']),
  31046. LinesToStr([ // $mod.$main
  31047. '$mod.s = rtl.getResStr($mod, "Blue") + rtl.getResStr($mod, "ImplGreen");',
  31048. '$mod.s = rtl.getResStr($mod, "Blue") + rtl.getResStr($mod, "ImplGreen");',
  31049. '$mod.s = rtl.getResStr($mod, "Blue").charAt(0) + rtl.getResStr($mod, "ImplGreen").charAt(1);',
  31050. '$mod.s = rtl.getResStr(pas.unit2, "Title");',
  31051. '']));
  31052. end;
  31053. procedure TTestModule.TestResourcestringImplementation;
  31054. begin
  31055. StartUnit(false);
  31056. Add([
  31057. 'interface',
  31058. 'implementation',
  31059. 'resourcestring',
  31060. ' ImplRed = ''red'';']);
  31061. ConvertUnit;
  31062. CheckSource('TestResourcestringImplementation',
  31063. LinesToStr([ // intf statements
  31064. 'var $impl = $mod.$impl;']),
  31065. LinesToStr([ // $mod.$init
  31066. '']),
  31067. LinesToStr([ // impl statements
  31068. '$mod.$resourcestrings = {',
  31069. ' ImplRed: {',
  31070. ' org: "red"',
  31071. ' }',
  31072. '};',
  31073. '']));
  31074. end;
  31075. procedure TTestModule.TestAttributes_Members;
  31076. begin
  31077. WithTypeInfo:=true;
  31078. StartProgram(false);
  31079. Add([
  31080. '{$modeswitch PrefixedAttributes}',
  31081. 'type',
  31082. ' TObject = class',
  31083. ' constructor Create;',
  31084. ' end;',
  31085. ' TCustomAttribute = class',
  31086. ' constructor Create(Id: word);',
  31087. ' end;',
  31088. ' [Missing]',
  31089. ' TBird = class',
  31090. ' published',
  31091. ' [Tcustom]',
  31092. ' FField: word;',
  31093. ' [tcustom(14)]',
  31094. ' property Size: word read FField;',
  31095. ' [Tcustom(15)]',
  31096. ' procedure Fly; virtual; abstract;',
  31097. ' end;',
  31098. ' TRec = record',
  31099. ' [Tcustom,tcustom(14)]',
  31100. ' Size: word;',
  31101. ' end;',
  31102. 'constructor TObject.Create; begin end;',
  31103. 'constructor TCustomAttribute.Create(Id: word); begin end;',
  31104. 'begin',
  31105. '']);
  31106. ConvertProgram;
  31107. CheckSource('TestAttributes_Members',
  31108. LinesToStr([ // statements
  31109. 'rtl.createClass(this, "TObject", null, function () {',
  31110. ' this.$init = function () {',
  31111. ' };',
  31112. ' this.$final = function () {',
  31113. ' };',
  31114. ' this.Create = function () {',
  31115. ' return this;',
  31116. ' };',
  31117. '});',
  31118. 'rtl.createClass(this, "TCustomAttribute", this.TObject, function () {',
  31119. ' this.Create$1 = function (Id) {',
  31120. ' return this;',
  31121. ' };',
  31122. '});',
  31123. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  31124. ' this.$init = function () {',
  31125. ' $mod.TObject.$init.call(this);',
  31126. ' this.FField = 0;',
  31127. ' };',
  31128. ' var $r = this.$rtti;',
  31129. ' $r.addField("FField", rtl.word, {',
  31130. ' attr: [$mod.TCustomAttribute, "Create"]',
  31131. ' });',
  31132. ' $r.addProperty(',
  31133. ' "Size",',
  31134. ' 0,',
  31135. ' rtl.word,',
  31136. ' "FField",',
  31137. ' "",',
  31138. ' {',
  31139. ' attr: [$mod.TCustomAttribute, "Create$1", [14]]',
  31140. ' }',
  31141. ' );',
  31142. ' $r.addMethod("Fly", 0, [], null, 0, {',
  31143. ' attr: [$mod.TCustomAttribute, "Create$1", [15]]',
  31144. ' });',
  31145. '});',
  31146. 'rtl.recNewT(this, "TRec", function () {',
  31147. ' this.Size = 0;',
  31148. ' this.$eq = function (b) {',
  31149. ' return this.Size === b.Size;',
  31150. ' };',
  31151. ' this.$assign = function (s) {',
  31152. ' this.Size = s.Size;',
  31153. ' return this;',
  31154. ' };',
  31155. ' var $r = $mod.$rtti.$Record("TRec", {});',
  31156. ' $r.addField("Size", rtl.word, {',
  31157. ' attr: [',
  31158. ' $mod.TCustomAttribute,',
  31159. ' "Create",',
  31160. ' $mod.TCustomAttribute,',
  31161. ' "Create$1",',
  31162. ' [14]',
  31163. ' ]',
  31164. ' });',
  31165. '});',
  31166. '']),
  31167. LinesToStr([ // $mod.$main
  31168. '']));
  31169. end;
  31170. procedure TTestModule.TestAttributes_Types;
  31171. begin
  31172. WithTypeInfo:=true;
  31173. StartProgram(false);
  31174. Add([
  31175. '{$modeswitch PrefixedAttributes}',
  31176. 'type',
  31177. ' TObject = class',
  31178. ' constructor Create(Id: word);',
  31179. ' end;',
  31180. ' TCustomAttribute = class',
  31181. ' end;',
  31182. ' [TCustom(1)]',
  31183. ' TMyClass = class',
  31184. ' end;',
  31185. ' [TCustom(11)]',
  31186. ' TMyDescendant = class(TMyClass)',
  31187. ' end;',
  31188. ' [TCustom(2)]',
  31189. ' TRec = record',
  31190. ' end;',
  31191. ' [TCustom(3)]',
  31192. ' TInt = type word;',
  31193. 'constructor TObject.Create(Id: word);',
  31194. 'begin',
  31195. 'end;',
  31196. 'var p: pointer;',
  31197. 'begin',
  31198. ' p:=typeinfo(TMyClass);',
  31199. ' p:=typeinfo(TRec);',
  31200. ' p:=typeinfo(TInt);',
  31201. '']);
  31202. ConvertProgram;
  31203. CheckSource('TestAttributes_Types',
  31204. LinesToStr([ // statements
  31205. 'rtl.createClass(this, "TObject", null, function () {',
  31206. ' this.$init = function () {',
  31207. ' };',
  31208. ' this.$final = function () {',
  31209. ' };',
  31210. ' this.Create = function (Id) {',
  31211. ' return this;',
  31212. ' };',
  31213. '});',
  31214. 'rtl.createClass(this, "TCustomAttribute", this.TObject, function () {',
  31215. '});',
  31216. 'rtl.createClass(this, "TMyClass", this.TObject, function () {',
  31217. ' var $r = this.$rtti;',
  31218. ' $r.attr = [$mod.TCustomAttribute, "Create", [1]];',
  31219. '});',
  31220. 'rtl.createClass(this, "TMyDescendant", this.TMyClass, function () {',
  31221. ' var $r = this.$rtti;',
  31222. ' $r.attr = [$mod.TCustomAttribute, "Create", [11]];',
  31223. '});',
  31224. 'rtl.recNewT(this, "TRec", function () {',
  31225. ' this.$eq = function (b) {',
  31226. ' return true;',
  31227. ' };',
  31228. ' this.$assign = function (s) {',
  31229. ' return this;',
  31230. ' };',
  31231. ' $mod.$rtti.$Record("TRec", {',
  31232. ' attr: [$mod.TCustomAttribute, "Create", [2]]',
  31233. ' });',
  31234. '});',
  31235. 'this.$rtti.$inherited("TInt", rtl.word, {',
  31236. ' attr: [this.TCustomAttribute, "Create", [3]]',
  31237. '});',
  31238. 'this.p = null;',
  31239. '']),
  31240. LinesToStr([ // $mod.$main
  31241. '$mod.p = $mod.$rtti["TMyClass"];',
  31242. '$mod.p = $mod.$rtti["TRec"];',
  31243. '$mod.p = $mod.$rtti["TInt"];',
  31244. '']));
  31245. end;
  31246. procedure TTestModule.TestAttributes_HelperConstructor_Fail;
  31247. begin
  31248. WithTypeInfo:=true;
  31249. StartProgram(false);
  31250. Add([
  31251. '{$modeswitch PrefixedAttributes}',
  31252. 'type',
  31253. ' TObject = class',
  31254. ' constructor Create;',
  31255. ' end;',
  31256. ' TCustomAttribute = class',
  31257. ' end;',
  31258. ' THelper = class helper for TCustomAttribute',
  31259. ' constructor Create(Id: word);',
  31260. ' end;',
  31261. ' [TCustom(3)]',
  31262. ' TMyInt = word;',
  31263. 'constructor TObject.Create; begin end;',
  31264. 'constructor THelper.Create(Id: word); begin end;',
  31265. 'begin',
  31266. ' if typeinfo(TMyInt)=nil then ;']);
  31267. ConvertProgram;
  31268. end;
  31269. procedure TTestModule.TestAssert;
  31270. begin
  31271. StartProgram(false);
  31272. Add([
  31273. 'procedure DoIt;',
  31274. 'var',
  31275. ' b: boolean;',
  31276. ' s: string;',
  31277. 'begin',
  31278. ' {$Assertions on}',
  31279. ' Assert(b);',
  31280. 'end;',
  31281. 'begin',
  31282. ' DoIt;',
  31283. '']);
  31284. ConvertProgram;
  31285. CheckSource('TestAssert',
  31286. LinesToStr([ // statements
  31287. 'this.DoIt = function () {',
  31288. ' var b = false;',
  31289. ' var s = "";',
  31290. ' if (!b) throw "assert failed";',
  31291. '};',
  31292. '']),
  31293. LinesToStr([ // $mod.$main
  31294. '$mod.DoIt();',
  31295. '']));
  31296. end;
  31297. procedure TTestModule.TestAssert_SysUtils;
  31298. begin
  31299. AddModuleWithIntfImplSrc('SysUtils.pas',
  31300. LinesToStr([
  31301. 'type',
  31302. ' TObject = class',
  31303. ' constructor Create;',
  31304. ' end;',
  31305. ' EAssertionFailed = class',
  31306. ' constructor Create(s: string);',
  31307. ' end;',
  31308. '']),
  31309. LinesToStr([
  31310. 'constructor TObject.Create;',
  31311. 'begin end;',
  31312. 'constructor EAssertionFailed.Create(s: string);',
  31313. 'begin end;',
  31314. '']) );
  31315. StartProgram(true);
  31316. Add([
  31317. 'uses sysutils;',
  31318. 'procedure DoIt;',
  31319. 'var',
  31320. ' b: boolean;',
  31321. ' s: string;',
  31322. 'begin',
  31323. ' {$Assertions on}',
  31324. ' Assert(b);',
  31325. ' Assert(b,''msg'');',
  31326. 'end;',
  31327. 'begin',
  31328. ' DoIt;',
  31329. '']);
  31330. ConvertProgram;
  31331. CheckSource('TestAssert_SysUtils',
  31332. LinesToStr([ // statements
  31333. 'this.DoIt = function () {',
  31334. ' var b = false;',
  31335. ' var s = "";',
  31336. ' if (!b) throw pas.SysUtils.EAssertionFailed.$create("Create");',
  31337. ' if (!b) throw pas.SysUtils.EAssertionFailed.$create("Create$1", ["msg"]);',
  31338. '};',
  31339. '']),
  31340. LinesToStr([ // $mod.$main
  31341. '$mod.DoIt();',
  31342. '']));
  31343. end;
  31344. procedure TTestModule.TestObjectChecks;
  31345. begin
  31346. Scanner.CurrentBoolSwitches:=Scanner.CurrentBoolSwitches+[bsObjectChecks];
  31347. StartProgram(false);
  31348. Add([
  31349. 'type',
  31350. ' TObject = class',
  31351. ' procedure DoIt;',
  31352. ' end;',
  31353. ' TClass = class of tobject;',
  31354. ' TBird = class',
  31355. ' end;',
  31356. ' TBirdClass = class of TBird;',
  31357. 'var',
  31358. ' o : TObject;',
  31359. ' c: TClass;',
  31360. ' b: TBird;',
  31361. ' bc: TBirdClass;',
  31362. 'procedure TObject.DoIt;',
  31363. 'begin',
  31364. ' b:=TBird(o);',
  31365. 'end;',
  31366. 'begin',
  31367. ' o.DoIt;',
  31368. ' b:=TBird(o);',
  31369. ' bc:=TBirdClass(c);',
  31370. '']);
  31371. ConvertProgram;
  31372. CheckSource('TestCheckMethodCall',
  31373. LinesToStr([ // statements
  31374. 'rtl.createClass(this, "TObject", null, function () {',
  31375. ' this.$init = function () {',
  31376. ' };',
  31377. ' this.$final = function () {',
  31378. ' };',
  31379. ' this.DoIt = function () {',
  31380. ' rtl.checkMethodCall(this,$mod.TObject);',
  31381. ' $mod.b = rtl.asExt($mod.o, $mod.TBird, 1);',
  31382. ' };',
  31383. '});',
  31384. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  31385. '});',
  31386. 'this.o = null;',
  31387. 'this.c = null;',
  31388. 'this.b = null;',
  31389. 'this.bc = null;',
  31390. '']),
  31391. LinesToStr([ // $mod.$main
  31392. '$mod.o.DoIt();',
  31393. '$mod.b = rtl.asExt($mod.o,$mod.TBird, 1);',
  31394. '$mod.bc = rtl.asExt($mod.c, $mod.TBird, 2);',
  31395. '']));
  31396. end;
  31397. procedure TTestModule.TestOverflowChecks_Int;
  31398. begin
  31399. Scanner.CurrentBoolSwitches:=Scanner.CurrentBoolSwitches+[bsOverflowChecks];
  31400. StartProgram(false);
  31401. Add([
  31402. 'procedure DoIt;',
  31403. 'var',
  31404. ' b: byte;',
  31405. ' n: nativeint;',
  31406. ' u: nativeuint;',
  31407. ' c: currency;',
  31408. 'begin',
  31409. ' n:=n+n;',
  31410. ' n:=n-n;',
  31411. ' n:=n+b;',
  31412. ' n:=b-n;',
  31413. ' n:=n*n;',
  31414. ' n:=n*u;',
  31415. ' c:=c+b;',
  31416. ' c:=b+c;',
  31417. ' c:=c*b;',
  31418. ' c:=b*c;',
  31419. 'end;',
  31420. 'begin',
  31421. '']);
  31422. ConvertProgram;
  31423. CheckSource('TestOverflowChecks_Int',
  31424. LinesToStr([ // statements
  31425. 'this.DoIt = function () {',
  31426. ' var b = 0;',
  31427. ' var n = 0;',
  31428. ' var u = 0;',
  31429. ' var c = 0;',
  31430. ' n = rtl.oc(n + n);',
  31431. ' n = rtl.oc(n - n);',
  31432. ' n = rtl.oc(n + b);',
  31433. ' n = rtl.oc(b - n);',
  31434. ' n = rtl.oc(n * n);',
  31435. ' n = rtl.oc(n * u);',
  31436. ' c = rtl.oc(c + (b * 10000));',
  31437. ' c = rtl.oc((b * 10000) + c);',
  31438. ' c = rtl.oc(c * b);',
  31439. ' c = rtl.oc(b * c);',
  31440. '};',
  31441. '']),
  31442. LinesToStr([ // $mod.$main
  31443. '']));
  31444. end;
  31445. procedure TTestModule.TestRangeChecks_AssignInt;
  31446. begin
  31447. Scanner.Options:=Scanner.Options+[po_CAssignments];
  31448. StartProgram(false);
  31449. Add([
  31450. '{$R+}',
  31451. 'var',
  31452. ' b: byte = 2;',
  31453. ' w: word = 3;',
  31454. 'procedure DoIt(p: byte);',
  31455. 'begin',
  31456. ' b:=w;',
  31457. ' b+=w;',
  31458. ' b:=1;',
  31459. 'end;',
  31460. '{$R-}',
  31461. 'procedure DoSome;',
  31462. 'begin',
  31463. ' DoIt(w);',
  31464. ' b:=w;',
  31465. ' b:=2;',
  31466. 'end;',
  31467. 'begin',
  31468. '{$R+}',
  31469. '']);
  31470. ConvertProgram;
  31471. CheckSource('TestRangeChecks_AssignInt',
  31472. LinesToStr([ // statements
  31473. 'this.b = 2;',
  31474. 'this.w = 3;',
  31475. 'this.DoIt = function (p) {',
  31476. ' rtl.rc(p, 0, 255);',
  31477. ' $mod.b = rtl.rc($mod.w,0,255);',
  31478. ' rtl.rc($mod.b += $mod.w, 0, 255);',
  31479. ' $mod.b = 1;',
  31480. '};',
  31481. 'this.DoSome = function () {',
  31482. ' $mod.DoIt($mod.w);',
  31483. ' $mod.b = $mod.w;',
  31484. ' $mod.b = 2;',
  31485. '};',
  31486. '']),
  31487. LinesToStr([ // $mod.$main
  31488. '']));
  31489. end;
  31490. procedure TTestModule.TestRangeChecks_AssignIntRange;
  31491. begin
  31492. Scanner.Options:=Scanner.Options+[po_CAssignments];
  31493. StartProgram(false);
  31494. Add([
  31495. '{$R+}',
  31496. 'type Ten = 1..10;',
  31497. 'var',
  31498. ' b: Ten = 2;',
  31499. ' w: Ten = 3;',
  31500. 'procedure DoIt(p: Ten);',
  31501. 'begin',
  31502. ' b:=w;',
  31503. ' b+=w;',
  31504. ' b:=1;',
  31505. 'end;',
  31506. '{$R-}',
  31507. 'procedure DoSome;',
  31508. 'begin',
  31509. ' DoIt(w);',
  31510. ' b:=w;',
  31511. ' b:=2;',
  31512. 'end;',
  31513. 'begin',
  31514. '{$R+}',
  31515. '']);
  31516. ConvertProgram;
  31517. CheckSource('TestRangeChecks_AssignIntRange',
  31518. LinesToStr([ // statements
  31519. 'this.b = 2;',
  31520. 'this.w = 3;',
  31521. 'this.DoIt = function (p) {',
  31522. ' rtl.rc(p, 1, 10);',
  31523. ' $mod.b = rtl.rc($mod.w, 1, 10);',
  31524. ' rtl.rc($mod.b += $mod.w, 1, 10);',
  31525. ' $mod.b = 1;',
  31526. '};',
  31527. 'this.DoSome = function () {',
  31528. ' $mod.DoIt($mod.w);',
  31529. ' $mod.b = $mod.w;',
  31530. ' $mod.b = 2;',
  31531. '};',
  31532. '']),
  31533. LinesToStr([ // $mod.$main
  31534. '']));
  31535. end;
  31536. procedure TTestModule.TestRangeChecks_AssignEnum;
  31537. begin
  31538. StartProgram(false);
  31539. Add([
  31540. '{$R+}',
  31541. 'type TEnum = (red,green);',
  31542. 'var',
  31543. ' e: TEnum = red;',
  31544. 'procedure DoIt(p: TEnum);',
  31545. 'begin',
  31546. ' e:=p;',
  31547. ' p:=TEnum(0);',
  31548. ' p:=succ(e);',
  31549. 'end;',
  31550. '{$R-}',
  31551. 'procedure DoSome;',
  31552. 'begin',
  31553. ' DoIt(e);',
  31554. ' e:=TEnum(1);',
  31555. ' e:=pred(e);',
  31556. 'end;',
  31557. 'begin',
  31558. '{$R+}',
  31559. '']);
  31560. ConvertProgram;
  31561. CheckSource('TestRangeChecks_AssignEnum',
  31562. LinesToStr([ // statements
  31563. 'this.TEnum = {',
  31564. ' "0": "red",',
  31565. ' red: 0,',
  31566. ' "1": "green",',
  31567. ' green: 1',
  31568. '};',
  31569. 'this.e = this.TEnum.red;',
  31570. 'this.DoIt = function (p) {',
  31571. ' rtl.rc(p, 0, 1);',
  31572. ' $mod.e = rtl.rc(p, 0, 1);',
  31573. ' p = 0;',
  31574. ' p = rtl.rc($mod.e + 1, 0, 1);',
  31575. '};',
  31576. 'this.DoSome = function () {',
  31577. ' $mod.DoIt($mod.e);',
  31578. ' $mod.e = 1;',
  31579. ' $mod.e = $mod.e - 1;',
  31580. '};',
  31581. '']),
  31582. LinesToStr([ // $mod.$main
  31583. '']));
  31584. end;
  31585. procedure TTestModule.TestRangeChecks_AssignEnumRange;
  31586. begin
  31587. StartProgram(false);
  31588. Add([
  31589. '{$R+}',
  31590. 'type',
  31591. ' TEnum = (red,green);',
  31592. ' TEnumRg = red..green;',
  31593. 'var',
  31594. ' e: TEnumRg = red;',
  31595. 'procedure DoIt(p: TEnumRg);',
  31596. 'begin',
  31597. ' e:=p;',
  31598. ' p:=TEnumRg(0);',
  31599. ' p:=succ(e);',
  31600. 'end;',
  31601. '{$R-}',
  31602. 'procedure DoSome;',
  31603. 'begin',
  31604. ' DoIt(e);',
  31605. ' e:=TEnum(1);',
  31606. ' e:=pred(e);',
  31607. 'end;',
  31608. 'begin',
  31609. '{$R+}',
  31610. '']);
  31611. ConvertProgram;
  31612. CheckSource('TestRangeChecks_AssignEnumRange',
  31613. LinesToStr([ // statements
  31614. 'this.TEnum = {',
  31615. ' "0": "red",',
  31616. ' red: 0,',
  31617. ' "1": "green",',
  31618. ' green: 1',
  31619. '};',
  31620. 'this.e = this.TEnum.red;',
  31621. 'this.DoIt = function (p) {',
  31622. ' rtl.rc(p, 0, 1);',
  31623. ' $mod.e = rtl.rc(p, 0, 1);',
  31624. ' p = 0;',
  31625. ' p = rtl.rc($mod.e + 1, 0, 1);',
  31626. '};',
  31627. 'this.DoSome = function () {',
  31628. ' $mod.DoIt($mod.e);',
  31629. ' $mod.e = 1;',
  31630. ' $mod.e = $mod.e - 1;',
  31631. '};',
  31632. '']),
  31633. LinesToStr([ // $mod.$main
  31634. '']));
  31635. end;
  31636. procedure TTestModule.TestRangeChecks_AssignChar;
  31637. begin
  31638. StartProgram(false);
  31639. Add([
  31640. '{$R+}',
  31641. 'type',
  31642. ' TLetter = char;',
  31643. 'var',
  31644. ' b: TLetter = ''2'';',
  31645. ' w: TLetter = ''3'';',
  31646. 'procedure DoIt(p: TLetter);',
  31647. 'begin',
  31648. ' b:=w;',
  31649. ' b:=''1'';',
  31650. 'end;',
  31651. '{$R-}',
  31652. 'procedure DoSome;',
  31653. 'begin',
  31654. ' DoIt(w);',
  31655. ' b:=w;',
  31656. ' b:=''2'';',
  31657. 'end;',
  31658. 'begin',
  31659. '{$R+}',
  31660. '']);
  31661. ConvertProgram;
  31662. CheckSource('TestRangeChecks_AssignChar',
  31663. LinesToStr([ // statements
  31664. 'this.b = "2";',
  31665. 'this.w = "3";',
  31666. 'this.DoIt = function (p) {',
  31667. ' rtl.rcc(p, 0, 65535);',
  31668. ' $mod.b = rtl.rcc($mod.w, 0, 65535);',
  31669. ' $mod.b = "1";',
  31670. '};',
  31671. 'this.DoSome = function () {',
  31672. ' $mod.DoIt($mod.w);',
  31673. ' $mod.b = $mod.w;',
  31674. ' $mod.b = "2";',
  31675. '};',
  31676. '']),
  31677. LinesToStr([ // $mod.$main
  31678. '']));
  31679. end;
  31680. procedure TTestModule.TestRangeChecks_AssignCharRange;
  31681. begin
  31682. StartProgram(false);
  31683. Add([
  31684. '{$R+}',
  31685. 'type TDigit = ''0''..''9'';',
  31686. 'var',
  31687. ' b: TDigit = ''2'';',
  31688. ' w: TDigit = ''3'';',
  31689. 'procedure DoIt(p: TDigit);',
  31690. 'begin',
  31691. ' b:=w;',
  31692. ' b:=''1'';',
  31693. 'end;',
  31694. '{$R-}',
  31695. 'procedure DoSome;',
  31696. 'begin',
  31697. ' DoIt(w);',
  31698. ' b:=w;',
  31699. ' b:=''2'';',
  31700. 'end;',
  31701. 'begin',
  31702. '{$R+}',
  31703. '']);
  31704. ConvertProgram;
  31705. CheckSource('TestRangeChecks_AssignCharRange',
  31706. LinesToStr([ // statements
  31707. 'this.b = "2";',
  31708. 'this.w = "3";',
  31709. 'this.DoIt = function (p) {',
  31710. ' rtl.rcc(p, 48, 57);',
  31711. ' $mod.b = rtl.rcc($mod.w, 48, 57);',
  31712. ' $mod.b = "1";',
  31713. '};',
  31714. 'this.DoSome = function () {',
  31715. ' $mod.DoIt($mod.w);',
  31716. ' $mod.b = $mod.w;',
  31717. ' $mod.b = "2";',
  31718. '};',
  31719. '']),
  31720. LinesToStr([ // $mod.$main
  31721. '']));
  31722. end;
  31723. procedure TTestModule.TestRangeChecks_ArrayIndex;
  31724. begin
  31725. StartProgram(false);
  31726. Add([
  31727. '{$R+}',
  31728. 'type',
  31729. ' Ten = 1..10;',
  31730. ' TArr = array of Ten;',
  31731. ' TArrArr = array of TArr;',
  31732. ' TArrByte = array[byte] of Ten;',
  31733. ' TArrChar = array[''0''..''9''] of Ten;',
  31734. ' TArrByteChar = array[byte,''0''..''9''] of Ten;',
  31735. ' TObject = class',
  31736. ' A: TArr;',
  31737. ' end;',
  31738. 'procedure DoIt;',
  31739. 'var',
  31740. ' Arr: TArr;',
  31741. ' ArrArr: TArrArr;',
  31742. ' ArrByte: TArrByte;',
  31743. ' ArrChar: TArrChar;',
  31744. ' ArrByteChar: TArrByteChar;',
  31745. ' i: Ten;',
  31746. ' c: char;',
  31747. ' o: tobject;',
  31748. 'begin',
  31749. ' i:=Arr[1];',
  31750. ' i:=ArrByteChar[1,''2''];',
  31751. ' Arr[1]:=Arr[1];',
  31752. ' Arr[i]:=Arr[i];',
  31753. ' ArrByte[3]:=ArrByte[3];',
  31754. ' ArrByte[i]:=ArrByte[i];',
  31755. ' ArrChar[''5'']:=ArrChar[''5''];',
  31756. ' ArrChar[c]:=ArrChar[c];',
  31757. ' ArrByteChar[7,''7'']:=ArrByteChar[7,''7''];',
  31758. ' ArrByteChar[i,c]:=ArrByteChar[i,c];',
  31759. ' o.a[i]:=o.a[i];',
  31760. 'end;',
  31761. 'begin',
  31762. '']);
  31763. ConvertProgram;
  31764. CheckSource('TestRangeChecks_ArrayIndex',
  31765. LinesToStr([ // statements
  31766. 'rtl.createClass(this, "TObject", null, function () {',
  31767. ' this.$init = function () {',
  31768. ' this.A = [];',
  31769. ' };',
  31770. ' this.$final = function () {',
  31771. ' this.A = undefined;',
  31772. ' };',
  31773. '});',
  31774. 'this.DoIt = function () {',
  31775. ' var Arr = [];',
  31776. ' var ArrArr = [];',
  31777. ' var ArrByte = rtl.arraySetLength(null, 0, 256);',
  31778. ' var ArrChar = rtl.arraySetLength(null, 0, 10);',
  31779. ' var ArrByteChar = rtl.arraySetLength(null, 0, 256, 10);',
  31780. ' var i = 0;',
  31781. ' var c = "";',
  31782. ' var o = null;',
  31783. ' i = rtl.rc(Arr[1], 1, 10);',
  31784. ' i = rtl.rc(ArrByteChar[1][2], 1, 10);',
  31785. ' Arr[1] = rtl.rc(Arr[1], 1, 10);',
  31786. ' rtl.rcArrW(Arr, i, rtl.rcArrR(Arr, i));',
  31787. ' ArrByte[3] = rtl.rc(ArrByte[3], 1, 10);',
  31788. ' rtl.rcArrW(ArrByte, i, rtl.rcArrR(ArrByte, i));',
  31789. ' ArrChar[5] = rtl.rc(ArrChar[5], 1, 10);',
  31790. ' rtl.rcArrW(ArrChar, c.charCodeAt() - 48, rtl.rcArrR(ArrChar, c.charCodeAt() - 48));',
  31791. ' ArrByteChar[7][7] = rtl.rc(ArrByteChar[7][7], 1, 10);',
  31792. ' rtl.rcArrW(ArrByteChar, i, c.charCodeAt() - 48, rtl.rcArrR(ArrByteChar, i, c.charCodeAt() - 48));',
  31793. ' rtl.rcArrW(o.A, i, rtl.rcArrR(o.A, i));',
  31794. '};',
  31795. '']),
  31796. LinesToStr([ // $mod.$main
  31797. '']));
  31798. end;
  31799. procedure TTestModule.TestRangeChecks_ArrayOfRecIndex;
  31800. begin
  31801. StartProgram(false);
  31802. Add([
  31803. '{$R+}',
  31804. 'type',
  31805. ' Ten = 1..10;',
  31806. ' TRec = record x: Ten end;',
  31807. ' TArr = array of TRec;',
  31808. ' TArrArr = array of TArr;',
  31809. ' TObject = class',
  31810. ' A: TArr;',
  31811. ' end;',
  31812. 'procedure DoIt;',
  31813. 'var',
  31814. ' Arr: TArr;',
  31815. ' ArrArr: TArrArr;',
  31816. ' i: Ten;',
  31817. ' o: tobject;',
  31818. 'begin',
  31819. ' Arr[1]:=Arr[1];',
  31820. ' Arr[i]:=Arr[i+1];',
  31821. ' o.a[i]:=o.a[i+2];',
  31822. 'end;',
  31823. 'begin',
  31824. '']);
  31825. ConvertProgram;
  31826. CheckSource('TestRangeChecks_ArrayOfRecIndex',
  31827. LinesToStr([ // statements
  31828. 'rtl.recNewT(this, "TRec", function () {',
  31829. ' this.x = 0;',
  31830. ' this.$eq = function (b) {',
  31831. ' return this.x === b.x;',
  31832. ' };',
  31833. ' this.$assign = function (s) {',
  31834. ' this.x = s.x;',
  31835. ' return this;',
  31836. ' };',
  31837. '});',
  31838. 'rtl.createClass(this, "TObject", null, function () {',
  31839. ' this.$init = function () {',
  31840. ' this.A = [];',
  31841. ' };',
  31842. ' this.$final = function () {',
  31843. ' this.A = undefined;',
  31844. ' };',
  31845. '});',
  31846. 'this.DoIt = function () {',
  31847. ' var Arr = [];',
  31848. ' var ArrArr = [];',
  31849. ' var i = 0;',
  31850. ' var o = null;',
  31851. ' Arr[1].$assign(Arr[1]);',
  31852. ' rtl.rcArrR(Arr, i).$assign(rtl.rcArrR(Arr, i + 1));',
  31853. ' rtl.rcArrR(o.A, i).$assign(rtl.rcArrR(o.A, i + 2));',
  31854. '};',
  31855. '']),
  31856. LinesToStr([ // $mod.$main
  31857. '']));
  31858. end;
  31859. procedure TTestModule.TestRangeChecks_StringIndex;
  31860. begin
  31861. StartProgram(false);
  31862. Add([
  31863. 'type',
  31864. ' TObject = class',
  31865. ' S: string;',
  31866. ' end;',
  31867. '{$R+}',
  31868. 'procedure DoIt(var h: string);',
  31869. 'var',
  31870. ' s: string;',
  31871. ' i: longint;',
  31872. ' c: char;',
  31873. ' o: tobject;',
  31874. 'begin',
  31875. ' c:=s[1];',
  31876. ' s[i]:=s[i];',
  31877. ' h[i]:=h[i];',
  31878. ' c:=o.s[i];',
  31879. ' o.s[i]:=c;',
  31880. 'end;',
  31881. 'begin',
  31882. '']);
  31883. ConvertProgram;
  31884. CheckSource('TestRangeChecks_StringIndex',
  31885. LinesToStr([ // statements
  31886. 'rtl.createClass(this, "TObject", null, function () {',
  31887. ' this.$init = function () {',
  31888. ' this.S = "";',
  31889. ' };',
  31890. ' this.$final = function () {',
  31891. ' };',
  31892. '});',
  31893. 'this.DoIt = function (h) {',
  31894. ' var s = "";',
  31895. ' var i = 0;',
  31896. ' var c = "";',
  31897. ' var o = null;',
  31898. ' c = rtl.rcc(rtl.rcCharAt(s, 0), 0, 65535);',
  31899. ' s = rtl.rcSetCharAt(s, i - 1, rtl.rcCharAt(s, i - 1));',
  31900. ' h.set(rtl.rcSetCharAt(h.get(), i - 1, rtl.rcCharAt(h.get(), i - 1)));',
  31901. ' c = rtl.rcc(rtl.rcCharAt(o.S, i - 1), 0, 65535);',
  31902. ' o.S = rtl.rcSetCharAt(o.S, i - 1, c);',
  31903. '};',
  31904. '']),
  31905. LinesToStr([ // $mod.$main
  31906. '']));
  31907. end;
  31908. procedure TTestModule.TestRangeChecks_TypecastInt;
  31909. begin
  31910. StartProgram(false);
  31911. Add([
  31912. '{$R+}',
  31913. 'var',
  31914. ' i: nativeint;',
  31915. ' b: byte;',
  31916. ' sh: shortint;',
  31917. ' w: word;',
  31918. ' sm: smallint;',
  31919. ' lw: longword;',
  31920. ' li: longint;',
  31921. 'begin',
  31922. ' b:=12+byte(i);',
  31923. ' sh:=12+shortint(i);',
  31924. ' w:=12+word(i);',
  31925. ' sm:=12+smallint(i);',
  31926. ' lw:=12+longword(i);',
  31927. ' li:=12+longint(i);',
  31928. '']);
  31929. ConvertProgram;
  31930. CheckSource('TestRangeChecks_TypecastInt',
  31931. LinesToStr([
  31932. 'this.i = 0;',
  31933. 'this.b = 0;',
  31934. 'this.sh = 0;',
  31935. 'this.w = 0;',
  31936. 'this.sm = 0;',
  31937. 'this.lw = 0;',
  31938. 'this.li = 0;',
  31939. '']),
  31940. LinesToStr([
  31941. '$mod.b = rtl.rc(12 + rtl.rc($mod.i, 0, 255), 0, 255);',
  31942. '$mod.sh = rtl.rc(12 + rtl.rc($mod.i, -128, 127), -128, 127);',
  31943. '$mod.w = rtl.rc(12 + rtl.rc($mod.i, 0, 65535), 0, 65535);',
  31944. '$mod.sm = rtl.rc(12 + rtl.rc($mod.i, -32768, 32767), -32768, 32767);',
  31945. '$mod.lw = rtl.rc(12 + rtl.rc($mod.i, 0, 4294967295), 0, 4294967295);',
  31946. '$mod.li = rtl.rc(12 + rtl.rc($mod.i, -2147483648, 2147483647), -2147483648, 2147483647);',
  31947. '']));
  31948. end;
  31949. procedure TTestModule.TestRangeChecks_TypeHelperInt;
  31950. begin
  31951. Scanner.Options:=Scanner.Options+[po_CAssignments];
  31952. StartProgram(false);
  31953. Add([
  31954. '{$modeswitch typehelpers}',
  31955. '{$R+}',
  31956. 'type',
  31957. ' TObject = class',
  31958. ' FSize: byte;',
  31959. ' property Size: byte read FSize;',
  31960. ' end;',
  31961. ' THelper = type helper for byte',
  31962. ' procedure SetIt(w: word);',
  31963. ' end;',
  31964. 'procedure THelper.SetIt(w: word);',
  31965. 'begin',
  31966. ' Self:=w;',
  31967. 'end;',
  31968. 'function GetIt: byte;',
  31969. 'begin',
  31970. ' Result.SetIt(2);',
  31971. 'end;',
  31972. 'var',
  31973. ' b: byte = 3;',
  31974. ' o: TObject;',
  31975. 'begin',
  31976. ' b.SetIt(14);',
  31977. ' with b do SetIt(15);',
  31978. ' o.Size.SetIt(16);',
  31979. '']);
  31980. ConvertProgram;
  31981. CheckSource('TestRangeChecks_AssignInt',
  31982. LinesToStr([ // statements
  31983. 'rtl.createClass(this, "TObject", null, function () {',
  31984. ' this.$init = function () {',
  31985. ' this.FSize = 0;',
  31986. ' };',
  31987. ' this.$final = function () {',
  31988. ' };',
  31989. '});',
  31990. 'rtl.createHelper(this, "THelper", null, function () {',
  31991. ' this.SetIt = function (w) {',
  31992. ' rtl.rc(w, 0, 65535);',
  31993. ' this.set(w);',
  31994. ' };',
  31995. '});',
  31996. 'this.GetIt = function () {',
  31997. ' var Result = 0;',
  31998. ' $mod.THelper.SetIt.call({',
  31999. ' get: function () {',
  32000. ' return Result;',
  32001. ' },',
  32002. ' set: function (v) {',
  32003. ' rtl.rc(v, 0, 255);',
  32004. ' Result = v;',
  32005. ' }',
  32006. ' }, 2);',
  32007. ' return Result;',
  32008. '};',
  32009. 'this.b = 3;',
  32010. 'this.o = null;',
  32011. '']),
  32012. LinesToStr([ // $mod.$main
  32013. '$mod.THelper.SetIt.call({',
  32014. ' p: $mod,',
  32015. ' get: function () {',
  32016. ' return this.p.b;',
  32017. ' },',
  32018. ' set: function (v) {',
  32019. ' rtl.rc(v, 0, 255);',
  32020. ' this.p.b = v;',
  32021. ' }',
  32022. '}, 14);',
  32023. 'var $with = $mod.b;',
  32024. '$mod.THelper.SetIt.call({',
  32025. ' get: function () {',
  32026. ' return $with;',
  32027. ' },',
  32028. ' set: function (v) {',
  32029. ' rtl.rc(v, 0, 255);',
  32030. ' $with = v;',
  32031. ' }',
  32032. '}, 15);',
  32033. '$mod.THelper.SetIt.call({',
  32034. ' p: $mod.o,',
  32035. ' get: function () {',
  32036. ' return this.p.FSize;',
  32037. ' },',
  32038. ' set: function (v) {',
  32039. ' rtl.rc(v, 0, 255);',
  32040. ' this.p.FSize = v;',
  32041. ' }',
  32042. '}, 16);',
  32043. '']));
  32044. end;
  32045. procedure TTestModule.TestAsync_Proc;
  32046. begin
  32047. StartProgram(false);
  32048. Add([
  32049. 'procedure Fly(w: word = 1); async; forward;',
  32050. 'procedure Run(w: word = 2); async;',
  32051. 'begin',
  32052. ' Fly(w);',
  32053. ' Fly;',
  32054. ' await(Fly(w));',
  32055. ' await(Fly);',
  32056. 'end;',
  32057. 'procedure Fly(w: word); ',
  32058. 'begin',
  32059. 'end;',
  32060. 'begin',
  32061. ' Run;',
  32062. ' Run(3);',
  32063. '']);
  32064. CheckResolverUnexpectedHints();
  32065. ConvertProgram;
  32066. CheckSource('TestAsync_Proc',
  32067. LinesToStr([ // statements
  32068. 'this.Run = async function (w) {',
  32069. ' $mod.Fly(w);',
  32070. ' $mod.Fly(1);',
  32071. ' await $mod.Fly(w);',
  32072. ' await $mod.Fly(1);',
  32073. '};',
  32074. 'this.Fly = async function (w) {',
  32075. '};',
  32076. '']),
  32077. LinesToStr([
  32078. '$mod.Run(2);',
  32079. '$mod.Run(3);',
  32080. '']));
  32081. end;
  32082. procedure TTestModule.TestAsync_CallResultIsPromise;
  32083. begin
  32084. StartProgram(false);
  32085. Add([
  32086. '{$modeswitch externalclass}',
  32087. 'type',
  32088. ' TObject = class',
  32089. ' end;',
  32090. ' TJSPromise = class external name ''Promise''',
  32091. ' end;',
  32092. ' TBird = class',
  32093. ' function Fly: word; async; ',
  32094. ' end;',
  32095. 'function TBird.Fly: word; async; ',
  32096. 'begin',
  32097. ' Result:=3;',
  32098. ' Fly:=4+Result;',
  32099. ' if Result=5 then ;',
  32100. ' exit(6);',
  32101. 'end;',
  32102. 'function Run: word; async;',
  32103. 'begin',
  32104. ' Result:=11+Result;',
  32105. ' inc(Result);',
  32106. 'end;',
  32107. 'var',
  32108. ' p: TJSPromise;',
  32109. ' o: TBird;',
  32110. 'begin',
  32111. ' p:=Run;',
  32112. ' p:=Run();',
  32113. ' if Run=p then ;',
  32114. ' if p=Run then ;',
  32115. ' if Run()=p then ;',
  32116. ' if p=Run() then ;',
  32117. ' p:=o.Fly;',
  32118. ' p:=o.Fly();',
  32119. ' if o.Fly=p then ;',
  32120. ' if o.Fly()=p then ;',
  32121. ' with o do begin',
  32122. ' p:=Fly;',
  32123. ' p:=Fly();',
  32124. ' if Fly=p then ;',
  32125. ' if Fly()=p then ;',
  32126. ' end;',
  32127. '']);
  32128. CheckResolverUnexpectedHints();
  32129. ConvertProgram;
  32130. CheckSource('TestAsync_CallResultIsPromise',
  32131. LinesToStr([ // statements
  32132. 'rtl.createClass(this, "TObject", null, function () {',
  32133. ' this.$init = function () {',
  32134. ' };',
  32135. ' this.$final = function () {',
  32136. ' };',
  32137. '});',
  32138. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  32139. ' this.Fly = async function () {',
  32140. ' var Result = 0;',
  32141. ' Result = 3;',
  32142. ' Result = 4 + Result;',
  32143. ' if (Result === 5) ;',
  32144. ' return 6;',
  32145. ' return Result;',
  32146. ' };',
  32147. '});',
  32148. 'this.Run = async function () {',
  32149. ' var Result = 0;',
  32150. ' Result = 11 + Result;',
  32151. ' Result += 1;',
  32152. ' return Result;',
  32153. '};',
  32154. 'this.p = null;',
  32155. 'this.o = null;',
  32156. '']),
  32157. LinesToStr([
  32158. '$mod.p = $mod.Run();',
  32159. '$mod.p = $mod.Run();',
  32160. 'if ($mod.Run() === $mod.p) ;',
  32161. 'if ($mod.p === $mod.Run()) ;',
  32162. 'if ($mod.Run() === $mod.p) ;',
  32163. 'if ($mod.p === $mod.Run()) ;',
  32164. '$mod.p = $mod.o.Fly();',
  32165. '$mod.p = $mod.o.Fly();',
  32166. 'if ($mod.o.Fly() === $mod.p) ;',
  32167. 'if ($mod.o.Fly() === $mod.p) ;',
  32168. 'var $with = $mod.o;',
  32169. '$mod.p = $with.Fly();',
  32170. '$mod.p = $with.Fly();',
  32171. 'if ($with.Fly() === $mod.p) ;',
  32172. 'if ($with.Fly() === $mod.p) ;',
  32173. '']));
  32174. end;
  32175. procedure TTestModule.TestAsync_ConstructorFail;
  32176. begin
  32177. StartProgram(false);
  32178. Add([
  32179. 'type',
  32180. ' TObject = class',
  32181. ' end;',
  32182. ' TBird = class',
  32183. ' constructor Create; async;',
  32184. ' end;',
  32185. 'constructor TBird.Create; async;',
  32186. 'begin',
  32187. 'end;',
  32188. 'begin',
  32189. '']);
  32190. SetExpectedPasResolverError('Invalid constructor modifier async',nInvalidXModifierY);
  32191. ConvertProgram;
  32192. end;
  32193. procedure TTestModule.TestAsync_PropertyGetterFail;
  32194. begin
  32195. StartProgram(false);
  32196. Add([
  32197. 'type',
  32198. ' TObject = class',
  32199. ' end;',
  32200. ' TBird = class',
  32201. ' function GetSize: word; async;',
  32202. ' property Size: word read GetSize;',
  32203. ' end;',
  32204. 'function TBird.GetSize: word; async;',
  32205. 'begin',
  32206. 'end;',
  32207. 'begin',
  32208. '']);
  32209. SetExpectedPasResolverError('Invalid property getter modifier async',nInvalidXModifierY);
  32210. ConvertProgram;
  32211. end;
  32212. procedure TTestModule.TestAwait_NonPromiseWithTypeFail;
  32213. begin
  32214. StartProgram(false);
  32215. Add([
  32216. 'procedure Run; async;',
  32217. 'begin',
  32218. ' await(word,1);',
  32219. 'end;',
  32220. 'begin',
  32221. '']);
  32222. SetExpectedPasResolverError('Incompatible type arg no. 2: Got "Longint", expected "TJSPromise"',nIncompatibleTypeArgNo);
  32223. ConvertProgram;
  32224. end;
  32225. procedure TTestModule.TestAwait_AsyncCallTypeMismatch;
  32226. begin
  32227. StartProgram(false);
  32228. Add([
  32229. 'type',
  32230. ' TObject = class',
  32231. ' end;',
  32232. ' TBird = class',
  32233. ' end;',
  32234. 'function Fly: TObject; async;',
  32235. 'begin',
  32236. 'end;',
  32237. 'procedure Run; async;',
  32238. 'begin',
  32239. ' await(TBird,Fly);',
  32240. 'end;',
  32241. 'begin',
  32242. '']);
  32243. SetExpectedPasResolverError('Incompatible type arg no. 2: Got "TObject", expected "TBird"',nIncompatibleTypeArgNo);
  32244. ConvertProgram;
  32245. end;
  32246. procedure TTestModule.TestAWait_OutsideAsyncFail;
  32247. begin
  32248. StartProgram(false);
  32249. Add([
  32250. 'procedure Crawl(w: double); ',
  32251. 'begin',
  32252. 'end;',
  32253. 'procedure Run(w: double);',
  32254. 'begin',
  32255. ' await(Crawl(w));',
  32256. 'end;',
  32257. 'begin',
  32258. ' Run(1);']);
  32259. SetExpectedPasResolverError(sAWaitOnlyInAsyncProcedure,nAWaitOnlyInAsyncProcedure);
  32260. ConvertProgram;
  32261. end;
  32262. procedure TTestModule.TestAWait_IntegerFail;
  32263. begin
  32264. StartProgram(false);
  32265. Add([
  32266. 'function Run: word;',
  32267. 'begin',
  32268. 'end;',
  32269. 'procedure Fly(w: word); async;',
  32270. 'begin',
  32271. ' await(Run());',
  32272. 'end;',
  32273. 'begin',
  32274. ' Fly(1);']);
  32275. SetExpectedPasResolverError('async function expected, but Result:Word found',nXExpectedButYFound);
  32276. ConvertProgram;
  32277. end;
  32278. procedure TTestModule.TestAWait_ExternalClassPromise;
  32279. begin
  32280. StartProgram(false);
  32281. Add([
  32282. '{$modeswitch externalclass}',
  32283. 'type',
  32284. ' TJSPromise = class external name ''Promise''',
  32285. ' end;',
  32286. ' TJSThenable = class external name ''Thenable''',
  32287. ' end;',
  32288. 'function Fly(w: word): TJSPromise;',
  32289. 'begin',
  32290. 'end;',
  32291. 'function Jump(w: word): word; async;',
  32292. 'begin',
  32293. 'end;',
  32294. 'function Eat(w: word): TJSPromise; async;',
  32295. 'begin',
  32296. 'end;',
  32297. 'function Run(d: double): word; async;',
  32298. 'var',
  32299. ' p: TJSPromise;',
  32300. 'begin',
  32301. ' Result:=await(word,p);', // promise needs type
  32302. ' Result:=await(word,Fly(3));', // promise needs type
  32303. ' Result:=await(Jump(4));', // async non promise must omit the type
  32304. ' Result:=await(word,Jump(5));', // async call can provide fitting type
  32305. ' Result:=await(word,Eat(6));', // promise needs type
  32306. 'end;',
  32307. 'begin',
  32308. '']);
  32309. ConvertProgram;
  32310. CheckSource('TestAWait_ExternalClassPromise',
  32311. LinesToStr([ // statements
  32312. 'this.Fly = function (w) {',
  32313. ' var Result = null;',
  32314. ' return Result;',
  32315. '};',
  32316. 'this.Jump = async function (w) {',
  32317. ' var Result = 0;',
  32318. ' return Result;',
  32319. '};',
  32320. 'this.Eat = async function (w) {',
  32321. ' var Result = null;',
  32322. ' return Result;',
  32323. '};',
  32324. 'this.Run = async function (d) {',
  32325. ' var Result = 0;',
  32326. ' var p = null;',
  32327. ' Result = await p;',
  32328. ' Result = await $mod.Fly(3);',
  32329. ' Result = await $mod.Jump(4);',
  32330. ' Result = await $mod.Jump(5);',
  32331. ' Result = await $mod.Eat(6);',
  32332. ' return Result;',
  32333. '};',
  32334. '']),
  32335. LinesToStr([
  32336. ]));
  32337. CheckResolverUnexpectedHints();
  32338. end;
  32339. procedure TTestModule.TestAWait_JSValue;
  32340. begin
  32341. StartProgram(false);
  32342. Add([
  32343. '{$modeswitch externalclass}',
  32344. 'type',
  32345. ' TJSPromise = class external name ''Promise''',
  32346. ' end;',
  32347. 'function Fly(w: word): jsvalue; async;',
  32348. 'begin',
  32349. 'end;',
  32350. 'function Run(d: jsvalue; var e): word; async;',
  32351. 'begin',
  32352. ' Result:=await(word,d);', // promise needs type
  32353. ' d:=await(Fly(4));', // async non promise must omit the type
  32354. ' Result:=await(word,e);', // promise needs type
  32355. 'end;',
  32356. 'begin',
  32357. '']);
  32358. ConvertProgram;
  32359. CheckSource('TestAWait_JSValue',
  32360. LinesToStr([ // statements
  32361. 'this.Fly = async function (w) {',
  32362. ' var Result = undefined;',
  32363. ' return Result;',
  32364. '};',
  32365. 'this.Run = async function (d, e) {',
  32366. ' var Result = 0;',
  32367. ' Result = await d;',
  32368. ' d = await $mod.Fly(4);',
  32369. ' Result = await e.get();',
  32370. ' return Result;',
  32371. '};',
  32372. '']),
  32373. LinesToStr([
  32374. ]));
  32375. CheckResolverUnexpectedHints();
  32376. end;
  32377. procedure TTestModule.TestAWait_Result;
  32378. begin
  32379. StartProgram(false);
  32380. Add([
  32381. '{$modeswitch externalclass}',
  32382. 'type',
  32383. ' TJSPromise = class external name ''Promise''',
  32384. ' end;',
  32385. 'function Crawl(d: double = 1.3): TJSPromise; ',
  32386. 'begin',
  32387. 'end;',
  32388. 'function Run(d: double = 1.6): word; async;',
  32389. 'begin',
  32390. ' Result:=await(word,Crawl);',
  32391. ' Result:=await(word,Crawl(4.5));',
  32392. ' Result:=await(Run);',
  32393. ' Result:=await(Run(6.7));',
  32394. 'end;',
  32395. 'begin',
  32396. ' Run(1);']);
  32397. ConvertProgram;
  32398. CheckSource('TestAWait_Result',
  32399. LinesToStr([ // statements
  32400. 'this.Crawl = function (d) {',
  32401. ' var Result = null;',
  32402. ' return Result;',
  32403. '};',
  32404. 'this.Run = async function (d) {',
  32405. ' var Result = 0;',
  32406. ' Result = await $mod.Crawl(1.3);',
  32407. ' Result = await $mod.Crawl(4.5);',
  32408. ' Result = await $mod.Run(1.6);',
  32409. ' Result = await $mod.Run(6.7);',
  32410. ' return Result;',
  32411. '};',
  32412. '']),
  32413. LinesToStr([
  32414. '$mod.Run(1);'
  32415. ]));
  32416. CheckResolverUnexpectedHints();
  32417. end;
  32418. procedure TTestModule.TestAWait_ResultPromiseMissingTypeFail;
  32419. begin
  32420. StartProgram(false);
  32421. Add([
  32422. '{$mode objfpc}',
  32423. '{$modeswitch externalclass}',
  32424. 'type',
  32425. ' TJSPromise = class external name ''Promise''',
  32426. ' end;',
  32427. 'function Run: TJSPromise; async;',
  32428. 'begin',
  32429. 'end;',
  32430. 'procedure Fly(w: word); async;',
  32431. 'begin',
  32432. ' await(Run());',
  32433. 'end;',
  32434. 'begin',
  32435. ' Fly(1);']);
  32436. SetExpectedPasResolverError('Wrong number of parameters specified for call to "function await(aType,TJSPromise):aType"',
  32437. nWrongNumberOfParametersForCallTo);
  32438. ConvertProgram;
  32439. end;
  32440. procedure TTestModule.TestAsync_AnonymousProc;
  32441. begin
  32442. StartProgram(false);
  32443. Add([
  32444. '{$mode objfpc}',
  32445. '{$modeswitch externalclass}',
  32446. 'type',
  32447. ' TJSPromise = class external name ''Promise''',
  32448. ' end;',
  32449. 'type',
  32450. ' TFunc = reference to function(x: double): word; async;',
  32451. 'function Crawl(d: double = 1.3): word; async;',
  32452. 'begin',
  32453. 'end;',
  32454. 'var Func: TFunc;',
  32455. 'begin',
  32456. ' Func:=function(c:double):word async begin',
  32457. ' Result:=await(Crawl(c));',
  32458. ' end;',
  32459. ' Func:=function(c:double):word async assembler asm',
  32460. ' end;',
  32461. ' ']);
  32462. ConvertProgram;
  32463. CheckSource('TestAsync_AnonymousProc',
  32464. LinesToStr([ // statements
  32465. 'this.Crawl = async function (d) {',
  32466. ' var Result = 0;',
  32467. ' return Result;',
  32468. '};',
  32469. 'this.Func = null;',
  32470. '']),
  32471. LinesToStr([
  32472. '$mod.Func = async function (c) {',
  32473. ' var Result = 0;',
  32474. ' Result = await $mod.Crawl(c);',
  32475. ' return Result;',
  32476. '};',
  32477. '$mod.Func = async function (c) {',
  32478. '};',
  32479. '']));
  32480. CheckResolverUnexpectedHints();
  32481. end;
  32482. procedure TTestModule.TestAsync_ProcType;
  32483. begin
  32484. StartProgram(false);
  32485. Add([
  32486. '{$mode objfpc}',
  32487. 'type',
  32488. ' TRefFunc = reference to function(x: double = 1.3): word; async;',
  32489. ' TFunc = function(x: double = 1.1): word; async;',
  32490. ' TProc = procedure(x: longint = 7); async;',
  32491. 'function Crawl(d: double): word; async;',
  32492. 'begin',
  32493. 'end;',
  32494. 'procedure Run(e:longint); async;',
  32495. 'begin',
  32496. 'end;',
  32497. 'procedure Fly(p: TProc); async;',
  32498. 'begin',
  32499. ' await(p);',
  32500. ' await(p());',
  32501. 'end;',
  32502. 'var',
  32503. ' RefFunc: TRefFunc;',
  32504. ' Func: TFunc;',
  32505. ' Proc, ProcB: TProc;',
  32506. 'begin',
  32507. ' Func:=@Crawl;',
  32508. ' RefFunc:=@Crawl;',
  32509. ' RefFunc:=function(c:double):word async begin',
  32510. ' Result:=await(RefFunc);',
  32511. ' Result:=await(RefFunc());',
  32512. ' Result:=await(Func);',
  32513. ' Result:=await(Func());',
  32514. ' await(Proc);',
  32515. ' await(Proc());',
  32516. ' await(Proc(13));',
  32517. ' end;',
  32518. ' Proc:=@Run;',
  32519. ' if Proc=ProcB then ;',
  32520. ' ']);
  32521. ConvertProgram;
  32522. CheckResolverUnexpectedHints();
  32523. CheckSource('TestAsync_ProcType',
  32524. LinesToStr([ // statements
  32525. 'this.Crawl = async function (d) {',
  32526. ' var Result = 0;',
  32527. ' return Result;',
  32528. '};',
  32529. 'this.Run = async function (e) {',
  32530. '};',
  32531. 'this.Fly = async function (p) {',
  32532. ' await p(7);',
  32533. ' await p(7);',
  32534. '};',
  32535. 'this.RefFunc = null;',
  32536. 'this.Func = null;',
  32537. 'this.Proc = null;',
  32538. 'this.ProcB = null;',
  32539. '']),
  32540. LinesToStr([
  32541. '$mod.Func = $mod.Crawl;',
  32542. '$mod.RefFunc = $mod.Crawl;',
  32543. '$mod.RefFunc = async function (c) {',
  32544. ' var Result = 0;',
  32545. ' Result = await $mod.RefFunc(1.3);',
  32546. ' Result = await $mod.RefFunc(1.3);',
  32547. ' Result = await $mod.Func(1.1);',
  32548. ' Result = await $mod.Func(1.1);',
  32549. ' await $mod.Proc(7);',
  32550. ' await $mod.Proc(7);',
  32551. ' await $mod.Proc(13);',
  32552. ' return Result;',
  32553. '};',
  32554. '$mod.Proc = $mod.Run;',
  32555. 'if (rtl.eqCallback($mod.Proc, $mod.ProcB)) ;',
  32556. '']));
  32557. end;
  32558. procedure TTestModule.TestAsync_ProcTypeAsyncModMismatchFail;
  32559. begin
  32560. StartProgram(false);
  32561. Add([
  32562. '{$mode objfpc}',
  32563. 'type',
  32564. ' TRefFunc = reference to function(x: double = 1.3): word;',
  32565. 'function Crawl(d: double): word; async;',
  32566. 'begin',
  32567. 'end;',
  32568. 'var',
  32569. ' RefFunc: TRefFunc;',
  32570. 'begin',
  32571. ' RefFunc:=@Crawl;',
  32572. ' ']);
  32573. SetExpectedPasResolverError('procedure type modifier "async" mismatch',nXModifierMismatchY);
  32574. ConvertProgram;
  32575. end;
  32576. procedure TTestModule.TestAsync_Inherited;
  32577. begin
  32578. StartProgram(false);
  32579. Add([
  32580. '{$mode objfpc}',
  32581. '{$modeswitch externalclass}',
  32582. 'type',
  32583. ' TJSPromise = class external name ''Promise''',
  32584. ' end;',
  32585. ' TObject = class',
  32586. ' function Run(w: word = 3): word; async; virtual;',
  32587. ' end;',
  32588. ' TBird = class',
  32589. ' function Run(w: word = 3): word; async; override;',
  32590. ' end;',
  32591. 'function TObject.Run(w: word = 3): word; async;',
  32592. 'begin',
  32593. 'end;',
  32594. 'function TBird.Run(w: word = 3): word;', // async modifier not needed in impl
  32595. 'var p: TJSPromise;',
  32596. 'begin',
  32597. ' p:=inherited;',
  32598. ' p:=inherited Run;',
  32599. ' p:=inherited Run();',
  32600. ' p:=inherited Run(4);',
  32601. ' exit(p);',
  32602. ' exit(inherited);',
  32603. ' exit(inherited Run);',
  32604. ' exit(inherited Run(5));',
  32605. ' exit(6);',
  32606. 'end;',
  32607. 'begin',
  32608. ' ']);
  32609. ConvertProgram;
  32610. CheckSource('TestAsync_Inherited',
  32611. LinesToStr([ // statements
  32612. 'rtl.createClass(this, "TObject", null, function () {',
  32613. ' this.$init = function () {',
  32614. ' };',
  32615. ' this.$final = function () {',
  32616. ' };',
  32617. ' this.Run = async function (w) {',
  32618. ' var Result = 0;',
  32619. ' return Result;',
  32620. ' };',
  32621. '});',
  32622. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  32623. ' this.Run = async function (w) {',
  32624. ' var Result = 0;',
  32625. ' var p = null;',
  32626. ' p = $mod.TObject.Run.apply(this, arguments);',
  32627. ' p = $mod.TObject.Run.call(this, 3);',
  32628. ' p = $mod.TObject.Run.call(this, 3);',
  32629. ' p = $mod.TObject.Run.call(this, 4);',
  32630. ' return p;',
  32631. ' return $mod.TObject.Run.apply(this, arguments);',
  32632. ' return $mod.TObject.Run.call(this, 3);',
  32633. ' return $mod.TObject.Run.call(this, 5);',
  32634. ' return 6;',
  32635. ' return Result;',
  32636. ' };',
  32637. '});',
  32638. '']),
  32639. LinesToStr([
  32640. '']));
  32641. CheckResolverUnexpectedHints();
  32642. end;
  32643. procedure TTestModule.TestAsync_ClassInterface;
  32644. begin
  32645. StartProgram(false);
  32646. Add([
  32647. '{$mode objfpc}',
  32648. '{$modeswitch externalclass}',
  32649. 'type',
  32650. ' TJSPromise = class external name ''Promise''',
  32651. ' end;',
  32652. ' IUnknown = interface',
  32653. ' function _AddRef: longint;',
  32654. ' function _Release: longint;',
  32655. ' end;',
  32656. 'function Say(i: IUnknown): IUnknown; async;',
  32657. 'begin',
  32658. 'end;',
  32659. 'function Run: IUnknown; async;',
  32660. 'begin',
  32661. ' Result:=await(Run);',
  32662. ' Result:=await(Run());',
  32663. ' Result:=await(Run) as IUnknown;',
  32664. ' Result:=await(Say(nil));',
  32665. ' Result:=await(Say(await(Run())));',
  32666. ' Result:=await(Say(await(Run()) as IUnknown));',
  32667. ' Result:=await(Say(await(Run()) as IUnknown)) as IUnknown;',
  32668. 'end;',
  32669. 'procedure Fly;',
  32670. 'var p: TJSPromise;',
  32671. 'begin',
  32672. ' Run;',
  32673. ' Run();',
  32674. ' p:=Run;',
  32675. ' p:=Run();',
  32676. 'end;',
  32677. 'begin',
  32678. ' ']);
  32679. ConvertProgram;
  32680. CheckSource('TestAsync_ClassInterface',
  32681. LinesToStr([ // statements
  32682. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  32683. 'this.Say = async function (i) {',
  32684. ' var Result = null;',
  32685. ' return Result;',
  32686. '};',
  32687. 'this.Run = async function () {',
  32688. ' var Result = null;',
  32689. ' var $ok = false;',
  32690. ' try {',
  32691. ' Result = rtl.setIntfL(Result, await $mod.Run());',
  32692. ' Result = rtl.setIntfL(Result, await $mod.Run());',
  32693. ' Result = rtl.setIntfL(Result, rtl.intfAsIntfT(await $mod.Run(), $mod.IUnknown));',
  32694. ' Result = rtl.setIntfL(Result, await $mod.Say(null));',
  32695. ' Result = rtl.setIntfL(Result, await $mod.Say(await $mod.Run()));',
  32696. ' Result = rtl.setIntfL(Result, await $mod.Say(rtl.intfAsIntfT(await $mod.Run(), $mod.IUnknown)));',
  32697. ' Result = rtl.setIntfL(Result, rtl.intfAsIntfT(await $mod.Say(rtl.intfAsIntfT(await $mod.Run(), $mod.IUnknown)), $mod.IUnknown));',
  32698. ' $ok = true;',
  32699. ' } finally {',
  32700. ' if (!$ok) rtl._Release(Result);',
  32701. ' };',
  32702. ' return Result;',
  32703. '};',
  32704. 'this.Fly = function () {',
  32705. ' var p = null;',
  32706. ' $mod.Run();',
  32707. ' $mod.Run();',
  32708. ' p = $mod.Run();',
  32709. ' p = $mod.Run();',
  32710. '};',
  32711. '']),
  32712. LinesToStr([
  32713. '']));
  32714. CheckResolverUnexpectedHints();
  32715. end;
  32716. procedure TTestModule.TestAsync_ClassInterface_AsyncMissmatchFail;
  32717. begin
  32718. StartProgram(true,[supTInterfacedObject]);
  32719. Add([
  32720. '{$mode objfpc}',
  32721. '{$modeswitch externalclass}',
  32722. 'type',
  32723. ' TJSPromise = class external name ''Promise''',
  32724. ' end;',
  32725. ' IBird = interface',
  32726. ' procedure Run;',
  32727. ' end;',
  32728. ' TBird = class(TInterfacedObject,IBird)',
  32729. ' procedure Run; async;',
  32730. ' end;',
  32731. 'procedure TBird.Run;',
  32732. 'begin',
  32733. 'end;',
  32734. 'begin',
  32735. ' ']);
  32736. SetExpectedPasResolverError('procedure type modifier "async" mismatch',nXModifierMismatchY);
  32737. ConvertProgram;
  32738. end;
  32739. procedure TTestModule.TestLibrary_Empty;
  32740. begin
  32741. StartLibrary(false);
  32742. Add([
  32743. '']);
  32744. ConvertLibrary;
  32745. CheckSource('TestLibrary_Empty',
  32746. LinesToStr([ // statements
  32747. '']),
  32748. LinesToStr([
  32749. '']));
  32750. CheckResolverUnexpectedHints();
  32751. end;
  32752. procedure TTestModule.TestLibrary_ExportFunc;
  32753. begin
  32754. exit;
  32755. StartLibrary(false);
  32756. Add([
  32757. 'procedure Run(w: word);',
  32758. 'begin',
  32759. 'end;',
  32760. 'exports',
  32761. ' Run,',
  32762. ' run name ''Foo'';',
  32763. '']);
  32764. ConvertLibrary;
  32765. CheckSource('TestLibrary_ExportFunc',
  32766. LinesToStr([ // statements
  32767. '']),
  32768. LinesToStr([
  32769. '']));
  32770. CheckResolverUnexpectedHints();
  32771. end;
  32772. Initialization
  32773. RegisterTests([TTestModule]);
  32774. end.