tcmodules.pas 876 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766247672476824769247702477124772247732477424775247762477724778247792478024781247822478324784247852478624787247882478924790247912479224793247942479524796247972479824799248002480124802248032480424805248062480724808248092481024811248122481324814248152481624817248182481924820248212482224823248242482524826248272482824829248302483124832248332483424835248362483724838248392484024841248422484324844248452484624847248482484924850248512485224853248542485524856248572485824859248602486124862248632486424865248662486724868248692487024871248722487324874248752487624877248782487924880248812488224883248842488524886248872488824889248902489124892248932489424895248962489724898248992490024901249022490324904249052490624907249082490924910249112491224913249142491524916249172491824919249202492124922249232492424925249262492724928249292493024931249322493324934249352493624937249382493924940249412494224943249442494524946249472494824949249502495124952249532495424955249562495724958249592496024961249622496324964249652496624967249682496924970249712497224973249742497524976249772497824979249802498124982249832498424985249862498724988249892499024991249922499324994249952499624997249982499925000250012500225003250042500525006250072500825009250102501125012250132501425015250162501725018250192502025021250222502325024250252502625027250282502925030250312503225033250342503525036250372503825039250402504125042250432504425045250462504725048250492505025051250522505325054250552505625057250582505925060250612506225063250642506525066250672506825069250702507125072250732507425075250762507725078250792508025081250822508325084250852508625087250882508925090250912509225093250942509525096250972509825099251002510125102251032510425105251062510725108251092511025111251122511325114251152511625117251182511925120251212512225123251242512525126251272512825129251302513125132251332513425135251362513725138251392514025141251422514325144251452514625147251482514925150251512515225153251542515525156251572515825159251602516125162251632516425165251662516725168251692517025171251722517325174251752517625177251782517925180251812518225183251842518525186251872518825189251902519125192251932519425195251962519725198251992520025201252022520325204252052520625207252082520925210252112521225213252142521525216252172521825219252202522125222252232522425225252262522725228252292523025231252322523325234252352523625237252382523925240252412524225243252442524525246252472524825249252502525125252252532525425255252562525725258252592526025261252622526325264252652526625267252682526925270252712527225273252742527525276252772527825279252802528125282252832528425285252862528725288252892529025291252922529325294252952529625297252982529925300253012530225303253042530525306253072530825309253102531125312253132531425315253162531725318253192532025321253222532325324253252532625327253282532925330253312533225333253342533525336253372533825339253402534125342253432534425345253462534725348253492535025351253522535325354253552535625357253582535925360253612536225363253642536525366253672536825369253702537125372253732537425375253762537725378253792538025381253822538325384253852538625387253882538925390253912539225393253942539525396253972539825399254002540125402254032540425405254062540725408254092541025411254122541325414254152541625417254182541925420254212542225423254242542525426254272542825429254302543125432254332543425435254362543725438254392544025441254422544325444254452544625447254482544925450254512545225453254542545525456254572545825459254602546125462254632546425465254662546725468254692547025471254722547325474254752547625477254782547925480254812548225483254842548525486254872548825489254902549125492254932549425495254962549725498254992550025501255022550325504255052550625507255082550925510255112551225513255142551525516255172551825519255202552125522255232552425525255262552725528255292553025531255322553325534255352553625537255382553925540255412554225543255442554525546255472554825549255502555125552255532555425555255562555725558255592556025561255622556325564255652556625567255682556925570255712557225573255742557525576255772557825579255802558125582255832558425585255862558725588255892559025591255922559325594255952559625597255982559925600256012560225603256042560525606256072560825609256102561125612256132561425615256162561725618256192562025621256222562325624256252562625627256282562925630256312563225633256342563525636256372563825639256402564125642256432564425645256462564725648256492565025651256522565325654256552565625657256582565925660256612566225663256642566525666256672566825669256702567125672256732567425675256762567725678256792568025681256822568325684256852568625687256882568925690256912569225693256942569525696256972569825699257002570125702257032570425705257062570725708257092571025711257122571325714257152571625717257182571925720257212572225723257242572525726257272572825729257302573125732257332573425735257362573725738257392574025741257422574325744257452574625747257482574925750257512575225753257542575525756257572575825759257602576125762257632576425765257662576725768257692577025771257722577325774257752577625777257782577925780257812578225783257842578525786257872578825789257902579125792257932579425795257962579725798257992580025801258022580325804258052580625807258082580925810258112581225813258142581525816258172581825819258202582125822258232582425825258262582725828258292583025831258322583325834258352583625837258382583925840258412584225843258442584525846258472584825849258502585125852258532585425855258562585725858258592586025861258622586325864258652586625867258682586925870258712587225873258742587525876258772587825879258802588125882258832588425885258862588725888258892589025891258922589325894258952589625897258982589925900259012590225903259042590525906259072590825909259102591125912259132591425915259162591725918259192592025921259222592325924259252592625927259282592925930259312593225933259342593525936259372593825939259402594125942259432594425945259462594725948259492595025951259522595325954259552595625957259582595925960259612596225963259642596525966259672596825969259702597125972259732597425975259762597725978259792598025981259822598325984259852598625987259882598925990259912599225993259942599525996259972599825999260002600126002260032600426005260062600726008260092601026011260122601326014260152601626017260182601926020260212602226023260242602526026260272602826029260302603126032260332603426035260362603726038260392604026041260422604326044260452604626047260482604926050260512605226053260542605526056260572605826059260602606126062260632606426065260662606726068260692607026071260722607326074260752607626077260782607926080260812608226083260842608526086260872608826089260902609126092260932609426095260962609726098260992610026101261022610326104261052610626107261082610926110261112611226113261142611526116261172611826119261202612126122261232612426125261262612726128261292613026131261322613326134261352613626137261382613926140261412614226143261442614526146261472614826149261502615126152261532615426155261562615726158261592616026161261622616326164261652616626167261682616926170261712617226173261742617526176261772617826179261802618126182261832618426185261862618726188261892619026191261922619326194261952619626197261982619926200262012620226203262042620526206262072620826209262102621126212262132621426215262162621726218262192622026221262222622326224262252622626227262282622926230262312623226233262342623526236262372623826239262402624126242262432624426245262462624726248262492625026251262522625326254262552625626257262582625926260262612626226263262642626526266262672626826269262702627126272262732627426275262762627726278262792628026281262822628326284262852628626287262882628926290262912629226293262942629526296262972629826299263002630126302263032630426305263062630726308263092631026311263122631326314263152631626317263182631926320263212632226323263242632526326263272632826329263302633126332263332633426335263362633726338263392634026341263422634326344263452634626347263482634926350263512635226353263542635526356263572635826359263602636126362263632636426365263662636726368263692637026371263722637326374263752637626377263782637926380263812638226383263842638526386263872638826389263902639126392263932639426395263962639726398263992640026401264022640326404264052640626407264082640926410264112641226413264142641526416264172641826419264202642126422264232642426425264262642726428264292643026431264322643326434264352643626437264382643926440264412644226443264442644526446264472644826449264502645126452264532645426455264562645726458264592646026461264622646326464264652646626467264682646926470264712647226473264742647526476264772647826479264802648126482264832648426485264862648726488264892649026491264922649326494264952649626497264982649926500265012650226503265042650526506265072650826509265102651126512265132651426515265162651726518265192652026521265222652326524265252652626527265282652926530265312653226533265342653526536265372653826539265402654126542265432654426545265462654726548265492655026551265522655326554265552655626557265582655926560265612656226563265642656526566265672656826569265702657126572265732657426575265762657726578265792658026581265822658326584265852658626587265882658926590265912659226593265942659526596265972659826599266002660126602266032660426605266062660726608266092661026611266122661326614266152661626617266182661926620266212662226623266242662526626266272662826629266302663126632266332663426635266362663726638266392664026641266422664326644266452664626647266482664926650266512665226653266542665526656266572665826659266602666126662266632666426665266662666726668266692667026671266722667326674266752667626677266782667926680266812668226683266842668526686266872668826689266902669126692266932669426695266962669726698266992670026701267022670326704267052670626707267082670926710267112671226713267142671526716267172671826719267202672126722267232672426725267262672726728267292673026731267322673326734267352673626737267382673926740267412674226743267442674526746267472674826749267502675126752267532675426755267562675726758267592676026761267622676326764267652676626767267682676926770267712677226773267742677526776267772677826779267802678126782267832678426785267862678726788267892679026791267922679326794267952679626797267982679926800268012680226803268042680526806268072680826809268102681126812268132681426815268162681726818268192682026821268222682326824268252682626827268282682926830268312683226833268342683526836268372683826839268402684126842268432684426845268462684726848268492685026851268522685326854268552685626857268582685926860268612686226863268642686526866268672686826869268702687126872268732687426875268762687726878268792688026881268822688326884268852688626887268882688926890268912689226893268942689526896268972689826899269002690126902269032690426905269062690726908269092691026911269122691326914269152691626917269182691926920269212692226923269242692526926269272692826929269302693126932269332693426935269362693726938269392694026941269422694326944269452694626947269482694926950269512695226953269542695526956269572695826959269602696126962269632696426965269662696726968269692697026971269722697326974269752697626977269782697926980269812698226983269842698526986269872698826989269902699126992269932699426995269962699726998269992700027001270022700327004270052700627007270082700927010270112701227013270142701527016270172701827019270202702127022270232702427025270262702727028270292703027031270322703327034270352703627037270382703927040270412704227043270442704527046270472704827049270502705127052270532705427055270562705727058270592706027061270622706327064270652706627067270682706927070270712707227073270742707527076270772707827079270802708127082270832708427085270862708727088270892709027091270922709327094270952709627097270982709927100271012710227103271042710527106271072710827109271102711127112271132711427115271162711727118271192712027121271222712327124271252712627127271282712927130271312713227133271342713527136271372713827139271402714127142271432714427145271462714727148271492715027151271522715327154271552715627157271582715927160271612716227163271642716527166271672716827169271702717127172271732717427175271762717727178271792718027181271822718327184271852718627187271882718927190271912719227193271942719527196271972719827199272002720127202272032720427205272062720727208272092721027211272122721327214272152721627217272182721927220272212722227223272242722527226272272722827229272302723127232272332723427235272362723727238272392724027241272422724327244272452724627247272482724927250272512725227253272542725527256272572725827259272602726127262272632726427265272662726727268272692727027271272722727327274272752727627277272782727927280272812728227283272842728527286272872728827289272902729127292272932729427295272962729727298272992730027301273022730327304273052730627307273082730927310273112731227313273142731527316273172731827319273202732127322273232732427325273262732727328273292733027331273322733327334273352733627337273382733927340273412734227343273442734527346273472734827349273502735127352273532735427355273562735727358273592736027361273622736327364273652736627367273682736927370273712737227373273742737527376273772737827379273802738127382273832738427385273862738727388273892739027391273922739327394273952739627397273982739927400274012740227403274042740527406274072740827409274102741127412274132741427415274162741727418274192742027421274222742327424274252742627427274282742927430274312743227433274342743527436274372743827439274402744127442274432744427445274462744727448274492745027451274522745327454274552745627457274582745927460274612746227463274642746527466274672746827469274702747127472274732747427475274762747727478274792748027481274822748327484274852748627487274882748927490274912749227493274942749527496274972749827499275002750127502275032750427505275062750727508275092751027511275122751327514275152751627517275182751927520275212752227523275242752527526275272752827529275302753127532275332753427535275362753727538275392754027541275422754327544275452754627547275482754927550275512755227553275542755527556275572755827559275602756127562275632756427565275662756727568275692757027571275722757327574275752757627577275782757927580275812758227583275842758527586275872758827589275902759127592275932759427595275962759727598275992760027601276022760327604276052760627607276082760927610276112761227613276142761527616276172761827619276202762127622276232762427625276262762727628276292763027631276322763327634276352763627637276382763927640276412764227643276442764527646276472764827649276502765127652276532765427655276562765727658276592766027661276622766327664276652766627667276682766927670276712767227673276742767527676276772767827679276802768127682276832768427685276862768727688276892769027691276922769327694276952769627697276982769927700277012770227703277042770527706277072770827709277102771127712277132771427715277162771727718277192772027721277222772327724277252772627727277282772927730277312773227733277342773527736277372773827739277402774127742277432774427745277462774727748277492775027751277522775327754277552775627757277582775927760277612776227763277642776527766277672776827769277702777127772277732777427775277762777727778277792778027781277822778327784277852778627787277882778927790277912779227793277942779527796277972779827799278002780127802278032780427805278062780727808278092781027811278122781327814278152781627817278182781927820278212782227823278242782527826278272782827829278302783127832278332783427835278362783727838278392784027841278422784327844278452784627847278482784927850278512785227853278542785527856278572785827859278602786127862278632786427865278662786727868278692787027871278722787327874278752787627877278782787927880278812788227883278842788527886278872788827889278902789127892278932789427895278962789727898278992790027901279022790327904279052790627907279082790927910279112791227913279142791527916279172791827919279202792127922279232792427925279262792727928279292793027931279322793327934279352793627937279382793927940279412794227943279442794527946279472794827949279502795127952279532795427955279562795727958279592796027961279622796327964279652796627967279682796927970279712797227973279742797527976279772797827979279802798127982279832798427985279862798727988279892799027991279922799327994279952799627997279982799928000280012800228003280042800528006280072800828009280102801128012280132801428015280162801728018280192802028021280222802328024280252802628027280282802928030280312803228033280342803528036280372803828039280402804128042280432804428045280462804728048280492805028051280522805328054280552805628057280582805928060280612806228063280642806528066280672806828069280702807128072280732807428075280762807728078280792808028081280822808328084280852808628087280882808928090280912809228093280942809528096280972809828099281002810128102281032810428105281062810728108281092811028111281122811328114281152811628117281182811928120281212812228123281242812528126281272812828129281302813128132281332813428135281362813728138281392814028141281422814328144281452814628147281482814928150281512815228153281542815528156281572815828159281602816128162281632816428165281662816728168281692817028171281722817328174281752817628177281782817928180281812818228183281842818528186281872818828189281902819128192281932819428195281962819728198281992820028201282022820328204282052820628207282082820928210282112821228213282142821528216282172821828219282202822128222282232822428225282262822728228282292823028231282322823328234282352823628237282382823928240282412824228243282442824528246282472824828249282502825128252282532825428255282562825728258282592826028261282622826328264282652826628267282682826928270282712827228273282742827528276282772827828279282802828128282282832828428285282862828728288282892829028291282922829328294282952829628297282982829928300283012830228303283042830528306283072830828309283102831128312283132831428315283162831728318283192832028321283222832328324283252832628327283282832928330283312833228333283342833528336283372833828339283402834128342283432834428345283462834728348283492835028351283522835328354283552835628357283582835928360283612836228363283642836528366283672836828369283702837128372283732837428375283762837728378283792838028381283822838328384283852838628387283882838928390283912839228393283942839528396283972839828399284002840128402284032840428405284062840728408284092841028411284122841328414284152841628417284182841928420284212842228423284242842528426284272842828429284302843128432284332843428435284362843728438284392844028441284422844328444284452844628447284482844928450284512845228453284542845528456284572845828459284602846128462284632846428465284662846728468284692847028471284722847328474284752847628477284782847928480284812848228483284842848528486284872848828489284902849128492284932849428495284962849728498284992850028501285022850328504285052850628507285082850928510285112851228513285142851528516285172851828519285202852128522285232852428525285262852728528285292853028531285322853328534285352853628537285382853928540285412854228543285442854528546285472854828549285502855128552285532855428555285562855728558285592856028561285622856328564285652856628567285682856928570285712857228573285742857528576285772857828579285802858128582285832858428585285862858728588285892859028591285922859328594285952859628597285982859928600286012860228603286042860528606286072860828609286102861128612286132861428615286162861728618286192862028621286222862328624286252862628627286282862928630286312863228633286342863528636286372863828639286402864128642286432864428645286462864728648286492865028651286522865328654286552865628657286582865928660286612866228663286642866528666286672866828669286702867128672286732867428675286762867728678286792868028681286822868328684286852868628687286882868928690286912869228693286942869528696286972869828699287002870128702287032870428705287062870728708287092871028711287122871328714287152871628717287182871928720287212872228723287242872528726287272872828729287302873128732287332873428735287362873728738287392874028741287422874328744287452874628747287482874928750287512875228753287542875528756287572875828759287602876128762287632876428765287662876728768287692877028771287722877328774287752877628777287782877928780287812878228783287842878528786287872878828789287902879128792287932879428795287962879728798287992880028801288022880328804288052880628807288082880928810288112881228813288142881528816288172881828819288202882128822288232882428825288262882728828288292883028831288322883328834288352883628837288382883928840288412884228843288442884528846288472884828849288502885128852288532885428855288562885728858288592886028861288622886328864288652886628867288682886928870288712887228873288742887528876288772887828879288802888128882288832888428885288862888728888288892889028891288922889328894288952889628897288982889928900289012890228903289042890528906289072890828909289102891128912289132891428915289162891728918289192892028921289222892328924289252892628927289282892928930289312893228933289342893528936289372893828939289402894128942289432894428945289462894728948289492895028951289522895328954289552895628957289582895928960289612896228963289642896528966289672896828969289702897128972289732897428975289762897728978289792898028981289822898328984289852898628987289882898928990289912899228993289942899528996289972899828999290002900129002290032900429005290062900729008290092901029011290122901329014290152901629017290182901929020290212902229023290242902529026290272902829029290302903129032290332903429035290362903729038290392904029041290422904329044290452904629047290482904929050290512905229053290542905529056290572905829059290602906129062290632906429065290662906729068290692907029071290722907329074290752907629077290782907929080290812908229083290842908529086290872908829089290902909129092290932909429095290962909729098290992910029101291022910329104291052910629107291082910929110291112911229113291142911529116291172911829119291202912129122291232912429125291262912729128291292913029131291322913329134291352913629137291382913929140291412914229143291442914529146291472914829149291502915129152291532915429155291562915729158291592916029161291622916329164291652916629167291682916929170291712917229173291742917529176291772917829179291802918129182291832918429185291862918729188291892919029191291922919329194291952919629197291982919929200292012920229203292042920529206292072920829209292102921129212292132921429215292162921729218292192922029221292222922329224292252922629227292282922929230292312923229233292342923529236292372923829239292402924129242292432924429245292462924729248292492925029251292522925329254292552925629257292582925929260292612926229263292642926529266292672926829269292702927129272292732927429275292762927729278292792928029281292822928329284292852928629287292882928929290292912929229293292942929529296292972929829299293002930129302293032930429305293062930729308293092931029311293122931329314293152931629317293182931929320293212932229323293242932529326293272932829329293302933129332293332933429335293362933729338293392934029341293422934329344293452934629347293482934929350293512935229353293542935529356293572935829359293602936129362293632936429365293662936729368293692937029371293722937329374293752937629377293782937929380293812938229383293842938529386293872938829389293902939129392293932939429395293962939729398293992940029401294022940329404294052940629407294082940929410294112941229413294142941529416294172941829419294202942129422294232942429425294262942729428294292943029431294322943329434294352943629437294382943929440294412944229443294442944529446294472944829449294502945129452294532945429455294562945729458294592946029461294622946329464294652946629467294682946929470294712947229473294742947529476294772947829479294802948129482294832948429485294862948729488294892949029491294922949329494294952949629497294982949929500295012950229503295042950529506295072950829509295102951129512295132951429515295162951729518295192952029521295222952329524295252952629527295282952929530295312953229533295342953529536295372953829539295402954129542295432954429545295462954729548295492955029551295522955329554295552955629557295582955929560295612956229563295642956529566295672956829569295702957129572295732957429575295762957729578295792958029581295822958329584295852958629587295882958929590295912959229593295942959529596295972959829599296002960129602296032960429605296062960729608296092961029611296122961329614296152961629617296182961929620296212962229623296242962529626296272962829629296302963129632296332963429635296362963729638296392964029641296422964329644296452964629647296482964929650296512965229653296542965529656296572965829659296602966129662296632966429665296662966729668296692967029671296722967329674296752967629677296782967929680296812968229683296842968529686296872968829689296902969129692296932969429695296962969729698296992970029701297022970329704297052970629707297082970929710297112971229713297142971529716297172971829719297202972129722297232972429725297262972729728297292973029731297322973329734297352973629737297382973929740297412974229743297442974529746297472974829749297502975129752297532975429755297562975729758297592976029761297622976329764297652976629767297682976929770297712977229773297742977529776297772977829779297802978129782297832978429785297862978729788297892979029791297922979329794297952979629797297982979929800298012980229803298042980529806298072980829809298102981129812298132981429815298162981729818298192982029821298222982329824298252982629827298282982929830298312983229833298342983529836298372983829839298402984129842298432984429845298462984729848298492985029851298522985329854298552985629857298582985929860298612986229863298642986529866298672986829869298702987129872298732987429875298762987729878298792988029881298822988329884298852988629887298882988929890298912989229893298942989529896298972989829899299002990129902299032990429905299062990729908299092991029911299122991329914299152991629917299182991929920299212992229923299242992529926299272992829929299302993129932299332993429935299362993729938299392994029941299422994329944299452994629947299482994929950299512995229953299542995529956299572995829959299602996129962299632996429965299662996729968299692997029971299722997329974299752997629977299782997929980299812998229983299842998529986299872998829989299902999129992299932999429995299962999729998299993000030001300023000330004300053000630007300083000930010300113001230013300143001530016300173001830019300203002130022300233002430025300263002730028300293003030031300323003330034300353003630037300383003930040300413004230043300443004530046300473004830049300503005130052300533005430055300563005730058300593006030061300623006330064300653006630067300683006930070300713007230073300743007530076300773007830079300803008130082300833008430085300863008730088300893009030091300923009330094300953009630097300983009930100301013010230103301043010530106301073010830109301103011130112301133011430115301163011730118301193012030121301223012330124301253012630127301283012930130301313013230133301343013530136301373013830139301403014130142301433014430145301463014730148301493015030151301523015330154301553015630157301583015930160301613016230163301643016530166301673016830169301703017130172301733017430175301763017730178301793018030181301823018330184301853018630187301883018930190301913019230193301943019530196301973019830199302003020130202302033020430205302063020730208302093021030211302123021330214302153021630217302183021930220302213022230223302243022530226302273022830229302303023130232302333023430235302363023730238302393024030241302423024330244302453024630247302483024930250302513025230253302543025530256302573025830259302603026130262302633026430265302663026730268302693027030271302723027330274302753027630277302783027930280302813028230283302843028530286302873028830289302903029130292302933029430295302963029730298302993030030301303023030330304303053030630307303083030930310303113031230313303143031530316303173031830319303203032130322303233032430325303263032730328303293033030331303323033330334303353033630337303383033930340303413034230343303443034530346303473034830349303503035130352303533035430355303563035730358303593036030361303623036330364303653036630367303683036930370303713037230373303743037530376303773037830379303803038130382303833038430385303863038730388303893039030391303923039330394303953039630397303983039930400304013040230403304043040530406304073040830409304103041130412304133041430415304163041730418304193042030421304223042330424304253042630427304283042930430304313043230433304343043530436304373043830439304403044130442304433044430445304463044730448304493045030451304523045330454304553045630457304583045930460304613046230463304643046530466304673046830469304703047130472304733047430475304763047730478304793048030481304823048330484304853048630487304883048930490304913049230493304943049530496304973049830499305003050130502305033050430505305063050730508305093051030511305123051330514305153051630517305183051930520305213052230523305243052530526305273052830529305303053130532305333053430535305363053730538305393054030541305423054330544305453054630547305483054930550305513055230553305543055530556305573055830559305603056130562305633056430565305663056730568305693057030571305723057330574305753057630577305783057930580305813058230583305843058530586305873058830589305903059130592305933059430595305963059730598305993060030601306023060330604306053060630607306083060930610306113061230613306143061530616306173061830619306203062130622306233062430625306263062730628306293063030631306323063330634306353063630637306383063930640306413064230643306443064530646306473064830649306503065130652306533065430655306563065730658306593066030661306623066330664306653066630667306683066930670306713067230673306743067530676306773067830679306803068130682306833068430685306863068730688306893069030691306923069330694306953069630697306983069930700307013070230703307043070530706307073070830709307103071130712307133071430715307163071730718307193072030721307223072330724307253072630727307283072930730307313073230733307343073530736307373073830739307403074130742307433074430745307463074730748307493075030751307523075330754307553075630757307583075930760307613076230763307643076530766307673076830769307703077130772307733077430775307763077730778307793078030781307823078330784307853078630787307883078930790307913079230793307943079530796307973079830799308003080130802308033080430805308063080730808308093081030811308123081330814308153081630817308183081930820308213082230823308243082530826308273082830829308303083130832308333083430835308363083730838308393084030841308423084330844308453084630847308483084930850308513085230853308543085530856308573085830859308603086130862308633086430865308663086730868308693087030871308723087330874308753087630877308783087930880308813088230883308843088530886308873088830889308903089130892308933089430895308963089730898308993090030901309023090330904309053090630907309083090930910309113091230913309143091530916309173091830919309203092130922309233092430925309263092730928309293093030931309323093330934309353093630937309383093930940309413094230943309443094530946309473094830949309503095130952309533095430955309563095730958309593096030961309623096330964309653096630967309683096930970309713097230973309743097530976309773097830979309803098130982309833098430985309863098730988309893099030991309923099330994309953099630997309983099931000310013100231003310043100531006310073100831009310103101131012310133101431015310163101731018310193102031021310223102331024310253102631027310283102931030310313103231033310343103531036310373103831039310403104131042310433104431045310463104731048310493105031051310523105331054310553105631057310583105931060310613106231063310643106531066310673106831069310703107131072310733107431075310763107731078310793108031081310823108331084310853108631087310883108931090310913109231093310943109531096310973109831099311003110131102311033110431105311063110731108311093111031111311123111331114311153111631117311183111931120311213112231123311243112531126311273112831129311303113131132311333113431135311363113731138311393114031141311423114331144311453114631147311483114931150311513115231153311543115531156311573115831159311603116131162311633116431165311663116731168311693117031171311723117331174311753117631177311783117931180311813118231183311843118531186311873118831189311903119131192311933119431195311963119731198311993120031201312023120331204312053120631207312083120931210312113121231213312143121531216312173121831219312203122131222312233122431225312263122731228312293123031231312323123331234312353123631237312383123931240312413124231243312443124531246312473124831249312503125131252312533125431255312563125731258312593126031261312623126331264312653126631267312683126931270312713127231273312743127531276312773127831279312803128131282312833128431285312863128731288312893129031291312923129331294312953129631297312983129931300313013130231303313043130531306313073130831309313103131131312313133131431315313163131731318313193132031321313223132331324313253132631327313283132931330313313133231333313343133531336313373133831339313403134131342313433134431345313463134731348313493135031351313523135331354313553135631357313583135931360313613136231363313643136531366313673136831369313703137131372313733137431375313763137731378313793138031381313823138331384313853138631387313883138931390313913139231393313943139531396313973139831399314003140131402314033140431405314063140731408314093141031411314123141331414314153141631417314183141931420314213142231423314243142531426314273142831429314303143131432314333143431435314363143731438314393144031441314423144331444314453144631447314483144931450314513145231453314543145531456314573145831459314603146131462314633146431465314663146731468314693147031471314723147331474314753147631477314783147931480314813148231483314843148531486314873148831489314903149131492314933149431495314963149731498314993150031501315023150331504315053150631507315083150931510315113151231513315143151531516315173151831519315203152131522315233152431525315263152731528315293153031531315323153331534315353153631537315383153931540315413154231543315443154531546315473154831549315503155131552315533155431555315563155731558315593156031561315623156331564315653156631567315683156931570315713157231573315743157531576315773157831579315803158131582315833158431585315863158731588315893159031591315923159331594315953159631597315983159931600316013160231603316043160531606316073160831609316103161131612316133161431615316163161731618316193162031621316223162331624316253162631627316283162931630316313163231633316343163531636316373163831639316403164131642316433164431645316463164731648316493165031651316523165331654316553165631657316583165931660316613166231663316643166531666316673166831669316703167131672316733167431675316763167731678316793168031681316823168331684316853168631687316883168931690316913169231693316943169531696316973169831699317003170131702317033170431705317063170731708317093171031711317123171331714317153171631717317183171931720317213172231723317243172531726317273172831729317303173131732317333173431735317363173731738317393174031741317423174331744317453174631747317483174931750317513175231753317543175531756317573175831759317603176131762317633176431765317663176731768317693177031771317723177331774317753177631777317783177931780317813178231783317843178531786317873178831789317903179131792317933179431795317963179731798317993180031801318023180331804318053180631807318083180931810318113181231813318143181531816318173181831819318203182131822318233182431825318263182731828318293183031831318323183331834318353183631837318383183931840318413184231843318443184531846318473184831849318503185131852318533185431855318563185731858318593186031861318623186331864318653186631867318683186931870318713187231873318743187531876318773187831879318803188131882318833188431885318863188731888318893189031891318923189331894318953189631897318983189931900319013190231903319043190531906319073190831909319103191131912319133191431915319163191731918319193192031921319223192331924319253192631927319283192931930319313193231933319343193531936319373193831939319403194131942319433194431945319463194731948319493195031951319523195331954319553195631957319583195931960319613196231963319643196531966319673196831969319703197131972319733197431975319763197731978319793198031981319823198331984319853198631987319883198931990319913199231993319943199531996319973199831999320003200132002320033200432005320063200732008320093201032011320123201332014320153201632017320183201932020320213202232023320243202532026320273202832029320303203132032320333203432035320363203732038320393204032041320423204332044320453204632047320483204932050320513205232053320543205532056320573205832059320603206132062320633206432065320663206732068320693207032071320723207332074320753207632077320783207932080320813208232083320843208532086320873208832089320903209132092320933209432095320963209732098320993210032101321023210332104321053210632107321083210932110321113211232113321143211532116321173211832119321203212132122321233212432125321263212732128321293213032131321323213332134321353213632137321383213932140321413214232143321443214532146321473214832149321503215132152321533215432155321563215732158321593216032161321623216332164321653216632167321683216932170321713217232173321743217532176321773217832179321803218132182321833218432185321863218732188321893219032191321923219332194321953219632197321983219932200322013220232203322043220532206322073220832209322103221132212322133221432215322163221732218322193222032221322223222332224322253222632227322283222932230322313223232233322343223532236322373223832239322403224132242322433224432245322463224732248322493225032251322523225332254322553225632257322583225932260322613226232263322643226532266322673226832269322703227132272322733227432275322763227732278322793228032281322823228332284322853228632287322883228932290322913229232293322943229532296322973229832299323003230132302323033230432305323063230732308323093231032311323123231332314323153231632317323183231932320323213232232323323243232532326323273232832329323303233132332323333233432335323363233732338323393234032341323423234332344323453234632347323483234932350323513235232353323543235532356323573235832359323603236132362323633236432365323663236732368323693237032371323723237332374323753237632377323783237932380323813238232383323843238532386323873238832389323903239132392323933239432395323963239732398323993240032401324023240332404324053240632407324083240932410324113241232413324143241532416324173241832419324203242132422324233242432425324263242732428324293243032431324323243332434324353243632437324383243932440324413244232443324443244532446324473244832449324503245132452324533245432455324563245732458324593246032461324623246332464324653246632467324683246932470324713247232473324743247532476324773247832479324803248132482324833248432485324863248732488324893249032491324923249332494324953249632497324983249932500325013250232503325043250532506325073250832509325103251132512325133251432515325163251732518325193252032521325223252332524325253252632527325283252932530325313253232533325343253532536325373253832539325403254132542325433254432545325463254732548325493255032551325523255332554325553255632557325583255932560325613256232563325643256532566325673256832569325703257132572325733257432575325763257732578325793258032581325823258332584325853258632587325883258932590325913259232593325943259532596325973259832599326003260132602326033260432605326063260732608326093261032611326123261332614326153261632617326183261932620326213262232623326243262532626326273262832629326303263132632326333263432635326363263732638326393264032641326423264332644326453264632647326483264932650326513265232653326543265532656326573265832659326603266132662326633266432665326663266732668326693267032671326723267332674326753267632677326783267932680326813268232683326843268532686326873268832689326903269132692326933269432695326963269732698326993270032701327023270332704327053270632707327083270932710327113271232713327143271532716327173271832719327203272132722327233272432725327263272732728327293273032731327323273332734327353273632737327383273932740327413274232743327443274532746327473274832749327503275132752327533275432755327563275732758327593276032761327623276332764327653276632767327683276932770327713277232773327743277532776327773277832779327803278132782327833278432785327863278732788327893279032791327923279332794327953279632797327983279932800328013280232803328043280532806328073280832809328103281132812328133281432815328163281732818328193282032821328223282332824328253282632827328283282932830328313283232833328343283532836328373283832839328403284132842328433284432845328463284732848328493285032851328523285332854328553285632857328583285932860328613286232863328643286532866328673286832869328703287132872328733287432875328763287732878328793288032881328823288332884328853288632887328883288932890328913289232893328943289532896328973289832899329003290132902329033290432905329063290732908329093291032911329123291332914329153291632917329183291932920329213292232923329243292532926329273292832929329303293132932329333293432935329363293732938329393294032941329423294332944329453294632947329483294932950329513295232953329543295532956329573295832959329603296132962329633296432965329663296732968329693297032971329723297332974329753297632977329783297932980329813298232983329843298532986329873298832989329903299132992329933299432995329963299732998329993300033001330023300333004330053300633007330083300933010330113301233013330143301533016330173301833019330203302133022330233302433025330263302733028330293303033031330323303333034330353303633037330383303933040330413304233043330443304533046330473304833049330503305133052330533305433055330563305733058330593306033061330623306333064330653306633067330683306933070330713307233073330743307533076330773307833079330803308133082330833308433085330863308733088330893309033091330923309333094330953309633097330983309933100331013310233103331043310533106331073310833109331103311133112331133311433115331163311733118331193312033121331223312333124331253312633127331283312933130331313313233133331343313533136331373313833139331403314133142331433314433145331463314733148331493315033151331523315333154331553315633157331583315933160331613316233163331643316533166331673316833169331703317133172331733317433175331763317733178331793318033181331823318333184331853318633187331883318933190331913319233193331943319533196331973319833199332003320133202332033320433205332063320733208332093321033211332123321333214332153321633217332183321933220332213322233223332243322533226332273322833229332303323133232332333323433235332363323733238332393324033241332423324333244332453324633247332483324933250332513325233253332543325533256332573325833259332603326133262332633326433265332663326733268332693327033271332723327333274332753327633277332783327933280332813328233283332843328533286332873328833289332903329133292332933329433295332963329733298332993330033301333023330333304333053330633307333083330933310333113331233313333143331533316333173331833319333203332133322333233332433325333263332733328333293333033331333323333333334333353333633337333383333933340333413334233343333443334533346333473334833349333503335133352333533335433355333563335733358333593336033361333623336333364333653336633367333683336933370333713337233373333743337533376333773337833379333803338133382333833338433385333863338733388333893339033391333923339333394333953339633397333983339933400334013340233403334043340533406334073340833409334103341133412334133341433415334163341733418334193342033421334223342333424334253342633427334283342933430
  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_IndexModifier;
  785. Procedure TestRTTI_StoredModifier;
  786. Procedure TestRTTI_DefaultValue;
  787. Procedure TestRTTI_DefaultValueSet;
  788. Procedure TestRTTI_DefaultValueRangeType;
  789. Procedure TestRTTI_DefaultValueInherit;
  790. Procedure TestRTTI_OverrideMethod;
  791. Procedure TestRTTI_ReintroduceMethod;
  792. Procedure TestRTTI_OverloadProperty;
  793. // ToDo: array argument
  794. Procedure TestRTTI_ClassForward;
  795. Procedure TestRTTI_ClassOf;
  796. Procedure TestRTTI_Record;
  797. Procedure TestRTTI_RecordAnonymousArray;
  798. Procedure TestRTTI_Record_ClassVarType;
  799. Procedure TestRTTI_LocalTypes;
  800. Procedure TestRTTI_TypeInfo_BaseTypes;
  801. Procedure TestRTTI_TypeInfo_Type_BaseTypes;
  802. Procedure TestRTTI_TypeInfo_LocalFail;
  803. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses1;
  804. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses2;
  805. Procedure TestRTTI_TypeInfo_ExtTypeInfoClasses3;
  806. Procedure TestRTTI_TypeInfo_FunctionClassType;
  807. Procedure TestRTTI_TypeInfo_MixedUnits_PointerAndClass;
  808. Procedure TestRTTI_Interface_Corba;
  809. Procedure TestRTTI_Interface_COM;
  810. Procedure TestRTTI_ClassHelper;
  811. Procedure TestRTTI_ExternalClass;
  812. Procedure TestRTTI_Unit;
  813. // Resourcestring
  814. Procedure TestResourcestringProgram;
  815. Procedure TestResourcestringUnit;
  816. Procedure TestResourcestringImplementation;
  817. // Attributes
  818. Procedure TestAttributes_Members;
  819. Procedure TestAttributes_Types;
  820. Procedure TestAttributes_HelperConstructor_Fail;
  821. // Assertions, checks
  822. procedure TestAssert;
  823. procedure TestAssert_SysUtils;
  824. procedure TestObjectChecks;
  825. procedure TestOverflowChecks_Int;
  826. procedure TestRangeChecks_AssignInt;
  827. procedure TestRangeChecks_AssignIntRange;
  828. procedure TestRangeChecks_AssignEnum;
  829. procedure TestRangeChecks_AssignEnumRange;
  830. procedure TestRangeChecks_AssignChar;
  831. procedure TestRangeChecks_AssignCharRange;
  832. procedure TestRangeChecks_ArrayIndex;
  833. procedure TestRangeChecks_ArrayOfRecIndex;
  834. procedure TestRangeChecks_StringIndex;
  835. procedure TestRangeChecks_TypecastInt;
  836. procedure TestRangeChecks_TypeHelperInt;
  837. // Async/AWait
  838. Procedure TestAsync_Proc;
  839. Procedure TestAsync_CallResultIsPromise;
  840. Procedure TestAsync_ConstructorFail;
  841. Procedure TestAsync_PropertyGetterFail;
  842. Procedure TestAwait_NonPromiseWithTypeFail;
  843. Procedure TestAwait_AsyncCallTypeMismatch;
  844. Procedure TestAWait_OutsideAsyncFail;
  845. Procedure TestAWait_IntegerFail;
  846. Procedure TestAWait_ExternalClassPromise;
  847. Procedure TestAWait_JSValue;
  848. Procedure TestAWait_Result;
  849. Procedure TestAWait_ResultPromiseMissingTypeFail; // await(AsyncCallResultPromise) needs T
  850. Procedure TestAsync_AnonymousProc;
  851. Procedure TestAsync_ProcType;
  852. Procedure TestAsync_ProcTypeAsyncModMismatchFail;
  853. Procedure TestAsync_Inherited;
  854. Procedure TestAsync_ClassInterface;
  855. Procedure TestAsync_ClassInterface_AsyncMissmatchFail;
  856. end;
  857. function LinesToStr(Args: array of const): string;
  858. function ExtractFileUnitName(aFilename: string): string;
  859. function JSToStr(El: TJSElement): string;
  860. function CheckSrcDiff(Expected, Actual: string; out Msg: string): boolean;
  861. implementation
  862. function LinesToStr(Args: array of const): string;
  863. var
  864. s: String;
  865. i: Integer;
  866. begin
  867. s:='';
  868. for i:=Low(Args) to High(Args) do
  869. case Args[i].VType of
  870. vtChar: s += Args[i].VChar+LineEnding;
  871. vtString: s += Args[i].VString^+LineEnding;
  872. vtPChar: s += Args[i].VPChar+LineEnding;
  873. vtWideChar: s += AnsiString(Args[i].VWideChar)+LineEnding;
  874. vtPWideChar: s += AnsiString(Args[i].VPWideChar)+LineEnding;
  875. vtAnsiString: s += AnsiString(Args[i].VAnsiString)+LineEnding;
  876. vtWidestring: s += AnsiString(WideString(Args[i].VWideString))+LineEnding;
  877. vtUnicodeString:s += AnsiString(UnicodeString(Args[i].VUnicodeString))+LineEnding;
  878. end;
  879. Result:=s;
  880. end;
  881. function ExtractFileUnitName(aFilename: string): string;
  882. var
  883. p: Integer;
  884. begin
  885. Result:=ExtractFileName(aFilename);
  886. if Result='' then exit;
  887. for p:=length(Result) downto 1 do
  888. case Result[p] of
  889. '/','\': exit;
  890. '.':
  891. begin
  892. Delete(Result,p,length(Result));
  893. exit;
  894. end;
  895. end;
  896. end;
  897. function JSToStr(El: TJSElement): string;
  898. var
  899. aWriter: TBufferWriter;
  900. aJSWriter: TJSWriter;
  901. begin
  902. aJSWriter:=nil;
  903. aWriter:=TBufferWriter.Create(1000);
  904. try
  905. aJSWriter:=TJSWriter.Create(aWriter);
  906. aJSWriter.IndentSize:=2;
  907. aJSWriter.WriteJS(El);
  908. Result:=aWriter.AsString;
  909. finally
  910. aJSWriter.Free;
  911. aWriter.Free;
  912. end;
  913. end;
  914. function CheckSrcDiff(Expected, Actual: string; out Msg: string): boolean;
  915. // search diff, ignore changes in spaces
  916. const
  917. SpaceChars = [#9,#10,#13,' '];
  918. var
  919. ExpectedP, ActualP: PChar;
  920. function FindLineEnd(p: PChar): PChar;
  921. begin
  922. Result:=p;
  923. while not (Result^ in [#0,#10,#13]) do inc(Result);
  924. end;
  925. function FindLineStart(p, MinP: PChar): PChar;
  926. begin
  927. while (p>MinP) and not (p[-1] in [#10,#13]) do dec(p);
  928. Result:=p;
  929. end;
  930. procedure SkipLineEnd(var p: PChar);
  931. begin
  932. if p^ in [#10,#13] then
  933. begin
  934. if (p[1] in [#10,#13]) and (p^<>p[1]) then
  935. inc(p,2)
  936. else
  937. inc(p);
  938. end;
  939. end;
  940. function HasSpecialChar(s: string): boolean;
  941. var
  942. i: Integer;
  943. begin
  944. for i:=1 to length(s) do
  945. if s[i] in [#0..#31,#127..#255] then
  946. exit(true);
  947. Result:=false;
  948. end;
  949. function HashSpecialChars(s: string): string;
  950. var
  951. i: Integer;
  952. begin
  953. Result:='';
  954. for i:=1 to length(s) do
  955. if s[i] in [#0..#31,#127..#255] then
  956. Result:=Result+'#'+hexstr(ord(s[i]),2)
  957. else
  958. Result:=Result+s[i];
  959. end;
  960. procedure DiffFound;
  961. var
  962. ActLineStartP, ActLineEndP, p, StartPos: PChar;
  963. ExpLine, ActLine: String;
  964. i, LineNo, DiffLineNo: Integer;
  965. begin
  966. writeln('Diff found "',Msg,'". Lines:');
  967. // write correct lines
  968. p:=PChar(Expected);
  969. LineNo:=0;
  970. DiffLineNo:=0;
  971. repeat
  972. StartPos:=p;
  973. while not (p^ in [#0,#10,#13]) do inc(p);
  974. ExpLine:=copy(Expected,StartPos-PChar(Expected)+1,p-StartPos);
  975. SkipLineEnd(p);
  976. inc(LineNo);
  977. if (p<=ExpectedP) and (p^<>#0) then
  978. begin
  979. writeln('= ',ExpLine);
  980. end else begin
  981. // diff line
  982. if DiffLineNo=0 then DiffLineNo:=LineNo;
  983. // write actual line
  984. ActLineStartP:=FindLineStart(ActualP,PChar(Actual));
  985. ActLineEndP:=FindLineEnd(ActualP);
  986. ActLine:=copy(Actual,ActLineStartP-PChar(Actual)+1,ActLineEndP-ActLineStartP);
  987. writeln('- ',ActLine);
  988. if HasSpecialChar(ActLine) then
  989. writeln('- ',HashSpecialChars(ActLine));
  990. // write expected line
  991. writeln('+ ',ExpLine);
  992. if HasSpecialChar(ExpLine) then
  993. writeln('- ',HashSpecialChars(ExpLine));
  994. // write empty line with pointer ^
  995. for i:=1 to 2+ExpectedP-StartPos do write(' ');
  996. writeln('^');
  997. Msg:='expected "'+ExpLine+'", but got "'+ActLine+'".';
  998. CheckSrcDiff:=false;
  999. // write up to three following actual lines to get some context
  1000. for i:=1 to 3 do begin
  1001. ActLineStartP:=ActLineEndP;
  1002. SkipLineEnd(ActLineStartP);
  1003. if ActLineStartP^=#0 then break;
  1004. ActLineEndP:=FindLineEnd(ActLineStartP);
  1005. ActLine:=copy(Actual,ActLineStartP-PChar(Actual)+1,ActLineEndP-ActLineStartP);
  1006. writeln('~ ',ActLine);
  1007. end;
  1008. exit;
  1009. end;
  1010. until p^=#0;
  1011. writeln('DiffFound Actual:-----------------------');
  1012. writeln(Actual);
  1013. writeln('DiffFound Expected:---------------------');
  1014. writeln(Expected);
  1015. writeln('DiffFound ------------------------------');
  1016. Msg:='diff found, but lines are the same, internal error';
  1017. CheckSrcDiff:=false;
  1018. end;
  1019. var
  1020. IsSpaceNeeded: Boolean;
  1021. LastChar, Quote: Char;
  1022. begin
  1023. Result:=true;
  1024. Msg:='';
  1025. if Expected='' then Expected:=' ';
  1026. if Actual='' then Actual:=' ';
  1027. ExpectedP:=PChar(Expected);
  1028. ActualP:=PChar(Actual);
  1029. repeat
  1030. //writeln('TTestModule.CheckDiff Exp="',ExpectedP^,'" Act="',ActualP^,'"');
  1031. case ExpectedP^ of
  1032. #0:
  1033. begin
  1034. // check that rest of Actual has only spaces
  1035. while ActualP^ in SpaceChars do inc(ActualP);
  1036. if ActualP^<>#0 then
  1037. begin
  1038. DiffFound;
  1039. exit;
  1040. end;
  1041. exit(true);
  1042. end;
  1043. ' ',#9,#10,#13:
  1044. begin
  1045. // skip space in Expected
  1046. IsSpaceNeeded:=false;
  1047. if ExpectedP>PChar(Expected) then
  1048. LastChar:=ExpectedP[-1]
  1049. else
  1050. LastChar:=#0;
  1051. while ExpectedP^ in SpaceChars do inc(ExpectedP);
  1052. if (LastChar in ['a'..'z','A'..'Z','0'..'9','_','$'])
  1053. and (ExpectedP^ in ['a'..'z','A'..'Z','0'..'9','_','$']) then
  1054. IsSpaceNeeded:=true;
  1055. if IsSpaceNeeded and (not (ActualP^ in SpaceChars)) then
  1056. begin
  1057. DiffFound;
  1058. exit;
  1059. end;
  1060. while ActualP^ in SpaceChars do inc(ActualP);
  1061. end;
  1062. '''','"':
  1063. begin
  1064. while ActualP^ in SpaceChars do inc(ActualP);
  1065. if ExpectedP^<>ActualP^ then
  1066. begin
  1067. DiffFound;
  1068. exit;
  1069. end;
  1070. Quote:=ExpectedP^;
  1071. repeat
  1072. inc(ExpectedP);
  1073. inc(ActualP);
  1074. if ExpectedP^<>ActualP^ then
  1075. begin
  1076. DiffFound;
  1077. exit;
  1078. end;
  1079. if (ExpectedP^ in [#0,#10,#13]) then
  1080. break
  1081. else if (ExpectedP^=Quote) then
  1082. begin
  1083. inc(ExpectedP);
  1084. inc(ActualP);
  1085. break;
  1086. end;
  1087. until false;
  1088. end;
  1089. else
  1090. while ActualP^ in SpaceChars do inc(ActualP);
  1091. if ExpectedP^<>ActualP^ then
  1092. begin
  1093. DiffFound;
  1094. exit;
  1095. end;
  1096. inc(ExpectedP);
  1097. inc(ActualP);
  1098. end;
  1099. until false;
  1100. end;
  1101. { TTestEnginePasResolver }
  1102. destructor TTestEnginePasResolver.Destroy;
  1103. begin
  1104. FreeAndNil(FStreamResolver);
  1105. FreeAndNil(FParser);
  1106. FreeAndNil(FScanner);
  1107. FreeAndNil(FStreamResolver);
  1108. if Module<>nil then
  1109. begin
  1110. Module.Release{$IFDEF CheckPasTreeRefCount}('CreateElement'){$ENDIF};
  1111. FModule:=nil;
  1112. end;
  1113. inherited Destroy;
  1114. end;
  1115. function TTestEnginePasResolver.FindUnit(const AName, InFilename: String;
  1116. NameExpr, InFileExpr: TPasExpr): TPasModule;
  1117. begin
  1118. Result:=nil;
  1119. if InFilename<>'' then
  1120. RaiseNotYetImplemented(20180224101926,InFileExpr,'Use testcase tcunitsearch instead');
  1121. if Assigned(OnFindUnit) then
  1122. Result:=OnFindUnit(AName);
  1123. if NameExpr=nil then ;
  1124. end;
  1125. procedure TTestEnginePasResolver.UsedInterfacesFinished(Section: TPasSection);
  1126. begin
  1127. // do not parse recursively
  1128. // parse via the queue
  1129. if Section=nil then ;
  1130. end;
  1131. { TCustomTestModule }
  1132. function TCustomTestModule.GetMsgCount: integer;
  1133. begin
  1134. Result:=FHintMsgs.Count;
  1135. end;
  1136. function TCustomTestModule.GetMsgs(Index: integer): TTestHintMessage;
  1137. begin
  1138. Result:=TTestHintMessage(FHintMsgs[Index]);
  1139. end;
  1140. function TCustomTestModule.GetResolverCount: integer;
  1141. begin
  1142. Result:=FModules.Count;
  1143. end;
  1144. function TCustomTestModule.GetResolvers(Index: integer
  1145. ): TTestEnginePasResolver;
  1146. begin
  1147. Result:=TTestEnginePasResolver(FModules[Index]);
  1148. end;
  1149. function TCustomTestModule.OnPasResolverFindUnit(const aUnitName: String
  1150. ): TPasModule;
  1151. var
  1152. DefNamespace: String;
  1153. begin
  1154. //writeln('TTestModule.OnPasResolverFindUnit START Unit="',aUnitName,'"');
  1155. if (Pos('.',aUnitName)<1) then
  1156. begin
  1157. DefNamespace:=GetDefaultNamespace;
  1158. if DefNamespace<>'' then
  1159. begin
  1160. Result:=LoadUnit(DefNamespace+'.'+aUnitName);
  1161. if Result<>nil then exit;
  1162. end;
  1163. end;
  1164. Result:=LoadUnit(aUnitName);
  1165. if Result<>nil then exit;
  1166. {$IFDEF VerbosePas2JS}
  1167. writeln('TTestModule.OnPasResolverFindUnit missing unit "',aUnitName,'"');
  1168. {$ENDIF}
  1169. Fail('can''t find unit "'+aUnitName+'"');
  1170. end;
  1171. procedure TCustomTestModule.OnParserLog(Sender: TObject; const Msg: String);
  1172. var
  1173. aParser: TPasParser;
  1174. Item: TTestHintMessage;
  1175. begin
  1176. aParser:=Sender as TPasParser;
  1177. Item:=TTestHintMessage.Create;
  1178. Item.Id:=aParser.LastMsgNumber;
  1179. Item.MsgType:=aParser.LastMsgType;
  1180. Item.MsgNumber:=aParser.LastMsgNumber;
  1181. Item.Msg:=Msg;
  1182. Item.SourcePos:=aParser.Scanner.CurSourcePos;
  1183. {$IFDEF VerbosePas2JS}
  1184. writeln('TCustomTestModule.OnParserLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1185. {$ENDIF}
  1186. FHintMsgs.Add(Item);
  1187. end;
  1188. procedure TCustomTestModule.OnPasResolverLog(Sender: TObject; const Msg: String
  1189. );
  1190. var
  1191. aResolver: TTestEnginePasResolver;
  1192. Item: TTestHintMessage;
  1193. begin
  1194. aResolver:=Sender as TTestEnginePasResolver;
  1195. Item:=TTestHintMessage.Create;
  1196. Item.Id:=aResolver.LastMsgId;
  1197. Item.MsgType:=aResolver.LastMsgType;
  1198. Item.MsgNumber:=aResolver.LastMsgNumber;
  1199. Item.Msg:=Msg;
  1200. Item.SourcePos:=aResolver.LastSourcePos;
  1201. {$IFDEF VerbosePas2JS}
  1202. writeln('TCustomTestModule.OnPasResolverLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1203. {$ENDIF}
  1204. FHintMsgs.Add(Item);
  1205. end;
  1206. procedure TCustomTestModule.OnScannerLog(Sender: TObject; const Msg: String);
  1207. var
  1208. Item: TTestHintMessage;
  1209. aScanner: TPas2jsPasScanner;
  1210. begin
  1211. aScanner:=Sender as TPas2jsPasScanner;
  1212. Item:=TTestHintMessage.Create;
  1213. Item.Id:=aScanner.LastMsgNumber;
  1214. Item.MsgType:=aScanner.LastMsgType;
  1215. Item.MsgNumber:=aScanner.LastMsgNumber;
  1216. Item.Msg:=Msg;
  1217. Item.SourcePos:=aScanner.CurSourcePos;
  1218. {$IFDEF VerbosePas2JS}
  1219. writeln('TCustomTestModule.OnScannerLog ',GetObjName(Sender),' ',Item.MsgType,' (',Item.MsgNumber,') {',Msg,'}');
  1220. {$ENDIF}
  1221. FHintMsgs.Add(Item);
  1222. end;
  1223. procedure TCustomTestModule.SetWithTypeInfo(const AValue: boolean);
  1224. begin
  1225. if FWithTypeInfo=AValue then Exit;
  1226. FWithTypeInfo:=AValue;
  1227. if AValue then
  1228. Converter.Options:=Converter.Options-[coNoTypeInfo]
  1229. else
  1230. Converter.Options:=Converter.Options+[coNoTypeInfo];
  1231. end;
  1232. function TCustomTestModule.LoadUnit(const aUnitName: String): TPasModule;
  1233. var
  1234. i: Integer;
  1235. CurEngine: TTestEnginePasResolver;
  1236. CurUnitName: String;
  1237. begin
  1238. //writeln('TTestModule.FindUnit START Unit="',aUnitName,'"');
  1239. Result:=nil;
  1240. if (Module.ClassType=TPasModule)
  1241. and (CompareText(Module.Name,aUnitName)=0) then
  1242. exit(Module);
  1243. for i:=0 to ResolverCount-1 do
  1244. begin
  1245. CurEngine:=Resolvers[i];
  1246. CurUnitName:=ExtractFileUnitName(CurEngine.Filename);
  1247. //writeln('TTestModule.FindUnit Checking ',i,'/',ResolverCount,' ',CurEngine.Filename,' ',CurUnitName);
  1248. if CompareText(aUnitName,CurUnitName)=0 then
  1249. begin
  1250. Result:=CurEngine.Module;
  1251. if Result<>nil then exit;
  1252. //writeln('TTestModule.FindUnit PARSING unit "',CurEngine.Filename,'"');
  1253. FileResolver.FindSourceFile(aUnitName);
  1254. CurEngine.StreamResolver:=TStreamResolver.Create;
  1255. CurEngine.StreamResolver.OwnsStreams:=True;
  1256. //writeln('TTestModule.FindUnit SOURCE=',CurEngine.Source);
  1257. CurEngine.StreamResolver.AddStream(CurEngine.FileName,TStringStream.Create(CurEngine.Source));
  1258. CurEngine.Scanner:=TPas2jsPasScanner.Create(CurEngine.StreamResolver);
  1259. InitScanner(CurEngine.Scanner);
  1260. CurEngine.Parser:=TTestPasParser.Create(CurEngine.Scanner,CurEngine.StreamResolver,CurEngine);
  1261. CurEngine.Parser.Options:=po_tcmodules;
  1262. if CompareText(CurUnitName,'System')=0 then
  1263. CurEngine.Parser.ImplicitUses.Clear;
  1264. CurEngine.Scanner.OpenFile(CurEngine.Filename);
  1265. try
  1266. CurEngine.Parser.NextToken;
  1267. CurEngine.Parser.ParseUnit(CurEngine.FModule);
  1268. except
  1269. on E: Exception do
  1270. HandleException(E);
  1271. end;
  1272. //writeln('TTestModule.FindUnit END ',CurUnitName);
  1273. Result:=CurEngine.Module;
  1274. exit;
  1275. end;
  1276. end;
  1277. end;
  1278. procedure TCustomTestModule.SetUp;
  1279. begin
  1280. {$IFDEF EnablePasTreeGlobalRefCount}
  1281. FElementRefCountAtSetup:=TPasElement.GlobalRefCount;
  1282. {$ENDIF}
  1283. if FModules<>nil then
  1284. begin
  1285. writeln('TCustomTestModule.SetUp FModules<>nil');
  1286. Halt;
  1287. end;
  1288. inherited SetUp;
  1289. FSkipTests:=false;
  1290. FWithTypeInfo:=false;
  1291. FSource:=TStringList.Create;
  1292. FHub:=TPas2JSResolverHub.Create(Self);
  1293. FModules:=TObjectList.Create(true);
  1294. FFilename:='test1.pp';
  1295. FFileResolver:=TStreamResolver.Create;
  1296. FFileResolver.OwnsStreams:=True;
  1297. FScanner:=TPas2jsPasScanner.Create(FFileResolver);
  1298. InitScanner(FScanner);
  1299. FEngine:=AddModule(Filename);
  1300. FEngine.Scanner:=FScanner;
  1301. FScanner.Resolver:=FEngine;
  1302. FParser:=TTestPasParser.Create(FScanner,FFileResolver,FEngine);
  1303. FParser.OnLog:=@OnParserLog;
  1304. FEngine.Parser:=FParser;
  1305. Parser.Options:=po_tcmodules;
  1306. FModule:=Nil;
  1307. FConverter:=CreateConverter;
  1308. FExpectedErrorClass:=nil;
  1309. end;
  1310. function TCustomTestModule.CreateConverter: TPasToJSConverter;
  1311. var
  1312. Options: TPasToJsConverterOptions;
  1313. begin
  1314. Result:=TPasToJSConverter.Create;
  1315. Options:=co_tcmodules;
  1316. if WithTypeInfo then
  1317. Exclude(Options,coNoTypeInfo)
  1318. else
  1319. Include(Options,coNoTypeInfo);
  1320. Result.Options:=Options;
  1321. Result.Globals:=TPasToJSConverterGlobals.Create(Result);
  1322. end;
  1323. procedure TCustomTestModule.InitScanner(aScanner: TPas2jsPasScanner);
  1324. begin
  1325. aScanner.AllowedModeSwitches:=msAllPas2jsModeSwitches;
  1326. aScanner.ReadOnlyModeSwitches:=msAllPas2jsModeSwitchesReadOnly;
  1327. aScanner.CurrentModeSwitches:=OBJFPCModeSwitches*msAllPas2jsModeSwitches+msAllPas2jsModeSwitchesReadOnly;
  1328. aScanner.AllowedBoolSwitches:=bsAllPas2jsBoolSwitches;
  1329. aScanner.ReadOnlyBoolSwitches:=bsAllPas2jsBoolSwitchesReadOnly;
  1330. aScanner.CurrentBoolSwitches:=bsAllPas2jsBoolSwitchesReadOnly+[bsHints,bsNotes,bsWarnings,bsWriteableConst];
  1331. aScanner.AllowedValueSwitches:=vsAllPas2jsValueSwitches;
  1332. aScanner.ReadOnlyValueSwitches:=vsAllPas2jsValueSwitchesReadOnly;
  1333. aScanner.OnLog:=@OnScannerLog;
  1334. aScanner.CompilerVersion:='Comp.Ver.tcmodules';
  1335. end;
  1336. procedure TCustomTestModule.TearDown;
  1337. {$IFDEF CheckPasTreeRefCount}
  1338. var
  1339. El: TPasElement;
  1340. {$ENDIF}
  1341. var
  1342. i: Integer;
  1343. CurModule: TPasModule;
  1344. begin
  1345. FHintMsgs.Clear;
  1346. FHintMsgsGood.Clear;
  1347. FSkipTests:=false;
  1348. FWithTypeInfo:=false;
  1349. FJSRegModuleCall:=nil;
  1350. FJSModuleCallArgs:=nil;
  1351. FJSImplentationUses:=nil;
  1352. FJSInterfaceUses:=nil;
  1353. FJSModuleSrc:=nil;
  1354. FJSInitBody:=nil;
  1355. FreeAndNil(FJSSource);
  1356. FreeAndNil(FJSModule);
  1357. FreeAndNil(FConverter);
  1358. Engine.Clear;
  1359. FreeAndNil(FSource);
  1360. FreeAndNil(FFileResolver);
  1361. if FModules<>nil then
  1362. begin
  1363. for i:=0 to FModules.Count-1 do
  1364. begin
  1365. CurModule:=TTestEnginePasResolver(FModules[i]).Module;
  1366. if CurModule=nil then continue;
  1367. //writeln('TCustomTestModule.TearDown ReleaseUsedUnits ',CurModule.Name,' ',CurModule.RefCount,' ',CurModule.RefIds.Text);
  1368. CurModule.ReleaseUsedUnits;
  1369. end;
  1370. if FModule<>nil then
  1371. FModule.ReleaseUsedUnits;
  1372. for i:=0 to FModules.Count-1 do
  1373. begin
  1374. CurModule:=TTestEnginePasResolver(FModules[i]).Module;
  1375. if CurModule=nil then continue;
  1376. //writeln('TCustomTestModule.TearDown UsesReleased ',CurModule.Name,' ',CurModule.RefCount,' ',CurModule.RefIds.Text);
  1377. end;
  1378. FreeAndNil(FModules);
  1379. ReleaseAndNil(TPasElement(FModule){$IFDEF CheckPasTreeRefCount},'CreateElement'{$ENDIF});
  1380. FEngine:=nil;
  1381. end;
  1382. FreeAndNil(FHub);
  1383. inherited TearDown;
  1384. {$IFDEF EnablePasTreeGlobalRefCount}
  1385. if FElementRefCountAtSetup<>TPasElement.GlobalRefCount then
  1386. begin
  1387. writeln('TCustomTestModule.TearDown GlobalRefCount Was='+IntToStr(FElementRefCountAtSetup)+' Now='+IntToStr(TPasElement.GlobalRefCount));
  1388. {$IFDEF CheckPasTreeRefCount}
  1389. El:=TPasElement.FirstRefEl;
  1390. while El<>nil do
  1391. begin
  1392. writeln(' ',GetObjName(El),' RefIds.Count=',El.RefIds.Count,':');
  1393. for i:=0 to El.RefIds.Count-1 do
  1394. writeln(' ',El.RefIds[i]);
  1395. El:=El.NextRefEl;
  1396. end;
  1397. {$ENDIF}
  1398. Halt;
  1399. Fail('TCustomTestModule.TearDown Was='+IntToStr(FElementRefCountAtSetup)+' Now='+IntToStr(TPasElement.GlobalRefCount));
  1400. end;
  1401. {$ENDIF}
  1402. end;
  1403. procedure TCustomTestModule.Add(Line: string);
  1404. begin
  1405. Source.Add(Line);
  1406. end;
  1407. procedure TCustomTestModule.Add(const Lines: array of string);
  1408. var
  1409. i: Integer;
  1410. begin
  1411. for i:=low(Lines) to high(Lines) do
  1412. Add(Lines[i]);
  1413. end;
  1414. procedure TCustomTestModule.StartParsing;
  1415. var
  1416. Src: String;
  1417. begin
  1418. Src:=Source.Text;
  1419. FEngine.Source:=Src;
  1420. FileResolver.AddStream(FileName,TStringStream.Create(Src));
  1421. Scanner.OpenFile(FileName);
  1422. Writeln('// Test : ',Self.TestName);
  1423. Writeln(Src);
  1424. end;
  1425. procedure TCustomTestModule.ParseModuleQueue;
  1426. var
  1427. i: Integer;
  1428. CurResolver: TTestEnginePasResolver;
  1429. Found: Boolean;
  1430. Section: TPasSection;
  1431. begin
  1432. // parse til exception or all modules finished
  1433. while not SkipTests do
  1434. begin
  1435. Found:=false;
  1436. for i:=0 to ResolverCount-1 do
  1437. begin
  1438. CurResolver:=Resolvers[i];
  1439. if CurResolver.CurrentParser=nil then continue;
  1440. if not CurResolver.CurrentParser.CanParseContinue(Section) then
  1441. continue;
  1442. CurResolver.Parser.ParseContinue;
  1443. Found:=true;
  1444. break;
  1445. end;
  1446. if not Found then break;
  1447. end;
  1448. for i:=0 to ResolverCount-1 do
  1449. begin
  1450. CurResolver:=Resolvers[i];
  1451. if CurResolver.Parser=nil then
  1452. begin
  1453. if CurResolver.CurrentParser<>nil then
  1454. Fail('TCustomTestModule.ParseModuleQueue '+CurResolver.Filename+' '+GetObjName(CurResolver.Parser)+'=Parser<>CurrentParser='+GetObjName(CurResolver.CurrentParser));
  1455. continue;
  1456. end;
  1457. if CurResolver.Parser.CurModule<>nil then
  1458. Fail('TCustomTestModule.ParseModuleQueue '+CurResolver.Filename+' NOT FINISHED CurModule='+GetObjName(CurResolver.Parser.CurModule));
  1459. end;
  1460. end;
  1461. procedure TCustomTestModule.ParseModule;
  1462. begin
  1463. if SkipTests then exit;
  1464. FFirstPasStatement:=nil;
  1465. try
  1466. StartParsing;
  1467. Parser.ParseMain(FModule);
  1468. ParseModuleQueue;
  1469. except
  1470. on E: Exception do
  1471. HandleException(E);
  1472. end;
  1473. if SkipTests then exit;
  1474. AssertNotNull('Module resulted in Module',Module);
  1475. AssertEquals('modulename',lowercase(ChangeFileExt(FFileName,'')),lowercase(Module.Name));
  1476. TAssert.AssertSame('Has resolver',Engine,Parser.Engine);
  1477. end;
  1478. procedure TCustomTestModule.ParseProgram;
  1479. begin
  1480. if SkipTests then exit;
  1481. ParseModule;
  1482. if SkipTests then exit;
  1483. AssertEquals('Has program',TPasProgram,Module.ClassType);
  1484. FPasProgram:=TPasProgram(Module);
  1485. AssertNotNull('Has program section',PasProgram.ProgramSection);
  1486. AssertNotNull('Has initialization section',PasProgram.InitializationSection);
  1487. if (PasProgram.InitializationSection.Elements.Count>0) then
  1488. if TObject(PasProgram.InitializationSection.Elements[0]) is TPasImplBlock then
  1489. FFirstPasStatement:=TPasImplBlock(PasProgram.InitializationSection.Elements[0]);
  1490. end;
  1491. procedure TCustomTestModule.ParseUnit;
  1492. begin
  1493. if SkipTests then exit;
  1494. ParseModule;
  1495. if SkipTests then exit;
  1496. AssertEquals('Has unit (TPasModule)',TPasModule,Module.ClassType);
  1497. AssertNotNull('Has interface section',Module.InterfaceSection);
  1498. AssertNotNull('Has implementation section',Module.ImplementationSection);
  1499. if (Module.InitializationSection<>nil)
  1500. and (Module.InitializationSection.Elements.Count>0)
  1501. and (TObject(Module.InitializationSection.Elements[0]) is TPasImplBlock) then
  1502. FFirstPasStatement:=TPasImplBlock(Module.InitializationSection.Elements[0]);
  1503. end;
  1504. function TCustomTestModule.FindModuleWithFilename(aFilename: string
  1505. ): TTestEnginePasResolver;
  1506. var
  1507. i: Integer;
  1508. begin
  1509. for i:=0 to ResolverCount-1 do
  1510. if CompareText(Resolvers[i].Filename,aFilename)=0 then
  1511. exit(Resolvers[i]);
  1512. Result:=nil;
  1513. end;
  1514. function TCustomTestModule.AddModule(aFilename: string
  1515. ): TTestEnginePasResolver;
  1516. begin
  1517. //writeln('TTestModuleConverter.AddModule ',aFilename);
  1518. if FindModuleWithFilename(aFilename)<>nil then
  1519. Fail('TTestModuleConverter.AddModule: file "'+aFilename+'" already exists');
  1520. Result:=TTestEnginePasResolver.Create;
  1521. Result.Filename:=aFilename;
  1522. Result.AddObjFPCBuiltInIdentifiers(btAllJSBaseTypes,bfAllJSBaseProcs);
  1523. Result.OnFindUnit:=@OnPasResolverFindUnit;
  1524. Result.OnLog:=@OnPasResolverLog;
  1525. Result.Hub:=Hub;
  1526. FModules.Add(Result);
  1527. end;
  1528. function TCustomTestModule.AddModuleWithSrc(aFilename, Src: string
  1529. ): TTestEnginePasResolver;
  1530. begin
  1531. Result:=AddModule(aFilename);
  1532. Result.Source:=Src;
  1533. end;
  1534. function TCustomTestModule.AddModuleWithIntfImplSrc(aFilename, InterfaceSrc,
  1535. ImplementationSrc: string): TTestEnginePasResolver;
  1536. var
  1537. Src: String;
  1538. begin
  1539. Src:='unit '+ExtractFileUnitName(aFilename)+';'+LineEnding;
  1540. Src+=LineEnding;
  1541. Src+='interface'+LineEnding;
  1542. Src+=LineEnding;
  1543. Src+=InterfaceSrc;
  1544. Src+='implementation'+LineEnding;
  1545. Src+=LineEnding;
  1546. Src+=ImplementationSrc;
  1547. Src+='end.'+LineEnding;
  1548. Result:=AddModuleWithSrc(aFilename,Src);
  1549. end;
  1550. procedure TCustomTestModule.AddSystemUnit(Parts: TSystemUnitParts);
  1551. var
  1552. Intf, Impl: TStringList;
  1553. begin
  1554. Intf:=TStringList.Create;
  1555. if supTInterfacedObject in Parts then Include(Parts,supTObject);
  1556. // unit interface
  1557. if [supTVarRec,supTypeInfo]*Parts<>[] then
  1558. Intf.Add('{$modeswitch externalclass}');
  1559. Intf.Add('type');
  1560. Intf.Add(' integer=longint;');
  1561. Intf.Add(' sizeint=nativeint;');
  1562. //'const',
  1563. //' LineEnding = #10;',
  1564. //' DirectorySeparator = ''/'';',
  1565. //' DriveSeparator = '''';',
  1566. //' AllowDirectorySeparators : set of char = [''\'',''/''];',
  1567. //' AllowDriveSeparators : set of char = [];',
  1568. if supTObject in Parts then
  1569. Intf.AddStrings([
  1570. 'type',
  1571. ' TClass = class of TObject;',
  1572. ' TObject = class',
  1573. ' constructor Create;',
  1574. ' destructor Destroy; virtual;',
  1575. ' class function ClassType: TClass; assembler;',
  1576. ' class function ClassName: String; assembler;',
  1577. ' class function ClassNameIs(const Name: string): boolean;',
  1578. ' class function ClassParent: TClass; assembler;',
  1579. ' class function InheritsFrom(aClass: TClass): boolean; assembler;',
  1580. ' class function UnitName: String; assembler;',
  1581. ' procedure AfterConstruction; virtual;',
  1582. ' procedure BeforeDestruction;virtual;',
  1583. ' function Equals(Obj: TObject): boolean; virtual;',
  1584. ' function ToString: String; virtual;',
  1585. ' end;']);
  1586. if supTInterfacedObject in Parts then
  1587. Intf.AddStrings([
  1588. ' {$Interfaces COM}',
  1589. ' IUnknown = interface',
  1590. ' [''{00000000-0000-0000-C000-000000000046}'']',
  1591. //' function QueryInterface(const iid: TGuid; out obj): Integer;',
  1592. ' function _AddRef: Integer;',
  1593. ' function _Release: Integer;',
  1594. ' end;',
  1595. ' IInterface = IUnknown;',
  1596. ' TInterfacedObject = class(TObject,IUnknown)',
  1597. ' protected',
  1598. ' fRefCount: Integer;',
  1599. ' { implement methods of IUnknown }',
  1600. //' function QueryInterface(const iid: TGuid; out obj): Integer; virtual;',
  1601. ' function _AddRef: Integer; virtual;',
  1602. ' function _Release: Integer; virtual;',
  1603. ' end;',
  1604. ' TInterfacedClass = class of TInterfacedObject;',
  1605. '',
  1606. '']);
  1607. if supTVarRec in Parts then
  1608. Intf.AddStrings([
  1609. 'const',
  1610. ' vtInteger = 0;',
  1611. ' vtBoolean = 1;',
  1612. ' vtJSValue = 19;',
  1613. 'type',
  1614. ' PVarRec = ^TVarRec;',
  1615. ' TVarRec = record',
  1616. ' VType : byte;',
  1617. ' VJSValue: JSValue;',
  1618. ' vInteger: longint external name ''VJSValue'';',
  1619. ' vBoolean: boolean external name ''VJSValue'';',
  1620. ' end;',
  1621. ' TVarRecArray = array of TVarRec;',
  1622. 'function VarRecs: TVarRecArray; varargs;',
  1623. '']);
  1624. if supTypeInfo in Parts then
  1625. begin
  1626. Intf.AddStrings([
  1627. 'type',
  1628. ' TTypeKind = (',
  1629. ' tkUnknown, // 0',
  1630. ' tkInteger, // 1',
  1631. ' tkChar, // 2 in Delphi/FPC tkWChar, tkUChar',
  1632. ' tkString, // 3 in Delphi/FPC tkSString, tkWString or tkUString',
  1633. ' tkEnumeration, // 4',
  1634. ' tkSet, // 5',
  1635. ' tkDouble, // 6',
  1636. ' tkBool, // 7',
  1637. ' tkProcVar, // 8 function or procedure',
  1638. ' tkMethod, // 9 proc var of object',
  1639. ' tkArray, // 10 static array',
  1640. ' tkDynArray, // 11',
  1641. ' tkRecord, // 12',
  1642. ' tkClass, // 13',
  1643. ' tkClassRef, // 14',
  1644. ' tkPointer, // 15',
  1645. ' tkJSValue, // 16',
  1646. ' tkRefToProcVar, // 17 variable of procedure type',
  1647. ' tkInterface, // 18',
  1648. ' //tkObject,',
  1649. ' //tkSString,tkLString,tkAString,tkWString,',
  1650. ' //tkVariant,',
  1651. ' //tkWChar,',
  1652. ' //tkInt64,',
  1653. ' //tkQWord,',
  1654. ' //tkInterfaceRaw,',
  1655. ' //tkUString,tkUChar,',
  1656. ' tkHelper, // 19',
  1657. ' //tkFile,',
  1658. ' tkExtClass // 20',
  1659. ' );',
  1660. ' TTypeKinds = set of TTypeKind;',
  1661. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  1662. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo)',
  1663. ' end;',
  1664. ' TTypeInfoEnum = class external name ''rtl.tTypeInfoEnum''(TTypeInfoInteger) end;',
  1665. ' TTypeInfoSet = class external name ''rtl.tTypeInfoSet''(TTypeInfo) end;',
  1666. ' TTypeInfoStaticArray = class external name ''rtl.tTypeInfoStaticArray''(TTypeInfo) end;',
  1667. ' TTypeInfoDynArray = class external name ''rtl.tTypeInfoDynArray''(TTypeInfo) end;',
  1668. ' TTypeInfoProcVar = class external name ''rtl.tTypeInfoProcVar''(TTypeInfo) end;',
  1669. ' TTypeInfoMethodVar = class external name ''rtl.tTypeInfoMethodVar''(TTypeInfoProcVar) end;',
  1670. ' TTypeInfoClass = class external name ''rtl.tTypeInfoClass''(TTypeInfo) end;',
  1671. ' TTypeInfoClassRef = class external name ''rtl.tTypeInfoClassRef''(TTypeInfo) end;',
  1672. ' TTypeInfoExtClass = class external name ''rtl.tTypeInfoExtClass''(TTypeInfo) end;',
  1673. ' TTypeInfoRecord = class external name ''rtl.tTypeInfoRecord''(TTypeInfo) end;',
  1674. ' TTypeInfoPointer = class external name ''rtl.tTypeInfoPointer''(TTypeInfo) end;',
  1675. ' TTypeInfoHelper = class external name ''rtl.tTypeInfoHelper''(TTypeInfo) end;',
  1676. ' TTypeInfoInterface = class external name ''rtl.tTypeInfoInterface''(TTypeInfo) end;',
  1677. '']);
  1678. end;
  1679. if supWriteln in Parts then
  1680. Intf.Add('procedure writeln; varargs; external name ''console.log'';');
  1681. Intf.Add('var');
  1682. Intf.Add(' ExitCode: Longint = 0;');
  1683. // unit implementation
  1684. Impl:=TStringList.Create;
  1685. if supTObject in Parts then
  1686. Impl.AddStrings([
  1687. '// needed by ClassNameIs, the real SameText is in SysUtils',
  1688. 'function SameText(const s1, s2: String): Boolean; assembler;',
  1689. 'asm',
  1690. 'end;',
  1691. 'constructor TObject.Create; begin end;',
  1692. 'destructor TObject.Destroy; begin end;',
  1693. 'class function TObject.ClassType: TClass; assembler;',
  1694. 'asm',
  1695. 'end;',
  1696. 'class function TObject.ClassName: String; assembler;',
  1697. 'asm',
  1698. 'end;',
  1699. 'class function TObject.ClassNameIs(const Name: string): boolean;',
  1700. 'begin',
  1701. ' Result:=SameText(Name,ClassName);',
  1702. 'end;',
  1703. 'class function TObject.ClassParent: TClass; assembler;',
  1704. 'asm',
  1705. 'end;',
  1706. 'class function TObject.InheritsFrom(aClass: TClass): boolean; assembler;',
  1707. 'asm',
  1708. 'end;',
  1709. 'class function TObject.UnitName: String; assembler;',
  1710. 'asm',
  1711. 'end;',
  1712. 'procedure TObject.AfterConstruction; begin end;',
  1713. 'procedure TObject.BeforeDestruction; begin end;',
  1714. 'function TObject.Equals(Obj: TObject): boolean;',
  1715. 'begin',
  1716. ' Result:=Obj=Self;',
  1717. 'end;',
  1718. 'function TObject.ToString: String;',
  1719. 'begin',
  1720. ' Result:=ClassName;',
  1721. 'end;'
  1722. ]);
  1723. if supTInterfacedObject in Parts then
  1724. Impl.AddStrings([
  1725. //'function TInterfacedObject.QueryInterface(const iid: TGuid; out obj): Integer;',
  1726. //'begin',
  1727. //'end;',
  1728. 'function TInterfacedObject._AddRef: Integer;',
  1729. 'begin',
  1730. 'end;',
  1731. 'function TInterfacedObject._Release: Integer;',
  1732. 'begin',
  1733. 'end;',
  1734. '']);
  1735. if supTVarRec in Parts then
  1736. Impl.AddStrings([
  1737. 'function VarRecs: TVarRecArray; varargs;',
  1738. 'var',
  1739. ' v: PVarRec;',
  1740. 'begin',
  1741. ' v^.VType:=1;',
  1742. ' v^.VJSValue:=2;',
  1743. 'end;',
  1744. '']);
  1745. try
  1746. AddModuleWithIntfImplSrc('system.pp',Intf.Text,Impl.Text);
  1747. finally
  1748. Intf.Free;
  1749. Impl.Free;
  1750. end;
  1751. end;
  1752. procedure TCustomTestModule.StartProgram(NeedSystemUnit: boolean;
  1753. SystemUnitParts: TSystemUnitParts);
  1754. begin
  1755. if NeedSystemUnit then
  1756. AddSystemUnit(SystemUnitParts)
  1757. else
  1758. Parser.ImplicitUses.Clear;
  1759. Add('program '+ExtractFileUnitName(Filename)+';');
  1760. Add('');
  1761. end;
  1762. procedure TCustomTestModule.StartUnit(NeedSystemUnit: boolean;
  1763. SystemUnitParts: TSystemUnitParts);
  1764. begin
  1765. if NeedSystemUnit then
  1766. AddSystemUnit(SystemUnitParts)
  1767. else
  1768. Parser.ImplicitUses.Clear;
  1769. Add('unit Test1;');
  1770. Add('');
  1771. end;
  1772. procedure TCustomTestModule.ConvertModule;
  1773. procedure CheckUsesList(UsesName: String; Arg: TJSArrayLiteralElement;
  1774. out UsesLit: TJSArrayLiteral);
  1775. var
  1776. i: Integer;
  1777. Item: TJSElement;
  1778. Lit: TJSLiteral;
  1779. begin
  1780. UsesLit:=nil;
  1781. AssertNotNull(UsesName+' uses section',Arg.Expr);
  1782. if (Arg.Expr.ClassType=TJSLiteral) and TJSLiteral(Arg.Expr).Value.IsNull then
  1783. exit; // null is ok
  1784. AssertEquals(UsesName+' uses section param is array',TJSArrayLiteral,Arg.Expr.ClassType);
  1785. FJSInterfaceUses:=TJSArrayLiteral(Arg.Expr);
  1786. for i:=0 to FJSInterfaceUses.Elements.Count-1 do
  1787. begin
  1788. Item:=FJSInterfaceUses.Elements.Elements[i].Expr;
  1789. AssertNotNull(UsesName+' uses section item['+IntToStr(i)+'].Expr',Item);
  1790. AssertEquals(UsesName+' uses section item['+IntToStr(i)+'] is lit',TJSLiteral,Item.ClassType);
  1791. Lit:=TJSLiteral(Item);
  1792. AssertEquals(UsesName+' uses section item['+IntToStr(i)+'] is string lit',
  1793. ord(jsbase.jstString),ord(Lit.Value.ValueType));
  1794. end;
  1795. end;
  1796. procedure CheckFunctionParam(ParamName: string; Arg: TJSArrayLiteralElement;
  1797. out Src: TJSSourceElements);
  1798. var
  1799. FunDecl: TJSFunctionDeclarationStatement;
  1800. FunDef: TJSFuncDef;
  1801. FunBody: TJSFunctionBody;
  1802. begin
  1803. Src:=nil;
  1804. AssertNotNull(ParamName,Arg.Expr);
  1805. AssertEquals(ParamName+' Arg.Expr type',TJSFunctionDeclarationStatement,Arg.Expr.ClassType);
  1806. FunDecl:=Arg.Expr as TJSFunctionDeclarationStatement;
  1807. AssertNotNull(ParamName+' FunDecl.AFunction',FunDecl.AFunction);
  1808. AssertEquals(ParamName+' FunDecl.AFunction type',TJSFuncDef,FunDecl.AFunction.ClassType);
  1809. FunDef:=FunDecl.AFunction as TJSFuncDef;
  1810. AssertEquals(ParamName+' name empty','',String(FunDef.Name));
  1811. AssertNotNull(ParamName+' body',FunDef.Body);
  1812. AssertEquals(ParamName+' body type',TJSFunctionBody,FunDef.Body.ClassType);
  1813. FunBody:=FunDef.Body as TJSFunctionBody;
  1814. AssertNotNull(ParamName+' body.A',FunBody.A);
  1815. AssertEquals(ParamName+' body.A type',TJSSourceElements,FunBody.A.ClassType);
  1816. Src:=FunBody.A as TJSSourceElements;
  1817. end;
  1818. var
  1819. ModuleNameExpr: TJSLiteral;
  1820. InitFunction: TJSFunctionDeclarationStatement;
  1821. InitAssign: TJSSimpleAssignStatement;
  1822. InitName: String;
  1823. LastNode: TJSElement;
  1824. Arg: TJSArrayLiteralElement;
  1825. begin
  1826. if SkipTests then exit;
  1827. try
  1828. FJSModule:=FConverter.ConvertPasElement(Module,Engine) as TJSSourceElements;
  1829. except
  1830. on E: Exception do
  1831. HandleException(E);
  1832. end;
  1833. if SkipTests then exit;
  1834. if ExpectedErrorClass<>nil then
  1835. Fail('Missing '+ExpectedErrorClass.ClassName+' error {'+ExpectedErrorMsg+'} ('+IntToStr(ExpectedErrorNumber)+')');
  1836. FJSSource:=TStringList.Create;
  1837. FJSSource.Text:=ConvertJSModuleToString(JSModule);
  1838. {$IFDEF VerbosePas2JS}
  1839. writeln('TTestModule.ConvertModule JS:');
  1840. write(FJSSource.Text);
  1841. {$ENDIF}
  1842. // rtl.module(...
  1843. AssertEquals('jsmodule has one statement - the call',1,JSModule.Statements.Count);
  1844. AssertNotNull('register module call',JSModule.Statements.Nodes[0].Node);
  1845. AssertEquals('register module call',TJSCallExpression,JSModule.Statements.Nodes[0].Node.ClassType);
  1846. FJSRegModuleCall:=JSModule.Statements.Nodes[0].Node as TJSCallExpression;
  1847. AssertNotNull('register module rtl.module expr',JSRegModuleCall.Expr);
  1848. AssertNotNull('register module rtl.module args',JSRegModuleCall.Args);
  1849. AssertEquals('rtl.module args',TJSArguments,JSRegModuleCall.Args.ClassType);
  1850. FJSModuleCallArgs:=JSRegModuleCall.Args as TJSArguments;
  1851. // parameter 'unitname'
  1852. if JSModuleCallArgs.Elements.Count<1 then
  1853. Fail('rtl.module first param unit missing');
  1854. Arg:=JSModuleCallArgs.Elements.Elements[0];
  1855. AssertNotNull('module name param',Arg.Expr);
  1856. ModuleNameExpr:=Arg.Expr as TJSLiteral;
  1857. AssertEquals('module name param is string',ord(jstString),ord(ModuleNameExpr.Value.ValueType));
  1858. if Module is TPasProgram then
  1859. AssertEquals('module name','program',String(ModuleNameExpr.Value.AsString))
  1860. else
  1861. AssertEquals('module name',Module.Name,String(ModuleNameExpr.Value.AsString));
  1862. // main uses section
  1863. if JSModuleCallArgs.Elements.Count<2 then
  1864. Fail('rtl.module second param main uses missing');
  1865. Arg:=JSModuleCallArgs.Elements.Elements[1];
  1866. CheckUsesList('interface',Arg,FJSInterfaceUses);
  1867. // program/library/interface function()
  1868. if JSModuleCallArgs.Elements.Count<3 then
  1869. Fail('rtl.module third param intf-function missing');
  1870. Arg:=JSModuleCallArgs.Elements.Elements[2];
  1871. CheckFunctionParam('module intf-function',Arg,FJSModuleSrc);
  1872. // search for $mod.$init or $mod.$main - the last statement
  1873. if Module is TPasProgram then
  1874. begin
  1875. InitName:='$main';
  1876. AssertEquals('$mod.'+InitName+' function 1',true,JSModuleSrc.Statements.Count>0);
  1877. end
  1878. else
  1879. InitName:='$init';
  1880. FJSInitBody:=nil;
  1881. if JSModuleSrc.Statements.Count>0 then
  1882. begin
  1883. LastNode:=JSModuleSrc.Statements.Nodes[JSModuleSrc.Statements.Count-1].Node;
  1884. if LastNode is TJSSimpleAssignStatement then
  1885. begin
  1886. InitAssign:=LastNode as TJSSimpleAssignStatement;
  1887. if GetDottedIdentifier(InitAssign.LHS)='$mod.'+InitName then
  1888. begin
  1889. InitFunction:=InitAssign.Expr as TJSFunctionDeclarationStatement;
  1890. FJSInitBody:=InitFunction.AFunction.Body as TJSFunctionBody;
  1891. end
  1892. else if Module is TPasProgram then
  1893. CheckDottedIdentifier('init function',InitAssign.LHS,'$mod.'+InitName);
  1894. end;
  1895. end;
  1896. // optional: implementation uses section
  1897. if JSModuleCallArgs.Elements.Count<4 then
  1898. exit;
  1899. Arg:=JSModuleCallArgs.Elements.Elements[3];
  1900. CheckUsesList('implementation',Arg,FJSImplentationUses);
  1901. end;
  1902. procedure TCustomTestModule.ConvertProgram;
  1903. begin
  1904. Add('end.');
  1905. ParseProgram;
  1906. ConvertModule;
  1907. end;
  1908. procedure TCustomTestModule.ConvertUnit;
  1909. begin
  1910. Add('end.');
  1911. ParseUnit;
  1912. ConvertModule;
  1913. end;
  1914. function TCustomTestModule.ConvertJSModuleToString(El: TJSElement): string;
  1915. begin
  1916. Result:=tcmodules.JSToStr(El);
  1917. end;
  1918. procedure TCustomTestModule.CheckDottedIdentifier(Msg: string; El: TJSElement;
  1919. DottedName: string);
  1920. begin
  1921. if DottedName='' then
  1922. begin
  1923. AssertNull(Msg,El);
  1924. end
  1925. else
  1926. begin
  1927. AssertNotNull(Msg,El);
  1928. AssertEquals(Msg,DottedName,GetDottedIdentifier(El));
  1929. end;
  1930. end;
  1931. function TCustomTestModule.GetDottedIdentifier(El: TJSElement): string;
  1932. begin
  1933. if El=nil then
  1934. Result:=''
  1935. else if El is TJSPrimaryExpressionIdent then
  1936. Result:=String(TJSPrimaryExpressionIdent(El).Name)
  1937. else if El is TJSDotMemberExpression then
  1938. Result:=GetDottedIdentifier(TJSDotMemberExpression(El).MExpr)+'.'+String(TJSDotMemberExpression(El).Name)
  1939. else
  1940. AssertEquals('GetDottedIdentifier',TJSPrimaryExpressionIdent,El.ClassType);
  1941. end;
  1942. procedure TCustomTestModule.CheckSource(Msg, Statements: String;
  1943. InitStatements: string; ImplStatements: string);
  1944. var
  1945. ActualSrc, ExpectedSrc, InitName: String;
  1946. begin
  1947. ActualSrc:=JSToStr(JSModuleSrc);
  1948. if coUseStrict in Converter.Options then
  1949. ExpectedSrc:='"use strict";'+LineEnding
  1950. else
  1951. ExpectedSrc:='';
  1952. ExpectedSrc:=ExpectedSrc+'var $mod = this;'+LineEnding;
  1953. ExpectedSrc:=ExpectedSrc+Statements;
  1954. // unit implementation
  1955. if (Trim(ImplStatements)<>'') then
  1956. ExpectedSrc:=ExpectedSrc+LineEnding
  1957. +'$mod.$implcode = function () {'+LineEnding
  1958. +ImplStatements
  1959. +'};'+LineEnding;
  1960. // program main or unit initialization
  1961. if (Module is TPasProgram) or (Trim(InitStatements)<>'') then
  1962. begin
  1963. if Module is TPasProgram then
  1964. InitName:='$main'
  1965. else
  1966. InitName:='$init';
  1967. ExpectedSrc:=ExpectedSrc+LineEnding
  1968. +'$mod.'+InitName+' = function () {'+LineEnding
  1969. +InitStatements
  1970. +'};'+LineEnding;
  1971. end;
  1972. //writeln('TCustomTestModule.CheckSource ExpectedIntf="',ExpectedSrc,'"');
  1973. //writeln('TTestModule.CheckSource InitStatements="',Trim(InitStatements),'"');
  1974. CheckDiff(Msg,ExpectedSrc,ActualSrc);
  1975. end;
  1976. procedure TCustomTestModule.CheckDiff(Msg, Expected, Actual: string);
  1977. // search diff, ignore changes in spaces
  1978. var
  1979. s: string;
  1980. begin
  1981. if CheckSrcDiff(Expected,Actual,s) then exit;
  1982. Fail(Msg+': '+s);
  1983. end;
  1984. procedure TCustomTestModule.CheckUnit(Filename, ExpectedSrc: string);
  1985. var
  1986. aResolver: TTestEnginePasResolver;
  1987. aConverter: TPasToJSConverter;
  1988. aJSModule: TJSSourceElements;
  1989. ActualSrc: String;
  1990. begin
  1991. aResolver:=GetResolver(Filename);
  1992. AssertNotNull('missing resolver of unit '+Filename,aResolver);
  1993. AssertNotNull('missing resolver.module of unit '+Filename,aResolver.Module);
  1994. {$IFDEF VerbosePas2JS}
  1995. writeln('CheckUnit '+Filename+' converting ...');
  1996. {$ENDIF}
  1997. aConverter:=CreateConverter;
  1998. aJSModule:=nil;
  1999. try
  2000. try
  2001. aJSModule:=aConverter.ConvertPasElement(aResolver.Module,aResolver) as TJSSourceElements;
  2002. except
  2003. on E: Exception do
  2004. HandleException(E);
  2005. end;
  2006. ActualSrc:=ConvertJSModuleToString(aJSModule);
  2007. {$IFDEF VerbosePas2JS}
  2008. writeln('TTestModule.CheckUnit ',Filename,' Pas:');
  2009. write(aResolver.Source);
  2010. writeln('TTestModule.CheckUnit ',Filename,' JS:');
  2011. write(ActualSrc);
  2012. {$ENDIF}
  2013. CheckDiff('Converted unit: "'+ChangeFileExt(Filename,'.js')+'"',ExpectedSrc,ActualSrc);
  2014. finally
  2015. aJSModule.Free;
  2016. aConverter.Free;
  2017. end;
  2018. end;
  2019. procedure TCustomTestModule.CheckHint(MsgType: TMessageType;
  2020. MsgNumber: integer; Msg: string; Marker: PSrcMarker);
  2021. var
  2022. i: Integer;
  2023. Item: TTestHintMessage;
  2024. Expected,Actual: string;
  2025. begin
  2026. //writeln('TCustomTestModule.CheckHint MsgCount=',MsgCount);
  2027. for i:=0 to MsgCount-1 do
  2028. begin
  2029. Item:=Msgs[i];
  2030. if (Item.MsgNumber<>MsgNumber) or (Item.Msg<>Msg) then continue;
  2031. if (Marker<>nil) then
  2032. begin
  2033. if Item.SourcePos.Row<>cardinal(Marker^.Row) then continue;
  2034. if (Item.SourcePos.Column<cardinal(Marker^.StartCol))
  2035. or (Item.SourcePos.Column>cardinal(Marker^.EndCol)) then continue;
  2036. end;
  2037. // found
  2038. FHintMsgsGood.Add(Item);
  2039. str(Item.MsgType,Actual);
  2040. str(MsgType,Expected);
  2041. AssertEquals('MsgType',Expected,Actual);
  2042. exit;
  2043. end;
  2044. // needed message missing -> show emitted messages
  2045. WriteSources('',0,0);
  2046. for i:=0 to MsgCount-1 do
  2047. begin
  2048. Item:=Msgs[i];
  2049. write('TCustomTestModule.CheckHint ',i,'/',MsgCount,' ',Item.MsgType,
  2050. ' ('+IntToStr(Item.MsgNumber),')');
  2051. if Marker<>nil then
  2052. write(' '+ExtractFileName(Item.SourcePos.FileName),'(',Item.SourcePos.Row,',',Item.SourcePos.Column,')');
  2053. writeln(' {',Item.Msg,'}');
  2054. end;
  2055. str(MsgType,Expected);
  2056. Actual:='Missing '+Expected+' ('+IntToStr(MsgNumber)+')';
  2057. if Marker<>nil then
  2058. Actual:=Actual+' '+ExtractFileName(Marker^.Filename)+'('+IntToStr(Marker^.Row)+','+IntToStr(Marker^.StartCol)+'..'+IntToStr(Marker^.EndCol)+')';
  2059. Actual:=Actual+' '+Msg;
  2060. Fail(Actual);
  2061. end;
  2062. procedure TCustomTestModule.CheckResolverUnexpectedHints(WithSourcePos: boolean
  2063. );
  2064. var
  2065. i: Integer;
  2066. s, Txt: String;
  2067. Msg: TTestHintMessage;
  2068. begin
  2069. for i:=0 to MsgCount-1 do
  2070. begin
  2071. Msg:=Msgs[i];
  2072. if FHintMsgsGood.IndexOf(Msg)>=0 then continue;
  2073. s:='';
  2074. str(Msg.MsgType,s);
  2075. Txt:='Unexpected resolver message found ['+IntToStr(Msg.Id)+'] '
  2076. +s+': ('+IntToStr(Msg.MsgNumber)+')';
  2077. if WithSourcePos then
  2078. Txt:=Txt+' '+ExtractFileName(Msg.SourcePos.FileName)+'('+IntToStr(Msg.SourcePos.Row)+','+IntToStr(Msg.SourcePos.Column)+')';
  2079. Txt:=Txt+' {'+Msg.Msg+'}';
  2080. Fail(Txt);
  2081. end;
  2082. end;
  2083. procedure TCustomTestModule.SetExpectedScannerError(Msg: string;
  2084. MsgNumber: integer);
  2085. begin
  2086. ExpectedErrorClass:=EScannerError;
  2087. ExpectedErrorMsg:=Msg;
  2088. ExpectedErrorNumber:=MsgNumber;
  2089. end;
  2090. procedure TCustomTestModule.SetExpectedParserError(Msg: string;
  2091. MsgNumber: integer);
  2092. begin
  2093. ExpectedErrorClass:=EParserError;
  2094. ExpectedErrorMsg:=Msg;
  2095. ExpectedErrorNumber:=MsgNumber;
  2096. end;
  2097. procedure TCustomTestModule.SetExpectedPasResolverError(Msg: string;
  2098. MsgNumber: integer);
  2099. begin
  2100. ExpectedErrorClass:=EPasResolve;
  2101. ExpectedErrorMsg:=Msg;
  2102. ExpectedErrorNumber:=MsgNumber;
  2103. end;
  2104. procedure TCustomTestModule.SetExpectedConverterError(Msg: string;
  2105. MsgNumber: integer);
  2106. begin
  2107. ExpectedErrorClass:=EPas2JS;
  2108. ExpectedErrorMsg:=Msg;
  2109. ExpectedErrorNumber:=MsgNumber;
  2110. end;
  2111. function TCustomTestModule.IsErrorExpected(E: Exception): boolean;
  2112. var
  2113. MsgNumber: Integer;
  2114. Msg: String;
  2115. begin
  2116. Result:=false;
  2117. if (ExpectedErrorClass=nil) or (ExpectedErrorClass<>E.ClassType) then exit;
  2118. Msg:=E.Message;
  2119. if E is EPas2JS then
  2120. MsgNumber:=EPas2JS(E).MsgNumber
  2121. else if E is EPasResolve then
  2122. MsgNumber:=EPasResolve(E).MsgNumber
  2123. else if E is EParserError then
  2124. MsgNumber:=Parser.LastMsgNumber
  2125. else if E is EScannerError then
  2126. begin
  2127. MsgNumber:=Scanner.LastMsgNumber;
  2128. Msg:=Scanner.LastMsg;
  2129. end
  2130. else
  2131. MsgNumber:=0;
  2132. Result:=(MsgNumber=ExpectedErrorNumber) and (Msg=ExpectedErrorMsg);
  2133. if Result then
  2134. SkipTests:=true;
  2135. end;
  2136. procedure TCustomTestModule.HandleScannerError(E: EScannerError);
  2137. begin
  2138. if IsErrorExpected(E) then exit;
  2139. WriteSources(Scanner.CurFilename,Scanner.CurRow,Scanner.CurColumn);
  2140. writeln('ERROR: TCustomTestModule.HandleScannerError '+E.ClassName+':'+E.Message
  2141. +' '+Scanner.CurFilename
  2142. +'('+IntToStr(Scanner.CurRow)+','+IntToStr(Scanner.CurColumn)+')');
  2143. FailException(E);
  2144. end;
  2145. procedure TCustomTestModule.HandleParserError(E: EParserError);
  2146. begin
  2147. if IsErrorExpected(E) then exit;
  2148. WriteSources(E.Filename,E.Row,E.Column);
  2149. writeln('ERROR: TCustomTestModule.HandleParserError '+E.ClassName+':'+E.Message
  2150. +' '+E.Filename+'('+IntToStr(E.Row)+','+IntToStr(E.Column)+')'
  2151. +' MainModuleScannerLine="'+Scanner.CurLine+'"'
  2152. );
  2153. FailException(E);
  2154. end;
  2155. procedure TCustomTestModule.HandlePasResolveError(E: EPasResolve);
  2156. var
  2157. P: TPasSourcePos;
  2158. begin
  2159. if IsErrorExpected(E) then exit;
  2160. P:=E.SourcePos;
  2161. WriteSources(P.FileName,P.Row,P.Column);
  2162. writeln('ERROR: TCustomTestModule.HandlePasResolveError '+E.ClassName+':'+E.Message
  2163. +' '+P.FileName+'('+IntToStr(P.Row)+','+IntToStr(P.Column)+')');
  2164. FailException(E);
  2165. end;
  2166. procedure TCustomTestModule.HandlePas2JSError(E: EPas2JS);
  2167. var
  2168. Row, Col: integer;
  2169. begin
  2170. if IsErrorExpected(E) then exit;
  2171. Engine.UnmangleSourceLineNumber(E.PasElement.SourceLinenumber,Row,Col);
  2172. WriteSources(E.PasElement.SourceFilename,Row,Col);
  2173. writeln('ERROR: TCustomTestModule.HandlePas2JSError '+E.ClassName+':'+E.Message
  2174. +' '+E.PasElement.SourceFilename
  2175. +'('+IntToStr(Row)+','+IntToStr(Col)+')');
  2176. FailException(E);
  2177. end;
  2178. procedure TCustomTestModule.HandleException(E: Exception);
  2179. begin
  2180. if E is EScannerError then
  2181. HandleScannerError(EScannerError(E))
  2182. else if E is EParserError then
  2183. HandleParserError(EParserError(E))
  2184. else if E is EPasResolve then
  2185. HandlePasResolveError(EPasResolve(E))
  2186. else if E is EPas2JS then
  2187. HandlePas2JSError(EPas2JS(E))
  2188. else
  2189. begin
  2190. if IsErrorExpected(E) then exit;
  2191. if not (E is EAssertionFailedError) then
  2192. begin
  2193. WriteSources('',0,0);
  2194. writeln('ERROR: TCustomTestModule.HandleException '+E.ClassName+':'+E.Message);
  2195. end;
  2196. FailException(E);
  2197. end;
  2198. end;
  2199. procedure TCustomTestModule.FailException(E: Exception);
  2200. var
  2201. MsgNumber: Integer;
  2202. begin
  2203. if ExpectedErrorClass<>nil then
  2204. begin
  2205. if FExpectedErrorClass=E.ClassType then
  2206. begin
  2207. if E is EPas2JS then
  2208. MsgNumber:=EPas2JS(E).MsgNumber
  2209. else if E is EPasResolve then
  2210. MsgNumber:=EPasResolve(E).MsgNumber
  2211. else if E is EParserError then
  2212. MsgNumber:=Parser.LastMsgNumber
  2213. else if E is EScannerError then
  2214. MsgNumber:=Scanner.LastMsgNumber
  2215. else
  2216. MsgNumber:=0;
  2217. AssertEquals('Expected error message ('+IntToStr(ExpectedErrorNumber)+')','{'+ExpectedErrorMsg+'}','{'+E.Message+'}');
  2218. AssertEquals('Expected {'+ExpectedErrorMsg+'}, but got msg {'+E.Message+'} number',
  2219. ExpectedErrorNumber,MsgNumber);
  2220. end else begin
  2221. AssertEquals('Wrong exception class',ExpectedErrorClass.ClassName,E.ClassName);
  2222. end;
  2223. end;
  2224. Fail(E.Message);
  2225. end;
  2226. procedure TCustomTestModule.WriteSources(const aFilename: string; aRow,
  2227. aCol: integer);
  2228. var
  2229. IsSrc: Boolean;
  2230. i, j: Integer;
  2231. SrcLines: TStringList;
  2232. Line: string;
  2233. aModule: TTestEnginePasResolver;
  2234. begin
  2235. writeln('TCustomTestModule.WriteSources File="',aFilename,'" Row=',aRow,' Col=',aCol);
  2236. for i:=0 to ResolverCount-1 do
  2237. begin
  2238. aModule:=Resolvers[i];
  2239. SrcLines:=TStringList.Create;
  2240. try
  2241. SrcLines.Text:=aModule.Source;
  2242. IsSrc:=ExtractFilename(aModule.Filename)=ExtractFileName(aFilename);
  2243. writeln('Testcode:-File="',aModule.Filename,'"----------------------------------:');
  2244. for j:=1 to SrcLines.Count do
  2245. begin
  2246. Line:=SrcLines[j-1];
  2247. if IsSrc and (j=aRow) then
  2248. begin
  2249. write('*');
  2250. Line:=LeftStr(Line,aCol-1)+'|'+copy(Line,aCol,length(Line));
  2251. end;
  2252. writeln(Format('%:4d: ',[j]),Line);
  2253. end;
  2254. finally
  2255. SrcLines.Free;
  2256. end;
  2257. end;
  2258. end;
  2259. function TCustomTestModule.IndexOfResolver(const Filename: string): integer;
  2260. var
  2261. i: Integer;
  2262. begin
  2263. for i:=0 to ResolverCount-1 do
  2264. if Filename=Resolvers[i].Filename then exit(i);
  2265. Result:=-1;
  2266. end;
  2267. function TCustomTestModule.GetResolver(const Filename: string
  2268. ): TTestEnginePasResolver;
  2269. var
  2270. i: Integer;
  2271. begin
  2272. i:=IndexOfResolver(Filename);
  2273. if i<0 then exit(nil);
  2274. Result:=Resolvers[i];
  2275. end;
  2276. function TCustomTestModule.GetDefaultNamespace: string;
  2277. var
  2278. C: TClass;
  2279. begin
  2280. Result:='';
  2281. if FModule=nil then exit;
  2282. C:=FModule.ClassType;
  2283. if (C=TPasProgram) or (C=TPasLibrary) or (C=TPasPackage) then
  2284. Result:=Engine.DefaultNameSpace;
  2285. end;
  2286. constructor TCustomTestModule.Create;
  2287. begin
  2288. inherited Create;
  2289. FHintMsgs:=TObjectList.Create(true);
  2290. FHintMsgsGood:=TFPList.Create;
  2291. end;
  2292. destructor TCustomTestModule.Destroy;
  2293. begin
  2294. FreeAndNil(FHintMsgs);
  2295. FreeAndNil(FHintMsgsGood);
  2296. inherited Destroy;
  2297. end;
  2298. { TTestModule }
  2299. procedure TTestModule.TestReservedWords;
  2300. var
  2301. i: integer;
  2302. begin
  2303. for i:=low(JSReservedWords) to High(JSReservedWords)-1 do
  2304. if CompareStr(JSReservedWords[i],JSReservedWords[i+1])>=0 then
  2305. Fail('20170203135442 '+JSReservedWords[i]+' >= '+JSReservedWords[i+1]);
  2306. for i:=low(JSReservedGlobalWords) to High(JSReservedGlobalWords)-1 do
  2307. if CompareStr(JSReservedGlobalWords[i],JSReservedGlobalWords[i+1])>=0 then
  2308. Fail('20170203135443 '+JSReservedGlobalWords[i]+' >= '+JSReservedGlobalWords[i+1]);
  2309. end;
  2310. procedure TTestModule.TestEmptyProgram;
  2311. begin
  2312. StartProgram(false);
  2313. Add('begin');
  2314. ConvertProgram;
  2315. CheckSource('TestEmptyProgram','','');
  2316. end;
  2317. procedure TTestModule.TestEmptyProgramUseStrict;
  2318. begin
  2319. Converter.Options:=Converter.Options+[coUseStrict];
  2320. StartProgram(false);
  2321. Add('begin');
  2322. ConvertProgram;
  2323. CheckSource('TestEmptyProgramUseStrict','','');
  2324. end;
  2325. procedure TTestModule.TestEmptyUnit;
  2326. begin
  2327. StartUnit(false);
  2328. Add('interface');
  2329. Add('implementation');
  2330. ConvertUnit;
  2331. CheckSource('TestEmptyUnit',
  2332. LinesToStr([
  2333. ]),
  2334. '');
  2335. end;
  2336. procedure TTestModule.TestEmptyUnitUseStrict;
  2337. begin
  2338. Converter.Options:=Converter.Options+[coUseStrict];
  2339. StartUnit(false);
  2340. Add('interface');
  2341. Add('implementation');
  2342. ConvertUnit;
  2343. CheckSource('TestEmptyUnitUseStrict',
  2344. LinesToStr([
  2345. ''
  2346. ]),
  2347. '');
  2348. end;
  2349. procedure TTestModule.TestDottedUnitNames;
  2350. begin
  2351. AddModuleWithIntfImplSrc('NS1.Unit2.pas',
  2352. LinesToStr([
  2353. 'var iV: longint;'
  2354. ]),
  2355. '');
  2356. FFilename:='ns1.test1.pp';
  2357. StartProgram(true);
  2358. Add('uses unIt2;');
  2359. Add('var');
  2360. Add(' i: longint;');
  2361. Add('begin');
  2362. Add(' i:=iv;');
  2363. Add(' i:=uNit2.iv;');
  2364. Add(' i:=Ns1.TEst1.i;');
  2365. ConvertProgram;
  2366. CheckSource('TestDottedUnitNames',
  2367. LinesToStr([
  2368. 'this.i = 0;',
  2369. '']),
  2370. LinesToStr([ // this.$init
  2371. '$mod.i = pas["NS1.Unit2"].iV;',
  2372. '$mod.i = pas["NS1.Unit2"].iV;',
  2373. '$mod.i = $mod.i;',
  2374. '']) );
  2375. end;
  2376. procedure TTestModule.TestDottedUnitNameImpl;
  2377. begin
  2378. AddModuleWithIntfImplSrc('TEST.UnitA.pas',
  2379. LinesToStr([
  2380. 'type',
  2381. ' TObject = class end;',
  2382. ' TTestA = class',
  2383. ' end;'
  2384. ]),
  2385. LinesToStr(['uses TEST.UnitB;'])
  2386. );
  2387. AddModuleWithIntfImplSrc('TEST.UnitB.pas',
  2388. LinesToStr([
  2389. 'uses TEST.UnitA;',
  2390. 'type TTestB = class(TTestA);'
  2391. ]),
  2392. ''
  2393. );
  2394. StartProgram(true);
  2395. Add('uses TEST.UnitA;');
  2396. Add('begin');
  2397. ConvertProgram;
  2398. CheckSource('TestDottedUnitNameImpl',
  2399. LinesToStr([
  2400. '']),
  2401. LinesToStr([ // this.$init
  2402. '']) );
  2403. CheckUnit('TEST.UnitA.pas',
  2404. LinesToStr([
  2405. 'rtl.module("TEST.UnitA", ["system"], function () {',
  2406. ' var $mod = this;',
  2407. ' rtl.createClass(this, "TObject", null, function () {',
  2408. ' this.$init = function () {',
  2409. ' };',
  2410. ' this.$final = function () {',
  2411. ' };',
  2412. ' });',
  2413. ' rtl.createClass(this, "TTestA", this.TObject, function () {',
  2414. ' });',
  2415. '}, ["TEST.UnitB"]);'
  2416. ]));
  2417. CheckUnit('TEST.UnitB.pas',
  2418. LinesToStr([
  2419. 'rtl.module("TEST.UnitB", ["system","TEST.UnitA"], function () {',
  2420. ' var $mod = this;',
  2421. ' rtl.createClass(this, "TTestB", pas["TEST.UnitA"].TTestA, function () {',
  2422. ' });',
  2423. '});'
  2424. ]));
  2425. end;
  2426. procedure TTestModule.TestDottedUnitExpr;
  2427. begin
  2428. AddModuleWithIntfImplSrc('NS2.SubNs2.Unit2.pas',
  2429. LinesToStr([
  2430. 'procedure DoIt;'
  2431. ]),
  2432. 'procedure DoIt; begin end;');
  2433. FFilename:='Ns1.SubNs1.Test1.pp';
  2434. StartProgram(true);
  2435. Add('uses Ns2.sUbnS2.unIt2;');
  2436. Add('var');
  2437. Add(' i: longint;');
  2438. Add('begin');
  2439. Add(' ns2.subns2.unit2.doit;');
  2440. Add(' i:=Ns1.SubNS1.TEst1.i;');
  2441. ConvertProgram;
  2442. CheckSource('TestDottedUnitExpr',
  2443. LinesToStr([
  2444. 'this.i = 0;',
  2445. '']),
  2446. LinesToStr([ // this.$init
  2447. 'pas["NS2.SubNs2.Unit2"].DoIt();',
  2448. '$mod.i = $mod.i;',
  2449. '']) );
  2450. end;
  2451. procedure TTestModule.Test_ModeFPCFail;
  2452. begin
  2453. StartProgram(false);
  2454. Add('{$mode FPC}');
  2455. Add('begin');
  2456. SetExpectedScannerError('Invalid mode: "FPC"',nErrInvalidMode);
  2457. ConvertProgram;
  2458. end;
  2459. procedure TTestModule.Test_ModeSwitchCBlocksFail;
  2460. begin
  2461. StartProgram(false);
  2462. Add('{$modeswitch cblocks-}');
  2463. Add('begin');
  2464. ConvertProgram;
  2465. CheckHint(mtWarning,nErrInvalidModeSwitch,'Warning: test1.pp(3,23) : Invalid mode switch: "cblocks"');
  2466. CheckResolverUnexpectedHints();
  2467. end;
  2468. procedure TTestModule.TestUnit_UseSystem;
  2469. begin
  2470. StartUnit(true);
  2471. Add([
  2472. 'interface',
  2473. 'var i: integer;',
  2474. 'implementation']);
  2475. ConvertUnit;
  2476. CheckSource('TestUnit_UseSystem',
  2477. LinesToStr([
  2478. 'this.i = 0;',
  2479. '']),
  2480. LinesToStr([
  2481. '']) );
  2482. end;
  2483. procedure TTestModule.TestUnit_Intf1Impl2Intf1;
  2484. begin
  2485. AddModuleWithIntfImplSrc('unit1.pp',
  2486. LinesToStr([
  2487. 'type number = longint;']),
  2488. LinesToStr([
  2489. 'uses test1;',
  2490. 'procedure DoIt;',
  2491. 'begin',
  2492. ' i:=3;',
  2493. 'end;']));
  2494. StartUnit(true);
  2495. Add([
  2496. 'interface',
  2497. 'uses unit1;',
  2498. 'var i: number;',
  2499. 'implementation']);
  2500. ConvertUnit;
  2501. CheckSource('TestUnit_Intf1Impl2Intf1',
  2502. LinesToStr([
  2503. 'this.i = 0;',
  2504. '']),
  2505. LinesToStr([
  2506. '']) );
  2507. end;
  2508. procedure TTestModule.TestIncludeVersion;
  2509. begin
  2510. StartProgram(false);
  2511. Add([
  2512. 'var',
  2513. ' s: string;',
  2514. ' i: word;',
  2515. 'begin',
  2516. ' s:={$I %line%};',
  2517. ' i:={$I %linenum%};',
  2518. ' s:={$I %currentroutine%};',
  2519. ' s:={$I %pas2jsversion%};',
  2520. ' s:={$I %pas2jstarget%};',
  2521. ' s:={$I %pas2jstargetos%};',
  2522. ' s:={$I %pas2jstargetcpu%};',
  2523. ' s:={$I %file%};',
  2524. '']);
  2525. ConvertProgram;
  2526. CheckSource('TestIncludeVersion',
  2527. LinesToStr([
  2528. 'this.s="";',
  2529. 'this.i = 0;']),
  2530. LinesToStr([
  2531. '$mod.s = "7";',
  2532. '$mod.i = 8;',
  2533. '$mod.s = "<anonymous>";',
  2534. '$mod.s = "Comp.Ver.tcmodules";',
  2535. '$mod.s = "Browser";',
  2536. '$mod.s = "Browser";',
  2537. '$mod.s = "ECMAScript5";',
  2538. '$mod.s = "test1.pp";',
  2539. '']));
  2540. end;
  2541. procedure TTestModule.TestVarInt;
  2542. begin
  2543. StartProgram(false);
  2544. Add('var MyI: longint;');
  2545. Add('begin');
  2546. ConvertProgram;
  2547. CheckSource('TestVarInt','this.MyI=0;','');
  2548. end;
  2549. procedure TTestModule.TestVarBaseTypes;
  2550. begin
  2551. StartProgram(false);
  2552. Add('var');
  2553. Add(' i: longint;');
  2554. Add(' s: string;');
  2555. Add(' c: char;');
  2556. Add(' b: boolean;');
  2557. Add(' d: double;');
  2558. Add(' i2: longint = 3;');
  2559. Add(' s2: string = ''foo'';');
  2560. Add(' c2: char = ''4'';');
  2561. Add(' b2: boolean = true;');
  2562. Add(' d2: double = 5.6;');
  2563. Add(' i3: longint = $707;');
  2564. Add(' i4: nativeint = 9007199254740991;');
  2565. Add(' i5: nativeint = -9007199254740991-1;');
  2566. Add(' i6: nativeint = $fffffffffffff;');
  2567. Add(' i7: nativeint = -$fffffffffffff-1;');
  2568. Add(' i8: byte = 00;');
  2569. Add(' u8: nativeuint = $fffffffffffff;');
  2570. Add(' u9: nativeuint = $0000000000000;');
  2571. Add(' u10: nativeuint = $00ff00;');
  2572. Add('begin');
  2573. ConvertProgram;
  2574. CheckSource('TestVarBaseTypes',
  2575. LinesToStr([
  2576. 'this.i = 0;',
  2577. 'this.s = "";',
  2578. 'this.c = "";',
  2579. 'this.b = false;',
  2580. 'this.d = 0.0;',
  2581. 'this.i2 = 3;',
  2582. 'this.s2 = "foo";',
  2583. 'this.c2 = "4";',
  2584. 'this.b2 = true;',
  2585. 'this.d2 = 5.6;',
  2586. 'this.i3 = 0x707;',
  2587. 'this.i4 = 9007199254740991;',
  2588. 'this.i5 = -9007199254740991-1;',
  2589. 'this.i6 = 0xfffffffffffff;',
  2590. 'this.i7 =-0xfffffffffffff-1;',
  2591. 'this.i8 = 0;',
  2592. 'this.u8 = 0xfffffffffffff;',
  2593. 'this.u9 = 0x0;',
  2594. 'this.u10 = 0xff00;'
  2595. ]),
  2596. '');
  2597. end;
  2598. procedure TTestModule.TestBaseTypeSingleFail;
  2599. begin
  2600. StartProgram(false);
  2601. Add('var s: single;');
  2602. SetExpectedPasResolverError('identifier not found "single"',PasResolveEval.nIdentifierNotFound);
  2603. ConvertProgram;
  2604. end;
  2605. procedure TTestModule.TestBaseTypeExtendedFail;
  2606. begin
  2607. StartProgram(false);
  2608. Add('var e: extended;');
  2609. SetExpectedPasResolverError('identifier not found "extended"',PasResolveEval.nIdentifierNotFound);
  2610. ConvertProgram;
  2611. end;
  2612. procedure TTestModule.TestConstBaseTypes;
  2613. begin
  2614. StartProgram(false);
  2615. Add('const');
  2616. Add(' i: longint = 3;');
  2617. Add(' s: string = ''foo'';');
  2618. Add(' c: char = ''4'';');
  2619. Add(' b: boolean = true;');
  2620. Add(' d: double = 5.6;');
  2621. Add(' e = low(word);');
  2622. Add(' f = high(word);');
  2623. Add('begin');
  2624. ConvertProgram;
  2625. CheckSource('TestVarBaseTypes',
  2626. LinesToStr([
  2627. 'this.i=3;',
  2628. 'this.s="foo";',
  2629. 'this.c="4";',
  2630. 'this.b=true;',
  2631. 'this.d=5.6;',
  2632. 'this.e = 0;',
  2633. 'this.f = 65535;'
  2634. ]),
  2635. '');
  2636. end;
  2637. procedure TTestModule.TestAliasTypeRef;
  2638. begin
  2639. StartProgram(false);
  2640. Add('type');
  2641. Add(' a=longint;');
  2642. Add(' b=a;');
  2643. Add('var');
  2644. Add(' c: A;');
  2645. Add(' d: B;');
  2646. Add('begin');
  2647. ConvertProgram;
  2648. CheckSource('TestAliasTypeRef',
  2649. LinesToStr([ // statements
  2650. 'this.c = 0;',
  2651. 'this.d = 0;'
  2652. ]),
  2653. LinesToStr([ // this.$main
  2654. ''
  2655. ]));
  2656. end;
  2657. procedure TTestModule.TestTypeCast_BaseTypes;
  2658. begin
  2659. StartProgram(false);
  2660. Add([
  2661. 'var',
  2662. ' i: longint;',
  2663. ' b: boolean;',
  2664. ' d: double;',
  2665. ' s: string;',
  2666. ' c: char;',
  2667. 'begin',
  2668. ' i:=longint(i);',
  2669. ' i:=longint(b);',
  2670. ' b:=boolean(b);',
  2671. ' b:=boolean(i);',
  2672. ' d:=double(d);',
  2673. ' d:=double(i);',
  2674. ' s:=string(s);',
  2675. ' s:=string(c);',
  2676. ' c:=char(c);',
  2677. ' c:=char(i);',
  2678. ' c:=char(65);',
  2679. ' c:=char(#10);',
  2680. ' c:=char(#$E000);',
  2681. '']);
  2682. ConvertProgram;
  2683. CheckSource('TestAliasTypeRef',
  2684. LinesToStr([ // statements
  2685. 'this.i = 0;',
  2686. 'this.b = false;',
  2687. 'this.d = 0.0;',
  2688. 'this.s = "";',
  2689. 'this.c = "";',
  2690. '']),
  2691. LinesToStr([ // this.$main
  2692. '$mod.i = $mod.i;',
  2693. '$mod.i = ($mod.b ? 1 : 0);',
  2694. '$mod.b = $mod.b;',
  2695. '$mod.b = $mod.i != 0;',
  2696. '$mod.d = $mod.d;',
  2697. '$mod.d = $mod.i;',
  2698. '$mod.s = $mod.s;',
  2699. '$mod.s = $mod.c;',
  2700. '$mod.c = $mod.c;',
  2701. '$mod.c = String.fromCharCode($mod.i);',
  2702. '$mod.c = "A";',
  2703. '$mod.c = "\n";',
  2704. '$mod.c = "";',
  2705. '']));
  2706. end;
  2707. procedure TTestModule.TestTypeCast_AliasBaseTypes;
  2708. begin
  2709. StartProgram(false);
  2710. Add('type');
  2711. Add(' integer = longint;');
  2712. Add(' TYesNo = boolean;');
  2713. Add(' TFloat = double;');
  2714. Add(' TCaption = string;');
  2715. Add(' TChar = char;');
  2716. Add('var');
  2717. Add(' i: integer;');
  2718. Add(' b: TYesNo;');
  2719. Add(' d: TFloat;');
  2720. Add(' s: TCaption;');
  2721. Add(' c: TChar;');
  2722. Add('begin');
  2723. Add(' i:=integer(i);');
  2724. Add(' i:=integer(b);');
  2725. Add(' b:=TYesNo(b);');
  2726. Add(' b:=TYesNo(i);');
  2727. Add(' d:=TFloat(d);');
  2728. Add(' d:=TFloat(i);');
  2729. Add(' s:=TCaption(s);');
  2730. Add(' s:=TCaption(c);');
  2731. Add(' c:=TChar(c);');
  2732. ConvertProgram;
  2733. CheckSource('TestAliasTypeRef',
  2734. LinesToStr([ // statements
  2735. 'this.i = 0;',
  2736. 'this.b = false;',
  2737. 'this.d = 0.0;',
  2738. 'this.s = "";',
  2739. 'this.c = "";',
  2740. '']),
  2741. LinesToStr([ // this.$main
  2742. '$mod.i = $mod.i;',
  2743. '$mod.i = ($mod.b ? 1 : 0);',
  2744. '$mod.b = $mod.b;',
  2745. '$mod.b = $mod.i != 0;',
  2746. '$mod.d = $mod.d;',
  2747. '$mod.d = $mod.i;',
  2748. '$mod.s = $mod.s;',
  2749. '$mod.s = $mod.c;',
  2750. '$mod.c = $mod.c;',
  2751. '']));
  2752. end;
  2753. procedure TTestModule.TestEmptyProc;
  2754. begin
  2755. StartProgram(false);
  2756. Add('procedure Test;');
  2757. Add('begin');
  2758. Add('end;');
  2759. Add('begin');
  2760. ConvertProgram;
  2761. CheckSource('TestEmptyProc',
  2762. LinesToStr([ // statements
  2763. 'this.Test = function () {',
  2764. '};'
  2765. ]),
  2766. LinesToStr([ // this.$main
  2767. ''
  2768. ]));
  2769. end;
  2770. procedure TTestModule.TestProcOneParam;
  2771. begin
  2772. StartProgram(false);
  2773. Add('procedure ProcA(i: longint);');
  2774. Add('begin');
  2775. Add('end;');
  2776. Add('begin');
  2777. Add(' PROCA(3);');
  2778. ConvertProgram;
  2779. CheckSource('TestProcOneParam',
  2780. LinesToStr([ // statements
  2781. 'this.ProcA = function (i) {',
  2782. '};'
  2783. ]),
  2784. LinesToStr([ // this.$main
  2785. '$mod.ProcA(3);'
  2786. ]));
  2787. end;
  2788. procedure TTestModule.TestFunctionWithoutParams;
  2789. begin
  2790. StartProgram(false);
  2791. Add('function FuncA: longint;');
  2792. Add('begin');
  2793. Add('end;');
  2794. Add('var i: longint;');
  2795. Add('begin');
  2796. Add(' I:=FUNCA();');
  2797. Add(' I:=FUNCA;');
  2798. Add(' FUNCA();');
  2799. Add(' FUNCA;');
  2800. ConvertProgram;
  2801. CheckSource('TestProcWithoutParams',
  2802. LinesToStr([ // statements
  2803. 'this.FuncA = function () {',
  2804. ' var Result = 0;',
  2805. ' return Result;',
  2806. '};',
  2807. 'this.i=0;'
  2808. ]),
  2809. LinesToStr([ // this.$main
  2810. '$mod.i=$mod.FuncA();',
  2811. '$mod.i=$mod.FuncA();',
  2812. '$mod.FuncA();',
  2813. '$mod.FuncA();'
  2814. ]));
  2815. end;
  2816. procedure TTestModule.TestProcedureWithoutParams;
  2817. begin
  2818. StartProgram(false);
  2819. Add('procedure ProcA;');
  2820. Add('begin');
  2821. Add('end;');
  2822. Add('begin');
  2823. Add(' PROCA();');
  2824. Add(' PROCA;');
  2825. ConvertProgram;
  2826. CheckSource('TestProcWithoutParams',
  2827. LinesToStr([ // statements
  2828. 'this.ProcA = function () {',
  2829. '};'
  2830. ]),
  2831. LinesToStr([ // this.$main
  2832. '$mod.ProcA();',
  2833. '$mod.ProcA();'
  2834. ]));
  2835. end;
  2836. procedure TTestModule.TestIncDec;
  2837. begin
  2838. StartProgram(false);
  2839. Add([
  2840. 'procedure DoIt(var i: longint);',
  2841. 'begin',
  2842. ' inc(i);',
  2843. ' inc(i,2);',
  2844. 'end;',
  2845. 'var',
  2846. ' Bar: longint;',
  2847. 'begin',
  2848. ' inc(bar);',
  2849. ' inc(bar,2);',
  2850. ' dec(bar);',
  2851. ' dec(bar,3);',
  2852. '']);
  2853. ConvertProgram;
  2854. CheckSource('TestIncDec',
  2855. LinesToStr([ // statements
  2856. 'this.DoIt = function (i) {',
  2857. ' i.set(i.get()+1);',
  2858. ' i.set(i.get()+2);',
  2859. '};',
  2860. 'this.Bar = 0;'
  2861. ]),
  2862. LinesToStr([ // this.$main
  2863. '$mod.Bar+=1;',
  2864. '$mod.Bar+=2;',
  2865. '$mod.Bar-=1;',
  2866. '$mod.Bar-=3;'
  2867. ]));
  2868. end;
  2869. procedure TTestModule.TestLoHiFpcMode;
  2870. begin
  2871. StartProgram(false);
  2872. Add([
  2873. '{$mode objfpc}',
  2874. 'const',
  2875. ' LoByte1 = Lo(Word($1234));',
  2876. ' HiByte1 = Hi(Word($1234));',
  2877. ' LoByte2 = Lo(SmallInt($1234));',
  2878. ' HiByte2 = Hi(SmallInt($1234));',
  2879. ' LoWord1 = Lo($1234CDEF);',
  2880. ' HiWord1 = Hi($1234CDEF);',
  2881. ' LoWord2 = Lo(-$1234CDEF);',
  2882. ' HiWord2 = Hi(-$1234CDEF);',
  2883. ' lo4:byte=lo(byte($34));',
  2884. ' hi4:byte=hi(byte($34));',
  2885. ' lo5:byte=lo(shortint(-$34));',
  2886. ' hi5:byte=hi(shortint(-$34));',
  2887. ' lo6:longword=lo($123456789ABCD);',
  2888. ' hi6:longword=hi($123456789ABCD);',
  2889. ' lo7:longword=lo(-$123456789ABCD);',
  2890. ' hi7:longword=hi(-$123456789ABCD);',
  2891. 'var',
  2892. ' b: Byte;',
  2893. ' ss: shortint;',
  2894. ' w: Word;',
  2895. ' si: SmallInt;',
  2896. ' lw: LongWord;',
  2897. ' li: LongInt;',
  2898. ' b2: Byte;',
  2899. ' ni: nativeint;',
  2900. 'begin',
  2901. ' w := $1234;',
  2902. ' ss := -$12;',
  2903. ' b := lo(ss);',
  2904. ' b := HI(ss);',
  2905. ' b := lo(w);',
  2906. ' b := HI(w);',
  2907. ' b2 := lo(b);',
  2908. ' b2 := hi(b);',
  2909. ' lw := $1234CDEF;',
  2910. ' w := lo(lw);',
  2911. ' w := hi(lw);',
  2912. ' ni := $123456789ABCD;',
  2913. ' lw := lo(ni);',
  2914. ' lw := hi(ni);',
  2915. '']);
  2916. ConvertProgram;
  2917. CheckSource('TestLoHiFpcMode',
  2918. LinesToStr([ // statements
  2919. 'this.LoByte1 = 0x1234 & 0xFF;',
  2920. 'this.HiByte1 = (0x1234 >> 8) & 0xFF;',
  2921. 'this.LoByte2 = 0x1234 & 0xFF;',
  2922. 'this.HiByte2 = (0x1234 >> 8) & 0xFF;',
  2923. 'this.LoWord1 = 0x1234CDEF & 0xFFFF;',
  2924. 'this.HiWord1 = (0x1234CDEF >> 16) & 0xFFFF;',
  2925. 'this.LoWord2 = -0x1234CDEF & 0xFFFF;',
  2926. 'this.HiWord2 = (-0x1234CDEF >> 16) & 0xFFFF;',
  2927. 'this.lo4 = 0x34 & 0xF;',
  2928. 'this.hi4 = (0x34 >> 4) & 0xF;',
  2929. 'this.lo5 = (((-0x34 & 255) << 24) >> 24) & 0xFF;',
  2930. 'this.hi5 = ((((-0x34 & 255) << 24) >> 24) >> 8) & 0xFF;',
  2931. 'this.lo6 = 0x123456789ABCD >>> 0;',
  2932. 'this.hi6 = 74565 >>> 0;',
  2933. 'this.lo7 = -0x123456789ABCD >>> 0;',
  2934. 'this.hi7 = Math.floor(-0x123456789ABCD / 4294967296) >>> 0;',
  2935. 'this.b = 0;',
  2936. 'this.ss = 0;',
  2937. 'this.w = 0;',
  2938. 'this.si = 0;',
  2939. 'this.lw = 0;',
  2940. 'this.li = 0;',
  2941. 'this.b2 = 0;',
  2942. 'this.ni = 0;',
  2943. '']),
  2944. LinesToStr([ // this.$main
  2945. '$mod.w = 0x1234;',
  2946. '$mod.ss = -0x12;',
  2947. '$mod.b = $mod.ss & 0xFF;',
  2948. '$mod.b = ($mod.ss >> 8) & 0xFF;',
  2949. '$mod.b = $mod.w & 0xFF;',
  2950. '$mod.b = ($mod.w >> 8) & 0xFF;',
  2951. '$mod.b2 = $mod.b & 0xF;',
  2952. '$mod.b2 = ($mod.b >> 4) & 0xF;',
  2953. '$mod.lw = 0x1234CDEF;',
  2954. '$mod.w = $mod.lw & 0xFFFF;',
  2955. '$mod.w = ($mod.lw >> 16) & 0xFFFF;',
  2956. '$mod.ni = 0x123456789ABCD;',
  2957. '$mod.lw = $mod.ni >>> 0;',
  2958. '$mod.lw = Math.floor($mod.ni / 4294967296) >>> 0;',
  2959. '']));
  2960. end;
  2961. procedure TTestModule.TestLoHiDelphiMode;
  2962. begin
  2963. StartProgram(false);
  2964. Add([
  2965. '{$mode delphi}',
  2966. 'const',
  2967. ' LoByte1 = Lo(Word($1234));',
  2968. ' HiByte1 = Hi(Word($1234));',
  2969. ' LoByte2 = Lo(SmallInt($1234));',
  2970. ' HiByte2 = Hi(SmallInt($1234));',
  2971. ' LoByte3 = Lo($1234CDEF);',
  2972. ' HiByte3 = Hi($1234CDEF);',
  2973. ' LoByte4 = Lo(-$1234CDEF);',
  2974. ' HiByte4 = Hi(-$1234CDEF);',
  2975. 'var',
  2976. ' b: Byte;',
  2977. ' w: Word;',
  2978. ' si: SmallInt;',
  2979. ' lw: LongWord;',
  2980. ' li: LongInt;',
  2981. 'begin',
  2982. ' w := $1234;',
  2983. ' b := lo(w);',
  2984. ' b := HI(w);',
  2985. ' lw := $1234CDEF;',
  2986. ' b := lo(lw);',
  2987. ' b := hi(lw);',
  2988. '']);
  2989. ConvertProgram;
  2990. CheckSource('TestLoHiDelphiMode',
  2991. LinesToStr([ // statements
  2992. 'this.LoByte1 = 0x1234 & 0xFF;',
  2993. 'this.HiByte1 = (0x1234 >> 8) & 0xFF;',
  2994. 'this.LoByte2 = 0x1234 & 0xFF;',
  2995. 'this.HiByte2 = (0x1234 >> 8) & 0xFF;',
  2996. 'this.LoByte3 = 0x1234CDEF & 0xFF;',
  2997. 'this.HiByte3 = (0x1234CDEF >> 8) & 0xFF;',
  2998. 'this.LoByte4 = -0x1234CDEF & 0xFF;',
  2999. 'this.HiByte4 = (-0x1234CDEF >> 8) & 0xFF;',
  3000. 'this.b = 0;',
  3001. 'this.w = 0;',
  3002. 'this.si = 0;',
  3003. 'this.lw = 0;',
  3004. 'this.li = 0;'
  3005. ]),
  3006. LinesToStr([ // this.$main
  3007. '$mod.w = 0x1234;',
  3008. '$mod.b = $mod.w & 0xFF;',
  3009. '$mod.b = ($mod.w >> 8) & 0xFF;',
  3010. '$mod.lw = 0x1234CDEF;',
  3011. '$mod.b = $mod.lw & 0xFF;',
  3012. '$mod.b = ($mod.lw >> 8) & 0xFF;'
  3013. ]));
  3014. end;
  3015. procedure TTestModule.TestAssignments;
  3016. begin
  3017. StartProgram(false);
  3018. Parser.Options:=Parser.Options+[po_cassignments];
  3019. Add('var');
  3020. Add(' Bar:longint;');
  3021. Add('begin');
  3022. Add(' bar:=3;');
  3023. Add(' bar+=4;');
  3024. Add(' bar-=5;');
  3025. Add(' bar*=6;');
  3026. ConvertProgram;
  3027. CheckSource('TestAssignments',
  3028. LinesToStr([ // statements
  3029. 'this.Bar = 0;'
  3030. ]),
  3031. LinesToStr([ // this.$main
  3032. '$mod.Bar=3;',
  3033. '$mod.Bar+=4;',
  3034. '$mod.Bar-=5;',
  3035. '$mod.Bar*=6;'
  3036. ]));
  3037. end;
  3038. procedure TTestModule.TestArithmeticOperators1;
  3039. begin
  3040. StartProgram(false);
  3041. Add('var');
  3042. Add(' vA,vB,vC:longint;');
  3043. Add('begin');
  3044. Add(' va:=1;');
  3045. Add(' vb:=va+va;');
  3046. Add(' vb:=va div vb;');
  3047. Add(' vb:=va mod vb;');
  3048. Add(' vb:=va+va*vb+va div vb;');
  3049. Add(' vc:=-va;');
  3050. Add(' va:=va-vb;');
  3051. Add(' vb:=va;');
  3052. Add(' if va<vb then vc:=va else vc:=vb;');
  3053. ConvertProgram;
  3054. CheckSource('TestArithmeticOperators1',
  3055. LinesToStr([ // statements
  3056. 'this.vA = 0;',
  3057. 'this.vB = 0;',
  3058. 'this.vC = 0;'
  3059. ]),
  3060. LinesToStr([ // this.$main
  3061. '$mod.vA = 1;',
  3062. '$mod.vB = $mod.vA + $mod.vA;',
  3063. '$mod.vB = rtl.trunc($mod.vA / $mod.vB);',
  3064. '$mod.vB = $mod.vA % $mod.vB;',
  3065. '$mod.vB = $mod.vA + ($mod.vA * $mod.vB) + rtl.trunc($mod.vA / $mod.vB);',
  3066. '$mod.vC = -$mod.vA;',
  3067. '$mod.vA = $mod.vA - $mod.vB;',
  3068. '$mod.vB = $mod.vA;',
  3069. 'if ($mod.vA < $mod.vB){ $mod.vC = $mod.vA } else $mod.vC = $mod.vB;'
  3070. ]));
  3071. end;
  3072. procedure TTestModule.TestMultiAdd;
  3073. begin
  3074. StartProgram(false);
  3075. Add([
  3076. 'function TryEncodeDate(Year, Month, Day: Word): Boolean;',
  3077. 'var Date: double;',
  3078. 'begin',
  3079. ' Result:=(Year>0) and (Year<10000) and',
  3080. ' (Month >= 1) and (Month<=12) and',
  3081. ' (Day>0) and (Day<=31);',
  3082. ' Date := (146097*Year) SHR 2 + (1461*Year) SHR 2 + (153*LongWord(Month)+2) DIV 5 + LongWord(Day);',
  3083. 'end;',
  3084. 'var s: string;',
  3085. 'begin',
  3086. ' s:=''a''+''b''+''c''+''d'';']);
  3087. ConvertProgram;
  3088. CheckSource('TestMultiAdd',
  3089. LinesToStr([ // statements
  3090. 'this.TryEncodeDate = function (Year, Month, Day) {',
  3091. ' var Result = false;',
  3092. ' var date = 0.0;',
  3093. ' Result = (Year > 0) && (Year < 10000) && (Month >= 1) && (Month <= 12) && (Day > 0) && (Day <= 31);',
  3094. ' date = ((146097 * Year) >>> 2) + ((1461 * Year) >>> 2) + rtl.trunc(((153 * Month) + 2) / 5) + Day;',
  3095. ' return Result;',
  3096. '};',
  3097. 'this.s = "";',
  3098. '']),
  3099. LinesToStr([ // this.$main
  3100. '$mod.s = "a" + "b" + "c" + "d";',
  3101. '']));
  3102. end;
  3103. procedure TTestModule.TestLogicalOperators;
  3104. begin
  3105. StartProgram(false);
  3106. Add('var');
  3107. Add(' vA,vB,vC:boolean;');
  3108. Add('begin');
  3109. Add(' va:=vb and vc;');
  3110. Add(' va:=vb or vc;');
  3111. Add(' va:=vb xor vc;');
  3112. Add(' va:=true and vc;');
  3113. Add(' va:=(vb and vc) or (va and vb);');
  3114. Add(' va:=not vb;');
  3115. ConvertProgram;
  3116. CheckSource('TestLogicalOperators',
  3117. LinesToStr([ // statements
  3118. 'this.vA = false;',
  3119. 'this.vB = false;',
  3120. 'this.vC = false;'
  3121. ]),
  3122. LinesToStr([ // this.$main
  3123. '$mod.vA = $mod.vB && $mod.vC;',
  3124. '$mod.vA = $mod.vB || $mod.vC;',
  3125. '$mod.vA = $mod.vB ^ $mod.vC;',
  3126. '$mod.vA = true && $mod.vC;',
  3127. '$mod.vA = ($mod.vB && $mod.vC) || ($mod.vA && $mod.vB);',
  3128. '$mod.vA = !$mod.vB;'
  3129. ]));
  3130. end;
  3131. procedure TTestModule.TestBitwiseOperators;
  3132. begin
  3133. StartProgram(false);
  3134. Add([
  3135. 'var',
  3136. ' vA,vB,vC:longint;',
  3137. ' X,Y,Z: nativeint;',
  3138. 'begin',
  3139. ' va:=vb and vc;',
  3140. ' va:=vb or vc;',
  3141. ' va:=vb xor vc;',
  3142. ' va:=vb shl vc;',
  3143. ' va:=vb shr vc;',
  3144. ' va:=3 and vc;',
  3145. ' va:=(vb and vc) or (va and vb);',
  3146. ' va:=not vb;',
  3147. ' X:=Y and Z;',
  3148. ' X:=Y and va;',
  3149. ' X:=Y or Z;',
  3150. ' X:=Y or va;',
  3151. ' X:=Y xor Z;',
  3152. ' X:=Y xor va;',
  3153. '']);
  3154. ConvertProgram;
  3155. CheckSource('TestBitwiseOperators',
  3156. LinesToStr([ // statements
  3157. 'this.vA = 0;',
  3158. 'this.vB = 0;',
  3159. 'this.vC = 0;',
  3160. 'this.X = 0;',
  3161. 'this.Y = 0;',
  3162. 'this.Z = 0;',
  3163. '']),
  3164. LinesToStr([ // this.$main
  3165. '$mod.vA = $mod.vB & $mod.vC;',
  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 = 3 & $mod.vC;',
  3171. '$mod.vA = ($mod.vB & $mod.vC) | ($mod.vA & $mod.vB);',
  3172. '$mod.vA = ~$mod.vB;',
  3173. '$mod.X = rtl.and($mod.Y, $mod.Z);',
  3174. '$mod.X = $mod.Y & $mod.vA;',
  3175. '$mod.X = rtl.or($mod.Y, $mod.Z);',
  3176. '$mod.X = rtl.or($mod.Y, $mod.vA);',
  3177. '$mod.X = rtl.xor($mod.Y, $mod.Z);',
  3178. '$mod.X = rtl.xor($mod.Y, $mod.vA);',
  3179. '']));
  3180. end;
  3181. procedure TTestModule.TestBitwiseOperatorsLongword;
  3182. begin
  3183. StartProgram(false);
  3184. Add([
  3185. 'var',
  3186. ' a,b,c:longword;',
  3187. ' i: longint;',
  3188. 'begin',
  3189. ' a:=$12345678;',
  3190. ' b:=$EDCBA987;',
  3191. ' c:=not a;',
  3192. ' c:=a and b;',
  3193. ' c:=a and $ffff0000;',
  3194. ' c:=a or b;',
  3195. ' c:=a or $ff00ff00;',
  3196. ' c:=a xor b;',
  3197. ' c:=a xor $f0f0f0f0;',
  3198. ' c:=a shl 1;',
  3199. ' c:=a shl 16;',
  3200. ' c:=a shl 24;',
  3201. ' c:=a shl b;',
  3202. ' c:=a shr 1;',
  3203. ' c:=a shr 16;',
  3204. ' c:=a shr 24;',
  3205. ' c:=a shr b;',
  3206. ' c:=(b and c) or (a and b);',
  3207. ' c:=i and a;',
  3208. ' c:=i or a;',
  3209. ' c:=i xor a;',
  3210. '']);
  3211. ConvertProgram;
  3212. CheckSource('TestBitwiseOperatorsLongword',
  3213. LinesToStr([ // statements
  3214. 'this.a = 0;',
  3215. 'this.b = 0;',
  3216. 'this.c = 0;',
  3217. 'this.i = 0;',
  3218. '']),
  3219. LinesToStr([ // this.$main
  3220. '$mod.a = 0x12345678;',
  3221. '$mod.b = 0xEDCBA987;',
  3222. '$mod.c = rtl.lw(~$mod.a);',
  3223. '$mod.c = rtl.lw($mod.a & $mod.b);',
  3224. '$mod.c = rtl.lw($mod.a & 0xffff0000);',
  3225. '$mod.c = rtl.lw($mod.a | $mod.b);',
  3226. '$mod.c = rtl.lw($mod.a | 0xff00ff00);',
  3227. '$mod.c = rtl.lw($mod.a ^ $mod.b);',
  3228. '$mod.c = rtl.lw($mod.a ^ 0xf0f0f0f0);',
  3229. '$mod.c = rtl.lw($mod.a << 1);',
  3230. '$mod.c = rtl.lw($mod.a << 16);',
  3231. '$mod.c = rtl.lw($mod.a << 24);',
  3232. '$mod.c = rtl.lw($mod.a << $mod.b);',
  3233. '$mod.c = rtl.lw($mod.a >>> 1);',
  3234. '$mod.c = rtl.lw($mod.a >>> 16);',
  3235. '$mod.c = rtl.lw($mod.a >>> 24);',
  3236. '$mod.c = rtl.lw($mod.a >>> $mod.b);',
  3237. '$mod.c = rtl.lw(rtl.lw($mod.b & $mod.c) | rtl.lw($mod.a & $mod.b));',
  3238. '$mod.c = $mod.i & $mod.a;',
  3239. '$mod.c = $mod.i | $mod.a;',
  3240. '$mod.c = $mod.i ^ $mod.a;',
  3241. '']));
  3242. end;
  3243. procedure TTestModule.TestPrgProcVar;
  3244. begin
  3245. StartProgram(false);
  3246. Add('procedure Proc1;');
  3247. Add('type');
  3248. Add(' t1=longint;');
  3249. Add('var');
  3250. Add(' vA:t1;');
  3251. Add('begin');
  3252. Add('end;');
  3253. Add('begin');
  3254. ConvertProgram;
  3255. CheckSource('TestPrgProcVar',
  3256. LinesToStr([ // statements
  3257. 'this.Proc1 = function () {',
  3258. ' var vA=0;',
  3259. '};'
  3260. ]),
  3261. LinesToStr([ // this.$main
  3262. ''
  3263. ]));
  3264. end;
  3265. procedure TTestModule.TestUnitProcVar;
  3266. begin
  3267. StartUnit(false);
  3268. Add('interface');
  3269. Add('');
  3270. Add('type tA=string; // unit scope');
  3271. Add('procedure Proc1;');
  3272. Add('');
  3273. Add('implementation');
  3274. Add('');
  3275. Add('procedure Proc1;');
  3276. Add('type tA=longint; // local proc scope');
  3277. Add('var v1:tA; // using local tA');
  3278. Add('begin');
  3279. Add('end;');
  3280. Add('var v2:tA; // using interface tA');
  3281. ConvertUnit;
  3282. CheckSource('TestUnitProcVar',
  3283. LinesToStr([ // statements
  3284. 'var $impl = $mod.$impl;',
  3285. 'this.Proc1 = function () {',
  3286. ' var v1 = 0;',
  3287. '};',
  3288. '']),
  3289. // this.$init
  3290. '',
  3291. // implementation
  3292. LinesToStr([
  3293. '$impl.v2 = "";',
  3294. '']));
  3295. end;
  3296. procedure TTestModule.TestImplProc;
  3297. begin
  3298. StartUnit(false);
  3299. Add('interface');
  3300. Add('');
  3301. Add('procedure Proc1;');
  3302. Add('');
  3303. Add('implementation');
  3304. Add('');
  3305. Add('procedure Proc1; begin end;');
  3306. Add('procedure Proc2; begin end;');
  3307. Add('initialization');
  3308. Add(' Proc1;');
  3309. Add(' Proc2;');
  3310. ConvertUnit;
  3311. CheckSource('TestImplProc',
  3312. LinesToStr([ // statements
  3313. 'var $impl = $mod.$impl;',
  3314. 'this.Proc1 = function () {',
  3315. '};',
  3316. '']),
  3317. LinesToStr([ // this.$init
  3318. '$mod.Proc1();',
  3319. '$impl.Proc2();',
  3320. '']),
  3321. LinesToStr([ // implementation
  3322. '$impl.Proc2 = function () {',
  3323. '};',
  3324. ''])
  3325. );
  3326. end;
  3327. procedure TTestModule.TestFunctionResult;
  3328. begin
  3329. StartProgram(false);
  3330. Add('function Func1: longint;');
  3331. Add('begin');
  3332. Add(' Result:=3;');
  3333. Add(' Func1:=4;');
  3334. Add('end;');
  3335. Add('begin');
  3336. ConvertProgram;
  3337. CheckSource('TestFunctionResult',
  3338. LinesToStr([ // statements
  3339. 'this.Func1 = function () {',
  3340. ' var Result = 0;',
  3341. ' Result = 3;',
  3342. ' Result = 4;',
  3343. ' return Result;',
  3344. '};'
  3345. ]),
  3346. '');
  3347. end;
  3348. procedure TTestModule.TestNestedProc;
  3349. begin
  3350. StartProgram(false);
  3351. Add([
  3352. 'var vInUnit: longint;',
  3353. 'function DoIt(pA,pD: longint): longint;',
  3354. 'var',
  3355. ' vB: longint;',
  3356. ' vC: longint;',
  3357. ' function Nesty(pA: longint): longint; ',
  3358. ' var vB: longint;',
  3359. ' begin',
  3360. ' Result:=pa+vb+vc+pd+vInUnit;',
  3361. ' nesty:=3;',
  3362. ' doit:=4;',
  3363. ' exit;',
  3364. ' end;',
  3365. 'begin',
  3366. ' Result:=pa+vb+vc;',
  3367. ' doit:=6;',
  3368. ' exit;',
  3369. 'end;',
  3370. 'begin']);
  3371. ConvertProgram;
  3372. CheckSource('TestNestedProc',
  3373. LinesToStr([ // statements
  3374. 'this.vInUnit = 0;',
  3375. 'this.DoIt = function (pA, pD) {',
  3376. ' var Result = 0;',
  3377. ' var vB = 0;',
  3378. ' var vC = 0;',
  3379. ' function Nesty(pA) {',
  3380. ' var Result$1 = 0;',
  3381. ' var vB = 0;',
  3382. ' Result$1 = pA + vB + vC + pD + $mod.vInUnit;',
  3383. ' Result$1 = 3;',
  3384. ' Result = 4;',
  3385. ' return Result$1;',
  3386. ' return Result$1;',
  3387. ' };',
  3388. ' Result = pA + vB + vC;',
  3389. ' Result = 6;',
  3390. ' return Result;',
  3391. ' return Result;',
  3392. '};'
  3393. ]),
  3394. '');
  3395. end;
  3396. procedure TTestModule.TestNestedProc_ResultString;
  3397. begin
  3398. StartProgram(false);
  3399. Add([
  3400. 'function DoIt: string;',
  3401. ' function Nesty: string; ',
  3402. ' begin',
  3403. ' nesty:=#65#66;',
  3404. ' nesty[1]:=#67;',
  3405. ' doit:=#68;',
  3406. ' doit[2]:=#69;',
  3407. ' end;',
  3408. 'begin',
  3409. ' doit:=#70;',
  3410. ' doit[3]:=#71;',
  3411. 'end;',
  3412. 'begin']);
  3413. ConvertProgram;
  3414. CheckSource('TestNestedProc_ResultString',
  3415. LinesToStr([ // statements
  3416. 'this.DoIt = function () {',
  3417. ' var Result = "";',
  3418. ' function Nesty() {',
  3419. ' var Result$1 = "";',
  3420. ' Result$1 = "AB";',
  3421. ' Result$1 = rtl.setCharAt(Result$1, 0, "C");',
  3422. ' Result = "D";',
  3423. ' Result = rtl.setCharAt(Result, 1, "E");',
  3424. ' return Result$1;',
  3425. ' };',
  3426. ' Result = "F";',
  3427. ' Result = rtl.setCharAt(Result, 2, "G");',
  3428. ' return Result;',
  3429. '};'
  3430. ]),
  3431. '');
  3432. end;
  3433. procedure TTestModule.TestForwardProc;
  3434. begin
  3435. StartProgram(false);
  3436. Add('procedure FuncA(Bar: longint); forward;');
  3437. Add('procedure FuncB(Bar: longint);');
  3438. Add('begin');
  3439. Add(' funca(bar);');
  3440. Add('end;');
  3441. Add('procedure funca(bar: longint);');
  3442. Add('begin');
  3443. Add(' if bar=3 then ;');
  3444. Add('end;');
  3445. Add('begin');
  3446. Add(' funca(4);');
  3447. Add(' funcb(5);');
  3448. ConvertProgram;
  3449. CheckSource('TestForwardProc',
  3450. LinesToStr([ // statements'
  3451. 'this.FuncB = function (Bar) {',
  3452. ' $mod.FuncA(Bar);',
  3453. '};',
  3454. 'this.FuncA = function (Bar) {',
  3455. ' if (Bar === 3);',
  3456. '};'
  3457. ]),
  3458. LinesToStr([
  3459. '$mod.FuncA(4);',
  3460. '$mod.FuncB(5);'
  3461. ])
  3462. );
  3463. end;
  3464. procedure TTestModule.TestNestedForwardProc;
  3465. begin
  3466. StartProgram(false);
  3467. Add('procedure FuncA;');
  3468. Add(' procedure FuncB(i: longint); forward;');
  3469. Add(' procedure FuncC(i: longint);');
  3470. Add(' begin');
  3471. Add(' funcb(i);');
  3472. Add(' end;');
  3473. Add(' procedure FuncB(i: longint);');
  3474. Add(' begin');
  3475. Add(' if i=3 then ;');
  3476. Add(' end;');
  3477. Add('begin');
  3478. Add(' funcc(4)');
  3479. Add('end;');
  3480. Add('begin');
  3481. Add(' funca;');
  3482. ConvertProgram;
  3483. CheckSource('TestNestedForwardProc',
  3484. LinesToStr([ // statements'
  3485. 'this.FuncA = function () {',
  3486. ' function FuncC(i) {',
  3487. ' FuncB(i);',
  3488. ' };',
  3489. ' function FuncB(i) {',
  3490. ' if (i === 3);',
  3491. ' };',
  3492. ' FuncC(4);',
  3493. '};'
  3494. ]),
  3495. LinesToStr([
  3496. '$mod.FuncA();'
  3497. ])
  3498. );
  3499. end;
  3500. procedure TTestModule.TestAssignFunctionResult;
  3501. begin
  3502. StartProgram(false);
  3503. Add('function Func1: longint;');
  3504. Add('begin');
  3505. Add('end;');
  3506. Add('var i: longint;');
  3507. Add('begin');
  3508. Add(' i:=func1();');
  3509. Add(' i:=func1()+func1();');
  3510. ConvertProgram;
  3511. CheckSource('TestAssignFunctionResult',
  3512. LinesToStr([ // statements
  3513. 'this.Func1 = function () {',
  3514. ' var Result = 0;',
  3515. ' return Result;',
  3516. '};',
  3517. 'this.i = 0;'
  3518. ]),
  3519. LinesToStr([
  3520. '$mod.i = $mod.Func1();',
  3521. '$mod.i = $mod.Func1() + $mod.Func1();'
  3522. ]));
  3523. end;
  3524. procedure TTestModule.TestFunctionResultInCondition;
  3525. begin
  3526. StartProgram(false);
  3527. Add('function Func1: longint;');
  3528. Add('begin');
  3529. Add('end;');
  3530. Add('function Func2: boolean;');
  3531. Add('begin');
  3532. Add('end;');
  3533. Add('var i: longint;');
  3534. Add('begin');
  3535. Add(' if func2 then ;');
  3536. Add(' if i=func1() then ;');
  3537. Add(' if i=func1 then ;');
  3538. ConvertProgram;
  3539. CheckSource('TestFunctionResultInCondition',
  3540. LinesToStr([ // statements
  3541. 'this.Func1 = function () {',
  3542. ' var Result = 0;',
  3543. ' return Result;',
  3544. '};',
  3545. 'this.Func2 = function () {',
  3546. ' var Result = false;',
  3547. ' return Result;',
  3548. '};',
  3549. 'this.i = 0;'
  3550. ]),
  3551. LinesToStr([
  3552. 'if ($mod.Func2());',
  3553. 'if ($mod.i === $mod.Func1());',
  3554. 'if ($mod.i === $mod.Func1());'
  3555. ]));
  3556. end;
  3557. procedure TTestModule.TestFunctionResultInForLoop;
  3558. begin
  3559. StartProgram(false);
  3560. Add([
  3561. 'function Func1(a: array of longint): longint;',
  3562. 'begin',
  3563. ' for Result:=High(a) downto Low(a) do if a[Result]=0 then exit;',
  3564. ' for Result in a do if a[Result]=0 then exit;',
  3565. 'end;',
  3566. 'begin',
  3567. ' Func1([1,2,3])']);
  3568. ConvertProgram;
  3569. CheckSource('TestFunctionResultInForLoop',
  3570. LinesToStr([ // statements
  3571. 'this.Func1 = function (a) {',
  3572. ' var Result = 0;',
  3573. ' for (var $l = rtl.length(a) - 1; $l >= 0; $l--) {',
  3574. ' Result = $l;',
  3575. ' if (a[Result] === 0) return Result;',
  3576. ' };',
  3577. ' for (var $in = a, $l1 = 0, $end = rtl.length($in) - 1; $l1 <= $end; $l1++) {',
  3578. ' Result = $in[$l1];',
  3579. ' if (a[Result] === 0) return Result;',
  3580. ' };',
  3581. ' return Result;',
  3582. '};',
  3583. '']),
  3584. LinesToStr([
  3585. '$mod.Func1([1, 2, 3]);'
  3586. ]));
  3587. end;
  3588. procedure TTestModule.TestFunctionResultInTypeCast;
  3589. begin
  3590. StartProgram(false);
  3591. Add([
  3592. 'function GetInt: longint;',
  3593. 'begin',
  3594. 'end;',
  3595. 'begin',
  3596. ' if Byte(GetInt)=0 then ;',
  3597. '']);
  3598. ConvertProgram;
  3599. CheckSource('TestFunctionResultInTypeCast',
  3600. LinesToStr([ // statements
  3601. 'this.GetInt = function () {',
  3602. ' var Result = 0;',
  3603. ' return Result;',
  3604. '};',
  3605. '']),
  3606. LinesToStr([
  3607. 'if (($mod.GetInt() & 255) === 0) ;'
  3608. ]));
  3609. end;
  3610. procedure TTestModule.TestExit;
  3611. begin
  3612. StartProgram(false);
  3613. Add('procedure ProcA;');
  3614. Add('begin');
  3615. Add(' exit;');
  3616. Add('end;');
  3617. Add('function FuncB: longint;');
  3618. Add('begin');
  3619. Add(' exit;');
  3620. Add(' exit(3);');
  3621. Add('end;');
  3622. Add('function FuncC: string;');
  3623. Add('begin');
  3624. Add(' exit;');
  3625. Add(' exit(''a'');');
  3626. Add(' exit(''abc'');');
  3627. Add('end;');
  3628. Add('begin');
  3629. Add(' exit;');
  3630. Add(' exit(1);');
  3631. ConvertProgram;
  3632. CheckSource('TestExit',
  3633. LinesToStr([ // statements
  3634. 'this.ProcA = function () {',
  3635. ' return;',
  3636. '};',
  3637. 'this.FuncB = function () {',
  3638. ' var Result = 0;',
  3639. ' return Result;',
  3640. ' return 3;',
  3641. ' return Result;',
  3642. '};',
  3643. 'this.FuncC = function () {',
  3644. ' var Result = "";',
  3645. ' return Result;',
  3646. ' return "a";',
  3647. ' return "abc";',
  3648. ' return Result;',
  3649. '};'
  3650. ]),
  3651. LinesToStr([
  3652. 'return;',
  3653. 'return 1;',
  3654. '']));
  3655. end;
  3656. procedure TTestModule.TestExit_ResultInFinally;
  3657. begin
  3658. StartProgram(false);
  3659. Add([
  3660. 'function Run: word;',
  3661. 'begin',
  3662. ' try',
  3663. ' exit(3);', // no Result in finally -> use return 3
  3664. ' finally',
  3665. ' end;',
  3666. 'end;',
  3667. 'function Fly: word;',
  3668. 'begin',
  3669. ' try',
  3670. ' exit(3);',
  3671. ' finally',
  3672. ' if Result>0 then ;',
  3673. ' end;',
  3674. 'end;',
  3675. 'function Jump: word;',
  3676. 'begin',
  3677. ' try',
  3678. ' try',
  3679. ' exit(4);',
  3680. ' finally',
  3681. ' end;',
  3682. ' finally',
  3683. ' if Result>0 then ;',
  3684. ' end;',
  3685. 'end;',
  3686. 'begin',
  3687. '']);
  3688. ConvertProgram;
  3689. CheckSource('TestExit_ResultInFinally',
  3690. LinesToStr([ // statements
  3691. 'this.Run = function () {',
  3692. ' var Result = 0;',
  3693. ' try {',
  3694. ' return 3;',
  3695. ' } finally {',
  3696. ' };',
  3697. ' return Result;',
  3698. '};',
  3699. 'this.Fly = function () {',
  3700. ' var Result = 0;',
  3701. ' try {',
  3702. ' Result = 3;',
  3703. ' return Result;',
  3704. ' } finally {',
  3705. ' if (Result > 0) ;',
  3706. ' };',
  3707. ' return Result;',
  3708. '};',
  3709. 'this.Jump = function () {',
  3710. ' var Result = 0;',
  3711. ' try {',
  3712. ' try {',
  3713. ' Result = 4;',
  3714. ' return Result;',
  3715. ' } finally {',
  3716. ' };',
  3717. ' } finally {',
  3718. ' if (Result > 0) ;',
  3719. ' };',
  3720. ' return Result;',
  3721. '};',
  3722. '']),
  3723. LinesToStr([
  3724. '']));
  3725. end;
  3726. procedure TTestModule.TestBreak;
  3727. begin
  3728. StartProgram(false);
  3729. Add([
  3730. 'var',
  3731. ' i: longint;',
  3732. 'begin',
  3733. ' repeat',
  3734. ' break;',
  3735. ' until true;',
  3736. ' while true do',
  3737. ' break;',
  3738. ' for i:=1 to 2 do',
  3739. ' break;']);
  3740. ConvertProgram;
  3741. CheckSource('TestBreak',
  3742. LinesToStr([ // statements
  3743. 'this.i = 0;'
  3744. ]),
  3745. LinesToStr([
  3746. 'do {',
  3747. ' break;',
  3748. '} while (!true);',
  3749. 'while (true) break;',
  3750. 'for ($mod.i = 1; $mod.i <= 2; $mod.i++) break;',
  3751. '']));
  3752. end;
  3753. procedure TTestModule.TestBreakAsVar;
  3754. begin
  3755. StartProgram(false);
  3756. Add([
  3757. 'procedure DoIt(break: boolean);',
  3758. 'begin',
  3759. ' if break then ;',
  3760. 'end;',
  3761. 'var',
  3762. ' break: boolean;',
  3763. 'begin',
  3764. ' if break then ;']);
  3765. ConvertProgram;
  3766. CheckSource('TestBreakAsVar',
  3767. LinesToStr([ // statements
  3768. 'this.DoIt = function (Break) {',
  3769. ' if (Break) ;',
  3770. '};',
  3771. 'this.Break = false;',
  3772. '']),
  3773. LinesToStr([
  3774. 'if($mod.Break) ;',
  3775. '']));
  3776. end;
  3777. procedure TTestModule.TestContinue;
  3778. begin
  3779. StartProgram(false);
  3780. Add('var i: longint;');
  3781. Add('begin');
  3782. Add(' repeat');
  3783. Add(' continue;');
  3784. Add(' until true;');
  3785. Add(' while true do');
  3786. Add(' continue;');
  3787. Add(' for i:=1 to 2 do');
  3788. Add(' continue;');
  3789. ConvertProgram;
  3790. CheckSource('TestContinue',
  3791. LinesToStr([ // statements
  3792. 'this.i = 0;'
  3793. ]),
  3794. LinesToStr([
  3795. 'do {',
  3796. ' continue;',
  3797. '} while (!true);',
  3798. 'while (true) continue;',
  3799. 'for ($mod.i = 1; $mod.i <= 2; $mod.i++) continue;',
  3800. '']));
  3801. end;
  3802. procedure TTestModule.TestProc_External;
  3803. begin
  3804. StartProgram(false);
  3805. Add('procedure Foo; external name ''console.log'';');
  3806. Add('function Bar: longint; external name ''get.item'';');
  3807. Add('function Bla(s: string): longint; external name ''apply.something'';');
  3808. Add('var');
  3809. Add(' i: longint;');
  3810. Add('begin');
  3811. Add(' Foo;');
  3812. Add(' i:=Bar;');
  3813. Add(' i:=Bla(''abc'');');
  3814. ConvertProgram;
  3815. CheckSource('TestProc_External',
  3816. LinesToStr([ // statements
  3817. 'this.i = 0;'
  3818. ]),
  3819. LinesToStr([
  3820. 'console.log();',
  3821. '$mod.i = get.item();',
  3822. '$mod.i = apply.something("abc");'
  3823. ]));
  3824. end;
  3825. procedure TTestModule.TestProc_ExternalOtherUnit;
  3826. begin
  3827. AddModuleWithIntfImplSrc('unit2.pas',
  3828. LinesToStr([
  3829. 'procedure Now; external name ''Date.now'';',
  3830. 'procedure DoIt;'
  3831. ]),
  3832. 'procedure doit; begin end;');
  3833. StartUnit(true);
  3834. Add('interface');
  3835. Add('uses unit2;');
  3836. Add('implementation');
  3837. Add('begin');
  3838. Add(' now;');
  3839. Add(' now();');
  3840. Add(' uNit2.now;');
  3841. Add(' uNit2.now();');
  3842. Add(' doit;');
  3843. Add(' uNit2.doit;');
  3844. ConvertUnit;
  3845. CheckSource('TestProc_ExternalOtherUnit',
  3846. LinesToStr([
  3847. '']),
  3848. LinesToStr([
  3849. 'Date.now();',
  3850. 'Date.now();',
  3851. 'Date.now();',
  3852. 'Date.now();',
  3853. 'pas.unit2.DoIt();',
  3854. 'pas.unit2.DoIt();',
  3855. '']));
  3856. end;
  3857. procedure TTestModule.TestProc_Asm;
  3858. begin
  3859. StartProgram(false);
  3860. Add([
  3861. '{$mode delphi}',
  3862. 'function DoIt: longint;',
  3863. 'begin;',
  3864. ' asm',
  3865. ' { a:{ b:{}, c:[]}, d:''1'' };',
  3866. ' end;',
  3867. ' asm console.log(); end;',
  3868. ' asm',
  3869. ' s = "'' ";',
  3870. ' s = ''" '';',
  3871. ' s = s + "world" + "''";',
  3872. ' // end',
  3873. ' s = ''end'';',
  3874. ' s = "end";',
  3875. ' s = "foo\"bar";',
  3876. ' s = ''a\''b'';',
  3877. ' s = `${expr}\`-"-''-`;',
  3878. ' s = `multi',
  3879. 'line`;',
  3880. ' end;',
  3881. 'end;',
  3882. 'procedure Fly;',
  3883. 'asm',
  3884. ' return;',
  3885. 'end;',
  3886. 'begin']);
  3887. ConvertProgram;
  3888. CheckSource('TestProc_Asm',
  3889. LinesToStr([ // statements
  3890. 'this.DoIt = function () {',
  3891. ' var Result = 0;',
  3892. ' { a:{ b:{}, c:[]}, d:''1'' };',
  3893. ' console.log();',
  3894. ' s = "'' ";',
  3895. ' s = ''" '';',
  3896. ' s = s + "world" + "''";',
  3897. ' // end',
  3898. ' s = ''end'';',
  3899. ' s = "end";',
  3900. ' s = "foo\"bar";',
  3901. ' s = ''a\''b'';',
  3902. ' s = `${expr}\`-"-''-`;',
  3903. ' s = `multi',
  3904. 'line`;',
  3905. ' return Result;',
  3906. '};',
  3907. 'this.Fly = function () {',
  3908. ' return;',
  3909. '};',
  3910. '']),
  3911. LinesToStr([
  3912. ''
  3913. ]));
  3914. end;
  3915. procedure TTestModule.TestProc_AsmSubBlock;
  3916. begin
  3917. StartProgram(true,[supTObject]);
  3918. Add([
  3919. '{$mode delphi}',
  3920. 'type',
  3921. ' TBird = class end;',
  3922. 'procedure Run(w: word);',
  3923. 'begin;',
  3924. ' if true then asm console.log(); end;',
  3925. ' if w>3 then asm',
  3926. ' var a = w+1;',
  3927. ' w = a+3;',
  3928. ' end;',
  3929. ' while (w>7) do asm',
  3930. ' w+=3; w*=2;',
  3931. ' end;',
  3932. ' try',
  3933. ' except',
  3934. ' on E: TBird do',
  3935. ' asm console.log(E); end;',
  3936. ' on E: TObject do',
  3937. ' asm var i=3; i--; end;',
  3938. ' else asm Fly; High; end;',
  3939. ' end;',
  3940. 'end;',
  3941. 'begin']);
  3942. ConvertProgram;
  3943. CheckSource('TestProc_AsmSubBlock',
  3944. LinesToStr([ // statements
  3945. 'rtl.createClass(this, "TBird", pas.system.TObject, function () {',
  3946. '});',
  3947. 'this.Run = function (w) {',
  3948. ' if (true) console.log();',
  3949. ' if (w > 3) {',
  3950. ' var a = w+1;',
  3951. ' w = a+3;',
  3952. ' };',
  3953. ' while (w > 7) {',
  3954. ' w+=3; w*=2;',
  3955. ' };',
  3956. ' try {} catch ($e) {',
  3957. ' if ($mod.TBird.isPrototypeOf($e)) {',
  3958. ' var E = $e;',
  3959. ' console.log(E);',
  3960. ' } else if (pas.system.TObject.isPrototypeOf($e)) {',
  3961. ' var E = $e;',
  3962. ' var i=3; i--;',
  3963. ' } else {',
  3964. ' Fly; High;',
  3965. ' }',
  3966. ' };',
  3967. '};',
  3968. '']),
  3969. LinesToStr([
  3970. ''
  3971. ]));
  3972. end;
  3973. procedure TTestModule.TestProc_Assembler;
  3974. begin
  3975. StartProgram(false);
  3976. Add('function DoIt: longint; assembler;');
  3977. Add('asm');
  3978. Add('{ a:{ b:{}, c:[]}, d:''1'' };');
  3979. Add('end;');
  3980. Add('begin');
  3981. ConvertProgram;
  3982. CheckSource('TestProc_Assembler',
  3983. LinesToStr([ // statements
  3984. 'this.DoIt = function () {',
  3985. ' { a:{ b:{}, c:[]}, d:''1'' };',
  3986. '};'
  3987. ]),
  3988. LinesToStr([
  3989. ''
  3990. ]));
  3991. end;
  3992. procedure TTestModule.TestProc_VarParam;
  3993. begin
  3994. StartProgram(false);
  3995. Add('type integer = longint;');
  3996. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  3997. Add('var vJ: integer;');
  3998. Add('begin');
  3999. Add(' vg:=vg+1;');
  4000. Add(' vj:=vh+2;');
  4001. Add(' vi:=vi+3;');
  4002. Add(' doit(vg,vg,vg);');
  4003. Add(' doit(vh,vh,vj);');
  4004. Add(' doit(vi,vi,vi);');
  4005. Add(' doit(vj,vj,vj);');
  4006. Add('end;');
  4007. Add('var i: integer;');
  4008. Add('begin');
  4009. Add(' doit(i,i,i);');
  4010. ConvertProgram;
  4011. CheckSource('TestProc_VarParam',
  4012. LinesToStr([ // statements
  4013. 'this.DoIt = function (vG,vH,vI) {',
  4014. ' var vJ = 0;',
  4015. ' vG = vG + 1;',
  4016. ' vJ = vH + 2;',
  4017. ' vI.set(vI.get()+3);',
  4018. ' $mod.DoIt(vG, vG, {',
  4019. ' get: function () {',
  4020. ' return vG;',
  4021. ' },',
  4022. ' set: function (v) {',
  4023. ' vG = v;',
  4024. ' }',
  4025. ' });',
  4026. ' $mod.DoIt(vH, vH, {',
  4027. ' get: function () {',
  4028. ' return vJ;',
  4029. ' },',
  4030. ' set: function (v) {',
  4031. ' vJ = v;',
  4032. ' }',
  4033. ' });',
  4034. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  4035. ' $mod.DoIt(vJ, vJ, {',
  4036. ' get: function () {',
  4037. ' return vJ;',
  4038. ' },',
  4039. ' set: function (v) {',
  4040. ' vJ = v;',
  4041. ' }',
  4042. ' });',
  4043. '};',
  4044. 'this.i = 0;'
  4045. ]),
  4046. LinesToStr([
  4047. '$mod.DoIt($mod.i,$mod.i,{',
  4048. ' p: $mod,',
  4049. ' get: function () {',
  4050. ' return this.p.i;',
  4051. ' },',
  4052. ' set: function (v) {',
  4053. ' this.p.i = v;',
  4054. ' }',
  4055. '});'
  4056. ]));
  4057. end;
  4058. procedure TTestModule.TestProc_VarParamString;
  4059. begin
  4060. StartProgram(false);
  4061. Add(['type TCaption = string;',
  4062. 'procedure DoIt(vA: TCaption; var vB: TCaption; out vC: TCaption);',
  4063. 'var c: char;',
  4064. 'begin',
  4065. ' va[1]:=c;',
  4066. ' vb[2]:=c;',
  4067. ' vc[3]:=c;',
  4068. 'end;',
  4069. 'begin']);
  4070. ConvertProgram;
  4071. CheckSource('TestProc_VarParamString',
  4072. LinesToStr([ // statements
  4073. 'this.DoIt = function (vA,vB,vC) {',
  4074. ' var c = "";',
  4075. ' vA = rtl.setCharAt(vA, 0, c);',
  4076. ' vB.set(rtl.setCharAt(vB.get(), 1, c));',
  4077. ' vC.set(rtl.setCharAt(vC.get(), 2, c));',
  4078. '};',
  4079. '']),
  4080. LinesToStr([
  4081. ]));
  4082. end;
  4083. procedure TTestModule.TestProc_VarParamV;
  4084. begin
  4085. StartProgram(false);
  4086. Add([
  4087. 'procedure Inc2(var i: longint);',
  4088. 'begin',
  4089. ' i:=i+2;',
  4090. 'end;',
  4091. 'procedure DoIt(v: longint);',
  4092. 'var p: array of longint;',
  4093. 'begin',
  4094. ' Inc2(v);',
  4095. ' Inc2(p[v]);',
  4096. 'end;',
  4097. 'begin']);
  4098. ConvertProgram;
  4099. CheckSource('TestProc_VarParamV',
  4100. LinesToStr([ // statements
  4101. 'this.Inc2 = function (i) {',
  4102. ' i.set(i.get()+2);',
  4103. '};',
  4104. 'this.DoIt = function (v) {',
  4105. ' var p = [];',
  4106. ' $mod.Inc2({get: function () {',
  4107. ' return v;',
  4108. ' }, set: function (w) {',
  4109. ' v = w;',
  4110. ' }});',
  4111. ' $mod.Inc2({',
  4112. ' a: v,',
  4113. ' p: p,',
  4114. ' get: function () {',
  4115. ' return this.p[this.a];',
  4116. ' },',
  4117. ' set: function (v) {',
  4118. ' this.p[this.a] = v;',
  4119. ' }',
  4120. ' });',
  4121. '};',
  4122. '']),
  4123. LinesToStr([
  4124. '']));
  4125. end;
  4126. procedure TTestModule.TestProc_Overload;
  4127. begin
  4128. StartProgram(false);
  4129. Add('procedure DoIt(vI: longint); begin end;');
  4130. Add('procedure DoIt(vI, vJ: longint); begin end;');
  4131. Add('procedure DoIt(vD: double); begin end;');
  4132. Add('begin');
  4133. Add(' DoIt(1);');
  4134. Add(' DoIt(2,3);');
  4135. Add(' DoIt(4.5);');
  4136. ConvertProgram;
  4137. CheckSource('TestProcedureOverload',
  4138. LinesToStr([ // statements
  4139. 'this.DoIt = function (vI) {',
  4140. '};',
  4141. 'this.DoIt$1 = function (vI, vJ) {',
  4142. '};',
  4143. 'this.DoIt$2 = function (vD) {',
  4144. '};',
  4145. '']),
  4146. LinesToStr([
  4147. '$mod.DoIt(1);',
  4148. '$mod.DoIt$1(2, 3);',
  4149. '$mod.DoIt$2(4.5);',
  4150. '']));
  4151. end;
  4152. procedure TTestModule.TestProc_OverloadForward;
  4153. begin
  4154. StartProgram(false);
  4155. Add('procedure DoIt(vI: longint); forward;');
  4156. Add('procedure DoIt(vI, vJ: longint); begin end;');
  4157. Add('procedure doit(vi: longint); begin end;');
  4158. Add('begin');
  4159. Add(' doit(1);');
  4160. Add(' doit(2,3);');
  4161. ConvertProgram;
  4162. CheckSource('TestProcedureOverloadForward',
  4163. LinesToStr([ // statements
  4164. 'this.DoIt$1 = function (vI, vJ) {',
  4165. '};',
  4166. 'this.DoIt = function (vI) {',
  4167. '};',
  4168. '']),
  4169. LinesToStr([
  4170. '$mod.DoIt(1);',
  4171. '$mod.DoIt$1(2, 3);',
  4172. '']));
  4173. end;
  4174. procedure TTestModule.TestProc_OverloadIntfImpl;
  4175. begin
  4176. StartUnit(false);
  4177. Add('interface');
  4178. Add('procedure DoIt(vI: longint);');
  4179. Add('procedure DoIt(vI, vJ: longint);');
  4180. Add('implementation');
  4181. Add('procedure DoIt(vI, vJ, vK, vL, vM: longint); forward;');
  4182. Add('procedure DoIt(vI, vJ, vK: longint); begin end;');
  4183. Add('procedure DoIt(vi: longint); begin end;');
  4184. Add('procedure DoIt(vI, vJ, vK, vL: longint); begin end;');
  4185. Add('procedure DoIt(vi, vj: longint); begin end;');
  4186. Add('procedure DoIt(vi, vj, vk, vl, vm: longint); begin end;');
  4187. Add('begin');
  4188. Add(' doit(1);');
  4189. Add(' doit(2,3);');
  4190. Add(' doit(4,5,6);');
  4191. Add(' doit(7,8,9,10);');
  4192. Add(' doit(11,12,13,14,15);');
  4193. ConvertUnit;
  4194. CheckSource('TestProcedureOverloadUnit',
  4195. LinesToStr([ // statements
  4196. 'var $impl = $mod.$impl;',
  4197. 'this.DoIt = function (vI) {',
  4198. '};',
  4199. 'this.DoIt$1 = function (vI, vJ) {',
  4200. '};',
  4201. '']),
  4202. LinesToStr([ // this.$init
  4203. '$mod.DoIt(1);',
  4204. '$mod.DoIt$1(2, 3);',
  4205. '$impl.DoIt$3(4,5,6);',
  4206. '$impl.DoIt$4(7,8,9,10);',
  4207. '$impl.DoIt$2(11,12,13,14,15);',
  4208. '']),
  4209. LinesToStr([ // implementation
  4210. '$impl.DoIt$3 = function (vI, vJ, vK) {',
  4211. '};',
  4212. '$impl.DoIt$4 = function (vI, vJ, vK, vL) {',
  4213. '};',
  4214. '$impl.DoIt$2 = function (vI, vJ, vK, vL, vM) {',
  4215. '};',
  4216. '']));
  4217. end;
  4218. procedure TTestModule.TestProc_OverloadNested;
  4219. begin
  4220. StartProgram(false);
  4221. Add([
  4222. 'procedure doit(vA: longint);',
  4223. ' procedure DoIt(vA, vB: longint); overload;',
  4224. ' begin',
  4225. ' doit(1);',
  4226. ' doit(1,2);',
  4227. ' end;',
  4228. ' procedure doit(vA, vB, vC: longint);',
  4229. ' begin',
  4230. ' doit(1);',
  4231. ' doit(1,2);',
  4232. ' doit(1,2,3);',
  4233. ' end;',
  4234. 'begin',
  4235. ' doit(1);',
  4236. ' doit(1,2);',
  4237. ' doit(1,2,3);',
  4238. 'end;',
  4239. 'begin // main',
  4240. ' doit(1);']);
  4241. ConvertProgram;
  4242. CheckSource('TestProcedureOverloadNested',
  4243. LinesToStr([ // statements
  4244. 'this.doit = function (vA) {',
  4245. ' function DoIt$1(vA, vB) {',
  4246. ' $mod.doit(1);',
  4247. ' DoIt$1(1, 2);',
  4248. ' };',
  4249. ' function doit$2(vA, vB, vC) {',
  4250. ' $mod.doit(1);',
  4251. ' DoIt$1(1, 2);',
  4252. ' doit$2(1, 2, 3);',
  4253. ' };',
  4254. ' $mod.doit(1);',
  4255. ' DoIt$1(1, 2);',
  4256. ' doit$2(1, 2, 3);',
  4257. '};',
  4258. '']),
  4259. LinesToStr([
  4260. '$mod.doit(1);',
  4261. '']));
  4262. end;
  4263. procedure TTestModule.TestProc_OverloadNestedForward;
  4264. begin
  4265. StartProgram(false);
  4266. Add([
  4267. 'procedure DoIt(vA: longint); overload; forward;',
  4268. 'procedure DoIt(vB, vC: longint); overload;',
  4269. 'begin // 2 param overload',
  4270. ' doit(1);',
  4271. ' doit(1,2);',
  4272. 'end;',
  4273. 'procedure doit(vA: longint);',
  4274. ' procedure DoIt(vA, vB, vC: longint); overload; forward;',
  4275. ' procedure DoIt(vA, vB, vC, vD: longint); overload;',
  4276. ' begin // 4 param overload',
  4277. ' doit(1);',
  4278. ' doit(1,2);',
  4279. ' doit(1,2,3);',
  4280. ' doit(1,2,3,4);',
  4281. ' end;',
  4282. ' procedure doit(vA, vB, vC: longint);',
  4283. ' procedure DoIt(vA, vB, vC, vD, vE: longint); overload; forward;',
  4284. ' procedure DoIt(vA, vB, vC, vD, vE, vF: longint); overload;',
  4285. ' begin // 6 param overload',
  4286. ' doit(1);',
  4287. ' doit(1,2);',
  4288. ' doit(1,2,3);',
  4289. ' doit(1,2,3,4);',
  4290. ' doit(1,2,3,4,5);',
  4291. ' doit(1,2,3,4,5,6);',
  4292. ' end;',
  4293. ' procedure doit(vA, vB, vC, vD, vE: longint);',
  4294. ' begin // 5 param overload',
  4295. ' doit(1);',
  4296. ' doit(1,2);',
  4297. ' doit(1,2,3);',
  4298. ' doit(1,2,3,4);',
  4299. ' doit(1,2,3,4,5);',
  4300. ' doit(1,2,3,4,5,6);',
  4301. ' end;',
  4302. ' begin // 3 param overload',
  4303. ' doit(1);',
  4304. ' doit(1,2);',
  4305. ' doit(1,2,3);',
  4306. ' doit(1,2,3,4);',
  4307. ' doit(1,2,3,4,5);',
  4308. ' doit(1,2,3,4,5,6);',
  4309. ' end;',
  4310. 'begin // 1 param overload',
  4311. ' doit(1);',
  4312. ' doit(1,2);',
  4313. ' doit(1,2,3);',
  4314. ' doit(1,2,3,4);',
  4315. 'end;',
  4316. 'begin // main',
  4317. ' doit(1);',
  4318. ' doit(1,2);']);
  4319. ConvertProgram;
  4320. CheckSource('TestProc_OverloadNestedForward',
  4321. LinesToStr([ // statements
  4322. 'this.DoIt$1 = function (vB, vC) {',
  4323. ' $mod.DoIt(1);',
  4324. ' $mod.DoIt$1(1, 2);',
  4325. '};',
  4326. 'this.DoIt = function (vA) {',
  4327. ' function DoIt$3(vA, vB, vC, vD) {',
  4328. ' $mod.DoIt(1);',
  4329. ' $mod.DoIt$1(1, 2);',
  4330. ' DoIt$2(1, 2, 3);',
  4331. ' DoIt$3(1, 2, 3, 4);',
  4332. ' };',
  4333. ' function DoIt$2(vA, vB, vC) {',
  4334. ' function DoIt$5(vA, vB, vC, vD, vE, vF) {',
  4335. ' $mod.DoIt(1);',
  4336. ' $mod.DoIt$1(1, 2);',
  4337. ' DoIt$2(1, 2, 3);',
  4338. ' DoIt$3(1, 2, 3, 4);',
  4339. ' DoIt$4(1, 2, 3, 4, 5);',
  4340. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  4341. ' };',
  4342. ' function DoIt$4(vA, vB, vC, vD, vE) {',
  4343. ' $mod.DoIt(1);',
  4344. ' $mod.DoIt$1(1, 2);',
  4345. ' DoIt$2(1, 2, 3);',
  4346. ' DoIt$3(1, 2, 3, 4);',
  4347. ' DoIt$4(1, 2, 3, 4, 5);',
  4348. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  4349. ' };',
  4350. ' $mod.DoIt(1);',
  4351. ' $mod.DoIt$1(1, 2);',
  4352. ' DoIt$2(1, 2, 3);',
  4353. ' DoIt$3(1, 2, 3, 4);',
  4354. ' DoIt$4(1, 2, 3, 4, 5);',
  4355. ' DoIt$5(1, 2, 3, 4, 5, 6);',
  4356. ' };',
  4357. ' $mod.DoIt(1);',
  4358. ' $mod.DoIt$1(1, 2);',
  4359. ' DoIt$2(1, 2, 3);',
  4360. ' DoIt$3(1, 2, 3, 4);',
  4361. '};',
  4362. '']),
  4363. LinesToStr([
  4364. '$mod.DoIt(1);',
  4365. '$mod.DoIt$1(1, 2);',
  4366. '']));
  4367. end;
  4368. procedure TTestModule.TestProc_OverloadUnitCycle;
  4369. begin
  4370. AddModuleWithIntfImplSrc('Unit2.pas',
  4371. LinesToStr([
  4372. 'type',
  4373. ' TObject = class',
  4374. ' procedure DoIt(b: boolean); virtual; abstract;',
  4375. ' procedure DoIt(i: longint); virtual; abstract;',
  4376. ' end;',
  4377. '']),
  4378. 'uses test1;');
  4379. StartUnit(true);
  4380. Add([
  4381. 'interface',
  4382. 'uses unit2;',
  4383. 'type',
  4384. ' TEagle = class(TObject)',
  4385. ' procedure DoIt(b: boolean); override;',
  4386. ' procedure DoIt(i: longint); override;',
  4387. ' end;',
  4388. 'implementation',
  4389. 'procedure TEagle.DoIt(b: boolean); begin end;',
  4390. 'procedure TEagle.DoIt(i: longint); begin end;',
  4391. '']);
  4392. ConvertUnit;
  4393. CheckSource('TestProc_OverloadUnitCycle',
  4394. LinesToStr([ // statements
  4395. 'rtl.createClass(this, "TEagle", pas.Unit2.TObject, function () {',
  4396. ' this.DoIt = function (b) {',
  4397. ' };',
  4398. ' this.DoIt$1 = function (i) {',
  4399. ' };',
  4400. '});',
  4401. '']),
  4402. '',
  4403. LinesToStr([
  4404. '']));
  4405. end;
  4406. procedure TTestModule.TestProc_Varargs;
  4407. begin
  4408. StartProgram(false);
  4409. Add([
  4410. 'procedure ProcA(i:longint); varargs; external name ''ProcA'';',
  4411. 'procedure ProcB; varargs; external name ''ProcB'';',
  4412. 'procedure ProcC(i: longint = 17); varargs; external name ''ProcC'';',
  4413. 'function GetIt: longint; begin end;',
  4414. 'begin',
  4415. ' ProcA(1);',
  4416. ' ProcA(1,2);',
  4417. ' ProcA(1,2.0);',
  4418. ' ProcA(1,2,3);',
  4419. ' ProcA(1,''2'');',
  4420. ' ProcA(2,'''');',
  4421. ' ProcA(3,false);',
  4422. ' ProcB;',
  4423. ' ProcB();',
  4424. ' ProcB(4);',
  4425. ' ProcB(''foo'');',
  4426. ' ProcC;',
  4427. ' ProcC();',
  4428. ' ProcC(4);',
  4429. ' ProcC(5,''foo'');',
  4430. ' ProcB(GetIt);',
  4431. ' ProcB(GetIt());',
  4432. ' ProcB(GetIt,GetIt());']);
  4433. ConvertProgram;
  4434. CheckSource('TestProc_Varargs',
  4435. LinesToStr([ // statements
  4436. 'this.GetIt = function () {',
  4437. ' var Result = 0;',
  4438. ' return Result;',
  4439. '};',
  4440. '']),
  4441. LinesToStr([
  4442. 'ProcA(1);',
  4443. 'ProcA(1, 2);',
  4444. 'ProcA(1, 2.0);',
  4445. 'ProcA(1, 2, 3);',
  4446. 'ProcA(1, "2");',
  4447. 'ProcA(2, "");',
  4448. 'ProcA(3, false);',
  4449. 'ProcB();',
  4450. 'ProcB();',
  4451. 'ProcB(4);',
  4452. 'ProcB("foo");',
  4453. 'ProcC(17);',
  4454. 'ProcC(17);',
  4455. 'ProcC(4);',
  4456. 'ProcC(5, "foo");',
  4457. 'ProcB($mod.GetIt());',
  4458. 'ProcB($mod.GetIt());',
  4459. 'ProcB($mod.GetIt(), $mod.GetIt());',
  4460. '']));
  4461. end;
  4462. procedure TTestModule.TestProc_ConstOrder;
  4463. begin
  4464. StartProgram(false);
  4465. Add([
  4466. 'const A = 3;',
  4467. 'const B = A+1;',
  4468. 'procedure DoIt;',
  4469. 'const C = A+1;',
  4470. 'const D = B+1;',
  4471. 'const E = D+C+B+A;',
  4472. 'begin',
  4473. 'end;',
  4474. 'begin'
  4475. ]);
  4476. ConvertProgram;
  4477. CheckSource('TestProc_ConstOrder',
  4478. LinesToStr([ // statements
  4479. 'this.A = 3;',
  4480. 'this.B = 3 + 1;',
  4481. 'var C = 3 + 1;',
  4482. 'var D = 4 + 1;',
  4483. 'var E = 5 + 4 + 4 + 3;',
  4484. 'this.DoIt = function () {',
  4485. '};',
  4486. '']),
  4487. LinesToStr([
  4488. ''
  4489. ]));
  4490. end;
  4491. procedure TTestModule.TestProc_DuplicateConst;
  4492. begin
  4493. StartProgram(false);
  4494. Add([
  4495. 'const A = 1;',
  4496. 'procedure DoIt;',
  4497. 'const A = 2;',
  4498. ' procedure SubIt;',
  4499. ' const A = 21;',
  4500. ' begin',
  4501. ' end;',
  4502. 'begin',
  4503. 'end;',
  4504. 'procedure DoSome;',
  4505. 'const A = 3;',
  4506. 'begin',
  4507. 'end;',
  4508. 'begin'
  4509. ]);
  4510. ConvertProgram;
  4511. CheckSource('TestProc_DuplicateConst',
  4512. LinesToStr([ // statements
  4513. 'this.A = 1;',
  4514. 'var A$1 = 2;',
  4515. 'var A$2 = 21;',
  4516. 'this.DoIt = function () {',
  4517. ' function SubIt() {',
  4518. ' };',
  4519. '};',
  4520. 'var A$3 = 3;',
  4521. 'this.DoSome = function () {',
  4522. '};',
  4523. '']),
  4524. LinesToStr([
  4525. ''
  4526. ]));
  4527. end;
  4528. procedure TTestModule.TestProc_LocalVarAbsolute;
  4529. begin
  4530. StartProgram(false);
  4531. Add([
  4532. 'type',
  4533. ' TObject = class',
  4534. ' Index: longint;',
  4535. ' procedure DoAbs(Item: pointer);',
  4536. ' end;',
  4537. 'procedure TObject.DoAbs(Item: pointer);',
  4538. 'var',
  4539. ' o: TObject absolute Item;',
  4540. 'begin',
  4541. ' if o.Index<o.Index then o.Index:=o.Index;',
  4542. 'end;',
  4543. 'procedure DoIt(i: longint; p: pointer);',
  4544. 'var',
  4545. ' d: double absolute i;',
  4546. ' s: string absolute d;',
  4547. ' oi: TObject absolute i;',
  4548. ' op: TObject absolute p;',
  4549. 'begin',
  4550. ' if d=d then d:=d;',
  4551. ' if s=s then s:=s;',
  4552. ' if oi.Index<oi.Index then oi.Index:=oi.Index;',
  4553. ' if op.Index=op.Index then op.Index:=op.Index;',
  4554. 'end;',
  4555. 'begin']);
  4556. ConvertProgram;
  4557. CheckSource('TestProc_LocalVarAbsolute',
  4558. LinesToStr([ // statements
  4559. 'rtl.createClass(this, "TObject", null, function () {',
  4560. ' this.$init = function () {',
  4561. ' this.Index = 0;',
  4562. ' };',
  4563. ' this.$final = function () {',
  4564. ' };',
  4565. ' this.DoAbs = function (Item) {',
  4566. ' if (Item.Index < Item.Index) Item.Index = Item.Index;',
  4567. ' };',
  4568. '});',
  4569. 'this.DoIt = function (i, p) {',
  4570. ' if (i === i) i = i;',
  4571. ' if (i === i) i = i;',
  4572. ' if (i.Index < i.Index) i.Index = i.Index;',
  4573. ' if (p.Index === p.Index) p.Index = p.Index;',
  4574. '};'
  4575. ]),
  4576. LinesToStr([
  4577. ]));
  4578. end;
  4579. procedure TTestModule.TestProc_LocalVarInit;
  4580. begin
  4581. StartProgram(false);
  4582. Add([
  4583. 'type TBytes = array of byte;',
  4584. 'procedure DoIt;',
  4585. 'const c = 4;',
  4586. 'var',
  4587. ' b: byte = 1;',
  4588. ' w: word = 2+c;',
  4589. ' p: pointer = nil;',
  4590. ' Buffer: TBytes = nil;',
  4591. 'begin',
  4592. 'end;',
  4593. 'begin']);
  4594. ConvertProgram;
  4595. CheckSource('TestProc_LocalVarInit',
  4596. LinesToStr([ // statements
  4597. 'var c = 4;',
  4598. 'this.DoIt = function () {',
  4599. ' var b = 1;',
  4600. ' var w = 2 + 4;',
  4601. ' var p = null;',
  4602. ' var Buffer = [];',
  4603. '};',
  4604. '']),
  4605. LinesToStr([
  4606. ]));
  4607. end;
  4608. procedure TTestModule.TestProc_ReservedWords;
  4609. begin
  4610. StartProgram(false);
  4611. Add([
  4612. 'procedure Date(ArrayBuffer: longint);',
  4613. 'const',
  4614. ' NaN: longint = 3;',
  4615. 'var',
  4616. ' &Boolean: longint;',
  4617. ' procedure Error(ArrayBuffer: longint);',
  4618. ' begin',
  4619. ' end;',
  4620. 'begin',
  4621. ' Nan:=&bOolean;',
  4622. 'end;',
  4623. 'begin',
  4624. ' Date(1);']);
  4625. ConvertProgram;
  4626. CheckSource('TestProc_ReservedWords',
  4627. LinesToStr([ // statements
  4628. 'var naN = 3;',
  4629. 'this.Date = function (arrayBuffer) {',
  4630. ' var boolean = 0;',
  4631. ' function error(arrayBuffer) {',
  4632. ' };',
  4633. ' naN = boolean;',
  4634. '};',
  4635. '']),
  4636. LinesToStr([
  4637. ' $mod.Date(1);'
  4638. ]));
  4639. end;
  4640. procedure TTestModule.TestProc_ConstRefWord;
  4641. begin
  4642. StartProgram(false);
  4643. Add([
  4644. 'procedure Run(constref w: word);',
  4645. 'var l: word;',
  4646. 'begin',
  4647. ' l:=w;',
  4648. ' Run(w);',
  4649. ' Run(l);',
  4650. 'end;',
  4651. 'procedure Fly(a: word; var b: word; out c: word; const d: word; constref e: word);',
  4652. 'begin',
  4653. ' Run(a);',
  4654. ' Run(b);',
  4655. ' Run(c);',
  4656. ' Run(d);',
  4657. ' Run(e);',
  4658. 'end;',
  4659. 'begin',
  4660. ' Run(1);']);
  4661. ConvertProgram;
  4662. CheckHint(mtWarning,nConstRefNotForXAsConst,'ConstRef not yet implemented for Word. Treating as Const');
  4663. CheckSource('TestProc_ConstRefWord',
  4664. LinesToStr([ // statements
  4665. 'this.Run = function (w) {',
  4666. ' var l = 0;',
  4667. ' l = w;',
  4668. ' $mod.Run(w);',
  4669. ' $mod.Run(l);',
  4670. '};',
  4671. 'this.Fly = function (a, b, c, d, e) {',
  4672. ' $mod.Run(a);',
  4673. ' $mod.Run(b.get());',
  4674. ' $mod.Run(c.get());',
  4675. ' $mod.Run(d);',
  4676. ' $mod.Run(e);',
  4677. '};',
  4678. '']),
  4679. LinesToStr([
  4680. '$mod.Run(1);'
  4681. ]));
  4682. end;
  4683. procedure TTestModule.TestAnonymousProc_Assign_ObjFPC;
  4684. begin
  4685. StartProgram(false);
  4686. Add([
  4687. '{$mode objfpc}',
  4688. 'type',
  4689. ' TFunc = reference to function(x: word): word;',
  4690. 'var Func: TFunc;',
  4691. 'procedure DoIt(a: word);',
  4692. 'begin',
  4693. ' Func:=function(b:word): word',
  4694. ' begin',
  4695. ' Result:=a+b;',
  4696. ' exit(b);',
  4697. ' exit(Result);',
  4698. ' end;',// test semicolon
  4699. ' a:=3;',
  4700. 'end;',
  4701. 'begin',
  4702. ' Func:=function(c:word):word begin',
  4703. ' Result:=3+c;',
  4704. ' exit(c);',
  4705. ' exit(Result);',
  4706. ' end;']);
  4707. ConvertProgram;
  4708. CheckSource('TestAnonymousProc_Assign_ObjFPC',
  4709. LinesToStr([ // statements
  4710. 'this.Func = null;',
  4711. 'this.DoIt = function (a) {',
  4712. ' $mod.Func = function (b) {',
  4713. ' var Result = 0;',
  4714. ' Result = a + b;',
  4715. ' return b;',
  4716. ' return Result;',
  4717. ' return Result;',
  4718. ' };',
  4719. ' a = 3;',
  4720. '};',
  4721. '']),
  4722. LinesToStr([
  4723. '$mod.Func = function (c) {',
  4724. ' var Result = 0;',
  4725. ' Result = 3 + c;',
  4726. ' return c;',
  4727. ' return Result;',
  4728. ' return Result;',
  4729. '};',
  4730. '']));
  4731. end;
  4732. procedure TTestModule.TestAnonymousProc_Assign_Delphi;
  4733. begin
  4734. StartProgram(false);
  4735. Add([
  4736. '{$mode delphi}',
  4737. 'type',
  4738. ' TProc = reference to procedure(x: word);',
  4739. 'procedure DoIt(a: word);',
  4740. 'var Proc: TProc;',
  4741. 'begin',
  4742. ' Proc:=procedure(b:word) begin end;',
  4743. 'end;',
  4744. 'var Proc: TProc;',
  4745. 'begin',
  4746. ' Proc:=procedure(c:word) begin end;',
  4747. '']);
  4748. ConvertProgram;
  4749. CheckSource('TestAnonymousProc_Assign_Delphi',
  4750. LinesToStr([ // statements
  4751. 'this.DoIt = function (a) {',
  4752. ' var Proc = null;',
  4753. ' Proc = function (b) {',
  4754. ' };',
  4755. '};',
  4756. 'this.Proc = null;',
  4757. '']),
  4758. LinesToStr([
  4759. '$mod.Proc = function (c) {',
  4760. '};',
  4761. '']));
  4762. end;
  4763. procedure TTestModule.TestAnonymousProc_Arg;
  4764. begin
  4765. StartProgram(false);
  4766. Add([
  4767. 'type',
  4768. ' TProc = reference to procedure;',
  4769. ' TFunc = reference to function(x: word): word;',
  4770. 'procedure DoMore(f,g: TProc);',
  4771. 'begin',
  4772. 'end;',
  4773. 'procedure DoOdd(v: jsvalue);',
  4774. 'begin',
  4775. 'end;',
  4776. 'procedure DoIt(f: TFunc);',
  4777. 'begin',
  4778. ' DoIt(function(b:word): word',
  4779. ' begin',
  4780. ' Result:=1+b;',
  4781. ' end);',
  4782. ' DoMore(procedure begin end, procedure begin end);',
  4783. ' DoOdd(procedure begin end);',
  4784. 'end;',
  4785. 'begin',
  4786. ' DoMore(procedure begin end,',
  4787. ' procedure assembler asm',
  4788. ' console.log("c");',
  4789. ' end);',
  4790. '']);
  4791. ConvertProgram;
  4792. CheckSource('TestAnonymousProc_Arg',
  4793. LinesToStr([ // statements
  4794. 'this.DoMore = function (f, g) {',
  4795. '};',
  4796. 'this.DoOdd = function (v) {',
  4797. '};',
  4798. 'this.DoIt = function (f) {',
  4799. ' $mod.DoIt(function (b) {',
  4800. ' var Result = 0;',
  4801. ' Result = 1 + b;',
  4802. ' return Result;',
  4803. ' });',
  4804. ' $mod.DoMore(function () {',
  4805. ' }, function () {',
  4806. ' });',
  4807. ' $mod.DoOdd(function () {',
  4808. ' });',
  4809. '};',
  4810. '']),
  4811. LinesToStr([
  4812. '$mod.DoMore(function () {',
  4813. '}, function () {',
  4814. ' console.log("c");',
  4815. '});',
  4816. '']));
  4817. end;
  4818. procedure TTestModule.TestAnonymousProc_Typecast;
  4819. begin
  4820. StartProgram(false);
  4821. Add([
  4822. 'type',
  4823. ' TProc = reference to procedure(w: word);',
  4824. ' TArr = array of word;',
  4825. ' TFuncArr = reference to function: TArr;',
  4826. 'procedure DoIt(p: TProc);',
  4827. 'var',
  4828. ' w: word;',
  4829. ' a: TArr;',
  4830. 'begin',
  4831. ' p:=TProc(procedure(b: smallint) begin end);',
  4832. ' a:=TFuncArr(function: TArr begin end)();',
  4833. ' w:=TFuncArr(function: TArr begin end)()[3];',
  4834. 'end;',
  4835. 'begin']);
  4836. ConvertProgram;
  4837. CheckSource('TestAnonymousProc_Typecast',
  4838. LinesToStr([ // statements
  4839. 'this.DoIt = function (p) {',
  4840. ' var w = 0;',
  4841. ' var a = [];',
  4842. ' p = function (b) {',
  4843. ' };',
  4844. ' a = function () {',
  4845. ' var Result = [];',
  4846. ' return Result;',
  4847. ' }();',
  4848. ' w = function () {',
  4849. ' var Result = [];',
  4850. ' return Result;',
  4851. ' }()[3];',
  4852. '};',
  4853. '']),
  4854. LinesToStr([
  4855. '']));
  4856. end;
  4857. procedure TTestModule.TestAnonymousProc_With;
  4858. begin
  4859. StartProgram(false);
  4860. Add([
  4861. 'type',
  4862. ' TProc = reference to procedure(w: word);',
  4863. ' TObject = class',
  4864. ' b: boolean;',
  4865. ' end;',
  4866. 'var',
  4867. ' p: TProc;',
  4868. ' bird: TObject;',
  4869. 'begin',
  4870. ' with bird do',
  4871. ' p:=procedure(w: word)',
  4872. ' begin',
  4873. ' b:=w>2;',
  4874. ' end;',
  4875. '']);
  4876. ConvertProgram;
  4877. CheckSource('TestAnonymousProc_With',
  4878. LinesToStr([ // statements
  4879. 'rtl.createClass(this, "TObject", null, function () {',
  4880. ' this.$init = function () {',
  4881. ' this.b = false;',
  4882. ' };',
  4883. ' this.$final = function () {',
  4884. ' };',
  4885. '});',
  4886. 'this.p = null;',
  4887. 'this.bird = null;',
  4888. '']),
  4889. LinesToStr([
  4890. 'var $with = $mod.bird;',
  4891. '$mod.p = function (w) {',
  4892. ' $with.b = w > 2;',
  4893. '};',
  4894. '']));
  4895. end;
  4896. procedure TTestModule.TestAnonymousProc_ExceptOn;
  4897. begin
  4898. StartProgram(false);
  4899. Add([
  4900. 'type',
  4901. ' TProc = reference to procedure;',
  4902. ' TObject = class',
  4903. ' b: boolean;',
  4904. ' end;',
  4905. 'procedure DoIt;',
  4906. 'var',
  4907. ' p: TProc;',
  4908. 'begin',
  4909. ' try',
  4910. ' except',
  4911. ' on E: TObject do',
  4912. ' p:=procedure',
  4913. ' begin',
  4914. ' E.b:=true;',
  4915. ' end;',
  4916. ' end;',
  4917. 'end;',
  4918. 'begin']);
  4919. ConvertProgram;
  4920. CheckSource('TestAnonymousProc_ExceptOn',
  4921. LinesToStr([ // statements
  4922. 'rtl.createClass(this, "TObject", null, function () {',
  4923. ' this.$init = function () {',
  4924. ' this.b = false;',
  4925. ' };',
  4926. ' this.$final = function () {',
  4927. ' };',
  4928. '});',
  4929. 'this.DoIt = function () {',
  4930. ' var p = null;',
  4931. ' try {} catch ($e) {',
  4932. ' if ($mod.TObject.isPrototypeOf($e)) {',
  4933. ' var E = $e;',
  4934. ' p = function () {',
  4935. ' E.b = true;',
  4936. ' };',
  4937. ' } else throw $e',
  4938. ' };',
  4939. '};',
  4940. '']),
  4941. LinesToStr([
  4942. '']));
  4943. end;
  4944. procedure TTestModule.TestAnonymousProc_Nested;
  4945. begin
  4946. StartProgram(false);
  4947. Add([
  4948. 'type',
  4949. ' TProc = reference to procedure;',
  4950. ' TObject = class',
  4951. ' i: byte;',
  4952. ' procedure DoIt;',
  4953. ' end;',
  4954. 'procedure TObject.DoIt;',
  4955. 'var',
  4956. ' p: TProc;',
  4957. ' procedure Sub;',
  4958. ' begin',
  4959. ' p:=procedure',
  4960. ' begin',
  4961. ' i:=3;',
  4962. ' Self.i:=4;',
  4963. ' p:=procedure',
  4964. ' procedure SubSub;',
  4965. ' begin',
  4966. ' i:=13;',
  4967. ' Self.i:=14;',
  4968. ' end;',
  4969. ' begin',
  4970. ' i:=13;',
  4971. ' Self.i:=14;',
  4972. ' end;',
  4973. ' end;',
  4974. ' end;',
  4975. 'begin',
  4976. 'end;',
  4977. 'begin']);
  4978. ConvertProgram;
  4979. CheckSource('TestAnonymousProc_Nested',
  4980. LinesToStr([ // statements
  4981. 'rtl.createClass(this, "TObject", null, function () {',
  4982. ' this.$init = function () {',
  4983. ' this.i = 0;',
  4984. ' };',
  4985. ' this.$final = function () {',
  4986. ' };',
  4987. ' this.DoIt = function () {',
  4988. ' var $Self = this;',
  4989. ' var p = null;',
  4990. ' function Sub() {',
  4991. ' p = function () {',
  4992. ' $Self.i = 3;',
  4993. ' $Self.i = 4;',
  4994. ' p = function () {',
  4995. ' function SubSub() {',
  4996. ' $Self.i = 13;',
  4997. ' $Self.i = 14;',
  4998. ' };',
  4999. ' $Self.i = 13;',
  5000. ' $Self.i = 14;',
  5001. ' };',
  5002. ' };',
  5003. ' };',
  5004. ' };',
  5005. '});',
  5006. '']),
  5007. LinesToStr([
  5008. '']));
  5009. end;
  5010. procedure TTestModule.TestAnonymousProc_NestedAssignResult;
  5011. begin
  5012. StartProgram(false);
  5013. Add([
  5014. 'type',
  5015. ' TProc = reference to procedure;',
  5016. 'function DoIt: TProc;',
  5017. ' function Sub: TProc;',
  5018. ' begin',
  5019. ' Result:=procedure',
  5020. ' begin',
  5021. ' Sub:=procedure',
  5022. ' procedure SubSub;',
  5023. ' begin',
  5024. ' Result:=nil;',
  5025. ' Sub:=nil;',
  5026. ' DoIt:=nil;',
  5027. ' end;',
  5028. ' begin',
  5029. ' Result:=nil;',
  5030. ' Sub:=nil;',
  5031. ' DoIt:=nil;',
  5032. ' end;',
  5033. ' end;',
  5034. ' end;',
  5035. 'begin',
  5036. 'end;',
  5037. 'begin']);
  5038. ConvertProgram;
  5039. CheckSource('TestAnonymousProc_NestedAssignResult',
  5040. LinesToStr([ // statements
  5041. 'this.DoIt = function () {',
  5042. ' var Result = null;',
  5043. ' function Sub() {',
  5044. ' var Result$1 = null;',
  5045. ' Result$1 = function () {',
  5046. ' Result$1 = function () {',
  5047. ' function SubSub() {',
  5048. ' Result$1 = null;',
  5049. ' Result$1 = null;',
  5050. ' Result = null;',
  5051. ' };',
  5052. ' Result$1 = null;',
  5053. ' Result$1 = null;',
  5054. ' Result = null;',
  5055. ' };',
  5056. ' };',
  5057. ' return Result$1;',
  5058. ' };',
  5059. ' return Result;',
  5060. '};',
  5061. '']),
  5062. LinesToStr([
  5063. '']));
  5064. end;
  5065. procedure TTestModule.TestAnonymousProc_Class;
  5066. begin
  5067. StartProgram(false);
  5068. Add([
  5069. 'type',
  5070. ' TProc = reference to procedure;',
  5071. ' TEvent = procedure of object;',
  5072. ' TObject = class',
  5073. ' Size: word;',
  5074. ' function GetIt: TProc;',
  5075. ' procedure DoIt; virtual; abstract;',
  5076. ' end;',
  5077. 'function TObject.GetIt: TProc;',
  5078. 'begin',
  5079. ' Result:=procedure',
  5080. ' var p: TEvent;',
  5081. ' begin',
  5082. ' Size:=Size;',
  5083. ' Size:=Self.Size;',
  5084. ' p:=@DoIt;',
  5085. ' p:[email protected];',
  5086. ' end;',
  5087. 'end;',
  5088. 'begin']);
  5089. ConvertProgram;
  5090. CheckSource('TestAnonymousProc_Class',
  5091. LinesToStr([ // statements
  5092. 'rtl.createClass(this, "TObject", null, function () {',
  5093. ' this.$init = function () {',
  5094. ' this.Size = 0;',
  5095. ' };',
  5096. ' this.$final = function () {',
  5097. ' };',
  5098. ' this.GetIt = function () {',
  5099. ' var $Self = this;',
  5100. ' var Result = null;',
  5101. ' Result = function () {',
  5102. ' var p = null;',
  5103. ' $Self.Size = $Self.Size;',
  5104. ' $Self.Size = $Self.Size;',
  5105. ' p = rtl.createCallback($Self, "DoIt");',
  5106. ' p = rtl.createCallback($Self, "DoIt");',
  5107. ' };',
  5108. ' return Result;',
  5109. ' };',
  5110. '});',
  5111. '']),
  5112. LinesToStr([
  5113. '']));
  5114. end;
  5115. procedure TTestModule.TestAnonymousProc_ForLoop;
  5116. begin
  5117. StartProgram(false);
  5118. Add([
  5119. 'type TProc = reference to procedure;',
  5120. 'procedure Foo(p: TProc);',
  5121. 'begin',
  5122. 'end;',
  5123. 'procedure DoIt;',
  5124. 'var i: word;',
  5125. ' a: word;',
  5126. 'begin',
  5127. ' for i:=1 to 10 do begin',
  5128. ' Foo(procedure begin a:=3; end);',
  5129. ' end;',
  5130. 'end;',
  5131. 'begin',
  5132. ' DoIt;']);
  5133. ConvertProgram;
  5134. CheckSource('TestAnonymousProc_ForLoop',
  5135. LinesToStr([ // statements
  5136. 'this.Foo = function (p) {',
  5137. '};',
  5138. 'this.DoIt = function () {',
  5139. ' var i = 0;',
  5140. ' var a = 0;',
  5141. ' for (i = 1; i <= 10; i++) {',
  5142. ' $mod.Foo(function () {',
  5143. ' a = 3;',
  5144. ' });',
  5145. ' };',
  5146. '};',
  5147. '']),
  5148. LinesToStr([
  5149. '$mod.DoIt();'
  5150. ]));
  5151. end;
  5152. procedure TTestModule.TestAnonymousProc_AsmDelphi;
  5153. begin
  5154. StartProgram(false);
  5155. Add([
  5156. '{$mode delphi}',
  5157. 'type',
  5158. ' TProc = reference to procedure;',
  5159. ' TFunc = reference to function(x: word): word;',
  5160. 'procedure Run;',
  5161. 'asm',
  5162. 'end;',
  5163. 'procedure Walk(p: TProc; f: TFunc);',
  5164. 'begin',
  5165. ' Walk(procedure asm end, function(b:word): word asm return 1+b; end);',
  5166. 'end;',
  5167. 'begin',
  5168. ' Walk(procedure',
  5169. ' asm',
  5170. ' console.log("a");',
  5171. ' end,',
  5172. ' function(x: word): word asm',
  5173. ' console.log("c");',
  5174. ' end);',
  5175. '']);
  5176. ConvertProgram;
  5177. CheckSource('TestAnonymousProc_AsmDelphi',
  5178. LinesToStr([ // statements
  5179. 'this.Run = function () {',
  5180. '};',
  5181. 'this.Walk = function (p, f) {',
  5182. ' $mod.Walk(function () {',
  5183. ' }, function (b) {',
  5184. ' return 1+b;',
  5185. ' });',
  5186. '};',
  5187. '']),
  5188. LinesToStr([
  5189. '$mod.Walk(function () {',
  5190. ' console.log("a");',
  5191. '}, function (x) {',
  5192. ' console.log("c");',
  5193. '});',
  5194. '']));
  5195. end;
  5196. procedure TTestModule.TestEnum_Name;
  5197. begin
  5198. StartProgram(false);
  5199. Add('type TMyEnum = (Red, Green, Blue);');
  5200. Add('var e: TMyEnum;');
  5201. Add('var f: TMyEnum = Blue;');
  5202. Add('begin');
  5203. Add(' e:=green;');
  5204. Add(' e:=default(TMyEnum);');
  5205. ConvertProgram;
  5206. CheckSource('TestEnum_Name',
  5207. LinesToStr([ // statements
  5208. 'this.TMyEnum = {',
  5209. ' "0":"Red",',
  5210. ' Red:0,',
  5211. ' "1":"Green",',
  5212. ' Green:1,',
  5213. ' "2":"Blue",',
  5214. ' Blue:2',
  5215. ' };',
  5216. 'this.e = 0;',
  5217. 'this.f = this.TMyEnum.Blue;'
  5218. ]),
  5219. LinesToStr([
  5220. '$mod.e=$mod.TMyEnum.Green;',
  5221. '$mod.e=$mod.TMyEnum.Red;'
  5222. ]));
  5223. end;
  5224. procedure TTestModule.TestEnum_Number;
  5225. begin
  5226. Converter.Options:=Converter.Options+[coEnumNumbers];
  5227. StartProgram(false);
  5228. Add('type TMyEnum = (Red, Green);');
  5229. Add('var');
  5230. Add(' e: TMyEnum;');
  5231. Add(' f: TMyEnum = Green;');
  5232. Add(' i: longint;');
  5233. Add('begin');
  5234. Add(' e:=green;');
  5235. Add(' i:=longint(e);');
  5236. ConvertProgram;
  5237. CheckSource('TestEnumNumber',
  5238. LinesToStr([ // statements
  5239. 'this.TMyEnum = {',
  5240. ' "0":"Red",',
  5241. ' Red:0,',
  5242. ' "1":"Green",',
  5243. ' Green:1',
  5244. ' };',
  5245. 'this.e = 0;',
  5246. 'this.f = 1;',
  5247. 'this.i = 0;'
  5248. ]),
  5249. LinesToStr([
  5250. '$mod.e=1;',
  5251. '$mod.i=$mod.e;'
  5252. ]));
  5253. end;
  5254. procedure TTestModule.TestEnum_ConstFail;
  5255. begin
  5256. StartProgram(false);
  5257. Add([
  5258. 'type TMyEnum = (Red = 100, Green = 101);',
  5259. 'var',
  5260. ' e: TMyEnum;',
  5261. ' f: TMyEnum = Green;',
  5262. 'begin',
  5263. ' e:=green;']);
  5264. SetExpectedPasResolverError('not yet implemented: Red:TPasEnumValue [20180126202434] "enum const"',3002);
  5265. ConvertProgram;
  5266. end;
  5267. procedure TTestModule.TestEnum_Functions;
  5268. begin
  5269. StartProgram(false);
  5270. Add([
  5271. 'type TMyEnum = (Red, Green);',
  5272. 'procedure DoIt(var e: TMyEnum; var i: word);',
  5273. 'var',
  5274. ' v: longint;',
  5275. ' s: string;',
  5276. 'begin',
  5277. ' val(s,e,v);',
  5278. ' val(s,e,i);',
  5279. 'end;',
  5280. 'var',
  5281. ' e: TMyEnum;',
  5282. ' i: longint;',
  5283. ' s: string;',
  5284. ' b: boolean;',
  5285. 'begin',
  5286. ' i:=ord(red);',
  5287. ' i:=ord(green);',
  5288. ' i:=ord(e);',
  5289. ' i:=ord(b);',
  5290. ' e:=low(tmyenum);',
  5291. ' e:=low(e);',
  5292. ' b:=low(boolean);',
  5293. ' e:=high(tmyenum);',
  5294. ' e:=high(e);',
  5295. ' b:=high(boolean);',
  5296. ' e:=pred(green);',
  5297. ' e:=pred(e);',
  5298. ' b:=pred(b);',
  5299. ' e:=succ(red);',
  5300. ' e:=succ(e);',
  5301. ' b:=succ(b);',
  5302. ' e:=tmyenum(1);',
  5303. ' e:=tmyenum(i);',
  5304. ' s:=str(e);',
  5305. ' str(e,s);',
  5306. ' str(red,s);',
  5307. ' s:=str(e:3);',
  5308. ' writestr(s,e:3,red);',
  5309. ' val(s,e,i);',
  5310. ' i:=longint(e);']);
  5311. ConvertProgram;
  5312. CheckSource('TestEnum_Functions',
  5313. LinesToStr([ // statements
  5314. 'this.TMyEnum = {',
  5315. ' "0":"Red",',
  5316. ' Red:0,',
  5317. ' "1":"Green",',
  5318. ' Green:1',
  5319. ' };',
  5320. 'this.DoIt = function (e, i) {',
  5321. ' var v = 0;',
  5322. ' var s = "";',
  5323. ' e.set(rtl.valEnum(s, $mod.TMyEnum, function (w) {',
  5324. ' v = w;',
  5325. ' }));',
  5326. ' e.set(rtl.valEnum(s, $mod.TMyEnum, i.set));',
  5327. '};',
  5328. 'this.e = 0;',
  5329. 'this.i = 0;',
  5330. 'this.s = "";',
  5331. 'this.b = false;',
  5332. '']),
  5333. LinesToStr([
  5334. '$mod.i=$mod.TMyEnum.Red;',
  5335. '$mod.i=$mod.TMyEnum.Green;',
  5336. '$mod.i=$mod.e;',
  5337. '$mod.i=$mod.b+0;',
  5338. '$mod.e=$mod.TMyEnum.Red;',
  5339. '$mod.e=$mod.TMyEnum.Red;',
  5340. '$mod.b=false;',
  5341. '$mod.e=$mod.TMyEnum.Green;',
  5342. '$mod.e=$mod.TMyEnum.Green;',
  5343. '$mod.b=true;',
  5344. '$mod.e=$mod.TMyEnum.Green-1;',
  5345. '$mod.e=$mod.e-1;',
  5346. '$mod.b=false;',
  5347. '$mod.e=$mod.TMyEnum.Red+1;',
  5348. '$mod.e=$mod.e+1;',
  5349. '$mod.b=true;',
  5350. '$mod.e=1;',
  5351. '$mod.e=$mod.i;',
  5352. '$mod.s = $mod.TMyEnum[$mod.e];',
  5353. '$mod.s = $mod.TMyEnum[$mod.e];',
  5354. '$mod.s = $mod.TMyEnum[$mod.TMyEnum.Red];',
  5355. '$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3);',
  5356. '$mod.s = rtl.spaceLeft($mod.TMyEnum[$mod.e], 3)+$mod.TMyEnum[$mod.TMyEnum.Red];',
  5357. '$mod.e = rtl.valEnum($mod.s, $mod.TMyEnum, function (v) {',
  5358. ' $mod.i = v;',
  5359. '});',
  5360. '$mod.i=$mod.e;',
  5361. '']));
  5362. end;
  5363. procedure TTestModule.TestEnumRg_Functions;
  5364. begin
  5365. StartProgram(false);
  5366. Add([
  5367. 'type',
  5368. ' TEnum = (Red, Green, Blue);',
  5369. ' TEnumRg = Green..Blue;',
  5370. 'procedure DoIt(var e: TEnumRg; var i: word);',
  5371. 'var',
  5372. ' v: longint;',
  5373. ' s: string;',
  5374. 'begin',
  5375. ' val(s,e,v);',
  5376. ' val(s,e,i);',
  5377. 'end;',
  5378. 'var',
  5379. ' e: TEnumRg;',
  5380. ' i: longint;',
  5381. ' s: string;',
  5382. 'begin',
  5383. ' i:=ord(green);',
  5384. ' i:=ord(e);',
  5385. ' e:=low(tenumrg);',
  5386. ' e:=low(e);',
  5387. ' e:=high(tenumrg);',
  5388. ' e:=high(e);',
  5389. ' e:=pred(blue);',
  5390. ' e:=pred(e);',
  5391. ' e:=succ(green);',
  5392. ' e:=succ(e);',
  5393. ' e:=tenumrg(1);',
  5394. ' e:=tenumrg(i);',
  5395. ' s:=str(e);',
  5396. ' str(e,s);',
  5397. ' str(red,s);',
  5398. ' s:=str(e:3);',
  5399. ' writestr(s,e:3,blue);',
  5400. ' val(s,e,i);',
  5401. ' i:=longint(e);']);
  5402. ConvertProgram;
  5403. CheckSource('TestEnumRg_Functions',
  5404. LinesToStr([ // statements
  5405. 'this.TEnum = {',
  5406. ' "0":"Red",',
  5407. ' Red:0,',
  5408. ' "1":"Green",',
  5409. ' Green:1,',
  5410. ' "2":"Blue",',
  5411. ' Blue:2',
  5412. ' };',
  5413. 'this.DoIt = function (e, i) {',
  5414. ' var v = 0;',
  5415. ' var s = "";',
  5416. ' e.set(rtl.valEnum(s, $mod.TEnum, function (w) {',
  5417. ' v = w;',
  5418. ' }));',
  5419. ' e.set(rtl.valEnum(s, $mod.TEnum, i.set));',
  5420. '};',
  5421. 'this.e = this.TEnum.Green;',
  5422. 'this.i = 0;',
  5423. 'this.s = "";',
  5424. '']),
  5425. LinesToStr([
  5426. '$mod.i=$mod.TEnum.Green;',
  5427. '$mod.i=$mod.e;',
  5428. '$mod.e=$mod.TEnum.Green;',
  5429. '$mod.e=$mod.TEnum.Green;',
  5430. '$mod.e=$mod.TEnum.Blue;',
  5431. '$mod.e=$mod.TEnum.Blue;',
  5432. '$mod.e=$mod.TEnum.Blue-1;',
  5433. '$mod.e=$mod.e-1;',
  5434. '$mod.e=$mod.TEnum.Green+1;',
  5435. '$mod.e=$mod.e+1;',
  5436. '$mod.e=1;',
  5437. '$mod.e=$mod.i;',
  5438. '$mod.s = $mod.TEnum[$mod.e];',
  5439. '$mod.s = $mod.TEnum[$mod.e];',
  5440. '$mod.s = $mod.TEnum[$mod.TEnum.Red];',
  5441. '$mod.s = rtl.spaceLeft($mod.TEnum[$mod.e], 3);',
  5442. '$mod.s = rtl.spaceLeft($mod.TEnum[$mod.e], 3)+$mod.TEnum[$mod.TEnum.Blue];',
  5443. '$mod.e = rtl.valEnum($mod.s, $mod.TEnum, function (v) {',
  5444. ' $mod.i = v;',
  5445. '});',
  5446. '$mod.i=$mod.e;',
  5447. '']));
  5448. end;
  5449. procedure TTestModule.TestEnum_AsParams;
  5450. begin
  5451. StartProgram(false);
  5452. Add('type TEnum = (Red,Blue);');
  5453. Add('procedure DoIt(vG: TEnum; const vH: TEnum; var vI: TEnum);');
  5454. Add('var vJ: TEnum;');
  5455. Add('begin');
  5456. Add(' vg:=vg;');
  5457. Add(' vj:=vh;');
  5458. Add(' vi:=vi;');
  5459. Add(' doit(vg,vg,vg);');
  5460. Add(' doit(vh,vh,vj);');
  5461. Add(' doit(vi,vi,vi);');
  5462. Add(' doit(vj,vj,vj);');
  5463. Add('end;');
  5464. Add('var i: TEnum;');
  5465. Add('begin');
  5466. Add(' doit(i,i,i);');
  5467. ConvertProgram;
  5468. CheckSource('TestEnum_AsParams',
  5469. LinesToStr([ // statements
  5470. 'this.TEnum = {',
  5471. ' "0": "Red",',
  5472. ' Red: 0,',
  5473. ' "1": "Blue",',
  5474. ' Blue: 1',
  5475. '};',
  5476. 'this.DoIt = function (vG,vH,vI) {',
  5477. ' var vJ = 0;',
  5478. ' vG = vG;',
  5479. ' vJ = vH;',
  5480. ' vI.set(vI.get());',
  5481. ' $mod.DoIt(vG, vG, {',
  5482. ' get: function () {',
  5483. ' return vG;',
  5484. ' },',
  5485. ' set: function (v) {',
  5486. ' vG = v;',
  5487. ' }',
  5488. ' });',
  5489. ' $mod.DoIt(vH, vH, {',
  5490. ' get: function () {',
  5491. ' return vJ;',
  5492. ' },',
  5493. ' set: function (v) {',
  5494. ' vJ = v;',
  5495. ' }',
  5496. ' });',
  5497. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  5498. ' $mod.DoIt(vJ, vJ, {',
  5499. ' get: function () {',
  5500. ' return vJ;',
  5501. ' },',
  5502. ' set: function (v) {',
  5503. ' vJ = v;',
  5504. ' }',
  5505. ' });',
  5506. '};',
  5507. 'this.i = 0;'
  5508. ]),
  5509. LinesToStr([
  5510. '$mod.DoIt($mod.i,$mod.i,{',
  5511. ' p: $mod,',
  5512. ' get: function () {',
  5513. ' return this.p.i;',
  5514. ' },',
  5515. ' set: function (v) {',
  5516. ' this.p.i = v;',
  5517. ' }',
  5518. '});'
  5519. ]));
  5520. end;
  5521. procedure TTestModule.TestEnumRange_Array;
  5522. begin
  5523. StartProgram(false);
  5524. Add([
  5525. 'type',
  5526. ' TEnum = (Red, Green, Blue);',
  5527. ' TEnumRg = green..blue;',
  5528. ' TArr = array[TEnumRg] of byte;',
  5529. ' TArr2 = array[green..blue] of byte;',
  5530. 'var',
  5531. ' a: TArr;',
  5532. ' b: TArr = (3,4);',
  5533. ' c: TArr2 = (5,6);',
  5534. 'begin',
  5535. ' a[green] := b[blue];',
  5536. ' c[green] := c[blue];',
  5537. '']);
  5538. ConvertProgram;
  5539. CheckSource('TestEnumRange_Array',
  5540. LinesToStr([ // statements
  5541. 'this.TEnum = {',
  5542. ' "0": "Red",',
  5543. ' Red: 0,',
  5544. ' "1": "Green",',
  5545. ' Green: 1,',
  5546. ' "2": "Blue",',
  5547. ' Blue: 2',
  5548. '};',
  5549. 'this.a = rtl.arraySetLength(null, 0, 2);',
  5550. 'this.b = [3, 4];',
  5551. 'this.c = [5, 6];',
  5552. '']),
  5553. LinesToStr([
  5554. ' $mod.a[$mod.TEnum.Green - 1] = $mod.b[$mod.TEnum.Blue - 1];',
  5555. ' $mod.c[$mod.TEnum.Green - 1] = $mod.c[$mod.TEnum.Blue - 1];',
  5556. '']));
  5557. end;
  5558. procedure TTestModule.TestEnum_ForIn;
  5559. begin
  5560. StartProgram(false);
  5561. Add([
  5562. 'type',
  5563. ' TEnum = (Red, Green, Blue);',
  5564. ' TEnumRg = green..blue;',
  5565. ' TArr = array[TEnum] of byte;',
  5566. ' TArrRg = array[TEnumRg] of byte;',
  5567. 'var',
  5568. ' e: TEnum;',
  5569. ' a1: TArr = (3,4,5);',
  5570. ' a2: TArrRg = (11,12);',
  5571. ' b: byte;',
  5572. 'begin',
  5573. ' for e in TEnum do ;',
  5574. ' for e in TEnumRg do ;',
  5575. ' for e in TArr do ;',
  5576. ' for e in TArrRg do ;',
  5577. ' for b in a1 do ;',
  5578. ' for b in a2 do ;',
  5579. '']);
  5580. ConvertProgram;
  5581. CheckSource('TestEnum_ForIn',
  5582. LinesToStr([ // statements
  5583. 'this.TEnum = {',
  5584. ' "0": "Red",',
  5585. ' Red: 0,',
  5586. ' "1": "Green",',
  5587. ' Green: 1,',
  5588. ' "2": "Blue",',
  5589. ' Blue: 2',
  5590. '};',
  5591. 'this.e = 0;',
  5592. 'this.a1 = [3, 4, 5];',
  5593. 'this.a2 = [11, 12];',
  5594. 'this.b = 0;',
  5595. '']),
  5596. LinesToStr([
  5597. ' for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  5598. ' for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  5599. ' for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  5600. ' for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  5601. ' for (var $in = $mod.a1, $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) $mod.b = $in[$l];',
  5602. ' for (var $in1 = $mod.a2, $l1 = 0, $end1 = rtl.length($in1) - 1; $l1 <= $end1; $l1++) $mod.b = $in1[$l1];',
  5603. '']));
  5604. end;
  5605. procedure TTestModule.TestEnum_ScopedNumber;
  5606. begin
  5607. Converter.Options:=Converter.Options+[coEnumNumbers];
  5608. StartProgram(false);
  5609. Add([
  5610. 'type',
  5611. ' TEnum = (Red, Green);',
  5612. 'var',
  5613. ' e: TEnum;',
  5614. 'begin',
  5615. ' e:=TEnum.Green;',
  5616. '']);
  5617. ConvertProgram;
  5618. CheckSource('TestEnum_ScopedNumber',
  5619. LinesToStr([ // statements
  5620. 'this.TEnum = {',
  5621. ' "0": "Red",',
  5622. ' Red: 0,',
  5623. ' "1": "Green",',
  5624. ' Green: 1',
  5625. '};',
  5626. 'this.e = 0;',
  5627. '']),
  5628. LinesToStr([
  5629. '$mod.e = 1;']));
  5630. end;
  5631. procedure TTestModule.TestEnum_InFunction;
  5632. begin
  5633. StartProgram(false);
  5634. Add([
  5635. 'const TEnum = 3;',
  5636. 'procedure DoIt;',
  5637. 'type',
  5638. ' TEnum = (Red, Green, Blue);',
  5639. ' procedure Sub;',
  5640. ' type',
  5641. ' TEnumSub = (Left, Right);',
  5642. ' var',
  5643. ' es: TEnumSub;',
  5644. ' begin',
  5645. ' es:=Left;',
  5646. ' end;',
  5647. 'var',
  5648. ' e, e2: TEnum;',
  5649. 'begin',
  5650. ' if e in [red,blue] then e2:=e;',
  5651. 'end;',
  5652. 'begin']);
  5653. ConvertProgram;
  5654. CheckSource('TestEnum_InFunction',
  5655. LinesToStr([ // statements
  5656. 'this.TEnum = 3;',
  5657. 'var TEnum$1 = {',
  5658. ' "0":"Red",',
  5659. ' Red:0,',
  5660. ' "1":"Green",',
  5661. ' Green:1,',
  5662. ' "2":"Blue",',
  5663. ' Blue:2',
  5664. ' };',
  5665. 'var TEnumSub = {',
  5666. ' "0": "Left",',
  5667. ' Left: 0,',
  5668. ' "1": "Right",',
  5669. ' Right: 1',
  5670. '};',
  5671. 'this.DoIt = function () {',
  5672. ' function Sub() {',
  5673. ' var es = 0;',
  5674. ' es = TEnumSub.Left;',
  5675. ' };',
  5676. ' var e = 0;',
  5677. ' var e2 = 0;',
  5678. ' if (e in rtl.createSet(TEnum$1.Red, TEnum$1.Blue)) e2 = e;',
  5679. '};',
  5680. '']),
  5681. LinesToStr([
  5682. '']));
  5683. end;
  5684. procedure TTestModule.TestEnum_Name_Anonymous_Unit;
  5685. begin
  5686. StartUnit(true);
  5687. Add([
  5688. 'interface',
  5689. 'var color: (red, green);',
  5690. 'implementation',
  5691. 'initialization',
  5692. ' color:=green;',
  5693. '']);
  5694. ConvertUnit;
  5695. CheckSource('TestEnum_Name_Anonymous_Unit',
  5696. LinesToStr([
  5697. 'this.color$a = {',
  5698. ' "0": "red",',
  5699. ' red: 0,',
  5700. ' "1": "green",',
  5701. ' green: 1',
  5702. '};',
  5703. 'this.color = 0;',
  5704. '']),
  5705. LinesToStr([ // this.$init
  5706. '$mod.color = $mod.color$a.green;',
  5707. '']),
  5708. LinesToStr([ // implementation
  5709. '']) );
  5710. end;
  5711. procedure TTestModule.TestSet_Enum;
  5712. begin
  5713. StartProgram(false);
  5714. Add([
  5715. 'type',
  5716. ' TColor = (Red, Green, Blue);',
  5717. ' TColors = set of TColor;',
  5718. 'var',
  5719. ' c: TColor;',
  5720. ' s: TColors;',
  5721. ' t: TColors = [];',
  5722. ' u: TColors = [Red];',
  5723. 'begin',
  5724. ' s:=[];',
  5725. ' s:=[Green];',
  5726. ' s:=[Green,Blue];',
  5727. ' s:=[Red..Blue];',
  5728. ' s:=[Red,Green..Blue];',
  5729. ' s:=[Red,c];',
  5730. ' s:=t;',
  5731. ' s:=default(TColors);',
  5732. '']);
  5733. ConvertProgram;
  5734. CheckSource('TestSet',
  5735. LinesToStr([ // statements
  5736. 'this.TColor = {',
  5737. ' "0":"Red",',
  5738. ' Red:0,',
  5739. ' "1":"Green",',
  5740. ' Green:1,',
  5741. ' "2":"Blue",',
  5742. ' Blue:2',
  5743. ' };',
  5744. 'this.c = 0;',
  5745. 'this.s = {};',
  5746. 'this.t = {};',
  5747. 'this.u = rtl.createSet(this.TColor.Red);'
  5748. ]),
  5749. LinesToStr([
  5750. '$mod.s={};',
  5751. '$mod.s=rtl.createSet($mod.TColor.Green);',
  5752. '$mod.s=rtl.createSet($mod.TColor.Green,$mod.TColor.Blue);',
  5753. '$mod.s=rtl.createSet(null,$mod.TColor.Red,$mod.TColor.Blue);',
  5754. '$mod.s=rtl.createSet($mod.TColor.Red,null,$mod.TColor.Green,$mod.TColor.Blue);',
  5755. '$mod.s=rtl.createSet($mod.TColor.Red,$mod.c);',
  5756. '$mod.s=rtl.refSet($mod.t);',
  5757. '$mod.s={};',
  5758. '']));
  5759. end;
  5760. procedure TTestModule.TestSet_Operators;
  5761. begin
  5762. StartProgram(false);
  5763. Add('type');
  5764. Add(' TColor = (Red, Green, Blue);');
  5765. Add(' TColors = set of tcolor;');
  5766. Add('var');
  5767. Add(' vC: TColor;');
  5768. Add(' vS: TColors;');
  5769. Add(' vT: TColors;');
  5770. Add(' vU: TColors;');
  5771. Add(' B: boolean;');
  5772. Add('begin');
  5773. Add(' include(vs,green);');
  5774. Add(' exclude(vs,vc);');
  5775. Add(' vs:=vt+vu;');
  5776. Add(' vs:=vt+[red];');
  5777. Add(' vs:=[red]+vt;');
  5778. Add(' vs:=[red]+[green];');
  5779. Add(' vs:=vt-vu;');
  5780. Add(' vs:=vt-[red];');
  5781. Add(' vs:=[red]-vt;');
  5782. Add(' vs:=[red]-[green];');
  5783. Add(' vs:=vt*vu;');
  5784. Add(' vs:=vt*[red];');
  5785. Add(' vs:=[red]*vt;');
  5786. Add(' vs:=[red]*[green];');
  5787. Add(' vs:=vt><vu;');
  5788. Add(' vs:=vt><[red];');
  5789. Add(' vs:=[red]><vt;');
  5790. Add(' vs:=[red]><[green];');
  5791. Add(' b:=vt=vu;');
  5792. Add(' b:=vt=[red];');
  5793. Add(' b:=[red]=vt;');
  5794. Add(' b:=[red]=[green];');
  5795. Add(' b:=vt<>vu;');
  5796. Add(' b:=vt<>[red];');
  5797. Add(' b:=[red]<>vt;');
  5798. Add(' b:=[red]<>[green];');
  5799. Add(' b:=vt<=vu;');
  5800. Add(' b:=vt<=[red];');
  5801. Add(' b:=[red]<=vt;');
  5802. Add(' b:=[red]<=[green];');
  5803. Add(' b:=vt>=vu;');
  5804. Add(' b:=vt>=[red];');
  5805. Add(' b:=[red]>=vt;');
  5806. Add(' b:=[red]>=[green];');
  5807. ConvertProgram;
  5808. CheckSource('TestSet_Operators',
  5809. LinesToStr([ // statements
  5810. 'this.TColor = {',
  5811. ' "0":"Red",',
  5812. ' Red:0,',
  5813. ' "1":"Green",',
  5814. ' Green:1,',
  5815. ' "2":"Blue",',
  5816. ' Blue:2',
  5817. ' };',
  5818. 'this.vC = 0;',
  5819. 'this.vS = {};',
  5820. 'this.vT = {};',
  5821. 'this.vU = {};',
  5822. 'this.B = false;'
  5823. ]),
  5824. LinesToStr([
  5825. '$mod.vS = rtl.includeSet($mod.vS,$mod.TColor.Green);',
  5826. '$mod.vS = rtl.excludeSet($mod.vS,$mod.vC);',
  5827. '$mod.vS = rtl.unionSet($mod.vT, $mod.vU);',
  5828. '$mod.vS = rtl.unionSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5829. '$mod.vS = rtl.unionSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5830. '$mod.vS = rtl.unionSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5831. '$mod.vS = rtl.diffSet($mod.vT, $mod.vU);',
  5832. '$mod.vS = rtl.diffSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5833. '$mod.vS = rtl.diffSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5834. '$mod.vS = rtl.diffSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5835. '$mod.vS = rtl.intersectSet($mod.vT, $mod.vU);',
  5836. '$mod.vS = rtl.intersectSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5837. '$mod.vS = rtl.intersectSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5838. '$mod.vS = rtl.intersectSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5839. '$mod.vS = rtl.symDiffSet($mod.vT, $mod.vU);',
  5840. '$mod.vS = rtl.symDiffSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5841. '$mod.vS = rtl.symDiffSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5842. '$mod.vS = rtl.symDiffSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5843. '$mod.B = rtl.eqSet($mod.vT, $mod.vU);',
  5844. '$mod.B = rtl.eqSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5845. '$mod.B = rtl.eqSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5846. '$mod.B = rtl.eqSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5847. '$mod.B = rtl.neSet($mod.vT, $mod.vU);',
  5848. '$mod.B = rtl.neSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5849. '$mod.B = rtl.neSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5850. '$mod.B = rtl.neSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5851. '$mod.B = rtl.leSet($mod.vT, $mod.vU);',
  5852. '$mod.B = rtl.leSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5853. '$mod.B = rtl.leSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5854. '$mod.B = rtl.leSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5855. '$mod.B = rtl.geSet($mod.vT, $mod.vU);',
  5856. '$mod.B = rtl.geSet($mod.vT, rtl.createSet($mod.TColor.Red));',
  5857. '$mod.B = rtl.geSet(rtl.createSet($mod.TColor.Red), $mod.vT);',
  5858. '$mod.B = rtl.geSet(rtl.createSet($mod.TColor.Red), rtl.createSet($mod.TColor.Green));',
  5859. '']));
  5860. end;
  5861. procedure TTestModule.TestSet_Operator_In;
  5862. begin
  5863. StartProgram(false);
  5864. Add([
  5865. 'type',
  5866. ' TColor = (Red, Green, Blue);',
  5867. ' TColors = set of tcolor;',
  5868. ' TColorRg = green..blue;',
  5869. 'var',
  5870. ' vC: tcolor;',
  5871. ' vT: tcolors;',
  5872. ' B: boolean;',
  5873. ' rg: TColorRg;',
  5874. 'begin',
  5875. ' b:=red in vt;',
  5876. ' b:=vc in vt;',
  5877. ' b:=green in [red..blue];',
  5878. ' b:=vc in [red..blue];',
  5879. ' ',
  5880. ' if red in vt then ;',
  5881. ' while vC in vt do ;',
  5882. ' repeat',
  5883. ' until vC in vt;',
  5884. ' if rg in [green..blue] then ;',
  5885. '']);
  5886. ConvertProgram;
  5887. CheckSource('TestSet_Operator_In',
  5888. LinesToStr([ // statements
  5889. 'this.TColor = {',
  5890. ' "0":"Red",',
  5891. ' Red:0,',
  5892. ' "1":"Green",',
  5893. ' Green:1,',
  5894. ' "2":"Blue",',
  5895. ' Blue:2',
  5896. ' };',
  5897. 'this.vC = 0;',
  5898. 'this.vT = {};',
  5899. 'this.B = false;',
  5900. 'this.rg = this.TColor.Green;',
  5901. '']),
  5902. LinesToStr([
  5903. '$mod.B = $mod.TColor.Red in $mod.vT;',
  5904. '$mod.B = $mod.vC in $mod.vT;',
  5905. '$mod.B = $mod.TColor.Green in rtl.createSet(null, $mod.TColor.Red, $mod.TColor.Blue);',
  5906. '$mod.B = $mod.vC in rtl.createSet(null, $mod.TColor.Red, $mod.TColor.Blue);',
  5907. 'if ($mod.TColor.Red in $mod.vT) ;',
  5908. 'while ($mod.vC in $mod.vT) {',
  5909. '};',
  5910. 'do {',
  5911. '} while (!($mod.vC in $mod.vT));',
  5912. 'if ($mod.rg in rtl.createSet(null, $mod.TColor.Green, $mod.TColor.Blue)) ;',
  5913. '']));
  5914. end;
  5915. procedure TTestModule.TestSet_Functions;
  5916. begin
  5917. StartProgram(false);
  5918. Add('type');
  5919. Add(' TMyEnum = (Red, Green);');
  5920. Add(' TMyEnums = set of TMyEnum;');
  5921. Add('var');
  5922. Add(' e: TMyEnum;');
  5923. Add(' s: TMyEnums;');
  5924. Add('begin');
  5925. Add(' e:=Low(TMyEnums);');
  5926. Add(' e:=Low(s);');
  5927. Add(' e:=High(TMyEnums);');
  5928. Add(' e:=High(s);');
  5929. ConvertProgram;
  5930. CheckSource('TestSetFunctions',
  5931. LinesToStr([ // statements
  5932. 'this.TMyEnum = {',
  5933. ' "0":"Red",',
  5934. ' Red:0,',
  5935. ' "1":"Green",',
  5936. ' Green:1',
  5937. ' };',
  5938. 'this.e = 0;',
  5939. 'this.s = {};'
  5940. ]),
  5941. LinesToStr([
  5942. '$mod.e=$mod.TMyEnum.Red;',
  5943. '$mod.e=$mod.TMyEnum.Red;',
  5944. '$mod.e=$mod.TMyEnum.Green;',
  5945. '$mod.e=$mod.TMyEnum.Green;',
  5946. '']));
  5947. end;
  5948. procedure TTestModule.TestSet_PassAsArgClone;
  5949. begin
  5950. StartProgram(false);
  5951. Add('type');
  5952. Add(' TMyEnum = (Red, Green);');
  5953. Add(' TMyEnums = set of TMyEnum;');
  5954. Add('procedure DoDefault(s: tmyenums); begin end;');
  5955. Add('procedure DoConst(const s: tmyenums); begin end;');
  5956. Add('var');
  5957. Add(' aSet: tmyenums;');
  5958. Add('begin');
  5959. Add(' dodefault(aset);');
  5960. Add(' doconst(aset);');
  5961. ConvertProgram;
  5962. CheckSource('TestSetFunctions',
  5963. LinesToStr([ // statements
  5964. 'this.TMyEnum = {',
  5965. ' "0":"Red",',
  5966. ' Red:0,',
  5967. ' "1":"Green",',
  5968. ' Green:1',
  5969. ' };',
  5970. 'this.DoDefault = function (s) {',
  5971. '};',
  5972. 'this.DoConst = function (s) {',
  5973. '};',
  5974. 'this.aSet = {};'
  5975. ]),
  5976. LinesToStr([
  5977. '$mod.DoDefault(rtl.refSet($mod.aSet));',
  5978. '$mod.DoConst($mod.aSet);',
  5979. '']));
  5980. end;
  5981. procedure TTestModule.TestSet_AsParams;
  5982. begin
  5983. StartProgram(false);
  5984. Add([
  5985. 'type TEnum = (Red,Blue);',
  5986. 'type TEnums = set of TEnum;',
  5987. 'function DoIt(vG: TEnums; const vH: TEnums; var vI: TEnums): TEnums;',
  5988. 'var vJ: TEnums;',
  5989. 'begin',
  5990. ' Include(vg,red);',
  5991. ' Include(result,blue);',
  5992. ' vg:=vg;',
  5993. ' vj:=vh;',
  5994. ' vi:=vi;',
  5995. ' doit(vg,vg,vg);',
  5996. ' doit(vh,vh,vj);',
  5997. ' doit(vi,vi,vi);',
  5998. ' doit(vj,vj,vj);',
  5999. 'end;',
  6000. 'var i: TEnums;',
  6001. 'begin',
  6002. ' doit(i,i,i);']);
  6003. ConvertProgram;
  6004. CheckSource('TestSet_AsParams',
  6005. LinesToStr([ // statements
  6006. 'this.TEnum = {',
  6007. ' "0": "Red",',
  6008. ' Red: 0,',
  6009. ' "1": "Blue",',
  6010. ' Blue: 1',
  6011. '};',
  6012. 'this.DoIt = function (vG,vH,vI) {',
  6013. ' var Result = {};',
  6014. ' var vJ = {};',
  6015. ' vG = rtl.includeSet(vG, $mod.TEnum.Red);',
  6016. ' Result = rtl.includeSet(Result, $mod.TEnum.Blue);',
  6017. ' vG = rtl.refSet(vG);',
  6018. ' vJ = rtl.refSet(vH);',
  6019. ' vI.set(rtl.refSet(vI.get()));',
  6020. ' $mod.DoIt(rtl.refSet(vG), vG, {',
  6021. ' get: function () {',
  6022. ' return vG;',
  6023. ' },',
  6024. ' set: function (v) {',
  6025. ' vG = v;',
  6026. ' }',
  6027. ' });',
  6028. ' $mod.DoIt(rtl.refSet(vH), vH, {',
  6029. ' get: function () {',
  6030. ' return vJ;',
  6031. ' },',
  6032. ' set: function (v) {',
  6033. ' vJ = v;',
  6034. ' }',
  6035. ' });',
  6036. ' $mod.DoIt(rtl.refSet(vI.get()), vI.get(), vI);',
  6037. ' $mod.DoIt(rtl.refSet(vJ), vJ, {',
  6038. ' get: function () {',
  6039. ' return vJ;',
  6040. ' },',
  6041. ' set: function (v) {',
  6042. ' vJ = v;',
  6043. ' }',
  6044. ' });',
  6045. ' return Result;',
  6046. '};',
  6047. 'this.i = {};'
  6048. ]),
  6049. LinesToStr([
  6050. '$mod.DoIt(rtl.refSet($mod.i),$mod.i,{',
  6051. ' p: $mod,',
  6052. ' get: function () {',
  6053. ' return this.p.i;',
  6054. ' },',
  6055. ' set: function (v) {',
  6056. ' this.p.i = v;',
  6057. ' }',
  6058. '});'
  6059. ]));
  6060. end;
  6061. procedure TTestModule.TestSet_Property;
  6062. begin
  6063. StartProgram(false);
  6064. Add('type');
  6065. Add(' TEnum = (Red,Blue);');
  6066. Add(' TEnums = set of TEnum;');
  6067. Add(' TObject = class');
  6068. Add(' function GetColors: TEnums; external name ''GetColors'';');
  6069. Add(' procedure SetColors(const Value: TEnums); external name ''SetColors'';');
  6070. Add(' property Colors: TEnums read GetColors write SetColors;');
  6071. Add(' end;');
  6072. Add('procedure DoIt(i: TEnums; const j: TEnums; var k: TEnums; out l: TEnums);');
  6073. Add('begin end;');
  6074. Add('var Obj: TObject;');
  6075. Add('begin');
  6076. Add(' Include(Obj.Colors,Red);');
  6077. Add(' Exclude(Obj.Colors,Red);');
  6078. //Add(' DoIt(Obj.Colors,Obj.Colors,Obj.Colors,Obj.Colors);');
  6079. ConvertProgram;
  6080. CheckSource('TestSet_Property',
  6081. LinesToStr([ // statements
  6082. 'this.TEnum = {',
  6083. ' "0": "Red",',
  6084. ' Red: 0,',
  6085. ' "1": "Blue",',
  6086. ' Blue: 1',
  6087. '};',
  6088. 'rtl.createClass(this, "TObject", null, function () {',
  6089. ' this.$init = function () {',
  6090. ' };',
  6091. ' this.$final = function () {',
  6092. ' };',
  6093. '});',
  6094. 'this.DoIt = function (i, j, k, l) {',
  6095. '};',
  6096. 'this.Obj = null;',
  6097. '']),
  6098. LinesToStr([
  6099. '$mod.Obj.SetColors(rtl.includeSet($mod.Obj.GetColors(), $mod.TEnum.Red));',
  6100. '$mod.Obj.SetColors(rtl.excludeSet($mod.Obj.GetColors(), $mod.TEnum.Red));',
  6101. '']));
  6102. end;
  6103. procedure TTestModule.TestSet_EnumConst;
  6104. begin
  6105. StartProgram(false);
  6106. Add([
  6107. 'type',
  6108. ' TEnum = (Red,Blue);',
  6109. ' TEnums = set of TEnum;',
  6110. 'const',
  6111. ' Orange = red;',
  6112. 'var',
  6113. ' Enum: tenum;',
  6114. ' Enums: tenums;',
  6115. 'begin',
  6116. ' Include(enums,orange);',
  6117. ' Exclude(enums,orange);',
  6118. ' if orange in enums then;',
  6119. ' if orange in [orange,red] then;']);
  6120. ConvertProgram;
  6121. CheckSource('TestSet_EnumConst',
  6122. LinesToStr([ // statements
  6123. 'this.TEnum = {',
  6124. ' "0": "Red",',
  6125. ' Red: 0,',
  6126. ' "1": "Blue",',
  6127. ' Blue: 1',
  6128. '};',
  6129. 'this.Orange = this.TEnum.Red;',
  6130. 'this.Enum = 0;',
  6131. 'this.Enums = {};',
  6132. '']),
  6133. LinesToStr([
  6134. '$mod.Enums = rtl.includeSet($mod.Enums, $mod.TEnum.Red);',
  6135. '$mod.Enums = rtl.excludeSet($mod.Enums, $mod.TEnum.Red);',
  6136. 'if ($mod.TEnum.Red in $mod.Enums) ;',
  6137. 'if ($mod.TEnum.Red in rtl.createSet($mod.TEnum.Red, $mod.TEnum.Red)) ;',
  6138. '']));
  6139. end;
  6140. procedure TTestModule.TestSet_IntConst;
  6141. begin
  6142. StartProgram(false);
  6143. Add([
  6144. 'type',
  6145. ' TEnums = set of Byte;',
  6146. 'const',
  6147. ' Orange = 0;',
  6148. 'var',
  6149. ' Enum: byte;',
  6150. ' Enums: tenums;',
  6151. 'begin',
  6152. ' Enums:=[];',
  6153. ' Enums:=[0];',
  6154. ' Enums:=[1..2];',
  6155. //' Include(enums,orange);',
  6156. //' Exclude(enums,orange);',
  6157. ' if orange in enums then;',
  6158. ' if orange in [orange,1] then;']);
  6159. ConvertProgram;
  6160. CheckSource('TestSet_IntConst',
  6161. LinesToStr([ // statements
  6162. 'this.Orange = 0;',
  6163. 'this.Enum = 0;',
  6164. 'this.Enums = {};',
  6165. '']),
  6166. LinesToStr([
  6167. '$mod.Enums = {};',
  6168. '$mod.Enums = rtl.createSet(0);',
  6169. '$mod.Enums = rtl.createSet(null, 1, 2);',
  6170. 'if (0 in $mod.Enums) ;',
  6171. 'if (0 in rtl.createSet(0, 1)) ;',
  6172. '']));
  6173. end;
  6174. procedure TTestModule.TestSet_IntRange;
  6175. begin
  6176. StartProgram(false);
  6177. Add([
  6178. 'type',
  6179. ' TRange = 1..3;',
  6180. ' TEnums = set of TRange;',
  6181. 'const',
  6182. ' Orange = 2;',
  6183. 'var',
  6184. ' Enum: byte;',
  6185. ' Enums: TEnums;',
  6186. 'begin',
  6187. ' Enums:=[];',
  6188. ' Enums:=[1];',
  6189. ' Enums:=[2..3];',
  6190. ' Include(enums,orange);',
  6191. ' Exclude(enums,orange);',
  6192. ' if orange in enums then;',
  6193. ' if orange in [orange,1] then;']);
  6194. ConvertProgram;
  6195. CheckSource('TestSet_IntRange',
  6196. LinesToStr([ // statements
  6197. 'this.Orange = 2;',
  6198. 'this.Enum = 0;',
  6199. 'this.Enums = {};',
  6200. '']),
  6201. LinesToStr([
  6202. '$mod.Enums = {};',
  6203. '$mod.Enums = rtl.createSet(1);',
  6204. '$mod.Enums = rtl.createSet(null, 2, 3);',
  6205. '$mod.Enums = rtl.includeSet($mod.Enums, 2);',
  6206. '$mod.Enums = rtl.excludeSet($mod.Enums, 2);',
  6207. 'if (2 in $mod.Enums) ;',
  6208. 'if (2 in rtl.createSet(2, 1)) ;',
  6209. '']));
  6210. end;
  6211. procedure TTestModule.TestSet_AnonymousEnumType;
  6212. begin
  6213. StartProgram(false);
  6214. Add('type');
  6215. Add(' TFlags = set of (red, green);');
  6216. Add('const');
  6217. Add(' favorite = red;');
  6218. Add('var');
  6219. Add(' f: TFlags;');
  6220. Add(' i: longint;');
  6221. Add('begin');
  6222. Add(' Include(f,red);');
  6223. Add(' Include(f,favorite);');
  6224. Add(' i:=ord(red);');
  6225. Add(' i:=ord(favorite);');
  6226. Add(' i:=ord(low(TFlags));');
  6227. Add(' i:=ord(low(f));');
  6228. Add(' i:=ord(low(favorite));');
  6229. Add(' i:=ord(high(TFlags));');
  6230. Add(' i:=ord(high(f));');
  6231. Add(' i:=ord(high(favorite));');
  6232. Add(' f:=[green,favorite];');
  6233. ConvertProgram;
  6234. CheckSource('TestSet_AnonymousEnumType',
  6235. LinesToStr([ // statements
  6236. 'this.TFlags$a = {',
  6237. ' "0": "red",',
  6238. ' red: 0,',
  6239. ' "1": "green",',
  6240. ' green: 1',
  6241. '};',
  6242. 'this.favorite = this.TFlags$a.red;',
  6243. 'this.f = {};',
  6244. 'this.i = 0;',
  6245. '']),
  6246. LinesToStr([
  6247. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  6248. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  6249. '$mod.i = $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.green;',
  6255. '$mod.i = $mod.TFlags$a.green;',
  6256. '$mod.i = $mod.TFlags$a.green;',
  6257. '$mod.f = rtl.createSet($mod.TFlags$a.green, $mod.TFlags$a.red);',
  6258. '']));
  6259. end;
  6260. procedure TTestModule.TestSet_AnonymousEnumTypeChar;
  6261. begin
  6262. exit;
  6263. StartProgram(false);
  6264. Add([
  6265. 'type',
  6266. ' TAtoZ = ''A''..''Z'';',
  6267. ' TSetOfAZ = set of TAtoZ;',
  6268. 'var',
  6269. ' c: char;',
  6270. ' a: TAtoZ;',
  6271. ' s: TSetOfAZ = [''P'',''A''];',
  6272. ' i: longint;',
  6273. 'begin',
  6274. ' Include(s,''S'');',
  6275. ' Include(s,c);',
  6276. ' Include(s,a);',
  6277. ' c:=low(TAtoZ);',
  6278. ' i:=ord(low(TAtoZ));',
  6279. ' a:=high(TAtoZ);',
  6280. ' a:=high(TSetOfAtoZ);',
  6281. ' s:=[a,c,''M''];',
  6282. '']);
  6283. ConvertProgram;
  6284. CheckSource('TestSet_AnonymousEnumTypeChar',
  6285. LinesToStr([ // statements
  6286. '']),
  6287. LinesToStr([
  6288. '']));
  6289. end;
  6290. procedure TTestModule.TestSet_ConstEnum;
  6291. begin
  6292. StartProgram(false);
  6293. Add([
  6294. 'type',
  6295. ' TEnum = (red,blue,green);',
  6296. ' TEnums = set of TEnum;',
  6297. 'const',
  6298. ' teAny = [low(TEnum)..high(TEnum)];',
  6299. ' teRedBlue = [low(TEnum)..pred(high(TEnum))];',
  6300. 'var',
  6301. ' e: TEnum;',
  6302. ' s: TEnums;',
  6303. 'begin',
  6304. ' if blue in teAny then;',
  6305. ' if blue in teAny+[e] then;',
  6306. ' if blue in teAny+teRedBlue then;',
  6307. ' if e in [red,blue] then;',
  6308. ' s:=teAny;',
  6309. ' s:=teAny+[e];',
  6310. ' s:=[e]+teAny;',
  6311. ' s:=teAny+teRedBlue;',
  6312. ' s:=teAny+teRedBlue+[e];',
  6313. '']);
  6314. ConvertProgram;
  6315. CheckSource('TestSet_ConstEnum',
  6316. LinesToStr([ // statements
  6317. 'this.TEnum = {',
  6318. ' "0": "red",',
  6319. ' red: 0,',
  6320. ' "1": "blue",',
  6321. ' blue: 1,',
  6322. ' "2": "green",',
  6323. ' green: 2',
  6324. '};',
  6325. 'this.teAny = rtl.createSet(null, this.TEnum.red, this.TEnum.green);',
  6326. 'this.teRedBlue = rtl.createSet(null, this.TEnum.red, this.TEnum.green - 1);',
  6327. 'this.e = 0;',
  6328. 'this.s = {};',
  6329. '']),
  6330. LinesToStr([
  6331. 'if ($mod.TEnum.blue in $mod.teAny) ;',
  6332. 'if ($mod.TEnum.blue in rtl.unionSet($mod.teAny, rtl.createSet($mod.e))) ;',
  6333. 'if ($mod.TEnum.blue in rtl.unionSet($mod.teAny, $mod.teRedBlue)) ;',
  6334. 'if ($mod.e in rtl.createSet($mod.TEnum.red, $mod.TEnum.blue)) ;',
  6335. '$mod.s = rtl.refSet($mod.teAny);',
  6336. '$mod.s = rtl.unionSet($mod.teAny, rtl.createSet($mod.e));',
  6337. '$mod.s = rtl.unionSet(rtl.createSet($mod.e), $mod.teAny);',
  6338. '$mod.s = rtl.unionSet($mod.teAny, $mod.teRedBlue);',
  6339. '$mod.s = rtl.unionSet(rtl.unionSet($mod.teAny, $mod.teRedBlue), rtl.createSet($mod.e));',
  6340. '']));
  6341. end;
  6342. procedure TTestModule.TestSet_ConstChar;
  6343. begin
  6344. StartProgram(false);
  6345. Add([
  6346. 'const',
  6347. ' LowChars = [''a''..''z''];',
  6348. ' Chars = LowChars+[''A''..''Z''];',
  6349. ' sc = [''А'', ''Я''];',
  6350. 'var',
  6351. ' c: char;',
  6352. ' s: string;',
  6353. 'begin',
  6354. ' if c in lowchars then ;',
  6355. ' if ''a'' in lowchars then ;',
  6356. ' if s[1] in lowchars then ;',
  6357. ' if c in chars then ;',
  6358. ' if c in [''a''..''z'',''_''] then ;',
  6359. ' if ''b'' in [''a''..''z'',''_''] then ;',
  6360. ' if ''Я'' in sc then ;',
  6361. ' if 3=ord('' '') then ;',
  6362. '']);
  6363. ConvertProgram;
  6364. CheckSource('TestSet_ConstChar',
  6365. LinesToStr([ // statements
  6366. 'this.LowChars = rtl.createSet(null, 97, 122);',
  6367. 'this.Chars = rtl.unionSet(this.LowChars, rtl.createSet(null, 65, 90));',
  6368. 'this.sc = rtl.createSet(1040, 1071);',
  6369. 'this.c = "";',
  6370. 'this.s = "";',
  6371. '']),
  6372. LinesToStr([
  6373. 'if ($mod.c.charCodeAt() in $mod.LowChars) ;',
  6374. 'if (97 in $mod.LowChars) ;',
  6375. 'if ($mod.s.charCodeAt(0) in $mod.LowChars) ;',
  6376. 'if ($mod.c.charCodeAt() in $mod.Chars) ;',
  6377. 'if ($mod.c.charCodeAt() in rtl.createSet(null, 97, 122, 95)) ;',
  6378. 'if (98 in rtl.createSet(null, 97, 122, 95)) ;',
  6379. 'if (1071 in $mod.sc) ;',
  6380. 'if (3 === 32) ;',
  6381. '']));
  6382. end;
  6383. procedure TTestModule.TestSet_ConstInt;
  6384. begin
  6385. StartProgram(false);
  6386. Add([
  6387. 'const',
  6388. ' Months = [1..12];',
  6389. ' Mirror = [-12..-1]+Months;',
  6390. 'var',
  6391. ' i: smallint;',
  6392. 'begin',
  6393. ' if 3 in Months then;',
  6394. ' if i in Months+[i] then;',
  6395. ' if i in Months+Mirror then;',
  6396. ' if i in [4..6,8] then;',
  6397. '']);
  6398. ConvertProgram;
  6399. CheckSource('TestSet_ConstInt',
  6400. LinesToStr([ // statements
  6401. 'this.Months = rtl.createSet(null, 1, 12);',
  6402. 'this.Mirror = rtl.unionSet(rtl.createSet(null, -12, -1), this.Months);',
  6403. 'this.i = 0;',
  6404. '']),
  6405. LinesToStr([
  6406. 'if (3 in $mod.Months) ;',
  6407. 'if ($mod.i in rtl.unionSet($mod.Months, rtl.createSet($mod.i))) ;',
  6408. 'if ($mod.i in rtl.unionSet($mod.Months, $mod.Mirror)) ;',
  6409. 'if ($mod.i in rtl.createSet(null, 4, 6, 8)) ;',
  6410. '']));
  6411. end;
  6412. procedure TTestModule.TestSet_InFunction;
  6413. begin
  6414. StartProgram(false);
  6415. Add([
  6416. 'const',
  6417. ' TEnum = 3;',
  6418. ' TSetOfEnum = 4;',
  6419. ' TSetOfAno = 5;',
  6420. 'procedure DoIt;',
  6421. 'type',
  6422. ' TEnum = (red, blue);',
  6423. ' TSetOfEnum = set of TEnum;',
  6424. ' TSetOfAno = set of (up,down);',
  6425. 'var',
  6426. ' e: TEnum;',
  6427. ' se: TSetOfEnum;',
  6428. ' sa: TSetOfAno;',
  6429. 'begin',
  6430. ' se:=[e];',
  6431. ' sa:=[up];',
  6432. 'end;',
  6433. 'begin',
  6434. '']);
  6435. ConvertProgram;
  6436. CheckSource('TestSet_InFunction',
  6437. LinesToStr([ // statements
  6438. 'this.TEnum = 3;',
  6439. 'this.TSetOfEnum = 4;',
  6440. 'this.TSetOfAno = 5;',
  6441. 'var TEnum$1 = {',
  6442. ' "0": "red",',
  6443. ' red: 0,',
  6444. ' "1": "blue",',
  6445. ' blue: 1',
  6446. '};',
  6447. 'var TSetOfAno$a = {',
  6448. ' "0": "up",',
  6449. ' up: 0,',
  6450. ' "1": "down",',
  6451. ' down: 1',
  6452. '};',
  6453. 'this.DoIt = function () {',
  6454. ' var e = 0;',
  6455. ' var se = {};',
  6456. ' var sa = {};',
  6457. ' se = rtl.createSet(e);',
  6458. ' sa = rtl.createSet(TSetOfAno$a.up);',
  6459. '};',
  6460. '']),
  6461. LinesToStr([
  6462. '']));
  6463. end;
  6464. procedure TTestModule.TestSet_ForIn;
  6465. begin
  6466. StartProgram(false);
  6467. Add([
  6468. 'type',
  6469. ' TEnum = (Red, Green, Blue);',
  6470. ' TEnumRg = green..blue;',
  6471. ' TSetOfEnum = set of TEnum;',
  6472. ' TSetOfEnumRg = set of TEnumRg;',
  6473. 'var',
  6474. ' e, e2: TEnum;',
  6475. ' er: TEnum;',
  6476. ' s: TSetOfEnum;',
  6477. 'begin',
  6478. ' for e in TSetOfEnum do ;',
  6479. ' for e in TSetOfEnumRg do ;',
  6480. ' for e in [] do e2:=e;',
  6481. ' for e in [red..green] do e2:=e;',
  6482. ' for e in [green,blue] do e2:=e;',
  6483. ' for e in [red,blue] do e2:=e;',
  6484. ' for e in s do e2:=e;',
  6485. ' for er in TSetOfEnumRg do ;',
  6486. '']);
  6487. ConvertProgram;
  6488. CheckSource('TestSet_ForIn',
  6489. LinesToStr([ // statements
  6490. 'this.TEnum = {',
  6491. ' "0":"Red",',
  6492. ' Red:0,',
  6493. ' "1":"Green",',
  6494. ' Green:1,',
  6495. ' "2":"Blue",',
  6496. ' Blue:2',
  6497. ' };',
  6498. 'this.e = 0;',
  6499. 'this.e2 = 0;',
  6500. 'this.er = 0;',
  6501. 'this.s = {};',
  6502. '']),
  6503. LinesToStr([
  6504. 'for ($mod.e = 0; $mod.e <= 2; $mod.e++) ;',
  6505. 'for ($mod.e = 1; $mod.e <= 2; $mod.e++) ;',
  6506. 'for ($mod.e = 0; $mod.e <= 1; $mod.e++) $mod.e2 = $mod.e;',
  6507. 'for ($mod.e = 1; $mod.e <= 2; $mod.e++) $mod.e2 = $mod.e;',
  6508. 'for ($mod.e in rtl.createSet($mod.TEnum.Red, $mod.TEnum.Blue)) $mod.e2 = $mod.e;',
  6509. 'for (var $l in $mod.s){',
  6510. ' $mod.e = +$l;',
  6511. ' $mod.e2 = $mod.e;',
  6512. '};',
  6513. 'for ($mod.er = 1; $mod.er <= 2; $mod.er++) ;',
  6514. '']));
  6515. end;
  6516. procedure TTestModule.TestNestBegin;
  6517. begin
  6518. StartProgram(false);
  6519. Add('begin');
  6520. Add(' begin');
  6521. Add(' begin');
  6522. Add(' end;');
  6523. Add(' begin');
  6524. Add(' if true then ;');
  6525. Add(' end;');
  6526. Add(' end;');
  6527. ConvertProgram;
  6528. CheckSource('TestNestBegin',
  6529. '',
  6530. 'if (true) ;');
  6531. end;
  6532. procedure TTestModule.TestUnitImplVars;
  6533. begin
  6534. StartUnit(false);
  6535. Add('interface');
  6536. Add('implementation');
  6537. Add('var');
  6538. Add(' V1:longint;');
  6539. Add(' V2:longint = 3;');
  6540. Add(' V3:string = ''abc'';');
  6541. ConvertUnit;
  6542. CheckSource('TestUnitImplVars',
  6543. LinesToStr([ // statements
  6544. 'var $impl = $mod.$impl;',
  6545. '']),
  6546. '', // this.$init
  6547. LinesToStr([ // implementation
  6548. '$impl.V1 = 0;',
  6549. '$impl.V2 = 3;',
  6550. '$impl.V3 = "abc";',
  6551. '']) );
  6552. end;
  6553. procedure TTestModule.TestUnitImplConsts;
  6554. begin
  6555. StartUnit(false);
  6556. Add('interface');
  6557. Add('implementation');
  6558. Add('const');
  6559. Add(' v1 = 3;');
  6560. Add(' v2:longint = 4;');
  6561. Add(' v3:string = ''abc'';');
  6562. ConvertUnit;
  6563. CheckSource('TestUnitImplConsts',
  6564. LinesToStr([ // statements
  6565. 'var $impl = $mod.$impl;',
  6566. '']),
  6567. '', // this.$init
  6568. LinesToStr([ // implementation
  6569. '$impl.v1 = 3;',
  6570. '$impl.v2 = 4;',
  6571. '$impl.v3 = "abc";',
  6572. '']) );
  6573. end;
  6574. procedure TTestModule.TestUnitImplRecord;
  6575. begin
  6576. StartUnit(false);
  6577. Add('interface');
  6578. Add('implementation');
  6579. Add('type');
  6580. Add(' TMyRecord = record');
  6581. Add(' i: longint;');
  6582. Add(' end;');
  6583. Add('var aRec: TMyRecord;');
  6584. Add('initialization');
  6585. Add(' arec.i:=3;');
  6586. ConvertUnit;
  6587. CheckSource('TestUnitImplRecord',
  6588. LinesToStr([ // statements
  6589. 'var $impl = $mod.$impl;',
  6590. '']),
  6591. // this.$init
  6592. '$impl.aRec.i = 3;',
  6593. LinesToStr([ // implementation
  6594. 'rtl.recNewT($impl, "TMyRecord", function () {',
  6595. ' this.i = 0;',
  6596. ' this.$eq = function (b) {',
  6597. ' return this.i === b.i;',
  6598. ' };',
  6599. ' this.$assign = function (s) {',
  6600. ' this.i = s.i;',
  6601. ' return this;',
  6602. ' };',
  6603. '});',
  6604. '$impl.aRec = $impl.TMyRecord.$new();',
  6605. '']) );
  6606. end;
  6607. procedure TTestModule.TestRenameJSNameConflict;
  6608. begin
  6609. StartProgram(false);
  6610. Add('var apply: longint;');
  6611. Add('var bind: longint;');
  6612. Add('var call: longint;');
  6613. Add('begin');
  6614. ConvertProgram;
  6615. CheckSource('TestRenameJSNameConflict',
  6616. LinesToStr([ // statements
  6617. 'this.Apply = 0;',
  6618. 'this.Bind = 0;',
  6619. 'this.Call = 0;'
  6620. ]),
  6621. LinesToStr([ // this.$main
  6622. ''
  6623. ]));
  6624. end;
  6625. procedure TTestModule.TestLocalConst;
  6626. begin
  6627. StartProgram(false);
  6628. Add('procedure DoIt;');
  6629. Add('const');
  6630. Add(' cA: longint = 1;');
  6631. Add(' cB = 2;');
  6632. Add(' procedure Sub;');
  6633. Add(' const');
  6634. Add(' csA = 3;');
  6635. Add(' cB: double = 4;');
  6636. Add(' begin');
  6637. Add(' cb:=cb+csa;');
  6638. Add(' ca:=ca+csa+5;');
  6639. Add(' end;');
  6640. Add('begin');
  6641. Add(' ca:=ca+cb+6;');
  6642. Add('end;');
  6643. Add('begin');
  6644. ConvertProgram;
  6645. CheckSource('TestLocalConst',
  6646. LinesToStr([
  6647. 'var cA = 1;',
  6648. 'var cB = 2;',
  6649. 'var csA = 3;',
  6650. 'var cB$1 = 4;',
  6651. 'this.DoIt = function () {',
  6652. ' function Sub() {',
  6653. ' cB$1 = cB$1 + 3;',
  6654. ' cA = cA + 3 + 5;',
  6655. ' };',
  6656. ' cA = cA + 2 + 6;',
  6657. '};'
  6658. ]),
  6659. LinesToStr([
  6660. ]));
  6661. end;
  6662. procedure TTestModule.TestVarExternal;
  6663. begin
  6664. StartProgram(false);
  6665. Add('var');
  6666. Add(' NaN: double; external name ''Global.NaN'';');
  6667. Add(' d: double;');
  6668. Add('begin');
  6669. Add(' d:=NaN;');
  6670. ConvertProgram;
  6671. CheckSource('TestVarExternal',
  6672. LinesToStr([
  6673. 'this.d = 0.0;'
  6674. ]),
  6675. LinesToStr([
  6676. '$mod.d = Global.NaN;'
  6677. ]));
  6678. end;
  6679. procedure TTestModule.TestVarExternalOtherUnit;
  6680. begin
  6681. AddModuleWithIntfImplSrc('unit2.pas',
  6682. LinesToStr([
  6683. 'var NaN: double; external name ''Global.NaN'';',
  6684. 'var iV: longint;'
  6685. ]),
  6686. '');
  6687. StartUnit(true);
  6688. Add('interface');
  6689. Add('uses unit2;');
  6690. Add('implementation');
  6691. Add('var');
  6692. Add(' d: double;');
  6693. Add(' i: longint; external name ''$i'';');
  6694. Add('begin');
  6695. Add(' d:=nan;');
  6696. Add(' d:=uNit2.nan;');
  6697. Add(' d:=test1.d;');
  6698. Add(' i:=iv;');
  6699. Add(' i:=uNit2.iv;');
  6700. Add(' i:=test1.i;');
  6701. ConvertUnit;
  6702. CheckSource('TestVarExternalOtherUnit',
  6703. LinesToStr([
  6704. 'var $impl = $mod.$impl;',
  6705. '']),
  6706. LinesToStr([ // this.$init
  6707. '$impl.d = Global.NaN;',
  6708. '$impl.d = Global.NaN;',
  6709. '$impl.d = $impl.d;',
  6710. '$i = pas.unit2.iV;',
  6711. '$i = pas.unit2.iV;',
  6712. '$i = $i;',
  6713. '']),
  6714. LinesToStr([ // implementation
  6715. '$impl.d = 0.0;',
  6716. '']) );
  6717. end;
  6718. procedure TTestModule.TestVarAbsoluteFail;
  6719. begin
  6720. StartProgram(false);
  6721. Add([
  6722. 'var',
  6723. ' a: longint;',
  6724. ' b: longword absolute a;',
  6725. 'begin']);
  6726. SetExpectedPasResolverError('Invalid variable modifier "absolute"',nInvalidVariableModifier);
  6727. ConvertProgram;
  6728. end;
  6729. procedure TTestModule.TestConstExternal;
  6730. begin
  6731. StartProgram(false);
  6732. Add([
  6733. 'const',
  6734. ' PI: double; external name ''Global.PI'';',
  6735. ' Tau = 2*pi;',
  6736. 'var d: double;',
  6737. 'begin',
  6738. ' d:=pi;',
  6739. ' d:=tau+pi;']);
  6740. ConvertProgram;
  6741. CheckSource('TestConstExternal',
  6742. LinesToStr([
  6743. 'this.Tau = 2*Global.PI;',
  6744. 'this.d = 0.0;'
  6745. ]),
  6746. LinesToStr([
  6747. '$mod.d = Global.PI;',
  6748. '$mod.d = $mod.Tau + Global.PI;'
  6749. ]));
  6750. end;
  6751. procedure TTestModule.TestDouble;
  6752. begin
  6753. StartProgram(false);
  6754. Add([
  6755. 'type',
  6756. ' TDateTime = double;',
  6757. 'const',
  6758. ' a = TDateTime(2.7);',
  6759. ' b = a + TDateTime(1.7);',
  6760. ' c = 0.9 + 0.1;',
  6761. ' f0_1 = 0.1;',
  6762. ' f0_3 = 0.3;',
  6763. ' fn0_1 = -0.1;',
  6764. ' fn0_3 = -0.3;',
  6765. ' fn0_003 = -0.003;',
  6766. ' fn0_123456789 = -0.123456789;',
  6767. ' fn300_0 = -300.0;',
  6768. ' fn123456_0 = -123456.0;',
  6769. ' fn1234567_8 = -1234567.8;',
  6770. ' fn12345678_9 = -12345678.9;',
  6771. ' f1_0En12 = 1E-12;',
  6772. ' fn1_0En12 = -1E-12;',
  6773. ' maxdouble = 1.7e+308;',
  6774. ' mindouble = -1.7e+308;',
  6775. ' MinSafeIntDouble = -$1fffffffffffff;',
  6776. ' MinSafeIntDouble2 = -$20000000000000-1;',
  6777. ' MaxSafeIntDouble = $1fffffffffffff;',
  6778. ' DZeroResolution = 1E-12;',
  6779. ' Minus1 = -1E-12;',
  6780. ' EPS = 1E-9;',
  6781. ' DELTA = 0.001;',
  6782. ' Big = 129.789E+100;',
  6783. ' Test0_15 = 0.15;',
  6784. ' Test999 = 2.9999999999999;',
  6785. ' Test111999 = 211199999999999000.0;',
  6786. ' TestMinus111999 = -211199999999999000.0;',
  6787. 'var',
  6788. ' d: double = b;',
  6789. 'begin',
  6790. ' d:=1.0;',
  6791. ' d:=1.0/3.0;',
  6792. ' d:=1/3;',
  6793. ' d:=5.0E-324;',
  6794. ' d:=1.7E308;',
  6795. ' d:=001.00E00;',
  6796. ' d:=002.00E001;',
  6797. ' d:=003.000E000;',
  6798. ' d:=-004.00E-00;',
  6799. ' d:=-005.00E-001;',
  6800. ' d:=10**3;',
  6801. ' d:=10 mod 3;',
  6802. ' d:=10 div 3;',
  6803. ' d:=c;',
  6804. ' d:=f0_1;',
  6805. ' d:=f0_3;',
  6806. ' d:=fn0_1;',
  6807. ' d:=fn0_3;',
  6808. ' d:=fn0_003;',
  6809. ' d:=fn0_123456789;',
  6810. ' d:=fn300_0;',
  6811. ' d:=fn123456_0;',
  6812. ' d:=fn1234567_8;',
  6813. ' d:=fn12345678_9;',
  6814. ' d:=f1_0En12;',
  6815. ' d:=fn1_0En12;',
  6816. ' d:=maxdouble;',
  6817. ' d:=mindouble;',
  6818. ' d:=MinSafeIntDouble;',
  6819. ' d:=double(MinSafeIntDouble);',
  6820. ' d:=MinSafeIntDouble2;',
  6821. ' d:=double(MinSafeIntDouble2);',
  6822. ' d:=MaxSafeIntDouble;',
  6823. ' d:=default(double);',
  6824. '']);
  6825. ConvertProgram;
  6826. CheckSource('TestDouble',
  6827. LinesToStr([
  6828. 'this.a = 2.7;',
  6829. 'this.b = 2.7 + 1.7;',
  6830. 'this.c = 0.9 + 0.1;',
  6831. 'this.f0_1 = 0.1;',
  6832. 'this.f0_3 = 0.3;',
  6833. 'this.fn0_1 = -0.1;',
  6834. 'this.fn0_3 = -0.3;',
  6835. 'this.fn0_003 = -0.003;',
  6836. 'this.fn0_123456789 = -0.123456789;',
  6837. 'this.fn300_0 = -300.0;',
  6838. 'this.fn123456_0 = -123456.0;',
  6839. 'this.fn1234567_8 = -1234567.8;',
  6840. 'this.fn12345678_9 = -12345678.9;',
  6841. 'this.f1_0En12 = 1E-12;',
  6842. 'this.fn1_0En12 = -1E-12;',
  6843. 'this.maxdouble = 1.7e+308;',
  6844. 'this.mindouble = -1.7e+308;',
  6845. 'this.MinSafeIntDouble = -0x1fffffffffffff;',
  6846. 'this.MinSafeIntDouble2 = -0x20000000000000 - 1;',
  6847. 'this.MaxSafeIntDouble = 0x1fffffffffffff;',
  6848. 'this.DZeroResolution = 1E-12;',
  6849. 'this.Minus1 = -1E-12;',
  6850. 'this.EPS = 1E-9;',
  6851. 'this.DELTA = 0.001;',
  6852. 'this.Big = 129.789E+100;',
  6853. 'this.Test0_15 = 0.15;',
  6854. 'this.Test999 = 2.9999999999999;',
  6855. 'this.Test111999 = 211199999999999000.0;',
  6856. 'this.TestMinus111999 = -211199999999999000.0;',
  6857. 'this.d = 4.4;'
  6858. ]),
  6859. LinesToStr([
  6860. '$mod.d = 1.0;',
  6861. '$mod.d = 1.0 / 3.0;',
  6862. '$mod.d = 1 / 3;',
  6863. '$mod.d = 5.0E-324;',
  6864. '$mod.d = 1.7E308;',
  6865. '$mod.d = 1.00E0;',
  6866. '$mod.d = 2.00E1;',
  6867. '$mod.d = 3.000E0;',
  6868. '$mod.d = -4.00E-0;',
  6869. '$mod.d = -5.00E-1;',
  6870. '$mod.d = Math.pow(10, 3);',
  6871. '$mod.d = 10 % 3;',
  6872. '$mod.d = rtl.trunc(10 / 3);',
  6873. '$mod.d = 1;',
  6874. '$mod.d = 0.1;',
  6875. '$mod.d = 0.3;',
  6876. '$mod.d = -0.1;',
  6877. '$mod.d = -0.3;',
  6878. '$mod.d = -0.003;',
  6879. '$mod.d = -0.123456789;',
  6880. '$mod.d = -300;',
  6881. '$mod.d = -123456;',
  6882. '$mod.d = -1234567.8;',
  6883. '$mod.d = -1.23456789E7;',
  6884. '$mod.d = 1E-12;',
  6885. '$mod.d = -1E-12;',
  6886. '$mod.d = 1.7E308;',
  6887. '$mod.d = -1.7E308;',
  6888. '$mod.d = -9007199254740991;',
  6889. '$mod.d = -9007199254740991;',
  6890. '$mod.d = -9.007199254740992E15;',
  6891. '$mod.d = -9.007199254740992E15;',
  6892. '$mod.d = 9007199254740991;',
  6893. '$mod.d = 0.0;',
  6894. '']));
  6895. end;
  6896. procedure TTestModule.TestInteger;
  6897. begin
  6898. StartProgram(false);
  6899. Add([
  6900. 'const',
  6901. ' MinInt = low(NativeInt);',
  6902. ' MaxInt = high(NativeInt);',
  6903. 'type',
  6904. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  6905. 'const',
  6906. ' a = low(TMyInt)+High(TMyInt);',
  6907. 'var',
  6908. ' i: TMyInt;',
  6909. 'begin',
  6910. ' i:=-MinInt;',
  6911. ' i:=default(TMyInt);',
  6912. ' i:=low(i)+high(i);',
  6913. '']);
  6914. ConvertProgram;
  6915. CheckSource('TestIntegerRange',
  6916. LinesToStr([
  6917. 'this.MinInt = -9007199254740991;',
  6918. 'this.MaxInt = 9007199254740991;',
  6919. 'this.a = -9007199254740991 + 9007199254740991;',
  6920. 'this.i = 0;',
  6921. '']),
  6922. LinesToStr([
  6923. '$mod.i = - -9007199254740991;',
  6924. '$mod.i = -9007199254740991;',
  6925. '$mod.i = -9007199254740991 + 9007199254740991;',
  6926. '']));
  6927. end;
  6928. procedure TTestModule.TestIntegerRange;
  6929. begin
  6930. StartProgram(false);
  6931. Add([
  6932. 'const',
  6933. ' MinInt = -1;',
  6934. ' MaxInt = +1;',
  6935. 'type',
  6936. ' {#TMyInt}TMyInt = MinInt..MaxInt;',
  6937. ' TInt2 = 1..3;',
  6938. 'const',
  6939. ' a = low(TMyInt)+High(TMyInt);',
  6940. ' b = low(TInt2)+High(TInt2);',
  6941. ' s1 = [1];',
  6942. ' s2 = [1,2];',
  6943. ' s3 = [1..3];',
  6944. ' s4 = [low(shortint)..high(shortint)];',
  6945. ' s5 = [succ(low(shortint))..pred(high(shortint))];',
  6946. ' s6 = 1 in s2;',
  6947. 'var',
  6948. ' i: TMyInt;',
  6949. ' i2: TInt2;',
  6950. 'begin',
  6951. ' i:=i2;',
  6952. ' i:=default(TMyInt);',
  6953. ' if i=i2 then ;',
  6954. ' i:=ord(i2);',
  6955. '']);
  6956. ConvertProgram;
  6957. CheckSource('TestIntegerRange',
  6958. LinesToStr([
  6959. 'this.MinInt = -1;',
  6960. 'this.MaxInt = +1;',
  6961. 'this.a = -1 + 1;',
  6962. 'this.b = 1 + 3;',
  6963. 'this.s1 = rtl.createSet(1);',
  6964. 'this.s2 = rtl.createSet(1, 2);',
  6965. 'this.s3 = rtl.createSet(null, 1, 3);',
  6966. 'this.s4 = rtl.createSet(null, -128, 127);',
  6967. 'this.s5 = rtl.createSet(null, -128 + 1, 127 - 1);',
  6968. 'this.s6 = 1 in this.s2;',
  6969. 'this.i = 0;',
  6970. 'this.i2 = 0;',
  6971. '']),
  6972. LinesToStr([
  6973. '$mod.i = $mod.i2;',
  6974. '$mod.i = -1;',
  6975. 'if ($mod.i === $mod.i2) ;',
  6976. '$mod.i = $mod.i2;',
  6977. '']));
  6978. end;
  6979. procedure TTestModule.TestIntegerTypecasts;
  6980. begin
  6981. StartProgram(false);
  6982. Add([
  6983. 'var',
  6984. ' i: nativeint;',
  6985. ' b: byte;',
  6986. ' sh: shortint;',
  6987. ' w: word;',
  6988. ' sm: smallint;',
  6989. ' lw: longword;',
  6990. ' li: longint;',
  6991. 'begin',
  6992. ' b:=byte(i);',
  6993. ' sh:=shortint(i);',
  6994. ' w:=word(i);',
  6995. ' sm:=smallint(i);',
  6996. ' lw:=longword(i);',
  6997. ' li:=longint(i);',
  6998. '']);
  6999. ConvertProgram;
  7000. CheckSource('TestIntegerTypecasts',
  7001. LinesToStr([
  7002. 'this.i = 0;',
  7003. 'this.b = 0;',
  7004. 'this.sh = 0;',
  7005. 'this.w = 0;',
  7006. 'this.sm = 0;',
  7007. 'this.lw = 0;',
  7008. 'this.li = 0;',
  7009. '']),
  7010. LinesToStr([
  7011. '$mod.b = $mod.i & 255;',
  7012. '$mod.sh = (($mod.i & 255) << 24) >> 24;',
  7013. '$mod.w = $mod.i & 65535;',
  7014. '$mod.sm = (($mod.i & 65535) << 16) >> 16;',
  7015. '$mod.lw = $mod.i >>> 0;',
  7016. '$mod.li = $mod.i & 0xFFFFFFFF;',
  7017. '']));
  7018. end;
  7019. procedure TTestModule.TestInteger_BitwiseShrNativeInt;
  7020. begin
  7021. StartProgram(false);
  7022. Add([
  7023. 'var',
  7024. ' i,j: nativeint;',
  7025. 'begin',
  7026. ' i:=i shr 0;',
  7027. ' i:=i shr 1;',
  7028. ' i:=i shr 3;',
  7029. ' i:=i shr 54;',
  7030. ' i:=j shr i;',
  7031. '']);
  7032. ConvertProgram;
  7033. CheckResolverUnexpectedHints;
  7034. CheckSource('TestInteger_BitwiseShrNativeInt',
  7035. LinesToStr([
  7036. 'this.i = 0;',
  7037. 'this.j = 0;',
  7038. '']),
  7039. LinesToStr([
  7040. '$mod.i = $mod.i;',
  7041. '$mod.i = Math.floor($mod.i / 2);',
  7042. '$mod.i = Math.floor($mod.i / 8);',
  7043. '$mod.i = 0;',
  7044. '$mod.i = rtl.shr($mod.j, $mod.i);',
  7045. '']));
  7046. end;
  7047. procedure TTestModule.TestInteger_BitwiseShlNativeInt;
  7048. begin
  7049. StartProgram(false);
  7050. Add([
  7051. 'var',
  7052. ' i: nativeint;',
  7053. 'begin',
  7054. ' i:=i shl 0;',
  7055. ' i:=i shl 54;',
  7056. ' i:=123456789012 shl 1;',
  7057. ' i:=i shl 1;',
  7058. '']);
  7059. ConvertProgram;
  7060. CheckResolverUnexpectedHints;
  7061. CheckSource('TestInteger_BitwiseShrNativeInt',
  7062. LinesToStr([
  7063. 'this.i = 0;',
  7064. '']),
  7065. LinesToStr([
  7066. '$mod.i = $mod.i;',
  7067. '$mod.i = 0;',
  7068. '$mod.i = 246913578024;',
  7069. '$mod.i = rtl.shl($mod.i, 1);',
  7070. '']));
  7071. end;
  7072. procedure TTestModule.TestInteger_SystemFunc;
  7073. begin
  7074. StartProgram(true);
  7075. Add([
  7076. 'var',
  7077. ' i: byte;',
  7078. ' s: string;',
  7079. 'begin',
  7080. ' system.inc(i);',
  7081. ' system.str(i,s);',
  7082. ' s:=system.str(i);',
  7083. ' i:=system.low(i);',
  7084. ' i:=system.high(i);',
  7085. ' i:=system.pred(i);',
  7086. ' i:=system.succ(i);',
  7087. ' i:=system.ord(i);',
  7088. '']);
  7089. ConvertProgram;
  7090. CheckResolverUnexpectedHints;
  7091. CheckSource('TestInteger_SystemFunc',
  7092. LinesToStr([
  7093. 'this.i = 0;',
  7094. 'this.s = "";',
  7095. '']),
  7096. LinesToStr([
  7097. '$mod.i += 1;',
  7098. '$mod.s = "" + $mod.i;',
  7099. '$mod.s = "" + $mod.i;',
  7100. '$mod.i = 0;',
  7101. '$mod.i = 255;',
  7102. '$mod.i = $mod.i - 1;',
  7103. '$mod.i = $mod.i + 1;',
  7104. '$mod.i = $mod.i;',
  7105. '']));
  7106. end;
  7107. procedure TTestModule.TestCurrency;
  7108. begin
  7109. StartProgram(false);
  7110. Add([
  7111. 'type',
  7112. ' TCoin = currency;',
  7113. 'const',
  7114. ' a = TCoin(2.7);',
  7115. ' b = a + TCoin(1.7);',
  7116. ' MinSafeIntCurrency: TCoin = -92233720368.5477;',
  7117. ' MaxSafeIntCurrency: TCoin = 92233720368.5477;',
  7118. 'var',
  7119. ' c: TCoin = b;',
  7120. ' i: nativeint;',
  7121. ' d: double;',
  7122. ' j: jsvalue;',
  7123. 'function DoIt(c: currency): currency; begin end;',
  7124. 'function GetIt(d: double): double; begin end;',
  7125. 'procedure Write(v: jsvalue); begin end;',
  7126. 'begin',
  7127. ' c:=1.0;',
  7128. ' c:=0.1;',
  7129. ' c:=1.0/3.0;',
  7130. ' c:=1/3;',
  7131. ' c:=a;',
  7132. ' d:=c;',
  7133. ' c:=d;',
  7134. ' c:=currency(c);',
  7135. ' c:=currency(d);',
  7136. ' d:=double(c);',
  7137. ' c:=i;',
  7138. ' c:=currency(i);',
  7139. //' i:=c;', not allowed
  7140. ' i:=nativeint(c);',
  7141. ' c:=c+a;',
  7142. ' c:=-c-a;',
  7143. ' c:=d+c;',
  7144. ' c:=c+d;',
  7145. ' c:=d-c;',
  7146. ' c:=c-d;',
  7147. ' c:=c*a;',
  7148. ' c:=a*c;',
  7149. ' c:=d*c;',
  7150. ' c:=c*d;',
  7151. ' c:=c/a;',
  7152. ' c:=a/c;',
  7153. ' c:=d/c;',
  7154. ' c:=c/d;',
  7155. ' c:=c**a;',
  7156. ' c:=a**c;',
  7157. ' c:=d**c;',
  7158. ' c:=c**d;',
  7159. ' if c=c then ;',
  7160. ' if c=a then ;',
  7161. ' if a=c then ;',
  7162. ' if d=c then ;',
  7163. ' if c=d then ;',
  7164. ' c:=DoIt(c);',
  7165. ' c:=DoIt(i);',
  7166. ' c:=DoIt(d);',
  7167. ' c:=GetIt(c);',
  7168. ' j:=c;',
  7169. ' Write(c);',
  7170. ' c:=default(currency);',
  7171. ' j:=str(c);',
  7172. ' j:=str(c:0:3);',
  7173. '']);
  7174. ConvertProgram;
  7175. CheckSource('TestCurrency',
  7176. LinesToStr([
  7177. 'this.a = 27000;',
  7178. 'this.b = this.a + 17000;',
  7179. 'this.MinSafeIntCurrency = -92233720368.5477;',
  7180. 'this.MaxSafeIntCurrency = 92233720368.5477;',
  7181. 'this.c = this.b;',
  7182. 'this.i = 0;',
  7183. 'this.d = 0.0;',
  7184. 'this.j = undefined;',
  7185. 'this.DoIt = function (c) {',
  7186. ' var Result = 0;',
  7187. ' return Result;',
  7188. '};',
  7189. 'this.GetIt = function (d) {',
  7190. ' var Result = 0.0;',
  7191. ' return Result;',
  7192. '};',
  7193. 'this.Write = function (v) {',
  7194. '};',
  7195. '']),
  7196. LinesToStr([
  7197. '$mod.c = 10000;',
  7198. '$mod.c = 1000;',
  7199. '$mod.c = rtl.trunc((1.0 / 3.0) * 10000);',
  7200. '$mod.c = rtl.trunc((1 / 3) * 10000);',
  7201. '$mod.c = $mod.a;',
  7202. '$mod.d = $mod.c / 10000;',
  7203. '$mod.c = rtl.trunc($mod.d * 10000);',
  7204. '$mod.c = $mod.c;',
  7205. '$mod.c = $mod.d * 10000;',
  7206. '$mod.d = $mod.c / 10000;',
  7207. '$mod.c = $mod.i * 10000;',
  7208. '$mod.c = $mod.i * 10000;',
  7209. '$mod.i = rtl.trunc($mod.c / 10000);',
  7210. '$mod.c = $mod.c + $mod.a;',
  7211. '$mod.c = -$mod.c - $mod.a;',
  7212. '$mod.c = ($mod.d * 10000) + $mod.c;',
  7213. '$mod.c = $mod.c + ($mod.d * 10000);',
  7214. '$mod.c = ($mod.d * 10000) - $mod.c;',
  7215. '$mod.c = $mod.c - ($mod.d * 10000);',
  7216. '$mod.c = ($mod.c * $mod.a) / 10000;',
  7217. '$mod.c = ($mod.a * $mod.c) / 10000;',
  7218. '$mod.c = $mod.d * $mod.c;',
  7219. '$mod.c = $mod.c * $mod.d;',
  7220. '$mod.c = rtl.trunc(($mod.c / $mod.a) * 10000);',
  7221. '$mod.c = rtl.trunc(($mod.a / $mod.c) * 10000);',
  7222. '$mod.c = rtl.trunc($mod.d / $mod.c);',
  7223. '$mod.c = rtl.trunc($mod.c / $mod.d);',
  7224. '$mod.c = rtl.trunc(Math.pow($mod.c / 10000, $mod.a / 10000) * 10000);',
  7225. '$mod.c = rtl.trunc(Math.pow($mod.a / 10000, $mod.c / 10000) * 10000);',
  7226. '$mod.c = rtl.trunc(Math.pow($mod.d, $mod.c / 10000) * 10000);',
  7227. '$mod.c = rtl.trunc(Math.pow($mod.c / 10000, $mod.d) * 10000);',
  7228. 'if ($mod.c === $mod.c) ;',
  7229. 'if ($mod.c === $mod.a) ;',
  7230. 'if ($mod.a === $mod.c) ;',
  7231. 'if (($mod.d * 10000) === $mod.c) ;',
  7232. 'if ($mod.c === ($mod.d * 10000)) ;',
  7233. '$mod.c = $mod.DoIt($mod.c);',
  7234. '$mod.c = $mod.DoIt($mod.i * 10000);',
  7235. '$mod.c = $mod.DoIt($mod.d * 10000);',
  7236. '$mod.c = rtl.trunc($mod.GetIt($mod.c / 10000) * 10000);',
  7237. '$mod.j = $mod.c / 10000;',
  7238. '$mod.Write($mod.c / 10000);',
  7239. '$mod.c = 0;',
  7240. '$mod.j = rtl.floatToStr($mod.c / 10000);',
  7241. '$mod.j = rtl.floatToStr($mod.c / 10000, 0, 3);',
  7242. '']));
  7243. end;
  7244. procedure TTestModule.TestForBoolDo;
  7245. begin
  7246. StartProgram(false);
  7247. Add([
  7248. 'var b: boolean;',
  7249. 'begin',
  7250. ' for b:=false to true do ;',
  7251. ' for b:=b downto false do ;',
  7252. ' for b in boolean do ;',
  7253. '']);
  7254. ConvertProgram;
  7255. CheckSource('TestForBoolDo',
  7256. LinesToStr([ // statements
  7257. 'this.b = false;']),
  7258. LinesToStr([ // this.$main
  7259. 'for (var $l = 0; $l <= 1; $l++) $mod.b = $l !== 0;',
  7260. 'for (var $l1 = +$mod.b; $l1 >= 0; $l1--) $mod.b = $l1 !== 0;',
  7261. 'for (var $l2 = 0; $l2 <= 1; $l2++) $mod.b = $l2 !== 0;',
  7262. '']));
  7263. end;
  7264. procedure TTestModule.TestForIntDo;
  7265. begin
  7266. StartProgram(false);
  7267. Add([
  7268. 'var i: longint;',
  7269. 'begin',
  7270. ' for i:=3 to 5 do ;',
  7271. ' for i:=i downto 2 do ;',
  7272. ' for i in byte do ;',
  7273. '']);
  7274. ConvertProgram;
  7275. CheckSource('TestForIntDo',
  7276. LinesToStr([ // statements
  7277. 'this.i = 0;']),
  7278. LinesToStr([ // this.$main
  7279. 'for ($mod.i = 3; $mod.i <= 5; $mod.i++) ;',
  7280. 'for (var $l = $mod.i; $l >= 2; $l--) $mod.i = $l;',
  7281. 'for (var $l1 = 0; $l1 <= 255; $l1++) $mod.i = $l1;',
  7282. '']));
  7283. end;
  7284. procedure TTestModule.TestForIntInDo;
  7285. begin
  7286. StartProgram(false);
  7287. Add([
  7288. 'type',
  7289. ' TSetOfInt = set of byte;',
  7290. ' TIntRg = 3..7;',
  7291. ' TSetOfIntRg = set of TIntRg;',
  7292. 'var',
  7293. ' i,i2: longint;',
  7294. ' a1: array of byte;',
  7295. ' a2: array[1..3] of byte;',
  7296. ' soi: TSetOfInt;',
  7297. ' soir: TSetOfIntRg;',
  7298. ' ir: TIntRg;',
  7299. 'begin',
  7300. ' for i in byte do ;',
  7301. ' for i in a1 do ;',
  7302. ' for i in a2 do ;',
  7303. ' for i in [11..13] do ;',
  7304. ' for i in TSetOfInt do ;',
  7305. ' for i in TIntRg do ;',
  7306. ' for i in soi do i2:=i;',
  7307. ' for i in TSetOfIntRg do ;',
  7308. ' for i in soir do ;',
  7309. ' for ir in TIntRg do ;',
  7310. ' for ir in TSetOfIntRg do ;',
  7311. ' for ir in soir do ;',
  7312. '']);
  7313. ConvertProgram;
  7314. CheckSource('TestForIntInDo',
  7315. LinesToStr([ // statements
  7316. 'this.i = 0;',
  7317. 'this.i2 = 0;',
  7318. 'this.a1 = [];',
  7319. 'this.a2 = rtl.arraySetLength(null, 0, 3);',
  7320. 'this.soi = {};',
  7321. 'this.soir = {};',
  7322. 'this.ir = 0;',
  7323. '']),
  7324. LinesToStr([ // this.$main
  7325. 'for (var $l = 0; $l <= 255; $l++) $mod.i = $l;',
  7326. 'for (var $in = $mod.a1, $l1 = 0, $end = rtl.length($in) - 1; $l1 <= $end; $l1++) $mod.i = $in[$l1];',
  7327. 'for (var $in1 = $mod.a2, $l2 = 0, $end1 = rtl.length($in1) - 1; $l2 <= $end1; $l2++) $mod.i = $in1[$l2];',
  7328. 'for (var $l3 = 11; $l3 <= 13; $l3++) $mod.i = $l3;',
  7329. 'for (var $l4 = 0; $l4 <= 255; $l4++) $mod.i = $l4;',
  7330. 'for (var $l5 = 3; $l5 <= 7; $l5++) $mod.i = $l5;',
  7331. 'for (var $l6 in $mod.soi) {',
  7332. ' $mod.i = +$l6;',
  7333. ' $mod.i2 = $mod.i;',
  7334. '};',
  7335. 'for (var $l7 = 3; $l7 <= 7; $l7++) $mod.i = $l7;',
  7336. 'for (var $l8 in $mod.soir) $mod.i = +$l8;',
  7337. 'for (var $l9 = 3; $l9 <= 7; $l9++) $mod.ir = $l9;',
  7338. 'for (var $l10 = 3; $l10 <= 7; $l10++) $mod.ir = $l10;',
  7339. 'for (var $l11 in $mod.soir) $mod.ir = +$l11;',
  7340. '']));
  7341. end;
  7342. procedure TTestModule.TestCharConst;
  7343. begin
  7344. StartProgram(false);
  7345. Add([
  7346. 'const',
  7347. ' a = #$00F3;',
  7348. ' c: char = ''1'';',
  7349. ' wc: widechar = ''ä'';',
  7350. 'begin',
  7351. ' c:=#0;',
  7352. ' c:=#1;',
  7353. ' c:=#9;',
  7354. ' c:=#10;',
  7355. ' c:=#13;',
  7356. ' c:=#31;',
  7357. ' c:=#32;',
  7358. ' c:=#$A;',
  7359. ' c:=#$0A;',
  7360. ' c:=#$b;',
  7361. ' c:=#$0b;',
  7362. ' c:=^A;',
  7363. ' c:=''"'';',
  7364. ' c:=default(char);',
  7365. ' c:=#$00E4;', // ä
  7366. ' c:=''ä'';',
  7367. ' c:=#$E4;', // ä
  7368. ' c:=#$D800;', // invalid UTF-16
  7369. ' c:=#$DFFF;', // invalid UTF-16
  7370. ' c:=#$FFFF;', // last UCS-2
  7371. ' c:=high(c);', // last UCS-2
  7372. ' c:=#269;',
  7373. '']);
  7374. ConvertProgram;
  7375. CheckSource('TestCharConst',
  7376. LinesToStr([
  7377. 'this.a="ó";',
  7378. 'this.c="1";',
  7379. 'this.wc="ä";'
  7380. ]),
  7381. LinesToStr([
  7382. '$mod.c="\x00";',
  7383. '$mod.c="\x01";',
  7384. '$mod.c="\t";',
  7385. '$mod.c="\n";',
  7386. '$mod.c="\r";',
  7387. '$mod.c="\x1F";',
  7388. '$mod.c=" ";',
  7389. '$mod.c="\n";',
  7390. '$mod.c="\n";',
  7391. '$mod.c="\x0B";',
  7392. '$mod.c="\x0B";',
  7393. '$mod.c="\x01";',
  7394. '$mod.c=''"'';',
  7395. '$mod.c="\x00";',
  7396. '$mod.c = "ä";',
  7397. '$mod.c = "ä";',
  7398. '$mod.c = "ä";',
  7399. '$mod.c="\uD800";',
  7400. '$mod.c="\uDFFF";',
  7401. '$mod.c="\uFFFF";',
  7402. '$mod.c="\uFFFF";',
  7403. '$mod.c = "č";',
  7404. '']));
  7405. end;
  7406. procedure TTestModule.TestChar_Compare;
  7407. begin
  7408. StartProgram(false);
  7409. Add('var');
  7410. Add(' c: char;');
  7411. Add(' b: boolean;');
  7412. Add('begin');
  7413. Add(' b:=c=''1'';');
  7414. Add(' b:=''2''=c;');
  7415. Add(' b:=''3''=''4'';');
  7416. Add(' b:=c<>''5'';');
  7417. Add(' b:=''6''<>c;');
  7418. Add(' b:=c>''7'';');
  7419. Add(' b:=''8''>c;');
  7420. Add(' b:=c>=''9'';');
  7421. Add(' b:=''A''>=c;');
  7422. Add(' b:=c<''B'';');
  7423. Add(' b:=''C''<c;');
  7424. Add(' b:=c<=''D'';');
  7425. Add(' b:=''E''<=c;');
  7426. ConvertProgram;
  7427. CheckSource('TestChar_Compare',
  7428. LinesToStr([
  7429. 'this.c="";',
  7430. 'this.b = false;'
  7431. ]),
  7432. LinesToStr([
  7433. '$mod.b = $mod.c === "1";',
  7434. '$mod.b = "2" === $mod.c;',
  7435. '$mod.b = "3" === "4";',
  7436. '$mod.b = $mod.c !== "5";',
  7437. '$mod.b = "6" !== $mod.c;',
  7438. '$mod.b = $mod.c > "7";',
  7439. '$mod.b = "8" > $mod.c;',
  7440. '$mod.b = $mod.c >= "9";',
  7441. '$mod.b = "A" >= $mod.c;',
  7442. '$mod.b = $mod.c < "B";',
  7443. '$mod.b = "C" < $mod.c;',
  7444. '$mod.b = $mod.c <= "D";',
  7445. '$mod.b = "E" <= $mod.c;',
  7446. '']));
  7447. end;
  7448. procedure TTestModule.TestChar_BuiltInProcs;
  7449. begin
  7450. StartProgram(false);
  7451. Add([
  7452. 'var',
  7453. ' c: char;',
  7454. ' i: longint;',
  7455. ' s: string;',
  7456. 'begin',
  7457. ' i:=ord(c);',
  7458. ' i:=ord(s[i]);',
  7459. ' c:=chr(i);',
  7460. ' c:=pred(c);',
  7461. ' c:=succ(c);',
  7462. ' c:=low(c);',
  7463. ' c:=high(c);',
  7464. ' i:=byte(c);',
  7465. ' i:=word(c);',
  7466. ' i:=longint(c);',
  7467. '']);
  7468. ConvertProgram;
  7469. CheckSource('TestChar_BuiltInProcs',
  7470. LinesToStr([
  7471. 'this.c = "";',
  7472. 'this.i = 0;',
  7473. 'this.s = "";'
  7474. ]),
  7475. LinesToStr([
  7476. '$mod.i = $mod.c.charCodeAt();',
  7477. '$mod.i = $mod.s.charCodeAt($mod.i-1);',
  7478. '$mod.c = String.fromCharCode($mod.i);',
  7479. '$mod.c = String.fromCharCode($mod.c.charCodeAt() - 1);',
  7480. '$mod.c = String.fromCharCode($mod.c.charCodeAt() + 1);',
  7481. '$mod.c = "\x00";',
  7482. '$mod.c = "\uFFFF";',
  7483. '$mod.i = $mod.c.charCodeAt() & 255;',
  7484. '$mod.i = $mod.c.charCodeAt();',
  7485. '$mod.i = $mod.c.charCodeAt() & 0xFFFFFFFF;',
  7486. '']));
  7487. end;
  7488. procedure TTestModule.TestStringConst;
  7489. begin
  7490. StartProgram(false);
  7491. Add([
  7492. '{$H+}',
  7493. 'const',
  7494. ' a = #$00F3#$017C;', // first <256, then >=256
  7495. ' b = string(''a'');',
  7496. ' c = string(''ä'');',
  7497. ' d = UnicodeString(''b'');',
  7498. ' e = UnicodeString(''ö'');',
  7499. 'var',
  7500. ' s: string = ''abc'';',
  7501. 'begin',
  7502. ' s:='''';',
  7503. ' s:=#13#10;',
  7504. ' s:=#9''foo'';',
  7505. ' s:=#$A9;',
  7506. ' s:=''foo''#13''bar'';',
  7507. ' s:=''"'';',
  7508. ' s:=''"''''"'';',
  7509. ' s:=#$20AC;', // euro
  7510. ' s:=#$10437;', // outside BMP
  7511. ' s:=''abc''#$20AC;', // ascii,#
  7512. ' s:=''ä''#$20AC;', // non ascii,#
  7513. ' s:=#$20AC''abc'';', // #, ascii
  7514. ' s:=#$20AC''ä'';', // #, non ascii
  7515. ' s:=default(string);',
  7516. ' s:=concat(s);',
  7517. ' s:=concat(s,''a'',s);',
  7518. ' s:=#250#269;',
  7519. //' s:=#$2F804;',
  7520. // ToDo: \uD87E\uDC04 -> \u{2F804}
  7521. '']);
  7522. ConvertProgram;
  7523. CheckSource('TestStringConst',
  7524. LinesToStr([
  7525. 'this.a = "óż";',
  7526. 'this.b = "a";',
  7527. 'this.c = "ä";',
  7528. 'this.d = "b";',
  7529. 'this.e = "ö";',
  7530. 'this.s="abc";',
  7531. '']),
  7532. LinesToStr([
  7533. '$mod.s="";',
  7534. '$mod.s="\r\n";',
  7535. '$mod.s="\tfoo";',
  7536. '$mod.s="©";',
  7537. '$mod.s="foo\rbar";',
  7538. '$mod.s=''"'';',
  7539. '$mod.s=''"\''"'';',
  7540. '$mod.s="€";',
  7541. '$mod.s="'#$F0#$90#$90#$B7'";',
  7542. '$mod.s = "abc€";',
  7543. '$mod.s = "ä€";',
  7544. '$mod.s = "€abc";',
  7545. '$mod.s = "ۊ";',
  7546. '$mod.s="";',
  7547. '$mod.s = $mod.s;',
  7548. '$mod.s = $mod.s.concat("a", $mod.s);',
  7549. '$mod.s = "úč";',
  7550. '']));
  7551. end;
  7552. procedure TTestModule.TestStringConst_InvalidUTF16;
  7553. begin
  7554. StartProgram(false);
  7555. Add([
  7556. 'const',
  7557. ' a: char = #$D87E;',
  7558. ' b: string = #$D87E;',
  7559. ' c: string = #$D87E#43;',
  7560. 'begin',
  7561. ' c:=''abc''#$D87E;',
  7562. ' c:=#0#1#2;',
  7563. ' c:=#127;',
  7564. ' c:=#128;',
  7565. ' c:=#255;',
  7566. ' c:=#256;',
  7567. '']);
  7568. ConvertProgram;
  7569. CheckSource('TestStringConst',
  7570. LinesToStr([
  7571. 'this.a = "\uD87E";',
  7572. 'this.b = "\uD87E";',
  7573. 'this.c = "\uD87E+";',
  7574. '']),
  7575. LinesToStr([
  7576. '$mod.c = "abc\uD87E";',
  7577. '$mod.c = "\x00\x01\x02";',
  7578. '$mod.c = "'#127'";',
  7579. '$mod.c = "'#$c2#$80'";',
  7580. '$mod.c = "'#$c3#$BF'";',
  7581. '$mod.c = "'#$c4#$80'";',
  7582. '']));
  7583. end;
  7584. procedure TTestModule.TestStringConstSurrogate;
  7585. begin
  7586. StartProgram(false);
  7587. Add([
  7588. 'var',
  7589. ' s: string;',
  7590. 'begin',
  7591. ' s:=''😊'';', // 1F60A
  7592. '']);
  7593. ConvertProgram;
  7594. CheckSource('TestStringConstSurrogate',
  7595. LinesToStr([
  7596. 'this.s="";'
  7597. ]),
  7598. LinesToStr([
  7599. '$mod.s="😊";'
  7600. ]));
  7601. end;
  7602. procedure TTestModule.TestString_Length;
  7603. begin
  7604. StartProgram(false);
  7605. Add('const c = ''foo'';');
  7606. Add('var');
  7607. Add(' s: string;');
  7608. Add(' i: longint;');
  7609. Add('begin');
  7610. Add(' i:=length(s);');
  7611. Add(' i:=length(s+s);');
  7612. Add(' i:=length(''abc'');');
  7613. Add(' i:=length(c);');
  7614. ConvertProgram;
  7615. CheckSource('TestString_Length',
  7616. LinesToStr([
  7617. 'this.c = "foo";',
  7618. 'this.s = "";',
  7619. 'this.i = 0;',
  7620. '']),
  7621. LinesToStr([
  7622. '$mod.i = $mod.s.length;',
  7623. '$mod.i = ($mod.s+$mod.s).length;',
  7624. '$mod.i = "abc".length;',
  7625. '$mod.i = $mod.c.length;',
  7626. '']));
  7627. end;
  7628. procedure TTestModule.TestString_Compare;
  7629. begin
  7630. StartProgram(false);
  7631. Add('var');
  7632. Add(' s, t: string;');
  7633. Add(' b: boolean;');
  7634. Add('begin');
  7635. Add(' b:=s=t;');
  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. ConvertProgram;
  7642. CheckSource('TestString_Compare',
  7643. LinesToStr([ // statements
  7644. 'this.s = "";',
  7645. 'this.t = "";',
  7646. 'this.b =false;'
  7647. ]),
  7648. LinesToStr([ // this.$main
  7649. '$mod.b = $mod.s === $mod.t;',
  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. '']));
  7656. end;
  7657. procedure TTestModule.TestString_SetLength;
  7658. begin
  7659. StartProgram(false);
  7660. Add([
  7661. 'procedure DoIt(var s: string);',
  7662. 'begin',
  7663. ' SetLength(s,2);',
  7664. 'end;',
  7665. 'var s: string;',
  7666. 'begin',
  7667. ' SetLength(s,3);',
  7668. '']);
  7669. ConvertProgram;
  7670. CheckSource('TestString_SetLength',
  7671. LinesToStr([ // statements
  7672. 'this.DoIt = function (s) {',
  7673. ' s.set(rtl.strSetLength(s.get(), 2));',
  7674. '};',
  7675. 'this.s = "";',
  7676. '']),
  7677. LinesToStr([ // this.$main
  7678. '$mod.s = rtl.strSetLength($mod.s, 3);'
  7679. ]));
  7680. end;
  7681. procedure TTestModule.TestString_CharAt;
  7682. begin
  7683. StartProgram(false);
  7684. Add([
  7685. 'var',
  7686. ' s: string;',
  7687. ' c: char;',
  7688. ' b: boolean;',
  7689. 'begin',
  7690. ' b:= s[1] = c;',
  7691. ' b:= c = s[1];',
  7692. ' b:= c <> s[1];',
  7693. ' b:= c > s[1];',
  7694. ' b:= c >= s[1];',
  7695. ' b:= c < s[2];',
  7696. ' b:= c <= s[1];',
  7697. ' s[1] := c;',
  7698. ' s[2+3] := c;']);
  7699. ConvertProgram;
  7700. CheckSource('TestString_CharAt',
  7701. LinesToStr([ // statements
  7702. 'this.s = "";',
  7703. 'this.c = "";',
  7704. 'this.b = false;'
  7705. ]),
  7706. LinesToStr([ // this.$main
  7707. '$mod.b = $mod.s.charAt(0) === $mod.c;',
  7708. '$mod.b = $mod.c === $mod.s.charAt(0);',
  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(1);',
  7713. '$mod.b = $mod.c <= $mod.s.charAt(0);',
  7714. '$mod.s = rtl.setCharAt($mod.s, 0, $mod.c);',
  7715. '$mod.s = rtl.setCharAt($mod.s, (2 + 3) - 1, $mod.c);',
  7716. '']));
  7717. end;
  7718. procedure TTestModule.TestStringHMinusFail;
  7719. begin
  7720. StartProgram(false);
  7721. Add([
  7722. '{$H-}',
  7723. 'var s: string;',
  7724. 'begin']);
  7725. ConvertProgram;
  7726. CheckHint(mtWarning,nWarnIllegalCompilerDirectiveX,'Warning: test1.pp(3,6) : Illegal compiler directive "H-"');
  7727. end;
  7728. procedure TTestModule.TestStr;
  7729. begin
  7730. StartProgram(false);
  7731. Add('var');
  7732. Add(' b: boolean;');
  7733. Add(' i: longint;');
  7734. Add(' d: double;');
  7735. Add(' s: string;');
  7736. Add('begin');
  7737. Add(' str(b,s);');
  7738. Add(' str(i,s);');
  7739. Add(' str(d,s);');
  7740. Add(' str(i:3,s);');
  7741. Add(' str(d:3:2,s);');
  7742. Add(' Str(12.456:12:1,s);');
  7743. Add(' Str(12.456:12,s);');
  7744. Add(' s:=str(b);');
  7745. Add(' s:=str(i);');
  7746. Add(' s:=str(d);');
  7747. Add(' s:=str(i,i);');
  7748. Add(' s:=str(i:3);');
  7749. Add(' s:=str(d:3:2);');
  7750. Add(' s:=str(i:4,i);');
  7751. Add(' s:=str(i,i:5);');
  7752. Add(' s:=str(i:4,i:5);');
  7753. Add(' s:=str(s,s);');
  7754. Add(' s:=str(s,''foo'');');
  7755. ConvertProgram;
  7756. CheckSource('TestStr',
  7757. LinesToStr([ // statements
  7758. 'this.b = false;',
  7759. 'this.i = 0;',
  7760. 'this.d = 0.0;',
  7761. 'this.s = "";',
  7762. '']),
  7763. LinesToStr([ // this.$main
  7764. '$mod.s = ""+$mod.b;',
  7765. '$mod.s = ""+$mod.i;',
  7766. '$mod.s = rtl.floatToStr($mod.d);',
  7767. '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
  7768. '$mod.s = rtl.floatToStr($mod.d,3,2);',
  7769. '$mod.s = rtl.floatToStr(12.456,12,1);',
  7770. '$mod.s = rtl.floatToStr(12.456,12);',
  7771. '$mod.s = ""+$mod.b;',
  7772. '$mod.s = ""+$mod.i;',
  7773. '$mod.s = rtl.floatToStr($mod.d);',
  7774. '$mod.s = ""+$mod.i+$mod.i;',
  7775. '$mod.s = rtl.spaceLeft(""+$mod.i,3);',
  7776. '$mod.s = rtl.floatToStr($mod.d,3,2);',
  7777. '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + $mod.i;',
  7778. '$mod.s = "" + $mod.i + rtl.spaceLeft("" + $mod.i, 5);',
  7779. '$mod.s = rtl.spaceLeft("" + $mod.i, 4) + rtl.spaceLeft("" + $mod.i, 5);',
  7780. '$mod.s = $mod.s + $mod.s;',
  7781. '$mod.s = $mod.s + "foo";',
  7782. '']));
  7783. end;
  7784. procedure TTestModule.TestBaseType_AnsiStringFail;
  7785. begin
  7786. StartProgram(false);
  7787. Add('var s: AnsiString');
  7788. SetExpectedPasResolverError('identifier not found "AnsiString"',PasResolveEval.nIdentifierNotFound);
  7789. ConvertProgram;
  7790. end;
  7791. procedure TTestModule.TestBaseType_WideStringFail;
  7792. begin
  7793. StartProgram(false);
  7794. Add('var s: WideString');
  7795. SetExpectedPasResolverError('identifier not found "WideString"',PasResolveEval.nIdentifierNotFound);
  7796. ConvertProgram;
  7797. end;
  7798. procedure TTestModule.TestBaseType_ShortStringFail;
  7799. begin
  7800. StartProgram(false);
  7801. Add('var s: ShortString');
  7802. SetExpectedPasResolverError('identifier not found "ShortString"',PasResolveEval.nIdentifierNotFound);
  7803. ConvertProgram;
  7804. end;
  7805. procedure TTestModule.TestBaseType_RawByteStringFail;
  7806. begin
  7807. StartProgram(false);
  7808. Add('var s: RawByteString');
  7809. SetExpectedPasResolverError('identifier not found "RawByteString"',PasResolveEval.nIdentifierNotFound);
  7810. ConvertProgram;
  7811. end;
  7812. procedure TTestModule.TestTypeShortstring_Fail;
  7813. begin
  7814. StartProgram(false);
  7815. Add('type t = string[12];');
  7816. Add('var s: t;');
  7817. Add('begin');
  7818. SetExpectedPasResolverError('illegal qualifier "["',nIllegalQualifier);
  7819. ConvertProgram;
  7820. end;
  7821. procedure TTestModule.TestCharSet_Custom;
  7822. begin
  7823. StartProgram(false);
  7824. Add([
  7825. 'type',
  7826. ' TCharRg = ''a''..''z'';',
  7827. ' TSetOfCharRg = set of TCharRg;',
  7828. ' TCharRg2 = ''m''..''p'';',
  7829. 'const',
  7830. ' crg: TCharRg = ''b'';',
  7831. 'var',
  7832. ' c: char;',
  7833. ' crg2: TCharRg2;',
  7834. ' s: TSetOfCharRg;',
  7835. 'begin',
  7836. ' c:=crg;',
  7837. ' crg:=c;',
  7838. ' crg2:=crg;',
  7839. ' if c=crg then ;',
  7840. ' if crg=c then ;',
  7841. ' if crg=crg2 then ;',
  7842. ' if c in s then ;',
  7843. ' if crg2 in s then ;',
  7844. ' c:=default(TCharRg);',
  7845. '']);
  7846. ConvertProgram;
  7847. CheckSource('TestCharSet_Custom',
  7848. LinesToStr([ // statements
  7849. 'this.crg = "b";',
  7850. 'this.c = "";',
  7851. 'this.crg2 = "m";',
  7852. 'this.s = {};',
  7853. '']),
  7854. LinesToStr([ // this.$main
  7855. '$mod.c = $mod.crg;',
  7856. '$mod.crg = $mod.c;',
  7857. '$mod.crg2 = $mod.crg;',
  7858. 'if ($mod.c === $mod.crg) ;',
  7859. 'if ($mod.crg === $mod.c) ;',
  7860. 'if ($mod.crg === $mod.crg2) ;',
  7861. 'if ($mod.c.charCodeAt() in $mod.s) ;',
  7862. 'if ($mod.crg2.charCodeAt() in $mod.s) ;',
  7863. '$mod.c = "a";',
  7864. '']));
  7865. end;
  7866. procedure TTestModule.TestWideChar;
  7867. begin
  7868. StartProgram(false);
  7869. Add([
  7870. 'procedure Fly(var c: char);',
  7871. 'begin',
  7872. 'end;',
  7873. 'procedure Run(var c: widechar);',
  7874. 'begin',
  7875. 'end;',
  7876. 'var',
  7877. ' c: char;',
  7878. ' wc: widechar;',
  7879. ' w: word;',
  7880. 'begin',
  7881. ' Fly(wc);',
  7882. ' Run(c);',
  7883. ' wc:=WideChar(w);',
  7884. ' w:=ord(wc);',
  7885. '']);
  7886. ConvertProgram;
  7887. CheckSource('TestWideChar_VarArg',
  7888. LinesToStr([ // statements
  7889. 'this.Fly = function (c) {',
  7890. '};',
  7891. 'this.Run = function (c) {',
  7892. '};',
  7893. 'this.c = "";',
  7894. 'this.wc = "";',
  7895. 'this.w = 0;',
  7896. '']),
  7897. LinesToStr([ // this.$main
  7898. '$mod.Fly({',
  7899. ' p: $mod,',
  7900. ' get: function () {',
  7901. ' return this.p.wc;',
  7902. ' },',
  7903. ' set: function (v) {',
  7904. ' this.p.wc = v;',
  7905. ' }',
  7906. '});',
  7907. '$mod.Run({',
  7908. ' p: $mod,',
  7909. ' get: function () {',
  7910. ' return this.p.c;',
  7911. ' },',
  7912. ' set: function (v) {',
  7913. ' this.p.c = v;',
  7914. ' }',
  7915. '});',
  7916. '$mod.wc = String.fromCharCode($mod.w);',
  7917. '$mod.w = $mod.wc.charCodeAt();',
  7918. '',
  7919. '']));
  7920. end;
  7921. procedure TTestModule.TestForCharDo;
  7922. begin
  7923. StartProgram(false);
  7924. Add([
  7925. 'var c: char;',
  7926. 'begin',
  7927. ' for c:=''a'' to ''c'' do ;',
  7928. ' for c:=c downto ''a'' do ;',
  7929. ' for c:=''Б'' to ''Я'' do ;',
  7930. '']);
  7931. ConvertProgram;
  7932. CheckSource('TestForCharDo',
  7933. LinesToStr([ // statements
  7934. 'this.c = "";']),
  7935. LinesToStr([ // this.$main
  7936. 'for (var $l = 97; $l <= 99; $l++) $mod.c = String.fromCharCode($l);',
  7937. 'for (var $l1 = $mod.c.charCodeAt(); $l1 >= 97; $l1--) $mod.c = String.fromCharCode($l1);',
  7938. 'for (var $l2 = 1041; $l2 <= 1071; $l2++) $mod.c = String.fromCharCode($l2);',
  7939. '']));
  7940. end;
  7941. procedure TTestModule.TestForCharInDo;
  7942. begin
  7943. StartProgram(false);
  7944. Add([
  7945. 'type',
  7946. ' TSetOfChar = set of char;',
  7947. ' TCharRg = ''a''..''z'';',
  7948. ' TSetOfCharRg = set of TCharRg;',
  7949. 'const Foo = ''foo'';',
  7950. 'var',
  7951. ' c,c2: char;',
  7952. ' s: string;',
  7953. ' a1: array of char;',
  7954. ' a2: array[1..3] of char;',
  7955. ' soc: TSetOfChar;',
  7956. ' socr: TSetOfCharRg;',
  7957. ' cr: TCharRg;',
  7958. 'begin',
  7959. ' for c in foo do ;',
  7960. ' for c in s do ;',
  7961. ' for c in char do ;',
  7962. ' for c in a1 do ;',
  7963. ' for c in a2 do ;',
  7964. ' for c in [''1''..''3''] do ;',
  7965. ' for c in TSetOfChar do ;',
  7966. ' for c in TCharRg do ;',
  7967. ' for c in soc do c2:=c;',
  7968. ' for c in TSetOfCharRg do ;',
  7969. ' for c in socr do ;',
  7970. ' for cr in TCharRg do ;',
  7971. ' for cr in TSetOfCharRg do ;',
  7972. ' for cr in socr do ;',
  7973. '']);
  7974. ConvertProgram;
  7975. CheckSource('TestForCharInDo',
  7976. LinesToStr([ // statements
  7977. 'this.Foo = "foo";',
  7978. 'this.c = "";',
  7979. 'this.c2 = "";',
  7980. 'this.s = "";',
  7981. 'this.a1 = [];',
  7982. 'this.a2 = rtl.arraySetLength(null, "", 3);',
  7983. 'this.soc = {};',
  7984. 'this.socr = {};',
  7985. 'this.cr = "a";',
  7986. '']),
  7987. LinesToStr([ // this.$main
  7988. 'for (var $in = $mod.Foo, $l = 0, $end = $in.length - 1; $l <= $end; $l++) $mod.c = $in.charAt($l);',
  7989. 'for (var $in1 = $mod.s, $l1 = 0, $end1 = $in1.length - 1; $l1 <= $end1; $l1++) $mod.c = $in1.charAt($l1);',
  7990. 'for (var $l2 = 0; $l2 <= 65535; $l2++) $mod.c = String.fromCharCode($l2);',
  7991. 'for (var $in2 = $mod.a1, $l3 = 0, $end2 = rtl.length($in2) - 1; $l3 <= $end2; $l3++) $mod.c = $in2[$l3];',
  7992. 'for (var $in3 = $mod.a2, $l4 = 0, $end3 = rtl.length($in3) - 1; $l4 <= $end3; $l4++) $mod.c = $in3[$l4];',
  7993. 'for (var $l5 = 49; $l5 <= 51; $l5++) $mod.c = String.fromCharCode($l5);',
  7994. 'for (var $l6 = 0; $l6 <= 65535; $l6++) $mod.c = String.fromCharCode($l6);',
  7995. 'for (var $l7 = 97; $l7 <= 122; $l7++) $mod.c = String.fromCharCode($l7);',
  7996. 'for (var $l8 in $mod.soc) {',
  7997. ' $mod.c = String.fromCharCode($l8);',
  7998. ' $mod.c2 = $mod.c;',
  7999. '};',
  8000. 'for (var $l9 = 97; $l9 <= 122; $l9++) $mod.c = String.fromCharCode($l9);',
  8001. 'for (var $l10 in $mod.socr) $mod.c = String.fromCharCode($l10);',
  8002. 'for (var $l11 = 97; $l11 <= 122; $l11++) $mod.cr = String.fromCharCode($l11);',
  8003. 'for (var $l12 = 97; $l12 <= 122; $l12++) $mod.cr = String.fromCharCode($l12);',
  8004. 'for (var $l13 in $mod.socr) $mod.cr = String.fromCharCode($l13);',
  8005. '']));
  8006. end;
  8007. procedure TTestModule.TestProcTwoArgs;
  8008. begin
  8009. StartProgram(false);
  8010. Add('procedure Test(a,b: longint);');
  8011. Add('begin');
  8012. Add('end;');
  8013. Add('begin');
  8014. ConvertProgram;
  8015. CheckSource('TestProcTwoArgs',
  8016. LinesToStr([ // statements
  8017. 'this.Test = function (a,b) {',
  8018. '};'
  8019. ]),
  8020. LinesToStr([ // this.$main
  8021. ''
  8022. ]));
  8023. end;
  8024. procedure TTestModule.TestProc_DefaultValue;
  8025. begin
  8026. StartProgram(false);
  8027. Add('procedure p1(i: longint = 1);');
  8028. Add('begin');
  8029. Add('end;');
  8030. Add('procedure p2(i: longint = 1; c: char = ''a'');');
  8031. Add('begin');
  8032. Add('end;');
  8033. Add('procedure p3(d: double = 1.0; b: boolean = false; s: string = ''abc'');');
  8034. Add('begin');
  8035. Add('end;');
  8036. Add('begin');
  8037. Add(' p1;');
  8038. Add(' p1();');
  8039. Add(' p1(11);');
  8040. Add(' p2;');
  8041. Add(' p2();');
  8042. Add(' p2(12);');
  8043. Add(' p2(13,''b'');');
  8044. Add(' p3();');
  8045. ConvertProgram;
  8046. CheckSource('TestProc_DefaultValue',
  8047. LinesToStr([ // statements
  8048. 'this.p1 = function (i) {',
  8049. '};',
  8050. 'this.p2 = function (i,c) {',
  8051. '};',
  8052. 'this.p3 = function (d,b,s) {',
  8053. '};'
  8054. ]),
  8055. LinesToStr([ // this.$main
  8056. ' $mod.p1(1);',
  8057. ' $mod.p1(1);',
  8058. ' $mod.p1(11);',
  8059. ' $mod.p2(1,"a");',
  8060. ' $mod.p2(1,"a");',
  8061. ' $mod.p2(12,"a");',
  8062. ' $mod.p2(13,"b");',
  8063. ' $mod.p3(1.0,false,"abc");'
  8064. ]));
  8065. end;
  8066. procedure TTestModule.TestFunctionInt;
  8067. begin
  8068. StartProgram(false);
  8069. Add('function MyTest(Bar: longint): longint;');
  8070. Add('begin');
  8071. Add(' Result:=2*bar');
  8072. Add('end;');
  8073. Add('begin');
  8074. ConvertProgram;
  8075. CheckSource('TestFunctionInt',
  8076. LinesToStr([ // statements
  8077. 'this.MyTest = function (Bar) {',
  8078. ' var Result = 0;',
  8079. ' Result = 2*Bar;',
  8080. ' return Result;',
  8081. '};'
  8082. ]),
  8083. LinesToStr([ // this.$main
  8084. ''
  8085. ]));
  8086. end;
  8087. procedure TTestModule.TestFunctionString;
  8088. begin
  8089. StartProgram(false);
  8090. Add('function Test(Bar: string): string;');
  8091. Add('begin');
  8092. Add(' Result:=bar+BAR');
  8093. Add('end;');
  8094. Add('begin');
  8095. ConvertProgram;
  8096. CheckSource('TestFunctionString',
  8097. LinesToStr([ // statements
  8098. 'this.Test = function (Bar) {',
  8099. ' var Result = "";',
  8100. ' Result = Bar+Bar;',
  8101. ' return Result;',
  8102. '};'
  8103. ]),
  8104. LinesToStr([ // this.$main
  8105. ''
  8106. ]));
  8107. end;
  8108. procedure TTestModule.TestIfThen;
  8109. begin
  8110. StartProgram(false);
  8111. Add([
  8112. 'var b: boolean;',
  8113. 'begin',
  8114. ' if b then ;',
  8115. ' if b then else ;']);
  8116. ConvertProgram;
  8117. CheckSource('TestIfThen',
  8118. LinesToStr([ // statements
  8119. 'this.b = false;',
  8120. '']),
  8121. LinesToStr([ // this.$main
  8122. 'if ($mod.b) ;',
  8123. 'if ($mod.b) ;',
  8124. '']));
  8125. end;
  8126. procedure TTestModule.TestForLoop;
  8127. begin
  8128. StartProgram(false);
  8129. Add('var');
  8130. Add(' vI, vJ, vN: longint;');
  8131. Add('begin');
  8132. Add(' VJ:=0;');
  8133. Add(' VN:=3;');
  8134. Add(' for VI:=1 to VN do');
  8135. Add(' begin');
  8136. Add(' VJ:=VJ+VI;');
  8137. Add(' end;');
  8138. ConvertProgram;
  8139. CheckSource('TestForLoop',
  8140. LinesToStr([ // statements
  8141. 'this.vI = 0;',
  8142. 'this.vJ = 0;',
  8143. 'this.vN = 0;'
  8144. ]),
  8145. LinesToStr([ // this.$main
  8146. ' $mod.vJ = 0;',
  8147. ' $mod.vN = 3;',
  8148. ' for (var $l = 1, $end = $mod.vN; $l <= $end; $l++) {',
  8149. ' $mod.vI = $l;',
  8150. ' $mod.vJ = $mod.vJ + $mod.vI;',
  8151. ' };',
  8152. '']));
  8153. end;
  8154. procedure TTestModule.TestForLoopInsideFunction;
  8155. begin
  8156. StartProgram(false);
  8157. Add('function SumNumbers(Count: longint): longint;');
  8158. Add('var');
  8159. Add(' vI, vJ: longint;');
  8160. Add('begin');
  8161. Add(' vj:=0;');
  8162. Add(' for vi:=1 to count do');
  8163. Add(' begin');
  8164. Add(' vj:=vj+vi;');
  8165. Add(' end;');
  8166. Add('end;');
  8167. Add('begin');
  8168. Add(' sumnumbers(3);');
  8169. ConvertProgram;
  8170. CheckSource('TestForLoopInsideFunction',
  8171. LinesToStr([ // statements
  8172. 'this.SumNumbers = function (Count) {',
  8173. ' var Result = 0;',
  8174. ' var vI = 0;',
  8175. ' var vJ = 0;',
  8176. ' vJ = 0;',
  8177. ' for (var $l = 1, $end = Count; $l <= $end; $l++) {',
  8178. ' vI = $l;',
  8179. ' vJ = vJ + vI;',
  8180. ' };',
  8181. ' return Result;',
  8182. '};'
  8183. ]),
  8184. LinesToStr([ // $mod.$main
  8185. ' $mod.SumNumbers(3);'
  8186. ]));
  8187. end;
  8188. procedure TTestModule.TestForLoop_ReadVarAfter;
  8189. begin
  8190. StartProgram(false);
  8191. Add('var');
  8192. Add(' vI: longint;');
  8193. Add('begin');
  8194. Add(' for vi:=1 to 2 do ;');
  8195. Add(' if vi=3 then ;');
  8196. ConvertProgram;
  8197. CheckSource('TestForLoop',
  8198. LinesToStr([ // statements
  8199. 'this.vI = 0;'
  8200. ]),
  8201. LinesToStr([ // this.$main
  8202. ' for ($mod.vI = 1; $mod.vI <= 2; $mod.vI++) ;',
  8203. ' if ($mod.vI===3) ;'
  8204. ]));
  8205. end;
  8206. procedure TTestModule.TestForLoop_Nested;
  8207. begin
  8208. StartProgram(false);
  8209. Add('function SumNumbers(Count: longint): longint;');
  8210. Add('var');
  8211. Add(' vI, vJ, vK: longint;');
  8212. Add('begin');
  8213. Add(' VK:=0;');
  8214. Add(' for VI:=1 to count do');
  8215. Add(' begin');
  8216. Add(' for vj:=1 to vi do');
  8217. Add(' begin');
  8218. Add(' vk:=VK+VI;');
  8219. Add(' end;');
  8220. Add(' end;');
  8221. Add('end;');
  8222. Add('begin');
  8223. Add(' sumnumbers(3);');
  8224. ConvertProgram;
  8225. CheckSource('TestForLoopInFunction',
  8226. LinesToStr([ // statements
  8227. 'this.SumNumbers = function (Count) {',
  8228. ' var Result = 0;',
  8229. ' var vI = 0;',
  8230. ' var vJ = 0;',
  8231. ' var vK = 0;',
  8232. ' vK = 0;',
  8233. ' for (var $l = 1, $end = Count; $l <= $end; $l++) {',
  8234. ' vI = $l;',
  8235. ' for (var $l1 = 1, $end1 = vI; $l1 <= $end1; $l1++) {',
  8236. ' vJ = $l1;',
  8237. ' vK = vK + vI;',
  8238. ' };',
  8239. ' };',
  8240. ' return Result;',
  8241. '};'
  8242. ]),
  8243. LinesToStr([ // $mod.$main
  8244. ' $mod.SumNumbers(3);'
  8245. ]));
  8246. end;
  8247. procedure TTestModule.TestRepeatUntil;
  8248. begin
  8249. StartProgram(false);
  8250. Add('var');
  8251. Add(' vI, vJ, vN: longint;');
  8252. Add('begin');
  8253. Add(' vn:=3;');
  8254. Add(' vj:=0;');
  8255. Add(' VI:=0;');
  8256. Add(' repeat');
  8257. Add(' VI:=vi+1;');
  8258. Add(' vj:=VJ+vI;');
  8259. Add(' until vi>=vn');
  8260. ConvertProgram;
  8261. CheckSource('TestRepeatUntil',
  8262. LinesToStr([ // statements
  8263. 'this.vI = 0;',
  8264. 'this.vJ = 0;',
  8265. 'this.vN = 0;'
  8266. ]),
  8267. LinesToStr([ // $mod.$main
  8268. ' $mod.vN = 3;',
  8269. ' $mod.vJ = 0;',
  8270. ' $mod.vI = 0;',
  8271. ' do{',
  8272. ' $mod.vI = $mod.vI + 1;',
  8273. ' $mod.vJ = $mod.vJ + $mod.vI;',
  8274. ' }while(!($mod.vI>=$mod.vN));'
  8275. ]));
  8276. end;
  8277. procedure TTestModule.TestAsmBlock;
  8278. begin
  8279. StartProgram(false);
  8280. Add([
  8281. 'var',
  8282. ' vI: longint;',
  8283. 'begin',
  8284. ' vi:=1;',
  8285. ' asm',
  8286. ' if (vI===1) {',
  8287. ' vI=2;',
  8288. //' console.log(''end;'');', ToDo
  8289. ' }',
  8290. ' if (vI===2){ vI=3; }',
  8291. ' end;',
  8292. ' VI:=4;']);
  8293. ConvertProgram;
  8294. CheckSource('TestAsmBlock',
  8295. LinesToStr([ // statements
  8296. 'this.vI = 0;'
  8297. ]),
  8298. LinesToStr([ // $mod.$main
  8299. '$mod.vI = 1;',
  8300. 'if (vI===1) {',
  8301. ' vI=2;',
  8302. '}',
  8303. 'if (vI===2){ vI=3; }',
  8304. ';',
  8305. '$mod.vI = 4;'
  8306. ]));
  8307. end;
  8308. procedure TTestModule.TestAsmPas_Impl;
  8309. begin
  8310. StartUnit(false);
  8311. Add('interface');
  8312. Add('const cIntf: longint = 1;');
  8313. Add('var vIntf: longint;');
  8314. Add('implementation');
  8315. Add('const cImpl: longint = 2;');
  8316. Add('var vImpl: longint;');
  8317. Add('procedure DoIt;');
  8318. Add('const cLoc: longint = 3;');
  8319. Add('var vLoc: longint;');
  8320. Add('begin;');
  8321. Add(' asm');
  8322. //Add(' pas(vIntf)=pas(cIntf);');
  8323. //Add(' pas(vImpl)=pas(cImpl);');
  8324. //Add(' pas(vLoc)=pas(cLoc);');
  8325. Add(' end;');
  8326. Add('end;');
  8327. ConvertUnit;
  8328. CheckSource('TestAsmPas_Impl',
  8329. LinesToStr([
  8330. 'var $impl = $mod.$impl;',
  8331. 'this.cIntf = 1;',
  8332. 'this.vIntf = 0;',
  8333. '']),
  8334. '', // this.$init
  8335. LinesToStr([ // implementation
  8336. '$impl.cImpl = 2;',
  8337. '$impl.vImpl = 0;',
  8338. 'var cLoc = 3;',
  8339. '$impl.DoIt = function () {',
  8340. ' var vLoc = 0;',
  8341. '};',
  8342. '']) );
  8343. end;
  8344. procedure TTestModule.TestTryFinally;
  8345. begin
  8346. StartProgram(false);
  8347. Add('var i: longint;');
  8348. Add('begin');
  8349. Add(' try');
  8350. Add(' i:=0; i:=2 div i;');
  8351. Add(' finally');
  8352. Add(' i:=3');
  8353. Add(' end;');
  8354. ConvertProgram;
  8355. CheckSource('TestTryFinally',
  8356. LinesToStr([ // statements
  8357. 'this.i = 0;'
  8358. ]),
  8359. LinesToStr([ // $mod.$main
  8360. 'try {',
  8361. ' $mod.i = 0;',
  8362. ' $mod.i = rtl.trunc(2 / $mod.i);',
  8363. '} finally {',
  8364. ' $mod.i = 3;',
  8365. '};'
  8366. ]));
  8367. end;
  8368. procedure TTestModule.TestTryExcept;
  8369. begin
  8370. StartProgram(false);
  8371. Add([
  8372. 'type',
  8373. ' TObject = class end;',
  8374. ' Exception = class Msg: string; end;',
  8375. ' EInvalidCast = class(Exception) end;',
  8376. 'var vI: longint;',
  8377. 'begin',
  8378. ' try',
  8379. ' vi:=1;',
  8380. ' except',
  8381. ' vi:=2',
  8382. ' end;',
  8383. ' try',
  8384. ' vi:=3;',
  8385. ' except',
  8386. ' raise;',
  8387. ' end;',
  8388. ' try',
  8389. ' VI:=4;',
  8390. ' except',
  8391. ' on einvalidcast do',
  8392. ' raise;',
  8393. ' on E: exception do',
  8394. ' if e.msg='''' then',
  8395. ' raise e;',
  8396. ' else',
  8397. ' vi:=5',
  8398. ' end;',
  8399. ' try',
  8400. ' VI:=6;',
  8401. ' except',
  8402. ' on einvalidcast do ;',
  8403. ' end;',
  8404. '']);
  8405. ConvertProgram;
  8406. CheckSource('TestTryExcept',
  8407. LinesToStr([ // statements
  8408. 'rtl.createClass(this, "TObject", null, function () {',
  8409. ' this.$init = function () {',
  8410. ' };',
  8411. ' this.$final = function () {',
  8412. ' };',
  8413. '});',
  8414. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  8415. ' this.$init = function () {',
  8416. ' $mod.TObject.$init.call(this);',
  8417. ' this.Msg = "";',
  8418. ' };',
  8419. '});',
  8420. 'rtl.createClass(this, "EInvalidCast", this.Exception, function () {',
  8421. '});',
  8422. 'this.vI = 0;'
  8423. ]),
  8424. LinesToStr([ // $mod.$main
  8425. 'try {',
  8426. ' $mod.vI = 1;',
  8427. '} catch ($e) {',
  8428. ' $mod.vI = 2;',
  8429. '};',
  8430. 'try {',
  8431. ' $mod.vI = 3;',
  8432. '} catch ($e) {',
  8433. ' throw $e;',
  8434. '};',
  8435. 'try {',
  8436. ' $mod.vI = 4;',
  8437. '} catch ($e) {',
  8438. ' if ($mod.EInvalidCast.isPrototypeOf($e)){',
  8439. ' throw $e',
  8440. ' } else if ($mod.Exception.isPrototypeOf($e)) {',
  8441. ' var E = $e;',
  8442. ' if (E.Msg === "") throw E;',
  8443. ' } else {',
  8444. ' $mod.vI = 5;',
  8445. ' }',
  8446. '};',
  8447. 'try {',
  8448. ' $mod.vI = 6;',
  8449. '} catch ($e) {',
  8450. ' if ($mod.EInvalidCast.isPrototypeOf($e)){' ,
  8451. ' } else throw $e',
  8452. '};',
  8453. '']));
  8454. end;
  8455. procedure TTestModule.TestTryExcept_ReservedWords;
  8456. begin
  8457. StartProgram(false);
  8458. Add([
  8459. 'type',
  8460. ' TObject = class end;',
  8461. ' Exception = class',
  8462. ' Symbol: string;',
  8463. ' end;',
  8464. 'var &try: longint;',
  8465. 'begin',
  8466. ' try',
  8467. ' &try:=4;',
  8468. ' except',
  8469. ' on Error: exception do',
  8470. ' if errOR.symBol='''' then',
  8471. ' raise ERRor;',
  8472. ' end;',
  8473. '']);
  8474. ConvertProgram;
  8475. CheckSource('TestTryExcept_ReservedWords',
  8476. LinesToStr([ // statements
  8477. 'rtl.createClass(this, "TObject", null, function () {',
  8478. ' this.$init = function () {',
  8479. ' };',
  8480. ' this.$final = function () {',
  8481. ' };',
  8482. '});',
  8483. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  8484. ' this.$init = function () {',
  8485. ' $mod.TObject.$init.call(this);',
  8486. ' this.Symbol = "";',
  8487. ' };',
  8488. '});',
  8489. 'this.Try = 0;',
  8490. '']),
  8491. LinesToStr([ // $mod.$main
  8492. 'try {',
  8493. ' $mod.Try = 4;',
  8494. '} catch ($e) {',
  8495. ' if ($mod.Exception.isPrototypeOf($e)) {',
  8496. ' var error = $e;',
  8497. ' if (error.Symbol === "") throw error;',
  8498. ' } else throw $e',
  8499. '};',
  8500. '']));
  8501. end;
  8502. procedure TTestModule.TestIfThenRaiseElse;
  8503. begin
  8504. StartProgram(false);
  8505. Add([
  8506. 'type',
  8507. ' TObject = class',
  8508. ' constructor Create;',
  8509. ' end;',
  8510. 'constructor TObject.Create;',
  8511. 'begin',
  8512. 'end;',
  8513. 'var b: boolean;',
  8514. 'begin',
  8515. ' if b then',
  8516. ' raise TObject.Create',
  8517. ' else',
  8518. ' b:=false;',
  8519. '']);
  8520. ConvertProgram;
  8521. CheckSource('TestIfThenRaiseElse',
  8522. LinesToStr([ // statements
  8523. 'rtl.createClass(this, "TObject", null, function () {',
  8524. ' this.$init = function () {',
  8525. ' };',
  8526. ' this.$final = function () {',
  8527. ' };',
  8528. ' this.Create = function () {',
  8529. ' return this;',
  8530. ' };',
  8531. '});',
  8532. 'this.b = false;',
  8533. '']),
  8534. LinesToStr([ // $mod.$main
  8535. 'if ($mod.b) {',
  8536. ' throw $mod.TObject.$create("Create")}',
  8537. ' else $mod.b = false;',
  8538. '']));
  8539. end;
  8540. procedure TTestModule.TestCaseOf;
  8541. begin
  8542. StartProgram(false);
  8543. Add([
  8544. 'const e: longint; external name ''$e'';',
  8545. 'var vI: longint;',
  8546. 'begin',
  8547. ' case vi of',
  8548. ' 1: ;',
  8549. ' 2: vi:=3;',
  8550. ' e: ;',
  8551. ' else',
  8552. ' VI:=4',
  8553. ' end;']);
  8554. ConvertProgram;
  8555. CheckSource('TestCaseOf',
  8556. LinesToStr([ // statements
  8557. 'this.vI = 0;'
  8558. ]),
  8559. LinesToStr([ // $mod.$main
  8560. 'var $tmp = $mod.vI;',
  8561. 'if ($tmp === 1) {}',
  8562. 'else if ($tmp === 2) {',
  8563. ' $mod.vI = 3}',
  8564. ' else if ($tmp === $e) {}',
  8565. 'else {',
  8566. ' $mod.vI = 4;',
  8567. '};'
  8568. ]));
  8569. end;
  8570. procedure TTestModule.TestCaseOf_UseSwitch;
  8571. begin
  8572. StartProgram(false);
  8573. Converter.UseSwitchStatement:=true;
  8574. Add('var Vi: longint;');
  8575. Add('begin');
  8576. Add(' case vi of');
  8577. Add(' 1: ;');
  8578. Add(' 2: VI:=3;');
  8579. Add(' else');
  8580. Add(' vi:=4');
  8581. Add(' end;');
  8582. ConvertProgram;
  8583. CheckSource('TestCaseOf_UseSwitch',
  8584. LinesToStr([ // statements
  8585. 'this.Vi = 0;'
  8586. ]),
  8587. LinesToStr([ // $mod.$main
  8588. 'switch ($mod.Vi) {',
  8589. 'case 1:',
  8590. ' break;',
  8591. 'case 2:',
  8592. ' $mod.Vi = 3;',
  8593. ' break;',
  8594. 'default:',
  8595. ' $mod.Vi = 4;',
  8596. '};'
  8597. ]));
  8598. end;
  8599. procedure TTestModule.TestCaseOfNoElse;
  8600. begin
  8601. StartProgram(false);
  8602. Add('var Vi: longint;');
  8603. Add('begin');
  8604. Add(' case vi of');
  8605. Add(' 1: begin vi:=2; VI:=3; end;');
  8606. Add(' end;');
  8607. ConvertProgram;
  8608. CheckSource('TestCaseOfNoElse',
  8609. LinesToStr([ // statements
  8610. 'this.Vi = 0;'
  8611. ]),
  8612. LinesToStr([ // $mod.$main
  8613. 'var $tmp = $mod.Vi;',
  8614. 'if ($tmp === 1) {',
  8615. ' $mod.Vi = 2;',
  8616. ' $mod.Vi = 3;',
  8617. '};'
  8618. ]));
  8619. end;
  8620. procedure TTestModule.TestCaseOfNoElse_UseSwitch;
  8621. begin
  8622. StartProgram(false);
  8623. Converter.UseSwitchStatement:=true;
  8624. Add('var vI: longint;');
  8625. Add('begin');
  8626. Add(' case vi of');
  8627. Add(' 1: begin VI:=2; vi:=3; end;');
  8628. Add(' end;');
  8629. ConvertProgram;
  8630. CheckSource('TestCaseOfNoElse_UseSwitch',
  8631. LinesToStr([ // statements
  8632. 'this.vI = 0;'
  8633. ]),
  8634. LinesToStr([ // $mod.$main
  8635. 'switch ($mod.vI) {',
  8636. 'case 1:',
  8637. ' $mod.vI = 2;',
  8638. ' $mod.vI = 3;',
  8639. ' break;',
  8640. '};'
  8641. ]));
  8642. end;
  8643. procedure TTestModule.TestCaseOfRange;
  8644. begin
  8645. StartProgram(false);
  8646. Add('var vI: longint;');
  8647. Add('begin');
  8648. Add(' case vi of');
  8649. Add(' 1..3: vi:=14;');
  8650. Add(' 4,5: vi:=16;');
  8651. Add(' 6..7,9..10: ;');
  8652. Add(' else ;');
  8653. Add(' end;');
  8654. ConvertProgram;
  8655. CheckSource('TestCaseOfRange',
  8656. LinesToStr([ // statements
  8657. 'this.vI = 0;'
  8658. ]),
  8659. LinesToStr([ // $mod.$main
  8660. 'var $tmp = $mod.vI;',
  8661. 'if (($tmp >= 1) && ($tmp <= 3)){',
  8662. ' $mod.vI = 14',
  8663. '} else if (($tmp === 4) || ($tmp === 5)){',
  8664. ' $mod.vI = 16',
  8665. '} else if ((($tmp >= 6) && ($tmp <= 7)) || (($tmp >= 9) && ($tmp <= 10))) ;'
  8666. ]));
  8667. end;
  8668. procedure TTestModule.TestCaseOfString;
  8669. begin
  8670. StartProgram(false);
  8671. Add([
  8672. 'var s,h: string;',
  8673. 'begin',
  8674. ' case s of',
  8675. ' ''foo'': s:=h;',
  8676. ' ''a''..''z'': h:=s;',
  8677. ' ''ў'', ''ё'': ;',
  8678. ' ''Б''..''Я'': ;',
  8679. ' end;',
  8680. '']);
  8681. ConvertProgram;
  8682. CheckSource('TestCaseOfString',
  8683. LinesToStr([ // statements
  8684. 'this.s = "";',
  8685. 'this.h = "";',
  8686. '']),
  8687. LinesToStr([ // $mod.$main
  8688. 'var $tmp = $mod.s;',
  8689. 'if ($tmp === "foo") {',
  8690. ' $mod.s = $mod.h}',
  8691. ' else if (($tmp.length === 1) && ($tmp >= "a") && ($tmp <= "z")) {',
  8692. ' $mod.h = $mod.s}',
  8693. ' else if (($tmp === "ў") || ($tmp === "ё")) {}',
  8694. ' else if (($tmp.length === 1) && ($tmp >= "Б") && ($tmp <= "Я")) ;',
  8695. '']));
  8696. end;
  8697. procedure TTestModule.TestCaseOfChar;
  8698. begin
  8699. StartProgram(false);
  8700. Add([
  8701. 'var s,h: char;',
  8702. 'begin',
  8703. ' case s of',
  8704. ' ''a''..''z'': h:=s;',
  8705. ' ''ä'': ;',
  8706. ' ''ў'', ''ё'': ;',
  8707. ' ''Б''..''Я'': ;',
  8708. ' end;',
  8709. '']);
  8710. ConvertProgram;
  8711. CheckSource('TestCaseOfString',
  8712. LinesToStr([ // statements
  8713. 'this.s = "";',
  8714. 'this.h = "";',
  8715. '']),
  8716. LinesToStr([ // $mod.$main
  8717. 'var $tmp = $mod.s;',
  8718. 'if (($tmp >= "a") && ($tmp <= "z")) {',
  8719. ' $mod.h = $mod.s}',
  8720. ' else if ($tmp === "ä") {}',
  8721. ' else if (($tmp === "ў") || ($tmp === "ё")) {}',
  8722. ' else if (($tmp >= "Б") && ($tmp <= "Я")) ;',
  8723. '']));
  8724. end;
  8725. procedure TTestModule.TestCaseOfExternalClassConst;
  8726. begin
  8727. StartProgram(false);
  8728. Add([
  8729. '{$modeswitch externalclass}',
  8730. 'type',
  8731. ' TBird = class external name ''Bird''',
  8732. ' const e: longint;',
  8733. ' end;',
  8734. 'var vI: longint;',
  8735. 'begin',
  8736. ' case vi of',
  8737. ' 1: vi:=3;',
  8738. ' TBird.e: ;',
  8739. ' end;']);
  8740. ConvertProgram;
  8741. CheckSource('TestCaseOfExternalClassConst',
  8742. LinesToStr([ // statements
  8743. 'this.vI = 0;'
  8744. ]),
  8745. LinesToStr([ // $mod.$main
  8746. 'var $tmp = $mod.vI;',
  8747. 'if ($tmp === 1) {',
  8748. ' $mod.vI = 3}',
  8749. ' else if ($tmp === Bird.e) ;'
  8750. ]));
  8751. end;
  8752. procedure TTestModule.TestDebugger;
  8753. begin
  8754. StartProgram(false);
  8755. Add([
  8756. 'procedure DoIt;',
  8757. 'begin',
  8758. ' deBugger;',
  8759. ' DeBugger();',
  8760. 'end;',
  8761. 'begin',
  8762. ' Debugger;']);
  8763. ConvertProgram;
  8764. CheckSource('TestDebugger',
  8765. LinesToStr([ // statements
  8766. 'this.DoIt = function () {',
  8767. ' debugger;',
  8768. ' debugger;',
  8769. '};',
  8770. '']),
  8771. LinesToStr([ // $mod.$main
  8772. 'debugger;',
  8773. '']));
  8774. end;
  8775. procedure TTestModule.TestArray_Dynamic;
  8776. begin
  8777. StartProgram(false);
  8778. Add([
  8779. 'type',
  8780. ' TArrayInt = array of longint;',
  8781. 'var',
  8782. ' Arr: TArrayInt;',
  8783. ' i: longint;',
  8784. ' b: boolean;',
  8785. 'begin',
  8786. ' SetLength(arr,3);',
  8787. ' arr[0]:=4;',
  8788. ' arr[1]:=length(arr)+arr[0];',
  8789. ' arr[i]:=5;',
  8790. ' arr[arr[i]]:=arr[6];',
  8791. ' i:=low(arr);',
  8792. ' i:=high(arr);',
  8793. ' b:=Assigned(arr);',
  8794. ' Arr:=default(TArrayInt);']);
  8795. ConvertProgram;
  8796. CheckSource('TestArray_Dynamic',
  8797. LinesToStr([ // statements
  8798. 'this.Arr = [];',
  8799. 'this.i = 0;',
  8800. 'this.b = false;'
  8801. ]),
  8802. LinesToStr([ // $mod.$main
  8803. '$mod.Arr = rtl.arraySetLength($mod.Arr,0,3);',
  8804. '$mod.Arr[0] = 4;',
  8805. '$mod.Arr[1] = rtl.length($mod.Arr) + $mod.Arr[0];',
  8806. '$mod.Arr[$mod.i] = 5;',
  8807. '$mod.Arr[$mod.Arr[$mod.i]] = $mod.Arr[6];',
  8808. '$mod.i = 0;',
  8809. '$mod.i = rtl.length($mod.Arr) - 1;',
  8810. '$mod.b = rtl.length($mod.Arr) > 0;',
  8811. '$mod.Arr = [];',
  8812. '']));
  8813. end;
  8814. procedure TTestModule.TestArray_Dynamic_Nil;
  8815. begin
  8816. StartProgram(false);
  8817. Add('type');
  8818. Add(' TArrayInt = array of longint;');
  8819. Add('var');
  8820. Add(' Arr: TArrayInt;');
  8821. Add('procedure DoIt(const i: TArrayInt; j: TArrayInt); begin end;');
  8822. Add('begin');
  8823. Add(' arr:=nil;');
  8824. Add(' if arr=nil then;');
  8825. Add(' if nil=arr then;');
  8826. Add(' if arr<>nil then;');
  8827. Add(' if nil<>arr then;');
  8828. Add(' DoIt(nil,nil);');
  8829. ConvertProgram;
  8830. CheckSource('TestArray_Dynamic',
  8831. LinesToStr([ // statements
  8832. 'this.Arr = [];',
  8833. 'this.DoIt = function(i,j){',
  8834. '};'
  8835. ]),
  8836. LinesToStr([ // $mod.$main
  8837. '$mod.Arr = [];',
  8838. 'if (rtl.length($mod.Arr) === 0) ;',
  8839. 'if (rtl.length($mod.Arr) === 0) ;',
  8840. 'if (rtl.length($mod.Arr) > 0) ;',
  8841. 'if (rtl.length($mod.Arr) > 0) ;',
  8842. '$mod.DoIt([],[]);',
  8843. '']));
  8844. end;
  8845. procedure TTestModule.TestArray_DynMultiDimensional;
  8846. begin
  8847. StartProgram(false);
  8848. Add([
  8849. 'type',
  8850. ' TArrayInt = array of longint;',
  8851. ' TArrayArrayInt = array of TArrayInt;',
  8852. 'var',
  8853. ' Arr: TArrayInt;',
  8854. ' Arr2: TArrayArrayInt;',
  8855. ' i: longint;',
  8856. 'begin',
  8857. ' arr2:=nil;',
  8858. ' if arr2=nil then;',
  8859. ' if nil=arr2 then;',
  8860. ' i:=low(arr2);',
  8861. ' i:=low(arr2[1]);',
  8862. ' i:=high(arr2);',
  8863. ' i:=high(arr2[2]);',
  8864. ' arr2[3]:=arr;',
  8865. ' arr2[4][5]:=i;',
  8866. ' i:=arr2[6][7];',
  8867. ' arr2[8,9]:=i;',
  8868. ' i:=arr2[10,11];',
  8869. ' SetLength(arr2,14);',
  8870. ' SetLength(arr2[15],16);']);
  8871. ConvertProgram;
  8872. CheckSource('TestArray_Dynamic',
  8873. LinesToStr([ // statements
  8874. 'this.Arr = [];',
  8875. 'this.Arr2 = [];',
  8876. 'this.i = 0;'
  8877. ]),
  8878. LinesToStr([ // $mod.$main
  8879. '$mod.Arr2 = [];',
  8880. 'if (rtl.length($mod.Arr2) === 0) ;',
  8881. 'if (rtl.length($mod.Arr2) === 0) ;',
  8882. '$mod.i = 0;',
  8883. '$mod.i = 0;',
  8884. '$mod.i = rtl.length($mod.Arr2) - 1;',
  8885. '$mod.i = rtl.length($mod.Arr2[2]) - 1;',
  8886. '$mod.Arr2[3] = rtl.arrayRef($mod.Arr);',
  8887. '$mod.Arr2[4][5] = $mod.i;',
  8888. '$mod.i = $mod.Arr2[6][7];',
  8889. '$mod.Arr2[8][9] = $mod.i;',
  8890. '$mod.i = $mod.Arr2[10][11];',
  8891. '$mod.Arr2 = rtl.arraySetLength($mod.Arr2, [], 14);',
  8892. '$mod.Arr2[15] = rtl.arraySetLength($mod.Arr2[15], 0, 16);',
  8893. '']));
  8894. end;
  8895. procedure TTestModule.TestArray_DynamicAssign;
  8896. begin
  8897. StartProgram(false);
  8898. Add([
  8899. 'type',
  8900. ' TArrayInt = array of longint;',
  8901. ' TArrayArrayInt = array of TArrayInt;',
  8902. 'procedure Run(a: TArrayInt; const b: TArrayInt; constref c: TArrayInt);',
  8903. 'begin',
  8904. 'end;',
  8905. 'procedure Fly(var a: TArrayInt);',
  8906. 'begin',
  8907. 'end;',
  8908. 'var',
  8909. ' Arr: TArrayInt;',
  8910. ' Arr2: TArrayArrayInt;',
  8911. 'begin',
  8912. ' arr:=nil;',
  8913. ' arr2:=nil;',
  8914. ' arr2[1]:=nil;',
  8915. ' arr2[2]:=arr;',
  8916. ' Run(arr,arr,arr);',
  8917. ' Fly(arr);',
  8918. ' Run(arr2[4],arr2[5],arr2[6]);',
  8919. ' Fly(arr2[7]);',
  8920. '']);
  8921. ConvertProgram;
  8922. CheckSource('TestArray_DynamicAssign',
  8923. LinesToStr([ // statements
  8924. 'this.Run = function (a, b, c) {',
  8925. '};',
  8926. 'this.Fly = function (a) {',
  8927. '};',
  8928. 'this.Arr = [];',
  8929. 'this.Arr2 = [];',
  8930. '']),
  8931. LinesToStr([ // $mod.$main
  8932. '$mod.Arr = [];',
  8933. '$mod.Arr2 = [];',
  8934. '$mod.Arr2[1] = [];',
  8935. '$mod.Arr2[2] = rtl.arrayRef($mod.Arr);',
  8936. '$mod.Run(rtl.arrayRef($mod.Arr), $mod.Arr, $mod.Arr);',
  8937. '$mod.Fly({',
  8938. ' p: $mod,',
  8939. ' get: function () {',
  8940. ' return this.p.Arr;',
  8941. ' },',
  8942. ' set: function (v) {',
  8943. ' this.p.Arr = v;',
  8944. ' }',
  8945. '});',
  8946. '$mod.Run(rtl.arrayRef($mod.Arr2[4]), $mod.Arr2[5], $mod.Arr2[6]);',
  8947. '$mod.Fly({',
  8948. ' a: 7,',
  8949. ' p: $mod.Arr2,',
  8950. ' get: function () {',
  8951. ' return this.p[this.a];',
  8952. ' },',
  8953. ' set: function (v) {',
  8954. ' this.p[this.a] = v;',
  8955. ' }',
  8956. '});',
  8957. '']));
  8958. end;
  8959. procedure TTestModule.TestArray_StaticInt;
  8960. begin
  8961. StartProgram(false);
  8962. Add('type');
  8963. Add(' TArrayInt = array[2..4] of longint;');
  8964. Add('var');
  8965. Add(' Arr: TArrayInt;');
  8966. Add(' Arr2: TArrayInt = (5,6,7);');
  8967. Add(' i: longint;');
  8968. Add(' b: boolean;');
  8969. Add('begin');
  8970. Add(' arr[2]:=4;');
  8971. Add(' arr[3]:=arr[2]+arr[3];');
  8972. Add(' arr[i]:=5;');
  8973. Add(' arr[arr[i]]:=arr[high(arr)];');
  8974. Add(' i:=low(arr);');
  8975. Add(' i:=high(arr);');
  8976. Add(' b:=arr[2]=arr[3];');
  8977. Add(' arr:=default(TArrayInt);');
  8978. ConvertProgram;
  8979. CheckSource('TestArray_StaticInt',
  8980. LinesToStr([ // statements
  8981. 'this.Arr = rtl.arraySetLength(null,0,3);',
  8982. 'this.Arr2 = [5, 6, 7];',
  8983. 'this.i = 0;',
  8984. 'this.b = false;'
  8985. ]),
  8986. LinesToStr([ // $mod.$main
  8987. '$mod.Arr[0] = 4;',
  8988. '$mod.Arr[1] = $mod.Arr[0] + $mod.Arr[1];',
  8989. '$mod.Arr[$mod.i-2] = 5;',
  8990. '$mod.Arr[$mod.Arr[$mod.i-2]-2] = $mod.Arr[2];',
  8991. '$mod.i = 2;',
  8992. '$mod.i = 4;',
  8993. '$mod.b = $mod.Arr[0] === $mod.Arr[1];',
  8994. '$mod.Arr = rtl.arraySetLength(null,0,3);',
  8995. '']));
  8996. end;
  8997. procedure TTestModule.TestArray_StaticBool;
  8998. begin
  8999. StartProgram(false);
  9000. Add('type');
  9001. Add(' TBools = array[boolean] of boolean;');
  9002. Add(' TBool2 = array[true..true] of boolean;');
  9003. Add('var');
  9004. Add(' Arr: TBools;');
  9005. Add(' Arr2: TBool2;');
  9006. Add(' Arr3: TBools = (true,false);');
  9007. Add(' b: boolean;');
  9008. Add('begin');
  9009. Add(' b:=low(arr);');
  9010. Add(' b:=high(arr);');
  9011. Add(' arr[true]:=false;');
  9012. Add(' arr[false]:=arr[b] or arr[true];');
  9013. Add(' arr[b]:=true;');
  9014. Add(' arr[arr[b]]:=arr[high(arr)];');
  9015. Add(' b:=arr[false]=arr[true];');
  9016. Add(' b:=low(arr2);');
  9017. Add(' b:=high(arr2);');
  9018. Add(' arr2[true]:=true;');
  9019. Add(' arr2[true]:=arr2[true] and arr2[b];');
  9020. Add(' arr2[b]:=false;');
  9021. ConvertProgram;
  9022. CheckSource('TestArray_StaticBool',
  9023. LinesToStr([ // statements
  9024. 'this.Arr = rtl.arraySetLength(null,false,2);',
  9025. 'this.Arr2 = rtl.arraySetLength(null,false,1);',
  9026. 'this.Arr3 = [true, false];',
  9027. 'this.b = false;'
  9028. ]),
  9029. LinesToStr([ // $mod.$main
  9030. '$mod.b = false;',
  9031. '$mod.b = true;',
  9032. '$mod.Arr[1] = false;',
  9033. '$mod.Arr[0] = $mod.Arr[+$mod.b] || $mod.Arr[1];',
  9034. '$mod.Arr[+$mod.b] = true;',
  9035. '$mod.Arr[+$mod.Arr[+$mod.b]] = $mod.Arr[1];',
  9036. '$mod.b = $mod.Arr[0] === $mod.Arr[1];',
  9037. '$mod.b = true;',
  9038. '$mod.b = true;',
  9039. '$mod.Arr2[0] = true;',
  9040. '$mod.Arr2[0] = $mod.Arr2[0] && $mod.Arr2[1-$mod.b];',
  9041. '$mod.Arr2[1-$mod.b] = false;',
  9042. '']));
  9043. end;
  9044. procedure TTestModule.TestArray_StaticChar;
  9045. begin
  9046. StartProgram(false);
  9047. Add([
  9048. 'type',
  9049. ' TChars = array[char] of char;',
  9050. ' TChars2 = array[''a''..''z''] of char;',
  9051. 'var',
  9052. ' Arr: TChars;',
  9053. ' Arr2: TChars2;',
  9054. ' Arr3: array[2..4] of char = (''p'',''a'',''s'');',
  9055. ' Arr4: array[11..13] of char = ''pas'';',
  9056. ' Arr5: array[21..22] of char = ''äö'';',
  9057. ' Arr6: array[31..32] of char = ''ä''+''ö'';',
  9058. ' c: char;',
  9059. ' b: boolean;',
  9060. 'begin',
  9061. ' c:=low(arr);',
  9062. ' c:=high(arr);',
  9063. ' arr[''B'']:=''a'';',
  9064. ' arr[''D'']:=arr[c];',
  9065. ' arr[c]:=arr[''d''];',
  9066. ' arr[arr[c]]:=arr[high(arr)];',
  9067. ' b:=arr[low(arr)]=arr[''e''];',
  9068. ' c:=low(arr2);',
  9069. ' c:=high(arr2);',
  9070. ' arr2[''b'']:=''f'';',
  9071. ' arr2[''a'']:=arr2[c];',
  9072. ' arr2[c]:=arr2[''g''];']);
  9073. ConvertProgram;
  9074. CheckSource('TestArray_StaticChar',
  9075. LinesToStr([ // statements
  9076. 'this.Arr = rtl.arraySetLength(null, "", 65536);',
  9077. 'this.Arr2 = rtl.arraySetLength(null, "", 26);',
  9078. 'this.Arr3 = ["p", "a", "s"];',
  9079. 'this.Arr4 = ["p", "a", "s"];',
  9080. 'this.Arr5 = ["ä", "ö"];',
  9081. 'this.Arr6 = ["ä", "ö"];',
  9082. 'this.c = "";',
  9083. 'this.b = false;',
  9084. '']),
  9085. LinesToStr([ // $mod.$main
  9086. '$mod.c = "\x00";',
  9087. '$mod.c = "\uFFFF";',
  9088. '$mod.Arr[66] = "a";',
  9089. '$mod.Arr[68] = $mod.Arr[$mod.c.charCodeAt()];',
  9090. '$mod.Arr[$mod.c.charCodeAt()] = $mod.Arr[100];',
  9091. '$mod.Arr[$mod.Arr[$mod.c.charCodeAt()].charCodeAt()] = $mod.Arr[65535];',
  9092. '$mod.b = $mod.Arr[0] === $mod.Arr[101];',
  9093. '$mod.c = "a";',
  9094. '$mod.c = "z";',
  9095. '$mod.Arr2[1] = "f";',
  9096. '$mod.Arr2[0] = $mod.Arr2[$mod.c.charCodeAt() - 97];',
  9097. '$mod.Arr2[$mod.c.charCodeAt() - 97] = $mod.Arr2[6];',
  9098. '']));
  9099. end;
  9100. procedure TTestModule.TestArray_StaticMultiDim;
  9101. begin
  9102. StartProgram(false);
  9103. Add([
  9104. 'type',
  9105. ' TArrayInt = array[1..3] of longint;',
  9106. ' TArrayArrayInt = array[5..6] of TArrayInt;',
  9107. 'var',
  9108. ' Arr: TArrayInt;',
  9109. ' Arr2: TArrayArrayInt;',
  9110. ' Arr3: array[boolean] of TArrayInt = ((11,12,13),(21,22,23));',
  9111. ' i: longint;',
  9112. 'begin',
  9113. ' i:=low(arr);',
  9114. ' i:=low(arr2);',
  9115. ' i:=low(arr2[5]);',
  9116. ' i:=high(arr);',
  9117. ' i:=high(arr2);',
  9118. ' i:=high(arr2[6]);',
  9119. ' arr2[5]:=arr;',
  9120. ' arr2[6][2]:=i;',
  9121. ' i:=arr2[6][3];',
  9122. ' arr2[6,3]:=i;',
  9123. ' i:=arr2[5,2];',
  9124. ' arr2:=arr2;',// clone multi dim static array
  9125. ' arr3:=arr3;',// clone anonymous multi dim static array
  9126. '']);
  9127. ConvertProgram;
  9128. CheckSource('TestArray_StaticMultiDim',
  9129. LinesToStr([ // statements
  9130. 'this.TArrayArrayInt$clone = function (a) {',
  9131. ' var r = [];',
  9132. ' for (var i = 0; i < 2; i++) r.push(a[i].slice(0));',
  9133. ' return r;',
  9134. '};',
  9135. 'this.Arr = rtl.arraySetLength(null, 0, 3);',
  9136. 'this.Arr2 = rtl.arraySetLength(null, 0, 2, 3);',
  9137. 'this.Arr3$a$clone = function (a) {',
  9138. ' var r = [];',
  9139. ' for (var i = 0; i < 2; i++) r.push(a[i].slice(0));',
  9140. ' return r;',
  9141. '};',
  9142. 'this.Arr3 = [[11, 12, 13], [21, 22, 23]];',
  9143. 'this.i = 0;'
  9144. ]),
  9145. LinesToStr([ // $mod.$main
  9146. '$mod.i = 1;',
  9147. '$mod.i = 5;',
  9148. '$mod.i = 1;',
  9149. '$mod.i = 3;',
  9150. '$mod.i = 6;',
  9151. '$mod.i = 3;',
  9152. '$mod.Arr2[0] = $mod.Arr.slice(0);',
  9153. '$mod.Arr2[1][1] = $mod.i;',
  9154. '$mod.i = $mod.Arr2[1][2];',
  9155. '$mod.Arr2[1][2] = $mod.i;',
  9156. '$mod.i = $mod.Arr2[0][1];',
  9157. '$mod.Arr2 = $mod.TArrayArrayInt$clone($mod.Arr2);',
  9158. '$mod.Arr3 = $mod.Arr3$a$clone($mod.Arr3);',
  9159. '']));
  9160. end;
  9161. procedure TTestModule.TestArray_StaticInFunction;
  9162. begin
  9163. StartProgram(false);
  9164. Add([
  9165. 'const TArrayInt = 3;',
  9166. 'const TArrayArrayInt = 4;',
  9167. 'procedure DoIt;',
  9168. 'type',
  9169. ' TArrayInt = array[1..3] of longint;',
  9170. ' TArrayArrayInt = array[5..6] of TArrayInt;',
  9171. 'var',
  9172. ' Arr: TArrayInt;',
  9173. ' Arr2: TArrayArrayInt;',
  9174. ' Arr3: array[boolean] of TArrayInt = ((11,12,13),(21,22,23));',
  9175. ' i: longint;',
  9176. 'begin',
  9177. ' arr2[5]:=arr;',
  9178. ' arr2:=arr2;',// clone multi dim static array
  9179. ' arr3:=arr3;',// clone multi dim anonymous static array
  9180. 'end;',
  9181. 'begin',
  9182. '']);
  9183. ConvertProgram;
  9184. CheckSource('TestArray_StaticInFunction',
  9185. LinesToStr([ // statements
  9186. 'this.TArrayInt = 3;',
  9187. 'this.TArrayArrayInt = 4;',
  9188. 'var TArrayArrayInt$1$clone = function (a) {',
  9189. ' var r = [];',
  9190. ' for (var i = 0; i < 2; i++) r.push(a[i].slice(0));',
  9191. ' return r;',
  9192. '};',
  9193. 'var Arr3$a$clone = function (a) {',
  9194. ' var r = [];',
  9195. ' for (var i = 0; i < 2; i++) r.push(a[i].slice(0));',
  9196. ' return r;',
  9197. '};',
  9198. 'this.DoIt = function () {',
  9199. ' var Arr = rtl.arraySetLength(null, 0, 3);',
  9200. ' var Arr2 = rtl.arraySetLength(null, 0, 2, 3);',
  9201. ' var Arr3 = [[11, 12, 13], [21, 22, 23]];',
  9202. ' var i = 0;',
  9203. ' Arr2[0] = Arr.slice(0);',
  9204. ' Arr2 = TArrayArrayInt$1$clone(Arr2);',
  9205. ' Arr3 = Arr3$a$clone(Arr3);',
  9206. '};',
  9207. '']),
  9208. LinesToStr([ // $mod.$main
  9209. '']));
  9210. end;
  9211. procedure TTestModule.TestArray_StaticMultiDimEqualNotImplemented;
  9212. begin
  9213. StartProgram(false);
  9214. Add([
  9215. 'type',
  9216. ' TArrayInt = array[1..3,1..2] of longint;',
  9217. 'var',
  9218. ' a,b: TArrayInt;',
  9219. 'begin',
  9220. ' if a=b then ;',
  9221. '']);
  9222. SetExpectedPasResolverError('compare static array is not supported',
  9223. nXIsNotSupported);
  9224. ConvertProgram;
  9225. end;
  9226. procedure TTestModule.TestArrayOfRecord;
  9227. begin
  9228. StartProgram(false);
  9229. Add([
  9230. 'type',
  9231. ' TRec = record',
  9232. ' Int: longint;',
  9233. ' end;',
  9234. ' TArrayRec = array of TRec;',
  9235. 'procedure DoIt(vd: TRec; const vc: TRec; var vv: TRec);',
  9236. 'begin',
  9237. 'end;',
  9238. 'var',
  9239. ' Arr: TArrayRec;',
  9240. ' r: TRec;',
  9241. ' i: longint;',
  9242. 'begin',
  9243. ' SetLength(arr,3);',
  9244. ' arr[0].int:=4;',
  9245. ' arr[1].int:=length(arr)+arr[2].int;',
  9246. ' arr[arr[i].int].int:=arr[5].int;',
  9247. ' arr[7]:=r;',
  9248. ' r:=arr[8];',
  9249. ' i:=low(arr);',
  9250. ' i:=high(arr);',
  9251. ' DoIt(Arr[9],Arr[10],Arr[11]);']);
  9252. ConvertProgram;
  9253. CheckSource('TestArrayOfRecord',
  9254. LinesToStr([ // statements
  9255. 'rtl.recNewT(this, "TRec", function () {',
  9256. ' this.Int = 0;',
  9257. ' this.$eq = function (b) {',
  9258. ' return this.Int === b.Int;',
  9259. ' };',
  9260. ' this.$assign = function (s) {',
  9261. ' this.Int = s.Int;',
  9262. ' return this;',
  9263. ' };',
  9264. '});',
  9265. 'this.DoIt = function (vd, vc, vv) {',
  9266. '};',
  9267. 'this.Arr = [];',
  9268. 'this.r = this.TRec.$new();',
  9269. 'this.i = 0;'
  9270. ]),
  9271. LinesToStr([ // $mod.$main
  9272. '$mod.Arr = rtl.arraySetLength($mod.Arr,$mod.TRec,3);',
  9273. '$mod.Arr[0].Int = 4;',
  9274. '$mod.Arr[1].Int = rtl.length($mod.Arr)+$mod.Arr[2].Int;',
  9275. '$mod.Arr[$mod.Arr[$mod.i].Int].Int = $mod.Arr[5].Int;',
  9276. '$mod.Arr[7].$assign($mod.r);',
  9277. '$mod.r.$assign($mod.Arr[8]);',
  9278. '$mod.i = 0;',
  9279. '$mod.i = rtl.length($mod.Arr)-1;',
  9280. '$mod.DoIt($mod.TRec.$clone($mod.Arr[9]), $mod.Arr[10], $mod.Arr[11]);',
  9281. '']));
  9282. end;
  9283. procedure TTestModule.TestArray_StaticRecord;
  9284. begin
  9285. StartProgram(false);
  9286. Add([
  9287. 'type',
  9288. ' TRec = record',
  9289. ' Int: longint;',
  9290. ' end;',
  9291. ' TArrayRec = array[1..2] of TRec;',
  9292. 'var',
  9293. ' Arr: TArrayRec;',
  9294. 'begin',
  9295. ' arr[1].int:=length(arr)+low(arr)+high(arr);',
  9296. '']);
  9297. ConvertProgram;
  9298. CheckSource('TestArray_StaticRecord',
  9299. LinesToStr([ // statements
  9300. 'rtl.recNewT(this, "TRec", function () {',
  9301. ' this.Int = 0;',
  9302. ' this.$eq = function (b) {',
  9303. ' return this.Int === b.Int;',
  9304. ' };',
  9305. ' this.$assign = function (s) {',
  9306. ' this.Int = s.Int;',
  9307. ' return this;',
  9308. ' };',
  9309. '});',
  9310. 'this.TArrayRec$clone = function (a) {',
  9311. ' var r = [];',
  9312. ' for (var i = 0; i < 2; i++) r.push($mod.TRec.$clone(a[i]));',
  9313. ' return r;',
  9314. '};',
  9315. 'this.Arr = rtl.arraySetLength(null, this.TRec, 2);',
  9316. '']),
  9317. LinesToStr([ // $mod.$main
  9318. '$mod.Arr[0].Int = 2 + 1 + 2;']));
  9319. end;
  9320. procedure TTestModule.TestArrayOfSet;
  9321. begin
  9322. StartProgram(false);
  9323. Add([
  9324. 'type',
  9325. ' TFlag = (big,small);',
  9326. ' TSetOfFlag = set of tflag;',
  9327. ' TArrayFlag = array of TSetOfFlag;',
  9328. 'procedure DoIt(const a: Tarrayflag);',
  9329. 'begin',
  9330. 'end;',
  9331. 'var',
  9332. ' f: TFlag;',
  9333. ' s: TSetOfFlag;',
  9334. ' Arr: TArrayFlag;',
  9335. ' i: longint;',
  9336. 'begin',
  9337. ' SetLength(arr,3);',
  9338. ' arr[0]:=s;',
  9339. ' arr[1]:=[big];',
  9340. ' arr[2]:=[big]+s;',
  9341. ' arr[3]:=s+[big];',
  9342. ' arr[4]:=arr[5];',
  9343. ' s:=arr[6];',
  9344. ' i:=low(arr);',
  9345. ' i:=high(arr);',
  9346. ' DoIt(arr);',
  9347. ' DoIt([s]);',
  9348. ' DoIt([[],s]);',
  9349. ' DoIt([s,[]]);',
  9350. '']);
  9351. ConvertProgram;
  9352. CheckSource('TestArrayOfSet',
  9353. LinesToStr([ // statements
  9354. 'this.TFlag = {',
  9355. ' "0": "big",',
  9356. ' big: 0,',
  9357. ' "1": "small",',
  9358. ' small: 1',
  9359. '};',
  9360. 'this.DoIt = function (a) {',
  9361. '};',
  9362. 'this.f = 0;',
  9363. 'this.s = {};',
  9364. 'this.Arr = [];',
  9365. 'this.i = 0;',
  9366. '']),
  9367. LinesToStr([ // $mod.$main
  9368. '$mod.Arr = rtl.arraySetLength($mod.Arr, {}, 3);',
  9369. '$mod.Arr[0] = rtl.refSet($mod.s);',
  9370. '$mod.Arr[1] = rtl.createSet($mod.TFlag.big);',
  9371. '$mod.Arr[2] = rtl.unionSet(rtl.createSet($mod.TFlag.big), $mod.s);',
  9372. '$mod.Arr[3] = rtl.unionSet($mod.s, rtl.createSet($mod.TFlag.big));',
  9373. '$mod.Arr[4] = rtl.refSet($mod.Arr[5]);',
  9374. '$mod.s = rtl.refSet($mod.Arr[6]);',
  9375. '$mod.i = 0;',
  9376. '$mod.i = rtl.length($mod.Arr) - 1;',
  9377. '$mod.DoIt($mod.Arr);',
  9378. '$mod.DoIt([rtl.refSet($mod.s)]);',
  9379. '$mod.DoIt([{}, rtl.refSet($mod.s)]);',
  9380. '$mod.DoIt([rtl.refSet($mod.s), {}]);',
  9381. '']));
  9382. end;
  9383. procedure TTestModule.TestArray_DynAsParam;
  9384. begin
  9385. StartProgram(false);
  9386. Add([
  9387. 'type integer = longint;',
  9388. 'type TArrInt = array of integer;',
  9389. 'procedure DoIt(vG: TArrInt; const vH: TArrInt; var vI: TArrInt);',
  9390. 'var vJ: TArrInt;',
  9391. 'begin',
  9392. ' vg:=vg;',
  9393. ' vj:=vh;',
  9394. ' vi:=vi;',
  9395. ' doit(vg,vg,vg);',
  9396. ' doit(vh,vh,vj);',
  9397. ' doit(vi,vi,vi);',
  9398. ' doit(vj,vj,vj);',
  9399. 'end;',
  9400. 'var i: TArrInt;',
  9401. 'begin',
  9402. ' doit(i,i,i);']);
  9403. ConvertProgram;
  9404. CheckSource('TestArray_DynAsParams',
  9405. LinesToStr([ // statements
  9406. 'this.DoIt = function (vG,vH,vI) {',
  9407. ' var vJ = [];',
  9408. ' vG = rtl.arrayRef(vG);',
  9409. ' vJ = rtl.arrayRef(vH);',
  9410. ' vI.set(rtl.arrayRef(vI.get()));',
  9411. ' $mod.DoIt(rtl.arrayRef(vG), vG, {',
  9412. ' get: function () {',
  9413. ' return vG;',
  9414. ' },',
  9415. ' set: function (v) {',
  9416. ' vG = v;',
  9417. ' }',
  9418. ' });',
  9419. ' $mod.DoIt(rtl.arrayRef(vH), vH, {',
  9420. ' get: function () {',
  9421. ' return vJ;',
  9422. ' },',
  9423. ' set: function (v) {',
  9424. ' vJ = v;',
  9425. ' }',
  9426. ' });',
  9427. ' $mod.DoIt(rtl.arrayRef(vI.get()), vI.get(), vI);',
  9428. ' $mod.DoIt(rtl.arrayRef(vJ), vJ, {',
  9429. ' get: function () {',
  9430. ' return vJ;',
  9431. ' },',
  9432. ' set: function (v) {',
  9433. ' vJ = v;',
  9434. ' }',
  9435. ' });',
  9436. '};',
  9437. 'this.i = [];'
  9438. ]),
  9439. LinesToStr([
  9440. '$mod.DoIt(rtl.arrayRef($mod.i),$mod.i,{',
  9441. ' p: $mod,',
  9442. ' get: function () {',
  9443. ' return this.p.i;',
  9444. ' },',
  9445. ' set: function (v) {',
  9446. ' this.p.i = v;',
  9447. ' }',
  9448. '});'
  9449. ]));
  9450. end;
  9451. procedure TTestModule.TestArray_StaticAsParam;
  9452. begin
  9453. StartProgram(false);
  9454. Add([
  9455. 'type integer = longint;',
  9456. 'type TArrInt = array[1..2] of integer;',
  9457. 'procedure DoIt(vG: TArrInt; const vH: TArrInt; var vI: TArrInt);',
  9458. 'var vJ: TArrInt;',
  9459. 'begin',
  9460. ' vg:=vg;',
  9461. ' vj:=vh;',
  9462. ' vi:=vi;',
  9463. ' doit(vg,vg,vg);',
  9464. ' doit(vh,vh,vj);',
  9465. ' doit(vi,vi,vi);',
  9466. ' doit(vj,vj,vj);',
  9467. 'end;',
  9468. 'var i: TArrInt;',
  9469. 'begin',
  9470. ' doit(i,i,i);']);
  9471. ConvertProgram;
  9472. CheckSource('TestArray_StaticAsParams',
  9473. LinesToStr([ // statements
  9474. 'this.DoIt = function (vG,vH,vI) {',
  9475. ' var vJ = rtl.arraySetLength(null, 0, 2);',
  9476. ' vG = vG.slice(0);',
  9477. ' vJ = vH.slice(0);',
  9478. ' vI.set(vI.get().slice(0));',
  9479. ' $mod.DoIt(vG.slice(0), vG, {',
  9480. ' get: function () {',
  9481. ' return vG;',
  9482. ' },',
  9483. ' set: function (v) {',
  9484. ' vG = v;',
  9485. ' }',
  9486. ' });',
  9487. ' $mod.DoIt(vH.slice(0), vH, {',
  9488. ' get: function () {',
  9489. ' return vJ;',
  9490. ' },',
  9491. ' set: function (v) {',
  9492. ' vJ = v;',
  9493. ' }',
  9494. ' });',
  9495. ' $mod.DoIt(vI.get().slice(0), vI.get(), vI);',
  9496. ' $mod.DoIt(vJ.slice(0), vJ, {',
  9497. ' get: function () {',
  9498. ' return vJ;',
  9499. ' },',
  9500. ' set: function (v) {',
  9501. ' vJ = v;',
  9502. ' }',
  9503. ' });',
  9504. '};',
  9505. 'this.i = rtl.arraySetLength(null, 0, 2);'
  9506. ]),
  9507. LinesToStr([
  9508. '$mod.DoIt($mod.i.slice(0),$mod.i,{',
  9509. ' p: $mod,',
  9510. ' get: function () {',
  9511. ' return this.p.i;',
  9512. ' },',
  9513. ' set: function (v) {',
  9514. ' this.p.i = v;',
  9515. ' }',
  9516. '});'
  9517. ]));
  9518. end;
  9519. procedure TTestModule.TestArrayElement_AsParams;
  9520. begin
  9521. StartProgram(false);
  9522. Add('type integer = longint;');
  9523. Add('type TArrayInt = array of integer;');
  9524. Add('procedure DoIt(vG: Integer; const vH: Integer; var vI: Integer);');
  9525. Add('var vJ: tarrayint;');
  9526. Add('begin');
  9527. Add(' vi:=vi;');
  9528. Add(' doit(vi,vi,vi);');
  9529. Add(' doit(vj[1+1],vj[1+2],vj[1+3]);');
  9530. Add('end;');
  9531. Add('var a: TArrayInt;');
  9532. Add('begin');
  9533. Add(' doit(a[1+4],a[1+5],a[1+6]);');
  9534. ConvertProgram;
  9535. CheckSource('TestArrayElement_AsParams',
  9536. LinesToStr([ // statements
  9537. 'this.DoIt = function (vG,vH,vI) {',
  9538. ' var vJ = [];',
  9539. ' vI.set(vI.get());',
  9540. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  9541. ' $mod.DoIt(vJ[1+1], vJ[1+2], {',
  9542. ' a:1+3,',
  9543. ' p:vJ,',
  9544. ' get: function () {',
  9545. ' return this.p[this.a];',
  9546. ' },',
  9547. ' set: function (v) {',
  9548. ' this.p[this.a] = v;',
  9549. ' }',
  9550. ' });',
  9551. '};',
  9552. 'this.a = [];'
  9553. ]),
  9554. LinesToStr([
  9555. '$mod.DoIt($mod.a[1+4],$mod.a[1+5],{',
  9556. ' a: 1+6,',
  9557. ' p: $mod.a,',
  9558. ' get: function () {',
  9559. ' return this.p[this.a];',
  9560. ' },',
  9561. ' set: function (v) {',
  9562. ' this.p[this.a] = v;',
  9563. ' }',
  9564. '});'
  9565. ]));
  9566. end;
  9567. procedure TTestModule.TestArrayElementFromFuncResult_AsParams;
  9568. begin
  9569. StartProgram(false);
  9570. Add('type Integer = longint;');
  9571. Add('type TArrayInt = array of integer;');
  9572. Add('function GetArr(vB: integer = 0): tarrayint;');
  9573. Add('begin');
  9574. Add('end;');
  9575. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  9576. Add('begin');
  9577. Add('end;');
  9578. Add('begin');
  9579. Add(' doit(getarr[1+1],getarr[1+2],getarr[1+3]);');
  9580. Add(' doit(getarr()[2+1],getarr()[2+2],getarr()[2+3]);');
  9581. Add(' doit(getarr(7)[3+1],getarr(8)[3+2],getarr(9)[3+3]);');
  9582. ConvertProgram;
  9583. CheckSource('TestArrayElementFromFuncResult_AsParams',
  9584. LinesToStr([ // statements
  9585. 'this.GetArr = function (vB) {',
  9586. ' var Result = [];',
  9587. ' return Result;',
  9588. '};',
  9589. 'this.DoIt = function (vG,vH,vI) {',
  9590. '};'
  9591. ]),
  9592. LinesToStr([
  9593. '$mod.DoIt($mod.GetArr(0)[1+1],$mod.GetArr(0)[1+2],{',
  9594. ' a: 1+3,',
  9595. ' p: $mod.GetArr(0),',
  9596. ' get: function () {',
  9597. ' return this.p[this.a];',
  9598. ' },',
  9599. ' set: function (v) {',
  9600. ' this.p[this.a] = v;',
  9601. ' }',
  9602. '});',
  9603. '$mod.DoIt($mod.GetArr(0)[2+1],$mod.GetArr(0)[2+2],{',
  9604. ' a: 2+3,',
  9605. ' p: $mod.GetArr(0),',
  9606. ' get: function () {',
  9607. ' return this.p[this.a];',
  9608. ' },',
  9609. ' set: function (v) {',
  9610. ' this.p[this.a] = v;',
  9611. ' }',
  9612. '});',
  9613. '$mod.DoIt($mod.GetArr(7)[3+1],$mod.GetArr(8)[3+2],{',
  9614. ' a: 3+3,',
  9615. ' p: $mod.GetArr(9),',
  9616. ' get: function () {',
  9617. ' return this.p[this.a];',
  9618. ' },',
  9619. ' set: function (v) {',
  9620. ' this.p[this.a] = v;',
  9621. ' }',
  9622. '});',
  9623. '']));
  9624. end;
  9625. procedure TTestModule.TestArrayEnumTypeRange;
  9626. begin
  9627. StartProgram(false);
  9628. Add([
  9629. 'type',
  9630. ' TEnum = (red,blue);',
  9631. ' TEnumArray = array[TEnum] of longint;',
  9632. 'var',
  9633. ' e: TEnum;',
  9634. ' i: longint;',
  9635. ' a: TEnumArray;',
  9636. ' numbers: TEnumArray = (1,2);',
  9637. ' names: array[TEnum] of string = (''red'',''blue'');',
  9638. 'begin',
  9639. ' e:=low(a);',
  9640. ' e:=high(a);',
  9641. ' i:=a[red];',
  9642. ' a[e]:=a[e];']);
  9643. ConvertProgram;
  9644. CheckSource('TestArrayEnumTypeRange',
  9645. LinesToStr([ // statements
  9646. ' this.TEnum = {',
  9647. ' "0": "red",',
  9648. ' red: 0,',
  9649. ' "1": "blue",',
  9650. ' blue: 1',
  9651. '};',
  9652. 'this.e = 0;',
  9653. 'this.i = 0;',
  9654. 'this.a = rtl.arraySetLength(null,0,2);',
  9655. 'this.numbers = [1, 2];',
  9656. 'this.names = ["red", "blue"];',
  9657. '']),
  9658. LinesToStr([ // $mod.$main
  9659. '$mod.e = $mod.TEnum.red;',
  9660. '$mod.e = $mod.TEnum.blue;',
  9661. '$mod.i = $mod.a[$mod.TEnum.red];',
  9662. '$mod.a[$mod.e] = $mod.a[$mod.e];',
  9663. '']));
  9664. end;
  9665. procedure TTestModule.TestArray_SetLengthOutArg;
  9666. begin
  9667. StartProgram(false);
  9668. Add([
  9669. 'type TArrInt = array of longint;',
  9670. 'procedure DoIt(out a: TArrInt);',
  9671. 'begin',
  9672. ' SetLength(a,2);',
  9673. 'end;',
  9674. 'begin',
  9675. '']);
  9676. ConvertProgram;
  9677. CheckSource('TestArray_SetLengthOutArg',
  9678. LinesToStr([ // statements
  9679. 'this.DoIt = function (a) {',
  9680. ' a.set(rtl.arraySetLength(a.get(), 0, 2));',
  9681. '};',
  9682. '']),
  9683. LinesToStr([
  9684. '']));
  9685. end;
  9686. procedure TTestModule.TestArray_SetLengthProperty;
  9687. begin
  9688. StartProgram(false);
  9689. Add('type');
  9690. Add(' TArrInt = array of longint;');
  9691. Add(' TObject = class');
  9692. Add(' function GetColors: TArrInt; external name ''GetColors'';');
  9693. Add(' procedure SetColors(const Value: TArrInt); external name ''SetColors'';');
  9694. Add(' property Colors: TArrInt read GetColors write SetColors;');
  9695. Add(' end;');
  9696. Add('var Obj: TObject;');
  9697. Add('begin');
  9698. Add(' SetLength(Obj.Colors,2);');
  9699. ConvertProgram;
  9700. CheckSource('TestArray_SetLengthProperty',
  9701. LinesToStr([ // statements
  9702. 'rtl.createClass(this, "TObject", null, function () {',
  9703. ' this.$init = function () {',
  9704. ' };',
  9705. ' this.$final = function () {',
  9706. ' };',
  9707. '});',
  9708. 'this.Obj = null;',
  9709. '']),
  9710. LinesToStr([
  9711. '$mod.Obj.SetColors(rtl.arraySetLength($mod.Obj.GetColors(), 0, 2));',
  9712. '']));
  9713. end;
  9714. procedure TTestModule.TestArray_SetLengthMultiDim;
  9715. begin
  9716. StartProgram(false);
  9717. Add([
  9718. 'type',
  9719. ' TArrArrInt = array of array of longint;',
  9720. ' TArrStaInt = array of array[1..2] of longint;',
  9721. 'var',
  9722. ' a: TArrArrInt;',
  9723. ' b: TArrStaInt;',
  9724. 'begin',
  9725. ' SetLength(a,2);',
  9726. ' SetLength(a,3,4);',
  9727. ' SetLength(b,5);',
  9728. '']);
  9729. ConvertProgram;
  9730. CheckSource('TestArray_SetLengthMultiDim',
  9731. LinesToStr([ // statements
  9732. 'this.a = [];',
  9733. 'this.b = [];',
  9734. '']),
  9735. LinesToStr([
  9736. '$mod.a = rtl.arraySetLength($mod.a, [], 2);',
  9737. '$mod.a = rtl.arraySetLength($mod.a, 0, 3, 4);',
  9738. '$mod.b = rtl.arraySetLength($mod.b, 0, 5, "s", 2);',
  9739. '']));
  9740. end;
  9741. procedure TTestModule.TestArray_SetLengthDynOfStatic;
  9742. begin
  9743. StartProgram(false);
  9744. Add([
  9745. 'type',
  9746. ' TStaArr1 = array[1..3] of boolean;',
  9747. //' TStaArr2 = array[5..6] of TStaArr1;',
  9748. ' TDynArr1StaArr1 = array of TStaArr1;',
  9749. //' TDynArr1StaArr2 = array of TStaArr2;',
  9750. ' TDynArr2StaArr1 = array of TDynArr1StaArr1;',
  9751. //' TDynArr2StaArr2 = array of TDynArr1StaArr2;',
  9752. 'var',
  9753. ' DynArr1StaArr1: TDynArr1StaArr1;',
  9754. //' DynArr1StaArr2: TDynArr1StaArr1;',
  9755. ' DynArr2StaArr1: TDynArr2StaArr1;',
  9756. //' DynArr2StaArr2: TDynArr2StaArr2;',
  9757. 'begin',
  9758. ' SetLength(DynArr1StaArr1,11);',
  9759. ' SetLength(DynArr2StaArr1,12);',
  9760. ' SetLength(DynArr2StaArr1[13],14);',
  9761. ' SetLength(DynArr2StaArr1,15,16);',
  9762. //' SetLength(DynArr1StaArr2,21);',
  9763. //' SetLength(DynArr2StaArr2,22);',
  9764. //' SetLength(DynArr2StaArr2[23],24);',
  9765. //' SetLength(DynArr2StaArr2,25,26);',
  9766. '']);
  9767. ConvertProgram;
  9768. CheckSource('TestArray_DynOfStatic',
  9769. LinesToStr([ // statements
  9770. 'this.DynArr1StaArr1 = [];',
  9771. 'this.DynArr2StaArr1 = [];',
  9772. '']),
  9773. LinesToStr([ // $mod.$main
  9774. '$mod.DynArr1StaArr1 = rtl.arraySetLength($mod.DynArr1StaArr1, false, 11, "s", 3);',
  9775. '$mod.DynArr2StaArr1 = rtl.arraySetLength($mod.DynArr2StaArr1, [], 12);',
  9776. '$mod.DynArr2StaArr1[13] = rtl.arraySetLength($mod.DynArr2StaArr1[13], false, 14, "s", 3);',
  9777. '$mod.DynArr2StaArr1 = rtl.arraySetLength(',
  9778. ' $mod.DynArr2StaArr1,',
  9779. ' false,',
  9780. ' 15,',
  9781. ' 16,',
  9782. ' "s",',
  9783. ' 3',
  9784. ');',
  9785. '']));
  9786. end;
  9787. procedure TTestModule.TestArray_OpenArrayOfString;
  9788. begin
  9789. StartProgram(false);
  9790. Add('procedure DoIt(const a: array of String);');
  9791. Add('var');
  9792. Add(' i: longint;');
  9793. Add(' s: string;');
  9794. Add('begin');
  9795. Add(' for i:=low(a) to high(a) do s:=a[length(a)-i-1];');
  9796. Add('end;');
  9797. Add('var s: string;');
  9798. Add('begin');
  9799. Add(' DoIt([]);');
  9800. Add(' DoIt([s,''foo'','''',s+s]);');
  9801. ConvertProgram;
  9802. CheckSource('TestArray_OpenArrayOfString',
  9803. LinesToStr([ // statements
  9804. 'this.DoIt = function (a) {',
  9805. ' var i = 0;',
  9806. ' var s = "";',
  9807. ' for (var $l = 0, $end = rtl.length(a) - 1; $l <= $end; $l++) {',
  9808. ' i = $l;',
  9809. ' s = a[rtl.length(a) - i - 1];',
  9810. ' };',
  9811. '};',
  9812. 'this.s = "";',
  9813. '']),
  9814. LinesToStr([
  9815. '$mod.DoIt([]);',
  9816. '$mod.DoIt([$mod.s, "foo", "", $mod.s + $mod.s]);',
  9817. '']));
  9818. end;
  9819. procedure TTestModule.TestArray_ArrayOfCharAssignString;
  9820. begin
  9821. StartProgram(false);
  9822. Add([
  9823. 'type TArr = array of char;',
  9824. 'var',
  9825. ' c: char;',
  9826. ' s: string;',
  9827. ' a: TArr;',
  9828. 'procedure Run(const a: array of char);',
  9829. 'begin',
  9830. ' Run(c);',
  9831. ' Run(s);',
  9832. 'end;',
  9833. 'begin',
  9834. ' a:=c;',
  9835. ' a:=s;',
  9836. ' a:=#13;',
  9837. ' a:=''Foo'';',
  9838. ' Run(c);',
  9839. ' Run(s);',
  9840. '']);
  9841. ConvertProgram;
  9842. CheckSource('TestArray_ArrayOfCharAssignString',
  9843. LinesToStr([ // statements
  9844. 'this.c = "";',
  9845. 'this.s = "";',
  9846. 'this.a = [];',
  9847. 'this.Run = function (a) {',
  9848. ' $mod.Run($mod.c.split(""));',
  9849. ' $mod.Run($mod.s.split(""));',
  9850. '};',
  9851. '']),
  9852. LinesToStr([
  9853. '$mod.a = $mod.c.split("");',
  9854. '$mod.a = $mod.s.split("");',
  9855. '$mod.a = "\r".split("");',
  9856. '$mod.a = "Foo".split("");',
  9857. '$mod.Run($mod.c.split(""));',
  9858. '$mod.Run($mod.s.split(""));',
  9859. '']));
  9860. end;
  9861. procedure TTestModule.TestArray_ConstRef;
  9862. begin
  9863. StartProgram(false);
  9864. Add([
  9865. 'type TArr = array of word;',
  9866. 'procedure Run(constref a: TArr);',
  9867. 'begin',
  9868. 'end;',
  9869. 'procedure Fly(a: TArr; var b: TArr; out c: TArr; const d: TArr; constref e: TArr);',
  9870. 'var l: TArr;',
  9871. 'begin',
  9872. ' Run(l);',
  9873. ' Run(a);',
  9874. ' Run(b);',
  9875. ' Run(c);',
  9876. ' Run(d);',
  9877. ' Run(e);',
  9878. 'end;',
  9879. 'begin',
  9880. '']);
  9881. ConvertProgram;
  9882. CheckResolverUnexpectedHints();
  9883. CheckSource('TestArray_ConstRef',
  9884. LinesToStr([ // statements
  9885. 'this.Run = function (a) {',
  9886. '};',
  9887. 'this.Fly = function (a, b, c, d, e) {',
  9888. ' var l = [];',
  9889. ' $mod.Run(l);',
  9890. ' $mod.Run(a);',
  9891. ' $mod.Run(b.get());',
  9892. ' $mod.Run(c.get());',
  9893. ' $mod.Run(d);',
  9894. ' $mod.Run(e);',
  9895. '};',
  9896. '']),
  9897. LinesToStr([
  9898. '']));
  9899. end;
  9900. procedure TTestModule.TestArray_Concat;
  9901. begin
  9902. StartProgram(false);
  9903. Add([
  9904. 'type',
  9905. ' integer = longint;',
  9906. ' TFlag = (big,small);',
  9907. ' TFlags = set of TFlag;',
  9908. ' TRec = record',
  9909. ' i: integer;',
  9910. ' end;',
  9911. ' TArrInt = array of integer;',
  9912. ' TArrRec = array of TRec;',
  9913. ' TArrFlag = array of TFlag;',
  9914. ' TArrSet = array of TFlags;',
  9915. ' TArrJSValue = array of jsvalue;',
  9916. 'var',
  9917. ' ArrInt: tarrint;',
  9918. ' ArrRec: tarrrec;',
  9919. ' ArrFlag: tarrflag;',
  9920. ' ArrSet: tarrset;',
  9921. ' ArrJSValue: tarrjsvalue;',
  9922. 'begin',
  9923. ' arrint:=concat(arrint);',
  9924. ' arrint:=concat(arrint,arrint);',
  9925. ' arrint:=concat(arrint,arrint,arrint);',
  9926. ' arrrec:=concat(arrrec);',
  9927. ' arrrec:=concat(arrrec,arrrec);',
  9928. ' arrrec:=concat(arrrec,arrrec,arrrec);',
  9929. ' arrset:=concat(arrset);',
  9930. ' arrset:=concat(arrset,arrset);',
  9931. ' arrset:=concat(arrset,arrset,arrset);',
  9932. ' arrjsvalue:=concat(arrjsvalue);',
  9933. ' arrjsvalue:=concat(arrjsvalue,arrjsvalue);',
  9934. ' arrjsvalue:=concat(arrjsvalue,arrjsvalue,arrjsvalue);',
  9935. ' arrint:=concat([1],arrint);',
  9936. ' arrflag:=concat([big]);',
  9937. ' arrflag:=concat([big],arrflag);',
  9938. ' arrflag:=concat(arrflag,[small]);',
  9939. '']);
  9940. ConvertProgram;
  9941. CheckSource('TestArray_Concat',
  9942. LinesToStr([ // statements
  9943. 'this.TFlag = {',
  9944. ' "0": "big",',
  9945. ' big: 0,',
  9946. ' "1": "small",',
  9947. ' small: 1',
  9948. '};',
  9949. 'rtl.recNewT(this, "TRec", function () {',
  9950. ' this.i = 0;',
  9951. ' this.$eq = function (b) {',
  9952. ' return this.i === b.i;',
  9953. ' };',
  9954. ' this.$assign = function (s) {',
  9955. ' this.i = s.i;',
  9956. ' return this;',
  9957. ' };',
  9958. '});',
  9959. 'this.ArrInt = [];',
  9960. 'this.ArrRec = [];',
  9961. 'this.ArrFlag = [];',
  9962. 'this.ArrSet = [];',
  9963. 'this.ArrJSValue = [];',
  9964. '']),
  9965. LinesToStr([ // $mod.$main
  9966. '$mod.ArrInt = rtl.arrayRef($mod.ArrInt);',
  9967. '$mod.ArrInt = rtl.arrayConcatN($mod.ArrInt, $mod.ArrInt);',
  9968. '$mod.ArrInt = rtl.arrayConcatN($mod.ArrInt, $mod.ArrInt, $mod.ArrInt);',
  9969. '$mod.ArrRec = rtl.arrayRef($mod.ArrRec);',
  9970. '$mod.ArrRec = rtl.arrayConcat($mod.TRec, $mod.ArrRec, $mod.ArrRec);',
  9971. '$mod.ArrRec = rtl.arrayConcat($mod.TRec, $mod.ArrRec, $mod.ArrRec, $mod.ArrRec);',
  9972. '$mod.ArrSet = rtl.arrayRef($mod.ArrSet);',
  9973. '$mod.ArrSet = rtl.arrayConcat("refSet", $mod.ArrSet, $mod.ArrSet);',
  9974. '$mod.ArrSet = rtl.arrayConcat("refSet", $mod.ArrSet, $mod.ArrSet, $mod.ArrSet);',
  9975. '$mod.ArrJSValue = rtl.arrayRef($mod.ArrJSValue);',
  9976. '$mod.ArrJSValue = rtl.arrayConcatN($mod.ArrJSValue, $mod.ArrJSValue);',
  9977. '$mod.ArrJSValue = rtl.arrayConcatN($mod.ArrJSValue, $mod.ArrJSValue, $mod.ArrJSValue);',
  9978. '$mod.ArrInt = rtl.arrayConcatN([1], $mod.ArrInt);',
  9979. '$mod.ArrFlag = [$mod.TFlag.big];',
  9980. '$mod.ArrFlag = rtl.arrayConcatN([$mod.TFlag.big], $mod.ArrFlag);',
  9981. '$mod.ArrFlag = rtl.arrayConcatN($mod.ArrFlag, [$mod.TFlag.small]);',
  9982. '']));
  9983. end;
  9984. procedure TTestModule.TestArray_Copy;
  9985. begin
  9986. StartProgram(false);
  9987. Add([
  9988. 'type',
  9989. ' integer = longint;',
  9990. ' TFlag = (big,small);',
  9991. ' TFlags = set of TFlag;',
  9992. ' TRec = record',
  9993. ' i: integer;',
  9994. ' end;',
  9995. ' TArrInt = array of integer;',
  9996. ' TArrRec = array of TRec;',
  9997. ' TArrSet = array of TFlags;',
  9998. ' TArrJSValue = array of jsvalue;',
  9999. 'var',
  10000. ' ArrInt: tarrint;',
  10001. ' ArrRec: tarrrec;',
  10002. ' ArrSet: tarrset;',
  10003. ' ArrJSValue: tarrjsvalue;',
  10004. 'begin',
  10005. ' arrint:=copy(arrint);',
  10006. ' arrint:=copy(arrint,2);',
  10007. ' arrint:=copy(arrint,3,4);',
  10008. ' arrint:=copy([1,1],1,2);',
  10009. ' arrrec:=copy(arrrec);',
  10010. ' arrrec:=copy(arrrec,5);',
  10011. ' arrrec:=copy(arrrec,6,7);',
  10012. ' arrset:=copy(arrset);',
  10013. ' arrset:=copy(arrset,8);',
  10014. ' arrset:=copy(arrset,9,10);',
  10015. ' arrjsvalue:=copy(arrjsvalue);',
  10016. ' arrjsvalue:=copy(arrjsvalue,11);',
  10017. ' arrjsvalue:=copy(arrjsvalue,12,13);',
  10018. ' ']);
  10019. ConvertProgram;
  10020. CheckSource('TestArray_Copy',
  10021. LinesToStr([ // statements
  10022. 'this.TFlag = {',
  10023. ' "0": "big",',
  10024. ' big: 0,',
  10025. ' "1": "small",',
  10026. ' small: 1',
  10027. '};',
  10028. 'rtl.recNewT(this, "TRec", function () {',
  10029. ' this.i = 0;',
  10030. ' this.$eq = function (b) {',
  10031. ' return this.i === b.i;',
  10032. ' };',
  10033. ' this.$assign = function (s) {',
  10034. ' this.i = s.i;',
  10035. ' return this;',
  10036. ' };',
  10037. '});',
  10038. 'this.ArrInt = [];',
  10039. 'this.ArrRec = [];',
  10040. 'this.ArrSet = [];',
  10041. 'this.ArrJSValue = [];',
  10042. '']),
  10043. LinesToStr([ // $mod.$main
  10044. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 0);',
  10045. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 2);',
  10046. '$mod.ArrInt = rtl.arrayCopy(0, $mod.ArrInt, 3, 4);',
  10047. '$mod.ArrInt = rtl.arrayCopy(0, [1, 1], 1, 2);',
  10048. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 0);',
  10049. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 5);',
  10050. '$mod.ArrRec = rtl.arrayCopy($mod.TRec, $mod.ArrRec, 6, 7);',
  10051. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 0);',
  10052. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 8);',
  10053. '$mod.ArrSet = rtl.arrayCopy("refSet", $mod.ArrSet, 9, 10);',
  10054. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 0);',
  10055. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 11);',
  10056. '$mod.ArrJSValue = rtl.arrayCopy(0, $mod.ArrJSValue, 12, 13);',
  10057. '']));
  10058. end;
  10059. procedure TTestModule.TestArray_InsertDelete;
  10060. begin
  10061. StartProgram(false);
  10062. Add([
  10063. 'type',
  10064. ' integer = longint;',
  10065. ' TFlag = (big,small);',
  10066. ' TFlags = set of TFlag;',
  10067. ' TRec = record',
  10068. ' i: integer;',
  10069. ' end;',
  10070. ' TArrInt = array of integer;',
  10071. ' TArrRec = array of TRec;',
  10072. ' TArrSet = array of TFlags;',
  10073. ' TArrJSValue = array of jsvalue;',
  10074. ' TArrArrInt = array of TArrInt;',
  10075. 'var',
  10076. ' ArrInt: tarrint;',
  10077. ' ArrRec: tarrrec;',
  10078. ' ArrSet: tarrset;',
  10079. ' ArrJSValue: tarrjsvalue;',
  10080. ' ArrArrInt: TArrArrInt;',
  10081. 'begin',
  10082. ' Insert(1,arrint,2);',
  10083. ' Insert(arrint[3],arrint,4);',
  10084. ' Insert(arrrec[5],arrrec,6);',
  10085. ' Insert(arrset[7],arrset,7);',
  10086. ' Insert(arrjsvalue[8],arrjsvalue,9);',
  10087. ' Insert(10,arrjsvalue,11);',
  10088. ' Insert([23],arrarrint,22);',
  10089. ' Delete(arrint,12,13);',
  10090. ' Delete(arrrec,14,15);',
  10091. ' Delete(arrset,17,18);',
  10092. ' Delete(arrjsvalue,19,10);']);
  10093. ConvertProgram;
  10094. CheckSource('TestArray_InsertDelete',
  10095. LinesToStr([ // statements
  10096. 'this.TFlag = {',
  10097. ' "0": "big",',
  10098. ' big: 0,',
  10099. ' "1": "small",',
  10100. ' small: 1',
  10101. '};',
  10102. 'rtl.recNewT(this, "TRec", function () {',
  10103. ' this.i = 0;',
  10104. ' this.$eq = function (b) {',
  10105. ' return this.i === b.i;',
  10106. ' };',
  10107. ' this.$assign = function (s) {',
  10108. ' this.i = s.i;',
  10109. ' return this;',
  10110. ' };',
  10111. '});',
  10112. 'this.ArrInt = [];',
  10113. 'this.ArrRec = [];',
  10114. 'this.ArrSet = [];',
  10115. 'this.ArrJSValue = [];',
  10116. 'this.ArrArrInt = [];',
  10117. '']),
  10118. LinesToStr([ // $mod.$main
  10119. '$mod.ArrInt.splice(2, 0, 1);',
  10120. '$mod.ArrInt.splice(4, 0, $mod.ArrInt[3]);',
  10121. '$mod.ArrRec.splice(6, 0, $mod.ArrRec[5]);',
  10122. '$mod.ArrSet.splice(7, 0, $mod.ArrSet[7]);',
  10123. '$mod.ArrJSValue.splice(9, 0, $mod.ArrJSValue[8]);',
  10124. '$mod.ArrJSValue.splice(11, 0, 10);',
  10125. '$mod.ArrArrInt.splice(22, 0, [23]);',
  10126. '$mod.ArrInt.splice(12, 13);',
  10127. '$mod.ArrRec.splice(14, 15);',
  10128. '$mod.ArrSet.splice(17, 18);',
  10129. '$mod.ArrJSValue.splice(19, 10);',
  10130. '']));
  10131. end;
  10132. procedure TTestModule.TestArray_DynArrayConstObjFPC;
  10133. begin
  10134. Parser.Options:=Parser.Options+[po_cassignments];
  10135. StartProgram(false);
  10136. Add([
  10137. '{$modeswitch arrayoperators}',
  10138. 'type',
  10139. ' integer = longint;',
  10140. ' TArrInt = array of integer;',
  10141. ' TArrStr = array of string;',
  10142. 'const',
  10143. ' Ints: TArrInt = (1,2,3);',
  10144. ' Aliases: TarrStr = (''foo'',''b'');',
  10145. ' OneInt: TArrInt = (7);',
  10146. ' OneStr: array of integer = (7);',
  10147. ' Chars: array of char = ''aoc'';',
  10148. ' Names: array of string = (''a'',''foo'');',
  10149. ' NameCount = low(Names)+high(Names)+length(Names);',
  10150. 'var i: integer;',
  10151. 'begin',
  10152. ' Ints:=[];',
  10153. ' Ints:=[1,1];',
  10154. ' Ints:=[1]+[2];',
  10155. ' Ints:=[2];',
  10156. ' Ints:=[]+ints;',
  10157. ' Ints:=Ints+[];',
  10158. ' Ints:=Ints+OneInt;',
  10159. ' Ints:=Ints+[1,1];',
  10160. ' Ints:=[i,i]+Ints;',
  10161. ' Ints:=[1]+[i]+[3];',
  10162. '']);
  10163. ConvertProgram;
  10164. CheckSource('TestArray_DynArrayConstObjFPC',
  10165. LinesToStr([ // statements
  10166. 'this.Ints = [1, 2, 3];',
  10167. 'this.Aliases = ["foo", "b"];',
  10168. 'this.OneInt = [7];',
  10169. 'this.OneStr = [7];',
  10170. 'this.Chars = ["a", "o", "c"];',
  10171. 'this.Names = ["a", "foo"];',
  10172. 'this.NameCount = 0 + (rtl.length(this.Names) - 1) + rtl.length(this.Names);',
  10173. 'this.i = 0;',
  10174. '']),
  10175. LinesToStr([ // $mod.$main
  10176. '$mod.Ints = [];',
  10177. '$mod.Ints = [1, 1];',
  10178. '$mod.Ints = rtl.arrayConcatN([1], [2]);',
  10179. '$mod.Ints = [2];',
  10180. '$mod.Ints = rtl.arrayConcatN([], $mod.Ints);',
  10181. '$mod.Ints = rtl.arrayConcatN($mod.Ints, []);',
  10182. '$mod.Ints = rtl.arrayConcatN($mod.Ints, $mod.OneInt);',
  10183. '$mod.Ints = rtl.arrayConcatN($mod.Ints, [1, 1]);',
  10184. '$mod.Ints = rtl.arrayConcatN([$mod.i, $mod.i], $mod.Ints);',
  10185. '$mod.Ints = rtl.arrayConcatN(rtl.arrayConcatN([1], [$mod.i]), [3]);',
  10186. '']));
  10187. end;
  10188. procedure TTestModule.TestArray_DynArrayConstDelphi;
  10189. begin
  10190. StartProgram(false);
  10191. // Note: const c = [1,1]; defines a set!
  10192. Add([
  10193. '{$mode delphi}',
  10194. 'type',
  10195. ' integer = longint;',
  10196. ' TArrInt = array of integer;',
  10197. ' TArrStr = array of string;',
  10198. 'const',
  10199. ' Ints: TArrInt = [1,1,2];',
  10200. ' Aliases: TarrStr = [''foo'',''b''];',
  10201. ' OneInt: TArrInt = [7];',
  10202. ' OneStr: array of integer = [7]+[8];',
  10203. ' Chars: array of char = ''aoc'';',
  10204. ' Names: array of string = [''a'',''a''];',
  10205. ' NameCount = low(Names)+high(Names)+length(Names);',
  10206. 'begin',
  10207. '']);
  10208. ConvertProgram;
  10209. CheckSource('TestArray_DynArrayConstDelphi',
  10210. LinesToStr([ // statements
  10211. 'this.Ints = [1, 1, 2];',
  10212. 'this.Aliases = ["foo", "b"];',
  10213. 'this.OneInt = [7];',
  10214. 'this.OneStr = rtl.arrayConcatN([7],[8]);',
  10215. 'this.Chars = ["a", "o", "c"];',
  10216. 'this.Names = ["a", "a"];',
  10217. 'this.NameCount = 0 + (rtl.length(this.Names) - 1) + rtl.length(this.Names);',
  10218. '']),
  10219. LinesToStr([ // $mod.$main
  10220. '']));
  10221. end;
  10222. procedure TTestModule.TestArray_ArrayLitAsParam;
  10223. begin
  10224. StartProgram(false);
  10225. Add([
  10226. '{$modeswitch arrayoperators}',
  10227. 'type',
  10228. ' integer = longint;',
  10229. ' TArrInt = array of integer;',
  10230. ' TArrSet = array of (red,green,blue);',
  10231. 'procedure DoOpenInt(const a: array of integer); forward;',
  10232. 'procedure DoInt(const a: TArrInt);',
  10233. 'begin',
  10234. ' DoInt(a+[1]);',
  10235. ' DoInt([1]+a);',
  10236. ' DoOpenInt(a);',
  10237. ' DoOpenInt(a+[1]);',
  10238. ' DoOpenInt([1]+a);',
  10239. 'end;',
  10240. 'procedure DoOpenInt(const a: array of integer);',
  10241. 'begin',
  10242. ' DoOpenInt(a+[1]);',
  10243. ' DoOpenInt([1]+a);',
  10244. ' DoInt(a);',
  10245. ' DoInt(a+[1]);',
  10246. ' DoInt([1]+a);',
  10247. 'end;',
  10248. 'procedure DoSet(const a: TArrSet);',
  10249. 'begin',
  10250. ' DoSet(a+[red]);',
  10251. ' DoSet([blue]+a);',
  10252. 'end;',
  10253. 'var',
  10254. ' i: TArrInt;',
  10255. ' s: TArrSet;',
  10256. 'begin',
  10257. ' DoInt([1]);',
  10258. ' DoInt([1]+[2]);',
  10259. ' DoInt(i+[1]);',
  10260. ' DoInt([1]+i);',
  10261. ' DoOpenInt([1]);',
  10262. ' DoOpenInt([1]+[2]);',
  10263. ' DoOpenInt(i+[1]);',
  10264. ' DoOpenInt([1]+i);',
  10265. ' DoSet([red]);',
  10266. ' DoSet([blue]+[green]);',
  10267. ' DoSet(s+[blue]);',
  10268. ' DoSet([red]+s);',
  10269. '']);
  10270. ConvertProgram;
  10271. CheckSource('TestArray_ArrayLitAsParam',
  10272. LinesToStr([ // statements
  10273. 'this.TArrSet$a = {',
  10274. ' "0": "red",',
  10275. ' red: 0,',
  10276. ' "1": "green",',
  10277. ' green: 1,',
  10278. ' "2": "blue",',
  10279. ' blue: 2',
  10280. '};',
  10281. 'this.DoInt = function (a) {',
  10282. ' $mod.DoInt(rtl.arrayConcatN(a, [1]));',
  10283. ' $mod.DoInt(rtl.arrayConcatN([1], a));',
  10284. ' $mod.DoOpenInt(a);',
  10285. ' $mod.DoOpenInt(rtl.arrayConcatN(a, [1]));',
  10286. ' $mod.DoOpenInt(rtl.arrayConcatN([1], a));',
  10287. '};',
  10288. 'this.DoOpenInt = function (a) {',
  10289. ' $mod.DoOpenInt(rtl.arrayConcatN(a, [1]));',
  10290. ' $mod.DoOpenInt(rtl.arrayConcatN([1], a));',
  10291. ' $mod.DoInt(a);',
  10292. ' $mod.DoInt(rtl.arrayConcatN(a, [1]));',
  10293. ' $mod.DoInt(rtl.arrayConcatN([1], a));',
  10294. '};',
  10295. 'this.DoSet = function (a) {',
  10296. ' $mod.DoSet(rtl.arrayConcatN(a, [$mod.TArrSet$a.red]));',
  10297. ' $mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.blue], a));',
  10298. '};',
  10299. 'this.i = [];',
  10300. 'this.s = [];',
  10301. '']),
  10302. LinesToStr([ // $mod.$main
  10303. '$mod.DoInt([1]);',
  10304. '$mod.DoInt(rtl.arrayConcatN([1], [2]));',
  10305. '$mod.DoInt(rtl.arrayConcatN($mod.i, [1]));',
  10306. '$mod.DoInt(rtl.arrayConcatN([1], $mod.i));',
  10307. '$mod.DoOpenInt([1]);',
  10308. '$mod.DoOpenInt(rtl.arrayConcatN([1], [2]));',
  10309. '$mod.DoOpenInt(rtl.arrayConcatN($mod.i, [1]));',
  10310. '$mod.DoOpenInt(rtl.arrayConcatN([1], $mod.i));',
  10311. '$mod.DoSet([$mod.TArrSet$a.red]);',
  10312. '$mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.blue], [$mod.TArrSet$a.green]));',
  10313. '$mod.DoSet(rtl.arrayConcatN($mod.s, [$mod.TArrSet$a.blue]));',
  10314. '$mod.DoSet(rtl.arrayConcatN([$mod.TArrSet$a.red], $mod.s));',
  10315. '']));
  10316. end;
  10317. procedure TTestModule.TestArray_ArrayLitMultiDimAsParam;
  10318. begin
  10319. StartProgram(false);
  10320. Add([
  10321. '{$modeswitch arrayoperators}',
  10322. 'type',
  10323. ' integer = longint;',
  10324. ' TArrInt = array of integer;',
  10325. ' TArrArrInt = array of TArrInt;',
  10326. 'procedure DoInt(const a: TArrArrInt);',
  10327. 'begin',
  10328. ' DoInt(a+[[1]]);',
  10329. ' DoInt([[1]]+a);',
  10330. ' DoInt(a);',
  10331. 'end;',
  10332. 'var',
  10333. ' i: TArrInt;',
  10334. ' a: TArrArrInt;',
  10335. 'begin',
  10336. ' a:=[[1]];',
  10337. ' a:=[i];',
  10338. ' a:=a+[i];',
  10339. ' a:=[i]+a;',
  10340. ' a:=[[1]+i];',
  10341. ' a:=[[1]+[2]];',
  10342. ' a:=[i+[2]];',
  10343. ' DoInt([[1]]);',
  10344. ' DoInt([[1]+[2],[3,4],[5]]);',
  10345. ' DoInt([i+[1]]+a);',
  10346. ' DoInt([i]+a);',
  10347. '']);
  10348. ConvertProgram;
  10349. CheckSource('TestArray_ArrayLitMultiDimAsParam',
  10350. LinesToStr([ // statements
  10351. 'this.DoInt = function (a) {',
  10352. ' $mod.DoInt(rtl.arrayConcatN(a, [[1]]));',
  10353. ' $mod.DoInt(rtl.arrayConcatN([[1]], a));',
  10354. ' $mod.DoInt(a);',
  10355. '};',
  10356. 'this.i = [];',
  10357. 'this.a = [];',
  10358. '']),
  10359. LinesToStr([ // $mod.$main
  10360. '$mod.a = [[1]];',
  10361. '$mod.a = [$mod.i];',
  10362. '$mod.a = rtl.arrayConcatN($mod.a, [$mod.i]);',
  10363. '$mod.a = rtl.arrayConcatN([$mod.i], $mod.a);',
  10364. '$mod.a = [rtl.arrayConcatN([1], $mod.i)];',
  10365. '$mod.a = [rtl.arrayConcatN([1], [2])];',
  10366. '$mod.a = [rtl.arrayConcatN($mod.i, [2])];',
  10367. '$mod.DoInt([[1]]);',
  10368. '$mod.DoInt([rtl.arrayConcatN([1], [2]), [3, 4], [5]]);',
  10369. '$mod.DoInt(rtl.arrayConcatN([rtl.arrayConcatN($mod.i, [1])], $mod.a));',
  10370. '$mod.DoInt(rtl.arrayConcatN([$mod.i], $mod.a));',
  10371. '']));
  10372. end;
  10373. procedure TTestModule.TestArray_ArrayLitStaticAsParam;
  10374. begin
  10375. StartProgram(false);
  10376. Add([
  10377. '{$modeswitch arrayoperators}',
  10378. 'type',
  10379. ' integer = longint;',
  10380. ' TArrInt = array[1..2] of integer;',
  10381. ' TArrArrInt = array of TArrInt;',
  10382. 'procedure DoInt(const a: TArrArrInt);',
  10383. 'begin',
  10384. ' DoInt(a+[[1,2]]);',
  10385. ' DoInt([[1,2]]+a);',
  10386. ' DoInt(a);',
  10387. 'end;',
  10388. 'var',
  10389. ' i: TArrInt;',
  10390. ' a: TArrArrInt;',
  10391. 'begin',
  10392. ' a:=[[1,1]];',
  10393. ' a:=[i];',
  10394. ' a:=a+[i];',
  10395. ' a:=[i]+a;',
  10396. ' DoInt([[1,1]]);',
  10397. ' DoInt([[1,2],[3,4]]);',
  10398. '']);
  10399. ConvertProgram;
  10400. CheckSource('TestArray_ArrayLitStaticAsParam',
  10401. LinesToStr([ // statements
  10402. 'this.DoInt = function (a) {',
  10403. ' $mod.DoInt(rtl.arrayConcatN(a, [[1, 2]]));',
  10404. ' $mod.DoInt(rtl.arrayConcatN([[1, 2]], a));',
  10405. ' $mod.DoInt(a);',
  10406. '};',
  10407. 'this.i = rtl.arraySetLength(null, 0, 2);',
  10408. 'this.a = [];',
  10409. '']),
  10410. LinesToStr([ // $mod.$main
  10411. '$mod.a = [[1, 1]];',
  10412. '$mod.a = [$mod.i.slice(0)];',
  10413. '$mod.a = rtl.arrayConcatN($mod.a, [$mod.i.slice(0)]);',
  10414. '$mod.a = rtl.arrayConcatN([$mod.i.slice(0)], $mod.a);',
  10415. '$mod.DoInt([[1, 1]]);',
  10416. '$mod.DoInt([[1, 2], [3, 4]]);',
  10417. '']));
  10418. end;
  10419. procedure TTestModule.TestArray_ForInArrOfString;
  10420. begin
  10421. StartProgram(false);
  10422. Add([
  10423. 'type',
  10424. 'type',
  10425. ' TMonthNameArray = array [1..12] of string;',
  10426. ' TMonthNames = TMonthNameArray;',
  10427. ' TObject = class',
  10428. ' private',
  10429. ' function GetLongMonthNames: TMonthNames; virtual; abstract;',
  10430. ' public',
  10431. ' Property LongMonthNames : TMonthNames Read GetLongMonthNames;',
  10432. ' end;',
  10433. 'var',
  10434. ' f: TObject;',
  10435. ' Month: string;',
  10436. ' Names: array of string = (''a'',''foo'',''bar'');',
  10437. ' i: longint;',
  10438. 'begin',
  10439. ' for Month in f.LongMonthNames do ;',
  10440. ' for Month in Names do ;',
  10441. ' for i:=low(Names) to high(Names) do ;',
  10442. '']);
  10443. ConvertProgram;
  10444. CheckSource('TestArray_ForInArrOfString',
  10445. LinesToStr([ // statements
  10446. 'rtl.createClass(this, "TObject", null, function () {',
  10447. ' this.$init = function () {',
  10448. ' };',
  10449. ' this.$final = function () {',
  10450. ' };',
  10451. '});',
  10452. 'this.f = null;',
  10453. 'this.Month = "";',
  10454. 'this.Names = ["a", "foo", "bar"];',
  10455. 'this.i = 0;',
  10456. '']),
  10457. LinesToStr([ // $mod.$main
  10458. 'for (var $in = $mod.f.GetLongMonthNames(), $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) $mod.Month = $in[$l];',
  10459. 'for (var $in1 = $mod.Names, $l1 = 0, $end1 = rtl.length($in1) - 1; $l1 <= $end1; $l1++) $mod.Month = $in1[$l1];',
  10460. 'for (var $l2 = 0, $end2 = rtl.length($mod.Names) - 1; $l2 <= $end2; $l2++) $mod.i = $l2;',
  10461. '']));
  10462. end;
  10463. procedure TTestModule.TestExternalClass_TypeCastArrayToExternalClass;
  10464. begin
  10465. StartProgram(false);
  10466. Add([
  10467. '{$modeswitch externalclass}',
  10468. 'type',
  10469. ' TJSObject = class external name ''Object''',
  10470. ' end;',
  10471. ' TJSArray = class external name ''Array''',
  10472. ' class function isArray(Value: JSValue) : boolean;',
  10473. ' function concat() : TJSArray; varargs;',
  10474. ' end;',
  10475. 'var',
  10476. ' aObj: TJSArray;',
  10477. ' a: array of longint;',
  10478. ' o: TJSObject;',
  10479. 'begin',
  10480. ' if TJSArray.isArray(65) then ;',
  10481. ' aObj:=TJSArray(a).concat(a);',
  10482. ' o:=TJSObject(a);']);
  10483. ConvertProgram;
  10484. CheckSource('TestExternalClass_TypeCastArrayToExternalClass',
  10485. LinesToStr([ // statements
  10486. 'this.aObj = null;',
  10487. 'this.a = [];',
  10488. 'this.o = null;',
  10489. '']),
  10490. LinesToStr([ // $mod.$main
  10491. 'if (Array.isArray(65)) ;',
  10492. '$mod.aObj = $mod.a.concat($mod.a);',
  10493. '$mod.o = $mod.a;',
  10494. '']));
  10495. end;
  10496. procedure TTestModule.TestExternalClass_TypeCastArrayFromExternalClass;
  10497. begin
  10498. StartProgram(false);
  10499. Add([
  10500. '{$modeswitch externalclass}',
  10501. 'type',
  10502. ' TArrStr = array of string;',
  10503. ' TJSArray = class external name ''Array''',
  10504. ' end;',
  10505. ' TJSObject = class external name ''Object''',
  10506. ' end;',
  10507. 'var',
  10508. ' aObj: TJSArray;',
  10509. ' a: TArrStr;',
  10510. ' jo: TJSObject;',
  10511. 'begin',
  10512. ' a:=TArrStr(aObj);',
  10513. ' TArrStr(aObj)[1]:=TArrStr(aObj)[2];',
  10514. ' a:=TarrStr(jo);',
  10515. '']);
  10516. ConvertProgram;
  10517. CheckSource('TestExternalClass_TypeCastArrayFromExternalClass',
  10518. LinesToStr([ // statements
  10519. 'this.aObj = null;',
  10520. 'this.a = [];',
  10521. 'this.jo = null;',
  10522. '']),
  10523. LinesToStr([ // $mod.$main
  10524. '$mod.a = $mod.aObj;',
  10525. '$mod.aObj[1] = $mod.aObj[2];',
  10526. '$mod.a = $mod.jo;',
  10527. '']));
  10528. end;
  10529. procedure TTestModule.TestArrayOfConst_TVarRec;
  10530. begin
  10531. StartProgram(true,[supTVarRec]);
  10532. Add([
  10533. 'procedure Say(args: array of const);',
  10534. 'var',
  10535. ' i: longint;',
  10536. ' v: TVarRec;',
  10537. 'begin',
  10538. ' for i:=low(args) to high(args) do begin',
  10539. ' v:=args[i];',
  10540. ' case v.vtype of',
  10541. ' vtInteger: if length(args)=args[i].vInteger then ;',
  10542. ' end;',
  10543. ' end;',
  10544. ' for v in args do ;',
  10545. ' args:=nil;',
  10546. ' SetLength(args,2);',
  10547. 'end;',
  10548. 'begin']);
  10549. ConvertProgram;
  10550. CheckSource('TestArrayOfConst_TVarRec',
  10551. LinesToStr([ // statements
  10552. 'this.Say = function (args) {',
  10553. ' var i = 0;',
  10554. ' var v = pas.system.TVarRec.$new();',
  10555. ' for (var $l = 0, $end = rtl.length(args) - 1; $l <= $end; $l++) {',
  10556. ' i = $l;',
  10557. ' v.$assign(args[i]);',
  10558. ' var $tmp = v.VType;',
  10559. ' if ($tmp === 0) if (rtl.length(args) === args[i].VJSValue) ;',
  10560. ' };',
  10561. ' for (var $in = args, $l1 = 0, $end1 = rtl.length($in) - 1; $l1 <= $end1; $l1++) v = $in[$l1];',
  10562. ' args = [];',
  10563. ' args = rtl.arraySetLength(args, pas.system.TVarRec, 2);',
  10564. '};',
  10565. '']),
  10566. LinesToStr([ // $mod.$main
  10567. ]));
  10568. end;
  10569. procedure TTestModule.TestArrayOfConst_PassBaseTypes;
  10570. begin
  10571. StartProgram(true,[supTVarRec]);
  10572. Add([
  10573. 'procedure Say(args: array of const);',
  10574. 'begin',
  10575. ' Say(args);',
  10576. 'end;',
  10577. 'var',
  10578. ' p: Pointer;',
  10579. ' j: jsvalue;',
  10580. ' c: currency;',
  10581. 'begin',
  10582. ' Say([]);',
  10583. ' Say([1]);',
  10584. ' Say([''c'',''foo'',nil,true,1.3,p,j,c]);',
  10585. '']);
  10586. ConvertProgram;
  10587. CheckSource('TestArrayOfConst_PassBaseTypes',
  10588. LinesToStr([ // statements
  10589. 'this.Say = function (args) {',
  10590. ' $mod.Say(args);',
  10591. '};',
  10592. 'this.p = null;',
  10593. 'this.j = undefined;',
  10594. 'this.c = 0;',
  10595. '']),
  10596. LinesToStr([ // $mod.$main
  10597. '$mod.Say([]);',
  10598. '$mod.Say(pas.system.VarRecs(0, 1));',
  10599. '$mod.Say(pas.system.VarRecs(',
  10600. ' 9,',
  10601. ' "c",',
  10602. ' 18,',
  10603. ' "foo",',
  10604. ' 5,',
  10605. ' null,',
  10606. ' 1,',
  10607. ' true,',
  10608. ' 3,',
  10609. ' 1.3,',
  10610. ' 5,',
  10611. ' $mod.p,',
  10612. ' 20,',
  10613. ' $mod.j,',
  10614. ' 12,',
  10615. ' $mod.c',
  10616. ' ));',
  10617. '']));
  10618. end;
  10619. procedure TTestModule.TestArrayOfConst_PassObj;
  10620. begin
  10621. StartProgram(true,[supTVarRec]);
  10622. Add([
  10623. '{$interfaces corba}',
  10624. 'type',
  10625. ' TObject = class',
  10626. ' end;',
  10627. ' TClass = class of TObject;',
  10628. ' IUnknown = interface',
  10629. ' end;',
  10630. 'procedure Say(args: array of const);',
  10631. 'begin',
  10632. 'end;',
  10633. 'var',
  10634. ' o: TObject;',
  10635. ' c: TClass;',
  10636. ' i: IUnknown;',
  10637. 'begin',
  10638. ' Say([o,c,TObject]);',
  10639. ' Say([nil,i]);',
  10640. '']);
  10641. ConvertProgram;
  10642. CheckSource('TestArrayOfConst_PassObj',
  10643. LinesToStr([ // statements
  10644. 'rtl.createClass(this, "TObject", null, function () {',
  10645. ' this.$init = function () {',
  10646. ' };',
  10647. ' this.$final = function () {',
  10648. ' };',
  10649. '});',
  10650. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  10651. 'this.Say = function (args) {',
  10652. '};',
  10653. 'this.o = null;',
  10654. 'this.c = null;',
  10655. 'this.i = null;',
  10656. '']),
  10657. LinesToStr([ // $mod.$main
  10658. '$mod.Say(pas.system.VarRecs(',
  10659. ' 7,',
  10660. ' $mod.o,',
  10661. ' 8,',
  10662. ' $mod.c,',
  10663. ' 8,',
  10664. ' $mod.TObject',
  10665. '));',
  10666. '$mod.Say(pas.system.VarRecs(5, null, 14, $mod.i));',
  10667. '']));
  10668. end;
  10669. procedure TTestModule.TestRecord_Empty;
  10670. begin
  10671. StartProgram(false);
  10672. Add([
  10673. 'type',
  10674. ' TRecA = record',
  10675. ' end;',
  10676. 'var a,b: TRecA;',
  10677. 'begin',
  10678. ' if a=b then ;']);
  10679. ConvertProgram;
  10680. CheckSource('TestRecord_Empty',
  10681. LinesToStr([ // statements
  10682. 'rtl.recNewT(this, "TRecA", function () {',
  10683. ' this.$eq = function (b) {',
  10684. ' return true;',
  10685. ' };',
  10686. ' this.$assign = function (s) {',
  10687. ' return this;',
  10688. ' };',
  10689. '});',
  10690. 'this.a = this.TRecA.$new();',
  10691. 'this.b = this.TRecA.$new();',
  10692. '']),
  10693. LinesToStr([ // $mod.$main
  10694. 'if ($mod.a.$eq($mod.b)) ;'
  10695. ]));
  10696. end;
  10697. procedure TTestModule.TestRecord_Var;
  10698. begin
  10699. StartProgram(false);
  10700. Add('type');
  10701. Add(' TRecA = record');
  10702. Add(' Bold: longint;');
  10703. Add(' end;');
  10704. Add('var Rec: TRecA;');
  10705. Add('begin');
  10706. Add(' rec.bold:=123');
  10707. ConvertProgram;
  10708. CheckSource('TestRecord_Var',
  10709. LinesToStr([ // statements
  10710. 'rtl.recNewT(this, "TRecA", function () {',
  10711. ' this.Bold = 0;',
  10712. ' this.$eq = function (b) {',
  10713. ' return this.Bold === b.Bold;',
  10714. ' };',
  10715. ' this.$assign = function (s) {',
  10716. ' this.Bold = s.Bold;',
  10717. ' return this;',
  10718. ' };',
  10719. '});',
  10720. 'this.Rec = this.TRecA.$new();',
  10721. '']),
  10722. LinesToStr([ // $mod.$main
  10723. '$mod.Rec.Bold = 123;'
  10724. ]));
  10725. end;
  10726. procedure TTestModule.TestRecord_VarExternal;
  10727. begin
  10728. StartProgram(false);
  10729. Add([
  10730. '{$modeswitch externalclass}',
  10731. 'type',
  10732. ' TRecA = record',
  10733. ' i: byte;',
  10734. ' length_: longint external name ''length'';',
  10735. ' end;',
  10736. 'var Rec: TRecA;',
  10737. 'begin',
  10738. ' rec.length_ := rec.length_',
  10739. '']);
  10740. ConvertProgram;
  10741. CheckSource('TestRecord_VarExternal',
  10742. LinesToStr([ // statements
  10743. 'rtl.recNewT(this, "TRecA", function () {',
  10744. ' this.i = 0;',
  10745. ' this.$eq = function (b) {',
  10746. ' return (this.i === b.i) && (this.length === b.length);',
  10747. ' };',
  10748. ' this.$assign = function (s) {',
  10749. ' this.i = s.i;',
  10750. ' this.length = s.length;',
  10751. ' return this;',
  10752. ' };',
  10753. '});',
  10754. 'this.Rec = this.TRecA.$new();',
  10755. '']),
  10756. LinesToStr([ // $mod.$main
  10757. '$mod.Rec.length = $mod.Rec.length;'
  10758. ]));
  10759. end;
  10760. procedure TTestModule.TestRecord_WithDo;
  10761. begin
  10762. StartProgram(false);
  10763. Add('type');
  10764. Add(' TRec = record');
  10765. Add(' vI: longint;');
  10766. Add(' end;');
  10767. Add('var');
  10768. Add(' Int: longint;');
  10769. Add(' r: TRec;');
  10770. Add('begin');
  10771. Add(' with r do');
  10772. Add(' int:=vi;');
  10773. Add(' with r do begin');
  10774. Add(' int:=vi;');
  10775. Add(' vi:=int;');
  10776. Add(' end;');
  10777. ConvertProgram;
  10778. CheckSource('TestWithRecordDo',
  10779. LinesToStr([ // statements
  10780. 'rtl.recNewT(this, "TRec", function () {',
  10781. ' this.vI = 0;',
  10782. ' this.$eq = function (b) {',
  10783. ' return this.vI === b.vI;',
  10784. ' };',
  10785. ' this.$assign = function (s) {',
  10786. ' this.vI = s.vI;',
  10787. ' return this;',
  10788. ' };',
  10789. '});',
  10790. 'this.Int = 0;',
  10791. 'this.r = this.TRec.$new();',
  10792. '']),
  10793. LinesToStr([ // $mod.$main
  10794. 'var $with = $mod.r;',
  10795. '$mod.Int = $with.vI;',
  10796. 'var $with1 = $mod.r;',
  10797. '$mod.Int = $with1.vI;',
  10798. '$with1.vI = $mod.Int;'
  10799. ]));
  10800. end;
  10801. procedure TTestModule.TestRecord_Assign;
  10802. begin
  10803. StartProgram(false);
  10804. Add([
  10805. 'type',
  10806. ' TEnum = (red,green);',
  10807. ' TEnums = set of TEnum;',
  10808. ' TSmallRec = record',
  10809. ' N: longint;',
  10810. ' end;',
  10811. ' TBigRec = record',
  10812. ' Int: longint;',
  10813. ' D: double;',
  10814. ' Arr: array of longint;',
  10815. ' Arr2: array[1..2] of longint;',
  10816. ' Small: TSmallRec;',
  10817. ' Enums: TEnums;',
  10818. ' end;',
  10819. 'var',
  10820. ' r, s: TBigRec;',
  10821. 'begin',
  10822. ' r:=s;',
  10823. ' r:=default(TBigRec);',
  10824. ' r:=default(s);',
  10825. '']);
  10826. ConvertProgram;
  10827. CheckSource('TestRecord_Assign',
  10828. LinesToStr([ // statements
  10829. 'this.TEnum = {',
  10830. ' "0": "red",',
  10831. ' red: 0,',
  10832. ' "1": "green",',
  10833. ' green: 1',
  10834. '};',
  10835. 'rtl.recNewT(this, "TSmallRec", function () {',
  10836. ' this.N = 0;',
  10837. ' this.$eq = function (b) {',
  10838. ' return this.N === b.N;',
  10839. ' };',
  10840. ' this.$assign = function (s) {',
  10841. ' this.N = s.N;',
  10842. ' return this;',
  10843. ' };',
  10844. '});',
  10845. 'rtl.recNewT(this, "TBigRec", function () {',
  10846. ' this.Int = 0;',
  10847. ' this.D = 0.0;',
  10848. ' this.$new = function () {',
  10849. ' var r = Object.create(this);',
  10850. ' r.Arr = [];',
  10851. ' r.Arr2 = rtl.arraySetLength(null, 0, 2);',
  10852. ' r.Small = $mod.TSmallRec.$new();',
  10853. ' r.Enums = {};',
  10854. ' return r;',
  10855. ' };',
  10856. ' this.$eq = function (b) {',
  10857. ' 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);',
  10858. ' };',
  10859. ' this.$assign = function (s) {',
  10860. ' this.Int = s.Int;',
  10861. ' this.D = s.D;',
  10862. ' this.Arr = rtl.arrayRef(s.Arr);',
  10863. ' this.Arr2 = s.Arr2.slice(0);',
  10864. ' this.Small.$assign(s.Small);',
  10865. ' this.Enums = rtl.refSet(s.Enums);',
  10866. ' return this;',
  10867. ' };',
  10868. '});',
  10869. 'this.r = this.TBigRec.$new();',
  10870. 'this.s = this.TBigRec.$new();',
  10871. '']),
  10872. LinesToStr([ // $mod.$main
  10873. '$mod.r.$assign($mod.s);',
  10874. '$mod.r.$assign($mod.TBigRec.$new());',
  10875. '$mod.r.$assign($mod.TBigRec.$new());',
  10876. '']));
  10877. end;
  10878. procedure TTestModule.TestRecord_AsParams;
  10879. begin
  10880. StartProgram(false);
  10881. Add([
  10882. 'type',
  10883. ' integer = longint;',
  10884. ' TRecord = record',
  10885. ' i: integer;',
  10886. ' end;',
  10887. 'procedure DoIt(vD: TRecord; const vC: TRecord; var vV: TRecord; var U);',
  10888. 'var vL: TRecord;',
  10889. 'begin',
  10890. ' vd:=vd;',
  10891. ' vd.i:=vd.i;',
  10892. ' vl:=vc;',
  10893. ' vv:=vv;',
  10894. ' vv.i:=vv.i;',
  10895. ' U:=vl;',
  10896. ' U:=vd;',
  10897. ' U:=vc;',
  10898. ' U:=vv;',
  10899. ' vl:=TRecord(U);',
  10900. ' vd:=TRecord(U);',
  10901. ' vv:=TRecord(U);',
  10902. ' doit(vd,vd,vd,vd);',
  10903. ' doit(vc,vc,vl,vl);',
  10904. ' doit(vv,vv,vv,vv);',
  10905. ' doit(vl,vl,vl,vl);',
  10906. ' TRecord(U).i:=3;',
  10907. 'end;',
  10908. 'var i: TRecord;',
  10909. 'begin',
  10910. ' doit(i,i,i,i);',
  10911. '']);
  10912. ConvertProgram;
  10913. CheckSource('TestRecord_AsParams',
  10914. LinesToStr([ // statements
  10915. 'rtl.recNewT(this, "TRecord", function () {',
  10916. ' this.i = 0;',
  10917. ' this.$eq = function (b) {',
  10918. ' return this.i === b.i;',
  10919. ' };',
  10920. ' this.$assign = function (s) {',
  10921. ' this.i = s.i;',
  10922. ' return this;',
  10923. ' };',
  10924. '});',
  10925. 'this.DoIt = function (vD, vC, vV, U) {',
  10926. ' var vL = $mod.TRecord.$new();',
  10927. ' vD.$assign(vD);',
  10928. ' vD.i = vD.i;',
  10929. ' vL.$assign(vC);',
  10930. ' vV.$assign(vV);',
  10931. ' vV.i = vV.i;',
  10932. ' U.$assign(vL);',
  10933. ' U.$assign(vD);',
  10934. ' U.$assign(vC);',
  10935. ' U.$assign(vV);',
  10936. ' vL.$assign(U);',
  10937. ' vD.$assign(U);',
  10938. ' vV.$assign(U);',
  10939. ' $mod.DoIt($mod.TRecord.$clone(vD), vD, vD, vD);',
  10940. ' $mod.DoIt($mod.TRecord.$clone(vC), vC, vL, vL);',
  10941. ' $mod.DoIt($mod.TRecord.$clone(vV), vV, vV, vV);',
  10942. ' $mod.DoIt($mod.TRecord.$clone(vL), vL, vL, vL);',
  10943. ' U.i = 3;',
  10944. '};',
  10945. 'this.i = this.TRecord.$new();'
  10946. ]),
  10947. LinesToStr([
  10948. '$mod.DoIt($mod.TRecord.$clone($mod.i), $mod.i, $mod.i, $mod.i);',
  10949. '']));
  10950. end;
  10951. procedure TTestModule.TestRecord_ConstRef;
  10952. begin
  10953. StartProgram(false);
  10954. Add([
  10955. 'type TRec = record i: word; end;',
  10956. 'procedure Run(constref a: TRec);',
  10957. 'begin',
  10958. 'end;',
  10959. 'procedure Fly(a: TRec; var b: TRec; out c: TRec; const d: TRec; constref e: TRec);',
  10960. 'var l: TRec;',
  10961. 'begin',
  10962. ' Run(l);',
  10963. ' Run(a);',
  10964. ' Run(b);',
  10965. ' Run(c);',
  10966. ' Run(d);',
  10967. ' Run(e);',
  10968. 'end;',
  10969. 'begin',
  10970. '']);
  10971. ConvertProgram;
  10972. CheckResolverUnexpectedHints();
  10973. CheckSource('TestRecord_ConstRef',
  10974. LinesToStr([ // statements
  10975. 'rtl.recNewT(this, "TRec", function () {',
  10976. ' this.i = 0;',
  10977. ' this.$eq = function (b) {',
  10978. ' return this.i === b.i;',
  10979. ' };',
  10980. ' this.$assign = function (s) {',
  10981. ' this.i = s.i;',
  10982. ' return this;',
  10983. ' };',
  10984. '});',
  10985. 'this.Run = function (a) {',
  10986. '};',
  10987. 'this.Fly = function (a, b, c, d, e) {',
  10988. ' var l = $mod.TRec.$new();',
  10989. ' $mod.Run(l);',
  10990. ' $mod.Run(a);',
  10991. ' $mod.Run(b);',
  10992. ' $mod.Run(c);',
  10993. ' $mod.Run(d);',
  10994. ' $mod.Run(e);',
  10995. '};',
  10996. '']),
  10997. LinesToStr([
  10998. '']));
  10999. end;
  11000. procedure TTestModule.TestRecordElement_AsParams;
  11001. begin
  11002. StartProgram(false);
  11003. Add('type');
  11004. Add(' integer = longint;');
  11005. Add(' TRecord = record');
  11006. Add(' i: integer;');
  11007. Add(' end;');
  11008. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  11009. Add('var vJ: TRecord;');
  11010. Add('begin');
  11011. Add(' doit(vj.i,vj.i,vj.i);');
  11012. Add('end;');
  11013. Add('var r: TRecord;');
  11014. Add('begin');
  11015. Add(' doit(r.i,r.i,r.i);');
  11016. ConvertProgram;
  11017. CheckSource('TestRecordElement_AsParams',
  11018. LinesToStr([ // statements
  11019. 'rtl.recNewT(this, "TRecord", function () {',
  11020. ' this.i = 0;',
  11021. ' this.$eq = function (b) {',
  11022. ' return this.i === b.i;',
  11023. ' };',
  11024. ' this.$assign = function (s) {',
  11025. ' this.i = s.i;',
  11026. ' return this;',
  11027. ' };',
  11028. '});',
  11029. 'this.DoIt = function (vG,vH,vI) {',
  11030. ' var vJ = $mod.TRecord.$new();',
  11031. ' $mod.DoIt(vJ.i, vJ.i, {',
  11032. ' p: vJ,',
  11033. ' get: function () {',
  11034. ' return this.p.i;',
  11035. ' },',
  11036. ' set: function (v) {',
  11037. ' this.p.i = v;',
  11038. ' }',
  11039. ' });',
  11040. '};',
  11041. 'this.r = this.TRecord.$new();'
  11042. ]),
  11043. LinesToStr([
  11044. '$mod.DoIt($mod.r.i,$mod.r.i,{',
  11045. ' p: $mod.r,',
  11046. ' get: function () {',
  11047. ' return this.p.i;',
  11048. ' },',
  11049. ' set: function (v) {',
  11050. ' this.p.i = v;',
  11051. ' }',
  11052. '});'
  11053. ]));
  11054. end;
  11055. procedure TTestModule.TestRecordElementFromFuncResult_AsParams;
  11056. begin
  11057. StartProgram(false);
  11058. Add('type');
  11059. Add(' integer = longint;');
  11060. Add(' TRecord = record');
  11061. Add(' i: integer;');
  11062. Add(' end;');
  11063. Add('function GetRec(vB: integer = 0): TRecord;');
  11064. Add('begin');
  11065. Add('end;');
  11066. Add('procedure DoIt(vG: integer; const vH: integer);');
  11067. Add('begin');
  11068. Add('end;');
  11069. Add('begin');
  11070. Add(' doit(getrec.i,getrec.i);');
  11071. Add(' doit(getrec().i,getrec().i);');
  11072. Add(' doit(getrec(1).i,getrec(2).i);');
  11073. ConvertProgram;
  11074. CheckSource('TestRecordElementFromFuncResult_AsParams',
  11075. LinesToStr([ // statements
  11076. 'rtl.recNewT(this, "TRecord", function () {',
  11077. ' this.i = 0;',
  11078. ' this.$eq = function (b) {',
  11079. ' return this.i === b.i;',
  11080. ' };',
  11081. ' this.$assign = function (s) {',
  11082. ' this.i = s.i;',
  11083. ' return this;',
  11084. ' };',
  11085. '});',
  11086. 'this.GetRec = function (vB) {',
  11087. ' var Result = $mod.TRecord.$new();',
  11088. ' return Result;',
  11089. '};',
  11090. 'this.DoIt = function (vG, vH) {',
  11091. '};',
  11092. '']),
  11093. LinesToStr([
  11094. '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
  11095. '$mod.DoIt($mod.GetRec(0).i,$mod.GetRec(0).i);',
  11096. '$mod.DoIt($mod.GetRec(1).i,$mod.GetRec(2).i);',
  11097. '']));
  11098. end;
  11099. procedure TTestModule.TestRecordElementFromWith_AsParams;
  11100. begin
  11101. StartProgram(false);
  11102. Add('type');
  11103. Add(' integer = longint;');
  11104. Add(' TRecord = record');
  11105. Add(' i: integer;');
  11106. Add(' end;');
  11107. Add('procedure DoIt(vG: integer; const vH: integer; var vI: integer);');
  11108. Add('begin');
  11109. Add('end;');
  11110. Add('var r: trecord;');
  11111. Add('begin');
  11112. Add(' with r do ');
  11113. Add(' doit(i,i,i);');
  11114. ConvertProgram;
  11115. CheckSource('TestRecordElementFromWith_AsParams',
  11116. LinesToStr([ // statements
  11117. 'rtl.recNewT(this, "TRecord", function () {',
  11118. ' this.i = 0;',
  11119. ' this.$eq = function (b) {',
  11120. ' return this.i === b.i;',
  11121. ' };',
  11122. ' this.$assign = function (s) {',
  11123. ' this.i = s.i;',
  11124. ' return this;',
  11125. ' };',
  11126. '});',
  11127. 'this.DoIt = function (vG,vH,vI) {',
  11128. '};',
  11129. 'this.r = this.TRecord.$new();'
  11130. ]),
  11131. LinesToStr([
  11132. 'var $with = $mod.r;',
  11133. '$mod.DoIt($with.i,$with.i,{',
  11134. ' p: $with,',
  11135. ' get: function () {',
  11136. ' return this.p.i;',
  11137. ' },',
  11138. ' set: function (v) {',
  11139. ' this.p.i = v;',
  11140. ' }',
  11141. '});',
  11142. '']));
  11143. end;
  11144. procedure TTestModule.TestRecord_Equal;
  11145. begin
  11146. StartProgram(false);
  11147. Add('type');
  11148. Add(' integer = longint;');
  11149. Add(' TFlag = (red,blue);');
  11150. Add(' TFlags = set of TFlag;');
  11151. Add(' TProc = procedure;');
  11152. Add(' TRecord = record');
  11153. Add(' i: integer;');
  11154. Add(' Event: TProc;');
  11155. Add(' f: TFlags;');
  11156. Add(' end;');
  11157. Add(' TNested = record');
  11158. Add(' r: TRecord;');
  11159. Add(' end;');
  11160. Add('var');
  11161. Add(' b: boolean;');
  11162. Add(' r,s: trecord;');
  11163. Add('begin');
  11164. Add(' b:=r=s;');
  11165. Add(' b:=r<>s;');
  11166. ConvertProgram;
  11167. CheckSource('TestRecord_Equal',
  11168. LinesToStr([ // statements
  11169. 'this.TFlag = {',
  11170. ' "0": "red",',
  11171. ' red: 0,',
  11172. ' "1": "blue",',
  11173. ' blue: 1',
  11174. '};',
  11175. 'rtl.recNewT(this, "TRecord", function () {',
  11176. ' this.i = 0;',
  11177. ' this.Event = null;',
  11178. ' this.$new = function () {',
  11179. ' var r = Object.create(this);',
  11180. ' r.f = {};',
  11181. ' return r;',
  11182. ' };',
  11183. ' this.$eq = function (b) {',
  11184. ' return (this.i === b.i) && rtl.eqCallback(this.Event, b.Event) && rtl.eqSet(this.f, b.f);',
  11185. ' };',
  11186. ' this.$assign = function (s) {',
  11187. ' this.i = s.i;',
  11188. ' this.Event = s.Event;',
  11189. ' this.f = rtl.refSet(s.f);',
  11190. ' return this;',
  11191. ' };',
  11192. '});',
  11193. 'rtl.recNewT(this, "TNested", function () {',
  11194. ' this.$new = function () {',
  11195. ' var r = Object.create(this);',
  11196. ' r.r = $mod.TRecord.$new();',
  11197. ' return r;',
  11198. ' };',
  11199. ' this.$eq = function (b) {',
  11200. ' return this.r.$eq(b.r);',
  11201. ' };',
  11202. ' this.$assign = function (s) {',
  11203. ' this.r.$assign(s.r);',
  11204. ' return this;',
  11205. ' };',
  11206. '});',
  11207. 'this.b = false;',
  11208. 'this.r = this.TRecord.$new();',
  11209. 'this.s = this.TRecord.$new();',
  11210. '']),
  11211. LinesToStr([
  11212. '$mod.b = $mod.r.$eq($mod.s);',
  11213. '$mod.b = !$mod.r.$eq($mod.s);',
  11214. '']));
  11215. end;
  11216. procedure TTestModule.TestRecord_JSValue;
  11217. begin
  11218. StartProgram(false);
  11219. Add([
  11220. 'type',
  11221. ' TRecord = record',
  11222. ' i: longint;',
  11223. ' end;',
  11224. 'procedure Fly(d: jsvalue; const c: jsvalue);',
  11225. 'begin',
  11226. 'end;',
  11227. 'procedure Run(d: TRecord; const c: TRecord; var v: TRecord);',
  11228. 'begin',
  11229. ' if jsvalue(d) then ;',
  11230. ' if jsvalue(c) then ;',
  11231. ' if jsvalue(v) then ;',
  11232. 'end;',
  11233. 'var',
  11234. ' Jv: jsvalue;',
  11235. ' Rec: trecord;',
  11236. 'begin',
  11237. ' rec:=trecord(jv);',
  11238. ' jv:=rec;',
  11239. ' Fly(rec,rec);',
  11240. ' Fly(@rec,@rec);',
  11241. ' if jsvalue(Rec) then ;',
  11242. ' Run(trecord(jv),trecord(jv),rec);',
  11243. '']);
  11244. ConvertProgram;
  11245. CheckSource('TestRecord_JSValue',
  11246. LinesToStr([ // statements
  11247. 'rtl.recNewT(this, "TRecord", function () {',
  11248. ' this.i = 0;',
  11249. ' this.$eq = function (b) {',
  11250. ' return this.i === b.i;',
  11251. ' };',
  11252. ' this.$assign = function (s) {',
  11253. ' this.i = s.i;',
  11254. ' return this;',
  11255. ' };',
  11256. '});',
  11257. 'this.Fly = function (d, c) {',
  11258. '};',
  11259. 'this.Run = function (d, c, v) {',
  11260. ' if (d) ;',
  11261. ' if (c) ;',
  11262. ' if (v) ;',
  11263. '};',
  11264. 'this.Jv = undefined;',
  11265. 'this.Rec = this.TRecord.$new();',
  11266. '']),
  11267. LinesToStr([
  11268. '$mod.Rec.$assign(rtl.getObject($mod.Jv));',
  11269. '$mod.Jv = $mod.Rec;',
  11270. '$mod.Fly($mod.TRecord.$clone($mod.Rec), $mod.Rec);',
  11271. '$mod.Fly($mod.Rec, $mod.Rec);',
  11272. 'if ($mod.Rec) ;',
  11273. '$mod.Run($mod.TRecord.$clone(rtl.getObject($mod.Jv)), rtl.getObject($mod.Jv), $mod.Rec);',
  11274. '']));
  11275. end;
  11276. procedure TTestModule.TestRecord_VariantFail;
  11277. begin
  11278. StartProgram(false);
  11279. Add([
  11280. 'type',
  11281. ' TRec = record',
  11282. ' case word of',
  11283. ' 0: (b0, b1: Byte);',
  11284. ' 1: (i: word);',
  11285. ' end;',
  11286. 'begin']);
  11287. SetExpectedPasResolverError('variant record is not supported',
  11288. nXIsNotSupported);
  11289. ConvertProgram;
  11290. end;
  11291. procedure TTestModule.TestRecord_FieldArray;
  11292. begin
  11293. StartProgram(false);
  11294. Add([
  11295. 'type',
  11296. ' TArrInt = array[3..4] of longint;',
  11297. ' TArrArrInt = array[3..4] of longint;',
  11298. ' TRec = record',
  11299. ' a: array of longint;',
  11300. ' s: array[1..2] of longint;',
  11301. ' m: array[1..2,3..4] of longint;',
  11302. ' o: TArrArrInt;',
  11303. ' end;',
  11304. 'begin']);
  11305. ConvertProgram;
  11306. CheckSource('TestRecord_FieldArray',
  11307. LinesToStr([ // statements
  11308. 'rtl.recNewT(this, "TRec", function () {',
  11309. ' this.$new = function () {',
  11310. ' var r = Object.create(this);',
  11311. ' r.a = [];',
  11312. ' r.s = rtl.arraySetLength(null, 0, 2);',
  11313. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  11314. ' r.o = rtl.arraySetLength(null, 0, 2);',
  11315. ' return r;',
  11316. ' };',
  11317. ' this.$eq = function (b) {',
  11318. ' return (this.a === b.a) && rtl.arrayEq(this.s, b.s) && rtl.arrayEq(this.m, b.m) && rtl.arrayEq(this.o, b.o);',
  11319. ' };',
  11320. ' this.$assign = function (s) {',
  11321. ' this.a = rtl.arrayRef(s.a);',
  11322. ' this.s = s.s.slice(0);',
  11323. ' this.m = s.m.slice(0);',
  11324. ' this.o = s.o.slice(0);',
  11325. ' return this;',
  11326. ' };',
  11327. '});',
  11328. '']),
  11329. LinesToStr([ // $mod.$main
  11330. '']));
  11331. end;
  11332. procedure TTestModule.TestRecord_Const;
  11333. begin
  11334. StartProgram(false);
  11335. Add([
  11336. 'type',
  11337. ' TArrInt = array[3..4] of longint;',
  11338. ' TPoint = record x,y: longint; end;',
  11339. ' TRec = record',
  11340. ' i: longint;',
  11341. ' a: array of longint;',
  11342. ' s: array[1..2] of longint;',
  11343. ' m: array[1..2,3..4] of longint;',
  11344. ' p: TPoint;',
  11345. ' end;',
  11346. ' TPoints = array of TPoint;',
  11347. 'const',
  11348. ' r: TRec = (',
  11349. ' i:1;',
  11350. ' a:(2,3);',
  11351. ' s:(4,5);',
  11352. ' m:( (11,12), (13,14) );',
  11353. ' p: (x:21; y:22)',
  11354. ' );',
  11355. ' p: TPoints = ( (x:1;y:2), (x:3;y:4) );',
  11356. 'begin']);
  11357. ConvertProgram;
  11358. CheckSource('TestRecord_Const',
  11359. LinesToStr([ // statements
  11360. 'rtl.recNewT(this, "TPoint", function () {',
  11361. ' this.x = 0;',
  11362. ' this.y = 0;',
  11363. ' this.$eq = function (b) {',
  11364. ' return (this.x === b.x) && (this.y === b.y);',
  11365. ' };',
  11366. ' this.$assign = function (s) {',
  11367. ' this.x = s.x;',
  11368. ' this.y = s.y;',
  11369. ' return this;',
  11370. ' };',
  11371. '});',
  11372. 'rtl.recNewT(this, "TRec", function () {',
  11373. ' this.i = 0;',
  11374. ' this.$new = function () {',
  11375. ' var r = Object.create(this);',
  11376. ' r.a = [];',
  11377. ' r.s = rtl.arraySetLength(null, 0, 2);',
  11378. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  11379. ' r.p = $mod.TPoint.$new();',
  11380. ' return r;',
  11381. ' };',
  11382. ' this.$eq = function (b) {',
  11383. ' 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);',
  11384. ' };',
  11385. ' this.$assign = function (s) {',
  11386. ' this.i = s.i;',
  11387. ' this.a = rtl.arrayRef(s.a);',
  11388. ' this.s = s.s.slice(0);',
  11389. ' this.m = s.m.slice(0);',
  11390. ' this.p.$assign(s.p);',
  11391. ' return this;',
  11392. ' };',
  11393. '});',
  11394. 'this.r = this.TRec.$clone({',
  11395. ' i: 1,',
  11396. ' a: [2, 3],',
  11397. ' s: [4, 5],',
  11398. ' m: [[11, 12], [13, 14]],',
  11399. ' p: this.TPoint.$clone({',
  11400. ' x: 21,',
  11401. ' y: 22',
  11402. ' })',
  11403. '});',
  11404. 'this.p = [this.TPoint.$clone({',
  11405. ' x: 1,',
  11406. ' y: 2',
  11407. '}), this.TPoint.$clone({',
  11408. ' x: 3,',
  11409. ' y: 4',
  11410. '})];',
  11411. '']),
  11412. LinesToStr([ // $mod.$main
  11413. '']));
  11414. end;
  11415. procedure TTestModule.TestRecord_TypecastFail;
  11416. begin
  11417. StartProgram(false);
  11418. Add([
  11419. 'type',
  11420. ' TPoint = record x,y: longint; end;',
  11421. ' TRec = record l: longint end;',
  11422. 'var p: TPoint;',
  11423. 'begin',
  11424. ' if TRec(p).l=2 then ;']);
  11425. SetExpectedPasResolverError('Illegal type conversion: "TPoint" to "record TRec"',
  11426. nIllegalTypeConversionTo);
  11427. ConvertProgram;
  11428. end;
  11429. procedure TTestModule.TestRecord_InFunction;
  11430. begin
  11431. StartProgram(false);
  11432. Add([
  11433. 'var TPoint: longint = 3;',
  11434. 'procedure DoIt;',
  11435. 'type',
  11436. ' TPoint = record x,y: longint; end;',
  11437. ' TPoints = array of TPoint;',
  11438. 'var',
  11439. ' r: TPoint;',
  11440. ' p: TPoints;',
  11441. 'begin',
  11442. ' SetLength(p,2);',
  11443. 'end;',
  11444. 'begin']);
  11445. ConvertProgram;
  11446. CheckSource('TestRecord_InFunction',
  11447. LinesToStr([ // statements
  11448. 'this.TPoint = 3;',
  11449. 'var TPoint$1 = rtl.recNewT(null, "", function () {',
  11450. ' this.x = 0;',
  11451. ' this.y = 0;',
  11452. ' this.$eq = function (b) {',
  11453. ' return (this.x === b.x) && (this.y === b.y);',
  11454. ' };',
  11455. ' this.$assign = function (s) {',
  11456. ' this.x = s.x;',
  11457. ' this.y = s.y;',
  11458. ' return this;',
  11459. ' };',
  11460. '});',
  11461. 'this.DoIt = function () {',
  11462. ' var r = TPoint$1.$new();',
  11463. ' var p = [];',
  11464. ' p = rtl.arraySetLength(p, TPoint$1, 2);',
  11465. '};',
  11466. '']),
  11467. LinesToStr([ // $mod.$main
  11468. '']));
  11469. end;
  11470. procedure TTestModule.TestRecord_AnonymousFail;
  11471. begin
  11472. StartProgram(false);
  11473. Add([
  11474. 'var',
  11475. ' r: record x: word end;',
  11476. 'begin']);
  11477. SetExpectedPasResolverError('not yet implemented: :TPasRecordType [20190408224556] "anonymous record type"',
  11478. nNotYetImplemented);
  11479. ConvertProgram;
  11480. end;
  11481. procedure TTestModule.TestAdvRecord_Function;
  11482. begin
  11483. StartProgram(false);
  11484. Parser.Options:=Parser.Options+[po_cassignments];
  11485. Add([
  11486. '{$modeswitch AdvancedRecords}',
  11487. 'type',
  11488. ' TPoint = record',
  11489. ' x,y: word;',
  11490. ' function Add(const apt: TPoint): TPoint;',
  11491. ' end;',
  11492. 'function TPoint.Add(const apt: TPoint): TPoint;',
  11493. 'begin',
  11494. ' Result:=Self;',
  11495. ' Result.x+=apt.x;',
  11496. ' Result.y:=Result.y+apt.y;',
  11497. ' Self:=apt;',
  11498. 'end;',
  11499. 'var p,q: TPoint;',
  11500. 'begin',
  11501. ' p.add(q);',
  11502. ' p:=default(TPoint);',
  11503. ' p:=q;',
  11504. '']);
  11505. ConvertProgram;
  11506. CheckSource('TestAdvRecord_Function',
  11507. LinesToStr([ // statements
  11508. 'rtl.recNewT(this, "TPoint", function () {',
  11509. ' this.x = 0;',
  11510. ' this.y = 0;',
  11511. ' this.$eq = function (b) {',
  11512. ' return (this.x === b.x) && (this.y === b.y);',
  11513. ' };',
  11514. ' this.$assign = function (s) {',
  11515. ' this.x = s.x;',
  11516. ' this.y = s.y;',
  11517. ' return this;',
  11518. ' };',
  11519. ' this.Add = function (apt) {',
  11520. ' var Result = $mod.TPoint.$new();',
  11521. ' Result.$assign(this);',
  11522. ' Result.x += apt.x;',
  11523. ' Result.y = Result.y + apt.y;',
  11524. ' this.$assign(apt);',
  11525. ' return Result;',
  11526. ' };',
  11527. '});',
  11528. 'this.p = this.TPoint.$new();',
  11529. 'this.q = this.TPoint.$new();',
  11530. '']),
  11531. LinesToStr([ // $mod.$main
  11532. '$mod.p.Add($mod.q);',
  11533. '$mod.p.$assign($mod.TPoint.$new());',
  11534. '$mod.p.$assign($mod.q);',
  11535. '']));
  11536. end;
  11537. procedure TTestModule.TestAdvRecord_Property;
  11538. begin
  11539. StartProgram(false);
  11540. Add([
  11541. '{$modeswitch AdvancedRecords}',
  11542. 'type',
  11543. ' TPoint = record',
  11544. ' x,y: word;',
  11545. ' strict private',
  11546. ' function GetSize: longword;',
  11547. ' procedure SetSize(Value: longword);',
  11548. ' public',
  11549. ' property Size: longword read GetSize write SetSize;',
  11550. ' property Left: word read x write y;',
  11551. ' end;',
  11552. 'procedure SetSize(Value: longword); begin end;',// check auto rename
  11553. 'function TPoint.GetSize: longword;',
  11554. 'begin',
  11555. ' x:=y;',
  11556. ' Size:=Size;',
  11557. ' Left:=Left;',
  11558. 'end;',
  11559. 'procedure TPoint.SetSize(Value: longword);',
  11560. 'begin',
  11561. 'end;',
  11562. 'var p,q: TPoint;',
  11563. 'begin',
  11564. ' p.Size:=q.Size;',
  11565. ' p.Left:=q.Left;',
  11566. '']);
  11567. ConvertProgram;
  11568. CheckSource('TestAdvRecord_Property',
  11569. LinesToStr([ // statements
  11570. 'rtl.recNewT(this, "TPoint", function () {',
  11571. ' this.x = 0;',
  11572. ' this.y = 0;',
  11573. ' this.$eq = function (b) {',
  11574. ' return (this.x === b.x) && (this.y === b.y);',
  11575. ' };',
  11576. ' this.$assign = function (s) {',
  11577. ' this.x = s.x;',
  11578. ' this.y = s.y;',
  11579. ' return this;',
  11580. ' };',
  11581. ' this.GetSize = function () {',
  11582. ' var Result = 0;',
  11583. ' this.x = this.y;',
  11584. ' this.SetSize(this.GetSize());',
  11585. ' this.y = this.x;',
  11586. ' return Result;',
  11587. ' };',
  11588. ' this.SetSize = function (Value) {',
  11589. ' };',
  11590. '});',
  11591. 'this.SetSize = function (Value) {',
  11592. '};',
  11593. 'this.p = this.TPoint.$new();',
  11594. 'this.q = this.TPoint.$new();',
  11595. '']),
  11596. LinesToStr([ // $mod.$main
  11597. '$mod.p.SetSize($mod.q.GetSize());',
  11598. '$mod.p.y = $mod.q.x;',
  11599. '']));
  11600. end;
  11601. procedure TTestModule.TestAdvRecord_PropertyDefault;
  11602. begin
  11603. StartProgram(false);
  11604. Add([
  11605. '{$modeswitch AdvancedRecords}',
  11606. 'type',
  11607. ' TPoint = record',
  11608. ' strict private',
  11609. ' function GetItems(Index: word): word;',
  11610. ' procedure SetItems(Index: word; Value: word);',
  11611. ' public',
  11612. ' property Items[Index: word]: word read GetItems write SetItems; default;',
  11613. ' end;',
  11614. 'function TPoint.GetItems(Index: word): word;',
  11615. 'begin',
  11616. ' Items[index]:=Items[index];',
  11617. ' self.Items[index]:=self.Items[index];',
  11618. 'end;',
  11619. 'procedure TPoint.SetItems(Index: word; Value: word);',
  11620. 'begin',
  11621. 'end;',
  11622. 'var p: TPoint;',
  11623. 'begin',
  11624. ' p[1]:=p[2];',
  11625. ' p.Items[3]:=p.Items[4];',
  11626. '']);
  11627. ConvertProgram;
  11628. CheckSource('TestAdvRecord_PropertyDefault',
  11629. LinesToStr([ // statements
  11630. 'rtl.recNewT(this, "TPoint", function () {',
  11631. ' this.$eq = function (b) {',
  11632. ' return true;',
  11633. ' };',
  11634. ' this.$assign = function (s) {',
  11635. ' return this;',
  11636. ' };',
  11637. ' this.GetItems = function (Index) {',
  11638. ' var Result = 0;',
  11639. ' this.SetItems(Index, this.GetItems(Index));',
  11640. ' this.SetItems(Index, this.GetItems(Index));',
  11641. ' return Result;',
  11642. ' };',
  11643. ' this.SetItems = function (Index, Value) {',
  11644. ' };',
  11645. '});',
  11646. 'this.p = this.TPoint.$new();',
  11647. '']),
  11648. LinesToStr([ // $mod.$main
  11649. '$mod.p.SetItems(1, $mod.p.GetItems(2));',
  11650. '$mod.p.SetItems(3, $mod.p.GetItems(4));',
  11651. '']));
  11652. end;
  11653. procedure TTestModule.TestAdvRecord_Property_ClassMethod;
  11654. begin
  11655. StartProgram(false);
  11656. Add([
  11657. '{$modeswitch AdvancedRecords}',
  11658. 'type',
  11659. ' TRec = record',
  11660. ' class var',
  11661. ' Fx: longint;',
  11662. ' Fy: longint;',
  11663. ' class function GetInt: longint; static;',
  11664. ' class procedure SetInt(Value: longint); static;',
  11665. ' class procedure DoIt; static;',
  11666. ' class property IntA: longint read Fx write Fy;',
  11667. ' class property IntB: longint read GetInt write SetInt;',
  11668. ' end;',
  11669. 'class function trec.getint: longint;',
  11670. 'begin',
  11671. ' result:=fx;',
  11672. 'end;',
  11673. 'class procedure trec.setint(value: longint);',
  11674. 'begin',
  11675. 'end;',
  11676. 'class procedure trec.doit;',
  11677. 'begin',
  11678. ' IntA:=IntA+1;',
  11679. ' IntB:=IntB+1;',
  11680. 'end;',
  11681. 'var r: trec;',
  11682. 'begin',
  11683. ' trec.inta:=trec.inta+1;',
  11684. ' if trec.intb=2 then;',
  11685. ' trec.intb:=trec.intb+2;',
  11686. ' trec.setint(trec.inta);',
  11687. ' r.inta:=r.inta+1;',
  11688. ' if r.intb=2 then;',
  11689. ' r.intb:=r.intb+2;',
  11690. ' r.setint(r.inta);']);
  11691. ConvertProgram;
  11692. CheckSource('TestAdvRecord_Property_ClassMethod',
  11693. LinesToStr([ // statements
  11694. 'rtl.recNewT(this, "TRec", function () {',
  11695. ' this.Fx = 0;',
  11696. ' this.Fy = 0;',
  11697. ' this.$eq = function (b) {',
  11698. ' return true;',
  11699. ' };',
  11700. ' this.$assign = function (s) {',
  11701. ' return this;',
  11702. ' };',
  11703. ' this.GetInt = function () {',
  11704. ' var Result = 0;',
  11705. ' Result = $mod.TRec.Fx;',
  11706. ' return Result;',
  11707. ' };',
  11708. ' this.SetInt = function (Value) {',
  11709. ' };',
  11710. ' this.DoIt = function () {',
  11711. ' $mod.TRec.Fy = $mod.TRec.Fx + 1;',
  11712. ' $mod.TRec.SetInt($mod.TRec.GetInt() + 1);',
  11713. ' };',
  11714. '}, true);',
  11715. 'this.r = this.TRec.$new();',
  11716. '']),
  11717. LinesToStr([ // $mod.$main
  11718. '$mod.TRec.Fy = $mod.TRec.Fx + 1;',
  11719. 'if ($mod.TRec.GetInt() === 2) ;',
  11720. '$mod.TRec.SetInt($mod.TRec.GetInt() + 2);',
  11721. '$mod.TRec.SetInt($mod.TRec.Fx);',
  11722. '$mod.TRec.Fy = $mod.r.Fx + 1;',
  11723. 'if ($mod.TRec.GetInt() === 2) ;',
  11724. '$mod.TRec.SetInt($mod.TRec.GetInt() + 2);',
  11725. '$mod.TRec.SetInt($mod.r.Fx);',
  11726. '']));
  11727. end;
  11728. procedure TTestModule.TestAdvRecord_Const;
  11729. begin
  11730. StartProgram(false);
  11731. Add([
  11732. '{$modeswitch AdvancedRecords}',
  11733. 'type',
  11734. ' TArrInt = array[3..4] of longint;',
  11735. ' TPoint = record',
  11736. ' x,y: longint;',
  11737. ' class var Count: nativeint;',
  11738. ' end;',
  11739. ' TRec = record',
  11740. ' i: longint;',
  11741. ' a: array of longint;',
  11742. ' s: array[1..2] of longint;',
  11743. ' m: array[1..2,3..4] of longint;',
  11744. ' p: TPoint;',
  11745. ' end;',
  11746. ' TPoints = array of TPoint;',
  11747. 'const',
  11748. ' r: TRec = (',
  11749. ' i:1;',
  11750. ' a:(2,3);',
  11751. ' s:(4,5);',
  11752. ' m:( (11,12), (13,14) );',
  11753. ' p: (x:21)',
  11754. ' );',
  11755. ' p: TPoints = ( (x:1;y:2), (x:3;y:4) );',
  11756. 'begin']);
  11757. ConvertProgram;
  11758. CheckSource('TestAdvRecord_Const',
  11759. LinesToStr([ // statements
  11760. 'rtl.recNewT(this, "TPoint", function () {',
  11761. ' this.x = 0;',
  11762. ' this.y = 0;',
  11763. ' this.Count = 0;',
  11764. ' this.$eq = function (b) {',
  11765. ' return (this.x === b.x) && (this.y === b.y);',
  11766. ' };',
  11767. ' this.$assign = function (s) {',
  11768. ' this.x = s.x;',
  11769. ' this.y = s.y;',
  11770. ' return this;',
  11771. ' };',
  11772. '}, true);',
  11773. 'rtl.recNewT(this, "TRec", function () {',
  11774. ' this.i = 0;',
  11775. ' this.$new = function () {',
  11776. ' var r = Object.create(this);',
  11777. ' r.a = [];',
  11778. ' r.s = rtl.arraySetLength(null, 0, 2);',
  11779. ' r.m = rtl.arraySetLength(null, 0, 2, 2);',
  11780. ' r.p = $mod.TPoint.$new();',
  11781. ' return r;',
  11782. ' };',
  11783. ' this.$eq = function (b) {',
  11784. ' 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);',
  11785. ' };',
  11786. ' this.$assign = function (s) {',
  11787. ' this.i = s.i;',
  11788. ' this.a = rtl.arrayRef(s.a);',
  11789. ' this.s = s.s.slice(0);',
  11790. ' this.m = s.m.slice(0);',
  11791. ' this.p.$assign(s.p);',
  11792. ' return this;',
  11793. ' };',
  11794. '});',
  11795. 'this.r = this.TRec.$clone({',
  11796. ' i: 1,',
  11797. ' a: [2, 3],',
  11798. ' s: [4, 5],',
  11799. ' m: [[11, 12], [13, 14]],',
  11800. ' p: this.TPoint.$clone({',
  11801. ' x: 21,',
  11802. ' y: 0',
  11803. ' })',
  11804. '});',
  11805. 'this.p = [this.TPoint.$clone({',
  11806. ' x: 1,',
  11807. ' y: 2',
  11808. '}), this.TPoint.$clone({',
  11809. ' x: 3,',
  11810. ' y: 4',
  11811. '})];',
  11812. '']),
  11813. LinesToStr([ // $mod.$main
  11814. '']));
  11815. end;
  11816. procedure TTestModule.TestAdvRecord_ExternalField;
  11817. begin
  11818. StartProgram(false);
  11819. Add([
  11820. '{$modeswitch AdvancedRecords}',
  11821. '{$modeswitch externalclass}',
  11822. 'type',
  11823. ' TCar = record',
  11824. ' public',
  11825. ' Intern: longint external name ''$Intern'';',
  11826. ' Intern2: longint external name ''$Intern2'';',
  11827. ' Bracket: longint external name ''["A B"]'';',
  11828. ' procedure DoIt;',
  11829. ' end;',
  11830. 'procedure tcar.doit;',
  11831. 'begin',
  11832. ' Intern:=Intern+1;',
  11833. ' Intern2:=Intern2+2;',
  11834. ' Bracket:=Bracket+3;',
  11835. 'end;',
  11836. 'var Rec: TCar = (intern: 11; intern2: 12; bracket: 13);',
  11837. 'begin',
  11838. ' Rec.intern:=Rec.intern+1;',
  11839. ' Rec.intern2:=Rec.intern2+2;',
  11840. ' Rec.Bracket:=Rec.Bracket+3;',
  11841. ' with Rec do begin',
  11842. ' intern:=intern+1;',
  11843. ' intern2:=intern2+2;',
  11844. ' Bracket:=Bracket+3;',
  11845. ' end;']);
  11846. ConvertProgram;
  11847. CheckSource('TestAdvRecord_ExternalField',
  11848. LinesToStr([ // statements
  11849. 'rtl.recNewT(this, "TCar", function () {',
  11850. ' this.$eq = function (b) {',
  11851. ' return (this.$Intern === b.$Intern) && (this.$Intern2 === b.$Intern2) && (this["A B"] === b["A B"]);',
  11852. ' };',
  11853. ' this.$assign = function (s) {',
  11854. ' this.$Intern = s.$Intern;',
  11855. ' this.$Intern2 = s.$Intern2;',
  11856. ' this["A B"] = s["A B"];',
  11857. ' return this;',
  11858. ' };',
  11859. ' this.DoIt = function () {',
  11860. ' this.$Intern = this.$Intern + 1;',
  11861. ' this.$Intern2 = this.$Intern2 + 2;',
  11862. ' this["A B"] = this["A B"] + 3;',
  11863. ' };',
  11864. '});',
  11865. 'this.Rec = this.TCar.$clone({',
  11866. ' $Intern: 11,',
  11867. ' $Intern2: 12,',
  11868. ' "A B": 13',
  11869. '});',
  11870. '']),
  11871. LinesToStr([ // $mod.$main
  11872. '$mod.Rec.$Intern = $mod.Rec.$Intern + 1;',
  11873. '$mod.Rec.$Intern2 = $mod.Rec.$Intern2 + 2;',
  11874. '$mod.Rec["A B"] = $mod.Rec["A B"] + 3;',
  11875. 'var $with = $mod.Rec;',
  11876. '$with.$Intern = $with.$Intern + 1;',
  11877. '$with.$Intern2 = $with.$Intern2 + 2;',
  11878. '$with["A B"] = $with["A B"] + 3;',
  11879. '']));
  11880. end;
  11881. procedure TTestModule.TestAdvRecord_SubRecord;
  11882. begin
  11883. StartProgram(false);
  11884. Add([
  11885. '{$modeswitch AdvancedRecords}',
  11886. 'type',
  11887. ' TRec = record',
  11888. ' type',
  11889. ' TPoint = record',
  11890. ' x,y: longint;',
  11891. ' class var Count: nativeint;',
  11892. ' procedure DoIt;',
  11893. ' class procedure DoThat; static;',
  11894. ' end;',
  11895. ' var',
  11896. ' i: longint;',
  11897. ' p: TPoint;',
  11898. ' procedure DoSome;',
  11899. ' end;',
  11900. 'const',
  11901. ' r: TRec = (',
  11902. ' i:1;',
  11903. ' p: (x:21;y:22)',
  11904. ' );',
  11905. 'procedure TRec.DoSome;',
  11906. 'begin',
  11907. ' p.x:=p.y+1;',
  11908. ' p.Count:=p.Count+2;',
  11909. 'end;',
  11910. 'procedure TRec.TPoint.DoIt;',
  11911. 'begin',
  11912. ' Count:=Count+3;',
  11913. 'end;',
  11914. 'class procedure TRec.TPoint.DoThat;',
  11915. 'begin',
  11916. ' Count:=Count+4;',
  11917. 'end;',
  11918. 'begin']);
  11919. ConvertProgram;
  11920. CheckSource('TestAdvRecord_SubRecord',
  11921. LinesToStr([ // statements
  11922. 'rtl.recNewT(this, "TRec", function () {',
  11923. ' rtl.recNewT(this, "TPoint", function () {',
  11924. ' this.x = 0;',
  11925. ' this.y = 0;',
  11926. ' this.Count = 0;',
  11927. ' this.$eq = function (b) {',
  11928. ' return (this.x === b.x) && (this.y === b.y);',
  11929. ' };',
  11930. ' this.$assign = function (s) {',
  11931. ' this.x = s.x;',
  11932. ' this.y = s.y;',
  11933. ' return this;',
  11934. ' };',
  11935. ' this.DoIt = function () {',
  11936. ' $mod.TRec.TPoint.Count = this.Count + 3;',
  11937. ' };',
  11938. ' this.DoThat = function () {',
  11939. ' $mod.TRec.TPoint.Count = $mod.TRec.TPoint.Count + 4;',
  11940. ' };',
  11941. ' }, true);',
  11942. ' this.i = 0;',
  11943. ' this.$new = function () {',
  11944. ' var r = Object.create(this);',
  11945. ' r.p = this.TPoint.$new();',
  11946. ' return r;',
  11947. ' };',
  11948. ' this.$eq = function (b) {',
  11949. ' return (this.i === b.i) && this.p.$eq(b.p);',
  11950. ' };',
  11951. ' this.$assign = function (s) {',
  11952. ' this.i = s.i;',
  11953. ' this.p.$assign(s.p);',
  11954. ' return this;',
  11955. ' };',
  11956. ' this.DoSome = function () {',
  11957. ' this.p.x = this.p.y + 1;',
  11958. ' this.TPoint.Count = this.p.Count + 2;',
  11959. ' };',
  11960. '}, true);',
  11961. 'this.r = this.TRec.$clone({',
  11962. ' i: 1,',
  11963. ' p: this.TRec.TPoint.$clone({',
  11964. ' x: 21,',
  11965. ' y: 22',
  11966. ' })',
  11967. '});',
  11968. '']),
  11969. LinesToStr([ // $mod.$main
  11970. '']));
  11971. end;
  11972. procedure TTestModule.TestAdvRecord_SubClass;
  11973. begin
  11974. StartProgram(false);
  11975. Add([
  11976. '{$modeswitch AdvancedRecords}',
  11977. 'type',
  11978. ' TObject = class end;',
  11979. ' TPoint = record',
  11980. ' type',
  11981. ' TBird = class',
  11982. ' procedure DoIt;',
  11983. ' class procedure Glob;',
  11984. ' end;',
  11985. ' procedure DoIt(b: TBird);',
  11986. ' end;',
  11987. 'procedure TPoint.TBird.DoIt;',
  11988. 'begin',
  11989. ' doit;',
  11990. ' self.doit;',
  11991. ' glob;',
  11992. ' self.glob;',
  11993. 'end;',
  11994. 'class procedure TPoint.TBird.Glob;',
  11995. 'begin',
  11996. ' glob;',
  11997. ' self.glob;',
  11998. 'end;',
  11999. 'procedure TPoint.DoIt(b: TBird);',
  12000. 'begin',
  12001. ' b.doit;',
  12002. ' b.glob;',
  12003. ' TBird.glob;',
  12004. 'end;',
  12005. 'begin',
  12006. '']);
  12007. ConvertProgram;
  12008. CheckSource('TestAdvRecord_SubClass',
  12009. LinesToStr([ // statements
  12010. 'rtl.createClass(this, "TObject", null, function () {',
  12011. ' this.$init = function () {',
  12012. ' };',
  12013. ' this.$final = function () {',
  12014. ' };',
  12015. '});',
  12016. 'rtl.recNewT(this, "TPoint", function () {',
  12017. ' rtl.createClass(this, "TBird", $mod.TObject, function () {',
  12018. ' this.DoIt = function () {',
  12019. ' this.DoIt();',
  12020. ' this.DoIt();',
  12021. ' this.$class.Glob();',
  12022. ' this.$class.Glob();',
  12023. ' };',
  12024. ' this.Glob = function () {',
  12025. ' this.Glob();',
  12026. ' this.Glob();',
  12027. ' };',
  12028. ' }, "TPoint.TBird");',
  12029. ' this.$eq = function (b) {',
  12030. ' return true;',
  12031. ' };',
  12032. ' this.$assign = function (s) {',
  12033. ' return this;',
  12034. ' };',
  12035. ' this.DoIt = function (b) {',
  12036. ' b.DoIt();',
  12037. ' b.$class.Glob();',
  12038. ' this.TBird.Glob();',
  12039. ' };',
  12040. '}, true);',
  12041. '']),
  12042. LinesToStr([ // $mod.$main
  12043. '']));
  12044. end;
  12045. procedure TTestModule.TestAdvRecord_SubInterfaceFail;
  12046. begin
  12047. StartProgram(false);
  12048. Add([
  12049. '{$modeswitch AdvancedRecords}',
  12050. 'type',
  12051. ' IUnknown = interface end;',
  12052. ' TPoint = record',
  12053. ' type IBird = interface end;',
  12054. ' end;',
  12055. 'begin',
  12056. '']);
  12057. SetExpectedPasResolverError('not yet implemented: IBird:TPasClassType [20190105143752] "interface inside record"',
  12058. nNotYetImplemented);
  12059. ParseProgram;
  12060. end;
  12061. procedure TTestModule.TestAdvRecord_Constructor;
  12062. begin
  12063. StartProgram(false);
  12064. Add([
  12065. '{$modeswitch AdvancedRecords}',
  12066. 'type',
  12067. ' TPoint = record',
  12068. ' x,y: longint;',
  12069. ' class procedure Run(w: longint = 13); static;',
  12070. ' constructor Create(ax: longint; ay: longint = -1);',
  12071. ' end;',
  12072. 'class procedure tpoint.run(w: longint);',
  12073. 'begin',
  12074. ' run;',
  12075. ' run();',
  12076. 'end;',
  12077. 'constructor tpoint.create(ax,ay: longint);',
  12078. 'begin',
  12079. ' x:=ax;',
  12080. ' self.y:=ay;',
  12081. ' run;',
  12082. ' run(ax);',
  12083. 'end;',
  12084. 'var r: TPoint;',
  12085. 'begin',
  12086. ' r:=TPoint.Create(1,2);',
  12087. ' with TPoint do r:=Create(1,2);',
  12088. ' r.Create(3);',
  12089. ' r:=r.Create(4);',
  12090. '']);
  12091. ConvertProgram;
  12092. CheckSource('TestAdvRecord_Constructor',
  12093. LinesToStr([ // statements
  12094. 'rtl.recNewT(this, "TPoint", function () {',
  12095. ' this.x = 0;',
  12096. ' this.y = 0;',
  12097. ' this.$eq = function (b) {',
  12098. ' return (this.x === b.x) && (this.y === b.y);',
  12099. ' };',
  12100. ' this.$assign = function (s) {',
  12101. ' this.x = s.x;',
  12102. ' this.y = s.y;',
  12103. ' return this;',
  12104. ' };',
  12105. ' this.Run = function (w) {',
  12106. ' $mod.TPoint.Run(13);',
  12107. ' $mod.TPoint.Run(13);',
  12108. ' };',
  12109. ' this.Create = function (ax, ay) {',
  12110. ' this.x = ax;',
  12111. ' this.y = ay;',
  12112. ' this.Run(13);',
  12113. ' this.Run(ax);',
  12114. ' return this;',
  12115. ' };',
  12116. '});',
  12117. 'this.r = this.TPoint.$new();',
  12118. '']),
  12119. LinesToStr([ // $mod.$main
  12120. '$mod.r.$assign($mod.TPoint.$new().Create(1, 2));',
  12121. 'var $with = $mod.TPoint;',
  12122. '$mod.r.$assign($with.$new().Create(1, 2));',
  12123. '$mod.r.Create(3, -1);',
  12124. '$mod.r.$assign($mod.r.Create(4, -1));',
  12125. '']));
  12126. end;
  12127. procedure TTestModule.TestAdvRecord_ClassConstructor_Program;
  12128. begin
  12129. StartProgram(false);
  12130. Add([
  12131. '{$modeswitch AdvancedRecords}',
  12132. 'type',
  12133. ' TPoint = record',
  12134. ' class var x: longint;',
  12135. ' class procedure Fly; static;',
  12136. ' class constructor Init;',
  12137. ' end;',
  12138. 'var count: word;',
  12139. 'class procedure Tpoint.Fly;',
  12140. 'begin',
  12141. 'end;',
  12142. 'class constructor tpoint.init;',
  12143. 'begin',
  12144. ' count:=count+1;',
  12145. ' x:=x+3;',
  12146. ' tpoint.x:=tpoint.x+4;',
  12147. ' fly;',
  12148. ' tpoint.fly;',
  12149. 'end;',
  12150. 'var r: TPoint;',
  12151. 'begin',
  12152. ' r.x:=r.x+10;',
  12153. ' r.Fly;',
  12154. ' r.Fly();',
  12155. '']);
  12156. ConvertProgram;
  12157. CheckSource('TestAdvRecord_ClassConstructor_Program',
  12158. LinesToStr([ // statements
  12159. 'rtl.recNewT(this, "TPoint", function () {',
  12160. ' this.x = 0;',
  12161. ' this.$eq = function (b) {',
  12162. ' return true;',
  12163. ' };',
  12164. ' this.$assign = function (s) {',
  12165. ' return this;',
  12166. ' };',
  12167. ' this.Fly = function () {',
  12168. ' };',
  12169. '}, true);',
  12170. 'this.count = 0;',
  12171. 'this.r = this.TPoint.$new();',
  12172. '']),
  12173. LinesToStr([ // $mod.$main
  12174. '(function () {',
  12175. ' $mod.count = $mod.count + 1;',
  12176. ' $mod.TPoint.x = $mod.TPoint.x + 3;',
  12177. ' $mod.TPoint.x = $mod.TPoint.x + 4;',
  12178. ' $mod.TPoint.Fly();',
  12179. ' $mod.TPoint.Fly();',
  12180. '})();',
  12181. '$mod.TPoint.x = $mod.r.x + 10;',
  12182. '$mod.TPoint.Fly();',
  12183. '$mod.TPoint.Fly();',
  12184. '']));
  12185. end;
  12186. procedure TTestModule.TestAdvRecord_ClassConstructor_Unit;
  12187. begin
  12188. StartUnit(false);
  12189. Add([
  12190. 'interface',
  12191. '{$modeswitch AdvancedRecords}',
  12192. 'type',
  12193. ' TPoint = record',
  12194. ' class var x: longint;',
  12195. ' class procedure Fly; static;',
  12196. ' class constructor Init;',
  12197. ' end;',
  12198. 'implementation',
  12199. 'var count: word;',
  12200. 'class procedure Tpoint.Fly;',
  12201. 'begin',
  12202. 'end;',
  12203. 'class constructor tpoint.init;',
  12204. 'begin',
  12205. ' count:=count+1;',
  12206. ' x:=3;',
  12207. ' tpoint.x:=4;',
  12208. ' fly;',
  12209. ' tpoint.fly;',
  12210. 'end;',
  12211. '']);
  12212. ConvertUnit;
  12213. CheckSource('TestAdvRecord_ClassConstructor_Unit',
  12214. LinesToStr([ // statements
  12215. 'var $impl = $mod.$impl;',
  12216. 'rtl.recNewT(this, "TPoint", function () {',
  12217. ' this.x = 0;',
  12218. ' this.$eq = function (b) {',
  12219. ' return true;',
  12220. ' };',
  12221. ' this.$assign = function (s) {',
  12222. ' return this;',
  12223. ' };',
  12224. ' this.Fly = function () {',
  12225. ' };',
  12226. '}, true);',
  12227. '']),
  12228. LinesToStr([ // $mod.$init
  12229. '(function () {',
  12230. ' $impl.count = $impl.count + 1;',
  12231. ' $mod.TPoint.x = 3;',
  12232. ' $mod.TPoint.x = 4;',
  12233. ' $mod.TPoint.Fly();',
  12234. ' $mod.TPoint.Fly();',
  12235. '})();',
  12236. '']),
  12237. LinesToStr([ // $mod.$main
  12238. '$impl.count = 0;',
  12239. '']));
  12240. end;
  12241. procedure TTestModule.TestClass_TObjectDefaultConstructor;
  12242. begin
  12243. StartProgram(false);
  12244. Add(['type',
  12245. ' TObject = class',
  12246. ' public',
  12247. ' constructor Create;',
  12248. ' destructor Destroy;',
  12249. ' end;',
  12250. ' TBird = TObject;',
  12251. 'constructor tobject.create;',
  12252. 'begin end;',
  12253. 'destructor tobject.destroy;',
  12254. 'begin end;',
  12255. 'var Obj: tobject;',
  12256. 'begin',
  12257. ' obj:=tobject.create;',
  12258. ' obj:=tobject.create();',
  12259. ' obj:=tbird.create;',
  12260. ' obj:=tbird.create();',
  12261. ' obj:=obj.create();',
  12262. ' obj.destroy;',
  12263. '']);
  12264. ConvertProgram;
  12265. CheckSource('TestClass_TObjectDefaultConstructor',
  12266. LinesToStr([ // statements
  12267. 'rtl.createClass(this,"TObject",null,function(){',
  12268. ' this.$init = function () {',
  12269. ' };',
  12270. ' this.$final = function () {',
  12271. ' };',
  12272. ' this.Create = function(){',
  12273. ' return this;',
  12274. ' };',
  12275. ' this.Destroy = function(){',
  12276. ' };',
  12277. '});',
  12278. 'this.Obj = null;'
  12279. ]),
  12280. LinesToStr([ // $mod.$main
  12281. '$mod.Obj = $mod.TObject.$create("Create");',
  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.Obj.Create();',
  12286. '$mod.Obj.$destroy("Destroy");',
  12287. '']));
  12288. end;
  12289. procedure TTestModule.TestClass_TObjectConstructorWithParams;
  12290. begin
  12291. StartProgram(false);
  12292. Add('type');
  12293. Add(' TObject = class');
  12294. Add(' public');
  12295. Add(' constructor Create(Par: longint);');
  12296. Add(' end;');
  12297. Add('constructor tobject.create(par: longint);');
  12298. Add('begin end;');
  12299. Add('var Obj: tobject;');
  12300. Add('begin');
  12301. Add(' obj:=tobject.create(3);');
  12302. ConvertProgram;
  12303. CheckSource('TestClass_TObjectConstructorWithParams',
  12304. LinesToStr([ // statements
  12305. 'rtl.createClass(this,"TObject",null,function(){',
  12306. ' this.$init = function () {',
  12307. ' };',
  12308. ' this.$final = function () {',
  12309. ' };',
  12310. ' this.Create = function(Par){',
  12311. ' return this;',
  12312. ' };',
  12313. '});',
  12314. 'this.Obj = null;'
  12315. ]),
  12316. LinesToStr([ // $mod.$main
  12317. '$mod.Obj = $mod.TObject.$create("Create",[3]);'
  12318. ]));
  12319. end;
  12320. procedure TTestModule.TestClass_TObjectConstructorWithDefaultParam;
  12321. begin
  12322. StartProgram(false);
  12323. Add('type');
  12324. Add(' TObject = class');
  12325. Add(' public');
  12326. Add(' constructor Create;');
  12327. Add(' end;');
  12328. Add(' TTest = class(TObject)');
  12329. Add(' public');
  12330. Add(' constructor Create(const Par: longint = 1);');
  12331. Add(' end;');
  12332. Add('constructor tobject.create;');
  12333. Add('begin end;');
  12334. Add('constructor ttest.create(const par: longint);');
  12335. Add('begin end;');
  12336. Add('var t: ttest;');
  12337. Add('begin');
  12338. Add(' t:=ttest.create;');
  12339. Add(' t:=ttest.create(2);');
  12340. ConvertProgram;
  12341. CheckSource('TestClass_TObjectConstructorWithDefaultParam',
  12342. LinesToStr([ // statements
  12343. 'rtl.createClass(this,"TObject",null,function(){',
  12344. ' this.$init = function () {',
  12345. ' };',
  12346. ' this.$final = function () {',
  12347. ' };',
  12348. ' this.Create = function(){',
  12349. ' return this;',
  12350. ' };',
  12351. '});',
  12352. 'rtl.createClass(this, "TTest", this.TObject, function () {',
  12353. ' this.Create$1 = function (Par) {',
  12354. ' return this;',
  12355. ' };',
  12356. '});',
  12357. 'this.t = null;'
  12358. ]),
  12359. LinesToStr([ // $mod.$main
  12360. '$mod.t = $mod.TTest.$create("Create$1", [1]);',
  12361. '$mod.t = $mod.TTest.$create("Create$1", [2]);'
  12362. ]));
  12363. end;
  12364. procedure TTestModule.TestClass_Var;
  12365. begin
  12366. StartProgram(false);
  12367. Add([
  12368. 'type',
  12369. ' TObject = class',
  12370. ' public',
  12371. ' vI: longint;',
  12372. ' constructor Create(Par: longint);',
  12373. ' end;',
  12374. 'constructor tobject.create(par: longint);',
  12375. 'begin',
  12376. ' vi:=par+3',
  12377. 'end;',
  12378. 'var Obj: tobject;',
  12379. 'begin',
  12380. ' obj:=tobject.create(4);',
  12381. ' obj.vi:=obj.VI+5;']);
  12382. ConvertProgram;
  12383. CheckSource('TestClass_Var',
  12384. LinesToStr([ // statements
  12385. 'rtl.createClass(this,"TObject",null,function(){',
  12386. ' this.$init = function () {',
  12387. ' this.vI = 0;',
  12388. ' };',
  12389. ' this.$final = function () {',
  12390. ' };',
  12391. ' this.Create = function(Par){',
  12392. ' this.vI = Par+3;',
  12393. ' return this;',
  12394. ' };',
  12395. '});',
  12396. 'this.Obj = null;'
  12397. ]),
  12398. LinesToStr([ // $mod.$main
  12399. '$mod.Obj = $mod.TObject.$create("Create",[4]);',
  12400. '$mod.Obj.vI = $mod.Obj.vI + 5;'
  12401. ]));
  12402. end;
  12403. procedure TTestModule.TestClass_Method;
  12404. begin
  12405. StartProgram(false);
  12406. Add('type');
  12407. Add(' TObject = class');
  12408. Add(' public');
  12409. Add(' vI: longint;');
  12410. Add(' Sub: TObject;');
  12411. Add(' constructor Create;');
  12412. Add(' function GetIt(Par: longint): tobject;');
  12413. Add(' end;');
  12414. Add('constructor tobject.create; begin end;');
  12415. Add('function tobject.getit(par: longint): tobject;');
  12416. Add('begin');
  12417. Add(' Self.vi:=par+3;');
  12418. Add(' Result:=self.sub;');
  12419. Add('end;');
  12420. Add('var Obj: tobject;');
  12421. Add('begin');
  12422. Add(' obj:=tobject.create;');
  12423. Add(' obj.getit(4);');
  12424. Add(' obj.sub.sub:=nil;');
  12425. Add(' obj.sub.getit(5);');
  12426. Add(' obj.sub.getit(6).SUB:=nil;');
  12427. Add(' obj.sub.getit(7).GETIT(8);');
  12428. Add(' obj.sub.getit(9).SuB.getit(10);');
  12429. ConvertProgram;
  12430. CheckSource('TestClass_Method',
  12431. LinesToStr([ // statements
  12432. 'rtl.createClass(this,"TObject",null,function(){',
  12433. ' this.$init = function () {',
  12434. ' this.vI = 0;',
  12435. ' this.Sub = null;',
  12436. ' };',
  12437. ' this.$final = function () {',
  12438. ' this.Sub = undefined;',
  12439. ' };',
  12440. ' this.Create = function(){',
  12441. ' return this;',
  12442. ' };',
  12443. ' this.GetIt = function(Par){',
  12444. ' var Result = null;',
  12445. ' this.vI = Par + 3;',
  12446. ' Result = this.Sub;',
  12447. ' return Result;',
  12448. ' };',
  12449. '});',
  12450. 'this.Obj = null;'
  12451. ]),
  12452. LinesToStr([ // $mod.$main
  12453. '$mod.Obj = $mod.TObject.$create("Create");',
  12454. '$mod.Obj.GetIt(4);',
  12455. '$mod.Obj.Sub.Sub=null;',
  12456. '$mod.Obj.Sub.GetIt(5);',
  12457. '$mod.Obj.Sub.GetIt(6).Sub=null;',
  12458. '$mod.Obj.Sub.GetIt(7).GetIt(8);',
  12459. '$mod.Obj.Sub.GetIt(9).Sub.GetIt(10);'
  12460. ]));
  12461. end;
  12462. procedure TTestModule.TestClass_Implementation;
  12463. begin
  12464. StartUnit(false);
  12465. Add([
  12466. 'interface',
  12467. 'type',
  12468. ' TObject = class',
  12469. ' constructor Create;',
  12470. ' end;',
  12471. 'implementation',
  12472. 'type',
  12473. ' TIntClass = class',
  12474. ' constructor Create; reintroduce;',
  12475. ' class procedure DoGlob;',
  12476. ' end;',
  12477. 'constructor tintclass.create;',
  12478. 'begin',
  12479. ' inherited;',
  12480. ' inherited create;',
  12481. ' doglob;',
  12482. 'end;',
  12483. 'class procedure tintclass.doglob;',
  12484. 'begin',
  12485. 'end;',
  12486. 'constructor tobject.create;',
  12487. 'var',
  12488. ' iC: tintclass;',
  12489. 'begin',
  12490. ' ic:=tintclass.create;',
  12491. ' tintclass.doglob;',
  12492. ' ic.doglob;',
  12493. 'end;',
  12494. 'initialization',
  12495. ' tintclass.doglob;',
  12496. '']);
  12497. ConvertUnit;
  12498. CheckSource('TestClass_Implementation',
  12499. LinesToStr([ // statements
  12500. 'var $impl = $mod.$impl;',
  12501. 'rtl.createClass(this, "TObject", null, function () {',
  12502. ' this.$init = function () {',
  12503. ' };',
  12504. ' this.$final = function () {',
  12505. ' };',
  12506. ' this.Create = function () {',
  12507. ' var iC = null;',
  12508. ' iC = $impl.TIntClass.$create("Create$1");',
  12509. ' $impl.TIntClass.DoGlob();',
  12510. ' iC.$class.DoGlob();',
  12511. ' return this;',
  12512. ' };',
  12513. '});',
  12514. '']),
  12515. LinesToStr([ // $mod.$main
  12516. '$impl.TIntClass.DoGlob();',
  12517. '']),
  12518. LinesToStr([
  12519. 'rtl.createClass($impl, "TIntClass", $mod.TObject, function () {',
  12520. ' this.Create$1 = function () {',
  12521. ' $mod.TObject.Create.call(this);',
  12522. ' $mod.TObject.Create.call(this);',
  12523. ' this.$class.DoGlob();',
  12524. ' return this;',
  12525. ' };',
  12526. ' this.DoGlob = function () {',
  12527. ' };',
  12528. '});',
  12529. '']));
  12530. end;
  12531. procedure TTestModule.TestClass_Inheritance;
  12532. begin
  12533. StartProgram(false);
  12534. Add('type');
  12535. Add(' TObject = class');
  12536. Add(' public');
  12537. Add(' constructor Create;');
  12538. Add(' end;');
  12539. Add(' TClassA = class');
  12540. Add(' end;');
  12541. Add(' TClassB = class(TObject)');
  12542. Add(' procedure ProcB;');
  12543. Add(' end;');
  12544. Add('constructor tobject.create; begin end;');
  12545. Add('procedure tclassb.procb; begin end;');
  12546. Add('var');
  12547. Add(' oO: TObject;');
  12548. Add(' oA: TClassA;');
  12549. Add(' oB: TClassB;');
  12550. Add('begin');
  12551. Add(' oO:=tobject.Create;');
  12552. Add(' oA:=tclassa.Create;');
  12553. Add(' ob:=tclassb.Create;');
  12554. Add(' if oo is tclassa then ;');
  12555. Add(' ob:=oo as tclassb;');
  12556. Add(' (oo as tclassb).procb;');
  12557. ConvertProgram;
  12558. CheckSource('TestClass_Inheritance',
  12559. LinesToStr([ // statements
  12560. 'rtl.createClass(this,"TObject",null,function(){',
  12561. ' this.$init = function () {',
  12562. ' };',
  12563. ' this.$final = function () {',
  12564. ' };',
  12565. ' this.Create = function () {',
  12566. ' return this;',
  12567. ' };',
  12568. '});',
  12569. 'rtl.createClass(this,"TClassA",this.TObject,function(){',
  12570. '});',
  12571. 'rtl.createClass(this,"TClassB",this.TObject,function(){',
  12572. ' this.ProcB = function () {',
  12573. ' };',
  12574. '});',
  12575. 'this.oO = null;',
  12576. 'this.oA = null;',
  12577. 'this.oB = null;'
  12578. ]),
  12579. LinesToStr([ // $mod.$main
  12580. '$mod.oO = $mod.TObject.$create("Create");',
  12581. '$mod.oA = $mod.TClassA.$create("Create");',
  12582. '$mod.oB = $mod.TClassB.$create("Create");',
  12583. 'if ($mod.TClassA.isPrototypeOf($mod.oO));',
  12584. '$mod.oB = rtl.as($mod.oO, $mod.TClassB);',
  12585. 'rtl.as($mod.oO, $mod.TClassB).ProcB();'
  12586. ]));
  12587. end;
  12588. procedure TTestModule.TestClass_TypeAlias;
  12589. begin
  12590. StartProgram(false);
  12591. Add([
  12592. '{$interfaces corba}',
  12593. 'type',
  12594. ' IObject = interface',
  12595. ' end;',
  12596. ' IBird = type IObject;',
  12597. ' TObject = class',
  12598. ' end;',
  12599. ' TBird = type TObject;',
  12600. 'var',
  12601. ' oObj: TObject;',
  12602. ' oBird: TBird;',
  12603. ' IntfObj: IObject;',
  12604. ' IntfBird: IBird;',
  12605. 'begin',
  12606. ' oObj:=oBird;',
  12607. '']);
  12608. ConvertProgram;
  12609. CheckSource('TestClass_TypeAlias',
  12610. LinesToStr([ // statements
  12611. 'rtl.createInterface(this, "IObject", "{B92D5841-6F2A-306A-8000-000000000000}", [], null);',
  12612. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-387B-AE88-F10981585074}", [], this.IObject);',
  12613. 'rtl.createClass(this, "TObject", null, function () {',
  12614. ' this.$init = function () {',
  12615. ' };',
  12616. ' this.$final = function () {',
  12617. ' };',
  12618. '});',
  12619. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  12620. '});',
  12621. 'this.oObj = null;',
  12622. 'this.oBird = null;',
  12623. 'this.IntfObj = null;',
  12624. 'this.IntfBird = null;',
  12625. '']),
  12626. LinesToStr([ // $mod.$main
  12627. '$mod.oObj = $mod.oBird;',
  12628. '']));
  12629. end;
  12630. procedure TTestModule.TestClass_AbstractMethod;
  12631. begin
  12632. StartProgram(false);
  12633. Add('type');
  12634. Add(' TObject = class');
  12635. Add(' public');
  12636. Add(' procedure DoIt; virtual; abstract;');
  12637. Add(' end;');
  12638. Add('begin');
  12639. ConvertProgram;
  12640. CheckSource('TestClass_AbstractMethod',
  12641. LinesToStr([ // statements
  12642. 'rtl.createClass(this,"TObject",null,function(){',
  12643. ' this.$init = function () {',
  12644. ' };',
  12645. ' this.$final = function () {',
  12646. ' };',
  12647. '});'
  12648. ]),
  12649. LinesToStr([ // this.$main
  12650. ''
  12651. ]));
  12652. end;
  12653. procedure TTestModule.TestClass_CallInherited_ProcNoParams;
  12654. begin
  12655. StartProgram(false);
  12656. Add([
  12657. 'type',
  12658. ' TObject = class',
  12659. ' procedure DoAbstract; virtual; abstract;',
  12660. ' procedure DoVirtual; virtual;',
  12661. ' procedure DoIt;',
  12662. ' end;',
  12663. ' TA = class',
  12664. ' procedure doabstract; override;',
  12665. ' procedure dovirtual; override;',
  12666. ' procedure DoSome;',
  12667. ' end;',
  12668. 'procedure tobject.dovirtual;',
  12669. 'begin',
  12670. ' inherited; // call non existing ancestor -> ignore silently',
  12671. 'end;',
  12672. 'procedure tobject.doit;',
  12673. 'begin',
  12674. 'end;',
  12675. 'procedure ta.doabstract;',
  12676. 'begin',
  12677. ' inherited dovirtual; // call TObject.DoVirtual',
  12678. 'end;',
  12679. 'procedure ta.dovirtual;',
  12680. 'begin',
  12681. ' inherited; // call TObject.DoVirtual',
  12682. ' inherited dovirtual; // call TObject.DoVirtual',
  12683. ' inherited dovirtual(); // call TObject.DoVirtual',
  12684. ' doit;',
  12685. ' doit();',
  12686. 'end;',
  12687. 'procedure ta.dosome;',
  12688. 'begin',
  12689. ' inherited; // call non existing ancestor method -> silently ignore',
  12690. 'end;',
  12691. 'begin']);
  12692. ConvertProgram;
  12693. CheckSource('TestClass_CallInherited_ProcNoParams',
  12694. LinesToStr([ // statements
  12695. 'rtl.createClass(this,"TObject",null,function(){',
  12696. ' this.$init = function () {',
  12697. ' };',
  12698. ' this.$final = function () {',
  12699. ' };',
  12700. ' this.DoVirtual = function () {',
  12701. ' };',
  12702. ' this.DoIt = function () {',
  12703. ' };',
  12704. '});',
  12705. 'rtl.createClass(this, "TA", this.TObject, function () {',
  12706. ' this.DoAbstract = function () {',
  12707. ' $mod.TObject.DoVirtual.call(this);',
  12708. ' };',
  12709. ' this.DoVirtual = function () {',
  12710. ' $mod.TObject.DoVirtual.call(this);',
  12711. ' $mod.TObject.DoVirtual.call(this);',
  12712. ' $mod.TObject.DoVirtual.call(this);',
  12713. ' this.DoIt();',
  12714. ' this.DoIt();',
  12715. ' };',
  12716. ' this.DoSome = function () {',
  12717. ' };',
  12718. '});'
  12719. ]),
  12720. LinesToStr([ // this.$main
  12721. ''
  12722. ]));
  12723. end;
  12724. procedure TTestModule.TestClass_CallInherited_WithParams;
  12725. begin
  12726. StartProgram(false);
  12727. Add([
  12728. 'type',
  12729. ' TObject = class',
  12730. ' procedure DoAbstract(pA: longint; pB: longint = 0); virtual; abstract;',
  12731. ' procedure DoVirtual(pA: longint; pB: longint = 0); virtual;',
  12732. ' procedure DoIt(pA: longint; pB: longint = 0);',
  12733. ' procedure DoIt2(pA: longint = 1; pB: longint = 2);',
  12734. ' function GetIt(pA: longint = 1; pB: longint = 2): longint;',
  12735. ' end;',
  12736. ' TClassA = class',
  12737. ' procedure DoAbstract(pA: longint; pB: longint = 0); override;',
  12738. ' procedure DoVirtual(pA: longint; pB: longint = 0); override;',
  12739. ' function GetIt(pA: longint = 1; pB: longint = 2): longint;',
  12740. ' end;',
  12741. 'procedure tobject.dovirtual(pa: longint; pb: longint = 0);',
  12742. 'begin',
  12743. 'end;',
  12744. 'procedure tobject.doit(pa: longint; pb: longint = 0);',
  12745. 'begin',
  12746. 'end;',
  12747. 'procedure tobject.doit2(pa: longint; pb: longint = 0);',
  12748. 'begin',
  12749. 'end;',
  12750. 'function tobject.getit(pa: longint; pb: longint = 0): longint;',
  12751. 'begin',
  12752. 'end;',
  12753. 'procedure tclassa.doabstract(pa: longint; pb: longint = 0);',
  12754. 'begin',
  12755. ' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)',
  12756. ' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)',
  12757. 'end;',
  12758. 'procedure tclassa.dovirtual(pa: longint; pb: longint = 0);',
  12759. 'begin',
  12760. ' inherited; // call TObject.DoVirtual(pA,pB)',
  12761. ' inherited dovirtual(pa,pb); // call TObject.DoVirtual(pA,pB)',
  12762. ' inherited dovirtual(pa); // call TObject.DoVirtual(pA,0)',
  12763. ' doit(pa,pb);',
  12764. ' doit(pa);',
  12765. ' doit2(pa);',
  12766. ' doit2;',
  12767. 'end;',
  12768. 'function tclassa.getit(pa: longint; pb: longint = 0): longint;',
  12769. 'begin',
  12770. ' pa:=inherited;',
  12771. 'end;',
  12772. 'begin']);
  12773. ConvertProgram;
  12774. CheckSource('TestClass_CallInherited_WithParams',
  12775. LinesToStr([ // statements
  12776. 'rtl.createClass(this,"TObject",null,function(){',
  12777. ' this.$init = function () {',
  12778. ' };',
  12779. ' this.$final = function () {',
  12780. ' };',
  12781. ' this.DoVirtual = function (pA,pB) {',
  12782. ' };',
  12783. ' this.DoIt = function (pA,pB) {',
  12784. ' };',
  12785. ' this.DoIt2 = function (pA,pB) {',
  12786. ' };',
  12787. ' this.GetIt = function (pA, pB) {',
  12788. ' var Result = 0;',
  12789. ' return Result;',
  12790. ' };',
  12791. '});',
  12792. 'rtl.createClass(this, "TClassA", this.TObject, function () {',
  12793. ' this.DoAbstract = function (pA,pB) {',
  12794. ' $mod.TObject.DoVirtual.call(this,pA,pB);',
  12795. ' $mod.TObject.DoVirtual.call(this,pA,0);',
  12796. ' };',
  12797. ' this.DoVirtual = function (pA,pB) {',
  12798. ' $mod.TObject.DoVirtual.apply(this, arguments);',
  12799. ' $mod.TObject.DoVirtual.call(this,pA,pB);',
  12800. ' $mod.TObject.DoVirtual.call(this,pA,0);',
  12801. ' this.DoIt(pA,pB);',
  12802. ' this.DoIt(pA,0);',
  12803. ' this.DoIt2(pA,2);',
  12804. ' this.DoIt2(1,2);',
  12805. ' };',
  12806. ' this.GetIt$1 = function (pA, pB) {',
  12807. ' var Result = 0;',
  12808. ' pA = $mod.TObject.GetIt.apply(this, arguments);',
  12809. ' return Result;',
  12810. ' };',
  12811. '});'
  12812. ]),
  12813. LinesToStr([ // this.$main
  12814. ''
  12815. ]));
  12816. end;
  12817. procedure TTestModule.TestClasS_CallInheritedConstructor;
  12818. begin
  12819. StartProgram(false);
  12820. Add('type');
  12821. Add(' TObject = class');
  12822. Add(' constructor Create; virtual;');
  12823. Add(' constructor CreateWithB(b: boolean);');
  12824. Add(' end;');
  12825. Add(' TA = class');
  12826. Add(' constructor Create; override;');
  12827. Add(' constructor CreateWithC(c: char);');
  12828. Add(' procedure DoIt;');
  12829. Add(' class function DoSome: TObject;');
  12830. Add(' end;');
  12831. Add('constructor tobject.create;');
  12832. Add('begin');
  12833. Add(' inherited; // call non existing ancestor -> ignore silently');
  12834. Add('end;');
  12835. Add('constructor tobject.createwithb(b: boolean);');
  12836. Add('begin');
  12837. Add(' inherited; // call non existing ancestor -> ignore silently');
  12838. Add(' create; // normal call');
  12839. Add('end;');
  12840. Add('constructor ta.create;');
  12841. Add('begin');
  12842. Add(' inherited; // normal call TObject.Create');
  12843. Add(' inherited create; // normal call TObject.Create');
  12844. Add(' inherited createwithb(false); // normal call TObject.CreateWithB');
  12845. Add('end;');
  12846. Add('constructor ta.createwithc(c: char);');
  12847. Add('begin');
  12848. Add(' inherited create; // call TObject.Create');
  12849. Add(' inherited createwithb(true); // call TObject.CreateWithB');
  12850. Add(' doit;');
  12851. Add(' doit();');
  12852. Add(' dosome;');
  12853. Add('end;');
  12854. Add('procedure ta.doit;');
  12855. Add('begin');
  12856. Add(' create; // normal call');
  12857. Add(' createwithb(false); // normal call');
  12858. Add(' createwithc(''c''); // normal call');
  12859. Add('end;');
  12860. Add('class function ta.dosome: TObject;');
  12861. Add('begin');
  12862. Add(' Result:=create; // constructor');
  12863. Add(' Result:=createwithb(true); // constructor');
  12864. Add(' Result:=createwithc(''c''); // constructor');
  12865. Add('end;');
  12866. Add('begin');
  12867. ConvertProgram;
  12868. CheckSource('TestClass_CallInheritedConstructor',
  12869. LinesToStr([ // statements
  12870. 'rtl.createClass(this,"TObject",null,function(){',
  12871. ' this.$init = function () {',
  12872. ' };',
  12873. ' this.$final = function () {',
  12874. ' };',
  12875. ' this.Create = function () {',
  12876. ' return this;',
  12877. ' };',
  12878. ' this.CreateWithB = function (b) {',
  12879. ' this.Create();',
  12880. ' return this;',
  12881. ' };',
  12882. '});',
  12883. 'rtl.createClass(this, "TA", this.TObject, function () {',
  12884. ' this.Create = function () {',
  12885. ' $mod.TObject.Create.call(this);',
  12886. ' $mod.TObject.Create.call(this);',
  12887. ' $mod.TObject.CreateWithB.call(this, false);',
  12888. ' return this;',
  12889. ' };',
  12890. ' this.CreateWithC = function (c) {',
  12891. ' $mod.TObject.Create.call(this);',
  12892. ' $mod.TObject.CreateWithB.call(this, true);',
  12893. ' this.DoIt();',
  12894. ' this.DoIt();',
  12895. ' this.$class.DoSome();',
  12896. ' return this;',
  12897. ' };',
  12898. ' this.DoIt = function () {',
  12899. ' this.Create();',
  12900. ' this.CreateWithB(false);',
  12901. ' this.CreateWithC("c");',
  12902. ' };',
  12903. ' this.DoSome = function () {',
  12904. ' var Result = null;',
  12905. ' Result = this.$create("Create");',
  12906. ' Result = this.$create("CreateWithB", [true]);',
  12907. ' Result = this.$create("CreateWithC", ["c"]);',
  12908. ' return Result;',
  12909. ' };',
  12910. '});'
  12911. ]),
  12912. LinesToStr([ // this.$main
  12913. ''
  12914. ]));
  12915. end;
  12916. procedure TTestModule.TestClass_ClassVar_Assign;
  12917. begin
  12918. StartProgram(false);
  12919. Add([
  12920. 'type',
  12921. ' TObject = class',
  12922. ' public',
  12923. ' class var vI: longint;',
  12924. ' class var Sub: TObject;',
  12925. ' constructor Create;',
  12926. ' class function GetIt(var Par: longint): tobject;',
  12927. ' end;',
  12928. 'constructor tobject.create;',
  12929. 'begin',
  12930. ' vi:=vi+1;',
  12931. ' Self.vi:=Self.vi+1;',
  12932. ' inc(vi);',
  12933. 'end;',
  12934. 'class function tobject.getit(var par: longint): tobject;',
  12935. 'begin',
  12936. ' vi:=vi+3;',
  12937. ' Self.vi:=Self.vi+4;',
  12938. ' inc(vi);',
  12939. ' Result:=self.sub;',
  12940. ' GetIt(vi);',
  12941. 'end;',
  12942. 'var Obj: tobject;',
  12943. 'begin',
  12944. ' obj:=tobject.create;',
  12945. ' tobject.vi:=3;',
  12946. ' if tobject.vi=4 then ;',
  12947. ' tobject.sub:=nil;',
  12948. ' obj.sub:=nil;',
  12949. ' obj.sub.sub:=nil;']);
  12950. ConvertProgram;
  12951. CheckSource('TestClass_ClassVar_Assign',
  12952. LinesToStr([ // statements
  12953. 'rtl.createClass(this,"TObject",null,function(){',
  12954. ' this.vI = 0;',
  12955. ' this.Sub = null;',
  12956. ' this.$init = function () {',
  12957. ' };',
  12958. ' this.$final = function () {',
  12959. ' };',
  12960. ' this.Create = function(){',
  12961. ' $mod.TObject.vI = this.vI+1;',
  12962. ' $mod.TObject.vI = this.vI+1;',
  12963. ' $mod.TObject.vI += 1;',
  12964. ' return this;',
  12965. ' };',
  12966. ' this.GetIt = function(Par){',
  12967. ' var Result = null;',
  12968. ' $mod.TObject.vI = this.vI + 3;',
  12969. ' $mod.TObject.vI = this.vI + 4;',
  12970. ' $mod.TObject.vI += 1;',
  12971. ' Result = this.Sub;',
  12972. ' this.GetIt({',
  12973. ' p: $mod.TObject,',
  12974. ' get: function () {',
  12975. ' return this.p.vI;',
  12976. ' },',
  12977. ' set: function (v) {',
  12978. ' this.p.vI = v;',
  12979. ' }',
  12980. ' });',
  12981. ' return Result;',
  12982. ' };',
  12983. '});',
  12984. 'this.Obj = null;'
  12985. ]),
  12986. LinesToStr([ // $mod.$main
  12987. '$mod.Obj = $mod.TObject.$create("Create");',
  12988. '$mod.TObject.vI = 3;',
  12989. 'if ($mod.TObject.vI === 4);',
  12990. '$mod.TObject.Sub=null;',
  12991. '$mod.TObject.Sub=null;',
  12992. '$mod.TObject.Sub=null;',
  12993. '']));
  12994. end;
  12995. procedure TTestModule.TestClass_CallClassMethod;
  12996. begin
  12997. StartProgram(false);
  12998. Add('type');
  12999. Add(' TObject = class');
  13000. Add(' public');
  13001. Add(' class var vI: longint;');
  13002. Add(' class var Sub: TObject;');
  13003. Add(' constructor Create;');
  13004. Add(' function GetMore(Par: longint): longint;');
  13005. Add(' class function GetIt(Par: longint): tobject;');
  13006. Add(' end;');
  13007. Add('constructor tobject.create;');
  13008. Add('begin');
  13009. Add(' sub:=getit(3);');
  13010. Add(' vi:=getmore(4);');
  13011. Add(' sub:=Self.getit(5);');
  13012. Add(' vi:=Self.getmore(6);');
  13013. Add('end;');
  13014. Add('function tobject.getmore(par: longint): longint;');
  13015. Add('begin');
  13016. Add(' sub:=getit(11);');
  13017. Add(' vi:=getmore(12);');
  13018. Add(' sub:=self.getit(13);');
  13019. Add(' vi:=self.getmore(14);');
  13020. Add('end;');
  13021. Add('class function tobject.getit(par: longint): tobject;');
  13022. Add('begin');
  13023. Add(' sub:=getit(21);');
  13024. Add(' vi:=sub.getmore(22);');
  13025. Add(' sub:=self.getit(23);');
  13026. Add(' vi:=self.sub.getmore(24);');
  13027. Add('end;');
  13028. Add('var Obj: tobject;');
  13029. Add('begin');
  13030. Add(' obj:=tobject.create;');
  13031. Add(' tobject.getit(5);');
  13032. Add(' obj.getit(6);');
  13033. Add(' obj.sub.getit(7);');
  13034. Add(' obj.sub.getit(8).SUB:=nil;');
  13035. Add(' obj.sub.getit(9).GETIT(10);');
  13036. Add(' obj.sub.getit(11).SuB.getit(12);');
  13037. ConvertProgram;
  13038. CheckSource('TestClass_CallClassMethod',
  13039. LinesToStr([ // statements
  13040. 'rtl.createClass(this,"TObject",null,function(){',
  13041. ' this.vI = 0;',
  13042. ' this.Sub = null;',
  13043. ' this.$init = function () {',
  13044. ' };',
  13045. ' this.$final = function () {',
  13046. ' };',
  13047. ' this.Create = function(){',
  13048. ' $mod.TObject.Sub = this.$class.GetIt(3);',
  13049. ' $mod.TObject.vI = this.GetMore(4);',
  13050. ' $mod.TObject.Sub = this.$class.GetIt(5);',
  13051. ' $mod.TObject.vI = this.GetMore(6);',
  13052. ' return this;',
  13053. ' };',
  13054. ' this.GetMore = function(Par){',
  13055. ' var Result = 0;',
  13056. ' $mod.TObject.Sub = this.$class.GetIt(11);',
  13057. ' $mod.TObject.vI = this.GetMore(12);',
  13058. ' $mod.TObject.Sub = this.$class.GetIt(13);',
  13059. ' $mod.TObject.vI = this.GetMore(14);',
  13060. ' return Result;',
  13061. ' };',
  13062. ' this.GetIt = function(Par){',
  13063. ' var Result = null;',
  13064. ' $mod.TObject.Sub = this.GetIt(21);',
  13065. ' $mod.TObject.vI = this.Sub.GetMore(22);',
  13066. ' $mod.TObject.Sub = this.GetIt(23);',
  13067. ' $mod.TObject.vI = this.Sub.GetMore(24);',
  13068. ' return Result;',
  13069. ' };',
  13070. '});',
  13071. 'this.Obj = null;'
  13072. ]),
  13073. LinesToStr([ // $mod.$main
  13074. '$mod.Obj = $mod.TObject.$create("Create");',
  13075. '$mod.TObject.GetIt(5);',
  13076. '$mod.Obj.$class.GetIt(6);',
  13077. '$mod.Obj.Sub.$class.GetIt(7);',
  13078. '$mod.TObject.Sub=null;',
  13079. '$mod.Obj.Sub.$class.GetIt(9).$class.GetIt(10);',
  13080. '$mod.Obj.Sub.$class.GetIt(11).Sub.$class.GetIt(12);',
  13081. '']));
  13082. end;
  13083. procedure TTestModule.TestClass_CallClassMethodStatic;
  13084. begin
  13085. StartProgram(false);
  13086. Add([
  13087. 'type',
  13088. ' TObject = class',
  13089. ' public',
  13090. ' class function Fly: tobject; static;',
  13091. ' end;',
  13092. 'class function tobject.Fly: tobject;',
  13093. 'begin',
  13094. ' Result.Fly;',
  13095. ' Result.Fly();',
  13096. ' Fly;',
  13097. ' Fly();',
  13098. ' Fly.Fly;',
  13099. ' Fly.Fly();',
  13100. 'end;',
  13101. 'var Obj: tobject;',
  13102. 'begin',
  13103. ' obj.Fly;',
  13104. ' obj.Fly();',
  13105. ' with obj do begin',
  13106. ' Fly;',
  13107. ' Fly();',
  13108. ' end;',
  13109. '']);
  13110. ConvertProgram;
  13111. CheckSource('TestClass_CallClassMethodStatic',
  13112. LinesToStr([ // statements
  13113. 'rtl.createClass(this, "TObject", null, function () {',
  13114. ' this.$init = function () {',
  13115. ' };',
  13116. ' this.$final = function () {',
  13117. ' };',
  13118. ' this.Fly = function () {',
  13119. ' var Result = null;',
  13120. ' $mod.TObject.Fly();',
  13121. ' $mod.TObject.Fly();',
  13122. ' $mod.TObject.Fly();',
  13123. ' $mod.TObject.Fly();',
  13124. ' $mod.TObject.Fly();',
  13125. ' $mod.TObject.Fly();',
  13126. ' return Result;',
  13127. ' };',
  13128. '});',
  13129. 'this.Obj = null;'
  13130. ]),
  13131. LinesToStr([ // $mod.$main
  13132. '$mod.TObject.Fly();',
  13133. '$mod.TObject.Fly();',
  13134. 'var $with = $mod.Obj;',
  13135. '$with.Fly();',
  13136. '$with.Fly();',
  13137. '']));
  13138. end;
  13139. procedure TTestModule.TestClass_Property;
  13140. begin
  13141. StartProgram(false);
  13142. Add('type');
  13143. Add(' TObject = class');
  13144. Add(' Fx: longint;');
  13145. Add(' Fy: longint;');
  13146. Add(' function GetInt: longint;');
  13147. Add(' procedure SetInt(Value: longint);');
  13148. Add(' procedure DoIt;');
  13149. Add(' property IntA: longint read Fx write Fy;');
  13150. Add(' property IntB: longint read GetInt write SetInt;');
  13151. Add(' end;');
  13152. Add('function tobject.getint: longint;');
  13153. Add('begin');
  13154. Add(' result:=fx;');
  13155. Add('end;');
  13156. Add('procedure tobject.setint(value: longint);');
  13157. Add('begin');
  13158. Add(' if value=fy then exit;');
  13159. Add(' fy:=value;');
  13160. Add('end;');
  13161. Add('procedure tobject.doit;');
  13162. Add('begin');
  13163. Add(' IntA:=IntA+1;');
  13164. Add(' Self.IntA:=Self.IntA+1;');
  13165. Add(' IntB:=IntB+1;');
  13166. Add(' Self.IntB:=Self.IntB+1;');
  13167. Add('end;');
  13168. Add('var Obj: tobject;');
  13169. Add('begin');
  13170. Add(' obj.inta:=obj.inta+1;');
  13171. Add(' if obj.intb=2 then;');
  13172. Add(' obj.intb:=obj.intb+2;');
  13173. Add(' obj.setint(obj.inta);');
  13174. ConvertProgram;
  13175. CheckSource('TestClass_Property',
  13176. LinesToStr([ // statements
  13177. 'rtl.createClass(this, "TObject", null, function () {',
  13178. ' this.$init = function () {',
  13179. ' this.Fx = 0;',
  13180. ' this.Fy = 0;',
  13181. ' };',
  13182. ' this.$final = function () {',
  13183. ' };',
  13184. ' this.GetInt = function () {',
  13185. ' var Result = 0;',
  13186. ' Result = this.Fx;',
  13187. ' return Result;',
  13188. ' };',
  13189. ' this.SetInt = function (Value) {',
  13190. ' if (Value === this.Fy) return;',
  13191. ' this.Fy = Value;',
  13192. ' };',
  13193. ' this.DoIt = function () {',
  13194. ' this.Fy = this.Fx + 1;',
  13195. ' this.Fy = this.Fx + 1;',
  13196. ' this.SetInt(this.GetInt() + 1);',
  13197. ' this.SetInt(this.GetInt() + 1);',
  13198. ' };',
  13199. '});',
  13200. 'this.Obj = null;'
  13201. ]),
  13202. LinesToStr([ // $mod.$main
  13203. '$mod.Obj.Fy = $mod.Obj.Fx + 1;',
  13204. 'if ($mod.Obj.GetInt() === 2);',
  13205. '$mod.Obj.SetInt($mod.Obj.GetInt() + 2);',
  13206. '$mod.Obj.SetInt($mod.Obj.Fx);'
  13207. ]));
  13208. end;
  13209. procedure TTestModule.TestClass_Property_ClassMethod;
  13210. begin
  13211. StartProgram(false);
  13212. Add([
  13213. 'type',
  13214. ' TObject = class',
  13215. ' class var Fx: longint;',
  13216. ' class var Fy: longint;',
  13217. ' class function GetInt: longint;',
  13218. ' class procedure SetInt(Value: longint);',
  13219. ' end;',
  13220. ' TBird = class',
  13221. ' class procedure DoIt;',
  13222. ' class property IntA: longint read Fx write Fy;',
  13223. ' class property IntB: longint read GetInt write SetInt;',
  13224. ' end;',
  13225. 'class function tobject.getint: longint;',
  13226. 'begin',
  13227. ' result:=fx;',
  13228. 'end;',
  13229. 'class procedure tobject.setint(value: longint);',
  13230. 'begin',
  13231. 'end;',
  13232. 'class procedure tbird.doit;',
  13233. 'begin',
  13234. ' FX:=3;',
  13235. ' IntA:=IntA+1;',
  13236. ' Self.IntA:=Self.IntA+1;',
  13237. ' IntB:=IntB+1;',
  13238. ' Self.IntB:=Self.IntB+1;',
  13239. ' with Self do begin',
  13240. ' FX:=11;',
  13241. ' IntA:=IntA+12;',
  13242. ' IntB:=IntB+13;',
  13243. ' end;',
  13244. 'end;',
  13245. 'var Obj: tbird;',
  13246. 'begin',
  13247. ' tbird.fx:=tbird.fx+1;',
  13248. ' tbird.inta:=tbird.inta+1;',
  13249. ' if tbird.intb=2 then;',
  13250. ' tbird.intb:=tbird.intb+2;',
  13251. ' tbird.setint(tbird.inta);',
  13252. ' obj.inta:=obj.inta+1;',
  13253. ' if obj.intb=2 then;',
  13254. ' obj.intb:=obj.intb+2;',
  13255. ' obj.setint(obj.inta);',
  13256. ' with Tbird do begin',
  13257. ' FX:=FY+1;',
  13258. ' inta:=inta+2;',
  13259. ' intb:=intb+3;',
  13260. ' end;',
  13261. ' with Obj do begin',
  13262. ' FX:=FY+1;',
  13263. ' inta:=inta+2;',
  13264. ' intb:=intb+3;',
  13265. ' end;',
  13266. '']);
  13267. ConvertProgram;
  13268. CheckSource('TestClass_Property_ClassMethod',
  13269. LinesToStr([ // statements
  13270. 'rtl.createClass(this, "TObject", null, function () {',
  13271. ' this.Fx = 0;',
  13272. ' this.Fy = 0;',
  13273. ' this.$init = function () {',
  13274. ' };',
  13275. ' this.$final = function () {',
  13276. ' };',
  13277. ' this.GetInt = function () {',
  13278. ' var Result = 0;',
  13279. ' Result = this.Fx;',
  13280. ' return Result;',
  13281. ' };',
  13282. ' this.SetInt = function (Value) {',
  13283. ' };',
  13284. '});',
  13285. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  13286. ' this.DoIt = function () {',
  13287. ' $mod.TObject.Fx = 3;',
  13288. ' $mod.TObject.Fy = this.Fx + 1;',
  13289. ' $mod.TObject.Fy = this.Fx + 1;',
  13290. ' this.SetInt(this.GetInt() + 1);',
  13291. ' this.SetInt(this.GetInt() + 1);',
  13292. ' $mod.TObject.Fx = 11;',
  13293. ' $mod.TObject.Fy = this.Fx + 12;',
  13294. ' this.SetInt(this.GetInt() + 13);',
  13295. ' };',
  13296. '});',
  13297. 'this.Obj = null;'
  13298. ]),
  13299. LinesToStr([ // $mod.$main
  13300. '$mod.TObject.Fx = $mod.TBird.Fx + 1;',
  13301. '$mod.TObject.Fy = $mod.TBird.Fx + 1;',
  13302. 'if ($mod.TBird.GetInt() === 2);',
  13303. '$mod.TBird.SetInt($mod.TBird.GetInt() + 2);',
  13304. '$mod.TBird.SetInt($mod.TBird.Fx);',
  13305. '$mod.TObject.Fy = $mod.Obj.Fx + 1;',
  13306. 'if ($mod.Obj.$class.GetInt() === 2);',
  13307. '$mod.Obj.$class.SetInt($mod.Obj.$class.GetInt() + 2);',
  13308. '$mod.Obj.$class.SetInt($mod.Obj.Fx);',
  13309. 'var $with = $mod.TBird;',
  13310. '$mod.TObject.Fx = $with.Fy + 1;',
  13311. '$mod.TObject.Fy = $with.Fx + 2;',
  13312. '$with.SetInt($with.GetInt() + 3);',
  13313. 'var $with1 = $mod.Obj;',
  13314. '$mod.TObject.Fx = $with1.Fy + 1;',
  13315. '$mod.TObject.Fy = $with1.Fx + 2;',
  13316. '$with1.$class.SetInt($with1.$class.GetInt() + 3);',
  13317. '']));
  13318. end;
  13319. procedure TTestModule.TestClass_Property_Indexed;
  13320. begin
  13321. StartProgram(false);
  13322. Add([
  13323. 'type',
  13324. ' TObject = class',
  13325. ' FItems: array of longint;',
  13326. ' function GetItems(Index: longint): longint;',
  13327. ' procedure SetItems(Index: longint; Value: longint);',
  13328. ' procedure DoIt;',
  13329. ' property Items[Index: longint]: longint read getitems write setitems;',
  13330. ' end;',
  13331. 'function tobject.getitems(index: longint): longint;',
  13332. 'begin',
  13333. ' Result:=fitems[index];',
  13334. 'end;',
  13335. 'procedure tobject.setitems(index: longint; value: longint);',
  13336. 'begin',
  13337. ' fitems[index]:=value;',
  13338. 'end;',
  13339. 'procedure tobject.doit;',
  13340. 'begin',
  13341. ' items[1]:=2;',
  13342. ' items[3]:=items[4];',
  13343. ' self.items[5]:=self.items[6];',
  13344. ' items[items[7]]:=items[items[8]];',
  13345. 'end;',
  13346. 'var Obj: tobject;',
  13347. 'begin',
  13348. ' obj.Items[11]:=obj.Items[12];',
  13349. '']);
  13350. ConvertProgram;
  13351. CheckSource('TestClass_Property_Indexed',
  13352. LinesToStr([ // statements
  13353. 'rtl.createClass(this, "TObject", null, function () {',
  13354. ' this.$init = function () {',
  13355. ' this.FItems = [];',
  13356. ' };',
  13357. ' this.$final = function () {',
  13358. ' this.FItems = undefined;',
  13359. ' };',
  13360. ' this.GetItems = function (Index) {',
  13361. ' var Result = 0;',
  13362. ' Result = this.FItems[Index];',
  13363. ' return Result;',
  13364. ' };',
  13365. ' this.SetItems = function (Index, Value) {',
  13366. ' this.FItems[Index] = Value;',
  13367. ' };',
  13368. ' this.DoIt = function () {',
  13369. ' this.SetItems(1, 2);',
  13370. ' this.SetItems(3,this.GetItems(4));',
  13371. ' this.SetItems(5,this.GetItems(6));',
  13372. ' this.SetItems(this.GetItems(7), this.GetItems(this.GetItems(8)));',
  13373. ' };',
  13374. '});',
  13375. 'this.Obj = null;'
  13376. ]),
  13377. LinesToStr([ // $mod.$main
  13378. '$mod.Obj.SetItems(11,$mod.Obj.GetItems(12));'
  13379. ]));
  13380. end;
  13381. procedure TTestModule.TestClass_Property_IndexSpec;
  13382. begin
  13383. StartProgram(false);
  13384. Add([
  13385. 'type',
  13386. ' TEnum = (red, blue);',
  13387. ' TObject = class',
  13388. ' function GetIntBool(Index: longint): boolean; virtual; abstract;',
  13389. ' procedure SetIntBool(Index: longint; b: boolean); virtual; abstract;',
  13390. ' function GetEnumBool(Index: TEnum): boolean; virtual; abstract;',
  13391. ' procedure SetEnumBool(Index: TEnum; b: boolean); virtual; abstract;',
  13392. ' function GetStrIntBool(A: String; I: longint): boolean; virtual; abstract;',
  13393. ' procedure SetStrIntBool(A: String; I: longint; b: boolean); virtual; abstract;',
  13394. ' property B1: boolean index 1 read GetIntBool write SetIntBool;',
  13395. ' property B2: boolean index TEnum.blue read GetEnumBool write SetEnumBool;',
  13396. ' property B3: boolean index ord(red) read GetIntBool write SetIntBool;',
  13397. ' property I1[A: String]: boolean index ord(blue) read GetStrIntBool write SetStrIntBool;',
  13398. ' end;',
  13399. 'procedure DoIt(b: boolean); begin end;',
  13400. 'var',
  13401. ' o: TObject;',
  13402. 'begin',
  13403. ' o.B1:=o.B1;',
  13404. ' o.B2:=o.B2;',
  13405. ' o.B3:=o.B3;',
  13406. ' o.I1[''a'']:=o.I1[''b''];',
  13407. ' doit(o.b1);',
  13408. ' doit(o.b2);',
  13409. ' doit(o.i1[''c'']);',
  13410. '']);
  13411. ConvertProgram;
  13412. CheckSource('TestClass_Property_IndexSpec',
  13413. LinesToStr([ // statements
  13414. 'this.TEnum = {',
  13415. ' "0": "red",',
  13416. ' red: 0,',
  13417. ' "1": "blue",',
  13418. ' blue: 1',
  13419. '};',
  13420. 'rtl.createClass(this, "TObject", null, function () {',
  13421. ' this.$init = function () {',
  13422. ' };',
  13423. ' this.$final = function () {',
  13424. ' };',
  13425. '});',
  13426. 'this.DoIt = function (b) {',
  13427. '};',
  13428. 'this.o = null;',
  13429. '']),
  13430. LinesToStr([ // $mod.$main
  13431. '$mod.o.SetIntBool(1, $mod.o.GetIntBool(1));',
  13432. '$mod.o.SetEnumBool($mod.TEnum.blue, $mod.o.GetEnumBool($mod.TEnum.blue));',
  13433. '$mod.o.SetIntBool(0, $mod.o.GetIntBool(0));',
  13434. '$mod.o.SetStrIntBool("a", 1, $mod.o.GetStrIntBool("b", 1));',
  13435. '$mod.DoIt($mod.o.GetIntBool(1));',
  13436. '$mod.DoIt($mod.o.GetEnumBool($mod.TEnum.blue));',
  13437. '$mod.DoIt($mod.o.GetStrIntBool("c", 1));',
  13438. '']));
  13439. end;
  13440. procedure TTestModule.TestClass_PropertyOfTypeArray;
  13441. begin
  13442. StartProgram(false);
  13443. Add('type');
  13444. Add(' TArray = array of longint;');
  13445. Add(' TObject = class');
  13446. Add(' FItems: TArray;');
  13447. Add(' function GetItems: tarray;');
  13448. Add(' procedure SetItems(Value: tarray);');
  13449. Add(' property Items: tarray read getitems write setitems;');
  13450. Add(' procedure SetNumbers(const Value: tarray);');
  13451. Add(' property Numbers: tarray write setnumbers;');
  13452. Add(' end;');
  13453. Add('function tobject.getitems: tarray;');
  13454. Add('begin');
  13455. Add(' Result:=fitems;');
  13456. Add('end;');
  13457. Add('procedure tobject.setitems(value: tarray);');
  13458. Add('begin');
  13459. Add(' fitems:=value;');
  13460. Add(' fitems:=nil;');
  13461. Add(' Items:=nil;');
  13462. Add(' Items:=Items;');
  13463. Add(' Items[1]:=2;');
  13464. Add(' fitems[3]:=Items[4];');
  13465. Add(' Items[5]:=Items[6];');
  13466. Add(' Self.Items[7]:=8;');
  13467. Add(' Self.Items[9]:=Self.Items[10];');
  13468. Add(' Items[Items[11]]:=Items[Items[12]];');
  13469. Add('end;');
  13470. Add('procedure tobject.SetNumbers(const Value: tarray);');
  13471. Add('begin;');
  13472. Add(' Numbers:=nil;');
  13473. Add(' Numbers:=Value;');
  13474. Add(' Self.Numbers:=Value;');
  13475. Add('end;');
  13476. Add('var Obj: tobject;');
  13477. Add('begin');
  13478. Add(' obj.items:=nil;');
  13479. Add(' obj.items:=obj.items;');
  13480. Add(' obj.items[11]:=obj.items[12];');
  13481. ConvertProgram;
  13482. CheckSource('TestClass_PropertyOfTypeArray',
  13483. LinesToStr([ // statements
  13484. 'rtl.createClass(this, "TObject", null, function () {',
  13485. ' this.$init = function () {',
  13486. ' this.FItems = [];',
  13487. ' };',
  13488. ' this.$final = function () {',
  13489. ' this.FItems = undefined;',
  13490. ' };',
  13491. ' this.GetItems = function () {',
  13492. ' var Result = [];',
  13493. ' Result = rtl.arrayRef(this.FItems);',
  13494. ' return Result;',
  13495. ' };',
  13496. ' this.SetItems = function (Value) {',
  13497. ' this.FItems = rtl.arrayRef(Value);',
  13498. ' this.FItems = [];',
  13499. ' this.SetItems([]);',
  13500. ' this.SetItems(rtl.arrayRef(this.GetItems()));',
  13501. ' this.GetItems()[1] = 2;',
  13502. ' this.FItems[3] = this.GetItems()[4];',
  13503. ' this.GetItems()[5] = this.GetItems()[6];',
  13504. ' this.GetItems()[7] = 8;',
  13505. ' this.GetItems()[9] = this.GetItems()[10];',
  13506. ' this.GetItems()[this.GetItems()[11]] = this.GetItems()[this.GetItems()[12]];',
  13507. ' };',
  13508. ' this.SetNumbers = function (Value) {',
  13509. ' this.SetNumbers([]);',
  13510. ' this.SetNumbers(Value);',
  13511. ' this.SetNumbers(Value);',
  13512. ' };',
  13513. '});',
  13514. 'this.Obj = null;'
  13515. ]),
  13516. LinesToStr([ // $mod.$main
  13517. '$mod.Obj.SetItems([]);',
  13518. '$mod.Obj.SetItems($mod.Obj.GetItems());',
  13519. '$mod.Obj.GetItems()[11] = $mod.Obj.GetItems()[12];'
  13520. ]));
  13521. end;
  13522. procedure TTestModule.TestClass_PropertyDefault;
  13523. begin
  13524. StartProgram(false);
  13525. Add([
  13526. 'type',
  13527. ' TArray = array of longint;',
  13528. ' TObject = class',
  13529. ' end;',
  13530. ' TBird = class',
  13531. ' FItems: TArray;',
  13532. ' function GetItems(Index: longint): longint;',
  13533. ' procedure SetItems(Index, Value: longint);',
  13534. ' property Items[Index: longint]: longint read getitems write setitems; default;',
  13535. ' end;',
  13536. 'function TBird.getitems(index: longint): longint;',
  13537. 'begin',
  13538. 'end;',
  13539. 'procedure TBird.setitems(index, value: longint);',
  13540. 'begin',
  13541. ' Self[1]:=2;',
  13542. ' Self[3]:=Self[index];',
  13543. ' Self[index]:=Self[Self[value]];',
  13544. ' Self[Self[4]]:=value;',
  13545. 'end;',
  13546. 'var',
  13547. ' Bird: TBird;',
  13548. ' Obj: TObject;',
  13549. 'begin',
  13550. ' bird[11]:=12;',
  13551. ' bird[13]:=bird[14];',
  13552. ' bird[Bird[15]]:=bird[Bird[15]];',
  13553. ' TBird(obj)[16]:=TBird(obj)[17];',
  13554. ' (obj as tbird)[18]:=19;',
  13555. '']);
  13556. ConvertProgram;
  13557. CheckSource('TestClass_PropertyDefault',
  13558. LinesToStr([ // statements
  13559. 'rtl.createClass(this, "TObject", null, function () {',
  13560. ' this.$init = function () {',
  13561. ' };',
  13562. ' this.$final = function () {',
  13563. ' };',
  13564. '});',
  13565. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  13566. ' this.$init = function () {',
  13567. ' $mod.TObject.$init.call(this);',
  13568. ' this.FItems = [];',
  13569. ' };',
  13570. ' this.$final = function () {',
  13571. ' this.FItems = undefined;',
  13572. ' $mod.TObject.$final.call(this);',
  13573. ' };',
  13574. ' this.GetItems = function (Index) {',
  13575. ' var Result = 0;',
  13576. ' return Result;',
  13577. ' };',
  13578. ' this.SetItems = function (Index, Value) {',
  13579. ' this.SetItems(1, 2);',
  13580. ' this.SetItems(3, this.GetItems(Index));',
  13581. ' this.SetItems(Index, this.GetItems(this.GetItems(Value)));',
  13582. ' this.SetItems(this.GetItems(4), Value);',
  13583. ' };',
  13584. '});',
  13585. 'this.Bird = null;',
  13586. 'this.Obj = null;',
  13587. '']),
  13588. LinesToStr([ // $mod.$main
  13589. '$mod.Bird.SetItems(11, 12);',
  13590. '$mod.Bird.SetItems(13, $mod.Bird.GetItems(14));',
  13591. '$mod.Bird.SetItems($mod.Bird.GetItems(15), $mod.Bird.GetItems($mod.Bird.GetItems(15)));',
  13592. '$mod.Obj.SetItems(16, $mod.Obj.GetItems(17));',
  13593. 'rtl.as($mod.Obj, $mod.TBird).SetItems(18, 19);',
  13594. '']));
  13595. end;
  13596. procedure TTestModule.TestClass_PropertyDefault_TypecastToOtherDefault;
  13597. begin
  13598. StartProgram(false);
  13599. Add([
  13600. 'type',
  13601. ' TObject = class end;',
  13602. ' TAlphaList = class',
  13603. ' function GetAlphas(Index: boolean): Pointer; virtual; abstract;',
  13604. ' procedure SetAlphas(Index: boolean; Value: Pointer); virtual; abstract;',
  13605. ' property Alphas[Index: boolean]: Pointer read getAlphas write setAlphas; default;',
  13606. ' end;',
  13607. ' TBetaList = class',
  13608. ' function GetBetas(Index: longint): Pointer; virtual; abstract;',
  13609. ' procedure SetBetas(Index: longint; Value: Pointer); virtual; abstract;',
  13610. ' property Betas[Index: longint]: Pointer read getBetas write setBetas; default;',
  13611. ' end;',
  13612. ' TBird = class',
  13613. ' procedure DoIt;',
  13614. ' end;',
  13615. 'procedure TBird.DoIt;',
  13616. 'var',
  13617. ' List: TAlphaList;',
  13618. 'begin',
  13619. ' if TBetaList(List[true])[3]=nil then ;',
  13620. ' TBetaList(List[false])[5]:=nil;',
  13621. 'end;',
  13622. 'var',
  13623. ' List: TAlphaList;',
  13624. 'begin',
  13625. ' if TBetaList(List[true])[3]=nil then ;',
  13626. ' TBetaList(List[false])[5]:=nil;',
  13627. '']);
  13628. ConvertProgram;
  13629. CheckSource('TestClass_PropertyDefault_TypecastToOtherDefault',
  13630. LinesToStr([ // statements
  13631. 'rtl.createClass(this, "TObject", null, function () {',
  13632. ' this.$init = function () {',
  13633. ' };',
  13634. ' this.$final = function () {',
  13635. ' };',
  13636. '});',
  13637. 'rtl.createClass(this, "TAlphaList", this.TObject, function () {',
  13638. '});',
  13639. 'rtl.createClass(this, "TBetaList", this.TObject, function () {',
  13640. '});',
  13641. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  13642. ' this.DoIt = function () {',
  13643. ' var List = null;',
  13644. ' if (List.GetAlphas(true).GetBetas(3) === null) ;',
  13645. ' List.GetAlphas(false).SetBetas(5, null);',
  13646. ' };',
  13647. '});',
  13648. 'this.List = null;',
  13649. '']),
  13650. LinesToStr([ // $mod.$main
  13651. 'if ($mod.List.GetAlphas(true).GetBetas(3) === null) ;',
  13652. '$mod.List.GetAlphas(false).SetBetas(5, null);',
  13653. '']));
  13654. end;
  13655. procedure TTestModule.TestClass_PropertyOverride;
  13656. begin
  13657. StartProgram(false);
  13658. Add('type');
  13659. Add(' integer = longint;');
  13660. Add(' TObject = class');
  13661. Add(' FItem: integer;');
  13662. Add(' function GetItem: integer; external name ''GetItem'';');
  13663. Add(' procedure SetItem(Value: integer); external name ''SetItem'';');
  13664. Add(' property Item: integer read getitem write setitem;');
  13665. Add(' end;');
  13666. Add(' TCar = class');
  13667. Add(' FBag: integer;');
  13668. Add(' function GetBag: integer; external name ''GetBag'';');
  13669. Add(' property Item read getbag;');
  13670. Add(' end;');
  13671. Add('var');
  13672. Add(' Obj: tobject;');
  13673. Add(' Car: tcar;');
  13674. Add('begin');
  13675. Add(' Obj.Item:=Obj.Item;');
  13676. Add(' Car.Item:=Car.Item;');
  13677. ConvertProgram;
  13678. CheckSource('TestClass_PropertyOverride',
  13679. LinesToStr([ // statements
  13680. 'rtl.createClass(this, "TObject", null, function () {',
  13681. ' this.$init = function () {',
  13682. ' this.FItem = 0;',
  13683. ' };',
  13684. ' this.$final = function () {',
  13685. ' };',
  13686. '});',
  13687. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  13688. ' this.$init = function () {',
  13689. ' $mod.TObject.$init.call(this);',
  13690. ' this.FBag = 0;',
  13691. ' };',
  13692. '});',
  13693. 'this.Obj = null;',
  13694. 'this.Car = null;',
  13695. '']),
  13696. LinesToStr([ // $mod.$main
  13697. '$mod.Obj.SetItem($mod.Obj.GetItem());',
  13698. '$mod.Car.SetItem($mod.Car.GetBag());',
  13699. '']));
  13700. end;
  13701. procedure TTestModule.TestClass_PropertyIncVisibility;
  13702. begin
  13703. AddModuleWithIntfImplSrc('unit1.pp',
  13704. LinesToStr([
  13705. 'type',
  13706. ' TNumber = longint;',
  13707. ' TInteger = longint;',
  13708. ' TObject = class',
  13709. ' private',
  13710. ' function GetItems(Index: TNumber): TInteger; virtual; abstract;',
  13711. ' procedure SetItems(Index: TInteger; Value: TNumber); virtual; abstract;',
  13712. ' protected',
  13713. ' property Items[Index: TNumber]: longint read GetItems write SetItems;',
  13714. ' end;']),
  13715. LinesToStr([
  13716. '']));
  13717. StartProgram(true);
  13718. Add([
  13719. 'uses unit1;',
  13720. 'type',
  13721. ' TBird = class',
  13722. ' public',
  13723. ' property Items;',
  13724. ' end;',
  13725. 'procedure DoIt(i: TInteger);',
  13726. 'begin',
  13727. 'end;',
  13728. 'var b: TBird;',
  13729. 'begin',
  13730. ' b.Items[1]:=2;',
  13731. ' b.Items[3]:=b.Items[4];',
  13732. ' DoIt(b.Items[5]);',
  13733. '']);
  13734. ConvertProgram;
  13735. CheckSource('TestClass_PropertyIncVisibility',
  13736. LinesToStr([ // statements
  13737. 'rtl.createClass(this, "TBird", pas.unit1.TObject, function () {',
  13738. '});',
  13739. 'this.DoIt = function (i) {',
  13740. '};',
  13741. 'this.b = null;'
  13742. ]),
  13743. LinesToStr([ // $mod.$main
  13744. '$mod.b.SetItems(1, 2);',
  13745. '$mod.b.SetItems(3, $mod.b.GetItems(4));',
  13746. '$mod.DoIt($mod.b.GetItems(5));'
  13747. ]));
  13748. end;
  13749. procedure TTestModule.TestClass_Assigned;
  13750. begin
  13751. StartProgram(false);
  13752. Add('type');
  13753. Add(' TObject = class');
  13754. Add(' end;');
  13755. Add('var');
  13756. Add(' Obj: tobject;');
  13757. Add(' b: boolean;');
  13758. Add('begin');
  13759. Add(' if Assigned(obj) then ;');
  13760. Add(' b:=Assigned(obj) or false;');
  13761. ConvertProgram;
  13762. CheckSource('TestClass_Assigned',
  13763. LinesToStr([ // statements
  13764. 'rtl.createClass(this, "TObject", null, function () {',
  13765. ' this.$init = function () {',
  13766. ' };',
  13767. ' this.$final = function () {',
  13768. ' };',
  13769. '});',
  13770. 'this.Obj = null;',
  13771. 'this.b = false;'
  13772. ]),
  13773. LinesToStr([ // $mod.$main
  13774. 'if ($mod.Obj != null);',
  13775. '$mod.b = ($mod.Obj != null) || false;'
  13776. ]));
  13777. end;
  13778. procedure TTestModule.TestClass_WithClassDoCreate;
  13779. begin
  13780. StartProgram(false);
  13781. Add('type');
  13782. Add(' TObject = class');
  13783. Add(' aBool: boolean;');
  13784. Add(' Arr: array of boolean;');
  13785. Add(' constructor Create;');
  13786. Add(' end;');
  13787. Add('constructor TObject.Create; begin end;');
  13788. Add('var');
  13789. Add(' Obj: tobject;');
  13790. Add(' b: boolean;');
  13791. Add('begin');
  13792. Add(' with tobject.create do begin');
  13793. Add(' b:=abool;');
  13794. Add(' abool:=b;');
  13795. Add(' b:=arr[1];');
  13796. Add(' arr[2]:=b;');
  13797. Add(' end;');
  13798. Add(' with tobject do');
  13799. Add(' obj:=create;');
  13800. Add(' with obj do begin');
  13801. Add(' create;');
  13802. Add(' b:=abool;');
  13803. Add(' abool:=b;');
  13804. Add(' b:=arr[3];');
  13805. Add(' arr[4]:=b;');
  13806. Add(' end;');
  13807. ConvertProgram;
  13808. CheckSource('TestClass_WithClassDoCreate',
  13809. LinesToStr([ // statements
  13810. 'rtl.createClass(this, "TObject", null, function () {',
  13811. ' this.$init = function () {',
  13812. ' this.aBool = false;',
  13813. ' this.Arr = [];',
  13814. ' };',
  13815. ' this.$final = function () {',
  13816. ' this.Arr = undefined;',
  13817. ' };',
  13818. ' this.Create = function () {',
  13819. ' return this;',
  13820. ' };',
  13821. '});',
  13822. 'this.Obj = null;',
  13823. 'this.b = false;'
  13824. ]),
  13825. LinesToStr([ // $mod.$main
  13826. 'var $with = $mod.TObject.$create("Create");',
  13827. '$mod.b = $with.aBool;',
  13828. '$with.aBool = $mod.b;',
  13829. '$mod.b = $with.Arr[1];',
  13830. '$with.Arr[2] = $mod.b;',
  13831. 'var $with1 = $mod.TObject;',
  13832. '$mod.Obj = $with1.$create("Create");',
  13833. 'var $with2 = $mod.Obj;',
  13834. '$with2.Create();',
  13835. '$mod.b = $with2.aBool;',
  13836. '$with2.aBool = $mod.b;',
  13837. '$mod.b = $with2.Arr[3];',
  13838. '$with2.Arr[4] = $mod.b;',
  13839. '']));
  13840. end;
  13841. procedure TTestModule.TestClass_WithClassInstDoProperty;
  13842. begin
  13843. StartProgram(false);
  13844. Add('type');
  13845. Add(' TObject = class');
  13846. Add(' FInt: longint;');
  13847. Add(' constructor Create;');
  13848. Add(' function GetSize: longint;');
  13849. Add(' procedure SetSize(Value: longint);');
  13850. Add(' property Int: longint read FInt write FInt;');
  13851. Add(' property Size: longint read GetSize write SetSize;');
  13852. Add(' end;');
  13853. Add('constructor TObject.Create; begin end;');
  13854. Add('function TObject.GetSize: longint; begin; end;');
  13855. Add('procedure TObject.SetSize(Value: longint); begin; end;');
  13856. Add('var');
  13857. Add(' Obj: tobject;');
  13858. Add(' i: longint;');
  13859. Add('begin');
  13860. Add(' with TObject.Create do begin');
  13861. Add(' i:=int;');
  13862. Add(' int:=i;');
  13863. Add(' i:=size;');
  13864. Add(' size:=i;');
  13865. Add(' end;');
  13866. Add(' with obj do begin');
  13867. Add(' i:=int;');
  13868. Add(' int:=i;');
  13869. Add(' i:=size;');
  13870. Add(' size:=i;');
  13871. Add(' end;');
  13872. ConvertProgram;
  13873. CheckSource('TestClass_WithClassInstDoProperty',
  13874. LinesToStr([ // statements
  13875. 'rtl.createClass(this, "TObject", null, function () {',
  13876. ' this.$init = function () {',
  13877. ' this.FInt = 0;',
  13878. ' };',
  13879. ' this.$final = function () {',
  13880. ' };',
  13881. ' this.Create = function () {',
  13882. ' return this;',
  13883. ' };',
  13884. ' this.GetSize = function () {',
  13885. ' var Result = 0;',
  13886. ' return Result;',
  13887. ' };',
  13888. ' this.SetSize = function (Value) {',
  13889. ' };',
  13890. '});',
  13891. 'this.Obj = null;',
  13892. 'this.i = 0;'
  13893. ]),
  13894. LinesToStr([ // $mod.$main
  13895. 'var $with = $mod.TObject.$create("Create");',
  13896. '$mod.i = $with.FInt;',
  13897. '$with.FInt = $mod.i;',
  13898. '$mod.i = $with.GetSize();',
  13899. '$with.SetSize($mod.i);',
  13900. 'var $with1 = $mod.Obj;',
  13901. '$mod.i = $with1.FInt;',
  13902. '$with1.FInt = $mod.i;',
  13903. '$mod.i = $with1.GetSize();',
  13904. '$with1.SetSize($mod.i);',
  13905. '']));
  13906. end;
  13907. procedure TTestModule.TestClass_WithClassInstDoPropertyWithParams;
  13908. begin
  13909. StartProgram(false);
  13910. Add('type');
  13911. Add(' TObject = class');
  13912. Add(' constructor Create;');
  13913. Add(' function GetItems(Index: longint): longint;');
  13914. Add(' procedure SetItems(Index, Value: longint);');
  13915. Add(' property Items[Index: longint]: longint read GetItems write SetItems;');
  13916. Add(' end;');
  13917. Add('constructor TObject.Create; begin end;');
  13918. Add('function tobject.getitems(index: longint): longint; begin; end;');
  13919. Add('procedure tobject.setitems(index, value: longint); begin; end;');
  13920. Add('var');
  13921. Add(' Obj: tobject;');
  13922. Add(' i: longint;');
  13923. Add('begin');
  13924. Add(' with TObject.Create do begin');
  13925. Add(' i:=Items[1];');
  13926. Add(' Items[2]:=i;');
  13927. Add(' end;');
  13928. Add(' with obj do begin');
  13929. Add(' i:=Items[3];');
  13930. Add(' Items[4]:=i;');
  13931. Add(' end;');
  13932. ConvertProgram;
  13933. CheckSource('TestClass_WithClassInstDoPropertyWithParams',
  13934. LinesToStr([ // statements
  13935. 'rtl.createClass(this, "TObject", null, function () {',
  13936. ' this.$init = function () {',
  13937. ' };',
  13938. ' this.$final = function () {',
  13939. ' };',
  13940. ' this.Create = function () {',
  13941. ' return this;',
  13942. ' };',
  13943. ' this.GetItems = function (Index) {',
  13944. ' var Result = 0;',
  13945. ' return Result;',
  13946. ' };',
  13947. ' this.SetItems = function (Index, Value) {',
  13948. ' };',
  13949. '});',
  13950. 'this.Obj = null;',
  13951. 'this.i = 0;'
  13952. ]),
  13953. LinesToStr([ // $mod.$main
  13954. 'var $with = $mod.TObject.$create("Create");',
  13955. '$mod.i = $with.GetItems(1);',
  13956. '$with.SetItems(2, $mod.i);',
  13957. 'var $with1 = $mod.Obj;',
  13958. '$mod.i = $with1.GetItems(3);',
  13959. '$with1.SetItems(4, $mod.i);',
  13960. '']));
  13961. end;
  13962. procedure TTestModule.TestClass_WithClassInstDoFunc;
  13963. begin
  13964. StartProgram(false);
  13965. Add('type');
  13966. Add(' TObject = class');
  13967. Add(' constructor Create;');
  13968. Add(' function GetSize: longint;');
  13969. Add(' procedure SetSize(Value: longint);');
  13970. Add(' end;');
  13971. Add('constructor TObject.Create; begin end;');
  13972. Add('function TObject.GetSize: longint; begin; end;');
  13973. Add('procedure TObject.SetSize(Value: longint); begin; end;');
  13974. Add('var');
  13975. Add(' Obj: tobject;');
  13976. Add(' i: longint;');
  13977. Add('begin');
  13978. Add(' with TObject.Create do begin');
  13979. Add(' i:=GetSize;');
  13980. Add(' i:=GetSize();');
  13981. Add(' SetSize(i);');
  13982. Add(' end;');
  13983. Add(' with obj do begin');
  13984. Add(' i:=GetSize;');
  13985. Add(' i:=GetSize();');
  13986. Add(' SetSize(i);');
  13987. Add(' end;');
  13988. ConvertProgram;
  13989. CheckSource('TestClass_WithClassInstDoFunc',
  13990. LinesToStr([ // statements
  13991. 'rtl.createClass(this, "TObject", null, function () {',
  13992. ' this.$init = function () {',
  13993. ' };',
  13994. ' this.$final = function () {',
  13995. ' };',
  13996. ' this.Create = function () {',
  13997. ' return this;',
  13998. ' };',
  13999. ' this.GetSize = function () {',
  14000. ' var Result = 0;',
  14001. ' return Result;',
  14002. ' };',
  14003. ' this.SetSize = function (Value) {',
  14004. ' };',
  14005. '});',
  14006. 'this.Obj = null;',
  14007. 'this.i = 0;'
  14008. ]),
  14009. LinesToStr([ // $mod.$main
  14010. 'var $with = $mod.TObject.$create("Create");',
  14011. '$mod.i = $with.GetSize();',
  14012. '$mod.i = $with.GetSize();',
  14013. '$with.SetSize($mod.i);',
  14014. 'var $with1 = $mod.Obj;',
  14015. '$mod.i = $with1.GetSize();',
  14016. '$mod.i = $with1.GetSize();',
  14017. '$with1.SetSize($mod.i);',
  14018. '']));
  14019. end;
  14020. procedure TTestModule.TestClass_TypeCast;
  14021. begin
  14022. StartProgram(false);
  14023. Add('type');
  14024. Add(' TObject = class');
  14025. Add(' Next: TObject;');
  14026. Add(' constructor Create;');
  14027. Add(' end;');
  14028. Add(' TControl = class(TObject)');
  14029. Add(' Arr: array of TObject;');
  14030. Add(' function GetIt(vI: longint = 0): TObject;');
  14031. Add(' end;');
  14032. Add('constructor tobject.create; begin end;');
  14033. Add('function tcontrol.getit(vi: longint = 0): tobject; begin end;');
  14034. Add('var');
  14035. Add(' Obj: tobject;');
  14036. Add('begin');
  14037. Add(' obj:=tcontrol(obj).next;');
  14038. Add(' tcontrol(obj):=nil;');
  14039. Add(' obj:=tcontrol(obj);');
  14040. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit);');
  14041. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit());');
  14042. Add(' tcontrol(obj):=tcontrol(tcontrol(obj).getit(1));');
  14043. Add(' tcontrol(obj):=tcontrol(tcontrol(tcontrol(obj).getit).arr[2]);');
  14044. Add(' obj:=tcontrol(nil);');
  14045. ConvertProgram;
  14046. CheckSource('TestClass_TypeCast',
  14047. LinesToStr([ // statements
  14048. 'rtl.createClass(this, "TObject", null, function () {',
  14049. ' this.$init = function () {',
  14050. ' this.Next = null;',
  14051. ' };',
  14052. ' this.$final = function () {',
  14053. ' this.Next = undefined;',
  14054. ' };',
  14055. ' this.Create = function () {',
  14056. ' return this;',
  14057. ' };',
  14058. '});',
  14059. 'rtl.createClass(this, "TControl", this.TObject, function () {',
  14060. ' this.$init = function () {',
  14061. ' $mod.TObject.$init.call(this);',
  14062. ' this.Arr = [];',
  14063. ' };',
  14064. ' this.$final = function () {',
  14065. ' this.Arr = undefined;',
  14066. ' $mod.TObject.$final.call(this);',
  14067. ' };',
  14068. ' this.GetIt = function (vI) {',
  14069. ' var Result = null;',
  14070. ' return Result;',
  14071. ' };',
  14072. '});',
  14073. 'this.Obj = null;'
  14074. ]),
  14075. LinesToStr([ // $mod.$main
  14076. '$mod.Obj = $mod.Obj.Next;',
  14077. '$mod.Obj = null;',
  14078. '$mod.Obj = $mod.Obj;',
  14079. '$mod.Obj = $mod.Obj.GetIt(0);',
  14080. '$mod.Obj = $mod.Obj.GetIt(0);',
  14081. '$mod.Obj = $mod.Obj.GetIt(1);',
  14082. '$mod.Obj = $mod.Obj.GetIt(0).Arr[2];',
  14083. '$mod.Obj = null;',
  14084. '']));
  14085. end;
  14086. procedure TTestModule.TestClass_TypeCastUntypedParam;
  14087. begin
  14088. StartProgram(false);
  14089. Add('type');
  14090. Add(' TObject = class end;');
  14091. Add('procedure ProcA(var A);');
  14092. Add('begin');
  14093. Add(' TObject(A):=nil;');
  14094. Add(' TObject(A):=TObject(A);');
  14095. Add(' if TObject(A)=nil then ;');
  14096. Add(' if nil=TObject(A) then ;');
  14097. Add('end;');
  14098. Add('procedure ProcB(out A);');
  14099. Add('begin');
  14100. Add(' TObject(A):=nil;');
  14101. Add(' TObject(A):=TObject(A);');
  14102. Add(' if TObject(A)=nil then ;');
  14103. Add(' if nil=TObject(A) then ;');
  14104. Add('end;');
  14105. Add('procedure ProcC(const A);');
  14106. Add('begin');
  14107. Add(' if TObject(A)=nil then ;');
  14108. Add(' if nil=TObject(A) then ;');
  14109. Add('end;');
  14110. Add('var o: TObject;');
  14111. Add('begin');
  14112. Add(' ProcA(o);');
  14113. Add(' ProcB(o);');
  14114. Add(' ProcC(o);');
  14115. ConvertProgram;
  14116. CheckSource('TestClass_TypeCastUntypedParam',
  14117. LinesToStr([ // statements
  14118. 'rtl.createClass(this, "TObject", null, function () {',
  14119. ' this.$init = function () {',
  14120. ' };',
  14121. ' this.$final = function () {',
  14122. ' };',
  14123. '});',
  14124. 'this.ProcA = function (A) {',
  14125. ' A.set(null);',
  14126. ' A.set(A.get());',
  14127. ' if (A.get() === null);',
  14128. ' if (null === A.get());',
  14129. '};',
  14130. 'this.ProcB = function (A) {',
  14131. ' A.set(null);',
  14132. ' A.set(A.get());',
  14133. ' if (A.get() === null);',
  14134. ' if (null === A.get());',
  14135. '};',
  14136. 'this.ProcC = function (A) {',
  14137. ' if (A === null);',
  14138. ' if (null === A);',
  14139. '};',
  14140. 'this.o = null;',
  14141. '']),
  14142. LinesToStr([ // $mod.$main
  14143. '$mod.ProcA({',
  14144. ' p: $mod,',
  14145. ' get: function () {',
  14146. ' return this.p.o;',
  14147. ' },',
  14148. ' set: function (v) {',
  14149. ' this.p.o = v;',
  14150. ' }',
  14151. '});',
  14152. '$mod.ProcB({',
  14153. ' p: $mod,',
  14154. ' get: function () {',
  14155. ' return this.p.o;',
  14156. ' },',
  14157. ' set: function (v) {',
  14158. ' this.p.o = v;',
  14159. ' }',
  14160. '});',
  14161. '$mod.ProcC($mod.o);',
  14162. '']));
  14163. end;
  14164. procedure TTestModule.TestClass_Overloads;
  14165. begin
  14166. StartProgram(false);
  14167. Add('type');
  14168. Add(' TObject = class');
  14169. Add(' procedure DoIt;');
  14170. Add(' procedure DoIt(vI: longint);');
  14171. Add(' end;');
  14172. Add('procedure TObject.DoIt;');
  14173. Add('begin');
  14174. Add(' DoIt;');
  14175. Add(' DoIt(1);');
  14176. Add('end;');
  14177. Add('procedure TObject.DoIt(vI: longint); begin end;');
  14178. Add('begin');
  14179. ConvertProgram;
  14180. CheckSource('TestClass_Overloads',
  14181. LinesToStr([ // statements
  14182. 'rtl.createClass(this, "TObject", null, function () {',
  14183. ' this.$init = function () {',
  14184. ' };',
  14185. ' this.$final = function () {',
  14186. ' };',
  14187. ' this.DoIt = function () {',
  14188. ' this.DoIt();',
  14189. ' this.DoIt$1(1);',
  14190. ' };',
  14191. ' this.DoIt$1 = function (vI) {',
  14192. ' };',
  14193. '});',
  14194. '']),
  14195. LinesToStr([ // $mod.$main
  14196. '']));
  14197. end;
  14198. procedure TTestModule.TestClass_OverloadsAncestor;
  14199. begin
  14200. StartProgram(false);
  14201. Add('type');
  14202. Add(' TObject = class;');
  14203. Add(' TObject = class');
  14204. Add(' procedure DoIt(vA: longint);');
  14205. Add(' procedure DoIt(vA, vB: longint);');
  14206. Add(' end;');
  14207. Add(' TCar = class;');
  14208. Add(' TCar = class');
  14209. Add(' procedure DoIt(vA: longint);');
  14210. Add(' procedure DoIt(vA, vB: longint);');
  14211. Add(' end;');
  14212. Add('procedure tobject.doit(va: longint);');
  14213. Add('begin');
  14214. Add(' doit(1);');
  14215. Add(' doit(1,2);');
  14216. Add('end;');
  14217. Add('procedure tobject.doit(va, vb: longint); begin end;');
  14218. Add('procedure tcar.doit(va: longint);');
  14219. Add('begin');
  14220. Add(' doit(1);');
  14221. Add(' doit(1,2);');
  14222. Add(' inherited doit(1);');
  14223. Add(' inherited doit(1,2);');
  14224. Add('end;');
  14225. Add('procedure tcar.doit(va, vb: longint); begin end;');
  14226. Add('begin');
  14227. ConvertProgram;
  14228. CheckSource('TestClass_OverloadsAncestor',
  14229. LinesToStr([ // statements
  14230. 'rtl.createClass(this, "TObject", null, function () {',
  14231. ' this.$init = function () {',
  14232. ' };',
  14233. ' this.$final = function () {',
  14234. ' };',
  14235. ' this.DoIt = function (vA) {',
  14236. ' this.DoIt(1);',
  14237. ' this.DoIt$1(1,2);',
  14238. ' };',
  14239. ' this.DoIt$1 = function (vA, vB) {',
  14240. ' };',
  14241. '});',
  14242. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  14243. ' this.DoIt$2 = function (vA) {',
  14244. ' this.DoIt$2(1);',
  14245. ' this.DoIt$3(1, 2);',
  14246. ' $mod.TObject.DoIt.call(this, 1);',
  14247. ' $mod.TObject.DoIt$1.call(this, 1, 2);',
  14248. ' };',
  14249. ' this.DoIt$3 = function (vA, vB) {',
  14250. ' };',
  14251. '});',
  14252. '']),
  14253. LinesToStr([ // $mod.$main
  14254. '']));
  14255. end;
  14256. procedure TTestModule.TestClass_OverloadConstructor;
  14257. begin
  14258. StartProgram(false);
  14259. Add('type');
  14260. Add(' TObject = class');
  14261. Add(' constructor Create(vA: longint);');
  14262. Add(' constructor Create(vA, vB: longint);');
  14263. Add(' end;');
  14264. Add(' TCar = class');
  14265. Add(' constructor Create(vA: longint);');
  14266. Add(' constructor Create(vA, vB: longint);');
  14267. Add(' end;');
  14268. Add('constructor tobject.create(va: longint);');
  14269. Add('begin');
  14270. Add(' create(1);');
  14271. Add(' create(1,2);');
  14272. Add('end;');
  14273. Add('constructor tobject.create(va, vb: longint); begin end;');
  14274. Add('constructor tcar.create(va: longint);');
  14275. Add('begin');
  14276. Add(' create(1);');
  14277. Add(' create(1,2);');
  14278. Add(' inherited create(1);');
  14279. Add(' inherited create(1,2);');
  14280. Add('end;');
  14281. Add('constructor tcar.create(va, vb: longint); begin end;');
  14282. Add('begin');
  14283. Add(' tobject.create(1);');
  14284. Add(' tobject.create(1,2);');
  14285. Add(' tcar.create(1);');
  14286. Add(' tcar.create(1,2);');
  14287. ConvertProgram;
  14288. CheckSource('TestClass_OverloadConstructor',
  14289. LinesToStr([ // statements
  14290. 'rtl.createClass(this, "TObject", null, function () {',
  14291. ' this.$init = function () {',
  14292. ' };',
  14293. ' this.$final = function () {',
  14294. ' };',
  14295. ' this.Create = function (vA) {',
  14296. ' this.Create(1);',
  14297. ' this.Create$1(1,2);',
  14298. ' return this;',
  14299. ' };',
  14300. ' this.Create$1 = function (vA, vB) {',
  14301. ' return this;',
  14302. ' };',
  14303. '});',
  14304. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  14305. ' this.Create$2 = function (vA) {',
  14306. ' this.Create$2(1);',
  14307. ' this.Create$3(1, 2);',
  14308. ' $mod.TObject.Create.call(this, 1);',
  14309. ' $mod.TObject.Create$1.call(this, 1, 2);',
  14310. ' return this;',
  14311. ' };',
  14312. ' this.Create$3 = function (vA, vB) {',
  14313. ' return this;',
  14314. ' };',
  14315. '});',
  14316. '']),
  14317. LinesToStr([ // $mod.$main
  14318. '$mod.TObject.$create("Create", [1]);',
  14319. '$mod.TObject.$create("Create$1", [1, 2]);',
  14320. '$mod.TCar.$create("Create$2", [1]);',
  14321. '$mod.TCar.$create("Create$3", [1, 2]);',
  14322. '']));
  14323. end;
  14324. procedure TTestModule.TestClass_OverloadDelphiOverride;
  14325. begin
  14326. StartProgram(false);
  14327. Add([
  14328. '{$mode delphi}',
  14329. 'type',
  14330. ' TObject = class end;',
  14331. ' TBird = class',
  14332. ' function {#a}GetValue: longint; overload; virtual;',
  14333. ' function {#b}GetValue(AValue: longint): longint; overload; virtual;',
  14334. ' end;',
  14335. ' TEagle = class(TBird)',
  14336. ' function {#c}GetValue: longint; overload; override;',
  14337. ' function {#d}GetValue(AValue: longint): longint; overload; override;',
  14338. ' end;',
  14339. 'function TBird.GetValue: longint;',
  14340. 'begin',
  14341. ' if 3={@a}GetValue then ;',
  14342. ' if 4={@b}GetValue(5) then ;',
  14343. 'end;',
  14344. 'function TBird.GetValue(AValue: longint): longint;',
  14345. 'begin',
  14346. 'end;',
  14347. 'function TEagle.GetValue: longint;',
  14348. 'begin',
  14349. ' if 13={@c}GetValue then ;',
  14350. ' if 14={@d}GetValue(15) then ;',
  14351. ' if 15=inherited {@a}GetValue then ;',
  14352. ' if 16=inherited {@b}GetValue(17) then ;',
  14353. 'end;',
  14354. 'function TEagle.GetValue(AValue: longint): longint;',
  14355. 'begin',
  14356. 'end;',
  14357. 'var',
  14358. ' e: TEagle;',
  14359. 'begin',
  14360. ' if 23=e.{@c}GetValue then ;',
  14361. ' if 24=e.{@d}GetValue(25) then ;']);
  14362. ConvertProgram;
  14363. CheckSource('TestClass_OverloadDelphiOverride',
  14364. LinesToStr([ // statements
  14365. 'rtl.createClass(this, "TObject", null, function () {',
  14366. ' this.$init = function () {',
  14367. ' };',
  14368. ' this.$final = function () {',
  14369. ' };',
  14370. '});',
  14371. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  14372. ' this.GetValue = function () {',
  14373. ' var Result = 0;',
  14374. ' if (3 === this.GetValue()) ;',
  14375. ' if (4 === this.GetValue$1(5)) ;',
  14376. ' return Result;',
  14377. ' };',
  14378. ' this.GetValue$1 = function (AValue) {',
  14379. ' var Result = 0;',
  14380. ' return Result;',
  14381. ' };',
  14382. '});',
  14383. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  14384. ' this.GetValue = function () {',
  14385. ' var Result = 0;',
  14386. ' if (13 === this.GetValue()) ;',
  14387. ' if (14 === this.GetValue$1(15)) ;',
  14388. ' if (15 === $mod.TBird.GetValue.call(this)) ;',
  14389. ' if (16 === $mod.TBird.GetValue$1.call(this, 17)) ;',
  14390. ' return Result;',
  14391. ' };',
  14392. ' this.GetValue$1 = function (AValue) {',
  14393. ' var Result = 0;',
  14394. ' return Result;',
  14395. ' };',
  14396. '});',
  14397. 'this.e = null;',
  14398. '']),
  14399. LinesToStr([ // $mod.$main
  14400. 'if (23 === $mod.e.GetValue()) ;',
  14401. 'if (24 === $mod.e.GetValue$1(25)) ;',
  14402. '']));
  14403. end;
  14404. procedure TTestModule.TestClass_ReintroduceVarDelphi;
  14405. begin
  14406. StartProgram(false);
  14407. Add([
  14408. '{$mode delphi}',
  14409. 'type',
  14410. ' TObject = class end;',
  14411. ' TAnimal = class',
  14412. ' public',
  14413. ' {#animal_a}A: longint;',
  14414. ' function {#animal_b}B: longint;',
  14415. ' end;',
  14416. ' TBird = class(TAnimal)',
  14417. ' public',
  14418. ' {#bird_a}A: double;',
  14419. ' {#bird_b}B: boolean;',
  14420. ' end;',
  14421. ' TEagle = class(TBird)',
  14422. ' public',
  14423. ' function {#eagle_a}A: boolean;',
  14424. ' {#eagle_b}B: double;',
  14425. ' end;',
  14426. 'function TAnimal.B: longint;',
  14427. 'begin',
  14428. 'end;',
  14429. 'function TEagle.A: boolean;',
  14430. 'begin',
  14431. ' {@eagle_b}B:=3.3;',
  14432. ' {@eagle_a}A();',
  14433. ' TBird(Self).{@bird_b}B:=true;',
  14434. ' TAnimal(Self).{@animal_a}A:=17;',
  14435. ' inherited {@bird_b}B:=inherited {bird_a}A>1;', // Delphi allows only inherited <functionname>
  14436. 'end;',
  14437. 'var',
  14438. ' e: TEagle;',
  14439. 'begin',
  14440. ' e.{@eagle_b}B:=5.3;',
  14441. ' if e.{@eagle_a}A then ;',
  14442. '']);
  14443. ConvertProgram;
  14444. CheckSource('TestClass_ReintroduceVarDelphi',
  14445. LinesToStr([ // statements
  14446. 'rtl.createClass(this, "TObject", null, function () {',
  14447. ' this.$init = function () {',
  14448. ' };',
  14449. ' this.$final = function () {',
  14450. ' };',
  14451. '});',
  14452. 'rtl.createClass(this, "TAnimal", this.TObject, function () {',
  14453. ' this.$init = function () {',
  14454. ' $mod.TObject.$init.call(this);',
  14455. ' this.A = 0;',
  14456. ' };',
  14457. ' this.B = function () {',
  14458. ' var Result = 0;',
  14459. ' return Result;',
  14460. ' };',
  14461. '});',
  14462. 'rtl.createClass(this, "TBird", this.TAnimal, function () {',
  14463. ' this.$init = function () {',
  14464. ' $mod.TAnimal.$init.call(this);',
  14465. ' this.A$1 = 0.0;',
  14466. ' this.B$1 = false;',
  14467. ' };',
  14468. '});',
  14469. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  14470. ' this.$init = function () {',
  14471. ' $mod.TBird.$init.call(this);',
  14472. ' this.B$2 = 0.0;',
  14473. ' };',
  14474. ' this.A$2 = function () {',
  14475. ' var Result = false;',
  14476. ' this.B$2 = 3.3;',
  14477. ' this.A$2();',
  14478. ' this.B$1 = true;',
  14479. ' this.A = 17;',
  14480. ' this.B$1 = this.A$1 > 1;',
  14481. ' return Result;',
  14482. ' };',
  14483. '});',
  14484. 'this.e = null;',
  14485. '']),
  14486. LinesToStr([ // $mod.$main
  14487. '$mod.e.B$2 = 5.3;',
  14488. 'if ($mod.e.A$2()) ;',
  14489. '']));
  14490. end;
  14491. procedure TTestModule.TestClass_ReintroducedVar;
  14492. begin
  14493. StartProgram(false);
  14494. Add('type');
  14495. Add(' TObject = class');
  14496. Add(' strict private');
  14497. Add(' Some: longint;');
  14498. Add(' end;');
  14499. Add(' TMobile = class');
  14500. Add(' strict private');
  14501. Add(' Some: string;');
  14502. Add(' end;');
  14503. Add(' TCar = class(tmobile)');
  14504. Add(' procedure Some;');
  14505. Add(' procedure Some(vA: longint);');
  14506. Add(' end;');
  14507. Add('procedure tcar.some;');
  14508. Add('begin');
  14509. Add(' Some;');
  14510. Add(' Some(1);');
  14511. Add('end;');
  14512. Add('procedure tcar.some(va: longint); begin end;');
  14513. Add('begin');
  14514. ConvertProgram;
  14515. CheckSource('TestClass_ReintroducedVar',
  14516. LinesToStr([ // statements
  14517. 'rtl.createClass(this, "TObject", null, function () {',
  14518. ' this.$init = function () {',
  14519. ' this.Some = 0;',
  14520. ' };',
  14521. ' this.$final = function () {',
  14522. ' };',
  14523. '});',
  14524. 'rtl.createClass(this, "TMobile", this.TObject, function () {',
  14525. ' this.$init = function () {',
  14526. ' $mod.TObject.$init.call(this);',
  14527. ' this.Some$1 = "";',
  14528. ' };',
  14529. '});',
  14530. 'rtl.createClass(this, "TCar", this.TMobile, function () {',
  14531. ' this.Some$2 = function () {',
  14532. ' this.Some$2();',
  14533. ' this.Some$3(1);',
  14534. ' };',
  14535. ' this.Some$3 = function (vA) {',
  14536. ' };',
  14537. '});',
  14538. '']),
  14539. LinesToStr([ // $mod.$main
  14540. '']));
  14541. end;
  14542. procedure TTestModule.TestClass_RaiseDescendant;
  14543. begin
  14544. StartProgram(false);
  14545. Add([
  14546. 'type',
  14547. ' TObject = class',
  14548. ' constructor Create(Msg: string);',
  14549. ' end;',
  14550. ' Exception = class',
  14551. ' end;',
  14552. ' EConvertError = class(Exception)',
  14553. ' end;',
  14554. 'constructor TObject.Create(Msg: string); begin end;',
  14555. 'function AssertConv(Msg: string = ''def''): EConvertError; begin end;',
  14556. 'begin',
  14557. ' raise Exception.Create(''Bar1'');',
  14558. ' raise EConvertError.Create(''Bar2'');',
  14559. ' raise AssertConv(''Bar2'');',
  14560. ' raise AssertConv;',
  14561. '']);
  14562. ConvertProgram;
  14563. CheckSource('TestClass_RaiseDescendant',
  14564. LinesToStr([ // statements
  14565. 'rtl.createClass(this, "TObject", null, function () {',
  14566. ' this.$init = function () {',
  14567. ' };',
  14568. ' this.$final = function () {',
  14569. ' };',
  14570. ' this.Create = function (Msg) {',
  14571. ' return this;',
  14572. ' };',
  14573. '});',
  14574. 'rtl.createClass(this, "Exception", this.TObject, function () {',
  14575. '});',
  14576. 'rtl.createClass(this, "EConvertError", this.Exception, function () {',
  14577. '});',
  14578. 'this.AssertConv = function (Msg) {',
  14579. ' var Result = null;',
  14580. ' return Result;',
  14581. '};',
  14582. '']),
  14583. LinesToStr([ // $mod.$main
  14584. 'throw $mod.Exception.$create("Create",["Bar1"]);',
  14585. 'throw $mod.EConvertError.$create("Create",["Bar2"]);',
  14586. 'throw $mod.AssertConv("Bar2");',
  14587. 'throw $mod.AssertConv("def");',
  14588. '']));
  14589. end;
  14590. procedure TTestModule.TestClass_ExternalMethod;
  14591. begin
  14592. AddModuleWithIntfImplSrc('unit2.pas',
  14593. LinesToStr([
  14594. 'type',
  14595. ' TObject = class',
  14596. ' public',
  14597. ' procedure Intern; external name ''$DoIntern'';',
  14598. ' end;',
  14599. '']),
  14600. LinesToStr([
  14601. '']));
  14602. StartUnit(true);
  14603. Add('interface');
  14604. Add('uses unit2;');
  14605. Add('type');
  14606. Add(' TCar = class(TObject)');
  14607. Add(' public');
  14608. Add(' procedure Intern2; external name ''$DoIntern2'';');
  14609. Add(' procedure DoIt;');
  14610. Add(' end;');
  14611. Add('implementation');
  14612. Add('procedure tcar.doit;');
  14613. Add('begin');
  14614. Add(' Intern;');
  14615. Add(' Intern();');
  14616. Add(' Intern2;');
  14617. Add(' Intern2();');
  14618. Add('end;');
  14619. Add('var Obj: TCar;');
  14620. Add('begin');
  14621. Add(' obj.intern;');
  14622. Add(' obj.intern();');
  14623. Add(' obj.intern2;');
  14624. Add(' obj.intern2();');
  14625. Add(' obj.doit;');
  14626. Add(' obj.doit();');
  14627. Add(' with obj do begin');
  14628. Add(' Intern;');
  14629. Add(' Intern();');
  14630. Add(' Intern2;');
  14631. Add(' Intern2();');
  14632. Add(' end;');
  14633. ConvertUnit;
  14634. CheckSource('TestClass_ExternalMethod',
  14635. LinesToStr([
  14636. 'var $impl = $mod.$impl;',
  14637. 'rtl.createClass(this, "TCar", pas.unit2.TObject, function () {',
  14638. ' this.DoIt = function () {',
  14639. ' this.$DoIntern();',
  14640. ' this.$DoIntern();',
  14641. ' this.$DoIntern2();',
  14642. ' this.$DoIntern2();',
  14643. ' };',
  14644. ' });',
  14645. '']),
  14646. LinesToStr([ // this.$init
  14647. '$impl.Obj.$DoIntern();',
  14648. '$impl.Obj.$DoIntern();',
  14649. '$impl.Obj.$DoIntern2();',
  14650. '$impl.Obj.$DoIntern2();',
  14651. '$impl.Obj.DoIt();',
  14652. '$impl.Obj.DoIt();',
  14653. 'var $with = $impl.Obj;',
  14654. '$with.$DoIntern();',
  14655. '$with.$DoIntern();',
  14656. '$with.$DoIntern2();',
  14657. '$with.$DoIntern2();',
  14658. '']),
  14659. LinesToStr([ // implementation
  14660. '$impl.Obj = null;',
  14661. '']) );
  14662. end;
  14663. procedure TTestModule.TestClass_ExternalVirtualNameMismatchFail;
  14664. begin
  14665. StartProgram(false);
  14666. Add('type');
  14667. Add(' TObject = class');
  14668. Add(' procedure DoIt; virtual; external name ''Foo'';');
  14669. Add(' end;');
  14670. Add('begin');
  14671. SetExpectedPasResolverError('Virtual method name must match external',
  14672. nVirtualMethodNameMustMatchExternal);
  14673. ConvertProgram;
  14674. end;
  14675. procedure TTestModule.TestClass_ExternalOverrideFail;
  14676. begin
  14677. StartProgram(false);
  14678. Add('type');
  14679. Add(' TObject = class');
  14680. Add(' procedure DoIt; virtual; external name ''DoIt'';');
  14681. Add(' end;');
  14682. Add(' TCar = class');
  14683. Add(' procedure DoIt; override; external name ''DoIt'';');
  14684. Add(' end;');
  14685. Add('begin');
  14686. SetExpectedPasResolverError('Invalid procedure modifier override,external',
  14687. nInvalidXModifierY);
  14688. ConvertProgram;
  14689. end;
  14690. procedure TTestModule.TestClass_ExternalVar;
  14691. begin
  14692. AddModuleWithIntfImplSrc('unit2.pas',
  14693. LinesToStr([
  14694. '{$modeswitch externalclass}',
  14695. 'type',
  14696. ' TObject = class',
  14697. ' public',
  14698. ' Intern: longint external name ''$Intern'';',
  14699. ' Bracket: longint external name ''["A B"]'';',
  14700. ' end;',
  14701. '']),
  14702. LinesToStr([
  14703. '']));
  14704. StartUnit(true);
  14705. Add([
  14706. 'interface',
  14707. 'uses unit2;',
  14708. '{$modeswitch externalclass}',
  14709. 'type',
  14710. ' TCar = class(tobject)',
  14711. ' public',
  14712. ' Intern2: longint external name ''$Intern2'';',
  14713. ' procedure DoIt;',
  14714. ' end;',
  14715. 'implementation',
  14716. 'procedure tcar.doit;',
  14717. 'begin',
  14718. ' Intern:=Intern+1;',
  14719. ' Intern2:=Intern2+2;',
  14720. ' Bracket:=Bracket+3;',
  14721. 'end;',
  14722. 'var Obj: TCar;',
  14723. 'begin',
  14724. ' obj.intern:=obj.intern+1;',
  14725. ' obj.intern2:=obj.intern2+2;',
  14726. ' obj.Bracket:=obj.Bracket+3;',
  14727. ' with obj do begin',
  14728. ' intern:=intern+1;',
  14729. ' intern2:=intern2+2;',
  14730. ' Bracket:=Bracket+3;',
  14731. ' end;']);
  14732. ConvertUnit;
  14733. CheckSource('TestClass_ExternalVar',
  14734. LinesToStr([
  14735. 'var $impl = $mod.$impl;',
  14736. 'rtl.createClass(this, "TCar", pas.unit2.TObject, function () {',
  14737. ' this.DoIt = function () {',
  14738. ' this.$Intern = this.$Intern + 1;',
  14739. ' this.$Intern2 = this.$Intern2 + 2;',
  14740. ' this["A B"] = this["A B"] + 3;',
  14741. ' };',
  14742. ' });',
  14743. '']),
  14744. LinesToStr([
  14745. '$impl.Obj.$Intern = $impl.Obj.$Intern + 1;',
  14746. '$impl.Obj.$Intern2 = $impl.Obj.$Intern2 + 2;',
  14747. '$impl.Obj["A B"] = $impl.Obj["A B"] + 3;',
  14748. 'var $with = $impl.Obj;',
  14749. '$with.$Intern = $with.$Intern + 1;',
  14750. '$with.$Intern2 = $with.$Intern2 + 2;',
  14751. '$with["A B"] = $with["A B"] + 3;',
  14752. '']),
  14753. LinesToStr([ // implementation
  14754. '$impl.Obj = null;',
  14755. '']));
  14756. end;
  14757. procedure TTestModule.TestClass_Const;
  14758. begin
  14759. StartProgram(false);
  14760. Add([
  14761. 'type',
  14762. ' integer = longint;',
  14763. ' TClass = class of TObject;',
  14764. ' TObject = class',
  14765. ' public',
  14766. ' const cI: integer = 3;',
  14767. ' procedure DoIt;',
  14768. ' class procedure DoMore;',
  14769. ' end;',
  14770. 'procedure tobject.doit;',
  14771. 'begin',
  14772. ' if cI=4 then;',
  14773. ' if 5=cI then;',
  14774. ' if Self.cI=6 then;',
  14775. ' if 7=Self.cI then;',
  14776. ' with Self do begin',
  14777. ' if cI=11 then;',
  14778. ' if 12=cI then;',
  14779. ' end;',
  14780. 'end;',
  14781. 'class procedure tobject.domore;',
  14782. 'begin',
  14783. ' if cI=8 then;',
  14784. ' if Self.cI=9 then;',
  14785. ' if 10=cI then;',
  14786. ' if 11=Self.cI then;',
  14787. ' with Self do begin',
  14788. ' if cI=13 then;',
  14789. ' if 14=cI then;',
  14790. ' end;',
  14791. 'end;',
  14792. 'var',
  14793. ' Obj: TObject;',
  14794. ' Cla: TClass;',
  14795. 'begin',
  14796. ' if TObject.cI=21 then ;',
  14797. ' if Obj.cI=22 then ;',
  14798. ' if Cla.cI=23 then ;',
  14799. ' with obj do if ci=24 then;',
  14800. ' with TObject do if ci=25 then;',
  14801. ' with Cla do if ci=26 then;']);
  14802. ConvertProgram;
  14803. CheckSource('TestClass_Const',
  14804. LinesToStr([
  14805. 'rtl.createClass(this, "TObject", null, function () {',
  14806. ' this.cI = 3;',
  14807. ' this.$init = function () {',
  14808. ' };',
  14809. ' this.$final = function () {',
  14810. ' };',
  14811. ' this.DoIt = function () {',
  14812. ' if (this.cI === 4) ;',
  14813. ' if (5 === this.cI) ;',
  14814. ' if (this.cI === 6) ;',
  14815. ' if (7 === this.cI) ;',
  14816. ' if (this.cI === 11) ;',
  14817. ' if (12 === this.cI) ;',
  14818. ' };',
  14819. ' this.DoMore = function () {',
  14820. ' if (this.cI === 8) ;',
  14821. ' if (this.cI === 9) ;',
  14822. ' if (10 === this.cI) ;',
  14823. ' if (11 === this.cI) ;',
  14824. ' if (this.cI === 13) ;',
  14825. ' if (14 === this.cI) ;',
  14826. ' };',
  14827. '});',
  14828. 'this.Obj = null;',
  14829. 'this.Cla = null;',
  14830. '']),
  14831. LinesToStr([
  14832. 'if ($mod.TObject.cI === 21) ;',
  14833. 'if ($mod.Obj.cI === 22) ;',
  14834. 'if ($mod.Cla.cI === 23) ;',
  14835. 'var $with = $mod.Obj;',
  14836. 'if ($with.cI === 24) ;',
  14837. 'var $with1 = $mod.TObject;',
  14838. 'if ($with1.cI === 25) ;',
  14839. 'var $with2 = $mod.Cla;',
  14840. 'if ($with2.cI === 26) ;',
  14841. '']));
  14842. end;
  14843. procedure TTestModule.TestClass_ConstEnum;
  14844. begin
  14845. StartProgram(false);
  14846. Add([
  14847. 'type',
  14848. ' TEnum = (red,blue);',
  14849. ' TObject = class',
  14850. ' end;',
  14851. ' TAnimal = class',
  14852. ' public',
  14853. ' type TSubEnum = (light,dark);',
  14854. ' const a = high(TEnum);',
  14855. ' const b = high(TSubEnum);',
  14856. ' end;',
  14857. ' TBird = class(TAnimal)',
  14858. ' public',
  14859. ' const c = high(TEnum);',
  14860. ' const d = high(TSubEnum);',
  14861. ' end;',
  14862. ' TAnt = class',
  14863. ' public',
  14864. ' const e = high(TEnum);',
  14865. ' const f = high(TBird.TSubEnum);',
  14866. ' end;',
  14867. 'begin',
  14868. '']);
  14869. ConvertProgram;
  14870. CheckSource('TestClass_ConstEnum',
  14871. LinesToStr([
  14872. 'this.TEnum = {',
  14873. ' "0": "red",',
  14874. ' red: 0,',
  14875. ' "1": "blue",',
  14876. ' blue: 1',
  14877. '};',
  14878. 'rtl.createClass(this, "TObject", null, function () {',
  14879. ' this.$init = function () {',
  14880. ' };',
  14881. ' this.$final = function () {',
  14882. ' };',
  14883. '});',
  14884. 'rtl.createClass(this, "TAnimal", this.TObject, function () {',
  14885. ' this.TSubEnum = {',
  14886. ' "0": "light",',
  14887. ' light: 0,',
  14888. ' "1": "dark",',
  14889. ' dark: 1',
  14890. ' };',
  14891. ' this.a = $mod.TEnum.blue;',
  14892. ' this.b = this.TSubEnum.dark;',
  14893. '});',
  14894. 'rtl.createClass(this, "TBird", this.TAnimal, function () {',
  14895. ' this.c = $mod.TEnum.blue;',
  14896. ' this.d = this.TSubEnum.dark;',
  14897. '});',
  14898. 'rtl.createClass(this, "TAnt", this.TObject, function () {',
  14899. ' this.e = $mod.TEnum.blue;',
  14900. ' this.f = $mod.TAnimal.TSubEnum.dark;',
  14901. '});',
  14902. '']),
  14903. LinesToStr([
  14904. '']));
  14905. end;
  14906. procedure TTestModule.TestClass_LocalConstDuplicate_Prg;
  14907. begin
  14908. StartProgram(false);
  14909. Add([
  14910. 'type',
  14911. ' TObject = class',
  14912. ' const cI: longint = 3;',
  14913. ' procedure Fly;',
  14914. ' procedure Run;',
  14915. ' end;',
  14916. ' TBird = class',
  14917. ' procedure Go;',
  14918. ' end;',
  14919. 'procedure tobject.fly;',
  14920. 'const cI: word = 4;',
  14921. 'begin',
  14922. ' if cI=Self.cI then ;',
  14923. 'end;',
  14924. 'procedure tobject.run;',
  14925. 'const cI: word = 5;',
  14926. 'begin',
  14927. ' if cI=Self.cI then ;',
  14928. 'end;',
  14929. 'procedure tbird.go;',
  14930. 'const cI: word = 6;',
  14931. 'begin',
  14932. ' if cI=Self.cI then ;',
  14933. 'end;',
  14934. 'begin',
  14935. '']);
  14936. ConvertProgram;
  14937. CheckSource('TestClass_LocalConstDuplicate_Prg',
  14938. LinesToStr([
  14939. 'rtl.createClass(this, "TObject", null, function () {',
  14940. ' this.cI = 3;',
  14941. ' this.$init = function () {',
  14942. ' };',
  14943. ' this.$final = function () {',
  14944. ' };',
  14945. ' var cI$1 = 4;',
  14946. ' this.Fly = function () {',
  14947. ' if (cI$1 === this.cI) ;',
  14948. ' };',
  14949. ' var cI$2 = 5;',
  14950. ' this.Run = function () {',
  14951. ' if (cI$2 === this.cI) ;',
  14952. ' };',
  14953. '});',
  14954. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  14955. ' var cI$3 = 6;',
  14956. ' this.Go = function () {',
  14957. ' if (cI$3 === this.cI) ;',
  14958. ' };',
  14959. '});',
  14960. '']),
  14961. LinesToStr([
  14962. '']));
  14963. end;
  14964. procedure TTestModule.TestClass_LocalConstDuplicate_Unit;
  14965. begin
  14966. StartUnit(false);
  14967. Add([
  14968. 'interface',
  14969. 'type',
  14970. ' TObject = class',
  14971. ' const cI: longint = 3;',
  14972. ' procedure Fly;',
  14973. ' procedure Run;',
  14974. ' end;',
  14975. ' TBird = class',
  14976. ' procedure Go;',
  14977. ' end;',
  14978. 'implementation',
  14979. 'procedure tobject.fly;',
  14980. 'const cI: word = 4;',
  14981. 'begin',
  14982. ' if cI=Self.cI then ;',
  14983. 'end;',
  14984. 'procedure tobject.run;',
  14985. 'const cI: word = 5;',
  14986. 'begin',
  14987. ' if cI=Self.cI then ;',
  14988. 'end;',
  14989. 'procedure tbird.go;',
  14990. 'const cI: word = 6;',
  14991. 'begin',
  14992. ' if cI=Self.cI then ;',
  14993. 'end;',
  14994. '']);
  14995. ConvertUnit;
  14996. CheckSource('TestClass_LocalConstDuplicate_Unit',
  14997. LinesToStr([
  14998. 'rtl.createClass(this, "TObject", null, function () {',
  14999. ' this.cI = 3;',
  15000. ' this.$init = function () {',
  15001. ' };',
  15002. ' this.$final = function () {',
  15003. ' };',
  15004. ' var cI$1 = 4;',
  15005. ' this.Fly = function () {',
  15006. ' if (cI$1 === this.cI) ;',
  15007. ' };',
  15008. ' var cI$2 = 5;',
  15009. ' this.Run = function () {',
  15010. ' if (cI$2 === this.cI) ;',
  15011. ' };',
  15012. '});',
  15013. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15014. ' var cI$3 = 6;',
  15015. ' this.Go = function () {',
  15016. ' if (cI$3 === this.cI) ;',
  15017. ' };',
  15018. '});',
  15019. '']),
  15020. '',
  15021. '');
  15022. end;
  15023. procedure TTestModule.TestClass_LocalVarSelfFail;
  15024. begin
  15025. StartProgram(false);
  15026. Add([
  15027. 'type',
  15028. ' TObject = class',
  15029. ' constructor Create;',
  15030. ' end;',
  15031. 'constructor tobject.create;',
  15032. 'var self: longint;',
  15033. 'begin',
  15034. 'end',
  15035. 'begin',
  15036. '']);
  15037. SetExpectedPasResolverError('Duplicate identifier "self" at (0)',nDuplicateIdentifier);
  15038. ConvertProgram;
  15039. end;
  15040. procedure TTestModule.TestClass_ArgSelfFail;
  15041. begin
  15042. StartProgram(false);
  15043. Add([
  15044. 'type',
  15045. ' TObject = class',
  15046. ' procedure DoIt(Self: longint);',
  15047. ' end;',
  15048. 'procedure tobject.doit(self: longint);',
  15049. 'begin',
  15050. 'end',
  15051. 'begin',
  15052. '']);
  15053. SetExpectedPasResolverError('Duplicate identifier "Self" at test1.pp(5,24)',nDuplicateIdentifier);
  15054. ConvertProgram;
  15055. end;
  15056. procedure TTestModule.TestClass_NestedProcSelf;
  15057. begin
  15058. StartProgram(false);
  15059. Add([
  15060. 'type',
  15061. ' TObject = class',
  15062. ' Key: longint;',
  15063. ' class var State: longint;',
  15064. ' procedure DoIt;',
  15065. ' function GetSize: longint; virtual; abstract;',
  15066. ' procedure SetSize(Value: longint); virtual; abstract;',
  15067. ' property Size: longint read GetSize write SetSize;',
  15068. ' end;',
  15069. 'procedure tobject.doit;',
  15070. ' procedure Sub;',
  15071. ' begin',
  15072. ' key:=key+2;',
  15073. ' self.key:=self.key+3;',
  15074. ' state:=state+4;',
  15075. ' self.state:=self.state+5;',
  15076. ' tobject.state:=tobject.state+6;',
  15077. ' size:=size+7;',
  15078. ' self.size:=self.size+8;',
  15079. ' end;',
  15080. 'begin',
  15081. ' sub;',
  15082. ' key:=key+12;',
  15083. ' self.key:=self.key+13;',
  15084. ' state:=state+14;',
  15085. ' self.state:=self.state+15;',
  15086. ' tobject.state:=tobject.state+16;',
  15087. ' size:=size+17;',
  15088. ' self.size:=self.size+18;',
  15089. 'end;',
  15090. 'begin',
  15091. '']);
  15092. ConvertProgram;
  15093. CheckSource('TestClass_NestedProcSelf',
  15094. LinesToStr([ // statements
  15095. 'rtl.createClass(this, "TObject", null, function () {',
  15096. ' this.State = 0;',
  15097. ' this.$init = function () {',
  15098. ' this.Key = 0;',
  15099. ' };',
  15100. ' this.$final = function () {',
  15101. ' };',
  15102. ' this.DoIt = function () {',
  15103. ' var $Self = this;',
  15104. ' function Sub() {',
  15105. ' $Self.Key = $Self.Key + 2;',
  15106. ' $Self.Key = $Self.Key + 3;',
  15107. ' $mod.TObject.State = $Self.State + 4;',
  15108. ' $mod.TObject.State = $Self.State + 5;',
  15109. ' $mod.TObject.State = $mod.TObject.State + 6;',
  15110. ' $Self.SetSize($Self.GetSize() + 7);',
  15111. ' $Self.SetSize($Self.GetSize() + 8);',
  15112. ' };',
  15113. ' Sub();',
  15114. ' this.Key = this.Key + 12;',
  15115. ' $Self.Key = $Self.Key + 13;',
  15116. ' $mod.TObject.State = this.State + 14;',
  15117. ' $mod.TObject.State = $Self.State + 15;',
  15118. ' $mod.TObject.State = $mod.TObject.State + 16;',
  15119. ' this.SetSize(this.GetSize() + 17);',
  15120. ' $Self.SetSize($Self.GetSize() + 18);',
  15121. ' };',
  15122. '});',
  15123. '']),
  15124. LinesToStr([ // $mod.$main
  15125. '']));
  15126. end;
  15127. procedure TTestModule.TestClass_NestedProcSelf2;
  15128. begin
  15129. StartProgram(false);
  15130. Add([
  15131. 'type',
  15132. ' TObject = class',
  15133. ' Key: longint;',
  15134. ' class var State: longint;',
  15135. ' function GetSize: longint; virtual; abstract;',
  15136. ' procedure SetSize(Value: longint); virtual; abstract;',
  15137. ' property Size: longint read GetSize write SetSize;',
  15138. ' end;',
  15139. ' TBird = class',
  15140. ' procedure DoIt;',
  15141. ' end;',
  15142. 'procedure tbird.doit;',
  15143. ' procedure Sub;',
  15144. ' begin',
  15145. ' key:=key+2;',
  15146. ' self.key:=self.key+3;',
  15147. ' state:=state+4;',
  15148. ' self.state:=self.state+5;',
  15149. ' tobject.state:=tobject.state+6;',
  15150. ' size:=size+7;',
  15151. ' self.size:=self.size+8;',
  15152. ' end;',
  15153. 'begin',
  15154. ' sub;',
  15155. ' key:=key+12;',
  15156. ' self.key:=self.key+13;',
  15157. ' state:=state+14;',
  15158. ' self.state:=self.state+15;',
  15159. ' tobject.state:=tobject.state+16;',
  15160. ' size:=size+17;',
  15161. ' self.size:=self.size+18;',
  15162. 'end;',
  15163. 'begin',
  15164. '']);
  15165. ConvertProgram;
  15166. CheckSource('TestClass_NestedProcSelf2',
  15167. LinesToStr([ // statements
  15168. 'rtl.createClass(this, "TObject", null, function () {',
  15169. ' this.State = 0;',
  15170. ' this.$init = function () {',
  15171. ' this.Key = 0;',
  15172. ' };',
  15173. ' this.$final = function () {',
  15174. ' };',
  15175. '});',
  15176. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15177. ' this.DoIt = function () {',
  15178. ' var $Self = this;',
  15179. ' function Sub() {',
  15180. ' $Self.Key = $Self.Key + 2;',
  15181. ' $Self.Key = $Self.Key + 3;',
  15182. ' $mod.TObject.State = $Self.State + 4;',
  15183. ' $mod.TObject.State = $Self.State + 5;',
  15184. ' $mod.TObject.State = $mod.TObject.State + 6;',
  15185. ' $Self.SetSize($Self.GetSize() + 7);',
  15186. ' $Self.SetSize($Self.GetSize() + 8);',
  15187. ' };',
  15188. ' Sub();',
  15189. ' this.Key = this.Key + 12;',
  15190. ' $Self.Key = $Self.Key + 13;',
  15191. ' $mod.TObject.State = this.State + 14;',
  15192. ' $mod.TObject.State = $Self.State + 15;',
  15193. ' $mod.TObject.State = $mod.TObject.State + 16;',
  15194. ' this.SetSize(this.GetSize() + 17);',
  15195. ' $Self.SetSize($Self.GetSize() + 18);',
  15196. ' };',
  15197. '});',
  15198. '']),
  15199. LinesToStr([ // $mod.$main
  15200. '']));
  15201. end;
  15202. procedure TTestModule.TestClass_NestedProcClassSelf;
  15203. begin
  15204. StartProgram(false);
  15205. Add([
  15206. 'type',
  15207. ' TObject = class',
  15208. ' class var State: longint;',
  15209. ' class procedure DoIt;',
  15210. ' class function GetSize: longint; virtual; abstract;',
  15211. ' class procedure SetSize(Value: longint); virtual; abstract;',
  15212. ' class property Size: longint read GetSize write SetSize;',
  15213. ' end;',
  15214. 'class procedure tobject.doit;',
  15215. ' procedure Sub;',
  15216. ' begin',
  15217. ' state:=state+2;',
  15218. ' self.state:=self.state+3;',
  15219. ' tobject.state:=tobject.state+4;',
  15220. ' size:=size+5;',
  15221. ' self.size:=self.size+6;',
  15222. ' tobject.size:=tobject.size+7;',
  15223. ' end;',
  15224. 'begin',
  15225. ' sub;',
  15226. ' state:=state+12;',
  15227. ' self.state:=self.state+13;',
  15228. ' tobject.state:=tobject.state+14;',
  15229. ' size:=size+15;',
  15230. ' self.size:=self.size+16;',
  15231. ' tobject.size:=tobject.size+17;',
  15232. 'end;',
  15233. 'begin',
  15234. '']);
  15235. ConvertProgram;
  15236. CheckSource('TestClass_NestedProcClassSelf',
  15237. LinesToStr([ // statements
  15238. 'rtl.createClass(this, "TObject", null, function () {',
  15239. ' this.State = 0;',
  15240. ' this.$init = function () {',
  15241. ' };',
  15242. ' this.$final = function () {',
  15243. ' };',
  15244. ' this.DoIt = function () {',
  15245. ' var $Self = this;',
  15246. ' function Sub() {',
  15247. ' $mod.TObject.State = $Self.State + 2;',
  15248. ' $mod.TObject.State = $Self.State + 3;',
  15249. ' $mod.TObject.State = $mod.TObject.State + 4;',
  15250. ' $Self.SetSize($Self.GetSize() + 5);',
  15251. ' $Self.SetSize($Self.GetSize() + 6);',
  15252. ' $mod.TObject.SetSize($mod.TObject.GetSize() + 7);',
  15253. ' };',
  15254. ' Sub();',
  15255. ' $mod.TObject.State = this.State + 12;',
  15256. ' $mod.TObject.State = $Self.State + 13;',
  15257. ' $mod.TObject.State = $mod.TObject.State + 14;',
  15258. ' this.SetSize(this.GetSize() + 15);',
  15259. ' $Self.SetSize($Self.GetSize() + 16);',
  15260. ' $mod.TObject.SetSize($mod.TObject.GetSize() + 17);',
  15261. ' };',
  15262. '});',
  15263. '']),
  15264. LinesToStr([ // $mod.$main
  15265. '']));
  15266. end;
  15267. procedure TTestModule.TestClass_NestedProcCallInherited;
  15268. begin
  15269. StartProgram(false);
  15270. Add([
  15271. 'type',
  15272. ' TObject = class',
  15273. ' function DoIt(k: boolean): longint; virtual;',
  15274. ' end;',
  15275. ' TBird = class',
  15276. ' function DoIt(k: boolean): longint; override;',
  15277. ' end;',
  15278. 'function tobject.doit(k: boolean): longint;',
  15279. 'begin',
  15280. 'end;',
  15281. 'function tbird.doit(k: boolean): longint;',
  15282. ' procedure Sub;',
  15283. ' begin',
  15284. ' inherited DoIt(true);',
  15285. //' if inherited DoIt(false)=4 then ;',
  15286. ' end;',
  15287. 'begin',
  15288. ' Sub;',
  15289. ' inherited;',
  15290. ' inherited DoIt(true);',
  15291. //' if inherited DoIt(false)=14 then ;',
  15292. 'end;',
  15293. 'begin',
  15294. '']);
  15295. ConvertProgram;
  15296. CheckSource('TestClass_NestedProcCallInherited',
  15297. LinesToStr([ // statements
  15298. 'rtl.createClass(this, "TObject", null, function () {',
  15299. ' this.$init = function () {',
  15300. ' };',
  15301. ' this.$final = function () {',
  15302. ' };',
  15303. ' this.DoIt = function (k) {',
  15304. ' var Result = 0;',
  15305. ' return Result;',
  15306. ' };',
  15307. '});',
  15308. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15309. ' this.DoIt = function (k) {',
  15310. ' var $Self = this;',
  15311. ' var Result = 0;',
  15312. ' function Sub() {',
  15313. ' $mod.TObject.DoIt.call($Self, true);',
  15314. ' };',
  15315. ' Sub();',
  15316. ' $mod.TObject.DoIt.apply(this, arguments);',
  15317. ' $mod.TObject.DoIt.call(this, true);',
  15318. ' return Result;',
  15319. ' };',
  15320. '});',
  15321. '']),
  15322. LinesToStr([ // $mod.$main
  15323. '']));
  15324. end;
  15325. procedure TTestModule.TestClass_TObjectFree;
  15326. begin
  15327. StartProgram(false);
  15328. Add([
  15329. 'type',
  15330. ' TObject = class',
  15331. ' Obj: tobject;',
  15332. ' procedure Free;',
  15333. ' procedure Release;',
  15334. ' end;',
  15335. 'procedure tobject.free;',
  15336. 'begin',
  15337. 'end;',
  15338. 'procedure tobject.release;',
  15339. 'begin',
  15340. ' free;',
  15341. ' if true then free;',
  15342. 'end;',
  15343. 'function DoIt(o: tobject): tobject;',
  15344. 'var l: tobject;',
  15345. 'begin',
  15346. ' o.free;',
  15347. ' o.free();',
  15348. ' l.free;',
  15349. ' l.free();',
  15350. ' o.obj.free;',
  15351. ' o.obj.free();',
  15352. ' with o do obj.free;',
  15353. ' with o do obj.free();',
  15354. ' result.Free;',
  15355. ' result.Free();',
  15356. 'end;',
  15357. 'var o: tobject;',
  15358. ' a: array of tobject;',
  15359. 'begin',
  15360. ' o.free;',
  15361. ' o.obj.free;',
  15362. ' a[1+2].free;',
  15363. '']);
  15364. ConvertProgram;
  15365. CheckSource('TestClass_TObjectFree',
  15366. LinesToStr([ // statements
  15367. 'rtl.createClass(this, "TObject", null, function () {',
  15368. ' this.$init = function () {',
  15369. ' this.Obj = null;',
  15370. ' };',
  15371. ' this.$final = function () {',
  15372. ' this.Obj = undefined;',
  15373. ' };',
  15374. ' this.Free = function () {',
  15375. ' };',
  15376. ' this.Release = function () {',
  15377. ' this.Free();',
  15378. ' if (true) this.Free();',
  15379. ' };',
  15380. '});',
  15381. 'this.DoIt = function (o) {',
  15382. ' var Result = null;',
  15383. ' var l = null;',
  15384. ' o = rtl.freeLoc(o);',
  15385. ' o = rtl.freeLoc(o);',
  15386. ' l = rtl.freeLoc(l);',
  15387. ' l = rtl.freeLoc(l);',
  15388. ' rtl.free(o, "Obj");',
  15389. ' rtl.free(o, "Obj");',
  15390. ' rtl.free(o, "Obj");',
  15391. ' rtl.free(o, "Obj");',
  15392. ' Result = rtl.freeLoc(Result);',
  15393. ' Result = rtl.freeLoc(Result);',
  15394. ' return Result;',
  15395. '};',
  15396. 'this.o = null;',
  15397. 'this.a = [];',
  15398. '']),
  15399. LinesToStr([ // $mod.$main
  15400. 'rtl.free($mod, "o");',
  15401. 'rtl.free($mod.o, "Obj");',
  15402. 'rtl.free($mod.a, 1 + 2);',
  15403. '']));
  15404. end;
  15405. procedure TTestModule.TestClass_TObjectFree_VarArg;
  15406. begin
  15407. StartProgram(false);
  15408. Add([
  15409. 'type',
  15410. ' TObject = class',
  15411. ' Obj: tobject;',
  15412. ' procedure Free;',
  15413. ' end;',
  15414. 'procedure tobject.free;',
  15415. 'begin',
  15416. 'end;',
  15417. 'procedure DoIt(var o: tobject);',
  15418. 'begin',
  15419. ' o.free;',
  15420. ' o.free();',
  15421. 'end;',
  15422. 'begin',
  15423. '']);
  15424. ConvertProgram;
  15425. CheckSource('TestClass_TObjectFree_VarArg',
  15426. LinesToStr([ // statements
  15427. 'rtl.createClass(this, "TObject", null, function () {',
  15428. ' this.$init = function () {',
  15429. ' this.Obj = null;',
  15430. ' };',
  15431. ' this.$final = function () {',
  15432. ' this.Obj = undefined;',
  15433. ' };',
  15434. ' this.Free = function () {',
  15435. ' };',
  15436. '});',
  15437. 'this.DoIt = function (o) {',
  15438. ' o.set(rtl.freeLoc(o.get()));',
  15439. ' o.set(rtl.freeLoc(o.get()));',
  15440. '};',
  15441. '']),
  15442. LinesToStr([ // $mod.$main
  15443. '']));
  15444. end;
  15445. procedure TTestModule.TestClass_TObjectFreeNewInstance;
  15446. begin
  15447. StartProgram(false);
  15448. Add([
  15449. 'type',
  15450. ' TObject = class',
  15451. ' constructor Create;',
  15452. ' procedure Free;',
  15453. ' end;',
  15454. 'constructor TObject.Create; begin end;',
  15455. 'procedure tobject.free; begin end;',
  15456. 'begin',
  15457. ' with tobject.create do free;',
  15458. '']);
  15459. ConvertProgram;
  15460. CheckSource('TestClass_TObjectFreeNewInstance',
  15461. LinesToStr([ // statements
  15462. 'rtl.createClass(this, "TObject", null, function () {',
  15463. ' this.$init = function () {',
  15464. ' };',
  15465. ' this.$final = function () {',
  15466. ' };',
  15467. ' this.Create = function () {',
  15468. ' return this;',
  15469. ' };',
  15470. ' this.Free = function () {',
  15471. ' };',
  15472. '});',
  15473. '']),
  15474. LinesToStr([ // $mod.$main
  15475. 'var $with = $mod.TObject.$create("Create");',
  15476. '$with=rtl.freeLoc($with);',
  15477. '']));
  15478. end;
  15479. procedure TTestModule.TestClass_TObjectFreeLowerCase;
  15480. begin
  15481. StartProgram(false);
  15482. Add([
  15483. 'type',
  15484. ' TObject = class',
  15485. ' destructor Destroy;',
  15486. ' procedure Free;',
  15487. ' end;',
  15488. 'destructor TObject.Destroy; begin end;',
  15489. 'procedure tobject.free; begin end;',
  15490. 'var o: tobject;',
  15491. 'begin',
  15492. ' o.free;',
  15493. '']);
  15494. Converter.UseLowerCase:=true;
  15495. ConvertProgram;
  15496. CheckSource('TestClass_TObjectFreeLowerCase',
  15497. LinesToStr([ // statements
  15498. 'rtl.createClass(this, "tobject", null, function () {',
  15499. ' this.$init = function () {',
  15500. ' };',
  15501. ' this.$final = function () {',
  15502. ' };',
  15503. ' rtl.tObjectDestroy = "destroy";',
  15504. ' this.destroy = function () {',
  15505. ' };',
  15506. ' this.free = function () {',
  15507. ' };',
  15508. '});',
  15509. 'this.o = null;',
  15510. '']),
  15511. LinesToStr([ // $mod.$main
  15512. 'rtl.free($mod, "o");',
  15513. '']));
  15514. end;
  15515. procedure TTestModule.TestClass_TObjectFreeFunctionFail;
  15516. begin
  15517. StartProgram(false);
  15518. Add([
  15519. 'type',
  15520. ' TObject = class',
  15521. ' procedure Free;',
  15522. ' function GetObj: tobject; virtual; abstract;',
  15523. ' end;',
  15524. 'procedure tobject.free;',
  15525. 'begin',
  15526. 'end;',
  15527. 'var o: tobject;',
  15528. 'begin',
  15529. ' o.getobj.free;',
  15530. '']);
  15531. SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
  15532. ConvertProgram;
  15533. end;
  15534. procedure TTestModule.TestClass_TObjectFreePropertyFail;
  15535. begin
  15536. StartProgram(false);
  15537. Add([
  15538. 'type',
  15539. ' TObject = class',
  15540. ' procedure Free;',
  15541. ' FObj: TObject;',
  15542. ' property Obj: tobject read FObj write FObj;',
  15543. ' end;',
  15544. 'procedure tobject.free;',
  15545. 'begin',
  15546. 'end;',
  15547. 'var o: tobject;',
  15548. 'begin',
  15549. ' o.obj.free;',
  15550. '']);
  15551. SetExpectedPasResolverError(sFreeNeedsVar,nFreeNeedsVar);
  15552. ConvertProgram;
  15553. end;
  15554. procedure TTestModule.TestClass_ForIn;
  15555. begin
  15556. StartProgram(false);
  15557. Add([
  15558. 'type',
  15559. ' TObject = class end;',
  15560. ' TItem = TObject;',
  15561. ' TEnumerator = class',
  15562. ' FCurrent: TItem;',
  15563. ' property Current: TItem read FCurrent;',
  15564. ' function MoveNext: boolean;',
  15565. ' end;',
  15566. ' TBird = class',
  15567. ' function GetEnumerator: TEnumerator;',
  15568. ' end;',
  15569. 'function TEnumerator.MoveNext: boolean;',
  15570. 'begin',
  15571. 'end;',
  15572. 'function TBird.GetEnumerator: TEnumerator;',
  15573. 'begin',
  15574. 'end;',
  15575. 'var',
  15576. ' b: TBird;',
  15577. ' i, i2: TItem;',
  15578. 'begin',
  15579. ' for i in b do i2:=i;']);
  15580. ConvertProgram;
  15581. CheckSource('TestClass_ForIn',
  15582. LinesToStr([ // statements
  15583. 'rtl.createClass(this, "TObject", null, function () {',
  15584. ' this.$init = function () {',
  15585. ' };',
  15586. ' this.$final = function () {',
  15587. ' };',
  15588. '});',
  15589. 'rtl.createClass(this, "TEnumerator", this.TObject, function () {',
  15590. ' this.$init = function () {',
  15591. ' $mod.TObject.$init.call(this);',
  15592. ' this.FCurrent = null;',
  15593. ' };',
  15594. ' this.$final = function () {',
  15595. ' this.FCurrent = undefined;',
  15596. ' $mod.TObject.$final.call(this);',
  15597. ' };',
  15598. ' this.MoveNext = function () {',
  15599. ' var Result = false;',
  15600. ' return Result;',
  15601. ' };',
  15602. '});',
  15603. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15604. ' this.GetEnumerator = function () {',
  15605. ' var Result = null;',
  15606. ' return Result;',
  15607. ' };',
  15608. '});',
  15609. 'this.b = null;',
  15610. 'this.i = null;',
  15611. 'this.i2 = null;'
  15612. ]),
  15613. LinesToStr([ // $mod.$main
  15614. 'var $in = $mod.b.GetEnumerator();',
  15615. 'try {',
  15616. ' while ($in.MoveNext()){',
  15617. ' $mod.i = $in.FCurrent;',
  15618. ' $mod.i2 = $mod.i;',
  15619. ' }',
  15620. '} finally {',
  15621. ' $in = rtl.freeLoc($in)',
  15622. '};',
  15623. '']));
  15624. end;
  15625. procedure TTestModule.TestClass_DispatchMessage;
  15626. begin
  15627. StartProgram(false);
  15628. Add([
  15629. 'type',
  15630. ' TObject = class',
  15631. ' {$DispatchField DispInt}',
  15632. ' procedure Dispatch(var Msg); virtual; abstract;',
  15633. ' {$DispatchStrField DispStr}',
  15634. ' procedure DispatchStr(var Msg); virtual; abstract;',
  15635. ' end;',
  15636. ' THopMsg = record',
  15637. ' DispInt: longint;',
  15638. ' end;',
  15639. ' TPutMsg = record',
  15640. ' DispStr: string;',
  15641. ' end;',
  15642. ' TBird = class',
  15643. ' procedure Fly(var Msg); virtual; abstract; message 2;',
  15644. ' procedure Run; overload; virtual; abstract;',
  15645. ' procedure Run(var Msg); overload; message ''Fast'';',
  15646. ' procedure Hop(var Msg: THopMsg); virtual; abstract; message 3;',
  15647. ' procedure Put(var Msg: TPutMsg); virtual; abstract; message ''foo'';',
  15648. ' end;',
  15649. 'procedure TBird.Run(var Msg);',
  15650. 'begin',
  15651. 'end;',
  15652. 'begin',
  15653. '']);
  15654. ConvertProgram;
  15655. CheckSource('TestClass_Message',
  15656. LinesToStr([ // statements
  15657. 'rtl.createClass(this, "TObject", null, function () {',
  15658. ' this.$init = function () {',
  15659. ' };',
  15660. ' this.$final = function () {',
  15661. ' };',
  15662. '});',
  15663. 'rtl.recNewT(this, "THopMsg", function () {',
  15664. ' this.DispInt = 0;',
  15665. ' this.$eq = function (b) {',
  15666. ' return this.DispInt === b.DispInt;',
  15667. ' };',
  15668. ' this.$assign = function (s) {',
  15669. ' this.DispInt = s.DispInt;',
  15670. ' return this;',
  15671. ' };',
  15672. '});',
  15673. 'rtl.recNewT(this, "TPutMsg", function () {',
  15674. ' this.DispStr = "";',
  15675. ' this.$eq = function (b) {',
  15676. ' return this.DispStr === b.DispStr;',
  15677. ' };',
  15678. ' this.$assign = function (s) {',
  15679. ' this.DispStr = s.DispStr;',
  15680. ' return this;',
  15681. ' };',
  15682. '});',
  15683. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  15684. ' this.Run$1 = function (Msg) {',
  15685. ' };',
  15686. ' this.$msgint = {',
  15687. ' "2": "Fly",',
  15688. ' "3": "Hop"',
  15689. ' };',
  15690. ' this.$msgstr = {',
  15691. ' Fast: "Run$1",',
  15692. ' foo: "Put"',
  15693. ' };',
  15694. '});',
  15695. '']),
  15696. LinesToStr([ // $mod.$main
  15697. '']));
  15698. end;
  15699. procedure TTestModule.TestClass_Message_DuplicateIntFail;
  15700. begin
  15701. StartProgram(false);
  15702. Add([
  15703. 'type',
  15704. ' TObject = class',
  15705. ' procedure Fly(var Msg); virtual; abstract; message 3;',
  15706. ' procedure Run(var Msg); virtual; abstract; message 1+2;',
  15707. ' end;',
  15708. 'begin',
  15709. '']);
  15710. SetExpectedPasResolverError('Duplicate message id "3" at test1.pp(5,56)',nDuplicateMessageIdXAtY);
  15711. ConvertProgram;
  15712. end;
  15713. procedure TTestModule.TestClass_DispatchMessage_WrongFieldNameFail;
  15714. begin
  15715. StartProgram(false);
  15716. Add([
  15717. 'type',
  15718. ' TObject = class',
  15719. ' {$dispatchfield Msg}',
  15720. ' procedure Dispatch(var Msg); virtual; abstract;',
  15721. ' end;',
  15722. ' TFlyMsg = record',
  15723. ' FlyId: longint;',
  15724. ' end;',
  15725. ' TBird = class',
  15726. ' procedure Fly(var Msg: TFlyMsg); virtual; abstract; message 3;',
  15727. ' end;',
  15728. 'begin',
  15729. '']);
  15730. ConvertProgram;
  15731. CheckHint(mtWarning,nDispatchRequiresX,'Dispatch requires record field "Msg"');
  15732. end;
  15733. procedure TTestModule.TestClassOf_Create;
  15734. begin
  15735. StartProgram(false);
  15736. Add('type');
  15737. Add(' TObject = class');
  15738. Add(' constructor Create;');
  15739. Add(' end;');
  15740. Add(' TClass = class of TObject;');
  15741. Add('constructor tobject.create; begin end;');
  15742. Add('var');
  15743. Add(' Obj: tobject;');
  15744. Add(' C: tclass;');
  15745. Add('begin');
  15746. Add(' obj:=C.create;');
  15747. Add(' with c do obj:=create;');
  15748. ConvertProgram;
  15749. CheckSource('TestClassOf_Create',
  15750. LinesToStr([ // statements
  15751. 'rtl.createClass(this, "TObject", null, function () {',
  15752. ' this.$init = function () {',
  15753. ' };',
  15754. ' this.$final = function () {',
  15755. ' };',
  15756. ' this.Create = function () {',
  15757. ' return this;',
  15758. ' };',
  15759. '});',
  15760. 'this.Obj = null;',
  15761. 'this.C = null;'
  15762. ]),
  15763. LinesToStr([ // $mod.$main
  15764. '$mod.Obj = $mod.C.$create("Create");',
  15765. 'var $with = $mod.C;',
  15766. '$mod.Obj = $with.$create("Create");',
  15767. '']));
  15768. end;
  15769. procedure TTestModule.TestClassOf_Call;
  15770. begin
  15771. StartProgram(false);
  15772. Add('type');
  15773. Add(' TObject = class');
  15774. Add(' class procedure DoIt;');
  15775. Add(' end;');
  15776. Add(' TClass = class of TObject;');
  15777. Add('class procedure tobject.doit; begin end;');
  15778. Add('var');
  15779. Add(' C: tclass;');
  15780. Add('begin');
  15781. Add(' c.doit;');
  15782. Add(' with c do doit;');
  15783. ConvertProgram;
  15784. CheckSource('TestClassOf_Call',
  15785. LinesToStr([ // statements
  15786. 'rtl.createClass(this, "TObject", null, function () {',
  15787. ' this.$init = function () {',
  15788. ' };',
  15789. ' this.$final = function () {',
  15790. ' };',
  15791. ' this.DoIt = function () {',
  15792. ' };',
  15793. '});',
  15794. 'this.C = null;'
  15795. ]),
  15796. LinesToStr([ // $mod.$main
  15797. '$mod.C.DoIt();',
  15798. 'var $with = $mod.C;',
  15799. '$with.DoIt();',
  15800. '']));
  15801. end;
  15802. procedure TTestModule.TestClassOf_Assign;
  15803. begin
  15804. StartProgram(false);
  15805. Add('type');
  15806. Add(' TClass = class of TObject;');
  15807. Add(' TObject = class');
  15808. Add(' ClassType: TClass; ');
  15809. Add(' end;');
  15810. Add('var');
  15811. Add(' Obj: tobject;');
  15812. Add(' C: tclass;');
  15813. Add('begin');
  15814. Add(' c:=nil;');
  15815. Add(' c:=obj.classtype;');
  15816. ConvertProgram;
  15817. CheckSource('TestClassOf_Assign',
  15818. LinesToStr([ // statements
  15819. 'rtl.createClass(this, "TObject", null, function () {',
  15820. ' this.$init = function () {',
  15821. ' this.ClassType = null;',
  15822. ' };',
  15823. ' this.$final = function () {',
  15824. ' this.ClassType = undefined;',
  15825. ' };',
  15826. '});',
  15827. 'this.Obj = null;',
  15828. 'this.C = null;'
  15829. ]),
  15830. LinesToStr([ // $mod.$main
  15831. '$mod.C = null;',
  15832. '$mod.C = $mod.Obj.ClassType;',
  15833. '']));
  15834. end;
  15835. procedure TTestModule.TestClassOf_Is;
  15836. begin
  15837. StartProgram(false);
  15838. Add('type');
  15839. Add(' TClass = class of TObject;');
  15840. Add(' TObject = class');
  15841. Add(' end;');
  15842. Add(' TCar = class');
  15843. Add(' end;');
  15844. Add(' TCars = class of TCar;');
  15845. Add('var');
  15846. Add(' Obj: tobject;');
  15847. Add(' C: tclass;');
  15848. Add(' Cars: tcars;');
  15849. Add('begin');
  15850. Add(' if c is tcar then ;');
  15851. Add(' if c is tcars then ;');
  15852. ConvertProgram;
  15853. CheckSource('TestClassOf_Is',
  15854. LinesToStr([ // statements
  15855. 'rtl.createClass(this, "TObject", null, function () {',
  15856. ' this.$init = function () {',
  15857. ' };',
  15858. ' this.$final = function () {',
  15859. ' };',
  15860. '});',
  15861. 'rtl.createClass(this, "TCar", this.TObject, function () {',
  15862. '});',
  15863. 'this.Obj = null;',
  15864. 'this.C = null;',
  15865. 'this.Cars = null;'
  15866. ]),
  15867. LinesToStr([ // $mod.$main
  15868. 'if(rtl.is($mod.C,$mod.TCar));',
  15869. 'if(rtl.is($mod.C,$mod.TCar));',
  15870. '']));
  15871. end;
  15872. procedure TTestModule.TestClassOf_Compare;
  15873. begin
  15874. StartProgram(false);
  15875. Add('type');
  15876. Add(' TClass = class of TObject;');
  15877. Add(' TObject = class');
  15878. Add(' ClassType: TClass; ');
  15879. Add(' end;');
  15880. Add('var');
  15881. Add(' b: boolean;');
  15882. Add(' Obj: tobject;');
  15883. Add(' C: tclass;');
  15884. Add('begin');
  15885. Add(' b:=c=nil;');
  15886. Add(' b:=nil=c;');
  15887. Add(' b:=c=obj.classtype;');
  15888. Add(' b:=obj.classtype=c;');
  15889. Add(' b:=c=TObject;');
  15890. Add(' b:=TObject=c;');
  15891. Add(' b:=c<>nil;');
  15892. Add(' b:=nil<>c;');
  15893. Add(' b:=c<>obj.classtype;');
  15894. Add(' b:=obj.classtype<>c;');
  15895. Add(' b:=c<>TObject;');
  15896. Add(' b:=TObject<>c;');
  15897. ConvertProgram;
  15898. CheckSource('TestClassOf_Compare',
  15899. LinesToStr([ // statements
  15900. 'rtl.createClass(this, "TObject", null, function () {',
  15901. ' this.$init = function () {',
  15902. ' this.ClassType = null;',
  15903. ' };',
  15904. ' this.$final = function () {',
  15905. ' this.ClassType = undefined;',
  15906. ' };',
  15907. '});',
  15908. 'this.b = false;',
  15909. 'this.Obj = null;',
  15910. 'this.C = null;'
  15911. ]),
  15912. LinesToStr([ // $mod.$main
  15913. '$mod.b = $mod.C === null;',
  15914. '$mod.b = null === $mod.C;',
  15915. '$mod.b = $mod.C === $mod.Obj.ClassType;',
  15916. '$mod.b = $mod.Obj.ClassType === $mod.C;',
  15917. '$mod.b = $mod.C === $mod.TObject;',
  15918. '$mod.b = $mod.TObject === $mod.C;',
  15919. '$mod.b = $mod.C !== null;',
  15920. '$mod.b = null !== $mod.C;',
  15921. '$mod.b = $mod.C !== $mod.Obj.ClassType;',
  15922. '$mod.b = $mod.Obj.ClassType !== $mod.C;',
  15923. '$mod.b = $mod.C !== $mod.TObject;',
  15924. '$mod.b = $mod.TObject !== $mod.C;',
  15925. '']));
  15926. end;
  15927. procedure TTestModule.TestClassOf_ClassVar;
  15928. begin
  15929. StartProgram(false);
  15930. Add('type');
  15931. Add(' TObject = class');
  15932. Add(' class var id: longint;');
  15933. Add(' end;');
  15934. Add(' TClass = class of TObject;');
  15935. Add('var');
  15936. Add(' C: tclass;');
  15937. Add('begin');
  15938. Add(' C.id:=C.id;');
  15939. ConvertProgram;
  15940. CheckSource('TestClassOf_ClassVar',
  15941. LinesToStr([ // statements
  15942. 'rtl.createClass(this, "TObject", null, function () {',
  15943. ' this.id = 0;',
  15944. ' this.$init = function () {',
  15945. ' };',
  15946. ' this.$final = function () {',
  15947. ' };',
  15948. '});',
  15949. 'this.C = null;'
  15950. ]),
  15951. LinesToStr([ // $mod.$main
  15952. '$mod.TObject.id = $mod.C.id;',
  15953. '']));
  15954. end;
  15955. procedure TTestModule.TestClassOf_ClassMethod;
  15956. begin
  15957. StartProgram(false);
  15958. Add('type');
  15959. Add(' TObject = class');
  15960. Add(' class function DoIt(i: longint = 0): longint;');
  15961. Add(' end;');
  15962. Add(' TClass = class of TObject;');
  15963. Add('class function tobject.doit(i: longint = 0): longint; begin end;');
  15964. Add('var');
  15965. Add(' i: longint;');
  15966. Add(' C: tclass;');
  15967. Add('begin');
  15968. Add(' C.DoIt;');
  15969. Add(' C.DoIt();');
  15970. Add(' i:=C.DoIt;');
  15971. Add(' i:=C.DoIt();');
  15972. ConvertProgram;
  15973. CheckSource('TestClassOf_ClassMethod',
  15974. LinesToStr([ // statements
  15975. 'rtl.createClass(this, "TObject", null, function () {',
  15976. ' this.$init = function () {',
  15977. ' };',
  15978. ' this.$final = function () {',
  15979. ' };',
  15980. ' this.DoIt = function (i) {',
  15981. ' var Result = 0;',
  15982. ' return Result;',
  15983. ' };',
  15984. '});',
  15985. 'this.i = 0;',
  15986. 'this.C = null;'
  15987. ]),
  15988. LinesToStr([ // $mod.$main
  15989. '$mod.C.DoIt(0);',
  15990. '$mod.C.DoIt(0);',
  15991. '$mod.i = $mod.C.DoIt(0);',
  15992. '$mod.i = $mod.C.DoIt(0);',
  15993. '']));
  15994. end;
  15995. procedure TTestModule.TestClassOf_ClassProperty;
  15996. begin
  15997. StartProgram(false);
  15998. Add([
  15999. 'type',
  16000. ' TObject = class',
  16001. ' class var FA: longint;',
  16002. ' class function GetA: longint;',
  16003. ' class procedure SetA(Value: longint);',
  16004. ' class property pA: longint read fa write fa;',
  16005. ' class property pB: longint read geta write seta;',
  16006. ' end;',
  16007. ' TObjectClass = class of tobject;',
  16008. 'class function tobject.geta: longint; begin end;',
  16009. 'class procedure tobject.seta(value: longint); begin end;',
  16010. 'var',
  16011. ' b: boolean;',
  16012. ' Obj: tobject;',
  16013. ' Cla: tobjectclass;',
  16014. 'begin',
  16015. ' obj.pa:=obj.pa;',
  16016. ' obj.pb:=obj.pb;',
  16017. ' b:=obj.pa=4;',
  16018. ' b:=obj.pb=obj.pb;',
  16019. ' b:=5=obj.pa;',
  16020. ' cla.pa:=6;',
  16021. ' cla.pa:=cla.pa;',
  16022. ' cla.pb:=cla.pb;',
  16023. ' b:=cla.pa=7;',
  16024. ' b:=cla.pb=cla.pb;',
  16025. ' b:=8=cla.pa;',
  16026. ' tobject.pa:=9;',
  16027. ' tobject.pb:=tobject.pb;',
  16028. ' b:=tobject.pa=10;',
  16029. ' b:=11=tobject.pa;',
  16030. '']);
  16031. ConvertProgram;
  16032. CheckSource('TestClassOf_ClassProperty',
  16033. LinesToStr([ // statements
  16034. 'rtl.createClass(this, "TObject", null, function () {',
  16035. ' this.FA = 0;',
  16036. ' this.$init = function () {',
  16037. ' };',
  16038. ' this.$final = function () {',
  16039. ' };',
  16040. ' this.GetA = function () {',
  16041. ' var Result = 0;',
  16042. ' return Result;',
  16043. ' };',
  16044. ' this.SetA = function (Value) {',
  16045. ' };',
  16046. '});',
  16047. 'this.b = false;',
  16048. 'this.Obj = null;',
  16049. 'this.Cla = null;'
  16050. ]),
  16051. LinesToStr([ // $mod.$main
  16052. '$mod.TObject.FA = $mod.Obj.FA;',
  16053. '$mod.Obj.$class.SetA($mod.Obj.$class.GetA());',
  16054. '$mod.b = $mod.Obj.FA === 4;',
  16055. '$mod.b = $mod.Obj.$class.GetA() === $mod.Obj.$class.GetA();',
  16056. '$mod.b = 5 === $mod.Obj.FA;',
  16057. '$mod.TObject.FA = 6;',
  16058. '$mod.TObject.FA = $mod.Cla.FA;',
  16059. '$mod.Cla.SetA($mod.Cla.GetA());',
  16060. '$mod.b = $mod.Cla.FA === 7;',
  16061. '$mod.b = $mod.Cla.GetA() === $mod.Cla.GetA();',
  16062. '$mod.b = 8 === $mod.Cla.FA;',
  16063. '$mod.TObject.FA = 9;',
  16064. '$mod.TObject.SetA($mod.TObject.GetA());',
  16065. '$mod.b = $mod.TObject.FA === 10;',
  16066. '$mod.b = 11 === $mod.TObject.FA;',
  16067. '']));
  16068. end;
  16069. procedure TTestModule.TestClassOf_ClassMethodSelf;
  16070. begin
  16071. StartProgram(false);
  16072. Add('type');
  16073. Add(' TObject = class');
  16074. Add(' class var GlobalId: longint;');
  16075. Add(' class procedure ProcA;');
  16076. Add(' end;');
  16077. Add('class procedure tobject.proca;');
  16078. Add('var b: boolean;');
  16079. Add('begin');
  16080. Add(' b:=self=nil;');
  16081. Add(' b:=self.globalid=3;');
  16082. Add(' b:=4=self.globalid;');
  16083. Add(' self.globalid:=5;');
  16084. Add(' self.proca;');
  16085. Add('end;');
  16086. Add('begin');
  16087. ConvertProgram;
  16088. CheckSource('TestClassOf_ClassMethodSelf',
  16089. LinesToStr([ // statements
  16090. 'rtl.createClass(this, "TObject", null, function () {',
  16091. ' this.GlobalId = 0;',
  16092. ' this.$init = function () {',
  16093. ' };',
  16094. ' this.$final = function () {',
  16095. ' };',
  16096. ' this.ProcA = function () {',
  16097. ' var b = false;',
  16098. ' b = this === null;',
  16099. ' b = this.GlobalId === 3;',
  16100. ' b = 4 === this.GlobalId;',
  16101. ' $mod.TObject.GlobalId = 5;',
  16102. ' this.ProcA();',
  16103. ' };',
  16104. '});'
  16105. ]),
  16106. LinesToStr([ // $mod.$main
  16107. '']));
  16108. end;
  16109. procedure TTestModule.TestClassOf_TypeCast;
  16110. begin
  16111. StartProgram(false);
  16112. Add('type');
  16113. Add(' TObject = class');
  16114. Add(' class procedure {#TObject_DoIt}DoIt;');
  16115. Add(' end;');
  16116. Add(' TClass = class of TObject;');
  16117. Add(' TMobile = class');
  16118. Add(' class procedure {#TMobile_DoIt}DoIt;');
  16119. Add(' end;');
  16120. Add(' TMobileClass = class of TMobile;');
  16121. Add(' TCar = class(TMobile)');
  16122. Add(' class procedure {#TCar_DoIt}DoIt;');
  16123. Add(' end;');
  16124. Add(' TCarClass = class of TCar;');
  16125. Add('class procedure TObject.DoIt;');
  16126. Add('begin');
  16127. Add(' TClass(Self).{@TObject_DoIt}DoIt;');
  16128. Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
  16129. Add('end;');
  16130. Add('class procedure TMobile.DoIt;');
  16131. Add('begin');
  16132. Add(' TClass(Self).{@TObject_DoIt}DoIt;');
  16133. Add(' TMobileClass(Self).{@TMobile_DoIt}DoIt;');
  16134. Add(' TCarClass(Self).{@TCar_DoIt}DoIt;');
  16135. Add('end;');
  16136. Add('class procedure TCar.DoIt; begin end;');
  16137. Add('var');
  16138. Add(' ObjC: TClass;');
  16139. Add(' MobileC: TMobileClass;');
  16140. Add(' CarC: TCarClass;');
  16141. Add('begin');
  16142. Add(' ObjC.{@TObject_DoIt}DoIt;');
  16143. Add(' MobileC.{@TMobile_DoIt}DoIt;');
  16144. Add(' CarC.{@TCar_DoIt}DoIt;');
  16145. Add(' TClass(ObjC).{@TObject_DoIt}DoIt;');
  16146. Add(' TMobileClass(ObjC).{@TMobile_DoIt}DoIt;');
  16147. Add(' TCarClass(ObjC).{@TCar_DoIt}DoIt;');
  16148. Add(' TClass(MobileC).{@TObject_DoIt}DoIt;');
  16149. Add(' TMobileClass(MobileC).{@TMobile_DoIt}DoIt;');
  16150. Add(' TCarClass(MobileC).{@TCar_DoIt}DoIt;');
  16151. Add(' TClass(CarC).{@TObject_DoIt}DoIt;');
  16152. Add(' TMobileClass(CarC).{@TMobile_DoIt}DoIt;');
  16153. Add(' TCarClass(CarC).{@TCar_DoIt}DoIt;');
  16154. ConvertProgram;
  16155. CheckSource('TestClassOf_TypeCast',
  16156. LinesToStr([ // statements
  16157. 'rtl.createClass(this, "TObject", null, function () {',
  16158. ' this.$init = function () {',
  16159. ' };',
  16160. ' this.$final = function () {',
  16161. ' };',
  16162. ' this.DoIt = function () {',
  16163. ' this.DoIt();',
  16164. ' this.DoIt$1();',
  16165. ' };',
  16166. '});',
  16167. 'rtl.createClass(this, "TMobile", this.TObject, function () {',
  16168. ' this.DoIt$1 = function () {',
  16169. ' this.DoIt();',
  16170. ' this.DoIt$1();',
  16171. ' this.DoIt$2();',
  16172. ' };',
  16173. '});',
  16174. 'rtl.createClass(this, "TCar", this.TMobile, function () {',
  16175. ' this.DoIt$2 = function () {',
  16176. ' };',
  16177. '});',
  16178. 'this.ObjC = null;',
  16179. 'this.MobileC = null;',
  16180. 'this.CarC = null;',
  16181. '']),
  16182. LinesToStr([ // $mod.$main
  16183. '$mod.ObjC.DoIt();',
  16184. '$mod.MobileC.DoIt$1();',
  16185. '$mod.CarC.DoIt$2();',
  16186. '$mod.ObjC.DoIt();',
  16187. '$mod.ObjC.DoIt$1();',
  16188. '$mod.ObjC.DoIt$2();',
  16189. '$mod.MobileC.DoIt();',
  16190. '$mod.MobileC.DoIt$1();',
  16191. '$mod.MobileC.DoIt$2();',
  16192. '$mod.CarC.DoIt();',
  16193. '$mod.CarC.DoIt$1();',
  16194. '$mod.CarC.DoIt$2();',
  16195. '']));
  16196. end;
  16197. procedure TTestModule.TestClassOf_ImplicitFunctionCall;
  16198. begin
  16199. StartProgram(false);
  16200. Add('type');
  16201. Add(' TObject = class');
  16202. Add(' function CurNow: longint; ');
  16203. Add(' class function Now: longint; ');
  16204. Add(' end;');
  16205. Add('function TObject.CurNow: longint; begin end;');
  16206. Add('class function TObject.Now: longint; begin end;');
  16207. Add('var');
  16208. Add(' Obj: tobject;');
  16209. Add(' vI: longint;');
  16210. Add('begin');
  16211. Add(' obj.curnow;');
  16212. Add(' vi:=obj.curnow;');
  16213. Add(' tobject.now;');
  16214. Add(' vi:=tobject.now;');
  16215. ConvertProgram;
  16216. CheckSource('TestClassOf_ImplicitFunctionCall',
  16217. LinesToStr([ // statements
  16218. 'rtl.createClass(this, "TObject", null, function () {',
  16219. ' this.$init = function () {',
  16220. ' };',
  16221. ' this.$final = function () {',
  16222. ' };',
  16223. ' this.CurNow = function () {',
  16224. ' var Result = 0;',
  16225. ' return Result;',
  16226. ' };',
  16227. ' this.Now = function () {',
  16228. ' var Result = 0;',
  16229. ' return Result;',
  16230. ' };',
  16231. '});',
  16232. 'this.Obj = null;',
  16233. 'this.vI = 0;',
  16234. '']),
  16235. LinesToStr([ // $mod.$main
  16236. '$mod.Obj.CurNow();',
  16237. '$mod.vI = $mod.Obj.CurNow();',
  16238. '$mod.TObject.Now();',
  16239. '$mod.vI = $mod.TObject.Now();',
  16240. '']));
  16241. end;
  16242. procedure TTestModule.TestClassOf_Const;
  16243. begin
  16244. StartProgram(false);
  16245. Add([
  16246. 'type',
  16247. ' TObject = class',
  16248. ' end;',
  16249. ' TBird = TObject;',
  16250. ' TBirds = class of TBird;',
  16251. ' TEagles = TBirds;',
  16252. ' THawk = class(TBird);',
  16253. 'const',
  16254. ' Hawk: TEagles = THawk;',
  16255. ' DefaultBirdClasses : Array [1..2] of TEagles = (',
  16256. ' TBird,',
  16257. ' THawk',
  16258. ' );',
  16259. 'begin']);
  16260. ConvertProgram;
  16261. CheckSource('TestClassOf_Const',
  16262. LinesToStr([ // statements
  16263. 'rtl.createClass(this, "TObject", null, function () {',
  16264. ' this.$init = function () {',
  16265. ' };',
  16266. ' this.$final = function () {',
  16267. ' };',
  16268. '});',
  16269. 'rtl.createClass(this, "THawk", this.TObject, function () {',
  16270. '});',
  16271. 'this.Hawk = this.THawk;',
  16272. 'this.DefaultBirdClasses = [this.TObject, this.THawk];',
  16273. '']),
  16274. LinesToStr([ // $mod.$main
  16275. '']));
  16276. end;
  16277. procedure TTestModule.TestNestedClass_Alias;
  16278. begin
  16279. WithTypeInfo:=true;
  16280. StartProgram(false);
  16281. Add([
  16282. 'type',
  16283. ' TObject = class',
  16284. ' type TNested = type longint;',
  16285. ' end;',
  16286. 'type TAlias = type tobject.tnested;',
  16287. 'var i: tobject.tnested = 3;',
  16288. 'var j: TAlias = 4;',
  16289. 'begin',
  16290. ' if typeinfo(TAlias)=nil then ;',
  16291. ' if typeinfo(tobject.tnested)=nil then ;',
  16292. '']);
  16293. ConvertProgram;
  16294. CheckSource('TestNestedClass_Alias',
  16295. LinesToStr([ // statements
  16296. 'rtl.createClass(this, "TObject", null, function () {',
  16297. ' $mod.$rtti.$inherited("TObject.TNested", rtl.longint, {});',
  16298. ' this.$init = function () {',
  16299. ' };',
  16300. ' this.$final = function () {',
  16301. ' };',
  16302. '});',
  16303. 'this.$rtti.$inherited("TAlias", this.$rtti["TObject.TNested"], {});',
  16304. 'this.i = 3;',
  16305. 'this.j = 4;',
  16306. '']),
  16307. LinesToStr([ // $mod.$main
  16308. 'if ($mod.$rtti["TAlias"] === null) ;',
  16309. 'if ($mod.$rtti["TObject.TNested"] === null) ;',
  16310. '']));
  16311. end;
  16312. procedure TTestModule.TestNestedClass_Record;
  16313. begin
  16314. WithTypeInfo:=true;
  16315. StartProgram(false);
  16316. Add([
  16317. 'type',
  16318. ' TObject = class',
  16319. ' type TPoint = record',
  16320. ' x,y: byte;',
  16321. ' end;',
  16322. ' procedure DoIt(t: TPoint);',
  16323. ' end;',
  16324. 'procedure tobject.DoIt(t: TPoint);',
  16325. 'var p: TPoint;',
  16326. 'begin',
  16327. ' t.x:=t.y;',
  16328. ' p:=t;',
  16329. 'end;',
  16330. 'var',
  16331. ' p: tobject.tpoint = (x:2; y:4);',
  16332. ' o: TObject;',
  16333. 'begin',
  16334. ' p:=p;',
  16335. ' o.doit(p);',
  16336. '']);
  16337. ConvertProgram;
  16338. CheckSource('TestNestedClass_Record',
  16339. LinesToStr([ // statements
  16340. 'rtl.createClass(this, "TObject", null, function () {',
  16341. ' rtl.recNewT(this, "TPoint", function () {',
  16342. ' this.x = 0;',
  16343. ' this.y = 0;',
  16344. ' this.$eq = function (b) {',
  16345. ' return (this.x === b.x) && (this.y === b.y);',
  16346. ' };',
  16347. ' this.$assign = function (s) {',
  16348. ' this.x = s.x;',
  16349. ' this.y = s.y;',
  16350. ' return this;',
  16351. ' };',
  16352. ' var $r = $mod.$rtti.$Record("TObject.TPoint", {});',
  16353. ' $r.addField("x", rtl.byte);',
  16354. ' $r.addField("y", rtl.byte);',
  16355. ' });',
  16356. ' this.$init = function () {',
  16357. ' };',
  16358. ' this.$final = function () {',
  16359. ' };',
  16360. ' this.DoIt = function (t) {',
  16361. ' var p = this.TPoint.$new();',
  16362. ' t.x = t.y;',
  16363. ' p.$assign(t);',
  16364. ' };',
  16365. '});',
  16366. 'this.p = this.TObject.TPoint.$clone({',
  16367. ' x: 2,',
  16368. ' y: 4',
  16369. '});',
  16370. 'this.o = null;',
  16371. '']),
  16372. LinesToStr([ // $mod.$main
  16373. '$mod.p.$assign($mod.p);',
  16374. '$mod.o.DoIt($mod.TObject.TPoint.$clone($mod.p));',
  16375. '']));
  16376. end;
  16377. procedure TTestModule.TestNestedClass_Class;
  16378. begin
  16379. WithTypeInfo:=true;
  16380. StartProgram(false);
  16381. Add([
  16382. 'type',
  16383. ' TObject = class end;',
  16384. ' TBird = class',
  16385. ' type TLeg = class',
  16386. ' FId: longint;',
  16387. ' constructor Create;',
  16388. ' function Create(i: longint): TLeg;',
  16389. ' end;',
  16390. ' function DoIt(b: TBird): Tleg;',
  16391. ' end;',
  16392. 'constructor tbird.tleg.create;',
  16393. 'begin',
  16394. ' FId:=3;',
  16395. 'end;',
  16396. 'function tbird.tleg.Create(i: longint): TLeg;',
  16397. 'begin',
  16398. ' Create;',
  16399. ' Result:=TLeg.Create;',
  16400. ' Result:=TBird.TLeg.Create;',
  16401. ' Result:=Create(3);',
  16402. ' FId:=i;',
  16403. 'end;',
  16404. 'function tbird.DoIt(b: tbird): tleg;',
  16405. 'begin',
  16406. ' Result.Create;',
  16407. ' Result:=TLeg.Create;',
  16408. ' Result:=TBird.TLeg.Create;',
  16409. ' Result:=Result.Create(3);',
  16410. 'end;',
  16411. 'var',
  16412. ' b: Tbird.tleg;',
  16413. 'begin',
  16414. ' b.Create;',
  16415. ' b:=TBird.TLeg.Create;',
  16416. ' b:=b.Create(3);',
  16417. '']);
  16418. ConvertProgram;
  16419. CheckSource('TestNestedClass_Class',
  16420. LinesToStr([ // statements
  16421. 'rtl.createClass(this, "TObject", null, function () {',
  16422. ' this.$init = function () {',
  16423. ' };',
  16424. ' this.$final = function () {',
  16425. ' };',
  16426. '});',
  16427. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  16428. ' rtl.createClass(this, "TLeg", $mod.TObject, function () {',
  16429. ' this.$init = function () {',
  16430. ' $mod.TObject.$init.call(this);',
  16431. ' this.FId = 0;',
  16432. ' };',
  16433. ' this.Create = function () {',
  16434. ' this.FId = 3;',
  16435. ' return this;',
  16436. ' };',
  16437. ' this.Create$1 = function (i) {',
  16438. ' var Result = null;',
  16439. ' this.Create();',
  16440. ' Result = $mod.TBird.TLeg.$create("Create");',
  16441. ' Result = $mod.TBird.TLeg.$create("Create");',
  16442. ' Result = this.Create$1(3);',
  16443. ' this.FId = i;',
  16444. ' return Result;',
  16445. ' };',
  16446. ' }, "TBird.TLeg");',
  16447. ' this.DoIt = function (b) {',
  16448. ' var Result = null;',
  16449. ' Result.Create();',
  16450. ' Result = this.TLeg.$create("Create");',
  16451. ' Result = $mod.TBird.TLeg.$create("Create");',
  16452. ' Result = Result.Create$1(3);',
  16453. ' return Result;',
  16454. ' };',
  16455. '});',
  16456. 'this.b = null;',
  16457. '']),
  16458. LinesToStr([ // $mod.$main
  16459. '$mod.b.Create();',
  16460. '$mod.b = $mod.TBird.TLeg.$create("Create");',
  16461. '$mod.b = $mod.b.Create$1(3);',
  16462. '']));
  16463. end;
  16464. procedure TTestModule.TestExternalClass_Var;
  16465. begin
  16466. StartProgram(false);
  16467. Add([
  16468. '{$modeswitch externalclass}',
  16469. 'type',
  16470. ' TExtA = class external name ''ExtObj''',
  16471. ' Id: longint external name ''$Id'';',
  16472. ' B: longint;',
  16473. ' end;',
  16474. 'var Obj: TExtA;',
  16475. 'begin',
  16476. ' obj.id:=obj.id+1;',
  16477. ' obj.B:=obj.B+1;']);
  16478. ConvertProgram;
  16479. CheckSource('TestExternalClass_Var',
  16480. LinesToStr([ // statements
  16481. 'this.Obj = null;',
  16482. '']),
  16483. LinesToStr([ // $mod.$main
  16484. '$mod.Obj.$Id = $mod.Obj.$Id + 1;',
  16485. '$mod.Obj.B = $mod.Obj.B + 1;',
  16486. '']));
  16487. end;
  16488. procedure TTestModule.TestExternalClass_Const;
  16489. begin
  16490. StartProgram(false);
  16491. Add([
  16492. '{$modeswitch externalclass}',
  16493. 'type',
  16494. ' TExtA = class external name ''ExtObj''',
  16495. ' const Two: longint = 2;',
  16496. ' const Three = 3;',
  16497. ' const Id: longint;',
  16498. ' end;',
  16499. ' TExtB = class external name ''ExtB''',
  16500. ' A: TExtA;',
  16501. ' end;',
  16502. 'var',
  16503. ' A: texta;',
  16504. ' B: textb;',
  16505. ' i: longint;',
  16506. 'begin',
  16507. ' i:=a.two;',
  16508. ' i:=texta.two;',
  16509. ' i:=a.three;',
  16510. ' i:=texta.three;',
  16511. ' i:=a.id;',
  16512. ' i:=texta.id;',
  16513. '']);
  16514. ConvertProgram;
  16515. CheckSource('TestExternalClass_Const',
  16516. LinesToStr([ // statements
  16517. 'this.A = null;',
  16518. 'this.B = null;',
  16519. 'this.i = 0;',
  16520. '']),
  16521. LinesToStr([ // $mod.$main
  16522. '$mod.i = 2;',
  16523. '$mod.i = 2;',
  16524. '$mod.i = 3;',
  16525. '$mod.i = 3;',
  16526. '$mod.i = $mod.A.Id;',
  16527. '$mod.i = ExtObj.Id;',
  16528. '']));
  16529. end;
  16530. procedure TTestModule.TestExternalClass_Dollar;
  16531. begin
  16532. StartProgram(false);
  16533. Add([
  16534. '{$modeswitch externalclass}',
  16535. 'type',
  16536. ' TExtA = class external name ''$''',
  16537. ' Id: longint external name ''$'';',
  16538. ' function Bla(i: longint): longint; external name ''$'';',
  16539. ' end;',
  16540. 'function dollar(k: longint): longint; external name ''$'';',
  16541. 'var Obj: TExtA;',
  16542. 'begin',
  16543. ' dollar(1);',
  16544. ' obj.id:=obj.id+2;',
  16545. ' obj.Bla(3);',
  16546. '']);
  16547. ConvertProgram;
  16548. CheckSource('TestExternalClass_Dollar',
  16549. LinesToStr([ // statements
  16550. 'this.Obj = null;',
  16551. '']),
  16552. LinesToStr([ // $mod.$main
  16553. '$(1);',
  16554. '$mod.Obj.$ = $mod.Obj.$ + 2;',
  16555. '$mod.Obj.$(3);',
  16556. '']));
  16557. end;
  16558. procedure TTestModule.TestExternalClass_DuplicateVarFail;
  16559. begin
  16560. StartProgram(false);
  16561. Add('{$modeswitch externalclass}');
  16562. Add('type');
  16563. Add(' TExtA = class external name ''ExtA''');
  16564. Add(' Id: longint external name ''$Id'';');
  16565. Add(' end;');
  16566. Add(' TExtB = class external ''lib'' name ''ExtB''(TExtA)');
  16567. Add(' Id: longint;');
  16568. Add(' end;');
  16569. Add('begin');
  16570. SetExpectedPasResolverError('Duplicate identifier "Id" at test1.pp(6,5)',nDuplicateIdentifier);
  16571. ConvertProgram;
  16572. end;
  16573. procedure TTestModule.TestExternalClass_Method;
  16574. begin
  16575. StartProgram(false);
  16576. Add(['{$modeswitch externalclass}',
  16577. 'type',
  16578. ' TExtA = class external name ''ExtObj''',
  16579. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  16580. ' procedure DoSome(Id: longint = 1);',
  16581. ' end;',
  16582. 'var Obj: texta;',
  16583. 'begin',
  16584. ' obj.doit;',
  16585. ' obj.doit();',
  16586. ' obj.doit(2);',
  16587. ' with obj do begin',
  16588. ' doit;',
  16589. ' doit();',
  16590. ' doit(3);',
  16591. ' end;']);
  16592. ConvertProgram;
  16593. CheckSource('TestExternalClass_Method',
  16594. LinesToStr([ // statements
  16595. 'this.Obj = null;',
  16596. '']),
  16597. LinesToStr([ // $mod.$main
  16598. '$mod.Obj.$Execute(1);',
  16599. '$mod.Obj.$Execute(1);',
  16600. '$mod.Obj.$Execute(2);',
  16601. 'var $with = $mod.Obj;',
  16602. '$with.$Execute(1);',
  16603. '$with.$Execute(1);',
  16604. '$with.$Execute(3);',
  16605. '']));
  16606. end;
  16607. procedure TTestModule.TestExternalClass_ClassMethod;
  16608. begin
  16609. StartProgram(false);
  16610. Add([
  16611. '{$modeswitch externalclass}',
  16612. 'type',
  16613. ' TExtA = class external name ''ExtObj''',
  16614. ' class procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  16615. ' end;',
  16616. ' TExtB = TExtA;',
  16617. 'var p: Pointer;',
  16618. 'begin',
  16619. ' texta.doit;',
  16620. ' texta.doit();',
  16621. ' texta.doit(2);',
  16622. ' p:[email protected];',
  16623. ' with texta do begin',
  16624. ' doit;',
  16625. ' doit();',
  16626. ' doit(3);',
  16627. ' p:=@DoIt;',
  16628. ' end;',
  16629. ' textb.doit;',
  16630. ' textb.doit();',
  16631. ' textb.doit(4);',
  16632. ' with textb do begin',
  16633. ' doit;',
  16634. ' doit();',
  16635. ' doit(5);',
  16636. ' end;',
  16637. '']);
  16638. ConvertProgram;
  16639. CheckSource('TestExternalClass_ClassMethod',
  16640. LinesToStr([ // statements
  16641. 'this.p = null;',
  16642. '']),
  16643. LinesToStr([ // $mod.$main
  16644. 'ExtObj.$Execute(1);',
  16645. 'ExtObj.$Execute(1);',
  16646. 'ExtObj.$Execute(2);',
  16647. '$mod.p = rtl.createCallback(ExtObj, "$Execute");',
  16648. 'ExtObj.$Execute(1);',
  16649. 'ExtObj.$Execute(1);',
  16650. 'ExtObj.$Execute(3);',
  16651. '$mod.p = rtl.createCallback(ExtObj, "$Execute");',
  16652. 'ExtObj.$Execute(1);',
  16653. 'ExtObj.$Execute(1);',
  16654. 'ExtObj.$Execute(4);',
  16655. 'ExtObj.$Execute(1);',
  16656. 'ExtObj.$Execute(1);',
  16657. 'ExtObj.$Execute(5);',
  16658. '']));
  16659. end;
  16660. procedure TTestModule.TestExternalClass_ClassMethodStatic;
  16661. begin
  16662. StartProgram(false);
  16663. Add([
  16664. '{$modeswitch externalclass}',
  16665. 'type',
  16666. ' TExtA = class external name ''ExtObj''',
  16667. ' class procedure DoIt(Id: longint = 1); static;',
  16668. ' end;',
  16669. 'var p: Pointer;',
  16670. 'begin',
  16671. ' texta.doit;',
  16672. ' texta.doit();',
  16673. ' texta.doit(2);',
  16674. ' p:[email protected];',
  16675. ' with texta do begin',
  16676. ' doit;',
  16677. ' doit();',
  16678. ' doit(3);',
  16679. ' p:=@DoIt;',
  16680. ' end;',
  16681. '']);
  16682. ConvertProgram;
  16683. CheckSource('TestExternalClass_ClassMethodStatic',
  16684. LinesToStr([ // statements
  16685. 'this.p = null;',
  16686. '']),
  16687. LinesToStr([ // $mod.$main
  16688. 'ExtObj.DoIt(1);',
  16689. 'ExtObj.DoIt(1);',
  16690. 'ExtObj.DoIt(2);',
  16691. '$mod.p = ExtObj.DoIt;',
  16692. 'ExtObj.DoIt(1);',
  16693. 'ExtObj.DoIt(1);',
  16694. 'ExtObj.DoIt(3);',
  16695. '$mod.p = ExtObj.DoIt;',
  16696. '']));
  16697. end;
  16698. procedure TTestModule.TestExternalClass_FunctionResultInTypeCast;
  16699. begin
  16700. StartProgram(false);
  16701. Add([
  16702. '{$modeswitch externalclass}',
  16703. 'type',
  16704. ' TBird = class external name ''Array''',
  16705. ' end;',
  16706. 'function GetPtr: Pointer;',
  16707. 'begin',
  16708. 'end;',
  16709. 'procedure Write(const p);',
  16710. 'begin',
  16711. 'end;',
  16712. 'procedure WriteLn; varargs;',
  16713. 'begin',
  16714. 'end;',
  16715. 'begin',
  16716. ' if TBird(GetPtr)=nil then ;',
  16717. ' Write(GetPtr);',
  16718. ' WriteLn(GetPtr);',
  16719. ' Write(TBird(GetPtr));',
  16720. ' WriteLn(TBird(GetPtr));',
  16721. '']);
  16722. ConvertProgram;
  16723. CheckSource('TestFunctionResultInTypeCast',
  16724. LinesToStr([ // statements
  16725. 'this.GetPtr = function () {',
  16726. ' var Result = null;',
  16727. ' return Result;',
  16728. '};',
  16729. 'this.Write = function (p) {',
  16730. '};',
  16731. 'this.WriteLn = function () {',
  16732. '};',
  16733. '']),
  16734. LinesToStr([
  16735. 'if ($mod.GetPtr() === null) ;',
  16736. '$mod.Write($mod.GetPtr());',
  16737. '$mod.WriteLn($mod.GetPtr());',
  16738. '$mod.Write($mod.GetPtr());',
  16739. '$mod.WriteLn($mod.GetPtr());',
  16740. '']));
  16741. end;
  16742. procedure TTestModule.TestExternalClass_NonExternalOverride;
  16743. begin
  16744. StartProgram(false);
  16745. Add([
  16746. '{$modeswitch externalclass}',
  16747. 'type',
  16748. ' TExtA = class external name ''ExtObjA''',
  16749. ' procedure ProcA; virtual;',
  16750. ' procedure ProcB; virtual;',
  16751. ' end;',
  16752. ' TExtB = class external name ''ExtObjB'' (TExtA)',
  16753. ' end;',
  16754. ' TExtC = class (TExtB)',
  16755. ' procedure ProcA; override;',
  16756. ' end;',
  16757. 'procedure TExtC.ProcA;',
  16758. 'begin',
  16759. ' ProcA;',
  16760. ' Self.ProcA;',
  16761. ' ProcB;',
  16762. ' Self.ProcB;',
  16763. 'end;',
  16764. 'var',
  16765. ' A: texta;',
  16766. ' B: textb;',
  16767. ' C: textc;',
  16768. 'begin',
  16769. ' a.proca;',
  16770. ' b.proca;',
  16771. ' c.proca;']);
  16772. ConvertProgram;
  16773. CheckSource('TestExternalClass_NonExternalOverride',
  16774. LinesToStr([ // statements
  16775. 'rtl.createClassExt(this, "TExtC", ExtObjB, "", function () {',
  16776. ' this.$init = function () {',
  16777. ' };',
  16778. ' this.$final = function () {',
  16779. ' };',
  16780. ' this.ProcA = function () {',
  16781. ' this.ProcA();',
  16782. ' this.ProcA();',
  16783. ' this.ProcB();',
  16784. ' this.ProcB();',
  16785. ' };',
  16786. '});',
  16787. 'this.A = null;',
  16788. 'this.B = null;',
  16789. 'this.C = null;',
  16790. '']),
  16791. LinesToStr([ // $mod.$main
  16792. '$mod.A.ProcA();',
  16793. '$mod.B.ProcA();',
  16794. '$mod.C.ProcA();',
  16795. '']));
  16796. end;
  16797. procedure TTestModule.TestExternalClass_OverloadHint;
  16798. begin
  16799. StartProgram(false);
  16800. Add([
  16801. '{$modeswitch externalclass}',
  16802. 'type',
  16803. ' TExtA = class external name ''ExtObjA''',
  16804. ' procedure DoIt;',
  16805. ' procedure DoIt(i: longint);',
  16806. ' end;',
  16807. 'begin',
  16808. '']);
  16809. ConvertProgram;
  16810. CheckResolverUnexpectedHints(true);
  16811. CheckSource('TestExternalClass_OverloadHint',
  16812. LinesToStr([ // statements
  16813. '']),
  16814. LinesToStr([ // $mod.$main
  16815. '']));
  16816. end;
  16817. procedure TTestModule.TestExternalClass_SameNamePublishedProperty;
  16818. begin
  16819. WithTypeInfo:=true;
  16820. StartProgram(false);
  16821. Add([
  16822. '{$modeswitch externalclass}',
  16823. 'type',
  16824. ' JSwiper = class external name ''Swiper''',
  16825. ' constructor New;',
  16826. ' end;',
  16827. ' TObject = class',
  16828. ' private',
  16829. ' FSwiper: JSwiper;',
  16830. ' published',
  16831. ' property Swiper: JSwiper read FSwiper write FSwiper;',
  16832. ' end;',
  16833. 'begin',
  16834. ' JSwiper.new;',
  16835. '']);
  16836. ConvertProgram;
  16837. CheckSource('TestExternalClass_SameNamePublishedProperty',
  16838. LinesToStr([ // statements
  16839. 'this.$rtti.$ExtClass("JSwiper", {',
  16840. ' jsclass: "Swiper"',
  16841. '});',
  16842. 'rtl.createClass(this, "TObject", null, function () {',
  16843. ' this.$init = function () {',
  16844. ' this.FSwiper = null;',
  16845. ' };',
  16846. ' this.$final = function () {',
  16847. ' this.FSwiper = undefined;',
  16848. ' };',
  16849. ' var $r = this.$rtti;',
  16850. ' $r.addProperty("Swiper", 0, $mod.$rtti["JSwiper"], "FSwiper", "FSwiper");',
  16851. '});',
  16852. '']),
  16853. LinesToStr([ // $mod.$main
  16854. 'new Swiper();',
  16855. '']));
  16856. end;
  16857. procedure TTestModule.TestExternalClass_Property;
  16858. begin
  16859. StartProgram(false);
  16860. Add([
  16861. '{$modeswitch externalclass}',
  16862. 'type',
  16863. ' TExtA = class external name ''ExtA''',
  16864. ' function getYear: longint;',
  16865. ' procedure setYear(Value: longint);',
  16866. ' property Year: longint read getyear write setyear;',
  16867. ' end;',
  16868. ' TExtB = class (TExtA)',
  16869. ' procedure OtherSetYear(Value: longint);',
  16870. ' property year write othersetyear;',
  16871. ' end;',
  16872. 'procedure textb.othersetyear(value: longint);',
  16873. 'begin',
  16874. ' setYear(Value+4);',
  16875. 'end;',
  16876. 'var',
  16877. ' A: texta;',
  16878. ' B: textb;',
  16879. 'begin',
  16880. ' a.year:=a.year+1;',
  16881. ' b.year:=b.year+2;']);
  16882. ConvertProgram;
  16883. CheckSource('TestExternalClass_NonExternalOverride',
  16884. LinesToStr([ // statements
  16885. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  16886. ' this.$init = function () {',
  16887. ' };',
  16888. ' this.$final = function () {',
  16889. ' };',
  16890. ' this.OtherSetYear = function (Value) {',
  16891. ' this.setYear(Value+4);',
  16892. ' };',
  16893. '});',
  16894. 'this.A = null;',
  16895. 'this.B = null;',
  16896. '']),
  16897. LinesToStr([ // $mod.$main
  16898. '$mod.A.setYear($mod.A.getYear()+1);',
  16899. '$mod.B.OtherSetYear($mod.B.getYear()+2);',
  16900. '']));
  16901. end;
  16902. procedure TTestModule.TestExternalClass_PropertyDate;
  16903. begin
  16904. StartProgram(false);
  16905. Add([
  16906. '{$modeswitch externalclass}',
  16907. 'type',
  16908. ' TExtA = class external name ''ExtA''',
  16909. ' end;',
  16910. ' TExtB = class (TExtA)',
  16911. ' FDate: string;',
  16912. ' property Date: string read FDate write FDate;',
  16913. ' property ExtA: string read FDate write FDate;',
  16914. ' end;',
  16915. ' {$M+}',
  16916. ' TObject = class',
  16917. ' FDate: string;',
  16918. ' published',
  16919. ' property Date: string read FDate write FDate;',
  16920. ' property ExtA: string read FDate write FDate;',
  16921. ' end;',
  16922. 'var',
  16923. ' B: textb;',
  16924. ' o: TObject;',
  16925. 'begin',
  16926. ' b.date:=b.exta;',
  16927. ' o.date:=o.exta;']);
  16928. ConvertProgram;
  16929. CheckSource('TestExternalClass_PropertyDate',
  16930. LinesToStr([ // statements
  16931. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  16932. ' this.$init = function () {',
  16933. ' this.FDate = "";',
  16934. ' };',
  16935. ' this.$final = function () {',
  16936. ' };',
  16937. '});',
  16938. 'rtl.createClass(this, "TObject", null, function () {',
  16939. ' this.$init = function () {',
  16940. ' this.FDate = "";',
  16941. ' };',
  16942. ' this.$final = function () {',
  16943. ' };',
  16944. ' var $r = this.$rtti;',
  16945. ' $r.addField("FDate", rtl.string);',
  16946. ' $r.addProperty("Date", 0, rtl.string, "FDate", "FDate");',
  16947. ' $r.addProperty("ExtA", 0, rtl.string, "FDate", "FDate");',
  16948. '});',
  16949. 'this.B = null;',
  16950. 'this.o = null;',
  16951. '']),
  16952. LinesToStr([ // $mod.$main
  16953. '$mod.B.FDate = $mod.B.FDate;',
  16954. '$mod.o.FDate = $mod.o.FDate;',
  16955. '']));
  16956. end;
  16957. procedure TTestModule.TestExternalClass_ClassProperty;
  16958. begin
  16959. StartProgram(false);
  16960. Add('{$modeswitch externalclass}');
  16961. Add('type');
  16962. Add(' TExtA = class external name ''ExtA''');
  16963. Add(' class function getYear: longint;');
  16964. Add(' class procedure setYear(Value: longint);');
  16965. Add(' class property Year: longint read getyear write setyear;');
  16966. Add(' end;');
  16967. Add(' TExtB = class (TExtA)');
  16968. Add(' class function GetCentury: longint;');
  16969. Add(' class procedure SetCentury(Value: longint);');
  16970. Add(' class property Century: longint read getcentury write setcentury;');
  16971. Add(' end;');
  16972. Add('class function textb.getcentury: longint;');
  16973. Add('begin');
  16974. Add('end;');
  16975. Add('class procedure textb.setcentury(value: longint);');
  16976. Add('begin');
  16977. Add(' setyear(value+11);');
  16978. Add(' texta.year:=texta.year+12;');
  16979. Add(' year:=year+13;');
  16980. Add(' textb.century:=textb.century+14;');
  16981. Add(' century:=century+15;');
  16982. Add('end;');
  16983. Add('var');
  16984. Add(' A: texta;');
  16985. Add(' B: textb;');
  16986. Add('begin');
  16987. Add(' texta.year:=texta.year+1;');
  16988. Add(' textb.year:=textb.year+2;');
  16989. Add(' TextA.year:=TextA.year+3;');
  16990. Add(' b.year:=b.year+4;');
  16991. Add(' textb.century:=textb.century+5;');
  16992. Add(' b.century:=b.century+6;');
  16993. ConvertProgram;
  16994. CheckSource('TestExternalClass_ClassProperty',
  16995. LinesToStr([ // statements
  16996. 'rtl.createClassExt(this, "TExtB", ExtA, "", function () {',
  16997. ' this.$init = function () {',
  16998. ' };',
  16999. ' this.$final = function () {',
  17000. ' };',
  17001. ' this.GetCentury = function () {',
  17002. ' var Result = 0;',
  17003. ' return Result;',
  17004. ' };',
  17005. ' this.SetCentury = function (Value) {',
  17006. ' this.setYear(Value + 11);',
  17007. ' ExtA.setYear(ExtA.getYear() + 12);',
  17008. ' this.setYear(this.getYear() + 13);',
  17009. ' $mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 14);',
  17010. ' this.SetCentury(this.GetCentury() + 15);',
  17011. ' };',
  17012. '});',
  17013. 'this.A = null;',
  17014. 'this.B = null;',
  17015. '']),
  17016. LinesToStr([ // $mod.$main
  17017. 'ExtA.setYear(ExtA.getYear() + 1);',
  17018. '$mod.TExtB.setYear($mod.TExtB.getYear() + 2);',
  17019. 'ExtA.setYear(ExtA.getYear() + 3);',
  17020. '$mod.B.setYear($mod.B.getYear() + 4);',
  17021. '$mod.TExtB.SetCentury($mod.TExtB.GetCentury() + 5);',
  17022. '$mod.B.$class.SetCentury($mod.B.$class.GetCentury() + 6);',
  17023. '']));
  17024. end;
  17025. procedure TTestModule.TestExternalClass_ClassOf;
  17026. begin
  17027. StartProgram(false);
  17028. Add('{$modeswitch externalclass}');
  17029. Add('type');
  17030. Add(' TExtA = class external name ''ExtA''');
  17031. Add(' procedure ProcA; virtual;');
  17032. Add(' procedure ProcB; virtual;');
  17033. Add(' end;');
  17034. Add(' TExtAClass = class of TExtA;');
  17035. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  17036. Add(' end;');
  17037. Add(' TExtBClass = class of TExtB;');
  17038. Add(' TExtC = class (TExtB)');
  17039. Add(' procedure ProcA; override;');
  17040. Add(' end;');
  17041. Add(' TExtCClass = class of TExtC;');
  17042. Add('procedure TExtC.ProcA; begin end;');
  17043. Add('var');
  17044. Add(' A: texta; ClA: TExtAClass;');
  17045. Add(' B: textb; ClB: TExtBClass;');
  17046. Add(' C: textc; ClC: TExtCClass;');
  17047. Add('begin');
  17048. Add(' ClA:=texta;');
  17049. Add(' ClA:=textb;');
  17050. Add(' ClA:=textc;');
  17051. Add(' ClB:=textb;');
  17052. Add(' ClB:=textc;');
  17053. Add(' ClC:=textc;');
  17054. ConvertProgram;
  17055. CheckSource('TestExternalClass_ClassOf',
  17056. LinesToStr([ // statements
  17057. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  17058. ' this.$init = function () {',
  17059. ' };',
  17060. ' this.$final = function () {',
  17061. ' };',
  17062. ' this.ProcA = function () {',
  17063. ' };',
  17064. '});',
  17065. 'this.A = null;',
  17066. 'this.ClA = null;',
  17067. 'this.B = null;',
  17068. 'this.ClB = null;',
  17069. 'this.C = null;',
  17070. 'this.ClC = null;',
  17071. '']),
  17072. LinesToStr([ // $mod.$main
  17073. '$mod.ClA = ExtA;',
  17074. '$mod.ClA = ExtB;',
  17075. '$mod.ClA = $mod.TExtC;',
  17076. '$mod.ClB = ExtB;',
  17077. '$mod.ClB = $mod.TExtC;',
  17078. '$mod.ClC = $mod.TExtC;',
  17079. '']));
  17080. end;
  17081. procedure TTestModule.TestExternalClass_ClassOtherUnit;
  17082. begin
  17083. AddModuleWithIntfImplSrc('unit2.pas',
  17084. LinesToStr([
  17085. '{$modeswitch externalclass}',
  17086. 'type',
  17087. ' TExtA = class external name ''ExtA''',
  17088. ' class var Id: longint;',
  17089. ' end;',
  17090. '']),
  17091. '');
  17092. StartUnit(true);
  17093. Add('interface');
  17094. Add('uses unit2;');
  17095. Add('implementation');
  17096. Add('begin');
  17097. Add(' unit2.texta.id:=unit2.texta.id+1;');
  17098. ConvertUnit;
  17099. CheckSource('TestExternalClass_ClassOtherUnit',
  17100. LinesToStr([
  17101. '']),
  17102. LinesToStr([
  17103. 'ExtA.Id = ExtA.Id + 1;',
  17104. '']));
  17105. end;
  17106. procedure TTestModule.TestExternalClass_Is;
  17107. begin
  17108. StartProgram(false);
  17109. Add([
  17110. '{$modeswitch externalclass}',
  17111. 'type',
  17112. ' TExtA = class external name ''ExtA''',
  17113. ' end;',
  17114. ' TExtAClass = class of TExtA;',
  17115. ' TExtB = class external name ''ExtB'' (TExtA)',
  17116. ' end;',
  17117. ' TExtBClass = class of TExtB;',
  17118. ' TExtC = class (TExtB)',
  17119. ' end;',
  17120. ' TExtCClass = class of TExtC;',
  17121. 'var',
  17122. ' A: texta; ClA: TExtAClass;',
  17123. ' B: textb; ClB: TExtBClass;',
  17124. ' C: textc; ClC: TExtCClass;',
  17125. 'begin',
  17126. ' if a is textb then ;',
  17127. ' if a is textc then ;',
  17128. ' if b is textc then ;',
  17129. ' if cla is textb then ;',
  17130. ' if cla is textc then ;',
  17131. ' if clb is textc then ;',
  17132. ' try',
  17133. ' except',
  17134. ' on TExtA do ;',
  17135. ' on e: TExtB do ;',
  17136. ' end;',
  17137. '']);
  17138. ConvertProgram;
  17139. CheckSource('TestExternalClass_Is',
  17140. LinesToStr([ // statements
  17141. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  17142. ' this.$init = function () {',
  17143. ' };',
  17144. ' this.$final = function () {',
  17145. ' };',
  17146. '});',
  17147. 'this.A = null;',
  17148. 'this.ClA = null;',
  17149. 'this.B = null;',
  17150. 'this.ClB = null;',
  17151. 'this.C = null;',
  17152. 'this.ClC = null;',
  17153. '']),
  17154. LinesToStr([ // $mod.$main
  17155. 'if (rtl.isExt($mod.A, ExtB)) ;',
  17156. 'if ($mod.TExtC.isPrototypeOf($mod.A)) ;',
  17157. 'if ($mod.TExtC.isPrototypeOf($mod.B)) ;',
  17158. 'if (rtl.isExt($mod.ClA, ExtB)) ;',
  17159. 'if (rtl.is($mod.ClA, $mod.TExtC)) ;',
  17160. 'if (rtl.is($mod.ClB, $mod.TExtC)) ;',
  17161. 'try {} catch ($e) {',
  17162. ' if (rtl.isExt($e,ExtA)) {}',
  17163. ' else if (rtl.isExt($e,ExtB)) {',
  17164. ' var e = $e;',
  17165. ' } else throw $e',
  17166. '};',
  17167. '']));
  17168. end;
  17169. procedure TTestModule.TestExternalClass_As;
  17170. begin
  17171. StartProgram(false);
  17172. Add('{$modeswitch externalclass}');
  17173. Add('type');
  17174. Add(' TExtA = class external name ''ExtA''');
  17175. Add(' end;');
  17176. Add(' TExtB = class external name ''ExtB'' (TExtA)');
  17177. Add(' end;');
  17178. Add(' TExtC = class (TExtB)');
  17179. Add(' end;');
  17180. Add('var');
  17181. Add(' A: texta;');
  17182. Add(' B: textb;');
  17183. Add(' C: textc;');
  17184. Add('begin');
  17185. Add(' b:=a as textb;');
  17186. Add(' c:=a as textc;');
  17187. Add(' c:=b as textc;');
  17188. ConvertProgram;
  17189. CheckSource('TestExternalClass_Is',
  17190. LinesToStr([ // statements
  17191. 'rtl.createClassExt(this, "TExtC", ExtB, "", function () {',
  17192. ' this.$init = function () {',
  17193. ' };',
  17194. ' this.$final = function () {',
  17195. ' };',
  17196. '});',
  17197. 'this.A = null;',
  17198. 'this.B = null;',
  17199. 'this.C = null;',
  17200. '']),
  17201. LinesToStr([ // $mod.$main
  17202. '$mod.B = rtl.asExt($mod.A, ExtB);',
  17203. '$mod.C = rtl.as($mod.A, $mod.TExtC);',
  17204. '$mod.C = rtl.as($mod.B, $mod.TExtC);',
  17205. '']));
  17206. end;
  17207. procedure TTestModule.TestExternalClass_DestructorFail;
  17208. begin
  17209. StartProgram(false);
  17210. Add('{$modeswitch externalclass}');
  17211. Add('type');
  17212. Add(' TExtA = class external name ''ExtA''');
  17213. Add(' destructor Free;');
  17214. Add(' end;');
  17215. SetExpectedPasResolverError('Pascal element not supported: destructor',
  17216. nPasElementNotSupported);
  17217. ConvertProgram;
  17218. end;
  17219. procedure TTestModule.TestExternalClass_New;
  17220. begin
  17221. StartProgram(false);
  17222. Add([
  17223. '{$modeswitch externalclass}',
  17224. 'type',
  17225. ' TExtA = class external name ''ExtA''',
  17226. ' constructor New;',
  17227. ' constructor New(i: longint; j: longint = 2);',
  17228. ' end;',
  17229. 'var',
  17230. ' A: texta;',
  17231. 'begin',
  17232. ' a:=texta.new;',
  17233. ' a:=texta(texta.new);',
  17234. ' a:=texta.new();',
  17235. ' a:=texta.new(1);',
  17236. ' with texta do begin',
  17237. ' a:=new;',
  17238. ' a:=new();',
  17239. ' a:=new(2);',
  17240. ' end;',
  17241. ' a:=test1.texta.new;',
  17242. ' a:=test1.texta.new();',
  17243. ' a:=test1.texta.new(3);',
  17244. '']);
  17245. ConvertProgram;
  17246. CheckSource('TestExternalClass_New',
  17247. LinesToStr([ // statements
  17248. 'this.A = null;',
  17249. '']),
  17250. LinesToStr([ // $mod.$main
  17251. '$mod.A = new ExtA();',
  17252. '$mod.A = new ExtA();',
  17253. '$mod.A = new ExtA();',
  17254. '$mod.A = new ExtA(1,2);',
  17255. '$mod.A = new ExtA();',
  17256. '$mod.A = new ExtA();',
  17257. '$mod.A = new ExtA(2,2);',
  17258. '$mod.A = new ExtA();',
  17259. '$mod.A = new ExtA();',
  17260. '$mod.A = new ExtA(3,2);',
  17261. '']));
  17262. end;
  17263. procedure TTestModule.TestExternalClass_ClassOf_New;
  17264. begin
  17265. StartProgram(false);
  17266. Add('{$modeswitch externalclass}');
  17267. Add('type');
  17268. Add(' TExtAClass = class of TExtA;');
  17269. Add(' TExtA = class external name ''ExtA''');
  17270. Add(' C: TExtAClass;');
  17271. Add(' constructor New;');
  17272. Add(' end;');
  17273. Add('var');
  17274. Add(' A: texta;');
  17275. Add(' C: textaclass;');
  17276. Add('begin');
  17277. Add(' a:=c.new;');
  17278. Add(' a:=c.new();');
  17279. Add(' with C do begin');
  17280. Add(' a:=new;');
  17281. Add(' a:=new();');
  17282. Add(' end;');
  17283. Add(' a:=test1.c.new;');
  17284. Add(' a:=test1.c.new();');
  17285. Add(' a:=A.c.new();');
  17286. ConvertProgram;
  17287. CheckSource('TestExternalClass_ClassOf_New',
  17288. LinesToStr([ // statements
  17289. 'this.A = null;',
  17290. 'this.C = null;',
  17291. '']),
  17292. LinesToStr([ // $mod.$main
  17293. '$mod.A = new $mod.C();',
  17294. '$mod.A = new $mod.C();',
  17295. 'var $with = $mod.C;',
  17296. '$mod.A = new $with();',
  17297. '$mod.A = new $with();',
  17298. '$mod.A = new $mod.C();',
  17299. '$mod.A = new $mod.C();',
  17300. '$mod.A = new $mod.A.C();',
  17301. '']));
  17302. end;
  17303. procedure TTestModule.TestExternalClass_FuncClassOf_New;
  17304. begin
  17305. StartProgram(false);
  17306. Add([
  17307. '{$modeswitch externalclass}',
  17308. 'type',
  17309. ' TExtAClass = class of TExtA;',
  17310. ' TExtA = class external name ''ExtA''',
  17311. ' constructor New;',
  17312. ' end;',
  17313. 'function GetCreator: TExtAClass;',
  17314. 'begin',
  17315. ' Result:=TExtA;',
  17316. 'end;',
  17317. 'var',
  17318. ' A: texta;',
  17319. 'begin',
  17320. ' a:=getcreator.new;',
  17321. ' a:=getcreator().new;',
  17322. ' a:=getcreator().new();',
  17323. ' a:=getcreator.new();',
  17324. ' with getcreator do begin',
  17325. ' a:=new;',
  17326. ' a:=new();',
  17327. ' end;']);
  17328. ConvertProgram;
  17329. CheckSource('TestExternalClass_FuncClassOf_New',
  17330. LinesToStr([ // statements
  17331. 'this.GetCreator = function () {',
  17332. ' var Result = null;',
  17333. ' Result = ExtA;',
  17334. ' return Result;',
  17335. '};',
  17336. 'this.A = null;',
  17337. '']),
  17338. LinesToStr([ // $mod.$main
  17339. '$mod.A = new ($mod.GetCreator())();',
  17340. '$mod.A = new ($mod.GetCreator())();',
  17341. '$mod.A = new ($mod.GetCreator())();',
  17342. '$mod.A = new ($mod.GetCreator())();',
  17343. 'var $with = $mod.GetCreator();',
  17344. '$mod.A = new $with();',
  17345. '$mod.A = new $with();',
  17346. '']));
  17347. end;
  17348. procedure TTestModule.TestExternalClass_New_PasClassFail;
  17349. begin
  17350. StartProgram(false);
  17351. Add([
  17352. '{$modeswitch externalclass}',
  17353. 'type',
  17354. ' TExtA = class external name ''ExtA''',
  17355. ' constructor New;',
  17356. ' end;',
  17357. ' TBird = class(TExtA)',
  17358. ' end;',
  17359. 'begin',
  17360. ' TBird.new;',
  17361. '']);
  17362. SetExpectedPasResolverError(sJSNewNotSupported,nJSNewNotSupported);
  17363. ConvertProgram;
  17364. end;
  17365. procedure TTestModule.TestExternalClass_New_PasClassBracketsFail;
  17366. begin
  17367. StartProgram(false);
  17368. Add([
  17369. '{$modeswitch externalclass}',
  17370. 'type',
  17371. ' TExtA = class external name ''ExtA''',
  17372. ' constructor New;',
  17373. ' end;',
  17374. ' TBird = class(TExtA)',
  17375. ' end;',
  17376. 'begin',
  17377. ' TBird.new();',
  17378. '']);
  17379. SetExpectedPasResolverError(sJSNewNotSupported,nJSNewNotSupported);
  17380. ConvertProgram;
  17381. end;
  17382. procedure TTestModule.TestExternalClass_NewExtName;
  17383. begin
  17384. StartProgram(false);
  17385. Add([
  17386. '{$modeswitch externalclass}',
  17387. 'type',
  17388. ' TExtA = class external name ''ExtA''',
  17389. ' constructor New; external name ''Other'';',
  17390. ' constructor New(i: longint; j: longint = 2); external name ''A.B'';',
  17391. ' end;',
  17392. 'var',
  17393. ' A: texta;',
  17394. 'begin',
  17395. ' a:=texta.new;',
  17396. ' a:=texta(texta.new);',
  17397. ' a:=texta.new();',
  17398. ' a:=texta.new(1);',
  17399. ' with texta do begin',
  17400. ' a:=new;',
  17401. ' a:=new();',
  17402. ' a:=new(2);',
  17403. ' end;',
  17404. ' a:=test1.texta.new;',
  17405. ' a:=test1.texta.new();',
  17406. ' a:=test1.texta.new(3);',
  17407. '']);
  17408. ConvertProgram;
  17409. CheckSource('TestExternalClass_NewExtName',
  17410. LinesToStr([ // statements
  17411. 'this.A = null;',
  17412. '']),
  17413. LinesToStr([ // $mod.$main
  17414. '$mod.A = new Other();',
  17415. '$mod.A = new Other();',
  17416. '$mod.A = new Other();',
  17417. '$mod.A = new A.B(1,2);',
  17418. '$mod.A = new Other();',
  17419. '$mod.A = new Other();',
  17420. '$mod.A = new A.B(2,2);',
  17421. '$mod.A = new Other();',
  17422. '$mod.A = new Other();',
  17423. '$mod.A = new A.B(3,2);',
  17424. '']));
  17425. end;
  17426. procedure TTestModule.TestExternalClass_Constructor;
  17427. begin
  17428. StartProgram(false);
  17429. Add([
  17430. '{$modeswitch externalclass}',
  17431. 'type',
  17432. ' TExtA = class external name ''ExtA''',
  17433. ' constructor Create;',
  17434. ' constructor Create(i: longint; j: longint = 2);',
  17435. ' end;',
  17436. 'var',
  17437. ' A: texta;',
  17438. 'begin',
  17439. ' a:=texta.create;',
  17440. ' a:=texta(texta.create);',
  17441. ' a:=texta.create();',
  17442. ' a:=texta.create(1);',
  17443. ' with texta do begin',
  17444. ' a:=create;',
  17445. ' a:=create();',
  17446. ' a:=create(2);',
  17447. ' end;',
  17448. ' a:=test1.texta.create;',
  17449. ' a:=test1.texta.create();',
  17450. ' a:=test1.texta.create(3);',
  17451. '']);
  17452. ConvertProgram;
  17453. CheckSource('TestExternalClass_Constructor',
  17454. LinesToStr([ // statements
  17455. 'this.A = null;',
  17456. '']),
  17457. LinesToStr([ // $mod.$main
  17458. '$mod.A = new ExtA.Create();',
  17459. '$mod.A = new ExtA.Create();',
  17460. '$mod.A = new ExtA.Create();',
  17461. '$mod.A = new ExtA.Create(1,2);',
  17462. '$mod.A = new ExtA.Create();',
  17463. '$mod.A = new ExtA.Create();',
  17464. '$mod.A = new ExtA.Create(2,2);',
  17465. '$mod.A = new ExtA.Create();',
  17466. '$mod.A = new ExtA.Create();',
  17467. '$mod.A = new ExtA.Create(3,2);',
  17468. '']));
  17469. end;
  17470. procedure TTestModule.TestExternalClass_ConstructorBrackets;
  17471. begin
  17472. StartProgram(false);
  17473. Add([
  17474. '{$modeswitch externalclass}',
  17475. 'type',
  17476. ' TExtA = class external name ''ExtA''',
  17477. ' constructor Create; external name ''{}'';',
  17478. ' end;',
  17479. 'var',
  17480. ' A: texta;',
  17481. 'begin',
  17482. ' a:=texta.create;',
  17483. ' a:=texta(texta.create);',
  17484. ' a:=texta.create();',
  17485. ' with texta do begin',
  17486. ' a:=create;',
  17487. ' a:=create();',
  17488. ' end;',
  17489. ' a:=test1.texta.create;',
  17490. ' a:=test1.texta.create();',
  17491. '']);
  17492. ConvertProgram;
  17493. CheckSource('TestExternalClass_ConstructorBrackets',
  17494. LinesToStr([ // statements
  17495. 'this.A = null;',
  17496. '']),
  17497. LinesToStr([ // $mod.$main
  17498. '$mod.A = {};',
  17499. '$mod.A = {};',
  17500. '$mod.A = {};',
  17501. '$mod.A = {};',
  17502. '$mod.A = {};',
  17503. '$mod.A = {};',
  17504. '$mod.A = {};',
  17505. '']));
  17506. end;
  17507. procedure TTestModule.TestExternalClass_LocalConstSameName;
  17508. begin
  17509. StartProgram(false);
  17510. Add('{$modeswitch externalclass}');
  17511. Add('type');
  17512. Add(' TExtA = class external name ''ExtA''');
  17513. Add(' constructor New;');
  17514. Add(' end;');
  17515. Add('function DoIt: longint;');
  17516. Add('const ExtA: longint = 3;');
  17517. Add('begin');
  17518. Add(' Result:=ExtA;');
  17519. Add('end;');
  17520. Add('var');
  17521. Add(' A: texta;');
  17522. Add('begin');
  17523. Add(' a:=texta.new;');
  17524. ConvertProgram;
  17525. CheckSource('TestExternalClass_LocalConstSameName',
  17526. LinesToStr([ // statements
  17527. 'var ExtA$1 = 3;',
  17528. 'this.DoIt = function () {',
  17529. ' var Result = 0;',
  17530. ' Result = ExtA$1;',
  17531. ' return Result;',
  17532. '};',
  17533. 'this.A = null;',
  17534. '']),
  17535. LinesToStr([ // $mod.$main
  17536. '$mod.A = new ExtA();',
  17537. '']));
  17538. end;
  17539. procedure TTestModule.TestExternalClass_ReintroduceOverload;
  17540. begin
  17541. StartProgram(false);
  17542. Add('{$modeswitch externalclass}');
  17543. Add('type');
  17544. Add(' TExtA = class external name ''ExtA''');
  17545. Add(' procedure DoIt;');
  17546. Add(' end;');
  17547. Add(' TMyA = class(TExtA)');
  17548. Add(' procedure DoIt;');
  17549. Add(' end;');
  17550. Add('procedure TMyA.DoIt; begin end;');
  17551. Add('begin');
  17552. ConvertProgram;
  17553. CheckSource('TestExternalClass_ReintroduceOverload',
  17554. LinesToStr([ // statements
  17555. 'rtl.createClassExt(this, "TMyA", ExtA, "", function () {',
  17556. ' this.$init = function () {',
  17557. ' };',
  17558. ' this.$final = function () {',
  17559. ' };',
  17560. ' this.DoIt$1 = function () {',
  17561. ' };',
  17562. '});',
  17563. '']),
  17564. LinesToStr([ // $mod.$main
  17565. '']));
  17566. end;
  17567. procedure TTestModule.TestExternalClass_Inherited;
  17568. begin
  17569. StartProgram(false);
  17570. Add('{$modeswitch externalclass}');
  17571. Add('type');
  17572. Add(' TExtA = class external name ''ExtA''');
  17573. Add(' procedure DoIt(i: longint = 1); virtual;');
  17574. Add(' procedure DoSome(j: longint = 2);');
  17575. Add(' end;');
  17576. Add(' TExtB = class external name ''ExtB''(TExtA)');
  17577. Add(' end;');
  17578. Add(' TMyC = class(TExtB)');
  17579. Add(' procedure DoIt(i: longint = 1); override;');
  17580. Add(' procedure DoSome(j: longint = 2); reintroduce;');
  17581. Add(' end;');
  17582. Add('procedure TMyC.DoIt(i: longint);');
  17583. Add('begin');
  17584. Add(' inherited;');
  17585. Add(' inherited DoIt;');
  17586. Add(' inherited DoIt();');
  17587. Add(' inherited DoIt(3);');
  17588. Add(' inherited DoSome;');
  17589. Add(' inherited DoSome();');
  17590. Add(' inherited DoSome(4);');
  17591. Add('end;');
  17592. Add('procedure TMyC.DoSome(j: longint);');
  17593. Add('begin');
  17594. Add(' inherited;');
  17595. Add('end;');
  17596. Add('begin');
  17597. ConvertProgram;
  17598. CheckSource('TestExternalClass_ReintroduceOverload',
  17599. LinesToStr([ // statements
  17600. 'rtl.createClassExt(this, "TMyC", ExtB, "", function () {',
  17601. ' this.$init = function () {',
  17602. ' };',
  17603. ' this.$final = function () {',
  17604. ' };',
  17605. ' this.DoIt = function (i) {',
  17606. ' ExtB.DoIt.apply(this, arguments);',
  17607. ' ExtB.DoIt.call(this, 1);',
  17608. ' ExtB.DoIt.call(this, 1);',
  17609. ' ExtB.DoIt.call(this, 3);',
  17610. ' ExtB.DoSome.call(this, 2);',
  17611. ' ExtB.DoSome.call(this, 2);',
  17612. ' ExtB.DoSome.call(this, 4);',
  17613. ' };',
  17614. ' this.DoSome$1 = function (j) {',
  17615. ' ExtB.DoSome.apply(this, arguments);',
  17616. ' };',
  17617. '});',
  17618. '']),
  17619. LinesToStr([ // $mod.$main
  17620. '']));
  17621. end;
  17622. procedure TTestModule.TestExternalClass_PascalAncestorFail;
  17623. begin
  17624. StartProgram(false);
  17625. Add('{$modeswitch externalclass}');
  17626. Add('type');
  17627. Add(' TObject = class');
  17628. Add(' end;');
  17629. Add(' TExtA = class external name ''ExtA''(TObject)');
  17630. Add(' end;');
  17631. Add('begin');
  17632. SetExpectedPasResolverError('Ancestor "TObject" is not external',nAncestorIsNotExternal);
  17633. ConvertProgram;
  17634. end;
  17635. procedure TTestModule.TestExternalClass_NewInstance;
  17636. begin
  17637. StartProgram(false);
  17638. Add('{$modeswitch externalclass}');
  17639. Add('type');
  17640. Add(' TExtA = class external name ''ExtA''');
  17641. Add(' end;');
  17642. Add(' TMyB = class(TExtA)');
  17643. Add(' protected');
  17644. Add(' class function NewInstance(fnname: string; const paramarray): TMyB; virtual;');
  17645. Add(' end;');
  17646. Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
  17647. Add('begin end;');
  17648. Add('begin');
  17649. ConvertProgram;
  17650. CheckSource('TestExternalClass_NewInstance',
  17651. LinesToStr([ // statements
  17652. 'rtl.createClassExt(this, "TMyB", ExtA, "NewInstance", function () {',
  17653. ' this.$init = function () {',
  17654. ' };',
  17655. ' this.$final = function () {',
  17656. ' };',
  17657. ' this.NewInstance = function (fnname, paramarray) {',
  17658. ' var Result = null;',
  17659. ' return Result;',
  17660. ' };',
  17661. '});',
  17662. '']),
  17663. LinesToStr([ // $mod.$main
  17664. '']));
  17665. end;
  17666. procedure TTestModule.TestExternalClass_NewInstance_NonVirtualFail;
  17667. begin
  17668. StartProgram(false);
  17669. Add('{$modeswitch externalclass}');
  17670. Add('type');
  17671. Add(' TExtA = class external name ''ExtA''');
  17672. Add(' end;');
  17673. Add(' TMyB = class(TExtA)');
  17674. Add(' protected');
  17675. Add(' class function NewInstance(fnname: string; const paramarray): TMyB;');
  17676. Add(' end;');
  17677. Add('class function TMyB.NewInstance(fnname: string; const paramarray): TMyB;');
  17678. Add('begin end;');
  17679. Add('begin');
  17680. SetExpectedPasResolverError(sNewInstanceFunctionMustBeVirtual,nNewInstanceFunctionMustBeVirtual);
  17681. ConvertProgram;
  17682. end;
  17683. procedure TTestModule.TestExternalClass_NewInstance_FirstParamNotString_Fail;
  17684. begin
  17685. StartProgram(false);
  17686. Add('{$modeswitch externalclass}');
  17687. Add('type');
  17688. Add(' TExtA = class external name ''ExtA''');
  17689. Add(' end;');
  17690. Add(' TMyB = class(TExtA)');
  17691. Add(' protected');
  17692. Add(' class function NewInstance(fnname: longint; const paramarray): TMyB; virtual;');
  17693. Add(' end;');
  17694. Add('class function TMyB.NewInstance(fnname: longint; const paramarray): TMyB;');
  17695. Add('begin end;');
  17696. Add('begin');
  17697. SetExpectedPasResolverError('Incompatible type arg no. 1: Got "Longint", expected "String"',
  17698. nIncompatibleTypeArgNo);
  17699. ConvertProgram;
  17700. end;
  17701. procedure TTestModule.TestExternalClass_NewInstance_SecondParamTyped_Fail;
  17702. begin
  17703. StartProgram(false);
  17704. Add('{$modeswitch externalclass}');
  17705. Add('type');
  17706. Add(' TExtA = class external name ''ExtA''');
  17707. Add(' end;');
  17708. Add(' TMyB = class(TExtA)');
  17709. Add(' protected');
  17710. Add(' class function NewInstance(fnname: string; const paramarray: string): TMyB; virtual;');
  17711. Add(' end;');
  17712. Add('class function TMyB.NewInstance(fnname: string; const paramarray: string): TMyB;');
  17713. Add('begin end;');
  17714. Add('begin');
  17715. SetExpectedPasResolverError('Incompatible type arg no. 2: Got "type", expected "untyped"',
  17716. nIncompatibleTypeArgNo);
  17717. ConvertProgram;
  17718. end;
  17719. procedure TTestModule.TestExternalClass_JSFunctionPasDescendant;
  17720. begin
  17721. StartProgram(false);
  17722. Add([
  17723. '{$modeswitch externalclass}',
  17724. 'type',
  17725. ' TJSFunction = class external name ''Function''',
  17726. ' end;',
  17727. ' TExtA = class external name ''ExtA''(TJSFunction)',
  17728. ' constructor New(w: word);',
  17729. ' end;',
  17730. ' TBird = class (TExtA)',
  17731. ' public',
  17732. ' Size: word;',
  17733. ' class var Legs: word;',
  17734. ' constructor Create(a: word);',
  17735. ' end;',
  17736. ' TEagle = class (TBird)',
  17737. ' public',
  17738. ' constructor Create(b: word); reintroduce;',
  17739. ' end;',
  17740. 'constructor TBird.Create(a: word);',
  17741. 'begin',
  17742. ' inherited;', // silently ignored
  17743. ' inherited New(a);', // this.$func(a)
  17744. 'end;',
  17745. 'constructor TEagle.Create(b: word);',
  17746. 'begin',
  17747. ' inherited Create(b);',
  17748. 'end;',
  17749. 'var',
  17750. ' Bird: TBird;',
  17751. ' Eagle: TEagle;',
  17752. 'begin',
  17753. ' Bird:=TBird.Create(3);',
  17754. ' Eagle:=TEagle.Create(4);',
  17755. ' Bird.Size:=Bird.Size+5;',
  17756. ' Bird.Legs:=Bird.Legs+6;',
  17757. ' Eagle.Size:=Eagle.Size+5;',
  17758. ' Eagle.Legs:=Eagle.Legs+6;',
  17759. '']);
  17760. ConvertProgram;
  17761. CheckSource('TestExternalClass_JSFunctionPasDescendant',
  17762. LinesToStr([ // statements
  17763. 'rtl.createClassExt(this, "TBird", ExtA, "", function () {',
  17764. ' this.Legs = 0;',
  17765. ' this.$init = function () {',
  17766. ' this.Size = 0;',
  17767. ' };',
  17768. ' this.$final = function () {',
  17769. ' };',
  17770. ' this.Create = function (a) {',
  17771. ' this.$ancestorfunc(a);',
  17772. ' return this;',
  17773. ' };',
  17774. '});',
  17775. 'rtl.createClassExt(this, "TEagle", this.TBird, "", function () {',
  17776. ' this.Create$1 = function (b) {',
  17777. ' $mod.TBird.Create.call(this, b);',
  17778. ' return this;',
  17779. ' };',
  17780. '});',
  17781. 'this.Bird = null;',
  17782. 'this.Eagle = null;',
  17783. '']),
  17784. LinesToStr([ // $mod.$main
  17785. '$mod.Bird = $mod.TBird.$create("Create", [3]);',
  17786. '$mod.Eagle = $mod.TEagle.$create("Create$1", [4]);',
  17787. '$mod.Bird.Size = $mod.Bird.Size + 5;',
  17788. '$mod.TBird.Legs = $mod.Bird.Legs + 6;',
  17789. '$mod.Eagle.Size = $mod.Eagle.Size + 5;',
  17790. '$mod.TBird.Legs = $mod.Eagle.Legs + 6;',
  17791. '']));
  17792. end;
  17793. procedure TTestModule.TestExternalClass_PascalProperty;
  17794. begin
  17795. StartProgram(false);
  17796. Add('{$modeswitch externalclass}');
  17797. Add('type');
  17798. Add(' TJSElement = class;');
  17799. Add(' TJSNotifyEvent = procedure(Sender: TJSElement) of object;');
  17800. Add(' TJSElement = class external name ''ExtA''');
  17801. Add(' end;');
  17802. Add(' TControl = class(TJSElement)');
  17803. Add(' private');
  17804. Add(' FOnClick: TJSNotifyEvent;');
  17805. Add(' property OnClick: TJSNotifyEvent read FOnClick write FOnClick;');
  17806. Add(' procedure Click(Sender: TJSElement);');
  17807. Add(' end;');
  17808. Add('procedure TControl.Click(Sender: TJSElement);');
  17809. Add('begin');
  17810. Add(' OnClick(Self);');
  17811. Add('end;');
  17812. Add('var');
  17813. Add(' Ctrl: TControl;');
  17814. Add('begin');
  17815. Add(' Ctrl.OnClick:[email protected];');
  17816. Add(' Ctrl.OnClick(Ctrl);');
  17817. ConvertProgram;
  17818. CheckSource('TestExternalClass_PascalProperty',
  17819. LinesToStr([ // statements
  17820. 'rtl.createClassExt(this, "TControl", ExtA, "", function () {',
  17821. ' this.$init = function () {',
  17822. ' this.FOnClick = null;',
  17823. ' };',
  17824. ' this.$final = function () {',
  17825. ' this.FOnClick = undefined;',
  17826. ' };',
  17827. ' this.Click = function (Sender) {',
  17828. ' this.FOnClick(this);',
  17829. ' };',
  17830. '});',
  17831. 'this.Ctrl = null;',
  17832. '']),
  17833. LinesToStr([ // $mod.$main
  17834. '$mod.Ctrl.FOnClick = rtl.createCallback($mod.Ctrl, "Click");',
  17835. '$mod.Ctrl.FOnClick($mod.Ctrl);',
  17836. '']));
  17837. end;
  17838. procedure TTestModule.TestExternalClass_TypeCastToRootClass;
  17839. begin
  17840. StartProgram(false);
  17841. Add([
  17842. '{$modeswitch externalclass}',
  17843. 'type',
  17844. ' IUnknown = interface end;',
  17845. ' TObject = class',
  17846. ' end;',
  17847. ' TChild = class',
  17848. ' end;',
  17849. ' TExtRootA = class external name ''ExtRootA''',
  17850. ' end;',
  17851. ' TExtChildA = class external name ''ExtChildA''(TExtRootA)',
  17852. ' end;',
  17853. ' TExtRootB = class external name ''ExtRootB''',
  17854. ' end;',
  17855. ' TExtChildB = class external name ''ExtChildB''(TExtRootB)',
  17856. ' end;',
  17857. 'var',
  17858. ' Obj: TObject;',
  17859. ' Child: TChild;',
  17860. ' RootA: TExtRootA;',
  17861. ' ChildA: TExtChildA;',
  17862. ' RootB: TExtRootB;',
  17863. ' ChildB: TExtChildB;',
  17864. ' i: IUnknown;',
  17865. 'begin',
  17866. ' obj:=tobject(roota);',
  17867. ' obj:=tobject(childa);',
  17868. ' child:=tchild(tobject(roota));',
  17869. ' roota:=textroota(obj);',
  17870. ' roota:=textroota(child);',
  17871. ' roota:=textroota(rootb);',
  17872. ' roota:=textroota(childb);',
  17873. ' childa:=textchilda(textroota(obj));',
  17874. ' roota:=TExtRootA(i)',
  17875. '']);
  17876. ConvertProgram;
  17877. CheckSource('TestExternalClass_TypeCastToRootClass',
  17878. LinesToStr([ // statements
  17879. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  17880. 'rtl.createClass(this, "TObject", null, function () {',
  17881. ' this.$init = function () {',
  17882. ' };',
  17883. ' this.$final = function () {',
  17884. ' };',
  17885. '});',
  17886. 'rtl.createClass(this, "TChild", this.TObject, function () {',
  17887. '});',
  17888. 'this.Obj = null;',
  17889. 'this.Child = null;',
  17890. 'this.RootA = null;',
  17891. 'this.ChildA = null;',
  17892. 'this.RootB = null;',
  17893. 'this.ChildB = null;',
  17894. 'this.i = null;',
  17895. '']),
  17896. LinesToStr([ // $mod.$main
  17897. '$mod.Obj = $mod.RootA;',
  17898. '$mod.Obj = $mod.ChildA;',
  17899. '$mod.Child = $mod.RootA;',
  17900. '$mod.RootA = $mod.Obj;',
  17901. '$mod.RootA = $mod.Child;',
  17902. '$mod.RootA = $mod.RootB;',
  17903. '$mod.RootA = $mod.ChildB;',
  17904. '$mod.ChildA = $mod.Obj;',
  17905. '$mod.RootA = $mod.i;',
  17906. '']));
  17907. end;
  17908. procedure TTestModule.TestExternalClass_TypeCastToJSObject;
  17909. begin
  17910. StartProgram(false);
  17911. Add([
  17912. '{$modeswitch externalclass}',
  17913. 'type',
  17914. ' IUnknown = interface end;',
  17915. ' IBird = interface(IUnknown) end;',
  17916. ' TClass = class of TObject;',
  17917. ' TObject = class',
  17918. ' end;',
  17919. ' TChild = class',
  17920. ' end;',
  17921. ' TJSObject = class external name ''Object''',
  17922. ' end;',
  17923. ' TRec = record end;',
  17924. 'var',
  17925. ' Obj: TObject;',
  17926. ' Child: TChild;',
  17927. ' i: IUnknown;',
  17928. ' Bird: IBird;',
  17929. ' j: TJSObject;',
  17930. ' r: TRec;',
  17931. ' c: TClass;',
  17932. 'begin',
  17933. ' j:=tjsobject(IUnknown);',
  17934. ' j:=tjsobject(IBird);',
  17935. ' j:=tjsobject(TObject);',
  17936. ' j:=tjsobject(TChild);',
  17937. ' j:=tjsobject(TRec);',
  17938. ' j:=tjsobject(Obj);',
  17939. ' j:=tjsobject(Child);',
  17940. ' j:=tjsobject(i);',
  17941. ' j:=tjsobject(Bird);',
  17942. ' j:=tjsobject(r);',
  17943. ' j:=tjsobject(c);',
  17944. '']);
  17945. ConvertProgram;
  17946. CheckSource('TestExternalClass_TypeCastToJSObject',
  17947. LinesToStr([ // statements
  17948. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  17949. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], this.IUnknown);',
  17950. 'rtl.createClass(this, "TObject", null, function () {',
  17951. ' this.$init = function () {',
  17952. ' };',
  17953. ' this.$final = function () {',
  17954. ' };',
  17955. '});',
  17956. 'rtl.createClass(this, "TChild", this.TObject, function () {',
  17957. '});',
  17958. 'rtl.recNewT(this, "TRec", function () {',
  17959. ' this.$eq = function (b) {',
  17960. ' return true;',
  17961. ' };',
  17962. ' this.$assign = function (s) {',
  17963. ' return this;',
  17964. ' };',
  17965. '});',
  17966. 'this.Obj = null;',
  17967. 'this.Child = null;',
  17968. 'this.i = null;',
  17969. 'this.Bird = null;',
  17970. 'this.j = null;',
  17971. 'this.r = this.TRec.$new();',
  17972. 'this.c = null;',
  17973. '']),
  17974. LinesToStr([ // $mod.$main
  17975. '$mod.j = $mod.IUnknown;',
  17976. '$mod.j = $mod.IBird;',
  17977. '$mod.j = $mod.TObject;',
  17978. '$mod.j = $mod.TChild;',
  17979. '$mod.j = $mod.TRec;',
  17980. '$mod.j = $mod.Obj;',
  17981. '$mod.j = $mod.Child;',
  17982. '$mod.j = $mod.i;',
  17983. '$mod.j = $mod.Bird;',
  17984. '$mod.j = $mod.r;',
  17985. '$mod.j = $mod.c;',
  17986. '']));
  17987. end;
  17988. procedure TTestModule.TestExternalClass_TypeCastStringToExternalString;
  17989. begin
  17990. StartProgram(false);
  17991. Add('{$modeswitch externalclass}');
  17992. Add('type');
  17993. Add(' TJSString = class external name ''String''');
  17994. Add(' class function fromCharCode() : string; varargs;');
  17995. Add(' function anchor(const aName : string) : string;');
  17996. Add(' end;');
  17997. Add('var');
  17998. Add(' s: string;');
  17999. Add('begin');
  18000. Add(' s:=TJSString.fromCharCode(65,66);');
  18001. Add(' s:=TJSString(s).anchor(s);');
  18002. Add(' s:=TJSString(''foo'').anchor(s);');
  18003. ConvertProgram;
  18004. CheckSource('TestExternalClass_TypeCastStringToExternalString',
  18005. LinesToStr([ // statements
  18006. 'this.s = "";',
  18007. '']),
  18008. LinesToStr([ // $mod.$main
  18009. '$mod.s = String.fromCharCode(65, 66);',
  18010. '$mod.s = $mod.s.anchor($mod.s);',
  18011. '$mod.s = "foo".anchor($mod.s);',
  18012. '']));
  18013. end;
  18014. procedure TTestModule.TestExternalClass_TypeCastToJSFunction;
  18015. begin
  18016. StartProgram(false);
  18017. Add([
  18018. '{$modeswitch externalclass}',
  18019. 'type',
  18020. ' TJSObject = class external name ''Object'' end;',
  18021. ' TJSFunction = class external name ''Function''',
  18022. ' function bind(thisArg: TJSObject): TJSFunction; varargs;',
  18023. ' function call(thisArg: TJSObject): JSValue; varargs;',
  18024. ' end;',
  18025. ' TObject = class',
  18026. ' procedure DoIt(i: longint);',
  18027. ' end;',
  18028. ' TFuncInt = function(o: TObject): longint;',
  18029. 'function GetIt(o: TObject): longint;',
  18030. ' procedure Sub; begin end;',
  18031. 'var',
  18032. ' f: TJSFunction;',
  18033. ' fi: TFuncInt;',
  18034. 'begin',
  18035. ' fi:=TFuncInt(f);',
  18036. ' f:=TJSFunction(fi);',
  18037. ' f:=TJSFunction(@GetIt);',
  18038. ' f:=TJSFunction(@GetIt).bind(nil,3);',
  18039. ' f:=TJSFunction(@Sub);',
  18040. ' f:=TJSFunction(@o.doit);',
  18041. ' f:=TJSFunction(fi).bind(nil,4)',
  18042. 'end;',
  18043. 'procedure TObject.DoIt(i: longint);',
  18044. ' procedure Sub; begin end;',
  18045. 'var f: TJSFunction;',
  18046. 'begin',
  18047. ' f:=TJSFunction(@DoIt);',
  18048. ' f:=TJSFunction(@DoIt).bind(nil,13);',
  18049. ' f:=TJSFunction(@Sub);',
  18050. ' f:=TJSFunction(@GetIt);',
  18051. 'end;',
  18052. 'begin']);
  18053. ConvertProgram;
  18054. CheckSource('TestExternalClass_TypeCastToJSFunction',
  18055. LinesToStr([ // statements
  18056. 'rtl.createClass(this, "TObject", null, function () {',
  18057. ' this.$init = function () {',
  18058. ' };',
  18059. ' this.$final = function () {',
  18060. ' };',
  18061. ' this.DoIt = function (i) {',
  18062. ' var $Self = this;',
  18063. ' function Sub() {',
  18064. ' };',
  18065. ' var f = null;',
  18066. ' f = this.DoIt;',
  18067. ' f = this.DoIt.bind(null, 13);',
  18068. ' f = Sub;',
  18069. ' f = $mod.GetIt;',
  18070. ' };',
  18071. '});',
  18072. 'this.GetIt = function (o) {',
  18073. ' var Result = 0;',
  18074. ' function Sub() {',
  18075. ' };',
  18076. ' var f = null;',
  18077. ' var fi = null;',
  18078. ' fi = f;',
  18079. ' f = fi;',
  18080. ' f = $mod.GetIt;',
  18081. ' f = $mod.GetIt.bind(null, 3);',
  18082. ' f = Sub;',
  18083. ' f = $mod.TObject.DoIt;',
  18084. ' f = fi.bind(null, 4);',
  18085. ' return Result;',
  18086. '};',
  18087. '']),
  18088. LinesToStr([ // $mod.$main
  18089. '']));
  18090. end;
  18091. procedure TTestModule.TestExternalClass_TypeCastDelphiUnrelated;
  18092. begin
  18093. StartProgram(false);
  18094. Add([
  18095. '{$mode delphi}',
  18096. '{$modeswitch externalclass}',
  18097. 'type',
  18098. ' TJSObject = class external name ''Object'' end;',
  18099. ' TJSWindow = class external name ''Window''(TJSObject)',
  18100. ' procedure Open;',
  18101. ' end;',
  18102. ' TJSEventTarget = class external name ''Event''(TJSObject)',
  18103. ' procedure Execute;',
  18104. ' end;',
  18105. 'procedure Fly;',
  18106. 'var',
  18107. ' w: TJSWindow;',
  18108. ' e: TJSEventTarget;',
  18109. 'begin',
  18110. ' w:=TJSWindow(e);',
  18111. ' e:=TJSEventTarget(w);',
  18112. 'end;',
  18113. 'begin']);
  18114. ConvertProgram;
  18115. CheckSource('TestExternalClass_TypeCastDelphiUnrelated',
  18116. LinesToStr([ // statements
  18117. 'this.Fly = function () {',
  18118. ' var w = null;',
  18119. ' var e = null;',
  18120. ' w = e;',
  18121. ' e = w;',
  18122. '};',
  18123. '']),
  18124. LinesToStr([ // $mod.$main
  18125. '']));
  18126. end;
  18127. procedure TTestModule.TestExternalClass_CallClassFunctionOfInstanceFail;
  18128. begin
  18129. StartProgram(false);
  18130. Add('{$modeswitch externalclass}');
  18131. Add('type');
  18132. Add(' TJSString = class external name ''String''');
  18133. Add(' class function fromCharCode() : string; varargs;');
  18134. Add(' end;');
  18135. Add('var');
  18136. Add(' s: string;');
  18137. Add(' sObj: TJSString;');
  18138. Add('begin');
  18139. Add(' s:=sObj.fromCharCode(65,66);');
  18140. SetExpectedPasResolverError('External class instance cannot access static class function fromCharCode',
  18141. nExternalClassInstanceCannotAccessStaticX);
  18142. ConvertProgram;
  18143. end;
  18144. procedure TTestModule.TestExternalClass_BracketAccessor;
  18145. begin
  18146. StartProgram(false);
  18147. Add([
  18148. '{$modeswitch externalclass}',
  18149. 'type',
  18150. ' TJSArray = class external name ''Array2''',
  18151. ' function GetItems(Index: longint): jsvalue; external name ''[]'';',
  18152. ' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';',
  18153. ' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;',
  18154. ' end;',
  18155. 'procedure DoIt(vI: JSValue; const vJ: jsvalue; var vK: jsvalue; out vL: jsvalue);',
  18156. 'begin end;',
  18157. 'var',
  18158. ' Arr: tjsarray;',
  18159. ' s: string;',
  18160. ' i: longint;',
  18161. ' v: jsvalue;',
  18162. 'begin',
  18163. ' v:=arr[0];',
  18164. ' v:=arr.items[1];',
  18165. ' arr[2]:=s;',
  18166. ' arr.items[3]:=s;',
  18167. ' arr[4]:=i;',
  18168. ' arr[5]:=arr[6];',
  18169. ' arr.items[7]:=arr.items[8];',
  18170. ' with arr do items[9]:=items[10];',
  18171. ' doit(arr[7],arr[8],arr[9],arr[10]);',
  18172. ' with arr do begin',
  18173. ' v:=GetItems(14);',
  18174. ' setitems(15,16);',
  18175. ' end;',
  18176. ' v:=test1.arr.items[17];',
  18177. ' test1.arr.items[18]:=v;',
  18178. '']);
  18179. ConvertProgram;
  18180. CheckSource('TestExternalClass_BracketAccessor',
  18181. LinesToStr([ // statements
  18182. 'this.DoIt = function (vI, vJ, vK, vL) {',
  18183. '};',
  18184. 'this.Arr = null;',
  18185. 'this.s = "";',
  18186. 'this.i = 0;',
  18187. 'this.v = undefined;',
  18188. '']),
  18189. LinesToStr([ // $mod.$main
  18190. '$mod.v = $mod.Arr[0];',
  18191. '$mod.v = $mod.Arr[1];',
  18192. '$mod.Arr[2] = $mod.s;',
  18193. '$mod.Arr[3] = $mod.s;',
  18194. '$mod.Arr[4] = $mod.i;',
  18195. '$mod.Arr[5] = $mod.Arr[6];',
  18196. '$mod.Arr[7] = $mod.Arr[8];',
  18197. 'var $with = $mod.Arr;',
  18198. '$with[9] = $with[10];',
  18199. '$mod.DoIt($mod.Arr[7], $mod.Arr[8], {',
  18200. ' a: 9,',
  18201. ' p: $mod.Arr,',
  18202. ' get: function () {',
  18203. ' return this.p[this.a];',
  18204. ' },',
  18205. ' set: function (v) {',
  18206. ' this.p[this.a] = v;',
  18207. ' }',
  18208. '}, {',
  18209. ' a: 10,',
  18210. ' p: $mod.Arr,',
  18211. ' get: function () {',
  18212. ' return this.p[this.a];',
  18213. ' },',
  18214. ' set: function (v) {',
  18215. ' this.p[this.a] = v;',
  18216. ' }',
  18217. '});',
  18218. 'var $with1 = $mod.Arr;',
  18219. '$mod.v = $with1[14];',
  18220. '$with1[15] = 16;',
  18221. '$mod.v = $mod.Arr[17];',
  18222. '$mod.Arr[18] = $mod.v;',
  18223. '']));
  18224. end;
  18225. procedure TTestModule.TestExternalClass_BracketAccessor_Call;
  18226. begin
  18227. StartProgram(false);
  18228. Add([
  18229. '{$modeswitch externalclass}',
  18230. 'type',
  18231. ' TJSArray = class external name ''Array2''',
  18232. ' function GetItems(Index: longint): jsvalue; external name ''[]'';',
  18233. ' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';',
  18234. ' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;',
  18235. ' end;',
  18236. ' TMyArr = class(TJSArray)',
  18237. ' procedure DoIt;',
  18238. ' end;',
  18239. 'procedure tmyarr.DoIt;',
  18240. 'begin',
  18241. ' Items[1]:=Items[2];',
  18242. ' SetItems(3,getItems(4));',
  18243. 'end;',
  18244. 'var',
  18245. ' Arr: tmyarr;',
  18246. ' s: string;',
  18247. ' i: longint;',
  18248. ' v: jsvalue;',
  18249. 'begin',
  18250. ' v:=arr[0];',
  18251. ' v:=arr.items[1];',
  18252. ' arr[2]:=s;',
  18253. ' arr.items[3]:=s;',
  18254. ' arr[4]:=i;',
  18255. ' arr[5]:=arr[6];',
  18256. ' arr.items[7]:=arr.items[8];',
  18257. ' with arr do items[9]:=items[10];',
  18258. ' with arr do begin',
  18259. ' v:=GetItems(14);',
  18260. ' setitems(15,16);',
  18261. ' end;',
  18262. '']);
  18263. ConvertProgram;
  18264. CheckSource('TestExternalClass_BracketAccessor_Call',
  18265. LinesToStr([ // statements
  18266. 'rtl.createClassExt(this, "TMyArr", Array2, "", function () {',
  18267. ' this.$init = function () {',
  18268. ' };',
  18269. ' this.$final = function () {',
  18270. ' };',
  18271. ' this.DoIt = function () {',
  18272. ' this[1] = this[2];',
  18273. ' this[3] = this[4];',
  18274. ' };',
  18275. '});',
  18276. 'this.Arr = null;',
  18277. 'this.s = "";',
  18278. 'this.i = 0;',
  18279. 'this.v = undefined;',
  18280. '']),
  18281. LinesToStr([ // $mod.$main
  18282. '$mod.v = $mod.Arr[0];',
  18283. '$mod.v = $mod.Arr[1];',
  18284. '$mod.Arr[2] = $mod.s;',
  18285. '$mod.Arr[3] = $mod.s;',
  18286. '$mod.Arr[4] = $mod.i;',
  18287. '$mod.Arr[5] = $mod.Arr[6];',
  18288. '$mod.Arr[7] = $mod.Arr[8];',
  18289. 'var $with = $mod.Arr;',
  18290. '$with[9] = $with[10];',
  18291. 'var $with1 = $mod.Arr;',
  18292. '$mod.v = $with1[14];',
  18293. '$with1[15] = 16;',
  18294. '']));
  18295. end;
  18296. procedure TTestModule.TestExternalClass_BracketAccessor_2ParamsFail;
  18297. begin
  18298. StartProgram(false);
  18299. Add('{$modeswitch externalclass}');
  18300. Add('type');
  18301. Add(' TJSArray = class external name ''Array2''');
  18302. Add(' function GetItems(Index1, Index2: longint): jsvalue; external name ''[]'';');
  18303. Add(' procedure SetItems(Index1, Index2: longint; Value: jsvalue); external name ''[]'';');
  18304. Add(' property Items[Index1, Index2: longint]: jsvalue read GetItems write SetItems; default;');
  18305. Add(' end;');
  18306. Add('begin');
  18307. SetExpectedPasResolverError(sBracketAccessorOfExternalClassMustHaveOneParameter,
  18308. nBracketAccessorOfExternalClassMustHaveOneParameter);
  18309. ConvertProgram;
  18310. end;
  18311. procedure TTestModule.TestExternalClass_BracketAccessor_ReadOnly;
  18312. begin
  18313. StartProgram(false);
  18314. Add('{$modeswitch externalclass}');
  18315. Add('type');
  18316. Add(' TJSArray = class external name ''Array2''');
  18317. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  18318. Add(' property Items[Index: longint]: jsvalue read GetItems; default;');
  18319. Add(' end;');
  18320. Add('procedure DoIt(vI: JSValue; const vJ: jsvalue);');
  18321. Add('begin end;');
  18322. Add('var');
  18323. Add(' Arr: tjsarray;');
  18324. Add(' v: jsvalue;');
  18325. Add('begin');
  18326. Add(' v:=arr[0];');
  18327. Add(' v:=arr.items[1];');
  18328. Add(' with arr do v:=items[2];');
  18329. Add(' doit(arr[3],arr[4]);');
  18330. ConvertProgram;
  18331. CheckSource('TestExternalClass_BracketAccessor_ReadOnly',
  18332. LinesToStr([ // statements
  18333. 'this.DoIt = function (vI, vJ) {',
  18334. '};',
  18335. 'this.Arr = null;',
  18336. 'this.v = undefined;',
  18337. '']),
  18338. LinesToStr([ // $mod.$main
  18339. '$mod.v = $mod.Arr[0];',
  18340. '$mod.v = $mod.Arr[1];',
  18341. 'var $with = $mod.Arr;',
  18342. '$mod.v = $with[2];',
  18343. '$mod.DoIt($mod.Arr[3], $mod.Arr[4]);',
  18344. '']));
  18345. end;
  18346. procedure TTestModule.TestExternalClass_BracketAccessor_WriteOnly;
  18347. begin
  18348. StartProgram(false);
  18349. Add('{$modeswitch externalclass}');
  18350. Add('type');
  18351. Add(' TJSArray = class external name ''Array2''');
  18352. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  18353. Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
  18354. Add(' end;');
  18355. Add('var');
  18356. Add(' Arr: tjsarray;');
  18357. Add(' s: string;');
  18358. Add(' i: longint;');
  18359. Add(' v: jsvalue;');
  18360. Add('begin');
  18361. Add(' arr[2]:=s;');
  18362. Add(' arr.items[3]:=s;');
  18363. Add(' arr[4]:=i;');
  18364. Add(' with arr do items[5]:=i;');
  18365. ConvertProgram;
  18366. CheckSource('TestExternalClass_BracketAccessor_WriteOnly',
  18367. LinesToStr([ // statements
  18368. 'this.Arr = null;',
  18369. 'this.s = "";',
  18370. 'this.i = 0;',
  18371. 'this.v = undefined;',
  18372. '']),
  18373. LinesToStr([ // $mod.$main
  18374. '$mod.Arr[2] = $mod.s;',
  18375. '$mod.Arr[3] = $mod.s;',
  18376. '$mod.Arr[4] = $mod.i;',
  18377. 'var $with = $mod.Arr;',
  18378. '$with[5] = $mod.i;',
  18379. '']));
  18380. end;
  18381. procedure TTestModule.TestExternalClass_BracketAccessor_MultiType;
  18382. begin
  18383. StartProgram(false);
  18384. Add('{$modeswitch externalclass}');
  18385. Add('type');
  18386. Add(' TJSArray = class external name ''Array2''');
  18387. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  18388. Add(' property Items[Index: longint]: jsvalue write SetItems; default;');
  18389. Add(' procedure SetNumbers(Index: longint; Value: longint); external name ''[]'';');
  18390. Add(' property Numbers[Index: longint]: longint write SetNumbers;');
  18391. Add(' end;');
  18392. Add('var');
  18393. Add(' Arr: tjsarray;');
  18394. Add(' s: string;');
  18395. Add(' i: longint;');
  18396. Add(' v: jsvalue;');
  18397. Add('begin');
  18398. Add(' arr[2]:=s;');
  18399. Add(' arr.items[3]:=s;');
  18400. Add(' arr.numbers[4]:=i;');
  18401. Add(' with arr do items[5]:=i;');
  18402. Add(' with arr do numbers[6]:=i;');
  18403. ConvertProgram;
  18404. CheckSource('TestExternalClass_BracketAccessor_MultiType',
  18405. LinesToStr([ // statements
  18406. 'this.Arr = null;',
  18407. 'this.s = "";',
  18408. 'this.i = 0;',
  18409. 'this.v = undefined;',
  18410. '']),
  18411. LinesToStr([ // $mod.$main
  18412. '$mod.Arr[2] = $mod.s;',
  18413. '$mod.Arr[3] = $mod.s;',
  18414. '$mod.Arr[4] = $mod.i;',
  18415. 'var $with = $mod.Arr;',
  18416. '$with[5] = $mod.i;',
  18417. 'var $with1 = $mod.Arr;',
  18418. '$with1[6] = $mod.i;',
  18419. '']));
  18420. end;
  18421. procedure TTestModule.TestExternalClass_BracketAccessor_Index;
  18422. begin
  18423. StartProgram(false);
  18424. Add('{$modeswitch externalclass}');
  18425. Add('type');
  18426. Add(' TJSArray = class external name ''Array2''');
  18427. Add(' function GetItems(Index: longint): jsvalue; external name ''[]'';');
  18428. Add(' procedure SetItems(Index: longint; Value: jsvalue); external name ''[]'';');
  18429. Add(' property Items[Index: longint]: jsvalue read GetItems write SetItems; default;');
  18430. Add(' end;');
  18431. Add('var');
  18432. Add(' Arr: tjsarray;');
  18433. Add(' i: longint;');
  18434. Add(' IntArr: array of longint;');
  18435. Add(' v: jsvalue;');
  18436. Add('begin');
  18437. Add(' v:=arr.items[i];');
  18438. Add(' arr[longint(v)]:=arr.items[intarr[0]];');
  18439. Add(' arr.items[intarr[1]]:=arr[IntArr[2]];');
  18440. ConvertProgram;
  18441. CheckSource('TestExternalClass_BracketAccessor_Index',
  18442. LinesToStr([ // statements
  18443. 'this.Arr = null;',
  18444. 'this.i = 0;',
  18445. 'this.IntArr = [];',
  18446. 'this.v = undefined;',
  18447. '']),
  18448. LinesToStr([ // $mod.$main
  18449. '$mod.v = $mod.Arr[$mod.i];',
  18450. '$mod.Arr[rtl.trunc($mod.v)] = $mod.Arr[$mod.IntArr[0]];',
  18451. '$mod.Arr[$mod.IntArr[1]] = $mod.Arr[$mod.IntArr[2]];',
  18452. '']));
  18453. end;
  18454. procedure TTestModule.TestExternalClass_ForInJSObject;
  18455. begin
  18456. StartProgram(false);
  18457. Add([
  18458. '{$modeswitch externalclass}',
  18459. 'type',
  18460. ' TJSObject = class external name ''Object''',
  18461. ' end;',
  18462. 'var',
  18463. ' o: TJSObject;',
  18464. ' key: string;',
  18465. 'begin',
  18466. ' for key in o do',
  18467. ' if key=''abc'' then ;',
  18468. '']);
  18469. ConvertProgram;
  18470. CheckSource('TestExternalClass_ForInJSObject',
  18471. LinesToStr([ // statements
  18472. 'this.o = null;',
  18473. 'this.key = "";',
  18474. '']),
  18475. LinesToStr([ // $mod.$main
  18476. 'for ($mod.key in $mod.o) if ($mod.key === "abc") ;',
  18477. '']));
  18478. end;
  18479. procedure TTestModule.TestExternalClass_ForInJSArray;
  18480. begin
  18481. StartProgram(false);
  18482. Add([
  18483. '{$modeswitch externalclass}',
  18484. 'type',
  18485. ' TJSInt8Array = class external name ''Int8Array''',
  18486. ' private',
  18487. ' flength: NativeInt external name ''length'';',
  18488. ' function getValue(Index: NativeInt): shortint; external name ''[]'';',
  18489. ' public',
  18490. ' property values[Index: NativeInt]: Shortint Read getValue; default;',
  18491. ' property Length: NativeInt read flength;',
  18492. ' end;',
  18493. 'var',
  18494. ' a: TJSInt8Array;',
  18495. ' value: shortint;',
  18496. 'begin',
  18497. ' for value in a do',
  18498. ' if value=3 then ;',
  18499. '']);
  18500. ConvertProgram;
  18501. CheckSource('TestExternalClass_ForInJSArray',
  18502. LinesToStr([ // statements
  18503. 'this.a = null;',
  18504. 'this.value = 0;',
  18505. '']),
  18506. LinesToStr([ // $mod.$main
  18507. 'for (var $in = $mod.a, $l = 0, $end = rtl.length($in) - 1; $l <= $end; $l++) {',
  18508. ' $mod.value = $in[$l];',
  18509. ' if ($mod.value === 3) ;',
  18510. '};',
  18511. '']));
  18512. end;
  18513. procedure TTestModule.TestExternalClass_IncompatibleArgDuplicateIdentifier;
  18514. begin
  18515. AddModuleWithIntfImplSrc('unit2.pas',
  18516. LinesToStr([
  18517. '{$modeswitch externalclass}',
  18518. 'type',
  18519. ' TJSBufferSource = class external name ''BufferSource''',
  18520. ' end;',
  18521. 'procedure DoIt(s: TJSBufferSource); external name ''DoIt'';',
  18522. '']),
  18523. '');
  18524. AddModuleWithIntfImplSrc('unit3.pas',
  18525. LinesToStr([
  18526. '{$modeswitch externalclass}',
  18527. 'type',
  18528. ' TJSBufferSource = class external name ''BufferSource''',
  18529. ' end;',
  18530. '']),
  18531. '');
  18532. StartUnit(true);
  18533. Add([
  18534. 'interface',
  18535. 'uses unit2, unit3;',
  18536. 'procedure DoSome(s: TJSBufferSource);',
  18537. 'implementation',
  18538. 'procedure DoSome(s: TJSBufferSource);',
  18539. 'begin',
  18540. ' DoIt(s);',
  18541. 'end;',
  18542. '']);
  18543. SetExpectedPasResolverError('Incompatible type arg no. 1: Got "unit3.TJSBufferSource", expected "unit2.TJSBufferSource"',
  18544. nIncompatibleTypeArgNo);
  18545. ConvertUnit;
  18546. end;
  18547. procedure TTestModule.TestClassInterface_Corba;
  18548. begin
  18549. StartProgram(false);
  18550. Add([
  18551. '{$interfaces corba}',
  18552. 'type',
  18553. ' IUnknown = interface;',
  18554. ' IUnknown = interface',
  18555. ' [''{00000000-0000-0000-C000-000000000046}'']',
  18556. ' end;',
  18557. ' IInterface = IUnknown;',
  18558. ' IBird = interface(IInterface)',
  18559. ' function GetSize: longint;',
  18560. ' procedure SetSize(i: longint);',
  18561. ' property Size: longint read GetSize write SetSize;',
  18562. ' procedure DoIt(i: longint);',
  18563. ' end;',
  18564. ' TObject = class',
  18565. ' end;',
  18566. ' TBird = class(TObject,IBird)',
  18567. ' function GetSize: longint; virtual; abstract;',
  18568. ' procedure SetSize(i: longint); virtual; abstract;',
  18569. ' procedure DoIt(i: longint); virtual; abstract;',
  18570. ' end;',
  18571. 'var',
  18572. ' BirdIntf: IBird;',
  18573. 'begin',
  18574. ' BirdIntf.Size:=BirdIntf.Size;',
  18575. '']);
  18576. ConvertProgram;
  18577. CheckSource('TestClassInterface_Corba',
  18578. LinesToStr([ // statements
  18579. 'rtl.createInterface(this, "IUnknown", "{00000000-0000-0000-C000-000000000046}", [], null);',
  18580. 'rtl.createInterface(this, "IBird", "{5BD1A53B-69BB-37EE-AF32-BEFB86D85B03}", ["GetSize", "SetSize", "DoIt"], this.IUnknown);',
  18581. 'rtl.createClass(this, "TObject", null, function () {',
  18582. ' this.$init = function () {',
  18583. ' };',
  18584. ' this.$final = function () {',
  18585. ' };',
  18586. '});',
  18587. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18588. ' rtl.addIntf(this, $mod.IBird);',
  18589. '});',
  18590. 'this.BirdIntf = null;',
  18591. '']),
  18592. LinesToStr([ // $mod.$main
  18593. ' $mod.BirdIntf.SetSize($mod.BirdIntf.GetSize());',
  18594. '']));
  18595. end;
  18596. procedure TTestModule.TestClassInterface_ProcExternalFail;
  18597. begin
  18598. StartProgram(false);
  18599. Add([
  18600. '{$interfaces corba}',
  18601. 'type',
  18602. ' IUnknown = interface',
  18603. ' procedure DoIt; external name ''foo'';',
  18604. ' end;',
  18605. 'begin']);
  18606. SetExpectedParserError(
  18607. 'Fields are not allowed in interface at token "Identifier external" in file test1.pp at line 6 column 21',
  18608. nParserNoFieldsAllowed);
  18609. ConvertProgram;
  18610. end;
  18611. procedure TTestModule.TestClassInterface_Overloads;
  18612. begin
  18613. StartProgram(false);
  18614. Add([
  18615. '{$interfaces corba}',
  18616. 'type',
  18617. ' integer = longint;',
  18618. ' IUnknown = interface',
  18619. ' procedure DoIt(i: integer);',
  18620. ' procedure DoIt(s: string);',
  18621. ' end;',
  18622. ' IBird = interface(IUnknown)',
  18623. ' procedure DoIt(b: boolean); overload;',
  18624. ' end;',
  18625. ' TObject = class',
  18626. ' end;',
  18627. ' TBird = class(TObject,IBird)',
  18628. ' procedure DoIt(o: TObject);',
  18629. ' procedure DoIt(s: string);',
  18630. ' procedure DoIt(i: integer);',
  18631. ' procedure DoIt(b: boolean);',
  18632. ' end;',
  18633. 'procedure TBird.DoIt(o: TObject); begin end;',
  18634. 'procedure TBird.DoIt(s: string); begin end;',
  18635. 'procedure TBird.DoIt(i: integer); begin end;',
  18636. 'procedure TBird.DoIt(b: boolean); begin end;',
  18637. 'var',
  18638. ' BirdIntf: IBird;',
  18639. 'begin',
  18640. ' BirdIntf.DoIt(3);',
  18641. ' BirdIntf.DoIt(''abc'');',
  18642. ' BirdIntf.DoIt(true);',
  18643. '']);
  18644. ConvertProgram;
  18645. CheckSource('TestClassInterface_Overloads',
  18646. LinesToStr([ // statements
  18647. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDC4-8A2AE2C59400}", ["DoIt", "DoIt$1"], null);',
  18648. 'rtl.createInterface(this, "IBird", "{8285DD5E-EA3E-396E-AE88-000B86AABF05}", ["DoIt$2"], this.IUnknown);',
  18649. 'rtl.createClass(this, "TObject", null, function () {',
  18650. ' this.$init = function () {',
  18651. ' };',
  18652. ' this.$final = function () {',
  18653. ' };',
  18654. '});',
  18655. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18656. ' this.DoIt = function (o) {',
  18657. ' };',
  18658. ' this.DoIt$1 = function (s) {',
  18659. ' };',
  18660. ' this.DoIt$2 = function (i) {',
  18661. ' };',
  18662. ' this.DoIt$3 = function (b) {',
  18663. ' };',
  18664. ' rtl.addIntf(this, $mod.IBird, {',
  18665. ' DoIt$2: "DoIt$3",',
  18666. ' DoIt: "DoIt$2"',
  18667. ' });',
  18668. '});',
  18669. 'this.BirdIntf = null;',
  18670. '']),
  18671. LinesToStr([ // $mod.$main
  18672. '$mod.BirdIntf.DoIt(3);',
  18673. '$mod.BirdIntf.DoIt$1("abc");',
  18674. '$mod.BirdIntf.DoIt$2(true);',
  18675. '']));
  18676. end;
  18677. procedure TTestModule.TestClassInterface_DuplicateGUIInIntfListFail;
  18678. begin
  18679. StartProgram(false);
  18680. Add([
  18681. '{$interfaces corba}',
  18682. 'type',
  18683. ' IBird = interface',
  18684. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  18685. ' end;',
  18686. ' IDog = interface',
  18687. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  18688. ' end;',
  18689. ' TObject = class(IBird,IDog)',
  18690. ' end;',
  18691. 'begin']);
  18692. SetExpectedPasResolverError('Duplicate GUID {4B3BA825-E0EC-4799-A19C-55F714A07959} in IDog and IBird',
  18693. nDuplicateGUIDXInYZ);
  18694. ConvertProgram;
  18695. end;
  18696. procedure TTestModule.TestClassInterface_DuplicateGUIInAncestorFail;
  18697. begin
  18698. StartProgram(false);
  18699. Add([
  18700. '{$interfaces corba}',
  18701. 'type',
  18702. ' IAnimal = interface',
  18703. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  18704. ' end;',
  18705. ' IBird = interface(IAnimal)',
  18706. ' end;',
  18707. ' IHawk = interface(IBird)',
  18708. ' [''{4B3BA825-E0EC-4799-A19C-55F714A07959}'']',
  18709. ' end;',
  18710. 'begin']);
  18711. SetExpectedPasResolverError('Duplicate GUID {4B3BA825-E0EC-4799-A19C-55F714A07959} in IHawk and IAnimal',
  18712. nDuplicateGUIDXInYZ);
  18713. ConvertProgram;
  18714. end;
  18715. procedure TTestModule.TestClassInterface_AncestorImpl;
  18716. begin
  18717. StartProgram(false);
  18718. Add([
  18719. '{$interfaces corba}',
  18720. 'type',
  18721. ' integer = longint;',
  18722. ' IUnknown = interface',
  18723. ' procedure DoIt(i: integer);',
  18724. ' end;',
  18725. ' IBird = interface',
  18726. ' procedure Fly(i: integer);',
  18727. ' end;',
  18728. ' TObject = class(IUnknown)',
  18729. ' procedure DoIt(i: integer);',
  18730. ' end;',
  18731. ' TBird = class(IBird)',
  18732. ' procedure Fly(i: integer);',
  18733. ' end;',
  18734. 'procedure TObject.DoIt(i: integer); begin end;',
  18735. 'procedure TBird.Fly(i: integer); begin end;',
  18736. 'begin',
  18737. '']);
  18738. ConvertProgram;
  18739. CheckSource('TestClassInterface_AncestorIntf',
  18740. LinesToStr([ // statements
  18741. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDC4-8A2800000000}", ["DoIt"], null);',
  18742. 'rtl.createInterface(this, "IBird", "{B92D5841-6264-3AE3-BF20-000000000000}", ["Fly"], null);',
  18743. 'rtl.createClass(this, "TObject", null, function () {',
  18744. ' this.$init = function () {',
  18745. ' };',
  18746. ' this.$final = function () {',
  18747. ' };',
  18748. ' this.DoIt = function (i) {',
  18749. ' };',
  18750. ' rtl.addIntf(this, $mod.IUnknown);',
  18751. '});',
  18752. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18753. ' this.Fly = function (i) {',
  18754. ' };',
  18755. ' rtl.addIntf(this, $mod.IBird);',
  18756. ' rtl.addIntf(this, $mod.IUnknown);',
  18757. '});',
  18758. '']),
  18759. LinesToStr([ // $mod.$main
  18760. '']));
  18761. end;
  18762. procedure TTestModule.TestClassInterface_ImplReintroduce;
  18763. begin
  18764. StartProgram(false);
  18765. Add([
  18766. '{$interfaces corba}',
  18767. 'type',
  18768. ' integer = longint;',
  18769. ' IBird = interface',
  18770. ' procedure DoIt(i: integer);',
  18771. ' end;',
  18772. ' TObject = class',
  18773. ' procedure DoIt(i: integer);',
  18774. ' end;',
  18775. ' TBird = class(IBird)',
  18776. ' procedure DoIt(i: integer); virtual; reintroduce;',
  18777. ' end;',
  18778. 'procedure TObject.DoIt(i: integer); begin end;',
  18779. 'procedure TBird.DoIt(i: integer); begin end;',
  18780. 'begin',
  18781. '']);
  18782. ConvertProgram;
  18783. CheckSource('TestClassInterface_ImplReintroduce',
  18784. LinesToStr([ // statements
  18785. 'rtl.createInterface(this, "IBird", "{B92D5841-6264-3AE2-8594-000000000000}", ["DoIt"], null);',
  18786. 'rtl.createClass(this, "TObject", null, function () {',
  18787. ' this.$init = function () {',
  18788. ' };',
  18789. ' this.$final = function () {',
  18790. ' };',
  18791. ' this.DoIt = function (i) {',
  18792. ' };',
  18793. '});',
  18794. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18795. ' this.DoIt$1 = function (i) {',
  18796. ' };',
  18797. ' rtl.addIntf(this, $mod.IBird, {',
  18798. ' DoIt: "DoIt$1"',
  18799. ' });',
  18800. '});',
  18801. '']),
  18802. LinesToStr([ // $mod.$main
  18803. '']));
  18804. end;
  18805. procedure TTestModule.TestClassInterface_MethodResolution;
  18806. begin
  18807. StartProgram(false);
  18808. Add([
  18809. '{$interfaces corba}',
  18810. 'type',
  18811. ' IUnknown = interface',
  18812. ' procedure Walk(i: longint);',
  18813. ' end;',
  18814. ' IBird = interface(IUnknown)',
  18815. ' procedure Walk(b: boolean); overload;',
  18816. ' procedure Fly(s: string);',
  18817. ' end;',
  18818. ' TObject = class',
  18819. ' end;',
  18820. ' TBird = class(TObject,IBird)',
  18821. ' procedure IBird.Fly = Move;',
  18822. ' procedure IBird.Walk = Hop;',
  18823. ' procedure Hop(i: longint);',
  18824. ' procedure Move(s: string);',
  18825. ' procedure Hop(b: boolean);',
  18826. ' end;',
  18827. 'procedure TBird.Move(s: string); begin end;',
  18828. 'procedure TBird.Hop(i: longint); begin end;',
  18829. 'procedure TBird.Hop(b: boolean); begin end;',
  18830. 'var',
  18831. ' BirdIntf: IBird;',
  18832. 'begin',
  18833. ' BirdIntf.Walk(3);',
  18834. ' BirdIntf.Walk(true);',
  18835. ' BirdIntf.Fly(''abc'');',
  18836. '']);
  18837. ConvertProgram;
  18838. CheckSource('TestClassInterface_MethodResolution',
  18839. LinesToStr([ // statements
  18840. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-BDD7-23D600000000}", ["Walk"], null);',
  18841. 'rtl.createInterface(this, "IBird", "{CF8A4986-80F6-396E-AE88-000B86AAE208}", ["Walk$1", "Fly"], this.IUnknown);',
  18842. 'rtl.createClass(this, "TObject", null, function () {',
  18843. ' this.$init = function () {',
  18844. ' };',
  18845. ' this.$final = function () {',
  18846. ' };',
  18847. '});',
  18848. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18849. ' this.Hop = function (i) {',
  18850. ' };',
  18851. ' this.Move = function (s) {',
  18852. ' };',
  18853. ' this.Hop$1 = function (b) {',
  18854. ' };',
  18855. ' rtl.addIntf(this, $mod.IBird, {',
  18856. ' Walk$1: "Hop$1",',
  18857. ' Fly: "Move",',
  18858. ' Walk: "Hop"',
  18859. ' });',
  18860. '});',
  18861. 'this.BirdIntf = null;',
  18862. '']),
  18863. LinesToStr([ // $mod.$main
  18864. '$mod.BirdIntf.Walk(3);',
  18865. '$mod.BirdIntf.Walk$1(true);',
  18866. '$mod.BirdIntf.Fly("abc");',
  18867. '']));
  18868. end;
  18869. procedure TTestModule.TestClassInterface_AncestorMoreInterfaces;
  18870. begin
  18871. StartProgram(false);
  18872. Add([
  18873. '{$interfaces com}',
  18874. 'type',
  18875. ' IUnknown = interface',
  18876. ' function _AddRef: longint;',
  18877. ' procedure Walk;',
  18878. ' end;',
  18879. ' IBird = interface end;',
  18880. ' IDog = interface end;',
  18881. ' TObject = class(IBird,IDog)',
  18882. ' function _AddRef: longint; virtual; abstract;',
  18883. ' procedure Walk; virtual; abstract;',
  18884. ' end;',
  18885. ' TBird = class(IUnknown)',
  18886. ' end;',
  18887. 'begin',
  18888. '']);
  18889. ConvertProgram;
  18890. CheckSource('TestClassInterface_COM_AncestorLess',
  18891. LinesToStr([ // statements
  18892. 'rtl.createInterface(this, "IUnknown", "{8F2D5841-758A-322B-BDDF-21CD521DD723}", ["_AddRef", "Walk"], null);',
  18893. 'rtl.createInterface(this, "IBird", "{CCE11D4C-6504-3AEE-AE88-000B86AAE675}", [], this.IUnknown);',
  18894. 'rtl.createInterface(this, "IDog", "{CCE11D4C-6504-3AEE-AE88-000B8E5FC675}", [], this.IUnknown);',
  18895. 'rtl.createClass(this, "TObject", null, function () {',
  18896. ' this.$init = function () {',
  18897. ' };',
  18898. ' this.$final = function () {',
  18899. ' };',
  18900. ' rtl.addIntf(this, $mod.IBird);',
  18901. ' rtl.addIntf(this, $mod.IDog);',
  18902. '});',
  18903. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18904. ' rtl.addIntf(this, $mod.IUnknown);',
  18905. ' rtl.addIntf(this, $mod.IBird);',
  18906. ' rtl.addIntf(this, $mod.IDog);',
  18907. '});',
  18908. '']),
  18909. LinesToStr([ // $mod.$main
  18910. '']));
  18911. end;
  18912. procedure TTestModule.TestClassInterface_MethodOverride;
  18913. begin
  18914. StartProgram(false);
  18915. Add([
  18916. '{$interfaces corba}',
  18917. 'type',
  18918. ' IUnknown = interface',
  18919. ' [''{D6D98E5B-8A10-4FEC-856A-7BFC847FE74B}'']',
  18920. ' procedure Go;',
  18921. ' end;',
  18922. ' TObject = class(IUnknown)',
  18923. ' procedure Go; virtual; abstract;',
  18924. ' end;',
  18925. ' TBird = class',
  18926. ' procedure Go; override;',
  18927. ' end;',
  18928. ' TCat = class(TObject)',
  18929. ' procedure Go; override;',
  18930. ' end;',
  18931. ' TDog = class(TObject, IUnknown)',
  18932. ' procedure Go; override;',
  18933. ' end;',
  18934. 'procedure TBird.Go; begin end;',
  18935. 'procedure TCat.Go; begin end;',
  18936. 'procedure TDog.Go; begin end;',
  18937. 'begin',
  18938. '']);
  18939. ConvertProgram;
  18940. CheckSource('TestClassInterface_MethodOverride',
  18941. LinesToStr([ // statements
  18942. 'rtl.createInterface(this, "IUnknown", "{D6D98E5B-8A10-4FEC-856A-7BFC847FE74B}", ["Go"], null);',
  18943. 'rtl.createClass(this, "TObject", null, function () {',
  18944. ' this.$init = function () {',
  18945. ' };',
  18946. ' this.$final = function () {',
  18947. ' };',
  18948. ' rtl.addIntf(this, $mod.IUnknown);',
  18949. '});',
  18950. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  18951. ' this.Go = function () {',
  18952. ' };',
  18953. ' rtl.addIntf(this, $mod.IUnknown);',
  18954. '});',
  18955. 'rtl.createClass(this, "TCat", this.TObject, function () {',
  18956. ' this.Go = function () {',
  18957. ' };',
  18958. ' rtl.addIntf(this, $mod.IUnknown);',
  18959. '});',
  18960. 'rtl.createClass(this, "TDog", this.TObject, function () {',
  18961. ' this.Go = function () {',
  18962. ' };',
  18963. ' rtl.addIntf(this, $mod.IUnknown);',
  18964. '});',
  18965. '']),
  18966. LinesToStr([ // $mod.$main
  18967. '']));
  18968. end;
  18969. procedure TTestModule.TestClassInterface_Corba_Delegation;
  18970. begin
  18971. StartProgram(false);
  18972. Add([
  18973. '{$interfaces corba}',
  18974. 'type',
  18975. ' IUnknown = interface',
  18976. ' end;',
  18977. ' IBird = interface(IUnknown)',
  18978. ' procedure Fly(s: string);',
  18979. ' end;',
  18980. ' IEagle = interface(IBird)',
  18981. ' end;',
  18982. ' IDove = interface(IBird)',
  18983. ' end;',
  18984. ' ISwallow = interface(IBird)',
  18985. ' end;',
  18986. ' TObject = class',
  18987. ' end;',
  18988. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  18989. ' procedure Fly(s: string); virtual; abstract;',
  18990. ' end;',
  18991. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  18992. ' FBirdIntf: IBird;',
  18993. ' property BirdIntf: IBird read FBirdIntf implements IBird;',
  18994. ' function GetEagleIntf: IEagle; virtual; abstract;',
  18995. ' property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  18996. ' FDoveObj: TBird;',
  18997. ' property DoveObj: TBird read FDoveObj implements IDove;',
  18998. ' function GetSwallowObj: TBird; virtual; abstract;',
  18999. ' property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  19000. ' end;',
  19001. 'begin',
  19002. '']);
  19003. ConvertProgram;
  19004. CheckSource('TestClassInterface_Corba_Delegation',
  19005. LinesToStr([ // statements
  19006. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19007. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  19008. 'rtl.createInterface(this, "IEagle", "{489289DE-FDE2-34A6-8288-39119022B1B4}", [], this.IBird);',
  19009. 'rtl.createInterface(this, "IDove", "{489289DE-FDE2-34A6-8288-39118EF16074}", [], this.IBird);',
  19010. 'rtl.createInterface(this, "ISwallow", "{B89289DE-FDE2-34A6-8288-3911CBDCB359}", [], this.IBird);',
  19011. 'rtl.createClass(this, "TObject", null, function () {',
  19012. ' this.$init = function () {',
  19013. ' };',
  19014. ' this.$final = function () {',
  19015. ' };',
  19016. '});',
  19017. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  19018. ' rtl.addIntf(this, $mod.IBird);',
  19019. ' rtl.addIntf(this, $mod.IEagle);',
  19020. ' rtl.addIntf(this, $mod.IDove);',
  19021. ' rtl.addIntf(this, $mod.ISwallow);',
  19022. '});',
  19023. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  19024. ' this.$init = function () {',
  19025. ' $mod.TObject.$init.call(this);',
  19026. ' this.FBirdIntf = null;',
  19027. ' this.FDoveObj = null;',
  19028. ' };',
  19029. ' this.$final = function () {',
  19030. ' this.FBirdIntf = undefined;',
  19031. ' this.FDoveObj = undefined;',
  19032. ' $mod.TObject.$final.call(this);',
  19033. ' };',
  19034. ' this.$intfmaps = {',
  19035. ' "{478D080B-C0F6-396E-AE88-000B87785B07}": function () {',
  19036. ' return this.FBirdIntf;',
  19037. ' },',
  19038. ' "{489289DE-FDE2-34A6-8288-39119022B1B4}": function () {',
  19039. ' return this.GetEagleIntf();',
  19040. ' },',
  19041. ' "{489289DE-FDE2-34A6-8288-39118EF16074}": function () {',
  19042. ' return rtl.getIntfT(this.FDoveObj, $mod.IDove);',
  19043. ' },',
  19044. ' "{B89289DE-FDE2-34A6-8288-3911CBDCB359}": function () {',
  19045. ' return rtl.getIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  19046. ' }',
  19047. ' };',
  19048. '});',
  19049. '']),
  19050. LinesToStr([ // $mod.$main
  19051. '']));
  19052. end;
  19053. procedure TTestModule.TestClassInterface_Corba_DelegationStatic;
  19054. begin
  19055. StartProgram(false);
  19056. Add([
  19057. '{$interfaces corba}',
  19058. 'type',
  19059. ' IUnknown = interface',
  19060. ' end;',
  19061. ' IBird = interface(IUnknown)',
  19062. ' procedure Fly(s: string);',
  19063. ' end;',
  19064. ' IEagle = interface(IBird)',
  19065. ' end;',
  19066. ' IDove = interface(IBird)',
  19067. ' end;',
  19068. ' ISwallow = interface(IBird)',
  19069. ' end;',
  19070. ' TObject = class',
  19071. ' end;',
  19072. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  19073. ' procedure Fly(s: string); virtual; abstract;',
  19074. ' end;',
  19075. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  19076. ' private',
  19077. ' class var FBirdIntf: IBird;',
  19078. ' class var FDoveObj: TBird;',
  19079. ' class function GetEagleIntf: IEagle; virtual; abstract;',
  19080. ' class function GetSwallowObj: TBird; virtual; abstract;',
  19081. ' protected',
  19082. ' class property BirdIntf: IBird read FBirdIntf implements IBird;',
  19083. ' class property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  19084. ' class property DoveObj: TBird read FDoveObj implements IDove;',
  19085. ' class property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  19086. ' end;',
  19087. 'begin',
  19088. '']);
  19089. ConvertProgram;
  19090. CheckSource('TestClassInterface_Corba_DelegationStatic',
  19091. LinesToStr([ // statements
  19092. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19093. 'rtl.createInterface(this, "IBird", "{478D080B-C0F6-396E-AE88-000B87785B07}", ["Fly"], this.IUnknown);',
  19094. 'rtl.createInterface(this, "IEagle", "{489289DE-FDE2-34A6-8288-39119022B1B4}", [], this.IBird);',
  19095. 'rtl.createInterface(this, "IDove", "{489289DE-FDE2-34A6-8288-39118EF16074}", [], this.IBird);',
  19096. 'rtl.createInterface(this, "ISwallow", "{B89289DE-FDE2-34A6-8288-3911CBDCB359}", [], this.IBird);',
  19097. 'rtl.createClass(this, "TObject", null, function () {',
  19098. ' this.$init = function () {',
  19099. ' };',
  19100. ' this.$final = function () {',
  19101. ' };',
  19102. '});',
  19103. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  19104. ' rtl.addIntf(this, $mod.IBird);',
  19105. ' rtl.addIntf(this, $mod.IEagle);',
  19106. ' rtl.addIntf(this, $mod.IDove);',
  19107. ' rtl.addIntf(this, $mod.ISwallow);',
  19108. '});',
  19109. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  19110. ' this.FBirdIntf = null;',
  19111. ' this.FDoveObj = null;',
  19112. ' this.$intfmaps = {',
  19113. ' "{478D080B-C0F6-396E-AE88-000B87785B07}": function () {',
  19114. ' return this.FBirdIntf;',
  19115. ' },',
  19116. ' "{489289DE-FDE2-34A6-8288-39119022B1B4}": function () {',
  19117. ' return this.GetEagleIntf();',
  19118. ' },',
  19119. ' "{489289DE-FDE2-34A6-8288-39118EF16074}": function () {',
  19120. ' return rtl.getIntfT(this.FDoveObj, $mod.IDove);',
  19121. ' },',
  19122. ' "{B89289DE-FDE2-34A6-8288-3911CBDCB359}": function () {',
  19123. ' return rtl.getIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  19124. ' }',
  19125. ' };',
  19126. '});',
  19127. '']),
  19128. LinesToStr([ // $mod.$main
  19129. '']));
  19130. end;
  19131. procedure TTestModule.TestClassInterface_Corba_Operators;
  19132. begin
  19133. StartProgram(false);
  19134. Add([
  19135. '{$interfaces corba}',
  19136. 'type',
  19137. ' IUnknown = interface',
  19138. ' end;',
  19139. ' IBird = interface(IUnknown)',
  19140. ' function GetItems(Index: longint): longint;',
  19141. ' procedure SetItems(Index: longint; Value: longint);',
  19142. ' property Items[Index: longint]: longint read GetItems write SetItems; default;',
  19143. ' end;',
  19144. ' TObject = class',
  19145. ' end;',
  19146. ' TBird = class(TObject,IBird)',
  19147. ' function GetItems(Index: longint): longint; virtual; abstract;',
  19148. ' procedure SetItems(Index: longint; Value: longint); virtual; abstract;',
  19149. ' end;',
  19150. 'var',
  19151. ' IntfVar: IBird = nil;',
  19152. ' IntfVar2: IBird;',
  19153. ' ObjVar: TBird;',
  19154. ' v: JSValue;',
  19155. 'begin',
  19156. ' IntfVar:=nil;',
  19157. ' IntfVar[3]:=IntfVar[4];',
  19158. ' if Assigned(IntfVar) then ;',
  19159. ' IntfVar:=IntfVar2;',
  19160. ' IntfVar:=ObjVar;',
  19161. ' if IntfVar=IntfVar2 then ;',
  19162. ' if IntfVar<>IntfVar2 then ;',
  19163. ' if IntfVar is IBird then ;',
  19164. ' if IntfVar is TBird then ;',
  19165. ' if ObjVar is IBird then ;',
  19166. ' IntfVar:=IntfVar2 as IBird;',
  19167. ' ObjVar:=IntfVar2 as TBird;',
  19168. ' IntfVar:=ObjVar as IBird;',
  19169. ' IntfVar:=IBird(IntfVar2);',
  19170. ' ObjVar:=TBird(IntfVar);',
  19171. ' IntfVar:=IBird(ObjVar);',
  19172. ' v:=IntfVar;',
  19173. ' IntfVar:=IBird(v);',
  19174. ' if v is IBird then ;',
  19175. ' v:=JSValue(IntfVar);',
  19176. ' v:=IBird;',
  19177. '']);
  19178. ConvertProgram;
  19179. CheckSource('TestClassInterface_Corba_Operators',
  19180. LinesToStr([ // statements
  19181. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19182. 'rtl.createInterface(this, "IBird", "{D53FED90-DE59-3202-B1AE-000B87785B08}", ["GetItems", "SetItems"], this.IUnknown);',
  19183. 'rtl.createClass(this, "TObject", null, function () {',
  19184. ' this.$init = function () {',
  19185. ' };',
  19186. ' this.$final = function () {',
  19187. ' };',
  19188. '});',
  19189. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  19190. ' rtl.addIntf(this, $mod.IBird);',
  19191. '});',
  19192. 'this.IntfVar = null;',
  19193. 'this.IntfVar2 = null;',
  19194. 'this.ObjVar = null;',
  19195. 'this.v = undefined;',
  19196. '']),
  19197. LinesToStr([ // $mod.$main
  19198. '$mod.IntfVar = null;',
  19199. '$mod.IntfVar.SetItems(3, $mod.IntfVar.GetItems(4));',
  19200. 'if ($mod.IntfVar != null) ;',
  19201. '$mod.IntfVar = $mod.IntfVar2;',
  19202. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar,$mod.IBird);',
  19203. 'if ($mod.IntfVar === $mod.IntfVar2) ;',
  19204. 'if ($mod.IntfVar !== $mod.IntfVar2) ;',
  19205. 'if ($mod.IBird.isPrototypeOf($mod.IntfVar)) ;',
  19206. 'if (rtl.intfIsClass($mod.IntfVar, $mod.TBird)) ;',
  19207. 'if (rtl.getIntfT($mod.ObjVar, $mod.IBird) !== null) ;',
  19208. '$mod.IntfVar = rtl.as($mod.IntfVar2, $mod.IBird);',
  19209. '$mod.ObjVar = rtl.intfAsClass($mod.IntfVar2, $mod.TBird);',
  19210. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar, $mod.IBird);',
  19211. '$mod.IntfVar = $mod.IntfVar2;',
  19212. '$mod.ObjVar = rtl.intfToClass($mod.IntfVar, $mod.TBird);',
  19213. '$mod.IntfVar = rtl.getIntfT($mod.ObjVar, $mod.IBird);',
  19214. '$mod.v = $mod.IntfVar;',
  19215. '$mod.IntfVar = rtl.getObject($mod.v);',
  19216. 'if (rtl.isExt($mod.v, $mod.IBird, 1)) ;',
  19217. '$mod.v = $mod.IntfVar;',
  19218. '$mod.v = $mod.IBird;',
  19219. '']));
  19220. end;
  19221. procedure TTestModule.TestClassInterface_Corba_Args;
  19222. begin
  19223. StartProgram(false);
  19224. Add([
  19225. '{$interfaces corba}',
  19226. 'type',
  19227. ' IUnknown = interface',
  19228. ' end;',
  19229. ' IBird = interface(IUnknown)',
  19230. ' end;',
  19231. ' TObject = class',
  19232. ' end;',
  19233. ' TBird = class(TObject,IBird)',
  19234. ' end;',
  19235. 'procedure DoIt(var u; i: IBird; const j: IBird);',
  19236. 'begin',
  19237. ' DoIt(i,i,i);',
  19238. 'end;',
  19239. 'procedure Change(var i: IBird; out j: IBird);',
  19240. 'begin',
  19241. ' DoIt(i,i,i);',
  19242. ' Change(i,i);',
  19243. 'end;',
  19244. 'var',
  19245. ' i: IBird;',
  19246. ' o: TBird;',
  19247. 'begin',
  19248. ' DoIt(i,i,i);',
  19249. ' Change(i,i);',
  19250. ' DoIt(o,o,o);',
  19251. '']);
  19252. ConvertProgram;
  19253. CheckSource('TestClassInterface_Corba_Args',
  19254. LinesToStr([ // statements
  19255. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19256. 'rtl.createInterface(this, "IBird", "{4B0D080B-C0F6-396E-AE88-000B87785074}", [], this.IUnknown);',
  19257. 'rtl.createClass(this, "TObject", null, function () {',
  19258. ' this.$init = function () {',
  19259. ' };',
  19260. ' this.$final = function () {',
  19261. ' };',
  19262. '});',
  19263. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  19264. ' rtl.addIntf(this, $mod.IBird);',
  19265. '});',
  19266. 'this.DoIt = function (u, i, j) {',
  19267. ' $mod.DoIt({',
  19268. ' get: function () {',
  19269. ' return i;',
  19270. ' },',
  19271. ' set: function (v) {',
  19272. ' i = v;',
  19273. ' }',
  19274. ' }, i, i);',
  19275. '};',
  19276. 'this.Change = function (i, j) {',
  19277. ' $mod.DoIt(i, i.get(), i.get());',
  19278. ' $mod.Change(i, i);',
  19279. '};',
  19280. 'this.i = null;',
  19281. 'this.o = null;',
  19282. '']),
  19283. LinesToStr([ // $mod.$main
  19284. '$mod.DoIt({',
  19285. ' p: $mod,',
  19286. ' get: function () {',
  19287. ' return this.p.i;',
  19288. ' },',
  19289. ' set: function (v) {',
  19290. ' this.p.i = v;',
  19291. ' }',
  19292. '}, $mod.i, $mod.i);',
  19293. '$mod.Change({',
  19294. ' p: $mod,',
  19295. ' get: function () {',
  19296. ' return this.p.i;',
  19297. ' },',
  19298. ' set: function (v) {',
  19299. ' this.p.i = v;',
  19300. ' }',
  19301. '}, {',
  19302. ' p: $mod,',
  19303. ' get: function () {',
  19304. ' return this.p.i;',
  19305. ' },',
  19306. ' set: function (v) {',
  19307. ' this.p.i = v;',
  19308. ' }',
  19309. '});',
  19310. '$mod.DoIt({',
  19311. ' p: $mod,',
  19312. ' get: function () {',
  19313. ' return this.p.o;',
  19314. ' },',
  19315. ' set: function (v) {',
  19316. ' this.p.o = v;',
  19317. ' }',
  19318. '}, rtl.getIntfT($mod.o, $mod.IBird), rtl.getIntfT($mod.o, $mod.IBird));',
  19319. '']));
  19320. end;
  19321. procedure TTestModule.TestClassInterface_Corba_ForIn;
  19322. begin
  19323. StartProgram(false);
  19324. Add([
  19325. '{$interfaces corba}',
  19326. 'type',
  19327. ' IUnknown = interface end;',
  19328. ' TObject = class',
  19329. ' Id: longint;',
  19330. ' end;',
  19331. ' IEnumerator = interface(IUnknown)',
  19332. ' function GetCurrent: TObject;',
  19333. ' function MoveNext: Boolean;',
  19334. ' property Current: TObject read GetCurrent;',
  19335. ' end;',
  19336. ' IEnumerable = interface(IUnknown)',
  19337. ' function GetEnumerator: IEnumerator;',
  19338. ' end;',
  19339. 'var',
  19340. ' o: TObject;',
  19341. ' i: IEnumerable;',
  19342. 'begin',
  19343. ' for o in i do o.Id:=3;',
  19344. '']);
  19345. ConvertProgram;
  19346. CheckSource('TestClassInterface_Corba_ForIn',
  19347. LinesToStr([ // statements
  19348. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  19349. 'rtl.createClass(this, "TObject", null, function () {',
  19350. ' this.$init = function () {',
  19351. ' this.Id = 0;',
  19352. ' };',
  19353. ' this.$final = function () {',
  19354. ' };',
  19355. '});',
  19356. 'rtl.createInterface(this, "IEnumerator", "{95D7745D-ED61-3F13-BBE4-07708161999E}", ["GetCurrent", "MoveNext"], this.IUnknown);',
  19357. 'rtl.createInterface(this, "IEnumerable", "{8CC9D45D-ED7D-3B73-96B6-290B931BB19E}", ["GetEnumerator"], this.IUnknown);',
  19358. 'this.o = null;',
  19359. 'this.i = null;',
  19360. '']),
  19361. LinesToStr([ // $mod.$main
  19362. 'var $in = $mod.i.GetEnumerator();',
  19363. 'while ($in.MoveNext()) {',
  19364. ' $mod.o = $in.GetCurrent();',
  19365. ' $mod.o.Id = 3;',
  19366. '};',
  19367. '']));
  19368. end;
  19369. procedure TTestModule.TestClassInterface_COM_AssignVar;
  19370. begin
  19371. StartProgram(false);
  19372. Add([
  19373. '{$interfaces com}',
  19374. 'type',
  19375. ' IUnknown = interface',
  19376. ' function _AddRef: longint;',
  19377. ' function _Release: longint;',
  19378. ' end;',
  19379. ' TObject = class(IUnknown)',
  19380. ' function _AddRef: longint; virtual; abstract;',
  19381. ' function _Release: longint; virtual; abstract;',
  19382. ' end;',
  19383. 'var',
  19384. ' i: IUnknown;',
  19385. 'procedure DoGlobal(o: TObject);',
  19386. 'begin',
  19387. ' i:=nil;',
  19388. ' i:=o;',
  19389. ' i:=i;',
  19390. 'end;',
  19391. 'procedure DoLocal(o: TObject);',
  19392. 'const k: IUnknown = nil;',
  19393. 'var j: IUnknown;',
  19394. 'begin',
  19395. ' k:=o;',
  19396. ' k:=i;',
  19397. ' j:=o;',
  19398. ' j:=i;',
  19399. 'end;',
  19400. 'var o: TObject;',
  19401. 'begin',
  19402. ' i:=nil;',
  19403. ' i:=o;',
  19404. '']);
  19405. ConvertProgram;
  19406. CheckSource('TestClassInterface_COM_AssignVar',
  19407. LinesToStr([ // statements
  19408. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19409. 'rtl.createClass(this, "TObject", null, function () {',
  19410. ' this.$init = function () {',
  19411. ' };',
  19412. ' this.$final = function () {',
  19413. ' };',
  19414. ' rtl.addIntf(this, $mod.IUnknown);',
  19415. '});',
  19416. 'this.i = null;',
  19417. 'this.DoGlobal = function (o) {',
  19418. ' rtl.setIntfP($mod, "i", null);',
  19419. ' rtl.setIntfP($mod, "i", rtl.queryIntfT(o, $mod.IUnknown), true);',
  19420. ' rtl.setIntfP($mod, "i", $mod.i);',
  19421. '};',
  19422. 'var k = null;',
  19423. 'this.DoLocal = function (o) {',
  19424. ' var j = null;',
  19425. ' try{',
  19426. ' k = rtl.setIntfL(k, rtl.queryIntfT(o, $mod.IUnknown), true);',
  19427. ' k = rtl.setIntfL(k, $mod.i);',
  19428. ' j = rtl.setIntfL(j, rtl.queryIntfT(o, $mod.IUnknown), true);',
  19429. ' j = rtl.setIntfL(j, $mod.i);',
  19430. ' }finally{',
  19431. ' rtl._Release(j);',
  19432. ' };',
  19433. '};',
  19434. 'this.o = null;',
  19435. '']),
  19436. LinesToStr([ // $mod.$main
  19437. 'rtl.setIntfP($mod, "i", null);',
  19438. 'rtl.setIntfP($mod, "i", rtl.queryIntfT($mod.o, $mod.IUnknown), true);',
  19439. '']));
  19440. end;
  19441. procedure TTestModule.TestClassInterface_COM_AssignArg;
  19442. begin
  19443. StartProgram(false);
  19444. Add([
  19445. '{$interfaces com}',
  19446. 'type',
  19447. ' IUnknown = interface',
  19448. ' function _AddRef: longint;',
  19449. ' function _Release: longint;',
  19450. ' end;',
  19451. ' TObject = class(IUnknown)',
  19452. ' function _AddRef: longint; virtual; abstract;',
  19453. ' function _Release: longint; virtual; abstract;',
  19454. ' end;',
  19455. 'procedure DoDefault(i, j: IUnknown);',
  19456. 'begin',
  19457. ' i:=nil;',
  19458. ' i:=j;',
  19459. 'end;',
  19460. 'begin',
  19461. '']);
  19462. ConvertProgram;
  19463. CheckSource('TestClassInterface_COM_AssignArg',
  19464. LinesToStr([ // statements
  19465. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19466. 'rtl.createClass(this, "TObject", null, function () {',
  19467. ' this.$init = function () {',
  19468. ' };',
  19469. ' this.$final = function () {',
  19470. ' };',
  19471. ' rtl.addIntf(this, $mod.IUnknown);',
  19472. '});',
  19473. 'this.DoDefault = function (i, j) {',
  19474. ' rtl._AddRef(i);',
  19475. ' try {',
  19476. ' i = rtl.setIntfL(i, null);',
  19477. ' i = rtl.setIntfL(i, j);',
  19478. ' } finally {',
  19479. ' rtl._Release(i);',
  19480. ' };',
  19481. '};',
  19482. '']),
  19483. LinesToStr([ // $mod.$main
  19484. '']));
  19485. end;
  19486. procedure TTestModule.TestClassInterface_COM_FunctionResult;
  19487. begin
  19488. StartProgram(false);
  19489. Add([
  19490. '{$interfaces com}',
  19491. 'type',
  19492. ' IUnknown = interface',
  19493. ' function _AddRef: longint;',
  19494. ' function _Release: longint;',
  19495. ' end;',
  19496. ' TObject = class(IUnknown)',
  19497. ' function _AddRef: longint; virtual; abstract;',
  19498. ' function _Release: longint; virtual; abstract;',
  19499. ' end;',
  19500. 'function DoDefault(i: IUnknown): IUnknown;',
  19501. 'begin',
  19502. ' Result:=i;',
  19503. ' if Result<>nil then exit;',
  19504. 'end;',
  19505. 'begin',
  19506. '']);
  19507. ConvertProgram;
  19508. CheckSource('TestClassInterface_COM_FunctionResult',
  19509. LinesToStr([ // statements
  19510. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19511. 'rtl.createClass(this, "TObject", null, function () {',
  19512. ' this.$init = function () {',
  19513. ' };',
  19514. ' this.$final = function () {',
  19515. ' };',
  19516. ' rtl.addIntf(this, $mod.IUnknown);',
  19517. '});',
  19518. 'this.DoDefault = function (i) {',
  19519. ' var Result = null;',
  19520. ' var $ok = false;',
  19521. ' try {',
  19522. ' Result = rtl.setIntfL(Result, i);',
  19523. ' if(Result !== null){',
  19524. ' $ok = true;',
  19525. ' return Result;',
  19526. ' };',
  19527. ' $ok = true;',
  19528. ' } finally {',
  19529. ' if(!$ok) rtl._Release(Result);',
  19530. ' };',
  19531. ' return Result;',
  19532. '};',
  19533. '']),
  19534. LinesToStr([ // $mod.$main
  19535. '']));
  19536. end;
  19537. procedure TTestModule.TestClassInterface_COM_InheritedFuncResult;
  19538. begin
  19539. StartProgram(false);
  19540. Add([
  19541. '{$interfaces com}',
  19542. 'type',
  19543. ' IUnknown = interface',
  19544. ' function _AddRef: longint;',
  19545. ' function _Release: longint;',
  19546. ' end;',
  19547. ' TObject = class(IUnknown)',
  19548. ' function _AddRef: longint; virtual; abstract;',
  19549. ' function _Release: longint; virtual; abstract;',
  19550. ' function GetIntf: IUnknown; virtual;',
  19551. ' end;',
  19552. ' TMouse = class',
  19553. ' function GetIntf: IUnknown; override;',
  19554. ' end;',
  19555. 'function TObject.GetIntf: IUnknown; begin end;',
  19556. 'function TMouse.GetIntf: IUnknown;',
  19557. 'var i: IUnknown;',
  19558. 'begin',
  19559. ' inherited;',
  19560. ' inherited GetIntf;',
  19561. ' inherited GetIntf();',
  19562. ' Result:=inherited GetIntf;',
  19563. ' Result:=inherited GetIntf();',
  19564. ' i:=inherited GetIntf;',
  19565. ' i:=inherited GetIntf();',
  19566. 'end;',
  19567. 'begin',
  19568. '']);
  19569. ConvertProgram;
  19570. CheckSource('TestClassInterface_COM_InheritedFuncResult',
  19571. LinesToStr([ // statements
  19572. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19573. 'rtl.createClass(this, "TObject", null, function () {',
  19574. ' this.$init = function () {',
  19575. ' };',
  19576. ' this.$final = function () {',
  19577. ' };',
  19578. ' this.GetIntf = function () {',
  19579. ' var Result = null;',
  19580. ' return Result;',
  19581. ' };',
  19582. ' rtl.addIntf(this, $mod.IUnknown);',
  19583. '});',
  19584. 'rtl.createClass(this, "TMouse", this.TObject, function () {',
  19585. ' this.GetIntf = function () {',
  19586. ' var Result = null;',
  19587. ' var i = null;',
  19588. ' var $ir = rtl.createIntfRefs();',
  19589. ' var $ok = false;',
  19590. ' try {',
  19591. ' $ir.ref(1, $mod.TObject.GetIntf.call(this));',
  19592. ' $ir.ref(2, $mod.TObject.GetIntf.call(this));',
  19593. ' $ir.ref(3, $mod.TObject.GetIntf.call(this));',
  19594. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  19595. ' Result = rtl.setIntfL(Result, $mod.TObject.GetIntf.call(this), true);',
  19596. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  19597. ' i = rtl.setIntfL(i, $mod.TObject.GetIntf.call(this), true);',
  19598. ' $ok = true;',
  19599. ' } finally {',
  19600. ' $ir.free();',
  19601. ' rtl._Release(i);',
  19602. ' if (!$ok) rtl._Release(Result);',
  19603. ' };',
  19604. ' return Result;',
  19605. ' };',
  19606. ' rtl.addIntf(this, $mod.IUnknown);',
  19607. '});',
  19608. '']),
  19609. LinesToStr([ // $mod.$main
  19610. '']));
  19611. end;
  19612. procedure TTestModule.TestClassInterface_COM_IsAsTypeCasts;
  19613. begin
  19614. StartProgram(false);
  19615. Add([
  19616. '{$interfaces com}',
  19617. 'type',
  19618. ' IUnknown = interface',
  19619. ' function _AddRef: longint;',
  19620. ' function _Release: longint;',
  19621. ' end;',
  19622. ' TObject = class(IUnknown)',
  19623. ' function _AddRef: longint; virtual; abstract;',
  19624. ' function _Release: longint; virtual; abstract;',
  19625. ' end;',
  19626. 'procedure DoDefault(i, j: IUnknown; o: TObject);',
  19627. 'begin',
  19628. ' if i is IUnknown then ;',
  19629. ' if o is IUnknown then ;',
  19630. ' if i is TObject then ;',
  19631. ' i:=j as IUnknown;',
  19632. ' i:=o as IUnknown;',
  19633. ' o:=j as TObject;',
  19634. ' i:=IUnknown(j);',
  19635. ' i:=IUnknown(o);',
  19636. ' o:=TObject(i);',
  19637. 'end;',
  19638. 'begin',
  19639. '']);
  19640. ConvertProgram;
  19641. CheckSource('TestClassInterface_COM_IsAsTypeCasts',
  19642. LinesToStr([ // statements
  19643. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19644. 'rtl.createClass(this, "TObject", null, function () {',
  19645. ' this.$init = function () {',
  19646. ' };',
  19647. ' this.$final = function () {',
  19648. ' };',
  19649. ' rtl.addIntf(this, $mod.IUnknown);',
  19650. '});',
  19651. 'this.DoDefault = function (i, j, o) {',
  19652. ' rtl._AddRef(i);',
  19653. ' try {',
  19654. ' if (rtl.intfIsIntfT(i, $mod.IUnknown)) ;',
  19655. ' if (rtl.queryIntfIsT(o, $mod.IUnknown)) ;',
  19656. ' if (rtl.intfIsClass(i, $mod.TObject)) ;',
  19657. ' i = rtl.setIntfL(i, rtl.intfAsIntfT(j, $mod.IUnknown));',
  19658. ' i = rtl.setIntfL(i, rtl.queryIntfT(o, $mod.IUnknown), true);',
  19659. ' o = rtl.intfAsClass(j, $mod.TObject);',
  19660. ' i = rtl.setIntfL(i, j);',
  19661. ' i = rtl.setIntfL(i, rtl.queryIntfT(o, $mod.IUnknown), true);',
  19662. ' o = rtl.intfToClass(i, $mod.TObject);',
  19663. ' } finally {',
  19664. ' rtl._Release(i);',
  19665. ' };',
  19666. '};',
  19667. '']),
  19668. LinesToStr([ // $mod.$main
  19669. '']));
  19670. end;
  19671. procedure TTestModule.TestClassInterface_COM_PassAsArg;
  19672. begin
  19673. StartProgram(false);
  19674. Add([
  19675. '{$interfaces com}',
  19676. 'type',
  19677. ' IUnknown = interface',
  19678. ' function _AddRef: longint;',
  19679. ' function _Release: longint;',
  19680. ' end;',
  19681. ' TObject = class(IUnknown)',
  19682. ' function _AddRef: longint; virtual; abstract;',
  19683. ' function _Release: longint; virtual; abstract;',
  19684. ' end;',
  19685. 'procedure DoIt(v: IUnknown; const j: IUnknown; var k: IUnknown; out l: IUnknown);',
  19686. 'var o: TObject;',
  19687. 'begin',
  19688. ' DoIt(v,v,v,v);',
  19689. ' DoIt(o,o,k,k);',
  19690. 'end;',
  19691. 'procedure DoSome;',
  19692. 'var v: IUnknown;',
  19693. 'begin',
  19694. ' DoIt(v,v,v,v);',
  19695. 'end;',
  19696. 'var i: IUnknown;',
  19697. 'begin',
  19698. ' DoIt(i,i,i,i);',
  19699. '']);
  19700. ConvertProgram;
  19701. CheckSource('TestClassInterface_COM_PassAsArg',
  19702. LinesToStr([ // statements
  19703. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19704. 'rtl.createClass(this, "TObject", null, function () {',
  19705. ' this.$init = function () {',
  19706. ' };',
  19707. ' this.$final = function () {',
  19708. ' };',
  19709. ' rtl.addIntf(this, $mod.IUnknown);',
  19710. '});',
  19711. 'this.DoIt = function (v, j, k, l) {',
  19712. ' var o = null;',
  19713. ' var $ir = rtl.createIntfRefs();',
  19714. ' rtl._AddRef(v);',
  19715. ' try {',
  19716. ' $mod.DoIt(v, v, {',
  19717. ' get: function () {',
  19718. ' return v;',
  19719. ' },',
  19720. ' set: function (w) {',
  19721. ' v = rtl.setIntfL(v, w);',
  19722. ' }',
  19723. ' }, {',
  19724. ' get: function () {',
  19725. ' return v;',
  19726. ' },',
  19727. ' set: function (w) {',
  19728. ' v = rtl.setIntfL(v, w);',
  19729. ' }',
  19730. ' });',
  19731. ' $mod.DoIt($ir.ref(1, rtl.queryIntfT(o, $mod.IUnknown)), $ir.ref(2, rtl.queryIntfT(o, $mod.IUnknown)), k, k);',
  19732. ' } finally {',
  19733. ' $ir.free();',
  19734. ' rtl._Release(v);',
  19735. ' };',
  19736. '};',
  19737. 'this.DoSome = function () {',
  19738. ' var v = null;',
  19739. ' try {',
  19740. ' $mod.DoIt(v, v, {',
  19741. ' get: function () {',
  19742. ' return v;',
  19743. ' },',
  19744. ' set: function (w) {',
  19745. ' v = rtl.setIntfL(v, w);',
  19746. ' }',
  19747. ' }, {',
  19748. ' get: function () {',
  19749. ' return v;',
  19750. ' },',
  19751. ' set: function (w) {',
  19752. ' v = rtl.setIntfL(v, w);',
  19753. ' }',
  19754. ' });',
  19755. ' } finally {',
  19756. ' rtl._Release(v);',
  19757. ' };',
  19758. '};',
  19759. 'this.i = null;',
  19760. '']),
  19761. LinesToStr([ // $mod.$main
  19762. '$mod.DoIt($mod.i, $mod.i, {',
  19763. ' p: $mod,',
  19764. ' get: function () {',
  19765. ' return this.p.i;',
  19766. ' },',
  19767. ' set: function (v) {',
  19768. ' rtl.setIntfP(this.p, "i", v);',
  19769. ' }',
  19770. '}, {',
  19771. ' p: $mod,',
  19772. ' get: function () {',
  19773. ' return this.p.i;',
  19774. ' },',
  19775. ' set: function (v) {',
  19776. ' rtl.setIntfP(this.p, "i", v);',
  19777. ' }',
  19778. '});',
  19779. '']));
  19780. end;
  19781. procedure TTestModule.TestClassInterface_COM_PassToUntypedParam;
  19782. begin
  19783. StartProgram(false);
  19784. Add([
  19785. '{$interfaces com}',
  19786. 'type',
  19787. ' IUnknown = interface',
  19788. ' function _AddRef: longint;',
  19789. ' function _Release: longint;',
  19790. ' end;',
  19791. ' TObject = class(IUnknown)',
  19792. ' function _AddRef: longint; virtual; abstract;',
  19793. ' function _Release: longint; virtual; abstract;',
  19794. ' end;',
  19795. 'procedure DoIt(out i);',
  19796. 'begin end;',
  19797. 'procedure DoSome;',
  19798. 'var v: IUnknown;',
  19799. 'begin',
  19800. ' DoIt(v);',
  19801. 'end;',
  19802. 'function GetIt: IUnknown;',
  19803. 'begin',
  19804. ' DoIt(Result);',
  19805. 'end;',
  19806. 'var i: IUnknown;',
  19807. 'begin',
  19808. ' DoIt(i);',
  19809. '']);
  19810. ConvertProgram;
  19811. CheckSource('TestClassInterface_COM_PassToUntypedParam',
  19812. LinesToStr([ // statements
  19813. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19814. 'rtl.createClass(this, "TObject", null, function () {',
  19815. ' this.$init = function () {',
  19816. ' };',
  19817. ' this.$final = function () {',
  19818. ' };',
  19819. ' rtl.addIntf(this, $mod.IUnknown);',
  19820. '});',
  19821. 'this.DoIt = function (i) {',
  19822. '};',
  19823. 'this.DoSome = function () {',
  19824. ' var v = null;',
  19825. ' try {',
  19826. ' $mod.DoIt({',
  19827. ' get: function () {',
  19828. ' return v;',
  19829. ' },',
  19830. ' set: function (w) {',
  19831. ' v = w;',
  19832. ' }',
  19833. ' });',
  19834. ' } finally {',
  19835. ' rtl._Release(v);',
  19836. ' };',
  19837. '};',
  19838. 'this.GetIt = function () {',
  19839. ' var Result = null;',
  19840. ' var $ok = false;',
  19841. ' try {',
  19842. ' $mod.DoIt({',
  19843. ' get: function () {',
  19844. ' return Result;',
  19845. ' },',
  19846. ' set: function (v) {',
  19847. ' Result = v;',
  19848. ' }',
  19849. ' });',
  19850. ' $ok = true;',
  19851. ' } finally {',
  19852. ' if (!$ok) rtl._Release(Result);',
  19853. ' };',
  19854. ' return Result;',
  19855. '};',
  19856. 'this.i = null;',
  19857. '']),
  19858. LinesToStr([ // $mod.$main
  19859. 'try {',
  19860. ' $mod.DoIt({',
  19861. ' p: $mod,',
  19862. ' get: function () {',
  19863. ' return this.p.i;',
  19864. ' },',
  19865. ' set: function (v) {',
  19866. ' this.p.i = v;',
  19867. ' }',
  19868. ' });',
  19869. '} finally {',
  19870. ' rtl._Release($mod.i);',
  19871. '};',
  19872. '']));
  19873. end;
  19874. procedure TTestModule.TestClassInterface_COM_FunctionInExpr;
  19875. begin
  19876. StartProgram(false);
  19877. Add([
  19878. '{$interfaces com}',
  19879. 'type',
  19880. ' IUnknown = interface',
  19881. ' function _AddRef: longint;',
  19882. ' function _Release: longint;',
  19883. ' end;',
  19884. ' TObject = class(IUnknown)',
  19885. ' function _AddRef: longint; virtual; abstract;',
  19886. ' function _Release: longint; virtual; abstract;',
  19887. ' end;',
  19888. 'function GetIt: IUnknown;',
  19889. 'begin',
  19890. 'end;',
  19891. 'procedure DoSome;',
  19892. 'var v: IUnknown;',
  19893. ' i: longint;',
  19894. 'begin',
  19895. ' v:=GetIt;',
  19896. ' v:=GetIt();',
  19897. ' GetIt()._AddRef;',
  19898. ' i:=GetIt()._AddRef;',
  19899. 'end;',
  19900. 'var v: IUnknown;',
  19901. ' i: longint;',
  19902. 'begin',
  19903. ' v:=GetIt;',
  19904. ' v:=GetIt();',
  19905. ' GetIt()._AddRef;',
  19906. ' i:=GetIt()._AddRef;',
  19907. '']);
  19908. ConvertProgram;
  19909. CheckSource('TestClassInterface_COM_FunctionInExpr',
  19910. LinesToStr([ // statements
  19911. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19912. 'rtl.createClass(this, "TObject", null, function () {',
  19913. ' this.$init = function () {',
  19914. ' };',
  19915. ' this.$final = function () {',
  19916. ' };',
  19917. ' rtl.addIntf(this, $mod.IUnknown);',
  19918. '});',
  19919. 'this.GetIt = function () {',
  19920. ' var Result = null;',
  19921. ' return Result;',
  19922. '};',
  19923. 'this.DoSome = function () {',
  19924. ' var v = null;',
  19925. ' var i = 0;',
  19926. ' var $ir = rtl.createIntfRefs();',
  19927. ' try {',
  19928. ' v = rtl.setIntfL(v, $mod.GetIt(), true);',
  19929. ' v = rtl.setIntfL(v, $mod.GetIt(), true);',
  19930. ' $ir.ref(1, $mod.GetIt())._AddRef();',
  19931. ' i = $ir.ref(2, $mod.GetIt())._AddRef();',
  19932. ' } finally {',
  19933. ' $ir.free();',
  19934. ' rtl._Release(v);',
  19935. ' };',
  19936. '};',
  19937. 'this.v = null;',
  19938. 'this.i = 0;',
  19939. '']),
  19940. LinesToStr([ // $mod.$main
  19941. 'var $ir = rtl.createIntfRefs();',
  19942. 'try {',
  19943. ' rtl.setIntfP($mod, "v", $mod.GetIt(), true);',
  19944. ' rtl.setIntfP($mod, "v", $mod.GetIt(), true);',
  19945. ' $ir.ref(1, $mod.GetIt())._AddRef();',
  19946. ' $mod.i = $ir.ref(2, $mod.GetIt())._AddRef();',
  19947. '} finally {',
  19948. ' $ir.free();',
  19949. '};',
  19950. '']));
  19951. end;
  19952. procedure TTestModule.TestClassInterface_COM_Property;
  19953. begin
  19954. StartProgram(false);
  19955. Add([
  19956. '{$interfaces com}',
  19957. 'type',
  19958. ' IUnknown = interface',
  19959. ' function _AddRef: longint;',
  19960. ' function _Release: longint;',
  19961. ' end;',
  19962. ' TObject = class(IUnknown)',
  19963. ' FAnt: IUnknown;',
  19964. ' function _AddRef: longint; virtual; abstract;',
  19965. ' function _Release: longint; virtual; abstract;',
  19966. ' function GetBird: IUnknown; virtual; abstract;',
  19967. ' procedure SetBird(Value: IUnknown); virtual; abstract;',
  19968. ' function GetItems(Index: longint): IUnknown; virtual; abstract;',
  19969. ' procedure SetItems(Index: longint; Value: IUnknown); virtual; abstract;',
  19970. ' property Ant: IUnknown read FAnt write FAnt;',
  19971. ' property Bird: IUnknown read GetBird write SetBird;',
  19972. ' property Items[Index: longint]: IUnknown read GetItems write SetItems; default;',
  19973. ' end;',
  19974. 'procedure DoIt;',
  19975. 'var',
  19976. ' o: TObject;',
  19977. ' v: IUnknown;',
  19978. 'begin',
  19979. ' v:=o.Ant;',
  19980. ' o.Ant:=v;',
  19981. ' o.Ant:=o.Ant;',
  19982. ' v:=o.Bird;',
  19983. ' o.Bird:=v;',
  19984. ' o.Bird:=o.Bird;',
  19985. ' v:=o.Items[1];',
  19986. ' o.Items[2]:=v;',
  19987. ' o.Items[3]:=o.Items[4];',
  19988. ' v:=o[5];',
  19989. ' o[6]:=v;',
  19990. ' o[7]:=o[8];',
  19991. 'end;',
  19992. 'begin',
  19993. '']);
  19994. ConvertProgram;
  19995. CheckSource('TestClassInterface_COM_Property',
  19996. LinesToStr([ // statements
  19997. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  19998. 'rtl.createClass(this, "TObject", null, function () {',
  19999. ' this.$init = function () {',
  20000. ' this.FAnt = null;',
  20001. ' };',
  20002. ' this.$final = function () {',
  20003. ' this.FAnt = undefined;',
  20004. ' };',
  20005. ' rtl.addIntf(this, $mod.IUnknown);',
  20006. '});',
  20007. 'this.DoIt = function () {',
  20008. ' var o = null;',
  20009. ' var v = null;',
  20010. ' var $ir = rtl.createIntfRefs();',
  20011. ' try {',
  20012. ' v = rtl.setIntfL(v, o.FAnt);',
  20013. ' rtl.setIntfP(o, "FAnt", v);',
  20014. ' rtl.setIntfP(o, "FAnt", o.FAnt);',
  20015. ' v = rtl.setIntfL(v, o.GetBird(), true);',
  20016. ' o.SetBird(v);',
  20017. ' o.SetBird($ir.ref(1, o.GetBird()));',
  20018. ' v = rtl.setIntfL(v, o.GetItems(1), true);',
  20019. ' o.SetItems(2, v);',
  20020. ' o.SetItems(3, $ir.ref(2, o.GetItems(4)));',
  20021. ' v = rtl.setIntfL(v, o.GetItems(5), true);',
  20022. ' o.SetItems(6, v);',
  20023. ' o.SetItems(7, $ir.ref(3, o.GetItems(8)));',
  20024. ' } finally {',
  20025. ' $ir.free();',
  20026. ' rtl._Release(v);',
  20027. ' };',
  20028. '};',
  20029. '']),
  20030. LinesToStr([ // $mod.$main
  20031. '']));
  20032. end;
  20033. procedure TTestModule.TestClassInterface_COM_IntfProperty;
  20034. begin
  20035. StartProgram(false);
  20036. Add([
  20037. '{$interfaces com}',
  20038. 'type',
  20039. ' IUnknown = interface',
  20040. ' function _AddRef: longint;',
  20041. ' function _Release: longint;',
  20042. ' function GetBird: IUnknown;',
  20043. ' procedure SetBird(Value: IUnknown);',
  20044. ' function GetItems(Index: longint): IUnknown;',
  20045. ' procedure SetItems(Index: longint; Value: IUnknown);',
  20046. ' property Bird: IUnknown read GetBird write SetBird;',
  20047. ' property Items[Index: longint]: IUnknown read GetItems write SetItems; default;',
  20048. ' end;',
  20049. ' TObject = class(IUnknown)',
  20050. ' function _AddRef: longint; virtual; abstract;',
  20051. ' function _Release: longint; virtual; abstract;',
  20052. ' function GetBird: IUnknown; virtual; abstract;',
  20053. ' procedure SetBird(Value: IUnknown); virtual; abstract;',
  20054. ' function GetItems(Index: longint): IUnknown; virtual; abstract;',
  20055. ' procedure SetItems(Index: longint; Value: IUnknown); virtual; abstract;',
  20056. ' end;',
  20057. 'procedure DoIt;',
  20058. 'var',
  20059. ' o: TObject;',
  20060. ' v: IUnknown;',
  20061. 'begin',
  20062. ' v:=v.Items[1];',
  20063. ' v.Items[2]:=v;',
  20064. ' v.Items[3]:=v.Items[4];',
  20065. ' v:=v[5];',
  20066. ' v[6]:=v;',
  20067. ' v[7]:=v[8];',
  20068. ' v[9].Bird.Bird:=v;',
  20069. ' v:=v.Bird[10].Bird',
  20070. 'end;',
  20071. 'begin',
  20072. '']);
  20073. ConvertProgram;
  20074. CheckSource('TestClassInterface_COM_IntfProperty',
  20075. LinesToStr([ // statements
  20076. 'rtl.createInterface(this, "IUnknown", "{385F5482-571B-338C-8130-4E97F330543B}", [',
  20077. ' "_AddRef",',
  20078. ' "_Release",',
  20079. ' "GetBird",',
  20080. ' "SetBird",',
  20081. ' "GetItems",',
  20082. ' "SetItems"',
  20083. '], null);',
  20084. 'rtl.createClass(this, "TObject", null, function () {',
  20085. ' this.$init = function () {',
  20086. ' };',
  20087. ' this.$final = function () {',
  20088. ' };',
  20089. ' rtl.addIntf(this, $mod.IUnknown);',
  20090. '});',
  20091. 'this.DoIt = function () {',
  20092. ' var o = null;',
  20093. ' var v = null;',
  20094. ' var $ir = rtl.createIntfRefs();',
  20095. ' try {',
  20096. ' v = rtl.setIntfL(v, v.GetItems(1), true);',
  20097. ' v.SetItems(2, v);',
  20098. ' v.SetItems(3, $ir.ref(1, v.GetItems(4)));',
  20099. ' v = rtl.setIntfL(v, v.GetItems(5), true);',
  20100. ' v.SetItems(6, v);',
  20101. ' v.SetItems(7, $ir.ref(2, v.GetItems(8)));',
  20102. ' $ir.ref(4, $ir.ref(3, v.GetItems(9)).GetBird()).SetBird(v);',
  20103. ' v = rtl.setIntfL(v, $ir.ref(6, $ir.ref(5, v.GetBird()).GetItems(10)).GetBird(), true);',
  20104. ' } finally {',
  20105. ' $ir.free();',
  20106. ' rtl._Release(v);',
  20107. ' };',
  20108. '};',
  20109. '']),
  20110. LinesToStr([ // $mod.$main
  20111. '']));
  20112. end;
  20113. procedure TTestModule.TestClassInterface_COM_Delegation;
  20114. begin
  20115. StartProgram(false);
  20116. Add([
  20117. '{$interfaces com}',
  20118. 'type',
  20119. ' IUnknown = interface',
  20120. ' function _AddRef: longint;',
  20121. ' function _Release: longint;',
  20122. ' end;',
  20123. ' IBird = interface(IUnknown)',
  20124. ' procedure Fly(s: string);',
  20125. ' end;',
  20126. ' IEagle = interface(IBird) end;',
  20127. ' IDove = interface(IBird) end;',
  20128. ' ISwallow = interface(IBird) end;',
  20129. ' TObject = class',
  20130. ' end;',
  20131. ' TBird = class(TObject,IBird,IEagle,IDove,ISwallow)',
  20132. ' function _AddRef: longint; virtual; abstract;',
  20133. ' function _Release: longint; virtual; abstract;',
  20134. ' procedure Fly(s: string); virtual; abstract;',
  20135. ' end;',
  20136. ' TBat = class(IBird,IEagle,IDove,ISwallow)',
  20137. ' function _AddRef: longint; virtual; abstract;',
  20138. ' function _Release: longint; virtual; abstract;',
  20139. ' FBirdIntf: IBird;',
  20140. ' property BirdIntf: IBird read FBirdIntf implements IBird;',
  20141. ' function GetEagleIntf: IEagle; virtual; abstract;',
  20142. ' property EagleIntf: IEagle read GetEagleIntf implements IEagle;',
  20143. ' FDoveObj: TBird;',
  20144. ' property DoveObj: TBird read FDoveObj implements IDove;',
  20145. ' function GetSwallowObj: TBird; virtual; abstract;',
  20146. ' property SwallowObj: TBird read GetSwallowObj implements ISwallow;',
  20147. ' end;',
  20148. 'begin',
  20149. '']);
  20150. ConvertProgram;
  20151. CheckSource('TestClassInterface_COM_Delegation',
  20152. LinesToStr([ // statements
  20153. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  20154. 'rtl.createInterface(this, "IBird", "{CC440C7F-7623-3DEE-AE88-000B86AAF108}", ["Fly"], this.IUnknown);',
  20155. 'rtl.createInterface(this, "IEagle", "{4B6A41C9-B020-3D7C-B688-96D19022B1B4}", [], this.IBird);',
  20156. 'rtl.createInterface(this, "IDove", "{4B6A41C9-B020-3D7C-B688-96D18EF16074}", [], this.IBird);',
  20157. 'rtl.createInterface(this, "ISwallow", "{BB6A41C9-B020-3D7C-B688-96D1CBDCB359}", [], this.IBird);',
  20158. 'rtl.createClass(this, "TObject", null, function () {',
  20159. ' this.$init = function () {',
  20160. ' };',
  20161. ' this.$final = function () {',
  20162. ' };',
  20163. '});',
  20164. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  20165. ' rtl.addIntf(this, $mod.IBird);',
  20166. ' rtl.addIntf(this, $mod.IEagle);',
  20167. ' rtl.addIntf(this, $mod.IDove);',
  20168. ' rtl.addIntf(this, $mod.ISwallow);',
  20169. '});',
  20170. 'rtl.createClass(this, "TBat", this.TObject, function () {',
  20171. ' this.$init = function () {',
  20172. ' $mod.TObject.$init.call(this);',
  20173. ' this.FBirdIntf = null;',
  20174. ' this.FDoveObj = null;',
  20175. ' };',
  20176. ' this.$final = function () {',
  20177. ' this.FBirdIntf = undefined;',
  20178. ' this.FDoveObj = undefined;',
  20179. ' $mod.TObject.$final.call(this);',
  20180. ' };',
  20181. ' this.$intfmaps = {',
  20182. ' "{CC440C7F-7623-3DEE-AE88-000B86AAF108}": function () {',
  20183. ' return rtl._AddRef(this.FBirdIntf);',
  20184. ' },',
  20185. ' "{4B6A41C9-B020-3D7C-B688-96D19022B1B4}": function () {',
  20186. ' return this.GetEagleIntf();',
  20187. ' },',
  20188. ' "{4B6A41C9-B020-3D7C-B688-96D18EF16074}": function () {',
  20189. ' return rtl.queryIntfT(this.FDoveObj, $mod.IDove);',
  20190. ' },',
  20191. ' "{BB6A41C9-B020-3D7C-B688-96D1CBDCB359}": function () {',
  20192. ' return rtl.queryIntfT(this.GetSwallowObj(), $mod.ISwallow);',
  20193. ' }',
  20194. ' };',
  20195. '});',
  20196. '']),
  20197. LinesToStr([ // $mod.$main
  20198. '']));
  20199. end;
  20200. procedure TTestModule.TestClassInterface_COM_With;
  20201. begin
  20202. StartProgram(false);
  20203. Add([
  20204. '{$interfaces com}',
  20205. 'type',
  20206. ' IUnknown = interface',
  20207. ' function _AddRef: longint;',
  20208. ' function _Release: longint;',
  20209. ' function GetAnt: IUnknown;',
  20210. ' property Ant: IUnknown read GetAnt;',
  20211. ' end;',
  20212. ' TObject = class(IUnknown)',
  20213. ' function _AddRef: longint; virtual; abstract;',
  20214. ' function _Release: longint; virtual; abstract;',
  20215. ' function GetAnt: IUnknown; virtual; abstract;',
  20216. ' property Ant: IUnknown read GetAnt;',
  20217. ' end;',
  20218. 'procedure DoIt;',
  20219. 'var',
  20220. ' i: IUnknown;',
  20221. 'begin',
  20222. ' with i do ',
  20223. ' GetAnt;',
  20224. ' with i.Ant, Ant do ',
  20225. ' GetAnt;',
  20226. 'end;',
  20227. 'begin',
  20228. '']);
  20229. ConvertProgram;
  20230. CheckSource('TestClassInterface_COM_With',
  20231. LinesToStr([ // statements
  20232. 'rtl.createInterface(this, "IUnknown", "{D7ADB00D-C6B6-39FB-BDDF-21CD521DDFA9}", ["_AddRef", "_Release", "GetAnt"], null);',
  20233. 'rtl.createClass(this, "TObject", null, function () {',
  20234. ' this.$init = function () {',
  20235. ' };',
  20236. ' this.$final = function () {',
  20237. ' };',
  20238. ' rtl.addIntf(this, $mod.IUnknown);',
  20239. '});',
  20240. 'this.DoIt = function () {',
  20241. ' var i = null;',
  20242. ' var $ir = rtl.createIntfRefs();',
  20243. ' try {',
  20244. ' $ir.ref(1, i.GetAnt());',
  20245. ' var $with = $ir.ref(2, i.GetAnt());',
  20246. ' var $with1 = $ir.ref(3, $with.GetAnt());',
  20247. ' $ir.ref(4, $with1.GetAnt());',
  20248. ' } finally {',
  20249. ' $ir.free();',
  20250. ' };',
  20251. '};',
  20252. '']),
  20253. LinesToStr([ // $mod.$main
  20254. '']));
  20255. end;
  20256. procedure TTestModule.TestClassInterface_COM_ForIn;
  20257. begin
  20258. StartProgram(false);
  20259. Add([
  20260. '{$interfaces com}',
  20261. 'type',
  20262. ' IUnknown = interface end;',
  20263. ' TObject = class',
  20264. ' Id: longint;',
  20265. ' end;',
  20266. ' IEnumerator = interface(IUnknown)',
  20267. ' function GetCurrent: TObject;',
  20268. ' function MoveNext: Boolean;',
  20269. ' property Current: TObject read GetCurrent;',
  20270. ' end;',
  20271. ' IEnumerable = interface(IUnknown)',
  20272. ' function GetEnumerator: IEnumerator;',
  20273. ' end;',
  20274. 'var',
  20275. ' o: TObject;',
  20276. ' i: IEnumerable;',
  20277. 'begin',
  20278. ' for o in i do o.Id:=3;',
  20279. '']);
  20280. ConvertProgram;
  20281. CheckSource('TestClassInterface_COM_ForIn',
  20282. LinesToStr([ // statements
  20283. 'rtl.createInterface(this, "IUnknown", "{B92D5841-758A-322B-B800-000000000000}", [], null);',
  20284. 'rtl.createClass(this, "TObject", null, function () {',
  20285. ' this.$init = function () {',
  20286. ' this.Id = 0;',
  20287. ' };',
  20288. ' this.$final = function () {',
  20289. ' };',
  20290. '});',
  20291. 'rtl.createInterface(this, "IEnumerator", "{95D7745D-ED61-3F13-BBE4-07708161999E}", ["GetCurrent", "MoveNext"], this.IUnknown);',
  20292. 'rtl.createInterface(this, "IEnumerable", "{8CC9D45D-ED7D-3B73-96B6-290B931BB19E}", ["GetEnumerator"], this.IUnknown);',
  20293. 'this.o = null;',
  20294. 'this.i = null;',
  20295. '']),
  20296. LinesToStr([ // $mod.$main
  20297. 'var $in = $mod.i.GetEnumerator();',
  20298. 'try {',
  20299. ' while ($in.MoveNext()) {',
  20300. ' $mod.o = $in.GetCurrent();',
  20301. ' $mod.o.Id = 3;',
  20302. ' }',
  20303. '} finally {',
  20304. ' rtl._Release($in)',
  20305. '};',
  20306. '']));
  20307. end;
  20308. procedure TTestModule.TestClassInterface_COM_ArrayOfIntfFail;
  20309. begin
  20310. StartProgram(false);
  20311. Add([
  20312. '{$interfaces com}',
  20313. 'type',
  20314. ' IUnknown = interface',
  20315. ' function _AddRef: longint;',
  20316. ' function _Release: longint;',
  20317. ' end;',
  20318. ' TObject = class',
  20319. ' end;',
  20320. ' TArrOfIntf = array of IUnknown;',
  20321. 'begin',
  20322. '']);
  20323. SetExpectedPasResolverError('Not supported: array of COM-interface',nNotSupportedX);
  20324. ConvertProgram;
  20325. end;
  20326. procedure TTestModule.TestClassInterface_COM_RecordIntfFail;
  20327. begin
  20328. StartProgram(false);
  20329. Add([
  20330. '{$interfaces com}',
  20331. 'type',
  20332. ' IUnknown = interface',
  20333. ' function _AddRef: longint;',
  20334. ' function _Release: longint;',
  20335. ' end;',
  20336. ' TRec = record',
  20337. ' i: IUnknown;',
  20338. ' end;',
  20339. 'begin',
  20340. '']);
  20341. SetExpectedPasResolverError('Not supported: COM-interface as record member',nNotSupportedX);
  20342. ConvertProgram;
  20343. end;
  20344. procedure TTestModule.TestClassInterface_COM_UnitInitialization;
  20345. begin
  20346. StartUnit(false);
  20347. Add([
  20348. '{$interfaces com}',
  20349. 'interface',
  20350. 'implementation',
  20351. 'type',
  20352. ' IUnknown = interface',
  20353. ' function _AddRef: longint;',
  20354. ' end;',
  20355. ' TObject = class(IUnknown)',
  20356. ' function _AddRef: longint;',
  20357. ' end;',
  20358. 'function TObject._AddRef: longint; begin end;',
  20359. 'var i: IUnknown;',
  20360. ' o: TObject;',
  20361. 'initialization',
  20362. ' i:=nil;',
  20363. ' i:=i;',
  20364. ' i:=o;',
  20365. ' if (o as IUnknown)=nil then ;',
  20366. '']);
  20367. ConvertUnit;
  20368. CheckSource('TestClassInterface_COM_UnitInitialization',
  20369. LinesToStr([ // statements
  20370. 'var $impl = $mod.$impl;',
  20371. '']),
  20372. LinesToStr([ // this.$init
  20373. 'var $ir = rtl.createIntfRefs();',
  20374. 'try {',
  20375. ' rtl.setIntfP($impl, "i", null);',
  20376. ' rtl.setIntfP($impl, "i", $impl.i);',
  20377. ' rtl.setIntfP($impl, "i", rtl.queryIntfT($impl.o, $impl.IUnknown), true);',
  20378. ' if ($ir.ref(1, rtl.queryIntfT($impl.o, $impl.IUnknown)) === null) ;',
  20379. '} finally {',
  20380. ' $ir.free();',
  20381. '};',
  20382. '']),
  20383. LinesToStr([ // implementation
  20384. 'rtl.createInterface($impl, "IUnknown", "{B92D5841-758A-322B-BDDF-21CD52180000}", ["_AddRef"], null);',
  20385. 'rtl.createClass($impl, "TObject", null, function () {',
  20386. ' this.$init = function () {',
  20387. ' };',
  20388. ' this.$final = function () {',
  20389. ' };',
  20390. ' this._AddRef = function () {',
  20391. ' var Result = 0;',
  20392. ' return Result;',
  20393. ' };',
  20394. ' rtl.addIntf(this, $impl.IUnknown);',
  20395. '});',
  20396. '$impl.i = null;',
  20397. '$impl.o = null;',
  20398. ''])
  20399. );
  20400. end;
  20401. procedure TTestModule.TestClassInterface_GUID;
  20402. begin
  20403. StartProgram(false);
  20404. Add([
  20405. '{$interfaces corba}',
  20406. 'type',
  20407. ' IUnknown = interface',
  20408. ' [''{f31db68f-3010-D355-4EBA-CDD4EF4A737C}'']',
  20409. ' end;',
  20410. ' TObject = class end;',
  20411. ' TGUID = record D1, D2, D3, D4: word; end;',
  20412. ' TAliasGUID = TGUID;',
  20413. ' TGUIDString = type string;',
  20414. ' TAliasGUIDString = TGUIDString;',
  20415. 'procedure DoConstGUIDIt(const g: TAliasGUID); overload;',
  20416. 'begin end;',
  20417. 'procedure DoDefGUID(g: TAliasGUID); overload;',
  20418. 'begin end;',
  20419. 'procedure DoStr(const s: TAliasGUIDString); overload;',
  20420. 'begin end;',
  20421. 'var',
  20422. ' i: IUnknown;',
  20423. ' g: TAliasGUID = ''{d91c9af4-3C93-420F-A303-BF5BA82BFD23}'';',
  20424. ' s: TAliasGUIDString;',
  20425. 'begin',
  20426. ' DoConstGUIDIt(IUnknown);',
  20427. ' DoDefGUID(IUnknown);',
  20428. ' DoStr(IUnknown);',
  20429. ' DoConstGUIDIt(i);',
  20430. ' DoDefGUID(i);',
  20431. ' DoStr(i);',
  20432. ' DoConstGUIDIt(''{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}'');',
  20433. ' DoDefGUID(''{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}'');',
  20434. ' DoStr(g);',
  20435. ' g:=i;',
  20436. ' g:=IUnknown;',
  20437. ' g:=''{D91C9AF4-3C93-420F-A303-bf5ba82bfd23}'';',
  20438. ' s:=i;',
  20439. ' s:=IUnknown;',
  20440. ' s:=g;',
  20441. ' if g=i then ;',
  20442. ' if i=g then ;',
  20443. ' if g=IUnknown then ;',
  20444. ' if IUnknown=g then ;',
  20445. ' if s=i then ;',
  20446. ' if i=s then ;',
  20447. ' if s=IUnknown then ;',
  20448. ' if IUnknown=s then ;',
  20449. ' if s=g then ;',
  20450. ' if g=s then ;',
  20451. '']);
  20452. ConvertProgram;
  20453. CheckSource('TestClassInterface_GUID',
  20454. LinesToStr([ // statements
  20455. 'rtl.createInterface(this, "IUnknown", "{F31DB68F-3010-D355-4EBA-CDD4EF4A737C}", [], null);',
  20456. 'rtl.createClass(this, "TObject", null, function () {',
  20457. ' this.$init = function () {',
  20458. ' };',
  20459. ' this.$final = function () {',
  20460. ' };',
  20461. '});',
  20462. 'rtl.recNewT(this, "TGUID", function () {',
  20463. ' this.D1 = 0;',
  20464. ' this.D2 = 0;',
  20465. ' this.D3 = 0;',
  20466. ' this.D4 = 0;',
  20467. ' this.$eq = function (b) {',
  20468. ' return (this.D1 === b.D1) && (this.D2 === b.D2) && (this.D3 === b.D3) && (this.D4 === b.D4);',
  20469. ' };',
  20470. ' this.$assign = function (s) {',
  20471. ' this.D1 = s.D1;',
  20472. ' this.D2 = s.D2;',
  20473. ' this.D3 = s.D3;',
  20474. ' this.D4 = s.D4;',
  20475. ' return this;',
  20476. ' };',
  20477. '});',
  20478. 'this.DoConstGUIDIt = function (g) {',
  20479. '};',
  20480. 'this.DoDefGUID = function (g) {',
  20481. '};',
  20482. 'this.DoStr = function (s) {',
  20483. '};',
  20484. 'this.i = null;',
  20485. 'this.g = this.TGUID.$clone({',
  20486. ' D1: 0xD91C9AF4,',
  20487. ' D2: 0x3C93,',
  20488. ' D3: 0x420F,',
  20489. ' D4: [',
  20490. ' 0xA3,',
  20491. ' 0x03,',
  20492. ' 0xBF,',
  20493. ' 0x5B,',
  20494. ' 0xA8,',
  20495. ' 0x2B,',
  20496. ' 0xFD,',
  20497. ' 0x23',
  20498. ' ]',
  20499. '});',
  20500. 'this.s = "";',
  20501. '']),
  20502. LinesToStr([ // $mod.$main
  20503. '$mod.DoConstGUIDIt(rtl.getIntfGUIDR($mod.IUnknown));',
  20504. '$mod.DoDefGUID($mod.TGUID.$clone(rtl.getIntfGUIDR($mod.IUnknown)));',
  20505. '$mod.DoStr($mod.IUnknown.$guid);',
  20506. '$mod.DoConstGUIDIt(rtl.getIntfGUIDR($mod.i));',
  20507. '$mod.DoDefGUID($mod.TGUID.$clone(rtl.getIntfGUIDR($mod.i)));',
  20508. '$mod.DoStr($mod.i.$guid);',
  20509. '$mod.DoConstGUIDIt(rtl.strToGUIDR("{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}"));',
  20510. '$mod.DoDefGUID(rtl.strToGUIDR("{D91C9AF4-3c93-420f-A303-BF5BA82BFD23}"));',
  20511. '$mod.DoStr(rtl.guidrToStr($mod.g));',
  20512. '$mod.g.$assign(rtl.getIntfGUIDR($mod.i));',
  20513. '$mod.g.$assign(rtl.getIntfGUIDR($mod.IUnknown));',
  20514. '$mod.g.$assign({',
  20515. ' D1: 0xD91C9AF4,',
  20516. ' D2: 0x3C93,',
  20517. ' D3: 0x420F,',
  20518. ' D4: [',
  20519. ' 0xA3,',
  20520. ' 0x03,',
  20521. ' 0xBF,',
  20522. ' 0x5B,',
  20523. ' 0xA8,',
  20524. ' 0x2B,',
  20525. ' 0xFD,',
  20526. ' 0x23',
  20527. ' ]',
  20528. '});',
  20529. '$mod.s = $mod.i.$guid;',
  20530. '$mod.s = $mod.IUnknown.$guid;',
  20531. '$mod.s = rtl.guidrToStr($mod.g);',
  20532. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.i))) ;',
  20533. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.i))) ;',
  20534. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.IUnknown))) ;',
  20535. 'if ($mod.g.$eq(rtl.getIntfGUIDR($mod.IUnknown))) ;',
  20536. 'if ($mod.s === $mod.i.$guid) ;',
  20537. 'if ($mod.i.$guid === $mod.s) ;',
  20538. 'if ($mod.s === $mod.IUnknown.$guid) ;',
  20539. 'if ($mod.IUnknown.$guid === $mod.s) ;',
  20540. 'if ($mod.g.$eq(rtl.createTGUID($mod.s))) ;',
  20541. 'if ($mod.g.$eq(rtl.createTGUID($mod.s))) ;',
  20542. '']));
  20543. end;
  20544. procedure TTestModule.TestClassInterface_GUIDProperty;
  20545. begin
  20546. StartProgram(false);
  20547. Add([
  20548. '{$interfaces corba}',
  20549. 'type',
  20550. ' IUnknown = interface',
  20551. ' [''{f31db68f-3010-D355-4EBA-CDD4EF4A737C}'']',
  20552. ' end;',
  20553. ' TGUID = record D1, D2, D3, D4: word; end;',
  20554. ' TAliasGUID = TGUID;',
  20555. ' TGUIDString = type string;',
  20556. ' TAliasGUIDString = TGUIDString;',
  20557. ' TObject = class',
  20558. ' function GetG: TAliasGUID; virtual; abstract;',
  20559. ' procedure SetG(const Value: TAliasGUID); virtual; abstract;',
  20560. ' function GetS: TAliasGUIDString; virtual; abstract;',
  20561. ' procedure SetS(const Value: TAliasGUIDString); virtual; abstract;',
  20562. ' property g: TAliasGUID read GetG write SetG;',
  20563. ' property s: TAliasGUIDString read GetS write SetS;',
  20564. ' end;',
  20565. 'var o: TObject;',
  20566. 'begin',
  20567. ' o.g:=IUnknown;',
  20568. ' o.g:=''{D91C9AF4-3C93-420F-A303-bf5ba82bfd23}'';',
  20569. ' o.s:=IUnknown;',
  20570. ' o.s:=o.g;',
  20571. '']);
  20572. ConvertProgram;
  20573. CheckSource('TestClassInterface_GUIDProperty',
  20574. LinesToStr([ // statements
  20575. 'rtl.createInterface(this, "IUnknown", "{F31DB68F-3010-D355-4EBA-CDD4EF4A737C}", [], null);',
  20576. 'rtl.recNewT(this, "TGUID", function () {',
  20577. ' this.D1 = 0;',
  20578. ' this.D2 = 0;',
  20579. ' this.D3 = 0;',
  20580. ' this.D4 = 0;',
  20581. ' this.$eq = function (b) {',
  20582. ' return (this.D1 === b.D1) && (this.D2 === b.D2) && (this.D3 === b.D3) && (this.D4 === b.D4);',
  20583. ' };',
  20584. ' this.$assign = function (s) {',
  20585. ' this.D1 = s.D1;',
  20586. ' this.D2 = s.D2;',
  20587. ' this.D3 = s.D3;',
  20588. ' this.D4 = s.D4;',
  20589. ' return this;',
  20590. ' };',
  20591. '});',
  20592. 'rtl.createClass(this, "TObject", null, function () {',
  20593. ' this.$init = function () {',
  20594. ' };',
  20595. ' this.$final = function () {',
  20596. ' };',
  20597. '});',
  20598. 'this.o = null;',
  20599. '']),
  20600. LinesToStr([ // $mod.$main
  20601. '$mod.o.SetG(rtl.getIntfGUIDR($mod.IUnknown));',
  20602. '$mod.o.SetG({',
  20603. ' D1: 0xD91C9AF4,',
  20604. ' D2: 0x3C93,',
  20605. ' D3: 0x420F,',
  20606. ' D4: [',
  20607. ' 0xA3,',
  20608. ' 0x03,',
  20609. ' 0xBF,',
  20610. ' 0x5B,',
  20611. ' 0xA8,',
  20612. ' 0x2B,',
  20613. ' 0xFD,',
  20614. ' 0x23',
  20615. ' ]',
  20616. '});',
  20617. '$mod.o.SetS($mod.IUnknown.$guid);',
  20618. '$mod.o.SetS(rtl.guidrToStr($mod.o.GetG()));',
  20619. '']));
  20620. end;
  20621. procedure TTestModule.TestClassHelper_ClassVar;
  20622. begin
  20623. StartProgram(false);
  20624. Add([
  20625. 'type',
  20626. ' TObject = class',
  20627. ' end;',
  20628. ' THelper = class helper for TObject',
  20629. ' const',
  20630. ' One = 1;',
  20631. ' Two: word = 2;',
  20632. ' class var',
  20633. ' Glob: word;',
  20634. ' function Foo(w: word): word;',
  20635. ' class function Bar(w: word): word;',
  20636. ' end;',
  20637. 'function THelper.foo(w: word): word;',
  20638. 'begin',
  20639. ' Result:=w;',
  20640. ' Two:=One+w;',
  20641. ' Glob:=Glob;',
  20642. ' Result:=Self.Glob;',
  20643. ' Self.Glob:=Self.Glob;',
  20644. ' with Self do Glob:=Glob;',
  20645. 'end;',
  20646. 'class function THelper.bar(w: word): word;',
  20647. 'begin',
  20648. ' Result:=w;',
  20649. ' Two:=One;',
  20650. ' Glob:=Glob;',
  20651. ' Self.Glob:=Self.Glob;',
  20652. ' with Self do Glob:=Glob;',
  20653. 'end;',
  20654. 'var o: TObject;',
  20655. 'begin',
  20656. ' tobject.two:=tobject.one;',
  20657. ' tobject.Glob:=tobject.Glob;',
  20658. ' with tobject do begin',
  20659. ' two:=one;',
  20660. ' Glob:=Glob;',
  20661. ' end;',
  20662. ' o.two:=o.one;',
  20663. ' o.Glob:=o.Glob;',
  20664. ' with o do begin',
  20665. ' two:=one;',
  20666. ' Glob:=Glob;',
  20667. ' end;',
  20668. '']);
  20669. ConvertProgram;
  20670. CheckSource('TestClassHelper_ClassVar',
  20671. LinesToStr([ // statements
  20672. 'rtl.createClass(this, "TObject", null, function () {',
  20673. ' this.$init = function () {',
  20674. ' };',
  20675. ' this.$final = function () {',
  20676. ' };',
  20677. '});',
  20678. 'rtl.createHelper(this, "THelper", null, function () {',
  20679. ' this.One = 1;',
  20680. ' this.Two = 2;',
  20681. ' this.Glob = 0;',
  20682. ' this.Foo = function (w) {',
  20683. ' var Result = 0;',
  20684. ' Result = w;',
  20685. ' $mod.THelper.Two = 1 + w;',
  20686. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20687. ' Result = $mod.THelper.Glob;',
  20688. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20689. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20690. ' return Result;',
  20691. ' };',
  20692. ' this.Bar = function (w) {',
  20693. ' var Result = 0;',
  20694. ' Result = w;',
  20695. ' $mod.THelper.Two = 1;',
  20696. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20697. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20698. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  20699. ' return Result;',
  20700. ' };',
  20701. '});',
  20702. 'this.o = null;',
  20703. '']),
  20704. LinesToStr([ // $mod.$main
  20705. '$mod.THelper.Two = 1;',
  20706. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20707. 'var $with = $mod.TObject;',
  20708. '$mod.THelper.Two = 1;',
  20709. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20710. '$mod.THelper.Two = 1;',
  20711. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20712. 'var $with1 = $mod.o;',
  20713. '$mod.THelper.Two = 1;',
  20714. '$mod.THelper.Glob = $mod.THelper.Glob;',
  20715. '']));
  20716. end;
  20717. procedure TTestModule.TestClassHelper_Method_AccessInstanceFields;
  20718. begin
  20719. StartProgram(false);
  20720. Add([
  20721. 'type',
  20722. ' TObject = class',
  20723. ' FSize: word;',
  20724. ' property Size: word read FSize write FSize;',
  20725. ' end;',
  20726. ' THelper = class helper for TObject',
  20727. ' function Foo(w: word = 1): word;',
  20728. ' end;',
  20729. 'function THelper.foo(w: word): word;',
  20730. 'begin',
  20731. ' Result:=Size;',
  20732. ' Size:=Size+2;',
  20733. ' Self.Size:=Self.Size+3;',
  20734. ' FSize:=FSize+4;',
  20735. ' Self.FSize:=Self.FSize+5;',
  20736. ' with Self do begin',
  20737. ' Size:=Size+6;',
  20738. ' FSize:=FSize+7;',
  20739. ' FSize:=FSize+8;',
  20740. ' end;',
  20741. 'end;',
  20742. 'begin',
  20743. '']);
  20744. ConvertProgram;
  20745. CheckSource('TestClassHelper_Method_AccessInstanceFields',
  20746. LinesToStr([ // statements
  20747. 'rtl.createClass(this, "TObject", null, function () {',
  20748. ' this.$init = function () {',
  20749. ' this.FSize = 0;',
  20750. ' };',
  20751. ' this.$final = function () {',
  20752. ' };',
  20753. '});',
  20754. 'rtl.createHelper(this, "THelper", null, function () {',
  20755. ' this.Foo = function (w) {',
  20756. ' var Result = 0;',
  20757. ' Result = this.FSize;',
  20758. ' this.FSize = this.FSize + 2;',
  20759. ' this.FSize = this.FSize + 3;',
  20760. ' this.FSize = this.FSize + 4;',
  20761. ' this.FSize = this.FSize + 5;',
  20762. ' this.FSize = this.FSize + 6;',
  20763. ' this.FSize = this.FSize + 7;',
  20764. ' this.FSize = this.FSize + 8;',
  20765. ' return Result;',
  20766. ' };',
  20767. '});',
  20768. '']),
  20769. LinesToStr([ // $mod.$main
  20770. '']));
  20771. end;
  20772. procedure TTestModule.TestClassHelper_Method_Call;
  20773. begin
  20774. StartProgram(false);
  20775. Add([
  20776. 'type',
  20777. ' TObject = class',
  20778. ' procedure Run(w: word = 10);',
  20779. ' end;',
  20780. ' THelper = class helper for TObject',
  20781. ' function Foo(w: word = 1): word;',
  20782. ' end;',
  20783. 'procedure TObject.Run(w: word);',
  20784. 'var o: TObject;',
  20785. 'begin',
  20786. ' Foo;',
  20787. ' Foo();',
  20788. ' Foo(2);',
  20789. ' Self.Foo;',
  20790. ' Self.Foo();',
  20791. ' Self.Foo(3);',
  20792. ' with Self do begin',
  20793. ' Foo;',
  20794. ' Foo();',
  20795. ' Foo(4);',
  20796. ' end;',
  20797. ' with o do Foo(5);',
  20798. 'end;',
  20799. 'function THelper.foo(w: word): word;',
  20800. 'begin',
  20801. ' Run;',
  20802. ' Run();',
  20803. ' Run(11);',
  20804. ' Foo;',
  20805. ' Foo();',
  20806. ' Foo(12);',
  20807. ' Self.Foo;',
  20808. ' Self.Foo();',
  20809. ' Self.Foo(13);',
  20810. ' with Self do begin',
  20811. ' Foo;',
  20812. ' Foo();',
  20813. ' Foo(14);',
  20814. ' end;',
  20815. 'end;',
  20816. 'var Obj: TObject;',
  20817. 'begin',
  20818. ' obj.Foo;',
  20819. ' obj.Foo();',
  20820. ' obj.Foo(21);',
  20821. ' with obj do begin',
  20822. ' Foo;',
  20823. ' Foo();',
  20824. ' Foo(22);',
  20825. ' end;',
  20826. '']);
  20827. ConvertProgram;
  20828. CheckSource('TestClassHelper_Method_Call',
  20829. LinesToStr([ // statements
  20830. 'rtl.createClass(this, "TObject", null, function () {',
  20831. ' this.$init = function () {',
  20832. ' };',
  20833. ' this.$final = function () {',
  20834. ' };',
  20835. ' this.Run = function (w) {',
  20836. ' var o = null;',
  20837. ' $mod.THelper.Foo.call(this, 1);',
  20838. ' $mod.THelper.Foo.call(this, 1);',
  20839. ' $mod.THelper.Foo.call(this, 2);',
  20840. ' $mod.THelper.Foo.call(this, 1);',
  20841. ' $mod.THelper.Foo.call(this, 1);',
  20842. ' $mod.THelper.Foo.call(this, 3);',
  20843. ' $mod.THelper.Foo.call(this, 1);',
  20844. ' $mod.THelper.Foo.call(this, 1);',
  20845. ' $mod.THelper.Foo.call(this, 4);',
  20846. ' $mod.THelper.Foo.call(o, 5);',
  20847. ' };',
  20848. '});',
  20849. 'rtl.createHelper(this, "THelper", null, function () {',
  20850. ' this.Foo = function (w) {',
  20851. ' var Result = 0;',
  20852. ' this.Run(10);',
  20853. ' this.Run(10);',
  20854. ' this.Run(11);',
  20855. ' $mod.THelper.Foo.call(this, 1);',
  20856. ' $mod.THelper.Foo.call(this, 1);',
  20857. ' $mod.THelper.Foo.call(this, 12);',
  20858. ' $mod.THelper.Foo.call(this, 1);',
  20859. ' $mod.THelper.Foo.call(this, 1);',
  20860. ' $mod.THelper.Foo.call(this, 13);',
  20861. ' $mod.THelper.Foo.call(this, 1);',
  20862. ' $mod.THelper.Foo.call(this, 1);',
  20863. ' $mod.THelper.Foo.call(this, 14);',
  20864. ' return Result;',
  20865. ' };',
  20866. '});',
  20867. 'this.Obj = null;',
  20868. '']),
  20869. LinesToStr([ // $mod.$main
  20870. '$mod.THelper.Foo.call($mod.Obj, 1);',
  20871. '$mod.THelper.Foo.call($mod.Obj, 1);',
  20872. '$mod.THelper.Foo.call($mod.Obj, 21);',
  20873. 'var $with = $mod.Obj;',
  20874. '$mod.THelper.Foo.call($with, 1);',
  20875. '$mod.THelper.Foo.call($with, 1);',
  20876. '$mod.THelper.Foo.call($with, 22);',
  20877. '']));
  20878. end;
  20879. procedure TTestModule.TestClassHelper_Method_Nested_Call;
  20880. begin
  20881. StartProgram(false);
  20882. Add([
  20883. 'type',
  20884. ' TObject = class',
  20885. ' procedure Run(w: word = 10);',
  20886. ' end;',
  20887. ' THelper = class helper for TObject',
  20888. ' function Foo(w: word = 1): word;',
  20889. ' end;',
  20890. 'procedure TObject.Run(w: word);',
  20891. ' procedure Sub(Self: TObject);',
  20892. ' begin',
  20893. ' Foo;',
  20894. ' Foo();',
  20895. ' Self.Foo;',
  20896. ' Self.Foo();',
  20897. ' with Self do begin',
  20898. ' Foo;',
  20899. ' Foo();',
  20900. ' end;',
  20901. ' end;',
  20902. 'begin',
  20903. 'end;',
  20904. 'function THelper.foo(w: word): word;',
  20905. ' procedure Sub(Self: TObject);',
  20906. ' begin',
  20907. ' Run;',
  20908. ' Run();',
  20909. ' Foo;',
  20910. ' Foo();',
  20911. ' Self.Foo;',
  20912. ' Self.Foo();',
  20913. ' with Self do begin',
  20914. ' Foo;',
  20915. ' Foo();',
  20916. ' end;',
  20917. ' end;',
  20918. 'begin',
  20919. 'end;',
  20920. 'begin',
  20921. '']);
  20922. ConvertProgram;
  20923. CheckSource('TestClassHelper_Method_Nested_Call',
  20924. LinesToStr([ // statements
  20925. 'rtl.createClass(this, "TObject", null, function () {',
  20926. ' this.$init = function () {',
  20927. ' };',
  20928. ' this.$final = function () {',
  20929. ' };',
  20930. ' this.Run = function (w) {',
  20931. ' var $Self = this;',
  20932. ' function Sub(Self) {',
  20933. ' $mod.THelper.Foo.call($Self, 1);',
  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. ' };',
  20940. ' };',
  20941. '});',
  20942. 'rtl.createHelper(this, "THelper", null, function () {',
  20943. ' this.Foo = function (w) {',
  20944. ' var $Self = this;',
  20945. ' var Result = 0;',
  20946. ' function Sub(Self) {',
  20947. ' $Self.Run(10);',
  20948. ' $Self.Run(10);',
  20949. ' $mod.THelper.Foo.call($Self, 1);',
  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. ' };',
  20956. ' return Result;',
  20957. ' };',
  20958. '});',
  20959. '']),
  20960. LinesToStr([ // $mod.$main
  20961. '']));
  20962. end;
  20963. procedure TTestModule.TestClassHelper_ClassMethod_Call;
  20964. begin
  20965. StartProgram(false);
  20966. Add([
  20967. 'type',
  20968. ' TObject = class',
  20969. ' class procedure Run(w: word = 10);',
  20970. ' end;',
  20971. ' THelper = class helper for TObject',
  20972. ' class function Foo(w: word = 1): word;',
  20973. ' end;',
  20974. 'class procedure TObject.Run(w: word);',
  20975. 'begin',
  20976. ' Foo;',
  20977. ' Foo();',
  20978. ' Self.Foo;',
  20979. ' Self.Foo();',
  20980. ' with Self do begin',
  20981. ' Foo;',
  20982. ' Foo();',
  20983. ' end;',
  20984. 'end;',
  20985. 'class function THelper.foo(w: word): word;',
  20986. 'begin',
  20987. ' Run;',
  20988. ' Run();',
  20989. ' Foo;',
  20990. ' Foo();',
  20991. ' Self.Foo;',
  20992. ' Self.Foo();',
  20993. ' with Self do begin',
  20994. ' Foo;',
  20995. ' Foo();',
  20996. ' end;',
  20997. 'end;',
  20998. 'var',
  20999. ' Obj: TObject;',
  21000. 'begin',
  21001. ' obj.Foo;',
  21002. ' obj.Foo();',
  21003. ' with obj do begin',
  21004. ' Foo;',
  21005. ' Foo();',
  21006. ' end;',
  21007. ' tobject.Foo;',
  21008. ' tobject.Foo();',
  21009. ' with tobject do begin',
  21010. ' Foo;',
  21011. ' Foo();',
  21012. ' end;',
  21013. '']);
  21014. ConvertProgram;
  21015. CheckSource('TestClassHelper_ClassMethod_Call',
  21016. LinesToStr([ // statements
  21017. 'rtl.createClass(this, "TObject", null, function () {',
  21018. ' this.$init = function () {',
  21019. ' };',
  21020. ' this.$final = function () {',
  21021. ' };',
  21022. ' this.Run = function (w) {',
  21023. ' $mod.THelper.Foo.call(this, 1);',
  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. ' };',
  21030. '});',
  21031. 'rtl.createHelper(this, "THelper", null, function () {',
  21032. ' this.Foo = function (w) {',
  21033. ' var Result = 0;',
  21034. ' this.Run(10);',
  21035. ' this.Run(10);',
  21036. ' $mod.THelper.Foo.call(this, 1);',
  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. ' return Result;',
  21043. ' };',
  21044. '});',
  21045. 'this.Obj = null;',
  21046. '']),
  21047. LinesToStr([ // $mod.$main
  21048. '$mod.THelper.Foo.call($mod.Obj.$class, 1);',
  21049. '$mod.THelper.Foo.call($mod.Obj.$class, 1);',
  21050. 'var $with = $mod.Obj;',
  21051. '$mod.THelper.Foo.call($with.$class, 1);',
  21052. '$mod.THelper.Foo.call($with.$class, 1);',
  21053. '$mod.THelper.Foo.call($mod.TObject, 1);',
  21054. '$mod.THelper.Foo.call($mod.TObject, 1);',
  21055. 'var $with1 = $mod.TObject;',
  21056. '$mod.THelper.Foo.call($mod.TObject, 1);',
  21057. '$mod.THelper.Foo.call($mod.TObject, 1);',
  21058. '']));
  21059. end;
  21060. procedure TTestModule.TestClassHelper_ClassOf;
  21061. begin
  21062. StartProgram(false);
  21063. Add([
  21064. 'type',
  21065. ' TObject = class',
  21066. ' end;',
  21067. ' TClass = class of TObject;',
  21068. ' THelper = class helper for TObject',
  21069. ' class function Foo(w: word = 1): word;',
  21070. ' end;',
  21071. 'class function THelper.foo(w: word): word;',
  21072. 'begin',
  21073. 'end;',
  21074. 'var',
  21075. ' c: TClass;',
  21076. 'begin',
  21077. ' c.Foo;',
  21078. ' c.Foo();',
  21079. ' with c do begin',
  21080. ' Foo;',
  21081. ' Foo();',
  21082. ' end;',
  21083. '']);
  21084. ConvertProgram;
  21085. CheckSource('TestClassHelper_ClassOf',
  21086. LinesToStr([ // statements
  21087. 'rtl.createClass(this, "TObject", null, function () {',
  21088. ' this.$init = function () {',
  21089. ' };',
  21090. ' this.$final = function () {',
  21091. ' };',
  21092. '});',
  21093. 'rtl.createHelper(this, "THelper", null, function () {',
  21094. ' this.Foo = function (w) {',
  21095. ' var Result = 0;',
  21096. ' return Result;',
  21097. ' };',
  21098. '});',
  21099. 'this.c = null;',
  21100. '']),
  21101. LinesToStr([ // $mod.$main
  21102. '$mod.THelper.Foo.call($mod.c, 1);',
  21103. '$mod.THelper.Foo.call($mod.c, 1);',
  21104. 'var $with = $mod.c;',
  21105. '$mod.THelper.Foo.call($with, 1);',
  21106. '$mod.THelper.Foo.call($with, 1);',
  21107. '']));
  21108. end;
  21109. procedure TTestModule.TestClassHelper_MethodRefObjFPC;
  21110. begin
  21111. StartProgram(false);
  21112. Add([
  21113. '{$mode objfpc}',
  21114. 'type',
  21115. ' TObject = class',
  21116. ' procedure DoIt;',
  21117. ' end;',
  21118. ' THelper = class helper for TObject',
  21119. ' procedure Fly(w: word = 1);',
  21120. ' class procedure Glide(w: word = 1);',
  21121. ' class procedure Run(w: word = 1); static;',
  21122. ' end;',
  21123. ' TFly = procedure(w: word) of object;',
  21124. ' TGlide = TFly;',
  21125. ' TRun = procedure(w: word);',
  21126. 'var',
  21127. ' f: TFly;',
  21128. ' g: TGlide;',
  21129. ' r: TRun;',
  21130. 'procedure TObject.DoIt;',
  21131. 'begin',
  21132. ' f:=@fly;',
  21133. ' g:=@glide;',
  21134. ' r:=@run;',
  21135. ' f:[email protected];',
  21136. ' g:[email protected];',
  21137. ' r:[email protected];',
  21138. ' with self do begin',
  21139. ' f:=@fly;',
  21140. ' g:=@glide;',
  21141. ' r:=@run;',
  21142. ' end;',
  21143. 'end;',
  21144. 'procedure THelper.fly(w: word);',
  21145. 'begin',
  21146. ' f:=@fly;',
  21147. ' g:=@glide;',
  21148. ' r:=@run;',
  21149. 'end;',
  21150. 'class procedure THelper.glide(w: word);',
  21151. 'begin',
  21152. ' g:=@glide;',
  21153. ' r:=@run;',
  21154. 'end;',
  21155. 'class procedure THelper.run(w: word);',
  21156. 'begin',
  21157. ' g:=@glide;',
  21158. ' r:=@run;',
  21159. 'end;',
  21160. 'var',
  21161. ' Obj: TObject;',
  21162. 'begin',
  21163. ' f:[email protected];',
  21164. ' g:[email protected];',
  21165. ' r:[email protected];',
  21166. ' with obj do begin',
  21167. ' f:=@fly;',
  21168. ' g:=@glide;',
  21169. ' r:=@run;',
  21170. ' end;',
  21171. ' g:[email protected];',
  21172. ' r:[email protected];',
  21173. ' with tobject do begin',
  21174. ' g:=@glide;',
  21175. ' r:=@run;',
  21176. ' end;',
  21177. '']);
  21178. ConvertProgram;
  21179. CheckSource('TestClassHelper_MethodRefObjFPC',
  21180. LinesToStr([ // statements
  21181. 'rtl.createClass(this, "TObject", null, function () {',
  21182. ' this.$init = function () {',
  21183. ' };',
  21184. ' this.$final = function () {',
  21185. ' };',
  21186. ' this.DoIt = function () {',
  21187. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  21188. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  21189. ' $mod.r = $mod.THelper.Run;',
  21190. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  21191. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  21192. ' $mod.r = $mod.THelper.Run;',
  21193. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  21194. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  21195. ' $mod.r = $mod.THelper.Run;',
  21196. ' };',
  21197. '});',
  21198. 'rtl.createHelper(this, "THelper", null, function () {',
  21199. ' this.Fly = function (w) {',
  21200. ' $mod.f = rtl.createCallback(this, $mod.THelper.Fly);',
  21201. ' $mod.g = rtl.createCallback(this.$class, $mod.THelper.Glide);',
  21202. ' $mod.r = $mod.THelper.Run;',
  21203. ' };',
  21204. ' this.Glide = function (w) {',
  21205. ' $mod.g = rtl.createCallback(this, $mod.THelper.Glide);',
  21206. ' $mod.r = $mod.THelper.Run;',
  21207. ' };',
  21208. ' this.Run = function (w) {',
  21209. ' $mod.g = rtl.createCallback($mod.THelper, $mod.THelper.Glide);',
  21210. ' $mod.r = $mod.THelper.Run;',
  21211. ' };',
  21212. '});',
  21213. 'this.f = null;',
  21214. 'this.g = null;',
  21215. 'this.r = null;',
  21216. 'this.Obj = null;',
  21217. '']),
  21218. LinesToStr([ // $mod.$main
  21219. '$mod.f = rtl.createCallback($mod.Obj, $mod.THelper.Fly);',
  21220. '$mod.g = rtl.createCallback($mod.Obj.$class, $mod.THelper.Glide);',
  21221. '$mod.r = $mod.THelper.Run;',
  21222. 'var $with = $mod.Obj;',
  21223. '$mod.f = rtl.createCallback($with, $mod.THelper.Fly);',
  21224. '$mod.g = rtl.createCallback($with.$class, $mod.THelper.Glide);',
  21225. '$mod.r = $mod.THelper.Run;',
  21226. '$mod.g = rtl.createCallback($mod.TObject, $mod.THelper.Glide);',
  21227. '$mod.r = $mod.THelper.Run;',
  21228. 'var $with1 = $mod.TObject;',
  21229. '$mod.g = rtl.createCallback($with1, $mod.THelper.Glide);',
  21230. '$mod.r = $mod.THelper.Run;',
  21231. '']));
  21232. end;
  21233. procedure TTestModule.TestClassHelper_Constructor;
  21234. begin
  21235. StartProgram(false);
  21236. Add([
  21237. 'type',
  21238. ' TObject = class',
  21239. ' constructor Create;',
  21240. ' end;',
  21241. ' TClass = class of TObject;',
  21242. ' THelper = class helper for TObject',
  21243. ' constructor NewHlp(w: word);',
  21244. ' end;',
  21245. 'var',
  21246. ' obj: TObject;',
  21247. ' c: TClass;',
  21248. 'constructor TObject.Create;',
  21249. 'begin',
  21250. ' NewHlp(2);', // normal call
  21251. ' tobject.NewHlp(3);', // new instance
  21252. ' c.newhlp(4);', // new instance
  21253. 'end;',
  21254. 'constructor THelper.NewHlp(w: word);',
  21255. 'begin',
  21256. ' create;', // normal call
  21257. ' tobject.create;', // new instance
  21258. ' NewHlp(2);', // normal call
  21259. ' tobject.NewHlp(3);', // new instance
  21260. ' c.newhlp(4);', // new instance
  21261. 'end;',
  21262. 'begin',
  21263. ' obj.newhlp(2);', // normal call
  21264. ' with Obj do newhlp(12);', // normal call
  21265. ' tobject.newhlp(3);', // new instance
  21266. ' with tobject do newhlp(13);', // new instance
  21267. ' c.newhlp(4);', // new instance
  21268. ' with c do newhlp(14);', // new instance
  21269. '']);
  21270. ConvertProgram;
  21271. CheckSource('TestClassHelper_Constructor',
  21272. LinesToStr([ // statements
  21273. 'rtl.createClass(this, "TObject", null, function () {',
  21274. ' this.$init = function () {',
  21275. ' };',
  21276. ' this.$final = function () {',
  21277. ' };',
  21278. ' this.Create = function () {',
  21279. ' $mod.THelper.NewHlp.call(this, 2);',
  21280. ' $mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  21281. ' $mod.c.$create($mod.THelper.NewHlp, [4]);',
  21282. ' return this;',
  21283. ' };',
  21284. '});',
  21285. 'rtl.createHelper(this, "THelper", null, function () {',
  21286. ' this.NewHlp = function (w) {',
  21287. ' this.Create();',
  21288. ' $mod.TObject.$create("Create");',
  21289. ' $mod.THelper.NewHlp.call(this, 2);',
  21290. ' $mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  21291. ' $mod.c.$create($mod.THelper.NewHlp, [4]);',
  21292. ' return this;',
  21293. ' };',
  21294. '});',
  21295. 'this.obj = null;',
  21296. 'this.c = null;',
  21297. '']),
  21298. LinesToStr([ // $mod.$main
  21299. '$mod.THelper.NewHlp.call($mod.obj, 2);',
  21300. 'var $with = $mod.obj;',
  21301. '$mod.THelper.NewHlp.call($with, 12);',
  21302. '$mod.TObject.$create($mod.THelper.NewHlp, [3]);',
  21303. 'var $with1 = $mod.TObject;',
  21304. '$with1.$create($mod.THelper.NewHlp, [13]);',
  21305. '$mod.c.$create($mod.THelper.NewHlp, [4]);',
  21306. 'var $with2 = $mod.c;',
  21307. '$with2.$create($mod.THelper.NewHlp, [14]);',
  21308. '']));
  21309. end;
  21310. procedure TTestModule.TestClassHelper_InheritedObjFPC;
  21311. begin
  21312. StartProgram(false);
  21313. Add([
  21314. 'type',
  21315. ' TObject = class',
  21316. ' procedure Fly;',
  21317. ' end;',
  21318. ' TObjHelper = class helper for TObject',
  21319. ' procedure Fly;',
  21320. ' end;',
  21321. ' TBird = class',
  21322. ' procedure Fly;',
  21323. ' end;',
  21324. ' TBirdHelper = class helper for TBird',
  21325. ' procedure Fly;',
  21326. ' procedure Walk(w: word);',
  21327. ' end;',
  21328. ' TEagleHelper = class helper(TBirdHelper) for TBird',
  21329. ' procedure Fly;',
  21330. ' procedure Walk(w: word);',
  21331. ' end;',
  21332. 'procedure Tobject.fly;',
  21333. 'begin',
  21334. ' inherited;', // ignore
  21335. 'end;',
  21336. 'procedure Tobjhelper.fly;',
  21337. 'begin',
  21338. ' {@TObject_Fly}inherited;',
  21339. ' inherited {@TObject_Fly}Fly;',
  21340. 'end;',
  21341. 'procedure Tbird.fly;',
  21342. 'begin',
  21343. ' {@TObjHelper_Fly}inherited;',
  21344. ' inherited {@TObjHelper_Fly}Fly;',
  21345. 'end;',
  21346. 'procedure Tbirdhelper.fly;',
  21347. 'begin',
  21348. ' {@TBird_Fly}inherited;',
  21349. ' inherited {@TBird_Fly}Fly;',
  21350. 'end;',
  21351. 'procedure Tbirdhelper.walk(w: word);',
  21352. 'begin',
  21353. 'end;',
  21354. 'procedure teagleHelper.fly;',
  21355. 'begin',
  21356. ' {@TBird_Fly}inherited;',
  21357. ' inherited {@TBird_Fly}Fly;',
  21358. 'end;',
  21359. 'procedure teagleHelper.walk(w: word);',
  21360. 'begin',
  21361. ' {@TBirdHelper_Walk}inherited;',
  21362. ' inherited {@TBirdHelper_Walk}Walk(3);',
  21363. 'end;',
  21364. 'begin',
  21365. '']);
  21366. ConvertProgram;
  21367. CheckSource('TestClassHelper_InheritedObjFPC',
  21368. LinesToStr([ // statements
  21369. 'rtl.createClass(this, "TObject", null, function () {',
  21370. ' this.$init = function () {',
  21371. ' };',
  21372. ' this.$final = function () {',
  21373. ' };',
  21374. ' this.Fly = function () {',
  21375. ' };',
  21376. '});',
  21377. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21378. ' this.Fly = function () {',
  21379. ' $mod.TObject.Fly.call(this);',
  21380. ' $mod.TObject.Fly.call(this);',
  21381. ' };',
  21382. '});',
  21383. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21384. ' this.Fly$1 = function () {',
  21385. ' $mod.TObjHelper.Fly.call(this);',
  21386. ' $mod.TObjHelper.Fly.call(this);',
  21387. ' };',
  21388. '});',
  21389. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  21390. ' this.Fly = function () {',
  21391. ' $mod.TBird.Fly$1.call(this);',
  21392. ' $mod.TBird.Fly$1.call(this);',
  21393. ' };',
  21394. ' this.Walk = function (w) {',
  21395. ' };',
  21396. '});',
  21397. 'rtl.createHelper(this, "TEagleHelper", this.TBirdHelper, function () {',
  21398. ' this.Fly$1 = function () {',
  21399. ' $mod.TBird.Fly$1.call(this);',
  21400. ' $mod.TBird.Fly$1.call(this);',
  21401. ' };',
  21402. ' this.Walk$1 = function (w) {',
  21403. ' $mod.TBirdHelper.Walk.apply(this, arguments);',
  21404. ' $mod.TBirdHelper.Walk.call(this, 3);',
  21405. ' };',
  21406. '});',
  21407. '']),
  21408. LinesToStr([ // $mod.$main
  21409. '']));
  21410. end;
  21411. procedure TTestModule.TestClassHelper_Property;
  21412. begin
  21413. StartProgram(false);
  21414. Add([
  21415. 'type',
  21416. ' TObject = class',
  21417. ' FSize: word;',
  21418. ' function GetSpeed: word;',
  21419. ' procedure SetSpeed(Value: word);',
  21420. ' end;',
  21421. ' TObjHelper = class helper for TObject',
  21422. ' function GetLeft: word;',
  21423. ' procedure SetLeft(Value: word);',
  21424. ' property Size: word read FSize write FSize;',
  21425. ' property Speed: word read GetSpeed write SetSpeed;',
  21426. ' property Left: word read GetLeft write SetLeft;',
  21427. ' end;',
  21428. ' TBird = class',
  21429. ' property NotRight: word read GetLeft write SetLeft;',
  21430. ' procedure DoIt;',
  21431. ' end;',
  21432. 'var',
  21433. ' b: TBird;',
  21434. 'function Tobject.GetSpeed: word;',
  21435. 'begin',
  21436. ' Size:=Size+11;',
  21437. ' Speed:=Speed+12;',
  21438. ' Result:=Left+13;',
  21439. ' Left:=13;',
  21440. ' Left:=Left+13;',
  21441. ' Self.Size:=Self.Size+21;',
  21442. ' Self.Speed:=Self.Speed+22;',
  21443. ' Self.Left:=Self.Left+23;',
  21444. ' with Self do begin',
  21445. ' Size:=Size+31;',
  21446. ' Speed:=Speed+32;',
  21447. ' Left:=Left+33;',
  21448. ' end;',
  21449. 'end;',
  21450. 'procedure Tobject.SetSpeed(Value: word);',
  21451. 'begin',
  21452. 'end;',
  21453. 'function TObjHelper.GetLeft: word;',
  21454. 'begin',
  21455. ' Size:=Size+11;',
  21456. ' Speed:=Speed+12;',
  21457. ' Left:=Left+13;',
  21458. ' Self.Size:=Self.Size+21;',
  21459. ' Self.Speed:=Self.Speed+22;',
  21460. ' Self.Left:=Self.Left+23;',
  21461. ' with Self do begin',
  21462. ' Size:=Size+31;',
  21463. ' Speed:=Speed+32;',
  21464. ' Left:=Left+33;',
  21465. ' end;',
  21466. 'end;',
  21467. 'procedure TObjHelper.SetLeft(Value: word);',
  21468. 'begin',
  21469. 'end;',
  21470. 'procedure TBird.DoIt;',
  21471. 'begin',
  21472. ' NotRight:=NotRight+11;',
  21473. ' Self.NotRight:=Self.NotRight+21;',
  21474. ' with Self do begin',
  21475. ' NotRight:=NotRight+31;',
  21476. ' end;',
  21477. 'end;',
  21478. 'begin',
  21479. ' b.Size:=b.Size+11;',
  21480. ' b.Speed:=b.Speed+12;',
  21481. ' b.Left:=b.Left+13;',
  21482. ' b.NotRight:=b.NotRight+14;',
  21483. ' with b do begin',
  21484. ' Size:=Size+31;',
  21485. ' Speed:=Speed+32;',
  21486. ' Left:=Left+33;',
  21487. ' NotRight:=NotRight+34;',
  21488. ' end;',
  21489. '']);
  21490. ConvertProgram;
  21491. CheckSource('TestClassHelper_Property',
  21492. LinesToStr([ // statements
  21493. 'rtl.createClass(this, "TObject", null, function () {',
  21494. ' this.$init = function () {',
  21495. ' this.FSize = 0;',
  21496. ' };',
  21497. ' this.$final = function () {',
  21498. ' };',
  21499. ' this.GetSpeed = function () {',
  21500. ' var Result = 0;',
  21501. ' this.FSize = this.FSize + 11;',
  21502. ' this.SetSpeed(this.GetSpeed() + 12);',
  21503. ' Result = $mod.TObjHelper.GetLeft.call(this) + 13;',
  21504. ' $mod.TObjHelper.SetLeft.call(this, 13);',
  21505. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  21506. ' this.FSize = this.FSize + 21;',
  21507. ' this.SetSpeed(this.GetSpeed() + 22);',
  21508. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  21509. ' this.FSize = this.FSize + 31;',
  21510. ' this.SetSpeed(this.GetSpeed() + 32);',
  21511. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  21512. ' return Result;',
  21513. ' };',
  21514. ' this.SetSpeed = function (Value) {',
  21515. ' };',
  21516. '});',
  21517. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21518. ' this.GetLeft = function () {',
  21519. ' var Result = 0;',
  21520. ' this.FSize = this.FSize + 11;',
  21521. ' this.SetSpeed(this.GetSpeed() + 12);',
  21522. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  21523. ' this.FSize = this.FSize + 21;',
  21524. ' this.SetSpeed(this.GetSpeed() + 22);',
  21525. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  21526. ' this.FSize = this.FSize + 31;',
  21527. ' this.SetSpeed(this.GetSpeed() + 32);',
  21528. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  21529. ' return Result;',
  21530. ' };',
  21531. ' this.SetLeft = function (Value) {',
  21532. ' };',
  21533. '});',
  21534. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21535. ' this.DoIt = function () {',
  21536. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 11);',
  21537. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 21);',
  21538. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 31);',
  21539. ' };',
  21540. '});',
  21541. 'this.b = null;',
  21542. '']),
  21543. LinesToStr([ // $mod.$main
  21544. '$mod.b.FSize = $mod.b.FSize + 11;',
  21545. '$mod.b.SetSpeed($mod.b.GetSpeed() + 12);',
  21546. '$mod.TObjHelper.SetLeft.call($mod.b, $mod.TObjHelper.GetLeft.call($mod.b) + 13);',
  21547. '$mod.TObjHelper.SetLeft.call($mod.b, $mod.TObjHelper.GetLeft.call($mod.b) + 14);',
  21548. 'var $with = $mod.b;',
  21549. '$with.FSize = $with.FSize + 31;',
  21550. '$with.SetSpeed($with.GetSpeed() + 32);',
  21551. '$mod.TObjHelper.SetLeft.call($with, $mod.TObjHelper.GetLeft.call($with) + 33);',
  21552. '$mod.TObjHelper.SetLeft.call($with, $mod.TObjHelper.GetLeft.call($with) + 34);',
  21553. '']));
  21554. end;
  21555. procedure TTestModule.TestClassHelper_Property_Array;
  21556. begin
  21557. StartProgram(false);
  21558. Add([
  21559. 'type',
  21560. ' TObject = class',
  21561. ' function GetSpeed(Index: boolean): word;',
  21562. ' procedure SetSpeed(Index: boolean; Value: word);',
  21563. ' end;',
  21564. ' TObjHelper = class helper for TObject',
  21565. ' function GetSize(Index: boolean): word;',
  21566. ' procedure SetSize(Index: boolean; Value: word);',
  21567. ' property Size[Index: boolean]: word read GetSize write SetSize;',
  21568. ' property Speed[Index: boolean]: word read GetSpeed write SetSpeed;',
  21569. ' end;',
  21570. ' TBird = class',
  21571. ' property Items[Index: boolean]: word read GetSize write SetSize;',
  21572. ' procedure DoIt;',
  21573. ' end;',
  21574. 'var',
  21575. ' b: TBird;',
  21576. 'function Tobject.GetSpeed(Index: boolean): word;',
  21577. 'begin',
  21578. ' Result:=Size[false];',
  21579. ' Size[true]:=Size[false]+11;',
  21580. ' Speed[true]:=Speed[false]+12;',
  21581. ' Self.Size[true]:=Self.Size[false]+21;',
  21582. ' Self.Speed[true]:=Self.Speed[false]+22;',
  21583. ' with Self do begin',
  21584. ' Size[true]:=Size[false]+31;',
  21585. ' Speed[true]:=Speed[false]+32;',
  21586. ' end;',
  21587. 'end;',
  21588. 'procedure Tobject.SetSpeed(Index: boolean; Value: word);',
  21589. 'begin',
  21590. 'end;',
  21591. 'function TObjHelper.GetSize(Index: boolean): word;',
  21592. 'begin',
  21593. ' Size[true]:=Size[false]+11;',
  21594. ' Speed[true]:=Speed[false]+12;',
  21595. ' Self.Size[true]:=Self.Size[false]+21;',
  21596. ' Self.Speed[true]:=Self.Speed[false]+22;',
  21597. ' with Self do begin',
  21598. ' Size[true]:=Size[false]+31;',
  21599. ' Speed[true]:=Speed[false]+32;',
  21600. ' end;',
  21601. 'end;',
  21602. 'procedure TObjHelper.SetSize(Index: boolean; Value: word);',
  21603. 'begin',
  21604. 'end;',
  21605. 'procedure TBird.DoIt;',
  21606. 'begin',
  21607. ' Items[true]:=Items[false]+11;',
  21608. ' Self.Items[true]:=Self.Items[false]+21;',
  21609. ' with Self do Items[true]:=Items[false]+31;',
  21610. 'end;',
  21611. 'begin',
  21612. ' b.Size[true]:=b.Size[false]+11;',
  21613. ' b.Speed[true]:=b.Speed[false]+12;',
  21614. ' b.Items[true]:=b.Items[false]+13;',
  21615. ' with b do begin',
  21616. ' Size[true]:=Size[false]+21;',
  21617. ' Speed[true]:=Speed[false]+22;',
  21618. ' Items[true]:=Items[false]+23;',
  21619. ' end;',
  21620. '']);
  21621. ConvertProgram;
  21622. CheckSource('TestClassHelper_Property_Array',
  21623. LinesToStr([ // statements
  21624. 'rtl.createClass(this, "TObject", null, function () {',
  21625. ' this.$init = function () {',
  21626. ' };',
  21627. ' this.$final = function () {',
  21628. ' };',
  21629. ' this.GetSpeed = function (Index) {',
  21630. ' var Result = 0;',
  21631. ' Result = $mod.TObjHelper.GetSize.call(this, false);',
  21632. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  21633. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  21634. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  21635. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  21636. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  21637. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  21638. ' return Result;',
  21639. ' };',
  21640. ' this.SetSpeed = function (Index, Value) {',
  21641. ' };',
  21642. '});',
  21643. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21644. ' this.GetSize = function (Index) {',
  21645. ' var Result = 0;',
  21646. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  21647. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  21648. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  21649. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  21650. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  21651. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  21652. ' return Result;',
  21653. ' };',
  21654. ' this.SetSize = function (Index, Value) {',
  21655. ' };',
  21656. '});',
  21657. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21658. ' this.DoIt = function () {',
  21659. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  21660. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  21661. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  21662. ' };',
  21663. '});',
  21664. 'this.b = null;',
  21665. '']),
  21666. LinesToStr([ // $mod.$main
  21667. '$mod.TObjHelper.SetSize.call($mod.b, true, $mod.TObjHelper.GetSize.call($mod.b, false) + 11);',
  21668. '$mod.b.SetSpeed(true, $mod.b.GetSpeed(false) + 12);',
  21669. '$mod.TObjHelper.SetSize.call($mod.b, true, $mod.TObjHelper.GetSize.call($mod.b, false) + 13);',
  21670. 'var $with = $mod.b;',
  21671. '$mod.TObjHelper.SetSize.call($with, true, $mod.TObjHelper.GetSize.call($with, false) + 21);',
  21672. '$with.SetSpeed(true, $with.GetSpeed(false) + 22);',
  21673. '$mod.TObjHelper.SetSize.call($with, true, $mod.TObjHelper.GetSize.call($with, false) + 23);',
  21674. '']));
  21675. end;
  21676. procedure TTestModule.TestClassHelper_Property_Array_Default;
  21677. begin
  21678. StartProgram(false);
  21679. Add([
  21680. 'type',
  21681. ' TObject = class',
  21682. ' function GetSpeed(Index: boolean): word;',
  21683. ' procedure SetSpeed(Index: boolean; Value: word);',
  21684. ' end;',
  21685. ' TObjHelper = class helper for TObject',
  21686. ' property Speed[Index: boolean]: word read GetSpeed write SetSpeed; default;',
  21687. ' end;',
  21688. ' TBird = class',
  21689. ' end;',
  21690. ' TBirdHelper = class helper for TBird',
  21691. ' function GetSize(Index: word): boolean;',
  21692. ' procedure SetSize(Index: word; Value: boolean);',
  21693. ' property Size[Index: word]: boolean read GetSize write SetSize; default;',
  21694. ' end;',
  21695. 'function Tobject.GetSpeed(Index: boolean): word;',
  21696. 'begin',
  21697. ' Self[true]:=Self[false]+1;',
  21698. 'end;',
  21699. 'procedure Tobject.SetSpeed(Index: boolean; Value: word);',
  21700. 'begin',
  21701. 'end;',
  21702. 'function TBirdHelper.GetSize(Index: word): boolean;',
  21703. 'begin',
  21704. ' Self[1]:=not Self[2];',
  21705. 'end;',
  21706. 'procedure TBirdHelper.SetSize(Index: word; Value: boolean);',
  21707. 'begin',
  21708. 'end;',
  21709. 'var',
  21710. ' o: TObject;',
  21711. ' b: TBird;',
  21712. 'begin',
  21713. ' o[true]:=o[false]+1;',
  21714. ' b[3]:=not b[4];',
  21715. '']);
  21716. ConvertProgram;
  21717. CheckSource('TestClassHelper_Property_Array_Default',
  21718. LinesToStr([ // statements
  21719. 'rtl.createClass(this, "TObject", null, function () {',
  21720. ' this.$init = function () {',
  21721. ' };',
  21722. ' this.$final = function () {',
  21723. ' };',
  21724. ' this.GetSpeed = function (Index) {',
  21725. ' var Result = 0;',
  21726. ' this.SetSpeed(true, this.GetSpeed(false) + 1);',
  21727. ' return Result;',
  21728. ' };',
  21729. ' this.SetSpeed = function (Index, Value) {',
  21730. ' };',
  21731. '});',
  21732. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21733. '});',
  21734. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21735. '});',
  21736. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  21737. ' this.GetSize = function (Index) {',
  21738. ' var Result = false;',
  21739. ' $mod.TBirdHelper.SetSize.call(this, 1, !$mod.TBirdHelper.GetSize.call(this, 2));',
  21740. ' return Result;',
  21741. ' };',
  21742. ' this.SetSize = function (Index, Value) {',
  21743. ' };',
  21744. '});',
  21745. 'this.o = null;',
  21746. 'this.b = null;',
  21747. '']),
  21748. LinesToStr([ // $mod.$main
  21749. '$mod.o.SetSpeed(true, $mod.o.GetSpeed(false) + 1);',
  21750. '$mod.TBirdHelper.SetSize.call($mod.b, 3, !$mod.TBirdHelper.GetSize.call($mod.b, 4));',
  21751. '']));
  21752. end;
  21753. procedure TTestModule.TestClassHelper_Property_Array_DefaultDefault;
  21754. begin
  21755. StartProgram(false);
  21756. Add([
  21757. 'type',
  21758. ' TObject = class',
  21759. ' end;',
  21760. ' TObjHelper = class helper for TObject',
  21761. ' function GetItems(Index: word): TObject;',
  21762. ' procedure SetItems(Index: word; Value: TObject);',
  21763. ' property Items[Index: word]: TObject read GetItems write SetItems; default;',
  21764. ' end;',
  21765. 'function Tobjhelper.GetItems(Index: word): TObject;',
  21766. 'begin',
  21767. ' Self[1][2]:=Self[3][4];',
  21768. 'end;',
  21769. 'procedure Tobjhelper.SetItems(Index: word; Value: TObject);',
  21770. 'begin',
  21771. 'end;',
  21772. 'var',
  21773. ' o: TObject;',
  21774. 'begin',
  21775. ' o[1][2]:=o[3][4];',
  21776. '']);
  21777. ConvertProgram;
  21778. CheckSource('TestClassHelper_Property_Array_DefaultDefault',
  21779. LinesToStr([ // statements
  21780. 'rtl.createClass(this, "TObject", null, function () {',
  21781. ' this.$init = function () {',
  21782. ' };',
  21783. ' this.$final = function () {',
  21784. ' };',
  21785. '});',
  21786. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21787. ' this.GetItems = function (Index) {',
  21788. ' var Result = null;',
  21789. ' $mod.TObjHelper.SetItems.call($mod.TObjHelper.GetItems.call(this, 1), 2, $mod.TObjHelper.GetItems.call($mod.TObjHelper.GetItems.call(this, 3), 4));',
  21790. ' return Result;',
  21791. ' };',
  21792. ' this.SetItems = function (Index, Value) {',
  21793. ' };',
  21794. '});',
  21795. 'this.o = null;',
  21796. '']),
  21797. LinesToStr([ // $mod.$main
  21798. '$mod.TObjHelper.SetItems.call($mod.TObjHelper.GetItems.call($mod.o, 1), 2, $mod.TObjHelper.GetItems.call($mod.TObjHelper.GetItems.call($mod.o, 3), 4));',
  21799. '']));
  21800. end;
  21801. procedure TTestModule.TestClassHelper_ClassProperty;
  21802. begin
  21803. StartProgram(false);
  21804. Add([
  21805. 'type',
  21806. ' TObject = class',
  21807. ' class var FSize: word;',
  21808. ' class function GetSpeed: word;',
  21809. ' class procedure SetSpeed(Value: word); virtual; abstract;',
  21810. ' end;',
  21811. ' TObjHelper = class helper for TObject',
  21812. ' class function GetLeft: word;',
  21813. ' class procedure SetLeft(Value: word);',
  21814. ' class property Size: word read FSize write FSize;',
  21815. ' class property Speed: word read GetSpeed write SetSpeed;',
  21816. ' class property Left: word read GetLeft write SetLeft;',
  21817. ' end;',
  21818. ' TBird = class',
  21819. ' class property NotRight: word read GetLeft write SetLeft;',
  21820. ' class procedure DoIt;',
  21821. ' end;',
  21822. ' TBirdClass = class of TBird;',
  21823. 'class function Tobject.GetSpeed: word;',
  21824. 'begin',
  21825. ' Size:=Size+11;',
  21826. ' Speed:=Speed+12;',
  21827. ' Left:=Left+13;',
  21828. ' Self.Size:=Self.Size+21;',
  21829. ' Self.Speed:=Self.Speed+22;',
  21830. ' Self.Left:=Self.Left+23;',
  21831. ' with Self do begin',
  21832. ' Size:=Size+31;',
  21833. ' Speed:=Speed+32;',
  21834. ' Left:=Left+33;',
  21835. ' end;',
  21836. 'end;',
  21837. 'class function TObjHelper.GetLeft: word;',
  21838. 'begin',
  21839. ' Size:=Size+11;',
  21840. ' Speed:=Speed+12;',
  21841. ' Left:=Left+13;',
  21842. ' Self.Size:=Self.Size+21;',
  21843. ' Self.Speed:=Self.Speed+22;',
  21844. ' Self.Left:=Self.Left+23;',
  21845. ' with Self do begin',
  21846. ' Size:=Size+31;',
  21847. ' Speed:=Speed+32;',
  21848. ' Left:=Left+33;',
  21849. ' end;',
  21850. 'end;',
  21851. 'class procedure TObjHelper.SetLeft(Value: word);',
  21852. 'begin',
  21853. 'end;',
  21854. 'class procedure TBird.DoIt;',
  21855. 'begin',
  21856. ' NotRight:=NotRight+11;',
  21857. ' Self.NotRight:=Self.NotRight+21;',
  21858. ' with Self do NotRight:=NotRight+31;',
  21859. 'end;',
  21860. 'var',
  21861. ' b: TBird;',
  21862. ' c: TBirdClass;',
  21863. 'begin',
  21864. ' b.Size:=b.Size+11;',
  21865. ' b.Speed:=b.Speed+12;',
  21866. ' b.Left:=b.Left+13;',
  21867. ' b.NotRight:=b.NotRight+14;',
  21868. ' with b do begin',
  21869. ' Size:=Size+31;',
  21870. ' Speed:=Speed+32;',
  21871. ' Left:=Left+33;',
  21872. ' NotRight:=NotRight+34;',
  21873. ' end;',
  21874. ' c.Size:=c.Size+11;',
  21875. ' c.Speed:=c.Speed+12;',
  21876. ' c.Left:=c.Left+13;',
  21877. ' c.NotRight:=c.NotRight+14;',
  21878. ' with c do begin',
  21879. ' Size:=Size+31;',
  21880. ' Speed:=Speed+32;',
  21881. ' Left:=Left+33;',
  21882. ' NotRight:=NotRight+34;',
  21883. ' end;',
  21884. ' tbird.Size:=tbird.Size+11;',
  21885. ' tbird.Speed:=tbird.Speed+12;',
  21886. ' tbird.Left:=tbird.Left+13;',
  21887. ' tbird.NotRight:=tbird.NotRight+14;',
  21888. ' with tbird do begin',
  21889. ' Size:=Size+31;',
  21890. ' Speed:=Speed+32;',
  21891. ' Left:=Left+33;',
  21892. ' NotRight:=NotRight+34;',
  21893. ' end;',
  21894. '']);
  21895. ConvertProgram;
  21896. CheckSource('TestClassHelper_ClassProperty',
  21897. LinesToStr([ // statements
  21898. 'rtl.createClass(this, "TObject", null, function () {',
  21899. ' this.FSize = 0;',
  21900. ' this.$init = function () {',
  21901. ' };',
  21902. ' this.$final = function () {',
  21903. ' };',
  21904. ' this.GetSpeed = function () {',
  21905. ' var Result = 0;',
  21906. ' $mod.TObject.FSize = this.FSize + 11;',
  21907. ' this.SetSpeed(this.GetSpeed() + 12);',
  21908. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  21909. ' $mod.TObject.FSize = this.FSize + 21;',
  21910. ' this.SetSpeed(this.GetSpeed() + 22);',
  21911. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  21912. ' $mod.TObject.FSize = this.FSize + 31;',
  21913. ' this.SetSpeed(this.GetSpeed() + 32);',
  21914. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  21915. ' return Result;',
  21916. ' };',
  21917. '});',
  21918. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  21919. ' this.GetLeft = function () {',
  21920. ' var Result = 0;',
  21921. ' $mod.TObject.FSize = this.FSize + 11;',
  21922. ' this.SetSpeed(this.GetSpeed() + 12);',
  21923. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 13);',
  21924. ' $mod.TObject.FSize = this.FSize + 21;',
  21925. ' this.SetSpeed(this.GetSpeed() + 22);',
  21926. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 23);',
  21927. ' $mod.TObject.FSize = this.FSize + 31;',
  21928. ' this.SetSpeed(this.GetSpeed() + 32);',
  21929. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 33);',
  21930. ' return Result;',
  21931. ' };',
  21932. ' this.SetLeft = function (Value) {',
  21933. ' };',
  21934. '});',
  21935. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  21936. ' this.DoIt = function () {',
  21937. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 11);',
  21938. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 21);',
  21939. ' $mod.TObjHelper.SetLeft.call(this, $mod.TObjHelper.GetLeft.call(this) + 31);',
  21940. ' };',
  21941. '});',
  21942. 'this.b = null;',
  21943. 'this.c = null;',
  21944. '']),
  21945. LinesToStr([ // $mod.$main
  21946. '$mod.TObject.FSize = $mod.b.FSize + 11;',
  21947. '$mod.b.$class.SetSpeed($mod.b.$class.GetSpeed() + 12);',
  21948. '$mod.TObjHelper.SetLeft.call($mod.b.$class, $mod.TObjHelper.GetLeft.call($mod.b.$class) + 13);',
  21949. '$mod.TObjHelper.SetLeft.call($mod.b.$class, $mod.TObjHelper.GetLeft.call($mod.b.$class) + 14);',
  21950. 'var $with = $mod.b;',
  21951. '$mod.TObject.FSize = $with.FSize + 31;',
  21952. '$with.$class.SetSpeed($with.$class.GetSpeed() + 32);',
  21953. '$mod.TObjHelper.SetLeft.call($with.$class, $mod.TObjHelper.GetLeft.call($with.$class) + 33);',
  21954. '$mod.TObjHelper.SetLeft.call($with.$class, $mod.TObjHelper.GetLeft.call($with.$class) + 34);',
  21955. '$mod.TObject.FSize = $mod.c.FSize + 11;',
  21956. '$mod.c.SetSpeed($mod.c.GetSpeed() + 12);',
  21957. '$mod.TObjHelper.SetLeft.call($mod.c, $mod.TObjHelper.GetLeft.call($mod.c) + 13);',
  21958. '$mod.TObjHelper.SetLeft.call($mod.c, $mod.TObjHelper.GetLeft.call($mod.c) + 14);',
  21959. 'var $with1 = $mod.c;',
  21960. '$mod.TObject.FSize = $with1.FSize + 31;',
  21961. '$with1.SetSpeed($with1.GetSpeed() + 32);',
  21962. '$mod.TObjHelper.SetLeft.call($with1, $mod.TObjHelper.GetLeft.call($with1) + 33);',
  21963. '$mod.TObjHelper.SetLeft.call($with1, $mod.TObjHelper.GetLeft.call($with1) + 34);',
  21964. '$mod.TObject.FSize = $mod.TBird.FSize + 11;',
  21965. '$mod.TBird.SetSpeed($mod.TBird.GetSpeed() + 12);',
  21966. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 13);',
  21967. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 14);',
  21968. 'var $with2 = $mod.TBird;',
  21969. '$mod.TObject.FSize = $with2.FSize + 31;',
  21970. '$with2.SetSpeed($with2.GetSpeed() + 32);',
  21971. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 33);',
  21972. '$mod.TObjHelper.SetLeft.call($mod.TBird, $mod.TObjHelper.GetLeft.call($mod.TBird) + 34);',
  21973. '']));
  21974. end;
  21975. procedure TTestModule.TestClassHelper_ClassPropertyStatic;
  21976. begin
  21977. StartProgram(false);
  21978. Add([
  21979. 'type',
  21980. ' TObject = class',
  21981. ' class function GetSpeed: word; static;',
  21982. ' class procedure SetSpeed(Value: word); static;',
  21983. ' end;',
  21984. ' TObjHelper = class helper for TObject',
  21985. ' class function GetLeft: word; static;',
  21986. ' class procedure SetLeft(Value: word); static;',
  21987. ' class property Speed: word read GetSpeed write SetSpeed;',
  21988. ' class property Left: word read GetLeft write SetLeft;',
  21989. ' end;',
  21990. ' TBird = class',
  21991. ' class property NotRight: word read GetLeft write SetLeft;',
  21992. ' class procedure DoIt; static;',
  21993. ' class procedure DoSome;',
  21994. ' end;',
  21995. ' TBirdClass = class of TBird;',
  21996. 'class function Tobject.GetSpeed: word;',
  21997. 'begin',
  21998. ' Speed:=Speed+12;',
  21999. ' Left:=Left+13;',
  22000. 'end;',
  22001. 'class procedure TObject.SetSpeed(Value: word);',
  22002. 'begin',
  22003. 'end;',
  22004. 'class function TObjHelper.GetLeft: word;',
  22005. 'begin',
  22006. ' Speed:=Speed+12;',
  22007. ' Left:=Left+13;',
  22008. 'end;',
  22009. 'class procedure TObjHelper.SetLeft(Value: word);',
  22010. 'begin',
  22011. 'end;',
  22012. 'class procedure TBird.DoIt;',
  22013. 'begin',
  22014. ' NotRight:=NotRight+11;',
  22015. 'end;',
  22016. 'class procedure TBird.DoSome;',
  22017. 'begin',
  22018. ' Speed:=Speed+12;',
  22019. ' Left:=Left+13;',
  22020. ' Self.Speed:=Self.Speed+22;',
  22021. ' Self.Left:=Self.Left+23;',
  22022. ' with Self do begin',
  22023. ' Speed:=Speed+32;',
  22024. ' Left:=Left+33;',
  22025. ' end;',
  22026. ' NotRight:=NotRight+11;',
  22027. ' Self.NotRight:=Self.NotRight+21;',
  22028. ' with Self do NotRight:=NotRight+31;',
  22029. 'end;',
  22030. 'var',
  22031. ' b: TBird;',
  22032. ' c: TBirdClass;',
  22033. 'begin',
  22034. ' b.Speed:=b.Speed+12;',
  22035. ' b.Left:=b.Left+13;',
  22036. ' b.NotRight:=b.NotRight+14;',
  22037. ' with b do begin',
  22038. ' Speed:=Speed+32;',
  22039. ' Left:=Left+33;',
  22040. ' NotRight:=NotRight+34;',
  22041. ' end;',
  22042. ' c.Speed:=c.Speed+12;',
  22043. ' c.Left:=c.Left+13;',
  22044. ' c.NotRight:=c.NotRight+14;',
  22045. ' with c do begin',
  22046. ' Speed:=Speed+32;',
  22047. ' Left:=Left+33;',
  22048. ' NotRight:=NotRight+34;',
  22049. ' end;',
  22050. ' tbird.Speed:=tbird.Speed+12;',
  22051. ' tbird.Left:=tbird.Left+13;',
  22052. ' tbird.NotRight:=tbird.NotRight+14;',
  22053. ' with tbird do begin',
  22054. ' Speed:=Speed+32;',
  22055. ' Left:=Left+33;',
  22056. ' NotRight:=NotRight+34;',
  22057. ' end;',
  22058. '']);
  22059. ConvertProgram;
  22060. CheckSource('TestClassHelper_ClassPropertyStatic',
  22061. LinesToStr([ // statements
  22062. 'rtl.createClass(this, "TObject", null, function () {',
  22063. ' this.$init = function () {',
  22064. ' };',
  22065. ' this.$final = function () {',
  22066. ' };',
  22067. ' this.GetSpeed = function () {',
  22068. ' var Result = 0;',
  22069. ' $mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  22070. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  22071. ' return Result;',
  22072. ' };',
  22073. ' this.SetSpeed = function (Value) {',
  22074. ' };',
  22075. '});',
  22076. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  22077. ' this.GetLeft = function () {',
  22078. ' var Result = 0;',
  22079. ' $mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  22080. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  22081. ' return Result;',
  22082. ' };',
  22083. ' this.SetLeft = function (Value) {',
  22084. ' };',
  22085. '});',
  22086. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  22087. ' this.DoIt = function () {',
  22088. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 11);',
  22089. ' };',
  22090. ' this.DoSome = function () {',
  22091. ' this.SetSpeed(this.GetSpeed() + 12);',
  22092. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  22093. ' this.SetSpeed(this.GetSpeed() + 22);',
  22094. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 23);',
  22095. ' this.SetSpeed(this.GetSpeed() + 32);',
  22096. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  22097. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 11);',
  22098. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 21);',
  22099. ' $mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 31);',
  22100. ' };',
  22101. '});',
  22102. 'this.b = null;',
  22103. 'this.c = null;',
  22104. '']),
  22105. LinesToStr([ // $mod.$main
  22106. '$mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  22107. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  22108. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  22109. 'var $with = $mod.b;',
  22110. '$with.SetSpeed($with.GetSpeed() + 32);',
  22111. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  22112. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  22113. '$mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  22114. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  22115. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  22116. 'var $with1 = $mod.c;',
  22117. '$with1.SetSpeed($with1.GetSpeed() + 32);',
  22118. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  22119. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  22120. '$mod.TObject.SetSpeed($mod.TObject.GetSpeed() + 12);',
  22121. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 13);',
  22122. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 14);',
  22123. 'var $with2 = $mod.TBird;',
  22124. '$with2.SetSpeed($with2.GetSpeed() + 32);',
  22125. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 33);',
  22126. '$mod.TObjHelper.SetLeft($mod.TObjHelper.GetLeft() + 34);',
  22127. '']));
  22128. end;
  22129. procedure TTestModule.TestClassHelper_ClassProperty_Array;
  22130. begin
  22131. StartProgram(false);
  22132. Add([
  22133. 'type',
  22134. ' TObject = class',
  22135. ' class function GetSpeed(Index: boolean): word;',
  22136. ' class procedure SetSpeed(Index: boolean; Value: word); virtual; abstract;',
  22137. ' end;',
  22138. ' TObjHelper = class helper for TObject',
  22139. ' class function GetSize(Index: boolean): word;',
  22140. ' class procedure SetSize(Index: boolean; Value: word);',
  22141. ' class property Size[Index: boolean]: word read GetSize write SetSize;',
  22142. ' class property Speed[Index: boolean]: word read GetSpeed write SetSpeed;',
  22143. ' end;',
  22144. ' TBird = class',
  22145. ' class property Items[Index: boolean]: word read GetSize write SetSize;',
  22146. ' class procedure DoIt;',
  22147. ' end;',
  22148. ' TBirdClass = class of TBird;',
  22149. 'class function Tobject.GetSpeed(Index: boolean): word;',
  22150. 'begin',
  22151. ' Size[true]:=Size[false]+11;',
  22152. ' Speed[true]:=Speed[false]+12;',
  22153. ' Self.Size[true]:=Self.Size[false]+21;',
  22154. ' Self.Speed[true]:=Self.Speed[false]+22;',
  22155. ' with Self do begin',
  22156. ' Size[true]:=Size[false]+31;',
  22157. ' Speed[true]:=Speed[false]+32;',
  22158. ' end;',
  22159. 'end;',
  22160. 'class function TObjHelper.GetSize(Index: boolean): word;',
  22161. 'begin',
  22162. ' Size[true]:=Size[false]+11;',
  22163. ' Speed[true]:=Speed[false]+12;',
  22164. ' Self.Size[true]:=Self.Size[false]+21;',
  22165. ' Self.Speed[true]:=Self.Speed[false]+22;',
  22166. ' with Self do begin',
  22167. ' Size[true]:=Size[false]+31;',
  22168. ' Speed[true]:=Speed[false]+32;',
  22169. ' end;',
  22170. 'end;',
  22171. 'class procedure TObjHelper.SetSize(Index: boolean; Value: word);',
  22172. 'begin',
  22173. 'end;',
  22174. 'class procedure TBird.DoIt;',
  22175. 'begin',
  22176. ' Items[true]:=Items[false]+11;',
  22177. ' Self.Items[true]:=Self.Items[false]+21;',
  22178. ' with Self do Items[true]:=Items[false]+31;',
  22179. 'end;',
  22180. 'var',
  22181. ' b: TBird;',
  22182. ' c: TBirdClass;',
  22183. 'begin',
  22184. ' b.Size[true]:=b.Size[false]+11;',
  22185. ' b.Speed[true]:=b.Speed[false]+12;',
  22186. ' b.Items[true]:=b.Items[false]+13;',
  22187. ' with b do begin',
  22188. ' Size[true]:=Size[false]+21;',
  22189. ' Speed[true]:=Speed[false]+22;',
  22190. ' Items[true]:=Items[false]+23;',
  22191. ' end;',
  22192. ' c.Size[true]:=c.Size[false]+11;',
  22193. ' c.Speed[true]:=c.Speed[false]+12;',
  22194. ' c.Items[true]:=c.Items[false]+13;',
  22195. ' with c do begin',
  22196. ' Size[true]:=Size[false]+21;',
  22197. ' Speed[true]:=Speed[false]+22;',
  22198. ' Items[true]:=Items[false]+23;',
  22199. ' end;',
  22200. ' TBird.Size[true]:=TBird.Size[false]+11;',
  22201. ' TBird.Speed[true]:=TBird.Speed[false]+12;',
  22202. ' TBird.Items[true]:=TBird.Items[false]+13;',
  22203. ' with TBird do begin',
  22204. ' Size[true]:=Size[false]+21;',
  22205. ' Speed[true]:=Speed[false]+22;',
  22206. ' Items[true]:=Items[false]+23;',
  22207. ' end;',
  22208. '']);
  22209. ConvertProgram;
  22210. CheckSource('TestClassHelper_ClassProperty_Array',
  22211. LinesToStr([ // statements
  22212. 'rtl.createClass(this, "TObject", null, function () {',
  22213. ' this.$init = function () {',
  22214. ' };',
  22215. ' this.$final = function () {',
  22216. ' };',
  22217. ' this.GetSpeed = function (Index) {',
  22218. ' var Result = 0;',
  22219. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  22220. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  22221. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  22222. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  22223. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  22224. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  22225. ' return Result;',
  22226. ' };',
  22227. '});',
  22228. 'rtl.createHelper(this, "TObjHelper", null, function () {',
  22229. ' this.GetSize = function (Index) {',
  22230. ' var Result = 0;',
  22231. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  22232. ' this.SetSpeed(true, this.GetSpeed(false) + 12);',
  22233. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  22234. ' this.SetSpeed(true, this.GetSpeed(false) + 22);',
  22235. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  22236. ' this.SetSpeed(true, this.GetSpeed(false) + 32);',
  22237. ' return Result;',
  22238. ' };',
  22239. ' this.SetSize = function (Index, Value) {',
  22240. ' };',
  22241. '});',
  22242. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  22243. ' this.DoIt = function () {',
  22244. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 11);',
  22245. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 21);',
  22246. ' $mod.TObjHelper.SetSize.call(this, true, $mod.TObjHelper.GetSize.call(this, false) + 31);',
  22247. ' };',
  22248. '});',
  22249. 'this.b = null;',
  22250. 'this.c = null;',
  22251. '']),
  22252. LinesToStr([ // $mod.$main
  22253. '$mod.TObjHelper.SetSize.call($mod.b.$class, true, $mod.TObjHelper.GetSize.call($mod.b.$class, false) + 11);',
  22254. '$mod.b.$class.SetSpeed(true, $mod.b.$class.GetSpeed(false) + 12);',
  22255. '$mod.TObjHelper.SetSize.call($mod.b.$class, true, $mod.TObjHelper.GetSize.call($mod.b.$class, false) + 13);',
  22256. 'var $with = $mod.b;',
  22257. '$mod.TObjHelper.SetSize.call($with.$class, true, $mod.TObjHelper.GetSize.call($with.$class, false) + 21);',
  22258. '$with.$class.SetSpeed(true, $with.$class.GetSpeed(false) + 22);',
  22259. '$mod.TObjHelper.SetSize.call($with.$class, true, $mod.TObjHelper.GetSize.call($with.$class, false) + 23);',
  22260. '$mod.TObjHelper.SetSize.call($mod.c, true, $mod.TObjHelper.GetSize.call($mod.c, false) + 11);',
  22261. '$mod.c.SetSpeed(true, $mod.c.GetSpeed(false) + 12);',
  22262. '$mod.TObjHelper.SetSize.call($mod.c, true, $mod.TObjHelper.GetSize.call($mod.c, false) + 13);',
  22263. 'var $with1 = $mod.c;',
  22264. '$mod.TObjHelper.SetSize.call($with1, true, $mod.TObjHelper.GetSize.call($with1, false) + 21);',
  22265. '$with1.SetSpeed(true, $with1.GetSpeed(false) + 22);',
  22266. '$mod.TObjHelper.SetSize.call($with1, true, $mod.TObjHelper.GetSize.call($with1, false) + 23);',
  22267. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 11);',
  22268. '$mod.TBird.SetSpeed(true, $mod.TBird.GetSpeed(false) + 12);',
  22269. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 13);',
  22270. 'var $with2 = $mod.TBird;',
  22271. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 21);',
  22272. '$with2.SetSpeed(true, $with2.GetSpeed(false) + 22);',
  22273. '$mod.TObjHelper.SetSize.call($mod.TBird, true, $mod.TObjHelper.GetSize.call($mod.TBird, false) + 23);',
  22274. '']));
  22275. end;
  22276. procedure TTestModule.TestClassHelper_ForIn;
  22277. begin
  22278. StartProgram(false);
  22279. Add([
  22280. 'type',
  22281. ' TObject = class end;',
  22282. ' TItem = TObject;',
  22283. ' TEnumerator = class',
  22284. ' FCurrent: TItem;',
  22285. ' property Current: TItem read FCurrent;',
  22286. ' function MoveNext: boolean;',
  22287. ' end;',
  22288. ' TBird = class',
  22289. ' end;',
  22290. ' TBirdHelper = class helper for TBird',
  22291. ' function GetEnumerator: TEnumerator;',
  22292. ' end;',
  22293. 'function TEnumerator.MoveNext: boolean;',
  22294. 'begin',
  22295. 'end;',
  22296. 'function TBirdHelper.GetEnumerator: TEnumerator;',
  22297. 'begin',
  22298. 'end;',
  22299. 'var',
  22300. ' b: TBird;',
  22301. ' i, i2: TItem;',
  22302. 'begin',
  22303. ' for i in b do i2:=i;']);
  22304. ConvertProgram;
  22305. CheckSource('TestClassHelper_ForIn',
  22306. LinesToStr([ // statements
  22307. 'rtl.createClass(this, "TObject", null, function () {',
  22308. ' this.$init = function () {',
  22309. ' };',
  22310. ' this.$final = function () {',
  22311. ' };',
  22312. '});',
  22313. 'rtl.createClass(this, "TEnumerator", this.TObject, function () {',
  22314. ' this.$init = function () {',
  22315. ' $mod.TObject.$init.call(this);',
  22316. ' this.FCurrent = null;',
  22317. ' };',
  22318. ' this.$final = function () {',
  22319. ' this.FCurrent = undefined;',
  22320. ' $mod.TObject.$final.call(this);',
  22321. ' };',
  22322. ' this.MoveNext = function () {',
  22323. ' var Result = false;',
  22324. ' return Result;',
  22325. ' };',
  22326. '});',
  22327. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  22328. '});',
  22329. 'rtl.createHelper(this, "TBirdHelper", null, function () {',
  22330. ' this.GetEnumerator = function () {',
  22331. ' var Result = null;',
  22332. ' return Result;',
  22333. ' };',
  22334. '});',
  22335. 'this.b = null;',
  22336. 'this.i = null;',
  22337. 'this.i2 = null;'
  22338. ]),
  22339. LinesToStr([ // $mod.$main
  22340. 'var $in = $mod.TBirdHelper.GetEnumerator.call($mod.b);',
  22341. 'try {',
  22342. ' while ($in.MoveNext()){',
  22343. ' $mod.i = $in.FCurrent;',
  22344. ' $mod.i2 = $mod.i;',
  22345. ' }',
  22346. '} finally {',
  22347. ' $in = rtl.freeLoc($in)',
  22348. '};',
  22349. '']));
  22350. end;
  22351. procedure TTestModule.TestClassHelper_PassProperty;
  22352. begin
  22353. StartProgram(false);
  22354. Add([
  22355. 'type',
  22356. ' TObject = class',
  22357. ' FField: TObject;',
  22358. ' property Field: TObject read FField write FField;',
  22359. ' end;',
  22360. ' THelper = class helper for TObject',
  22361. ' procedure Fly;',
  22362. ' class procedure Run;',
  22363. ' class procedure Jump; static;',
  22364. ' end;',
  22365. 'procedure THelper.Fly;',
  22366. 'begin',
  22367. ' Field.Fly;',
  22368. ' Field.Run;',
  22369. ' Field.Jump;',
  22370. ' with Field do begin',
  22371. ' Fly;',
  22372. ' Run;',
  22373. ' Jump;',
  22374. ' end;',
  22375. 'end;',
  22376. 'class procedure THelper.Run;',
  22377. 'begin',
  22378. 'end;',
  22379. 'class procedure THelper.Jump;',
  22380. 'begin',
  22381. 'end;',
  22382. 'var',
  22383. ' b: TObject;',
  22384. 'begin',
  22385. ' b.Field.Fly;',
  22386. ' b.Field.Run;',
  22387. ' b.Field.Jump;',
  22388. ' with b do begin',
  22389. ' Field.Run;',
  22390. ' Field.Fly;',
  22391. ' Field.Jump;',
  22392. ' end;',
  22393. ' with b.Field do begin',
  22394. ' Run;',
  22395. ' Fly;',
  22396. ' Jump;',
  22397. ' end;',
  22398. '']);
  22399. ConvertProgram;
  22400. CheckSource('TestClassHelper_PassProperty',
  22401. LinesToStr([ // statements
  22402. 'rtl.createClass(this, "TObject", null, function () {',
  22403. ' this.$init = function () {',
  22404. ' this.FField = null;',
  22405. ' };',
  22406. ' this.$final = function () {',
  22407. ' this.FField = undefined;',
  22408. ' };',
  22409. '});',
  22410. 'rtl.createHelper(this, "THelper", null, function () {',
  22411. ' this.Fly = function () {',
  22412. ' $mod.THelper.Fly.call(this.FField);',
  22413. ' $mod.THelper.Run.call(this.FField.$class);',
  22414. ' $mod.THelper.Jump();',
  22415. ' var $with = this.FField;',
  22416. ' $mod.THelper.Fly.call($with);',
  22417. ' $mod.THelper.Run.call($with.$class);',
  22418. ' $mod.THelper.Jump();',
  22419. ' };',
  22420. ' this.Run = function () {',
  22421. ' };',
  22422. ' this.Jump = function () {',
  22423. ' };',
  22424. '});',
  22425. 'this.b = null;',
  22426. '']),
  22427. LinesToStr([ // $mod.$main
  22428. '$mod.THelper.Fly.call($mod.b.FField);',
  22429. '$mod.THelper.Run.call($mod.b.FField.$class);',
  22430. '$mod.THelper.Jump();',
  22431. 'var $with = $mod.b;',
  22432. '$mod.THelper.Run.call($with.FField.$class);',
  22433. '$mod.THelper.Fly.call($with.FField);',
  22434. '$mod.THelper.Jump();',
  22435. 'var $with1 = $mod.b.FField;',
  22436. '$mod.THelper.Run.call($with1.$class);',
  22437. '$mod.THelper.Fly.call($with1);',
  22438. '$mod.THelper.Jump();',
  22439. '']));
  22440. end;
  22441. procedure TTestModule.TestExtClassHelper_ClassVar;
  22442. begin
  22443. StartProgram(false);
  22444. Add([
  22445. '{$modeswitch externalclass}',
  22446. 'type',
  22447. ' TExtA = class external name ''ExtObj''',
  22448. ' end;',
  22449. ' THelper = class helper for TExtA',
  22450. ' const',
  22451. ' One = 1;',
  22452. ' Two: word = 2;',
  22453. ' class var',
  22454. ' Glob: word;',
  22455. ' function Foo(w: word): word;',
  22456. ' class function Bar(w: word): word; static;',
  22457. ' end;',
  22458. 'function THelper.foo(w: word): word;',
  22459. 'begin',
  22460. ' Result:=w;',
  22461. ' Two:=One+w;',
  22462. ' Glob:=Glob;',
  22463. ' Result:=Self.Glob;',
  22464. ' Self.Glob:=Self.Glob;',
  22465. ' with Self do Glob:=Glob;',
  22466. 'end;',
  22467. 'class function THelper.bar(w: word): word;',
  22468. 'begin',
  22469. ' Result:=w;',
  22470. ' Two:=One;',
  22471. ' Glob:=Glob;',
  22472. 'end;',
  22473. 'var o: TExtA;',
  22474. 'begin',
  22475. ' texta.two:=texta.one;',
  22476. ' texta.Glob:=texta.Glob;',
  22477. ' with texta do begin',
  22478. ' two:=one;',
  22479. ' Glob:=Glob;',
  22480. ' end;',
  22481. ' o.two:=o.one;',
  22482. ' o.Glob:=o.Glob;',
  22483. ' with o do begin',
  22484. ' two:=one;',
  22485. ' Glob:=Glob;',
  22486. ' end;',
  22487. '']);
  22488. ConvertProgram;
  22489. CheckSource('TestExtClassHelper_ClassVar',
  22490. LinesToStr([ // statements
  22491. 'rtl.createHelper(this, "THelper", null, function () {',
  22492. ' this.One = 1;',
  22493. ' this.Two = 2;',
  22494. ' this.Glob = 0;',
  22495. ' this.Foo = function (w) {',
  22496. ' var Result = 0;',
  22497. ' Result = w;',
  22498. ' $mod.THelper.Two = 1 + w;',
  22499. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22500. ' Result = $mod.THelper.Glob;',
  22501. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22502. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22503. ' return Result;',
  22504. ' };',
  22505. ' this.Bar = function (w) {',
  22506. ' var Result = 0;',
  22507. ' Result = w;',
  22508. ' $mod.THelper.Two = 1;',
  22509. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22510. ' return Result;',
  22511. ' };',
  22512. '});',
  22513. 'this.o = null;',
  22514. '']),
  22515. LinesToStr([ // $mod.$main
  22516. '$mod.THelper.Two = 1;',
  22517. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22518. '$mod.THelper.Two = 1;',
  22519. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22520. '$mod.THelper.Two = 1;',
  22521. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22522. 'var $with = $mod.o;',
  22523. '$mod.THelper.Two = 1;',
  22524. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22525. '']));
  22526. end;
  22527. procedure TTestModule.TestExtClassHelper_Method_Call;
  22528. begin
  22529. StartProgram(false);
  22530. Add([
  22531. '{$modeswitch externalclass}',
  22532. 'type',
  22533. ' TFly = function(w: word): word of object;',
  22534. ' TExtA = class external name ''ExtObj''',
  22535. ' procedure Run(w: word = 10);',
  22536. ' end;',
  22537. ' THelper = class helper for TExtA',
  22538. ' function Foo(w: word = 1): word;',
  22539. ' function Fly(w: word = 2): word; external name ''Fly'';',
  22540. ' end;',
  22541. 'var p: TFly;',
  22542. 'function THelper.foo(w: word): word;',
  22543. 'begin',
  22544. ' Run;',
  22545. ' Run();',
  22546. ' Run(11);',
  22547. ' Foo;',
  22548. ' Foo();',
  22549. ' Foo(12);',
  22550. ' Self.Foo;',
  22551. ' Self.Foo();',
  22552. ' Self.Foo(13);',
  22553. ' Fly;',
  22554. ' Fly();',
  22555. ' with Self do begin',
  22556. ' Foo;',
  22557. ' Foo();',
  22558. ' Foo(14);',
  22559. ' Fly;',
  22560. ' Fly();',
  22561. ' end;',
  22562. ' p:=@Fly;',
  22563. 'end;',
  22564. 'var Obj: TExtA;',
  22565. 'begin',
  22566. ' obj.Foo;',
  22567. ' obj.Foo();',
  22568. ' obj.Foo(21);',
  22569. ' obj.Fly;',
  22570. ' obj.Fly();',
  22571. ' with obj do begin',
  22572. ' Foo;',
  22573. ' Foo();',
  22574. ' Foo(22);',
  22575. ' Fly;',
  22576. ' Fly();',
  22577. ' end;',
  22578. ' p:[email protected];',
  22579. '']);
  22580. ConvertProgram;
  22581. CheckSource('TestExtClassHelper_Method_Call',
  22582. LinesToStr([ // statements
  22583. 'rtl.createHelper(this, "THelper", null, function () {',
  22584. ' this.Foo = function (w) {',
  22585. ' var Result = 0;',
  22586. ' this.Run(10);',
  22587. ' this.Run(10);',
  22588. ' this.Run(11);',
  22589. ' $mod.THelper.Foo.call(this, 1);',
  22590. ' $mod.THelper.Foo.call(this, 1);',
  22591. ' $mod.THelper.Foo.call(this, 12);',
  22592. ' $mod.THelper.Foo.call(this, 1);',
  22593. ' $mod.THelper.Foo.call(this, 1);',
  22594. ' $mod.THelper.Foo.call(this, 13);',
  22595. ' this.Fly(2);',
  22596. ' this.Fly(2);',
  22597. ' $mod.THelper.Foo.call(this, 1);',
  22598. ' $mod.THelper.Foo.call(this, 1);',
  22599. ' $mod.THelper.Foo.call(this, 14);',
  22600. ' this.Fly(2);',
  22601. ' this.Fly(2);',
  22602. ' $mod.p = rtl.createCallback(this, "Fly");',
  22603. ' return Result;',
  22604. ' };',
  22605. '});',
  22606. 'this.p = null;',
  22607. 'this.Obj = null;',
  22608. '']),
  22609. LinesToStr([ // $mod.$main
  22610. '$mod.THelper.Foo.call($mod.Obj, 1);',
  22611. '$mod.THelper.Foo.call($mod.Obj, 1);',
  22612. '$mod.THelper.Foo.call($mod.Obj, 21);',
  22613. '$mod.Obj.Fly(2);',
  22614. '$mod.Obj.Fly(2);',
  22615. 'var $with = $mod.Obj;',
  22616. '$mod.THelper.Foo.call($with, 1);',
  22617. '$mod.THelper.Foo.call($with, 1);',
  22618. '$mod.THelper.Foo.call($with, 22);',
  22619. '$with.Fly(2);',
  22620. '$with.Fly(2);',
  22621. '$mod.p = rtl.createCallback($mod.Obj, "Fly");',
  22622. '']));
  22623. end;
  22624. procedure TTestModule.TestExtClassHelper_ClassMethod_MissingStatic;
  22625. begin
  22626. StartProgram(false);
  22627. Add([
  22628. '{$modeswitch externalclass}',
  22629. 'type',
  22630. ' TExtA = class external name ''ExtObj''',
  22631. ' procedure Run(w: word = 10);',
  22632. ' end;',
  22633. ' THelper = class helper for TExtA',
  22634. ' class procedure Fly;',
  22635. ' end;',
  22636. 'class procedure THelper.Fly;',
  22637. 'begin end;',
  22638. 'begin',
  22639. '']);
  22640. SetExpectedPasResolverError(sHelperClassMethodForExtClassMustBeStatic,
  22641. nHelperClassMethodForExtClassMustBeStatic);
  22642. ConvertProgram;
  22643. end;
  22644. procedure TTestModule.TestRecordHelper_ClassVar;
  22645. begin
  22646. StartProgram(false);
  22647. Add([
  22648. 'type',
  22649. ' TRec = record',
  22650. ' end;',
  22651. ' THelper = record helper for TRec',
  22652. ' const',
  22653. ' One = 1;',
  22654. ' Two: word = 2;',
  22655. ' class var',
  22656. ' Glob: word;',
  22657. ' function Foo(w: word): word;',
  22658. ' class function Bar(w: word): word; static;',
  22659. ' end;',
  22660. 'function THelper.foo(w: word): word;',
  22661. 'begin',
  22662. ' Result:=w;',
  22663. ' Two:=One+w;',
  22664. ' Glob:=Glob;',
  22665. ' Result:=Self.Glob;',
  22666. ' Self.Glob:=Self.Glob;',
  22667. ' with Self do Glob:=Glob;',
  22668. ' Self:=Self;',
  22669. 'end;',
  22670. 'class function THelper.bar(w: word): word;',
  22671. 'begin',
  22672. ' Result:=w;',
  22673. ' Two:=One;',
  22674. ' Glob:=Glob;',
  22675. 'end;',
  22676. 'var r: TRec;',
  22677. 'begin',
  22678. ' trec.two:=trec.one;',
  22679. ' trec.Glob:=trec.Glob;',
  22680. ' with trec do begin',
  22681. ' two:=one;',
  22682. ' Glob:=Glob;',
  22683. ' end;',
  22684. ' r.two:=r.one;',
  22685. ' r.Glob:=r.Glob;',
  22686. ' with r do begin',
  22687. ' two:=one;',
  22688. ' Glob:=Glob;',
  22689. ' end;',
  22690. '']);
  22691. ConvertProgram;
  22692. CheckSource('TestRecordHelper_ClassVar',
  22693. LinesToStr([ // statements
  22694. 'rtl.recNewT(this, "TRec", function () {',
  22695. ' this.$eq = function (b) {',
  22696. ' return true;',
  22697. ' };',
  22698. ' this.$assign = function (s) {',
  22699. ' return this;',
  22700. ' };',
  22701. '});',
  22702. 'rtl.createHelper(this, "THelper", null, function () {',
  22703. ' this.One = 1;',
  22704. ' this.Two = 2;',
  22705. ' this.Glob = 0;',
  22706. ' this.Foo = function (w) {',
  22707. ' var Result = 0;',
  22708. ' Result = w;',
  22709. ' $mod.THelper.Two = 1 + w;',
  22710. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22711. ' Result = $mod.THelper.Glob;',
  22712. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22713. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22714. ' this.$assign(this);',
  22715. ' return Result;',
  22716. ' };',
  22717. ' this.Bar = function (w) {',
  22718. ' var Result = 0;',
  22719. ' Result = w;',
  22720. ' $mod.THelper.Two = 1;',
  22721. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22722. ' return Result;',
  22723. ' };',
  22724. '});',
  22725. 'this.r = this.TRec.$new();',
  22726. '']),
  22727. LinesToStr([ // $mod.$main
  22728. '$mod.THelper.Two = 1;',
  22729. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22730. 'var $with = $mod.TRec;',
  22731. '$mod.THelper.Two = 1;',
  22732. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22733. '$mod.THelper.Two = 1;',
  22734. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22735. 'var $with1 = $mod.r;',
  22736. '$mod.THelper.Two = 1;',
  22737. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22738. '']));
  22739. end;
  22740. procedure TTestModule.TestRecordHelper_Method_Call;
  22741. begin
  22742. StartProgram(false);
  22743. Add([
  22744. '{$modeswitch AdvancedRecords}',
  22745. 'type',
  22746. ' TRec = record',
  22747. ' procedure Run(w: word = 10);',
  22748. ' end;',
  22749. ' THelper = record helper for TRec',
  22750. ' function Foo(w: word = 1): word;',
  22751. ' end;',
  22752. 'procedure TRec.Run(w: word);',
  22753. 'begin',
  22754. ' Foo;',
  22755. ' Foo();',
  22756. ' Foo(2);',
  22757. ' Self.Foo;',
  22758. ' Self.Foo();',
  22759. ' Self.Foo(3);',
  22760. ' with Self do begin',
  22761. ' Foo;',
  22762. ' Foo();',
  22763. ' Foo(4);',
  22764. ' end;',
  22765. 'end;',
  22766. 'function THelper.foo(w: word): word;',
  22767. 'begin',
  22768. ' Run;',
  22769. ' Run();',
  22770. ' Run(11);',
  22771. ' Foo;',
  22772. ' Foo();',
  22773. ' Foo(12);',
  22774. ' Self.Foo;',
  22775. ' Self.Foo();',
  22776. ' Self.Foo(13);',
  22777. ' with Self do begin',
  22778. ' Foo;',
  22779. ' Foo();',
  22780. ' Foo(14);',
  22781. ' end;',
  22782. 'end;',
  22783. 'var Rec: TRec;',
  22784. 'begin',
  22785. ' Rec.Foo;',
  22786. ' Rec.Foo();',
  22787. ' Rec.Foo(21);',
  22788. ' with Rec do begin',
  22789. ' Foo;',
  22790. ' Foo();',
  22791. ' Foo(22);',
  22792. ' end;',
  22793. '']);
  22794. ConvertProgram;
  22795. CheckSource('TestRecordHelper_Method_Call',
  22796. LinesToStr([ // statements
  22797. 'rtl.recNewT(this, "TRec", function () {',
  22798. ' this.$eq = function (b) {',
  22799. ' return true;',
  22800. ' };',
  22801. ' this.$assign = function (s) {',
  22802. ' return this;',
  22803. ' };',
  22804. ' this.Run = function (w) {',
  22805. ' $mod.THelper.Foo.call(this, 1);',
  22806. ' $mod.THelper.Foo.call(this, 1);',
  22807. ' $mod.THelper.Foo.call(this, 2);',
  22808. ' $mod.THelper.Foo.call(this, 1);',
  22809. ' $mod.THelper.Foo.call(this, 1);',
  22810. ' $mod.THelper.Foo.call(this, 3);',
  22811. ' $mod.THelper.Foo.call(this, 1);',
  22812. ' $mod.THelper.Foo.call(this, 1);',
  22813. ' $mod.THelper.Foo.call(this, 4);',
  22814. ' };',
  22815. '});',
  22816. 'rtl.createHelper(this, "THelper", null, function () {',
  22817. ' this.Foo = function (w) {',
  22818. ' var Result = 0;',
  22819. ' this.Run(10);',
  22820. ' this.Run(10);',
  22821. ' this.Run(11);',
  22822. ' $mod.THelper.Foo.call(this, 1);',
  22823. ' $mod.THelper.Foo.call(this, 1);',
  22824. ' $mod.THelper.Foo.call(this, 12);',
  22825. ' $mod.THelper.Foo.call(this, 1);',
  22826. ' $mod.THelper.Foo.call(this, 1);',
  22827. ' $mod.THelper.Foo.call(this, 13);',
  22828. ' $mod.THelper.Foo.call(this, 1);',
  22829. ' $mod.THelper.Foo.call(this, 1);',
  22830. ' $mod.THelper.Foo.call(this, 14);',
  22831. ' return Result;',
  22832. ' };',
  22833. '});',
  22834. 'this.Rec = this.TRec.$new();',
  22835. '']),
  22836. LinesToStr([ // $mod.$main
  22837. '$mod.THelper.Foo.call($mod.Rec, 1);',
  22838. '$mod.THelper.Foo.call($mod.Rec, 1);',
  22839. '$mod.THelper.Foo.call($mod.Rec, 21);',
  22840. 'var $with = $mod.Rec;',
  22841. '$mod.THelper.Foo.call($with, 1);',
  22842. '$mod.THelper.Foo.call($with, 1);',
  22843. '$mod.THelper.Foo.call($with, 22);',
  22844. '']));
  22845. end;
  22846. procedure TTestModule.TestRecordHelper_Constructor;
  22847. begin
  22848. StartProgram(false);
  22849. Add([
  22850. '{$modeswitch AdvancedRecords}',
  22851. 'type',
  22852. ' TRec = record',
  22853. ' constructor Create(w: word);',
  22854. ' end;',
  22855. ' THelper = record helper for TRec',
  22856. ' constructor NewHlp(w: word);',
  22857. ' end;',
  22858. 'var',
  22859. ' Rec: TRec;',
  22860. 'constructor TRec.Create(w: word);',
  22861. 'begin',
  22862. ' NewHlp(2);', // normal call
  22863. ' trec.NewHlp(3);', // new instance
  22864. 'end;',
  22865. 'constructor THelper.NewHlp(w: word);',
  22866. 'begin',
  22867. ' create(2);', // normal call
  22868. ' trec.create(3);', // new instance
  22869. ' NewHlp(4);', // normal call
  22870. ' trec.NewHlp(5);', // new instance
  22871. 'end;',
  22872. 'begin',
  22873. ' rec.newhlp(2);', // normal call
  22874. ' with rec do newhlp(12);', // normal call
  22875. ' trec.newhlp(3);', // new instance
  22876. ' with trec do newhlp(13);', // new instance
  22877. '']);
  22878. ConvertProgram;
  22879. CheckSource('TestRecordHelper_Constructor',
  22880. LinesToStr([ // statements
  22881. 'rtl.recNewT(this, "TRec", function () {',
  22882. ' this.$eq = function (b) {',
  22883. ' return true;',
  22884. ' };',
  22885. ' this.$assign = function (s) {',
  22886. ' return this;',
  22887. ' };',
  22888. ' this.Create = function (w) {',
  22889. ' $mod.THelper.NewHlp.call(this, 2);',
  22890. ' $mod.THelper.$new("NewHlp", [3]);',
  22891. ' return this;',
  22892. ' };',
  22893. '});',
  22894. 'rtl.createHelper(this, "THelper", null, function () {',
  22895. ' this.NewHlp = function (w) {',
  22896. ' this.Create(2);',
  22897. ' $mod.TRec.$new().Create(3);',
  22898. ' $mod.THelper.NewHlp.call(this, 4);',
  22899. ' $mod.THelper.$new("NewHlp", [5]);',
  22900. ' return this;',
  22901. ' };',
  22902. ' this.$new = function (fn, args) {',
  22903. ' return this[fn].apply($mod.TRec.$new(), args);',
  22904. ' };',
  22905. '});',
  22906. 'this.Rec = this.TRec.$new();',
  22907. '']),
  22908. LinesToStr([ // $mod.$main
  22909. '$mod.THelper.NewHlp.call($mod.Rec, 2);',
  22910. 'var $with = $mod.Rec;',
  22911. '$mod.THelper.NewHlp.call($with, 12);',
  22912. '$mod.THelper.$new("NewHlp", [3]);',
  22913. 'var $with1 = $mod.TRec;',
  22914. '$mod.THelper.$new("NewHlp", [13]);',
  22915. '']));
  22916. end;
  22917. procedure TTestModule.TestTypeHelper_ClassVar;
  22918. begin
  22919. StartProgram(false);
  22920. Add([
  22921. '{$modeswitch typehelpers}',
  22922. 'type',
  22923. ' THelper = type helper for byte',
  22924. ' const',
  22925. ' One = 1;',
  22926. ' Two: word = 2;',
  22927. ' class var',
  22928. ' Glob: word;',
  22929. ' function Foo(w: word): word;',
  22930. ' class function Bar(w: word): word; static;',
  22931. ' end;',
  22932. 'function THelper.foo(w: word): word;',
  22933. 'begin',
  22934. ' Result:=w;',
  22935. ' Two:=One+w;',
  22936. ' Glob:=Glob;',
  22937. ' Result:=Self.Glob;',
  22938. ' Self.Glob:=Self.Glob;',
  22939. ' with Self do Glob:=Glob;',
  22940. 'end;',
  22941. 'class function THelper.bar(w: word): word;',
  22942. 'begin',
  22943. ' Result:=w;',
  22944. ' Two:=One;',
  22945. ' Glob:=Glob;',
  22946. 'end;',
  22947. 'var b: byte;',
  22948. 'begin',
  22949. ' byte.two:=byte.one;',
  22950. ' byte.Glob:=byte.Glob;',
  22951. ' with byte do begin',
  22952. ' two:=one;',
  22953. ' Glob:=Glob;',
  22954. ' end;',
  22955. ' b.two:=b.one;',
  22956. ' b.Glob:=b.Glob;',
  22957. ' with b do begin',
  22958. ' two:=one;',
  22959. ' Glob:=Glob;',
  22960. ' end;',
  22961. '']);
  22962. ConvertProgram;
  22963. CheckSource('TestTypeHelper_ClassVar',
  22964. LinesToStr([ // statements
  22965. 'rtl.createHelper(this, "THelper", null, function () {',
  22966. ' this.One = 1;',
  22967. ' this.Two = 2;',
  22968. ' this.Glob = 0;',
  22969. ' this.Foo = function (w) {',
  22970. ' var Result = 0;',
  22971. ' Result = w;',
  22972. ' $mod.THelper.Two = 1 + w;',
  22973. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22974. ' Result = $mod.THelper.Glob;',
  22975. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22976. ' var $with = this.get();',
  22977. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22978. ' return Result;',
  22979. ' };',
  22980. ' this.Bar = function (w) {',
  22981. ' var Result = 0;',
  22982. ' Result = w;',
  22983. ' $mod.THelper.Two = 1;',
  22984. ' $mod.THelper.Glob = $mod.THelper.Glob;',
  22985. ' return Result;',
  22986. ' };',
  22987. '});',
  22988. 'this.b = 0;',
  22989. '']),
  22990. LinesToStr([ // $mod.$main
  22991. '$mod.THelper.Two = 1;',
  22992. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22993. '$mod.THelper.Two = 1;',
  22994. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22995. '$mod.THelper.Two = 1;',
  22996. '$mod.THelper.Glob = $mod.THelper.Glob;',
  22997. 'var $with = $mod.b;',
  22998. '$mod.THelper.Two = 1;',
  22999. '$mod.THelper.Glob = $mod.THelper.Glob;',
  23000. '']));
  23001. end;
  23002. procedure TTestModule.TestTypeHelper_PassResultElement;
  23003. begin
  23004. StartProgram(false);
  23005. Add([
  23006. '{$modeswitch typehelpers}',
  23007. 'type',
  23008. ' THelper = type helper for word',
  23009. ' procedure DoIt(e: byte = 123);',
  23010. ' class procedure DoSome(e: byte = 456); static;',
  23011. ' end;',
  23012. 'procedure THelper.DoIt(e: byte);',
  23013. 'begin',
  23014. 'end;',
  23015. 'class procedure THelper.DoSome(e: byte);',
  23016. 'begin',
  23017. 'end;',
  23018. 'function Foo(w: word): word;',
  23019. 'begin',
  23020. ' Result.DoIt;',
  23021. ' Result.DoIt();',
  23022. ' Result.DoSome;',
  23023. ' Result.DoSome();',
  23024. ' with Result do begin',
  23025. ' DoIt;',
  23026. ' DoIt();',
  23027. ' DoSome;',
  23028. ' DoSome();',
  23029. ' end;',
  23030. 'end;',
  23031. 'begin',
  23032. '']);
  23033. ConvertProgram;
  23034. CheckSource('TestTypeHelper_PassResultElement',
  23035. LinesToStr([ // statements
  23036. 'rtl.createHelper(this, "THelper", null, function () {',
  23037. ' this.DoIt = function (e) {',
  23038. ' };',
  23039. ' this.DoSome = function (e) {',
  23040. ' };',
  23041. '});',
  23042. 'this.Foo = function (w) {',
  23043. ' var Result = 0;',
  23044. ' $mod.THelper.DoIt.call({',
  23045. ' get: function () {',
  23046. ' return Result;',
  23047. ' },',
  23048. ' set: function (v) {',
  23049. ' Result = v;',
  23050. ' }',
  23051. ' }, 123);',
  23052. ' $mod.THelper.DoIt.call({',
  23053. ' get: function () {',
  23054. ' return Result;',
  23055. ' },',
  23056. ' set: function (v) {',
  23057. ' Result = v;',
  23058. ' }',
  23059. ' }, 123);',
  23060. ' $mod.THelper.DoSome(456);',
  23061. ' $mod.THelper.DoSome(456);',
  23062. ' $mod.THelper.DoIt.call({',
  23063. ' get: function () {',
  23064. ' return Result;',
  23065. ' },',
  23066. ' set: function (v) {',
  23067. ' Result = v;',
  23068. ' }',
  23069. ' }, 123);',
  23070. ' $mod.THelper.DoIt.call({',
  23071. ' get: function () {',
  23072. ' return Result;',
  23073. ' },',
  23074. ' set: function (v) {',
  23075. ' Result = v;',
  23076. ' }',
  23077. ' }, 123);',
  23078. ' $mod.THelper.DoSome(456);',
  23079. ' $mod.THelper.DoSome(456);',
  23080. ' return Result;',
  23081. '};',
  23082. '']),
  23083. LinesToStr([ // $mod.$main
  23084. '']));
  23085. end;
  23086. procedure TTestModule.TestTypeHelper_PassArgs;
  23087. begin
  23088. StartProgram(false);
  23089. Add([
  23090. '{$modeswitch typehelpers}',
  23091. 'type',
  23092. ' THelper = type helper for word',
  23093. ' procedure DoIt(e: byte = 123);',
  23094. ' end;',
  23095. 'procedure THelper.DoIt(e: byte);',
  23096. 'begin',
  23097. 'end;',
  23098. 'procedure FooDefault(a: word);',
  23099. 'begin',
  23100. ' a.DoIt;',
  23101. ' with a do DoIt;',
  23102. 'end;',
  23103. 'procedure FooConst(const a: word);',
  23104. 'begin',
  23105. ' a.DoIt;',
  23106. ' with a do DoIt;',
  23107. 'end;',
  23108. 'procedure FooVar(var a: word);',
  23109. 'begin',
  23110. ' a.DoIt;',
  23111. ' with a do DoIt;',
  23112. 'end;',
  23113. 'begin',
  23114. '']);
  23115. ConvertProgram;
  23116. CheckSource('TestTypeHelper_PassArgs',
  23117. LinesToStr([ // statements
  23118. 'rtl.createHelper(this, "THelper", null, function () {',
  23119. ' this.DoIt = function (e) {',
  23120. ' };',
  23121. '});',
  23122. 'this.FooDefault = function (a) {',
  23123. ' $mod.THelper.DoIt.call({',
  23124. ' get: function () {',
  23125. ' return a;',
  23126. ' },',
  23127. ' set: function (v) {',
  23128. ' a = v;',
  23129. ' }',
  23130. ' }, 123);',
  23131. ' $mod.THelper.DoIt.call({',
  23132. ' get: function () {',
  23133. ' return a;',
  23134. ' },',
  23135. ' set: function (v) {',
  23136. ' a = v;',
  23137. ' }',
  23138. ' }, 123);',
  23139. '};',
  23140. 'this.FooConst = function (a) {',
  23141. ' $mod.THelper.DoIt.call({',
  23142. ' get: function () {',
  23143. ' return a;',
  23144. ' },',
  23145. ' set: function (v) {',
  23146. ' rtl.raiseE("EPropReadOnly");',
  23147. ' }',
  23148. ' }, 123);',
  23149. ' $mod.THelper.DoIt.call({',
  23150. ' get: function () {',
  23151. ' return a;',
  23152. ' },',
  23153. ' set: function () {',
  23154. ' rtl.raiseE("EPropReadOnly");',
  23155. ' }',
  23156. ' }, 123);',
  23157. '};',
  23158. 'this.FooVar = function (a) {',
  23159. ' $mod.THelper.DoIt.call(a, 123);',
  23160. ' var $with = a.get();',
  23161. ' $mod.THelper.DoIt.call(a, 123);',
  23162. '};',
  23163. '']),
  23164. LinesToStr([ // $mod.$main
  23165. '']));
  23166. end;
  23167. procedure TTestModule.TestTypeHelper_PassVarConst;
  23168. begin
  23169. StartProgram(false);
  23170. Add([
  23171. '{$modeswitch typehelpers}',
  23172. 'type',
  23173. ' THelper = type helper for word',
  23174. ' procedure DoIt(e: byte = 123);',
  23175. ' end;',
  23176. 'procedure THelper.DoIt(e: byte);',
  23177. 'begin',
  23178. 'end;',
  23179. 'var a: word;',
  23180. 'const c: word = 2;',
  23181. '{$writeableconst off}',
  23182. 'const r: word = 3;',
  23183. 'begin',
  23184. ' a.DoIt;',
  23185. ' with a do DoIt;',
  23186. ' c.DoIt;',
  23187. ' with c do DoIt;',
  23188. ' r.DoIt;',
  23189. ' with r do DoIt;',
  23190. '']);
  23191. ConvertProgram;
  23192. CheckSource('TestTypeHelper_PassVarConst',
  23193. LinesToStr([ // statements
  23194. 'rtl.createHelper(this, "THelper", null, function () {',
  23195. ' this.DoIt = function (e) {',
  23196. ' };',
  23197. '});',
  23198. 'this.a = 0;',
  23199. 'this.c = 2;',
  23200. 'this.r = 3;',
  23201. '']),
  23202. LinesToStr([ // $mod.$main
  23203. '$mod.THelper.DoIt.call({',
  23204. ' p: $mod,',
  23205. ' get: function () {',
  23206. ' return this.p.a;',
  23207. ' },',
  23208. ' set: function (v) {',
  23209. ' this.p.a = v;',
  23210. ' }',
  23211. '}, 123);',
  23212. 'var $with = $mod.a;',
  23213. '$mod.THelper.DoIt.call({',
  23214. ' get: function () {',
  23215. ' return $with;',
  23216. ' },',
  23217. ' set: function (v) {',
  23218. ' $with = v;',
  23219. ' }',
  23220. '}, 123);',
  23221. '$mod.THelper.DoIt.call({',
  23222. ' p: $mod,',
  23223. ' get: function () {',
  23224. ' return this.p.c;',
  23225. ' },',
  23226. ' set: function (v) {',
  23227. ' this.p.c = v;',
  23228. ' }',
  23229. '}, 123);',
  23230. 'var $with1 = $mod.c;',
  23231. '$mod.THelper.DoIt.call({',
  23232. ' get: function () {',
  23233. ' return $with1;',
  23234. ' },',
  23235. ' set: function (v) {',
  23236. ' $with1 = v;',
  23237. ' }',
  23238. '}, 123);',
  23239. '$mod.THelper.DoIt.call({',
  23240. ' get: function () {',
  23241. ' return 3;',
  23242. ' },',
  23243. ' set: function (v) {',
  23244. ' rtl.raiseE("EPropReadOnly");',
  23245. ' }',
  23246. '}, 123);',
  23247. 'var $with2 = 3;',
  23248. ' $mod.THelper.DoIt.call({',
  23249. ' get: function () {',
  23250. ' return $with2;',
  23251. ' },',
  23252. ' set: function () {',
  23253. ' rtl.raiseE("EPropReadOnly");',
  23254. ' }',
  23255. ' }, 123);',
  23256. '']));
  23257. end;
  23258. procedure TTestModule.TestTypeHelper_PassFuncResult;
  23259. begin
  23260. StartProgram(false);
  23261. Add([
  23262. '{$modeswitch typehelpers}',
  23263. 'type',
  23264. ' THelper = type helper for word',
  23265. ' procedure DoIt(e: byte = 123);',
  23266. ' end;',
  23267. 'procedure THelper.DoIt(e: byte);',
  23268. 'begin',
  23269. 'end;',
  23270. 'function Foo(b: byte = 1): word;',
  23271. 'begin',
  23272. 'end;',
  23273. 'begin',
  23274. ' Foo.DoIt;',
  23275. ' Foo().DoIt;',
  23276. ' with Foo do DoIt;',
  23277. ' with Foo() do DoIt;',
  23278. '']);
  23279. ConvertProgram;
  23280. CheckSource('TestTypeHelper_PassFuncResult',
  23281. LinesToStr([ // statements
  23282. 'rtl.createHelper(this, "THelper", null, function () {',
  23283. ' this.DoIt = function (e) {',
  23284. ' };',
  23285. '});',
  23286. 'this.Foo = function (b) {',
  23287. ' var Result = 0;',
  23288. ' return Result;',
  23289. '};',
  23290. '']),
  23291. LinesToStr([ // $mod.$main
  23292. '$mod.THelper.DoIt.call({',
  23293. ' a: $mod.Foo(1),',
  23294. ' get: function () {',
  23295. ' return this.a;',
  23296. ' },',
  23297. ' set: function (v) {',
  23298. ' this.a = v;',
  23299. ' }',
  23300. '}, 123);',
  23301. '$mod.THelper.DoIt.call({',
  23302. ' a: $mod.Foo(1),',
  23303. ' get: function () {',
  23304. ' return this.a;',
  23305. ' },',
  23306. ' set: function (v) {',
  23307. ' this.a = v;',
  23308. ' }',
  23309. '}, 123);',
  23310. 'var $with = $mod.Foo(1);',
  23311. '$mod.THelper.DoIt.call({',
  23312. ' get: function () {',
  23313. ' return $with;',
  23314. ' },',
  23315. ' set: function (v) {',
  23316. ' $with = v;',
  23317. ' }',
  23318. '}, 123);',
  23319. 'var $with1 = $mod.Foo(1);',
  23320. '$mod.THelper.DoIt.call({',
  23321. ' get: function () {',
  23322. ' return $with1;',
  23323. ' },',
  23324. ' set: function (v) {',
  23325. ' $with1 = v;',
  23326. ' }',
  23327. '}, 123);',
  23328. '']));
  23329. end;
  23330. procedure TTestModule.TestTypeHelper_PassPropertyField;
  23331. begin
  23332. StartProgram(false);
  23333. Add([
  23334. '{$modeswitch typehelpers}',
  23335. 'type',
  23336. ' TObject = class',
  23337. ' FField: word;',
  23338. ' procedure SetField(Value: word);',
  23339. ' property Field: word read FField write SetField;',
  23340. ' end;',
  23341. ' THelper = type helper for word',
  23342. ' procedure Fly;',
  23343. ' class procedure Run; static;',
  23344. ' end;',
  23345. 'procedure TObject.SetField(Value: word);',
  23346. 'begin',
  23347. ' Field.Fly;',
  23348. ' Field.Run;',
  23349. ' Self.Field.Fly;',
  23350. ' Self.Field.Run;',
  23351. ' with Self do begin',
  23352. ' Field.Fly;',
  23353. ' Field.Run;',
  23354. ' end;',
  23355. ' with Self.Field do begin',
  23356. ' Fly;',
  23357. ' Run;',
  23358. ' end;',
  23359. 'end;',
  23360. 'procedure THelper.Fly;',
  23361. 'begin',
  23362. 'end;',
  23363. 'class procedure THelper.Run;',
  23364. 'begin',
  23365. 'end;',
  23366. 'var',
  23367. ' o: TObject;',
  23368. 'begin',
  23369. ' o.Field.Fly;',
  23370. ' o.Field.Run;',
  23371. ' with o do begin',
  23372. ' Field.Fly;',
  23373. ' Field.Run;',
  23374. ' end;',
  23375. ' with o.Field do begin',
  23376. ' Fly;',
  23377. ' Run;',
  23378. ' end;',
  23379. '']);
  23380. ConvertProgram;
  23381. CheckSource('TestTypeHelper_PassPropertyField',
  23382. LinesToStr([ // statements
  23383. 'rtl.createClass(this, "TObject", null, function () {',
  23384. ' this.$init = function () {',
  23385. ' this.FField = 0;',
  23386. ' };',
  23387. ' this.$final = function () {',
  23388. ' };',
  23389. ' this.SetField = function (Value) {',
  23390. ' $mod.THelper.Fly.call({',
  23391. ' p: this,',
  23392. ' get: function () {',
  23393. ' return this.p.FField;',
  23394. ' },',
  23395. ' set: function (v) {',
  23396. ' this.p.FField = v;',
  23397. ' }',
  23398. ' });',
  23399. ' $mod.THelper.Run();',
  23400. ' $mod.THelper.Fly.call({',
  23401. ' p: this,',
  23402. ' get: function () {',
  23403. ' return this.p.FField;',
  23404. ' },',
  23405. ' set: function (v) {',
  23406. ' this.p.FField = v;',
  23407. ' }',
  23408. ' });',
  23409. ' $mod.THelper.Run();',
  23410. ' $mod.THelper.Fly.call({',
  23411. ' p: this,',
  23412. ' get: function () {',
  23413. ' return this.p.FField;',
  23414. ' },',
  23415. ' set: function (v) {',
  23416. ' this.p.FField = v;',
  23417. ' }',
  23418. ' });',
  23419. ' $mod.THelper.Run();',
  23420. ' var $with = this.FField;',
  23421. ' $mod.THelper.Fly.call({',
  23422. ' get: function () {',
  23423. ' return $with;',
  23424. ' },',
  23425. ' set: function (v) {',
  23426. ' $with = v;',
  23427. ' }',
  23428. ' });',
  23429. ' $mod.THelper.Run();',
  23430. ' };',
  23431. '});',
  23432. 'rtl.createHelper(this, "THelper", null, function () {',
  23433. ' this.Fly = function () {',
  23434. ' };',
  23435. ' this.Run = function () {',
  23436. ' };',
  23437. '});',
  23438. 'this.o = null;',
  23439. '']),
  23440. LinesToStr([ // $mod.$main
  23441. '$mod.THelper.Fly.call({',
  23442. ' p: $mod.o,',
  23443. ' get: function () {',
  23444. ' return this.p.FField;',
  23445. ' },',
  23446. ' set: function (v) {',
  23447. ' this.p.FField = v;',
  23448. ' }',
  23449. '});',
  23450. '$mod.THelper.Run();',
  23451. 'var $with = $mod.o;',
  23452. '$mod.THelper.Fly.call({',
  23453. ' p: $with,',
  23454. ' get: function () {',
  23455. ' return this.p.FField;',
  23456. ' },',
  23457. ' set: function (v) {',
  23458. ' this.p.FField = v;',
  23459. ' }',
  23460. '});',
  23461. '$mod.THelper.Run();',
  23462. 'var $with1 = $mod.o.FField;',
  23463. '$mod.THelper.Fly.call({',
  23464. ' get: function () {',
  23465. ' return $with1;',
  23466. ' },',
  23467. ' set: function (v) {',
  23468. ' $with1 = v;',
  23469. ' }',
  23470. '});',
  23471. '$mod.THelper.Run();',
  23472. '']));
  23473. end;
  23474. procedure TTestModule.TestTypeHelper_PassPropertyGetter;
  23475. begin
  23476. StartProgram(false);
  23477. Add([
  23478. '{$modeswitch typehelpers}',
  23479. 'type',
  23480. ' TObject = class',
  23481. ' FField: word;',
  23482. ' function GetField: word;',
  23483. ' property Field: word read GetField write FField;',
  23484. ' end;',
  23485. ' THelper = type helper for word',
  23486. ' procedure Fly;',
  23487. ' class procedure Run; static;',
  23488. ' end;',
  23489. 'function TObject.GetField: word;',
  23490. 'begin',
  23491. ' Field.Fly;',
  23492. ' Field.Run;',
  23493. ' Self.Field.Fly;',
  23494. ' Self.Field.Run;',
  23495. ' with Self do begin',
  23496. ' Field.Fly;',
  23497. ' Field.Run;',
  23498. ' end;',
  23499. ' with Self.Field do begin',
  23500. ' Fly;',
  23501. ' Run;',
  23502. ' end;',
  23503. 'end;',
  23504. 'procedure THelper.Fly;',
  23505. 'begin',
  23506. 'end;',
  23507. 'class procedure THelper.Run;',
  23508. 'begin',
  23509. 'end;',
  23510. 'var',
  23511. ' o: TObject;',
  23512. 'begin',
  23513. ' o.Field.Fly;',
  23514. ' o.Field.Run;',
  23515. ' with o do begin',
  23516. ' Field.Fly;',
  23517. ' Field.Run;',
  23518. ' end;',
  23519. ' with o.Field do begin',
  23520. ' Fly;',
  23521. ' Run;',
  23522. ' end;',
  23523. '']);
  23524. ConvertProgram;
  23525. CheckSource('TestTypeHelper_PassPropertyGetter',
  23526. LinesToStr([ // statements
  23527. 'rtl.createClass(this, "TObject", null, function () {',
  23528. ' this.$init = function () {',
  23529. ' this.FField = 0;',
  23530. ' };',
  23531. ' this.$final = function () {',
  23532. ' };',
  23533. ' this.GetField = function () {',
  23534. ' var Result = 0;',
  23535. ' $mod.THelper.Fly.call({',
  23536. ' p: this.GetField(),',
  23537. ' get: function () {',
  23538. ' return this.p;',
  23539. ' },',
  23540. ' set: function (v) {',
  23541. ' this.p = v;',
  23542. ' }',
  23543. ' });',
  23544. ' $mod.THelper.Run();',
  23545. ' $mod.THelper.Fly.call({',
  23546. ' p: this.GetField(),',
  23547. ' get: function () {',
  23548. ' return this.p;',
  23549. ' },',
  23550. ' set: function (v) {',
  23551. ' this.p = v;',
  23552. ' }',
  23553. ' });',
  23554. ' $mod.THelper.Run();',
  23555. ' $mod.THelper.Fly.call({',
  23556. ' p: this.GetField(),',
  23557. ' get: function () {',
  23558. ' return this.p;',
  23559. ' },',
  23560. ' set: function (v) {',
  23561. ' this.p = v;',
  23562. ' }',
  23563. ' });',
  23564. ' $mod.THelper.Run();',
  23565. ' var $with = this.GetField();',
  23566. ' $mod.THelper.Fly.call({',
  23567. ' get: function () {',
  23568. ' return $with;',
  23569. ' },',
  23570. ' set: function (v) {',
  23571. ' $with = v;',
  23572. ' }',
  23573. ' });',
  23574. ' $mod.THelper.Run();',
  23575. ' return Result;',
  23576. ' };',
  23577. '});',
  23578. 'rtl.createHelper(this, "THelper", null, function () {',
  23579. ' this.Fly = function () {',
  23580. ' };',
  23581. ' this.Run = function () {',
  23582. ' };',
  23583. '});',
  23584. 'this.o = null;',
  23585. '']),
  23586. LinesToStr([ // $mod.$main
  23587. '$mod.THelper.Fly.call({',
  23588. ' p: $mod.o.GetField(),',
  23589. ' get: function () {',
  23590. ' return this.p;',
  23591. ' },',
  23592. ' set: function (v) {',
  23593. ' this.p = v;',
  23594. ' }',
  23595. '});',
  23596. '$mod.THelper.Run();',
  23597. 'var $with = $mod.o;',
  23598. '$mod.THelper.Fly.call({',
  23599. ' p: $with.GetField(),',
  23600. ' get: function () {',
  23601. ' return this.p;',
  23602. ' },',
  23603. ' set: function (v) {',
  23604. ' this.p = v;',
  23605. ' }',
  23606. '});',
  23607. '$mod.THelper.Run();',
  23608. 'var $with1 = $mod.o.GetField();',
  23609. '$mod.THelper.Fly.call({',
  23610. ' get: function () {',
  23611. ' return $with1;',
  23612. ' },',
  23613. ' set: function (v) {',
  23614. ' $with1 = v;',
  23615. ' }',
  23616. '});',
  23617. '$mod.THelper.Run();',
  23618. '']));
  23619. end;
  23620. procedure TTestModule.TestTypeHelper_PassClassPropertyField;
  23621. begin
  23622. StartProgram(false);
  23623. Add([
  23624. '{$modeswitch typehelpers}',
  23625. 'type',
  23626. ' TObject = class',
  23627. ' class var FField: word;',
  23628. ' class procedure SetField(Value: word);',
  23629. ' class property Field: word read FField write SetField;',
  23630. ' end;',
  23631. ' THelper = type helper for word',
  23632. ' procedure Fly(n: byte);',
  23633. ' end;',
  23634. 'class procedure TObject.SetField(Value: word);',
  23635. 'begin',
  23636. ' Field.Fly(1);',
  23637. ' Self.Field.Fly(2);',
  23638. ' with Self do Field.Fly(3);',
  23639. ' with Self.Field do Fly(4);',
  23640. ' TObject.Field.Fly(5);',
  23641. ' with TObject do Field.Fly(6);',
  23642. ' with TObject.Field do Fly(7);',
  23643. 'end;',
  23644. 'procedure THelper.Fly(n: byte);',
  23645. 'begin',
  23646. 'end;',
  23647. 'var',
  23648. ' o: TObject;',
  23649. 'begin',
  23650. ' o.Field.Fly(11);',
  23651. ' with o do Field.Fly(12);',
  23652. ' with o.Field do Fly(13);',
  23653. ' TObject.Field.Fly(14);',
  23654. ' with TObject do Field.Fly(15);',
  23655. ' with TObject.Field do Fly(16);',
  23656. '']);
  23657. ConvertProgram;
  23658. CheckSource('TestTypeHelper_PassClassPropertyField',
  23659. LinesToStr([ // statements
  23660. 'rtl.createClass(this, "TObject", null, function () {',
  23661. ' this.FField = 0;',
  23662. ' this.$init = function () {',
  23663. ' };',
  23664. ' this.$final = function () {',
  23665. ' };',
  23666. ' this.SetField = function (Value) {',
  23667. ' $mod.THelper.Fly.call({',
  23668. ' p: this,',
  23669. ' get: function () {',
  23670. ' return this.p.FField;',
  23671. ' },',
  23672. ' set: function (v) {',
  23673. ' $mod.TObject.FField = v;',
  23674. ' }',
  23675. ' }, 1);',
  23676. ' $mod.THelper.Fly.call({',
  23677. ' p: this,',
  23678. ' get: function () {',
  23679. ' return this.p.FField;',
  23680. ' },',
  23681. ' set: function (v) {',
  23682. ' $mod.TObject.FField = v;',
  23683. ' }',
  23684. ' }, 2);',
  23685. ' $mod.THelper.Fly.call({',
  23686. ' p: this,',
  23687. ' get: function () {',
  23688. ' return this.p.FField;',
  23689. ' },',
  23690. ' set: function (v) {',
  23691. ' $mod.TObject.FField = v;',
  23692. ' }',
  23693. ' }, 3);',
  23694. ' var $with = this.FField;',
  23695. ' $mod.THelper.Fly.call({',
  23696. ' get: function () {',
  23697. ' return $with;',
  23698. ' },',
  23699. ' set: function (v) {',
  23700. ' $with = v;',
  23701. ' }',
  23702. ' }, 4);',
  23703. ' $mod.THelper.Fly.call({',
  23704. ' p: $mod.TObject,',
  23705. ' get: function () {',
  23706. ' return this.p.FField;',
  23707. ' },',
  23708. ' set: function (v) {',
  23709. ' $mod.TObject.FField = v;',
  23710. ' }',
  23711. ' }, 5);',
  23712. ' var $with1 = $mod.TObject;',
  23713. ' $mod.THelper.Fly.call({',
  23714. ' p: $with1,',
  23715. ' get: function () {',
  23716. ' return this.p.FField;',
  23717. ' },',
  23718. ' set: function (v) {',
  23719. ' $mod.TObject.FField = v;',
  23720. ' }',
  23721. ' }, 6);',
  23722. ' var $with2 = $mod.TObject.FField;',
  23723. ' $mod.THelper.Fly.call({',
  23724. ' get: function () {',
  23725. ' return $with2;',
  23726. ' },',
  23727. ' set: function (v) {',
  23728. ' $with2 = v;',
  23729. ' }',
  23730. ' }, 7);',
  23731. ' };',
  23732. '});',
  23733. 'rtl.createHelper(this, "THelper", null, function () {',
  23734. ' this.Fly = function (n) {',
  23735. ' };',
  23736. '});',
  23737. 'this.o = null;',
  23738. '']),
  23739. LinesToStr([ // $mod.$main
  23740. '$mod.THelper.Fly.call({',
  23741. ' p: $mod.o,',
  23742. ' get: function () {',
  23743. ' return this.p.FField;',
  23744. ' },',
  23745. ' set: function (v) {',
  23746. ' $mod.TObject.FField = v;',
  23747. ' }',
  23748. '}, 11);',
  23749. 'var $with = $mod.o;',
  23750. '$mod.THelper.Fly.call({',
  23751. ' p: $with,',
  23752. ' get: function () {',
  23753. ' return this.p.FField;',
  23754. ' },',
  23755. ' set: function (v) {',
  23756. ' $mod.TObject.FField = v;',
  23757. ' }',
  23758. '}, 12);',
  23759. 'var $with1 = $mod.o.FField;',
  23760. '$mod.THelper.Fly.call({',
  23761. ' get: function () {',
  23762. ' return $with1;',
  23763. ' },',
  23764. ' set: function (v) {',
  23765. ' $with1 = v;',
  23766. ' }',
  23767. '}, 13);',
  23768. '$mod.THelper.Fly.call({',
  23769. ' p: $mod.TObject,',
  23770. ' get: function () {',
  23771. ' return this.p.FField;',
  23772. ' },',
  23773. ' set: function (v) {',
  23774. ' $mod.TObject.FField = v;',
  23775. ' }',
  23776. '}, 14);',
  23777. 'var $with2 = $mod.TObject;',
  23778. '$mod.THelper.Fly.call({',
  23779. ' p: $with2,',
  23780. ' get: function () {',
  23781. ' return this.p.FField;',
  23782. ' },',
  23783. ' set: function (v) {',
  23784. ' $mod.TObject.FField = v;',
  23785. ' }',
  23786. '}, 15);',
  23787. 'var $with3 = $mod.TObject.FField;',
  23788. '$mod.THelper.Fly.call({',
  23789. ' get: function () {',
  23790. ' return $with3;',
  23791. ' },',
  23792. ' set: function (v) {',
  23793. ' $with3 = v;',
  23794. ' }',
  23795. '}, 16);',
  23796. '']));
  23797. end;
  23798. procedure TTestModule.TestTypeHelper_PassClassPropertyGetterStatic;
  23799. begin
  23800. StartProgram(false);
  23801. Add([
  23802. '{$modeswitch typehelpers}',
  23803. 'type',
  23804. ' TObject = class',
  23805. ' class var FField: word;',
  23806. ' class function GetField: word; static;',
  23807. ' class property Field: word read GetField write FField;',
  23808. ' end;',
  23809. ' THelper = type helper for word',
  23810. ' procedure Fly(n: byte);',
  23811. ' end;',
  23812. 'class function TObject.GetField: word;',
  23813. 'begin',
  23814. ' Field.Fly(1);',
  23815. ' TObject.Field.Fly(5);',
  23816. ' with TObject do Field.Fly(6);',
  23817. ' with TObject.Field do Fly(7);',
  23818. 'end;',
  23819. 'procedure THelper.Fly(n: byte);',
  23820. 'begin',
  23821. 'end;',
  23822. 'var',
  23823. ' o: TObject;',
  23824. 'begin',
  23825. ' o.Field.Fly(11);',
  23826. ' with o do Field.Fly(12);',
  23827. ' with o.Field do Fly(13);',
  23828. '']);
  23829. ConvertProgram;
  23830. CheckSource('TestTypeHelper_PassClassPropertyGetterStatic',
  23831. LinesToStr([ // statements
  23832. 'rtl.createClass(this, "TObject", null, function () {',
  23833. ' this.FField = 0;',
  23834. ' this.$init = function () {',
  23835. ' };',
  23836. ' this.$final = function () {',
  23837. ' };',
  23838. ' this.GetField = function () {',
  23839. ' var Result = 0;',
  23840. ' $mod.THelper.Fly.call({',
  23841. ' p: $mod.TObject.GetField(),',
  23842. ' get: function () {',
  23843. ' return this.p;',
  23844. ' },',
  23845. ' set: function (v) {',
  23846. ' this.p = v;',
  23847. ' }',
  23848. ' }, 1);',
  23849. ' $mod.THelper.Fly.call({',
  23850. ' p: $mod.TObject.GetField(),',
  23851. ' get: function () {',
  23852. ' return this.p;',
  23853. ' },',
  23854. ' set: function (v) {',
  23855. ' this.p = v;',
  23856. ' }',
  23857. ' }, 5);',
  23858. ' var $with = $mod.TObject;',
  23859. ' $mod.THelper.Fly.call({',
  23860. ' p: $with.GetField(),',
  23861. ' get: function () {',
  23862. ' return this.p;',
  23863. ' },',
  23864. ' set: function (v) {',
  23865. ' this.p = v;',
  23866. ' }',
  23867. ' }, 6);',
  23868. ' var $with1 = $mod.TObject.GetField();',
  23869. ' $mod.THelper.Fly.call({',
  23870. ' get: function () {',
  23871. ' return $with1;',
  23872. ' },',
  23873. ' set: function (v) {',
  23874. ' $with1 = v;',
  23875. ' }',
  23876. ' }, 7);',
  23877. ' return Result;',
  23878. ' };',
  23879. '});',
  23880. 'rtl.createHelper(this, "THelper", null, function () {',
  23881. ' this.Fly = function (n) {',
  23882. ' };',
  23883. '});',
  23884. 'this.o = null;',
  23885. '']),
  23886. LinesToStr([ // $mod.$main
  23887. '$mod.THelper.Fly.call({',
  23888. ' p: $mod.TObject.GetField(),',
  23889. ' get: function () {',
  23890. ' return this.p;',
  23891. ' },',
  23892. ' set: function (v) {',
  23893. ' this.p = v;',
  23894. ' }',
  23895. '}, 11);',
  23896. 'var $with = $mod.o;',
  23897. '$mod.THelper.Fly.call({',
  23898. ' p: $with.GetField(),',
  23899. ' get: function () {',
  23900. ' return this.p;',
  23901. ' },',
  23902. ' set: function (v) {',
  23903. ' this.p = v;',
  23904. ' }',
  23905. '}, 12);',
  23906. 'var $with1 = $mod.TObject.GetField();',
  23907. '$mod.THelper.Fly.call({',
  23908. ' get: function () {',
  23909. ' return $with1;',
  23910. ' },',
  23911. ' set: function (v) {',
  23912. ' $with1 = v;',
  23913. ' }',
  23914. '}, 13);',
  23915. '']));
  23916. end;
  23917. procedure TTestModule.TestTypeHelper_PassClassPropertyGetterNonStatic;
  23918. begin
  23919. StartProgram(false);
  23920. Add([
  23921. '{$modeswitch typehelpers}',
  23922. 'type',
  23923. ' TObject = class',
  23924. ' class var FField: word;',
  23925. ' class function GetField: word;',
  23926. ' class property Field: word read GetField write FField;',
  23927. ' end;',
  23928. ' TClass = class of TObject;',
  23929. ' THelper = type helper for word',
  23930. ' procedure Fly(n: byte);',
  23931. ' end;',
  23932. 'class function TObject.GetField: word;',
  23933. 'begin',
  23934. ' Field.Fly(1);',
  23935. ' Self.Field.Fly(5);',
  23936. ' with Self do Field.Fly(6);',
  23937. ' with Self.Field do Fly(7);',
  23938. 'end;',
  23939. 'procedure THelper.Fly(n: byte);',
  23940. 'begin',
  23941. 'end;',
  23942. 'var',
  23943. ' o: TObject;',
  23944. ' c: TClass;',
  23945. 'begin',
  23946. ' o.Field.Fly(11);',
  23947. ' with o do Field.Fly(12);',
  23948. ' with o.Field do Fly(13);',
  23949. ' c.Field.Fly(14);',
  23950. ' with c do Field.Fly(15);',
  23951. ' with c.Field do Fly(16);',
  23952. '']);
  23953. ConvertProgram;
  23954. CheckSource('TestTypeHelper_PassClassPropertyGetterNonStatic',
  23955. LinesToStr([ // statements
  23956. 'rtl.createClass(this, "TObject", null, function () {',
  23957. ' this.FField = 0;',
  23958. ' this.$init = function () {',
  23959. ' };',
  23960. ' this.$final = function () {',
  23961. ' };',
  23962. ' this.GetField = function () {',
  23963. ' var Result = 0;',
  23964. ' $mod.THelper.Fly.call({',
  23965. ' p: this.GetField(),',
  23966. ' get: function () {',
  23967. ' return this.p;',
  23968. ' },',
  23969. ' set: function (v) {',
  23970. ' this.p = v;',
  23971. ' }',
  23972. ' }, 1);',
  23973. ' $mod.THelper.Fly.call({',
  23974. ' p: this.GetField(),',
  23975. ' get: function () {',
  23976. ' return this.p;',
  23977. ' },',
  23978. ' set: function (v) {',
  23979. ' this.p = v;',
  23980. ' }',
  23981. ' }, 5);',
  23982. ' $mod.THelper.Fly.call({',
  23983. ' p: this.GetField(),',
  23984. ' get: function () {',
  23985. ' return this.p;',
  23986. ' },',
  23987. ' set: function (v) {',
  23988. ' this.p = v;',
  23989. ' }',
  23990. ' }, 6);',
  23991. ' var $with = this.GetField();',
  23992. ' $mod.THelper.Fly.call({',
  23993. ' get: function () {',
  23994. ' return $with;',
  23995. ' },',
  23996. ' set: function (v) {',
  23997. ' $with = v;',
  23998. ' }',
  23999. ' }, 7);',
  24000. ' return Result;',
  24001. ' };',
  24002. '});',
  24003. 'rtl.createHelper(this, "THelper", null, function () {',
  24004. ' this.Fly = function (n) {',
  24005. ' };',
  24006. '});',
  24007. 'this.o = null;',
  24008. 'this.c = null;',
  24009. '']),
  24010. LinesToStr([ // $mod.$main
  24011. '$mod.THelper.Fly.call({',
  24012. ' p: $mod.o.$class.GetField(),',
  24013. ' get: function () {',
  24014. ' return this.p;',
  24015. ' },',
  24016. ' set: function (v) {',
  24017. ' this.p = v;',
  24018. ' }',
  24019. '}, 11);',
  24020. 'var $with = $mod.o;',
  24021. '$mod.THelper.Fly.call({',
  24022. ' p: $with.$class.GetField(),',
  24023. ' get: function () {',
  24024. ' return this.p;',
  24025. ' },',
  24026. ' set: function (v) {',
  24027. ' this.p = v;',
  24028. ' }',
  24029. '}, 12);',
  24030. 'var $with1 = $mod.o.$class.GetField();',
  24031. '$mod.THelper.Fly.call({',
  24032. ' get: function () {',
  24033. ' return $with1;',
  24034. ' },',
  24035. ' set: function (v) {',
  24036. ' $with1 = v;',
  24037. ' }',
  24038. '}, 13);',
  24039. '$mod.THelper.Fly.call({',
  24040. ' p: $mod.c.GetField(),',
  24041. ' get: function () {',
  24042. ' return this.p;',
  24043. ' },',
  24044. ' set: function (v) {',
  24045. ' this.p = v;',
  24046. ' }',
  24047. '}, 14);',
  24048. 'var $with2 = $mod.c;',
  24049. '$mod.THelper.Fly.call({',
  24050. ' p: $with2.GetField(),',
  24051. ' get: function () {',
  24052. ' return this.p;',
  24053. ' },',
  24054. ' set: function (v) {',
  24055. ' this.p = v;',
  24056. ' }',
  24057. '}, 15);',
  24058. 'var $with3 = $mod.c.GetField();',
  24059. '$mod.THelper.Fly.call({',
  24060. ' get: function () {',
  24061. ' return $with3;',
  24062. ' },',
  24063. ' set: function (v) {',
  24064. ' $with3 = v;',
  24065. ' }',
  24066. '}, 16);',
  24067. '']));
  24068. end;
  24069. procedure TTestModule.TestTypeHelper_Property;
  24070. begin
  24071. StartProgram(false);
  24072. Add([
  24073. '{$modeswitch typehelpers}',
  24074. 'type',
  24075. ' THelper = type helper for word',
  24076. ' function GetSize: longint;',
  24077. ' procedure SetSize(Value: longint);',
  24078. ' property Size: longint read GetSize write SetSize;',
  24079. ' end;',
  24080. 'function THelper.GetSize: longint;',
  24081. 'begin',
  24082. ' Result:=Size+1;',
  24083. ' Size:=2;',
  24084. ' Result:=Self.Size+3;',
  24085. ' Self.Size:=4;',
  24086. ' with Self do begin',
  24087. ' Result:=Size+5;',
  24088. ' Size:=6;',
  24089. ' end;',
  24090. 'end;',
  24091. 'procedure THelper.SetSize(Value: longint);',
  24092. 'begin',
  24093. 'end;',
  24094. 'var w: word;',
  24095. 'begin',
  24096. ' w:=w.Size+7;',
  24097. ' w.Size:=w+8;',
  24098. ' with w do begin',
  24099. ' w:=Size+9;',
  24100. ' Size:=w+10;',
  24101. ' end;',
  24102. '']);
  24103. ConvertProgram;
  24104. CheckSource('TestTypeHelper_Property',
  24105. LinesToStr([ // statements
  24106. 'rtl.createHelper(this, "THelper", null, function () {',
  24107. ' this.GetSize = function () {',
  24108. ' var Result = 0;',
  24109. ' Result = $mod.THelper.GetSize.call(this) + 1;',
  24110. ' $mod.THelper.SetSize.call(this, 2);',
  24111. ' Result = $mod.THelper.GetSize.call(this) + 3;',
  24112. ' $mod.THelper.SetSize.call(this, 4);',
  24113. ' var $with = this.get();',
  24114. ' Result = $mod.THelper.GetSize.call(this) + 5;',
  24115. ' $mod.THelper.SetSize.call(this, 6);',
  24116. ' return Result;',
  24117. ' };',
  24118. ' this.SetSize = function (Value) {',
  24119. ' };',
  24120. '});',
  24121. 'this.w = 0;',
  24122. '']),
  24123. LinesToStr([ // $mod.$main
  24124. '$mod.w = $mod.THelper.GetSize.call({',
  24125. ' p: $mod,',
  24126. ' get: function () {',
  24127. ' return this.p.w;',
  24128. ' },',
  24129. ' set: function (v) {',
  24130. ' this.p.w = v;',
  24131. ' }',
  24132. '}) + 7;',
  24133. '$mod.THelper.SetSize.call({',
  24134. ' p: $mod,',
  24135. ' get: function () {',
  24136. ' return this.p.w;',
  24137. ' },',
  24138. ' set: function (v) {',
  24139. ' this.p.w = v;',
  24140. ' }',
  24141. '}, $mod.w + 8);',
  24142. 'var $with = $mod.w;',
  24143. '$mod.w = $mod.THelper.GetSize.call({',
  24144. ' get: function () {',
  24145. ' return $with;',
  24146. ' },',
  24147. ' set: function (v) {',
  24148. ' $with = v;',
  24149. ' }',
  24150. '}) + 9;',
  24151. '$mod.THelper.SetSize.call({',
  24152. ' get: function () {',
  24153. ' return $with;',
  24154. ' },',
  24155. ' set: function (v) {',
  24156. ' $with = v;',
  24157. ' }',
  24158. '}, $mod.w + 10);',
  24159. '']));
  24160. end;
  24161. procedure TTestModule.TestTypeHelper_Property_Array;
  24162. begin
  24163. StartProgram(false);
  24164. Add([
  24165. '{$modeswitch typehelpers}',
  24166. 'type',
  24167. ' THelper = type helper for word',
  24168. ' function GetItems(Index: byte): boolean;',
  24169. ' procedure SetItems(Index: byte; Value: boolean);',
  24170. ' property Items[Index: byte]: boolean read GetItems write SetItems;',
  24171. ' end;',
  24172. 'function THelper.GetItems(Index: byte): boolean;',
  24173. 'begin',
  24174. ' Result:=Items[1];',
  24175. ' Items[2]:=false;',
  24176. ' Result:=Self.Items[3];',
  24177. ' Self.Items[4]:=true;',
  24178. ' with Self do begin',
  24179. ' Result:=Items[5];',
  24180. ' Items[6]:=false;',
  24181. ' end;',
  24182. 'end;',
  24183. 'procedure THelper.SetItems(Index: byte; Value: boolean);',
  24184. 'begin',
  24185. 'end;',
  24186. 'var',
  24187. ' w: word;',
  24188. ' b: boolean;',
  24189. 'begin',
  24190. ' b:=w.Items[1];',
  24191. ' w.Items[2]:=b;',
  24192. ' with w do begin',
  24193. ' b:=Items[3];',
  24194. ' Items[4]:=b;',
  24195. ' end;',
  24196. '']);
  24197. ConvertProgram;
  24198. CheckSource('TestTypeHelper_Property_Array',
  24199. LinesToStr([ // statements
  24200. 'rtl.createHelper(this, "THelper", null, function () {',
  24201. ' this.GetItems = function (Index) {',
  24202. ' var Result = false;',
  24203. ' Result = $mod.THelper.GetItems.call(this, 1);',
  24204. ' $mod.THelper.SetItems.call(this, 2, false);',
  24205. ' Result = $mod.THelper.GetItems.call(this, 3);',
  24206. ' $mod.THelper.SetItems.call(this, 4, true);',
  24207. ' var $with = this.get();',
  24208. ' Result = $mod.THelper.GetItems.call(this, 5);',
  24209. ' $mod.THelper.SetItems.call(this, 6, false);',
  24210. ' return Result;',
  24211. ' };',
  24212. ' this.SetItems = function (Index, Value) {',
  24213. ' };',
  24214. '});',
  24215. 'this.w = 0;',
  24216. 'this.b = false;',
  24217. '']),
  24218. LinesToStr([ // $mod.$main
  24219. '$mod.b = $mod.THelper.GetItems.call({',
  24220. ' p: $mod,',
  24221. ' get: function () {',
  24222. ' return this.p.w;',
  24223. ' },',
  24224. ' set: function (v) {',
  24225. ' this.p.w = v;',
  24226. ' }',
  24227. '}, 1);',
  24228. '$mod.THelper.SetItems.call({',
  24229. ' p: $mod,',
  24230. ' get: function () {',
  24231. ' return this.p.w;',
  24232. ' },',
  24233. ' set: function (v) {',
  24234. ' this.p.w = v;',
  24235. ' }',
  24236. '}, 2, $mod.b);',
  24237. 'var $with = $mod.w;',
  24238. '$mod.b = $mod.THelper.GetItems.call({',
  24239. ' get: function () {',
  24240. ' return $with;',
  24241. ' },',
  24242. ' set: function (v) {',
  24243. ' $with = v;',
  24244. ' }',
  24245. '}, 3);',
  24246. '$mod.THelper.SetItems.call({',
  24247. ' get: function () {',
  24248. ' return $with;',
  24249. ' },',
  24250. ' set: function (v) {',
  24251. ' $with = v;',
  24252. ' }',
  24253. '}, 4, $mod.b);',
  24254. '']));
  24255. end;
  24256. procedure TTestModule.TestTypeHelper_ClassProperty;
  24257. begin
  24258. StartProgram(false);
  24259. Add([
  24260. '{$modeswitch typehelpers}',
  24261. 'type',
  24262. ' THelper = type helper for word',
  24263. ' class function GetSize: longint; static;',
  24264. ' class procedure SetSize(Value: longint); static;',
  24265. ' class property Size: longint read GetSize write SetSize;',
  24266. ' end;',
  24267. 'class function THelper.GetSize: longint;',
  24268. 'begin',
  24269. ' Result:=Size+1;',
  24270. ' Size:=2;',
  24271. 'end;',
  24272. 'class procedure THelper.SetSize(Value: longint);',
  24273. 'begin',
  24274. 'end;',
  24275. 'begin',
  24276. '']);
  24277. ConvertProgram;
  24278. CheckSource('TestTypeHelper_ClassProperty',
  24279. LinesToStr([ // statements
  24280. 'rtl.createHelper(this, "THelper", null, function () {',
  24281. ' this.GetSize = function () {',
  24282. ' var Result = 0;',
  24283. ' Result = $mod.THelper.GetSize() + 1;',
  24284. ' $mod.THelper.SetSize(2);',
  24285. ' return Result;',
  24286. ' };',
  24287. ' this.SetSize = function (Value) {',
  24288. ' };',
  24289. '});',
  24290. '']),
  24291. LinesToStr([ // $mod.$main
  24292. '']));
  24293. end;
  24294. procedure TTestModule.TestTypeHelper_ClassProperty_Array;
  24295. begin
  24296. StartProgram(false);
  24297. Add([
  24298. '{$modeswitch typehelpers}',
  24299. 'type',
  24300. ' THelper = type helper for word',
  24301. ' class function GetItems(Index: byte): boolean; static;',
  24302. ' class procedure SetItems(Index: byte; Value: boolean); static;',
  24303. ' class property Items[Index: byte]: boolean read GetItems write SetItems;',
  24304. ' end;',
  24305. 'class function THelper.GetItems(Index: byte): boolean;',
  24306. 'begin',
  24307. ' Result:=Items[1];',
  24308. ' Items[2]:=false;',
  24309. 'end;',
  24310. 'class procedure THelper.SetItems(Index: byte; Value: boolean);',
  24311. 'begin',
  24312. 'end;',
  24313. 'var',
  24314. ' w: word;',
  24315. ' b: boolean;',
  24316. 'begin',
  24317. ' b:=w.Items[1];',
  24318. ' w.Items[2]:=b;',
  24319. ' with w do begin',
  24320. ' b:=Items[3];',
  24321. ' Items[4]:=b;',
  24322. ' end;',
  24323. '']);
  24324. ConvertProgram;
  24325. CheckSource('TestTypeHelper_ClassProperty_Array',
  24326. LinesToStr([ // statements
  24327. 'rtl.createHelper(this, "THelper", null, function () {',
  24328. ' this.GetItems = function (Index) {',
  24329. ' var Result = false;',
  24330. ' Result = $mod.THelper.GetItems(1);',
  24331. ' $mod.THelper.SetItems(2, false);',
  24332. ' return Result;',
  24333. ' };',
  24334. ' this.SetItems = function (Index, Value) {',
  24335. ' };',
  24336. '});',
  24337. 'this.w = 0;',
  24338. 'this.b = false;',
  24339. '']),
  24340. LinesToStr([ // $mod.$main
  24341. '$mod.b = $mod.THelper.GetItems(1);',
  24342. '$mod.THelper.SetItems(2, $mod.b);',
  24343. 'var $with = $mod.w;',
  24344. '$mod.b = $mod.THelper.GetItems(3);',
  24345. '$mod.THelper.SetItems(4, $mod.b);',
  24346. '']));
  24347. end;
  24348. procedure TTestModule.TestTypeHelper_ClassMethod;
  24349. begin
  24350. StartProgram(false);
  24351. Add([
  24352. '{$modeswitch typehelpers}',
  24353. 'type',
  24354. ' THelper = type helper for word',
  24355. ' class procedure DoStatic; static;',
  24356. ' end;',
  24357. 'class procedure THelper.DoStatic;',
  24358. 'begin',
  24359. ' DoStatic;',
  24360. ' DoStatic();',
  24361. 'end;',
  24362. 'var w: word;',
  24363. 'begin',
  24364. ' w.DoStatic;',
  24365. ' w.DoStatic();',
  24366. '']);
  24367. ConvertProgram;
  24368. CheckSource('TestTypeHelper_ClassMethod',
  24369. LinesToStr([ // statements
  24370. 'rtl.createHelper(this, "THelper", null, function () {',
  24371. ' this.DoStatic = function () {',
  24372. ' $mod.THelper.DoStatic();',
  24373. ' $mod.THelper.DoStatic();',
  24374. ' };',
  24375. '});',
  24376. 'this.w = 0;',
  24377. '']),
  24378. LinesToStr([ // $mod.$main
  24379. '$mod.THelper.DoStatic();',
  24380. '$mod.THelper.DoStatic();',
  24381. '']));
  24382. end;
  24383. procedure TTestModule.TestTypeHelper_ExtClassMethodFail;
  24384. begin
  24385. StartProgram(false);
  24386. Add([
  24387. '{$modeswitch typehelpers}',
  24388. 'type',
  24389. ' THelper = type helper for word',
  24390. ' procedure Run; external name ''Run'';',
  24391. ' end;',
  24392. 'var w: word;',
  24393. 'begin',
  24394. ' w.Run;',
  24395. '']);
  24396. SetExpectedPasResolverError('Not supported: external method in type helper',nNotSupportedX);
  24397. ConvertProgram;
  24398. end;
  24399. procedure TTestModule.TestTypeHelper_Constructor;
  24400. begin
  24401. StartProgram(false);
  24402. Add([
  24403. '{$modeswitch typehelpers}',
  24404. 'type',
  24405. ' THelper = type helper for word',
  24406. ' constructor Init(e: longint);',
  24407. ' end;',
  24408. 'constructor THelper.Init(e: longint);',
  24409. 'begin',
  24410. ' Self:=e;',
  24411. ' Init(e+1);',
  24412. 'end;',
  24413. 'var w: word;',
  24414. 'begin',
  24415. ' w:=word.Init(2);',
  24416. ' w:=w.Init(3);',
  24417. ' with word do w:=Init(4);',
  24418. ' with w do w:=Init(5);',
  24419. '']);
  24420. ConvertProgram;
  24421. CheckSource('TestTypeHelper_Constructor',
  24422. LinesToStr([ // statements
  24423. 'rtl.createHelper(this, "THelper", null, function () {',
  24424. ' this.Init = function (e) {',
  24425. ' this.set(e);',
  24426. ' $mod.THelper.Init.call(this, e + 1);',
  24427. ' return this.get();',
  24428. ' };',
  24429. ' this.$new = function (fn, args) {',
  24430. ' return this[fn].apply({',
  24431. ' p: 0,',
  24432. ' get: function () {',
  24433. ' return this.p;',
  24434. ' },',
  24435. ' set: function (v) {',
  24436. ' this.p = v;',
  24437. ' }',
  24438. ' }, args);',
  24439. ' };',
  24440. '});',
  24441. 'this.w = 0;',
  24442. '']),
  24443. LinesToStr([ // $mod.$main
  24444. '$mod.w = $mod.THelper.$new("Init", [2]);',
  24445. '$mod.w = $mod.THelper.Init.call({',
  24446. ' p: $mod,',
  24447. ' get: function () {',
  24448. ' return this.p.w;',
  24449. ' },',
  24450. ' set: function (v) {',
  24451. ' this.p.w = v;',
  24452. ' }',
  24453. '}, 3);',
  24454. '$mod.w = $mod.THelper.$new("Init", [4]);',
  24455. 'var $with = $mod.w;',
  24456. '$mod.w = $mod.THelper.Init.call({',
  24457. ' get: function () {',
  24458. ' return $with;',
  24459. ' },',
  24460. ' set: function (v) {',
  24461. ' $with = v;',
  24462. ' }',
  24463. '}, 5);',
  24464. '']));
  24465. end;
  24466. procedure TTestModule.TestTypeHelper_Word;
  24467. begin
  24468. StartProgram(false);
  24469. Add([
  24470. '{$modeswitch typehelpers}',
  24471. 'type',
  24472. ' THelper = type helper for word',
  24473. ' procedure DoIt(e: byte = 123);',
  24474. ' end;',
  24475. 'procedure THelper.DoIt(e: byte);',
  24476. 'begin',
  24477. ' Self:=e;',
  24478. ' Self:=Self+1;',
  24479. ' with Self do Doit;',
  24480. 'end;',
  24481. 'begin',
  24482. ' word(3).DoIt;',
  24483. '']);
  24484. ConvertProgram;
  24485. CheckSource('TestTypeHelper_Word',
  24486. LinesToStr([ // statements
  24487. 'rtl.createHelper(this, "THelper", null, function () {',
  24488. ' this.DoIt = function (e) {',
  24489. ' this.set(e);',
  24490. ' this.set(this.get() + 1);',
  24491. ' var $with = this.get();',
  24492. ' $mod.THelper.DoIt.call(this, 123);',
  24493. ' };',
  24494. '});',
  24495. '']),
  24496. LinesToStr([ // $mod.$main
  24497. '$mod.THelper.DoIt.call({',
  24498. ' get: function () {',
  24499. ' return 3;',
  24500. ' },',
  24501. ' set: function (v) {',
  24502. ' rtl.raiseE("EPropReadOnly");',
  24503. ' }',
  24504. '}, 123);',
  24505. '']));
  24506. end;
  24507. procedure TTestModule.TestTypeHelper_Boolean;
  24508. begin
  24509. StartProgram(false);
  24510. Add([
  24511. '{$modeswitch typehelpers}',
  24512. 'type',
  24513. ' Integer = longint;',
  24514. ' THelper = type helper for boolean',
  24515. ' procedure Run(e: wordbool = true);',
  24516. ' end;',
  24517. 'procedure THelper.Run(e: wordbool);',
  24518. 'begin',
  24519. ' Self:=e;',
  24520. ' Self:=not Self;',
  24521. ' with Self do Run;',
  24522. ' if Integer(Self)=0 then ;',
  24523. 'end;',
  24524. 'begin',
  24525. ' boolean(3).Run;',
  24526. '']);
  24527. ConvertProgram;
  24528. CheckSource('TestTypeHelper_Boolean',
  24529. LinesToStr([ // statements
  24530. 'rtl.createHelper(this, "THelper", null, function () {',
  24531. ' this.Run = function (e) {',
  24532. ' this.set(e);',
  24533. ' this.set(!this.get());',
  24534. ' var $with = this.get();',
  24535. ' $mod.THelper.Run.call(this, true);',
  24536. ' if ((this.get() ? 1 : 0) === 0) ;',
  24537. ' };',
  24538. '});',
  24539. '']),
  24540. LinesToStr([ // $mod.$main
  24541. '$mod.THelper.Run.call({',
  24542. ' a: 3 != 0,',
  24543. ' get: function () {',
  24544. ' return this.a;',
  24545. ' },',
  24546. ' set: function (v) {',
  24547. ' rtl.raiseE("EPropReadOnly");',
  24548. ' }',
  24549. '}, true);',
  24550. '']));
  24551. end;
  24552. procedure TTestModule.TestTypeHelper_WordBool;
  24553. begin
  24554. StartProgram(false);
  24555. Add([
  24556. '{$modeswitch typehelpers}',
  24557. 'type',
  24558. ' Integer = longint;',
  24559. ' THelper = type helper for WordBool',
  24560. ' procedure Run(e: wordbool = true);',
  24561. ' end;',
  24562. 'procedure THelper.Run(e: wordbool);',
  24563. 'var i: integer;',
  24564. 'begin',
  24565. ' i:=Integer(Self);',
  24566. 'end;',
  24567. 'var w: wordbool;',
  24568. 'begin',
  24569. ' w.Run;',
  24570. ' wordbool(3).Run;',
  24571. '']);
  24572. ConvertProgram;
  24573. CheckSource('TestTypeHelper_WordBool',
  24574. LinesToStr([ // statements
  24575. 'rtl.createHelper(this, "THelper", null, function () {',
  24576. ' this.Run = function (e) {',
  24577. ' var i = 0;',
  24578. ' i = (this.get() ? 1 : 0);',
  24579. ' };',
  24580. '});',
  24581. 'this.w = false;',
  24582. '']),
  24583. LinesToStr([ // $mod.$main
  24584. '$mod.THelper.Run.call({',
  24585. ' p: $mod,',
  24586. ' get: function () {',
  24587. ' return this.p.w;',
  24588. ' },',
  24589. ' set: function (v) {',
  24590. ' this.p.w = v;',
  24591. ' }',
  24592. '}, true);',
  24593. '$mod.THelper.Run.call({',
  24594. ' a: 3 != 0,',
  24595. ' get: function () {',
  24596. ' return this.a;',
  24597. ' },',
  24598. ' set: function (v) {',
  24599. ' rtl.raiseE("EPropReadOnly");',
  24600. ' }',
  24601. '}, true);',
  24602. '']));
  24603. end;
  24604. procedure TTestModule.TestTypeHelper_Double;
  24605. begin
  24606. StartProgram(false);
  24607. Add([
  24608. '{$modeswitch typehelpers}',
  24609. 'type',
  24610. ' Float = type double;',
  24611. ' THelper = type helper for Float',
  24612. ' const NPI = 3.141592;',
  24613. ' function ToStr: String;',
  24614. ' end;',
  24615. 'function THelper.ToStr: String;',
  24616. 'begin',
  24617. 'end;',
  24618. 'procedure DoIt(s: string);',
  24619. 'begin',
  24620. 'end;',
  24621. 'var f: Float;',
  24622. 'begin',
  24623. ' DoIt(f.toStr);',
  24624. ' DoIt(f.toStr());',
  24625. ' (f*f).toStr;',
  24626. ' DoIt((f*f).toStr);',
  24627. '']);
  24628. ConvertProgram;
  24629. CheckSource('TestTypeHelper_Double',
  24630. LinesToStr([ // statements
  24631. 'rtl.createHelper(this, "THelper", null, function () {',
  24632. ' this.NPI = 3.141592;',
  24633. ' this.ToStr = function () {',
  24634. ' var Result = "";',
  24635. ' return Result;',
  24636. ' };',
  24637. '});',
  24638. 'this.DoIt = function (s) {',
  24639. '};',
  24640. 'this.f = 0.0;',
  24641. '']),
  24642. LinesToStr([ // $mod.$main
  24643. '$mod.DoIt($mod.THelper.ToStr.call({',
  24644. ' p: $mod,',
  24645. ' get: function () {',
  24646. ' return this.p.f;',
  24647. ' },',
  24648. ' set: function (v) {',
  24649. ' this.p.f = v;',
  24650. ' }',
  24651. '}));',
  24652. '$mod.DoIt($mod.THelper.ToStr.call({',
  24653. ' p: $mod,',
  24654. ' get: function () {',
  24655. ' return this.p.f;',
  24656. ' },',
  24657. ' set: function (v) {',
  24658. ' this.p.f = v;',
  24659. ' }',
  24660. '}));',
  24661. '$mod.THelper.ToStr.call({',
  24662. ' a: $mod.f * $mod.f,',
  24663. ' get: function () {',
  24664. ' return this.a;',
  24665. ' },',
  24666. ' set: function (v) {',
  24667. ' rtl.raiseE("EPropReadOnly");',
  24668. ' }',
  24669. '});',
  24670. '$mod.DoIt($mod.THelper.ToStr.call({',
  24671. ' a: $mod.f * $mod.f,',
  24672. ' get: function () {',
  24673. ' return this.a;',
  24674. ' },',
  24675. ' set: function (v) {',
  24676. ' rtl.raiseE("EPropReadOnly");',
  24677. ' }',
  24678. '}));',
  24679. '']));
  24680. end;
  24681. procedure TTestModule.TestTypeHelper_NativeInt;
  24682. begin
  24683. StartProgram(false);
  24684. Add([
  24685. '{$modeswitch typehelpers}',
  24686. 'type',
  24687. ' MaxInt = type nativeint;',
  24688. ' THelperI = type helper for MaxInt',
  24689. ' function ToStr: String;',
  24690. ' end;',
  24691. ' MaxUInt = type nativeuint;',
  24692. ' THelperU = type helper for MaxUInt',
  24693. ' function ToStr: String;',
  24694. ' end;',
  24695. 'function THelperI.ToStr: String;',
  24696. 'begin',
  24697. ' Result:=str(Self);',
  24698. 'end;',
  24699. 'function THelperU.ToStr: String;',
  24700. 'begin',
  24701. ' Result:=str(Self);',
  24702. 'end;',
  24703. 'procedure DoIt(s: string);',
  24704. 'begin',
  24705. 'end;',
  24706. 'var i: MaxInt;',
  24707. 'begin',
  24708. ' DoIt(i.toStr);',
  24709. ' DoIt(i.toStr());',
  24710. ' (i*i).toStr;',
  24711. ' DoIt((i*i).toStr);',
  24712. '']);
  24713. ConvertProgram;
  24714. CheckSource('TestTypeHelper_NativeInt',
  24715. LinesToStr([ // statements
  24716. 'rtl.createHelper(this, "THelperI", null, function () {',
  24717. ' this.ToStr = function () {',
  24718. ' var Result = "";',
  24719. ' Result = "" + this.get();',
  24720. ' return Result;',
  24721. ' };',
  24722. '});',
  24723. 'rtl.createHelper(this, "THelperU", null, function () {',
  24724. ' this.ToStr = function () {',
  24725. ' var Result = "";',
  24726. ' Result = "" + this.get();',
  24727. ' return Result;',
  24728. ' };',
  24729. '});',
  24730. 'this.DoIt = function (s) {',
  24731. '};',
  24732. 'this.i = 0;',
  24733. '']),
  24734. LinesToStr([ // $mod.$main
  24735. '$mod.DoIt($mod.THelperI.ToStr.call({',
  24736. ' p: $mod,',
  24737. ' get: function () {',
  24738. ' return this.p.i;',
  24739. ' },',
  24740. ' set: function (v) {',
  24741. ' this.p.i = v;',
  24742. ' }',
  24743. '}));',
  24744. '$mod.DoIt($mod.THelperI.ToStr.call({',
  24745. ' p: $mod,',
  24746. ' get: function () {',
  24747. ' return this.p.i;',
  24748. ' },',
  24749. ' set: function (v) {',
  24750. ' this.p.i = v;',
  24751. ' }',
  24752. '}));',
  24753. '$mod.THelperI.ToStr.call({',
  24754. ' a: $mod.i * $mod.i,',
  24755. ' get: function () {',
  24756. ' return this.a;',
  24757. ' },',
  24758. ' set: function (v) {',
  24759. ' rtl.raiseE("EPropReadOnly");',
  24760. ' }',
  24761. '});',
  24762. '$mod.DoIt($mod.THelperI.ToStr.call({',
  24763. ' a: $mod.i * $mod.i,',
  24764. ' get: function () {',
  24765. ' return this.a;',
  24766. ' },',
  24767. ' set: function (v) {',
  24768. ' rtl.raiseE("EPropReadOnly");',
  24769. ' }',
  24770. '}));',
  24771. '']));
  24772. end;
  24773. procedure TTestModule.TestTypeHelper_StringChar;
  24774. begin
  24775. StartProgram(false);
  24776. Add([
  24777. '{$modeswitch typehelpers}',
  24778. 'type',
  24779. ' TStringHelper = type helper for string',
  24780. ' procedure DoIt(e: byte = 123);',
  24781. ' end;',
  24782. ' TCharHelper = type helper for char',
  24783. ' procedure Fly;',
  24784. ' end;',
  24785. 'procedure TStringHelper.DoIt(e: byte);',
  24786. 'begin',
  24787. ' Self[1]:=''c'';',
  24788. ' Self[2]:=Self[3];',
  24789. 'end;',
  24790. 'procedure TCharHelper.Fly;',
  24791. 'begin',
  24792. ' Self:=''c'';',
  24793. 'end;',
  24794. 'begin',
  24795. ' ''abc''.DoIt;',
  24796. ' ''xyz''.DoIt();',
  24797. ' ''c''.Fly();',
  24798. '']);
  24799. ConvertProgram;
  24800. CheckSource('TestTypeHelper_StringChar',
  24801. LinesToStr([ // statements
  24802. 'rtl.createHelper(this, "TStringHelper", null, function () {',
  24803. ' this.DoIt = function (e) {',
  24804. ' this.set(rtl.setCharAt(this.get(), 0, "c"));',
  24805. ' this.set(rtl.setCharAt(this.get(), 1, this.get().charAt(2)));',
  24806. ' };',
  24807. '});',
  24808. 'rtl.createHelper(this, "TCharHelper", null, function () {',
  24809. ' this.Fly = function () {',
  24810. ' this.set("c");',
  24811. ' };',
  24812. '});',
  24813. '']),
  24814. LinesToStr([ // $mod.$main
  24815. '$mod.TStringHelper.DoIt.call({',
  24816. ' get: function () {',
  24817. ' return "abc";',
  24818. ' },',
  24819. ' set: function (v) {',
  24820. ' rtl.raiseE("EPropReadOnly");',
  24821. ' }',
  24822. '}, 123);',
  24823. '$mod.TStringHelper.DoIt.call({',
  24824. ' get: function () {',
  24825. ' return "xyz";',
  24826. ' },',
  24827. ' set: function (v) {',
  24828. ' rtl.raiseE("EPropReadOnly");',
  24829. ' }',
  24830. '}, 123);',
  24831. '$mod.TCharHelper.Fly.call({',
  24832. ' get: function () {',
  24833. ' return "c";',
  24834. ' },',
  24835. ' set: function (v) {',
  24836. ' rtl.raiseE("EPropReadOnly");',
  24837. ' }',
  24838. '});',
  24839. '']));
  24840. end;
  24841. procedure TTestModule.TestTypeHelper_JSValue;
  24842. begin
  24843. StartProgram(false);
  24844. Add([
  24845. '{$modeswitch typehelpers}',
  24846. 'type',
  24847. ' TExtValue = type jsvalue;',
  24848. ' THelper = type helper for TExtValue',
  24849. ' function ToStr: String;',
  24850. ' end;',
  24851. 'function THelper.ToStr: String;',
  24852. 'begin',
  24853. 'end;',
  24854. 'var',
  24855. ' s: string;',
  24856. ' v: TExtValue;',
  24857. 'begin',
  24858. ' s:=v.toStr;',
  24859. ' s:=v.toStr();',
  24860. ' TExtValue(s).toStr;',
  24861. '']);
  24862. ConvertProgram;
  24863. CheckSource('TestTypeHelper_JSValue',
  24864. LinesToStr([ // statements
  24865. 'rtl.createHelper(this, "THelper", null, function () {',
  24866. ' this.ToStr = function () {',
  24867. ' var Result = "";',
  24868. ' return Result;',
  24869. ' };',
  24870. '});',
  24871. 'this.s = "";',
  24872. 'this.v = undefined;',
  24873. '']),
  24874. LinesToStr([ // $mod.$main
  24875. '$mod.s = $mod.THelper.ToStr.call({',
  24876. ' p: $mod,',
  24877. ' get: function () {',
  24878. ' return this.p.v;',
  24879. ' },',
  24880. ' set: function (v) {',
  24881. ' this.p.v = v;',
  24882. ' }',
  24883. '});',
  24884. '$mod.s = $mod.THelper.ToStr.call({',
  24885. ' p: $mod,',
  24886. ' get: function () {',
  24887. ' return this.p.v;',
  24888. ' },',
  24889. ' set: function (v) {',
  24890. ' this.p.v = v;',
  24891. ' }',
  24892. '});',
  24893. '$mod.THelper.ToStr.call({',
  24894. ' p: $mod,',
  24895. ' get: function () {',
  24896. ' return this.p.s;',
  24897. ' },',
  24898. ' set: function (v) {',
  24899. ' rtl.raiseE("EPropReadOnly");',
  24900. ' }',
  24901. '});',
  24902. '']));
  24903. end;
  24904. procedure TTestModule.TestTypeHelper_Array;
  24905. begin
  24906. StartProgram(false);
  24907. Add([
  24908. '{$modeswitch typehelpers}',
  24909. 'type',
  24910. ' TArrOfBool = array of boolean;',
  24911. ' TArrOfJS = array of jsvalue;',
  24912. ' THelper = type helper for TArrOfBool',
  24913. ' procedure DoIt(e: byte = 123);',
  24914. ' end;',
  24915. 'procedure THelper.DoIt(e: byte);',
  24916. 'begin',
  24917. ' Self[1]:=true;',
  24918. ' Self[2]:=not Self[3];',
  24919. ' SetLength(Self,4);',
  24920. 'end;',
  24921. 'var',
  24922. ' b: TArrOfBool;',
  24923. ' j: TArrOfJS;',
  24924. 'begin',
  24925. ' b.DoIt;',
  24926. ' TArrOfBool(j).DoIt();',
  24927. '']);
  24928. ConvertProgram;
  24929. CheckSource('TestTypeHelper_Array',
  24930. LinesToStr([ // statements
  24931. 'rtl.createHelper(this, "THelper", null, function () {',
  24932. ' this.DoIt = function (e) {',
  24933. ' this.get()[1] = true;',
  24934. ' this.get()[2] = !this.get()[3];',
  24935. ' this.set(rtl.arraySetLength(this.get(), false, 4));',
  24936. ' };',
  24937. '});',
  24938. 'this.b = [];',
  24939. 'this.j = [];',
  24940. '']),
  24941. LinesToStr([ // $mod.$main
  24942. '$mod.THelper.DoIt.call({',
  24943. ' p: $mod,',
  24944. ' get: function () {',
  24945. ' return this.p.b;',
  24946. ' },',
  24947. ' set: function (v) {',
  24948. ' this.p.b = v;',
  24949. ' }',
  24950. '}, 123);',
  24951. '$mod.THelper.DoIt.call({',
  24952. ' p: $mod,',
  24953. ' get: function () {',
  24954. ' return this.p.j;',
  24955. ' },',
  24956. ' set: function (v) {',
  24957. ' this.p.j = v;',
  24958. ' }',
  24959. '}, 123);',
  24960. '']));
  24961. end;
  24962. procedure TTestModule.TestTypeHelper_EnumType;
  24963. begin
  24964. StartProgram(false);
  24965. Add([
  24966. '{$modeswitch typehelpers}',
  24967. 'type',
  24968. ' TEnum = (red,blue);',
  24969. ' THelper = type helper for TEnum',
  24970. ' procedure DoIt(e: byte = 123);',
  24971. ' class procedure Swing(w: word); static;',
  24972. ' end;',
  24973. 'procedure THelper.DoIt(e: byte);',
  24974. 'begin',
  24975. ' Self:=red;',
  24976. ' Self:=succ(Self);',
  24977. ' with Self do Doit;',
  24978. 'end;',
  24979. 'class procedure THelper.Swing(w: word);',
  24980. 'begin',
  24981. 'end;',
  24982. 'var e: TEnum;',
  24983. 'begin',
  24984. ' e.DoIt;',
  24985. ' red.DoIt;',
  24986. ' TEnum.blue.DoIt;',
  24987. ' TEnum(1).DoIt;',
  24988. ' TEnum.Swing(3);',
  24989. '']);
  24990. ConvertProgram;
  24991. CheckSource('TestTypeHelper_EnumType',
  24992. LinesToStr([ // statements
  24993. 'this.TEnum = {',
  24994. ' "0": "red",',
  24995. ' red: 0,',
  24996. ' "1": "blue",',
  24997. ' blue: 1',
  24998. '};',
  24999. 'rtl.createHelper(this, "THelper", null, function () {',
  25000. ' this.DoIt = function (e) {',
  25001. ' this.set($mod.TEnum.red);',
  25002. ' this.set(this.get() + 1);',
  25003. ' var $with = this.get();',
  25004. ' $mod.THelper.DoIt.call(this, 123);',
  25005. ' };',
  25006. ' this.Swing = function (w) {',
  25007. ' };',
  25008. '});',
  25009. 'this.e = 0;',
  25010. '']),
  25011. LinesToStr([ // $mod.$main
  25012. '$mod.THelper.DoIt.call({',
  25013. ' p: $mod,',
  25014. ' get: function () {',
  25015. ' return this.p.e;',
  25016. ' },',
  25017. ' set: function (v) {',
  25018. ' this.p.e = v;',
  25019. ' }',
  25020. '}, 123);',
  25021. '$mod.THelper.DoIt.call({',
  25022. ' p: $mod.TEnum,',
  25023. ' get: function () {',
  25024. ' return this.p.red;',
  25025. ' },',
  25026. ' set: function (v) {',
  25027. ' rtl.raiseE("EPropReadOnly");',
  25028. ' }',
  25029. '}, 123);',
  25030. '$mod.THelper.DoIt.call({',
  25031. ' p: $mod.TEnum,',
  25032. ' get: function () {',
  25033. ' return this.p.blue;',
  25034. ' },',
  25035. ' set: function (v) {',
  25036. ' rtl.raiseE("EPropReadOnly");',
  25037. ' }',
  25038. '}, 123);',
  25039. '$mod.THelper.DoIt.call({',
  25040. ' get: function () {',
  25041. ' return 1;',
  25042. ' },',
  25043. ' set: function (v) {',
  25044. ' rtl.raiseE("EPropReadOnly");',
  25045. ' }',
  25046. '}, 123);',
  25047. '$mod.THelper.Swing(3);',
  25048. '']));
  25049. end;
  25050. procedure TTestModule.TestTypeHelper_SetType;
  25051. begin
  25052. StartProgram(false);
  25053. Add([
  25054. '{$modeswitch typehelpers}',
  25055. 'type',
  25056. ' TEnum = (red,blue);',
  25057. ' TSetOfEnum = set of TEnum;',
  25058. ' THelper = type helper for TSetOfEnum',
  25059. ' procedure DoIt(e: byte = 123);',
  25060. ' constructor Init(e: TEnum);',
  25061. ' constructor InitEmpty;',
  25062. ' end;',
  25063. 'procedure THelper.DoIt(e: byte);',
  25064. 'begin',
  25065. ' Self:=[];',
  25066. ' Self:=[red];',
  25067. ' Include(Self,blue);',
  25068. 'end;',
  25069. 'constructor THelper.Init(e: TEnum);',
  25070. 'begin',
  25071. ' Self:=[];',
  25072. ' Self:=[e];',
  25073. ' Include(Self,blue);',
  25074. 'end;',
  25075. 'constructor THelper.InitEmpty;',
  25076. 'begin',
  25077. 'end;',
  25078. 'var s: TSetOfEnum;',
  25079. 'begin',
  25080. ' s.DoIt;',
  25081. //' [red].DoIt;',
  25082. //' with s do DoIt;',
  25083. //' with [red,blue] do DoIt;',
  25084. ' s:=TSetOfEnum.Init(blue);',
  25085. ' s:=s.Init(blue);',
  25086. '']);
  25087. ConvertProgram;
  25088. CheckSource('TestTypeHelper_SetType',
  25089. LinesToStr([ // statements
  25090. 'this.TEnum = {',
  25091. ' "0": "red",',
  25092. ' red: 0,',
  25093. ' "1": "blue",',
  25094. ' blue: 1',
  25095. '};',
  25096. 'rtl.createHelper(this, "THelper", null, function () {',
  25097. ' this.DoIt = function (e) {',
  25098. ' this.set({});',
  25099. ' this.set(rtl.createSet($mod.TEnum.red));',
  25100. ' this.set(rtl.includeSet(this.get(), $mod.TEnum.blue));',
  25101. ' };',
  25102. ' this.Init = function (e) {',
  25103. ' this.set({});',
  25104. ' this.set(rtl.createSet(e));',
  25105. ' this.set(rtl.includeSet(this.get(), $mod.TEnum.blue));',
  25106. ' return this.get();',
  25107. ' };',
  25108. ' this.InitEmpty = function () {',
  25109. ' return this.get();',
  25110. ' };',
  25111. ' this.$new = function (fn, args) {',
  25112. ' return this[fn].apply({',
  25113. ' p: {},',
  25114. ' get: function () {',
  25115. ' return this.p;',
  25116. ' },',
  25117. ' set: function (v) {',
  25118. ' this.p = v;',
  25119. ' }',
  25120. ' }, args);',
  25121. ' };',
  25122. '});',
  25123. 'this.s = {};',
  25124. '']),
  25125. LinesToStr([ // $mod.$main
  25126. '$mod.THelper.DoIt.call({',
  25127. ' p: $mod,',
  25128. ' get: function () {',
  25129. ' return this.p.s;',
  25130. ' },',
  25131. ' set: function (v) {',
  25132. ' this.p.s = v;',
  25133. ' }',
  25134. '}, 123);',
  25135. '$mod.s = rtl.refSet($mod.THelper.$new("Init", [$mod.TEnum.blue]));',
  25136. '$mod.s = rtl.refSet($mod.THelper.Init.call({',
  25137. ' p: $mod,',
  25138. ' get: function () {',
  25139. ' return this.p.s;',
  25140. ' },',
  25141. ' set: function (v) {',
  25142. ' this.p.s = v;',
  25143. ' }',
  25144. '}, $mod.TEnum.blue));',
  25145. '']));
  25146. end;
  25147. procedure TTestModule.TestTypeHelper_InterfaceType;
  25148. begin
  25149. StartProgram(false);
  25150. Add([
  25151. '{$interfaces com}',
  25152. '{$modeswitch typehelpers}',
  25153. 'type',
  25154. ' IUnknown = interface',
  25155. ' function _AddRef: longint;',
  25156. ' function _Release: longint;',
  25157. ' end;',
  25158. ' TObject = class(IUnknown)',
  25159. ' function _AddRef: longint; virtual; abstract;',
  25160. ' function _Release: longint; virtual; abstract;',
  25161. ' end;',
  25162. ' THelper = type helper for IUnknown',
  25163. ' procedure Fly(e: byte = 123);',
  25164. ' class procedure Run; static;',
  25165. ' end;',
  25166. 'var',
  25167. ' i: IUnknown;',
  25168. ' o: TObject;',
  25169. 'procedure THelper.Fly(e: byte);',
  25170. 'begin',
  25171. ' i:=Self;',
  25172. ' o:=Self as TObject;',
  25173. ' Self:=nil;',
  25174. ' Self:=i;',
  25175. ' Self:=o;',
  25176. ' with Self do begin',
  25177. ' Fly;',
  25178. ' Fly();',
  25179. ' end;',
  25180. 'end;',
  25181. 'class procedure THelper.Run;',
  25182. 'var l: IUnknown;',
  25183. 'begin',
  25184. ' l.Fly;',
  25185. ' l.Fly();',
  25186. 'end;',
  25187. 'begin',
  25188. ' i.Fly;',
  25189. ' i.Fly();',
  25190. ' i.Run;',
  25191. ' i.Run();',
  25192. ' IUnknown.Run;',
  25193. ' IUnknown.Run();',
  25194. '']);
  25195. ConvertProgram;
  25196. CheckSource('TestTypeHelper_InterfaceType',
  25197. LinesToStr([ // statements
  25198. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  25199. 'rtl.createClass(this, "TObject", null, function () {',
  25200. ' this.$init = function () {',
  25201. ' };',
  25202. ' this.$final = function () {',
  25203. ' };',
  25204. ' rtl.addIntf(this, $mod.IUnknown);',
  25205. '});',
  25206. 'rtl.createHelper(this, "THelper", null, function () {',
  25207. ' this.Fly = function (e) {',
  25208. ' var $ir = rtl.createIntfRefs();',
  25209. ' try {',
  25210. ' rtl.setIntfP($mod, "i", this.get());',
  25211. ' $mod.o = rtl.intfAsClass(this.get(), $mod.TObject);',
  25212. ' this.set(null);',
  25213. ' this.set($mod.i);',
  25214. ' this.set($ir.ref(1, rtl.queryIntfT($mod.o, $mod.IUnknown)));',
  25215. ' var $with = this.get();',
  25216. ' $mod.THelper.Fly.call(this, 123);',
  25217. ' $mod.THelper.Fly.call(this, 123);',
  25218. ' } finally {',
  25219. ' $ir.free();',
  25220. ' };',
  25221. ' };',
  25222. ' this.Run = function () {',
  25223. ' var l = null;',
  25224. ' try {',
  25225. ' $mod.THelper.Fly.call({',
  25226. ' get: function () {',
  25227. ' return l;',
  25228. ' },',
  25229. ' set: function (v) {',
  25230. ' l = rtl.setIntfL(l, v);',
  25231. ' }',
  25232. ' }, 123);',
  25233. ' $mod.THelper.Fly.call({',
  25234. ' get: function () {',
  25235. ' return l;',
  25236. ' },',
  25237. ' set: function (v) {',
  25238. ' l = rtl.setIntfL(l, v);',
  25239. ' }',
  25240. ' }, 123);',
  25241. ' } finally {',
  25242. ' rtl._Release(l);',
  25243. ' };',
  25244. ' };',
  25245. '});',
  25246. 'this.i = null;',
  25247. 'this.o = null;',
  25248. '']),
  25249. LinesToStr([ // $mod.$main
  25250. '$mod.THelper.Fly.call({',
  25251. ' p: $mod,',
  25252. ' get: function () {',
  25253. ' return this.p.i;',
  25254. ' },',
  25255. ' set: function (v) {',
  25256. ' rtl.setIntfP(this.p, "i", v);',
  25257. ' }',
  25258. '}, 123);',
  25259. '$mod.THelper.Fly.call({',
  25260. ' p: $mod,',
  25261. ' get: function () {',
  25262. ' return this.p.i;',
  25263. ' },',
  25264. ' set: function (v) {',
  25265. ' rtl.setIntfP(this.p, "i", v);',
  25266. ' }',
  25267. '}, 123);',
  25268. '$mod.THelper.Run();',
  25269. '$mod.THelper.Run();',
  25270. '$mod.THelper.Run();',
  25271. '$mod.THelper.Run();',
  25272. '']));
  25273. end;
  25274. procedure TTestModule.TestTypeHelper_NestedSelf;
  25275. begin
  25276. StartProgram(false);
  25277. Add([
  25278. '{$modeswitch typehelpers}',
  25279. 'type',
  25280. ' THelper = type helper for string',
  25281. ' procedure Run(Value: string);',
  25282. ' end;',
  25283. 'procedure THelper.Run(Value: string);',
  25284. ' function Sub(i: nativeint): boolean;',
  25285. ' begin',
  25286. ' Result:=Self[i+1]=Value[i];',
  25287. ' end;',
  25288. 'begin',
  25289. ' if Self[3]=Value[4] then ;',
  25290. 'end;',
  25291. 'begin',
  25292. '']);
  25293. ConvertProgram;
  25294. CheckSource('TestTypeHelper_NestedSelf',
  25295. LinesToStr([ // statements
  25296. 'rtl.createHelper(this, "THelper", null, function () {',
  25297. ' this.Run = function (Value) {',
  25298. ' var $Self = this;',
  25299. ' function Sub(i) {',
  25300. ' var Result = false;',
  25301. ' Result = $Self.get().charAt((i + 1) - 1) === Value.charAt(i - 1);',
  25302. ' return Result;',
  25303. ' };',
  25304. ' if ($Self.get().charAt(2) === Value.charAt(3)) ;',
  25305. ' };',
  25306. '});',
  25307. '']),
  25308. LinesToStr([ // $mod.$main
  25309. '']));
  25310. end;
  25311. procedure TTestModule.TestProcType;
  25312. begin
  25313. StartProgram(false);
  25314. Add([
  25315. 'type',
  25316. ' TProcInt = procedure(vI: longint = 1);',
  25317. 'procedure DoIt(vJ: longint);',
  25318. 'begin end;',
  25319. 'var',
  25320. ' b: boolean;',
  25321. ' vP, vQ: tprocint;',
  25322. 'begin',
  25323. ' vp:=nil;',
  25324. ' vp:=vp;',
  25325. ' vp:=@doit;',
  25326. ' vp;',
  25327. ' vp();',
  25328. ' vp(2);',
  25329. ' b:=vp=nil;',
  25330. ' b:=nil=vp;',
  25331. ' b:=vp=vq;',
  25332. ' b:=vp=@doit;',
  25333. ' b:=@doit=vp;',
  25334. ' b:=vp<>nil;',
  25335. ' b:=nil<>vp;',
  25336. ' b:=vp<>vq;',
  25337. ' b:=vp<>@doit;',
  25338. ' b:=@doit<>vp;',
  25339. ' b:=Assigned(vp);',
  25340. ' if Assigned(vp) then ;']);
  25341. ConvertProgram;
  25342. CheckSource('TestProcType',
  25343. LinesToStr([ // statements
  25344. 'this.DoIt = function(vJ) {',
  25345. '};',
  25346. 'this.b = false;',
  25347. 'this.vP = null;',
  25348. 'this.vQ = null;'
  25349. ]),
  25350. LinesToStr([ // $mod.$main
  25351. '$mod.vP = null;',
  25352. '$mod.vP = $mod.vP;',
  25353. '$mod.vP = $mod.DoIt;',
  25354. '$mod.vP(1);',
  25355. '$mod.vP(1);',
  25356. '$mod.vP(2);',
  25357. '$mod.b = $mod.vP === null;',
  25358. '$mod.b = null === $mod.vP;',
  25359. '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
  25360. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  25361. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  25362. '$mod.b = $mod.vP !== null;',
  25363. '$mod.b = null !== $mod.vP;',
  25364. '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
  25365. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  25366. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  25367. '$mod.b = $mod.vP != null;',
  25368. 'if ($mod.vP != null) ;',
  25369. '']));
  25370. end;
  25371. procedure TTestModule.TestProcType_Arg;
  25372. begin
  25373. StartProgram(false);
  25374. Add([
  25375. 'type',
  25376. ' TProcInt = procedure(vI: longint = 1);',
  25377. 'procedure DoIt(vJ: longint); begin end;',
  25378. 'procedure DoSome(vP, vQ: TProcInt);',
  25379. 'var',
  25380. ' b: boolean;',
  25381. 'begin',
  25382. ' vp:=nil;',
  25383. ' vp:=vp;',
  25384. ' vp:=@doit;',
  25385. ' vp;',
  25386. ' vp();',
  25387. ' vp(2);',
  25388. ' b:=vp=nil;',
  25389. ' b:=nil=vp;',
  25390. ' b:=vp=vq;',
  25391. ' b:=vp=@doit;',
  25392. ' b:=@doit=vp;',
  25393. ' b:=vp<>nil;',
  25394. ' b:=nil<>vp;',
  25395. ' b:=vp<>vq;',
  25396. ' b:=vp<>@doit;',
  25397. ' b:=@doit<>vp;',
  25398. ' b:=Assigned(vp);',
  25399. ' if Assigned(vp) then ;',
  25400. 'end;',
  25401. 'begin',
  25402. ' DoSome(@DoIt,nil);']);
  25403. ConvertProgram;
  25404. CheckSource('TestProcType_Arg',
  25405. LinesToStr([ // statements
  25406. 'this.DoIt = function(vJ) {',
  25407. '};',
  25408. 'this.DoSome = function(vP, vQ) {',
  25409. ' var b = false;',
  25410. ' vP = null;',
  25411. ' vP = vP;',
  25412. ' vP = $mod.DoIt;',
  25413. ' vP(1);',
  25414. ' vP(1);',
  25415. ' vP(2);',
  25416. ' b = vP === null;',
  25417. ' b = null === vP;',
  25418. ' b = rtl.eqCallback(vP,vQ);',
  25419. ' b = rtl.eqCallback(vP, $mod.DoIt);',
  25420. ' b = rtl.eqCallback($mod.DoIt, vP);',
  25421. ' b = vP !== null;',
  25422. ' b = null !== vP;',
  25423. ' b = !rtl.eqCallback(vP, vQ);',
  25424. ' b = !rtl.eqCallback(vP, $mod.DoIt);',
  25425. ' b = !rtl.eqCallback($mod.DoIt, vP);',
  25426. ' b = vP != null;',
  25427. ' if (vP != null) ;',
  25428. '};',
  25429. '']),
  25430. LinesToStr([ // $mod.$main
  25431. '$mod.DoSome($mod.DoIt,null);',
  25432. '']));
  25433. end;
  25434. procedure TTestModule.TestProcType_FunctionFPC;
  25435. begin
  25436. StartProgram(false);
  25437. Add('type');
  25438. Add(' TFuncInt = function(vA: longint = 1): longint;');
  25439. Add('function DoIt(vI: longint): longint;');
  25440. Add('begin end;');
  25441. Add('var');
  25442. Add(' b: boolean;');
  25443. Add(' vP, vQ: tfuncint;');
  25444. Add('begin');
  25445. Add(' vp:=nil;');
  25446. Add(' vp:=vp;');
  25447. Add(' vp:=@doit;'); // ok in fpc and delphi
  25448. //Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  25449. Add(' vp;'); // ok in fpc and delphi
  25450. Add(' vp();');
  25451. Add(' vp(2);');
  25452. Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  25453. Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  25454. Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  25455. Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  25456. Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  25457. //Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
  25458. Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
  25459. Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  25460. Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  25461. Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  25462. Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  25463. Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  25464. //Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
  25465. Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
  25466. Add(' b:=Assigned(vp);');
  25467. //Add(' doit(vp);'); // illegal in fpc, ok in delphi
  25468. Add(' doit(vp());'); // ok in fpc and delphi
  25469. Add(' doit(vp(2));'); // ok in fpc and delphi
  25470. ConvertProgram;
  25471. CheckSource('TestProcType_FunctionFPC',
  25472. LinesToStr([ // statements
  25473. 'this.DoIt = function(vI) {',
  25474. ' var Result = 0;',
  25475. ' return Result;',
  25476. '};',
  25477. 'this.b = false;',
  25478. 'this.vP = null;',
  25479. 'this.vQ = null;'
  25480. ]),
  25481. LinesToStr([ // $mod.$main
  25482. '$mod.vP = null;',
  25483. '$mod.vP = $mod.vP;',
  25484. '$mod.vP = $mod.DoIt;',
  25485. '$mod.vP(1);',
  25486. '$mod.vP(1);',
  25487. '$mod.vP(2);',
  25488. '$mod.b = $mod.vP === null;',
  25489. '$mod.b = null === $mod.vP;',
  25490. '$mod.b = rtl.eqCallback($mod.vP,$mod.vQ);',
  25491. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  25492. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  25493. '$mod.b = 4 === $mod.vP(1);',
  25494. '$mod.b = $mod.vP !== null;',
  25495. '$mod.b = null !== $mod.vP;',
  25496. '$mod.b = !rtl.eqCallback($mod.vP,$mod.vQ);',
  25497. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  25498. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  25499. '$mod.b = 6 !== $mod.vP(1);',
  25500. '$mod.b = $mod.vP != null;',
  25501. '$mod.DoIt($mod.vP(1));',
  25502. '$mod.DoIt($mod.vP(2));',
  25503. '']));
  25504. end;
  25505. procedure TTestModule.TestProcType_FunctionDelphi;
  25506. begin
  25507. StartProgram(false);
  25508. Add('{$mode Delphi}');
  25509. Add('type');
  25510. Add(' TFuncInt = function(vA: longint = 1): longint;');
  25511. Add('function DoIt(vI: longint): longint;');
  25512. Add('begin end;');
  25513. Add('var');
  25514. Add(' b: boolean;');
  25515. Add(' vP, vQ: tfuncint;');
  25516. Add('begin');
  25517. Add(' vp:=nil;');
  25518. Add(' vp:=vp;');
  25519. Add(' vp:=@doit;'); // ok in fpc and delphi
  25520. Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  25521. Add(' vp;'); // ok in fpc and delphi
  25522. Add(' vp();');
  25523. Add(' vp(2);');
  25524. //Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  25525. //Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  25526. Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  25527. //Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  25528. //Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  25529. Add(' b:=vp=3;'); // illegal in fpc, ok in delphi
  25530. Add(' b:=4=vp;'); // illegal in fpc, ok in delphi
  25531. //Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  25532. //Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  25533. Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  25534. //Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  25535. //Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  25536. Add(' b:=vp<>5;'); // illegal in fpc, ok in delphi
  25537. Add(' b:=6<>vp;'); // illegal in fpc, ok in delphi
  25538. Add(' b:=Assigned(vp);');
  25539. Add(' doit(vp);'); // illegal in fpc, ok in delphi
  25540. Add(' doit(vp());'); // ok in fpc and delphi
  25541. Add(' doit(vp(2));'); // ok in fpc and delphi *)
  25542. ConvertProgram;
  25543. CheckSource('TestProcType_FunctionDelphi',
  25544. LinesToStr([ // statements
  25545. 'this.DoIt = function(vI) {',
  25546. ' var Result = 0;',
  25547. ' return Result;',
  25548. '};',
  25549. 'this.b = false;',
  25550. 'this.vP = null;',
  25551. 'this.vQ = null;'
  25552. ]),
  25553. LinesToStr([ // $mod.$main
  25554. '$mod.vP = null;',
  25555. '$mod.vP = $mod.vP;',
  25556. '$mod.vP = $mod.DoIt;',
  25557. '$mod.vP = $mod.DoIt;',
  25558. '$mod.vP(1);',
  25559. '$mod.vP(1);',
  25560. '$mod.vP(2);',
  25561. '$mod.b = $mod.vP(1) === $mod.vQ(1);',
  25562. '$mod.b = $mod.vP(1) === 3;',
  25563. '$mod.b = 4 === $mod.vP(1);',
  25564. '$mod.b = $mod.vP(1) !== $mod.vQ(1);',
  25565. '$mod.b = $mod.vP(1) !== 5;',
  25566. '$mod.b = 6 !== $mod.vP(1);',
  25567. '$mod.b = $mod.vP != null;',
  25568. '$mod.DoIt($mod.vP(1));',
  25569. '$mod.DoIt($mod.vP(1));',
  25570. '$mod.DoIt($mod.vP(2));',
  25571. '']));
  25572. end;
  25573. procedure TTestModule.TestProcType_ProcedureDelphi;
  25574. begin
  25575. StartProgram(false);
  25576. Add('{$mode Delphi}');
  25577. Add('type');
  25578. Add(' TProc = procedure;');
  25579. Add('procedure DoIt;');
  25580. Add('begin end;');
  25581. Add('var');
  25582. Add(' b: boolean;');
  25583. Add(' vP, vQ: tproc;');
  25584. Add('begin');
  25585. Add(' vp:=nil;');
  25586. Add(' vp:=vp;');
  25587. Add(' vp:=vq;');
  25588. 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
  25589. Add(' vp:=doit;'); // illegal in fpc, ok in delphi
  25590. //Add(' vp:=@doit;'); // illegal in fpc, ok in delphi (because Delphi treats @F as Pointer), not supported by resolver
  25591. Add(' vp;'); // ok in fpc and delphi
  25592. Add(' vp();');
  25593. // equal
  25594. //Add(' b:=vp=nil;'); // ok in fpc, illegal in delphi
  25595. Add(' b:=@@vp=nil;'); // ok in fpc delphi mode, ok in delphi
  25596. //Add(' b:=nil=vp;'); // ok in fpc, illegal in delphi
  25597. Add(' b:=nil=@@vp;'); // ok in fpc delphi mode, ok in delphi
  25598. Add(' b:=@@vp=@@vq;'); // ok in fpc delphi mode, ok in Delphi
  25599. //Add(' b:=vp=vq;'); // in fpc compare proctypes, in delphi compare results
  25600. //Add(' b:=vp=@doit;'); // ok in fpc, illegal in delphi
  25601. Add(' b:=@@vp=@doit;'); // ok in fpc delphi mode, ok in delphi
  25602. //Add(' b:=@doit=vp;'); // ok in fpc, illegal in delphi
  25603. Add(' b:=@doit=@@vp;'); // ok in fpc delphi mode, ok in delphi
  25604. // unequal
  25605. //Add(' b:=vp<>nil;'); // ok in fpc, illegal in delphi
  25606. Add(' b:=@@vp<>nil;'); // ok in fpc mode delphi, ok in delphi
  25607. //Add(' b:=nil<>vp;'); // ok in fpc, illegal in delphi
  25608. Add(' b:=nil<>@@vp;'); // ok in fpc mode delphi, ok in delphi
  25609. //Add(' b:=vp<>vq;'); // in fpc compare proctypes, in delphi compare results
  25610. Add(' b:=@@vp<>@@vq;'); // ok in fpc mode delphi, ok in delphi
  25611. //Add(' b:=vp<>@doit;'); // ok in fpc, illegal in delphi
  25612. Add(' b:=@@vp<>@doit;'); // ok in fpc mode delphi, illegal in delphi
  25613. //Add(' b:=@doit<>vp;'); // ok in fpc, illegal in delphi
  25614. Add(' b:=@doit<>@@vp;'); // ok in fpc mode delphi, illegal in delphi
  25615. Add(' b:=Assigned(vp);');
  25616. ConvertProgram;
  25617. CheckSource('TestProcType_ProcedureDelphi',
  25618. LinesToStr([ // statements
  25619. 'this.DoIt = function() {',
  25620. '};',
  25621. 'this.b = false;',
  25622. 'this.vP = null;',
  25623. 'this.vQ = null;'
  25624. ]),
  25625. LinesToStr([ // $mod.$main
  25626. '$mod.vP = null;',
  25627. '$mod.vP = $mod.vP;',
  25628. '$mod.vP = $mod.vQ;',
  25629. '$mod.vP = $mod.DoIt;',
  25630. '$mod.vP = $mod.DoIt;',
  25631. '$mod.vP();',
  25632. '$mod.vP();',
  25633. '$mod.b = $mod.vP === null;',
  25634. '$mod.b = null === $mod.vP;',
  25635. '$mod.b = rtl.eqCallback($mod.vP, $mod.vQ);',
  25636. '$mod.b = rtl.eqCallback($mod.vP, $mod.DoIt);',
  25637. '$mod.b = rtl.eqCallback($mod.DoIt, $mod.vP);',
  25638. '$mod.b = $mod.vP !== null;',
  25639. '$mod.b = null !== $mod.vP;',
  25640. '$mod.b = !rtl.eqCallback($mod.vP, $mod.vQ);',
  25641. '$mod.b = !rtl.eqCallback($mod.vP, $mod.DoIt);',
  25642. '$mod.b = !rtl.eqCallback($mod.DoIt, $mod.vP);',
  25643. '$mod.b = $mod.vP != null;',
  25644. '']));
  25645. end;
  25646. procedure TTestModule.TestProcType_AsParam;
  25647. begin
  25648. StartProgram(false);
  25649. Add('type');
  25650. Add(' TFuncInt = function(vA: longint = 1): longint;');
  25651. Add('procedure DoIt(vG: tfuncint; const vH: tfuncint; var vI: tfuncint);');
  25652. Add('var vJ: tfuncint;');
  25653. Add('begin');
  25654. Add(' vg:=vg;');
  25655. Add(' vj:=vh;');
  25656. Add(' vi:=vi;');
  25657. Add(' doit(vg,vg,vg);');
  25658. Add(' doit(vh,vh,vj);');
  25659. Add(' doit(vi,vi,vi);');
  25660. Add(' doit(vj,vj,vj);');
  25661. Add('end;');
  25662. Add('var i: tfuncint;');
  25663. Add('begin');
  25664. Add(' doit(i,i,i);');
  25665. ConvertProgram;
  25666. CheckSource('TestProcType_AsParam',
  25667. LinesToStr([ // statements
  25668. 'this.DoIt = function (vG,vH,vI) {',
  25669. ' var vJ = null;',
  25670. ' vG = vG;',
  25671. ' vJ = vH;',
  25672. ' vI.set(vI.get());',
  25673. ' $mod.DoIt(vG, vG, {',
  25674. ' get: function () {',
  25675. ' return vG;',
  25676. ' },',
  25677. ' set: function (v) {',
  25678. ' vG = v;',
  25679. ' }',
  25680. ' });',
  25681. ' $mod.DoIt(vH, vH, {',
  25682. ' get: function () {',
  25683. ' return vJ;',
  25684. ' },',
  25685. ' set: function (v) {',
  25686. ' vJ = v;',
  25687. ' }',
  25688. ' });',
  25689. ' $mod.DoIt(vI.get(), vI.get(), vI);',
  25690. ' $mod.DoIt(vJ, vJ, {',
  25691. ' get: function () {',
  25692. ' return vJ;',
  25693. ' },',
  25694. ' set: function (v) {',
  25695. ' vJ = v;',
  25696. ' }',
  25697. ' });',
  25698. '};',
  25699. 'this.i = null;'
  25700. ]),
  25701. LinesToStr([
  25702. '$mod.DoIt($mod.i,$mod.i,{',
  25703. ' p: $mod,',
  25704. ' get: function () {',
  25705. ' return this.p.i;',
  25706. ' },',
  25707. ' set: function (v) {',
  25708. ' this.p.i = v;',
  25709. ' }',
  25710. '});'
  25711. ]));
  25712. end;
  25713. procedure TTestModule.TestProcType_MethodFPC;
  25714. begin
  25715. StartProgram(false);
  25716. Add('type');
  25717. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  25718. Add(' TObject = class');
  25719. Add(' function DoIt(vA: longint = 1): longint;');
  25720. Add(' end;');
  25721. Add('function TObject.DoIt(vA: longint = 1): longint;');
  25722. Add('begin');
  25723. Add('end;');
  25724. Add('var');
  25725. Add(' Obj: TObject;');
  25726. Add(' vP: tfuncint;');
  25727. Add(' b: boolean;');
  25728. Add('begin');
  25729. Add(' vp:[email protected];'); // ok in fpc and delphi
  25730. //Add(' vp:=obj.doit;'); // illegal in fpc, ok in delphi
  25731. Add(' vp;'); // ok in fpc and delphi
  25732. Add(' vp();');
  25733. Add(' vp(2);');
  25734. Add(' b:[email protected];'); // ok in fpc, illegal in delphi
  25735. Add(' b:[email protected]=vp;'); // ok in fpc, illegal in delphi
  25736. Add(' b:=vp<>@obj.doit;'); // ok in fpc, illegal in delphi
  25737. Add(' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
  25738. ConvertProgram;
  25739. CheckSource('TestProcType_MethodFPC',
  25740. LinesToStr([ // statements
  25741. 'rtl.createClass(this, "TObject", null, function () {',
  25742. ' this.$init = function () {',
  25743. ' };',
  25744. ' this.$final = function () {',
  25745. ' };',
  25746. ' this.DoIt = function (vA) {',
  25747. ' var Result = 0;',
  25748. ' return Result;',
  25749. ' };',
  25750. '});',
  25751. 'this.Obj = null;',
  25752. 'this.vP = null;',
  25753. 'this.b = false;'
  25754. ]),
  25755. LinesToStr([
  25756. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  25757. '$mod.vP(1);',
  25758. '$mod.vP(1);',
  25759. '$mod.vP(2);',
  25760. '$mod.b = rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
  25761. '$mod.b = rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
  25762. '$mod.b = !rtl.eqCallback($mod.vP, rtl.createCallback($mod.Obj, "DoIt"));',
  25763. '$mod.b = !rtl.eqCallback(rtl.createCallback($mod.Obj, "DoIt"), $mod.vP);',
  25764. '']));
  25765. end;
  25766. procedure TTestModule.TestProcType_MethodDelphi;
  25767. begin
  25768. StartProgram(false);
  25769. Add([
  25770. '{$mode delphi}',
  25771. 'type',
  25772. ' TFuncInt = function(vA: longint = 1): longint of object;',
  25773. ' TObject = class',
  25774. ' function DoIt(vA: longint = 1): longint;',
  25775. ' end;',
  25776. 'function TObject.DoIt(vA: longint = 1): longint;',
  25777. 'begin',
  25778. 'end;',
  25779. 'var',
  25780. ' Obj: TObject;',
  25781. ' vP: tfuncint;',
  25782. ' b: boolean;',
  25783. 'begin',
  25784. ' vp:[email protected];', // ok in fpc and delphi
  25785. ' vp:=obj.doit;', // illegal in fpc, ok in delphi
  25786. ' vp;', // ok in fpc and delphi
  25787. ' vp();',
  25788. ' vp(2);',
  25789. //' b:[email protected];', // ok in fpc, illegal in delphi
  25790. //' b:[email protected]=vp;', // ok in fpc, illegal in delphi
  25791. //' b:=vp<>@obj.doit;', // ok in fpc, illegal in delphi
  25792. //' b:[email protected]<>vp;'); // ok in fpc, illegal in delphi
  25793. '']);
  25794. ConvertProgram;
  25795. CheckSource('TestProcType_MethodDelphi',
  25796. LinesToStr([ // statements
  25797. 'rtl.createClass(this, "TObject", null, function () {',
  25798. ' this.$init = function () {',
  25799. ' };',
  25800. ' this.$final = function () {',
  25801. ' };',
  25802. ' this.DoIt = function (vA) {',
  25803. ' var Result = 0;',
  25804. ' return Result;',
  25805. ' };',
  25806. '});',
  25807. 'this.Obj = null;',
  25808. 'this.vP = null;',
  25809. 'this.b = false;'
  25810. ]),
  25811. LinesToStr([
  25812. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  25813. '$mod.vP = rtl.createCallback($mod.Obj, "DoIt");',
  25814. '$mod.vP(1);',
  25815. '$mod.vP(1);',
  25816. '$mod.vP(2);',
  25817. '']));
  25818. end;
  25819. procedure TTestModule.TestProcType_PropertyFPC;
  25820. begin
  25821. StartProgram(false);
  25822. Add('type');
  25823. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  25824. Add(' TObject = class');
  25825. Add(' FOnFoo: TFuncInt;');
  25826. Add(' function DoIt(vA: longint = 1): longint;');
  25827. Add(' function GetFoo: TFuncInt;');
  25828. Add(' procedure SetFoo(const Value: TFuncInt);');
  25829. Add(' function GetEvents(Index: longint): TFuncInt;');
  25830. Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
  25831. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  25832. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  25833. Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
  25834. Add(' end;');
  25835. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  25836. Add('function tobject.getfoo: tfuncint; begin end;');
  25837. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  25838. Add('function tobject.getevents(index: longint): tfuncint; begin end;');
  25839. Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
  25840. Add('var');
  25841. Add(' Obj: TObject;');
  25842. Add(' vP: tfuncint;');
  25843. Add(' b: boolean;');
  25844. Add('begin');
  25845. Add(' obj.onfoo:=nil;');
  25846. Add(' obj.onbar:=nil;');
  25847. Add(' obj.events[1]:=nil;');
  25848. Add(' obj.onfoo:=obj.onfoo;');
  25849. Add(' obj.onbar:=obj.onbar;');
  25850. Add(' obj.events[2]:=obj.events[3];');
  25851. Add(' obj.onfoo:[email protected];');
  25852. Add(' obj.onbar:[email protected];');
  25853. Add(' obj.events[4]:[email protected];');
  25854. //Add(' obj.onfoo:=obj.doit;'); // delphi
  25855. //Add(' obj.onbar:=obj.doit;'); // delphi
  25856. //Add(' obj.events[4]:=obj.doit;'); // delphi
  25857. Add(' obj.onfoo;');
  25858. Add(' obj.onbar;');
  25859. //Add(' obj.events[5];'); ToDo in pasresolver
  25860. Add(' obj.onfoo();');
  25861. Add(' obj.onbar();');
  25862. Add(' obj.events[6]();');
  25863. Add(' b:=obj.onfoo=nil;');
  25864. Add(' b:=obj.onbar=nil;');
  25865. Add(' b:=obj.events[7]=nil;');
  25866. Add(' b:=obj.onfoo<>nil;');
  25867. Add(' b:=obj.onbar<>nil;');
  25868. Add(' b:=obj.events[8]<>nil;');
  25869. Add(' b:=obj.onfoo=vp;');
  25870. Add(' b:=obj.onbar=vp;');
  25871. Add(' b:=obj.events[9]=vp;');
  25872. Add(' b:=obj.onfoo=obj.onfoo;');
  25873. Add(' b:=obj.onbar=obj.onfoo;');
  25874. Add(' b:=obj.events[10]=obj.onfoo;');
  25875. Add(' b:=obj.onfoo<>obj.onfoo;');
  25876. Add(' b:=obj.onbar<>obj.onfoo;');
  25877. Add(' b:=obj.events[11]<>obj.onfoo;');
  25878. Add(' b:[email protected];');
  25879. Add(' b:[email protected];');
  25880. Add(' b:=obj.events[12][email protected];');
  25881. Add(' b:=obj.onfoo<>@obj.doit;');
  25882. Add(' b:=obj.onbar<>@obj.doit;');
  25883. Add(' b:=obj.events[12]<>@obj.doit;');
  25884. Add(' b:=Assigned(obj.onfoo);');
  25885. Add(' b:=Assigned(obj.onbar);');
  25886. Add(' b:=Assigned(obj.events[13]);');
  25887. ConvertProgram;
  25888. CheckSource('TestProcType_PropertyFPC',
  25889. LinesToStr([ // statements
  25890. 'rtl.createClass(this, "TObject", null, function () {',
  25891. ' this.$init = function () {',
  25892. ' this.FOnFoo = null;',
  25893. ' };',
  25894. ' this.$final = function () {',
  25895. ' this.FOnFoo = undefined;',
  25896. ' };',
  25897. ' this.DoIt = function (vA) {',
  25898. ' var Result = 0;',
  25899. ' return Result;',
  25900. ' };',
  25901. 'this.GetFoo = function () {',
  25902. ' var Result = null;',
  25903. ' return Result;',
  25904. '};',
  25905. 'this.SetFoo = function (Value) {',
  25906. '};',
  25907. 'this.GetEvents = function (Index) {',
  25908. ' var Result = null;',
  25909. ' return Result;',
  25910. '};',
  25911. 'this.SetEvents = function (Index, Value) {',
  25912. '};',
  25913. '});',
  25914. 'this.Obj = null;',
  25915. 'this.vP = null;',
  25916. 'this.b = false;'
  25917. ]),
  25918. LinesToStr([
  25919. '$mod.Obj.FOnFoo = null;',
  25920. '$mod.Obj.SetFoo(null);',
  25921. '$mod.Obj.SetEvents(1, null);',
  25922. '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
  25923. '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
  25924. '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
  25925. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  25926. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  25927. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  25928. '$mod.Obj.FOnFoo(1);',
  25929. '$mod.Obj.GetFoo();',
  25930. '$mod.Obj.FOnFoo(1);',
  25931. '$mod.Obj.GetFoo()(1);',
  25932. '$mod.Obj.GetEvents(6)(1);',
  25933. '$mod.b = $mod.Obj.FOnFoo === null;',
  25934. '$mod.b = $mod.Obj.GetFoo() === null;',
  25935. '$mod.b = $mod.Obj.GetEvents(7) === null;',
  25936. '$mod.b = $mod.Obj.FOnFoo !== null;',
  25937. '$mod.b = $mod.Obj.GetFoo() !== null;',
  25938. '$mod.b = $mod.Obj.GetEvents(8) !== null;',
  25939. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.vP);',
  25940. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.vP);',
  25941. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(9), $mod.vP);',
  25942. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
  25943. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
  25944. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(10), $mod.Obj.FOnFoo);',
  25945. '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, $mod.Obj.FOnFoo);',
  25946. '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), $mod.Obj.FOnFoo);',
  25947. '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(11), $mod.Obj.FOnFoo);',
  25948. '$mod.b = rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
  25949. '$mod.b = rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
  25950. '$mod.b = rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
  25951. '$mod.b = !rtl.eqCallback($mod.Obj.FOnFoo, rtl.createCallback($mod.Obj, "DoIt"));',
  25952. '$mod.b = !rtl.eqCallback($mod.Obj.GetFoo(), rtl.createCallback($mod.Obj, "DoIt"));',
  25953. '$mod.b = !rtl.eqCallback($mod.Obj.GetEvents(12), rtl.createCallback($mod.Obj, "DoIt"));',
  25954. '$mod.b = $mod.Obj.FOnFoo != null;',
  25955. '$mod.b = $mod.Obj.GetFoo() != null;',
  25956. '$mod.b = $mod.Obj.GetEvents(13) != null;',
  25957. '']));
  25958. end;
  25959. procedure TTestModule.TestProcType_PropertyDelphi;
  25960. begin
  25961. StartProgram(false);
  25962. Add('{$mode delphi}');
  25963. Add('type');
  25964. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  25965. Add(' TObject = class');
  25966. Add(' FOnFoo: TFuncInt;');
  25967. Add(' function DoIt(vA: longint = 1): longint;');
  25968. Add(' function GetFoo: TFuncInt;');
  25969. Add(' procedure SetFoo(const Value: TFuncInt);');
  25970. Add(' function GetEvents(Index: longint): TFuncInt;');
  25971. Add(' procedure SetEvents(Index: longint; const Value: TFuncInt);');
  25972. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  25973. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  25974. Add(' property Events[Index: longint]: TFuncInt read GetEvents write SetEvents; default;');
  25975. Add(' end;');
  25976. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  25977. Add('function tobject.getfoo: tfuncint; begin end;');
  25978. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  25979. Add('function tobject.getevents(index: longint): tfuncint; begin end;');
  25980. Add('procedure tobject.setevents(index: longint; const value: tfuncint); begin end;');
  25981. Add('var');
  25982. Add(' Obj: TObject;');
  25983. Add(' vP: tfuncint;');
  25984. Add(' b: boolean;');
  25985. Add('begin');
  25986. Add(' obj.onfoo:=nil;');
  25987. Add(' obj.onbar:=nil;');
  25988. Add(' obj.events[1]:=nil;');
  25989. Add(' obj.onfoo:=obj.onfoo;');
  25990. Add(' obj.onbar:=obj.onbar;');
  25991. Add(' obj.events[2]:=obj.events[3];');
  25992. Add(' obj.onfoo:[email protected];');
  25993. Add(' obj.onbar:[email protected];');
  25994. Add(' obj.events[4]:[email protected];');
  25995. Add(' obj.onfoo:=obj.doit;'); // delphi
  25996. Add(' obj.onbar:=obj.doit;'); // delphi
  25997. Add(' obj.events[4]:=obj.doit;'); // delphi
  25998. Add(' obj.onfoo;');
  25999. Add(' obj.onbar;');
  26000. //Add(' obj.events[5];'); ToDo in pasresolver
  26001. Add(' obj.onfoo();');
  26002. Add(' obj.onbar();');
  26003. Add(' obj.events[6]();');
  26004. //Add(' b:=obj.onfoo=nil;'); // fpc
  26005. //Add(' b:=obj.onbar=nil;'); // fpc
  26006. //Add(' b:=obj.events[7]=nil;'); // fpc
  26007. //Add(' b:=obj.onfoo<>nil;'); // fpc
  26008. //Add(' b:=obj.onbar<>nil;'); // fpc
  26009. //Add(' b:=obj.events[8]<>nil;'); // fpc
  26010. Add(' b:=obj.onfoo=vp;');
  26011. Add(' b:=obj.onbar=vp;');
  26012. //Add(' b:=obj.events[9]=vp;'); ToDo in pasresolver
  26013. Add(' b:=obj.onfoo=obj.onfoo;');
  26014. Add(' b:=obj.onbar=obj.onfoo;');
  26015. //Add(' b:=obj.events[10]=obj.onfoo;'); // ToDo in pasresolver
  26016. Add(' b:=obj.onfoo<>obj.onfoo;');
  26017. Add(' b:=obj.onbar<>obj.onfoo;');
  26018. //Add(' b:=obj.events[11]<>obj.onfoo;'); // ToDo in pasresolver
  26019. //Add(' b:[email protected];'); // fpc
  26020. //Add(' b:[email protected];'); // fpc
  26021. //Add(' b:=obj.events[12][email protected];'); // fpc
  26022. //Add(' b:=obj.onfoo<>@obj.doit;'); // fpc
  26023. //Add(' b:=obj.onbar<>@obj.doit;'); // fpc
  26024. //Add(' b:=obj.events[12]<>@obj.doit;'); // fpc
  26025. Add(' b:=Assigned(obj.onfoo);');
  26026. Add(' b:=Assigned(obj.onbar);');
  26027. Add(' b:=Assigned(obj.events[13]);');
  26028. ConvertProgram;
  26029. CheckSource('TestProcType_PropertyDelphi',
  26030. LinesToStr([ // statements
  26031. 'rtl.createClass(this, "TObject", null, function () {',
  26032. ' this.$init = function () {',
  26033. ' this.FOnFoo = null;',
  26034. ' };',
  26035. ' this.$final = function () {',
  26036. ' this.FOnFoo = undefined;',
  26037. ' };',
  26038. ' this.DoIt = function (vA) {',
  26039. ' var Result = 0;',
  26040. ' return Result;',
  26041. ' };',
  26042. 'this.GetFoo = function () {',
  26043. ' var Result = null;',
  26044. ' return Result;',
  26045. '};',
  26046. 'this.SetFoo = function (Value) {',
  26047. '};',
  26048. 'this.GetEvents = function (Index) {',
  26049. ' var Result = null;',
  26050. ' return Result;',
  26051. '};',
  26052. 'this.SetEvents = function (Index, Value) {',
  26053. '};',
  26054. '});',
  26055. 'this.Obj = null;',
  26056. 'this.vP = null;',
  26057. 'this.b = false;'
  26058. ]),
  26059. LinesToStr([
  26060. '$mod.Obj.FOnFoo = null;',
  26061. '$mod.Obj.SetFoo(null);',
  26062. '$mod.Obj.SetEvents(1, null);',
  26063. '$mod.Obj.FOnFoo = $mod.Obj.FOnFoo;',
  26064. '$mod.Obj.SetFoo($mod.Obj.GetFoo());',
  26065. '$mod.Obj.SetEvents(2, $mod.Obj.GetEvents(3));',
  26066. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  26067. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  26068. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  26069. '$mod.Obj.FOnFoo = rtl.createCallback($mod.Obj, "DoIt");',
  26070. '$mod.Obj.SetFoo(rtl.createCallback($mod.Obj, "DoIt"));',
  26071. '$mod.Obj.SetEvents(4, rtl.createCallback($mod.Obj, "DoIt"));',
  26072. '$mod.Obj.FOnFoo(1);',
  26073. '$mod.Obj.GetFoo();',
  26074. '$mod.Obj.FOnFoo(1);',
  26075. '$mod.Obj.GetFoo()(1);',
  26076. '$mod.Obj.GetEvents(6)(1);',
  26077. '$mod.b = $mod.Obj.FOnFoo(1) === $mod.vP(1);',
  26078. '$mod.b = $mod.Obj.GetFoo() === $mod.vP(1);',
  26079. '$mod.b = $mod.Obj.FOnFoo(1) === $mod.Obj.FOnFoo(1);',
  26080. '$mod.b = $mod.Obj.GetFoo() === $mod.Obj.FOnFoo(1);',
  26081. '$mod.b = $mod.Obj.FOnFoo(1) !== $mod.Obj.FOnFoo(1);',
  26082. '$mod.b = $mod.Obj.GetFoo() !== $mod.Obj.FOnFoo(1);',
  26083. '$mod.b = $mod.Obj.FOnFoo != null;',
  26084. '$mod.b = $mod.Obj.GetFoo() != null;',
  26085. '$mod.b = $mod.Obj.GetEvents(13) != null;',
  26086. '']));
  26087. end;
  26088. procedure TTestModule.TestProcType_WithClassInstDoPropertyFPC;
  26089. begin
  26090. StartProgram(false);
  26091. Add('type');
  26092. Add(' TFuncInt = function(vA: longint = 1): longint of object;');
  26093. Add(' TObject = class');
  26094. Add(' FOnFoo: TFuncInt;');
  26095. Add(' function DoIt(vA: longint = 1): longint;');
  26096. Add(' function GetFoo: TFuncInt;');
  26097. Add(' procedure SetFoo(const Value: TFuncInt);');
  26098. Add(' property OnFoo: TFuncInt read FOnFoo write FOnFoo;');
  26099. Add(' property OnBar: TFuncInt read GetFoo write SetFoo;');
  26100. Add(' end;');
  26101. Add('function tobject.doit(va: longint = 1): longint; begin end;');
  26102. Add('function tobject.getfoo: tfuncint; begin end;');
  26103. Add('procedure tobject.setfoo(const value: tfuncint); begin end;');
  26104. Add('var');
  26105. Add(' Obj: TObject;');
  26106. Add(' vP: tfuncint;');
  26107. Add(' b: boolean;');
  26108. Add('begin');
  26109. Add('with obj do begin');
  26110. Add(' fonfoo:=nil;');
  26111. Add(' onfoo:=nil;');
  26112. Add(' onbar:=nil;');
  26113. Add(' fonfoo:=fonfoo;');
  26114. Add(' onfoo:=onfoo;');
  26115. Add(' onbar:=onbar;');
  26116. Add(' fonfoo:=@doit;');
  26117. Add(' onfoo:=@doit;');
  26118. Add(' onbar:=@doit;');
  26119. //Add(' fonfoo:=doit;'); // delphi
  26120. //Add(' onfoo:=doit;'); // delphi
  26121. //Add(' onbar:=doit;'); // delphi
  26122. Add(' fonfoo;');
  26123. Add(' onfoo;');
  26124. Add(' onbar;');
  26125. Add(' fonfoo();');
  26126. Add(' onfoo();');
  26127. Add(' onbar();');
  26128. Add(' b:=fonfoo=nil;');
  26129. Add(' b:=onfoo=nil;');
  26130. Add(' b:=onbar=nil;');
  26131. Add(' b:=fonfoo<>nil;');
  26132. Add(' b:=onfoo<>nil;');
  26133. Add(' b:=onbar<>nil;');
  26134. Add(' b:=fonfoo=vp;');
  26135. Add(' b:=onfoo=vp;');
  26136. Add(' b:=onbar=vp;');
  26137. Add(' b:=fonfoo=fonfoo;');
  26138. Add(' b:=onfoo=onfoo;');
  26139. Add(' b:=onbar=onfoo;');
  26140. Add(' b:=fonfoo<>fonfoo;');
  26141. Add(' b:=onfoo<>onfoo;');
  26142. Add(' b:=onbar<>onfoo;');
  26143. Add(' b:=fonfoo=@doit;');
  26144. Add(' b:=onfoo=@doit;');
  26145. Add(' b:=onbar=@doit;');
  26146. Add(' b:=fonfoo<>@doit;');
  26147. Add(' b:=onfoo<>@doit;');
  26148. Add(' b:=onbar<>@doit;');
  26149. Add(' b:=Assigned(fonfoo);');
  26150. Add(' b:=Assigned(onfoo);');
  26151. Add(' b:=Assigned(onbar);');
  26152. Add('end;');
  26153. ConvertProgram;
  26154. CheckSource('TestProcType_WithClassInstDoPropertyFPC',
  26155. LinesToStr([ // statements
  26156. 'rtl.createClass(this, "TObject", null, function () {',
  26157. ' this.$init = function () {',
  26158. ' this.FOnFoo = null;',
  26159. ' };',
  26160. ' this.$final = function () {',
  26161. ' this.FOnFoo = undefined;',
  26162. ' };',
  26163. ' this.DoIt = function (vA) {',
  26164. ' var Result = 0;',
  26165. ' return Result;',
  26166. ' };',
  26167. ' this.GetFoo = function () {',
  26168. ' var Result = null;',
  26169. ' return Result;',
  26170. ' };',
  26171. ' this.SetFoo = function (Value) {',
  26172. ' };',
  26173. '});',
  26174. 'this.Obj = null;',
  26175. 'this.vP = null;',
  26176. 'this.b = false;'
  26177. ]),
  26178. LinesToStr([
  26179. 'var $with = $mod.Obj;',
  26180. '$with.FOnFoo = null;',
  26181. '$with.FOnFoo = null;',
  26182. '$with.SetFoo(null);',
  26183. '$with.FOnFoo = $with.FOnFoo;',
  26184. '$with.FOnFoo = $with.FOnFoo;',
  26185. '$with.SetFoo($with.GetFoo());',
  26186. '$with.FOnFoo = rtl.createCallback($with, "DoIt");',
  26187. '$with.FOnFoo = rtl.createCallback($with, "DoIt");',
  26188. '$with.SetFoo(rtl.createCallback($with, "DoIt"));',
  26189. '$with.FOnFoo(1);',
  26190. '$with.FOnFoo(1);',
  26191. '$with.GetFoo();',
  26192. '$with.FOnFoo(1);',
  26193. '$with.FOnFoo(1);',
  26194. '$with.GetFoo()(1);',
  26195. '$mod.b = $with.FOnFoo === null;',
  26196. '$mod.b = $with.FOnFoo === null;',
  26197. '$mod.b = $with.GetFoo() === null;',
  26198. '$mod.b = $with.FOnFoo !== null;',
  26199. '$mod.b = $with.FOnFoo !== null;',
  26200. '$mod.b = $with.GetFoo() !== null;',
  26201. '$mod.b = rtl.eqCallback($with.FOnFoo, $mod.vP);',
  26202. '$mod.b = rtl.eqCallback($with.FOnFoo, $mod.vP);',
  26203. '$mod.b = rtl.eqCallback($with.GetFoo(), $mod.vP);',
  26204. '$mod.b = rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  26205. '$mod.b = rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  26206. '$mod.b = rtl.eqCallback($with.GetFoo(), $with.FOnFoo);',
  26207. '$mod.b = !rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  26208. '$mod.b = !rtl.eqCallback($with.FOnFoo, $with.FOnFoo);',
  26209. '$mod.b = !rtl.eqCallback($with.GetFoo(), $with.FOnFoo);',
  26210. '$mod.b = rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  26211. '$mod.b = rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  26212. '$mod.b = rtl.eqCallback($with.GetFoo(), rtl.createCallback($with, "DoIt"));',
  26213. '$mod.b = !rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  26214. '$mod.b = !rtl.eqCallback($with.FOnFoo, rtl.createCallback($with, "DoIt"));',
  26215. '$mod.b = !rtl.eqCallback($with.GetFoo(), rtl.createCallback($with, "DoIt"));',
  26216. '$mod.b = $with.FOnFoo != null;',
  26217. '$mod.b = $with.FOnFoo != null;',
  26218. '$mod.b = $with.GetFoo() != null;',
  26219. '']));
  26220. end;
  26221. procedure TTestModule.TestProcType_Nested;
  26222. begin
  26223. StartProgram(false);
  26224. Add([
  26225. 'type',
  26226. ' TProcInt = procedure(vI: longint = 1);',
  26227. 'procedure DoIt(vJ: longint);',
  26228. 'var aProc: TProcInt;',
  26229. ' b: boolean;',
  26230. ' procedure Sub(vK: longint);',
  26231. ' var aSub: TProcInt;',
  26232. ' procedure SubSub(vK: longint);',
  26233. ' var aSubSub: TProcInt;',
  26234. ' begin;',
  26235. ' aProc:=@DoIt;',
  26236. ' aSub:=@DoIt;',
  26237. ' aSubSub:=@DoIt;',
  26238. ' aProc:=@Sub;',
  26239. ' aSub:=@Sub;',
  26240. ' aSubSub:=@Sub;',
  26241. ' aProc:=@SubSub;',
  26242. ' aSub:=@SubSub;',
  26243. ' aSubSub:=@SubSub;',
  26244. ' end;',
  26245. ' begin;',
  26246. ' end;',
  26247. 'begin;',
  26248. ' aProc:=@Sub;',
  26249. ' b:=aProc=@Sub;',
  26250. ' b:=@Sub=aProc;',
  26251. 'end;',
  26252. 'begin',
  26253. '']);
  26254. ConvertProgram;
  26255. CheckSource('TestProcType_Nested',
  26256. LinesToStr([ // statements
  26257. 'this.DoIt = function (vJ) {',
  26258. ' var aProc = null;',
  26259. ' var b = false;',
  26260. ' function Sub(vK) {',
  26261. ' var aSub = null;',
  26262. ' function SubSub(vK) {',
  26263. ' var aSubSub = null;',
  26264. ' aProc = $mod.DoIt;',
  26265. ' aSub = $mod.DoIt;',
  26266. ' aSubSub = $mod.DoIt;',
  26267. ' aProc = Sub;',
  26268. ' aSub = Sub;',
  26269. ' aSubSub = Sub;',
  26270. ' aProc = SubSub;',
  26271. ' aSub = SubSub;',
  26272. ' aSubSub = SubSub;',
  26273. ' };',
  26274. ' };',
  26275. ' aProc = Sub;',
  26276. ' b = rtl.eqCallback(aProc, Sub);',
  26277. ' b = rtl.eqCallback(Sub, aProc);',
  26278. '};',
  26279. '']),
  26280. LinesToStr([ // $mod.$main
  26281. '']));
  26282. end;
  26283. procedure TTestModule.TestProcType_NestedOfObject;
  26284. begin
  26285. StartProgram(false);
  26286. Add([
  26287. 'type',
  26288. ' TProcInt = procedure(vI: longint = 1) of object;',
  26289. ' TObject = class',
  26290. ' procedure DoIt(vJ: longint);',
  26291. ' end;',
  26292. 'procedure TObject.DoIt(vJ: longint);',
  26293. 'var aProc: TProcInt;',
  26294. ' b: boolean;',
  26295. ' procedure Sub(vK: longint);',
  26296. ' var aSub: TProcInt;',
  26297. ' procedure SubSub(vK: longint);',
  26298. ' var aSubSub: TProcInt;',
  26299. ' begin;',
  26300. ' aProc:=@DoIt;',
  26301. ' aSub:=@DoIt;',
  26302. ' aSubSub:=@DoIt;',
  26303. ' aProc:=@Sub;',
  26304. ' aSub:=@Sub;',
  26305. ' aSubSub:=@Sub;',
  26306. ' aProc:=@SubSub;',
  26307. ' aSub:=@SubSub;',
  26308. ' aSubSub:=@SubSub;',
  26309. ' end;',
  26310. ' begin;',
  26311. ' end;',
  26312. 'begin;',
  26313. ' aProc:=@Sub;',
  26314. ' b:=aProc=@Sub;',
  26315. ' b:=@Sub=aProc;',
  26316. 'end;',
  26317. 'begin',
  26318. '']);
  26319. ConvertProgram;
  26320. CheckSource('TestProcType_Nested',
  26321. LinesToStr([ // statements
  26322. 'rtl.createClass(this, "TObject", null, function () {',
  26323. ' this.$init = function () {',
  26324. ' };',
  26325. ' this.$final = function () {',
  26326. ' };',
  26327. ' this.DoIt = function (vJ) {',
  26328. ' var $Self = this;',
  26329. ' var aProc = null;',
  26330. ' var b = false;',
  26331. ' function Sub(vK) {',
  26332. ' var aSub = null;',
  26333. ' function SubSub(vK) {',
  26334. ' var aSubSub = null;',
  26335. ' aProc = rtl.createCallback($Self, "DoIt");',
  26336. ' aSub = rtl.createCallback($Self, "DoIt");',
  26337. ' aSubSub = rtl.createCallback($Self, "DoIt");',
  26338. ' aProc = Sub;',
  26339. ' aSub = Sub;',
  26340. ' aSubSub = Sub;',
  26341. ' aProc = SubSub;',
  26342. ' aSub = SubSub;',
  26343. ' aSubSub = SubSub;',
  26344. ' };',
  26345. ' };',
  26346. ' aProc = Sub;',
  26347. ' b = rtl.eqCallback(aProc, Sub);',
  26348. ' b = rtl.eqCallback(Sub, aProc);',
  26349. ' };',
  26350. '});',
  26351. '']),
  26352. LinesToStr([ // $mod.$main
  26353. '']));
  26354. end;
  26355. procedure TTestModule.TestProcType_ReferenceToProc;
  26356. begin
  26357. StartProgram(false);
  26358. Add([
  26359. 'type',
  26360. ' TProcRef = reference to procedure(i: longint = 0);',
  26361. ' TFuncRef = reference to function(i: longint = 0): longint;',
  26362. 'var',
  26363. ' p: TProcRef;',
  26364. ' f: TFuncRef;',
  26365. 'procedure DoIt(i: longint);',
  26366. 'begin',
  26367. 'end;',
  26368. 'function GetIt(i: longint): longint;',
  26369. 'begin',
  26370. ' p:=@DoIt;',
  26371. ' f:=@GetIt;',
  26372. ' f;',
  26373. ' f();',
  26374. ' f(1);',
  26375. 'end;',
  26376. 'begin',
  26377. ' p:=@DoIt;',
  26378. ' f:=@GetIt;',
  26379. ' f;',
  26380. ' f();',
  26381. ' f(1);',
  26382. ' p:=TProcRef(f);',
  26383. '']);
  26384. ConvertProgram;
  26385. CheckSource('TestProcType_ReferenceToProc',
  26386. LinesToStr([ // statements
  26387. 'this.p = null;',
  26388. 'this.f = null;',
  26389. 'this.DoIt = function (i) {',
  26390. '};',
  26391. 'this.GetIt = function (i) {',
  26392. ' var Result = 0;',
  26393. ' $mod.p = $mod.DoIt;',
  26394. ' $mod.f = $mod.GetIt;',
  26395. ' $mod.f(0);',
  26396. ' $mod.f(0);',
  26397. ' $mod.f(1);',
  26398. ' return Result;',
  26399. '};',
  26400. '']),
  26401. LinesToStr([ // $mod.$main
  26402. '$mod.p = $mod.DoIt;',
  26403. '$mod.f = $mod.GetIt;',
  26404. '$mod.f(0);',
  26405. '$mod.f(0);',
  26406. '$mod.f(1);',
  26407. '$mod.p = $mod.f;',
  26408. '']));
  26409. end;
  26410. procedure TTestModule.TestProcType_ReferenceToMethod;
  26411. begin
  26412. StartProgram(false);
  26413. Add([
  26414. 'type',
  26415. ' TFuncRef = reference to function(i: longint = 5): longint;',
  26416. ' TObject = class',
  26417. ' function Grow(s: longint): longint;',
  26418. ' end;',
  26419. 'var',
  26420. ' f: tfuncref;',
  26421. 'function tobject.grow(s: longint): longint;',
  26422. ' function GrowSub(i: longint): longint;',
  26423. ' begin',
  26424. ' f:=@grow;',
  26425. ' f:=@growsub;',
  26426. ' end;',
  26427. 'begin',
  26428. ' f:=@grow;',
  26429. ' f:=@growsub;',
  26430. 'end;',
  26431. 'begin',
  26432. '']);
  26433. ConvertProgram;
  26434. CheckSource('TestProcType_ReferenceToMethod',
  26435. LinesToStr([ // statements
  26436. 'rtl.createClass(this, "TObject", null, function () {',
  26437. ' this.$init = function () {',
  26438. ' };',
  26439. ' this.$final = function () {',
  26440. ' };',
  26441. ' this.Grow = function (s) {',
  26442. ' var $Self = this;',
  26443. ' var Result = 0;',
  26444. ' function GrowSub(i) {',
  26445. ' var Result = 0;',
  26446. ' $mod.f = rtl.createCallback($Self, "Grow");',
  26447. ' $mod.f = GrowSub;',
  26448. ' return Result;',
  26449. ' };',
  26450. ' $mod.f = rtl.createCallback($Self, "Grow");',
  26451. ' $mod.f = GrowSub;',
  26452. ' return Result;',
  26453. ' };',
  26454. '});',
  26455. 'this.f = null;',
  26456. '']),
  26457. LinesToStr([ // $mod.$main
  26458. '']));
  26459. end;
  26460. procedure TTestModule.TestProcType_Typecast;
  26461. begin
  26462. StartProgram(false);
  26463. Add([
  26464. 'type',
  26465. ' TNotifyEvent = procedure(Sender: Pointer) of object;',
  26466. ' TEvent = procedure of object;',
  26467. ' TGetter = function:longint of object;',
  26468. ' TProcA = procedure(i: longint);',
  26469. ' TFuncB = function(i, j: longint): longint;',
  26470. 'procedure DoIt(); varargs; begin end;',
  26471. 'var',
  26472. ' Notify: tnotifyevent;',
  26473. ' Event: tevent;',
  26474. ' Getter: tgetter;',
  26475. ' ProcA: tproca;',
  26476. ' FuncB: tfuncb;',
  26477. ' p: pointer;',
  26478. 'begin',
  26479. ' notify:=tnotifyevent(event);',
  26480. ' event:=tevent(event);',
  26481. ' event:=tevent(notify);',
  26482. ' event:=tevent(getter);',
  26483. ' event:=tevent(proca);',
  26484. ' proca:=tproca(funcb);',
  26485. ' funcb:=tfuncb(funcb);',
  26486. ' funcb:=tfuncb(proca);',
  26487. ' funcb:=tfuncb(getter);',
  26488. ' proca:=tproca(p);',
  26489. ' funcb:=tfuncb(p);',
  26490. ' getter:=tgetter(p);',
  26491. ' p:=pointer(notify);',
  26492. ' p:=notify;',
  26493. ' p:=pointer(proca);',
  26494. ' p:=proca;',
  26495. ' p:=pointer(funcb);',
  26496. ' p:=funcb;',
  26497. ' doit(Pointer(notify),pointer(event),pointer(proca));',
  26498. '']);
  26499. ConvertProgram;
  26500. CheckSource('TestProcType_Typecast',
  26501. LinesToStr([ // statements
  26502. 'this.DoIt = function () {',
  26503. '};',
  26504. 'this.Notify = null;',
  26505. 'this.Event = null;',
  26506. 'this.Getter = null;',
  26507. 'this.ProcA = null;',
  26508. 'this.FuncB = null;',
  26509. 'this.p = null;',
  26510. '']),
  26511. LinesToStr([ // $mod.$main
  26512. '$mod.Notify = $mod.Event;',
  26513. '$mod.Event = $mod.Event;',
  26514. '$mod.Event = $mod.Notify;',
  26515. '$mod.Event = $mod.Getter;',
  26516. '$mod.Event = $mod.ProcA;',
  26517. '$mod.ProcA = $mod.FuncB;',
  26518. '$mod.FuncB = $mod.FuncB;',
  26519. '$mod.FuncB = $mod.ProcA;',
  26520. '$mod.FuncB = $mod.Getter;',
  26521. '$mod.ProcA = $mod.p;',
  26522. '$mod.FuncB = $mod.p;',
  26523. '$mod.Getter = $mod.p;',
  26524. '$mod.p = $mod.Notify;',
  26525. '$mod.p = $mod.Notify;',
  26526. '$mod.p = $mod.ProcA;',
  26527. '$mod.p = $mod.ProcA;',
  26528. '$mod.p = $mod.FuncB;',
  26529. '$mod.p = $mod.FuncB;',
  26530. '$mod.DoIt($mod.Notify, $mod.Event, $mod.ProcA);',
  26531. '']));
  26532. end;
  26533. procedure TTestModule.TestProcType_PassProcToUntyped;
  26534. begin
  26535. StartProgram(false);
  26536. Add([
  26537. 'type',
  26538. ' TEvent = procedure of object;',
  26539. ' TFunc = function: longint;',
  26540. 'procedure DoIt(); varargs; begin end;',
  26541. 'procedure DoSome(const a; var b; p: pointer); begin end;',
  26542. 'var',
  26543. ' Event: tevent;',
  26544. ' Func: TFunc;',
  26545. 'begin',
  26546. ' doit(event,func);',
  26547. ' dosome(event,event,event);',
  26548. ' dosome(func,func,func);',
  26549. '']);
  26550. ConvertProgram;
  26551. CheckSource('TestProcType_PassProcToUntyped',
  26552. LinesToStr([ // statements
  26553. 'this.DoIt = function () {',
  26554. '};',
  26555. 'this.DoSome = function (a, b, p) {',
  26556. '};',
  26557. 'this.Event = null;',
  26558. 'this.Func = null;',
  26559. '']),
  26560. LinesToStr([ // $mod.$main
  26561. '$mod.DoIt($mod.Event, $mod.Func);',
  26562. '$mod.DoSome($mod.Event, {',
  26563. ' p: $mod,',
  26564. ' get: function () {',
  26565. ' return this.p.Event;',
  26566. ' },',
  26567. ' set: function (v) {',
  26568. ' this.p.Event = v;',
  26569. ' }',
  26570. '}, $mod.Event);',
  26571. '$mod.DoSome($mod.Func, {',
  26572. ' p: $mod,',
  26573. ' get: function () {',
  26574. ' return this.p.Func;',
  26575. ' },',
  26576. ' set: function (v) {',
  26577. ' this.p.Func = v;',
  26578. ' }',
  26579. '}, $mod.Func);',
  26580. '']));
  26581. end;
  26582. procedure TTestModule.TestProcType_PassProcToArray;
  26583. begin
  26584. StartProgram(false);
  26585. Add([
  26586. 'type',
  26587. ' TFunc = function: longint;',
  26588. ' TArrFunc = array of TFunc;',
  26589. 'procedure DoIt(Arr: TArrFunc); begin end;',
  26590. 'function GetIt: longint; begin end;',
  26591. 'var',
  26592. ' Func: tfunc;',
  26593. 'begin',
  26594. ' doit([]);',
  26595. ' doit([@GetIt]);',
  26596. ' doit([Func]);',
  26597. '']);
  26598. ConvertProgram;
  26599. CheckSource('TestProcType_PassProcToArray',
  26600. LinesToStr([ // statements
  26601. 'this.DoIt = function (Arr) {',
  26602. '};',
  26603. 'this.GetIt = function () {',
  26604. ' var Result = 0;',
  26605. ' return Result;',
  26606. '};',
  26607. 'this.Func = null;',
  26608. '']),
  26609. LinesToStr([ // $mod.$main
  26610. '$mod.DoIt([]);',
  26611. '$mod.DoIt([$mod.GetIt]);',
  26612. '$mod.DoIt([$mod.Func]);',
  26613. '']));
  26614. end;
  26615. procedure TTestModule.TestProcType_SafeCallObjFPC;
  26616. begin
  26617. StartProgram(false);
  26618. Add([
  26619. '{$modeswitch externalclass}',
  26620. 'type',
  26621. ' TProc = reference to procedure(i: longint); safecall;',
  26622. ' TEvent = procedure(i: longint) of object; safecall;',
  26623. ' TExtA = class external name ''ExtObj''',
  26624. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  26625. ' procedure DoSome(Id: longint = 1);',
  26626. ' procedure SetOnClick(const e: TEvent);',
  26627. ' property OnClick: TEvent write SetOnClick;',
  26628. ' class procedure Fly(Id: longint = 1); static;',
  26629. ' procedure SetOnShow(const p: TProc);',
  26630. ' property OnShow: TProc write SetOnShow;',
  26631. ' end;',
  26632. 'procedure Run(i: longint = 1);',
  26633. 'begin',
  26634. 'end;',
  26635. 'var',
  26636. ' Obj: texta;',
  26637. ' e: TEvent;',
  26638. ' p: TProc;',
  26639. 'begin',
  26640. ' e:=e;',
  26641. ' e:[email protected];',
  26642. ' e:[email protected];',
  26643. ' e:=TEvent(@obj.dosome);', // no safecall
  26644. ' obj.OnClick:[email protected];',
  26645. ' obj.OnClick:[email protected];',
  26646. ' obj.setonclick(@obj.doit);',
  26647. ' obj.setonclick(@obj.dosome);',
  26648. ' p:=@Run;',
  26649. ' p:[email protected];',
  26650. ' obj.OnShow:=@Run;',
  26651. ' obj.OnShow:[email protected];',
  26652. ' obj.setOnShow(@Run);',
  26653. ' obj.setOnShow(@TExtA.Fly);',
  26654. ' with obj do begin',
  26655. ' e:=@doit;',
  26656. ' e:=@dosome;',
  26657. ' OnClick:=@doit;',
  26658. ' OnClick:=@dosome;',
  26659. ' setonclick(@doit);',
  26660. ' setonclick(@dosome);',
  26661. ' OnShow:=@Run;',
  26662. ' setOnShow(@Run);',
  26663. ' end;']);
  26664. ConvertProgram;
  26665. CheckSource('TestProcType_SafeCallObjFPC',
  26666. LinesToStr([ // statements
  26667. 'this.Run = function (i) {',
  26668. '};',
  26669. 'this.Obj = null;',
  26670. 'this.e = null;',
  26671. 'this.p = null;',
  26672. '']),
  26673. LinesToStr([ // $mod.$main
  26674. '$mod.e = $mod.e;',
  26675. '$mod.e = rtl.createSafeCallback($mod.Obj, "$Execute");',
  26676. '$mod.e = rtl.createSafeCallback($mod.Obj, "DoSome");',
  26677. '$mod.e = rtl.createCallback($mod.Obj, "DoSome");',
  26678. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  26679. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  26680. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  26681. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  26682. '$mod.p = rtl.createSafeCallback($mod, "Run");',
  26683. '$mod.p = rtl.createSafeCallback(ExtObj, "Fly");',
  26684. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26685. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  26686. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26687. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  26688. 'var $with = $mod.Obj;',
  26689. '$mod.e = rtl.createSafeCallback($with, "$Execute");',
  26690. '$mod.e = rtl.createSafeCallback($with, "DoSome");',
  26691. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  26692. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  26693. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  26694. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  26695. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26696. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26697. '']));
  26698. end;
  26699. procedure TTestModule.TestProcType_SafeCallDelphi;
  26700. begin
  26701. StartProgram(false);
  26702. Add([
  26703. '{$mode delphi}',
  26704. '{$modeswitch externalclass}',
  26705. 'type',
  26706. ' TProc = reference to procedure(i: longint); safecall;',
  26707. ' TEvent = procedure(i: longint) of object; safecall;',
  26708. ' TExtA = class external name ''ExtObj''',
  26709. ' procedure DoIt(Id: longint = 1); external name ''$Execute'';',
  26710. ' procedure DoSome(Id: longint = 1);',
  26711. ' procedure SetOnClick(const e: TEvent);',
  26712. ' property OnClick: TEvent write SetOnClick;',
  26713. ' class procedure Fly(Id: longint = 1); static;',
  26714. ' procedure SetOnShow(const p: TProc);',
  26715. ' property OnShow: TProc write SetOnShow;',
  26716. ' end;',
  26717. 'procedure Run(i: longint = 1);',
  26718. 'begin',
  26719. 'end;',
  26720. 'var',
  26721. ' Obj: texta;',
  26722. ' e: TEvent;',
  26723. ' p: TProc;',
  26724. 'begin',
  26725. ' e:=e;',
  26726. ' e:=obj.doit;',
  26727. ' e:=obj.dosome;',
  26728. ' e:=TEvent(@obj.dosome);', // no safecall
  26729. ' obj.OnClick:=obj.doit;',
  26730. ' obj.OnClick:=obj.dosome;',
  26731. ' obj.setonclick(obj.doit);',
  26732. ' obj.setonclick(obj.dosome);',
  26733. ' p:=Run;',
  26734. ' p:=TExtA.Fly;',
  26735. ' obj.OnShow:=Run;',
  26736. ' obj.OnShow:=TExtA.Fly;',
  26737. ' obj.setOnShow(Run);',
  26738. ' obj.setOnShow(TExtA.Fly);',
  26739. ' with obj do begin',
  26740. ' e:=doit;',
  26741. ' e:=dosome;',
  26742. ' OnClick:=doit;',
  26743. ' OnClick:=dosome;',
  26744. ' setonclick(doit);',
  26745. ' setonclick(dosome);',
  26746. ' OnShow:=@Run;',
  26747. ' setOnShow(@Run);',
  26748. ' end;']);
  26749. ConvertProgram;
  26750. CheckSource('TestProcType_SafeCallDelphi',
  26751. LinesToStr([ // statements
  26752. 'this.Run = function (i) {',
  26753. '};',
  26754. 'this.Obj = null;',
  26755. 'this.e = null;',
  26756. 'this.p = null;',
  26757. '']),
  26758. LinesToStr([ // $mod.$main
  26759. '$mod.e = $mod.e;',
  26760. '$mod.e = rtl.createSafeCallback($mod.Obj, "$Execute");',
  26761. '$mod.e = rtl.createSafeCallback($mod.Obj, "DoSome");',
  26762. '$mod.e = rtl.createCallback($mod.Obj, "DoSome");',
  26763. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  26764. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  26765. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "$Execute"));',
  26766. '$mod.Obj.SetOnClick(rtl.createSafeCallback($mod.Obj, "DoSome"));',
  26767. '$mod.p = rtl.createSafeCallback($mod, "Run");',
  26768. '$mod.p = rtl.createSafeCallback(ExtObj, "Fly");',
  26769. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26770. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  26771. '$mod.Obj.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26772. '$mod.Obj.SetOnShow(rtl.createSafeCallback(ExtObj, "Fly"));',
  26773. 'var $with = $mod.Obj;',
  26774. '$mod.e = rtl.createSafeCallback($with, "$Execute");',
  26775. '$mod.e = rtl.createSafeCallback($with, "DoSome");',
  26776. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  26777. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  26778. '$with.SetOnClick(rtl.createSafeCallback($with, "$Execute"));',
  26779. '$with.SetOnClick(rtl.createSafeCallback($with, "DoSome"));',
  26780. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26781. '$with.SetOnShow(rtl.createSafeCallback($mod, "Run"));',
  26782. '']));
  26783. end;
  26784. procedure TTestModule.TestPointer;
  26785. begin
  26786. StartProgram(false);
  26787. Add(['type',
  26788. ' TObject = class end;',
  26789. ' TClass = class of TObject;',
  26790. ' TArrInt = array of longint;',
  26791. 'const',
  26792. ' n = nil;',
  26793. 'var',
  26794. ' v: jsvalue;',
  26795. ' Obj: tobject;',
  26796. ' C: tclass;',
  26797. ' a: tarrint;',
  26798. ' p: Pointer = nil;',
  26799. ' s: string;',
  26800. 'begin',
  26801. ' p:=p;',
  26802. ' p:=nil;',
  26803. ' if p=nil then;',
  26804. ' if nil=p then;',
  26805. ' if Assigned(p) then;',
  26806. ' p:=Pointer(v);',
  26807. ' p:=obj;',
  26808. ' p:=c;',
  26809. ' p:=a;',
  26810. ' p:=tobject;',
  26811. ' obj:=TObject(p);',
  26812. ' c:=TClass(p);',
  26813. ' a:=TArrInt(p);',
  26814. ' p:=n;',
  26815. ' p:=Pointer(a);',
  26816. ' p:=pointer(s);',
  26817. ' s:=string(p);',
  26818. '']);
  26819. ConvertProgram;
  26820. CheckSource('TestPointer',
  26821. LinesToStr([ // statements
  26822. 'rtl.createClass(this, "TObject", null, function () {',
  26823. ' this.$init = function () {',
  26824. ' };',
  26825. ' this.$final = function () {',
  26826. ' };',
  26827. '});',
  26828. 'this.n = null;',
  26829. 'this.v = undefined;',
  26830. 'this.Obj = null;',
  26831. 'this.C = null;',
  26832. 'this.a = [];',
  26833. 'this.p = null;',
  26834. 'this.s = "";',
  26835. '']),
  26836. LinesToStr([ // $mod.$main
  26837. '$mod.p = $mod.p;',
  26838. '$mod.p = null;',
  26839. 'if ($mod.p === null) ;',
  26840. 'if (null === $mod.p) ;',
  26841. 'if ($mod.p != null) ;',
  26842. '$mod.p = $mod.v;',
  26843. '$mod.p = $mod.Obj;',
  26844. '$mod.p = $mod.C;',
  26845. '$mod.p = $mod.a;',
  26846. '$mod.p = $mod.TObject;',
  26847. '$mod.Obj = $mod.p;',
  26848. '$mod.C = $mod.p;',
  26849. '$mod.a = $mod.p;',
  26850. '$mod.p = null;',
  26851. '$mod.p = $mod.a;',
  26852. '$mod.p = $mod.s;',
  26853. '$mod.s = $mod.p;',
  26854. '']));
  26855. end;
  26856. procedure TTestModule.TestPointer_Proc;
  26857. begin
  26858. StartProgram(false);
  26859. Add('type');
  26860. Add(' TObject = class');
  26861. Add(' procedure DoIt; virtual; abstract;');
  26862. Add(' end;');
  26863. Add('procedure DoSome; begin end;');
  26864. Add('var');
  26865. Add(' o: TObject;');
  26866. Add(' p: Pointer;');
  26867. Add('begin');
  26868. Add(' p:=@DoSome;');
  26869. Add(' p:[email protected];');
  26870. ConvertProgram;
  26871. CheckSource('TestPointer_Proc',
  26872. LinesToStr([ // statements
  26873. 'rtl.createClass(this, "TObject", null, function () {',
  26874. ' this.$init = function () {',
  26875. ' };',
  26876. ' this.$final = function () {',
  26877. ' };',
  26878. '});',
  26879. 'this.DoSome = function () {',
  26880. '};',
  26881. 'this.o = null;',
  26882. 'this.p = null;',
  26883. '']),
  26884. LinesToStr([ // $mod.$main
  26885. '$mod.p = $mod.DoSome;',
  26886. '$mod.p = rtl.createCallback($mod.o, "DoIt");',
  26887. '']));
  26888. end;
  26889. procedure TTestModule.TestPointer_AssignRecordFail;
  26890. begin
  26891. StartProgram(false);
  26892. Add('type');
  26893. Add(' TRec = record end;');
  26894. Add('var');
  26895. Add(' p: Pointer;');
  26896. Add(' r: TRec;');
  26897. Add('begin');
  26898. Add(' p:=r;');
  26899. SetExpectedPasResolverError('Incompatible types: got "TRec" expected "Pointer"',
  26900. nIncompatibleTypesGotExpected);
  26901. ConvertProgram;
  26902. end;
  26903. procedure TTestModule.TestPointer_AssignStaticArrayFail;
  26904. begin
  26905. StartProgram(false);
  26906. Add('type');
  26907. Add(' TArr = array[boolean] of longint;');
  26908. Add('var');
  26909. Add(' p: Pointer;');
  26910. Add(' a: TArr;');
  26911. Add('begin');
  26912. Add(' p:=a;');
  26913. SetExpectedPasResolverError('Incompatible types: got "TArr" expected "Pointer"',
  26914. nIncompatibleTypesGotExpected);
  26915. ConvertProgram;
  26916. end;
  26917. procedure TTestModule.TestPointer_TypeCastJSValueToPointer;
  26918. begin
  26919. StartProgram(false);
  26920. Add([
  26921. 'procedure DoIt(args: array of jsvalue); begin end;',
  26922. 'procedure DoAll; varargs; begin end;',
  26923. 'var',
  26924. ' v: jsvalue;',
  26925. 'begin',
  26926. ' DoIt([pointer(v)]);',
  26927. ' DoAll(pointer(v));',
  26928. '']);
  26929. ConvertProgram;
  26930. CheckSource('TestPointer_TypeCastJSValueToPointer',
  26931. LinesToStr([ // statements
  26932. 'this.DoIt = function (args) {',
  26933. '};',
  26934. 'this.DoAll = function () {',
  26935. '};',
  26936. 'this.v = undefined;',
  26937. '']),
  26938. LinesToStr([ // $mod.$main
  26939. '$mod.DoIt([$mod.v]);',
  26940. '$mod.DoAll($mod.v);',
  26941. '']));
  26942. end;
  26943. procedure TTestModule.TestPointer_NonRecordFail;
  26944. begin
  26945. StartProgram(false);
  26946. Add([
  26947. 'type',
  26948. ' p = ^longint;',
  26949. 'begin',
  26950. '']);
  26951. SetExpectedPasResolverError('Not supported: pointer of Longint',nNotSupportedX);
  26952. ConvertProgram;
  26953. end;
  26954. procedure TTestModule.TestPointer_AnonymousArgTypeFail;
  26955. begin
  26956. StartProgram(false);
  26957. Add([
  26958. 'procedure DoIt(p: ^longint); begin end;',
  26959. 'begin',
  26960. '']);
  26961. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  26962. ConvertProgram;
  26963. end;
  26964. procedure TTestModule.TestPointer_AnonymousVarTypeFail;
  26965. begin
  26966. StartProgram(false);
  26967. Add([
  26968. 'var p: ^longint;',
  26969. 'begin',
  26970. '']);
  26971. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  26972. ConvertProgram;
  26973. end;
  26974. procedure TTestModule.TestPointer_AnonymousResultTypeFail;
  26975. begin
  26976. StartProgram(false);
  26977. Add([
  26978. 'function DoIt: ^longint; begin end;',
  26979. 'begin',
  26980. '']);
  26981. SetExpectedPasResolverError('Not supported: pointer',nNotSupportedX);
  26982. ConvertProgram;
  26983. end;
  26984. procedure TTestModule.TestPointer_AddrOperatorFail;
  26985. begin
  26986. StartProgram(false);
  26987. Add([
  26988. 'var i: longint;',
  26989. 'begin',
  26990. ' if @i=nil then ;',
  26991. '']);
  26992. SetExpectedConverterError('illegal qualifier "@" in front of "i:Longint"',nIllegalQualifierInFrontOf);
  26993. ConvertProgram;
  26994. end;
  26995. procedure TTestModule.TestPointer_ArrayParamsFail;
  26996. begin
  26997. StartProgram(false);
  26998. Add([
  26999. 'var',
  27000. ' p: Pointer;',
  27001. 'begin',
  27002. ' p:=p[1];',
  27003. '']);
  27004. SetExpectedPasResolverError('illegal qualifier "[" after "Pointer"',nIllegalQualifierAfter);
  27005. ConvertProgram;
  27006. end;
  27007. procedure TTestModule.TestPointer_PointerAddFail;
  27008. begin
  27009. StartProgram(false);
  27010. Add([
  27011. 'var',
  27012. ' p: Pointer;',
  27013. 'begin',
  27014. ' p:=p+1;',
  27015. '']);
  27016. SetExpectedPasResolverError('Operator is not overloaded: "Pointer" + "Longint"',nOperatorIsNotOverloadedAOpB);
  27017. ConvertProgram;
  27018. end;
  27019. procedure TTestModule.TestPointer_IncPointerFail;
  27020. begin
  27021. StartProgram(false);
  27022. Add([
  27023. 'var',
  27024. ' p: Pointer;',
  27025. 'begin',
  27026. ' inc(p,1);',
  27027. '']);
  27028. SetExpectedPasResolverError('Incompatible type arg no. 1: Got "Pointer", expected "integer"',
  27029. nIncompatibleTypeArgNo);
  27030. ConvertProgram;
  27031. end;
  27032. procedure TTestModule.TestPointer_Record;
  27033. begin
  27034. StartProgram(false);
  27035. Add([
  27036. 'type',
  27037. ' TRec = record x: longint; end;',
  27038. ' PRec = ^TRec;',
  27039. 'var',
  27040. ' r: TRec;',
  27041. ' p: PRec;',
  27042. ' q: ^TRec;',
  27043. ' Ptr: pointer;',
  27044. 'begin',
  27045. ' new(p);',
  27046. ' p:=@r;',
  27047. ' r:=p^;',
  27048. ' r.x:=p^.x;',
  27049. ' p^.x:=r.x;',
  27050. ' if p^.x=3 then ;',
  27051. ' if 4=p^.x then ;',
  27052. ' dispose(p);',
  27053. ' new(q);',
  27054. ' dispose(q);',
  27055. ' Ptr:=p;',
  27056. ' p:=PRec(ptr);',
  27057. '']);
  27058. ConvertProgram;
  27059. CheckSource('TestPointer_Record',
  27060. LinesToStr([ // statements
  27061. 'rtl.recNewT(this, "TRec", function () {',
  27062. ' this.x = 0;',
  27063. ' this.$eq = function (b) {',
  27064. ' return this.x === b.x;',
  27065. ' };',
  27066. ' this.$assign = function (s) {',
  27067. ' this.x = s.x;',
  27068. ' return this;',
  27069. ' };',
  27070. '});',
  27071. 'this.r = this.TRec.$new();',
  27072. 'this.p = null;',
  27073. 'this.q = null;',
  27074. 'this.Ptr = null;',
  27075. '']),
  27076. LinesToStr([ // $mod.$main
  27077. '$mod.p = $mod.TRec.$new();',
  27078. '$mod.p = $mod.r;',
  27079. '$mod.r.$assign($mod.p);',
  27080. '$mod.r.x = $mod.p.x;',
  27081. '$mod.p.x = $mod.r.x;',
  27082. 'if ($mod.p.x === 3) ;',
  27083. 'if (4 === $mod.p.x) ;',
  27084. '$mod.p = null;',
  27085. '$mod.q = $mod.TRec.$new();',
  27086. '$mod.q = null;',
  27087. '$mod.Ptr = $mod.p;',
  27088. '$mod.p = $mod.Ptr;',
  27089. '']));
  27090. end;
  27091. procedure TTestModule.TestPointer_RecordArg;
  27092. begin
  27093. StartProgram(false);
  27094. Add([
  27095. '{$modeswitch autoderef}',
  27096. 'type',
  27097. ' TRec = record x: longint; end;',
  27098. ' PRec = ^TRec;',
  27099. 'function DoIt(const a: PRec; var b: PRec; out c: PRec): TRec;',
  27100. 'begin',
  27101. ' a.x:=a.x;',
  27102. ' a^.x:=a^.x;',
  27103. ' with a^ do',
  27104. ' x:=x;',
  27105. 'end;',
  27106. 'function GetIt(p: PRec): PRec;',
  27107. 'begin',
  27108. ' p.x:=p.x;',
  27109. ' p^.x:=p^.x;',
  27110. ' with p^ do',
  27111. ' x:=x;',
  27112. 'end;',
  27113. 'var',
  27114. ' r: TRec;',
  27115. ' p: PRec;',
  27116. 'begin',
  27117. ' p:=GetIt(p);',
  27118. ' p^:=GetIt(@r)^;',
  27119. ' DoIt(p,p,p);',
  27120. ' DoIt(@r,p,p);',
  27121. '']);
  27122. ConvertProgram;
  27123. CheckSource('TestPointer_RecordArg',
  27124. LinesToStr([ // statements
  27125. 'rtl.recNewT(this, "TRec", function () {',
  27126. ' this.x = 0;',
  27127. ' this.$eq = function (b) {',
  27128. ' return this.x === b.x;',
  27129. ' };',
  27130. ' this.$assign = function (s) {',
  27131. ' this.x = s.x;',
  27132. ' return this;',
  27133. ' };',
  27134. '});',
  27135. 'this.DoIt = function (a, b, c) {',
  27136. ' var Result = $mod.TRec.$new();',
  27137. ' a.x = a.x;',
  27138. ' a.x = a.x;',
  27139. ' a.x = a.x;',
  27140. ' return Result;',
  27141. '};',
  27142. 'this.GetIt = function (p) {',
  27143. ' var Result = null;',
  27144. ' p.x = p.x;',
  27145. ' p.x = p.x;',
  27146. ' p.x = p.x;',
  27147. ' return Result;',
  27148. '};',
  27149. 'this.r = this.TRec.$new();',
  27150. 'this.p = null;',
  27151. '']),
  27152. LinesToStr([ // $mod.$main
  27153. '$mod.p = $mod.GetIt($mod.p);',
  27154. '$mod.p.$assign($mod.GetIt($mod.r));',
  27155. '$mod.DoIt($mod.p, {',
  27156. ' p: $mod,',
  27157. ' get: function () {',
  27158. ' return this.p.p;',
  27159. ' },',
  27160. ' set: function (v) {',
  27161. ' this.p.p = v;',
  27162. ' }',
  27163. '}, {',
  27164. ' p: $mod,',
  27165. ' get: function () {',
  27166. ' return this.p.p;',
  27167. ' },',
  27168. ' set: function (v) {',
  27169. ' this.p.p = v;',
  27170. ' }',
  27171. '});',
  27172. '$mod.DoIt($mod.r, {',
  27173. ' p: $mod,',
  27174. ' get: function () {',
  27175. ' return this.p.p;',
  27176. ' },',
  27177. ' set: function (v) {',
  27178. ' this.p.p = v;',
  27179. ' }',
  27180. '}, {',
  27181. ' p: $mod,',
  27182. ' get: function () {',
  27183. ' return this.p.p;',
  27184. ' },',
  27185. ' set: function (v) {',
  27186. ' this.p.p = v;',
  27187. ' }',
  27188. '});',
  27189. '']));
  27190. end;
  27191. procedure TTestModule.TestJSValue_AssignToJSValue;
  27192. begin
  27193. StartProgram(false);
  27194. Add('var');
  27195. Add(' v: jsvalue;');
  27196. Add(' i: longint;');
  27197. Add(' s: string;');
  27198. Add(' b: boolean;');
  27199. Add(' d: double;');
  27200. Add(' p: pointer;');
  27201. Add('begin');
  27202. Add(' v:=v;');
  27203. Add(' v:=1;');
  27204. Add(' v:=i;');
  27205. Add(' v:='''';');
  27206. Add(' v:=''c'';');
  27207. Add(' v:=''foo'';');
  27208. Add(' v:=s;');
  27209. Add(' v:=false;');
  27210. Add(' v:=true;');
  27211. Add(' v:=b;');
  27212. Add(' v:=0.1;');
  27213. Add(' v:=d;');
  27214. Add(' v:=nil;');
  27215. Add(' v:=p;');
  27216. ConvertProgram;
  27217. CheckSource('TestJSValue_AssignToJSValue',
  27218. LinesToStr([ // statements
  27219. 'this.v = undefined;',
  27220. 'this.i = 0;',
  27221. 'this.s = "";',
  27222. 'this.b = false;',
  27223. 'this.d = 0.0;',
  27224. 'this.p = null;',
  27225. '']),
  27226. LinesToStr([ // $mod.$main
  27227. '$mod.v = $mod.v;',
  27228. '$mod.v = 1;',
  27229. '$mod.v = $mod.i;',
  27230. '$mod.v = "";',
  27231. '$mod.v = "c";',
  27232. '$mod.v = "foo";',
  27233. '$mod.v = $mod.s;',
  27234. '$mod.v = false;',
  27235. '$mod.v = true;',
  27236. '$mod.v = $mod.b;',
  27237. '$mod.v = 0.1;',
  27238. '$mod.v = $mod.d;',
  27239. '$mod.v = null;',
  27240. '$mod.v = $mod.p;',
  27241. '']));
  27242. end;
  27243. procedure TTestModule.TestJSValue_TypeCastToBaseType;
  27244. begin
  27245. StartProgram(false);
  27246. Add('type');
  27247. Add(' integer = longint;');
  27248. Add(' TYesNo = boolean;');
  27249. Add(' TFloat = double;');
  27250. Add(' TCaption = string;');
  27251. Add(' TChar = char;');
  27252. Add('var');
  27253. Add(' v: jsvalue;');
  27254. Add(' i: integer;');
  27255. Add(' s: TCaption;');
  27256. Add(' b: TYesNo;');
  27257. Add(' d: TFloat;');
  27258. Add(' c: char;');
  27259. Add('begin');
  27260. Add(' i:=longint(v);');
  27261. Add(' i:=integer(v);');
  27262. Add(' s:=string(v);');
  27263. Add(' s:=TCaption(v);');
  27264. Add(' b:=boolean(v);');
  27265. Add(' b:=TYesNo(v);');
  27266. Add(' d:=double(v);');
  27267. Add(' d:=TFloat(v);');
  27268. Add(' c:=char(v);');
  27269. Add(' c:=TChar(v);');
  27270. ConvertProgram;
  27271. CheckSource('TestJSValue_TypeCastToBaseType',
  27272. LinesToStr([ // statements
  27273. 'this.v = undefined;',
  27274. 'this.i = 0;',
  27275. 'this.s = "";',
  27276. 'this.b = false;',
  27277. 'this.d = 0.0;',
  27278. 'this.c = "";',
  27279. '']),
  27280. LinesToStr([ // $mod.$main
  27281. '$mod.i = rtl.trunc($mod.v);',
  27282. '$mod.i = rtl.trunc($mod.v);',
  27283. '$mod.s = "" + $mod.v;',
  27284. '$mod.s = "" + $mod.v;',
  27285. '$mod.b = !($mod.v == false);',
  27286. '$mod.b = !($mod.v == false);',
  27287. '$mod.d = rtl.getNumber($mod.v);',
  27288. '$mod.d = rtl.getNumber($mod.v);',
  27289. '$mod.c = rtl.getChar($mod.v);',
  27290. '$mod.c = rtl.getChar($mod.v);',
  27291. '']));
  27292. end;
  27293. procedure TTestModule.TestJSValue_TypecastToJSValue;
  27294. begin
  27295. StartProgram(false);
  27296. Add([
  27297. 'type',
  27298. ' TArr = array of word;',
  27299. ' TRec = record end;',
  27300. ' TSet = set of boolean;',
  27301. 'procedure Fly(v: jsvalue);',
  27302. 'begin',
  27303. 'end;',
  27304. 'var',
  27305. ' a: TArr;',
  27306. ' r: TRec;',
  27307. ' s: TSet;',
  27308. 'begin',
  27309. ' Fly(jsvalue(a));',
  27310. ' Fly(jsvalue(r));',
  27311. ' Fly(jsvalue(s));',
  27312. '']);
  27313. ConvertProgram;
  27314. CheckSource('TestJSValue_TypecastToJSValue',
  27315. LinesToStr([ // statements
  27316. 'rtl.recNewT(this, "TRec", function () {',
  27317. ' this.$eq = function (b) {',
  27318. ' return true;',
  27319. ' };',
  27320. ' this.$assign = function (s) {',
  27321. ' return this;',
  27322. ' };',
  27323. '});',
  27324. 'this.Fly = function (v) {',
  27325. '};',
  27326. 'this.a = [];',
  27327. 'this.r = this.TRec.$new();',
  27328. 'this.s = {};',
  27329. '']),
  27330. LinesToStr([ // $mod.$main
  27331. '$mod.Fly($mod.a);',
  27332. '$mod.Fly($mod.r);',
  27333. '$mod.Fly($mod.s);',
  27334. '']));
  27335. end;
  27336. procedure TTestModule.TestJSValue_Equal;
  27337. begin
  27338. StartProgram(false);
  27339. Add('type');
  27340. Add(' integer = longint;');
  27341. Add(' TYesNo = boolean;');
  27342. Add(' TFloat = double;');
  27343. Add(' TCaption = string;');
  27344. Add(' TChar = char;');
  27345. Add(' TMulti = JSValue;');
  27346. Add('var');
  27347. Add(' v: jsvalue;');
  27348. Add(' i: integer;');
  27349. Add(' s: TCaption;');
  27350. Add(' b: TYesNo;');
  27351. Add(' d: TFloat;');
  27352. Add(' c: char;');
  27353. Add(' m: TMulti;');
  27354. Add('begin');
  27355. Add(' b:=v=v;');
  27356. Add(' b:=v<>v;');
  27357. Add(' b:=v=1;');
  27358. Add(' b:=v<>1;');
  27359. Add(' b:=2=v;');
  27360. Add(' b:=2<>v;');
  27361. Add(' b:=v=i;');
  27362. Add(' b:=i=v;');
  27363. Add(' b:=v=nil;');
  27364. Add(' b:=nil=v;');
  27365. Add(' b:=v=false;');
  27366. Add(' b:=true=v;');
  27367. Add(' b:=v=b;');
  27368. Add(' b:=b=v;');
  27369. Add(' b:=v=s;');
  27370. Add(' b:=s=v;');
  27371. Add(' b:=v=''foo'';');
  27372. Add(' b:=''''=v;');
  27373. Add(' b:=v=d;');
  27374. Add(' b:=d=v;');
  27375. Add(' b:=v=3.4;');
  27376. Add(' b:=5.6=v;');
  27377. Add(' b:=v=c;');
  27378. Add(' b:=c=v;');
  27379. Add(' b:=m=m;');
  27380. Add(' b:=v=m;');
  27381. Add(' b:=m=v;');
  27382. ConvertProgram;
  27383. CheckSource('TestJSValue_Equal',
  27384. LinesToStr([ // statements
  27385. 'this.v = undefined;',
  27386. 'this.i = 0;',
  27387. 'this.s = "";',
  27388. 'this.b = false;',
  27389. 'this.d = 0.0;',
  27390. 'this.c = "";',
  27391. 'this.m = undefined;',
  27392. '']),
  27393. LinesToStr([ // $mod.$main
  27394. '$mod.b = $mod.v == $mod.v;',
  27395. '$mod.b = $mod.v != $mod.v;',
  27396. '$mod.b = $mod.v == 1;',
  27397. '$mod.b = $mod.v != 1;',
  27398. '$mod.b = 2 == $mod.v;',
  27399. '$mod.b = 2 != $mod.v;',
  27400. '$mod.b = $mod.v == $mod.i;',
  27401. '$mod.b = $mod.i == $mod.v;',
  27402. '$mod.b = $mod.v == null;',
  27403. '$mod.b = null == $mod.v;',
  27404. '$mod.b = $mod.v == false;',
  27405. '$mod.b = true == $mod.v;',
  27406. '$mod.b = $mod.v == $mod.b;',
  27407. '$mod.b = $mod.b == $mod.v;',
  27408. '$mod.b = $mod.v == $mod.s;',
  27409. '$mod.b = $mod.s == $mod.v;',
  27410. '$mod.b = $mod.v == "foo";',
  27411. '$mod.b = "" == $mod.v;',
  27412. '$mod.b = $mod.v == $mod.d;',
  27413. '$mod.b = $mod.d == $mod.v;',
  27414. '$mod.b = $mod.v == 3.4;',
  27415. '$mod.b = 5.6 == $mod.v;',
  27416. '$mod.b = $mod.v == $mod.c;',
  27417. '$mod.b = $mod.c == $mod.v;',
  27418. '$mod.b = $mod.m == $mod.m;',
  27419. '$mod.b = $mod.v == $mod.m;',
  27420. '$mod.b = $mod.m == $mod.v;',
  27421. '']));
  27422. end;
  27423. procedure TTestModule.TestJSValue_If;
  27424. begin
  27425. StartProgram(false);
  27426. Add([
  27427. 'procedure Fly(var u);',
  27428. 'begin',
  27429. ' if jsvalue(u) then ;',
  27430. 'end;',
  27431. 'var',
  27432. ' v: jsvalue;',
  27433. 'begin',
  27434. ' if v then ;',
  27435. ' while v do ;',
  27436. ' repeat until v;',
  27437. '']);
  27438. ConvertProgram;
  27439. CheckSource('TestJSValue_If',
  27440. LinesToStr([ // statements
  27441. 'this.Fly = function (u) {',
  27442. ' if (u.get()) ;',
  27443. '};',
  27444. 'this.v = undefined;',
  27445. '']),
  27446. LinesToStr([ // $mod.$main
  27447. 'if ($mod.v) ;',
  27448. 'while($mod.v){',
  27449. '};',
  27450. 'do{',
  27451. '} while(!$mod.v);',
  27452. '']));
  27453. end;
  27454. procedure TTestModule.TestJSValue_Not;
  27455. begin
  27456. StartProgram(false);
  27457. Add([
  27458. 'var',
  27459. ' v: jsvalue;',
  27460. ' b: boolean;',
  27461. 'begin',
  27462. ' b:=not v;',
  27463. ' if not v then ;',
  27464. ' while not v do ;',
  27465. ' repeat until not v;',
  27466. '']);
  27467. ConvertProgram;
  27468. CheckSource('TestJSValue_If',
  27469. LinesToStr([ // statements
  27470. 'this.v = undefined;',
  27471. 'this.b = false;',
  27472. '']),
  27473. LinesToStr([ // $mod.$main
  27474. '$mod.b=!$mod.v;',
  27475. 'if (!$mod.v) ;',
  27476. 'while(!$mod.v){',
  27477. '};',
  27478. 'do{',
  27479. '} while($mod.v);',
  27480. '']));
  27481. end;
  27482. procedure TTestModule.TestJSValue_Enum;
  27483. begin
  27484. StartProgram(false);
  27485. Add('type');
  27486. Add(' TColor = (red, blue);');
  27487. Add(' TRedBlue = TColor;');
  27488. Add('var');
  27489. Add(' v: jsvalue;');
  27490. Add(' e: TColor;');
  27491. Add('begin');
  27492. Add(' v:=e;');
  27493. Add(' v:=TColor(e);');
  27494. Add(' v:=TRedBlue(e);');
  27495. Add(' e:=TColor(v);');
  27496. Add(' e:=TRedBlue(v);');
  27497. ConvertProgram;
  27498. CheckSource('TestJSValue_Enum',
  27499. LinesToStr([ // statements
  27500. 'this.TColor = {',
  27501. ' "0": "red",',
  27502. ' red: 0,',
  27503. ' "1": "blue",',
  27504. ' blue: 1',
  27505. '};',
  27506. 'this.v = undefined;',
  27507. 'this.e = 0;',
  27508. '']),
  27509. LinesToStr([ // $mod.$main
  27510. '$mod.v = $mod.e;',
  27511. '$mod.v = $mod.e;',
  27512. '$mod.v = $mod.e;',
  27513. '$mod.e = $mod.v;',
  27514. '$mod.e = $mod.v;',
  27515. '']));
  27516. end;
  27517. procedure TTestModule.TestJSValue_ClassInstance;
  27518. begin
  27519. StartProgram(false);
  27520. Add([
  27521. 'type',
  27522. ' TObject = class',
  27523. ' end;',
  27524. ' TBirdObject = TObject;',
  27525. 'var',
  27526. ' v: jsvalue;',
  27527. ' o: TObject;',
  27528. 'begin',
  27529. ' v:=o;',
  27530. ' v:=TObject(o);',
  27531. ' v:=TBirdObject(o);',
  27532. ' o:=TObject(v);',
  27533. ' o:=TBirdObject(v);',
  27534. ' if v is TObject then ;',
  27535. '']);
  27536. ConvertProgram;
  27537. CheckSource('TestJSValue_ClassInstance',
  27538. LinesToStr([ // statements
  27539. 'rtl.createClass(this, "TObject", null, function () {',
  27540. ' this.$init = function () {',
  27541. ' };',
  27542. ' this.$final = function () {',
  27543. ' };',
  27544. '});',
  27545. 'this.v = undefined;',
  27546. 'this.o = null;',
  27547. '']),
  27548. LinesToStr([ // $mod.$main
  27549. '$mod.v = $mod.o;',
  27550. '$mod.v = $mod.o;',
  27551. '$mod.v = $mod.o;',
  27552. '$mod.o = rtl.getObject($mod.v);',
  27553. '$mod.o = rtl.getObject($mod.v);',
  27554. 'if (rtl.isExt($mod.v, $mod.TObject, 1)) ;',
  27555. '']));
  27556. end;
  27557. procedure TTestModule.TestJSValue_ClassOf;
  27558. begin
  27559. StartProgram(false);
  27560. Add([
  27561. 'type',
  27562. ' TClass = class of TObject;',
  27563. ' TObject = class',
  27564. ' end;',
  27565. ' TBirds = class of TBird;',
  27566. ' TBird = class(TObject) end;',
  27567. 'var',
  27568. ' v: jsvalue;',
  27569. ' c: TClass;',
  27570. 'begin',
  27571. ' v:=c;',
  27572. ' v:=TObject;',
  27573. ' v:=TClass(c);',
  27574. ' v:=TBirds(c);',
  27575. ' c:=TClass(v);',
  27576. ' c:=TBirds(v);',
  27577. ' if v is TClass then ;',
  27578. '']);
  27579. ConvertProgram;
  27580. CheckSource('TestJSValue_ClassOf',
  27581. LinesToStr([ // statements
  27582. 'rtl.createClass(this, "TObject", null, function () {',
  27583. ' this.$init = function () {',
  27584. ' };',
  27585. ' this.$final = function () {',
  27586. ' };',
  27587. '});',
  27588. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  27589. '});',
  27590. 'this.v = undefined;',
  27591. 'this.c = null;',
  27592. '']),
  27593. LinesToStr([ // $mod.$main
  27594. '$mod.v = $mod.c;',
  27595. '$mod.v = $mod.TObject;',
  27596. '$mod.v = $mod.c;',
  27597. '$mod.v = $mod.c;',
  27598. '$mod.c = rtl.getObject($mod.v);',
  27599. '$mod.c = rtl.getObject($mod.v);',
  27600. 'if (rtl.isExt($mod.v, $mod.TObject, 2)) ;',
  27601. '']));
  27602. end;
  27603. procedure TTestModule.TestJSValue_ArrayOfJSValue;
  27604. begin
  27605. StartProgram(false);
  27606. Add([
  27607. 'type',
  27608. ' integer = longint;',
  27609. ' TArray = array of JSValue;',
  27610. ' TArrgh = tarray;',
  27611. ' TArrInt = array of integer;',
  27612. 'var',
  27613. ' v: jsvalue;',
  27614. ' TheArray: tarray = (1,''2'');',
  27615. ' Arr: tarrgh;',
  27616. ' i: integer;',
  27617. ' ArrInt: tarrint;',
  27618. 'begin',
  27619. ' arr:=thearray;',
  27620. ' thearray:=arr;',
  27621. ' setlength(arr,2);',
  27622. ' setlength(thearray,3);',
  27623. ' arr[4]:=v;',
  27624. ' arr[5]:=length(thearray);',
  27625. ' arr[6]:=nil;',
  27626. ' arr[7]:=thearray[8];',
  27627. ' arr[low(arr)]:=high(thearray);',
  27628. ' arr:=arrint;',
  27629. ' arrInt:=tarrint(arr);',
  27630. ' if TheArray = nil then ;',
  27631. ' if nil = TheArray then ;',
  27632. ' if TheArray <> nil then ;',
  27633. ' if nil <> TheArray then ;',
  27634. '']);
  27635. ConvertProgram;
  27636. CheckSource('TestJSValue_ArrayOfJSValue',
  27637. LinesToStr([ // statements
  27638. 'this.v = undefined;',
  27639. 'this.TheArray = [1, "2"];',
  27640. 'this.Arr = [];',
  27641. 'this.i = 0;',
  27642. 'this.ArrInt = [];',
  27643. '']),
  27644. LinesToStr([ // $mod.$main
  27645. '$mod.Arr = rtl.arrayRef($mod.TheArray);',
  27646. '$mod.TheArray = rtl.arrayRef($mod.Arr);',
  27647. '$mod.Arr = rtl.arraySetLength($mod.Arr,undefined,2);',
  27648. '$mod.TheArray = rtl.arraySetLength($mod.TheArray,undefined,3);',
  27649. '$mod.Arr[4] = $mod.v;',
  27650. '$mod.Arr[5] = rtl.length($mod.TheArray);',
  27651. '$mod.Arr[6] = null;',
  27652. '$mod.Arr[7] = $mod.TheArray[8];',
  27653. '$mod.Arr[0] = rtl.length($mod.TheArray) - 1;',
  27654. '$mod.Arr = rtl.arrayRef($mod.ArrInt);',
  27655. '$mod.ArrInt = $mod.Arr;',
  27656. 'if (rtl.length($mod.TheArray) === 0) ;',
  27657. 'if (rtl.length($mod.TheArray) === 0) ;',
  27658. 'if (rtl.length($mod.TheArray) > 0) ;',
  27659. 'if (rtl.length($mod.TheArray) > 0) ;',
  27660. '']));
  27661. end;
  27662. procedure TTestModule.TestJSValue_ArrayLit;
  27663. begin
  27664. StartProgram(false);
  27665. Add([
  27666. 'type',
  27667. ' TFlag = (big,small);',
  27668. ' TArray = array of JSValue;',
  27669. ' TObject = class end;',
  27670. ' TClass = class of TObject;',
  27671. 'var',
  27672. ' v: jsvalue;',
  27673. ' a: TArray;',
  27674. ' o: TObject;',
  27675. 'begin',
  27676. ' a:=[];',
  27677. ' a:=[1];',
  27678. ' a:=[1,2];',
  27679. ' a:=[big];',
  27680. ' a:=[1,big];',
  27681. ' a:=[o,nil];',
  27682. '']);
  27683. ConvertProgram;
  27684. CheckSource('TestJSValue_ArrayLit',
  27685. LinesToStr([ // statements
  27686. 'this.TFlag = {',
  27687. ' "0": "big",',
  27688. ' big: 0,',
  27689. ' "1": "small",',
  27690. ' small: 1',
  27691. '};',
  27692. 'rtl.createClass(this, "TObject", null, function () {',
  27693. ' this.$init = function () {',
  27694. ' };',
  27695. ' this.$final = function () {',
  27696. ' };',
  27697. '});',
  27698. 'this.v = undefined;',
  27699. 'this.a = [];',
  27700. 'this.o = null;',
  27701. '']),
  27702. LinesToStr([ // $mod.$main
  27703. '$mod.a = [];',
  27704. '$mod.a = [1];',
  27705. '$mod.a = [1, 2];',
  27706. '$mod.a = [$mod.TFlag.big];',
  27707. '$mod.a = [1, $mod.TFlag.big];',
  27708. '$mod.a = [$mod.o, null];',
  27709. '']));
  27710. end;
  27711. procedure TTestModule.TestJSValue_Params;
  27712. begin
  27713. StartProgram(false);
  27714. Add('type');
  27715. Add(' integer = longint;');
  27716. Add(' TYesNo = boolean;');
  27717. Add(' TFloat = double;');
  27718. Add(' TCaption = string;');
  27719. Add(' TChar = char;');
  27720. Add('function DoIt(a: jsvalue; const b: jsvalue; var c: jsvalue; out d: jsvalue): jsvalue;');
  27721. Add('var');
  27722. Add(' l: jsvalue;');
  27723. Add('begin');
  27724. Add(' a:=a;');
  27725. Add(' l:=b;');
  27726. Add(' c:=c;');
  27727. Add(' d:=d;');
  27728. Add(' Result:=l;');
  27729. Add('end;');
  27730. Add('function DoSome(a: jsvalue; const b: jsvalue): jsvalue; begin end;');
  27731. Add('var');
  27732. Add(' v: jsvalue;');
  27733. Add(' i: integer;');
  27734. Add(' b: TYesNo;');
  27735. Add(' d: TFloat;');
  27736. Add(' s: TCaption;');
  27737. Add(' c: TChar;');
  27738. Add('begin');
  27739. Add(' v:=doit(v,v,v,v);');
  27740. Add(' i:=integer(dosome(i,i));');
  27741. Add(' b:=TYesNo(dosome(b,b));');
  27742. Add(' d:=TFloat(dosome(d,d));');
  27743. Add(' s:=TCaption(dosome(s,s));');
  27744. Add(' c:=TChar(dosome(c,c));');
  27745. ConvertProgram;
  27746. CheckSource('TestJSValue_Params',
  27747. LinesToStr([ // statements
  27748. 'this.DoIt = function (a, b, c, d) {',
  27749. ' var Result = undefined;',
  27750. ' var l = undefined;',
  27751. ' a = a;',
  27752. ' l = b;',
  27753. ' c.set(c.get());',
  27754. ' d.set(d.get());',
  27755. ' Result = l;',
  27756. ' return Result;',
  27757. '};',
  27758. 'this.DoSome = function (a, b) {',
  27759. ' var Result = undefined;',
  27760. ' return Result;',
  27761. '};',
  27762. 'this.v = undefined;',
  27763. 'this.i = 0;',
  27764. 'this.b = false;',
  27765. 'this.d = 0.0;',
  27766. 'this.s = "";',
  27767. 'this.c = "";',
  27768. '']),
  27769. LinesToStr([ // $mod.$main
  27770. '$mod.v = $mod.DoIt($mod.v, $mod.v, {',
  27771. ' p: $mod,',
  27772. ' get: function () {',
  27773. ' return this.p.v;',
  27774. ' },',
  27775. ' set: function (v) {',
  27776. ' this.p.v = v;',
  27777. ' }',
  27778. '}, {',
  27779. ' p: $mod,',
  27780. ' get: function () {',
  27781. ' return this.p.v;',
  27782. ' },',
  27783. ' set: function (v) {',
  27784. ' this.p.v = v;',
  27785. ' }',
  27786. '});',
  27787. '$mod.i = rtl.trunc($mod.DoSome($mod.i, $mod.i));',
  27788. '$mod.b = !($mod.DoSome($mod.b, $mod.b) == false);',
  27789. '$mod.d = rtl.getNumber($mod.DoSome($mod.d, $mod.d));',
  27790. '$mod.s = "" + $mod.DoSome($mod.s, $mod.s);',
  27791. '$mod.c = rtl.getChar($mod.DoSome($mod.c, $mod.c));',
  27792. '']));
  27793. end;
  27794. procedure TTestModule.TestJSValue_UntypedParam;
  27795. begin
  27796. StartProgram(false);
  27797. Add('function DoIt(const a; var b; out c): jsvalue;');
  27798. Add('begin');
  27799. Add(' Result:=a;');
  27800. Add(' Result:=b;');
  27801. Add(' Result:=c;');
  27802. Add(' b:=Result;');
  27803. Add(' c:=Result;');
  27804. Add('end;');
  27805. Add('var i: longint;');
  27806. Add('begin');
  27807. Add(' doit(i,i,i);');
  27808. ConvertProgram;
  27809. CheckSource('TestJSValue_UntypedParam',
  27810. LinesToStr([ // statements
  27811. 'this.DoIt = function (a, b, c) {',
  27812. ' var Result = undefined;',
  27813. ' Result = a;',
  27814. ' Result = b.get();',
  27815. ' Result = c.get();',
  27816. ' b.set(Result);',
  27817. ' c.set(Result);',
  27818. ' return Result;',
  27819. '};',
  27820. 'this.i = 0;',
  27821. '']),
  27822. LinesToStr([ // $mod.$main
  27823. '$mod.DoIt($mod.i, {',
  27824. ' p: $mod,',
  27825. ' get: function () {',
  27826. ' return this.p.i;',
  27827. ' },',
  27828. ' set: function (v) {',
  27829. ' this.p.i = v;',
  27830. ' }',
  27831. '}, {',
  27832. ' p: $mod,',
  27833. ' get: function () {',
  27834. ' return this.p.i;',
  27835. ' },',
  27836. ' set: function (v) {',
  27837. ' this.p.i = v;',
  27838. ' }',
  27839. '});',
  27840. '']));
  27841. end;
  27842. procedure TTestModule.TestJSValue_FuncResultType;
  27843. begin
  27844. StartProgram(false);
  27845. Add('type');
  27846. Add(' integer = longint;');
  27847. Add(' TJSValueArray = array of JSValue;');
  27848. Add(' TListSortCompare = function(Item1, Item2: JSValue): Integer;');
  27849. Add('procedure Sort(P: JSValue; aList: TJSValueArray; const Compare: TListSortCompare);');
  27850. Add('begin');
  27851. Add(' while Compare(P,aList[0])>0 do ;');
  27852. Add('end;');
  27853. Add('var');
  27854. Add(' Compare: TListSortCompare;');
  27855. Add(' V: JSValue;');
  27856. Add(' i: integer;');
  27857. Add('begin');
  27858. Add(' if Compare(V,V)>0 then ;');
  27859. Add(' if Compare(i,i)>1 then ;');
  27860. Add(' if Compare(nil,false)>2 then ;');
  27861. Add(' if Compare(1,true)>3 then ;');
  27862. ConvertProgram;
  27863. CheckSource('TestJSValue_UntypedParam',
  27864. LinesToStr([ // statements
  27865. 'this.Sort = function (P, aList, Compare) {',
  27866. ' while (Compare(P, aList[0]) > 0) {',
  27867. ' };',
  27868. '};',
  27869. 'this.Compare = null;',
  27870. 'this.V = undefined;',
  27871. 'this.i = 0;',
  27872. '']),
  27873. LinesToStr([ // $mod.$main
  27874. 'if ($mod.Compare($mod.V, $mod.V) > 0) ;',
  27875. 'if ($mod.Compare($mod.i, $mod.i) > 1) ;',
  27876. 'if ($mod.Compare(null, false) > 2) ;',
  27877. 'if ($mod.Compare(1, true) > 3) ;',
  27878. '']));
  27879. end;
  27880. procedure TTestModule.TestJSValue_ProcType_Assign;
  27881. begin
  27882. StartProgram(false);
  27883. Add('type');
  27884. Add(' integer = longint;');
  27885. Add(' TObject = class');
  27886. Add(' class function GetGlob: integer;');
  27887. Add(' function Getter: integer;');
  27888. Add(' end;');
  27889. Add('class function TObject.GetGlob: integer;');
  27890. Add('var v1: jsvalue;');
  27891. Add('begin');
  27892. Add(' v1:=@GetGlob;');
  27893. Add(' v1:[email protected];');
  27894. Add('end;');
  27895. Add('function TObject.Getter: integer;');
  27896. Add('var v2: jsvalue;');
  27897. Add('begin');
  27898. Add(' v2:=@Getter;');
  27899. Add(' v2:[email protected];');
  27900. Add(' v2:=@GetGlob;');
  27901. Add(' v2:[email protected];');
  27902. Add('end;');
  27903. Add('function GetIt(i: integer): integer;');
  27904. Add('var v3: jsvalue;');
  27905. Add('begin');
  27906. Add(' v3:=@GetIt;');
  27907. Add('end;');
  27908. Add('var');
  27909. Add(' V: JSValue;');
  27910. Add(' o: TObject;');
  27911. Add('begin');
  27912. Add(' v:=@GetIt;');
  27913. Add(' v:[email protected];');
  27914. Add(' v:[email protected];');
  27915. ConvertProgram;
  27916. CheckSource('TestJSValue_ProcType_Assign',
  27917. LinesToStr([ // statements
  27918. 'rtl.createClass(this, "TObject", null, function () {',
  27919. ' this.$init = function () {',
  27920. ' };',
  27921. ' this.$final = function () {',
  27922. ' };',
  27923. ' this.GetGlob = function () {',
  27924. ' var Result = 0;',
  27925. ' var v1 = undefined;',
  27926. ' v1 = rtl.createCallback(this, "GetGlob");',
  27927. ' v1 = rtl.createCallback(this, "GetGlob");',
  27928. ' return Result;',
  27929. ' };',
  27930. ' this.Getter = function () {',
  27931. ' var Result = 0;',
  27932. ' var v2 = undefined;',
  27933. ' v2 = rtl.createCallback(this, "Getter");',
  27934. ' v2 = rtl.createCallback(this, "Getter");',
  27935. ' v2 = rtl.createCallback(this.$class, "GetGlob");',
  27936. ' v2 = rtl.createCallback(this.$class, "GetGlob");',
  27937. ' return Result;',
  27938. ' };',
  27939. '});',
  27940. 'this.GetIt = function (i) {',
  27941. ' var Result = 0;',
  27942. ' var v3 = undefined;',
  27943. ' v3 = $mod.GetIt;',
  27944. ' return Result;',
  27945. '};',
  27946. 'this.V = undefined;',
  27947. 'this.o = null;',
  27948. '']),
  27949. LinesToStr([ // $mod.$main
  27950. '$mod.V = $mod.GetIt;',
  27951. '$mod.V = rtl.createCallback($mod.o, "Getter");',
  27952. '$mod.V = rtl.createCallback($mod.o.$class, "GetGlob");',
  27953. '']));
  27954. end;
  27955. procedure TTestModule.TestJSValue_ProcType_Equal;
  27956. begin
  27957. StartProgram(false);
  27958. Add('type');
  27959. Add(' integer = longint;');
  27960. Add(' TObject = class');
  27961. Add(' class function GetGlob: integer;');
  27962. Add(' function Getter: integer;');
  27963. Add(' end;');
  27964. Add('class function TObject.GetGlob: integer;');
  27965. Add('var v1: jsvalue;');
  27966. Add('begin');
  27967. Add(' if v1=@GetGlob then;');
  27968. Add(' if [email protected] then ;');
  27969. Add('end;');
  27970. Add('function TObject.Getter: integer;');
  27971. Add('var v2: jsvalue;');
  27972. Add('begin');
  27973. Add(' if v2=@Getter then;');
  27974. Add(' if [email protected] then ;');
  27975. Add(' if v2=@GetGlob then;');
  27976. Add(' if [email protected] then;');
  27977. Add('end;');
  27978. Add('function GetIt(i: integer): integer;');
  27979. Add('var v3: jsvalue;');
  27980. Add('begin');
  27981. Add(' if v3=@GetIt then;');
  27982. Add('end;');
  27983. Add('var');
  27984. Add(' V: JSValue;');
  27985. Add(' o: TObject;');
  27986. Add('begin');
  27987. Add(' if v=@GetIt then;');
  27988. Add(' if [email protected] then;');
  27989. Add(' if [email protected] then;');
  27990. Add(' if @GetIt=v then;');
  27991. Add(' if @o.Getter=v then;');
  27992. Add(' if @o.GetGlob=v then;');
  27993. ConvertProgram;
  27994. CheckSource('TestJSValue_ProcType_Equal',
  27995. LinesToStr([ // statements
  27996. 'rtl.createClass(this, "TObject", null, function () {',
  27997. ' this.$init = function () {',
  27998. ' };',
  27999. ' this.$final = function () {',
  28000. ' };',
  28001. ' this.GetGlob = function () {',
  28002. ' var Result = 0;',
  28003. ' var v1 = undefined;',
  28004. ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
  28005. ' if (rtl.eqCallback(v1, rtl.createCallback(this, "GetGlob"))) ;',
  28006. ' return Result;',
  28007. ' };',
  28008. ' this.Getter = function () {',
  28009. ' var Result = 0;',
  28010. ' var v2 = undefined;',
  28011. ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
  28012. ' if (rtl.eqCallback(v2, rtl.createCallback(this, "Getter"))) ;',
  28013. ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
  28014. ' if (rtl.eqCallback(v2, rtl.createCallback(this.$class, "GetGlob"))) ;',
  28015. ' return Result;',
  28016. ' };',
  28017. '});',
  28018. 'this.GetIt = function (i) {',
  28019. ' var Result = 0;',
  28020. ' var v3 = undefined;',
  28021. ' if (rtl.eqCallback(v3, $mod.GetIt)) ;',
  28022. ' return Result;',
  28023. '};',
  28024. 'this.V = undefined;',
  28025. 'this.o = null;',
  28026. '']),
  28027. LinesToStr([ // $mod.$main
  28028. 'if (rtl.eqCallback($mod.V, $mod.GetIt)) ;',
  28029. 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o, "Getter"))) ;',
  28030. 'if (rtl.eqCallback($mod.V, rtl.createCallback($mod.o.$class, "GetGlob"))) ;',
  28031. 'if (rtl.eqCallback($mod.GetIt, $mod.V)) ;',
  28032. 'if (rtl.eqCallback(rtl.createCallback($mod.o, "Getter"), $mod.V)) ;',
  28033. 'if (rtl.eqCallback(rtl.createCallback($mod.o.$class, "GetGlob"), $mod.V)) ;',
  28034. '']));
  28035. end;
  28036. procedure TTestModule.TestJSValue_ProcType_Param;
  28037. begin
  28038. StartProgram(false);
  28039. Add([
  28040. 'type',
  28041. ' variant = jsvalue;',
  28042. ' TArrVariant = array of variant;',
  28043. ' TArrVar2 = TArrVariant;',
  28044. ' TFuncInt = function: longint;',
  28045. 'function GetIt: longint;',
  28046. 'begin',
  28047. 'end;',
  28048. 'procedure DoIt(p: jsvalue; Arr: TArrVar2);',
  28049. 'var v: variant;',
  28050. 'begin',
  28051. ' v:=arr[1];',
  28052. 'end;',
  28053. 'var s: string;',
  28054. 'begin',
  28055. ' DoIt(GetIt,[]);',
  28056. ' DoIt(@GetIt,[]);',
  28057. ' DoIt(1,[s,GetIt]);',
  28058. ' DoIt(1,[s,@GetIt]);',
  28059. '']);
  28060. ConvertProgram;
  28061. CheckSource('TestJSValue_ProcType_Param',
  28062. LinesToStr([ // statements
  28063. 'this.GetIt = function () {',
  28064. ' var Result = 0;',
  28065. ' return Result;',
  28066. '};',
  28067. 'this.DoIt = function (p, Arr) {',
  28068. ' var v = undefined;',
  28069. ' v = Arr[1];',
  28070. '};',
  28071. 'this.s = "";',
  28072. '']),
  28073. LinesToStr([ // $mod.$main
  28074. '$mod.DoIt($mod.GetIt(), []);',
  28075. '$mod.DoIt($mod.GetIt, []);',
  28076. '$mod.DoIt(1, [$mod.s, $mod.GetIt()]);',
  28077. '$mod.DoIt(1, [$mod.s, $mod.GetIt]);',
  28078. '']));
  28079. end;
  28080. procedure TTestModule.TestJSValue_AssignToPointerFail;
  28081. begin
  28082. StartProgram(false);
  28083. Add([
  28084. 'var',
  28085. ' v: JSValue;',
  28086. ' p: Pointer;',
  28087. 'begin',
  28088. ' p:=v;',
  28089. '']);
  28090. SetExpectedPasResolverError('Incompatible types: got "JSValue" expected "Pointer"',
  28091. nIncompatibleTypesGotExpected);
  28092. ConvertProgram;
  28093. end;
  28094. procedure TTestModule.TestJSValue_OverloadDouble;
  28095. begin
  28096. StartProgram(false);
  28097. Add([
  28098. 'type',
  28099. ' integer = longint;',
  28100. ' tdatetime = double;',
  28101. 'procedure DoIt(d: double); begin end;',
  28102. 'procedure DoIt(v: jsvalue); begin end;',
  28103. 'var',
  28104. ' d: double;',
  28105. ' dt: tdatetime;',
  28106. ' i: integer;',
  28107. ' b: byte;',
  28108. ' shi: shortint;',
  28109. ' w: word;',
  28110. ' smi: smallint;',
  28111. ' lw: longword;',
  28112. ' li: longint;',
  28113. ' ni: nativeint;',
  28114. ' nu: nativeuint;',
  28115. 'begin',
  28116. ' DoIt(d);',
  28117. ' DoIt(dt);',
  28118. ' DoIt(i);',
  28119. ' DoIt(b);',
  28120. ' DoIt(shi);',
  28121. ' DoIt(w);',
  28122. ' DoIt(smi);',
  28123. ' DoIt(lw);',
  28124. ' DoIt(li);',
  28125. ' DoIt(ni);',
  28126. ' DoIt(nu);',
  28127. '']);
  28128. ConvertProgram;
  28129. CheckSource('TestJSValue_OverloadDouble',
  28130. LinesToStr([ // statements
  28131. 'this.DoIt = function (d) {',
  28132. '};',
  28133. 'this.DoIt$1 = function (v) {',
  28134. '};',
  28135. 'this.d = 0.0;',
  28136. 'this.dt = 0.0;',
  28137. 'this.i = 0;',
  28138. 'this.b = 0;',
  28139. 'this.shi = 0;',
  28140. 'this.w = 0;',
  28141. 'this.smi = 0;',
  28142. 'this.lw = 0;',
  28143. 'this.li = 0;',
  28144. 'this.ni = 0;',
  28145. 'this.nu = 0;',
  28146. '']),
  28147. LinesToStr([ // $mod.$main
  28148. '$mod.DoIt($mod.d);',
  28149. '$mod.DoIt($mod.dt);',
  28150. '$mod.DoIt$1($mod.i);',
  28151. '$mod.DoIt$1($mod.b);',
  28152. '$mod.DoIt$1($mod.shi);',
  28153. '$mod.DoIt$1($mod.w);',
  28154. '$mod.DoIt$1($mod.smi);',
  28155. '$mod.DoIt$1($mod.lw);',
  28156. '$mod.DoIt$1($mod.li);',
  28157. '$mod.DoIt$1($mod.ni);',
  28158. '$mod.DoIt$1($mod.nu);',
  28159. '']));
  28160. end;
  28161. procedure TTestModule.TestJSValue_OverloadNativeInt;
  28162. begin
  28163. StartProgram(false);
  28164. Add([
  28165. 'type',
  28166. ' integer = longint;',
  28167. ' int53 = nativeint;',
  28168. ' tdatetime = double;',
  28169. 'procedure DoIt(n: nativeint); begin end;',
  28170. 'procedure DoIt(v: jsvalue); begin end;',
  28171. 'var',
  28172. ' d: double;',
  28173. ' dt: tdatetime;',
  28174. ' i: integer;',
  28175. ' b: byte;',
  28176. ' shi: shortint;',
  28177. ' w: word;',
  28178. ' smi: smallint;',
  28179. ' lw: longword;',
  28180. ' li: longint;',
  28181. ' ni: nativeint;',
  28182. ' nu: nativeuint;',
  28183. 'begin',
  28184. ' DoIt(d);',
  28185. ' DoIt(dt);',
  28186. ' DoIt(i);',
  28187. ' DoIt(b);',
  28188. ' DoIt(shi);',
  28189. ' DoIt(w);',
  28190. ' DoIt(smi);',
  28191. ' DoIt(lw);',
  28192. ' DoIt(li);',
  28193. ' DoIt(ni);',
  28194. ' DoIt(nu);',
  28195. '']);
  28196. ConvertProgram;
  28197. CheckSource('TestJSValue_OverloadNativeInt',
  28198. LinesToStr([ // statements
  28199. 'this.DoIt = function (n) {',
  28200. '};',
  28201. 'this.DoIt$1 = function (v) {',
  28202. '};',
  28203. 'this.d = 0.0;',
  28204. 'this.dt = 0.0;',
  28205. 'this.i = 0;',
  28206. 'this.b = 0;',
  28207. 'this.shi = 0;',
  28208. 'this.w = 0;',
  28209. 'this.smi = 0;',
  28210. 'this.lw = 0;',
  28211. 'this.li = 0;',
  28212. 'this.ni = 0;',
  28213. 'this.nu = 0;',
  28214. '']),
  28215. LinesToStr([ // $mod.$main
  28216. '$mod.DoIt$1($mod.d);',
  28217. '$mod.DoIt$1($mod.dt);',
  28218. '$mod.DoIt($mod.i);',
  28219. '$mod.DoIt($mod.b);',
  28220. '$mod.DoIt($mod.shi);',
  28221. '$mod.DoIt($mod.w);',
  28222. '$mod.DoIt($mod.smi);',
  28223. '$mod.DoIt($mod.lw);',
  28224. '$mod.DoIt($mod.li);',
  28225. '$mod.DoIt($mod.ni);',
  28226. '$mod.DoIt($mod.nu);',
  28227. '']));
  28228. end;
  28229. procedure TTestModule.TestJSValue_OverloadWord;
  28230. begin
  28231. StartProgram(false);
  28232. Add([
  28233. 'type',
  28234. ' integer = longint;',
  28235. ' int53 = nativeint;',
  28236. ' tdatetime = double;',
  28237. 'procedure DoIt(w: word); begin end;',
  28238. 'procedure DoIt(v: jsvalue); begin end;',
  28239. 'var',
  28240. ' d: double;',
  28241. ' dt: tdatetime;',
  28242. ' i: integer;',
  28243. ' b: byte;',
  28244. ' shi: shortint;',
  28245. ' w: word;',
  28246. ' smi: smallint;',
  28247. ' lw: longword;',
  28248. ' li: longint;',
  28249. ' ni: nativeint;',
  28250. ' nu: nativeuint;',
  28251. 'begin',
  28252. ' DoIt(d);',
  28253. ' DoIt(dt);',
  28254. ' DoIt(i);',
  28255. ' DoIt(b);',
  28256. ' DoIt(shi);',
  28257. ' DoIt(w);',
  28258. ' DoIt(smi);',
  28259. ' DoIt(lw);',
  28260. ' DoIt(li);',
  28261. ' DoIt(ni);',
  28262. ' DoIt(nu);',
  28263. '']);
  28264. ConvertProgram;
  28265. CheckSource('TestJSValue_OverloadWord',
  28266. LinesToStr([ // statements
  28267. 'this.DoIt = function (w) {',
  28268. '};',
  28269. 'this.DoIt$1 = function (v) {',
  28270. '};',
  28271. 'this.d = 0.0;',
  28272. 'this.dt = 0.0;',
  28273. 'this.i = 0;',
  28274. 'this.b = 0;',
  28275. 'this.shi = 0;',
  28276. 'this.w = 0;',
  28277. 'this.smi = 0;',
  28278. 'this.lw = 0;',
  28279. 'this.li = 0;',
  28280. 'this.ni = 0;',
  28281. 'this.nu = 0;',
  28282. '']),
  28283. LinesToStr([ // $mod.$main
  28284. '$mod.DoIt$1($mod.d);',
  28285. '$mod.DoIt$1($mod.dt);',
  28286. '$mod.DoIt$1($mod.i);',
  28287. '$mod.DoIt($mod.b);',
  28288. '$mod.DoIt($mod.shi);',
  28289. '$mod.DoIt($mod.w);',
  28290. '$mod.DoIt$1($mod.smi);',
  28291. '$mod.DoIt$1($mod.lw);',
  28292. '$mod.DoIt$1($mod.li);',
  28293. '$mod.DoIt$1($mod.ni);',
  28294. '$mod.DoIt$1($mod.nu);',
  28295. '']));
  28296. end;
  28297. procedure TTestModule.TestJSValue_OverloadString;
  28298. begin
  28299. StartProgram(false);
  28300. Add([
  28301. 'type',
  28302. ' uni = string;',
  28303. ' WChar = char;',
  28304. 'procedure DoIt(s: string); begin end;',
  28305. 'procedure DoIt(v: jsvalue); begin end;',
  28306. 'var',
  28307. ' s: string;',
  28308. ' c: char;',
  28309. ' u: uni;',
  28310. 'begin',
  28311. ' DoIt(s);',
  28312. ' DoIt(c);',
  28313. ' DoIt(u);',
  28314. '']);
  28315. ConvertProgram;
  28316. CheckSource('TestJSValue_OverloadString',
  28317. LinesToStr([ // statements
  28318. 'this.DoIt = function (s) {',
  28319. '};',
  28320. 'this.DoIt$1 = function (v) {',
  28321. '};',
  28322. 'this.s = "";',
  28323. 'this.c = "";',
  28324. 'this.u = "";',
  28325. '']),
  28326. LinesToStr([ // $mod.$main
  28327. '$mod.DoIt($mod.s);',
  28328. '$mod.DoIt($mod.c);',
  28329. '$mod.DoIt($mod.u);',
  28330. '']));
  28331. end;
  28332. procedure TTestModule.TestJSValue_OverloadChar;
  28333. begin
  28334. StartProgram(false);
  28335. Add([
  28336. 'type',
  28337. ' uni = string;',
  28338. ' WChar = char;',
  28339. 'procedure DoIt(c: char); begin end;',
  28340. 'procedure DoIt(v: jsvalue); begin end;',
  28341. 'var',
  28342. ' s: string;',
  28343. ' c: char;',
  28344. ' u: uni;',
  28345. 'begin',
  28346. ' DoIt(s);',
  28347. ' DoIt(c);',
  28348. ' DoIt(u);',
  28349. '']);
  28350. ConvertProgram;
  28351. CheckSource('TestJSValue_OverloadChar',
  28352. LinesToStr([ // statements
  28353. 'this.DoIt = function (c) {',
  28354. '};',
  28355. 'this.DoIt$1 = function (v) {',
  28356. '};',
  28357. 'this.s = "";',
  28358. 'this.c = "";',
  28359. 'this.u = "";',
  28360. '']),
  28361. LinesToStr([ // $mod.$main
  28362. '$mod.DoIt$1($mod.s);',
  28363. '$mod.DoIt($mod.c);',
  28364. '$mod.DoIt$1($mod.u);',
  28365. '']));
  28366. end;
  28367. procedure TTestModule.TestJSValue_OverloadPointer;
  28368. begin
  28369. StartProgram(false);
  28370. Add([
  28371. 'type',
  28372. ' TObject = class end;',
  28373. 'procedure DoIt(p: pointer); begin end;',
  28374. 'procedure DoIt(v: jsvalue); begin end;',
  28375. 'var',
  28376. ' o: TObject;',
  28377. 'begin',
  28378. ' DoIt(o);',
  28379. '']);
  28380. ConvertProgram;
  28381. CheckSource('TestJSValue_OverloadPointer',
  28382. LinesToStr([ // statements
  28383. 'rtl.createClass(this, "TObject", null, function () {',
  28384. ' this.$init = function () {',
  28385. ' };',
  28386. ' this.$final = function () {',
  28387. ' };',
  28388. '});',
  28389. 'this.DoIt = function (p) {',
  28390. '};',
  28391. 'this.DoIt$1 = function (v) {',
  28392. '};',
  28393. 'this.o = null;',
  28394. '']),
  28395. LinesToStr([ // $mod.$main
  28396. '$mod.DoIt($mod.o);',
  28397. '']));
  28398. end;
  28399. procedure TTestModule.TestJSValue_ForIn;
  28400. begin
  28401. StartProgram(false);
  28402. Add([
  28403. 'var',
  28404. ' v: JSValue;',
  28405. ' key: string;',
  28406. 'begin',
  28407. ' for key in v do begin',
  28408. ' if key=''abc'' then ;',
  28409. ' end;',
  28410. '']);
  28411. ConvertProgram;
  28412. CheckSource('TestJSValue_ForIn',
  28413. LinesToStr([ // statements
  28414. 'this.v = undefined;',
  28415. 'this.key = "";',
  28416. '']),
  28417. LinesToStr([ // $mod.$main
  28418. 'for ($mod.key in $mod.v) {',
  28419. ' if ($mod.key === "abc") ;',
  28420. '};',
  28421. '']));
  28422. end;
  28423. procedure TTestModule.TestRTTI_IntRange;
  28424. begin
  28425. WithTypeInfo:=true;
  28426. StartProgram(true,[supTypeInfo]);
  28427. Add([
  28428. '{$modeswitch externalclass}',
  28429. 'type',
  28430. ' TGraphicsColor = -$7FFFFFFF-1..$7FFFFFFF;',
  28431. ' TColor = type TGraphicsColor;',
  28432. 'var',
  28433. ' p: TTypeInfo;',
  28434. ' k: TTypeKind;',
  28435. 'begin',
  28436. ' p:=typeinfo(TGraphicsColor);',
  28437. ' p:=typeinfo(TColor);',
  28438. ' k:=GetTypeKind(TGraphicsColor);',
  28439. ' k:=GetTypeKind(TColor);',
  28440. '']);
  28441. ConvertProgram;
  28442. CheckSource('TestRTTI_IntRange',
  28443. LinesToStr([ // statements
  28444. 'this.$rtti.$Int("TGraphicsColor", {',
  28445. ' minvalue: -2147483648,',
  28446. ' maxvalue: 2147483647,',
  28447. ' ordtype: 4',
  28448. '});',
  28449. 'this.$rtti.$inherited("TColor", this.$rtti["TGraphicsColor"], {});',
  28450. 'this.p = null;',
  28451. 'this.k = 0;',
  28452. '']),
  28453. LinesToStr([ // $mod.$main
  28454. '$mod.p = $mod.$rtti["TGraphicsColor"];',
  28455. '$mod.p = $mod.$rtti["TColor"];',
  28456. '$mod.k = 1;',
  28457. '$mod.k = 1;',
  28458. '']));
  28459. end;
  28460. procedure TTestModule.TestRTTI_Double;
  28461. begin
  28462. WithTypeInfo:=true;
  28463. StartProgram(true,[supTypeInfo]);
  28464. Add([
  28465. '{$modeswitch externalclass}',
  28466. 'type',
  28467. ' TFloat = type double;',
  28468. 'var',
  28469. ' p: TTypeInfo;',
  28470. 'begin',
  28471. ' p:=typeinfo(double);',
  28472. ' p:=typeinfo(TFloat);',
  28473. '']);
  28474. ConvertProgram;
  28475. CheckSource('TestRTTI_Double',
  28476. LinesToStr([ // statements
  28477. 'this.$rtti.$inherited("TFloat", rtl.double, {});',
  28478. 'this.p = null;',
  28479. '']),
  28480. LinesToStr([ // $mod.$main
  28481. '$mod.p = rtl.double;',
  28482. '$mod.p = $mod.$rtti["TFloat"];',
  28483. '']));
  28484. end;
  28485. procedure TTestModule.TestRTTI_ProcType;
  28486. begin
  28487. WithTypeInfo:=true;
  28488. StartProgram(false);
  28489. Add('type');
  28490. Add(' TProcA = procedure;');
  28491. Add(' TMethodB = procedure of object;');
  28492. Add(' TProcC = procedure; varargs;');
  28493. Add(' TProcD = procedure(i: longint; const j: string; var c: char; out d: double);');
  28494. Add(' TProcE = function: nativeint;');
  28495. Add(' TProcF = function(const p: TProcA): nativeuint;');
  28496. Add('var p: pointer;');
  28497. Add('begin');
  28498. Add(' p:=typeinfo(tproca);');
  28499. ConvertProgram;
  28500. CheckSource('TestRTTI_ProcType',
  28501. LinesToStr([ // statements
  28502. 'this.$rtti.$ProcVar("TProcA", {',
  28503. ' procsig: rtl.newTIProcSig(null)',
  28504. '});',
  28505. 'this.$rtti.$MethodVar("TMethodB", {',
  28506. ' procsig: rtl.newTIProcSig(null),',
  28507. ' methodkind: 0',
  28508. '});',
  28509. 'this.$rtti.$ProcVar("TProcC", {',
  28510. ' procsig: rtl.newTIProcSig(null, 2)',
  28511. '});',
  28512. 'this.$rtti.$ProcVar("TProcD", {',
  28513. ' procsig: rtl.newTIProcSig([["i", rtl.longint], ["j", rtl.string, 2], ["c", rtl.char, 1], ["d", rtl.double, 4]])',
  28514. '});',
  28515. 'this.$rtti.$ProcVar("TProcE", {',
  28516. ' procsig: rtl.newTIProcSig(null, rtl.nativeint)',
  28517. '});',
  28518. 'this.$rtti.$ProcVar("TProcF", {',
  28519. ' procsig: rtl.newTIProcSig([["p", this.$rtti["TProcA"], 2]], rtl.nativeuint)',
  28520. '});',
  28521. 'this.p = null;',
  28522. '']),
  28523. LinesToStr([ // $mod.$main
  28524. '$mod.p = $mod.$rtti["TProcA"];',
  28525. '']));
  28526. end;
  28527. procedure TTestModule.TestRTTI_ProcType_ArgFromOtherUnit;
  28528. begin
  28529. WithTypeInfo:=true;
  28530. AddModuleWithIntfImplSrc('unit2.pas',
  28531. LinesToStr([
  28532. 'type',
  28533. ' TObject = class end;'
  28534. ]),
  28535. '');
  28536. StartUnit(true);
  28537. Add('interface');
  28538. Add('uses unit2;');
  28539. Add('type');
  28540. Add(' TProcA = function(o: tobject): tobject;');
  28541. Add('implementation');
  28542. Add('type');
  28543. Add(' TProcB = function(o: tobject): tobject;');
  28544. Add('var p: Pointer;');
  28545. Add('initialization');
  28546. Add(' p:=typeinfo(tproca);');
  28547. Add(' p:=typeinfo(tprocb);');
  28548. ConvertUnit;
  28549. CheckSource('TestRTTI_ProcType_ArgFromOtherUnit',
  28550. LinesToStr([ // statements
  28551. 'var $impl = $mod.$impl;',
  28552. 'this.$rtti.$ProcVar("TProcA", {',
  28553. ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
  28554. '});',
  28555. '']),
  28556. LinesToStr([ // this.$init
  28557. '$impl.p = $mod.$rtti["TProcA"];',
  28558. '$impl.p = $mod.$rtti["TProcB"];',
  28559. '']),
  28560. LinesToStr([ // implementation
  28561. '$mod.$rtti.$ProcVar("TProcB", {',
  28562. ' procsig: rtl.newTIProcSig([["o", pas.unit2.$rtti["TObject"]]], pas.unit2.$rtti["TObject"])',
  28563. '});',
  28564. '$impl.p = null;',
  28565. '']) );
  28566. end;
  28567. procedure TTestModule.TestRTTI_EnumAndSetType;
  28568. begin
  28569. WithTypeInfo:=true;
  28570. StartProgram(false);
  28571. Add('type');
  28572. Add(' TFlag = (light,dark);');
  28573. Add(' TFlags = set of TFlag;');
  28574. Add(' TProc = function(f: TFlags): TFlag;');
  28575. Add('var p: pointer;');
  28576. Add('begin');
  28577. Add(' p:=typeinfo(tflag);');
  28578. Add(' p:=typeinfo(tflags);');
  28579. ConvertProgram;
  28580. CheckSource('TestRTTI_EnumAndType',
  28581. LinesToStr([ // statements
  28582. 'this.TFlag = {',
  28583. ' "0": "light",',
  28584. ' light: 0,',
  28585. ' "1": "dark",',
  28586. ' dark: 1',
  28587. '};',
  28588. 'this.$rtti.$Enum("TFlag", {',
  28589. ' minvalue: 0,',
  28590. ' maxvalue: 1,',
  28591. ' ordtype: 1,',
  28592. ' enumtype: this.TFlag',
  28593. '});',
  28594. 'this.$rtti.$Set("TFlags", {',
  28595. ' comptype: this.$rtti["TFlag"]',
  28596. '});',
  28597. 'this.$rtti.$ProcVar("TProc", {',
  28598. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TFlags"]]], this.$rtti["TFlag"])',
  28599. '});',
  28600. 'this.p = null;',
  28601. '']),
  28602. LinesToStr([ // $mod.$main
  28603. '$mod.p = $mod.$rtti["TFlag"];',
  28604. '$mod.p = $mod.$rtti["TFlags"];',
  28605. '']));
  28606. end;
  28607. procedure TTestModule.TestRTTI_EnumRange;
  28608. begin
  28609. WithTypeInfo:=true;
  28610. StartProgram(false);
  28611. Add([
  28612. 'type',
  28613. ' TCol = (red,green,blue);',
  28614. ' TColRg = green..blue;',
  28615. ' TSetOfColRg = set of TColRg;',
  28616. 'var p: pointer;',
  28617. 'begin',
  28618. ' p:=typeinfo(tcolrg);',
  28619. ' p:=typeinfo(tsetofcolrg);',
  28620. '']);
  28621. ConvertProgram;
  28622. end;
  28623. procedure TTestModule.TestRTTI_AnonymousEnumType;
  28624. begin
  28625. WithTypeInfo:=true;
  28626. StartProgram(false);
  28627. Add('type');
  28628. Add(' TFlags = set of (red, green);');
  28629. Add('var');
  28630. Add(' f: TFlags;');
  28631. Add('begin');
  28632. Add(' Include(f,red);');
  28633. ConvertProgram;
  28634. CheckSource('TestRTTI_AnonymousEnumType',
  28635. LinesToStr([ // statements
  28636. 'this.TFlags$a = {',
  28637. ' "0": "red",',
  28638. ' red: 0,',
  28639. ' "1": "green",',
  28640. ' green: 1',
  28641. '};',
  28642. 'this.$rtti.$Enum("TFlags$a", {',
  28643. ' minvalue: 0,',
  28644. ' maxvalue: 1,',
  28645. ' ordtype: 1,',
  28646. ' enumtype: this.TFlags$a',
  28647. '});',
  28648. 'this.$rtti.$Set("TFlags", {',
  28649. ' comptype: this.$rtti["TFlags$a"]',
  28650. '});',
  28651. 'this.f = {};',
  28652. '']),
  28653. LinesToStr([
  28654. '$mod.f = rtl.includeSet($mod.f, $mod.TFlags$a.red);',
  28655. '']));
  28656. end;
  28657. procedure TTestModule.TestRTTI_StaticArray;
  28658. begin
  28659. WithTypeInfo:=true;
  28660. StartProgram(false);
  28661. Add('type');
  28662. Add(' TFlag = (light,dark);');
  28663. Add(' TFlagNames = array[TFlag] of string;');
  28664. Add(' TBoolNames = array[boolean] of string;');
  28665. Add(' TByteArray = array[1..32768] of byte;');
  28666. Add(' TProc = function(f: TBoolNames): TFlagNames;');
  28667. Add('var p: pointer;');
  28668. Add('begin');
  28669. Add(' p:=typeinfo(TFlagNames);');
  28670. Add(' p:=typeinfo(TBoolNames);');
  28671. ConvertProgram;
  28672. CheckSource('TestRTTI_StaticArray',
  28673. LinesToStr([ // statements
  28674. 'this.TFlag = {',
  28675. ' "0": "light",',
  28676. ' light: 0,',
  28677. ' "1": "dark",',
  28678. ' dark: 1',
  28679. '};',
  28680. 'this.$rtti.$Enum("TFlag", {',
  28681. ' minvalue: 0,',
  28682. ' maxvalue: 1,',
  28683. ' ordtype: 1,',
  28684. ' enumtype: this.TFlag',
  28685. '});',
  28686. 'this.$rtti.$StaticArray("TFlagNames", {',
  28687. ' dims: [2],',
  28688. ' eltype: rtl.string',
  28689. '});',
  28690. 'this.$rtti.$StaticArray("TBoolNames", {',
  28691. ' dims: [2],',
  28692. ' eltype: rtl.string',
  28693. '});',
  28694. 'this.$rtti.$StaticArray("TByteArray", {',
  28695. ' dims: [32768],',
  28696. ' eltype: rtl.byte',
  28697. '});',
  28698. 'this.$rtti.$ProcVar("TProc", {',
  28699. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TBoolNames"]]], this.$rtti["TFlagNames"])',
  28700. '});',
  28701. 'this.p = null;',
  28702. '']),
  28703. LinesToStr([ // $mod.$main
  28704. '$mod.p = $mod.$rtti["TFlagNames"];',
  28705. '$mod.p = $mod.$rtti["TBoolNames"];',
  28706. '']));
  28707. end;
  28708. procedure TTestModule.TestRTTI_DynArray;
  28709. begin
  28710. WithTypeInfo:=true;
  28711. StartProgram(false);
  28712. Add('type');
  28713. Add(' TArrStr = array of string;');
  28714. Add(' TArr2Dim = array of tarrstr;');
  28715. Add(' TProc = function(f: TArrStr): TArr2Dim;');
  28716. Add('var p: pointer;');
  28717. Add('begin');
  28718. Add(' p:=typeinfo(tarrstr);');
  28719. Add(' p:=typeinfo(tarr2dim);');
  28720. ConvertProgram;
  28721. CheckSource('TestRTTI_DynArray',
  28722. LinesToStr([ // statements
  28723. 'this.$rtti.$DynArray("TArrStr", {',
  28724. ' eltype: rtl.string',
  28725. '});',
  28726. 'this.$rtti.$DynArray("TArr2Dim", {',
  28727. ' eltype: this.$rtti["TArrStr"]',
  28728. '});',
  28729. 'this.$rtti.$ProcVar("TProc", {',
  28730. ' procsig: rtl.newTIProcSig([["f", this.$rtti["TArrStr"]]], this.$rtti["TArr2Dim"])',
  28731. '});',
  28732. 'this.p = null;',
  28733. '']),
  28734. LinesToStr([ // $mod.$main
  28735. '$mod.p = $mod.$rtti["TArrStr"];',
  28736. '$mod.p = $mod.$rtti["TArr2Dim"];',
  28737. '']));
  28738. end;
  28739. procedure TTestModule.TestRTTI_ArrayNestedAnonymous;
  28740. begin
  28741. WithTypeInfo:=true;
  28742. StartProgram(false);
  28743. Add('type');
  28744. Add(' TArr = array of array of longint;');
  28745. Add('var a: TArr;');
  28746. Add('begin');
  28747. ConvertProgram;
  28748. CheckSource('TestRTTI_ArrayNestedAnonymous',
  28749. LinesToStr([ // statements
  28750. 'this.$rtti.$DynArray("TArr$a", {',
  28751. ' eltype: rtl.longint',
  28752. '});',
  28753. 'this.$rtti.$DynArray("TArr", {',
  28754. ' eltype: this.$rtti["TArr$a"]',
  28755. '});',
  28756. 'this.a = [];',
  28757. '']),
  28758. LinesToStr([ // $mod.$main
  28759. ]));
  28760. end;
  28761. procedure TTestModule.TestRTTI_PublishedMethodOverloadFail;
  28762. begin
  28763. WithTypeInfo:=true;
  28764. StartProgram(false);
  28765. Add('type');
  28766. Add(' TObject = class');
  28767. Add(' published');
  28768. Add(' procedure Proc; virtual; abstract;');
  28769. Add(' procedure Proc(Sender: tobject); virtual; abstract;');
  28770. Add(' end;');
  28771. Add('begin');
  28772. SetExpectedPasResolverError('Duplicate published method "Proc" at test1.pp(6,19)',
  28773. nDuplicatePublishedMethodXAtY);
  28774. ConvertProgram;
  28775. end;
  28776. procedure TTestModule.TestRTTI_PublishedMethodHideNoHint;
  28777. begin
  28778. WithTypeInfo:=true;
  28779. StartUnit(false);
  28780. Add([
  28781. 'interface',
  28782. 'type',
  28783. ' TObject = class',
  28784. ' end;',
  28785. ' {$M+}',
  28786. ' TBird = class',
  28787. ' procedure Fly;',
  28788. ' end;',
  28789. ' {$M-}',
  28790. 'type',
  28791. ' TEagle = class(TBird)',
  28792. ' procedure Fly;',
  28793. ' end;',
  28794. 'implementation',
  28795. 'procedure TBird.Fly;',
  28796. 'begin',
  28797. 'end;',
  28798. 'procedure TEagle.Fly;',
  28799. 'begin',
  28800. 'end;',
  28801. '']);
  28802. ConvertUnit;
  28803. CheckSource('TestRTTI_PublishedMethodHideNoHint',
  28804. LinesToStr([ // statements
  28805. 'rtl.createClass(this, "TObject", null, function () {',
  28806. ' this.$init = function () {',
  28807. ' };',
  28808. ' this.$final = function () {',
  28809. ' };',
  28810. '});',
  28811. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  28812. ' this.Fly = function () {',
  28813. ' };',
  28814. ' var $r = this.$rtti;',
  28815. ' $r.addMethod("Fly", 0, null);',
  28816. '});',
  28817. 'rtl.createClass(this, "TEagle", this.TBird, function () {',
  28818. ' this.Fly = function () {',
  28819. ' };',
  28820. ' var $r = this.$rtti;',
  28821. ' $r.addMethod("Fly", 0, null);',
  28822. '});',
  28823. '']),
  28824. LinesToStr([ // $mod.$main
  28825. ]));
  28826. CheckResolverUnexpectedHints(true);
  28827. end;
  28828. procedure TTestModule.TestRTTI_PublishedMethodExternalFail;
  28829. begin
  28830. WithTypeInfo:=true;
  28831. StartProgram(false);
  28832. Add('type');
  28833. Add(' TObject = class');
  28834. Add(' published');
  28835. Add(' procedure Proc; external name ''foo'';');
  28836. Add(' end;');
  28837. Add('begin');
  28838. SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
  28839. nPublishedNameMustMatchExternal);
  28840. ConvertProgram;
  28841. end;
  28842. procedure TTestModule.TestRTTI_PublishedClassPropertyFail;
  28843. begin
  28844. WithTypeInfo:=true;
  28845. StartProgram(false);
  28846. Add('type');
  28847. Add(' TObject = class');
  28848. Add(' class var FA: longint;');
  28849. Add(' published');
  28850. Add(' class property A: longint read FA;');
  28851. Add(' end;');
  28852. Add('begin');
  28853. SetExpectedPasResolverError('Invalid published property modifier "class"',
  28854. nInvalidXModifierY);
  28855. ConvertProgram;
  28856. end;
  28857. procedure TTestModule.TestRTTI_PublishedClassFieldFail;
  28858. begin
  28859. WithTypeInfo:=true;
  28860. StartProgram(false);
  28861. Add('type');
  28862. Add(' TObject = class');
  28863. Add(' published');
  28864. Add(' class var FA: longint;');
  28865. Add(' end;');
  28866. Add('begin');
  28867. SetExpectedPasResolverError(sSymbolCannotBePublished,
  28868. nSymbolCannotBePublished);
  28869. ConvertProgram;
  28870. end;
  28871. procedure TTestModule.TestRTTI_PublishedFieldExternalFail;
  28872. begin
  28873. WithTypeInfo:=true;
  28874. StartProgram(false);
  28875. Add('{$modeswitch externalclass}');
  28876. Add('type');
  28877. Add(' TObject = class');
  28878. Add(' published');
  28879. Add(' V: longint; external name ''foo'';');
  28880. Add(' end;');
  28881. Add('begin');
  28882. SetExpectedPasResolverError(sPublishedNameMustMatchExternal,
  28883. nPublishedNameMustMatchExternal);
  28884. ConvertProgram;
  28885. end;
  28886. procedure TTestModule.TestRTTI_Class_Field;
  28887. begin
  28888. WithTypeInfo:=true;
  28889. StartProgram(false);
  28890. Add('{$modeswitch externalclass}');
  28891. Add('type');
  28892. Add(' TObject = class');
  28893. Add(' private');
  28894. Add(' FPropA: string;');
  28895. Add(' published');
  28896. Add(' VarLI: longint;');
  28897. Add(' VarC: char;');
  28898. Add(' VarS: string;');
  28899. Add(' VarD: double;');
  28900. Add(' VarB: boolean;');
  28901. Add(' VarLW: longword;');
  28902. Add(' VarSmI: smallint;');
  28903. Add(' VarW: word;');
  28904. Add(' VarShI: shortint;');
  28905. Add(' VarBy: byte;');
  28906. Add(' VarExt: longint external name ''VarExt'';');
  28907. Add(' ArrA, ArrB: array of byte;');
  28908. Add(' end;');
  28909. Add('var p: pointer;');
  28910. Add(' Obj: tobject;');
  28911. Add('begin');
  28912. Add(' p:=typeinfo(tobject);');
  28913. Add(' p:=typeinfo(p);');
  28914. Add(' p:=typeinfo(obj);');
  28915. ConvertProgram;
  28916. CheckSource('TestRTTI_Class_Field',
  28917. LinesToStr([ // statements
  28918. 'rtl.createClass(this, "TObject", null, function () {',
  28919. ' $mod.$rtti.$DynArray("TObject.ArrB$a", {',
  28920. ' eltype: rtl.byte',
  28921. ' });',
  28922. ' this.$init = function () {',
  28923. ' this.FPropA = "";',
  28924. ' this.VarLI = 0;',
  28925. ' this.VarC = "";',
  28926. ' this.VarS = "";',
  28927. ' this.VarD = 0.0;',
  28928. ' this.VarB = false;',
  28929. ' this.VarLW = 0;',
  28930. ' this.VarSmI = 0;',
  28931. ' this.VarW = 0;',
  28932. ' this.VarShI = 0;',
  28933. ' this.VarBy = 0;',
  28934. ' this.ArrA = [];',
  28935. ' this.ArrB = [];',
  28936. ' };',
  28937. ' this.$final = function () {',
  28938. ' this.ArrA = undefined;',
  28939. ' this.ArrB = undefined;',
  28940. ' };',
  28941. ' var $r = this.$rtti;',
  28942. ' $r.addField("VarLI", rtl.longint);',
  28943. ' $r.addField("VarC", rtl.char);',
  28944. ' $r.addField("VarS", rtl.string);',
  28945. ' $r.addField("VarD", rtl.double);',
  28946. ' $r.addField("VarB", rtl.boolean);',
  28947. ' $r.addField("VarLW", rtl.longword);',
  28948. ' $r.addField("VarSmI", rtl.smallint);',
  28949. ' $r.addField("VarW", rtl.word);',
  28950. ' $r.addField("VarShI", rtl.shortint);',
  28951. ' $r.addField("VarBy", rtl.byte);',
  28952. ' $r.addField("VarExt", rtl.longint);',
  28953. ' $r.addField("ArrA", $mod.$rtti["TObject.ArrB$a"]);',
  28954. ' $r.addField("ArrB", $mod.$rtti["TObject.ArrB$a"]);',
  28955. '});',
  28956. 'this.p = null;',
  28957. 'this.Obj = null;',
  28958. '']),
  28959. LinesToStr([ // $mod.$main
  28960. '$mod.p = $mod.$rtti["TObject"];',
  28961. '$mod.p = rtl.pointer;',
  28962. '$mod.p = $mod.Obj.$rtti;',
  28963. '']));
  28964. end;
  28965. procedure TTestModule.TestRTTI_Class_Method;
  28966. begin
  28967. WithTypeInfo:=true;
  28968. StartProgram(false);
  28969. Add('type');
  28970. Add(' TObject = class');
  28971. Add(' private');
  28972. Add(' procedure Internal; external name ''$intern'';');
  28973. Add(' published');
  28974. Add(' procedure Click; virtual; abstract;');
  28975. Add(' procedure Notify(Sender: TObject); virtual; abstract;');
  28976. Add(' function GetNotify: boolean; external name ''GetNotify'';');
  28977. Add(' procedure Println(a,b: longint); varargs; virtual; abstract;');
  28978. Add(' end;');
  28979. Add('begin');
  28980. ConvertProgram;
  28981. CheckSource('TestRTTI_Class_Method',
  28982. LinesToStr([ // statements
  28983. 'rtl.createClass(this, "TObject", null, function () {',
  28984. ' this.$init = function () {',
  28985. ' };',
  28986. ' this.$final = function () {',
  28987. ' };',
  28988. ' var $r = this.$rtti;',
  28989. ' $r.addMethod("Click", 0, null);',
  28990. ' $r.addMethod("Notify", 0, [["Sender", $r]]);',
  28991. ' $r.addMethod("GetNotify", 1, null, rtl.boolean,{flags: 4});',
  28992. ' $r.addMethod("Println", 0, [["a", rtl.longint], ["b", rtl.longint]], null, {',
  28993. ' flags: 2',
  28994. ' });',
  28995. '});',
  28996. '']),
  28997. LinesToStr([ // $mod.$main
  28998. '']));
  28999. end;
  29000. procedure TTestModule.TestRTTI_Class_MethodArgFlags;
  29001. begin
  29002. WithTypeInfo:=true;
  29003. StartProgram(false);
  29004. Add('type');
  29005. Add(' TObject = class');
  29006. Add(' published');
  29007. Add(' procedure OpenArray(const Args: array of string); virtual; abstract;');
  29008. Add(' procedure ByRef(var Value: longint; out Item: longint); virtual; abstract;');
  29009. Add(' procedure Untyped(var Value; out Item); virtual; abstract;');
  29010. Add(' end;');
  29011. Add('begin');
  29012. ConvertProgram;
  29013. CheckSource('TestRTTI_Class_MethodOpenArray',
  29014. LinesToStr([ // statements
  29015. 'rtl.createClass(this, "TObject", null, function () {',
  29016. ' this.$init = function () {',
  29017. ' };',
  29018. ' this.$final = function () {',
  29019. ' };',
  29020. ' var $r = this.$rtti;',
  29021. '$r.addMethod("OpenArray", 0, [["Args", rtl.string, 10]]);',
  29022. '$r.addMethod("ByRef", 0, [["Value", rtl.longint, 1], ["Item", rtl.longint, 4]]);',
  29023. '$r.addMethod("Untyped", 0, [["Value", null, 1], ["Item", null, 4]]);',
  29024. '});',
  29025. '']),
  29026. LinesToStr([ // $mod.$main
  29027. '']));
  29028. end;
  29029. procedure TTestModule.TestRTTI_Class_Property;
  29030. begin
  29031. WithTypeInfo:=true;
  29032. StartProgram(false);
  29033. Add('{$modeswitch externalclass}');
  29034. Add('type');
  29035. Add(' TObject = class');
  29036. Add(' private');
  29037. Add(' FColor: longint;');
  29038. Add(' FColorStored: boolean;');
  29039. Add(' procedure SetColor(Value: longint); virtual; abstract;');
  29040. Add(' function GetColor: longint; virtual; abstract;');
  29041. Add(' function GetColorStored: boolean; virtual; abstract;');
  29042. Add(' FExtSize: longint external name ''$extSize'';');
  29043. Add(' FExtSizeStored: boolean external name ''$extSizeStored'';');
  29044. Add(' procedure SetExtSize(Value: longint); external name ''$setSize'';');
  29045. Add(' function GetExtSize: longint; external name ''$getSize'';');
  29046. Add(' function GetExtSizeStored: boolean; external name ''$getExtSizeStored'';');
  29047. Add(' published');
  29048. Add(' property ColorA: longint read FColor;');
  29049. Add(' property ColorB: longint write FColor;');
  29050. Add(' property ColorC: longint read GetColor write SetColor;');
  29051. Add(' property ColorD: longint read FColor write FColor stored FColorStored;');
  29052. Add(' property ExtSizeA: longint read FExtSize write FExtSize;');
  29053. Add(' property ExtSizeB: longint read GetExtSize write SetExtSize stored FExtSizeStored;');
  29054. Add(' property ExtSizeC: longint read FExtSize write FExtSize stored GetExtSizeStored;');
  29055. Add(' end;');
  29056. Add('begin');
  29057. ConvertProgram;
  29058. CheckSource('TestRTTI_Class_Property',
  29059. LinesToStr([ // statements
  29060. 'rtl.createClass(this, "TObject", null, function () {',
  29061. ' this.$init = function () {',
  29062. ' this.FColor = 0;',
  29063. ' this.FColorStored = false;',
  29064. ' };',
  29065. ' this.$final = function () {',
  29066. ' };',
  29067. ' var $r = this.$rtti;',
  29068. ' $r.addProperty("ColorA", 0, rtl.longint, "FColor", "");',
  29069. ' $r.addProperty("ColorB", 0, rtl.longint, "", "FColor");',
  29070. ' $r.addProperty("ColorC", 3, rtl.longint, "GetColor", "SetColor");',
  29071. ' $r.addProperty(',
  29072. ' "ColorD",',
  29073. ' 8,',
  29074. ' rtl.longint,',
  29075. ' "FColor",',
  29076. ' "FColor",',
  29077. ' {',
  29078. ' stored: "FColorStored"',
  29079. ' }',
  29080. ' );',
  29081. ' $r.addProperty("ExtSizeA", 0, rtl.longint, "$extSize", "$extSize");',
  29082. ' $r.addProperty(',
  29083. ' "ExtSizeB",',
  29084. ' 11,',
  29085. ' rtl.longint,',
  29086. ' "$getSize",',
  29087. ' "$setSize",',
  29088. ' {',
  29089. ' stored: "$extSizeStored"',
  29090. ' }',
  29091. ' );',
  29092. ' $r.addProperty(',
  29093. ' "ExtSizeC",',
  29094. ' 12,',
  29095. ' rtl.longint,',
  29096. ' "$extSize",',
  29097. ' "$extSize",',
  29098. ' {',
  29099. ' stored: "$getExtSizeStored"',
  29100. ' }',
  29101. ' );',
  29102. '});',
  29103. '']),
  29104. LinesToStr([ // $mod.$main
  29105. '']));
  29106. end;
  29107. procedure TTestModule.TestRTTI_Class_PropertyParams;
  29108. begin
  29109. WithTypeInfo:=true;
  29110. StartProgram(false);
  29111. Add('{$modeswitch externalclass}');
  29112. Add('type');
  29113. Add(' integer = longint;');
  29114. Add(' TObject = class');
  29115. Add(' private');
  29116. Add(' function GetItems(i: integer): tobject; virtual; abstract;');
  29117. Add(' procedure SetItems(i: integer; value: tobject); virtual; abstract;');
  29118. Add(' function GetValues(const i: integer; var b: boolean): char; virtual; abstract;');
  29119. Add(' procedure SetValues(const i: integer; var b: boolean; value: char); virtual; abstract;');
  29120. Add(' published');
  29121. Add(' property Items[Index: integer]: tobject read getitems write setitems;');
  29122. Add(' property Values[const keya: integer; var keyb: boolean]: char read getvalues write setvalues;');
  29123. Add(' end;');
  29124. Add('begin');
  29125. ConvertProgram;
  29126. CheckSource('TestRTTI_Class_PropertyParams',
  29127. LinesToStr([ // statements
  29128. 'rtl.createClass(this, "TObject", null, function () {',
  29129. ' this.$init = function () {',
  29130. ' };',
  29131. ' this.$final = function () {',
  29132. ' };',
  29133. ' var $r = this.$rtti;',
  29134. ' $r.addProperty("Items", 3, $r, "GetItems", "SetItems");',
  29135. ' $r.addProperty("Values", 3, rtl.char, "GetValues", "SetValues");',
  29136. '});',
  29137. '']),
  29138. LinesToStr([ // $mod.$main
  29139. '']));
  29140. end;
  29141. procedure TTestModule.TestRTTI_Class_OtherUnit_TypeAlias;
  29142. begin
  29143. WithTypeInfo:=true;
  29144. AddModuleWithIntfImplSrc('unit1.pas',
  29145. 'type TColor = -5..5;',
  29146. '');
  29147. StartProgram(true);
  29148. Add([
  29149. 'uses unit1;',
  29150. 'type',
  29151. ' TColorAlias = TColor;',
  29152. ' TColorTypeAlias = type TColor;',
  29153. ' TObject = class',
  29154. ' private',
  29155. ' fColor: TColor;',
  29156. ' fAlias: TColorAlias;',
  29157. ' fTypeAlias: TColorTypeAlias;',
  29158. ' published',
  29159. ' property Color: TColor read fcolor;',
  29160. ' property Alias: TColorAlias read falias;',
  29161. ' property TypeAlias: TColorTypeAlias read ftypealias;',
  29162. ' end;',
  29163. 'begin',
  29164. '']);
  29165. ConvertProgram;
  29166. CheckSource('TestRTTI_Class_OtherUnit_TypeAlias',
  29167. LinesToStr([ // statements
  29168. 'this.$rtti.$inherited("TColorTypeAlias", pas.unit1.$rtti["TColor"], {});',
  29169. 'rtl.createClass(this, "TObject", null, function () {',
  29170. ' this.$init = function () {',
  29171. ' this.fColor = 0;',
  29172. ' this.fAlias = 0;',
  29173. ' this.fTypeAlias = 0;',
  29174. ' };',
  29175. ' this.$final = function () {',
  29176. ' };',
  29177. ' var $r = this.$rtti;',
  29178. ' $r.addProperty("Color", 0, pas.unit1.$rtti["TColor"], "fColor", "");',
  29179. ' $r.addProperty("Alias", 0, pas.unit1.$rtti["TColor"], "fAlias", "");',
  29180. ' $r.addProperty("TypeAlias", 0, $mod.$rtti["TColorTypeAlias"], "fTypeAlias", "");',
  29181. '});',
  29182. '']),
  29183. LinesToStr([ // $mod.$main
  29184. '']));
  29185. end;
  29186. procedure TTestModule.TestRTTI_Class_OmitRTTI;
  29187. begin
  29188. WithTypeInfo:=true;
  29189. StartProgram(false);
  29190. Add([
  29191. '{$modeswitch omitrtti}',
  29192. 'type',
  29193. ' TObject = class',
  29194. ' private',
  29195. ' FA: byte;',
  29196. ' published',
  29197. ' property A: byte read FA write FA;',
  29198. ' end;',
  29199. 'begin']);
  29200. ConvertProgram;
  29201. CheckSource('TestRTTI_Class_OmitRTTI',
  29202. LinesToStr([ // statements
  29203. 'rtl.createClass(this, "TObject", null, function () {',
  29204. ' this.$init = function () {',
  29205. ' this.FA = 0;',
  29206. ' };',
  29207. ' this.$final = function () {',
  29208. ' };',
  29209. '});',
  29210. '']),
  29211. LinesToStr([ // $mod.$main
  29212. '']));
  29213. end;
  29214. procedure TTestModule.TestRTTI_IndexModifier;
  29215. begin
  29216. WithTypeInfo:=true;
  29217. StartProgram(false);
  29218. Add([
  29219. 'type',
  29220. ' TEnum = (red, blue);',
  29221. ' TObject = class',
  29222. ' FB: boolean;',
  29223. ' procedure SetIntBool(Index: longint; b: boolean); virtual; abstract;',
  29224. ' function GetBoolBool(Index: boolean): boolean; virtual; abstract;',
  29225. ' procedure SetBoolBool(Index: boolean; b: boolean); virtual; abstract;',
  29226. ' function GetEnumBool(Index: TEnum): boolean; virtual; abstract;',
  29227. ' function GetStrIntBool(A: String; I: longint): boolean; virtual; abstract;',
  29228. ' procedure SetStrIntBool(A: String; I: longint; b: boolean); virtual; abstract;',
  29229. ' published',
  29230. ' property B1: boolean index 1 read FB write SetIntBool;',
  29231. ' property B2: boolean index TEnum.blue read GetEnumBool write FB;',
  29232. ' property I1[A: String]: boolean index 2 read GetStrIntBool write SetStrIntBool;',
  29233. ' end;',
  29234. 'begin']);
  29235. ConvertProgram;
  29236. CheckSource('TestRTTI_IndexModifier',
  29237. LinesToStr([ // statements
  29238. 'this.TEnum = {',
  29239. ' "0": "red",',
  29240. ' red: 0,',
  29241. ' "1": "blue",',
  29242. ' blue: 1',
  29243. '};',
  29244. 'this.$rtti.$Enum("TEnum", {',
  29245. ' minvalue: 0,',
  29246. ' maxvalue: 1,',
  29247. ' ordtype: 1,',
  29248. ' enumtype: this.TEnum',
  29249. '});',
  29250. 'rtl.createClass(this, "TObject", null, function () {',
  29251. ' this.$init = function () {',
  29252. ' this.FB = false;',
  29253. ' };',
  29254. ' this.$final = function () {',
  29255. ' };',
  29256. ' var $r = this.$rtti;',
  29257. ' $r.addProperty(',
  29258. ' "B1",',
  29259. ' 18,',
  29260. ' rtl.boolean,',
  29261. ' "FB",',
  29262. ' "SetIntBool",',
  29263. ' {',
  29264. ' index: 1',
  29265. ' }',
  29266. ' );',
  29267. ' $r.addProperty(',
  29268. ' "B2",',
  29269. ' 17,',
  29270. ' rtl.boolean,',
  29271. ' "GetEnumBool",',
  29272. ' "FB",',
  29273. ' {',
  29274. ' index: $mod.TEnum.blue',
  29275. ' }',
  29276. ' );',
  29277. ' $r.addProperty(',
  29278. ' "I1",',
  29279. ' 19,',
  29280. ' rtl.boolean,',
  29281. ' "GetStrIntBool",',
  29282. ' "SetStrIntBool",',
  29283. ' {',
  29284. ' index: 2',
  29285. ' }',
  29286. ' );',
  29287. '});',
  29288. '']),
  29289. LinesToStr([ // $mod.$main
  29290. '']));
  29291. end;
  29292. procedure TTestModule.TestRTTI_StoredModifier;
  29293. begin
  29294. WithTypeInfo:=true;
  29295. StartProgram(false);
  29296. Add([
  29297. 'const',
  29298. ' ConstB = true;',
  29299. 'type',
  29300. ' TObject = class',
  29301. ' private',
  29302. ' FB: boolean;',
  29303. ' function IsBStored: boolean; virtual; abstract;',
  29304. ' published',
  29305. ' property BoolA: boolean read FB stored true;',
  29306. ' property BoolB: boolean read FB stored false;',
  29307. ' property BoolC: boolean read FB stored FB;',
  29308. ' property BoolD: boolean read FB stored ConstB;',
  29309. ' property BoolE: boolean read FB stored IsBStored;',
  29310. ' end;',
  29311. 'begin']);
  29312. ConvertProgram;
  29313. CheckSource('TestRTTI_StoredModifier',
  29314. LinesToStr([ // statements
  29315. 'this.ConstB = true;',
  29316. 'rtl.createClass(this, "TObject", null, function () {',
  29317. ' this.$init = function () {',
  29318. ' this.FB = false;',
  29319. ' };',
  29320. ' this.$final = function () {',
  29321. ' };',
  29322. ' var $r = this.$rtti;',
  29323. ' $r.addProperty("BoolA", 0, rtl.boolean, "FB", "");',
  29324. ' $r.addProperty("BoolB", 4, rtl.boolean, "FB", "");',
  29325. ' $r.addProperty(',
  29326. ' "BoolC",',
  29327. ' 8,',
  29328. ' rtl.boolean,',
  29329. ' "FB",',
  29330. ' "",',
  29331. ' {',
  29332. ' stored: "FB"',
  29333. ' }',
  29334. ' );',
  29335. ' $r.addProperty("BoolD", 0, rtl.boolean, "FB", "");',
  29336. ' $r.addProperty(',
  29337. ' "BoolE",',
  29338. ' 12,',
  29339. ' rtl.boolean,',
  29340. ' "FB",',
  29341. ' "",',
  29342. ' {',
  29343. ' stored: "IsBStored"',
  29344. ' }',
  29345. ' );',
  29346. '});',
  29347. '']),
  29348. LinesToStr([ // $mod.$main
  29349. '']));
  29350. end;
  29351. procedure TTestModule.TestRTTI_DefaultValue;
  29352. begin
  29353. WithTypeInfo:=true;
  29354. StartProgram(false);
  29355. Add([
  29356. 'type',
  29357. ' TEnum = (red, blue);',
  29358. 'const',
  29359. ' CB = true or false;',
  29360. ' CI = 1+2;',
  29361. 'type',
  29362. ' TObject = class',
  29363. ' FB: boolean;',
  29364. ' FI: longint;',
  29365. ' FE: TEnum;',
  29366. ' published',
  29367. ' property B1: boolean read FB default true;',
  29368. ' property B2: boolean read FB default CB;',
  29369. ' property B3: boolean read FB default test1.cb;',
  29370. ' property I1: longint read FI default 2;',
  29371. ' property I2: longint read FI default CI;',
  29372. ' property E1: TEnum read FE default red;',
  29373. ' property E2: TEnum read FE default TEnum.blue;',
  29374. ' end;',
  29375. 'begin']);
  29376. ConvertProgram;
  29377. CheckSource('TestRTTI_DefaultValue',
  29378. LinesToStr([ // statements
  29379. 'this.TEnum = {',
  29380. ' "0": "red",',
  29381. ' red: 0,',
  29382. ' "1": "blue",',
  29383. ' blue: 1',
  29384. '};',
  29385. 'this.$rtti.$Enum("TEnum", {',
  29386. ' minvalue: 0,',
  29387. ' maxvalue: 1,',
  29388. ' ordtype: 1,',
  29389. ' enumtype: this.TEnum',
  29390. '});',
  29391. 'this.CB = true || false;',
  29392. 'this.CI = 1 + 2;',
  29393. 'rtl.createClass(this, "TObject", null, function () {',
  29394. ' this.$init = function () {',
  29395. ' this.FB = false;',
  29396. ' this.FI = 0;',
  29397. ' this.FE = 0;',
  29398. ' };',
  29399. ' this.$final = function () {',
  29400. ' };',
  29401. ' var $r = this.$rtti;',
  29402. ' $r.addProperty(',
  29403. ' "B1",',
  29404. ' 0,',
  29405. ' rtl.boolean,',
  29406. ' "FB",',
  29407. ' "",',
  29408. ' {',
  29409. ' Default: true',
  29410. ' }',
  29411. ' );',
  29412. ' $r.addProperty(',
  29413. ' "B2",',
  29414. ' 0,',
  29415. ' rtl.boolean,',
  29416. ' "FB",',
  29417. ' "",',
  29418. ' {',
  29419. ' Default: true',
  29420. ' }',
  29421. ' );',
  29422. ' $r.addProperty(',
  29423. ' "B3",',
  29424. ' 0,',
  29425. ' rtl.boolean,',
  29426. ' "FB",',
  29427. ' "",',
  29428. ' {',
  29429. ' Default: true',
  29430. ' }',
  29431. ' );',
  29432. ' $r.addProperty(',
  29433. ' "I1",',
  29434. ' 0,',
  29435. ' rtl.longint,',
  29436. ' "FI",',
  29437. ' "",',
  29438. ' {',
  29439. ' Default: 2',
  29440. ' }',
  29441. ' );',
  29442. ' $r.addProperty(',
  29443. ' "I2",',
  29444. ' 0,',
  29445. ' rtl.longint,',
  29446. ' "FI",',
  29447. ' "",',
  29448. ' {',
  29449. ' Default: 3',
  29450. ' }',
  29451. ' );',
  29452. ' $r.addProperty(',
  29453. ' "E1",',
  29454. ' 0,',
  29455. ' $mod.$rtti["TEnum"],',
  29456. ' "FE",',
  29457. ' "",',
  29458. ' {',
  29459. ' Default: $mod.TEnum.red',
  29460. ' }',
  29461. ' );',
  29462. ' $r.addProperty(',
  29463. ' "E2",',
  29464. ' 0,',
  29465. ' $mod.$rtti["TEnum"],',
  29466. ' "FE",',
  29467. ' "",',
  29468. ' {',
  29469. ' Default: $mod.TEnum.blue',
  29470. ' }',
  29471. ' );',
  29472. '});',
  29473. '']),
  29474. LinesToStr([ // $mod.$main
  29475. '']));
  29476. end;
  29477. procedure TTestModule.TestRTTI_DefaultValueSet;
  29478. begin
  29479. WithTypeInfo:=true;
  29480. StartProgram(false);
  29481. Add([
  29482. 'type',
  29483. ' TEnum = (red, blue);',
  29484. ' TSet = set of TEnum;',
  29485. 'const',
  29486. ' CSet = [red,blue];',
  29487. 'type',
  29488. ' TObject = class',
  29489. ' FSet: TSet;',
  29490. ' published',
  29491. ' property Set1: TSet read FSet default [];',
  29492. ' property Set2: TSet read FSet default [red];',
  29493. ' property Set3: TSet read FSet default [red,blue];',
  29494. ' property Set4: TSet read FSet default CSet;',
  29495. ' end;',
  29496. 'begin']);
  29497. ConvertProgram;
  29498. CheckSource('TestRTTI_DefaultValueSet',
  29499. LinesToStr([ // statements
  29500. 'this.TEnum = {',
  29501. ' "0": "red",',
  29502. ' red: 0,',
  29503. ' "1": "blue",',
  29504. ' blue: 1',
  29505. '};',
  29506. 'this.$rtti.$Enum("TEnum", {',
  29507. ' minvalue: 0,',
  29508. ' maxvalue: 1,',
  29509. ' ordtype: 1,',
  29510. ' enumtype: this.TEnum',
  29511. '});',
  29512. 'this.$rtti.$Set("TSet", {',
  29513. ' comptype: this.$rtti["TEnum"]',
  29514. '});',
  29515. 'this.CSet = rtl.createSet(this.TEnum.red, this.TEnum.blue);',
  29516. 'rtl.createClass(this, "TObject", null, function () {',
  29517. ' this.$init = function () {',
  29518. ' this.FSet = {};',
  29519. ' };',
  29520. ' this.$final = function () {',
  29521. ' this.FSet = undefined;',
  29522. ' };',
  29523. ' var $r = this.$rtti;',
  29524. ' $r.addProperty(',
  29525. ' "Set1",',
  29526. ' 0,',
  29527. ' $mod.$rtti["TSet"],',
  29528. ' "FSet",',
  29529. ' "",',
  29530. ' {',
  29531. ' Default: {}',
  29532. ' }',
  29533. ' );',
  29534. ' $r.addProperty(',
  29535. ' "Set2",',
  29536. ' 0,',
  29537. ' $mod.$rtti["TSet"],',
  29538. ' "FSet",',
  29539. ' "",',
  29540. ' {',
  29541. ' Default: rtl.createSet($mod.TEnum.red)',
  29542. ' }',
  29543. ' );',
  29544. ' $r.addProperty(',
  29545. ' "Set3",',
  29546. ' 0,',
  29547. ' $mod.$rtti["TSet"],',
  29548. ' "FSet",',
  29549. ' "",',
  29550. ' {',
  29551. ' Default: rtl.createSet($mod.TEnum.red, $mod.TEnum.blue)',
  29552. ' }',
  29553. ' );',
  29554. ' $r.addProperty(',
  29555. ' "Set4",',
  29556. ' 0,',
  29557. ' $mod.$rtti["TSet"],',
  29558. ' "FSet",',
  29559. ' "",',
  29560. ' {',
  29561. ' Default: $mod.CSet',
  29562. ' }',
  29563. ' );',
  29564. '});',
  29565. '']),
  29566. LinesToStr([ // $mod.$main
  29567. '']));
  29568. end;
  29569. procedure TTestModule.TestRTTI_DefaultValueRangeType;
  29570. begin
  29571. WithTypeInfo:=true;
  29572. StartProgram(false);
  29573. Add([
  29574. 'type',
  29575. ' TRg = -1..1;',
  29576. 'const',
  29577. ' l = low(TRg);',
  29578. ' h = high(TRg);',
  29579. 'type',
  29580. ' TObject = class',
  29581. ' FV: TRg;',
  29582. ' published',
  29583. ' property V1: TRg read FV default -1;',
  29584. ' end;',
  29585. 'begin']);
  29586. ConvertProgram;
  29587. CheckSource('TestRTTI_DefaultValueRangeType',
  29588. LinesToStr([ // statements
  29589. 'this.$rtti.$Int("TRg", {',
  29590. ' minvalue: -1,',
  29591. ' maxvalue: 1,',
  29592. ' ordtype: 0',
  29593. '});',
  29594. 'this.l = -1;',
  29595. 'this.h = 1;',
  29596. 'rtl.createClass(this, "TObject", null, function () {',
  29597. ' this.$init = function () {',
  29598. ' this.FV = 0;',
  29599. ' };',
  29600. ' this.$final = function () {',
  29601. ' };',
  29602. ' var $r = this.$rtti;',
  29603. ' $r.addProperty(',
  29604. ' "V1",',
  29605. ' 0,',
  29606. ' $mod.$rtti["TRg"],',
  29607. ' "FV",',
  29608. ' "",',
  29609. ' {',
  29610. ' Default: -1',
  29611. ' }',
  29612. ' );',
  29613. '});',
  29614. '']),
  29615. LinesToStr([ // $mod.$main
  29616. '']));
  29617. end;
  29618. procedure TTestModule.TestRTTI_DefaultValueInherit;
  29619. begin
  29620. WithTypeInfo:=true;
  29621. StartProgram(false);
  29622. Add([
  29623. 'type',
  29624. ' TObject = class',
  29625. ' FA, FB: byte;',
  29626. ' property A: byte read FA default 1;',
  29627. ' property B: byte read FB default 2;',
  29628. ' end;',
  29629. ' TBird = class',
  29630. ' published',
  29631. ' property A;',
  29632. ' property B nodefault;',
  29633. ' end;',
  29634. 'begin']);
  29635. ConvertProgram;
  29636. CheckSource('TestRTTI_DefaultValueInherit',
  29637. LinesToStr([ // statements
  29638. 'rtl.createClass(this, "TObject", null, function () {',
  29639. ' this.$init = function () {',
  29640. ' this.FA = 0;',
  29641. ' this.FB = 0;',
  29642. ' };',
  29643. ' this.$final = function () {',
  29644. ' };',
  29645. '});',
  29646. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  29647. ' var $r = this.$rtti;',
  29648. ' $r.addProperty(',
  29649. ' "A",',
  29650. ' 0,',
  29651. ' rtl.byte,',
  29652. ' "FA",',
  29653. ' "",',
  29654. ' {',
  29655. ' Default: 1',
  29656. ' }',
  29657. ' );',
  29658. ' $r.addProperty("B", 0, rtl.byte, "FB", "");',
  29659. '});',
  29660. '']),
  29661. LinesToStr([ // $mod.$main
  29662. '']));
  29663. end;
  29664. procedure TTestModule.TestRTTI_OverrideMethod;
  29665. begin
  29666. WithTypeInfo:=true;
  29667. StartProgram(false);
  29668. Add('type');
  29669. Add(' TObject = class');
  29670. Add(' published');
  29671. Add(' procedure DoIt; virtual; abstract;');
  29672. Add(' end;');
  29673. Add(' TSky = class');
  29674. Add(' published');
  29675. Add(' procedure DoIt; override;');
  29676. Add(' end;');
  29677. Add('procedure TSky.DoIt; begin end;');
  29678. Add('begin');
  29679. ConvertProgram;
  29680. CheckSource('TestRTTI_OverrideMethod',
  29681. LinesToStr([ // statements
  29682. 'rtl.createClass(this, "TObject", null, function () {',
  29683. ' this.$init = function () {',
  29684. ' };',
  29685. ' this.$final = function () {',
  29686. ' };',
  29687. ' var $r = this.$rtti;',
  29688. ' $r.addMethod("DoIt", 0, null);',
  29689. '});',
  29690. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  29691. ' this.DoIt = function () {',
  29692. ' };',
  29693. '});',
  29694. '']),
  29695. LinesToStr([ // $mod.$main
  29696. '']));
  29697. end;
  29698. procedure TTestModule.TestRTTI_ReintroduceMethod;
  29699. begin
  29700. WithTypeInfo:=true;
  29701. StartProgram(false);
  29702. Add([
  29703. 'type',
  29704. ' TObject = class',
  29705. ' published',
  29706. ' procedure DoIt;',
  29707. ' end;',
  29708. ' TSky = class',
  29709. ' published',
  29710. ' procedure DoIt; reintroduce;',
  29711. ' end;',
  29712. 'procedure TObject.DoIt; begin end;',
  29713. 'procedure TSky.DoIt;',
  29714. 'begin',
  29715. ' inherited DoIt;',
  29716. 'end;',
  29717. 'begin']);
  29718. ConvertProgram;
  29719. CheckSource('TestRTTI_ReintroduceMethod',
  29720. LinesToStr([ // statements
  29721. 'rtl.createClass(this, "TObject", null, function () {',
  29722. ' this.$init = function () {',
  29723. ' };',
  29724. ' this.$final = function () {',
  29725. ' };',
  29726. ' this.DoIt = function () {',
  29727. ' };',
  29728. ' var $r = this.$rtti;',
  29729. ' $r.addMethod("DoIt", 0, null);',
  29730. '});',
  29731. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  29732. ' this.DoIt = function () {',
  29733. ' $mod.TObject.DoIt.call(this);',
  29734. ' };',
  29735. ' var $r = this.$rtti;',
  29736. ' $r.addMethod("DoIt", 0, null);',
  29737. '});',
  29738. '']),
  29739. LinesToStr([ // $mod.$main
  29740. '']));
  29741. end;
  29742. procedure TTestModule.TestRTTI_OverloadProperty;
  29743. begin
  29744. WithTypeInfo:=true;
  29745. StartProgram(false);
  29746. Add('type');
  29747. Add(' TObject = class');
  29748. Add(' protected');
  29749. Add(' FFlag: longint;');
  29750. Add(' published');
  29751. Add(' property Flag: longint read fflag;');
  29752. Add(' end;');
  29753. Add(' TSky = class');
  29754. Add(' published');
  29755. Add(' property FLAG: longint write fflag;');
  29756. Add(' end;');
  29757. Add('begin');
  29758. ConvertProgram;
  29759. CheckSource('TestRTTI_OverrideMethod',
  29760. LinesToStr([ // statements
  29761. 'rtl.createClass(this, "TObject", null, function () {',
  29762. ' this.$init = function () {',
  29763. ' this.FFlag = 0;',
  29764. ' };',
  29765. ' this.$final = function () {',
  29766. ' };',
  29767. ' var $r = this.$rtti;',
  29768. ' $r.addProperty("Flag", 0, rtl.longint, "FFlag", "");',
  29769. '});',
  29770. 'rtl.createClass(this, "TSky", this.TObject, function () {',
  29771. ' var $r = this.$rtti;',
  29772. ' $r.addProperty("Flag", 0, rtl.longint, "", "FFlag");',
  29773. '});',
  29774. '']),
  29775. LinesToStr([ // $mod.$main
  29776. '']));
  29777. end;
  29778. procedure TTestModule.TestRTTI_ClassForward;
  29779. begin
  29780. WithTypeInfo:=true;
  29781. StartProgram(false);
  29782. Add('type');
  29783. Add(' TObject = class end;');
  29784. Add(' tbridge = class;');
  29785. Add(' TProc = function: tbridge;');
  29786. Add(' TOger = class');
  29787. Add(' published');
  29788. Add(' FBridge: tbridge;');
  29789. Add(' procedure SetBridge(Value: tbridge); virtual; abstract;');
  29790. Add(' property Bridge: tbridge read fbridge write setbridge;');
  29791. Add(' end;');
  29792. Add(' TBridge = class');
  29793. Add(' FOger: toger;');
  29794. Add(' end;');
  29795. Add('var p: Pointer;');
  29796. Add(' b: tbridge;');
  29797. Add('begin');
  29798. Add(' p:=typeinfo(tbridge);');
  29799. Add(' p:=typeinfo(b);');
  29800. ConvertProgram;
  29801. CheckSource('TestRTTI_ClassForward',
  29802. LinesToStr([ // statements
  29803. 'rtl.createClass(this, "TObject", null, function () {',
  29804. ' this.$init = function () {',
  29805. ' };',
  29806. ' this.$final = function () {',
  29807. ' };',
  29808. '});',
  29809. 'this.$rtti.$Class("TBridge");',
  29810. 'this.$rtti.$ProcVar("TProc", {',
  29811. ' procsig: rtl.newTIProcSig(null, this.$rtti["TBridge"])',
  29812. '});',
  29813. 'rtl.createClass(this, "TOger", this.TObject, function () {',
  29814. ' this.$init = function () {',
  29815. ' $mod.TObject.$init.call(this);',
  29816. ' this.FBridge = null;',
  29817. ' };',
  29818. ' this.$final = function () {',
  29819. ' this.FBridge = undefined;',
  29820. ' $mod.TObject.$final.call(this);',
  29821. ' };',
  29822. ' var $r = this.$rtti;',
  29823. ' $r.addField("FBridge", $mod.$rtti["TBridge"]);',
  29824. ' $r.addMethod("SetBridge", 0, [["Value", $mod.$rtti["TBridge"]]]);',
  29825. ' $r.addProperty("Bridge", 2, $mod.$rtti["TBridge"], "FBridge", "SetBridge");',
  29826. '});',
  29827. 'rtl.createClass(this, "TBridge", this.TObject, function () {',
  29828. ' this.$init = function () {',
  29829. ' $mod.TObject.$init.call(this);',
  29830. ' this.FOger = null;',
  29831. ' };',
  29832. ' this.$final = function () {',
  29833. ' this.FOger = undefined;',
  29834. ' $mod.TObject.$final.call(this);',
  29835. ' };',
  29836. '});',
  29837. 'this.p = null;',
  29838. 'this.b = null;',
  29839. '']),
  29840. LinesToStr([ // $mod.$main
  29841. '$mod.p = $mod.$rtti["TBridge"];',
  29842. '$mod.p = $mod.b.$rtti;',
  29843. '']));
  29844. end;
  29845. procedure TTestModule.TestRTTI_ClassOf;
  29846. begin
  29847. WithTypeInfo:=true;
  29848. StartProgram(false);
  29849. Add('type');
  29850. Add(' TClass = class of tobject;');
  29851. Add(' TProcA = function: TClass;');
  29852. Add(' TObject = class');
  29853. Add(' published');
  29854. Add(' C: tclass;');
  29855. Add(' end;');
  29856. Add(' tfox = class;');
  29857. Add(' TBird = class end;');
  29858. Add(' TBirds = class of tbird;');
  29859. Add(' TFox = class end;');
  29860. Add(' TFoxes = class of tfox;');
  29861. Add(' TCows = class of TCow;');
  29862. Add(' TCow = class;');
  29863. Add(' TCow = class end;');
  29864. Add('begin');
  29865. ConvertProgram;
  29866. CheckSource('TestRTTI_ClassOf',
  29867. LinesToStr([ // statements
  29868. 'this.$rtti.$Class("TObject");',
  29869. 'this.$rtti.$ClassRef("TClass", {',
  29870. ' instancetype: this.$rtti["TObject"]',
  29871. '});',
  29872. 'this.$rtti.$ProcVar("TProcA", {',
  29873. ' procsig: rtl.newTIProcSig(null, this.$rtti["TClass"])',
  29874. '});',
  29875. 'rtl.createClass(this, "TObject", null, function () {',
  29876. ' this.$init = function () {',
  29877. ' this.C = null;',
  29878. ' };',
  29879. ' this.$final = function () {',
  29880. ' this.C = undefined;',
  29881. ' };',
  29882. ' var $r = this.$rtti;',
  29883. ' $r.addField("C", $mod.$rtti["TClass"]);',
  29884. '});',
  29885. 'this.$rtti.$Class("TFox");',
  29886. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  29887. '});',
  29888. 'this.$rtti.$ClassRef("TBirds", {',
  29889. ' instancetype: this.$rtti["TBird"]',
  29890. '});',
  29891. 'rtl.createClass(this, "TFox", this.TObject, function () {',
  29892. '});',
  29893. 'this.$rtti.$ClassRef("TFoxes", {',
  29894. ' instancetype: this.$rtti["TFox"]',
  29895. '});',
  29896. 'this.$rtti.$Class("TCow");',
  29897. 'this.$rtti.$ClassRef("TCows", {',
  29898. ' instancetype: this.$rtti["TCow"]',
  29899. '});',
  29900. 'rtl.createClass(this, "TCow", this.TObject, function () {',
  29901. '});',
  29902. '']),
  29903. LinesToStr([ // $mod.$main
  29904. '']));
  29905. end;
  29906. procedure TTestModule.TestRTTI_Record;
  29907. begin
  29908. WithTypeInfo:=true;
  29909. StartProgram(false);
  29910. Add('type');
  29911. Add(' integer = longint;');
  29912. Add(' TPoint = record');
  29913. Add(' x,y: integer;');
  29914. Add(' end;');
  29915. Add('var p: pointer;');
  29916. Add(' r: tpoint;');
  29917. Add('begin');
  29918. Add(' p:=typeinfo(tpoint);');
  29919. Add(' p:=typeinfo(r);');
  29920. Add(' p:=typeinfo(r.x);');
  29921. ConvertProgram;
  29922. CheckSource('TestRTTI_Record',
  29923. LinesToStr([ // statements
  29924. 'rtl.recNewT(this, "TPoint", function () {',
  29925. ' this.x = 0;',
  29926. ' this.y = 0;',
  29927. ' this.$eq = function (b) {',
  29928. ' return (this.x === b.x) && (this.y === b.y);',
  29929. ' };',
  29930. ' this.$assign = function (s) {',
  29931. ' this.x = s.x;',
  29932. ' this.y = s.y;',
  29933. ' return this;',
  29934. ' };',
  29935. ' var $r = $mod.$rtti.$Record("TPoint", {});',
  29936. ' $r.addField("x", rtl.longint);',
  29937. ' $r.addField("y", rtl.longint);',
  29938. '});',
  29939. 'this.p = null;',
  29940. 'this.r = this.TPoint.$new();',
  29941. '']),
  29942. LinesToStr([ // $mod.$main
  29943. '$mod.p = $mod.$rtti["TPoint"];',
  29944. '$mod.p = $mod.$rtti["TPoint"];',
  29945. '$mod.p = rtl.longint;',
  29946. '']));
  29947. end;
  29948. procedure TTestModule.TestRTTI_RecordAnonymousArray;
  29949. begin
  29950. WithTypeInfo:=true;
  29951. StartProgram(false);
  29952. Add('type');
  29953. Add(' TFloatRec = record');
  29954. Add(' c,d: array of char;');
  29955. // Add(' i: array of array of longint;');
  29956. Add(' end;');
  29957. Add('var p: pointer;');
  29958. Add(' r: tfloatrec;');
  29959. Add('begin');
  29960. Add(' p:=typeinfo(tfloatrec);');
  29961. Add(' p:=typeinfo(r);');
  29962. Add(' p:=typeinfo(r.d);');
  29963. ConvertProgram;
  29964. CheckSource('TestRTTI_Record',
  29965. LinesToStr([ // statements
  29966. 'rtl.recNewT(this, "TFloatRec", function () {',
  29967. ' $mod.$rtti.$DynArray("TFloatRec.d$a", {',
  29968. ' eltype: rtl.char',
  29969. ' });',
  29970. ' this.$new = function () {',
  29971. ' var r = Object.create(this);',
  29972. ' r.c = [];',
  29973. ' r.d = [];',
  29974. ' return r;',
  29975. ' };',
  29976. ' this.$eq = function (b) {',
  29977. ' return (this.c === b.c) && (this.d === b.d);',
  29978. ' };',
  29979. ' this.$assign = function (s) {',
  29980. ' this.c = rtl.arrayRef(s.c);',
  29981. ' this.d = rtl.arrayRef(s.d);',
  29982. ' return this;',
  29983. ' };',
  29984. ' var $r = $mod.$rtti.$Record("TFloatRec", {});',
  29985. ' $r.addField("c", $mod.$rtti["TFloatRec.d$a"]);',
  29986. ' $r.addField("d", $mod.$rtti["TFloatRec.d$a"]);',
  29987. '});',
  29988. 'this.p = null;',
  29989. 'this.r = this.TFloatRec.$new();',
  29990. '']),
  29991. LinesToStr([ // $mod.$main
  29992. '$mod.p = $mod.$rtti["TFloatRec"];',
  29993. '$mod.p = $mod.$rtti["TFloatRec"];',
  29994. '$mod.p = $mod.$rtti["TFloatRec.d$a"];',
  29995. '']));
  29996. end;
  29997. procedure TTestModule.TestRTTI_Record_ClassVarType;
  29998. begin
  29999. WithTypeInfo:=true;
  30000. StartProgram(false);
  30001. Add([
  30002. '{$modeswitch AdvancedRecords}',
  30003. 'type',
  30004. ' TPoint = record',
  30005. ' type TProc = procedure(w: word);',
  30006. ' class var p: TProc;',
  30007. ' end;',
  30008. 'begin',
  30009. '']);
  30010. ConvertProgram;
  30011. CheckSource('TestRTTI_Record_ClassVarType',
  30012. LinesToStr([ // statements
  30013. 'rtl.recNewT(this, "TPoint", function () {',
  30014. ' $mod.$rtti.$ProcVar("TPoint.TProc", {',
  30015. ' procsig: rtl.newTIProcSig([["w", rtl.word]])',
  30016. ' });',
  30017. ' this.p = null;',
  30018. ' this.$eq = function (b) {',
  30019. ' return true;',
  30020. ' };',
  30021. ' this.$assign = function (s) {',
  30022. ' return this;',
  30023. ' };',
  30024. ' var $r = $mod.$rtti.$Record("TPoint", {});',
  30025. ' $r.addField("p", $mod.$rtti["TPoint.TProc"]);',
  30026. '}, true);',
  30027. '']),
  30028. LinesToStr([ // $mod.$main
  30029. '']));
  30030. end;
  30031. procedure TTestModule.TestRTTI_LocalTypes;
  30032. begin
  30033. WithTypeInfo:=true;
  30034. StartProgram(false);
  30035. Add([
  30036. 'procedure DoIt;',
  30037. 'type',
  30038. ' integer = longint;',
  30039. ' TPoint = record',
  30040. ' x,y: integer;',
  30041. ' end;',
  30042. 'var p: TPoint;',
  30043. 'begin',
  30044. 'end;',
  30045. 'begin']);
  30046. ConvertProgram;
  30047. CheckSource('TestRTTI_LocalTypes',
  30048. LinesToStr([ // statements
  30049. 'var TPoint = rtl.recNewT(null, "", function () {',
  30050. ' this.x = 0;',
  30051. ' this.y = 0;',
  30052. ' this.$eq = function (b) {',
  30053. ' return (this.x === b.x) && (this.y === b.y);',
  30054. ' };',
  30055. ' this.$assign = function (s) {',
  30056. ' this.x = s.x;',
  30057. ' this.y = s.y;',
  30058. ' return this;',
  30059. ' };',
  30060. '});',
  30061. 'this.DoIt = function () {',
  30062. ' var p = TPoint.$new();',
  30063. '};',
  30064. '']),
  30065. LinesToStr([ // $mod.$main
  30066. '']));
  30067. end;
  30068. procedure TTestModule.TestRTTI_TypeInfo_BaseTypes;
  30069. begin
  30070. WithTypeInfo:=true;
  30071. StartProgram(false);
  30072. Add([
  30073. 'type',
  30074. ' TCaption = string;',
  30075. ' TYesNo = boolean;',
  30076. ' TLetter = char;',
  30077. ' TFloat = double;',
  30078. ' TPtr = pointer;',
  30079. ' TShortInt = shortint;',
  30080. ' TByte = byte;',
  30081. ' TSmallInt = smallint;',
  30082. ' TWord = word;',
  30083. ' TInt32 = longint;',
  30084. ' TDWord = longword;',
  30085. ' TValue = jsvalue;',
  30086. 'var p: TPtr;',
  30087. 'begin',
  30088. ' p:=typeinfo(string);',
  30089. ' p:=typeinfo(tcaption);',
  30090. ' p:=typeinfo(boolean);',
  30091. ' p:=typeinfo(tyesno);',
  30092. ' p:=typeinfo(char);',
  30093. ' p:=typeinfo(tletter);',
  30094. ' p:=typeinfo(double);',
  30095. ' p:=typeinfo(tfloat);',
  30096. ' p:=typeinfo(pointer);',
  30097. ' p:=typeinfo(tptr);',
  30098. ' p:=typeinfo(shortint);',
  30099. ' p:=typeinfo(tshortint);',
  30100. ' p:=typeinfo(byte);',
  30101. ' p:=typeinfo(tbyte);',
  30102. ' p:=typeinfo(smallint);',
  30103. ' p:=typeinfo(tsmallint);',
  30104. ' p:=typeinfo(word);',
  30105. ' p:=typeinfo(tword);',
  30106. ' p:=typeinfo(longword);',
  30107. ' p:=typeinfo(tdword);',
  30108. ' p:=typeinfo(jsvalue);',
  30109. ' p:=typeinfo(tvalue);',
  30110. '']);
  30111. ConvertProgram;
  30112. CheckSource('TestRTTI_TypeInfo_BaseTypes',
  30113. LinesToStr([ // statements
  30114. 'this.p = null;',
  30115. '']),
  30116. LinesToStr([ // $mod.$main
  30117. '$mod.p = rtl.string;',
  30118. '$mod.p = rtl.string;',
  30119. '$mod.p = rtl.boolean;',
  30120. '$mod.p = rtl.boolean;',
  30121. '$mod.p = rtl.char;',
  30122. '$mod.p = rtl.char;',
  30123. '$mod.p = rtl.double;',
  30124. '$mod.p = rtl.double;',
  30125. '$mod.p = rtl.pointer;',
  30126. '$mod.p = rtl.pointer;',
  30127. '$mod.p = rtl.shortint;',
  30128. '$mod.p = rtl.shortint;',
  30129. '$mod.p = rtl.byte;',
  30130. '$mod.p = rtl.byte;',
  30131. '$mod.p = rtl.smallint;',
  30132. '$mod.p = rtl.smallint;',
  30133. '$mod.p = rtl.word;',
  30134. '$mod.p = rtl.word;',
  30135. '$mod.p = rtl.longword;',
  30136. '$mod.p = rtl.longword;',
  30137. '$mod.p = rtl.jsvalue;',
  30138. '$mod.p = rtl.jsvalue;',
  30139. '']));
  30140. end;
  30141. procedure TTestModule.TestRTTI_TypeInfo_Type_BaseTypes;
  30142. begin
  30143. WithTypeInfo:=true;
  30144. StartProgram(false);
  30145. Add([
  30146. 'type',
  30147. ' TCaption = type string;',
  30148. ' TYesNo = type boolean;',
  30149. ' TLetter = type char;',
  30150. ' TFloat = type double;',
  30151. ' TPtr = type pointer;',
  30152. ' TShortInt = type shortint;',
  30153. ' TByte = type byte;',
  30154. ' TSmallInt = type smallint;',
  30155. ' TWord = type word;',
  30156. ' TInt32 = type longint;',
  30157. ' TDWord = type longword;',
  30158. ' TValue = type jsvalue;',
  30159. ' TAliasValue = type TValue;',
  30160. 'var',
  30161. ' p: TPtr;',
  30162. ' a: TAliasValue;',
  30163. 'begin',
  30164. ' p:=typeinfo(tcaption);',
  30165. ' p:=typeinfo(tyesno);',
  30166. ' p:=typeinfo(tletter);',
  30167. ' p:=typeinfo(tfloat);',
  30168. ' p:=typeinfo(tptr);',
  30169. ' p:=typeinfo(tshortint);',
  30170. ' p:=typeinfo(tbyte);',
  30171. ' p:=typeinfo(tsmallint);',
  30172. ' p:=typeinfo(tword);',
  30173. ' p:=typeinfo(tdword);',
  30174. ' p:=typeinfo(tvalue);',
  30175. ' p:=typeinfo(taliasvalue);',
  30176. ' p:=typeinfo(a);',
  30177. '']);
  30178. ConvertProgram;
  30179. CheckSource('TestRTTI_TypeInfo_Type_BaseTypes',
  30180. LinesToStr([ // statements
  30181. 'this.$rtti.$inherited("TCaption", rtl.string, {});',
  30182. 'this.$rtti.$inherited("TYesNo", rtl.boolean, {});',
  30183. 'this.$rtti.$inherited("TLetter", rtl.char, {});',
  30184. 'this.$rtti.$inherited("TFloat", rtl.double, {});',
  30185. 'this.$rtti.$inherited("TPtr", rtl.pointer, {});',
  30186. 'this.$rtti.$inherited("TShortInt", rtl.shortint, {});',
  30187. 'this.$rtti.$inherited("TByte", rtl.byte, {});',
  30188. 'this.$rtti.$inherited("TSmallInt", rtl.smallint, {});',
  30189. 'this.$rtti.$inherited("TWord", rtl.word, {});',
  30190. 'this.$rtti.$inherited("TInt32", rtl.longint, {});',
  30191. 'this.$rtti.$inherited("TDWord", rtl.longword, {});',
  30192. 'this.$rtti.$inherited("TValue", rtl.jsvalue, {});',
  30193. 'this.$rtti.$inherited("TAliasValue", this.$rtti["TValue"], {});',
  30194. 'this.p = null;',
  30195. 'this.a = undefined;',
  30196. '']),
  30197. LinesToStr([ // $mod.$main
  30198. '$mod.p = $mod.$rtti["TCaption"];',
  30199. '$mod.p = $mod.$rtti["TYesNo"];',
  30200. '$mod.p = $mod.$rtti["TLetter"];',
  30201. '$mod.p = $mod.$rtti["TFloat"];',
  30202. '$mod.p = $mod.$rtti["TPtr"];',
  30203. '$mod.p = $mod.$rtti["TShortInt"];',
  30204. '$mod.p = $mod.$rtti["TByte"];',
  30205. '$mod.p = $mod.$rtti["TSmallInt"];',
  30206. '$mod.p = $mod.$rtti["TWord"];',
  30207. '$mod.p = $mod.$rtti["TDWord"];',
  30208. '$mod.p = $mod.$rtti["TValue"];',
  30209. '$mod.p = $mod.$rtti["TAliasValue"];',
  30210. '$mod.p = $mod.$rtti["TAliasValue"];',
  30211. '']));
  30212. end;
  30213. procedure TTestModule.TestRTTI_TypeInfo_LocalFail;
  30214. begin
  30215. WithTypeInfo:=true;
  30216. StartProgram(false);
  30217. Add('procedure DoIt;');
  30218. Add('type');
  30219. Add(' integer = longint;');
  30220. Add(' TPoint = record');
  30221. Add(' x,y: integer;');
  30222. Add(' end;');
  30223. Add('var p: pointer;');
  30224. Add('begin');
  30225. Add(' p:=typeinfo(tpoint);');
  30226. Add('end;');
  30227. Add('begin');
  30228. SetExpectedPasResolverError(sSymbolCannotBePublished,nSymbolCannotBePublished);
  30229. ConvertProgram;
  30230. end;
  30231. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses1;
  30232. begin
  30233. WithTypeInfo:=true;
  30234. StartProgram(true,[supTypeInfo]);
  30235. Add([
  30236. '{$modeswitch externalclass}',
  30237. 'type',
  30238. ' TFlag = (up,down);',
  30239. ' TFlags = set of TFlag;',
  30240. 'var',
  30241. ' ti: TTypeInfo;',
  30242. ' tiInt: TTypeInfoInteger;',
  30243. ' tiEnum: TTypeInfoEnum;',
  30244. ' tiSet: TTypeInfoSet;',
  30245. 'begin',
  30246. ' ti:=typeinfo(string);',
  30247. ' ti:=typeinfo(boolean);',
  30248. ' ti:=typeinfo(char);',
  30249. ' ti:=typeinfo(double);',
  30250. ' tiInt:=typeinfo(shortint);',
  30251. ' tiInt:=typeinfo(byte);',
  30252. ' tiInt:=typeinfo(smallint);',
  30253. ' tiInt:=typeinfo(word);',
  30254. ' tiInt:=typeinfo(longint);',
  30255. ' tiInt:=typeinfo(longword);',
  30256. ' ti:=typeinfo(jsvalue);',
  30257. ' tiEnum:=typeinfo(tflag);',
  30258. ' tiSet:=typeinfo(tflags);']);
  30259. ConvertProgram;
  30260. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses1',
  30261. LinesToStr([ // statements
  30262. 'this.TFlag = {',
  30263. ' "0": "up",',
  30264. ' up: 0,',
  30265. ' "1": "down",',
  30266. ' down: 1',
  30267. '};',
  30268. 'this.$rtti.$Enum("TFlag", {',
  30269. ' minvalue: 0,',
  30270. ' maxvalue: 1,',
  30271. ' ordtype: 1,',
  30272. ' enumtype: this.TFlag',
  30273. '});',
  30274. 'this.$rtti.$Set("TFlags", {',
  30275. ' comptype: this.$rtti["TFlag"]',
  30276. '});',
  30277. 'this.ti = null;',
  30278. 'this.tiInt = null;',
  30279. 'this.tiEnum = null;',
  30280. 'this.tiSet = null;',
  30281. '']),
  30282. LinesToStr([ // $mod.$main
  30283. '$mod.ti = rtl.string;',
  30284. '$mod.ti = rtl.boolean;',
  30285. '$mod.ti = rtl.char;',
  30286. '$mod.ti = rtl.double;',
  30287. '$mod.tiInt = rtl.shortint;',
  30288. '$mod.tiInt = rtl.byte;',
  30289. '$mod.tiInt = rtl.smallint;',
  30290. '$mod.tiInt = rtl.word;',
  30291. '$mod.tiInt = rtl.longint;',
  30292. '$mod.tiInt = rtl.longword;',
  30293. '$mod.ti = rtl.jsvalue;',
  30294. '$mod.tiEnum = $mod.$rtti["TFlag"];',
  30295. '$mod.tiSet = $mod.$rtti["TFlags"];',
  30296. '']));
  30297. end;
  30298. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses2;
  30299. begin
  30300. WithTypeInfo:=true;
  30301. StartProgram(true,[supTypeInfo]);
  30302. Add('{$modeswitch externalclass}');
  30303. Add('type');
  30304. Add(' TStaticArr = array[boolean] of string;');
  30305. Add(' TDynArr = array of string;');
  30306. Add(' TProc = procedure;');
  30307. Add(' TMethod = procedure of object;');
  30308. Add('var');
  30309. Add(' StaticArray: TStaticArr;');
  30310. Add(' tiStaticArray: TTypeInfoStaticArray;');
  30311. Add(' DynArray: TDynArr;');
  30312. Add(' tiDynArray: TTypeInfoDynArray;');
  30313. Add(' ProcVar: TProc;');
  30314. Add(' tiProcVar: TTypeInfoProcVar;');
  30315. Add(' MethodVar: TMethod;');
  30316. Add(' tiMethodVar: TTypeInfoMethodVar;');
  30317. Add('begin');
  30318. Add(' tiStaticArray:=typeinfo(StaticArray);');
  30319. Add(' tiStaticArray:=typeinfo(TStaticArr);');
  30320. Add(' tiDynArray:=typeinfo(DynArray);');
  30321. Add(' tiDynArray:=typeinfo(TDynArr);');
  30322. Add(' tiProcVar:=typeinfo(ProcVar);');
  30323. Add(' tiProcVar:=typeinfo(TProc);');
  30324. Add(' tiMethodVar:=typeinfo(MethodVar);');
  30325. Add(' tiMethodVar:=typeinfo(TMethod);');
  30326. ConvertProgram;
  30327. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses2',
  30328. LinesToStr([ // statements
  30329. 'this.$rtti.$StaticArray("TStaticArr", {',
  30330. ' dims: [2],',
  30331. ' eltype: rtl.string',
  30332. '});',
  30333. 'this.$rtti.$DynArray("TDynArr", {',
  30334. ' eltype: rtl.string',
  30335. '});',
  30336. 'this.$rtti.$ProcVar("TProc", {',
  30337. ' procsig: rtl.newTIProcSig(null)',
  30338. '});',
  30339. 'this.$rtti.$MethodVar("TMethod", {',
  30340. ' procsig: rtl.newTIProcSig(null),',
  30341. ' methodkind: 0',
  30342. '});',
  30343. 'this.StaticArray = rtl.arraySetLength(null,"",2);',
  30344. 'this.tiStaticArray = null;',
  30345. 'this.DynArray = [];',
  30346. 'this.tiDynArray = null;',
  30347. 'this.ProcVar = null;',
  30348. 'this.tiProcVar = null;',
  30349. 'this.MethodVar = null;',
  30350. 'this.tiMethodVar = null;',
  30351. '']),
  30352. LinesToStr([ // $mod.$main
  30353. '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
  30354. '$mod.tiStaticArray = $mod.$rtti["TStaticArr"];',
  30355. '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
  30356. '$mod.tiDynArray = $mod.$rtti["TDynArr"];',
  30357. '$mod.tiProcVar = $mod.$rtti["TProc"];',
  30358. '$mod.tiProcVar = $mod.$rtti["TProc"];',
  30359. '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
  30360. '$mod.tiMethodVar = $mod.$rtti["TMethod"];',
  30361. '']));
  30362. end;
  30363. procedure TTestModule.TestRTTI_TypeInfo_ExtTypeInfoClasses3;
  30364. begin
  30365. WithTypeInfo:=true;
  30366. StartProgram(true,[supTypeInfo]);
  30367. Add('{$modeswitch externalclass}');
  30368. Add('type');
  30369. Add(' TRec = record end;');
  30370. // ToDo: ^TRec
  30371. Add(' TObject = class end;');
  30372. Add(' TClass = class of tobject;');
  30373. Add('var');
  30374. Add(' Rec: trec;');
  30375. Add(' tiRecord: ttypeinforecord;');
  30376. Add(' Obj: tobject;');
  30377. Add(' tiClass: ttypeinfoclass;');
  30378. Add(' aClass: tclass;');
  30379. Add(' tiClassRef: ttypeinfoclassref;');
  30380. // ToDo: ^TRec
  30381. Add(' tiPointer: ttypeinfopointer;');
  30382. Add('begin');
  30383. Add(' tirecord:=typeinfo(trec);');
  30384. Add(' tirecord:=typeinfo(trec);');
  30385. Add(' ticlass:=typeinfo(obj);');
  30386. Add(' ticlass:=typeinfo(tobject);');
  30387. Add(' ticlass:=typeinfo(aclass);');
  30388. Add(' ticlassref:=typeinfo(tclass);');
  30389. ConvertProgram;
  30390. CheckSource('TestRTTI_TypeInfo_ExtTypeInfoClasses3',
  30391. LinesToStr([ // statements
  30392. 'rtl.recNewT(this, "TRec", function () {',
  30393. ' this.$eq = function (b) {',
  30394. ' return true;',
  30395. ' };',
  30396. ' this.$assign = function (s) {',
  30397. ' return this;',
  30398. ' };',
  30399. ' $mod.$rtti.$Record("TRec", {});',
  30400. '});',
  30401. 'rtl.createClass(this, "TObject", null, function () {',
  30402. ' this.$init = function () {',
  30403. ' };',
  30404. ' this.$final = function () {',
  30405. ' };',
  30406. '});',
  30407. 'this.$rtti.$ClassRef("TClass", {',
  30408. ' instancetype: this.$rtti["TObject"]',
  30409. '});',
  30410. 'this.Rec = this.TRec.$new();',
  30411. 'this.tiRecord = null;',
  30412. 'this.Obj = null;',
  30413. 'this.tiClass = null;',
  30414. 'this.aClass = null;',
  30415. 'this.tiClassRef = null;',
  30416. 'this.tiPointer = null;',
  30417. '']),
  30418. LinesToStr([ // $mod.$main
  30419. '$mod.tiRecord = $mod.$rtti["TRec"];',
  30420. '$mod.tiRecord = $mod.$rtti["TRec"];',
  30421. '$mod.tiClass = $mod.Obj.$rtti;',
  30422. '$mod.tiClass = $mod.$rtti["TObject"];',
  30423. '$mod.tiClass = $mod.aClass.$rtti;',
  30424. '$mod.tiClassRef = $mod.$rtti["TClass"];',
  30425. '']));
  30426. end;
  30427. procedure TTestModule.TestRTTI_TypeInfo_FunctionClassType;
  30428. begin
  30429. WithTypeInfo:=true;
  30430. StartProgram(true,[supTypeInfo]);
  30431. Add([
  30432. '{$modeswitch externalclass}',
  30433. 'type',
  30434. ' TClass = class of tobject;',
  30435. ' TObject = class',
  30436. ' function MyClass: TClass;',
  30437. ' class function ClassType: TClass;',
  30438. ' end;',
  30439. 'function TObject.MyClass: TClass;',
  30440. 'var t: TTypeInfoClass;',
  30441. 'begin',
  30442. ' t:=TypeInfo(Self);',
  30443. ' t:=TypeInfo(Result);',
  30444. ' t:=TypeInfo(TObject);',
  30445. 'end;',
  30446. 'class function TObject.ClassType: TClass;',
  30447. 'var t: TTypeInfoClass;',
  30448. 'begin',
  30449. ' t:=TypeInfo(Self);',
  30450. ' t:=TypeInfo(Result);',
  30451. 'end;',
  30452. 'var',
  30453. ' Obj: TObject;',
  30454. ' t: TTypeInfoClass;',
  30455. 'begin',
  30456. ' t:=TypeInfo(TObject.ClassType);',
  30457. ' t:=TypeInfo(Obj.ClassType);',
  30458. ' t:=TypeInfo(Obj.MyClass);',
  30459. '']);
  30460. ConvertProgram;
  30461. CheckSource('TestRTTI_TypeInfo_FunctionClassType',
  30462. LinesToStr([ // statements
  30463. 'this.$rtti.$Class("TObject");',
  30464. 'this.$rtti.$ClassRef("TClass", {',
  30465. ' instancetype: this.$rtti["TObject"]',
  30466. '});',
  30467. 'rtl.createClass(this, "TObject", null, function () {',
  30468. ' this.$init = function () {',
  30469. ' };',
  30470. ' this.$final = function () {',
  30471. ' };',
  30472. ' this.MyClass = function () {',
  30473. ' var Result = null;',
  30474. ' var t = null;',
  30475. ' t = this.$rtti;',
  30476. ' t = Result.$rtti;',
  30477. ' t = $mod.$rtti["TObject"];',
  30478. ' return Result;',
  30479. ' };',
  30480. ' this.ClassType = function () {',
  30481. ' var Result = null;',
  30482. ' var t = null;',
  30483. ' t = this.$rtti;',
  30484. ' t = Result.$rtti;',
  30485. ' return Result;',
  30486. ' };',
  30487. '});',
  30488. 'this.Obj = null;',
  30489. 'this.t = null;',
  30490. '']),
  30491. LinesToStr([ // $mod.$main
  30492. '$mod.t = $mod.TObject.ClassType().$rtti;',
  30493. '$mod.t = $mod.Obj.$class.ClassType().$rtti;',
  30494. '$mod.t = $mod.Obj.MyClass().$rtti;',
  30495. '']));
  30496. end;
  30497. procedure TTestModule.TestRTTI_TypeInfo_MixedUnits_PointerAndClass;
  30498. begin
  30499. WithTypeInfo:=true;
  30500. AddModuleWithIntfImplSrc('typinfo.pas',
  30501. LinesToStr([
  30502. '{$modeswitch externalclass}',
  30503. 'type',
  30504. ' TTypeInfo = class external name ''rtl.tTypeInfo'' end;',
  30505. ' TTypeInfoInteger = class external name ''rtl.tTypeInfoInteger''(TTypeInfo) end;',
  30506. '']),
  30507. '');
  30508. AddModuleWithIntfImplSrc('unit2.pas',
  30509. LinesToStr([
  30510. 'uses typinfo;',
  30511. 'type PTypeInfo = TTypeInfo;', // delphi compatibility code
  30512. 'procedure DoPtr(p: PTypeInfo);',
  30513. 'procedure DoInfo(t: TTypeInfo);',
  30514. 'procedure DoInt(t: TTypeInfoInteger);',
  30515. '']),
  30516. LinesToStr([
  30517. 'procedure DoPtr(p: PTypeInfo);',
  30518. 'begin end;',
  30519. 'procedure DoInfo(t: TTypeInfo);',
  30520. 'begin end;',
  30521. 'procedure DoInt(t: TTypeInfoInteger);',
  30522. 'begin end;',
  30523. '']));
  30524. StartUnit(true);
  30525. Add([
  30526. 'interface',
  30527. 'uses unit2;', // does not use unit typinfo
  30528. 'implementation',
  30529. 'var',
  30530. ' i: byte;',
  30531. ' p: pointer;',
  30532. ' t: PTypeInfo;',
  30533. 'initialization',
  30534. ' p:=typeinfo(i);',
  30535. ' t:=typeinfo(i);',
  30536. ' if p=t then ;',
  30537. ' if p=typeinfo(i) then ;',
  30538. ' if typeinfo(i)=p then ;',
  30539. ' if t=typeinfo(i) then ;',
  30540. ' if typeinfo(i)=t then ;',
  30541. ' DoPtr(p);',
  30542. ' DoPtr(t);',
  30543. ' DoPtr(typeinfo(i));',
  30544. ' DoInfo(p);',
  30545. ' DoInfo(t);',
  30546. ' DoInfo(typeinfo(i));',
  30547. ' DoInt(typeinfo(i));',
  30548. '']);
  30549. ConvertUnit;
  30550. CheckSource('TestRTTI_TypeInfo_MixedUnits_PointerAndClass',
  30551. LinesToStr([ // statements
  30552. 'var $impl = $mod.$impl;',
  30553. '']),
  30554. LinesToStr([ // this.$init
  30555. '$impl.p = rtl.byte;',
  30556. '$impl.t = rtl.byte;',
  30557. 'if ($impl.p === $impl.t) ;',
  30558. 'if ($impl.p === rtl.byte) ;',
  30559. 'if (rtl.byte === $impl.p) ;',
  30560. 'if ($impl.t === rtl.byte) ;',
  30561. 'if (rtl.byte === $impl.t) ;',
  30562. 'pas.unit2.DoPtr($impl.p);',
  30563. 'pas.unit2.DoPtr($impl.t);',
  30564. 'pas.unit2.DoPtr(rtl.byte);',
  30565. 'pas.unit2.DoInfo($impl.p);',
  30566. 'pas.unit2.DoInfo($impl.t);',
  30567. 'pas.unit2.DoInfo(rtl.byte);',
  30568. 'pas.unit2.DoInt(rtl.byte);',
  30569. '']),
  30570. LinesToStr([ // implementation
  30571. '$impl.i = 0;',
  30572. '$impl.p = null;',
  30573. '$impl.t = null;',
  30574. '']) );
  30575. end;
  30576. procedure TTestModule.TestRTTI_Interface_Corba;
  30577. begin
  30578. WithTypeInfo:=true;
  30579. StartProgram(true,[supTypeInfo]);
  30580. Add([
  30581. '{$interfaces corba}',
  30582. '{$modeswitch externalclass}',
  30583. 'type',
  30584. ' IUnknown = interface',
  30585. ' end;',
  30586. ' IBird = interface',
  30587. ' function GetItem: longint;',
  30588. ' procedure SetItem(Value: longint);',
  30589. ' property Item: longint read GetItem write SetItem;',
  30590. ' end;',
  30591. 'procedure DoIt(t: TTypeInfoInterface); begin end;',
  30592. 'var',
  30593. ' i: IBird;',
  30594. ' t: TTypeInfoInterface;',
  30595. 'begin',
  30596. ' t:=TypeInfo(IBird);',
  30597. ' t:=TypeInfo(i);',
  30598. ' DoIt(t);',
  30599. ' DoIt(TypeInfo(IBird));',
  30600. '']);
  30601. ConvertProgram;
  30602. CheckSource('TestRTTI_Interface_Corba',
  30603. LinesToStr([ // statements
  30604. 'rtl.createInterface(',
  30605. ' this,',
  30606. ' "IUnknown",',
  30607. ' "{B92D5841-758A-322B-B800-000000000000}",',
  30608. ' [],',
  30609. ' null,',
  30610. ' function () {',
  30611. ' }',
  30612. ');',
  30613. 'rtl.createInterface(',
  30614. ' this,',
  30615. ' "IBird",',
  30616. ' "{D32D5841-6264-3AE3-A2C9-B91CE922C9B9}",',
  30617. ' ["GetItem", "SetItem"],',
  30618. ' null,',
  30619. ' function () {',
  30620. ' var $r = this.$rtti;',
  30621. ' $r.addMethod("GetItem", 1, null, rtl.longint);',
  30622. ' $r.addMethod("SetItem", 0, [["Value", rtl.longint]]);',
  30623. ' $r.addProperty("Item", 3, rtl.longint, "GetItem", "SetItem");',
  30624. ' }',
  30625. ');',
  30626. 'this.DoIt = function (t) {',
  30627. '}; ',
  30628. 'this.i = null;',
  30629. 'this.t = null;',
  30630. '']),
  30631. LinesToStr([ // $mod.$main
  30632. '$mod.t = $mod.$rtti["IBird"];',
  30633. '$mod.t = $mod.i.$rtti;',
  30634. '$mod.DoIt($mod.t);',
  30635. '$mod.DoIt($mod.$rtti["IBird"]);',
  30636. '']));
  30637. end;
  30638. procedure TTestModule.TestRTTI_Interface_COM;
  30639. begin
  30640. WithTypeInfo:=true;
  30641. StartProgram(true,[supTypeInfo]);
  30642. Add([
  30643. '{$interfaces com}',
  30644. '{$modeswitch externalclass}',
  30645. 'type',
  30646. ' TGuid = record end;',
  30647. ' integer = longint;',
  30648. ' IUnknown = interface',
  30649. ' function QueryInterface(const iid: TGuid; out obj): Integer;',
  30650. ' function _AddRef: Integer;',
  30651. ' function _Release: Integer;',
  30652. ' end;',
  30653. ' IBird = interface',
  30654. ' function GetItem: longint;',
  30655. ' procedure SetItem(Value: longint);',
  30656. ' property Item: longint read GetItem write SetItem;',
  30657. ' end;',
  30658. 'var',
  30659. ' i: IBird;',
  30660. ' t: TTypeInfoInterface;',
  30661. 'begin',
  30662. ' t:=TypeInfo(IBird);',
  30663. ' t:=TypeInfo(i);',
  30664. '']);
  30665. ConvertProgram;
  30666. CheckSource('TestRTTI_Interface_COM',
  30667. LinesToStr([ // statements
  30668. 'rtl.recNewT(this, "TGuid", function () {',
  30669. ' this.$eq = function (b) {',
  30670. ' return true;',
  30671. ' };',
  30672. ' this.$assign = function (s) {',
  30673. ' return this;',
  30674. ' };',
  30675. ' $mod.$rtti.$Record("TGuid", {});',
  30676. '});',
  30677. 'rtl.createInterface(',
  30678. ' this,',
  30679. ' "IUnknown",',
  30680. ' "{D7ADB00D-1A9B-3EDC-B123-730E661DDFA9}",',
  30681. ' ["QueryInterface", "_AddRef", "_Release"],',
  30682. ' null,',
  30683. ' function () {',
  30684. ' this.$kind = "com";',
  30685. ' var $r = this.$rtti;',
  30686. ' $r.addMethod("QueryInterface", 1, [["iid", $mod.$rtti["TGuid"], 2], ["obj", null, 4]], rtl.longint);',
  30687. ' $r.addMethod("_AddRef", 1, null, rtl.longint);',
  30688. ' $r.addMethod("_Release", 1, null, rtl.longint);',
  30689. ' }',
  30690. ');',
  30691. 'rtl.createInterface(',
  30692. ' this,',
  30693. ' "IBird",',
  30694. ' "{9CC77572-0E45-3594-9A88-9E8D865C9E0A}",',
  30695. ' ["GetItem", "SetItem"],',
  30696. ' this.IUnknown,',
  30697. ' function () {',
  30698. ' var $r = this.$rtti;',
  30699. ' $r.addMethod("GetItem", 1, null, rtl.longint);',
  30700. ' $r.addMethod("SetItem", 0, [["Value", rtl.longint]]);',
  30701. ' $r.addProperty("Item", 3, rtl.longint, "GetItem", "SetItem");',
  30702. ' }',
  30703. ');',
  30704. 'this.i = null;',
  30705. 'this.t = null;',
  30706. '']),
  30707. LinesToStr([ // $mod.$main
  30708. '$mod.t = $mod.$rtti["IBird"];',
  30709. '$mod.t = $mod.i.$rtti;',
  30710. '']));
  30711. end;
  30712. procedure TTestModule.TestRTTI_ClassHelper;
  30713. begin
  30714. WithTypeInfo:=true;
  30715. StartProgram(true,[supTypeInfo]);
  30716. Add([
  30717. '{$interfaces com}',
  30718. '{$modeswitch externalclass}',
  30719. 'type',
  30720. ' TObject = class',
  30721. ' end;',
  30722. ' THelper = class helper for TObject',
  30723. ' published',
  30724. ' function GetItem: longint;',
  30725. ' property Item: longint read GetItem;',
  30726. ' end;',
  30727. 'function THelper.GetItem: longint;',
  30728. 'begin',
  30729. 'end;',
  30730. 'var',
  30731. ' t: TTypeInfoHelper;',
  30732. 'begin',
  30733. ' t:=TypeInfo(THelper);',
  30734. '']);
  30735. ConvertProgram;
  30736. CheckSource('TestRTTI_ClassHelper',
  30737. LinesToStr([ // statements
  30738. 'rtl.createClass(this, "TObject", null, function () {',
  30739. ' this.$init = function () {',
  30740. ' };',
  30741. ' this.$final = function () {',
  30742. ' };',
  30743. '});',
  30744. 'rtl.createHelper(this, "THelper", null, function () {',
  30745. ' this.GetItem = function () {',
  30746. ' var Result = 0;',
  30747. ' return Result;',
  30748. ' };',
  30749. ' var $r = this.$rtti;',
  30750. ' $r.addMethod("GetItem", 1, null, rtl.longint);',
  30751. ' $r.addProperty("Item", 1, rtl.longint, "GetItem", "");',
  30752. '});',
  30753. 'this.t = null;',
  30754. '']),
  30755. LinesToStr([ // $mod.$main
  30756. '$mod.t = $mod.$rtti["THelper"];',
  30757. '']));
  30758. end;
  30759. procedure TTestModule.TestRTTI_ExternalClass;
  30760. begin
  30761. WithTypeInfo:=true;
  30762. StartProgram(true,[supTypeInfo]);
  30763. Add([
  30764. '{$modeswitch externalclass}',
  30765. 'type',
  30766. ' TJSObject = class external name ''Object''',
  30767. ' end;',
  30768. ' TJSArray = class external name ''Array'' (TJSObject)',
  30769. ' end;',
  30770. 'var',
  30771. ' p: Pointer;',
  30772. ' tc: TTypeInfoExtClass;',
  30773. 'begin',
  30774. ' p:=typeinfo(TJSArray);']);
  30775. ConvertProgram;
  30776. CheckSource('TestRTTI_ExternalClass',
  30777. LinesToStr([ // statements
  30778. 'this.$rtti.$ExtClass("TJSObject", {',
  30779. ' jsclass: "Object"',
  30780. '});',
  30781. 'this.$rtti.$ExtClass("TJSArray", {',
  30782. ' ancestor: this.$rtti["TJSObject"],',
  30783. ' jsclass: "Array"',
  30784. '});',
  30785. 'this.p = null;',
  30786. 'this.tc = null;',
  30787. '']),
  30788. LinesToStr([ // $mod.$main
  30789. '$mod.p = $mod.$rtti["TJSArray"];',
  30790. '']));
  30791. end;
  30792. procedure TTestModule.TestRTTI_Unit;
  30793. begin
  30794. WithTypeInfo:=true;
  30795. AddModuleWithIntfImplSrc('unit2.pas',
  30796. LinesToStr([
  30797. '{$mode delphi}',
  30798. 'type',
  30799. ' TWordArray = array of word;',
  30800. ' TArray<T> = array of T;',
  30801. '']),
  30802. '');
  30803. StartUnit(true,[supTypeInfo,supTInterfacedObject]);
  30804. Add([
  30805. '{$mode delphi}',
  30806. 'interface',
  30807. 'uses unit2;',
  30808. 'type',
  30809. ' IBird = interface',
  30810. ' function Swoop: TWordArray;',
  30811. ' function Glide: TArray<word>;',
  30812. ' end;',
  30813. 'procedure Fly;',
  30814. 'implementation',
  30815. 'procedure Fly;',
  30816. 'var',
  30817. ' ta: tTypeInfoDynArray;',
  30818. ' ti: tTypeInfoInterface;',
  30819. 'begin',
  30820. ' ta:=typeinfo(TWordArray);',
  30821. ' ta:=typeinfo(TArray<word>);',
  30822. ' ti:=typeinfo(IBird);',
  30823. 'end;',
  30824. '']);
  30825. ConvertUnit;
  30826. CheckSource('TestRTTI_ExternalClass',
  30827. LinesToStr([ // statements
  30828. 'rtl.createInterface(',
  30829. ' this,',
  30830. ' "IBird",',
  30831. ' "{3B98AAAC-6116-3E17-AA85-F16786D85B09}",',
  30832. ' ["Swoop", "Glide"],',
  30833. ' pas.system.IUnknown,',
  30834. ' function () {',
  30835. ' var $r = this.$rtti;',
  30836. ' $r.addMethod("Swoop", 1, null, pas.unit2.$rtti["TWordArray"]);',
  30837. ' $r.addMethod("Glide", 1, null, pas.unit2.$rtti["TArray<System.Word>"]);',
  30838. ' }',
  30839. ');',
  30840. 'this.Fly = function () {',
  30841. ' var ta = null;',
  30842. ' var ti = null;',
  30843. ' ta = pas.unit2.$rtti["TWordArray"];',
  30844. ' ta = pas.unit2.$rtti["TArray<System.Word>"];',
  30845. ' ti = $mod.$rtti["IBird"];',
  30846. '};',
  30847. '']),
  30848. LinesToStr([ // $mod.$main
  30849. '']));
  30850. end;
  30851. procedure TTestModule.TestResourcestringProgram;
  30852. begin
  30853. AddModuleWithIntfImplSrc('unit2.pas',
  30854. LinesToStr([
  30855. 'resourcestring Title = ''Nice'';',
  30856. '']),
  30857. '');
  30858. StartProgram(true);
  30859. Add([
  30860. 'uses unit2;',
  30861. 'const Bar = ''bar'';',
  30862. 'resourcestring',
  30863. ' Red = ''red'';',
  30864. ' Foobar = ''fOo''+bar;',
  30865. 'var s: string;',
  30866. ' c: char;',
  30867. 'begin',
  30868. ' s:=red;',
  30869. ' s:=test1.red;',
  30870. ' s:=Title;',
  30871. ' c:=red[1];',
  30872. ' c:=test1.red[2];',
  30873. ' if red=foobar then ;',
  30874. ' if red[3]=red[4] then ;']);
  30875. ConvertProgram;
  30876. CheckSource('TestResourcestringProgram',
  30877. LinesToStr([ // statements
  30878. 'this.Bar = "bar";',
  30879. 'this.s = "";',
  30880. 'this.c = "";',
  30881. '$mod.$resourcestrings = {',
  30882. ' Red: {',
  30883. ' org: "red"',
  30884. ' },',
  30885. ' Foobar: {',
  30886. ' org: "fOobar"',
  30887. ' }',
  30888. '};',
  30889. '']),
  30890. LinesToStr([ // $mod.$main
  30891. '$mod.s = rtl.getResStr($mod, "Red");',
  30892. '$mod.s = rtl.getResStr($mod, "Red");',
  30893. '$mod.s = rtl.getResStr(pas.unit2, "Title");',
  30894. '$mod.c = rtl.getResStr($mod, "Red").charAt(0);',
  30895. '$mod.c = rtl.getResStr($mod, "Red").charAt(1);',
  30896. 'if (rtl.getResStr($mod, "Red") === rtl.getResStr($mod, "Foobar")) ;',
  30897. 'if (rtl.getResStr($mod, "Red").charAt(2) === rtl.getResStr($mod, "Red").charAt(3)) ;',
  30898. '']));
  30899. end;
  30900. procedure TTestModule.TestResourcestringUnit;
  30901. begin
  30902. AddModuleWithIntfImplSrc('unit2.pas',
  30903. LinesToStr([
  30904. 'resourcestring Title = ''Nice'';',
  30905. '']),
  30906. '');
  30907. StartUnit(true);
  30908. Add([
  30909. 'interface',
  30910. 'uses unit2;',
  30911. 'const Red = ''rEd'';',
  30912. 'resourcestring',
  30913. ' Blue = ''blue'';',
  30914. ' NotRed = ''not''+Red;',
  30915. 'var s: string;',
  30916. 'implementation',
  30917. 'resourcestring',
  30918. ' ImplGreen = ''green'';',
  30919. 'initialization',
  30920. ' s:=blue+ImplGreen;',
  30921. ' s:=test1.blue+test1.implgreen;',
  30922. ' s:=blue[1]+implgreen[2];',
  30923. ' s:=Title;',
  30924. '']);
  30925. ConvertUnit;
  30926. CheckSource('TestResourcestringUnit',
  30927. LinesToStr([ // statements
  30928. 'this.Red = "rEd";',
  30929. 'this.s = "";',
  30930. '$mod.$resourcestrings = {',
  30931. ' Blue: {',
  30932. ' org: "blue"',
  30933. ' },',
  30934. ' NotRed: {',
  30935. ' org: "notrEd"',
  30936. ' },',
  30937. ' ImplGreen: {',
  30938. ' org: "green"',
  30939. ' }',
  30940. '};',
  30941. '']),
  30942. LinesToStr([ // $mod.$main
  30943. '$mod.s = rtl.getResStr($mod, "Blue") + rtl.getResStr($mod, "ImplGreen");',
  30944. '$mod.s = rtl.getResStr($mod, "Blue") + rtl.getResStr($mod, "ImplGreen");',
  30945. '$mod.s = rtl.getResStr($mod, "Blue").charAt(0) + rtl.getResStr($mod, "ImplGreen").charAt(1);',
  30946. '$mod.s = rtl.getResStr(pas.unit2, "Title");',
  30947. '']));
  30948. end;
  30949. procedure TTestModule.TestResourcestringImplementation;
  30950. begin
  30951. StartUnit(false);
  30952. Add([
  30953. 'interface',
  30954. 'implementation',
  30955. 'resourcestring',
  30956. ' ImplRed = ''red'';']);
  30957. ConvertUnit;
  30958. CheckSource('TestResourcestringImplementation',
  30959. LinesToStr([ // intf statements
  30960. 'var $impl = $mod.$impl;']),
  30961. LinesToStr([ // $mod.$init
  30962. '']),
  30963. LinesToStr([ // impl statements
  30964. '$mod.$resourcestrings = {',
  30965. ' ImplRed: {',
  30966. ' org: "red"',
  30967. ' }',
  30968. '};',
  30969. '']));
  30970. end;
  30971. procedure TTestModule.TestAttributes_Members;
  30972. begin
  30973. WithTypeInfo:=true;
  30974. StartProgram(false);
  30975. Add([
  30976. '{$modeswitch PrefixedAttributes}',
  30977. 'type',
  30978. ' TObject = class',
  30979. ' constructor Create;',
  30980. ' end;',
  30981. ' TCustomAttribute = class',
  30982. ' constructor Create(Id: word);',
  30983. ' end;',
  30984. ' [Missing]',
  30985. ' TBird = class',
  30986. ' published',
  30987. ' [Tcustom]',
  30988. ' FField: word;',
  30989. ' [tcustom(14)]',
  30990. ' property Size: word read FField;',
  30991. ' [Tcustom(15)]',
  30992. ' procedure Fly; virtual; abstract;',
  30993. ' end;',
  30994. ' TRec = record',
  30995. ' [Tcustom,tcustom(14)]',
  30996. ' Size: word;',
  30997. ' end;',
  30998. 'constructor TObject.Create; begin end;',
  30999. 'constructor TCustomAttribute.Create(Id: word); begin end;',
  31000. 'begin',
  31001. '']);
  31002. ConvertProgram;
  31003. CheckSource('TestAttributes_Members',
  31004. LinesToStr([ // statements
  31005. 'rtl.createClass(this, "TObject", null, function () {',
  31006. ' this.$init = function () {',
  31007. ' };',
  31008. ' this.$final = function () {',
  31009. ' };',
  31010. ' this.Create = function () {',
  31011. ' return this;',
  31012. ' };',
  31013. '});',
  31014. 'rtl.createClass(this, "TCustomAttribute", this.TObject, function () {',
  31015. ' this.Create$1 = function (Id) {',
  31016. ' return this;',
  31017. ' };',
  31018. '});',
  31019. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  31020. ' this.$init = function () {',
  31021. ' $mod.TObject.$init.call(this);',
  31022. ' this.FField = 0;',
  31023. ' };',
  31024. ' var $r = this.$rtti;',
  31025. ' $r.addField("FField", rtl.word, {',
  31026. ' attr: [$mod.TCustomAttribute, "Create"]',
  31027. ' });',
  31028. ' $r.addProperty(',
  31029. ' "Size",',
  31030. ' 0,',
  31031. ' rtl.word,',
  31032. ' "FField",',
  31033. ' "",',
  31034. ' {',
  31035. ' attr: [$mod.TCustomAttribute, "Create$1", [14]]',
  31036. ' }',
  31037. ' );',
  31038. ' $r.addMethod("Fly", 0, null, null, {',
  31039. ' attr: [$mod.TCustomAttribute, "Create$1", [15]]',
  31040. ' });',
  31041. '});',
  31042. 'rtl.recNewT(this, "TRec", function () {',
  31043. ' this.Size = 0;',
  31044. ' this.$eq = function (b) {',
  31045. ' return this.Size === b.Size;',
  31046. ' };',
  31047. ' this.$assign = function (s) {',
  31048. ' this.Size = s.Size;',
  31049. ' return this;',
  31050. ' };',
  31051. ' var $r = $mod.$rtti.$Record("TRec", {});',
  31052. ' $r.addField("Size", rtl.word, {',
  31053. ' attr: [',
  31054. ' $mod.TCustomAttribute,',
  31055. ' "Create",',
  31056. ' $mod.TCustomAttribute,',
  31057. ' "Create$1",',
  31058. ' [14]',
  31059. ' ]',
  31060. ' });',
  31061. '});',
  31062. '']),
  31063. LinesToStr([ // $mod.$main
  31064. '']));
  31065. end;
  31066. procedure TTestModule.TestAttributes_Types;
  31067. begin
  31068. WithTypeInfo:=true;
  31069. StartProgram(false);
  31070. Add([
  31071. '{$modeswitch PrefixedAttributes}',
  31072. 'type',
  31073. ' TObject = class',
  31074. ' constructor Create(Id: word);',
  31075. ' end;',
  31076. ' TCustomAttribute = class',
  31077. ' end;',
  31078. ' [TCustom(1)]',
  31079. ' TMyClass = class',
  31080. ' end;',
  31081. ' [TCustom(11)]',
  31082. ' TMyDescendant = class(TMyClass)',
  31083. ' end;',
  31084. ' [TCustom(2)]',
  31085. ' TRec = record',
  31086. ' end;',
  31087. ' [TCustom(3)]',
  31088. ' TInt = type word;',
  31089. 'constructor TObject.Create(Id: word);',
  31090. 'begin',
  31091. 'end;',
  31092. 'var p: pointer;',
  31093. 'begin',
  31094. ' p:=typeinfo(TMyClass);',
  31095. ' p:=typeinfo(TRec);',
  31096. ' p:=typeinfo(TInt);',
  31097. '']);
  31098. ConvertProgram;
  31099. CheckSource('TestAttributes_Types',
  31100. LinesToStr([ // statements
  31101. 'rtl.createClass(this, "TObject", null, function () {',
  31102. ' this.$init = function () {',
  31103. ' };',
  31104. ' this.$final = function () {',
  31105. ' };',
  31106. ' this.Create = function (Id) {',
  31107. ' return this;',
  31108. ' };',
  31109. '});',
  31110. 'rtl.createClass(this, "TCustomAttribute", this.TObject, function () {',
  31111. '});',
  31112. 'rtl.createClass(this, "TMyClass", this.TObject, function () {',
  31113. ' var $r = this.$rtti;',
  31114. ' $r.attr = [$mod.TCustomAttribute, "Create", [1]];',
  31115. '});',
  31116. 'rtl.createClass(this, "TMyDescendant", this.TMyClass, function () {',
  31117. ' var $r = this.$rtti;',
  31118. ' $r.attr = [$mod.TCustomAttribute, "Create", [11]];',
  31119. '});',
  31120. 'rtl.recNewT(this, "TRec", function () {',
  31121. ' this.$eq = function (b) {',
  31122. ' return true;',
  31123. ' };',
  31124. ' this.$assign = function (s) {',
  31125. ' return this;',
  31126. ' };',
  31127. ' $mod.$rtti.$Record("TRec", {',
  31128. ' attr: [$mod.TCustomAttribute, "Create", [2]]',
  31129. ' });',
  31130. '});',
  31131. 'this.$rtti.$inherited("TInt", rtl.word, {',
  31132. ' attr: [this.TCustomAttribute, "Create", [3]]',
  31133. '});',
  31134. 'this.p = null;',
  31135. '']),
  31136. LinesToStr([ // $mod.$main
  31137. '$mod.p = $mod.$rtti["TMyClass"];',
  31138. '$mod.p = $mod.$rtti["TRec"];',
  31139. '$mod.p = $mod.$rtti["TInt"];',
  31140. '']));
  31141. end;
  31142. procedure TTestModule.TestAttributes_HelperConstructor_Fail;
  31143. begin
  31144. WithTypeInfo:=true;
  31145. StartProgram(false);
  31146. Add([
  31147. '{$modeswitch PrefixedAttributes}',
  31148. 'type',
  31149. ' TObject = class',
  31150. ' constructor Create;',
  31151. ' end;',
  31152. ' TCustomAttribute = class',
  31153. ' end;',
  31154. ' THelper = class helper for TCustomAttribute',
  31155. ' constructor Create(Id: word);',
  31156. ' end;',
  31157. ' [TCustom(3)]',
  31158. ' TMyInt = word;',
  31159. 'constructor TObject.Create; begin end;',
  31160. 'constructor THelper.Create(Id: word); begin end;',
  31161. 'begin',
  31162. ' if typeinfo(TMyInt)=nil then ;']);
  31163. ConvertProgram;
  31164. end;
  31165. procedure TTestModule.TestAssert;
  31166. begin
  31167. StartProgram(false);
  31168. Add([
  31169. 'procedure DoIt;',
  31170. 'var',
  31171. ' b: boolean;',
  31172. ' s: string;',
  31173. 'begin',
  31174. ' {$Assertions on}',
  31175. ' Assert(b);',
  31176. 'end;',
  31177. 'begin',
  31178. ' DoIt;',
  31179. '']);
  31180. ConvertProgram;
  31181. CheckSource('TestAssert',
  31182. LinesToStr([ // statements
  31183. 'this.DoIt = function () {',
  31184. ' var b = false;',
  31185. ' var s = "";',
  31186. ' if (!b) throw "assert failed";',
  31187. '};',
  31188. '']),
  31189. LinesToStr([ // $mod.$main
  31190. '$mod.DoIt();',
  31191. '']));
  31192. end;
  31193. procedure TTestModule.TestAssert_SysUtils;
  31194. begin
  31195. AddModuleWithIntfImplSrc('SysUtils.pas',
  31196. LinesToStr([
  31197. 'type',
  31198. ' TObject = class',
  31199. ' constructor Create;',
  31200. ' end;',
  31201. ' EAssertionFailed = class',
  31202. ' constructor Create(s: string);',
  31203. ' end;',
  31204. '']),
  31205. LinesToStr([
  31206. 'constructor TObject.Create;',
  31207. 'begin end;',
  31208. 'constructor EAssertionFailed.Create(s: string);',
  31209. 'begin end;',
  31210. '']) );
  31211. StartProgram(true);
  31212. Add([
  31213. 'uses sysutils;',
  31214. 'procedure DoIt;',
  31215. 'var',
  31216. ' b: boolean;',
  31217. ' s: string;',
  31218. 'begin',
  31219. ' {$Assertions on}',
  31220. ' Assert(b);',
  31221. ' Assert(b,''msg'');',
  31222. 'end;',
  31223. 'begin',
  31224. ' DoIt;',
  31225. '']);
  31226. ConvertProgram;
  31227. CheckSource('TestAssert_SysUtils',
  31228. LinesToStr([ // statements
  31229. 'this.DoIt = function () {',
  31230. ' var b = false;',
  31231. ' var s = "";',
  31232. ' if (!b) throw pas.SysUtils.EAssertionFailed.$create("Create");',
  31233. ' if (!b) throw pas.SysUtils.EAssertionFailed.$create("Create$1", ["msg"]);',
  31234. '};',
  31235. '']),
  31236. LinesToStr([ // $mod.$main
  31237. '$mod.DoIt();',
  31238. '']));
  31239. end;
  31240. procedure TTestModule.TestObjectChecks;
  31241. begin
  31242. Scanner.CurrentBoolSwitches:=Scanner.CurrentBoolSwitches+[bsObjectChecks];
  31243. StartProgram(false);
  31244. Add([
  31245. 'type',
  31246. ' TObject = class',
  31247. ' procedure DoIt;',
  31248. ' end;',
  31249. ' TClass = class of tobject;',
  31250. ' TBird = class',
  31251. ' end;',
  31252. ' TBirdClass = class of TBird;',
  31253. 'var',
  31254. ' o : TObject;',
  31255. ' c: TClass;',
  31256. ' b: TBird;',
  31257. ' bc: TBirdClass;',
  31258. 'procedure TObject.DoIt;',
  31259. 'begin',
  31260. ' b:=TBird(o);',
  31261. 'end;',
  31262. 'begin',
  31263. ' o.DoIt;',
  31264. ' b:=TBird(o);',
  31265. ' bc:=TBirdClass(c);',
  31266. '']);
  31267. ConvertProgram;
  31268. CheckSource('TestCheckMethodCall',
  31269. LinesToStr([ // statements
  31270. 'rtl.createClass(this, "TObject", null, function () {',
  31271. ' this.$init = function () {',
  31272. ' };',
  31273. ' this.$final = function () {',
  31274. ' };',
  31275. ' this.DoIt = function () {',
  31276. ' rtl.checkMethodCall(this,$mod.TObject);',
  31277. ' $mod.b = rtl.asExt($mod.o, $mod.TBird, 1);',
  31278. ' };',
  31279. '});',
  31280. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  31281. '});',
  31282. 'this.o = null;',
  31283. 'this.c = null;',
  31284. 'this.b = null;',
  31285. 'this.bc = null;',
  31286. '']),
  31287. LinesToStr([ // $mod.$main
  31288. '$mod.o.DoIt();',
  31289. '$mod.b = rtl.asExt($mod.o,$mod.TBird, 1);',
  31290. '$mod.bc = rtl.asExt($mod.c, $mod.TBird, 2);',
  31291. '']));
  31292. end;
  31293. procedure TTestModule.TestOverflowChecks_Int;
  31294. begin
  31295. Scanner.CurrentBoolSwitches:=Scanner.CurrentBoolSwitches+[bsOverflowChecks];
  31296. StartProgram(false);
  31297. Add([
  31298. 'procedure DoIt;',
  31299. 'var',
  31300. ' b: byte;',
  31301. ' n: nativeint;',
  31302. ' u: nativeuint;',
  31303. ' c: currency;',
  31304. 'begin',
  31305. ' n:=n+n;',
  31306. ' n:=n-n;',
  31307. ' n:=n+b;',
  31308. ' n:=b-n;',
  31309. ' n:=n*n;',
  31310. ' n:=n*u;',
  31311. ' c:=c+b;',
  31312. ' c:=b+c;',
  31313. ' c:=c*b;',
  31314. ' c:=b*c;',
  31315. 'end;',
  31316. 'begin',
  31317. '']);
  31318. ConvertProgram;
  31319. CheckSource('TestOverflowChecks_Int',
  31320. LinesToStr([ // statements
  31321. 'this.DoIt = function () {',
  31322. ' var b = 0;',
  31323. ' var n = 0;',
  31324. ' var u = 0;',
  31325. ' var c = 0;',
  31326. ' n = rtl.oc(n + n);',
  31327. ' n = rtl.oc(n - n);',
  31328. ' n = rtl.oc(n + b);',
  31329. ' n = rtl.oc(b - n);',
  31330. ' n = rtl.oc(n * n);',
  31331. ' n = rtl.oc(n * u);',
  31332. ' c = rtl.oc(c + (b * 10000));',
  31333. ' c = rtl.oc((b * 10000) + c);',
  31334. ' c = rtl.oc(c * b);',
  31335. ' c = rtl.oc(b * c);',
  31336. '};',
  31337. '']),
  31338. LinesToStr([ // $mod.$main
  31339. '']));
  31340. end;
  31341. procedure TTestModule.TestRangeChecks_AssignInt;
  31342. begin
  31343. Scanner.Options:=Scanner.Options+[po_CAssignments];
  31344. StartProgram(false);
  31345. Add([
  31346. '{$R+}',
  31347. 'var',
  31348. ' b: byte = 2;',
  31349. ' w: word = 3;',
  31350. 'procedure DoIt(p: byte);',
  31351. 'begin',
  31352. ' b:=w;',
  31353. ' b+=w;',
  31354. ' b:=1;',
  31355. 'end;',
  31356. '{$R-}',
  31357. 'procedure DoSome;',
  31358. 'begin',
  31359. ' DoIt(w);',
  31360. ' b:=w;',
  31361. ' b:=2;',
  31362. 'end;',
  31363. 'begin',
  31364. '{$R+}',
  31365. '']);
  31366. ConvertProgram;
  31367. CheckSource('TestRangeChecks_AssignInt',
  31368. LinesToStr([ // statements
  31369. 'this.b = 2;',
  31370. 'this.w = 3;',
  31371. 'this.DoIt = function (p) {',
  31372. ' rtl.rc(p, 0, 255);',
  31373. ' $mod.b = rtl.rc($mod.w,0,255);',
  31374. ' rtl.rc($mod.b += $mod.w, 0, 255);',
  31375. ' $mod.b = 1;',
  31376. '};',
  31377. 'this.DoSome = function () {',
  31378. ' $mod.DoIt($mod.w);',
  31379. ' $mod.b = $mod.w;',
  31380. ' $mod.b = 2;',
  31381. '};',
  31382. '']),
  31383. LinesToStr([ // $mod.$main
  31384. '']));
  31385. end;
  31386. procedure TTestModule.TestRangeChecks_AssignIntRange;
  31387. begin
  31388. Scanner.Options:=Scanner.Options+[po_CAssignments];
  31389. StartProgram(false);
  31390. Add([
  31391. '{$R+}',
  31392. 'type Ten = 1..10;',
  31393. 'var',
  31394. ' b: Ten = 2;',
  31395. ' w: Ten = 3;',
  31396. 'procedure DoIt(p: Ten);',
  31397. 'begin',
  31398. ' b:=w;',
  31399. ' b+=w;',
  31400. ' b:=1;',
  31401. 'end;',
  31402. '{$R-}',
  31403. 'procedure DoSome;',
  31404. 'begin',
  31405. ' DoIt(w);',
  31406. ' b:=w;',
  31407. ' b:=2;',
  31408. 'end;',
  31409. 'begin',
  31410. '{$R+}',
  31411. '']);
  31412. ConvertProgram;
  31413. CheckSource('TestRangeChecks_AssignIntRange',
  31414. LinesToStr([ // statements
  31415. 'this.b = 2;',
  31416. 'this.w = 3;',
  31417. 'this.DoIt = function (p) {',
  31418. ' rtl.rc(p, 1, 10);',
  31419. ' $mod.b = rtl.rc($mod.w, 1, 10);',
  31420. ' rtl.rc($mod.b += $mod.w, 1, 10);',
  31421. ' $mod.b = 1;',
  31422. '};',
  31423. 'this.DoSome = function () {',
  31424. ' $mod.DoIt($mod.w);',
  31425. ' $mod.b = $mod.w;',
  31426. ' $mod.b = 2;',
  31427. '};',
  31428. '']),
  31429. LinesToStr([ // $mod.$main
  31430. '']));
  31431. end;
  31432. procedure TTestModule.TestRangeChecks_AssignEnum;
  31433. begin
  31434. StartProgram(false);
  31435. Add([
  31436. '{$R+}',
  31437. 'type TEnum = (red,green);',
  31438. 'var',
  31439. ' e: TEnum = red;',
  31440. 'procedure DoIt(p: TEnum);',
  31441. 'begin',
  31442. ' e:=p;',
  31443. ' p:=TEnum(0);',
  31444. ' p:=succ(e);',
  31445. 'end;',
  31446. '{$R-}',
  31447. 'procedure DoSome;',
  31448. 'begin',
  31449. ' DoIt(e);',
  31450. ' e:=TEnum(1);',
  31451. ' e:=pred(e);',
  31452. 'end;',
  31453. 'begin',
  31454. '{$R+}',
  31455. '']);
  31456. ConvertProgram;
  31457. CheckSource('TestRangeChecks_AssignEnum',
  31458. LinesToStr([ // statements
  31459. 'this.TEnum = {',
  31460. ' "0": "red",',
  31461. ' red: 0,',
  31462. ' "1": "green",',
  31463. ' green: 1',
  31464. '};',
  31465. 'this.e = this.TEnum.red;',
  31466. 'this.DoIt = function (p) {',
  31467. ' rtl.rc(p, 0, 1);',
  31468. ' $mod.e = rtl.rc(p, 0, 1);',
  31469. ' p = 0;',
  31470. ' p = rtl.rc($mod.e + 1, 0, 1);',
  31471. '};',
  31472. 'this.DoSome = function () {',
  31473. ' $mod.DoIt($mod.e);',
  31474. ' $mod.e = 1;',
  31475. ' $mod.e = $mod.e - 1;',
  31476. '};',
  31477. '']),
  31478. LinesToStr([ // $mod.$main
  31479. '']));
  31480. end;
  31481. procedure TTestModule.TestRangeChecks_AssignEnumRange;
  31482. begin
  31483. StartProgram(false);
  31484. Add([
  31485. '{$R+}',
  31486. 'type',
  31487. ' TEnum = (red,green);',
  31488. ' TEnumRg = red..green;',
  31489. 'var',
  31490. ' e: TEnumRg = red;',
  31491. 'procedure DoIt(p: TEnumRg);',
  31492. 'begin',
  31493. ' e:=p;',
  31494. ' p:=TEnumRg(0);',
  31495. ' p:=succ(e);',
  31496. 'end;',
  31497. '{$R-}',
  31498. 'procedure DoSome;',
  31499. 'begin',
  31500. ' DoIt(e);',
  31501. ' e:=TEnum(1);',
  31502. ' e:=pred(e);',
  31503. 'end;',
  31504. 'begin',
  31505. '{$R+}',
  31506. '']);
  31507. ConvertProgram;
  31508. CheckSource('TestRangeChecks_AssignEnumRange',
  31509. LinesToStr([ // statements
  31510. 'this.TEnum = {',
  31511. ' "0": "red",',
  31512. ' red: 0,',
  31513. ' "1": "green",',
  31514. ' green: 1',
  31515. '};',
  31516. 'this.e = this.TEnum.red;',
  31517. 'this.DoIt = function (p) {',
  31518. ' rtl.rc(p, 0, 1);',
  31519. ' $mod.e = rtl.rc(p, 0, 1);',
  31520. ' p = 0;',
  31521. ' p = rtl.rc($mod.e + 1, 0, 1);',
  31522. '};',
  31523. 'this.DoSome = function () {',
  31524. ' $mod.DoIt($mod.e);',
  31525. ' $mod.e = 1;',
  31526. ' $mod.e = $mod.e - 1;',
  31527. '};',
  31528. '']),
  31529. LinesToStr([ // $mod.$main
  31530. '']));
  31531. end;
  31532. procedure TTestModule.TestRangeChecks_AssignChar;
  31533. begin
  31534. StartProgram(false);
  31535. Add([
  31536. '{$R+}',
  31537. 'type',
  31538. ' TLetter = char;',
  31539. 'var',
  31540. ' b: TLetter = ''2'';',
  31541. ' w: TLetter = ''3'';',
  31542. 'procedure DoIt(p: TLetter);',
  31543. 'begin',
  31544. ' b:=w;',
  31545. ' b:=''1'';',
  31546. 'end;',
  31547. '{$R-}',
  31548. 'procedure DoSome;',
  31549. 'begin',
  31550. ' DoIt(w);',
  31551. ' b:=w;',
  31552. ' b:=''2'';',
  31553. 'end;',
  31554. 'begin',
  31555. '{$R+}',
  31556. '']);
  31557. ConvertProgram;
  31558. CheckSource('TestRangeChecks_AssignChar',
  31559. LinesToStr([ // statements
  31560. 'this.b = "2";',
  31561. 'this.w = "3";',
  31562. 'this.DoIt = function (p) {',
  31563. ' rtl.rcc(p, 0, 65535);',
  31564. ' $mod.b = rtl.rcc($mod.w, 0, 65535);',
  31565. ' $mod.b = "1";',
  31566. '};',
  31567. 'this.DoSome = function () {',
  31568. ' $mod.DoIt($mod.w);',
  31569. ' $mod.b = $mod.w;',
  31570. ' $mod.b = "2";',
  31571. '};',
  31572. '']),
  31573. LinesToStr([ // $mod.$main
  31574. '']));
  31575. end;
  31576. procedure TTestModule.TestRangeChecks_AssignCharRange;
  31577. begin
  31578. StartProgram(false);
  31579. Add([
  31580. '{$R+}',
  31581. 'type TDigit = ''0''..''9'';',
  31582. 'var',
  31583. ' b: TDigit = ''2'';',
  31584. ' w: TDigit = ''3'';',
  31585. 'procedure DoIt(p: TDigit);',
  31586. 'begin',
  31587. ' b:=w;',
  31588. ' b:=''1'';',
  31589. 'end;',
  31590. '{$R-}',
  31591. 'procedure DoSome;',
  31592. 'begin',
  31593. ' DoIt(w);',
  31594. ' b:=w;',
  31595. ' b:=''2'';',
  31596. 'end;',
  31597. 'begin',
  31598. '{$R+}',
  31599. '']);
  31600. ConvertProgram;
  31601. CheckSource('TestRangeChecks_AssignCharRange',
  31602. LinesToStr([ // statements
  31603. 'this.b = "2";',
  31604. 'this.w = "3";',
  31605. 'this.DoIt = function (p) {',
  31606. ' rtl.rcc(p, 48, 57);',
  31607. ' $mod.b = rtl.rcc($mod.w, 48, 57);',
  31608. ' $mod.b = "1";',
  31609. '};',
  31610. 'this.DoSome = function () {',
  31611. ' $mod.DoIt($mod.w);',
  31612. ' $mod.b = $mod.w;',
  31613. ' $mod.b = "2";',
  31614. '};',
  31615. '']),
  31616. LinesToStr([ // $mod.$main
  31617. '']));
  31618. end;
  31619. procedure TTestModule.TestRangeChecks_ArrayIndex;
  31620. begin
  31621. StartProgram(false);
  31622. Add([
  31623. '{$R+}',
  31624. 'type',
  31625. ' Ten = 1..10;',
  31626. ' TArr = array of Ten;',
  31627. ' TArrArr = array of TArr;',
  31628. ' TArrByte = array[byte] of Ten;',
  31629. ' TArrChar = array[''0''..''9''] of Ten;',
  31630. ' TArrByteChar = array[byte,''0''..''9''] of Ten;',
  31631. ' TObject = class',
  31632. ' A: TArr;',
  31633. ' end;',
  31634. 'procedure DoIt;',
  31635. 'var',
  31636. ' Arr: TArr;',
  31637. ' ArrArr: TArrArr;',
  31638. ' ArrByte: TArrByte;',
  31639. ' ArrChar: TArrChar;',
  31640. ' ArrByteChar: TArrByteChar;',
  31641. ' i: Ten;',
  31642. ' c: char;',
  31643. ' o: tobject;',
  31644. 'begin',
  31645. ' i:=Arr[1];',
  31646. ' i:=ArrByteChar[1,''2''];',
  31647. ' Arr[1]:=Arr[1];',
  31648. ' Arr[i]:=Arr[i];',
  31649. ' ArrByte[3]:=ArrByte[3];',
  31650. ' ArrByte[i]:=ArrByte[i];',
  31651. ' ArrChar[''5'']:=ArrChar[''5''];',
  31652. ' ArrChar[c]:=ArrChar[c];',
  31653. ' ArrByteChar[7,''7'']:=ArrByteChar[7,''7''];',
  31654. ' ArrByteChar[i,c]:=ArrByteChar[i,c];',
  31655. ' o.a[i]:=o.a[i];',
  31656. 'end;',
  31657. 'begin',
  31658. '']);
  31659. ConvertProgram;
  31660. CheckSource('TestRangeChecks_ArrayIndex',
  31661. LinesToStr([ // statements
  31662. 'rtl.createClass(this, "TObject", null, function () {',
  31663. ' this.$init = function () {',
  31664. ' this.A = [];',
  31665. ' };',
  31666. ' this.$final = function () {',
  31667. ' this.A = undefined;',
  31668. ' };',
  31669. '});',
  31670. 'this.DoIt = function () {',
  31671. ' var Arr = [];',
  31672. ' var ArrArr = [];',
  31673. ' var ArrByte = rtl.arraySetLength(null, 0, 256);',
  31674. ' var ArrChar = rtl.arraySetLength(null, 0, 10);',
  31675. ' var ArrByteChar = rtl.arraySetLength(null, 0, 256, 10);',
  31676. ' var i = 0;',
  31677. ' var c = "";',
  31678. ' var o = null;',
  31679. ' i = rtl.rc(Arr[1], 1, 10);',
  31680. ' i = rtl.rc(ArrByteChar[1][2], 1, 10);',
  31681. ' Arr[1] = rtl.rc(Arr[1], 1, 10);',
  31682. ' rtl.rcArrW(Arr, i, rtl.rcArrR(Arr, i));',
  31683. ' ArrByte[3] = rtl.rc(ArrByte[3], 1, 10);',
  31684. ' rtl.rcArrW(ArrByte, i, rtl.rcArrR(ArrByte, i));',
  31685. ' ArrChar[5] = rtl.rc(ArrChar[5], 1, 10);',
  31686. ' rtl.rcArrW(ArrChar, c.charCodeAt() - 48, rtl.rcArrR(ArrChar, c.charCodeAt() - 48));',
  31687. ' ArrByteChar[7][7] = rtl.rc(ArrByteChar[7][7], 1, 10);',
  31688. ' rtl.rcArrW(ArrByteChar, i, c.charCodeAt() - 48, rtl.rcArrR(ArrByteChar, i, c.charCodeAt() - 48));',
  31689. ' rtl.rcArrW(o.A, i, rtl.rcArrR(o.A, i));',
  31690. '};',
  31691. '']),
  31692. LinesToStr([ // $mod.$main
  31693. '']));
  31694. end;
  31695. procedure TTestModule.TestRangeChecks_ArrayOfRecIndex;
  31696. begin
  31697. StartProgram(false);
  31698. Add([
  31699. '{$R+}',
  31700. 'type',
  31701. ' Ten = 1..10;',
  31702. ' TRec = record x: Ten end;',
  31703. ' TArr = array of TRec;',
  31704. ' TArrArr = array of TArr;',
  31705. ' TObject = class',
  31706. ' A: TArr;',
  31707. ' end;',
  31708. 'procedure DoIt;',
  31709. 'var',
  31710. ' Arr: TArr;',
  31711. ' ArrArr: TArrArr;',
  31712. ' i: Ten;',
  31713. ' o: tobject;',
  31714. 'begin',
  31715. ' Arr[1]:=Arr[1];',
  31716. ' Arr[i]:=Arr[i+1];',
  31717. ' o.a[i]:=o.a[i+2];',
  31718. 'end;',
  31719. 'begin',
  31720. '']);
  31721. ConvertProgram;
  31722. CheckSource('TestRangeChecks_ArrayOfRecIndex',
  31723. LinesToStr([ // statements
  31724. 'rtl.recNewT(this, "TRec", function () {',
  31725. ' this.x = 0;',
  31726. ' this.$eq = function (b) {',
  31727. ' return this.x === b.x;',
  31728. ' };',
  31729. ' this.$assign = function (s) {',
  31730. ' this.x = s.x;',
  31731. ' return this;',
  31732. ' };',
  31733. '});',
  31734. 'rtl.createClass(this, "TObject", null, function () {',
  31735. ' this.$init = function () {',
  31736. ' this.A = [];',
  31737. ' };',
  31738. ' this.$final = function () {',
  31739. ' this.A = undefined;',
  31740. ' };',
  31741. '});',
  31742. 'this.DoIt = function () {',
  31743. ' var Arr = [];',
  31744. ' var ArrArr = [];',
  31745. ' var i = 0;',
  31746. ' var o = null;',
  31747. ' Arr[1].$assign(Arr[1]);',
  31748. ' rtl.rcArrR(Arr, i).$assign(rtl.rcArrR(Arr, i + 1));',
  31749. ' rtl.rcArrR(o.A, i).$assign(rtl.rcArrR(o.A, i + 2));',
  31750. '};',
  31751. '']),
  31752. LinesToStr([ // $mod.$main
  31753. '']));
  31754. end;
  31755. procedure TTestModule.TestRangeChecks_StringIndex;
  31756. begin
  31757. StartProgram(false);
  31758. Add([
  31759. 'type',
  31760. ' TObject = class',
  31761. ' S: string;',
  31762. ' end;',
  31763. '{$R+}',
  31764. 'procedure DoIt(var h: string);',
  31765. 'var',
  31766. ' s: string;',
  31767. ' i: longint;',
  31768. ' c: char;',
  31769. ' o: tobject;',
  31770. 'begin',
  31771. ' c:=s[1];',
  31772. ' s[i]:=s[i];',
  31773. ' h[i]:=h[i];',
  31774. ' c:=o.s[i];',
  31775. ' o.s[i]:=c;',
  31776. 'end;',
  31777. 'begin',
  31778. '']);
  31779. ConvertProgram;
  31780. CheckSource('TestRangeChecks_StringIndex',
  31781. LinesToStr([ // statements
  31782. 'rtl.createClass(this, "TObject", null, function () {',
  31783. ' this.$init = function () {',
  31784. ' this.S = "";',
  31785. ' };',
  31786. ' this.$final = function () {',
  31787. ' };',
  31788. '});',
  31789. 'this.DoIt = function (h) {',
  31790. ' var s = "";',
  31791. ' var i = 0;',
  31792. ' var c = "";',
  31793. ' var o = null;',
  31794. ' c = rtl.rcc(rtl.rcCharAt(s, 0), 0, 65535);',
  31795. ' s = rtl.rcSetCharAt(s, i - 1, rtl.rcCharAt(s, i - 1));',
  31796. ' h.set(rtl.rcSetCharAt(h.get(), i - 1, rtl.rcCharAt(h.get(), i - 1)));',
  31797. ' c = rtl.rcc(rtl.rcCharAt(o.S, i - 1), 0, 65535);',
  31798. ' o.S = rtl.rcSetCharAt(o.S, i - 1, c);',
  31799. '};',
  31800. '']),
  31801. LinesToStr([ // $mod.$main
  31802. '']));
  31803. end;
  31804. procedure TTestModule.TestRangeChecks_TypecastInt;
  31805. begin
  31806. StartProgram(false);
  31807. Add([
  31808. '{$R+}',
  31809. 'var',
  31810. ' i: nativeint;',
  31811. ' b: byte;',
  31812. ' sh: shortint;',
  31813. ' w: word;',
  31814. ' sm: smallint;',
  31815. ' lw: longword;',
  31816. ' li: longint;',
  31817. 'begin',
  31818. ' b:=12+byte(i);',
  31819. ' sh:=12+shortint(i);',
  31820. ' w:=12+word(i);',
  31821. ' sm:=12+smallint(i);',
  31822. ' lw:=12+longword(i);',
  31823. ' li:=12+longint(i);',
  31824. '']);
  31825. ConvertProgram;
  31826. CheckSource('TestRangeChecks_TypecastInt',
  31827. LinesToStr([
  31828. 'this.i = 0;',
  31829. 'this.b = 0;',
  31830. 'this.sh = 0;',
  31831. 'this.w = 0;',
  31832. 'this.sm = 0;',
  31833. 'this.lw = 0;',
  31834. 'this.li = 0;',
  31835. '']),
  31836. LinesToStr([
  31837. '$mod.b = rtl.rc(12 + rtl.rc($mod.i, 0, 255), 0, 255);',
  31838. '$mod.sh = rtl.rc(12 + rtl.rc($mod.i, -128, 127), -128, 127);',
  31839. '$mod.w = rtl.rc(12 + rtl.rc($mod.i, 0, 65535), 0, 65535);',
  31840. '$mod.sm = rtl.rc(12 + rtl.rc($mod.i, -32768, 32767), -32768, 32767);',
  31841. '$mod.lw = rtl.rc(12 + rtl.rc($mod.i, 0, 4294967295), 0, 4294967295);',
  31842. '$mod.li = rtl.rc(12 + rtl.rc($mod.i, -2147483648, 2147483647), -2147483648, 2147483647);',
  31843. '']));
  31844. end;
  31845. procedure TTestModule.TestRangeChecks_TypeHelperInt;
  31846. begin
  31847. Scanner.Options:=Scanner.Options+[po_CAssignments];
  31848. StartProgram(false);
  31849. Add([
  31850. '{$modeswitch typehelpers}',
  31851. '{$R+}',
  31852. 'type',
  31853. ' TObject = class',
  31854. ' FSize: byte;',
  31855. ' property Size: byte read FSize;',
  31856. ' end;',
  31857. ' THelper = type helper for byte',
  31858. ' procedure SetIt(w: word);',
  31859. ' end;',
  31860. 'procedure THelper.SetIt(w: word);',
  31861. 'begin',
  31862. ' Self:=w;',
  31863. 'end;',
  31864. 'function GetIt: byte;',
  31865. 'begin',
  31866. ' Result.SetIt(2);',
  31867. 'end;',
  31868. 'var',
  31869. ' b: byte = 3;',
  31870. ' o: TObject;',
  31871. 'begin',
  31872. ' b.SetIt(14);',
  31873. ' with b do SetIt(15);',
  31874. ' o.Size.SetIt(16);',
  31875. '']);
  31876. ConvertProgram;
  31877. CheckSource('TestRangeChecks_AssignInt',
  31878. LinesToStr([ // statements
  31879. 'rtl.createClass(this, "TObject", null, function () {',
  31880. ' this.$init = function () {',
  31881. ' this.FSize = 0;',
  31882. ' };',
  31883. ' this.$final = function () {',
  31884. ' };',
  31885. '});',
  31886. 'rtl.createHelper(this, "THelper", null, function () {',
  31887. ' this.SetIt = function (w) {',
  31888. ' rtl.rc(w, 0, 65535);',
  31889. ' this.set(w);',
  31890. ' };',
  31891. '});',
  31892. 'this.GetIt = function () {',
  31893. ' var Result = 0;',
  31894. ' $mod.THelper.SetIt.call({',
  31895. ' get: function () {',
  31896. ' return Result;',
  31897. ' },',
  31898. ' set: function (v) {',
  31899. ' rtl.rc(v, 0, 255);',
  31900. ' Result = v;',
  31901. ' }',
  31902. ' }, 2);',
  31903. ' return Result;',
  31904. '};',
  31905. 'this.b = 3;',
  31906. 'this.o = null;',
  31907. '']),
  31908. LinesToStr([ // $mod.$main
  31909. '$mod.THelper.SetIt.call({',
  31910. ' p: $mod,',
  31911. ' get: function () {',
  31912. ' return this.p.b;',
  31913. ' },',
  31914. ' set: function (v) {',
  31915. ' rtl.rc(v, 0, 255);',
  31916. ' this.p.b = v;',
  31917. ' }',
  31918. '}, 14);',
  31919. 'var $with = $mod.b;',
  31920. '$mod.THelper.SetIt.call({',
  31921. ' get: function () {',
  31922. ' return $with;',
  31923. ' },',
  31924. ' set: function (v) {',
  31925. ' rtl.rc(v, 0, 255);',
  31926. ' $with = v;',
  31927. ' }',
  31928. '}, 15);',
  31929. '$mod.THelper.SetIt.call({',
  31930. ' p: $mod.o,',
  31931. ' get: function () {',
  31932. ' return this.p.FSize;',
  31933. ' },',
  31934. ' set: function (v) {',
  31935. ' rtl.rc(v, 0, 255);',
  31936. ' this.p.FSize = v;',
  31937. ' }',
  31938. '}, 16);',
  31939. '']));
  31940. end;
  31941. procedure TTestModule.TestAsync_Proc;
  31942. begin
  31943. StartProgram(false);
  31944. Add([
  31945. 'procedure Fly(w: word = 1); async; forward;',
  31946. 'procedure Run(w: word = 2); async;',
  31947. 'begin',
  31948. ' Fly(w);',
  31949. ' Fly;',
  31950. ' await(Fly(w));',
  31951. ' await(Fly);',
  31952. 'end;',
  31953. 'procedure Fly(w: word); ',
  31954. 'begin',
  31955. 'end;',
  31956. 'begin',
  31957. ' Run;',
  31958. ' Run(3);',
  31959. '']);
  31960. CheckResolverUnexpectedHints();
  31961. ConvertProgram;
  31962. CheckSource('TestAsync_Proc',
  31963. LinesToStr([ // statements
  31964. 'this.Run = async function (w) {',
  31965. ' $mod.Fly(w);',
  31966. ' $mod.Fly(1);',
  31967. ' await $mod.Fly(w);',
  31968. ' await $mod.Fly(1);',
  31969. '};',
  31970. 'this.Fly = async function (w) {',
  31971. '};',
  31972. '']),
  31973. LinesToStr([
  31974. '$mod.Run(2);',
  31975. '$mod.Run(3);',
  31976. '']));
  31977. end;
  31978. procedure TTestModule.TestAsync_CallResultIsPromise;
  31979. begin
  31980. StartProgram(false);
  31981. Add([
  31982. '{$modeswitch externalclass}',
  31983. 'type',
  31984. ' TObject = class',
  31985. ' end;',
  31986. ' TJSPromise = class external name ''Promise''',
  31987. ' end;',
  31988. ' TBird = class',
  31989. ' function Fly: word; async; ',
  31990. ' end;',
  31991. 'function TBird.Fly: word; async; ',
  31992. 'begin',
  31993. ' Result:=3;',
  31994. ' Fly:=4+Result;',
  31995. ' if Result=5 then ;',
  31996. ' exit(6);',
  31997. 'end;',
  31998. 'function Run: word; async;',
  31999. 'begin',
  32000. ' Result:=11+Result;',
  32001. ' inc(Result);',
  32002. 'end;',
  32003. 'var',
  32004. ' p: TJSPromise;',
  32005. ' o: TBird;',
  32006. 'begin',
  32007. ' p:=Run;',
  32008. ' p:=Run();',
  32009. ' if Run=p then ;',
  32010. ' if p=Run then ;',
  32011. ' if Run()=p then ;',
  32012. ' if p=Run() then ;',
  32013. ' p:=o.Fly;',
  32014. ' p:=o.Fly();',
  32015. ' if o.Fly=p then ;',
  32016. ' if o.Fly()=p then ;',
  32017. ' with o do begin',
  32018. ' p:=Fly;',
  32019. ' p:=Fly();',
  32020. ' if Fly=p then ;',
  32021. ' if Fly()=p then ;',
  32022. ' end;',
  32023. '']);
  32024. CheckResolverUnexpectedHints();
  32025. ConvertProgram;
  32026. CheckSource('TestAsync_CallResultIsPromise',
  32027. LinesToStr([ // statements
  32028. 'rtl.createClass(this, "TObject", null, function () {',
  32029. ' this.$init = function () {',
  32030. ' };',
  32031. ' this.$final = function () {',
  32032. ' };',
  32033. '});',
  32034. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  32035. ' this.Fly = async function () {',
  32036. ' var Result = 0;',
  32037. ' Result = 3;',
  32038. ' Result = 4 + Result;',
  32039. ' if (Result === 5) ;',
  32040. ' return 6;',
  32041. ' return Result;',
  32042. ' };',
  32043. '});',
  32044. 'this.Run = async function () {',
  32045. ' var Result = 0;',
  32046. ' Result = 11 + Result;',
  32047. ' Result += 1;',
  32048. ' return Result;',
  32049. '};',
  32050. 'this.p = null;',
  32051. 'this.o = null;',
  32052. '']),
  32053. LinesToStr([
  32054. '$mod.p = $mod.Run();',
  32055. '$mod.p = $mod.Run();',
  32056. 'if ($mod.Run() === $mod.p) ;',
  32057. 'if ($mod.p === $mod.Run()) ;',
  32058. 'if ($mod.Run() === $mod.p) ;',
  32059. 'if ($mod.p === $mod.Run()) ;',
  32060. '$mod.p = $mod.o.Fly();',
  32061. '$mod.p = $mod.o.Fly();',
  32062. 'if ($mod.o.Fly() === $mod.p) ;',
  32063. 'if ($mod.o.Fly() === $mod.p) ;',
  32064. 'var $with = $mod.o;',
  32065. '$mod.p = $with.Fly();',
  32066. '$mod.p = $with.Fly();',
  32067. 'if ($with.Fly() === $mod.p) ;',
  32068. 'if ($with.Fly() === $mod.p) ;',
  32069. '']));
  32070. end;
  32071. procedure TTestModule.TestAsync_ConstructorFail;
  32072. begin
  32073. StartProgram(false);
  32074. Add([
  32075. 'type',
  32076. ' TObject = class',
  32077. ' end;',
  32078. ' TBird = class',
  32079. ' constructor Create; async;',
  32080. ' end;',
  32081. 'constructor TBird.Create; async;',
  32082. 'begin',
  32083. 'end;',
  32084. 'begin',
  32085. '']);
  32086. SetExpectedPasResolverError('Invalid constructor modifier async',nInvalidXModifierY);
  32087. ConvertProgram;
  32088. end;
  32089. procedure TTestModule.TestAsync_PropertyGetterFail;
  32090. begin
  32091. StartProgram(false);
  32092. Add([
  32093. 'type',
  32094. ' TObject = class',
  32095. ' end;',
  32096. ' TBird = class',
  32097. ' function GetSize: word; async;',
  32098. ' property Size: word read GetSize;',
  32099. ' end;',
  32100. 'function TBird.GetSize: word; async;',
  32101. 'begin',
  32102. 'end;',
  32103. 'begin',
  32104. '']);
  32105. SetExpectedPasResolverError('Invalid property getter modifier async',nInvalidXModifierY);
  32106. ConvertProgram;
  32107. end;
  32108. procedure TTestModule.TestAwait_NonPromiseWithTypeFail;
  32109. begin
  32110. StartProgram(false);
  32111. Add([
  32112. 'procedure Run; async;',
  32113. 'begin',
  32114. ' await(word,1);',
  32115. 'end;',
  32116. 'begin',
  32117. '']);
  32118. SetExpectedPasResolverError('Incompatible type arg no. 2: Got "Longint", expected "TJSPromise"',nIncompatibleTypeArgNo);
  32119. ConvertProgram;
  32120. end;
  32121. procedure TTestModule.TestAwait_AsyncCallTypeMismatch;
  32122. begin
  32123. StartProgram(false);
  32124. Add([
  32125. 'type',
  32126. ' TObject = class',
  32127. ' end;',
  32128. ' TBird = class',
  32129. ' end;',
  32130. 'function Fly: TObject; async;',
  32131. 'begin',
  32132. 'end;',
  32133. 'procedure Run; async;',
  32134. 'begin',
  32135. ' await(TBird,Fly);',
  32136. 'end;',
  32137. 'begin',
  32138. '']);
  32139. SetExpectedPasResolverError('Incompatible type arg no. 2: Got "TObject", expected "TBird"',nIncompatibleTypeArgNo);
  32140. ConvertProgram;
  32141. end;
  32142. procedure TTestModule.TestAWait_OutsideAsyncFail;
  32143. begin
  32144. StartProgram(false);
  32145. Add([
  32146. 'procedure Crawl(w: double); ',
  32147. 'begin',
  32148. 'end;',
  32149. 'procedure Run(w: double);',
  32150. 'begin',
  32151. ' await(Crawl(w));',
  32152. 'end;',
  32153. 'begin',
  32154. ' Run(1);']);
  32155. SetExpectedPasResolverError(sAWaitOnlyInAsyncProcedure,nAWaitOnlyInAsyncProcedure);
  32156. ConvertProgram;
  32157. end;
  32158. procedure TTestModule.TestAWait_IntegerFail;
  32159. begin
  32160. StartProgram(false);
  32161. Add([
  32162. 'function Run: word;',
  32163. 'begin',
  32164. 'end;',
  32165. 'procedure Fly(w: word); async;',
  32166. 'begin',
  32167. ' await(Run());',
  32168. 'end;',
  32169. 'begin',
  32170. ' Fly(1);']);
  32171. SetExpectedPasResolverError('async function expected, but Result:Word found',nXExpectedButYFound);
  32172. ConvertProgram;
  32173. end;
  32174. procedure TTestModule.TestAWait_ExternalClassPromise;
  32175. begin
  32176. StartProgram(false);
  32177. Add([
  32178. '{$modeswitch externalclass}',
  32179. 'type',
  32180. ' TJSPromise = class external name ''Promise''',
  32181. ' end;',
  32182. ' TJSThenable = class external name ''Thenable''',
  32183. ' end;',
  32184. 'function Fly(w: word): TJSPromise;',
  32185. 'begin',
  32186. 'end;',
  32187. 'function Jump(w: word): word; async;',
  32188. 'begin',
  32189. 'end;',
  32190. 'function Eat(w: word): TJSPromise; async;',
  32191. 'begin',
  32192. 'end;',
  32193. 'function Run(d: double): word; async;',
  32194. 'var',
  32195. ' p: TJSPromise;',
  32196. 'begin',
  32197. ' Result:=await(word,p);', // promise needs type
  32198. ' Result:=await(word,Fly(3));', // promise needs type
  32199. ' Result:=await(Jump(4));', // async non promise must omit the type
  32200. ' Result:=await(word,Jump(5));', // async call can provide fitting type
  32201. ' Result:=await(word,Eat(6));', // promise needs type
  32202. 'end;',
  32203. 'begin',
  32204. '']);
  32205. ConvertProgram;
  32206. CheckSource('TestAWait_ExternalClassPromise',
  32207. LinesToStr([ // statements
  32208. 'this.Fly = function (w) {',
  32209. ' var Result = null;',
  32210. ' return Result;',
  32211. '};',
  32212. 'this.Jump = async function (w) {',
  32213. ' var Result = 0;',
  32214. ' return Result;',
  32215. '};',
  32216. 'this.Eat = async function (w) {',
  32217. ' var Result = null;',
  32218. ' return Result;',
  32219. '};',
  32220. 'this.Run = async function (d) {',
  32221. ' var Result = 0;',
  32222. ' var p = null;',
  32223. ' Result = await p;',
  32224. ' Result = await $mod.Fly(3);',
  32225. ' Result = await $mod.Jump(4);',
  32226. ' Result = await $mod.Jump(5);',
  32227. ' Result = await $mod.Eat(6);',
  32228. ' return Result;',
  32229. '};',
  32230. '']),
  32231. LinesToStr([
  32232. ]));
  32233. CheckResolverUnexpectedHints();
  32234. end;
  32235. procedure TTestModule.TestAWait_JSValue;
  32236. begin
  32237. StartProgram(false);
  32238. Add([
  32239. '{$modeswitch externalclass}',
  32240. 'type',
  32241. ' TJSPromise = class external name ''Promise''',
  32242. ' end;',
  32243. 'function Fly(w: word): jsvalue; async;',
  32244. 'begin',
  32245. 'end;',
  32246. 'function Run(d: jsvalue; var e): word; async;',
  32247. 'begin',
  32248. ' Result:=await(word,d);', // promise needs type
  32249. ' d:=await(Fly(4));', // async non promise must omit the type
  32250. ' Result:=await(word,e);', // promise needs type
  32251. 'end;',
  32252. 'begin',
  32253. '']);
  32254. ConvertProgram;
  32255. CheckSource('TestAWait_JSValue',
  32256. LinesToStr([ // statements
  32257. 'this.Fly = async function (w) {',
  32258. ' var Result = undefined;',
  32259. ' return Result;',
  32260. '};',
  32261. 'this.Run = async function (d, e) {',
  32262. ' var Result = 0;',
  32263. ' Result = await d;',
  32264. ' d = await $mod.Fly(4);',
  32265. ' Result = await e.get();',
  32266. ' return Result;',
  32267. '};',
  32268. '']),
  32269. LinesToStr([
  32270. ]));
  32271. CheckResolverUnexpectedHints();
  32272. end;
  32273. procedure TTestModule.TestAWait_Result;
  32274. begin
  32275. StartProgram(false);
  32276. Add([
  32277. '{$modeswitch externalclass}',
  32278. 'type',
  32279. ' TJSPromise = class external name ''Promise''',
  32280. ' end;',
  32281. 'function Crawl(d: double = 1.3): TJSPromise; ',
  32282. 'begin',
  32283. 'end;',
  32284. 'function Run(d: double = 1.6): word; async;',
  32285. 'begin',
  32286. ' Result:=await(word,Crawl);',
  32287. ' Result:=await(word,Crawl(4.5));',
  32288. ' Result:=await(Run);',
  32289. ' Result:=await(Run(6.7));',
  32290. 'end;',
  32291. 'begin',
  32292. ' Run(1);']);
  32293. ConvertProgram;
  32294. CheckSource('TestAWait_Result',
  32295. LinesToStr([ // statements
  32296. 'this.Crawl = function (d) {',
  32297. ' var Result = null;',
  32298. ' return Result;',
  32299. '};',
  32300. 'this.Run = async function (d) {',
  32301. ' var Result = 0;',
  32302. ' Result = await $mod.Crawl(1.3);',
  32303. ' Result = await $mod.Crawl(4.5);',
  32304. ' Result = await $mod.Run(1.6);',
  32305. ' Result = await $mod.Run(6.7);',
  32306. ' return Result;',
  32307. '};',
  32308. '']),
  32309. LinesToStr([
  32310. '$mod.Run(1);'
  32311. ]));
  32312. CheckResolverUnexpectedHints();
  32313. end;
  32314. procedure TTestModule.TestAWait_ResultPromiseMissingTypeFail;
  32315. begin
  32316. StartProgram(false);
  32317. Add([
  32318. '{$mode objfpc}',
  32319. '{$modeswitch externalclass}',
  32320. 'type',
  32321. ' TJSPromise = class external name ''Promise''',
  32322. ' end;',
  32323. 'function Run: TJSPromise; async;',
  32324. 'begin',
  32325. 'end;',
  32326. 'procedure Fly(w: word); async;',
  32327. 'begin',
  32328. ' await(Run());',
  32329. 'end;',
  32330. 'begin',
  32331. ' Fly(1);']);
  32332. SetExpectedPasResolverError('Wrong number of parameters specified for call to "function await(aType,TJSPromise):aType"',
  32333. nWrongNumberOfParametersForCallTo);
  32334. ConvertProgram;
  32335. end;
  32336. procedure TTestModule.TestAsync_AnonymousProc;
  32337. begin
  32338. StartProgram(false);
  32339. Add([
  32340. '{$mode objfpc}',
  32341. '{$modeswitch externalclass}',
  32342. 'type',
  32343. ' TJSPromise = class external name ''Promise''',
  32344. ' end;',
  32345. 'type',
  32346. ' TFunc = reference to function(x: double): word; async;',
  32347. 'function Crawl(d: double = 1.3): word; async;',
  32348. 'begin',
  32349. 'end;',
  32350. 'var Func: TFunc;',
  32351. 'begin',
  32352. ' Func:=function(c:double):word async begin',
  32353. ' Result:=await(Crawl(c));',
  32354. ' end;',
  32355. ' Func:=function(c:double):word async assembler asm',
  32356. ' end;',
  32357. ' ']);
  32358. ConvertProgram;
  32359. CheckSource('TestAsync_AnonymousProc',
  32360. LinesToStr([ // statements
  32361. 'this.Crawl = async function (d) {',
  32362. ' var Result = 0;',
  32363. ' return Result;',
  32364. '};',
  32365. 'this.Func = null;',
  32366. '']),
  32367. LinesToStr([
  32368. '$mod.Func = async function (c) {',
  32369. ' var Result = 0;',
  32370. ' Result = await $mod.Crawl(c);',
  32371. ' return Result;',
  32372. '};',
  32373. '$mod.Func = async function (c) {',
  32374. '};',
  32375. '']));
  32376. CheckResolverUnexpectedHints();
  32377. end;
  32378. procedure TTestModule.TestAsync_ProcType;
  32379. begin
  32380. StartProgram(false);
  32381. Add([
  32382. '{$mode objfpc}',
  32383. 'type',
  32384. ' TRefFunc = reference to function(x: double = 1.3): word; async;',
  32385. ' TFunc = function(x: double = 1.1): word; async;',
  32386. ' TProc = procedure(x: longint = 7); async;',
  32387. 'function Crawl(d: double): word; async;',
  32388. 'begin',
  32389. 'end;',
  32390. 'procedure Run(e:longint); async;',
  32391. 'begin',
  32392. 'end;',
  32393. 'procedure Fly(p: TProc); async;',
  32394. 'begin',
  32395. ' await(p);',
  32396. ' await(p());',
  32397. 'end;',
  32398. 'var',
  32399. ' RefFunc: TRefFunc;',
  32400. ' Func: TFunc;',
  32401. ' Proc, ProcB: TProc;',
  32402. 'begin',
  32403. ' Func:=@Crawl;',
  32404. ' RefFunc:=@Crawl;',
  32405. ' RefFunc:=function(c:double):word async begin',
  32406. ' Result:=await(RefFunc);',
  32407. ' Result:=await(RefFunc());',
  32408. ' Result:=await(Func);',
  32409. ' Result:=await(Func());',
  32410. ' await(Proc);',
  32411. ' await(Proc());',
  32412. ' await(Proc(13));',
  32413. ' end;',
  32414. ' Proc:=@Run;',
  32415. ' if Proc=ProcB then ;',
  32416. ' ']);
  32417. ConvertProgram;
  32418. CheckResolverUnexpectedHints();
  32419. CheckSource('TestAsync_ProcType',
  32420. LinesToStr([ // statements
  32421. 'this.Crawl = async function (d) {',
  32422. ' var Result = 0;',
  32423. ' return Result;',
  32424. '};',
  32425. 'this.Run = async function (e) {',
  32426. '};',
  32427. 'this.Fly = async function (p) {',
  32428. ' await p(7);',
  32429. ' await p(7);',
  32430. '};',
  32431. 'this.RefFunc = null;',
  32432. 'this.Func = null;',
  32433. 'this.Proc = null;',
  32434. 'this.ProcB = null;',
  32435. '']),
  32436. LinesToStr([
  32437. '$mod.Func = $mod.Crawl;',
  32438. '$mod.RefFunc = $mod.Crawl;',
  32439. '$mod.RefFunc = async function (c) {',
  32440. ' var Result = 0;',
  32441. ' Result = await $mod.RefFunc(1.3);',
  32442. ' Result = await $mod.RefFunc(1.3);',
  32443. ' Result = await $mod.Func(1.1);',
  32444. ' Result = await $mod.Func(1.1);',
  32445. ' await $mod.Proc(7);',
  32446. ' await $mod.Proc(7);',
  32447. ' await $mod.Proc(13);',
  32448. ' return Result;',
  32449. '};',
  32450. '$mod.Proc = $mod.Run;',
  32451. 'if (rtl.eqCallback($mod.Proc, $mod.ProcB)) ;',
  32452. '']));
  32453. end;
  32454. procedure TTestModule.TestAsync_ProcTypeAsyncModMismatchFail;
  32455. begin
  32456. StartProgram(false);
  32457. Add([
  32458. '{$mode objfpc}',
  32459. 'type',
  32460. ' TRefFunc = reference to function(x: double = 1.3): word;',
  32461. 'function Crawl(d: double): word; async;',
  32462. 'begin',
  32463. 'end;',
  32464. 'var',
  32465. ' RefFunc: TRefFunc;',
  32466. 'begin',
  32467. ' RefFunc:=@Crawl;',
  32468. ' ']);
  32469. SetExpectedPasResolverError('procedure type modifier "async" mismatch',nXModifierMismatchY);
  32470. ConvertProgram;
  32471. end;
  32472. procedure TTestModule.TestAsync_Inherited;
  32473. begin
  32474. StartProgram(false);
  32475. Add([
  32476. '{$mode objfpc}',
  32477. '{$modeswitch externalclass}',
  32478. 'type',
  32479. ' TJSPromise = class external name ''Promise''',
  32480. ' end;',
  32481. ' TObject = class',
  32482. ' function Run(w: word = 3): word; async; virtual;',
  32483. ' end;',
  32484. ' TBird = class',
  32485. ' function Run(w: word = 3): word; async; override;',
  32486. ' end;',
  32487. 'function TObject.Run(w: word = 3): word; async;',
  32488. 'begin',
  32489. 'end;',
  32490. 'function TBird.Run(w: word = 3): word;', // async modifier not needed in impl
  32491. 'var p: TJSPromise;',
  32492. 'begin',
  32493. ' p:=inherited;',
  32494. ' p:=inherited Run;',
  32495. ' p:=inherited Run();',
  32496. ' p:=inherited Run(4);',
  32497. ' exit(p);',
  32498. ' exit(inherited);',
  32499. ' exit(inherited Run);',
  32500. ' exit(inherited Run(5));',
  32501. ' exit(6);',
  32502. 'end;',
  32503. 'begin',
  32504. ' ']);
  32505. ConvertProgram;
  32506. CheckSource('TestAsync_Inherited',
  32507. LinesToStr([ // statements
  32508. 'rtl.createClass(this, "TObject", null, function () {',
  32509. ' this.$init = function () {',
  32510. ' };',
  32511. ' this.$final = function () {',
  32512. ' };',
  32513. ' this.Run = async function (w) {',
  32514. ' var Result = 0;',
  32515. ' return Result;',
  32516. ' };',
  32517. '});',
  32518. 'rtl.createClass(this, "TBird", this.TObject, function () {',
  32519. ' this.Run = async function (w) {',
  32520. ' var Result = 0;',
  32521. ' var p = null;',
  32522. ' p = $mod.TObject.Run.apply(this, arguments);',
  32523. ' p = $mod.TObject.Run.call(this, 3);',
  32524. ' p = $mod.TObject.Run.call(this, 3);',
  32525. ' p = $mod.TObject.Run.call(this, 4);',
  32526. ' return p;',
  32527. ' return $mod.TObject.Run.apply(this, arguments);',
  32528. ' return $mod.TObject.Run.call(this, 3);',
  32529. ' return $mod.TObject.Run.call(this, 5);',
  32530. ' return 6;',
  32531. ' return Result;',
  32532. ' };',
  32533. '});',
  32534. '']),
  32535. LinesToStr([
  32536. '']));
  32537. CheckResolverUnexpectedHints();
  32538. end;
  32539. procedure TTestModule.TestAsync_ClassInterface;
  32540. begin
  32541. StartProgram(false);
  32542. Add([
  32543. '{$mode objfpc}',
  32544. '{$modeswitch externalclass}',
  32545. 'type',
  32546. ' TJSPromise = class external name ''Promise''',
  32547. ' end;',
  32548. ' IUnknown = interface',
  32549. ' function _AddRef: longint;',
  32550. ' function _Release: longint;',
  32551. ' end;',
  32552. 'function Say(i: IUnknown): IUnknown; async;',
  32553. 'begin',
  32554. 'end;',
  32555. 'function Run: IUnknown; async;',
  32556. 'begin',
  32557. ' Result:=await(Run);',
  32558. ' Result:=await(Run());',
  32559. ' Result:=await(Run) as IUnknown;',
  32560. ' Result:=await(Say(nil));',
  32561. ' Result:=await(Say(await(Run())));',
  32562. ' Result:=await(Say(await(Run()) as IUnknown));',
  32563. ' Result:=await(Say(await(Run()) as IUnknown)) as IUnknown;',
  32564. 'end;',
  32565. 'procedure Fly;',
  32566. 'var p: TJSPromise;',
  32567. 'begin',
  32568. ' Run;',
  32569. ' Run();',
  32570. ' p:=Run;',
  32571. ' p:=Run();',
  32572. 'end;',
  32573. 'begin',
  32574. ' ']);
  32575. ConvertProgram;
  32576. CheckSource('TestAsync_ClassInterface',
  32577. LinesToStr([ // statements
  32578. 'rtl.createInterface(this, "IUnknown", "{D7ADB0E1-758A-322B-BDDF-21CD521DDFA9}", ["_AddRef", "_Release"], null);',
  32579. 'this.Say = async function (i) {',
  32580. ' var Result = null;',
  32581. ' return Result;',
  32582. '};',
  32583. 'this.Run = async function () {',
  32584. ' var Result = null;',
  32585. ' var $ok = false;',
  32586. ' try {',
  32587. ' Result = rtl.setIntfL(Result, await $mod.Run());',
  32588. ' Result = rtl.setIntfL(Result, await $mod.Run());',
  32589. ' Result = rtl.setIntfL(Result, rtl.intfAsIntfT(await $mod.Run(), $mod.IUnknown));',
  32590. ' Result = rtl.setIntfL(Result, await $mod.Say(null));',
  32591. ' Result = rtl.setIntfL(Result, await $mod.Say(await $mod.Run()));',
  32592. ' Result = rtl.setIntfL(Result, await $mod.Say(rtl.intfAsIntfT(await $mod.Run(), $mod.IUnknown)));',
  32593. ' Result = rtl.setIntfL(Result, rtl.intfAsIntfT(await $mod.Say(rtl.intfAsIntfT(await $mod.Run(), $mod.IUnknown)), $mod.IUnknown));',
  32594. ' $ok = true;',
  32595. ' } finally {',
  32596. ' if (!$ok) rtl._Release(Result);',
  32597. ' };',
  32598. ' return Result;',
  32599. '};',
  32600. 'this.Fly = function () {',
  32601. ' var p = null;',
  32602. ' $mod.Run();',
  32603. ' $mod.Run();',
  32604. ' p = $mod.Run();',
  32605. ' p = $mod.Run();',
  32606. '};',
  32607. '']),
  32608. LinesToStr([
  32609. '']));
  32610. CheckResolverUnexpectedHints();
  32611. end;
  32612. procedure TTestModule.TestAsync_ClassInterface_AsyncMissmatchFail;
  32613. begin
  32614. StartProgram(true,[supTInterfacedObject]);
  32615. Add([
  32616. '{$mode objfpc}',
  32617. '{$modeswitch externalclass}',
  32618. 'type',
  32619. ' TJSPromise = class external name ''Promise''',
  32620. ' end;',
  32621. ' IBird = interface',
  32622. ' procedure Run;',
  32623. ' end;',
  32624. ' TBird = class(TInterfacedObject,IBird)',
  32625. ' procedure Run; async;',
  32626. ' end;',
  32627. 'procedure TBird.Run;',
  32628. 'begin',
  32629. 'end;',
  32630. 'begin',
  32631. ' ']);
  32632. SetExpectedPasResolverError('procedure type modifier "async" mismatch',nXModifierMismatchY);
  32633. ConvertProgram;
  32634. end;
  32635. Initialization
  32636. RegisterTests([TTestModule]);
  32637. end.