tcmodules.pas 877 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664236652366623667236682366923670236712367223673236742367523676236772367823679236802368123682236832368423685236862368723688236892369023691236922369323694236952369623697236982369923700237012370223703237042370523706237072370823709237102371123712237132371423715237162371723718237192372023721237222372323724237252372623727237282372923730237312373223733237342373523736237372373823739237402374123742237432374423745237462374723748237492375023751237522375323754237552375623757237582375923760237612376223763237642376523766237672376823769237702377123772237732377423775237762377723778237792378023781237822378323784237852378623787237882378923790237912379223793237942379523796237972379823799238002380123802238032380423805238062380723808238092381023811238122381323814238152381623817238182381923820238212382223823238242382523826238272382823829238302383123832238332383423835238362383723838238392384023841238422384323844238452384623847238482384923850238512385223853238542385523856238572385823859238602386123862238632386423865238662386723868238692387023871238722387323874238752387623877238782387923880238812388223883238842388523886238872388823889238902389123892238932389423895238962389723898238992390023901239022390323904239052390623907239082390923910239112391223913239142391523916239172391823919239202392123922239232392423925239262392723928239292393023931239322393323934239352393623937239382393923940239412394223943239442394523946239472394823949239502395123952239532395423955239562395723958239592396023961239622396323964239652396623967239682396923970239712397223973239742397523976239772397823979239802398123982239832398423985239862398723988239892399023991239922399323994239952399623997239982399924000240012400224003240042400524006240072400824009240102401124012240132401424015240162401724018240192402024021240222402324024240252402624027240282402924030240312403224033240342403524036240372403824039240402404124042240432404424045240462404724048240492405024051240522405324054240552405624057240582405924060240612406224063240642406524066240672406824069240702407124072240732407424075240762407724078240792408024081240822408324084240852408624087240882408924090240912409224093240942409524096240972409824099241002410124102241032410424105241062410724108241092411024111241122411324114241152411624117241182411924120241212412224123241242412524126241272412824129241302413124132241332413424135241362413724138241392414024141241422414324144241452414624147241482414924150241512415224153241542415524156241572415824159241602416124162241632416424165241662416724168241692417024171241722417324174241752417624177241782417924180241812418224183241842418524186241872418824189241902419124192241932419424195241962419724198241992420024201242022420324204242052420624207242082420924210242112421224213242142421524216242172421824219242202422124222242232422424225242262422724228242292423024231242322423324234242352423624237242382423924240242412424224243242442424524246242472424824249242502425124252242532425424255242562425724258242592426024261242622426324264242652426624267242682426924270242712427224273242742427524276242772427824279242802428124282242832428424285242862428724288242892429024291242922429324294242952429624297242982429924300243012430224303243042430524306243072430824309243102431124312243132431424315243162431724318243192432024321243222432324324243252432624327243282432924330243312433224333243342433524336243372433824339243402434124342243432434424345243462434724348243492435024351243522435324354243552435624357243582435924360243612436224363243642436524366243672436824369243702437124372243732437424375243762437724378243792438024381243822438324384243852438624387243882438924390243912439224393243942439524396243972439824399244002440124402244032440424405244062440724408244092441024411244122441324414244152441624417244182441924420244212442224423244242442524426244272442824429244302443124432244332443424435244362443724438244392444024441244422444324444244452444624447244482444924450244512445224453244542445524456244572445824459244602446124462244632446424465244662446724468244692447024471244722447324474244752447624477244782447924480244812448224483244842448524486244872448824489244902449124492244932449424495244962449724498244992450024501245022450324504245052450624507245082450924510245112451224513245142451524516245172451824519245202452124522245232452424525245262452724528245292453024531245322453324534245352453624537245382453924540245412454224543245442454524546245472454824549245502455124552245532455424555245562455724558245592456024561245622456324564245652456624567245682456924570245712457224573245742457524576245772457824579245802458124582245832458424585245862458724588245892459024591245922459324594245952459624597245982459924600246012460224603246042460524606246072460824609246102461124612246132461424615246162461724618246192462024621246222462324624246252462624627246282462924630246312463224633246342463524636246372463824639246402464124642246432464424645246462464724648246492465024651246522465324654246552465624657246582465924660246612466224663246642466524666246672466824669246702467124672246732467424675246762467724678246792468024681246822468324684246852468624687246882468924690246912469224693246942469524696246972469824699247002470124702247032470424705247062470724708247092471024711247122471324714247152471624717247182471924720247212472224723247242472524726247272472824729247302473124732247332473424735247362473724738247392474024741247422474324744247452474624747247482474924750247512475224753247542475524756247572475824759247602476124762247632476424765247662476724768247692477024771247722477324774247752477624777247782477924780247812478224783247842478524786247872478824789247902479124792247932479424795247962479724798247992480024801248022480324804248052480624807248082480924810248112481224813248142481524816248172481824819248202482124822248232482424825248262482724828248292483024831248322483324834248352483624837248382483924840248412484224843248442484524846248472484824849248502485124852248532485424855248562485724858248592486024861248622486324864248652486624867248682486924870248712487224873248742487524876248772487824879248802488124882248832488424885248862488724888248892489024891248922489324894248952489624897248982489924900249012490224903249042490524906249072490824909249102491124912249132491424915249162491724918249192492024921249222492324924249252492624927249282492924930249312493224933249342493524936249372493824939249402494124942249432494424945249462494724948249492495024951249522495324954249552495624957249582495924960249612496224963249642496524966249672496824969249702497124972249732497424975249762497724978249792498024981249822498324984249852498624987249882498924990249912499224993249942499524996249972499824999250002500125002250032500425005250062500725008250092501025011250122501325014250152501625017250182501925020250212502225023250242502525026250272502825029250302503125032250332503425035250362503725038250392504025041250422504325044250452504625047250482504925050250512505225053250542505525056250572505825059250602506125062250632506425065250662506725068250692507025071250722507325074250752507625077250782507925080250812508225083250842508525086250872508825089250902509125092250932509425095250962509725098250992510025101251022510325104251052510625107251082510925110251112511225113251142511525116251172511825119251202512125122251232512425125251262512725128251292513025131251322513325134251352513625137251382513925140251412514225143251442514525146251472514825149251502515125152251532515425155251562515725158251592516025161251622516325164251652516625167251682516925170251712517225173251742517525176251772517825179251802518125182251832518425185251862518725188251892519025191251922519325194251952519625197251982519925200252012520225203252042520525206252072520825209252102521125212252132521425215252162521725218252192522025221252222522325224252252522625227252282522925230252312523225233252342523525236252372523825239252402524125242252432524425245252462524725248252492525025251252522525325254252552525625257252582525925260252612526225263252642526525266252672526825269252702527125272252732527425275252762527725278252792528025281252822528325284252852528625287252882528925290252912529225293252942529525296252972529825299253002530125302253032530425305253062530725308253092531025311253122531325314253152531625317253182531925320253212532225323253242532525326253272532825329253302533125332253332533425335253362533725338253392534025341253422534325344253452534625347253482534925350253512535225353253542535525356253572535825359253602536125362253632536425365253662536725368253692537025371253722537325374253752537625377253782537925380253812538225383253842538525386253872538825389253902539125392253932539425395253962539725398253992540025401254022540325404254052540625407254082540925410254112541225413254142541525416254172541825419254202542125422254232542425425254262542725428254292543025431254322543325434254352543625437254382543925440254412544225443254442544525446254472544825449254502545125452254532545425455254562545725458254592546025461254622546325464254652546625467254682546925470254712547225473254742547525476254772547825479254802548125482254832548425485254862548725488254892549025491254922549325494254952549625497254982549925500255012550225503255042550525506255072550825509255102551125512255132551425515255162551725518255192552025521255222552325524255252552625527255282552925530255312553225533255342553525536255372553825539255402554125542255432554425545255462554725548255492555025551255522555325554255552555625557255582555925560255612556225563255642556525566255672556825569255702557125572255732557425575255762557725578255792558025581255822558325584255852558625587255882558925590255912559225593255942559525596255972559825599256002560125602256032560425605256062560725608256092561025611256122561325614256152561625617256182561925620256212562225623256242562525626256272562825629256302563125632256332563425635256362563725638256392564025641256422564325644256452564625647256482564925650256512565225653256542565525656256572565825659256602566125662256632566425665256662566725668256692567025671256722567325674256752567625677256782567925680256812568225683256842568525686256872568825689256902569125692256932569425695256962569725698256992570025701257022570325704257052570625707257082570925710257112571225713257142571525716257172571825719257202572125722257232572425725257262572725728257292573025731257322573325734257352573625737257382573925740257412574225743257442574525746257472574825749257502575125752257532575425755257562575725758257592576025761257622576325764257652576625767257682576925770257712577225773257742577525776257772577825779257802578125782257832578425785257862578725788257892579025791257922579325794257952579625797257982579925800258012580225803258042580525806258072580825809258102581125812258132581425815258162581725818258192582025821258222582325824258252582625827258282582925830258312583225833258342583525836258372583825839258402584125842258432584425845258462584725848258492585025851258522585325854258552585625857258582585925860258612586225863258642586525866258672586825869258702587125872258732587425875258762587725878258792588025881258822588325884258852588625887258882588925890258912589225893258942589525896258972589825899259002590125902259032590425905259062590725908259092591025911259122591325914259152591625917259182591925920259212592225923259242592525926259272592825929259302593125932259332593425935259362593725938259392594025941259422594325944259452594625947259482594925950259512595225953259542595525956259572595825959259602596125962259632596425965259662596725968259692597025971259722597325974259752597625977259782597925980259812598225983259842598525986259872598825989259902599125992259932599425995259962599725998259992600026001260022600326004260052600626007260082600926010260112601226013260142601526016260172601826019260202602126022260232602426025260262602726028260292603026031260322603326034260352603626037260382603926040260412604226043260442604526046260472604826049260502605126052260532605426055260562605726058260592606026061260622606326064260652606626067260682606926070260712607226073260742607526076260772607826079260802608126082260832608426085260862608726088260892609026091260922609326094260952609626097260982609926100261012610226103261042610526106261072610826109261102611126112261132611426115261162611726118261192612026121261222612326124261252612626127261282612926130261312613226133261342613526136261372613826139261402614126142261432614426145261462614726148261492615026151261522615326154261552615626157261582615926160261612616226163261642616526166261672616826169261702617126172261732617426175261762617726178261792618026181261822618326184261852618626187261882618926190261912619226193261942619526196261972619826199262002620126202262032620426205262062620726208262092621026211262122621326214262152621626217262182621926220262212622226223262242622526226262272622826229262302623126232262332623426235262362623726238262392624026241262422624326244262452624626247262482624926250262512625226253262542625526256262572625826259262602626126262262632626426265262662626726268262692627026271262722627326274262752627626277262782627926280262812628226283262842628526286262872628826289262902629126292262932629426295262962629726298262992630026301263022630326304263052630626307263082630926310263112631226313263142631526316263172631826319263202632126322263232632426325263262632726328263292633026331263322633326334263352633626337263382633926340263412634226343263442634526346263472634826349263502635126352263532635426355263562635726358263592636026361263622636326364263652636626367263682636926370263712637226373263742637526376263772637826379263802638126382263832638426385263862638726388263892639026391263922639326394263952639626397263982639926400264012640226403264042640526406264072640826409264102641126412264132641426415264162641726418264192642026421264222642326424264252642626427264282642926430264312643226433264342643526436264372643826439264402644126442264432644426445264462644726448264492645026451264522645326454264552645626457264582645926460264612646226463264642646526466264672646826469264702647126472264732647426475264762647726478264792648026481264822648326484264852648626487264882648926490264912649226493264942649526496264972649826499265002650126502265032650426505265062650726508265092651026511265122651326514265152651626517265182651926520265212652226523265242652526526265272652826529265302653126532265332653426535265362653726538265392654026541265422654326544265452654626547265482654926550265512655226553265542655526556265572655826559265602656126562265632656426565265662656726568265692657026571265722657326574265752657626577265782657926580265812658226583265842658526586265872658826589265902659126592265932659426595265962659726598265992660026601266022660326604266052660626607266082660926610266112661226613266142661526616266172661826619266202662126622266232662426625266262662726628266292663026631266322663326634266352663626637266382663926640266412664226643266442664526646266472664826649266502665126652266532665426655266562665726658266592666026661266622666326664266652666626667266682666926670266712667226673266742667526676266772667826679266802668126682266832668426685266862668726688266892669026691266922669326694266952669626697266982669926700267012670226703267042670526706267072670826709267102671126712267132671426715267162671726718267192672026721267222672326724267252672626727267282672926730267312673226733267342673526736267372673826739267402674126742267432674426745267462674726748267492675026751267522675326754267552675626757267582675926760267612676226763267642676526766267672676826769267702677126772267732677426775267762677726778267792678026781267822678326784267852678626787267882678926790267912679226793267942679526796267972679826799268002680126802268032680426805268062680726808268092681026811268122681326814268152681626817268182681926820268212682226823268242682526826268272682826829268302683126832268332683426835268362683726838268392684026841268422684326844268452684626847268482684926850268512685226853268542685526856268572685826859268602686126862268632686426865268662686726868268692687026871268722687326874268752687626877268782687926880268812688226883268842688526886268872688826889268902689126892268932689426895268962689726898268992690026901269022690326904269052690626907269082690926910269112691226913269142691526916269172691826919269202692126922269232692426925269262692726928269292693026931269322693326934269352693626937269382693926940269412694226943269442694526946269472694826949269502695126952269532695426955269562695726958269592696026961269622696326964269652696626967269682696926970269712697226973269742697526976269772697826979269802698126982269832698426985269862698726988269892699026991269922699326994269952699626997269982699927000270012700227003270042700527006270072700827009270102701127012270132701427015270162701727018270192702027021270222702327024270252702627027270282702927030270312703227033270342703527036270372703827039270402704127042270432704427045270462704727048270492705027051270522705327054270552705627057270582705927060270612706227063270642706527066270672706827069270702707127072270732707427075270762707727078270792708027081270822708327084270852708627087270882708927090270912709227093270942709527096270972709827099271002710127102271032710427105271062710727108271092711027111271122711327114271152711627117271182711927120271212712227123271242712527126271272712827129271302713127132271332713427135271362713727138271392714027141271422714327144271452714627147271482714927150271512715227153271542715527156271572715827159271602716127162271632716427165271662716727168271692717027171271722717327174271752717627177271782717927180271812718227183271842718527186271872718827189271902719127192271932719427195271962719727198271992720027201272022720327204272052720627207272082720927210272112721227213272142721527216272172721827219272202722127222272232722427225272262722727228272292723027231272322723327234272352723627237272382723927240272412724227243272442724527246272472724827249272502725127252272532725427255272562725727258272592726027261272622726327264272652726627267272682726927270272712727227273272742727527276272772727827279272802728127282272832728427285272862728727288272892729027291272922729327294272952729627297272982729927300273012730227303273042730527306273072730827309273102731127312273132731427315273162731727318273192732027321273222732327324273252732627327273282732927330273312733227333273342733527336273372733827339273402734127342273432734427345273462734727348273492735027351273522735327354273552735627357273582735927360273612736227363273642736527366273672736827369273702737127372273732737427375273762737727378273792738027381273822738327384273852738627387273882738927390273912739227393273942739527396273972739827399274002740127402274032740427405274062740727408274092741027411274122741327414274152741627417274182741927420274212742227423274242742527426274272742827429274302743127432274332743427435274362743727438274392744027441274422744327444274452744627447274482744927450274512745227453274542745527456274572745827459274602746127462274632746427465274662746727468274692747027471274722747327474274752747627477274782747927480274812748227483274842748527486274872748827489274902749127492274932749427495274962749727498274992750027501275022750327504275052750627507275082750927510275112751227513275142751527516275172751827519275202752127522275232752427525275262752727528275292753027531275322753327534275352753627537275382753927540275412754227543275442754527546275472754827549275502755127552275532755427555275562755727558275592756027561275622756327564275652756627567275682756927570275712757227573275742757527576275772757827579275802758127582275832758427585275862758727588275892759027591275922759327594275952759627597275982759927600276012760227603276042760527606276072760827609276102761127612276132761427615276162761727618276192762027621276222762327624276252762627627276282762927630276312763227633276342763527636276372763827639276402764127642276432764427645276462764727648276492765027651276522765327654276552765627657276582765927660276612766227663276642766527666276672766827669276702767127672276732767427675276762767727678276792768027681276822768327684276852768627687276882768927690276912769227693276942769527696276972769827699277002770127702277032770427705277062770727708277092771027711277122771327714277152771627717277182771927720277212772227723277242772527726277272772827729277302773127732277332773427735277362773727738277392774027741277422774327744277452774627747277482774927750277512775227753277542775527756277572775827759277602776127762277632776427765277662776727768277692777027771277722777327774277752777627777277782777927780277812778227783277842778527786277872778827789277902779127792277932779427795277962779727798277992780027801278022780327804278052780627807278082780927810278112781227813278142781527816278172781827819278202782127822278232782427825278262782727828278292783027831278322783327834278352783627837278382783927840278412784227843278442784527846278472784827849278502785127852278532785427855278562785727858278592786027861278622786327864278652786627867278682786927870278712787227873278742787527876278772787827879278802788127882278832788427885278862788727888278892789027891278922789327894278952789627897278982789927900279012790227903279042790527906279072790827909279102791127912279132791427915279162791727918279192792027921279222792327924279252792627927279282792927930279312793227933279342793527936279372793827939279402794127942279432794427945279462794727948279492795027951279522795327954279552795627957279582795927960279612796227963279642796527966279672796827969279702797127972279732797427975279762797727978279792798027981279822798327984279852798627987279882798927990279912799227993279942799527996279972799827999280002800128002280032800428005280062800728008280092801028011280122801328014280152801628017280182801928020280212802228023280242802528026280272802828029280302803128032280332803428035280362803728038280392804028041280422804328044280452804628047280482804928050280512805228053280542805528056280572805828059280602806128062280632806428065280662806728068280692807028071280722807328074280752807628077280782807928080280812808228083280842808528086280872808828089280902809128092280932809428095280962809728098280992810028101281022810328104281052810628107281082810928110281112811228113281142811528116281172811828119281202812128122281232812428125281262812728128281292813028131281322813328134281352813628137281382813928140281412814228143281442814528146281472814828149281502815128152281532815428155281562815728158281592816028161281622816328164281652816628167281682816928170281712817228173281742817528176281772817828179281802818128182281832818428185281862818728188281892819028191281922819328194281952819628197281982819928200282012820228203282042820528206282072820828209282102821128212282132821428215282162821728218282192822028221282222822328224282252822628227282282822928230282312823228233282342823528236282372823828239282402824128242282432824428245282462824728248282492825028251282522825328254282552825628257282582825928260282612826228263282642826528266282672826828269282702827128272282732827428275282762827728278282792828028281282822828328284282852828628287282882828928290282912829228293282942829528296282972829828299283002830128302283032830428305283062830728308283092831028311283122831328314283152831628317283182831928320283212832228323283242832528326283272832828329283302833128332283332833428335283362833728338283392834028341283422834328344283452834628347283482834928350283512835228353283542835528356283572835828359283602836128362283632836428365283662836728368283692837028371283722837328374283752837628377283782837928380283812838228383283842838528386283872838828389283902839128392283932839428395283962839728398283992840028401284022840328404284052840628407284082840928410284112841228413284142841528416284172841828419284202842128422284232842428425284262842728428284292843028431284322843328434284352843628437284382843928440284412844228443284442844528446284472844828449284502845128452284532845428455284562845728458284592846028461284622846328464284652846628467284682846928470284712847228473284742847528476284772847828479284802848128482284832848428485284862848728488284892849028491284922849328494284952849628497284982849928500285012850228503285042850528506285072850828509285102851128512285132851428515285162851728518285192852028521285222852328524285252852628527285282852928530285312853228533285342853528536285372853828539285402854128542285432854428545285462854728548285492855028551285522855328554285552855628557285582855928560285612856228563285642856528566285672856828569285702857128572285732857428575285762857728578285792858028581285822858328584285852858628587285882858928590285912859228593285942859528596285972859828599286002860128602286032860428605286062860728608286092861028611286122861328614286152861628617286182861928620286212862228623286242862528626286272862828629286302863128632286332863428635286362863728638286392864028641286422864328644286452864628647286482864928650286512865228653286542865528656286572865828659286602866128662286632866428665286662866728668286692867028671286722867328674286752867628677286782867928680286812868228683286842868528686286872868828689286902869128692286932869428695286962869728698286992870028701287022870328704287052870628707287082870928710287112871228713287142871528716287172871828719287202872128722287232872428725287262872728728287292873028731287322873328734287352873628737287382873928740287412874228743287442874528746287472874828749287502875128752287532875428755287562875728758287592876028761287622876328764287652876628767287682876928770287712877228773287742877528776287772877828779287802878128782287832878428785287862878728788287892879028791287922879328794287952879628797287982879928800288012880228803288042880528806288072880828809288102881128812288132881428815288162881728818288192882028821288222882328824288252882628827288282882928830288312883228833288342883528836288372883828839288402884128842288432884428845288462884728848288492885028851288522885328854288552885628857288582885928860288612886228863288642886528866288672886828869288702887128872288732887428875288762887728878288792888028881288822888328884288852888628887288882888928890288912889228893288942889528896288972889828899289002890128902289032890428905289062890728908289092891028911289122891328914289152891628917289182891928920289212892228923289242892528926289272892828929289302893128932289332893428935289362893728938289392894028941289422894328944289452894628947289482894928950289512895228953289542895528956289572895828959289602896128962289632896428965289662896728968289692897028971289722897328974289752897628977289782897928980289812898228983289842898528986289872898828989289902899128992289932899428995289962899728998289992900029001290022900329004290052900629007290082900929010290112901229013290142901529016290172901829019290202902129022290232902429025290262902729028290292903029031290322903329034290352903629037290382903929040290412904229043290442904529046290472904829049290502905129052290532905429055290562905729058290592906029061290622906329064290652906629067290682906929070290712907229073290742907529076290772907829079290802908129082290832908429085290862908729088290892909029091290922909329094290952909629097290982909929100291012910229103291042910529106291072910829109291102911129112291132911429115291162911729118291192912029121291222912329124291252912629127291282912929130291312913229133291342913529136291372913829139291402914129142291432914429145291462914729148291492915029151291522915329154291552915629157291582915929160291612916229163291642916529166291672916829169291702917129172291732917429175291762917729178291792918029181291822918329184291852918629187291882918929190291912919229193291942919529196291972919829199292002920129202292032920429205292062920729208292092921029211292122921329214292152921629217292182921929220292212922229223292242922529226292272922829229292302923129232292332923429235292362923729238292392924029241292422924329244292452924629247292482924929250292512925229253292542925529256292572925829259292602926129262292632926429265292662926729268292692927029271292722927329274292752927629277292782927929280292812928229283292842928529286292872928829289292902929129292292932929429295292962929729298292992930029301293022930329304293052930629307293082930929310293112931229313293142931529316293172931829319293202932129322293232932429325293262932729328293292933029331293322933329334293352933629337293382933929340293412934229343293442934529346293472934829349293502935129352293532935429355293562935729358293592936029361293622936329364293652936629367293682936929370293712937229373293742937529376293772937829379293802938129382293832938429385293862938729388293892939029391293922939329394293952939629397293982939929400294012940229403294042940529406294072940829409294102941129412294132941429415294162941729418294192942029421294222942329424294252942629427294282942929430294312943229433294342943529436294372943829439294402944129442294432944429445294462944729448294492945029451294522945329454294552945629457294582945929460294612946229463294642946529466294672946829469294702947129472294732947429475294762947729478294792948029481294822948329484294852948629487294882948929490294912949229493294942949529496294972949829499295002950129502295032950429505295062950729508295092951029511295122951329514295152951629517295182951929520295212952229523295242952529526295272952829529295302953129532295332953429535295362953729538295392954029541295422954329544295452954629547295482954929550295512955229553295542955529556295572955829559295602956129562295632956429565295662956729568295692957029571295722957329574295752957629577295782957929580295812958229583295842958529586295872958829589295902959129592295932959429595295962959729598295992960029601296022960329604296052960629607296082960929610296112961229613296142961529616296172961829619296202962129622296232962429625296262962729628296292963029631296322963329634296352963629637296382963929640296412964229643296442964529646296472964829649296502965129652296532965429655296562965729658296592966029661296622966329664296652966629667296682966929670296712967229673296742967529676296772967829679296802968129682296832968429685296862968729688296892969029691296922969329694296952969629697296982969929700297012970229703297042970529706297072970829709297102971129712297132971429715297162971729718297192972029721297222972329724297252972629727297282972929730297312973229733297342973529736297372973829739297402974129742297432974429745297462974729748297492975029751297522975329754297552975629757297582975929760297612976229763297642976529766297672976829769297702977129772297732977429775297762977729778297792978029781297822978329784297852978629787297882978929790297912979229793297942979529796297972979829799298002980129802298032980429805298062980729808298092981029811298122981329814298152981629817298182981929820298212982229823298242982529826298272982829829298302983129832298332983429835298362983729838298392984029841298422984329844298452984629847298482984929850298512985229853298542985529856298572985829859298602986129862298632986429865298662986729868298692987029871298722987329874298752987629877298782987929880298812988229883298842988529886298872988829889298902989129892298932989429895298962989729898298992990029901299022990329904299052990629907299082990929910299112991229913299142991529916299172991829919299202992129922299232992429925299262992729928299292993029931299322993329934299352993629937299382993929940299412994229943299442994529946299472994829949299502995129952299532995429955299562995729958299592996029961299622996329964299652996629967299682996929970299712997229973299742997529976299772997829979299802998129982299832998429985299862998729988299892999029991299922999329994299952999629997299982999930000300013000230003300043000530006300073000830009300103001130012300133001430015300163001730018300193002030021300223002330024300253002630027300283002930030300313003230033300343003530036300373003830039300403004130042300433004430045300463004730048300493005030051300523005330054300553005630057300583005930060300613006230063300643006530066300673006830069300703007130072300733007430075300763007730078300793008030081300823008330084300853008630087300883008930090300913009230093300943009530096300973009830099301003010130102301033010430105301063010730108301093011030111301123011330114301153011630117301183011930120301213012230123301243012530126301273012830129301303013130132301333013430135301363013730138301393014030141301423014330144301453014630147301483014930150301513015230153301543015530156301573015830159301603016130162301633016430165301663016730168301693017030171301723017330174301753017630177301783017930180301813018230183301843018530186301873018830189301903019130192301933019430195301963019730198301993020030201302023020330204302053020630207302083020930210302113021230213302143021530216302173021830219302203022130222302233022430225302263022730228302293023030231302323023330234302353023630237302383023930240302413024230243302443024530246302473024830249302503025130252302533025430255302563025730258302593026030261302623026330264302653026630267302683026930270302713027230273302743027530276302773027830279302803028130282302833028430285302863028730288302893029030291302923029330294302953029630297302983029930300303013030230303303043030530306303073030830309303103031130312303133031430315303163031730318303193032030321303223032330324303253032630327303283032930330303313033230333303343033530336303373033830339303403034130342303433034430345303463034730348303493035030351303523035330354303553035630357303583035930360303613036230363303643036530366303673036830369303703037130372303733037430375303763037730378303793038030381303823038330384303853038630387303883038930390303913039230393303943039530396303973039830399304003040130402304033040430405304063040730408304093041030411304123041330414304153041630417304183041930420304213042230423304243042530426304273042830429304303043130432304333043430435304363043730438304393044030441304423044330444304453044630447304483044930450304513045230453304543045530456304573045830459304603046130462304633046430465304663046730468304693047030471304723047330474304753047630477304783047930480304813048230483304843048530486304873048830489304903049130492304933049430495304963049730498304993050030501305023050330504305053050630507305083050930510305113051230513305143051530516305173051830519305203052130522305233052430525305263052730528305293053030531305323053330534305353053630537305383053930540305413054230543305443054530546305473054830549305503055130552305533055430555305563055730558305593056030561305623056330564305653056630567305683056930570305713057230573305743057530576305773057830579305803058130582305833058430585305863058730588305893059030591305923059330594305953059630597305983059930600306013060230603306043060530606306073060830609306103061130612306133061430615306163061730618306193062030621306223062330624306253062630627306283062930630306313063230633306343063530636306373063830639306403064130642306433064430645306463064730648306493065030651306523065330654306553065630657306583065930660306613066230663306643066530666306673066830669306703067130672306733067430675306763067730678306793068030681306823068330684306853068630687306883068930690306913069230693306943069530696306973069830699307003070130702307033070430705307063070730708307093071030711307123071330714307153071630717307183071930720307213072230723307243072530726307273072830729307303073130732307333073430735307363073730738307393074030741307423074330744307453074630747307483074930750307513075230753307543075530756307573075830759307603076130762307633076430765307663076730768307693077030771307723077330774307753077630777307783077930780307813078230783307843078530786307873078830789307903079130792307933079430795307963079730798307993080030801308023080330804308053080630807308083080930810308113081230813308143081530816308173081830819308203082130822308233082430825308263082730828308293083030831308323083330834308353083630837308383083930840308413084230843308443084530846308473084830849308503085130852308533085430855308563085730858308593086030861308623086330864308653086630867308683086930870308713087230873308743087530876308773087830879308803088130882308833088430885308863088730888308893089030891308923089330894308953089630897308983089930900309013090230903309043090530906309073090830909309103091130912309133091430915309163091730918309193092030921309223092330924309253092630927309283092930930309313093230933309343093530936309373093830939309403094130942309433094430945309463094730948309493095030951309523095330954309553095630957309583095930960309613096230963309643096530966309673096830969309703097130972309733097430975309763097730978309793098030981309823098330984309853098630987309883098930990309913099230993309943099530996309973099830999310003100131002310033100431005310063100731008310093101031011310123101331014310153101631017310183101931020310213102231023310243102531026310273102831029310303103131032310333103431035310363103731038310393104031041310423104331044310453104631047310483104931050310513105231053310543105531056310573105831059310603106131062310633106431065310663106731068310693107031071310723107331074310753107631077310783107931080310813108231083310843108531086310873108831089310903109131092310933109431095310963109731098310993110031101311023110331104311053110631107311083110931110311113111231113311143111531116311173111831119311203112131122311233112431125311263112731128311293113031131311323113331134311353113631137311383113931140311413114231143311443114531146311473114831149311503115131152311533115431155311563115731158311593116031161311623116331164311653116631167311683116931170311713117231173311743117531176311773117831179311803118131182311833118431185311863118731188311893119031191311923119331194311953119631197311983119931200312013120231203312043120531206312073120831209312103121131212312133121431215312163121731218312193122031221312223122331224312253122631227312283122931230312313123231233312343123531236312373123831239312403124131242312433124431245312463124731248312493125031251312523125331254312553125631257312583125931260312613126231263312643126531266312673126831269312703127131272312733127431275312763127731278312793128031281312823128331284312853128631287312883128931290312913129231293312943129531296312973129831299313003130131302313033130431305313063130731308313093131031311313123131331314313153131631317313183131931320313213132231323313243132531326313273132831329313303133131332313333133431335313363133731338313393134031341313423134331344313453134631347313483134931350313513135231353313543135531356313573135831359313603136131362313633136431365313663136731368313693137031371313723137331374313753137631377313783137931380313813138231383313843138531386313873138831389313903139131392313933139431395313963139731398313993140031401314023140331404314053140631407314083140931410314113141231413314143141531416314173141831419314203142131422314233142431425314263142731428314293143031431314323143331434314353143631437314383143931440314413144231443314443144531446314473144831449314503145131452314533145431455314563145731458314593146031461314623146331464314653146631467314683146931470314713147231473314743147531476314773147831479314803148131482314833148431485314863148731488314893149031491314923149331494314953149631497314983149931500315013150231503315043150531506315073150831509315103151131512315133151431515315163151731518315193152031521315223152331524315253152631527315283152931530315313153231533315343153531536315373153831539315403154131542315433154431545315463154731548315493155031551315523155331554315553155631557315583155931560315613156231563315643156531566315673156831569315703157131572315733157431575315763157731578315793158031581315823158331584315853158631587315883158931590315913159231593315943159531596315973159831599316003160131602316033160431605316063160731608316093161031611316123161331614316153161631617316183161931620316213162231623316243162531626316273162831629316303163131632316333163431635316363163731638316393164031641316423164331644316453164631647316483164931650316513165231653316543165531656316573165831659316603166131662316633166431665316663166731668316693167031671316723167331674316753167631677316783167931680316813168231683316843168531686316873168831689316903169131692316933169431695316963169731698316993170031701317023170331704317053170631707317083170931710317113171231713317143171531716317173171831719317203172131722317233172431725317263172731728317293173031731317323173331734317353173631737317383173931740317413174231743317443174531746317473174831749317503175131752317533175431755317563175731758317593176031761317623176331764317653176631767317683176931770317713177231773317743177531776317773177831779317803178131782317833178431785317863178731788317893179031791317923179331794317953179631797317983179931800318013180231803318043180531806318073180831809318103181131812318133181431815318163181731818318193182031821318223182331824318253182631827318283182931830318313183231833318343183531836318373183831839318403184131842318433184431845318463184731848318493185031851318523185331854318553185631857318583185931860318613186231863318643186531866318673186831869318703187131872318733187431875318763187731878318793188031881318823188331884318853188631887318883188931890318913189231893318943189531896318973189831899319003190131902319033190431905319063190731908319093191031911319123191331914319153191631917319183191931920319213192231923319243192531926319273192831929319303193131932319333193431935319363193731938319393194031941319423194331944319453194631947319483194931950319513195231953319543195531956319573195831959319603196131962319633196431965319663196731968319693197031971319723197331974319753197631977319783197931980319813198231983319843198531986319873198831989319903199131992319933199431995319963199731998319993200032001320023200332004320053200632007320083200932010320113201232013320143201532016320173201832019320203202132022320233202432025320263202732028320293203032031320323203332034320353203632037320383203932040320413204232043320443204532046320473204832049320503205132052320533205432055320563205732058320593206032061320623206332064320653206632067320683206932070320713207232073320743207532076320773207832079320803208132082320833208432085320863208732088320893209032091320923209332094320953209632097320983209932100321013210232103321043210532106321073210832109321103211132112321133211432115321163211732118321193212032121321223212332124321253212632127321283212932130321313213232133321343213532136321373213832139321403214132142321433214432145321463214732148321493215032151321523215332154321553215632157321583215932160321613216232163321643216532166321673216832169321703217132172321733217432175321763217732178321793218032181321823218332184321853218632187321883218932190321913219232193321943219532196321973219832199322003220132202322033220432205322063220732208322093221032211322123221332214322153221632217322183221932220322213222232223322243222532226322273222832229322303223132232322333223432235322363223732238322393224032241322423224332244322453224632247322483224932250322513225232253322543225532256322573225832259322603226132262322633226432265322663226732268322693227032271322723227332274322753227632277322783227932280322813228232283322843228532286322873228832289322903229132292322933229432295322963229732298322993230032301323023230332304323053230632307323083230932310323113231232313323143231532316323173231832319323203232132322323233232432325323263232732328323293233032331323323233332334323353233632337323383233932340323413234232343323443234532346323473234832349323503235132352323533235432355323563235732358323593236032361323623236332364323653236632367323683236932370323713237232373323743237532376323773237832379323803238132382323833238432385323863238732388323893239032391323923239332394323953239632397323983239932400324013240232403324043240532406324073240832409324103241132412324133241432415324163241732418324193242032421324223242332424324253242632427324283242932430324313243232433324343243532436324373243832439324403244132442324433244432445324463244732448324493245032451324523245332454324553245632457324583245932460324613246232463324643246532466324673246832469324703247132472324733247432475324763247732478324793248032481324823248332484324853248632487324883248932490324913249232493324943249532496324973249832499325003250132502325033250432505325063250732508325093251032511325123251332514325153251632517325183251932520325213252232523325243252532526325273252832529325303253132532325333253432535325363253732538325393254032541325423254332544325453254632547325483254932550325513255232553325543255532556325573255832559325603256132562325633256432565325663256732568325693257032571325723257332574325753257632577325783257932580325813258232583325843258532586325873258832589325903259132592325933259432595325963259732598325993260032601326023260332604326053260632607326083260932610326113261232613326143261532616326173261832619326203262132622326233262432625326263262732628326293263032631326323263332634326353263632637326383263932640326413264232643326443264532646326473264832649326503265132652326533265432655326563265732658326593266032661326623266332664326653266632667326683266932670326713267232673326743267532676326773267832679326803268132682326833268432685326863268732688326893269032691326923269332694326953269632697326983269932700327013270232703327043270532706327073270832709327103271132712327133271432715327163271732718327193272032721327223272332724327253272632727327283272932730327313273232733327343273532736327373273832739327403274132742327433274432745327463274732748327493275032751327523275332754327553275632757327583275932760327613276232763327643276532766327673276832769327703277132772327733277432775327763277732778327793278032781327823278332784327853278632787327883278932790327913279232793327943279532796327973279832799328003280132802328033280432805328063280732808328093281032811328123281332814328153281632817328183281932820328213282232823328243282532826328273282832829328303283132832328333283432835328363283732838328393284032841328423284332844328453284632847328483284932850328513285232853328543285532856328573285832859328603286132862328633286432865328663286732868328693287032871328723287332874328753287632877328783287932880328813288232883328843288532886328873288832889328903289132892328933289432895328963289732898328993290032901329023290332904329053290632907329083290932910329113291232913329143291532916329173291832919329203292132922329233292432925329263292732928329293293032931329323293332934329353293632937329383293932940329413294232943329443294532946329473294832949329503295132952329533295432955329563295732958329593296032961329623296332964329653296632967329683296932970329713297232973329743297532976329773297832979329803298132982329833298432985329863298732988329893299032991329923299332994329953299632997329983299933000330013300233003330043300533006330073300833009330103301133012330133301433015330163301733018330193302033021330223302333024330253302633027330283302933030330313303233033330343303533036330373303833039330403304133042330433304433045330463304733048330493305033051330523305333054330553305633057330583305933060330613306233063330643306533066330673306833069330703307133072330733307433075330763307733078330793308033081330823308333084330853308633087330883308933090330913309233093330943309533096330973309833099331003310133102331033310433105331063310733108331093311033111331123311333114331153311633117331183311933120331213312233123331243312533126331273312833129331303313133132331333313433135331363313733138331393314033141331423314333144331453314633147331483314933150331513315233153331543315533156331573315833159331603316133162331633316433165331663316733168331693317033171331723317333174331753317633177331783317933180331813318233183331843318533186331873318833189331903319133192331933319433195331963319733198331993320033201332023320333204332053320633207332083320933210332113321233213332143321533216332173321833219332203322133222332233322433225332263322733228332293323033231332323323333234332353323633237332383323933240332413324233243332443324533246332473324833249332503325133252332533325433255332563325733258332593326033261332623326333264332653326633267332683326933270332713327233273332743327533276332773327833279332803328133282332833328433285332863328733288332893329033291332923329333294332953329633297332983329933300333013330233303333043330533306333073330833309333103331133312333133331433315333163331733318333193332033321333223332333324333253332633327333283332933330333313333233333333343333533336333373333833339333403334133342333433334433345333463334733348333493335033351333523335333354333553335633357333583335933360333613336233363333643336533366333673336833369333703337133372333733337433375333763337733378333793338033381333823338333384333853338633387333883338933390333913339233393333943339533396333973339833399334003340133402334033340433405334063340733408334093341033411334123341333414334153341633417334183341933420334213342233423334243342533426334273342833429334303343133432334333343433435334363343733438334393344033441334423344333444334453344633447334483344933450334513345233453334543345533456334573345833459334603346133462334633346433465334663346733468
  1. {
  2. This file is part of the Free Component Library (FCL)
  3. Copyright (c) 2018 by Michael Van Canneyt
  4. Unit tests for Pascal-to-Javascript converter class.
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************
  11. Examples:
  12. ./testpas2js --suite=TTestModule.TestEmptyProgram
  13. ./testpas2js --suite=TTestModule.TestEmptyUnit
  14. }
  15. unit TCModules;
  16. {$mode objfpc}{$H+}
  17. interface
  18. uses
  19. Classes, SysUtils, fpcunit, testregistry, contnrs,
  20. jstree, jswriter, jsbase,
  21. PasTree, PScanner, PasResolver, PParser, PasResolveEval,
  22. FPPas2Js;
  23. const
  24. // default parser+scanner options
  25. po_tcmodules = po_Pas2js+[po_KeepScannerError];
  26. co_tcmodules = [];
  27. type
  28. TSrcMarkerKind = (
  29. mkLabel,
  30. mkResolverReference,
  31. mkDirectReference
  32. );
  33. PSrcMarker = ^TSrcMarker;
  34. TSrcMarker = record
  35. Kind: TSrcMarkerKind;
  36. Filename: string;
  37. Row: integer;
  38. StartCol, EndCol: integer; // token start, end column
  39. Identifier: string;
  40. Next: PSrcMarker;
  41. end;
  42. TSystemUnitPart = (
  43. supTObject,
  44. supTVarRec,
  45. supTypeInfo,
  46. supTInterfacedObject,
  47. supWriteln
  48. );
  49. TSystemUnitParts = set of TSystemUnitPart;
  50. { TTestHintMessage }
  51. TTestHintMessage = class
  52. public
  53. Id: int64;
  54. MsgType: TMessageType;
  55. MsgNumber: integer;
  56. Msg: string;
  57. SourcePos: TPasSourcePos;
  58. end;
  59. { TTestPasParser }
  60. TTestPasParser = Class(TPasParser)
  61. end;
  62. TOnFindUnit = function(const aUnitName: String): TPasModule of object;
  63. { TTestEnginePasResolver }
  64. TTestEnginePasResolver = class(TPas2JsResolver)
  65. private
  66. FFilename: string;
  67. FModule: TPasModule;
  68. FOnFindUnit: TOnFindUnit;
  69. FParser: TTestPasParser;
  70. FStreamResolver: TStreamResolver;
  71. FScanner: TPas2jsPasScanner;
  72. FSource: string;
  73. public
  74. destructor Destroy; override;
  75. function FindUnit(const AName, InFilename: String; NameExpr,
  76. InFileExpr: TPasExpr): TPasModule; override;
  77. procedure UsedInterfacesFinished(Section: TPasSection); override;
  78. property OnFindUnit: TOnFindUnit read FOnFindUnit write FOnFindUnit;
  79. property Filename: string read FFilename write FFilename;
  80. property StreamResolver: TStreamResolver read FStreamResolver write FStreamResolver;
  81. property Scanner: TPas2jsPasScanner read FScanner write FScanner;
  82. property Parser: TTestPasParser read FParser write FParser;
  83. property Source: string read FSource write FSource;
  84. property Module: TPasModule read FModule;
  85. end;
  86. { TCustomTestModule }
  87. TCustomTestModule = Class(TTestCase)
  88. private
  89. FConverter: TPasToJSConverter;
  90. FEngine: TTestEnginePasResolver;
  91. FExpectedErrorClass: ExceptClass;
  92. FExpectedErrorMsg: string;
  93. FExpectedErrorNumber: integer;
  94. FFilename: string;
  95. FFileResolver: TStreamResolver;
  96. FHub: TPas2JSResolverHub;
  97. FJSImplementationUses: TJSArrayLiteral;
  98. FJSInitBody: TJSFunctionBody;
  99. FJSImplentationUses: TJSArrayLiteral;
  100. FJSInterfaceUses: TJSArrayLiteral;
  101. FJSModule: TJSSourceElements;
  102. FJSModuleSrc: TJSSourceElements;
  103. FJSSource: TStringList;
  104. FModule: TPasModule;
  105. FJSModuleCallArgs: TJSArguments;
  106. FModules: TObjectList;// list of TTestEnginePasResolver
  107. FParser: TTestPasParser;
  108. FPasProgram: TPasProgram;
  109. FHintMsgs: TObjectList; // list of TTestHintMessage
  110. FHintMsgsGood: TFPList; // list of TTestHintMessage marked as expected
  111. FJSRegModuleCall: TJSCallExpression;
  112. FScanner: TPas2jsPasScanner;
  113. FSkipTests: boolean;
  114. FSource: TStringList;
  115. FFirstPasStatement: TPasImplBlock;
  116. FWithTypeInfo: boolean;
  117. {$IFDEF EnablePasTreeGlobalRefCount}
  118. FElementRefCountAtSetup: int64;
  119. {$ENDIF}
  120. function GetMsgCount: integer;
  121. function GetMsgs(Index: integer): TTestHintMessage;
  122. function GetResolverCount: integer;
  123. function GetResolvers(Index: integer): TTestEnginePasResolver;
  124. function OnPasResolverFindUnit(const aUnitName: String): TPasModule;
  125. procedure OnParserLog(Sender: TObject; const Msg: String);
  126. procedure OnPasResolverLog(Sender: TObject; const Msg: String);
  127. procedure OnScannerLog(Sender: TObject; const Msg: String);
  128. procedure SetWithTypeInfo(const AValue: boolean);
  129. protected
  130. procedure SetUp; override;
  131. function CreateConverter: TPasToJSConverter; virtual;
  132. function LoadUnit(const aUnitName: String): TPasModule;
  133. procedure InitScanner(aScanner: TPas2jsPasScanner); virtual;
  134. procedure TearDown; override;
  135. Procedure Add(Line: string); virtual;
  136. Procedure Add(const Lines: array of string);
  137. Procedure StartParsing; virtual;
  138. procedure ParseModuleQueue; virtual;
  139. procedure ParseModule; virtual;
  140. procedure ParseProgram; virtual;
  141. procedure ParseUnit; virtual;
  142. protected
  143. function FindModuleWithFilename(aFilename: string): TTestEnginePasResolver; virtual;
  144. function AddModule(aFilename: string): TTestEnginePasResolver; virtual;
  145. function AddModuleWithSrc(aFilename, Src: string): TTestEnginePasResolver; virtual;
  146. function AddModuleWithIntfImplSrc(aFilename, InterfaceSrc,
  147. ImplementationSrc: string): TTestEnginePasResolver; virtual;
  148. procedure AddSystemUnit(Parts: TSystemUnitParts = []); virtual;
  149. procedure StartProgram(NeedSystemUnit: boolean; SystemUnitParts: TSystemUnitParts = []); virtual;
  150. procedure StartUnit(NeedSystemUnit: boolean; SystemUnitParts: TSystemUnitParts = []); virtual;
  151. procedure ConvertModule; virtual;
  152. procedure ConvertProgram; virtual;
  153. procedure ConvertUnit; virtual;
  154. function ConvertJSModuleToString(El: TJSElement): string; virtual;
  155. procedure CheckDottedIdentifier(Msg: string; El: TJSElement; DottedName: string);
  156. function GetDottedIdentifier(El: TJSElement): string;
  157. procedure CheckSource(Msg,Statements: String; InitStatements: string = '';
  158. ImplStatements: string = ''); virtual;
  159. procedure CheckDiff(Msg, Expected, Actual: string); virtual;
  160. procedure CheckUnit(Filename, ExpectedSrc: string); virtual;
  161. procedure CheckHint(MsgType: TMessageType; MsgNumber: integer;
  162. Msg: string; Marker: PSrcMarker = nil); virtual;
  163. procedure CheckResolverUnexpectedHints(WithSourcePos: boolean = false); virtual;
  164. procedure SetExpectedScannerError(Msg: string; MsgNumber: integer);
  165. procedure SetExpectedParserError(Msg: string; MsgNumber: integer);
  166. procedure SetExpectedPasResolverError(Msg: string; MsgNumber: integer);
  167. procedure SetExpectedConverterError(Msg: string; MsgNumber: integer);
  168. function IsErrorExpected(E: Exception): boolean;
  169. procedure HandleScannerError(E: EScannerError);
  170. procedure HandleParserError(E: EParserError);
  171. procedure HandlePasResolveError(E: EPasResolve);
  172. procedure HandlePas2JSError(E: EPas2JS);
  173. procedure HandleException(E: Exception);
  174. procedure FailException(E: Exception);
  175. procedure WriteSources(const aFilename: string; aRow, aCol: integer);
  176. function IndexOfResolver(const Filename: string): integer;
  177. function GetResolver(const Filename: string): TTestEnginePasResolver;
  178. function GetDefaultNamespace: string;
  179. property PasProgram: TPasProgram Read FPasProgram;
  180. property Resolvers[Index: integer]: TTestEnginePasResolver read GetResolvers;
  181. property ResolverCount: integer read GetResolverCount;
  182. property Engine: TTestEnginePasResolver read FEngine;
  183. property Filename: string read FFilename;
  184. Property Module: TPasModule Read FModule;
  185. property FirstPasStatement: TPasImplBlock read FFirstPasStatement;
  186. property Converter: TPasToJSConverter read FConverter;
  187. property JSSource: TStringList read FJSSource;
  188. property JSModule: TJSSourceElements read FJSModule;
  189. property JSRegModuleCall: TJSCallExpression read FJSRegModuleCall;
  190. property JSModuleCallArgs: TJSArguments read FJSModuleCallArgs;
  191. property JSImplementationUses: TJSArrayLiteral read FJSImplementationUses;
  192. property JSInterfaceUses: TJSArrayLiteral read FJSInterfaceUses;
  193. property JSModuleSrc: TJSSourceElements read FJSModuleSrc;
  194. property JSInitBody: TJSFunctionBody read FJSInitBody;
  195. property ExpectedErrorClass: ExceptClass read FExpectedErrorClass write FExpectedErrorClass;
  196. property ExpectedErrorMsg: string read FExpectedErrorMsg write FExpectedErrorMsg;
  197. property ExpectedErrorNumber: integer read FExpectedErrorNumber write FExpectedErrorNumber;
  198. property SkipTests: boolean read FSkipTests write FSkipTests;
  199. public
  200. constructor Create; override;
  201. destructor Destroy; override;
  202. property Hub: TPas2JSResolverHub read FHub;
  203. property Source: TStringList read FSource;
  204. property FileResolver: TStreamResolver read FFileResolver;
  205. property Scanner: TPas2jsPasScanner read FScanner;
  206. property Parser: TTestPasParser read FParser;
  207. property MsgCount: integer read GetMsgCount;
  208. property Msgs[Index: integer]: TTestHintMessage read GetMsgs;
  209. property WithTypeInfo: boolean read FWithTypeInfo write SetWithTypeInfo;
  210. end;
  211. { TTestModule }
  212. TTestModule = class(TCustomTestModule)
  213. Published
  214. Procedure TestReservedWords;
  215. // program, units, includes
  216. Procedure TestEmptyProgram;
  217. Procedure TestEmptyProgramUseStrict;
  218. Procedure TestEmptyUnit;
  219. Procedure TestEmptyUnitUseStrict;
  220. Procedure TestDottedUnitNames;
  221. Procedure TestDottedUnitNameImpl;
  222. Procedure TestDottedUnitExpr;
  223. Procedure Test_ModeFPCFail;
  224. Procedure Test_ModeSwitchCBlocksFail;
  225. Procedure TestUnit_UseSystem;
  226. Procedure TestUnit_Intf1Impl2Intf1;
  227. Procedure TestIncludeVersion;
  228. // vars/const
  229. Procedure TestVarInt;
  230. Procedure TestVarBaseTypes;
  231. Procedure TestBaseTypeSingleFail;
  232. Procedure TestBaseTypeExtendedFail;
  233. Procedure TestConstBaseTypes;
  234. Procedure TestUnitImplVars;
  235. Procedure TestUnitImplConsts;
  236. Procedure TestUnitImplRecord;
  237. Procedure TestRenameJSNameConflict;
  238. Procedure TestLocalConst;
  239. Procedure TestVarExternal;
  240. Procedure TestVarExternalOtherUnit;
  241. Procedure TestVarAbsoluteFail;
  242. Procedure TestConstExternal;
  243. // numbers
  244. Procedure TestDouble;
  245. Procedure TestInteger;
  246. Procedure TestIntegerRange;
  247. Procedure TestIntegerTypecasts;
  248. Procedure TestInteger_BitwiseShrNativeInt;
  249. Procedure TestInteger_BitwiseShlNativeInt;
  250. Procedure TestInteger_SystemFunc;
  251. Procedure TestCurrency;
  252. Procedure TestForBoolDo;
  253. Procedure TestForIntDo;
  254. Procedure TestForIntInDo;
  255. // strings
  256. Procedure TestCharConst;
  257. Procedure TestChar_Compare;
  258. Procedure TestChar_BuiltInProcs;
  259. Procedure TestStringConst;
  260. Procedure TestStringConst_InvalidUTF16;
  261. Procedure TestStringConstSurrogate;
  262. Procedure TestString_Length;
  263. Procedure TestString_Compare;
  264. Procedure TestString_SetLength;
  265. Procedure TestString_CharAt;
  266. Procedure TestStringHMinusFail;
  267. Procedure TestStr;
  268. Procedure TestBaseType_AnsiStringFail;
  269. Procedure TestBaseType_WideStringFail;
  270. Procedure TestBaseType_ShortStringFail;
  271. Procedure TestBaseType_RawByteStringFail;
  272. Procedure TestTypeShortstring_Fail;
  273. Procedure TestCharSet_Custom;
  274. Procedure TestWideChar;
  275. Procedure TestForCharDo;
  276. Procedure TestForCharInDo;
  277. // alias types
  278. Procedure TestAliasTypeRef;
  279. Procedure TestTypeCast_BaseTypes;
  280. Procedure TestTypeCast_AliasBaseTypes;
  281. // functions
  282. Procedure TestEmptyProc;
  283. Procedure TestProcOneParam;
  284. Procedure TestFunctionWithoutParams;
  285. Procedure TestProcedureWithoutParams;
  286. Procedure TestPrgProcVar;
  287. Procedure TestProcTwoArgs;
  288. Procedure TestProc_DefaultValue;
  289. Procedure TestUnitProcVar;
  290. Procedure TestImplProc;
  291. Procedure TestFunctionResult;
  292. Procedure TestNestedProc;
  293. Procedure TestNestedProc_ResultString;
  294. Procedure TestForwardProc;
  295. Procedure TestNestedForwardProc;
  296. Procedure TestAssignFunctionResult;
  297. Procedure TestFunctionResultInCondition;
  298. Procedure TestFunctionResultInForLoop;
  299. Procedure TestFunctionResultInTypeCast;
  300. Procedure TestExit;
  301. Procedure TestExit_ResultInFinally;
  302. Procedure TestBreak;
  303. Procedure TestBreakAsVar;
  304. Procedure TestContinue;
  305. Procedure TestProc_External;
  306. Procedure TestProc_ExternalOtherUnit;
  307. Procedure TestProc_Asm;
  308. Procedure TestProc_AsmSubBlock;
  309. Procedure TestProc_Assembler;
  310. Procedure TestProc_VarParam;
  311. Procedure TestProc_VarParamString;
  312. Procedure TestProc_VarParamV;
  313. Procedure TestProc_Overload;
  314. Procedure TestProc_OverloadForward;
  315. Procedure TestProc_OverloadIntfImpl;
  316. Procedure TestProc_OverloadNested;
  317. Procedure TestProc_OverloadNestedForward;
  318. Procedure TestProc_OverloadUnitCycle;
  319. Procedure TestProc_Varargs;
  320. Procedure TestProc_ConstOrder;
  321. Procedure TestProc_DuplicateConst;
  322. Procedure TestProc_LocalVarAbsolute;
  323. Procedure TestProc_LocalVarInit;
  324. Procedure TestProc_ReservedWords;
  325. Procedure TestProc_ConstRefWord;
  326. // anonymous functions
  327. Procedure TestAnonymousProc_Assign_ObjFPC;
  328. Procedure TestAnonymousProc_Assign_Delphi;
  329. Procedure TestAnonymousProc_Arg;
  330. Procedure TestAnonymousProc_Typecast;
  331. Procedure TestAnonymousProc_With;
  332. Procedure TestAnonymousProc_ExceptOn;
  333. Procedure TestAnonymousProc_Nested;
  334. Procedure TestAnonymousProc_NestedAssignResult;
  335. Procedure TestAnonymousProc_Class;
  336. Procedure TestAnonymousProc_ForLoop;
  337. Procedure TestAnonymousProc_AsmDelphi;
  338. // enums, sets
  339. Procedure TestEnum_Name;
  340. Procedure TestEnum_Number;
  341. Procedure TestEnum_ConstFail;
  342. Procedure TestEnum_Functions;
  343. Procedure TestEnumRg_Functions;
  344. Procedure TestEnum_AsParams;
  345. Procedure TestEnumRange_Array;
  346. Procedure TestEnum_ForIn;
  347. Procedure TestEnum_ScopedNumber;
  348. Procedure TestEnum_InFunction;
  349. Procedure TestEnum_Name_Anonymous_Unit;
  350. Procedure TestSet_Enum;
  351. Procedure TestSet_Operators;
  352. Procedure TestSet_Operator_In;
  353. Procedure TestSet_Functions;
  354. Procedure TestSet_PassAsArgClone;
  355. Procedure TestSet_AsParams;
  356. Procedure TestSet_Property;
  357. Procedure TestSet_EnumConst;
  358. Procedure TestSet_IntConst;
  359. Procedure TestSet_IntRange;
  360. Procedure TestSet_AnonymousEnumType;
  361. Procedure TestSet_AnonymousEnumTypeChar; // ToDo
  362. Procedure TestSet_ConstEnum;
  363. Procedure TestSet_ConstChar;
  364. Procedure TestSet_ConstInt;
  365. Procedure TestSet_InFunction;
  366. Procedure TestSet_ForIn;
  367. // statements
  368. Procedure TestNestBegin;
  369. Procedure TestIncDec;
  370. Procedure TestLoHiFpcMode;
  371. Procedure TestLoHiDelphiMode;
  372. Procedure TestAssignments;
  373. Procedure TestArithmeticOperators1;
  374. Procedure TestMultiAdd;
  375. Procedure TestLogicalOperators;
  376. Procedure TestBitwiseOperators;
  377. Procedure TestBitwiseOperatorsLongword;
  378. Procedure TestFunctionInt;
  379. Procedure TestFunctionString;
  380. Procedure TestIfThen;
  381. Procedure TestForLoop;
  382. Procedure TestForLoopInsideFunction;
  383. Procedure TestForLoop_ReadVarAfter;
  384. Procedure TestForLoop_Nested;
  385. Procedure TestRepeatUntil;
  386. Procedure TestAsmBlock;
  387. Procedure TestAsmPas_Impl; // ToDo
  388. Procedure TestTryFinally;
  389. Procedure TestTryExcept;
  390. Procedure TestTryExcept_ReservedWords;
  391. Procedure TestIfThenRaiseElse;
  392. Procedure TestCaseOf;
  393. Procedure TestCaseOf_UseSwitch;
  394. Procedure TestCaseOfNoElse;
  395. Procedure TestCaseOfNoElse_UseSwitch;
  396. Procedure TestCaseOfRange;
  397. Procedure TestCaseOfString;
  398. Procedure TestCaseOfChar;
  399. Procedure TestCaseOfExternalClassConst;
  400. Procedure TestDebugger;
  401. // arrays
  402. Procedure TestArray_Dynamic;
  403. Procedure TestArray_Dynamic_Nil;
  404. Procedure TestArray_DynMultiDimensional;
  405. Procedure TestArray_DynamicAssign;
  406. Procedure TestArray_StaticInt;
  407. Procedure TestArray_StaticBool;
  408. Procedure TestArray_StaticChar;
  409. Procedure TestArray_StaticMultiDim;
  410. Procedure TestArray_StaticInFunction;
  411. Procedure TestArray_StaticMultiDimEqualNotImplemented;
  412. Procedure TestArrayOfRecord;
  413. Procedure TestArray_StaticRecord;
  414. Procedure TestArrayOfSet;
  415. Procedure TestArray_DynAsParam;
  416. Procedure TestArray_StaticAsParam;
  417. Procedure TestArrayElement_AsParams;
  418. Procedure TestArrayElementFromFuncResult_AsParams;
  419. Procedure TestArrayEnumTypeRange;
  420. Procedure TestArray_SetLengthOutArg;
  421. Procedure TestArray_SetLengthProperty;
  422. Procedure TestArray_SetLengthMultiDim;
  423. Procedure TestArray_SetLengthDynOfStatic;
  424. Procedure TestArray_OpenArrayOfString;
  425. Procedure TestArray_ArrayOfCharAssignString; // ToDo
  426. Procedure TestArray_ConstRef;
  427. Procedure TestArray_Concat;
  428. Procedure TestArray_Copy;
  429. Procedure TestArray_InsertDelete;
  430. Procedure TestArray_DynArrayConstObjFPC;
  431. Procedure TestArray_DynArrayConstDelphi;
  432. Procedure TestArray_ArrayLitAsParam;
  433. Procedure TestArray_ArrayLitMultiDimAsParam;
  434. Procedure TestArray_ArrayLitStaticAsParam;
  435. Procedure TestArray_ForInArrOfString;
  436. Procedure TestExternalClass_TypeCastArrayToExternalClass;
  437. Procedure TestExternalClass_TypeCastArrayFromExternalClass;
  438. Procedure TestArrayOfConst_TVarRec;
  439. Procedure TestArrayOfConst_PassBaseTypes;
  440. Procedure TestArrayOfConst_PassObj;
  441. // record
  442. Procedure TestRecord_Empty;
  443. Procedure TestRecord_Var;
  444. Procedure TestRecord_VarExternal;
  445. Procedure TestRecord_WithDo;
  446. Procedure TestRecord_Assign;
  447. Procedure TestRecord_AsParams;
  448. Procedure TestRecord_ConstRef;
  449. Procedure TestRecordElement_AsParams;
  450. Procedure TestRecordElementFromFuncResult_AsParams;
  451. Procedure TestRecordElementFromWith_AsParams;
  452. Procedure TestRecord_Equal;
  453. Procedure TestRecord_JSValue;
  454. Procedure TestRecord_VariantFail;
  455. Procedure TestRecord_FieldArray;
  456. Procedure TestRecord_Const;
  457. Procedure TestRecord_TypecastFail;
  458. Procedure TestRecord_InFunction;
  459. Procedure TestRecord_AnonymousFail;
  460. // advanced record
  461. Procedure TestAdvRecord_Function;
  462. Procedure TestAdvRecord_Property;
  463. Procedure TestAdvRecord_PropertyDefault;
  464. Procedure TestAdvRecord_Property_ClassMethod;
  465. Procedure TestAdvRecord_Const;
  466. Procedure TestAdvRecord_ExternalField;
  467. Procedure TestAdvRecord_SubRecord;
  468. Procedure TestAdvRecord_SubClass;
  469. Procedure TestAdvRecord_SubInterfaceFail;
  470. Procedure TestAdvRecord_Constructor;
  471. Procedure TestAdvRecord_ClassConstructor_Program;
  472. Procedure TestAdvRecord_ClassConstructor_Unit;
  473. // classes
  474. Procedure TestClass_TObjectDefaultConstructor;
  475. Procedure TestClass_TObjectConstructorWithParams;
  476. Procedure TestClass_TObjectConstructorWithDefaultParam;
  477. Procedure TestClass_Var;
  478. Procedure TestClass_Method;
  479. Procedure TestClass_Implementation;
  480. Procedure TestClass_Inheritance;
  481. Procedure TestClass_TypeAlias;
  482. Procedure TestClass_AbstractMethod;
  483. Procedure TestClass_CallInherited_ProcNoParams;
  484. Procedure TestClass_CallInherited_WithParams;
  485. Procedure TestClasS_CallInheritedConstructor;
  486. Procedure TestClass_ClassVar_Assign;
  487. Procedure TestClass_CallClassMethod;
  488. Procedure TestClass_CallClassMethodStatic; // ToDo
  489. Procedure TestClass_Property;
  490. Procedure TestClass_Property_ClassMethod;
  491. Procedure TestClass_Property_Indexed;
  492. Procedure TestClass_Property_IndexSpec;
  493. Procedure TestClass_PropertyOfTypeArray;
  494. Procedure TestClass_PropertyDefault;
  495. Procedure TestClass_PropertyDefault_TypecastToOtherDefault;
  496. //Procedure TestClass_PropertyDefault;
  497. Procedure TestClass_PropertyOverride;
  498. Procedure TestClass_PropertyIncVisibility;
  499. Procedure TestClass_Assigned;
  500. Procedure TestClass_WithClassDoCreate;
  501. Procedure TestClass_WithClassInstDoProperty;
  502. Procedure TestClass_WithClassInstDoPropertyWithParams;
  503. Procedure TestClass_WithClassInstDoFunc;
  504. Procedure TestClass_TypeCast;
  505. Procedure TestClass_TypeCastUntypedParam;
  506. Procedure TestClass_Overloads;
  507. Procedure TestClass_OverloadsAncestor;
  508. Procedure TestClass_OverloadConstructor;
  509. Procedure TestClass_OverloadDelphiOverride;
  510. Procedure TestClass_ReintroduceVarDelphi;
  511. Procedure TestClass_ReintroducedVar;
  512. Procedure TestClass_RaiseDescendant;
  513. Procedure TestClass_ExternalMethod;
  514. Procedure TestClass_ExternalVirtualNameMismatchFail;
  515. Procedure TestClass_ExternalOverrideFail;
  516. Procedure TestClass_ExternalVar;
  517. Procedure TestClass_Const;
  518. Procedure TestClass_ConstEnum;
  519. Procedure TestClass_LocalConstDuplicate_Prg;
  520. Procedure TestClass_LocalConstDuplicate_Unit;
  521. // ToDo: Procedure TestAdvRecord_LocalConstDuplicate;
  522. Procedure TestClass_LocalVarSelfFail;
  523. Procedure TestClass_ArgSelfFail;
  524. Procedure TestClass_NestedProcSelf;
  525. Procedure TestClass_NestedProcSelf2;
  526. Procedure TestClass_NestedProcClassSelf;
  527. Procedure TestClass_NestedProcCallInherited;
  528. Procedure TestClass_TObjectFree;
  529. Procedure TestClass_TObjectFree_VarArg;
  530. Procedure TestClass_TObjectFreeNewInstance;
  531. Procedure TestClass_TObjectFreeLowerCase;
  532. Procedure TestClass_TObjectFreeFunctionFail;
  533. Procedure TestClass_TObjectFreePropertyFail;
  534. Procedure TestClass_ForIn;
  535. Procedure TestClass_DispatchMessage;
  536. Procedure TestClass_Message_DuplicateIntFail;
  537. Procedure TestClass_DispatchMessage_WrongFieldNameFail;
  538. // class of
  539. Procedure TestClassOf_Create;
  540. Procedure TestClassOf_Call;
  541. Procedure TestClassOf_Assign;
  542. Procedure TestClassOf_Is;
  543. Procedure TestClassOf_Compare;
  544. Procedure TestClassOf_ClassVar;
  545. Procedure TestClassOf_ClassMethod;
  546. Procedure TestClassOf_ClassProperty;
  547. Procedure TestClassOf_ClassMethodSelf;
  548. Procedure TestClassOf_TypeCast;
  549. Procedure TestClassOf_ImplicitFunctionCall;
  550. Procedure TestClassOf_Const;
  551. // nested class
  552. Procedure TestNestedClass_Alias;
  553. Procedure TestNestedClass_Record;
  554. Procedure TestNestedClass_Class;
  555. // external class
  556. Procedure TestExternalClass_Var;
  557. Procedure TestExternalClass_Const;
  558. Procedure TestExternalClass_Dollar;
  559. Procedure TestExternalClass_DuplicateVarFail;
  560. Procedure TestExternalClass_Method;
  561. Procedure TestExternalClass_ClassMethod;
  562. Procedure TestExternalClass_ClassMethodStatic;
  563. Procedure TestExternalClass_FunctionResultInTypeCast;
  564. Procedure TestExternalClass_NonExternalOverride;
  565. Procedure TestExternalClass_OverloadHint;
  566. Procedure TestExternalClass_SameNamePublishedProperty;
  567. Procedure TestExternalClass_Property;
  568. Procedure TestExternalClass_PropertyDate;
  569. Procedure TestExternalClass_ClassProperty;
  570. Procedure TestExternalClass_ClassOf;
  571. Procedure TestExternalClass_ClassOtherUnit;
  572. Procedure TestExternalClass_Is;
  573. Procedure TestExternalClass_As;
  574. Procedure TestExternalClass_DestructorFail;
  575. Procedure TestExternalClass_New;
  576. Procedure TestExternalClass_ClassOf_New;
  577. Procedure TestExternalClass_FuncClassOf_New;
  578. Procedure TestExternalClass_New_PasClassFail;
  579. Procedure TestExternalClass_New_PasClassBracketsFail;
  580. Procedure TestExternalClass_NewExtName;
  581. Procedure TestExternalClass_Constructor;
  582. Procedure TestExternalClass_ConstructorBrackets;
  583. Procedure TestExternalClass_LocalConstSameName;
  584. Procedure TestExternalClass_ReintroduceOverload;
  585. Procedure TestExternalClass_Inherited;
  586. Procedure TestExternalClass_PascalAncestorFail;
  587. Procedure TestExternalClass_NewInstance;
  588. Procedure TestExternalClass_NewInstance_NonVirtualFail;
  589. Procedure TestExternalClass_NewInstance_FirstParamNotString_Fail;
  590. Procedure TestExternalClass_NewInstance_SecondParamTyped_Fail;
  591. Procedure TestExternalClass_JSFunctionPasDescendant;
  592. Procedure TestExternalClass_PascalProperty;
  593. Procedure TestExternalClass_TypeCastToRootClass;
  594. Procedure TestExternalClass_TypeCastToJSObject;
  595. Procedure TestExternalClass_TypeCastStringToExternalString;
  596. Procedure TestExternalClass_TypeCastToJSFunction;
  597. Procedure TestExternalClass_TypeCastDelphiUnrelated;
  598. Procedure TestExternalClass_CallClassFunctionOfInstanceFail;
  599. Procedure TestExternalClass_BracketAccessor;
  600. Procedure TestExternalClass_BracketAccessor_Call;
  601. Procedure TestExternalClass_BracketAccessor_2ParamsFail;
  602. Procedure TestExternalClass_BracketAccessor_ReadOnly;
  603. Procedure TestExternalClass_BracketAccessor_WriteOnly;
  604. Procedure TestExternalClass_BracketAccessor_MultiType;
  605. Procedure TestExternalClass_BracketAccessor_Index;
  606. Procedure TestExternalClass_ForInJSObject;
  607. Procedure TestExternalClass_ForInJSArray;
  608. Procedure TestExternalClass_IncompatibleArgDuplicateIdentifier;
  609. // class interfaces
  610. Procedure TestClassInterface_Corba;
  611. Procedure TestClassInterface_ProcExternalFail;
  612. Procedure TestClassInterface_Overloads;
  613. Procedure TestClassInterface_DuplicateGUIInIntfListFail;
  614. Procedure TestClassInterface_DuplicateGUIInAncestorFail;
  615. Procedure TestClassInterface_AncestorImpl;
  616. Procedure TestClassInterface_ImplReintroduce;
  617. Procedure TestClassInterface_MethodResolution;
  618. Procedure TestClassInterface_AncestorMoreInterfaces;
  619. Procedure TestClassInterface_MethodOverride;
  620. Procedure TestClassInterface_Corba_Delegation;
  621. Procedure TestClassInterface_Corba_DelegationStatic;
  622. Procedure TestClassInterface_Corba_Operators;
  623. Procedure TestClassInterface_Corba_Args;
  624. Procedure TestClassInterface_Corba_ForIn;
  625. Procedure TestClassInterface_COM_AssignVar;
  626. Procedure TestClassInterface_COM_AssignArg;
  627. Procedure TestClassInterface_COM_FunctionResult;
  628. Procedure TestClassInterface_COM_InheritedFuncResult;
  629. Procedure TestClassInterface_COM_IsAsTypeCasts;
  630. Procedure TestClassInterface_COM_PassAsArg;
  631. Procedure TestClassInterface_COM_PassToUntypedParam;
  632. Procedure TestClassInterface_COM_FunctionInExpr;
  633. Procedure TestClassInterface_COM_Property;
  634. Procedure TestClassInterface_COM_IntfProperty;
  635. Procedure TestClassInterface_COM_Delegation;
  636. Procedure TestClassInterface_COM_With;
  637. Procedure TestClassInterface_COM_ForIn;
  638. Procedure TestClassInterface_COM_ArrayOfIntfFail;
  639. Procedure TestClassInterface_COM_RecordIntfFail;
  640. Procedure TestClassInterface_COM_UnitInitialization;
  641. Procedure TestClassInterface_GUID;
  642. Procedure TestClassInterface_GUIDProperty;
  643. // helpers
  644. Procedure TestClassHelper_ClassVar;
  645. Procedure TestClassHelper_Method_AccessInstanceFields;
  646. Procedure TestClassHelper_Method_Call;
  647. Procedure TestClassHelper_Method_Nested_Call;
  648. Procedure TestClassHelper_ClassMethod_Call;
  649. Procedure TestClassHelper_ClassOf;
  650. Procedure TestClassHelper_MethodRefObjFPC;
  651. Procedure TestClassHelper_Constructor;
  652. Procedure TestClassHelper_InheritedObjFPC;
  653. Procedure TestClassHelper_Property;
  654. Procedure TestClassHelper_Property_Array;
  655. Procedure TestClassHelper_Property_Array_Default;
  656. Procedure TestClassHelper_Property_Array_DefaultDefault;
  657. Procedure TestClassHelper_ClassProperty;
  658. Procedure TestClassHelper_ClassPropertyStatic;
  659. Procedure TestClassHelper_ClassProperty_Array;
  660. Procedure TestClassHelper_ForIn;
  661. Procedure TestClassHelper_PassProperty;
  662. Procedure TestExtClassHelper_ClassVar;
  663. Procedure TestExtClassHelper_Method_Call;
  664. Procedure TestExtClassHelper_ClassMethod_MissingStatic;
  665. Procedure TestRecordHelper_ClassVar;
  666. Procedure TestRecordHelper_Method_Call;
  667. Procedure TestRecordHelper_Constructor;
  668. Procedure TestTypeHelper_ClassVar;
  669. Procedure TestTypeHelper_PassResultElement;
  670. Procedure TestTypeHelper_PassArgs;
  671. Procedure TestTypeHelper_PassVarConst;
  672. Procedure TestTypeHelper_PassFuncResult;
  673. Procedure TestTypeHelper_PassPropertyField;
  674. Procedure TestTypeHelper_PassPropertyGetter;
  675. Procedure TestTypeHelper_PassClassPropertyField;
  676. Procedure TestTypeHelper_PassClassPropertyGetterStatic;
  677. Procedure TestTypeHelper_PassClassPropertyGetterNonStatic;
  678. Procedure TestTypeHelper_Property;
  679. Procedure TestTypeHelper_Property_Array;
  680. Procedure TestTypeHelper_ClassProperty;
  681. Procedure TestTypeHelper_ClassProperty_Array;
  682. Procedure TestTypeHelper_ClassMethod;
  683. Procedure TestTypeHelper_ExtClassMethodFail;
  684. Procedure TestTypeHelper_Constructor;
  685. Procedure TestTypeHelper_Word;
  686. Procedure TestTypeHelper_Boolean;
  687. Procedure TestTypeHelper_WordBool;
  688. Procedure TestTypeHelper_Double;
  689. Procedure TestTypeHelper_NativeInt;
  690. Procedure TestTypeHelper_StringChar;
  691. Procedure TestTypeHelper_JSValue;
  692. Procedure TestTypeHelper_Array;
  693. Procedure TestTypeHelper_EnumType;
  694. Procedure TestTypeHelper_SetType;
  695. Procedure TestTypeHelper_InterfaceType;
  696. Procedure TestTypeHelper_NestedSelf;
  697. // proc types
  698. Procedure TestProcType;
  699. Procedure TestProcType_Arg;
  700. Procedure TestProcType_FunctionFPC;
  701. Procedure TestProcType_FunctionDelphi;
  702. Procedure TestProcType_ProcedureDelphi;
  703. Procedure TestProcType_AsParam;
  704. Procedure TestProcType_MethodFPC;
  705. Procedure TestProcType_MethodDelphi;
  706. Procedure TestProcType_PropertyFPC;
  707. Procedure TestProcType_PropertyDelphi;
  708. Procedure TestProcType_WithClassInstDoPropertyFPC;
  709. Procedure TestProcType_Nested;
  710. Procedure TestProcType_NestedOfObject;
  711. Procedure TestProcType_ReferenceToProc;
  712. Procedure TestProcType_ReferenceToMethod;
  713. Procedure TestProcType_Typecast;
  714. Procedure TestProcType_PassProcToUntyped;
  715. Procedure TestProcType_PassProcToArray;
  716. Procedure TestProcType_SafeCallObjFPC;
  717. Procedure TestProcType_SafeCallDelphi;
  718. // pointer
  719. Procedure TestPointer;
  720. Procedure TestPointer_Proc;
  721. Procedure TestPointer_AssignRecordFail;
  722. Procedure TestPointer_AssignStaticArrayFail;
  723. Procedure TestPointer_TypeCastJSValueToPointer;
  724. Procedure TestPointer_NonRecordFail;
  725. Procedure TestPointer_AnonymousArgTypeFail;
  726. Procedure TestPointer_AnonymousVarTypeFail;
  727. Procedure TestPointer_AnonymousResultTypeFail;
  728. Procedure TestPointer_AddrOperatorFail;
  729. Procedure TestPointer_ArrayParamsFail;
  730. Procedure TestPointer_PointerAddFail;
  731. Procedure TestPointer_IncPointerFail;
  732. Procedure TestPointer_Record;
  733. Procedure TestPointer_RecordArg;
  734. // jsvalue
  735. Procedure TestJSValue_AssignToJSValue;
  736. Procedure TestJSValue_TypeCastToBaseType;
  737. Procedure TestJSValue_TypecastToJSValue;
  738. Procedure TestJSValue_Equal;
  739. Procedure TestJSValue_If;
  740. Procedure TestJSValue_Not;
  741. Procedure TestJSValue_Enum;
  742. Procedure TestJSValue_ClassInstance;
  743. Procedure TestJSValue_ClassOf;
  744. Procedure TestJSValue_ArrayOfJSValue;
  745. Procedure TestJSValue_ArrayLit;
  746. Procedure TestJSValue_Params;
  747. Procedure TestJSValue_UntypedParam;
  748. Procedure TestJSValue_FuncResultType;
  749. Procedure TestJSValue_ProcType_Assign;
  750. Procedure TestJSValue_ProcType_Equal;
  751. Procedure TestJSValue_ProcType_Param;
  752. Procedure TestJSValue_AssignToPointerFail;
  753. Procedure TestJSValue_OverloadDouble;
  754. Procedure TestJSValue_OverloadNativeInt;
  755. Procedure TestJSValue_OverloadWord;
  756. Procedure TestJSValue_OverloadString;
  757. Procedure TestJSValue_OverloadChar;
  758. Procedure TestJSValue_OverloadPointer;
  759. Procedure TestJSValue_ForIn;
  760. // RTTI
  761. Procedure TestRTTI_IntRange;
  762. Procedure TestRTTI_Double;
  763. Procedure TestRTTI_ProcType;
  764. Procedure TestRTTI_ProcType_ArgFromOtherUnit;
  765. Procedure TestRTTI_EnumAndSetType;
  766. Procedure TestRTTI_EnumRange;
  767. Procedure TestRTTI_AnonymousEnumType;
  768. Procedure TestRTTI_StaticArray;
  769. Procedure TestRTTI_DynArray;
  770. Procedure TestRTTI_ArrayNestedAnonymous;
  771. Procedure TestRTTI_PublishedMethodOverloadFail;
  772. Procedure TestRTTI_PublishedMethodHideNoHint;
  773. Procedure TestRTTI_PublishedMethodExternalFail;
  774. Procedure TestRTTI_PublishedClassPropertyFail;
  775. Procedure TestRTTI_PublishedClassFieldFail;
  776. Procedure TestRTTI_PublishedFieldExternalFail;
  777. Procedure TestRTTI_Class_Field;
  778. Procedure TestRTTI_Class_Method;
  779. Procedure TestRTTI_Class_MethodArgFlags;
  780. Procedure TestRTTI_Class_Property;
  781. Procedure TestRTTI_Class_PropertyParams;
  782. Procedure TestRTTI_Class_OtherUnit_TypeAlias;
  783. Procedure TestRTTI_Class_OmitRTTI;
  784. Procedure TestRTTI_Class_Field_AnonymousArrayOfSelfClass;
  785. Procedure TestRTTI_IndexModifier;
  786. Procedure TestRTTI_StoredModifier;
  787. Procedure TestRTTI_DefaultValue;
  788. Procedure TestRTTI_DefaultValueSet;
  789. Procedure TestRTTI_DefaultValueRangeType;
  790. Procedure TestRTTI_DefaultValueInherit;
  791. Procedure TestRTTI_OverrideMethod;
  792. Procedure TestRTTI_ReintroduceMethod;
  793. Procedure TestRTTI_OverloadProperty;
  794. // ToDo: array argument
  795. Procedure TestRTTI_ClassForward;
  796. Procedure TestRTTI_ClassOf;
  797. Procedure TestRTTI_Record;
  798. Procedure TestRTTI_RecordAnonymousArray;
  799. Procedure TestRTTI_Record_ClassVarType;
  800. Procedure TestRTTI_LocalTypes;
  801. Procedure TestRTTI_TypeInfo_BaseTypes;
  802. Procedure TestRTTI_TypeInfo_Type_BaseTypes;
  803. Procedure TestRTTI_TypeInfo_LocalFail;
  804. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses1;
  805. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses2;
  806. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses3;
  807. Procedure TestRTTI_TypeInfo_FunctionClassType;
  808. Procedure TestRTTI_TypeInfo_MixedUnits_PointerAndClass;
  809. Procedure TestRTTI_Interface_Corba;
  810. Procedure TestRTTI_Interface_COM;
  811. Procedure TestRTTI_ClassHelper;
  812. Procedure TestRTTI_ExternalClass;
  813. Procedure TestRTTI_Unit;
  814. // Resourcestring
  815. Procedure TestResourcestringProgram;
  816. Procedure TestResourcestringUnit;
  817. Procedure TestResourcestringImplementation;
  818. // Attributes
  819. Procedure TestAttributes_Members;
  820. Procedure TestAttributes_Types;
  821. Procedure TestAttributes_HelperConstructor_Fail;
  822. // Assertions, checks
  823. procedure TestAssert;
  824. procedure TestAssert_SysUtils;
  825. procedure TestObjectChecks;
  826. procedure TestOverflowChecks_Int;
  827. procedure TestRangeChecks_AssignInt;
  828. procedure TestRangeChecks_AssignIntRange;
  829. procedure TestRangeChecks_AssignEnum;
  830. procedure TestRangeChecks_AssignEnumRange;
  831. procedure TestRangeChecks_AssignChar;
  832. procedure TestRangeChecks_AssignCharRange;
  833. procedure TestRangeChecks_ArrayIndex;
  834. procedure TestRangeChecks_ArrayOfRecIndex;
  835. procedure TestRangeChecks_StringIndex;
  836. procedure TestRangeChecks_TypecastInt;
  837. procedure TestRangeChecks_TypeHelperInt;
  838. // Async/AWait
  839. Procedure TestAsync_Proc;
  840. Procedure TestAsync_CallResultIsPromise;
  841. Procedure TestAsync_ConstructorFail;
  842. Procedure TestAsync_PropertyGetterFail;
  843. Procedure TestAwait_NonPromiseWithTypeFail;
  844. Procedure TestAwait_AsyncCallTypeMismatch;
  845. Procedure TestAWait_OutsideAsyncFail;
  846. Procedure TestAWait_IntegerFail;
  847. Procedure TestAWait_ExternalClassPromise;
  848. Procedure TestAWait_JSValue;
  849. Procedure TestAWait_Result;
  850. Procedure TestAWait_ResultPromiseMissingTypeFail; // await(AsyncCallResultPromise) needs T
  851. Procedure TestAsync_AnonymousProc;
  852. Procedure TestAsync_ProcType;
  853. Procedure TestAsync_ProcTypeAsyncModMismatchFail;
  854. Procedure TestAsync_Inherited;
  855. Procedure TestAsync_ClassInterface;
  856. Procedure TestAsync_ClassInterface_AsyncMissmatchFail;
  857. end;
  858. function LinesToStr(Args: array of const): string;
  859. function ExtractFileUnitName(aFilename: string): string;
  860. function JSToStr(El: TJSElement): string;
  861. function CheckSrcDiff(Expected, Actual: string; out Msg: string): boolean;
  862. implementation
  863. function LinesToStr(Args: array of const): string;
  864. var
  865. s: String;
  866. i: Integer;
  867. begin
  868. s:='';
  869. for i:=Low(Args) to High(Args) do
  870. case Args[i].VType of
  871. vtChar: s += Args[i].VChar+LineEnding;
  872. vtString: s += Args[i].VString^+LineEnding;
  873. vtPChar: s += Args[i].VPChar+LineEnding;
  874. vtWideChar: s += AnsiString(Args[i].VWideChar)+LineEnding;
  875. vtPWideChar: s += AnsiString(Args[i].VPWideChar)+LineEnding;
  876. vtAnsiString: s += AnsiString(Args[i].VAnsiString)+LineEnding;
  877. vtWidestring: s += AnsiString(WideString(Args[i].VWideString))+LineEnding;
  878. vtUnicodeString:s += AnsiString(UnicodeString(Args[i].VUnicodeString))+LineEnding;
  879. end;
  880. Result:=s;
  881. end;
  882. function ExtractFileUnitName(aFilename: string): string;
  883. var
  884. p: Integer;
  885. begin
  886. Result:=ExtractFileName(aFilename);
  887. if Result='' then exit;
  888. for p:=length(Result) downto 1 do
  889. case Result[p] of
  890. '/','\': exit;
  891. '.':
  892. begin
  893. Delete(Result,p,length(Result));
  894. exit;
  895. end;
  896. end;
  897. end;
  898. function JSToStr(El: TJSElement): string;
  899. var
  900. aWriter: TBufferWriter;
  901. aJSWriter: TJSWriter;
  902. begin
  903. aJSWriter:=nil;
  904. aWriter:=TBufferWriter.Create(1000);
  905. try
  906. aJSWriter:=TJSWriter.Create(aWriter);
  907. aJSWriter.IndentSize:=2;
  908. aJSWriter.WriteJS(El);
  909. Result:=aWriter.AsString;
  910. finally
  911. aJSWriter.Free;
  912. aWriter.Free;
  913. end;
  914. end;
  915. function CheckSrcDiff(Expected, Actual: string; out Msg: string): boolean;
  916. // search diff, ignore changes in spaces
  917. const
  918. SpaceChars = [#9,#10,#13,' '];
  919. var
  920. ExpectedP, ActualP: PChar;
  921. function FindLineEnd(p: PChar): PChar;
  922. begin
  923. Result:=p;
  924. while not (Result^ in [#0,#10,#13]) do inc(Result);
  925. end;
  926. function FindLineStart(p, MinP: PChar): PChar;
  927. begin
  928. while (p>MinP) and not (p[-1] in [#10,#13]) do dec(p);
  929. Result:=p;
  930. end;
  931. procedure SkipLineEnd(var p: PChar);
  932. begin
  933. if p^ in [#10,#13] then
  934. begin
  935. if (p[1] in [#10,#13]) and (p^<>p[1]) then
  936. inc(p,2)
  937. else
  938. inc(p);
  939. end;
  940. end;
  941. function HasSpecialChar(s: string): boolean;
  942. var
  943. i: Integer;
  944. begin
  945. for i:=1 to length(s) do
  946. if s[i] in [#0..#31,#127..#255] then
  947. exit(true);
  948. Result:=false;
  949. end;
  950. function HashSpecialChars(s: string): string;
  951. var
  952. i: Integer;
  953. begin
  954. Result:='';
  955. for i:=1 to length(s) do
  956. if s[i] in [#0..#31,#127..#255] then
  957. Result:=Result+'#'+hexstr(ord(s[i]),2)
  958. else
  959. Result:=Result+s[i];
  960. end;
  961. procedure DiffFound;
  962. var
  963. ActLineStartP, ActLineEndP, p, StartPos: PChar;
  964. ExpLine, ActLine: String;
  965. i, LineNo, DiffLineNo: Integer;
  966. begin
  967. writeln('Diff found "',Msg,'". Lines:');
  968. // write correct lines
  969. p:=PChar(Expected);
  970. LineNo:=0;
  971. DiffLineNo:=0;
  972. repeat
  973. StartPos:=p;
  974. while not (p^ in [#0,#10,#13]) do inc(p);
  975. ExpLine:=copy(Expected,StartPos-PChar(Expected)+1,p-StartPos);
  976. SkipLineEnd(p);
  977. inc(LineNo);
  978. if (p<=ExpectedP) and (p^<>#0) then
  979. begin
  980. writeln('= ',ExpLine);
  981. end else begin
  982. // diff line
  983. if DiffLineNo=0 then DiffLineNo:=LineNo;
  984. // write actual line
  985. ActLineStartP:=FindLineStart(ActualP,PChar(Actual));
  986. ActLineEndP:=FindLineEnd(ActualP);
  987. ActLine:=copy(Actual,ActLineStartP-PChar(Actual)+1,ActLineEndP-ActLineStartP);
  988. writeln('- ',ActLine);
  989. if HasSpecialChar(ActLine) then
  990. writeln('- ',HashSpecialChars(ActLine));
  991. // write expected line
  992. writeln('+ ',ExpLine);
  993. if HasSpecialChar(ExpLine) then
  994. writeln('- ',HashSpecialChars(ExpLine));
  995. // write empty line with pointer ^
  996. for i:=1 to 2+ExpectedP-StartPos do write(' ');
  997. writeln('^');
  998. Msg:='expected "'+ExpLine+'", but got "'+ActLine+'".';
  999. CheckSrcDiff:=false;
  1000. // write up to three following actual lines to get some context
  1001. for i:=1 to 3 do begin
  1002. ActLineStartP:=ActLineEndP;
  1003. SkipLineEnd(ActLineStartP);
  1004. if ActLineStartP^=#0 then break;
  1005. ActLineEndP:=FindLineEnd(ActLineStartP);
  1006. ActLine:=copy(Actual,ActLineStartP-PChar(Actual)+1,ActLineEndP-ActLineStartP);
  1007. writeln('~ ',ActLine);
  1008. end;
  1009. exit;
  1010. end;
  1011. until p^=#0;
  1012. writeln('DiffFound Actual:-----------------------');
  1013. writeln(Actual);
  1014. writeln('DiffFound Expected:---------------------');
  1015. writeln(Expected);
  1016. writeln('DiffFound ------------------------------');
  1017. Msg:='diff found, but lines are the same, internal error';
  1018. CheckSrcDiff:=false;
  1019. end;
  1020. var
  1021. IsSpaceNeeded: Boolean;
  1022. LastChar, Quote: Char;
  1023. begin
  1024. Result:=true;
  1025. Msg:='';
  1026. if Expected='' then Expected:=' ';
  1027. if Actual='' then Actual:=' ';
  1028. ExpectedP:=PChar(Expected);
  1029. ActualP:=PChar(Actual);
  1030. repeat
  1031. //writeln('TTestModule.CheckDiff Exp="',ExpectedP^,'" Act="',ActualP^,'"');
  1032. case ExpectedP^ of
  1033. #0:
  1034. begin
  1035. // check that rest of Actual has only spaces
  1036. while ActualP^ in SpaceChars do inc(ActualP);
  1037. if ActualP^<>#0 then
  1038. begin
  1039. DiffFound;
  1040. exit;
  1041. end;
  1042. exit(true);
  1043. end;
  1044. ' ',#9,#10,#13:
  1045. begin
  1046. // skip space in Expected
  1047. IsSpaceNeeded:=false;
  1048. if ExpectedP>PChar(Expected) then
  1049. LastChar:=ExpectedP[-1]
  1050. else
  1051. LastChar:=#0;
  1052. while ExpectedP^ in SpaceChars do inc(ExpectedP);
  1053. if (LastChar in ['a'..'z','A'..'Z','0'..'9','_','$'])
  1054. and (ExpectedP^ in ['a'..'z','A'..'Z','0'..'9','_','$']) then
  1055. IsSpaceNeeded:=true;
  1056. if IsSpaceNeeded and (not (ActualP^ in SpaceChars)) then
  1057. begin
  1058. DiffFound;
  1059. exit;
  1060. end;
  1061. while ActualP^ in SpaceChars do inc(ActualP);
  1062. end;
  1063. '''','"':
  1064. begin
  1065. while ActualP^ in SpaceChars do inc(ActualP);
  1066. if ExpectedP^<>ActualP^ then
  1067. begin
  1068. DiffFound;
  1069. exit;
  1070. end;
  1071. Quote:=ExpectedP^;
  1072. repeat
  1073. inc(ExpectedP);
  1074. inc(ActualP);
  1075. if ExpectedP^<>ActualP^ then
  1076. begin
  1077. DiffFound;
  1078. exit;
  1079. end;
  1080. if (ExpectedP^ in [#0,#10,#13]) then
  1081. break
  1082. else if (ExpectedP^=Quote) then
  1083. begin
  1084. inc(ExpectedP);
  1085. inc(ActualP);
  1086. break;
  1087. end;
  1088. until false;
  1089. end;
  1090. else
  1091. while ActualP^ in SpaceChars do inc(ActualP);
  1092. if ExpectedP^<>ActualP^ then
  1093. begin
  1094. DiffFound;
  1095. exit;
  1096. end;
  1097. inc(ExpectedP);
  1098. inc(ActualP);
  1099. end;
  1100. until false;
  1101. end;
  1102. { TTestEnginePasResolver }
  1103. destructor TTestEnginePasResolver.Destroy;
  1104. begin
  1105. FreeAndNil(FStreamResolver);
  1106. FreeAndNil(FParser);
  1107. FreeAndNil(FScanner);
  1108. FreeAndNil(FStreamResolver);
  1109. if Module<>nil then
  1110. begin
  1111. Module.Release{$IFDEF CheckPasTreeRefCount}('CreateElement'){$ENDIF};
  1112. FModule:=nil;
  1113. end;
  1114. inherited Destroy;
  1115. end;
  1116. function TTestEnginePasResolver.FindUnit(const AName, InFilename: String;
  1117. NameExpr, InFileExpr: TPasExpr): TPasModule;
  1118. begin
  1119. Result:=nil;
  1120. if InFilename<>'' then
  1121. RaiseNotYetImplemented(20180224101926,InFileExpr,'Use testcase tcunitsearch instead');
  1122. if Assigned(OnFindUnit) then
  1123. Result:=OnFindUnit(AName);
  1124. if NameExpr=nil then ;
  1125. end;
  1126. procedure TTestEnginePasResolver.UsedInterfacesFinished(Section: TPasSection);
  1127. begin
  1128. // do not parse recursively
  1129. // parse via the queue
  1130. if Section=nil then ;
  1131. end;
  1132. { TCustomTestModule }
  1133. function TCustomTestModule.GetMsgCount: integer;
  1134. begin
  1135. Result:=FHintMsgs.Count;
  1136. end;
  1137. function TCustomTestModule.GetMsgs(Index: integer): TTestHintMessage;
  1138. begin
  1139. Result:=TTestHintMessage(FHintMsgs[Index]);
  1140. end;
  1141. function TCustomTestModule.GetResolverCount: integer;
  1142. begin
  1143. Result:=FModules.Count;
  1144. end;
  1145. function TCustomTestModule.GetResolvers(Index: integer
  1146. ): TTestEnginePasResolver;
  1147. begin
  1148. Result:=TTestEnginePasResolver(FModules[Index]);
  1149. end;
  1150. function TCustomTestModule.OnPasResolverFindUnit(const aUnitName: String
  1151. ): TPasModule;
  1152. var
  1153. DefNamespace: String;
  1154. begin
  1155. //writeln('TTestModule.OnPasResolverFindUnit START Unit="',aUnitName,'"');
  1156. if (Pos('.',aUnitName)<1) then
  1157. begin
  1158. DefNamespace:=GetDefaultNamespace;
  1159. if DefNamespace<>'' then
  1160. begin
  1161. Result:=LoadUnit(DefNamespace+'.'+aUnitName);
  1162. if Result<>nil then exit;
  1163. end;
  1164. end;
  1165. Result:=LoadUnit(aUnitName);
  1166. if Result<>nil then exit;
  1167. {$IFDEF VerbosePas2JS}
  1168. writeln('TTestModule.OnPasResolverFindUnit missing unit "',aUnitName,'"');
  1169. {$ENDIF}
  1170. Fail('can''t find unit "'+aUnitName+'"');
  1171. end;
  1172. procedure TCustomTestModule.OnParserLog(Sender: TObject; const Msg: String);
  1173. var
  1174. aParser: TPasParser;
  1175. Item: TTestHintMessage;
  1176. begin
  1177. aParser:=Sender as TPasParser;
  1178. Item:=TTestHintMessage.Create;
  1179. Item.Id:=aParser.LastMsgNumber;
  1180. Item.MsgType:=aParser.LastMsgType;
  1181. Item.MsgNumber:=aParser.LastMsgNumber;
  1182. Item.Msg:=Msg;
  1183. Item.SourcePos:=aParser.Scanner.CurSourcePos;
  1184. {$IFDEF VerbosePas2JS}
  1185. writeln('TCustomTestModule.OnParserLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1186. {$ENDIF}
  1187. FHintMsgs.Add(Item);
  1188. end;
  1189. procedure TCustomTestModule.OnPasResolverLog(Sender: TObject; const Msg: String
  1190. );
  1191. var
  1192. aResolver: TTestEnginePasResolver;
  1193. Item: TTestHintMessage;
  1194. begin
  1195. aResolver:=Sender as TTestEnginePasResolver;
  1196. Item:=TTestHintMessage.Create;
  1197. Item.Id:=aResolver.LastMsgId;
  1198. Item.MsgType:=aResolver.LastMsgType;
  1199. Item.MsgNumber:=aResolver.LastMsgNumber;
  1200. Item.Msg:=Msg;
  1201. Item.SourcePos:=aResolver.LastSourcePos;
  1202. {$IFDEF VerbosePas2JS}
  1203. writeln('TCustomTestModule.OnPasResolverLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1204. {$ENDIF}
  1205. FHintMsgs.Add(Item);
  1206. end;
  1207. procedure TCustomTestModule.OnScannerLog(Sender: TObject; const Msg: String);
  1208. var
  1209. Item: TTestHintMessage;
  1210. aScanner: TPas2jsPasScanner;
  1211. begin
  1212. aScanner:=Sender as TPas2jsPasScanner;
  1213. Item:=TTestHintMessage.Create;
  1214. Item.Id:=aScanner.LastMsgNumber;
  1215. Item.MsgType:=aScanner.LastMsgType;
  1216. Item.MsgNumber:=aScanner.LastMsgNumber;
  1217. Item.Msg:=Msg;
  1218. Item.SourcePos:=aScanner.CurSourcePos;
  1219. {$IFDEF VerbosePas2JS}
  1220. writeln('TCustomTestModule.OnScannerLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1221. {$ENDIF}
  1222. FHintMsgs.Add(Item);
  1223. end;
  1224. procedure TCustomTestModule.SetWithTypeInfo(const AValue: boolean);
  1225. begin
  1226. if FWithTypeInfo=AValue then Exit;
  1227. FWithTypeInfo:=AValue;
  1228. if AValue then
  1229. Converter.Options:=Converter.Options-[coNoTypeInfo]
  1230. else
  1231. Converter.Options:=Converter.Options+[coNoTypeInfo];
  1232. end;
  1233. function TCustomTestModule.LoadUnit(const aUnitName: String): TPasModule;
  1234. var
  1235. i: Integer;
  1236. CurEngine: TTestEnginePasResolver;
  1237. CurUnitName: String;
  1238. begin
  1239. //writeln('TTestModule.FindUnit START Unit="',aUnitName,'"');
  1240. Result:=nil;
  1241. if (Module.ClassType=TPasModule)
  1242. and (CompareText(Module.Name,aUnitName)=0) then
  1243. exit(Module);
  1244. for i:=0 to ResolverCount-1 do
  1245. begin
  1246. CurEngine:=Resolvers[i];
  1247. CurUnitName:=ExtractFileUnitName(CurEngine.Filename);
  1248. //writeln('TTestModule.FindUnit Checking ',i,'/',ResolverCount,' ',CurEngine.Filename,' ',CurUnitName);
  1249. if CompareText(aUnitName,CurUnitName)=0 then
  1250. begin
  1251. Result:=CurEngine.Module;
  1252. if Result<>nil then exit;
  1253. //writeln('TTestModule.FindUnit PARSING unit "',CurEngine.Filename,'"');
  1254. FileResolver.FindSourceFile(aUnitName);
  1255. CurEngine.StreamResolver:=TStreamResolver.Create;
  1256. CurEngine.StreamResolver.OwnsStreams:=True;
  1257. //writeln('TTestModule.FindUnit SOURCE=',CurEngine.Source);
  1258. CurEngine.StreamResolver.AddStream(CurEngine.FileName,TStringStream.Create(CurEngine.Source));
  1259. CurEngine.Scanner:=TPas2jsPasScanner.Create(CurEngine.StreamResolver);
  1260. InitScanner(CurEngine.Scanner);
  1261. CurEngine.Parser:=TTestPasParser.Create(CurEngine.Scanner,CurEngine.StreamResolver,CurEngine);
  1262. CurEngine.Parser.Options:=po_tcmodules;
  1263. if CompareText(CurUnitName,'System')=0 then
  1264. CurEngine.Parser.ImplicitUses.Clear;
  1265. CurEngine.Scanner.OpenFile(CurEngine.Filename);
  1266. try
  1267. CurEngine.Parser.NextToken;
  1268. CurEngine.Parser.ParseUnit(CurEngine.FModule);
  1269. except
  1270. on E: Exception do
  1271. HandleException(E);
  1272. end;
  1273. //writeln('TTestModule.FindUnit END ',CurUnitName);
  1274. Result:=CurEngine.Module;
  1275. exit;
  1276. end;
  1277. end;
  1278. end;
  1279. procedure TCustomTestModule.SetUp;
  1280. begin
  1281. {$IFDEF EnablePasTreeGlobalRefCount}
  1282. FElementRefCountAtSetup:=TPasElement.GlobalRefCount;
  1283. {$ENDIF}
  1284. if FModules<>nil then
  1285. begin
  1286. writeln('TCustomTestModule.SetUp FModules<>nil');
  1287. Halt;
  1288. end;
  1289. inherited SetUp;
  1290. FSkipTests:=false;
  1291. FWithTypeInfo:=false;
  1292. FSource:=TStringList.Create;
  1293. FHub:=TPas2JSResolverHub.Create(Self);
  1294. FModules:=TObjectList.Create(true);
  1295. FFilename:='test1.pp';
  1296. FFileResolver:=TStreamResolver.Create;
  1297. FFileResolver.OwnsStreams:=True;
  1298. FScanner:=TPas2jsPasScanner.Create(FFileResolver);
  1299. InitScanner(FScanner);
  1300. FEngine:=AddModule(Filename);
  1301. FEngine.Scanner:=FScanner;
  1302. FScanner.Resolver:=FEngine;
  1303. FParser:=TTestPasParser.Create(FScanner,FFileResolver,FEngine);
  1304. FParser.OnLog:=@OnParserLog;
  1305. FEngine.Parser:=FParser;
  1306. Parser.Options:=po_tcmodules;
  1307. FModule:=Nil;
  1308. FConverter:=CreateConverter;
  1309. FExpectedErrorClass:=nil;
  1310. end;
  1311. function TCustomTestModule.CreateConverter: TPasToJSConverter;
  1312. var
  1313. Options: TPasToJsConverterOptions;
  1314. begin
  1315. Result:=TPasToJSConverter.Create;
  1316. Options:=co_tcmodules;
  1317. if WithTypeInfo then
  1318. Exclude(Options,coNoTypeInfo)
  1319. else
  1320. Include(Options,coNoTypeInfo);
  1321. Result.Options:=Options;
  1322. Result.Globals:=TPasToJSConverterGlobals.Create(Result);
  1323. end;
  1324. procedure TCustomTestModule.InitScanner(aScanner: TPas2jsPasScanner);
  1325. begin
  1326. aScanner.AllowedModeSwitches:=msAllPas2jsModeSwitches;
  1327. aScanner.ReadOnlyModeSwitches:=msAllPas2jsModeSwitchesReadOnly;
  1328. aScanner.CurrentModeSwitches:=OBJFPCModeSwitches*msAllPas2jsModeSwitches+msAllPas2jsModeSwitchesReadOnly;
  1329. aScanner.AllowedBoolSwitches:=bsAllPas2jsBoolSwitches;
  1330. aScanner.ReadOnlyBoolSwitches:=bsAllPas2jsBoolSwitchesReadOnly;
  1331. aScanner.CurrentBoolSwitches:=bsAllPas2jsBoolSwitchesReadOnly+[bsHints,bsNotes,bsWarnings,bsWriteableConst];
  1332. aScanner.AllowedValueSwitches:=vsAllPas2jsValueSwitches;
  1333. aScanner.ReadOnlyValueSwitches:=vsAllPas2jsValueSwitchesReadOnly;
  1334. aScanner.OnLog:=@OnScannerLog;
  1335. aScanner.CompilerVersion:='Comp.Ver.tcmodules';
  1336. end;
  1337. procedure TCustomTestModule.TearDown;
  1338. {$IFDEF CheckPasTreeRefCount}
  1339. var
  1340. El: TPasElement;
  1341. {$ENDIF}
  1342. var
  1343. i: Integer;
  1344. CurModule: TPasModule;
  1345. begin
  1346. FHintMsgs.Clear;
  1347. FHintMsgsGood.Clear;
  1348. FSkipTests:=false;
  1349. FWithTypeInfo:=false;
  1350. FJSRegModuleCall:=nil;
  1351. FJSModuleCallArgs:=nil;
  1352. FJSImplentationUses:=nil;
  1353. FJSInterfaceUses:=nil;
  1354. FJSModuleSrc:=nil;
  1355. FJSInitBody:=nil;
  1356. FreeAndNil(FJSSource);
  1357. FreeAndNil(FJSModule);
  1358. FreeAndNil(FConverter);
  1359. Engine.Clear;
  1360. FreeAndNil(FSource);
  1361. FreeAndNil(FFileResolver);
  1362. if FModules<>nil then
  1363. begin
  1364. for i:=0 to FModules.Count-1 do
  1365. begin
  1366. CurModule:=TTestEnginePasResolver(FModules[i]).Module;
  1367. if CurModule=nil then continue;
  1368. //writeln('TCustomTestModule.TearDown ReleaseUsedUnits ',CurModule.Name,' ',CurModule.RefCount,' ',CurModule.RefIds.Text);
  1369. CurModule.ReleaseUsedUnits;
  1370. end;
  1371. if FModule<>nil then
  1372. FModule.ReleaseUsedUnits;
  1373. for i:=0 to FModules.Count-1 do
  1374. begin
  1375. CurModule:=TTestEnginePasResolver(FModules[i]).Module;
  1376. if CurModule=nil then continue;
  1377. //writeln('TCustomTestModule.TearDown UsesReleased ',CurModule.Name,' ',CurModule.RefCount,' ',CurModule.RefIds.Text);
  1378. end;
  1379. FreeAndNil(FModules);
  1380. ReleaseAndNil(TPasElement(FModule){$IFDEF CheckPasTreeRefCount},'CreateElement'{$ENDIF});
  1381. FEngine:=nil;
  1382. end;
  1383. FreeAndNil(FHub);
  1384. inherited TearDown;
  1385. {$IFDEF EnablePasTreeGlobalRefCount}
  1386. if FElementRefCountAtSetup<>TPasElement.GlobalRefCount then
  1387. begin
  1388. writeln('TCustomTestModule.TearDown GlobalRefCount Was='+IntToStr(FElementRefCountAtSetup)+' Now='+IntToStr(TPasElement.GlobalRefCount));
  1389. {$IFDEF CheckPasTreeRefCount}
  1390. El:=TPasElement.FirstRefEl;
  1391. while El<>nil do
  1392. begin
  1393. writeln(' ',GetObjName(El),' RefIds.Count=',El.RefIds.Count,':');
  1394. for i:=0 to El.RefIds.Count-1 do
  1395. writeln(' ',El.RefIds[i]);
  1396. El:=El.NextRefEl;
  1397. end;
  1398. {$ENDIF}
  1399. Halt;
  1400. Fail('TCustomTestModule.TearDown Was='+IntToStr(FElementRefCountAtSetup)+' Now='+IntToStr(TPasElement.GlobalRefCount));
  1401. end;
  1402. {$ENDIF}
  1403. end;
  1404. procedure TCustomTestModule.Add(Line: string);
  1405. begin
  1406. Source.Add(Line);
  1407. end;
  1408. procedure TCustomTestModule.Add(const Lines: array of string);
  1409. var
  1410. i: Integer;
  1411. begin
  1412. for i:=low(Lines) to high(Lines) do
  1413. Add(Lines[i]);
  1414. end;
  1415. procedure TCustomTestModule.StartParsing;
  1416. var
  1417. Src: String;
  1418. begin
  1419. Src:=Source.Text;
  1420. FEngine.Source:=Src;
  1421. FileResolver.AddStream(FileName,TStringStream.Create(Src));
  1422. Scanner.OpenFile(FileName);
  1423. Writeln('// Test : ',Self.TestName);
  1424. Writeln(Src);
  1425. end;
  1426. procedure TCustomTestModule.ParseModuleQueue;
  1427. var
  1428. i: Integer;
  1429. CurResolver: TTestEnginePasResolver;
  1430. Found: Boolean;
  1431. Section: TPasSection;
  1432. begin
  1433. // parse til exception or all modules finished
  1434. while not SkipTests do
  1435. begin
  1436. Found:=false;
  1437. for i:=0 to ResolverCount-1 do
  1438. begin
  1439. CurResolver:=Resolvers[i];
  1440. if CurResolver.CurrentParser=nil then continue;
  1441. if not CurResolver.CurrentParser.CanParseContinue(Section) then
  1442. continue;
  1443. CurResolver.Parser.ParseContinue;
  1444. Found:=true;
  1445. break;
  1446. end;
  1447. if not Found then break;
  1448. end;
  1449. for i:=0 to ResolverCount-1 do
  1450. begin
  1451. CurResolver:=Resolvers[i];
  1452. if CurResolver.Parser=nil then
  1453. begin
  1454. if CurResolver.CurrentParser<>nil then
  1455. Fail('TCustomTestModule.ParseModuleQueue '+CurResolver.Filename+' '+GetObjName(CurResolver.Parser)+'=Parser<>CurrentParser='+GetObjName(CurResolver.CurrentParser));
  1456. continue;
  1457. end;
  1458. if CurResolver.Parser.CurModule<>nil then
  1459. Fail('TCustomTestModule.ParseModuleQueue '+CurResolver.Filename+' NOT FINISHED CurModule='+GetObjName(CurResolver.Parser.CurModule));
  1460. end;
  1461. end;
  1462. procedure TCustomTestModule.ParseModule;
  1463. begin
  1464. if SkipTests then exit;
  1465. FFirstPasStatement:=nil;
  1466. try
  1467. StartParsing;
  1468. Parser.ParseMain(FModule);
  1469. ParseModuleQueue;
  1470. except
  1471. on E: Exception do
  1472. HandleException(E);
  1473. end;
  1474. if SkipTests then exit;
  1475. AssertNotNull('Module resulted in Module',Module);
  1476. AssertEquals('modulename',lowercase(ChangeFileExt(FFileName,'')),lowercase(Module.Name));
  1477. TAssert.AssertSame('Has resolver',Engine,Parser.Engine);
  1478. end;
  1479. procedure TCustomTestModule.ParseProgram;
  1480. begin
  1481. if SkipTests then exit;
  1482. ParseModule;
  1483. if SkipTests then exit;
  1484. AssertEquals('Has program',TPasProgram,Module.ClassType);
  1485. FPasProgram:=TPasProgram(Module);
  1486. AssertNotNull('Has program section',PasProgram.ProgramSection);
  1487. AssertNotNull('Has initialization section',PasProgram.InitializationSection);
  1488. if (PasProgram.InitializationSection.Elements.Count>0) then
  1489. if TObject(PasProgram.InitializationSection.Elements[0]) is TPasImplBlock then
  1490. FFirstPasStatement:=TPasImplBlock(PasProgram.InitializationSection.Elements[0]);
  1491. end;
  1492. procedure TCustomTestModule.ParseUnit;
  1493. begin
  1494. if SkipTests then exit;
  1495. ParseModule;
  1496. if SkipTests then exit;
  1497. AssertEquals('Has unit (TPasModule)',TPasModule,Module.ClassType);
  1498. AssertNotNull('Has interface section',Module.InterfaceSection);
  1499. AssertNotNull('Has implementation section',Module.ImplementationSection);
  1500. if (Module.InitializationSection<>nil)
  1501. and (Module.InitializationSection.Elements.Count>0)
  1502. and (TObject(Module.InitializationSection.Elements[0]) is TPasImplBlock) then
  1503. FFirstPasStatement:=TPasImplBlock(Module.InitializationSection.Elements[0]);
  1504. end;
  1505. function TCustomTestModule.FindModuleWithFilename(aFilename: string
  1506. ): TTestEnginePasResolver;
  1507. var
  1508. i: Integer;
  1509. begin
  1510. for i:=0 to ResolverCount-1 do
  1511. if CompareText(Resolvers[i].Filename,aFilename)=0 then
  1512. exit(Resolvers[i]);
  1513. Result:=nil;
  1514. end;
  1515. function TCustomTestModule.AddModule(aFilename: string
  1516. ): TTestEnginePasResolver;
  1517. begin
  1518. //writeln('TTestModuleConverter.AddModule ',aFilename);
  1519. if FindModuleWithFilename(aFilename)<>nil then
  1520. Fail('TTestModuleConverter.AddModule: file "'+aFilename+'" already exists');
  1521. Result:=TTestEnginePasResolver.Create;
  1522. Result.Filename:=aFilename;
  1523. Result.AddObjFPCBuiltInIdentifiers(btAllJSBaseTypes,bfAllJSBaseProcs);
  1524. Result.OnFindUnit:=@OnPasResolverFindUnit;
  1525. Result.OnLog:=@OnPasResolverLog;
  1526. Result.Hub:=Hub;
  1527. FModules.Add(Result);
  1528. end;
  1529. function TCustomTestModule.AddModuleWithSrc(aFilename, Src: string
  1530. ): TTestEnginePasResolver;
  1531. begin
  1532. Result:=AddModule(aFilename);
  1533. Result.Source:=Src;
  1534. end;
  1535. function TCustomTestModule.AddModuleWithIntfImplSrc(aFilename, InterfaceSrc,
  1536. ImplementationSrc: string): TTestEnginePasResolver;
  1537. var
  1538. Src: String;
  1539. begin
  1540. Src:='unit '+ExtractFileUnitName(aFilename)+';'+LineEnding;
  1541. Src+=LineEnding;
  1542. Src+='interface'+LineEnding;
  1543. Src+=LineEnding;
  1544. Src+=InterfaceSrc;
  1545. Src+='implementation'+LineEnding;
  1546. Src+=LineEnding;
  1547. Src+=ImplementationSrc;
  1548. Src+='end.'+LineEnding;
  1549. Result:=AddModuleWithSrc(aFilename,Src);
  1550. end;
  1551. procedure TCustomTestModule.AddSystemUnit(Parts: TSystemUnitParts);
  1552. var
  1553. Intf, Impl: TStringList;
  1554. begin
  1555. Intf:=TStringList.Create;
  1556. if supTInterfacedObject in Parts then Include(Parts,supTObject);
  1557. // unit interface
  1558. if [supTVarRec,supTypeInfo]*Parts<>[] then
  1559. Intf.Add('{$modeswitch externalclass}');
  1560. Intf.Add('type');
  1561. Intf.Add(' integer=longint;');
  1562. Intf.Add(' sizeint=nativeint;');
  1563. //'const',
  1564. //' LineEnding = #10;',
  1565. //' DirectorySeparator = ''/'';',
  1566. //' DriveSeparator = '''';',
  1567. //' AllowDirectorySeparators : set of char = [''\'',''/''];',
  1568. //' AllowDriveSeparators : set of char = [];',
  1569. if supTObject in Parts then
  1570. Intf.AddStrings([
  1571. 'type',
  1572. ' TClass = class of TObject;',
  1573. ' TObject = class',
  1574. ' constructor Create;',
  1575. ' destructor Destroy; virtual;',
  1576. ' class function ClassType: TClass; assembler;',
  1577. ' class function ClassName: String; assembler;',
  1578. ' class function ClassNameIs(const Name: string): boolean;',
  1579. ' class function ClassParent: TClass; assembler;',
  1580. ' class function InheritsFrom(aClass: TClass): boolean; assembler;',
  1581. ' class function UnitName: String; assembler;',
  1582. ' procedure AfterConstruction; virtual;',
  1583. ' procedure BeforeDestruction;virtual;',
  1584. ' function Equals(Obj: TObject): boolean; virtual;',
  1585. ' function ToString: String; virtual;',
  1586. ' end;']);
  1587. if supTInterfacedObject in Parts then
  1588. Intf.AddStrings([
  1589. ' {$Interfaces COM}',
  1590. ' IUnknown = interface',
  1591. ' [''{00000000-0000-0000-C000-000000000046}'']',
  1592. //' function QueryInterface(const iid: TGuid; out obj): Integer;',
  1593. ' function _AddRef: Integer;',
  1594. ' function _Release: Integer;',
  1595. ' end;',
  1596. ' IInterface = IUnknown;',
  1597. ' TInterfacedObject = class(TObject,IUnknown)',
  1598. ' protected',
  1599. ' fRefCount: Integer;',
  1600. ' { implement methods of IUnknown }',
  1601. //' function QueryInterface(const iid: TGuid; out obj): Integer; virtual;',
  1602. ' function _AddRef: Integer; virtual;',
  1603. ' function _Release: Integer; virtual;',
  1604. ' end;',
  1605. ' TInterfacedClass = class of TInterfacedObject;',
  1606. '',
  1607. '']);
  1608. if supTVarRec in Parts then
  1609. Intf.AddStrings([
  1610. 'const',
  1611. ' vtInteger = 0;',
  1612. ' vtBoolean = 1;',
  1613. ' vtJSValue = 19;',
  1614. 'type',
  1615. ' PVarRec = ^TVarRec;',
  1616. ' TVarRec = record',
  1617. ' VType : byte;',
  1618. ' VJSValue: JSValue;',
  1619. ' vInteger: longint external name ''VJSValue'';',
  1620. ' vBoolean: boolean external name ''VJSValue'';',
  1621. ' end;',
  1622. ' TVarRecArray = array of TVarRec;',
  1623. 'function VarRecs: TVarRecArray; varargs;',
  1624. '']);
  1625. if supTypeInfo in Parts then
  1626. begin
  1627. Intf.AddStrings([
  1628. 'type',
  1629. ' TTypeKind = (',
  1630. ' tkUnknown, // 0',
  1631. ' tkInteger, // 1',
  1632. ' tkChar, // 2 in Delphi/FPC tkWChar, tkUChar',
  1633. ' tkString, // 3 in Delphi/FPC tkSString, tkWString or tkUString',
  1634. ' tkEnumeration, // 4',
  1635. ' tkSet, // 5',
  1636. ' tkDouble, // 6',
  1637. ' tkBool, // 7',
  1638. ' tkProcVar, // 8 function or procedure',
  1639. ' tkMethod, // 9 proc var of object',
  1640. ' tkArray, // 10 static array',
  1641. ' tkDynArray, // 11',
  1642. ' tkRecord, // 12',
  1643. ' tkClass, // 13',
  1644. ' tkClassRef, // 14',
  1645. ' tkPointer, // 15',
  1646. ' tkJSValue, // 16',
  1647. ' tkRefToProcVar, // 17 variable of procedure type',
  1648. ' tkInterface, // 18',
  1649. ' //tkObject,',
  1650. ' //tkSString,tkLString,tkAString,tkWString,',
  1651. ' //tkVariant,',
  1652. ' //tkWChar,',
  1653. ' //tkInt64,',
  1654. ' //tkQWord,',
  1655. ' //tkInterfaceRaw,',
  1656. ' //tkUString,tkUChar,',
  1657. ' tkHelper, // 19',
  1658. ' //tkFile,',
  1659. ' tkExtClass // 20',
  1660. ' );',
  1661. ' TTypeKinds = set of TTypeKind;',
  1662. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  1663. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo)',
  1664. ' end;',
  1665. ' TTypeInfoEnum = class external name ''rtl.tTypeInfoEnum''(TTypeInfoInteger) end;',
  1666. ' TTypeInfoSet = class external name ''rtl.tTypeInfoSet''(TTypeInfo) end;',
  1667. ' TTypeInfoStaticArray = class external name ''rtl.tTypeInfoStaticArray''(TTypeInfo) end;',
  1668. ' TTypeInfoDynArray = class external name ''rtl.tTypeInfoDynArray''(TTypeInfo) end;',
  1669. ' TTypeInfoProcVar = class external name ''rtl.tTypeInfoProcVar''(TTypeInfo) end;',
  1670. ' TTypeInfoMethodVar = class external name ''rtl.tTypeInfoMethodVar''(TTypeInfoProcVar) end;',
  1671. ' TTypeInfoClass = class external name ''rtl.tTypeInfoClass''(TTypeInfo) end;',
  1672. ' TTypeInfoClassRef = class external name ''rtl.tTypeInfoClassRef''(TTypeInfo) end;',
  1673. ' TTypeInfoExtClass = class external name ''rtl.tTypeInfoExtClass''(TTypeInfo) end;',
  1674. ' TTypeInfoRecord = class external name ''rtl.tTypeInfoRecord''(TTypeInfo) end;',
  1675. ' TTypeInfoPointer = class external name ''rtl.tTypeInfoPointer''(TTypeInfo) end;',
  1676. ' TTypeInfoHelper = class external name ''rtl.tTypeInfoHelper''(TTypeInfo) end;',
  1677. ' TTypeInfoInterface = class external name ''rtl.tTypeInfoInterface''(TTypeInfo) end;',
  1678. '']);
  1679. end;
  1680. if supWriteln in Parts then
  1681. Intf.Add('procedure writeln; varargs; external name ''console.log'';');
  1682. Intf.Add('var');
  1683. Intf.Add(' ExitCode: Longint = 0;');
  1684. // unit implementation
  1685. Impl:=TStringList.Create;
  1686. if supTObject in Parts then
  1687. Impl.AddStrings([
  1688. '// needed by ClassNameIs, the real SameText is in SysUtils',
  1689. 'function SameText(const s1, s2: String): Boolean; assembler;',
  1690. 'asm',
  1691. 'end;',
  1692. 'constructor TObject.Create; begin end;',
  1693. 'destructor TObject.Destroy; begin end;',
  1694. 'class function TObject.ClassType: TClass; assembler;',
  1695. 'asm',
  1696. 'end;',
  1697. 'class function TObject.ClassName: String; assembler;',
  1698. 'asm',
  1699. 'end;',
  1700. 'class function TObject.ClassNameIs(const Name: string): boolean;',
  1701. 'begin',
  1702. ' Result:=SameText(Name,ClassName);',
  1703. 'end;',
  1704. 'class function TObject.ClassParent: TClass; assembler;',
  1705. 'asm',
  1706. 'end;',
  1707. 'class function TObject.InheritsFrom(aClass: TClass): boolean; assembler;',
  1708. 'asm',
  1709. 'end;',
  1710. 'class function TObject.UnitName: String; assembler;',
  1711. 'asm',
  1712. 'end;',
  1713. 'procedure TObject.AfterConstruction; begin end;',
  1714. 'procedure TObject.BeforeDestruction; begin end;',
  1715. 'function TObject.Equals(Obj: TObject): boolean;',
  1716. 'begin',
  1717. ' Result:=Obj=Self;',
  1718. 'end;',
  1719. 'function TObject.ToString: String;',
  1720. 'begin',
  1721. ' Result:=ClassName;',
  1722. 'end;'
  1723. ]);
  1724. if supTInterfacedObject in Parts then
  1725. Impl.AddStrings([
  1726. //'function TInterfacedObject.QueryInterface(const iid: TGuid; out obj): Integer;',
  1727. //'begin',
  1728. //'end;',
  1729. 'function TInterfacedObject._AddRef: Integer;',
  1730. 'begin',
  1731. 'end;',
  1732. 'function TInterfacedObject._Release: Integer;',
  1733. 'begin',
  1734. 'end;',
  1735. '']);
  1736. if supTVarRec in Parts then
  1737. Impl.AddStrings([
  1738. 'function VarRecs: TVarRecArray; varargs;',
  1739. 'var',
  1740. ' v: PVarRec;',
  1741. 'begin',
  1742. ' v^.VType:=1;',
  1743. ' v^.VJSValue:=2;',
  1744. 'end;',
  1745. '']);
  1746. try
  1747. AddModuleWithIntfImplSrc('system.pp',Intf.Text,Impl.Text);
  1748. finally
  1749. Intf.Free;
  1750. Impl.Free;
  1751. end;
  1752. end;
  1753. procedure TCustomTestModule.StartProgram(NeedSystemUnit: boolean;
  1754. SystemUnitParts: TSystemUnitParts);
  1755. begin
  1756. if NeedSystemUnit then
  1757. AddSystemUnit(SystemUnitParts)
  1758. else
  1759. Parser.ImplicitUses.Clear;
  1760. Add('program '+ExtractFileUnitName(Filename)+';');
  1761. Add('');
  1762. end;
  1763. procedure TCustomTestModule.StartUnit(NeedSystemUnit: boolean;
  1764. SystemUnitParts: TSystemUnitParts);
  1765. begin
  1766. if NeedSystemUnit then
  1767. AddSystemUnit(SystemUnitParts)
  1768. else
  1769. Parser.ImplicitUses.Clear;
  1770. Add('unit Test1;');
  1771. Add('');
  1772. end;
  1773. procedure TCustomTestModule.ConvertModule;
  1774. procedure CheckUsesList(UsesName: String; Arg: TJSArrayLiteralElement;
  1775. out UsesLit: TJSArrayLiteral);
  1776. var
  1777. i: Integer;
  1778. Item: TJSElement;
  1779. Lit: TJSLiteral;
  1780. begin
  1781. UsesLit:=nil;
  1782. AssertNotNull(UsesName+' uses section',Arg.Expr);
  1783. if (Arg.Expr.ClassType=TJSLiteral) and TJSLiteral(Arg.Expr).Value.IsNull then
  1784. exit; // null is ok
  1785. AssertEquals(UsesName+' uses section param is array',TJSArrayLiteral,Arg.Expr.ClassType);
  1786. FJSInterfaceUses:=TJSArrayLiteral(Arg.Expr);
  1787. for i:=0 to FJSInterfaceUses.Elements.Count-1 do
  1788. begin
  1789. Item:=FJSInterfaceUses.Elements.Elements[i].Expr;
  1790. AssertNotNull(UsesName+' uses section item['+IntToStr(i)+'].Expr',Item);
  1791. AssertEquals(UsesName+' uses section item['+IntToStr(i)+'] is lit',TJSLiteral,Item.ClassType);
  1792. Lit:=TJSLiteral(Item);
  1793. AssertEquals(UsesName+' uses section item['+IntToStr(i)+'] is string lit',
  1794. ord(jsbase.jstString),ord(Lit.Value.ValueType));
  1795. end;
  1796. end;
  1797. procedure CheckFunctionParam(ParamName: string; Arg: TJSArrayLiteralElement;
  1798. out Src: TJSSourceElements);
  1799. var
  1800. FunDecl: TJSFunctionDeclarationStatement;
  1801. FunDef: TJSFuncDef;
  1802. FunBody: TJSFunctionBody;
  1803. begin
  1804. Src:=nil;
  1805. AssertNotNull(ParamName,Arg.Expr);
  1806. AssertEquals(ParamName+' Arg.Expr type',TJSFunctionDeclarationStatement,Arg.Expr.ClassType);
  1807. FunDecl:=Arg.Expr as TJSFunctionDeclarationStatement;
  1808. AssertNotNull(ParamName+' FunDecl.AFunction',FunDecl.AFunction);
  1809. AssertEquals(ParamName+' FunDecl.AFunction type',TJSFuncDef,FunDecl.AFunction.ClassType);
  1810. FunDef:=FunDecl.AFunction as TJSFuncDef;
  1811. AssertEquals(ParamName+' name empty','',String(FunDef.Name));
  1812. AssertNotNull(ParamName+' body',FunDef.Body);
  1813. AssertEquals(ParamName+' body type',TJSFunctionBody,FunDef.Body.ClassType);
  1814. FunBody:=FunDef.Body as TJSFunctionBody;
  1815. AssertNotNull(ParamName+' body.A',FunBody.A);
  1816. AssertEquals(ParamName+' body.A type',TJSSourceElements,FunBody.A.ClassType);
  1817. Src:=FunBody.A as TJSSourceElements;
  1818. end;
  1819. var
  1820. ModuleNameExpr: TJSLiteral;
  1821. InitFunction: TJSFunctionDeclarationStatement;
  1822. InitAssign: TJSSimpleAssignStatement;
  1823. InitName: String;
  1824. LastNode: TJSElement;
  1825. Arg: TJSArrayLiteralElement;
  1826. begin
  1827. if SkipTests then exit;
  1828. try
  1829. FJSModule:=FConverter.ConvertPasElement(Module,Engine) as TJSSourceElements;
  1830. except
  1831. on E: Exception do
  1832. HandleException(E);
  1833. end;
  1834. if SkipTests then exit;
  1835. if ExpectedErrorClass<>nil then
  1836. Fail('Missing '+ExpectedErrorClass.ClassName+' error {'+ExpectedErrorMsg+'} ('+IntToStr(ExpectedErrorNumber)+')');
  1837. FJSSource:=TStringList.Create;
  1838. FJSSource.Text:=ConvertJSModuleToString(JSModule);
  1839. {$IFDEF VerbosePas2JS}
  1840. writeln('TTestModule.ConvertModule JS:');
  1841. write(FJSSource.Text);
  1842. {$ENDIF}
  1843. // rtl.module(...
  1844. AssertEquals('jsmodule has one statement - the call',1,JSModule.Statements.Count);
  1845. AssertNotNull('register module call',JSModule.Statements.Nodes[0].Node);
  1846. AssertEquals('register module call',TJSCallExpression,JSModule.Statements.Nodes[0].Node.ClassType);
  1847. FJSRegModuleCall:=JSModule.Statements.Nodes[0].Node as TJSCallExpression;
  1848. AssertNotNull('register module rtl.module expr',JSRegModuleCall.Expr);
  1849. AssertNotNull('register module rtl.module args',JSRegModuleCall.Args);
  1850. AssertEquals('rtl.module args',TJSArguments,JSRegModuleCall.Args.ClassType);
  1851. FJSModuleCallArgs:=JSRegModuleCall.Args as TJSArguments;
  1852. // parameter 'unitname'
  1853. if JSModuleCallArgs.Elements.Count<1 then
  1854. Fail('rtl.module first param unit missing');
  1855. Arg:=JSModuleCallArgs.Elements.Elements[0];
  1856. AssertNotNull('module name param',Arg.Expr);
  1857. ModuleNameExpr:=Arg.Expr as TJSLiteral;
  1858. AssertEquals('module name param is string',ord(jstString),ord(ModuleNameExpr.Value.ValueType));
  1859. if Module is TPasProgram then
  1860. AssertEquals('module name','program',String(ModuleNameExpr.Value.AsString))
  1861. else
  1862. AssertEquals('module name',Module.Name,String(ModuleNameExpr.Value.AsString));
  1863. // main uses section
  1864. if JSModuleCallArgs.Elements.Count<2 then
  1865. Fail('rtl.module second param main uses missing');
  1866. Arg:=JSModuleCallArgs.Elements.Elements[1];
  1867. CheckUsesList('interface',Arg,FJSInterfaceUses);
  1868. // program/library/interface function()
  1869. if JSModuleCallArgs.Elements.Count<3 then
  1870. Fail('rtl.module third param intf-function missing');
  1871. Arg:=JSModuleCallArgs.Elements.Elements[2];
  1872. CheckFunctionParam('module intf-function',Arg,FJSModuleSrc);
  1873. // search for $mod.$init or $mod.$main - the last statement
  1874. if Module is TPasProgram then
  1875. begin
  1876. InitName:='$main';
  1877. AssertEquals('$mod.'+InitName+' function 1',true,JSModuleSrc.Statements.Count>0);
  1878. end
  1879. else
  1880. InitName:='$init';
  1881. FJSInitBody:=nil;
  1882. if JSModuleSrc.Statements.Count>0 then
  1883. begin
  1884. LastNode:=JSModuleSrc.Statements.Nodes[JSModuleSrc.Statements.Count-1].Node;
  1885. if LastNode is TJSSimpleAssignStatement then
  1886. begin
  1887. InitAssign:=LastNode as TJSSimpleAssignStatement;
  1888. if GetDottedIdentifier(InitAssign.LHS)='$mod.'+InitName then
  1889. begin
  1890. InitFunction:=InitAssign.Expr as TJSFunctionDeclarationStatement;
  1891. FJSInitBody:=InitFunction.AFunction.Body as TJSFunctionBody;
  1892. end
  1893. else if Module is TPasProgram then
  1894. CheckDottedIdentifier('init function',InitAssign.LHS,'$mod.'+InitName);
  1895. end;
  1896. end;
  1897. // optional: implementation uses section
  1898. if JSModuleCallArgs.Elements.Count<4 then
  1899. exit;
  1900. Arg:=JSModuleCallArgs.Elements.Elements[3];
  1901. CheckUsesList('implementation',Arg,FJSImplentationUses);
  1902. end;
  1903. procedure TCustomTestModule.ConvertProgram;
  1904. begin
  1905. Add('end.');
  1906. ParseProgram;
  1907. ConvertModule;
  1908. end;
  1909. procedure TCustomTestModule.ConvertUnit;
  1910. begin
  1911. Add('end.');
  1912. ParseUnit;
  1913. ConvertModule;
  1914. end;
  1915. function TCustomTestModule.ConvertJSModuleToString(El: TJSElement): string;
  1916. begin
  1917. Result:=tcmodules.JSToStr(El);
  1918. end;
  1919. procedure TCustomTestModule.CheckDottedIdentifier(Msg: string; El: TJSElement;
  1920. DottedName: string);
  1921. begin
  1922. if DottedName='' then
  1923. begin
  1924. AssertNull(Msg,El);
  1925. end
  1926. else
  1927. begin
  1928. AssertNotNull(Msg,El);
  1929. AssertEquals(Msg,DottedName,GetDottedIdentifier(El));
  1930. end;
  1931. end;
  1932. function TCustomTestModule.GetDottedIdentifier(El: TJSElement): string;
  1933. begin
  1934. if El=nil then
  1935. Result:=''
  1936. else if El is TJSPrimaryExpressionIdent then
  1937. Result:=String(TJSPrimaryExpressionIdent(El).Name)
  1938. else if El is TJSDotMemberExpression then
  1939. Result:=GetDottedIdentifier(TJSDotMemberExpression(El).MExpr)+'.'+String(TJSDotMemberExpression(El).Name)
  1940. else
  1941. AssertEquals('GetDottedIdentifier',TJSPrimaryExpressionIdent,El.ClassType);
  1942. end;
  1943. procedure TCustomTestModule.CheckSource(Msg, Statements: String;
  1944. InitStatements: string; ImplStatements: string);
  1945. var
  1946. ActualSrc, ExpectedSrc, InitName: String;
  1947. begin
  1948. ActualSrc:=JSToStr(JSModuleSrc);
  1949. if coUseStrict in Converter.Options then
  1950. ExpectedSrc:='"use strict";'+LineEnding
  1951. else
  1952. ExpectedSrc:='';
  1953. ExpectedSrc:=ExpectedSrc+'var $mod = this;'+LineEnding;
  1954. ExpectedSrc:=ExpectedSrc+Statements;
  1955. // unit implementation
  1956. if (Trim(ImplStatements)<>'') then
  1957. ExpectedSrc:=ExpectedSrc+LineEnding
  1958. +'$mod.$implcode = function () {'+LineEnding
  1959. +ImplStatements
  1960. +'};'+LineEnding;
  1961. // program main or unit initialization
  1962. if (Module is TPasProgram) or (Trim(InitStatements)<>'') then
  1963. begin
  1964. if Module is TPasProgram then
  1965. InitName:='$main'
  1966. else
  1967. InitName:='$init';
  1968. ExpectedSrc:=ExpectedSrc+LineEnding
  1969. +'$mod.'+InitName+' = function () {'+LineEnding
  1970. +InitStatements
  1971. +'};'+LineEnding;
  1972. end;
  1973. //writeln('TCustomTestModule.CheckSource ExpectedIntf="',ExpectedSrc,'"');
  1974. //writeln('TTestModule.CheckSource InitStatements="',Trim(InitStatements),'"');
  1975. CheckDiff(Msg,ExpectedSrc,ActualSrc);
  1976. end;
  1977. procedure TCustomTestModule.CheckDiff(Msg, Expected, Actual: string);
  1978. // search diff, ignore changes in spaces
  1979. var
  1980. s: string;
  1981. begin
  1982. if CheckSrcDiff(Expected,Actual,s) then exit;
  1983. Fail(Msg+': '+s);
  1984. end;
  1985. procedure TCustomTestModule.CheckUnit(Filename, ExpectedSrc: string);
  1986. var
  1987. aResolver: TTestEnginePasResolver;
  1988. aConverter: TPasToJSConverter;
  1989. aJSModule: TJSSourceElements;
  1990. ActualSrc: String;
  1991. begin
  1992. aResolver:=GetResolver(Filename);
  1993. AssertNotNull('missing resolver of unit '+Filename,aResolver);
  1994. AssertNotNull('missing resolver.module of unit '+Filename,aResolver.Module);
  1995. {$IFDEF VerbosePas2JS}
  1996. writeln('CheckUnit '+Filename+' converting ...');
  1997. {$ENDIF}
  1998. aConverter:=CreateConverter;
  1999. aJSModule:=nil;
  2000. try
  2001. try
  2002. aJSModule:=aConverter.ConvertPasElement(aResolver.Module,aResolver) as TJSSourceElements;
  2003. except
  2004. on E: Exception do
  2005. HandleException(E);
  2006. end;
  2007. ActualSrc:=ConvertJSModuleToString(aJSModule);
  2008. {$IFDEF VerbosePas2JS}
  2009. writeln('TTestModule.CheckUnit ',Filename,' Pas:');
  2010. write(aResolver.Source);
  2011. writeln('TTestModule.CheckUnit ',Filename,' JS:');
  2012. write(ActualSrc);
  2013. {$ENDIF}
  2014. CheckDiff('Converted unit: "'+ChangeFileExt(Filename,'.js')+'"',ExpectedSrc,ActualSrc);
  2015. finally
  2016. aJSModule.Free;
  2017. aConverter.Free;
  2018. end;
  2019. end;
  2020. procedure TCustomTestModule.CheckHint(MsgType: TMessageType;
  2021. MsgNumber: integer; Msg: string; Marker: PSrcMarker);
  2022. var
  2023. i: Integer;
  2024. Item: TTestHintMessage;
  2025. Expected,Actual: string;
  2026. begin
  2027. //writeln('TCustomTestModule.CheckHint MsgCount=',MsgCount);
  2028. for i:=0 to MsgCount-1 do
  2029. begin
  2030. Item:=Msgs[i];
  2031. if (Item.MsgNumber<>MsgNumber) or (Item.Msg<>Msg) then continue;
  2032. if (Marker<>nil) then
  2033. begin
  2034. if Item.SourcePos.Row<>cardinal(Marker^.Row) then continue;
  2035. if (Item.SourcePos.Column<cardinal(Marker^.StartCol))
  2036. or (Item.SourcePos.Column>cardinal(Marker^.EndCol)) then continue;
  2037. end;
  2038. // found
  2039. FHintMsgsGood.Add(Item);
  2040. str(Item.MsgType,Actual);
  2041. str(MsgType,Expected);
  2042. AssertEquals('MsgType',Expected,Actual);
  2043. exit;
  2044. end;
  2045. // needed message missing -> show emitted messages
  2046. WriteSources('',0,0);
  2047. for i:=0 to MsgCount-1 do
  2048. begin
  2049. Item:=Msgs[i];
  2050. write('TCustomTestModule.CheckHint ',i,'/',MsgCount,' ',Item.MsgType,
  2051. ' ('+IntToStr(Item.MsgNumber),')');
  2052. if Marker<>nil then
  2053. write(' '+ExtractFileName(Item.SourcePos.FileName),'(',Item.SourcePos.Row,',',Item.SourcePos.Column,')');
  2054. writeln(' {',Item.Msg,'}');
  2055. end;
  2056. str(MsgType,Expected);
  2057. Actual:='Missing '+Expected+' ('+IntToStr(MsgNumber)+')';
  2058. if Marker<>nil then
  2059. Actual:=Actual+' '+ExtractFileName(Marker^.Filename)+'('+IntToStr(Marker^.Row)+','+IntToStr(Marker^.StartCol)+'..'+IntToStr(Marker^.EndCol)+')';
  2060. Actual:=Actual+' '+Msg;
  2061. Fail(Actual);
  2062. end;
  2063. procedure TCustomTestModule.CheckResolverUnexpectedHints(WithSourcePos: boolean
  2064. );
  2065. var
  2066. i: Integer;
  2067. s, Txt: String;
  2068. Msg: TTestHintMessage;
  2069. begin
  2070. for i:=0 to MsgCount-1 do
  2071. begin
  2072. Msg:=Msgs[i];
  2073. if FHintMsgsGood.IndexOf(Msg)>=0 then continue;
  2074. s:='';
  2075. str(Msg.MsgType,s);
  2076. Txt:='Unexpected resolver message found ['+IntToStr(Msg.Id)+'] '
  2077. +s+': ('+IntToStr(Msg.MsgNumber)+')';
  2078. if WithSourcePos then
  2079. Txt:=Txt+' '+ExtractFileName(Msg.SourcePos.FileName)+'('+IntToStr(Msg.SourcePos.Row)+','+IntToStr(Msg.SourcePos.Column)+')';
  2080. Txt:=Txt+' {'+Msg.Msg+'}';
  2081. Fail(Txt);
  2082. end;
  2083. end;
  2084. procedure TCustomTestModule.SetExpectedScannerError(Msg: string;
  2085. MsgNumber: integer);
  2086. begin
  2087. ExpectedErrorClass:=EScannerError;
  2088. ExpectedErrorMsg:=Msg;
  2089. ExpectedErrorNumber:=MsgNumber;
  2090. end;
  2091. procedure TCustomTestModule.SetExpectedParserError(Msg: string;
  2092. MsgNumber: integer);
  2093. begin
  2094. ExpectedErrorClass:=EParserError;
  2095. ExpectedErrorMsg:=Msg;
  2096. ExpectedErrorNumber:=MsgNumber;
  2097. end;
  2098. procedure TCustomTestModule.SetExpectedPasResolverError(Msg: string;
  2099. MsgNumber: integer);
  2100. begin
  2101. ExpectedErrorClass:=EPasResolve;
  2102. ExpectedErrorMsg:=Msg;
  2103. ExpectedErrorNumber:=MsgNumber;
  2104. end;
  2105. procedure TCustomTestModule.SetExpectedConverterError(Msg: string;
  2106. MsgNumber: integer);
  2107. begin
  2108. ExpectedErrorClass:=EPas2JS;
  2109. ExpectedErrorMsg:=Msg;
  2110. ExpectedErrorNumber:=MsgNumber;
  2111. end;
  2112. function TCustomTestModule.IsErrorExpected(E: Exception): boolean;
  2113. var
  2114. MsgNumber: Integer;
  2115. Msg: String;
  2116. begin
  2117. Result:=false;
  2118. if (ExpectedErrorClass=nil) or (ExpectedErrorClass<>E.ClassType) then exit;
  2119. Msg:=E.Message;
  2120. if E is EPas2JS then
  2121. MsgNumber:=EPas2JS(E).MsgNumber
  2122. else if E is EPasResolve then
  2123. MsgNumber:=EPasResolve(E).MsgNumber
  2124. else if E is EParserError then
  2125. MsgNumber:=Parser.LastMsgNumber
  2126. else if E is EScannerError then
  2127. begin
  2128. MsgNumber:=Scanner.LastMsgNumber;
  2129. Msg:=Scanner.LastMsg;
  2130. end
  2131. else
  2132. MsgNumber:=0;
  2133. Result:=(MsgNumber=ExpectedErrorNumber) and (Msg=ExpectedErrorMsg);
  2134. if Result then
  2135. SkipTests:=true;
  2136. end;
  2137. procedure TCustomTestModule.HandleScannerError(E: EScannerError);
  2138. begin
  2139. if IsErrorExpected(E) then exit;
  2140. WriteSources(Scanner.CurFilename,Scanner.CurRow,Scanner.CurColumn);
  2141. writeln('ERROR: TCustomTestModule.HandleScannerError '+E.ClassName+':'+E.Message
  2142. +' '+Scanner.CurFilename
  2143. +'('+IntToStr(Scanner.CurRow)+','+IntToStr(Scanner.CurColumn)+')');
  2144. FailException(E);
  2145. end;
  2146. procedure TCustomTestModule.HandleParserError(E: EParserError);
  2147. begin
  2148. if IsErrorExpected(E) then exit;
  2149. WriteSources(E.Filename,E.Row,E.Column);
  2150. writeln('ERROR: TCustomTestModule.HandleParserError '+E.ClassName+':'+E.Message
  2151. +' '+E.Filename+'('+IntToStr(E.Row)+','+IntToStr(E.Column)+')'
  2152. +' MainModuleScannerLine="'+Scanner.CurLine+'"'
  2153. );
  2154. FailException(E);
  2155. end;
  2156. procedure TCustomTestModule.HandlePasResolveError(E: EPasResolve);
  2157. var
  2158. P: TPasSourcePos;
  2159. begin
  2160. if IsErrorExpected(E) then exit;
  2161. P:=E.SourcePos;
  2162. WriteSources(P.FileName,P.Row,P.Column);
  2163. writeln('ERROR: TCustomTestModule.HandlePasResolveError '+E.ClassName+':'+E.Message
  2164. +' '+P.FileName+'('+IntToStr(P.Row)+','+IntToStr(P.Column)+')');
  2165. FailException(E);
  2166. end;
  2167. procedure TCustomTestModule.HandlePas2JSError(E: EPas2JS);
  2168. var
  2169. Row, Col: integer;
  2170. begin
  2171. if IsErrorExpected(E) then exit;
  2172. Engine.UnmangleSourceLineNumber(E.PasElement.SourceLinenumber,Row,Col);
  2173. WriteSources(E.PasElement.SourceFilename,Row,Col);
  2174. writeln('ERROR: TCustomTestModule.HandlePas2JSError '+E.ClassName+':'+E.Message
  2175. +' '+E.PasElement.SourceFilename
  2176. +'('+IntToStr(Row)+','+IntToStr(Col)+')');
  2177. FailException(E);
  2178. end;
  2179. procedure TCustomTestModule.HandleException(E: Exception);
  2180. begin
  2181. if E is EScannerError then
  2182. HandleScannerError(EScannerError(E))
  2183. else if E is EParserError then
  2184. HandleParserError(EParserError(E))
  2185. else if E is EPasResolve then
  2186. HandlePasResolveError(EPasResolve(E))
  2187. else if E is EPas2JS then
  2188. HandlePas2JSError(EPas2JS(E))
  2189. else
  2190. begin
  2191. if IsErrorExpected(E) then exit;
  2192. if not (E is EAssertionFailedError) then
  2193. begin
  2194. WriteSources('',0,0);
  2195. writeln('ERROR: TCustomTestModule.HandleException '+E.ClassName+':'+E.Message);
  2196. end;
  2197. FailException(E);
  2198. end;
  2199. end;
  2200. procedure TCustomTestModule.FailException(E: Exception);
  2201. var
  2202. MsgNumber: Integer;
  2203. begin
  2204. if ExpectedErrorClass<>nil then
  2205. begin
  2206. if FExpectedErrorClass=E.ClassType then
  2207. begin
  2208. if E is EPas2JS then
  2209. MsgNumber:=EPas2JS(E).MsgNumber
  2210. else if E is EPasResolve then
  2211. MsgNumber:=EPasResolve(E).MsgNumber
  2212. else if E is EParserError then
  2213. MsgNumber:=Parser.LastMsgNumber
  2214. else if E is EScannerError then
  2215. MsgNumber:=Scanner.LastMsgNumber
  2216. else
  2217. MsgNumber:=0;
  2218. AssertEquals('Expected error message ('+IntToStr(ExpectedErrorNumber)+')','{'+ExpectedErrorMsg+'}','{'+E.Message+'}');
  2219. AssertEquals('Expected {'+ExpectedErrorMsg+'}, but got msg {'+E.Message+'} number',
  2220. ExpectedErrorNumber,MsgNumber);
  2221. end else begin
  2222. AssertEquals('Wrong exception class',ExpectedErrorClass.ClassName,E.ClassName);
  2223. end;
  2224. end;
  2225. Fail(E.Message);
  2226. end;
  2227. procedure TCustomTestModule.WriteSources(const aFilename: string; aRow,
  2228. aCol: integer);
  2229. var
  2230. IsSrc: Boolean;
  2231. i, j: Integer;
  2232. SrcLines: TStringList;
  2233. Line: string;
  2234. aModule: TTestEnginePasResolver;
  2235. begin
  2236. writeln('TCustomTestModule.WriteSources File="',aFilename,'" Row=',aRow,' Col=',aCol);
  2237. for i:=0 to ResolverCount-1 do
  2238. begin
  2239. aModule:=Resolvers[i];
  2240. SrcLines:=TStringList.Create;
  2241. try
  2242. SrcLines.Text:=aModule.Source;
  2243. IsSrc:=ExtractFilename(aModule.Filename)=ExtractFileName(aFilename);
  2244. writeln('Testcode:-File="',aModule.Filename,'"----------------------------------:');
  2245. for j:=1 to SrcLines.Count do
  2246. begin
  2247. Line:=SrcLines[j-1];
  2248. if IsSrc and (j=aRow) then
  2249. begin
  2250. write('*');
  2251. Line:=LeftStr(Line,aCol-1)+'|'+copy(Line,aCol,length(Line));
  2252. end;
  2253. writeln(Format('%:4d: ',[j]),Line);
  2254. end;
  2255. finally
  2256. SrcLines.Free;
  2257. end;
  2258. end;
  2259. end;
  2260. function TCustomTestModule.IndexOfResolver(const Filename: string): integer;
  2261. var
  2262. i: Integer;
  2263. begin
  2264. for i:=0 to ResolverCount-1 do
  2265. if Filename=Resolvers[i].Filename then exit(i);
  2266. Result:=-1;
  2267. end;
  2268. function TCustomTestModule.GetResolver(const Filename: string
  2269. ): TTestEnginePasResolver;
  2270. var
  2271. i: Integer;
  2272. begin
  2273. i:=IndexOfResolver(Filename);
  2274. if i<0 then exit(nil);
  2275. Result:=Resolvers[i];
  2276. end;
  2277. function TCustomTestModule.GetDefaultNamespace: string;
  2278. var
  2279. C: TClass;
  2280. begin
  2281. Result:='';
  2282. if FModule=nil then exit;
  2283. C:=FModule.ClassType;
  2284. if (C=TPasProgram) or (C=TPasLibrary) or (C=TPasPackage) then
  2285. Result:=Engine.DefaultNameSpace;
  2286. end;
  2287. constructor TCustomTestModule.Create;
  2288. begin
  2289. inherited Create;
  2290. FHintMsgs:=TObjectList.Create(true);
  2291. FHintMsgsGood:=TFPList.Create;
  2292. end;
  2293. destructor TCustomTestModule.Destroy;
  2294. begin
  2295. FreeAndNil(FHintMsgs);
  2296. FreeAndNil(FHintMsgsGood);
  2297. inherited Destroy;
  2298. end;
  2299. { TTestModule }
  2300. procedure TTestModule.TestReservedWords;
  2301. var
  2302. i: integer;
  2303. begin
  2304. for i:=low(JSReservedWords) to High(JSReservedWords)-1 do
  2305. if CompareStr(JSReservedWords[i],JSReservedWords[i+1])>=0 then
  2306. Fail('20170203135442 '+JSReservedWords[i]+' >= '+JSReservedWords[i+1]);
  2307. for i:=low(JSReservedGlobalWords) to High(JSReservedGlobalWords)-1 do
  2308. if CompareStr(JSReservedGlobalWords[i],JSReservedGlobalWords[i+1])>=0 then
  2309. Fail('20170203135443 '+JSReservedGlobalWords[i]+' >= '+JSReservedGlobalWords[i+1]);
  2310. end;
  2311. procedure TTestModule.TestEmptyProgram;
  2312. begin
  2313. StartProgram(false);
  2314. Add('begin');
  2315. ConvertProgram;
  2316. CheckSource('TestEmptyProgram','','');
  2317. end;
  2318. procedure TTestModule.TestEmptyProgramUseStrict;
  2319. begin
  2320. Converter.Options:=Converter.Options+[coUseStrict];
  2321. StartProgram(false);
  2322. Add('begin');
  2323. ConvertProgram;
  2324. CheckSource('TestEmptyProgramUseStrict','','');
  2325. end;
  2326. procedure TTestModule.TestEmptyUnit;
  2327. begin
  2328. StartUnit(false);
  2329. Add('interface');
  2330. Add('implementation');
  2331. ConvertUnit;
  2332. CheckSource('TestEmptyUnit',
  2333. LinesToStr([
  2334. ]),
  2335. '');
  2336. end;
  2337. procedure TTestModule.TestEmptyUnitUseStrict;
  2338. begin
  2339. Converter.Options:=Converter.Options+[coUseStrict];
  2340. StartUnit(false);
  2341. Add('interface');
  2342. Add('implementation');
  2343. ConvertUnit;
  2344. CheckSource('TestEmptyUnitUseStrict',
  2345. LinesToStr([
  2346. ''
  2347. ]),
  2348. '');
  2349. end;
  2350. procedure TTestModule.TestDottedUnitNames;
  2351. begin
  2352. AddModuleWithIntfImplSrc('NS1.Unit2.pas',
  2353. LinesToStr([
  2354. 'var iV: longint;'
  2355. ]),
  2356. '');
  2357. FFilename:='ns1.test1.pp';
  2358. StartProgram(true);
  2359. Add('uses unIt2;');
  2360. Add('var');
  2361. Add(' i: longint;');
  2362. Add('begin');
  2363. Add(' i:=iv;');
  2364. Add(' i:=uNit2.iv;');
  2365. Add(' i:=Ns1.TEst1.i;');
  2366. ConvertProgram;
  2367. CheckSource('TestDottedUnitNames',
  2368. LinesToStr([
  2369. 'this.i = 0;',
  2370. '']),
  2371. LinesToStr([ // this.$init
  2372. '$mod.i = pas["NS1.Unit2"].iV;',
  2373. '$mod.i = pas["NS1.Unit2"].iV;',
  2374. '$mod.i = $mod.i;',
  2375. '']) );
  2376. end;
  2377. procedure TTestModule.TestDottedUnitNameImpl;
  2378. begin
  2379. AddModuleWithIntfImplSrc('TEST.UnitA.pas',
  2380. LinesToStr([
  2381. 'type',
  2382. ' TObject = class end;',
  2383. ' TTestA = class',
  2384. ' end;'
  2385. ]),
  2386. LinesToStr(['uses TEST.UnitB;'])
  2387. );
  2388. AddModuleWithIntfImplSrc('TEST.UnitB.pas',
  2389. LinesToStr([
  2390. 'uses TEST.UnitA;',
  2391. 'type TTestB = class(TTestA);'
  2392. ]),
  2393. ''
  2394. );
  2395. StartProgram(true);
  2396. Add('uses TEST.UnitA;');
  2397. Add('begin');
  2398. ConvertProgram;
  2399. CheckSource('TestDottedUnitNameImpl',
  2400. LinesToStr([
  2401. '']),
  2402. LinesToStr([ // this.$init
  2403. '']) );
  2404. CheckUnit('TEST.UnitA.pas',
  2405. LinesToStr([
  2406. 'rtl.module("TEST.UnitA", ["system"], function () {',
  2407. ' var $mod = this;',
  2408. ' rtl.createClass(this, "TObject", null, function () {',
  2409. ' this.$init = function () {',
  2410. ' };',
  2411. ' this.$final = function () {',
  2412. ' };',
  2413. ' });',
  2414. ' rtl.createClass(this, "TTestA", this.TObject, function () {',
  2415. ' });',
  2416. '}, ["TEST.UnitB"]);'
  2417. ]));
  2418. CheckUnit('TEST.UnitB.pas',
  2419. LinesToStr([
  2420. 'rtl.module("TEST.UnitB", ["system","TEST.UnitA"], function () {',
  2421. ' var $mod = this;',
  2422. ' rtl.createClass(this, "TTestB", pas["TEST.UnitA"].TTestA, function () {',
  2423. ' });',
  2424. '});'
  2425. ]));
  2426. end;
  2427. procedure TTestModule.TestDottedUnitExpr;
  2428. begin
  2429. AddModuleWithIntfImplSrc('NS2.SubNs2.Unit2.pas',
  2430. LinesToStr([
  2431. 'procedure DoIt;'
  2432. ]),
  2433. 'procedure DoIt; begin end;');
  2434. FFilename:='Ns1.SubNs1.Test1.pp';
  2435. StartProgram(true);
  2436. Add('uses Ns2.sUbnS2.unIt2;');
  2437. Add('var');
  2438. Add(' i: longint;');
  2439. Add('begin');
  2440. Add(' ns2.subns2.unit2.doit;');
  2441. Add(' i:=Ns1.SubNS1.TEst1.i;');
  2442. ConvertProgram;
  2443. CheckSource('TestDottedUnitExpr',
  2444. LinesToStr([
  2445. 'this.i = 0;',
  2446. '']),
  2447. LinesToStr([ // this.$init
  2448. 'pas["NS2.SubNs2.Unit2"].DoIt();',
  2449. '$mod.i = $mod.i;',
  2450. '']) );
  2451. end;
  2452. procedure TTestModule.Test_ModeFPCFail;
  2453. begin
  2454. StartProgram(false);
  2455. Add('{$mode FPC}');
  2456. Add('begin');
  2457. SetExpectedScannerError('Invalid mode: "FPC"',nErrInvalidMode);
  2458. ConvertProgram;
  2459. end;
  2460. procedure TTestModule.Test_ModeSwitchCBlocksFail;
  2461. begin
  2462. StartProgram(false);
  2463. Add('{$modeswitch cblocks-}');
  2464. Add('begin');
  2465. ConvertProgram;
  2466. CheckHint(mtWarning,nErrInvalidModeSwitch,'Warning: test1.pp(3,23) : Invalid mode switch: "cblocks"');
  2467. CheckResolverUnexpectedHints();
  2468. end;
  2469. procedure TTestModule.TestUnit_UseSystem;
  2470. begin
  2471. StartUnit(true);
  2472. Add([
  2473. 'interface',
  2474. 'var i: integer;',
  2475. 'implementation']);
  2476. ConvertUnit;
  2477. CheckSource('TestUnit_UseSystem',
  2478. LinesToStr([
  2479. 'this.i = 0;',
  2480. '']),
  2481. LinesToStr([
  2482. '']) );
  2483. end;
  2484. procedure TTestModule.TestUnit_Intf1Impl2Intf1;
  2485. begin
  2486. AddModuleWithIntfImplSrc('unit1.pp',
  2487. LinesToStr([
  2488. 'type number = longint;']),
  2489. LinesToStr([
  2490. 'uses test1;',
  2491. 'procedure DoIt;',
  2492. 'begin',
  2493. ' i:=3;',
  2494. 'end;']));
  2495. StartUnit(true);
  2496. Add([
  2497. 'interface',
  2498. 'uses unit1;',
  2499. 'var i: number;',
  2500. 'implementation']);
  2501. ConvertUnit;
  2502. CheckSource('TestUnit_Intf1Impl2Intf1',
  2503. LinesToStr([
  2504. 'this.i = 0;',
  2505. '']),
  2506. LinesToStr([
  2507. '']) );
  2508. end;
  2509. procedure TTestModule.TestIncludeVersion;
  2510. begin
  2511. StartProgram(false);
  2512. Add([
  2513. 'var',
  2514. ' s: string;',
  2515. ' i: word;',
  2516. 'begin',
  2517. ' s:={$I %line%};',
  2518. ' i:={$I %linenum%};',
  2519. ' s:={$I %currentroutine%};',
  2520. ' s:={$I %pas2jsversion%};',
  2521. ' s:={$I %pas2jstarget%};',
  2522. ' s:={$I %pas2jstargetos%};',
  2523. ' s:={$I %pas2jstargetcpu%};',
  2524. ' s:={$I %file%};',
  2525. '']);
  2526. ConvertProgram;
  2527. CheckSource('TestIncludeVersion',
  2528. LinesToStr([
  2529. 'this.s="";',
  2530. 'this.i = 0;']),
  2531. LinesToStr([
  2532. '$mod.s = "7";',
  2533. '$mod.i = 8;',
  2534. '$mod.s = "<anonymous>";',
  2535. '$mod.s = "Comp.Ver.tcmodules";',
  2536. '$mod.s = "Browser";',
  2537. '$mod.s = "Browser";',
  2538. '$mod.s = "ECMAScript5";',
  2539. '$mod.s = "test1.pp";',
  2540. '']));
  2541. end;
  2542. procedure TTestModule.TestVarInt;
  2543. begin
  2544. StartProgram(false);
  2545. Add('var MyI: longint;');
  2546. Add('begin');
  2547. ConvertProgram;
  2548. CheckSource('TestVarInt','this.MyI=0;','');
  2549. end;
  2550. procedure TTestModule.TestVarBaseTypes;
  2551. begin
  2552. StartProgram(false);
  2553. Add('var');
  2554. Add(' i: longint;');
  2555. Add(' s: string;');
  2556. Add(' c: char;');
  2557. Add(' b: boolean;');
  2558. Add(' d: double;');
  2559. Add(' i2: longint = 3;');
  2560. Add(' s2: string = ''foo'';');
  2561. Add(' c2: char = ''4'';');
  2562. Add(' b2: boolean = true;');
  2563. Add(' d2: double = 5.6;');
  2564. Add(' i3: longint = $707;');
  2565. Add(' i4: nativeint = 9007199254740991;');
  2566. Add(' i5: nativeint = -9007199254740991-1;');
  2567. Add(' i6: nativeint = $fffffffffffff;');
  2568. Add(' i7: nativeint = -$fffffffffffff-1;');
  2569. Add(' i8: byte = 00;');
  2570. Add(' u8: nativeuint = $fffffffffffff;');
  2571. Add(' u9: nativeuint = $0000000000000;');
  2572. Add(' u10: nativeuint = $00ff00;');
  2573. Add('begin');
  2574. ConvertProgram;
  2575. CheckSource('TestVarBaseTypes',
  2576. LinesToStr([
  2577. 'this.i = 0;',
  2578. 'this.s = "";',
  2579. 'this.c = "";',
  2580. 'this.b = false;',
  2581. 'this.d = 0.0;',
  2582. 'this.i2 = 3;',
  2583. 'this.s2 = "foo";',
  2584. 'this.c2 = "4";',
  2585. 'this.b2 = true;',
  2586. 'this.d2 = 5.6;',
  2587. 'this.i3 = 0x707;',
  2588. 'this.i4 = 9007199254740991;',
  2589. 'this.i5 = -9007199254740991-1;',
  2590. 'this.i6 = 0xfffffffffffff;',
  2591. 'this.i7 =-0xfffffffffffff-1;',
  2592. 'this.i8 = 0;',
  2593. 'this.u8 = 0xfffffffffffff;',
  2594. 'this.u9 = 0x0;',
  2595. 'this.u10 = 0xff00;'
  2596. ]),
  2597. '');
  2598. end;
  2599. procedure TTestModule.TestBaseTypeSingleFail;
  2600. begin
  2601. StartProgram(false);
  2602. Add('var s: single;');
  2603. SetExpectedPasResolverError('identifier not found "single"',PasResolveEval.nIdentifierNotFound);
  2604. ConvertProgram;
  2605. end;
  2606. procedure TTestModule.TestBaseTypeExtendedFail;
  2607. begin
  2608. StartProgram(false);
  2609. Add('var e: extended;');
  2610. SetExpectedPasResolverError('identifier not found "extended"',PasResolveEval.nIdentifierNotFound);
  2611. ConvertProgram;
  2612. end;
  2613. procedure TTestModule.TestConstBaseTypes;
  2614. begin
  2615. StartProgram(false);
  2616. Add('const');
  2617. Add(' i: longint = 3;');
  2618. Add(' s: string = ''foo'';');
  2619. Add(' c: char = ''4'';');
  2620. Add(' b: boolean = true;');
  2621. Add(' d: double = 5.6;');
  2622. Add(' e = low(word);');
  2623. Add(' f = high(word);');
  2624. Add('begin');
  2625. ConvertProgram;
  2626. CheckSource('TestVarBaseTypes',
  2627. LinesToStr([
  2628. 'this.i=3;',
  2629. 'this.s="foo";',
  2630. 'this.c="4";',
  2631. 'this.b=true;',
  2632. 'this.d=5.6;',
  2633. 'this.e = 0;',
  2634. 'this.f = 65535;'
  2635. ]),
  2636. '');
  2637. end;
  2638. procedure TTestModule.TestAliasTypeRef;
  2639. begin
  2640. StartProgram(false);
  2641. Add('type');
  2642. Add(' a=longint;');
  2643. Add(' b=a;');
  2644. Add('var');
  2645. Add(' c: A;');
  2646. Add(' d: B;');
  2647. Add('begin');
  2648. ConvertProgram;
  2649. CheckSource('TestAliasTypeRef',
  2650. LinesToStr([ // statements
  2651. 'this.c = 0;',
  2652. 'this.d = 0;'
  2653. ]),
  2654. LinesToStr([ // this.$main
  2655. ''
  2656. ]));
  2657. end;
  2658. procedure TTestModule.TestTypeCast_BaseTypes;
  2659. begin
  2660. StartProgram(false);
  2661. Add([
  2662. 'var',
  2663. ' i: longint;',
  2664. ' b: boolean;',
  2665. ' d: double;',
  2666. ' s: string;',
  2667. ' c: char;',
  2668. 'begin',
  2669. ' i:=longint(i);',
  2670. ' i:=longint(b);',
  2671. ' b:=boolean(b);',
  2672. ' b:=boolean(i);',
  2673. ' d:=double(d);',
  2674. ' d:=double(i);',
  2675. ' s:=string(s);',
  2676. ' s:=string(c);',
  2677. ' c:=char(c);',
  2678. ' c:=char(i);',
  2679. ' c:=char(65);',
  2680. ' c:=char(#10);',
  2681. ' c:=char(#$E000);',
  2682. '']);
  2683. ConvertProgram;
  2684. CheckSource('TestAliasTypeRef',
  2685. LinesToStr([ // statements
  2686. 'this.i = 0;',
  2687. 'this.b = false;',
  2688. 'this.d = 0.0;',
  2689. 'this.s = "";',
  2690. 'this.c = "";',
  2691. '']),
  2692. LinesToStr([ // this.$main
  2693. '$mod.i = $mod.i;',
  2694. '$mod.i = ($mod.b ? 1 : 0);',
  2695. '$mod.b = $mod.b;',
  2696. '$mod.b = $mod.i != 0;',
  2697. '$mod.d = $mod.d;',
  2698. '$mod.d = $mod.i;',
  2699. '$mod.s = $mod.s;',
  2700. '$mod.s = $mod.c;',
  2701. '$mod.c = $mod.c;',
  2702. '$mod.c = String.fromCharCode($mod.i);',
  2703. '$mod.c = "A";',
  2704. '$mod.c = "\n";',
  2705. '$mod.c = "";',
  2706. '']));
  2707. end;
  2708. procedure TTestModule.TestTypeCast_AliasBaseTypes;
  2709. begin
  2710. StartProgram(false);
  2711. Add('type');
  2712. Add(' integer = longint;');
  2713. Add(' TYesNo = boolean;');
  2714. Add(' TFloat = double;');
  2715. Add(' TCaption = string;');
  2716. Add(' TChar = char;');
  2717. Add('var');
  2718. Add(' i: integer;');
  2719. Add(' b: TYesNo;');
  2720. Add(' d: TFloat;');
  2721. Add(' s: TCaption;');
  2722. Add(' c: TChar;');
  2723. Add('begin');
  2724. Add(' i:=integer(i);');
  2725. Add(' i:=integer(b);');
  2726. Add(' b:=TYesNo(b);');
  2727. Add(' b:=TYesNo(i);');
  2728. Add(' d:=TFloat(d);');
  2729. Add(' d:=TFloat(i);');
  2730. Add(' s:=TCaption(s);');
  2731. Add(' s:=TCaption(c);');
  2732. Add(' c:=TChar(c);');
  2733. ConvertProgram;
  2734. CheckSource('TestAliasTypeRef',
  2735. LinesToStr([ // statements
  2736. 'this.i = 0;',
  2737. 'this.b = false;',
  2738. 'this.d = 0.0;',
  2739. 'this.s = "";',
  2740. 'this.c = "";',
  2741. '']),
  2742. LinesToStr([ // this.$main
  2743. '$mod.i = $mod.i;',
  2744. '$mod.i = ($mod.b ? 1 : 0);',
  2745. '$mod.b = $mod.b;',
  2746. '$mod.b = $mod.i != 0;',
  2747. '$mod.d = $mod.d;',
  2748. '$mod.d = $mod.i;',
  2749. '$mod.s = $mod.s;',
  2750. '$mod.s = $mod.c;',
  2751. '$mod.c = $mod.c;',
  2752. '']));
  2753. end;
  2754. procedure TTestModule.TestEmptyProc;
  2755. begin
  2756. StartProgram(false);
  2757. Add('procedure Test;');
  2758. Add('begin');
  2759. Add('end;');
  2760. Add('begin');
  2761. ConvertProgram;
  2762. CheckSource('TestEmptyProc',
  2763. LinesToStr([ // statements
  2764. 'this.Test = function () {',
  2765. '};'
  2766. ]),
  2767. LinesToStr([ // this.$main
  2768. ''
  2769. ]));
  2770. end;
  2771. procedure TTestModule.TestProcOneParam;
  2772. begin
  2773. StartProgram(false);
  2774. Add('procedure ProcA(i: longint);');
  2775. Add('begin');
  2776. Add('end;');
  2777. Add('begin');
  2778. Add(' PROCA(3);');
  2779. ConvertProgram;
  2780. CheckSource('TestProcOneParam',
  2781. LinesToStr([ // statements
  2782. 'this.ProcA = function (i) {',
  2783. '};'
  2784. ]),
  2785. LinesToStr([ // this.$main
  2786. '$mod.ProcA(3);'
  2787. ]));
  2788. end;
  2789. procedure TTestModule.TestFunctionWithoutParams;
  2790. begin
  2791. StartProgram(false);
  2792. Add('function FuncA: longint;');
  2793. Add('begin');
  2794. Add('end;');
  2795. Add('var i: longint;');
  2796. Add('begin');
  2797. Add(' I:=FUNCA();');
  2798. Add(' I:=FUNCA;');
  2799. Add(' FUNCA();');
  2800. Add(' FUNCA;');
  2801. ConvertProgram;
  2802. CheckSource('TestProcWithoutParams',
  2803. LinesToStr([ // statements
  2804. 'this.FuncA = function () {',
  2805. ' var Result = 0;',
  2806. ' return Result;',
  2807. '};',
  2808. 'this.i=0;'
  2809. ]),
  2810. LinesToStr([ // this.$main
  2811. '$mod.i=$mod.FuncA();',
  2812. '$mod.i=$mod.FuncA();',
  2813. '$mod.FuncA();',
  2814. '$mod.FuncA();'
  2815. ]));
  2816. end;
  2817. procedure TTestModule.TestProcedureWithoutParams;
  2818. begin
  2819. StartProgram(false);
  2820. Add('procedure ProcA;');
  2821. Add('begin');
  2822. Add('end;');
  2823. Add('begin');
  2824. Add(' PROCA();');
  2825. Add(' PROCA;');
  2826. ConvertProgram;
  2827. CheckSource('TestProcWithoutParams',
  2828. LinesToStr([ // statements
  2829. 'this.ProcA = function () {',
  2830. '};'
  2831. ]),
  2832. LinesToStr([ // this.$main
  2833. '$mod.ProcA();',
  2834. '$mod.ProcA();'
  2835. ]));
  2836. end;
  2837. procedure TTestModule.TestIncDec;
  2838. begin
  2839. StartProgram(false);
  2840. Add([
  2841. 'procedure DoIt(var i: longint);',
  2842. 'begin',
  2843. ' inc(i);',
  2844. ' inc(i,2);',
  2845. 'end;',
  2846. 'var',
  2847. ' Bar: longint;',
  2848. 'begin',
  2849. ' inc(bar);',
  2850. ' inc(bar,2);',
  2851. ' dec(bar);',
  2852. ' dec(bar,3);',
  2853. '']);
  2854. ConvertProgram;
  2855. CheckSource('TestIncDec',
  2856. LinesToStr([ // statements
  2857. 'this.DoIt = function (i) {',
  2858. ' i.set(i.get()+1);',
  2859. ' i.set(i.get()+2);',
  2860. '};',
  2861. 'this.Bar = 0;'
  2862. ]),
  2863. LinesToStr([ // this.$main
  2864. '$mod.Bar+=1;',
  2865. '$mod.Bar+=2;',
  2866. '$mod.Bar-=1;',
  2867. '$mod.Bar-=3;'
  2868. ]));
  2869. end;
  2870. procedure TTestModule.TestLoHiFpcMode;
  2871. begin
  2872. StartProgram(false);
  2873. Add([
  2874. '{$mode objfpc}',
  2875. 'const',
  2876. ' LoByte1 = Lo(Word($1234));',
  2877. ' HiByte1 = Hi(Word($1234));',
  2878. ' LoByte2 = Lo(SmallInt($1234));',
  2879. ' HiByte2 = Hi(SmallInt($1234));',
  2880. ' LoWord1 = Lo($1234CDEF);',
  2881. ' HiWord1 = Hi($1234CDEF);',
  2882. ' LoWord2 = Lo(-$1234CDEF);',
  2883. ' HiWord2 = Hi(-$1234CDEF);',
  2884. ' lo4:byte=lo(byte($34));',
  2885. ' hi4:byte=hi(byte($34));',
  2886. ' lo5:byte=lo(shortint(-$34));',
  2887. ' hi5:byte=hi(shortint(-$34));',
  2888. ' lo6:longword=lo($123456789ABCD);',
  2889. ' hi6:longword=hi($123456789ABCD);',
  2890. ' lo7:longword=lo(-$123456789ABCD);',
  2891. ' hi7:longword=hi(-$123456789ABCD);',
  2892. 'var',
  2893. ' b: Byte;',
  2894. ' ss: shortint;',
  2895. ' w: Word;',
  2896. ' si: SmallInt;',
  2897. ' lw: LongWord;',
  2898. ' li: LongInt;',
  2899. ' b2: Byte;',
  2900. ' ni: nativeint;',
  2901. 'begin',
  2902. ' w := $1234;',
  2903. ' ss := -$12;',
  2904. ' b := lo(ss);',
  2905. ' b := HI(ss);',
  2906. ' b := lo(w);',
  2907. ' b := HI(w);',
  2908. ' b2 := lo(b);',
  2909. ' b2 := hi(b);',
  2910. ' lw := $1234CDEF;',
  2911. ' w := lo(lw);',
  2912. ' w := hi(lw);',
  2913. ' ni := $123456789ABCD;',
  2914. ' lw := lo(ni);',
  2915. ' lw := hi(ni);',
  2916. '']);
  2917. ConvertProgram;
  2918. CheckSource('TestLoHiFpcMode',
  2919. LinesToStr([ // statements
  2920. 'this.LoByte1 = 0x1234 & 0xFF;',
  2921. 'this.HiByte1 = (0x1234 >> 8) & 0xFF;',
  2922. 'this.LoByte2 = 0x1234 & 0xFF;',
  2923. 'this.HiByte2 = (0x1234 >> 8) & 0xFF;',
  2924. 'this.LoWord1 = 0x1234CDEF & 0xFFFF;',
  2925. 'this.HiWord1 = (0x1234CDEF >> 16) & 0xFFFF;',
  2926. 'this.LoWord2 = -0x1234CDEF & 0xFFFF;',
  2927. 'this.HiWord2 = (-0x1234CDEF >> 16) & 0xFFFF;',
  2928. 'this.lo4 = 0x34 & 0xF;',
  2929. 'this.hi4 = (0x34 >> 4) & 0xF;',
  2930. 'this.lo5 = (((-0x34 & 255) << 24) >> 24) & 0xFF;',
  2931. 'this.hi5 = ((((-0x34 & 255) << 24) >> 24) >> 8) & 0xFF;',
  2932. 'this.lo6 = 0x123456789ABCD >>> 0;',
  2933. 'this.hi6 = 74565 >>> 0;',
  2934. 'this.lo7 = -0x123456789ABCD >>> 0;',
  2935. 'this.hi7 = Math.floor(-0x123456789ABCD / 4294967296) >>> 0;',
  2936. 'this.b = 0;',
  2937. 'this.ss = 0;',
  2938. 'this.w = 0;',
  2939. 'this.si = 0;',
  2940. 'this.lw = 0;',
  2941. 'this.li = 0;',
  2942. 'this.b2 = 0;',
  2943. 'this.ni = 0;',
  2944. '']),
  2945. LinesToStr([ // this.$main
  2946. '$mod.w = 0x1234;',
  2947. '$mod.ss = -0x12;',
  2948. '$mod.b = $mod.ss & 0xFF;',
  2949. '$mod.b = ($mod.ss >> 8) & 0xFF;',
  2950. '$mod.b = $mod.w & 0xFF;',
  2951. '$mod.b = ($mod.w >> 8) & 0xFF;',
  2952. '$mod.b2 = $mod.b & 0xF;',
  2953. '$mod.b2 = ($mod.b >> 4) & 0xF;',
  2954. '$mod.lw = 0x1234CDEF;',
  2955. '$mod.w = $mod.lw & 0xFFFF;',
  2956. '$mod.w = ($mod.lw >> 16) & 0xFFFF;',
  2957. '$mod.ni = 0x123456789ABCD;',
  2958. '$mod.lw = $mod.ni >>> 0;',
  2959. '$mod.lw = Math.floor($mod.ni / 4294967296) >>> 0;',
  2960. '']));
  2961. end;
  2962. procedure TTestModule.TestLoHiDelphiMode;
  2963. begin
  2964. StartProgram(false);
  2965. Add([
  2966. '{$mode delphi}',
  2967. 'const',
  2968. ' LoByte1 = Lo(Word($1234));',
  2969. ' HiByte1 = Hi(Word($1234));',
  2970. ' LoByte2 = Lo(SmallInt($1234));',
  2971. ' HiByte2 = Hi(SmallInt($1234));',
  2972. ' LoByte3 = Lo($1234CDEF);',
  2973. ' HiByte3 = Hi($1234CDEF);',
  2974. ' LoByte4 = Lo(-$1234CDEF);',
  2975. ' HiByte4 = Hi(-$1234CDEF);',
  2976. 'var',
  2977. ' b: Byte;',
  2978. ' w: Word;',
  2979. ' si: SmallInt;',
  2980. ' lw: LongWord;',
  2981. ' li: LongInt;',
  2982. 'begin',
  2983. ' w := $1234;',
  2984. ' b := lo(w);',
  2985. ' b := HI(w);',
  2986. ' lw := $1234CDEF;',
  2987. ' b := lo(lw);',
  2988. ' b := hi(lw);',
  2989. '']);
  2990. ConvertProgram;
  2991. CheckSource('TestLoHiDelphiMode',
  2992. LinesToStr([ // statements
  2993. 'this.LoByte1 = 0x1234 & 0xFF;',
  2994. 'this.HiByte1 = (0x1234 >> 8) & 0xFF;',
  2995. 'this.LoByte2 = 0x1234 & 0xFF;',
  2996. 'this.HiByte2 = (0x1234 >> 8) & 0xFF;',
  2997. 'this.LoByte3 = 0x1234CDEF & 0xFF;',
  2998. 'this.HiByte3 = (0x1234CDEF >> 8) & 0xFF;',
  2999. 'this.LoByte4 = -0x1234CDEF & 0xFF;',
  3000. 'this.HiByte4 = (-0x1234CDEF >> 8) & 0xFF;',
  3001. 'this.b = 0;',
  3002. 'this.w = 0;',
  3003. 'this.si = 0;',
  3004. 'this.lw = 0;',
  3005. 'this.li = 0;'
  3006. ]),
  3007. LinesToStr([ // this.$main
  3008. '$mod.w = 0x1234;',
  3009. '$mod.b = $mod.w & 0xFF;',
  3010. '$mod.b = ($mod.w >> 8) & 0xFF;',
  3011. '$mod.lw = 0x1234CDEF;',
  3012. '$mod.b = $mod.lw & 0xFF;',
  3013. '$mod.b = ($mod.lw >> 8) & 0xFF;'
  3014. ]));
  3015. end;
  3016. procedure TTestModule.TestAssignments;
  3017. begin
  3018. StartProgram(false);
  3019. Parser.Options:=Parser.Options+[po_cassignments];
  3020. Add('var');
  3021. Add(' Bar:longint;');
  3022. Add('begin');
  3023. Add(' bar:=3;');
  3024. Add(' bar+=4;');
  3025. Add(' bar-=5;');
  3026. Add(' bar*=6;');
  3027. ConvertProgram;
  3028. CheckSource('TestAssignments',
  3029. LinesToStr([ // statements
  3030. 'this.Bar = 0;'
  3031. ]),
  3032. LinesToStr([ // this.$main
  3033. '$mod.Bar=3;',
  3034. '$mod.Bar+=4;',
  3035. '$mod.Bar-=5;',
  3036. '$mod.Bar*=6;'
  3037. ]));
  3038. end;
  3039. procedure TTestModule.TestArithmeticOperators1;
  3040. begin
  3041. StartProgram(false);
  3042. Add('var');
  3043. Add(' vA,vB,vC:longint;');
  3044. Add('begin');
  3045. Add(' va:=1;');
  3046. Add(' vb:=va+va;');
  3047. Add(' vb:=va div vb;');
  3048. Add(' vb:=va mod vb;');
  3049. Add(' vb:=va+va*vb+va div vb;');
  3050. Add(' vc:=-va;');
  3051. Add(' va:=va-vb;');
  3052. Add(' vb:=va;');
  3053. Add(' if va<vb then vc:=va else vc:=vb;');
  3054. ConvertProgram;
  3055. CheckSource('TestArithmeticOperators1',
  3056. LinesToStr([ // statements
  3057. 'this.vA = 0;',
  3058. 'this.vB = 0;',
  3059. 'this.vC = 0;'
  3060. ]),
  3061. LinesToStr([ // this.$main
  3062. '$mod.vA = 1;',
  3063. '$mod.vB = $mod.vA + $mod.vA;',
  3064. '$mod.vB = rtl.trunc($mod.vA / $mod.vB);',
  3065. '$mod.vB = $mod.vA % $mod.vB;',
  3066. '$mod.vB = $mod.vA + ($mod.vA * $mod.vB) + rtl.trunc($mod.vA / $mod.vB);',
  3067. '$mod.vC = -$mod.vA;',
  3068. '$mod.vA = $mod.vA - $mod.vB;',
  3069. '$mod.vB = $mod.vA;',
  3070. 'if ($mod.vA < $mod.vB){ $mod.vC = $mod.vA } else $mod.vC = $mod.vB;'
  3071. ]));
  3072. end;
  3073. procedure TTestModule.TestMultiAdd;
  3074. begin
  3075. StartProgram(false);
  3076. Add([
  3077. 'function TryEncodeDate(Year, Month, Day: Word): Boolean;',
  3078. 'var Date: double;',
  3079. 'begin',
  3080. ' Result:=(Year>0) and (Year<10000) and',
  3081. ' (Month >= 1) and (Month<=12) and',
  3082. ' (Day>0) and (Day<=31);',
  3083. ' Date := (146097*Year) SHR 2 + (1461*Year) SHR 2 + (153*LongWord(Month)+2) DIV 5 + LongWord(Day);',
  3084. 'end;',
  3085. 'var s: string;',
  3086. 'begin',
  3087. ' s:=''a''+''b''+''c''+''d'';']);
  3088. ConvertProgram;
  3089. CheckSource('TestMultiAdd',
  3090. LinesToStr([ // statements
  3091. 'this.TryEncodeDate = function (Year, Month, Day) {',
  3092. ' var Result = false;',
  3093. ' var date = 0.0;',
  3094. ' Result = (Year > 0) && (Year < 10000) && (Month >= 1) && (Month <= 12) && (Day > 0) && (Day <= 31);',
  3095. ' date = ((146097 * Year) >>> 2) + ((1461 * Year) >>> 2) + rtl.trunc(((153 * Month) + 2) / 5) + Day;',
  3096. ' return Result;',
  3097. '};',
  3098. 'this.s = "";',
  3099. '']),
  3100. LinesToStr([ // this.$main
  3101. '$mod.s = "a" + "b" + "c" + "d";',
  3102. '']));
  3103. end;
  3104. procedure TTestModule.TestLogicalOperators;
  3105. begin
  3106. StartProgram(false);
  3107. Add('var');
  3108. Add(' vA,vB,vC:boolean;');
  3109. Add('begin');
  3110. Add(' va:=vb and vc;');
  3111. Add(' va:=vb or vc;');
  3112. Add(' va:=vb xor vc;');
  3113. Add(' va:=true and vc;');
  3114. Add(' va:=(vb and vc) or (va and vb);');
  3115. Add(' va:=not vb;');
  3116. ConvertProgram;
  3117. CheckSource('TestLogicalOperators',
  3118. LinesToStr([ // statements
  3119. 'this.vA = false;',
  3120. 'this.vB = false;',
  3121. 'this.vC = false;'
  3122. ]),
  3123. LinesToStr([ // this.$main
  3124. '$mod.vA = $mod.vB && $mod.vC;',
  3125. '$mod.vA = $mod.vB || $mod.vC;',
  3126. '$mod.vA = $mod.vB ^ $mod.vC;',
  3127. '$mod.vA = true && $mod.vC;',
  3128. '$mod.vA = ($mod.vB && $mod.vC) || ($mod.vA && $mod.vB);',
  3129. '$mod.vA = !$mod.vB;'
  3130. ]));
  3131. end;
  3132. procedure TTestModule.TestBitwiseOperators;
  3133. begin
  3134. StartProgram(false);
  3135. Add([
  3136. 'var',
  3137. ' vA,vB,vC:longint;',
  3138. ' X,Y,Z: nativeint;',
  3139. 'begin',
  3140. ' va:=vb and vc;',
  3141. ' va:=vb or vc;',
  3142. ' va:=vb xor vc;',
  3143. ' va:=vb shl vc;',
  3144. ' va:=vb shr vc;',
  3145. ' va:=3 and vc;',
  3146. ' va:=(vb and vc) or (va and vb);',
  3147. ' va:=not vb;',
  3148. ' X:=Y and Z;',
  3149. ' X:=Y and va;',
  3150. ' X:=Y or Z;',
  3151. ' X:=Y or va;',
  3152. ' X:=Y xor Z;',
  3153. ' X:=Y xor va;',
  3154. '']);
  3155. ConvertProgram;
  3156. CheckSource('TestBitwiseOperators',
  3157. LinesToStr([ // statements
  3158. 'this.vA = 0;',
  3159. 'this.vB = 0;',
  3160. 'this.vC = 0;',
  3161. 'this.X = 0;',
  3162. 'this.Y = 0;',
  3163. 'this.Z = 0;',
  3164. '']),
  3165. LinesToStr([ // this.$main
  3166. '$mod.vA = $mod.vB & $mod.vC;',
  3167. '$mod.vA = $mod.vB | $mod.vC;',
  3168. '$mod.vA = $mod.vB ^ $mod.vC;',
  3169. '$mod.vA = $mod.vB << $mod.vC;',
  3170. '$mod.vA = $mod.vB >>> $mod.vC;',
  3171. '$mod.vA = 3 & $mod.vC;',
  3172. '$mod.vA = ($mod.vB & $mod.vC) | ($mod.vA & $mod.vB);',
  3173. '$mod.vA = ~$mod.vB;',
  3174. '$mod.X = rtl.and($mod.Y, $mod.Z);',
  3175. '$mod.X = $mod.Y & $mod.vA;',
  3176. '$mod.X = rtl.or($mod.Y, $mod.Z);',
  3177. '$mod.X = rtl.or($mod.Y, $mod.vA);',
  3178. '$mod.X = rtl.xor($mod.Y, $mod.Z);',
  3179. '$mod.X = rtl.xor($mod.Y, $mod.vA);',
  3180. '']));
  3181. end;
  3182. procedure TTestModule.TestBitwiseOperatorsLongword;
  3183. begin
  3184. StartProgram(false);
  3185. Add([
  3186. 'var',
  3187. ' a,b,c:longword;',
  3188. ' i: longint;',
  3189. 'begin',
  3190. ' a:=$12345678;',
  3191. ' b:=$EDCBA987;',
  3192. ' c:=not a;',
  3193. ' c:=a and b;',
  3194. ' c:=a and $ffff0000;',
  3195. ' c:=a or b;',
  3196. ' c:=a or $ff00ff00;',
  3197. ' c:=a xor b;',
  3198. ' c:=a xor $f0f0f0f0;',
  3199. ' c:=a shl 1;',
  3200. ' c:=a shl 16;',
  3201. ' c:=a shl 24;',
  3202. ' c:=a shl b;',
  3203. ' c:=a shr 1;',
  3204. ' c:=a shr 16;',
  3205. ' c:=a shr 24;',
  3206. ' c:=a shr b;',
  3207. ' c:=(b and c) or (a and b);',
  3208. ' c:=i and a;',
  3209. ' c:=i or a;',
  3210. ' c:=i xor a;',
  3211. '']);
  3212. ConvertProgram;
  3213. CheckSource('TestBitwiseOperatorsLongword',
  3214. LinesToStr([ // statements
  3215. 'this.a = 0;',
  3216. 'this.b = 0;',
  3217. 'this.c = 0;',
  3218. 'this.i = 0;',
  3219. '']),
  3220. LinesToStr([ // this.$main
  3221. '$mod.a = 0x12345678;',
  3222. '$mod.b = 0xEDCBA987;',
  3223. '$mod.c = rtl.lw(~$mod.a);',
  3224. '$mod.c = rtl.lw($mod.a & $mod.b);',
  3225. '$mod.c = rtl.lw($mod.a & 0xffff0000);',
  3226. '$mod.c = rtl.lw($mod.a | $mod.b);',
  3227. '$mod.c = rtl.lw($mod.a | 0xff00ff00);',
  3228. '$mod.c = rtl.lw($mod.a ^ $mod.b);',
  3229. '$mod.c = rtl.lw($mod.a ^ 0xf0f0f0f0);',
  3230. '$mod.c = rtl.lw($mod.a << 1);',
  3231. '$mod.c = rtl.lw($mod.a << 16);',
  3232. '$mod.c = rtl.lw($mod.a << 24);',
  3233. '$mod.c = rtl.lw($mod.a << $mod.b);',
  3234. '$mod.c = rtl.lw($mod.a >>> 1);',
  3235. '$mod.c = rtl.lw($mod.a >>> 16);',
  3236. '$mod.c = rtl.lw($mod.a >>> 24);',
  3237. '$mod.c = rtl.lw($mod.a >>> $mod.b);',
  3238. '$mod.c = rtl.lw(rtl.lw($mod.b & $mod.c) | rtl.lw($mod.a & $mod.b));',
  3239. '$mod.c = $mod.i & $mod.a;',
  3240. '$mod.c = $mod.i | $mod.a;',
  3241. '$mod.c = $mod.i ^ $mod.a;',
  3242. '']));
  3243. end;
  3244. procedure TTestModule.TestPrgProcVar;
  3245. begin
  3246. StartProgram(false);
  3247. Add('procedure Proc1;');
  3248. Add('type');
  3249. Add(' t1=longint;');
  3250. Add('var');
  3251. Add(' vA:t1;');
  3252. Add('begin');
  3253. Add('end;');
  3254. Add('begin');
  3255. ConvertProgram;
  3256. CheckSource('TestPrgProcVar',
  3257. LinesToStr([ // statements
  3258. 'this.Proc1 = function () {',
  3259. ' var vA=0;',
  3260. '};'
  3261. ]),
  3262. LinesToStr([ // this.$main
  3263. ''
  3264. ]));
  3265. end;
  3266. procedure TTestModule.TestUnitProcVar;
  3267. begin
  3268. StartUnit(false);
  3269. Add('interface');
  3270. Add('');
  3271. Add('type tA=string; // unit scope');
  3272. Add('procedure Proc1;');
  3273. Add('');
  3274. Add('implementation');
  3275. Add('');
  3276. Add('procedure Proc1;');
  3277. Add('type tA=longint; // local proc scope');
  3278. Add('var v1:tA; // using local tA');
  3279. Add('begin');
  3280. Add('end;');
  3281. Add('var v2:tA; // using interface tA');
  3282. ConvertUnit;
  3283. CheckSource('TestUnitProcVar',
  3284. LinesToStr([ // statements
  3285. 'var $impl = $mod.$impl;',
  3286. 'this.Proc1 = function () {',
  3287. ' var v1 = 0;',
  3288. '};',
  3289. '']),
  3290. // this.$init
  3291. '',
  3292. // implementation
  3293. LinesToStr([
  3294. '$impl.v2 = "";',
  3295. '']));
  3296. end;
  3297. procedure TTestModule.TestImplProc;
  3298. begin
  3299. StartUnit(false);
  3300. Add('interface');
  3301. Add('');
  3302. Add('procedure Proc1;');
  3303. Add('');
  3304. Add('implementation');
  3305. Add('');
  3306. Add('procedure Proc1; begin end;');
  3307. Add('procedure Proc2; begin end;');
  3308. Add('initialization');
  3309. Add(' Proc1;');
  3310. Add(' Proc2;');
  3311. ConvertUnit;
  3312. CheckSource('TestImplProc',
  3313. LinesToStr([ // statements
  3314. 'var $impl = $mod.$impl;',
  3315. 'this.Proc1 = function () {',
  3316. '};',
  3317. '']),
  3318. LinesToStr([ // this.$init
  3319. '$mod.Proc1();',
  3320. '$impl.Proc2();',
  3321. '']),
  3322. LinesToStr([ // implementation
  3323. '$impl.Proc2 = function () {',
  3324. '};',
  3325. ''])
  3326. );
  3327. end;
  3328. procedure TTestModule.TestFunctionResult;
  3329. begin
  3330. StartProgram(false);
  3331. Add('function Func1: longint;');
  3332. Add('begin');
  3333. Add(' Result:=3;');
  3334. Add(' Func1:=4;');
  3335. Add('end;');
  3336. Add('begin');
  3337. ConvertProgram;
  3338. CheckSource('TestFunctionResult',
  3339. LinesToStr([ // statements
  3340. 'this.Func1 = function () {',
  3341. ' var Result = 0;',
  3342. ' Result = 3;',
  3343. ' Result = 4;',
  3344. ' return Result;',
  3345. '};'
  3346. ]),
  3347. '');
  3348. end;
  3349. procedure TTestModule.TestNestedProc;
  3350. begin
  3351. StartProgram(false);
  3352. Add([
  3353. 'var vInUnit: longint;',
  3354. 'function DoIt(pA,pD: longint): longint;',
  3355. 'var',
  3356. ' vB: longint;',
  3357. ' vC: longint;',
  3358. ' function Nesty(pA: longint): longint; ',
  3359. ' var vB: longint;',
  3360. ' begin',
  3361. ' Result:=pa+vb+vc+pd+vInUnit;',
  3362. ' nesty:=3;',
  3363. ' doit:=4;',
  3364. ' exit;',
  3365. ' end;',
  3366. 'begin',
  3367. ' Result:=pa+vb+vc;',
  3368. ' doit:=6;',
  3369. ' exit;',
  3370. 'end;',
  3371. 'begin']);
  3372. ConvertProgram;
  3373. CheckSource('TestNestedProc',
  3374. LinesToStr([ // statements
  3375. 'this.vInUnit = 0;',
  3376. 'this.DoIt = function (pA, pD) {',
  3377. ' var Result = 0;',
  3378. ' var vB = 0;',
  3379. ' var vC = 0;',
  3380. ' function Nesty(pA) {',
  3381. ' var Result$1 = 0;',
  3382. ' var vB = 0;',
  3383. ' Result$1 = pA + vB + vC + pD + $mod.vInUnit;',
  3384. ' Result$1 = 3;',
  3385. ' Result = 4;',
  3386. ' return Result$1;',
  3387. ' return Result$1;',
  3388. ' };',
  3389. ' Result = pA + vB + vC;',
  3390. ' Result = 6;',
  3391. ' return Result;',
  3392. ' return Result;',
  3393. '};'
  3394. ]),
  3395. '');
  3396. end;
  3397. procedure TTestModule.TestNestedProc_ResultString;
  3398. begin
  3399. StartProgram(false);
  3400. Add([
  3401. 'function DoIt: string;',
  3402. ' function Nesty: string; ',
  3403. ' begin',
  3404. ' nesty:=#65#66;',
  3405. ' nesty[1]:=#67;',
  3406. ' doit:=#68;',
  3407. ' doit[2]:=#69;',
  3408. ' end;',
  3409. 'begin',
  3410. ' doit:=#70;',
  3411. ' doit[3]:=#71;',
  3412. 'end;',
  3413. 'begin']);
  3414. ConvertProgram;
  3415. CheckSource('TestNestedProc_ResultString',
  3416. LinesToStr([ // statements
  3417. 'this.DoIt = function () {',
  3418. ' var Result = "";',
  3419. ' function Nesty() {',
  3420. ' var Result$1 = "";',
  3421. ' Result$1 = "AB";',
  3422. ' Result$1 = rtl.setCharAt(Result$1, 0, "C");',
  3423. ' Result = "D";',
  3424. ' Result = rtl.setCharAt(Result, 1, "E");',
  3425. ' return Result$1;',
  3426. ' };',
  3427. ' Result = "F";',
  3428. ' Result = rtl.setCharAt(Result, 2, "G");',
  3429. ' return Result;',
  3430. '};'
  3431. ]),
  3432. '');
  3433. end;
  3434. procedure TTestModule.TestForwardProc;
  3435. begin
  3436. StartProgram(false);
  3437. Add('procedure FuncA(Bar: longint); forward;');
  3438. Add('procedure FuncB(Bar: longint);');
  3439. Add('begin');
  3440. Add(' funca(bar);');
  3441. Add('end;');
  3442. Add('procedure funca(bar: longint);');
  3443. Add('begin');
  3444. Add(' if bar=3 then ;');
  3445. Add('end;');
  3446. Add('begin');
  3447. Add(' funca(4);');
  3448. Add(' funcb(5);');
  3449. ConvertProgram;
  3450. CheckSource('TestForwardProc',
  3451. LinesToStr([ // statements'
  3452. 'this.FuncB = function (Bar) {',
  3453. ' $mod.FuncA(Bar);',
  3454. '};',
  3455. 'this.FuncA = function (Bar) {',
  3456. ' if (Bar === 3);',
  3457. '};'
  3458. ]),
  3459. LinesToStr([
  3460. '$mod.FuncA(4);',
  3461. '$mod.FuncB(5);'
  3462. ])
  3463. );
  3464. end;
  3465. procedure TTestModule.TestNestedForwardProc;
  3466. begin
  3467. StartProgram(false);
  3468. Add('procedure FuncA;');
  3469. Add(' procedure FuncB(i: longint); forward;');
  3470. Add(' procedure FuncC(i: longint);');
  3471. Add(' begin');
  3472. Add(' funcb(i);');
  3473. Add(' end;');
  3474. Add(' procedure FuncB(i: longint);');
  3475. Add(' begin');
  3476. Add(' if i=3 then ;');
  3477. Add(' end;');
  3478. Add('begin');
  3479. Add(' funcc(4)');
  3480. Add('end;');
  3481. Add('begin');
  3482. Add(' funca;');
  3483. ConvertProgram;
  3484. CheckSource('TestNestedForwardProc',
  3485. LinesToStr([ // statements'
  3486. 'this.FuncA = function () {',
  3487. ' function FuncC(i) {',
  3488. ' FuncB(i);',
  3489. ' };',
  3490. ' function FuncB(i) {',
  3491. ' if (i === 3);',
  3492. ' };',
  3493. ' FuncC(4);',
  3494. '};'
  3495. ]),
  3496. LinesToStr([
  3497. '$mod.FuncA();'
  3498. ])
  3499. );
  3500. end;
  3501. procedure TTestModule.TestAssignFunctionResult;
  3502. begin
  3503. StartProgram(false);
  3504. Add('function Func1: longint;');
  3505. Add('begin');
  3506. Add('end;');
  3507. Add('var i: longint;');
  3508. Add('begin');
  3509. Add(' i:=func1();');
  3510. Add(' i:=func1()+func1();');
  3511. ConvertProgram;
  3512. CheckSource('TestAssignFunctionResult',
  3513. LinesToStr([ // statements
  3514. 'this.Func1 = function () {',
  3515. ' var Result = 0;',
  3516. ' return Result;',
  3517. '};',
  3518. 'this.i = 0;'
  3519. ]),
  3520. LinesToStr([
  3521. '$mod.i = $mod.Func1();',
  3522. '$mod.i = $mod.Func1() + $mod.Func1();'
  3523. ]));
  3524. end;
  3525. procedure TTestModule.TestFunctionResultInCondition;
  3526. begin
  3527. StartProgram(false);
  3528. Add('function Func1: longint;');
  3529. Add('begin');
  3530. Add('end;');
  3531. Add('function Func2: boolean;');
  3532. Add('begin');
  3533. Add('end;');
  3534. Add('var i: longint;');
  3535. Add('begin');
  3536. Add(' if func2 then ;');
  3537. Add(' if i=func1() then ;');
  3538. Add(' if i=func1 then ;');
  3539. ConvertProgram;
  3540. CheckSource('TestFunctionResultInCondition',
  3541. LinesToStr([ // statements
  3542. 'this.Func1 = function () {',
  3543. ' var Result = 0;',
  3544. ' return Result;',
  3545. '};',
  3546. 'this.Func2 = function () {',
  3547. ' var Result = false;',
  3548. ' return Result;',
  3549. '};',
  3550. 'this.i = 0;'
  3551. ]),
  3552. LinesToStr([
  3553. 'if ($mod.Func2());',
  3554. 'if ($mod.i === $mod.Func1());',
  3555. 'if ($mod.i === $mod.Func1());'
  3556. ]));
  3557. end;
  3558. procedure TTestModule.TestFunctionResultInForLoop;
  3559. begin
  3560. StartProgram(false);
  3561. Add([
  3562. 'function Func1(a: array of longint): longint;',
  3563. 'begin',
  3564. ' for Result:=High(a) downto Low(a) do if a[Result]=0 then exit;',
  3565. ' for Result in a do if a[Result]=0 then exit;',
  3566. 'end;',
  3567. 'begin',
  3568. ' Func1([1,2,3])']);
  3569. ConvertProgram;
  3570. CheckSource('TestFunctionResultInForLoop',
  3571. LinesToStr([ // statements
  3572. 'this.Func1 = function (a) {',
  3573. ' var Result = 0;',
  3574. ' for (var $l = rtl.length(a) - 1; $l >= 0; $l--) {',
  3575. ' Result = $l;',
  3576. ' if (a[Result] === 0) return Result;',
  3577. ' };',
  3578. ' for (var $in = a, $l1 = 0, $end = rtl.length($in) - 1; $l1 <= $end; $l1++) {',
  3579. ' Result = $in[$l1];',
  3580. ' if (a[Result] === 0) return Result;',
  3581. ' };',
  3582. ' return Result;',
  3583. '};',
  3584. '']),
  3585. LinesToStr([
  3586. '$mod.Func1([1, 2, 3]);'
  3587. ]));
  3588. end;
  3589. procedure TTestModule.TestFunctionResultInTypeCast;
  3590. begin
  3591. StartProgram(false);
  3592. Add([
  3593. 'function GetInt: longint;',
  3594. 'begin',
  3595. 'end;',
  3596. 'begin',
  3597. ' if Byte(GetInt)=0 then ;',
  3598. '']);
  3599. ConvertProgram;
  3600. CheckSource('TestFunctionResultInTypeCast',
  3601. LinesToStr([ // statements
  3602. 'this.GetInt = function () {',
  3603. ' var Result = 0;',
  3604. ' return Result;',
  3605. '};',
  3606. '']),
  3607. LinesToStr([
  3608. 'if (($mod.GetInt() & 255) === 0) ;'
  3609. ]));
  3610. end;
  3611. procedure TTestModule.TestExit;
  3612. begin
  3613. StartProgram(false);
  3614. Add('procedure ProcA;');
  3615. Add('begin');
  3616. Add(' exit;');
  3617. Add('end;');
  3618. Add('function FuncB: longint;');
  3619. Add('begin');
  3620. Add(' exit;');
  3621. Add(' exit(3);');
  3622. Add('end;');
  3623. Add('function FuncC: string;');
  3624. Add('begin');
  3625. Add(' exit;');
  3626. Add(' exit(''a'');');
  3627. Add(' exit(''abc'');');
  3628. Add('end;');
  3629. Add('begin');
  3630. Add(' exit;');
  3631. Add(' exit(1);');
  3632. ConvertProgram;
  3633. CheckSource('TestExit',
  3634. LinesToStr([ // statements
  3635. 'this.ProcA = function () {',
  3636. ' return;',
  3637. '};',
  3638. 'this.FuncB = function () {',
  3639. ' var Result = 0;',
  3640. ' return Result;',
  3641. ' return 3;',
  3642. ' return Result;',
  3643. '};',
  3644. 'this.FuncC = function () {',
  3645. ' var Result = "";',
  3646. ' return Result;',
  3647. ' return "a";',
  3648. ' return "abc";',
  3649. ' return Result;',
  3650. '};'
  3651. ]),
  3652. LinesToStr([
  3653. 'return;',
  3654. 'return 1;',
  3655. '']));
  3656. end;
  3657. procedure TTestModule.TestExit_ResultInFinally;
  3658. begin
  3659. StartProgram(false);
  3660. Add([
  3661. 'function Run: word;',
  3662. 'begin',
  3663. ' try',
  3664. ' exit(3);', // no Result in finally -> use return 3
  3665. ' finally',
  3666. ' end;',
  3667. 'end;',
  3668. 'function Fly: word;',
  3669. 'begin',
  3670. ' try',
  3671. ' exit(3);',
  3672. ' finally',
  3673. ' if Result>0 then ;',
  3674. ' end;',
  3675. 'end;',
  3676. 'function Jump: word;',
  3677. 'begin',
  3678. ' try',
  3679. ' try',
  3680. ' exit(4);',
  3681. ' finally',
  3682. ' end;',
  3683. ' finally',
  3684. ' if Result>0 then ;',
  3685. ' end;',
  3686. 'end;',
  3687. 'begin',
  3688. '']);
  3689. ConvertProgram;
  3690. CheckSource('TestExit_ResultInFinally',
  3691. LinesToStr([ // statements
  3692. 'this.Run = function () {',
  3693. ' var Result = 0;',
  3694. ' try {',
  3695. ' return 3;',
  3696. ' } finally {',
  3697. ' };',
  3698. ' return Result;',
  3699. '};',
  3700. 'this.Fly = function () {',
  3701. ' var Result = 0;',
  3702. ' try {',
  3703. ' Result = 3;',
  3704. ' return Result;',
  3705. ' } finally {',
  3706. ' if (Result > 0) ;',
  3707. ' };',
  3708. ' return Result;',
  3709. '};',
  3710. 'this.Jump = function () {',
  3711. ' var Result = 0;',
  3712. ' try {',
  3713. ' try {',
  3714. ' Result = 4;',
  3715. ' return Result;',
  3716. ' } finally {',
  3717. ' };',
  3718. ' } finally {',
  3719. ' if (Result > 0) ;',
  3720. ' };',
  3721. ' return Result;',
  3722. '};',
  3723. '']),
  3724. LinesToStr([
  3725. '']));
  3726. end;
  3727. procedure TTestModule.TestBreak;
  3728. begin
  3729. StartProgram(false);
  3730. Add([
  3731. 'var',
  3732. ' i: longint;',
  3733. 'begin',
  3734. ' repeat',
  3735. ' break;',
  3736. ' until true;',
  3737. ' while true do',
  3738. ' break;',
  3739. ' for i:=1 to 2 do',
  3740. ' break;']);
  3741. ConvertProgram;
  3742. CheckSource('TestBreak',
  3743. LinesToStr([ // statements
  3744. 'this.i = 0;'
  3745. ]),
  3746. LinesToStr([
  3747. 'do {',
  3748. ' break;',
  3749. '} while (!true);',
  3750. 'while (true) break;',
  3751. 'for ($mod.i = 1; $mod.i <= 2; $mod.i++) break;',
  3752. '']));
  3753. end;
  3754. procedure TTestModule.TestBreakAsVar;
  3755. begin
  3756. StartProgram(false);
  3757. Add([
  3758. 'procedure DoIt(break: boolean);',
  3759. 'begin',
  3760. ' if break then ;',
  3761. 'end;',
  3762. 'var',
  3763. ' break: boolean;',
  3764. 'begin',
  3765. ' if break then ;']);
  3766. ConvertProgram;
  3767. CheckSource('TestBreakAsVar',
  3768. LinesToStr([ // statements
  3769. 'this.DoIt = function (Break) {',
  3770. ' if (Break) ;',
  3771. '};',
  3772. 'this.Break = false;',
  3773. '']),
  3774. LinesToStr([
  3775. 'if($mod.Break) ;',
  3776. '']));
  3777. end;
  3778. procedure TTestModule.TestContinue;
  3779. begin
  3780. StartProgram(false);
  3781. Add('var i: longint;');
  3782. Add('begin');
  3783. Add(' repeat');
  3784. Add(' continue;');
  3785. Add(' until true;');
  3786. Add(' while true do');
  3787. Add(' continue;');
  3788. Add(' for i:=1 to 2 do');
  3789. Add(' continue;');
  3790. ConvertProgram;
  3791. CheckSource('TestContinue',
  3792. LinesToStr([ // statements
  3793. 'this.i = 0;'
  3794. ]),
  3795. LinesToStr([
  3796. 'do {',
  3797. ' continue;',
  3798. '} while (!true);',
  3799. 'while (true) continue;',
  3800. 'for ($mod.i = 1; $mod.i <= 2; $mod.i++) continue;',
  3801. '']));
  3802. end;
  3803. procedure TTestModule.TestProc_External;
  3804. begin
  3805. StartProgram(false);
  3806. Add('procedure Foo; external name ''console.log'';');
  3807. Add('function Bar: longint; external name ''get.item'';');
  3808. Add('function Bla(s: string): longint; external name ''apply.something'';');
  3809. Add('var');
  3810. Add(' i: longint;');
  3811. Add('begin');
  3812. Add(' Foo;');
  3813. Add(' i:=Bar;');
  3814. Add(' i:=Bla(''abc'');');
  3815. ConvertProgram;
  3816. CheckSource('TestProc_External',
  3817. LinesToStr([ // statements
  3818. 'this.i = 0;'
  3819. ]),
  3820. LinesToStr([
  3821. 'console.log();',
  3822. '$mod.i = get.item();',
  3823. '$mod.i = apply.something("abc");'
  3824. ]));
  3825. end;
  3826. procedure TTestModule.TestProc_ExternalOtherUnit;
  3827. begin
  3828. AddModuleWithIntfImplSrc('unit2.pas',
  3829. LinesToStr([
  3830. 'procedure Now; external name ''Date.now'';',
  3831. 'procedure DoIt;'
  3832. ]),
  3833. 'procedure doit; begin end;');
  3834. StartUnit(true);
  3835. Add('interface');
  3836. Add('uses unit2;');
  3837. Add('implementation');
  3838. Add('begin');
  3839. Add(' now;');
  3840. Add(' now();');
  3841. Add(' uNit2.now;');
  3842. Add(' uNit2.now();');
  3843. Add(' doit;');
  3844. Add(' uNit2.doit;');
  3845. ConvertUnit;
  3846. CheckSource('TestProc_ExternalOtherUnit',
  3847. LinesToStr([
  3848. '']),
  3849. LinesToStr([
  3850. 'Date.now();',
  3851. 'Date.now();',
  3852. 'Date.now();',
  3853. 'Date.now();',
  3854. 'pas.unit2.DoIt();',
  3855. 'pas.unit2.DoIt();',
  3856. '']));
  3857. end;
  3858. procedure TTestModule.TestProc_Asm;
  3859. begin
  3860. StartProgram(false);
  3861. Add([
  3862. '{$mode delphi}',
  3863. 'function DoIt: longint;',
  3864. 'begin;',
  3865. ' asm',
  3866. ' { a:{ b:{}, c:[]}, d:''1'' };',
  3867. ' end;',
  3868. ' asm console.log(); end;',
  3869. ' asm',
  3870. ' s = "'' ";',
  3871. ' s = ''" '';',
  3872. ' s = s + "world" + "''";',
  3873. ' // end',
  3874. ' s = ''end'';',
  3875. ' s = "end";',
  3876. ' s = "foo\"bar";',
  3877. ' s = ''a\''b'';',
  3878. ' s = `${expr}\`-"-''-`;',
  3879. ' s = `multi',
  3880. 'line`;',
  3881. ' end;',
  3882. 'end;',
  3883. 'procedure Fly;',
  3884. 'asm',
  3885. ' return;',
  3886. 'end;',
  3887. 'begin']);
  3888. ConvertProgram;
  3889. CheckSource('TestProc_Asm',
  3890. LinesToStr([ // statements
  3891. 'this.DoIt = function () {',
  3892. ' var Result = 0;',
  3893. ' { a:{ b:{}, c:[]}, d:''1'' };',
  3894. ' console.log();',
  3895. ' s = "'' ";',
  3896. ' s = ''" '';',
  3897. ' s = s + "world" + "''";',
  3898. ' // end',
  3899. ' s = ''end'';',
  3900. ' s = "end";',
  3901. ' s = "foo\"bar";',
  3902. ' s = ''a\''b'';',
  3903. ' s = `${expr}\`-"-''-`;',
  3904. ' s = `multi',
  3905. 'line`;',
  3906. ' return Result;',
  3907. '};',
  3908. 'this.Fly = function () {',
  3909. ' return;',
  3910. '};',
  3911. '']),
  3912. LinesToStr([
  3913. ''
  3914. ]));
  3915. end;
  3916. procedure TTestModule.TestProc_AsmSubBlock;
  3917. begin
  3918. StartProgram(true,[supTObject]);
  3919. Add([
  3920. '{$mode delphi}',
  3921. 'type',
  3922. ' TBird = class end;',
  3923. 'procedure Run(w: word);',
  3924. 'begin;',
  3925. ' if true then asm console.log(); end;',
  3926. ' if w>3 then asm',
  3927. ' var a = w+1;',
  3928. ' w = a+3;',
  3929. ' end;',
  3930. ' while (w>7) do asm',
  3931. ' w+=3; w*=2;',
  3932. ' end;',
  3933. ' try',
  3934. ' except',
  3935. ' on E: TBird do',
  3936. ' asm console.log(E); end;',
  3937. ' on E: TObject do',
  3938. ' asm var i=3; i--; end;',
  3939. ' else asm Fly; High; end;',
  3940. ' end;',
  3941. 'end;',
  3942. 'begin']);
  3943. ConvertProgram;
  3944. CheckSource('TestProc_AsmSubBlock',
  3945. LinesToStr([ // statements
  3946. 'rtl.createClass(this, "TBird", pas.system.TObject, function () {',
  3947. '});',
  3948. 'this.Run = function (w) {',
  3949. ' if (true) console.log();',
  3950. ' if (w > 3) {',
  3951. ' var a = w+1;',
  3952. ' w = a+3;',
  3953. ' };',
  3954. ' while (w > 7) {',
  3955. ' w+=3; w*=2;',
  3956. ' };',
  3957. ' try {} catch ($e) {',
  3958. ' if ($mod.TBird.isPrototypeOf($e)) {',
  3959. ' var E = $e;',
  3960. ' console.log(E);',
  3961. ' } else if (pas.system.TObject.isPrototypeOf($e)) {',
  3962. ' var E = $e;',
  3963. ' var i=3; i--;',
  3964. ' } else {',
  3965. ' Fly; High;',
  3966. ' }',
  3967. ' };',
  3968. '};',
  3969. '']),
  3970. LinesToStr([
  3971. ''
  3972. ]));
  3973. end;
  3974. procedure TTestModule.TestProc_Assembler;
  3975. begin
  3976. StartProgram(false);
  3977. Add('function DoIt: longint; assembler;');
  3978. Add('asm');
  3979. Add('{ a:{ b:{}, c:[]}, d:''1'' };');
  3980. Add('end;');
  3981. Add('begin');
  3982. ConvertProgram;
  3983. CheckSource('TestProc_Assembler',
  3984. LinesToStr([ // statements
  3985. 'this.DoIt = function () {',
  3986. ' { a:{ b:{}, c:[]}, d:''1'' };',
  3987. '};'
  3988. ]),
  3989. LinesToStr([
  3990. ''
  3991. ]));
  3992. end;
  3993. procedure TTestModule.TestProc_VarParam;
  3994. begin
  3995. StartProgram(false);
  3996. Add('type integer = longint;');
  3997. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  3998. Add('var vJ: integer;');
  3999. Add('begin');
  4000. Add(' vg:=vg+1;');
  4001. Add(' vj:=vh+2;');
  4002. Add(' vi:=vi+3;');
  4003. Add(' doit(vg,vg,vg);');
  4004. Add(' doit(vh,vh,vj);');
  4005. Add(' doit(vi,vi,vi);');
  4006. Add(' doit(vj,vj,vj);');
  4007. Add('end;');
  4008. Add('var i: integer;');
  4009. Add('begin');
  4010. Add(' doit(i,i,i);');
  4011. ConvertProgram;
  4012. CheckSource('TestProc_VarParam',
  4013. LinesToStr([ // statements
  4014. 'this.DoIt = function (vG,vH,vI) {',
  4015. ' var vJ = 0;',
  4016. ' vG = vG + 1;',
  4017. ' vJ = vH + 2;',
  4018. ' vI.set(vI.get()+3);',
  4019. ' $mod.DoIt(vG, vG, {',
  4020. ' get: function () {',
  4021. ' return vG;',
  4022. ' },',
  4023. ' set: function (v) {',
  4024. ' vG = v;',
  4025. ' }',
  4026. ' });',
  4027. ' $mod.DoIt(vH, vH, {',
  4028. ' get: function () {',
  4029. ' return vJ;',
  4030. ' },',
  4031. ' set: function (v) {',
  4032. ' vJ = v;',
  4033. ' }',
  4034. ' });',
  4035. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  4036. ' $mod.DoIt(vJ, vJ, {',
  4037. ' get: function () {',
  4038. ' return vJ;',
  4039. ' },',
  4040. ' set: function (v) {',
  4041. ' vJ = v;',
  4042. ' }',
  4043. ' });',
  4044. '};',
  4045. 'this.i = 0;'
  4046. ]),
  4047. LinesToStr([
  4048. '$mod.DoIt($mod.i,$mod.i,{',
  4049. ' p: $mod,',
  4050. ' get: function () {',
  4051. ' return this.p.i;',
  4052. ' },',
  4053. ' set: function (v) {',
  4054. ' this.p.i = v;',
  4055. ' }',
  4056. '});'
  4057. ]));
  4058. end;
  4059. procedure TTestModule.TestProc_VarParamString;
  4060. begin
  4061. StartProgram(false);
  4062. Add(['type TCaption = string;',
  4063. 'procedure DoIt(vA: TCaption; var vB: TCaption; out vC: TCaption);',
  4064. 'var c: char;',
  4065. 'begin',
  4066. ' va[1]:=c;',
  4067. ' vb[2]:=c;',
  4068. ' vc[3]:=c;',
  4069. 'end;',
  4070. 'begin']);
  4071. ConvertProgram;
  4072. CheckSource('TestProc_VarParamString',
  4073. LinesToStr([ // statements
  4074. 'this.DoIt = function (vA,vB,vC) {',
  4075. ' var c = "";',
  4076. ' vA = rtl.setCharAt(vA, 0, c);',
  4077. ' vB.set(rtl.setCharAt(vB.get(), 1, c));',
  4078. ' vC.set(rtl.setCharAt(vC.get(), 2, c));',
  4079. '};',
  4080. '']),
  4081. LinesToStr([
  4082. ]));
  4083. end;
  4084. procedure TTestModule.TestProc_VarParamV;
  4085. begin
  4086. StartProgram(false);
  4087. Add([
  4088. 'procedure Inc2(var i: longint);',
  4089. 'begin',
  4090. ' i:=i+2;',
  4091. 'end;',
  4092. 'procedure DoIt(v: longint);',
  4093. 'var p: array of longint;',
  4094. 'begin',
  4095. ' Inc2(v);',
  4096. ' Inc2(p[v]);',
  4097. 'end;',
  4098. 'begin']);
  4099. ConvertProgram;
  4100. CheckSource('TestProc_VarParamV',
  4101. LinesToStr([ // statements
  4102. 'this.Inc2 = function (i) {',
  4103. ' i.set(i.get()+2);',
  4104. '};',
  4105. 'this.DoIt = function (v) {',
  4106. ' var p = [];',
  4107. ' $mod.Inc2({get: function () {',
  4108. ' return v;',
  4109. ' }, set: function (w) {',
  4110. ' v = w;',
  4111. ' }});',
  4112. ' $mod.Inc2({',
  4113. ' a: v,',
  4114. ' p: p,',
  4115. ' get: function () {',
  4116. ' return this.p[this.a];',
  4117. ' },',
  4118. ' set: function (v) {',
  4119. ' this.p[this.a] = v;',
  4120. ' }',
  4121. ' });',
  4122. '};',
  4123. '']),
  4124. LinesToStr([
  4125. '']));
  4126. end;
  4127. procedure TTestModule.TestProc_Overload;
  4128. begin
  4129. StartProgram(false);
  4130. Add('procedure DoIt(vI: longint); begin end;');
  4131. Add('procedure DoIt(vI, vJ: longint); begin end;');
  4132. Add('procedure DoIt(vD: double); begin end;');
  4133. Add('begin');
  4134. Add(' DoIt(1);');
  4135. Add(' DoIt(2,3);');
  4136. Add(' DoIt(4.5);');
  4137. ConvertProgram;
  4138. CheckSource('TestProcedureOverload',
  4139. LinesToStr([ // statements
  4140. 'this.DoIt = function (vI) {',
  4141. '};',
  4142. 'this.DoIt$1 = function (vI, vJ) {',
  4143. '};',
  4144. 'this.DoIt$2 = function (vD) {',
  4145. '};',
  4146. '']),
  4147. LinesToStr([
  4148. '$mod.DoIt(1);',
  4149. '$mod.DoIt$1(2, 3);',
  4150. '$mod.DoIt$2(4.5);',
  4151. '']));
  4152. end;
  4153. procedure TTestModule.TestProc_OverloadForward;
  4154. begin
  4155. StartProgram(false);
  4156. Add('procedure DoIt(vI: longint); forward;');
  4157. Add('procedure DoIt(vI, vJ: longint); begin end;');
  4158. Add('procedure doit(vi: longint); begin end;');
  4159. Add('begin');
  4160. Add(' doit(1);');
  4161. Add(' doit(2,3);');
  4162. ConvertProgram;
  4163. CheckSource('TestProcedureOverloadForward',
  4164. LinesToStr([ // statements
  4165. 'this.DoIt$1 = function (vI, vJ) {',
  4166. '};',
  4167. 'this.DoIt = function (vI) {',
  4168. '};',
  4169. '']),
  4170. LinesToStr([
  4171. '$mod.DoIt(1);',
  4172. '$mod.DoIt$1(2, 3);',
  4173. '']));
  4174. end;
  4175. procedure TTestModule.TestProc_OverloadIntfImpl;
  4176. begin
  4177. StartUnit(false);
  4178. Add('interface');
  4179. Add('procedure DoIt(vI: longint);');
  4180. Add('procedure DoIt(vI, vJ: longint);');
  4181. Add('implementation');
  4182. Add('procedure DoIt(vI, vJ, vK, vL, vM: longint); forward;');
  4183. Add('procedure DoIt(vI, vJ, vK: longint); begin end;');
  4184. Add('procedure DoIt(vi: longint); begin end;');
  4185. Add('procedure DoIt(vI, vJ, vK, vL: longint); begin end;');
  4186. Add('procedure DoIt(vi, vj: longint); begin end;');
  4187. Add('procedure DoIt(vi, vj, vk, vl, vm: longint); begin end;');
  4188. Add('begin');
  4189. Add(' doit(1);');
  4190. Add(' doit(2,3);');
  4191. Add(' doit(4,5,6);');
  4192. Add(' doit(7,8,9,10);');
  4193. Add(' doit(11,12,13,14,15);');
  4194. ConvertUnit;
  4195. CheckSource('TestProcedureOverloadUnit',
  4196. LinesToStr([ // statements
  4197. 'var $impl = $mod.$impl;',
  4198. 'this.DoIt = function (vI) {',
  4199. '};',
  4200. 'this.DoIt$1 = function (vI, vJ) {',
  4201. '};',
  4202. '']),
  4203. LinesToStr([ // this.$init
  4204. '$mod.DoIt(1);',
  4205. '$mod.DoIt$1(2, 3);',
  4206. '$impl.DoIt$3(4,5,6);',
  4207. '$impl.DoIt$4(7,8,9,10);',
  4208. '$impl.DoIt$2(11,12,13,14,15);',
  4209. '']),
  4210. LinesToStr([ // implementation
  4211. '$impl.DoIt$3 = function (vI, vJ, vK) {',
  4212. '};',
  4213. '$impl.DoIt$4 = function (vI, vJ, vK, vL) {',
  4214. '};',
  4215. '$impl.DoIt$2 = function (vI, vJ, vK, vL, vM) {',
  4216. '};',
  4217. '']));
  4218. end;
  4219. procedure TTestModule.TestProc_OverloadNested;
  4220. begin
  4221. StartProgram(false);
  4222. Add([
  4223. 'procedure doit(vA: longint);',
  4224. ' procedure DoIt(vA, vB: longint); overload;',
  4225. ' begin',
  4226. ' doit(1);',
  4227. ' doit(1,2);',
  4228. ' end;',
  4229. ' procedure doit(vA, vB, vC: longint);',
  4230. ' begin',
  4231. ' doit(1);',
  4232. ' doit(1,2);',
  4233. ' doit(1,2,3);',
  4234. ' end;',
  4235. 'begin',
  4236. ' doit(1);',
  4237. ' doit(1,2);',
  4238. ' doit(1,2,3);',
  4239. 'end;',
  4240. 'begin // main',
  4241. ' doit(1);']);
  4242. ConvertProgram;
  4243. CheckSource('TestProcedureOverloadNested',
  4244. LinesToStr([ // statements
  4245. 'this.doit = function (vA) {',
  4246. ' function DoIt$1(vA, vB) {',
  4247. ' $mod.doit(1);',
  4248. ' DoIt$1(1, 2);',
  4249. ' };',
  4250. ' function doit$2(vA, vB, vC) {',
  4251. ' $mod.doit(1);',
  4252. ' DoIt$1(1, 2);',
  4253. ' doit$2(1, 2, 3);',
  4254. ' };',
  4255. ' $mod.doit(1);',
  4256. ' DoIt$1(1, 2);',
  4257. ' doit$2(1, 2, 3);',
  4258. '};',
  4259. '']),
  4260. LinesToStr([
  4261. '$mod.doit(1);',
  4262. '']));
  4263. end;
  4264. procedure TTestModule.TestProc_OverloadNestedForward;
  4265. begin
  4266. StartProgram(false);
  4267. Add([
  4268. 'procedure DoIt(vA: longint); overload; forward;',
  4269. 'procedure DoIt(vB, vC: longint); overload;',
  4270. 'begin // 2 param overload',
  4271. ' doit(1);',
  4272. ' doit(1,2);',
  4273. 'end;',
  4274. 'procedure doit(vA: longint);',
  4275. ' procedure DoIt(vA, vB, vC: longint); overload; forward;',
  4276. ' procedure DoIt(vA, vB, vC, vD: longint); overload;',
  4277. ' begin // 4 param overload',
  4278. ' doit(1);',
  4279. ' doit(1,2);',
  4280. ' doit(1,2,3);',
  4281. ' doit(1,2,3,4);',
  4282. ' end;',
  4283. ' procedure doit(vA, vB, vC: longint);',
  4284. ' procedure DoIt(vA, vB, vC, vD, vE: longint); overload; forward;',
  4285. ' procedure DoIt(vA, vB, vC, vD, vE, vF: longint); overload;',
  4286. ' begin // 6 param overload',
  4287. ' doit(1);',
  4288. ' doit(1,2);',
  4289. ' doit(1,2,3);',
  4290. ' doit(1,2,3,4);',
  4291. ' doit(1,2,3,4,5);',
  4292. ' doit(1,2,3,4,5,6);',
  4293. ' end;',
  4294. ' procedure doit(vA, vB, vC, vD, vE: longint);',
  4295. ' begin // 5 param overload',
  4296. ' doit(1);',
  4297. ' doit(1,2);',
  4298. ' doit(1,2,3);',
  4299. ' doit(1,2,3,4);',
  4300. ' doit(1,2,3,4,5);',
  4301. ' doit(1,2,3,4,5,6);',
  4302. ' end;',
  4303. ' begin // 3 param overload',
  4304. ' doit(1);',
  4305. ' doit(1,2);',
  4306. ' doit(1,2,3);',
  4307. ' doit(1,2,3,4);',
  4308. ' doit(1,2,3,4,5);',
  4309. ' doit(1,2,3,4,5,6);',
  4310. ' end;',
  4311. 'begin // 1 param overload',
  4312. ' doit(1);',
  4313. ' doit(1,2);',
  4314. ' doit(1,2,3);',
  4315. ' doit(1,2,3,4);',
  4316. 'end;',
  4317. 'begin // main',
  4318. ' doit(1);',
  4319. ' doit(1,2);']);
  4320. ConvertProgram;
  4321. CheckSource('TestProc_OverloadNestedForward',
  4322. LinesToStr([ // statements
  4323. 'this.DoIt$1 = function (vB, vC) {',
  4324. ' $mod.DoIt(1);',
  4325. ' $mod.DoIt$1(1, 2);',
  4326. '};',
  4327. 'this.DoIt = function (vA) {',
  4328. ' function DoIt$3(vA, vB, vC, vD) {',
  4329. ' $mod.DoIt(1);',
  4330. ' $mod.DoIt$1(1, 2);',
  4331. ' DoIt$2(1, 2, 3);',
  4332. ' DoIt$3(1, 2, 3, 4);',
  4333. ' };',
  4334. ' function DoIt$2(vA, vB, vC) {',
  4335. ' function DoIt$5(vA, vB, vC, vD, vE, vF) {',
  4336. ' $mod.DoIt(1);',
  4337. ' $mod.DoIt$1(1, 2);',
  4338. ' DoIt$2(1, 2, 3);',
  4339. ' DoIt$3(1, 2, 3, 4);',
  4340. ' DoIt$4(1, 2, 3, 4, 5);',
  4341. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  4342. ' };',
  4343. ' function DoIt$4(vA, vB, vC, vD, vE) {',
  4344. ' $mod.DoIt(1);',
  4345. ' $mod.DoIt$1(1, 2);',
  4346. ' DoIt$2(1, 2, 3);',
  4347. ' DoIt$3(1, 2, 3, 4);',
  4348. ' DoIt$4(1, 2, 3, 4, 5);',
  4349. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  4350. ' };',
  4351. ' $mod.DoIt(1);',
  4352. ' $mod.DoIt$1(1, 2);',
  4353. ' DoIt$2(1, 2, 3);',
  4354. ' DoIt$3(1, 2, 3, 4);',
  4355. ' DoIt$4(1, 2, 3, 4, 5);',
  4356. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  4357. ' };',
  4358. ' $mod.DoIt(1);',
  4359. ' $mod.DoIt$1(1, 2);',
  4360. ' DoIt$2(1, 2, 3);',
  4361. ' DoIt$3(1, 2, 3, 4);',
  4362. '};',
  4363. '']),
  4364. LinesToStr([
  4365. '$mod.DoIt(1);',
  4366. '$mod.DoIt$1(1, 2);',
  4367. '']));
  4368. end;
  4369. procedure TTestModule.TestProc_OverloadUnitCycle;
  4370. begin
  4371. AddModuleWithIntfImplSrc('Unit2.pas',
  4372. LinesToStr([
  4373. 'type',
  4374. ' TObject = class',
  4375. ' procedure DoIt(b: boolean); virtual; abstract;',
  4376. ' procedure DoIt(i: longint); virtual; abstract;',
  4377. ' end;',
  4378. '']),
  4379. 'uses test1;');
  4380. StartUnit(true);
  4381. Add([
  4382. 'interface',
  4383. 'uses unit2;',
  4384. 'type',
  4385. ' TEagle = class(TObject)',
  4386. ' procedure DoIt(b: boolean); override;',
  4387. ' procedure DoIt(i: longint); override;',
  4388. ' end;',
  4389. 'implementation',
  4390. 'procedure TEagle.DoIt(b: boolean); begin end;',
  4391. 'procedure TEagle.DoIt(i: longint); begin end;',
  4392. '']);
  4393. ConvertUnit;
  4394. CheckSource('TestProc_OverloadUnitCycle',
  4395. LinesToStr([ // statements
  4396. 'rtl.createClass(this, "TEagle", pas.Unit2.TObject, function () {',
  4397. ' this.DoIt = function (b) {',
  4398. ' };',
  4399. ' this.DoIt$1 = function (i) {',
  4400. ' };',
  4401. '});',
  4402. '']),
  4403. '',
  4404. LinesToStr([
  4405. '']));
  4406. end;
  4407. procedure TTestModule.TestProc_Varargs;
  4408. begin
  4409. StartProgram(false);
  4410. Add([
  4411. 'procedure ProcA(i:longint); varargs; external name ''ProcA'';',
  4412. 'procedure ProcB; varargs; external name ''ProcB'';',
  4413. 'procedure ProcC(i: longint = 17); varargs; external name ''ProcC'';',
  4414. 'function GetIt: longint; begin end;',
  4415. 'begin',
  4416. ' ProcA(1);',
  4417. ' ProcA(1,2);',
  4418. ' ProcA(1,2.0);',
  4419. ' ProcA(1,2,3);',
  4420. ' ProcA(1,''2'');',
  4421. ' ProcA(2,'''');',
  4422. ' ProcA(3,false);',
  4423. ' ProcB;',
  4424. ' ProcB();',
  4425. ' ProcB(4);',
  4426. ' ProcB(''foo'');',
  4427. ' ProcC;',
  4428. ' ProcC();',
  4429. ' ProcC(4);',
  4430. ' ProcC(5,''foo'');',
  4431. ' ProcB(GetIt);',
  4432. ' ProcB(GetIt());',
  4433. ' ProcB(GetIt,GetIt());']);
  4434. ConvertProgram;
  4435. CheckSource('TestProc_Varargs',
  4436. LinesToStr([ // statements
  4437. 'this.GetIt = function () {',
  4438. ' var Result = 0;',
  4439. ' return Result;',
  4440. '};',
  4441. '']),
  4442. LinesToStr([
  4443. 'ProcA(1);',
  4444. 'ProcA(1, 2);',
  4445. 'ProcA(1, 2.0);',
  4446. 'ProcA(1, 2, 3);',
  4447. 'ProcA(1, "2");',
  4448. 'ProcA(2, "");',
  4449. 'ProcA(3, false);',
  4450. 'ProcB();',
  4451. 'ProcB();',
  4452. 'ProcB(4);',
  4453. 'ProcB("foo");',
  4454. 'ProcC(17);',
  4455. 'ProcC(17);',
  4456. 'ProcC(4);',
  4457. 'ProcC(5, "foo");',
  4458. 'ProcB($mod.GetIt());',
  4459. 'ProcB($mod.GetIt());',
  4460. 'ProcB($mod.GetIt(), $mod.GetIt());',
  4461. '']));
  4462. end;
  4463. procedure TTestModule.TestProc_ConstOrder;
  4464. begin
  4465. StartProgram(false);
  4466. Add([
  4467. 'const A = 3;',
  4468. 'const B = A+1;',
  4469. 'procedure DoIt;',
  4470. 'const C = A+1;',
  4471. 'const D = B+1;',
  4472. 'const E = D+C+B+A;',
  4473. 'begin',
  4474. 'end;',
  4475. 'begin'
  4476. ]);
  4477. ConvertProgram;
  4478. CheckSource('TestProc_ConstOrder',
  4479. LinesToStr([ // statements
  4480. 'this.A = 3;',
  4481. 'this.B = 3 + 1;',
  4482. 'var C = 3 + 1;',
  4483. 'var D = 4 + 1;',
  4484. 'var E = 5 + 4 + 4 + 3;',
  4485. 'this.DoIt = function () {',
  4486. '};',
  4487. '']),
  4488. LinesToStr([
  4489. ''
  4490. ]));
  4491. end;
  4492. procedure TTestModule.TestProc_DuplicateConst;
  4493. begin
  4494. StartProgram(false);
  4495. Add([
  4496. 'const A = 1;',
  4497. 'procedure DoIt;',
  4498. 'const A = 2;',
  4499. ' procedure SubIt;',
  4500. ' const A = 21;',
  4501. ' begin',
  4502. ' end;',
  4503. 'begin',
  4504. 'end;',
  4505. 'procedure DoSome;',
  4506. 'const A = 3;',
  4507. 'begin',
  4508. 'end;',
  4509. 'begin'
  4510. ]);
  4511. ConvertProgram;
  4512. CheckSource('TestProc_DuplicateConst',
  4513. LinesToStr([ // statements
  4514. 'this.A = 1;',
  4515. 'var A$1 = 2;',
  4516. 'var A$2 = 21;',
  4517. 'this.DoIt = function () {',
  4518. ' function SubIt() {',
  4519. ' };',
  4520. '};',
  4521. 'var A$3 = 3;',
  4522. 'this.DoSome = function () {',
  4523. '};',
  4524. '']),
  4525. LinesToStr([
  4526. ''
  4527. ]));
  4528. end;
  4529. procedure TTestModule.TestProc_LocalVarAbsolute;
  4530. begin
  4531. StartProgram(false);
  4532. Add([
  4533. 'type',
  4534. ' TObject = class',
  4535. ' Index: longint;',
  4536. ' procedure DoAbs(Item: pointer);',
  4537. ' end;',
  4538. 'procedure TObject.DoAbs(Item: pointer);',
  4539. 'var',
  4540. ' o: TObject absolute Item;',
  4541. 'begin',
  4542. ' if o.Index<o.Index then o.Index:=o.Index;',
  4543. 'end;',
  4544. 'procedure DoIt(i: longint; p: pointer);',
  4545. 'var',
  4546. ' d: double absolute i;',
  4547. ' s: string absolute d;',
  4548. ' oi: TObject absolute i;',
  4549. ' op: TObject absolute p;',
  4550. 'begin',
  4551. ' if d=d then d:=d;',
  4552. ' if s=s then s:=s;',
  4553. ' if oi.Index<oi.Index then oi.Index:=oi.Index;',
  4554. ' if op.Index=op.Index then op.Index:=op.Index;',
  4555. 'end;',
  4556. 'begin']);
  4557. ConvertProgram;
  4558. CheckSource('TestProc_LocalVarAbsolute',
  4559. LinesToStr([ // statements
  4560. 'rtl.createClass(this, "TObject", null, function () {',
  4561. ' this.$init = function () {',
  4562. ' this.Index = 0;',
  4563. ' };',
  4564. ' this.$final = function () {',
  4565. ' };',
  4566. ' this.DoAbs = function (Item) {',
  4567. ' if (Item.Index < Item.Index) Item.Index = Item.Index;',
  4568. ' };',
  4569. '});',
  4570. 'this.DoIt = function (i, p) {',
  4571. ' if (i === i) i = i;',
  4572. ' if (i === i) i = i;',
  4573. ' if (i.Index < i.Index) i.Index = i.Index;',
  4574. ' if (p.Index === p.Index) p.Index = p.Index;',
  4575. '};'
  4576. ]),
  4577. LinesToStr([
  4578. ]));
  4579. end;
  4580. procedure TTestModule.TestProc_LocalVarInit;
  4581. begin
  4582. StartProgram(false);
  4583. Add([
  4584. 'type TBytes = array of byte;',
  4585. 'procedure DoIt;',
  4586. 'const c = 4;',
  4587. 'var',
  4588. ' b: byte = 1;',
  4589. ' w: word = 2+c;',
  4590. ' p: pointer = nil;',
  4591. ' Buffer: TBytes = nil;',
  4592. 'begin',
  4593. 'end;',
  4594. 'begin']);
  4595. ConvertProgram;
  4596. CheckSource('TestProc_LocalVarInit',
  4597. LinesToStr([ // statements
  4598. 'var c = 4;',
  4599. 'this.DoIt = function () {',
  4600. ' var b = 1;',
  4601. ' var w = 2 + 4;',
  4602. ' var p = null;',
  4603. ' var Buffer = [];',
  4604. '};',
  4605. '']),
  4606. LinesToStr([
  4607. ]));
  4608. end;
  4609. procedure TTestModule.TestProc_ReservedWords;
  4610. begin
  4611. StartProgram(false);
  4612. Add([
  4613. 'procedure Date(ArrayBuffer: longint);',
  4614. 'const',
  4615. ' NaN: longint = 3;',
  4616. 'var',
  4617. ' &Boolean: longint;',
  4618. ' procedure Error(ArrayBuffer: longint);',
  4619. ' begin',
  4620. ' end;',
  4621. 'begin',
  4622. ' Nan:=&bOolean;',
  4623. 'end;',
  4624. 'begin',
  4625. ' Date(1);']);
  4626. ConvertProgram;
  4627. CheckSource('TestProc_ReservedWords',
  4628. LinesToStr([ // statements
  4629. 'var naN = 3;',
  4630. 'this.Date = function (arrayBuffer) {',
  4631. ' var boolean = 0;',
  4632. ' function error(arrayBuffer) {',
  4633. ' };',
  4634. ' naN = boolean;',
  4635. '};',
  4636. '']),
  4637. LinesToStr([
  4638. ' $mod.Date(1);'
  4639. ]));
  4640. end;
  4641. procedure TTestModule.TestProc_ConstRefWord;
  4642. begin
  4643. StartProgram(false);
  4644. Add([
  4645. 'procedure Run(constref w: word);',
  4646. 'var l: word;',
  4647. 'begin',
  4648. ' l:=w;',
  4649. ' Run(w);',
  4650. ' Run(l);',
  4651. 'end;',
  4652. 'procedure Fly(a: word; var b: word; out c: word; const d: word; constref e: word);',
  4653. 'begin',
  4654. ' Run(a);',
  4655. ' Run(b);',
  4656. ' Run(c);',
  4657. ' Run(d);',
  4658. ' Run(e);',
  4659. 'end;',
  4660. 'begin',
  4661. ' Run(1);']);
  4662. ConvertProgram;
  4663. CheckHint(mtWarning,nConstRefNotForXAsConst,'ConstRef not yet implemented for Word. Treating as Const');
  4664. CheckSource('TestProc_ConstRefWord',
  4665. LinesToStr([ // statements
  4666. 'this.Run = function (w) {',
  4667. ' var l = 0;',
  4668. ' l = w;',
  4669. ' $mod.Run(w);',
  4670. ' $mod.Run(l);',
  4671. '};',
  4672. 'this.Fly = function (a, b, c, d, e) {',
  4673. ' $mod.Run(a);',
  4674. ' $mod.Run(b.get());',
  4675. ' $mod.Run(c.get());',
  4676. ' $mod.Run(d);',
  4677. ' $mod.Run(e);',
  4678. '};',
  4679. '']),
  4680. LinesToStr([
  4681. '$mod.Run(1);'
  4682. ]));
  4683. end;
  4684. procedure TTestModule.TestAnonymousProc_Assign_ObjFPC;
  4685. begin
  4686. StartProgram(false);
  4687. Add([
  4688. '{$mode objfpc}',
  4689. 'type',
  4690. ' TFunc = reference to function(x: word): word;',
  4691. 'var Func: TFunc;',
  4692. 'procedure DoIt(a: word);',
  4693. 'begin',
  4694. ' Func:=function(b:word): word',
  4695. ' begin',
  4696. ' Result:=a+b;',
  4697. ' exit(b);',
  4698. ' exit(Result);',
  4699. ' end;',// test semicolon
  4700. ' a:=3;',
  4701. 'end;',
  4702. 'begin',
  4703. ' Func:=function(c:word):word begin',
  4704. ' Result:=3+c;',
  4705. ' exit(c);',
  4706. ' exit(Result);',
  4707. ' end;']);
  4708. ConvertProgram;
  4709. CheckSource('TestAnonymousProc_Assign_ObjFPC',
  4710. LinesToStr([ // statements
  4711. 'this.Func = null;',
  4712. 'this.DoIt = function (a) {',
  4713. ' $mod.Func = function (b) {',
  4714. ' var Result = 0;',
  4715. ' Result = a + b;',
  4716. ' return b;',
  4717. ' return Result;',
  4718. ' return Result;',
  4719. ' };',
  4720. ' a = 3;',
  4721. '};',
  4722. '']),
  4723. LinesToStr([
  4724. '$mod.Func = function (c) {',
  4725. ' var Result = 0;',
  4726. ' Result = 3 + c;',
  4727. ' return c;',
  4728. ' return Result;',
  4729. ' return Result;',
  4730. '};',
  4731. '']));
  4732. end;
  4733. procedure TTestModule.TestAnonymousProc_Assign_Delphi;
  4734. begin
  4735. StartProgram(false);
  4736. Add([
  4737. '{$mode delphi}',
  4738. 'type',
  4739. ' TProc = reference to procedure(x: word);',
  4740. 'procedure DoIt(a: word);',
  4741. 'var Proc: TProc;',
  4742. 'begin',
  4743. ' Proc:=procedure(b:word) begin end;',
  4744. 'end;',
  4745. 'var Proc: TProc;',
  4746. 'begin',
  4747. ' Proc:=procedure(c:word) begin end;',
  4748. '']);
  4749. ConvertProgram;
  4750. CheckSource('TestAnonymousProc_Assign_Delphi',
  4751. LinesToStr([ // statements
  4752. 'this.DoIt = function (a) {',
  4753. ' var Proc = null;',
  4754. ' Proc = function (b) {',
  4755. ' };',
  4756. '};',
  4757. 'this.Proc = null;',
  4758. '']),
  4759. LinesToStr([
  4760. '$mod.Proc = function (c) {',
  4761. '};',
  4762. '']));
  4763. end;
  4764. procedure TTestModule.TestAnonymousProc_Arg;
  4765. begin
  4766. StartProgram(false);
  4767. Add([
  4768. 'type',
  4769. ' TProc = reference to procedure;',
  4770. ' TFunc = reference to function(x: word): word;',
  4771. 'procedure DoMore(f,g: TProc);',
  4772. 'begin',
  4773. 'end;',
  4774. 'procedure DoOdd(v: jsvalue);',
  4775. 'begin',
  4776. 'end;',
  4777. 'procedure DoIt(f: TFunc);',
  4778. 'begin',
  4779. ' DoIt(function(b:word): word',
  4780. ' begin',
  4781. ' Result:=1+b;',
  4782. ' end);',
  4783. ' DoMore(procedure begin end, procedure begin end);',
  4784. ' DoOdd(procedure begin end);',
  4785. 'end;',
  4786. 'begin',
  4787. ' DoMore(procedure begin end,',
  4788. ' procedure assembler asm',
  4789. ' console.log("c");',
  4790. ' end);',
  4791. '']);
  4792. ConvertProgram;
  4793. CheckSource('TestAnonymousProc_Arg',
  4794. LinesToStr([ // statements
  4795. 'this.DoMore = function (f, g) {',
  4796. '};',
  4797. 'this.DoOdd = function (v) {',
  4798. '};',
  4799. 'this.DoIt = function (f) {',
  4800. ' $mod.DoIt(function (b) {',
  4801. ' var Result = 0;',
  4802. ' Result = 1 + b;',
  4803. ' return Result;',
  4804. ' });',
  4805. ' $mod.DoMore(function () {',
  4806. ' }, function () {',
  4807. ' });',
  4808. ' $mod.DoOdd(function () {',
  4809. ' });',
  4810. '};',
  4811. '']),
  4812. LinesToStr([
  4813. '$mod.DoMore(function () {',
  4814. '}, function () {',
  4815. ' console.log("c");',
  4816. '});',
  4817. '']));
  4818. end;
  4819. procedure TTestModule.TestAnonymousProc_Typecast;
  4820. begin
  4821. StartProgram(false);
  4822. Add([
  4823. 'type',
  4824. ' TProc = reference to procedure(w: word);',
  4825. ' TArr = array of word;',
  4826. ' TFuncArr = reference to function: TArr;',
  4827. 'procedure DoIt(p: TProc);',
  4828. 'var',
  4829. ' w: word;',
  4830. ' a: TArr;',
  4831. 'begin',
  4832. ' p:=TProc(procedure(b: smallint) begin end);',
  4833. ' a:=TFuncArr(function: TArr begin end)();',
  4834. ' w:=TFuncArr(function: TArr begin end)()[3];',
  4835. 'end;',
  4836. 'begin']);
  4837. ConvertProgram;
  4838. CheckSource('TestAnonymousProc_Typecast',
  4839. LinesToStr([ // statements
  4840. 'this.DoIt = function (p) {',
  4841. ' var w = 0;',
  4842. ' var a = [];',
  4843. ' p = function (b) {',
  4844. ' };',
  4845. ' a = function () {',
  4846. ' var Result = [];',
  4847. ' return Result;',
  4848. ' }();',
  4849. ' w = function () {',
  4850. ' var Result = [];',
  4851. ' return Result;',
  4852. ' }()[3];',
  4853. '};',
  4854. '']),
  4855. LinesToStr([
  4856. '']));
  4857. end;
  4858. procedure TTestModule.TestAnonymousProc_With;
  4859. begin
  4860. StartProgram(false);
  4861. Add([
  4862. 'type',
  4863. ' TProc = reference to procedure(w: word);',
  4864. ' TObject = class',
  4865. ' b: boolean;',
  4866. ' end;',
  4867. 'var',
  4868. ' p: TProc;',
  4869. ' bird: TObject;',
  4870. 'begin',
  4871. ' with bird do',
  4872. ' p:=procedure(w: word)',
  4873. ' begin',
  4874. ' b:=w>2;',
  4875. ' end;',
  4876. '']);
  4877. ConvertProgram;
  4878. CheckSource('TestAnonymousProc_With',
  4879. LinesToStr([ // statements
  4880. 'rtl.createClass(this, "TObject", null, function () {',
  4881. ' this.$init = function () {',
  4882. ' this.b = false;',
  4883. ' };',
  4884. ' this.$final = function () {',
  4885. ' };',
  4886. '});',
  4887. 'this.p = null;',
  4888. 'this.bird = null;',
  4889. '']),
  4890. LinesToStr([
  4891. 'var $with = $mod.bird;',
  4892. '$mod.p = function (w) {',
  4893. ' $with.b = w > 2;',
  4894. '};',
  4895. '']));
  4896. end;
  4897. procedure TTestModule.TestAnonymousProc_ExceptOn;
  4898. begin
  4899. StartProgram(false);
  4900. Add([
  4901. 'type',
  4902. ' TProc = reference to procedure;',
  4903. ' TObject = class',
  4904. ' b: boolean;',
  4905. ' end;',
  4906. 'procedure DoIt;',
  4907. 'var',
  4908. ' p: TProc;',
  4909. 'begin',
  4910. ' try',
  4911. ' except',
  4912. ' on E: TObject do',
  4913. ' p:=procedure',
  4914. ' begin',
  4915. ' E.b:=true;',
  4916. ' end;',
  4917. ' end;',
  4918. 'end;',
  4919. 'begin']);
  4920. ConvertProgram;
  4921. CheckSource('TestAnonymousProc_ExceptOn',
  4922. LinesToStr([ // statements
  4923. 'rtl.createClass(this, "TObject", null, function () {',
  4924. ' this.$init = function () {',
  4925. ' this.b = false;',
  4926. ' };',
  4927. ' this.$final = function () {',
  4928. ' };',
  4929. '});',
  4930. 'this.DoIt = function () {',
  4931. ' var p = null;',
  4932. ' try {} catch ($e) {',
  4933. ' if ($mod.TObject.isPrototypeOf($e)) {',
  4934. ' var E = $e;',
  4935. ' p = function () {',
  4936. ' E.b = true;',
  4937. ' };',
  4938. ' } else throw $e',
  4939. ' };',
  4940. '};',
  4941. '']),
  4942. LinesToStr([
  4943. '']));
  4944. end;
  4945. procedure TTestModule.TestAnonymousProc_Nested;
  4946. begin
  4947. StartProgram(false);
  4948. Add([
  4949. 'type',
  4950. ' TProc = reference to procedure;',
  4951. ' TObject = class',
  4952. ' i: byte;',
  4953. ' procedure DoIt;',
  4954. ' end;',
  4955. 'procedure TObject.DoIt;',
  4956. 'var',
  4957. ' p: TProc;',
  4958. ' procedure Sub;',
  4959. ' begin',
  4960. ' p:=procedure',
  4961. ' begin',
  4962. ' i:=3;',
  4963. ' Self.i:=4;',
  4964. ' p:=procedure',
  4965. ' procedure SubSub;',
  4966. ' begin',
  4967. ' i:=13;',
  4968. ' Self.i:=14;',
  4969. ' end;',
  4970. ' begin',
  4971. ' i:=13;',
  4972. ' Self.i:=14;',
  4973. ' end;',
  4974. ' end;',
  4975. ' end;',
  4976. 'begin',
  4977. 'end;',
  4978. 'begin']);
  4979. ConvertProgram;
  4980. CheckSource('TestAnonymousProc_Nested',
  4981. LinesToStr([ // statements
  4982. 'rtl.createClass(this, "TObject", null, function () {',
  4983. ' this.$init = function () {',
  4984. ' this.i = 0;',
  4985. ' };',
  4986. ' this.$final = function () {',
  4987. ' };',
  4988. ' this.DoIt = function () {',
  4989. ' var $Self = this;',
  4990. ' var p = null;',
  4991. ' function Sub() {',
  4992. ' p = function () {',
  4993. ' $Self.i = 3;',
  4994. ' $Self.i = 4;',
  4995. ' p = function () {',
  4996. ' function SubSub() {',
  4997. ' $Self.i = 13;',
  4998. ' $Self.i = 14;',
  4999. ' };',
  5000. ' $Self.i = 13;',
  5001. ' $Self.i = 14;',
  5002. ' };',
  5003. ' };',
  5004. ' };',
  5005. ' };',
  5006. '});',
  5007. '']),
  5008. LinesToStr([
  5009. '']));
  5010. end;
  5011. procedure TTestModule.TestAnonymousProc_NestedAssignResult;
  5012. begin
  5013. StartProgram(false);
  5014. Add([
  5015. 'type',
  5016. ' TProc = reference to procedure;',
  5017. 'function DoIt: TProc;',
  5018. ' function Sub: TProc;',
  5019. ' begin',
  5020. ' Result:=procedure',
  5021. ' begin',
  5022. ' Sub:=procedure',
  5023. ' procedure SubSub;',
  5024. ' begin',
  5025. ' Result:=nil;',
  5026. ' Sub:=nil;',
  5027. ' DoIt:=nil;',
  5028. ' end;',
  5029. ' begin',
  5030. ' Result:=nil;',
  5031. ' Sub:=nil;',
  5032. ' DoIt:=nil;',
  5033. ' end;',
  5034. ' end;',
  5035. ' end;',
  5036. 'begin',
  5037. 'end;',
  5038. 'begin']);
  5039. ConvertProgram;
  5040. CheckSource('TestAnonymousProc_NestedAssignResult',
  5041. LinesToStr([ // statements
  5042. 'this.DoIt = function () {',
  5043. ' var Result = null;',
  5044. ' function Sub() {',
  5045. ' var Result$1 = null;',
  5046. ' Result$1 = function () {',
  5047. ' Result$1 = function () {',
  5048. ' function SubSub() {',
  5049. ' Result$1 = null;',
  5050. ' Result$1 = null;',
  5051. ' Result = null;',
  5052. ' };',
  5053. ' Result$1 = null;',
  5054. ' Result$1 = null;',
  5055. ' Result = null;',
  5056. ' };',
  5057. ' };',
  5058. ' return Result$1;',
  5059. ' };',
  5060. ' return Result;',
  5061. '};',
  5062. '']),
  5063. LinesToStr([
  5064. '']));
  5065. end;
  5066. procedure TTestModule.TestAnonymousProc_Class;
  5067. begin
  5068. StartProgram(false);
  5069. Add([
  5070. 'type',
  5071. ' TProc = reference to procedure;',
  5072. ' TEvent = procedure of object;',
  5073. ' TObject = class',
  5074. ' Size: word;',
  5075. ' function GetIt: TProc;',
  5076. ' procedure DoIt; virtual; abstract;',
  5077. ' end;',
  5078. 'function TObject.GetIt: TProc;',
  5079. 'begin',
  5080. ' Result:=procedure',
  5081. ' var p: TEvent;',
  5082. ' begin',
  5083. ' Size:=Size;',
  5084. ' Size:=Self.Size;',
  5085. ' p:=@DoIt;',
  5086. ' p:[email protected];',
  5087. ' end;',
  5088. 'end;',
  5089. 'begin']);
  5090. ConvertProgram;
  5091. CheckSource('TestAnonymousProc_Class',
  5092. LinesToStr([ // statements
  5093. 'rtl.createClass(this, "TObject", null, function () {',
  5094. ' this.$init = function () {',
  5095. ' this.Size = 0;',
  5096. ' };',
  5097. ' this.$final = function () {',
  5098. ' };',
  5099. ' this.GetIt = function () {',
  5100. ' var $Self = this;',
  5101. ' var Result = null;',
  5102. ' Result = function () {',
  5103. ' var p = null;',
  5104. ' $Self.Size = $Self.Size;',
  5105. ' $Self.Size = $Self.Size;',
  5106. ' p = rtl.createCallback($Self, "DoIt");',
  5107. ' p = rtl.createCallback($Self, "DoIt");',
  5108. ' };',
  5109. ' return Result;',
  5110. ' };',
  5111. '});',
  5112. '']),
  5113. LinesToStr([
  5114. '']));
  5115. end;
  5116. procedure TTestModule.TestAnonymousProc_ForLoop;
  5117. begin
  5118. StartProgram(false);
  5119. Add([
  5120. 'type TProc = reference to procedure;',
  5121. 'procedure Foo(p: TProc);',
  5122. 'begin',
  5123. 'end;',
  5124. 'procedure DoIt;',
  5125. 'var i: word;',
  5126. ' a: word;',
  5127. 'begin',
  5128. ' for i:=1 to 10 do begin',
  5129. ' Foo(procedure begin a:=3; end);',
  5130. ' end;',
  5131. 'end;',
  5132. 'begin',
  5133. ' DoIt;']);
  5134. ConvertProgram;
  5135. CheckSource('TestAnonymousProc_ForLoop',
  5136. LinesToStr([ // statements
  5137. 'this.Foo = function (p) {',
  5138. '};',
  5139. 'this.DoIt = function () {',
  5140. ' var i = 0;',
  5141. ' var a = 0;',
  5142. ' for (i = 1; i <= 10; i++) {',
  5143. ' $mod.Foo(function () {',
  5144. ' a = 3;',
  5145. ' });',
  5146. ' };',
  5147. '};',
  5148. '']),
  5149. LinesToStr([
  5150. '$mod.DoIt();'
  5151. ]));
  5152. end;
  5153. procedure TTestModule.TestAnonymousProc_AsmDelphi;
  5154. begin
  5155. StartProgram(false);
  5156. Add([
  5157. '{$mode delphi}',
  5158. 'type',
  5159. ' TProc = reference to procedure;',
  5160. ' TFunc = reference to function(x: word): word;',
  5161. 'procedure Run;',
  5162. 'asm',
  5163. 'end;',
  5164. 'procedure Walk(p: TProc; f: TFunc);',
  5165. 'begin',
  5166. ' Walk(procedure asm end, function(b:word): word asm return 1+b; end);',
  5167. 'end;',
  5168. 'begin',
  5169. ' Walk(procedure',
  5170. ' asm',
  5171. ' console.log("a");',
  5172. ' end,',
  5173. ' function(x: word): word asm',
  5174. ' console.log("c");',
  5175. ' end);',
  5176. '']);
  5177. ConvertProgram;
  5178. CheckSource('TestAnonymousProc_AsmDelphi',
  5179. LinesToStr([ // statements
  5180. 'this.Run = function () {',
  5181. '};',
  5182. 'this.Walk = function (p, f) {',
  5183. ' $mod.Walk(function () {',
  5184. ' }, function (b) {',
  5185. ' return 1+b;',
  5186. ' });',
  5187. '};',
  5188. '']),
  5189. LinesToStr([
  5190. '$mod.Walk(function () {',
  5191. ' console.log("a");',
  5192. '}, function (x) {',
  5193. ' console.log("c");',
  5194. '});',
  5195. '']));
  5196. end;
  5197. procedure TTestModule.TestEnum_Name;
  5198. begin
  5199. StartProgram(false);
  5200. Add('type TMyEnum = (Red, Green, Blue);');
  5201. Add('var e: TMyEnum;');
  5202. Add('var f: TMyEnum = Blue;');
  5203. Add('begin');
  5204. Add(' e:=green;');
  5205. Add(' e:=default(TMyEnum);');
  5206. ConvertProgram;
  5207. CheckSource('TestEnum_Name',
  5208. LinesToStr([ // statements
  5209. 'this.TMyEnum = {',
  5210. ' "0":"Red",',
  5211. ' Red:0,',
  5212. ' "1":"Green",',
  5213. ' Green:1,',
  5214. ' "2":"Blue",',
  5215. ' Blue:2',
  5216. ' };',
  5217. 'this.e = 0;',
  5218. 'this.f = this.TMyEnum.Blue;'
  5219. ]),
  5220. LinesToStr([
  5221. '$mod.e=$mod.TMyEnum.Green;',
  5222. '$mod.e=$mod.TMyEnum.Red;'
  5223. ]));
  5224. end;
  5225. procedure TTestModule.TestEnum_Number;
  5226. begin
  5227. Converter.Options:=Converter.Options+[coEnumNumbers];
  5228. StartProgram(false);
  5229. Add('type TMyEnum = (Red, Green);');
  5230. Add('var');
  5231. Add(' e: TMyEnum;');
  5232. Add(' f: TMyEnum = Green;');
  5233. Add(' i: longint;');
  5234. Add('begin');
  5235. Add(' e:=green;');
  5236. Add(' i:=longint(e);');
  5237. ConvertProgram;
  5238. CheckSource('TestEnumNumber',
  5239. LinesToStr([ // statements
  5240. 'this.TMyEnum = {',
  5241. ' "0":"Red",',
  5242. ' Red:0,',
  5243. ' "1":"Green",',
  5244. ' Green:1',
  5245. ' };',
  5246. 'this.e = 0;',
  5247. 'this.f = 1;',
  5248. 'this.i = 0;'
  5249. ]),
  5250. LinesToStr([
  5251. '$mod.e=1;',
  5252. '$mod.i=$mod.e;'
  5253. ]));
  5254. end;
  5255. procedure TTestModule.TestEnum_ConstFail;
  5256. begin
  5257. StartProgram(false);
  5258. Add([
  5259. 'type TMyEnum = (Red = 100, Green = 101);',
  5260. 'var',
  5261. ' e: TMyEnum;',
  5262. ' f: TMyEnum = Green;',
  5263. 'begin',
  5264. ' e:=green;']);
  5265. SetExpectedPasResolverError('not yet implemented: Red:TPasEnumValue [20180126202434] "enum const"',3002);
  5266. ConvertProgram;
  5267. end;
  5268. procedure TTestModule.TestEnum_Functions;
  5269. begin
  5270. StartProgram(false);
  5271. Add([
  5272. 'type TMyEnum = (Red, Green);',
  5273. 'procedure DoIt(var e: TMyEnum; var i: word);',
  5274. 'var',
  5275. ' v: longint;',
  5276. ' s: string;',
  5277. 'begin',
  5278. ' val(s,e,v);',
  5279. ' val(s,e,i);',
  5280. 'end;',
  5281. 'var',
  5282. ' e: TMyEnum;',
  5283. ' i: longint;',
  5284. ' s: string;',
  5285. ' b: boolean;',
  5286. 'begin',
  5287. ' i:=ord(red);',
  5288. ' i:=ord(green);',
  5289. ' i:=ord(e);',
  5290. ' i:=ord(b);',
  5291. ' e:=low(tmyenum);',
  5292. ' e:=low(e);',
  5293. ' b:=low(boolean);',
  5294. ' e:=high(tmyenum);',
  5295. ' e:=high(e);',
  5296. ' b:=high(boolean);',
  5297. ' e:=pred(green);',
  5298. ' e:=pred(e);',
  5299. ' b:=pred(b);',
  5300. ' e:=succ(red);',
  5301. ' e:=succ(e);',
  5302. ' b:=succ(b);',
  5303. ' e:=tmyenum(1);',
  5304. ' e:=tmyenum(i);',
  5305. ' s:=str(e);',
  5306. ' str(e,s);',
  5307. ' str(red,s);',
  5308. ' s:=str(e:3);',
  5309. ' writestr(s,e:3,red);',
  5310. ' val(s,e,i);',
  5311. ' i:=longint(e);']);
  5312. ConvertProgram;
  5313. CheckSource('TestEnum_Functions',
  5314. LinesToStr([ // statements
  5315. 'this.TMyEnum = {',
  5316. ' "0":"Red",',
  5317. ' Red:0,',
  5318. ' "1":"Green",',
  5319. ' Green:1',
  5320. ' };',
  5321. 'this.DoIt = function (e, i) {',
  5322. ' var v = 0;',
  5323. ' var s = "";',
  5324. ' e.set(rtl.valEnum(s, $mod.TMyEnum, function (w) {',
  5325. ' v = w;',
  5326. ' }));',
  5327. ' e.set(rtl.valEnum(s, $mod.TMyEnum, i.set));',
  5328. '};',
  5329. 'this.e = 0;',
  5330. 'this.i = 0;',
  5331. 'this.s = "";',
  5332. 'this.b = false;',
  5333. '']),
  5334. LinesToStr([
  5335. '$mod.i=$mod.TMyEnum.Red;',
  5336. '$mod.i=$mod.TMyEnum.Green;',
  5337. '$mod.i=$mod.e;',
  5338. '$mod.i=$mod.b+0;',
  5339. '$mod.e=$mod.TMyEnum.Red;',
  5340. '$mod.e=$mod.TMyEnum.Red;',
  5341. '$mod.b=false;',
  5342. '$mod.e=$mod.TMyEnum.Green;',
  5343. '$mod.e=$mod.TMyEnum.Green;',
  5344. '$mod.b=true;',
  5345. '$mod.e=$mod.TMyEnum.Green-1;',
  5346. '$mod.e=$mod.e-1;',
  5347. '$mod.b=false;',
  5348. '$mod.e=$mod.TMyEnum.Red+1;',
  5349. '$mod.e=$mod.e+1;',
  5350. '$mod.b=true;',
  5351. '$mod.e=1;',
  5352. '$mod.e=$mod.i;',
  5353. '$mod.s = $mod.TMyEnum[$mod.e];',
  5354. '$mod.s = $mod.TMyEnum[$mod.e];',
  5355. '$mod.s = $mod.TMyEnum[$mod.TMyEnum.Red];',
  5356. '$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3);',
  5357. '$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3)+$mod.TMyEnum[$mod.TMyEnum.Red];',
  5358. '$mod.e = rtl.valEnum($mod.s, $mod.TMyEnum, function (v) {',
  5359. ' $mod.i = v;',
  5360. '});',
  5361. '$mod.i=$mod.e;',
  5362. '']));
  5363. end;
  5364. procedure TTestModule.TestEnumRg_Functions;
  5365. begin
  5366. StartProgram(false);
  5367. Add([
  5368. 'type',
  5369. ' TEnum = (Red, Green, Blue);',
  5370. ' TEnumRg = Green..Blue;',
  5371. 'procedure DoIt(var e: TEnumRg; var i: word);',
  5372. 'var',
  5373. ' v: longint;',
  5374. ' s: string;',
  5375. 'begin',
  5376. ' val(s,e,v);',
  5377. ' val(s,e,i);',
  5378. 'end;',
  5379. 'var',
  5380. ' e: TEnumRg;',
  5381. ' i: longint;',
  5382. ' s: string;',
  5383. 'begin',
  5384. ' i:=ord(green);',
  5385. ' i:=ord(e);',
  5386. ' e:=low(tenumrg);',
  5387. ' e:=low(e);',
  5388. ' e:=high(tenumrg);',
  5389. ' e:=high(e);',
  5390. ' e:=pred(blue);',
  5391. ' e:=pred(e);',
  5392. ' e:=succ(green);',
  5393. ' e:=succ(e);',
  5394. ' e:=tenumrg(1);',
  5395. ' e:=tenumrg(i);',
  5396. ' s:=str(e);',
  5397. ' str(e,s);',
  5398. ' str(red,s);',
  5399. ' s:=str(e:3);',
  5400. ' writestr(s,e:3,blue);',
  5401. ' val(s,e,i);',
  5402. ' i:=longint(e);']);
  5403. ConvertProgram;
  5404. CheckSource('TestEnumRg_Functions',
  5405. LinesToStr([ // statements
  5406. 'this.TEnum = {',
  5407. ' "0":"Red",',
  5408. ' Red:0,',
  5409. ' "1":"Green",',
  5410. ' Green:1,',
  5411. ' "2":"Blue",',
  5412. ' Blue:2',
  5413. ' };',
  5414. 'this.DoIt = function (e, i) {',
  5415. ' var v = 0;',
  5416. ' var s = "";',
  5417. ' e.set(rtl.valEnum(s, $mod.TEnum, function (w) {',
  5418. ' v = w;',
  5419. ' }));',
  5420. ' e.set(rtl.valEnum(s, $mod.TEnum, i.set));',
  5421. '};',
  5422. 'this.e = this.TEnum.Green;',
  5423. 'this.i = 0;',
  5424. 'this.s = "";',
  5425. '']),
  5426. LinesToStr([
  5427. '$mod.i=$mod.TEnum.Green;',
  5428. '$mod.i=$mod.e;',
  5429. '$mod.e=$mod.TEnum.Green;',
  5430. '$mod.e=$mod.TEnum.Green;',
  5431. '$mod.e=$mod.TEnum.Blue;',
  5432. '$mod.e=$mod.TEnum.Blue;',
  5433. '$mod.e=$mod.TEnum.Blue-1;',
  5434. '$mod.e=$mod.e-1;',
  5435. '$mod.e=$mod.TEnum.Green+1;',
  5436. '$mod.e=$mod.e+1;',
  5437. '$mod.e=1;',
  5438. '$mod.e=$mod.i;',
  5439. '$mod.s = $mod.TEnum[$mod.e];',
  5440. '$mod.s = $mod.TEnum[$mod.e];',
  5441. '$mod.s = $mod.TEnum[$mod.TEnum.Red];',
  5442. '$mod.s = rtl.spaceLeft($mod.TEnum[$mod.e], 3);',
  5443. '$mod.s = rtl.spaceLeft($mod.TEnum[$mod.e], 3)+$mod.TEnum[$mod.TEnum.Blue];',
  5444. '$mod.e = rtl.valEnum($mod.s, $mod.TEnum, function (v) {',
  5445. ' $mod.i = v;',
  5446. '});',
  5447. '$mod.i=$mod.e;',
  5448. '']));
  5449. end;
  5450. procedure TTestModule.TestEnum_AsParams;
  5451. begin
  5452. StartProgram(false);
  5453. Add('type TEnum = (Red,Blue);');
  5454. Add('procedure DoIt(vG: TEnum; const vH: TEnum; var vI: TEnum);');
  5455. Add('var vJ: TEnum;');
  5456. Add('begin');
  5457. Add(' vg:=vg;');
  5458. Add(' vj:=vh;');
  5459. Add(' vi:=vi;');
  5460. Add(' doit(vg,vg,vg);');
  5461. Add(' doit(vh,vh,vj);');
  5462. Add(' doit(vi,vi,vi);');
  5463. Add(' doit(vj,vj,vj);');
  5464. Add('end;');
  5465. Add('var i: TEnum;');
  5466. Add('begin');
  5467. Add(' doit(i,i,i);');
  5468. ConvertProgram;
  5469. CheckSource('TestEnum_AsParams',
  5470. LinesToStr([ // statements
  5471. 'this.TEnum = {',
  5472. ' "0": "Red",',
  5473. ' Red: 0,',
  5474. ' "1": "Blue",',
  5475. ' Blue: 1',
  5476. '};',
  5477. 'this.DoIt = function (vG,vH,vI) {',
  5478. ' var vJ = 0;',
  5479. ' vG = vG;',
  5480. ' vJ = vH;',
  5481. ' vI.set(vI.get());',
  5482. ' $mod.DoIt(vG, vG, {',
  5483. ' get: function () {',
  5484. ' return vG;',
  5485. ' },',
  5486. ' set: function (v) {',
  5487. ' vG = v;',
  5488. ' }',
  5489. ' });',
  5490. ' $mod.DoIt(vH, vH, {',
  5491. ' get: function () {',
  5492. ' return vJ;',
  5493. ' },',
  5494. ' set: function (v) {',
  5495. ' vJ = v;',
  5496. ' }',
  5497. ' });',
  5498. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  5499. ' $mod.DoIt(vJ, vJ, {',
  5500. ' get: function () {',
  5501. ' return vJ;',
  5502. ' },',
  5503. ' set: function (v) {',
  5504. ' vJ = v;',
  5505. ' }',
  5506. ' });',
  5507. '};',
  5508. 'this.i = 0;'
  5509. ]),
  5510. LinesToStr([
  5511. '$mod.DoIt($mod.i,$mod.i,{',
  5512. ' p: $mod,',
  5513. ' get: function () {',
  5514. ' return this.p.i;',
  5515. ' },',
  5516. ' set: function (v) {',
  5517. ' this.p.i = v;',
  5518. ' }',
  5519. '});'
  5520. ]));
  5521. end;
  5522. procedure TTestModule.TestEnumRange_Array;
  5523. begin
  5524. StartProgram(false);
  5525. Add([
  5526. 'type',
  5527. ' TEnum = (Red, Green, Blue);',
  5528. ' TEnumRg = green..blue;',
  5529. ' TArr = array[TEnumRg] of byte;',
  5530. ' TArr2 = array[green..blue] of byte;',
  5531. 'var',
  5532. ' a: TArr;',
  5533. ' b: TArr = (3,4);',
  5534. ' c: TArr2 = (5,6);',
  5535. 'begin',
  5536. ' a[green] := b[blue];',
  5537. ' c[green] := c[blue];',
  5538. '']);
  5539. ConvertProgram;
  5540. CheckSource('TestEnumRange_Array',
  5541. LinesToStr([ // statements
  5542. 'this.TEnum = {',
  5543. ' "0": "Red",',
  5544. ' Red: 0,',
  5545. ' "1": "Green",',
  5546. ' Green: 1,',
  5547. ' "2": "Blue",',
  5548. ' Blue: 2',
  5549. '};',
  5550. 'this.a = rtl.arraySetLength(null, 0, 2);',
  5551. 'this.b = [3, 4];',
  5552. 'this.c = [5, 6];',
  5553. '']),
  5554. LinesToStr([
  5555. ' $mod.a[$mod.TEnum.Green - 1] = $mod.b[$mod.TEnum.Blue - 1];',
  5556. ' $mod.c[$mod.TEnum.Green - 1] = $mod.c[$mod.TEnum.Blue - 1];',
  5557. '']));
  5558. end;
  5559. procedure TTestModule.TestEnum_ForIn;
  5560. begin
  5561. StartProgram(false);
  5562. Add([
  5563. 'type',
  5564. ' TEnum = (Red, Green, Blue);',
  5565. ' TEnumRg = green..blue;',
  5566. ' TArr = array[TEnum] of byte;',
  5567. ' TArrRg = array[TEnumRg] of byte;',
  5568. 'var',
  5569. ' e: TEnum;',
  5570. ' a1: TArr = (3,4,5);',
  5571. ' a2: TArrRg = (11,12);',
  5572. ' b: byte;',
  5573. 'begin',
  5574. ' for e in TEnum do ;',
  5575. ' for e in TEnumRg do ;',
  5576. ' for e in TArr do ;',
  5577. ' for e in TArrRg do ;',
  5578. ' for b in a1 do ;',
  5579. ' for b in a2 do ;',
  5580. '']);
  5581. ConvertProgram;
  5582. CheckSource('TestEnum_ForIn',
  5583. LinesToStr([ // statements
  5584. 'this.TEnum = {',
  5585. ' "0": "Red",',
  5586. ' Red: 0,',
  5587. ' "1": "Green",',
  5588. ' Green: 1,',
  5589. ' "2": "Blue",',
  5590. ' Blue: 2',
  5591. '};',
  5592. 'this.e = 0;',
  5593. 'this.a1 = [3, 4, 5];',
  5594. 'this.a2 = [11, 12];',
  5595. 'this.b = 0;',
  5596. '']),
  5597. LinesToStr([
  5598. ' for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  5599. ' for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  5600. ' for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  5601. ' for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  5602. ' for (var $in = $mod.a1, $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) $mod.b = $in[$l];',
  5603. ' for (var $in1 = $mod.a2, $l1 = 0, $end1 = rtl.length($in1) - 1; $l1 <= $end1; $l1++) $mod.b = $in1[$l1];',
  5604. '']));
  5605. end;
  5606. procedure TTestModule.TestEnum_ScopedNumber;
  5607. begin
  5608. Converter.Options:=Converter.Options+[coEnumNumbers];
  5609. StartProgram(false);
  5610. Add([
  5611. 'type',
  5612. ' TEnum = (Red, Green);',
  5613. 'var',
  5614. ' e: TEnum;',
  5615. 'begin',
  5616. ' e:=TEnum.Green;',
  5617. '']);
  5618. ConvertProgram;
  5619. CheckSource('TestEnum_ScopedNumber',
  5620. LinesToStr([ // statements
  5621. 'this.TEnum = {',
  5622. ' "0": "Red",',
  5623. ' Red: 0,',
  5624. ' "1": "Green",',
  5625. ' Green: 1',
  5626. '};',
  5627. 'this.e = 0;',
  5628. '']),
  5629. LinesToStr([
  5630. '$mod.e = 1;']));
  5631. end;
  5632. procedure TTestModule.TestEnum_InFunction;
  5633. begin
  5634. StartProgram(false);
  5635. Add([
  5636. 'const TEnum = 3;',
  5637. 'procedure DoIt;',
  5638. 'type',
  5639. ' TEnum = (Red, Green, Blue);',
  5640. ' procedure Sub;',
  5641. ' type',
  5642. ' TEnumSub = (Left, Right);',
  5643. ' var',
  5644. ' es: TEnumSub;',
  5645. ' begin',
  5646. ' es:=Left;',
  5647. ' end;',
  5648. 'var',
  5649. ' e, e2: TEnum;',
  5650. 'begin',
  5651. ' if e in [red,blue] then e2:=e;',
  5652. 'end;',
  5653. 'begin']);
  5654. ConvertProgram;
  5655. CheckSource('TestEnum_InFunction',
  5656. LinesToStr([ // statements
  5657. 'this.TEnum = 3;',
  5658. 'var TEnum$1 = {',
  5659. ' "0":"Red",',
  5660. ' Red:0,',
  5661. ' "1":"Green",',
  5662. ' Green:1,',
  5663. ' "2":"Blue",',
  5664. ' Blue:2',
  5665. ' };',
  5666. 'var TEnumSub = {',
  5667. ' "0": "Left",',
  5668. ' Left: 0,',
  5669. ' "1": "Right",',
  5670. ' Right: 1',
  5671. '};',
  5672. 'this.DoIt = function () {',
  5673. ' function Sub() {',
  5674. ' var es = 0;',
  5675. ' es = TEnumSub.Left;',
  5676. ' };',
  5677. ' var e = 0;',
  5678. ' var e2 = 0;',
  5679. ' if (e in rtl.createSet(TEnum$1.Red, TEnum$1.Blue)) e2 = e;',
  5680. '};',
  5681. '']),
  5682. LinesToStr([
  5683. '']));
  5684. end;
  5685. procedure TTestModule.TestEnum_Name_Anonymous_Unit;
  5686. begin
  5687. StartUnit(true);
  5688. Add([
  5689. 'interface',
  5690. 'var color: (red, green);',
  5691. 'implementation',
  5692. 'initialization',
  5693. ' color:=green;',
  5694. '']);
  5695. ConvertUnit;
  5696. CheckSource('TestEnum_Name_Anonymous_Unit',
  5697. LinesToStr([
  5698. 'this.color$a = {',
  5699. ' "0": "red",',
  5700. ' red: 0,',
  5701. ' "1": "green",',
  5702. ' green: 1',
  5703. '};',
  5704. 'this.color = 0;',
  5705. '']),
  5706. LinesToStr([ // this.$init
  5707. '$mod.color = $mod.color$a.green;',
  5708. '']),
  5709. LinesToStr([ // implementation
  5710. '']) );
  5711. end;
  5712. procedure TTestModule.TestSet_Enum;
  5713. begin
  5714. StartProgram(false);
  5715. Add([
  5716. 'type',
  5717. ' TColor = (Red, Green, Blue);',
  5718. ' TColors = set of TColor;',
  5719. 'var',
  5720. ' c: TColor;',
  5721. ' s: TColors;',
  5722. ' t: TColors = [];',
  5723. ' u: TColors = [Red];',
  5724. 'begin',
  5725. ' s:=[];',
  5726. ' s:=[Green];',
  5727. ' s:=[Green,Blue];',
  5728. ' s:=[Red..Blue];',
  5729. ' s:=[Red,Green..Blue];',
  5730. ' s:=[Red,c];',
  5731. ' s:=t;',
  5732. ' s:=default(TColors);',
  5733. '']);
  5734. ConvertProgram;
  5735. CheckSource('TestSet',
  5736. LinesToStr([ // statements
  5737. 'this.TColor = {',
  5738. ' "0":"Red",',
  5739. ' Red:0,',
  5740. ' "1":"Green",',
  5741. ' Green:1,',
  5742. ' "2":"Blue",',
  5743. ' Blue:2',
  5744. ' };',
  5745. 'this.c = 0;',
  5746. 'this.s = {};',
  5747. 'this.t = {};',
  5748. 'this.u = rtl.createSet(this.TColor.Red);'
  5749. ]),
  5750. LinesToStr([
  5751. '$mod.s={};',
  5752. '$mod.s=rtl.createSet($mod.TColor.Green);',
  5753. '$mod.s=rtl.createSet($mod.TColor.Green,$mod.TColor.Blue);',
  5754. '$mod.s=rtl.createSet(null,$mod.TColor.Red,$mod.TColor.Blue);',
  5755. '$mod.s=rtl.createSet($mod.TColor.Red,null,$mod.TColor.Green,$mod.TColor.Blue);',
  5756. '$mod.s=rtl.createSet($mod.TColor.Red,$mod.c);',
  5757. '$mod.s=rtl.refSet($mod.t);',
  5758. '$mod.s={};',
  5759. '']));
  5760. end;
  5761. procedure TTestModule.TestSet_Operators;
  5762. begin
  5763. StartProgram(false);
  5764. Add('type');
  5765. Add(' TColor = (Red, Green, Blue);');
  5766. Add(' TColors = set of tcolor;');
  5767. Add('var');
  5768. Add(' vC: TColor;');
  5769. Add(' vS: TColors;');
  5770. Add(' vT: TColors;');
  5771. Add(' vU: TColors;');
  5772. Add(' B: boolean;');
  5773. Add('begin');
  5774. Add(' include(vs,green);');
  5775. Add(' exclude(vs,vc);');
  5776. Add(' vs:=vt+vu;');
  5777. Add(' vs:=vt+[red];');
  5778. Add(' vs:=[red]+vt;');
  5779. Add(' vs:=[red]+[green];');
  5780. Add(' vs:=vt-vu;');
  5781. Add(' vs:=vt-[red];');
  5782. Add(' vs:=[red]-vt;');
  5783. Add(' vs:=[red]-[green];');
  5784. Add(' vs:=vt*vu;');
  5785. Add(' vs:=vt*[red];');
  5786. Add(' vs:=[red]*vt;');
  5787. Add(' vs:=[red]*[green];');
  5788. Add(' vs:=vt><vu;');
  5789. Add(' vs:=vt><[red];');
  5790. Add(' vs:=[red]><vt;');
  5791. Add(' vs:=[red]><[green];');
  5792. Add(' b:=vt=vu;');
  5793. Add(' b:=vt=[red];');
  5794. Add(' b:=[red]=vt;');
  5795. Add(' b:=[red]=[green];');
  5796. Add(' b:=vt<>vu;');
  5797. Add(' b:=vt<>[red];');
  5798. Add(' b:=[red]<>vt;');
  5799. Add(' b:=[red]<>[green];');
  5800. Add(' b:=vt<=vu;');
  5801. Add(' b:=vt<=[red];');
  5802. Add(' b:=[red]<=vt;');
  5803. Add(' b:=[red]<=[green];');
  5804. Add(' b:=vt>=vu;');
  5805. Add(' b:=vt>=[red];');
  5806. Add(' b:=[red]>=vt;');
  5807. Add(' b:=[red]>=[green];');
  5808. ConvertProgram;
  5809. CheckSource('TestSet_Operators',
  5810. LinesToStr([ // statements
  5811. 'this.TColor = {',
  5812. ' "0":"Red",',
  5813. ' Red:0,',
  5814. ' "1":"Green",',
  5815. ' Green:1,',
  5816. ' "2":"Blue",',
  5817. ' Blue:2',
  5818. ' };',
  5819. 'this.vC = 0;',
  5820. 'this.vS = {};',
  5821. 'this.vT = {};',
  5822. 'this.vU = {};',
  5823. 'this.B = false;'
  5824. ]),
  5825. LinesToStr([
  5826. '$mod.vS = rtl.includeSet($mod.vS,$mod.TColor.Green);',
  5827. '$mod.vS = rtl.excludeSet($mod.vS,$mod.vC);',
  5828. '$mod.vS = rtl.unionSet($mod.vT, $mod.vU);',
  5829. '$mod.vS = rtl.unionSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5830. '$mod.vS = rtl.unionSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5831. '$mod.vS = rtl.unionSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5832. '$mod.vS = rtl.diffSet($mod.vT, $mod.vU);',
  5833. '$mod.vS = rtl.diffSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5834. '$mod.vS = rtl.diffSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5835. '$mod.vS = rtl.diffSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5836. '$mod.vS = rtl.intersectSet($mod.vT, $mod.vU);',
  5837. '$mod.vS = rtl.intersectSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5838. '$mod.vS = rtl.intersectSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5839. '$mod.vS = rtl.intersectSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5840. '$mod.vS = rtl.symDiffSet($mod.vT, $mod.vU);',
  5841. '$mod.vS = rtl.symDiffSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5842. '$mod.vS = rtl.symDiffSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5843. '$mod.vS = rtl.symDiffSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5844. '$mod.B = rtl.eqSet($mod.vT, $mod.vU);',
  5845. '$mod.B = rtl.eqSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5846. '$mod.B = rtl.eqSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5847. '$mod.B = rtl.eqSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5848. '$mod.B = rtl.neSet($mod.vT, $mod.vU);',
  5849. '$mod.B = rtl.neSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5850. '$mod.B = rtl.neSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5851. '$mod.B = rtl.neSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5852. '$mod.B = rtl.leSet($mod.vT, $mod.vU);',
  5853. '$mod.B = rtl.leSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5854. '$mod.B = rtl.leSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5855. '$mod.B = rtl.leSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5856. '$mod.B = rtl.geSet($mod.vT, $mod.vU);',
  5857. '$mod.B = rtl.geSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5858. '$mod.B = rtl.geSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5859. '$mod.B = rtl.geSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5860. '']));
  5861. end;
  5862. procedure TTestModule.TestSet_Operator_In;
  5863. begin
  5864. StartProgram(false);
  5865. Add([
  5866. 'type',
  5867. ' TColor = (Red, Green, Blue);',
  5868. ' TColors = set of tcolor;',
  5869. ' TColorRg = green..blue;',
  5870. 'var',
  5871. ' vC: tcolor;',
  5872. ' vT: tcolors;',
  5873. ' B: boolean;',
  5874. ' rg: TColorRg;',
  5875. 'begin',
  5876. ' b:=red in vt;',
  5877. ' b:=vc in vt;',
  5878. ' b:=green in [red..blue];',
  5879. ' b:=vc in [red..blue];',
  5880. ' ',
  5881. ' if red in vt then ;',
  5882. ' while vC in vt do ;',
  5883. ' repeat',
  5884. ' until vC in vt;',
  5885. ' if rg in [green..blue] then ;',
  5886. '']);
  5887. ConvertProgram;
  5888. CheckSource('TestSet_Operator_In',
  5889. LinesToStr([ // statements
  5890. 'this.TColor = {',
  5891. ' "0":"Red",',
  5892. ' Red:0,',
  5893. ' "1":"Green",',
  5894. ' Green:1,',
  5895. ' "2":"Blue",',
  5896. ' Blue:2',
  5897. ' };',
  5898. 'this.vC = 0;',
  5899. 'this.vT = {};',
  5900. 'this.B = false;',
  5901. 'this.rg = this.TColor.Green;',
  5902. '']),
  5903. LinesToStr([
  5904. '$mod.B = $mod.TColor.Red in $mod.vT;',
  5905. '$mod.B = $mod.vC in $mod.vT;',
  5906. '$mod.B = $mod.TColor.Green in rtl.createSet(null, $mod.TColor.Red, $mod.TColor.Blue);',
  5907. '$mod.B = $mod.vC in rtl.createSet(null, $mod.TColor.Red, $mod.TColor.Blue);',
  5908. 'if ($mod.TColor.Red in $mod.vT) ;',
  5909. 'while ($mod.vC in $mod.vT) {',
  5910. '};',
  5911. 'do {',
  5912. '} while (!($mod.vC in $mod.vT));',
  5913. 'if ($mod.rg in rtl.createSet(null, $mod.TColor.Green, $mod.TColor.Blue)) ;',
  5914. '']));
  5915. end;
  5916. procedure TTestModule.TestSet_Functions;
  5917. begin
  5918. StartProgram(false);
  5919. Add('type');
  5920. Add(' TMyEnum = (Red, Green);');
  5921. Add(' TMyEnums = set of TMyEnum;');
  5922. Add('var');
  5923. Add(' e: TMyEnum;');
  5924. Add(' s: TMyEnums;');
  5925. Add('begin');
  5926. Add(' e:=Low(TMyEnums);');
  5927. Add(' e:=Low(s);');
  5928. Add(' e:=High(TMyEnums);');
  5929. Add(' e:=High(s);');
  5930. ConvertProgram;
  5931. CheckSource('TestSetFunctions',
  5932. LinesToStr([ // statements
  5933. 'this.TMyEnum = {',
  5934. ' "0":"Red",',
  5935. ' Red:0,',
  5936. ' "1":"Green",',
  5937. ' Green:1',
  5938. ' };',
  5939. 'this.e = 0;',
  5940. 'this.s = {};'
  5941. ]),
  5942. LinesToStr([
  5943. '$mod.e=$mod.TMyEnum.Red;',
  5944. '$mod.e=$mod.TMyEnum.Red;',
  5945. '$mod.e=$mod.TMyEnum.Green;',
  5946. '$mod.e=$mod.TMyEnum.Green;',
  5947. '']));
  5948. end;
  5949. procedure TTestModule.TestSet_PassAsArgClone;
  5950. begin
  5951. StartProgram(false);
  5952. Add('type');
  5953. Add(' TMyEnum = (Red, Green);');
  5954. Add(' TMyEnums = set of TMyEnum;');
  5955. Add('procedure DoDefault(s: tmyenums); begin end;');
  5956. Add('procedure DoConst(const s: tmyenums); begin end;');
  5957. Add('var');
  5958. Add(' aSet: tmyenums;');
  5959. Add('begin');
  5960. Add(' dodefault(aset);');
  5961. Add(' doconst(aset);');
  5962. ConvertProgram;
  5963. CheckSource('TestSetFunctions',
  5964. LinesToStr([ // statements
  5965. 'this.TMyEnum = {',
  5966. ' "0":"Red",',
  5967. ' Red:0,',
  5968. ' "1":"Green",',
  5969. ' Green:1',
  5970. ' };',
  5971. 'this.DoDefault = function (s) {',
  5972. '};',
  5973. 'this.DoConst = function (s) {',
  5974. '};',
  5975. 'this.aSet = {};'
  5976. ]),
  5977. LinesToStr([
  5978. '$mod.DoDefault(rtl.refSet($mod.aSet));',
  5979. '$mod.DoConst($mod.aSet);',
  5980. '']));
  5981. end;
  5982. procedure TTestModule.TestSet_AsParams;
  5983. begin
  5984. StartProgram(false);
  5985. Add([
  5986. 'type TEnum = (Red,Blue);',
  5987. 'type TEnums = set of TEnum;',
  5988. 'function DoIt(vG: TEnums; const vH: TEnums; var vI: TEnums): TEnums;',
  5989. 'var vJ: TEnums;',
  5990. 'begin',
  5991. ' Include(vg,red);',
  5992. ' Include(result,blue);',
  5993. ' vg:=vg;',
  5994. ' vj:=vh;',
  5995. ' vi:=vi;',
  5996. ' doit(vg,vg,vg);',
  5997. ' doit(vh,vh,vj);',
  5998. ' doit(vi,vi,vi);',
  5999. ' doit(vj,vj,vj);',
  6000. 'end;',
  6001. 'var i: TEnums;',
  6002. 'begin',
  6003. ' doit(i,i,i);']);
  6004. ConvertProgram;
  6005. CheckSource('TestSet_AsParams',
  6006. LinesToStr([ // statements
  6007. 'this.TEnum = {',
  6008. ' "0": "Red",',
  6009. ' Red: 0,',
  6010. ' "1": "Blue",',
  6011. ' Blue: 1',
  6012. '};',
  6013. 'this.DoIt = function (vG,vH,vI) {',
  6014. ' var Result = {};',
  6015. ' var vJ = {};',
  6016. ' vG = rtl.includeSet(vG, $mod.TEnum.Red);',
  6017. ' Result = rtl.includeSet(Result, $mod.TEnum.Blue);',
  6018. ' vG = rtl.refSet(vG);',
  6019. ' vJ = rtl.refSet(vH);',
  6020. ' vI.set(rtl.refSet(vI.get()));',
  6021. ' $mod.DoIt(rtl.refSet(vG), vG, {',
  6022. ' get: function () {',
  6023. ' return vG;',
  6024. ' },',
  6025. ' set: function (v) {',
  6026. ' vG = v;',
  6027. ' }',
  6028. ' });',
  6029. ' $mod.DoIt(rtl.refSet(vH), vH, {',
  6030. ' get: function () {',
  6031. ' return vJ;',
  6032. ' },',
  6033. ' set: function (v) {',
  6034. ' vJ = v;',
  6035. ' }',
  6036. ' });',
  6037. ' $mod.DoIt(rtl.refSet(vI.get()), vI.get(), vI);',
  6038. ' $mod.DoIt(rtl.refSet(vJ), vJ, {',
  6039. ' get: function () {',
  6040. ' return vJ;',
  6041. ' },',
  6042. ' set: function (v) {',
  6043. ' vJ = v;',
  6044. ' }',
  6045. ' });',
  6046. ' return Result;',
  6047. '};',
  6048. 'this.i = {};'
  6049. ]),
  6050. LinesToStr([
  6051. '$mod.DoIt(rtl.refSet($mod.i),$mod.i,{',
  6052. ' p: $mod,',
  6053. ' get: function () {',
  6054. ' return this.p.i;',
  6055. ' },',
  6056. ' set: function (v) {',
  6057. ' this.p.i = v;',
  6058. ' }',
  6059. '});'
  6060. ]));
  6061. end;
  6062. procedure TTestModule.TestSet_Property;
  6063. begin
  6064. StartProgram(false);
  6065. Add('type');
  6066. Add(' TEnum = (Red,Blue);');
  6067. Add(' TEnums = set of TEnum;');
  6068. Add(' TObject = class');
  6069. Add(' function GetColors: TEnums; external name ''GetColors'';');
  6070. Add(' procedure SetColors(const Value: TEnums); external name ''SetColors'';');
  6071. Add(' property Colors: TEnums read GetColors write SetColors;');
  6072. Add(' end;');
  6073. Add('procedure DoIt(i: TEnums; const j: TEnums; var k: TEnums; out l: TEnums);');
  6074. Add('begin end;');
  6075. Add('var Obj: TObject;');
  6076. Add('begin');
  6077. Add(' Include(Obj.Colors,Red);');
  6078. Add(' Exclude(Obj.Colors,Red);');
  6079. //Add(' DoIt(Obj.Colors,Obj.Colors,Obj.Colors,Obj.Colors);');
  6080. ConvertProgram;
  6081. CheckSource('TestSet_Property',
  6082. LinesToStr([ // statements
  6083. 'this.TEnum = {',
  6084. ' "0": "Red",',
  6085. ' Red: 0,',
  6086. ' "1": "Blue",',
  6087. ' Blue: 1',
  6088. '};',
  6089. 'rtl.createClass(this, "TObject", null, function () {',
  6090. ' this.$init = function () {',
  6091. ' };',
  6092. ' this.$final = function () {',
  6093. ' };',
  6094. '});',
  6095. 'this.DoIt = function (i, j, k, l) {',
  6096. '};',
  6097. 'this.Obj = null;',
  6098. '']),
  6099. LinesToStr([
  6100. '$mod.Obj.SetColors(rtl.includeSet($mod.Obj.GetColors(), $mod.TEnum.Red));',
  6101. '$mod.Obj.SetColors(rtl.excludeSet($mod.Obj.GetColors(), $mod.TEnum.Red));',
  6102. '']));
  6103. end;
  6104. procedure TTestModule.TestSet_EnumConst;
  6105. begin
  6106. StartProgram(false);
  6107. Add([
  6108. 'type',
  6109. ' TEnum = (Red,Blue);',
  6110. ' TEnums = set of TEnum;',
  6111. 'const',
  6112. ' Orange = red;',
  6113. 'var',
  6114. ' Enum: tenum;',
  6115. ' Enums: tenums;',
  6116. 'begin',
  6117. ' Include(enums,orange);',
  6118. ' Exclude(enums,orange);',
  6119. ' if orange in enums then;',
  6120. ' if orange in [orange,red] then;']);
  6121. ConvertProgram;
  6122. CheckSource('TestSet_EnumConst',
  6123. LinesToStr([ // statements
  6124. 'this.TEnum = {',
  6125. ' "0": "Red",',
  6126. ' Red: 0,',
  6127. ' "1": "Blue",',
  6128. ' Blue: 1',
  6129. '};',
  6130. 'this.Orange = this.TEnum.Red;',
  6131. 'this.Enum = 0;',
  6132. 'this.Enums = {};',
  6133. '']),
  6134. LinesToStr([
  6135. '$mod.Enums = rtl.includeSet($mod.Enums, $mod.TEnum.Red);',
  6136. '$mod.Enums = rtl.excludeSet($mod.Enums, $mod.TEnum.Red);',
  6137. 'if ($mod.TEnum.Red in $mod.Enums) ;',
  6138. 'if ($mod.TEnum.Red in rtl.createSet($mod.TEnum.Red, $mod.TEnum.Red)) ;',
  6139. '']));
  6140. end;
  6141. procedure TTestModule.TestSet_IntConst;
  6142. begin
  6143. StartProgram(false);
  6144. Add([
  6145. 'type',
  6146. ' TEnums = set of Byte;',
  6147. 'const',
  6148. ' Orange = 0;',
  6149. 'var',
  6150. ' Enum: byte;',
  6151. ' Enums: tenums;',
  6152. 'begin',
  6153. ' Enums:=[];',
  6154. ' Enums:=[0];',
  6155. ' Enums:=[1..2];',
  6156. //' Include(enums,orange);',
  6157. //' Exclude(enums,orange);',
  6158. ' if orange in enums then;',
  6159. ' if orange in [orange,1] then;']);
  6160. ConvertProgram;
  6161. CheckSource('TestSet_IntConst',
  6162. LinesToStr([ // statements
  6163. 'this.Orange = 0;',
  6164. 'this.Enum = 0;',
  6165. 'this.Enums = {};',
  6166. '']),
  6167. LinesToStr([
  6168. '$mod.Enums = {};',
  6169. '$mod.Enums = rtl.createSet(0);',
  6170. '$mod.Enums = rtl.createSet(null, 1, 2);',
  6171. 'if (0 in $mod.Enums) ;',
  6172. 'if (0 in rtl.createSet(0, 1)) ;',
  6173. '']));
  6174. end;
  6175. procedure TTestModule.TestSet_IntRange;
  6176. begin
  6177. StartProgram(false);
  6178. Add([
  6179. 'type',
  6180. ' TRange = 1..3;',
  6181. ' TEnums = set of TRange;',
  6182. 'const',
  6183. ' Orange = 2;',
  6184. 'var',
  6185. ' Enum: byte;',
  6186. ' Enums: TEnums;',
  6187. 'begin',
  6188. ' Enums:=[];',
  6189. ' Enums:=[1];',
  6190. ' Enums:=[2..3];',
  6191. ' Include(enums,orange);',
  6192. ' Exclude(enums,orange);',
  6193. ' if orange in enums then;',
  6194. ' if orange in [orange,1] then;']);
  6195. ConvertProgram;
  6196. CheckSource('TestSet_IntRange',
  6197. LinesToStr([ // statements
  6198. 'this.Orange = 2;',
  6199. 'this.Enum = 0;',
  6200. 'this.Enums = {};',
  6201. '']),
  6202. LinesToStr([
  6203. '$mod.Enums = {};',
  6204. '$mod.Enums = rtl.createSet(1);',
  6205. '$mod.Enums = rtl.createSet(null, 2, 3);',
  6206. '$mod.Enums = rtl.includeSet($mod.Enums, 2);',
  6207. '$mod.Enums = rtl.excludeSet($mod.Enums, 2);',
  6208. 'if (2 in $mod.Enums) ;',
  6209. 'if (2 in rtl.createSet(2, 1)) ;',
  6210. '']));
  6211. end;
  6212. procedure TTestModule.TestSet_AnonymousEnumType;
  6213. begin
  6214. StartProgram(false);
  6215. Add('type');
  6216. Add(' TFlags = set of (red, green);');
  6217. Add('const');
  6218. Add(' favorite = red;');
  6219. Add('var');
  6220. Add(' f: TFlags;');
  6221. Add(' i: longint;');
  6222. Add('begin');
  6223. Add(' Include(f,red);');
  6224. Add(' Include(f,favorite);');
  6225. Add(' i:=ord(red);');
  6226. Add(' i:=ord(favorite);');
  6227. Add(' i:=ord(low(TFlags));');
  6228. Add(' i:=ord(low(f));');
  6229. Add(' i:=ord(low(favorite));');
  6230. Add(' i:=ord(high(TFlags));');
  6231. Add(' i:=ord(high(f));');
  6232. Add(' i:=ord(high(favorite));');
  6233. Add(' f:=[green,favorite];');
  6234. ConvertProgram;
  6235. CheckSource('TestSet_AnonymousEnumType',
  6236. LinesToStr([ // statements
  6237. 'this.TFlags$a = {',
  6238. ' "0": "red",',
  6239. ' red: 0,',
  6240. ' "1": "green",',
  6241. ' green: 1',
  6242. '};',
  6243. 'this.favorite = this.TFlags$a.red;',
  6244. 'this.f = {};',
  6245. 'this.i = 0;',
  6246. '']),
  6247. LinesToStr([
  6248. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  6249. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  6250. '$mod.i = $mod.TFlags$a.red;',
  6251. '$mod.i = $mod.TFlags$a.red;',
  6252. '$mod.i = $mod.TFlags$a.red;',
  6253. '$mod.i = $mod.TFlags$a.red;',
  6254. '$mod.i = $mod.TFlags$a.red;',
  6255. '$mod.i = $mod.TFlags$a.green;',
  6256. '$mod.i = $mod.TFlags$a.green;',
  6257. '$mod.i = $mod.TFlags$a.green;',
  6258. '$mod.f = rtl.createSet($mod.TFlags$a.green, $mod.TFlags$a.red);',
  6259. '']));
  6260. end;
  6261. procedure TTestModule.TestSet_AnonymousEnumTypeChar;
  6262. begin
  6263. exit;
  6264. StartProgram(false);
  6265. Add([
  6266. 'type',
  6267. ' TAtoZ = ''A''..''Z'';',
  6268. ' TSetOfAZ = set of TAtoZ;',
  6269. 'var',
  6270. ' c: char;',
  6271. ' a: TAtoZ;',
  6272. ' s: TSetOfAZ = [''P'',''A''];',
  6273. ' i: longint;',
  6274. 'begin',
  6275. ' Include(s,''S'');',
  6276. ' Include(s,c);',
  6277. ' Include(s,a);',
  6278. ' c:=low(TAtoZ);',
  6279. ' i:=ord(low(TAtoZ));',
  6280. ' a:=high(TAtoZ);',
  6281. ' a:=high(TSetOfAtoZ);',
  6282. ' s:=[a,c,''M''];',
  6283. '']);
  6284. ConvertProgram;
  6285. CheckSource('TestSet_AnonymousEnumTypeChar',
  6286. LinesToStr([ // statements
  6287. '']),
  6288. LinesToStr([
  6289. '']));
  6290. end;
  6291. procedure TTestModule.TestSet_ConstEnum;
  6292. begin
  6293. StartProgram(false);
  6294. Add([
  6295. 'type',
  6296. ' TEnum = (red,blue,green);',
  6297. ' TEnums = set of TEnum;',
  6298. 'const',
  6299. ' teAny = [low(TEnum)..high(TEnum)];',
  6300. ' teRedBlue = [low(TEnum)..pred(high(TEnum))];',
  6301. 'var',
  6302. ' e: TEnum;',
  6303. ' s: TEnums;',
  6304. 'begin',
  6305. ' if blue in teAny then;',
  6306. ' if blue in teAny+[e] then;',
  6307. ' if blue in teAny+teRedBlue then;',
  6308. ' if e in [red,blue] then;',
  6309. ' s:=teAny;',
  6310. ' s:=teAny+[e];',
  6311. ' s:=[e]+teAny;',
  6312. ' s:=teAny+teRedBlue;',
  6313. ' s:=teAny+teRedBlue+[e];',
  6314. '']);
  6315. ConvertProgram;
  6316. CheckSource('TestSet_ConstEnum',
  6317. LinesToStr([ // statements
  6318. 'this.TEnum = {',
  6319. ' "0": "red",',
  6320. ' red: 0,',
  6321. ' "1": "blue",',
  6322. ' blue: 1,',
  6323. ' "2": "green",',
  6324. ' green: 2',
  6325. '};',
  6326. 'this.teAny = rtl.createSet(null, this.TEnum.red, this.TEnum.green);',
  6327. 'this.teRedBlue = rtl.createSet(null, this.TEnum.red, this.TEnum.green - 1);',
  6328. 'this.e = 0;',
  6329. 'this.s = {};',
  6330. '']),
  6331. LinesToStr([
  6332. 'if ($mod.TEnum.blue in $mod.teAny) ;',
  6333. 'if ($mod.TEnum.blue in rtl.unionSet($mod.teAny, rtl.createSet($mod.e))) ;',
  6334. 'if ($mod.TEnum.blue in rtl.unionSet($mod.teAny, $mod.teRedBlue)) ;',
  6335. 'if ($mod.e in rtl.createSet($mod.TEnum.red, $mod.TEnum.blue)) ;',
  6336. '$mod.s = rtl.refSet($mod.teAny);',
  6337. '$mod.s = rtl.unionSet($mod.teAny, rtl.createSet($mod.e));',
  6338. '$mod.s = rtl.unionSet(rtl.createSet($mod.e), $mod.teAny);',
  6339. '$mod.s = rtl.unionSet($mod.teAny, $mod.teRedBlue);',
  6340. '$mod.s = rtl.unionSet(rtl.unionSet($mod.teAny, $mod.teRedBlue), rtl.createSet($mod.e));',
  6341. '']));
  6342. end;
  6343. procedure TTestModule.TestSet_ConstChar;
  6344. begin
  6345. StartProgram(false);
  6346. Add([
  6347. 'const',
  6348. ' LowChars = [''a''..''z''];',
  6349. ' Chars = LowChars+[''A''..''Z''];',
  6350. ' sc = [''А'', ''Я''];',
  6351. 'var',
  6352. ' c: char;',
  6353. ' s: string;',
  6354. 'begin',
  6355. ' if c in lowchars then ;',
  6356. ' if ''a'' in lowchars then ;',
  6357. ' if s[1] in lowchars then ;',
  6358. ' if c in chars then ;',
  6359. ' if c in [''a''..''z'',''_''] then ;',
  6360. ' if ''b'' in [''a''..''z'',''_''] then ;',
  6361. ' if ''Я'' in sc then ;',
  6362. ' if 3=ord('' '') then ;',
  6363. '']);
  6364. ConvertProgram;
  6365. CheckSource('TestSet_ConstChar',
  6366. LinesToStr([ // statements
  6367. 'this.LowChars = rtl.createSet(null, 97, 122);',
  6368. 'this.Chars = rtl.unionSet(this.LowChars, rtl.createSet(null, 65, 90));',
  6369. 'this.sc = rtl.createSet(1040, 1071);',
  6370. 'this.c = "";',
  6371. 'this.s = "";',
  6372. '']),
  6373. LinesToStr([
  6374. 'if ($mod.c.charCodeAt() in $mod.LowChars) ;',
  6375. 'if (97 in $mod.LowChars) ;',
  6376. 'if ($mod.s.charCodeAt(0) in $mod.LowChars) ;',
  6377. 'if ($mod.c.charCodeAt() in $mod.Chars) ;',
  6378. 'if ($mod.c.charCodeAt() in rtl.createSet(null, 97, 122, 95)) ;',
  6379. 'if (98 in rtl.createSet(null, 97, 122, 95)) ;',
  6380. 'if (1071 in $mod.sc) ;',
  6381. 'if (3 === 32) ;',
  6382. '']));
  6383. end;
  6384. procedure TTestModule.TestSet_ConstInt;
  6385. begin
  6386. StartProgram(false);
  6387. Add([
  6388. 'const',
  6389. ' Months = [1..12];',
  6390. ' Mirror = [-12..-1]+Months;',
  6391. 'var',
  6392. ' i: smallint;',
  6393. 'begin',
  6394. ' if 3 in Months then;',
  6395. ' if i in Months+[i] then;',
  6396. ' if i in Months+Mirror then;',
  6397. ' if i in [4..6,8] then;',
  6398. '']);
  6399. ConvertProgram;
  6400. CheckSource('TestSet_ConstInt',
  6401. LinesToStr([ // statements
  6402. 'this.Months = rtl.createSet(null, 1, 12);',
  6403. 'this.Mirror = rtl.unionSet(rtl.createSet(null, -12, -1), this.Months);',
  6404. 'this.i = 0;',
  6405. '']),
  6406. LinesToStr([
  6407. 'if (3 in $mod.Months) ;',
  6408. 'if ($mod.i in rtl.unionSet($mod.Months, rtl.createSet($mod.i))) ;',
  6409. 'if ($mod.i in rtl.unionSet($mod.Months, $mod.Mirror)) ;',
  6410. 'if ($mod.i in rtl.createSet(null, 4, 6, 8)) ;',
  6411. '']));
  6412. end;
  6413. procedure TTestModule.TestSet_InFunction;
  6414. begin
  6415. StartProgram(false);
  6416. Add([
  6417. 'const',
  6418. ' TEnum = 3;',
  6419. ' TSetOfEnum = 4;',
  6420. ' TSetOfAno = 5;',
  6421. 'procedure DoIt;',
  6422. 'type',
  6423. ' TEnum = (red, blue);',
  6424. ' TSetOfEnum = set of TEnum;',
  6425. ' TSetOfAno = set of (up,down);',
  6426. 'var',
  6427. ' e: TEnum;',
  6428. ' se: TSetOfEnum;',
  6429. ' sa: TSetOfAno;',
  6430. 'begin',
  6431. ' se:=[e];',
  6432. ' sa:=[up];',
  6433. 'end;',
  6434. 'begin',
  6435. '']);
  6436. ConvertProgram;
  6437. CheckSource('TestSet_InFunction',
  6438. LinesToStr([ // statements
  6439. 'this.TEnum = 3;',
  6440. 'this.TSetOfEnum = 4;',
  6441. 'this.TSetOfAno = 5;',
  6442. 'var TEnum$1 = {',
  6443. ' "0": "red",',
  6444. ' red: 0,',
  6445. ' "1": "blue",',
  6446. ' blue: 1',
  6447. '};',
  6448. 'var TSetOfAno$a = {',
  6449. ' "0": "up",',
  6450. ' up: 0,',
  6451. ' "1": "down",',
  6452. ' down: 1',
  6453. '};',
  6454. 'this.DoIt = function () {',
  6455. ' var e = 0;',
  6456. ' var se = {};',
  6457. ' var sa = {};',
  6458. ' se = rtl.createSet(e);',
  6459. ' sa = rtl.createSet(TSetOfAno$a.up);',
  6460. '};',
  6461. '']),
  6462. LinesToStr([
  6463. '']));
  6464. end;
  6465. procedure TTestModule.TestSet_ForIn;
  6466. begin
  6467. StartProgram(false);
  6468. Add([
  6469. 'type',
  6470. ' TEnum = (Red, Green, Blue);',
  6471. ' TEnumRg = green..blue;',
  6472. ' TSetOfEnum = set of TEnum;',
  6473. ' TSetOfEnumRg = set of TEnumRg;',
  6474. 'var',
  6475. ' e, e2: TEnum;',
  6476. ' er: TEnum;',
  6477. ' s: TSetOfEnum;',
  6478. 'begin',
  6479. ' for e in TSetOfEnum do ;',
  6480. ' for e in TSetOfEnumRg do ;',
  6481. ' for e in [] do e2:=e;',
  6482. ' for e in [red..green] do e2:=e;',
  6483. ' for e in [green,blue] do e2:=e;',
  6484. ' for e in [red,blue] do e2:=e;',
  6485. ' for e in s do e2:=e;',
  6486. ' for er in TSetOfEnumRg do ;',
  6487. '']);
  6488. ConvertProgram;
  6489. CheckSource('TestSet_ForIn',
  6490. LinesToStr([ // statements
  6491. 'this.TEnum = {',
  6492. ' "0":"Red",',
  6493. ' Red:0,',
  6494. ' "1":"Green",',
  6495. ' Green:1,',
  6496. ' "2":"Blue",',
  6497. ' Blue:2',
  6498. ' };',
  6499. 'this.e = 0;',
  6500. 'this.e2 = 0;',
  6501. 'this.er = 0;',
  6502. 'this.s = {};',
  6503. '']),
  6504. LinesToStr([
  6505. 'for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  6506. 'for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  6507. 'for ($mod.e = 0; $mod.e <= 1; $mod.e++) $mod.e2 = $mod.e;',
  6508. 'for ($mod.e = 1; $mod.e <= 2; $mod.e++) $mod.e2 = $mod.e;',
  6509. 'for ($mod.e in rtl.createSet($mod.TEnum.Red, $mod.TEnum.Blue)) $mod.e2 = $mod.e;',
  6510. 'for (var $l in $mod.s){',
  6511. ' $mod.e = +$l;',
  6512. ' $mod.e2 = $mod.e;',
  6513. '};',
  6514. 'for ($mod.er = 1; $mod.er <= 2; $mod.er++) ;',
  6515. '']));
  6516. end;
  6517. procedure TTestModule.TestNestBegin;
  6518. begin
  6519. StartProgram(false);
  6520. Add('begin');
  6521. Add(' begin');
  6522. Add(' begin');
  6523. Add(' end;');
  6524. Add(' begin');
  6525. Add(' if true then ;');
  6526. Add(' end;');
  6527. Add(' end;');
  6528. ConvertProgram;
  6529. CheckSource('TestNestBegin',
  6530. '',
  6531. 'if (true) ;');
  6532. end;
  6533. procedure TTestModule.TestUnitImplVars;
  6534. begin
  6535. StartUnit(false);
  6536. Add('interface');
  6537. Add('implementation');
  6538. Add('var');
  6539. Add(' V1:longint;');
  6540. Add(' V2:longint = 3;');
  6541. Add(' V3:string = ''abc'';');
  6542. ConvertUnit;
  6543. CheckSource('TestUnitImplVars',
  6544. LinesToStr([ // statements
  6545. 'var $impl = $mod.$impl;',
  6546. '']),
  6547. '', // this.$init
  6548. LinesToStr([ // implementation
  6549. '$impl.V1 = 0;',
  6550. '$impl.V2 = 3;',
  6551. '$impl.V3 = "abc";',
  6552. '']) );
  6553. end;
  6554. procedure TTestModule.TestUnitImplConsts;
  6555. begin
  6556. StartUnit(false);
  6557. Add('interface');
  6558. Add('implementation');
  6559. Add('const');
  6560. Add(' v1 = 3;');
  6561. Add(' v2:longint = 4;');
  6562. Add(' v3:string = ''abc'';');
  6563. ConvertUnit;
  6564. CheckSource('TestUnitImplConsts',
  6565. LinesToStr([ // statements
  6566. 'var $impl = $mod.$impl;',
  6567. '']),
  6568. '', // this.$init
  6569. LinesToStr([ // implementation
  6570. '$impl.v1 = 3;',
  6571. '$impl.v2 = 4;',
  6572. '$impl.v3 = "abc";',
  6573. '']) );
  6574. end;
  6575. procedure TTestModule.TestUnitImplRecord;
  6576. begin
  6577. StartUnit(false);
  6578. Add('interface');
  6579. Add('implementation');
  6580. Add('type');
  6581. Add(' TMyRecord = record');
  6582. Add(' i: longint;');
  6583. Add(' end;');
  6584. Add('var aRec: TMyRecord;');
  6585. Add('initialization');
  6586. Add(' arec.i:=3;');
  6587. ConvertUnit;
  6588. CheckSource('TestUnitImplRecord',
  6589. LinesToStr([ // statements
  6590. 'var $impl = $mod.$impl;',
  6591. '']),
  6592. // this.$init
  6593. '$impl.aRec.i = 3;',
  6594. LinesToStr([ // implementation
  6595. 'rtl.recNewT($impl, "TMyRecord", function () {',
  6596. ' this.i = 0;',
  6597. ' this.$eq = function (b) {',
  6598. ' return this.i === b.i;',
  6599. ' };',
  6600. ' this.$assign = function (s) {',
  6601. ' this.i = s.i;',
  6602. ' return this;',
  6603. ' };',
  6604. '});',
  6605. '$impl.aRec = $impl.TMyRecord.$new();',
  6606. '']) );
  6607. end;
  6608. procedure TTestModule.TestRenameJSNameConflict;
  6609. begin
  6610. StartProgram(false);
  6611. Add('var apply: longint;');
  6612. Add('var bind: longint;');
  6613. Add('var call: longint;');
  6614. Add('begin');
  6615. ConvertProgram;
  6616. CheckSource('TestRenameJSNameConflict',
  6617. LinesToStr([ // statements
  6618. 'this.Apply = 0;',
  6619. 'this.Bind = 0;',
  6620. 'this.Call = 0;'
  6621. ]),
  6622. LinesToStr([ // this.$main
  6623. ''
  6624. ]));
  6625. end;
  6626. procedure TTestModule.TestLocalConst;
  6627. begin
  6628. StartProgram(false);
  6629. Add('procedure DoIt;');
  6630. Add('const');
  6631. Add(' cA: longint = 1;');
  6632. Add(' cB = 2;');
  6633. Add(' procedure Sub;');
  6634. Add(' const');
  6635. Add(' csA = 3;');
  6636. Add(' cB: double = 4;');
  6637. Add(' begin');
  6638. Add(' cb:=cb+csa;');
  6639. Add(' ca:=ca+csa+5;');
  6640. Add(' end;');
  6641. Add('begin');
  6642. Add(' ca:=ca+cb+6;');
  6643. Add('end;');
  6644. Add('begin');
  6645. ConvertProgram;
  6646. CheckSource('TestLocalConst',
  6647. LinesToStr([
  6648. 'var cA = 1;',
  6649. 'var cB = 2;',
  6650. 'var csA = 3;',
  6651. 'var cB$1 = 4;',
  6652. 'this.DoIt = function () {',
  6653. ' function Sub() {',
  6654. ' cB$1 = cB$1 + 3;',
  6655. ' cA = cA + 3 + 5;',
  6656. ' };',
  6657. ' cA = cA + 2 + 6;',
  6658. '};'
  6659. ]),
  6660. LinesToStr([
  6661. ]));
  6662. end;
  6663. procedure TTestModule.TestVarExternal;
  6664. begin
  6665. StartProgram(false);
  6666. Add('var');
  6667. Add(' NaN: double; external name ''Global.NaN'';');
  6668. Add(' d: double;');
  6669. Add('begin');
  6670. Add(' d:=NaN;');
  6671. ConvertProgram;
  6672. CheckSource('TestVarExternal',
  6673. LinesToStr([
  6674. 'this.d = 0.0;'
  6675. ]),
  6676. LinesToStr([
  6677. '$mod.d = Global.NaN;'
  6678. ]));
  6679. end;
  6680. procedure TTestModule.TestVarExternalOtherUnit;
  6681. begin
  6682. AddModuleWithIntfImplSrc('unit2.pas',
  6683. LinesToStr([
  6684. 'var NaN: double; external name ''Global.NaN'';',
  6685. 'var iV: longint;'
  6686. ]),
  6687. '');
  6688. StartUnit(true);
  6689. Add('interface');
  6690. Add('uses unit2;');
  6691. Add('implementation');
  6692. Add('var');
  6693. Add(' d: double;');
  6694. Add(' i: longint; external name ''$i'';');
  6695. Add('begin');
  6696. Add(' d:=nan;');
  6697. Add(' d:=uNit2.nan;');
  6698. Add(' d:=test1.d;');
  6699. Add(' i:=iv;');
  6700. Add(' i:=uNit2.iv;');
  6701. Add(' i:=test1.i;');
  6702. ConvertUnit;
  6703. CheckSource('TestVarExternalOtherUnit',
  6704. LinesToStr([
  6705. 'var $impl = $mod.$impl;',
  6706. '']),
  6707. LinesToStr([ // this.$init
  6708. '$impl.d = Global.NaN;',
  6709. '$impl.d = Global.NaN;',
  6710. '$impl.d = $impl.d;',
  6711. '$i = pas.unit2.iV;',
  6712. '$i = pas.unit2.iV;',
  6713. '$i = $i;',
  6714. '']),
  6715. LinesToStr([ // implementation
  6716. '$impl.d = 0.0;',
  6717. '']) );
  6718. end;
  6719. procedure TTestModule.TestVarAbsoluteFail;
  6720. begin
  6721. StartProgram(false);
  6722. Add([
  6723. 'var',
  6724. ' a: longint;',
  6725. ' b: longword absolute a;',
  6726. 'begin']);
  6727. SetExpectedPasResolverError('Invalid variable modifier "absolute"',nInvalidVariableModifier);
  6728. ConvertProgram;
  6729. end;
  6730. procedure TTestModule.TestConstExternal;
  6731. begin
  6732. StartProgram(false);
  6733. Add([
  6734. 'const',
  6735. ' PI: double; external name ''Global.PI'';',
  6736. ' Tau = 2*pi;',
  6737. 'var d: double;',
  6738. 'begin',
  6739. ' d:=pi;',
  6740. ' d:=tau+pi;']);
  6741. ConvertProgram;
  6742. CheckSource('TestConstExternal',
  6743. LinesToStr([
  6744. 'this.Tau = 2*Global.PI;',
  6745. 'this.d = 0.0;'
  6746. ]),
  6747. LinesToStr([
  6748. '$mod.d = Global.PI;',
  6749. '$mod.d = $mod.Tau + Global.PI;'
  6750. ]));
  6751. end;
  6752. procedure TTestModule.TestDouble;
  6753. begin
  6754. StartProgram(false);
  6755. Add([
  6756. 'type',
  6757. ' TDateTime = double;',
  6758. 'const',
  6759. ' a = TDateTime(2.7);',
  6760. ' b = a + TDateTime(1.7);',
  6761. ' c = 0.9 + 0.1;',
  6762. ' f0_1 = 0.1;',
  6763. ' f0_3 = 0.3;',
  6764. ' fn0_1 = -0.1;',
  6765. ' fn0_3 = -0.3;',
  6766. ' fn0_003 = -0.003;',
  6767. ' fn0_123456789 = -0.123456789;',
  6768. ' fn300_0 = -300.0;',
  6769. ' fn123456_0 = -123456.0;',
  6770. ' fn1234567_8 = -1234567.8;',
  6771. ' fn12345678_9 = -12345678.9;',
  6772. ' f1_0En12 = 1E-12;',
  6773. ' fn1_0En12 = -1E-12;',
  6774. ' maxdouble = 1.7e+308;',
  6775. ' mindouble = -1.7e+308;',
  6776. ' MinSafeIntDouble = -$1fffffffffffff;',
  6777. ' MinSafeIntDouble2 = -$20000000000000-1;',
  6778. ' MaxSafeIntDouble = $1fffffffffffff;',
  6779. ' DZeroResolution = 1E-12;',
  6780. ' Minus1 = -1E-12;',
  6781. ' EPS = 1E-9;',
  6782. ' DELTA = 0.001;',
  6783. ' Big = 129.789E+100;',
  6784. ' Test0_15 = 0.15;',
  6785. ' Test999 = 2.9999999999999;',
  6786. ' Test111999 = 211199999999999000.0;',
  6787. ' TestMinus111999 = -211199999999999000.0;',
  6788. 'var',
  6789. ' d: double = b;',
  6790. 'begin',
  6791. ' d:=1.0;',
  6792. ' d:=1.0/3.0;',
  6793. ' d:=1/3;',
  6794. ' d:=5.0E-324;',
  6795. ' d:=1.7E308;',
  6796. ' d:=001.00E00;',
  6797. ' d:=002.00E001;',
  6798. ' d:=003.000E000;',
  6799. ' d:=-004.00E-00;',
  6800. ' d:=-005.00E-001;',
  6801. ' d:=10**3;',
  6802. ' d:=10 mod 3;',
  6803. ' d:=10 div 3;',
  6804. ' d:=c;',
  6805. ' d:=f0_1;',
  6806. ' d:=f0_3;',
  6807. ' d:=fn0_1;',
  6808. ' d:=fn0_3;',
  6809. ' d:=fn0_003;',
  6810. ' d:=fn0_123456789;',
  6811. ' d:=fn300_0;',
  6812. ' d:=fn123456_0;',
  6813. ' d:=fn1234567_8;',
  6814. ' d:=fn12345678_9;',
  6815. ' d:=f1_0En12;',
  6816. ' d:=fn1_0En12;',
  6817. ' d:=maxdouble;',
  6818. ' d:=mindouble;',
  6819. ' d:=MinSafeIntDouble;',
  6820. ' d:=double(MinSafeIntDouble);',
  6821. ' d:=MinSafeIntDouble2;',
  6822. ' d:=double(MinSafeIntDouble2);',
  6823. ' d:=MaxSafeIntDouble;',
  6824. ' d:=default(double);',
  6825. '']);
  6826. ConvertProgram;
  6827. CheckSource('TestDouble',
  6828. LinesToStr([
  6829. 'this.a = 2.7;',
  6830. 'this.b = 2.7 + 1.7;',
  6831. 'this.c = 0.9 + 0.1;',
  6832. 'this.f0_1 = 0.1;',
  6833. 'this.f0_3 = 0.3;',
  6834. 'this.fn0_1 = -0.1;',
  6835. 'this.fn0_3 = -0.3;',
  6836. 'this.fn0_003 = -0.003;',
  6837. 'this.fn0_123456789 = -0.123456789;',
  6838. 'this.fn300_0 = -300.0;',
  6839. 'this.fn123456_0 = -123456.0;',
  6840. 'this.fn1234567_8 = -1234567.8;',
  6841. 'this.fn12345678_9 = -12345678.9;',
  6842. 'this.f1_0En12 = 1E-12;',
  6843. 'this.fn1_0En12 = -1E-12;',
  6844. 'this.maxdouble = 1.7e+308;',
  6845. 'this.mindouble = -1.7e+308;',
  6846. 'this.MinSafeIntDouble = -0x1fffffffffffff;',
  6847. 'this.MinSafeIntDouble2 = -0x20000000000000 - 1;',
  6848. 'this.MaxSafeIntDouble = 0x1fffffffffffff;',
  6849. 'this.DZeroResolution = 1E-12;',
  6850. 'this.Minus1 = -1E-12;',
  6851. 'this.EPS = 1E-9;',
  6852. 'this.DELTA = 0.001;',
  6853. 'this.Big = 129.789E+100;',
  6854. 'this.Test0_15 = 0.15;',
  6855. 'this.Test999 = 2.9999999999999;',
  6856. 'this.Test111999 = 211199999999999000.0;',
  6857. 'this.TestMinus111999 = -211199999999999000.0;',
  6858. 'this.d = 4.4;'
  6859. ]),
  6860. LinesToStr([
  6861. '$mod.d = 1.0;',
  6862. '$mod.d = 1.0 / 3.0;',
  6863. '$mod.d = 1 / 3;',
  6864. '$mod.d = 5.0E-324;',
  6865. '$mod.d = 1.7E308;',
  6866. '$mod.d = 1.00E0;',
  6867. '$mod.d = 2.00E1;',
  6868. '$mod.d = 3.000E0;',
  6869. '$mod.d = -4.00E-0;',
  6870. '$mod.d = -5.00E-1;',
  6871. '$mod.d = Math.pow(10, 3);',
  6872. '$mod.d = 10 % 3;',
  6873. '$mod.d = rtl.trunc(10 / 3);',
  6874. '$mod.d = 1;',
  6875. '$mod.d = 0.1;',
  6876. '$mod.d = 0.3;',
  6877. '$mod.d = -0.1;',
  6878. '$mod.d = -0.3;',
  6879. '$mod.d = -0.003;',
  6880. '$mod.d = -0.123456789;',
  6881. '$mod.d = -300;',
  6882. '$mod.d = -123456;',
  6883. '$mod.d = -1234567.8;',
  6884. '$mod.d = -1.23456789E7;',
  6885. '$mod.d = 1E-12;',
  6886. '$mod.d = -1E-12;',
  6887. '$mod.d = 1.7E308;',
  6888. '$mod.d = -1.7E308;',
  6889. '$mod.d = -9007199254740991;',
  6890. '$mod.d = -9007199254740991;',
  6891. '$mod.d = -9.007199254740992E15;',
  6892. '$mod.d = -9.007199254740992E15;',
  6893. '$mod.d = 9007199254740991;',
  6894. '$mod.d = 0.0;',
  6895. '']));
  6896. end;
  6897. procedure TTestModule.TestInteger;
  6898. begin
  6899. StartProgram(false);
  6900. Add([
  6901. 'const',
  6902. ' MinInt = low(NativeInt);',
  6903. ' MaxInt = high(NativeInt);',
  6904. 'type',
  6905. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  6906. 'const',
  6907. ' a = low(TMyInt)+High(TMyInt);',
  6908. 'var',
  6909. ' i: TMyInt;',
  6910. 'begin',
  6911. ' i:=-MinInt;',
  6912. ' i:=default(TMyInt);',
  6913. ' i:=low(i)+high(i);',
  6914. '']);
  6915. ConvertProgram;
  6916. CheckSource('TestIntegerRange',
  6917. LinesToStr([
  6918. 'this.MinInt = -9007199254740991;',
  6919. 'this.MaxInt = 9007199254740991;',
  6920. 'this.a = -9007199254740991 + 9007199254740991;',
  6921. 'this.i = 0;',
  6922. '']),
  6923. LinesToStr([
  6924. '$mod.i = - -9007199254740991;',
  6925. '$mod.i = -9007199254740991;',
  6926. '$mod.i = -9007199254740991 + 9007199254740991;',
  6927. '']));
  6928. end;
  6929. procedure TTestModule.TestIntegerRange;
  6930. begin
  6931. StartProgram(false);
  6932. Add([
  6933. 'const',
  6934. ' MinInt = -1;',
  6935. ' MaxInt = +1;',
  6936. 'type',
  6937. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  6938. ' TInt2 = 1..3;',
  6939. 'const',
  6940. ' a = low(TMyInt)+High(TMyInt);',
  6941. ' b = low(TInt2)+High(TInt2);',
  6942. ' s1 = [1];',
  6943. ' s2 = [1,2];',
  6944. ' s3 = [1..3];',
  6945. ' s4 = [low(shortint)..high(shortint)];',
  6946. ' s5 = [succ(low(shortint))..pred(high(shortint))];',
  6947. ' s6 = 1 in s2;',
  6948. 'var',
  6949. ' i: TMyInt;',
  6950. ' i2: TInt2;',
  6951. 'begin',
  6952. ' i:=i2;',
  6953. ' i:=default(TMyInt);',
  6954. ' if i=i2 then ;',
  6955. ' i:=ord(i2);',
  6956. '']);
  6957. ConvertProgram;
  6958. CheckSource('TestIntegerRange',
  6959. LinesToStr([
  6960. 'this.MinInt = -1;',
  6961. 'this.MaxInt = +1;',
  6962. 'this.a = -1 + 1;',
  6963. 'this.b = 1 + 3;',
  6964. 'this.s1 = rtl.createSet(1);',
  6965. 'this.s2 = rtl.createSet(1, 2);',
  6966. 'this.s3 = rtl.createSet(null, 1, 3);',
  6967. 'this.s4 = rtl.createSet(null, -128, 127);',
  6968. 'this.s5 = rtl.createSet(null, -128 + 1, 127 - 1);',
  6969. 'this.s6 = 1 in this.s2;',
  6970. 'this.i = 0;',
  6971. 'this.i2 = 0;',
  6972. '']),
  6973. LinesToStr([
  6974. '$mod.i = $mod.i2;',
  6975. '$mod.i = -1;',
  6976. 'if ($mod.i === $mod.i2) ;',
  6977. '$mod.i = $mod.i2;',
  6978. '']));
  6979. end;
  6980. procedure TTestModule.TestIntegerTypecasts;
  6981. begin
  6982. StartProgram(false);
  6983. Add([
  6984. 'var',
  6985. ' i: nativeint;',
  6986. ' b: byte;',
  6987. ' sh: shortint;',
  6988. ' w: word;',
  6989. ' sm: smallint;',
  6990. ' lw: longword;',
  6991. ' li: longint;',
  6992. 'begin',
  6993. ' b:=byte(i);',
  6994. ' sh:=shortint(i);',
  6995. ' w:=word(i);',
  6996. ' sm:=smallint(i);',
  6997. ' lw:=longword(i);',
  6998. ' li:=longint(i);',
  6999. '']);
  7000. ConvertProgram;
  7001. CheckSource('TestIntegerTypecasts',
  7002. LinesToStr([
  7003. 'this.i = 0;',
  7004. 'this.b = 0;',
  7005. 'this.sh = 0;',
  7006. 'this.w = 0;',
  7007. 'this.sm = 0;',
  7008. 'this.lw = 0;',
  7009. 'this.li = 0;',
  7010. '']),
  7011. LinesToStr([
  7012. '$mod.b = $mod.i & 255;',
  7013. '$mod.sh = (($mod.i & 255) << 24) >> 24;',
  7014. '$mod.w = $mod.i & 65535;',
  7015. '$mod.sm = (($mod.i & 65535) << 16) >> 16;',
  7016. '$mod.lw = $mod.i >>> 0;',
  7017. '$mod.li = $mod.i & 0xFFFFFFFF;',
  7018. '']));
  7019. end;
  7020. procedure TTestModule.TestInteger_BitwiseShrNativeInt;
  7021. begin
  7022. StartProgram(false);
  7023. Add([
  7024. 'var',
  7025. ' i,j: nativeint;',
  7026. 'begin',
  7027. ' i:=i shr 0;',
  7028. ' i:=i shr 1;',
  7029. ' i:=i shr 3;',
  7030. ' i:=i shr 54;',
  7031. ' i:=j shr i;',
  7032. '']);
  7033. ConvertProgram;
  7034. CheckResolverUnexpectedHints;
  7035. CheckSource('TestInteger_BitwiseShrNativeInt',
  7036. LinesToStr([
  7037. 'this.i = 0;',
  7038. 'this.j = 0;',
  7039. '']),
  7040. LinesToStr([
  7041. '$mod.i = $mod.i;',
  7042. '$mod.i = Math.floor($mod.i / 2);',
  7043. '$mod.i = Math.floor($mod.i / 8);',
  7044. '$mod.i = 0;',
  7045. '$mod.i = rtl.shr($mod.j, $mod.i);',
  7046. '']));
  7047. end;
  7048. procedure TTestModule.TestInteger_BitwiseShlNativeInt;
  7049. begin
  7050. StartProgram(false);
  7051. Add([
  7052. 'var',
  7053. ' i: nativeint;',
  7054. 'begin',
  7055. ' i:=i shl 0;',
  7056. ' i:=i shl 54;',
  7057. ' i:=123456789012 shl 1;',
  7058. ' i:=i shl 1;',
  7059. '']);
  7060. ConvertProgram;
  7061. CheckResolverUnexpectedHints;
  7062. CheckSource('TestInteger_BitwiseShrNativeInt',
  7063. LinesToStr([
  7064. 'this.i = 0;',
  7065. '']),
  7066. LinesToStr([
  7067. '$mod.i = $mod.i;',
  7068. '$mod.i = 0;',
  7069. '$mod.i = 246913578024;',
  7070. '$mod.i = rtl.shl($mod.i, 1);',
  7071. '']));
  7072. end;
  7073. procedure TTestModule.TestInteger_SystemFunc;
  7074. begin
  7075. StartProgram(true);
  7076. Add([
  7077. 'var',
  7078. ' i: byte;',
  7079. ' s: string;',
  7080. 'begin',
  7081. ' system.inc(i);',
  7082. ' system.str(i,s);',
  7083. ' s:=system.str(i);',
  7084. ' i:=system.low(i);',
  7085. ' i:=system.high(i);',
  7086. ' i:=system.pred(i);',
  7087. ' i:=system.succ(i);',
  7088. ' i:=system.ord(i);',
  7089. '']);
  7090. ConvertProgram;
  7091. CheckResolverUnexpectedHints;
  7092. CheckSource('TestInteger_SystemFunc',
  7093. LinesToStr([
  7094. 'this.i = 0;',
  7095. 'this.s = "";',
  7096. '']),
  7097. LinesToStr([
  7098. '$mod.i += 1;',
  7099. '$mod.s = "" + $mod.i;',
  7100. '$mod.s = "" + $mod.i;',
  7101. '$mod.i = 0;',
  7102. '$mod.i = 255;',
  7103. '$mod.i = $mod.i - 1;',
  7104. '$mod.i = $mod.i + 1;',
  7105. '$mod.i = $mod.i;',
  7106. '']));
  7107. end;
  7108. procedure TTestModule.TestCurrency;
  7109. begin
  7110. StartProgram(false);
  7111. Add([
  7112. 'type',
  7113. ' TCoin = currency;',
  7114. 'const',
  7115. ' a = TCoin(2.7);',
  7116. ' b = a + TCoin(1.7);',
  7117. ' MinSafeIntCurrency: TCoin = -92233720368.5477;',
  7118. ' MaxSafeIntCurrency: TCoin = 92233720368.5477;',
  7119. 'var',
  7120. ' c: TCoin = b;',
  7121. ' i: nativeint;',
  7122. ' d: double;',
  7123. ' j: jsvalue;',
  7124. 'function DoIt(c: currency): currency; begin end;',
  7125. 'function GetIt(d: double): double; begin end;',
  7126. 'procedure Write(v: jsvalue); begin end;',
  7127. 'begin',
  7128. ' c:=1.0;',
  7129. ' c:=0.1;',
  7130. ' c:=1.0/3.0;',
  7131. ' c:=1/3;',
  7132. ' c:=a;',
  7133. ' d:=c;',
  7134. ' c:=d;',
  7135. ' c:=currency(c);',
  7136. ' c:=currency(d);',
  7137. ' d:=double(c);',
  7138. ' c:=i;',
  7139. ' c:=currency(i);',
  7140. //' i:=c;', not allowed
  7141. ' i:=nativeint(c);',
  7142. ' c:=c+a;',
  7143. ' c:=-c-a;',
  7144. ' c:=d+c;',
  7145. ' c:=c+d;',
  7146. ' c:=d-c;',
  7147. ' c:=c-d;',
  7148. ' c:=c*a;',
  7149. ' c:=a*c;',
  7150. ' c:=d*c;',
  7151. ' c:=c*d;',
  7152. ' c:=c/a;',
  7153. ' c:=a/c;',
  7154. ' c:=d/c;',
  7155. ' c:=c/d;',
  7156. ' c:=c**a;',
  7157. ' c:=a**c;',
  7158. ' c:=d**c;',
  7159. ' c:=c**d;',
  7160. ' if c=c then ;',
  7161. ' if c=a then ;',
  7162. ' if a=c then ;',
  7163. ' if d=c then ;',
  7164. ' if c=d then ;',
  7165. ' c:=DoIt(c);',
  7166. ' c:=DoIt(i);',
  7167. ' c:=DoIt(d);',
  7168. ' c:=GetIt(c);',
  7169. ' j:=c;',
  7170. ' Write(c);',
  7171. ' c:=default(currency);',
  7172. ' j:=str(c);',
  7173. ' j:=str(c:0:3);',
  7174. '']);
  7175. ConvertProgram;
  7176. CheckSource('TestCurrency',
  7177. LinesToStr([
  7178. 'this.a = 27000;',
  7179. 'this.b = this.a + 17000;',
  7180. 'this.MinSafeIntCurrency = -92233720368.5477;',
  7181. 'this.MaxSafeIntCurrency = 92233720368.5477;',
  7182. 'this.c = this.b;',
  7183. 'this.i = 0;',
  7184. 'this.d = 0.0;',
  7185. 'this.j = undefined;',
  7186. 'this.DoIt = function (c) {',
  7187. ' var Result = 0;',
  7188. ' return Result;',
  7189. '};',
  7190. 'this.GetIt = function (d) {',
  7191. ' var Result = 0.0;',
  7192. ' return Result;',
  7193. '};',
  7194. 'this.Write = function (v) {',
  7195. '};',
  7196. '']),
  7197. LinesToStr([
  7198. '$mod.c = 10000;',
  7199. '$mod.c = 1000;',
  7200. '$mod.c = rtl.trunc((1.0 / 3.0) * 10000);',
  7201. '$mod.c = rtl.trunc((1 / 3) * 10000);',
  7202. '$mod.c = $mod.a;',
  7203. '$mod.d = $mod.c / 10000;',
  7204. '$mod.c = rtl.trunc($mod.d * 10000);',
  7205. '$mod.c = $mod.c;',
  7206. '$mod.c = $mod.d * 10000;',
  7207. '$mod.d = $mod.c / 10000;',
  7208. '$mod.c = $mod.i * 10000;',
  7209. '$mod.c = $mod.i * 10000;',
  7210. '$mod.i = rtl.trunc($mod.c / 10000);',
  7211. '$mod.c = $mod.c + $mod.a;',
  7212. '$mod.c = -$mod.c - $mod.a;',
  7213. '$mod.c = ($mod.d * 10000) + $mod.c;',
  7214. '$mod.c = $mod.c + ($mod.d * 10000);',
  7215. '$mod.c = ($mod.d * 10000) - $mod.c;',
  7216. '$mod.c = $mod.c - ($mod.d * 10000);',
  7217. '$mod.c = ($mod.c * $mod.a) / 10000;',
  7218. '$mod.c = ($mod.a * $mod.c) / 10000;',
  7219. '$mod.c = $mod.d * $mod.c;',
  7220. '$mod.c = $mod.c * $mod.d;',
  7221. '$mod.c = rtl.trunc(($mod.c / $mod.a) * 10000);',
  7222. '$mod.c = rtl.trunc(($mod.a / $mod.c) * 10000);',
  7223. '$mod.c = rtl.trunc($mod.d / $mod.c);',
  7224. '$mod.c = rtl.trunc($mod.c / $mod.d);',
  7225. '$mod.c = rtl.trunc(Math.pow($mod.c / 10000, $mod.a / 10000) * 10000);',
  7226. '$mod.c = rtl.trunc(Math.pow($mod.a / 10000, $mod.c / 10000) * 10000);',
  7227. '$mod.c = rtl.trunc(Math.pow($mod.d, $mod.c / 10000) * 10000);',
  7228. '$mod.c = rtl.trunc(Math.pow($mod.c / 10000, $mod.d) * 10000);',
  7229. 'if ($mod.c === $mod.c) ;',
  7230. 'if ($mod.c === $mod.a) ;',
  7231. 'if ($mod.a === $mod.c) ;',
  7232. 'if (($mod.d * 10000) === $mod.c) ;',
  7233. 'if ($mod.c === ($mod.d * 10000)) ;',
  7234. '$mod.c = $mod.DoIt($mod.c);',
  7235. '$mod.c = $mod.DoIt($mod.i * 10000);',
  7236. '$mod.c = $mod.DoIt($mod.d * 10000);',
  7237. '$mod.c = rtl.trunc($mod.GetIt($mod.c / 10000) * 10000);',
  7238. '$mod.j = $mod.c / 10000;',
  7239. '$mod.Write($mod.c / 10000);',
  7240. '$mod.c = 0;',
  7241. '$mod.j = rtl.floatToStr($mod.c / 10000);',
  7242. '$mod.j = rtl.floatToStr($mod.c / 10000, 0, 3);',
  7243. '']));
  7244. end;
  7245. procedure TTestModule.TestForBoolDo;
  7246. begin
  7247. StartProgram(false);
  7248. Add([
  7249. 'var b: boolean;',
  7250. 'begin',
  7251. ' for b:=false to true do ;',
  7252. ' for b:=b downto false do ;',
  7253. ' for b in boolean do ;',
  7254. '']);
  7255. ConvertProgram;
  7256. CheckSource('TestForBoolDo',
  7257. LinesToStr([ // statements
  7258. 'this.b = false;']),
  7259. LinesToStr([ // this.$main
  7260. 'for (var $l = 0; $l <= 1; $l++) $mod.b = $l !== 0;',
  7261. 'for (var $l1 = +$mod.b; $l1 >= 0; $l1--) $mod.b = $l1 !== 0;',
  7262. 'for (var $l2 = 0; $l2 <= 1; $l2++) $mod.b = $l2 !== 0;',
  7263. '']));
  7264. end;
  7265. procedure TTestModule.TestForIntDo;
  7266. begin
  7267. StartProgram(false);
  7268. Add([
  7269. 'var i: longint;',
  7270. 'begin',
  7271. ' for i:=3 to 5 do ;',
  7272. ' for i:=i downto 2 do ;',
  7273. ' for i in byte do ;',
  7274. '']);
  7275. ConvertProgram;
  7276. CheckSource('TestForIntDo',
  7277. LinesToStr([ // statements
  7278. 'this.i = 0;']),
  7279. LinesToStr([ // this.$main
  7280. 'for ($mod.i = 3; $mod.i <= 5; $mod.i++) ;',
  7281. 'for (var $l = $mod.i; $l >= 2; $l--) $mod.i = $l;',
  7282. 'for (var $l1 = 0; $l1 <= 255; $l1++) $mod.i = $l1;',
  7283. '']));
  7284. end;
  7285. procedure TTestModule.TestForIntInDo;
  7286. begin
  7287. StartProgram(false);
  7288. Add([
  7289. 'type',
  7290. ' TSetOfInt = set of byte;',
  7291. ' TIntRg = 3..7;',
  7292. ' TSetOfIntRg = set of TIntRg;',
  7293. 'var',
  7294. ' i,i2: longint;',
  7295. ' a1: array of byte;',
  7296. ' a2: array[1..3] of byte;',
  7297. ' soi: TSetOfInt;',
  7298. ' soir: TSetOfIntRg;',
  7299. ' ir: TIntRg;',
  7300. 'begin',
  7301. ' for i in byte do ;',
  7302. ' for i in a1 do ;',
  7303. ' for i in a2 do ;',
  7304. ' for i in [11..13] do ;',
  7305. ' for i in TSetOfInt do ;',
  7306. ' for i in TIntRg do ;',
  7307. ' for i in soi do i2:=i;',
  7308. ' for i in TSetOfIntRg do ;',
  7309. ' for i in soir do ;',
  7310. ' for ir in TIntRg do ;',
  7311. ' for ir in TSetOfIntRg do ;',
  7312. ' for ir in soir do ;',
  7313. '']);
  7314. ConvertProgram;
  7315. CheckSource('TestForIntInDo',
  7316. LinesToStr([ // statements
  7317. 'this.i = 0;',
  7318. 'this.i2 = 0;',
  7319. 'this.a1 = [];',
  7320. 'this.a2 = rtl.arraySetLength(null, 0, 3);',
  7321. 'this.soi = {};',
  7322. 'this.soir = {};',
  7323. 'this.ir = 0;',
  7324. '']),
  7325. LinesToStr([ // this.$main
  7326. 'for (var $l = 0; $l <= 255; $l++) $mod.i = $l;',
  7327. 'for (var $in = $mod.a1, $l1 = 0, $end = rtl.length($in) - 1; $l1 <= $end; $l1++) $mod.i = $in[$l1];',
  7328. 'for (var $in1 = $mod.a2, $l2 = 0, $end1 = rtl.length($in1) - 1; $l2 <= $end1; $l2++) $mod.i = $in1[$l2];',
  7329. 'for (var $l3 = 11; $l3 <= 13; $l3++) $mod.i = $l3;',
  7330. 'for (var $l4 = 0; $l4 <= 255; $l4++) $mod.i = $l4;',
  7331. 'for (var $l5 = 3; $l5 <= 7; $l5++) $mod.i = $l5;',
  7332. 'for (var $l6 in $mod.soi) {',
  7333. ' $mod.i = +$l6;',
  7334. ' $mod.i2 = $mod.i;',
  7335. '};',
  7336. 'for (var $l7 = 3; $l7 <= 7; $l7++) $mod.i = $l7;',
  7337. 'for (var $l8 in $mod.soir) $mod.i = +$l8;',
  7338. 'for (var $l9 = 3; $l9 <= 7; $l9++) $mod.ir = $l9;',
  7339. 'for (var $l10 = 3; $l10 <= 7; $l10++) $mod.ir = $l10;',
  7340. 'for (var $l11 in $mod.soir) $mod.ir = +$l11;',
  7341. '']));
  7342. end;
  7343. procedure TTestModule.TestCharConst;
  7344. begin
  7345. StartProgram(false);
  7346. Add([
  7347. 'const',
  7348. ' a = #$00F3;',
  7349. ' c: char = ''1'';',
  7350. ' wc: widechar = ''ä'';',
  7351. 'begin',
  7352. ' c:=#0;',
  7353. ' c:=#1;',
  7354. ' c:=#9;',
  7355. ' c:=#10;',
  7356. ' c:=#13;',
  7357. ' c:=#31;',
  7358. ' c:=#32;',
  7359. ' c:=#$A;',
  7360. ' c:=#$0A;',
  7361. ' c:=#$b;',
  7362. ' c:=#$0b;',
  7363. ' c:=^A;',
  7364. ' c:=''"'';',
  7365. ' c:=default(char);',
  7366. ' c:=#$00E4;', // ä
  7367. ' c:=''ä'';',
  7368. ' c:=#$E4;', // ä
  7369. ' c:=#$D800;', // invalid UTF-16
  7370. ' c:=#$DFFF;', // invalid UTF-16
  7371. ' c:=#$FFFF;', // last UCS-2
  7372. ' c:=high(c);', // last UCS-2
  7373. ' c:=#269;',
  7374. '']);
  7375. ConvertProgram;
  7376. CheckSource('TestCharConst',
  7377. LinesToStr([
  7378. 'this.a="ó";',
  7379. 'this.c="1";',
  7380. 'this.wc="ä";'
  7381. ]),
  7382. LinesToStr([
  7383. '$mod.c="\x00";',
  7384. '$mod.c="\x01";',
  7385. '$mod.c="\t";',
  7386. '$mod.c="\n";',
  7387. '$mod.c="\r";',
  7388. '$mod.c="\x1F";',
  7389. '$mod.c=" ";',
  7390. '$mod.c="\n";',
  7391. '$mod.c="\n";',
  7392. '$mod.c="\x0B";',
  7393. '$mod.c="\x0B";',
  7394. '$mod.c="\x01";',
  7395. '$mod.c=''"'';',
  7396. '$mod.c="\x00";',
  7397. '$mod.c = "ä";',
  7398. '$mod.c = "ä";',
  7399. '$mod.c = "ä";',
  7400. '$mod.c="\uD800";',
  7401. '$mod.c="\uDFFF";',
  7402. '$mod.c="\uFFFF";',
  7403. '$mod.c="\uFFFF";',
  7404. '$mod.c = "č";',
  7405. '']));
  7406. end;
  7407. procedure TTestModule.TestChar_Compare;
  7408. begin
  7409. StartProgram(false);
  7410. Add('var');
  7411. Add(' c: char;');
  7412. Add(' b: boolean;');
  7413. Add('begin');
  7414. Add(' b:=c=''1'';');
  7415. Add(' b:=''2''=c;');
  7416. Add(' b:=''3''=''4'';');
  7417. Add(' b:=c<>''5'';');
  7418. Add(' b:=''6''<>c;');
  7419. Add(' b:=c>''7'';');
  7420. Add(' b:=''8''>c;');
  7421. Add(' b:=c>=''9'';');
  7422. Add(' b:=''A''>=c;');
  7423. Add(' b:=c<''B'';');
  7424. Add(' b:=''C''<c;');
  7425. Add(' b:=c<=''D'';');
  7426. Add(' b:=''E''<=c;');
  7427. ConvertProgram;
  7428. CheckSource('TestChar_Compare',
  7429. LinesToStr([
  7430. 'this.c="";',
  7431. 'this.b = false;'
  7432. ]),
  7433. LinesToStr([
  7434. '$mod.b = $mod.c === "1";',
  7435. '$mod.b = "2" === $mod.c;',
  7436. '$mod.b = "3" === "4";',
  7437. '$mod.b = $mod.c !== "5";',
  7438. '$mod.b = "6" !== $mod.c;',
  7439. '$mod.b = $mod.c > "7";',
  7440. '$mod.b = "8" > $mod.c;',
  7441. '$mod.b = $mod.c >= "9";',
  7442. '$mod.b = "A" >= $mod.c;',
  7443. '$mod.b = $mod.c < "B";',
  7444. '$mod.b = "C" < $mod.c;',
  7445. '$mod.b = $mod.c <= "D";',
  7446. '$mod.b = "E" <= $mod.c;',
  7447. '']));
  7448. end;
  7449. procedure TTestModule.TestChar_BuiltInProcs;
  7450. begin
  7451. StartProgram(false);
  7452. Add([
  7453. 'var',
  7454. ' c: char;',
  7455. ' i: longint;',
  7456. ' s: string;',
  7457. 'begin',
  7458. ' i:=ord(c);',
  7459. ' i:=ord(s[i]);',
  7460. ' c:=chr(i);',
  7461. ' c:=pred(c);',
  7462. ' c:=succ(c);',
  7463. ' c:=low(c);',
  7464. ' c:=high(c);',
  7465. ' i:=byte(c);',
  7466. ' i:=word(c);',
  7467. ' i:=longint(c);',
  7468. '']);
  7469. ConvertProgram;
  7470. CheckSource('TestChar_BuiltInProcs',
  7471. LinesToStr([
  7472. 'this.c = "";',
  7473. 'this.i = 0;',
  7474. 'this.s = "";'
  7475. ]),
  7476. LinesToStr([
  7477. '$mod.i = $mod.c.charCodeAt();',
  7478. '$mod.i = $mod.s.charCodeAt($mod.i-1);',
  7479. '$mod.c = String.fromCharCode($mod.i);',
  7480. '$mod.c = String.fromCharCode($mod.c.charCodeAt() - 1);',
  7481. '$mod.c = String.fromCharCode($mod.c.charCodeAt() + 1);',
  7482. '$mod.c = "\x00";',
  7483. '$mod.c = "\uFFFF";',
  7484. '$mod.i = $mod.c.charCodeAt() & 255;',
  7485. '$mod.i = $mod.c.charCodeAt();',
  7486. '$mod.i = $mod.c.charCodeAt() & 0xFFFFFFFF;',
  7487. '']));
  7488. end;
  7489. procedure TTestModule.TestStringConst;
  7490. begin
  7491. StartProgram(false);
  7492. Add([
  7493. '{$H+}',
  7494. 'const',
  7495. ' a = #$00F3#$017C;', // first <256, then >=256
  7496. ' b = string(''a'');',
  7497. ' c = string(''ä'');',
  7498. ' d = UnicodeString(''b'');',
  7499. ' e = UnicodeString(''ö'');',
  7500. 'var',
  7501. ' s: string = ''abc'';',
  7502. 'begin',
  7503. ' s:='''';',
  7504. ' s:=#13#10;',
  7505. ' s:=#9''foo'';',
  7506. ' s:=#$A9;',
  7507. ' s:=''foo''#13''bar'';',
  7508. ' s:=''"'';',
  7509. ' s:=''"''''"'';',
  7510. ' s:=#$20AC;', // euro
  7511. ' s:=#$10437;', // outside BMP
  7512. ' s:=''abc''#$20AC;', // ascii,#
  7513. ' s:=''ä''#$20AC;', // non ascii,#
  7514. ' s:=#$20AC''abc'';', // #, ascii
  7515. ' s:=#$20AC''ä'';', // #, non ascii
  7516. ' s:=default(string);',
  7517. ' s:=concat(s);',
  7518. ' s:=concat(s,''a'',s);',
  7519. ' s:=#250#269;',
  7520. //' s:=#$2F804;',
  7521. // ToDo: \uD87E\uDC04 -> \u{2F804}
  7522. '']);
  7523. ConvertProgram;
  7524. CheckSource('TestStringConst',
  7525. LinesToStr([
  7526. 'this.a = "óż";',
  7527. 'this.b = "a";',
  7528. 'this.c = "ä";',
  7529. 'this.d = "b";',
  7530. 'this.e = "ö";',
  7531. 'this.s="abc";',
  7532. '']),
  7533. LinesToStr([
  7534. '$mod.s="";',
  7535. '$mod.s="\r\n";',
  7536. '$mod.s="\tfoo";',
  7537. '$mod.s="©";',
  7538. '$mod.s="foo\rbar";',
  7539. '$mod.s=''"'';',
  7540. '$mod.s=''"\''"'';',
  7541. '$mod.s="€";',
  7542. '$mod.s="'#$F0#$90#$90#$B7'";',
  7543. '$mod.s = "abc€";',
  7544. '$mod.s = "ä€";',
  7545. '$mod.s = "€abc";',
  7546. '$mod.s = "ۊ";',
  7547. '$mod.s="";',
  7548. '$mod.s = $mod.s;',
  7549. '$mod.s = $mod.s.concat("a", $mod.s);',
  7550. '$mod.s = "úč";',
  7551. '']));
  7552. end;
  7553. procedure TTestModule.TestStringConst_InvalidUTF16;
  7554. begin
  7555. StartProgram(false);
  7556. Add([
  7557. 'const',
  7558. ' a: char = #$D87E;',
  7559. ' b: string = #$D87E;',
  7560. ' c: string = #$D87E#43;',
  7561. 'begin',
  7562. ' c:=''abc''#$D87E;',
  7563. ' c:=#0#1#2;',
  7564. ' c:=#127;',
  7565. ' c:=#128;',
  7566. ' c:=#255;',
  7567. ' c:=#256;',
  7568. '']);
  7569. ConvertProgram;
  7570. CheckSource('TestStringConst',
  7571. LinesToStr([
  7572. 'this.a = "\uD87E";',
  7573. 'this.b = "\uD87E";',
  7574. 'this.c = "\uD87E+";',
  7575. '']),
  7576. LinesToStr([
  7577. '$mod.c = "abc\uD87E";',
  7578. '$mod.c = "\x00\x01\x02";',
  7579. '$mod.c = "'#127'";',
  7580. '$mod.c = "'#$c2#$80'";',
  7581. '$mod.c = "'#$c3#$BF'";',
  7582. '$mod.c = "'#$c4#$80'";',
  7583. '']));
  7584. end;
  7585. procedure TTestModule.TestStringConstSurrogate;
  7586. begin
  7587. StartProgram(false);
  7588. Add([
  7589. 'var',
  7590. ' s: string;',
  7591. 'begin',
  7592. ' s:=''😊'';', // 1F60A
  7593. '']);
  7594. ConvertProgram;
  7595. CheckSource('TestStringConstSurrogate',
  7596. LinesToStr([
  7597. 'this.s="";'
  7598. ]),
  7599. LinesToStr([
  7600. '$mod.s="😊";'
  7601. ]));
  7602. end;
  7603. procedure TTestModule.TestString_Length;
  7604. begin
  7605. StartProgram(false);
  7606. Add('const c = ''foo'';');
  7607. Add('var');
  7608. Add(' s: string;');
  7609. Add(' i: longint;');
  7610. Add('begin');
  7611. Add(' i:=length(s);');
  7612. Add(' i:=length(s+s);');
  7613. Add(' i:=length(''abc'');');
  7614. Add(' i:=length(c);');
  7615. ConvertProgram;
  7616. CheckSource('TestString_Length',
  7617. LinesToStr([
  7618. 'this.c = "foo";',
  7619. 'this.s = "";',
  7620. 'this.i = 0;',
  7621. '']),
  7622. LinesToStr([
  7623. '$mod.i = $mod.s.length;',
  7624. '$mod.i = ($mod.s+$mod.s).length;',
  7625. '$mod.i = "abc".length;',
  7626. '$mod.i = $mod.c.length;',
  7627. '']));
  7628. end;
  7629. procedure TTestModule.TestString_Compare;
  7630. begin
  7631. StartProgram(false);
  7632. Add('var');
  7633. Add(' s, t: string;');
  7634. Add(' b: boolean;');
  7635. Add('begin');
  7636. Add(' b:=s=t;');
  7637. Add(' b:=s<>t;');
  7638. Add(' b:=s>t;');
  7639. Add(' b:=s>=t;');
  7640. Add(' b:=s<t;');
  7641. Add(' b:=s<=t;');
  7642. ConvertProgram;
  7643. CheckSource('TestString_Compare',
  7644. LinesToStr([ // statements
  7645. 'this.s = "";',
  7646. 'this.t = "";',
  7647. 'this.b =false;'
  7648. ]),
  7649. LinesToStr([ // this.$main
  7650. '$mod.b = $mod.s === $mod.t;',
  7651. '$mod.b = $mod.s !== $mod.t;',
  7652. '$mod.b = $mod.s > $mod.t;',
  7653. '$mod.b = $mod.s >= $mod.t;',
  7654. '$mod.b = $mod.s < $mod.t;',
  7655. '$mod.b = $mod.s <= $mod.t;',
  7656. '']));
  7657. end;
  7658. procedure TTestModule.TestString_SetLength;
  7659. begin
  7660. StartProgram(false);
  7661. Add([
  7662. 'procedure DoIt(var s: string);',
  7663. 'begin',
  7664. ' SetLength(s,2);',
  7665. 'end;',
  7666. 'var s: string;',
  7667. 'begin',
  7668. ' SetLength(s,3);',
  7669. '']);
  7670. ConvertProgram;
  7671. CheckSource('TestString_SetLength',
  7672. LinesToStr([ // statements
  7673. 'this.DoIt = function (s) {',
  7674. ' s.set(rtl.strSetLength(s.get(), 2));',
  7675. '};',
  7676. 'this.s = "";',
  7677. '']),
  7678. LinesToStr([ // this.$main
  7679. '$mod.s = rtl.strSetLength($mod.s, 3);'
  7680. ]));
  7681. end;
  7682. procedure TTestModule.TestString_CharAt;
  7683. begin
  7684. StartProgram(false);
  7685. Add([
  7686. 'var',
  7687. ' s: string;',
  7688. ' c: char;',
  7689. ' b: boolean;',
  7690. 'begin',
  7691. ' b:= s[1] = c;',
  7692. ' b:= c = s[1];',
  7693. ' b:= c <> s[1];',
  7694. ' b:= c > s[1];',
  7695. ' b:= c >= s[1];',
  7696. ' b:= c < s[2];',
  7697. ' b:= c <= s[1];',
  7698. ' s[1] := c;',
  7699. ' s[2+3] := c;']);
  7700. ConvertProgram;
  7701. CheckSource('TestString_CharAt',
  7702. LinesToStr([ // statements
  7703. 'this.s = "";',
  7704. 'this.c = "";',
  7705. 'this.b = false;'
  7706. ]),
  7707. LinesToStr([ // this.$main
  7708. '$mod.b = $mod.s.charAt(0) === $mod.c;',
  7709. '$mod.b = $mod.c === $mod.s.charAt(0);',
  7710. '$mod.b = $mod.c !== $mod.s.charAt(0);',
  7711. '$mod.b = $mod.c > $mod.s.charAt(0);',
  7712. '$mod.b = $mod.c >= $mod.s.charAt(0);',
  7713. '$mod.b = $mod.c < $mod.s.charAt(1);',
  7714. '$mod.b = $mod.c <= $mod.s.charAt(0);',
  7715. '$mod.s = rtl.setCharAt($mod.s, 0, $mod.c);',
  7716. '$mod.s = rtl.setCharAt($mod.s, (2 + 3) - 1, $mod.c);',
  7717. '']));
  7718. end;
  7719. procedure TTestModule.TestStringHMinusFail;
  7720. begin
  7721. StartProgram(false);
  7722. Add([
  7723. '{$H-}',
  7724. 'var s: string;',
  7725. 'begin']);
  7726. ConvertProgram;
  7727. CheckHint(mtWarning,nWarnIllegalCompilerDirectiveX,'Warning: test1.pp(3,6) : Illegal compiler directive "H-"');
  7728. end;
  7729. procedure TTestModule.TestStr;
  7730. begin
  7731. StartProgram(false);
  7732. Add('var');
  7733. Add(' b: boolean;');
  7734. Add(' i: longint;');
  7735. Add(' d: double;');
  7736. Add(' s: string;');
  7737. Add('begin');
  7738. Add(' str(b,s);');
  7739. Add(' str(i,s);');
  7740. Add(' str(d,s);');
  7741. Add(' str(i:3,s);');
  7742. Add(' str(d:3:2,s);');
  7743. Add(' Str(12.456:12:1,s);');
  7744. Add(' Str(12.456:12,s);');
  7745. Add(' s:=str(b);');
  7746. Add(' s:=str(i);');
  7747. Add(' s:=str(d);');
  7748. Add(' s:=str(i,i);');
  7749. Add(' s:=str(i:3);');
  7750. Add(' s:=str(d:3:2);');
  7751. Add(' s:=str(i:4,i);');
  7752. Add(' s:=str(i,i:5);');
  7753. Add(' s:=str(i:4,i:5);');
  7754. Add(' s:=str(s,s);');
  7755. Add(' s:=str(s,''foo'');');
  7756. ConvertProgram;
  7757. CheckSource('TestStr',
  7758. LinesToStr([ // statements
  7759. 'this.b = false;',
  7760. 'this.i = 0;',
  7761. 'this.d = 0.0;',
  7762. 'this.s = "";',
  7763. '']),
  7764. LinesToStr([ // this.$main
  7765. '$mod.s = ""+$mod.b;',
  7766. '$mod.s = ""+$mod.i;',
  7767. '$mod.s = rtl.floatToStr($mod.d);',
  7768. '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
  7769. '$mod.s = rtl.floatToStr($mod.d,3,2);',
  7770. '$mod.s = rtl.floatToStr(12.456,12,1);',
  7771. '$mod.s = rtl.floatToStr(12.456,12);',
  7772. '$mod.s = ""+$mod.b;',
  7773. '$mod.s = ""+$mod.i;',
  7774. '$mod.s = rtl.floatToStr($mod.d);',
  7775. '$mod.s = ""+$mod.i+$mod.i;',
  7776. '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
  7777. '$mod.s = rtl.floatToStr($mod.d,3,2);',
  7778. '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + $mod.i;',
  7779. '$mod.s = "" + $mod.i + rtl.spaceLeft("" + $mod.i, 5);',
  7780. '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + rtl.spaceLeft("" + $mod.i, 5);',
  7781. '$mod.s = $mod.s + $mod.s;',
  7782. '$mod.s = $mod.s + "foo";',
  7783. '']));
  7784. end;
  7785. procedure TTestModule.TestBaseType_AnsiStringFail;
  7786. begin
  7787. StartProgram(false);
  7788. Add('var s: AnsiString');
  7789. SetExpectedPasResolverError('identifier not found "AnsiString"',PasResolveEval.nIdentifierNotFound);
  7790. ConvertProgram;
  7791. end;
  7792. procedure TTestModule.TestBaseType_WideStringFail;
  7793. begin
  7794. StartProgram(false);
  7795. Add('var s: WideString');
  7796. SetExpectedPasResolverError('identifier not found "WideString"',PasResolveEval.nIdentifierNotFound);
  7797. ConvertProgram;
  7798. end;
  7799. procedure TTestModule.TestBaseType_ShortStringFail;
  7800. begin
  7801. StartProgram(false);
  7802. Add('var s: ShortString');
  7803. SetExpectedPasResolverError('identifier not found "ShortString"',PasResolveEval.nIdentifierNotFound);
  7804. ConvertProgram;
  7805. end;
  7806. procedure TTestModule.TestBaseType_RawByteStringFail;
  7807. begin
  7808. StartProgram(false);
  7809. Add('var s: RawByteString');
  7810. SetExpectedPasResolverError('identifier not found "RawByteString"',PasResolveEval.nIdentifierNotFound);
  7811. ConvertProgram;
  7812. end;
  7813. procedure TTestModule.TestTypeShortstring_Fail;
  7814. begin
  7815. StartProgram(false);
  7816. Add('type t = string[12];');
  7817. Add('var s: t;');
  7818. Add('begin');
  7819. SetExpectedPasResolverError('illegal qualifier "["',nIllegalQualifier);
  7820. ConvertProgram;
  7821. end;
  7822. procedure TTestModule.TestCharSet_Custom;
  7823. begin
  7824. StartProgram(false);
  7825. Add([
  7826. 'type',
  7827. ' TCharRg = ''a''..''z'';',
  7828. ' TSetOfCharRg = set of TCharRg;',
  7829. ' TCharRg2 = ''m''..''p'';',
  7830. 'const',
  7831. ' crg: TCharRg = ''b'';',
  7832. 'var',
  7833. ' c: char;',
  7834. ' crg2: TCharRg2;',
  7835. ' s: TSetOfCharRg;',
  7836. 'begin',
  7837. ' c:=crg;',
  7838. ' crg:=c;',
  7839. ' crg2:=crg;',
  7840. ' if c=crg then ;',
  7841. ' if crg=c then ;',
  7842. ' if crg=crg2 then ;',
  7843. ' if c in s then ;',
  7844. ' if crg2 in s then ;',
  7845. ' c:=default(TCharRg);',
  7846. '']);
  7847. ConvertProgram;
  7848. CheckSource('TestCharSet_Custom',
  7849. LinesToStr([ // statements
  7850. 'this.crg = "b";',
  7851. 'this.c = "";',
  7852. 'this.crg2 = "m";',
  7853. 'this.s = {};',
  7854. '']),
  7855. LinesToStr([ // this.$main
  7856. '$mod.c = $mod.crg;',
  7857. '$mod.crg = $mod.c;',
  7858. '$mod.crg2 = $mod.crg;',
  7859. 'if ($mod.c === $mod.crg) ;',
  7860. 'if ($mod.crg === $mod.c) ;',
  7861. 'if ($mod.crg === $mod.crg2) ;',
  7862. 'if ($mod.c.charCodeAt() in $mod.s) ;',
  7863. 'if ($mod.crg2.charCodeAt() in $mod.s) ;',
  7864. '$mod.c = "a";',
  7865. '']));
  7866. end;
  7867. procedure TTestModule.TestWideChar;
  7868. begin
  7869. StartProgram(false);
  7870. Add([
  7871. 'procedure Fly(var c: char);',
  7872. 'begin',
  7873. 'end;',
  7874. 'procedure Run(var c: widechar);',
  7875. 'begin',
  7876. 'end;',
  7877. 'var',
  7878. ' c: char;',
  7879. ' wc: widechar;',
  7880. ' w: word;',
  7881. 'begin',
  7882. ' Fly(wc);',
  7883. ' Run(c);',
  7884. ' wc:=WideChar(w);',
  7885. ' w:=ord(wc);',
  7886. '']);
  7887. ConvertProgram;
  7888. CheckSource('TestWideChar_VarArg',
  7889. LinesToStr([ // statements
  7890. 'this.Fly = function (c) {',
  7891. '};',
  7892. 'this.Run = function (c) {',
  7893. '};',
  7894. 'this.c = "";',
  7895. 'this.wc = "";',
  7896. 'this.w = 0;',
  7897. '']),
  7898. LinesToStr([ // this.$main
  7899. '$mod.Fly({',
  7900. ' p: $mod,',
  7901. ' get: function () {',
  7902. ' return this.p.wc;',
  7903. ' },',
  7904. ' set: function (v) {',
  7905. ' this.p.wc = v;',
  7906. ' }',
  7907. '});',
  7908. '$mod.Run({',
  7909. ' p: $mod,',
  7910. ' get: function () {',
  7911. ' return this.p.c;',
  7912. ' },',
  7913. ' set: function (v) {',
  7914. ' this.p.c = v;',
  7915. ' }',
  7916. '});',
  7917. '$mod.wc = String.fromCharCode($mod.w);',
  7918. '$mod.w = $mod.wc.charCodeAt();',
  7919. '',
  7920. '']));
  7921. end;
  7922. procedure TTestModule.TestForCharDo;
  7923. begin
  7924. StartProgram(false);
  7925. Add([
  7926. 'var c: char;',
  7927. 'begin',
  7928. ' for c:=''a'' to ''c'' do ;',
  7929. ' for c:=c downto ''a'' do ;',
  7930. ' for c:=''Б'' to ''Я'' do ;',
  7931. '']);
  7932. ConvertProgram;
  7933. CheckSource('TestForCharDo',
  7934. LinesToStr([ // statements
  7935. 'this.c = "";']),
  7936. LinesToStr([ // this.$main
  7937. 'for (var $l = 97; $l <= 99; $l++) $mod.c = String.fromCharCode($l);',
  7938. 'for (var $l1 = $mod.c.charCodeAt(); $l1 >= 97; $l1--) $mod.c = String.fromCharCode($l1);',
  7939. 'for (var $l2 = 1041; $l2 <= 1071; $l2++) $mod.c = String.fromCharCode($l2);',
  7940. '']));
  7941. end;
  7942. procedure TTestModule.TestForCharInDo;
  7943. begin
  7944. StartProgram(false);
  7945. Add([
  7946. 'type',
  7947. ' TSetOfChar = set of char;',
  7948. ' TCharRg = ''a''..''z'';',
  7949. ' TSetOfCharRg = set of TCharRg;',
  7950. 'const Foo = ''foo'';',
  7951. 'var',
  7952. ' c,c2: char;',
  7953. ' s: string;',
  7954. ' a1: array of char;',
  7955. ' a2: array[1..3] of char;',
  7956. ' soc: TSetOfChar;',
  7957. ' socr: TSetOfCharRg;',
  7958. ' cr: TCharRg;',
  7959. 'begin',
  7960. ' for c in foo do ;',
  7961. ' for c in s do ;',
  7962. ' for c in char do ;',
  7963. ' for c in a1 do ;',
  7964. ' for c in a2 do ;',
  7965. ' for c in [''1''..''3''] do ;',
  7966. ' for c in TSetOfChar do ;',
  7967. ' for c in TCharRg do ;',
  7968. ' for c in soc do c2:=c;',
  7969. ' for c in TSetOfCharRg do ;',
  7970. ' for c in socr do ;',
  7971. ' for cr in TCharRg do ;',
  7972. ' for cr in TSetOfCharRg do ;',
  7973. ' for cr in socr do ;',
  7974. '']);
  7975. ConvertProgram;
  7976. CheckSource('TestForCharInDo',
  7977. LinesToStr([ // statements
  7978. 'this.Foo = "foo";',
  7979. 'this.c = "";',
  7980. 'this.c2 = "";',
  7981. 'this.s = "";',
  7982. 'this.a1 = [];',
  7983. 'this.a2 = rtl.arraySetLength(null, "", 3);',
  7984. 'this.soc = {};',
  7985. 'this.socr = {};',
  7986. 'this.cr = "a";',
  7987. '']),
  7988. LinesToStr([ // this.$main
  7989. 'for (var $in = $mod.Foo, $l = 0, $end = $in.length - 1; $l <= $end; $l++) $mod.c = $in.charAt($l);',
  7990. 'for (var $in1 = $mod.s, $l1 = 0, $end1 = $in1.length - 1; $l1 <= $end1; $l1++) $mod.c = $in1.charAt($l1);',
  7991. 'for (var $l2 = 0; $l2 <= 65535; $l2++) $mod.c = String.fromCharCode($l2);',
  7992. 'for (var $in2 = $mod.a1, $l3 = 0, $end2 = rtl.length($in2) - 1; $l3 <= $end2; $l3++) $mod.c = $in2[$l3];',
  7993. 'for (var $in3 = $mod.a2, $l4 = 0, $end3 = rtl.length($in3) - 1; $l4 <= $end3; $l4++) $mod.c = $in3[$l4];',
  7994. 'for (var $l5 = 49; $l5 <= 51; $l5++) $mod.c = String.fromCharCode($l5);',
  7995. 'for (var $l6 = 0; $l6 <= 65535; $l6++) $mod.c = String.fromCharCode($l6);',
  7996. 'for (var $l7 = 97; $l7 <= 122; $l7++) $mod.c = String.fromCharCode($l7);',
  7997. 'for (var $l8 in $mod.soc) {',
  7998. ' $mod.c = String.fromCharCode($l8);',
  7999. ' $mod.c2 = $mod.c;',
  8000. '};',
  8001. 'for (var $l9 = 97; $l9 <= 122; $l9++) $mod.c = String.fromCharCode($l9);',
  8002. 'for (var $l10 in $mod.socr) $mod.c = String.fromCharCode($l10);',
  8003. 'for (var $l11 = 97; $l11 <= 122; $l11++) $mod.cr = String.fromCharCode($l11);',
  8004. 'for (var $l12 = 97; $l12 <= 122; $l12++) $mod.cr = String.fromCharCode($l12);',
  8005. 'for (var $l13 in $mod.socr) $mod.cr = String.fromCharCode($l13);',
  8006. '']));
  8007. end;
  8008. procedure TTestModule.TestProcTwoArgs;
  8009. begin
  8010. StartProgram(false);
  8011. Add('procedure Test(a,b: longint);');
  8012. Add('begin');
  8013. Add('end;');
  8014. Add('begin');
  8015. ConvertProgram;
  8016. CheckSource('TestProcTwoArgs',
  8017. LinesToStr([ // statements
  8018. 'this.Test = function (a,b) {',
  8019. '};'
  8020. ]),
  8021. LinesToStr([ // this.$main
  8022. ''
  8023. ]));
  8024. end;
  8025. procedure TTestModule.TestProc_DefaultValue;
  8026. begin
  8027. StartProgram(false);
  8028. Add('procedure p1(i: longint = 1);');
  8029. Add('begin');
  8030. Add('end;');
  8031. Add('procedure p2(i: longint = 1; c: char = ''a'');');
  8032. Add('begin');
  8033. Add('end;');
  8034. Add('procedure p3(d: double = 1.0; b: boolean = false; s: string = ''abc'');');
  8035. Add('begin');
  8036. Add('end;');
  8037. Add('begin');
  8038. Add(' p1;');
  8039. Add(' p1();');
  8040. Add(' p1(11);');
  8041. Add(' p2;');
  8042. Add(' p2();');
  8043. Add(' p2(12);');
  8044. Add(' p2(13,''b'');');
  8045. Add(' p3();');
  8046. ConvertProgram;
  8047. CheckSource('TestProc_DefaultValue',
  8048. LinesToStr([ // statements
  8049. 'this.p1 = function (i) {',
  8050. '};',
  8051. 'this.p2 = function (i,c) {',
  8052. '};',
  8053. 'this.p3 = function (d,b,s) {',
  8054. '};'
  8055. ]),
  8056. LinesToStr([ // this.$main
  8057. ' $mod.p1(1);',
  8058. ' $mod.p1(1);',
  8059. ' $mod.p1(11);',
  8060. ' $mod.p2(1,"a");',
  8061. ' $mod.p2(1,"a");',
  8062. ' $mod.p2(12,"a");',
  8063. ' $mod.p2(13,"b");',
  8064. ' $mod.p3(1.0,false,"abc");'
  8065. ]));
  8066. end;
  8067. procedure TTestModule.TestFunctionInt;
  8068. begin
  8069. StartProgram(false);
  8070. Add('function MyTest(Bar: longint): longint;');
  8071. Add('begin');
  8072. Add(' Result:=2*bar');
  8073. Add('end;');
  8074. Add('begin');
  8075. ConvertProgram;
  8076. CheckSource('TestFunctionInt',
  8077. LinesToStr([ // statements
  8078. 'this.MyTest = function (Bar) {',
  8079. ' var Result = 0;',
  8080. ' Result = 2*Bar;',
  8081. ' return Result;',
  8082. '};'
  8083. ]),
  8084. LinesToStr([ // this.$main
  8085. ''
  8086. ]));
  8087. end;
  8088. procedure TTestModule.TestFunctionString;
  8089. begin
  8090. StartProgram(false);
  8091. Add('function Test(Bar: string): string;');
  8092. Add('begin');
  8093. Add(' Result:=bar+BAR');
  8094. Add('end;');
  8095. Add('begin');
  8096. ConvertProgram;
  8097. CheckSource('TestFunctionString',
  8098. LinesToStr([ // statements
  8099. 'this.Test = function (Bar) {',
  8100. ' var Result = "";',
  8101. ' Result = Bar+Bar;',
  8102. ' return Result;',
  8103. '};'
  8104. ]),
  8105. LinesToStr([ // this.$main
  8106. ''
  8107. ]));
  8108. end;
  8109. procedure TTestModule.TestIfThen;
  8110. begin
  8111. StartProgram(false);
  8112. Add([
  8113. 'var b: boolean;',
  8114. 'begin',
  8115. ' if b then ;',
  8116. ' if b then else ;']);
  8117. ConvertProgram;
  8118. CheckSource('TestIfThen',
  8119. LinesToStr([ // statements
  8120. 'this.b = false;',
  8121. '']),
  8122. LinesToStr([ // this.$main
  8123. 'if ($mod.b) ;',
  8124. 'if ($mod.b) ;',
  8125. '']));
  8126. end;
  8127. procedure TTestModule.TestForLoop;
  8128. begin
  8129. StartProgram(false);
  8130. Add('var');
  8131. Add(' vI, vJ, vN: longint;');
  8132. Add('begin');
  8133. Add(' VJ:=0;');
  8134. Add(' VN:=3;');
  8135. Add(' for VI:=1 to VN do');
  8136. Add(' begin');
  8137. Add(' VJ:=VJ+VI;');
  8138. Add(' end;');
  8139. ConvertProgram;
  8140. CheckSource('TestForLoop',
  8141. LinesToStr([ // statements
  8142. 'this.vI = 0;',
  8143. 'this.vJ = 0;',
  8144. 'this.vN = 0;'
  8145. ]),
  8146. LinesToStr([ // this.$main
  8147. ' $mod.vJ = 0;',
  8148. ' $mod.vN = 3;',
  8149. ' for (var $l = 1, $end = $mod.vN; $l <= $end; $l++) {',
  8150. ' $mod.vI = $l;',
  8151. ' $mod.vJ = $mod.vJ + $mod.vI;',
  8152. ' };',
  8153. '']));
  8154. end;
  8155. procedure TTestModule.TestForLoopInsideFunction;
  8156. begin
  8157. StartProgram(false);
  8158. Add('function SumNumbers(Count: longint): longint;');
  8159. Add('var');
  8160. Add(' vI, vJ: longint;');
  8161. Add('begin');
  8162. Add(' vj:=0;');
  8163. Add(' for vi:=1 to count do');
  8164. Add(' begin');
  8165. Add(' vj:=vj+vi;');
  8166. Add(' end;');
  8167. Add('end;');
  8168. Add('begin');
  8169. Add(' sumnumbers(3);');
  8170. ConvertProgram;
  8171. CheckSource('TestForLoopInsideFunction',
  8172. LinesToStr([ // statements
  8173. 'this.SumNumbers = function (Count) {',
  8174. ' var Result = 0;',
  8175. ' var vI = 0;',
  8176. ' var vJ = 0;',
  8177. ' vJ = 0;',
  8178. ' for (var $l = 1, $end = Count; $l <= $end; $l++) {',
  8179. ' vI = $l;',
  8180. ' vJ = vJ + vI;',
  8181. ' };',
  8182. ' return Result;',
  8183. '};'
  8184. ]),
  8185. LinesToStr([ // $mod.$main
  8186. ' $mod.SumNumbers(3);'
  8187. ]));
  8188. end;
  8189. procedure TTestModule.TestForLoop_ReadVarAfter;
  8190. begin
  8191. StartProgram(false);
  8192. Add('var');
  8193. Add(' vI: longint;');
  8194. Add('begin');
  8195. Add(' for vi:=1 to 2 do ;');
  8196. Add(' if vi=3 then ;');
  8197. ConvertProgram;
  8198. CheckSource('TestForLoop',
  8199. LinesToStr([ // statements
  8200. 'this.vI = 0;'
  8201. ]),
  8202. LinesToStr([ // this.$main
  8203. ' for ($mod.vI = 1; $mod.vI <= 2; $mod.vI++) ;',
  8204. ' if ($mod.vI===3) ;'
  8205. ]));
  8206. end;
  8207. procedure TTestModule.TestForLoop_Nested;
  8208. begin
  8209. StartProgram(false);
  8210. Add('function SumNumbers(Count: longint): longint;');
  8211. Add('var');
  8212. Add(' vI, vJ, vK: longint;');
  8213. Add('begin');
  8214. Add(' VK:=0;');
  8215. Add(' for VI:=1 to count do');
  8216. Add(' begin');
  8217. Add(' for vj:=1 to vi do');
  8218. Add(' begin');
  8219. Add(' vk:=VK+VI;');
  8220. Add(' end;');
  8221. Add(' end;');
  8222. Add('end;');
  8223. Add('begin');
  8224. Add(' sumnumbers(3);');
  8225. ConvertProgram;
  8226. CheckSource('TestForLoopInFunction',
  8227. LinesToStr([ // statements
  8228. 'this.SumNumbers = function (Count) {',
  8229. ' var Result = 0;',
  8230. ' var vI = 0;',
  8231. ' var vJ = 0;',
  8232. ' var vK = 0;',
  8233. ' vK = 0;',
  8234. ' for (var $l = 1, $end = Count; $l <= $end; $l++) {',
  8235. ' vI = $l;',
  8236. ' for (var $l1 = 1, $end1 = vI; $l1 <= $end1; $l1++) {',
  8237. ' vJ = $l1;',
  8238. ' vK = vK + vI;',
  8239. ' };',
  8240. ' };',
  8241. ' return Result;',
  8242. '};'
  8243. ]),
  8244. LinesToStr([ // $mod.$main
  8245. ' $mod.SumNumbers(3);'
  8246. ]));
  8247. end;
  8248. procedure TTestModule.TestRepeatUntil;
  8249. begin
  8250. StartProgram(false);
  8251. Add('var');
  8252. Add(' vI, vJ, vN: longint;');
  8253. Add('begin');
  8254. Add(' vn:=3;');
  8255. Add(' vj:=0;');
  8256. Add(' VI:=0;');
  8257. Add(' repeat');
  8258. Add(' VI:=vi+1;');
  8259. Add(' vj:=VJ+vI;');
  8260. Add(' until vi>=vn');
  8261. ConvertProgram;
  8262. CheckSource('TestRepeatUntil',
  8263. LinesToStr([ // statements
  8264. 'this.vI = 0;',
  8265. 'this.vJ = 0;',
  8266. 'this.vN = 0;'
  8267. ]),
  8268. LinesToStr([ // $mod.$main
  8269. ' $mod.vN = 3;',
  8270. ' $mod.vJ = 0;',
  8271. ' $mod.vI = 0;',
  8272. ' do{',
  8273. ' $mod.vI = $mod.vI + 1;',
  8274. ' $mod.vJ = $mod.vJ + $mod.vI;',
  8275. ' }while(!($mod.vI>=$mod.vN));'
  8276. ]));
  8277. end;
  8278. procedure TTestModule.TestAsmBlock;
  8279. begin
  8280. StartProgram(false);
  8281. Add([
  8282. 'var',
  8283. ' vI: longint;',
  8284. 'begin',
  8285. ' vi:=1;',
  8286. ' asm',
  8287. ' if (vI===1) {',
  8288. ' vI=2;',
  8289. //' console.log(''end;'');', ToDo
  8290. ' }',
  8291. ' if (vI===2){ vI=3; }',
  8292. ' end;',
  8293. ' VI:=4;']);
  8294. ConvertProgram;
  8295. CheckSource('TestAsmBlock',
  8296. LinesToStr([ // statements
  8297. 'this.vI = 0;'
  8298. ]),
  8299. LinesToStr([ // $mod.$main
  8300. '$mod.vI = 1;',
  8301. 'if (vI===1) {',
  8302. ' vI=2;',
  8303. '}',
  8304. 'if (vI===2){ vI=3; }',
  8305. ';',
  8306. '$mod.vI = 4;'
  8307. ]));
  8308. end;
  8309. procedure TTestModule.TestAsmPas_Impl;
  8310. begin
  8311. StartUnit(false);
  8312. Add('interface');
  8313. Add('const cIntf: longint = 1;');
  8314. Add('var vIntf: longint;');
  8315. Add('implementation');
  8316. Add('const cImpl: longint = 2;');
  8317. Add('var vImpl: longint;');
  8318. Add('procedure DoIt;');
  8319. Add('const cLoc: longint = 3;');
  8320. Add('var vLoc: longint;');
  8321. Add('begin;');
  8322. Add(' asm');
  8323. //Add(' pas(vIntf)=pas(cIntf);');
  8324. //Add(' pas(vImpl)=pas(cImpl);');
  8325. //Add(' pas(vLoc)=pas(cLoc);');
  8326. Add(' end;');
  8327. Add('end;');
  8328. ConvertUnit;
  8329. CheckSource('TestAsmPas_Impl',
  8330. LinesToStr([
  8331. 'var $impl = $mod.$impl;',
  8332. 'this.cIntf = 1;',
  8333. 'this.vIntf = 0;',
  8334. '']),
  8335. '', // this.$init
  8336. LinesToStr([ // implementation
  8337. '$impl.cImpl = 2;',
  8338. '$impl.vImpl = 0;',
  8339. 'var cLoc = 3;',
  8340. '$impl.DoIt = function () {',
  8341. ' var vLoc = 0;',
  8342. '};',
  8343. '']) );
  8344. end;
  8345. procedure TTestModule.TestTryFinally;
  8346. begin
  8347. StartProgram(false);
  8348. Add('var i: longint;');
  8349. Add('begin');
  8350. Add(' try');
  8351. Add(' i:=0; i:=2 div i;');
  8352. Add(' finally');
  8353. Add(' i:=3');
  8354. Add(' end;');
  8355. ConvertProgram;
  8356. CheckSource('TestTryFinally',
  8357. LinesToStr([ // statements
  8358. 'this.i = 0;'
  8359. ]),
  8360. LinesToStr([ // $mod.$main
  8361. 'try {',
  8362. ' $mod.i = 0;',
  8363. ' $mod.i = rtl.trunc(2 / $mod.i);',
  8364. '} finally {',
  8365. ' $mod.i = 3;',
  8366. '};'
  8367. ]));
  8368. end;
  8369. procedure TTestModule.TestTryExcept;
  8370. begin
  8371. StartProgram(false);
  8372. Add([
  8373. 'type',
  8374. ' TObject = class end;',
  8375. ' Exception = class Msg: string; end;',
  8376. ' EInvalidCast = class(Exception) end;',
  8377. 'var vI: longint;',
  8378. 'begin',
  8379. ' try',
  8380. ' vi:=1;',
  8381. ' except',
  8382. ' vi:=2',
  8383. ' end;',
  8384. ' try',
  8385. ' vi:=3;',
  8386. ' except',
  8387. ' raise;',
  8388. ' end;',
  8389. ' try',
  8390. ' VI:=4;',
  8391. ' except',
  8392. ' on einvalidcast do',
  8393. ' raise;',
  8394. ' on E: exception do',
  8395. ' if e.msg='''' then',
  8396. ' raise e;',
  8397. ' else',
  8398. ' vi:=5',
  8399. ' end;',
  8400. ' try',
  8401. ' VI:=6;',
  8402. ' except',
  8403. ' on einvalidcast do ;',
  8404. ' end;',
  8405. '']);
  8406. ConvertProgram;
  8407. CheckSource('TestTryExcept',
  8408. LinesToStr([ // statements
  8409. 'rtl.createClass(this, "TObject", null, function () {',
  8410. ' this.$init = function () {',
  8411. ' };',
  8412. ' this.$final = function () {',
  8413. ' };',
  8414. '});',
  8415. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  8416. ' this.$init = function () {',
  8417. ' $mod.TObject.$init.call(this);',
  8418. ' this.Msg = "";',
  8419. ' };',
  8420. '});',
  8421. 'rtl.createClass(this, "EInvalidCast", this.Exception, function () {',
  8422. '});',
  8423. 'this.vI = 0;'
  8424. ]),
  8425. LinesToStr([ // $mod.$main
  8426. 'try {',
  8427. ' $mod.vI = 1;',
  8428. '} catch ($e) {',
  8429. ' $mod.vI = 2;',
  8430. '};',
  8431. 'try {',
  8432. ' $mod.vI = 3;',
  8433. '} catch ($e) {',
  8434. ' throw $e;',
  8435. '};',
  8436. 'try {',
  8437. ' $mod.vI = 4;',
  8438. '} catch ($e) {',
  8439. ' if ($mod.EInvalidCast.isPrototypeOf($e)){',
  8440. ' throw $e',
  8441. ' } else if ($mod.Exception.isPrototypeOf($e)) {',
  8442. ' var E = $e;',
  8443. ' if (E.Msg === "") throw E;',
  8444. ' } else {',
  8445. ' $mod.vI = 5;',
  8446. ' }',
  8447. '};',
  8448. 'try {',
  8449. ' $mod.vI = 6;',
  8450. '} catch ($e) {',
  8451. ' if ($mod.EInvalidCast.isPrototypeOf($e)){' ,
  8452. ' } else throw $e',
  8453. '};',
  8454. '']));
  8455. end;
  8456. procedure TTestModule.TestTryExcept_ReservedWords;
  8457. begin
  8458. StartProgram(false);
  8459. Add([
  8460. 'type',
  8461. ' TObject = class end;',
  8462. ' Exception = class',
  8463. ' Symbol: string;',
  8464. ' end;',
  8465. 'var &try: longint;',
  8466. 'begin',
  8467. ' try',
  8468. ' &try:=4;',
  8469. ' except',
  8470. ' on Error: exception do',
  8471. ' if errOR.symBol='''' then',
  8472. ' raise ERRor;',
  8473. ' end;',
  8474. '']);
  8475. ConvertProgram;
  8476. CheckSource('TestTryExcept_ReservedWords',
  8477. LinesToStr([ // statements
  8478. 'rtl.createClass(this, "TObject", null, function () {',
  8479. ' this.$init = function () {',
  8480. ' };',
  8481. ' this.$final = function () {',
  8482. ' };',
  8483. '});',
  8484. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  8485. ' this.$init = function () {',
  8486. ' $mod.TObject.$init.call(this);',
  8487. ' this.Symbol = "";',
  8488. ' };',
  8489. '});',
  8490. 'this.Try = 0;',
  8491. '']),
  8492. LinesToStr([ // $mod.$main
  8493. 'try {',
  8494. ' $mod.Try = 4;',
  8495. '} catch ($e) {',
  8496. ' if ($mod.Exception.isPrototypeOf($e)) {',
  8497. ' var error = $e;',
  8498. ' if (error.Symbol === "") throw error;',
  8499. ' } else throw $e',
  8500. '};',
  8501. '']));
  8502. end;
  8503. procedure TTestModule.TestIfThenRaiseElse;
  8504. begin
  8505. StartProgram(false);
  8506. Add([
  8507. 'type',
  8508. ' TObject = class',
  8509. ' constructor Create;',
  8510. ' end;',
  8511. 'constructor TObject.Create;',
  8512. 'begin',
  8513. 'end;',
  8514. 'var b: boolean;',
  8515. 'begin',
  8516. ' if b then',
  8517. ' raise TObject.Create',
  8518. ' else',
  8519. ' b:=false;',
  8520. '']);
  8521. ConvertProgram;
  8522. CheckSource('TestIfThenRaiseElse',
  8523. LinesToStr([ // statements
  8524. 'rtl.createClass(this, "TObject", null, function () {',
  8525. ' this.$init = function () {',
  8526. ' };',
  8527. ' this.$final = function () {',
  8528. ' };',
  8529. ' this.Create = function () {',
  8530. ' return this;',
  8531. ' };',
  8532. '});',
  8533. 'this.b = false;',
  8534. '']),
  8535. LinesToStr([ // $mod.$main
  8536. 'if ($mod.b) {',
  8537. ' throw $mod.TObject.$create("Create")}',
  8538. ' else $mod.b = false;',
  8539. '']));
  8540. end;
  8541. procedure TTestModule.TestCaseOf;
  8542. begin
  8543. StartProgram(false);
  8544. Add([
  8545. 'const e: longint; external name ''$e'';',
  8546. 'var vI: longint;',
  8547. 'begin',
  8548. ' case vi of',
  8549. ' 1: ;',
  8550. ' 2: vi:=3;',
  8551. ' e: ;',
  8552. ' else',
  8553. ' VI:=4',
  8554. ' end;']);
  8555. ConvertProgram;
  8556. CheckSource('TestCaseOf',
  8557. LinesToStr([ // statements
  8558. 'this.vI = 0;'
  8559. ]),
  8560. LinesToStr([ // $mod.$main
  8561. 'var $tmp = $mod.vI;',
  8562. 'if ($tmp === 1) {}',
  8563. 'else if ($tmp === 2) {',
  8564. ' $mod.vI = 3}',
  8565. ' else if ($tmp === $e) {}',
  8566. 'else {',
  8567. ' $mod.vI = 4;',
  8568. '};'
  8569. ]));
  8570. end;
  8571. procedure TTestModule.TestCaseOf_UseSwitch;
  8572. begin
  8573. StartProgram(false);
  8574. Converter.UseSwitchStatement:=true;
  8575. Add('var Vi: longint;');
  8576. Add('begin');
  8577. Add(' case vi of');
  8578. Add(' 1: ;');
  8579. Add(' 2: VI:=3;');
  8580. Add(' else');
  8581. Add(' vi:=4');
  8582. Add(' end;');
  8583. ConvertProgram;
  8584. CheckSource('TestCaseOf_UseSwitch',
  8585. LinesToStr([ // statements
  8586. 'this.Vi = 0;'
  8587. ]),
  8588. LinesToStr([ // $mod.$main
  8589. 'switch ($mod.Vi) {',
  8590. 'case 1:',
  8591. ' break;',
  8592. 'case 2:',
  8593. ' $mod.Vi = 3;',
  8594. ' break;',
  8595. 'default:',
  8596. ' $mod.Vi = 4;',
  8597. '};'
  8598. ]));
  8599. end;
  8600. procedure TTestModule.TestCaseOfNoElse;
  8601. begin
  8602. StartProgram(false);
  8603. Add('var Vi: longint;');
  8604. Add('begin');
  8605. Add(' case vi of');
  8606. Add(' 1: begin vi:=2; VI:=3; end;');
  8607. Add(' end;');
  8608. ConvertProgram;
  8609. CheckSource('TestCaseOfNoElse',
  8610. LinesToStr([ // statements
  8611. 'this.Vi = 0;'
  8612. ]),
  8613. LinesToStr([ // $mod.$main
  8614. 'var $tmp = $mod.Vi;',
  8615. 'if ($tmp === 1) {',
  8616. ' $mod.Vi = 2;',
  8617. ' $mod.Vi = 3;',
  8618. '};'
  8619. ]));
  8620. end;
  8621. procedure TTestModule.TestCaseOfNoElse_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: begin VI:=2; vi:=3; end;');
  8629. Add(' end;');
  8630. ConvertProgram;
  8631. CheckSource('TestCaseOfNoElse_UseSwitch',
  8632. LinesToStr([ // statements
  8633. 'this.vI = 0;'
  8634. ]),
  8635. LinesToStr([ // $mod.$main
  8636. 'switch ($mod.vI) {',
  8637. 'case 1:',
  8638. ' $mod.vI = 2;',
  8639. ' $mod.vI = 3;',
  8640. ' break;',
  8641. '};'
  8642. ]));
  8643. end;
  8644. procedure TTestModule.TestCaseOfRange;
  8645. begin
  8646. StartProgram(false);
  8647. Add('var vI: longint;');
  8648. Add('begin');
  8649. Add(' case vi of');
  8650. Add(' 1..3: vi:=14;');
  8651. Add(' 4,5: vi:=16;');
  8652. Add(' 6..7,9..10: ;');
  8653. Add(' else ;');
  8654. Add(' end;');
  8655. ConvertProgram;
  8656. CheckSource('TestCaseOfRange',
  8657. LinesToStr([ // statements
  8658. 'this.vI = 0;'
  8659. ]),
  8660. LinesToStr([ // $mod.$main
  8661. 'var $tmp = $mod.vI;',
  8662. 'if (($tmp >= 1) && ($tmp <= 3)){',
  8663. ' $mod.vI = 14',
  8664. '} else if (($tmp === 4) || ($tmp === 5)){',
  8665. ' $mod.vI = 16',
  8666. '} else if ((($tmp >= 6) && ($tmp <= 7)) || (($tmp >= 9) && ($tmp <= 10))) ;'
  8667. ]));
  8668. end;
  8669. procedure TTestModule.TestCaseOfString;
  8670. begin
  8671. StartProgram(false);
  8672. Add([
  8673. 'var s,h: string;',
  8674. 'begin',
  8675. ' case s of',
  8676. ' ''foo'': s:=h;',
  8677. ' ''a''..''z'': h:=s;',
  8678. ' ''ў'', ''ё'': ;',
  8679. ' ''Б''..''Я'': ;',
  8680. ' end;',
  8681. '']);
  8682. ConvertProgram;
  8683. CheckSource('TestCaseOfString',
  8684. LinesToStr([ // statements
  8685. 'this.s = "";',
  8686. 'this.h = "";',
  8687. '']),
  8688. LinesToStr([ // $mod.$main
  8689. 'var $tmp = $mod.s;',
  8690. 'if ($tmp === "foo") {',
  8691. ' $mod.s = $mod.h}',
  8692. ' else if (($tmp.length === 1) && ($tmp >= "a") && ($tmp <= "z")) {',
  8693. ' $mod.h = $mod.s}',
  8694. ' else if (($tmp === "ў") || ($tmp === "ё")) {}',
  8695. ' else if (($tmp.length === 1) && ($tmp >= "Б") && ($tmp <= "Я")) ;',
  8696. '']));
  8697. end;
  8698. procedure TTestModule.TestCaseOfChar;
  8699. begin
  8700. StartProgram(false);
  8701. Add([
  8702. 'var s,h: char;',
  8703. 'begin',
  8704. ' case s of',
  8705. ' ''a''..''z'': h:=s;',
  8706. ' ''ä'': ;',
  8707. ' ''ў'', ''ё'': ;',
  8708. ' ''Б''..''Я'': ;',
  8709. ' end;',
  8710. '']);
  8711. ConvertProgram;
  8712. CheckSource('TestCaseOfString',
  8713. LinesToStr([ // statements
  8714. 'this.s = "";',
  8715. 'this.h = "";',
  8716. '']),
  8717. LinesToStr([ // $mod.$main
  8718. 'var $tmp = $mod.s;',
  8719. 'if (($tmp >= "a") && ($tmp <= "z")) {',
  8720. ' $mod.h = $mod.s}',
  8721. ' else if ($tmp === "ä") {}',
  8722. ' else if (($tmp === "ў") || ($tmp === "ё")) {}',
  8723. ' else if (($tmp >= "Б") && ($tmp <= "Я")) ;',
  8724. '']));
  8725. end;
  8726. procedure TTestModule.TestCaseOfExternalClassConst;
  8727. begin
  8728. StartProgram(false);
  8729. Add([
  8730. '{$modeswitch externalclass}',
  8731. 'type',
  8732. ' TBird = class external name ''Bird''',
  8733. ' const e: longint;',
  8734. ' end;',
  8735. 'var vI: longint;',
  8736. 'begin',
  8737. ' case vi of',
  8738. ' 1: vi:=3;',
  8739. ' TBird.e: ;',
  8740. ' end;']);
  8741. ConvertProgram;
  8742. CheckSource('TestCaseOfExternalClassConst',
  8743. LinesToStr([ // statements
  8744. 'this.vI = 0;'
  8745. ]),
  8746. LinesToStr([ // $mod.$main
  8747. 'var $tmp = $mod.vI;',
  8748. 'if ($tmp === 1) {',
  8749. ' $mod.vI = 3}',
  8750. ' else if ($tmp === Bird.e) ;'
  8751. ]));
  8752. end;
  8753. procedure TTestModule.TestDebugger;
  8754. begin
  8755. StartProgram(false);
  8756. Add([
  8757. 'procedure DoIt;',
  8758. 'begin',
  8759. ' deBugger;',
  8760. ' DeBugger();',
  8761. 'end;',
  8762. 'begin',
  8763. ' Debugger;']);
  8764. ConvertProgram;
  8765. CheckSource('TestDebugger',
  8766. LinesToStr([ // statements
  8767. 'this.DoIt = function () {',
  8768. ' debugger;',
  8769. ' debugger;',
  8770. '};',
  8771. '']),
  8772. LinesToStr([ // $mod.$main
  8773. 'debugger;',
  8774. '']));
  8775. end;
  8776. procedure TTestModule.TestArray_Dynamic;
  8777. begin
  8778. StartProgram(false);
  8779. Add([
  8780. 'type',
  8781. ' TArrayInt = array of longint;',
  8782. 'var',
  8783. ' Arr: TArrayInt;',
  8784. ' i: longint;',
  8785. ' b: boolean;',
  8786. 'begin',
  8787. ' SetLength(arr,3);',
  8788. ' arr[0]:=4;',
  8789. ' arr[1]:=length(arr)+arr[0];',
  8790. ' arr[i]:=5;',
  8791. ' arr[arr[i]]:=arr[6];',
  8792. ' i:=low(arr);',
  8793. ' i:=high(arr);',
  8794. ' b:=Assigned(arr);',
  8795. ' Arr:=default(TArrayInt);']);
  8796. ConvertProgram;
  8797. CheckSource('TestArray_Dynamic',
  8798. LinesToStr([ // statements
  8799. 'this.Arr = [];',
  8800. 'this.i = 0;',
  8801. 'this.b = false;'
  8802. ]),
  8803. LinesToStr([ // $mod.$main
  8804. '$mod.Arr = rtl.arraySetLength($mod.Arr,0,3);',
  8805. '$mod.Arr[0] = 4;',
  8806. '$mod.Arr[1] = rtl.length($mod.Arr) + $mod.Arr[0];',
  8807. '$mod.Arr[$mod.i] = 5;',
  8808. '$mod.Arr[$mod.Arr[$mod.i]] = $mod.Arr[6];',
  8809. '$mod.i = 0;',
  8810. '$mod.i = rtl.length($mod.Arr) - 1;',
  8811. '$mod.b = rtl.length($mod.Arr) > 0;',
  8812. '$mod.Arr = [];',
  8813. '']));
  8814. end;
  8815. procedure TTestModule.TestArray_Dynamic_Nil;
  8816. begin
  8817. StartProgram(false);
  8818. Add('type');
  8819. Add(' TArrayInt = array of longint;');
  8820. Add('var');
  8821. Add(' Arr: TArrayInt;');
  8822. Add('procedure DoIt(const i: TArrayInt; j: TArrayInt); begin end;');
  8823. Add('begin');
  8824. Add(' arr:=nil;');
  8825. Add(' if arr=nil then;');
  8826. Add(' if nil=arr then;');
  8827. Add(' if arr<>nil then;');
  8828. Add(' if nil<>arr then;');
  8829. Add(' DoIt(nil,nil);');
  8830. ConvertProgram;
  8831. CheckSource('TestArray_Dynamic',
  8832. LinesToStr([ // statements
  8833. 'this.Arr = [];',
  8834. 'this.DoIt = function(i,j){',
  8835. '};'
  8836. ]),
  8837. LinesToStr([ // $mod.$main
  8838. '$mod.Arr = [];',
  8839. 'if (rtl.length($mod.Arr) === 0) ;',
  8840. 'if (rtl.length($mod.Arr) === 0) ;',
  8841. 'if (rtl.length($mod.Arr) > 0) ;',
  8842. 'if (rtl.length($mod.Arr) > 0) ;',
  8843. '$mod.DoIt([],[]);',
  8844. '']));
  8845. end;
  8846. procedure TTestModule.TestArray_DynMultiDimensional;
  8847. begin
  8848. StartProgram(false);
  8849. Add([
  8850. 'type',
  8851. ' TArrayInt = array of longint;',
  8852. ' TArrayArrayInt = array of TArrayInt;',
  8853. 'var',
  8854. ' Arr: TArrayInt;',
  8855. ' Arr2: TArrayArrayInt;',
  8856. ' i: longint;',
  8857. 'begin',
  8858. ' arr2:=nil;',
  8859. ' if arr2=nil then;',
  8860. ' if nil=arr2 then;',
  8861. ' i:=low(arr2);',
  8862. ' i:=low(arr2[1]);',
  8863. ' i:=high(arr2);',
  8864. ' i:=high(arr2[2]);',
  8865. ' arr2[3]:=arr;',
  8866. ' arr2[4][5]:=i;',
  8867. ' i:=arr2[6][7];',
  8868. ' arr2[8,9]:=i;',
  8869. ' i:=arr2[10,11];',
  8870. ' SetLength(arr2,14);',
  8871. ' SetLength(arr2[15],16);']);
  8872. ConvertProgram;
  8873. CheckSource('TestArray_Dynamic',
  8874. LinesToStr([ // statements
  8875. 'this.Arr = [];',
  8876. 'this.Arr2 = [];',
  8877. 'this.i = 0;'
  8878. ]),
  8879. LinesToStr([ // $mod.$main
  8880. '$mod.Arr2 = [];',
  8881. 'if (rtl.length($mod.Arr2) === 0) ;',
  8882. 'if (rtl.length($mod.Arr2) === 0) ;',
  8883. '$mod.i = 0;',
  8884. '$mod.i = 0;',
  8885. '$mod.i = rtl.length($mod.Arr2) - 1;',
  8886. '$mod.i = rtl.length($mod.Arr2[2]) - 1;',
  8887. '$mod.Arr2[3] = rtl.arrayRef($mod.Arr);',
  8888. '$mod.Arr2[4][5] = $mod.i;',
  8889. '$mod.i = $mod.Arr2[6][7];',
  8890. '$mod.Arr2[8][9] = $mod.i;',
  8891. '$mod.i = $mod.Arr2[10][11];',
  8892. '$mod.Arr2 = rtl.arraySetLength($mod.Arr2, [], 14);',
  8893. '$mod.Arr2[15] = rtl.arraySetLength($mod.Arr2[15], 0, 16);',
  8894. '']));
  8895. end;
  8896. procedure TTestModule.TestArray_DynamicAssign;
  8897. begin
  8898. StartProgram(false);
  8899. Add([
  8900. 'type',
  8901. ' TArrayInt = array of longint;',
  8902. ' TArrayArrayInt = array of TArrayInt;',
  8903. 'procedure Run(a: TArrayInt; const b: TArrayInt; constref c: TArrayInt);',
  8904. 'begin',
  8905. 'end;',
  8906. 'procedure Fly(var a: TArrayInt);',
  8907. 'begin',
  8908. 'end;',
  8909. 'var',
  8910. ' Arr: TArrayInt;',
  8911. ' Arr2: TArrayArrayInt;',
  8912. 'begin',
  8913. ' arr:=nil;',
  8914. ' arr2:=nil;',
  8915. ' arr2[1]:=nil;',
  8916. ' arr2[2]:=arr;',
  8917. ' Run(arr,arr,arr);',
  8918. ' Fly(arr);',
  8919. ' Run(arr2[4],arr2[5],arr2[6]);',
  8920. ' Fly(arr2[7]);',
  8921. '']);
  8922. ConvertProgram;
  8923. CheckSource('TestArray_DynamicAssign',
  8924. LinesToStr([ // statements
  8925. 'this.Run = function (a, b, c) {',
  8926. '};',
  8927. 'this.Fly = function (a) {',
  8928. '};',
  8929. 'this.Arr = [];',
  8930. 'this.Arr2 = [];',
  8931. '']),
  8932. LinesToStr([ // $mod.$main
  8933. '$mod.Arr = [];',
  8934. '$mod.Arr2 = [];',
  8935. '$mod.Arr2[1] = [];',
  8936. '$mod.Arr2[2] = rtl.arrayRef($mod.Arr);',
  8937. '$mod.Run(rtl.arrayRef($mod.Arr), $mod.Arr, $mod.Arr);',
  8938. '$mod.Fly({',
  8939. ' p: $mod,',
  8940. ' get: function () {',
  8941. ' return this.p.Arr;',
  8942. ' },',
  8943. ' set: function (v) {',
  8944. ' this.p.Arr = v;',
  8945. ' }',
  8946. '});',
  8947. '$mod.Run(rtl.arrayRef($mod.Arr2[4]), $mod.Arr2[5], $mod.Arr2[6]);',
  8948. '$mod.Fly({',
  8949. ' a: 7,',
  8950. ' p: $mod.Arr2,',
  8951. ' get: function () {',
  8952. ' return this.p[this.a];',
  8953. ' },',
  8954. ' set: function (v) {',
  8955. ' this.p[this.a] = v;',
  8956. ' }',
  8957. '});',
  8958. '']));
  8959. end;
  8960. procedure TTestModule.TestArray_StaticInt;
  8961. begin
  8962. StartProgram(false);
  8963. Add('type');
  8964. Add(' TArrayInt = array[2..4] of longint;');
  8965. Add('var');
  8966. Add(' Arr: TArrayInt;');
  8967. Add(' Arr2: TArrayInt = (5,6,7);');
  8968. Add(' i: longint;');
  8969. Add(' b: boolean;');
  8970. Add('begin');
  8971. Add(' arr[2]:=4;');
  8972. Add(' arr[3]:=arr[2]+arr[3];');
  8973. Add(' arr[i]:=5;');
  8974. Add(' arr[arr[i]]:=arr[high(arr)];');
  8975. Add(' i:=low(arr);');
  8976. Add(' i:=high(arr);');
  8977. Add(' b:=arr[2]=arr[3];');
  8978. Add(' arr:=default(TArrayInt);');
  8979. ConvertProgram;
  8980. CheckSource('TestArray_StaticInt',
  8981. LinesToStr([ // statements
  8982. 'this.Arr = rtl.arraySetLength(null,0,3);',
  8983. 'this.Arr2 = [5, 6, 7];',
  8984. 'this.i = 0;',
  8985. 'this.b = false;'
  8986. ]),
  8987. LinesToStr([ // $mod.$main
  8988. '$mod.Arr[0] = 4;',
  8989. '$mod.Arr[1] = $mod.Arr[0] + $mod.Arr[1];',
  8990. '$mod.Arr[$mod.i-2] = 5;',
  8991. '$mod.Arr[$mod.Arr[$mod.i-2]-2] = $mod.Arr[2];',
  8992. '$mod.i = 2;',
  8993. '$mod.i = 4;',
  8994. '$mod.b = $mod.Arr[0] === $mod.Arr[1];',
  8995. '$mod.Arr = rtl.arraySetLength(null,0,3);',
  8996. '']));
  8997. end;
  8998. procedure TTestModule.TestArray_StaticBool;
  8999. begin
  9000. StartProgram(false);
  9001. Add('type');
  9002. Add(' TBools = array[boolean] of boolean;');
  9003. Add(' TBool2 = array[true..true] of boolean;');
  9004. Add('var');
  9005. Add(' Arr: TBools;');
  9006. Add(' Arr2: TBool2;');
  9007. Add(' Arr3: TBools = (true,false);');
  9008. Add(' b: boolean;');
  9009. Add('begin');
  9010. Add(' b:=low(arr);');
  9011. Add(' b:=high(arr);');
  9012. Add(' arr[true]:=false;');
  9013. Add(' arr[false]:=arr[b] or arr[true];');
  9014. Add(' arr[b]:=true;');
  9015. Add(' arr[arr[b]]:=arr[high(arr)];');
  9016. Add(' b:=arr[false]=arr[true];');
  9017. Add(' b:=low(arr2);');
  9018. Add(' b:=high(arr2);');
  9019. Add(' arr2[true]:=true;');
  9020. Add(' arr2[true]:=arr2[true] and arr2[b];');
  9021. Add(' arr2[b]:=false;');
  9022. ConvertProgram;
  9023. CheckSource('TestArray_StaticBool',
  9024. LinesToStr([ // statements
  9025. 'this.Arr = rtl.arraySetLength(null,false,2);',
  9026. 'this.Arr2 = rtl.arraySetLength(null,false,1);',
  9027. 'this.Arr3 = [true, false];',
  9028. 'this.b = false;'
  9029. ]),
  9030. LinesToStr([ // $mod.$main
  9031. '$mod.b = false;',
  9032. '$mod.b = true;',
  9033. '$mod.Arr[1] = false;',
  9034. '$mod.Arr[0] = $mod.Arr[+$mod.b] || $mod.Arr[1];',
  9035. '$mod.Arr[+$mod.b] = true;',
  9036. '$mod.Arr[+$mod.Arr[+$mod.b]] = $mod.Arr[1];',
  9037. '$mod.b = $mod.Arr[0] === $mod.Arr[1];',
  9038. '$mod.b = true;',
  9039. '$mod.b = true;',
  9040. '$mod.Arr2[0] = true;',
  9041. '$mod.Arr2[0] = $mod.Arr2[0] && $mod.Arr2[1-$mod.b];',
  9042. '$mod.Arr2[1-$mod.b] = false;',
  9043. '']));
  9044. end;
  9045. procedure TTestModule.TestArray_StaticChar;
  9046. begin
  9047. StartProgram(false);
  9048. Add([
  9049. 'type',
  9050. ' TChars = array[char] of char;',
  9051. ' TChars2 = array[''a''..''z''] of char;',
  9052. 'var',
  9053. ' Arr: TChars;',
  9054. ' Arr2: TChars2;',
  9055. ' Arr3: array[2..4] of char = (''p'',''a'',''s'');',
  9056. ' Arr4: array[11..13] of char = ''pas'';',
  9057. ' Arr5: array[21..22] of char = ''äö'';',
  9058. ' Arr6: array[31..32] of char = ''ä''+''ö'';',
  9059. ' c: char;',
  9060. ' b: boolean;',
  9061. 'begin',
  9062. ' c:=low(arr);',
  9063. ' c:=high(arr);',
  9064. ' arr[''B'']:=''a'';',
  9065. ' arr[''D'']:=arr[c];',
  9066. ' arr[c]:=arr[''d''];',
  9067. ' arr[arr[c]]:=arr[high(arr)];',
  9068. ' b:=arr[low(arr)]=arr[''e''];',
  9069. ' c:=low(arr2);',
  9070. ' c:=high(arr2);',
  9071. ' arr2[''b'']:=''f'';',
  9072. ' arr2[''a'']:=arr2[c];',
  9073. ' arr2[c]:=arr2[''g''];']);
  9074. ConvertProgram;
  9075. CheckSource('TestArray_StaticChar',
  9076. LinesToStr([ // statements
  9077. 'this.Arr = rtl.arraySetLength(null, "", 65536);',
  9078. 'this.Arr2 = rtl.arraySetLength(null, "", 26);',
  9079. 'this.Arr3 = ["p", "a", "s"];',
  9080. 'this.Arr4 = ["p", "a", "s"];',
  9081. 'this.Arr5 = ["ä", "ö"];',
  9082. 'this.Arr6 = ["ä", "ö"];',
  9083. 'this.c = "";',
  9084. 'this.b = false;',
  9085. '']),
  9086. LinesToStr([ // $mod.$main
  9087. '$mod.c = "\x00";',
  9088. '$mod.c = "\uFFFF";',
  9089. '$mod.Arr[66] = "a";',
  9090. '$mod.Arr[68] = $mod.Arr[$mod.c.charCodeAt()];',
  9091. '$mod.Arr[$mod.c.charCodeAt()] = $mod.Arr[100];',
  9092. '$mod.Arr[$mod.Arr[$mod.c.charCodeAt()].charCodeAt()] = $mod.Arr[65535];',
  9093. '$mod.b = $mod.Arr[0] === $mod.Arr[101];',
  9094. '$mod.c = "a";',
  9095. '$mod.c = "z";',
  9096. '$mod.Arr2[1] = "f";',
  9097. '$mod.Arr2[0] = $mod.Arr2[$mod.c.charCodeAt() - 97];',
  9098. '$mod.Arr2[$mod.c.charCodeAt() - 97] = $mod.Arr2[6];',
  9099. '']));
  9100. end;
  9101. procedure TTestModule.TestArray_StaticMultiDim;
  9102. begin
  9103. StartProgram(false);
  9104. Add([
  9105. 'type',
  9106. ' TArrayInt = array[1..3] of longint;',
  9107. ' TArrayArrayInt = array[5..6] of TArrayInt;',
  9108. 'var',
  9109. ' Arr: TArrayInt;',
  9110. ' Arr2: TArrayArrayInt;',
  9111. ' Arr3: array[boolean] of TArrayInt = ((11,12,13),(21,22,23));',
  9112. ' i: longint;',
  9113. 'begin',
  9114. ' i:=low(arr);',
  9115. ' i:=low(arr2);',
  9116. ' i:=low(arr2[5]);',
  9117. ' i:=high(arr);',
  9118. ' i:=high(arr2);',
  9119. ' i:=high(arr2[6]);',
  9120. ' arr2[5]:=arr;',
  9121. ' arr2[6][2]:=i;',
  9122. ' i:=arr2[6][3];',
  9123. ' arr2[6,3]:=i;',
  9124. ' i:=arr2[5,2];',
  9125. ' arr2:=arr2;',// clone multi dim static array
  9126. ' arr3:=arr3;',// clone anonymous multi dim static array
  9127. '']);
  9128. ConvertProgram;
  9129. CheckSource('TestArray_StaticMultiDim',
  9130. LinesToStr([ // statements
  9131. 'this.TArrayArrayInt$clone = function (a) {',
  9132. ' var r = [];',
  9133. ' for (var i = 0; i < 2; i++) r.push(a[i].slice(0));',
  9134. ' return r;',
  9135. '};',
  9136. 'this.Arr = rtl.arraySetLength(null, 0, 3);',
  9137. 'this.Arr2 = rtl.arraySetLength(null, 0, 2, 3);',
  9138. 'this.Arr3$a$clone = function (a) {',
  9139. ' var r = [];',
  9140. ' for (var i = 0; i < 2; i++) r.push(a[i].slice(0));',
  9141. ' return r;',
  9142. '};',
  9143. 'this.Arr3 = [[11, 12, 13], [21, 22, 23]];',
  9144. 'this.i = 0;'
  9145. ]),
  9146. LinesToStr([ // $mod.$main
  9147. '$mod.i = 1;',
  9148. '$mod.i = 5;',
  9149. '$mod.i = 1;',
  9150. '$mod.i = 3;',
  9151. '$mod.i = 6;',
  9152. '$mod.i = 3;',
  9153. '$mod.Arr2[0] = $mod.Arr.slice(0);',
  9154. '$mod.Arr2[1][1] = $mod.i;',
  9155. '$mod.i = $mod.Arr2[1][2];',
  9156. '$mod.Arr2[1][2] = $mod.i;',
  9157. '$mod.i = $mod.Arr2[0][1];',
  9158. '$mod.Arr2 = $mod.TArrayArrayInt$clone($mod.Arr2);',
  9159. '$mod.Arr3 = $mod.Arr3$a$clone($mod.Arr3);',
  9160. '']));
  9161. end;
  9162. procedure TTestModule.TestArray_StaticInFunction;
  9163. begin
  9164. StartProgram(false);
  9165. Add([
  9166. 'const TArrayInt = 3;',
  9167. 'const TArrayArrayInt = 4;',
  9168. 'procedure DoIt;',
  9169. 'type',
  9170. ' TArrayInt = array[1..3] of longint;',
  9171. ' TArrayArrayInt = array[5..6] of TArrayInt;',
  9172. 'var',
  9173. ' Arr: TArrayInt;',
  9174. ' Arr2: TArrayArrayInt;',
  9175. ' Arr3: array[boolean] of TArrayInt = ((11,12,13),(21,22,23));',
  9176. ' i: longint;',
  9177. 'begin',
  9178. ' arr2[5]:=arr;',
  9179. ' arr2:=arr2;',// clone multi dim static array
  9180. ' arr3:=arr3;',// clone multi dim anonymous static array
  9181. 'end;',
  9182. 'begin',
  9183. '']);
  9184. ConvertProgram;
  9185. CheckSource('TestArray_StaticInFunction',
  9186. LinesToStr([ // statements
  9187. 'this.TArrayInt = 3;',
  9188. 'this.TArrayArrayInt = 4;',
  9189. 'var TArrayArrayInt$1$clone = function (a) {',
  9190. ' var r = [];',
  9191. ' for (var i = 0; i < 2; i++) r.push(a[i].slice(0));',
  9192. ' return r;',
  9193. '};',
  9194. 'var Arr3$a$clone = function (a) {',
  9195. ' var r = [];',
  9196. ' for (var i = 0; i < 2; i++) r.push(a[i].slice(0));',
  9197. ' return r;',
  9198. '};',
  9199. 'this.DoIt = function () {',
  9200. ' var Arr = rtl.arraySetLength(null, 0, 3);',
  9201. ' var Arr2 = rtl.arraySetLength(null, 0, 2, 3);',
  9202. ' var Arr3 = [[11, 12, 13], [21, 22, 23]];',
  9203. ' var i = 0;',
  9204. ' Arr2[0] = Arr.slice(0);',
  9205. ' Arr2 = TArrayArrayInt$1$clone(Arr2);',
  9206. ' Arr3 = Arr3$a$clone(Arr3);',
  9207. '};',
  9208. '']),
  9209. LinesToStr([ // $mod.$main
  9210. '']));
  9211. end;
  9212. procedure TTestModule.TestArray_StaticMultiDimEqualNotImplemented;
  9213. begin
  9214. StartProgram(false);
  9215. Add([
  9216. 'type',
  9217. ' TArrayInt = array[1..3,1..2] of longint;',
  9218. 'var',
  9219. ' a,b: TArrayInt;',
  9220. 'begin',
  9221. ' if a=b then ;',
  9222. '']);
  9223. SetExpectedPasResolverError('compare static array is not supported',
  9224. nXIsNotSupported);
  9225. ConvertProgram;
  9226. end;
  9227. procedure TTestModule.TestArrayOfRecord;
  9228. begin
  9229. StartProgram(false);
  9230. Add([
  9231. 'type',
  9232. ' TRec = record',
  9233. ' Int: longint;',
  9234. ' end;',
  9235. ' TArrayRec = array of TRec;',
  9236. 'procedure DoIt(vd: TRec; const vc: TRec; var vv: TRec);',
  9237. 'begin',
  9238. 'end;',
  9239. 'var',
  9240. ' Arr: TArrayRec;',
  9241. ' r: TRec;',
  9242. ' i: longint;',
  9243. 'begin',
  9244. ' SetLength(arr,3);',
  9245. ' arr[0].int:=4;',
  9246. ' arr[1].int:=length(arr)+arr[2].int;',
  9247. ' arr[arr[i].int].int:=arr[5].int;',
  9248. ' arr[7]:=r;',
  9249. ' r:=arr[8];',
  9250. ' i:=low(arr);',
  9251. ' i:=high(arr);',
  9252. ' DoIt(Arr[9],Arr[10],Arr[11]);']);
  9253. ConvertProgram;
  9254. CheckSource('TestArrayOfRecord',
  9255. LinesToStr([ // statements
  9256. 'rtl.recNewT(this, "TRec", function () {',
  9257. ' this.Int = 0;',
  9258. ' this.$eq = function (b) {',
  9259. ' return this.Int === b.Int;',
  9260. ' };',
  9261. ' this.$assign = function (s) {',
  9262. ' this.Int = s.Int;',
  9263. ' return this;',
  9264. ' };',
  9265. '});',
  9266. 'this.DoIt = function (vd, vc, vv) {',
  9267. '};',
  9268. 'this.Arr = [];',
  9269. 'this.r = this.TRec.$new();',
  9270. 'this.i = 0;'
  9271. ]),
  9272. LinesToStr([ // $mod.$main
  9273. '$mod.Arr = rtl.arraySetLength($mod.Arr,$mod.TRec,3);',
  9274. '$mod.Arr[0].Int = 4;',
  9275. '$mod.Arr[1].Int = rtl.length($mod.Arr)+$mod.Arr[2].Int;',
  9276. '$mod.Arr[$mod.Arr[$mod.i].Int].Int = $mod.Arr[5].Int;',
  9277. '$mod.Arr[7].$assign($mod.r);',
  9278. '$mod.r.$assign($mod.Arr[8]);',
  9279. '$mod.i = 0;',
  9280. '$mod.i = rtl.length($mod.Arr)-1;',
  9281. '$mod.DoIt($mod.TRec.$clone($mod.Arr[9]), $mod.Arr[10], $mod.Arr[11]);',
  9282. '']));
  9283. end;
  9284. procedure TTestModule.TestArray_StaticRecord;
  9285. begin
  9286. StartProgram(false);
  9287. Add([
  9288. 'type',
  9289. ' TRec = record',
  9290. ' Int: longint;',
  9291. ' end;',
  9292. ' TArrayRec = array[1..2] of TRec;',
  9293. 'var',
  9294. ' Arr: TArrayRec;',
  9295. 'begin',
  9296. ' arr[1].int:=length(arr)+low(arr)+high(arr);',
  9297. '']);
  9298. ConvertProgram;
  9299. CheckSource('TestArray_StaticRecord',
  9300. LinesToStr([ // statements
  9301. 'rtl.recNewT(this, "TRec", function () {',
  9302. ' this.Int = 0;',
  9303. ' this.$eq = function (b) {',
  9304. ' return this.Int === b.Int;',
  9305. ' };',
  9306. ' this.$assign = function (s) {',
  9307. ' this.Int = s.Int;',
  9308. ' return this;',
  9309. ' };',
  9310. '});',
  9311. 'this.TArrayRec$clone = function (a) {',
  9312. ' var r = [];',
  9313. ' for (var i = 0; i < 2; i++) r.push($mod.TRec.$clone(a[i]));',
  9314. ' return r;',
  9315. '};',
  9316. 'this.Arr = rtl.arraySetLength(null, this.TRec, 2);',
  9317. '']),
  9318. LinesToStr([ // $mod.$main
  9319. '$mod.Arr[0].Int = 2 + 1 + 2;']));
  9320. end;
  9321. procedure TTestModule.TestArrayOfSet;
  9322. begin
  9323. StartProgram(false);
  9324. Add([
  9325. 'type',
  9326. ' TFlag = (big,small);',
  9327. ' TSetOfFlag = set of tflag;',
  9328. ' TArrayFlag = array of TSetOfFlag;',
  9329. 'procedure DoIt(const a: Tarrayflag);',
  9330. 'begin',
  9331. 'end;',
  9332. 'var',
  9333. ' f: TFlag;',
  9334. ' s: TSetOfFlag;',
  9335. ' Arr: TArrayFlag;',
  9336. ' i: longint;',
  9337. 'begin',
  9338. ' SetLength(arr,3);',
  9339. ' arr[0]:=s;',
  9340. ' arr[1]:=[big];',
  9341. ' arr[2]:=[big]+s;',
  9342. ' arr[3]:=s+[big];',
  9343. ' arr[4]:=arr[5];',
  9344. ' s:=arr[6];',
  9345. ' i:=low(arr);',
  9346. ' i:=high(arr);',
  9347. ' DoIt(arr);',
  9348. ' DoIt([s]);',
  9349. ' DoIt([[],s]);',
  9350. ' DoIt([s,[]]);',
  9351. '']);
  9352. ConvertProgram;
  9353. CheckSource('TestArrayOfSet',
  9354. LinesToStr([ // statements
  9355. 'this.TFlag = {',
  9356. ' "0": "big",',
  9357. ' big: 0,',
  9358. ' "1": "small",',
  9359. ' small: 1',
  9360. '};',
  9361. 'this.DoIt = function (a) {',
  9362. '};',
  9363. 'this.f = 0;',
  9364. 'this.s = {};',
  9365. 'this.Arr = [];',
  9366. 'this.i = 0;',
  9367. '']),
  9368. LinesToStr([ // $mod.$main
  9369. '$mod.Arr = rtl.arraySetLength($mod.Arr, {}, 3);',
  9370. '$mod.Arr[0] = rtl.refSet($mod.s);',
  9371. '$mod.Arr[1] = rtl.createSet($mod.TFlag.big);',
  9372. '$mod.Arr[2] = rtl.unionSet(rtl.createSet($mod.TFlag.big), $mod.s);',
  9373. '$mod.Arr[3] = rtl.unionSet($mod.s, rtl.createSet($mod.TFlag.big));',
  9374. '$mod.Arr[4] = rtl.refSet($mod.Arr[5]);',
  9375. '$mod.s = rtl.refSet($mod.Arr[6]);',
  9376. '$mod.i = 0;',
  9377. '$mod.i = rtl.length($mod.Arr) - 1;',
  9378. '$mod.DoIt($mod.Arr);',
  9379. '$mod.DoIt([rtl.refSet($mod.s)]);',
  9380. '$mod.DoIt([{}, rtl.refSet($mod.s)]);',
  9381. '$mod.DoIt([rtl.refSet($mod.s), {}]);',
  9382. '']));
  9383. end;
  9384. procedure TTestModule.TestArray_DynAsParam;
  9385. begin
  9386. StartProgram(false);
  9387. Add([
  9388. 'type integer = longint;',
  9389. 'type TArrInt = array of integer;',
  9390. 'procedure DoIt(vG: TArrInt; const vH: TArrInt; var vI: TArrInt);',
  9391. 'var vJ: TArrInt;',
  9392. 'begin',
  9393. ' vg:=vg;',
  9394. ' vj:=vh;',
  9395. ' vi:=vi;',
  9396. ' doit(vg,vg,vg);',
  9397. ' doit(vh,vh,vj);',
  9398. ' doit(vi,vi,vi);',
  9399. ' doit(vj,vj,vj);',
  9400. 'end;',
  9401. 'var i: TArrInt;',
  9402. 'begin',
  9403. ' doit(i,i,i);']);
  9404. ConvertProgram;
  9405. CheckSource('TestArray_DynAsParams',
  9406. LinesToStr([ // statements
  9407. 'this.DoIt = function (vG,vH,vI) {',
  9408. ' var vJ = [];',
  9409. ' vG = rtl.arrayRef(vG);',
  9410. ' vJ = rtl.arrayRef(vH);',
  9411. ' vI.set(rtl.arrayRef(vI.get()));',
  9412. ' $mod.DoIt(rtl.arrayRef(vG), vG, {',
  9413. ' get: function () {',
  9414. ' return vG;',
  9415. ' },',
  9416. ' set: function (v) {',
  9417. ' vG = v;',
  9418. ' }',
  9419. ' });',
  9420. ' $mod.DoIt(rtl.arrayRef(vH), vH, {',
  9421. ' get: function () {',
  9422. ' return vJ;',
  9423. ' },',
  9424. ' set: function (v) {',
  9425. ' vJ = v;',
  9426. ' }',
  9427. ' });',
  9428. ' $mod.DoIt(rtl.arrayRef(vI.get()), vI.get(), vI);',
  9429. ' $mod.DoIt(rtl.arrayRef(vJ), vJ, {',
  9430. ' get: function () {',
  9431. ' return vJ;',
  9432. ' },',
  9433. ' set: function (v) {',
  9434. ' vJ = v;',
  9435. ' }',
  9436. ' });',
  9437. '};',
  9438. 'this.i = [];'
  9439. ]),
  9440. LinesToStr([
  9441. '$mod.DoIt(rtl.arrayRef($mod.i),$mod.i,{',
  9442. ' p: $mod,',
  9443. ' get: function () {',
  9444. ' return this.p.i;',
  9445. ' },',
  9446. ' set: function (v) {',
  9447. ' this.p.i = v;',
  9448. ' }',
  9449. '});'
  9450. ]));
  9451. end;
  9452. procedure TTestModule.TestArray_StaticAsParam;
  9453. begin
  9454. StartProgram(false);
  9455. Add([
  9456. 'type integer = longint;',
  9457. 'type TArrInt = array[1..2] of integer;',
  9458. 'procedure DoIt(vG: TArrInt; const vH: TArrInt; var vI: TArrInt);',
  9459. 'var vJ: TArrInt;',
  9460. 'begin',
  9461. ' vg:=vg;',
  9462. ' vj:=vh;',
  9463. ' vi:=vi;',
  9464. ' doit(vg,vg,vg);',
  9465. ' doit(vh,vh,vj);',
  9466. ' doit(vi,vi,vi);',
  9467. ' doit(vj,vj,vj);',
  9468. 'end;',
  9469. 'var i: TArrInt;',
  9470. 'begin',
  9471. ' doit(i,i,i);']);
  9472. ConvertProgram;
  9473. CheckSource('TestArray_StaticAsParams',
  9474. LinesToStr([ // statements
  9475. 'this.DoIt = function (vG,vH,vI) {',
  9476. ' var vJ = rtl.arraySetLength(null, 0, 2);',
  9477. ' vG = vG.slice(0);',
  9478. ' vJ = vH.slice(0);',
  9479. ' vI.set(vI.get().slice(0));',
  9480. ' $mod.DoIt(vG.slice(0), vG, {',
  9481. ' get: function () {',
  9482. ' return vG;',
  9483. ' },',
  9484. ' set: function (v) {',
  9485. ' vG = v;',
  9486. ' }',
  9487. ' });',
  9488. ' $mod.DoIt(vH.slice(0), vH, {',
  9489. ' get: function () {',
  9490. ' return vJ;',
  9491. ' },',
  9492. ' set: function (v) {',
  9493. ' vJ = v;',
  9494. ' }',
  9495. ' });',
  9496. ' $mod.DoIt(vI.get().slice(0), vI.get(), vI);',
  9497. ' $mod.DoIt(vJ.slice(0), vJ, {',
  9498. ' get: function () {',
  9499. ' return vJ;',
  9500. ' },',
  9501. ' set: function (v) {',
  9502. ' vJ = v;',
  9503. ' }',
  9504. ' });',
  9505. '};',
  9506. 'this.i = rtl.arraySetLength(null, 0, 2);'
  9507. ]),
  9508. LinesToStr([
  9509. '$mod.DoIt($mod.i.slice(0),$mod.i,{',
  9510. ' p: $mod,',
  9511. ' get: function () {',
  9512. ' return this.p.i;',
  9513. ' },',
  9514. ' set: function (v) {',
  9515. ' this.p.i = v;',
  9516. ' }',
  9517. '});'
  9518. ]));
  9519. end;
  9520. procedure TTestModule.TestArrayElement_AsParams;
  9521. begin
  9522. StartProgram(false);
  9523. Add('type integer = longint;');
  9524. Add('type TArrayInt = array of integer;');
  9525. Add('procedure DoIt(vG: Integer; const vH: Integer; var vI: Integer);');
  9526. Add('var vJ: tarrayint;');
  9527. Add('begin');
  9528. Add(' vi:=vi;');
  9529. Add(' doit(vi,vi,vi);');
  9530. Add(' doit(vj[1+1],vj[1+2],vj[1+3]);');
  9531. Add('end;');
  9532. Add('var a: TArrayInt;');
  9533. Add('begin');
  9534. Add(' doit(a[1+4],a[1+5],a[1+6]);');
  9535. ConvertProgram;
  9536. CheckSource('TestArrayElement_AsParams',
  9537. LinesToStr([ // statements
  9538. 'this.DoIt = function (vG,vH,vI) {',
  9539. ' var vJ = [];',
  9540. ' vI.set(vI.get());',
  9541. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  9542. ' $mod.DoIt(vJ[1+1], vJ[1+2], {',
  9543. ' a:1+3,',
  9544. ' p:vJ,',
  9545. ' get: function () {',
  9546. ' return this.p[this.a];',
  9547. ' },',
  9548. ' set: function (v) {',
  9549. ' this.p[this.a] = v;',
  9550. ' }',
  9551. ' });',
  9552. '};',
  9553. 'this.a = [];'
  9554. ]),
  9555. LinesToStr([
  9556. '$mod.DoIt($mod.a[1+4],$mod.a[1+5],{',
  9557. ' a: 1+6,',
  9558. ' p: $mod.a,',
  9559. ' get: function () {',
  9560. ' return this.p[this.a];',
  9561. ' },',
  9562. ' set: function (v) {',
  9563. ' this.p[this.a] = v;',
  9564. ' }',
  9565. '});'
  9566. ]));
  9567. end;
  9568. procedure TTestModule.TestArrayElementFromFuncResult_AsParams;
  9569. begin
  9570. StartProgram(false);
  9571. Add('type Integer = longint;');
  9572. Add('type TArrayInt = array of integer;');
  9573. Add('function GetArr(vB: integer = 0): tarrayint;');
  9574. Add('begin');
  9575. Add('end;');
  9576. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  9577. Add('begin');
  9578. Add('end;');
  9579. Add('begin');
  9580. Add(' doit(getarr[1+1],getarr[1+2],getarr[1+3]);');
  9581. Add(' doit(getarr()[2+1],getarr()[2+2],getarr()[2+3]);');
  9582. Add(' doit(getarr(7)[3+1],getarr(8)[3+2],getarr(9)[3+3]);');
  9583. ConvertProgram;
  9584. CheckSource('TestArrayElementFromFuncResult_AsParams',
  9585. LinesToStr([ // statements
  9586. 'this.GetArr = function (vB) {',
  9587. ' var Result = [];',
  9588. ' return Result;',
  9589. '};',
  9590. 'this.DoIt = function (vG,vH,vI) {',
  9591. '};'
  9592. ]),
  9593. LinesToStr([
  9594. '$mod.DoIt($mod.GetArr(0)[1+1],$mod.GetArr(0)[1+2],{',
  9595. ' a: 1+3,',
  9596. ' p: $mod.GetArr(0),',
  9597. ' get: function () {',
  9598. ' return this.p[this.a];',
  9599. ' },',
  9600. ' set: function (v) {',
  9601. ' this.p[this.a] = v;',
  9602. ' }',
  9603. '});',
  9604. '$mod.DoIt($mod.GetArr(0)[2+1],$mod.GetArr(0)[2+2],{',
  9605. ' a: 2+3,',
  9606. ' p: $mod.GetArr(0),',
  9607. ' get: function () {',
  9608. ' return this.p[this.a];',
  9609. ' },',
  9610. ' set: function (v) {',
  9611. ' this.p[this.a] = v;',
  9612. ' }',
  9613. '});',
  9614. '$mod.DoIt($mod.GetArr(7)[3+1],$mod.GetArr(8)[3+2],{',
  9615. ' a: 3+3,',
  9616. ' p: $mod.GetArr(9),',
  9617. ' get: function () {',
  9618. ' return this.p[this.a];',
  9619. ' },',
  9620. ' set: function (v) {',
  9621. ' this.p[this.a] = v;',
  9622. ' }',
  9623. '});',
  9624. '']));
  9625. end;
  9626. procedure TTestModule.TestArrayEnumTypeRange;
  9627. begin
  9628. StartProgram(false);
  9629. Add([
  9630. 'type',
  9631. ' TEnum = (red,blue);',
  9632. ' TEnumArray = array[TEnum] of longint;',
  9633. 'var',
  9634. ' e: TEnum;',
  9635. ' i: longint;',
  9636. ' a: TEnumArray;',
  9637. ' numbers: TEnumArray = (1,2);',
  9638. ' names: array[TEnum] of string = (''red'',''blue'');',
  9639. 'begin',
  9640. ' e:=low(a);',
  9641. ' e:=high(a);',
  9642. ' i:=a[red];',
  9643. ' a[e]:=a[e];']);
  9644. ConvertProgram;
  9645. CheckSource('TestArrayEnumTypeRange',
  9646. LinesToStr([ // statements
  9647. ' this.TEnum = {',
  9648. ' "0": "red",',
  9649. ' red: 0,',
  9650. ' "1": "blue",',
  9651. ' blue: 1',
  9652. '};',
  9653. 'this.e = 0;',
  9654. 'this.i = 0;',
  9655. 'this.a = rtl.arraySetLength(null,0,2);',
  9656. 'this.numbers = [1, 2];',
  9657. 'this.names = ["red", "blue"];',
  9658. '']),
  9659. LinesToStr([ // $mod.$main
  9660. '$mod.e = $mod.TEnum.red;',
  9661. '$mod.e = $mod.TEnum.blue;',
  9662. '$mod.i = $mod.a[$mod.TEnum.red];',
  9663. '$mod.a[$mod.e] = $mod.a[$mod.e];',
  9664. '']));
  9665. end;
  9666. procedure TTestModule.TestArray_SetLengthOutArg;
  9667. begin
  9668. StartProgram(false);
  9669. Add([
  9670. 'type TArrInt = array of longint;',
  9671. 'procedure DoIt(out a: TArrInt);',
  9672. 'begin',
  9673. ' SetLength(a,2);',
  9674. 'end;',
  9675. 'begin',
  9676. '']);
  9677. ConvertProgram;
  9678. CheckSource('TestArray_SetLengthOutArg',
  9679. LinesToStr([ // statements
  9680. 'this.DoIt = function (a) {',
  9681. ' a.set(rtl.arraySetLength(a.get(), 0, 2));',
  9682. '};',
  9683. '']),
  9684. LinesToStr([
  9685. '']));
  9686. end;
  9687. procedure TTestModule.TestArray_SetLengthProperty;
  9688. begin
  9689. StartProgram(false);
  9690. Add('type');
  9691. Add(' TArrInt = array of longint;');
  9692. Add(' TObject = class');
  9693. Add(' function GetColors: TArrInt; external name ''GetColors'';');
  9694. Add(' procedure SetColors(const Value: TArrInt); external name ''SetColors'';');
  9695. Add(' property Colors: TArrInt read GetColors write SetColors;');
  9696. Add(' end;');
  9697. Add('var Obj: TObject;');
  9698. Add('begin');
  9699. Add(' SetLength(Obj.Colors,2);');
  9700. ConvertProgram;
  9701. CheckSource('TestArray_SetLengthProperty',
  9702. LinesToStr([ // statements
  9703. 'rtl.createClass(this, "TObject", null, function () {',
  9704. ' this.$init = function () {',
  9705. ' };',
  9706. ' this.$final = function () {',
  9707. ' };',
  9708. '});',
  9709. 'this.Obj = null;',
  9710. '']),
  9711. LinesToStr([
  9712. '$mod.Obj.SetColors(rtl.arraySetLength($mod.Obj.GetColors(), 0, 2));',
  9713. '']));
  9714. end;
  9715. procedure TTestModule.TestArray_SetLengthMultiDim;
  9716. begin
  9717. StartProgram(false);
  9718. Add([
  9719. 'type',
  9720. ' TArrArrInt = array of array of longint;',
  9721. ' TArrStaInt = array of array[1..2] of longint;',
  9722. 'var',
  9723. ' a: TArrArrInt;',
  9724. ' b: TArrStaInt;',
  9725. 'begin',
  9726. ' SetLength(a,2);',
  9727. ' SetLength(a,3,4);',
  9728. ' SetLength(b,5);',
  9729. '']);
  9730. ConvertProgram;
  9731. CheckSource('TestArray_SetLengthMultiDim',
  9732. LinesToStr([ // statements
  9733. 'this.a = [];',
  9734. 'this.b = [];',
  9735. '']),
  9736. LinesToStr([
  9737. '$mod.a = rtl.arraySetLength($mod.a, [], 2);',
  9738. '$mod.a = rtl.arraySetLength($mod.a, 0, 3, 4);',
  9739. '$mod.b = rtl.arraySetLength($mod.b, 0, 5, "s", 2);',
  9740. '']));
  9741. end;
  9742. procedure TTestModule.TestArray_SetLengthDynOfStatic;
  9743. begin
  9744. StartProgram(false);
  9745. Add([
  9746. 'type',
  9747. ' TStaArr1 = array[1..3] of boolean;',
  9748. //' TStaArr2 = array[5..6] of TStaArr1;',
  9749. ' TDynArr1StaArr1 = array of TStaArr1;',
  9750. //' TDynArr1StaArr2 = array of TStaArr2;',
  9751. ' TDynArr2StaArr1 = array of TDynArr1StaArr1;',
  9752. //' TDynArr2StaArr2 = array of TDynArr1StaArr2;',
  9753. 'var',
  9754. ' DynArr1StaArr1: TDynArr1StaArr1;',
  9755. //' DynArr1StaArr2: TDynArr1StaArr1;',
  9756. ' DynArr2StaArr1: TDynArr2StaArr1;',
  9757. //' DynArr2StaArr2: TDynArr2StaArr2;',
  9758. 'begin',
  9759. ' SetLength(DynArr1StaArr1,11);',
  9760. ' SetLength(DynArr2StaArr1,12);',
  9761. ' SetLength(DynArr2StaArr1[13],14);',
  9762. ' SetLength(DynArr2StaArr1,15,16);',
  9763. //' SetLength(DynArr1StaArr2,21);',
  9764. //' SetLength(DynArr2StaArr2,22);',
  9765. //' SetLength(DynArr2StaArr2[23],24);',
  9766. //' SetLength(DynArr2StaArr2,25,26);',
  9767. '']);
  9768. ConvertProgram;
  9769. CheckSource('TestArray_DynOfStatic',
  9770. LinesToStr([ // statements
  9771. 'this.DynArr1StaArr1 = [];',
  9772. 'this.DynArr2StaArr1 = [];',
  9773. '']),
  9774. LinesToStr([ // $mod.$main
  9775. '$mod.DynArr1StaArr1 = rtl.arraySetLength($mod.DynArr1StaArr1, false, 11, "s", 3);',
  9776. '$mod.DynArr2StaArr1 = rtl.arraySetLength($mod.DynArr2StaArr1, [], 12);',
  9777. '$mod.DynArr2StaArr1[13] = rtl.arraySetLength($mod.DynArr2StaArr1[13], false, 14, "s", 3);',
  9778. '$mod.DynArr2StaArr1 = rtl.arraySetLength(',
  9779. ' $mod.DynArr2StaArr1,',
  9780. ' false,',
  9781. ' 15,',
  9782. ' 16,',
  9783. ' "s",',
  9784. ' 3',
  9785. ');',
  9786. '']));
  9787. end;
  9788. procedure TTestModule.TestArray_OpenArrayOfString;
  9789. begin
  9790. StartProgram(false);
  9791. Add('procedure DoIt(const a: array of String);');
  9792. Add('var');
  9793. Add(' i: longint;');
  9794. Add(' s: string;');
  9795. Add('begin');
  9796. Add(' for i:=low(a) to high(a) do s:=a[length(a)-i-1];');
  9797. Add('end;');
  9798. Add('var s: string;');
  9799. Add('begin');
  9800. Add(' DoIt([]);');
  9801. Add(' DoIt([s,''foo'','''',s+s]);');
  9802. ConvertProgram;
  9803. CheckSource('TestArray_OpenArrayOfString',
  9804. LinesToStr([ // statements
  9805. 'this.DoIt = function (a) {',
  9806. ' var i = 0;',
  9807. ' var s = "";',
  9808. ' for (var $l = 0, $end = rtl.length(a) - 1; $l <= $end; $l++) {',
  9809. ' i = $l;',
  9810. ' s = a[rtl.length(a) - i - 1];',
  9811. ' };',
  9812. '};',
  9813. 'this.s = "";',
  9814. '']),
  9815. LinesToStr([
  9816. '$mod.DoIt([]);',
  9817. '$mod.DoIt([$mod.s, "foo", "", $mod.s + $mod.s]);',
  9818. '']));
  9819. end;
  9820. procedure TTestModule.TestArray_ArrayOfCharAssignString;
  9821. begin
  9822. StartProgram(false);
  9823. Add([
  9824. 'type TArr = array of char;',
  9825. 'var',
  9826. ' c: char;',
  9827. ' s: string;',
  9828. ' a: TArr;',
  9829. 'procedure Run(const a: array of char);',
  9830. 'begin',
  9831. ' Run(c);',
  9832. ' Run(s);',
  9833. 'end;',
  9834. 'begin',
  9835. ' a:=c;',
  9836. ' a:=s;',
  9837. ' a:=#13;',
  9838. ' a:=''Foo'';',
  9839. ' Run(c);',
  9840. ' Run(s);',
  9841. '']);
  9842. ConvertProgram;
  9843. CheckSource('TestArray_ArrayOfCharAssignString',
  9844. LinesToStr([ // statements
  9845. 'this.c = "";',
  9846. 'this.s = "";',
  9847. 'this.a = [];',
  9848. 'this.Run = function (a) {',
  9849. ' $mod.Run($mod.c.split(""));',
  9850. ' $mod.Run($mod.s.split(""));',
  9851. '};',
  9852. '']),
  9853. LinesToStr([
  9854. '$mod.a = $mod.c.split("");',
  9855. '$mod.a = $mod.s.split("");',
  9856. '$mod.a = "\r".split("");',
  9857. '$mod.a = "Foo".split("");',
  9858. '$mod.Run($mod.c.split(""));',
  9859. '$mod.Run($mod.s.split(""));',
  9860. '']));
  9861. end;
  9862. procedure TTestModule.TestArray_ConstRef;
  9863. begin
  9864. StartProgram(false);
  9865. Add([
  9866. 'type TArr = array of word;',
  9867. 'procedure Run(constref a: TArr);',
  9868. 'begin',
  9869. 'end;',
  9870. 'procedure Fly(a: TArr; var b: TArr; out c: TArr; const d: TArr; constref e: TArr);',
  9871. 'var l: TArr;',
  9872. 'begin',
  9873. ' Run(l);',
  9874. ' Run(a);',
  9875. ' Run(b);',
  9876. ' Run(c);',
  9877. ' Run(d);',
  9878. ' Run(e);',
  9879. 'end;',
  9880. 'begin',
  9881. '']);
  9882. ConvertProgram;
  9883. CheckResolverUnexpectedHints();
  9884. CheckSource('TestArray_ConstRef',
  9885. LinesToStr([ // statements
  9886. 'this.Run = function (a) {',
  9887. '};',
  9888. 'this.Fly = function (a, b, c, d, e) {',
  9889. ' var l = [];',
  9890. ' $mod.Run(l);',
  9891. ' $mod.Run(a);',
  9892. ' $mod.Run(b.get());',
  9893. ' $mod.Run(c.get());',
  9894. ' $mod.Run(d);',
  9895. ' $mod.Run(e);',
  9896. '};',
  9897. '']),
  9898. LinesToStr([
  9899. '']));
  9900. end;
  9901. procedure TTestModule.TestArray_Concat;
  9902. begin
  9903. StartProgram(false);
  9904. Add([
  9905. 'type',
  9906. ' integer = longint;',
  9907. ' TFlag = (big,small);',
  9908. ' TFlags = set of TFlag;',
  9909. ' TRec = record',
  9910. ' i: integer;',
  9911. ' end;',
  9912. ' TArrInt = array of integer;',
  9913. ' TArrRec = array of TRec;',
  9914. ' TArrFlag = array of TFlag;',
  9915. ' TArrSet = array of TFlags;',
  9916. ' TArrJSValue = array of jsvalue;',
  9917. 'var',
  9918. ' ArrInt: tarrint;',
  9919. ' ArrRec: tarrrec;',
  9920. ' ArrFlag: tarrflag;',
  9921. ' ArrSet: tarrset;',
  9922. ' ArrJSValue: tarrjsvalue;',
  9923. 'begin',
  9924. ' arrint:=concat(arrint);',
  9925. ' arrint:=concat(arrint,arrint);',
  9926. ' arrint:=concat(arrint,arrint,arrint);',
  9927. ' arrrec:=concat(arrrec);',
  9928. ' arrrec:=concat(arrrec,arrrec);',
  9929. ' arrrec:=concat(arrrec,arrrec,arrrec);',
  9930. ' arrset:=concat(arrset);',
  9931. ' arrset:=concat(arrset,arrset);',
  9932. ' arrset:=concat(arrset,arrset,arrset);',
  9933. ' arrjsvalue:=concat(arrjsvalue);',
  9934. ' arrjsvalue:=concat(arrjsvalue,arrjsvalue);',
  9935. ' arrjsvalue:=concat(arrjsvalue,arrjsvalue,arrjsvalue);',
  9936. ' arrint:=concat([1],arrint);',
  9937. ' arrflag:=concat([big]);',
  9938. ' arrflag:=concat([big],arrflag);',
  9939. ' arrflag:=concat(arrflag,[small]);',
  9940. '']);
  9941. ConvertProgram;
  9942. CheckSource('TestArray_Concat',
  9943. LinesToStr([ // statements
  9944. 'this.TFlag = {',
  9945. ' "0": "big",',
  9946. ' big: 0,',
  9947. ' "1": "small",',
  9948. ' small: 1',
  9949. '};',
  9950. 'rtl.recNewT(this, "TRec", function () {',
  9951. ' this.i = 0;',
  9952. ' this.$eq = function (b) {',
  9953. ' return this.i === b.i;',
  9954. ' };',
  9955. ' this.$assign = function (s) {',
  9956. ' this.i = s.i;',
  9957. ' return this;',
  9958. ' };',
  9959. '});',
  9960. 'this.ArrInt = [];',
  9961. 'this.ArrRec = [];',
  9962. 'this.ArrFlag = [];',
  9963. 'this.ArrSet = [];',
  9964. 'this.ArrJSValue = [];',
  9965. '']),
  9966. LinesToStr([ // $mod.$main
  9967. '$mod.ArrInt = rtl.arrayRef($mod.ArrInt);',
  9968. '$mod.ArrInt = rtl.arrayConcatN($mod.ArrInt, $mod.ArrInt);',
  9969. '$mod.ArrInt = rtl.arrayConcatN($mod.ArrInt, $mod.ArrInt, $mod.ArrInt);',
  9970. '$mod.ArrRec = rtl.arrayRef($mod.ArrRec);',
  9971. '$mod.ArrRec = rtl.arrayConcat($mod.TRec, $mod.ArrRec, $mod.ArrRec);',
  9972. '$mod.ArrRec = rtl.arrayConcat($mod.TRec, $mod.ArrRec, $mod.ArrRec, $mod.ArrRec);',
  9973. '$mod.ArrSet = rtl.arrayRef($mod.ArrSet);',
  9974. '$mod.ArrSet = rtl.arrayConcat("refSet", $mod.ArrSet, $mod.ArrSet);',
  9975. '$mod.ArrSet = rtl.arrayConcat("refSet", $mod.ArrSet, $mod.ArrSet, $mod.ArrSet);',
  9976. '$mod.ArrJSValue = rtl.arrayRef($mod.ArrJSValue);',
  9977. '$mod.ArrJSValue = rtl.arrayConcatN($mod.ArrJSValue, $mod.ArrJSValue);',
  9978. '$mod.ArrJSValue = rtl.arrayConcatN($mod.ArrJSValue, $mod.ArrJSValue, $mod.ArrJSValue);',
  9979. '$mod.ArrInt = rtl.arrayConcatN([1], $mod.ArrInt);',
  9980. '$mod.ArrFlag = [$mod.TFlag.big];',
  9981. '$mod.ArrFlag = rtl.arrayConcatN([$mod.TFlag.big], $mod.ArrFlag);',
  9982. '$mod.ArrFlag = rtl.arrayConcatN($mod.ArrFlag, [$mod.TFlag.small]);',
  9983. '']));
  9984. end;
  9985. procedure TTestModule.TestArray_Copy;
  9986. begin
  9987. StartProgram(false);
  9988. Add([
  9989. 'type',
  9990. ' integer = longint;',
  9991. ' TFlag = (big,small);',
  9992. ' TFlags = set of TFlag;',
  9993. ' TRec = record',
  9994. ' i: integer;',
  9995. ' end;',
  9996. ' TArrInt = array of integer;',
  9997. ' TArrRec = array of TRec;',
  9998. ' TArrSet = array of TFlags;',
  9999. ' TArrJSValue = array of jsvalue;',
  10000. 'var',
  10001. ' ArrInt: tarrint;',
  10002. ' ArrRec: tarrrec;',
  10003. ' ArrSet: tarrset;',
  10004. ' ArrJSValue: tarrjsvalue;',
  10005. 'begin',
  10006. ' arrint:=copy(arrint);',
  10007. ' arrint:=copy(arrint,2);',
  10008. ' arrint:=copy(arrint,3,4);',
  10009. ' arrint:=copy([1,1],1,2);',
  10010. ' arrrec:=copy(arrrec);',
  10011. ' arrrec:=copy(arrrec,5);',
  10012. ' arrrec:=copy(arrrec,6,7);',
  10013. ' arrset:=copy(arrset);',
  10014. ' arrset:=copy(arrset,8);',
  10015. ' arrset:=copy(arrset,9,10);',
  10016. ' arrjsvalue:=copy(arrjsvalue);',
  10017. ' arrjsvalue:=copy(arrjsvalue,11);',
  10018. ' arrjsvalue:=copy(arrjsvalue,12,13);',
  10019. ' ']);
  10020. ConvertProgram;
  10021. CheckSource('TestArray_Copy',
  10022. LinesToStr([ // statements
  10023. 'this.TFlag = {',
  10024. ' "0": "big",',
  10025. ' big: 0,',
  10026. ' "1": "small",',
  10027. ' small: 1',
  10028. '};',
  10029. 'rtl.recNewT(this, "TRec", function () {',
  10030. ' this.i = 0;',
  10031. ' this.$eq = function (b) {',
  10032. ' return this.i === b.i;',
  10033. ' };',
  10034. ' this.$assign = function (s) {',
  10035. ' this.i = s.i;',
  10036. ' return this;',
  10037. ' };',
  10038. '});',
  10039. 'this.ArrInt = [];',
  10040. 'this.ArrRec = [];',
  10041. 'this.ArrSet = [];',
  10042. 'this.ArrJSValue = [];',
  10043. '']),
  10044. LinesToStr([ // $mod.$main
  10045. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 0);',
  10046. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 2);',
  10047. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 3, 4);',
  10048. '$mod.ArrInt = rtl.arrayCopy(0, [1, 1], 1, 2);',
  10049. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 0);',
  10050. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 5);',
  10051. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 6, 7);',
  10052. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 0);',
  10053. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 8);',
  10054. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 9, 10);',
  10055. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 0);',
  10056. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 11);',
  10057. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 12, 13);',
  10058. '']));
  10059. end;
  10060. procedure TTestModule.TestArray_InsertDelete;
  10061. begin
  10062. StartProgram(false);
  10063. Add([
  10064. 'type',
  10065. ' integer = longint;',
  10066. ' TFlag = (big,small);',
  10067. ' TFlags = set of TFlag;',
  10068. ' TRec = record',
  10069. ' i: integer;',
  10070. ' end;',
  10071. ' TArrInt = array of integer;',
  10072. ' TArrRec = array of TRec;',
  10073. ' TArrSet = array of TFlags;',
  10074. ' TArrJSValue = array of jsvalue;',
  10075. ' TArrArrInt = array of TArrInt;',
  10076. 'var',
  10077. ' ArrInt: tarrint;',
  10078. ' ArrRec: tarrrec;',
  10079. ' ArrSet: tarrset;',
  10080. ' ArrJSValue: tarrjsvalue;',
  10081. ' ArrArrInt: TArrArrInt;',
  10082. 'begin',
  10083. ' Insert(1,arrint,2);',
  10084. ' Insert(arrint[3],arrint,4);',
  10085. ' Insert(arrrec[5],arrrec,6);',
  10086. ' Insert(arrset[7],arrset,7);',
  10087. ' Insert(arrjsvalue[8],arrjsvalue,9);',
  10088. ' Insert(10,arrjsvalue,11);',
  10089. ' Insert([23],arrarrint,22);',
  10090. ' Delete(arrint,12,13);',
  10091. ' Delete(arrrec,14,15);',
  10092. ' Delete(arrset,17,18);',
  10093. ' Delete(arrjsvalue,19,10);']);
  10094. ConvertProgram;
  10095. CheckSource('TestArray_InsertDelete',
  10096. LinesToStr([ // statements
  10097. 'this.TFlag = {',
  10098. ' "0": "big",',
  10099. ' big: 0,',
  10100. ' "1": "small",',
  10101. ' small: 1',
  10102. '};',
  10103. 'rtl.recNewT(this, "TRec", function () {',
  10104. ' this.i = 0;',
  10105. ' this.$eq = function (b) {',
  10106. ' return this.i === b.i;',
  10107. ' };',
  10108. ' this.$assign = function (s) {',
  10109. ' this.i = s.i;',
  10110. ' return this;',
  10111. ' };',
  10112. '});',
  10113. 'this.ArrInt = [];',
  10114. 'this.ArrRec = [];',
  10115. 'this.ArrSet = [];',
  10116. 'this.ArrJSValue = [];',
  10117. 'this.ArrArrInt = [];',
  10118. '']),
  10119. LinesToStr([ // $mod.$main
  10120. '$mod.ArrInt = rtl.arrayInsert(1, $mod.ArrInt, 2);',
  10121. '$mod.ArrInt = rtl.arrayInsert($mod.ArrInt[3], $mod.ArrInt, 4);',
  10122. '$mod.ArrRec = rtl.arrayInsert($mod.ArrRec[5], $mod.ArrRec, 6);',
  10123. '$mod.ArrSet = rtl.arrayInsert($mod.ArrSet[7], $mod.ArrSet, 7);',
  10124. '$mod.ArrJSValue = rtl.arrayInsert($mod.ArrJSValue[8], $mod.ArrJSValue, 9);',
  10125. '$mod.ArrJSValue = rtl.arrayInsert(10, $mod.ArrJSValue, 11);',
  10126. '$mod.ArrArrInt = rtl.arrayInsert([23], $mod.ArrArrInt, 22);',
  10127. '$mod.ArrInt.splice(12, 13);',
  10128. '$mod.ArrRec.splice(14, 15);',
  10129. '$mod.ArrSet.splice(17, 18);',
  10130. '$mod.ArrJSValue.splice(19, 10);',
  10131. '']));
  10132. end;
  10133. procedure TTestModule.TestArray_DynArrayConstObjFPC;
  10134. begin
  10135. Parser.Options:=Parser.Options+[po_cassignments];
  10136. StartProgram(false);
  10137. Add([
  10138. '{$modeswitch arrayoperators}',
  10139. 'type',
  10140. ' integer = longint;',
  10141. ' TArrInt = array of integer;',
  10142. ' TArrStr = array of string;',
  10143. 'const',
  10144. ' Ints: TArrInt = (1,2,3);',
  10145. ' Aliases: TarrStr = (''foo'',''b'');',
  10146. ' OneInt: TArrInt = (7);',
  10147. ' OneStr: array of integer = (7);',
  10148. ' Chars: array of char = ''aoc'';',
  10149. ' Names: array of string = (''a'',''foo'');',
  10150. ' NameCount = low(Names)+high(Names)+length(Names);',
  10151. 'var i: integer;',
  10152. 'begin',
  10153. ' Ints:=[];',
  10154. ' Ints:=[1,1];',
  10155. ' Ints:=[1]+[2];',
  10156. ' Ints:=[2];',
  10157. ' Ints:=[]+ints;',
  10158. ' Ints:=Ints+[];',
  10159. ' Ints:=Ints+OneInt;',
  10160. ' Ints:=Ints+[1,1];',
  10161. ' Ints:=[i,i]+Ints;',
  10162. ' Ints:=[1]+[i]+[3];',
  10163. '']);
  10164. ConvertProgram;
  10165. CheckSource('TestArray_DynArrayConstObjFPC',
  10166. LinesToStr([ // statements
  10167. 'this.Ints = [1, 2, 3];',
  10168. 'this.Aliases = ["foo", "b"];',
  10169. 'this.OneInt = [7];',
  10170. 'this.OneStr = [7];',
  10171. 'this.Chars = ["a", "o", "c"];',
  10172. 'this.Names = ["a", "foo"];',
  10173. 'this.NameCount = 0 + (rtl.length(this.Names) - 1) + rtl.length(this.Names);',
  10174. 'this.i = 0;',
  10175. '']),
  10176. LinesToStr([ // $mod.$main
  10177. '$mod.Ints = [];',
  10178. '$mod.Ints = [1, 1];',
  10179. '$mod.Ints = rtl.arrayConcatN([1], [2]);',
  10180. '$mod.Ints = [2];',
  10181. '$mod.Ints = rtl.arrayConcatN([], $mod.Ints);',
  10182. '$mod.Ints = rtl.arrayConcatN($mod.Ints, []);',
  10183. '$mod.Ints = rtl.arrayConcatN($mod.Ints, $mod.OneInt);',
  10184. '$mod.Ints = rtl.arrayConcatN($mod.Ints, [1, 1]);',
  10185. '$mod.Ints = rtl.arrayConcatN([$mod.i, $mod.i], $mod.Ints);',
  10186. '$mod.Ints = rtl.arrayConcatN(rtl.arrayConcatN([1], [$mod.i]), [3]);',
  10187. '']));
  10188. end;
  10189. procedure TTestModule.TestArray_DynArrayConstDelphi;
  10190. begin
  10191. StartProgram(false);
  10192. // Note: const c = [1,1]; defines a set!
  10193. Add([
  10194. '{$mode delphi}',
  10195. 'type',
  10196. ' integer = longint;',
  10197. ' TArrInt = array of integer;',
  10198. ' TArrStr = array of string;',
  10199. 'const',
  10200. ' Ints: TArrInt = [1,1,2];',
  10201. ' Aliases: TarrStr = [''foo'',''b''];',
  10202. ' OneInt: TArrInt = [7];',
  10203. ' OneStr: array of integer = [7]+[8];',
  10204. ' Chars: array of char = ''aoc'';',
  10205. ' Names: array of string = [''a'',''a''];',
  10206. ' NameCount = low(Names)+high(Names)+length(Names);',
  10207. 'begin',
  10208. '']);
  10209. ConvertProgram;
  10210. CheckSource('TestArray_DynArrayConstDelphi',
  10211. LinesToStr([ // statements
  10212. 'this.Ints = [1, 1, 2];',
  10213. 'this.Aliases = ["foo", "b"];',
  10214. 'this.OneInt = [7];',
  10215. 'this.OneStr = rtl.arrayConcatN([7],[8]);',
  10216. 'this.Chars = ["a", "o", "c"];',
  10217. 'this.Names = ["a", "a"];',
  10218. 'this.NameCount = 0 + (rtl.length(this.Names) - 1) + rtl.length(this.Names);',
  10219. '']),
  10220. LinesToStr([ // $mod.$main
  10221. '']));
  10222. end;
  10223. procedure TTestModule.TestArray_ArrayLitAsParam;
  10224. begin
  10225. StartProgram(false);
  10226. Add([
  10227. '{$modeswitch arrayoperators}',
  10228. 'type',
  10229. ' integer = longint;',
  10230. ' TArrInt = array of integer;',
  10231. ' TArrSet = array of (red,green,blue);',
  10232. 'procedure DoOpenInt(const a: array of integer); forward;',
  10233. 'procedure DoInt(const a: TArrInt);',
  10234. 'begin',
  10235. ' DoInt(a+[1]);',
  10236. ' DoInt([1]+a);',
  10237. ' DoOpenInt(a);',
  10238. ' DoOpenInt(a+[1]);',
  10239. ' DoOpenInt([1]+a);',
  10240. 'end;',
  10241. 'procedure DoOpenInt(const a: array of integer);',
  10242. 'begin',
  10243. ' DoOpenInt(a+[1]);',
  10244. ' DoOpenInt([1]+a);',
  10245. ' DoInt(a);',
  10246. ' DoInt(a+[1]);',
  10247. ' DoInt([1]+a);',
  10248. 'end;',
  10249. 'procedure DoSet(const a: TArrSet);',
  10250. 'begin',
  10251. ' DoSet(a+[red]);',
  10252. ' DoSet([blue]+a);',
  10253. 'end;',
  10254. 'var',
  10255. ' i: TArrInt;',
  10256. ' s: TArrSet;',
  10257. 'begin',
  10258. ' DoInt([1]);',
  10259. ' DoInt([1]+[2]);',
  10260. ' DoInt(i+[1]);',
  10261. ' DoInt([1]+i);',
  10262. ' DoOpenInt([1]);',
  10263. ' DoOpenInt([1]+[2]);',
  10264. ' DoOpenInt(i+[1]);',
  10265. ' DoOpenInt([1]+i);',
  10266. ' DoSet([red]);',
  10267. ' DoSet([blue]+[green]);',
  10268. ' DoSet(s+[blue]);',
  10269. ' DoSet([red]+s);',
  10270. '']);
  10271. ConvertProgram;
  10272. CheckSource('TestArray_ArrayLitAsParam',
  10273. LinesToStr([ // statements
  10274. 'this.TArrSet$a = {',
  10275. ' "0": "red",',
  10276. ' red: 0,',
  10277. ' "1": "green",',
  10278. ' green: 1,',
  10279. ' "2": "blue",',
  10280. ' blue: 2',
  10281. '};',
  10282. 'this.DoInt = function (a) {',
  10283. ' $mod.DoInt(rtl.arrayConcatN(a, [1]));',
  10284. ' $mod.DoInt(rtl.arrayConcatN([1], a));',
  10285. ' $mod.DoOpenInt(a);',
  10286. ' $mod.DoOpenInt(rtl.arrayConcatN(a, [1]));',
  10287. ' $mod.DoOpenInt(rtl.arrayConcatN([1], a));',
  10288. '};',
  10289. 'this.DoOpenInt = function (a) {',
  10290. ' $mod.DoOpenInt(rtl.arrayConcatN(a, [1]));',
  10291. ' $mod.DoOpenInt(rtl.arrayConcatN([1], a));',
  10292. ' $mod.DoInt(a);',
  10293. ' $mod.DoInt(rtl.arrayConcatN(a, [1]));',
  10294. ' $mod.DoInt(rtl.arrayConcatN([1], a));',
  10295. '};',
  10296. 'this.DoSet = function (a) {',
  10297. ' $mod.DoSet(rtl.arrayConcatN(a, [$mod.TArrSet$a.red]));',
  10298. ' $mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.blue], a));',
  10299. '};',
  10300. 'this.i = [];',
  10301. 'this.s = [];',
  10302. '']),
  10303. LinesToStr([ // $mod.$main
  10304. '$mod.DoInt([1]);',
  10305. '$mod.DoInt(rtl.arrayConcatN([1], [2]));',
  10306. '$mod.DoInt(rtl.arrayConcatN($mod.i, [1]));',
  10307. '$mod.DoInt(rtl.arrayConcatN([1], $mod.i));',
  10308. '$mod.DoOpenInt([1]);',
  10309. '$mod.DoOpenInt(rtl.arrayConcatN([1], [2]));',
  10310. '$mod.DoOpenInt(rtl.arrayConcatN($mod.i, [1]));',
  10311. '$mod.DoOpenInt(rtl.arrayConcatN([1], $mod.i));',
  10312. '$mod.DoSet([$mod.TArrSet$a.red]);',
  10313. '$mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.blue], [$mod.TArrSet$a.green]));',
  10314. '$mod.DoSet(rtl.arrayConcatN($mod.s, [$mod.TArrSet$a.blue]));',
  10315. '$mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.red], $mod.s));',
  10316. '']));
  10317. end;
  10318. procedure TTestModule.TestArray_ArrayLitMultiDimAsParam;
  10319. begin
  10320. StartProgram(false);
  10321. Add([
  10322. '{$modeswitch arrayoperators}',
  10323. 'type',
  10324. ' integer = longint;',
  10325. ' TArrInt = array of integer;',
  10326. ' TArrArrInt = array of TArrInt;',
  10327. 'procedure DoInt(const a: TArrArrInt);',
  10328. 'begin',
  10329. ' DoInt(a+[[1]]);',
  10330. ' DoInt([[1]]+a);',
  10331. ' DoInt(a);',
  10332. 'end;',
  10333. 'var',
  10334. ' i: TArrInt;',
  10335. ' a: TArrArrInt;',
  10336. 'begin',
  10337. ' a:=[[1]];',
  10338. ' a:=[i];',
  10339. ' a:=a+[i];',
  10340. ' a:=[i]+a;',
  10341. ' a:=[[1]+i];',
  10342. ' a:=[[1]+[2]];',
  10343. ' a:=[i+[2]];',
  10344. ' DoInt([[1]]);',
  10345. ' DoInt([[1]+[2],[3,4],[5]]);',
  10346. ' DoInt([i+[1]]+a);',
  10347. ' DoInt([i]+a);',
  10348. '']);
  10349. ConvertProgram;
  10350. CheckSource('TestArray_ArrayLitMultiDimAsParam',
  10351. LinesToStr([ // statements
  10352. 'this.DoInt = function (a) {',
  10353. ' $mod.DoInt(rtl.arrayConcatN(a, [[1]]));',
  10354. ' $mod.DoInt(rtl.arrayConcatN([[1]], a));',
  10355. ' $mod.DoInt(a);',
  10356. '};',
  10357. 'this.i = [];',
  10358. 'this.a = [];',
  10359. '']),
  10360. LinesToStr([ // $mod.$main
  10361. '$mod.a = [[1]];',
  10362. '$mod.a = [$mod.i];',
  10363. '$mod.a = rtl.arrayConcatN($mod.a, [$mod.i]);',
  10364. '$mod.a = rtl.arrayConcatN([$mod.i], $mod.a);',
  10365. '$mod.a = [rtl.arrayConcatN([1], $mod.i)];',
  10366. '$mod.a = [rtl.arrayConcatN([1], [2])];',
  10367. '$mod.a = [rtl.arrayConcatN($mod.i, [2])];',
  10368. '$mod.DoInt([[1]]);',
  10369. '$mod.DoInt([rtl.arrayConcatN([1], [2]), [3, 4], [5]]);',
  10370. '$mod.DoInt(rtl.arrayConcatN([rtl.arrayConcatN($mod.i, [1])], $mod.a));',
  10371. '$mod.DoInt(rtl.arrayConcatN([$mod.i], $mod.a));',
  10372. '']));
  10373. end;
  10374. procedure TTestModule.TestArray_ArrayLitStaticAsParam;
  10375. begin
  10376. StartProgram(false);
  10377. Add([
  10378. '{$modeswitch arrayoperators}',
  10379. 'type',
  10380. ' integer = longint;',
  10381. ' TArrInt = array[1..2] of integer;',
  10382. ' TArrArrInt = array of TArrInt;',
  10383. 'procedure DoInt(const a: TArrArrInt);',
  10384. 'begin',
  10385. ' DoInt(a+[[1,2]]);',
  10386. ' DoInt([[1,2]]+a);',
  10387. ' DoInt(a);',
  10388. 'end;',
  10389. 'var',
  10390. ' i: TArrInt;',
  10391. ' a: TArrArrInt;',
  10392. 'begin',
  10393. ' a:=[[1,1]];',
  10394. ' a:=[i];',
  10395. ' a:=a+[i];',
  10396. ' a:=[i]+a;',
  10397. ' DoInt([[1,1]]);',
  10398. ' DoInt([[1,2],[3,4]]);',
  10399. '']);
  10400. ConvertProgram;
  10401. CheckSource('TestArray_ArrayLitStaticAsParam',
  10402. LinesToStr([ // statements
  10403. 'this.DoInt = function (a) {',
  10404. ' $mod.DoInt(rtl.arrayConcatN(a, [[1, 2]]));',
  10405. ' $mod.DoInt(rtl.arrayConcatN([[1, 2]], a));',
  10406. ' $mod.DoInt(a);',
  10407. '};',
  10408. 'this.i = rtl.arraySetLength(null, 0, 2);',
  10409. 'this.a = [];',
  10410. '']),
  10411. LinesToStr([ // $mod.$main
  10412. '$mod.a = [[1, 1]];',
  10413. '$mod.a = [$mod.i.slice(0)];',
  10414. '$mod.a = rtl.arrayConcatN($mod.a, [$mod.i.slice(0)]);',
  10415. '$mod.a = rtl.arrayConcatN([$mod.i.slice(0)], $mod.a);',
  10416. '$mod.DoInt([[1, 1]]);',
  10417. '$mod.DoInt([[1, 2], [3, 4]]);',
  10418. '']));
  10419. end;
  10420. procedure TTestModule.TestArray_ForInArrOfString;
  10421. begin
  10422. StartProgram(false);
  10423. Add([
  10424. 'type',
  10425. 'type',
  10426. ' TMonthNameArray = array [1..12] of string;',
  10427. ' TMonthNames = TMonthNameArray;',
  10428. ' TObject = class',
  10429. ' private',
  10430. ' function GetLongMonthNames: TMonthNames; virtual; abstract;',
  10431. ' public',
  10432. ' Property LongMonthNames : TMonthNames Read GetLongMonthNames;',
  10433. ' end;',
  10434. 'var',
  10435. ' f: TObject;',
  10436. ' Month: string;',
  10437. ' Names: array of string = (''a'',''foo'',''bar'');',
  10438. ' i: longint;',
  10439. 'begin',
  10440. ' for Month in f.LongMonthNames do ;',
  10441. ' for Month in Names do ;',
  10442. ' for i:=low(Names) to high(Names) do ;',
  10443. '']);
  10444. ConvertProgram;
  10445. CheckSource('TestArray_ForInArrOfString',
  10446. LinesToStr([ // statements
  10447. 'rtl.createClass(this, "TObject", null, function () {',
  10448. ' this.$init = function () {',
  10449. ' };',
  10450. ' this.$final = function () {',
  10451. ' };',
  10452. '});',
  10453. 'this.f = null;',
  10454. 'this.Month = "";',
  10455. 'this.Names = ["a", "foo", "bar"];',
  10456. 'this.i = 0;',
  10457. '']),
  10458. LinesToStr([ // $mod.$main
  10459. 'for (var $in = $mod.f.GetLongMonthNames(), $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) $mod.Month = $in[$l];',
  10460. 'for (var $in1 = $mod.Names, $l1 = 0, $end1 = rtl.length($in1) - 1; $l1 <= $end1; $l1++) $mod.Month = $in1[$l1];',
  10461. 'for (var $l2 = 0, $end2 = rtl.length($mod.Names) - 1; $l2 <= $end2; $l2++) $mod.i = $l2;',
  10462. '']));
  10463. end;
  10464. procedure TTestModule.TestExternalClass_TypeCastArrayToExternalClass;
  10465. begin
  10466. StartProgram(false);
  10467. Add([
  10468. '{$modeswitch externalclass}',
  10469. 'type',
  10470. ' TJSObject = class external name ''Object''',
  10471. ' end;',
  10472. ' TJSArray = class external name ''Array''',
  10473. ' class function isArray(Value: JSValue) : boolean;',
  10474. ' function concat() : TJSArray; varargs;',
  10475. ' end;',
  10476. 'var',
  10477. ' aObj: TJSArray;',
  10478. ' a: array of longint;',
  10479. ' o: TJSObject;',
  10480. 'begin',
  10481. ' if TJSArray.isArray(65) then ;',
  10482. ' aObj:=TJSArray(a).concat(a);',
  10483. ' o:=TJSObject(a);']);
  10484. ConvertProgram;
  10485. CheckSource('TestExternalClass_TypeCastArrayToExternalClass',
  10486. LinesToStr([ // statements
  10487. 'this.aObj = null;',
  10488. 'this.a = [];',
  10489. 'this.o = null;',
  10490. '']),
  10491. LinesToStr([ // $mod.$main
  10492. 'if (Array.isArray(65)) ;',
  10493. '$mod.aObj = $mod.a.concat($mod.a);',
  10494. '$mod.o = $mod.a;',
  10495. '']));
  10496. end;
  10497. procedure TTestModule.TestExternalClass_TypeCastArrayFromExternalClass;
  10498. begin
  10499. StartProgram(false);
  10500. Add([
  10501. '{$modeswitch externalclass}',
  10502. 'type',
  10503. ' TArrStr = array of string;',
  10504. ' TJSArray = class external name ''Array''',
  10505. ' end;',
  10506. ' TJSObject = class external name ''Object''',
  10507. ' end;',
  10508. 'var',
  10509. ' aObj: TJSArray;',
  10510. ' a: TArrStr;',
  10511. ' jo: TJSObject;',
  10512. 'begin',
  10513. ' a:=TArrStr(aObj);',
  10514. ' TArrStr(aObj)[1]:=TArrStr(aObj)[2];',
  10515. ' a:=TarrStr(jo);',
  10516. '']);
  10517. ConvertProgram;
  10518. CheckSource('TestExternalClass_TypeCastArrayFromExternalClass',
  10519. LinesToStr([ // statements
  10520. 'this.aObj = null;',
  10521. 'this.a = [];',
  10522. 'this.jo = null;',
  10523. '']),
  10524. LinesToStr([ // $mod.$main
  10525. '$mod.a = $mod.aObj;',
  10526. '$mod.aObj[1] = $mod.aObj[2];',
  10527. '$mod.a = $mod.jo;',
  10528. '']));
  10529. end;
  10530. procedure TTestModule.TestArrayOfConst_TVarRec;
  10531. begin
  10532. StartProgram(true,[supTVarRec]);
  10533. Add([
  10534. 'procedure Say(args: array of const);',
  10535. 'var',
  10536. ' i: longint;',
  10537. ' v: TVarRec;',
  10538. 'begin',
  10539. ' for i:=low(args) to high(args) do begin',
  10540. ' v:=args[i];',
  10541. ' case v.vtype of',
  10542. ' vtInteger: if length(args)=args[i].vInteger then ;',
  10543. ' end;',
  10544. ' end;',
  10545. ' for v in args do ;',
  10546. ' args:=nil;',
  10547. ' SetLength(args,2);',
  10548. 'end;',
  10549. 'begin']);
  10550. ConvertProgram;
  10551. CheckSource('TestArrayOfConst_TVarRec',
  10552. LinesToStr([ // statements
  10553. 'this.Say = function (args) {',
  10554. ' var i = 0;',
  10555. ' var v = pas.system.TVarRec.$new();',
  10556. ' for (var $l = 0, $end = rtl.length(args) - 1; $l <= $end; $l++) {',
  10557. ' i = $l;',
  10558. ' v.$assign(args[i]);',
  10559. ' var $tmp = v.VType;',
  10560. ' if ($tmp === 0) if (rtl.length(args) === args[i].VJSValue) ;',
  10561. ' };',
  10562. ' for (var $in = args, $l1 = 0, $end1 = rtl.length($in) - 1; $l1 <= $end1; $l1++) v = $in[$l1];',
  10563. ' args = [];',
  10564. ' args = rtl.arraySetLength(args, pas.system.TVarRec, 2);',
  10565. '};',
  10566. '']),
  10567. LinesToStr([ // $mod.$main
  10568. ]));
  10569. end;
  10570. procedure TTestModule.TestArrayOfConst_PassBaseTypes;
  10571. begin
  10572. StartProgram(true,[supTVarRec]);
  10573. Add([
  10574. 'procedure Say(args: array of const);',
  10575. 'begin',
  10576. ' Say(args);',
  10577. 'end;',
  10578. 'var',
  10579. ' p: Pointer;',
  10580. ' j: jsvalue;',
  10581. ' c: currency;',
  10582. 'begin',
  10583. ' Say([]);',
  10584. ' Say([1]);',
  10585. ' Say([''c'',''foo'',nil,true,1.3,p,j,c]);',
  10586. '']);
  10587. ConvertProgram;
  10588. CheckSource('TestArrayOfConst_PassBaseTypes',
  10589. LinesToStr([ // statements
  10590. 'this.Say = function (args) {',
  10591. ' $mod.Say(args);',
  10592. '};',
  10593. 'this.p = null;',
  10594. 'this.j = undefined;',
  10595. 'this.c = 0;',
  10596. '']),
  10597. LinesToStr([ // $mod.$main
  10598. '$mod.Say([]);',
  10599. '$mod.Say(pas.system.VarRecs(0, 1));',
  10600. '$mod.Say(pas.system.VarRecs(',
  10601. ' 9,',
  10602. ' "c",',
  10603. ' 18,',
  10604. ' "foo",',
  10605. ' 5,',
  10606. ' null,',
  10607. ' 1,',
  10608. ' true,',
  10609. ' 3,',
  10610. ' 1.3,',
  10611. ' 5,',
  10612. ' $mod.p,',
  10613. ' 20,',
  10614. ' $mod.j,',
  10615. ' 12,',
  10616. ' $mod.c',
  10617. ' ));',
  10618. '']));
  10619. end;
  10620. procedure TTestModule.TestArrayOfConst_PassObj;
  10621. begin
  10622. StartProgram(true,[supTVarRec]);
  10623. Add([
  10624. '{$interfaces corba}',
  10625. 'type',
  10626. ' TObject = class',
  10627. ' end;',
  10628. ' TClass = class of TObject;',
  10629. ' IUnknown = interface',
  10630. ' end;',
  10631. 'procedure Say(args: array of const);',
  10632. 'begin',
  10633. 'end;',
  10634. 'var',
  10635. ' o: TObject;',
  10636. ' c: TClass;',
  10637. ' i: IUnknown;',
  10638. 'begin',
  10639. ' Say([o,c,TObject]);',
  10640. ' Say([nil,i]);',
  10641. '']);
  10642. ConvertProgram;
  10643. CheckSource('TestArrayOfConst_PassObj',
  10644. LinesToStr([ // statements
  10645. 'rtl.createClass(this, "TObject", null, function () {',
  10646. ' this.$init = function () {',
  10647. ' };',
  10648. ' this.$final = function () {',
  10649. ' };',
  10650. '});',
  10651. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  10652. 'this.Say = function (args) {',
  10653. '};',
  10654. 'this.o = null;',
  10655. 'this.c = null;',
  10656. 'this.i = null;',
  10657. '']),
  10658. LinesToStr([ // $mod.$main
  10659. '$mod.Say(pas.system.VarRecs(',
  10660. ' 7,',
  10661. ' $mod.o,',
  10662. ' 8,',
  10663. ' $mod.c,',
  10664. ' 8,',
  10665. ' $mod.TObject',
  10666. '));',
  10667. '$mod.Say(pas.system.VarRecs(5, null, 14, $mod.i));',
  10668. '']));
  10669. end;
  10670. procedure TTestModule.TestRecord_Empty;
  10671. begin
  10672. StartProgram(false);
  10673. Add([
  10674. 'type',
  10675. ' TRecA = record',
  10676. ' end;',
  10677. 'var a,b: TRecA;',
  10678. 'begin',
  10679. ' if a=b then ;']);
  10680. ConvertProgram;
  10681. CheckSource('TestRecord_Empty',
  10682. LinesToStr([ // statements
  10683. 'rtl.recNewT(this, "TRecA", function () {',
  10684. ' this.$eq = function (b) {',
  10685. ' return true;',
  10686. ' };',
  10687. ' this.$assign = function (s) {',
  10688. ' return this;',
  10689. ' };',
  10690. '});',
  10691. 'this.a = this.TRecA.$new();',
  10692. 'this.b = this.TRecA.$new();',
  10693. '']),
  10694. LinesToStr([ // $mod.$main
  10695. 'if ($mod.a.$eq($mod.b)) ;'
  10696. ]));
  10697. end;
  10698. procedure TTestModule.TestRecord_Var;
  10699. begin
  10700. StartProgram(false);
  10701. Add('type');
  10702. Add(' TRecA = record');
  10703. Add(' Bold: longint;');
  10704. Add(' end;');
  10705. Add('var Rec: TRecA;');
  10706. Add('begin');
  10707. Add(' rec.bold:=123');
  10708. ConvertProgram;
  10709. CheckSource('TestRecord_Var',
  10710. LinesToStr([ // statements
  10711. 'rtl.recNewT(this, "TRecA", function () {',
  10712. ' this.Bold = 0;',
  10713. ' this.$eq = function (b) {',
  10714. ' return this.Bold === b.Bold;',
  10715. ' };',
  10716. ' this.$assign = function (s) {',
  10717. ' this.Bold = s.Bold;',
  10718. ' return this;',
  10719. ' };',
  10720. '});',
  10721. 'this.Rec = this.TRecA.$new();',
  10722. '']),
  10723. LinesToStr([ // $mod.$main
  10724. '$mod.Rec.Bold = 123;'
  10725. ]));
  10726. end;
  10727. procedure TTestModule.TestRecord_VarExternal;
  10728. begin
  10729. StartProgram(false);
  10730. Add([
  10731. '{$modeswitch externalclass}',
  10732. 'type',
  10733. ' TRecA = record',
  10734. ' i: byte;',
  10735. ' length_: longint external name ''length'';',
  10736. ' end;',
  10737. 'var Rec: TRecA;',
  10738. 'begin',
  10739. ' rec.length_ := rec.length_',
  10740. '']);
  10741. ConvertProgram;
  10742. CheckSource('TestRecord_VarExternal',
  10743. LinesToStr([ // statements
  10744. 'rtl.recNewT(this, "TRecA", function () {',
  10745. ' this.i = 0;',
  10746. ' this.$eq = function (b) {',
  10747. ' return (this.i === b.i) && (this.length === b.length);',
  10748. ' };',
  10749. ' this.$assign = function (s) {',
  10750. ' this.i = s.i;',
  10751. ' this.length = s.length;',
  10752. ' return this;',
  10753. ' };',
  10754. '});',
  10755. 'this.Rec = this.TRecA.$new();',
  10756. '']),
  10757. LinesToStr([ // $mod.$main
  10758. '$mod.Rec.length = $mod.Rec.length;'
  10759. ]));
  10760. end;
  10761. procedure TTestModule.TestRecord_WithDo;
  10762. begin
  10763. StartProgram(false);
  10764. Add('type');
  10765. Add(' TRec = record');
  10766. Add(' vI: longint;');
  10767. Add(' end;');
  10768. Add('var');
  10769. Add(' Int: longint;');
  10770. Add(' r: TRec;');
  10771. Add('begin');
  10772. Add(' with r do');
  10773. Add(' int:=vi;');
  10774. Add(' with r do begin');
  10775. Add(' int:=vi;');
  10776. Add(' vi:=int;');
  10777. Add(' end;');
  10778. ConvertProgram;
  10779. CheckSource('TestWithRecordDo',
  10780. LinesToStr([ // statements
  10781. 'rtl.recNewT(this, "TRec", function () {',
  10782. ' this.vI = 0;',
  10783. ' this.$eq = function (b) {',
  10784. ' return this.vI === b.vI;',
  10785. ' };',
  10786. ' this.$assign = function (s) {',
  10787. ' this.vI = s.vI;',
  10788. ' return this;',
  10789. ' };',
  10790. '});',
  10791. 'this.Int = 0;',
  10792. 'this.r = this.TRec.$new();',
  10793. '']),
  10794. LinesToStr([ // $mod.$main
  10795. 'var $with = $mod.r;',
  10796. '$mod.Int = $with.vI;',
  10797. 'var $with1 = $mod.r;',
  10798. '$mod.Int = $with1.vI;',
  10799. '$with1.vI = $mod.Int;'
  10800. ]));
  10801. end;
  10802. procedure TTestModule.TestRecord_Assign;
  10803. begin
  10804. StartProgram(false);
  10805. Add([
  10806. 'type',
  10807. ' TEnum = (red,green);',
  10808. ' TEnums = set of TEnum;',
  10809. ' TSmallRec = record',
  10810. ' N: longint;',
  10811. ' end;',
  10812. ' TBigRec = record',
  10813. ' Int: longint;',
  10814. ' D: double;',
  10815. ' Arr: array of longint;',
  10816. ' Arr2: array[1..2] of longint;',
  10817. ' Small: TSmallRec;',
  10818. ' Enums: TEnums;',
  10819. ' end;',
  10820. 'var',
  10821. ' r, s: TBigRec;',
  10822. 'begin',
  10823. ' r:=s;',
  10824. ' r:=default(TBigRec);',
  10825. ' r:=default(s);',
  10826. '']);
  10827. ConvertProgram;
  10828. CheckSource('TestRecord_Assign',
  10829. LinesToStr([ // statements
  10830. 'this.TEnum = {',
  10831. ' "0": "red",',
  10832. ' red: 0,',
  10833. ' "1": "green",',
  10834. ' green: 1',
  10835. '};',
  10836. 'rtl.recNewT(this, "TSmallRec", function () {',
  10837. ' this.N = 0;',
  10838. ' this.$eq = function (b) {',
  10839. ' return this.N === b.N;',
  10840. ' };',
  10841. ' this.$assign = function (s) {',
  10842. ' this.N = s.N;',
  10843. ' return this;',
  10844. ' };',
  10845. '});',
  10846. 'rtl.recNewT(this, "TBigRec", function () {',
  10847. ' this.Int = 0;',
  10848. ' this.D = 0.0;',
  10849. ' this.$new = function () {',
  10850. ' var r = Object.create(this);',
  10851. ' r.Arr = [];',
  10852. ' r.Arr2 = rtl.arraySetLength(null, 0, 2);',
  10853. ' r.Small = $mod.TSmallRec.$new();',
  10854. ' r.Enums = {};',
  10855. ' return r;',
  10856. ' };',
  10857. ' this.$eq = function (b) {',
  10858. ' 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);',
  10859. ' };',
  10860. ' this.$assign = function (s) {',
  10861. ' this.Int = s.Int;',
  10862. ' this.D = s.D;',
  10863. ' this.Arr = rtl.arrayRef(s.Arr);',
  10864. ' this.Arr2 = s.Arr2.slice(0);',
  10865. ' this.Small.$assign(s.Small);',
  10866. ' this.Enums = rtl.refSet(s.Enums);',
  10867. ' return this;',
  10868. ' };',
  10869. '});',
  10870. 'this.r = this.TBigRec.$new();',
  10871. 'this.s = this.TBigRec.$new();',
  10872. '']),
  10873. LinesToStr([ // $mod.$main
  10874. '$mod.r.$assign($mod.s);',
  10875. '$mod.r.$assign($mod.TBigRec.$new());',
  10876. '$mod.r.$assign($mod.TBigRec.$new());',
  10877. '']));
  10878. end;
  10879. procedure TTestModule.TestRecord_AsParams;
  10880. begin
  10881. StartProgram(false);
  10882. Add([
  10883. 'type',
  10884. ' integer = longint;',
  10885. ' TRecord = record',
  10886. ' i: integer;',
  10887. ' end;',
  10888. 'procedure DoIt(vD: TRecord; const vC: TRecord; var vV: TRecord; var U);',
  10889. 'var vL: TRecord;',
  10890. 'begin',
  10891. ' vd:=vd;',
  10892. ' vd.i:=vd.i;',
  10893. ' vl:=vc;',
  10894. ' vv:=vv;',
  10895. ' vv.i:=vv.i;',
  10896. ' U:=vl;',
  10897. ' U:=vd;',
  10898. ' U:=vc;',
  10899. ' U:=vv;',
  10900. ' vl:=TRecord(U);',
  10901. ' vd:=TRecord(U);',
  10902. ' vv:=TRecord(U);',
  10903. ' doit(vd,vd,vd,vd);',
  10904. ' doit(vc,vc,vl,vl);',
  10905. ' doit(vv,vv,vv,vv);',
  10906. ' doit(vl,vl,vl,vl);',
  10907. ' TRecord(U).i:=3;',
  10908. 'end;',
  10909. 'var i: TRecord;',
  10910. 'begin',
  10911. ' doit(i,i,i,i);',
  10912. '']);
  10913. ConvertProgram;
  10914. CheckSource('TestRecord_AsParams',
  10915. LinesToStr([ // statements
  10916. 'rtl.recNewT(this, "TRecord", function () {',
  10917. ' this.i = 0;',
  10918. ' this.$eq = function (b) {',
  10919. ' return this.i === b.i;',
  10920. ' };',
  10921. ' this.$assign = function (s) {',
  10922. ' this.i = s.i;',
  10923. ' return this;',
  10924. ' };',
  10925. '});',
  10926. 'this.DoIt = function (vD, vC, vV, U) {',
  10927. ' var vL = $mod.TRecord.$new();',
  10928. ' vD.$assign(vD);',
  10929. ' vD.i = vD.i;',
  10930. ' vL.$assign(vC);',
  10931. ' vV.$assign(vV);',
  10932. ' vV.i = vV.i;',
  10933. ' U.$assign(vL);',
  10934. ' U.$assign(vD);',
  10935. ' U.$assign(vC);',
  10936. ' U.$assign(vV);',
  10937. ' vL.$assign(U);',
  10938. ' vD.$assign(U);',
  10939. ' vV.$assign(U);',
  10940. ' $mod.DoIt($mod.TRecord.$clone(vD), vD, vD, vD);',
  10941. ' $mod.DoIt($mod.TRecord.$clone(vC), vC, vL, vL);',
  10942. ' $mod.DoIt($mod.TRecord.$clone(vV), vV, vV, vV);',
  10943. ' $mod.DoIt($mod.TRecord.$clone(vL), vL, vL, vL);',
  10944. ' U.i = 3;',
  10945. '};',
  10946. 'this.i = this.TRecord.$new();'
  10947. ]),
  10948. LinesToStr([
  10949. '$mod.DoIt($mod.TRecord.$clone($mod.i), $mod.i, $mod.i, $mod.i);',
  10950. '']));
  10951. end;
  10952. procedure TTestModule.TestRecord_ConstRef;
  10953. begin
  10954. StartProgram(false);
  10955. Add([
  10956. 'type TRec = record i: word; end;',
  10957. 'procedure Run(constref a: TRec);',
  10958. 'begin',
  10959. 'end;',
  10960. 'procedure Fly(a: TRec; var b: TRec; out c: TRec; const d: TRec; constref e: TRec);',
  10961. 'var l: TRec;',
  10962. 'begin',
  10963. ' Run(l);',
  10964. ' Run(a);',
  10965. ' Run(b);',
  10966. ' Run(c);',
  10967. ' Run(d);',
  10968. ' Run(e);',
  10969. 'end;',
  10970. 'begin',
  10971. '']);
  10972. ConvertProgram;
  10973. CheckResolverUnexpectedHints();
  10974. CheckSource('TestRecord_ConstRef',
  10975. LinesToStr([ // statements
  10976. 'rtl.recNewT(this, "TRec", function () {',
  10977. ' this.i = 0;',
  10978. ' this.$eq = function (b) {',
  10979. ' return this.i === b.i;',
  10980. ' };',
  10981. ' this.$assign = function (s) {',
  10982. ' this.i = s.i;',
  10983. ' return this;',
  10984. ' };',
  10985. '});',
  10986. 'this.Run = function (a) {',
  10987. '};',
  10988. 'this.Fly = function (a, b, c, d, e) {',
  10989. ' var l = $mod.TRec.$new();',
  10990. ' $mod.Run(l);',
  10991. ' $mod.Run(a);',
  10992. ' $mod.Run(b);',
  10993. ' $mod.Run(c);',
  10994. ' $mod.Run(d);',
  10995. ' $mod.Run(e);',
  10996. '};',
  10997. '']),
  10998. LinesToStr([
  10999. '']));
  11000. end;
  11001. procedure TTestModule.TestRecordElement_AsParams;
  11002. begin
  11003. StartProgram(false);
  11004. Add('type');
  11005. Add(' integer = longint;');
  11006. Add(' TRecord = record');
  11007. Add(' i: integer;');
  11008. Add(' end;');
  11009. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  11010. Add('var vJ: TRecord;');
  11011. Add('begin');
  11012. Add(' doit(vj.i,vj.i,vj.i);');
  11013. Add('end;');
  11014. Add('var r: TRecord;');
  11015. Add('begin');
  11016. Add(' doit(r.i,r.i,r.i);');
  11017. ConvertProgram;
  11018. CheckSource('TestRecordElement_AsParams',
  11019. LinesToStr([ // statements
  11020. 'rtl.recNewT(this, "TRecord", function () {',
  11021. ' this.i = 0;',
  11022. ' this.$eq = function (b) {',
  11023. ' return this.i === b.i;',
  11024. ' };',
  11025. ' this.$assign = function (s) {',
  11026. ' this.i = s.i;',
  11027. ' return this;',
  11028. ' };',
  11029. '});',
  11030. 'this.DoIt = function (vG,vH,vI) {',
  11031. ' var vJ = $mod.TRecord.$new();',
  11032. ' $mod.DoIt(vJ.i, vJ.i, {',
  11033. ' p: vJ,',
  11034. ' get: function () {',
  11035. ' return this.p.i;',
  11036. ' },',
  11037. ' set: function (v) {',
  11038. ' this.p.i = v;',
  11039. ' }',
  11040. ' });',
  11041. '};',
  11042. 'this.r = this.TRecord.$new();'
  11043. ]),
  11044. LinesToStr([
  11045. '$mod.DoIt($mod.r.i,$mod.r.i,{',
  11046. ' p: $mod.r,',
  11047. ' get: function () {',
  11048. ' return this.p.i;',
  11049. ' },',
  11050. ' set: function (v) {',
  11051. ' this.p.i = v;',
  11052. ' }',
  11053. '});'
  11054. ]));
  11055. end;
  11056. procedure TTestModule.TestRecordElementFromFuncResult_AsParams;
  11057. begin
  11058. StartProgram(false);
  11059. Add('type');
  11060. Add(' integer = longint;');
  11061. Add(' TRecord = record');
  11062. Add(' i: integer;');
  11063. Add(' end;');
  11064. Add('function GetRec(vB: integer = 0): TRecord;');
  11065. Add('begin');
  11066. Add('end;');
  11067. Add('procedure DoIt(vG: integer; const vH: integer);');
  11068. Add('begin');
  11069. Add('end;');
  11070. Add('begin');
  11071. Add(' doit(getrec.i,getrec.i);');
  11072. Add(' doit(getrec().i,getrec().i);');
  11073. Add(' doit(getrec(1).i,getrec(2).i);');
  11074. ConvertProgram;
  11075. CheckSource('TestRecordElementFromFuncResult_AsParams',
  11076. LinesToStr([ // statements
  11077. 'rtl.recNewT(this, "TRecord", function () {',
  11078. ' this.i = 0;',
  11079. ' this.$eq = function (b) {',
  11080. ' return this.i === b.i;',
  11081. ' };',
  11082. ' this.$assign = function (s) {',
  11083. ' this.i = s.i;',
  11084. ' return this;',
  11085. ' };',
  11086. '});',
  11087. 'this.GetRec = function (vB) {',
  11088. ' var Result = $mod.TRecord.$new();',
  11089. ' return Result;',
  11090. '};',
  11091. 'this.DoIt = function (vG, vH) {',
  11092. '};',
  11093. '']),
  11094. LinesToStr([
  11095. '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
  11096. '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
  11097. '$mod.DoIt($mod.GetRec(1).i,$mod.GetRec(2).i);',
  11098. '']));
  11099. end;
  11100. procedure TTestModule.TestRecordElementFromWith_AsParams;
  11101. begin
  11102. StartProgram(false);
  11103. Add('type');
  11104. Add(' integer = longint;');
  11105. Add(' TRecord = record');
  11106. Add(' i: integer;');
  11107. Add(' end;');
  11108. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  11109. Add('begin');
  11110. Add('end;');
  11111. Add('var r: trecord;');
  11112. Add('begin');
  11113. Add(' with r do ');
  11114. Add(' doit(i,i,i);');
  11115. ConvertProgram;
  11116. CheckSource('TestRecordElementFromWith_AsParams',
  11117. LinesToStr([ // statements
  11118. 'rtl.recNewT(this, "TRecord", function () {',
  11119. ' this.i = 0;',
  11120. ' this.$eq = function (b) {',
  11121. ' return this.i === b.i;',
  11122. ' };',
  11123. ' this.$assign = function (s) {',
  11124. ' this.i = s.i;',
  11125. ' return this;',
  11126. ' };',
  11127. '});',
  11128. 'this.DoIt = function (vG,vH,vI) {',
  11129. '};',
  11130. 'this.r = this.TRecord.$new();'
  11131. ]),
  11132. LinesToStr([
  11133. 'var $with = $mod.r;',
  11134. '$mod.DoIt($with.i,$with.i,{',
  11135. ' p: $with,',
  11136. ' get: function () {',
  11137. ' return this.p.i;',
  11138. ' },',
  11139. ' set: function (v) {',
  11140. ' this.p.i = v;',
  11141. ' }',
  11142. '});',
  11143. '']));
  11144. end;
  11145. procedure TTestModule.TestRecord_Equal;
  11146. begin
  11147. StartProgram(false);
  11148. Add('type');
  11149. Add(' integer = longint;');
  11150. Add(' TFlag = (red,blue);');
  11151. Add(' TFlags = set of TFlag;');
  11152. Add(' TProc = procedure;');
  11153. Add(' TRecord = record');
  11154. Add(' i: integer;');
  11155. Add(' Event: TProc;');
  11156. Add(' f: TFlags;');
  11157. Add(' end;');
  11158. Add(' TNested = record');
  11159. Add(' r: TRecord;');
  11160. Add(' end;');
  11161. Add('var');
  11162. Add(' b: boolean;');
  11163. Add(' r,s: trecord;');
  11164. Add('begin');
  11165. Add(' b:=r=s;');
  11166. Add(' b:=r<>s;');
  11167. ConvertProgram;
  11168. CheckSource('TestRecord_Equal',
  11169. LinesToStr([ // statements
  11170. 'this.TFlag = {',
  11171. ' "0": "red",',
  11172. ' red: 0,',
  11173. ' "1": "blue",',
  11174. ' blue: 1',
  11175. '};',
  11176. 'rtl.recNewT(this, "TRecord", function () {',
  11177. ' this.i = 0;',
  11178. ' this.Event = null;',
  11179. ' this.$new = function () {',
  11180. ' var r = Object.create(this);',
  11181. ' r.f = {};',
  11182. ' return r;',
  11183. ' };',
  11184. ' this.$eq = function (b) {',
  11185. ' return (this.i === b.i) && rtl.eqCallback(this.Event, b.Event) && rtl.eqSet(this.f, b.f);',
  11186. ' };',
  11187. ' this.$assign = function (s) {',
  11188. ' this.i = s.i;',
  11189. ' this.Event = s.Event;',
  11190. ' this.f = rtl.refSet(s.f);',
  11191. ' return this;',
  11192. ' };',
  11193. '});',
  11194. 'rtl.recNewT(this, "TNested", function () {',
  11195. ' this.$new = function () {',
  11196. ' var r = Object.create(this);',
  11197. ' r.r = $mod.TRecord.$new();',
  11198. ' return r;',
  11199. ' };',
  11200. ' this.$eq = function (b) {',
  11201. ' return this.r.$eq(b.r);',
  11202. ' };',
  11203. ' this.$assign = function (s) {',
  11204. ' this.r.$assign(s.r);',
  11205. ' return this;',
  11206. ' };',
  11207. '});',
  11208. 'this.b = false;',
  11209. 'this.r = this.TRecord.$new();',
  11210. 'this.s = this.TRecord.$new();',
  11211. '']),
  11212. LinesToStr([
  11213. '$mod.b = $mod.r.$eq($mod.s);',
  11214. '$mod.b = !$mod.r.$eq($mod.s);',
  11215. '']));
  11216. end;
  11217. procedure TTestModule.TestRecord_JSValue;
  11218. begin
  11219. StartProgram(false);
  11220. Add([
  11221. 'type',
  11222. ' TRecord = record',
  11223. ' i: longint;',
  11224. ' end;',
  11225. 'procedure Fly(d: jsvalue; const c: jsvalue);',
  11226. 'begin',
  11227. 'end;',
  11228. 'procedure Run(d: TRecord; const c: TRecord; var v: TRecord);',
  11229. 'begin',
  11230. ' if jsvalue(d) then ;',
  11231. ' if jsvalue(c) then ;',
  11232. ' if jsvalue(v) then ;',
  11233. 'end;',
  11234. 'var',
  11235. ' Jv: jsvalue;',
  11236. ' Rec: trecord;',
  11237. 'begin',
  11238. ' rec:=trecord(jv);',
  11239. ' jv:=rec;',
  11240. ' Fly(rec,rec);',
  11241. ' Fly(@rec,@rec);',
  11242. ' if jsvalue(Rec) then ;',
  11243. ' Run(trecord(jv),trecord(jv),rec);',
  11244. '']);
  11245. ConvertProgram;
  11246. CheckSource('TestRecord_JSValue',
  11247. LinesToStr([ // statements
  11248. 'rtl.recNewT(this, "TRecord", function () {',
  11249. ' this.i = 0;',
  11250. ' this.$eq = function (b) {',
  11251. ' return this.i === b.i;',
  11252. ' };',
  11253. ' this.$assign = function (s) {',
  11254. ' this.i = s.i;',
  11255. ' return this;',
  11256. ' };',
  11257. '});',
  11258. 'this.Fly = function (d, c) {',
  11259. '};',
  11260. 'this.Run = function (d, c, v) {',
  11261. ' if (d) ;',
  11262. ' if (c) ;',
  11263. ' if (v) ;',
  11264. '};',
  11265. 'this.Jv = undefined;',
  11266. 'this.Rec = this.TRecord.$new();',
  11267. '']),
  11268. LinesToStr([
  11269. '$mod.Rec.$assign(rtl.getObject($mod.Jv));',
  11270. '$mod.Jv = $mod.Rec;',
  11271. '$mod.Fly($mod.TRecord.$clone($mod.Rec), $mod.Rec);',
  11272. '$mod.Fly($mod.Rec, $mod.Rec);',
  11273. 'if ($mod.Rec) ;',
  11274. '$mod.Run($mod.TRecord.$clone(rtl.getObject($mod.Jv)), rtl.getObject($mod.Jv), $mod.Rec);',
  11275. '']));
  11276. end;
  11277. procedure TTestModule.TestRecord_VariantFail;
  11278. begin
  11279. StartProgram(false);
  11280. Add([
  11281. 'type',
  11282. ' TRec = record',
  11283. ' case word of',
  11284. ' 0: (b0, b1: Byte);',
  11285. ' 1: (i: word);',
  11286. ' end;',
  11287. 'begin']);
  11288. SetExpectedPasResolverError('variant record is not supported',
  11289. nXIsNotSupported);
  11290. ConvertProgram;
  11291. end;
  11292. procedure TTestModule.TestRecord_FieldArray;
  11293. begin
  11294. StartProgram(false);
  11295. Add([
  11296. 'type',
  11297. ' TArrInt = array[3..4] of longint;',
  11298. ' TArrArrInt = array[3..4] of longint;',
  11299. ' TRec = record',
  11300. ' a: array of longint;',
  11301. ' s: array[1..2] of longint;',
  11302. ' m: array[1..2,3..4] of longint;',
  11303. ' o: TArrArrInt;',
  11304. ' end;',
  11305. 'begin']);
  11306. ConvertProgram;
  11307. CheckSource('TestRecord_FieldArray',
  11308. LinesToStr([ // statements
  11309. 'rtl.recNewT(this, "TRec", function () {',
  11310. ' this.$new = function () {',
  11311. ' var r = Object.create(this);',
  11312. ' r.a = [];',
  11313. ' r.s = rtl.arraySetLength(null, 0, 2);',
  11314. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  11315. ' r.o = rtl.arraySetLength(null, 0, 2);',
  11316. ' return r;',
  11317. ' };',
  11318. ' this.$eq = function (b) {',
  11319. ' return (this.a === b.a) && rtl.arrayEq(this.s, b.s) && rtl.arrayEq(this.m, b.m) && rtl.arrayEq(this.o, b.o);',
  11320. ' };',
  11321. ' this.$assign = function (s) {',
  11322. ' this.a = rtl.arrayRef(s.a);',
  11323. ' this.s = s.s.slice(0);',
  11324. ' this.m = s.m.slice(0);',
  11325. ' this.o = s.o.slice(0);',
  11326. ' return this;',
  11327. ' };',
  11328. '});',
  11329. '']),
  11330. LinesToStr([ // $mod.$main
  11331. '']));
  11332. end;
  11333. procedure TTestModule.TestRecord_Const;
  11334. begin
  11335. StartProgram(false);
  11336. Add([
  11337. 'type',
  11338. ' TArrInt = array[3..4] of longint;',
  11339. ' TPoint = record x,y: longint; end;',
  11340. ' TRec = record',
  11341. ' i: longint;',
  11342. ' a: array of longint;',
  11343. ' s: array[1..2] of longint;',
  11344. ' m: array[1..2,3..4] of longint;',
  11345. ' p: TPoint;',
  11346. ' end;',
  11347. ' TPoints = array of TPoint;',
  11348. 'const',
  11349. ' r: TRec = (',
  11350. ' i:1;',
  11351. ' a:(2,3);',
  11352. ' s:(4,5);',
  11353. ' m:( (11,12), (13,14) );',
  11354. ' p: (x:21; y:22)',
  11355. ' );',
  11356. ' p: TPoints = ( (x:1;y:2), (x:3;y:4) );',
  11357. 'begin']);
  11358. ConvertProgram;
  11359. CheckSource('TestRecord_Const',
  11360. LinesToStr([ // statements
  11361. 'rtl.recNewT(this, "TPoint", function () {',
  11362. ' this.x = 0;',
  11363. ' this.y = 0;',
  11364. ' this.$eq = function (b) {',
  11365. ' return (this.x === b.x) && (this.y === b.y);',
  11366. ' };',
  11367. ' this.$assign = function (s) {',
  11368. ' this.x = s.x;',
  11369. ' this.y = s.y;',
  11370. ' return this;',
  11371. ' };',
  11372. '});',
  11373. 'rtl.recNewT(this, "TRec", function () {',
  11374. ' this.i = 0;',
  11375. ' this.$new = function () {',
  11376. ' var r = Object.create(this);',
  11377. ' r.a = [];',
  11378. ' r.s = rtl.arraySetLength(null, 0, 2);',
  11379. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  11380. ' r.p = $mod.TPoint.$new();',
  11381. ' return r;',
  11382. ' };',
  11383. ' this.$eq = function (b) {',
  11384. ' 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);',
  11385. ' };',
  11386. ' this.$assign = function (s) {',
  11387. ' this.i = s.i;',
  11388. ' this.a = rtl.arrayRef(s.a);',
  11389. ' this.s = s.s.slice(0);',
  11390. ' this.m = s.m.slice(0);',
  11391. ' this.p.$assign(s.p);',
  11392. ' return this;',
  11393. ' };',
  11394. '});',
  11395. 'this.r = this.TRec.$clone({',
  11396. ' i: 1,',
  11397. ' a: [2, 3],',
  11398. ' s: [4, 5],',
  11399. ' m: [[11, 12], [13, 14]],',
  11400. ' p: this.TPoint.$clone({',
  11401. ' x: 21,',
  11402. ' y: 22',
  11403. ' })',
  11404. '});',
  11405. 'this.p = [this.TPoint.$clone({',
  11406. ' x: 1,',
  11407. ' y: 2',
  11408. '}), this.TPoint.$clone({',
  11409. ' x: 3,',
  11410. ' y: 4',
  11411. '})];',
  11412. '']),
  11413. LinesToStr([ // $mod.$main
  11414. '']));
  11415. end;
  11416. procedure TTestModule.TestRecord_TypecastFail;
  11417. begin
  11418. StartProgram(false);
  11419. Add([
  11420. 'type',
  11421. ' TPoint = record x,y: longint; end;',
  11422. ' TRec = record l: longint end;',
  11423. 'var p: TPoint;',
  11424. 'begin',
  11425. ' if TRec(p).l=2 then ;']);
  11426. SetExpectedPasResolverError('Illegal type conversion: "TPoint" to "record TRec"',
  11427. nIllegalTypeConversionTo);
  11428. ConvertProgram;
  11429. end;
  11430. procedure TTestModule.TestRecord_InFunction;
  11431. begin
  11432. StartProgram(false);
  11433. Add([
  11434. 'var TPoint: longint = 3;',
  11435. 'procedure DoIt;',
  11436. 'type',
  11437. ' TPoint = record x,y: longint; end;',
  11438. ' TPoints = array of TPoint;',
  11439. 'var',
  11440. ' r: TPoint;',
  11441. ' p: TPoints;',
  11442. 'begin',
  11443. ' SetLength(p,2);',
  11444. 'end;',
  11445. 'begin']);
  11446. ConvertProgram;
  11447. CheckSource('TestRecord_InFunction',
  11448. LinesToStr([ // statements
  11449. 'this.TPoint = 3;',
  11450. 'var TPoint$1 = rtl.recNewT(null, "", function () {',
  11451. ' this.x = 0;',
  11452. ' this.y = 0;',
  11453. ' this.$eq = function (b) {',
  11454. ' return (this.x === b.x) && (this.y === b.y);',
  11455. ' };',
  11456. ' this.$assign = function (s) {',
  11457. ' this.x = s.x;',
  11458. ' this.y = s.y;',
  11459. ' return this;',
  11460. ' };',
  11461. '});',
  11462. 'this.DoIt = function () {',
  11463. ' var r = TPoint$1.$new();',
  11464. ' var p = [];',
  11465. ' p = rtl.arraySetLength(p, TPoint$1, 2);',
  11466. '};',
  11467. '']),
  11468. LinesToStr([ // $mod.$main
  11469. '']));
  11470. end;
  11471. procedure TTestModule.TestRecord_AnonymousFail;
  11472. begin
  11473. StartProgram(false);
  11474. Add([
  11475. 'var',
  11476. ' r: record x: word end;',
  11477. 'begin']);
  11478. SetExpectedPasResolverError('not yet implemented: :TPasRecordType [20190408224556] "anonymous record type"',
  11479. nNotYetImplemented);
  11480. ConvertProgram;
  11481. end;
  11482. procedure TTestModule.TestAdvRecord_Function;
  11483. begin
  11484. StartProgram(false);
  11485. Parser.Options:=Parser.Options+[po_cassignments];
  11486. Add([
  11487. '{$modeswitch AdvancedRecords}',
  11488. 'type',
  11489. ' TPoint = record',
  11490. ' x,y: word;',
  11491. ' function Add(const apt: TPoint): TPoint;',
  11492. ' end;',
  11493. 'function TPoint.Add(const apt: TPoint): TPoint;',
  11494. 'begin',
  11495. ' Result:=Self;',
  11496. ' Result.x+=apt.x;',
  11497. ' Result.y:=Result.y+apt.y;',
  11498. ' Self:=apt;',
  11499. 'end;',
  11500. 'var p,q: TPoint;',
  11501. 'begin',
  11502. ' p.add(q);',
  11503. ' p:=default(TPoint);',
  11504. ' p:=q;',
  11505. '']);
  11506. ConvertProgram;
  11507. CheckSource('TestAdvRecord_Function',
  11508. LinesToStr([ // statements
  11509. 'rtl.recNewT(this, "TPoint", function () {',
  11510. ' this.x = 0;',
  11511. ' this.y = 0;',
  11512. ' this.$eq = function (b) {',
  11513. ' return (this.x === b.x) && (this.y === b.y);',
  11514. ' };',
  11515. ' this.$assign = function (s) {',
  11516. ' this.x = s.x;',
  11517. ' this.y = s.y;',
  11518. ' return this;',
  11519. ' };',
  11520. ' this.Add = function (apt) {',
  11521. ' var Result = $mod.TPoint.$new();',
  11522. ' Result.$assign(this);',
  11523. ' Result.x += apt.x;',
  11524. ' Result.y = Result.y + apt.y;',
  11525. ' this.$assign(apt);',
  11526. ' return Result;',
  11527. ' };',
  11528. '});',
  11529. 'this.p = this.TPoint.$new();',
  11530. 'this.q = this.TPoint.$new();',
  11531. '']),
  11532. LinesToStr([ // $mod.$main
  11533. '$mod.p.Add($mod.q);',
  11534. '$mod.p.$assign($mod.TPoint.$new());',
  11535. '$mod.p.$assign($mod.q);',
  11536. '']));
  11537. end;
  11538. procedure TTestModule.TestAdvRecord_Property;
  11539. begin
  11540. StartProgram(false);
  11541. Add([
  11542. '{$modeswitch AdvancedRecords}',
  11543. 'type',
  11544. ' TPoint = record',
  11545. ' x,y: word;',
  11546. ' strict private',
  11547. ' function GetSize: longword;',
  11548. ' procedure SetSize(Value: longword);',
  11549. ' public',
  11550. ' property Size: longword read GetSize write SetSize;',
  11551. ' property Left: word read x write y;',
  11552. ' end;',
  11553. 'procedure SetSize(Value: longword); begin end;',// check auto rename
  11554. 'function TPoint.GetSize: longword;',
  11555. 'begin',
  11556. ' x:=y;',
  11557. ' Size:=Size;',
  11558. ' Left:=Left;',
  11559. 'end;',
  11560. 'procedure TPoint.SetSize(Value: longword);',
  11561. 'begin',
  11562. 'end;',
  11563. 'var p,q: TPoint;',
  11564. 'begin',
  11565. ' p.Size:=q.Size;',
  11566. ' p.Left:=q.Left;',
  11567. '']);
  11568. ConvertProgram;
  11569. CheckSource('TestAdvRecord_Property',
  11570. LinesToStr([ // statements
  11571. 'rtl.recNewT(this, "TPoint", function () {',
  11572. ' this.x = 0;',
  11573. ' this.y = 0;',
  11574. ' this.$eq = function (b) {',
  11575. ' return (this.x === b.x) && (this.y === b.y);',
  11576. ' };',
  11577. ' this.$assign = function (s) {',
  11578. ' this.x = s.x;',
  11579. ' this.y = s.y;',
  11580. ' return this;',
  11581. ' };',
  11582. ' this.GetSize = function () {',
  11583. ' var Result = 0;',
  11584. ' this.x = this.y;',
  11585. ' this.SetSize(this.GetSize());',
  11586. ' this.y = this.x;',
  11587. ' return Result;',
  11588. ' };',
  11589. ' this.SetSize = function (Value) {',
  11590. ' };',
  11591. '});',
  11592. 'this.SetSize = function (Value) {',
  11593. '};',
  11594. 'this.p = this.TPoint.$new();',
  11595. 'this.q = this.TPoint.$new();',
  11596. '']),
  11597. LinesToStr([ // $mod.$main
  11598. '$mod.p.SetSize($mod.q.GetSize());',
  11599. '$mod.p.y = $mod.q.x;',
  11600. '']));
  11601. end;
  11602. procedure TTestModule.TestAdvRecord_PropertyDefault;
  11603. begin
  11604. StartProgram(false);
  11605. Add([
  11606. '{$modeswitch AdvancedRecords}',
  11607. 'type',
  11608. ' TPoint = record',
  11609. ' strict private',
  11610. ' function GetItems(Index: word): word;',
  11611. ' procedure SetItems(Index: word; Value: word);',
  11612. ' public',
  11613. ' property Items[Index: word]: word read GetItems write SetItems; default;',
  11614. ' end;',
  11615. 'function TPoint.GetItems(Index: word): word;',
  11616. 'begin',
  11617. ' Items[index]:=Items[index];',
  11618. ' self.Items[index]:=self.Items[index];',
  11619. 'end;',
  11620. 'procedure TPoint.SetItems(Index: word; Value: word);',
  11621. 'begin',
  11622. 'end;',
  11623. 'var p: TPoint;',
  11624. 'begin',
  11625. ' p[1]:=p[2];',
  11626. ' p.Items[3]:=p.Items[4];',
  11627. '']);
  11628. ConvertProgram;
  11629. CheckSource('TestAdvRecord_PropertyDefault',
  11630. LinesToStr([ // statements
  11631. 'rtl.recNewT(this, "TPoint", function () {',
  11632. ' this.$eq = function (b) {',
  11633. ' return true;',
  11634. ' };',
  11635. ' this.$assign = function (s) {',
  11636. ' return this;',
  11637. ' };',
  11638. ' this.GetItems = function (Index) {',
  11639. ' var Result = 0;',
  11640. ' this.SetItems(Index, this.GetItems(Index));',
  11641. ' this.SetItems(Index, this.GetItems(Index));',
  11642. ' return Result;',
  11643. ' };',
  11644. ' this.SetItems = function (Index, Value) {',
  11645. ' };',
  11646. '});',
  11647. 'this.p = this.TPoint.$new();',
  11648. '']),
  11649. LinesToStr([ // $mod.$main
  11650. '$mod.p.SetItems(1, $mod.p.GetItems(2));',
  11651. '$mod.p.SetItems(3, $mod.p.GetItems(4));',
  11652. '']));
  11653. end;
  11654. procedure TTestModule.TestAdvRecord_Property_ClassMethod;
  11655. begin
  11656. StartProgram(false);
  11657. Add([
  11658. '{$modeswitch AdvancedRecords}',
  11659. 'type',
  11660. ' TRec = record',
  11661. ' class var',
  11662. ' Fx: longint;',
  11663. ' Fy: longint;',
  11664. ' class function GetInt: longint; static;',
  11665. ' class procedure SetInt(Value: longint); static;',
  11666. ' class procedure DoIt; static;',
  11667. ' class property IntA: longint read Fx write Fy;',
  11668. ' class property IntB: longint read GetInt write SetInt;',
  11669. ' end;',
  11670. 'class function trec.getint: longint;',
  11671. 'begin',
  11672. ' result:=fx;',
  11673. 'end;',
  11674. 'class procedure trec.setint(value: longint);',
  11675. 'begin',
  11676. 'end;',
  11677. 'class procedure trec.doit;',
  11678. 'begin',
  11679. ' IntA:=IntA+1;',
  11680. ' IntB:=IntB+1;',
  11681. 'end;',
  11682. 'var r: trec;',
  11683. 'begin',
  11684. ' trec.inta:=trec.inta+1;',
  11685. ' if trec.intb=2 then;',
  11686. ' trec.intb:=trec.intb+2;',
  11687. ' trec.setint(trec.inta);',
  11688. ' r.inta:=r.inta+1;',
  11689. ' if r.intb=2 then;',
  11690. ' r.intb:=r.intb+2;',
  11691. ' r.setint(r.inta);']);
  11692. ConvertProgram;
  11693. CheckSource('TestAdvRecord_Property_ClassMethod',
  11694. LinesToStr([ // statements
  11695. 'rtl.recNewT(this, "TRec", function () {',
  11696. ' this.Fx = 0;',
  11697. ' this.Fy = 0;',
  11698. ' this.$eq = function (b) {',
  11699. ' return true;',
  11700. ' };',
  11701. ' this.$assign = function (s) {',
  11702. ' return this;',
  11703. ' };',
  11704. ' this.GetInt = function () {',
  11705. ' var Result = 0;',
  11706. ' Result = $mod.TRec.Fx;',
  11707. ' return Result;',
  11708. ' };',
  11709. ' this.SetInt = function (Value) {',
  11710. ' };',
  11711. ' this.DoIt = function () {',
  11712. ' $mod.TRec.Fy = $mod.TRec.Fx + 1;',
  11713. ' $mod.TRec.SetInt($mod.TRec.GetInt() + 1);',
  11714. ' };',
  11715. '}, true);',
  11716. 'this.r = this.TRec.$new();',
  11717. '']),
  11718. LinesToStr([ // $mod.$main
  11719. '$mod.TRec.Fy = $mod.TRec.Fx + 1;',
  11720. 'if ($mod.TRec.GetInt() === 2) ;',
  11721. '$mod.TRec.SetInt($mod.TRec.GetInt() + 2);',
  11722. '$mod.TRec.SetInt($mod.TRec.Fx);',
  11723. '$mod.TRec.Fy = $mod.r.Fx + 1;',
  11724. 'if ($mod.TRec.GetInt() === 2) ;',
  11725. '$mod.TRec.SetInt($mod.TRec.GetInt() + 2);',
  11726. '$mod.TRec.SetInt($mod.r.Fx);',
  11727. '']));
  11728. end;
  11729. procedure TTestModule.TestAdvRecord_Const;
  11730. begin
  11731. StartProgram(false);
  11732. Add([
  11733. '{$modeswitch AdvancedRecords}',
  11734. 'type',
  11735. ' TArrInt = array[3..4] of longint;',
  11736. ' TPoint = record',
  11737. ' x,y: longint;',
  11738. ' class var Count: nativeint;',
  11739. ' end;',
  11740. ' TRec = record',
  11741. ' i: longint;',
  11742. ' a: array of longint;',
  11743. ' s: array[1..2] of longint;',
  11744. ' m: array[1..2,3..4] of longint;',
  11745. ' p: TPoint;',
  11746. ' end;',
  11747. ' TPoints = array of TPoint;',
  11748. 'const',
  11749. ' r: TRec = (',
  11750. ' i:1;',
  11751. ' a:(2,3);',
  11752. ' s:(4,5);',
  11753. ' m:( (11,12), (13,14) );',
  11754. ' p: (x:21)',
  11755. ' );',
  11756. ' p: TPoints = ( (x:1;y:2), (x:3;y:4) );',
  11757. 'begin']);
  11758. ConvertProgram;
  11759. CheckSource('TestAdvRecord_Const',
  11760. LinesToStr([ // statements
  11761. 'rtl.recNewT(this, "TPoint", function () {',
  11762. ' this.x = 0;',
  11763. ' this.y = 0;',
  11764. ' this.Count = 0;',
  11765. ' this.$eq = function (b) {',
  11766. ' return (this.x === b.x) && (this.y === b.y);',
  11767. ' };',
  11768. ' this.$assign = function (s) {',
  11769. ' this.x = s.x;',
  11770. ' this.y = s.y;',
  11771. ' return this;',
  11772. ' };',
  11773. '}, true);',
  11774. 'rtl.recNewT(this, "TRec", function () {',
  11775. ' this.i = 0;',
  11776. ' this.$new = function () {',
  11777. ' var r = Object.create(this);',
  11778. ' r.a = [];',
  11779. ' r.s = rtl.arraySetLength(null, 0, 2);',
  11780. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  11781. ' r.p = $mod.TPoint.$new();',
  11782. ' return r;',
  11783. ' };',
  11784. ' this.$eq = function (b) {',
  11785. ' 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);',
  11786. ' };',
  11787. ' this.$assign = function (s) {',
  11788. ' this.i = s.i;',
  11789. ' this.a = rtl.arrayRef(s.a);',
  11790. ' this.s = s.s.slice(0);',
  11791. ' this.m = s.m.slice(0);',
  11792. ' this.p.$assign(s.p);',
  11793. ' return this;',
  11794. ' };',
  11795. '});',
  11796. 'this.r = this.TRec.$clone({',
  11797. ' i: 1,',
  11798. ' a: [2, 3],',
  11799. ' s: [4, 5],',
  11800. ' m: [[11, 12], [13, 14]],',
  11801. ' p: this.TPoint.$clone({',
  11802. ' x: 21,',
  11803. ' y: 0',
  11804. ' })',
  11805. '});',
  11806. 'this.p = [this.TPoint.$clone({',
  11807. ' x: 1,',
  11808. ' y: 2',
  11809. '}), this.TPoint.$clone({',
  11810. ' x: 3,',
  11811. ' y: 4',
  11812. '})];',
  11813. '']),
  11814. LinesToStr([ // $mod.$main
  11815. '']));
  11816. end;
  11817. procedure TTestModule.TestAdvRecord_ExternalField;
  11818. begin
  11819. StartProgram(false);
  11820. Add([
  11821. '{$modeswitch AdvancedRecords}',
  11822. '{$modeswitch externalclass}',
  11823. 'type',
  11824. ' TCar = record',
  11825. ' public',
  11826. ' Intern: longint external name ''$Intern'';',
  11827. ' Intern2: longint external name ''$Intern2'';',
  11828. ' Bracket: longint external name ''["A B"]'';',
  11829. ' procedure DoIt;',
  11830. ' end;',
  11831. 'procedure tcar.doit;',
  11832. 'begin',
  11833. ' Intern:=Intern+1;',
  11834. ' Intern2:=Intern2+2;',
  11835. ' Bracket:=Bracket+3;',
  11836. 'end;',
  11837. 'var Rec: TCar = (intern: 11; intern2: 12; bracket: 13);',
  11838. 'begin',
  11839. ' Rec.intern:=Rec.intern+1;',
  11840. ' Rec.intern2:=Rec.intern2+2;',
  11841. ' Rec.Bracket:=Rec.Bracket+3;',
  11842. ' with Rec do begin',
  11843. ' intern:=intern+1;',
  11844. ' intern2:=intern2+2;',
  11845. ' Bracket:=Bracket+3;',
  11846. ' end;']);
  11847. ConvertProgram;
  11848. CheckSource('TestAdvRecord_ExternalField',
  11849. LinesToStr([ // statements
  11850. 'rtl.recNewT(this, "TCar", function () {',
  11851. ' this.$eq = function (b) {',
  11852. ' return (this.$Intern === b.$Intern) && (this.$Intern2 === b.$Intern2) && (this["A B"] === b["A B"]);',
  11853. ' };',
  11854. ' this.$assign = function (s) {',
  11855. ' this.$Intern = s.$Intern;',
  11856. ' this.$Intern2 = s.$Intern2;',
  11857. ' this["A B"] = s["A B"];',
  11858. ' return this;',
  11859. ' };',
  11860. ' this.DoIt = function () {',
  11861. ' this.$Intern = this.$Intern + 1;',
  11862. ' this.$Intern2 = this.$Intern2 + 2;',
  11863. ' this["A B"] = this["A B"] + 3;',
  11864. ' };',
  11865. '});',
  11866. 'this.Rec = this.TCar.$clone({',
  11867. ' $Intern: 11,',
  11868. ' $Intern2: 12,',
  11869. ' "A B": 13',
  11870. '});',
  11871. '']),
  11872. LinesToStr([ // $mod.$main
  11873. '$mod.Rec.$Intern = $mod.Rec.$Intern + 1;',
  11874. '$mod.Rec.$Intern2 = $mod.Rec.$Intern2 + 2;',
  11875. '$mod.Rec["A B"] = $mod.Rec["A B"] + 3;',
  11876. 'var $with = $mod.Rec;',
  11877. '$with.$Intern = $with.$Intern + 1;',
  11878. '$with.$Intern2 = $with.$Intern2 + 2;',
  11879. '$with["A B"] = $with["A B"] + 3;',
  11880. '']));
  11881. end;
  11882. procedure TTestModule.TestAdvRecord_SubRecord;
  11883. begin
  11884. StartProgram(false);
  11885. Add([
  11886. '{$modeswitch AdvancedRecords}',
  11887. 'type',
  11888. ' TRec = record',
  11889. ' type',
  11890. ' TPoint = record',
  11891. ' x,y: longint;',
  11892. ' class var Count: nativeint;',
  11893. ' procedure DoIt;',
  11894. ' class procedure DoThat; static;',
  11895. ' end;',
  11896. ' var',
  11897. ' i: longint;',
  11898. ' p: TPoint;',
  11899. ' procedure DoSome;',
  11900. ' end;',
  11901. 'const',
  11902. ' r: TRec = (',
  11903. ' i:1;',
  11904. ' p: (x:21;y:22)',
  11905. ' );',
  11906. 'procedure TRec.DoSome;',
  11907. 'begin',
  11908. ' p.x:=p.y+1;',
  11909. ' p.Count:=p.Count+2;',
  11910. 'end;',
  11911. 'procedure TRec.TPoint.DoIt;',
  11912. 'begin',
  11913. ' Count:=Count+3;',
  11914. 'end;',
  11915. 'class procedure TRec.TPoint.DoThat;',
  11916. 'begin',
  11917. ' Count:=Count+4;',
  11918. 'end;',
  11919. 'begin']);
  11920. ConvertProgram;
  11921. CheckSource('TestAdvRecord_SubRecord',
  11922. LinesToStr([ // statements
  11923. 'rtl.recNewT(this, "TRec", function () {',
  11924. ' rtl.recNewT(this, "TPoint", function () {',
  11925. ' this.x = 0;',
  11926. ' this.y = 0;',
  11927. ' this.Count = 0;',
  11928. ' this.$eq = function (b) {',
  11929. ' return (this.x === b.x) && (this.y === b.y);',
  11930. ' };',
  11931. ' this.$assign = function (s) {',
  11932. ' this.x = s.x;',
  11933. ' this.y = s.y;',
  11934. ' return this;',
  11935. ' };',
  11936. ' this.DoIt = function () {',
  11937. ' $mod.TRec.TPoint.Count = this.Count + 3;',
  11938. ' };',
  11939. ' this.DoThat = function () {',
  11940. ' $mod.TRec.TPoint.Count = $mod.TRec.TPoint.Count + 4;',
  11941. ' };',
  11942. ' }, true);',
  11943. ' this.i = 0;',
  11944. ' this.$new = function () {',
  11945. ' var r = Object.create(this);',
  11946. ' r.p = this.TPoint.$new();',
  11947. ' return r;',
  11948. ' };',
  11949. ' this.$eq = function (b) {',
  11950. ' return (this.i === b.i) && this.p.$eq(b.p);',
  11951. ' };',
  11952. ' this.$assign = function (s) {',
  11953. ' this.i = s.i;',
  11954. ' this.p.$assign(s.p);',
  11955. ' return this;',
  11956. ' };',
  11957. ' this.DoSome = function () {',
  11958. ' this.p.x = this.p.y + 1;',
  11959. ' this.TPoint.Count = this.p.Count + 2;',
  11960. ' };',
  11961. '}, true);',
  11962. 'this.r = this.TRec.$clone({',
  11963. ' i: 1,',
  11964. ' p: this.TRec.TPoint.$clone({',
  11965. ' x: 21,',
  11966. ' y: 22',
  11967. ' })',
  11968. '});',
  11969. '']),
  11970. LinesToStr([ // $mod.$main
  11971. '']));
  11972. end;
  11973. procedure TTestModule.TestAdvRecord_SubClass;
  11974. begin
  11975. StartProgram(false);
  11976. Add([
  11977. '{$modeswitch AdvancedRecords}',
  11978. 'type',
  11979. ' TObject = class end;',
  11980. ' TPoint = record',
  11981. ' type',
  11982. ' TBird = class',
  11983. ' procedure DoIt;',
  11984. ' class procedure Glob;',
  11985. ' end;',
  11986. ' procedure DoIt(b: TBird);',
  11987. ' end;',
  11988. 'procedure TPoint.TBird.DoIt;',
  11989. 'begin',
  11990. ' doit;',
  11991. ' self.doit;',
  11992. ' glob;',
  11993. ' self.glob;',
  11994. 'end;',
  11995. 'class procedure TPoint.TBird.Glob;',
  11996. 'begin',
  11997. ' glob;',
  11998. ' self.glob;',
  11999. 'end;',
  12000. 'procedure TPoint.DoIt(b: TBird);',
  12001. 'begin',
  12002. ' b.doit;',
  12003. ' b.glob;',
  12004. ' TBird.glob;',
  12005. 'end;',
  12006. 'begin',
  12007. '']);
  12008. ConvertProgram;
  12009. CheckSource('TestAdvRecord_SubClass',
  12010. LinesToStr([ // statements
  12011. 'rtl.createClass(this, "TObject", null, function () {',
  12012. ' this.$init = function () {',
  12013. ' };',
  12014. ' this.$final = function () {',
  12015. ' };',
  12016. '});',
  12017. 'rtl.recNewT(this, "TPoint", function () {',
  12018. ' rtl.createClass(this, "TBird", $mod.TObject, function () {',
  12019. ' this.DoIt = function () {',
  12020. ' this.DoIt();',
  12021. ' this.DoIt();',
  12022. ' this.$class.Glob();',
  12023. ' this.$class.Glob();',
  12024. ' };',
  12025. ' this.Glob = function () {',
  12026. ' this.Glob();',
  12027. ' this.Glob();',
  12028. ' };',
  12029. ' }, "TPoint.TBird");',
  12030. ' this.$eq = function (b) {',
  12031. ' return true;',
  12032. ' };',
  12033. ' this.$assign = function (s) {',
  12034. ' return this;',
  12035. ' };',
  12036. ' this.DoIt = function (b) {',
  12037. ' b.DoIt();',
  12038. ' b.$class.Glob();',
  12039. ' this.TBird.Glob();',
  12040. ' };',
  12041. '}, true);',
  12042. '']),
  12043. LinesToStr([ // $mod.$main
  12044. '']));
  12045. end;
  12046. procedure TTestModule.TestAdvRecord_SubInterfaceFail;
  12047. begin
  12048. StartProgram(false);
  12049. Add([
  12050. '{$modeswitch AdvancedRecords}',
  12051. 'type',
  12052. ' IUnknown = interface end;',
  12053. ' TPoint = record',
  12054. ' type IBird = interface end;',
  12055. ' end;',
  12056. 'begin',
  12057. '']);
  12058. SetExpectedPasResolverError('not yet implemented: IBird:TPasClassType [20190105143752] "interface inside record"',
  12059. nNotYetImplemented);
  12060. ParseProgram;
  12061. end;
  12062. procedure TTestModule.TestAdvRecord_Constructor;
  12063. begin
  12064. StartProgram(false);
  12065. Add([
  12066. '{$modeswitch AdvancedRecords}',
  12067. 'type',
  12068. ' TPoint = record',
  12069. ' x,y: longint;',
  12070. ' class procedure Run(w: longint = 13); static;',
  12071. ' constructor Create(ax: longint; ay: longint = -1);',
  12072. ' end;',
  12073. 'class procedure tpoint.run(w: longint);',
  12074. 'begin',
  12075. ' run;',
  12076. ' run();',
  12077. 'end;',
  12078. 'constructor tpoint.create(ax,ay: longint);',
  12079. 'begin',
  12080. ' x:=ax;',
  12081. ' self.y:=ay;',
  12082. ' run;',
  12083. ' run(ax);',
  12084. 'end;',
  12085. 'var r: TPoint;',
  12086. 'begin',
  12087. ' r:=TPoint.Create(1,2);',
  12088. ' with TPoint do r:=Create(1,2);',
  12089. ' r.Create(3);',
  12090. ' r:=r.Create(4);',
  12091. '']);
  12092. ConvertProgram;
  12093. CheckSource('TestAdvRecord_Constructor',
  12094. LinesToStr([ // statements
  12095. 'rtl.recNewT(this, "TPoint", function () {',
  12096. ' this.x = 0;',
  12097. ' this.y = 0;',
  12098. ' this.$eq = function (b) {',
  12099. ' return (this.x === b.x) && (this.y === b.y);',
  12100. ' };',
  12101. ' this.$assign = function (s) {',
  12102. ' this.x = s.x;',
  12103. ' this.y = s.y;',
  12104. ' return this;',
  12105. ' };',
  12106. ' this.Run = function (w) {',
  12107. ' $mod.TPoint.Run(13);',
  12108. ' $mod.TPoint.Run(13);',
  12109. ' };',
  12110. ' this.Create = function (ax, ay) {',
  12111. ' this.x = ax;',
  12112. ' this.y = ay;',
  12113. ' this.Run(13);',
  12114. ' this.Run(ax);',
  12115. ' return this;',
  12116. ' };',
  12117. '});',
  12118. 'this.r = this.TPoint.$new();',
  12119. '']),
  12120. LinesToStr([ // $mod.$main
  12121. '$mod.r.$assign($mod.TPoint.$new().Create(1, 2));',
  12122. 'var $with = $mod.TPoint;',
  12123. '$mod.r.$assign($with.$new().Create(1, 2));',
  12124. '$mod.r.Create(3, -1);',
  12125. '$mod.r.$assign($mod.r.Create(4, -1));',
  12126. '']));
  12127. end;
  12128. procedure TTestModule.TestAdvRecord_ClassConstructor_Program;
  12129. begin
  12130. StartProgram(false);
  12131. Add([
  12132. '{$modeswitch AdvancedRecords}',
  12133. 'type',
  12134. ' TPoint = record',
  12135. ' class var x: longint;',
  12136. ' class procedure Fly; static;',
  12137. ' class constructor Init;',
  12138. ' end;',
  12139. 'var count: word;',
  12140. 'class procedure Tpoint.Fly;',
  12141. 'begin',
  12142. 'end;',
  12143. 'class constructor tpoint.init;',
  12144. 'begin',
  12145. ' count:=count+1;',
  12146. ' x:=x+3;',
  12147. ' tpoint.x:=tpoint.x+4;',
  12148. ' fly;',
  12149. ' tpoint.fly;',
  12150. 'end;',
  12151. 'var r: TPoint;',
  12152. 'begin',
  12153. ' r.x:=r.x+10;',
  12154. ' r.Fly;',
  12155. ' r.Fly();',
  12156. '']);
  12157. ConvertProgram;
  12158. CheckSource('TestAdvRecord_ClassConstructor_Program',
  12159. LinesToStr([ // statements
  12160. 'rtl.recNewT(this, "TPoint", function () {',
  12161. ' this.x = 0;',
  12162. ' this.$eq = function (b) {',
  12163. ' return true;',
  12164. ' };',
  12165. ' this.$assign = function (s) {',
  12166. ' return this;',
  12167. ' };',
  12168. ' this.Fly = function () {',
  12169. ' };',
  12170. '}, true);',
  12171. 'this.count = 0;',
  12172. 'this.r = this.TPoint.$new();',
  12173. '']),
  12174. LinesToStr([ // $mod.$main
  12175. '(function () {',
  12176. ' $mod.count = $mod.count + 1;',
  12177. ' $mod.TPoint.x = $mod.TPoint.x + 3;',
  12178. ' $mod.TPoint.x = $mod.TPoint.x + 4;',
  12179. ' $mod.TPoint.Fly();',
  12180. ' $mod.TPoint.Fly();',
  12181. '})();',
  12182. '$mod.TPoint.x = $mod.r.x + 10;',
  12183. '$mod.TPoint.Fly();',
  12184. '$mod.TPoint.Fly();',
  12185. '']));
  12186. end;
  12187. procedure TTestModule.TestAdvRecord_ClassConstructor_Unit;
  12188. begin
  12189. StartUnit(false);
  12190. Add([
  12191. 'interface',
  12192. '{$modeswitch AdvancedRecords}',
  12193. 'type',
  12194. ' TPoint = record',
  12195. ' class var x: longint;',
  12196. ' class procedure Fly; static;',
  12197. ' class constructor Init;',
  12198. ' end;',
  12199. 'implementation',
  12200. 'var count: word;',
  12201. 'class procedure Tpoint.Fly;',
  12202. 'begin',
  12203. 'end;',
  12204. 'class constructor tpoint.init;',
  12205. 'begin',
  12206. ' count:=count+1;',
  12207. ' x:=3;',
  12208. ' tpoint.x:=4;',
  12209. ' fly;',
  12210. ' tpoint.fly;',
  12211. 'end;',
  12212. '']);
  12213. ConvertUnit;
  12214. CheckSource('TestAdvRecord_ClassConstructor_Unit',
  12215. LinesToStr([ // statements
  12216. 'var $impl = $mod.$impl;',
  12217. 'rtl.recNewT(this, "TPoint", function () {',
  12218. ' this.x = 0;',
  12219. ' this.$eq = function (b) {',
  12220. ' return true;',
  12221. ' };',
  12222. ' this.$assign = function (s) {',
  12223. ' return this;',
  12224. ' };',
  12225. ' this.Fly = function () {',
  12226. ' };',
  12227. '}, true);',
  12228. '']),
  12229. LinesToStr([ // $mod.$init
  12230. '(function () {',
  12231. ' $impl.count = $impl.count + 1;',
  12232. ' $mod.TPoint.x = 3;',
  12233. ' $mod.TPoint.x = 4;',
  12234. ' $mod.TPoint.Fly();',
  12235. ' $mod.TPoint.Fly();',
  12236. '})();',
  12237. '']),
  12238. LinesToStr([ // $mod.$main
  12239. '$impl.count = 0;',
  12240. '']));
  12241. end;
  12242. procedure TTestModule.TestClass_TObjectDefaultConstructor;
  12243. begin
  12244. StartProgram(false);
  12245. Add(['type',
  12246. ' TObject = class',
  12247. ' public',
  12248. ' constructor Create;',
  12249. ' destructor Destroy;',
  12250. ' end;',
  12251. ' TBird = TObject;',
  12252. 'constructor tobject.create;',
  12253. 'begin end;',
  12254. 'destructor tobject.destroy;',
  12255. 'begin end;',
  12256. 'var Obj: tobject;',
  12257. 'begin',
  12258. ' obj:=tobject.create;',
  12259. ' obj:=tobject.create();',
  12260. ' obj:=tbird.create;',
  12261. ' obj:=tbird.create();',
  12262. ' obj:=obj.create();',
  12263. ' obj.destroy;',
  12264. '']);
  12265. ConvertProgram;
  12266. CheckSource('TestClass_TObjectDefaultConstructor',
  12267. LinesToStr([ // statements
  12268. 'rtl.createClass(this,"TObject",null,function(){',
  12269. ' this.$init = function () {',
  12270. ' };',
  12271. ' this.$final = function () {',
  12272. ' };',
  12273. ' this.Create = function(){',
  12274. ' return this;',
  12275. ' };',
  12276. ' this.Destroy = function(){',
  12277. ' };',
  12278. '});',
  12279. 'this.Obj = null;'
  12280. ]),
  12281. LinesToStr([ // $mod.$main
  12282. '$mod.Obj = $mod.TObject.$create("Create");',
  12283. '$mod.Obj = $mod.TObject.$create("Create");',
  12284. '$mod.Obj = $mod.TObject.$create("Create");',
  12285. '$mod.Obj = $mod.TObject.$create("Create");',
  12286. '$mod.Obj = $mod.Obj.Create();',
  12287. '$mod.Obj.$destroy("Destroy");',
  12288. '']));
  12289. end;
  12290. procedure TTestModule.TestClass_TObjectConstructorWithParams;
  12291. begin
  12292. StartProgram(false);
  12293. Add('type');
  12294. Add(' TObject = class');
  12295. Add(' public');
  12296. Add(' constructor Create(Par: longint);');
  12297. Add(' end;');
  12298. Add('constructor tobject.create(par: longint);');
  12299. Add('begin end;');
  12300. Add('var Obj: tobject;');
  12301. Add('begin');
  12302. Add(' obj:=tobject.create(3);');
  12303. ConvertProgram;
  12304. CheckSource('TestClass_TObjectConstructorWithParams',
  12305. LinesToStr([ // statements
  12306. 'rtl.createClass(this,"TObject",null,function(){',
  12307. ' this.$init = function () {',
  12308. ' };',
  12309. ' this.$final = function () {',
  12310. ' };',
  12311. ' this.Create = function(Par){',
  12312. ' return this;',
  12313. ' };',
  12314. '});',
  12315. 'this.Obj = null;'
  12316. ]),
  12317. LinesToStr([ // $mod.$main
  12318. '$mod.Obj = $mod.TObject.$create("Create",[3]);'
  12319. ]));
  12320. end;
  12321. procedure TTestModule.TestClass_TObjectConstructorWithDefaultParam;
  12322. begin
  12323. StartProgram(false);
  12324. Add('type');
  12325. Add(' TObject = class');
  12326. Add(' public');
  12327. Add(' constructor Create;');
  12328. Add(' end;');
  12329. Add(' TTest = class(TObject)');
  12330. Add(' public');
  12331. Add(' constructor Create(const Par: longint = 1);');
  12332. Add(' end;');
  12333. Add('constructor tobject.create;');
  12334. Add('begin end;');
  12335. Add('constructor ttest.create(const par: longint);');
  12336. Add('begin end;');
  12337. Add('var t: ttest;');
  12338. Add('begin');
  12339. Add(' t:=ttest.create;');
  12340. Add(' t:=ttest.create(2);');
  12341. ConvertProgram;
  12342. CheckSource('TestClass_TObjectConstructorWithDefaultParam',
  12343. LinesToStr([ // statements
  12344. 'rtl.createClass(this,"TObject",null,function(){',
  12345. ' this.$init = function () {',
  12346. ' };',
  12347. ' this.$final = function () {',
  12348. ' };',
  12349. ' this.Create = function(){',
  12350. ' return this;',
  12351. ' };',
  12352. '});',
  12353. 'rtl.createClass(this, "TTest", this.TObject, function () {',
  12354. ' this.Create$1 = function (Par) {',
  12355. ' return this;',
  12356. ' };',
  12357. '});',
  12358. 'this.t = null;'
  12359. ]),
  12360. LinesToStr([ // $mod.$main
  12361. '$mod.t = $mod.TTest.$create("Create$1", [1]);',
  12362. '$mod.t = $mod.TTest.$create("Create$1", [2]);'
  12363. ]));
  12364. end;
  12365. procedure TTestModule.TestClass_Var;
  12366. begin
  12367. StartProgram(false);
  12368. Add([
  12369. 'type',
  12370. ' TObject = class',
  12371. ' public',
  12372. ' vI: longint;',
  12373. ' constructor Create(Par: longint);',
  12374. ' end;',
  12375. 'constructor tobject.create(par: longint);',
  12376. 'begin',
  12377. ' vi:=par+3',
  12378. 'end;',
  12379. 'var Obj: tobject;',
  12380. 'begin',
  12381. ' obj:=tobject.create(4);',
  12382. ' obj.vi:=obj.VI+5;']);
  12383. ConvertProgram;
  12384. CheckSource('TestClass_Var',
  12385. LinesToStr([ // statements
  12386. 'rtl.createClass(this,"TObject",null,function(){',
  12387. ' this.$init = function () {',
  12388. ' this.vI = 0;',
  12389. ' };',
  12390. ' this.$final = function () {',
  12391. ' };',
  12392. ' this.Create = function(Par){',
  12393. ' this.vI = Par+3;',
  12394. ' return this;',
  12395. ' };',
  12396. '});',
  12397. 'this.Obj = null;'
  12398. ]),
  12399. LinesToStr([ // $mod.$main
  12400. '$mod.Obj = $mod.TObject.$create("Create",[4]);',
  12401. '$mod.Obj.vI = $mod.Obj.vI + 5;'
  12402. ]));
  12403. end;
  12404. procedure TTestModule.TestClass_Method;
  12405. begin
  12406. StartProgram(false);
  12407. Add('type');
  12408. Add(' TObject = class');
  12409. Add(' public');
  12410. Add(' vI: longint;');
  12411. Add(' Sub: TObject;');
  12412. Add(' constructor Create;');
  12413. Add(' function GetIt(Par: longint): tobject;');
  12414. Add(' end;');
  12415. Add('constructor tobject.create; begin end;');
  12416. Add('function tobject.getit(par: longint): tobject;');
  12417. Add('begin');
  12418. Add(' Self.vi:=par+3;');
  12419. Add(' Result:=self.sub;');
  12420. Add('end;');
  12421. Add('var Obj: tobject;');
  12422. Add('begin');
  12423. Add(' obj:=tobject.create;');
  12424. Add(' obj.getit(4);');
  12425. Add(' obj.sub.sub:=nil;');
  12426. Add(' obj.sub.getit(5);');
  12427. Add(' obj.sub.getit(6).SUB:=nil;');
  12428. Add(' obj.sub.getit(7).GETIT(8);');
  12429. Add(' obj.sub.getit(9).SuB.getit(10);');
  12430. ConvertProgram;
  12431. CheckSource('TestClass_Method',
  12432. LinesToStr([ // statements
  12433. 'rtl.createClass(this,"TObject",null,function(){',
  12434. ' this.$init = function () {',
  12435. ' this.vI = 0;',
  12436. ' this.Sub = null;',
  12437. ' };',
  12438. ' this.$final = function () {',
  12439. ' this.Sub = undefined;',
  12440. ' };',
  12441. ' this.Create = function(){',
  12442. ' return this;',
  12443. ' };',
  12444. ' this.GetIt = function(Par){',
  12445. ' var Result = null;',
  12446. ' this.vI = Par + 3;',
  12447. ' Result = this.Sub;',
  12448. ' return Result;',
  12449. ' };',
  12450. '});',
  12451. 'this.Obj = null;'
  12452. ]),
  12453. LinesToStr([ // $mod.$main
  12454. '$mod.Obj = $mod.TObject.$create("Create");',
  12455. '$mod.Obj.GetIt(4);',
  12456. '$mod.Obj.Sub.Sub=null;',
  12457. '$mod.Obj.Sub.GetIt(5);',
  12458. '$mod.Obj.Sub.GetIt(6).Sub=null;',
  12459. '$mod.Obj.Sub.GetIt(7).GetIt(8);',
  12460. '$mod.Obj.Sub.GetIt(9).Sub.GetIt(10);'
  12461. ]));
  12462. end;
  12463. procedure TTestModule.TestClass_Implementation;
  12464. begin
  12465. StartUnit(false);
  12466. Add([
  12467. 'interface',
  12468. 'type',
  12469. ' TObject = class',
  12470. ' constructor Create;',
  12471. ' end;',
  12472. 'implementation',
  12473. 'type',
  12474. ' TIntClass = class',
  12475. ' constructor Create; reintroduce;',
  12476. ' class procedure DoGlob;',
  12477. ' end;',
  12478. 'constructor tintclass.create;',
  12479. 'begin',
  12480. ' inherited;',
  12481. ' inherited create;',
  12482. ' doglob;',
  12483. 'end;',
  12484. 'class procedure tintclass.doglob;',
  12485. 'begin',
  12486. 'end;',
  12487. 'constructor tobject.create;',
  12488. 'var',
  12489. ' iC: tintclass;',
  12490. 'begin',
  12491. ' ic:=tintclass.create;',
  12492. ' tintclass.doglob;',
  12493. ' ic.doglob;',
  12494. 'end;',
  12495. 'initialization',
  12496. ' tintclass.doglob;',
  12497. '']);
  12498. ConvertUnit;
  12499. CheckSource('TestClass_Implementation',
  12500. LinesToStr([ // statements
  12501. 'var $impl = $mod.$impl;',
  12502. 'rtl.createClass(this, "TObject", null, function () {',
  12503. ' this.$init = function () {',
  12504. ' };',
  12505. ' this.$final = function () {',
  12506. ' };',
  12507. ' this.Create = function () {',
  12508. ' var iC = null;',
  12509. ' iC = $impl.TIntClass.$create("Create$1");',
  12510. ' $impl.TIntClass.DoGlob();',
  12511. ' iC.$class.DoGlob();',
  12512. ' return this;',
  12513. ' };',
  12514. '});',
  12515. '']),
  12516. LinesToStr([ // $mod.$main
  12517. '$impl.TIntClass.DoGlob();',
  12518. '']),
  12519. LinesToStr([
  12520. 'rtl.createClass($impl, "TIntClass", $mod.TObject, function () {',
  12521. ' this.Create$1 = function () {',
  12522. ' $mod.TObject.Create.call(this);',
  12523. ' $mod.TObject.Create.call(this);',
  12524. ' this.$class.DoGlob();',
  12525. ' return this;',
  12526. ' };',
  12527. ' this.DoGlob = function () {',
  12528. ' };',
  12529. '});',
  12530. '']));
  12531. end;
  12532. procedure TTestModule.TestClass_Inheritance;
  12533. begin
  12534. StartProgram(false);
  12535. Add('type');
  12536. Add(' TObject = class');
  12537. Add(' public');
  12538. Add(' constructor Create;');
  12539. Add(' end;');
  12540. Add(' TClassA = class');
  12541. Add(' end;');
  12542. Add(' TClassB = class(TObject)');
  12543. Add(' procedure ProcB;');
  12544. Add(' end;');
  12545. Add('constructor tobject.create; begin end;');
  12546. Add('procedure tclassb.procb; begin end;');
  12547. Add('var');
  12548. Add(' oO: TObject;');
  12549. Add(' oA: TClassA;');
  12550. Add(' oB: TClassB;');
  12551. Add('begin');
  12552. Add(' oO:=tobject.Create;');
  12553. Add(' oA:=tclassa.Create;');
  12554. Add(' ob:=tclassb.Create;');
  12555. Add(' if oo is tclassa then ;');
  12556. Add(' ob:=oo as tclassb;');
  12557. Add(' (oo as tclassb).procb;');
  12558. ConvertProgram;
  12559. CheckSource('TestClass_Inheritance',
  12560. LinesToStr([ // statements
  12561. 'rtl.createClass(this,"TObject",null,function(){',
  12562. ' this.$init = function () {',
  12563. ' };',
  12564. ' this.$final = function () {',
  12565. ' };',
  12566. ' this.Create = function () {',
  12567. ' return this;',
  12568. ' };',
  12569. '});',
  12570. 'rtl.createClass(this,"TClassA",this.TObject,function(){',
  12571. '});',
  12572. 'rtl.createClass(this,"TClassB",this.TObject,function(){',
  12573. ' this.ProcB = function () {',
  12574. ' };',
  12575. '});',
  12576. 'this.oO = null;',
  12577. 'this.oA = null;',
  12578. 'this.oB = null;'
  12579. ]),
  12580. LinesToStr([ // $mod.$main
  12581. '$mod.oO = $mod.TObject.$create("Create");',
  12582. '$mod.oA = $mod.TClassA.$create("Create");',
  12583. '$mod.oB = $mod.TClassB.$create("Create");',
  12584. 'if ($mod.TClassA.isPrototypeOf($mod.oO));',
  12585. '$mod.oB = rtl.as($mod.oO, $mod.TClassB);',
  12586. 'rtl.as($mod.oO, $mod.TClassB).ProcB();'
  12587. ]));
  12588. end;
  12589. procedure TTestModule.TestClass_TypeAlias;
  12590. begin
  12591. StartProgram(false);
  12592. Add([
  12593. '{$interfaces corba}',
  12594. 'type',
  12595. ' IObject = interface',
  12596. ' end;',
  12597. ' IBird = type IObject;',
  12598. ' TObject = class',
  12599. ' end;',
  12600. ' TBird = type TObject;',
  12601. 'var',
  12602. ' oObj: TObject;',
  12603. ' oBird: TBird;',
  12604. ' IntfObj: IObject;',
  12605. ' IntfBird: IBird;',
  12606. 'begin',
  12607. ' oObj:=oBird;',
  12608. '']);
  12609. ConvertProgram;
  12610. CheckSource('TestClass_TypeAlias',
  12611. LinesToStr([ // statements
  12612. 'rtl.createInterface(this, "IObject", "{B92D5841-6F2A-306A-8000-000000000000}", [], null);',
  12613. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-387B-AE88-F10981585074}", [], this.IObject);',
  12614. 'rtl.createClass(this, "TObject", null, function () {',
  12615. ' this.$init = function () {',
  12616. ' };',
  12617. ' this.$final = function () {',
  12618. ' };',
  12619. '});',
  12620. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  12621. '});',
  12622. 'this.oObj = null;',
  12623. 'this.oBird = null;',
  12624. 'this.IntfObj = null;',
  12625. 'this.IntfBird = null;',
  12626. '']),
  12627. LinesToStr([ // $mod.$main
  12628. '$mod.oObj = $mod.oBird;',
  12629. '']));
  12630. end;
  12631. procedure TTestModule.TestClass_AbstractMethod;
  12632. begin
  12633. StartProgram(false);
  12634. Add('type');
  12635. Add(' TObject = class');
  12636. Add(' public');
  12637. Add(' procedure DoIt; virtual; abstract;');
  12638. Add(' end;');
  12639. Add('begin');
  12640. ConvertProgram;
  12641. CheckSource('TestClass_AbstractMethod',
  12642. LinesToStr([ // statements
  12643. 'rtl.createClass(this,"TObject",null,function(){',
  12644. ' this.$init = function () {',
  12645. ' };',
  12646. ' this.$final = function () {',
  12647. ' };',
  12648. '});'
  12649. ]),
  12650. LinesToStr([ // this.$main
  12651. ''
  12652. ]));
  12653. end;
  12654. procedure TTestModule.TestClass_CallInherited_ProcNoParams;
  12655. begin
  12656. StartProgram(false);
  12657. Add([
  12658. 'type',
  12659. ' TObject = class',
  12660. ' procedure DoAbstract; virtual; abstract;',
  12661. ' procedure DoVirtual; virtual;',
  12662. ' procedure DoIt;',
  12663. ' end;',
  12664. ' TA = class',
  12665. ' procedure doabstract; override;',
  12666. ' procedure dovirtual; override;',
  12667. ' procedure DoSome;',
  12668. ' end;',
  12669. 'procedure tobject.dovirtual;',
  12670. 'begin',
  12671. ' inherited; // call non existing ancestor -> ignore silently',
  12672. 'end;',
  12673. 'procedure tobject.doit;',
  12674. 'begin',
  12675. 'end;',
  12676. 'procedure ta.doabstract;',
  12677. 'begin',
  12678. ' inherited dovirtual; // call TObject.DoVirtual',
  12679. 'end;',
  12680. 'procedure ta.dovirtual;',
  12681. 'begin',
  12682. ' inherited; // call TObject.DoVirtual',
  12683. ' inherited dovirtual; // call TObject.DoVirtual',
  12684. ' inherited dovirtual(); // call TObject.DoVirtual',
  12685. ' doit;',
  12686. ' doit();',
  12687. 'end;',
  12688. 'procedure ta.dosome;',
  12689. 'begin',
  12690. ' inherited; // call non existing ancestor method -> silently ignore',
  12691. 'end;',
  12692. 'begin']);
  12693. ConvertProgram;
  12694. CheckSource('TestClass_CallInherited_ProcNoParams',
  12695. LinesToStr([ // statements
  12696. 'rtl.createClass(this,"TObject",null,function(){',
  12697. ' this.$init = function () {',
  12698. ' };',
  12699. ' this.$final = function () {',
  12700. ' };',
  12701. ' this.DoVirtual = function () {',
  12702. ' };',
  12703. ' this.DoIt = function () {',
  12704. ' };',
  12705. '});',
  12706. 'rtl.createClass(this, "TA", this.TObject, function () {',
  12707. ' this.DoAbstract = function () {',
  12708. ' $mod.TObject.DoVirtual.call(this);',
  12709. ' };',
  12710. ' this.DoVirtual = function () {',
  12711. ' $mod.TObject.DoVirtual.call(this);',
  12712. ' $mod.TObject.DoVirtual.call(this);',
  12713. ' $mod.TObject.DoVirtual.call(this);',
  12714. ' this.DoIt();',
  12715. ' this.DoIt();',
  12716. ' };',
  12717. ' this.DoSome = function () {',
  12718. ' };',
  12719. '});'
  12720. ]),
  12721. LinesToStr([ // this.$main
  12722. ''
  12723. ]));
  12724. end;
  12725. procedure TTestModule.TestClass_CallInherited_WithParams;
  12726. begin
  12727. StartProgram(false);
  12728. Add([
  12729. 'type',
  12730. ' TObject = class',
  12731. ' procedure DoAbstract(pA: longint; pB: longint = 0); virtual; abstract;',
  12732. ' procedure DoVirtual(pA: longint; pB: longint = 0); virtual;',
  12733. ' procedure DoIt(pA: longint; pB: longint = 0);',
  12734. ' procedure DoIt2(pA: longint = 1; pB: longint = 2);',
  12735. ' function GetIt(pA: longint = 1; pB: longint = 2): longint;',
  12736. ' end;',
  12737. ' TClassA = class',
  12738. ' procedure DoAbstract(pA: longint; pB: longint = 0); override;',
  12739. ' procedure DoVirtual(pA: longint; pB: longint = 0); override;',
  12740. ' function GetIt(pA: longint = 1; pB: longint = 2): longint;',
  12741. ' end;',
  12742. 'procedure tobject.dovirtual(pa: longint; pb: longint = 0);',
  12743. 'begin',
  12744. 'end;',
  12745. 'procedure tobject.doit(pa: longint; pb: longint = 0);',
  12746. 'begin',
  12747. 'end;',
  12748. 'procedure tobject.doit2(pa: longint; pb: longint = 0);',
  12749. 'begin',
  12750. 'end;',
  12751. 'function tobject.getit(pa: longint; pb: longint = 0): longint;',
  12752. 'begin',
  12753. 'end;',
  12754. 'procedure tclassa.doabstract(pa: longint; pb: longint = 0);',
  12755. 'begin',
  12756. ' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)',
  12757. ' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)',
  12758. 'end;',
  12759. 'procedure tclassa.dovirtual(pa: longint; pb: longint = 0);',
  12760. 'begin',
  12761. ' inherited; // call TObject.DoVirtual(pA,pB)',
  12762. ' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)',
  12763. ' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)',
  12764. ' doit(pa,pb);',
  12765. ' doit(pa);',
  12766. ' doit2(pa);',
  12767. ' doit2;',
  12768. 'end;',
  12769. 'function tclassa.getit(pa: longint; pb: longint = 0): longint;',
  12770. 'begin',
  12771. ' pa:=inherited;',
  12772. 'end;',
  12773. 'begin']);
  12774. ConvertProgram;
  12775. CheckSource('TestClass_CallInherited_WithParams',
  12776. LinesToStr([ // statements
  12777. 'rtl.createClass(this,"TObject",null,function(){',
  12778. ' this.$init = function () {',
  12779. ' };',
  12780. ' this.$final = function () {',
  12781. ' };',
  12782. ' this.DoVirtual = function (pA,pB) {',
  12783. ' };',
  12784. ' this.DoIt = function (pA,pB) {',
  12785. ' };',
  12786. ' this.DoIt2 = function (pA,pB) {',
  12787. ' };',
  12788. ' this.GetIt = function (pA, pB) {',
  12789. ' var Result = 0;',
  12790. ' return Result;',
  12791. ' };',
  12792. '});',
  12793. 'rtl.createClass(this, "TClassA", this.TObject, function () {',
  12794. ' this.DoAbstract = function (pA,pB) {',
  12795. ' $mod.TObject.DoVirtual.call(this,pA,pB);',
  12796. ' $mod.TObject.DoVirtual.call(this,pA,0);',
  12797. ' };',
  12798. ' this.DoVirtual = function (pA,pB) {',
  12799. ' $mod.TObject.DoVirtual.apply(this, arguments);',
  12800. ' $mod.TObject.DoVirtual.call(this,pA,pB);',
  12801. ' $mod.TObject.DoVirtual.call(this,pA,0);',
  12802. ' this.DoIt(pA,pB);',
  12803. ' this.DoIt(pA,0);',
  12804. ' this.DoIt2(pA,2);',
  12805. ' this.DoIt2(1,2);',
  12806. ' };',
  12807. ' this.GetIt$1 = function (pA, pB) {',
  12808. ' var Result = 0;',
  12809. ' pA = $mod.TObject.GetIt.apply(this, arguments);',
  12810. ' return Result;',
  12811. ' };',
  12812. '});'
  12813. ]),
  12814. LinesToStr([ // this.$main
  12815. ''
  12816. ]));
  12817. end;
  12818. procedure TTestModule.TestClasS_CallInheritedConstructor;
  12819. begin
  12820. StartProgram(false);
  12821. Add('type');
  12822. Add(' TObject = class');
  12823. Add(' constructor Create; virtual;');
  12824. Add(' constructor CreateWithB(b: boolean);');
  12825. Add(' end;');
  12826. Add(' TA = class');
  12827. Add(' constructor Create; override;');
  12828. Add(' constructor CreateWithC(c: char);');
  12829. Add(' procedure DoIt;');
  12830. Add(' class function DoSome: TObject;');
  12831. Add(' end;');
  12832. Add('constructor tobject.create;');
  12833. Add('begin');
  12834. Add(' inherited; // call non existing ancestor -> ignore silently');
  12835. Add('end;');
  12836. Add('constructor tobject.createwithb(b: boolean);');
  12837. Add('begin');
  12838. Add(' inherited; // call non existing ancestor -> ignore silently');
  12839. Add(' create; // normal call');
  12840. Add('end;');
  12841. Add('constructor ta.create;');
  12842. Add('begin');
  12843. Add(' inherited; // normal call TObject.Create');
  12844. Add(' inherited create; // normal call TObject.Create');
  12845. Add(' inherited createwithb(false); // normal call TObject.CreateWithB');
  12846. Add('end;');
  12847. Add('constructor ta.createwithc(c: char);');
  12848. Add('begin');
  12849. Add(' inherited create; // call TObject.Create');
  12850. Add(' inherited createwithb(true); // call TObject.CreateWithB');
  12851. Add(' doit;');
  12852. Add(' doit();');
  12853. Add(' dosome;');
  12854. Add('end;');
  12855. Add('procedure ta.doit;');
  12856. Add('begin');
  12857. Add(' create; // normal call');
  12858. Add(' createwithb(false); // normal call');
  12859. Add(' createwithc(''c''); // normal call');
  12860. Add('end;');
  12861. Add('class function ta.dosome: TObject;');
  12862. Add('begin');
  12863. Add(' Result:=create; // constructor');
  12864. Add(' Result:=createwithb(true); // constructor');
  12865. Add(' Result:=createwithc(''c''); // constructor');
  12866. Add('end;');
  12867. Add('begin');
  12868. ConvertProgram;
  12869. CheckSource('TestClass_CallInheritedConstructor',
  12870. LinesToStr([ // statements
  12871. 'rtl.createClass(this,"TObject",null,function(){',
  12872. ' this.$init = function () {',
  12873. ' };',
  12874. ' this.$final = function () {',
  12875. ' };',
  12876. ' this.Create = function () {',
  12877. ' return this;',
  12878. ' };',
  12879. ' this.CreateWithB = function (b) {',
  12880. ' this.Create();',
  12881. ' return this;',
  12882. ' };',
  12883. '});',
  12884. 'rtl.createClass(this, "TA", this.TObject, function () {',
  12885. ' this.Create = function () {',
  12886. ' $mod.TObject.Create.call(this);',
  12887. ' $mod.TObject.Create.call(this);',
  12888. ' $mod.TObject.CreateWithB.call(this, false);',
  12889. ' return this;',
  12890. ' };',
  12891. ' this.CreateWithC = function (c) {',
  12892. ' $mod.TObject.Create.call(this);',
  12893. ' $mod.TObject.CreateWithB.call(this, true);',
  12894. ' this.DoIt();',
  12895. ' this.DoIt();',
  12896. ' this.$class.DoSome();',
  12897. ' return this;',
  12898. ' };',
  12899. ' this.DoIt = function () {',
  12900. ' this.Create();',
  12901. ' this.CreateWithB(false);',
  12902. ' this.CreateWithC("c");',
  12903. ' };',
  12904. ' this.DoSome = function () {',
  12905. ' var Result = null;',
  12906. ' Result = this.$create("Create");',
  12907. ' Result = this.$create("CreateWithB", [true]);',
  12908. ' Result = this.$create("CreateWithC", ["c"]);',
  12909. ' return Result;',
  12910. ' };',
  12911. '});'
  12912. ]),
  12913. LinesToStr([ // this.$main
  12914. ''
  12915. ]));
  12916. end;
  12917. procedure TTestModule.TestClass_ClassVar_Assign;
  12918. begin
  12919. StartProgram(false);
  12920. Add([
  12921. 'type',
  12922. ' TObject = class',
  12923. ' public',
  12924. ' class var vI: longint;',
  12925. ' class var Sub: TObject;',
  12926. ' constructor Create;',
  12927. ' class function GetIt(var Par: longint): tobject;',
  12928. ' end;',
  12929. 'constructor tobject.create;',
  12930. 'begin',
  12931. ' vi:=vi+1;',
  12932. ' Self.vi:=Self.vi+1;',
  12933. ' inc(vi);',
  12934. 'end;',
  12935. 'class function tobject.getit(var par: longint): tobject;',
  12936. 'begin',
  12937. ' vi:=vi+3;',
  12938. ' Self.vi:=Self.vi+4;',
  12939. ' inc(vi);',
  12940. ' Result:=self.sub;',
  12941. ' GetIt(vi);',
  12942. 'end;',
  12943. 'var Obj: tobject;',
  12944. 'begin',
  12945. ' obj:=tobject.create;',
  12946. ' tobject.vi:=3;',
  12947. ' if tobject.vi=4 then ;',
  12948. ' tobject.sub:=nil;',
  12949. ' obj.sub:=nil;',
  12950. ' obj.sub.sub:=nil;']);
  12951. ConvertProgram;
  12952. CheckSource('TestClass_ClassVar_Assign',
  12953. LinesToStr([ // statements
  12954. 'rtl.createClass(this,"TObject",null,function(){',
  12955. ' this.vI = 0;',
  12956. ' this.Sub = null;',
  12957. ' this.$init = function () {',
  12958. ' };',
  12959. ' this.$final = function () {',
  12960. ' };',
  12961. ' this.Create = function(){',
  12962. ' $mod.TObject.vI = this.vI+1;',
  12963. ' $mod.TObject.vI = this.vI+1;',
  12964. ' $mod.TObject.vI += 1;',
  12965. ' return this;',
  12966. ' };',
  12967. ' this.GetIt = function(Par){',
  12968. ' var Result = null;',
  12969. ' $mod.TObject.vI = this.vI + 3;',
  12970. ' $mod.TObject.vI = this.vI + 4;',
  12971. ' $mod.TObject.vI += 1;',
  12972. ' Result = this.Sub;',
  12973. ' this.GetIt({',
  12974. ' p: $mod.TObject,',
  12975. ' get: function () {',
  12976. ' return this.p.vI;',
  12977. ' },',
  12978. ' set: function (v) {',
  12979. ' this.p.vI = v;',
  12980. ' }',
  12981. ' });',
  12982. ' return Result;',
  12983. ' };',
  12984. '});',
  12985. 'this.Obj = null;'
  12986. ]),
  12987. LinesToStr([ // $mod.$main
  12988. '$mod.Obj = $mod.TObject.$create("Create");',
  12989. '$mod.TObject.vI = 3;',
  12990. 'if ($mod.TObject.vI === 4);',
  12991. '$mod.TObject.Sub=null;',
  12992. '$mod.TObject.Sub=null;',
  12993. '$mod.TObject.Sub=null;',
  12994. '']));
  12995. end;
  12996. procedure TTestModule.TestClass_CallClassMethod;
  12997. begin
  12998. StartProgram(false);
  12999. Add('type');
  13000. Add(' TObject = class');
  13001. Add(' public');
  13002. Add(' class var vI: longint;');
  13003. Add(' class var Sub: TObject;');
  13004. Add(' constructor Create;');
  13005. Add(' function GetMore(Par: longint): longint;');
  13006. Add(' class function GetIt(Par: longint): tobject;');
  13007. Add(' end;');
  13008. Add('constructor tobject.create;');
  13009. Add('begin');
  13010. Add(' sub:=getit(3);');
  13011. Add(' vi:=getmore(4);');
  13012. Add(' sub:=Self.getit(5);');
  13013. Add(' vi:=Self.getmore(6);');
  13014. Add('end;');
  13015. Add('function tobject.getmore(par: longint): longint;');
  13016. Add('begin');
  13017. Add(' sub:=getit(11);');
  13018. Add(' vi:=getmore(12);');
  13019. Add(' sub:=self.getit(13);');
  13020. Add(' vi:=self.getmore(14);');
  13021. Add('end;');
  13022. Add('class function tobject.getit(par: longint): tobject;');
  13023. Add('begin');
  13024. Add(' sub:=getit(21);');
  13025. Add(' vi:=sub.getmore(22);');
  13026. Add(' sub:=self.getit(23);');
  13027. Add(' vi:=self.sub.getmore(24);');
  13028. Add('end;');
  13029. Add('var Obj: tobject;');
  13030. Add('begin');
  13031. Add(' obj:=tobject.create;');
  13032. Add(' tobject.getit(5);');
  13033. Add(' obj.getit(6);');
  13034. Add(' obj.sub.getit(7);');
  13035. Add(' obj.sub.getit(8).SUB:=nil;');
  13036. Add(' obj.sub.getit(9).GETIT(10);');
  13037. Add(' obj.sub.getit(11).SuB.getit(12);');
  13038. ConvertProgram;
  13039. CheckSource('TestClass_CallClassMethod',
  13040. LinesToStr([ // statements
  13041. 'rtl.createClass(this,"TObject",null,function(){',
  13042. ' this.vI = 0;',
  13043. ' this.Sub = null;',
  13044. ' this.$init = function () {',
  13045. ' };',
  13046. ' this.$final = function () {',
  13047. ' };',
  13048. ' this.Create = function(){',
  13049. ' $mod.TObject.Sub = this.$class.GetIt(3);',
  13050. ' $mod.TObject.vI = this.GetMore(4);',
  13051. ' $mod.TObject.Sub = this.$class.GetIt(5);',
  13052. ' $mod.TObject.vI = this.GetMore(6);',
  13053. ' return this;',
  13054. ' };',
  13055. ' this.GetMore = function(Par){',
  13056. ' var Result = 0;',
  13057. ' $mod.TObject.Sub = this.$class.GetIt(11);',
  13058. ' $mod.TObject.vI = this.GetMore(12);',
  13059. ' $mod.TObject.Sub = this.$class.GetIt(13);',
  13060. ' $mod.TObject.vI = this.GetMore(14);',
  13061. ' return Result;',
  13062. ' };',
  13063. ' this.GetIt = function(Par){',
  13064. ' var Result = null;',
  13065. ' $mod.TObject.Sub = this.GetIt(21);',
  13066. ' $mod.TObject.vI = this.Sub.GetMore(22);',
  13067. ' $mod.TObject.Sub = this.GetIt(23);',
  13068. ' $mod.TObject.vI = this.Sub.GetMore(24);',
  13069. ' return Result;',
  13070. ' };',
  13071. '});',
  13072. 'this.Obj = null;'
  13073. ]),
  13074. LinesToStr([ // $mod.$main
  13075. '$mod.Obj = $mod.TObject.$create("Create");',
  13076. '$mod.TObject.GetIt(5);',
  13077. '$mod.Obj.$class.GetIt(6);',
  13078. '$mod.Obj.Sub.$class.GetIt(7);',
  13079. '$mod.TObject.Sub=null;',
  13080. '$mod.Obj.Sub.$class.GetIt(9).$class.GetIt(10);',
  13081. '$mod.Obj.Sub.$class.GetIt(11).Sub.$class.GetIt(12);',
  13082. '']));
  13083. end;
  13084. procedure TTestModule.TestClass_CallClassMethodStatic;
  13085. begin
  13086. StartProgram(false);
  13087. Add([
  13088. 'type',
  13089. ' TObject = class',
  13090. ' public',
  13091. ' class function Fly: tobject; static;',
  13092. ' end;',
  13093. 'class function tobject.Fly: tobject;',
  13094. 'begin',
  13095. ' Result.Fly;',
  13096. ' Result.Fly();',
  13097. ' Fly;',
  13098. ' Fly();',
  13099. ' Fly.Fly;',
  13100. ' Fly.Fly();',
  13101. 'end;',
  13102. 'var Obj: tobject;',
  13103. 'begin',
  13104. ' obj.Fly;',
  13105. ' obj.Fly();',
  13106. ' with obj do begin',
  13107. ' Fly;',
  13108. ' Fly();',
  13109. ' end;',
  13110. '']);
  13111. ConvertProgram;
  13112. CheckSource('TestClass_CallClassMethodStatic',
  13113. LinesToStr([ // statements
  13114. 'rtl.createClass(this, "TObject", null, function () {',
  13115. ' this.$init = function () {',
  13116. ' };',
  13117. ' this.$final = function () {',
  13118. ' };',
  13119. ' this.Fly = function () {',
  13120. ' var Result = null;',
  13121. ' $mod.TObject.Fly();',
  13122. ' $mod.TObject.Fly();',
  13123. ' $mod.TObject.Fly();',
  13124. ' $mod.TObject.Fly();',
  13125. ' $mod.TObject.Fly();',
  13126. ' $mod.TObject.Fly();',
  13127. ' return Result;',
  13128. ' };',
  13129. '});',
  13130. 'this.Obj = null;'
  13131. ]),
  13132. LinesToStr([ // $mod.$main
  13133. '$mod.TObject.Fly();',
  13134. '$mod.TObject.Fly();',
  13135. 'var $with = $mod.Obj;',
  13136. '$with.Fly();',
  13137. '$with.Fly();',
  13138. '']));
  13139. end;
  13140. procedure TTestModule.TestClass_Property;
  13141. begin
  13142. StartProgram(false);
  13143. Add('type');
  13144. Add(' TObject = class');
  13145. Add(' Fx: longint;');
  13146. Add(' Fy: longint;');
  13147. Add(' function GetInt: longint;');
  13148. Add(' procedure SetInt(Value: longint);');
  13149. Add(' procedure DoIt;');
  13150. Add(' property IntA: longint read Fx write Fy;');
  13151. Add(' property IntB: longint read GetInt write SetInt;');
  13152. Add(' end;');
  13153. Add('function tobject.getint: longint;');
  13154. Add('begin');
  13155. Add(' result:=fx;');
  13156. Add('end;');
  13157. Add('procedure tobject.setint(value: longint);');
  13158. Add('begin');
  13159. Add(' if value=fy then exit;');
  13160. Add(' fy:=value;');
  13161. Add('end;');
  13162. Add('procedure tobject.doit;');
  13163. Add('begin');
  13164. Add(' IntA:=IntA+1;');
  13165. Add(' Self.IntA:=Self.IntA+1;');
  13166. Add(' IntB:=IntB+1;');
  13167. Add(' Self.IntB:=Self.IntB+1;');
  13168. Add('end;');
  13169. Add('var Obj: tobject;');
  13170. Add('begin');
  13171. Add(' obj.inta:=obj.inta+1;');
  13172. Add(' if obj.intb=2 then;');
  13173. Add(' obj.intb:=obj.intb+2;');
  13174. Add(' obj.setint(obj.inta);');
  13175. ConvertProgram;
  13176. CheckSource('TestClass_Property',
  13177. LinesToStr([ // statements
  13178. 'rtl.createClass(this, "TObject", null, function () {',
  13179. ' this.$init = function () {',
  13180. ' this.Fx = 0;',
  13181. ' this.Fy = 0;',
  13182. ' };',
  13183. ' this.$final = function () {',
  13184. ' };',
  13185. ' this.GetInt = function () {',
  13186. ' var Result = 0;',
  13187. ' Result = this.Fx;',
  13188. ' return Result;',
  13189. ' };',
  13190. ' this.SetInt = function (Value) {',
  13191. ' if (Value === this.Fy) return;',
  13192. ' this.Fy = Value;',
  13193. ' };',
  13194. ' this.DoIt = function () {',
  13195. ' this.Fy = this.Fx + 1;',
  13196. ' this.Fy = this.Fx + 1;',
  13197. ' this.SetInt(this.GetInt() + 1);',
  13198. ' this.SetInt(this.GetInt() + 1);',
  13199. ' };',
  13200. '});',
  13201. 'this.Obj = null;'
  13202. ]),
  13203. LinesToStr([ // $mod.$main
  13204. '$mod.Obj.Fy = $mod.Obj.Fx + 1;',
  13205. 'if ($mod.Obj.GetInt() === 2);',
  13206. '$mod.Obj.SetInt($mod.Obj.GetInt() + 2);',
  13207. '$mod.Obj.SetInt($mod.Obj.Fx);'
  13208. ]));
  13209. end;
  13210. procedure TTestModule.TestClass_Property_ClassMethod;
  13211. begin
  13212. StartProgram(false);
  13213. Add([
  13214. 'type',
  13215. ' TObject = class',
  13216. ' class var Fx: longint;',
  13217. ' class var Fy: longint;',
  13218. ' class function GetInt: longint;',
  13219. ' class procedure SetInt(Value: longint);',
  13220. ' end;',
  13221. ' TBird = class',
  13222. ' class procedure DoIt;',
  13223. ' class property IntA: longint read Fx write Fy;',
  13224. ' class property IntB: longint read GetInt write SetInt;',
  13225. ' end;',
  13226. 'class function tobject.getint: longint;',
  13227. 'begin',
  13228. ' result:=fx;',
  13229. 'end;',
  13230. 'class procedure tobject.setint(value: longint);',
  13231. 'begin',
  13232. 'end;',
  13233. 'class procedure tbird.doit;',
  13234. 'begin',
  13235. ' FX:=3;',
  13236. ' IntA:=IntA+1;',
  13237. ' Self.IntA:=Self.IntA+1;',
  13238. ' IntB:=IntB+1;',
  13239. ' Self.IntB:=Self.IntB+1;',
  13240. ' with Self do begin',
  13241. ' FX:=11;',
  13242. ' IntA:=IntA+12;',
  13243. ' IntB:=IntB+13;',
  13244. ' end;',
  13245. 'end;',
  13246. 'var Obj: tbird;',
  13247. 'begin',
  13248. ' tbird.fx:=tbird.fx+1;',
  13249. ' tbird.inta:=tbird.inta+1;',
  13250. ' if tbird.intb=2 then;',
  13251. ' tbird.intb:=tbird.intb+2;',
  13252. ' tbird.setint(tbird.inta);',
  13253. ' obj.inta:=obj.inta+1;',
  13254. ' if obj.intb=2 then;',
  13255. ' obj.intb:=obj.intb+2;',
  13256. ' obj.setint(obj.inta);',
  13257. ' with Tbird do begin',
  13258. ' FX:=FY+1;',
  13259. ' inta:=inta+2;',
  13260. ' intb:=intb+3;',
  13261. ' end;',
  13262. ' with Obj do begin',
  13263. ' FX:=FY+1;',
  13264. ' inta:=inta+2;',
  13265. ' intb:=intb+3;',
  13266. ' end;',
  13267. '']);
  13268. ConvertProgram;
  13269. CheckSource('TestClass_Property_ClassMethod',
  13270. LinesToStr([ // statements
  13271. 'rtl.createClass(this, "TObject", null, function () {',
  13272. ' this.Fx = 0;',
  13273. ' this.Fy = 0;',
  13274. ' this.$init = function () {',
  13275. ' };',
  13276. ' this.$final = function () {',
  13277. ' };',
  13278. ' this.GetInt = function () {',
  13279. ' var Result = 0;',
  13280. ' Result = this.Fx;',
  13281. ' return Result;',
  13282. ' };',
  13283. ' this.SetInt = function (Value) {',
  13284. ' };',
  13285. '});',
  13286. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  13287. ' this.DoIt = function () {',
  13288. ' $mod.TObject.Fx = 3;',
  13289. ' $mod.TObject.Fy = this.Fx + 1;',
  13290. ' $mod.TObject.Fy = this.Fx + 1;',
  13291. ' this.SetInt(this.GetInt() + 1);',
  13292. ' this.SetInt(this.GetInt() + 1);',
  13293. ' $mod.TObject.Fx = 11;',
  13294. ' $mod.TObject.Fy = this.Fx + 12;',
  13295. ' this.SetInt(this.GetInt() + 13);',
  13296. ' };',
  13297. '});',
  13298. 'this.Obj = null;'
  13299. ]),
  13300. LinesToStr([ // $mod.$main
  13301. '$mod.TObject.Fx = $mod.TBird.Fx + 1;',
  13302. '$mod.TObject.Fy = $mod.TBird.Fx + 1;',
  13303. 'if ($mod.TBird.GetInt() === 2);',
  13304. '$mod.TBird.SetInt($mod.TBird.GetInt() + 2);',
  13305. '$mod.TBird.SetInt($mod.TBird.Fx);',
  13306. '$mod.TObject.Fy = $mod.Obj.Fx + 1;',
  13307. 'if ($mod.Obj.$class.GetInt() === 2);',
  13308. '$mod.Obj.$class.SetInt($mod.Obj.$class.GetInt() + 2);',
  13309. '$mod.Obj.$class.SetInt($mod.Obj.Fx);',
  13310. 'var $with = $mod.TBird;',
  13311. '$mod.TObject.Fx = $with.Fy + 1;',
  13312. '$mod.TObject.Fy = $with.Fx + 2;',
  13313. '$with.SetInt($with.GetInt() + 3);',
  13314. 'var $with1 = $mod.Obj;',
  13315. '$mod.TObject.Fx = $with1.Fy + 1;',
  13316. '$mod.TObject.Fy = $with1.Fx + 2;',
  13317. '$with1.$class.SetInt($with1.$class.GetInt() + 3);',
  13318. '']));
  13319. end;
  13320. procedure TTestModule.TestClass_Property_Indexed;
  13321. begin
  13322. StartProgram(false);
  13323. Add([
  13324. 'type',
  13325. ' TObject = class',
  13326. ' FItems: array of longint;',
  13327. ' function GetItems(Index: longint): longint;',
  13328. ' procedure SetItems(Index: longint; Value: longint);',
  13329. ' procedure DoIt;',
  13330. ' property Items[Index: longint]: longint read getitems write setitems;',
  13331. ' end;',
  13332. 'function tobject.getitems(index: longint): longint;',
  13333. 'begin',
  13334. ' Result:=fitems[index];',
  13335. 'end;',
  13336. 'procedure tobject.setitems(index: longint; value: longint);',
  13337. 'begin',
  13338. ' fitems[index]:=value;',
  13339. 'end;',
  13340. 'procedure tobject.doit;',
  13341. 'begin',
  13342. ' items[1]:=2;',
  13343. ' items[3]:=items[4];',
  13344. ' self.items[5]:=self.items[6];',
  13345. ' items[items[7]]:=items[items[8]];',
  13346. 'end;',
  13347. 'var Obj: tobject;',
  13348. 'begin',
  13349. ' obj.Items[11]:=obj.Items[12];',
  13350. '']);
  13351. ConvertProgram;
  13352. CheckSource('TestClass_Property_Indexed',
  13353. LinesToStr([ // statements
  13354. 'rtl.createClass(this, "TObject", null, function () {',
  13355. ' this.$init = function () {',
  13356. ' this.FItems = [];',
  13357. ' };',
  13358. ' this.$final = function () {',
  13359. ' this.FItems = undefined;',
  13360. ' };',
  13361. ' this.GetItems = function (Index) {',
  13362. ' var Result = 0;',
  13363. ' Result = this.FItems[Index];',
  13364. ' return Result;',
  13365. ' };',
  13366. ' this.SetItems = function (Index, Value) {',
  13367. ' this.FItems[Index] = Value;',
  13368. ' };',
  13369. ' this.DoIt = function () {',
  13370. ' this.SetItems(1, 2);',
  13371. ' this.SetItems(3,this.GetItems(4));',
  13372. ' this.SetItems(5,this.GetItems(6));',
  13373. ' this.SetItems(this.GetItems(7), this.GetItems(this.GetItems(8)));',
  13374. ' };',
  13375. '});',
  13376. 'this.Obj = null;'
  13377. ]),
  13378. LinesToStr([ // $mod.$main
  13379. '$mod.Obj.SetItems(11,$mod.Obj.GetItems(12));'
  13380. ]));
  13381. end;
  13382. procedure TTestModule.TestClass_Property_IndexSpec;
  13383. begin
  13384. StartProgram(false);
  13385. Add([
  13386. 'type',
  13387. ' TEnum = (red, blue);',
  13388. ' TObject = class',
  13389. ' function GetIntBool(Index: longint): boolean; virtual; abstract;',
  13390. ' procedure SetIntBool(Index: longint; b: boolean); virtual; abstract;',
  13391. ' function GetEnumBool(Index: TEnum): boolean; virtual; abstract;',
  13392. ' procedure SetEnumBool(Index: TEnum; b: boolean); virtual; abstract;',
  13393. ' function GetStrIntBool(A: String; I: longint): boolean; virtual; abstract;',
  13394. ' procedure SetStrIntBool(A: String; I: longint; b: boolean); virtual; abstract;',
  13395. ' property B1: boolean index 1 read GetIntBool write SetIntBool;',
  13396. ' property B2: boolean index TEnum.blue read GetEnumBool write SetEnumBool;',
  13397. ' property B3: boolean index ord(red) read GetIntBool write SetIntBool;',
  13398. ' property I1[A: String]: boolean index ord(blue) read GetStrIntBool write SetStrIntBool;',
  13399. ' end;',
  13400. 'procedure DoIt(b: boolean); begin end;',
  13401. 'var',
  13402. ' o: TObject;',
  13403. 'begin',
  13404. ' o.B1:=o.B1;',
  13405. ' o.B2:=o.B2;',
  13406. ' o.B3:=o.B3;',
  13407. ' o.I1[''a'']:=o.I1[''b''];',
  13408. ' doit(o.b1);',
  13409. ' doit(o.b2);',
  13410. ' doit(o.i1[''c'']);',
  13411. '']);
  13412. ConvertProgram;
  13413. CheckSource('TestClass_Property_IndexSpec',
  13414. LinesToStr([ // statements
  13415. 'this.TEnum = {',
  13416. ' "0": "red",',
  13417. ' red: 0,',
  13418. ' "1": "blue",',
  13419. ' blue: 1',
  13420. '};',
  13421. 'rtl.createClass(this, "TObject", null, function () {',
  13422. ' this.$init = function () {',
  13423. ' };',
  13424. ' this.$final = function () {',
  13425. ' };',
  13426. '});',
  13427. 'this.DoIt = function (b) {',
  13428. '};',
  13429. 'this.o = null;',
  13430. '']),
  13431. LinesToStr([ // $mod.$main
  13432. '$mod.o.SetIntBool(1, $mod.o.GetIntBool(1));',
  13433. '$mod.o.SetEnumBool($mod.TEnum.blue, $mod.o.GetEnumBool($mod.TEnum.blue));',
  13434. '$mod.o.SetIntBool(0, $mod.o.GetIntBool(0));',
  13435. '$mod.o.SetStrIntBool("a", 1, $mod.o.GetStrIntBool("b", 1));',
  13436. '$mod.DoIt($mod.o.GetIntBool(1));',
  13437. '$mod.DoIt($mod.o.GetEnumBool($mod.TEnum.blue));',
  13438. '$mod.DoIt($mod.o.GetStrIntBool("c", 1));',
  13439. '']));
  13440. end;
  13441. procedure TTestModule.TestClass_PropertyOfTypeArray;
  13442. begin
  13443. StartProgram(false);
  13444. Add('type');
  13445. Add(' TArray = array of longint;');
  13446. Add(' TObject = class');
  13447. Add(' FItems: TArray;');
  13448. Add(' function GetItems: tarray;');
  13449. Add(' procedure SetItems(Value: tarray);');
  13450. Add(' property Items: tarray read getitems write setitems;');
  13451. Add(' procedure SetNumbers(const Value: tarray);');
  13452. Add(' property Numbers: tarray write setnumbers;');
  13453. Add(' end;');
  13454. Add('function tobject.getitems: tarray;');
  13455. Add('begin');
  13456. Add(' Result:=fitems;');
  13457. Add('end;');
  13458. Add('procedure tobject.setitems(value: tarray);');
  13459. Add('begin');
  13460. Add(' fitems:=value;');
  13461. Add(' fitems:=nil;');
  13462. Add(' Items:=nil;');
  13463. Add(' Items:=Items;');
  13464. Add(' Items[1]:=2;');
  13465. Add(' fitems[3]:=Items[4];');
  13466. Add(' Items[5]:=Items[6];');
  13467. Add(' Self.Items[7]:=8;');
  13468. Add(' Self.Items[9]:=Self.Items[10];');
  13469. Add(' Items[Items[11]]:=Items[Items[12]];');
  13470. Add('end;');
  13471. Add('procedure tobject.SetNumbers(const Value: tarray);');
  13472. Add('begin;');
  13473. Add(' Numbers:=nil;');
  13474. Add(' Numbers:=Value;');
  13475. Add(' Self.Numbers:=Value;');
  13476. Add('end;');
  13477. Add('var Obj: tobject;');
  13478. Add('begin');
  13479. Add(' obj.items:=nil;');
  13480. Add(' obj.items:=obj.items;');
  13481. Add(' obj.items[11]:=obj.items[12];');
  13482. ConvertProgram;
  13483. CheckSource('TestClass_PropertyOfTypeArray',
  13484. LinesToStr([ // statements
  13485. 'rtl.createClass(this, "TObject", null, function () {',
  13486. ' this.$init = function () {',
  13487. ' this.FItems = [];',
  13488. ' };',
  13489. ' this.$final = function () {',
  13490. ' this.FItems = undefined;',
  13491. ' };',
  13492. ' this.GetItems = function () {',
  13493. ' var Result = [];',
  13494. ' Result = rtl.arrayRef(this.FItems);',
  13495. ' return Result;',
  13496. ' };',
  13497. ' this.SetItems = function (Value) {',
  13498. ' this.FItems = rtl.arrayRef(Value);',
  13499. ' this.FItems = [];',
  13500. ' this.SetItems([]);',
  13501. ' this.SetItems(rtl.arrayRef(this.GetItems()));',
  13502. ' this.GetItems()[1] = 2;',
  13503. ' this.FItems[3] = this.GetItems()[4];',
  13504. ' this.GetItems()[5] = this.GetItems()[6];',
  13505. ' this.GetItems()[7] = 8;',
  13506. ' this.GetItems()[9] = this.GetItems()[10];',
  13507. ' this.GetItems()[this.GetItems()[11]] = this.GetItems()[this.GetItems()[12]];',
  13508. ' };',
  13509. ' this.SetNumbers = function (Value) {',
  13510. ' this.SetNumbers([]);',
  13511. ' this.SetNumbers(Value);',
  13512. ' this.SetNumbers(Value);',
  13513. ' };',
  13514. '});',
  13515. 'this.Obj = null;'
  13516. ]),
  13517. LinesToStr([ // $mod.$main
  13518. '$mod.Obj.SetItems([]);',
  13519. '$mod.Obj.SetItems($mod.Obj.GetItems());',
  13520. '$mod.Obj.GetItems()[11] = $mod.Obj.GetItems()[12];'
  13521. ]));
  13522. end;
  13523. procedure TTestModule.TestClass_PropertyDefault;
  13524. begin
  13525. StartProgram(false);
  13526. Add([
  13527. 'type',
  13528. ' TArray = array of longint;',
  13529. ' TObject = class',
  13530. ' end;',
  13531. ' TBird = class',
  13532. ' FItems: TArray;',
  13533. ' function GetItems(Index: longint): longint;',
  13534. ' procedure SetItems(Index, Value: longint);',
  13535. ' property Items[Index: longint]: longint read getitems write setitems; default;',
  13536. ' end;',
  13537. 'function TBird.getitems(index: longint): longint;',
  13538. 'begin',
  13539. 'end;',
  13540. 'procedure TBird.setitems(index, value: longint);',
  13541. 'begin',
  13542. ' Self[1]:=2;',
  13543. ' Self[3]:=Self[index];',
  13544. ' Self[index]:=Self[Self[value]];',
  13545. ' Self[Self[4]]:=value;',
  13546. 'end;',
  13547. 'var',
  13548. ' Bird: TBird;',
  13549. ' Obj: TObject;',
  13550. 'begin',
  13551. ' bird[11]:=12;',
  13552. ' bird[13]:=bird[14];',
  13553. ' bird[Bird[15]]:=bird[Bird[15]];',
  13554. ' TBird(obj)[16]:=TBird(obj)[17];',
  13555. ' (obj as tbird)[18]:=19;',
  13556. '']);
  13557. ConvertProgram;
  13558. CheckSource('TestClass_PropertyDefault',
  13559. LinesToStr([ // statements
  13560. 'rtl.createClass(this, "TObject", null, function () {',
  13561. ' this.$init = function () {',
  13562. ' };',
  13563. ' this.$final = function () {',
  13564. ' };',
  13565. '});',
  13566. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  13567. ' this.$init = function () {',
  13568. ' $mod.TObject.$init.call(this);',
  13569. ' this.FItems = [];',
  13570. ' };',
  13571. ' this.$final = function () {',
  13572. ' this.FItems = undefined;',
  13573. ' $mod.TObject.$final.call(this);',
  13574. ' };',
  13575. ' this.GetItems = function (Index) {',
  13576. ' var Result = 0;',
  13577. ' return Result;',
  13578. ' };',
  13579. ' this.SetItems = function (Index, Value) {',
  13580. ' this.SetItems(1, 2);',
  13581. ' this.SetItems(3, this.GetItems(Index));',
  13582. ' this.SetItems(Index, this.GetItems(this.GetItems(Value)));',
  13583. ' this.SetItems(this.GetItems(4), Value);',
  13584. ' };',
  13585. '});',
  13586. 'this.Bird = null;',
  13587. 'this.Obj = null;',
  13588. '']),
  13589. LinesToStr([ // $mod.$main
  13590. '$mod.Bird.SetItems(11, 12);',
  13591. '$mod.Bird.SetItems(13, $mod.Bird.GetItems(14));',
  13592. '$mod.Bird.SetItems($mod.Bird.GetItems(15), $mod.Bird.GetItems($mod.Bird.GetItems(15)));',
  13593. '$mod.Obj.SetItems(16, $mod.Obj.GetItems(17));',
  13594. 'rtl.as($mod.Obj, $mod.TBird).SetItems(18, 19);',
  13595. '']));
  13596. end;
  13597. procedure TTestModule.TestClass_PropertyDefault_TypecastToOtherDefault;
  13598. begin
  13599. StartProgram(false);
  13600. Add([
  13601. 'type',
  13602. ' TObject = class end;',
  13603. ' TAlphaList = class',
  13604. ' function GetAlphas(Index: boolean): Pointer; virtual; abstract;',
  13605. ' procedure SetAlphas(Index: boolean; Value: Pointer); virtual; abstract;',
  13606. ' property Alphas[Index: boolean]: Pointer read getAlphas write setAlphas; default;',
  13607. ' end;',
  13608. ' TBetaList = class',
  13609. ' function GetBetas(Index: longint): Pointer; virtual; abstract;',
  13610. ' procedure SetBetas(Index: longint; Value: Pointer); virtual; abstract;',
  13611. ' property Betas[Index: longint]: Pointer read getBetas write setBetas; default;',
  13612. ' end;',
  13613. ' TBird = class',
  13614. ' procedure DoIt;',
  13615. ' end;',
  13616. 'procedure TBird.DoIt;',
  13617. 'var',
  13618. ' List: TAlphaList;',
  13619. 'begin',
  13620. ' if TBetaList(List[true])[3]=nil then ;',
  13621. ' TBetaList(List[false])[5]:=nil;',
  13622. 'end;',
  13623. 'var',
  13624. ' List: TAlphaList;',
  13625. 'begin',
  13626. ' if TBetaList(List[true])[3]=nil then ;',
  13627. ' TBetaList(List[false])[5]:=nil;',
  13628. '']);
  13629. ConvertProgram;
  13630. CheckSource('TestClass_PropertyDefault_TypecastToOtherDefault',
  13631. LinesToStr([ // statements
  13632. 'rtl.createClass(this, "TObject", null, function () {',
  13633. ' this.$init = function () {',
  13634. ' };',
  13635. ' this.$final = function () {',
  13636. ' };',
  13637. '});',
  13638. 'rtl.createClass(this, "TAlphaList", this.TObject, function () {',
  13639. '});',
  13640. 'rtl.createClass(this, "TBetaList", this.TObject, function () {',
  13641. '});',
  13642. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  13643. ' this.DoIt = function () {',
  13644. ' var List = null;',
  13645. ' if (List.GetAlphas(true).GetBetas(3) === null) ;',
  13646. ' List.GetAlphas(false).SetBetas(5, null);',
  13647. ' };',
  13648. '});',
  13649. 'this.List = null;',
  13650. '']),
  13651. LinesToStr([ // $mod.$main
  13652. 'if ($mod.List.GetAlphas(true).GetBetas(3) === null) ;',
  13653. '$mod.List.GetAlphas(false).SetBetas(5, null);',
  13654. '']));
  13655. end;
  13656. procedure TTestModule.TestClass_PropertyOverride;
  13657. begin
  13658. StartProgram(false);
  13659. Add('type');
  13660. Add(' integer = longint;');
  13661. Add(' TObject = class');
  13662. Add(' FItem: integer;');
  13663. Add(' function GetItem: integer; external name ''GetItem'';');
  13664. Add(' procedure SetItem(Value: integer); external name ''SetItem'';');
  13665. Add(' property Item: integer read getitem write setitem;');
  13666. Add(' end;');
  13667. Add(' TCar = class');
  13668. Add(' FBag: integer;');
  13669. Add(' function GetBag: integer; external name ''GetBag'';');
  13670. Add(' property Item read getbag;');
  13671. Add(' end;');
  13672. Add('var');
  13673. Add(' Obj: tobject;');
  13674. Add(' Car: tcar;');
  13675. Add('begin');
  13676. Add(' Obj.Item:=Obj.Item;');
  13677. Add(' Car.Item:=Car.Item;');
  13678. ConvertProgram;
  13679. CheckSource('TestClass_PropertyOverride',
  13680. LinesToStr([ // statements
  13681. 'rtl.createClass(this, "TObject", null, function () {',
  13682. ' this.$init = function () {',
  13683. ' this.FItem = 0;',
  13684. ' };',
  13685. ' this.$final = function () {',
  13686. ' };',
  13687. '});',
  13688. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  13689. ' this.$init = function () {',
  13690. ' $mod.TObject.$init.call(this);',
  13691. ' this.FBag = 0;',
  13692. ' };',
  13693. '});',
  13694. 'this.Obj = null;',
  13695. 'this.Car = null;',
  13696. '']),
  13697. LinesToStr([ // $mod.$main
  13698. '$mod.Obj.SetItem($mod.Obj.GetItem());',
  13699. '$mod.Car.SetItem($mod.Car.GetBag());',
  13700. '']));
  13701. end;
  13702. procedure TTestModule.TestClass_PropertyIncVisibility;
  13703. begin
  13704. AddModuleWithIntfImplSrc('unit1.pp',
  13705. LinesToStr([
  13706. 'type',
  13707. ' TNumber = longint;',
  13708. ' TInteger = longint;',
  13709. ' TObject = class',
  13710. ' private',
  13711. ' function GetItems(Index: TNumber): TInteger; virtual; abstract;',
  13712. ' procedure SetItems(Index: TInteger; Value: TNumber); virtual; abstract;',
  13713. ' protected',
  13714. ' property Items[Index: TNumber]: longint read GetItems write SetItems;',
  13715. ' end;']),
  13716. LinesToStr([
  13717. '']));
  13718. StartProgram(true);
  13719. Add([
  13720. 'uses unit1;',
  13721. 'type',
  13722. ' TBird = class',
  13723. ' public',
  13724. ' property Items;',
  13725. ' end;',
  13726. 'procedure DoIt(i: TInteger);',
  13727. 'begin',
  13728. 'end;',
  13729. 'var b: TBird;',
  13730. 'begin',
  13731. ' b.Items[1]:=2;',
  13732. ' b.Items[3]:=b.Items[4];',
  13733. ' DoIt(b.Items[5]);',
  13734. '']);
  13735. ConvertProgram;
  13736. CheckSource('TestClass_PropertyIncVisibility',
  13737. LinesToStr([ // statements
  13738. 'rtl.createClass(this, "TBird", pas.unit1.TObject, function () {',
  13739. '});',
  13740. 'this.DoIt = function (i) {',
  13741. '};',
  13742. 'this.b = null;'
  13743. ]),
  13744. LinesToStr([ // $mod.$main
  13745. '$mod.b.SetItems(1, 2);',
  13746. '$mod.b.SetItems(3, $mod.b.GetItems(4));',
  13747. '$mod.DoIt($mod.b.GetItems(5));'
  13748. ]));
  13749. end;
  13750. procedure TTestModule.TestClass_Assigned;
  13751. begin
  13752. StartProgram(false);
  13753. Add('type');
  13754. Add(' TObject = class');
  13755. Add(' end;');
  13756. Add('var');
  13757. Add(' Obj: tobject;');
  13758. Add(' b: boolean;');
  13759. Add('begin');
  13760. Add(' if Assigned(obj) then ;');
  13761. Add(' b:=Assigned(obj) or false;');
  13762. ConvertProgram;
  13763. CheckSource('TestClass_Assigned',
  13764. LinesToStr([ // statements
  13765. 'rtl.createClass(this, "TObject", null, function () {',
  13766. ' this.$init = function () {',
  13767. ' };',
  13768. ' this.$final = function () {',
  13769. ' };',
  13770. '});',
  13771. 'this.Obj = null;',
  13772. 'this.b = false;'
  13773. ]),
  13774. LinesToStr([ // $mod.$main
  13775. 'if ($mod.Obj != null);',
  13776. '$mod.b = ($mod.Obj != null) || false;'
  13777. ]));
  13778. end;
  13779. procedure TTestModule.TestClass_WithClassDoCreate;
  13780. begin
  13781. StartProgram(false);
  13782. Add('type');
  13783. Add(' TObject = class');
  13784. Add(' aBool: boolean;');
  13785. Add(' Arr: array of boolean;');
  13786. Add(' constructor Create;');
  13787. Add(' end;');
  13788. Add('constructor TObject.Create; begin end;');
  13789. Add('var');
  13790. Add(' Obj: tobject;');
  13791. Add(' b: boolean;');
  13792. Add('begin');
  13793. Add(' with tobject.create do begin');
  13794. Add(' b:=abool;');
  13795. Add(' abool:=b;');
  13796. Add(' b:=arr[1];');
  13797. Add(' arr[2]:=b;');
  13798. Add(' end;');
  13799. Add(' with tobject do');
  13800. Add(' obj:=create;');
  13801. Add(' with obj do begin');
  13802. Add(' create;');
  13803. Add(' b:=abool;');
  13804. Add(' abool:=b;');
  13805. Add(' b:=arr[3];');
  13806. Add(' arr[4]:=b;');
  13807. Add(' end;');
  13808. ConvertProgram;
  13809. CheckSource('TestClass_WithClassDoCreate',
  13810. LinesToStr([ // statements
  13811. 'rtl.createClass(this, "TObject", null, function () {',
  13812. ' this.$init = function () {',
  13813. ' this.aBool = false;',
  13814. ' this.Arr = [];',
  13815. ' };',
  13816. ' this.$final = function () {',
  13817. ' this.Arr = undefined;',
  13818. ' };',
  13819. ' this.Create = function () {',
  13820. ' return this;',
  13821. ' };',
  13822. '});',
  13823. 'this.Obj = null;',
  13824. 'this.b = false;'
  13825. ]),
  13826. LinesToStr([ // $mod.$main
  13827. 'var $with = $mod.TObject.$create("Create");',
  13828. '$mod.b = $with.aBool;',
  13829. '$with.aBool = $mod.b;',
  13830. '$mod.b = $with.Arr[1];',
  13831. '$with.Arr[2] = $mod.b;',
  13832. 'var $with1 = $mod.TObject;',
  13833. '$mod.Obj = $with1.$create("Create");',
  13834. 'var $with2 = $mod.Obj;',
  13835. '$with2.Create();',
  13836. '$mod.b = $with2.aBool;',
  13837. '$with2.aBool = $mod.b;',
  13838. '$mod.b = $with2.Arr[3];',
  13839. '$with2.Arr[4] = $mod.b;',
  13840. '']));
  13841. end;
  13842. procedure TTestModule.TestClass_WithClassInstDoProperty;
  13843. begin
  13844. StartProgram(false);
  13845. Add('type');
  13846. Add(' TObject = class');
  13847. Add(' FInt: longint;');
  13848. Add(' constructor Create;');
  13849. Add(' function GetSize: longint;');
  13850. Add(' procedure SetSize(Value: longint);');
  13851. Add(' property Int: longint read FInt write FInt;');
  13852. Add(' property Size: longint read GetSize write SetSize;');
  13853. Add(' end;');
  13854. Add('constructor TObject.Create; begin end;');
  13855. Add('function TObject.GetSize: longint; begin; end;');
  13856. Add('procedure TObject.SetSize(Value: longint); begin; end;');
  13857. Add('var');
  13858. Add(' Obj: tobject;');
  13859. Add(' i: longint;');
  13860. Add('begin');
  13861. Add(' with TObject.Create do begin');
  13862. Add(' i:=int;');
  13863. Add(' int:=i;');
  13864. Add(' i:=size;');
  13865. Add(' size:=i;');
  13866. Add(' end;');
  13867. Add(' with obj do begin');
  13868. Add(' i:=int;');
  13869. Add(' int:=i;');
  13870. Add(' i:=size;');
  13871. Add(' size:=i;');
  13872. Add(' end;');
  13873. ConvertProgram;
  13874. CheckSource('TestClass_WithClassInstDoProperty',
  13875. LinesToStr([ // statements
  13876. 'rtl.createClass(this, "TObject", null, function () {',
  13877. ' this.$init = function () {',
  13878. ' this.FInt = 0;',
  13879. ' };',
  13880. ' this.$final = function () {',
  13881. ' };',
  13882. ' this.Create = function () {',
  13883. ' return this;',
  13884. ' };',
  13885. ' this.GetSize = function () {',
  13886. ' var Result = 0;',
  13887. ' return Result;',
  13888. ' };',
  13889. ' this.SetSize = function (Value) {',
  13890. ' };',
  13891. '});',
  13892. 'this.Obj = null;',
  13893. 'this.i = 0;'
  13894. ]),
  13895. LinesToStr([ // $mod.$main
  13896. 'var $with = $mod.TObject.$create("Create");',
  13897. '$mod.i = $with.FInt;',
  13898. '$with.FInt = $mod.i;',
  13899. '$mod.i = $with.GetSize();',
  13900. '$with.SetSize($mod.i);',
  13901. 'var $with1 = $mod.Obj;',
  13902. '$mod.i = $with1.FInt;',
  13903. '$with1.FInt = $mod.i;',
  13904. '$mod.i = $with1.GetSize();',
  13905. '$with1.SetSize($mod.i);',
  13906. '']));
  13907. end;
  13908. procedure TTestModule.TestClass_WithClassInstDoPropertyWithParams;
  13909. begin
  13910. StartProgram(false);
  13911. Add('type');
  13912. Add(' TObject = class');
  13913. Add(' constructor Create;');
  13914. Add(' function GetItems(Index: longint): longint;');
  13915. Add(' procedure SetItems(Index, Value: longint);');
  13916. Add(' property Items[Index: longint]: longint read GetItems write SetItems;');
  13917. Add(' end;');
  13918. Add('constructor TObject.Create; begin end;');
  13919. Add('function tobject.getitems(index: longint): longint; begin; end;');
  13920. Add('procedure tobject.setitems(index, value: longint); begin; end;');
  13921. Add('var');
  13922. Add(' Obj: tobject;');
  13923. Add(' i: longint;');
  13924. Add('begin');
  13925. Add(' with TObject.Create do begin');
  13926. Add(' i:=Items[1];');
  13927. Add(' Items[2]:=i;');
  13928. Add(' end;');
  13929. Add(' with obj do begin');
  13930. Add(' i:=Items[3];');
  13931. Add(' Items[4]:=i;');
  13932. Add(' end;');
  13933. ConvertProgram;
  13934. CheckSource('TestClass_WithClassInstDoPropertyWithParams',
  13935. LinesToStr([ // statements
  13936. 'rtl.createClass(this, "TObject", null, function () {',
  13937. ' this.$init = function () {',
  13938. ' };',
  13939. ' this.$final = function () {',
  13940. ' };',
  13941. ' this.Create = function () {',
  13942. ' return this;',
  13943. ' };',
  13944. ' this.GetItems = function (Index) {',
  13945. ' var Result = 0;',
  13946. ' return Result;',
  13947. ' };',
  13948. ' this.SetItems = function (Index, Value) {',
  13949. ' };',
  13950. '});',
  13951. 'this.Obj = null;',
  13952. 'this.i = 0;'
  13953. ]),
  13954. LinesToStr([ // $mod.$main
  13955. 'var $with = $mod.TObject.$create("Create");',
  13956. '$mod.i = $with.GetItems(1);',
  13957. '$with.SetItems(2, $mod.i);',
  13958. 'var $with1 = $mod.Obj;',
  13959. '$mod.i = $with1.GetItems(3);',
  13960. '$with1.SetItems(4, $mod.i);',
  13961. '']));
  13962. end;
  13963. procedure TTestModule.TestClass_WithClassInstDoFunc;
  13964. begin
  13965. StartProgram(false);
  13966. Add('type');
  13967. Add(' TObject = class');
  13968. Add(' constructor Create;');
  13969. Add(' function GetSize: longint;');
  13970. Add(' procedure SetSize(Value: longint);');
  13971. Add(' end;');
  13972. Add('constructor TObject.Create; begin end;');
  13973. Add('function TObject.GetSize: longint; begin; end;');
  13974. Add('procedure TObject.SetSize(Value: longint); begin; end;');
  13975. Add('var');
  13976. Add(' Obj: tobject;');
  13977. Add(' i: longint;');
  13978. Add('begin');
  13979. Add(' with TObject.Create do begin');
  13980. Add(' i:=GetSize;');
  13981. Add(' i:=GetSize();');
  13982. Add(' SetSize(i);');
  13983. Add(' end;');
  13984. Add(' with obj do begin');
  13985. Add(' i:=GetSize;');
  13986. Add(' i:=GetSize();');
  13987. Add(' SetSize(i);');
  13988. Add(' end;');
  13989. ConvertProgram;
  13990. CheckSource('TestClass_WithClassInstDoFunc',
  13991. LinesToStr([ // statements
  13992. 'rtl.createClass(this, "TObject", null, function () {',
  13993. ' this.$init = function () {',
  13994. ' };',
  13995. ' this.$final = function () {',
  13996. ' };',
  13997. ' this.Create = function () {',
  13998. ' return this;',
  13999. ' };',
  14000. ' this.GetSize = function () {',
  14001. ' var Result = 0;',
  14002. ' return Result;',
  14003. ' };',
  14004. ' this.SetSize = function (Value) {',
  14005. ' };',
  14006. '});',
  14007. 'this.Obj = null;',
  14008. 'this.i = 0;'
  14009. ]),
  14010. LinesToStr([ // $mod.$main
  14011. 'var $with = $mod.TObject.$create("Create");',
  14012. '$mod.i = $with.GetSize();',
  14013. '$mod.i = $with.GetSize();',
  14014. '$with.SetSize($mod.i);',
  14015. 'var $with1 = $mod.Obj;',
  14016. '$mod.i = $with1.GetSize();',
  14017. '$mod.i = $with1.GetSize();',
  14018. '$with1.SetSize($mod.i);',
  14019. '']));
  14020. end;
  14021. procedure TTestModule.TestClass_TypeCast;
  14022. begin
  14023. StartProgram(false);
  14024. Add('type');
  14025. Add(' TObject = class');
  14026. Add(' Next: TObject;');
  14027. Add(' constructor Create;');
  14028. Add(' end;');
  14029. Add(' TControl = class(TObject)');
  14030. Add(' Arr: array of TObject;');
  14031. Add(' function GetIt(vI: longint = 0): TObject;');
  14032. Add(' end;');
  14033. Add('constructor tobject.create; begin end;');
  14034. Add('function tcontrol.getit(vi: longint = 0): tobject; begin end;');
  14035. Add('var');
  14036. Add(' Obj: tobject;');
  14037. Add('begin');
  14038. Add(' obj:=tcontrol(obj).next;');
  14039. Add(' tcontrol(obj):=nil;');
  14040. Add(' obj:=tcontrol(obj);');
  14041. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit);');
  14042. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit());');
  14043. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit(1));');
  14044. Add(' tcontrol(obj):=tcontrol(tcontrol(tcontrol(obj).getit).arr[2]);');
  14045. Add(' obj:=tcontrol(nil);');
  14046. ConvertProgram;
  14047. CheckSource('TestClass_TypeCast',
  14048. LinesToStr([ // statements
  14049. 'rtl.createClass(this, "TObject", null, function () {',
  14050. ' this.$init = function () {',
  14051. ' this.Next = null;',
  14052. ' };',
  14053. ' this.$final = function () {',
  14054. ' this.Next = undefined;',
  14055. ' };',
  14056. ' this.Create = function () {',
  14057. ' return this;',
  14058. ' };',
  14059. '});',
  14060. 'rtl.createClass(this, "TControl", this.TObject, function () {',
  14061. ' this.$init = function () {',
  14062. ' $mod.TObject.$init.call(this);',
  14063. ' this.Arr = [];',
  14064. ' };',
  14065. ' this.$final = function () {',
  14066. ' this.Arr = undefined;',
  14067. ' $mod.TObject.$final.call(this);',
  14068. ' };',
  14069. ' this.GetIt = function (vI) {',
  14070. ' var Result = null;',
  14071. ' return Result;',
  14072. ' };',
  14073. '});',
  14074. 'this.Obj = null;'
  14075. ]),
  14076. LinesToStr([ // $mod.$main
  14077. '$mod.Obj = $mod.Obj.Next;',
  14078. '$mod.Obj = null;',
  14079. '$mod.Obj = $mod.Obj;',
  14080. '$mod.Obj = $mod.Obj.GetIt(0);',
  14081. '$mod.Obj = $mod.Obj.GetIt(0);',
  14082. '$mod.Obj = $mod.Obj.GetIt(1);',
  14083. '$mod.Obj = $mod.Obj.GetIt(0).Arr[2];',
  14084. '$mod.Obj = null;',
  14085. '']));
  14086. end;
  14087. procedure TTestModule.TestClass_TypeCastUntypedParam;
  14088. begin
  14089. StartProgram(false);
  14090. Add('type');
  14091. Add(' TObject = class end;');
  14092. Add('procedure ProcA(var A);');
  14093. Add('begin');
  14094. Add(' TObject(A):=nil;');
  14095. Add(' TObject(A):=TObject(A);');
  14096. Add(' if TObject(A)=nil then ;');
  14097. Add(' if nil=TObject(A) then ;');
  14098. Add('end;');
  14099. Add('procedure ProcB(out A);');
  14100. Add('begin');
  14101. Add(' TObject(A):=nil;');
  14102. Add(' TObject(A):=TObject(A);');
  14103. Add(' if TObject(A)=nil then ;');
  14104. Add(' if nil=TObject(A) then ;');
  14105. Add('end;');
  14106. Add('procedure ProcC(const A);');
  14107. Add('begin');
  14108. Add(' if TObject(A)=nil then ;');
  14109. Add(' if nil=TObject(A) then ;');
  14110. Add('end;');
  14111. Add('var o: TObject;');
  14112. Add('begin');
  14113. Add(' ProcA(o);');
  14114. Add(' ProcB(o);');
  14115. Add(' ProcC(o);');
  14116. ConvertProgram;
  14117. CheckSource('TestClass_TypeCastUntypedParam',
  14118. LinesToStr([ // statements
  14119. 'rtl.createClass(this, "TObject", null, function () {',
  14120. ' this.$init = function () {',
  14121. ' };',
  14122. ' this.$final = function () {',
  14123. ' };',
  14124. '});',
  14125. 'this.ProcA = function (A) {',
  14126. ' A.set(null);',
  14127. ' A.set(A.get());',
  14128. ' if (A.get() === null);',
  14129. ' if (null === A.get());',
  14130. '};',
  14131. 'this.ProcB = function (A) {',
  14132. ' A.set(null);',
  14133. ' A.set(A.get());',
  14134. ' if (A.get() === null);',
  14135. ' if (null === A.get());',
  14136. '};',
  14137. 'this.ProcC = function (A) {',
  14138. ' if (A === null);',
  14139. ' if (null === A);',
  14140. '};',
  14141. 'this.o = null;',
  14142. '']),
  14143. LinesToStr([ // $mod.$main
  14144. '$mod.ProcA({',
  14145. ' p: $mod,',
  14146. ' get: function () {',
  14147. ' return this.p.o;',
  14148. ' },',
  14149. ' set: function (v) {',
  14150. ' this.p.o = v;',
  14151. ' }',
  14152. '});',
  14153. '$mod.ProcB({',
  14154. ' p: $mod,',
  14155. ' get: function () {',
  14156. ' return this.p.o;',
  14157. ' },',
  14158. ' set: function (v) {',
  14159. ' this.p.o = v;',
  14160. ' }',
  14161. '});',
  14162. '$mod.ProcC($mod.o);',
  14163. '']));
  14164. end;
  14165. procedure TTestModule.TestClass_Overloads;
  14166. begin
  14167. StartProgram(false);
  14168. Add('type');
  14169. Add(' TObject = class');
  14170. Add(' procedure DoIt;');
  14171. Add(' procedure DoIt(vI: longint);');
  14172. Add(' end;');
  14173. Add('procedure TObject.DoIt;');
  14174. Add('begin');
  14175. Add(' DoIt;');
  14176. Add(' DoIt(1);');
  14177. Add('end;');
  14178. Add('procedure TObject.DoIt(vI: longint); begin end;');
  14179. Add('begin');
  14180. ConvertProgram;
  14181. CheckSource('TestClass_Overloads',
  14182. LinesToStr([ // statements
  14183. 'rtl.createClass(this, "TObject", null, function () {',
  14184. ' this.$init = function () {',
  14185. ' };',
  14186. ' this.$final = function () {',
  14187. ' };',
  14188. ' this.DoIt = function () {',
  14189. ' this.DoIt();',
  14190. ' this.DoIt$1(1);',
  14191. ' };',
  14192. ' this.DoIt$1 = function (vI) {',
  14193. ' };',
  14194. '});',
  14195. '']),
  14196. LinesToStr([ // $mod.$main
  14197. '']));
  14198. end;
  14199. procedure TTestModule.TestClass_OverloadsAncestor;
  14200. begin
  14201. StartProgram(false);
  14202. Add('type');
  14203. Add(' TObject = class;');
  14204. Add(' TObject = class');
  14205. Add(' procedure DoIt(vA: longint);');
  14206. Add(' procedure DoIt(vA, vB: longint);');
  14207. Add(' end;');
  14208. Add(' TCar = class;');
  14209. Add(' TCar = class');
  14210. Add(' procedure DoIt(vA: longint);');
  14211. Add(' procedure DoIt(vA, vB: longint);');
  14212. Add(' end;');
  14213. Add('procedure tobject.doit(va: longint);');
  14214. Add('begin');
  14215. Add(' doit(1);');
  14216. Add(' doit(1,2);');
  14217. Add('end;');
  14218. Add('procedure tobject.doit(va, vb: longint); begin end;');
  14219. Add('procedure tcar.doit(va: longint);');
  14220. Add('begin');
  14221. Add(' doit(1);');
  14222. Add(' doit(1,2);');
  14223. Add(' inherited doit(1);');
  14224. Add(' inherited doit(1,2);');
  14225. Add('end;');
  14226. Add('procedure tcar.doit(va, vb: longint); begin end;');
  14227. Add('begin');
  14228. ConvertProgram;
  14229. CheckSource('TestClass_OverloadsAncestor',
  14230. LinesToStr([ // statements
  14231. 'rtl.createClass(this, "TObject", null, function () {',
  14232. ' this.$init = function () {',
  14233. ' };',
  14234. ' this.$final = function () {',
  14235. ' };',
  14236. ' this.DoIt = function (vA) {',
  14237. ' this.DoIt(1);',
  14238. ' this.DoIt$1(1,2);',
  14239. ' };',
  14240. ' this.DoIt$1 = function (vA, vB) {',
  14241. ' };',
  14242. '});',
  14243. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  14244. ' this.DoIt$2 = function (vA) {',
  14245. ' this.DoIt$2(1);',
  14246. ' this.DoIt$3(1, 2);',
  14247. ' $mod.TObject.DoIt.call(this, 1);',
  14248. ' $mod.TObject.DoIt$1.call(this, 1, 2);',
  14249. ' };',
  14250. ' this.DoIt$3 = function (vA, vB) {',
  14251. ' };',
  14252. '});',
  14253. '']),
  14254. LinesToStr([ // $mod.$main
  14255. '']));
  14256. end;
  14257. procedure TTestModule.TestClass_OverloadConstructor;
  14258. begin
  14259. StartProgram(false);
  14260. Add('type');
  14261. Add(' TObject = class');
  14262. Add(' constructor Create(vA: longint);');
  14263. Add(' constructor Create(vA, vB: longint);');
  14264. Add(' end;');
  14265. Add(' TCar = class');
  14266. Add(' constructor Create(vA: longint);');
  14267. Add(' constructor Create(vA, vB: longint);');
  14268. Add(' end;');
  14269. Add('constructor tobject.create(va: longint);');
  14270. Add('begin');
  14271. Add(' create(1);');
  14272. Add(' create(1,2);');
  14273. Add('end;');
  14274. Add('constructor tobject.create(va, vb: longint); begin end;');
  14275. Add('constructor tcar.create(va: longint);');
  14276. Add('begin');
  14277. Add(' create(1);');
  14278. Add(' create(1,2);');
  14279. Add(' inherited create(1);');
  14280. Add(' inherited create(1,2);');
  14281. Add('end;');
  14282. Add('constructor tcar.create(va, vb: longint); begin end;');
  14283. Add('begin');
  14284. Add(' tobject.create(1);');
  14285. Add(' tobject.create(1,2);');
  14286. Add(' tcar.create(1);');
  14287. Add(' tcar.create(1,2);');
  14288. ConvertProgram;
  14289. CheckSource('TestClass_OverloadConstructor',
  14290. LinesToStr([ // statements
  14291. 'rtl.createClass(this, "TObject", null, function () {',
  14292. ' this.$init = function () {',
  14293. ' };',
  14294. ' this.$final = function () {',
  14295. ' };',
  14296. ' this.Create = function (vA) {',
  14297. ' this.Create(1);',
  14298. ' this.Create$1(1,2);',
  14299. ' return this;',
  14300. ' };',
  14301. ' this.Create$1 = function (vA, vB) {',
  14302. ' return this;',
  14303. ' };',
  14304. '});',
  14305. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  14306. ' this.Create$2 = function (vA) {',
  14307. ' this.Create$2(1);',
  14308. ' this.Create$3(1, 2);',
  14309. ' $mod.TObject.Create.call(this, 1);',
  14310. ' $mod.TObject.Create$1.call(this, 1, 2);',
  14311. ' return this;',
  14312. ' };',
  14313. ' this.Create$3 = function (vA, vB) {',
  14314. ' return this;',
  14315. ' };',
  14316. '});',
  14317. '']),
  14318. LinesToStr([ // $mod.$main
  14319. '$mod.TObject.$create("Create", [1]);',
  14320. '$mod.TObject.$create("Create$1", [1, 2]);',
  14321. '$mod.TCar.$create("Create$2", [1]);',
  14322. '$mod.TCar.$create("Create$3", [1, 2]);',
  14323. '']));
  14324. end;
  14325. procedure TTestModule.TestClass_OverloadDelphiOverride;
  14326. begin
  14327. StartProgram(false);
  14328. Add([
  14329. '{$mode delphi}',
  14330. 'type',
  14331. ' TObject = class end;',
  14332. ' TBird = class',
  14333. ' function {#a}GetValue: longint; overload; virtual;',
  14334. ' function {#b}GetValue(AValue: longint): longint; overload; virtual;',
  14335. ' end;',
  14336. ' TEagle = class(TBird)',
  14337. ' function {#c}GetValue: longint; overload; override;',
  14338. ' function {#d}GetValue(AValue: longint): longint; overload; override;',
  14339. ' end;',
  14340. 'function TBird.GetValue: longint;',
  14341. 'begin',
  14342. ' if 3={@a}GetValue then ;',
  14343. ' if 4={@b}GetValue(5) then ;',
  14344. 'end;',
  14345. 'function TBird.GetValue(AValue: longint): longint;',
  14346. 'begin',
  14347. 'end;',
  14348. 'function TEagle.GetValue: longint;',
  14349. 'begin',
  14350. ' if 13={@c}GetValue then ;',
  14351. ' if 14={@d}GetValue(15) then ;',
  14352. ' if 15=inherited {@a}GetValue then ;',
  14353. ' if 16=inherited {@b}GetValue(17) then ;',
  14354. 'end;',
  14355. 'function TEagle.GetValue(AValue: longint): longint;',
  14356. 'begin',
  14357. 'end;',
  14358. 'var',
  14359. ' e: TEagle;',
  14360. 'begin',
  14361. ' if 23=e.{@c}GetValue then ;',
  14362. ' if 24=e.{@d}GetValue(25) then ;']);
  14363. ConvertProgram;
  14364. CheckSource('TestClass_OverloadDelphiOverride',
  14365. LinesToStr([ // statements
  14366. 'rtl.createClass(this, "TObject", null, function () {',
  14367. ' this.$init = function () {',
  14368. ' };',
  14369. ' this.$final = function () {',
  14370. ' };',
  14371. '});',
  14372. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  14373. ' this.GetValue = function () {',
  14374. ' var Result = 0;',
  14375. ' if (3 === this.GetValue()) ;',
  14376. ' if (4 === this.GetValue$1(5)) ;',
  14377. ' return Result;',
  14378. ' };',
  14379. ' this.GetValue$1 = function (AValue) {',
  14380. ' var Result = 0;',
  14381. ' return Result;',
  14382. ' };',
  14383. '});',
  14384. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  14385. ' this.GetValue = function () {',
  14386. ' var Result = 0;',
  14387. ' if (13 === this.GetValue()) ;',
  14388. ' if (14 === this.GetValue$1(15)) ;',
  14389. ' if (15 === $mod.TBird.GetValue.call(this)) ;',
  14390. ' if (16 === $mod.TBird.GetValue$1.call(this, 17)) ;',
  14391. ' return Result;',
  14392. ' };',
  14393. ' this.GetValue$1 = function (AValue) {',
  14394. ' var Result = 0;',
  14395. ' return Result;',
  14396. ' };',
  14397. '});',
  14398. 'this.e = null;',
  14399. '']),
  14400. LinesToStr([ // $mod.$main
  14401. 'if (23 === $mod.e.GetValue()) ;',
  14402. 'if (24 === $mod.e.GetValue$1(25)) ;',
  14403. '']));
  14404. end;
  14405. procedure TTestModule.TestClass_ReintroduceVarDelphi;
  14406. begin
  14407. StartProgram(false);
  14408. Add([
  14409. '{$mode delphi}',
  14410. 'type',
  14411. ' TObject = class end;',
  14412. ' TAnimal = class',
  14413. ' public',
  14414. ' {#animal_a}A: longint;',
  14415. ' function {#animal_b}B: longint;',
  14416. ' end;',
  14417. ' TBird = class(TAnimal)',
  14418. ' public',
  14419. ' {#bird_a}A: double;',
  14420. ' {#bird_b}B: boolean;',
  14421. ' end;',
  14422. ' TEagle = class(TBird)',
  14423. ' public',
  14424. ' function {#eagle_a}A: boolean;',
  14425. ' {#eagle_b}B: double;',
  14426. ' end;',
  14427. 'function TAnimal.B: longint;',
  14428. 'begin',
  14429. 'end;',
  14430. 'function TEagle.A: boolean;',
  14431. 'begin',
  14432. ' {@eagle_b}B:=3.3;',
  14433. ' {@eagle_a}A();',
  14434. ' TBird(Self).{@bird_b}B:=true;',
  14435. ' TAnimal(Self).{@animal_a}A:=17;',
  14436. ' inherited {@bird_b}B:=inherited {bird_a}A>1;', // Delphi allows only inherited <functionname>
  14437. 'end;',
  14438. 'var',
  14439. ' e: TEagle;',
  14440. 'begin',
  14441. ' e.{@eagle_b}B:=5.3;',
  14442. ' if e.{@eagle_a}A then ;',
  14443. '']);
  14444. ConvertProgram;
  14445. CheckSource('TestClass_ReintroduceVarDelphi',
  14446. LinesToStr([ // statements
  14447. 'rtl.createClass(this, "TObject", null, function () {',
  14448. ' this.$init = function () {',
  14449. ' };',
  14450. ' this.$final = function () {',
  14451. ' };',
  14452. '});',
  14453. 'rtl.createClass(this, "TAnimal", this.TObject, function () {',
  14454. ' this.$init = function () {',
  14455. ' $mod.TObject.$init.call(this);',
  14456. ' this.A = 0;',
  14457. ' };',
  14458. ' this.B = function () {',
  14459. ' var Result = 0;',
  14460. ' return Result;',
  14461. ' };',
  14462. '});',
  14463. 'rtl.createClass(this, "TBird", this.TAnimal, function () {',
  14464. ' this.$init = function () {',
  14465. ' $mod.TAnimal.$init.call(this);',
  14466. ' this.A$1 = 0.0;',
  14467. ' this.B$1 = false;',
  14468. ' };',
  14469. '});',
  14470. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  14471. ' this.$init = function () {',
  14472. ' $mod.TBird.$init.call(this);',
  14473. ' this.B$2 = 0.0;',
  14474. ' };',
  14475. ' this.A$2 = function () {',
  14476. ' var Result = false;',
  14477. ' this.B$2 = 3.3;',
  14478. ' this.A$2();',
  14479. ' this.B$1 = true;',
  14480. ' this.A = 17;',
  14481. ' this.B$1 = this.A$1 > 1;',
  14482. ' return Result;',
  14483. ' };',
  14484. '});',
  14485. 'this.e = null;',
  14486. '']),
  14487. LinesToStr([ // $mod.$main
  14488. '$mod.e.B$2 = 5.3;',
  14489. 'if ($mod.e.A$2()) ;',
  14490. '']));
  14491. end;
  14492. procedure TTestModule.TestClass_ReintroducedVar;
  14493. begin
  14494. StartProgram(false);
  14495. Add('type');
  14496. Add(' TObject = class');
  14497. Add(' strict private');
  14498. Add(' Some: longint;');
  14499. Add(' end;');
  14500. Add(' TMobile = class');
  14501. Add(' strict private');
  14502. Add(' Some: string;');
  14503. Add(' end;');
  14504. Add(' TCar = class(tmobile)');
  14505. Add(' procedure Some;');
  14506. Add(' procedure Some(vA: longint);');
  14507. Add(' end;');
  14508. Add('procedure tcar.some;');
  14509. Add('begin');
  14510. Add(' Some;');
  14511. Add(' Some(1);');
  14512. Add('end;');
  14513. Add('procedure tcar.some(va: longint); begin end;');
  14514. Add('begin');
  14515. ConvertProgram;
  14516. CheckSource('TestClass_ReintroducedVar',
  14517. LinesToStr([ // statements
  14518. 'rtl.createClass(this, "TObject", null, function () {',
  14519. ' this.$init = function () {',
  14520. ' this.Some = 0;',
  14521. ' };',
  14522. ' this.$final = function () {',
  14523. ' };',
  14524. '});',
  14525. 'rtl.createClass(this, "TMobile", this.TObject, function () {',
  14526. ' this.$init = function () {',
  14527. ' $mod.TObject.$init.call(this);',
  14528. ' this.Some$1 = "";',
  14529. ' };',
  14530. '});',
  14531. 'rtl.createClass(this, "TCar", this.TMobile, function () {',
  14532. ' this.Some$2 = function () {',
  14533. ' this.Some$2();',
  14534. ' this.Some$3(1);',
  14535. ' };',
  14536. ' this.Some$3 = function (vA) {',
  14537. ' };',
  14538. '});',
  14539. '']),
  14540. LinesToStr([ // $mod.$main
  14541. '']));
  14542. end;
  14543. procedure TTestModule.TestClass_RaiseDescendant;
  14544. begin
  14545. StartProgram(false);
  14546. Add([
  14547. 'type',
  14548. ' TObject = class',
  14549. ' constructor Create(Msg: string);',
  14550. ' end;',
  14551. ' Exception = class',
  14552. ' end;',
  14553. ' EConvertError = class(Exception)',
  14554. ' end;',
  14555. 'constructor TObject.Create(Msg: string); begin end;',
  14556. 'function AssertConv(Msg: string = ''def''): EConvertError; begin end;',
  14557. 'begin',
  14558. ' raise Exception.Create(''Bar1'');',
  14559. ' raise EConvertError.Create(''Bar2'');',
  14560. ' raise AssertConv(''Bar2'');',
  14561. ' raise AssertConv;',
  14562. '']);
  14563. ConvertProgram;
  14564. CheckSource('TestClass_RaiseDescendant',
  14565. LinesToStr([ // statements
  14566. 'rtl.createClass(this, "TObject", null, function () {',
  14567. ' this.$init = function () {',
  14568. ' };',
  14569. ' this.$final = function () {',
  14570. ' };',
  14571. ' this.Create = function (Msg) {',
  14572. ' return this;',
  14573. ' };',
  14574. '});',
  14575. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  14576. '});',
  14577. 'rtl.createClass(this, "EConvertError", this.Exception, function () {',
  14578. '});',
  14579. 'this.AssertConv = function (Msg) {',
  14580. ' var Result = null;',
  14581. ' return Result;',
  14582. '};',
  14583. '']),
  14584. LinesToStr([ // $mod.$main
  14585. 'throw $mod.Exception.$create("Create",["Bar1"]);',
  14586. 'throw $mod.EConvertError.$create("Create",["Bar2"]);',
  14587. 'throw $mod.AssertConv("Bar2");',
  14588. 'throw $mod.AssertConv("def");',
  14589. '']));
  14590. end;
  14591. procedure TTestModule.TestClass_ExternalMethod;
  14592. begin
  14593. AddModuleWithIntfImplSrc('unit2.pas',
  14594. LinesToStr([
  14595. 'type',
  14596. ' TObject = class',
  14597. ' public',
  14598. ' procedure Intern; external name ''$DoIntern'';',
  14599. ' end;',
  14600. '']),
  14601. LinesToStr([
  14602. '']));
  14603. StartUnit(true);
  14604. Add('interface');
  14605. Add('uses unit2;');
  14606. Add('type');
  14607. Add(' TCar = class(TObject)');
  14608. Add(' public');
  14609. Add(' procedure Intern2; external name ''$DoIntern2'';');
  14610. Add(' procedure DoIt;');
  14611. Add(' end;');
  14612. Add('implementation');
  14613. Add('procedure tcar.doit;');
  14614. Add('begin');
  14615. Add(' Intern;');
  14616. Add(' Intern();');
  14617. Add(' Intern2;');
  14618. Add(' Intern2();');
  14619. Add('end;');
  14620. Add('var Obj: TCar;');
  14621. Add('begin');
  14622. Add(' obj.intern;');
  14623. Add(' obj.intern();');
  14624. Add(' obj.intern2;');
  14625. Add(' obj.intern2();');
  14626. Add(' obj.doit;');
  14627. Add(' obj.doit();');
  14628. Add(' with obj do begin');
  14629. Add(' Intern;');
  14630. Add(' Intern();');
  14631. Add(' Intern2;');
  14632. Add(' Intern2();');
  14633. Add(' end;');
  14634. ConvertUnit;
  14635. CheckSource('TestClass_ExternalMethod',
  14636. LinesToStr([
  14637. 'var $impl = $mod.$impl;',
  14638. 'rtl.createClass(this, "TCar", pas.unit2.TObject, function () {',
  14639. ' this.DoIt = function () {',
  14640. ' this.$DoIntern();',
  14641. ' this.$DoIntern();',
  14642. ' this.$DoIntern2();',
  14643. ' this.$DoIntern2();',
  14644. ' };',
  14645. ' });',
  14646. '']),
  14647. LinesToStr([ // this.$init
  14648. '$impl.Obj.$DoIntern();',
  14649. '$impl.Obj.$DoIntern();',
  14650. '$impl.Obj.$DoIntern2();',
  14651. '$impl.Obj.$DoIntern2();',
  14652. '$impl.Obj.DoIt();',
  14653. '$impl.Obj.DoIt();',
  14654. 'var $with = $impl.Obj;',
  14655. '$with.$DoIntern();',
  14656. '$with.$DoIntern();',
  14657. '$with.$DoIntern2();',
  14658. '$with.$DoIntern2();',
  14659. '']),
  14660. LinesToStr([ // implementation
  14661. '$impl.Obj = null;',
  14662. '']) );
  14663. end;
  14664. procedure TTestModule.TestClass_ExternalVirtualNameMismatchFail;
  14665. begin
  14666. StartProgram(false);
  14667. Add('type');
  14668. Add(' TObject = class');
  14669. Add(' procedure DoIt; virtual; external name ''Foo'';');
  14670. Add(' end;');
  14671. Add('begin');
  14672. SetExpectedPasResolverError('Virtual method name must match external',
  14673. nVirtualMethodNameMustMatchExternal);
  14674. ConvertProgram;
  14675. end;
  14676. procedure TTestModule.TestClass_ExternalOverrideFail;
  14677. begin
  14678. StartProgram(false);
  14679. Add('type');
  14680. Add(' TObject = class');
  14681. Add(' procedure DoIt; virtual; external name ''DoIt'';');
  14682. Add(' end;');
  14683. Add(' TCar = class');
  14684. Add(' procedure DoIt; override; external name ''DoIt'';');
  14685. Add(' end;');
  14686. Add('begin');
  14687. SetExpectedPasResolverError('Invalid procedure modifier override,external',
  14688. nInvalidXModifierY);
  14689. ConvertProgram;
  14690. end;
  14691. procedure TTestModule.TestClass_ExternalVar;
  14692. begin
  14693. AddModuleWithIntfImplSrc('unit2.pas',
  14694. LinesToStr([
  14695. '{$modeswitch externalclass}',
  14696. 'type',
  14697. ' TObject = class',
  14698. ' public',
  14699. ' Intern: longint external name ''$Intern'';',
  14700. ' Bracket: longint external name ''["A B"]'';',
  14701. ' end;',
  14702. '']),
  14703. LinesToStr([
  14704. '']));
  14705. StartUnit(true);
  14706. Add([
  14707. 'interface',
  14708. 'uses unit2;',
  14709. '{$modeswitch externalclass}',
  14710. 'type',
  14711. ' TCar = class(tobject)',
  14712. ' public',
  14713. ' Intern2: longint external name ''$Intern2'';',
  14714. ' procedure DoIt;',
  14715. ' end;',
  14716. 'implementation',
  14717. 'procedure tcar.doit;',
  14718. 'begin',
  14719. ' Intern:=Intern+1;',
  14720. ' Intern2:=Intern2+2;',
  14721. ' Bracket:=Bracket+3;',
  14722. 'end;',
  14723. 'var Obj: TCar;',
  14724. 'begin',
  14725. ' obj.intern:=obj.intern+1;',
  14726. ' obj.intern2:=obj.intern2+2;',
  14727. ' obj.Bracket:=obj.Bracket+3;',
  14728. ' with obj do begin',
  14729. ' intern:=intern+1;',
  14730. ' intern2:=intern2+2;',
  14731. ' Bracket:=Bracket+3;',
  14732. ' end;']);
  14733. ConvertUnit;
  14734. CheckSource('TestClass_ExternalVar',
  14735. LinesToStr([
  14736. 'var $impl = $mod.$impl;',
  14737. 'rtl.createClass(this, "TCar", pas.unit2.TObject, function () {',
  14738. ' this.DoIt = function () {',
  14739. ' this.$Intern = this.$Intern + 1;',
  14740. ' this.$Intern2 = this.$Intern2 + 2;',
  14741. ' this["A B"] = this["A B"] + 3;',
  14742. ' };',
  14743. ' });',
  14744. '']),
  14745. LinesToStr([
  14746. '$impl.Obj.$Intern = $impl.Obj.$Intern + 1;',
  14747. '$impl.Obj.$Intern2 = $impl.Obj.$Intern2 + 2;',
  14748. '$impl.Obj["A B"] = $impl.Obj["A B"] + 3;',
  14749. 'var $with = $impl.Obj;',
  14750. '$with.$Intern = $with.$Intern + 1;',
  14751. '$with.$Intern2 = $with.$Intern2 + 2;',
  14752. '$with["A B"] = $with["A B"] + 3;',
  14753. '']),
  14754. LinesToStr([ // implementation
  14755. '$impl.Obj = null;',
  14756. '']));
  14757. end;
  14758. procedure TTestModule.TestClass_Const;
  14759. begin
  14760. StartProgram(false);
  14761. Add([
  14762. 'type',
  14763. ' integer = longint;',
  14764. ' TClass = class of TObject;',
  14765. ' TObject = class',
  14766. ' public',
  14767. ' const cI: integer = 3;',
  14768. ' procedure DoIt;',
  14769. ' class procedure DoMore;',
  14770. ' end;',
  14771. 'procedure tobject.doit;',
  14772. 'begin',
  14773. ' if cI=4 then;',
  14774. ' if 5=cI then;',
  14775. ' if Self.cI=6 then;',
  14776. ' if 7=Self.cI then;',
  14777. ' with Self do begin',
  14778. ' if cI=11 then;',
  14779. ' if 12=cI then;',
  14780. ' end;',
  14781. 'end;',
  14782. 'class procedure tobject.domore;',
  14783. 'begin',
  14784. ' if cI=8 then;',
  14785. ' if Self.cI=9 then;',
  14786. ' if 10=cI then;',
  14787. ' if 11=Self.cI then;',
  14788. ' with Self do begin',
  14789. ' if cI=13 then;',
  14790. ' if 14=cI then;',
  14791. ' end;',
  14792. 'end;',
  14793. 'var',
  14794. ' Obj: TObject;',
  14795. ' Cla: TClass;',
  14796. 'begin',
  14797. ' if TObject.cI=21 then ;',
  14798. ' if Obj.cI=22 then ;',
  14799. ' if Cla.cI=23 then ;',
  14800. ' with obj do if ci=24 then;',
  14801. ' with TObject do if ci=25 then;',
  14802. ' with Cla do if ci=26 then;']);
  14803. ConvertProgram;
  14804. CheckSource('TestClass_Const',
  14805. LinesToStr([
  14806. 'rtl.createClass(this, "TObject", null, function () {',
  14807. ' this.cI = 3;',
  14808. ' this.$init = function () {',
  14809. ' };',
  14810. ' this.$final = function () {',
  14811. ' };',
  14812. ' this.DoIt = function () {',
  14813. ' if (this.cI === 4) ;',
  14814. ' if (5 === this.cI) ;',
  14815. ' if (this.cI === 6) ;',
  14816. ' if (7 === this.cI) ;',
  14817. ' if (this.cI === 11) ;',
  14818. ' if (12 === this.cI) ;',
  14819. ' };',
  14820. ' this.DoMore = function () {',
  14821. ' if (this.cI === 8) ;',
  14822. ' if (this.cI === 9) ;',
  14823. ' if (10 === this.cI) ;',
  14824. ' if (11 === this.cI) ;',
  14825. ' if (this.cI === 13) ;',
  14826. ' if (14 === this.cI) ;',
  14827. ' };',
  14828. '});',
  14829. 'this.Obj = null;',
  14830. 'this.Cla = null;',
  14831. '']),
  14832. LinesToStr([
  14833. 'if ($mod.TObject.cI === 21) ;',
  14834. 'if ($mod.Obj.cI === 22) ;',
  14835. 'if ($mod.Cla.cI === 23) ;',
  14836. 'var $with = $mod.Obj;',
  14837. 'if ($with.cI === 24) ;',
  14838. 'var $with1 = $mod.TObject;',
  14839. 'if ($with1.cI === 25) ;',
  14840. 'var $with2 = $mod.Cla;',
  14841. 'if ($with2.cI === 26) ;',
  14842. '']));
  14843. end;
  14844. procedure TTestModule.TestClass_ConstEnum;
  14845. begin
  14846. StartProgram(false);
  14847. Add([
  14848. 'type',
  14849. ' TEnum = (red,blue);',
  14850. ' TObject = class',
  14851. ' end;',
  14852. ' TAnimal = class',
  14853. ' public',
  14854. ' type TSubEnum = (light,dark);',
  14855. ' const a = high(TEnum);',
  14856. ' const b = high(TSubEnum);',
  14857. ' end;',
  14858. ' TBird = class(TAnimal)',
  14859. ' public',
  14860. ' const c = high(TEnum);',
  14861. ' const d = high(TSubEnum);',
  14862. ' end;',
  14863. ' TAnt = class',
  14864. ' public',
  14865. ' const e = high(TEnum);',
  14866. ' const f = high(TBird.TSubEnum);',
  14867. ' end;',
  14868. 'begin',
  14869. '']);
  14870. ConvertProgram;
  14871. CheckSource('TestClass_ConstEnum',
  14872. LinesToStr([
  14873. 'this.TEnum = {',
  14874. ' "0": "red",',
  14875. ' red: 0,',
  14876. ' "1": "blue",',
  14877. ' blue: 1',
  14878. '};',
  14879. 'rtl.createClass(this, "TObject", null, function () {',
  14880. ' this.$init = function () {',
  14881. ' };',
  14882. ' this.$final = function () {',
  14883. ' };',
  14884. '});',
  14885. 'rtl.createClass(this, "TAnimal", this.TObject, function () {',
  14886. ' this.TSubEnum = {',
  14887. ' "0": "light",',
  14888. ' light: 0,',
  14889. ' "1": "dark",',
  14890. ' dark: 1',
  14891. ' };',
  14892. ' this.a = $mod.TEnum.blue;',
  14893. ' this.b = this.TSubEnum.dark;',
  14894. '});',
  14895. 'rtl.createClass(this, "TBird", this.TAnimal, function () {',
  14896. ' this.c = $mod.TEnum.blue;',
  14897. ' this.d = this.TSubEnum.dark;',
  14898. '});',
  14899. 'rtl.createClass(this, "TAnt", this.TObject, function () {',
  14900. ' this.e = $mod.TEnum.blue;',
  14901. ' this.f = $mod.TAnimal.TSubEnum.dark;',
  14902. '});',
  14903. '']),
  14904. LinesToStr([
  14905. '']));
  14906. end;
  14907. procedure TTestModule.TestClass_LocalConstDuplicate_Prg;
  14908. begin
  14909. StartProgram(false);
  14910. Add([
  14911. 'type',
  14912. ' TObject = class',
  14913. ' const cI: longint = 3;',
  14914. ' procedure Fly;',
  14915. ' procedure Run;',
  14916. ' end;',
  14917. ' TBird = class',
  14918. ' procedure Go;',
  14919. ' end;',
  14920. 'procedure tobject.fly;',
  14921. 'const cI: word = 4;',
  14922. 'begin',
  14923. ' if cI=Self.cI then ;',
  14924. 'end;',
  14925. 'procedure tobject.run;',
  14926. 'const cI: word = 5;',
  14927. 'begin',
  14928. ' if cI=Self.cI then ;',
  14929. 'end;',
  14930. 'procedure tbird.go;',
  14931. 'const cI: word = 6;',
  14932. 'begin',
  14933. ' if cI=Self.cI then ;',
  14934. 'end;',
  14935. 'begin',
  14936. '']);
  14937. ConvertProgram;
  14938. CheckSource('TestClass_LocalConstDuplicate_Prg',
  14939. LinesToStr([
  14940. 'rtl.createClass(this, "TObject", null, function () {',
  14941. ' this.cI = 3;',
  14942. ' this.$init = function () {',
  14943. ' };',
  14944. ' this.$final = function () {',
  14945. ' };',
  14946. ' var cI$1 = 4;',
  14947. ' this.Fly = function () {',
  14948. ' if (cI$1 === this.cI) ;',
  14949. ' };',
  14950. ' var cI$2 = 5;',
  14951. ' this.Run = function () {',
  14952. ' if (cI$2 === this.cI) ;',
  14953. ' };',
  14954. '});',
  14955. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  14956. ' var cI$3 = 6;',
  14957. ' this.Go = function () {',
  14958. ' if (cI$3 === this.cI) ;',
  14959. ' };',
  14960. '});',
  14961. '']),
  14962. LinesToStr([
  14963. '']));
  14964. end;
  14965. procedure TTestModule.TestClass_LocalConstDuplicate_Unit;
  14966. begin
  14967. StartUnit(false);
  14968. Add([
  14969. 'interface',
  14970. 'type',
  14971. ' TObject = class',
  14972. ' const cI: longint = 3;',
  14973. ' procedure Fly;',
  14974. ' procedure Run;',
  14975. ' end;',
  14976. ' TBird = class',
  14977. ' procedure Go;',
  14978. ' end;',
  14979. 'implementation',
  14980. 'procedure tobject.fly;',
  14981. 'const cI: word = 4;',
  14982. 'begin',
  14983. ' if cI=Self.cI then ;',
  14984. 'end;',
  14985. 'procedure tobject.run;',
  14986. 'const cI: word = 5;',
  14987. 'begin',
  14988. ' if cI=Self.cI then ;',
  14989. 'end;',
  14990. 'procedure tbird.go;',
  14991. 'const cI: word = 6;',
  14992. 'begin',
  14993. ' if cI=Self.cI then ;',
  14994. 'end;',
  14995. '']);
  14996. ConvertUnit;
  14997. CheckSource('TestClass_LocalConstDuplicate_Unit',
  14998. LinesToStr([
  14999. 'rtl.createClass(this, "TObject", null, function () {',
  15000. ' this.cI = 3;',
  15001. ' this.$init = function () {',
  15002. ' };',
  15003. ' this.$final = function () {',
  15004. ' };',
  15005. ' var cI$1 = 4;',
  15006. ' this.Fly = function () {',
  15007. ' if (cI$1 === this.cI) ;',
  15008. ' };',
  15009. ' var cI$2 = 5;',
  15010. ' this.Run = function () {',
  15011. ' if (cI$2 === this.cI) ;',
  15012. ' };',
  15013. '});',
  15014. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15015. ' var cI$3 = 6;',
  15016. ' this.Go = function () {',
  15017. ' if (cI$3 === this.cI) ;',
  15018. ' };',
  15019. '});',
  15020. '']),
  15021. '',
  15022. '');
  15023. end;
  15024. procedure TTestModule.TestClass_LocalVarSelfFail;
  15025. begin
  15026. StartProgram(false);
  15027. Add([
  15028. 'type',
  15029. ' TObject = class',
  15030. ' constructor Create;',
  15031. ' end;',
  15032. 'constructor tobject.create;',
  15033. 'var self: longint;',
  15034. 'begin',
  15035. 'end',
  15036. 'begin',
  15037. '']);
  15038. SetExpectedPasResolverError('Duplicate identifier "self" at (0)',nDuplicateIdentifier);
  15039. ConvertProgram;
  15040. end;
  15041. procedure TTestModule.TestClass_ArgSelfFail;
  15042. begin
  15043. StartProgram(false);
  15044. Add([
  15045. 'type',
  15046. ' TObject = class',
  15047. ' procedure DoIt(Self: longint);',
  15048. ' end;',
  15049. 'procedure tobject.doit(self: longint);',
  15050. 'begin',
  15051. 'end',
  15052. 'begin',
  15053. '']);
  15054. SetExpectedPasResolverError('Duplicate identifier "Self" at test1.pp(5,24)',nDuplicateIdentifier);
  15055. ConvertProgram;
  15056. end;
  15057. procedure TTestModule.TestClass_NestedProcSelf;
  15058. begin
  15059. StartProgram(false);
  15060. Add([
  15061. 'type',
  15062. ' TObject = class',
  15063. ' Key: longint;',
  15064. ' class var State: longint;',
  15065. ' procedure DoIt;',
  15066. ' function GetSize: longint; virtual; abstract;',
  15067. ' procedure SetSize(Value: longint); virtual; abstract;',
  15068. ' property Size: longint read GetSize write SetSize;',
  15069. ' end;',
  15070. 'procedure tobject.doit;',
  15071. ' procedure Sub;',
  15072. ' begin',
  15073. ' key:=key+2;',
  15074. ' self.key:=self.key+3;',
  15075. ' state:=state+4;',
  15076. ' self.state:=self.state+5;',
  15077. ' tobject.state:=tobject.state+6;',
  15078. ' size:=size+7;',
  15079. ' self.size:=self.size+8;',
  15080. ' end;',
  15081. 'begin',
  15082. ' sub;',
  15083. ' key:=key+12;',
  15084. ' self.key:=self.key+13;',
  15085. ' state:=state+14;',
  15086. ' self.state:=self.state+15;',
  15087. ' tobject.state:=tobject.state+16;',
  15088. ' size:=size+17;',
  15089. ' self.size:=self.size+18;',
  15090. 'end;',
  15091. 'begin',
  15092. '']);
  15093. ConvertProgram;
  15094. CheckSource('TestClass_NestedProcSelf',
  15095. LinesToStr([ // statements
  15096. 'rtl.createClass(this, "TObject", null, function () {',
  15097. ' this.State = 0;',
  15098. ' this.$init = function () {',
  15099. ' this.Key = 0;',
  15100. ' };',
  15101. ' this.$final = function () {',
  15102. ' };',
  15103. ' this.DoIt = function () {',
  15104. ' var $Self = this;',
  15105. ' function Sub() {',
  15106. ' $Self.Key = $Self.Key + 2;',
  15107. ' $Self.Key = $Self.Key + 3;',
  15108. ' $mod.TObject.State = $Self.State + 4;',
  15109. ' $mod.TObject.State = $Self.State + 5;',
  15110. ' $mod.TObject.State = $mod.TObject.State + 6;',
  15111. ' $Self.SetSize($Self.GetSize() + 7);',
  15112. ' $Self.SetSize($Self.GetSize() + 8);',
  15113. ' };',
  15114. ' Sub();',
  15115. ' this.Key = this.Key + 12;',
  15116. ' $Self.Key = $Self.Key + 13;',
  15117. ' $mod.TObject.State = this.State + 14;',
  15118. ' $mod.TObject.State = $Self.State + 15;',
  15119. ' $mod.TObject.State = $mod.TObject.State + 16;',
  15120. ' this.SetSize(this.GetSize() + 17);',
  15121. ' $Self.SetSize($Self.GetSize() + 18);',
  15122. ' };',
  15123. '});',
  15124. '']),
  15125. LinesToStr([ // $mod.$main
  15126. '']));
  15127. end;
  15128. procedure TTestModule.TestClass_NestedProcSelf2;
  15129. begin
  15130. StartProgram(false);
  15131. Add([
  15132. 'type',
  15133. ' TObject = class',
  15134. ' Key: longint;',
  15135. ' class var State: longint;',
  15136. ' function GetSize: longint; virtual; abstract;',
  15137. ' procedure SetSize(Value: longint); virtual; abstract;',
  15138. ' property Size: longint read GetSize write SetSize;',
  15139. ' end;',
  15140. ' TBird = class',
  15141. ' procedure DoIt;',
  15142. ' end;',
  15143. 'procedure tbird.doit;',
  15144. ' procedure Sub;',
  15145. ' begin',
  15146. ' key:=key+2;',
  15147. ' self.key:=self.key+3;',
  15148. ' state:=state+4;',
  15149. ' self.state:=self.state+5;',
  15150. ' tobject.state:=tobject.state+6;',
  15151. ' size:=size+7;',
  15152. ' self.size:=self.size+8;',
  15153. ' end;',
  15154. 'begin',
  15155. ' sub;',
  15156. ' key:=key+12;',
  15157. ' self.key:=self.key+13;',
  15158. ' state:=state+14;',
  15159. ' self.state:=self.state+15;',
  15160. ' tobject.state:=tobject.state+16;',
  15161. ' size:=size+17;',
  15162. ' self.size:=self.size+18;',
  15163. 'end;',
  15164. 'begin',
  15165. '']);
  15166. ConvertProgram;
  15167. CheckSource('TestClass_NestedProcSelf2',
  15168. LinesToStr([ // statements
  15169. 'rtl.createClass(this, "TObject", null, function () {',
  15170. ' this.State = 0;',
  15171. ' this.$init = function () {',
  15172. ' this.Key = 0;',
  15173. ' };',
  15174. ' this.$final = function () {',
  15175. ' };',
  15176. '});',
  15177. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15178. ' this.DoIt = function () {',
  15179. ' var $Self = this;',
  15180. ' function Sub() {',
  15181. ' $Self.Key = $Self.Key + 2;',
  15182. ' $Self.Key = $Self.Key + 3;',
  15183. ' $mod.TObject.State = $Self.State + 4;',
  15184. ' $mod.TObject.State = $Self.State + 5;',
  15185. ' $mod.TObject.State = $mod.TObject.State + 6;',
  15186. ' $Self.SetSize($Self.GetSize() + 7);',
  15187. ' $Self.SetSize($Self.GetSize() + 8);',
  15188. ' };',
  15189. ' Sub();',
  15190. ' this.Key = this.Key + 12;',
  15191. ' $Self.Key = $Self.Key + 13;',
  15192. ' $mod.TObject.State = this.State + 14;',
  15193. ' $mod.TObject.State = $Self.State + 15;',
  15194. ' $mod.TObject.State = $mod.TObject.State + 16;',
  15195. ' this.SetSize(this.GetSize() + 17);',
  15196. ' $Self.SetSize($Self.GetSize() + 18);',
  15197. ' };',
  15198. '});',
  15199. '']),
  15200. LinesToStr([ // $mod.$main
  15201. '']));
  15202. end;
  15203. procedure TTestModule.TestClass_NestedProcClassSelf;
  15204. begin
  15205. StartProgram(false);
  15206. Add([
  15207. 'type',
  15208. ' TObject = class',
  15209. ' class var State: longint;',
  15210. ' class procedure DoIt;',
  15211. ' class function GetSize: longint; virtual; abstract;',
  15212. ' class procedure SetSize(Value: longint); virtual; abstract;',
  15213. ' class property Size: longint read GetSize write SetSize;',
  15214. ' end;',
  15215. 'class procedure tobject.doit;',
  15216. ' procedure Sub;',
  15217. ' begin',
  15218. ' state:=state+2;',
  15219. ' self.state:=self.state+3;',
  15220. ' tobject.state:=tobject.state+4;',
  15221. ' size:=size+5;',
  15222. ' self.size:=self.size+6;',
  15223. ' tobject.size:=tobject.size+7;',
  15224. ' end;',
  15225. 'begin',
  15226. ' sub;',
  15227. ' state:=state+12;',
  15228. ' self.state:=self.state+13;',
  15229. ' tobject.state:=tobject.state+14;',
  15230. ' size:=size+15;',
  15231. ' self.size:=self.size+16;',
  15232. ' tobject.size:=tobject.size+17;',
  15233. 'end;',
  15234. 'begin',
  15235. '']);
  15236. ConvertProgram;
  15237. CheckSource('TestClass_NestedProcClassSelf',
  15238. LinesToStr([ // statements
  15239. 'rtl.createClass(this, "TObject", null, function () {',
  15240. ' this.State = 0;',
  15241. ' this.$init = function () {',
  15242. ' };',
  15243. ' this.$final = function () {',
  15244. ' };',
  15245. ' this.DoIt = function () {',
  15246. ' var $Self = this;',
  15247. ' function Sub() {',
  15248. ' $mod.TObject.State = $Self.State + 2;',
  15249. ' $mod.TObject.State = $Self.State + 3;',
  15250. ' $mod.TObject.State = $mod.TObject.State + 4;',
  15251. ' $Self.SetSize($Self.GetSize() + 5);',
  15252. ' $Self.SetSize($Self.GetSize() + 6);',
  15253. ' $mod.TObject.SetSize($mod.TObject.GetSize() + 7);',
  15254. ' };',
  15255. ' Sub();',
  15256. ' $mod.TObject.State = this.State + 12;',
  15257. ' $mod.TObject.State = $Self.State + 13;',
  15258. ' $mod.TObject.State = $mod.TObject.State + 14;',
  15259. ' this.SetSize(this.GetSize() + 15);',
  15260. ' $Self.SetSize($Self.GetSize() + 16);',
  15261. ' $mod.TObject.SetSize($mod.TObject.GetSize() + 17);',
  15262. ' };',
  15263. '});',
  15264. '']),
  15265. LinesToStr([ // $mod.$main
  15266. '']));
  15267. end;
  15268. procedure TTestModule.TestClass_NestedProcCallInherited;
  15269. begin
  15270. StartProgram(false);
  15271. Add([
  15272. 'type',
  15273. ' TObject = class',
  15274. ' function DoIt(k: boolean): longint; virtual;',
  15275. ' end;',
  15276. ' TBird = class',
  15277. ' function DoIt(k: boolean): longint; override;',
  15278. ' end;',
  15279. 'function tobject.doit(k: boolean): longint;',
  15280. 'begin',
  15281. 'end;',
  15282. 'function tbird.doit(k: boolean): longint;',
  15283. ' procedure Sub;',
  15284. ' begin',
  15285. ' inherited DoIt(true);',
  15286. //' if inherited DoIt(false)=4 then ;',
  15287. ' end;',
  15288. 'begin',
  15289. ' Sub;',
  15290. ' inherited;',
  15291. ' inherited DoIt(true);',
  15292. //' if inherited DoIt(false)=14 then ;',
  15293. 'end;',
  15294. 'begin',
  15295. '']);
  15296. ConvertProgram;
  15297. CheckSource('TestClass_NestedProcCallInherited',
  15298. LinesToStr([ // statements
  15299. 'rtl.createClass(this, "TObject", null, function () {',
  15300. ' this.$init = function () {',
  15301. ' };',
  15302. ' this.$final = function () {',
  15303. ' };',
  15304. ' this.DoIt = function (k) {',
  15305. ' var Result = 0;',
  15306. ' return Result;',
  15307. ' };',
  15308. '});',
  15309. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15310. ' this.DoIt = function (k) {',
  15311. ' var $Self = this;',
  15312. ' var Result = 0;',
  15313. ' function Sub() {',
  15314. ' $mod.TObject.DoIt.call($Self, true);',
  15315. ' };',
  15316. ' Sub();',
  15317. ' $mod.TObject.DoIt.apply(this, arguments);',
  15318. ' $mod.TObject.DoIt.call(this, true);',
  15319. ' return Result;',
  15320. ' };',
  15321. '});',
  15322. '']),
  15323. LinesToStr([ // $mod.$main
  15324. '']));
  15325. end;
  15326. procedure TTestModule.TestClass_TObjectFree;
  15327. begin
  15328. StartProgram(false);
  15329. Add([
  15330. 'type',
  15331. ' TObject = class',
  15332. ' Obj: tobject;',
  15333. ' procedure Free;',
  15334. ' procedure Release;',
  15335. ' end;',
  15336. 'procedure tobject.free;',
  15337. 'begin',
  15338. 'end;',
  15339. 'procedure tobject.release;',
  15340. 'begin',
  15341. ' free;',
  15342. ' if true then free;',
  15343. 'end;',
  15344. 'function DoIt(o: tobject): tobject;',
  15345. 'var l: tobject;',
  15346. 'begin',
  15347. ' o.free;',
  15348. ' o.free();',
  15349. ' l.free;',
  15350. ' l.free();',
  15351. ' o.obj.free;',
  15352. ' o.obj.free();',
  15353. ' with o do obj.free;',
  15354. ' with o do obj.free();',
  15355. ' result.Free;',
  15356. ' result.Free();',
  15357. 'end;',
  15358. 'var o: tobject;',
  15359. ' a: array of tobject;',
  15360. 'begin',
  15361. ' o.free;',
  15362. ' o.obj.free;',
  15363. ' a[1+2].free;',
  15364. '']);
  15365. ConvertProgram;
  15366. CheckSource('TestClass_TObjectFree',
  15367. LinesToStr([ // statements
  15368. 'rtl.createClass(this, "TObject", null, function () {',
  15369. ' this.$init = function () {',
  15370. ' this.Obj = null;',
  15371. ' };',
  15372. ' this.$final = function () {',
  15373. ' this.Obj = undefined;',
  15374. ' };',
  15375. ' this.Free = function () {',
  15376. ' };',
  15377. ' this.Release = function () {',
  15378. ' this.Free();',
  15379. ' if (true) this.Free();',
  15380. ' };',
  15381. '});',
  15382. 'this.DoIt = function (o) {',
  15383. ' var Result = null;',
  15384. ' var l = null;',
  15385. ' o = rtl.freeLoc(o);',
  15386. ' o = rtl.freeLoc(o);',
  15387. ' l = rtl.freeLoc(l);',
  15388. ' l = rtl.freeLoc(l);',
  15389. ' rtl.free(o, "Obj");',
  15390. ' rtl.free(o, "Obj");',
  15391. ' rtl.free(o, "Obj");',
  15392. ' rtl.free(o, "Obj");',
  15393. ' Result = rtl.freeLoc(Result);',
  15394. ' Result = rtl.freeLoc(Result);',
  15395. ' return Result;',
  15396. '};',
  15397. 'this.o = null;',
  15398. 'this.a = [];',
  15399. '']),
  15400. LinesToStr([ // $mod.$main
  15401. 'rtl.free($mod, "o");',
  15402. 'rtl.free($mod.o, "Obj");',
  15403. 'rtl.free($mod.a, 1 + 2);',
  15404. '']));
  15405. end;
  15406. procedure TTestModule.TestClass_TObjectFree_VarArg;
  15407. begin
  15408. StartProgram(false);
  15409. Add([
  15410. 'type',
  15411. ' TObject = class',
  15412. ' Obj: tobject;',
  15413. ' procedure Free;',
  15414. ' end;',
  15415. 'procedure tobject.free;',
  15416. 'begin',
  15417. 'end;',
  15418. 'procedure DoIt(var o: tobject);',
  15419. 'begin',
  15420. ' o.free;',
  15421. ' o.free();',
  15422. 'end;',
  15423. 'begin',
  15424. '']);
  15425. ConvertProgram;
  15426. CheckSource('TestClass_TObjectFree_VarArg',
  15427. LinesToStr([ // statements
  15428. 'rtl.createClass(this, "TObject", null, function () {',
  15429. ' this.$init = function () {',
  15430. ' this.Obj = null;',
  15431. ' };',
  15432. ' this.$final = function () {',
  15433. ' this.Obj = undefined;',
  15434. ' };',
  15435. ' this.Free = function () {',
  15436. ' };',
  15437. '});',
  15438. 'this.DoIt = function (o) {',
  15439. ' o.set(rtl.freeLoc(o.get()));',
  15440. ' o.set(rtl.freeLoc(o.get()));',
  15441. '};',
  15442. '']),
  15443. LinesToStr([ // $mod.$main
  15444. '']));
  15445. end;
  15446. procedure TTestModule.TestClass_TObjectFreeNewInstance;
  15447. begin
  15448. StartProgram(false);
  15449. Add([
  15450. 'type',
  15451. ' TObject = class',
  15452. ' constructor Create;',
  15453. ' procedure Free;',
  15454. ' end;',
  15455. 'constructor TObject.Create; begin end;',
  15456. 'procedure tobject.free; begin end;',
  15457. 'begin',
  15458. ' with tobject.create do free;',
  15459. '']);
  15460. ConvertProgram;
  15461. CheckSource('TestClass_TObjectFreeNewInstance',
  15462. LinesToStr([ // statements
  15463. 'rtl.createClass(this, "TObject", null, function () {',
  15464. ' this.$init = function () {',
  15465. ' };',
  15466. ' this.$final = function () {',
  15467. ' };',
  15468. ' this.Create = function () {',
  15469. ' return this;',
  15470. ' };',
  15471. ' this.Free = function () {',
  15472. ' };',
  15473. '});',
  15474. '']),
  15475. LinesToStr([ // $mod.$main
  15476. 'var $with = $mod.TObject.$create("Create");',
  15477. '$with=rtl.freeLoc($with);',
  15478. '']));
  15479. end;
  15480. procedure TTestModule.TestClass_TObjectFreeLowerCase;
  15481. begin
  15482. StartProgram(false);
  15483. Add([
  15484. 'type',
  15485. ' TObject = class',
  15486. ' destructor Destroy;',
  15487. ' procedure Free;',
  15488. ' end;',
  15489. 'destructor TObject.Destroy; begin end;',
  15490. 'procedure tobject.free; begin end;',
  15491. 'var o: tobject;',
  15492. 'begin',
  15493. ' o.free;',
  15494. '']);
  15495. Converter.UseLowerCase:=true;
  15496. ConvertProgram;
  15497. CheckSource('TestClass_TObjectFreeLowerCase',
  15498. LinesToStr([ // statements
  15499. 'rtl.createClass(this, "tobject", null, function () {',
  15500. ' this.$init = function () {',
  15501. ' };',
  15502. ' this.$final = function () {',
  15503. ' };',
  15504. ' rtl.tObjectDestroy = "destroy";',
  15505. ' this.destroy = function () {',
  15506. ' };',
  15507. ' this.free = function () {',
  15508. ' };',
  15509. '});',
  15510. 'this.o = null;',
  15511. '']),
  15512. LinesToStr([ // $mod.$main
  15513. 'rtl.free($mod, "o");',
  15514. '']));
  15515. end;
  15516. procedure TTestModule.TestClass_TObjectFreeFunctionFail;
  15517. begin
  15518. StartProgram(false);
  15519. Add([
  15520. 'type',
  15521. ' TObject = class',
  15522. ' procedure Free;',
  15523. ' function GetObj: tobject; virtual; abstract;',
  15524. ' end;',
  15525. 'procedure tobject.free;',
  15526. 'begin',
  15527. 'end;',
  15528. 'var o: tobject;',
  15529. 'begin',
  15530. ' o.getobj.free;',
  15531. '']);
  15532. SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
  15533. ConvertProgram;
  15534. end;
  15535. procedure TTestModule.TestClass_TObjectFreePropertyFail;
  15536. begin
  15537. StartProgram(false);
  15538. Add([
  15539. 'type',
  15540. ' TObject = class',
  15541. ' procedure Free;',
  15542. ' FObj: TObject;',
  15543. ' property Obj: tobject read FObj write FObj;',
  15544. ' end;',
  15545. 'procedure tobject.free;',
  15546. 'begin',
  15547. 'end;',
  15548. 'var o: tobject;',
  15549. 'begin',
  15550. ' o.obj.free;',
  15551. '']);
  15552. SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
  15553. ConvertProgram;
  15554. end;
  15555. procedure TTestModule.TestClass_ForIn;
  15556. begin
  15557. StartProgram(false);
  15558. Add([
  15559. 'type',
  15560. ' TObject = class end;',
  15561. ' TItem = TObject;',
  15562. ' TEnumerator = class',
  15563. ' FCurrent: TItem;',
  15564. ' property Current: TItem read FCurrent;',
  15565. ' function MoveNext: boolean;',
  15566. ' end;',
  15567. ' TBird = class',
  15568. ' function GetEnumerator: TEnumerator;',
  15569. ' end;',
  15570. 'function TEnumerator.MoveNext: boolean;',
  15571. 'begin',
  15572. 'end;',
  15573. 'function TBird.GetEnumerator: TEnumerator;',
  15574. 'begin',
  15575. 'end;',
  15576. 'var',
  15577. ' b: TBird;',
  15578. ' i, i2: TItem;',
  15579. 'begin',
  15580. ' for i in b do i2:=i;']);
  15581. ConvertProgram;
  15582. CheckSource('TestClass_ForIn',
  15583. LinesToStr([ // statements
  15584. 'rtl.createClass(this, "TObject", null, function () {',
  15585. ' this.$init = function () {',
  15586. ' };',
  15587. ' this.$final = function () {',
  15588. ' };',
  15589. '});',
  15590. 'rtl.createClass(this, "TEnumerator", this.TObject, function () {',
  15591. ' this.$init = function () {',
  15592. ' $mod.TObject.$init.call(this);',
  15593. ' this.FCurrent = null;',
  15594. ' };',
  15595. ' this.$final = function () {',
  15596. ' this.FCurrent = undefined;',
  15597. ' $mod.TObject.$final.call(this);',
  15598. ' };',
  15599. ' this.MoveNext = function () {',
  15600. ' var Result = false;',
  15601. ' return Result;',
  15602. ' };',
  15603. '});',
  15604. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15605. ' this.GetEnumerator = function () {',
  15606. ' var Result = null;',
  15607. ' return Result;',
  15608. ' };',
  15609. '});',
  15610. 'this.b = null;',
  15611. 'this.i = null;',
  15612. 'this.i2 = null;'
  15613. ]),
  15614. LinesToStr([ // $mod.$main
  15615. 'var $in = $mod.b.GetEnumerator();',
  15616. 'try {',
  15617. ' while ($in.MoveNext()){',
  15618. ' $mod.i = $in.FCurrent;',
  15619. ' $mod.i2 = $mod.i;',
  15620. ' }',
  15621. '} finally {',
  15622. ' $in = rtl.freeLoc($in)',
  15623. '};',
  15624. '']));
  15625. end;
  15626. procedure TTestModule.TestClass_DispatchMessage;
  15627. begin
  15628. StartProgram(false);
  15629. Add([
  15630. 'type',
  15631. ' TObject = class',
  15632. ' {$DispatchField DispInt}',
  15633. ' procedure Dispatch(var Msg); virtual; abstract;',
  15634. ' {$DispatchStrField DispStr}',
  15635. ' procedure DispatchStr(var Msg); virtual; abstract;',
  15636. ' end;',
  15637. ' THopMsg = record',
  15638. ' DispInt: longint;',
  15639. ' end;',
  15640. ' TPutMsg = record',
  15641. ' DispStr: string;',
  15642. ' end;',
  15643. ' TBird = class',
  15644. ' procedure Fly(var Msg); virtual; abstract; message 2;',
  15645. ' procedure Run; overload; virtual; abstract;',
  15646. ' procedure Run(var Msg); overload; message ''Fast'';',
  15647. ' procedure Hop(var Msg: THopMsg); virtual; abstract; message 3;',
  15648. ' procedure Put(var Msg: TPutMsg); virtual; abstract; message ''foo'';',
  15649. ' end;',
  15650. 'procedure TBird.Run(var Msg);',
  15651. 'begin',
  15652. 'end;',
  15653. 'begin',
  15654. '']);
  15655. ConvertProgram;
  15656. CheckSource('TestClass_Message',
  15657. LinesToStr([ // statements
  15658. 'rtl.createClass(this, "TObject", null, function () {',
  15659. ' this.$init = function () {',
  15660. ' };',
  15661. ' this.$final = function () {',
  15662. ' };',
  15663. '});',
  15664. 'rtl.recNewT(this, "THopMsg", function () {',
  15665. ' this.DispInt = 0;',
  15666. ' this.$eq = function (b) {',
  15667. ' return this.DispInt === b.DispInt;',
  15668. ' };',
  15669. ' this.$assign = function (s) {',
  15670. ' this.DispInt = s.DispInt;',
  15671. ' return this;',
  15672. ' };',
  15673. '});',
  15674. 'rtl.recNewT(this, "TPutMsg", function () {',
  15675. ' this.DispStr = "";',
  15676. ' this.$eq = function (b) {',
  15677. ' return this.DispStr === b.DispStr;',
  15678. ' };',
  15679. ' this.$assign = function (s) {',
  15680. ' this.DispStr = s.DispStr;',
  15681. ' return this;',
  15682. ' };',
  15683. '});',
  15684. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15685. ' this.Run$1 = function (Msg) {',
  15686. ' };',
  15687. ' this.$msgint = {',
  15688. ' "2": "Fly",',
  15689. ' "3": "Hop"',
  15690. ' };',
  15691. ' this.$msgstr = {',
  15692. ' Fast: "Run$1",',
  15693. ' foo: "Put"',
  15694. ' };',
  15695. '});',
  15696. '']),
  15697. LinesToStr([ // $mod.$main
  15698. '']));
  15699. end;
  15700. procedure TTestModule.TestClass_Message_DuplicateIntFail;
  15701. begin
  15702. StartProgram(false);
  15703. Add([
  15704. 'type',
  15705. ' TObject = class',
  15706. ' procedure Fly(var Msg); virtual; abstract; message 3;',
  15707. ' procedure Run(var Msg); virtual; abstract; message 1+2;',
  15708. ' end;',
  15709. 'begin',
  15710. '']);
  15711. SetExpectedPasResolverError('Duplicate message id "3" at test1.pp(5,56)',nDuplicateMessageIdXAtY);
  15712. ConvertProgram;
  15713. end;
  15714. procedure TTestModule.TestClass_DispatchMessage_WrongFieldNameFail;
  15715. begin
  15716. StartProgram(false);
  15717. Add([
  15718. 'type',
  15719. ' TObject = class',
  15720. ' {$dispatchfield Msg}',
  15721. ' procedure Dispatch(var Msg); virtual; abstract;',
  15722. ' end;',
  15723. ' TFlyMsg = record',
  15724. ' FlyId: longint;',
  15725. ' end;',
  15726. ' TBird = class',
  15727. ' procedure Fly(var Msg: TFlyMsg); virtual; abstract; message 3;',
  15728. ' end;',
  15729. 'begin',
  15730. '']);
  15731. ConvertProgram;
  15732. CheckHint(mtWarning,nDispatchRequiresX,'Dispatch requires record field "Msg"');
  15733. end;
  15734. procedure TTestModule.TestClassOf_Create;
  15735. begin
  15736. StartProgram(false);
  15737. Add('type');
  15738. Add(' TObject = class');
  15739. Add(' constructor Create;');
  15740. Add(' end;');
  15741. Add(' TClass = class of TObject;');
  15742. Add('constructor tobject.create; begin end;');
  15743. Add('var');
  15744. Add(' Obj: tobject;');
  15745. Add(' C: tclass;');
  15746. Add('begin');
  15747. Add(' obj:=C.create;');
  15748. Add(' with c do obj:=create;');
  15749. ConvertProgram;
  15750. CheckSource('TestClassOf_Create',
  15751. LinesToStr([ // statements
  15752. 'rtl.createClass(this, "TObject", null, function () {',
  15753. ' this.$init = function () {',
  15754. ' };',
  15755. ' this.$final = function () {',
  15756. ' };',
  15757. ' this.Create = function () {',
  15758. ' return this;',
  15759. ' };',
  15760. '});',
  15761. 'this.Obj = null;',
  15762. 'this.C = null;'
  15763. ]),
  15764. LinesToStr([ // $mod.$main
  15765. '$mod.Obj = $mod.C.$create("Create");',
  15766. 'var $with = $mod.C;',
  15767. '$mod.Obj = $with.$create("Create");',
  15768. '']));
  15769. end;
  15770. procedure TTestModule.TestClassOf_Call;
  15771. begin
  15772. StartProgram(false);
  15773. Add('type');
  15774. Add(' TObject = class');
  15775. Add(' class procedure DoIt;');
  15776. Add(' end;');
  15777. Add(' TClass = class of TObject;');
  15778. Add('class procedure tobject.doit; begin end;');
  15779. Add('var');
  15780. Add(' C: tclass;');
  15781. Add('begin');
  15782. Add(' c.doit;');
  15783. Add(' with c do doit;');
  15784. ConvertProgram;
  15785. CheckSource('TestClassOf_Call',
  15786. LinesToStr([ // statements
  15787. 'rtl.createClass(this, "TObject", null, function () {',
  15788. ' this.$init = function () {',
  15789. ' };',
  15790. ' this.$final = function () {',
  15791. ' };',
  15792. ' this.DoIt = function () {',
  15793. ' };',
  15794. '});',
  15795. 'this.C = null;'
  15796. ]),
  15797. LinesToStr([ // $mod.$main
  15798. '$mod.C.DoIt();',
  15799. 'var $with = $mod.C;',
  15800. '$with.DoIt();',
  15801. '']));
  15802. end;
  15803. procedure TTestModule.TestClassOf_Assign;
  15804. begin
  15805. StartProgram(false);
  15806. Add('type');
  15807. Add(' TClass = class of TObject;');
  15808. Add(' TObject = class');
  15809. Add(' ClassType: TClass; ');
  15810. Add(' end;');
  15811. Add('var');
  15812. Add(' Obj: tobject;');
  15813. Add(' C: tclass;');
  15814. Add('begin');
  15815. Add(' c:=nil;');
  15816. Add(' c:=obj.classtype;');
  15817. ConvertProgram;
  15818. CheckSource('TestClassOf_Assign',
  15819. LinesToStr([ // statements
  15820. 'rtl.createClass(this, "TObject", null, function () {',
  15821. ' this.$init = function () {',
  15822. ' this.ClassType = null;',
  15823. ' };',
  15824. ' this.$final = function () {',
  15825. ' this.ClassType = undefined;',
  15826. ' };',
  15827. '});',
  15828. 'this.Obj = null;',
  15829. 'this.C = null;'
  15830. ]),
  15831. LinesToStr([ // $mod.$main
  15832. '$mod.C = null;',
  15833. '$mod.C = $mod.Obj.ClassType;',
  15834. '']));
  15835. end;
  15836. procedure TTestModule.TestClassOf_Is;
  15837. begin
  15838. StartProgram(false);
  15839. Add('type');
  15840. Add(' TClass = class of TObject;');
  15841. Add(' TObject = class');
  15842. Add(' end;');
  15843. Add(' TCar = class');
  15844. Add(' end;');
  15845. Add(' TCars = class of TCar;');
  15846. Add('var');
  15847. Add(' Obj: tobject;');
  15848. Add(' C: tclass;');
  15849. Add(' Cars: tcars;');
  15850. Add('begin');
  15851. Add(' if c is tcar then ;');
  15852. Add(' if c is tcars then ;');
  15853. ConvertProgram;
  15854. CheckSource('TestClassOf_Is',
  15855. LinesToStr([ // statements
  15856. 'rtl.createClass(this, "TObject", null, function () {',
  15857. ' this.$init = function () {',
  15858. ' };',
  15859. ' this.$final = function () {',
  15860. ' };',
  15861. '});',
  15862. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  15863. '});',
  15864. 'this.Obj = null;',
  15865. 'this.C = null;',
  15866. 'this.Cars = null;'
  15867. ]),
  15868. LinesToStr([ // $mod.$main
  15869. 'if(rtl.is($mod.C,$mod.TCar));',
  15870. 'if(rtl.is($mod.C,$mod.TCar));',
  15871. '']));
  15872. end;
  15873. procedure TTestModule.TestClassOf_Compare;
  15874. begin
  15875. StartProgram(false);
  15876. Add('type');
  15877. Add(' TClass = class of TObject;');
  15878. Add(' TObject = class');
  15879. Add(' ClassType: TClass; ');
  15880. Add(' end;');
  15881. Add('var');
  15882. Add(' b: boolean;');
  15883. Add(' Obj: tobject;');
  15884. Add(' C: tclass;');
  15885. Add('begin');
  15886. Add(' b:=c=nil;');
  15887. Add(' b:=nil=c;');
  15888. Add(' b:=c=obj.classtype;');
  15889. Add(' b:=obj.classtype=c;');
  15890. Add(' b:=c=TObject;');
  15891. Add(' b:=TObject=c;');
  15892. Add(' b:=c<>nil;');
  15893. Add(' b:=nil<>c;');
  15894. Add(' b:=c<>obj.classtype;');
  15895. Add(' b:=obj.classtype<>c;');
  15896. Add(' b:=c<>TObject;');
  15897. Add(' b:=TObject<>c;');
  15898. ConvertProgram;
  15899. CheckSource('TestClassOf_Compare',
  15900. LinesToStr([ // statements
  15901. 'rtl.createClass(this, "TObject", null, function () {',
  15902. ' this.$init = function () {',
  15903. ' this.ClassType = null;',
  15904. ' };',
  15905. ' this.$final = function () {',
  15906. ' this.ClassType = undefined;',
  15907. ' };',
  15908. '});',
  15909. 'this.b = false;',
  15910. 'this.Obj = null;',
  15911. 'this.C = null;'
  15912. ]),
  15913. LinesToStr([ // $mod.$main
  15914. '$mod.b = $mod.C === null;',
  15915. '$mod.b = null === $mod.C;',
  15916. '$mod.b = $mod.C === $mod.Obj.ClassType;',
  15917. '$mod.b = $mod.Obj.ClassType === $mod.C;',
  15918. '$mod.b = $mod.C === $mod.TObject;',
  15919. '$mod.b = $mod.TObject === $mod.C;',
  15920. '$mod.b = $mod.C !== null;',
  15921. '$mod.b = null !== $mod.C;',
  15922. '$mod.b = $mod.C !== $mod.Obj.ClassType;',
  15923. '$mod.b = $mod.Obj.ClassType !== $mod.C;',
  15924. '$mod.b = $mod.C !== $mod.TObject;',
  15925. '$mod.b = $mod.TObject !== $mod.C;',
  15926. '']));
  15927. end;
  15928. procedure TTestModule.TestClassOf_ClassVar;
  15929. begin
  15930. StartProgram(false);
  15931. Add('type');
  15932. Add(' TObject = class');
  15933. Add(' class var id: longint;');
  15934. Add(' end;');
  15935. Add(' TClass = class of TObject;');
  15936. Add('var');
  15937. Add(' C: tclass;');
  15938. Add('begin');
  15939. Add(' C.id:=C.id;');
  15940. ConvertProgram;
  15941. CheckSource('TestClassOf_ClassVar',
  15942. LinesToStr([ // statements
  15943. 'rtl.createClass(this, "TObject", null, function () {',
  15944. ' this.id = 0;',
  15945. ' this.$init = function () {',
  15946. ' };',
  15947. ' this.$final = function () {',
  15948. ' };',
  15949. '});',
  15950. 'this.C = null;'
  15951. ]),
  15952. LinesToStr([ // $mod.$main
  15953. '$mod.TObject.id = $mod.C.id;',
  15954. '']));
  15955. end;
  15956. procedure TTestModule.TestClassOf_ClassMethod;
  15957. begin
  15958. StartProgram(false);
  15959. Add('type');
  15960. Add(' TObject = class');
  15961. Add(' class function DoIt(i: longint = 0): longint;');
  15962. Add(' end;');
  15963. Add(' TClass = class of TObject;');
  15964. Add('class function tobject.doit(i: longint = 0): longint; begin end;');
  15965. Add('var');
  15966. Add(' i: longint;');
  15967. Add(' C: tclass;');
  15968. Add('begin');
  15969. Add(' C.DoIt;');
  15970. Add(' C.DoIt();');
  15971. Add(' i:=C.DoIt;');
  15972. Add(' i:=C.DoIt();');
  15973. ConvertProgram;
  15974. CheckSource('TestClassOf_ClassMethod',
  15975. LinesToStr([ // statements
  15976. 'rtl.createClass(this, "TObject", null, function () {',
  15977. ' this.$init = function () {',
  15978. ' };',
  15979. ' this.$final = function () {',
  15980. ' };',
  15981. ' this.DoIt = function (i) {',
  15982. ' var Result = 0;',
  15983. ' return Result;',
  15984. ' };',
  15985. '});',
  15986. 'this.i = 0;',
  15987. 'this.C = null;'
  15988. ]),
  15989. LinesToStr([ // $mod.$main
  15990. '$mod.C.DoIt(0);',
  15991. '$mod.C.DoIt(0);',
  15992. '$mod.i = $mod.C.DoIt(0);',
  15993. '$mod.i = $mod.C.DoIt(0);',
  15994. '']));
  15995. end;
  15996. procedure TTestModule.TestClassOf_ClassProperty;
  15997. begin
  15998. StartProgram(false);
  15999. Add([
  16000. 'type',
  16001. ' TObject = class',
  16002. ' class var FA: longint;',
  16003. ' class function GetA: longint;',
  16004. ' class procedure SetA(Value: longint);',
  16005. ' class property pA: longint read fa write fa;',
  16006. ' class property pB: longint read geta write seta;',
  16007. ' end;',
  16008. ' TObjectClass = class of tobject;',
  16009. 'class function tobject.geta: longint; begin end;',
  16010. 'class procedure tobject.seta(value: longint); begin end;',
  16011. 'var',
  16012. ' b: boolean;',
  16013. ' Obj: tobject;',
  16014. ' Cla: tobjectclass;',
  16015. 'begin',
  16016. ' obj.pa:=obj.pa;',
  16017. ' obj.pb:=obj.pb;',
  16018. ' b:=obj.pa=4;',
  16019. ' b:=obj.pb=obj.pb;',
  16020. ' b:=5=obj.pa;',
  16021. ' cla.pa:=6;',
  16022. ' cla.pa:=cla.pa;',
  16023. ' cla.pb:=cla.pb;',
  16024. ' b:=cla.pa=7;',
  16025. ' b:=cla.pb=cla.pb;',
  16026. ' b:=8=cla.pa;',
  16027. ' tobject.pa:=9;',
  16028. ' tobject.pb:=tobject.pb;',
  16029. ' b:=tobject.pa=10;',
  16030. ' b:=11=tobject.pa;',
  16031. '']);
  16032. ConvertProgram;
  16033. CheckSource('TestClassOf_ClassProperty',
  16034. LinesToStr([ // statements
  16035. 'rtl.createClass(this, "TObject", null, function () {',
  16036. ' this.FA = 0;',
  16037. ' this.$init = function () {',
  16038. ' };',
  16039. ' this.$final = function () {',
  16040. ' };',
  16041. ' this.GetA = function () {',
  16042. ' var Result = 0;',
  16043. ' return Result;',
  16044. ' };',
  16045. ' this.SetA = function (Value) {',
  16046. ' };',
  16047. '});',
  16048. 'this.b = false;',
  16049. 'this.Obj = null;',
  16050. 'this.Cla = null;'
  16051. ]),
  16052. LinesToStr([ // $mod.$main
  16053. '$mod.TObject.FA = $mod.Obj.FA;',
  16054. '$mod.Obj.$class.SetA($mod.Obj.$class.GetA());',
  16055. '$mod.b = $mod.Obj.FA === 4;',
  16056. '$mod.b = $mod.Obj.$class.GetA() === $mod.Obj.$class.GetA();',
  16057. '$mod.b = 5 === $mod.Obj.FA;',
  16058. '$mod.TObject.FA = 6;',
  16059. '$mod.TObject.FA = $mod.Cla.FA;',
  16060. '$mod.Cla.SetA($mod.Cla.GetA());',
  16061. '$mod.b = $mod.Cla.FA === 7;',
  16062. '$mod.b = $mod.Cla.GetA() === $mod.Cla.GetA();',
  16063. '$mod.b = 8 === $mod.Cla.FA;',
  16064. '$mod.TObject.FA = 9;',
  16065. '$mod.TObject.SetA($mod.TObject.GetA());',
  16066. '$mod.b = $mod.TObject.FA === 10;',
  16067. '$mod.b = 11 === $mod.TObject.FA;',
  16068. '']));
  16069. end;
  16070. procedure TTestModule.TestClassOf_ClassMethodSelf;
  16071. begin
  16072. StartProgram(false);
  16073. Add('type');
  16074. Add(' TObject = class');
  16075. Add(' class var GlobalId: longint;');
  16076. Add(' class procedure ProcA;');
  16077. Add(' end;');
  16078. Add('class procedure tobject.proca;');
  16079. Add('var b: boolean;');
  16080. Add('begin');
  16081. Add(' b:=self=nil;');
  16082. Add(' b:=self.globalid=3;');
  16083. Add(' b:=4=self.globalid;');
  16084. Add(' self.globalid:=5;');
  16085. Add(' self.proca;');
  16086. Add('end;');
  16087. Add('begin');
  16088. ConvertProgram;
  16089. CheckSource('TestClassOf_ClassMethodSelf',
  16090. LinesToStr([ // statements
  16091. 'rtl.createClass(this, "TObject", null, function () {',
  16092. ' this.GlobalId = 0;',
  16093. ' this.$init = function () {',
  16094. ' };',
  16095. ' this.$final = function () {',
  16096. ' };',
  16097. ' this.ProcA = function () {',
  16098. ' var b = false;',
  16099. ' b = this === null;',
  16100. ' b = this.GlobalId === 3;',
  16101. ' b = 4 === this.GlobalId;',
  16102. ' $mod.TObject.GlobalId = 5;',
  16103. ' this.ProcA();',
  16104. ' };',
  16105. '});'
  16106. ]),
  16107. LinesToStr([ // $mod.$main
  16108. '']));
  16109. end;
  16110. procedure TTestModule.TestClassOf_TypeCast;
  16111. begin
  16112. StartProgram(false);
  16113. Add('type');
  16114. Add(' TObject = class');
  16115. Add(' class procedure {#TObject_DoIt}DoIt;');
  16116. Add(' end;');
  16117. Add(' TClass = class of TObject;');
  16118. Add(' TMobile = class');
  16119. Add(' class procedure {#TMobile_DoIt}DoIt;');
  16120. Add(' end;');
  16121. Add(' TMobileClass = class of TMobile;');
  16122. Add(' TCar = class(TMobile)');
  16123. Add(' class procedure {#TCar_DoIt}DoIt;');
  16124. Add(' end;');
  16125. Add(' TCarClass = class of TCar;');
  16126. Add('class procedure TObject.DoIt;');
  16127. Add('begin');
  16128. Add(' TClass(Self).{@TObject_DoIt}DoIt;');
  16129. Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
  16130. Add('end;');
  16131. Add('class procedure TMobile.DoIt;');
  16132. Add('begin');
  16133. Add(' TClass(Self).{@TObject_DoIt}DoIt;');
  16134. Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
  16135. Add(' TCarClass(Self).{@TCar_DoIt}DoIt;');
  16136. Add('end;');
  16137. Add('class procedure TCar.DoIt; begin end;');
  16138. Add('var');
  16139. Add(' ObjC: TClass;');
  16140. Add(' MobileC: TMobileClass;');
  16141. Add(' CarC: TCarClass;');
  16142. Add('begin');
  16143. Add(' ObjC.{@TObject_DoIt}DoIt;');
  16144. Add(' MobileC.{@TMobile_DoIt}DoIt;');
  16145. Add(' CarC.{@TCar_DoIt}DoIt;');
  16146. Add(' TClass(ObjC).{@TObject_DoIt}DoIt;');
  16147. Add(' TMobileClass(ObjC).{@TMobile_DoIt}DoIt;');
  16148. Add(' TCarClass(ObjC).{@TCar_DoIt}DoIt;');
  16149. Add(' TClass(MobileC).{@TObject_DoIt}DoIt;');
  16150. Add(' TMobileClass(MobileC).{@TMobile_DoIt}DoIt;');
  16151. Add(' TCarClass(MobileC).{@TCar_DoIt}DoIt;');
  16152. Add(' TClass(CarC).{@TObject_DoIt}DoIt;');
  16153. Add(' TMobileClass(CarC).{@TMobile_DoIt}DoIt;');
  16154. Add(' TCarClass(CarC).{@TCar_DoIt}DoIt;');
  16155. ConvertProgram;
  16156. CheckSource('TestClassOf_TypeCast',
  16157. LinesToStr([ // statements
  16158. 'rtl.createClass(this, "TObject", null, function () {',
  16159. ' this.$init = function () {',
  16160. ' };',
  16161. ' this.$final = function () {',
  16162. ' };',
  16163. ' this.DoIt = function () {',
  16164. ' this.DoIt();',
  16165. ' this.DoIt$1();',
  16166. ' };',
  16167. '});',
  16168. 'rtl.createClass(this, "TMobile", this.TObject, function () {',
  16169. ' this.DoIt$1 = function () {',
  16170. ' this.DoIt();',
  16171. ' this.DoIt$1();',
  16172. ' this.DoIt$2();',
  16173. ' };',
  16174. '});',
  16175. 'rtl.createClass(this, "TCar", this.TMobile, function () {',
  16176. ' this.DoIt$2 = function () {',
  16177. ' };',
  16178. '});',
  16179. 'this.ObjC = null;',
  16180. 'this.MobileC = null;',
  16181. 'this.CarC = null;',
  16182. '']),
  16183. LinesToStr([ // $mod.$main
  16184. '$mod.ObjC.DoIt();',
  16185. '$mod.MobileC.DoIt$1();',
  16186. '$mod.CarC.DoIt$2();',
  16187. '$mod.ObjC.DoIt();',
  16188. '$mod.ObjC.DoIt$1();',
  16189. '$mod.ObjC.DoIt$2();',
  16190. '$mod.MobileC.DoIt();',
  16191. '$mod.MobileC.DoIt$1();',
  16192. '$mod.MobileC.DoIt$2();',
  16193. '$mod.CarC.DoIt();',
  16194. '$mod.CarC.DoIt$1();',
  16195. '$mod.CarC.DoIt$2();',
  16196. '']));
  16197. end;
  16198. procedure TTestModule.TestClassOf_ImplicitFunctionCall;
  16199. begin
  16200. StartProgram(false);
  16201. Add('type');
  16202. Add(' TObject = class');
  16203. Add(' function CurNow: longint; ');
  16204. Add(' class function Now: longint; ');
  16205. Add(' end;');
  16206. Add('function TObject.CurNow: longint; begin end;');
  16207. Add('class function TObject.Now: longint; begin end;');
  16208. Add('var');
  16209. Add(' Obj: tobject;');
  16210. Add(' vI: longint;');
  16211. Add('begin');
  16212. Add(' obj.curnow;');
  16213. Add(' vi:=obj.curnow;');
  16214. Add(' tobject.now;');
  16215. Add(' vi:=tobject.now;');
  16216. ConvertProgram;
  16217. CheckSource('TestClassOf_ImplicitFunctionCall',
  16218. LinesToStr([ // statements
  16219. 'rtl.createClass(this, "TObject", null, function () {',
  16220. ' this.$init = function () {',
  16221. ' };',
  16222. ' this.$final = function () {',
  16223. ' };',
  16224. ' this.CurNow = function () {',
  16225. ' var Result = 0;',
  16226. ' return Result;',
  16227. ' };',
  16228. ' this.Now = function () {',
  16229. ' var Result = 0;',
  16230. ' return Result;',
  16231. ' };',
  16232. '});',
  16233. 'this.Obj = null;',
  16234. 'this.vI = 0;',
  16235. '']),
  16236. LinesToStr([ // $mod.$main
  16237. '$mod.Obj.CurNow();',
  16238. '$mod.vI = $mod.Obj.CurNow();',
  16239. '$mod.TObject.Now();',
  16240. '$mod.vI = $mod.TObject.Now();',
  16241. '']));
  16242. end;
  16243. procedure TTestModule.TestClassOf_Const;
  16244. begin
  16245. StartProgram(false);
  16246. Add([
  16247. 'type',
  16248. ' TObject = class',
  16249. ' end;',
  16250. ' TBird = TObject;',
  16251. ' TBirds = class of TBird;',
  16252. ' TEagles = TBirds;',
  16253. ' THawk = class(TBird);',
  16254. 'const',
  16255. ' Hawk: TEagles = THawk;',
  16256. ' DefaultBirdClasses : Array [1..2] of TEagles = (',
  16257. ' TBird,',
  16258. ' THawk',
  16259. ' );',
  16260. 'begin']);
  16261. ConvertProgram;
  16262. CheckSource('TestClassOf_Const',
  16263. LinesToStr([ // statements
  16264. 'rtl.createClass(this, "TObject", null, function () {',
  16265. ' this.$init = function () {',
  16266. ' };',
  16267. ' this.$final = function () {',
  16268. ' };',
  16269. '});',
  16270. 'rtl.createClass(this, "THawk", this.TObject, function () {',
  16271. '});',
  16272. 'this.Hawk = this.THawk;',
  16273. 'this.DefaultBirdClasses = [this.TObject, this.THawk];',
  16274. '']),
  16275. LinesToStr([ // $mod.$main
  16276. '']));
  16277. end;
  16278. procedure TTestModule.TestNestedClass_Alias;
  16279. begin
  16280. WithTypeInfo:=true;
  16281. StartProgram(false);
  16282. Add([
  16283. 'type',
  16284. ' TObject = class',
  16285. ' type TNested = type longint;',
  16286. ' end;',
  16287. 'type TAlias = type tobject.tnested;',
  16288. 'var i: tobject.tnested = 3;',
  16289. 'var j: TAlias = 4;',
  16290. 'begin',
  16291. ' if typeinfo(TAlias)=nil then ;',
  16292. ' if typeinfo(tobject.tnested)=nil then ;',
  16293. '']);
  16294. ConvertProgram;
  16295. CheckSource('TestNestedClass_Alias',
  16296. LinesToStr([ // statements
  16297. 'rtl.createClass(this, "TObject", null, function () {',
  16298. ' $mod.$rtti.$inherited("TObject.TNested", rtl.longint, {});',
  16299. ' this.$init = function () {',
  16300. ' };',
  16301. ' this.$final = function () {',
  16302. ' };',
  16303. '});',
  16304. 'this.$rtti.$inherited("TAlias", this.$rtti["TObject.TNested"], {});',
  16305. 'this.i = 3;',
  16306. 'this.j = 4;',
  16307. '']),
  16308. LinesToStr([ // $mod.$main
  16309. 'if ($mod.$rtti["TAlias"] === null) ;',
  16310. 'if ($mod.$rtti["TObject.TNested"] === null) ;',
  16311. '']));
  16312. end;
  16313. procedure TTestModule.TestNestedClass_Record;
  16314. begin
  16315. WithTypeInfo:=true;
  16316. StartProgram(false);
  16317. Add([
  16318. 'type',
  16319. ' TObject = class',
  16320. ' type TPoint = record',
  16321. ' x,y: byte;',
  16322. ' end;',
  16323. ' procedure DoIt(t: TPoint);',
  16324. ' end;',
  16325. 'procedure tobject.DoIt(t: TPoint);',
  16326. 'var p: TPoint;',
  16327. 'begin',
  16328. ' t.x:=t.y;',
  16329. ' p:=t;',
  16330. 'end;',
  16331. 'var',
  16332. ' p: tobject.tpoint = (x:2; y:4);',
  16333. ' o: TObject;',
  16334. 'begin',
  16335. ' p:=p;',
  16336. ' o.doit(p);',
  16337. '']);
  16338. ConvertProgram;
  16339. CheckSource('TestNestedClass_Record',
  16340. LinesToStr([ // statements
  16341. 'rtl.createClass(this, "TObject", null, function () {',
  16342. ' rtl.recNewT(this, "TPoint", function () {',
  16343. ' this.x = 0;',
  16344. ' this.y = 0;',
  16345. ' this.$eq = function (b) {',
  16346. ' return (this.x === b.x) && (this.y === b.y);',
  16347. ' };',
  16348. ' this.$assign = function (s) {',
  16349. ' this.x = s.x;',
  16350. ' this.y = s.y;',
  16351. ' return this;',
  16352. ' };',
  16353. ' var $r = $mod.$rtti.$Record("TObject.TPoint", {});',
  16354. ' $r.addField("x", rtl.byte);',
  16355. ' $r.addField("y", rtl.byte);',
  16356. ' });',
  16357. ' this.$init = function () {',
  16358. ' };',
  16359. ' this.$final = function () {',
  16360. ' };',
  16361. ' this.DoIt = function (t) {',
  16362. ' var p = this.TPoint.$new();',
  16363. ' t.x = t.y;',
  16364. ' p.$assign(t);',
  16365. ' };',
  16366. '});',
  16367. 'this.p = this.TObject.TPoint.$clone({',
  16368. ' x: 2,',
  16369. ' y: 4',
  16370. '});',
  16371. 'this.o = null;',
  16372. '']),
  16373. LinesToStr([ // $mod.$main
  16374. '$mod.p.$assign($mod.p);',
  16375. '$mod.o.DoIt($mod.TObject.TPoint.$clone($mod.p));',
  16376. '']));
  16377. end;
  16378. procedure TTestModule.TestNestedClass_Class;
  16379. begin
  16380. WithTypeInfo:=true;
  16381. StartProgram(false);
  16382. Add([
  16383. 'type',
  16384. ' TObject = class end;',
  16385. ' TBird = class',
  16386. ' type TLeg = class',
  16387. ' FId: longint;',
  16388. ' constructor Create;',
  16389. ' function Create(i: longint): TLeg;',
  16390. ' end;',
  16391. ' function DoIt(b: TBird): Tleg;',
  16392. ' end;',
  16393. 'constructor tbird.tleg.create;',
  16394. 'begin',
  16395. ' FId:=3;',
  16396. 'end;',
  16397. 'function tbird.tleg.Create(i: longint): TLeg;',
  16398. 'begin',
  16399. ' Create;',
  16400. ' Result:=TLeg.Create;',
  16401. ' Result:=TBird.TLeg.Create;',
  16402. ' Result:=Create(3);',
  16403. ' FId:=i;',
  16404. 'end;',
  16405. 'function tbird.DoIt(b: tbird): tleg;',
  16406. 'begin',
  16407. ' Result.Create;',
  16408. ' Result:=TLeg.Create;',
  16409. ' Result:=TBird.TLeg.Create;',
  16410. ' Result:=Result.Create(3);',
  16411. 'end;',
  16412. 'var',
  16413. ' b: Tbird.tleg;',
  16414. 'begin',
  16415. ' b.Create;',
  16416. ' b:=TBird.TLeg.Create;',
  16417. ' b:=b.Create(3);',
  16418. '']);
  16419. ConvertProgram;
  16420. CheckSource('TestNestedClass_Class',
  16421. LinesToStr([ // statements
  16422. 'rtl.createClass(this, "TObject", null, function () {',
  16423. ' this.$init = function () {',
  16424. ' };',
  16425. ' this.$final = function () {',
  16426. ' };',
  16427. '});',
  16428. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16429. ' rtl.createClass(this, "TLeg", $mod.TObject, function () {',
  16430. ' this.$init = function () {',
  16431. ' $mod.TObject.$init.call(this);',
  16432. ' this.FId = 0;',
  16433. ' };',
  16434. ' this.Create = function () {',
  16435. ' this.FId = 3;',
  16436. ' return this;',
  16437. ' };',
  16438. ' this.Create$1 = function (i) {',
  16439. ' var Result = null;',
  16440. ' this.Create();',
  16441. ' Result = $mod.TBird.TLeg.$create("Create");',
  16442. ' Result = $mod.TBird.TLeg.$create("Create");',
  16443. ' Result = this.Create$1(3);',
  16444. ' this.FId = i;',
  16445. ' return Result;',
  16446. ' };',
  16447. ' }, "TBird.TLeg");',
  16448. ' this.DoIt = function (b) {',
  16449. ' var Result = null;',
  16450. ' Result.Create();',
  16451. ' Result = this.TLeg.$create("Create");',
  16452. ' Result = $mod.TBird.TLeg.$create("Create");',
  16453. ' Result = Result.Create$1(3);',
  16454. ' return Result;',
  16455. ' };',
  16456. '});',
  16457. 'this.b = null;',
  16458. '']),
  16459. LinesToStr([ // $mod.$main
  16460. '$mod.b.Create();',
  16461. '$mod.b = $mod.TBird.TLeg.$create("Create");',
  16462. '$mod.b = $mod.b.Create$1(3);',
  16463. '']));
  16464. end;
  16465. procedure TTestModule.TestExternalClass_Var;
  16466. begin
  16467. StartProgram(false);
  16468. Add([
  16469. '{$modeswitch externalclass}',
  16470. 'type',
  16471. ' TExtA = class external name ''ExtObj''',
  16472. ' Id: longint external name ''$Id'';',
  16473. ' B: longint;',
  16474. ' end;',
  16475. 'var Obj: TExtA;',
  16476. 'begin',
  16477. ' obj.id:=obj.id+1;',
  16478. ' obj.B:=obj.B+1;']);
  16479. ConvertProgram;
  16480. CheckSource('TestExternalClass_Var',
  16481. LinesToStr([ // statements
  16482. 'this.Obj = null;',
  16483. '']),
  16484. LinesToStr([ // $mod.$main
  16485. '$mod.Obj.$Id = $mod.Obj.$Id + 1;',
  16486. '$mod.Obj.B = $mod.Obj.B + 1;',
  16487. '']));
  16488. end;
  16489. procedure TTestModule.TestExternalClass_Const;
  16490. begin
  16491. StartProgram(false);
  16492. Add([
  16493. '{$modeswitch externalclass}',
  16494. 'type',
  16495. ' TExtA = class external name ''ExtObj''',
  16496. ' const Two: longint = 2;',
  16497. ' const Three = 3;',
  16498. ' const Id: longint;',
  16499. ' end;',
  16500. ' TExtB = class external name ''ExtB''',
  16501. ' A: TExtA;',
  16502. ' end;',
  16503. 'var',
  16504. ' A: texta;',
  16505. ' B: textb;',
  16506. ' i: longint;',
  16507. 'begin',
  16508. ' i:=a.two;',
  16509. ' i:=texta.two;',
  16510. ' i:=a.three;',
  16511. ' i:=texta.three;',
  16512. ' i:=a.id;',
  16513. ' i:=texta.id;',
  16514. '']);
  16515. ConvertProgram;
  16516. CheckSource('TestExternalClass_Const',
  16517. LinesToStr([ // statements
  16518. 'this.A = null;',
  16519. 'this.B = null;',
  16520. 'this.i = 0;',
  16521. '']),
  16522. LinesToStr([ // $mod.$main
  16523. '$mod.i = 2;',
  16524. '$mod.i = 2;',
  16525. '$mod.i = 3;',
  16526. '$mod.i = 3;',
  16527. '$mod.i = $mod.A.Id;',
  16528. '$mod.i = ExtObj.Id;',
  16529. '']));
  16530. end;
  16531. procedure TTestModule.TestExternalClass_Dollar;
  16532. begin
  16533. StartProgram(false);
  16534. Add([
  16535. '{$modeswitch externalclass}',
  16536. 'type',
  16537. ' TExtA = class external name ''$''',
  16538. ' Id: longint external name ''$'';',
  16539. ' function Bla(i: longint): longint; external name ''$'';',
  16540. ' end;',
  16541. 'function dollar(k: longint): longint; external name ''$'';',
  16542. 'var Obj: TExtA;',
  16543. 'begin',
  16544. ' dollar(1);',
  16545. ' obj.id:=obj.id+2;',
  16546. ' obj.Bla(3);',
  16547. '']);
  16548. ConvertProgram;
  16549. CheckSource('TestExternalClass_Dollar',
  16550. LinesToStr([ // statements
  16551. 'this.Obj = null;',
  16552. '']),
  16553. LinesToStr([ // $mod.$main
  16554. '$(1);',
  16555. '$mod.Obj.$ = $mod.Obj.$ + 2;',
  16556. '$mod.Obj.$(3);',
  16557. '']));
  16558. end;
  16559. procedure TTestModule.TestExternalClass_DuplicateVarFail;
  16560. begin
  16561. StartProgram(false);
  16562. Add('{$modeswitch externalclass}');
  16563. Add('type');
  16564. Add(' TExtA = class external name ''ExtA''');
  16565. Add(' Id: longint external name ''$Id'';');
  16566. Add(' end;');
  16567. Add(' TExtB = class external ''lib'' name ''ExtB''(TExtA)');
  16568. Add(' Id: longint;');
  16569. Add(' end;');
  16570. Add('begin');
  16571. SetExpectedPasResolverError('Duplicate identifier "Id" at test1.pp(6,5)',nDuplicateIdentifier);
  16572. ConvertProgram;
  16573. end;
  16574. procedure TTestModule.TestExternalClass_Method;
  16575. begin
  16576. StartProgram(false);
  16577. Add(['{$modeswitch externalclass}',
  16578. 'type',
  16579. ' TExtA = class external name ''ExtObj''',
  16580. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  16581. ' procedure DoSome(Id: longint = 1);',
  16582. ' end;',
  16583. 'var Obj: texta;',
  16584. 'begin',
  16585. ' obj.doit;',
  16586. ' obj.doit();',
  16587. ' obj.doit(2);',
  16588. ' with obj do begin',
  16589. ' doit;',
  16590. ' doit();',
  16591. ' doit(3);',
  16592. ' end;']);
  16593. ConvertProgram;
  16594. CheckSource('TestExternalClass_Method',
  16595. LinesToStr([ // statements
  16596. 'this.Obj = null;',
  16597. '']),
  16598. LinesToStr([ // $mod.$main
  16599. '$mod.Obj.$Execute(1);',
  16600. '$mod.Obj.$Execute(1);',
  16601. '$mod.Obj.$Execute(2);',
  16602. 'var $with = $mod.Obj;',
  16603. '$with.$Execute(1);',
  16604. '$with.$Execute(1);',
  16605. '$with.$Execute(3);',
  16606. '']));
  16607. end;
  16608. procedure TTestModule.TestExternalClass_ClassMethod;
  16609. begin
  16610. StartProgram(false);
  16611. Add([
  16612. '{$modeswitch externalclass}',
  16613. 'type',
  16614. ' TExtA = class external name ''ExtObj''',
  16615. ' class procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  16616. ' end;',
  16617. ' TExtB = TExtA;',
  16618. 'var p: Pointer;',
  16619. 'begin',
  16620. ' texta.doit;',
  16621. ' texta.doit();',
  16622. ' texta.doit(2);',
  16623. ' p:[email protected];',
  16624. ' with texta do begin',
  16625. ' doit;',
  16626. ' doit();',
  16627. ' doit(3);',
  16628. ' p:=@DoIt;',
  16629. ' end;',
  16630. ' textb.doit;',
  16631. ' textb.doit();',
  16632. ' textb.doit(4);',
  16633. ' with textb do begin',
  16634. ' doit;',
  16635. ' doit();',
  16636. ' doit(5);',
  16637. ' end;',
  16638. '']);
  16639. ConvertProgram;
  16640. CheckSource('TestExternalClass_ClassMethod',
  16641. LinesToStr([ // statements
  16642. 'this.p = null;',
  16643. '']),
  16644. LinesToStr([ // $mod.$main
  16645. 'ExtObj.$Execute(1);',
  16646. 'ExtObj.$Execute(1);',
  16647. 'ExtObj.$Execute(2);',
  16648. '$mod.p = rtl.createCallback(ExtObj, "$Execute");',
  16649. 'ExtObj.$Execute(1);',
  16650. 'ExtObj.$Execute(1);',
  16651. 'ExtObj.$Execute(3);',
  16652. '$mod.p = rtl.createCallback(ExtObj, "$Execute");',
  16653. 'ExtObj.$Execute(1);',
  16654. 'ExtObj.$Execute(1);',
  16655. 'ExtObj.$Execute(4);',
  16656. 'ExtObj.$Execute(1);',
  16657. 'ExtObj.$Execute(1);',
  16658. 'ExtObj.$Execute(5);',
  16659. '']));
  16660. end;
  16661. procedure TTestModule.TestExternalClass_ClassMethodStatic;
  16662. begin
  16663. StartProgram(false);
  16664. Add([
  16665. '{$modeswitch externalclass}',
  16666. 'type',
  16667. ' TExtA = class external name ''ExtObj''',
  16668. ' class procedure DoIt(Id: longint = 1); static;',
  16669. ' end;',
  16670. 'var p: Pointer;',
  16671. 'begin',
  16672. ' texta.doit;',
  16673. ' texta.doit();',
  16674. ' texta.doit(2);',
  16675. ' p:[email protected];',
  16676. ' with texta do begin',
  16677. ' doit;',
  16678. ' doit();',
  16679. ' doit(3);',
  16680. ' p:=@DoIt;',
  16681. ' end;',
  16682. '']);
  16683. ConvertProgram;
  16684. CheckSource('TestExternalClass_ClassMethodStatic',
  16685. LinesToStr([ // statements
  16686. 'this.p = null;',
  16687. '']),
  16688. LinesToStr([ // $mod.$main
  16689. 'ExtObj.DoIt(1);',
  16690. 'ExtObj.DoIt(1);',
  16691. 'ExtObj.DoIt(2);',
  16692. '$mod.p = ExtObj.DoIt;',
  16693. 'ExtObj.DoIt(1);',
  16694. 'ExtObj.DoIt(1);',
  16695. 'ExtObj.DoIt(3);',
  16696. '$mod.p = ExtObj.DoIt;',
  16697. '']));
  16698. end;
  16699. procedure TTestModule.TestExternalClass_FunctionResultInTypeCast;
  16700. begin
  16701. StartProgram(false);
  16702. Add([
  16703. '{$modeswitch externalclass}',
  16704. 'type',
  16705. ' TBird = class external name ''Array''',
  16706. ' end;',
  16707. 'function GetPtr: Pointer;',
  16708. 'begin',
  16709. 'end;',
  16710. 'procedure Write(const p);',
  16711. 'begin',
  16712. 'end;',
  16713. 'procedure WriteLn; varargs;',
  16714. 'begin',
  16715. 'end;',
  16716. 'begin',
  16717. ' if TBird(GetPtr)=nil then ;',
  16718. ' Write(GetPtr);',
  16719. ' WriteLn(GetPtr);',
  16720. ' Write(TBird(GetPtr));',
  16721. ' WriteLn(TBird(GetPtr));',
  16722. '']);
  16723. ConvertProgram;
  16724. CheckSource('TestFunctionResultInTypeCast',
  16725. LinesToStr([ // statements
  16726. 'this.GetPtr = function () {',
  16727. ' var Result = null;',
  16728. ' return Result;',
  16729. '};',
  16730. 'this.Write = function (p) {',
  16731. '};',
  16732. 'this.WriteLn = function () {',
  16733. '};',
  16734. '']),
  16735. LinesToStr([
  16736. 'if ($mod.GetPtr() === null) ;',
  16737. '$mod.Write($mod.GetPtr());',
  16738. '$mod.WriteLn($mod.GetPtr());',
  16739. '$mod.Write($mod.GetPtr());',
  16740. '$mod.WriteLn($mod.GetPtr());',
  16741. '']));
  16742. end;
  16743. procedure TTestModule.TestExternalClass_NonExternalOverride;
  16744. begin
  16745. StartProgram(false);
  16746. Add([
  16747. '{$modeswitch externalclass}',
  16748. 'type',
  16749. ' TExtA = class external name ''ExtObjA''',
  16750. ' procedure ProcA; virtual;',
  16751. ' procedure ProcB; virtual;',
  16752. ' end;',
  16753. ' TExtB = class external name ''ExtObjB'' (TExtA)',
  16754. ' end;',
  16755. ' TExtC = class (TExtB)',
  16756. ' procedure ProcA; override;',
  16757. ' end;',
  16758. 'procedure TExtC.ProcA;',
  16759. 'begin',
  16760. ' ProcA;',
  16761. ' Self.ProcA;',
  16762. ' ProcB;',
  16763. ' Self.ProcB;',
  16764. 'end;',
  16765. 'var',
  16766. ' A: texta;',
  16767. ' B: textb;',
  16768. ' C: textc;',
  16769. 'begin',
  16770. ' a.proca;',
  16771. ' b.proca;',
  16772. ' c.proca;']);
  16773. ConvertProgram;
  16774. CheckSource('TestExternalClass_NonExternalOverride',
  16775. LinesToStr([ // statements
  16776. 'rtl.createClassExt(this, "TExtC", ExtObjB, "", function () {',
  16777. ' this.$init = function () {',
  16778. ' };',
  16779. ' this.$final = function () {',
  16780. ' };',
  16781. ' this.ProcA = function () {',
  16782. ' this.ProcA();',
  16783. ' this.ProcA();',
  16784. ' this.ProcB();',
  16785. ' this.ProcB();',
  16786. ' };',
  16787. '});',
  16788. 'this.A = null;',
  16789. 'this.B = null;',
  16790. 'this.C = null;',
  16791. '']),
  16792. LinesToStr([ // $mod.$main
  16793. '$mod.A.ProcA();',
  16794. '$mod.B.ProcA();',
  16795. '$mod.C.ProcA();',
  16796. '']));
  16797. end;
  16798. procedure TTestModule.TestExternalClass_OverloadHint;
  16799. begin
  16800. StartProgram(false);
  16801. Add([
  16802. '{$modeswitch externalclass}',
  16803. 'type',
  16804. ' TExtA = class external name ''ExtObjA''',
  16805. ' procedure DoIt;',
  16806. ' procedure DoIt(i: longint);',
  16807. ' end;',
  16808. 'begin',
  16809. '']);
  16810. ConvertProgram;
  16811. CheckResolverUnexpectedHints(true);
  16812. CheckSource('TestExternalClass_OverloadHint',
  16813. LinesToStr([ // statements
  16814. '']),
  16815. LinesToStr([ // $mod.$main
  16816. '']));
  16817. end;
  16818. procedure TTestModule.TestExternalClass_SameNamePublishedProperty;
  16819. begin
  16820. WithTypeInfo:=true;
  16821. StartProgram(false);
  16822. Add([
  16823. '{$modeswitch externalclass}',
  16824. 'type',
  16825. ' JSwiper = class external name ''Swiper''',
  16826. ' constructor New;',
  16827. ' end;',
  16828. ' TObject = class',
  16829. ' private',
  16830. ' FSwiper: JSwiper;',
  16831. ' published',
  16832. ' property Swiper: JSwiper read FSwiper write FSwiper;',
  16833. ' end;',
  16834. 'begin',
  16835. ' JSwiper.new;',
  16836. '']);
  16837. ConvertProgram;
  16838. CheckSource('TestExternalClass_SameNamePublishedProperty',
  16839. LinesToStr([ // statements
  16840. 'this.$rtti.$ExtClass("JSwiper", {',
  16841. ' jsclass: "Swiper"',
  16842. '});',
  16843. 'rtl.createClass(this, "TObject", null, function () {',
  16844. ' this.$init = function () {',
  16845. ' this.FSwiper = null;',
  16846. ' };',
  16847. ' this.$final = function () {',
  16848. ' this.FSwiper = undefined;',
  16849. ' };',
  16850. ' var $r = this.$rtti;',
  16851. ' $r.addProperty("Swiper", 0, $mod.$rtti["JSwiper"], "FSwiper", "FSwiper");',
  16852. '});',
  16853. '']),
  16854. LinesToStr([ // $mod.$main
  16855. 'new Swiper();',
  16856. '']));
  16857. end;
  16858. procedure TTestModule.TestExternalClass_Property;
  16859. begin
  16860. StartProgram(false);
  16861. Add([
  16862. '{$modeswitch externalclass}',
  16863. 'type',
  16864. ' TExtA = class external name ''ExtA''',
  16865. ' function getYear: longint;',
  16866. ' procedure setYear(Value: longint);',
  16867. ' property Year: longint read getyear write setyear;',
  16868. ' end;',
  16869. ' TExtB = class (TExtA)',
  16870. ' procedure OtherSetYear(Value: longint);',
  16871. ' property year write othersetyear;',
  16872. ' end;',
  16873. 'procedure textb.othersetyear(value: longint);',
  16874. 'begin',
  16875. ' setYear(Value+4);',
  16876. 'end;',
  16877. 'var',
  16878. ' A: texta;',
  16879. ' B: textb;',
  16880. 'begin',
  16881. ' a.year:=a.year+1;',
  16882. ' b.year:=b.year+2;']);
  16883. ConvertProgram;
  16884. CheckSource('TestExternalClass_NonExternalOverride',
  16885. LinesToStr([ // statements
  16886. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  16887. ' this.$init = function () {',
  16888. ' };',
  16889. ' this.$final = function () {',
  16890. ' };',
  16891. ' this.OtherSetYear = function (Value) {',
  16892. ' this.setYear(Value+4);',
  16893. ' };',
  16894. '});',
  16895. 'this.A = null;',
  16896. 'this.B = null;',
  16897. '']),
  16898. LinesToStr([ // $mod.$main
  16899. '$mod.A.setYear($mod.A.getYear()+1);',
  16900. '$mod.B.OtherSetYear($mod.B.getYear()+2);',
  16901. '']));
  16902. end;
  16903. procedure TTestModule.TestExternalClass_PropertyDate;
  16904. begin
  16905. StartProgram(false);
  16906. Add([
  16907. '{$modeswitch externalclass}',
  16908. 'type',
  16909. ' TExtA = class external name ''ExtA''',
  16910. ' end;',
  16911. ' TExtB = class (TExtA)',
  16912. ' FDate: string;',
  16913. ' property Date: string read FDate write FDate;',
  16914. ' property ExtA: string read FDate write FDate;',
  16915. ' end;',
  16916. ' {$M+}',
  16917. ' TObject = class',
  16918. ' FDate: string;',
  16919. ' published',
  16920. ' property Date: string read FDate write FDate;',
  16921. ' property ExtA: string read FDate write FDate;',
  16922. ' end;',
  16923. 'var',
  16924. ' B: textb;',
  16925. ' o: TObject;',
  16926. 'begin',
  16927. ' b.date:=b.exta;',
  16928. ' o.date:=o.exta;']);
  16929. ConvertProgram;
  16930. CheckSource('TestExternalClass_PropertyDate',
  16931. LinesToStr([ // statements
  16932. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  16933. ' this.$init = function () {',
  16934. ' this.FDate = "";',
  16935. ' };',
  16936. ' this.$final = function () {',
  16937. ' };',
  16938. '});',
  16939. 'rtl.createClass(this, "TObject", null, function () {',
  16940. ' this.$init = function () {',
  16941. ' this.FDate = "";',
  16942. ' };',
  16943. ' this.$final = function () {',
  16944. ' };',
  16945. ' var $r = this.$rtti;',
  16946. ' $r.addField("FDate", rtl.string);',
  16947. ' $r.addProperty("Date", 0, rtl.string, "FDate", "FDate");',
  16948. ' $r.addProperty("ExtA", 0, rtl.string, "FDate", "FDate");',
  16949. '});',
  16950. 'this.B = null;',
  16951. 'this.o = null;',
  16952. '']),
  16953. LinesToStr([ // $mod.$main
  16954. '$mod.B.FDate = $mod.B.FDate;',
  16955. '$mod.o.FDate = $mod.o.FDate;',
  16956. '']));
  16957. end;
  16958. procedure TTestModule.TestExternalClass_ClassProperty;
  16959. begin
  16960. StartProgram(false);
  16961. Add('{$modeswitch externalclass}');
  16962. Add('type');
  16963. Add(' TExtA = class external name ''ExtA''');
  16964. Add(' class function getYear: longint;');
  16965. Add(' class procedure setYear(Value: longint);');
  16966. Add(' class property Year: longint read getyear write setyear;');
  16967. Add(' end;');
  16968. Add(' TExtB = class (TExtA)');
  16969. Add(' class function GetCentury: longint;');
  16970. Add(' class procedure SetCentury(Value: longint);');
  16971. Add(' class property Century: longint read getcentury write setcentury;');
  16972. Add(' end;');
  16973. Add('class function textb.getcentury: longint;');
  16974. Add('begin');
  16975. Add('end;');
  16976. Add('class procedure textb.setcentury(value: longint);');
  16977. Add('begin');
  16978. Add(' setyear(value+11);');
  16979. Add(' texta.year:=texta.year+12;');
  16980. Add(' year:=year+13;');
  16981. Add(' textb.century:=textb.century+14;');
  16982. Add(' century:=century+15;');
  16983. Add('end;');
  16984. Add('var');
  16985. Add(' A: texta;');
  16986. Add(' B: textb;');
  16987. Add('begin');
  16988. Add(' texta.year:=texta.year+1;');
  16989. Add(' textb.year:=textb.year+2;');
  16990. Add(' TextA.year:=TextA.year+3;');
  16991. Add(' b.year:=b.year+4;');
  16992. Add(' textb.century:=textb.century+5;');
  16993. Add(' b.century:=b.century+6;');
  16994. ConvertProgram;
  16995. CheckSource('TestExternalClass_ClassProperty',
  16996. LinesToStr([ // statements
  16997. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  16998. ' this.$init = function () {',
  16999. ' };',
  17000. ' this.$final = function () {',
  17001. ' };',
  17002. ' this.GetCentury = function () {',
  17003. ' var Result = 0;',
  17004. ' return Result;',
  17005. ' };',
  17006. ' this.SetCentury = function (Value) {',
  17007. ' this.setYear(Value + 11);',
  17008. ' ExtA.setYear(ExtA.getYear() + 12);',
  17009. ' this.setYear(this.getYear() + 13);',
  17010. ' $mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 14);',
  17011. ' this.SetCentury(this.GetCentury() + 15);',
  17012. ' };',
  17013. '});',
  17014. 'this.A = null;',
  17015. 'this.B = null;',
  17016. '']),
  17017. LinesToStr([ // $mod.$main
  17018. 'ExtA.setYear(ExtA.getYear() + 1);',
  17019. '$mod.TExtB.setYear($mod.TExtB.getYear() + 2);',
  17020. 'ExtA.setYear(ExtA.getYear() + 3);',
  17021. '$mod.B.setYear($mod.B.getYear() + 4);',
  17022. '$mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 5);',
  17023. '$mod.B.$class.SetCentury($mod.B.$class.GetCentury() + 6);',
  17024. '']));
  17025. end;
  17026. procedure TTestModule.TestExternalClass_ClassOf;
  17027. begin
  17028. StartProgram(false);
  17029. Add('{$modeswitch externalclass}');
  17030. Add('type');
  17031. Add(' TExtA = class external name ''ExtA''');
  17032. Add(' procedure ProcA; virtual;');
  17033. Add(' procedure ProcB; virtual;');
  17034. Add(' end;');
  17035. Add(' TExtAClass = class of TExtA;');
  17036. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  17037. Add(' end;');
  17038. Add(' TExtBClass = class of TExtB;');
  17039. Add(' TExtC = class (TExtB)');
  17040. Add(' procedure ProcA; override;');
  17041. Add(' end;');
  17042. Add(' TExtCClass = class of TExtC;');
  17043. Add('procedure TExtC.ProcA; begin end;');
  17044. Add('var');
  17045. Add(' A: texta; ClA: TExtAClass;');
  17046. Add(' B: textb; ClB: TExtBClass;');
  17047. Add(' C: textc; ClC: TExtCClass;');
  17048. Add('begin');
  17049. Add(' ClA:=texta;');
  17050. Add(' ClA:=textb;');
  17051. Add(' ClA:=textc;');
  17052. Add(' ClB:=textb;');
  17053. Add(' ClB:=textc;');
  17054. Add(' ClC:=textc;');
  17055. ConvertProgram;
  17056. CheckSource('TestExternalClass_ClassOf',
  17057. LinesToStr([ // statements
  17058. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  17059. ' this.$init = function () {',
  17060. ' };',
  17061. ' this.$final = function () {',
  17062. ' };',
  17063. ' this.ProcA = function () {',
  17064. ' };',
  17065. '});',
  17066. 'this.A = null;',
  17067. 'this.ClA = null;',
  17068. 'this.B = null;',
  17069. 'this.ClB = null;',
  17070. 'this.C = null;',
  17071. 'this.ClC = null;',
  17072. '']),
  17073. LinesToStr([ // $mod.$main
  17074. '$mod.ClA = ExtA;',
  17075. '$mod.ClA = ExtB;',
  17076. '$mod.ClA = $mod.TExtC;',
  17077. '$mod.ClB = ExtB;',
  17078. '$mod.ClB = $mod.TExtC;',
  17079. '$mod.ClC = $mod.TExtC;',
  17080. '']));
  17081. end;
  17082. procedure TTestModule.TestExternalClass_ClassOtherUnit;
  17083. begin
  17084. AddModuleWithIntfImplSrc('unit2.pas',
  17085. LinesToStr([
  17086. '{$modeswitch externalclass}',
  17087. 'type',
  17088. ' TExtA = class external name ''ExtA''',
  17089. ' class var Id: longint;',
  17090. ' end;',
  17091. '']),
  17092. '');
  17093. StartUnit(true);
  17094. Add('interface');
  17095. Add('uses unit2;');
  17096. Add('implementation');
  17097. Add('begin');
  17098. Add(' unit2.texta.id:=unit2.texta.id+1;');
  17099. ConvertUnit;
  17100. CheckSource('TestExternalClass_ClassOtherUnit',
  17101. LinesToStr([
  17102. '']),
  17103. LinesToStr([
  17104. 'ExtA.Id = ExtA.Id + 1;',
  17105. '']));
  17106. end;
  17107. procedure TTestModule.TestExternalClass_Is;
  17108. begin
  17109. StartProgram(false);
  17110. Add([
  17111. '{$modeswitch externalclass}',
  17112. 'type',
  17113. ' TExtA = class external name ''ExtA''',
  17114. ' end;',
  17115. ' TExtAClass = class of TExtA;',
  17116. ' TExtB = class external name ''ExtB'' (TExtA)',
  17117. ' end;',
  17118. ' TExtBClass = class of TExtB;',
  17119. ' TExtC = class (TExtB)',
  17120. ' end;',
  17121. ' TExtCClass = class of TExtC;',
  17122. 'var',
  17123. ' A: texta; ClA: TExtAClass;',
  17124. ' B: textb; ClB: TExtBClass;',
  17125. ' C: textc; ClC: TExtCClass;',
  17126. 'begin',
  17127. ' if a is textb then ;',
  17128. ' if a is textc then ;',
  17129. ' if b is textc then ;',
  17130. ' if cla is textb then ;',
  17131. ' if cla is textc then ;',
  17132. ' if clb is textc then ;',
  17133. ' try',
  17134. ' except',
  17135. ' on TExtA do ;',
  17136. ' on e: TExtB do ;',
  17137. ' end;',
  17138. '']);
  17139. ConvertProgram;
  17140. CheckSource('TestExternalClass_Is',
  17141. LinesToStr([ // statements
  17142. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  17143. ' this.$init = function () {',
  17144. ' };',
  17145. ' this.$final = function () {',
  17146. ' };',
  17147. '});',
  17148. 'this.A = null;',
  17149. 'this.ClA = null;',
  17150. 'this.B = null;',
  17151. 'this.ClB = null;',
  17152. 'this.C = null;',
  17153. 'this.ClC = null;',
  17154. '']),
  17155. LinesToStr([ // $mod.$main
  17156. 'if (rtl.isExt($mod.A, ExtB)) ;',
  17157. 'if ($mod.TExtC.isPrototypeOf($mod.A)) ;',
  17158. 'if ($mod.TExtC.isPrototypeOf($mod.B)) ;',
  17159. 'if (rtl.isExt($mod.ClA, ExtB)) ;',
  17160. 'if (rtl.is($mod.ClA, $mod.TExtC)) ;',
  17161. 'if (rtl.is($mod.ClB, $mod.TExtC)) ;',
  17162. 'try {} catch ($e) {',
  17163. ' if (rtl.isExt($e,ExtA)) {}',
  17164. ' else if (rtl.isExt($e,ExtB)) {',
  17165. ' var e = $e;',
  17166. ' } else throw $e',
  17167. '};',
  17168. '']));
  17169. end;
  17170. procedure TTestModule.TestExternalClass_As;
  17171. begin
  17172. StartProgram(false);
  17173. Add('{$modeswitch externalclass}');
  17174. Add('type');
  17175. Add(' TExtA = class external name ''ExtA''');
  17176. Add(' end;');
  17177. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  17178. Add(' end;');
  17179. Add(' TExtC = class (TExtB)');
  17180. Add(' end;');
  17181. Add('var');
  17182. Add(' A: texta;');
  17183. Add(' B: textb;');
  17184. Add(' C: textc;');
  17185. Add('begin');
  17186. Add(' b:=a as textb;');
  17187. Add(' c:=a as textc;');
  17188. Add(' c:=b as textc;');
  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.B = null;',
  17200. 'this.C = null;',
  17201. '']),
  17202. LinesToStr([ // $mod.$main
  17203. '$mod.B = rtl.asExt($mod.A, ExtB);',
  17204. '$mod.C = rtl.as($mod.A, $mod.TExtC);',
  17205. '$mod.C = rtl.as($mod.B, $mod.TExtC);',
  17206. '']));
  17207. end;
  17208. procedure TTestModule.TestExternalClass_DestructorFail;
  17209. begin
  17210. StartProgram(false);
  17211. Add('{$modeswitch externalclass}');
  17212. Add('type');
  17213. Add(' TExtA = class external name ''ExtA''');
  17214. Add(' destructor Free;');
  17215. Add(' end;');
  17216. SetExpectedPasResolverError('Pascal element not supported: destructor',
  17217. nPasElementNotSupported);
  17218. ConvertProgram;
  17219. end;
  17220. procedure TTestModule.TestExternalClass_New;
  17221. begin
  17222. StartProgram(false);
  17223. Add([
  17224. '{$modeswitch externalclass}',
  17225. 'type',
  17226. ' TExtA = class external name ''ExtA''',
  17227. ' constructor New;',
  17228. ' constructor New(i: longint; j: longint = 2);',
  17229. ' end;',
  17230. 'var',
  17231. ' A: texta;',
  17232. 'begin',
  17233. ' a:=texta.new;',
  17234. ' a:=texta(texta.new);',
  17235. ' a:=texta.new();',
  17236. ' a:=texta.new(1);',
  17237. ' with texta do begin',
  17238. ' a:=new;',
  17239. ' a:=new();',
  17240. ' a:=new(2);',
  17241. ' end;',
  17242. ' a:=test1.texta.new;',
  17243. ' a:=test1.texta.new();',
  17244. ' a:=test1.texta.new(3);',
  17245. '']);
  17246. ConvertProgram;
  17247. CheckSource('TestExternalClass_New',
  17248. LinesToStr([ // statements
  17249. 'this.A = null;',
  17250. '']),
  17251. LinesToStr([ // $mod.$main
  17252. '$mod.A = new ExtA();',
  17253. '$mod.A = new ExtA();',
  17254. '$mod.A = new ExtA();',
  17255. '$mod.A = new ExtA(1,2);',
  17256. '$mod.A = new ExtA();',
  17257. '$mod.A = new ExtA();',
  17258. '$mod.A = new ExtA(2,2);',
  17259. '$mod.A = new ExtA();',
  17260. '$mod.A = new ExtA();',
  17261. '$mod.A = new ExtA(3,2);',
  17262. '']));
  17263. end;
  17264. procedure TTestModule.TestExternalClass_ClassOf_New;
  17265. begin
  17266. StartProgram(false);
  17267. Add('{$modeswitch externalclass}');
  17268. Add('type');
  17269. Add(' TExtAClass = class of TExtA;');
  17270. Add(' TExtA = class external name ''ExtA''');
  17271. Add(' C: TExtAClass;');
  17272. Add(' constructor New;');
  17273. Add(' end;');
  17274. Add('var');
  17275. Add(' A: texta;');
  17276. Add(' C: textaclass;');
  17277. Add('begin');
  17278. Add(' a:=c.new;');
  17279. Add(' a:=c.new();');
  17280. Add(' with C do begin');
  17281. Add(' a:=new;');
  17282. Add(' a:=new();');
  17283. Add(' end;');
  17284. Add(' a:=test1.c.new;');
  17285. Add(' a:=test1.c.new();');
  17286. Add(' a:=A.c.new();');
  17287. ConvertProgram;
  17288. CheckSource('TestExternalClass_ClassOf_New',
  17289. LinesToStr([ // statements
  17290. 'this.A = null;',
  17291. 'this.C = null;',
  17292. '']),
  17293. LinesToStr([ // $mod.$main
  17294. '$mod.A = new $mod.C();',
  17295. '$mod.A = new $mod.C();',
  17296. 'var $with = $mod.C;',
  17297. '$mod.A = new $with();',
  17298. '$mod.A = new $with();',
  17299. '$mod.A = new $mod.C();',
  17300. '$mod.A = new $mod.C();',
  17301. '$mod.A = new $mod.A.C();',
  17302. '']));
  17303. end;
  17304. procedure TTestModule.TestExternalClass_FuncClassOf_New;
  17305. begin
  17306. StartProgram(false);
  17307. Add([
  17308. '{$modeswitch externalclass}',
  17309. 'type',
  17310. ' TExtAClass = class of TExtA;',
  17311. ' TExtA = class external name ''ExtA''',
  17312. ' constructor New;',
  17313. ' end;',
  17314. 'function GetCreator: TExtAClass;',
  17315. 'begin',
  17316. ' Result:=TExtA;',
  17317. 'end;',
  17318. 'var',
  17319. ' A: texta;',
  17320. 'begin',
  17321. ' a:=getcreator.new;',
  17322. ' a:=getcreator().new;',
  17323. ' a:=getcreator().new();',
  17324. ' a:=getcreator.new();',
  17325. ' with getcreator do begin',
  17326. ' a:=new;',
  17327. ' a:=new();',
  17328. ' end;']);
  17329. ConvertProgram;
  17330. CheckSource('TestExternalClass_FuncClassOf_New',
  17331. LinesToStr([ // statements
  17332. 'this.GetCreator = function () {',
  17333. ' var Result = null;',
  17334. ' Result = ExtA;',
  17335. ' return Result;',
  17336. '};',
  17337. 'this.A = null;',
  17338. '']),
  17339. LinesToStr([ // $mod.$main
  17340. '$mod.A = new ($mod.GetCreator())();',
  17341. '$mod.A = new ($mod.GetCreator())();',
  17342. '$mod.A = new ($mod.GetCreator())();',
  17343. '$mod.A = new ($mod.GetCreator())();',
  17344. 'var $with = $mod.GetCreator();',
  17345. '$mod.A = new $with();',
  17346. '$mod.A = new $with();',
  17347. '']));
  17348. end;
  17349. procedure TTestModule.TestExternalClass_New_PasClassFail;
  17350. begin
  17351. StartProgram(false);
  17352. Add([
  17353. '{$modeswitch externalclass}',
  17354. 'type',
  17355. ' TExtA = class external name ''ExtA''',
  17356. ' constructor New;',
  17357. ' end;',
  17358. ' TBird = class(TExtA)',
  17359. ' end;',
  17360. 'begin',
  17361. ' TBird.new;',
  17362. '']);
  17363. SetExpectedPasResolverError(sJSNewNotSupported,nJSNewNotSupported);
  17364. ConvertProgram;
  17365. end;
  17366. procedure TTestModule.TestExternalClass_New_PasClassBracketsFail;
  17367. begin
  17368. StartProgram(false);
  17369. Add([
  17370. '{$modeswitch externalclass}',
  17371. 'type',
  17372. ' TExtA = class external name ''ExtA''',
  17373. ' constructor New;',
  17374. ' end;',
  17375. ' TBird = class(TExtA)',
  17376. ' end;',
  17377. 'begin',
  17378. ' TBird.new();',
  17379. '']);
  17380. SetExpectedPasResolverError(sJSNewNotSupported,nJSNewNotSupported);
  17381. ConvertProgram;
  17382. end;
  17383. procedure TTestModule.TestExternalClass_NewExtName;
  17384. begin
  17385. StartProgram(false);
  17386. Add([
  17387. '{$modeswitch externalclass}',
  17388. 'type',
  17389. ' TExtA = class external name ''ExtA''',
  17390. ' constructor New; external name ''Other'';',
  17391. ' constructor New(i: longint; j: longint = 2); external name ''A.B'';',
  17392. ' end;',
  17393. 'var',
  17394. ' A: texta;',
  17395. 'begin',
  17396. ' a:=texta.new;',
  17397. ' a:=texta(texta.new);',
  17398. ' a:=texta.new();',
  17399. ' a:=texta.new(1);',
  17400. ' with texta do begin',
  17401. ' a:=new;',
  17402. ' a:=new();',
  17403. ' a:=new(2);',
  17404. ' end;',
  17405. ' a:=test1.texta.new;',
  17406. ' a:=test1.texta.new();',
  17407. ' a:=test1.texta.new(3);',
  17408. '']);
  17409. ConvertProgram;
  17410. CheckSource('TestExternalClass_NewExtName',
  17411. LinesToStr([ // statements
  17412. 'this.A = null;',
  17413. '']),
  17414. LinesToStr([ // $mod.$main
  17415. '$mod.A = new Other();',
  17416. '$mod.A = new Other();',
  17417. '$mod.A = new Other();',
  17418. '$mod.A = new A.B(1,2);',
  17419. '$mod.A = new Other();',
  17420. '$mod.A = new Other();',
  17421. '$mod.A = new A.B(2,2);',
  17422. '$mod.A = new Other();',
  17423. '$mod.A = new Other();',
  17424. '$mod.A = new A.B(3,2);',
  17425. '']));
  17426. end;
  17427. procedure TTestModule.TestExternalClass_Constructor;
  17428. begin
  17429. StartProgram(false);
  17430. Add([
  17431. '{$modeswitch externalclass}',
  17432. 'type',
  17433. ' TExtA = class external name ''ExtA''',
  17434. ' constructor Create;',
  17435. ' constructor Create(i: longint; j: longint = 2);',
  17436. ' end;',
  17437. 'var',
  17438. ' A: texta;',
  17439. 'begin',
  17440. ' a:=texta.create;',
  17441. ' a:=texta(texta.create);',
  17442. ' a:=texta.create();',
  17443. ' a:=texta.create(1);',
  17444. ' with texta do begin',
  17445. ' a:=create;',
  17446. ' a:=create();',
  17447. ' a:=create(2);',
  17448. ' end;',
  17449. ' a:=test1.texta.create;',
  17450. ' a:=test1.texta.create();',
  17451. ' a:=test1.texta.create(3);',
  17452. '']);
  17453. ConvertProgram;
  17454. CheckSource('TestExternalClass_Constructor',
  17455. LinesToStr([ // statements
  17456. 'this.A = null;',
  17457. '']),
  17458. LinesToStr([ // $mod.$main
  17459. '$mod.A = new ExtA.Create();',
  17460. '$mod.A = new ExtA.Create();',
  17461. '$mod.A = new ExtA.Create();',
  17462. '$mod.A = new ExtA.Create(1,2);',
  17463. '$mod.A = new ExtA.Create();',
  17464. '$mod.A = new ExtA.Create();',
  17465. '$mod.A = new ExtA.Create(2,2);',
  17466. '$mod.A = new ExtA.Create();',
  17467. '$mod.A = new ExtA.Create();',
  17468. '$mod.A = new ExtA.Create(3,2);',
  17469. '']));
  17470. end;
  17471. procedure TTestModule.TestExternalClass_ConstructorBrackets;
  17472. begin
  17473. StartProgram(false);
  17474. Add([
  17475. '{$modeswitch externalclass}',
  17476. 'type',
  17477. ' TExtA = class external name ''ExtA''',
  17478. ' constructor Create; external name ''{}'';',
  17479. ' end;',
  17480. 'var',
  17481. ' A: texta;',
  17482. 'begin',
  17483. ' a:=texta.create;',
  17484. ' a:=texta(texta.create);',
  17485. ' a:=texta.create();',
  17486. ' with texta do begin',
  17487. ' a:=create;',
  17488. ' a:=create();',
  17489. ' end;',
  17490. ' a:=test1.texta.create;',
  17491. ' a:=test1.texta.create();',
  17492. '']);
  17493. ConvertProgram;
  17494. CheckSource('TestExternalClass_ConstructorBrackets',
  17495. LinesToStr([ // statements
  17496. 'this.A = null;',
  17497. '']),
  17498. LinesToStr([ // $mod.$main
  17499. '$mod.A = {};',
  17500. '$mod.A = {};',
  17501. '$mod.A = {};',
  17502. '$mod.A = {};',
  17503. '$mod.A = {};',
  17504. '$mod.A = {};',
  17505. '$mod.A = {};',
  17506. '']));
  17507. end;
  17508. procedure TTestModule.TestExternalClass_LocalConstSameName;
  17509. begin
  17510. StartProgram(false);
  17511. Add('{$modeswitch externalclass}');
  17512. Add('type');
  17513. Add(' TExtA = class external name ''ExtA''');
  17514. Add(' constructor New;');
  17515. Add(' end;');
  17516. Add('function DoIt: longint;');
  17517. Add('const ExtA: longint = 3;');
  17518. Add('begin');
  17519. Add(' Result:=ExtA;');
  17520. Add('end;');
  17521. Add('var');
  17522. Add(' A: texta;');
  17523. Add('begin');
  17524. Add(' a:=texta.new;');
  17525. ConvertProgram;
  17526. CheckSource('TestExternalClass_LocalConstSameName',
  17527. LinesToStr([ // statements
  17528. 'var ExtA$1 = 3;',
  17529. 'this.DoIt = function () {',
  17530. ' var Result = 0;',
  17531. ' Result = ExtA$1;',
  17532. ' return Result;',
  17533. '};',
  17534. 'this.A = null;',
  17535. '']),
  17536. LinesToStr([ // $mod.$main
  17537. '$mod.A = new ExtA();',
  17538. '']));
  17539. end;
  17540. procedure TTestModule.TestExternalClass_ReintroduceOverload;
  17541. begin
  17542. StartProgram(false);
  17543. Add('{$modeswitch externalclass}');
  17544. Add('type');
  17545. Add(' TExtA = class external name ''ExtA''');
  17546. Add(' procedure DoIt;');
  17547. Add(' end;');
  17548. Add(' TMyA = class(TExtA)');
  17549. Add(' procedure DoIt;');
  17550. Add(' end;');
  17551. Add('procedure TMyA.DoIt; begin end;');
  17552. Add('begin');
  17553. ConvertProgram;
  17554. CheckSource('TestExternalClass_ReintroduceOverload',
  17555. LinesToStr([ // statements
  17556. 'rtl.createClassExt(this, "TMyA", ExtA, "", function () {',
  17557. ' this.$init = function () {',
  17558. ' };',
  17559. ' this.$final = function () {',
  17560. ' };',
  17561. ' this.DoIt$1 = function () {',
  17562. ' };',
  17563. '});',
  17564. '']),
  17565. LinesToStr([ // $mod.$main
  17566. '']));
  17567. end;
  17568. procedure TTestModule.TestExternalClass_Inherited;
  17569. begin
  17570. StartProgram(false);
  17571. Add('{$modeswitch externalclass}');
  17572. Add('type');
  17573. Add(' TExtA = class external name ''ExtA''');
  17574. Add(' procedure DoIt(i: longint = 1); virtual;');
  17575. Add(' procedure DoSome(j: longint = 2);');
  17576. Add(' end;');
  17577. Add(' TExtB = class external name ''ExtB''(TExtA)');
  17578. Add(' end;');
  17579. Add(' TMyC = class(TExtB)');
  17580. Add(' procedure DoIt(i: longint = 1); override;');
  17581. Add(' procedure DoSome(j: longint = 2); reintroduce;');
  17582. Add(' end;');
  17583. Add('procedure TMyC.DoIt(i: longint);');
  17584. Add('begin');
  17585. Add(' inherited;');
  17586. Add(' inherited DoIt;');
  17587. Add(' inherited DoIt();');
  17588. Add(' inherited DoIt(3);');
  17589. Add(' inherited DoSome;');
  17590. Add(' inherited DoSome();');
  17591. Add(' inherited DoSome(4);');
  17592. Add('end;');
  17593. Add('procedure TMyC.DoSome(j: longint);');
  17594. Add('begin');
  17595. Add(' inherited;');
  17596. Add('end;');
  17597. Add('begin');
  17598. ConvertProgram;
  17599. CheckSource('TestExternalClass_ReintroduceOverload',
  17600. LinesToStr([ // statements
  17601. 'rtl.createClassExt(this, "TMyC", ExtB, "", function () {',
  17602. ' this.$init = function () {',
  17603. ' };',
  17604. ' this.$final = function () {',
  17605. ' };',
  17606. ' this.DoIt = function (i) {',
  17607. ' ExtB.DoIt.apply(this, arguments);',
  17608. ' ExtB.DoIt.call(this, 1);',
  17609. ' ExtB.DoIt.call(this, 1);',
  17610. ' ExtB.DoIt.call(this, 3);',
  17611. ' ExtB.DoSome.call(this, 2);',
  17612. ' ExtB.DoSome.call(this, 2);',
  17613. ' ExtB.DoSome.call(this, 4);',
  17614. ' };',
  17615. ' this.DoSome$1 = function (j) {',
  17616. ' ExtB.DoSome.apply(this, arguments);',
  17617. ' };',
  17618. '});',
  17619. '']),
  17620. LinesToStr([ // $mod.$main
  17621. '']));
  17622. end;
  17623. procedure TTestModule.TestExternalClass_PascalAncestorFail;
  17624. begin
  17625. StartProgram(false);
  17626. Add('{$modeswitch externalclass}');
  17627. Add('type');
  17628. Add(' TObject = class');
  17629. Add(' end;');
  17630. Add(' TExtA = class external name ''ExtA''(TObject)');
  17631. Add(' end;');
  17632. Add('begin');
  17633. SetExpectedPasResolverError('Ancestor "TObject" is not external',nAncestorIsNotExternal);
  17634. ConvertProgram;
  17635. end;
  17636. procedure TTestModule.TestExternalClass_NewInstance;
  17637. begin
  17638. StartProgram(false);
  17639. Add('{$modeswitch externalclass}');
  17640. Add('type');
  17641. Add(' TExtA = class external name ''ExtA''');
  17642. Add(' end;');
  17643. Add(' TMyB = class(TExtA)');
  17644. Add(' protected');
  17645. Add(' class function NewInstance(fnname: string; const paramarray): TMyB; virtual;');
  17646. Add(' end;');
  17647. Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
  17648. Add('begin end;');
  17649. Add('begin');
  17650. ConvertProgram;
  17651. CheckSource('TestExternalClass_NewInstance',
  17652. LinesToStr([ // statements
  17653. 'rtl.createClassExt(this, "TMyB", ExtA, "NewInstance", function () {',
  17654. ' this.$init = function () {',
  17655. ' };',
  17656. ' this.$final = function () {',
  17657. ' };',
  17658. ' this.NewInstance = function (fnname, paramarray) {',
  17659. ' var Result = null;',
  17660. ' return Result;',
  17661. ' };',
  17662. '});',
  17663. '']),
  17664. LinesToStr([ // $mod.$main
  17665. '']));
  17666. end;
  17667. procedure TTestModule.TestExternalClass_NewInstance_NonVirtualFail;
  17668. begin
  17669. StartProgram(false);
  17670. Add('{$modeswitch externalclass}');
  17671. Add('type');
  17672. Add(' TExtA = class external name ''ExtA''');
  17673. Add(' end;');
  17674. Add(' TMyB = class(TExtA)');
  17675. Add(' protected');
  17676. Add(' class function NewInstance(fnname: string; const paramarray): TMyB;');
  17677. Add(' end;');
  17678. Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
  17679. Add('begin end;');
  17680. Add('begin');
  17681. SetExpectedPasResolverError(sNewInstanceFunctionMustBeVirtual,nNewInstanceFunctionMustBeVirtual);
  17682. ConvertProgram;
  17683. end;
  17684. procedure TTestModule.TestExternalClass_NewInstance_FirstParamNotString_Fail;
  17685. begin
  17686. StartProgram(false);
  17687. Add('{$modeswitch externalclass}');
  17688. Add('type');
  17689. Add(' TExtA = class external name ''ExtA''');
  17690. Add(' end;');
  17691. Add(' TMyB = class(TExtA)');
  17692. Add(' protected');
  17693. Add(' class function NewInstance(fnname: longint; const paramarray): TMyB; virtual;');
  17694. Add(' end;');
  17695. Add('class function TMyB.NewInstance(fnname: longint; const paramarray): TMyB;');
  17696. Add('begin end;');
  17697. Add('begin');
  17698. SetExpectedPasResolverError('Incompatible type arg no. 1: Got "Longint", expected "String"',
  17699. nIncompatibleTypeArgNo);
  17700. ConvertProgram;
  17701. end;
  17702. procedure TTestModule.TestExternalClass_NewInstance_SecondParamTyped_Fail;
  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: string): TMyB; virtual;');
  17712. Add(' end;');
  17713. Add('class function TMyB.NewInstance(fnname: string; const paramarray: string): TMyB;');
  17714. Add('begin end;');
  17715. Add('begin');
  17716. SetExpectedPasResolverError('Incompatible type arg no. 2: Got "type", expected "untyped"',
  17717. nIncompatibleTypeArgNo);
  17718. ConvertProgram;
  17719. end;
  17720. procedure TTestModule.TestExternalClass_JSFunctionPasDescendant;
  17721. begin
  17722. StartProgram(false);
  17723. Add([
  17724. '{$modeswitch externalclass}',
  17725. 'type',
  17726. ' TJSFunction = class external name ''Function''',
  17727. ' end;',
  17728. ' TExtA = class external name ''ExtA''(TJSFunction)',
  17729. ' constructor New(w: word);',
  17730. ' end;',
  17731. ' TBird = class (TExtA)',
  17732. ' public',
  17733. ' Size: word;',
  17734. ' class var Legs: word;',
  17735. ' constructor Create(a: word);',
  17736. ' end;',
  17737. ' TEagle = class (TBird)',
  17738. ' public',
  17739. ' constructor Create(b: word); reintroduce;',
  17740. ' end;',
  17741. 'constructor TBird.Create(a: word);',
  17742. 'begin',
  17743. ' inherited;', // silently ignored
  17744. ' inherited New(a);', // this.$func(a)
  17745. 'end;',
  17746. 'constructor TEagle.Create(b: word);',
  17747. 'begin',
  17748. ' inherited Create(b);',
  17749. 'end;',
  17750. 'var',
  17751. ' Bird: TBird;',
  17752. ' Eagle: TEagle;',
  17753. 'begin',
  17754. ' Bird:=TBird.Create(3);',
  17755. ' Eagle:=TEagle.Create(4);',
  17756. ' Bird.Size:=Bird.Size+5;',
  17757. ' Bird.Legs:=Bird.Legs+6;',
  17758. ' Eagle.Size:=Eagle.Size+5;',
  17759. ' Eagle.Legs:=Eagle.Legs+6;',
  17760. '']);
  17761. ConvertProgram;
  17762. CheckSource('TestExternalClass_JSFunctionPasDescendant',
  17763. LinesToStr([ // statements
  17764. 'rtl.createClassExt(this, "TBird", ExtA, "", function () {',
  17765. ' this.Legs = 0;',
  17766. ' this.$init = function () {',
  17767. ' this.Size = 0;',
  17768. ' };',
  17769. ' this.$final = function () {',
  17770. ' };',
  17771. ' this.Create = function (a) {',
  17772. ' this.$ancestorfunc(a);',
  17773. ' return this;',
  17774. ' };',
  17775. '});',
  17776. 'rtl.createClassExt(this, "TEagle", this.TBird, "", function () {',
  17777. ' this.Create$1 = function (b) {',
  17778. ' $mod.TBird.Create.call(this, b);',
  17779. ' return this;',
  17780. ' };',
  17781. '});',
  17782. 'this.Bird = null;',
  17783. 'this.Eagle = null;',
  17784. '']),
  17785. LinesToStr([ // $mod.$main
  17786. '$mod.Bird = $mod.TBird.$create("Create", [3]);',
  17787. '$mod.Eagle = $mod.TEagle.$create("Create$1", [4]);',
  17788. '$mod.Bird.Size = $mod.Bird.Size + 5;',
  17789. '$mod.TBird.Legs = $mod.Bird.Legs + 6;',
  17790. '$mod.Eagle.Size = $mod.Eagle.Size + 5;',
  17791. '$mod.TBird.Legs = $mod.Eagle.Legs + 6;',
  17792. '']));
  17793. end;
  17794. procedure TTestModule.TestExternalClass_PascalProperty;
  17795. begin
  17796. StartProgram(false);
  17797. Add('{$modeswitch externalclass}');
  17798. Add('type');
  17799. Add(' TJSElement = class;');
  17800. Add(' TJSNotifyEvent = procedure(Sender: TJSElement) of object;');
  17801. Add(' TJSElement = class external name ''ExtA''');
  17802. Add(' end;');
  17803. Add(' TControl = class(TJSElement)');
  17804. Add(' private');
  17805. Add(' FOnClick: TJSNotifyEvent;');
  17806. Add(' property OnClick: TJSNotifyEvent read FOnClick write FOnClick;');
  17807. Add(' procedure Click(Sender: TJSElement);');
  17808. Add(' end;');
  17809. Add('procedure TControl.Click(Sender: TJSElement);');
  17810. Add('begin');
  17811. Add(' OnClick(Self);');
  17812. Add('end;');
  17813. Add('var');
  17814. Add(' Ctrl: TControl;');
  17815. Add('begin');
  17816. Add(' Ctrl.OnClick:[email protected];');
  17817. Add(' Ctrl.OnClick(Ctrl);');
  17818. ConvertProgram;
  17819. CheckSource('TestExternalClass_PascalProperty',
  17820. LinesToStr([ // statements
  17821. 'rtl.createClassExt(this, "TControl", ExtA, "", function () {',
  17822. ' this.$init = function () {',
  17823. ' this.FOnClick = null;',
  17824. ' };',
  17825. ' this.$final = function () {',
  17826. ' this.FOnClick = undefined;',
  17827. ' };',
  17828. ' this.Click = function (Sender) {',
  17829. ' this.FOnClick(this);',
  17830. ' };',
  17831. '});',
  17832. 'this.Ctrl = null;',
  17833. '']),
  17834. LinesToStr([ // $mod.$main
  17835. '$mod.Ctrl.FOnClick = rtl.createCallback($mod.Ctrl, "Click");',
  17836. '$mod.Ctrl.FOnClick($mod.Ctrl);',
  17837. '']));
  17838. end;
  17839. procedure TTestModule.TestExternalClass_TypeCastToRootClass;
  17840. begin
  17841. StartProgram(false);
  17842. Add([
  17843. '{$modeswitch externalclass}',
  17844. 'type',
  17845. ' IUnknown = interface end;',
  17846. ' TObject = class',
  17847. ' end;',
  17848. ' TChild = class',
  17849. ' end;',
  17850. ' TExtRootA = class external name ''ExtRootA''',
  17851. ' end;',
  17852. ' TExtChildA = class external name ''ExtChildA''(TExtRootA)',
  17853. ' end;',
  17854. ' TExtRootB = class external name ''ExtRootB''',
  17855. ' end;',
  17856. ' TExtChildB = class external name ''ExtChildB''(TExtRootB)',
  17857. ' end;',
  17858. 'var',
  17859. ' Obj: TObject;',
  17860. ' Child: TChild;',
  17861. ' RootA: TExtRootA;',
  17862. ' ChildA: TExtChildA;',
  17863. ' RootB: TExtRootB;',
  17864. ' ChildB: TExtChildB;',
  17865. ' i: IUnknown;',
  17866. 'begin',
  17867. ' obj:=tobject(roota);',
  17868. ' obj:=tobject(childa);',
  17869. ' child:=tchild(tobject(roota));',
  17870. ' roota:=textroota(obj);',
  17871. ' roota:=textroota(child);',
  17872. ' roota:=textroota(rootb);',
  17873. ' roota:=textroota(childb);',
  17874. ' childa:=textchilda(textroota(obj));',
  17875. ' roota:=TExtRootA(i)',
  17876. '']);
  17877. ConvertProgram;
  17878. CheckSource('TestExternalClass_TypeCastToRootClass',
  17879. LinesToStr([ // statements
  17880. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  17881. 'rtl.createClass(this, "TObject", null, function () {',
  17882. ' this.$init = function () {',
  17883. ' };',
  17884. ' this.$final = function () {',
  17885. ' };',
  17886. '});',
  17887. 'rtl.createClass(this, "TChild", this.TObject, function () {',
  17888. '});',
  17889. 'this.Obj = null;',
  17890. 'this.Child = null;',
  17891. 'this.RootA = null;',
  17892. 'this.ChildA = null;',
  17893. 'this.RootB = null;',
  17894. 'this.ChildB = null;',
  17895. 'this.i = null;',
  17896. '']),
  17897. LinesToStr([ // $mod.$main
  17898. '$mod.Obj = $mod.RootA;',
  17899. '$mod.Obj = $mod.ChildA;',
  17900. '$mod.Child = $mod.RootA;',
  17901. '$mod.RootA = $mod.Obj;',
  17902. '$mod.RootA = $mod.Child;',
  17903. '$mod.RootA = $mod.RootB;',
  17904. '$mod.RootA = $mod.ChildB;',
  17905. '$mod.ChildA = $mod.Obj;',
  17906. '$mod.RootA = $mod.i;',
  17907. '']));
  17908. end;
  17909. procedure TTestModule.TestExternalClass_TypeCastToJSObject;
  17910. begin
  17911. StartProgram(false);
  17912. Add([
  17913. '{$modeswitch externalclass}',
  17914. 'type',
  17915. ' IUnknown = interface end;',
  17916. ' IBird = interface(IUnknown) end;',
  17917. ' TClass = class of TObject;',
  17918. ' TObject = class',
  17919. ' end;',
  17920. ' TChild = class',
  17921. ' end;',
  17922. ' TJSObject = class external name ''Object''',
  17923. ' end;',
  17924. ' TRec = record end;',
  17925. 'var',
  17926. ' Obj: TObject;',
  17927. ' Child: TChild;',
  17928. ' i: IUnknown;',
  17929. ' Bird: IBird;',
  17930. ' j: TJSObject;',
  17931. ' r: TRec;',
  17932. ' c: TClass;',
  17933. 'begin',
  17934. ' j:=tjsobject(IUnknown);',
  17935. ' j:=tjsobject(IBird);',
  17936. ' j:=tjsobject(TObject);',
  17937. ' j:=tjsobject(TChild);',
  17938. ' j:=tjsobject(TRec);',
  17939. ' j:=tjsobject(Obj);',
  17940. ' j:=tjsobject(Child);',
  17941. ' j:=tjsobject(i);',
  17942. ' j:=tjsobject(Bird);',
  17943. ' j:=tjsobject(r);',
  17944. ' j:=tjsobject(c);',
  17945. '']);
  17946. ConvertProgram;
  17947. CheckSource('TestExternalClass_TypeCastToJSObject',
  17948. LinesToStr([ // statements
  17949. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  17950. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], this.IUnknown);',
  17951. 'rtl.createClass(this, "TObject", null, function () {',
  17952. ' this.$init = function () {',
  17953. ' };',
  17954. ' this.$final = function () {',
  17955. ' };',
  17956. '});',
  17957. 'rtl.createClass(this, "TChild", this.TObject, function () {',
  17958. '});',
  17959. 'rtl.recNewT(this, "TRec", function () {',
  17960. ' this.$eq = function (b) {',
  17961. ' return true;',
  17962. ' };',
  17963. ' this.$assign = function (s) {',
  17964. ' return this;',
  17965. ' };',
  17966. '});',
  17967. 'this.Obj = null;',
  17968. 'this.Child = null;',
  17969. 'this.i = null;',
  17970. 'this.Bird = null;',
  17971. 'this.j = null;',
  17972. 'this.r = this.TRec.$new();',
  17973. 'this.c = null;',
  17974. '']),
  17975. LinesToStr([ // $mod.$main
  17976. '$mod.j = $mod.IUnknown;',
  17977. '$mod.j = $mod.IBird;',
  17978. '$mod.j = $mod.TObject;',
  17979. '$mod.j = $mod.TChild;',
  17980. '$mod.j = $mod.TRec;',
  17981. '$mod.j = $mod.Obj;',
  17982. '$mod.j = $mod.Child;',
  17983. '$mod.j = $mod.i;',
  17984. '$mod.j = $mod.Bird;',
  17985. '$mod.j = $mod.r;',
  17986. '$mod.j = $mod.c;',
  17987. '']));
  17988. end;
  17989. procedure TTestModule.TestExternalClass_TypeCastStringToExternalString;
  17990. begin
  17991. StartProgram(false);
  17992. Add('{$modeswitch externalclass}');
  17993. Add('type');
  17994. Add(' TJSString = class external name ''String''');
  17995. Add(' class function fromCharCode() : string; varargs;');
  17996. Add(' function anchor(const aName : string) : string;');
  17997. Add(' end;');
  17998. Add('var');
  17999. Add(' s: string;');
  18000. Add('begin');
  18001. Add(' s:=TJSString.fromCharCode(65,66);');
  18002. Add(' s:=TJSString(s).anchor(s);');
  18003. Add(' s:=TJSString(''foo'').anchor(s);');
  18004. ConvertProgram;
  18005. CheckSource('TestExternalClass_TypeCastStringToExternalString',
  18006. LinesToStr([ // statements
  18007. 'this.s = "";',
  18008. '']),
  18009. LinesToStr([ // $mod.$main
  18010. '$mod.s = String.fromCharCode(65, 66);',
  18011. '$mod.s = $mod.s.anchor($mod.s);',
  18012. '$mod.s = "foo".anchor($mod.s);',
  18013. '']));
  18014. end;
  18015. procedure TTestModule.TestExternalClass_TypeCastToJSFunction;
  18016. begin
  18017. StartProgram(false);
  18018. Add([
  18019. '{$modeswitch externalclass}',
  18020. 'type',
  18021. ' TJSObject = class external name ''Object'' end;',
  18022. ' TJSFunction = class external name ''Function''',
  18023. ' function bind(thisArg: TJSObject): TJSFunction; varargs;',
  18024. ' function call(thisArg: TJSObject): JSValue; varargs;',
  18025. ' end;',
  18026. ' TObject = class',
  18027. ' procedure DoIt(i: longint);',
  18028. ' end;',
  18029. ' TFuncInt = function(o: TObject): longint;',
  18030. 'function GetIt(o: TObject): longint;',
  18031. ' procedure Sub; begin end;',
  18032. 'var',
  18033. ' f: TJSFunction;',
  18034. ' fi: TFuncInt;',
  18035. 'begin',
  18036. ' fi:=TFuncInt(f);',
  18037. ' f:=TJSFunction(fi);',
  18038. ' f:=TJSFunction(@GetIt);',
  18039. ' f:=TJSFunction(@GetIt).bind(nil,3);',
  18040. ' f:=TJSFunction(@Sub);',
  18041. ' f:=TJSFunction(@o.doit);',
  18042. ' f:=TJSFunction(fi).bind(nil,4)',
  18043. 'end;',
  18044. 'procedure TObject.DoIt(i: longint);',
  18045. ' procedure Sub; begin end;',
  18046. 'var f: TJSFunction;',
  18047. 'begin',
  18048. ' f:=TJSFunction(@DoIt);',
  18049. ' f:=TJSFunction(@DoIt).bind(nil,13);',
  18050. ' f:=TJSFunction(@Sub);',
  18051. ' f:=TJSFunction(@GetIt);',
  18052. 'end;',
  18053. 'begin']);
  18054. ConvertProgram;
  18055. CheckSource('TestExternalClass_TypeCastToJSFunction',
  18056. LinesToStr([ // statements
  18057. 'rtl.createClass(this, "TObject", null, function () {',
  18058. ' this.$init = function () {',
  18059. ' };',
  18060. ' this.$final = function () {',
  18061. ' };',
  18062. ' this.DoIt = function (i) {',
  18063. ' var $Self = this;',
  18064. ' function Sub() {',
  18065. ' };',
  18066. ' var f = null;',
  18067. ' f = this.DoIt;',
  18068. ' f = this.DoIt.bind(null, 13);',
  18069. ' f = Sub;',
  18070. ' f = $mod.GetIt;',
  18071. ' };',
  18072. '});',
  18073. 'this.GetIt = function (o) {',
  18074. ' var Result = 0;',
  18075. ' function Sub() {',
  18076. ' };',
  18077. ' var f = null;',
  18078. ' var fi = null;',
  18079. ' fi = f;',
  18080. ' f = fi;',
  18081. ' f = $mod.GetIt;',
  18082. ' f = $mod.GetIt.bind(null, 3);',
  18083. ' f = Sub;',
  18084. ' f = $mod.TObject.DoIt;',
  18085. ' f = fi.bind(null, 4);',
  18086. ' return Result;',
  18087. '};',
  18088. '']),
  18089. LinesToStr([ // $mod.$main
  18090. '']));
  18091. end;
  18092. procedure TTestModule.TestExternalClass_TypeCastDelphiUnrelated;
  18093. begin
  18094. StartProgram(false);
  18095. Add([
  18096. '{$mode delphi}',
  18097. '{$modeswitch externalclass}',
  18098. 'type',
  18099. ' TJSObject = class external name ''Object'' end;',
  18100. ' TJSWindow = class external name ''Window''(TJSObject)',
  18101. ' procedure Open;',
  18102. ' end;',
  18103. ' TJSEventTarget = class external name ''Event''(TJSObject)',
  18104. ' procedure Execute;',
  18105. ' end;',
  18106. 'procedure Fly;',
  18107. 'var',
  18108. ' w: TJSWindow;',
  18109. ' e: TJSEventTarget;',
  18110. 'begin',
  18111. ' w:=TJSWindow(e);',
  18112. ' e:=TJSEventTarget(w);',
  18113. 'end;',
  18114. 'begin']);
  18115. ConvertProgram;
  18116. CheckSource('TestExternalClass_TypeCastDelphiUnrelated',
  18117. LinesToStr([ // statements
  18118. 'this.Fly = function () {',
  18119. ' var w = null;',
  18120. ' var e = null;',
  18121. ' w = e;',
  18122. ' e = w;',
  18123. '};',
  18124. '']),
  18125. LinesToStr([ // $mod.$main
  18126. '']));
  18127. end;
  18128. procedure TTestModule.TestExternalClass_CallClassFunctionOfInstanceFail;
  18129. begin
  18130. StartProgram(false);
  18131. Add('{$modeswitch externalclass}');
  18132. Add('type');
  18133. Add(' TJSString = class external name ''String''');
  18134. Add(' class function fromCharCode() : string; varargs;');
  18135. Add(' end;');
  18136. Add('var');
  18137. Add(' s: string;');
  18138. Add(' sObj: TJSString;');
  18139. Add('begin');
  18140. Add(' s:=sObj.fromCharCode(65,66);');
  18141. SetExpectedPasResolverError('External class instance cannot access static class function fromCharCode',
  18142. nExternalClassInstanceCannotAccessStaticX);
  18143. ConvertProgram;
  18144. end;
  18145. procedure TTestModule.TestExternalClass_BracketAccessor;
  18146. begin
  18147. StartProgram(false);
  18148. Add([
  18149. '{$modeswitch externalclass}',
  18150. 'type',
  18151. ' TJSArray = class external name ''Array2''',
  18152. ' function GetItems(Index: longint): jsvalue; external name ''[]'';',
  18153. ' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';',
  18154. ' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;',
  18155. ' end;',
  18156. 'procedure DoIt(vI: JSValue; const vJ: jsvalue; var vK: jsvalue; out vL: jsvalue);',
  18157. 'begin end;',
  18158. 'var',
  18159. ' Arr: tjsarray;',
  18160. ' s: string;',
  18161. ' i: longint;',
  18162. ' v: jsvalue;',
  18163. 'begin',
  18164. ' v:=arr[0];',
  18165. ' v:=arr.items[1];',
  18166. ' arr[2]:=s;',
  18167. ' arr.items[3]:=s;',
  18168. ' arr[4]:=i;',
  18169. ' arr[5]:=arr[6];',
  18170. ' arr.items[7]:=arr.items[8];',
  18171. ' with arr do items[9]:=items[10];',
  18172. ' doit(arr[7],arr[8],arr[9],arr[10]);',
  18173. ' with arr do begin',
  18174. ' v:=GetItems(14);',
  18175. ' setitems(15,16);',
  18176. ' end;',
  18177. ' v:=test1.arr.items[17];',
  18178. ' test1.arr.items[18]:=v;',
  18179. '']);
  18180. ConvertProgram;
  18181. CheckSource('TestExternalClass_BracketAccessor',
  18182. LinesToStr([ // statements
  18183. 'this.DoIt = function (vI, vJ, vK, vL) {',
  18184. '};',
  18185. 'this.Arr = null;',
  18186. 'this.s = "";',
  18187. 'this.i = 0;',
  18188. 'this.v = undefined;',
  18189. '']),
  18190. LinesToStr([ // $mod.$main
  18191. '$mod.v = $mod.Arr[0];',
  18192. '$mod.v = $mod.Arr[1];',
  18193. '$mod.Arr[2] = $mod.s;',
  18194. '$mod.Arr[3] = $mod.s;',
  18195. '$mod.Arr[4] = $mod.i;',
  18196. '$mod.Arr[5] = $mod.Arr[6];',
  18197. '$mod.Arr[7] = $mod.Arr[8];',
  18198. 'var $with = $mod.Arr;',
  18199. '$with[9] = $with[10];',
  18200. '$mod.DoIt($mod.Arr[7], $mod.Arr[8], {',
  18201. ' a: 9,',
  18202. ' p: $mod.Arr,',
  18203. ' get: function () {',
  18204. ' return this.p[this.a];',
  18205. ' },',
  18206. ' set: function (v) {',
  18207. ' this.p[this.a] = v;',
  18208. ' }',
  18209. '}, {',
  18210. ' a: 10,',
  18211. ' p: $mod.Arr,',
  18212. ' get: function () {',
  18213. ' return this.p[this.a];',
  18214. ' },',
  18215. ' set: function (v) {',
  18216. ' this.p[this.a] = v;',
  18217. ' }',
  18218. '});',
  18219. 'var $with1 = $mod.Arr;',
  18220. '$mod.v = $with1[14];',
  18221. '$with1[15] = 16;',
  18222. '$mod.v = $mod.Arr[17];',
  18223. '$mod.Arr[18] = $mod.v;',
  18224. '']));
  18225. end;
  18226. procedure TTestModule.TestExternalClass_BracketAccessor_Call;
  18227. begin
  18228. StartProgram(false);
  18229. Add([
  18230. '{$modeswitch externalclass}',
  18231. 'type',
  18232. ' TJSArray = class external name ''Array2''',
  18233. ' function GetItems(Index: longint): jsvalue; external name ''[]'';',
  18234. ' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';',
  18235. ' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;',
  18236. ' end;',
  18237. ' TMyArr = class(TJSArray)',
  18238. ' procedure DoIt;',
  18239. ' end;',
  18240. 'procedure tmyarr.DoIt;',
  18241. 'begin',
  18242. ' Items[1]:=Items[2];',
  18243. ' SetItems(3,getItems(4));',
  18244. 'end;',
  18245. 'var',
  18246. ' Arr: tmyarr;',
  18247. ' s: string;',
  18248. ' i: longint;',
  18249. ' v: jsvalue;',
  18250. 'begin',
  18251. ' v:=arr[0];',
  18252. ' v:=arr.items[1];',
  18253. ' arr[2]:=s;',
  18254. ' arr.items[3]:=s;',
  18255. ' arr[4]:=i;',
  18256. ' arr[5]:=arr[6];',
  18257. ' arr.items[7]:=arr.items[8];',
  18258. ' with arr do items[9]:=items[10];',
  18259. ' with arr do begin',
  18260. ' v:=GetItems(14);',
  18261. ' setitems(15,16);',
  18262. ' end;',
  18263. '']);
  18264. ConvertProgram;
  18265. CheckSource('TestExternalClass_BracketAccessor_Call',
  18266. LinesToStr([ // statements
  18267. 'rtl.createClassExt(this, "TMyArr", Array2, "", function () {',
  18268. ' this.$init = function () {',
  18269. ' };',
  18270. ' this.$final = function () {',
  18271. ' };',
  18272. ' this.DoIt = function () {',
  18273. ' this[1] = this[2];',
  18274. ' this[3] = this[4];',
  18275. ' };',
  18276. '});',
  18277. 'this.Arr = null;',
  18278. 'this.s = "";',
  18279. 'this.i = 0;',
  18280. 'this.v = undefined;',
  18281. '']),
  18282. LinesToStr([ // $mod.$main
  18283. '$mod.v = $mod.Arr[0];',
  18284. '$mod.v = $mod.Arr[1];',
  18285. '$mod.Arr[2] = $mod.s;',
  18286. '$mod.Arr[3] = $mod.s;',
  18287. '$mod.Arr[4] = $mod.i;',
  18288. '$mod.Arr[5] = $mod.Arr[6];',
  18289. '$mod.Arr[7] = $mod.Arr[8];',
  18290. 'var $with = $mod.Arr;',
  18291. '$with[9] = $with[10];',
  18292. 'var $with1 = $mod.Arr;',
  18293. '$mod.v = $with1[14];',
  18294. '$with1[15] = 16;',
  18295. '']));
  18296. end;
  18297. procedure TTestModule.TestExternalClass_BracketAccessor_2ParamsFail;
  18298. begin
  18299. StartProgram(false);
  18300. Add('{$modeswitch externalclass}');
  18301. Add('type');
  18302. Add(' TJSArray = class external name ''Array2''');
  18303. Add(' function GetItems(Index1, Index2: longint): jsvalue; external name ''[]'';');
  18304. Add(' procedure SetItems(Index1, Index2: longint; Value: jsvalue); external name ''[]'';');
  18305. Add(' property Items[Index1, Index2: longint]: jsvalue read GetItems write SetItems; default;');
  18306. Add(' end;');
  18307. Add('begin');
  18308. SetExpectedPasResolverError(sBracketAccessorOfExternalClassMustHaveOneParameter,
  18309. nBracketAccessorOfExternalClassMustHaveOneParameter);
  18310. ConvertProgram;
  18311. end;
  18312. procedure TTestModule.TestExternalClass_BracketAccessor_ReadOnly;
  18313. begin
  18314. StartProgram(false);
  18315. Add('{$modeswitch externalclass}');
  18316. Add('type');
  18317. Add(' TJSArray = class external name ''Array2''');
  18318. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  18319. Add(' property Items[Index: longint]: jsvalue read GetItems; default;');
  18320. Add(' end;');
  18321. Add('procedure DoIt(vI: JSValue; const vJ: jsvalue);');
  18322. Add('begin end;');
  18323. Add('var');
  18324. Add(' Arr: tjsarray;');
  18325. Add(' v: jsvalue;');
  18326. Add('begin');
  18327. Add(' v:=arr[0];');
  18328. Add(' v:=arr.items[1];');
  18329. Add(' with arr do v:=items[2];');
  18330. Add(' doit(arr[3],arr[4]);');
  18331. ConvertProgram;
  18332. CheckSource('TestExternalClass_BracketAccessor_ReadOnly',
  18333. LinesToStr([ // statements
  18334. 'this.DoIt = function (vI, vJ) {',
  18335. '};',
  18336. 'this.Arr = null;',
  18337. 'this.v = undefined;',
  18338. '']),
  18339. LinesToStr([ // $mod.$main
  18340. '$mod.v = $mod.Arr[0];',
  18341. '$mod.v = $mod.Arr[1];',
  18342. 'var $with = $mod.Arr;',
  18343. '$mod.v = $with[2];',
  18344. '$mod.DoIt($mod.Arr[3], $mod.Arr[4]);',
  18345. '']));
  18346. end;
  18347. procedure TTestModule.TestExternalClass_BracketAccessor_WriteOnly;
  18348. begin
  18349. StartProgram(false);
  18350. Add('{$modeswitch externalclass}');
  18351. Add('type');
  18352. Add(' TJSArray = class external name ''Array2''');
  18353. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  18354. Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
  18355. Add(' end;');
  18356. Add('var');
  18357. Add(' Arr: tjsarray;');
  18358. Add(' s: string;');
  18359. Add(' i: longint;');
  18360. Add(' v: jsvalue;');
  18361. Add('begin');
  18362. Add(' arr[2]:=s;');
  18363. Add(' arr.items[3]:=s;');
  18364. Add(' arr[4]:=i;');
  18365. Add(' with arr do items[5]:=i;');
  18366. ConvertProgram;
  18367. CheckSource('TestExternalClass_BracketAccessor_WriteOnly',
  18368. LinesToStr([ // statements
  18369. 'this.Arr = null;',
  18370. 'this.s = "";',
  18371. 'this.i = 0;',
  18372. 'this.v = undefined;',
  18373. '']),
  18374. LinesToStr([ // $mod.$main
  18375. '$mod.Arr[2] = $mod.s;',
  18376. '$mod.Arr[3] = $mod.s;',
  18377. '$mod.Arr[4] = $mod.i;',
  18378. 'var $with = $mod.Arr;',
  18379. '$with[5] = $mod.i;',
  18380. '']));
  18381. end;
  18382. procedure TTestModule.TestExternalClass_BracketAccessor_MultiType;
  18383. begin
  18384. StartProgram(false);
  18385. Add('{$modeswitch externalclass}');
  18386. Add('type');
  18387. Add(' TJSArray = class external name ''Array2''');
  18388. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  18389. Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
  18390. Add(' procedure SetNumbers(Index: longint; Value: longint); external name ''[]'';');
  18391. Add(' property Numbers[Index: longint]: longint write SetNumbers;');
  18392. Add(' end;');
  18393. Add('var');
  18394. Add(' Arr: tjsarray;');
  18395. Add(' s: string;');
  18396. Add(' i: longint;');
  18397. Add(' v: jsvalue;');
  18398. Add('begin');
  18399. Add(' arr[2]:=s;');
  18400. Add(' arr.items[3]:=s;');
  18401. Add(' arr.numbers[4]:=i;');
  18402. Add(' with arr do items[5]:=i;');
  18403. Add(' with arr do numbers[6]:=i;');
  18404. ConvertProgram;
  18405. CheckSource('TestExternalClass_BracketAccessor_MultiType',
  18406. LinesToStr([ // statements
  18407. 'this.Arr = null;',
  18408. 'this.s = "";',
  18409. 'this.i = 0;',
  18410. 'this.v = undefined;',
  18411. '']),
  18412. LinesToStr([ // $mod.$main
  18413. '$mod.Arr[2] = $mod.s;',
  18414. '$mod.Arr[3] = $mod.s;',
  18415. '$mod.Arr[4] = $mod.i;',
  18416. 'var $with = $mod.Arr;',
  18417. '$with[5] = $mod.i;',
  18418. 'var $with1 = $mod.Arr;',
  18419. '$with1[6] = $mod.i;',
  18420. '']));
  18421. end;
  18422. procedure TTestModule.TestExternalClass_BracketAccessor_Index;
  18423. begin
  18424. StartProgram(false);
  18425. Add('{$modeswitch externalclass}');
  18426. Add('type');
  18427. Add(' TJSArray = class external name ''Array2''');
  18428. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  18429. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  18430. Add(' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;');
  18431. Add(' end;');
  18432. Add('var');
  18433. Add(' Arr: tjsarray;');
  18434. Add(' i: longint;');
  18435. Add(' IntArr: array of longint;');
  18436. Add(' v: jsvalue;');
  18437. Add('begin');
  18438. Add(' v:=arr.items[i];');
  18439. Add(' arr[longint(v)]:=arr.items[intarr[0]];');
  18440. Add(' arr.items[intarr[1]]:=arr[IntArr[2]];');
  18441. ConvertProgram;
  18442. CheckSource('TestExternalClass_BracketAccessor_Index',
  18443. LinesToStr([ // statements
  18444. 'this.Arr = null;',
  18445. 'this.i = 0;',
  18446. 'this.IntArr = [];',
  18447. 'this.v = undefined;',
  18448. '']),
  18449. LinesToStr([ // $mod.$main
  18450. '$mod.v = $mod.Arr[$mod.i];',
  18451. '$mod.Arr[rtl.trunc($mod.v)] = $mod.Arr[$mod.IntArr[0]];',
  18452. '$mod.Arr[$mod.IntArr[1]] = $mod.Arr[$mod.IntArr[2]];',
  18453. '']));
  18454. end;
  18455. procedure TTestModule.TestExternalClass_ForInJSObject;
  18456. begin
  18457. StartProgram(false);
  18458. Add([
  18459. '{$modeswitch externalclass}',
  18460. 'type',
  18461. ' TJSObject = class external name ''Object''',
  18462. ' end;',
  18463. 'var',
  18464. ' o: TJSObject;',
  18465. ' key: string;',
  18466. 'begin',
  18467. ' for key in o do',
  18468. ' if key=''abc'' then ;',
  18469. '']);
  18470. ConvertProgram;
  18471. CheckSource('TestExternalClass_ForInJSObject',
  18472. LinesToStr([ // statements
  18473. 'this.o = null;',
  18474. 'this.key = "";',
  18475. '']),
  18476. LinesToStr([ // $mod.$main
  18477. 'for ($mod.key in $mod.o) if ($mod.key === "abc") ;',
  18478. '']));
  18479. end;
  18480. procedure TTestModule.TestExternalClass_ForInJSArray;
  18481. begin
  18482. StartProgram(false);
  18483. Add([
  18484. '{$modeswitch externalclass}',
  18485. 'type',
  18486. ' TJSInt8Array = class external name ''Int8Array''',
  18487. ' private',
  18488. ' flength: NativeInt external name ''length'';',
  18489. ' function getValue(Index: NativeInt): shortint; external name ''[]'';',
  18490. ' public',
  18491. ' property values[Index: NativeInt]: Shortint Read getValue; default;',
  18492. ' property Length: NativeInt read flength;',
  18493. ' end;',
  18494. 'var',
  18495. ' a: TJSInt8Array;',
  18496. ' value: shortint;',
  18497. 'begin',
  18498. ' for value in a do',
  18499. ' if value=3 then ;',
  18500. '']);
  18501. ConvertProgram;
  18502. CheckSource('TestExternalClass_ForInJSArray',
  18503. LinesToStr([ // statements
  18504. 'this.a = null;',
  18505. 'this.value = 0;',
  18506. '']),
  18507. LinesToStr([ // $mod.$main
  18508. 'for (var $in = $mod.a, $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) {',
  18509. ' $mod.value = $in[$l];',
  18510. ' if ($mod.value === 3) ;',
  18511. '};',
  18512. '']));
  18513. end;
  18514. procedure TTestModule.TestExternalClass_IncompatibleArgDuplicateIdentifier;
  18515. begin
  18516. AddModuleWithIntfImplSrc('unit2.pas',
  18517. LinesToStr([
  18518. '{$modeswitch externalclass}',
  18519. 'type',
  18520. ' TJSBufferSource = class external name ''BufferSource''',
  18521. ' end;',
  18522. 'procedure DoIt(s: TJSBufferSource); external name ''DoIt'';',
  18523. '']),
  18524. '');
  18525. AddModuleWithIntfImplSrc('unit3.pas',
  18526. LinesToStr([
  18527. '{$modeswitch externalclass}',
  18528. 'type',
  18529. ' TJSBufferSource = class external name ''BufferSource''',
  18530. ' end;',
  18531. '']),
  18532. '');
  18533. StartUnit(true);
  18534. Add([
  18535. 'interface',
  18536. 'uses unit2, unit3;',
  18537. 'procedure DoSome(s: TJSBufferSource);',
  18538. 'implementation',
  18539. 'procedure DoSome(s: TJSBufferSource);',
  18540. 'begin',
  18541. ' DoIt(s);',
  18542. 'end;',
  18543. '']);
  18544. SetExpectedPasResolverError('Incompatible type arg no. 1: Got "unit3.TJSBufferSource", expected "unit2.TJSBufferSource"',
  18545. nIncompatibleTypeArgNo);
  18546. ConvertUnit;
  18547. end;
  18548. procedure TTestModule.TestClassInterface_Corba;
  18549. begin
  18550. StartProgram(false);
  18551. Add([
  18552. '{$interfaces corba}',
  18553. 'type',
  18554. ' IUnknown = interface;',
  18555. ' IUnknown = interface',
  18556. ' [''{00000000-0000-0000-C000-000000000046}'']',
  18557. ' end;',
  18558. ' IInterface = IUnknown;',
  18559. ' IBird = interface(IInterface)',
  18560. ' function GetSize: longint;',
  18561. ' procedure SetSize(i: longint);',
  18562. ' property Size: longint read GetSize write SetSize;',
  18563. ' procedure DoIt(i: longint);',
  18564. ' end;',
  18565. ' TObject = class',
  18566. ' end;',
  18567. ' TBird = class(TObject,IBird)',
  18568. ' function GetSize: longint; virtual; abstract;',
  18569. ' procedure SetSize(i: longint); virtual; abstract;',
  18570. ' procedure DoIt(i: longint); virtual; abstract;',
  18571. ' end;',
  18572. 'var',
  18573. ' BirdIntf: IBird;',
  18574. 'begin',
  18575. ' BirdIntf.Size:=BirdIntf.Size;',
  18576. '']);
  18577. ConvertProgram;
  18578. CheckSource('TestClassInterface_Corba',
  18579. LinesToStr([ // statements
  18580. 'rtl.createInterface(this, "IUnknown", "{00000000-0000-0000-C000-000000000046}", [], null);',
  18581. 'rtl.createInterface(this, "IBird", "{5BD1A53B-69BB-37EE-AF32-BEFB86D85B03}", ["GetSize", "SetSize", "DoIt"], this.IUnknown);',
  18582. 'rtl.createClass(this, "TObject", null, function () {',
  18583. ' this.$init = function () {',
  18584. ' };',
  18585. ' this.$final = function () {',
  18586. ' };',
  18587. '});',
  18588. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18589. ' rtl.addIntf(this, $mod.IBird);',
  18590. '});',
  18591. 'this.BirdIntf = null;',
  18592. '']),
  18593. LinesToStr([ // $mod.$main
  18594. ' $mod.BirdIntf.SetSize($mod.BirdIntf.GetSize());',
  18595. '']));
  18596. end;
  18597. procedure TTestModule.TestClassInterface_ProcExternalFail;
  18598. begin
  18599. StartProgram(false);
  18600. Add([
  18601. '{$interfaces corba}',
  18602. 'type',
  18603. ' IUnknown = interface',
  18604. ' procedure DoIt; external name ''foo'';',
  18605. ' end;',
  18606. 'begin']);
  18607. SetExpectedParserError(
  18608. 'Fields are not allowed in interface at token "Identifier external" in file test1.pp at line 6 column 21',
  18609. nParserNoFieldsAllowed);
  18610. ConvertProgram;
  18611. end;
  18612. procedure TTestModule.TestClassInterface_Overloads;
  18613. begin
  18614. StartProgram(false);
  18615. Add([
  18616. '{$interfaces corba}',
  18617. 'type',
  18618. ' integer = longint;',
  18619. ' IUnknown = interface',
  18620. ' procedure DoIt(i: integer);',
  18621. ' procedure DoIt(s: string);',
  18622. ' end;',
  18623. ' IBird = interface(IUnknown)',
  18624. ' procedure DoIt(b: boolean); overload;',
  18625. ' end;',
  18626. ' TObject = class',
  18627. ' end;',
  18628. ' TBird = class(TObject,IBird)',
  18629. ' procedure DoIt(o: TObject);',
  18630. ' procedure DoIt(s: string);',
  18631. ' procedure DoIt(i: integer);',
  18632. ' procedure DoIt(b: boolean);',
  18633. ' end;',
  18634. 'procedure TBird.DoIt(o: TObject); begin end;',
  18635. 'procedure TBird.DoIt(s: string); begin end;',
  18636. 'procedure TBird.DoIt(i: integer); begin end;',
  18637. 'procedure TBird.DoIt(b: boolean); begin end;',
  18638. 'var',
  18639. ' BirdIntf: IBird;',
  18640. 'begin',
  18641. ' BirdIntf.DoIt(3);',
  18642. ' BirdIntf.DoIt(''abc'');',
  18643. ' BirdIntf.DoIt(true);',
  18644. '']);
  18645. ConvertProgram;
  18646. CheckSource('TestClassInterface_Overloads',
  18647. LinesToStr([ // statements
  18648. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDC4-8A2AE2C59400}", ["DoIt", "DoIt$1"], null);',
  18649. 'rtl.createInterface(this, "IBird", "{8285DD5E-EA3E-396E-AE88-000B86AABF05}", ["DoIt$2"], this.IUnknown);',
  18650. 'rtl.createClass(this, "TObject", null, function () {',
  18651. ' this.$init = function () {',
  18652. ' };',
  18653. ' this.$final = function () {',
  18654. ' };',
  18655. '});',
  18656. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18657. ' this.DoIt = function (o) {',
  18658. ' };',
  18659. ' this.DoIt$1 = function (s) {',
  18660. ' };',
  18661. ' this.DoIt$2 = function (i) {',
  18662. ' };',
  18663. ' this.DoIt$3 = function (b) {',
  18664. ' };',
  18665. ' rtl.addIntf(this, $mod.IBird, {',
  18666. ' DoIt$2: "DoIt$3",',
  18667. ' DoIt: "DoIt$2"',
  18668. ' });',
  18669. '});',
  18670. 'this.BirdIntf = null;',
  18671. '']),
  18672. LinesToStr([ // $mod.$main
  18673. '$mod.BirdIntf.DoIt(3);',
  18674. '$mod.BirdIntf.DoIt$1("abc");',
  18675. '$mod.BirdIntf.DoIt$2(true);',
  18676. '']));
  18677. end;
  18678. procedure TTestModule.TestClassInterface_DuplicateGUIInIntfListFail;
  18679. begin
  18680. StartProgram(false);
  18681. Add([
  18682. '{$interfaces corba}',
  18683. 'type',
  18684. ' IBird = interface',
  18685. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  18686. ' end;',
  18687. ' IDog = interface',
  18688. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  18689. ' end;',
  18690. ' TObject = class(IBird,IDog)',
  18691. ' end;',
  18692. 'begin']);
  18693. SetExpectedPasResolverError('Duplicate GUID {4B3BA825-E0EC-4799-A19C-55F714A07959} in IDog and IBird',
  18694. nDuplicateGUIDXInYZ);
  18695. ConvertProgram;
  18696. end;
  18697. procedure TTestModule.TestClassInterface_DuplicateGUIInAncestorFail;
  18698. begin
  18699. StartProgram(false);
  18700. Add([
  18701. '{$interfaces corba}',
  18702. 'type',
  18703. ' IAnimal = interface',
  18704. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  18705. ' end;',
  18706. ' IBird = interface(IAnimal)',
  18707. ' end;',
  18708. ' IHawk = interface(IBird)',
  18709. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  18710. ' end;',
  18711. 'begin']);
  18712. SetExpectedPasResolverError('Duplicate GUID {4B3BA825-E0EC-4799-A19C-55F714A07959} in IHawk and IAnimal',
  18713. nDuplicateGUIDXInYZ);
  18714. ConvertProgram;
  18715. end;
  18716. procedure TTestModule.TestClassInterface_AncestorImpl;
  18717. begin
  18718. StartProgram(false);
  18719. Add([
  18720. '{$interfaces corba}',
  18721. 'type',
  18722. ' integer = longint;',
  18723. ' IUnknown = interface',
  18724. ' procedure DoIt(i: integer);',
  18725. ' end;',
  18726. ' IBird = interface',
  18727. ' procedure Fly(i: integer);',
  18728. ' end;',
  18729. ' TObject = class(IUnknown)',
  18730. ' procedure DoIt(i: integer);',
  18731. ' end;',
  18732. ' TBird = class(IBird)',
  18733. ' procedure Fly(i: integer);',
  18734. ' end;',
  18735. 'procedure TObject.DoIt(i: integer); begin end;',
  18736. 'procedure TBird.Fly(i: integer); begin end;',
  18737. 'begin',
  18738. '']);
  18739. ConvertProgram;
  18740. CheckSource('TestClassInterface_AncestorIntf',
  18741. LinesToStr([ // statements
  18742. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDC4-8A2800000000}", ["DoIt"], null);',
  18743. 'rtl.createInterface(this, "IBird", "{B92D5841-6264-3AE3-BF20-000000000000}", ["Fly"], null);',
  18744. 'rtl.createClass(this, "TObject", null, function () {',
  18745. ' this.$init = function () {',
  18746. ' };',
  18747. ' this.$final = function () {',
  18748. ' };',
  18749. ' this.DoIt = function (i) {',
  18750. ' };',
  18751. ' rtl.addIntf(this, $mod.IUnknown);',
  18752. '});',
  18753. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18754. ' this.Fly = function (i) {',
  18755. ' };',
  18756. ' rtl.addIntf(this, $mod.IBird);',
  18757. ' rtl.addIntf(this, $mod.IUnknown);',
  18758. '});',
  18759. '']),
  18760. LinesToStr([ // $mod.$main
  18761. '']));
  18762. end;
  18763. procedure TTestModule.TestClassInterface_ImplReintroduce;
  18764. begin
  18765. StartProgram(false);
  18766. Add([
  18767. '{$interfaces corba}',
  18768. 'type',
  18769. ' integer = longint;',
  18770. ' IBird = interface',
  18771. ' procedure DoIt(i: integer);',
  18772. ' end;',
  18773. ' TObject = class',
  18774. ' procedure DoIt(i: integer);',
  18775. ' end;',
  18776. ' TBird = class(IBird)',
  18777. ' procedure DoIt(i: integer); virtual; reintroduce;',
  18778. ' end;',
  18779. 'procedure TObject.DoIt(i: integer); begin end;',
  18780. 'procedure TBird.DoIt(i: integer); begin end;',
  18781. 'begin',
  18782. '']);
  18783. ConvertProgram;
  18784. CheckSource('TestClassInterface_ImplReintroduce',
  18785. LinesToStr([ // statements
  18786. 'rtl.createInterface(this, "IBird", "{B92D5841-6264-3AE2-8594-000000000000}", ["DoIt"], null);',
  18787. 'rtl.createClass(this, "TObject", null, function () {',
  18788. ' this.$init = function () {',
  18789. ' };',
  18790. ' this.$final = function () {',
  18791. ' };',
  18792. ' this.DoIt = function (i) {',
  18793. ' };',
  18794. '});',
  18795. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18796. ' this.DoIt$1 = function (i) {',
  18797. ' };',
  18798. ' rtl.addIntf(this, $mod.IBird, {',
  18799. ' DoIt: "DoIt$1"',
  18800. ' });',
  18801. '});',
  18802. '']),
  18803. LinesToStr([ // $mod.$main
  18804. '']));
  18805. end;
  18806. procedure TTestModule.TestClassInterface_MethodResolution;
  18807. begin
  18808. StartProgram(false);
  18809. Add([
  18810. '{$interfaces corba}',
  18811. 'type',
  18812. ' IUnknown = interface',
  18813. ' procedure Walk(i: longint);',
  18814. ' end;',
  18815. ' IBird = interface(IUnknown)',
  18816. ' procedure Walk(b: boolean); overload;',
  18817. ' procedure Fly(s: string);',
  18818. ' end;',
  18819. ' TObject = class',
  18820. ' end;',
  18821. ' TBird = class(TObject,IBird)',
  18822. ' procedure IBird.Fly = Move;',
  18823. ' procedure IBird.Walk = Hop;',
  18824. ' procedure Hop(i: longint);',
  18825. ' procedure Move(s: string);',
  18826. ' procedure Hop(b: boolean);',
  18827. ' end;',
  18828. 'procedure TBird.Move(s: string); begin end;',
  18829. 'procedure TBird.Hop(i: longint); begin end;',
  18830. 'procedure TBird.Hop(b: boolean); begin end;',
  18831. 'var',
  18832. ' BirdIntf: IBird;',
  18833. 'begin',
  18834. ' BirdIntf.Walk(3);',
  18835. ' BirdIntf.Walk(true);',
  18836. ' BirdIntf.Fly(''abc'');',
  18837. '']);
  18838. ConvertProgram;
  18839. CheckSource('TestClassInterface_MethodResolution',
  18840. LinesToStr([ // statements
  18841. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDD7-23D600000000}", ["Walk"], null);',
  18842. 'rtl.createInterface(this, "IBird", "{CF8A4986-80F6-396E-AE88-000B86AAE208}", ["Walk$1", "Fly"], this.IUnknown);',
  18843. 'rtl.createClass(this, "TObject", null, function () {',
  18844. ' this.$init = function () {',
  18845. ' };',
  18846. ' this.$final = function () {',
  18847. ' };',
  18848. '});',
  18849. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18850. ' this.Hop = function (i) {',
  18851. ' };',
  18852. ' this.Move = function (s) {',
  18853. ' };',
  18854. ' this.Hop$1 = function (b) {',
  18855. ' };',
  18856. ' rtl.addIntf(this, $mod.IBird, {',
  18857. ' Walk$1: "Hop$1",',
  18858. ' Fly: "Move",',
  18859. ' Walk: "Hop"',
  18860. ' });',
  18861. '});',
  18862. 'this.BirdIntf = null;',
  18863. '']),
  18864. LinesToStr([ // $mod.$main
  18865. '$mod.BirdIntf.Walk(3);',
  18866. '$mod.BirdIntf.Walk$1(true);',
  18867. '$mod.BirdIntf.Fly("abc");',
  18868. '']));
  18869. end;
  18870. procedure TTestModule.TestClassInterface_AncestorMoreInterfaces;
  18871. begin
  18872. StartProgram(false);
  18873. Add([
  18874. '{$interfaces com}',
  18875. 'type',
  18876. ' IUnknown = interface',
  18877. ' function _AddRef: longint;',
  18878. ' procedure Walk;',
  18879. ' end;',
  18880. ' IBird = interface end;',
  18881. ' IDog = interface end;',
  18882. ' TObject = class(IBird,IDog)',
  18883. ' function _AddRef: longint; virtual; abstract;',
  18884. ' procedure Walk; virtual; abstract;',
  18885. ' end;',
  18886. ' TBird = class(IUnknown)',
  18887. ' end;',
  18888. 'begin',
  18889. '']);
  18890. ConvertProgram;
  18891. CheckSource('TestClassInterface_COM_AncestorLess',
  18892. LinesToStr([ // statements
  18893. 'rtl.createInterface(this, "IUnknown", "{8F2D5841-758A-322B-BDDF-21CD521DD723}", ["_AddRef", "Walk"], null);',
  18894. 'rtl.createInterface(this, "IBird", "{CCE11D4C-6504-3AEE-AE88-000B86AAE675}", [], this.IUnknown);',
  18895. 'rtl.createInterface(this, "IDog", "{CCE11D4C-6504-3AEE-AE88-000B8E5FC675}", [], this.IUnknown);',
  18896. 'rtl.createClass(this, "TObject", null, function () {',
  18897. ' this.$init = function () {',
  18898. ' };',
  18899. ' this.$final = function () {',
  18900. ' };',
  18901. ' rtl.addIntf(this, $mod.IBird);',
  18902. ' rtl.addIntf(this, $mod.IDog);',
  18903. '});',
  18904. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18905. ' rtl.addIntf(this, $mod.IUnknown);',
  18906. ' rtl.addIntf(this, $mod.IBird);',
  18907. ' rtl.addIntf(this, $mod.IDog);',
  18908. '});',
  18909. '']),
  18910. LinesToStr([ // $mod.$main
  18911. '']));
  18912. end;
  18913. procedure TTestModule.TestClassInterface_MethodOverride;
  18914. begin
  18915. StartProgram(false);
  18916. Add([
  18917. '{$interfaces corba}',
  18918. 'type',
  18919. ' IUnknown = interface',
  18920. ' [''{D6D98E5B-8A10-4FEC-856A-7BFC847FE74B}'']',
  18921. ' procedure Go;',
  18922. ' end;',
  18923. ' TObject = class(IUnknown)',
  18924. ' procedure Go; virtual; abstract;',
  18925. ' end;',
  18926. ' TBird = class',
  18927. ' procedure Go; override;',
  18928. ' end;',
  18929. ' TCat = class(TObject)',
  18930. ' procedure Go; override;',
  18931. ' end;',
  18932. ' TDog = class(TObject, IUnknown)',
  18933. ' procedure Go; override;',
  18934. ' end;',
  18935. 'procedure TBird.Go; begin end;',
  18936. 'procedure TCat.Go; begin end;',
  18937. 'procedure TDog.Go; begin end;',
  18938. 'begin',
  18939. '']);
  18940. ConvertProgram;
  18941. CheckSource('TestClassInterface_MethodOverride',
  18942. LinesToStr([ // statements
  18943. 'rtl.createInterface(this, "IUnknown", "{D6D98E5B-8A10-4FEC-856A-7BFC847FE74B}", ["Go"], null);',
  18944. 'rtl.createClass(this, "TObject", null, function () {',
  18945. ' this.$init = function () {',
  18946. ' };',
  18947. ' this.$final = function () {',
  18948. ' };',
  18949. ' rtl.addIntf(this, $mod.IUnknown);',
  18950. '});',
  18951. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18952. ' this.Go = function () {',
  18953. ' };',
  18954. ' rtl.addIntf(this, $mod.IUnknown);',
  18955. '});',
  18956. 'rtl.createClass(this, "TCat", this.TObject, function () {',
  18957. ' this.Go = function () {',
  18958. ' };',
  18959. ' rtl.addIntf(this, $mod.IUnknown);',
  18960. '});',
  18961. 'rtl.createClass(this, "TDog", this.TObject, function () {',
  18962. ' this.Go = function () {',
  18963. ' };',
  18964. ' rtl.addIntf(this, $mod.IUnknown);',
  18965. '});',
  18966. '']),
  18967. LinesToStr([ // $mod.$main
  18968. '']));
  18969. end;
  18970. procedure TTestModule.TestClassInterface_Corba_Delegation;
  18971. begin
  18972. StartProgram(false);
  18973. Add([
  18974. '{$interfaces corba}',
  18975. 'type',
  18976. ' IUnknown = interface',
  18977. ' end;',
  18978. ' IBird = interface(IUnknown)',
  18979. ' procedure Fly(s: string);',
  18980. ' end;',
  18981. ' IEagle = interface(IBird)',
  18982. ' end;',
  18983. ' IDove = interface(IBird)',
  18984. ' end;',
  18985. ' ISwallow = interface(IBird)',
  18986. ' end;',
  18987. ' TObject = class',
  18988. ' end;',
  18989. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  18990. ' procedure Fly(s: string); virtual; abstract;',
  18991. ' end;',
  18992. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  18993. ' FBirdIntf: IBird;',
  18994. ' property BirdIntf: IBird read FBirdIntf implements IBird;',
  18995. ' function GetEagleIntf: IEagle; virtual; abstract;',
  18996. ' property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  18997. ' FDoveObj: TBird;',
  18998. ' property DoveObj: TBird read FDoveObj implements IDove;',
  18999. ' function GetSwallowObj: TBird; virtual; abstract;',
  19000. ' property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  19001. ' end;',
  19002. 'begin',
  19003. '']);
  19004. ConvertProgram;
  19005. CheckSource('TestClassInterface_Corba_Delegation',
  19006. LinesToStr([ // statements
  19007. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19008. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  19009. 'rtl.createInterface(this, "IEagle", "{489289DE-FDE2-34A6-8288-39119022B1B4}", [], this.IBird);',
  19010. 'rtl.createInterface(this, "IDove", "{489289DE-FDE2-34A6-8288-39118EF16074}", [], this.IBird);',
  19011. 'rtl.createInterface(this, "ISwallow", "{B89289DE-FDE2-34A6-8288-3911CBDCB359}", [], this.IBird);',
  19012. 'rtl.createClass(this, "TObject", null, function () {',
  19013. ' this.$init = function () {',
  19014. ' };',
  19015. ' this.$final = function () {',
  19016. ' };',
  19017. '});',
  19018. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  19019. ' rtl.addIntf(this, $mod.IBird);',
  19020. ' rtl.addIntf(this, $mod.IEagle);',
  19021. ' rtl.addIntf(this, $mod.IDove);',
  19022. ' rtl.addIntf(this, $mod.ISwallow);',
  19023. '});',
  19024. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  19025. ' this.$init = function () {',
  19026. ' $mod.TObject.$init.call(this);',
  19027. ' this.FBirdIntf = null;',
  19028. ' this.FDoveObj = null;',
  19029. ' };',
  19030. ' this.$final = function () {',
  19031. ' this.FBirdIntf = undefined;',
  19032. ' this.FDoveObj = undefined;',
  19033. ' $mod.TObject.$final.call(this);',
  19034. ' };',
  19035. ' this.$intfmaps = {',
  19036. ' "{478D080B-C0F6-396E-AE88-000B87785B07}": function () {',
  19037. ' return this.FBirdIntf;',
  19038. ' },',
  19039. ' "{489289DE-FDE2-34A6-8288-39119022B1B4}": function () {',
  19040. ' return this.GetEagleIntf();',
  19041. ' },',
  19042. ' "{489289DE-FDE2-34A6-8288-39118EF16074}": function () {',
  19043. ' return rtl.getIntfT(this.FDoveObj, $mod.IDove);',
  19044. ' },',
  19045. ' "{B89289DE-FDE2-34A6-8288-3911CBDCB359}": function () {',
  19046. ' return rtl.getIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  19047. ' }',
  19048. ' };',
  19049. '});',
  19050. '']),
  19051. LinesToStr([ // $mod.$main
  19052. '']));
  19053. end;
  19054. procedure TTestModule.TestClassInterface_Corba_DelegationStatic;
  19055. begin
  19056. StartProgram(false);
  19057. Add([
  19058. '{$interfaces corba}',
  19059. 'type',
  19060. ' IUnknown = interface',
  19061. ' end;',
  19062. ' IBird = interface(IUnknown)',
  19063. ' procedure Fly(s: string);',
  19064. ' end;',
  19065. ' IEagle = interface(IBird)',
  19066. ' end;',
  19067. ' IDove = interface(IBird)',
  19068. ' end;',
  19069. ' ISwallow = interface(IBird)',
  19070. ' end;',
  19071. ' TObject = class',
  19072. ' end;',
  19073. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  19074. ' procedure Fly(s: string); virtual; abstract;',
  19075. ' end;',
  19076. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  19077. ' private',
  19078. ' class var FBirdIntf: IBird;',
  19079. ' class var FDoveObj: TBird;',
  19080. ' class function GetEagleIntf: IEagle; virtual; abstract;',
  19081. ' class function GetSwallowObj: TBird; virtual; abstract;',
  19082. ' protected',
  19083. ' class property BirdIntf: IBird read FBirdIntf implements IBird;',
  19084. ' class property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  19085. ' class property DoveObj: TBird read FDoveObj implements IDove;',
  19086. ' class property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  19087. ' end;',
  19088. 'begin',
  19089. '']);
  19090. ConvertProgram;
  19091. CheckSource('TestClassInterface_Corba_DelegationStatic',
  19092. LinesToStr([ // statements
  19093. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19094. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  19095. 'rtl.createInterface(this, "IEagle", "{489289DE-FDE2-34A6-8288-39119022B1B4}", [], this.IBird);',
  19096. 'rtl.createInterface(this, "IDove", "{489289DE-FDE2-34A6-8288-39118EF16074}", [], this.IBird);',
  19097. 'rtl.createInterface(this, "ISwallow", "{B89289DE-FDE2-34A6-8288-3911CBDCB359}", [], this.IBird);',
  19098. 'rtl.createClass(this, "TObject", null, function () {',
  19099. ' this.$init = function () {',
  19100. ' };',
  19101. ' this.$final = function () {',
  19102. ' };',
  19103. '});',
  19104. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  19105. ' rtl.addIntf(this, $mod.IBird);',
  19106. ' rtl.addIntf(this, $mod.IEagle);',
  19107. ' rtl.addIntf(this, $mod.IDove);',
  19108. ' rtl.addIntf(this, $mod.ISwallow);',
  19109. '});',
  19110. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  19111. ' this.FBirdIntf = null;',
  19112. ' this.FDoveObj = null;',
  19113. ' this.$intfmaps = {',
  19114. ' "{478D080B-C0F6-396E-AE88-000B87785B07}": function () {',
  19115. ' return this.FBirdIntf;',
  19116. ' },',
  19117. ' "{489289DE-FDE2-34A6-8288-39119022B1B4}": function () {',
  19118. ' return this.GetEagleIntf();',
  19119. ' },',
  19120. ' "{489289DE-FDE2-34A6-8288-39118EF16074}": function () {',
  19121. ' return rtl.getIntfT(this.FDoveObj, $mod.IDove);',
  19122. ' },',
  19123. ' "{B89289DE-FDE2-34A6-8288-3911CBDCB359}": function () {',
  19124. ' return rtl.getIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  19125. ' }',
  19126. ' };',
  19127. '});',
  19128. '']),
  19129. LinesToStr([ // $mod.$main
  19130. '']));
  19131. end;
  19132. procedure TTestModule.TestClassInterface_Corba_Operators;
  19133. begin
  19134. StartProgram(false);
  19135. Add([
  19136. '{$interfaces corba}',
  19137. 'type',
  19138. ' IUnknown = interface',
  19139. ' end;',
  19140. ' IBird = interface(IUnknown)',
  19141. ' function GetItems(Index: longint): longint;',
  19142. ' procedure SetItems(Index: longint; Value: longint);',
  19143. ' property Items[Index: longint]: longint read GetItems write SetItems; default;',
  19144. ' end;',
  19145. ' TObject = class',
  19146. ' end;',
  19147. ' TBird = class(TObject,IBird)',
  19148. ' function GetItems(Index: longint): longint; virtual; abstract;',
  19149. ' procedure SetItems(Index: longint; Value: longint); virtual; abstract;',
  19150. ' end;',
  19151. 'var',
  19152. ' IntfVar: IBird = nil;',
  19153. ' IntfVar2: IBird;',
  19154. ' ObjVar: TBird;',
  19155. ' v: JSValue;',
  19156. 'begin',
  19157. ' IntfVar:=nil;',
  19158. ' IntfVar[3]:=IntfVar[4];',
  19159. ' if Assigned(IntfVar) then ;',
  19160. ' IntfVar:=IntfVar2;',
  19161. ' IntfVar:=ObjVar;',
  19162. ' if IntfVar=IntfVar2 then ;',
  19163. ' if IntfVar<>IntfVar2 then ;',
  19164. ' if IntfVar is IBird then ;',
  19165. ' if IntfVar is TBird then ;',
  19166. ' if ObjVar is IBird then ;',
  19167. ' IntfVar:=IntfVar2 as IBird;',
  19168. ' ObjVar:=IntfVar2 as TBird;',
  19169. ' IntfVar:=ObjVar as IBird;',
  19170. ' IntfVar:=IBird(IntfVar2);',
  19171. ' ObjVar:=TBird(IntfVar);',
  19172. ' IntfVar:=IBird(ObjVar);',
  19173. ' v:=IntfVar;',
  19174. ' IntfVar:=IBird(v);',
  19175. ' if v is IBird then ;',
  19176. ' v:=JSValue(IntfVar);',
  19177. ' v:=IBird;',
  19178. '']);
  19179. ConvertProgram;
  19180. CheckSource('TestClassInterface_Corba_Operators',
  19181. LinesToStr([ // statements
  19182. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19183. 'rtl.createInterface(this, "IBird", "{D53FED90-DE59-3202-B1AE-000B87785B08}", ["GetItems", "SetItems"], this.IUnknown);',
  19184. 'rtl.createClass(this, "TObject", null, function () {',
  19185. ' this.$init = function () {',
  19186. ' };',
  19187. ' this.$final = function () {',
  19188. ' };',
  19189. '});',
  19190. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  19191. ' rtl.addIntf(this, $mod.IBird);',
  19192. '});',
  19193. 'this.IntfVar = null;',
  19194. 'this.IntfVar2 = null;',
  19195. 'this.ObjVar = null;',
  19196. 'this.v = undefined;',
  19197. '']),
  19198. LinesToStr([ // $mod.$main
  19199. '$mod.IntfVar = null;',
  19200. '$mod.IntfVar.SetItems(3, $mod.IntfVar.GetItems(4));',
  19201. 'if ($mod.IntfVar != null) ;',
  19202. '$mod.IntfVar = $mod.IntfVar2;',
  19203. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar,$mod.IBird);',
  19204. 'if ($mod.IntfVar === $mod.IntfVar2) ;',
  19205. 'if ($mod.IntfVar !== $mod.IntfVar2) ;',
  19206. 'if ($mod.IBird.isPrototypeOf($mod.IntfVar)) ;',
  19207. 'if (rtl.intfIsClass($mod.IntfVar, $mod.TBird)) ;',
  19208. 'if (rtl.getIntfT($mod.ObjVar, $mod.IBird) !== null) ;',
  19209. '$mod.IntfVar = rtl.as($mod.IntfVar2, $mod.IBird);',
  19210. '$mod.ObjVar = rtl.intfAsClass($mod.IntfVar2, $mod.TBird);',
  19211. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar, $mod.IBird);',
  19212. '$mod.IntfVar = $mod.IntfVar2;',
  19213. '$mod.ObjVar = rtl.intfToClass($mod.IntfVar, $mod.TBird);',
  19214. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar, $mod.IBird);',
  19215. '$mod.v = $mod.IntfVar;',
  19216. '$mod.IntfVar = rtl.getObject($mod.v);',
  19217. 'if (rtl.isExt($mod.v, $mod.IBird, 1)) ;',
  19218. '$mod.v = $mod.IntfVar;',
  19219. '$mod.v = $mod.IBird;',
  19220. '']));
  19221. end;
  19222. procedure TTestModule.TestClassInterface_Corba_Args;
  19223. begin
  19224. StartProgram(false);
  19225. Add([
  19226. '{$interfaces corba}',
  19227. 'type',
  19228. ' IUnknown = interface',
  19229. ' end;',
  19230. ' IBird = interface(IUnknown)',
  19231. ' end;',
  19232. ' TObject = class',
  19233. ' end;',
  19234. ' TBird = class(TObject,IBird)',
  19235. ' end;',
  19236. 'procedure DoIt(var u; i: IBird; const j: IBird);',
  19237. 'begin',
  19238. ' DoIt(i,i,i);',
  19239. 'end;',
  19240. 'procedure Change(var i: IBird; out j: IBird);',
  19241. 'begin',
  19242. ' DoIt(i,i,i);',
  19243. ' Change(i,i);',
  19244. 'end;',
  19245. 'var',
  19246. ' i: IBird;',
  19247. ' o: TBird;',
  19248. 'begin',
  19249. ' DoIt(i,i,i);',
  19250. ' Change(i,i);',
  19251. ' DoIt(o,o,o);',
  19252. '']);
  19253. ConvertProgram;
  19254. CheckSource('TestClassInterface_Corba_Args',
  19255. LinesToStr([ // statements
  19256. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19257. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], this.IUnknown);',
  19258. 'rtl.createClass(this, "TObject", null, function () {',
  19259. ' this.$init = function () {',
  19260. ' };',
  19261. ' this.$final = function () {',
  19262. ' };',
  19263. '});',
  19264. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  19265. ' rtl.addIntf(this, $mod.IBird);',
  19266. '});',
  19267. 'this.DoIt = function (u, i, j) {',
  19268. ' $mod.DoIt({',
  19269. ' get: function () {',
  19270. ' return i;',
  19271. ' },',
  19272. ' set: function (v) {',
  19273. ' i = v;',
  19274. ' }',
  19275. ' }, i, i);',
  19276. '};',
  19277. 'this.Change = function (i, j) {',
  19278. ' $mod.DoIt(i, i.get(), i.get());',
  19279. ' $mod.Change(i, i);',
  19280. '};',
  19281. 'this.i = null;',
  19282. 'this.o = null;',
  19283. '']),
  19284. LinesToStr([ // $mod.$main
  19285. '$mod.DoIt({',
  19286. ' p: $mod,',
  19287. ' get: function () {',
  19288. ' return this.p.i;',
  19289. ' },',
  19290. ' set: function (v) {',
  19291. ' this.p.i = v;',
  19292. ' }',
  19293. '}, $mod.i, $mod.i);',
  19294. '$mod.Change({',
  19295. ' p: $mod,',
  19296. ' get: function () {',
  19297. ' return this.p.i;',
  19298. ' },',
  19299. ' set: function (v) {',
  19300. ' this.p.i = v;',
  19301. ' }',
  19302. '}, {',
  19303. ' p: $mod,',
  19304. ' get: function () {',
  19305. ' return this.p.i;',
  19306. ' },',
  19307. ' set: function (v) {',
  19308. ' this.p.i = v;',
  19309. ' }',
  19310. '});',
  19311. '$mod.DoIt({',
  19312. ' p: $mod,',
  19313. ' get: function () {',
  19314. ' return this.p.o;',
  19315. ' },',
  19316. ' set: function (v) {',
  19317. ' this.p.o = v;',
  19318. ' }',
  19319. '}, rtl.getIntfT($mod.o, $mod.IBird), rtl.getIntfT($mod.o, $mod.IBird));',
  19320. '']));
  19321. end;
  19322. procedure TTestModule.TestClassInterface_Corba_ForIn;
  19323. begin
  19324. StartProgram(false);
  19325. Add([
  19326. '{$interfaces corba}',
  19327. 'type',
  19328. ' IUnknown = interface end;',
  19329. ' TObject = class',
  19330. ' Id: longint;',
  19331. ' end;',
  19332. ' IEnumerator = interface(IUnknown)',
  19333. ' function GetCurrent: TObject;',
  19334. ' function MoveNext: Boolean;',
  19335. ' property Current: TObject read GetCurrent;',
  19336. ' end;',
  19337. ' IEnumerable = interface(IUnknown)',
  19338. ' function GetEnumerator: IEnumerator;',
  19339. ' end;',
  19340. 'var',
  19341. ' o: TObject;',
  19342. ' i: IEnumerable;',
  19343. 'begin',
  19344. ' for o in i do o.Id:=3;',
  19345. '']);
  19346. ConvertProgram;
  19347. CheckSource('TestClassInterface_Corba_ForIn',
  19348. LinesToStr([ // statements
  19349. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19350. 'rtl.createClass(this, "TObject", null, function () {',
  19351. ' this.$init = function () {',
  19352. ' this.Id = 0;',
  19353. ' };',
  19354. ' this.$final = function () {',
  19355. ' };',
  19356. '});',
  19357. 'rtl.createInterface(this, "IEnumerator", "{95D7745D-ED61-3F13-BBE4-07708161999E}", ["GetCurrent", "MoveNext"], this.IUnknown);',
  19358. 'rtl.createInterface(this, "IEnumerable", "{8CC9D45D-ED7D-3B73-96B6-290B931BB19E}", ["GetEnumerator"], this.IUnknown);',
  19359. 'this.o = null;',
  19360. 'this.i = null;',
  19361. '']),
  19362. LinesToStr([ // $mod.$main
  19363. 'var $in = $mod.i.GetEnumerator();',
  19364. 'while ($in.MoveNext()) {',
  19365. ' $mod.o = $in.GetCurrent();',
  19366. ' $mod.o.Id = 3;',
  19367. '};',
  19368. '']));
  19369. end;
  19370. procedure TTestModule.TestClassInterface_COM_AssignVar;
  19371. begin
  19372. StartProgram(false);
  19373. Add([
  19374. '{$interfaces com}',
  19375. 'type',
  19376. ' IUnknown = interface',
  19377. ' function _AddRef: longint;',
  19378. ' function _Release: longint;',
  19379. ' end;',
  19380. ' TObject = class(IUnknown)',
  19381. ' function _AddRef: longint; virtual; abstract;',
  19382. ' function _Release: longint; virtual; abstract;',
  19383. ' end;',
  19384. 'var',
  19385. ' i: IUnknown;',
  19386. 'procedure DoGlobal(o: TObject);',
  19387. 'begin',
  19388. ' i:=nil;',
  19389. ' i:=o;',
  19390. ' i:=i;',
  19391. 'end;',
  19392. 'procedure DoLocal(o: TObject);',
  19393. 'const k: IUnknown = nil;',
  19394. 'var j: IUnknown;',
  19395. 'begin',
  19396. ' k:=o;',
  19397. ' k:=i;',
  19398. ' j:=o;',
  19399. ' j:=i;',
  19400. 'end;',
  19401. 'var o: TObject;',
  19402. 'begin',
  19403. ' i:=nil;',
  19404. ' i:=o;',
  19405. '']);
  19406. ConvertProgram;
  19407. CheckSource('TestClassInterface_COM_AssignVar',
  19408. LinesToStr([ // statements
  19409. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19410. 'rtl.createClass(this, "TObject", null, function () {',
  19411. ' this.$init = function () {',
  19412. ' };',
  19413. ' this.$final = function () {',
  19414. ' };',
  19415. ' rtl.addIntf(this, $mod.IUnknown);',
  19416. '});',
  19417. 'this.i = null;',
  19418. 'this.DoGlobal = function (o) {',
  19419. ' rtl.setIntfP($mod, "i", null);',
  19420. ' rtl.setIntfP($mod, "i", rtl.queryIntfT(o, $mod.IUnknown), true);',
  19421. ' rtl.setIntfP($mod, "i", $mod.i);',
  19422. '};',
  19423. 'var k = null;',
  19424. 'this.DoLocal = function (o) {',
  19425. ' var j = null;',
  19426. ' try{',
  19427. ' k = rtl.setIntfL(k, rtl.queryIntfT(o, $mod.IUnknown), true);',
  19428. ' k = rtl.setIntfL(k, $mod.i);',
  19429. ' j = rtl.setIntfL(j, rtl.queryIntfT(o, $mod.IUnknown), true);',
  19430. ' j = rtl.setIntfL(j, $mod.i);',
  19431. ' }finally{',
  19432. ' rtl._Release(j);',
  19433. ' };',
  19434. '};',
  19435. 'this.o = null;',
  19436. '']),
  19437. LinesToStr([ // $mod.$main
  19438. 'rtl.setIntfP($mod, "i", null);',
  19439. 'rtl.setIntfP($mod, "i", rtl.queryIntfT($mod.o, $mod.IUnknown), true);',
  19440. '']));
  19441. end;
  19442. procedure TTestModule.TestClassInterface_COM_AssignArg;
  19443. begin
  19444. StartProgram(false);
  19445. Add([
  19446. '{$interfaces com}',
  19447. 'type',
  19448. ' IUnknown = interface',
  19449. ' function _AddRef: longint;',
  19450. ' function _Release: longint;',
  19451. ' end;',
  19452. ' TObject = class(IUnknown)',
  19453. ' function _AddRef: longint; virtual; abstract;',
  19454. ' function _Release: longint; virtual; abstract;',
  19455. ' end;',
  19456. 'procedure DoDefault(i, j: IUnknown);',
  19457. 'begin',
  19458. ' i:=nil;',
  19459. ' i:=j;',
  19460. 'end;',
  19461. 'begin',
  19462. '']);
  19463. ConvertProgram;
  19464. CheckSource('TestClassInterface_COM_AssignArg',
  19465. LinesToStr([ // statements
  19466. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19467. 'rtl.createClass(this, "TObject", null, function () {',
  19468. ' this.$init = function () {',
  19469. ' };',
  19470. ' this.$final = function () {',
  19471. ' };',
  19472. ' rtl.addIntf(this, $mod.IUnknown);',
  19473. '});',
  19474. 'this.DoDefault = function (i, j) {',
  19475. ' rtl._AddRef(i);',
  19476. ' try {',
  19477. ' i = rtl.setIntfL(i, null);',
  19478. ' i = rtl.setIntfL(i, j);',
  19479. ' } finally {',
  19480. ' rtl._Release(i);',
  19481. ' };',
  19482. '};',
  19483. '']),
  19484. LinesToStr([ // $mod.$main
  19485. '']));
  19486. end;
  19487. procedure TTestModule.TestClassInterface_COM_FunctionResult;
  19488. begin
  19489. StartProgram(false);
  19490. Add([
  19491. '{$interfaces com}',
  19492. 'type',
  19493. ' IUnknown = interface',
  19494. ' function _AddRef: longint;',
  19495. ' function _Release: longint;',
  19496. ' end;',
  19497. ' TObject = class(IUnknown)',
  19498. ' function _AddRef: longint; virtual; abstract;',
  19499. ' function _Release: longint; virtual; abstract;',
  19500. ' end;',
  19501. 'function DoDefault(i: IUnknown): IUnknown;',
  19502. 'begin',
  19503. ' Result:=i;',
  19504. ' if Result<>nil then exit;',
  19505. 'end;',
  19506. 'begin',
  19507. '']);
  19508. ConvertProgram;
  19509. CheckSource('TestClassInterface_COM_FunctionResult',
  19510. LinesToStr([ // statements
  19511. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19512. 'rtl.createClass(this, "TObject", null, function () {',
  19513. ' this.$init = function () {',
  19514. ' };',
  19515. ' this.$final = function () {',
  19516. ' };',
  19517. ' rtl.addIntf(this, $mod.IUnknown);',
  19518. '});',
  19519. 'this.DoDefault = function (i) {',
  19520. ' var Result = null;',
  19521. ' var $ok = false;',
  19522. ' try {',
  19523. ' Result = rtl.setIntfL(Result, i);',
  19524. ' if(Result !== null){',
  19525. ' $ok = true;',
  19526. ' return Result;',
  19527. ' };',
  19528. ' $ok = true;',
  19529. ' } finally {',
  19530. ' if(!$ok) rtl._Release(Result);',
  19531. ' };',
  19532. ' return Result;',
  19533. '};',
  19534. '']),
  19535. LinesToStr([ // $mod.$main
  19536. '']));
  19537. end;
  19538. procedure TTestModule.TestClassInterface_COM_InheritedFuncResult;
  19539. begin
  19540. StartProgram(false);
  19541. Add([
  19542. '{$interfaces com}',
  19543. 'type',
  19544. ' IUnknown = interface',
  19545. ' function _AddRef: longint;',
  19546. ' function _Release: longint;',
  19547. ' end;',
  19548. ' TObject = class(IUnknown)',
  19549. ' function _AddRef: longint; virtual; abstract;',
  19550. ' function _Release: longint; virtual; abstract;',
  19551. ' function GetIntf: IUnknown; virtual;',
  19552. ' end;',
  19553. ' TMouse = class',
  19554. ' function GetIntf: IUnknown; override;',
  19555. ' end;',
  19556. 'function TObject.GetIntf: IUnknown; begin end;',
  19557. 'function TMouse.GetIntf: IUnknown;',
  19558. 'var i: IUnknown;',
  19559. 'begin',
  19560. ' inherited;',
  19561. ' inherited GetIntf;',
  19562. ' inherited GetIntf();',
  19563. ' Result:=inherited GetIntf;',
  19564. ' Result:=inherited GetIntf();',
  19565. ' i:=inherited GetIntf;',
  19566. ' i:=inherited GetIntf();',
  19567. 'end;',
  19568. 'begin',
  19569. '']);
  19570. ConvertProgram;
  19571. CheckSource('TestClassInterface_COM_InheritedFuncResult',
  19572. LinesToStr([ // statements
  19573. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19574. 'rtl.createClass(this, "TObject", null, function () {',
  19575. ' this.$init = function () {',
  19576. ' };',
  19577. ' this.$final = function () {',
  19578. ' };',
  19579. ' this.GetIntf = function () {',
  19580. ' var Result = null;',
  19581. ' return Result;',
  19582. ' };',
  19583. ' rtl.addIntf(this, $mod.IUnknown);',
  19584. '});',
  19585. 'rtl.createClass(this, "TMouse", this.TObject, function () {',
  19586. ' this.GetIntf = function () {',
  19587. ' var Result = null;',
  19588. ' var i = null;',
  19589. ' var $ir = rtl.createIntfRefs();',
  19590. ' var $ok = false;',
  19591. ' try {',
  19592. ' $ir.ref(1, $mod.TObject.GetIntf.call(this));',
  19593. ' $ir.ref(2, $mod.TObject.GetIntf.call(this));',
  19594. ' $ir.ref(3, $mod.TObject.GetIntf.call(this));',
  19595. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  19596. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  19597. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  19598. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  19599. ' $ok = true;',
  19600. ' } finally {',
  19601. ' $ir.free();',
  19602. ' rtl._Release(i);',
  19603. ' if (!$ok) rtl._Release(Result);',
  19604. ' };',
  19605. ' return Result;',
  19606. ' };',
  19607. ' rtl.addIntf(this, $mod.IUnknown);',
  19608. '});',
  19609. '']),
  19610. LinesToStr([ // $mod.$main
  19611. '']));
  19612. end;
  19613. procedure TTestModule.TestClassInterface_COM_IsAsTypeCasts;
  19614. begin
  19615. StartProgram(false);
  19616. Add([
  19617. '{$interfaces com}',
  19618. 'type',
  19619. ' IUnknown = interface',
  19620. ' function _AddRef: longint;',
  19621. ' function _Release: longint;',
  19622. ' end;',
  19623. ' TObject = class(IUnknown)',
  19624. ' function _AddRef: longint; virtual; abstract;',
  19625. ' function _Release: longint; virtual; abstract;',
  19626. ' end;',
  19627. 'procedure DoDefault(i, j: IUnknown; o: TObject);',
  19628. 'begin',
  19629. ' if i is IUnknown then ;',
  19630. ' if o is IUnknown then ;',
  19631. ' if i is TObject then ;',
  19632. ' i:=j as IUnknown;',
  19633. ' i:=o as IUnknown;',
  19634. ' o:=j as TObject;',
  19635. ' i:=IUnknown(j);',
  19636. ' i:=IUnknown(o);',
  19637. ' o:=TObject(i);',
  19638. 'end;',
  19639. 'begin',
  19640. '']);
  19641. ConvertProgram;
  19642. CheckSource('TestClassInterface_COM_IsAsTypeCasts',
  19643. LinesToStr([ // statements
  19644. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19645. 'rtl.createClass(this, "TObject", null, function () {',
  19646. ' this.$init = function () {',
  19647. ' };',
  19648. ' this.$final = function () {',
  19649. ' };',
  19650. ' rtl.addIntf(this, $mod.IUnknown);',
  19651. '});',
  19652. 'this.DoDefault = function (i, j, o) {',
  19653. ' rtl._AddRef(i);',
  19654. ' try {',
  19655. ' if (rtl.intfIsIntfT(i, $mod.IUnknown)) ;',
  19656. ' if (rtl.queryIntfIsT(o, $mod.IUnknown)) ;',
  19657. ' if (rtl.intfIsClass(i, $mod.TObject)) ;',
  19658. ' i = rtl.setIntfL(i, rtl.intfAsIntfT(j, $mod.IUnknown));',
  19659. ' i = rtl.setIntfL(i, rtl.queryIntfT(o, $mod.IUnknown), true);',
  19660. ' o = rtl.intfAsClass(j, $mod.TObject);',
  19661. ' i = rtl.setIntfL(i, j);',
  19662. ' i = rtl.setIntfL(i, rtl.queryIntfT(o, $mod.IUnknown), true);',
  19663. ' o = rtl.intfToClass(i, $mod.TObject);',
  19664. ' } finally {',
  19665. ' rtl._Release(i);',
  19666. ' };',
  19667. '};',
  19668. '']),
  19669. LinesToStr([ // $mod.$main
  19670. '']));
  19671. end;
  19672. procedure TTestModule.TestClassInterface_COM_PassAsArg;
  19673. begin
  19674. StartProgram(false);
  19675. Add([
  19676. '{$interfaces com}',
  19677. 'type',
  19678. ' IUnknown = interface',
  19679. ' function _AddRef: longint;',
  19680. ' function _Release: longint;',
  19681. ' end;',
  19682. ' TObject = class(IUnknown)',
  19683. ' function _AddRef: longint; virtual; abstract;',
  19684. ' function _Release: longint; virtual; abstract;',
  19685. ' end;',
  19686. 'procedure DoIt(v: IUnknown; const j: IUnknown; var k: IUnknown; out l: IUnknown);',
  19687. 'var o: TObject;',
  19688. 'begin',
  19689. ' DoIt(v,v,v,v);',
  19690. ' DoIt(o,o,k,k);',
  19691. 'end;',
  19692. 'procedure DoSome;',
  19693. 'var v: IUnknown;',
  19694. 'begin',
  19695. ' DoIt(v,v,v,v);',
  19696. 'end;',
  19697. 'var i: IUnknown;',
  19698. 'begin',
  19699. ' DoIt(i,i,i,i);',
  19700. '']);
  19701. ConvertProgram;
  19702. CheckSource('TestClassInterface_COM_PassAsArg',
  19703. LinesToStr([ // statements
  19704. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19705. 'rtl.createClass(this, "TObject", null, function () {',
  19706. ' this.$init = function () {',
  19707. ' };',
  19708. ' this.$final = function () {',
  19709. ' };',
  19710. ' rtl.addIntf(this, $mod.IUnknown);',
  19711. '});',
  19712. 'this.DoIt = function (v, j, k, l) {',
  19713. ' var o = null;',
  19714. ' var $ir = rtl.createIntfRefs();',
  19715. ' rtl._AddRef(v);',
  19716. ' try {',
  19717. ' $mod.DoIt(v, v, {',
  19718. ' get: function () {',
  19719. ' return v;',
  19720. ' },',
  19721. ' set: function (w) {',
  19722. ' v = rtl.setIntfL(v, w);',
  19723. ' }',
  19724. ' }, {',
  19725. ' get: function () {',
  19726. ' return v;',
  19727. ' },',
  19728. ' set: function (w) {',
  19729. ' v = rtl.setIntfL(v, w);',
  19730. ' }',
  19731. ' });',
  19732. ' $mod.DoIt($ir.ref(1, rtl.queryIntfT(o, $mod.IUnknown)), $ir.ref(2, rtl.queryIntfT(o, $mod.IUnknown)), k, k);',
  19733. ' } finally {',
  19734. ' $ir.free();',
  19735. ' rtl._Release(v);',
  19736. ' };',
  19737. '};',
  19738. 'this.DoSome = function () {',
  19739. ' var v = null;',
  19740. ' try {',
  19741. ' $mod.DoIt(v, v, {',
  19742. ' get: function () {',
  19743. ' return v;',
  19744. ' },',
  19745. ' set: function (w) {',
  19746. ' v = rtl.setIntfL(v, w);',
  19747. ' }',
  19748. ' }, {',
  19749. ' get: function () {',
  19750. ' return v;',
  19751. ' },',
  19752. ' set: function (w) {',
  19753. ' v = rtl.setIntfL(v, w);',
  19754. ' }',
  19755. ' });',
  19756. ' } finally {',
  19757. ' rtl._Release(v);',
  19758. ' };',
  19759. '};',
  19760. 'this.i = null;',
  19761. '']),
  19762. LinesToStr([ // $mod.$main
  19763. '$mod.DoIt($mod.i, $mod.i, {',
  19764. ' p: $mod,',
  19765. ' get: function () {',
  19766. ' return this.p.i;',
  19767. ' },',
  19768. ' set: function (v) {',
  19769. ' rtl.setIntfP(this.p, "i", v);',
  19770. ' }',
  19771. '}, {',
  19772. ' p: $mod,',
  19773. ' get: function () {',
  19774. ' return this.p.i;',
  19775. ' },',
  19776. ' set: function (v) {',
  19777. ' rtl.setIntfP(this.p, "i", v);',
  19778. ' }',
  19779. '});',
  19780. '']));
  19781. end;
  19782. procedure TTestModule.TestClassInterface_COM_PassToUntypedParam;
  19783. begin
  19784. StartProgram(false);
  19785. Add([
  19786. '{$interfaces com}',
  19787. 'type',
  19788. ' IUnknown = interface',
  19789. ' function _AddRef: longint;',
  19790. ' function _Release: longint;',
  19791. ' end;',
  19792. ' TObject = class(IUnknown)',
  19793. ' function _AddRef: longint; virtual; abstract;',
  19794. ' function _Release: longint; virtual; abstract;',
  19795. ' end;',
  19796. 'procedure DoIt(out i);',
  19797. 'begin end;',
  19798. 'procedure DoSome;',
  19799. 'var v: IUnknown;',
  19800. 'begin',
  19801. ' DoIt(v);',
  19802. 'end;',
  19803. 'function GetIt: IUnknown;',
  19804. 'begin',
  19805. ' DoIt(Result);',
  19806. 'end;',
  19807. 'var i: IUnknown;',
  19808. 'begin',
  19809. ' DoIt(i);',
  19810. '']);
  19811. ConvertProgram;
  19812. CheckSource('TestClassInterface_COM_PassToUntypedParam',
  19813. LinesToStr([ // statements
  19814. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19815. 'rtl.createClass(this, "TObject", null, function () {',
  19816. ' this.$init = function () {',
  19817. ' };',
  19818. ' this.$final = function () {',
  19819. ' };',
  19820. ' rtl.addIntf(this, $mod.IUnknown);',
  19821. '});',
  19822. 'this.DoIt = function (i) {',
  19823. '};',
  19824. 'this.DoSome = function () {',
  19825. ' var v = null;',
  19826. ' try {',
  19827. ' $mod.DoIt({',
  19828. ' get: function () {',
  19829. ' return v;',
  19830. ' },',
  19831. ' set: function (w) {',
  19832. ' v = w;',
  19833. ' }',
  19834. ' });',
  19835. ' } finally {',
  19836. ' rtl._Release(v);',
  19837. ' };',
  19838. '};',
  19839. 'this.GetIt = function () {',
  19840. ' var Result = null;',
  19841. ' var $ok = false;',
  19842. ' try {',
  19843. ' $mod.DoIt({',
  19844. ' get: function () {',
  19845. ' return Result;',
  19846. ' },',
  19847. ' set: function (v) {',
  19848. ' Result = v;',
  19849. ' }',
  19850. ' });',
  19851. ' $ok = true;',
  19852. ' } finally {',
  19853. ' if (!$ok) rtl._Release(Result);',
  19854. ' };',
  19855. ' return Result;',
  19856. '};',
  19857. 'this.i = null;',
  19858. '']),
  19859. LinesToStr([ // $mod.$main
  19860. 'try {',
  19861. ' $mod.DoIt({',
  19862. ' p: $mod,',
  19863. ' get: function () {',
  19864. ' return this.p.i;',
  19865. ' },',
  19866. ' set: function (v) {',
  19867. ' this.p.i = v;',
  19868. ' }',
  19869. ' });',
  19870. '} finally {',
  19871. ' rtl._Release($mod.i);',
  19872. '};',
  19873. '']));
  19874. end;
  19875. procedure TTestModule.TestClassInterface_COM_FunctionInExpr;
  19876. begin
  19877. StartProgram(false);
  19878. Add([
  19879. '{$interfaces com}',
  19880. 'type',
  19881. ' IUnknown = interface',
  19882. ' function _AddRef: longint;',
  19883. ' function _Release: longint;',
  19884. ' end;',
  19885. ' TObject = class(IUnknown)',
  19886. ' function _AddRef: longint; virtual; abstract;',
  19887. ' function _Release: longint; virtual; abstract;',
  19888. ' end;',
  19889. 'function GetIt: IUnknown;',
  19890. 'begin',
  19891. 'end;',
  19892. 'procedure DoSome;',
  19893. 'var v: IUnknown;',
  19894. ' i: longint;',
  19895. 'begin',
  19896. ' v:=GetIt;',
  19897. ' v:=GetIt();',
  19898. ' GetIt()._AddRef;',
  19899. ' i:=GetIt()._AddRef;',
  19900. 'end;',
  19901. 'var v: IUnknown;',
  19902. ' i: longint;',
  19903. 'begin',
  19904. ' v:=GetIt;',
  19905. ' v:=GetIt();',
  19906. ' GetIt()._AddRef;',
  19907. ' i:=GetIt()._AddRef;',
  19908. '']);
  19909. ConvertProgram;
  19910. CheckSource('TestClassInterface_COM_FunctionInExpr',
  19911. LinesToStr([ // statements
  19912. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19913. 'rtl.createClass(this, "TObject", null, function () {',
  19914. ' this.$init = function () {',
  19915. ' };',
  19916. ' this.$final = function () {',
  19917. ' };',
  19918. ' rtl.addIntf(this, $mod.IUnknown);',
  19919. '});',
  19920. 'this.GetIt = function () {',
  19921. ' var Result = null;',
  19922. ' return Result;',
  19923. '};',
  19924. 'this.DoSome = function () {',
  19925. ' var v = null;',
  19926. ' var i = 0;',
  19927. ' var $ir = rtl.createIntfRefs();',
  19928. ' try {',
  19929. ' v = rtl.setIntfL(v, $mod.GetIt(), true);',
  19930. ' v = rtl.setIntfL(v, $mod.GetIt(), true);',
  19931. ' $ir.ref(1, $mod.GetIt())._AddRef();',
  19932. ' i = $ir.ref(2, $mod.GetIt())._AddRef();',
  19933. ' } finally {',
  19934. ' $ir.free();',
  19935. ' rtl._Release(v);',
  19936. ' };',
  19937. '};',
  19938. 'this.v = null;',
  19939. 'this.i = 0;',
  19940. '']),
  19941. LinesToStr([ // $mod.$main
  19942. 'var $ir = rtl.createIntfRefs();',
  19943. 'try {',
  19944. ' rtl.setIntfP($mod, "v", $mod.GetIt(), true);',
  19945. ' rtl.setIntfP($mod, "v", $mod.GetIt(), true);',
  19946. ' $ir.ref(1, $mod.GetIt())._AddRef();',
  19947. ' $mod.i = $ir.ref(2, $mod.GetIt())._AddRef();',
  19948. '} finally {',
  19949. ' $ir.free();',
  19950. '};',
  19951. '']));
  19952. end;
  19953. procedure TTestModule.TestClassInterface_COM_Property;
  19954. begin
  19955. StartProgram(false);
  19956. Add([
  19957. '{$interfaces com}',
  19958. 'type',
  19959. ' IUnknown = interface',
  19960. ' function _AddRef: longint;',
  19961. ' function _Release: longint;',
  19962. ' end;',
  19963. ' TObject = class(IUnknown)',
  19964. ' FAnt: IUnknown;',
  19965. ' function _AddRef: longint; virtual; abstract;',
  19966. ' function _Release: longint; virtual; abstract;',
  19967. ' function GetBird: IUnknown; virtual; abstract;',
  19968. ' procedure SetBird(Value: IUnknown); virtual; abstract;',
  19969. ' function GetItems(Index: longint): IUnknown; virtual; abstract;',
  19970. ' procedure SetItems(Index: longint; Value: IUnknown); virtual; abstract;',
  19971. ' property Ant: IUnknown read FAnt write FAnt;',
  19972. ' property Bird: IUnknown read GetBird write SetBird;',
  19973. ' property Items[Index: longint]: IUnknown read GetItems write SetItems; default;',
  19974. ' end;',
  19975. 'procedure DoIt;',
  19976. 'var',
  19977. ' o: TObject;',
  19978. ' v: IUnknown;',
  19979. 'begin',
  19980. ' v:=o.Ant;',
  19981. ' o.Ant:=v;',
  19982. ' o.Ant:=o.Ant;',
  19983. ' v:=o.Bird;',
  19984. ' o.Bird:=v;',
  19985. ' o.Bird:=o.Bird;',
  19986. ' v:=o.Items[1];',
  19987. ' o.Items[2]:=v;',
  19988. ' o.Items[3]:=o.Items[4];',
  19989. ' v:=o[5];',
  19990. ' o[6]:=v;',
  19991. ' o[7]:=o[8];',
  19992. 'end;',
  19993. 'begin',
  19994. '']);
  19995. ConvertProgram;
  19996. CheckSource('TestClassInterface_COM_Property',
  19997. LinesToStr([ // statements
  19998. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19999. 'rtl.createClass(this, "TObject", null, function () {',
  20000. ' this.$init = function () {',
  20001. ' this.FAnt = null;',
  20002. ' };',
  20003. ' this.$final = function () {',
  20004. ' this.FAnt = undefined;',
  20005. ' };',
  20006. ' rtl.addIntf(this, $mod.IUnknown);',
  20007. '});',
  20008. 'this.DoIt = function () {',
  20009. ' var o = null;',
  20010. ' var v = null;',
  20011. ' var $ir = rtl.createIntfRefs();',
  20012. ' try {',
  20013. ' v = rtl.setIntfL(v, o.FAnt);',
  20014. ' rtl.setIntfP(o, "FAnt", v);',
  20015. ' rtl.setIntfP(o, "FAnt", o.FAnt);',
  20016. ' v = rtl.setIntfL(v, o.GetBird(), true);',
  20017. ' o.SetBird(v);',
  20018. ' o.SetBird($ir.ref(1, o.GetBird()));',
  20019. ' v = rtl.setIntfL(v, o.GetItems(1), true);',
  20020. ' o.SetItems(2, v);',
  20021. ' o.SetItems(3, $ir.ref(2, o.GetItems(4)));',
  20022. ' v = rtl.setIntfL(v, o.GetItems(5), true);',
  20023. ' o.SetItems(6, v);',
  20024. ' o.SetItems(7, $ir.ref(3, o.GetItems(8)));',
  20025. ' } finally {',
  20026. ' $ir.free();',
  20027. ' rtl._Release(v);',
  20028. ' };',
  20029. '};',
  20030. '']),
  20031. LinesToStr([ // $mod.$main
  20032. '']));
  20033. end;
  20034. procedure TTestModule.TestClassInterface_COM_IntfProperty;
  20035. begin
  20036. StartProgram(false);
  20037. Add([
  20038. '{$interfaces com}',
  20039. 'type',
  20040. ' IUnknown = interface',
  20041. ' function _AddRef: longint;',
  20042. ' function _Release: longint;',
  20043. ' function GetBird: IUnknown;',
  20044. ' procedure SetBird(Value: IUnknown);',
  20045. ' function GetItems(Index: longint): IUnknown;',
  20046. ' procedure SetItems(Index: longint; Value: IUnknown);',
  20047. ' property Bird: IUnknown read GetBird write SetBird;',
  20048. ' property Items[Index: longint]: IUnknown read GetItems write SetItems; default;',
  20049. ' end;',
  20050. ' TObject = class(IUnknown)',
  20051. ' function _AddRef: longint; virtual; abstract;',
  20052. ' function _Release: longint; virtual; abstract;',
  20053. ' function GetBird: IUnknown; virtual; abstract;',
  20054. ' procedure SetBird(Value: IUnknown); virtual; abstract;',
  20055. ' function GetItems(Index: longint): IUnknown; virtual; abstract;',
  20056. ' procedure SetItems(Index: longint; Value: IUnknown); virtual; abstract;',
  20057. ' end;',
  20058. 'procedure DoIt;',
  20059. 'var',
  20060. ' o: TObject;',
  20061. ' v: IUnknown;',
  20062. 'begin',
  20063. ' v:=v.Items[1];',
  20064. ' v.Items[2]:=v;',
  20065. ' v.Items[3]:=v.Items[4];',
  20066. ' v:=v[5];',
  20067. ' v[6]:=v;',
  20068. ' v[7]:=v[8];',
  20069. ' v[9].Bird.Bird:=v;',
  20070. ' v:=v.Bird[10].Bird',
  20071. 'end;',
  20072. 'begin',
  20073. '']);
  20074. ConvertProgram;
  20075. CheckSource('TestClassInterface_COM_IntfProperty',
  20076. LinesToStr([ // statements
  20077. 'rtl.createInterface(this, "IUnknown", "{385F5482-571B-338C-8130-4E97F330543B}", [',
  20078. ' "_AddRef",',
  20079. ' "_Release",',
  20080. ' "GetBird",',
  20081. ' "SetBird",',
  20082. ' "GetItems",',
  20083. ' "SetItems"',
  20084. '], null);',
  20085. 'rtl.createClass(this, "TObject", null, function () {',
  20086. ' this.$init = function () {',
  20087. ' };',
  20088. ' this.$final = function () {',
  20089. ' };',
  20090. ' rtl.addIntf(this, $mod.IUnknown);',
  20091. '});',
  20092. 'this.DoIt = function () {',
  20093. ' var o = null;',
  20094. ' var v = null;',
  20095. ' var $ir = rtl.createIntfRefs();',
  20096. ' try {',
  20097. ' v = rtl.setIntfL(v, v.GetItems(1), true);',
  20098. ' v.SetItems(2, v);',
  20099. ' v.SetItems(3, $ir.ref(1, v.GetItems(4)));',
  20100. ' v = rtl.setIntfL(v, v.GetItems(5), true);',
  20101. ' v.SetItems(6, v);',
  20102. ' v.SetItems(7, $ir.ref(2, v.GetItems(8)));',
  20103. ' $ir.ref(4, $ir.ref(3, v.GetItems(9)).GetBird()).SetBird(v);',
  20104. ' v = rtl.setIntfL(v, $ir.ref(6, $ir.ref(5, v.GetBird()).GetItems(10)).GetBird(), true);',
  20105. ' } finally {',
  20106. ' $ir.free();',
  20107. ' rtl._Release(v);',
  20108. ' };',
  20109. '};',
  20110. '']),
  20111. LinesToStr([ // $mod.$main
  20112. '']));
  20113. end;
  20114. procedure TTestModule.TestClassInterface_COM_Delegation;
  20115. begin
  20116. StartProgram(false);
  20117. Add([
  20118. '{$interfaces com}',
  20119. 'type',
  20120. ' IUnknown = interface',
  20121. ' function _AddRef: longint;',
  20122. ' function _Release: longint;',
  20123. ' end;',
  20124. ' IBird = interface(IUnknown)',
  20125. ' procedure Fly(s: string);',
  20126. ' end;',
  20127. ' IEagle = interface(IBird) end;',
  20128. ' IDove = interface(IBird) end;',
  20129. ' ISwallow = interface(IBird) end;',
  20130. ' TObject = class',
  20131. ' end;',
  20132. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  20133. ' function _AddRef: longint; virtual; abstract;',
  20134. ' function _Release: longint; virtual; abstract;',
  20135. ' procedure Fly(s: string); virtual; abstract;',
  20136. ' end;',
  20137. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  20138. ' function _AddRef: longint; virtual; abstract;',
  20139. ' function _Release: longint; virtual; abstract;',
  20140. ' FBirdIntf: IBird;',
  20141. ' property BirdIntf: IBird read FBirdIntf implements IBird;',
  20142. ' function GetEagleIntf: IEagle; virtual; abstract;',
  20143. ' property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  20144. ' FDoveObj: TBird;',
  20145. ' property DoveObj: TBird read FDoveObj implements IDove;',
  20146. ' function GetSwallowObj: TBird; virtual; abstract;',
  20147. ' property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  20148. ' end;',
  20149. 'begin',
  20150. '']);
  20151. ConvertProgram;
  20152. CheckSource('TestClassInterface_COM_Delegation',
  20153. LinesToStr([ // statements
  20154. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  20155. 'rtl.createInterface(this, "IBird", "{CC440C7F-7623-3DEE-AE88-000B86AAF108}", ["Fly"], this.IUnknown);',
  20156. 'rtl.createInterface(this, "IEagle", "{4B6A41C9-B020-3D7C-B688-96D19022B1B4}", [], this.IBird);',
  20157. 'rtl.createInterface(this, "IDove", "{4B6A41C9-B020-3D7C-B688-96D18EF16074}", [], this.IBird);',
  20158. 'rtl.createInterface(this, "ISwallow", "{BB6A41C9-B020-3D7C-B688-96D1CBDCB359}", [], this.IBird);',
  20159. 'rtl.createClass(this, "TObject", null, function () {',
  20160. ' this.$init = function () {',
  20161. ' };',
  20162. ' this.$final = function () {',
  20163. ' };',
  20164. '});',
  20165. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20166. ' rtl.addIntf(this, $mod.IBird);',
  20167. ' rtl.addIntf(this, $mod.IEagle);',
  20168. ' rtl.addIntf(this, $mod.IDove);',
  20169. ' rtl.addIntf(this, $mod.ISwallow);',
  20170. '});',
  20171. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  20172. ' this.$init = function () {',
  20173. ' $mod.TObject.$init.call(this);',
  20174. ' this.FBirdIntf = null;',
  20175. ' this.FDoveObj = null;',
  20176. ' };',
  20177. ' this.$final = function () {',
  20178. ' this.FBirdIntf = undefined;',
  20179. ' this.FDoveObj = undefined;',
  20180. ' $mod.TObject.$final.call(this);',
  20181. ' };',
  20182. ' this.$intfmaps = {',
  20183. ' "{CC440C7F-7623-3DEE-AE88-000B86AAF108}": function () {',
  20184. ' return rtl._AddRef(this.FBirdIntf);',
  20185. ' },',
  20186. ' "{4B6A41C9-B020-3D7C-B688-96D19022B1B4}": function () {',
  20187. ' return this.GetEagleIntf();',
  20188. ' },',
  20189. ' "{4B6A41C9-B020-3D7C-B688-96D18EF16074}": function () {',
  20190. ' return rtl.queryIntfT(this.FDoveObj, $mod.IDove);',
  20191. ' },',
  20192. ' "{BB6A41C9-B020-3D7C-B688-96D1CBDCB359}": function () {',
  20193. ' return rtl.queryIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  20194. ' }',
  20195. ' };',
  20196. '});',
  20197. '']),
  20198. LinesToStr([ // $mod.$main
  20199. '']));
  20200. end;
  20201. procedure TTestModule.TestClassInterface_COM_With;
  20202. begin
  20203. StartProgram(false);
  20204. Add([
  20205. '{$interfaces com}',
  20206. 'type',
  20207. ' IUnknown = interface',
  20208. ' function _AddRef: longint;',
  20209. ' function _Release: longint;',
  20210. ' function GetAnt: IUnknown;',
  20211. ' property Ant: IUnknown read GetAnt;',
  20212. ' end;',
  20213. ' TObject = class(IUnknown)',
  20214. ' function _AddRef: longint; virtual; abstract;',
  20215. ' function _Release: longint; virtual; abstract;',
  20216. ' function GetAnt: IUnknown; virtual; abstract;',
  20217. ' property Ant: IUnknown read GetAnt;',
  20218. ' end;',
  20219. 'procedure DoIt;',
  20220. 'var',
  20221. ' i: IUnknown;',
  20222. 'begin',
  20223. ' with i do ',
  20224. ' GetAnt;',
  20225. ' with i.Ant, Ant do ',
  20226. ' GetAnt;',
  20227. 'end;',
  20228. 'begin',
  20229. '']);
  20230. ConvertProgram;
  20231. CheckSource('TestClassInterface_COM_With',
  20232. LinesToStr([ // statements
  20233. 'rtl.createInterface(this, "IUnknown", "{D7ADB00D-C6B6-39FB-BDDF-21CD521DDFA9}", ["_AddRef", "_Release", "GetAnt"], null);',
  20234. 'rtl.createClass(this, "TObject", null, function () {',
  20235. ' this.$init = function () {',
  20236. ' };',
  20237. ' this.$final = function () {',
  20238. ' };',
  20239. ' rtl.addIntf(this, $mod.IUnknown);',
  20240. '});',
  20241. 'this.DoIt = function () {',
  20242. ' var i = null;',
  20243. ' var $ir = rtl.createIntfRefs();',
  20244. ' try {',
  20245. ' $ir.ref(1, i.GetAnt());',
  20246. ' var $with = $ir.ref(2, i.GetAnt());',
  20247. ' var $with1 = $ir.ref(3, $with.GetAnt());',
  20248. ' $ir.ref(4, $with1.GetAnt());',
  20249. ' } finally {',
  20250. ' $ir.free();',
  20251. ' };',
  20252. '};',
  20253. '']),
  20254. LinesToStr([ // $mod.$main
  20255. '']));
  20256. end;
  20257. procedure TTestModule.TestClassInterface_COM_ForIn;
  20258. begin
  20259. StartProgram(false);
  20260. Add([
  20261. '{$interfaces com}',
  20262. 'type',
  20263. ' IUnknown = interface end;',
  20264. ' TObject = class',
  20265. ' Id: longint;',
  20266. ' end;',
  20267. ' IEnumerator = interface(IUnknown)',
  20268. ' function GetCurrent: TObject;',
  20269. ' function MoveNext: Boolean;',
  20270. ' property Current: TObject read GetCurrent;',
  20271. ' end;',
  20272. ' IEnumerable = interface(IUnknown)',
  20273. ' function GetEnumerator: IEnumerator;',
  20274. ' end;',
  20275. 'var',
  20276. ' o: TObject;',
  20277. ' i: IEnumerable;',
  20278. 'begin',
  20279. ' for o in i do o.Id:=3;',
  20280. '']);
  20281. ConvertProgram;
  20282. CheckSource('TestClassInterface_COM_ForIn',
  20283. LinesToStr([ // statements
  20284. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  20285. 'rtl.createClass(this, "TObject", null, function () {',
  20286. ' this.$init = function () {',
  20287. ' this.Id = 0;',
  20288. ' };',
  20289. ' this.$final = function () {',
  20290. ' };',
  20291. '});',
  20292. 'rtl.createInterface(this, "IEnumerator", "{95D7745D-ED61-3F13-BBE4-07708161999E}", ["GetCurrent", "MoveNext"], this.IUnknown);',
  20293. 'rtl.createInterface(this, "IEnumerable", "{8CC9D45D-ED7D-3B73-96B6-290B931BB19E}", ["GetEnumerator"], this.IUnknown);',
  20294. 'this.o = null;',
  20295. 'this.i = null;',
  20296. '']),
  20297. LinesToStr([ // $mod.$main
  20298. 'var $in = $mod.i.GetEnumerator();',
  20299. 'try {',
  20300. ' while ($in.MoveNext()) {',
  20301. ' $mod.o = $in.GetCurrent();',
  20302. ' $mod.o.Id = 3;',
  20303. ' }',
  20304. '} finally {',
  20305. ' rtl._Release($in)',
  20306. '};',
  20307. '']));
  20308. end;
  20309. procedure TTestModule.TestClassInterface_COM_ArrayOfIntfFail;
  20310. begin
  20311. StartProgram(false);
  20312. Add([
  20313. '{$interfaces com}',
  20314. 'type',
  20315. ' IUnknown = interface',
  20316. ' function _AddRef: longint;',
  20317. ' function _Release: longint;',
  20318. ' end;',
  20319. ' TObject = class',
  20320. ' end;',
  20321. ' TArrOfIntf = array of IUnknown;',
  20322. 'begin',
  20323. '']);
  20324. SetExpectedPasResolverError('Not supported: array of COM-interface',nNotSupportedX);
  20325. ConvertProgram;
  20326. end;
  20327. procedure TTestModule.TestClassInterface_COM_RecordIntfFail;
  20328. begin
  20329. StartProgram(false);
  20330. Add([
  20331. '{$interfaces com}',
  20332. 'type',
  20333. ' IUnknown = interface',
  20334. ' function _AddRef: longint;',
  20335. ' function _Release: longint;',
  20336. ' end;',
  20337. ' TRec = record',
  20338. ' i: IUnknown;',
  20339. ' end;',
  20340. 'begin',
  20341. '']);
  20342. SetExpectedPasResolverError('Not supported: COM-interface as record member',nNotSupportedX);
  20343. ConvertProgram;
  20344. end;
  20345. procedure TTestModule.TestClassInterface_COM_UnitInitialization;
  20346. begin
  20347. StartUnit(false);
  20348. Add([
  20349. '{$interfaces com}',
  20350. 'interface',
  20351. 'implementation',
  20352. 'type',
  20353. ' IUnknown = interface',
  20354. ' function _AddRef: longint;',
  20355. ' end;',
  20356. ' TObject = class(IUnknown)',
  20357. ' function _AddRef: longint;',
  20358. ' end;',
  20359. 'function TObject._AddRef: longint; begin end;',
  20360. 'var i: IUnknown;',
  20361. ' o: TObject;',
  20362. 'initialization',
  20363. ' i:=nil;',
  20364. ' i:=i;',
  20365. ' i:=o;',
  20366. ' if (o as IUnknown)=nil then ;',
  20367. '']);
  20368. ConvertUnit;
  20369. CheckSource('TestClassInterface_COM_UnitInitialization',
  20370. LinesToStr([ // statements
  20371. 'var $impl = $mod.$impl;',
  20372. '']),
  20373. LinesToStr([ // this.$init
  20374. 'var $ir = rtl.createIntfRefs();',
  20375. 'try {',
  20376. ' rtl.setIntfP($impl, "i", null);',
  20377. ' rtl.setIntfP($impl, "i", $impl.i);',
  20378. ' rtl.setIntfP($impl, "i", rtl.queryIntfT($impl.o, $impl.IUnknown), true);',
  20379. ' if ($ir.ref(1, rtl.queryIntfT($impl.o, $impl.IUnknown)) === null) ;',
  20380. '} finally {',
  20381. ' $ir.free();',
  20382. '};',
  20383. '']),
  20384. LinesToStr([ // implementation
  20385. 'rtl.createInterface($impl, "IUnknown", "{B92D5841-758A-322B-BDDF-21CD52180000}", ["_AddRef"], null);',
  20386. 'rtl.createClass($impl, "TObject", null, function () {',
  20387. ' this.$init = function () {',
  20388. ' };',
  20389. ' this.$final = function () {',
  20390. ' };',
  20391. ' this._AddRef = function () {',
  20392. ' var Result = 0;',
  20393. ' return Result;',
  20394. ' };',
  20395. ' rtl.addIntf(this, $impl.IUnknown);',
  20396. '});',
  20397. '$impl.i = null;',
  20398. '$impl.o = null;',
  20399. ''])
  20400. );
  20401. end;
  20402. procedure TTestModule.TestClassInterface_GUID;
  20403. begin
  20404. StartProgram(false);
  20405. Add([
  20406. '{$interfaces corba}',
  20407. 'type',
  20408. ' IUnknown = interface',
  20409. ' [''{f31db68f-3010-D355-4EBA-CDD4EF4A737C}'']',
  20410. ' end;',
  20411. ' TObject = class end;',
  20412. ' TGUID = record D1, D2, D3, D4: word; end;',
  20413. ' TAliasGUID = TGUID;',
  20414. ' TGUIDString = type string;',
  20415. ' TAliasGUIDString = TGUIDString;',
  20416. 'procedure DoConstGUIDIt(const g: TAliasGUID); overload;',
  20417. 'begin end;',
  20418. 'procedure DoDefGUID(g: TAliasGUID); overload;',
  20419. 'begin end;',
  20420. 'procedure DoStr(const s: TAliasGUIDString); overload;',
  20421. 'begin end;',
  20422. 'var',
  20423. ' i: IUnknown;',
  20424. ' g: TAliasGUID = ''{d91c9af4-3C93-420F-A303-BF5BA82BFD23}'';',
  20425. ' s: TAliasGUIDString;',
  20426. 'begin',
  20427. ' DoConstGUIDIt(IUnknown);',
  20428. ' DoDefGUID(IUnknown);',
  20429. ' DoStr(IUnknown);',
  20430. ' DoConstGUIDIt(i);',
  20431. ' DoDefGUID(i);',
  20432. ' DoStr(i);',
  20433. ' DoConstGUIDIt(''{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}'');',
  20434. ' DoDefGUID(''{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}'');',
  20435. ' DoStr(g);',
  20436. ' g:=i;',
  20437. ' g:=IUnknown;',
  20438. ' g:=''{D91C9AF4-3C93-420F-A303-bf5ba82bfd23}'';',
  20439. ' s:=i;',
  20440. ' s:=IUnknown;',
  20441. ' s:=g;',
  20442. ' if g=i then ;',
  20443. ' if i=g then ;',
  20444. ' if g=IUnknown then ;',
  20445. ' if IUnknown=g then ;',
  20446. ' if s=i then ;',
  20447. ' if i=s then ;',
  20448. ' if s=IUnknown then ;',
  20449. ' if IUnknown=s then ;',
  20450. ' if s=g then ;',
  20451. ' if g=s then ;',
  20452. '']);
  20453. ConvertProgram;
  20454. CheckSource('TestClassInterface_GUID',
  20455. LinesToStr([ // statements
  20456. 'rtl.createInterface(this, "IUnknown", "{F31DB68F-3010-D355-4EBA-CDD4EF4A737C}", [], null);',
  20457. 'rtl.createClass(this, "TObject", null, function () {',
  20458. ' this.$init = function () {',
  20459. ' };',
  20460. ' this.$final = function () {',
  20461. ' };',
  20462. '});',
  20463. 'rtl.recNewT(this, "TGUID", function () {',
  20464. ' this.D1 = 0;',
  20465. ' this.D2 = 0;',
  20466. ' this.D3 = 0;',
  20467. ' this.D4 = 0;',
  20468. ' this.$eq = function (b) {',
  20469. ' return (this.D1 === b.D1) && (this.D2 === b.D2) && (this.D3 === b.D3) && (this.D4 === b.D4);',
  20470. ' };',
  20471. ' this.$assign = function (s) {',
  20472. ' this.D1 = s.D1;',
  20473. ' this.D2 = s.D2;',
  20474. ' this.D3 = s.D3;',
  20475. ' this.D4 = s.D4;',
  20476. ' return this;',
  20477. ' };',
  20478. '});',
  20479. 'this.DoConstGUIDIt = function (g) {',
  20480. '};',
  20481. 'this.DoDefGUID = function (g) {',
  20482. '};',
  20483. 'this.DoStr = function (s) {',
  20484. '};',
  20485. 'this.i = null;',
  20486. 'this.g = this.TGUID.$clone({',
  20487. ' D1: 0xD91C9AF4,',
  20488. ' D2: 0x3C93,',
  20489. ' D3: 0x420F,',
  20490. ' D4: [',
  20491. ' 0xA3,',
  20492. ' 0x03,',
  20493. ' 0xBF,',
  20494. ' 0x5B,',
  20495. ' 0xA8,',
  20496. ' 0x2B,',
  20497. ' 0xFD,',
  20498. ' 0x23',
  20499. ' ]',
  20500. '});',
  20501. 'this.s = "";',
  20502. '']),
  20503. LinesToStr([ // $mod.$main
  20504. '$mod.DoConstGUIDIt(rtl.getIntfGUIDR($mod.IUnknown));',
  20505. '$mod.DoDefGUID($mod.TGUID.$clone(rtl.getIntfGUIDR($mod.IUnknown)));',
  20506. '$mod.DoStr($mod.IUnknown.$guid);',
  20507. '$mod.DoConstGUIDIt(rtl.getIntfGUIDR($mod.i));',
  20508. '$mod.DoDefGUID($mod.TGUID.$clone(rtl.getIntfGUIDR($mod.i)));',
  20509. '$mod.DoStr($mod.i.$guid);',
  20510. '$mod.DoConstGUIDIt(rtl.strToGUIDR("{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}"));',
  20511. '$mod.DoDefGUID(rtl.strToGUIDR("{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}"));',
  20512. '$mod.DoStr(rtl.guidrToStr($mod.g));',
  20513. '$mod.g.$assign(rtl.getIntfGUIDR($mod.i));',
  20514. '$mod.g.$assign(rtl.getIntfGUIDR($mod.IUnknown));',
  20515. '$mod.g.$assign({',
  20516. ' D1: 0xD91C9AF4,',
  20517. ' D2: 0x3C93,',
  20518. ' D3: 0x420F,',
  20519. ' D4: [',
  20520. ' 0xA3,',
  20521. ' 0x03,',
  20522. ' 0xBF,',
  20523. ' 0x5B,',
  20524. ' 0xA8,',
  20525. ' 0x2B,',
  20526. ' 0xFD,',
  20527. ' 0x23',
  20528. ' ]',
  20529. '});',
  20530. '$mod.s = $mod.i.$guid;',
  20531. '$mod.s = $mod.IUnknown.$guid;',
  20532. '$mod.s = rtl.guidrToStr($mod.g);',
  20533. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.i))) ;',
  20534. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.i))) ;',
  20535. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.IUnknown))) ;',
  20536. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.IUnknown))) ;',
  20537. 'if ($mod.s === $mod.i.$guid) ;',
  20538. 'if ($mod.i.$guid === $mod.s) ;',
  20539. 'if ($mod.s === $mod.IUnknown.$guid) ;',
  20540. 'if ($mod.IUnknown.$guid === $mod.s) ;',
  20541. 'if ($mod.g.$eq(rtl.createTGUID($mod.s))) ;',
  20542. 'if ($mod.g.$eq(rtl.createTGUID($mod.s))) ;',
  20543. '']));
  20544. end;
  20545. procedure TTestModule.TestClassInterface_GUIDProperty;
  20546. begin
  20547. StartProgram(false);
  20548. Add([
  20549. '{$interfaces corba}',
  20550. 'type',
  20551. ' IUnknown = interface',
  20552. ' [''{f31db68f-3010-D355-4EBA-CDD4EF4A737C}'']',
  20553. ' end;',
  20554. ' TGUID = record D1, D2, D3, D4: word; end;',
  20555. ' TAliasGUID = TGUID;',
  20556. ' TGUIDString = type string;',
  20557. ' TAliasGUIDString = TGUIDString;',
  20558. ' TObject = class',
  20559. ' function GetG: TAliasGUID; virtual; abstract;',
  20560. ' procedure SetG(const Value: TAliasGUID); virtual; abstract;',
  20561. ' function GetS: TAliasGUIDString; virtual; abstract;',
  20562. ' procedure SetS(const Value: TAliasGUIDString); virtual; abstract;',
  20563. ' property g: TAliasGUID read GetG write SetG;',
  20564. ' property s: TAliasGUIDString read GetS write SetS;',
  20565. ' end;',
  20566. 'var o: TObject;',
  20567. 'begin',
  20568. ' o.g:=IUnknown;',
  20569. ' o.g:=''{D91C9AF4-3C93-420F-A303-bf5ba82bfd23}'';',
  20570. ' o.s:=IUnknown;',
  20571. ' o.s:=o.g;',
  20572. '']);
  20573. ConvertProgram;
  20574. CheckSource('TestClassInterface_GUIDProperty',
  20575. LinesToStr([ // statements
  20576. 'rtl.createInterface(this, "IUnknown", "{F31DB68F-3010-D355-4EBA-CDD4EF4A737C}", [], null);',
  20577. 'rtl.recNewT(this, "TGUID", function () {',
  20578. ' this.D1 = 0;',
  20579. ' this.D2 = 0;',
  20580. ' this.D3 = 0;',
  20581. ' this.D4 = 0;',
  20582. ' this.$eq = function (b) {',
  20583. ' return (this.D1 === b.D1) && (this.D2 === b.D2) && (this.D3 === b.D3) && (this.D4 === b.D4);',
  20584. ' };',
  20585. ' this.$assign = function (s) {',
  20586. ' this.D1 = s.D1;',
  20587. ' this.D2 = s.D2;',
  20588. ' this.D3 = s.D3;',
  20589. ' this.D4 = s.D4;',
  20590. ' return this;',
  20591. ' };',
  20592. '});',
  20593. 'rtl.createClass(this, "TObject", null, function () {',
  20594. ' this.$init = function () {',
  20595. ' };',
  20596. ' this.$final = function () {',
  20597. ' };',
  20598. '});',
  20599. 'this.o = null;',
  20600. '']),
  20601. LinesToStr([ // $mod.$main
  20602. '$mod.o.SetG(rtl.getIntfGUIDR($mod.IUnknown));',
  20603. '$mod.o.SetG({',
  20604. ' D1: 0xD91C9AF4,',
  20605. ' D2: 0x3C93,',
  20606. ' D3: 0x420F,',
  20607. ' D4: [',
  20608. ' 0xA3,',
  20609. ' 0x03,',
  20610. ' 0xBF,',
  20611. ' 0x5B,',
  20612. ' 0xA8,',
  20613. ' 0x2B,',
  20614. ' 0xFD,',
  20615. ' 0x23',
  20616. ' ]',
  20617. '});',
  20618. '$mod.o.SetS($mod.IUnknown.$guid);',
  20619. '$mod.o.SetS(rtl.guidrToStr($mod.o.GetG()));',
  20620. '']));
  20621. end;
  20622. procedure TTestModule.TestClassHelper_ClassVar;
  20623. begin
  20624. StartProgram(false);
  20625. Add([
  20626. 'type',
  20627. ' TObject = class',
  20628. ' end;',
  20629. ' THelper = class helper for TObject',
  20630. ' const',
  20631. ' One = 1;',
  20632. ' Two: word = 2;',
  20633. ' class var',
  20634. ' Glob: word;',
  20635. ' function Foo(w: word): word;',
  20636. ' class function Bar(w: word): word;',
  20637. ' end;',
  20638. 'function THelper.foo(w: word): word;',
  20639. 'begin',
  20640. ' Result:=w;',
  20641. ' Two:=One+w;',
  20642. ' Glob:=Glob;',
  20643. ' Result:=Self.Glob;',
  20644. ' Self.Glob:=Self.Glob;',
  20645. ' with Self do Glob:=Glob;',
  20646. 'end;',
  20647. 'class function THelper.bar(w: word): word;',
  20648. 'begin',
  20649. ' Result:=w;',
  20650. ' Two:=One;',
  20651. ' Glob:=Glob;',
  20652. ' Self.Glob:=Self.Glob;',
  20653. ' with Self do Glob:=Glob;',
  20654. 'end;',
  20655. 'var o: TObject;',
  20656. 'begin',
  20657. ' tobject.two:=tobject.one;',
  20658. ' tobject.Glob:=tobject.Glob;',
  20659. ' with tobject do begin',
  20660. ' two:=one;',
  20661. ' Glob:=Glob;',
  20662. ' end;',
  20663. ' o.two:=o.one;',
  20664. ' o.Glob:=o.Glob;',
  20665. ' with o do begin',
  20666. ' two:=one;',
  20667. ' Glob:=Glob;',
  20668. ' end;',
  20669. '']);
  20670. ConvertProgram;
  20671. CheckSource('TestClassHelper_ClassVar',
  20672. LinesToStr([ // statements
  20673. 'rtl.createClass(this, "TObject", null, function () {',
  20674. ' this.$init = function () {',
  20675. ' };',
  20676. ' this.$final = function () {',
  20677. ' };',
  20678. '});',
  20679. 'rtl.createHelper(this, "THelper", null, function () {',
  20680. ' this.One = 1;',
  20681. ' this.Two = 2;',
  20682. ' this.Glob = 0;',
  20683. ' this.Foo = function (w) {',
  20684. ' var Result = 0;',
  20685. ' Result = w;',
  20686. ' $mod.THelper.Two = 1 + w;',
  20687. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20688. ' Result = $mod.THelper.Glob;',
  20689. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20690. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20691. ' return Result;',
  20692. ' };',
  20693. ' this.Bar = function (w) {',
  20694. ' var Result = 0;',
  20695. ' Result = w;',
  20696. ' $mod.THelper.Two = 1;',
  20697. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20698. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20699. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20700. ' return Result;',
  20701. ' };',
  20702. '});',
  20703. 'this.o = null;',
  20704. '']),
  20705. LinesToStr([ // $mod.$main
  20706. '$mod.THelper.Two = 1;',
  20707. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20708. 'var $with = $mod.TObject;',
  20709. '$mod.THelper.Two = 1;',
  20710. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20711. '$mod.THelper.Two = 1;',
  20712. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20713. 'var $with1 = $mod.o;',
  20714. '$mod.THelper.Two = 1;',
  20715. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20716. '']));
  20717. end;
  20718. procedure TTestModule.TestClassHelper_Method_AccessInstanceFields;
  20719. begin
  20720. StartProgram(false);
  20721. Add([
  20722. 'type',
  20723. ' TObject = class',
  20724. ' FSize: word;',
  20725. ' property Size: word read FSize write FSize;',
  20726. ' end;',
  20727. ' THelper = class helper for TObject',
  20728. ' function Foo(w: word = 1): word;',
  20729. ' end;',
  20730. 'function THelper.foo(w: word): word;',
  20731. 'begin',
  20732. ' Result:=Size;',
  20733. ' Size:=Size+2;',
  20734. ' Self.Size:=Self.Size+3;',
  20735. ' FSize:=FSize+4;',
  20736. ' Self.FSize:=Self.FSize+5;',
  20737. ' with Self do begin',
  20738. ' Size:=Size+6;',
  20739. ' FSize:=FSize+7;',
  20740. ' FSize:=FSize+8;',
  20741. ' end;',
  20742. 'end;',
  20743. 'begin',
  20744. '']);
  20745. ConvertProgram;
  20746. CheckSource('TestClassHelper_Method_AccessInstanceFields',
  20747. LinesToStr([ // statements
  20748. 'rtl.createClass(this, "TObject", null, function () {',
  20749. ' this.$init = function () {',
  20750. ' this.FSize = 0;',
  20751. ' };',
  20752. ' this.$final = function () {',
  20753. ' };',
  20754. '});',
  20755. 'rtl.createHelper(this, "THelper", null, function () {',
  20756. ' this.Foo = function (w) {',
  20757. ' var Result = 0;',
  20758. ' Result = this.FSize;',
  20759. ' this.FSize = this.FSize + 2;',
  20760. ' this.FSize = this.FSize + 3;',
  20761. ' this.FSize = this.FSize + 4;',
  20762. ' this.FSize = this.FSize + 5;',
  20763. ' this.FSize = this.FSize + 6;',
  20764. ' this.FSize = this.FSize + 7;',
  20765. ' this.FSize = this.FSize + 8;',
  20766. ' return Result;',
  20767. ' };',
  20768. '});',
  20769. '']),
  20770. LinesToStr([ // $mod.$main
  20771. '']));
  20772. end;
  20773. procedure TTestModule.TestClassHelper_Method_Call;
  20774. begin
  20775. StartProgram(false);
  20776. Add([
  20777. 'type',
  20778. ' TObject = class',
  20779. ' procedure Run(w: word = 10);',
  20780. ' end;',
  20781. ' THelper = class helper for TObject',
  20782. ' function Foo(w: word = 1): word;',
  20783. ' end;',
  20784. 'procedure TObject.Run(w: word);',
  20785. 'var o: TObject;',
  20786. 'begin',
  20787. ' Foo;',
  20788. ' Foo();',
  20789. ' Foo(2);',
  20790. ' Self.Foo;',
  20791. ' Self.Foo();',
  20792. ' Self.Foo(3);',
  20793. ' with Self do begin',
  20794. ' Foo;',
  20795. ' Foo();',
  20796. ' Foo(4);',
  20797. ' end;',
  20798. ' with o do Foo(5);',
  20799. 'end;',
  20800. 'function THelper.foo(w: word): word;',
  20801. 'begin',
  20802. ' Run;',
  20803. ' Run();',
  20804. ' Run(11);',
  20805. ' Foo;',
  20806. ' Foo();',
  20807. ' Foo(12);',
  20808. ' Self.Foo;',
  20809. ' Self.Foo();',
  20810. ' Self.Foo(13);',
  20811. ' with Self do begin',
  20812. ' Foo;',
  20813. ' Foo();',
  20814. ' Foo(14);',
  20815. ' end;',
  20816. 'end;',
  20817. 'var Obj: TObject;',
  20818. 'begin',
  20819. ' obj.Foo;',
  20820. ' obj.Foo();',
  20821. ' obj.Foo(21);',
  20822. ' with obj do begin',
  20823. ' Foo;',
  20824. ' Foo();',
  20825. ' Foo(22);',
  20826. ' end;',
  20827. '']);
  20828. ConvertProgram;
  20829. CheckSource('TestClassHelper_Method_Call',
  20830. LinesToStr([ // statements
  20831. 'rtl.createClass(this, "TObject", null, function () {',
  20832. ' this.$init = function () {',
  20833. ' };',
  20834. ' this.$final = function () {',
  20835. ' };',
  20836. ' this.Run = function (w) {',
  20837. ' var o = null;',
  20838. ' $mod.THelper.Foo.call(this, 1);',
  20839. ' $mod.THelper.Foo.call(this, 1);',
  20840. ' $mod.THelper.Foo.call(this, 2);',
  20841. ' $mod.THelper.Foo.call(this, 1);',
  20842. ' $mod.THelper.Foo.call(this, 1);',
  20843. ' $mod.THelper.Foo.call(this, 3);',
  20844. ' $mod.THelper.Foo.call(this, 1);',
  20845. ' $mod.THelper.Foo.call(this, 1);',
  20846. ' $mod.THelper.Foo.call(this, 4);',
  20847. ' $mod.THelper.Foo.call(o, 5);',
  20848. ' };',
  20849. '});',
  20850. 'rtl.createHelper(this, "THelper", null, function () {',
  20851. ' this.Foo = function (w) {',
  20852. ' var Result = 0;',
  20853. ' this.Run(10);',
  20854. ' this.Run(10);',
  20855. ' this.Run(11);',
  20856. ' $mod.THelper.Foo.call(this, 1);',
  20857. ' $mod.THelper.Foo.call(this, 1);',
  20858. ' $mod.THelper.Foo.call(this, 12);',
  20859. ' $mod.THelper.Foo.call(this, 1);',
  20860. ' $mod.THelper.Foo.call(this, 1);',
  20861. ' $mod.THelper.Foo.call(this, 13);',
  20862. ' $mod.THelper.Foo.call(this, 1);',
  20863. ' $mod.THelper.Foo.call(this, 1);',
  20864. ' $mod.THelper.Foo.call(this, 14);',
  20865. ' return Result;',
  20866. ' };',
  20867. '});',
  20868. 'this.Obj = null;',
  20869. '']),
  20870. LinesToStr([ // $mod.$main
  20871. '$mod.THelper.Foo.call($mod.Obj, 1);',
  20872. '$mod.THelper.Foo.call($mod.Obj, 1);',
  20873. '$mod.THelper.Foo.call($mod.Obj, 21);',
  20874. 'var $with = $mod.Obj;',
  20875. '$mod.THelper.Foo.call($with, 1);',
  20876. '$mod.THelper.Foo.call($with, 1);',
  20877. '$mod.THelper.Foo.call($with, 22);',
  20878. '']));
  20879. end;
  20880. procedure TTestModule.TestClassHelper_Method_Nested_Call;
  20881. begin
  20882. StartProgram(false);
  20883. Add([
  20884. 'type',
  20885. ' TObject = class',
  20886. ' procedure Run(w: word = 10);',
  20887. ' end;',
  20888. ' THelper = class helper for TObject',
  20889. ' function Foo(w: word = 1): word;',
  20890. ' end;',
  20891. 'procedure TObject.Run(w: word);',
  20892. ' procedure Sub(Self: TObject);',
  20893. ' begin',
  20894. ' Foo;',
  20895. ' Foo();',
  20896. ' Self.Foo;',
  20897. ' Self.Foo();',
  20898. ' with Self do begin',
  20899. ' Foo;',
  20900. ' Foo();',
  20901. ' end;',
  20902. ' end;',
  20903. 'begin',
  20904. 'end;',
  20905. 'function THelper.foo(w: word): word;',
  20906. ' procedure Sub(Self: TObject);',
  20907. ' begin',
  20908. ' Run;',
  20909. ' Run();',
  20910. ' Foo;',
  20911. ' Foo();',
  20912. ' Self.Foo;',
  20913. ' Self.Foo();',
  20914. ' with Self do begin',
  20915. ' Foo;',
  20916. ' Foo();',
  20917. ' end;',
  20918. ' end;',
  20919. 'begin',
  20920. 'end;',
  20921. 'begin',
  20922. '']);
  20923. ConvertProgram;
  20924. CheckSource('TestClassHelper_Method_Nested_Call',
  20925. LinesToStr([ // statements
  20926. 'rtl.createClass(this, "TObject", null, function () {',
  20927. ' this.$init = function () {',
  20928. ' };',
  20929. ' this.$final = function () {',
  20930. ' };',
  20931. ' this.Run = function (w) {',
  20932. ' var $Self = this;',
  20933. ' function Sub(Self) {',
  20934. ' $mod.THelper.Foo.call($Self, 1);',
  20935. ' $mod.THelper.Foo.call($Self, 1);',
  20936. ' $mod.THelper.Foo.call(Self, 1);',
  20937. ' $mod.THelper.Foo.call(Self, 1);',
  20938. ' $mod.THelper.Foo.call(Self, 1);',
  20939. ' $mod.THelper.Foo.call(Self, 1);',
  20940. ' };',
  20941. ' };',
  20942. '});',
  20943. 'rtl.createHelper(this, "THelper", null, function () {',
  20944. ' this.Foo = function (w) {',
  20945. ' var $Self = this;',
  20946. ' var Result = 0;',
  20947. ' function Sub(Self) {',
  20948. ' $Self.Run(10);',
  20949. ' $Self.Run(10);',
  20950. ' $mod.THelper.Foo.call($Self, 1);',
  20951. ' $mod.THelper.Foo.call($Self, 1);',
  20952. ' $mod.THelper.Foo.call(Self, 1);',
  20953. ' $mod.THelper.Foo.call(Self, 1);',
  20954. ' $mod.THelper.Foo.call(Self, 1);',
  20955. ' $mod.THelper.Foo.call(Self, 1);',
  20956. ' };',
  20957. ' return Result;',
  20958. ' };',
  20959. '});',
  20960. '']),
  20961. LinesToStr([ // $mod.$main
  20962. '']));
  20963. end;
  20964. procedure TTestModule.TestClassHelper_ClassMethod_Call;
  20965. begin
  20966. StartProgram(false);
  20967. Add([
  20968. 'type',
  20969. ' TObject = class',
  20970. ' class procedure Run(w: word = 10);',
  20971. ' end;',
  20972. ' THelper = class helper for TObject',
  20973. ' class function Foo(w: word = 1): word;',
  20974. ' end;',
  20975. 'class procedure TObject.Run(w: word);',
  20976. 'begin',
  20977. ' Foo;',
  20978. ' Foo();',
  20979. ' Self.Foo;',
  20980. ' Self.Foo();',
  20981. ' with Self do begin',
  20982. ' Foo;',
  20983. ' Foo();',
  20984. ' end;',
  20985. 'end;',
  20986. 'class function THelper.foo(w: word): word;',
  20987. 'begin',
  20988. ' Run;',
  20989. ' Run();',
  20990. ' Foo;',
  20991. ' Foo();',
  20992. ' Self.Foo;',
  20993. ' Self.Foo();',
  20994. ' with Self do begin',
  20995. ' Foo;',
  20996. ' Foo();',
  20997. ' end;',
  20998. 'end;',
  20999. 'var',
  21000. ' Obj: TObject;',
  21001. 'begin',
  21002. ' obj.Foo;',
  21003. ' obj.Foo();',
  21004. ' with obj do begin',
  21005. ' Foo;',
  21006. ' Foo();',
  21007. ' end;',
  21008. ' tobject.Foo;',
  21009. ' tobject.Foo();',
  21010. ' with tobject do begin',
  21011. ' Foo;',
  21012. ' Foo();',
  21013. ' end;',
  21014. '']);
  21015. ConvertProgram;
  21016. CheckSource('TestClassHelper_ClassMethod_Call',
  21017. LinesToStr([ // statements
  21018. 'rtl.createClass(this, "TObject", null, function () {',
  21019. ' this.$init = function () {',
  21020. ' };',
  21021. ' this.$final = function () {',
  21022. ' };',
  21023. ' this.Run = function (w) {',
  21024. ' $mod.THelper.Foo.call(this, 1);',
  21025. ' $mod.THelper.Foo.call(this, 1);',
  21026. ' $mod.THelper.Foo.call(this, 1);',
  21027. ' $mod.THelper.Foo.call(this, 1);',
  21028. ' $mod.THelper.Foo.call(this, 1);',
  21029. ' $mod.THelper.Foo.call(this, 1);',
  21030. ' };',
  21031. '});',
  21032. 'rtl.createHelper(this, "THelper", null, function () {',
  21033. ' this.Foo = function (w) {',
  21034. ' var Result = 0;',
  21035. ' this.Run(10);',
  21036. ' this.Run(10);',
  21037. ' $mod.THelper.Foo.call(this, 1);',
  21038. ' $mod.THelper.Foo.call(this, 1);',
  21039. ' $mod.THelper.Foo.call(this, 1);',
  21040. ' $mod.THelper.Foo.call(this, 1);',
  21041. ' $mod.THelper.Foo.call(this, 1);',
  21042. ' $mod.THelper.Foo.call(this, 1);',
  21043. ' return Result;',
  21044. ' };',
  21045. '});',
  21046. 'this.Obj = null;',
  21047. '']),
  21048. LinesToStr([ // $mod.$main
  21049. '$mod.THelper.Foo.call($mod.Obj.$class, 1);',
  21050. '$mod.THelper.Foo.call($mod.Obj.$class, 1);',
  21051. 'var $with = $mod.Obj;',
  21052. '$mod.THelper.Foo.call($with.$class, 1);',
  21053. '$mod.THelper.Foo.call($with.$class, 1);',
  21054. '$mod.THelper.Foo.call($mod.TObject, 1);',
  21055. '$mod.THelper.Foo.call($mod.TObject, 1);',
  21056. 'var $with1 = $mod.TObject;',
  21057. '$mod.THelper.Foo.call($mod.TObject, 1);',
  21058. '$mod.THelper.Foo.call($mod.TObject, 1);',
  21059. '']));
  21060. end;
  21061. procedure TTestModule.TestClassHelper_ClassOf;
  21062. begin
  21063. StartProgram(false);
  21064. Add([
  21065. 'type',
  21066. ' TObject = class',
  21067. ' end;',
  21068. ' TClass = class of TObject;',
  21069. ' THelper = class helper for TObject',
  21070. ' class function Foo(w: word = 1): word;',
  21071. ' end;',
  21072. 'class function THelper.foo(w: word): word;',
  21073. 'begin',
  21074. 'end;',
  21075. 'var',
  21076. ' c: TClass;',
  21077. 'begin',
  21078. ' c.Foo;',
  21079. ' c.Foo();',
  21080. ' with c do begin',
  21081. ' Foo;',
  21082. ' Foo();',
  21083. ' end;',
  21084. '']);
  21085. ConvertProgram;
  21086. CheckSource('TestClassHelper_ClassOf',
  21087. LinesToStr([ // statements
  21088. 'rtl.createClass(this, "TObject", null, function () {',
  21089. ' this.$init = function () {',
  21090. ' };',
  21091. ' this.$final = function () {',
  21092. ' };',
  21093. '});',
  21094. 'rtl.createHelper(this, "THelper", null, function () {',
  21095. ' this.Foo = function (w) {',
  21096. ' var Result = 0;',
  21097. ' return Result;',
  21098. ' };',
  21099. '});',
  21100. 'this.c = null;',
  21101. '']),
  21102. LinesToStr([ // $mod.$main
  21103. '$mod.THelper.Foo.call($mod.c, 1);',
  21104. '$mod.THelper.Foo.call($mod.c, 1);',
  21105. 'var $with = $mod.c;',
  21106. '$mod.THelper.Foo.call($with, 1);',
  21107. '$mod.THelper.Foo.call($with, 1);',
  21108. '']));
  21109. end;
  21110. procedure TTestModule.TestClassHelper_MethodRefObjFPC;
  21111. begin
  21112. StartProgram(false);
  21113. Add([
  21114. '{$mode objfpc}',
  21115. 'type',
  21116. ' TObject = class',
  21117. ' procedure DoIt;',
  21118. ' end;',
  21119. ' THelper = class helper for TObject',
  21120. ' procedure Fly(w: word = 1);',
  21121. ' class procedure Glide(w: word = 1);',
  21122. ' class procedure Run(w: word = 1); static;',
  21123. ' end;',
  21124. ' TFly = procedure(w: word) of object;',
  21125. ' TGlide = TFly;',
  21126. ' TRun = procedure(w: word);',
  21127. 'var',
  21128. ' f: TFly;',
  21129. ' g: TGlide;',
  21130. ' r: TRun;',
  21131. 'procedure TObject.DoIt;',
  21132. 'begin',
  21133. ' f:=@fly;',
  21134. ' g:=@glide;',
  21135. ' r:=@run;',
  21136. ' f:[email protected];',
  21137. ' g:[email protected];',
  21138. ' r:[email protected];',
  21139. ' with self do begin',
  21140. ' f:=@fly;',
  21141. ' g:=@glide;',
  21142. ' r:=@run;',
  21143. ' end;',
  21144. 'end;',
  21145. 'procedure THelper.fly(w: word);',
  21146. 'begin',
  21147. ' f:=@fly;',
  21148. ' g:=@glide;',
  21149. ' r:=@run;',
  21150. 'end;',
  21151. 'class procedure THelper.glide(w: word);',
  21152. 'begin',
  21153. ' g:=@glide;',
  21154. ' r:=@run;',
  21155. 'end;',
  21156. 'class procedure THelper.run(w: word);',
  21157. 'begin',
  21158. ' g:=@glide;',
  21159. ' r:=@run;',
  21160. 'end;',
  21161. 'var',
  21162. ' Obj: TObject;',
  21163. 'begin',
  21164. ' f:[email protected];',
  21165. ' g:[email protected];',
  21166. ' r:[email protected];',
  21167. ' with obj do begin',
  21168. ' f:=@fly;',
  21169. ' g:=@glide;',
  21170. ' r:=@run;',
  21171. ' end;',
  21172. ' g:[email protected];',
  21173. ' r:[email protected];',
  21174. ' with tobject do begin',
  21175. ' g:=@glide;',
  21176. ' r:=@run;',
  21177. ' end;',
  21178. '']);
  21179. ConvertProgram;
  21180. CheckSource('TestClassHelper_MethodRefObjFPC',
  21181. LinesToStr([ // statements
  21182. 'rtl.createClass(this, "TObject", null, function () {',
  21183. ' this.$init = function () {',
  21184. ' };',
  21185. ' this.$final = function () {',
  21186. ' };',
  21187. ' this.DoIt = function () {',
  21188. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  21189. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  21190. ' $mod.r = $mod.THelper.Run;',
  21191. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  21192. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  21193. ' $mod.r = $mod.THelper.Run;',
  21194. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  21195. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  21196. ' $mod.r = $mod.THelper.Run;',
  21197. ' };',
  21198. '});',
  21199. 'rtl.createHelper(this, "THelper", null, function () {',
  21200. ' this.Fly = function (w) {',
  21201. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  21202. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  21203. ' $mod.r = $mod.THelper.Run;',
  21204. ' };',
  21205. ' this.Glide = function (w) {',
  21206. ' $mod.g = rtl.createCallback(this, $mod.THelper.Glide);',
  21207. ' $mod.r = $mod.THelper.Run;',
  21208. ' };',
  21209. ' this.Run = function (w) {',
  21210. ' $mod.g = rtl.createCallback($mod.THelper, $mod.THelper.Glide);',
  21211. ' $mod.r = $mod.THelper.Run;',
  21212. ' };',
  21213. '});',
  21214. 'this.f = null;',
  21215. 'this.g = null;',
  21216. 'this.r = null;',
  21217. 'this.Obj = null;',
  21218. '']),
  21219. LinesToStr([ // $mod.$main
  21220. '$mod.f = rtl.createCallback($mod.Obj, $mod.THelper.Fly);',
  21221. '$mod.g = rtl.createCallback($mod.Obj.$class, $mod.THelper.Glide);',
  21222. '$mod.r = $mod.THelper.Run;',
  21223. 'var $with = $mod.Obj;',
  21224. '$mod.f = rtl.createCallback($with, $mod.THelper.Fly);',
  21225. '$mod.g = rtl.createCallback($with.$class, $mod.THelper.Glide);',
  21226. '$mod.r = $mod.THelper.Run;',
  21227. '$mod.g = rtl.createCallback($mod.TObject, $mod.THelper.Glide);',
  21228. '$mod.r = $mod.THelper.Run;',
  21229. 'var $with1 = $mod.TObject;',
  21230. '$mod.g = rtl.createCallback($with1, $mod.THelper.Glide);',
  21231. '$mod.r = $mod.THelper.Run;',
  21232. '']));
  21233. end;
  21234. procedure TTestModule.TestClassHelper_Constructor;
  21235. begin
  21236. StartProgram(false);
  21237. Add([
  21238. 'type',
  21239. ' TObject = class',
  21240. ' constructor Create;',
  21241. ' end;',
  21242. ' TClass = class of TObject;',
  21243. ' THelper = class helper for TObject',
  21244. ' constructor NewHlp(w: word);',
  21245. ' end;',
  21246. 'var',
  21247. ' obj: TObject;',
  21248. ' c: TClass;',
  21249. 'constructor TObject.Create;',
  21250. 'begin',
  21251. ' NewHlp(2);', // normal call
  21252. ' tobject.NewHlp(3);', // new instance
  21253. ' c.newhlp(4);', // new instance
  21254. 'end;',
  21255. 'constructor THelper.NewHlp(w: word);',
  21256. 'begin',
  21257. ' create;', // normal call
  21258. ' tobject.create;', // new instance
  21259. ' NewHlp(2);', // normal call
  21260. ' tobject.NewHlp(3);', // new instance
  21261. ' c.newhlp(4);', // new instance
  21262. 'end;',
  21263. 'begin',
  21264. ' obj.newhlp(2);', // normal call
  21265. ' with Obj do newhlp(12);', // normal call
  21266. ' tobject.newhlp(3);', // new instance
  21267. ' with tobject do newhlp(13);', // new instance
  21268. ' c.newhlp(4);', // new instance
  21269. ' with c do newhlp(14);', // new instance
  21270. '']);
  21271. ConvertProgram;
  21272. CheckSource('TestClassHelper_Constructor',
  21273. LinesToStr([ // statements
  21274. 'rtl.createClass(this, "TObject", null, function () {',
  21275. ' this.$init = function () {',
  21276. ' };',
  21277. ' this.$final = function () {',
  21278. ' };',
  21279. ' this.Create = function () {',
  21280. ' $mod.THelper.NewHlp.call(this, 2);',
  21281. ' $mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  21282. ' $mod.c.$create($mod.THelper.NewHlp, [4]);',
  21283. ' return this;',
  21284. ' };',
  21285. '});',
  21286. 'rtl.createHelper(this, "THelper", null, function () {',
  21287. ' this.NewHlp = function (w) {',
  21288. ' this.Create();',
  21289. ' $mod.TObject.$create("Create");',
  21290. ' $mod.THelper.NewHlp.call(this, 2);',
  21291. ' $mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  21292. ' $mod.c.$create($mod.THelper.NewHlp, [4]);',
  21293. ' return this;',
  21294. ' };',
  21295. '});',
  21296. 'this.obj = null;',
  21297. 'this.c = null;',
  21298. '']),
  21299. LinesToStr([ // $mod.$main
  21300. '$mod.THelper.NewHlp.call($mod.obj, 2);',
  21301. 'var $with = $mod.obj;',
  21302. '$mod.THelper.NewHlp.call($with, 12);',
  21303. '$mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  21304. 'var $with1 = $mod.TObject;',
  21305. '$with1.$create($mod.THelper.NewHlp, [13]);',
  21306. '$mod.c.$create($mod.THelper.NewHlp, [4]);',
  21307. 'var $with2 = $mod.c;',
  21308. '$with2.$create($mod.THelper.NewHlp, [14]);',
  21309. '']));
  21310. end;
  21311. procedure TTestModule.TestClassHelper_InheritedObjFPC;
  21312. begin
  21313. StartProgram(false);
  21314. Add([
  21315. 'type',
  21316. ' TObject = class',
  21317. ' procedure Fly;',
  21318. ' end;',
  21319. ' TObjHelper = class helper for TObject',
  21320. ' procedure Fly;',
  21321. ' end;',
  21322. ' TBird = class',
  21323. ' procedure Fly;',
  21324. ' end;',
  21325. ' TBirdHelper = class helper for TBird',
  21326. ' procedure Fly;',
  21327. ' procedure Walk(w: word);',
  21328. ' end;',
  21329. ' TEagleHelper = class helper(TBirdHelper) for TBird',
  21330. ' procedure Fly;',
  21331. ' procedure Walk(w: word);',
  21332. ' end;',
  21333. 'procedure Tobject.fly;',
  21334. 'begin',
  21335. ' inherited;', // ignore
  21336. 'end;',
  21337. 'procedure Tobjhelper.fly;',
  21338. 'begin',
  21339. ' {@TObject_Fly}inherited;',
  21340. ' inherited {@TObject_Fly}Fly;',
  21341. 'end;',
  21342. 'procedure Tbird.fly;',
  21343. 'begin',
  21344. ' {@TObjHelper_Fly}inherited;',
  21345. ' inherited {@TObjHelper_Fly}Fly;',
  21346. 'end;',
  21347. 'procedure Tbirdhelper.fly;',
  21348. 'begin',
  21349. ' {@TBird_Fly}inherited;',
  21350. ' inherited {@TBird_Fly}Fly;',
  21351. 'end;',
  21352. 'procedure Tbirdhelper.walk(w: word);',
  21353. 'begin',
  21354. 'end;',
  21355. 'procedure teagleHelper.fly;',
  21356. 'begin',
  21357. ' {@TBird_Fly}inherited;',
  21358. ' inherited {@TBird_Fly}Fly;',
  21359. 'end;',
  21360. 'procedure teagleHelper.walk(w: word);',
  21361. 'begin',
  21362. ' {@TBirdHelper_Walk}inherited;',
  21363. ' inherited {@TBirdHelper_Walk}Walk(3);',
  21364. 'end;',
  21365. 'begin',
  21366. '']);
  21367. ConvertProgram;
  21368. CheckSource('TestClassHelper_InheritedObjFPC',
  21369. LinesToStr([ // statements
  21370. 'rtl.createClass(this, "TObject", null, function () {',
  21371. ' this.$init = function () {',
  21372. ' };',
  21373. ' this.$final = function () {',
  21374. ' };',
  21375. ' this.Fly = function () {',
  21376. ' };',
  21377. '});',
  21378. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21379. ' this.Fly = function () {',
  21380. ' $mod.TObject.Fly.call(this);',
  21381. ' $mod.TObject.Fly.call(this);',
  21382. ' };',
  21383. '});',
  21384. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21385. ' this.Fly$1 = function () {',
  21386. ' $mod.TObjHelper.Fly.call(this);',
  21387. ' $mod.TObjHelper.Fly.call(this);',
  21388. ' };',
  21389. '});',
  21390. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  21391. ' this.Fly = function () {',
  21392. ' $mod.TBird.Fly$1.call(this);',
  21393. ' $mod.TBird.Fly$1.call(this);',
  21394. ' };',
  21395. ' this.Walk = function (w) {',
  21396. ' };',
  21397. '});',
  21398. 'rtl.createHelper(this, "TEagleHelper", this.TBirdHelper, function () {',
  21399. ' this.Fly$1 = function () {',
  21400. ' $mod.TBird.Fly$1.call(this);',
  21401. ' $mod.TBird.Fly$1.call(this);',
  21402. ' };',
  21403. ' this.Walk$1 = function (w) {',
  21404. ' $mod.TBirdHelper.Walk.apply(this, arguments);',
  21405. ' $mod.TBirdHelper.Walk.call(this, 3);',
  21406. ' };',
  21407. '});',
  21408. '']),
  21409. LinesToStr([ // $mod.$main
  21410. '']));
  21411. end;
  21412. procedure TTestModule.TestClassHelper_Property;
  21413. begin
  21414. StartProgram(false);
  21415. Add([
  21416. 'type',
  21417. ' TObject = class',
  21418. ' FSize: word;',
  21419. ' function GetSpeed: word;',
  21420. ' procedure SetSpeed(Value: word);',
  21421. ' end;',
  21422. ' TObjHelper = class helper for TObject',
  21423. ' function GetLeft: word;',
  21424. ' procedure SetLeft(Value: word);',
  21425. ' property Size: word read FSize write FSize;',
  21426. ' property Speed: word read GetSpeed write SetSpeed;',
  21427. ' property Left: word read GetLeft write SetLeft;',
  21428. ' end;',
  21429. ' TBird = class',
  21430. ' property NotRight: word read GetLeft write SetLeft;',
  21431. ' procedure DoIt;',
  21432. ' end;',
  21433. 'var',
  21434. ' b: TBird;',
  21435. 'function Tobject.GetSpeed: word;',
  21436. 'begin',
  21437. ' Size:=Size+11;',
  21438. ' Speed:=Speed+12;',
  21439. ' Result:=Left+13;',
  21440. ' Left:=13;',
  21441. ' Left:=Left+13;',
  21442. ' Self.Size:=Self.Size+21;',
  21443. ' Self.Speed:=Self.Speed+22;',
  21444. ' Self.Left:=Self.Left+23;',
  21445. ' with Self do begin',
  21446. ' Size:=Size+31;',
  21447. ' Speed:=Speed+32;',
  21448. ' Left:=Left+33;',
  21449. ' end;',
  21450. 'end;',
  21451. 'procedure Tobject.SetSpeed(Value: word);',
  21452. 'begin',
  21453. 'end;',
  21454. 'function TObjHelper.GetLeft: word;',
  21455. 'begin',
  21456. ' Size:=Size+11;',
  21457. ' Speed:=Speed+12;',
  21458. ' Left:=Left+13;',
  21459. ' Self.Size:=Self.Size+21;',
  21460. ' Self.Speed:=Self.Speed+22;',
  21461. ' Self.Left:=Self.Left+23;',
  21462. ' with Self do begin',
  21463. ' Size:=Size+31;',
  21464. ' Speed:=Speed+32;',
  21465. ' Left:=Left+33;',
  21466. ' end;',
  21467. 'end;',
  21468. 'procedure TObjHelper.SetLeft(Value: word);',
  21469. 'begin',
  21470. 'end;',
  21471. 'procedure TBird.DoIt;',
  21472. 'begin',
  21473. ' NotRight:=NotRight+11;',
  21474. ' Self.NotRight:=Self.NotRight+21;',
  21475. ' with Self do begin',
  21476. ' NotRight:=NotRight+31;',
  21477. ' end;',
  21478. 'end;',
  21479. 'begin',
  21480. ' b.Size:=b.Size+11;',
  21481. ' b.Speed:=b.Speed+12;',
  21482. ' b.Left:=b.Left+13;',
  21483. ' b.NotRight:=b.NotRight+14;',
  21484. ' with b do begin',
  21485. ' Size:=Size+31;',
  21486. ' Speed:=Speed+32;',
  21487. ' Left:=Left+33;',
  21488. ' NotRight:=NotRight+34;',
  21489. ' end;',
  21490. '']);
  21491. ConvertProgram;
  21492. CheckSource('TestClassHelper_Property',
  21493. LinesToStr([ // statements
  21494. 'rtl.createClass(this, "TObject", null, function () {',
  21495. ' this.$init = function () {',
  21496. ' this.FSize = 0;',
  21497. ' };',
  21498. ' this.$final = function () {',
  21499. ' };',
  21500. ' this.GetSpeed = function () {',
  21501. ' var Result = 0;',
  21502. ' this.FSize = this.FSize + 11;',
  21503. ' this.SetSpeed(this.GetSpeed() + 12);',
  21504. ' Result = $mod.TObjHelper.GetLeft.call(this) + 13;',
  21505. ' $mod.TObjHelper.SetLeft.call(this, 13);',
  21506. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  21507. ' this.FSize = this.FSize + 21;',
  21508. ' this.SetSpeed(this.GetSpeed() + 22);',
  21509. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  21510. ' this.FSize = this.FSize + 31;',
  21511. ' this.SetSpeed(this.GetSpeed() + 32);',
  21512. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  21513. ' return Result;',
  21514. ' };',
  21515. ' this.SetSpeed = function (Value) {',
  21516. ' };',
  21517. '});',
  21518. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21519. ' this.GetLeft = function () {',
  21520. ' var Result = 0;',
  21521. ' this.FSize = this.FSize + 11;',
  21522. ' this.SetSpeed(this.GetSpeed() + 12);',
  21523. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  21524. ' this.FSize = this.FSize + 21;',
  21525. ' this.SetSpeed(this.GetSpeed() + 22);',
  21526. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  21527. ' this.FSize = this.FSize + 31;',
  21528. ' this.SetSpeed(this.GetSpeed() + 32);',
  21529. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  21530. ' return Result;',
  21531. ' };',
  21532. ' this.SetLeft = function (Value) {',
  21533. ' };',
  21534. '});',
  21535. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21536. ' this.DoIt = function () {',
  21537. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 11);',
  21538. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 21);',
  21539. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 31);',
  21540. ' };',
  21541. '});',
  21542. 'this.b = null;',
  21543. '']),
  21544. LinesToStr([ // $mod.$main
  21545. '$mod.b.FSize = $mod.b.FSize + 11;',
  21546. '$mod.b.SetSpeed($mod.b.GetSpeed() + 12);',
  21547. '$mod.TObjHelper.SetLeft.call($mod.b, $mod.TObjHelper.GetLeft.call($mod.b) + 13);',
  21548. '$mod.TObjHelper.SetLeft.call($mod.b, $mod.TObjHelper.GetLeft.call($mod.b) + 14);',
  21549. 'var $with = $mod.b;',
  21550. '$with.FSize = $with.FSize + 31;',
  21551. '$with.SetSpeed($with.GetSpeed() + 32);',
  21552. '$mod.TObjHelper.SetLeft.call($with, $mod.TObjHelper.GetLeft.call($with) + 33);',
  21553. '$mod.TObjHelper.SetLeft.call($with, $mod.TObjHelper.GetLeft.call($with) + 34);',
  21554. '']));
  21555. end;
  21556. procedure TTestModule.TestClassHelper_Property_Array;
  21557. begin
  21558. StartProgram(false);
  21559. Add([
  21560. 'type',
  21561. ' TObject = class',
  21562. ' function GetSpeed(Index: boolean): word;',
  21563. ' procedure SetSpeed(Index: boolean; Value: word);',
  21564. ' end;',
  21565. ' TObjHelper = class helper for TObject',
  21566. ' function GetSize(Index: boolean): word;',
  21567. ' procedure SetSize(Index: boolean; Value: word);',
  21568. ' property Size[Index: boolean]: word read GetSize write SetSize;',
  21569. ' property Speed[Index: boolean]: word read GetSpeed write SetSpeed;',
  21570. ' end;',
  21571. ' TBird = class',
  21572. ' property Items[Index: boolean]: word read GetSize write SetSize;',
  21573. ' procedure DoIt;',
  21574. ' end;',
  21575. 'var',
  21576. ' b: TBird;',
  21577. 'function Tobject.GetSpeed(Index: boolean): word;',
  21578. 'begin',
  21579. ' Result:=Size[false];',
  21580. ' Size[true]:=Size[false]+11;',
  21581. ' Speed[true]:=Speed[false]+12;',
  21582. ' Self.Size[true]:=Self.Size[false]+21;',
  21583. ' Self.Speed[true]:=Self.Speed[false]+22;',
  21584. ' with Self do begin',
  21585. ' Size[true]:=Size[false]+31;',
  21586. ' Speed[true]:=Speed[false]+32;',
  21587. ' end;',
  21588. 'end;',
  21589. 'procedure Tobject.SetSpeed(Index: boolean; Value: word);',
  21590. 'begin',
  21591. 'end;',
  21592. 'function TObjHelper.GetSize(Index: boolean): word;',
  21593. 'begin',
  21594. ' Size[true]:=Size[false]+11;',
  21595. ' Speed[true]:=Speed[false]+12;',
  21596. ' Self.Size[true]:=Self.Size[false]+21;',
  21597. ' Self.Speed[true]:=Self.Speed[false]+22;',
  21598. ' with Self do begin',
  21599. ' Size[true]:=Size[false]+31;',
  21600. ' Speed[true]:=Speed[false]+32;',
  21601. ' end;',
  21602. 'end;',
  21603. 'procedure TObjHelper.SetSize(Index: boolean; Value: word);',
  21604. 'begin',
  21605. 'end;',
  21606. 'procedure TBird.DoIt;',
  21607. 'begin',
  21608. ' Items[true]:=Items[false]+11;',
  21609. ' Self.Items[true]:=Self.Items[false]+21;',
  21610. ' with Self do Items[true]:=Items[false]+31;',
  21611. 'end;',
  21612. 'begin',
  21613. ' b.Size[true]:=b.Size[false]+11;',
  21614. ' b.Speed[true]:=b.Speed[false]+12;',
  21615. ' b.Items[true]:=b.Items[false]+13;',
  21616. ' with b do begin',
  21617. ' Size[true]:=Size[false]+21;',
  21618. ' Speed[true]:=Speed[false]+22;',
  21619. ' Items[true]:=Items[false]+23;',
  21620. ' end;',
  21621. '']);
  21622. ConvertProgram;
  21623. CheckSource('TestClassHelper_Property_Array',
  21624. LinesToStr([ // statements
  21625. 'rtl.createClass(this, "TObject", null, function () {',
  21626. ' this.$init = function () {',
  21627. ' };',
  21628. ' this.$final = function () {',
  21629. ' };',
  21630. ' this.GetSpeed = function (Index) {',
  21631. ' var Result = 0;',
  21632. ' Result = $mod.TObjHelper.GetSize.call(this, false);',
  21633. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  21634. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  21635. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  21636. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  21637. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  21638. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  21639. ' return Result;',
  21640. ' };',
  21641. ' this.SetSpeed = function (Index, Value) {',
  21642. ' };',
  21643. '});',
  21644. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21645. ' this.GetSize = function (Index) {',
  21646. ' var Result = 0;',
  21647. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  21648. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  21649. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  21650. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  21651. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  21652. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  21653. ' return Result;',
  21654. ' };',
  21655. ' this.SetSize = function (Index, Value) {',
  21656. ' };',
  21657. '});',
  21658. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21659. ' this.DoIt = function () {',
  21660. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  21661. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  21662. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  21663. ' };',
  21664. '});',
  21665. 'this.b = null;',
  21666. '']),
  21667. LinesToStr([ // $mod.$main
  21668. '$mod.TObjHelper.SetSize.call($mod.b, true, $mod.TObjHelper.GetSize.call($mod.b, false) + 11);',
  21669. '$mod.b.SetSpeed(true, $mod.b.GetSpeed(false) + 12);',
  21670. '$mod.TObjHelper.SetSize.call($mod.b, true, $mod.TObjHelper.GetSize.call($mod.b, false) + 13);',
  21671. 'var $with = $mod.b;',
  21672. '$mod.TObjHelper.SetSize.call($with, true, $mod.TObjHelper.GetSize.call($with, false) + 21);',
  21673. '$with.SetSpeed(true, $with.GetSpeed(false) + 22);',
  21674. '$mod.TObjHelper.SetSize.call($with, true, $mod.TObjHelper.GetSize.call($with, false) + 23);',
  21675. '']));
  21676. end;
  21677. procedure TTestModule.TestClassHelper_Property_Array_Default;
  21678. begin
  21679. StartProgram(false);
  21680. Add([
  21681. 'type',
  21682. ' TObject = class',
  21683. ' function GetSpeed(Index: boolean): word;',
  21684. ' procedure SetSpeed(Index: boolean; Value: word);',
  21685. ' end;',
  21686. ' TObjHelper = class helper for TObject',
  21687. ' property Speed[Index: boolean]: word read GetSpeed write SetSpeed; default;',
  21688. ' end;',
  21689. ' TBird = class',
  21690. ' end;',
  21691. ' TBirdHelper = class helper for TBird',
  21692. ' function GetSize(Index: word): boolean;',
  21693. ' procedure SetSize(Index: word; Value: boolean);',
  21694. ' property Size[Index: word]: boolean read GetSize write SetSize; default;',
  21695. ' end;',
  21696. 'function Tobject.GetSpeed(Index: boolean): word;',
  21697. 'begin',
  21698. ' Self[true]:=Self[false]+1;',
  21699. 'end;',
  21700. 'procedure Tobject.SetSpeed(Index: boolean; Value: word);',
  21701. 'begin',
  21702. 'end;',
  21703. 'function TBirdHelper.GetSize(Index: word): boolean;',
  21704. 'begin',
  21705. ' Self[1]:=not Self[2];',
  21706. 'end;',
  21707. 'procedure TBirdHelper.SetSize(Index: word; Value: boolean);',
  21708. 'begin',
  21709. 'end;',
  21710. 'var',
  21711. ' o: TObject;',
  21712. ' b: TBird;',
  21713. 'begin',
  21714. ' o[true]:=o[false]+1;',
  21715. ' b[3]:=not b[4];',
  21716. '']);
  21717. ConvertProgram;
  21718. CheckSource('TestClassHelper_Property_Array_Default',
  21719. LinesToStr([ // statements
  21720. 'rtl.createClass(this, "TObject", null, function () {',
  21721. ' this.$init = function () {',
  21722. ' };',
  21723. ' this.$final = function () {',
  21724. ' };',
  21725. ' this.GetSpeed = function (Index) {',
  21726. ' var Result = 0;',
  21727. ' this.SetSpeed(true, this.GetSpeed(false) + 1);',
  21728. ' return Result;',
  21729. ' };',
  21730. ' this.SetSpeed = function (Index, Value) {',
  21731. ' };',
  21732. '});',
  21733. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21734. '});',
  21735. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21736. '});',
  21737. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  21738. ' this.GetSize = function (Index) {',
  21739. ' var Result = false;',
  21740. ' $mod.TBirdHelper.SetSize.call(this, 1, !$mod.TBirdHelper.GetSize.call(this, 2));',
  21741. ' return Result;',
  21742. ' };',
  21743. ' this.SetSize = function (Index, Value) {',
  21744. ' };',
  21745. '});',
  21746. 'this.o = null;',
  21747. 'this.b = null;',
  21748. '']),
  21749. LinesToStr([ // $mod.$main
  21750. '$mod.o.SetSpeed(true, $mod.o.GetSpeed(false) + 1);',
  21751. '$mod.TBirdHelper.SetSize.call($mod.b, 3, !$mod.TBirdHelper.GetSize.call($mod.b, 4));',
  21752. '']));
  21753. end;
  21754. procedure TTestModule.TestClassHelper_Property_Array_DefaultDefault;
  21755. begin
  21756. StartProgram(false);
  21757. Add([
  21758. 'type',
  21759. ' TObject = class',
  21760. ' end;',
  21761. ' TObjHelper = class helper for TObject',
  21762. ' function GetItems(Index: word): TObject;',
  21763. ' procedure SetItems(Index: word; Value: TObject);',
  21764. ' property Items[Index: word]: TObject read GetItems write SetItems; default;',
  21765. ' end;',
  21766. 'function Tobjhelper.GetItems(Index: word): TObject;',
  21767. 'begin',
  21768. ' Self[1][2]:=Self[3][4];',
  21769. 'end;',
  21770. 'procedure Tobjhelper.SetItems(Index: word; Value: TObject);',
  21771. 'begin',
  21772. 'end;',
  21773. 'var',
  21774. ' o: TObject;',
  21775. 'begin',
  21776. ' o[1][2]:=o[3][4];',
  21777. '']);
  21778. ConvertProgram;
  21779. CheckSource('TestClassHelper_Property_Array_DefaultDefault',
  21780. LinesToStr([ // statements
  21781. 'rtl.createClass(this, "TObject", null, function () {',
  21782. ' this.$init = function () {',
  21783. ' };',
  21784. ' this.$final = function () {',
  21785. ' };',
  21786. '});',
  21787. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21788. ' this.GetItems = function (Index) {',
  21789. ' var Result = null;',
  21790. ' $mod.TObjHelper.SetItems.call($mod.TObjHelper.GetItems.call(this, 1), 2, $mod.TObjHelper.GetItems.call($mod.TObjHelper.GetItems.call(this, 3), 4));',
  21791. ' return Result;',
  21792. ' };',
  21793. ' this.SetItems = function (Index, Value) {',
  21794. ' };',
  21795. '});',
  21796. 'this.o = null;',
  21797. '']),
  21798. LinesToStr([ // $mod.$main
  21799. '$mod.TObjHelper.SetItems.call($mod.TObjHelper.GetItems.call($mod.o, 1), 2, $mod.TObjHelper.GetItems.call($mod.TObjHelper.GetItems.call($mod.o, 3), 4));',
  21800. '']));
  21801. end;
  21802. procedure TTestModule.TestClassHelper_ClassProperty;
  21803. begin
  21804. StartProgram(false);
  21805. Add([
  21806. 'type',
  21807. ' TObject = class',
  21808. ' class var FSize: word;',
  21809. ' class function GetSpeed: word;',
  21810. ' class procedure SetSpeed(Value: word); virtual; abstract;',
  21811. ' end;',
  21812. ' TObjHelper = class helper for TObject',
  21813. ' class function GetLeft: word;',
  21814. ' class procedure SetLeft(Value: word);',
  21815. ' class property Size: word read FSize write FSize;',
  21816. ' class property Speed: word read GetSpeed write SetSpeed;',
  21817. ' class property Left: word read GetLeft write SetLeft;',
  21818. ' end;',
  21819. ' TBird = class',
  21820. ' class property NotRight: word read GetLeft write SetLeft;',
  21821. ' class procedure DoIt;',
  21822. ' end;',
  21823. ' TBirdClass = class of TBird;',
  21824. 'class function Tobject.GetSpeed: word;',
  21825. 'begin',
  21826. ' Size:=Size+11;',
  21827. ' Speed:=Speed+12;',
  21828. ' Left:=Left+13;',
  21829. ' Self.Size:=Self.Size+21;',
  21830. ' Self.Speed:=Self.Speed+22;',
  21831. ' Self.Left:=Self.Left+23;',
  21832. ' with Self do begin',
  21833. ' Size:=Size+31;',
  21834. ' Speed:=Speed+32;',
  21835. ' Left:=Left+33;',
  21836. ' end;',
  21837. 'end;',
  21838. 'class function TObjHelper.GetLeft: word;',
  21839. 'begin',
  21840. ' Size:=Size+11;',
  21841. ' Speed:=Speed+12;',
  21842. ' Left:=Left+13;',
  21843. ' Self.Size:=Self.Size+21;',
  21844. ' Self.Speed:=Self.Speed+22;',
  21845. ' Self.Left:=Self.Left+23;',
  21846. ' with Self do begin',
  21847. ' Size:=Size+31;',
  21848. ' Speed:=Speed+32;',
  21849. ' Left:=Left+33;',
  21850. ' end;',
  21851. 'end;',
  21852. 'class procedure TObjHelper.SetLeft(Value: word);',
  21853. 'begin',
  21854. 'end;',
  21855. 'class procedure TBird.DoIt;',
  21856. 'begin',
  21857. ' NotRight:=NotRight+11;',
  21858. ' Self.NotRight:=Self.NotRight+21;',
  21859. ' with Self do NotRight:=NotRight+31;',
  21860. 'end;',
  21861. 'var',
  21862. ' b: TBird;',
  21863. ' c: TBirdClass;',
  21864. 'begin',
  21865. ' b.Size:=b.Size+11;',
  21866. ' b.Speed:=b.Speed+12;',
  21867. ' b.Left:=b.Left+13;',
  21868. ' b.NotRight:=b.NotRight+14;',
  21869. ' with b do begin',
  21870. ' Size:=Size+31;',
  21871. ' Speed:=Speed+32;',
  21872. ' Left:=Left+33;',
  21873. ' NotRight:=NotRight+34;',
  21874. ' end;',
  21875. ' c.Size:=c.Size+11;',
  21876. ' c.Speed:=c.Speed+12;',
  21877. ' c.Left:=c.Left+13;',
  21878. ' c.NotRight:=c.NotRight+14;',
  21879. ' with c do begin',
  21880. ' Size:=Size+31;',
  21881. ' Speed:=Speed+32;',
  21882. ' Left:=Left+33;',
  21883. ' NotRight:=NotRight+34;',
  21884. ' end;',
  21885. ' tbird.Size:=tbird.Size+11;',
  21886. ' tbird.Speed:=tbird.Speed+12;',
  21887. ' tbird.Left:=tbird.Left+13;',
  21888. ' tbird.NotRight:=tbird.NotRight+14;',
  21889. ' with tbird do begin',
  21890. ' Size:=Size+31;',
  21891. ' Speed:=Speed+32;',
  21892. ' Left:=Left+33;',
  21893. ' NotRight:=NotRight+34;',
  21894. ' end;',
  21895. '']);
  21896. ConvertProgram;
  21897. CheckSource('TestClassHelper_ClassProperty',
  21898. LinesToStr([ // statements
  21899. 'rtl.createClass(this, "TObject", null, function () {',
  21900. ' this.FSize = 0;',
  21901. ' this.$init = function () {',
  21902. ' };',
  21903. ' this.$final = function () {',
  21904. ' };',
  21905. ' this.GetSpeed = function () {',
  21906. ' var Result = 0;',
  21907. ' $mod.TObject.FSize = this.FSize + 11;',
  21908. ' this.SetSpeed(this.GetSpeed() + 12);',
  21909. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  21910. ' $mod.TObject.FSize = this.FSize + 21;',
  21911. ' this.SetSpeed(this.GetSpeed() + 22);',
  21912. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  21913. ' $mod.TObject.FSize = this.FSize + 31;',
  21914. ' this.SetSpeed(this.GetSpeed() + 32);',
  21915. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  21916. ' return Result;',
  21917. ' };',
  21918. '});',
  21919. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21920. ' this.GetLeft = function () {',
  21921. ' var Result = 0;',
  21922. ' $mod.TObject.FSize = this.FSize + 11;',
  21923. ' this.SetSpeed(this.GetSpeed() + 12);',
  21924. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  21925. ' $mod.TObject.FSize = this.FSize + 21;',
  21926. ' this.SetSpeed(this.GetSpeed() + 22);',
  21927. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  21928. ' $mod.TObject.FSize = this.FSize + 31;',
  21929. ' this.SetSpeed(this.GetSpeed() + 32);',
  21930. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  21931. ' return Result;',
  21932. ' };',
  21933. ' this.SetLeft = function (Value) {',
  21934. ' };',
  21935. '});',
  21936. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21937. ' this.DoIt = function () {',
  21938. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 11);',
  21939. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 21);',
  21940. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 31);',
  21941. ' };',
  21942. '});',
  21943. 'this.b = null;',
  21944. 'this.c = null;',
  21945. '']),
  21946. LinesToStr([ // $mod.$main
  21947. '$mod.TObject.FSize = $mod.b.FSize + 11;',
  21948. '$mod.b.$class.SetSpeed($mod.b.$class.GetSpeed() + 12);',
  21949. '$mod.TObjHelper.SetLeft.call($mod.b.$class, $mod.TObjHelper.GetLeft.call($mod.b.$class) + 13);',
  21950. '$mod.TObjHelper.SetLeft.call($mod.b.$class, $mod.TObjHelper.GetLeft.call($mod.b.$class) + 14);',
  21951. 'var $with = $mod.b;',
  21952. '$mod.TObject.FSize = $with.FSize + 31;',
  21953. '$with.$class.SetSpeed($with.$class.GetSpeed() + 32);',
  21954. '$mod.TObjHelper.SetLeft.call($with.$class, $mod.TObjHelper.GetLeft.call($with.$class) + 33);',
  21955. '$mod.TObjHelper.SetLeft.call($with.$class, $mod.TObjHelper.GetLeft.call($with.$class) + 34);',
  21956. '$mod.TObject.FSize = $mod.c.FSize + 11;',
  21957. '$mod.c.SetSpeed($mod.c.GetSpeed() + 12);',
  21958. '$mod.TObjHelper.SetLeft.call($mod.c, $mod.TObjHelper.GetLeft.call($mod.c) + 13);',
  21959. '$mod.TObjHelper.SetLeft.call($mod.c, $mod.TObjHelper.GetLeft.call($mod.c) + 14);',
  21960. 'var $with1 = $mod.c;',
  21961. '$mod.TObject.FSize = $with1.FSize + 31;',
  21962. '$with1.SetSpeed($with1.GetSpeed() + 32);',
  21963. '$mod.TObjHelper.SetLeft.call($with1, $mod.TObjHelper.GetLeft.call($with1) + 33);',
  21964. '$mod.TObjHelper.SetLeft.call($with1, $mod.TObjHelper.GetLeft.call($with1) + 34);',
  21965. '$mod.TObject.FSize = $mod.TBird.FSize + 11;',
  21966. '$mod.TBird.SetSpeed($mod.TBird.GetSpeed() + 12);',
  21967. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 13);',
  21968. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 14);',
  21969. 'var $with2 = $mod.TBird;',
  21970. '$mod.TObject.FSize = $with2.FSize + 31;',
  21971. '$with2.SetSpeed($with2.GetSpeed() + 32);',
  21972. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 33);',
  21973. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 34);',
  21974. '']));
  21975. end;
  21976. procedure TTestModule.TestClassHelper_ClassPropertyStatic;
  21977. begin
  21978. StartProgram(false);
  21979. Add([
  21980. 'type',
  21981. ' TObject = class',
  21982. ' class function GetSpeed: word; static;',
  21983. ' class procedure SetSpeed(Value: word); static;',
  21984. ' end;',
  21985. ' TObjHelper = class helper for TObject',
  21986. ' class function GetLeft: word; static;',
  21987. ' class procedure SetLeft(Value: word); static;',
  21988. ' class property Speed: word read GetSpeed write SetSpeed;',
  21989. ' class property Left: word read GetLeft write SetLeft;',
  21990. ' end;',
  21991. ' TBird = class',
  21992. ' class property NotRight: word read GetLeft write SetLeft;',
  21993. ' class procedure DoIt; static;',
  21994. ' class procedure DoSome;',
  21995. ' end;',
  21996. ' TBirdClass = class of TBird;',
  21997. 'class function Tobject.GetSpeed: word;',
  21998. 'begin',
  21999. ' Speed:=Speed+12;',
  22000. ' Left:=Left+13;',
  22001. 'end;',
  22002. 'class procedure TObject.SetSpeed(Value: word);',
  22003. 'begin',
  22004. 'end;',
  22005. 'class function TObjHelper.GetLeft: word;',
  22006. 'begin',
  22007. ' Speed:=Speed+12;',
  22008. ' Left:=Left+13;',
  22009. 'end;',
  22010. 'class procedure TObjHelper.SetLeft(Value: word);',
  22011. 'begin',
  22012. 'end;',
  22013. 'class procedure TBird.DoIt;',
  22014. 'begin',
  22015. ' NotRight:=NotRight+11;',
  22016. 'end;',
  22017. 'class procedure TBird.DoSome;',
  22018. 'begin',
  22019. ' Speed:=Speed+12;',
  22020. ' Left:=Left+13;',
  22021. ' Self.Speed:=Self.Speed+22;',
  22022. ' Self.Left:=Self.Left+23;',
  22023. ' with Self do begin',
  22024. ' Speed:=Speed+32;',
  22025. ' Left:=Left+33;',
  22026. ' end;',
  22027. ' NotRight:=NotRight+11;',
  22028. ' Self.NotRight:=Self.NotRight+21;',
  22029. ' with Self do NotRight:=NotRight+31;',
  22030. 'end;',
  22031. 'var',
  22032. ' b: TBird;',
  22033. ' c: TBirdClass;',
  22034. 'begin',
  22035. ' b.Speed:=b.Speed+12;',
  22036. ' b.Left:=b.Left+13;',
  22037. ' b.NotRight:=b.NotRight+14;',
  22038. ' with b do begin',
  22039. ' Speed:=Speed+32;',
  22040. ' Left:=Left+33;',
  22041. ' NotRight:=NotRight+34;',
  22042. ' end;',
  22043. ' c.Speed:=c.Speed+12;',
  22044. ' c.Left:=c.Left+13;',
  22045. ' c.NotRight:=c.NotRight+14;',
  22046. ' with c do begin',
  22047. ' Speed:=Speed+32;',
  22048. ' Left:=Left+33;',
  22049. ' NotRight:=NotRight+34;',
  22050. ' end;',
  22051. ' tbird.Speed:=tbird.Speed+12;',
  22052. ' tbird.Left:=tbird.Left+13;',
  22053. ' tbird.NotRight:=tbird.NotRight+14;',
  22054. ' with tbird do begin',
  22055. ' Speed:=Speed+32;',
  22056. ' Left:=Left+33;',
  22057. ' NotRight:=NotRight+34;',
  22058. ' end;',
  22059. '']);
  22060. ConvertProgram;
  22061. CheckSource('TestClassHelper_ClassPropertyStatic',
  22062. LinesToStr([ // statements
  22063. 'rtl.createClass(this, "TObject", null, function () {',
  22064. ' this.$init = function () {',
  22065. ' };',
  22066. ' this.$final = function () {',
  22067. ' };',
  22068. ' this.GetSpeed = function () {',
  22069. ' var Result = 0;',
  22070. ' $mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  22071. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  22072. ' return Result;',
  22073. ' };',
  22074. ' this.SetSpeed = function (Value) {',
  22075. ' };',
  22076. '});',
  22077. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  22078. ' this.GetLeft = function () {',
  22079. ' var Result = 0;',
  22080. ' $mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  22081. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  22082. ' return Result;',
  22083. ' };',
  22084. ' this.SetLeft = function (Value) {',
  22085. ' };',
  22086. '});',
  22087. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  22088. ' this.DoIt = function () {',
  22089. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 11);',
  22090. ' };',
  22091. ' this.DoSome = function () {',
  22092. ' this.SetSpeed(this.GetSpeed() + 12);',
  22093. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  22094. ' this.SetSpeed(this.GetSpeed() + 22);',
  22095. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 23);',
  22096. ' this.SetSpeed(this.GetSpeed() + 32);',
  22097. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  22098. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 11);',
  22099. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 21);',
  22100. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 31);',
  22101. ' };',
  22102. '});',
  22103. 'this.b = null;',
  22104. 'this.c = null;',
  22105. '']),
  22106. LinesToStr([ // $mod.$main
  22107. '$mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  22108. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  22109. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  22110. 'var $with = $mod.b;',
  22111. '$with.SetSpeed($with.GetSpeed() + 32);',
  22112. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  22113. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  22114. '$mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  22115. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  22116. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  22117. 'var $with1 = $mod.c;',
  22118. '$with1.SetSpeed($with1.GetSpeed() + 32);',
  22119. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  22120. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  22121. '$mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  22122. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  22123. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  22124. 'var $with2 = $mod.TBird;',
  22125. '$with2.SetSpeed($with2.GetSpeed() + 32);',
  22126. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  22127. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  22128. '']));
  22129. end;
  22130. procedure TTestModule.TestClassHelper_ClassProperty_Array;
  22131. begin
  22132. StartProgram(false);
  22133. Add([
  22134. 'type',
  22135. ' TObject = class',
  22136. ' class function GetSpeed(Index: boolean): word;',
  22137. ' class procedure SetSpeed(Index: boolean; Value: word); virtual; abstract;',
  22138. ' end;',
  22139. ' TObjHelper = class helper for TObject',
  22140. ' class function GetSize(Index: boolean): word;',
  22141. ' class procedure SetSize(Index: boolean; Value: word);',
  22142. ' class property Size[Index: boolean]: word read GetSize write SetSize;',
  22143. ' class property Speed[Index: boolean]: word read GetSpeed write SetSpeed;',
  22144. ' end;',
  22145. ' TBird = class',
  22146. ' class property Items[Index: boolean]: word read GetSize write SetSize;',
  22147. ' class procedure DoIt;',
  22148. ' end;',
  22149. ' TBirdClass = class of TBird;',
  22150. 'class function Tobject.GetSpeed(Index: boolean): word;',
  22151. 'begin',
  22152. ' Size[true]:=Size[false]+11;',
  22153. ' Speed[true]:=Speed[false]+12;',
  22154. ' Self.Size[true]:=Self.Size[false]+21;',
  22155. ' Self.Speed[true]:=Self.Speed[false]+22;',
  22156. ' with Self do begin',
  22157. ' Size[true]:=Size[false]+31;',
  22158. ' Speed[true]:=Speed[false]+32;',
  22159. ' end;',
  22160. 'end;',
  22161. 'class function TObjHelper.GetSize(Index: boolean): word;',
  22162. 'begin',
  22163. ' Size[true]:=Size[false]+11;',
  22164. ' Speed[true]:=Speed[false]+12;',
  22165. ' Self.Size[true]:=Self.Size[false]+21;',
  22166. ' Self.Speed[true]:=Self.Speed[false]+22;',
  22167. ' with Self do begin',
  22168. ' Size[true]:=Size[false]+31;',
  22169. ' Speed[true]:=Speed[false]+32;',
  22170. ' end;',
  22171. 'end;',
  22172. 'class procedure TObjHelper.SetSize(Index: boolean; Value: word);',
  22173. 'begin',
  22174. 'end;',
  22175. 'class procedure TBird.DoIt;',
  22176. 'begin',
  22177. ' Items[true]:=Items[false]+11;',
  22178. ' Self.Items[true]:=Self.Items[false]+21;',
  22179. ' with Self do Items[true]:=Items[false]+31;',
  22180. 'end;',
  22181. 'var',
  22182. ' b: TBird;',
  22183. ' c: TBirdClass;',
  22184. 'begin',
  22185. ' b.Size[true]:=b.Size[false]+11;',
  22186. ' b.Speed[true]:=b.Speed[false]+12;',
  22187. ' b.Items[true]:=b.Items[false]+13;',
  22188. ' with b do begin',
  22189. ' Size[true]:=Size[false]+21;',
  22190. ' Speed[true]:=Speed[false]+22;',
  22191. ' Items[true]:=Items[false]+23;',
  22192. ' end;',
  22193. ' c.Size[true]:=c.Size[false]+11;',
  22194. ' c.Speed[true]:=c.Speed[false]+12;',
  22195. ' c.Items[true]:=c.Items[false]+13;',
  22196. ' with c do begin',
  22197. ' Size[true]:=Size[false]+21;',
  22198. ' Speed[true]:=Speed[false]+22;',
  22199. ' Items[true]:=Items[false]+23;',
  22200. ' end;',
  22201. ' TBird.Size[true]:=TBird.Size[false]+11;',
  22202. ' TBird.Speed[true]:=TBird.Speed[false]+12;',
  22203. ' TBird.Items[true]:=TBird.Items[false]+13;',
  22204. ' with TBird do begin',
  22205. ' Size[true]:=Size[false]+21;',
  22206. ' Speed[true]:=Speed[false]+22;',
  22207. ' Items[true]:=Items[false]+23;',
  22208. ' end;',
  22209. '']);
  22210. ConvertProgram;
  22211. CheckSource('TestClassHelper_ClassProperty_Array',
  22212. LinesToStr([ // statements
  22213. 'rtl.createClass(this, "TObject", null, function () {',
  22214. ' this.$init = function () {',
  22215. ' };',
  22216. ' this.$final = function () {',
  22217. ' };',
  22218. ' this.GetSpeed = function (Index) {',
  22219. ' var Result = 0;',
  22220. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  22221. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  22222. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  22223. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  22224. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  22225. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  22226. ' return Result;',
  22227. ' };',
  22228. '});',
  22229. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  22230. ' this.GetSize = function (Index) {',
  22231. ' var Result = 0;',
  22232. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  22233. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  22234. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  22235. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  22236. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  22237. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  22238. ' return Result;',
  22239. ' };',
  22240. ' this.SetSize = function (Index, Value) {',
  22241. ' };',
  22242. '});',
  22243. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  22244. ' this.DoIt = function () {',
  22245. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  22246. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  22247. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  22248. ' };',
  22249. '});',
  22250. 'this.b = null;',
  22251. 'this.c = null;',
  22252. '']),
  22253. LinesToStr([ // $mod.$main
  22254. '$mod.TObjHelper.SetSize.call($mod.b.$class, true, $mod.TObjHelper.GetSize.call($mod.b.$class, false) + 11);',
  22255. '$mod.b.$class.SetSpeed(true, $mod.b.$class.GetSpeed(false) + 12);',
  22256. '$mod.TObjHelper.SetSize.call($mod.b.$class, true, $mod.TObjHelper.GetSize.call($mod.b.$class, false) + 13);',
  22257. 'var $with = $mod.b;',
  22258. '$mod.TObjHelper.SetSize.call($with.$class, true, $mod.TObjHelper.GetSize.call($with.$class, false) + 21);',
  22259. '$with.$class.SetSpeed(true, $with.$class.GetSpeed(false) + 22);',
  22260. '$mod.TObjHelper.SetSize.call($with.$class, true, $mod.TObjHelper.GetSize.call($with.$class, false) + 23);',
  22261. '$mod.TObjHelper.SetSize.call($mod.c, true, $mod.TObjHelper.GetSize.call($mod.c, false) + 11);',
  22262. '$mod.c.SetSpeed(true, $mod.c.GetSpeed(false) + 12);',
  22263. '$mod.TObjHelper.SetSize.call($mod.c, true, $mod.TObjHelper.GetSize.call($mod.c, false) + 13);',
  22264. 'var $with1 = $mod.c;',
  22265. '$mod.TObjHelper.SetSize.call($with1, true, $mod.TObjHelper.GetSize.call($with1, false) + 21);',
  22266. '$with1.SetSpeed(true, $with1.GetSpeed(false) + 22);',
  22267. '$mod.TObjHelper.SetSize.call($with1, true, $mod.TObjHelper.GetSize.call($with1, false) + 23);',
  22268. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 11);',
  22269. '$mod.TBird.SetSpeed(true, $mod.TBird.GetSpeed(false) + 12);',
  22270. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 13);',
  22271. 'var $with2 = $mod.TBird;',
  22272. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 21);',
  22273. '$with2.SetSpeed(true, $with2.GetSpeed(false) + 22);',
  22274. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 23);',
  22275. '']));
  22276. end;
  22277. procedure TTestModule.TestClassHelper_ForIn;
  22278. begin
  22279. StartProgram(false);
  22280. Add([
  22281. 'type',
  22282. ' TObject = class end;',
  22283. ' TItem = TObject;',
  22284. ' TEnumerator = class',
  22285. ' FCurrent: TItem;',
  22286. ' property Current: TItem read FCurrent;',
  22287. ' function MoveNext: boolean;',
  22288. ' end;',
  22289. ' TBird = class',
  22290. ' end;',
  22291. ' TBirdHelper = class helper for TBird',
  22292. ' function GetEnumerator: TEnumerator;',
  22293. ' end;',
  22294. 'function TEnumerator.MoveNext: boolean;',
  22295. 'begin',
  22296. 'end;',
  22297. 'function TBirdHelper.GetEnumerator: TEnumerator;',
  22298. 'begin',
  22299. 'end;',
  22300. 'var',
  22301. ' b: TBird;',
  22302. ' i, i2: TItem;',
  22303. 'begin',
  22304. ' for i in b do i2:=i;']);
  22305. ConvertProgram;
  22306. CheckSource('TestClassHelper_ForIn',
  22307. LinesToStr([ // statements
  22308. 'rtl.createClass(this, "TObject", null, function () {',
  22309. ' this.$init = function () {',
  22310. ' };',
  22311. ' this.$final = function () {',
  22312. ' };',
  22313. '});',
  22314. 'rtl.createClass(this, "TEnumerator", this.TObject, function () {',
  22315. ' this.$init = function () {',
  22316. ' $mod.TObject.$init.call(this);',
  22317. ' this.FCurrent = null;',
  22318. ' };',
  22319. ' this.$final = function () {',
  22320. ' this.FCurrent = undefined;',
  22321. ' $mod.TObject.$final.call(this);',
  22322. ' };',
  22323. ' this.MoveNext = function () {',
  22324. ' var Result = false;',
  22325. ' return Result;',
  22326. ' };',
  22327. '});',
  22328. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  22329. '});',
  22330. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  22331. ' this.GetEnumerator = function () {',
  22332. ' var Result = null;',
  22333. ' return Result;',
  22334. ' };',
  22335. '});',
  22336. 'this.b = null;',
  22337. 'this.i = null;',
  22338. 'this.i2 = null;'
  22339. ]),
  22340. LinesToStr([ // $mod.$main
  22341. 'var $in = $mod.TBirdHelper.GetEnumerator.call($mod.b);',
  22342. 'try {',
  22343. ' while ($in.MoveNext()){',
  22344. ' $mod.i = $in.FCurrent;',
  22345. ' $mod.i2 = $mod.i;',
  22346. ' }',
  22347. '} finally {',
  22348. ' $in = rtl.freeLoc($in)',
  22349. '};',
  22350. '']));
  22351. end;
  22352. procedure TTestModule.TestClassHelper_PassProperty;
  22353. begin
  22354. StartProgram(false);
  22355. Add([
  22356. 'type',
  22357. ' TObject = class',
  22358. ' FField: TObject;',
  22359. ' property Field: TObject read FField write FField;',
  22360. ' end;',
  22361. ' THelper = class helper for TObject',
  22362. ' procedure Fly;',
  22363. ' class procedure Run;',
  22364. ' class procedure Jump; static;',
  22365. ' end;',
  22366. 'procedure THelper.Fly;',
  22367. 'begin',
  22368. ' Field.Fly;',
  22369. ' Field.Run;',
  22370. ' Field.Jump;',
  22371. ' with Field do begin',
  22372. ' Fly;',
  22373. ' Run;',
  22374. ' Jump;',
  22375. ' end;',
  22376. 'end;',
  22377. 'class procedure THelper.Run;',
  22378. 'begin',
  22379. 'end;',
  22380. 'class procedure THelper.Jump;',
  22381. 'begin',
  22382. 'end;',
  22383. 'var',
  22384. ' b: TObject;',
  22385. 'begin',
  22386. ' b.Field.Fly;',
  22387. ' b.Field.Run;',
  22388. ' b.Field.Jump;',
  22389. ' with b do begin',
  22390. ' Field.Run;',
  22391. ' Field.Fly;',
  22392. ' Field.Jump;',
  22393. ' end;',
  22394. ' with b.Field do begin',
  22395. ' Run;',
  22396. ' Fly;',
  22397. ' Jump;',
  22398. ' end;',
  22399. '']);
  22400. ConvertProgram;
  22401. CheckSource('TestClassHelper_PassProperty',
  22402. LinesToStr([ // statements
  22403. 'rtl.createClass(this, "TObject", null, function () {',
  22404. ' this.$init = function () {',
  22405. ' this.FField = null;',
  22406. ' };',
  22407. ' this.$final = function () {',
  22408. ' this.FField = undefined;',
  22409. ' };',
  22410. '});',
  22411. 'rtl.createHelper(this, "THelper", null, function () {',
  22412. ' this.Fly = function () {',
  22413. ' $mod.THelper.Fly.call(this.FField);',
  22414. ' $mod.THelper.Run.call(this.FField.$class);',
  22415. ' $mod.THelper.Jump();',
  22416. ' var $with = this.FField;',
  22417. ' $mod.THelper.Fly.call($with);',
  22418. ' $mod.THelper.Run.call($with.$class);',
  22419. ' $mod.THelper.Jump();',
  22420. ' };',
  22421. ' this.Run = function () {',
  22422. ' };',
  22423. ' this.Jump = function () {',
  22424. ' };',
  22425. '});',
  22426. 'this.b = null;',
  22427. '']),
  22428. LinesToStr([ // $mod.$main
  22429. '$mod.THelper.Fly.call($mod.b.FField);',
  22430. '$mod.THelper.Run.call($mod.b.FField.$class);',
  22431. '$mod.THelper.Jump();',
  22432. 'var $with = $mod.b;',
  22433. '$mod.THelper.Run.call($with.FField.$class);',
  22434. '$mod.THelper.Fly.call($with.FField);',
  22435. '$mod.THelper.Jump();',
  22436. 'var $with1 = $mod.b.FField;',
  22437. '$mod.THelper.Run.call($with1.$class);',
  22438. '$mod.THelper.Fly.call($with1);',
  22439. '$mod.THelper.Jump();',
  22440. '']));
  22441. end;
  22442. procedure TTestModule.TestExtClassHelper_ClassVar;
  22443. begin
  22444. StartProgram(false);
  22445. Add([
  22446. '{$modeswitch externalclass}',
  22447. 'type',
  22448. ' TExtA = class external name ''ExtObj''',
  22449. ' end;',
  22450. ' THelper = class helper for TExtA',
  22451. ' const',
  22452. ' One = 1;',
  22453. ' Two: word = 2;',
  22454. ' class var',
  22455. ' Glob: word;',
  22456. ' function Foo(w: word): word;',
  22457. ' class function Bar(w: word): word; static;',
  22458. ' end;',
  22459. 'function THelper.foo(w: word): word;',
  22460. 'begin',
  22461. ' Result:=w;',
  22462. ' Two:=One+w;',
  22463. ' Glob:=Glob;',
  22464. ' Result:=Self.Glob;',
  22465. ' Self.Glob:=Self.Glob;',
  22466. ' with Self do Glob:=Glob;',
  22467. 'end;',
  22468. 'class function THelper.bar(w: word): word;',
  22469. 'begin',
  22470. ' Result:=w;',
  22471. ' Two:=One;',
  22472. ' Glob:=Glob;',
  22473. 'end;',
  22474. 'var o: TExtA;',
  22475. 'begin',
  22476. ' texta.two:=texta.one;',
  22477. ' texta.Glob:=texta.Glob;',
  22478. ' with texta do begin',
  22479. ' two:=one;',
  22480. ' Glob:=Glob;',
  22481. ' end;',
  22482. ' o.two:=o.one;',
  22483. ' o.Glob:=o.Glob;',
  22484. ' with o do begin',
  22485. ' two:=one;',
  22486. ' Glob:=Glob;',
  22487. ' end;',
  22488. '']);
  22489. ConvertProgram;
  22490. CheckSource('TestExtClassHelper_ClassVar',
  22491. LinesToStr([ // statements
  22492. 'rtl.createHelper(this, "THelper", null, function () {',
  22493. ' this.One = 1;',
  22494. ' this.Two = 2;',
  22495. ' this.Glob = 0;',
  22496. ' this.Foo = function (w) {',
  22497. ' var Result = 0;',
  22498. ' Result = w;',
  22499. ' $mod.THelper.Two = 1 + w;',
  22500. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22501. ' Result = $mod.THelper.Glob;',
  22502. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22503. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22504. ' return Result;',
  22505. ' };',
  22506. ' this.Bar = function (w) {',
  22507. ' var Result = 0;',
  22508. ' Result = w;',
  22509. ' $mod.THelper.Two = 1;',
  22510. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22511. ' return Result;',
  22512. ' };',
  22513. '});',
  22514. 'this.o = null;',
  22515. '']),
  22516. LinesToStr([ // $mod.$main
  22517. '$mod.THelper.Two = 1;',
  22518. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22519. '$mod.THelper.Two = 1;',
  22520. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22521. '$mod.THelper.Two = 1;',
  22522. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22523. 'var $with = $mod.o;',
  22524. '$mod.THelper.Two = 1;',
  22525. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22526. '']));
  22527. end;
  22528. procedure TTestModule.TestExtClassHelper_Method_Call;
  22529. begin
  22530. StartProgram(false);
  22531. Add([
  22532. '{$modeswitch externalclass}',
  22533. 'type',
  22534. ' TFly = function(w: word): word of object;',
  22535. ' TExtA = class external name ''ExtObj''',
  22536. ' procedure Run(w: word = 10);',
  22537. ' end;',
  22538. ' THelper = class helper for TExtA',
  22539. ' function Foo(w: word = 1): word;',
  22540. ' function Fly(w: word = 2): word; external name ''Fly'';',
  22541. ' end;',
  22542. 'var p: TFly;',
  22543. 'function THelper.foo(w: word): word;',
  22544. 'begin',
  22545. ' Run;',
  22546. ' Run();',
  22547. ' Run(11);',
  22548. ' Foo;',
  22549. ' Foo();',
  22550. ' Foo(12);',
  22551. ' Self.Foo;',
  22552. ' Self.Foo();',
  22553. ' Self.Foo(13);',
  22554. ' Fly;',
  22555. ' Fly();',
  22556. ' with Self do begin',
  22557. ' Foo;',
  22558. ' Foo();',
  22559. ' Foo(14);',
  22560. ' Fly;',
  22561. ' Fly();',
  22562. ' end;',
  22563. ' p:=@Fly;',
  22564. 'end;',
  22565. 'var Obj: TExtA;',
  22566. 'begin',
  22567. ' obj.Foo;',
  22568. ' obj.Foo();',
  22569. ' obj.Foo(21);',
  22570. ' obj.Fly;',
  22571. ' obj.Fly();',
  22572. ' with obj do begin',
  22573. ' Foo;',
  22574. ' Foo();',
  22575. ' Foo(22);',
  22576. ' Fly;',
  22577. ' Fly();',
  22578. ' end;',
  22579. ' p:[email protected];',
  22580. '']);
  22581. ConvertProgram;
  22582. CheckSource('TestExtClassHelper_Method_Call',
  22583. LinesToStr([ // statements
  22584. 'rtl.createHelper(this, "THelper", null, function () {',
  22585. ' this.Foo = function (w) {',
  22586. ' var Result = 0;',
  22587. ' this.Run(10);',
  22588. ' this.Run(10);',
  22589. ' this.Run(11);',
  22590. ' $mod.THelper.Foo.call(this, 1);',
  22591. ' $mod.THelper.Foo.call(this, 1);',
  22592. ' $mod.THelper.Foo.call(this, 12);',
  22593. ' $mod.THelper.Foo.call(this, 1);',
  22594. ' $mod.THelper.Foo.call(this, 1);',
  22595. ' $mod.THelper.Foo.call(this, 13);',
  22596. ' this.Fly(2);',
  22597. ' this.Fly(2);',
  22598. ' $mod.THelper.Foo.call(this, 1);',
  22599. ' $mod.THelper.Foo.call(this, 1);',
  22600. ' $mod.THelper.Foo.call(this, 14);',
  22601. ' this.Fly(2);',
  22602. ' this.Fly(2);',
  22603. ' $mod.p = rtl.createCallback(this, "Fly");',
  22604. ' return Result;',
  22605. ' };',
  22606. '});',
  22607. 'this.p = null;',
  22608. 'this.Obj = null;',
  22609. '']),
  22610. LinesToStr([ // $mod.$main
  22611. '$mod.THelper.Foo.call($mod.Obj, 1);',
  22612. '$mod.THelper.Foo.call($mod.Obj, 1);',
  22613. '$mod.THelper.Foo.call($mod.Obj, 21);',
  22614. '$mod.Obj.Fly(2);',
  22615. '$mod.Obj.Fly(2);',
  22616. 'var $with = $mod.Obj;',
  22617. '$mod.THelper.Foo.call($with, 1);',
  22618. '$mod.THelper.Foo.call($with, 1);',
  22619. '$mod.THelper.Foo.call($with, 22);',
  22620. '$with.Fly(2);',
  22621. '$with.Fly(2);',
  22622. '$mod.p = rtl.createCallback($mod.Obj, "Fly");',
  22623. '']));
  22624. end;
  22625. procedure TTestModule.TestExtClassHelper_ClassMethod_MissingStatic;
  22626. begin
  22627. StartProgram(false);
  22628. Add([
  22629. '{$modeswitch externalclass}',
  22630. 'type',
  22631. ' TExtA = class external name ''ExtObj''',
  22632. ' procedure Run(w: word = 10);',
  22633. ' end;',
  22634. ' THelper = class helper for TExtA',
  22635. ' class procedure Fly;',
  22636. ' end;',
  22637. 'class procedure THelper.Fly;',
  22638. 'begin end;',
  22639. 'begin',
  22640. '']);
  22641. SetExpectedPasResolverError(sHelperClassMethodForExtClassMustBeStatic,
  22642. nHelperClassMethodForExtClassMustBeStatic);
  22643. ConvertProgram;
  22644. end;
  22645. procedure TTestModule.TestRecordHelper_ClassVar;
  22646. begin
  22647. StartProgram(false);
  22648. Add([
  22649. 'type',
  22650. ' TRec = record',
  22651. ' end;',
  22652. ' THelper = record helper for TRec',
  22653. ' const',
  22654. ' One = 1;',
  22655. ' Two: word = 2;',
  22656. ' class var',
  22657. ' Glob: word;',
  22658. ' function Foo(w: word): word;',
  22659. ' class function Bar(w: word): word; static;',
  22660. ' end;',
  22661. 'function THelper.foo(w: word): word;',
  22662. 'begin',
  22663. ' Result:=w;',
  22664. ' Two:=One+w;',
  22665. ' Glob:=Glob;',
  22666. ' Result:=Self.Glob;',
  22667. ' Self.Glob:=Self.Glob;',
  22668. ' with Self do Glob:=Glob;',
  22669. ' Self:=Self;',
  22670. 'end;',
  22671. 'class function THelper.bar(w: word): word;',
  22672. 'begin',
  22673. ' Result:=w;',
  22674. ' Two:=One;',
  22675. ' Glob:=Glob;',
  22676. 'end;',
  22677. 'var r: TRec;',
  22678. 'begin',
  22679. ' trec.two:=trec.one;',
  22680. ' trec.Glob:=trec.Glob;',
  22681. ' with trec do begin',
  22682. ' two:=one;',
  22683. ' Glob:=Glob;',
  22684. ' end;',
  22685. ' r.two:=r.one;',
  22686. ' r.Glob:=r.Glob;',
  22687. ' with r do begin',
  22688. ' two:=one;',
  22689. ' Glob:=Glob;',
  22690. ' end;',
  22691. '']);
  22692. ConvertProgram;
  22693. CheckSource('TestRecordHelper_ClassVar',
  22694. LinesToStr([ // statements
  22695. 'rtl.recNewT(this, "TRec", function () {',
  22696. ' this.$eq = function (b) {',
  22697. ' return true;',
  22698. ' };',
  22699. ' this.$assign = function (s) {',
  22700. ' return this;',
  22701. ' };',
  22702. '});',
  22703. 'rtl.createHelper(this, "THelper", null, function () {',
  22704. ' this.One = 1;',
  22705. ' this.Two = 2;',
  22706. ' this.Glob = 0;',
  22707. ' this.Foo = function (w) {',
  22708. ' var Result = 0;',
  22709. ' Result = w;',
  22710. ' $mod.THelper.Two = 1 + w;',
  22711. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22712. ' Result = $mod.THelper.Glob;',
  22713. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22714. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22715. ' this.$assign(this);',
  22716. ' return Result;',
  22717. ' };',
  22718. ' this.Bar = function (w) {',
  22719. ' var Result = 0;',
  22720. ' Result = w;',
  22721. ' $mod.THelper.Two = 1;',
  22722. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22723. ' return Result;',
  22724. ' };',
  22725. '});',
  22726. 'this.r = this.TRec.$new();',
  22727. '']),
  22728. LinesToStr([ // $mod.$main
  22729. '$mod.THelper.Two = 1;',
  22730. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22731. 'var $with = $mod.TRec;',
  22732. '$mod.THelper.Two = 1;',
  22733. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22734. '$mod.THelper.Two = 1;',
  22735. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22736. 'var $with1 = $mod.r;',
  22737. '$mod.THelper.Two = 1;',
  22738. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22739. '']));
  22740. end;
  22741. procedure TTestModule.TestRecordHelper_Method_Call;
  22742. begin
  22743. StartProgram(false);
  22744. Add([
  22745. '{$modeswitch AdvancedRecords}',
  22746. 'type',
  22747. ' TRec = record',
  22748. ' procedure Run(w: word = 10);',
  22749. ' end;',
  22750. ' THelper = record helper for TRec',
  22751. ' function Foo(w: word = 1): word;',
  22752. ' end;',
  22753. 'procedure TRec.Run(w: word);',
  22754. 'begin',
  22755. ' Foo;',
  22756. ' Foo();',
  22757. ' Foo(2);',
  22758. ' Self.Foo;',
  22759. ' Self.Foo();',
  22760. ' Self.Foo(3);',
  22761. ' with Self do begin',
  22762. ' Foo;',
  22763. ' Foo();',
  22764. ' Foo(4);',
  22765. ' end;',
  22766. 'end;',
  22767. 'function THelper.foo(w: word): word;',
  22768. 'begin',
  22769. ' Run;',
  22770. ' Run();',
  22771. ' Run(11);',
  22772. ' Foo;',
  22773. ' Foo();',
  22774. ' Foo(12);',
  22775. ' Self.Foo;',
  22776. ' Self.Foo();',
  22777. ' Self.Foo(13);',
  22778. ' with Self do begin',
  22779. ' Foo;',
  22780. ' Foo();',
  22781. ' Foo(14);',
  22782. ' end;',
  22783. 'end;',
  22784. 'var Rec: TRec;',
  22785. 'begin',
  22786. ' Rec.Foo;',
  22787. ' Rec.Foo();',
  22788. ' Rec.Foo(21);',
  22789. ' with Rec do begin',
  22790. ' Foo;',
  22791. ' Foo();',
  22792. ' Foo(22);',
  22793. ' end;',
  22794. '']);
  22795. ConvertProgram;
  22796. CheckSource('TestRecordHelper_Method_Call',
  22797. LinesToStr([ // statements
  22798. 'rtl.recNewT(this, "TRec", function () {',
  22799. ' this.$eq = function (b) {',
  22800. ' return true;',
  22801. ' };',
  22802. ' this.$assign = function (s) {',
  22803. ' return this;',
  22804. ' };',
  22805. ' this.Run = function (w) {',
  22806. ' $mod.THelper.Foo.call(this, 1);',
  22807. ' $mod.THelper.Foo.call(this, 1);',
  22808. ' $mod.THelper.Foo.call(this, 2);',
  22809. ' $mod.THelper.Foo.call(this, 1);',
  22810. ' $mod.THelper.Foo.call(this, 1);',
  22811. ' $mod.THelper.Foo.call(this, 3);',
  22812. ' $mod.THelper.Foo.call(this, 1);',
  22813. ' $mod.THelper.Foo.call(this, 1);',
  22814. ' $mod.THelper.Foo.call(this, 4);',
  22815. ' };',
  22816. '});',
  22817. 'rtl.createHelper(this, "THelper", null, function () {',
  22818. ' this.Foo = function (w) {',
  22819. ' var Result = 0;',
  22820. ' this.Run(10);',
  22821. ' this.Run(10);',
  22822. ' this.Run(11);',
  22823. ' $mod.THelper.Foo.call(this, 1);',
  22824. ' $mod.THelper.Foo.call(this, 1);',
  22825. ' $mod.THelper.Foo.call(this, 12);',
  22826. ' $mod.THelper.Foo.call(this, 1);',
  22827. ' $mod.THelper.Foo.call(this, 1);',
  22828. ' $mod.THelper.Foo.call(this, 13);',
  22829. ' $mod.THelper.Foo.call(this, 1);',
  22830. ' $mod.THelper.Foo.call(this, 1);',
  22831. ' $mod.THelper.Foo.call(this, 14);',
  22832. ' return Result;',
  22833. ' };',
  22834. '});',
  22835. 'this.Rec = this.TRec.$new();',
  22836. '']),
  22837. LinesToStr([ // $mod.$main
  22838. '$mod.THelper.Foo.call($mod.Rec, 1);',
  22839. '$mod.THelper.Foo.call($mod.Rec, 1);',
  22840. '$mod.THelper.Foo.call($mod.Rec, 21);',
  22841. 'var $with = $mod.Rec;',
  22842. '$mod.THelper.Foo.call($with, 1);',
  22843. '$mod.THelper.Foo.call($with, 1);',
  22844. '$mod.THelper.Foo.call($with, 22);',
  22845. '']));
  22846. end;
  22847. procedure TTestModule.TestRecordHelper_Constructor;
  22848. begin
  22849. StartProgram(false);
  22850. Add([
  22851. '{$modeswitch AdvancedRecords}',
  22852. 'type',
  22853. ' TRec = record',
  22854. ' constructor Create(w: word);',
  22855. ' end;',
  22856. ' THelper = record helper for TRec',
  22857. ' constructor NewHlp(w: word);',
  22858. ' end;',
  22859. 'var',
  22860. ' Rec: TRec;',
  22861. 'constructor TRec.Create(w: word);',
  22862. 'begin',
  22863. ' NewHlp(2);', // normal call
  22864. ' trec.NewHlp(3);', // new instance
  22865. 'end;',
  22866. 'constructor THelper.NewHlp(w: word);',
  22867. 'begin',
  22868. ' create(2);', // normal call
  22869. ' trec.create(3);', // new instance
  22870. ' NewHlp(4);', // normal call
  22871. ' trec.NewHlp(5);', // new instance
  22872. 'end;',
  22873. 'begin',
  22874. ' rec.newhlp(2);', // normal call
  22875. ' with rec do newhlp(12);', // normal call
  22876. ' trec.newhlp(3);', // new instance
  22877. ' with trec do newhlp(13);', // new instance
  22878. '']);
  22879. ConvertProgram;
  22880. CheckSource('TestRecordHelper_Constructor',
  22881. LinesToStr([ // statements
  22882. 'rtl.recNewT(this, "TRec", function () {',
  22883. ' this.$eq = function (b) {',
  22884. ' return true;',
  22885. ' };',
  22886. ' this.$assign = function (s) {',
  22887. ' return this;',
  22888. ' };',
  22889. ' this.Create = function (w) {',
  22890. ' $mod.THelper.NewHlp.call(this, 2);',
  22891. ' $mod.THelper.$new("NewHlp", [3]);',
  22892. ' return this;',
  22893. ' };',
  22894. '});',
  22895. 'rtl.createHelper(this, "THelper", null, function () {',
  22896. ' this.NewHlp = function (w) {',
  22897. ' this.Create(2);',
  22898. ' $mod.TRec.$new().Create(3);',
  22899. ' $mod.THelper.NewHlp.call(this, 4);',
  22900. ' $mod.THelper.$new("NewHlp", [5]);',
  22901. ' return this;',
  22902. ' };',
  22903. ' this.$new = function (fn, args) {',
  22904. ' return this[fn].apply($mod.TRec.$new(), args);',
  22905. ' };',
  22906. '});',
  22907. 'this.Rec = this.TRec.$new();',
  22908. '']),
  22909. LinesToStr([ // $mod.$main
  22910. '$mod.THelper.NewHlp.call($mod.Rec, 2);',
  22911. 'var $with = $mod.Rec;',
  22912. '$mod.THelper.NewHlp.call($with, 12);',
  22913. '$mod.THelper.$new("NewHlp", [3]);',
  22914. 'var $with1 = $mod.TRec;',
  22915. '$mod.THelper.$new("NewHlp", [13]);',
  22916. '']));
  22917. end;
  22918. procedure TTestModule.TestTypeHelper_ClassVar;
  22919. begin
  22920. StartProgram(false);
  22921. Add([
  22922. '{$modeswitch typehelpers}',
  22923. 'type',
  22924. ' THelper = type helper for byte',
  22925. ' const',
  22926. ' One = 1;',
  22927. ' Two: word = 2;',
  22928. ' class var',
  22929. ' Glob: word;',
  22930. ' function Foo(w: word): word;',
  22931. ' class function Bar(w: word): word; static;',
  22932. ' end;',
  22933. 'function THelper.foo(w: word): word;',
  22934. 'begin',
  22935. ' Result:=w;',
  22936. ' Two:=One+w;',
  22937. ' Glob:=Glob;',
  22938. ' Result:=Self.Glob;',
  22939. ' Self.Glob:=Self.Glob;',
  22940. ' with Self do Glob:=Glob;',
  22941. 'end;',
  22942. 'class function THelper.bar(w: word): word;',
  22943. 'begin',
  22944. ' Result:=w;',
  22945. ' Two:=One;',
  22946. ' Glob:=Glob;',
  22947. 'end;',
  22948. 'var b: byte;',
  22949. 'begin',
  22950. ' byte.two:=byte.one;',
  22951. ' byte.Glob:=byte.Glob;',
  22952. ' with byte do begin',
  22953. ' two:=one;',
  22954. ' Glob:=Glob;',
  22955. ' end;',
  22956. ' b.two:=b.one;',
  22957. ' b.Glob:=b.Glob;',
  22958. ' with b do begin',
  22959. ' two:=one;',
  22960. ' Glob:=Glob;',
  22961. ' end;',
  22962. '']);
  22963. ConvertProgram;
  22964. CheckSource('TestTypeHelper_ClassVar',
  22965. LinesToStr([ // statements
  22966. 'rtl.createHelper(this, "THelper", null, function () {',
  22967. ' this.One = 1;',
  22968. ' this.Two = 2;',
  22969. ' this.Glob = 0;',
  22970. ' this.Foo = function (w) {',
  22971. ' var Result = 0;',
  22972. ' Result = w;',
  22973. ' $mod.THelper.Two = 1 + w;',
  22974. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22975. ' Result = $mod.THelper.Glob;',
  22976. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22977. ' var $with = this.get();',
  22978. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22979. ' return Result;',
  22980. ' };',
  22981. ' this.Bar = function (w) {',
  22982. ' var Result = 0;',
  22983. ' Result = w;',
  22984. ' $mod.THelper.Two = 1;',
  22985. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22986. ' return Result;',
  22987. ' };',
  22988. '});',
  22989. 'this.b = 0;',
  22990. '']),
  22991. LinesToStr([ // $mod.$main
  22992. '$mod.THelper.Two = 1;',
  22993. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22994. '$mod.THelper.Two = 1;',
  22995. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22996. '$mod.THelper.Two = 1;',
  22997. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22998. 'var $with = $mod.b;',
  22999. '$mod.THelper.Two = 1;',
  23000. '$mod.THelper.Glob = $mod.THelper.Glob;',
  23001. '']));
  23002. end;
  23003. procedure TTestModule.TestTypeHelper_PassResultElement;
  23004. begin
  23005. StartProgram(false);
  23006. Add([
  23007. '{$modeswitch typehelpers}',
  23008. 'type',
  23009. ' THelper = type helper for word',
  23010. ' procedure DoIt(e: byte = 123);',
  23011. ' class procedure DoSome(e: byte = 456); static;',
  23012. ' end;',
  23013. 'procedure THelper.DoIt(e: byte);',
  23014. 'begin',
  23015. 'end;',
  23016. 'class procedure THelper.DoSome(e: byte);',
  23017. 'begin',
  23018. 'end;',
  23019. 'function Foo(w: word): word;',
  23020. 'begin',
  23021. ' Result.DoIt;',
  23022. ' Result.DoIt();',
  23023. ' Result.DoSome;',
  23024. ' Result.DoSome();',
  23025. ' with Result do begin',
  23026. ' DoIt;',
  23027. ' DoIt();',
  23028. ' DoSome;',
  23029. ' DoSome();',
  23030. ' end;',
  23031. 'end;',
  23032. 'begin',
  23033. '']);
  23034. ConvertProgram;
  23035. CheckSource('TestTypeHelper_PassResultElement',
  23036. LinesToStr([ // statements
  23037. 'rtl.createHelper(this, "THelper", null, function () {',
  23038. ' this.DoIt = function (e) {',
  23039. ' };',
  23040. ' this.DoSome = function (e) {',
  23041. ' };',
  23042. '});',
  23043. 'this.Foo = function (w) {',
  23044. ' var Result = 0;',
  23045. ' $mod.THelper.DoIt.call({',
  23046. ' get: function () {',
  23047. ' return Result;',
  23048. ' },',
  23049. ' set: function (v) {',
  23050. ' Result = v;',
  23051. ' }',
  23052. ' }, 123);',
  23053. ' $mod.THelper.DoIt.call({',
  23054. ' get: function () {',
  23055. ' return Result;',
  23056. ' },',
  23057. ' set: function (v) {',
  23058. ' Result = v;',
  23059. ' }',
  23060. ' }, 123);',
  23061. ' $mod.THelper.DoSome(456);',
  23062. ' $mod.THelper.DoSome(456);',
  23063. ' $mod.THelper.DoIt.call({',
  23064. ' get: function () {',
  23065. ' return Result;',
  23066. ' },',
  23067. ' set: function (v) {',
  23068. ' Result = v;',
  23069. ' }',
  23070. ' }, 123);',
  23071. ' $mod.THelper.DoIt.call({',
  23072. ' get: function () {',
  23073. ' return Result;',
  23074. ' },',
  23075. ' set: function (v) {',
  23076. ' Result = v;',
  23077. ' }',
  23078. ' }, 123);',
  23079. ' $mod.THelper.DoSome(456);',
  23080. ' $mod.THelper.DoSome(456);',
  23081. ' return Result;',
  23082. '};',
  23083. '']),
  23084. LinesToStr([ // $mod.$main
  23085. '']));
  23086. end;
  23087. procedure TTestModule.TestTypeHelper_PassArgs;
  23088. begin
  23089. StartProgram(false);
  23090. Add([
  23091. '{$modeswitch typehelpers}',
  23092. 'type',
  23093. ' THelper = type helper for word',
  23094. ' procedure DoIt(e: byte = 123);',
  23095. ' end;',
  23096. 'procedure THelper.DoIt(e: byte);',
  23097. 'begin',
  23098. 'end;',
  23099. 'procedure FooDefault(a: word);',
  23100. 'begin',
  23101. ' a.DoIt;',
  23102. ' with a do DoIt;',
  23103. 'end;',
  23104. 'procedure FooConst(const a: word);',
  23105. 'begin',
  23106. ' a.DoIt;',
  23107. ' with a do DoIt;',
  23108. 'end;',
  23109. 'procedure FooVar(var a: word);',
  23110. 'begin',
  23111. ' a.DoIt;',
  23112. ' with a do DoIt;',
  23113. 'end;',
  23114. 'begin',
  23115. '']);
  23116. ConvertProgram;
  23117. CheckSource('TestTypeHelper_PassArgs',
  23118. LinesToStr([ // statements
  23119. 'rtl.createHelper(this, "THelper", null, function () {',
  23120. ' this.DoIt = function (e) {',
  23121. ' };',
  23122. '});',
  23123. 'this.FooDefault = function (a) {',
  23124. ' $mod.THelper.DoIt.call({',
  23125. ' get: function () {',
  23126. ' return a;',
  23127. ' },',
  23128. ' set: function (v) {',
  23129. ' a = v;',
  23130. ' }',
  23131. ' }, 123);',
  23132. ' $mod.THelper.DoIt.call({',
  23133. ' get: function () {',
  23134. ' return a;',
  23135. ' },',
  23136. ' set: function (v) {',
  23137. ' a = v;',
  23138. ' }',
  23139. ' }, 123);',
  23140. '};',
  23141. 'this.FooConst = function (a) {',
  23142. ' $mod.THelper.DoIt.call({',
  23143. ' get: function () {',
  23144. ' return a;',
  23145. ' },',
  23146. ' set: function (v) {',
  23147. ' rtl.raiseE("EPropReadOnly");',
  23148. ' }',
  23149. ' }, 123);',
  23150. ' $mod.THelper.DoIt.call({',
  23151. ' get: function () {',
  23152. ' return a;',
  23153. ' },',
  23154. ' set: function () {',
  23155. ' rtl.raiseE("EPropReadOnly");',
  23156. ' }',
  23157. ' }, 123);',
  23158. '};',
  23159. 'this.FooVar = function (a) {',
  23160. ' $mod.THelper.DoIt.call(a, 123);',
  23161. ' var $with = a.get();',
  23162. ' $mod.THelper.DoIt.call(a, 123);',
  23163. '};',
  23164. '']),
  23165. LinesToStr([ // $mod.$main
  23166. '']));
  23167. end;
  23168. procedure TTestModule.TestTypeHelper_PassVarConst;
  23169. begin
  23170. StartProgram(false);
  23171. Add([
  23172. '{$modeswitch typehelpers}',
  23173. 'type',
  23174. ' THelper = type helper for word',
  23175. ' procedure DoIt(e: byte = 123);',
  23176. ' end;',
  23177. 'procedure THelper.DoIt(e: byte);',
  23178. 'begin',
  23179. 'end;',
  23180. 'var a: word;',
  23181. 'const c: word = 2;',
  23182. '{$writeableconst off}',
  23183. 'const r: word = 3;',
  23184. 'begin',
  23185. ' a.DoIt;',
  23186. ' with a do DoIt;',
  23187. ' c.DoIt;',
  23188. ' with c do DoIt;',
  23189. ' r.DoIt;',
  23190. ' with r do DoIt;',
  23191. '']);
  23192. ConvertProgram;
  23193. CheckSource('TestTypeHelper_PassVarConst',
  23194. LinesToStr([ // statements
  23195. 'rtl.createHelper(this, "THelper", null, function () {',
  23196. ' this.DoIt = function (e) {',
  23197. ' };',
  23198. '});',
  23199. 'this.a = 0;',
  23200. 'this.c = 2;',
  23201. 'this.r = 3;',
  23202. '']),
  23203. LinesToStr([ // $mod.$main
  23204. '$mod.THelper.DoIt.call({',
  23205. ' p: $mod,',
  23206. ' get: function () {',
  23207. ' return this.p.a;',
  23208. ' },',
  23209. ' set: function (v) {',
  23210. ' this.p.a = v;',
  23211. ' }',
  23212. '}, 123);',
  23213. 'var $with = $mod.a;',
  23214. '$mod.THelper.DoIt.call({',
  23215. ' get: function () {',
  23216. ' return $with;',
  23217. ' },',
  23218. ' set: function (v) {',
  23219. ' $with = v;',
  23220. ' }',
  23221. '}, 123);',
  23222. '$mod.THelper.DoIt.call({',
  23223. ' p: $mod,',
  23224. ' get: function () {',
  23225. ' return this.p.c;',
  23226. ' },',
  23227. ' set: function (v) {',
  23228. ' this.p.c = v;',
  23229. ' }',
  23230. '}, 123);',
  23231. 'var $with1 = $mod.c;',
  23232. '$mod.THelper.DoIt.call({',
  23233. ' get: function () {',
  23234. ' return $with1;',
  23235. ' },',
  23236. ' set: function (v) {',
  23237. ' $with1 = v;',
  23238. ' }',
  23239. '}, 123);',
  23240. '$mod.THelper.DoIt.call({',
  23241. ' get: function () {',
  23242. ' return 3;',
  23243. ' },',
  23244. ' set: function (v) {',
  23245. ' rtl.raiseE("EPropReadOnly");',
  23246. ' }',
  23247. '}, 123);',
  23248. 'var $with2 = 3;',
  23249. ' $mod.THelper.DoIt.call({',
  23250. ' get: function () {',
  23251. ' return $with2;',
  23252. ' },',
  23253. ' set: function () {',
  23254. ' rtl.raiseE("EPropReadOnly");',
  23255. ' }',
  23256. ' }, 123);',
  23257. '']));
  23258. end;
  23259. procedure TTestModule.TestTypeHelper_PassFuncResult;
  23260. begin
  23261. StartProgram(false);
  23262. Add([
  23263. '{$modeswitch typehelpers}',
  23264. 'type',
  23265. ' THelper = type helper for word',
  23266. ' procedure DoIt(e: byte = 123);',
  23267. ' end;',
  23268. 'procedure THelper.DoIt(e: byte);',
  23269. 'begin',
  23270. 'end;',
  23271. 'function Foo(b: byte = 1): word;',
  23272. 'begin',
  23273. 'end;',
  23274. 'begin',
  23275. ' Foo.DoIt;',
  23276. ' Foo().DoIt;',
  23277. ' with Foo do DoIt;',
  23278. ' with Foo() do DoIt;',
  23279. '']);
  23280. ConvertProgram;
  23281. CheckSource('TestTypeHelper_PassFuncResult',
  23282. LinesToStr([ // statements
  23283. 'rtl.createHelper(this, "THelper", null, function () {',
  23284. ' this.DoIt = function (e) {',
  23285. ' };',
  23286. '});',
  23287. 'this.Foo = function (b) {',
  23288. ' var Result = 0;',
  23289. ' return Result;',
  23290. '};',
  23291. '']),
  23292. LinesToStr([ // $mod.$main
  23293. '$mod.THelper.DoIt.call({',
  23294. ' a: $mod.Foo(1),',
  23295. ' get: function () {',
  23296. ' return this.a;',
  23297. ' },',
  23298. ' set: function (v) {',
  23299. ' this.a = v;',
  23300. ' }',
  23301. '}, 123);',
  23302. '$mod.THelper.DoIt.call({',
  23303. ' a: $mod.Foo(1),',
  23304. ' get: function () {',
  23305. ' return this.a;',
  23306. ' },',
  23307. ' set: function (v) {',
  23308. ' this.a = v;',
  23309. ' }',
  23310. '}, 123);',
  23311. 'var $with = $mod.Foo(1);',
  23312. '$mod.THelper.DoIt.call({',
  23313. ' get: function () {',
  23314. ' return $with;',
  23315. ' },',
  23316. ' set: function (v) {',
  23317. ' $with = v;',
  23318. ' }',
  23319. '}, 123);',
  23320. 'var $with1 = $mod.Foo(1);',
  23321. '$mod.THelper.DoIt.call({',
  23322. ' get: function () {',
  23323. ' return $with1;',
  23324. ' },',
  23325. ' set: function (v) {',
  23326. ' $with1 = v;',
  23327. ' }',
  23328. '}, 123);',
  23329. '']));
  23330. end;
  23331. procedure TTestModule.TestTypeHelper_PassPropertyField;
  23332. begin
  23333. StartProgram(false);
  23334. Add([
  23335. '{$modeswitch typehelpers}',
  23336. 'type',
  23337. ' TObject = class',
  23338. ' FField: word;',
  23339. ' procedure SetField(Value: word);',
  23340. ' property Field: word read FField write SetField;',
  23341. ' end;',
  23342. ' THelper = type helper for word',
  23343. ' procedure Fly;',
  23344. ' class procedure Run; static;',
  23345. ' end;',
  23346. 'procedure TObject.SetField(Value: word);',
  23347. 'begin',
  23348. ' Field.Fly;',
  23349. ' Field.Run;',
  23350. ' Self.Field.Fly;',
  23351. ' Self.Field.Run;',
  23352. ' with Self do begin',
  23353. ' Field.Fly;',
  23354. ' Field.Run;',
  23355. ' end;',
  23356. ' with Self.Field do begin',
  23357. ' Fly;',
  23358. ' Run;',
  23359. ' end;',
  23360. 'end;',
  23361. 'procedure THelper.Fly;',
  23362. 'begin',
  23363. 'end;',
  23364. 'class procedure THelper.Run;',
  23365. 'begin',
  23366. 'end;',
  23367. 'var',
  23368. ' o: TObject;',
  23369. 'begin',
  23370. ' o.Field.Fly;',
  23371. ' o.Field.Run;',
  23372. ' with o do begin',
  23373. ' Field.Fly;',
  23374. ' Field.Run;',
  23375. ' end;',
  23376. ' with o.Field do begin',
  23377. ' Fly;',
  23378. ' Run;',
  23379. ' end;',
  23380. '']);
  23381. ConvertProgram;
  23382. CheckSource('TestTypeHelper_PassPropertyField',
  23383. LinesToStr([ // statements
  23384. 'rtl.createClass(this, "TObject", null, function () {',
  23385. ' this.$init = function () {',
  23386. ' this.FField = 0;',
  23387. ' };',
  23388. ' this.$final = function () {',
  23389. ' };',
  23390. ' this.SetField = function (Value) {',
  23391. ' $mod.THelper.Fly.call({',
  23392. ' p: this,',
  23393. ' get: function () {',
  23394. ' return this.p.FField;',
  23395. ' },',
  23396. ' set: function (v) {',
  23397. ' this.p.FField = v;',
  23398. ' }',
  23399. ' });',
  23400. ' $mod.THelper.Run();',
  23401. ' $mod.THelper.Fly.call({',
  23402. ' p: this,',
  23403. ' get: function () {',
  23404. ' return this.p.FField;',
  23405. ' },',
  23406. ' set: function (v) {',
  23407. ' this.p.FField = v;',
  23408. ' }',
  23409. ' });',
  23410. ' $mod.THelper.Run();',
  23411. ' $mod.THelper.Fly.call({',
  23412. ' p: this,',
  23413. ' get: function () {',
  23414. ' return this.p.FField;',
  23415. ' },',
  23416. ' set: function (v) {',
  23417. ' this.p.FField = v;',
  23418. ' }',
  23419. ' });',
  23420. ' $mod.THelper.Run();',
  23421. ' var $with = this.FField;',
  23422. ' $mod.THelper.Fly.call({',
  23423. ' get: function () {',
  23424. ' return $with;',
  23425. ' },',
  23426. ' set: function (v) {',
  23427. ' $with = v;',
  23428. ' }',
  23429. ' });',
  23430. ' $mod.THelper.Run();',
  23431. ' };',
  23432. '});',
  23433. 'rtl.createHelper(this, "THelper", null, function () {',
  23434. ' this.Fly = function () {',
  23435. ' };',
  23436. ' this.Run = function () {',
  23437. ' };',
  23438. '});',
  23439. 'this.o = null;',
  23440. '']),
  23441. LinesToStr([ // $mod.$main
  23442. '$mod.THelper.Fly.call({',
  23443. ' p: $mod.o,',
  23444. ' get: function () {',
  23445. ' return this.p.FField;',
  23446. ' },',
  23447. ' set: function (v) {',
  23448. ' this.p.FField = v;',
  23449. ' }',
  23450. '});',
  23451. '$mod.THelper.Run();',
  23452. 'var $with = $mod.o;',
  23453. '$mod.THelper.Fly.call({',
  23454. ' p: $with,',
  23455. ' get: function () {',
  23456. ' return this.p.FField;',
  23457. ' },',
  23458. ' set: function (v) {',
  23459. ' this.p.FField = v;',
  23460. ' }',
  23461. '});',
  23462. '$mod.THelper.Run();',
  23463. 'var $with1 = $mod.o.FField;',
  23464. '$mod.THelper.Fly.call({',
  23465. ' get: function () {',
  23466. ' return $with1;',
  23467. ' },',
  23468. ' set: function (v) {',
  23469. ' $with1 = v;',
  23470. ' }',
  23471. '});',
  23472. '$mod.THelper.Run();',
  23473. '']));
  23474. end;
  23475. procedure TTestModule.TestTypeHelper_PassPropertyGetter;
  23476. begin
  23477. StartProgram(false);
  23478. Add([
  23479. '{$modeswitch typehelpers}',
  23480. 'type',
  23481. ' TObject = class',
  23482. ' FField: word;',
  23483. ' function GetField: word;',
  23484. ' property Field: word read GetField write FField;',
  23485. ' end;',
  23486. ' THelper = type helper for word',
  23487. ' procedure Fly;',
  23488. ' class procedure Run; static;',
  23489. ' end;',
  23490. 'function TObject.GetField: word;',
  23491. 'begin',
  23492. ' Field.Fly;',
  23493. ' Field.Run;',
  23494. ' Self.Field.Fly;',
  23495. ' Self.Field.Run;',
  23496. ' with Self do begin',
  23497. ' Field.Fly;',
  23498. ' Field.Run;',
  23499. ' end;',
  23500. ' with Self.Field do begin',
  23501. ' Fly;',
  23502. ' Run;',
  23503. ' end;',
  23504. 'end;',
  23505. 'procedure THelper.Fly;',
  23506. 'begin',
  23507. 'end;',
  23508. 'class procedure THelper.Run;',
  23509. 'begin',
  23510. 'end;',
  23511. 'var',
  23512. ' o: TObject;',
  23513. 'begin',
  23514. ' o.Field.Fly;',
  23515. ' o.Field.Run;',
  23516. ' with o do begin',
  23517. ' Field.Fly;',
  23518. ' Field.Run;',
  23519. ' end;',
  23520. ' with o.Field do begin',
  23521. ' Fly;',
  23522. ' Run;',
  23523. ' end;',
  23524. '']);
  23525. ConvertProgram;
  23526. CheckSource('TestTypeHelper_PassPropertyGetter',
  23527. LinesToStr([ // statements
  23528. 'rtl.createClass(this, "TObject", null, function () {',
  23529. ' this.$init = function () {',
  23530. ' this.FField = 0;',
  23531. ' };',
  23532. ' this.$final = function () {',
  23533. ' };',
  23534. ' this.GetField = function () {',
  23535. ' var Result = 0;',
  23536. ' $mod.THelper.Fly.call({',
  23537. ' p: this.GetField(),',
  23538. ' get: function () {',
  23539. ' return this.p;',
  23540. ' },',
  23541. ' set: function (v) {',
  23542. ' this.p = v;',
  23543. ' }',
  23544. ' });',
  23545. ' $mod.THelper.Run();',
  23546. ' $mod.THelper.Fly.call({',
  23547. ' p: this.GetField(),',
  23548. ' get: function () {',
  23549. ' return this.p;',
  23550. ' },',
  23551. ' set: function (v) {',
  23552. ' this.p = v;',
  23553. ' }',
  23554. ' });',
  23555. ' $mod.THelper.Run();',
  23556. ' $mod.THelper.Fly.call({',
  23557. ' p: this.GetField(),',
  23558. ' get: function () {',
  23559. ' return this.p;',
  23560. ' },',
  23561. ' set: function (v) {',
  23562. ' this.p = v;',
  23563. ' }',
  23564. ' });',
  23565. ' $mod.THelper.Run();',
  23566. ' var $with = this.GetField();',
  23567. ' $mod.THelper.Fly.call({',
  23568. ' get: function () {',
  23569. ' return $with;',
  23570. ' },',
  23571. ' set: function (v) {',
  23572. ' $with = v;',
  23573. ' }',
  23574. ' });',
  23575. ' $mod.THelper.Run();',
  23576. ' return Result;',
  23577. ' };',
  23578. '});',
  23579. 'rtl.createHelper(this, "THelper", null, function () {',
  23580. ' this.Fly = function () {',
  23581. ' };',
  23582. ' this.Run = function () {',
  23583. ' };',
  23584. '});',
  23585. 'this.o = null;',
  23586. '']),
  23587. LinesToStr([ // $mod.$main
  23588. '$mod.THelper.Fly.call({',
  23589. ' p: $mod.o.GetField(),',
  23590. ' get: function () {',
  23591. ' return this.p;',
  23592. ' },',
  23593. ' set: function (v) {',
  23594. ' this.p = v;',
  23595. ' }',
  23596. '});',
  23597. '$mod.THelper.Run();',
  23598. 'var $with = $mod.o;',
  23599. '$mod.THelper.Fly.call({',
  23600. ' p: $with.GetField(),',
  23601. ' get: function () {',
  23602. ' return this.p;',
  23603. ' },',
  23604. ' set: function (v) {',
  23605. ' this.p = v;',
  23606. ' }',
  23607. '});',
  23608. '$mod.THelper.Run();',
  23609. 'var $with1 = $mod.o.GetField();',
  23610. '$mod.THelper.Fly.call({',
  23611. ' get: function () {',
  23612. ' return $with1;',
  23613. ' },',
  23614. ' set: function (v) {',
  23615. ' $with1 = v;',
  23616. ' }',
  23617. '});',
  23618. '$mod.THelper.Run();',
  23619. '']));
  23620. end;
  23621. procedure TTestModule.TestTypeHelper_PassClassPropertyField;
  23622. begin
  23623. StartProgram(false);
  23624. Add([
  23625. '{$modeswitch typehelpers}',
  23626. 'type',
  23627. ' TObject = class',
  23628. ' class var FField: word;',
  23629. ' class procedure SetField(Value: word);',
  23630. ' class property Field: word read FField write SetField;',
  23631. ' end;',
  23632. ' THelper = type helper for word',
  23633. ' procedure Fly(n: byte);',
  23634. ' end;',
  23635. 'class procedure TObject.SetField(Value: word);',
  23636. 'begin',
  23637. ' Field.Fly(1);',
  23638. ' Self.Field.Fly(2);',
  23639. ' with Self do Field.Fly(3);',
  23640. ' with Self.Field do Fly(4);',
  23641. ' TObject.Field.Fly(5);',
  23642. ' with TObject do Field.Fly(6);',
  23643. ' with TObject.Field do Fly(7);',
  23644. 'end;',
  23645. 'procedure THelper.Fly(n: byte);',
  23646. 'begin',
  23647. 'end;',
  23648. 'var',
  23649. ' o: TObject;',
  23650. 'begin',
  23651. ' o.Field.Fly(11);',
  23652. ' with o do Field.Fly(12);',
  23653. ' with o.Field do Fly(13);',
  23654. ' TObject.Field.Fly(14);',
  23655. ' with TObject do Field.Fly(15);',
  23656. ' with TObject.Field do Fly(16);',
  23657. '']);
  23658. ConvertProgram;
  23659. CheckSource('TestTypeHelper_PassClassPropertyField',
  23660. LinesToStr([ // statements
  23661. 'rtl.createClass(this, "TObject", null, function () {',
  23662. ' this.FField = 0;',
  23663. ' this.$init = function () {',
  23664. ' };',
  23665. ' this.$final = function () {',
  23666. ' };',
  23667. ' this.SetField = function (Value) {',
  23668. ' $mod.THelper.Fly.call({',
  23669. ' p: this,',
  23670. ' get: function () {',
  23671. ' return this.p.FField;',
  23672. ' },',
  23673. ' set: function (v) {',
  23674. ' $mod.TObject.FField = v;',
  23675. ' }',
  23676. ' }, 1);',
  23677. ' $mod.THelper.Fly.call({',
  23678. ' p: this,',
  23679. ' get: function () {',
  23680. ' return this.p.FField;',
  23681. ' },',
  23682. ' set: function (v) {',
  23683. ' $mod.TObject.FField = v;',
  23684. ' }',
  23685. ' }, 2);',
  23686. ' $mod.THelper.Fly.call({',
  23687. ' p: this,',
  23688. ' get: function () {',
  23689. ' return this.p.FField;',
  23690. ' },',
  23691. ' set: function (v) {',
  23692. ' $mod.TObject.FField = v;',
  23693. ' }',
  23694. ' }, 3);',
  23695. ' var $with = this.FField;',
  23696. ' $mod.THelper.Fly.call({',
  23697. ' get: function () {',
  23698. ' return $with;',
  23699. ' },',
  23700. ' set: function (v) {',
  23701. ' $with = v;',
  23702. ' }',
  23703. ' }, 4);',
  23704. ' $mod.THelper.Fly.call({',
  23705. ' p: $mod.TObject,',
  23706. ' get: function () {',
  23707. ' return this.p.FField;',
  23708. ' },',
  23709. ' set: function (v) {',
  23710. ' $mod.TObject.FField = v;',
  23711. ' }',
  23712. ' }, 5);',
  23713. ' var $with1 = $mod.TObject;',
  23714. ' $mod.THelper.Fly.call({',
  23715. ' p: $with1,',
  23716. ' get: function () {',
  23717. ' return this.p.FField;',
  23718. ' },',
  23719. ' set: function (v) {',
  23720. ' $mod.TObject.FField = v;',
  23721. ' }',
  23722. ' }, 6);',
  23723. ' var $with2 = $mod.TObject.FField;',
  23724. ' $mod.THelper.Fly.call({',
  23725. ' get: function () {',
  23726. ' return $with2;',
  23727. ' },',
  23728. ' set: function (v) {',
  23729. ' $with2 = v;',
  23730. ' }',
  23731. ' }, 7);',
  23732. ' };',
  23733. '});',
  23734. 'rtl.createHelper(this, "THelper", null, function () {',
  23735. ' this.Fly = function (n) {',
  23736. ' };',
  23737. '});',
  23738. 'this.o = null;',
  23739. '']),
  23740. LinesToStr([ // $mod.$main
  23741. '$mod.THelper.Fly.call({',
  23742. ' p: $mod.o,',
  23743. ' get: function () {',
  23744. ' return this.p.FField;',
  23745. ' },',
  23746. ' set: function (v) {',
  23747. ' $mod.TObject.FField = v;',
  23748. ' }',
  23749. '}, 11);',
  23750. 'var $with = $mod.o;',
  23751. '$mod.THelper.Fly.call({',
  23752. ' p: $with,',
  23753. ' get: function () {',
  23754. ' return this.p.FField;',
  23755. ' },',
  23756. ' set: function (v) {',
  23757. ' $mod.TObject.FField = v;',
  23758. ' }',
  23759. '}, 12);',
  23760. 'var $with1 = $mod.o.FField;',
  23761. '$mod.THelper.Fly.call({',
  23762. ' get: function () {',
  23763. ' return $with1;',
  23764. ' },',
  23765. ' set: function (v) {',
  23766. ' $with1 = v;',
  23767. ' }',
  23768. '}, 13);',
  23769. '$mod.THelper.Fly.call({',
  23770. ' p: $mod.TObject,',
  23771. ' get: function () {',
  23772. ' return this.p.FField;',
  23773. ' },',
  23774. ' set: function (v) {',
  23775. ' $mod.TObject.FField = v;',
  23776. ' }',
  23777. '}, 14);',
  23778. 'var $with2 = $mod.TObject;',
  23779. '$mod.THelper.Fly.call({',
  23780. ' p: $with2,',
  23781. ' get: function () {',
  23782. ' return this.p.FField;',
  23783. ' },',
  23784. ' set: function (v) {',
  23785. ' $mod.TObject.FField = v;',
  23786. ' }',
  23787. '}, 15);',
  23788. 'var $with3 = $mod.TObject.FField;',
  23789. '$mod.THelper.Fly.call({',
  23790. ' get: function () {',
  23791. ' return $with3;',
  23792. ' },',
  23793. ' set: function (v) {',
  23794. ' $with3 = v;',
  23795. ' }',
  23796. '}, 16);',
  23797. '']));
  23798. end;
  23799. procedure TTestModule.TestTypeHelper_PassClassPropertyGetterStatic;
  23800. begin
  23801. StartProgram(false);
  23802. Add([
  23803. '{$modeswitch typehelpers}',
  23804. 'type',
  23805. ' TObject = class',
  23806. ' class var FField: word;',
  23807. ' class function GetField: word; static;',
  23808. ' class property Field: word read GetField write FField;',
  23809. ' end;',
  23810. ' THelper = type helper for word',
  23811. ' procedure Fly(n: byte);',
  23812. ' end;',
  23813. 'class function TObject.GetField: word;',
  23814. 'begin',
  23815. ' Field.Fly(1);',
  23816. ' TObject.Field.Fly(5);',
  23817. ' with TObject do Field.Fly(6);',
  23818. ' with TObject.Field do Fly(7);',
  23819. 'end;',
  23820. 'procedure THelper.Fly(n: byte);',
  23821. 'begin',
  23822. 'end;',
  23823. 'var',
  23824. ' o: TObject;',
  23825. 'begin',
  23826. ' o.Field.Fly(11);',
  23827. ' with o do Field.Fly(12);',
  23828. ' with o.Field do Fly(13);',
  23829. '']);
  23830. ConvertProgram;
  23831. CheckSource('TestTypeHelper_PassClassPropertyGetterStatic',
  23832. LinesToStr([ // statements
  23833. 'rtl.createClass(this, "TObject", null, function () {',
  23834. ' this.FField = 0;',
  23835. ' this.$init = function () {',
  23836. ' };',
  23837. ' this.$final = function () {',
  23838. ' };',
  23839. ' this.GetField = function () {',
  23840. ' var Result = 0;',
  23841. ' $mod.THelper.Fly.call({',
  23842. ' p: $mod.TObject.GetField(),',
  23843. ' get: function () {',
  23844. ' return this.p;',
  23845. ' },',
  23846. ' set: function (v) {',
  23847. ' this.p = v;',
  23848. ' }',
  23849. ' }, 1);',
  23850. ' $mod.THelper.Fly.call({',
  23851. ' p: $mod.TObject.GetField(),',
  23852. ' get: function () {',
  23853. ' return this.p;',
  23854. ' },',
  23855. ' set: function (v) {',
  23856. ' this.p = v;',
  23857. ' }',
  23858. ' }, 5);',
  23859. ' var $with = $mod.TObject;',
  23860. ' $mod.THelper.Fly.call({',
  23861. ' p: $with.GetField(),',
  23862. ' get: function () {',
  23863. ' return this.p;',
  23864. ' },',
  23865. ' set: function (v) {',
  23866. ' this.p = v;',
  23867. ' }',
  23868. ' }, 6);',
  23869. ' var $with1 = $mod.TObject.GetField();',
  23870. ' $mod.THelper.Fly.call({',
  23871. ' get: function () {',
  23872. ' return $with1;',
  23873. ' },',
  23874. ' set: function (v) {',
  23875. ' $with1 = v;',
  23876. ' }',
  23877. ' }, 7);',
  23878. ' return Result;',
  23879. ' };',
  23880. '});',
  23881. 'rtl.createHelper(this, "THelper", null, function () {',
  23882. ' this.Fly = function (n) {',
  23883. ' };',
  23884. '});',
  23885. 'this.o = null;',
  23886. '']),
  23887. LinesToStr([ // $mod.$main
  23888. '$mod.THelper.Fly.call({',
  23889. ' p: $mod.TObject.GetField(),',
  23890. ' get: function () {',
  23891. ' return this.p;',
  23892. ' },',
  23893. ' set: function (v) {',
  23894. ' this.p = v;',
  23895. ' }',
  23896. '}, 11);',
  23897. 'var $with = $mod.o;',
  23898. '$mod.THelper.Fly.call({',
  23899. ' p: $with.GetField(),',
  23900. ' get: function () {',
  23901. ' return this.p;',
  23902. ' },',
  23903. ' set: function (v) {',
  23904. ' this.p = v;',
  23905. ' }',
  23906. '}, 12);',
  23907. 'var $with1 = $mod.TObject.GetField();',
  23908. '$mod.THelper.Fly.call({',
  23909. ' get: function () {',
  23910. ' return $with1;',
  23911. ' },',
  23912. ' set: function (v) {',
  23913. ' $with1 = v;',
  23914. ' }',
  23915. '}, 13);',
  23916. '']));
  23917. end;
  23918. procedure TTestModule.TestTypeHelper_PassClassPropertyGetterNonStatic;
  23919. begin
  23920. StartProgram(false);
  23921. Add([
  23922. '{$modeswitch typehelpers}',
  23923. 'type',
  23924. ' TObject = class',
  23925. ' class var FField: word;',
  23926. ' class function GetField: word;',
  23927. ' class property Field: word read GetField write FField;',
  23928. ' end;',
  23929. ' TClass = class of TObject;',
  23930. ' THelper = type helper for word',
  23931. ' procedure Fly(n: byte);',
  23932. ' end;',
  23933. 'class function TObject.GetField: word;',
  23934. 'begin',
  23935. ' Field.Fly(1);',
  23936. ' Self.Field.Fly(5);',
  23937. ' with Self do Field.Fly(6);',
  23938. ' with Self.Field do Fly(7);',
  23939. 'end;',
  23940. 'procedure THelper.Fly(n: byte);',
  23941. 'begin',
  23942. 'end;',
  23943. 'var',
  23944. ' o: TObject;',
  23945. ' c: TClass;',
  23946. 'begin',
  23947. ' o.Field.Fly(11);',
  23948. ' with o do Field.Fly(12);',
  23949. ' with o.Field do Fly(13);',
  23950. ' c.Field.Fly(14);',
  23951. ' with c do Field.Fly(15);',
  23952. ' with c.Field do Fly(16);',
  23953. '']);
  23954. ConvertProgram;
  23955. CheckSource('TestTypeHelper_PassClassPropertyGetterNonStatic',
  23956. LinesToStr([ // statements
  23957. 'rtl.createClass(this, "TObject", null, function () {',
  23958. ' this.FField = 0;',
  23959. ' this.$init = function () {',
  23960. ' };',
  23961. ' this.$final = function () {',
  23962. ' };',
  23963. ' this.GetField = function () {',
  23964. ' var Result = 0;',
  23965. ' $mod.THelper.Fly.call({',
  23966. ' p: this.GetField(),',
  23967. ' get: function () {',
  23968. ' return this.p;',
  23969. ' },',
  23970. ' set: function (v) {',
  23971. ' this.p = v;',
  23972. ' }',
  23973. ' }, 1);',
  23974. ' $mod.THelper.Fly.call({',
  23975. ' p: this.GetField(),',
  23976. ' get: function () {',
  23977. ' return this.p;',
  23978. ' },',
  23979. ' set: function (v) {',
  23980. ' this.p = v;',
  23981. ' }',
  23982. ' }, 5);',
  23983. ' $mod.THelper.Fly.call({',
  23984. ' p: this.GetField(),',
  23985. ' get: function () {',
  23986. ' return this.p;',
  23987. ' },',
  23988. ' set: function (v) {',
  23989. ' this.p = v;',
  23990. ' }',
  23991. ' }, 6);',
  23992. ' var $with = this.GetField();',
  23993. ' $mod.THelper.Fly.call({',
  23994. ' get: function () {',
  23995. ' return $with;',
  23996. ' },',
  23997. ' set: function (v) {',
  23998. ' $with = v;',
  23999. ' }',
  24000. ' }, 7);',
  24001. ' return Result;',
  24002. ' };',
  24003. '});',
  24004. 'rtl.createHelper(this, "THelper", null, function () {',
  24005. ' this.Fly = function (n) {',
  24006. ' };',
  24007. '});',
  24008. 'this.o = null;',
  24009. 'this.c = null;',
  24010. '']),
  24011. LinesToStr([ // $mod.$main
  24012. '$mod.THelper.Fly.call({',
  24013. ' p: $mod.o.$class.GetField(),',
  24014. ' get: function () {',
  24015. ' return this.p;',
  24016. ' },',
  24017. ' set: function (v) {',
  24018. ' this.p = v;',
  24019. ' }',
  24020. '}, 11);',
  24021. 'var $with = $mod.o;',
  24022. '$mod.THelper.Fly.call({',
  24023. ' p: $with.$class.GetField(),',
  24024. ' get: function () {',
  24025. ' return this.p;',
  24026. ' },',
  24027. ' set: function (v) {',
  24028. ' this.p = v;',
  24029. ' }',
  24030. '}, 12);',
  24031. 'var $with1 = $mod.o.$class.GetField();',
  24032. '$mod.THelper.Fly.call({',
  24033. ' get: function () {',
  24034. ' return $with1;',
  24035. ' },',
  24036. ' set: function (v) {',
  24037. ' $with1 = v;',
  24038. ' }',
  24039. '}, 13);',
  24040. '$mod.THelper.Fly.call({',
  24041. ' p: $mod.c.GetField(),',
  24042. ' get: function () {',
  24043. ' return this.p;',
  24044. ' },',
  24045. ' set: function (v) {',
  24046. ' this.p = v;',
  24047. ' }',
  24048. '}, 14);',
  24049. 'var $with2 = $mod.c;',
  24050. '$mod.THelper.Fly.call({',
  24051. ' p: $with2.GetField(),',
  24052. ' get: function () {',
  24053. ' return this.p;',
  24054. ' },',
  24055. ' set: function (v) {',
  24056. ' this.p = v;',
  24057. ' }',
  24058. '}, 15);',
  24059. 'var $with3 = $mod.c.GetField();',
  24060. '$mod.THelper.Fly.call({',
  24061. ' get: function () {',
  24062. ' return $with3;',
  24063. ' },',
  24064. ' set: function (v) {',
  24065. ' $with3 = v;',
  24066. ' }',
  24067. '}, 16);',
  24068. '']));
  24069. end;
  24070. procedure TTestModule.TestTypeHelper_Property;
  24071. begin
  24072. StartProgram(false);
  24073. Add([
  24074. '{$modeswitch typehelpers}',
  24075. 'type',
  24076. ' THelper = type helper for word',
  24077. ' function GetSize: longint;',
  24078. ' procedure SetSize(Value: longint);',
  24079. ' property Size: longint read GetSize write SetSize;',
  24080. ' end;',
  24081. 'function THelper.GetSize: longint;',
  24082. 'begin',
  24083. ' Result:=Size+1;',
  24084. ' Size:=2;',
  24085. ' Result:=Self.Size+3;',
  24086. ' Self.Size:=4;',
  24087. ' with Self do begin',
  24088. ' Result:=Size+5;',
  24089. ' Size:=6;',
  24090. ' end;',
  24091. 'end;',
  24092. 'procedure THelper.SetSize(Value: longint);',
  24093. 'begin',
  24094. 'end;',
  24095. 'var w: word;',
  24096. 'begin',
  24097. ' w:=w.Size+7;',
  24098. ' w.Size:=w+8;',
  24099. ' with w do begin',
  24100. ' w:=Size+9;',
  24101. ' Size:=w+10;',
  24102. ' end;',
  24103. '']);
  24104. ConvertProgram;
  24105. CheckSource('TestTypeHelper_Property',
  24106. LinesToStr([ // statements
  24107. 'rtl.createHelper(this, "THelper", null, function () {',
  24108. ' this.GetSize = function () {',
  24109. ' var Result = 0;',
  24110. ' Result = $mod.THelper.GetSize.call(this) + 1;',
  24111. ' $mod.THelper.SetSize.call(this, 2);',
  24112. ' Result = $mod.THelper.GetSize.call(this) + 3;',
  24113. ' $mod.THelper.SetSize.call(this, 4);',
  24114. ' var $with = this.get();',
  24115. ' Result = $mod.THelper.GetSize.call(this) + 5;',
  24116. ' $mod.THelper.SetSize.call(this, 6);',
  24117. ' return Result;',
  24118. ' };',
  24119. ' this.SetSize = function (Value) {',
  24120. ' };',
  24121. '});',
  24122. 'this.w = 0;',
  24123. '']),
  24124. LinesToStr([ // $mod.$main
  24125. '$mod.w = $mod.THelper.GetSize.call({',
  24126. ' p: $mod,',
  24127. ' get: function () {',
  24128. ' return this.p.w;',
  24129. ' },',
  24130. ' set: function (v) {',
  24131. ' this.p.w = v;',
  24132. ' }',
  24133. '}) + 7;',
  24134. '$mod.THelper.SetSize.call({',
  24135. ' p: $mod,',
  24136. ' get: function () {',
  24137. ' return this.p.w;',
  24138. ' },',
  24139. ' set: function (v) {',
  24140. ' this.p.w = v;',
  24141. ' }',
  24142. '}, $mod.w + 8);',
  24143. 'var $with = $mod.w;',
  24144. '$mod.w = $mod.THelper.GetSize.call({',
  24145. ' get: function () {',
  24146. ' return $with;',
  24147. ' },',
  24148. ' set: function (v) {',
  24149. ' $with = v;',
  24150. ' }',
  24151. '}) + 9;',
  24152. '$mod.THelper.SetSize.call({',
  24153. ' get: function () {',
  24154. ' return $with;',
  24155. ' },',
  24156. ' set: function (v) {',
  24157. ' $with = v;',
  24158. ' }',
  24159. '}, $mod.w + 10);',
  24160. '']));
  24161. end;
  24162. procedure TTestModule.TestTypeHelper_Property_Array;
  24163. begin
  24164. StartProgram(false);
  24165. Add([
  24166. '{$modeswitch typehelpers}',
  24167. 'type',
  24168. ' THelper = type helper for word',
  24169. ' function GetItems(Index: byte): boolean;',
  24170. ' procedure SetItems(Index: byte; Value: boolean);',
  24171. ' property Items[Index: byte]: boolean read GetItems write SetItems;',
  24172. ' end;',
  24173. 'function THelper.GetItems(Index: byte): boolean;',
  24174. 'begin',
  24175. ' Result:=Items[1];',
  24176. ' Items[2]:=false;',
  24177. ' Result:=Self.Items[3];',
  24178. ' Self.Items[4]:=true;',
  24179. ' with Self do begin',
  24180. ' Result:=Items[5];',
  24181. ' Items[6]:=false;',
  24182. ' end;',
  24183. 'end;',
  24184. 'procedure THelper.SetItems(Index: byte; Value: boolean);',
  24185. 'begin',
  24186. 'end;',
  24187. 'var',
  24188. ' w: word;',
  24189. ' b: boolean;',
  24190. 'begin',
  24191. ' b:=w.Items[1];',
  24192. ' w.Items[2]:=b;',
  24193. ' with w do begin',
  24194. ' b:=Items[3];',
  24195. ' Items[4]:=b;',
  24196. ' end;',
  24197. '']);
  24198. ConvertProgram;
  24199. CheckSource('TestTypeHelper_Property_Array',
  24200. LinesToStr([ // statements
  24201. 'rtl.createHelper(this, "THelper", null, function () {',
  24202. ' this.GetItems = function (Index) {',
  24203. ' var Result = false;',
  24204. ' Result = $mod.THelper.GetItems.call(this, 1);',
  24205. ' $mod.THelper.SetItems.call(this, 2, false);',
  24206. ' Result = $mod.THelper.GetItems.call(this, 3);',
  24207. ' $mod.THelper.SetItems.call(this, 4, true);',
  24208. ' var $with = this.get();',
  24209. ' Result = $mod.THelper.GetItems.call(this, 5);',
  24210. ' $mod.THelper.SetItems.call(this, 6, false);',
  24211. ' return Result;',
  24212. ' };',
  24213. ' this.SetItems = function (Index, Value) {',
  24214. ' };',
  24215. '});',
  24216. 'this.w = 0;',
  24217. 'this.b = false;',
  24218. '']),
  24219. LinesToStr([ // $mod.$main
  24220. '$mod.b = $mod.THelper.GetItems.call({',
  24221. ' p: $mod,',
  24222. ' get: function () {',
  24223. ' return this.p.w;',
  24224. ' },',
  24225. ' set: function (v) {',
  24226. ' this.p.w = v;',
  24227. ' }',
  24228. '}, 1);',
  24229. '$mod.THelper.SetItems.call({',
  24230. ' p: $mod,',
  24231. ' get: function () {',
  24232. ' return this.p.w;',
  24233. ' },',
  24234. ' set: function (v) {',
  24235. ' this.p.w = v;',
  24236. ' }',
  24237. '}, 2, $mod.b);',
  24238. 'var $with = $mod.w;',
  24239. '$mod.b = $mod.THelper.GetItems.call({',
  24240. ' get: function () {',
  24241. ' return $with;',
  24242. ' },',
  24243. ' set: function (v) {',
  24244. ' $with = v;',
  24245. ' }',
  24246. '}, 3);',
  24247. '$mod.THelper.SetItems.call({',
  24248. ' get: function () {',
  24249. ' return $with;',
  24250. ' },',
  24251. ' set: function (v) {',
  24252. ' $with = v;',
  24253. ' }',
  24254. '}, 4, $mod.b);',
  24255. '']));
  24256. end;
  24257. procedure TTestModule.TestTypeHelper_ClassProperty;
  24258. begin
  24259. StartProgram(false);
  24260. Add([
  24261. '{$modeswitch typehelpers}',
  24262. 'type',
  24263. ' THelper = type helper for word',
  24264. ' class function GetSize: longint; static;',
  24265. ' class procedure SetSize(Value: longint); static;',
  24266. ' class property Size: longint read GetSize write SetSize;',
  24267. ' end;',
  24268. 'class function THelper.GetSize: longint;',
  24269. 'begin',
  24270. ' Result:=Size+1;',
  24271. ' Size:=2;',
  24272. 'end;',
  24273. 'class procedure THelper.SetSize(Value: longint);',
  24274. 'begin',
  24275. 'end;',
  24276. 'begin',
  24277. '']);
  24278. ConvertProgram;
  24279. CheckSource('TestTypeHelper_ClassProperty',
  24280. LinesToStr([ // statements
  24281. 'rtl.createHelper(this, "THelper", null, function () {',
  24282. ' this.GetSize = function () {',
  24283. ' var Result = 0;',
  24284. ' Result = $mod.THelper.GetSize() + 1;',
  24285. ' $mod.THelper.SetSize(2);',
  24286. ' return Result;',
  24287. ' };',
  24288. ' this.SetSize = function (Value) {',
  24289. ' };',
  24290. '});',
  24291. '']),
  24292. LinesToStr([ // $mod.$main
  24293. '']));
  24294. end;
  24295. procedure TTestModule.TestTypeHelper_ClassProperty_Array;
  24296. begin
  24297. StartProgram(false);
  24298. Add([
  24299. '{$modeswitch typehelpers}',
  24300. 'type',
  24301. ' THelper = type helper for word',
  24302. ' class function GetItems(Index: byte): boolean; static;',
  24303. ' class procedure SetItems(Index: byte; Value: boolean); static;',
  24304. ' class property Items[Index: byte]: boolean read GetItems write SetItems;',
  24305. ' end;',
  24306. 'class function THelper.GetItems(Index: byte): boolean;',
  24307. 'begin',
  24308. ' Result:=Items[1];',
  24309. ' Items[2]:=false;',
  24310. 'end;',
  24311. 'class procedure THelper.SetItems(Index: byte; Value: boolean);',
  24312. 'begin',
  24313. 'end;',
  24314. 'var',
  24315. ' w: word;',
  24316. ' b: boolean;',
  24317. 'begin',
  24318. ' b:=w.Items[1];',
  24319. ' w.Items[2]:=b;',
  24320. ' with w do begin',
  24321. ' b:=Items[3];',
  24322. ' Items[4]:=b;',
  24323. ' end;',
  24324. '']);
  24325. ConvertProgram;
  24326. CheckSource('TestTypeHelper_ClassProperty_Array',
  24327. LinesToStr([ // statements
  24328. 'rtl.createHelper(this, "THelper", null, function () {',
  24329. ' this.GetItems = function (Index) {',
  24330. ' var Result = false;',
  24331. ' Result = $mod.THelper.GetItems(1);',
  24332. ' $mod.THelper.SetItems(2, false);',
  24333. ' return Result;',
  24334. ' };',
  24335. ' this.SetItems = function (Index, Value) {',
  24336. ' };',
  24337. '});',
  24338. 'this.w = 0;',
  24339. 'this.b = false;',
  24340. '']),
  24341. LinesToStr([ // $mod.$main
  24342. '$mod.b = $mod.THelper.GetItems(1);',
  24343. '$mod.THelper.SetItems(2, $mod.b);',
  24344. 'var $with = $mod.w;',
  24345. '$mod.b = $mod.THelper.GetItems(3);',
  24346. '$mod.THelper.SetItems(4, $mod.b);',
  24347. '']));
  24348. end;
  24349. procedure TTestModule.TestTypeHelper_ClassMethod;
  24350. begin
  24351. StartProgram(false);
  24352. Add([
  24353. '{$modeswitch typehelpers}',
  24354. 'type',
  24355. ' THelper = type helper for word',
  24356. ' class procedure DoStatic; static;',
  24357. ' end;',
  24358. 'class procedure THelper.DoStatic;',
  24359. 'begin',
  24360. ' DoStatic;',
  24361. ' DoStatic();',
  24362. 'end;',
  24363. 'var w: word;',
  24364. 'begin',
  24365. ' w.DoStatic;',
  24366. ' w.DoStatic();',
  24367. '']);
  24368. ConvertProgram;
  24369. CheckSource('TestTypeHelper_ClassMethod',
  24370. LinesToStr([ // statements
  24371. 'rtl.createHelper(this, "THelper", null, function () {',
  24372. ' this.DoStatic = function () {',
  24373. ' $mod.THelper.DoStatic();',
  24374. ' $mod.THelper.DoStatic();',
  24375. ' };',
  24376. '});',
  24377. 'this.w = 0;',
  24378. '']),
  24379. LinesToStr([ // $mod.$main
  24380. '$mod.THelper.DoStatic();',
  24381. '$mod.THelper.DoStatic();',
  24382. '']));
  24383. end;
  24384. procedure TTestModule.TestTypeHelper_ExtClassMethodFail;
  24385. begin
  24386. StartProgram(false);
  24387. Add([
  24388. '{$modeswitch typehelpers}',
  24389. 'type',
  24390. ' THelper = type helper for word',
  24391. ' procedure Run; external name ''Run'';',
  24392. ' end;',
  24393. 'var w: word;',
  24394. 'begin',
  24395. ' w.Run;',
  24396. '']);
  24397. SetExpectedPasResolverError('Not supported: external method in type helper',nNotSupportedX);
  24398. ConvertProgram;
  24399. end;
  24400. procedure TTestModule.TestTypeHelper_Constructor;
  24401. begin
  24402. StartProgram(false);
  24403. Add([
  24404. '{$modeswitch typehelpers}',
  24405. 'type',
  24406. ' THelper = type helper for word',
  24407. ' constructor Init(e: longint);',
  24408. ' end;',
  24409. 'constructor THelper.Init(e: longint);',
  24410. 'begin',
  24411. ' Self:=e;',
  24412. ' Init(e+1);',
  24413. 'end;',
  24414. 'var w: word;',
  24415. 'begin',
  24416. ' w:=word.Init(2);',
  24417. ' w:=w.Init(3);',
  24418. ' with word do w:=Init(4);',
  24419. ' with w do w:=Init(5);',
  24420. '']);
  24421. ConvertProgram;
  24422. CheckSource('TestTypeHelper_Constructor',
  24423. LinesToStr([ // statements
  24424. 'rtl.createHelper(this, "THelper", null, function () {',
  24425. ' this.Init = function (e) {',
  24426. ' this.set(e);',
  24427. ' $mod.THelper.Init.call(this, e + 1);',
  24428. ' return this.get();',
  24429. ' };',
  24430. ' this.$new = function (fn, args) {',
  24431. ' return this[fn].apply({',
  24432. ' p: 0,',
  24433. ' get: function () {',
  24434. ' return this.p;',
  24435. ' },',
  24436. ' set: function (v) {',
  24437. ' this.p = v;',
  24438. ' }',
  24439. ' }, args);',
  24440. ' };',
  24441. '});',
  24442. 'this.w = 0;',
  24443. '']),
  24444. LinesToStr([ // $mod.$main
  24445. '$mod.w = $mod.THelper.$new("Init", [2]);',
  24446. '$mod.w = $mod.THelper.Init.call({',
  24447. ' p: $mod,',
  24448. ' get: function () {',
  24449. ' return this.p.w;',
  24450. ' },',
  24451. ' set: function (v) {',
  24452. ' this.p.w = v;',
  24453. ' }',
  24454. '}, 3);',
  24455. '$mod.w = $mod.THelper.$new("Init", [4]);',
  24456. 'var $with = $mod.w;',
  24457. '$mod.w = $mod.THelper.Init.call({',
  24458. ' get: function () {',
  24459. ' return $with;',
  24460. ' },',
  24461. ' set: function (v) {',
  24462. ' $with = v;',
  24463. ' }',
  24464. '}, 5);',
  24465. '']));
  24466. end;
  24467. procedure TTestModule.TestTypeHelper_Word;
  24468. begin
  24469. StartProgram(false);
  24470. Add([
  24471. '{$modeswitch typehelpers}',
  24472. 'type',
  24473. ' THelper = type helper for word',
  24474. ' procedure DoIt(e: byte = 123);',
  24475. ' end;',
  24476. 'procedure THelper.DoIt(e: byte);',
  24477. 'begin',
  24478. ' Self:=e;',
  24479. ' Self:=Self+1;',
  24480. ' with Self do Doit;',
  24481. 'end;',
  24482. 'begin',
  24483. ' word(3).DoIt;',
  24484. '']);
  24485. ConvertProgram;
  24486. CheckSource('TestTypeHelper_Word',
  24487. LinesToStr([ // statements
  24488. 'rtl.createHelper(this, "THelper", null, function () {',
  24489. ' this.DoIt = function (e) {',
  24490. ' this.set(e);',
  24491. ' this.set(this.get() + 1);',
  24492. ' var $with = this.get();',
  24493. ' $mod.THelper.DoIt.call(this, 123);',
  24494. ' };',
  24495. '});',
  24496. '']),
  24497. LinesToStr([ // $mod.$main
  24498. '$mod.THelper.DoIt.call({',
  24499. ' get: function () {',
  24500. ' return 3;',
  24501. ' },',
  24502. ' set: function (v) {',
  24503. ' rtl.raiseE("EPropReadOnly");',
  24504. ' }',
  24505. '}, 123);',
  24506. '']));
  24507. end;
  24508. procedure TTestModule.TestTypeHelper_Boolean;
  24509. begin
  24510. StartProgram(false);
  24511. Add([
  24512. '{$modeswitch typehelpers}',
  24513. 'type',
  24514. ' Integer = longint;',
  24515. ' THelper = type helper for boolean',
  24516. ' procedure Run(e: wordbool = true);',
  24517. ' end;',
  24518. 'procedure THelper.Run(e: wordbool);',
  24519. 'begin',
  24520. ' Self:=e;',
  24521. ' Self:=not Self;',
  24522. ' with Self do Run;',
  24523. ' if Integer(Self)=0 then ;',
  24524. 'end;',
  24525. 'begin',
  24526. ' boolean(3).Run;',
  24527. '']);
  24528. ConvertProgram;
  24529. CheckSource('TestTypeHelper_Boolean',
  24530. LinesToStr([ // statements
  24531. 'rtl.createHelper(this, "THelper", null, function () {',
  24532. ' this.Run = function (e) {',
  24533. ' this.set(e);',
  24534. ' this.set(!this.get());',
  24535. ' var $with = this.get();',
  24536. ' $mod.THelper.Run.call(this, true);',
  24537. ' if ((this.get() ? 1 : 0) === 0) ;',
  24538. ' };',
  24539. '});',
  24540. '']),
  24541. LinesToStr([ // $mod.$main
  24542. '$mod.THelper.Run.call({',
  24543. ' a: 3 != 0,',
  24544. ' get: function () {',
  24545. ' return this.a;',
  24546. ' },',
  24547. ' set: function (v) {',
  24548. ' rtl.raiseE("EPropReadOnly");',
  24549. ' }',
  24550. '}, true);',
  24551. '']));
  24552. end;
  24553. procedure TTestModule.TestTypeHelper_WordBool;
  24554. begin
  24555. StartProgram(false);
  24556. Add([
  24557. '{$modeswitch typehelpers}',
  24558. 'type',
  24559. ' Integer = longint;',
  24560. ' THelper = type helper for WordBool',
  24561. ' procedure Run(e: wordbool = true);',
  24562. ' end;',
  24563. 'procedure THelper.Run(e: wordbool);',
  24564. 'var i: integer;',
  24565. 'begin',
  24566. ' i:=Integer(Self);',
  24567. 'end;',
  24568. 'var w: wordbool;',
  24569. 'begin',
  24570. ' w.Run;',
  24571. ' wordbool(3).Run;',
  24572. '']);
  24573. ConvertProgram;
  24574. CheckSource('TestTypeHelper_WordBool',
  24575. LinesToStr([ // statements
  24576. 'rtl.createHelper(this, "THelper", null, function () {',
  24577. ' this.Run = function (e) {',
  24578. ' var i = 0;',
  24579. ' i = (this.get() ? 1 : 0);',
  24580. ' };',
  24581. '});',
  24582. 'this.w = false;',
  24583. '']),
  24584. LinesToStr([ // $mod.$main
  24585. '$mod.THelper.Run.call({',
  24586. ' p: $mod,',
  24587. ' get: function () {',
  24588. ' return this.p.w;',
  24589. ' },',
  24590. ' set: function (v) {',
  24591. ' this.p.w = v;',
  24592. ' }',
  24593. '}, true);',
  24594. '$mod.THelper.Run.call({',
  24595. ' a: 3 != 0,',
  24596. ' get: function () {',
  24597. ' return this.a;',
  24598. ' },',
  24599. ' set: function (v) {',
  24600. ' rtl.raiseE("EPropReadOnly");',
  24601. ' }',
  24602. '}, true);',
  24603. '']));
  24604. end;
  24605. procedure TTestModule.TestTypeHelper_Double;
  24606. begin
  24607. StartProgram(false);
  24608. Add([
  24609. '{$modeswitch typehelpers}',
  24610. 'type',
  24611. ' Float = type double;',
  24612. ' THelper = type helper for Float',
  24613. ' const NPI = 3.141592;',
  24614. ' function ToStr: String;',
  24615. ' end;',
  24616. 'function THelper.ToStr: String;',
  24617. 'begin',
  24618. 'end;',
  24619. 'procedure DoIt(s: string);',
  24620. 'begin',
  24621. 'end;',
  24622. 'var f: Float;',
  24623. 'begin',
  24624. ' DoIt(f.toStr);',
  24625. ' DoIt(f.toStr());',
  24626. ' (f*f).toStr;',
  24627. ' DoIt((f*f).toStr);',
  24628. '']);
  24629. ConvertProgram;
  24630. CheckSource('TestTypeHelper_Double',
  24631. LinesToStr([ // statements
  24632. 'rtl.createHelper(this, "THelper", null, function () {',
  24633. ' this.NPI = 3.141592;',
  24634. ' this.ToStr = function () {',
  24635. ' var Result = "";',
  24636. ' return Result;',
  24637. ' };',
  24638. '});',
  24639. 'this.DoIt = function (s) {',
  24640. '};',
  24641. 'this.f = 0.0;',
  24642. '']),
  24643. LinesToStr([ // $mod.$main
  24644. '$mod.DoIt($mod.THelper.ToStr.call({',
  24645. ' p: $mod,',
  24646. ' get: function () {',
  24647. ' return this.p.f;',
  24648. ' },',
  24649. ' set: function (v) {',
  24650. ' this.p.f = v;',
  24651. ' }',
  24652. '}));',
  24653. '$mod.DoIt($mod.THelper.ToStr.call({',
  24654. ' p: $mod,',
  24655. ' get: function () {',
  24656. ' return this.p.f;',
  24657. ' },',
  24658. ' set: function (v) {',
  24659. ' this.p.f = v;',
  24660. ' }',
  24661. '}));',
  24662. '$mod.THelper.ToStr.call({',
  24663. ' a: $mod.f * $mod.f,',
  24664. ' get: function () {',
  24665. ' return this.a;',
  24666. ' },',
  24667. ' set: function (v) {',
  24668. ' rtl.raiseE("EPropReadOnly");',
  24669. ' }',
  24670. '});',
  24671. '$mod.DoIt($mod.THelper.ToStr.call({',
  24672. ' a: $mod.f * $mod.f,',
  24673. ' get: function () {',
  24674. ' return this.a;',
  24675. ' },',
  24676. ' set: function (v) {',
  24677. ' rtl.raiseE("EPropReadOnly");',
  24678. ' }',
  24679. '}));',
  24680. '']));
  24681. end;
  24682. procedure TTestModule.TestTypeHelper_NativeInt;
  24683. begin
  24684. StartProgram(false);
  24685. Add([
  24686. '{$modeswitch typehelpers}',
  24687. 'type',
  24688. ' MaxInt = type nativeint;',
  24689. ' THelperI = type helper for MaxInt',
  24690. ' function ToStr: String;',
  24691. ' end;',
  24692. ' MaxUInt = type nativeuint;',
  24693. ' THelperU = type helper for MaxUInt',
  24694. ' function ToStr: String;',
  24695. ' end;',
  24696. 'function THelperI.ToStr: String;',
  24697. 'begin',
  24698. ' Result:=str(Self);',
  24699. 'end;',
  24700. 'function THelperU.ToStr: String;',
  24701. 'begin',
  24702. ' Result:=str(Self);',
  24703. 'end;',
  24704. 'procedure DoIt(s: string);',
  24705. 'begin',
  24706. 'end;',
  24707. 'var i: MaxInt;',
  24708. 'begin',
  24709. ' DoIt(i.toStr);',
  24710. ' DoIt(i.toStr());',
  24711. ' (i*i).toStr;',
  24712. ' DoIt((i*i).toStr);',
  24713. '']);
  24714. ConvertProgram;
  24715. CheckSource('TestTypeHelper_NativeInt',
  24716. LinesToStr([ // statements
  24717. 'rtl.createHelper(this, "THelperI", null, function () {',
  24718. ' this.ToStr = function () {',
  24719. ' var Result = "";',
  24720. ' Result = "" + this.get();',
  24721. ' return Result;',
  24722. ' };',
  24723. '});',
  24724. 'rtl.createHelper(this, "THelperU", null, function () {',
  24725. ' this.ToStr = function () {',
  24726. ' var Result = "";',
  24727. ' Result = "" + this.get();',
  24728. ' return Result;',
  24729. ' };',
  24730. '});',
  24731. 'this.DoIt = function (s) {',
  24732. '};',
  24733. 'this.i = 0;',
  24734. '']),
  24735. LinesToStr([ // $mod.$main
  24736. '$mod.DoIt($mod.THelperI.ToStr.call({',
  24737. ' p: $mod,',
  24738. ' get: function () {',
  24739. ' return this.p.i;',
  24740. ' },',
  24741. ' set: function (v) {',
  24742. ' this.p.i = v;',
  24743. ' }',
  24744. '}));',
  24745. '$mod.DoIt($mod.THelperI.ToStr.call({',
  24746. ' p: $mod,',
  24747. ' get: function () {',
  24748. ' return this.p.i;',
  24749. ' },',
  24750. ' set: function (v) {',
  24751. ' this.p.i = v;',
  24752. ' }',
  24753. '}));',
  24754. '$mod.THelperI.ToStr.call({',
  24755. ' a: $mod.i * $mod.i,',
  24756. ' get: function () {',
  24757. ' return this.a;',
  24758. ' },',
  24759. ' set: function (v) {',
  24760. ' rtl.raiseE("EPropReadOnly");',
  24761. ' }',
  24762. '});',
  24763. '$mod.DoIt($mod.THelperI.ToStr.call({',
  24764. ' a: $mod.i * $mod.i,',
  24765. ' get: function () {',
  24766. ' return this.a;',
  24767. ' },',
  24768. ' set: function (v) {',
  24769. ' rtl.raiseE("EPropReadOnly");',
  24770. ' }',
  24771. '}));',
  24772. '']));
  24773. end;
  24774. procedure TTestModule.TestTypeHelper_StringChar;
  24775. begin
  24776. StartProgram(false);
  24777. Add([
  24778. '{$modeswitch typehelpers}',
  24779. 'type',
  24780. ' TStringHelper = type helper for string',
  24781. ' procedure DoIt(e: byte = 123);',
  24782. ' end;',
  24783. ' TCharHelper = type helper for char',
  24784. ' procedure Fly;',
  24785. ' end;',
  24786. 'procedure TStringHelper.DoIt(e: byte);',
  24787. 'begin',
  24788. ' Self[1]:=''c'';',
  24789. ' Self[2]:=Self[3];',
  24790. 'end;',
  24791. 'procedure TCharHelper.Fly;',
  24792. 'begin',
  24793. ' Self:=''c'';',
  24794. 'end;',
  24795. 'begin',
  24796. ' ''abc''.DoIt;',
  24797. ' ''xyz''.DoIt();',
  24798. ' ''c''.Fly();',
  24799. '']);
  24800. ConvertProgram;
  24801. CheckSource('TestTypeHelper_StringChar',
  24802. LinesToStr([ // statements
  24803. 'rtl.createHelper(this, "TStringHelper", null, function () {',
  24804. ' this.DoIt = function (e) {',
  24805. ' this.set(rtl.setCharAt(this.get(), 0, "c"));',
  24806. ' this.set(rtl.setCharAt(this.get(), 1, this.get().charAt(2)));',
  24807. ' };',
  24808. '});',
  24809. 'rtl.createHelper(this, "TCharHelper", null, function () {',
  24810. ' this.Fly = function () {',
  24811. ' this.set("c");',
  24812. ' };',
  24813. '});',
  24814. '']),
  24815. LinesToStr([ // $mod.$main
  24816. '$mod.TStringHelper.DoIt.call({',
  24817. ' get: function () {',
  24818. ' return "abc";',
  24819. ' },',
  24820. ' set: function (v) {',
  24821. ' rtl.raiseE("EPropReadOnly");',
  24822. ' }',
  24823. '}, 123);',
  24824. '$mod.TStringHelper.DoIt.call({',
  24825. ' get: function () {',
  24826. ' return "xyz";',
  24827. ' },',
  24828. ' set: function (v) {',
  24829. ' rtl.raiseE("EPropReadOnly");',
  24830. ' }',
  24831. '}, 123);',
  24832. '$mod.TCharHelper.Fly.call({',
  24833. ' get: function () {',
  24834. ' return "c";',
  24835. ' },',
  24836. ' set: function (v) {',
  24837. ' rtl.raiseE("EPropReadOnly");',
  24838. ' }',
  24839. '});',
  24840. '']));
  24841. end;
  24842. procedure TTestModule.TestTypeHelper_JSValue;
  24843. begin
  24844. StartProgram(false);
  24845. Add([
  24846. '{$modeswitch typehelpers}',
  24847. 'type',
  24848. ' TExtValue = type jsvalue;',
  24849. ' THelper = type helper for TExtValue',
  24850. ' function ToStr: String;',
  24851. ' end;',
  24852. 'function THelper.ToStr: String;',
  24853. 'begin',
  24854. 'end;',
  24855. 'var',
  24856. ' s: string;',
  24857. ' v: TExtValue;',
  24858. 'begin',
  24859. ' s:=v.toStr;',
  24860. ' s:=v.toStr();',
  24861. ' TExtValue(s).toStr;',
  24862. '']);
  24863. ConvertProgram;
  24864. CheckSource('TestTypeHelper_JSValue',
  24865. LinesToStr([ // statements
  24866. 'rtl.createHelper(this, "THelper", null, function () {',
  24867. ' this.ToStr = function () {',
  24868. ' var Result = "";',
  24869. ' return Result;',
  24870. ' };',
  24871. '});',
  24872. 'this.s = "";',
  24873. 'this.v = undefined;',
  24874. '']),
  24875. LinesToStr([ // $mod.$main
  24876. '$mod.s = $mod.THelper.ToStr.call({',
  24877. ' p: $mod,',
  24878. ' get: function () {',
  24879. ' return this.p.v;',
  24880. ' },',
  24881. ' set: function (v) {',
  24882. ' this.p.v = v;',
  24883. ' }',
  24884. '});',
  24885. '$mod.s = $mod.THelper.ToStr.call({',
  24886. ' p: $mod,',
  24887. ' get: function () {',
  24888. ' return this.p.v;',
  24889. ' },',
  24890. ' set: function (v) {',
  24891. ' this.p.v = v;',
  24892. ' }',
  24893. '});',
  24894. '$mod.THelper.ToStr.call({',
  24895. ' p: $mod,',
  24896. ' get: function () {',
  24897. ' return this.p.s;',
  24898. ' },',
  24899. ' set: function (v) {',
  24900. ' rtl.raiseE("EPropReadOnly");',
  24901. ' }',
  24902. '});',
  24903. '']));
  24904. end;
  24905. procedure TTestModule.TestTypeHelper_Array;
  24906. begin
  24907. StartProgram(false);
  24908. Add([
  24909. '{$modeswitch typehelpers}',
  24910. 'type',
  24911. ' TArrOfBool = array of boolean;',
  24912. ' TArrOfJS = array of jsvalue;',
  24913. ' THelper = type helper for TArrOfBool',
  24914. ' procedure DoIt(e: byte = 123);',
  24915. ' end;',
  24916. 'procedure THelper.DoIt(e: byte);',
  24917. 'begin',
  24918. ' Self[1]:=true;',
  24919. ' Self[2]:=not Self[3];',
  24920. ' SetLength(Self,4);',
  24921. 'end;',
  24922. 'var',
  24923. ' b: TArrOfBool;',
  24924. ' j: TArrOfJS;',
  24925. 'begin',
  24926. ' b.DoIt;',
  24927. ' TArrOfBool(j).DoIt();',
  24928. '']);
  24929. ConvertProgram;
  24930. CheckSource('TestTypeHelper_Array',
  24931. LinesToStr([ // statements
  24932. 'rtl.createHelper(this, "THelper", null, function () {',
  24933. ' this.DoIt = function (e) {',
  24934. ' this.get()[1] = true;',
  24935. ' this.get()[2] = !this.get()[3];',
  24936. ' this.set(rtl.arraySetLength(this.get(), false, 4));',
  24937. ' };',
  24938. '});',
  24939. 'this.b = [];',
  24940. 'this.j = [];',
  24941. '']),
  24942. LinesToStr([ // $mod.$main
  24943. '$mod.THelper.DoIt.call({',
  24944. ' p: $mod,',
  24945. ' get: function () {',
  24946. ' return this.p.b;',
  24947. ' },',
  24948. ' set: function (v) {',
  24949. ' this.p.b = v;',
  24950. ' }',
  24951. '}, 123);',
  24952. '$mod.THelper.DoIt.call({',
  24953. ' p: $mod,',
  24954. ' get: function () {',
  24955. ' return this.p.j;',
  24956. ' },',
  24957. ' set: function (v) {',
  24958. ' this.p.j = v;',
  24959. ' }',
  24960. '}, 123);',
  24961. '']));
  24962. end;
  24963. procedure TTestModule.TestTypeHelper_EnumType;
  24964. begin
  24965. StartProgram(false);
  24966. Add([
  24967. '{$modeswitch typehelpers}',
  24968. 'type',
  24969. ' TEnum = (red,blue);',
  24970. ' THelper = type helper for TEnum',
  24971. ' procedure DoIt(e: byte = 123);',
  24972. ' class procedure Swing(w: word); static;',
  24973. ' end;',
  24974. 'procedure THelper.DoIt(e: byte);',
  24975. 'begin',
  24976. ' Self:=red;',
  24977. ' Self:=succ(Self);',
  24978. ' with Self do Doit;',
  24979. 'end;',
  24980. 'class procedure THelper.Swing(w: word);',
  24981. 'begin',
  24982. 'end;',
  24983. 'var e: TEnum;',
  24984. 'begin',
  24985. ' e.DoIt;',
  24986. ' red.DoIt;',
  24987. ' TEnum.blue.DoIt;',
  24988. ' TEnum(1).DoIt;',
  24989. ' TEnum.Swing(3);',
  24990. '']);
  24991. ConvertProgram;
  24992. CheckSource('TestTypeHelper_EnumType',
  24993. LinesToStr([ // statements
  24994. 'this.TEnum = {',
  24995. ' "0": "red",',
  24996. ' red: 0,',
  24997. ' "1": "blue",',
  24998. ' blue: 1',
  24999. '};',
  25000. 'rtl.createHelper(this, "THelper", null, function () {',
  25001. ' this.DoIt = function (e) {',
  25002. ' this.set($mod.TEnum.red);',
  25003. ' this.set(this.get() + 1);',
  25004. ' var $with = this.get();',
  25005. ' $mod.THelper.DoIt.call(this, 123);',
  25006. ' };',
  25007. ' this.Swing = function (w) {',
  25008. ' };',
  25009. '});',
  25010. 'this.e = 0;',
  25011. '']),
  25012. LinesToStr([ // $mod.$main
  25013. '$mod.THelper.DoIt.call({',
  25014. ' p: $mod,',
  25015. ' get: function () {',
  25016. ' return this.p.e;',
  25017. ' },',
  25018. ' set: function (v) {',
  25019. ' this.p.e = v;',
  25020. ' }',
  25021. '}, 123);',
  25022. '$mod.THelper.DoIt.call({',
  25023. ' p: $mod.TEnum,',
  25024. ' get: function () {',
  25025. ' return this.p.red;',
  25026. ' },',
  25027. ' set: function (v) {',
  25028. ' rtl.raiseE("EPropReadOnly");',
  25029. ' }',
  25030. '}, 123);',
  25031. '$mod.THelper.DoIt.call({',
  25032. ' p: $mod.TEnum,',
  25033. ' get: function () {',
  25034. ' return this.p.blue;',
  25035. ' },',
  25036. ' set: function (v) {',
  25037. ' rtl.raiseE("EPropReadOnly");',
  25038. ' }',
  25039. '}, 123);',
  25040. '$mod.THelper.DoIt.call({',
  25041. ' get: function () {',
  25042. ' return 1;',
  25043. ' },',
  25044. ' set: function (v) {',
  25045. ' rtl.raiseE("EPropReadOnly");',
  25046. ' }',
  25047. '}, 123);',
  25048. '$mod.THelper.Swing(3);',
  25049. '']));
  25050. end;
  25051. procedure TTestModule.TestTypeHelper_SetType;
  25052. begin
  25053. StartProgram(false);
  25054. Add([
  25055. '{$modeswitch typehelpers}',
  25056. 'type',
  25057. ' TEnum = (red,blue);',
  25058. ' TSetOfEnum = set of TEnum;',
  25059. ' THelper = type helper for TSetOfEnum',
  25060. ' procedure DoIt(e: byte = 123);',
  25061. ' constructor Init(e: TEnum);',
  25062. ' constructor InitEmpty;',
  25063. ' end;',
  25064. 'procedure THelper.DoIt(e: byte);',
  25065. 'begin',
  25066. ' Self:=[];',
  25067. ' Self:=[red];',
  25068. ' Include(Self,blue);',
  25069. 'end;',
  25070. 'constructor THelper.Init(e: TEnum);',
  25071. 'begin',
  25072. ' Self:=[];',
  25073. ' Self:=[e];',
  25074. ' Include(Self,blue);',
  25075. 'end;',
  25076. 'constructor THelper.InitEmpty;',
  25077. 'begin',
  25078. 'end;',
  25079. 'var s: TSetOfEnum;',
  25080. 'begin',
  25081. ' s.DoIt;',
  25082. //' [red].DoIt;',
  25083. //' with s do DoIt;',
  25084. //' with [red,blue] do DoIt;',
  25085. ' s:=TSetOfEnum.Init(blue);',
  25086. ' s:=s.Init(blue);',
  25087. '']);
  25088. ConvertProgram;
  25089. CheckSource('TestTypeHelper_SetType',
  25090. LinesToStr([ // statements
  25091. 'this.TEnum = {',
  25092. ' "0": "red",',
  25093. ' red: 0,',
  25094. ' "1": "blue",',
  25095. ' blue: 1',
  25096. '};',
  25097. 'rtl.createHelper(this, "THelper", null, function () {',
  25098. ' this.DoIt = function (e) {',
  25099. ' this.set({});',
  25100. ' this.set(rtl.createSet($mod.TEnum.red));',
  25101. ' this.set(rtl.includeSet(this.get(), $mod.TEnum.blue));',
  25102. ' };',
  25103. ' this.Init = function (e) {',
  25104. ' this.set({});',
  25105. ' this.set(rtl.createSet(e));',
  25106. ' this.set(rtl.includeSet(this.get(), $mod.TEnum.blue));',
  25107. ' return this.get();',
  25108. ' };',
  25109. ' this.InitEmpty = function () {',
  25110. ' return this.get();',
  25111. ' };',
  25112. ' this.$new = function (fn, args) {',
  25113. ' return this[fn].apply({',
  25114. ' p: {},',
  25115. ' get: function () {',
  25116. ' return this.p;',
  25117. ' },',
  25118. ' set: function (v) {',
  25119. ' this.p = v;',
  25120. ' }',
  25121. ' }, args);',
  25122. ' };',
  25123. '});',
  25124. 'this.s = {};',
  25125. '']),
  25126. LinesToStr([ // $mod.$main
  25127. '$mod.THelper.DoIt.call({',
  25128. ' p: $mod,',
  25129. ' get: function () {',
  25130. ' return this.p.s;',
  25131. ' },',
  25132. ' set: function (v) {',
  25133. ' this.p.s = v;',
  25134. ' }',
  25135. '}, 123);',
  25136. '$mod.s = rtl.refSet($mod.THelper.$new("Init", [$mod.TEnum.blue]));',
  25137. '$mod.s = rtl.refSet($mod.THelper.Init.call({',
  25138. ' p: $mod,',
  25139. ' get: function () {',
  25140. ' return this.p.s;',
  25141. ' },',
  25142. ' set: function (v) {',
  25143. ' this.p.s = v;',
  25144. ' }',
  25145. '}, $mod.TEnum.blue));',
  25146. '']));
  25147. end;
  25148. procedure TTestModule.TestTypeHelper_InterfaceType;
  25149. begin
  25150. StartProgram(false);
  25151. Add([
  25152. '{$interfaces com}',
  25153. '{$modeswitch typehelpers}',
  25154. 'type',
  25155. ' IUnknown = interface',
  25156. ' function _AddRef: longint;',
  25157. ' function _Release: longint;',
  25158. ' end;',
  25159. ' TObject = class(IUnknown)',
  25160. ' function _AddRef: longint; virtual; abstract;',
  25161. ' function _Release: longint; virtual; abstract;',
  25162. ' end;',
  25163. ' THelper = type helper for IUnknown',
  25164. ' procedure Fly(e: byte = 123);',
  25165. ' class procedure Run; static;',
  25166. ' end;',
  25167. 'var',
  25168. ' i: IUnknown;',
  25169. ' o: TObject;',
  25170. 'procedure THelper.Fly(e: byte);',
  25171. 'begin',
  25172. ' i:=Self;',
  25173. ' o:=Self as TObject;',
  25174. ' Self:=nil;',
  25175. ' Self:=i;',
  25176. ' Self:=o;',
  25177. ' with Self do begin',
  25178. ' Fly;',
  25179. ' Fly();',
  25180. ' end;',
  25181. 'end;',
  25182. 'class procedure THelper.Run;',
  25183. 'var l: IUnknown;',
  25184. 'begin',
  25185. ' l.Fly;',
  25186. ' l.Fly();',
  25187. 'end;',
  25188. 'begin',
  25189. ' i.Fly;',
  25190. ' i.Fly();',
  25191. ' i.Run;',
  25192. ' i.Run();',
  25193. ' IUnknown.Run;',
  25194. ' IUnknown.Run();',
  25195. '']);
  25196. ConvertProgram;
  25197. CheckSource('TestTypeHelper_InterfaceType',
  25198. LinesToStr([ // statements
  25199. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  25200. 'rtl.createClass(this, "TObject", null, function () {',
  25201. ' this.$init = function () {',
  25202. ' };',
  25203. ' this.$final = function () {',
  25204. ' };',
  25205. ' rtl.addIntf(this, $mod.IUnknown);',
  25206. '});',
  25207. 'rtl.createHelper(this, "THelper", null, function () {',
  25208. ' this.Fly = function (e) {',
  25209. ' var $ir = rtl.createIntfRefs();',
  25210. ' try {',
  25211. ' rtl.setIntfP($mod, "i", this.get());',
  25212. ' $mod.o = rtl.intfAsClass(this.get(), $mod.TObject);',
  25213. ' this.set(null);',
  25214. ' this.set($mod.i);',
  25215. ' this.set($ir.ref(1, rtl.queryIntfT($mod.o, $mod.IUnknown)));',
  25216. ' var $with = this.get();',
  25217. ' $mod.THelper.Fly.call(this, 123);',
  25218. ' $mod.THelper.Fly.call(this, 123);',
  25219. ' } finally {',
  25220. ' $ir.free();',
  25221. ' };',
  25222. ' };',
  25223. ' this.Run = function () {',
  25224. ' var l = null;',
  25225. ' try {',
  25226. ' $mod.THelper.Fly.call({',
  25227. ' get: function () {',
  25228. ' return l;',
  25229. ' },',
  25230. ' set: function (v) {',
  25231. ' l = rtl.setIntfL(l, v);',
  25232. ' }',
  25233. ' }, 123);',
  25234. ' $mod.THelper.Fly.call({',
  25235. ' get: function () {',
  25236. ' return l;',
  25237. ' },',
  25238. ' set: function (v) {',
  25239. ' l = rtl.setIntfL(l, v);',
  25240. ' }',
  25241. ' }, 123);',
  25242. ' } finally {',
  25243. ' rtl._Release(l);',
  25244. ' };',
  25245. ' };',
  25246. '});',
  25247. 'this.i = null;',
  25248. 'this.o = null;',
  25249. '']),
  25250. LinesToStr([ // $mod.$main
  25251. '$mod.THelper.Fly.call({',
  25252. ' p: $mod,',
  25253. ' get: function () {',
  25254. ' return this.p.i;',
  25255. ' },',
  25256. ' set: function (v) {',
  25257. ' rtl.setIntfP(this.p, "i", v);',
  25258. ' }',
  25259. '}, 123);',
  25260. '$mod.THelper.Fly.call({',
  25261. ' p: $mod,',
  25262. ' get: function () {',
  25263. ' return this.p.i;',
  25264. ' },',
  25265. ' set: function (v) {',
  25266. ' rtl.setIntfP(this.p, "i", v);',
  25267. ' }',
  25268. '}, 123);',
  25269. '$mod.THelper.Run();',
  25270. '$mod.THelper.Run();',
  25271. '$mod.THelper.Run();',
  25272. '$mod.THelper.Run();',
  25273. '']));
  25274. end;
  25275. procedure TTestModule.TestTypeHelper_NestedSelf;
  25276. begin
  25277. StartProgram(false);
  25278. Add([
  25279. '{$modeswitch typehelpers}',
  25280. 'type',
  25281. ' THelper = type helper for string',
  25282. ' procedure Run(Value: string);',
  25283. ' end;',
  25284. 'procedure THelper.Run(Value: string);',
  25285. ' function Sub(i: nativeint): boolean;',
  25286. ' begin',
  25287. ' Result:=Self[i+1]=Value[i];',
  25288. ' end;',
  25289. 'begin',
  25290. ' if Self[3]=Value[4] then ;',
  25291. 'end;',
  25292. 'begin',
  25293. '']);
  25294. ConvertProgram;
  25295. CheckSource('TestTypeHelper_NestedSelf',
  25296. LinesToStr([ // statements
  25297. 'rtl.createHelper(this, "THelper", null, function () {',
  25298. ' this.Run = function (Value) {',
  25299. ' var $Self = this;',
  25300. ' function Sub(i) {',
  25301. ' var Result = false;',
  25302. ' Result = $Self.get().charAt((i + 1) - 1) === Value.charAt(i - 1);',
  25303. ' return Result;',
  25304. ' };',
  25305. ' if ($Self.get().charAt(2) === Value.charAt(3)) ;',
  25306. ' };',
  25307. '});',
  25308. '']),
  25309. LinesToStr([ // $mod.$main
  25310. '']));
  25311. end;
  25312. procedure TTestModule.TestProcType;
  25313. begin
  25314. StartProgram(false);
  25315. Add([
  25316. 'type',
  25317. ' TProcInt = procedure(vI: longint = 1);',
  25318. 'procedure DoIt(vJ: longint);',
  25319. 'begin end;',
  25320. 'var',
  25321. ' b: boolean;',
  25322. ' vP, vQ: tprocint;',
  25323. 'begin',
  25324. ' vp:=nil;',
  25325. ' vp:=vp;',
  25326. ' vp:=@doit;',
  25327. ' vp;',
  25328. ' vp();',
  25329. ' vp(2);',
  25330. ' b:=vp=nil;',
  25331. ' b:=nil=vp;',
  25332. ' b:=vp=vq;',
  25333. ' b:=vp=@doit;',
  25334. ' b:=@doit=vp;',
  25335. ' b:=vp<>nil;',
  25336. ' b:=nil<>vp;',
  25337. ' b:=vp<>vq;',
  25338. ' b:=vp<>@doit;',
  25339. ' b:=@doit<>vp;',
  25340. ' b:=Assigned(vp);',
  25341. ' if Assigned(vp) then ;']);
  25342. ConvertProgram;
  25343. CheckSource('TestProcType',
  25344. LinesToStr([ // statements
  25345. 'this.DoIt = function(vJ) {',
  25346. '};',
  25347. 'this.b = false;',
  25348. 'this.vP = null;',
  25349. 'this.vQ = null;'
  25350. ]),
  25351. LinesToStr([ // $mod.$main
  25352. '$mod.vP = null;',
  25353. '$mod.vP = $mod.vP;',
  25354. '$mod.vP = $mod.DoIt;',
  25355. '$mod.vP(1);',
  25356. '$mod.vP(1);',
  25357. '$mod.vP(2);',
  25358. '$mod.b = $mod.vP === null;',
  25359. '$mod.b = null === $mod.vP;',
  25360. '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
  25361. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  25362. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  25363. '$mod.b = $mod.vP !== null;',
  25364. '$mod.b = null !== $mod.vP;',
  25365. '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
  25366. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  25367. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  25368. '$mod.b = $mod.vP != null;',
  25369. 'if ($mod.vP != null) ;',
  25370. '']));
  25371. end;
  25372. procedure TTestModule.TestProcType_Arg;
  25373. begin
  25374. StartProgram(false);
  25375. Add([
  25376. 'type',
  25377. ' TProcInt = procedure(vI: longint = 1);',
  25378. 'procedure DoIt(vJ: longint); begin end;',
  25379. 'procedure DoSome(vP, vQ: TProcInt);',
  25380. 'var',
  25381. ' b: boolean;',
  25382. 'begin',
  25383. ' vp:=nil;',
  25384. ' vp:=vp;',
  25385. ' vp:=@doit;',
  25386. ' vp;',
  25387. ' vp();',
  25388. ' vp(2);',
  25389. ' b:=vp=nil;',
  25390. ' b:=nil=vp;',
  25391. ' b:=vp=vq;',
  25392. ' b:=vp=@doit;',
  25393. ' b:=@doit=vp;',
  25394. ' b:=vp<>nil;',
  25395. ' b:=nil<>vp;',
  25396. ' b:=vp<>vq;',
  25397. ' b:=vp<>@doit;',
  25398. ' b:=@doit<>vp;',
  25399. ' b:=Assigned(vp);',
  25400. ' if Assigned(vp) then ;',
  25401. 'end;',
  25402. 'begin',
  25403. ' DoSome(@DoIt,nil);']);
  25404. ConvertProgram;
  25405. CheckSource('TestProcType_Arg',
  25406. LinesToStr([ // statements
  25407. 'this.DoIt = function(vJ) {',
  25408. '};',
  25409. 'this.DoSome = function(vP, vQ) {',
  25410. ' var b = false;',
  25411. ' vP = null;',
  25412. ' vP = vP;',
  25413. ' vP = $mod.DoIt;',
  25414. ' vP(1);',
  25415. ' vP(1);',
  25416. ' vP(2);',
  25417. ' b = vP === null;',
  25418. ' b = null === vP;',
  25419. ' b = rtl.eqCallback(vP,vQ);',
  25420. ' b = rtl.eqCallback(vP, $mod.DoIt);',
  25421. ' b = rtl.eqCallback($mod.DoIt, vP);',
  25422. ' b = vP !== null;',
  25423. ' b = null !== vP;',
  25424. ' b = !rtl.eqCallback(vP, vQ);',
  25425. ' b = !rtl.eqCallback(vP, $mod.DoIt);',
  25426. ' b = !rtl.eqCallback($mod.DoIt, vP);',
  25427. ' b = vP != null;',
  25428. ' if (vP != null) ;',
  25429. '};',
  25430. '']),
  25431. LinesToStr([ // $mod.$main
  25432. '$mod.DoSome($mod.DoIt,null);',
  25433. '']));
  25434. end;
  25435. procedure TTestModule.TestProcType_FunctionFPC;
  25436. begin
  25437. StartProgram(false);
  25438. Add('type');
  25439. Add(' TFuncInt = function(vA: longint = 1): longint;');
  25440. Add('function DoIt(vI: longint): longint;');
  25441. Add('begin end;');
  25442. Add('var');
  25443. Add(' b: boolean;');
  25444. Add(' vP, vQ: tfuncint;');
  25445. Add('begin');
  25446. Add(' vp:=nil;');
  25447. Add(' vp:=vp;');
  25448. Add(' vp:=@doit;'); // ok in fpc and delphi
  25449. //Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  25450. Add(' vp;'); // ok in fpc and delphi
  25451. Add(' vp();');
  25452. Add(' vp(2);');
  25453. Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  25454. Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  25455. Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  25456. Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  25457. Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  25458. //Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
  25459. Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
  25460. Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  25461. Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  25462. Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  25463. Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  25464. Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  25465. //Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
  25466. Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
  25467. Add(' b:=Assigned(vp);');
  25468. //Add(' doit(vp);'); // illegal in fpc, ok in delphi
  25469. Add(' doit(vp());'); // ok in fpc and delphi
  25470. Add(' doit(vp(2));'); // ok in fpc and delphi
  25471. ConvertProgram;
  25472. CheckSource('TestProcType_FunctionFPC',
  25473. LinesToStr([ // statements
  25474. 'this.DoIt = function(vI) {',
  25475. ' var Result = 0;',
  25476. ' return Result;',
  25477. '};',
  25478. 'this.b = false;',
  25479. 'this.vP = null;',
  25480. 'this.vQ = null;'
  25481. ]),
  25482. LinesToStr([ // $mod.$main
  25483. '$mod.vP = null;',
  25484. '$mod.vP = $mod.vP;',
  25485. '$mod.vP = $mod.DoIt;',
  25486. '$mod.vP(1);',
  25487. '$mod.vP(1);',
  25488. '$mod.vP(2);',
  25489. '$mod.b = $mod.vP === null;',
  25490. '$mod.b = null === $mod.vP;',
  25491. '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
  25492. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  25493. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  25494. '$mod.b = 4 === $mod.vP(1);',
  25495. '$mod.b = $mod.vP !== null;',
  25496. '$mod.b = null !== $mod.vP;',
  25497. '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
  25498. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  25499. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  25500. '$mod.b = 6 !== $mod.vP(1);',
  25501. '$mod.b = $mod.vP != null;',
  25502. '$mod.DoIt($mod.vP(1));',
  25503. '$mod.DoIt($mod.vP(2));',
  25504. '']));
  25505. end;
  25506. procedure TTestModule.TestProcType_FunctionDelphi;
  25507. begin
  25508. StartProgram(false);
  25509. Add('{$mode Delphi}');
  25510. Add('type');
  25511. Add(' TFuncInt = function(vA: longint = 1): longint;');
  25512. Add('function DoIt(vI: longint): longint;');
  25513. Add('begin end;');
  25514. Add('var');
  25515. Add(' b: boolean;');
  25516. Add(' vP, vQ: tfuncint;');
  25517. Add('begin');
  25518. Add(' vp:=nil;');
  25519. Add(' vp:=vp;');
  25520. Add(' vp:=@doit;'); // ok in fpc and delphi
  25521. Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  25522. Add(' vp;'); // ok in fpc and delphi
  25523. Add(' vp();');
  25524. Add(' vp(2);');
  25525. //Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  25526. //Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  25527. Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  25528. //Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  25529. //Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  25530. Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
  25531. Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
  25532. //Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  25533. //Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  25534. Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  25535. //Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  25536. //Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  25537. Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
  25538. Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
  25539. Add(' b:=Assigned(vp);');
  25540. Add(' doit(vp);'); // illegal in fpc, ok in delphi
  25541. Add(' doit(vp());'); // ok in fpc and delphi
  25542. Add(' doit(vp(2));'); // ok in fpc and delphi *)
  25543. ConvertProgram;
  25544. CheckSource('TestProcType_FunctionDelphi',
  25545. LinesToStr([ // statements
  25546. 'this.DoIt = function(vI) {',
  25547. ' var Result = 0;',
  25548. ' return Result;',
  25549. '};',
  25550. 'this.b = false;',
  25551. 'this.vP = null;',
  25552. 'this.vQ = null;'
  25553. ]),
  25554. LinesToStr([ // $mod.$main
  25555. '$mod.vP = null;',
  25556. '$mod.vP = $mod.vP;',
  25557. '$mod.vP = $mod.DoIt;',
  25558. '$mod.vP = $mod.DoIt;',
  25559. '$mod.vP(1);',
  25560. '$mod.vP(1);',
  25561. '$mod.vP(2);',
  25562. '$mod.b = $mod.vP(1) === $mod.vQ(1);',
  25563. '$mod.b = $mod.vP(1) === 3;',
  25564. '$mod.b = 4 === $mod.vP(1);',
  25565. '$mod.b = $mod.vP(1) !== $mod.vQ(1);',
  25566. '$mod.b = $mod.vP(1) !== 5;',
  25567. '$mod.b = 6 !== $mod.vP(1);',
  25568. '$mod.b = $mod.vP != null;',
  25569. '$mod.DoIt($mod.vP(1));',
  25570. '$mod.DoIt($mod.vP(1));',
  25571. '$mod.DoIt($mod.vP(2));',
  25572. '']));
  25573. end;
  25574. procedure TTestModule.TestProcType_ProcedureDelphi;
  25575. begin
  25576. StartProgram(false);
  25577. Add('{$mode Delphi}');
  25578. Add('type');
  25579. Add(' TProc = procedure;');
  25580. Add('procedure DoIt;');
  25581. Add('begin end;');
  25582. Add('var');
  25583. Add(' b: boolean;');
  25584. Add(' vP, vQ: tproc;');
  25585. Add('begin');
  25586. Add(' vp:=nil;');
  25587. Add(' vp:=vp;');
  25588. Add(' vp:=vq;');
  25589. 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
  25590. Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  25591. //Add(' vp:=@doit;'); // illegal in fpc, ok in delphi (because Delphi treats @F as Pointer), not supported by resolver
  25592. Add(' vp;'); // ok in fpc and delphi
  25593. Add(' vp();');
  25594. // equal
  25595. //Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  25596. Add(' b:=@@vp=nil;'); // ok in fpc delphi mode, ok in delphi
  25597. //Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  25598. Add(' b:=nil=@@vp;'); // ok in fpc delphi mode, ok in delphi
  25599. Add(' b:=@@vp=@@vq;'); // ok in fpc delphi mode, ok 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:=@@vp=@doit;'); // ok in fpc delphi mode, ok in delphi
  25603. //Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  25604. Add(' b:=@doit=@@vp;'); // ok in fpc delphi mode, ok in delphi
  25605. // unequal
  25606. //Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  25607. Add(' b:=@@vp<>nil;'); // ok in fpc mode delphi, ok in delphi
  25608. //Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  25609. Add(' b:=nil<>@@vp;'); // ok in fpc mode delphi, ok in delphi
  25610. //Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  25611. Add(' b:=@@vp<>@@vq;'); // ok in fpc mode delphi, ok in delphi
  25612. //Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  25613. Add(' b:=@@vp<>@doit;'); // ok in fpc mode delphi, illegal in delphi
  25614. //Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  25615. Add(' b:=@doit<>@@vp;'); // ok in fpc mode delphi, illegal in delphi
  25616. Add(' b:=Assigned(vp);');
  25617. ConvertProgram;
  25618. CheckSource('TestProcType_ProcedureDelphi',
  25619. LinesToStr([ // statements
  25620. 'this.DoIt = function() {',
  25621. '};',
  25622. 'this.b = false;',
  25623. 'this.vP = null;',
  25624. 'this.vQ = null;'
  25625. ]),
  25626. LinesToStr([ // $mod.$main
  25627. '$mod.vP = null;',
  25628. '$mod.vP = $mod.vP;',
  25629. '$mod.vP = $mod.vQ;',
  25630. '$mod.vP = $mod.DoIt;',
  25631. '$mod.vP = $mod.DoIt;',
  25632. '$mod.vP();',
  25633. '$mod.vP();',
  25634. '$mod.b = $mod.vP === null;',
  25635. '$mod.b = null === $mod.vP;',
  25636. '$mod.b = rtl.eqCallback($mod.vP, $mod.vQ);',
  25637. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  25638. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  25639. '$mod.b = $mod.vP !== null;',
  25640. '$mod.b = null !== $mod.vP;',
  25641. '$mod.b = !rtl.eqCallback($mod.vP, $mod.vQ);',
  25642. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  25643. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  25644. '$mod.b = $mod.vP != null;',
  25645. '']));
  25646. end;
  25647. procedure TTestModule.TestProcType_AsParam;
  25648. begin
  25649. StartProgram(false);
  25650. Add('type');
  25651. Add(' TFuncInt = function(vA: longint = 1): longint;');
  25652. Add('procedure DoIt(vG: tfuncint; const vH: tfuncint; var vI: tfuncint);');
  25653. Add('var vJ: tfuncint;');
  25654. Add('begin');
  25655. Add(' vg:=vg;');
  25656. Add(' vj:=vh;');
  25657. Add(' vi:=vi;');
  25658. Add(' doit(vg,vg,vg);');
  25659. Add(' doit(vh,vh,vj);');
  25660. Add(' doit(vi,vi,vi);');
  25661. Add(' doit(vj,vj,vj);');
  25662. Add('end;');
  25663. Add('var i: tfuncint;');
  25664. Add('begin');
  25665. Add(' doit(i,i,i);');
  25666. ConvertProgram;
  25667. CheckSource('TestProcType_AsParam',
  25668. LinesToStr([ // statements
  25669. 'this.DoIt = function (vG,vH,vI) {',
  25670. ' var vJ = null;',
  25671. ' vG = vG;',
  25672. ' vJ = vH;',
  25673. ' vI.set(vI.get());',
  25674. ' $mod.DoIt(vG, vG, {',
  25675. ' get: function () {',
  25676. ' return vG;',
  25677. ' },',
  25678. ' set: function (v) {',
  25679. ' vG = v;',
  25680. ' }',
  25681. ' });',
  25682. ' $mod.DoIt(vH, vH, {',
  25683. ' get: function () {',
  25684. ' return vJ;',
  25685. ' },',
  25686. ' set: function (v) {',
  25687. ' vJ = v;',
  25688. ' }',
  25689. ' });',
  25690. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  25691. ' $mod.DoIt(vJ, vJ, {',
  25692. ' get: function () {',
  25693. ' return vJ;',
  25694. ' },',
  25695. ' set: function (v) {',
  25696. ' vJ = v;',
  25697. ' }',
  25698. ' });',
  25699. '};',
  25700. 'this.i = null;'
  25701. ]),
  25702. LinesToStr([
  25703. '$mod.DoIt($mod.i,$mod.i,{',
  25704. ' p: $mod,',
  25705. ' get: function () {',
  25706. ' return this.p.i;',
  25707. ' },',
  25708. ' set: function (v) {',
  25709. ' this.p.i = v;',
  25710. ' }',
  25711. '});'
  25712. ]));
  25713. end;
  25714. procedure TTestModule.TestProcType_MethodFPC;
  25715. begin
  25716. StartProgram(false);
  25717. Add('type');
  25718. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  25719. Add(' TObject = class');
  25720. Add(' function DoIt(vA: longint = 1): longint;');
  25721. Add(' end;');
  25722. Add('function TObject.DoIt(vA: longint = 1): longint;');
  25723. Add('begin');
  25724. Add('end;');
  25725. Add('var');
  25726. Add(' Obj: TObject;');
  25727. Add(' vP: tfuncint;');
  25728. Add(' b: boolean;');
  25729. Add('begin');
  25730. Add(' vp:[email protected];'); // ok in fpc and delphi
  25731. //Add(' vp:=obj.doit;'); // illegal in fpc, ok in delphi
  25732. Add(' vp;'); // ok in fpc and delphi
  25733. Add(' vp();');
  25734. Add(' vp(2);');
  25735. Add(' b:[email protected];'); // ok in fpc, illegal in delphi
  25736. Add(' b:[email protected]=vp;'); // ok in fpc, illegal in delphi
  25737. Add(' b:=vp<>@obj.doit;'); // ok in fpc, illegal in delphi
  25738. Add(' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
  25739. ConvertProgram;
  25740. CheckSource('TestProcType_MethodFPC',
  25741. LinesToStr([ // statements
  25742. 'rtl.createClass(this, "TObject", null, function () {',
  25743. ' this.$init = function () {',
  25744. ' };',
  25745. ' this.$final = function () {',
  25746. ' };',
  25747. ' this.DoIt = function (vA) {',
  25748. ' var Result = 0;',
  25749. ' return Result;',
  25750. ' };',
  25751. '});',
  25752. 'this.Obj = null;',
  25753. 'this.vP = null;',
  25754. 'this.b = false;'
  25755. ]),
  25756. LinesToStr([
  25757. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  25758. '$mod.vP(1);',
  25759. '$mod.vP(1);',
  25760. '$mod.vP(2);',
  25761. '$mod.b = rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
  25762. '$mod.b = rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
  25763. '$mod.b = !rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
  25764. '$mod.b = !rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
  25765. '']));
  25766. end;
  25767. procedure TTestModule.TestProcType_MethodDelphi;
  25768. begin
  25769. StartProgram(false);
  25770. Add([
  25771. '{$mode delphi}',
  25772. 'type',
  25773. ' TFuncInt = function(vA: longint = 1): longint of object;',
  25774. ' TObject = class',
  25775. ' function DoIt(vA: longint = 1): longint;',
  25776. ' end;',
  25777. 'function TObject.DoIt(vA: longint = 1): longint;',
  25778. 'begin',
  25779. 'end;',
  25780. 'var',
  25781. ' Obj: TObject;',
  25782. ' vP: tfuncint;',
  25783. ' b: boolean;',
  25784. 'begin',
  25785. ' vp:[email protected];', // ok in fpc and delphi
  25786. ' vp:=obj.doit;', // illegal in fpc, ok in delphi
  25787. ' vp;', // ok in fpc and delphi
  25788. ' vp();',
  25789. ' vp(2);',
  25790. //' b:[email protected];', // ok in fpc, illegal in delphi
  25791. //' b:[email protected]=vp;', // ok in fpc, illegal in delphi
  25792. //' b:=vp<>@obj.doit;', // ok in fpc, illegal in delphi
  25793. //' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
  25794. '']);
  25795. ConvertProgram;
  25796. CheckSource('TestProcType_MethodDelphi',
  25797. LinesToStr([ // statements
  25798. 'rtl.createClass(this, "TObject", null, function () {',
  25799. ' this.$init = function () {',
  25800. ' };',
  25801. ' this.$final = function () {',
  25802. ' };',
  25803. ' this.DoIt = function (vA) {',
  25804. ' var Result = 0;',
  25805. ' return Result;',
  25806. ' };',
  25807. '});',
  25808. 'this.Obj = null;',
  25809. 'this.vP = null;',
  25810. 'this.b = false;'
  25811. ]),
  25812. LinesToStr([
  25813. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  25814. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  25815. '$mod.vP(1);',
  25816. '$mod.vP(1);',
  25817. '$mod.vP(2);',
  25818. '']));
  25819. end;
  25820. procedure TTestModule.TestProcType_PropertyFPC;
  25821. begin
  25822. StartProgram(false);
  25823. Add('type');
  25824. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  25825. Add(' TObject = class');
  25826. Add(' FOnFoo: TFuncInt;');
  25827. Add(' function DoIt(vA: longint = 1): longint;');
  25828. Add(' function GetFoo: TFuncInt;');
  25829. Add(' procedure SetFoo(const Value: TFuncInt);');
  25830. Add(' function GetEvents(Index: longint): TFuncInt;');
  25831. Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
  25832. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  25833. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  25834. Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
  25835. Add(' end;');
  25836. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  25837. Add('function tobject.getfoo: tfuncint; begin end;');
  25838. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  25839. Add('function tobject.getevents(index: longint): tfuncint; begin end;');
  25840. Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
  25841. Add('var');
  25842. Add(' Obj: TObject;');
  25843. Add(' vP: tfuncint;');
  25844. Add(' b: boolean;');
  25845. Add('begin');
  25846. Add(' obj.onfoo:=nil;');
  25847. Add(' obj.onbar:=nil;');
  25848. Add(' obj.events[1]:=nil;');
  25849. Add(' obj.onfoo:=obj.onfoo;');
  25850. Add(' obj.onbar:=obj.onbar;');
  25851. Add(' obj.events[2]:=obj.events[3];');
  25852. Add(' obj.onfoo:[email protected];');
  25853. Add(' obj.onbar:[email protected];');
  25854. Add(' obj.events[4]:[email protected];');
  25855. //Add(' obj.onfoo:=obj.doit;'); // delphi
  25856. //Add(' obj.onbar:=obj.doit;'); // delphi
  25857. //Add(' obj.events[4]:=obj.doit;'); // delphi
  25858. Add(' obj.onfoo;');
  25859. Add(' obj.onbar;');
  25860. //Add(' obj.events[5];'); ToDo in pasresolver
  25861. Add(' obj.onfoo();');
  25862. Add(' obj.onbar();');
  25863. Add(' obj.events[6]();');
  25864. Add(' b:=obj.onfoo=nil;');
  25865. Add(' b:=obj.onbar=nil;');
  25866. Add(' b:=obj.events[7]=nil;');
  25867. Add(' b:=obj.onfoo<>nil;');
  25868. Add(' b:=obj.onbar<>nil;');
  25869. Add(' b:=obj.events[8]<>nil;');
  25870. Add(' b:=obj.onfoo=vp;');
  25871. Add(' b:=obj.onbar=vp;');
  25872. Add(' b:=obj.events[9]=vp;');
  25873. Add(' b:=obj.onfoo=obj.onfoo;');
  25874. Add(' b:=obj.onbar=obj.onfoo;');
  25875. Add(' b:=obj.events[10]=obj.onfoo;');
  25876. Add(' b:=obj.onfoo<>obj.onfoo;');
  25877. Add(' b:=obj.onbar<>obj.onfoo;');
  25878. Add(' b:=obj.events[11]<>obj.onfoo;');
  25879. Add(' b:[email protected];');
  25880. Add(' b:[email protected];');
  25881. Add(' b:=obj.events[12][email protected];');
  25882. Add(' b:=obj.onfoo<>@obj.doit;');
  25883. Add(' b:=obj.onbar<>@obj.doit;');
  25884. Add(' b:=obj.events[12]<>@obj.doit;');
  25885. Add(' b:=Assigned(obj.onfoo);');
  25886. Add(' b:=Assigned(obj.onbar);');
  25887. Add(' b:=Assigned(obj.events[13]);');
  25888. ConvertProgram;
  25889. CheckSource('TestProcType_PropertyFPC',
  25890. LinesToStr([ // statements
  25891. 'rtl.createClass(this, "TObject", null, function () {',
  25892. ' this.$init = function () {',
  25893. ' this.FOnFoo = null;',
  25894. ' };',
  25895. ' this.$final = function () {',
  25896. ' this.FOnFoo = undefined;',
  25897. ' };',
  25898. ' this.DoIt = function (vA) {',
  25899. ' var Result = 0;',
  25900. ' return Result;',
  25901. ' };',
  25902. 'this.GetFoo = function () {',
  25903. ' var Result = null;',
  25904. ' return Result;',
  25905. '};',
  25906. 'this.SetFoo = function (Value) {',
  25907. '};',
  25908. 'this.GetEvents = function (Index) {',
  25909. ' var Result = null;',
  25910. ' return Result;',
  25911. '};',
  25912. 'this.SetEvents = function (Index, Value) {',
  25913. '};',
  25914. '});',
  25915. 'this.Obj = null;',
  25916. 'this.vP = null;',
  25917. 'this.b = false;'
  25918. ]),
  25919. LinesToStr([
  25920. '$mod.Obj.FOnFoo = null;',
  25921. '$mod.Obj.SetFoo(null);',
  25922. '$mod.Obj.SetEvents(1, null);',
  25923. '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
  25924. '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
  25925. '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
  25926. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  25927. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  25928. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  25929. '$mod.Obj.FOnFoo(1);',
  25930. '$mod.Obj.GetFoo();',
  25931. '$mod.Obj.FOnFoo(1);',
  25932. '$mod.Obj.GetFoo()(1);',
  25933. '$mod.Obj.GetEvents(6)(1);',
  25934. '$mod.b = $mod.Obj.FOnFoo === null;',
  25935. '$mod.b = $mod.Obj.GetFoo() === null;',
  25936. '$mod.b = $mod.Obj.GetEvents(7) === null;',
  25937. '$mod.b = $mod.Obj.FOnFoo !== null;',
  25938. '$mod.b = $mod.Obj.GetFoo() !== null;',
  25939. '$mod.b = $mod.Obj.GetEvents(8) !== null;',
  25940. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.vP);',
  25941. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.vP);',
  25942. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(9), $mod.vP);',
  25943. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
  25944. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
  25945. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(10), $mod.Obj.FOnFoo);',
  25946. '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
  25947. '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
  25948. '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(11), $mod.Obj.FOnFoo);',
  25949. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
  25950. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
  25951. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
  25952. '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
  25953. '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
  25954. '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
  25955. '$mod.b = $mod.Obj.FOnFoo != null;',
  25956. '$mod.b = $mod.Obj.GetFoo() != null;',
  25957. '$mod.b = $mod.Obj.GetEvents(13) != null;',
  25958. '']));
  25959. end;
  25960. procedure TTestModule.TestProcType_PropertyDelphi;
  25961. begin
  25962. StartProgram(false);
  25963. Add('{$mode delphi}');
  25964. Add('type');
  25965. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  25966. Add(' TObject = class');
  25967. Add(' FOnFoo: TFuncInt;');
  25968. Add(' function DoIt(vA: longint = 1): longint;');
  25969. Add(' function GetFoo: TFuncInt;');
  25970. Add(' procedure SetFoo(const Value: TFuncInt);');
  25971. Add(' function GetEvents(Index: longint): TFuncInt;');
  25972. Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
  25973. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  25974. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  25975. Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
  25976. Add(' end;');
  25977. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  25978. Add('function tobject.getfoo: tfuncint; begin end;');
  25979. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  25980. Add('function tobject.getevents(index: longint): tfuncint; begin end;');
  25981. Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
  25982. Add('var');
  25983. Add(' Obj: TObject;');
  25984. Add(' vP: tfuncint;');
  25985. Add(' b: boolean;');
  25986. Add('begin');
  25987. Add(' obj.onfoo:=nil;');
  25988. Add(' obj.onbar:=nil;');
  25989. Add(' obj.events[1]:=nil;');
  25990. Add(' obj.onfoo:=obj.onfoo;');
  25991. Add(' obj.onbar:=obj.onbar;');
  25992. Add(' obj.events[2]:=obj.events[3];');
  25993. Add(' obj.onfoo:[email protected];');
  25994. Add(' obj.onbar:[email protected];');
  25995. Add(' obj.events[4]:[email protected];');
  25996. Add(' obj.onfoo:=obj.doit;'); // delphi
  25997. Add(' obj.onbar:=obj.doit;'); // delphi
  25998. Add(' obj.events[4]:=obj.doit;'); // delphi
  25999. Add(' obj.onfoo;');
  26000. Add(' obj.onbar;');
  26001. //Add(' obj.events[5];'); ToDo in pasresolver
  26002. Add(' obj.onfoo();');
  26003. Add(' obj.onbar();');
  26004. Add(' obj.events[6]();');
  26005. //Add(' b:=obj.onfoo=nil;'); // fpc
  26006. //Add(' b:=obj.onbar=nil;'); // fpc
  26007. //Add(' b:=obj.events[7]=nil;'); // fpc
  26008. //Add(' b:=obj.onfoo<>nil;'); // fpc
  26009. //Add(' b:=obj.onbar<>nil;'); // fpc
  26010. //Add(' b:=obj.events[8]<>nil;'); // fpc
  26011. Add(' b:=obj.onfoo=vp;');
  26012. Add(' b:=obj.onbar=vp;');
  26013. //Add(' b:=obj.events[9]=vp;'); ToDo in pasresolver
  26014. Add(' b:=obj.onfoo=obj.onfoo;');
  26015. Add(' b:=obj.onbar=obj.onfoo;');
  26016. //Add(' b:=obj.events[10]=obj.onfoo;'); // ToDo in pasresolver
  26017. Add(' b:=obj.onfoo<>obj.onfoo;');
  26018. Add(' b:=obj.onbar<>obj.onfoo;');
  26019. //Add(' b:=obj.events[11]<>obj.onfoo;'); // ToDo in pasresolver
  26020. //Add(' b:[email protected];'); // fpc
  26021. //Add(' b:[email protected];'); // fpc
  26022. //Add(' b:=obj.events[12][email protected];'); // fpc
  26023. //Add(' b:=obj.onfoo<>@obj.doit;'); // fpc
  26024. //Add(' b:=obj.onbar<>@obj.doit;'); // fpc
  26025. //Add(' b:=obj.events[12]<>@obj.doit;'); // fpc
  26026. Add(' b:=Assigned(obj.onfoo);');
  26027. Add(' b:=Assigned(obj.onbar);');
  26028. Add(' b:=Assigned(obj.events[13]);');
  26029. ConvertProgram;
  26030. CheckSource('TestProcType_PropertyDelphi',
  26031. LinesToStr([ // statements
  26032. 'rtl.createClass(this, "TObject", null, function () {',
  26033. ' this.$init = function () {',
  26034. ' this.FOnFoo = null;',
  26035. ' };',
  26036. ' this.$final = function () {',
  26037. ' this.FOnFoo = undefined;',
  26038. ' };',
  26039. ' this.DoIt = function (vA) {',
  26040. ' var Result = 0;',
  26041. ' return Result;',
  26042. ' };',
  26043. 'this.GetFoo = function () {',
  26044. ' var Result = null;',
  26045. ' return Result;',
  26046. '};',
  26047. 'this.SetFoo = function (Value) {',
  26048. '};',
  26049. 'this.GetEvents = function (Index) {',
  26050. ' var Result = null;',
  26051. ' return Result;',
  26052. '};',
  26053. 'this.SetEvents = function (Index, Value) {',
  26054. '};',
  26055. '});',
  26056. 'this.Obj = null;',
  26057. 'this.vP = null;',
  26058. 'this.b = false;'
  26059. ]),
  26060. LinesToStr([
  26061. '$mod.Obj.FOnFoo = null;',
  26062. '$mod.Obj.SetFoo(null);',
  26063. '$mod.Obj.SetEvents(1, null);',
  26064. '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
  26065. '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
  26066. '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
  26067. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  26068. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  26069. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  26070. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  26071. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  26072. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  26073. '$mod.Obj.FOnFoo(1);',
  26074. '$mod.Obj.GetFoo();',
  26075. '$mod.Obj.FOnFoo(1);',
  26076. '$mod.Obj.GetFoo()(1);',
  26077. '$mod.Obj.GetEvents(6)(1);',
  26078. '$mod.b = $mod.Obj.FOnFoo(1) === $mod.vP(1);',
  26079. '$mod.b = $mod.Obj.GetFoo() === $mod.vP(1);',
  26080. '$mod.b = $mod.Obj.FOnFoo(1) === $mod.Obj.FOnFoo(1);',
  26081. '$mod.b = $mod.Obj.GetFoo() === $mod.Obj.FOnFoo(1);',
  26082. '$mod.b = $mod.Obj.FOnFoo(1) !== $mod.Obj.FOnFoo(1);',
  26083. '$mod.b = $mod.Obj.GetFoo() !== $mod.Obj.FOnFoo(1);',
  26084. '$mod.b = $mod.Obj.FOnFoo != null;',
  26085. '$mod.b = $mod.Obj.GetFoo() != null;',
  26086. '$mod.b = $mod.Obj.GetEvents(13) != null;',
  26087. '']));
  26088. end;
  26089. procedure TTestModule.TestProcType_WithClassInstDoPropertyFPC;
  26090. begin
  26091. StartProgram(false);
  26092. Add('type');
  26093. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  26094. Add(' TObject = class');
  26095. Add(' FOnFoo: TFuncInt;');
  26096. Add(' function DoIt(vA: longint = 1): longint;');
  26097. Add(' function GetFoo: TFuncInt;');
  26098. Add(' procedure SetFoo(const Value: TFuncInt);');
  26099. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  26100. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  26101. Add(' end;');
  26102. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  26103. Add('function tobject.getfoo: tfuncint; begin end;');
  26104. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  26105. Add('var');
  26106. Add(' Obj: TObject;');
  26107. Add(' vP: tfuncint;');
  26108. Add(' b: boolean;');
  26109. Add('begin');
  26110. Add('with obj do begin');
  26111. Add(' fonfoo:=nil;');
  26112. Add(' onfoo:=nil;');
  26113. Add(' onbar:=nil;');
  26114. Add(' fonfoo:=fonfoo;');
  26115. Add(' onfoo:=onfoo;');
  26116. Add(' onbar:=onbar;');
  26117. Add(' fonfoo:=@doit;');
  26118. Add(' onfoo:=@doit;');
  26119. Add(' onbar:=@doit;');
  26120. //Add(' fonfoo:=doit;'); // delphi
  26121. //Add(' onfoo:=doit;'); // delphi
  26122. //Add(' onbar:=doit;'); // delphi
  26123. Add(' fonfoo;');
  26124. Add(' onfoo;');
  26125. Add(' onbar;');
  26126. Add(' fonfoo();');
  26127. Add(' onfoo();');
  26128. Add(' onbar();');
  26129. Add(' b:=fonfoo=nil;');
  26130. Add(' b:=onfoo=nil;');
  26131. Add(' b:=onbar=nil;');
  26132. Add(' b:=fonfoo<>nil;');
  26133. Add(' b:=onfoo<>nil;');
  26134. Add(' b:=onbar<>nil;');
  26135. Add(' b:=fonfoo=vp;');
  26136. Add(' b:=onfoo=vp;');
  26137. Add(' b:=onbar=vp;');
  26138. Add(' b:=fonfoo=fonfoo;');
  26139. Add(' b:=onfoo=onfoo;');
  26140. Add(' b:=onbar=onfoo;');
  26141. Add(' b:=fonfoo<>fonfoo;');
  26142. Add(' b:=onfoo<>onfoo;');
  26143. Add(' b:=onbar<>onfoo;');
  26144. Add(' b:=fonfoo=@doit;');
  26145. Add(' b:=onfoo=@doit;');
  26146. Add(' b:=onbar=@doit;');
  26147. Add(' b:=fonfoo<>@doit;');
  26148. Add(' b:=onfoo<>@doit;');
  26149. Add(' b:=onbar<>@doit;');
  26150. Add(' b:=Assigned(fonfoo);');
  26151. Add(' b:=Assigned(onfoo);');
  26152. Add(' b:=Assigned(onbar);');
  26153. Add('end;');
  26154. ConvertProgram;
  26155. CheckSource('TestProcType_WithClassInstDoPropertyFPC',
  26156. LinesToStr([ // statements
  26157. 'rtl.createClass(this, "TObject", null, function () {',
  26158. ' this.$init = function () {',
  26159. ' this.FOnFoo = null;',
  26160. ' };',
  26161. ' this.$final = function () {',
  26162. ' this.FOnFoo = undefined;',
  26163. ' };',
  26164. ' this.DoIt = function (vA) {',
  26165. ' var Result = 0;',
  26166. ' return Result;',
  26167. ' };',
  26168. ' this.GetFoo = function () {',
  26169. ' var Result = null;',
  26170. ' return Result;',
  26171. ' };',
  26172. ' this.SetFoo = function (Value) {',
  26173. ' };',
  26174. '});',
  26175. 'this.Obj = null;',
  26176. 'this.vP = null;',
  26177. 'this.b = false;'
  26178. ]),
  26179. LinesToStr([
  26180. 'var $with = $mod.Obj;',
  26181. '$with.FOnFoo = null;',
  26182. '$with.FOnFoo = null;',
  26183. '$with.SetFoo(null);',
  26184. '$with.FOnFoo = $with.FOnFoo;',
  26185. '$with.FOnFoo = $with.FOnFoo;',
  26186. '$with.SetFoo($with.GetFoo());',
  26187. '$with.FOnFoo = rtl.createCallback($with, "DoIt");',
  26188. '$with.FOnFoo = rtl.createCallback($with, "DoIt");',
  26189. '$with.SetFoo(rtl.createCallback($with, "DoIt"));',
  26190. '$with.FOnFoo(1);',
  26191. '$with.FOnFoo(1);',
  26192. '$with.GetFoo();',
  26193. '$with.FOnFoo(1);',
  26194. '$with.FOnFoo(1);',
  26195. '$with.GetFoo()(1);',
  26196. '$mod.b = $with.FOnFoo === null;',
  26197. '$mod.b = $with.FOnFoo === null;',
  26198. '$mod.b = $with.GetFoo() === null;',
  26199. '$mod.b = $with.FOnFoo !== null;',
  26200. '$mod.b = $with.FOnFoo !== null;',
  26201. '$mod.b = $with.GetFoo() !== null;',
  26202. '$mod.b = rtl.eqCallback($with.FOnFoo, $mod.vP);',
  26203. '$mod.b = rtl.eqCallback($with.FOnFoo, $mod.vP);',
  26204. '$mod.b = rtl.eqCallback($with.GetFoo(), $mod.vP);',
  26205. '$mod.b = rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  26206. '$mod.b = rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  26207. '$mod.b = rtl.eqCallback($with.GetFoo(), $with.FOnFoo);',
  26208. '$mod.b = !rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  26209. '$mod.b = !rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  26210. '$mod.b = !rtl.eqCallback($with.GetFoo(), $with.FOnFoo);',
  26211. '$mod.b = rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  26212. '$mod.b = rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  26213. '$mod.b = rtl.eqCallback($with.GetFoo(), rtl.createCallback($with, "DoIt"));',
  26214. '$mod.b = !rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  26215. '$mod.b = !rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  26216. '$mod.b = !rtl.eqCallback($with.GetFoo(), rtl.createCallback($with, "DoIt"));',
  26217. '$mod.b = $with.FOnFoo != null;',
  26218. '$mod.b = $with.FOnFoo != null;',
  26219. '$mod.b = $with.GetFoo() != null;',
  26220. '']));
  26221. end;
  26222. procedure TTestModule.TestProcType_Nested;
  26223. begin
  26224. StartProgram(false);
  26225. Add([
  26226. 'type',
  26227. ' TProcInt = procedure(vI: longint = 1);',
  26228. 'procedure DoIt(vJ: longint);',
  26229. 'var aProc: TProcInt;',
  26230. ' b: boolean;',
  26231. ' procedure Sub(vK: longint);',
  26232. ' var aSub: TProcInt;',
  26233. ' procedure SubSub(vK: longint);',
  26234. ' var aSubSub: TProcInt;',
  26235. ' begin;',
  26236. ' aProc:=@DoIt;',
  26237. ' aSub:=@DoIt;',
  26238. ' aSubSub:=@DoIt;',
  26239. ' aProc:=@Sub;',
  26240. ' aSub:=@Sub;',
  26241. ' aSubSub:=@Sub;',
  26242. ' aProc:=@SubSub;',
  26243. ' aSub:=@SubSub;',
  26244. ' aSubSub:=@SubSub;',
  26245. ' end;',
  26246. ' begin;',
  26247. ' end;',
  26248. 'begin;',
  26249. ' aProc:=@Sub;',
  26250. ' b:=aProc=@Sub;',
  26251. ' b:=@Sub=aProc;',
  26252. 'end;',
  26253. 'begin',
  26254. '']);
  26255. ConvertProgram;
  26256. CheckSource('TestProcType_Nested',
  26257. LinesToStr([ // statements
  26258. 'this.DoIt = function (vJ) {',
  26259. ' var aProc = null;',
  26260. ' var b = false;',
  26261. ' function Sub(vK) {',
  26262. ' var aSub = null;',
  26263. ' function SubSub(vK) {',
  26264. ' var aSubSub = null;',
  26265. ' aProc = $mod.DoIt;',
  26266. ' aSub = $mod.DoIt;',
  26267. ' aSubSub = $mod.DoIt;',
  26268. ' aProc = Sub;',
  26269. ' aSub = Sub;',
  26270. ' aSubSub = Sub;',
  26271. ' aProc = SubSub;',
  26272. ' aSub = SubSub;',
  26273. ' aSubSub = SubSub;',
  26274. ' };',
  26275. ' };',
  26276. ' aProc = Sub;',
  26277. ' b = rtl.eqCallback(aProc, Sub);',
  26278. ' b = rtl.eqCallback(Sub, aProc);',
  26279. '};',
  26280. '']),
  26281. LinesToStr([ // $mod.$main
  26282. '']));
  26283. end;
  26284. procedure TTestModule.TestProcType_NestedOfObject;
  26285. begin
  26286. StartProgram(false);
  26287. Add([
  26288. 'type',
  26289. ' TProcInt = procedure(vI: longint = 1) of object;',
  26290. ' TObject = class',
  26291. ' procedure DoIt(vJ: longint);',
  26292. ' end;',
  26293. 'procedure TObject.DoIt(vJ: longint);',
  26294. 'var aProc: TProcInt;',
  26295. ' b: boolean;',
  26296. ' procedure Sub(vK: longint);',
  26297. ' var aSub: TProcInt;',
  26298. ' procedure SubSub(vK: longint);',
  26299. ' var aSubSub: TProcInt;',
  26300. ' begin;',
  26301. ' aProc:=@DoIt;',
  26302. ' aSub:=@DoIt;',
  26303. ' aSubSub:=@DoIt;',
  26304. ' aProc:=@Sub;',
  26305. ' aSub:=@Sub;',
  26306. ' aSubSub:=@Sub;',
  26307. ' aProc:=@SubSub;',
  26308. ' aSub:=@SubSub;',
  26309. ' aSubSub:=@SubSub;',
  26310. ' end;',
  26311. ' begin;',
  26312. ' end;',
  26313. 'begin;',
  26314. ' aProc:=@Sub;',
  26315. ' b:=aProc=@Sub;',
  26316. ' b:=@Sub=aProc;',
  26317. 'end;',
  26318. 'begin',
  26319. '']);
  26320. ConvertProgram;
  26321. CheckSource('TestProcType_Nested',
  26322. LinesToStr([ // statements
  26323. 'rtl.createClass(this, "TObject", null, function () {',
  26324. ' this.$init = function () {',
  26325. ' };',
  26326. ' this.$final = function () {',
  26327. ' };',
  26328. ' this.DoIt = function (vJ) {',
  26329. ' var $Self = this;',
  26330. ' var aProc = null;',
  26331. ' var b = false;',
  26332. ' function Sub(vK) {',
  26333. ' var aSub = null;',
  26334. ' function SubSub(vK) {',
  26335. ' var aSubSub = null;',
  26336. ' aProc = rtl.createCallback($Self, "DoIt");',
  26337. ' aSub = rtl.createCallback($Self, "DoIt");',
  26338. ' aSubSub = rtl.createCallback($Self, "DoIt");',
  26339. ' aProc = Sub;',
  26340. ' aSub = Sub;',
  26341. ' aSubSub = Sub;',
  26342. ' aProc = SubSub;',
  26343. ' aSub = SubSub;',
  26344. ' aSubSub = SubSub;',
  26345. ' };',
  26346. ' };',
  26347. ' aProc = Sub;',
  26348. ' b = rtl.eqCallback(aProc, Sub);',
  26349. ' b = rtl.eqCallback(Sub, aProc);',
  26350. ' };',
  26351. '});',
  26352. '']),
  26353. LinesToStr([ // $mod.$main
  26354. '']));
  26355. end;
  26356. procedure TTestModule.TestProcType_ReferenceToProc;
  26357. begin
  26358. StartProgram(false);
  26359. Add([
  26360. 'type',
  26361. ' TProcRef = reference to procedure(i: longint = 0);',
  26362. ' TFuncRef = reference to function(i: longint = 0): longint;',
  26363. 'var',
  26364. ' p: TProcRef;',
  26365. ' f: TFuncRef;',
  26366. 'procedure DoIt(i: longint);',
  26367. 'begin',
  26368. 'end;',
  26369. 'function GetIt(i: longint): longint;',
  26370. 'begin',
  26371. ' p:=@DoIt;',
  26372. ' f:=@GetIt;',
  26373. ' f;',
  26374. ' f();',
  26375. ' f(1);',
  26376. 'end;',
  26377. 'begin',
  26378. ' p:=@DoIt;',
  26379. ' f:=@GetIt;',
  26380. ' f;',
  26381. ' f();',
  26382. ' f(1);',
  26383. ' p:=TProcRef(f);',
  26384. '']);
  26385. ConvertProgram;
  26386. CheckSource('TestProcType_ReferenceToProc',
  26387. LinesToStr([ // statements
  26388. 'this.p = null;',
  26389. 'this.f = null;',
  26390. 'this.DoIt = function (i) {',
  26391. '};',
  26392. 'this.GetIt = function (i) {',
  26393. ' var Result = 0;',
  26394. ' $mod.p = $mod.DoIt;',
  26395. ' $mod.f = $mod.GetIt;',
  26396. ' $mod.f(0);',
  26397. ' $mod.f(0);',
  26398. ' $mod.f(1);',
  26399. ' return Result;',
  26400. '};',
  26401. '']),
  26402. LinesToStr([ // $mod.$main
  26403. '$mod.p = $mod.DoIt;',
  26404. '$mod.f = $mod.GetIt;',
  26405. '$mod.f(0);',
  26406. '$mod.f(0);',
  26407. '$mod.f(1);',
  26408. '$mod.p = $mod.f;',
  26409. '']));
  26410. end;
  26411. procedure TTestModule.TestProcType_ReferenceToMethod;
  26412. begin
  26413. StartProgram(false);
  26414. Add([
  26415. 'type',
  26416. ' TFuncRef = reference to function(i: longint = 5): longint;',
  26417. ' TObject = class',
  26418. ' function Grow(s: longint): longint;',
  26419. ' end;',
  26420. 'var',
  26421. ' f: tfuncref;',
  26422. 'function tobject.grow(s: longint): longint;',
  26423. ' function GrowSub(i: longint): longint;',
  26424. ' begin',
  26425. ' f:=@grow;',
  26426. ' f:=@growsub;',
  26427. ' end;',
  26428. 'begin',
  26429. ' f:=@grow;',
  26430. ' f:=@growsub;',
  26431. 'end;',
  26432. 'begin',
  26433. '']);
  26434. ConvertProgram;
  26435. CheckSource('TestProcType_ReferenceToMethod',
  26436. LinesToStr([ // statements
  26437. 'rtl.createClass(this, "TObject", null, function () {',
  26438. ' this.$init = function () {',
  26439. ' };',
  26440. ' this.$final = function () {',
  26441. ' };',
  26442. ' this.Grow = function (s) {',
  26443. ' var $Self = this;',
  26444. ' var Result = 0;',
  26445. ' function GrowSub(i) {',
  26446. ' var Result = 0;',
  26447. ' $mod.f = rtl.createCallback($Self, "Grow");',
  26448. ' $mod.f = GrowSub;',
  26449. ' return Result;',
  26450. ' };',
  26451. ' $mod.f = rtl.createCallback($Self, "Grow");',
  26452. ' $mod.f = GrowSub;',
  26453. ' return Result;',
  26454. ' };',
  26455. '});',
  26456. 'this.f = null;',
  26457. '']),
  26458. LinesToStr([ // $mod.$main
  26459. '']));
  26460. end;
  26461. procedure TTestModule.TestProcType_Typecast;
  26462. begin
  26463. StartProgram(false);
  26464. Add([
  26465. 'type',
  26466. ' TNotifyEvent = procedure(Sender: Pointer) of object;',
  26467. ' TEvent = procedure of object;',
  26468. ' TGetter = function:longint of object;',
  26469. ' TProcA = procedure(i: longint);',
  26470. ' TFuncB = function(i, j: longint): longint;',
  26471. 'procedure DoIt(); varargs; begin end;',
  26472. 'var',
  26473. ' Notify: tnotifyevent;',
  26474. ' Event: tevent;',
  26475. ' Getter: tgetter;',
  26476. ' ProcA: tproca;',
  26477. ' FuncB: tfuncb;',
  26478. ' p: pointer;',
  26479. 'begin',
  26480. ' notify:=tnotifyevent(event);',
  26481. ' event:=tevent(event);',
  26482. ' event:=tevent(notify);',
  26483. ' event:=tevent(getter);',
  26484. ' event:=tevent(proca);',
  26485. ' proca:=tproca(funcb);',
  26486. ' funcb:=tfuncb(funcb);',
  26487. ' funcb:=tfuncb(proca);',
  26488. ' funcb:=tfuncb(getter);',
  26489. ' proca:=tproca(p);',
  26490. ' funcb:=tfuncb(p);',
  26491. ' getter:=tgetter(p);',
  26492. ' p:=pointer(notify);',
  26493. ' p:=notify;',
  26494. ' p:=pointer(proca);',
  26495. ' p:=proca;',
  26496. ' p:=pointer(funcb);',
  26497. ' p:=funcb;',
  26498. ' doit(Pointer(notify),pointer(event),pointer(proca));',
  26499. '']);
  26500. ConvertProgram;
  26501. CheckSource('TestProcType_Typecast',
  26502. LinesToStr([ // statements
  26503. 'this.DoIt = function () {',
  26504. '};',
  26505. 'this.Notify = null;',
  26506. 'this.Event = null;',
  26507. 'this.Getter = null;',
  26508. 'this.ProcA = null;',
  26509. 'this.FuncB = null;',
  26510. 'this.p = null;',
  26511. '']),
  26512. LinesToStr([ // $mod.$main
  26513. '$mod.Notify = $mod.Event;',
  26514. '$mod.Event = $mod.Event;',
  26515. '$mod.Event = $mod.Notify;',
  26516. '$mod.Event = $mod.Getter;',
  26517. '$mod.Event = $mod.ProcA;',
  26518. '$mod.ProcA = $mod.FuncB;',
  26519. '$mod.FuncB = $mod.FuncB;',
  26520. '$mod.FuncB = $mod.ProcA;',
  26521. '$mod.FuncB = $mod.Getter;',
  26522. '$mod.ProcA = $mod.p;',
  26523. '$mod.FuncB = $mod.p;',
  26524. '$mod.Getter = $mod.p;',
  26525. '$mod.p = $mod.Notify;',
  26526. '$mod.p = $mod.Notify;',
  26527. '$mod.p = $mod.ProcA;',
  26528. '$mod.p = $mod.ProcA;',
  26529. '$mod.p = $mod.FuncB;',
  26530. '$mod.p = $mod.FuncB;',
  26531. '$mod.DoIt($mod.Notify, $mod.Event, $mod.ProcA);',
  26532. '']));
  26533. end;
  26534. procedure TTestModule.TestProcType_PassProcToUntyped;
  26535. begin
  26536. StartProgram(false);
  26537. Add([
  26538. 'type',
  26539. ' TEvent = procedure of object;',
  26540. ' TFunc = function: longint;',
  26541. 'procedure DoIt(); varargs; begin end;',
  26542. 'procedure DoSome(const a; var b; p: pointer); begin end;',
  26543. 'var',
  26544. ' Event: tevent;',
  26545. ' Func: TFunc;',
  26546. 'begin',
  26547. ' doit(event,func);',
  26548. ' dosome(event,event,event);',
  26549. ' dosome(func,func,func);',
  26550. '']);
  26551. ConvertProgram;
  26552. CheckSource('TestProcType_PassProcToUntyped',
  26553. LinesToStr([ // statements
  26554. 'this.DoIt = function () {',
  26555. '};',
  26556. 'this.DoSome = function (a, b, p) {',
  26557. '};',
  26558. 'this.Event = null;',
  26559. 'this.Func = null;',
  26560. '']),
  26561. LinesToStr([ // $mod.$main
  26562. '$mod.DoIt($mod.Event, $mod.Func);',
  26563. '$mod.DoSome($mod.Event, {',
  26564. ' p: $mod,',
  26565. ' get: function () {',
  26566. ' return this.p.Event;',
  26567. ' },',
  26568. ' set: function (v) {',
  26569. ' this.p.Event = v;',
  26570. ' }',
  26571. '}, $mod.Event);',
  26572. '$mod.DoSome($mod.Func, {',
  26573. ' p: $mod,',
  26574. ' get: function () {',
  26575. ' return this.p.Func;',
  26576. ' },',
  26577. ' set: function (v) {',
  26578. ' this.p.Func = v;',
  26579. ' }',
  26580. '}, $mod.Func);',
  26581. '']));
  26582. end;
  26583. procedure TTestModule.TestProcType_PassProcToArray;
  26584. begin
  26585. StartProgram(false);
  26586. Add([
  26587. 'type',
  26588. ' TFunc = function: longint;',
  26589. ' TArrFunc = array of TFunc;',
  26590. 'procedure DoIt(Arr: TArrFunc); begin end;',
  26591. 'function GetIt: longint; begin end;',
  26592. 'var',
  26593. ' Func: tfunc;',
  26594. 'begin',
  26595. ' doit([]);',
  26596. ' doit([@GetIt]);',
  26597. ' doit([Func]);',
  26598. '']);
  26599. ConvertProgram;
  26600. CheckSource('TestProcType_PassProcToArray',
  26601. LinesToStr([ // statements
  26602. 'this.DoIt = function (Arr) {',
  26603. '};',
  26604. 'this.GetIt = function () {',
  26605. ' var Result = 0;',
  26606. ' return Result;',
  26607. '};',
  26608. 'this.Func = null;',
  26609. '']),
  26610. LinesToStr([ // $mod.$main
  26611. '$mod.DoIt([]);',
  26612. '$mod.DoIt([$mod.GetIt]);',
  26613. '$mod.DoIt([$mod.Func]);',
  26614. '']));
  26615. end;
  26616. procedure TTestModule.TestProcType_SafeCallObjFPC;
  26617. begin
  26618. StartProgram(false);
  26619. Add([
  26620. '{$modeswitch externalclass}',
  26621. 'type',
  26622. ' TProc = reference to procedure(i: longint); safecall;',
  26623. ' TEvent = procedure(i: longint) of object; safecall;',
  26624. ' TExtA = class external name ''ExtObj''',
  26625. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  26626. ' procedure DoSome(Id: longint = 1);',
  26627. ' procedure SetOnClick(const e: TEvent);',
  26628. ' property OnClick: TEvent write SetOnClick;',
  26629. ' class procedure Fly(Id: longint = 1); static;',
  26630. ' procedure SetOnShow(const p: TProc);',
  26631. ' property OnShow: TProc write SetOnShow;',
  26632. ' end;',
  26633. 'procedure Run(i: longint = 1);',
  26634. 'begin',
  26635. 'end;',
  26636. 'var',
  26637. ' Obj: texta;',
  26638. ' e: TEvent;',
  26639. ' p: TProc;',
  26640. 'begin',
  26641. ' e:=e;',
  26642. ' e:[email protected];',
  26643. ' e:[email protected];',
  26644. ' e:=TEvent(@obj.dosome);', // no safecall
  26645. ' obj.OnClick:[email protected];',
  26646. ' obj.OnClick:[email protected];',
  26647. ' obj.setonclick(@obj.doit);',
  26648. ' obj.setonclick(@obj.dosome);',
  26649. ' p:=@Run;',
  26650. ' p:[email protected];',
  26651. ' obj.OnShow:=@Run;',
  26652. ' obj.OnShow:[email protected];',
  26653. ' obj.setOnShow(@Run);',
  26654. ' obj.setOnShow(@TExtA.Fly);',
  26655. ' with obj do begin',
  26656. ' e:=@doit;',
  26657. ' e:=@dosome;',
  26658. ' OnClick:=@doit;',
  26659. ' OnClick:=@dosome;',
  26660. ' setonclick(@doit);',
  26661. ' setonclick(@dosome);',
  26662. ' OnShow:=@Run;',
  26663. ' setOnShow(@Run);',
  26664. ' end;']);
  26665. ConvertProgram;
  26666. CheckSource('TestProcType_SafeCallObjFPC',
  26667. LinesToStr([ // statements
  26668. 'this.Run = function (i) {',
  26669. '};',
  26670. 'this.Obj = null;',
  26671. 'this.e = null;',
  26672. 'this.p = null;',
  26673. '']),
  26674. LinesToStr([ // $mod.$main
  26675. '$mod.e = $mod.e;',
  26676. '$mod.e = rtl.createSafeCallback($mod.Obj, "$Execute");',
  26677. '$mod.e = rtl.createSafeCallback($mod.Obj, "DoSome");',
  26678. '$mod.e = rtl.createCallback($mod.Obj, "DoSome");',
  26679. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  26680. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  26681. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  26682. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  26683. '$mod.p = rtl.createSafeCallback($mod, "Run");',
  26684. '$mod.p = rtl.createSafeCallback(ExtObj, "Fly");',
  26685. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26686. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  26687. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26688. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  26689. 'var $with = $mod.Obj;',
  26690. '$mod.e = rtl.createSafeCallback($with, "$Execute");',
  26691. '$mod.e = rtl.createSafeCallback($with, "DoSome");',
  26692. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  26693. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  26694. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  26695. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  26696. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26697. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26698. '']));
  26699. end;
  26700. procedure TTestModule.TestProcType_SafeCallDelphi;
  26701. begin
  26702. StartProgram(false);
  26703. Add([
  26704. '{$mode delphi}',
  26705. '{$modeswitch externalclass}',
  26706. 'type',
  26707. ' TProc = reference to procedure(i: longint); safecall;',
  26708. ' TEvent = procedure(i: longint) of object; safecall;',
  26709. ' TExtA = class external name ''ExtObj''',
  26710. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  26711. ' procedure DoSome(Id: longint = 1);',
  26712. ' procedure SetOnClick(const e: TEvent);',
  26713. ' property OnClick: TEvent write SetOnClick;',
  26714. ' class procedure Fly(Id: longint = 1); static;',
  26715. ' procedure SetOnShow(const p: TProc);',
  26716. ' property OnShow: TProc write SetOnShow;',
  26717. ' end;',
  26718. 'procedure Run(i: longint = 1);',
  26719. 'begin',
  26720. 'end;',
  26721. 'var',
  26722. ' Obj: texta;',
  26723. ' e: TEvent;',
  26724. ' p: TProc;',
  26725. 'begin',
  26726. ' e:=e;',
  26727. ' e:=obj.doit;',
  26728. ' e:=obj.dosome;',
  26729. ' e:=TEvent(@obj.dosome);', // no safecall
  26730. ' obj.OnClick:=obj.doit;',
  26731. ' obj.OnClick:=obj.dosome;',
  26732. ' obj.setonclick(obj.doit);',
  26733. ' obj.setonclick(obj.dosome);',
  26734. ' p:=Run;',
  26735. ' p:=TExtA.Fly;',
  26736. ' obj.OnShow:=Run;',
  26737. ' obj.OnShow:=TExtA.Fly;',
  26738. ' obj.setOnShow(Run);',
  26739. ' obj.setOnShow(TExtA.Fly);',
  26740. ' with obj do begin',
  26741. ' e:=doit;',
  26742. ' e:=dosome;',
  26743. ' OnClick:=doit;',
  26744. ' OnClick:=dosome;',
  26745. ' setonclick(doit);',
  26746. ' setonclick(dosome);',
  26747. ' OnShow:=@Run;',
  26748. ' setOnShow(@Run);',
  26749. ' end;']);
  26750. ConvertProgram;
  26751. CheckSource('TestProcType_SafeCallDelphi',
  26752. LinesToStr([ // statements
  26753. 'this.Run = function (i) {',
  26754. '};',
  26755. 'this.Obj = null;',
  26756. 'this.e = null;',
  26757. 'this.p = null;',
  26758. '']),
  26759. LinesToStr([ // $mod.$main
  26760. '$mod.e = $mod.e;',
  26761. '$mod.e = rtl.createSafeCallback($mod.Obj, "$Execute");',
  26762. '$mod.e = rtl.createSafeCallback($mod.Obj, "DoSome");',
  26763. '$mod.e = rtl.createCallback($mod.Obj, "DoSome");',
  26764. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  26765. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  26766. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  26767. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  26768. '$mod.p = rtl.createSafeCallback($mod, "Run");',
  26769. '$mod.p = rtl.createSafeCallback(ExtObj, "Fly");',
  26770. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26771. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  26772. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26773. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  26774. 'var $with = $mod.Obj;',
  26775. '$mod.e = rtl.createSafeCallback($with, "$Execute");',
  26776. '$mod.e = rtl.createSafeCallback($with, "DoSome");',
  26777. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  26778. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  26779. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  26780. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  26781. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26782. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26783. '']));
  26784. end;
  26785. procedure TTestModule.TestPointer;
  26786. begin
  26787. StartProgram(false);
  26788. Add(['type',
  26789. ' TObject = class end;',
  26790. ' TClass = class of TObject;',
  26791. ' TArrInt = array of longint;',
  26792. 'const',
  26793. ' n = nil;',
  26794. 'var',
  26795. ' v: jsvalue;',
  26796. ' Obj: tobject;',
  26797. ' C: tclass;',
  26798. ' a: tarrint;',
  26799. ' p: Pointer = nil;',
  26800. ' s: string;',
  26801. 'begin',
  26802. ' p:=p;',
  26803. ' p:=nil;',
  26804. ' if p=nil then;',
  26805. ' if nil=p then;',
  26806. ' if Assigned(p) then;',
  26807. ' p:=Pointer(v);',
  26808. ' p:=obj;',
  26809. ' p:=c;',
  26810. ' p:=a;',
  26811. ' p:=tobject;',
  26812. ' obj:=TObject(p);',
  26813. ' c:=TClass(p);',
  26814. ' a:=TArrInt(p);',
  26815. ' p:=n;',
  26816. ' p:=Pointer(a);',
  26817. ' p:=pointer(s);',
  26818. ' s:=string(p);',
  26819. '']);
  26820. ConvertProgram;
  26821. CheckSource('TestPointer',
  26822. LinesToStr([ // statements
  26823. 'rtl.createClass(this, "TObject", null, function () {',
  26824. ' this.$init = function () {',
  26825. ' };',
  26826. ' this.$final = function () {',
  26827. ' };',
  26828. '});',
  26829. 'this.n = null;',
  26830. 'this.v = undefined;',
  26831. 'this.Obj = null;',
  26832. 'this.C = null;',
  26833. 'this.a = [];',
  26834. 'this.p = null;',
  26835. 'this.s = "";',
  26836. '']),
  26837. LinesToStr([ // $mod.$main
  26838. '$mod.p = $mod.p;',
  26839. '$mod.p = null;',
  26840. 'if ($mod.p === null) ;',
  26841. 'if (null === $mod.p) ;',
  26842. 'if ($mod.p != null) ;',
  26843. '$mod.p = $mod.v;',
  26844. '$mod.p = $mod.Obj;',
  26845. '$mod.p = $mod.C;',
  26846. '$mod.p = $mod.a;',
  26847. '$mod.p = $mod.TObject;',
  26848. '$mod.Obj = $mod.p;',
  26849. '$mod.C = $mod.p;',
  26850. '$mod.a = $mod.p;',
  26851. '$mod.p = null;',
  26852. '$mod.p = $mod.a;',
  26853. '$mod.p = $mod.s;',
  26854. '$mod.s = $mod.p;',
  26855. '']));
  26856. end;
  26857. procedure TTestModule.TestPointer_Proc;
  26858. begin
  26859. StartProgram(false);
  26860. Add('type');
  26861. Add(' TObject = class');
  26862. Add(' procedure DoIt; virtual; abstract;');
  26863. Add(' end;');
  26864. Add('procedure DoSome; begin end;');
  26865. Add('var');
  26866. Add(' o: TObject;');
  26867. Add(' p: Pointer;');
  26868. Add('begin');
  26869. Add(' p:=@DoSome;');
  26870. Add(' p:[email protected];');
  26871. ConvertProgram;
  26872. CheckSource('TestPointer_Proc',
  26873. LinesToStr([ // statements
  26874. 'rtl.createClass(this, "TObject", null, function () {',
  26875. ' this.$init = function () {',
  26876. ' };',
  26877. ' this.$final = function () {',
  26878. ' };',
  26879. '});',
  26880. 'this.DoSome = function () {',
  26881. '};',
  26882. 'this.o = null;',
  26883. 'this.p = null;',
  26884. '']),
  26885. LinesToStr([ // $mod.$main
  26886. '$mod.p = $mod.DoSome;',
  26887. '$mod.p = rtl.createCallback($mod.o, "DoIt");',
  26888. '']));
  26889. end;
  26890. procedure TTestModule.TestPointer_AssignRecordFail;
  26891. begin
  26892. StartProgram(false);
  26893. Add('type');
  26894. Add(' TRec = record end;');
  26895. Add('var');
  26896. Add(' p: Pointer;');
  26897. Add(' r: TRec;');
  26898. Add('begin');
  26899. Add(' p:=r;');
  26900. SetExpectedPasResolverError('Incompatible types: got "TRec" expected "Pointer"',
  26901. nIncompatibleTypesGotExpected);
  26902. ConvertProgram;
  26903. end;
  26904. procedure TTestModule.TestPointer_AssignStaticArrayFail;
  26905. begin
  26906. StartProgram(false);
  26907. Add('type');
  26908. Add(' TArr = array[boolean] of longint;');
  26909. Add('var');
  26910. Add(' p: Pointer;');
  26911. Add(' a: TArr;');
  26912. Add('begin');
  26913. Add(' p:=a;');
  26914. SetExpectedPasResolverError('Incompatible types: got "TArr" expected "Pointer"',
  26915. nIncompatibleTypesGotExpected);
  26916. ConvertProgram;
  26917. end;
  26918. procedure TTestModule.TestPointer_TypeCastJSValueToPointer;
  26919. begin
  26920. StartProgram(false);
  26921. Add([
  26922. 'procedure DoIt(args: array of jsvalue); begin end;',
  26923. 'procedure DoAll; varargs; begin end;',
  26924. 'var',
  26925. ' v: jsvalue;',
  26926. 'begin',
  26927. ' DoIt([pointer(v)]);',
  26928. ' DoAll(pointer(v));',
  26929. '']);
  26930. ConvertProgram;
  26931. CheckSource('TestPointer_TypeCastJSValueToPointer',
  26932. LinesToStr([ // statements
  26933. 'this.DoIt = function (args) {',
  26934. '};',
  26935. 'this.DoAll = function () {',
  26936. '};',
  26937. 'this.v = undefined;',
  26938. '']),
  26939. LinesToStr([ // $mod.$main
  26940. '$mod.DoIt([$mod.v]);',
  26941. '$mod.DoAll($mod.v);',
  26942. '']));
  26943. end;
  26944. procedure TTestModule.TestPointer_NonRecordFail;
  26945. begin
  26946. StartProgram(false);
  26947. Add([
  26948. 'type',
  26949. ' p = ^longint;',
  26950. 'begin',
  26951. '']);
  26952. SetExpectedPasResolverError('Not supported: pointer of Longint',nNotSupportedX);
  26953. ConvertProgram;
  26954. end;
  26955. procedure TTestModule.TestPointer_AnonymousArgTypeFail;
  26956. begin
  26957. StartProgram(false);
  26958. Add([
  26959. 'procedure DoIt(p: ^longint); begin end;',
  26960. 'begin',
  26961. '']);
  26962. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  26963. ConvertProgram;
  26964. end;
  26965. procedure TTestModule.TestPointer_AnonymousVarTypeFail;
  26966. begin
  26967. StartProgram(false);
  26968. Add([
  26969. 'var p: ^longint;',
  26970. 'begin',
  26971. '']);
  26972. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  26973. ConvertProgram;
  26974. end;
  26975. procedure TTestModule.TestPointer_AnonymousResultTypeFail;
  26976. begin
  26977. StartProgram(false);
  26978. Add([
  26979. 'function DoIt: ^longint; begin end;',
  26980. 'begin',
  26981. '']);
  26982. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  26983. ConvertProgram;
  26984. end;
  26985. procedure TTestModule.TestPointer_AddrOperatorFail;
  26986. begin
  26987. StartProgram(false);
  26988. Add([
  26989. 'var i: longint;',
  26990. 'begin',
  26991. ' if @i=nil then ;',
  26992. '']);
  26993. SetExpectedConverterError('illegal qualifier "@" in front of "i:Longint"',nIllegalQualifierInFrontOf);
  26994. ConvertProgram;
  26995. end;
  26996. procedure TTestModule.TestPointer_ArrayParamsFail;
  26997. begin
  26998. StartProgram(false);
  26999. Add([
  27000. 'var',
  27001. ' p: Pointer;',
  27002. 'begin',
  27003. ' p:=p[1];',
  27004. '']);
  27005. SetExpectedPasResolverError('illegal qualifier "[" after "Pointer"',nIllegalQualifierAfter);
  27006. ConvertProgram;
  27007. end;
  27008. procedure TTestModule.TestPointer_PointerAddFail;
  27009. begin
  27010. StartProgram(false);
  27011. Add([
  27012. 'var',
  27013. ' p: Pointer;',
  27014. 'begin',
  27015. ' p:=p+1;',
  27016. '']);
  27017. SetExpectedPasResolverError('Operator is not overloaded: "Pointer" + "Longint"',nOperatorIsNotOverloadedAOpB);
  27018. ConvertProgram;
  27019. end;
  27020. procedure TTestModule.TestPointer_IncPointerFail;
  27021. begin
  27022. StartProgram(false);
  27023. Add([
  27024. 'var',
  27025. ' p: Pointer;',
  27026. 'begin',
  27027. ' inc(p,1);',
  27028. '']);
  27029. SetExpectedPasResolverError('Incompatible type arg no. 1: Got "Pointer", expected "integer"',
  27030. nIncompatibleTypeArgNo);
  27031. ConvertProgram;
  27032. end;
  27033. procedure TTestModule.TestPointer_Record;
  27034. begin
  27035. StartProgram(false);
  27036. Add([
  27037. 'type',
  27038. ' TRec = record x: longint; end;',
  27039. ' PRec = ^TRec;',
  27040. 'var',
  27041. ' r: TRec;',
  27042. ' p: PRec;',
  27043. ' q: ^TRec;',
  27044. ' Ptr: pointer;',
  27045. 'begin',
  27046. ' new(p);',
  27047. ' p:=@r;',
  27048. ' r:=p^;',
  27049. ' r.x:=p^.x;',
  27050. ' p^.x:=r.x;',
  27051. ' if p^.x=3 then ;',
  27052. ' if 4=p^.x then ;',
  27053. ' dispose(p);',
  27054. ' new(q);',
  27055. ' dispose(q);',
  27056. ' Ptr:=p;',
  27057. ' p:=PRec(ptr);',
  27058. '']);
  27059. ConvertProgram;
  27060. CheckSource('TestPointer_Record',
  27061. LinesToStr([ // statements
  27062. 'rtl.recNewT(this, "TRec", function () {',
  27063. ' this.x = 0;',
  27064. ' this.$eq = function (b) {',
  27065. ' return this.x === b.x;',
  27066. ' };',
  27067. ' this.$assign = function (s) {',
  27068. ' this.x = s.x;',
  27069. ' return this;',
  27070. ' };',
  27071. '});',
  27072. 'this.r = this.TRec.$new();',
  27073. 'this.p = null;',
  27074. 'this.q = null;',
  27075. 'this.Ptr = null;',
  27076. '']),
  27077. LinesToStr([ // $mod.$main
  27078. '$mod.p = $mod.TRec.$new();',
  27079. '$mod.p = $mod.r;',
  27080. '$mod.r.$assign($mod.p);',
  27081. '$mod.r.x = $mod.p.x;',
  27082. '$mod.p.x = $mod.r.x;',
  27083. 'if ($mod.p.x === 3) ;',
  27084. 'if (4 === $mod.p.x) ;',
  27085. '$mod.p = null;',
  27086. '$mod.q = $mod.TRec.$new();',
  27087. '$mod.q = null;',
  27088. '$mod.Ptr = $mod.p;',
  27089. '$mod.p = $mod.Ptr;',
  27090. '']));
  27091. end;
  27092. procedure TTestModule.TestPointer_RecordArg;
  27093. begin
  27094. StartProgram(false);
  27095. Add([
  27096. '{$modeswitch autoderef}',
  27097. 'type',
  27098. ' TRec = record x: longint; end;',
  27099. ' PRec = ^TRec;',
  27100. 'function DoIt(const a: PRec; var b: PRec; out c: PRec): TRec;',
  27101. 'begin',
  27102. ' a.x:=a.x;',
  27103. ' a^.x:=a^.x;',
  27104. ' with a^ do',
  27105. ' x:=x;',
  27106. 'end;',
  27107. 'function GetIt(p: PRec): PRec;',
  27108. 'begin',
  27109. ' p.x:=p.x;',
  27110. ' p^.x:=p^.x;',
  27111. ' with p^ do',
  27112. ' x:=x;',
  27113. 'end;',
  27114. 'var',
  27115. ' r: TRec;',
  27116. ' p: PRec;',
  27117. 'begin',
  27118. ' p:=GetIt(p);',
  27119. ' p^:=GetIt(@r)^;',
  27120. ' DoIt(p,p,p);',
  27121. ' DoIt(@r,p,p);',
  27122. '']);
  27123. ConvertProgram;
  27124. CheckSource('TestPointer_RecordArg',
  27125. LinesToStr([ // statements
  27126. 'rtl.recNewT(this, "TRec", function () {',
  27127. ' this.x = 0;',
  27128. ' this.$eq = function (b) {',
  27129. ' return this.x === b.x;',
  27130. ' };',
  27131. ' this.$assign = function (s) {',
  27132. ' this.x = s.x;',
  27133. ' return this;',
  27134. ' };',
  27135. '});',
  27136. 'this.DoIt = function (a, b, c) {',
  27137. ' var Result = $mod.TRec.$new();',
  27138. ' a.x = a.x;',
  27139. ' a.x = a.x;',
  27140. ' a.x = a.x;',
  27141. ' return Result;',
  27142. '};',
  27143. 'this.GetIt = function (p) {',
  27144. ' var Result = null;',
  27145. ' p.x = p.x;',
  27146. ' p.x = p.x;',
  27147. ' p.x = p.x;',
  27148. ' return Result;',
  27149. '};',
  27150. 'this.r = this.TRec.$new();',
  27151. 'this.p = null;',
  27152. '']),
  27153. LinesToStr([ // $mod.$main
  27154. '$mod.p = $mod.GetIt($mod.p);',
  27155. '$mod.p.$assign($mod.GetIt($mod.r));',
  27156. '$mod.DoIt($mod.p, {',
  27157. ' p: $mod,',
  27158. ' get: function () {',
  27159. ' return this.p.p;',
  27160. ' },',
  27161. ' set: function (v) {',
  27162. ' this.p.p = v;',
  27163. ' }',
  27164. '}, {',
  27165. ' p: $mod,',
  27166. ' get: function () {',
  27167. ' return this.p.p;',
  27168. ' },',
  27169. ' set: function (v) {',
  27170. ' this.p.p = v;',
  27171. ' }',
  27172. '});',
  27173. '$mod.DoIt($mod.r, {',
  27174. ' p: $mod,',
  27175. ' get: function () {',
  27176. ' return this.p.p;',
  27177. ' },',
  27178. ' set: function (v) {',
  27179. ' this.p.p = v;',
  27180. ' }',
  27181. '}, {',
  27182. ' p: $mod,',
  27183. ' get: function () {',
  27184. ' return this.p.p;',
  27185. ' },',
  27186. ' set: function (v) {',
  27187. ' this.p.p = v;',
  27188. ' }',
  27189. '});',
  27190. '']));
  27191. end;
  27192. procedure TTestModule.TestJSValue_AssignToJSValue;
  27193. begin
  27194. StartProgram(false);
  27195. Add('var');
  27196. Add(' v: jsvalue;');
  27197. Add(' i: longint;');
  27198. Add(' s: string;');
  27199. Add(' b: boolean;');
  27200. Add(' d: double;');
  27201. Add(' p: pointer;');
  27202. Add('begin');
  27203. Add(' v:=v;');
  27204. Add(' v:=1;');
  27205. Add(' v:=i;');
  27206. Add(' v:='''';');
  27207. Add(' v:=''c'';');
  27208. Add(' v:=''foo'';');
  27209. Add(' v:=s;');
  27210. Add(' v:=false;');
  27211. Add(' v:=true;');
  27212. Add(' v:=b;');
  27213. Add(' v:=0.1;');
  27214. Add(' v:=d;');
  27215. Add(' v:=nil;');
  27216. Add(' v:=p;');
  27217. ConvertProgram;
  27218. CheckSource('TestJSValue_AssignToJSValue',
  27219. LinesToStr([ // statements
  27220. 'this.v = undefined;',
  27221. 'this.i = 0;',
  27222. 'this.s = "";',
  27223. 'this.b = false;',
  27224. 'this.d = 0.0;',
  27225. 'this.p = null;',
  27226. '']),
  27227. LinesToStr([ // $mod.$main
  27228. '$mod.v = $mod.v;',
  27229. '$mod.v = 1;',
  27230. '$mod.v = $mod.i;',
  27231. '$mod.v = "";',
  27232. '$mod.v = "c";',
  27233. '$mod.v = "foo";',
  27234. '$mod.v = $mod.s;',
  27235. '$mod.v = false;',
  27236. '$mod.v = true;',
  27237. '$mod.v = $mod.b;',
  27238. '$mod.v = 0.1;',
  27239. '$mod.v = $mod.d;',
  27240. '$mod.v = null;',
  27241. '$mod.v = $mod.p;',
  27242. '']));
  27243. end;
  27244. procedure TTestModule.TestJSValue_TypeCastToBaseType;
  27245. begin
  27246. StartProgram(false);
  27247. Add('type');
  27248. Add(' integer = longint;');
  27249. Add(' TYesNo = boolean;');
  27250. Add(' TFloat = double;');
  27251. Add(' TCaption = string;');
  27252. Add(' TChar = char;');
  27253. Add('var');
  27254. Add(' v: jsvalue;');
  27255. Add(' i: integer;');
  27256. Add(' s: TCaption;');
  27257. Add(' b: TYesNo;');
  27258. Add(' d: TFloat;');
  27259. Add(' c: char;');
  27260. Add('begin');
  27261. Add(' i:=longint(v);');
  27262. Add(' i:=integer(v);');
  27263. Add(' s:=string(v);');
  27264. Add(' s:=TCaption(v);');
  27265. Add(' b:=boolean(v);');
  27266. Add(' b:=TYesNo(v);');
  27267. Add(' d:=double(v);');
  27268. Add(' d:=TFloat(v);');
  27269. Add(' c:=char(v);');
  27270. Add(' c:=TChar(v);');
  27271. ConvertProgram;
  27272. CheckSource('TestJSValue_TypeCastToBaseType',
  27273. LinesToStr([ // statements
  27274. 'this.v = undefined;',
  27275. 'this.i = 0;',
  27276. 'this.s = "";',
  27277. 'this.b = false;',
  27278. 'this.d = 0.0;',
  27279. 'this.c = "";',
  27280. '']),
  27281. LinesToStr([ // $mod.$main
  27282. '$mod.i = rtl.trunc($mod.v);',
  27283. '$mod.i = rtl.trunc($mod.v);',
  27284. '$mod.s = "" + $mod.v;',
  27285. '$mod.s = "" + $mod.v;',
  27286. '$mod.b = !($mod.v == false);',
  27287. '$mod.b = !($mod.v == false);',
  27288. '$mod.d = rtl.getNumber($mod.v);',
  27289. '$mod.d = rtl.getNumber($mod.v);',
  27290. '$mod.c = rtl.getChar($mod.v);',
  27291. '$mod.c = rtl.getChar($mod.v);',
  27292. '']));
  27293. end;
  27294. procedure TTestModule.TestJSValue_TypecastToJSValue;
  27295. begin
  27296. StartProgram(false);
  27297. Add([
  27298. 'type',
  27299. ' TArr = array of word;',
  27300. ' TRec = record end;',
  27301. ' TSet = set of boolean;',
  27302. 'procedure Fly(v: jsvalue);',
  27303. 'begin',
  27304. 'end;',
  27305. 'var',
  27306. ' a: TArr;',
  27307. ' r: TRec;',
  27308. ' s: TSet;',
  27309. 'begin',
  27310. ' Fly(jsvalue(a));',
  27311. ' Fly(jsvalue(r));',
  27312. ' Fly(jsvalue(s));',
  27313. '']);
  27314. ConvertProgram;
  27315. CheckSource('TestJSValue_TypecastToJSValue',
  27316. LinesToStr([ // statements
  27317. 'rtl.recNewT(this, "TRec", function () {',
  27318. ' this.$eq = function (b) {',
  27319. ' return true;',
  27320. ' };',
  27321. ' this.$assign = function (s) {',
  27322. ' return this;',
  27323. ' };',
  27324. '});',
  27325. 'this.Fly = function (v) {',
  27326. '};',
  27327. 'this.a = [];',
  27328. 'this.r = this.TRec.$new();',
  27329. 'this.s = {};',
  27330. '']),
  27331. LinesToStr([ // $mod.$main
  27332. '$mod.Fly($mod.a);',
  27333. '$mod.Fly($mod.r);',
  27334. '$mod.Fly($mod.s);',
  27335. '']));
  27336. end;
  27337. procedure TTestModule.TestJSValue_Equal;
  27338. begin
  27339. StartProgram(false);
  27340. Add('type');
  27341. Add(' integer = longint;');
  27342. Add(' TYesNo = boolean;');
  27343. Add(' TFloat = double;');
  27344. Add(' TCaption = string;');
  27345. Add(' TChar = char;');
  27346. Add(' TMulti = JSValue;');
  27347. Add('var');
  27348. Add(' v: jsvalue;');
  27349. Add(' i: integer;');
  27350. Add(' s: TCaption;');
  27351. Add(' b: TYesNo;');
  27352. Add(' d: TFloat;');
  27353. Add(' c: char;');
  27354. Add(' m: TMulti;');
  27355. Add('begin');
  27356. Add(' b:=v=v;');
  27357. Add(' b:=v<>v;');
  27358. Add(' b:=v=1;');
  27359. Add(' b:=v<>1;');
  27360. Add(' b:=2=v;');
  27361. Add(' b:=2<>v;');
  27362. Add(' b:=v=i;');
  27363. Add(' b:=i=v;');
  27364. Add(' b:=v=nil;');
  27365. Add(' b:=nil=v;');
  27366. Add(' b:=v=false;');
  27367. Add(' b:=true=v;');
  27368. Add(' b:=v=b;');
  27369. Add(' b:=b=v;');
  27370. Add(' b:=v=s;');
  27371. Add(' b:=s=v;');
  27372. Add(' b:=v=''foo'';');
  27373. Add(' b:=''''=v;');
  27374. Add(' b:=v=d;');
  27375. Add(' b:=d=v;');
  27376. Add(' b:=v=3.4;');
  27377. Add(' b:=5.6=v;');
  27378. Add(' b:=v=c;');
  27379. Add(' b:=c=v;');
  27380. Add(' b:=m=m;');
  27381. Add(' b:=v=m;');
  27382. Add(' b:=m=v;');
  27383. ConvertProgram;
  27384. CheckSource('TestJSValue_Equal',
  27385. LinesToStr([ // statements
  27386. 'this.v = undefined;',
  27387. 'this.i = 0;',
  27388. 'this.s = "";',
  27389. 'this.b = false;',
  27390. 'this.d = 0.0;',
  27391. 'this.c = "";',
  27392. 'this.m = undefined;',
  27393. '']),
  27394. LinesToStr([ // $mod.$main
  27395. '$mod.b = $mod.v == $mod.v;',
  27396. '$mod.b = $mod.v != $mod.v;',
  27397. '$mod.b = $mod.v == 1;',
  27398. '$mod.b = $mod.v != 1;',
  27399. '$mod.b = 2 == $mod.v;',
  27400. '$mod.b = 2 != $mod.v;',
  27401. '$mod.b = $mod.v == $mod.i;',
  27402. '$mod.b = $mod.i == $mod.v;',
  27403. '$mod.b = $mod.v == null;',
  27404. '$mod.b = null == $mod.v;',
  27405. '$mod.b = $mod.v == false;',
  27406. '$mod.b = true == $mod.v;',
  27407. '$mod.b = $mod.v == $mod.b;',
  27408. '$mod.b = $mod.b == $mod.v;',
  27409. '$mod.b = $mod.v == $mod.s;',
  27410. '$mod.b = $mod.s == $mod.v;',
  27411. '$mod.b = $mod.v == "foo";',
  27412. '$mod.b = "" == $mod.v;',
  27413. '$mod.b = $mod.v == $mod.d;',
  27414. '$mod.b = $mod.d == $mod.v;',
  27415. '$mod.b = $mod.v == 3.4;',
  27416. '$mod.b = 5.6 == $mod.v;',
  27417. '$mod.b = $mod.v == $mod.c;',
  27418. '$mod.b = $mod.c == $mod.v;',
  27419. '$mod.b = $mod.m == $mod.m;',
  27420. '$mod.b = $mod.v == $mod.m;',
  27421. '$mod.b = $mod.m == $mod.v;',
  27422. '']));
  27423. end;
  27424. procedure TTestModule.TestJSValue_If;
  27425. begin
  27426. StartProgram(false);
  27427. Add([
  27428. 'procedure Fly(var u);',
  27429. 'begin',
  27430. ' if jsvalue(u) then ;',
  27431. 'end;',
  27432. 'var',
  27433. ' v: jsvalue;',
  27434. 'begin',
  27435. ' if v then ;',
  27436. ' while v do ;',
  27437. ' repeat until v;',
  27438. '']);
  27439. ConvertProgram;
  27440. CheckSource('TestJSValue_If',
  27441. LinesToStr([ // statements
  27442. 'this.Fly = function (u) {',
  27443. ' if (u.get()) ;',
  27444. '};',
  27445. 'this.v = undefined;',
  27446. '']),
  27447. LinesToStr([ // $mod.$main
  27448. 'if ($mod.v) ;',
  27449. 'while($mod.v){',
  27450. '};',
  27451. 'do{',
  27452. '} while(!$mod.v);',
  27453. '']));
  27454. end;
  27455. procedure TTestModule.TestJSValue_Not;
  27456. begin
  27457. StartProgram(false);
  27458. Add([
  27459. 'var',
  27460. ' v: jsvalue;',
  27461. ' b: boolean;',
  27462. 'begin',
  27463. ' b:=not v;',
  27464. ' if not v then ;',
  27465. ' while not v do ;',
  27466. ' repeat until not v;',
  27467. '']);
  27468. ConvertProgram;
  27469. CheckSource('TestJSValue_If',
  27470. LinesToStr([ // statements
  27471. 'this.v = undefined;',
  27472. 'this.b = false;',
  27473. '']),
  27474. LinesToStr([ // $mod.$main
  27475. '$mod.b=!$mod.v;',
  27476. 'if (!$mod.v) ;',
  27477. 'while(!$mod.v){',
  27478. '};',
  27479. 'do{',
  27480. '} while($mod.v);',
  27481. '']));
  27482. end;
  27483. procedure TTestModule.TestJSValue_Enum;
  27484. begin
  27485. StartProgram(false);
  27486. Add('type');
  27487. Add(' TColor = (red, blue);');
  27488. Add(' TRedBlue = TColor;');
  27489. Add('var');
  27490. Add(' v: jsvalue;');
  27491. Add(' e: TColor;');
  27492. Add('begin');
  27493. Add(' v:=e;');
  27494. Add(' v:=TColor(e);');
  27495. Add(' v:=TRedBlue(e);');
  27496. Add(' e:=TColor(v);');
  27497. Add(' e:=TRedBlue(v);');
  27498. ConvertProgram;
  27499. CheckSource('TestJSValue_Enum',
  27500. LinesToStr([ // statements
  27501. 'this.TColor = {',
  27502. ' "0": "red",',
  27503. ' red: 0,',
  27504. ' "1": "blue",',
  27505. ' blue: 1',
  27506. '};',
  27507. 'this.v = undefined;',
  27508. 'this.e = 0;',
  27509. '']),
  27510. LinesToStr([ // $mod.$main
  27511. '$mod.v = $mod.e;',
  27512. '$mod.v = $mod.e;',
  27513. '$mod.v = $mod.e;',
  27514. '$mod.e = $mod.v;',
  27515. '$mod.e = $mod.v;',
  27516. '']));
  27517. end;
  27518. procedure TTestModule.TestJSValue_ClassInstance;
  27519. begin
  27520. StartProgram(false);
  27521. Add([
  27522. 'type',
  27523. ' TObject = class',
  27524. ' end;',
  27525. ' TBirdObject = TObject;',
  27526. 'var',
  27527. ' v: jsvalue;',
  27528. ' o: TObject;',
  27529. 'begin',
  27530. ' v:=o;',
  27531. ' v:=TObject(o);',
  27532. ' v:=TBirdObject(o);',
  27533. ' o:=TObject(v);',
  27534. ' o:=TBirdObject(v);',
  27535. ' if v is TObject then ;',
  27536. '']);
  27537. ConvertProgram;
  27538. CheckSource('TestJSValue_ClassInstance',
  27539. LinesToStr([ // statements
  27540. 'rtl.createClass(this, "TObject", null, function () {',
  27541. ' this.$init = function () {',
  27542. ' };',
  27543. ' this.$final = function () {',
  27544. ' };',
  27545. '});',
  27546. 'this.v = undefined;',
  27547. 'this.o = null;',
  27548. '']),
  27549. LinesToStr([ // $mod.$main
  27550. '$mod.v = $mod.o;',
  27551. '$mod.v = $mod.o;',
  27552. '$mod.v = $mod.o;',
  27553. '$mod.o = rtl.getObject($mod.v);',
  27554. '$mod.o = rtl.getObject($mod.v);',
  27555. 'if (rtl.isExt($mod.v, $mod.TObject, 1)) ;',
  27556. '']));
  27557. end;
  27558. procedure TTestModule.TestJSValue_ClassOf;
  27559. begin
  27560. StartProgram(false);
  27561. Add([
  27562. 'type',
  27563. ' TClass = class of TObject;',
  27564. ' TObject = class',
  27565. ' end;',
  27566. ' TBirds = class of TBird;',
  27567. ' TBird = class(TObject) end;',
  27568. 'var',
  27569. ' v: jsvalue;',
  27570. ' c: TClass;',
  27571. 'begin',
  27572. ' v:=c;',
  27573. ' v:=TObject;',
  27574. ' v:=TClass(c);',
  27575. ' v:=TBirds(c);',
  27576. ' c:=TClass(v);',
  27577. ' c:=TBirds(v);',
  27578. ' if v is TClass then ;',
  27579. '']);
  27580. ConvertProgram;
  27581. CheckSource('TestJSValue_ClassOf',
  27582. LinesToStr([ // statements
  27583. 'rtl.createClass(this, "TObject", null, function () {',
  27584. ' this.$init = function () {',
  27585. ' };',
  27586. ' this.$final = function () {',
  27587. ' };',
  27588. '});',
  27589. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  27590. '});',
  27591. 'this.v = undefined;',
  27592. 'this.c = null;',
  27593. '']),
  27594. LinesToStr([ // $mod.$main
  27595. '$mod.v = $mod.c;',
  27596. '$mod.v = $mod.TObject;',
  27597. '$mod.v = $mod.c;',
  27598. '$mod.v = $mod.c;',
  27599. '$mod.c = rtl.getObject($mod.v);',
  27600. '$mod.c = rtl.getObject($mod.v);',
  27601. 'if (rtl.isExt($mod.v, $mod.TObject, 2)) ;',
  27602. '']));
  27603. end;
  27604. procedure TTestModule.TestJSValue_ArrayOfJSValue;
  27605. begin
  27606. StartProgram(false);
  27607. Add([
  27608. 'type',
  27609. ' integer = longint;',
  27610. ' TArray = array of JSValue;',
  27611. ' TArrgh = tarray;',
  27612. ' TArrInt = array of integer;',
  27613. 'var',
  27614. ' v: jsvalue;',
  27615. ' TheArray: tarray = (1,''2'');',
  27616. ' Arr: tarrgh;',
  27617. ' i: integer;',
  27618. ' ArrInt: tarrint;',
  27619. 'begin',
  27620. ' arr:=thearray;',
  27621. ' thearray:=arr;',
  27622. ' setlength(arr,2);',
  27623. ' setlength(thearray,3);',
  27624. ' arr[4]:=v;',
  27625. ' arr[5]:=length(thearray);',
  27626. ' arr[6]:=nil;',
  27627. ' arr[7]:=thearray[8];',
  27628. ' arr[low(arr)]:=high(thearray);',
  27629. ' arr:=arrint;',
  27630. ' arrInt:=tarrint(arr);',
  27631. ' if TheArray = nil then ;',
  27632. ' if nil = TheArray then ;',
  27633. ' if TheArray <> nil then ;',
  27634. ' if nil <> TheArray then ;',
  27635. '']);
  27636. ConvertProgram;
  27637. CheckSource('TestJSValue_ArrayOfJSValue',
  27638. LinesToStr([ // statements
  27639. 'this.v = undefined;',
  27640. 'this.TheArray = [1, "2"];',
  27641. 'this.Arr = [];',
  27642. 'this.i = 0;',
  27643. 'this.ArrInt = [];',
  27644. '']),
  27645. LinesToStr([ // $mod.$main
  27646. '$mod.Arr = rtl.arrayRef($mod.TheArray);',
  27647. '$mod.TheArray = rtl.arrayRef($mod.Arr);',
  27648. '$mod.Arr = rtl.arraySetLength($mod.Arr,undefined,2);',
  27649. '$mod.TheArray = rtl.arraySetLength($mod.TheArray,undefined,3);',
  27650. '$mod.Arr[4] = $mod.v;',
  27651. '$mod.Arr[5] = rtl.length($mod.TheArray);',
  27652. '$mod.Arr[6] = null;',
  27653. '$mod.Arr[7] = $mod.TheArray[8];',
  27654. '$mod.Arr[0] = rtl.length($mod.TheArray) - 1;',
  27655. '$mod.Arr = rtl.arrayRef($mod.ArrInt);',
  27656. '$mod.ArrInt = $mod.Arr;',
  27657. 'if (rtl.length($mod.TheArray) === 0) ;',
  27658. 'if (rtl.length($mod.TheArray) === 0) ;',
  27659. 'if (rtl.length($mod.TheArray) > 0) ;',
  27660. 'if (rtl.length($mod.TheArray) > 0) ;',
  27661. '']));
  27662. end;
  27663. procedure TTestModule.TestJSValue_ArrayLit;
  27664. begin
  27665. StartProgram(false);
  27666. Add([
  27667. 'type',
  27668. ' TFlag = (big,small);',
  27669. ' TArray = array of JSValue;',
  27670. ' TObject = class end;',
  27671. ' TClass = class of TObject;',
  27672. 'var',
  27673. ' v: jsvalue;',
  27674. ' a: TArray;',
  27675. ' o: TObject;',
  27676. 'begin',
  27677. ' a:=[];',
  27678. ' a:=[1];',
  27679. ' a:=[1,2];',
  27680. ' a:=[big];',
  27681. ' a:=[1,big];',
  27682. ' a:=[o,nil];',
  27683. '']);
  27684. ConvertProgram;
  27685. CheckSource('TestJSValue_ArrayLit',
  27686. LinesToStr([ // statements
  27687. 'this.TFlag = {',
  27688. ' "0": "big",',
  27689. ' big: 0,',
  27690. ' "1": "small",',
  27691. ' small: 1',
  27692. '};',
  27693. 'rtl.createClass(this, "TObject", null, function () {',
  27694. ' this.$init = function () {',
  27695. ' };',
  27696. ' this.$final = function () {',
  27697. ' };',
  27698. '});',
  27699. 'this.v = undefined;',
  27700. 'this.a = [];',
  27701. 'this.o = null;',
  27702. '']),
  27703. LinesToStr([ // $mod.$main
  27704. '$mod.a = [];',
  27705. '$mod.a = [1];',
  27706. '$mod.a = [1, 2];',
  27707. '$mod.a = [$mod.TFlag.big];',
  27708. '$mod.a = [1, $mod.TFlag.big];',
  27709. '$mod.a = [$mod.o, null];',
  27710. '']));
  27711. end;
  27712. procedure TTestModule.TestJSValue_Params;
  27713. begin
  27714. StartProgram(false);
  27715. Add('type');
  27716. Add(' integer = longint;');
  27717. Add(' TYesNo = boolean;');
  27718. Add(' TFloat = double;');
  27719. Add(' TCaption = string;');
  27720. Add(' TChar = char;');
  27721. Add('function DoIt(a: jsvalue; const b: jsvalue; var c: jsvalue; out d: jsvalue): jsvalue;');
  27722. Add('var');
  27723. Add(' l: jsvalue;');
  27724. Add('begin');
  27725. Add(' a:=a;');
  27726. Add(' l:=b;');
  27727. Add(' c:=c;');
  27728. Add(' d:=d;');
  27729. Add(' Result:=l;');
  27730. Add('end;');
  27731. Add('function DoSome(a: jsvalue; const b: jsvalue): jsvalue; begin end;');
  27732. Add('var');
  27733. Add(' v: jsvalue;');
  27734. Add(' i: integer;');
  27735. Add(' b: TYesNo;');
  27736. Add(' d: TFloat;');
  27737. Add(' s: TCaption;');
  27738. Add(' c: TChar;');
  27739. Add('begin');
  27740. Add(' v:=doit(v,v,v,v);');
  27741. Add(' i:=integer(dosome(i,i));');
  27742. Add(' b:=TYesNo(dosome(b,b));');
  27743. Add(' d:=TFloat(dosome(d,d));');
  27744. Add(' s:=TCaption(dosome(s,s));');
  27745. Add(' c:=TChar(dosome(c,c));');
  27746. ConvertProgram;
  27747. CheckSource('TestJSValue_Params',
  27748. LinesToStr([ // statements
  27749. 'this.DoIt = function (a, b, c, d) {',
  27750. ' var Result = undefined;',
  27751. ' var l = undefined;',
  27752. ' a = a;',
  27753. ' l = b;',
  27754. ' c.set(c.get());',
  27755. ' d.set(d.get());',
  27756. ' Result = l;',
  27757. ' return Result;',
  27758. '};',
  27759. 'this.DoSome = function (a, b) {',
  27760. ' var Result = undefined;',
  27761. ' return Result;',
  27762. '};',
  27763. 'this.v = undefined;',
  27764. 'this.i = 0;',
  27765. 'this.b = false;',
  27766. 'this.d = 0.0;',
  27767. 'this.s = "";',
  27768. 'this.c = "";',
  27769. '']),
  27770. LinesToStr([ // $mod.$main
  27771. '$mod.v = $mod.DoIt($mod.v, $mod.v, {',
  27772. ' p: $mod,',
  27773. ' get: function () {',
  27774. ' return this.p.v;',
  27775. ' },',
  27776. ' set: function (v) {',
  27777. ' this.p.v = v;',
  27778. ' }',
  27779. '}, {',
  27780. ' p: $mod,',
  27781. ' get: function () {',
  27782. ' return this.p.v;',
  27783. ' },',
  27784. ' set: function (v) {',
  27785. ' this.p.v = v;',
  27786. ' }',
  27787. '});',
  27788. '$mod.i = rtl.trunc($mod.DoSome($mod.i, $mod.i));',
  27789. '$mod.b = !($mod.DoSome($mod.b, $mod.b) == false);',
  27790. '$mod.d = rtl.getNumber($mod.DoSome($mod.d, $mod.d));',
  27791. '$mod.s = "" + $mod.DoSome($mod.s, $mod.s);',
  27792. '$mod.c = rtl.getChar($mod.DoSome($mod.c, $mod.c));',
  27793. '']));
  27794. end;
  27795. procedure TTestModule.TestJSValue_UntypedParam;
  27796. begin
  27797. StartProgram(false);
  27798. Add('function DoIt(const a; var b; out c): jsvalue;');
  27799. Add('begin');
  27800. Add(' Result:=a;');
  27801. Add(' Result:=b;');
  27802. Add(' Result:=c;');
  27803. Add(' b:=Result;');
  27804. Add(' c:=Result;');
  27805. Add('end;');
  27806. Add('var i: longint;');
  27807. Add('begin');
  27808. Add(' doit(i,i,i);');
  27809. ConvertProgram;
  27810. CheckSource('TestJSValue_UntypedParam',
  27811. LinesToStr([ // statements
  27812. 'this.DoIt = function (a, b, c) {',
  27813. ' var Result = undefined;',
  27814. ' Result = a;',
  27815. ' Result = b.get();',
  27816. ' Result = c.get();',
  27817. ' b.set(Result);',
  27818. ' c.set(Result);',
  27819. ' return Result;',
  27820. '};',
  27821. 'this.i = 0;',
  27822. '']),
  27823. LinesToStr([ // $mod.$main
  27824. '$mod.DoIt($mod.i, {',
  27825. ' p: $mod,',
  27826. ' get: function () {',
  27827. ' return this.p.i;',
  27828. ' },',
  27829. ' set: function (v) {',
  27830. ' this.p.i = v;',
  27831. ' }',
  27832. '}, {',
  27833. ' p: $mod,',
  27834. ' get: function () {',
  27835. ' return this.p.i;',
  27836. ' },',
  27837. ' set: function (v) {',
  27838. ' this.p.i = v;',
  27839. ' }',
  27840. '});',
  27841. '']));
  27842. end;
  27843. procedure TTestModule.TestJSValue_FuncResultType;
  27844. begin
  27845. StartProgram(false);
  27846. Add('type');
  27847. Add(' integer = longint;');
  27848. Add(' TJSValueArray = array of JSValue;');
  27849. Add(' TListSortCompare = function(Item1, Item2: JSValue): Integer;');
  27850. Add('procedure Sort(P: JSValue; aList: TJSValueArray; const Compare: TListSortCompare);');
  27851. Add('begin');
  27852. Add(' while Compare(P,aList[0])>0 do ;');
  27853. Add('end;');
  27854. Add('var');
  27855. Add(' Compare: TListSortCompare;');
  27856. Add(' V: JSValue;');
  27857. Add(' i: integer;');
  27858. Add('begin');
  27859. Add(' if Compare(V,V)>0 then ;');
  27860. Add(' if Compare(i,i)>1 then ;');
  27861. Add(' if Compare(nil,false)>2 then ;');
  27862. Add(' if Compare(1,true)>3 then ;');
  27863. ConvertProgram;
  27864. CheckSource('TestJSValue_UntypedParam',
  27865. LinesToStr([ // statements
  27866. 'this.Sort = function (P, aList, Compare) {',
  27867. ' while (Compare(P, aList[0]) > 0) {',
  27868. ' };',
  27869. '};',
  27870. 'this.Compare = null;',
  27871. 'this.V = undefined;',
  27872. 'this.i = 0;',
  27873. '']),
  27874. LinesToStr([ // $mod.$main
  27875. 'if ($mod.Compare($mod.V, $mod.V) > 0) ;',
  27876. 'if ($mod.Compare($mod.i, $mod.i) > 1) ;',
  27877. 'if ($mod.Compare(null, false) > 2) ;',
  27878. 'if ($mod.Compare(1, true) > 3) ;',
  27879. '']));
  27880. end;
  27881. procedure TTestModule.TestJSValue_ProcType_Assign;
  27882. begin
  27883. StartProgram(false);
  27884. Add('type');
  27885. Add(' integer = longint;');
  27886. Add(' TObject = class');
  27887. Add(' class function GetGlob: integer;');
  27888. Add(' function Getter: integer;');
  27889. Add(' end;');
  27890. Add('class function TObject.GetGlob: integer;');
  27891. Add('var v1: jsvalue;');
  27892. Add('begin');
  27893. Add(' v1:=@GetGlob;');
  27894. Add(' v1:[email protected];');
  27895. Add('end;');
  27896. Add('function TObject.Getter: integer;');
  27897. Add('var v2: jsvalue;');
  27898. Add('begin');
  27899. Add(' v2:=@Getter;');
  27900. Add(' v2:[email protected];');
  27901. Add(' v2:=@GetGlob;');
  27902. Add(' v2:[email protected];');
  27903. Add('end;');
  27904. Add('function GetIt(i: integer): integer;');
  27905. Add('var v3: jsvalue;');
  27906. Add('begin');
  27907. Add(' v3:=@GetIt;');
  27908. Add('end;');
  27909. Add('var');
  27910. Add(' V: JSValue;');
  27911. Add(' o: TObject;');
  27912. Add('begin');
  27913. Add(' v:=@GetIt;');
  27914. Add(' v:[email protected];');
  27915. Add(' v:[email protected];');
  27916. ConvertProgram;
  27917. CheckSource('TestJSValue_ProcType_Assign',
  27918. LinesToStr([ // statements
  27919. 'rtl.createClass(this, "TObject", null, function () {',
  27920. ' this.$init = function () {',
  27921. ' };',
  27922. ' this.$final = function () {',
  27923. ' };',
  27924. ' this.GetGlob = function () {',
  27925. ' var Result = 0;',
  27926. ' var v1 = undefined;',
  27927. ' v1 = rtl.createCallback(this, "GetGlob");',
  27928. ' v1 = rtl.createCallback(this, "GetGlob");',
  27929. ' return Result;',
  27930. ' };',
  27931. ' this.Getter = function () {',
  27932. ' var Result = 0;',
  27933. ' var v2 = undefined;',
  27934. ' v2 = rtl.createCallback(this, "Getter");',
  27935. ' v2 = rtl.createCallback(this, "Getter");',
  27936. ' v2 = rtl.createCallback(this.$class, "GetGlob");',
  27937. ' v2 = rtl.createCallback(this.$class, "GetGlob");',
  27938. ' return Result;',
  27939. ' };',
  27940. '});',
  27941. 'this.GetIt = function (i) {',
  27942. ' var Result = 0;',
  27943. ' var v3 = undefined;',
  27944. ' v3 = $mod.GetIt;',
  27945. ' return Result;',
  27946. '};',
  27947. 'this.V = undefined;',
  27948. 'this.o = null;',
  27949. '']),
  27950. LinesToStr([ // $mod.$main
  27951. '$mod.V = $mod.GetIt;',
  27952. '$mod.V = rtl.createCallback($mod.o, "Getter");',
  27953. '$mod.V = rtl.createCallback($mod.o.$class, "GetGlob");',
  27954. '']));
  27955. end;
  27956. procedure TTestModule.TestJSValue_ProcType_Equal;
  27957. begin
  27958. StartProgram(false);
  27959. Add('type');
  27960. Add(' integer = longint;');
  27961. Add(' TObject = class');
  27962. Add(' class function GetGlob: integer;');
  27963. Add(' function Getter: integer;');
  27964. Add(' end;');
  27965. Add('class function TObject.GetGlob: integer;');
  27966. Add('var v1: jsvalue;');
  27967. Add('begin');
  27968. Add(' if v1=@GetGlob then;');
  27969. Add(' if [email protected] then ;');
  27970. Add('end;');
  27971. Add('function TObject.Getter: integer;');
  27972. Add('var v2: jsvalue;');
  27973. Add('begin');
  27974. Add(' if v2=@Getter then;');
  27975. Add(' if [email protected] then ;');
  27976. Add(' if v2=@GetGlob then;');
  27977. Add(' if [email protected] then;');
  27978. Add('end;');
  27979. Add('function GetIt(i: integer): integer;');
  27980. Add('var v3: jsvalue;');
  27981. Add('begin');
  27982. Add(' if v3=@GetIt then;');
  27983. Add('end;');
  27984. Add('var');
  27985. Add(' V: JSValue;');
  27986. Add(' o: TObject;');
  27987. Add('begin');
  27988. Add(' if v=@GetIt then;');
  27989. Add(' if [email protected] then;');
  27990. Add(' if [email protected] then;');
  27991. Add(' if @GetIt=v then;');
  27992. Add(' if @o.Getter=v then;');
  27993. Add(' if @o.GetGlob=v then;');
  27994. ConvertProgram;
  27995. CheckSource('TestJSValue_ProcType_Equal',
  27996. LinesToStr([ // statements
  27997. 'rtl.createClass(this, "TObject", null, function () {',
  27998. ' this.$init = function () {',
  27999. ' };',
  28000. ' this.$final = function () {',
  28001. ' };',
  28002. ' this.GetGlob = function () {',
  28003. ' var Result = 0;',
  28004. ' var v1 = undefined;',
  28005. ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
  28006. ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
  28007. ' return Result;',
  28008. ' };',
  28009. ' this.Getter = function () {',
  28010. ' var Result = 0;',
  28011. ' var v2 = undefined;',
  28012. ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
  28013. ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
  28014. ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
  28015. ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
  28016. ' return Result;',
  28017. ' };',
  28018. '});',
  28019. 'this.GetIt = function (i) {',
  28020. ' var Result = 0;',
  28021. ' var v3 = undefined;',
  28022. ' if (rtl.eqCallback(v3, $mod.GetIt)) ;',
  28023. ' return Result;',
  28024. '};',
  28025. 'this.V = undefined;',
  28026. 'this.o = null;',
  28027. '']),
  28028. LinesToStr([ // $mod.$main
  28029. 'if (rtl.eqCallback($mod.V, $mod.GetIt)) ;',
  28030. 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o, "Getter"))) ;',
  28031. 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o.$class, "GetGlob"))) ;',
  28032. 'if (rtl.eqCallback($mod.GetIt, $mod.V)) ;',
  28033. 'if (rtl.eqCallback(rtl.createCallback($mod.o, "Getter"), $mod.V)) ;',
  28034. 'if (rtl.eqCallback(rtl.createCallback($mod.o.$class, "GetGlob"), $mod.V)) ;',
  28035. '']));
  28036. end;
  28037. procedure TTestModule.TestJSValue_ProcType_Param;
  28038. begin
  28039. StartProgram(false);
  28040. Add([
  28041. 'type',
  28042. ' variant = jsvalue;',
  28043. ' TArrVariant = array of variant;',
  28044. ' TArrVar2 = TArrVariant;',
  28045. ' TFuncInt = function: longint;',
  28046. 'function GetIt: longint;',
  28047. 'begin',
  28048. 'end;',
  28049. 'procedure DoIt(p: jsvalue; Arr: TArrVar2);',
  28050. 'var v: variant;',
  28051. 'begin',
  28052. ' v:=arr[1];',
  28053. 'end;',
  28054. 'var s: string;',
  28055. 'begin',
  28056. ' DoIt(GetIt,[]);',
  28057. ' DoIt(@GetIt,[]);',
  28058. ' DoIt(1,[s,GetIt]);',
  28059. ' DoIt(1,[s,@GetIt]);',
  28060. '']);
  28061. ConvertProgram;
  28062. CheckSource('TestJSValue_ProcType_Param',
  28063. LinesToStr([ // statements
  28064. 'this.GetIt = function () {',
  28065. ' var Result = 0;',
  28066. ' return Result;',
  28067. '};',
  28068. 'this.DoIt = function (p, Arr) {',
  28069. ' var v = undefined;',
  28070. ' v = Arr[1];',
  28071. '};',
  28072. 'this.s = "";',
  28073. '']),
  28074. LinesToStr([ // $mod.$main
  28075. '$mod.DoIt($mod.GetIt(), []);',
  28076. '$mod.DoIt($mod.GetIt, []);',
  28077. '$mod.DoIt(1, [$mod.s, $mod.GetIt()]);',
  28078. '$mod.DoIt(1, [$mod.s, $mod.GetIt]);',
  28079. '']));
  28080. end;
  28081. procedure TTestModule.TestJSValue_AssignToPointerFail;
  28082. begin
  28083. StartProgram(false);
  28084. Add([
  28085. 'var',
  28086. ' v: JSValue;',
  28087. ' p: Pointer;',
  28088. 'begin',
  28089. ' p:=v;',
  28090. '']);
  28091. SetExpectedPasResolverError('Incompatible types: got "JSValue" expected "Pointer"',
  28092. nIncompatibleTypesGotExpected);
  28093. ConvertProgram;
  28094. end;
  28095. procedure TTestModule.TestJSValue_OverloadDouble;
  28096. begin
  28097. StartProgram(false);
  28098. Add([
  28099. 'type',
  28100. ' integer = longint;',
  28101. ' tdatetime = double;',
  28102. 'procedure DoIt(d: double); begin end;',
  28103. 'procedure DoIt(v: jsvalue); begin end;',
  28104. 'var',
  28105. ' d: double;',
  28106. ' dt: tdatetime;',
  28107. ' i: integer;',
  28108. ' b: byte;',
  28109. ' shi: shortint;',
  28110. ' w: word;',
  28111. ' smi: smallint;',
  28112. ' lw: longword;',
  28113. ' li: longint;',
  28114. ' ni: nativeint;',
  28115. ' nu: nativeuint;',
  28116. 'begin',
  28117. ' DoIt(d);',
  28118. ' DoIt(dt);',
  28119. ' DoIt(i);',
  28120. ' DoIt(b);',
  28121. ' DoIt(shi);',
  28122. ' DoIt(w);',
  28123. ' DoIt(smi);',
  28124. ' DoIt(lw);',
  28125. ' DoIt(li);',
  28126. ' DoIt(ni);',
  28127. ' DoIt(nu);',
  28128. '']);
  28129. ConvertProgram;
  28130. CheckSource('TestJSValue_OverloadDouble',
  28131. LinesToStr([ // statements
  28132. 'this.DoIt = function (d) {',
  28133. '};',
  28134. 'this.DoIt$1 = function (v) {',
  28135. '};',
  28136. 'this.d = 0.0;',
  28137. 'this.dt = 0.0;',
  28138. 'this.i = 0;',
  28139. 'this.b = 0;',
  28140. 'this.shi = 0;',
  28141. 'this.w = 0;',
  28142. 'this.smi = 0;',
  28143. 'this.lw = 0;',
  28144. 'this.li = 0;',
  28145. 'this.ni = 0;',
  28146. 'this.nu = 0;',
  28147. '']),
  28148. LinesToStr([ // $mod.$main
  28149. '$mod.DoIt($mod.d);',
  28150. '$mod.DoIt($mod.dt);',
  28151. '$mod.DoIt$1($mod.i);',
  28152. '$mod.DoIt$1($mod.b);',
  28153. '$mod.DoIt$1($mod.shi);',
  28154. '$mod.DoIt$1($mod.w);',
  28155. '$mod.DoIt$1($mod.smi);',
  28156. '$mod.DoIt$1($mod.lw);',
  28157. '$mod.DoIt$1($mod.li);',
  28158. '$mod.DoIt$1($mod.ni);',
  28159. '$mod.DoIt$1($mod.nu);',
  28160. '']));
  28161. end;
  28162. procedure TTestModule.TestJSValue_OverloadNativeInt;
  28163. begin
  28164. StartProgram(false);
  28165. Add([
  28166. 'type',
  28167. ' integer = longint;',
  28168. ' int53 = nativeint;',
  28169. ' tdatetime = double;',
  28170. 'procedure DoIt(n: nativeint); begin end;',
  28171. 'procedure DoIt(v: jsvalue); begin end;',
  28172. 'var',
  28173. ' d: double;',
  28174. ' dt: tdatetime;',
  28175. ' i: integer;',
  28176. ' b: byte;',
  28177. ' shi: shortint;',
  28178. ' w: word;',
  28179. ' smi: smallint;',
  28180. ' lw: longword;',
  28181. ' li: longint;',
  28182. ' ni: nativeint;',
  28183. ' nu: nativeuint;',
  28184. 'begin',
  28185. ' DoIt(d);',
  28186. ' DoIt(dt);',
  28187. ' DoIt(i);',
  28188. ' DoIt(b);',
  28189. ' DoIt(shi);',
  28190. ' DoIt(w);',
  28191. ' DoIt(smi);',
  28192. ' DoIt(lw);',
  28193. ' DoIt(li);',
  28194. ' DoIt(ni);',
  28195. ' DoIt(nu);',
  28196. '']);
  28197. ConvertProgram;
  28198. CheckSource('TestJSValue_OverloadNativeInt',
  28199. LinesToStr([ // statements
  28200. 'this.DoIt = function (n) {',
  28201. '};',
  28202. 'this.DoIt$1 = function (v) {',
  28203. '};',
  28204. 'this.d = 0.0;',
  28205. 'this.dt = 0.0;',
  28206. 'this.i = 0;',
  28207. 'this.b = 0;',
  28208. 'this.shi = 0;',
  28209. 'this.w = 0;',
  28210. 'this.smi = 0;',
  28211. 'this.lw = 0;',
  28212. 'this.li = 0;',
  28213. 'this.ni = 0;',
  28214. 'this.nu = 0;',
  28215. '']),
  28216. LinesToStr([ // $mod.$main
  28217. '$mod.DoIt$1($mod.d);',
  28218. '$mod.DoIt$1($mod.dt);',
  28219. '$mod.DoIt($mod.i);',
  28220. '$mod.DoIt($mod.b);',
  28221. '$mod.DoIt($mod.shi);',
  28222. '$mod.DoIt($mod.w);',
  28223. '$mod.DoIt($mod.smi);',
  28224. '$mod.DoIt($mod.lw);',
  28225. '$mod.DoIt($mod.li);',
  28226. '$mod.DoIt($mod.ni);',
  28227. '$mod.DoIt($mod.nu);',
  28228. '']));
  28229. end;
  28230. procedure TTestModule.TestJSValue_OverloadWord;
  28231. begin
  28232. StartProgram(false);
  28233. Add([
  28234. 'type',
  28235. ' integer = longint;',
  28236. ' int53 = nativeint;',
  28237. ' tdatetime = double;',
  28238. 'procedure DoIt(w: word); begin end;',
  28239. 'procedure DoIt(v: jsvalue); begin end;',
  28240. 'var',
  28241. ' d: double;',
  28242. ' dt: tdatetime;',
  28243. ' i: integer;',
  28244. ' b: byte;',
  28245. ' shi: shortint;',
  28246. ' w: word;',
  28247. ' smi: smallint;',
  28248. ' lw: longword;',
  28249. ' li: longint;',
  28250. ' ni: nativeint;',
  28251. ' nu: nativeuint;',
  28252. 'begin',
  28253. ' DoIt(d);',
  28254. ' DoIt(dt);',
  28255. ' DoIt(i);',
  28256. ' DoIt(b);',
  28257. ' DoIt(shi);',
  28258. ' DoIt(w);',
  28259. ' DoIt(smi);',
  28260. ' DoIt(lw);',
  28261. ' DoIt(li);',
  28262. ' DoIt(ni);',
  28263. ' DoIt(nu);',
  28264. '']);
  28265. ConvertProgram;
  28266. CheckSource('TestJSValue_OverloadWord',
  28267. LinesToStr([ // statements
  28268. 'this.DoIt = function (w) {',
  28269. '};',
  28270. 'this.DoIt$1 = function (v) {',
  28271. '};',
  28272. 'this.d = 0.0;',
  28273. 'this.dt = 0.0;',
  28274. 'this.i = 0;',
  28275. 'this.b = 0;',
  28276. 'this.shi = 0;',
  28277. 'this.w = 0;',
  28278. 'this.smi = 0;',
  28279. 'this.lw = 0;',
  28280. 'this.li = 0;',
  28281. 'this.ni = 0;',
  28282. 'this.nu = 0;',
  28283. '']),
  28284. LinesToStr([ // $mod.$main
  28285. '$mod.DoIt$1($mod.d);',
  28286. '$mod.DoIt$1($mod.dt);',
  28287. '$mod.DoIt$1($mod.i);',
  28288. '$mod.DoIt($mod.b);',
  28289. '$mod.DoIt($mod.shi);',
  28290. '$mod.DoIt($mod.w);',
  28291. '$mod.DoIt$1($mod.smi);',
  28292. '$mod.DoIt$1($mod.lw);',
  28293. '$mod.DoIt$1($mod.li);',
  28294. '$mod.DoIt$1($mod.ni);',
  28295. '$mod.DoIt$1($mod.nu);',
  28296. '']));
  28297. end;
  28298. procedure TTestModule.TestJSValue_OverloadString;
  28299. begin
  28300. StartProgram(false);
  28301. Add([
  28302. 'type',
  28303. ' uni = string;',
  28304. ' WChar = char;',
  28305. 'procedure DoIt(s: string); begin end;',
  28306. 'procedure DoIt(v: jsvalue); begin end;',
  28307. 'var',
  28308. ' s: string;',
  28309. ' c: char;',
  28310. ' u: uni;',
  28311. 'begin',
  28312. ' DoIt(s);',
  28313. ' DoIt(c);',
  28314. ' DoIt(u);',
  28315. '']);
  28316. ConvertProgram;
  28317. CheckSource('TestJSValue_OverloadString',
  28318. LinesToStr([ // statements
  28319. 'this.DoIt = function (s) {',
  28320. '};',
  28321. 'this.DoIt$1 = function (v) {',
  28322. '};',
  28323. 'this.s = "";',
  28324. 'this.c = "";',
  28325. 'this.u = "";',
  28326. '']),
  28327. LinesToStr([ // $mod.$main
  28328. '$mod.DoIt($mod.s);',
  28329. '$mod.DoIt($mod.c);',
  28330. '$mod.DoIt($mod.u);',
  28331. '']));
  28332. end;
  28333. procedure TTestModule.TestJSValue_OverloadChar;
  28334. begin
  28335. StartProgram(false);
  28336. Add([
  28337. 'type',
  28338. ' uni = string;',
  28339. ' WChar = char;',
  28340. 'procedure DoIt(c: char); begin end;',
  28341. 'procedure DoIt(v: jsvalue); begin end;',
  28342. 'var',
  28343. ' s: string;',
  28344. ' c: char;',
  28345. ' u: uni;',
  28346. 'begin',
  28347. ' DoIt(s);',
  28348. ' DoIt(c);',
  28349. ' DoIt(u);',
  28350. '']);
  28351. ConvertProgram;
  28352. CheckSource('TestJSValue_OverloadChar',
  28353. LinesToStr([ // statements
  28354. 'this.DoIt = function (c) {',
  28355. '};',
  28356. 'this.DoIt$1 = function (v) {',
  28357. '};',
  28358. 'this.s = "";',
  28359. 'this.c = "";',
  28360. 'this.u = "";',
  28361. '']),
  28362. LinesToStr([ // $mod.$main
  28363. '$mod.DoIt$1($mod.s);',
  28364. '$mod.DoIt($mod.c);',
  28365. '$mod.DoIt$1($mod.u);',
  28366. '']));
  28367. end;
  28368. procedure TTestModule.TestJSValue_OverloadPointer;
  28369. begin
  28370. StartProgram(false);
  28371. Add([
  28372. 'type',
  28373. ' TObject = class end;',
  28374. 'procedure DoIt(p: pointer); begin end;',
  28375. 'procedure DoIt(v: jsvalue); begin end;',
  28376. 'var',
  28377. ' o: TObject;',
  28378. 'begin',
  28379. ' DoIt(o);',
  28380. '']);
  28381. ConvertProgram;
  28382. CheckSource('TestJSValue_OverloadPointer',
  28383. LinesToStr([ // statements
  28384. 'rtl.createClass(this, "TObject", null, function () {',
  28385. ' this.$init = function () {',
  28386. ' };',
  28387. ' this.$final = function () {',
  28388. ' };',
  28389. '});',
  28390. 'this.DoIt = function (p) {',
  28391. '};',
  28392. 'this.DoIt$1 = function (v) {',
  28393. '};',
  28394. 'this.o = null;',
  28395. '']),
  28396. LinesToStr([ // $mod.$main
  28397. '$mod.DoIt($mod.o);',
  28398. '']));
  28399. end;
  28400. procedure TTestModule.TestJSValue_ForIn;
  28401. begin
  28402. StartProgram(false);
  28403. Add([
  28404. 'var',
  28405. ' v: JSValue;',
  28406. ' key: string;',
  28407. 'begin',
  28408. ' for key in v do begin',
  28409. ' if key=''abc'' then ;',
  28410. ' end;',
  28411. '']);
  28412. ConvertProgram;
  28413. CheckSource('TestJSValue_ForIn',
  28414. LinesToStr([ // statements
  28415. 'this.v = undefined;',
  28416. 'this.key = "";',
  28417. '']),
  28418. LinesToStr([ // $mod.$main
  28419. 'for ($mod.key in $mod.v) {',
  28420. ' if ($mod.key === "abc") ;',
  28421. '};',
  28422. '']));
  28423. end;
  28424. procedure TTestModule.TestRTTI_IntRange;
  28425. begin
  28426. WithTypeInfo:=true;
  28427. StartProgram(true,[supTypeInfo]);
  28428. Add([
  28429. '{$modeswitch externalclass}',
  28430. 'type',
  28431. ' TGraphicsColor = -$7FFFFFFF-1..$7FFFFFFF;',
  28432. ' TColor = type TGraphicsColor;',
  28433. 'var',
  28434. ' p: TTypeInfo;',
  28435. ' k: TTypeKind;',
  28436. 'begin',
  28437. ' p:=typeinfo(TGraphicsColor);',
  28438. ' p:=typeinfo(TColor);',
  28439. ' k:=GetTypeKind(TGraphicsColor);',
  28440. ' k:=GetTypeKind(TColor);',
  28441. '']);
  28442. ConvertProgram;
  28443. CheckSource('TestRTTI_IntRange',
  28444. LinesToStr([ // statements
  28445. 'this.$rtti.$Int("TGraphicsColor", {',
  28446. ' minvalue: -2147483648,',
  28447. ' maxvalue: 2147483647,',
  28448. ' ordtype: 4',
  28449. '});',
  28450. 'this.$rtti.$inherited("TColor", this.$rtti["TGraphicsColor"], {});',
  28451. 'this.p = null;',
  28452. 'this.k = 0;',
  28453. '']),
  28454. LinesToStr([ // $mod.$main
  28455. '$mod.p = $mod.$rtti["TGraphicsColor"];',
  28456. '$mod.p = $mod.$rtti["TColor"];',
  28457. '$mod.k = 1;',
  28458. '$mod.k = 1;',
  28459. '']));
  28460. end;
  28461. procedure TTestModule.TestRTTI_Double;
  28462. begin
  28463. WithTypeInfo:=true;
  28464. StartProgram(true,[supTypeInfo]);
  28465. Add([
  28466. '{$modeswitch externalclass}',
  28467. 'type',
  28468. ' TFloat = type double;',
  28469. 'var',
  28470. ' p: TTypeInfo;',
  28471. 'begin',
  28472. ' p:=typeinfo(double);',
  28473. ' p:=typeinfo(TFloat);',
  28474. '']);
  28475. ConvertProgram;
  28476. CheckSource('TestRTTI_Double',
  28477. LinesToStr([ // statements
  28478. 'this.$rtti.$inherited("TFloat", rtl.double, {});',
  28479. 'this.p = null;',
  28480. '']),
  28481. LinesToStr([ // $mod.$main
  28482. '$mod.p = rtl.double;',
  28483. '$mod.p = $mod.$rtti["TFloat"];',
  28484. '']));
  28485. end;
  28486. procedure TTestModule.TestRTTI_ProcType;
  28487. begin
  28488. WithTypeInfo:=true;
  28489. StartProgram(false);
  28490. Add('type');
  28491. Add(' TProcA = procedure;');
  28492. Add(' TMethodB = procedure of object;');
  28493. Add(' TProcC = procedure; varargs;');
  28494. Add(' TProcD = procedure(i: longint; const j: string; var c: char; out d: double);');
  28495. Add(' TProcE = function: nativeint;');
  28496. Add(' TProcF = function(const p: TProcA): nativeuint;');
  28497. Add('var p: pointer;');
  28498. Add('begin');
  28499. Add(' p:=typeinfo(tproca);');
  28500. ConvertProgram;
  28501. CheckSource('TestRTTI_ProcType',
  28502. LinesToStr([ // statements
  28503. 'this.$rtti.$ProcVar("TProcA", {',
  28504. ' procsig: rtl.newTIProcSig(null)',
  28505. '});',
  28506. 'this.$rtti.$MethodVar("TMethodB", {',
  28507. ' procsig: rtl.newTIProcSig(null),',
  28508. ' methodkind: 0',
  28509. '});',
  28510. 'this.$rtti.$ProcVar("TProcC", {',
  28511. ' procsig: rtl.newTIProcSig(null, 2)',
  28512. '});',
  28513. 'this.$rtti.$ProcVar("TProcD", {',
  28514. ' procsig: rtl.newTIProcSig([["i", rtl.longint], ["j", rtl.string, 2], ["c", rtl.char, 1], ["d", rtl.double, 4]])',
  28515. '});',
  28516. 'this.$rtti.$ProcVar("TProcE", {',
  28517. ' procsig: rtl.newTIProcSig(null, rtl.nativeint)',
  28518. '});',
  28519. 'this.$rtti.$ProcVar("TProcF", {',
  28520. ' procsig: rtl.newTIProcSig([["p", this.$rtti["TProcA"], 2]], rtl.nativeuint)',
  28521. '});',
  28522. 'this.p = null;',
  28523. '']),
  28524. LinesToStr([ // $mod.$main
  28525. '$mod.p = $mod.$rtti["TProcA"];',
  28526. '']));
  28527. end;
  28528. procedure TTestModule.TestRTTI_ProcType_ArgFromOtherUnit;
  28529. begin
  28530. WithTypeInfo:=true;
  28531. AddModuleWithIntfImplSrc('unit2.pas',
  28532. LinesToStr([
  28533. 'type',
  28534. ' TObject = class end;'
  28535. ]),
  28536. '');
  28537. StartUnit(true);
  28538. Add('interface');
  28539. Add('uses unit2;');
  28540. Add('type');
  28541. Add(' TProcA = function(o: tobject): tobject;');
  28542. Add('implementation');
  28543. Add('type');
  28544. Add(' TProcB = function(o: tobject): tobject;');
  28545. Add('var p: Pointer;');
  28546. Add('initialization');
  28547. Add(' p:=typeinfo(tproca);');
  28548. Add(' p:=typeinfo(tprocb);');
  28549. ConvertUnit;
  28550. CheckSource('TestRTTI_ProcType_ArgFromOtherUnit',
  28551. LinesToStr([ // statements
  28552. 'var $impl = $mod.$impl;',
  28553. 'this.$rtti.$ProcVar("TProcA", {',
  28554. ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
  28555. '});',
  28556. '']),
  28557. LinesToStr([ // this.$init
  28558. '$impl.p = $mod.$rtti["TProcA"];',
  28559. '$impl.p = $mod.$rtti["TProcB"];',
  28560. '']),
  28561. LinesToStr([ // implementation
  28562. '$mod.$rtti.$ProcVar("TProcB", {',
  28563. ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
  28564. '});',
  28565. '$impl.p = null;',
  28566. '']) );
  28567. end;
  28568. procedure TTestModule.TestRTTI_EnumAndSetType;
  28569. begin
  28570. WithTypeInfo:=true;
  28571. StartProgram(false);
  28572. Add('type');
  28573. Add(' TFlag = (light,dark);');
  28574. Add(' TFlags = set of TFlag;');
  28575. Add(' TProc = function(f: TFlags): TFlag;');
  28576. Add('var p: pointer;');
  28577. Add('begin');
  28578. Add(' p:=typeinfo(tflag);');
  28579. Add(' p:=typeinfo(tflags);');
  28580. ConvertProgram;
  28581. CheckSource('TestRTTI_EnumAndType',
  28582. LinesToStr([ // statements
  28583. 'this.TFlag = {',
  28584. ' "0": "light",',
  28585. ' light: 0,',
  28586. ' "1": "dark",',
  28587. ' dark: 1',
  28588. '};',
  28589. 'this.$rtti.$Enum("TFlag", {',
  28590. ' minvalue: 0,',
  28591. ' maxvalue: 1,',
  28592. ' ordtype: 1,',
  28593. ' enumtype: this.TFlag',
  28594. '});',
  28595. 'this.$rtti.$Set("TFlags", {',
  28596. ' comptype: this.$rtti["TFlag"]',
  28597. '});',
  28598. 'this.$rtti.$ProcVar("TProc", {',
  28599. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TFlags"]]], this.$rtti["TFlag"])',
  28600. '});',
  28601. 'this.p = null;',
  28602. '']),
  28603. LinesToStr([ // $mod.$main
  28604. '$mod.p = $mod.$rtti["TFlag"];',
  28605. '$mod.p = $mod.$rtti["TFlags"];',
  28606. '']));
  28607. end;
  28608. procedure TTestModule.TestRTTI_EnumRange;
  28609. begin
  28610. WithTypeInfo:=true;
  28611. StartProgram(false);
  28612. Add([
  28613. 'type',
  28614. ' TCol = (red,green,blue);',
  28615. ' TColRg = green..blue;',
  28616. ' TSetOfColRg = set of TColRg;',
  28617. 'var p: pointer;',
  28618. 'begin',
  28619. ' p:=typeinfo(tcolrg);',
  28620. ' p:=typeinfo(tsetofcolrg);',
  28621. '']);
  28622. ConvertProgram;
  28623. end;
  28624. procedure TTestModule.TestRTTI_AnonymousEnumType;
  28625. begin
  28626. WithTypeInfo:=true;
  28627. StartProgram(false);
  28628. Add('type');
  28629. Add(' TFlags = set of (red, green);');
  28630. Add('var');
  28631. Add(' f: TFlags;');
  28632. Add('begin');
  28633. Add(' Include(f,red);');
  28634. ConvertProgram;
  28635. CheckSource('TestRTTI_AnonymousEnumType',
  28636. LinesToStr([ // statements
  28637. 'this.TFlags$a = {',
  28638. ' "0": "red",',
  28639. ' red: 0,',
  28640. ' "1": "green",',
  28641. ' green: 1',
  28642. '};',
  28643. 'this.$rtti.$Enum("TFlags$a", {',
  28644. ' minvalue: 0,',
  28645. ' maxvalue: 1,',
  28646. ' ordtype: 1,',
  28647. ' enumtype: this.TFlags$a',
  28648. '});',
  28649. 'this.$rtti.$Set("TFlags", {',
  28650. ' comptype: this.$rtti["TFlags$a"]',
  28651. '});',
  28652. 'this.f = {};',
  28653. '']),
  28654. LinesToStr([
  28655. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  28656. '']));
  28657. end;
  28658. procedure TTestModule.TestRTTI_StaticArray;
  28659. begin
  28660. WithTypeInfo:=true;
  28661. StartProgram(false);
  28662. Add('type');
  28663. Add(' TFlag = (light,dark);');
  28664. Add(' TFlagNames = array[TFlag] of string;');
  28665. Add(' TBoolNames = array[boolean] of string;');
  28666. Add(' TByteArray = array[1..32768] of byte;');
  28667. Add(' TProc = function(f: TBoolNames): TFlagNames;');
  28668. Add('var p: pointer;');
  28669. Add('begin');
  28670. Add(' p:=typeinfo(TFlagNames);');
  28671. Add(' p:=typeinfo(TBoolNames);');
  28672. ConvertProgram;
  28673. CheckSource('TestRTTI_StaticArray',
  28674. LinesToStr([ // statements
  28675. 'this.TFlag = {',
  28676. ' "0": "light",',
  28677. ' light: 0,',
  28678. ' "1": "dark",',
  28679. ' dark: 1',
  28680. '};',
  28681. 'this.$rtti.$Enum("TFlag", {',
  28682. ' minvalue: 0,',
  28683. ' maxvalue: 1,',
  28684. ' ordtype: 1,',
  28685. ' enumtype: this.TFlag',
  28686. '});',
  28687. 'this.$rtti.$StaticArray("TFlagNames", {',
  28688. ' dims: [2],',
  28689. ' eltype: rtl.string',
  28690. '});',
  28691. 'this.$rtti.$StaticArray("TBoolNames", {',
  28692. ' dims: [2],',
  28693. ' eltype: rtl.string',
  28694. '});',
  28695. 'this.$rtti.$StaticArray("TByteArray", {',
  28696. ' dims: [32768],',
  28697. ' eltype: rtl.byte',
  28698. '});',
  28699. 'this.$rtti.$ProcVar("TProc", {',
  28700. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TBoolNames"]]], this.$rtti["TFlagNames"])',
  28701. '});',
  28702. 'this.p = null;',
  28703. '']),
  28704. LinesToStr([ // $mod.$main
  28705. '$mod.p = $mod.$rtti["TFlagNames"];',
  28706. '$mod.p = $mod.$rtti["TBoolNames"];',
  28707. '']));
  28708. end;
  28709. procedure TTestModule.TestRTTI_DynArray;
  28710. begin
  28711. WithTypeInfo:=true;
  28712. StartProgram(false);
  28713. Add('type');
  28714. Add(' TArrStr = array of string;');
  28715. Add(' TArr2Dim = array of tarrstr;');
  28716. Add(' TProc = function(f: TArrStr): TArr2Dim;');
  28717. Add('var p: pointer;');
  28718. Add('begin');
  28719. Add(' p:=typeinfo(tarrstr);');
  28720. Add(' p:=typeinfo(tarr2dim);');
  28721. ConvertProgram;
  28722. CheckSource('TestRTTI_DynArray',
  28723. LinesToStr([ // statements
  28724. 'this.$rtti.$DynArray("TArrStr", {',
  28725. ' eltype: rtl.string',
  28726. '});',
  28727. 'this.$rtti.$DynArray("TArr2Dim", {',
  28728. ' eltype: this.$rtti["TArrStr"]',
  28729. '});',
  28730. 'this.$rtti.$ProcVar("TProc", {',
  28731. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TArrStr"]]], this.$rtti["TArr2Dim"])',
  28732. '});',
  28733. 'this.p = null;',
  28734. '']),
  28735. LinesToStr([ // $mod.$main
  28736. '$mod.p = $mod.$rtti["TArrStr"];',
  28737. '$mod.p = $mod.$rtti["TArr2Dim"];',
  28738. '']));
  28739. end;
  28740. procedure TTestModule.TestRTTI_ArrayNestedAnonymous;
  28741. begin
  28742. WithTypeInfo:=true;
  28743. StartProgram(false);
  28744. Add('type');
  28745. Add(' TArr = array of array of longint;');
  28746. Add('var a: TArr;');
  28747. Add('begin');
  28748. ConvertProgram;
  28749. CheckSource('TestRTTI_ArrayNestedAnonymous',
  28750. LinesToStr([ // statements
  28751. 'this.$rtti.$DynArray("TArr$a", {',
  28752. ' eltype: rtl.longint',
  28753. '});',
  28754. 'this.$rtti.$DynArray("TArr", {',
  28755. ' eltype: this.$rtti["TArr$a"]',
  28756. '});',
  28757. 'this.a = [];',
  28758. '']),
  28759. LinesToStr([ // $mod.$main
  28760. ]));
  28761. end;
  28762. procedure TTestModule.TestRTTI_PublishedMethodOverloadFail;
  28763. begin
  28764. WithTypeInfo:=true;
  28765. StartProgram(false);
  28766. Add('type');
  28767. Add(' TObject = class');
  28768. Add(' published');
  28769. Add(' procedure Proc; virtual; abstract;');
  28770. Add(' procedure Proc(Sender: tobject); virtual; abstract;');
  28771. Add(' end;');
  28772. Add('begin');
  28773. SetExpectedPasResolverError('Duplicate published method "Proc" at test1.pp(6,19)',
  28774. nDuplicatePublishedMethodXAtY);
  28775. ConvertProgram;
  28776. end;
  28777. procedure TTestModule.TestRTTI_PublishedMethodHideNoHint;
  28778. begin
  28779. WithTypeInfo:=true;
  28780. StartUnit(false);
  28781. Add([
  28782. 'interface',
  28783. 'type',
  28784. ' TObject = class',
  28785. ' end;',
  28786. ' {$M+}',
  28787. ' TBird = class',
  28788. ' procedure Fly;',
  28789. ' end;',
  28790. ' {$M-}',
  28791. 'type',
  28792. ' TEagle = class(TBird)',
  28793. ' procedure Fly;',
  28794. ' end;',
  28795. 'implementation',
  28796. 'procedure TBird.Fly;',
  28797. 'begin',
  28798. 'end;',
  28799. 'procedure TEagle.Fly;',
  28800. 'begin',
  28801. 'end;',
  28802. '']);
  28803. ConvertUnit;
  28804. CheckSource('TestRTTI_PublishedMethodHideNoHint',
  28805. LinesToStr([ // statements
  28806. 'rtl.createClass(this, "TObject", null, function () {',
  28807. ' this.$init = function () {',
  28808. ' };',
  28809. ' this.$final = function () {',
  28810. ' };',
  28811. '});',
  28812. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  28813. ' this.Fly = function () {',
  28814. ' };',
  28815. ' var $r = this.$rtti;',
  28816. ' $r.addMethod("Fly", 0, null);',
  28817. '});',
  28818. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  28819. ' this.Fly = function () {',
  28820. ' };',
  28821. ' var $r = this.$rtti;',
  28822. ' $r.addMethod("Fly", 0, null);',
  28823. '});',
  28824. '']),
  28825. LinesToStr([ // $mod.$main
  28826. ]));
  28827. CheckResolverUnexpectedHints(true);
  28828. end;
  28829. procedure TTestModule.TestRTTI_PublishedMethodExternalFail;
  28830. begin
  28831. WithTypeInfo:=true;
  28832. StartProgram(false);
  28833. Add('type');
  28834. Add(' TObject = class');
  28835. Add(' published');
  28836. Add(' procedure Proc; external name ''foo'';');
  28837. Add(' end;');
  28838. Add('begin');
  28839. SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
  28840. nPublishedNameMustMatchExternal);
  28841. ConvertProgram;
  28842. end;
  28843. procedure TTestModule.TestRTTI_PublishedClassPropertyFail;
  28844. begin
  28845. WithTypeInfo:=true;
  28846. StartProgram(false);
  28847. Add('type');
  28848. Add(' TObject = class');
  28849. Add(' class var FA: longint;');
  28850. Add(' published');
  28851. Add(' class property A: longint read FA;');
  28852. Add(' end;');
  28853. Add('begin');
  28854. SetExpectedPasResolverError('Invalid published property modifier "class"',
  28855. nInvalidXModifierY);
  28856. ConvertProgram;
  28857. end;
  28858. procedure TTestModule.TestRTTI_PublishedClassFieldFail;
  28859. begin
  28860. WithTypeInfo:=true;
  28861. StartProgram(false);
  28862. Add('type');
  28863. Add(' TObject = class');
  28864. Add(' published');
  28865. Add(' class var FA: longint;');
  28866. Add(' end;');
  28867. Add('begin');
  28868. SetExpectedPasResolverError(sSymbolCannotBePublished,
  28869. nSymbolCannotBePublished);
  28870. ConvertProgram;
  28871. end;
  28872. procedure TTestModule.TestRTTI_PublishedFieldExternalFail;
  28873. begin
  28874. WithTypeInfo:=true;
  28875. StartProgram(false);
  28876. Add('{$modeswitch externalclass}');
  28877. Add('type');
  28878. Add(' TObject = class');
  28879. Add(' published');
  28880. Add(' V: longint; external name ''foo'';');
  28881. Add(' end;');
  28882. Add('begin');
  28883. SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
  28884. nPublishedNameMustMatchExternal);
  28885. ConvertProgram;
  28886. end;
  28887. procedure TTestModule.TestRTTI_Class_Field;
  28888. begin
  28889. WithTypeInfo:=true;
  28890. StartProgram(false);
  28891. Add('{$modeswitch externalclass}');
  28892. Add('type');
  28893. Add(' TObject = class');
  28894. Add(' private');
  28895. Add(' FPropA: string;');
  28896. Add(' published');
  28897. Add(' VarLI: longint;');
  28898. Add(' VarC: char;');
  28899. Add(' VarS: string;');
  28900. Add(' VarD: double;');
  28901. Add(' VarB: boolean;');
  28902. Add(' VarLW: longword;');
  28903. Add(' VarSmI: smallint;');
  28904. Add(' VarW: word;');
  28905. Add(' VarShI: shortint;');
  28906. Add(' VarBy: byte;');
  28907. Add(' VarExt: longint external name ''VarExt'';');
  28908. Add(' ArrA, ArrB: array of byte;');
  28909. Add(' end;');
  28910. Add('var p: pointer;');
  28911. Add(' Obj: tobject;');
  28912. Add('begin');
  28913. Add(' p:=typeinfo(tobject);');
  28914. Add(' p:=typeinfo(p);');
  28915. Add(' p:=typeinfo(obj);');
  28916. ConvertProgram;
  28917. CheckSource('TestRTTI_Class_Field',
  28918. LinesToStr([ // statements
  28919. 'rtl.createClass(this, "TObject", null, function () {',
  28920. ' this.$init = function () {',
  28921. ' this.FPropA = "";',
  28922. ' this.VarLI = 0;',
  28923. ' this.VarC = "";',
  28924. ' this.VarS = "";',
  28925. ' this.VarD = 0.0;',
  28926. ' this.VarB = false;',
  28927. ' this.VarLW = 0;',
  28928. ' this.VarSmI = 0;',
  28929. ' this.VarW = 0;',
  28930. ' this.VarShI = 0;',
  28931. ' this.VarBy = 0;',
  28932. ' this.ArrA = [];',
  28933. ' this.ArrB = [];',
  28934. ' };',
  28935. ' this.$final = function () {',
  28936. ' this.ArrA = undefined;',
  28937. ' this.ArrB = undefined;',
  28938. ' };',
  28939. ' var $r = this.$rtti;',
  28940. ' $r.addField("VarLI", rtl.longint);',
  28941. ' $r.addField("VarC", rtl.char);',
  28942. ' $r.addField("VarS", rtl.string);',
  28943. ' $r.addField("VarD", rtl.double);',
  28944. ' $r.addField("VarB", rtl.boolean);',
  28945. ' $r.addField("VarLW", rtl.longword);',
  28946. ' $r.addField("VarSmI", rtl.smallint);',
  28947. ' $r.addField("VarW", rtl.word);',
  28948. ' $r.addField("VarShI", rtl.shortint);',
  28949. ' $r.addField("VarBy", rtl.byte);',
  28950. ' $r.addField("VarExt", rtl.longint);',
  28951. ' $mod.$rtti.$DynArray("TObject.ArrB$a", {',
  28952. ' eltype: rtl.byte',
  28953. ' });',
  28954. ' $r.addField("ArrA", $mod.$rtti["TObject.ArrB$a"]);',
  28955. ' $r.addField("ArrB", $mod.$rtti["TObject.ArrB$a"]);',
  28956. '});',
  28957. 'this.p = null;',
  28958. 'this.Obj = null;',
  28959. '']),
  28960. LinesToStr([ // $mod.$main
  28961. '$mod.p = $mod.$rtti["TObject"];',
  28962. '$mod.p = rtl.pointer;',
  28963. '$mod.p = $mod.Obj.$rtti;',
  28964. '']));
  28965. end;
  28966. procedure TTestModule.TestRTTI_Class_Method;
  28967. begin
  28968. WithTypeInfo:=true;
  28969. StartProgram(false);
  28970. Add('type');
  28971. Add(' TObject = class');
  28972. Add(' private');
  28973. Add(' procedure Internal; external name ''$intern'';');
  28974. Add(' published');
  28975. Add(' procedure Click; virtual; abstract;');
  28976. Add(' procedure Notify(Sender: TObject); virtual; abstract;');
  28977. Add(' function GetNotify: boolean; external name ''GetNotify'';');
  28978. Add(' procedure Println(a,b: longint); varargs; virtual; abstract;');
  28979. Add(' end;');
  28980. Add('begin');
  28981. ConvertProgram;
  28982. CheckSource('TestRTTI_Class_Method',
  28983. LinesToStr([ // statements
  28984. 'rtl.createClass(this, "TObject", null, function () {',
  28985. ' this.$init = function () {',
  28986. ' };',
  28987. ' this.$final = function () {',
  28988. ' };',
  28989. ' var $r = this.$rtti;',
  28990. ' $r.addMethod("Click", 0, null);',
  28991. ' $r.addMethod("Notify", 0, [["Sender", $r]]);',
  28992. ' $r.addMethod("GetNotify", 1, null, rtl.boolean,{flags: 4});',
  28993. ' $r.addMethod("Println", 0, [["a", rtl.longint], ["b", rtl.longint]], null, {',
  28994. ' flags: 2',
  28995. ' });',
  28996. '});',
  28997. '']),
  28998. LinesToStr([ // $mod.$main
  28999. '']));
  29000. end;
  29001. procedure TTestModule.TestRTTI_Class_MethodArgFlags;
  29002. begin
  29003. WithTypeInfo:=true;
  29004. StartProgram(false);
  29005. Add('type');
  29006. Add(' TObject = class');
  29007. Add(' published');
  29008. Add(' procedure OpenArray(const Args: array of string); virtual; abstract;');
  29009. Add(' procedure ByRef(var Value: longint; out Item: longint); virtual; abstract;');
  29010. Add(' procedure Untyped(var Value; out Item); virtual; abstract;');
  29011. Add(' end;');
  29012. Add('begin');
  29013. ConvertProgram;
  29014. CheckSource('TestRTTI_Class_MethodOpenArray',
  29015. LinesToStr([ // statements
  29016. 'rtl.createClass(this, "TObject", null, function () {',
  29017. ' this.$init = function () {',
  29018. ' };',
  29019. ' this.$final = function () {',
  29020. ' };',
  29021. ' var $r = this.$rtti;',
  29022. '$r.addMethod("OpenArray", 0, [["Args", rtl.string, 10]]);',
  29023. '$r.addMethod("ByRef", 0, [["Value", rtl.longint, 1], ["Item", rtl.longint, 4]]);',
  29024. '$r.addMethod("Untyped", 0, [["Value", null, 1], ["Item", null, 4]]);',
  29025. '});',
  29026. '']),
  29027. LinesToStr([ // $mod.$main
  29028. '']));
  29029. end;
  29030. procedure TTestModule.TestRTTI_Class_Property;
  29031. begin
  29032. WithTypeInfo:=true;
  29033. StartProgram(false);
  29034. Add('{$modeswitch externalclass}');
  29035. Add('type');
  29036. Add(' TObject = class');
  29037. Add(' private');
  29038. Add(' FColor: longint;');
  29039. Add(' FColorStored: boolean;');
  29040. Add(' procedure SetColor(Value: longint); virtual; abstract;');
  29041. Add(' function GetColor: longint; virtual; abstract;');
  29042. Add(' function GetColorStored: boolean; virtual; abstract;');
  29043. Add(' FExtSize: longint external name ''$extSize'';');
  29044. Add(' FExtSizeStored: boolean external name ''$extSizeStored'';');
  29045. Add(' procedure SetExtSize(Value: longint); external name ''$setSize'';');
  29046. Add(' function GetExtSize: longint; external name ''$getSize'';');
  29047. Add(' function GetExtSizeStored: boolean; external name ''$getExtSizeStored'';');
  29048. Add(' published');
  29049. Add(' property ColorA: longint read FColor;');
  29050. Add(' property ColorB: longint write FColor;');
  29051. Add(' property ColorC: longint read GetColor write SetColor;');
  29052. Add(' property ColorD: longint read FColor write FColor stored FColorStored;');
  29053. Add(' property ExtSizeA: longint read FExtSize write FExtSize;');
  29054. Add(' property ExtSizeB: longint read GetExtSize write SetExtSize stored FExtSizeStored;');
  29055. Add(' property ExtSizeC: longint read FExtSize write FExtSize stored GetExtSizeStored;');
  29056. Add(' end;');
  29057. Add('begin');
  29058. ConvertProgram;
  29059. CheckSource('TestRTTI_Class_Property',
  29060. LinesToStr([ // statements
  29061. 'rtl.createClass(this, "TObject", null, function () {',
  29062. ' this.$init = function () {',
  29063. ' this.FColor = 0;',
  29064. ' this.FColorStored = false;',
  29065. ' };',
  29066. ' this.$final = function () {',
  29067. ' };',
  29068. ' var $r = this.$rtti;',
  29069. ' $r.addProperty("ColorA", 0, rtl.longint, "FColor", "");',
  29070. ' $r.addProperty("ColorB", 0, rtl.longint, "", "FColor");',
  29071. ' $r.addProperty("ColorC", 3, rtl.longint, "GetColor", "SetColor");',
  29072. ' $r.addProperty(',
  29073. ' "ColorD",',
  29074. ' 8,',
  29075. ' rtl.longint,',
  29076. ' "FColor",',
  29077. ' "FColor",',
  29078. ' {',
  29079. ' stored: "FColorStored"',
  29080. ' }',
  29081. ' );',
  29082. ' $r.addProperty("ExtSizeA", 0, rtl.longint, "$extSize", "$extSize");',
  29083. ' $r.addProperty(',
  29084. ' "ExtSizeB",',
  29085. ' 11,',
  29086. ' rtl.longint,',
  29087. ' "$getSize",',
  29088. ' "$setSize",',
  29089. ' {',
  29090. ' stored: "$extSizeStored"',
  29091. ' }',
  29092. ' );',
  29093. ' $r.addProperty(',
  29094. ' "ExtSizeC",',
  29095. ' 12,',
  29096. ' rtl.longint,',
  29097. ' "$extSize",',
  29098. ' "$extSize",',
  29099. ' {',
  29100. ' stored: "$getExtSizeStored"',
  29101. ' }',
  29102. ' );',
  29103. '});',
  29104. '']),
  29105. LinesToStr([ // $mod.$main
  29106. '']));
  29107. end;
  29108. procedure TTestModule.TestRTTI_Class_PropertyParams;
  29109. begin
  29110. WithTypeInfo:=true;
  29111. StartProgram(false);
  29112. Add('{$modeswitch externalclass}');
  29113. Add('type');
  29114. Add(' integer = longint;');
  29115. Add(' TObject = class');
  29116. Add(' private');
  29117. Add(' function GetItems(i: integer): tobject; virtual; abstract;');
  29118. Add(' procedure SetItems(i: integer; value: tobject); virtual; abstract;');
  29119. Add(' function GetValues(const i: integer; var b: boolean): char; virtual; abstract;');
  29120. Add(' procedure SetValues(const i: integer; var b: boolean; value: char); virtual; abstract;');
  29121. Add(' published');
  29122. Add(' property Items[Index: integer]: tobject read getitems write setitems;');
  29123. Add(' property Values[const keya: integer; var keyb: boolean]: char read getvalues write setvalues;');
  29124. Add(' end;');
  29125. Add('begin');
  29126. ConvertProgram;
  29127. CheckSource('TestRTTI_Class_PropertyParams',
  29128. LinesToStr([ // statements
  29129. 'rtl.createClass(this, "TObject", null, function () {',
  29130. ' this.$init = function () {',
  29131. ' };',
  29132. ' this.$final = function () {',
  29133. ' };',
  29134. ' var $r = this.$rtti;',
  29135. ' $r.addProperty("Items", 3, $r, "GetItems", "SetItems");',
  29136. ' $r.addProperty("Values", 3, rtl.char, "GetValues", "SetValues");',
  29137. '});',
  29138. '']),
  29139. LinesToStr([ // $mod.$main
  29140. '']));
  29141. end;
  29142. procedure TTestModule.TestRTTI_Class_OtherUnit_TypeAlias;
  29143. begin
  29144. WithTypeInfo:=true;
  29145. AddModuleWithIntfImplSrc('unit1.pas',
  29146. 'type TColor = -5..5;',
  29147. '');
  29148. StartProgram(true);
  29149. Add([
  29150. 'uses unit1;',
  29151. 'type',
  29152. ' TColorAlias = TColor;',
  29153. ' TColorTypeAlias = type TColor;',
  29154. ' TObject = class',
  29155. ' private',
  29156. ' fColor: TColor;',
  29157. ' fAlias: TColorAlias;',
  29158. ' fTypeAlias: TColorTypeAlias;',
  29159. ' published',
  29160. ' property Color: TColor read fcolor;',
  29161. ' property Alias: TColorAlias read falias;',
  29162. ' property TypeAlias: TColorTypeAlias read ftypealias;',
  29163. ' end;',
  29164. 'begin',
  29165. '']);
  29166. ConvertProgram;
  29167. CheckSource('TestRTTI_Class_OtherUnit_TypeAlias',
  29168. LinesToStr([ // statements
  29169. 'this.$rtti.$inherited("TColorTypeAlias", pas.unit1.$rtti["TColor"], {});',
  29170. 'rtl.createClass(this, "TObject", null, function () {',
  29171. ' this.$init = function () {',
  29172. ' this.fColor = 0;',
  29173. ' this.fAlias = 0;',
  29174. ' this.fTypeAlias = 0;',
  29175. ' };',
  29176. ' this.$final = function () {',
  29177. ' };',
  29178. ' var $r = this.$rtti;',
  29179. ' $r.addProperty("Color", 0, pas.unit1.$rtti["TColor"], "fColor", "");',
  29180. ' $r.addProperty("Alias", 0, pas.unit1.$rtti["TColor"], "fAlias", "");',
  29181. ' $r.addProperty("TypeAlias", 0, $mod.$rtti["TColorTypeAlias"], "fTypeAlias", "");',
  29182. '});',
  29183. '']),
  29184. LinesToStr([ // $mod.$main
  29185. '']));
  29186. end;
  29187. procedure TTestModule.TestRTTI_Class_OmitRTTI;
  29188. begin
  29189. WithTypeInfo:=true;
  29190. StartProgram(false);
  29191. Add([
  29192. '{$modeswitch omitrtti}',
  29193. 'type',
  29194. ' TObject = class',
  29195. ' private',
  29196. ' FA: byte;',
  29197. ' published',
  29198. ' property A: byte read FA write FA;',
  29199. ' end;',
  29200. 'begin']);
  29201. ConvertProgram;
  29202. CheckSource('TestRTTI_Class_OmitRTTI',
  29203. LinesToStr([ // statements
  29204. 'rtl.createClass(this, "TObject", null, function () {',
  29205. ' this.$init = function () {',
  29206. ' this.FA = 0;',
  29207. ' };',
  29208. ' this.$final = function () {',
  29209. ' };',
  29210. '});',
  29211. '']),
  29212. LinesToStr([ // $mod.$main
  29213. '']));
  29214. end;
  29215. procedure TTestModule.TestRTTI_Class_Field_AnonymousArrayOfSelfClass;
  29216. begin
  29217. WithTypeInfo:=true;
  29218. StartUnit(true,[supTObject]);
  29219. Add([
  29220. 'interface',
  29221. 'type',
  29222. ' {$M+1}',
  29223. ' TBird = class',
  29224. ' published',
  29225. ' Swarm: array of TBird;',
  29226. ' end;',
  29227. 'implementation',
  29228. '']);
  29229. ConvertUnit;
  29230. CheckSource('TestRTTI_Class_Field_AnonymousArrayOfSelfClass',
  29231. LinesToStr([ // statements
  29232. 'rtl.createClass(this, "TBird", pas.system.TObject, function () {',
  29233. ' this.$init = function () {',
  29234. ' pas.system.TObject.$init.call(this);',
  29235. ' this.Swarm = [];',
  29236. ' };',
  29237. ' this.$final = function () {',
  29238. ' this.Swarm = undefined;',
  29239. ' pas.system.TObject.$final.call(this);',
  29240. ' };',
  29241. ' var $r = this.$rtti;',
  29242. ' $mod.$rtti.$DynArray("TBird.Swarm$a", {',
  29243. ' eltype: $r',
  29244. ' });',
  29245. ' $r.addField("Swarm", $mod.$rtti["TBird.Swarm$a"]);',
  29246. '});',
  29247. '']),
  29248. LinesToStr([ // $mod.$main
  29249. '']));
  29250. end;
  29251. procedure TTestModule.TestRTTI_IndexModifier;
  29252. begin
  29253. WithTypeInfo:=true;
  29254. StartProgram(false);
  29255. Add([
  29256. 'type',
  29257. ' TEnum = (red, blue);',
  29258. ' TObject = class',
  29259. ' FB: boolean;',
  29260. ' procedure SetIntBool(Index: longint; b: boolean); virtual; abstract;',
  29261. ' function GetBoolBool(Index: boolean): boolean; virtual; abstract;',
  29262. ' procedure SetBoolBool(Index: boolean; b: boolean); virtual; abstract;',
  29263. ' function GetEnumBool(Index: TEnum): boolean; virtual; abstract;',
  29264. ' function GetStrIntBool(A: String; I: longint): boolean; virtual; abstract;',
  29265. ' procedure SetStrIntBool(A: String; I: longint; b: boolean); virtual; abstract;',
  29266. ' published',
  29267. ' property B1: boolean index 1 read FB write SetIntBool;',
  29268. ' property B2: boolean index TEnum.blue read GetEnumBool write FB;',
  29269. ' property I1[A: String]: boolean index 2 read GetStrIntBool write SetStrIntBool;',
  29270. ' end;',
  29271. 'begin']);
  29272. ConvertProgram;
  29273. CheckSource('TestRTTI_IndexModifier',
  29274. LinesToStr([ // statements
  29275. 'this.TEnum = {',
  29276. ' "0": "red",',
  29277. ' red: 0,',
  29278. ' "1": "blue",',
  29279. ' blue: 1',
  29280. '};',
  29281. 'this.$rtti.$Enum("TEnum", {',
  29282. ' minvalue: 0,',
  29283. ' maxvalue: 1,',
  29284. ' ordtype: 1,',
  29285. ' enumtype: this.TEnum',
  29286. '});',
  29287. 'rtl.createClass(this, "TObject", null, function () {',
  29288. ' this.$init = function () {',
  29289. ' this.FB = false;',
  29290. ' };',
  29291. ' this.$final = function () {',
  29292. ' };',
  29293. ' var $r = this.$rtti;',
  29294. ' $r.addProperty(',
  29295. ' "B1",',
  29296. ' 18,',
  29297. ' rtl.boolean,',
  29298. ' "FB",',
  29299. ' "SetIntBool",',
  29300. ' {',
  29301. ' index: 1',
  29302. ' }',
  29303. ' );',
  29304. ' $r.addProperty(',
  29305. ' "B2",',
  29306. ' 17,',
  29307. ' rtl.boolean,',
  29308. ' "GetEnumBool",',
  29309. ' "FB",',
  29310. ' {',
  29311. ' index: $mod.TEnum.blue',
  29312. ' }',
  29313. ' );',
  29314. ' $r.addProperty(',
  29315. ' "I1",',
  29316. ' 19,',
  29317. ' rtl.boolean,',
  29318. ' "GetStrIntBool",',
  29319. ' "SetStrIntBool",',
  29320. ' {',
  29321. ' index: 2',
  29322. ' }',
  29323. ' );',
  29324. '});',
  29325. '']),
  29326. LinesToStr([ // $mod.$main
  29327. '']));
  29328. end;
  29329. procedure TTestModule.TestRTTI_StoredModifier;
  29330. begin
  29331. WithTypeInfo:=true;
  29332. StartProgram(false);
  29333. Add([
  29334. 'const',
  29335. ' ConstB = true;',
  29336. 'type',
  29337. ' TObject = class',
  29338. ' private',
  29339. ' FB: boolean;',
  29340. ' function IsBStored: boolean; virtual; abstract;',
  29341. ' published',
  29342. ' property BoolA: boolean read FB stored true;',
  29343. ' property BoolB: boolean read FB stored false;',
  29344. ' property BoolC: boolean read FB stored FB;',
  29345. ' property BoolD: boolean read FB stored ConstB;',
  29346. ' property BoolE: boolean read FB stored IsBStored;',
  29347. ' end;',
  29348. 'begin']);
  29349. ConvertProgram;
  29350. CheckSource('TestRTTI_StoredModifier',
  29351. LinesToStr([ // statements
  29352. 'this.ConstB = true;',
  29353. 'rtl.createClass(this, "TObject", null, function () {',
  29354. ' this.$init = function () {',
  29355. ' this.FB = false;',
  29356. ' };',
  29357. ' this.$final = function () {',
  29358. ' };',
  29359. ' var $r = this.$rtti;',
  29360. ' $r.addProperty("BoolA", 0, rtl.boolean, "FB", "");',
  29361. ' $r.addProperty("BoolB", 4, rtl.boolean, "FB", "");',
  29362. ' $r.addProperty(',
  29363. ' "BoolC",',
  29364. ' 8,',
  29365. ' rtl.boolean,',
  29366. ' "FB",',
  29367. ' "",',
  29368. ' {',
  29369. ' stored: "FB"',
  29370. ' }',
  29371. ' );',
  29372. ' $r.addProperty("BoolD", 0, rtl.boolean, "FB", "");',
  29373. ' $r.addProperty(',
  29374. ' "BoolE",',
  29375. ' 12,',
  29376. ' rtl.boolean,',
  29377. ' "FB",',
  29378. ' "",',
  29379. ' {',
  29380. ' stored: "IsBStored"',
  29381. ' }',
  29382. ' );',
  29383. '});',
  29384. '']),
  29385. LinesToStr([ // $mod.$main
  29386. '']));
  29387. end;
  29388. procedure TTestModule.TestRTTI_DefaultValue;
  29389. begin
  29390. WithTypeInfo:=true;
  29391. StartProgram(false);
  29392. Add([
  29393. 'type',
  29394. ' TEnum = (red, blue);',
  29395. 'const',
  29396. ' CB = true or false;',
  29397. ' CI = 1+2;',
  29398. 'type',
  29399. ' TObject = class',
  29400. ' FB: boolean;',
  29401. ' FI: longint;',
  29402. ' FE: TEnum;',
  29403. ' published',
  29404. ' property B1: boolean read FB default true;',
  29405. ' property B2: boolean read FB default CB;',
  29406. ' property B3: boolean read FB default test1.cb;',
  29407. ' property I1: longint read FI default 2;',
  29408. ' property I2: longint read FI default CI;',
  29409. ' property E1: TEnum read FE default red;',
  29410. ' property E2: TEnum read FE default TEnum.blue;',
  29411. ' end;',
  29412. 'begin']);
  29413. ConvertProgram;
  29414. CheckSource('TestRTTI_DefaultValue',
  29415. LinesToStr([ // statements
  29416. 'this.TEnum = {',
  29417. ' "0": "red",',
  29418. ' red: 0,',
  29419. ' "1": "blue",',
  29420. ' blue: 1',
  29421. '};',
  29422. 'this.$rtti.$Enum("TEnum", {',
  29423. ' minvalue: 0,',
  29424. ' maxvalue: 1,',
  29425. ' ordtype: 1,',
  29426. ' enumtype: this.TEnum',
  29427. '});',
  29428. 'this.CB = true || false;',
  29429. 'this.CI = 1 + 2;',
  29430. 'rtl.createClass(this, "TObject", null, function () {',
  29431. ' this.$init = function () {',
  29432. ' this.FB = false;',
  29433. ' this.FI = 0;',
  29434. ' this.FE = 0;',
  29435. ' };',
  29436. ' this.$final = function () {',
  29437. ' };',
  29438. ' var $r = this.$rtti;',
  29439. ' $r.addProperty(',
  29440. ' "B1",',
  29441. ' 0,',
  29442. ' rtl.boolean,',
  29443. ' "FB",',
  29444. ' "",',
  29445. ' {',
  29446. ' Default: true',
  29447. ' }',
  29448. ' );',
  29449. ' $r.addProperty(',
  29450. ' "B2",',
  29451. ' 0,',
  29452. ' rtl.boolean,',
  29453. ' "FB",',
  29454. ' "",',
  29455. ' {',
  29456. ' Default: true',
  29457. ' }',
  29458. ' );',
  29459. ' $r.addProperty(',
  29460. ' "B3",',
  29461. ' 0,',
  29462. ' rtl.boolean,',
  29463. ' "FB",',
  29464. ' "",',
  29465. ' {',
  29466. ' Default: true',
  29467. ' }',
  29468. ' );',
  29469. ' $r.addProperty(',
  29470. ' "I1",',
  29471. ' 0,',
  29472. ' rtl.longint,',
  29473. ' "FI",',
  29474. ' "",',
  29475. ' {',
  29476. ' Default: 2',
  29477. ' }',
  29478. ' );',
  29479. ' $r.addProperty(',
  29480. ' "I2",',
  29481. ' 0,',
  29482. ' rtl.longint,',
  29483. ' "FI",',
  29484. ' "",',
  29485. ' {',
  29486. ' Default: 3',
  29487. ' }',
  29488. ' );',
  29489. ' $r.addProperty(',
  29490. ' "E1",',
  29491. ' 0,',
  29492. ' $mod.$rtti["TEnum"],',
  29493. ' "FE",',
  29494. ' "",',
  29495. ' {',
  29496. ' Default: $mod.TEnum.red',
  29497. ' }',
  29498. ' );',
  29499. ' $r.addProperty(',
  29500. ' "E2",',
  29501. ' 0,',
  29502. ' $mod.$rtti["TEnum"],',
  29503. ' "FE",',
  29504. ' "",',
  29505. ' {',
  29506. ' Default: $mod.TEnum.blue',
  29507. ' }',
  29508. ' );',
  29509. '});',
  29510. '']),
  29511. LinesToStr([ // $mod.$main
  29512. '']));
  29513. end;
  29514. procedure TTestModule.TestRTTI_DefaultValueSet;
  29515. begin
  29516. WithTypeInfo:=true;
  29517. StartProgram(false);
  29518. Add([
  29519. 'type',
  29520. ' TEnum = (red, blue);',
  29521. ' TSet = set of TEnum;',
  29522. 'const',
  29523. ' CSet = [red,blue];',
  29524. 'type',
  29525. ' TObject = class',
  29526. ' FSet: TSet;',
  29527. ' published',
  29528. ' property Set1: TSet read FSet default [];',
  29529. ' property Set2: TSet read FSet default [red];',
  29530. ' property Set3: TSet read FSet default [red,blue];',
  29531. ' property Set4: TSet read FSet default CSet;',
  29532. ' end;',
  29533. 'begin']);
  29534. ConvertProgram;
  29535. CheckSource('TestRTTI_DefaultValueSet',
  29536. LinesToStr([ // statements
  29537. 'this.TEnum = {',
  29538. ' "0": "red",',
  29539. ' red: 0,',
  29540. ' "1": "blue",',
  29541. ' blue: 1',
  29542. '};',
  29543. 'this.$rtti.$Enum("TEnum", {',
  29544. ' minvalue: 0,',
  29545. ' maxvalue: 1,',
  29546. ' ordtype: 1,',
  29547. ' enumtype: this.TEnum',
  29548. '});',
  29549. 'this.$rtti.$Set("TSet", {',
  29550. ' comptype: this.$rtti["TEnum"]',
  29551. '});',
  29552. 'this.CSet = rtl.createSet(this.TEnum.red, this.TEnum.blue);',
  29553. 'rtl.createClass(this, "TObject", null, function () {',
  29554. ' this.$init = function () {',
  29555. ' this.FSet = {};',
  29556. ' };',
  29557. ' this.$final = function () {',
  29558. ' this.FSet = undefined;',
  29559. ' };',
  29560. ' var $r = this.$rtti;',
  29561. ' $r.addProperty(',
  29562. ' "Set1",',
  29563. ' 0,',
  29564. ' $mod.$rtti["TSet"],',
  29565. ' "FSet",',
  29566. ' "",',
  29567. ' {',
  29568. ' Default: {}',
  29569. ' }',
  29570. ' );',
  29571. ' $r.addProperty(',
  29572. ' "Set2",',
  29573. ' 0,',
  29574. ' $mod.$rtti["TSet"],',
  29575. ' "FSet",',
  29576. ' "",',
  29577. ' {',
  29578. ' Default: rtl.createSet($mod.TEnum.red)',
  29579. ' }',
  29580. ' );',
  29581. ' $r.addProperty(',
  29582. ' "Set3",',
  29583. ' 0,',
  29584. ' $mod.$rtti["TSet"],',
  29585. ' "FSet",',
  29586. ' "",',
  29587. ' {',
  29588. ' Default: rtl.createSet($mod.TEnum.red, $mod.TEnum.blue)',
  29589. ' }',
  29590. ' );',
  29591. ' $r.addProperty(',
  29592. ' "Set4",',
  29593. ' 0,',
  29594. ' $mod.$rtti["TSet"],',
  29595. ' "FSet",',
  29596. ' "",',
  29597. ' {',
  29598. ' Default: $mod.CSet',
  29599. ' }',
  29600. ' );',
  29601. '});',
  29602. '']),
  29603. LinesToStr([ // $mod.$main
  29604. '']));
  29605. end;
  29606. procedure TTestModule.TestRTTI_DefaultValueRangeType;
  29607. begin
  29608. WithTypeInfo:=true;
  29609. StartProgram(false);
  29610. Add([
  29611. 'type',
  29612. ' TRg = -1..1;',
  29613. 'const',
  29614. ' l = low(TRg);',
  29615. ' h = high(TRg);',
  29616. 'type',
  29617. ' TObject = class',
  29618. ' FV: TRg;',
  29619. ' published',
  29620. ' property V1: TRg read FV default -1;',
  29621. ' end;',
  29622. 'begin']);
  29623. ConvertProgram;
  29624. CheckSource('TestRTTI_DefaultValueRangeType',
  29625. LinesToStr([ // statements
  29626. 'this.$rtti.$Int("TRg", {',
  29627. ' minvalue: -1,',
  29628. ' maxvalue: 1,',
  29629. ' ordtype: 0',
  29630. '});',
  29631. 'this.l = -1;',
  29632. 'this.h = 1;',
  29633. 'rtl.createClass(this, "TObject", null, function () {',
  29634. ' this.$init = function () {',
  29635. ' this.FV = 0;',
  29636. ' };',
  29637. ' this.$final = function () {',
  29638. ' };',
  29639. ' var $r = this.$rtti;',
  29640. ' $r.addProperty(',
  29641. ' "V1",',
  29642. ' 0,',
  29643. ' $mod.$rtti["TRg"],',
  29644. ' "FV",',
  29645. ' "",',
  29646. ' {',
  29647. ' Default: -1',
  29648. ' }',
  29649. ' );',
  29650. '});',
  29651. '']),
  29652. LinesToStr([ // $mod.$main
  29653. '']));
  29654. end;
  29655. procedure TTestModule.TestRTTI_DefaultValueInherit;
  29656. begin
  29657. WithTypeInfo:=true;
  29658. StartProgram(false);
  29659. Add([
  29660. 'type',
  29661. ' TObject = class',
  29662. ' FA, FB: byte;',
  29663. ' property A: byte read FA default 1;',
  29664. ' property B: byte read FB default 2;',
  29665. ' end;',
  29666. ' TBird = class',
  29667. ' published',
  29668. ' property A;',
  29669. ' property B nodefault;',
  29670. ' end;',
  29671. 'begin']);
  29672. ConvertProgram;
  29673. CheckSource('TestRTTI_DefaultValueInherit',
  29674. LinesToStr([ // statements
  29675. 'rtl.createClass(this, "TObject", null, function () {',
  29676. ' this.$init = function () {',
  29677. ' this.FA = 0;',
  29678. ' this.FB = 0;',
  29679. ' };',
  29680. ' this.$final = function () {',
  29681. ' };',
  29682. '});',
  29683. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  29684. ' var $r = this.$rtti;',
  29685. ' $r.addProperty(',
  29686. ' "A",',
  29687. ' 0,',
  29688. ' rtl.byte,',
  29689. ' "FA",',
  29690. ' "",',
  29691. ' {',
  29692. ' Default: 1',
  29693. ' }',
  29694. ' );',
  29695. ' $r.addProperty("B", 0, rtl.byte, "FB", "");',
  29696. '});',
  29697. '']),
  29698. LinesToStr([ // $mod.$main
  29699. '']));
  29700. end;
  29701. procedure TTestModule.TestRTTI_OverrideMethod;
  29702. begin
  29703. WithTypeInfo:=true;
  29704. StartProgram(false);
  29705. Add('type');
  29706. Add(' TObject = class');
  29707. Add(' published');
  29708. Add(' procedure DoIt; virtual; abstract;');
  29709. Add(' end;');
  29710. Add(' TSky = class');
  29711. Add(' published');
  29712. Add(' procedure DoIt; override;');
  29713. Add(' end;');
  29714. Add('procedure TSky.DoIt; begin end;');
  29715. Add('begin');
  29716. ConvertProgram;
  29717. CheckSource('TestRTTI_OverrideMethod',
  29718. LinesToStr([ // statements
  29719. 'rtl.createClass(this, "TObject", null, function () {',
  29720. ' this.$init = function () {',
  29721. ' };',
  29722. ' this.$final = function () {',
  29723. ' };',
  29724. ' var $r = this.$rtti;',
  29725. ' $r.addMethod("DoIt", 0, null);',
  29726. '});',
  29727. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  29728. ' this.DoIt = function () {',
  29729. ' };',
  29730. '});',
  29731. '']),
  29732. LinesToStr([ // $mod.$main
  29733. '']));
  29734. end;
  29735. procedure TTestModule.TestRTTI_ReintroduceMethod;
  29736. begin
  29737. WithTypeInfo:=true;
  29738. StartProgram(false);
  29739. Add([
  29740. 'type',
  29741. ' TObject = class',
  29742. ' published',
  29743. ' procedure DoIt;',
  29744. ' end;',
  29745. ' TSky = class',
  29746. ' published',
  29747. ' procedure DoIt; reintroduce;',
  29748. ' end;',
  29749. 'procedure TObject.DoIt; begin end;',
  29750. 'procedure TSky.DoIt;',
  29751. 'begin',
  29752. ' inherited DoIt;',
  29753. 'end;',
  29754. 'begin']);
  29755. ConvertProgram;
  29756. CheckSource('TestRTTI_ReintroduceMethod',
  29757. LinesToStr([ // statements
  29758. 'rtl.createClass(this, "TObject", null, function () {',
  29759. ' this.$init = function () {',
  29760. ' };',
  29761. ' this.$final = function () {',
  29762. ' };',
  29763. ' this.DoIt = function () {',
  29764. ' };',
  29765. ' var $r = this.$rtti;',
  29766. ' $r.addMethod("DoIt", 0, null);',
  29767. '});',
  29768. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  29769. ' this.DoIt = function () {',
  29770. ' $mod.TObject.DoIt.call(this);',
  29771. ' };',
  29772. ' var $r = this.$rtti;',
  29773. ' $r.addMethod("DoIt", 0, null);',
  29774. '});',
  29775. '']),
  29776. LinesToStr([ // $mod.$main
  29777. '']));
  29778. end;
  29779. procedure TTestModule.TestRTTI_OverloadProperty;
  29780. begin
  29781. WithTypeInfo:=true;
  29782. StartProgram(false);
  29783. Add('type');
  29784. Add(' TObject = class');
  29785. Add(' protected');
  29786. Add(' FFlag: longint;');
  29787. Add(' published');
  29788. Add(' property Flag: longint read fflag;');
  29789. Add(' end;');
  29790. Add(' TSky = class');
  29791. Add(' published');
  29792. Add(' property FLAG: longint write fflag;');
  29793. Add(' end;');
  29794. Add('begin');
  29795. ConvertProgram;
  29796. CheckSource('TestRTTI_OverrideMethod',
  29797. LinesToStr([ // statements
  29798. 'rtl.createClass(this, "TObject", null, function () {',
  29799. ' this.$init = function () {',
  29800. ' this.FFlag = 0;',
  29801. ' };',
  29802. ' this.$final = function () {',
  29803. ' };',
  29804. ' var $r = this.$rtti;',
  29805. ' $r.addProperty("Flag", 0, rtl.longint, "FFlag", "");',
  29806. '});',
  29807. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  29808. ' var $r = this.$rtti;',
  29809. ' $r.addProperty("Flag", 0, rtl.longint, "", "FFlag");',
  29810. '});',
  29811. '']),
  29812. LinesToStr([ // $mod.$main
  29813. '']));
  29814. end;
  29815. procedure TTestModule.TestRTTI_ClassForward;
  29816. begin
  29817. WithTypeInfo:=true;
  29818. StartProgram(false);
  29819. Add('type');
  29820. Add(' TObject = class end;');
  29821. Add(' tbridge = class;');
  29822. Add(' TProc = function: tbridge;');
  29823. Add(' TOger = class');
  29824. Add(' published');
  29825. Add(' FBridge: tbridge;');
  29826. Add(' procedure SetBridge(Value: tbridge); virtual; abstract;');
  29827. Add(' property Bridge: tbridge read fbridge write setbridge;');
  29828. Add(' end;');
  29829. Add(' TBridge = class');
  29830. Add(' FOger: toger;');
  29831. Add(' end;');
  29832. Add('var p: Pointer;');
  29833. Add(' b: tbridge;');
  29834. Add('begin');
  29835. Add(' p:=typeinfo(tbridge);');
  29836. Add(' p:=typeinfo(b);');
  29837. ConvertProgram;
  29838. CheckSource('TestRTTI_ClassForward',
  29839. LinesToStr([ // statements
  29840. 'rtl.createClass(this, "TObject", null, function () {',
  29841. ' this.$init = function () {',
  29842. ' };',
  29843. ' this.$final = function () {',
  29844. ' };',
  29845. '});',
  29846. 'this.$rtti.$Class("TBridge");',
  29847. 'this.$rtti.$ProcVar("TProc", {',
  29848. ' procsig: rtl.newTIProcSig(null, this.$rtti["TBridge"])',
  29849. '});',
  29850. 'rtl.createClass(this, "TOger", this.TObject, function () {',
  29851. ' this.$init = function () {',
  29852. ' $mod.TObject.$init.call(this);',
  29853. ' this.FBridge = null;',
  29854. ' };',
  29855. ' this.$final = function () {',
  29856. ' this.FBridge = undefined;',
  29857. ' $mod.TObject.$final.call(this);',
  29858. ' };',
  29859. ' var $r = this.$rtti;',
  29860. ' $r.addField("FBridge", $mod.$rtti["TBridge"]);',
  29861. ' $r.addMethod("SetBridge", 0, [["Value", $mod.$rtti["TBridge"]]]);',
  29862. ' $r.addProperty("Bridge", 2, $mod.$rtti["TBridge"], "FBridge", "SetBridge");',
  29863. '});',
  29864. 'rtl.createClass(this, "TBridge", this.TObject, function () {',
  29865. ' this.$init = function () {',
  29866. ' $mod.TObject.$init.call(this);',
  29867. ' this.FOger = null;',
  29868. ' };',
  29869. ' this.$final = function () {',
  29870. ' this.FOger = undefined;',
  29871. ' $mod.TObject.$final.call(this);',
  29872. ' };',
  29873. '});',
  29874. 'this.p = null;',
  29875. 'this.b = null;',
  29876. '']),
  29877. LinesToStr([ // $mod.$main
  29878. '$mod.p = $mod.$rtti["TBridge"];',
  29879. '$mod.p = $mod.b.$rtti;',
  29880. '']));
  29881. end;
  29882. procedure TTestModule.TestRTTI_ClassOf;
  29883. begin
  29884. WithTypeInfo:=true;
  29885. StartProgram(false);
  29886. Add('type');
  29887. Add(' TClass = class of tobject;');
  29888. Add(' TProcA = function: TClass;');
  29889. Add(' TObject = class');
  29890. Add(' published');
  29891. Add(' C: tclass;');
  29892. Add(' end;');
  29893. Add(' tfox = class;');
  29894. Add(' TBird = class end;');
  29895. Add(' TBirds = class of tbird;');
  29896. Add(' TFox = class end;');
  29897. Add(' TFoxes = class of tfox;');
  29898. Add(' TCows = class of TCow;');
  29899. Add(' TCow = class;');
  29900. Add(' TCow = class end;');
  29901. Add('begin');
  29902. ConvertProgram;
  29903. CheckSource('TestRTTI_ClassOf',
  29904. LinesToStr([ // statements
  29905. 'this.$rtti.$Class("TObject");',
  29906. 'this.$rtti.$ClassRef("TClass", {',
  29907. ' instancetype: this.$rtti["TObject"]',
  29908. '});',
  29909. 'this.$rtti.$ProcVar("TProcA", {',
  29910. ' procsig: rtl.newTIProcSig(null, this.$rtti["TClass"])',
  29911. '});',
  29912. 'rtl.createClass(this, "TObject", null, function () {',
  29913. ' this.$init = function () {',
  29914. ' this.C = null;',
  29915. ' };',
  29916. ' this.$final = function () {',
  29917. ' this.C = undefined;',
  29918. ' };',
  29919. ' var $r = this.$rtti;',
  29920. ' $r.addField("C", $mod.$rtti["TClass"]);',
  29921. '});',
  29922. 'this.$rtti.$Class("TFox");',
  29923. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  29924. '});',
  29925. 'this.$rtti.$ClassRef("TBirds", {',
  29926. ' instancetype: this.$rtti["TBird"]',
  29927. '});',
  29928. 'rtl.createClass(this, "TFox", this.TObject, function () {',
  29929. '});',
  29930. 'this.$rtti.$ClassRef("TFoxes", {',
  29931. ' instancetype: this.$rtti["TFox"]',
  29932. '});',
  29933. 'this.$rtti.$Class("TCow");',
  29934. 'this.$rtti.$ClassRef("TCows", {',
  29935. ' instancetype: this.$rtti["TCow"]',
  29936. '});',
  29937. 'rtl.createClass(this, "TCow", this.TObject, function () {',
  29938. '});',
  29939. '']),
  29940. LinesToStr([ // $mod.$main
  29941. '']));
  29942. end;
  29943. procedure TTestModule.TestRTTI_Record;
  29944. begin
  29945. WithTypeInfo:=true;
  29946. StartProgram(false);
  29947. Add('type');
  29948. Add(' integer = longint;');
  29949. Add(' TPoint = record');
  29950. Add(' x,y: integer;');
  29951. Add(' end;');
  29952. Add('var p: pointer;');
  29953. Add(' r: tpoint;');
  29954. Add('begin');
  29955. Add(' p:=typeinfo(tpoint);');
  29956. Add(' p:=typeinfo(r);');
  29957. Add(' p:=typeinfo(r.x);');
  29958. ConvertProgram;
  29959. CheckSource('TestRTTI_Record',
  29960. LinesToStr([ // statements
  29961. 'rtl.recNewT(this, "TPoint", function () {',
  29962. ' this.x = 0;',
  29963. ' this.y = 0;',
  29964. ' this.$eq = function (b) {',
  29965. ' return (this.x === b.x) && (this.y === b.y);',
  29966. ' };',
  29967. ' this.$assign = function (s) {',
  29968. ' this.x = s.x;',
  29969. ' this.y = s.y;',
  29970. ' return this;',
  29971. ' };',
  29972. ' var $r = $mod.$rtti.$Record("TPoint", {});',
  29973. ' $r.addField("x", rtl.longint);',
  29974. ' $r.addField("y", rtl.longint);',
  29975. '});',
  29976. 'this.p = null;',
  29977. 'this.r = this.TPoint.$new();',
  29978. '']),
  29979. LinesToStr([ // $mod.$main
  29980. '$mod.p = $mod.$rtti["TPoint"];',
  29981. '$mod.p = $mod.$rtti["TPoint"];',
  29982. '$mod.p = rtl.longint;',
  29983. '']));
  29984. end;
  29985. procedure TTestModule.TestRTTI_RecordAnonymousArray;
  29986. begin
  29987. WithTypeInfo:=true;
  29988. StartProgram(false);
  29989. Add('type');
  29990. Add(' TFloatRec = record');
  29991. Add(' c,d: array of char;');
  29992. // Add(' i: array of array of longint;');
  29993. Add(' end;');
  29994. Add('var p: pointer;');
  29995. Add(' r: tfloatrec;');
  29996. Add('begin');
  29997. Add(' p:=typeinfo(tfloatrec);');
  29998. Add(' p:=typeinfo(r);');
  29999. Add(' p:=typeinfo(r.d);');
  30000. ConvertProgram;
  30001. CheckSource('TestRTTI_Record',
  30002. LinesToStr([ // statements
  30003. 'rtl.recNewT(this, "TFloatRec", function () {',
  30004. ' this.$new = function () {',
  30005. ' var r = Object.create(this);',
  30006. ' r.c = [];',
  30007. ' r.d = [];',
  30008. ' return r;',
  30009. ' };',
  30010. ' this.$eq = function (b) {',
  30011. ' return (this.c === b.c) && (this.d === b.d);',
  30012. ' };',
  30013. ' this.$assign = function (s) {',
  30014. ' this.c = rtl.arrayRef(s.c);',
  30015. ' this.d = rtl.arrayRef(s.d);',
  30016. ' return this;',
  30017. ' };',
  30018. ' var $r = $mod.$rtti.$Record("TFloatRec", {});',
  30019. ' $mod.$rtti.$DynArray("TFloatRec.d$a", {',
  30020. ' eltype: rtl.char',
  30021. ' });',
  30022. ' $r.addField("c", $mod.$rtti["TFloatRec.d$a"]);',
  30023. ' $r.addField("d", $mod.$rtti["TFloatRec.d$a"]);',
  30024. '});',
  30025. 'this.p = null;',
  30026. 'this.r = this.TFloatRec.$new();',
  30027. '']),
  30028. LinesToStr([ // $mod.$main
  30029. '$mod.p = $mod.$rtti["TFloatRec"];',
  30030. '$mod.p = $mod.$rtti["TFloatRec"];',
  30031. '$mod.p = $mod.$rtti["TFloatRec.d$a"];',
  30032. '']));
  30033. end;
  30034. procedure TTestModule.TestRTTI_Record_ClassVarType;
  30035. begin
  30036. WithTypeInfo:=true;
  30037. StartProgram(false);
  30038. Add([
  30039. '{$modeswitch AdvancedRecords}',
  30040. 'type',
  30041. ' TPoint = record',
  30042. ' type TProc = procedure(w: word);',
  30043. ' class var p: TProc;',
  30044. ' end;',
  30045. 'begin',
  30046. '']);
  30047. ConvertProgram;
  30048. CheckSource('TestRTTI_Record_ClassVarType',
  30049. LinesToStr([ // statements
  30050. 'rtl.recNewT(this, "TPoint", function () {',
  30051. ' $mod.$rtti.$ProcVar("TPoint.TProc", {',
  30052. ' procsig: rtl.newTIProcSig([["w", rtl.word]])',
  30053. ' });',
  30054. ' this.p = null;',
  30055. ' this.$eq = function (b) {',
  30056. ' return true;',
  30057. ' };',
  30058. ' this.$assign = function (s) {',
  30059. ' return this;',
  30060. ' };',
  30061. ' var $r = $mod.$rtti.$Record("TPoint", {});',
  30062. ' $r.addField("p", $mod.$rtti["TPoint.TProc"]);',
  30063. '}, true);',
  30064. '']),
  30065. LinesToStr([ // $mod.$main
  30066. '']));
  30067. end;
  30068. procedure TTestModule.TestRTTI_LocalTypes;
  30069. begin
  30070. WithTypeInfo:=true;
  30071. StartProgram(false);
  30072. Add([
  30073. 'procedure DoIt;',
  30074. 'type',
  30075. ' integer = longint;',
  30076. ' TPoint = record',
  30077. ' x,y: integer;',
  30078. ' end;',
  30079. 'var p: TPoint;',
  30080. 'begin',
  30081. 'end;',
  30082. 'begin']);
  30083. ConvertProgram;
  30084. CheckSource('TestRTTI_LocalTypes',
  30085. LinesToStr([ // statements
  30086. 'var TPoint = rtl.recNewT(null, "", function () {',
  30087. ' this.x = 0;',
  30088. ' this.y = 0;',
  30089. ' this.$eq = function (b) {',
  30090. ' return (this.x === b.x) && (this.y === b.y);',
  30091. ' };',
  30092. ' this.$assign = function (s) {',
  30093. ' this.x = s.x;',
  30094. ' this.y = s.y;',
  30095. ' return this;',
  30096. ' };',
  30097. '});',
  30098. 'this.DoIt = function () {',
  30099. ' var p = TPoint.$new();',
  30100. '};',
  30101. '']),
  30102. LinesToStr([ // $mod.$main
  30103. '']));
  30104. end;
  30105. procedure TTestModule.TestRTTI_TypeInfo_BaseTypes;
  30106. begin
  30107. WithTypeInfo:=true;
  30108. StartProgram(false);
  30109. Add([
  30110. 'type',
  30111. ' TCaption = string;',
  30112. ' TYesNo = boolean;',
  30113. ' TLetter = char;',
  30114. ' TFloat = double;',
  30115. ' TPtr = pointer;',
  30116. ' TShortInt = shortint;',
  30117. ' TByte = byte;',
  30118. ' TSmallInt = smallint;',
  30119. ' TWord = word;',
  30120. ' TInt32 = longint;',
  30121. ' TDWord = longword;',
  30122. ' TValue = jsvalue;',
  30123. 'var p: TPtr;',
  30124. 'begin',
  30125. ' p:=typeinfo(string);',
  30126. ' p:=typeinfo(tcaption);',
  30127. ' p:=typeinfo(boolean);',
  30128. ' p:=typeinfo(tyesno);',
  30129. ' p:=typeinfo(char);',
  30130. ' p:=typeinfo(tletter);',
  30131. ' p:=typeinfo(double);',
  30132. ' p:=typeinfo(tfloat);',
  30133. ' p:=typeinfo(pointer);',
  30134. ' p:=typeinfo(tptr);',
  30135. ' p:=typeinfo(shortint);',
  30136. ' p:=typeinfo(tshortint);',
  30137. ' p:=typeinfo(byte);',
  30138. ' p:=typeinfo(tbyte);',
  30139. ' p:=typeinfo(smallint);',
  30140. ' p:=typeinfo(tsmallint);',
  30141. ' p:=typeinfo(word);',
  30142. ' p:=typeinfo(tword);',
  30143. ' p:=typeinfo(longword);',
  30144. ' p:=typeinfo(tdword);',
  30145. ' p:=typeinfo(jsvalue);',
  30146. ' p:=typeinfo(tvalue);',
  30147. '']);
  30148. ConvertProgram;
  30149. CheckSource('TestRTTI_TypeInfo_BaseTypes',
  30150. LinesToStr([ // statements
  30151. 'this.p = null;',
  30152. '']),
  30153. LinesToStr([ // $mod.$main
  30154. '$mod.p = rtl.string;',
  30155. '$mod.p = rtl.string;',
  30156. '$mod.p = rtl.boolean;',
  30157. '$mod.p = rtl.boolean;',
  30158. '$mod.p = rtl.char;',
  30159. '$mod.p = rtl.char;',
  30160. '$mod.p = rtl.double;',
  30161. '$mod.p = rtl.double;',
  30162. '$mod.p = rtl.pointer;',
  30163. '$mod.p = rtl.pointer;',
  30164. '$mod.p = rtl.shortint;',
  30165. '$mod.p = rtl.shortint;',
  30166. '$mod.p = rtl.byte;',
  30167. '$mod.p = rtl.byte;',
  30168. '$mod.p = rtl.smallint;',
  30169. '$mod.p = rtl.smallint;',
  30170. '$mod.p = rtl.word;',
  30171. '$mod.p = rtl.word;',
  30172. '$mod.p = rtl.longword;',
  30173. '$mod.p = rtl.longword;',
  30174. '$mod.p = rtl.jsvalue;',
  30175. '$mod.p = rtl.jsvalue;',
  30176. '']));
  30177. end;
  30178. procedure TTestModule.TestRTTI_TypeInfo_Type_BaseTypes;
  30179. begin
  30180. WithTypeInfo:=true;
  30181. StartProgram(false);
  30182. Add([
  30183. 'type',
  30184. ' TCaption = type string;',
  30185. ' TYesNo = type boolean;',
  30186. ' TLetter = type char;',
  30187. ' TFloat = type double;',
  30188. ' TPtr = type pointer;',
  30189. ' TShortInt = type shortint;',
  30190. ' TByte = type byte;',
  30191. ' TSmallInt = type smallint;',
  30192. ' TWord = type word;',
  30193. ' TInt32 = type longint;',
  30194. ' TDWord = type longword;',
  30195. ' TValue = type jsvalue;',
  30196. ' TAliasValue = type TValue;',
  30197. 'var',
  30198. ' p: TPtr;',
  30199. ' a: TAliasValue;',
  30200. 'begin',
  30201. ' p:=typeinfo(tcaption);',
  30202. ' p:=typeinfo(tyesno);',
  30203. ' p:=typeinfo(tletter);',
  30204. ' p:=typeinfo(tfloat);',
  30205. ' p:=typeinfo(tptr);',
  30206. ' p:=typeinfo(tshortint);',
  30207. ' p:=typeinfo(tbyte);',
  30208. ' p:=typeinfo(tsmallint);',
  30209. ' p:=typeinfo(tword);',
  30210. ' p:=typeinfo(tdword);',
  30211. ' p:=typeinfo(tvalue);',
  30212. ' p:=typeinfo(taliasvalue);',
  30213. ' p:=typeinfo(a);',
  30214. '']);
  30215. ConvertProgram;
  30216. CheckSource('TestRTTI_TypeInfo_Type_BaseTypes',
  30217. LinesToStr([ // statements
  30218. 'this.$rtti.$inherited("TCaption", rtl.string, {});',
  30219. 'this.$rtti.$inherited("TYesNo", rtl.boolean, {});',
  30220. 'this.$rtti.$inherited("TLetter", rtl.char, {});',
  30221. 'this.$rtti.$inherited("TFloat", rtl.double, {});',
  30222. 'this.$rtti.$inherited("TPtr", rtl.pointer, {});',
  30223. 'this.$rtti.$inherited("TShortInt", rtl.shortint, {});',
  30224. 'this.$rtti.$inherited("TByte", rtl.byte, {});',
  30225. 'this.$rtti.$inherited("TSmallInt", rtl.smallint, {});',
  30226. 'this.$rtti.$inherited("TWord", rtl.word, {});',
  30227. 'this.$rtti.$inherited("TInt32", rtl.longint, {});',
  30228. 'this.$rtti.$inherited("TDWord", rtl.longword, {});',
  30229. 'this.$rtti.$inherited("TValue", rtl.jsvalue, {});',
  30230. 'this.$rtti.$inherited("TAliasValue", this.$rtti["TValue"], {});',
  30231. 'this.p = null;',
  30232. 'this.a = undefined;',
  30233. '']),
  30234. LinesToStr([ // $mod.$main
  30235. '$mod.p = $mod.$rtti["TCaption"];',
  30236. '$mod.p = $mod.$rtti["TYesNo"];',
  30237. '$mod.p = $mod.$rtti["TLetter"];',
  30238. '$mod.p = $mod.$rtti["TFloat"];',
  30239. '$mod.p = $mod.$rtti["TPtr"];',
  30240. '$mod.p = $mod.$rtti["TShortInt"];',
  30241. '$mod.p = $mod.$rtti["TByte"];',
  30242. '$mod.p = $mod.$rtti["TSmallInt"];',
  30243. '$mod.p = $mod.$rtti["TWord"];',
  30244. '$mod.p = $mod.$rtti["TDWord"];',
  30245. '$mod.p = $mod.$rtti["TValue"];',
  30246. '$mod.p = $mod.$rtti["TAliasValue"];',
  30247. '$mod.p = $mod.$rtti["TAliasValue"];',
  30248. '']));
  30249. end;
  30250. procedure TTestModule.TestRTTI_TypeInfo_LocalFail;
  30251. begin
  30252. WithTypeInfo:=true;
  30253. StartProgram(false);
  30254. Add('procedure DoIt;');
  30255. Add('type');
  30256. Add(' integer = longint;');
  30257. Add(' TPoint = record');
  30258. Add(' x,y: integer;');
  30259. Add(' end;');
  30260. Add('var p: pointer;');
  30261. Add('begin');
  30262. Add(' p:=typeinfo(tpoint);');
  30263. Add('end;');
  30264. Add('begin');
  30265. SetExpectedPasResolverError(sSymbolCannotBePublished,nSymbolCannotBePublished);
  30266. ConvertProgram;
  30267. end;
  30268. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses1;
  30269. begin
  30270. WithTypeInfo:=true;
  30271. StartProgram(true,[supTypeInfo]);
  30272. Add([
  30273. '{$modeswitch externalclass}',
  30274. 'type',
  30275. ' TFlag = (up,down);',
  30276. ' TFlags = set of TFlag;',
  30277. 'var',
  30278. ' ti: TTypeInfo;',
  30279. ' tiInt: TTypeInfoInteger;',
  30280. ' tiEnum: TTypeInfoEnum;',
  30281. ' tiSet: TTypeInfoSet;',
  30282. 'begin',
  30283. ' ti:=typeinfo(string);',
  30284. ' ti:=typeinfo(boolean);',
  30285. ' ti:=typeinfo(char);',
  30286. ' ti:=typeinfo(double);',
  30287. ' tiInt:=typeinfo(shortint);',
  30288. ' tiInt:=typeinfo(byte);',
  30289. ' tiInt:=typeinfo(smallint);',
  30290. ' tiInt:=typeinfo(word);',
  30291. ' tiInt:=typeinfo(longint);',
  30292. ' tiInt:=typeinfo(longword);',
  30293. ' ti:=typeinfo(jsvalue);',
  30294. ' tiEnum:=typeinfo(tflag);',
  30295. ' tiSet:=typeinfo(tflags);']);
  30296. ConvertProgram;
  30297. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses1',
  30298. LinesToStr([ // statements
  30299. 'this.TFlag = {',
  30300. ' "0": "up",',
  30301. ' up: 0,',
  30302. ' "1": "down",',
  30303. ' down: 1',
  30304. '};',
  30305. 'this.$rtti.$Enum("TFlag", {',
  30306. ' minvalue: 0,',
  30307. ' maxvalue: 1,',
  30308. ' ordtype: 1,',
  30309. ' enumtype: this.TFlag',
  30310. '});',
  30311. 'this.$rtti.$Set("TFlags", {',
  30312. ' comptype: this.$rtti["TFlag"]',
  30313. '});',
  30314. 'this.ti = null;',
  30315. 'this.tiInt = null;',
  30316. 'this.tiEnum = null;',
  30317. 'this.tiSet = null;',
  30318. '']),
  30319. LinesToStr([ // $mod.$main
  30320. '$mod.ti = rtl.string;',
  30321. '$mod.ti = rtl.boolean;',
  30322. '$mod.ti = rtl.char;',
  30323. '$mod.ti = rtl.double;',
  30324. '$mod.tiInt = rtl.shortint;',
  30325. '$mod.tiInt = rtl.byte;',
  30326. '$mod.tiInt = rtl.smallint;',
  30327. '$mod.tiInt = rtl.word;',
  30328. '$mod.tiInt = rtl.longint;',
  30329. '$mod.tiInt = rtl.longword;',
  30330. '$mod.ti = rtl.jsvalue;',
  30331. '$mod.tiEnum = $mod.$rtti["TFlag"];',
  30332. '$mod.tiSet = $mod.$rtti["TFlags"];',
  30333. '']));
  30334. end;
  30335. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses2;
  30336. begin
  30337. WithTypeInfo:=true;
  30338. StartProgram(true,[supTypeInfo]);
  30339. Add('{$modeswitch externalclass}');
  30340. Add('type');
  30341. Add(' TStaticArr = array[boolean] of string;');
  30342. Add(' TDynArr = array of string;');
  30343. Add(' TProc = procedure;');
  30344. Add(' TMethod = procedure of object;');
  30345. Add('var');
  30346. Add(' StaticArray: TStaticArr;');
  30347. Add(' tiStaticArray: TTypeInfoStaticArray;');
  30348. Add(' DynArray: TDynArr;');
  30349. Add(' tiDynArray: TTypeInfoDynArray;');
  30350. Add(' ProcVar: TProc;');
  30351. Add(' tiProcVar: TTypeInfoProcVar;');
  30352. Add(' MethodVar: TMethod;');
  30353. Add(' tiMethodVar: TTypeInfoMethodVar;');
  30354. Add('begin');
  30355. Add(' tiStaticArray:=typeinfo(StaticArray);');
  30356. Add(' tiStaticArray:=typeinfo(TStaticArr);');
  30357. Add(' tiDynArray:=typeinfo(DynArray);');
  30358. Add(' tiDynArray:=typeinfo(TDynArr);');
  30359. Add(' tiProcVar:=typeinfo(ProcVar);');
  30360. Add(' tiProcVar:=typeinfo(TProc);');
  30361. Add(' tiMethodVar:=typeinfo(MethodVar);');
  30362. Add(' tiMethodVar:=typeinfo(TMethod);');
  30363. ConvertProgram;
  30364. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses2',
  30365. LinesToStr([ // statements
  30366. 'this.$rtti.$StaticArray("TStaticArr", {',
  30367. ' dims: [2],',
  30368. ' eltype: rtl.string',
  30369. '});',
  30370. 'this.$rtti.$DynArray("TDynArr", {',
  30371. ' eltype: rtl.string',
  30372. '});',
  30373. 'this.$rtti.$ProcVar("TProc", {',
  30374. ' procsig: rtl.newTIProcSig(null)',
  30375. '});',
  30376. 'this.$rtti.$MethodVar("TMethod", {',
  30377. ' procsig: rtl.newTIProcSig(null),',
  30378. ' methodkind: 0',
  30379. '});',
  30380. 'this.StaticArray = rtl.arraySetLength(null,"",2);',
  30381. 'this.tiStaticArray = null;',
  30382. 'this.DynArray = [];',
  30383. 'this.tiDynArray = null;',
  30384. 'this.ProcVar = null;',
  30385. 'this.tiProcVar = null;',
  30386. 'this.MethodVar = null;',
  30387. 'this.tiMethodVar = null;',
  30388. '']),
  30389. LinesToStr([ // $mod.$main
  30390. '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
  30391. '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
  30392. '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
  30393. '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
  30394. '$mod.tiProcVar = $mod.$rtti["TProc"];',
  30395. '$mod.tiProcVar = $mod.$rtti["TProc"];',
  30396. '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
  30397. '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
  30398. '']));
  30399. end;
  30400. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses3;
  30401. begin
  30402. WithTypeInfo:=true;
  30403. StartProgram(true,[supTypeInfo]);
  30404. Add('{$modeswitch externalclass}');
  30405. Add('type');
  30406. Add(' TRec = record end;');
  30407. // ToDo: ^TRec
  30408. Add(' TObject = class end;');
  30409. Add(' TClass = class of tobject;');
  30410. Add('var');
  30411. Add(' Rec: trec;');
  30412. Add(' tiRecord: ttypeinforecord;');
  30413. Add(' Obj: tobject;');
  30414. Add(' tiClass: ttypeinfoclass;');
  30415. Add(' aClass: tclass;');
  30416. Add(' tiClassRef: ttypeinfoclassref;');
  30417. // ToDo: ^TRec
  30418. Add(' tiPointer: ttypeinfopointer;');
  30419. Add('begin');
  30420. Add(' tirecord:=typeinfo(trec);');
  30421. Add(' tirecord:=typeinfo(trec);');
  30422. Add(' ticlass:=typeinfo(obj);');
  30423. Add(' ticlass:=typeinfo(tobject);');
  30424. Add(' ticlass:=typeinfo(aclass);');
  30425. Add(' ticlassref:=typeinfo(tclass);');
  30426. ConvertProgram;
  30427. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses3',
  30428. LinesToStr([ // statements
  30429. 'rtl.recNewT(this, "TRec", function () {',
  30430. ' this.$eq = function (b) {',
  30431. ' return true;',
  30432. ' };',
  30433. ' this.$assign = function (s) {',
  30434. ' return this;',
  30435. ' };',
  30436. ' $mod.$rtti.$Record("TRec", {});',
  30437. '});',
  30438. 'rtl.createClass(this, "TObject", null, function () {',
  30439. ' this.$init = function () {',
  30440. ' };',
  30441. ' this.$final = function () {',
  30442. ' };',
  30443. '});',
  30444. 'this.$rtti.$ClassRef("TClass", {',
  30445. ' instancetype: this.$rtti["TObject"]',
  30446. '});',
  30447. 'this.Rec = this.TRec.$new();',
  30448. 'this.tiRecord = null;',
  30449. 'this.Obj = null;',
  30450. 'this.tiClass = null;',
  30451. 'this.aClass = null;',
  30452. 'this.tiClassRef = null;',
  30453. 'this.tiPointer = null;',
  30454. '']),
  30455. LinesToStr([ // $mod.$main
  30456. '$mod.tiRecord = $mod.$rtti["TRec"];',
  30457. '$mod.tiRecord = $mod.$rtti["TRec"];',
  30458. '$mod.tiClass = $mod.Obj.$rtti;',
  30459. '$mod.tiClass = $mod.$rtti["TObject"];',
  30460. '$mod.tiClass = $mod.aClass.$rtti;',
  30461. '$mod.tiClassRef = $mod.$rtti["TClass"];',
  30462. '']));
  30463. end;
  30464. procedure TTestModule.TestRTTI_TypeInfo_FunctionClassType;
  30465. begin
  30466. WithTypeInfo:=true;
  30467. StartProgram(true,[supTypeInfo]);
  30468. Add([
  30469. '{$modeswitch externalclass}',
  30470. 'type',
  30471. ' TClass = class of tobject;',
  30472. ' TObject = class',
  30473. ' function MyClass: TClass;',
  30474. ' class function ClassType: TClass;',
  30475. ' end;',
  30476. 'function TObject.MyClass: TClass;',
  30477. 'var t: TTypeInfoClass;',
  30478. 'begin',
  30479. ' t:=TypeInfo(Self);',
  30480. ' t:=TypeInfo(Result);',
  30481. ' t:=TypeInfo(TObject);',
  30482. 'end;',
  30483. 'class function TObject.ClassType: TClass;',
  30484. 'var t: TTypeInfoClass;',
  30485. 'begin',
  30486. ' t:=TypeInfo(Self);',
  30487. ' t:=TypeInfo(Result);',
  30488. 'end;',
  30489. 'var',
  30490. ' Obj: TObject;',
  30491. ' t: TTypeInfoClass;',
  30492. 'begin',
  30493. ' t:=TypeInfo(TObject.ClassType);',
  30494. ' t:=TypeInfo(Obj.ClassType);',
  30495. ' t:=TypeInfo(Obj.MyClass);',
  30496. '']);
  30497. ConvertProgram;
  30498. CheckSource('TestRTTI_TypeInfo_FunctionClassType',
  30499. LinesToStr([ // statements
  30500. 'this.$rtti.$Class("TObject");',
  30501. 'this.$rtti.$ClassRef("TClass", {',
  30502. ' instancetype: this.$rtti["TObject"]',
  30503. '});',
  30504. 'rtl.createClass(this, "TObject", null, function () {',
  30505. ' this.$init = function () {',
  30506. ' };',
  30507. ' this.$final = function () {',
  30508. ' };',
  30509. ' this.MyClass = function () {',
  30510. ' var Result = null;',
  30511. ' var t = null;',
  30512. ' t = this.$rtti;',
  30513. ' t = Result.$rtti;',
  30514. ' t = $mod.$rtti["TObject"];',
  30515. ' return Result;',
  30516. ' };',
  30517. ' this.ClassType = function () {',
  30518. ' var Result = null;',
  30519. ' var t = null;',
  30520. ' t = this.$rtti;',
  30521. ' t = Result.$rtti;',
  30522. ' return Result;',
  30523. ' };',
  30524. '});',
  30525. 'this.Obj = null;',
  30526. 'this.t = null;',
  30527. '']),
  30528. LinesToStr([ // $mod.$main
  30529. '$mod.t = $mod.TObject.ClassType().$rtti;',
  30530. '$mod.t = $mod.Obj.$class.ClassType().$rtti;',
  30531. '$mod.t = $mod.Obj.MyClass().$rtti;',
  30532. '']));
  30533. end;
  30534. procedure TTestModule.TestRTTI_TypeInfo_MixedUnits_PointerAndClass;
  30535. begin
  30536. WithTypeInfo:=true;
  30537. AddModuleWithIntfImplSrc('typinfo.pas',
  30538. LinesToStr([
  30539. '{$modeswitch externalclass}',
  30540. 'type',
  30541. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  30542. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo) end;',
  30543. '']),
  30544. '');
  30545. AddModuleWithIntfImplSrc('unit2.pas',
  30546. LinesToStr([
  30547. 'uses typinfo;',
  30548. 'type PTypeInfo = TTypeInfo;', // delphi compatibility code
  30549. 'procedure DoPtr(p: PTypeInfo);',
  30550. 'procedure DoInfo(t: TTypeInfo);',
  30551. 'procedure DoInt(t: TTypeInfoInteger);',
  30552. '']),
  30553. LinesToStr([
  30554. 'procedure DoPtr(p: PTypeInfo);',
  30555. 'begin end;',
  30556. 'procedure DoInfo(t: TTypeInfo);',
  30557. 'begin end;',
  30558. 'procedure DoInt(t: TTypeInfoInteger);',
  30559. 'begin end;',
  30560. '']));
  30561. StartUnit(true);
  30562. Add([
  30563. 'interface',
  30564. 'uses unit2;', // does not use unit typinfo
  30565. 'implementation',
  30566. 'var',
  30567. ' i: byte;',
  30568. ' p: pointer;',
  30569. ' t: PTypeInfo;',
  30570. 'initialization',
  30571. ' p:=typeinfo(i);',
  30572. ' t:=typeinfo(i);',
  30573. ' if p=t then ;',
  30574. ' if p=typeinfo(i) then ;',
  30575. ' if typeinfo(i)=p then ;',
  30576. ' if t=typeinfo(i) then ;',
  30577. ' if typeinfo(i)=t then ;',
  30578. ' DoPtr(p);',
  30579. ' DoPtr(t);',
  30580. ' DoPtr(typeinfo(i));',
  30581. ' DoInfo(p);',
  30582. ' DoInfo(t);',
  30583. ' DoInfo(typeinfo(i));',
  30584. ' DoInt(typeinfo(i));',
  30585. '']);
  30586. ConvertUnit;
  30587. CheckSource('TestRTTI_TypeInfo_MixedUnits_PointerAndClass',
  30588. LinesToStr([ // statements
  30589. 'var $impl = $mod.$impl;',
  30590. '']),
  30591. LinesToStr([ // this.$init
  30592. '$impl.p = rtl.byte;',
  30593. '$impl.t = rtl.byte;',
  30594. 'if ($impl.p === $impl.t) ;',
  30595. 'if ($impl.p === rtl.byte) ;',
  30596. 'if (rtl.byte === $impl.p) ;',
  30597. 'if ($impl.t === rtl.byte) ;',
  30598. 'if (rtl.byte === $impl.t) ;',
  30599. 'pas.unit2.DoPtr($impl.p);',
  30600. 'pas.unit2.DoPtr($impl.t);',
  30601. 'pas.unit2.DoPtr(rtl.byte);',
  30602. 'pas.unit2.DoInfo($impl.p);',
  30603. 'pas.unit2.DoInfo($impl.t);',
  30604. 'pas.unit2.DoInfo(rtl.byte);',
  30605. 'pas.unit2.DoInt(rtl.byte);',
  30606. '']),
  30607. LinesToStr([ // implementation
  30608. '$impl.i = 0;',
  30609. '$impl.p = null;',
  30610. '$impl.t = null;',
  30611. '']) );
  30612. end;
  30613. procedure TTestModule.TestRTTI_Interface_Corba;
  30614. begin
  30615. WithTypeInfo:=true;
  30616. StartProgram(true,[supTypeInfo]);
  30617. Add([
  30618. '{$interfaces corba}',
  30619. '{$modeswitch externalclass}',
  30620. 'type',
  30621. ' IUnknown = interface',
  30622. ' end;',
  30623. ' IBird = interface',
  30624. ' function GetItem: longint;',
  30625. ' procedure SetItem(Value: longint);',
  30626. ' property Item: longint read GetItem write SetItem;',
  30627. ' end;',
  30628. 'procedure DoIt(t: TTypeInfoInterface); begin end;',
  30629. 'var',
  30630. ' i: IBird;',
  30631. ' t: TTypeInfoInterface;',
  30632. 'begin',
  30633. ' t:=TypeInfo(IBird);',
  30634. ' t:=TypeInfo(i);',
  30635. ' DoIt(t);',
  30636. ' DoIt(TypeInfo(IBird));',
  30637. '']);
  30638. ConvertProgram;
  30639. CheckSource('TestRTTI_Interface_Corba',
  30640. LinesToStr([ // statements
  30641. 'rtl.createInterface(',
  30642. ' this,',
  30643. ' "IUnknown",',
  30644. ' "{B92D5841-758A-322B-B800-000000000000}",',
  30645. ' [],',
  30646. ' null,',
  30647. ' function () {',
  30648. ' }',
  30649. ');',
  30650. 'rtl.createInterface(',
  30651. ' this,',
  30652. ' "IBird",',
  30653. ' "{D32D5841-6264-3AE3-A2C9-B91CE922C9B9}",',
  30654. ' ["GetItem", "SetItem"],',
  30655. ' null,',
  30656. ' function () {',
  30657. ' var $r = this.$rtti;',
  30658. ' $r.addMethod("GetItem", 1, null, rtl.longint);',
  30659. ' $r.addMethod("SetItem", 0, [["Value", rtl.longint]]);',
  30660. ' $r.addProperty("Item", 3, rtl.longint, "GetItem", "SetItem");',
  30661. ' }',
  30662. ');',
  30663. 'this.DoIt = function (t) {',
  30664. '}; ',
  30665. 'this.i = null;',
  30666. 'this.t = null;',
  30667. '']),
  30668. LinesToStr([ // $mod.$main
  30669. '$mod.t = $mod.$rtti["IBird"];',
  30670. '$mod.t = $mod.i.$rtti;',
  30671. '$mod.DoIt($mod.t);',
  30672. '$mod.DoIt($mod.$rtti["IBird"]);',
  30673. '']));
  30674. end;
  30675. procedure TTestModule.TestRTTI_Interface_COM;
  30676. begin
  30677. WithTypeInfo:=true;
  30678. StartProgram(true,[supTypeInfo]);
  30679. Add([
  30680. '{$interfaces com}',
  30681. '{$modeswitch externalclass}',
  30682. 'type',
  30683. ' TGuid = record end;',
  30684. ' integer = longint;',
  30685. ' IUnknown = interface',
  30686. ' function QueryInterface(const iid: TGuid; out obj): Integer;',
  30687. ' function _AddRef: Integer;',
  30688. ' function _Release: Integer;',
  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. 'var',
  30696. ' i: IBird;',
  30697. ' t: TTypeInfoInterface;',
  30698. 'begin',
  30699. ' t:=TypeInfo(IBird);',
  30700. ' t:=TypeInfo(i);',
  30701. '']);
  30702. ConvertProgram;
  30703. CheckSource('TestRTTI_Interface_COM',
  30704. LinesToStr([ // statements
  30705. 'rtl.recNewT(this, "TGuid", function () {',
  30706. ' this.$eq = function (b) {',
  30707. ' return true;',
  30708. ' };',
  30709. ' this.$assign = function (s) {',
  30710. ' return this;',
  30711. ' };',
  30712. ' $mod.$rtti.$Record("TGuid", {});',
  30713. '});',
  30714. 'rtl.createInterface(',
  30715. ' this,',
  30716. ' "IUnknown",',
  30717. ' "{D7ADB00D-1A9B-3EDC-B123-730E661DDFA9}",',
  30718. ' ["QueryInterface", "_AddRef", "_Release"],',
  30719. ' null,',
  30720. ' function () {',
  30721. ' this.$kind = "com";',
  30722. ' var $r = this.$rtti;',
  30723. ' $r.addMethod("QueryInterface", 1, [["iid", $mod.$rtti["TGuid"], 2], ["obj", null, 4]], rtl.longint);',
  30724. ' $r.addMethod("_AddRef", 1, null, rtl.longint);',
  30725. ' $r.addMethod("_Release", 1, null, rtl.longint);',
  30726. ' }',
  30727. ');',
  30728. 'rtl.createInterface(',
  30729. ' this,',
  30730. ' "IBird",',
  30731. ' "{9CC77572-0E45-3594-9A88-9E8D865C9E0A}",',
  30732. ' ["GetItem", "SetItem"],',
  30733. ' this.IUnknown,',
  30734. ' function () {',
  30735. ' var $r = this.$rtti;',
  30736. ' $r.addMethod("GetItem", 1, null, rtl.longint);',
  30737. ' $r.addMethod("SetItem", 0, [["Value", rtl.longint]]);',
  30738. ' $r.addProperty("Item", 3, rtl.longint, "GetItem", "SetItem");',
  30739. ' }',
  30740. ');',
  30741. 'this.i = null;',
  30742. 'this.t = null;',
  30743. '']),
  30744. LinesToStr([ // $mod.$main
  30745. '$mod.t = $mod.$rtti["IBird"];',
  30746. '$mod.t = $mod.i.$rtti;',
  30747. '']));
  30748. end;
  30749. procedure TTestModule.TestRTTI_ClassHelper;
  30750. begin
  30751. WithTypeInfo:=true;
  30752. StartProgram(true,[supTypeInfo]);
  30753. Add([
  30754. '{$interfaces com}',
  30755. '{$modeswitch externalclass}',
  30756. 'type',
  30757. ' TObject = class',
  30758. ' end;',
  30759. ' THelper = class helper for TObject',
  30760. ' published',
  30761. ' function GetItem: longint;',
  30762. ' property Item: longint read GetItem;',
  30763. ' end;',
  30764. 'function THelper.GetItem: longint;',
  30765. 'begin',
  30766. 'end;',
  30767. 'var',
  30768. ' t: TTypeInfoHelper;',
  30769. 'begin',
  30770. ' t:=TypeInfo(THelper);',
  30771. '']);
  30772. ConvertProgram;
  30773. CheckSource('TestRTTI_ClassHelper',
  30774. LinesToStr([ // statements
  30775. 'rtl.createClass(this, "TObject", null, function () {',
  30776. ' this.$init = function () {',
  30777. ' };',
  30778. ' this.$final = function () {',
  30779. ' };',
  30780. '});',
  30781. 'rtl.createHelper(this, "THelper", null, function () {',
  30782. ' this.GetItem = function () {',
  30783. ' var Result = 0;',
  30784. ' return Result;',
  30785. ' };',
  30786. ' var $r = this.$rtti;',
  30787. ' $r.addMethod("GetItem", 1, null, rtl.longint);',
  30788. ' $r.addProperty("Item", 1, rtl.longint, "GetItem", "");',
  30789. '});',
  30790. 'this.t = null;',
  30791. '']),
  30792. LinesToStr([ // $mod.$main
  30793. '$mod.t = $mod.$rtti["THelper"];',
  30794. '']));
  30795. end;
  30796. procedure TTestModule.TestRTTI_ExternalClass;
  30797. begin
  30798. WithTypeInfo:=true;
  30799. StartProgram(true,[supTypeInfo]);
  30800. Add([
  30801. '{$modeswitch externalclass}',
  30802. 'type',
  30803. ' TJSObject = class external name ''Object''',
  30804. ' end;',
  30805. ' TJSArray = class external name ''Array'' (TJSObject)',
  30806. ' end;',
  30807. 'var',
  30808. ' p: Pointer;',
  30809. ' tc: TTypeInfoExtClass;',
  30810. 'begin',
  30811. ' p:=typeinfo(TJSArray);']);
  30812. ConvertProgram;
  30813. CheckSource('TestRTTI_ExternalClass',
  30814. LinesToStr([ // statements
  30815. 'this.$rtti.$ExtClass("TJSObject", {',
  30816. ' jsclass: "Object"',
  30817. '});',
  30818. 'this.$rtti.$ExtClass("TJSArray", {',
  30819. ' ancestor: this.$rtti["TJSObject"],',
  30820. ' jsclass: "Array"',
  30821. '});',
  30822. 'this.p = null;',
  30823. 'this.tc = null;',
  30824. '']),
  30825. LinesToStr([ // $mod.$main
  30826. '$mod.p = $mod.$rtti["TJSArray"];',
  30827. '']));
  30828. end;
  30829. procedure TTestModule.TestRTTI_Unit;
  30830. begin
  30831. WithTypeInfo:=true;
  30832. AddModuleWithIntfImplSrc('unit2.pas',
  30833. LinesToStr([
  30834. '{$mode delphi}',
  30835. 'type',
  30836. ' TWordArray = array of word;',
  30837. ' TArray<T> = array of T;',
  30838. '']),
  30839. '');
  30840. StartUnit(true,[supTypeInfo,supTInterfacedObject]);
  30841. Add([
  30842. '{$mode delphi}',
  30843. 'interface',
  30844. 'uses unit2;',
  30845. 'type',
  30846. ' IBird = interface',
  30847. ' function Swoop: TWordArray;',
  30848. ' function Glide: TArray<word>;',
  30849. ' end;',
  30850. 'procedure Fly;',
  30851. 'implementation',
  30852. 'procedure Fly;',
  30853. 'var',
  30854. ' ta: tTypeInfoDynArray;',
  30855. ' ti: tTypeInfoInterface;',
  30856. 'begin',
  30857. ' ta:=typeinfo(TWordArray);',
  30858. ' ta:=typeinfo(TArray<word>);',
  30859. ' ti:=typeinfo(IBird);',
  30860. 'end;',
  30861. '']);
  30862. ConvertUnit;
  30863. CheckSource('TestRTTI_ExternalClass',
  30864. LinesToStr([ // statements
  30865. 'rtl.createInterface(',
  30866. ' this,',
  30867. ' "IBird",',
  30868. ' "{3B98AAAC-6116-3E17-AA85-F16786D85B09}",',
  30869. ' ["Swoop", "Glide"],',
  30870. ' pas.system.IUnknown,',
  30871. ' function () {',
  30872. ' var $r = this.$rtti;',
  30873. ' $r.addMethod("Swoop", 1, null, pas.unit2.$rtti["TWordArray"]);',
  30874. ' $r.addMethod("Glide", 1, null, pas.unit2.$rtti["TArray<System.Word>"]);',
  30875. ' }',
  30876. ');',
  30877. 'this.Fly = function () {',
  30878. ' var ta = null;',
  30879. ' var ti = null;',
  30880. ' ta = pas.unit2.$rtti["TWordArray"];',
  30881. ' ta = pas.unit2.$rtti["TArray<System.Word>"];',
  30882. ' ti = $mod.$rtti["IBird"];',
  30883. '};',
  30884. '']),
  30885. LinesToStr([ // $mod.$main
  30886. '']));
  30887. end;
  30888. procedure TTestModule.TestResourcestringProgram;
  30889. begin
  30890. AddModuleWithIntfImplSrc('unit2.pas',
  30891. LinesToStr([
  30892. 'resourcestring Title = ''Nice'';',
  30893. '']),
  30894. '');
  30895. StartProgram(true);
  30896. Add([
  30897. 'uses unit2;',
  30898. 'const Bar = ''bar'';',
  30899. 'resourcestring',
  30900. ' Red = ''red'';',
  30901. ' Foobar = ''fOo''+bar;',
  30902. 'var s: string;',
  30903. ' c: char;',
  30904. 'begin',
  30905. ' s:=red;',
  30906. ' s:=test1.red;',
  30907. ' s:=Title;',
  30908. ' c:=red[1];',
  30909. ' c:=test1.red[2];',
  30910. ' if red=foobar then ;',
  30911. ' if red[3]=red[4] then ;']);
  30912. ConvertProgram;
  30913. CheckSource('TestResourcestringProgram',
  30914. LinesToStr([ // statements
  30915. 'this.Bar = "bar";',
  30916. 'this.s = "";',
  30917. 'this.c = "";',
  30918. '$mod.$resourcestrings = {',
  30919. ' Red: {',
  30920. ' org: "red"',
  30921. ' },',
  30922. ' Foobar: {',
  30923. ' org: "fOobar"',
  30924. ' }',
  30925. '};',
  30926. '']),
  30927. LinesToStr([ // $mod.$main
  30928. '$mod.s = rtl.getResStr($mod, "Red");',
  30929. '$mod.s = rtl.getResStr($mod, "Red");',
  30930. '$mod.s = rtl.getResStr(pas.unit2, "Title");',
  30931. '$mod.c = rtl.getResStr($mod, "Red").charAt(0);',
  30932. '$mod.c = rtl.getResStr($mod, "Red").charAt(1);',
  30933. 'if (rtl.getResStr($mod, "Red") === rtl.getResStr($mod, "Foobar")) ;',
  30934. 'if (rtl.getResStr($mod, "Red").charAt(2) === rtl.getResStr($mod, "Red").charAt(3)) ;',
  30935. '']));
  30936. end;
  30937. procedure TTestModule.TestResourcestringUnit;
  30938. begin
  30939. AddModuleWithIntfImplSrc('unit2.pas',
  30940. LinesToStr([
  30941. 'resourcestring Title = ''Nice'';',
  30942. '']),
  30943. '');
  30944. StartUnit(true);
  30945. Add([
  30946. 'interface',
  30947. 'uses unit2;',
  30948. 'const Red = ''rEd'';',
  30949. 'resourcestring',
  30950. ' Blue = ''blue'';',
  30951. ' NotRed = ''not''+Red;',
  30952. 'var s: string;',
  30953. 'implementation',
  30954. 'resourcestring',
  30955. ' ImplGreen = ''green'';',
  30956. 'initialization',
  30957. ' s:=blue+ImplGreen;',
  30958. ' s:=test1.blue+test1.implgreen;',
  30959. ' s:=blue[1]+implgreen[2];',
  30960. ' s:=Title;',
  30961. '']);
  30962. ConvertUnit;
  30963. CheckSource('TestResourcestringUnit',
  30964. LinesToStr([ // statements
  30965. 'this.Red = "rEd";',
  30966. 'this.s = "";',
  30967. '$mod.$resourcestrings = {',
  30968. ' Blue: {',
  30969. ' org: "blue"',
  30970. ' },',
  30971. ' NotRed: {',
  30972. ' org: "notrEd"',
  30973. ' },',
  30974. ' ImplGreen: {',
  30975. ' org: "green"',
  30976. ' }',
  30977. '};',
  30978. '']),
  30979. LinesToStr([ // $mod.$main
  30980. '$mod.s = rtl.getResStr($mod, "Blue") + rtl.getResStr($mod, "ImplGreen");',
  30981. '$mod.s = rtl.getResStr($mod, "Blue") + rtl.getResStr($mod, "ImplGreen");',
  30982. '$mod.s = rtl.getResStr($mod, "Blue").charAt(0) + rtl.getResStr($mod, "ImplGreen").charAt(1);',
  30983. '$mod.s = rtl.getResStr(pas.unit2, "Title");',
  30984. '']));
  30985. end;
  30986. procedure TTestModule.TestResourcestringImplementation;
  30987. begin
  30988. StartUnit(false);
  30989. Add([
  30990. 'interface',
  30991. 'implementation',
  30992. 'resourcestring',
  30993. ' ImplRed = ''red'';']);
  30994. ConvertUnit;
  30995. CheckSource('TestResourcestringImplementation',
  30996. LinesToStr([ // intf statements
  30997. 'var $impl = $mod.$impl;']),
  30998. LinesToStr([ // $mod.$init
  30999. '']),
  31000. LinesToStr([ // impl statements
  31001. '$mod.$resourcestrings = {',
  31002. ' ImplRed: {',
  31003. ' org: "red"',
  31004. ' }',
  31005. '};',
  31006. '']));
  31007. end;
  31008. procedure TTestModule.TestAttributes_Members;
  31009. begin
  31010. WithTypeInfo:=true;
  31011. StartProgram(false);
  31012. Add([
  31013. '{$modeswitch PrefixedAttributes}',
  31014. 'type',
  31015. ' TObject = class',
  31016. ' constructor Create;',
  31017. ' end;',
  31018. ' TCustomAttribute = class',
  31019. ' constructor Create(Id: word);',
  31020. ' end;',
  31021. ' [Missing]',
  31022. ' TBird = class',
  31023. ' published',
  31024. ' [Tcustom]',
  31025. ' FField: word;',
  31026. ' [tcustom(14)]',
  31027. ' property Size: word read FField;',
  31028. ' [Tcustom(15)]',
  31029. ' procedure Fly; virtual; abstract;',
  31030. ' end;',
  31031. ' TRec = record',
  31032. ' [Tcustom,tcustom(14)]',
  31033. ' Size: word;',
  31034. ' end;',
  31035. 'constructor TObject.Create; begin end;',
  31036. 'constructor TCustomAttribute.Create(Id: word); begin end;',
  31037. 'begin',
  31038. '']);
  31039. ConvertProgram;
  31040. CheckSource('TestAttributes_Members',
  31041. LinesToStr([ // statements
  31042. 'rtl.createClass(this, "TObject", null, function () {',
  31043. ' this.$init = function () {',
  31044. ' };',
  31045. ' this.$final = function () {',
  31046. ' };',
  31047. ' this.Create = function () {',
  31048. ' return this;',
  31049. ' };',
  31050. '});',
  31051. 'rtl.createClass(this, "TCustomAttribute", this.TObject, function () {',
  31052. ' this.Create$1 = function (Id) {',
  31053. ' return this;',
  31054. ' };',
  31055. '});',
  31056. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  31057. ' this.$init = function () {',
  31058. ' $mod.TObject.$init.call(this);',
  31059. ' this.FField = 0;',
  31060. ' };',
  31061. ' var $r = this.$rtti;',
  31062. ' $r.addField("FField", rtl.word, {',
  31063. ' attr: [$mod.TCustomAttribute, "Create"]',
  31064. ' });',
  31065. ' $r.addProperty(',
  31066. ' "Size",',
  31067. ' 0,',
  31068. ' rtl.word,',
  31069. ' "FField",',
  31070. ' "",',
  31071. ' {',
  31072. ' attr: [$mod.TCustomAttribute, "Create$1", [14]]',
  31073. ' }',
  31074. ' );',
  31075. ' $r.addMethod("Fly", 0, null, null, {',
  31076. ' attr: [$mod.TCustomAttribute, "Create$1", [15]]',
  31077. ' });',
  31078. '});',
  31079. 'rtl.recNewT(this, "TRec", function () {',
  31080. ' this.Size = 0;',
  31081. ' this.$eq = function (b) {',
  31082. ' return this.Size === b.Size;',
  31083. ' };',
  31084. ' this.$assign = function (s) {',
  31085. ' this.Size = s.Size;',
  31086. ' return this;',
  31087. ' };',
  31088. ' var $r = $mod.$rtti.$Record("TRec", {});',
  31089. ' $r.addField("Size", rtl.word, {',
  31090. ' attr: [',
  31091. ' $mod.TCustomAttribute,',
  31092. ' "Create",',
  31093. ' $mod.TCustomAttribute,',
  31094. ' "Create$1",',
  31095. ' [14]',
  31096. ' ]',
  31097. ' });',
  31098. '});',
  31099. '']),
  31100. LinesToStr([ // $mod.$main
  31101. '']));
  31102. end;
  31103. procedure TTestModule.TestAttributes_Types;
  31104. begin
  31105. WithTypeInfo:=true;
  31106. StartProgram(false);
  31107. Add([
  31108. '{$modeswitch PrefixedAttributes}',
  31109. 'type',
  31110. ' TObject = class',
  31111. ' constructor Create(Id: word);',
  31112. ' end;',
  31113. ' TCustomAttribute = class',
  31114. ' end;',
  31115. ' [TCustom(1)]',
  31116. ' TMyClass = class',
  31117. ' end;',
  31118. ' [TCustom(11)]',
  31119. ' TMyDescendant = class(TMyClass)',
  31120. ' end;',
  31121. ' [TCustom(2)]',
  31122. ' TRec = record',
  31123. ' end;',
  31124. ' [TCustom(3)]',
  31125. ' TInt = type word;',
  31126. 'constructor TObject.Create(Id: word);',
  31127. 'begin',
  31128. 'end;',
  31129. 'var p: pointer;',
  31130. 'begin',
  31131. ' p:=typeinfo(TMyClass);',
  31132. ' p:=typeinfo(TRec);',
  31133. ' p:=typeinfo(TInt);',
  31134. '']);
  31135. ConvertProgram;
  31136. CheckSource('TestAttributes_Types',
  31137. LinesToStr([ // statements
  31138. 'rtl.createClass(this, "TObject", null, function () {',
  31139. ' this.$init = function () {',
  31140. ' };',
  31141. ' this.$final = function () {',
  31142. ' };',
  31143. ' this.Create = function (Id) {',
  31144. ' return this;',
  31145. ' };',
  31146. '});',
  31147. 'rtl.createClass(this, "TCustomAttribute", this.TObject, function () {',
  31148. '});',
  31149. 'rtl.createClass(this, "TMyClass", this.TObject, function () {',
  31150. ' var $r = this.$rtti;',
  31151. ' $r.attr = [$mod.TCustomAttribute, "Create", [1]];',
  31152. '});',
  31153. 'rtl.createClass(this, "TMyDescendant", this.TMyClass, function () {',
  31154. ' var $r = this.$rtti;',
  31155. ' $r.attr = [$mod.TCustomAttribute, "Create", [11]];',
  31156. '});',
  31157. 'rtl.recNewT(this, "TRec", function () {',
  31158. ' this.$eq = function (b) {',
  31159. ' return true;',
  31160. ' };',
  31161. ' this.$assign = function (s) {',
  31162. ' return this;',
  31163. ' };',
  31164. ' $mod.$rtti.$Record("TRec", {',
  31165. ' attr: [$mod.TCustomAttribute, "Create", [2]]',
  31166. ' });',
  31167. '});',
  31168. 'this.$rtti.$inherited("TInt", rtl.word, {',
  31169. ' attr: [this.TCustomAttribute, "Create", [3]]',
  31170. '});',
  31171. 'this.p = null;',
  31172. '']),
  31173. LinesToStr([ // $mod.$main
  31174. '$mod.p = $mod.$rtti["TMyClass"];',
  31175. '$mod.p = $mod.$rtti["TRec"];',
  31176. '$mod.p = $mod.$rtti["TInt"];',
  31177. '']));
  31178. end;
  31179. procedure TTestModule.TestAttributes_HelperConstructor_Fail;
  31180. begin
  31181. WithTypeInfo:=true;
  31182. StartProgram(false);
  31183. Add([
  31184. '{$modeswitch PrefixedAttributes}',
  31185. 'type',
  31186. ' TObject = class',
  31187. ' constructor Create;',
  31188. ' end;',
  31189. ' TCustomAttribute = class',
  31190. ' end;',
  31191. ' THelper = class helper for TCustomAttribute',
  31192. ' constructor Create(Id: word);',
  31193. ' end;',
  31194. ' [TCustom(3)]',
  31195. ' TMyInt = word;',
  31196. 'constructor TObject.Create; begin end;',
  31197. 'constructor THelper.Create(Id: word); begin end;',
  31198. 'begin',
  31199. ' if typeinfo(TMyInt)=nil then ;']);
  31200. ConvertProgram;
  31201. end;
  31202. procedure TTestModule.TestAssert;
  31203. begin
  31204. StartProgram(false);
  31205. Add([
  31206. 'procedure DoIt;',
  31207. 'var',
  31208. ' b: boolean;',
  31209. ' s: string;',
  31210. 'begin',
  31211. ' {$Assertions on}',
  31212. ' Assert(b);',
  31213. 'end;',
  31214. 'begin',
  31215. ' DoIt;',
  31216. '']);
  31217. ConvertProgram;
  31218. CheckSource('TestAssert',
  31219. LinesToStr([ // statements
  31220. 'this.DoIt = function () {',
  31221. ' var b = false;',
  31222. ' var s = "";',
  31223. ' if (!b) throw "assert failed";',
  31224. '};',
  31225. '']),
  31226. LinesToStr([ // $mod.$main
  31227. '$mod.DoIt();',
  31228. '']));
  31229. end;
  31230. procedure TTestModule.TestAssert_SysUtils;
  31231. begin
  31232. AddModuleWithIntfImplSrc('SysUtils.pas',
  31233. LinesToStr([
  31234. 'type',
  31235. ' TObject = class',
  31236. ' constructor Create;',
  31237. ' end;',
  31238. ' EAssertionFailed = class',
  31239. ' constructor Create(s: string);',
  31240. ' end;',
  31241. '']),
  31242. LinesToStr([
  31243. 'constructor TObject.Create;',
  31244. 'begin end;',
  31245. 'constructor EAssertionFailed.Create(s: string);',
  31246. 'begin end;',
  31247. '']) );
  31248. StartProgram(true);
  31249. Add([
  31250. 'uses sysutils;',
  31251. 'procedure DoIt;',
  31252. 'var',
  31253. ' b: boolean;',
  31254. ' s: string;',
  31255. 'begin',
  31256. ' {$Assertions on}',
  31257. ' Assert(b);',
  31258. ' Assert(b,''msg'');',
  31259. 'end;',
  31260. 'begin',
  31261. ' DoIt;',
  31262. '']);
  31263. ConvertProgram;
  31264. CheckSource('TestAssert_SysUtils',
  31265. LinesToStr([ // statements
  31266. 'this.DoIt = function () {',
  31267. ' var b = false;',
  31268. ' var s = "";',
  31269. ' if (!b) throw pas.SysUtils.EAssertionFailed.$create("Create");',
  31270. ' if (!b) throw pas.SysUtils.EAssertionFailed.$create("Create$1", ["msg"]);',
  31271. '};',
  31272. '']),
  31273. LinesToStr([ // $mod.$main
  31274. '$mod.DoIt();',
  31275. '']));
  31276. end;
  31277. procedure TTestModule.TestObjectChecks;
  31278. begin
  31279. Scanner.CurrentBoolSwitches:=Scanner.CurrentBoolSwitches+[bsObjectChecks];
  31280. StartProgram(false);
  31281. Add([
  31282. 'type',
  31283. ' TObject = class',
  31284. ' procedure DoIt;',
  31285. ' end;',
  31286. ' TClass = class of tobject;',
  31287. ' TBird = class',
  31288. ' end;',
  31289. ' TBirdClass = class of TBird;',
  31290. 'var',
  31291. ' o : TObject;',
  31292. ' c: TClass;',
  31293. ' b: TBird;',
  31294. ' bc: TBirdClass;',
  31295. 'procedure TObject.DoIt;',
  31296. 'begin',
  31297. ' b:=TBird(o);',
  31298. 'end;',
  31299. 'begin',
  31300. ' o.DoIt;',
  31301. ' b:=TBird(o);',
  31302. ' bc:=TBirdClass(c);',
  31303. '']);
  31304. ConvertProgram;
  31305. CheckSource('TestCheckMethodCall',
  31306. LinesToStr([ // statements
  31307. 'rtl.createClass(this, "TObject", null, function () {',
  31308. ' this.$init = function () {',
  31309. ' };',
  31310. ' this.$final = function () {',
  31311. ' };',
  31312. ' this.DoIt = function () {',
  31313. ' rtl.checkMethodCall(this,$mod.TObject);',
  31314. ' $mod.b = rtl.asExt($mod.o, $mod.TBird, 1);',
  31315. ' };',
  31316. '});',
  31317. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  31318. '});',
  31319. 'this.o = null;',
  31320. 'this.c = null;',
  31321. 'this.b = null;',
  31322. 'this.bc = null;',
  31323. '']),
  31324. LinesToStr([ // $mod.$main
  31325. '$mod.o.DoIt();',
  31326. '$mod.b = rtl.asExt($mod.o,$mod.TBird, 1);',
  31327. '$mod.bc = rtl.asExt($mod.c, $mod.TBird, 2);',
  31328. '']));
  31329. end;
  31330. procedure TTestModule.TestOverflowChecks_Int;
  31331. begin
  31332. Scanner.CurrentBoolSwitches:=Scanner.CurrentBoolSwitches+[bsOverflowChecks];
  31333. StartProgram(false);
  31334. Add([
  31335. 'procedure DoIt;',
  31336. 'var',
  31337. ' b: byte;',
  31338. ' n: nativeint;',
  31339. ' u: nativeuint;',
  31340. ' c: currency;',
  31341. 'begin',
  31342. ' n:=n+n;',
  31343. ' n:=n-n;',
  31344. ' n:=n+b;',
  31345. ' n:=b-n;',
  31346. ' n:=n*n;',
  31347. ' n:=n*u;',
  31348. ' c:=c+b;',
  31349. ' c:=b+c;',
  31350. ' c:=c*b;',
  31351. ' c:=b*c;',
  31352. 'end;',
  31353. 'begin',
  31354. '']);
  31355. ConvertProgram;
  31356. CheckSource('TestOverflowChecks_Int',
  31357. LinesToStr([ // statements
  31358. 'this.DoIt = function () {',
  31359. ' var b = 0;',
  31360. ' var n = 0;',
  31361. ' var u = 0;',
  31362. ' var c = 0;',
  31363. ' n = rtl.oc(n + n);',
  31364. ' n = rtl.oc(n - n);',
  31365. ' n = rtl.oc(n + b);',
  31366. ' n = rtl.oc(b - n);',
  31367. ' n = rtl.oc(n * n);',
  31368. ' n = rtl.oc(n * u);',
  31369. ' c = rtl.oc(c + (b * 10000));',
  31370. ' c = rtl.oc((b * 10000) + c);',
  31371. ' c = rtl.oc(c * b);',
  31372. ' c = rtl.oc(b * c);',
  31373. '};',
  31374. '']),
  31375. LinesToStr([ // $mod.$main
  31376. '']));
  31377. end;
  31378. procedure TTestModule.TestRangeChecks_AssignInt;
  31379. begin
  31380. Scanner.Options:=Scanner.Options+[po_CAssignments];
  31381. StartProgram(false);
  31382. Add([
  31383. '{$R+}',
  31384. 'var',
  31385. ' b: byte = 2;',
  31386. ' w: word = 3;',
  31387. 'procedure DoIt(p: byte);',
  31388. 'begin',
  31389. ' b:=w;',
  31390. ' b+=w;',
  31391. ' b:=1;',
  31392. 'end;',
  31393. '{$R-}',
  31394. 'procedure DoSome;',
  31395. 'begin',
  31396. ' DoIt(w);',
  31397. ' b:=w;',
  31398. ' b:=2;',
  31399. 'end;',
  31400. 'begin',
  31401. '{$R+}',
  31402. '']);
  31403. ConvertProgram;
  31404. CheckSource('TestRangeChecks_AssignInt',
  31405. LinesToStr([ // statements
  31406. 'this.b = 2;',
  31407. 'this.w = 3;',
  31408. 'this.DoIt = function (p) {',
  31409. ' rtl.rc(p, 0, 255);',
  31410. ' $mod.b = rtl.rc($mod.w,0,255);',
  31411. ' rtl.rc($mod.b += $mod.w, 0, 255);',
  31412. ' $mod.b = 1;',
  31413. '};',
  31414. 'this.DoSome = function () {',
  31415. ' $mod.DoIt($mod.w);',
  31416. ' $mod.b = $mod.w;',
  31417. ' $mod.b = 2;',
  31418. '};',
  31419. '']),
  31420. LinesToStr([ // $mod.$main
  31421. '']));
  31422. end;
  31423. procedure TTestModule.TestRangeChecks_AssignIntRange;
  31424. begin
  31425. Scanner.Options:=Scanner.Options+[po_CAssignments];
  31426. StartProgram(false);
  31427. Add([
  31428. '{$R+}',
  31429. 'type Ten = 1..10;',
  31430. 'var',
  31431. ' b: Ten = 2;',
  31432. ' w: Ten = 3;',
  31433. 'procedure DoIt(p: Ten);',
  31434. 'begin',
  31435. ' b:=w;',
  31436. ' b+=w;',
  31437. ' b:=1;',
  31438. 'end;',
  31439. '{$R-}',
  31440. 'procedure DoSome;',
  31441. 'begin',
  31442. ' DoIt(w);',
  31443. ' b:=w;',
  31444. ' b:=2;',
  31445. 'end;',
  31446. 'begin',
  31447. '{$R+}',
  31448. '']);
  31449. ConvertProgram;
  31450. CheckSource('TestRangeChecks_AssignIntRange',
  31451. LinesToStr([ // statements
  31452. 'this.b = 2;',
  31453. 'this.w = 3;',
  31454. 'this.DoIt = function (p) {',
  31455. ' rtl.rc(p, 1, 10);',
  31456. ' $mod.b = rtl.rc($mod.w, 1, 10);',
  31457. ' rtl.rc($mod.b += $mod.w, 1, 10);',
  31458. ' $mod.b = 1;',
  31459. '};',
  31460. 'this.DoSome = function () {',
  31461. ' $mod.DoIt($mod.w);',
  31462. ' $mod.b = $mod.w;',
  31463. ' $mod.b = 2;',
  31464. '};',
  31465. '']),
  31466. LinesToStr([ // $mod.$main
  31467. '']));
  31468. end;
  31469. procedure TTestModule.TestRangeChecks_AssignEnum;
  31470. begin
  31471. StartProgram(false);
  31472. Add([
  31473. '{$R+}',
  31474. 'type TEnum = (red,green);',
  31475. 'var',
  31476. ' e: TEnum = red;',
  31477. 'procedure DoIt(p: TEnum);',
  31478. 'begin',
  31479. ' e:=p;',
  31480. ' p:=TEnum(0);',
  31481. ' p:=succ(e);',
  31482. 'end;',
  31483. '{$R-}',
  31484. 'procedure DoSome;',
  31485. 'begin',
  31486. ' DoIt(e);',
  31487. ' e:=TEnum(1);',
  31488. ' e:=pred(e);',
  31489. 'end;',
  31490. 'begin',
  31491. '{$R+}',
  31492. '']);
  31493. ConvertProgram;
  31494. CheckSource('TestRangeChecks_AssignEnum',
  31495. LinesToStr([ // statements
  31496. 'this.TEnum = {',
  31497. ' "0": "red",',
  31498. ' red: 0,',
  31499. ' "1": "green",',
  31500. ' green: 1',
  31501. '};',
  31502. 'this.e = this.TEnum.red;',
  31503. 'this.DoIt = function (p) {',
  31504. ' rtl.rc(p, 0, 1);',
  31505. ' $mod.e = rtl.rc(p, 0, 1);',
  31506. ' p = 0;',
  31507. ' p = rtl.rc($mod.e + 1, 0, 1);',
  31508. '};',
  31509. 'this.DoSome = function () {',
  31510. ' $mod.DoIt($mod.e);',
  31511. ' $mod.e = 1;',
  31512. ' $mod.e = $mod.e - 1;',
  31513. '};',
  31514. '']),
  31515. LinesToStr([ // $mod.$main
  31516. '']));
  31517. end;
  31518. procedure TTestModule.TestRangeChecks_AssignEnumRange;
  31519. begin
  31520. StartProgram(false);
  31521. Add([
  31522. '{$R+}',
  31523. 'type',
  31524. ' TEnum = (red,green);',
  31525. ' TEnumRg = red..green;',
  31526. 'var',
  31527. ' e: TEnumRg = red;',
  31528. 'procedure DoIt(p: TEnumRg);',
  31529. 'begin',
  31530. ' e:=p;',
  31531. ' p:=TEnumRg(0);',
  31532. ' p:=succ(e);',
  31533. 'end;',
  31534. '{$R-}',
  31535. 'procedure DoSome;',
  31536. 'begin',
  31537. ' DoIt(e);',
  31538. ' e:=TEnum(1);',
  31539. ' e:=pred(e);',
  31540. 'end;',
  31541. 'begin',
  31542. '{$R+}',
  31543. '']);
  31544. ConvertProgram;
  31545. CheckSource('TestRangeChecks_AssignEnumRange',
  31546. LinesToStr([ // statements
  31547. 'this.TEnum = {',
  31548. ' "0": "red",',
  31549. ' red: 0,',
  31550. ' "1": "green",',
  31551. ' green: 1',
  31552. '};',
  31553. 'this.e = this.TEnum.red;',
  31554. 'this.DoIt = function (p) {',
  31555. ' rtl.rc(p, 0, 1);',
  31556. ' $mod.e = rtl.rc(p, 0, 1);',
  31557. ' p = 0;',
  31558. ' p = rtl.rc($mod.e + 1, 0, 1);',
  31559. '};',
  31560. 'this.DoSome = function () {',
  31561. ' $mod.DoIt($mod.e);',
  31562. ' $mod.e = 1;',
  31563. ' $mod.e = $mod.e - 1;',
  31564. '};',
  31565. '']),
  31566. LinesToStr([ // $mod.$main
  31567. '']));
  31568. end;
  31569. procedure TTestModule.TestRangeChecks_AssignChar;
  31570. begin
  31571. StartProgram(false);
  31572. Add([
  31573. '{$R+}',
  31574. 'type',
  31575. ' TLetter = char;',
  31576. 'var',
  31577. ' b: TLetter = ''2'';',
  31578. ' w: TLetter = ''3'';',
  31579. 'procedure DoIt(p: TLetter);',
  31580. 'begin',
  31581. ' b:=w;',
  31582. ' b:=''1'';',
  31583. 'end;',
  31584. '{$R-}',
  31585. 'procedure DoSome;',
  31586. 'begin',
  31587. ' DoIt(w);',
  31588. ' b:=w;',
  31589. ' b:=''2'';',
  31590. 'end;',
  31591. 'begin',
  31592. '{$R+}',
  31593. '']);
  31594. ConvertProgram;
  31595. CheckSource('TestRangeChecks_AssignChar',
  31596. LinesToStr([ // statements
  31597. 'this.b = "2";',
  31598. 'this.w = "3";',
  31599. 'this.DoIt = function (p) {',
  31600. ' rtl.rcc(p, 0, 65535);',
  31601. ' $mod.b = rtl.rcc($mod.w, 0, 65535);',
  31602. ' $mod.b = "1";',
  31603. '};',
  31604. 'this.DoSome = function () {',
  31605. ' $mod.DoIt($mod.w);',
  31606. ' $mod.b = $mod.w;',
  31607. ' $mod.b = "2";',
  31608. '};',
  31609. '']),
  31610. LinesToStr([ // $mod.$main
  31611. '']));
  31612. end;
  31613. procedure TTestModule.TestRangeChecks_AssignCharRange;
  31614. begin
  31615. StartProgram(false);
  31616. Add([
  31617. '{$R+}',
  31618. 'type TDigit = ''0''..''9'';',
  31619. 'var',
  31620. ' b: TDigit = ''2'';',
  31621. ' w: TDigit = ''3'';',
  31622. 'procedure DoIt(p: TDigit);',
  31623. 'begin',
  31624. ' b:=w;',
  31625. ' b:=''1'';',
  31626. 'end;',
  31627. '{$R-}',
  31628. 'procedure DoSome;',
  31629. 'begin',
  31630. ' DoIt(w);',
  31631. ' b:=w;',
  31632. ' b:=''2'';',
  31633. 'end;',
  31634. 'begin',
  31635. '{$R+}',
  31636. '']);
  31637. ConvertProgram;
  31638. CheckSource('TestRangeChecks_AssignCharRange',
  31639. LinesToStr([ // statements
  31640. 'this.b = "2";',
  31641. 'this.w = "3";',
  31642. 'this.DoIt = function (p) {',
  31643. ' rtl.rcc(p, 48, 57);',
  31644. ' $mod.b = rtl.rcc($mod.w, 48, 57);',
  31645. ' $mod.b = "1";',
  31646. '};',
  31647. 'this.DoSome = function () {',
  31648. ' $mod.DoIt($mod.w);',
  31649. ' $mod.b = $mod.w;',
  31650. ' $mod.b = "2";',
  31651. '};',
  31652. '']),
  31653. LinesToStr([ // $mod.$main
  31654. '']));
  31655. end;
  31656. procedure TTestModule.TestRangeChecks_ArrayIndex;
  31657. begin
  31658. StartProgram(false);
  31659. Add([
  31660. '{$R+}',
  31661. 'type',
  31662. ' Ten = 1..10;',
  31663. ' TArr = array of Ten;',
  31664. ' TArrArr = array of TArr;',
  31665. ' TArrByte = array[byte] of Ten;',
  31666. ' TArrChar = array[''0''..''9''] of Ten;',
  31667. ' TArrByteChar = array[byte,''0''..''9''] of Ten;',
  31668. ' TObject = class',
  31669. ' A: TArr;',
  31670. ' end;',
  31671. 'procedure DoIt;',
  31672. 'var',
  31673. ' Arr: TArr;',
  31674. ' ArrArr: TArrArr;',
  31675. ' ArrByte: TArrByte;',
  31676. ' ArrChar: TArrChar;',
  31677. ' ArrByteChar: TArrByteChar;',
  31678. ' i: Ten;',
  31679. ' c: char;',
  31680. ' o: tobject;',
  31681. 'begin',
  31682. ' i:=Arr[1];',
  31683. ' i:=ArrByteChar[1,''2''];',
  31684. ' Arr[1]:=Arr[1];',
  31685. ' Arr[i]:=Arr[i];',
  31686. ' ArrByte[3]:=ArrByte[3];',
  31687. ' ArrByte[i]:=ArrByte[i];',
  31688. ' ArrChar[''5'']:=ArrChar[''5''];',
  31689. ' ArrChar[c]:=ArrChar[c];',
  31690. ' ArrByteChar[7,''7'']:=ArrByteChar[7,''7''];',
  31691. ' ArrByteChar[i,c]:=ArrByteChar[i,c];',
  31692. ' o.a[i]:=o.a[i];',
  31693. 'end;',
  31694. 'begin',
  31695. '']);
  31696. ConvertProgram;
  31697. CheckSource('TestRangeChecks_ArrayIndex',
  31698. LinesToStr([ // statements
  31699. 'rtl.createClass(this, "TObject", null, function () {',
  31700. ' this.$init = function () {',
  31701. ' this.A = [];',
  31702. ' };',
  31703. ' this.$final = function () {',
  31704. ' this.A = undefined;',
  31705. ' };',
  31706. '});',
  31707. 'this.DoIt = function () {',
  31708. ' var Arr = [];',
  31709. ' var ArrArr = [];',
  31710. ' var ArrByte = rtl.arraySetLength(null, 0, 256);',
  31711. ' var ArrChar = rtl.arraySetLength(null, 0, 10);',
  31712. ' var ArrByteChar = rtl.arraySetLength(null, 0, 256, 10);',
  31713. ' var i = 0;',
  31714. ' var c = "";',
  31715. ' var o = null;',
  31716. ' i = rtl.rc(Arr[1], 1, 10);',
  31717. ' i = rtl.rc(ArrByteChar[1][2], 1, 10);',
  31718. ' Arr[1] = rtl.rc(Arr[1], 1, 10);',
  31719. ' rtl.rcArrW(Arr, i, rtl.rcArrR(Arr, i));',
  31720. ' ArrByte[3] = rtl.rc(ArrByte[3], 1, 10);',
  31721. ' rtl.rcArrW(ArrByte, i, rtl.rcArrR(ArrByte, i));',
  31722. ' ArrChar[5] = rtl.rc(ArrChar[5], 1, 10);',
  31723. ' rtl.rcArrW(ArrChar, c.charCodeAt() - 48, rtl.rcArrR(ArrChar, c.charCodeAt() - 48));',
  31724. ' ArrByteChar[7][7] = rtl.rc(ArrByteChar[7][7], 1, 10);',
  31725. ' rtl.rcArrW(ArrByteChar, i, c.charCodeAt() - 48, rtl.rcArrR(ArrByteChar, i, c.charCodeAt() - 48));',
  31726. ' rtl.rcArrW(o.A, i, rtl.rcArrR(o.A, i));',
  31727. '};',
  31728. '']),
  31729. LinesToStr([ // $mod.$main
  31730. '']));
  31731. end;
  31732. procedure TTestModule.TestRangeChecks_ArrayOfRecIndex;
  31733. begin
  31734. StartProgram(false);
  31735. Add([
  31736. '{$R+}',
  31737. 'type',
  31738. ' Ten = 1..10;',
  31739. ' TRec = record x: Ten end;',
  31740. ' TArr = array of TRec;',
  31741. ' TArrArr = array of TArr;',
  31742. ' TObject = class',
  31743. ' A: TArr;',
  31744. ' end;',
  31745. 'procedure DoIt;',
  31746. 'var',
  31747. ' Arr: TArr;',
  31748. ' ArrArr: TArrArr;',
  31749. ' i: Ten;',
  31750. ' o: tobject;',
  31751. 'begin',
  31752. ' Arr[1]:=Arr[1];',
  31753. ' Arr[i]:=Arr[i+1];',
  31754. ' o.a[i]:=o.a[i+2];',
  31755. 'end;',
  31756. 'begin',
  31757. '']);
  31758. ConvertProgram;
  31759. CheckSource('TestRangeChecks_ArrayOfRecIndex',
  31760. LinesToStr([ // statements
  31761. 'rtl.recNewT(this, "TRec", function () {',
  31762. ' this.x = 0;',
  31763. ' this.$eq = function (b) {',
  31764. ' return this.x === b.x;',
  31765. ' };',
  31766. ' this.$assign = function (s) {',
  31767. ' this.x = s.x;',
  31768. ' return this;',
  31769. ' };',
  31770. '});',
  31771. 'rtl.createClass(this, "TObject", null, function () {',
  31772. ' this.$init = function () {',
  31773. ' this.A = [];',
  31774. ' };',
  31775. ' this.$final = function () {',
  31776. ' this.A = undefined;',
  31777. ' };',
  31778. '});',
  31779. 'this.DoIt = function () {',
  31780. ' var Arr = [];',
  31781. ' var ArrArr = [];',
  31782. ' var i = 0;',
  31783. ' var o = null;',
  31784. ' Arr[1].$assign(Arr[1]);',
  31785. ' rtl.rcArrR(Arr, i).$assign(rtl.rcArrR(Arr, i + 1));',
  31786. ' rtl.rcArrR(o.A, i).$assign(rtl.rcArrR(o.A, i + 2));',
  31787. '};',
  31788. '']),
  31789. LinesToStr([ // $mod.$main
  31790. '']));
  31791. end;
  31792. procedure TTestModule.TestRangeChecks_StringIndex;
  31793. begin
  31794. StartProgram(false);
  31795. Add([
  31796. 'type',
  31797. ' TObject = class',
  31798. ' S: string;',
  31799. ' end;',
  31800. '{$R+}',
  31801. 'procedure DoIt(var h: string);',
  31802. 'var',
  31803. ' s: string;',
  31804. ' i: longint;',
  31805. ' c: char;',
  31806. ' o: tobject;',
  31807. 'begin',
  31808. ' c:=s[1];',
  31809. ' s[i]:=s[i];',
  31810. ' h[i]:=h[i];',
  31811. ' c:=o.s[i];',
  31812. ' o.s[i]:=c;',
  31813. 'end;',
  31814. 'begin',
  31815. '']);
  31816. ConvertProgram;
  31817. CheckSource('TestRangeChecks_StringIndex',
  31818. LinesToStr([ // statements
  31819. 'rtl.createClass(this, "TObject", null, function () {',
  31820. ' this.$init = function () {',
  31821. ' this.S = "";',
  31822. ' };',
  31823. ' this.$final = function () {',
  31824. ' };',
  31825. '});',
  31826. 'this.DoIt = function (h) {',
  31827. ' var s = "";',
  31828. ' var i = 0;',
  31829. ' var c = "";',
  31830. ' var o = null;',
  31831. ' c = rtl.rcc(rtl.rcCharAt(s, 0), 0, 65535);',
  31832. ' s = rtl.rcSetCharAt(s, i - 1, rtl.rcCharAt(s, i - 1));',
  31833. ' h.set(rtl.rcSetCharAt(h.get(), i - 1, rtl.rcCharAt(h.get(), i - 1)));',
  31834. ' c = rtl.rcc(rtl.rcCharAt(o.S, i - 1), 0, 65535);',
  31835. ' o.S = rtl.rcSetCharAt(o.S, i - 1, c);',
  31836. '};',
  31837. '']),
  31838. LinesToStr([ // $mod.$main
  31839. '']));
  31840. end;
  31841. procedure TTestModule.TestRangeChecks_TypecastInt;
  31842. begin
  31843. StartProgram(false);
  31844. Add([
  31845. '{$R+}',
  31846. 'var',
  31847. ' i: nativeint;',
  31848. ' b: byte;',
  31849. ' sh: shortint;',
  31850. ' w: word;',
  31851. ' sm: smallint;',
  31852. ' lw: longword;',
  31853. ' li: longint;',
  31854. 'begin',
  31855. ' b:=12+byte(i);',
  31856. ' sh:=12+shortint(i);',
  31857. ' w:=12+word(i);',
  31858. ' sm:=12+smallint(i);',
  31859. ' lw:=12+longword(i);',
  31860. ' li:=12+longint(i);',
  31861. '']);
  31862. ConvertProgram;
  31863. CheckSource('TestRangeChecks_TypecastInt',
  31864. LinesToStr([
  31865. 'this.i = 0;',
  31866. 'this.b = 0;',
  31867. 'this.sh = 0;',
  31868. 'this.w = 0;',
  31869. 'this.sm = 0;',
  31870. 'this.lw = 0;',
  31871. 'this.li = 0;',
  31872. '']),
  31873. LinesToStr([
  31874. '$mod.b = rtl.rc(12 + rtl.rc($mod.i, 0, 255), 0, 255);',
  31875. '$mod.sh = rtl.rc(12 + rtl.rc($mod.i, -128, 127), -128, 127);',
  31876. '$mod.w = rtl.rc(12 + rtl.rc($mod.i, 0, 65535), 0, 65535);',
  31877. '$mod.sm = rtl.rc(12 + rtl.rc($mod.i, -32768, 32767), -32768, 32767);',
  31878. '$mod.lw = rtl.rc(12 + rtl.rc($mod.i, 0, 4294967295), 0, 4294967295);',
  31879. '$mod.li = rtl.rc(12 + rtl.rc($mod.i, -2147483648, 2147483647), -2147483648, 2147483647);',
  31880. '']));
  31881. end;
  31882. procedure TTestModule.TestRangeChecks_TypeHelperInt;
  31883. begin
  31884. Scanner.Options:=Scanner.Options+[po_CAssignments];
  31885. StartProgram(false);
  31886. Add([
  31887. '{$modeswitch typehelpers}',
  31888. '{$R+}',
  31889. 'type',
  31890. ' TObject = class',
  31891. ' FSize: byte;',
  31892. ' property Size: byte read FSize;',
  31893. ' end;',
  31894. ' THelper = type helper for byte',
  31895. ' procedure SetIt(w: word);',
  31896. ' end;',
  31897. 'procedure THelper.SetIt(w: word);',
  31898. 'begin',
  31899. ' Self:=w;',
  31900. 'end;',
  31901. 'function GetIt: byte;',
  31902. 'begin',
  31903. ' Result.SetIt(2);',
  31904. 'end;',
  31905. 'var',
  31906. ' b: byte = 3;',
  31907. ' o: TObject;',
  31908. 'begin',
  31909. ' b.SetIt(14);',
  31910. ' with b do SetIt(15);',
  31911. ' o.Size.SetIt(16);',
  31912. '']);
  31913. ConvertProgram;
  31914. CheckSource('TestRangeChecks_AssignInt',
  31915. LinesToStr([ // statements
  31916. 'rtl.createClass(this, "TObject", null, function () {',
  31917. ' this.$init = function () {',
  31918. ' this.FSize = 0;',
  31919. ' };',
  31920. ' this.$final = function () {',
  31921. ' };',
  31922. '});',
  31923. 'rtl.createHelper(this, "THelper", null, function () {',
  31924. ' this.SetIt = function (w) {',
  31925. ' rtl.rc(w, 0, 65535);',
  31926. ' this.set(w);',
  31927. ' };',
  31928. '});',
  31929. 'this.GetIt = function () {',
  31930. ' var Result = 0;',
  31931. ' $mod.THelper.SetIt.call({',
  31932. ' get: function () {',
  31933. ' return Result;',
  31934. ' },',
  31935. ' set: function (v) {',
  31936. ' rtl.rc(v, 0, 255);',
  31937. ' Result = v;',
  31938. ' }',
  31939. ' }, 2);',
  31940. ' return Result;',
  31941. '};',
  31942. 'this.b = 3;',
  31943. 'this.o = null;',
  31944. '']),
  31945. LinesToStr([ // $mod.$main
  31946. '$mod.THelper.SetIt.call({',
  31947. ' p: $mod,',
  31948. ' get: function () {',
  31949. ' return this.p.b;',
  31950. ' },',
  31951. ' set: function (v) {',
  31952. ' rtl.rc(v, 0, 255);',
  31953. ' this.p.b = v;',
  31954. ' }',
  31955. '}, 14);',
  31956. 'var $with = $mod.b;',
  31957. '$mod.THelper.SetIt.call({',
  31958. ' get: function () {',
  31959. ' return $with;',
  31960. ' },',
  31961. ' set: function (v) {',
  31962. ' rtl.rc(v, 0, 255);',
  31963. ' $with = v;',
  31964. ' }',
  31965. '}, 15);',
  31966. '$mod.THelper.SetIt.call({',
  31967. ' p: $mod.o,',
  31968. ' get: function () {',
  31969. ' return this.p.FSize;',
  31970. ' },',
  31971. ' set: function (v) {',
  31972. ' rtl.rc(v, 0, 255);',
  31973. ' this.p.FSize = v;',
  31974. ' }',
  31975. '}, 16);',
  31976. '']));
  31977. end;
  31978. procedure TTestModule.TestAsync_Proc;
  31979. begin
  31980. StartProgram(false);
  31981. Add([
  31982. 'procedure Fly(w: word = 1); async; forward;',
  31983. 'procedure Run(w: word = 2); async;',
  31984. 'begin',
  31985. ' Fly(w);',
  31986. ' Fly;',
  31987. ' await(Fly(w));',
  31988. ' await(Fly);',
  31989. 'end;',
  31990. 'procedure Fly(w: word); ',
  31991. 'begin',
  31992. 'end;',
  31993. 'begin',
  31994. ' Run;',
  31995. ' Run(3);',
  31996. '']);
  31997. CheckResolverUnexpectedHints();
  31998. ConvertProgram;
  31999. CheckSource('TestAsync_Proc',
  32000. LinesToStr([ // statements
  32001. 'this.Run = async function (w) {',
  32002. ' $mod.Fly(w);',
  32003. ' $mod.Fly(1);',
  32004. ' await $mod.Fly(w);',
  32005. ' await $mod.Fly(1);',
  32006. '};',
  32007. 'this.Fly = async function (w) {',
  32008. '};',
  32009. '']),
  32010. LinesToStr([
  32011. '$mod.Run(2);',
  32012. '$mod.Run(3);',
  32013. '']));
  32014. end;
  32015. procedure TTestModule.TestAsync_CallResultIsPromise;
  32016. begin
  32017. StartProgram(false);
  32018. Add([
  32019. '{$modeswitch externalclass}',
  32020. 'type',
  32021. ' TObject = class',
  32022. ' end;',
  32023. ' TJSPromise = class external name ''Promise''',
  32024. ' end;',
  32025. ' TBird = class',
  32026. ' function Fly: word; async; ',
  32027. ' end;',
  32028. 'function TBird.Fly: word; async; ',
  32029. 'begin',
  32030. ' Result:=3;',
  32031. ' Fly:=4+Result;',
  32032. ' if Result=5 then ;',
  32033. ' exit(6);',
  32034. 'end;',
  32035. 'function Run: word; async;',
  32036. 'begin',
  32037. ' Result:=11+Result;',
  32038. ' inc(Result);',
  32039. 'end;',
  32040. 'var',
  32041. ' p: TJSPromise;',
  32042. ' o: TBird;',
  32043. 'begin',
  32044. ' p:=Run;',
  32045. ' p:=Run();',
  32046. ' if Run=p then ;',
  32047. ' if p=Run then ;',
  32048. ' if Run()=p then ;',
  32049. ' if p=Run() then ;',
  32050. ' p:=o.Fly;',
  32051. ' p:=o.Fly();',
  32052. ' if o.Fly=p then ;',
  32053. ' if o.Fly()=p then ;',
  32054. ' with o do begin',
  32055. ' p:=Fly;',
  32056. ' p:=Fly();',
  32057. ' if Fly=p then ;',
  32058. ' if Fly()=p then ;',
  32059. ' end;',
  32060. '']);
  32061. CheckResolverUnexpectedHints();
  32062. ConvertProgram;
  32063. CheckSource('TestAsync_CallResultIsPromise',
  32064. LinesToStr([ // statements
  32065. 'rtl.createClass(this, "TObject", null, function () {',
  32066. ' this.$init = function () {',
  32067. ' };',
  32068. ' this.$final = function () {',
  32069. ' };',
  32070. '});',
  32071. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  32072. ' this.Fly = async function () {',
  32073. ' var Result = 0;',
  32074. ' Result = 3;',
  32075. ' Result = 4 + Result;',
  32076. ' if (Result === 5) ;',
  32077. ' return 6;',
  32078. ' return Result;',
  32079. ' };',
  32080. '});',
  32081. 'this.Run = async function () {',
  32082. ' var Result = 0;',
  32083. ' Result = 11 + Result;',
  32084. ' Result += 1;',
  32085. ' return Result;',
  32086. '};',
  32087. 'this.p = null;',
  32088. 'this.o = null;',
  32089. '']),
  32090. LinesToStr([
  32091. '$mod.p = $mod.Run();',
  32092. '$mod.p = $mod.Run();',
  32093. 'if ($mod.Run() === $mod.p) ;',
  32094. 'if ($mod.p === $mod.Run()) ;',
  32095. 'if ($mod.Run() === $mod.p) ;',
  32096. 'if ($mod.p === $mod.Run()) ;',
  32097. '$mod.p = $mod.o.Fly();',
  32098. '$mod.p = $mod.o.Fly();',
  32099. 'if ($mod.o.Fly() === $mod.p) ;',
  32100. 'if ($mod.o.Fly() === $mod.p) ;',
  32101. 'var $with = $mod.o;',
  32102. '$mod.p = $with.Fly();',
  32103. '$mod.p = $with.Fly();',
  32104. 'if ($with.Fly() === $mod.p) ;',
  32105. 'if ($with.Fly() === $mod.p) ;',
  32106. '']));
  32107. end;
  32108. procedure TTestModule.TestAsync_ConstructorFail;
  32109. begin
  32110. StartProgram(false);
  32111. Add([
  32112. 'type',
  32113. ' TObject = class',
  32114. ' end;',
  32115. ' TBird = class',
  32116. ' constructor Create; async;',
  32117. ' end;',
  32118. 'constructor TBird.Create; async;',
  32119. 'begin',
  32120. 'end;',
  32121. 'begin',
  32122. '']);
  32123. SetExpectedPasResolverError('Invalid constructor modifier async',nInvalidXModifierY);
  32124. ConvertProgram;
  32125. end;
  32126. procedure TTestModule.TestAsync_PropertyGetterFail;
  32127. begin
  32128. StartProgram(false);
  32129. Add([
  32130. 'type',
  32131. ' TObject = class',
  32132. ' end;',
  32133. ' TBird = class',
  32134. ' function GetSize: word; async;',
  32135. ' property Size: word read GetSize;',
  32136. ' end;',
  32137. 'function TBird.GetSize: word; async;',
  32138. 'begin',
  32139. 'end;',
  32140. 'begin',
  32141. '']);
  32142. SetExpectedPasResolverError('Invalid property getter modifier async',nInvalidXModifierY);
  32143. ConvertProgram;
  32144. end;
  32145. procedure TTestModule.TestAwait_NonPromiseWithTypeFail;
  32146. begin
  32147. StartProgram(false);
  32148. Add([
  32149. 'procedure Run; async;',
  32150. 'begin',
  32151. ' await(word,1);',
  32152. 'end;',
  32153. 'begin',
  32154. '']);
  32155. SetExpectedPasResolverError('Incompatible type arg no. 2: Got "Longint", expected "TJSPromise"',nIncompatibleTypeArgNo);
  32156. ConvertProgram;
  32157. end;
  32158. procedure TTestModule.TestAwait_AsyncCallTypeMismatch;
  32159. begin
  32160. StartProgram(false);
  32161. Add([
  32162. 'type',
  32163. ' TObject = class',
  32164. ' end;',
  32165. ' TBird = class',
  32166. ' end;',
  32167. 'function Fly: TObject; async;',
  32168. 'begin',
  32169. 'end;',
  32170. 'procedure Run; async;',
  32171. 'begin',
  32172. ' await(TBird,Fly);',
  32173. 'end;',
  32174. 'begin',
  32175. '']);
  32176. SetExpectedPasResolverError('Incompatible type arg no. 2: Got "TObject", expected "TBird"',nIncompatibleTypeArgNo);
  32177. ConvertProgram;
  32178. end;
  32179. procedure TTestModule.TestAWait_OutsideAsyncFail;
  32180. begin
  32181. StartProgram(false);
  32182. Add([
  32183. 'procedure Crawl(w: double); ',
  32184. 'begin',
  32185. 'end;',
  32186. 'procedure Run(w: double);',
  32187. 'begin',
  32188. ' await(Crawl(w));',
  32189. 'end;',
  32190. 'begin',
  32191. ' Run(1);']);
  32192. SetExpectedPasResolverError(sAWaitOnlyInAsyncProcedure,nAWaitOnlyInAsyncProcedure);
  32193. ConvertProgram;
  32194. end;
  32195. procedure TTestModule.TestAWait_IntegerFail;
  32196. begin
  32197. StartProgram(false);
  32198. Add([
  32199. 'function Run: word;',
  32200. 'begin',
  32201. 'end;',
  32202. 'procedure Fly(w: word); async;',
  32203. 'begin',
  32204. ' await(Run());',
  32205. 'end;',
  32206. 'begin',
  32207. ' Fly(1);']);
  32208. SetExpectedPasResolverError('async function expected, but Result:Word found',nXExpectedButYFound);
  32209. ConvertProgram;
  32210. end;
  32211. procedure TTestModule.TestAWait_ExternalClassPromise;
  32212. begin
  32213. StartProgram(false);
  32214. Add([
  32215. '{$modeswitch externalclass}',
  32216. 'type',
  32217. ' TJSPromise = class external name ''Promise''',
  32218. ' end;',
  32219. ' TJSThenable = class external name ''Thenable''',
  32220. ' end;',
  32221. 'function Fly(w: word): TJSPromise;',
  32222. 'begin',
  32223. 'end;',
  32224. 'function Jump(w: word): word; async;',
  32225. 'begin',
  32226. 'end;',
  32227. 'function Eat(w: word): TJSPromise; async;',
  32228. 'begin',
  32229. 'end;',
  32230. 'function Run(d: double): word; async;',
  32231. 'var',
  32232. ' p: TJSPromise;',
  32233. 'begin',
  32234. ' Result:=await(word,p);', // promise needs type
  32235. ' Result:=await(word,Fly(3));', // promise needs type
  32236. ' Result:=await(Jump(4));', // async non promise must omit the type
  32237. ' Result:=await(word,Jump(5));', // async call can provide fitting type
  32238. ' Result:=await(word,Eat(6));', // promise needs type
  32239. 'end;',
  32240. 'begin',
  32241. '']);
  32242. ConvertProgram;
  32243. CheckSource('TestAWait_ExternalClassPromise',
  32244. LinesToStr([ // statements
  32245. 'this.Fly = function (w) {',
  32246. ' var Result = null;',
  32247. ' return Result;',
  32248. '};',
  32249. 'this.Jump = async function (w) {',
  32250. ' var Result = 0;',
  32251. ' return Result;',
  32252. '};',
  32253. 'this.Eat = async function (w) {',
  32254. ' var Result = null;',
  32255. ' return Result;',
  32256. '};',
  32257. 'this.Run = async function (d) {',
  32258. ' var Result = 0;',
  32259. ' var p = null;',
  32260. ' Result = await p;',
  32261. ' Result = await $mod.Fly(3);',
  32262. ' Result = await $mod.Jump(4);',
  32263. ' Result = await $mod.Jump(5);',
  32264. ' Result = await $mod.Eat(6);',
  32265. ' return Result;',
  32266. '};',
  32267. '']),
  32268. LinesToStr([
  32269. ]));
  32270. CheckResolverUnexpectedHints();
  32271. end;
  32272. procedure TTestModule.TestAWait_JSValue;
  32273. begin
  32274. StartProgram(false);
  32275. Add([
  32276. '{$modeswitch externalclass}',
  32277. 'type',
  32278. ' TJSPromise = class external name ''Promise''',
  32279. ' end;',
  32280. 'function Fly(w: word): jsvalue; async;',
  32281. 'begin',
  32282. 'end;',
  32283. 'function Run(d: jsvalue; var e): word; async;',
  32284. 'begin',
  32285. ' Result:=await(word,d);', // promise needs type
  32286. ' d:=await(Fly(4));', // async non promise must omit the type
  32287. ' Result:=await(word,e);', // promise needs type
  32288. 'end;',
  32289. 'begin',
  32290. '']);
  32291. ConvertProgram;
  32292. CheckSource('TestAWait_JSValue',
  32293. LinesToStr([ // statements
  32294. 'this.Fly = async function (w) {',
  32295. ' var Result = undefined;',
  32296. ' return Result;',
  32297. '};',
  32298. 'this.Run = async function (d, e) {',
  32299. ' var Result = 0;',
  32300. ' Result = await d;',
  32301. ' d = await $mod.Fly(4);',
  32302. ' Result = await e.get();',
  32303. ' return Result;',
  32304. '};',
  32305. '']),
  32306. LinesToStr([
  32307. ]));
  32308. CheckResolverUnexpectedHints();
  32309. end;
  32310. procedure TTestModule.TestAWait_Result;
  32311. begin
  32312. StartProgram(false);
  32313. Add([
  32314. '{$modeswitch externalclass}',
  32315. 'type',
  32316. ' TJSPromise = class external name ''Promise''',
  32317. ' end;',
  32318. 'function Crawl(d: double = 1.3): TJSPromise; ',
  32319. 'begin',
  32320. 'end;',
  32321. 'function Run(d: double = 1.6): word; async;',
  32322. 'begin',
  32323. ' Result:=await(word,Crawl);',
  32324. ' Result:=await(word,Crawl(4.5));',
  32325. ' Result:=await(Run);',
  32326. ' Result:=await(Run(6.7));',
  32327. 'end;',
  32328. 'begin',
  32329. ' Run(1);']);
  32330. ConvertProgram;
  32331. CheckSource('TestAWait_Result',
  32332. LinesToStr([ // statements
  32333. 'this.Crawl = function (d) {',
  32334. ' var Result = null;',
  32335. ' return Result;',
  32336. '};',
  32337. 'this.Run = async function (d) {',
  32338. ' var Result = 0;',
  32339. ' Result = await $mod.Crawl(1.3);',
  32340. ' Result = await $mod.Crawl(4.5);',
  32341. ' Result = await $mod.Run(1.6);',
  32342. ' Result = await $mod.Run(6.7);',
  32343. ' return Result;',
  32344. '};',
  32345. '']),
  32346. LinesToStr([
  32347. '$mod.Run(1);'
  32348. ]));
  32349. CheckResolverUnexpectedHints();
  32350. end;
  32351. procedure TTestModule.TestAWait_ResultPromiseMissingTypeFail;
  32352. begin
  32353. StartProgram(false);
  32354. Add([
  32355. '{$mode objfpc}',
  32356. '{$modeswitch externalclass}',
  32357. 'type',
  32358. ' TJSPromise = class external name ''Promise''',
  32359. ' end;',
  32360. 'function Run: TJSPromise; async;',
  32361. 'begin',
  32362. 'end;',
  32363. 'procedure Fly(w: word); async;',
  32364. 'begin',
  32365. ' await(Run());',
  32366. 'end;',
  32367. 'begin',
  32368. ' Fly(1);']);
  32369. SetExpectedPasResolverError('Wrong number of parameters specified for call to "function await(aType,TJSPromise):aType"',
  32370. nWrongNumberOfParametersForCallTo);
  32371. ConvertProgram;
  32372. end;
  32373. procedure TTestModule.TestAsync_AnonymousProc;
  32374. begin
  32375. StartProgram(false);
  32376. Add([
  32377. '{$mode objfpc}',
  32378. '{$modeswitch externalclass}',
  32379. 'type',
  32380. ' TJSPromise = class external name ''Promise''',
  32381. ' end;',
  32382. 'type',
  32383. ' TFunc = reference to function(x: double): word; async;',
  32384. 'function Crawl(d: double = 1.3): word; async;',
  32385. 'begin',
  32386. 'end;',
  32387. 'var Func: TFunc;',
  32388. 'begin',
  32389. ' Func:=function(c:double):word async begin',
  32390. ' Result:=await(Crawl(c));',
  32391. ' end;',
  32392. ' Func:=function(c:double):word async assembler asm',
  32393. ' end;',
  32394. ' ']);
  32395. ConvertProgram;
  32396. CheckSource('TestAsync_AnonymousProc',
  32397. LinesToStr([ // statements
  32398. 'this.Crawl = async function (d) {',
  32399. ' var Result = 0;',
  32400. ' return Result;',
  32401. '};',
  32402. 'this.Func = null;',
  32403. '']),
  32404. LinesToStr([
  32405. '$mod.Func = async function (c) {',
  32406. ' var Result = 0;',
  32407. ' Result = await $mod.Crawl(c);',
  32408. ' return Result;',
  32409. '};',
  32410. '$mod.Func = async function (c) {',
  32411. '};',
  32412. '']));
  32413. CheckResolverUnexpectedHints();
  32414. end;
  32415. procedure TTestModule.TestAsync_ProcType;
  32416. begin
  32417. StartProgram(false);
  32418. Add([
  32419. '{$mode objfpc}',
  32420. 'type',
  32421. ' TRefFunc = reference to function(x: double = 1.3): word; async;',
  32422. ' TFunc = function(x: double = 1.1): word; async;',
  32423. ' TProc = procedure(x: longint = 7); async;',
  32424. 'function Crawl(d: double): word; async;',
  32425. 'begin',
  32426. 'end;',
  32427. 'procedure Run(e:longint); async;',
  32428. 'begin',
  32429. 'end;',
  32430. 'procedure Fly(p: TProc); async;',
  32431. 'begin',
  32432. ' await(p);',
  32433. ' await(p());',
  32434. 'end;',
  32435. 'var',
  32436. ' RefFunc: TRefFunc;',
  32437. ' Func: TFunc;',
  32438. ' Proc, ProcB: TProc;',
  32439. 'begin',
  32440. ' Func:=@Crawl;',
  32441. ' RefFunc:=@Crawl;',
  32442. ' RefFunc:=function(c:double):word async begin',
  32443. ' Result:=await(RefFunc);',
  32444. ' Result:=await(RefFunc());',
  32445. ' Result:=await(Func);',
  32446. ' Result:=await(Func());',
  32447. ' await(Proc);',
  32448. ' await(Proc());',
  32449. ' await(Proc(13));',
  32450. ' end;',
  32451. ' Proc:=@Run;',
  32452. ' if Proc=ProcB then ;',
  32453. ' ']);
  32454. ConvertProgram;
  32455. CheckResolverUnexpectedHints();
  32456. CheckSource('TestAsync_ProcType',
  32457. LinesToStr([ // statements
  32458. 'this.Crawl = async function (d) {',
  32459. ' var Result = 0;',
  32460. ' return Result;',
  32461. '};',
  32462. 'this.Run = async function (e) {',
  32463. '};',
  32464. 'this.Fly = async function (p) {',
  32465. ' await p(7);',
  32466. ' await p(7);',
  32467. '};',
  32468. 'this.RefFunc = null;',
  32469. 'this.Func = null;',
  32470. 'this.Proc = null;',
  32471. 'this.ProcB = null;',
  32472. '']),
  32473. LinesToStr([
  32474. '$mod.Func = $mod.Crawl;',
  32475. '$mod.RefFunc = $mod.Crawl;',
  32476. '$mod.RefFunc = async function (c) {',
  32477. ' var Result = 0;',
  32478. ' Result = await $mod.RefFunc(1.3);',
  32479. ' Result = await $mod.RefFunc(1.3);',
  32480. ' Result = await $mod.Func(1.1);',
  32481. ' Result = await $mod.Func(1.1);',
  32482. ' await $mod.Proc(7);',
  32483. ' await $mod.Proc(7);',
  32484. ' await $mod.Proc(13);',
  32485. ' return Result;',
  32486. '};',
  32487. '$mod.Proc = $mod.Run;',
  32488. 'if (rtl.eqCallback($mod.Proc, $mod.ProcB)) ;',
  32489. '']));
  32490. end;
  32491. procedure TTestModule.TestAsync_ProcTypeAsyncModMismatchFail;
  32492. begin
  32493. StartProgram(false);
  32494. Add([
  32495. '{$mode objfpc}',
  32496. 'type',
  32497. ' TRefFunc = reference to function(x: double = 1.3): word;',
  32498. 'function Crawl(d: double): word; async;',
  32499. 'begin',
  32500. 'end;',
  32501. 'var',
  32502. ' RefFunc: TRefFunc;',
  32503. 'begin',
  32504. ' RefFunc:=@Crawl;',
  32505. ' ']);
  32506. SetExpectedPasResolverError('procedure type modifier "async" mismatch',nXModifierMismatchY);
  32507. ConvertProgram;
  32508. end;
  32509. procedure TTestModule.TestAsync_Inherited;
  32510. begin
  32511. StartProgram(false);
  32512. Add([
  32513. '{$mode objfpc}',
  32514. '{$modeswitch externalclass}',
  32515. 'type',
  32516. ' TJSPromise = class external name ''Promise''',
  32517. ' end;',
  32518. ' TObject = class',
  32519. ' function Run(w: word = 3): word; async; virtual;',
  32520. ' end;',
  32521. ' TBird = class',
  32522. ' function Run(w: word = 3): word; async; override;',
  32523. ' end;',
  32524. 'function TObject.Run(w: word = 3): word; async;',
  32525. 'begin',
  32526. 'end;',
  32527. 'function TBird.Run(w: word = 3): word;', // async modifier not needed in impl
  32528. 'var p: TJSPromise;',
  32529. 'begin',
  32530. ' p:=inherited;',
  32531. ' p:=inherited Run;',
  32532. ' p:=inherited Run();',
  32533. ' p:=inherited Run(4);',
  32534. ' exit(p);',
  32535. ' exit(inherited);',
  32536. ' exit(inherited Run);',
  32537. ' exit(inherited Run(5));',
  32538. ' exit(6);',
  32539. 'end;',
  32540. 'begin',
  32541. ' ']);
  32542. ConvertProgram;
  32543. CheckSource('TestAsync_Inherited',
  32544. LinesToStr([ // statements
  32545. 'rtl.createClass(this, "TObject", null, function () {',
  32546. ' this.$init = function () {',
  32547. ' };',
  32548. ' this.$final = function () {',
  32549. ' };',
  32550. ' this.Run = async function (w) {',
  32551. ' var Result = 0;',
  32552. ' return Result;',
  32553. ' };',
  32554. '});',
  32555. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  32556. ' this.Run = async function (w) {',
  32557. ' var Result = 0;',
  32558. ' var p = null;',
  32559. ' p = $mod.TObject.Run.apply(this, arguments);',
  32560. ' p = $mod.TObject.Run.call(this, 3);',
  32561. ' p = $mod.TObject.Run.call(this, 3);',
  32562. ' p = $mod.TObject.Run.call(this, 4);',
  32563. ' return p;',
  32564. ' return $mod.TObject.Run.apply(this, arguments);',
  32565. ' return $mod.TObject.Run.call(this, 3);',
  32566. ' return $mod.TObject.Run.call(this, 5);',
  32567. ' return 6;',
  32568. ' return Result;',
  32569. ' };',
  32570. '});',
  32571. '']),
  32572. LinesToStr([
  32573. '']));
  32574. CheckResolverUnexpectedHints();
  32575. end;
  32576. procedure TTestModule.TestAsync_ClassInterface;
  32577. begin
  32578. StartProgram(false);
  32579. Add([
  32580. '{$mode objfpc}',
  32581. '{$modeswitch externalclass}',
  32582. 'type',
  32583. ' TJSPromise = class external name ''Promise''',
  32584. ' end;',
  32585. ' IUnknown = interface',
  32586. ' function _AddRef: longint;',
  32587. ' function _Release: longint;',
  32588. ' end;',
  32589. 'function Say(i: IUnknown): IUnknown; async;',
  32590. 'begin',
  32591. 'end;',
  32592. 'function Run: IUnknown; async;',
  32593. 'begin',
  32594. ' Result:=await(Run);',
  32595. ' Result:=await(Run());',
  32596. ' Result:=await(Run) as IUnknown;',
  32597. ' Result:=await(Say(nil));',
  32598. ' Result:=await(Say(await(Run())));',
  32599. ' Result:=await(Say(await(Run()) as IUnknown));',
  32600. ' Result:=await(Say(await(Run()) as IUnknown)) as IUnknown;',
  32601. 'end;',
  32602. 'procedure Fly;',
  32603. 'var p: TJSPromise;',
  32604. 'begin',
  32605. ' Run;',
  32606. ' Run();',
  32607. ' p:=Run;',
  32608. ' p:=Run();',
  32609. 'end;',
  32610. 'begin',
  32611. ' ']);
  32612. ConvertProgram;
  32613. CheckSource('TestAsync_ClassInterface',
  32614. LinesToStr([ // statements
  32615. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  32616. 'this.Say = async function (i) {',
  32617. ' var Result = null;',
  32618. ' return Result;',
  32619. '};',
  32620. 'this.Run = async function () {',
  32621. ' var Result = null;',
  32622. ' var $ok = false;',
  32623. ' try {',
  32624. ' Result = rtl.setIntfL(Result, await $mod.Run());',
  32625. ' Result = rtl.setIntfL(Result, await $mod.Run());',
  32626. ' Result = rtl.setIntfL(Result, rtl.intfAsIntfT(await $mod.Run(), $mod.IUnknown));',
  32627. ' Result = rtl.setIntfL(Result, await $mod.Say(null));',
  32628. ' Result = rtl.setIntfL(Result, await $mod.Say(await $mod.Run()));',
  32629. ' Result = rtl.setIntfL(Result, await $mod.Say(rtl.intfAsIntfT(await $mod.Run(), $mod.IUnknown)));',
  32630. ' Result = rtl.setIntfL(Result, rtl.intfAsIntfT(await $mod.Say(rtl.intfAsIntfT(await $mod.Run(), $mod.IUnknown)), $mod.IUnknown));',
  32631. ' $ok = true;',
  32632. ' } finally {',
  32633. ' if (!$ok) rtl._Release(Result);',
  32634. ' };',
  32635. ' return Result;',
  32636. '};',
  32637. 'this.Fly = function () {',
  32638. ' var p = null;',
  32639. ' $mod.Run();',
  32640. ' $mod.Run();',
  32641. ' p = $mod.Run();',
  32642. ' p = $mod.Run();',
  32643. '};',
  32644. '']),
  32645. LinesToStr([
  32646. '']));
  32647. CheckResolverUnexpectedHints();
  32648. end;
  32649. procedure TTestModule.TestAsync_ClassInterface_AsyncMissmatchFail;
  32650. begin
  32651. StartProgram(true,[supTInterfacedObject]);
  32652. Add([
  32653. '{$mode objfpc}',
  32654. '{$modeswitch externalclass}',
  32655. 'type',
  32656. ' TJSPromise = class external name ''Promise''',
  32657. ' end;',
  32658. ' IBird = interface',
  32659. ' procedure Run;',
  32660. ' end;',
  32661. ' TBird = class(TInterfacedObject,IBird)',
  32662. ' procedure Run; async;',
  32663. ' end;',
  32664. 'procedure TBird.Run;',
  32665. 'begin',
  32666. 'end;',
  32667. 'begin',
  32668. ' ']);
  32669. SetExpectedPasResolverError('procedure type modifier "async" mismatch',nXModifierMismatchY);
  32670. ConvertProgram;
  32671. end;
  32672. Initialization
  32673. RegisterTests([TTestModule]);
  32674. end.